global: one big snapshot

This commit is contained in:
nym21
2025-08-02 16:59:22 +02:00
parent aa8b47a3dd
commit f7aa9424db
252 changed files with 6283 additions and 5264 deletions

View File

@@ -0,0 +1,63 @@
use super::{ByAmountRange, ByGreatEqualAmount, ByLowerThanAmount, GroupFilter};
#[derive(Default, Clone)]
pub struct AddressGroups<T> {
pub ge_amount: ByGreatEqualAmount<T>,
pub amount_range: ByAmountRange<T>,
pub lt_amount: ByLowerThanAmount<T>,
}
impl<T> AddressGroups<T> {
pub fn as_boxed_mut_vecs(&mut self) -> Vec<Box<[&mut T]>> {
vec![
Box::new(self.ge_amount.as_mut_vec()),
Box::new(self.amount_range.as_mut_vec()),
Box::new(self.lt_amount.as_mut_vec()),
]
}
pub fn as_mut_vecs(&mut self) -> Vec<&mut T> {
self.ge_amount
.as_mut_vec()
.into_iter()
.chain(self.amount_range.as_mut_vec())
.chain(self.lt_amount.as_mut_vec())
.collect::<Vec<_>>()
}
pub fn as_mut_separate_vecs(&mut self) -> Vec<&mut T> {
self.amount_range
.as_mut_vec()
.into_iter()
.collect::<Vec<_>>()
}
pub fn as_mut_overlapping_vecs(&mut self) -> Vec<&mut T> {
self.lt_amount
.as_mut_vec()
.into_iter()
.chain(self.ge_amount.as_mut_vec())
.collect::<Vec<_>>()
}
}
impl<T> AddressGroups<(GroupFilter, T)> {
pub fn vecs(&self) -> Vec<&T> {
self.amount_range
.vecs()
.into_iter()
.chain(self.lt_amount.vecs())
.chain(self.ge_amount.vecs())
.collect::<Vec<_>>()
}
}
impl<T> From<AddressGroups<T>> for AddressGroups<(GroupFilter, T)> {
fn from(value: AddressGroups<T>) -> Self {
Self {
amount_range: ByAmountRange::from(value.amount_range),
lt_amount: ByLowerThanAmount::from(value.lt_amount),
ge_amount: ByGreatEqualAmount::from(value.ge_amount),
}
}
}

View File

@@ -0,0 +1,181 @@
use std::{
mem,
ops::{Add, AddAssign},
};
use super::GroupFilter;
use crate::OutputType;
#[derive(Default, Clone, Debug)]
pub struct ByAddressType<T> {
pub p2pk65: T,
pub p2pk33: T,
pub p2pkh: T,
pub p2sh: T,
pub p2wpkh: T,
pub p2wsh: T,
pub p2tr: T,
pub p2a: T,
}
impl<T> ByAddressType<T> {
pub fn get_unwrap(&self, address_type: OutputType) -> &T {
self.get(address_type).unwrap()
}
pub fn get(&self, address_type: OutputType) -> Option<&T> {
match address_type {
OutputType::P2PK65 => Some(&self.p2pk65),
OutputType::P2PK33 => Some(&self.p2pk33),
OutputType::P2PKH => Some(&self.p2pkh),
OutputType::P2SH => Some(&self.p2sh),
OutputType::P2WPKH => Some(&self.p2wpkh),
OutputType::P2WSH => Some(&self.p2wsh),
OutputType::P2TR => Some(&self.p2tr),
OutputType::P2A => Some(&self.p2a),
_ => None,
}
}
pub fn get_mut(&mut self, address_type: OutputType) -> Option<&mut T> {
match address_type {
OutputType::P2PK65 => Some(&mut self.p2pk65),
OutputType::P2PK33 => Some(&mut self.p2pk33),
OutputType::P2PKH => Some(&mut self.p2pkh),
OutputType::P2SH => Some(&mut self.p2sh),
OutputType::P2WPKH => Some(&mut self.p2wpkh),
OutputType::P2WSH => Some(&mut self.p2wsh),
OutputType::P2TR => Some(&mut self.p2tr),
OutputType::P2A => Some(&mut self.p2a),
_ => None,
}
}
pub fn as_mut_vec(&mut self) -> [&mut T; 8] {
[
&mut self.p2pk65,
&mut self.p2pk33,
&mut self.p2pkh,
&mut self.p2sh,
&mut self.p2wpkh,
&mut self.p2wsh,
&mut self.p2tr,
&mut self.p2a,
]
}
pub fn as_typed_vec(&self) -> [(OutputType, &T); 8] {
[
(OutputType::P2PK65, &self.p2pk65),
(OutputType::P2PK33, &self.p2pk33),
(OutputType::P2PKH, &self.p2pkh),
(OutputType::P2SH, &self.p2sh),
(OutputType::P2WPKH, &self.p2wpkh),
(OutputType::P2WSH, &self.p2wsh),
(OutputType::P2TR, &self.p2tr),
(OutputType::P2A, &self.p2a),
]
}
pub fn as_mut_typed_vec(&mut self) -> [(OutputType, &mut T); 8] {
[
(OutputType::P2PK65, &mut self.p2pk65),
(OutputType::P2PK33, &mut self.p2pk33),
(OutputType::P2PKH, &mut self.p2pkh),
(OutputType::P2SH, &mut self.p2sh),
(OutputType::P2WPKH, &mut self.p2wpkh),
(OutputType::P2WSH, &mut self.p2wsh),
(OutputType::P2TR, &mut self.p2tr),
(OutputType::P2A, &mut self.p2a),
]
}
pub fn into_typed_vec(&mut self) -> [(OutputType, T); 8]
where
T: Default,
{
[
(OutputType::P2PK65, mem::take(&mut self.p2pk65)),
(OutputType::P2PK33, mem::take(&mut self.p2pk33)),
(OutputType::P2PKH, mem::take(&mut self.p2pkh)),
(OutputType::P2SH, mem::take(&mut self.p2sh)),
(OutputType::P2WPKH, mem::take(&mut self.p2wpkh)),
(OutputType::P2WSH, mem::take(&mut self.p2wsh)),
(OutputType::P2TR, mem::take(&mut self.p2tr)),
(OutputType::P2A, mem::take(&mut self.p2a)),
]
}
}
impl<T> ByAddressType<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 8] {
[
&self.p2pk65.1,
&self.p2pk33.1,
&self.p2pkh.1,
&self.p2sh.1,
&self.p2wpkh.1,
&self.p2wsh.1,
&self.p2tr.1,
&self.p2a.1,
]
}
}
impl<T> From<ByAddressType<T>> for ByAddressType<(GroupFilter, T)> {
fn from(value: ByAddressType<T>) -> Self {
Self {
p2pk65: (GroupFilter::Type(OutputType::P2PK65), value.p2pk65),
p2pk33: (GroupFilter::Type(OutputType::P2PK33), value.p2pk33),
p2pkh: (GroupFilter::Type(OutputType::P2PKH), value.p2pkh),
p2sh: (GroupFilter::Type(OutputType::P2SH), value.p2sh),
p2wpkh: (GroupFilter::Type(OutputType::P2WPKH), value.p2wpkh),
p2wsh: (GroupFilter::Type(OutputType::P2WSH), value.p2wsh),
p2tr: (GroupFilter::Type(OutputType::P2TR), value.p2tr),
p2a: (GroupFilter::Type(OutputType::P2A), value.p2a),
}
}
}
impl<T> Add for ByAddressType<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
p2pk65: self.p2pk65 + rhs.p2pk65,
p2pk33: self.p2pk33 + rhs.p2pk33,
p2pkh: self.p2pkh + rhs.p2pkh,
p2sh: self.p2sh + rhs.p2sh,
p2wpkh: self.p2wpkh + rhs.p2wpkh,
p2wsh: self.p2wsh + rhs.p2wsh,
p2tr: self.p2tr + rhs.p2tr,
p2a: self.p2a + rhs.p2a,
}
}
}
impl<T> AddAssign for ByAddressType<T>
where
T: AddAssign,
{
fn add_assign(&mut self, rhs: Self) {
self.p2pk65 += rhs.p2pk65;
self.p2pk33 += rhs.p2pk33;
self.p2pkh += rhs.p2pkh;
self.p2sh += rhs.p2sh;
self.p2wpkh += rhs.p2wpkh;
self.p2wsh += rhs.p2wsh;
self.p2tr += rhs.p2tr;
self.p2a += rhs.p2a;
}
}
impl<T> ByAddressType<Option<T>> {
pub fn take(&mut self) {
self.as_mut_vec().into_iter().for_each(|opt| {
opt.take();
});
}
}

View File

@@ -0,0 +1,131 @@
use super::GroupFilter;
#[derive(Default, Clone)]
pub struct ByAgeRange<T> {
pub up_to_1d: T,
pub _1d_to_1w: T,
pub _1w_to_1m: T,
pub _1m_to_2m: T,
pub _2m_to_3m: T,
pub _3m_to_4m: T,
pub _4m_to_5m: T,
pub _5m_to_6m: T,
pub _6m_to_1y: T,
pub _1y_to_2y: T,
pub _2y_to_3y: T,
pub _3y_to_4y: T,
pub _4y_to_5y: T,
pub _5y_to_6y: T,
pub _6y_to_7y: T,
pub _7y_to_8y: T,
pub _8y_to_10y: T,
pub _10y_to_12y: T,
pub _12y_to_15y: T,
pub from_15y: T,
}
impl<T> From<ByAgeRange<T>> for ByAgeRange<(GroupFilter, T)> {
fn from(value: ByAgeRange<T>) -> Self {
Self {
up_to_1d: (GroupFilter::LowerThan(1), value.up_to_1d),
_1d_to_1w: (GroupFilter::Range(1..7), value._1d_to_1w),
_1w_to_1m: (GroupFilter::Range(7..30), value._1w_to_1m),
_1m_to_2m: (GroupFilter::Range(30..2 * 30), value._1m_to_2m),
_2m_to_3m: (GroupFilter::Range(2 * 30..3 * 30), value._2m_to_3m),
_3m_to_4m: (GroupFilter::Range(3 * 30..4 * 30), value._3m_to_4m),
_4m_to_5m: (GroupFilter::Range(4 * 30..5 * 30), value._4m_to_5m),
_5m_to_6m: (GroupFilter::Range(5 * 30..6 * 30), value._5m_to_6m),
_6m_to_1y: (GroupFilter::Range(6 * 30..365), value._6m_to_1y),
_1y_to_2y: (GroupFilter::Range(365..2 * 365), value._1y_to_2y),
_2y_to_3y: (GroupFilter::Range(2 * 365..3 * 365), value._2y_to_3y),
_3y_to_4y: (GroupFilter::Range(3 * 365..4 * 365), value._3y_to_4y),
_4y_to_5y: (GroupFilter::Range(4 * 365..5 * 365), value._4y_to_5y),
_5y_to_6y: (GroupFilter::Range(5 * 365..6 * 365), value._5y_to_6y),
_6y_to_7y: (GroupFilter::Range(6 * 365..7 * 365), value._6y_to_7y),
_7y_to_8y: (GroupFilter::Range(7 * 365..8 * 365), value._7y_to_8y),
_8y_to_10y: (GroupFilter::Range(8 * 365..10 * 365), value._8y_to_10y),
_10y_to_12y: (GroupFilter::Range(10 * 365..12 * 365), value._10y_to_12y),
_12y_to_15y: (GroupFilter::Range(12 * 365..15 * 365), value._12y_to_15y),
from_15y: (GroupFilter::GreaterOrEqual(15 * 365), value.from_15y),
}
}
}
impl<T> ByAgeRange<T> {
pub fn as_vec(&mut self) -> [&T; 20] {
[
&self.up_to_1d,
&self._1d_to_1w,
&self._1w_to_1m,
&self._1m_to_2m,
&self._2m_to_3m,
&self._3m_to_4m,
&self._4m_to_5m,
&self._5m_to_6m,
&self._6m_to_1y,
&self._1y_to_2y,
&self._2y_to_3y,
&self._3y_to_4y,
&self._4y_to_5y,
&self._5y_to_6y,
&self._6y_to_7y,
&self._7y_to_8y,
&self._8y_to_10y,
&self._10y_to_12y,
&self._12y_to_15y,
&self.from_15y,
]
}
pub fn as_mut_vec(&mut self) -> [&mut T; 20] {
[
&mut self.up_to_1d,
&mut self._1d_to_1w,
&mut self._1w_to_1m,
&mut self._1m_to_2m,
&mut self._2m_to_3m,
&mut self._3m_to_4m,
&mut self._4m_to_5m,
&mut self._5m_to_6m,
&mut self._6m_to_1y,
&mut self._1y_to_2y,
&mut self._2y_to_3y,
&mut self._3y_to_4y,
&mut self._4y_to_5y,
&mut self._5y_to_6y,
&mut self._6y_to_7y,
&mut self._7y_to_8y,
&mut self._8y_to_10y,
&mut self._10y_to_12y,
&mut self._12y_to_15y,
&mut self.from_15y,
]
}
}
impl<T> ByAgeRange<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 20] {
[
&self.up_to_1d.1,
&self._1d_to_1w.1,
&self._1w_to_1m.1,
&self._1m_to_2m.1,
&self._2m_to_3m.1,
&self._3m_to_4m.1,
&self._4m_to_5m.1,
&self._5m_to_6m.1,
&self._6m_to_1y.1,
&self._1y_to_2y.1,
&self._2y_to_3y.1,
&self._3y_to_4y.1,
&self._4y_to_5y.1,
&self._5y_to_6y.1,
&self._6y_to_7y.1,
&self._7y_to_8y.1,
&self._8y_to_10y.1,
&self._10y_to_12y.1,
&self._12y_to_15y.1,
&self.from_15y.1,
]
}
}

View File

@@ -0,0 +1,257 @@
use std::ops::{Add, AddAssign};
use crate::Sats;
use super::GroupFilter;
#[derive(Debug, Default, Clone)]
pub struct ByAmountRange<T> {
pub _0sats: T,
pub _1sat_to_10sats: T,
pub _10sats_to_100sats: T,
pub _100sats_to_1k_sats: T,
pub _1k_sats_to_10k_sats: T,
pub _10k_sats_to_100k_sats: T,
pub _100k_sats_to_1m_sats: T,
pub _1m_sats_to_10m_sats: T,
pub _10m_sats_to_1btc: T,
pub _1btc_to_10btc: T,
pub _10btc_to_100btc: T,
pub _100btc_to_1k_btc: T,
pub _1k_btc_to_10k_btc: T,
pub _10k_btc_to_100k_btc: T,
pub _100k_btc_or_more: T,
}
impl<T> From<ByAmountRange<T>> for ByAmountRange<(GroupFilter, T)> {
fn from(value: ByAmountRange<T>) -> Self {
#[allow(clippy::inconsistent_digit_grouping)]
Self {
_0sats: (GroupFilter::LowerThan(Sats::_1.into()), value._0sats),
_1sat_to_10sats: (
GroupFilter::Range(Sats::_1.into()..Sats::_10.into()),
value._1sat_to_10sats,
),
_10sats_to_100sats: (
GroupFilter::Range(Sats::_10.into()..Sats::_100.into()),
value._10sats_to_100sats,
),
_100sats_to_1k_sats: (
GroupFilter::Range(Sats::_100.into()..Sats::_1K.into()),
value._100sats_to_1k_sats,
),
_1k_sats_to_10k_sats: (
GroupFilter::Range(Sats::_1K.into()..Sats::_10K.into()),
value._1k_sats_to_10k_sats,
),
_10k_sats_to_100k_sats: (
GroupFilter::Range(Sats::_10K.into()..Sats::_100K.into()),
value._10k_sats_to_100k_sats,
),
_100k_sats_to_1m_sats: (
GroupFilter::Range(Sats::_100K.into()..Sats::_1M.into()),
value._100k_sats_to_1m_sats,
),
_1m_sats_to_10m_sats: (
GroupFilter::Range(Sats::_1M.into()..Sats::_10M.into()),
value._1m_sats_to_10m_sats,
),
_10m_sats_to_1btc: (
GroupFilter::Range(Sats::_10M.into()..Sats::_1BTC.into()),
value._10m_sats_to_1btc,
),
_1btc_to_10btc: (
GroupFilter::Range(Sats::_1BTC.into()..Sats::_10BTC.into()),
value._1btc_to_10btc,
),
_10btc_to_100btc: (
GroupFilter::Range(Sats::_10BTC.into()..Sats::_100BTC.into()),
value._10btc_to_100btc,
),
_100btc_to_1k_btc: (
GroupFilter::Range(Sats::_100BTC.into()..Sats::_1K_BTC.into()),
value._100btc_to_1k_btc,
),
_1k_btc_to_10k_btc: (
GroupFilter::Range(Sats::_1K_BTC.into()..Sats::_10K_BTC.into()),
value._1k_btc_to_10k_btc,
),
_10k_btc_to_100k_btc: (
GroupFilter::Range(Sats::_10K_BTC.into()..Sats::_100K_BTC.into()),
value._10k_btc_to_100k_btc,
),
_100k_btc_or_more: (
GroupFilter::GreaterOrEqual(Sats::_100K_BTC.into()),
value._100k_btc_or_more,
),
}
}
}
impl<T> ByAmountRange<T> {
#[allow(clippy::inconsistent_digit_grouping)]
pub fn get_mut(&mut self, value: Sats) -> &mut T {
if value == Sats::ZERO {
&mut self._0sats
} else if value < Sats::_10 {
&mut self._1sat_to_10sats
} else if value < Sats::_100 {
&mut self._10sats_to_100sats
} else if value < Sats::_1K {
&mut self._100sats_to_1k_sats
} else if value < Sats::_10K {
&mut self._1k_sats_to_10k_sats
} else if value < Sats::_100K {
&mut self._10k_sats_to_100k_sats
} else if value < Sats::_1M {
&mut self._100k_sats_to_1m_sats
} else if value < Sats::_10M {
&mut self._1m_sats_to_10m_sats
} else if value < Sats::_1BTC {
&mut self._10m_sats_to_1btc
} else if value < Sats::_10BTC {
&mut self._1btc_to_10btc
} else if value < Sats::_100BTC {
&mut self._10btc_to_100btc
} else if value < Sats::_1K_BTC {
&mut self._100btc_to_1k_btc
} else if value < Sats::_10K_BTC {
&mut self._1k_btc_to_10k_btc
} else if value < Sats::_100K_BTC {
&mut self._10k_btc_to_100k_btc
} else {
&mut self._100k_btc_or_more
}
}
pub fn as_vec(&self) -> [&T; 15] {
[
&self._0sats,
&self._1sat_to_10sats,
&self._10sats_to_100sats,
&self._100sats_to_1k_sats,
&self._1k_sats_to_10k_sats,
&self._10k_sats_to_100k_sats,
&self._100k_sats_to_1m_sats,
&self._1m_sats_to_10m_sats,
&self._10m_sats_to_1btc,
&self._1btc_to_10btc,
&self._10btc_to_100btc,
&self._100btc_to_1k_btc,
&self._1k_btc_to_10k_btc,
&self._10k_btc_to_100k_btc,
&self._100k_btc_or_more,
]
}
pub fn as_typed_vec(&self) -> [(Sats, &T); 15] {
[
(Sats::ZERO, &self._0sats),
(Sats::_1, &self._1sat_to_10sats),
(Sats::_10, &self._10sats_to_100sats),
(Sats::_100, &self._100sats_to_1k_sats),
(Sats::_1K, &self._1k_sats_to_10k_sats),
(Sats::_10K, &self._10k_sats_to_100k_sats),
(Sats::_100K, &self._100k_sats_to_1m_sats),
(Sats::_1M, &self._1m_sats_to_10m_sats),
(Sats::_10M, &self._10m_sats_to_1btc),
(Sats::_1BTC, &self._1btc_to_10btc),
(Sats::_10BTC, &self._10btc_to_100btc),
(Sats::_100BTC, &self._100btc_to_1k_btc),
(Sats::_1K_BTC, &self._1k_btc_to_10k_btc),
(Sats::_10K_BTC, &self._10k_btc_to_100k_btc),
(Sats::_100K_BTC, &self._100k_btc_or_more),
]
}
pub fn as_mut_vec(&mut self) -> [&mut T; 15] {
[
&mut self._0sats,
&mut self._1sat_to_10sats,
&mut self._10sats_to_100sats,
&mut self._100sats_to_1k_sats,
&mut self._1k_sats_to_10k_sats,
&mut self._10k_sats_to_100k_sats,
&mut self._100k_sats_to_1m_sats,
&mut self._1m_sats_to_10m_sats,
&mut self._10m_sats_to_1btc,
&mut self._1btc_to_10btc,
&mut self._10btc_to_100btc,
&mut self._100btc_to_1k_btc,
&mut self._1k_btc_to_10k_btc,
&mut self._10k_btc_to_100k_btc,
&mut self._100k_btc_or_more,
]
}
}
impl<T> ByAmountRange<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 15] {
[
&self._0sats.1,
&self._1sat_to_10sats.1,
&self._10sats_to_100sats.1,
&self._100sats_to_1k_sats.1,
&self._1k_sats_to_10k_sats.1,
&self._10k_sats_to_100k_sats.1,
&self._100k_sats_to_1m_sats.1,
&self._1m_sats_to_10m_sats.1,
&self._10m_sats_to_1btc.1,
&self._1btc_to_10btc.1,
&self._10btc_to_100btc.1,
&self._100btc_to_1k_btc.1,
&self._1k_btc_to_10k_btc.1,
&self._10k_btc_to_100k_btc.1,
&self._100k_btc_or_more.1,
]
}
}
impl<T> Add for ByAmountRange<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
_0sats: self._0sats + rhs._0sats,
_1sat_to_10sats: self._1sat_to_10sats + rhs._1sat_to_10sats,
_10sats_to_100sats: self._10sats_to_100sats + rhs._10sats_to_100sats,
_100sats_to_1k_sats: self._100sats_to_1k_sats + rhs._100sats_to_1k_sats,
_1k_sats_to_10k_sats: self._1k_sats_to_10k_sats + rhs._1k_sats_to_10k_sats,
_10k_sats_to_100k_sats: self._10k_sats_to_100k_sats + rhs._10k_sats_to_100k_sats,
_100k_sats_to_1m_sats: self._100k_sats_to_1m_sats + rhs._100k_sats_to_1m_sats,
_1m_sats_to_10m_sats: self._1m_sats_to_10m_sats + rhs._1m_sats_to_10m_sats,
_10m_sats_to_1btc: self._10m_sats_to_1btc + rhs._10m_sats_to_1btc,
_1btc_to_10btc: self._1btc_to_10btc + rhs._1btc_to_10btc,
_10btc_to_100btc: self._10btc_to_100btc + rhs._10btc_to_100btc,
_100btc_to_1k_btc: self._100btc_to_1k_btc + rhs._100btc_to_1k_btc,
_1k_btc_to_10k_btc: self._1k_btc_to_10k_btc + rhs._1k_btc_to_10k_btc,
_10k_btc_to_100k_btc: self._10k_btc_to_100k_btc + rhs._10k_btc_to_100k_btc,
_100k_btc_or_more: self._100k_btc_or_more + rhs._100k_btc_or_more,
}
}
}
impl<T> AddAssign for ByAmountRange<T>
where
T: AddAssign,
{
fn add_assign(&mut self, rhs: Self) {
self._0sats += rhs._0sats;
self._1sat_to_10sats += rhs._1sat_to_10sats;
self._10sats_to_100sats += rhs._10sats_to_100sats;
self._100sats_to_1k_sats += rhs._100sats_to_1k_sats;
self._1k_sats_to_10k_sats += rhs._1k_sats_to_10k_sats;
self._10k_sats_to_100k_sats += rhs._10k_sats_to_100k_sats;
self._100k_sats_to_1m_sats += rhs._100k_sats_to_1m_sats;
self._1m_sats_to_10m_sats += rhs._1m_sats_to_10m_sats;
self._10m_sats_to_1btc += rhs._10m_sats_to_1btc;
self._1btc_to_10btc += rhs._1btc_to_10btc;
self._10btc_to_100btc += rhs._10btc_to_100btc;
self._100btc_to_1k_btc += rhs._100btc_to_1k_btc;
self._1k_btc_to_10k_btc += rhs._1k_btc_to_10k_btc;
self._10k_btc_to_100k_btc += rhs._10k_btc_to_100k_btc;
self._100k_btc_or_more += rhs._100k_btc_or_more;
}
}

