global: snapshot

This commit is contained in:
nym21
2025-03-10 11:42:15 +01:00
parent f9f7172702
commit 9428beeae5
44 changed files with 1348 additions and 581 deletions
+1 -8
View File
@@ -1,7 +1,6 @@
use std::ops::Add;
use byteview::ByteView;
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
@@ -15,8 +14,6 @@ use crate::Error;
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
@@ -29,16 +26,12 @@ pub struct Addressindex(u32);
impl Addressindex {
pub const BYTES: usize = size_of::<Self>();
pub fn decremented(self) -> Self {
Self(*self - 1)
}
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn incremented(self) -> Self {
Self(*self + 1)
Self(self.0 + 1)
}
}
@@ -12,8 +12,6 @@ use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
@@ -24,16 +22,12 @@ use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
pub struct Addresstypeindex(u32);
impl Addresstypeindex {
pub fn decremented(self) -> Self {
Self(*self - 1)
}
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn incremented(self) -> Self {
Self(*self + 1)
Self(self.0 + 1)
}
pub fn copy_then_increment(&mut self) -> Self {
+1 -1
View File
@@ -9,6 +9,6 @@ impl Bitcoin {
impl From<Sats> for Bitcoin {
fn from(value: Sats) -> Self {
Self((*value as f64) / Self::ONE.0)
Self(f64::from(value) / Self::ONE.0)
}
}
+6 -2
View File
@@ -1,4 +1,3 @@
use derive_deref::Deref;
use serde::Serialize;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
@@ -13,7 +12,6 @@ use super::Dollars;
Eq,
PartialOrd,
Ord,
Deref,
FromBytes,
Immutable,
IntoBytes,
@@ -27,3 +25,9 @@ impl From<Dollars> for Cents {
Self((*value * 100.0).floor() as u64)
}
}
impl From<Cents> for f64 {
fn from(value: Cents) -> Self {
value.0 as f64
}
}
+9 -5
View File
@@ -60,11 +60,15 @@ impl From<Timestamp> for Date {
impl From<Dateindex> for Date {
fn from(value: Dateindex) -> Self {
Self::from(
Self::INDEX_ZERO_
.checked_add(Span::new().days(i64::from(value)))
.unwrap(),
)
if value == Dateindex::default() {
Date::INDEX_ZERO
} else {
Self::from(
Self::INDEX_ONE_
.checked_add(Span::new().days(i64::from(value) - 1))
.unwrap(),
)
}
}
}
+17 -1
View File
@@ -9,10 +9,26 @@ use crate::Error;
use super::Date;
#[derive(
Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize,
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct Dateindex(u16);
impl Dateindex {
pub const BYTES: usize = size_of::<Self>();
}
impl From<Dateindex> for usize {
fn from(value: Dateindex) -> Self {
value.0 as usize
+10 -2
View File
@@ -4,7 +4,9 @@ use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Cents;
#[derive(Debug, Default, Clone, Copy, Deref, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)]
#[derive(
Debug, Default, Clone, Copy, Deref, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize,
)]
pub struct Dollars(f64);
impl From<f64> for Dollars {
@@ -15,6 +17,12 @@ impl From<f64> for Dollars {
impl From<Cents> for Dollars {
fn from(value: Cents) -> Self {
Self((*value as f64) / 100.0)
Self(f64::from(value) / 100.0)
}
}
impl From<Dollars> for f64 {
fn from(value: Dollars) -> Self {
value.0
}
}
+1 -3
View File
@@ -1,4 +1,2 @@
use derive_deref::Deref;
#[derive(Debug, Deref, Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct Feerate(f32);
+24 -30
View File
@@ -1,19 +1,18 @@
use std::{
fmt::{self, Debug},
ops::{Add, AddAssign, Rem, Sub},
ops::{Add, AddAssign, Rem},
};
use bitcoincore_rpc::{Client, RpcApi};
use derive_deref::{Deref, DerefMut};
use serde::{Deserialize, Serialize};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::CheckedSub;
#[derive(
Debug,
Clone,
Copy,
Deref,
DerefMut,
PartialEq,
Eq,
PartialOrd,
@@ -49,11 +48,11 @@ impl Height {
}
pub fn decrement(&mut self) {
self.0 -= 1;
*self = self.decremented().unwrap();
}
pub fn decremented(self) -> Self {
Self(self.0.checked_sub(1).unwrap_or_default())
pub fn decremented(self) -> Option<Self> {
self.checked_sub(1_u32)
}
pub fn is_zero(self) -> bool {
@@ -63,7 +62,7 @@ impl Height {
impl PartialEq<u64> for Height {
fn eq(&self, other: &u64) -> bool {
**self == *other as u32
self.0 == *other as u32
}
}
@@ -87,35 +86,25 @@ impl Add<usize> for Height {
type Output = Height;
fn add(self, rhs: usize) -> Self::Output {
Self::from(*self + rhs as u32)
Self::from(self.0 + rhs as u32)
}
}
impl Sub<Height> for Height {
type Output = Height;
fn sub(self, rhs: Height) -> Self::Output {
Self::from(*self - *rhs)
impl CheckedSub<Height> for Height {
fn checked_sub(self, rhs: Height) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Height::from)
}
}
impl Sub<i32> for Height {
type Output = Height;
fn sub(self, rhs: i32) -> Self::Output {
Self::from(*self - rhs as u32)
impl CheckedSub<u32> for Height {
fn checked_sub(self, rhs: u32) -> Option<Self> {
self.0.checked_sub(rhs).map(Height::from)
}
}
impl Sub<u32> for Height {
type Output = Height;
fn sub(self, rhs: u32) -> Self::Output {
Self::from(*self - rhs)
}
}
impl Sub<usize> for Height {
type Output = Height;
fn sub(self, rhs: usize) -> Self::Output {
Self::from(*self - rhs as u32)
impl CheckedSub<usize> for Height {
fn checked_sub(self, rhs: usize) -> Option<Self> {
self.0.checked_sub(rhs as u32).map(Height::from)
}
}
@@ -141,7 +130,7 @@ impl Rem<usize> for Height {
impl fmt::Display for Height {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", **self)
write!(f, "{}", self.0)
}
}
@@ -168,6 +157,11 @@ impl From<Height> for usize {
}
}
impl From<Height> for u32 {
fn from(value: Height) -> Self {
value.0
}
}
impl From<Height> for u64 {
fn from(value: Height) -> Self {
value.0 as u64
@@ -189,7 +183,7 @@ impl From<bitcoin::locktime::absolute::Height> for Height {
impl From<Height> for bitcoin::locktime::absolute::Height {
fn from(value: Height) -> Self {
bitcoin::locktime::absolute::Height::from_consensus(*value).unwrap()
bitcoin::locktime::absolute::Height::from_consensus(value.0).unwrap()
}
}
+1
View File
@@ -5,6 +5,7 @@ use super::{Height, Timestamp};
#[derive(Debug, Immutable, Clone, Copy, IntoBytes, KnownLayout, TryFromBytes, Serialize)]
#[repr(C)]
#[allow(warnings)]
pub enum LockTime {
Height(Height),
Timestamp(Timestamp),
+103 -2
View File
@@ -2,9 +2,86 @@ use derive_deref::Deref;
use serde::Serialize;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Cents;
use super::{Cents, Dollars};
pub type OHLCCents = (Open<Cents>, High<Cents>, Low<Cents>, Close<Cents>);
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)]
#[repr(C)]
pub struct OHLCCents {
pub open: Open<Cents>,
pub high: High<Cents>,
pub low: Low<Cents>,
pub close: Close<Cents>,
}
impl From<(Open<Cents>, High<Cents>, Low<Cents>, Close<Cents>)> for OHLCCents {
fn from(value: (Open<Cents>, High<Cents>, Low<Cents>, Close<Cents>)) -> Self {
Self {
open: value.0,
high: value.1,
low: value.2,
close: value.3,
}
}
}
impl From<Close<Cents>> for OHLCCents {
fn from(value: Close<Cents>) -> Self {
Self {
open: Open::from(value),
high: High::from(value),
low: Low::from(value),
close: value,
}
}
}
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)]
#[repr(C)]
pub struct OHLCDollars {
pub open: Open<Dollars>,
pub high: High<Dollars>,
pub low: Low<Dollars>,
pub close: Close<Dollars>,
}
impl From<(Open<Dollars>, High<Dollars>, Low<Dollars>, Close<Dollars>)> for OHLCDollars {
fn from(value: (Open<Dollars>, High<Dollars>, Low<Dollars>, Close<Dollars>)) -> Self {
Self {
open: value.0,
high: value.1,
low: value.2,
close: value.3,
}
}
}
impl From<Close<Dollars>> for OHLCDollars {
fn from(value: Close<Dollars>) -> Self {
Self {
open: Open::from(value),
high: High::from(value),
low: Low::from(value),
close: value,
}
}
}
impl From<OHLCCents> for OHLCDollars {
fn from(value: OHLCCents) -> Self {
Self::from(&value)
}
}
impl From<&OHLCCents> for OHLCDollars {
fn from(value: &OHLCCents) -> Self {
Self {
open: value.open.into(),
high: value.high.into(),
low: value.low.into(),
close: value.close.into(),
}
}
}
#[derive(
Debug,
@@ -39,6 +116,12 @@ where
}
}
impl From<Open<Cents>> for Open<Dollars> {
fn from(value: Open<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
#[derive(
Debug,
Default,
@@ -72,6 +155,12 @@ where
}
}
impl From<High<Cents>> for High<Dollars> {
fn from(value: High<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
#[derive(
Debug,
Default,
@@ -105,6 +194,12 @@ where
}
}
impl From<Low<Cents>> for Low<Dollars> {
fn from(value: Low<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
#[derive(
Debug,
Default,
@@ -128,3 +223,9 @@ impl<T> From<T> for Close<T> {
Self(value)
}
}
impl From<Close<Cents>> for Close<Dollars> {
fn from(value: Close<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
+12 -9
View File
@@ -4,7 +4,6 @@ use std::{
};
use bitcoin::Amount;
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
@@ -18,8 +17,6 @@ use super::Height;
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
@@ -40,7 +37,7 @@ impl Sats {
impl Add for Sats {
type Output = Sats;
fn add(self, rhs: Sats) -> Self::Output {
Sats::from(*self + *rhs)
Sats::from(self.0 + rhs.0)
}
}
@@ -53,7 +50,7 @@ impl AddAssign for Sats {
impl Sub for Sats {
type Output = Sats;
fn sub(self, rhs: Sats) -> Self::Output {
Sats::from(*self - *rhs)
Sats::from(self.0 - rhs.0)
}
}
@@ -66,27 +63,27 @@ impl SubAssign for Sats {
impl Mul<Sats> for Sats {
type Output = Sats;
fn mul(self, rhs: Sats) -> Self::Output {
Sats::from(*self * *rhs)
Sats::from(self.0 * rhs.0)
}
}
impl Mul<u64> for Sats {
type Output = Sats;
fn mul(self, rhs: u64) -> Self::Output {
Sats::from(*self * rhs)
Sats::from(self.0 * rhs)
}
}
impl Mul<Height> for Sats {
type Output = Sats;
fn mul(self, rhs: Height) -> Self::Output {
Sats::from(*self * *rhs as u64)
Sats::from(self.0 * u64::from(rhs))
}
}
impl Sum for Sats {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sats: u64 = iter.map(|sats| *sats).sum();
let sats: u64 = iter.map(|sats| sats.0).sum();
Sats::from(sats)
}
}
@@ -107,3 +104,9 @@ impl From<Sats> for Amount {
Self::from_sat(value.0)
}
}
impl From<Sats> for f64 {
fn from(value: Sats) -> Self {
value.0 as f64
}
}
-4
View File
@@ -30,10 +30,6 @@ impl Txindex {
pub fn incremented(self) -> Self {
Self(*self + 1)
}
pub fn decremented(self) -> Self {
Self(*self - 1)
}
}
impl Add<Txindex> for Txindex {
-4
View File
@@ -29,10 +29,6 @@ impl Txinindex {
pub fn incremented(self) -> Self {
Self(*self + 1)
}
pub fn decremented(self) -> Self {
Self(*self - 1)
}
}
impl Add<Txinindex> for Txinindex {
@@ -32,10 +32,6 @@ impl Txoutindex {
Self(*self + 1)
}
pub fn decremented(self) -> Self {
Self(*self - 1)
}
pub fn is_coinbase(self) -> bool {
self == Self::COINBASE
}