View File

@@ -0,0 +1,12 @@
#[derive(Debug, Default)]
pub struct ByAnyAddress<T> {
pub loaded: T,
pub empty: T,
}
impl<T> ByAnyAddress<Option<T>> {
pub fn take(&mut self) {
self.loaded.take();
self.empty.take();
}
}

View File

@@ -0,0 +1,59 @@
use crate::{HalvingEpoch, Height};
use super::GroupFilter;
#[derive(Default, Clone)]
pub struct ByEpoch<T> {
pub _0: T,
pub _1: T,
pub _2: T,
pub _3: T,
pub _4: T,
}
impl<T> From<ByEpoch<T>> for ByEpoch<(GroupFilter, T)> {
fn from(value: ByEpoch<T>) -> Self {
Self {
_0: (GroupFilter::Epoch(HalvingEpoch::new(0)), value._0),
_1: (GroupFilter::Epoch(HalvingEpoch::new(1)), value._1),
_2: (GroupFilter::Epoch(HalvingEpoch::new(2)), value._2),
_3: (GroupFilter::Epoch(HalvingEpoch::new(3)), value._3),
_4: (GroupFilter::Epoch(HalvingEpoch::new(4)), value._4),
}
}
}
impl<T> ByEpoch<T> {
pub fn as_mut_vec(&mut self) -> [&mut T; 5] {
[
&mut self._0,
&mut self._1,
&mut self._2,
&mut self._3,
&mut self._4,
]
}
pub fn mut_vec_from_height(&mut self, height: Height) -> &mut T {
let epoch = HalvingEpoch::from(height);
if epoch == HalvingEpoch::new(0) {
&mut self._0
} else if epoch == HalvingEpoch::new(1) {
&mut self._1
} else if epoch == HalvingEpoch::new(2) {
&mut self._2
} else if epoch == HalvingEpoch::new(3) {
&mut self._3
} else if epoch == HalvingEpoch::new(4) {
&mut self._4
} else {
todo!("")
}
}
}
impl<T> ByEpoch<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 5] {
[&self._0.1, &self._1.1, &self._2.1, &self._3.1, &self._4.1]
}
}

View File

@@ -0,0 +1,110 @@
use crate::Sats;
use super::GroupFilter;
#[derive(Default, Clone)]
pub struct ByGreatEqualAmount<T> {
pub _1sat: T,
pub _10sats: T,
pub _100sats: T,
pub _1k_sats: T,
pub _10k_sats: T,
pub _100k_sats: T,
pub _1m_sats: T,
pub _10m_sats: T,
pub _1btc: T,
pub _10btc: T,
pub _100btc: T,
pub _1k_btc: T,
pub _10k_btc: T,
}
impl<T> ByGreatEqualAmount<T> {
pub fn as_mut_vec(&mut self) -> [&mut T; 13] {
[
&mut self._1sat,
&mut self._10sats,
&mut self._100sats,
&mut self._1k_sats,
&mut self._10k_sats,
&mut self._100k_sats,
&mut self._1m_sats,
&mut self._10m_sats,
&mut self._1btc,
&mut self._10btc,
&mut self._100btc,
&mut self._1k_btc,
&mut self._10k_btc,
]
}
}
impl<T> ByGreatEqualAmount<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 13] {
[
&self._1sat.1,
&self._10sats.1,
&self._100sats.1,
&self._1k_sats.1,
&self._10k_sats.1,
&self._100k_sats.1,
&self._1m_sats.1,
&self._10m_sats.1,
&self._1btc.1,
&self._10btc.1,
&self._100btc.1,
&self._1k_btc.1,
&self._10k_btc.1,
]
}
}
impl<T> From<ByGreatEqualAmount<T>> for ByGreatEqualAmount<(GroupFilter, T)> {
fn from(value: ByGreatEqualAmount<T>) -> Self {
Self {
_1sat: (GroupFilter::GreaterOrEqual(Sats::_1.into()), value._1sat),
_10sats: (GroupFilter::GreaterOrEqual(Sats::_10.into()), value._10sats),
_100sats: (
GroupFilter::GreaterOrEqual(Sats::_100.into()),
value._100sats,
),
_1k_sats: (
GroupFilter::GreaterOrEqual(Sats::_1K.into()),
value._1k_sats,
),
_10k_sats: (
GroupFilter::GreaterOrEqual(Sats::_10K.into()),
value._10k_sats,
),
_100k_sats: (
GroupFilter::GreaterOrEqual(Sats::_100K.into()),
value._100k_sats,
),
_1m_sats: (
GroupFilter::GreaterOrEqual(Sats::_1M.into()),
value._1m_sats,
),
_10m_sats: (
GroupFilter::GreaterOrEqual(Sats::_10M.into()),
value._10m_sats,
),
_1btc: (GroupFilter::GreaterOrEqual(Sats::_1BTC.into()), value._1btc),
_10btc: (
GroupFilter::GreaterOrEqual(Sats::_10BTC.into()),
value._10btc,
),
_100btc: (
GroupFilter::GreaterOrEqual(Sats::_100BTC.into()),
value._100btc,
),
_1k_btc: (
GroupFilter::GreaterOrEqual(Sats::_1K_BTC.into()),
value._1k_btc,
),
_10k_btc: (
GroupFilter::GreaterOrEqual(Sats::_10K_BTC.into()),
value._10k_btc,
),
}
}
}

View File

@@ -0,0 +1,86 @@
use crate::Sats;
use super::GroupFilter;
#[derive(Default, Clone)]
pub struct ByLowerThanAmount<T> {
pub _10sats: T,
pub _100sats: T,
pub _1k_sats: T,
pub _10k_sats: T,
pub _100k_sats: T,
pub _1m_sats: T,
pub _10m_sats: T,
pub _1btc: T,
pub _10btc: T,
pub _100btc: T,
pub _1k_btc: T,
pub _10k_btc: T,
pub _100k_btc: T,
}
impl<T> ByLowerThanAmount<T> {
pub fn as_mut_vec(&mut self) -> [&mut T; 13] {
[
&mut self._10sats,
&mut self._100sats,
&mut self._1k_sats,
&mut self._10k_sats,
&mut self._100k_sats,
&mut self._1m_sats,
&mut self._10m_sats,
&mut self._1btc,
&mut self._10btc,
&mut self._100btc,
&mut self._1k_btc,
&mut self._10k_btc,
&mut self._100k_btc,
]
}
}
impl<T> ByLowerThanAmount<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 13] {
[
&self._10sats.1,
&self._100sats.1,
&self._1k_sats.1,
&self._10k_sats.1,
&self._100k_sats.1,
&self._1m_sats.1,
&self._10m_sats.1,
&self._1btc.1,
&self._10btc.1,
&self._100btc.1,
&self._1k_btc.1,
&self._10k_btc.1,
&self._100k_btc.1,
]
}
}
impl<T> From<ByLowerThanAmount<T>> for ByLowerThanAmount<(GroupFilter, T)> {
fn from(value: ByLowerThanAmount<T>) -> Self {
Self {
_10sats: (GroupFilter::LowerThan(Sats::_10.into()), value._10sats),
_100sats: (GroupFilter::LowerThan(Sats::_100.into()), value._100sats),
_1k_sats: (GroupFilter::LowerThan(Sats::_1K.into()), value._1k_sats),
_10k_sats: (GroupFilter::LowerThan(Sats::_10K.into()), value._10k_sats),
_100k_sats: (GroupFilter::LowerThan(Sats::_100K.into()), value._100k_sats),
_1m_sats: (GroupFilter::LowerThan(Sats::_1M.into()), value._1m_sats),
_10m_sats: (GroupFilter::LowerThan(Sats::_10M.into()), value._10m_sats),
_1btc: (GroupFilter::LowerThan(Sats::_1BTC.into()), value._1btc),
_10btc: (GroupFilter::LowerThan(Sats::_10BTC.into()), value._10btc),
_100btc: (GroupFilter::LowerThan(Sats::_100BTC.into()), value._100btc),
_1k_btc: (GroupFilter::LowerThan(Sats::_1K_BTC.into()), value._1k_btc),
_10k_btc: (
GroupFilter::LowerThan(Sats::_10K_BTC.into()),
value._10k_btc,
),
_100k_btc: (
GroupFilter::LowerThan(Sats::_100K_BTC.into()),
value._100k_btc,
),
}
}
}

View File

@@ -0,0 +1,98 @@
use super::GroupFilter;
#[derive(Default, Clone)]
pub struct ByMaxAge<T> {
pub _1w: T,
pub _1m: T,
pub _2m: T,
pub _3m: T,
pub _4m: T,
pub _5m: T,
pub _6m: T,
pub _1y: T,
pub _2y: T,
pub _3y: T,
pub _4y: T,
pub _5y: T,
pub _6y: T,
pub _7y: T,
pub _8y: T,
pub _10y: T,
pub _12y: T,
pub _15y: T,
}
impl<T> ByMaxAge<T> {
pub fn as_mut_vec(&mut self) -> [&mut T; 18] {
[
&mut self._1w,
&mut self._1m,
&mut self._2m,
&mut self._3m,
&mut self._4m,
&mut self._5m,
&mut self._6m,
&mut self._1y,
&mut self._2y,
&mut self._3y,
&mut self._4y,
&mut self._5y,
&mut self._6y,
&mut self._7y,
&mut self._8y,
&mut self._10y,
&mut self._12y,
&mut self._15y,
]
}
}
impl<T> ByMaxAge<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 18] {
[
&self._1w.1,
&self._1m.1,
&self._2m.1,
&self._3m.1,
&self._4m.1,
&self._5m.1,
&self._6m.1,
&self._1y.1,
&self._2y.1,
&self._3y.1,
&self._4y.1,
&self._5y.1,
&self._6y.1,
&self._7y.1,
&self._8y.1,
&self._10y.1,
&self._12y.1,
&self._15y.1,
]
}
}
impl<T> From<ByMaxAge<T>> for ByMaxAge<(GroupFilter, T)> {
fn from(value: ByMaxAge<T>) -> Self {
Self {
_1w: (GroupFilter::LowerThan(7), value._1w),
_1m: (GroupFilter::LowerThan(30), value._1m),
_2m: (GroupFilter::LowerThan(2 * 30), value._2m),
_3m: (GroupFilter::LowerThan(3 * 30), value._3m),
_4m: (GroupFilter::LowerThan(4 * 30), value._4m),
_5m: (GroupFilter::LowerThan(5 * 30), value._5m),
_6m: (GroupFilter::LowerThan(6 * 30), value._6m),
_1y: (GroupFilter::LowerThan(365), value._1y),
_2y: (GroupFilter::LowerThan(2 * 365), value._2y),
_3y: (GroupFilter::LowerThan(3 * 365), value._3y),
_4y: (GroupFilter::LowerThan(4 * 365), value._4y),
_5y: (GroupFilter::LowerThan(5 * 365), value._5y),
_6y: (GroupFilter::LowerThan(6 * 365), value._6y),
_7y: (GroupFilter::LowerThan(7 * 365), value._7y),
_8y: (GroupFilter::LowerThan(8 * 365), value._8y),
_10y: (GroupFilter::LowerThan(10 * 365), value._10y),
_12y: (GroupFilter::LowerThan(12 * 365), value._12y),
_15y: (GroupFilter::LowerThan(15 * 365), value._15y),
}
}
}

View File

@@ -0,0 +1,98 @@
use super::GroupFilter;
#[derive(Default, Clone)]
pub struct ByMinAge<T> {
pub _1d: T,
pub _1w: T,
pub _1m: T,
pub _2m: T,
pub _3m: T,
pub _4m: T,
pub _5m: T,
pub _6m: T,
pub _1y: T,
pub _2y: T,
pub _3y: T,
pub _4y: T,
pub _5y: T,
pub _6y: T,
pub _7y: T,
pub _8y: T,
pub _10y: T,
pub _12y: T,
}
impl<T> ByMinAge<T> {
pub fn as_mut_vec(&mut self) -> [&mut T; 18] {
[
&mut self._1d,
&mut self._1w,
&mut self._1m,
&mut self._2m,
&mut self._3m,
&mut self._4m,
&mut self._5m,
&mut self._6m,
&mut self._1y,
&mut self._2y,
&mut self._3y,
&mut self._4y,
&mut self._5y,
&mut self._6y,
&mut self._7y,
&mut self._8y,
&mut self._10y,
&mut self._12y,
]
}
}
impl<T> ByMinAge<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 18] {
[
&self._1d.1,
&self._1w.1,
&self._1m.1,
&self._2m.1,
&self._3m.1,
&self._4m.1,
&self._5m.1,
&self._6m.1,
&self._1y.1,
&self._2y.1,
&self._3y.1,
&self._4y.1,
&self._5y.1,
&self._6y.1,
&self._7y.1,
&self._8y.1,
&self._10y.1,
&self._12y.1,
]
}
}
impl<T> From<ByMinAge<T>> for ByMinAge<(GroupFilter, T)> {
fn from(value: ByMinAge<T>) -> Self {
Self {
_1d: (GroupFilter::GreaterOrEqual(1), value._1d),
_1w: (GroupFilter::GreaterOrEqual(7), value._1w),
_1m: (GroupFilter::GreaterOrEqual(30), value._1m),
_2m: (GroupFilter::GreaterOrEqual(2 * 30), value._2m),
_3m: (GroupFilter::GreaterOrEqual(3 * 30), value._3m),
_4m: (GroupFilter::GreaterOrEqual(4 * 30), value._4m),
_5m: (GroupFilter::GreaterOrEqual(5 * 30), value._5m),
_6m: (GroupFilter::GreaterOrEqual(6 * 30), value._6m),
_1y: (GroupFilter::GreaterOrEqual(365), value._1y),
_2y: (GroupFilter::GreaterOrEqual(2 * 365), value._2y),
_3y: (GroupFilter::GreaterOrEqual(3 * 365), value._3y),
_4y: (GroupFilter::GreaterOrEqual(4 * 365), value._4y),
_5y: (GroupFilter::GreaterOrEqual(5 * 365), value._5y),
_6y: (GroupFilter::GreaterOrEqual(6 * 365), value._6y),
_7y: (GroupFilter::GreaterOrEqual(7 * 365), value._7y),
_8y: (GroupFilter::GreaterOrEqual(8 * 365), value._8y),
_10y: (GroupFilter::GreaterOrEqual(10 * 365), value._10y),
_12y: (GroupFilter::GreaterOrEqual(12 * 365), value._12y),
}
}
}

View File

@@ -0,0 +1,148 @@
use std::ops::{Add, AddAssign};
use crate::OutputType;
use super::GroupFilter;
#[derive(Default, Clone, Debug)]
pub struct BySpendableType<T> {
pub p2pk65: T,
pub p2pk33: T,
pub p2pkh: T,
pub p2ms: T,
pub p2sh: T,
pub p2wpkh: T,
pub p2wsh: T,
pub p2tr: T,
pub p2a: T,
pub unknown: T,
pub empty: T,
}
impl<T> BySpendableType<T> {
pub fn get_mut(&mut self, output_type: OutputType) -> &mut T {
match output_type {
OutputType::P2PK65 => &mut self.p2pk65,
OutputType::P2PK33 => &mut self.p2pk33,
OutputType::P2PKH => &mut self.p2pkh,
OutputType::P2MS => &mut self.p2ms,
OutputType::P2SH => &mut self.p2sh,
OutputType::P2WPKH => &mut self.p2wpkh,
OutputType::P2WSH => &mut self.p2wsh,
OutputType::P2TR => &mut self.p2tr,
OutputType::P2A => &mut self.p2a,
OutputType::Unknown => &mut self.unknown,
OutputType::Empty => &mut self.empty,
_ => unreachable!(),
}
}
pub fn as_mut_vec(&mut self) -> [&mut T; 11] {
[
&mut self.p2pk65,
&mut self.p2pk33,
&mut self.p2pkh,
&mut self.p2ms,
&mut self.p2sh,
&mut self.p2wpkh,
&mut self.p2wsh,
&mut self.p2tr,
&mut self.p2a,
&mut self.unknown,
&mut self.empty,
]
}
pub fn as_typed_vec(&self) -> [(OutputType, &T); 11] {
[
(OutputType::P2PK65, &self.p2pk65),
(OutputType::P2PK33, &self.p2pk33),
(OutputType::P2PKH, &self.p2pkh),
(OutputType::P2MS, &self.p2ms),
(OutputType::P2SH, &self.p2sh),
(OutputType::P2WPKH, &self.p2wpkh),
(OutputType::P2WSH, &self.p2wsh),
(OutputType::P2TR, &self.p2tr),
(OutputType::P2A, &self.p2a),
(OutputType::Unknown, &self.unknown),
(OutputType::Empty, &self.empty),
]
}
}
impl<T> BySpendableType<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 11] {
[
&self.p2pk65.1,
&self.p2pk33.1,
&self.p2pkh.1,
&self.p2ms.1,
&self.p2sh.1,
&self.p2wpkh.1,
&self.p2wsh.1,
&self.p2tr.1,
&self.p2a.1,
&self.unknown.1,
&self.empty.1,
]
}
}
impl<T> From<BySpendableType<T>> for BySpendableType<(GroupFilter, T)> {
fn from(value: BySpendableType<T>) -> Self {
Self {
p2pk65: (GroupFilter::Type(OutputType::P2PK65), value.p2pk65),
p2pk33: (GroupFilter::Type(OutputType::P2PK33), value.p2pk33),
p2pkh: (GroupFilter::Type(OutputType::P2PKH), value.p2pkh),
p2ms: (GroupFilter::Type(OutputType::P2MS), value.p2ms),
p2sh: (GroupFilter::Type(OutputType::P2SH), value.p2sh),
p2wpkh: (GroupFilter::Type(OutputType::P2WPKH), value.p2wpkh),
p2wsh: (GroupFilter::Type(OutputType::P2WSH), value.p2wsh),
p2tr: (GroupFilter::Type(OutputType::P2TR), value.p2tr),
p2a: (GroupFilter::Type(OutputType::P2A), value.p2a),
unknown: (GroupFilter::Type(OutputType::Unknown), value.unknown),
empty: (GroupFilter::Type(OutputType::Empty), value.empty),
}
}
}
impl<T> Add for BySpendableType<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
p2pk65: self.p2pk65 + rhs.p2pk65,
p2pk33: self.p2pk33 + rhs.p2pk33,
p2pkh: self.p2pkh + rhs.p2pkh,
p2ms: self.p2ms + rhs.p2ms,
p2sh: self.p2sh + rhs.p2sh,
p2wpkh: self.p2wpkh + rhs.p2wpkh,
p2wsh: self.p2wsh + rhs.p2wsh,
p2tr: self.p2tr + rhs.p2tr,
p2a: self.p2a + rhs.p2a,
unknown: self.unknown + rhs.unknown,
empty: self.empty + rhs.empty,
}
}
}
impl<T> AddAssign for BySpendableType<T>
where
T: AddAssign,
{
fn add_assign(&mut self, rhs: Self) {
self.p2pk65 += rhs.p2pk65;
self.p2pk33 += rhs.p2pk33;
self.p2pkh += rhs.p2pkh;
self.p2ms += rhs.p2ms;
self.p2sh += rhs.p2sh;
self.p2wpkh += rhs.p2wpkh;
self.p2wsh += rhs.p2wsh;
self.p2tr += rhs.p2tr;
self.p2a += rhs.p2a;
self.unknown += rhs.unknown;
self.empty += rhs.empty;
}
}

View File

@@ -0,0 +1,28 @@
use super::GroupFilter;
#[derive(Default, Clone)]
pub struct ByTerm<T> {
pub short: T,
pub long: T,
}
impl<T> ByTerm<T> {
pub fn as_mut_vec(&mut self) -> [&mut T; 2] {
[&mut self.short, &mut self.long]
}
}
impl<T> ByTerm<(GroupFilter, T)> {
pub fn vecs(&self) -> [&T; 2] {
[&self.short.1, &self.long.1]
}
}
impl<T> From<ByTerm<T>> for ByTerm<(GroupFilter, T)> {
fn from(value: ByTerm<T>) -> Self {
Self {
short: (GroupFilter::LowerThan(5 * 30), value.short),
long: (GroupFilter::GreaterOrEqual(5 * 30), value.long),
}
}
}

View File

@@ -0,0 +1,72 @@
use std::ops::{Add, AddAssign};
use crate::OutputType;
use super::{BySpendableType, ByUnspendableType};
#[derive(Default, Clone, Debug)]
pub struct GroupedByType<T> {
pub spendable: BySpendableType<T>,
pub unspendable: ByUnspendableType<T>,
}
impl<T> GroupedByType<T> {
pub fn get(&self, output_type: OutputType) -> &T {
match output_type {
OutputType::P2PK65 => &self.spendable.p2pk65,
OutputType::P2PK33 => &self.spendable.p2pk33,
OutputType::P2PKH => &self.spendable.p2pkh,
OutputType::P2MS => &self.spendable.p2ms,
OutputType::P2SH => &self.spendable.p2sh,
OutputType::P2WPKH => &self.spendable.p2wpkh,
OutputType::P2WSH => &self.spendable.p2wsh,
OutputType::P2TR => &self.spendable.p2tr,
OutputType::P2A => &self.spendable.p2a,
OutputType::Empty => &self.spendable.empty,
OutputType::Unknown => &self.spendable.unknown,
OutputType::OpReturn => &self.unspendable.opreturn,
_ => unreachable!(),
}
}
pub fn get_mut(&mut self, output_type: OutputType) -> &mut T {
match output_type {
OutputType::P2PK65 => &mut self.spendable.p2pk65,
OutputType::P2PK33 => &mut self.spendable.p2pk33,
OutputType::P2PKH => &mut self.spendable.p2pkh,
OutputType::P2MS => &mut self.spendable.p2ms,
OutputType::P2SH => &mut self.spendable.p2sh,
OutputType::P2WPKH => &mut self.spendable.p2wpkh,
OutputType::P2WSH => &mut self.spendable.p2wsh,
OutputType::P2TR => &mut self.spendable.p2tr,
OutputType::P2A => &mut self.spendable.p2a,
OutputType::Unknown => &mut self.spendable.unknown,
OutputType::Empty => &mut self.spendable.empty,
OutputType::OpReturn => &mut self.unspendable.opreturn,
_ => unreachable!(),
}
}
}
impl<T> Add for GroupedByType<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
spendable: self.spendable + rhs.spendable,
unspendable: self.unspendable + rhs.unspendable,
}
}
}
impl<T> AddAssign for GroupedByType<T>
where
T: AddAssign,
{
fn add_assign(&mut self, rhs: Self) {
self.spendable += rhs.spendable;
self.unspendable += rhs.unspendable;
}
}

View File

@@ -0,0 +1,33 @@
use std::ops::{Add, AddAssign};
#[derive(Default, Clone, Debug)]
pub struct ByUnspendableType<T> {
pub opreturn: T,
}
impl<T> ByUnspendableType<T> {
pub fn as_vec(&self) -> [&T; 1] {
[&self.opreturn]
}
}
impl<T> Add for ByUnspendableType<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
opreturn: self.opreturn + rhs.opreturn,
}
}
}
impl<T> AddAssign for ByUnspendableType<T>
where
T: AddAssign,
{
fn add_assign(&mut self, rhs: Self) {
self.opreturn += rhs.opreturn;
}
}

View File

@@ -0,0 +1,37 @@
#[derive(Default, Clone)]
pub struct GroupedByValue<T> {
pub up_to_1cent: T,
pub from_1c_to_10c: T,
pub from_10c_to_1d: T,
pub from_1d_to_10d: T,
pub from_10usd_to_100usd: T,
pub from_100usd_to_1_000usd: T,
pub from_1_000usd_to_10_000usd: T,
pub from_10_000usd_to_100_000usd: T,
pub from_100_000usd_to_1_000_000usd: T,
pub from_1_000_000usd_to_10_000_000usd: T,
pub from_10_000_000usd_to_100_000_000usd: T,
pub from_100_000_000usd_to_1_000_000_000usd: T,
pub from_1_000_000_000usd: T,
// ...
}
impl<T> GroupedByValue<T> {
pub fn as_mut_vec(&mut self) -> Vec<&mut T> {
vec![
&mut self.up_to_1cent,
&mut self.from_1c_to_10c,
&mut self.from_10c_to_1d,
&mut self.from_1d_to_10d,
&mut self.from_10usd_to_100usd,
&mut self.from_100usd_to_1_000usd,
&mut self.from_1_000usd_to_10_000usd,
&mut self.from_10_000usd_to_100_000usd,
&mut self.from_100_000usd_to_1_000_000usd,
&mut self.from_1_000_000usd_to_10_000_000usd,
&mut self.from_10_000_000usd_to_100_000_000usd,
&mut self.from_100_000_000usd_to_1_000_000_000usd,
&mut self.from_1_000_000_000usd,
]
}
}

View File

@@ -0,0 +1,48 @@
use std::ops::Range;
use crate::{HalvingEpoch, OutputType};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GroupFilter {
All,
LowerThan(usize),
Range(Range<usize>),
GreaterOrEqual(usize),
Epoch(HalvingEpoch),
Type(OutputType),
}
impl GroupFilter {
pub fn contains(&self, value: usize) -> bool {
match self {
GroupFilter::Range(r) => r.contains(&value),
GroupFilter::LowerThan(max) => *max > value,
GroupFilter::GreaterOrEqual(min) => *min <= value,
GroupFilter::All => true,
GroupFilter::Epoch(_) | GroupFilter::Type(_) => false,
}
}
pub fn includes(&self, other: &GroupFilter) -> bool {
match self {
GroupFilter::All => true,
GroupFilter::LowerThan(max) => match other {
GroupFilter::LowerThan(max2) => max >= max2,
GroupFilter::Range(range) => range.end <= *max,
GroupFilter::All
| GroupFilter::GreaterOrEqual(_)
| GroupFilter::Epoch(_)
| GroupFilter::Type(_) => false,
},
GroupFilter::GreaterOrEqual(min) => match other {
GroupFilter::Range(range) => range.start >= *min,
GroupFilter::GreaterOrEqual(min2) => min <= min2,
GroupFilter::All
| GroupFilter::LowerThan(_)
| GroupFilter::Epoch(_)
| GroupFilter::Type(_) => false,
},
GroupFilter::Range(_) | GroupFilter::Epoch(_) | GroupFilter::Type(_) => false,
}
}
}

View File

@@ -0,0 +1,33 @@
mod address;
mod by_address_type;
mod by_age_range;
mod by_amount_range;
mod by_any_address;
mod by_epoch;
mod by_ge_amount;
mod by_lt_amount;
mod by_max_age;
mod by_min_age;
mod by_spendable_type;
mod by_term;
mod by_type;
mod by_unspendable_type;
mod filter;
mod utxo;
pub use address::*;
pub use by_address_type::*;
pub use by_age_range::*;
pub use by_amount_range::*;
pub use by_any_address::*;
pub use by_epoch::*;
pub use by_ge_amount::*;
pub use by_lt_amount::*;
pub use by_max_age::*;
pub use by_min_age::*;
pub use by_spendable_type::*;
pub use by_term::*;
pub use by_type::*;
pub use by_unspendable_type::*;
pub use filter::*;
pub use utxo::*;

View File

@@ -0,0 +1,105 @@
use crate::{
ByAgeRange, ByAmountRange, ByEpoch, ByGreatEqualAmount, ByLowerThanAmount, ByMaxAge, ByMinAge,
BySpendableType, ByTerm, GroupFilter,
};
#[derive(Default, Clone)]
pub struct UTXOGroups<T> {
pub all: T,
pub age_range: ByAgeRange<T>,
pub epoch: ByEpoch<T>,
pub min_age: ByMinAge<T>,
pub ge_amount: ByGreatEqualAmount<T>,
pub amount_range: ByAmountRange<T>,
pub term: ByTerm<T>,
pub _type: BySpendableType<T>,
pub max_age: ByMaxAge<T>,
pub lt_amount: ByLowerThanAmount<T>,
}
impl<T> UTXOGroups<T> {
pub fn as_boxed_mut_vecs(&mut self) -> Vec<Box<[&mut T]>> {
vec![
Box::new([&mut self.all]),
Box::new(self.term.as_mut_vec()),
Box::new(self.max_age.as_mut_vec()),
Box::new(self.min_age.as_mut_vec()),
Box::new(self.ge_amount.as_mut_vec()),
Box::new(self.age_range.as_mut_vec()),
Box::new(self.epoch.as_mut_vec()),
Box::new(self.amount_range.as_mut_vec()),
Box::new(self.lt_amount.as_mut_vec()),
Box::new(self._type.as_mut_vec()),
]
}
pub fn as_mut_vecs(&mut self) -> Vec<&mut T> {
[&mut self.all]
.into_iter()
.chain(self.term.as_mut_vec())
.chain(self.max_age.as_mut_vec())
.chain(self.min_age.as_mut_vec())
.chain(self.ge_amount.as_mut_vec())
.chain(self.age_range.as_mut_vec())
.chain(self.epoch.as_mut_vec())
.chain(self.amount_range.as_mut_vec())
.chain(self.lt_amount.as_mut_vec())
.chain(self._type.as_mut_vec())
.collect::<Vec<_>>()
}
pub fn as_mut_separate_vecs(&mut self) -> Vec<&mut T> {
self.age_range
.as_mut_vec()
.into_iter()
.chain(self.epoch.as_mut_vec())
.chain(self.amount_range.as_mut_vec())
.chain(self._type.as_mut_vec())
.collect::<Vec<_>>()
}
pub fn as_mut_overlapping_vecs(&mut self) -> Vec<&mut T> {
[&mut self.all]
.into_iter()
.chain(self.term.as_mut_vec())
.chain(self.max_age.as_mut_vec())
.chain(self.min_age.as_mut_vec())
.chain(self.lt_amount.as_mut_vec())
.chain(self.ge_amount.as_mut_vec())
.collect::<Vec<_>>()
}
}
impl<T> UTXOGroups<(GroupFilter, T)> {
pub fn vecs(&self) -> Vec<&T> {
[&self.all.1]
.into_iter()
.chain(self.term.vecs())
.chain(self.max_age.vecs())
.chain(self.min_age.vecs())
.chain(self.age_range.vecs())
.chain(self.epoch.vecs())
.chain(self.amount_range.vecs())
.chain(self._type.vecs())
.chain(self.lt_amount.vecs())
.chain(self.ge_amount.vecs())
.collect::<Vec<_>>()
}
}
impl<T> From<UTXOGroups<T>> for UTXOGroups<(GroupFilter, T)> {
fn from(value: UTXOGroups<T>) -> Self {
Self {
all: (GroupFilter::All, value.all),
term: ByTerm::from(value.term),
max_age: ByMaxAge::from(value.max_age),
min_age: ByMinAge::from(value.min_age),
age_range: ByAgeRange::from(value.age_range),
epoch: ByEpoch::from(value.epoch),
amount_range: ByAmountRange::from(value.amount_range),
lt_amount: ByLowerThanAmount::from(value.lt_amount),
ge_amount: ByGreatEqualAmount::from(value.ge_amount),
_type: BySpendableType::from(value._type),
}
}
}

View File

@@ -0,0 +1,9 @@
#![doc = include_str!("../README.md")]
mod groups;
mod structs;
pub use groups::*;
pub use structs::*;
pub use brk_vecs::{CheckedSub, Exit, Printable, Version};

View File

@@ -0,0 +1,448 @@
use std::fmt;
use bitcoin::{
Address, Network, ScriptBuf,
hex::{Case, DisplayHex},
opcodes,
script::Builder,
};
use brk_error::Error;
use derive_deref::{Deref, DerefMut};
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::OutputType;
#[derive(Debug, PartialEq, Eq)]
pub enum AddressBytes {
P2PK65(P2PK65Bytes),
P2PK33(P2PK33Bytes),
P2PKH(P2PKHBytes),
P2SH(P2SHBytes),
P2WPKH(P2WPKHBytes),
P2WSH(P2WSHBytes),
P2TR(P2TRBytes),
P2A(P2ABytes),
}
impl AddressBytes {
pub fn as_slice(&self) -> &[u8] {
match self {
AddressBytes::P2PK65(bytes) => &bytes[..],
AddressBytes::P2PK33(bytes) => &bytes[..],
AddressBytes::P2PKH(bytes) => &bytes[..],
AddressBytes::P2SH(bytes) => &bytes[..],
AddressBytes::P2WPKH(bytes) => &bytes[..],
AddressBytes::P2WSH(bytes) => &bytes[..],
AddressBytes::P2TR(bytes) => &bytes[..],
AddressBytes::P2A(bytes) => &bytes[..],
}
}
}
impl TryFrom<(&ScriptBuf, OutputType)> for AddressBytes {
type Error = Error;
fn try_from(tuple: (&ScriptBuf, OutputType)) -> Result<Self, Self::Error> {
let (script, outputtype) = tuple;
match outputtype {
OutputType::P2PK65 => {
let bytes = script.as_bytes();
let bytes = match bytes.len() {
67 => &bytes[1..66],
_ => {
dbg!(bytes);
return Err(Error::WrongLength);
}
};
Ok(Self::P2PK65(P2PK65Bytes(U8x65::from(bytes))))
}
OutputType::P2PK33 => {
let bytes = script.as_bytes();
let bytes = match bytes.len() {
35 => &bytes[1..34],
_ => {
dbg!(bytes);
return Err(Error::WrongLength);
}
};
Ok(Self::P2PK33(P2PK33Bytes(U8x33::from(bytes))))
}
OutputType::P2PKH => {
let bytes = &script.as_bytes()[3..23];
Ok(Self::P2PKH(P2PKHBytes(U8x20::from(bytes))))
}
OutputType::P2SH => {
let bytes = &script.as_bytes()[2..22];
Ok(Self::P2SH(P2SHBytes(U8x20::from(bytes))))
}
OutputType::P2WPKH => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2WPKH(P2WPKHBytes(U8x20::from(bytes))))
}
OutputType::P2WSH => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2WSH(P2WSHBytes(U8x32::from(bytes))))
}
OutputType::P2TR => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2TR(P2TRBytes(U8x32::from(bytes))))
}
OutputType::P2A => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2A(P2ABytes(U8x2::from(bytes))))
}
OutputType::P2MS => Err(Error::WrongAddressType),
OutputType::Unknown => Err(Error::WrongAddressType),
OutputType::Empty => Err(Error::WrongAddressType),
OutputType::OpReturn => Err(Error::WrongAddressType),
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2PK65Bytes(U8x65);
impl fmt::Display for P2PK65Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex_string(Case::Lower))
}
}
impl Serialize for P2PK65Bytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2PK65Bytes> for AddressBytes {
fn from(value: P2PK65Bytes) -> Self {
Self::P2PK65(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2PK33Bytes(U8x33);
impl fmt::Display for P2PK33Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex_string(Case::Lower))
}
}
impl Serialize for P2PK33Bytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2PK33Bytes> for AddressBytes {
fn from(value: P2PK33Bytes) -> Self {
Self::P2PK33(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2PKHBytes(U8x20);
impl fmt::Display for P2PKHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new()
.push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(*self.0)
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2PKHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2PKHBytes> for AddressBytes {
fn from(value: P2PKHBytes) -> Self {
Self::P2PKH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2SHBytes(U8x20);
impl fmt::Display for P2SHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new()
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(*self.0)
.push_opcode(opcodes::all::OP_EQUAL)
.into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2SHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2SHBytes> for AddressBytes {
fn from(value: P2SHBytes) -> Self {
Self::P2SH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2WPKHBytes(U8x20);
impl fmt::Display for P2WPKHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(0).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2WPKHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2WPKHBytes> for AddressBytes {
fn from(value: P2WPKHBytes) -> Self {
Self::P2WPKH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2WSHBytes(U8x32);
impl fmt::Display for P2WSHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(0).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2WSHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2WSHBytes> for AddressBytes {
fn from(value: P2WSHBytes) -> Self {
Self::P2WSH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2TRBytes(U8x32);
impl fmt::Display for P2TRBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(1).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2TRBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2TRBytes> for AddressBytes {
fn from(value: P2TRBytes) -> Self {
Self::P2TR(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2ABytes(U8x2);
impl fmt::Display for P2ABytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(1).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2ABytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2ABytes> for AddressBytes {
fn from(value: P2ABytes) -> Self {
Self::P2A(value)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x2([u8; 2]);
impl From<&[u8]> for U8x2 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 2];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x20([u8; 20]);
impl From<&[u8]> for U8x20 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 20];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x32([u8; 32]);
impl From<&[u8]> for U8x32 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 32];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x33(#[serde(with = "serde_bytes")] [u8; 33]);
impl From<&[u8]> for U8x33 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 33];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x64(#[serde(with = "serde_bytes")] [u8; 64]);
impl From<&[u8]> for U8x64 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 64];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x65(#[serde(with = "serde_bytes")] [u8; 65]);
impl From<&[u8]> for U8x65 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 65];
arr.copy_from_slice(slice);
Self(arr)
}
}

View File

@@ -0,0 +1,54 @@
use byteview::ByteView;
use derive_deref::Deref;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{AddressBytes, OutputType};
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
)]
pub struct AddressBytesHash([u8; 8]);
impl From<(&AddressBytes, OutputType)> for AddressBytesHash {
fn from((address_bytes, outputtype): (&AddressBytes, OutputType)) -> Self {
let mut slice = rapidhash::v3::rapidhash_v3(address_bytes.as_slice()).to_le_bytes();
slice[0] = slice[0].wrapping_add(outputtype as u8);
Self(slice)
}
}
impl From<[u8; 8]> for AddressBytesHash {
fn from(value: [u8; 8]) -> Self {
Self(value)
}
}
impl From<ByteView> for AddressBytesHash {
fn from(value: ByteView) -> Self {
Self::read_from_bytes(&value).unwrap()
}
}
impl From<&AddressBytesHash> for ByteView {
fn from(value: &AddressBytesHash) -> Self {
Self::new(value.as_bytes())
}
}
impl From<AddressBytesHash> for ByteView {
fn from(value: AddressBytesHash) -> Self {
Self::from(&value)
}
}

View File

@@ -0,0 +1,61 @@
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{
TypeIndex,
structs::{EmptyAddressIndex, LoadedAddressIndex},
};
const MIN_EMPTY_INDEX: u32 = u32::MAX - 4_000_000_000;
#[derive(
Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout,
)]
pub struct AnyAddressIndex(TypeIndex);
impl AnyAddressIndex {
pub fn to_enum(&self) -> AnyAddressDataIndexEnum {
AnyAddressDataIndexEnum::from(*self)
}
}
impl From<LoadedAddressIndex> for AnyAddressIndex {
fn from(value: LoadedAddressIndex) -> Self {
if u32::from(value) >= MIN_EMPTY_INDEX {
panic!("")
}
Self(*value)
}
}
impl From<EmptyAddressIndex> for AnyAddressIndex {
fn from(value: EmptyAddressIndex) -> Self {
Self(*value + MIN_EMPTY_INDEX)
}
}
impl Serialize for AnyAddressIndex {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.to_enum().serialize(serializer)
}
}
#[derive(Debug, Serialize)]
pub enum AnyAddressDataIndexEnum {
Loaded(LoadedAddressIndex),
Empty(EmptyAddressIndex),
}
impl From<AnyAddressIndex> for AnyAddressDataIndexEnum {
fn from(value: AnyAddressIndex) -> Self {
let uvalue = u32::from(value.0);
if uvalue >= MIN_EMPTY_INDEX {
Self::Empty(EmptyAddressIndex::from(uvalue - MIN_EMPTY_INDEX))
} else {
Self::Loaded(LoadedAddressIndex::from(value.0))
}
}
}

View File

@@ -0,0 +1,134 @@
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Div, Mul},
};
use brk_vecs::{CheckedSub, StoredCompressed};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Sats, StoredF64};
#[derive(
Debug,
Default,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct Bitcoin(f64);
impl Add for Bitcoin {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(Sats::from(self) + Sats::from(rhs))
}
}
impl AddAssign for Bitcoin {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Mul for Bitcoin {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::from(Sats::from(self) * Sats::from(rhs))
}
}
impl Mul<usize> for Bitcoin {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Self::from(Sats::from(self) * rhs)
}
}
impl Div<Bitcoin> for Bitcoin {
type Output = StoredF64;
fn div(self, rhs: Bitcoin) -> Self::Output {
StoredF64::from(self.0 / rhs.0)
// Self::from(Sats::from(self) / Sats::from(rhs))
}
}
impl Div<usize> for Bitcoin {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(Sats::from(self) / rhs)
}
}
impl From<Sats> for Bitcoin {
fn from(value: Sats) -> Self {
Self(f64::from(value) / (f64::from(Sats::ONE_BTC)))
}
}
impl From<f64> for Bitcoin {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<StoredF64> for Bitcoin {
fn from(value: StoredF64) -> Self {
Self(*value)
}
}
impl From<Bitcoin> for f64 {
fn from(value: Bitcoin) -> Self {
value.0
}
}
impl From<usize> for Bitcoin {
fn from(value: usize) -> Self {
Self(value as f64)
}
}
impl PartialEq for Bitcoin {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for Bitcoin {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for Bitcoin {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Bitcoin {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}
impl CheckedSub<usize> for Bitcoin {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Self(self.0 - rhs as f64))
}
}

View File

@@ -0,0 +1,52 @@
use std::{fmt, mem};
use bitcoin::hashes::Hash;
use bitcoincore_rpc::{Client, RpcApi};
use derive_deref::Deref;
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Height;
#[derive(Debug, Deref, Clone, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct BlockHash([u8; 32]);
impl From<bitcoin::BlockHash> for BlockHash {
fn from(value: bitcoin::BlockHash) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<BlockHash> for bitcoin::BlockHash {
fn from(value: BlockHash) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<&BlockHash> for bitcoin::BlockHash {
fn from(value: &BlockHash) -> Self {
bitcoin::BlockHash::from_slice(&value.0).unwrap()
}
}
impl TryFrom<(&Client, Height)> for BlockHash {
type Error = bitcoincore_rpc::Error;
fn try_from((rpc, height): (&Client, Height)) -> Result<Self, Self::Error> {
Ok(Self::from(rpc.get_block_hash(u64::from(height))?))
}
}
impl fmt::Display for BlockHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", bitcoin::BlockHash::from(self))
}
}
impl Serialize for BlockHash {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

View File

@@ -0,0 +1,54 @@
use byteview::ByteView;
use derive_deref::Deref;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_8bytes;
use super::BlockHash;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
)]
pub struct BlockHashPrefix([u8; 8]);
impl From<BlockHash> for BlockHashPrefix {
fn from(value: BlockHash) -> Self {
Self::from(&value)
}
}
impl From<&BlockHash> for BlockHashPrefix {
fn from(value: &BlockHash) -> Self {
Self(copy_first_8bytes(&value[..]).unwrap())
}
}
impl From<ByteView> for BlockHashPrefix {
fn from(value: ByteView) -> Self {
Self::read_from_bytes(&value).unwrap()
}
}
impl From<&BlockHashPrefix> for ByteView {
fn from(value: &BlockHashPrefix) -> Self {
Self::new(value.as_bytes())
}
}
impl From<BlockHashPrefix> for ByteView {
fn from(value: BlockHashPrefix) -> Self {
Self::from(&value)
}
}

View File

@@ -0,0 +1,169 @@
use std::ops::{Add, Div, Mul};
use brk_vecs::{CheckedSub, StoredCompressed};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Dollars;
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct Cents(i64);
const SIGNIFICANT_DIGITS: i32 = 4;
impl Cents {
pub const fn mint(value: i64) -> Self {
Self(value)
}
pub fn round_to_4_digits(self) -> Self {
let v = self.0;
let ilog10 = v.checked_ilog10().unwrap_or(0) as i32;
Self::from(if ilog10 >= SIGNIFICANT_DIGITS {
let log_diff = ilog10 - SIGNIFICANT_DIGITS + 1;
let pow = 10.0_f64.powi(log_diff);
((v as f64 / pow).round() * pow) as i64
} else {
v
})
}
}
impl From<Dollars> for Cents {
fn from(value: Dollars) -> Self {
Self((*value * 100.0).round() as i64)
}
}
impl From<Cents> for f64 {
fn from(value: Cents) -> Self {
value.0 as f64
}
}
impl From<i64> for Cents {
fn from(value: i64) -> Self {
Self(value)
}
}
impl From<u64> for Cents {
fn from(value: u64) -> Self {
Self(value as i64)
}
}
impl From<Cents> for usize {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!()
}
value.0 as usize
}
}
impl From<usize> for Cents {
fn from(value: usize) -> Self {
Self(value as i64)
}
}
impl From<Cents> for i64 {
fn from(value: Cents) -> Self {
value.0
}
}
impl From<Cents> for u64 {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!("Shouldn't convert neg cents to u64")
}
value.0 as u64
}
}
impl Add for Cents {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Div<Cents> for Cents {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}
impl Div<usize> for Cents {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as i64)
}
}
impl From<u128> for Cents {
fn from(value: u128) -> Self {
if value > i64::MAX as u128 {
panic!("u128 bigger than i64")
}
Self(value as i64)
}
}
impl From<Cents> for u128 {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!("Shouldn't convert neg cents to u128")
}
value.0 as u128
}
}
impl Mul<Cents> for Cents {
type Output = Cents;
fn mul(self, rhs: Cents) -> Self::Output {
Self(self.0.checked_mul(rhs.0).unwrap())
}
}
impl Mul<i64> for Cents {
type Output = Cents;
fn mul(self, rhs: i64) -> Self::Output {
Self(self.0 * rhs)
}
}
impl Mul<usize> for Cents {
type Output = Cents;
fn mul(self, rhs: usize) -> Self::Output {
Self(self.0 * rhs as i64)
}
}
impl CheckedSub for Cents {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Cents::from)
}
}

View File

@@ -0,0 +1,110 @@
use brk_vecs::StoredCompressed;
use jiff::{Span, civil::Date as Date_, tz::TimeZone};
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{DateIndex, Timestamp};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct Date(u32);
impl Date {
pub const INDEX_ZERO: Self = Self(20090103);
pub const INDEX_ZERO_: Date_ = Date_::constant(2009, 1, 3);
pub const INDEX_ONE: Self = Self(20090109);
pub const INDEX_ONE_: Date_ = Date_::constant(2009, 1, 9);
pub const MIN_RATIO: Self = Self(20120101);
pub fn new(year: u16, month: u8, day: u8) -> Self {
Self(year as u32 * 1_00_00 + month as u32 * 1_00 + day as u32)
}
pub fn year(&self) -> u16 {
(self.0 / 1_00_00) as u16
}
pub fn month(&self) -> u8 {
((self.0 % 1_00_00) / 1_00) as u8
}
pub fn day(&self) -> u8 {
(self.0 % 1_00) as u8
}
pub fn into_jiff(self) -> Date_ {
self.into()
}
}
impl Default for Date {
fn default() -> Self {
Self::INDEX_ZERO
}
}
impl From<Date_> for Date {
fn from(value: Date_) -> Self {
Self::new(value.year() as u16, value.month() as u8, value.day() as u8)
}
}
impl From<Date> for Date_ {
fn from(value: Date) -> Self {
Self::new(value.year() as i16, value.month() as i8, value.day() as i8).unwrap()
}
}
impl From<Timestamp> for Date {
fn from(value: Timestamp) -> Self {
Self::from(Date_::from(
jiff::Timestamp::from(value).to_zoned(TimeZone::UTC),
))
}
}
impl From<DateIndex> for Date {
fn from(value: DateIndex) -> Self {
if value == DateIndex::default() {
Date::INDEX_ZERO
} else {
Self::from(
Self::INDEX_ONE_
.checked_add(Span::new().days(i64::from(value) - 1))
.unwrap(),
)
}
}
}
impl std::fmt::Display for Date {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
"{}-{:0>2}-{:0>2}",
self.year(),
self.month(),
self.day()
))
}
}
impl Serialize for Date {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

View File

@@ -0,0 +1,246 @@
use std::{
fmt,
ops::{Add, Rem},
};
use brk_error::Error;
use brk_vecs::{CheckedSub, FromCoarserIndex, Printable, StoredCompressed};
use jiff::Span;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{DecadeIndex, MonthIndex, QuarterIndex, SemesterIndex, WeekIndex, YearIndex};
use super::Date;
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
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
}
}
impl From<DateIndex> for u64 {
fn from(value: DateIndex) -> Self {
value.0 as u64
}
}
impl From<usize> for DateIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<DateIndex> for i64 {
fn from(value: DateIndex) -> Self {
value.0 as i64
}
}
impl Add<usize> for DateIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u16)
}
}
impl TryFrom<Date> for DateIndex {
type Error = Error;
fn try_from(value: Date) -> Result<Self, Self::Error> {
let value_ = jiff::civil::Date::from(value);
if value_ < Date::INDEX_ZERO_ {
Err(Error::UnindexableDate)
} else if value == Date::INDEX_ZERO {
Ok(Self(0))
} else if value_ < Date::INDEX_ONE_ {
Err(Error::UnindexableDate)
} else if value == Date::INDEX_ONE {
Ok(Self(1))
} else {
Ok(Self(Date::INDEX_ONE_.until(value_)?.get_days() as u16 + 1))
}
}
}
impl CheckedSub for DateIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Rem<usize> for DateIndex {
type Output = Self;
fn rem(self, rhs: usize) -> Self::Output {
Self(self.0 % rhs as u16)
}
}
impl fmt::Display for DateIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Printable for DateIndex {
fn to_string() -> &'static str {
"dateindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["d", "date", "dateindex"]
}
}
impl FromCoarserIndex<WeekIndex> for DateIndex {
fn min_from(coarser: WeekIndex) -> usize {
let coarser = usize::from(coarser);
if coarser == 0 {
0
} else if coarser == 1 {
1
} else {
4 + (coarser - 2) * 7
}
}
fn max_from_(coarser: WeekIndex) -> usize {
let coarser = usize::from(coarser);
if coarser == 0 {
0
} else if coarser == 1 {
3
} else {
3 + (coarser - 1) * 7
}
}
}
impl FromCoarserIndex<MonthIndex> for DateIndex {
fn min_from(coarser: MonthIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
let d = Date::new(2009, 1, 1)
.into_jiff()
.checked_add(Span::new().months(coarser))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
fn max_from_(coarser: MonthIndex) -> usize {
let d = Date::new(2009, 1, 31)
.into_jiff()
.checked_add(Span::new().months(u16::from(coarser)))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
impl FromCoarserIndex<QuarterIndex> for DateIndex {
fn min_from(coarser: QuarterIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
let d = Date::new(2009, 1, 1)
.into_jiff()
.checked_add(Span::new().months(3 * coarser))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
fn max_from_(coarser: QuarterIndex) -> usize {
let d = Date::new(2009, 3, 31)
.into_jiff()
.checked_add(Span::new().months(3 * u16::from(coarser)))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
impl FromCoarserIndex<SemesterIndex> for DateIndex {
fn min_from(coarser: SemesterIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
let d = Date::new(2009, 1, 1)
.into_jiff()
.checked_add(Span::new().months(6 * coarser))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
fn max_from_(coarser: SemesterIndex) -> usize {
let d = Date::new(2009, 5, 31)
.into_jiff()
.checked_add(Span::new().months(1 + 6 * u16::from(coarser)))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
impl FromCoarserIndex<YearIndex> for DateIndex {
fn min_from(coarser: YearIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
Self::try_from(Date::new(2009 + coarser, 1, 1))
.unwrap()
.into()
}
}
fn max_from_(coarser: YearIndex) -> usize {
Self::try_from(Date::new(2009 + u16::from(coarser), 12, 31))
.unwrap()
.into()
}
}
impl FromCoarserIndex<DecadeIndex> for DateIndex {
fn min_from(coarser: DecadeIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
Self::try_from(Date::new(2000 + 10 * coarser, 1, 1))
.unwrap()
.into()
}
}
fn max_from_(coarser: DecadeIndex) -> usize {
let coarser = u16::from(coarser);
Self::try_from(Date::new(2009 + (10 * coarser), 12, 31))
.unwrap()
.into()
}
}

View File

@@ -0,0 +1,125 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex, YearIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct DecadeIndex(u16);
impl From<u16> for DecadeIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<DecadeIndex> for u16 {
fn from(value: DecadeIndex) -> Self {
value.0
}
}
impl From<usize> for DecadeIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<DecadeIndex> for usize {
fn from(value: DecadeIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for DecadeIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<DecadeIndex> for DecadeIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for DecadeIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for DecadeIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<DateIndex> for DecadeIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for DecadeIndex {
fn from(value: Date) -> Self {
let year = value.year();
if year < 2000 {
panic!("unsupported")
}
Self((year - 2000) / 10)
}
}
impl CheckedSub for DecadeIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl From<YearIndex> for DecadeIndex {
fn from(value: YearIndex) -> Self {
let v = usize::from(value);
if v == 0 {
Self(0)
} else {
Self((((v - 1) / 10) + 1) as u16)
}
}
}
impl Printable for DecadeIndex {
fn to_string() -> &'static str {
"decadeindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["decade", "decadeindex"]
}
}

View File

@@ -0,0 +1,98 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Height;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct DifficultyEpoch(u16);
impl From<u16> for DifficultyEpoch {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for DifficultyEpoch {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<DifficultyEpoch> for usize {
fn from(value: DifficultyEpoch) -> Self {
value.0 as usize
}
}
impl Add for DifficultyEpoch {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for DifficultyEpoch {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Add<usize> for DifficultyEpoch {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Div<usize> for DifficultyEpoch {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(self.0 as usize / rhs)
}
}
impl From<Height> for DifficultyEpoch {
fn from(value: Height) -> Self {
Self((u32::from(value) / 2016) as u16)
}
}
impl CheckedSub for DifficultyEpoch {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for DifficultyEpoch {
fn to_string() -> &'static str {
"difficultyepoch"
}
fn to_possible_strings() -> &'static [&'static str] {
&["difficulty", "difficultyepoch"]
}
}

View File

@@ -0,0 +1,360 @@
use std::{
cmp::Ordering,
f64,
ops::{Add, AddAssign, Div, Mul},
};
use brk_vecs::{CheckedSub, StoredCompressed};
use derive_deref::Deref;
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Bitcoin, Cents, Close, High, Sats, StoredF32, StoredF64};
#[derive(
Debug,
Default,
Clone,
Copy,
Deref,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
Deserialize,
StoredCompressed,
)]
pub struct Dollars(f64);
impl Dollars {
pub const ZERO: Self = Self(0.0);
pub const NAN: Self = Self(f64::NAN);
pub const fn mint(dollars: f64) -> Self {
Self(dollars)
}
pub fn round_nearest_cent(self) -> Self {
Dollars((self.0 * 100.0).round() / 100.0)
}
pub fn round_to_4_digits(self) -> Self {
Self::from(Cents::from(self).round_to_4_digits())
}
}
impl From<f32> for Dollars {
fn from(value: f32) -> Self {
Self(value as f64)
}
}
impl From<f64> for Dollars {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<Cents> for Dollars {
fn from(value: Cents) -> Self {
Self(f64::from(value) / 100.0)
}
}
impl From<Dollars> for f32 {
fn from(value: Dollars) -> Self {
value.0 as f32
}
}
impl From<Dollars> for f64 {
fn from(value: Dollars) -> Self {
value.0
}
}
impl From<Close<Dollars>> for Dollars {
fn from(value: Close<Dollars>) -> Self {
Self(value.0)
}
}
impl From<High<Dollars>> for Dollars {
fn from(value: High<Dollars>) -> Self {
Self(value.0)
}
}
impl From<usize> for Dollars {
fn from(value: usize) -> Self {
Self(value as f64)
}
}
impl Add for Dollars {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(Cents::from(self) + Cents::from(rhs))
}
}
impl Div<Dollars> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
if self.is_nan() || rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(f64::from(self) / f64::from(rhs))
}
}
}
impl Div<Close<Dollars>> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Close<Dollars>) -> Self::Output {
if self.is_nan() || *rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(f64::from(self) / f64::from(*rhs))
}
}
}
impl Div<Dollars> for Close<Dollars> {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
if self.is_nan() || rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(f64::from(*self) / f64::from(rhs))
}
}
}
impl Div<usize> for Dollars {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
if self.is_nan() || rhs == 0 {
Dollars::NAN
} else {
Self::from(Cents::from(self) / rhs)
}
}
}
impl Div<StoredF64> for Dollars {
type Output = Self;
fn div(self, rhs: StoredF64) -> Self::Output {
self / f64::from(rhs)
}
}
impl Div<f64> for Dollars {
type Output = Self;
fn div(self, rhs: f64) -> Self::Output {
if self.is_nan() || rhs == 0.0 {
Dollars::NAN
} else {
Dollars::from(Cents::from(Self::from(self.0 / rhs)))
}
}
}
impl Div<Bitcoin> for Dollars {
type Output = Self;
fn div(self, rhs: Bitcoin) -> Self::Output {
if self.is_nan() {
self
} else {
Self(f64::from(self) / f64::from(rhs))
}
}
}
impl Mul<Dollars> for Dollars {
type Output = Self;
fn mul(self, rhs: Dollars) -> Self::Output {
Self::from(Cents::from(self) * Cents::from(rhs))
}
}
impl Mul<Close<Dollars>> for Dollars {
type Output = Self;
fn mul(self, rhs: Close<Dollars>) -> Self::Output {
Self::from(Cents::from(self) * Cents::from(*rhs))
}
}
impl Mul<Dollars> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: Dollars) -> Self::Output {
Dollars::from(Cents::from(*self) * Cents::from(rhs))
}
}
impl Mul<usize> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: usize) -> Self::Output {
Dollars::from(Cents::from(*self) * rhs)
}
}
impl Mul<StoredF64> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: StoredF64) -> Self::Output {
*self * rhs
}
}
impl Mul<f64> for Dollars {
type Output = Dollars;
fn mul(self, rhs: f64) -> Self::Output {
if rhs.fract() != 0.0 {
Self::from(self.0 * rhs)
} else {
self * rhs as i64
}
}
}
impl Mul<Bitcoin> for Dollars {
type Output = Self;
fn mul(self, rhs: Bitcoin) -> Self::Output {
self * Sats::from(rhs)
}
}
impl Mul<Bitcoin> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: Bitcoin) -> Self::Output {
*self * Sats::from(rhs)
}
}
impl Mul<Sats> for Dollars {
type Output = Self;
fn mul(self, rhs: Sats) -> Self::Output {
if self.is_nan() {
self
} else {
Self::from(Cents::from(
u128::from(rhs) * u128::from(Cents::from(self)) / Sats::ONE_BTC_U128,
))
}
}
}
impl Mul<StoredF32> for Dollars {
type Output = Self;
fn mul(self, rhs: StoredF32) -> Self::Output {
self * *rhs as f64
}
}
impl Mul<StoredF64> for Dollars {
type Output = Self;
fn mul(self, rhs: StoredF64) -> Self::Output {
self * *rhs
}
}
impl Mul<i64> for Dollars {
type Output = Self;
fn mul(self, rhs: i64) -> Self::Output {
Self::from(Cents::from(self) * rhs)
}
}
impl Mul<usize> for Dollars {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
if self.is_nan() {
self
} else {
Self::from(Cents::from(self) * rhs)
}
}
}
impl From<u128> for Dollars {
fn from(value: u128) -> Self {
Self::from(Cents::from(value))
}
}
impl From<StoredF64> for Dollars {
fn from(value: StoredF64) -> Self {
Self(*value)
}
}
impl From<Close<Dollars>> for u128 {
fn from(value: Close<Dollars>) -> Self {
u128::from(*value)
}
}
impl From<Dollars> for u128 {
fn from(value: Dollars) -> Self {
u128::from(Cents::from(value))
}
}
impl AddAssign for Dollars {
fn add_assign(&mut self, rhs: Self) {
*self = Dollars::from(Cents::from(*self) + Cents::from(rhs));
}
}
impl CheckedSub for Dollars {
fn checked_sub(self, rhs: Self) -> Option<Self> {
if self.is_nan() {
Some(self)
} else {
Cents::from(self)
.checked_sub(Cents::from(rhs))
.map(Dollars::from)
}
}
}
impl CheckedSub<usize> for Dollars {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Dollars::from(
Cents::from(self).checked_sub(Cents::from(rhs)).unwrap(),
))
}
}
impl PartialEq for Dollars {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for Dollars {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for Dollars {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Dollars {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}

View File

@@ -0,0 +1,27 @@
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{LoadedAddressData, Sats};
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)]
pub struct EmptyAddressData {
pub transfered: Sats,
}
impl From<LoadedAddressData> for EmptyAddressData {
fn from(value: LoadedAddressData) -> Self {
Self::from(&value)
}
}
impl From<&LoadedAddressData> for EmptyAddressData {
fn from(value: &LoadedAddressData) -> Self {
if value.sent != value.received {
dbg!(&value);
panic!("Trying to convert not empty wallet to empty !");
}
Self {
transfered: value.sent,
}
}
}

View File

@@ -0,0 +1,70 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Deref,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct EmptyAddressIndex(TypeIndex);
impl From<TypeIndex> for EmptyAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<usize> for EmptyAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<u32> for EmptyAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<EmptyAddressIndex> for usize {
fn from(value: EmptyAddressIndex) -> Self {
usize::from(value.0)
}
}
impl Add<usize> for EmptyAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<EmptyAddressIndex> for EmptyAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for EmptyAddressIndex {
fn to_string() -> &'static str {
"emptyaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["emptyaddr", "emptyaddressindex"]
}
}

View File

@@ -0,0 +1,68 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct EmptyOutputIndex(TypeIndex);
impl From<TypeIndex> for EmptyOutputIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<EmptyOutputIndex> for u64 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(value.0)
}
}
impl From<EmptyOutputIndex> for usize {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(value.0)
}
}
impl From<usize> for EmptyOutputIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for EmptyOutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<EmptyOutputIndex> for EmptyOutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for EmptyOutputIndex {
fn to_string() -> &'static str {
"emptyoutputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["emptyout", "emptyoutputindex"]
}
}

View File

@@ -0,0 +1,90 @@
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Div},
};
use brk_vecs::StoredCompressed;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Sats, StoredU64};
#[derive(
Debug, Clone, Copy, Serialize, FromBytes, Immutable, IntoBytes, KnownLayout, StoredCompressed,
)]
pub struct Feerate(f32);
impl From<(Sats, StoredU64)> for Feerate {
fn from((sats, vsize): (Sats, StoredU64)) -> Self {
Self((f64::from(sats) / f64::from(vsize)) as f32)
}
}
impl From<f64> for Feerate {
fn from(value: f64) -> Self {
Self(value as f32)
}
}
impl From<Feerate> for f64 {
fn from(value: Feerate) -> Self {
value.0 as f64
}
}
impl Add for Feerate {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Feerate {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Div<usize> for Feerate {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self((self.0 as f64 / rhs as f64) as f32)
}
}
impl From<usize> for Feerate {
fn from(value: usize) -> Self {
Self(value as f32)
}
}
impl PartialEq for Feerate {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for Feerate {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for Feerate {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Feerate {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}

View File

@@ -0,0 +1,104 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Height;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct HalvingEpoch(u16);
impl HalvingEpoch {
pub fn new(value: u16) -> Self {
Self(value)
}
}
impl From<u16> for HalvingEpoch {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for HalvingEpoch {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<HalvingEpoch> for usize {
fn from(value: HalvingEpoch) -> Self {
value.0 as usize
}
}
impl Add for HalvingEpoch {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for HalvingEpoch {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Add<usize> for HalvingEpoch {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl From<Height> for HalvingEpoch {
fn from(value: Height) -> Self {
Self((u32::from(value) / 210_000) as u16)
}
}
impl CheckedSub for HalvingEpoch {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for HalvingEpoch {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(self.0 as usize / rhs)
}
}
impl Printable for HalvingEpoch {
fn to_string() -> &'static str {
"halvingepoch"
}
fn to_possible_strings() -> &'static [&'static str] {
&["halving", "halvingepoch"]
}
}

View File

@@ -0,0 +1,259 @@
use std::{
fmt::{self, Debug},
ops::{Add, AddAssign, Rem},
};
use bitcoincore_rpc::{Client, RpcApi};
use brk_vecs::{CheckedSub, Printable, Stamp, StoredCompressed};
use byteview::ByteView;
use derive_deref::Deref;
use serde::{Deserialize, Serialize};
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_4bytes;
use super::StoredU64;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Deref,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct Height(u32);
impl Height {
pub const ZERO: Self = Self(0);
pub const MAX: Self = Self(u32::MAX);
pub const fn new(height: u32) -> Self {
Self(height)
}
pub fn write(&self, path: &std::path::Path) -> Result<(), std::io::Error> {
std::fs::write(path, self.as_bytes())
}
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn incremented(self) -> Self {
Self(self.0 + 1)
}
pub fn decrement(&mut self) {
*self = self.decremented().unwrap();
}
pub fn decremented(self) -> Option<Self> {
self.checked_sub(1_u32)
}
pub fn is_zero(self) -> bool {
self == Self::ZERO
}
}
impl PartialEq<u64> for Height {
fn eq(&self, other: &u64) -> bool {
self.0 == *other as u32
}
}
impl Add<Height> for Height {
type Output = Self;
fn add(self, rhs: Height) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl Add<u32> for Height {
type Output = Self;
fn add(self, rhs: u32) -> Self::Output {
Self::from(self.0 + rhs)
}
}
impl Add<u64> for Height {
type Output = Self;
fn add(self, rhs: u64) -> Self::Output {
Self::from(self.0 + rhs as u32)
}
}
impl Add<usize> for Height {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u32)
}
}
impl CheckedSub<Height> for Height {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl CheckedSub<u32> for Height {
fn checked_sub(self, rhs: u32) -> Option<Self> {
self.0.checked_sub(rhs).map(Height::from)
}
}
impl CheckedSub<usize> for Height {
fn checked_sub(self, rhs: usize) -> Option<Self> {
self.0.checked_sub(rhs as u32).map(Height::from)
}
}
impl AddAssign<usize> for Height {
fn add_assign(&mut self, rhs: usize) {
*self = self.add(rhs);
}
}
impl CheckedSub<u64> for Height {
fn checked_sub(self, rhs: u64) -> Option<Self> {
self.0.checked_sub(rhs as u32).map(Height::from)
}
}
impl AddAssign<u64> for Height {
fn add_assign(&mut self, rhs: u64) {
*self = self.add(rhs);
}
}
impl Rem<Height> for Height {
type Output = Self;
fn rem(self, rhs: Height) -> Self::Output {
Self(self.0.rem(rhs.0))
}
}
impl Rem<usize> for Height {
type Output = Self;
fn rem(self, rhs: usize) -> Self::Output {
Self(self.0.rem(Height::from(rhs).0))
}
}
impl fmt::Display for Height {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u32> for Height {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<u64> for Height {
fn from(value: u64) -> Self {
Self(value as u32)
}
}
impl From<StoredU64> for Height {
fn from(value: StoredU64) -> Self {
Self(*value as u32)
}
}
impl From<usize> for Height {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Height> for usize {
fn from(value: Height) -> Self {
value.0 as 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
}
}
impl TryFrom<&Client> for Height {
type Error = bitcoincore_rpc::Error;
fn try_from(value: &Client) -> Result<Self, Self::Error> {
Ok((value.get_block_count()? as usize).into())
}
}
impl From<bitcoin::locktime::absolute::Height> for Height {
fn from(value: bitcoin::locktime::absolute::Height) -> Self {
Self(value.to_consensus_u32())
}
}
impl TryFrom<&std::path::Path> for Height {
type Error = brk_error::Error;
fn try_from(value: &std::path::Path) -> Result<Self, Self::Error> {
Ok(Self::read_from_bytes(std::fs::read(value)?.as_slice())?.to_owned())
}
}
impl From<ByteView> for Height {
fn from(value: byteview::ByteView) -> Self {
Self(u32::from_be_bytes(copy_first_4bytes(&value).unwrap()))
}
}
impl From<Height> for ByteView {
fn from(value: Height) -> Self {
Self::new(&value.0.to_be_bytes())
}
}
impl From<Stamp> for Height {
fn from(value: Stamp) -> Self {
let u = u64::from(value);
assert!(u <= u32::MAX as u64);
Self(u as u32)
}
}
impl From<Height> for Stamp {
fn from(value: Height) -> Self {
Self::from(value.0 as u64)
}
}
impl Printable for Height {
fn to_string() -> &'static str {
"height"
}
fn to_possible_strings() -> &'static [&'static str] {
&["h", "height"]
}
}

View File

@@ -0,0 +1,108 @@
use std::ops::{Add, AddAssign};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Vin;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct InputIndex(u64);
impl InputIndex {
pub fn incremented(self) -> Self {
Self(*self + 1)
}
}
impl Add<InputIndex> for InputIndex {
type Output = Self;
fn add(self, rhs: InputIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Add<Vin> for InputIndex {
type Output = Self;
fn add(self, rhs: Vin) -> Self::Output {
Self(self.0 + u64::from(rhs))
}
}
impl Add<usize> for InputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u64)
}
}
impl AddAssign<InputIndex> for InputIndex {
fn add_assign(&mut self, rhs: InputIndex) {
self.0 += rhs.0
}
}
impl CheckedSub<InputIndex> for InputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self::from)
}
}
impl From<InputIndex> for u32 {
fn from(value: InputIndex) -> Self {
if value.0 > u32::MAX as u64 {
panic!()
}
value.0 as u32
}
}
impl From<u64> for InputIndex {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<InputIndex> for u64 {
fn from(value: InputIndex) -> Self {
value.0
}
}
impl From<usize> for InputIndex {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<InputIndex> for usize {
fn from(value: InputIndex) -> Self {
value.0 as usize
}
}
impl Printable for InputIndex {
fn to_string() -> &'static str {
"inputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["in", "inputindex"]
}
}

View File

@@ -0,0 +1,76 @@
use brk_error::{Error, Result};
use brk_vecs::CheckedSub;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{Bitcoin, Dollars, EmptyAddressData, Sats};
#[derive(Debug, Default, Clone, Serialize, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct LoadedAddressData {
pub sent: Sats,
pub received: Sats,
pub realized_cap: Dollars,
pub outputs_len: u32,
padding: u32,
}
impl LoadedAddressData {
pub fn amount(&self) -> Sats {
(u64::from(self.received) - u64::from(self.sent)).into()
}
pub fn realized_price(&self) -> Dollars {
(self.realized_cap / Bitcoin::from(self.amount())).round_to_4_digits()
}
#[inline]
pub fn has_0_sats(&self) -> bool {
self.amount() == Sats::ZERO
}
#[inline]
pub fn has_0_utxos(&self) -> bool {
self.outputs_len == 0
}
pub fn receive(&mut self, amount: Sats, price: Option<Dollars>) {
self.received += amount;
self.outputs_len += 1;
if let Some(price) = price {
self.realized_cap += price * amount;
}
}
pub fn send(&mut self, amount: Sats, previous_price: Option<Dollars>) -> Result<()> {
if self.amount() < amount {
return Err(Error::Str("Previous_amount smaller than sent amount"));
}
self.sent += amount;
self.outputs_len -= 1;
if let Some(previous_price) = previous_price {
self.realized_cap = self
.realized_cap
.checked_sub(previous_price * amount)
.unwrap();
}
Ok(())
}
}
impl From<EmptyAddressData> for LoadedAddressData {
fn from(value: EmptyAddressData) -> Self {
Self::from(&value)
}
}
impl From<&EmptyAddressData> for LoadedAddressData {
fn from(value: &EmptyAddressData) -> Self {
Self {
sent: value.transfered,
received: value.transfered,
realized_cap: Dollars::ZERO,
outputs_len: 0,
padding: 0,
}
}
}

View File

@@ -0,0 +1,68 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct LoadedAddressIndex(TypeIndex);
impl From<TypeIndex> for LoadedAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<usize> for LoadedAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<LoadedAddressIndex> for usize {
fn from(value: LoadedAddressIndex) -> Self {
usize::from(value.0)
}
}
impl From<LoadedAddressIndex> for u32 {
fn from(value: LoadedAddressIndex) -> Self {
u32::from(value.0)
}
}
impl Add<usize> for LoadedAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<LoadedAddressIndex> for LoadedAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for LoadedAddressIndex {
fn to_string() -> &'static str {
"loadedaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["loadedaddr", "loadedaddressindex"]
}
}

View File

@@ -0,0 +1,149 @@
use brk_error::{Error, Result};
mod addressbytes;
mod addressbyteshash;
mod anyaddressindex;
mod bitcoin;
mod blockhash;
mod blockhashprefix;
mod cents;
mod date;
mod dateindex;
mod decadeindex;
mod difficultyepoch;
mod dollars;
mod emptyaddressdata;
mod emptyaddressindex;
mod emptyoutputindex;
mod feerate;
mod halvingepoch;
mod height;
mod inputindex;
mod loadedaddressdata;
mod loadedaddressindex;
mod monthindex;
mod ohlc;
mod opreturnindex;
mod outputindex;
mod outputtype;
mod p2aaddressindex;
mod p2msoutputindex;
mod p2pk33addressindex;
mod p2pk65addressindex;
mod p2pkhaddressindex;
mod p2shaddressindex;
mod p2traddressindex;
mod p2wpkhaddressindex;
mod p2wshaddressindex;
mod quarterindex;
mod rawlocktime;
mod sats;
mod semesterindex;
mod stored_bool;
mod stored_f32;
mod stored_f64;
mod stored_u16;
mod stored_u32;
mod stored_u64;
mod stored_u8;
mod timestamp;
mod txid;
mod txidprefix;
mod txindex;
mod txversion;
mod typeindex;
mod typeindex_with_outputindex;
mod unit;
mod unknownoutputindex;
mod vin;
mod vout;
mod weekindex;
mod weight;
mod yearindex;
pub use addressbytes::*;
pub use addressbyteshash::*;
pub use anyaddressindex::*;
pub use bitcoin::*;
pub use blockhash::*;
pub use blockhashprefix::*;
pub use cents::*;
pub use date::*;
pub use dateindex::*;
pub use decadeindex::*;
pub use difficultyepoch::*;
pub use dollars::*;
pub use emptyaddressdata::*;
pub use emptyaddressindex::*;
pub use emptyoutputindex::*;
pub use feerate::*;
pub use halvingepoch::*;
pub use height::*;
pub use inputindex::*;
pub use loadedaddressdata::*;
pub use loadedaddressindex::*;
pub use monthindex::*;
pub use ohlc::*;
pub use opreturnindex::*;
pub use outputindex::*;
pub use outputtype::*;
pub use p2aaddressindex::*;
pub use p2msoutputindex::*;
pub use p2pk33addressindex::*;
pub use p2pk65addressindex::*;
pub use p2pkhaddressindex::*;
pub use p2shaddressindex::*;
pub use p2traddressindex::*;
pub use p2wpkhaddressindex::*;
pub use p2wshaddressindex::*;
pub use quarterindex::*;
pub use rawlocktime::*;
pub use sats::*;
pub use semesterindex::*;
pub use stored_bool::*;
pub use stored_f32::*;
pub use stored_f64::*;
pub use stored_u8::*;
pub use stored_u16::*;
pub use stored_u32::*;
pub use stored_u64::*;
pub use timestamp::*;
pub use txid::*;
pub use txidprefix::*;
pub use txindex::*;
pub use txversion::*;
pub use typeindex::*;
pub use typeindex_with_outputindex::*;
pub use unit::*;
pub use unknownoutputindex::*;
pub use vin::*;
pub use vout::*;
pub use weekindex::*;
pub use weight::*;
pub use yearindex::*;
#[allow(clippy::result_unit_err)]
pub fn copy_first_4bytes(slice: &[u8]) -> Result<[u8; 4]> {
let mut buf: [u8; 4] = [0; 4];
let buf_len = buf.len();
if slice.len() < buf_len {
return Err(Error::Str("Buffer is too small to convert to 8 bytes"));
}
slice.iter().take(buf_len).enumerate().for_each(|(i, r)| {
buf[i] = *r;
});
Ok(buf)
}
#[allow(clippy::result_unit_err)]
pub fn copy_first_8bytes(slice: &[u8]) -> Result<[u8; 8]> {
let mut buf: [u8; 8] = [0; 8];
let buf_len = buf.len();
if slice.len() < buf_len {
return Err(Error::Str("Buffer is too small to convert to 8 bytes"));
}
slice.iter().take(buf_len).enumerate().for_each(|(i, r)| {
buf[i] = *r;
});
Ok(buf)
}

View File

@@ -0,0 +1,116 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex, YearIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct MonthIndex(u16);
impl From<u16> for MonthIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<MonthIndex> for u16 {
fn from(value: MonthIndex) -> Self {
value.0
}
}
impl From<usize> for MonthIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<MonthIndex> for u64 {
fn from(value: MonthIndex) -> Self {
value.0 as u64
}
}
impl From<MonthIndex> for usize {
fn from(value: MonthIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for MonthIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<MonthIndex> for MonthIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for MonthIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for MonthIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<DateIndex> for MonthIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for MonthIndex {
fn from(value: Date) -> Self {
Self(u16::from(YearIndex::from(value)) * 12 + value.month() as u16 - 1)
}
}
impl CheckedSub for MonthIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for MonthIndex {
fn to_string() -> &'static str {
"monthindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["m", "month", "monthindex"]
}
}

View File

@@ -0,0 +1,577 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::StoredCompressed;
use derive_deref::{Deref, DerefMut};
use serde::{Serialize, Serializer, ser::SerializeTuple};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Cents, Dollars, Sats};
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[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,
}
}
}
impl Serialize for OHLCCents {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(4)?;
tup.serialize_element(&self.open)?;
tup.serialize_element(&self.high)?;
tup.serialize_element(&self.low)?;
tup.serialize_element(&self.close)?;
tup.end()
}
}
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct OHLCDollars {
pub open: Open<Dollars>,
pub high: High<Dollars>,
pub low: Low<Dollars>,
pub close: Close<Dollars>,
}
impl Serialize for OHLCDollars {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(4)?;
tup.serialize_element(&self.open)?;
tup.serialize_element(&self.high)?;
tup.serialize_element(&self.low)?;
tup.serialize_element(&self.close)?;
tup.end()
}
}
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, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct OHLCSats {
pub open: Open<Sats>,
pub high: High<Sats>,
pub low: Low<Sats>,
pub close: Close<Sats>,
}
impl Serialize for OHLCSats {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(4)?;
tup.serialize_element(&self.open)?;
tup.serialize_element(&self.high)?;
tup.serialize_element(&self.low)?;
tup.serialize_element(&self.close)?;
tup.end()
}
}
impl From<(Open<Sats>, High<Sats>, Low<Sats>, Close<Sats>)> for OHLCSats {
fn from(value: (Open<Sats>, High<Sats>, Low<Sats>, Close<Sats>)) -> Self {
Self {
open: value.0,
high: value.1,
low: value.2,
close: value.3,
}
}
}
impl From<Close<Sats>> for OHLCSats {
fn from(value: Close<Sats>) -> Self {
Self {
open: Open::from(value),
high: High::from(value),
low: Low::from(value),
close: value,
}
}
}
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct Open<T>(T);
impl<T> Open<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for Open<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for Open<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<Open<T>> for f64
where
f64: From<T>,
{
fn from(value: Open<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for Open<T>
where
T: Copy,
{
fn from(value: Close<T>) -> Self {
Self(*value)
}
}
impl From<Open<Cents>> for Open<Dollars> {
fn from(value: Open<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for Open<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for Open<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for Open<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
#[derive(
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct High<T>(T);
impl<T> High<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for High<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for High<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<High<T>> for f64
where
f64: From<T>,
{
fn from(value: High<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for High<T>
where
T: Copy,
{
fn from(value: Close<T>) -> Self {
Self(*value)
}
}
impl From<High<Cents>> for High<Dollars> {
fn from(value: High<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for High<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for High<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for High<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct Low<T>(T);
impl<T> Low<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for Low<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for Low<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<Low<T>> for f64
where
f64: From<T>,
{
fn from(value: Low<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for Low<T>
where
T: Copy,
{
fn from(value: Close<T>) -> Self {
Self(*value)
}
}
impl From<Low<Cents>> for Low<Dollars> {
fn from(value: Low<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for Low<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for Low<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for Low<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct Close<T>(T);
impl<T> Close<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for Close<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f32> for Close<T>
where
T: From<f32>,
{
fn from(value: f32) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for Close<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<Close<T>> for f32
where
f32: From<T>,
{
fn from(value: Close<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for f64
where
f64: From<T>,
{
fn from(value: Close<T>) -> Self {
Self::from(value.0)
}
}
// impl<A, B> From<Close<A>> for Close<B>
// where
// B: From<A>,
// {
// fn from(value: Close<A>) -> Self {
// Self(B::from(*value))
impl From<Close<Cents>> for Close<Dollars> {
fn from(value: Close<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for Close<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for Close<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for Close<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
// impl<T> Mul<usize> for Close<T>
// where
// T: Mul<usize, Output = T>,
// {
// type Output = Self;
// fn mul(self, rhs: usize) -> Self::Output {
// Self(self.0 * rhs)
// }
// }

View File

@@ -0,0 +1,70 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct OpReturnIndex(TypeIndex);
impl From<TypeIndex> for OpReturnIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<OpReturnIndex> for usize {
fn from(value: OpReturnIndex) -> Self {
Self::from(*value)
}
}
impl From<OpReturnIndex> for u64 {
fn from(value: OpReturnIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for OpReturnIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for OpReturnIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<OpReturnIndex> for OpReturnIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for OpReturnIndex {
fn to_string() -> &'static str {
"opreturnindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["op", "opreturn", "opreturnindex"]
}
}

View File

@@ -0,0 +1,124 @@
use std::ops::{Add, AddAssign};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_8bytes;
use super::Vout;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct OutputIndex(u64);
impl OutputIndex {
pub const ZERO: Self = Self(0);
pub const COINBASE: Self = Self(u64::MAX);
pub fn incremented(self) -> Self {
Self(*self + 1)
}
pub fn is_coinbase(self) -> bool {
self == Self::COINBASE
}
}
impl Add<OutputIndex> for OutputIndex {
type Output = Self;
fn add(self, rhs: OutputIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Add<Vout> for OutputIndex {
type Output = Self;
fn add(self, rhs: Vout) -> Self::Output {
Self(self.0 + u64::from(rhs))
}
}
impl Add<usize> for OutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u64)
}
}
impl AddAssign<OutputIndex> for OutputIndex {
fn add_assign(&mut self, rhs: OutputIndex) {
self.0 += rhs.0
}
}
impl CheckedSub<OutputIndex> for OutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self::from)
}
}
impl From<OutputIndex> for u32 {
fn from(value: OutputIndex) -> Self {
if value.0 > u32::MAX as u64 {
panic!()
}
value.0 as u32
}
}
impl From<u64> for OutputIndex {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<OutputIndex> for u64 {
fn from(value: OutputIndex) -> Self {
value.0
}
}
impl From<usize> for OutputIndex {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<OutputIndex> for usize {
fn from(value: OutputIndex) -> Self {
value.0 as usize
}
}
impl From<&[u8]> for OutputIndex {
fn from(value: &[u8]) -> Self {
Self(u64::from_be_bytes(copy_first_8bytes(value).unwrap()))
}
}
impl Printable for OutputIndex {
fn to_string() -> &'static str {
"outputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["out", "outputindex"]
}
}

View File

@@ -0,0 +1,381 @@
use bitcoin::{ScriptBuf, opcodes::all::OP_PUSHBYTES_2};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
#[repr(u8)]
pub enum OutputType {
P2PK65,
P2PK33,
P2PKH,
P2MS,
P2SH,
OpReturn,
P2WPKH,
P2WSH,
P2TR,
P2A,
Dummy10,
Dummy11,
Dummy12,
Dummy13,
Dummy14,
Dummy15,
Dummy16,
Dummy17,
Dummy18,
Dummy19,
Dummy20,
Dummy21,
Dummy22,
Dummy23,
Dummy24,
Dummy25,
Dummy26,
Dummy27,
Dummy28,
Dummy29,
Dummy30,
Dummy31,
Dummy32,
Dummy33,
Dummy34,
Dummy35,
Dummy36,
Dummy37,
Dummy38,
Dummy39,
Dummy40,
Dummy41,
Dummy42,
Dummy43,
Dummy44,
Dummy45,
Dummy46,
Dummy47,
Dummy48,
Dummy49,
Dummy50,
Dummy51,
Dummy52,
Dummy53,
Dummy54,
Dummy55,
Dummy56,
Dummy57,
Dummy58,
Dummy59,
Dummy60,
Dummy61,
Dummy62,
Dummy63,
Dummy64,
Dummy65,
Dummy66,
Dummy67,
Dummy68,
Dummy69,
Dummy70,
Dummy71,
Dummy72,
Dummy73,
Dummy74,
Dummy75,
Dummy76,
Dummy77,
Dummy78,
Dummy79,
Dummy80,
Dummy81,
Dummy82,
Dummy83,
Dummy84,
Dummy85,
Dummy86,
Dummy87,
Dummy88,
Dummy89,
Dummy90,
Dummy91,
Dummy92,
Dummy93,
Dummy94,
Dummy95,
Dummy96,
Dummy97,
Dummy98,
Dummy99,
Dummy100,
Dummy101,
Dummy102,
Dummy103,
Dummy104,
Dummy105,
Dummy106,
Dummy107,
Dummy108,
Dummy109,
Dummy110,
Dummy111,
Dummy112,
Dummy113,
Dummy114,
Dummy115,
Dummy116,
Dummy117,
Dummy118,
Dummy119,
Dummy120,
Dummy121,
Dummy122,
Dummy123,
Dummy124,
Dummy125,
Dummy126,
Dummy127,
Dummy128,
Dummy129,
Dummy130,
Dummy131,
Dummy132,
Dummy133,
Dummy134,
Dummy135,
Dummy136,
Dummy137,
Dummy138,
Dummy139,
Dummy140,
Dummy141,
Dummy142,
Dummy143,
Dummy144,
Dummy145,
Dummy146,
Dummy147,
Dummy148,
Dummy149,
Dummy150,
Dummy151,
Dummy152,
Dummy153,
Dummy154,
Dummy155,
Dummy156,
Dummy157,
Dummy158,
Dummy159,
Dummy160,
Dummy161,
Dummy162,
Dummy163,
Dummy164,
Dummy165,
Dummy166,
Dummy167,
Dummy168,
Dummy169,
Dummy170,
Dummy171,
Dummy172,
Dummy173,
Dummy174,
Dummy175,
Dummy176,
Dummy177,
Dummy178,
Dummy179,
Dummy180,
Dummy181,
Dummy182,
Dummy183,
Dummy184,
Dummy185,
Dummy186,
Dummy187,
Dummy188,
Dummy189,
Dummy190,
Dummy191,
Dummy192,
Dummy193,
Dummy194,
Dummy195,
Dummy196,
Dummy197,
Dummy198,
Dummy199,
Dummy200,
Dummy201,
Dummy202,
Dummy203,
Dummy204,
Dummy205,
Dummy206,
Dummy207,
Dummy208,
Dummy209,
Dummy210,
Dummy211,
Dummy212,
Dummy213,
Dummy214,
Dummy215,
Dummy216,
Dummy217,
Dummy218,
Dummy219,
Dummy220,
Dummy221,
Dummy222,
Dummy223,
Dummy224,
Dummy225,
Dummy226,
Dummy227,
Dummy228,
Dummy229,
Dummy230,
Dummy231,
Dummy232,
Dummy233,
Dummy234,
Dummy235,
Dummy236,
Dummy237,
Dummy238,
Dummy239,
Dummy240,
Dummy241,
Dummy242,
Dummy243,
Dummy244,
Dummy245,
Dummy246,
Dummy247,
Dummy248,
Dummy249,
Dummy250,
Dummy251,
Dummy252,
Dummy253,
Empty = 254,
Unknown = 255,
}
impl OutputType {
pub fn is_spendable(&self) -> bool {
match self {
Self::P2PK65 => true,
Self::P2PK33 => true,
Self::P2PKH => true,
Self::P2MS => true,
Self::P2SH => true,
Self::OpReturn => false,
Self::P2WPKH => true,
Self::P2WSH => true,
Self::P2TR => true,
Self::P2A => true,
Self::Empty => true,
Self::Unknown => true,
_ => unreachable!(),
}
}
pub fn is_address(&self) -> bool {
match self {
Self::P2PK65 => true,
Self::P2PK33 => true,
Self::P2PKH => true,
Self::P2MS => false,
Self::P2SH => true,
Self::OpReturn => false,
Self::P2WPKH => true,
Self::P2WSH => true,
Self::P2TR => true,
Self::P2A => true,
Self::Empty => false,
Self::Unknown => false,
_ => unreachable!(),
}
}
pub fn is_not_address(&self) -> bool {
!self.is_address()
}
pub fn is_unspendable(&self) -> bool {
!self.is_spendable()
}
pub fn as_vec() -> Vec<Self> {
vec![
Self::P2PK65,
Self::P2PK33,
Self::P2PKH,
Self::P2MS,
Self::P2SH,
Self::OpReturn,
Self::P2WPKH,
Self::P2WSH,
Self::P2TR,
Self::P2A,
Self::Empty,
Self::Unknown,
]
}
}
impl From<&ScriptBuf> for OutputType {
fn from(script: &ScriptBuf) -> Self {
if script.is_p2pk() {
let bytes = script.as_bytes();
match bytes.len() {
67 => Self::P2PK65,
35 => Self::P2PK33,
_ => {
dbg!(bytes);
unreachable!()
}
}
} else if script.is_p2pkh() {
Self::P2PKH
} else if script.is_multisig() {
Self::P2MS
} else if script.is_p2sh() {
Self::P2SH
} else if script.is_op_return() {
Self::OpReturn
} else if script.is_p2wpkh() {
Self::P2WPKH
} else if script.is_p2wsh() {
Self::P2WSH
} else if script.is_p2tr() {
Self::P2TR
} else if script.witness_version() == Some(bitcoin::WitnessVersion::V1)
&& script.len() == 4
&& script.as_bytes()[1] == OP_PUSHBYTES_2.to_u8()
&& script.as_bytes()[2..4] == [78, 115]
{
Self::P2A
} else if script.is_empty() {
Self::Empty
} else {
Self::Unknown
}
}
}

View File

@@ -0,0 +1,83 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2AAddressIndex(TypeIndex);
impl From<TypeIndex> for P2AAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2AAddressIndex> for TypeIndex {
fn from(value: P2AAddressIndex) -> Self {
value.0
}
}
impl From<P2AAddressIndex> for u32 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2AAddressIndex> for u64 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2AAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2AAddressIndex> for usize {
fn from(value: P2AAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2AAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2AAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2AAddressIndex> for P2AAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2AAddressIndex {
fn to_string() -> &'static str {
"p2aaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["aaddr", "p2aaddr", "p2aaddressindex"]
}
}

View File

@@ -0,0 +1,69 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2MSOutputIndex(TypeIndex);
impl From<TypeIndex> for P2MSOutputIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2MSOutputIndex> for usize {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<P2MSOutputIndex> for u64 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2MSOutputIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2MSOutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2MSOutputIndex> for P2MSOutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2MSOutputIndex {
fn to_string() -> &'static str {
"p2msoutputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["msout", "p2msout", "p2msoutputindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2PK33AddressIndex(TypeIndex);
impl From<TypeIndex> for P2PK33AddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2PK33AddressIndex> for TypeIndex {
fn from(value: P2PK33AddressIndex) -> Self {
value.0
}
}
impl From<P2PK33AddressIndex> for u32 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PK33AddressIndex> for u64 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2PK33AddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2PK33AddressIndex> for usize {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2PK33AddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2PK33AddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2PK33AddressIndex> for P2PK33AddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2PK33AddressIndex {
fn to_string() -> &'static str {
"p2pk33addressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["pk33addr", "p2pk33addr", "p2pk33addressindex"]
}
}

View File

@@ -0,0 +1,83 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2PK65AddressIndex(TypeIndex);
impl From<TypeIndex> for P2PK65AddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2PK65AddressIndex> for TypeIndex {
fn from(value: P2PK65AddressIndex) -> Self {
value.0
}
}
impl From<P2PK65AddressIndex> for u32 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PK65AddressIndex> for u64 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PK65AddressIndex> for usize {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2PK65AddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<usize> for P2PK65AddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2PK65AddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2PK65AddressIndex> for P2PK65AddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2PK65AddressIndex {
fn to_string() -> &'static str {
"p2pk65addressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["pk65addr", "p2pk65addr", "p2pk65addressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2PKHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2PKHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2PKHAddressIndex> for TypeIndex {
fn from(value: P2PKHAddressIndex) -> Self {
value.0
}
}
impl From<P2PKHAddressIndex> for usize {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PKHAddressIndex> for u64 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PKHAddressIndex> for u32 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2PKHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<usize> for P2PKHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2PKHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2PKHAddressIndex> for P2PKHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2PKHAddressIndex {
fn to_string() -> &'static str {
"p2pkhaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["pkhaddr", "p2pkhaddr", "p2pkhaddressindex"]
}
}

View File

@@ -0,0 +1,89 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2SHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2SHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2SHAddressIndex> for TypeIndex {
fn from(value: P2SHAddressIndex) -> Self {
value.0
}
}
impl From<P2SHAddressIndex> for u32 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2SHAddressIndex> for u64 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2SHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<u64> for P2SHAddressIndex {
fn from(value: u64) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2SHAddressIndex> for usize {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2SHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2SHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2SHAddressIndex> for P2SHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2SHAddressIndex {
fn to_string() -> &'static str {
"p2shaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["shaddr", "p2shaddr", "p2shaddressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2TRAddressIndex(TypeIndex);
impl From<TypeIndex> for P2TRAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2TRAddressIndex> for TypeIndex {
fn from(value: P2TRAddressIndex) -> Self {
value.0
}
}
impl From<P2TRAddressIndex> for u32 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2TRAddressIndex> for u64 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2TRAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2TRAddressIndex> for usize {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2TRAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2TRAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2TRAddressIndex> for P2TRAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2TRAddressIndex {
fn to_string() -> &'static str {
"p2traddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["traddr", "p2traddr", "p2traddressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2WPKHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2WPKHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2WPKHAddressIndex> for TypeIndex {
fn from(value: P2WPKHAddressIndex) -> Self {
value.0
}
}
impl From<P2WPKHAddressIndex> for u32 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2WPKHAddressIndex> for u64 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2WPKHAddressIndex> for usize {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2WPKHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<usize> for P2WPKHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2WPKHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2WPKHAddressIndex> for P2WPKHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2WPKHAddressIndex {
fn to_string() -> &'static str {
"p2wpkhaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["wpkhaddr", "p2wpkhaddr", "p2wpkhaddressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2WSHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2WSHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2WSHAddressIndex> for TypeIndex {
fn from(value: P2WSHAddressIndex) -> Self {
value.0
}
}
impl From<P2WSHAddressIndex> for u32 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2WSHAddressIndex> for u64 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2WSHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2WSHAddressIndex> for usize {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2WSHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2WSHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2WSHAddressIndex> for P2WSHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2WSHAddressIndex {
fn to_string() -> &'static str {
"p2wshaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["wshaddr", "p2wshaddr", "p2wshaddressindex"]
}
}

View File

@@ -0,0 +1,104 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::MonthIndex;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct QuarterIndex(u16);
impl From<u16> for QuarterIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for QuarterIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<QuarterIndex> for u16 {
fn from(value: QuarterIndex) -> Self {
value.0
}
}
impl From<QuarterIndex> for usize {
fn from(value: QuarterIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for QuarterIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<QuarterIndex> for QuarterIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for QuarterIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for QuarterIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<MonthIndex> for QuarterIndex {
fn from(value: MonthIndex) -> Self {
Self((usize::from(value) / 3) as u16)
}
}
impl CheckedSub for QuarterIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for QuarterIndex {
fn to_string() -> &'static str {
"quarterindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["q", "quarter", "quarterindex"]
}
}

View File

@@ -0,0 +1,32 @@
use bitcoin::absolute::LockTime;
use brk_vecs::StoredCompressed;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug, Immutable, Clone, Copy, IntoBytes, KnownLayout, FromBytes, Serialize, StoredCompressed,
)]
pub struct RawLockTime(u32);
impl From<LockTime> for RawLockTime {
fn from(value: LockTime) -> Self {
Self(value.to_consensus_u32())
}
}
const CONSENSUS_DELIMITER: u32 = 500_000_000;
impl From<RawLockTime> for LockTime {
fn from(value: RawLockTime) -> Self {
let value = value.0;
if value >= CONSENSUS_DELIMITER {
bitcoin::locktime::absolute::Height::from_consensus(value)
.unwrap()
.into()
} else {
bitcoin::locktime::absolute::Time::from_consensus(value)
.unwrap()
.into()
}
}
}

View File

@@ -0,0 +1,251 @@
use std::{
iter::Sum,
ops::{Add, AddAssign, Div, Mul, SubAssign},
};
use bitcoin::Amount;
use brk_vecs::{CheckedSub, StoredCompressed};
use derive_deref::Deref;
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::StoredF64;
use super::{Bitcoin, Cents, Dollars, Height};
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
Deserialize,
StoredCompressed,
)]
pub struct Sats(u64);
#[allow(clippy::inconsistent_digit_grouping)]
impl Sats {
pub const ZERO: Self = Self(0);
pub const _1: Self = Self(1);
pub const _10: Self = Self(10);
pub const _100: Self = Self(100);
pub const _1K: Self = Self(1_000);
pub const _10K: Self = Self(10_000);
pub const _100K: Self = Self(100_000);
pub const _1M: Self = Self(1_000_000);
pub const _10M: Self = Self(10_000_000);
pub const _1BTC: Self = Self::ONE_BTC;
pub const _10BTC: Self = Self(10_00_000_000);
pub const _100BTC: Self = Self(100_00_000_000);
pub const _1K_BTC: Self = Self(1_000_00_000_000);
pub const _10K_BTC: Self = Self(10_000_00_000_000);
pub const _100K_BTC: Self = Self(100_000_00_000_000);
pub const ONE_BTC: Self = Self(1_00_000_000);
pub const MAX: Self = Self(u64::MAX);
pub const FIFTY_BTC: Self = Self(50_00_000_000);
pub const ONE_BTC_U128: u128 = 1_00_000_000;
pub fn new(sats: u64) -> Self {
Self(sats)
}
pub fn is_zero(&self) -> bool {
*self == Self::ZERO
}
pub fn is_not_zero(&self) -> bool {
*self != Self::ZERO
}
}
impl Add for Sats {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for Sats {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl CheckedSub<Sats> for Sats {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self::from)
}
}
impl CheckedSub<usize> for Sats {
fn checked_sub(self, rhs: usize) -> Option<Self> {
self.0.checked_sub(rhs as u64).map(Self::from)
}
}
impl SubAssign for Sats {
fn sub_assign(&mut self, rhs: Self) {
*self = self.checked_sub(rhs).unwrap();
// .unwrap_or_else(|| {
// dbg!((*self, rhs));
// unreachable!();
// });
}
}
impl Mul<Sats> for Sats {
type Output = Self;
fn mul(self, rhs: Sats) -> Self::Output {
Sats::from(self.0.checked_mul(rhs.0).unwrap())
}
}
impl Mul<usize> for Sats {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Sats::from(self.0.checked_mul(rhs as u64).unwrap())
}
}
impl Mul<u64> for Sats {
type Output = Self;
fn mul(self, rhs: u64) -> Self::Output {
Sats::from(self.0.checked_mul(rhs).unwrap())
}
}
impl Mul<Height> for Sats {
type Output = Self;
fn mul(self, rhs: Height) -> Self::Output {
Sats::from(self.0.checked_mul(u64::from(rhs)).unwrap())
}
}
impl Mul<StoredF64> for Sats {
type Output = Self;
fn mul(self, rhs: StoredF64) -> Self::Output {
Sats::from((self.0 as f64 * f64::from(rhs)) as u64)
}
}
impl Sum for Sats {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sats: u64 = iter.map(|sats| sats.0).sum();
Sats::from(sats)
}
}
impl Div<Dollars> for Sats {
type Output = Self;
fn div(self, rhs: Dollars) -> Self::Output {
let raw_cents = u64::from(Cents::from(rhs));
if raw_cents != 0 {
Self(self.0 * 100 / raw_cents)
} else {
Self::MAX
}
}
}
impl Div<Sats> for Sats {
type Output = Self;
fn div(self, rhs: Sats) -> Self::Output {
if rhs.0 == 0 {
Self(0)
} else {
Self(self.0 / rhs.0)
}
}
}
impl Div<usize> for Sats {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u64)
}
}
impl From<u64> for Sats {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<usize> for Sats {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<f64> for Sats {
fn from(value: f64) -> Self {
Self(value.round() as u64)
}
}
impl From<Sats> for f64 {
fn from(value: Sats) -> Self {
value.0 as f64
}
}
impl From<Sats> for usize {
fn from(value: Sats) -> Self {
value.0 as usize
}
}
impl From<Amount> for Sats {
fn from(value: Amount) -> Self {
Self(value.to_sat())
}
}
impl From<Sats> for Amount {
fn from(value: Sats) -> Self {
Self::from_sat(value.0)
}
}
impl From<Bitcoin> for Sats {
fn from(value: Bitcoin) -> Self {
Self((f64::from(value) * (Sats::ONE_BTC.0 as f64)).round() as u64)
}
}
impl From<Sats> for u64 {
fn from(value: Sats) -> Self {
value.0
}
}
impl From<u128> for Sats {
fn from(value: u128) -> Self {
if value > u64::MAX as u128 {
panic!("u128 bigger than u64")
}
Self(value as u64)
}
}
impl From<Sats> for u128 {
fn from(value: Sats) -> Self {
value.0 as u128
}
}
impl Mul<Sats> for usize {
type Output = Sats;
fn mul(self, rhs: Sats) -> Self::Output {
Self::Output::from(rhs.0 * self as u64)
}
}

View File

@@ -0,0 +1,104 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::MonthIndex;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct SemesterIndex(u16);
impl From<u16> for SemesterIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for SemesterIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<SemesterIndex> for u16 {
fn from(value: SemesterIndex) -> Self {
value.0
}
}
impl From<SemesterIndex> for usize {
fn from(value: SemesterIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for SemesterIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<SemesterIndex> for SemesterIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for SemesterIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for SemesterIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<MonthIndex> for SemesterIndex {
fn from(value: MonthIndex) -> Self {
Self((usize::from(value) / 6) as u16)
}
}
impl CheckedSub for SemesterIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for SemesterIndex {
fn to_string() -> &'static str {
"semesterindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["s", "semester", "semesterindex"]
}
}

View File

@@ -0,0 +1,50 @@
use brk_vecs::{Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug,
Deref,
Clone,
Default,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredBool(u16);
impl StoredBool {
pub const FALSE: Self = Self(0);
pub const TRUE: Self = Self(1);
}
impl From<bool> for StoredBool {
fn from(value: bool) -> Self {
if value { Self(1) } else { Self(0) }
}
}
impl From<StoredBool> for usize {
fn from(value: StoredBool) -> Self {
value.0 as usize
}
}
impl Printable for StoredBool {
fn to_string() -> &'static str {
"bool"
}
fn to_possible_strings() -> &'static [&'static str] {
&["bool"]
}
}

View File

@@ -0,0 +1,175 @@
use core::panic;
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Div, Mul, Sub},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Dollars, StoredF64};
#[derive(
Debug,
Deref,
Default,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredF32(f32);
impl From<f32> for StoredF32 {
fn from(value: f32) -> Self {
Self(value)
}
}
impl From<f64> for StoredF32 {
fn from(value: f64) -> Self {
if value > f32::MAX as f64 {
panic!("f64 is too big")
}
Self(value as f32)
}
}
impl From<StoredF32> for f64 {
fn from(value: StoredF32) -> Self {
value.0 as f64
}
}
impl From<StoredF64> for StoredF32 {
fn from(value: StoredF64) -> Self {
Self(*value as f32)
}
}
impl From<usize> for StoredF32 {
fn from(value: usize) -> Self {
Self(value as f32)
}
}
impl CheckedSub<StoredF32> for StoredF32 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
Some(Self(self.0 - rhs.0))
}
}
impl Div<usize> for StoredF32 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as f32)
}
}
impl Add for StoredF32 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredF32 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<StoredF32> for f32 {
fn from(value: StoredF32) -> Self {
value.0
}
}
impl From<Dollars> for StoredF32 {
fn from(value: Dollars) -> Self {
StoredF32::from(f64::from(value))
}
}
impl Div<Dollars> for StoredF32 {
type Output = Self;
fn div(self, rhs: Dollars) -> Self::Output {
Self::from(self.0 as f64 / *rhs)
}
}
impl Div<StoredF32> for StoredF32 {
type Output = Self;
fn div(self, rhs: StoredF32) -> Self::Output {
Self::from(self.0 / rhs.0)
}
}
impl Mul<usize> for StoredF32 {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Self(self.0 * rhs as f32)
}
}
impl Mul<StoredF32> for usize {
type Output = StoredF32;
fn mul(self, rhs: StoredF32) -> Self::Output {
StoredF32(self as f32 * rhs.0)
}
}
impl Sub<StoredF32> for StoredF32 {
type Output = Self;
fn sub(self, rhs: StoredF32) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl PartialEq for StoredF32 {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for StoredF32 {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for StoredF32 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for StoredF32 {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}
impl Printable for StoredF32 {
fn to_string() -> &'static str {
"f32"
}
fn to_possible_strings() -> &'static [&'static str] {
&["f32"]
}
}

View File

@@ -0,0 +1,149 @@
use std::{
cmp::Ordering,
f64,
ops::{Add, AddAssign, Div, Mul},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{Bitcoin, Dollars};
#[derive(
Debug,
Deref,
Default,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredF64(f64);
impl StoredF64 {
pub const NAN: Self = Self(f64::NAN);
}
impl From<f64> for StoredF64 {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<usize> for StoredF64 {
fn from(value: usize) -> Self {
Self(value as f64)
}
}
impl CheckedSub<StoredF64> for StoredF64 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
Some(Self(self.0 - rhs.0))
}
}
impl Mul<usize> for StoredF64 {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Self(self.0 * rhs as f64)
}
}
impl Div<usize> for StoredF64 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as f64)
}
}
impl Div<StoredF64> for StoredF64 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}
impl Add for StoredF64 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredF64 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<StoredF64> for f64 {
fn from(value: StoredF64) -> Self {
value.0
}
}
impl From<Dollars> for StoredF64 {
fn from(value: Dollars) -> Self {
Self(f64::from(value))
}
}
impl CheckedSub<usize> for StoredF64 {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Self(self.0 - rhs as f64))
}
}
impl PartialEq for StoredF64 {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for StoredF64 {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for StoredF64 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for StoredF64 {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}
impl From<Bitcoin> for StoredF64 {
fn from(value: Bitcoin) -> Self {
Self(f64::from(value))
}
}
impl Printable for StoredF64 {
fn to_string() -> &'static str {
"f64"
}
fn to_possible_strings() -> &'static [&'static str] {
&["f64"]
}
}

View File

@@ -0,0 +1,183 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{
EmptyOutputIndex, OpReturnIndex, P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex,
P2PK65AddressIndex, P2PKHAddressIndex, P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex,
P2WSHAddressIndex, UnknownOutputIndex,
};
#[derive(
Debug,
Deref,
Clone,
Default,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredU16(u16);
impl StoredU16 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u16) -> Self {
Self(counter)
}
}
impl From<u16> for StoredU16 {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for StoredU16 {
fn from(value: usize) -> Self {
if value > u16::MAX as usize {
panic!("usize too big (value = {value})")
}
Self(value as u16)
}
}
impl CheckedSub<StoredU16> for StoredU16 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU16 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u16)
}
}
impl Add for StoredU16 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU16 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU16 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u16::MAX as f64 {
panic!()
}
Self(value as u16)
}
}
impl From<StoredU16> for f64 {
fn from(value: StoredU16) -> Self {
value.0 as f64
}
}
impl From<StoredU16> for usize {
fn from(value: StoredU16) -> Self {
value.0 as usize
}
}
impl From<P2PK65AddressIndex> for StoredU16 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PK33AddressIndex> for StoredU16 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PKHAddressIndex> for StoredU16 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<OpReturnIndex> for StoredU16 {
fn from(value: OpReturnIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2MSOutputIndex> for StoredU16 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2SHAddressIndex> for StoredU16 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WSHAddressIndex> for StoredU16 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WPKHAddressIndex> for StoredU16 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2TRAddressIndex> for StoredU16 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2AAddressIndex> for StoredU16 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<UnknownOutputIndex> for StoredU16 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<EmptyOutputIndex> for StoredU16 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl Printable for StoredU16 {
fn to_string() -> &'static str {
"u16"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u16"]
}
}

View File

@@ -0,0 +1,183 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{
EmptyOutputIndex, OpReturnIndex, P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex,
P2PK65AddressIndex, P2PKHAddressIndex, P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex,
P2WSHAddressIndex, UnknownOutputIndex,
};
#[derive(
Debug,
Deref,
Clone,
Default,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredU32(u32);
impl StoredU32 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u32) -> Self {
Self(counter)
}
}
impl From<u32> for StoredU32 {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for StoredU32 {
fn from(value: usize) -> Self {
if value > u32::MAX as usize {
panic!("usize too big (value = {value})")
}
Self(value as u32)
}
}
impl CheckedSub<StoredU32> for StoredU32 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU32 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u32)
}
}
impl Add for StoredU32 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU32 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU32 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u32)
}
}
impl From<StoredU32> for f64 {
fn from(value: StoredU32) -> Self {
value.0 as f64
}
}
impl From<StoredU32> for usize {
fn from(value: StoredU32) -> Self {
value.0 as usize
}
}
impl From<P2PK65AddressIndex> for StoredU32 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PK33AddressIndex> for StoredU32 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PKHAddressIndex> for StoredU32 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<OpReturnIndex> for StoredU32 {
fn from(value: OpReturnIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2MSOutputIndex> for StoredU32 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2SHAddressIndex> for StoredU32 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WSHAddressIndex> for StoredU32 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WPKHAddressIndex> for StoredU32 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2TRAddressIndex> for StoredU32 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2AAddressIndex> for StoredU32 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<UnknownOutputIndex> for StoredU32 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<EmptyOutputIndex> for StoredU32 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl Printable for StoredU32 {
fn to_string() -> &'static str {
"u32"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u32"]
}
}

View File

@@ -0,0 +1,229 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{
DateIndex, EmptyOutputIndex, Height, InputIndex, MonthIndex, OpReturnIndex, OutputIndex,
P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex, P2PK65AddressIndex, P2PKHAddressIndex,
P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex, P2WSHAddressIndex, TxIndex,
UnknownOutputIndex, YearIndex,
};
#[derive(
Debug,
Default,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredU64(u64);
impl StoredU64 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u64) -> Self {
Self(counter)
}
}
impl From<u64> for StoredU64 {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<StoredU64> for u64 {
fn from(value: StoredU64) -> Self {
value.0
}
}
impl From<StoredU64> for usize {
fn from(value: StoredU64) -> Self {
value.0 as usize
}
}
impl From<usize> for StoredU64 {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl CheckedSub<StoredU64> for StoredU64 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU64 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u64)
}
}
impl Add for StoredU64 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU64 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU64 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u64)
}
}
impl From<StoredU64> for f64 {
fn from(value: StoredU64) -> Self {
value.0 as f64
}
}
impl From<TxIndex> for StoredU64 {
fn from(value: TxIndex) -> Self {
Self(*value as u64)
}
}
impl From<InputIndex> for StoredU64 {
fn from(value: InputIndex) -> Self {
Self(*value)
}
}
impl From<Height> for StoredU64 {
fn from(value: Height) -> Self {
Self(*value as u64)
}
}
impl From<OutputIndex> for StoredU64 {
fn from(value: OutputIndex) -> Self {
Self(*value)
}
}
impl From<DateIndex> for StoredU64 {
fn from(value: DateIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<MonthIndex> for StoredU64 {
fn from(value: MonthIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<YearIndex> for StoredU64 {
fn from(value: YearIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2PK65AddressIndex> for StoredU64 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2PK33AddressIndex> for StoredU64 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2PKHAddressIndex> for StoredU64 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<OpReturnIndex> for StoredU64 {
fn from(value: OpReturnIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2MSOutputIndex> for StoredU64 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2SHAddressIndex> for StoredU64 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2WSHAddressIndex> for StoredU64 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2WPKHAddressIndex> for StoredU64 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2TRAddressIndex> for StoredU64 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2AAddressIndex> for StoredU64 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<UnknownOutputIndex> for StoredU64 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<EmptyOutputIndex> for StoredU64 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(u64::from(value))
}
}
impl Printable for StoredU64 {
fn to_string() -> &'static str {
"u64"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u64"]
}
}

View File

@@ -0,0 +1,110 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
pub type StoredPhantom = StoredU8;
#[derive(
Default,
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct StoredU8(u8);
impl StoredU8 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u8) -> Self {
Self(counter)
}
}
impl From<u8> for StoredU8 {
fn from(value: u8) -> Self {
Self(value)
}
}
impl From<usize> for StoredU8 {
fn from(value: usize) -> Self {
Self(value as u8)
}
}
impl CheckedSub<StoredU8> for StoredU8 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU8 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u8)
}
}
impl Add for StoredU8 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU8 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU8 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u8)
}
}
impl From<StoredU8> for f64 {
fn from(value: StoredU8) -> Self {
value.0 as f64
}
}
impl Add<usize> for StoredU8 {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0.checked_add(rhs as u8).unwrap())
}
}
impl From<StoredU8> for usize {
fn from(value: StoredU8) -> Self {
value.0 as usize
}
}
impl Printable for StoredU8 {
fn to_string() -> &'static str {
"u8"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u8"]
}
}

View File

@@ -0,0 +1,154 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, StoredCompressed};
use derive_deref::Deref;
use jiff::{civil::date, tz::TimeZone};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Date;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct Timestamp(u32);
const ONE_HOUR_IN_SEC: u32 = 60 * 60;
const ONE_DAY_IN_SEC: u32 = 24 * 60 * 60;
const ONE_DAY_IN_SEC_F64: f64 = ONE_DAY_IN_SEC as f64;
impl Timestamp {
pub const ZERO: Self = Self(0);
pub fn new(timestamp: u32) -> Self {
Self(timestamp)
}
pub fn floor_seconds(self) -> Self {
let zoned = jiff::Timestamp::from(self).to_zoned(TimeZone::UTC);
let date_time = jiff::civil::DateTime::from(zoned);
let trunc_date_time = date(date_time.year(), date_time.month(), date_time.day()).at(
date_time.hour(),
date_time.minute(),
0,
0,
);
Self::from(trunc_date_time.to_zoned(TimeZone::UTC).unwrap().timestamp())
}
#[inline]
pub fn difference_in_days_between(&self, older: Self) -> usize {
// if self.0 < older.0 {
// unreachable!()
// }
((self.0 - older.0) / ONE_DAY_IN_SEC) as usize
}
#[inline]
pub fn difference_in_days_between_float(&self, older: Self) -> f64 {
// if self.0 < older.0 {
// unreachable!()
// }
(self.0 - older.0) as f64 / ONE_DAY_IN_SEC_F64
}
#[inline]
pub fn is_more_than_hour(&self) -> bool {
self.0 >= ONE_HOUR_IN_SEC
}
}
impl From<u32> for Timestamp {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<jiff::Timestamp> for Timestamp {
fn from(value: jiff::Timestamp) -> Self {
Self(value.as_second() as u32)
}
}
impl From<Timestamp> for jiff::Timestamp {
fn from(value: Timestamp) -> Self {
jiff::Timestamp::from_second(*value as i64).unwrap()
}
}
impl From<bitcoin::locktime::absolute::Time> for Timestamp {
fn from(value: bitcoin::locktime::absolute::Time) -> Self {
Self(value.to_consensus_u32())
}
}
impl From<usize> for Timestamp {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Date> for Timestamp {
fn from(value: Date) -> Self {
Self::from(
jiff::civil::Date::from(value)
.to_zoned(TimeZone::UTC)
.unwrap()
.timestamp(),
)
}
}
impl CheckedSub<Timestamp> for Timestamp {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for Timestamp {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u32)
}
}
impl Add for Timestamp {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Timestamp {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for Timestamp {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u32)
}
}
impl From<Timestamp> for f64 {
fn from(value: Timestamp) -> Self {
value.0 as f64
}
}

View File

@@ -0,0 +1,42 @@
use std::{fmt, mem};
use bitcoin::hashes::Hash;
use derive_deref::Deref;
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(Debug, Deref, Clone, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct Txid([u8; 32]);
impl From<bitcoin::Txid> for Txid {
fn from(value: bitcoin::Txid) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<Txid> for bitcoin::Txid {
fn from(value: Txid) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<&Txid> for bitcoin::Txid {
fn from(value: &Txid) -> Self {
bitcoin::Txid::from_slice(&value.0).unwrap()
}
}
impl fmt::Display for Txid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", bitcoin::Txid::from(self))
}
}
impl Serialize for Txid {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

View File

@@ -0,0 +1,60 @@
use byteview::ByteView;
use derive_deref::Deref;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_8bytes;
use super::Txid;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
)]
pub struct TxidPrefix([u8; 8]);
impl From<Txid> for TxidPrefix {
fn from(value: Txid) -> Self {
Self::from(&value)
}
}
impl From<&Txid> for TxidPrefix {
fn from(value: &Txid) -> Self {
Self(copy_first_8bytes(&value[..]).unwrap())
}
}
impl From<ByteView> for TxidPrefix {
fn from(value: ByteView) -> Self {
Self::read_from_bytes(&value).unwrap()
}
}
impl From<&TxidPrefix> for ByteView {
fn from(value: &TxidPrefix) -> Self {
Self::new(value.as_bytes())
}
}
impl From<TxidPrefix> for ByteView {
fn from(value: TxidPrefix) -> Self {
Self::from(&value)
}
}
impl From<[u8; 8]> for TxidPrefix {
fn from(value: [u8; 8]) -> Self {
Self(value)
}
}

View File

@@ -0,0 +1,124 @@
use std::ops::{Add, AddAssign};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use byteview::ByteView;
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_4bytes;
use super::StoredU32;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct TxIndex(u32);
impl TxIndex {
pub const ZERO: Self = Self(0);
pub fn new(txindex: u32) -> Self {
Self(txindex)
}
pub fn incremented(self) -> Self {
Self(*self + 1)
}
}
impl Add<TxIndex> for TxIndex {
type Output = Self;
fn add(self, rhs: TxIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Add<usize> for TxIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u32)
}
}
impl AddAssign<TxIndex> for TxIndex {
fn add_assign(&mut self, rhs: TxIndex) {
self.0 += rhs.0
}
}
impl CheckedSub<TxIndex> for TxIndex {
fn checked_sub(self, rhs: TxIndex) -> Option<Self> {
self.0.checked_sub(rhs.0).map(TxIndex::from)
}
}
impl From<u32> for TxIndex {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<u64> for TxIndex {
fn from(value: u64) -> Self {
Self(value as u32)
}
}
impl From<TxIndex> for u64 {
fn from(value: TxIndex) -> Self {
value.0 as u64
}
}
impl From<usize> for TxIndex {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<TxIndex> for usize {
fn from(value: TxIndex) -> Self {
value.0 as usize
}
}
impl From<ByteView> for TxIndex {
fn from(value: ByteView) -> Self {
Self(u32::from_be_bytes(copy_first_4bytes(&value).unwrap()))
}
}
impl From<TxIndex> for ByteView {
fn from(value: TxIndex) -> Self {
Self::new(&value.to_be_bytes())
}
}
impl From<TxIndex> for StoredU32 {
fn from(value: TxIndex) -> Self {
Self::from(value.0)
}
}
impl Printable for TxIndex {
fn to_string() -> &'static str {
"txindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["tx", "txindex"]
}
}

View File

@@ -0,0 +1,54 @@
use brk_vecs::StoredCompressed;
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::StoredU16;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
StoredCompressed,
)]
pub struct TxVersion(u16);
impl TxVersion {
pub const ONE: Self = Self(1);
pub const TWO: Self = Self(2);
pub const THREE: Self = Self(3);
pub const NON_STANDARD: Self = Self(u16::MAX);
}
impl From<bitcoin::transaction::Version> for TxVersion {
fn from(value: bitcoin::transaction::Version) -> Self {
match value.0 {
1 => Self::ONE,
2 => Self::TWO,
3 => Self::THREE,
_ => Self::NON_STANDARD,
}
}
}
impl From<TxVersion> for bitcoin::transaction::Version {
fn from(value: TxVersion) -> Self {
Self(value.0 as i32)
}
}
impl From<TxVersion> for StoredU16 {
fn from(value: TxVersion) -> Self {
Self::from(value.0)
}
}

View File

@@ -0,0 +1,119 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, StoredCompressed};
use byteview::ByteView;
use serde::Serialize;
use zerocopy::IntoBytes;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_4bytes;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct TypeIndex(u32);
impl TypeIndex {
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn incremented(self) -> Self {
Self(self.0 + 1)
}
pub fn copy_then_increment(&mut self) -> Self {
let i = *self;
self.increment();
i
}
}
impl From<u32> for TypeIndex {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<TypeIndex> for u32 {
fn from(value: TypeIndex) -> Self {
value.0
}
}
impl From<u64> for TypeIndex {
fn from(value: u64) -> Self {
Self(value as u32)
}
}
impl From<TypeIndex> for u64 {
fn from(value: TypeIndex) -> Self {
value.0 as u64
}
}
impl From<usize> for TypeIndex {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<TypeIndex> for usize {
fn from(value: TypeIndex) -> Self {
value.0 as usize
}
}
impl Add<u32> for TypeIndex {
type Output = Self;
fn add(self, rhs: u32) -> Self::Output {
Self(self.0 + rhs)
}
}
impl Add<usize> for TypeIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u32)
}
}
impl Add<TypeIndex> for TypeIndex {
type Output = Self;
fn add(self, rhs: TypeIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl From<&[u8]> for TypeIndex {
fn from(value: &[u8]) -> Self {
Self(u32::from_be_bytes(copy_first_4bytes(value).unwrap()))
}
}
impl From<ByteView> for TypeIndex {
fn from(value: ByteView) -> Self {
Self::from(value.as_bytes())
}
}
impl From<TypeIndex> for ByteView {
fn from(value: TypeIndex) -> Self {
Self::new(&value.0.to_be_bytes())
}
}
impl CheckedSub<TypeIndex> for TypeIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}

View File

@@ -0,0 +1,47 @@
use byteview::ByteView;
use serde::Serialize;
use super::{OutputIndex, TypeIndex};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)]
pub struct TypeIndexWithOutputindex {
typeindex: TypeIndex,
outputindex: OutputIndex,
}
impl From<(TypeIndex, OutputIndex)> for TypeIndexWithOutputindex {
fn from(value: (TypeIndex, OutputIndex)) -> Self {
Self {
typeindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for TypeIndexWithOutputindex {
fn from(value: ByteView) -> Self {
let typeindex = TypeIndex::from(&value[0..4]);
let outputindex = OutputIndex::from(&value[4..12]);
Self {
typeindex,
outputindex,
}
}
}
impl From<TypeIndexWithOutputindex> for ByteView {
fn from(value: TypeIndexWithOutputindex) -> Self {
ByteView::from(&value)
}
}
impl From<&TypeIndexWithOutputindex> for ByteView {
fn from(value: &TypeIndexWithOutputindex) -> Self {
ByteView::from(
[
u32::from(value.typeindex).to_be_bytes().as_slice(),
u64::from(value.outputindex).to_be_bytes().as_slice(),
]
.concat(),
)
}
}

View File

@@ -0,0 +1,15 @@
use byteview::ByteView;
#[derive(Debug, Clone)]
pub struct Unit;
impl From<ByteView> for Unit {
fn from(_: ByteView) -> Self {
Self
}
}
impl From<Unit> for ByteView {
fn from(_: Unit) -> Self {
Self::new(&[])
}
}

View File

@@ -0,0 +1,70 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct UnknownOutputIndex(TypeIndex);
impl From<TypeIndex> for UnknownOutputIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<UnknownOutputIndex> for u64 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<UnknownOutputIndex> for usize {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for UnknownOutputIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for UnknownOutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<UnknownOutputIndex> for UnknownOutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for UnknownOutputIndex {
fn to_string() -> &'static str {
"unknownoutputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["unknownout", "unknownoutputindex"]
}
}

View File

@@ -0,0 +1,31 @@
use derive_deref::Deref;
#[derive(Debug, Deref, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Vin(u32);
impl Vin {
pub const ZERO: Self = Vin(0);
pub const ONE: Self = Vin(1);
pub fn is_zero(&self) -> bool {
*self == Self::ZERO
}
}
impl From<u32> for Vin {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for Vin {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Vin> for u64 {
fn from(value: Vin) -> Self {
value.0 as u64
}
}

View File

@@ -0,0 +1,30 @@
use derive_deref::Deref;
#[derive(Debug, Deref, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Vout(u32);
impl Vout {
const ZERO: Self = Vout(0);
pub fn is_zero(&self) -> bool {
*self == Self::ZERO
}
}
impl From<u32> for Vout {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for Vout {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Vout> for u64 {
fn from(value: Vout) -> Self {
value.0 as u64
}
}

View File

@@ -0,0 +1,127 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct WeekIndex(u16);
impl From<u16> for WeekIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<WeekIndex> for u16 {
fn from(value: WeekIndex) -> Self {
value.0
}
}
impl From<usize> for WeekIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<WeekIndex> for usize {
fn from(value: WeekIndex) -> Self {
value.0 as usize
}
}
impl Add<WeekIndex> for WeekIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for WeekIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for WeekIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl Add<usize> for WeekIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl From<DateIndex> for WeekIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for WeekIndex {
fn from(value: Date) -> Self {
let date = jiff::civil::Date::from(value).iso_week_date();
let mut week: u16 = 0;
let mut year = 2009;
while date.year() > year {
let d = jiff::civil::Date::new(year, 6, 6).unwrap();
let i = d.iso_week_date();
let w = i.weeks_in_year();
week += w as u16;
year += 1;
}
week += date.week() as u16;
week -= 1;
Self(week)
}
}
impl CheckedSub for WeekIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for WeekIndex {
fn to_string() -> &'static str {
"weekindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["w", "week", "weekindex"]
}
}

View File

@@ -0,0 +1,81 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::StoredCompressed;
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
StoredCompressed,
)]
pub struct Weight(u64);
impl From<bitcoin::Weight> for Weight {
fn from(value: bitcoin::Weight) -> Self {
Self(value.to_wu())
}
}
impl From<Weight> for bitcoin::Weight {
fn from(value: Weight) -> Self {
Self::from_wu(value.0)
}
}
impl From<usize> for Weight {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<f64> for Weight {
fn from(value: f64) -> Self {
Self(value as u64)
}
}
impl From<Weight> for f64 {
fn from(value: Weight) -> Self {
value.0 as f64
}
}
impl Add for Weight {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Weight {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Div<usize> for Weight {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(self.0 as usize / rhs)
}
}
impl Div<Weight> for Weight {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}

View File

@@ -0,0 +1,122 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex, MonthIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct YearIndex(u16);
impl From<u16> for YearIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for YearIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<YearIndex> for u64 {
fn from(value: YearIndex) -> Self {
value.0 as u64
}
}
impl From<YearIndex> for usize {
fn from(value: YearIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for YearIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<YearIndex> for YearIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for YearIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for YearIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<DateIndex> for YearIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for YearIndex {
fn from(value: Date) -> Self {
Self(value.year() - 2009)
}
}
impl From<YearIndex> for u16 {
fn from(value: YearIndex) -> Self {
value.0
}
}
impl CheckedSub for YearIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl From<MonthIndex> for YearIndex {
fn from(value: MonthIndex) -> Self {
Self((usize::from(value) / 12) as u16)
}
}
impl Printable for YearIndex {
fn to_string() -> &'static str {
"yearindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["y", "year", "yearindex"]
}
}