mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-17 14:08:10 -07:00
computer: store part 6
This commit is contained in:
@@ -8,6 +8,7 @@ homepage.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
bincode = { workspace = true }
|
||||
bitcoin = { workspace = true }
|
||||
bitcoincore-rpc = { workspace = true }
|
||||
brk_core = { workspace = true }
|
||||
@@ -16,7 +17,6 @@ brk_fetcher = { workspace = true }
|
||||
brk_indexer = { workspace = true }
|
||||
brk_logger = { workspace = true }
|
||||
brk_parser = { workspace = true }
|
||||
brk_state = { workspace = true }
|
||||
brk_store = { workspace = true }
|
||||
brk_vec = { workspace = true }
|
||||
color-eyre = { workspace = true }
|
||||
@@ -25,3 +25,6 @@ fjall = { workspace = true }
|
||||
jiff = { workspace = true }
|
||||
log = { workspace = true }
|
||||
rayon = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
zerocopy = { workspace = true }
|
||||
zerocopy-derive = { workspace = true }
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
use super::{GroupFilter, GroupedByFromSize, GroupedBySizeRange, GroupedByUpToSize};
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct AddressGroups<T> {
|
||||
pub by_from_size: GroupedByFromSize<T>,
|
||||
pub by_size_range: GroupedBySizeRange<T>,
|
||||
pub by_up_to_size: GroupedByUpToSize<T>,
|
||||
}
|
||||
|
||||
impl<T> AddressGroups<T> {
|
||||
pub fn as_mut_vecs(&mut self) -> Vec<&mut T> {
|
||||
self.by_from_size
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.chain(self.by_size_range.as_mut_vec())
|
||||
.chain(self.by_up_to_size.as_mut_vec())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
pub fn as_mut_separate_vecs(&mut self) -> Vec<&mut T> {
|
||||
self.by_size_range
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
pub fn as_mut_overlapping_vecs(&mut self) -> Vec<&mut T> {
|
||||
self.by_up_to_size
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.chain(self.by_from_size.as_mut_vec())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AddressGroups<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> Vec<&T> {
|
||||
self.by_size_range
|
||||
.vecs()
|
||||
.into_iter()
|
||||
.chain(self.by_up_to_size.vecs())
|
||||
.chain(self.by_from_size.vecs())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<AddressGroups<T>> for AddressGroups<(GroupFilter, T)> {
|
||||
fn from(value: AddressGroups<T>) -> Self {
|
||||
Self {
|
||||
by_size_range: GroupedBySizeRange::from(value.by_size_range),
|
||||
by_up_to_size: GroupedByUpToSize::from(value.by_up_to_size),
|
||||
by_from_size: GroupedByFromSize::from(value.by_from_size),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
use std::{
|
||||
mem,
|
||||
ops::{Add, AddAssign},
|
||||
};
|
||||
|
||||
use brk_core::OutputType;
|
||||
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct GroupedByAddressType<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> GroupedByAddressType<T> {
|
||||
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> GroupedByAddressType<(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<GroupedByAddressType<T>> for GroupedByAddressType<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByAddressType<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 GroupedByAddressType<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 GroupedByAddressType<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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct GroupedByDateRange<T> {
|
||||
pub start_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_15y: T,
|
||||
pub _15y_to_end: T,
|
||||
}
|
||||
|
||||
impl<T> From<GroupedByDateRange<T>> for GroupedByDateRange<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByDateRange<T>) -> Self {
|
||||
Self {
|
||||
start_to_1d: (GroupFilter::To(1), value.start_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_15y: (GroupFilter::Range(10 * 365..15 * 365), value._10y_to_15y),
|
||||
_15y_to_end: (GroupFilter::From(15 * 365), value._15y_to_end),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedByDateRange<T> {
|
||||
pub fn as_vec(&mut self) -> [&T; 19] {
|
||||
[
|
||||
&self.start_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_15y,
|
||||
&self._15y_to_end,
|
||||
]
|
||||
}
|
||||
|
||||
pub fn as_mut_vec(&mut self) -> [&mut T; 19] {
|
||||
[
|
||||
&mut self.start_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_15y,
|
||||
&mut self._15y_to_end,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedByDateRange<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> [&T; 19] {
|
||||
[
|
||||
&self.start_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_15y.1,
|
||||
&self._15y_to_end.1,
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
use brk_core::{HalvingEpoch, Height};
|
||||
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct GroupedByEpoch<T> {
|
||||
pub _0: T,
|
||||
pub _1: T,
|
||||
pub _2: T,
|
||||
pub _3: T,
|
||||
pub _4: T,
|
||||
}
|
||||
|
||||
impl<T> From<GroupedByEpoch<T>> for GroupedByEpoch<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByEpoch<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> GroupedByEpoch<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> GroupedByEpoch<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> [&T; 5] {
|
||||
[&self._0.1, &self._1.1, &self._2.1, &self._3.1, &self._4.1]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct GroupedByFromDate<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 _15y: T,
|
||||
}
|
||||
|
||||
impl<T> GroupedByFromDate<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._15y,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedByFromDate<(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._15y.1,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<GroupedByFromDate<T>> for GroupedByFromDate<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByFromDate<T>) -> Self {
|
||||
Self {
|
||||
_1d: (GroupFilter::From(1), value._1d),
|
||||
_1w: (GroupFilter::From(7), value._1w),
|
||||
_1m: (GroupFilter::From(30), value._1m),
|
||||
_2m: (GroupFilter::From(2 * 30), value._2m),
|
||||
_3m: (GroupFilter::From(3 * 30), value._3m),
|
||||
_4m: (GroupFilter::From(4 * 30), value._4m),
|
||||
_5m: (GroupFilter::From(5 * 30), value._5m),
|
||||
_6m: (GroupFilter::From(6 * 30), value._6m),
|
||||
_1y: (GroupFilter::From(365), value._1y),
|
||||
_2y: (GroupFilter::From(2 * 365), value._2y),
|
||||
_3y: (GroupFilter::From(3 * 365), value._3y),
|
||||
_4y: (GroupFilter::From(4 * 365), value._4y),
|
||||
_5y: (GroupFilter::From(5 * 365), value._5y),
|
||||
_6y: (GroupFilter::From(6 * 365), value._6y),
|
||||
_7y: (GroupFilter::From(7 * 365), value._7y),
|
||||
_8y: (GroupFilter::From(8 * 365), value._8y),
|
||||
_10y: (GroupFilter::From(10 * 365), value._10y),
|
||||
_15y: (GroupFilter::From(15 * 365), value._15y),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
use brk_core::Sats;
|
||||
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct GroupedByFromSize<T> {
|
||||
pub _1_000sats: T,
|
||||
pub _1btc: T,
|
||||
pub _10btc: T,
|
||||
pub _100btc: T,
|
||||
}
|
||||
|
||||
impl<T> GroupedByFromSize<T> {
|
||||
pub fn as_mut_vec(&mut self) -> [&mut T; 4] {
|
||||
[
|
||||
&mut self._1_000sats,
|
||||
&mut self._1btc,
|
||||
&mut self._10btc,
|
||||
&mut self._100btc,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedByFromSize<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> [&T; 4] {
|
||||
[
|
||||
&self._1_000sats.1,
|
||||
&self._1btc.1,
|
||||
&self._10btc.1,
|
||||
&self._100btc.1,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<GroupedByFromSize<T>> for GroupedByFromSize<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByFromSize<T>) -> Self {
|
||||
Self {
|
||||
_1_000sats: (GroupFilter::From(1_000), value._1_000sats),
|
||||
_1btc: (GroupFilter::From(usize::from(Sats::ONE_BTC)), value._1btc),
|
||||
_10btc: (
|
||||
GroupFilter::From(usize::from(10 * Sats::ONE_BTC)),
|
||||
value._10btc,
|
||||
),
|
||||
_100btc: (
|
||||
GroupFilter::From(usize::from(100 * Sats::ONE_BTC)),
|
||||
value._100btc,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
use brk_core::Sats;
|
||||
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct GroupedBySizeRange<T> {
|
||||
pub _0sats: T,
|
||||
pub from_1sat_to_10sats: T,
|
||||
pub from_10sats_to_100sats: T,
|
||||
pub from_100sats_to_1_000sats: T,
|
||||
pub from_1_000sats_to_10_000sats: T,
|
||||
pub from_10_000sats_to_100_000sats: T,
|
||||
pub from_100_000sats_to_1_000_000sats: T,
|
||||
pub from_1_000_000sats_to_10_000_000sats: T,
|
||||
pub from_10_000_000sats_to_1btc: T,
|
||||
pub from_1btc_to_10btc: T,
|
||||
pub from_10btc_to_100btc: T,
|
||||
pub from_100btc_to_1_000btc: T,
|
||||
pub from_1_000btc_to_10_000btc: T,
|
||||
pub from_10_000btc_to_100_000btc: T,
|
||||
pub from_100_000btc: T,
|
||||
}
|
||||
|
||||
impl<T> From<GroupedBySizeRange<T>> for GroupedBySizeRange<(GroupFilter, T)> {
|
||||
fn from(value: GroupedBySizeRange<T>) -> Self {
|
||||
#[allow(clippy::inconsistent_digit_grouping)]
|
||||
Self {
|
||||
_0sats: (GroupFilter::To(1), value._0sats),
|
||||
from_1sat_to_10sats: (GroupFilter::Range(1..10), value.from_1sat_to_10sats),
|
||||
from_10sats_to_100sats: (GroupFilter::Range(10..100), value.from_10sats_to_100sats),
|
||||
from_100sats_to_1_000sats: (
|
||||
GroupFilter::Range(100..1_000),
|
||||
value.from_100sats_to_1_000sats,
|
||||
),
|
||||
from_1_000sats_to_10_000sats: (
|
||||
GroupFilter::Range(1_000..10_000),
|
||||
value.from_1_000sats_to_10_000sats,
|
||||
),
|
||||
from_10_000sats_to_100_000sats: (
|
||||
GroupFilter::Range(10_000..100_000),
|
||||
value.from_10_000sats_to_100_000sats,
|
||||
),
|
||||
from_100_000sats_to_1_000_000sats: (
|
||||
GroupFilter::Range(100_000..1_000_000),
|
||||
value.from_100_000sats_to_1_000_000sats,
|
||||
),
|
||||
from_1_000_000sats_to_10_000_000sats: (
|
||||
GroupFilter::Range(1_000_000..10_000_000),
|
||||
value.from_1_000_000sats_to_10_000_000sats,
|
||||
),
|
||||
from_10_000_000sats_to_1btc: (
|
||||
GroupFilter::Range(10_000_000..1_00_000_000),
|
||||
value.from_10_000_000sats_to_1btc,
|
||||
),
|
||||
from_1btc_to_10btc: (
|
||||
GroupFilter::Range(1_00_000_000..10_00_000_000),
|
||||
value.from_1btc_to_10btc,
|
||||
),
|
||||
from_10btc_to_100btc: (
|
||||
GroupFilter::Range(10_00_000_000..100_00_000_000),
|
||||
value.from_10btc_to_100btc,
|
||||
),
|
||||
from_100btc_to_1_000btc: (
|
||||
GroupFilter::Range(100_00_000_000..1_000_00_000_000),
|
||||
value.from_100btc_to_1_000btc,
|
||||
),
|
||||
from_1_000btc_to_10_000btc: (
|
||||
GroupFilter::Range(1_000_00_000_000..10_000_00_000_000),
|
||||
value.from_1_000btc_to_10_000btc,
|
||||
),
|
||||
from_10_000btc_to_100_000btc: (
|
||||
GroupFilter::Range(10_000_00_000_000..100_000_00_000_000),
|
||||
value.from_10_000btc_to_100_000btc,
|
||||
),
|
||||
from_100_000btc: (GroupFilter::From(100_000_00_000_000), value.from_100_000btc),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedBySizeRange<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.from_1sat_to_10sats
|
||||
} else if value < Sats::_100 {
|
||||
&mut self.from_10sats_to_100sats
|
||||
} else if value < Sats::_1K {
|
||||
&mut self.from_100sats_to_1_000sats
|
||||
} else if value < Sats::_10K {
|
||||
&mut self.from_1_000sats_to_10_000sats
|
||||
} else if value < Sats::_100K {
|
||||
&mut self.from_10_000sats_to_100_000sats
|
||||
} else if value < Sats::_1M {
|
||||
&mut self.from_100_000sats_to_1_000_000sats
|
||||
} else if value < Sats::_10M {
|
||||
&mut self.from_1_000_000sats_to_10_000_000sats
|
||||
} else if value < Sats::_1_BTC {
|
||||
&mut self.from_10_000_000sats_to_1btc
|
||||
} else if value < Sats::_10_BTC {
|
||||
&mut self.from_1btc_to_10btc
|
||||
} else if value < Sats::_100_BTC {
|
||||
&mut self.from_10btc_to_100btc
|
||||
} else if value < Sats::_1K_BTC {
|
||||
&mut self.from_100btc_to_1_000btc
|
||||
} else if value < Sats::_10K_BTC {
|
||||
&mut self.from_1_000btc_to_10_000btc
|
||||
} else if value < Sats::_100K_BTC {
|
||||
&mut self.from_10_000btc_to_100_000btc
|
||||
} else {
|
||||
&mut self.from_100_000btc
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_vec(&self) -> [&T; 15] {
|
||||
[
|
||||
&self._0sats,
|
||||
&self.from_1sat_to_10sats,
|
||||
&self.from_10sats_to_100sats,
|
||||
&self.from_100sats_to_1_000sats,
|
||||
&self.from_1_000sats_to_10_000sats,
|
||||
&self.from_10_000sats_to_100_000sats,
|
||||
&self.from_100_000sats_to_1_000_000sats,
|
||||
&self.from_1_000_000sats_to_10_000_000sats,
|
||||
&self.from_10_000_000sats_to_1btc,
|
||||
&self.from_1btc_to_10btc,
|
||||
&self.from_10btc_to_100btc,
|
||||
&self.from_100btc_to_1_000btc,
|
||||
&self.from_1_000btc_to_10_000btc,
|
||||
&self.from_10_000btc_to_100_000btc,
|
||||
&self.from_100_000btc,
|
||||
]
|
||||
}
|
||||
|
||||
pub fn as_typed_vec(&self) -> [(Sats, &T); 15] {
|
||||
[
|
||||
(Sats::ZERO, &self._0sats),
|
||||
(Sats::_1, &self.from_1sat_to_10sats),
|
||||
(Sats::_10, &self.from_10sats_to_100sats),
|
||||
(Sats::_100, &self.from_100sats_to_1_000sats),
|
||||
(Sats::_1K, &self.from_1_000sats_to_10_000sats),
|
||||
(Sats::_10K, &self.from_10_000sats_to_100_000sats),
|
||||
(Sats::_100K, &self.from_100_000sats_to_1_000_000sats),
|
||||
(Sats::_1M, &self.from_1_000_000sats_to_10_000_000sats),
|
||||
(Sats::_10M, &self.from_10_000_000sats_to_1btc),
|
||||
(Sats::_1_BTC, &self.from_1btc_to_10btc),
|
||||
(Sats::_10_BTC, &self.from_10btc_to_100btc),
|
||||
(Sats::_100_BTC, &self.from_100btc_to_1_000btc),
|
||||
(Sats::_1K_BTC, &self.from_1_000btc_to_10_000btc),
|
||||
(Sats::_10K_BTC, &self.from_10_000btc_to_100_000btc),
|
||||
(Sats::_100K_BTC, &self.from_100_000btc),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn as_mut_vec(&mut self) -> [&mut T; 15] {
|
||||
[
|
||||
&mut self._0sats,
|
||||
&mut self.from_1sat_to_10sats,
|
||||
&mut self.from_10sats_to_100sats,
|
||||
&mut self.from_100sats_to_1_000sats,
|
||||
&mut self.from_1_000sats_to_10_000sats,
|
||||
&mut self.from_10_000sats_to_100_000sats,
|
||||
&mut self.from_100_000sats_to_1_000_000sats,
|
||||
&mut self.from_1_000_000sats_to_10_000_000sats,
|
||||
&mut self.from_10_000_000sats_to_1btc,
|
||||
&mut self.from_1btc_to_10btc,
|
||||
&mut self.from_10btc_to_100btc,
|
||||
&mut self.from_100btc_to_1_000btc,
|
||||
&mut self.from_1_000btc_to_10_000btc,
|
||||
&mut self.from_10_000btc_to_100_000btc,
|
||||
&mut self.from_100_000btc,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedBySizeRange<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> [&T; 15] {
|
||||
[
|
||||
&self._0sats.1,
|
||||
&self.from_1sat_to_10sats.1,
|
||||
&self.from_10sats_to_100sats.1,
|
||||
&self.from_100sats_to_1_000sats.1,
|
||||
&self.from_1_000sats_to_10_000sats.1,
|
||||
&self.from_10_000sats_to_100_000sats.1,
|
||||
&self.from_100_000sats_to_1_000_000sats.1,
|
||||
&self.from_1_000_000sats_to_10_000_000sats.1,
|
||||
&self.from_10_000_000sats_to_1btc.1,
|
||||
&self.from_1btc_to_10btc.1,
|
||||
&self.from_10btc_to_100btc.1,
|
||||
&self.from_100btc_to_1_000btc.1,
|
||||
&self.from_1_000btc_to_10_000btc.1,
|
||||
&self.from_10_000btc_to_100_000btc.1,
|
||||
&self.from_100_000btc.1,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Add for GroupedBySizeRange<T>
|
||||
where
|
||||
T: Add<Output = T>,
|
||||
{
|
||||
type Output = Self;
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
_0sats: self._0sats + rhs._0sats,
|
||||
from_1sat_to_10sats: self.from_1sat_to_10sats + rhs.from_1sat_to_10sats,
|
||||
from_10sats_to_100sats: self.from_10sats_to_100sats + rhs.from_10sats_to_100sats,
|
||||
from_100sats_to_1_000sats: self.from_100sats_to_1_000sats
|
||||
+ rhs.from_100sats_to_1_000sats,
|
||||
from_1_000sats_to_10_000sats: self.from_1_000sats_to_10_000sats
|
||||
+ rhs.from_1_000sats_to_10_000sats,
|
||||
from_10_000sats_to_100_000sats: self.from_10_000sats_to_100_000sats
|
||||
+ rhs.from_10_000sats_to_100_000sats,
|
||||
from_100_000sats_to_1_000_000sats: self.from_100_000sats_to_1_000_000sats
|
||||
+ rhs.from_100_000sats_to_1_000_000sats,
|
||||
from_1_000_000sats_to_10_000_000sats: self.from_1_000_000sats_to_10_000_000sats
|
||||
+ rhs.from_1_000_000sats_to_10_000_000sats,
|
||||
from_10_000_000sats_to_1btc: self.from_10_000_000sats_to_1btc
|
||||
+ rhs.from_10_000_000sats_to_1btc,
|
||||
from_1btc_to_10btc: self.from_1btc_to_10btc + rhs.from_1btc_to_10btc,
|
||||
from_10btc_to_100btc: self.from_10btc_to_100btc + rhs.from_10btc_to_100btc,
|
||||
from_100btc_to_1_000btc: self.from_100btc_to_1_000btc + rhs.from_100btc_to_1_000btc,
|
||||
from_1_000btc_to_10_000btc: self.from_1_000btc_to_10_000btc
|
||||
+ rhs.from_1_000btc_to_10_000btc,
|
||||
from_10_000btc_to_100_000btc: self.from_10_000btc_to_100_000btc
|
||||
+ rhs.from_10_000btc_to_100_000btc,
|
||||
from_100_000btc: self.from_100_000btc + rhs.from_100_000btc,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AddAssign for GroupedBySizeRange<T>
|
||||
where
|
||||
T: AddAssign,
|
||||
{
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self._0sats += rhs._0sats;
|
||||
self.from_1sat_to_10sats += rhs.from_1sat_to_10sats;
|
||||
self.from_10sats_to_100sats += rhs.from_10sats_to_100sats;
|
||||
self.from_100sats_to_1_000sats += rhs.from_100sats_to_1_000sats;
|
||||
self.from_1_000sats_to_10_000sats += rhs.from_1_000sats_to_10_000sats;
|
||||
self.from_10_000sats_to_100_000sats += rhs.from_10_000sats_to_100_000sats;
|
||||
self.from_100_000sats_to_1_000_000sats += rhs.from_100_000sats_to_1_000_000sats;
|
||||
self.from_1_000_000sats_to_10_000_000sats += rhs.from_1_000_000sats_to_10_000_000sats;
|
||||
self.from_10_000_000sats_to_1btc += rhs.from_10_000_000sats_to_1btc;
|
||||
self.from_1btc_to_10btc += rhs.from_1btc_to_10btc;
|
||||
self.from_10btc_to_100btc += rhs.from_10btc_to_100btc;
|
||||
self.from_100btc_to_1_000btc += rhs.from_100btc_to_1_000btc;
|
||||
self.from_1_000btc_to_10_000btc += rhs.from_1_000btc_to_10_000btc;
|
||||
self.from_10_000btc_to_100_000btc += rhs.from_10_000btc_to_100_000btc;
|
||||
self.from_100_000btc += rhs.from_100_000btc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
use brk_core::OutputType;
|
||||
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct GroupedBySpendableType<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> GroupedBySpendableType<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> GroupedBySpendableType<(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<GroupedBySpendableType<T>> for GroupedBySpendableType<(GroupFilter, T)> {
|
||||
fn from(value: GroupedBySpendableType<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 GroupedBySpendableType<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 GroupedBySpendableType<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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct GroupedByTerm<T> {
|
||||
pub short: T,
|
||||
pub long: T,
|
||||
}
|
||||
|
||||
impl<T> GroupedByTerm<T> {
|
||||
pub fn as_mut_vec(&mut self) -> [&mut T; 2] {
|
||||
[&mut self.short, &mut self.long]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedByTerm<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> [&T; 2] {
|
||||
[&self.short.1, &self.long.1]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<GroupedByTerm<T>> for GroupedByTerm<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByTerm<T>) -> Self {
|
||||
Self {
|
||||
short: (GroupFilter::To(5 * 30), value.short),
|
||||
long: (GroupFilter::From(5 * 30), value.long),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
use brk_core::OutputType;
|
||||
|
||||
use super::{GroupedBySpendableType, GroupedByUnspendableType};
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct GroupedByType<T> {
|
||||
pub spendable: GroupedBySpendableType<T>,
|
||||
pub unspendable: GroupedByUnspendableType<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,
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct GroupedByUnspendableType<T> {
|
||||
pub opreturn: T,
|
||||
}
|
||||
|
||||
impl<T> GroupedByUnspendableType<T> {
|
||||
pub fn as_vec(&self) -> [&T; 1] {
|
||||
[&self.opreturn]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Add for GroupedByUnspendableType<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 GroupedByUnspendableType<T>
|
||||
where
|
||||
T: AddAssign,
|
||||
{
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.opreturn += rhs.opreturn;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct GroupedByUpToDate<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 _15y: T,
|
||||
}
|
||||
|
||||
impl<T> GroupedByUpToDate<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._15y,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedByUpToDate<(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._15y.1,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<GroupedByUpToDate<T>> for GroupedByUpToDate<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByUpToDate<T>) -> Self {
|
||||
Self {
|
||||
_1d: (GroupFilter::To(1), value._1d),
|
||||
_1w: (GroupFilter::To(7), value._1w),
|
||||
_1m: (GroupFilter::To(30), value._1m),
|
||||
_2m: (GroupFilter::To(2 * 30), value._2m),
|
||||
_3m: (GroupFilter::To(3 * 30), value._3m),
|
||||
_4m: (GroupFilter::To(4 * 30), value._4m),
|
||||
_5m: (GroupFilter::To(5 * 30), value._5m),
|
||||
_6m: (GroupFilter::To(6 * 30), value._6m),
|
||||
_1y: (GroupFilter::To(365), value._1y),
|
||||
_2y: (GroupFilter::To(2 * 365), value._2y),
|
||||
_3y: (GroupFilter::To(3 * 365), value._3y),
|
||||
_4y: (GroupFilter::To(4 * 365), value._4y),
|
||||
_5y: (GroupFilter::To(5 * 365), value._5y),
|
||||
_6y: (GroupFilter::To(6 * 365), value._6y),
|
||||
_7y: (GroupFilter::To(7 * 365), value._7y),
|
||||
_8y: (GroupFilter::To(8 * 365), value._8y),
|
||||
_10y: (GroupFilter::To(10 * 365), value._10y),
|
||||
_15y: (GroupFilter::To(15 * 365), value._15y),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use brk_core::Sats;
|
||||
|
||||
use super::GroupFilter;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct GroupedByUpToSize<T> {
|
||||
pub _1_000sats: T,
|
||||
pub _10_000sats: T,
|
||||
pub _1btc: T,
|
||||
pub _10btc: T,
|
||||
pub _100btc: T,
|
||||
}
|
||||
|
||||
impl<T> GroupedByUpToSize<T> {
|
||||
pub fn as_mut_vec(&mut self) -> [&mut T; 5] {
|
||||
[
|
||||
&mut self._1_000sats,
|
||||
&mut self._10_000sats,
|
||||
&mut self._1btc,
|
||||
&mut self._10btc,
|
||||
&mut self._100btc,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> GroupedByUpToSize<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> [&T; 5] {
|
||||
[
|
||||
&self._1_000sats.1,
|
||||
&self._10_000sats.1,
|
||||
&self._1btc.1,
|
||||
&self._10btc.1,
|
||||
&self._100btc.1,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<GroupedByUpToSize<T>> for GroupedByUpToSize<(GroupFilter, T)> {
|
||||
fn from(value: GroupedByUpToSize<T>) -> Self {
|
||||
Self {
|
||||
_1_000sats: (GroupFilter::To(1_000), value._1_000sats),
|
||||
_10_000sats: (GroupFilter::To(10_000), value._10_000sats),
|
||||
_1btc: (GroupFilter::To(usize::from(Sats::ONE_BTC)), value._1btc),
|
||||
_10btc: (
|
||||
GroupFilter::To(usize::from(10 * Sats::ONE_BTC)),
|
||||
value._10btc,
|
||||
),
|
||||
_100btc: (
|
||||
GroupFilter::To(usize::from(100 * Sats::ONE_BTC)),
|
||||
value._100btc,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use std::ops::Range;
|
||||
|
||||
use brk_core::{HalvingEpoch, OutputType};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum GroupFilter {
|
||||
All,
|
||||
To(usize),
|
||||
Range(Range<usize>),
|
||||
From(usize),
|
||||
Epoch(HalvingEpoch),
|
||||
Type(OutputType),
|
||||
}
|
||||
|
||||
impl GroupFilter {
|
||||
pub fn contains(&self, value: usize) -> bool {
|
||||
match self {
|
||||
GroupFilter::All => true,
|
||||
GroupFilter::To(to) => *to > value,
|
||||
GroupFilter::From(from) => *from <= value,
|
||||
GroupFilter::Range(r) => r.contains(&value),
|
||||
GroupFilter::Epoch(_) => false,
|
||||
GroupFilter::Type(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn includes(&self, other: &GroupFilter) -> bool {
|
||||
match self {
|
||||
GroupFilter::All => true,
|
||||
GroupFilter::To(to) => match other {
|
||||
GroupFilter::All => false,
|
||||
GroupFilter::To(to2) => to >= to2,
|
||||
GroupFilter::Range(range) => range.end <= *to,
|
||||
GroupFilter::From(_) => false,
|
||||
GroupFilter::Epoch(_) => false,
|
||||
GroupFilter::Type(_) => false,
|
||||
},
|
||||
GroupFilter::From(from) => match other {
|
||||
GroupFilter::All => false,
|
||||
GroupFilter::To(_) => false,
|
||||
GroupFilter::Range(range) => range.start >= *from,
|
||||
GroupFilter::From(from2) => from <= from2,
|
||||
GroupFilter::Epoch(_) => false,
|
||||
GroupFilter::Type(_) => false,
|
||||
},
|
||||
GroupFilter::Range(_) => false,
|
||||
GroupFilter::Epoch(_) => false,
|
||||
GroupFilter::Type(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
mod address;
|
||||
mod by_address_type;
|
||||
mod by_date_range;
|
||||
mod by_epoch;
|
||||
mod by_from_date;
|
||||
mod by_from_size;
|
||||
mod by_size_range;
|
||||
mod by_spendable_type;
|
||||
mod by_term;
|
||||
mod by_type;
|
||||
mod by_unspendable_type;
|
||||
mod by_up_to_date;
|
||||
mod by_up_to_size;
|
||||
mod filter;
|
||||
mod utxo;
|
||||
|
||||
pub use address::*;
|
||||
pub use by_address_type::*;
|
||||
pub use by_date_range::*;
|
||||
pub use by_epoch::*;
|
||||
pub use by_from_date::*;
|
||||
pub use by_from_size::*;
|
||||
pub use by_size_range::*;
|
||||
pub use by_spendable_type::*;
|
||||
pub use by_term::*;
|
||||
pub use by_type::*;
|
||||
pub use by_unspendable_type::*;
|
||||
pub use by_up_to_date::*;
|
||||
pub use by_up_to_size::*;
|
||||
pub use filter::*;
|
||||
pub use utxo::*;
|
||||
@@ -0,0 +1,91 @@
|
||||
use crate::{
|
||||
GroupFilter, GroupedByDateRange, GroupedByEpoch, GroupedByFromDate, GroupedByFromSize,
|
||||
GroupedBySizeRange, GroupedBySpendableType, GroupedByTerm, GroupedByUpToDate,
|
||||
GroupedByUpToSize,
|
||||
};
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct UTXOGroups<T> {
|
||||
pub all: T,
|
||||
pub by_date_range: GroupedByDateRange<T>,
|
||||
pub by_epoch: GroupedByEpoch<T>,
|
||||
pub by_from_date: GroupedByFromDate<T>,
|
||||
pub by_from_size: GroupedByFromSize<T>,
|
||||
pub by_size_range: GroupedBySizeRange<T>,
|
||||
pub by_term: GroupedByTerm<T>,
|
||||
pub by_type: GroupedBySpendableType<T>,
|
||||
pub by_up_to_date: GroupedByUpToDate<T>,
|
||||
pub by_up_to_size: GroupedByUpToSize<T>,
|
||||
}
|
||||
|
||||
impl<T> UTXOGroups<T> {
|
||||
pub fn as_mut_vecs(&mut self) -> Vec<&mut T> {
|
||||
[&mut self.all]
|
||||
.into_iter()
|
||||
.chain(self.by_term.as_mut_vec())
|
||||
.chain(self.by_up_to_date.as_mut_vec())
|
||||
.chain(self.by_from_date.as_mut_vec())
|
||||
.chain(self.by_from_size.as_mut_vec())
|
||||
.chain(self.by_date_range.as_mut_vec())
|
||||
.chain(self.by_epoch.as_mut_vec())
|
||||
.chain(self.by_size_range.as_mut_vec())
|
||||
.chain(self.by_up_to_size.as_mut_vec())
|
||||
.chain(self.by_type.as_mut_vec())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
pub fn as_mut_separate_vecs(&mut self) -> Vec<&mut T> {
|
||||
self.by_date_range
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.chain(self.by_epoch.as_mut_vec())
|
||||
.chain(self.by_size_range.as_mut_vec())
|
||||
.chain(self.by_type.as_mut_vec())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
pub fn as_mut_overlapping_vecs(&mut self) -> Vec<&mut T> {
|
||||
[&mut self.all]
|
||||
.into_iter()
|
||||
.chain(self.by_term.as_mut_vec())
|
||||
.chain(self.by_up_to_date.as_mut_vec())
|
||||
.chain(self.by_from_date.as_mut_vec())
|
||||
.chain(self.by_up_to_size.as_mut_vec())
|
||||
.chain(self.by_from_size.as_mut_vec())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> UTXOGroups<(GroupFilter, T)> {
|
||||
pub fn vecs(&self) -> Vec<&T> {
|
||||
[&self.all.1]
|
||||
.into_iter()
|
||||
.chain(self.by_term.vecs())
|
||||
.chain(self.by_up_to_date.vecs())
|
||||
.chain(self.by_from_date.vecs())
|
||||
.chain(self.by_date_range.vecs())
|
||||
.chain(self.by_epoch.vecs())
|
||||
.chain(self.by_size_range.vecs())
|
||||
.chain(self.by_type.vecs())
|
||||
.chain(self.by_up_to_size.vecs())
|
||||
.chain(self.by_from_size.vecs())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<UTXOGroups<T>> for UTXOGroups<(GroupFilter, T)> {
|
||||
fn from(value: UTXOGroups<T>) -> Self {
|
||||
Self {
|
||||
all: (GroupFilter::All, value.all),
|
||||
by_term: GroupedByTerm::from(value.by_term),
|
||||
by_up_to_date: GroupedByUpToDate::from(value.by_up_to_date),
|
||||
by_from_date: GroupedByFromDate::from(value.by_from_date),
|
||||
by_date_range: GroupedByDateRange::from(value.by_date_range),
|
||||
by_epoch: GroupedByEpoch::from(value.by_epoch),
|
||||
by_size_range: GroupedBySizeRange::from(value.by_size_range),
|
||||
by_up_to_size: GroupedByUpToSize::from(value.by_up_to_size),
|
||||
by_from_size: GroupedByFromSize::from(value.by_from_size),
|
||||
by_type: GroupedBySpendableType::from(value.by_type),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,16 @@ use brk_exit::Exit;
|
||||
use brk_fetcher::Fetcher;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{Computation, Format};
|
||||
use log::info;
|
||||
|
||||
mod groups;
|
||||
mod states;
|
||||
mod stores;
|
||||
mod utils;
|
||||
mod vecs;
|
||||
|
||||
use log::info;
|
||||
use groups::*;
|
||||
use states::*;
|
||||
use stores::Stores;
|
||||
use vecs::Vecs;
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
use std::ops::{Add, AddAssign, SubAssign};
|
||||
|
||||
use brk_core::{Dollars, Timestamp};
|
||||
|
||||
use super::SupplyState;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BlockState {
|
||||
pub supply: SupplyState,
|
||||
pub price: Option<Dollars>,
|
||||
pub timestamp: Timestamp,
|
||||
}
|
||||
|
||||
impl Add<BlockState> for BlockState {
|
||||
type Output = Self;
|
||||
fn add(mut self, rhs: BlockState) -> Self::Output {
|
||||
self.supply += &rhs.supply;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<&BlockState> for BlockState {
|
||||
fn add_assign(&mut self, rhs: &Self) {
|
||||
self.supply += &rhs.supply;
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<&BlockState> for BlockState {
|
||||
fn sub_assign(&mut self, rhs: &Self) {
|
||||
self.supply -= &rhs.supply;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_core::{AddressData, Dollars, Height, Result, Sats};
|
||||
|
||||
use crate::SupplyState;
|
||||
|
||||
use super::CohortState;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AddressCohortState {
|
||||
pub address_count: usize,
|
||||
pub inner: CohortState,
|
||||
}
|
||||
|
||||
impl AddressCohortState {
|
||||
pub fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result<Self> {
|
||||
Ok(Self {
|
||||
address_count: 0,
|
||||
inner: CohortState::default_and_import(path, name, compute_dollars)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reset_single_iteration_values(&mut self) {
|
||||
self.inner.reset_single_iteration_values();
|
||||
}
|
||||
|
||||
pub fn send(
|
||||
&mut self,
|
||||
value: Sats,
|
||||
current_price: Option<Dollars>,
|
||||
prev_price: Option<Dollars>,
|
||||
blocks_old: usize,
|
||||
days_old: f64,
|
||||
older_than_hour: bool,
|
||||
) {
|
||||
self.inner.send(
|
||||
&SupplyState { utxos: 1, value },
|
||||
current_price,
|
||||
prev_price,
|
||||
blocks_old,
|
||||
days_old,
|
||||
older_than_hour,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn receive(&mut self, value: Sats, price: Option<Dollars>) {
|
||||
self.inner.receive(&SupplyState { utxos: 1, value }, price);
|
||||
}
|
||||
|
||||
pub fn add(&mut self, addressdata: &AddressData) {
|
||||
self.address_count += 1;
|
||||
self.inner
|
||||
.increment_(&addressdata.into(), addressdata.realized_cap);
|
||||
}
|
||||
|
||||
pub fn subtract(&mut self, addressdata: &AddressData) {
|
||||
self.address_count.checked_sub(1).unwrap();
|
||||
self.inner
|
||||
.decrement_(&addressdata.into(), addressdata.realized_cap);
|
||||
}
|
||||
|
||||
pub fn commit(&mut self, height: Height) -> Result<()> {
|
||||
self.inner.commit(height)
|
||||
}
|
||||
}
|
||||
|
||||
// fn decrement(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
|
||||
// self.inner.decrement(supply_state, price);
|
||||
// }
|
||||
|
||||
// fn decrement_price_to_amount(&mut self, supply_state: &SupplyState, price: Dollars) {
|
||||
// self.inner.decrement_price_to_amount(supply_state, price);
|
||||
// }
|
||||
|
||||
// fn receive(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
|
||||
// self.inner.receive(supply_state, price);
|
||||
// }
|
||||
|
||||
// fn compute_unrealized_states(
|
||||
// &self,
|
||||
// height_price: Dollars,
|
||||
// date_price: Option<Dollars>,
|
||||
// ) -> (UnrealizedState, Option<UnrealizedState>) {
|
||||
// self.inner
|
||||
// .compute_unrealized_states(height_price, date_price)
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// impl Deref for AddressCohortState {
|
||||
// type Target = CohortState;
|
||||
// fn deref(&self) -> &Self::Target {
|
||||
// &self.inner
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl DerefMut for AddressCohortState {
|
||||
// fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
// &mut self.inner
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,202 @@
|
||||
use std::{cmp::Ordering, path::Path};
|
||||
|
||||
use brk_core::{Bitcoin, CheckedSub, Dollars, Height, Result, Sats};
|
||||
|
||||
use crate::{PriceToAmount, RealizedState, SupplyState, UnrealizedState};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CohortState {
|
||||
pub supply: SupplyState,
|
||||
pub realized: Option<RealizedState>,
|
||||
pub satblocks_destroyed: Sats,
|
||||
pub satdays_destroyed: Sats,
|
||||
pub price_to_amount: PriceToAmount,
|
||||
}
|
||||
|
||||
impl CohortState {
|
||||
pub fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result<Self> {
|
||||
Ok(Self {
|
||||
supply: SupplyState::default(),
|
||||
realized: compute_dollars.then_some(RealizedState::NAN),
|
||||
satblocks_destroyed: Sats::ZERO,
|
||||
satdays_destroyed: Sats::ZERO,
|
||||
price_to_amount: PriceToAmount::forced_import(path, name),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reset_single_iteration_values(&mut self) {
|
||||
self.satdays_destroyed = Sats::ZERO;
|
||||
self.satblocks_destroyed = Sats::ZERO;
|
||||
if let Some(realized) = self.realized.as_mut() {
|
||||
realized.reset_single_iteration_values();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn increment(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
|
||||
self.supply += supply_state;
|
||||
|
||||
if supply_state.value > Sats::ZERO {
|
||||
if let Some(realized) = self.realized.as_mut() {
|
||||
let price = price.unwrap();
|
||||
realized.increment(supply_state, price);
|
||||
*self.price_to_amount.entry(price).or_default() += supply_state.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn increment_(&mut self, supply_state: &SupplyState, realized_cap: Dollars) {
|
||||
self.supply += supply_state;
|
||||
|
||||
if supply_state.value > Sats::ZERO {
|
||||
if let Some(realized) = self.realized.as_mut() {
|
||||
realized.increment_(realized_cap);
|
||||
*self
|
||||
.price_to_amount
|
||||
.entry(realized_cap / Bitcoin::from(supply_state.value))
|
||||
.or_default() += supply_state.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrement(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
|
||||
self.supply -= supply_state;
|
||||
|
||||
if supply_state.value > Sats::ZERO {
|
||||
if let Some(realized) = self.realized.as_mut() {
|
||||
let price = price.unwrap();
|
||||
realized.decrement(supply_state, price);
|
||||
self.decrement_price_to_amount(supply_state, price);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrement_(&mut self, supply_state: &SupplyState, realized_cap: Dollars) {
|
||||
self.supply -= supply_state;
|
||||
|
||||
if supply_state.value > Sats::ZERO {
|
||||
if let Some(realized) = self.realized.as_mut() {
|
||||
realized.decrement_(realized_cap);
|
||||
self.decrement_price_to_amount(
|
||||
supply_state,
|
||||
realized_cap / Bitcoin::from(supply_state.value),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn decrement_price_to_amount(&mut self, supply_state: &SupplyState, price: Dollars) {
|
||||
let amount = self.price_to_amount.get_mut(&price).unwrap();
|
||||
*amount -= supply_state.value;
|
||||
if *amount == Sats::ZERO {
|
||||
self.price_to_amount.remove(&price);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
|
||||
self.supply += supply_state;
|
||||
|
||||
if supply_state.value > Sats::ZERO {
|
||||
if let Some(realized) = self.realized.as_mut() {
|
||||
let price = price.unwrap();
|
||||
realized.receive(supply_state, price);
|
||||
*self.price_to_amount.entry(price).or_default() += supply_state.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(
|
||||
&mut self,
|
||||
supply_state: &SupplyState,
|
||||
current_price: Option<Dollars>,
|
||||
prev_price: Option<Dollars>,
|
||||
blocks_old: usize,
|
||||
days_old: f64,
|
||||
older_than_hour: bool,
|
||||
) {
|
||||
if supply_state.utxos == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
self.supply -= supply_state;
|
||||
|
||||
if supply_state.value > Sats::ZERO {
|
||||
self.satblocks_destroyed += supply_state.value * blocks_old;
|
||||
|
||||
self.satdays_destroyed +=
|
||||
Sats::from((u64::from(supply_state.value) as f64 * days_old).floor() as u64);
|
||||
|
||||
if let Some(realized) = self.realized.as_mut() {
|
||||
let current_price = current_price.unwrap();
|
||||
let prev_price = prev_price.unwrap();
|
||||
realized.send(supply_state, current_price, prev_price, older_than_hour);
|
||||
self.decrement_price_to_amount(supply_state, prev_price);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compute_unrealized_states(
|
||||
&self,
|
||||
height_price: Dollars,
|
||||
date_price: Option<Dollars>,
|
||||
) -> (UnrealizedState, Option<UnrealizedState>) {
|
||||
if self.price_to_amount.is_empty() {
|
||||
return (
|
||||
UnrealizedState::NAN,
|
||||
date_price.map(|_| UnrealizedState::NAN),
|
||||
);
|
||||
}
|
||||
|
||||
let mut height_unrealized_state = UnrealizedState::ZERO;
|
||||
let mut date_unrealized_state = date_price.map(|_| UnrealizedState::ZERO);
|
||||
|
||||
let update_state =
|
||||
|price: Dollars, current_price: Dollars, sats: Sats, state: &mut UnrealizedState| {
|
||||
match price.cmp(¤t_price) {
|
||||
Ordering::Equal => {
|
||||
state.supply_even += sats;
|
||||
}
|
||||
Ordering::Less => {
|
||||
state.supply_in_profit += sats;
|
||||
if price > Dollars::ZERO && current_price > Dollars::ZERO {
|
||||
let diff = current_price.checked_sub(price).unwrap();
|
||||
if diff <= Dollars::ZERO {
|
||||
dbg!(price, current_price, diff, sats);
|
||||
panic!();
|
||||
}
|
||||
state.unrealized_profit += diff * sats;
|
||||
}
|
||||
}
|
||||
Ordering::Greater => {
|
||||
state.supply_in_loss += sats;
|
||||
if price > Dollars::ZERO && current_price > Dollars::ZERO {
|
||||
let diff = price.checked_sub(current_price).unwrap();
|
||||
if diff <= Dollars::ZERO {
|
||||
dbg!(price, current_price, diff, sats);
|
||||
panic!();
|
||||
}
|
||||
state.unrealized_loss += diff * sats;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.price_to_amount.iter().for_each(|(&price, &sats)| {
|
||||
update_state(price, height_price, sats, &mut height_unrealized_state);
|
||||
|
||||
if let Some(date_price) = date_price {
|
||||
update_state(
|
||||
price,
|
||||
date_price,
|
||||
sats,
|
||||
date_unrealized_state.as_mut().unwrap(),
|
||||
)
|
||||
}
|
||||
});
|
||||
|
||||
(height_unrealized_state, date_unrealized_state)
|
||||
}
|
||||
|
||||
pub fn commit(&mut self, height: Height) -> Result<()> {
|
||||
self.price_to_amount.flush(height)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
mod address;
|
||||
mod common;
|
||||
mod utxo;
|
||||
|
||||
pub use address::*;
|
||||
pub use common::*;
|
||||
pub use utxo::*;
|
||||
@@ -0,0 +1,19 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_core::Result;
|
||||
use derive_deref::{Deref, DerefMut};
|
||||
|
||||
use super::CohortState;
|
||||
|
||||
#[derive(Clone, Deref, DerefMut)]
|
||||
pub struct UTXOCohortState(CohortState);
|
||||
|
||||
impl UTXOCohortState {
|
||||
pub fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result<Self> {
|
||||
Ok(Self(CohortState::default_and_import(
|
||||
path,
|
||||
name,
|
||||
compute_dollars,
|
||||
)?))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
mod block;
|
||||
mod cohorts;
|
||||
mod price_to_amount;
|
||||
mod realized;
|
||||
mod supply;
|
||||
mod transacted;
|
||||
mod unrealized;
|
||||
|
||||
pub use block::*;
|
||||
pub use cohorts::*;
|
||||
pub use price_to_amount::*;
|
||||
pub use realized::*;
|
||||
pub use supply::*;
|
||||
pub use transacted::*;
|
||||
pub use unrealized::*;
|
||||
@@ -0,0 +1,104 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fs::{self, File},
|
||||
io::{BufReader, BufWriter},
|
||||
ops::{Deref, DerefMut},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use bincode::{Decode, Encode, config, decode_from_std_read, encode_into_std_write};
|
||||
use brk_core::{Dollars, Height, Result, Sats};
|
||||
use derive_deref::{Deref, DerefMut};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PriceToAmount {
|
||||
pathbuf: PathBuf,
|
||||
height: Option<Height>,
|
||||
state: State,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug, Deref, DerefMut, Serialize, Deserialize, Encode, Decode)]
|
||||
struct State(BTreeMap<Dollars, Sats>);
|
||||
|
||||
impl PriceToAmount {
|
||||
pub fn forced_import(path: &Path, name: &str) -> Self {
|
||||
Self::import(path, name).unwrap_or_else(|_| Self {
|
||||
pathbuf: Self::path_(path, name),
|
||||
height: None,
|
||||
state: State::default(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn import(path: &Path, name: &str) -> Result<Self> {
|
||||
let path = Self::path_(path, name);
|
||||
fs::create_dir_all(&path)?;
|
||||
|
||||
let config = config::standard();
|
||||
let file = File::open(Self::path_state_(&path))?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let state = decode_from_std_read(&mut reader, config)?;
|
||||
|
||||
Ok(Self {
|
||||
height: Height::try_from(Self::path_height_(&path).as_path()).ok(),
|
||||
pathbuf: path,
|
||||
state,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) -> Result<()> {
|
||||
self.clear();
|
||||
self.height = None;
|
||||
fs::remove_dir_all(&self.pathbuf)?;
|
||||
fs::create_dir_all(&self.pathbuf)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn flush(&mut self, height: Height) -> Result<()> {
|
||||
self.height = Some(height);
|
||||
height.write(&self.path_height())?;
|
||||
|
||||
let config = config::standard();
|
||||
let file = File::create(self.path_state()).inspect_err(|_| {
|
||||
dbg!(self.path_state());
|
||||
})?;
|
||||
let mut writer = BufWriter::new(file);
|
||||
encode_into_std_write(&self.state, &mut writer, config)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn height(&self) -> Option<Height> {
|
||||
self.height
|
||||
}
|
||||
|
||||
fn path_(path: &Path, name: &str) -> PathBuf {
|
||||
path.join(format!("{name}_price_to_amount"))
|
||||
}
|
||||
|
||||
fn path_state(&self) -> PathBuf {
|
||||
Self::path_state_(&self.pathbuf)
|
||||
}
|
||||
fn path_state_(path: &Path) -> PathBuf {
|
||||
path.join("state")
|
||||
}
|
||||
|
||||
fn path_height(&self) -> PathBuf {
|
||||
Self::path_height_(&self.pathbuf)
|
||||
}
|
||||
fn path_height_(path: &Path) -> PathBuf {
|
||||
path.join("height")
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for PriceToAmount {
|
||||
type Target = BTreeMap<Dollars, Sats>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.state
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for PriceToAmount {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.state
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use brk_core::{CheckedSub, Dollars};
|
||||
|
||||
use super::SupplyState;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct RealizedState {
|
||||
pub cap: Dollars,
|
||||
pub profit: Dollars,
|
||||
pub loss: Dollars,
|
||||
pub value_created: Dollars,
|
||||
pub adj_value_created: Dollars,
|
||||
pub value_destroyed: Dollars,
|
||||
pub adj_value_destroyed: Dollars,
|
||||
}
|
||||
|
||||
impl RealizedState {
|
||||
pub const NAN: Self = Self {
|
||||
cap: Dollars::NAN,
|
||||
profit: Dollars::NAN,
|
||||
loss: Dollars::NAN,
|
||||
value_created: Dollars::NAN,
|
||||
adj_value_created: Dollars::NAN,
|
||||
value_destroyed: Dollars::NAN,
|
||||
adj_value_destroyed: Dollars::NAN,
|
||||
};
|
||||
|
||||
pub fn reset_single_iteration_values(&mut self) {
|
||||
if self.cap != Dollars::NAN {
|
||||
self.profit = Dollars::ZERO;
|
||||
self.loss = Dollars::ZERO;
|
||||
self.value_created = Dollars::ZERO;
|
||||
self.adj_value_created = Dollars::ZERO;
|
||||
self.value_destroyed = Dollars::ZERO;
|
||||
self.adj_value_destroyed = Dollars::ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn increment(&mut self, supply_state: &SupplyState, price: Dollars) {
|
||||
if supply_state.value.is_zero() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.increment_(price * supply_state.value)
|
||||
}
|
||||
|
||||
pub fn increment_(&mut self, realized_cap: Dollars) {
|
||||
if self.cap == Dollars::NAN {
|
||||
self.cap = Dollars::ZERO;
|
||||
self.profit = Dollars::ZERO;
|
||||
self.loss = Dollars::ZERO;
|
||||
self.value_created = Dollars::ZERO;
|
||||
self.adj_value_created = Dollars::ZERO;
|
||||
self.value_destroyed = Dollars::ZERO;
|
||||
self.adj_value_destroyed = Dollars::ZERO;
|
||||
}
|
||||
|
||||
self.cap += realized_cap;
|
||||
}
|
||||
|
||||
pub fn decrement(&mut self, supply_state: &SupplyState, price: Dollars) {
|
||||
self.decrement_(price * supply_state.value);
|
||||
}
|
||||
|
||||
pub fn decrement_(&mut self, realized_cap: Dollars) {
|
||||
self.cap = self.cap.checked_sub(realized_cap).unwrap();
|
||||
}
|
||||
|
||||
pub fn receive(&mut self, supply_state: &SupplyState, current_price: Dollars) {
|
||||
self.increment(supply_state, current_price);
|
||||
}
|
||||
|
||||
pub fn send(
|
||||
&mut self,
|
||||
supply_state: &SupplyState,
|
||||
current_price: Dollars,
|
||||
prev_price: Dollars,
|
||||
older_than_hour: bool,
|
||||
) {
|
||||
let current_value = current_price * supply_state.value;
|
||||
let prev_value = prev_price * supply_state.value;
|
||||
|
||||
self.value_created += current_value;
|
||||
self.value_destroyed += prev_value;
|
||||
|
||||
if older_than_hour {
|
||||
self.adj_value_created += current_value;
|
||||
self.adj_value_destroyed += prev_value;
|
||||
}
|
||||
|
||||
match current_price.cmp(&prev_price) {
|
||||
Ordering::Greater => {
|
||||
self.profit += current_value.checked_sub(prev_value).unwrap();
|
||||
}
|
||||
Ordering::Less => {
|
||||
self.loss += prev_value.checked_sub(current_value).unwrap();
|
||||
}
|
||||
Ordering::Equal => {}
|
||||
}
|
||||
|
||||
self.decrement(supply_state, prev_price);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
use std::ops::{Add, AddAssign, SubAssign};
|
||||
|
||||
use brk_core::{AddressData, CheckedSub, Sats};
|
||||
use serde::Serialize;
|
||||
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)]
|
||||
pub struct SupplyState {
|
||||
pub utxos: usize,
|
||||
pub value: Sats,
|
||||
}
|
||||
|
||||
impl Add<SupplyState> for SupplyState {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: SupplyState) -> Self::Output {
|
||||
Self {
|
||||
utxos: self.utxos + rhs.utxos,
|
||||
value: self.value + rhs.value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<SupplyState> for SupplyState {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
*self += &rhs;
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<&SupplyState> for SupplyState {
|
||||
fn add_assign(&mut self, rhs: &Self) {
|
||||
self.utxos += rhs.utxos;
|
||||
self.value += rhs.value;
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<&SupplyState> for SupplyState {
|
||||
fn sub_assign(&mut self, rhs: &Self) {
|
||||
self.utxos = self.utxos.checked_sub(rhs.utxos).unwrap();
|
||||
self.value = self.value.checked_sub(rhs.value).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&AddressData> for SupplyState {
|
||||
fn from(value: &AddressData) -> Self {
|
||||
Self {
|
||||
utxos: value.outputs_len as usize,
|
||||
value: value.amount(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
use brk_core::{OutputType, Sats};
|
||||
|
||||
use crate::{GroupedBySizeRange, GroupedByType};
|
||||
|
||||
use super::SupplyState;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Transacted {
|
||||
pub spendable_supply: SupplyState,
|
||||
pub by_type: GroupedByType<SupplyState>,
|
||||
pub by_size_group: GroupedBySizeRange<SupplyState>,
|
||||
}
|
||||
|
||||
impl Transacted {
|
||||
#[allow(clippy::inconsistent_digit_grouping)]
|
||||
pub fn iterate(&mut self, value: Sats, _type: OutputType) {
|
||||
let supply = SupplyState { utxos: 1, value };
|
||||
|
||||
*self.by_type.get_mut(_type) += &supply;
|
||||
|
||||
if _type.is_unspendable() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.spendable_supply += &supply;
|
||||
|
||||
*self.by_size_group.get_mut(value) += &supply;
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Transacted {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
spendable_supply: self.spendable_supply + rhs.spendable_supply,
|
||||
by_type: self.by_type + rhs.by_type,
|
||||
by_size_group: self.by_size_group + rhs.by_size_group,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for Transacted {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.by_size_group += rhs.by_size_group;
|
||||
self.spendable_supply += &rhs.spendable_supply;
|
||||
self.by_type += rhs.by_type;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use brk_core::{Dollars, Sats};
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct UnrealizedState {
|
||||
pub supply_in_profit: Sats,
|
||||
pub supply_even: Sats,
|
||||
pub supply_in_loss: Sats,
|
||||
pub unrealized_profit: Dollars,
|
||||
pub unrealized_loss: Dollars,
|
||||
}
|
||||
|
||||
impl UnrealizedState {
|
||||
pub const NAN: Self = Self {
|
||||
supply_in_profit: Sats::ZERO,
|
||||
supply_even: Sats::ZERO,
|
||||
supply_in_loss: Sats::ZERO,
|
||||
unrealized_profit: Dollars::NAN,
|
||||
unrealized_loss: Dollars::NAN,
|
||||
};
|
||||
|
||||
pub const ZERO: Self = Self {
|
||||
supply_in_profit: Sats::ZERO,
|
||||
supply_even: Sats::ZERO,
|
||||
supply_in_loss: Sats::ZERO,
|
||||
unrealized_profit: Dollars::ZERO,
|
||||
unrealized_loss: Dollars::ZERO,
|
||||
};
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::{path::Path, thread};
|
||||
|
||||
use brk_core::{
|
||||
AddressData, EmptyAddressData, GroupedByAddressType, Height, OutputIndex, OutputType,
|
||||
P2AAddressIndex, P2AAddressIndexOutputindex, P2PK33AddressIndex, P2PK33AddressIndexOutputindex,
|
||||
AddressData, EmptyAddressData, Height, OutputIndex, OutputType, P2AAddressIndex,
|
||||
P2AAddressIndexOutputindex, P2PK33AddressIndex, P2PK33AddressIndexOutputindex,
|
||||
P2PK65AddressIndex, P2PK65AddressIndexOutputindex, P2PKHAddressIndex,
|
||||
P2PKHAddressIndexOutputindex, P2SHAddressIndex, P2SHAddressIndexOutputindex, P2TRAddressIndex,
|
||||
P2TRAddressIndexOutputindex, P2WPKHAddressIndex, P2WPKHAddressIndexOutputindex,
|
||||
@@ -12,8 +12,11 @@ use brk_store::{AnyStore, Store};
|
||||
use fjall::{PersistMode, TransactionalKeyspace};
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::vecs::stateful::{
|
||||
AddressTypeToTypeIndexTree, AddressTypeToTypeIndexVec, WithAddressDataSource,
|
||||
use crate::{
|
||||
GroupedByAddressType,
|
||||
vecs::stateful::{
|
||||
AddressTypeToTypeIndexTree, AddressTypeToTypeIndexVec, WithAddressDataSource,
|
||||
},
|
||||
};
|
||||
|
||||
const VERSION: Version = Version::ZERO;
|
||||
|
||||
@@ -264,7 +264,7 @@ impl Vecs {
|
||||
stateful: &stateful::Vecs,
|
||||
exit: &Exit,
|
||||
) -> color_eyre::Result<()> {
|
||||
let circulating_supply = &stateful.utxos_vecs.all.1.height_to_supply;
|
||||
let circulating_supply = &stateful.utxo_vecs.all.1.height_to_supply;
|
||||
|
||||
self.indexes_to_coinblocks_created.compute_all(
|
||||
indexer,
|
||||
@@ -282,7 +282,7 @@ impl Vecs {
|
||||
)?;
|
||||
|
||||
let indexes_to_coinblocks_destroyed =
|
||||
&stateful.utxos_vecs.all.1.indexes_to_coinblocks_destroyed;
|
||||
&stateful.utxo_vecs.all.1.indexes_to_coinblocks_destroyed;
|
||||
|
||||
self.indexes_to_coinblocks_stored.compute_all(
|
||||
indexer,
|
||||
@@ -392,7 +392,7 @@ impl Vecs {
|
||||
|
||||
if let Some(fetched) = fetched {
|
||||
let realized_cap = stateful
|
||||
.utxos_vecs
|
||||
.utxo_vecs
|
||||
.all
|
||||
.1
|
||||
.height_to_realized_cap
|
||||
@@ -400,7 +400,7 @@ impl Vecs {
|
||||
.unwrap();
|
||||
|
||||
let realized_price = stateful
|
||||
.utxos_vecs
|
||||
.utxo_vecs
|
||||
.all
|
||||
.1
|
||||
.indexes_to_realized_price
|
||||
|
||||
@@ -5,7 +5,7 @@ use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{AnyCollectableVec, EagerVec, Format};
|
||||
|
||||
use crate::vecs::{Indexes, indexes};
|
||||
use crate::vecs::{indexes, Indexes};
|
||||
|
||||
use super::{ComputedType, ComputedVecBuilder, StorableVecGeneatorOptions};
|
||||
|
||||
|
||||
@@ -3,14 +3,16 @@ use std::{ops::Deref, path::Path};
|
||||
use brk_core::{Bitcoin, DateIndex, Dollars, Height, Result, StoredUsize, Version};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_state::AddressCohortState;
|
||||
use brk_vec::{
|
||||
AnyCollectableVec, AnyIterableVec, AnyVec, Computation, EagerVec, Format, VecIterator,
|
||||
};
|
||||
|
||||
use crate::vecs::{
|
||||
Indexes, fetched, indexes, market,
|
||||
stateful::{common, r#trait::CohortVecs},
|
||||
use crate::{
|
||||
states::AddressCohortState,
|
||||
vecs::{
|
||||
Indexes, fetched, indexes, market,
|
||||
stateful::{common, r#trait::CohortVecs},
|
||||
},
|
||||
};
|
||||
|
||||
const VERSION: Version = Version::ZERO;
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_core::{
|
||||
AddressGroups, GroupFilter, GroupedByFromSize, GroupedBySizeRange, GroupedByUpToSize, Result,
|
||||
Version,
|
||||
};
|
||||
use brk_core::{Height, Result, Version};
|
||||
use brk_exit::Exit;
|
||||
use brk_vec::{Computation, Format};
|
||||
use derive_deref::{Deref, DerefMut};
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::vecs::{
|
||||
Indexes, fetched,
|
||||
stateful::{address_cohort, r#trait::CohortVecs},
|
||||
use crate::{
|
||||
AddressGroups, GroupFilter, GroupedByFromSize, GroupedBySizeRange, GroupedByUpToSize,
|
||||
vecs::{
|
||||
Indexes, fetched,
|
||||
stateful::{address_cohort, r#trait::CohortVecs},
|
||||
},
|
||||
};
|
||||
|
||||
const VERSION: Version = Version::new(0);
|
||||
@@ -326,4 +326,10 @@ impl Vecs {
|
||||
vecs.compute_from_stateful(starting_indexes, &stateful, exit)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn safe_flush_stateful_vecs(&mut self, height: Height, exit: &Exit) -> Result<()> {
|
||||
self.as_mut_separate_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| v.safe_flush_stateful_vecs(height, exit))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,18 +5,21 @@ use brk_core::{
|
||||
};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_state::CohortState;
|
||||
use brk_vec::{
|
||||
AnyCollectableVec, AnyIterableVec, AnyVec, Computation, EagerVec, Format, VecIterator,
|
||||
};
|
||||
|
||||
use crate::vecs::{
|
||||
Indexes, fetched,
|
||||
grouped::{
|
||||
ComputedHeightValueVecs, ComputedRatioVecsFromDateIndex, ComputedValueVecsFromDateIndex,
|
||||
ComputedVecsFromDateIndex, ComputedVecsFromHeight, StorableVecGeneatorOptions,
|
||||
use crate::{
|
||||
states::CohortState,
|
||||
vecs::{
|
||||
Indexes, fetched,
|
||||
grouped::{
|
||||
ComputedHeightValueVecs, ComputedRatioVecsFromDateIndex,
|
||||
ComputedValueVecsFromDateIndex, ComputedVecsFromDateIndex, ComputedVecsFromHeight,
|
||||
StorableVecGeneatorOptions,
|
||||
},
|
||||
indexes, market,
|
||||
},
|
||||
indexes, market,
|
||||
};
|
||||
|
||||
const VERSION: Version = Version::ZERO;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::{cmp::Ordering, collections::BTreeMap, mem, path::Path, thread};
|
||||
|
||||
use brk_core::{
|
||||
AddressData, DateIndex, Dollars, EmptyAddressData, GroupedByAddressType, Height, InputIndex,
|
||||
OutputIndex, OutputType, Result, Sats, StoredUsize, Version,
|
||||
AddressData, CheckedSub, DateIndex, Dollars, EmptyAddressData, Height, InputIndex, OutputIndex,
|
||||
OutputType, Result, Sats, StoredUsize, Version,
|
||||
};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
@@ -13,9 +13,9 @@ use brk_vec::{
|
||||
use log::info;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use brk_state::{BlockState, SupplyState, Transacted};
|
||||
|
||||
use crate::{stores::Stores, vecs::market};
|
||||
use crate::{
|
||||
BlockState, GroupedByAddressType, SupplyState, Transacted, stores::Stores, vecs::market,
|
||||
};
|
||||
|
||||
use super::{
|
||||
Indexes, fetched,
|
||||
@@ -49,8 +49,11 @@ pub struct Vecs {
|
||||
pub height_to_opreturn_supply: EagerVec<Height, Sats>,
|
||||
pub indexes_to_opreturn_supply: ComputedValueVecsFromHeight,
|
||||
pub height_to_address_count: EagerVec<Height, StoredUsize>,
|
||||
pub height_to_empty_address_count: EagerVec<Height, StoredUsize>,
|
||||
pub addresstype_to_height_to_address_count: GroupedByAddressType<EagerVec<Height, StoredUsize>>,
|
||||
pub utxos_vecs: utxo_cohorts::Vecs,
|
||||
pub addresstype_to_height_to_empty_address_count:
|
||||
GroupedByAddressType<EagerVec<Height, StoredUsize>>,
|
||||
pub utxo_vecs: utxo_cohorts::Vecs,
|
||||
pub address_vecs: address_cohorts::Vecs,
|
||||
}
|
||||
|
||||
@@ -113,6 +116,12 @@ impl Vecs {
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
height_to_empty_address_count: EagerVec::forced_import(
|
||||
path,
|
||||
"empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
addresstype_to_height_to_address_count: GroupedByAddressType {
|
||||
p2pk65: EagerVec::forced_import(
|
||||
path,
|
||||
@@ -163,7 +172,57 @@ impl Vecs {
|
||||
format,
|
||||
)?,
|
||||
},
|
||||
utxos_vecs: utxo_cohorts::Vecs::forced_import(
|
||||
addresstype_to_height_to_empty_address_count: GroupedByAddressType {
|
||||
p2pk65: EagerVec::forced_import(
|
||||
path,
|
||||
"p2pk65_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
p2pk33: EagerVec::forced_import(
|
||||
path,
|
||||
"p2pk33_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
p2pkh: EagerVec::forced_import(
|
||||
path,
|
||||
"p2pkh_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
p2sh: EagerVec::forced_import(
|
||||
path,
|
||||
"p2sh_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
p2wpkh: EagerVec::forced_import(
|
||||
path,
|
||||
"p2wpkh_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
p2wsh: EagerVec::forced_import(
|
||||
path,
|
||||
"p2wsh_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
p2tr: EagerVec::forced_import(
|
||||
path,
|
||||
"p2tr_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
p2a: EagerVec::forced_import(
|
||||
path,
|
||||
"p2a_empty_address_count",
|
||||
version + VERSION + Version::ZERO,
|
||||
format,
|
||||
)?,
|
||||
},
|
||||
utxo_vecs: utxo_cohorts::Vecs::forced_import(
|
||||
path,
|
||||
version,
|
||||
_computation,
|
||||
@@ -229,6 +288,7 @@ impl Vecs {
|
||||
let outputindex_to_txindex_mmap = outputindex_to_txindex.mmap().load();
|
||||
let txindex_to_height_mmap = txindex_to_height.mmap().load();
|
||||
let height_to_close_mmap = height_to_close.map(|v| v.mmap().load());
|
||||
let height_to_timestamp_fixed_mmap = height_to_timestamp_fixed.mmap().load();
|
||||
|
||||
let mut height_to_first_outputindex_iter = height_to_first_outputindex.into_iter();
|
||||
let mut height_to_first_inputindex_iter = height_to_first_inputindex.into_iter();
|
||||
@@ -242,7 +302,7 @@ impl Vecs {
|
||||
let mut dateindex_to_first_height_iter = dateindex_to_first_height.into_iter();
|
||||
let mut dateindex_to_height_count_iter = dateindex_to_height_count.into_iter();
|
||||
|
||||
let mut separate_utxo_vecs = self.utxos_vecs.as_mut_separate_vecs();
|
||||
let mut separate_utxo_vecs = self.utxo_vecs.as_mut_separate_vecs();
|
||||
|
||||
let base_version = Version::ZERO
|
||||
+ height_to_first_outputindex.version()
|
||||
@@ -373,7 +433,7 @@ impl Vecs {
|
||||
.try_for_each(|_height| -> color_eyre::Result<()> {
|
||||
height = _height;
|
||||
|
||||
self.utxos_vecs
|
||||
self.utxo_vecs
|
||||
.as_mut_separate_vecs()
|
||||
.iter_mut()
|
||||
.for_each(|(_, v)| v.state.reset_single_iteration_values());
|
||||
@@ -398,10 +458,10 @@ impl Vecs {
|
||||
let output_count = height_to_output_count_iter.unwrap_get_inner(height);
|
||||
let input_count = height_to_input_count_iter.unwrap_get_inner(height);
|
||||
|
||||
let ((mut height_to_sent, new_addresstype_to_typedindex_to_sent_outputindex, addresstype_to_typedindex_to_sent_sats_and_addressdata_opt), (mut received, new_addresstype_to_typedindex_to_received_outputindex, addresstype_to_typedindex_to_received_sats_and_addressdata_opt)) = thread::scope(|s| {
|
||||
let ((mut height_to_sent, new_addresstype_to_typedindex_to_sent_outputindex, addresstype_to_typedindex_to_sent_data), (mut received, new_addresstype_to_typedindex_to_received_outputindex, addresstype_to_typedindex_to_received_data)) = thread::scope(|s| {
|
||||
if chain_state_starting_height <= height {
|
||||
s.spawn(|| {
|
||||
self.utxos_vecs
|
||||
self.utxo_vecs
|
||||
.tick_tock_next_block(&chain_state, timestamp);
|
||||
});
|
||||
}
|
||||
@@ -452,33 +512,48 @@ impl Vecs {
|
||||
.unwrap()
|
||||
.into_owned();
|
||||
|
||||
let height = txindex_to_height
|
||||
let prev_height = txindex_to_height
|
||||
.get_or_read(input_txindex, &txindex_to_height_mmap)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_owned();
|
||||
|
||||
let dollars_opt = height_to_close.map(|m| *m.get_or_read(height, height_to_close_mmap.as_ref().unwrap()).unwrap()
|
||||
let prev_price = height_to_close.map(|m| *m.get_or_read(prev_height, height_to_close_mmap.as_ref().unwrap()).unwrap()
|
||||
.unwrap()
|
||||
.into_owned());
|
||||
|
||||
(height, value, input_type, typeindex, outputindex, addressdata_opt, dollars_opt)
|
||||
let prev_timestamp = height_to_timestamp_fixed.get_or_read(prev_height, &height_to_timestamp_fixed_mmap)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_owned();
|
||||
|
||||
let blocks_old = height.unwrap_to_usize() - prev_height.unwrap_to_usize();
|
||||
|
||||
let days_old = prev_timestamp
|
||||
.difference_in_days_between_float(timestamp);
|
||||
|
||||
let older_than_hour = timestamp
|
||||
.checked_sub(prev_timestamp)
|
||||
.unwrap()
|
||||
.is_more_than_hour();
|
||||
|
||||
(prev_height, value, input_type, typeindex, outputindex, addressdata_opt, prev_price, blocks_old, days_old, older_than_hour)
|
||||
})
|
||||
.fold(
|
||||
|| {
|
||||
(
|
||||
BTreeMap::<Height, Transacted>::default(),
|
||||
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>, Option<Dollars>)>::default(),
|
||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>, Option<Dollars>, usize, f64, bool)>::default(),
|
||||
)
|
||||
},
|
||||
|(mut tree, mut vecs, mut vecs2), (height, value, input_type, typeindex, outputindex, addressdata_opt, dollars_opt)| {
|
||||
|(mut tree, mut vecs, mut vecs2), (height, value, input_type, typeindex, outputindex, addressdata_opt, prev_price, blocks_old, days_old, older_than_hour)| {
|
||||
tree.entry(height).or_default().iterate(value, input_type);
|
||||
if let Some(vec) = vecs.get_mut(input_type) {
|
||||
vec.push((typeindex, outputindex));
|
||||
}
|
||||
if let Some(vec) = vecs2.get_mut(input_type) {
|
||||
vec.push((typeindex, (value, addressdata_opt, dollars_opt)));
|
||||
vec.push((typeindex, (value, addressdata_opt, prev_price, blocks_old, days_old, older_than_hour)));
|
||||
}
|
||||
(tree, vecs, vecs2)
|
||||
},
|
||||
@@ -487,7 +562,7 @@ impl Vecs {
|
||||
(
|
||||
BTreeMap::<Height, Transacted>::default(),
|
||||
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>, Option<Dollars>)>::default(),
|
||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>, Option<Dollars>, usize, f64, bool)>::default(),
|
||||
)
|
||||
}, |(first_tree, mut source_vecs,mut source_vecs2), (second_tree, other_vecs, other_vecs2)| {
|
||||
let (mut tree_source, tree_to_consume) = if first_tree.len() > second_tree.len() {
|
||||
@@ -571,14 +646,9 @@ impl Vecs {
|
||||
addresstype_to_typeindex_to_sent_outputindex.merge(new_addresstype_to_typedindex_to_sent_outputindex);
|
||||
addresstype_to_typeindex_to_received_outputindex.merge(new_addresstype_to_typedindex_to_received_outputindex);
|
||||
|
||||
addresstype_to_typedindex_to_received_sats_and_addressdata_opt.process_received(&mut self.address_vecs, &mut addresstype_to_typeindex_to_addressdata, &mut addresstype_to_typeindex_to_emptyaddressdata, price);
|
||||
addresstype_to_typedindex_to_received_data.process_received(&mut self.address_vecs, &mut addresstype_to_typeindex_to_addressdata, &mut addresstype_to_typeindex_to_emptyaddressdata, price);
|
||||
|
||||
addresstype_to_typedindex_to_sent_sats_and_addressdata_opt.process_sent(&mut self.address_vecs, &mut addresstype_to_typeindex_to_addressdata)?;
|
||||
|
||||
// addresstype_to_typedindex_to_sent_sats_and_addressdata_opt;
|
||||
// take from addressdata store
|
||||
// apply
|
||||
// update vecs states if cohort changes
|
||||
addresstype_to_typedindex_to_sent_data.process_sent(&mut self.address_vecs, &mut addresstype_to_typeindex_to_addressdata, &mut addresstype_to_typeindex_to_emptyaddressdata, price)?;
|
||||
|
||||
unspendable_supply += received
|
||||
.by_type
|
||||
@@ -612,7 +682,7 @@ impl Vecs {
|
||||
timestamp,
|
||||
});
|
||||
|
||||
self.utxos_vecs.receive(received, height, price);
|
||||
self.utxo_vecs.receive(received, height, price);
|
||||
|
||||
let unsafe_chain_state = UnsafeSlice::new(&mut chain_state);
|
||||
|
||||
@@ -621,18 +691,24 @@ impl Vecs {
|
||||
&sent.spendable_supply;
|
||||
});
|
||||
|
||||
self.utxos_vecs.send(height_to_sent, chain_state.as_slice());
|
||||
self.utxo_vecs.send(height_to_sent, chain_state.as_slice());
|
||||
} else {
|
||||
dbg!(chain_state_starting_height, height);
|
||||
panic!("temp, just making sure")
|
||||
}
|
||||
|
||||
let mut separate_utxo_vecs = self.utxos_vecs.as_mut_separate_vecs();
|
||||
let mut separate_utxo_vecs = self.utxo_vecs.as_mut_separate_vecs();
|
||||
|
||||
separate_utxo_vecs
|
||||
.iter_mut()
|
||||
.try_for_each(|(_, v)| v.forced_pushed_at(height, exit))?;
|
||||
|
||||
let mut separate_address_vecs = self.address_vecs.as_mut_separate_vecs();
|
||||
|
||||
separate_address_vecs
|
||||
.iter_mut()
|
||||
.try_for_each(|(_, v)| v.forced_pushed_at(height, exit))?;
|
||||
|
||||
self.height_to_unspendable_supply.forced_push_at(
|
||||
height,
|
||||
unspendable_supply,
|
||||
@@ -655,6 +731,8 @@ impl Vecs {
|
||||
.as_mut()
|
||||
.map(|v| is_date_last_height.then(|| *v.unwrap_get_inner(dateindex)));
|
||||
|
||||
// thread::scope(|scope| {
|
||||
// scope.spawn(|| {
|
||||
separate_utxo_vecs.par_iter_mut().try_for_each(|(_, v)| {
|
||||
v.compute_then_force_push_unrealized_states(
|
||||
height,
|
||||
@@ -664,6 +742,20 @@ impl Vecs {
|
||||
exit,
|
||||
)
|
||||
})?;
|
||||
// });
|
||||
// scope.spawn(|| {
|
||||
separate_address_vecs.par_iter_mut().try_for_each(|(_, v)| {
|
||||
v.compute_then_force_push_unrealized_states(
|
||||
height,
|
||||
price,
|
||||
is_date_last_height.then_some(dateindex),
|
||||
date_price,
|
||||
exit,
|
||||
)
|
||||
})?;
|
||||
// });
|
||||
// });
|
||||
|
||||
|
||||
if height != Height::ZERO && height.unwrap_to_usize() % 10_000 == 0 {
|
||||
info!("Flushing...");
|
||||
@@ -703,39 +795,63 @@ impl Vecs {
|
||||
|
||||
info!("Computing overlapping...");
|
||||
|
||||
self.utxos_vecs
|
||||
// thread::scope(|scope| {
|
||||
// scope.spawn(|| {
|
||||
self.utxo_vecs
|
||||
.compute_overlapping_vecs(starting_indexes, exit)?;
|
||||
// });
|
||||
// scope.spawn(|| {
|
||||
self.address_vecs
|
||||
.compute_overlapping_vecs(starting_indexes, exit)?;
|
||||
// });
|
||||
// });
|
||||
|
||||
info!("Computing rest part 1...");
|
||||
|
||||
self.utxos_vecs
|
||||
// thread::scope(|scope| {
|
||||
// scope.spawn(|| {
|
||||
self.utxo_vecs
|
||||
.as_mut_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| {
|
||||
v.compute_rest_part1(indexer, indexes, fetched, starting_indexes, exit)
|
||||
})?;
|
||||
})
|
||||
.unwrap();
|
||||
// });
|
||||
// scope.spawn(|| {
|
||||
self.address_vecs
|
||||
.as_mut_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| {
|
||||
v.compute_rest_part1(indexer, indexes, fetched, starting_indexes, exit)
|
||||
})
|
||||
.unwrap();
|
||||
// });
|
||||
// });
|
||||
|
||||
info!("Computing rest part 2...");
|
||||
|
||||
let height_to_supply = self.utxos_vecs.all.1.height_to_supply_value.bitcoin.clone();
|
||||
let height_to_supply = self.utxo_vecs.all.1.height_to_supply_value.bitcoin.clone();
|
||||
let dateindex_to_supply = self
|
||||
.utxos_vecs
|
||||
.utxo_vecs
|
||||
.all
|
||||
.1
|
||||
.indexes_to_supply
|
||||
.bitcoin
|
||||
.dateindex
|
||||
.clone();
|
||||
let height_to_realized_cap = self.utxos_vecs.all.1.height_to_realized_cap.clone();
|
||||
let height_to_realized_cap = self.utxo_vecs.all.1.height_to_realized_cap.clone();
|
||||
let dateindex_to_realized_cap = self
|
||||
.utxos_vecs
|
||||
.utxo_vecs
|
||||
.all
|
||||
.1
|
||||
.indexes_to_realized_cap
|
||||
.as_ref()
|
||||
.map(|v| v.dateindex.unwrap_last().clone());
|
||||
|
||||
self.utxos_vecs
|
||||
// thread::scope(|scope| {
|
||||
// scope.spawn(|| {
|
||||
self.utxo_vecs
|
||||
.as_mut_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| {
|
||||
@@ -752,6 +868,27 @@ impl Vecs {
|
||||
exit,
|
||||
)
|
||||
})?;
|
||||
// });
|
||||
// scope.spawn(|| {
|
||||
self.address_vecs
|
||||
.as_mut_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| {
|
||||
v.compute_rest_part2(
|
||||
indexer,
|
||||
indexes,
|
||||
fetched,
|
||||
starting_indexes,
|
||||
market,
|
||||
&height_to_supply,
|
||||
dateindex_to_supply.as_ref().unwrap(),
|
||||
height_to_realized_cap.as_ref(),
|
||||
dateindex_to_realized_cap.as_ref(),
|
||||
exit,
|
||||
)
|
||||
})?;
|
||||
// });
|
||||
// });
|
||||
self.indexes_to_unspendable_supply.compute_rest(
|
||||
indexer,
|
||||
indexes,
|
||||
@@ -782,12 +919,24 @@ impl Vecs {
|
||||
chain_state: &[BlockState],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.utxos_vecs
|
||||
self.utxo_vecs
|
||||
.as_mut_separate_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| v.safe_flush_stateful_vecs(height, exit))?;
|
||||
self.address_vecs
|
||||
.as_mut_separate_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| v.safe_flush_stateful_vecs(height, exit))?;
|
||||
self.height_to_unspendable_supply.safe_flush(exit)?;
|
||||
self.height_to_opreturn_supply.safe_flush(exit)?;
|
||||
self.addresstype_to_height_to_address_count
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.try_for_each(|v| v.safe_flush(exit))?;
|
||||
self.addresstype_to_height_to_empty_address_count
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.try_for_each(|v| v.safe_flush(exit))?;
|
||||
|
||||
self.chain_state.truncate_if_needed(Height::ZERO)?;
|
||||
chain_state.iter().for_each(|block_state| {
|
||||
@@ -800,16 +949,33 @@ impl Vecs {
|
||||
|
||||
pub fn vecs(&self) -> Vec<&dyn AnyCollectableVec> {
|
||||
[
|
||||
self.utxos_vecs
|
||||
self.utxo_vecs
|
||||
.vecs()
|
||||
.into_iter()
|
||||
.flat_map(|v| v.vecs())
|
||||
.collect::<Vec<_>>(),
|
||||
self.address_vecs
|
||||
.vecs()
|
||||
.into_iter()
|
||||
.flat_map(|v| v.vecs())
|
||||
.collect::<Vec<_>>(),
|
||||
self.indexes_to_unspendable_supply.vecs(),
|
||||
self.indexes_to_opreturn_supply.vecs(),
|
||||
self.addresstype_to_height_to_address_count
|
||||
.as_typed_vec()
|
||||
.into_iter()
|
||||
.map(|(_, v)| v as &dyn AnyCollectableVec)
|
||||
.collect::<Vec<_>>(),
|
||||
self.addresstype_to_height_to_empty_address_count
|
||||
.as_typed_vec()
|
||||
.into_iter()
|
||||
.map(|(_, v)| v as &dyn AnyCollectableVec)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
&self.height_to_unspendable_supply,
|
||||
&self.height_to_opreturn_supply,
|
||||
&self.height_to_address_count,
|
||||
&self.height_to_empty_address_count,
|
||||
],
|
||||
]
|
||||
.into_iter()
|
||||
@@ -833,11 +999,17 @@ impl AddressTypeToTypeIndexVec<(Sats, Option<WithAddressDataSource<AddressData>>
|
||||
self.into_typed_vec().into_iter().for_each(|(_type, vec)| {
|
||||
vec.into_iter()
|
||||
.for_each(|(type_index, (value, addressdata_opt))| {
|
||||
let mut is_new = false;
|
||||
|
||||
let addressdata_withsource = addresstype_to_typeindex_to_addressdata
|
||||
.get_mut(_type)
|
||||
.unwrap()
|
||||
.entry(type_index)
|
||||
.or_insert_with(|| {
|
||||
is_new = addressdata_opt
|
||||
.as_ref()
|
||||
.is_some_and(|a| matches!(a, WithAddressDataSource::New(_)));
|
||||
|
||||
addressdata_opt.unwrap_or_else(|| {
|
||||
addresstype_to_typeindex_to_emptyaddressdata
|
||||
.get_mut(_type)
|
||||
@@ -850,21 +1022,36 @@ impl AddressTypeToTypeIndexVec<(Sats, Option<WithAddressDataSource<AddressData>>
|
||||
|
||||
let addressdata = addressdata_withsource.deref_mut();
|
||||
|
||||
let prev_filter = vecs.by_size_range.get_mut(addressdata.amount()).0.clone();
|
||||
let prev_amount = addressdata.amount();
|
||||
|
||||
addressdata.receive(value, price);
|
||||
let amount = prev_amount + value;
|
||||
|
||||
let (filter, vecs) = vecs.by_size_range.get_mut(addressdata.amount());
|
||||
if is_new
|
||||
|| vecs.by_size_range.get_mut(amount).0.clone()
|
||||
!= vecs.by_size_range.get_mut(prev_amount).0.clone()
|
||||
{
|
||||
if !is_new {
|
||||
vecs.by_size_range
|
||||
.get_mut(prev_amount)
|
||||
.1
|
||||
.state
|
||||
.subtract(addressdata);
|
||||
}
|
||||
|
||||
if *filter != prev_filter {
|
||||
// vecs.state.decrement(supply_state, price);
|
||||
addressdata.receive(value, price);
|
||||
|
||||
vecs.by_size_range.get_mut(amount).1.state.add(addressdata);
|
||||
} else {
|
||||
addressdata.receive(value, price);
|
||||
|
||||
vecs.by_size_range
|
||||
.get_mut(amount)
|
||||
.1
|
||||
.state
|
||||
.receive(value, price);
|
||||
}
|
||||
|
||||
// update vecs states if cohort changes groups
|
||||
//
|
||||
// empty addresses ?
|
||||
//
|
||||
// count change ?
|
||||
// type count change ?
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -875,6 +1062,9 @@ impl
|
||||
Sats,
|
||||
Option<WithAddressDataSource<AddressData>>,
|
||||
Option<Dollars>,
|
||||
usize,
|
||||
f64,
|
||||
bool,
|
||||
)>
|
||||
{
|
||||
fn process_sent(
|
||||
@@ -883,26 +1073,76 @@ impl
|
||||
addresstype_to_typeindex_to_addressdata: &mut AddressTypeToTypeIndexTree<
|
||||
WithAddressDataSource<AddressData>,
|
||||
>,
|
||||
addresstype_to_typeindex_to_emptyaddressdata: &mut AddressTypeToTypeIndexTree<
|
||||
WithAddressDataSource<EmptyAddressData>,
|
||||
>,
|
||||
price: Option<Dollars>,
|
||||
) -> Result<()> {
|
||||
self.into_typed_vec()
|
||||
.into_iter()
|
||||
.try_for_each(|(_type, vec)| {
|
||||
vec.into_iter()
|
||||
.try_for_each(|(type_index, (value, addressdata_opt, price))| {
|
||||
let addressdata_withsource = addresstype_to_typeindex_to_addressdata
|
||||
vec.into_iter().try_for_each(
|
||||
|(
|
||||
type_index,
|
||||
(value, addressdata_opt, prev_price, blocks_old, days_old, older_than_hour),
|
||||
)| {
|
||||
let typeindex_to_addressdata = addresstype_to_typeindex_to_addressdata
|
||||
.get_mut(_type)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
let addressdata_withsource = typeindex_to_addressdata
|
||||
.entry(type_index)
|
||||
.or_insert(addressdata_opt.unwrap());
|
||||
|
||||
addressdata_withsource.deref_mut().send(value, price)?;
|
||||
let addressdata = addressdata_withsource.deref_mut();
|
||||
|
||||
let prev_amount = addressdata.amount();
|
||||
|
||||
let amount = prev_amount.checked_sub(value).unwrap();
|
||||
|
||||
let will_be_empty = addressdata.outputs_len - 1 == 0;
|
||||
|
||||
if will_be_empty
|
||||
|| vecs.by_size_range.get_mut(amount).0.clone()
|
||||
!= vecs.by_size_range.get_mut(prev_amount).0.clone()
|
||||
{
|
||||
vecs.by_size_range
|
||||
.get_mut(prev_amount)
|
||||
.1
|
||||
.state
|
||||
.subtract(addressdata);
|
||||
|
||||
addressdata.send(value, prev_price)?;
|
||||
|
||||
if will_be_empty {
|
||||
let addressdata =
|
||||
typeindex_to_addressdata.remove(&type_index).unwrap();
|
||||
|
||||
addresstype_to_typeindex_to_emptyaddressdata
|
||||
.get_mut(_type)
|
||||
.unwrap()
|
||||
.insert(type_index, addressdata.into());
|
||||
} else {
|
||||
vecs.by_size_range.get_mut(amount).1.state.add(addressdata);
|
||||
}
|
||||
} else {
|
||||
addressdata.send(value, prev_price)?;
|
||||
|
||||
vecs.by_size_range.get_mut(amount).1.state.send(
|
||||
value,
|
||||
price,
|
||||
prev_price,
|
||||
blocks_old,
|
||||
days_old,
|
||||
older_than_hour,
|
||||
);
|
||||
}
|
||||
|
||||
// type count change
|
||||
|
||||
Ok(())
|
||||
|
||||
// move to empty if empty
|
||||
|
||||
// update vecs states if cohort changes
|
||||
})
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ use brk_core::{
|
||||
CheckedSub, Dollars, GroupFilter, HalvingEpoch, Height, Result, Timestamp, UTXOGroups,
|
||||
};
|
||||
use brk_exit::Exit;
|
||||
use brk_state::{BlockState, CohortStateTrait, Transacted};
|
||||
use brk_vec::StoredIndex;
|
||||
use rayon::prelude::*;
|
||||
|
||||
|
||||
@@ -3,12 +3,14 @@ use std::{ops::Deref, path::Path};
|
||||
use brk_core::{Bitcoin, DateIndex, Dollars, Height, Result, Version};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_state::UTXOCohortState;
|
||||
use brk_vec::{AnyCollectableVec, AnyIterableVec, Computation, Format};
|
||||
|
||||
use crate::vecs::{
|
||||
Indexes, fetched, indexes, market,
|
||||
stateful::{common, r#trait::CohortVecs},
|
||||
use crate::{
|
||||
UTXOCohortState,
|
||||
vecs::{
|
||||
Indexes, fetched, indexes, market,
|
||||
stateful::{common, r#trait::CohortVecs},
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
use std::{collections::BTreeMap, ops::ControlFlow, path::Path};
|
||||
|
||||
use brk_core::{
|
||||
CheckedSub, Dollars, GroupFilter, GroupedByDateRange, GroupedByEpoch, GroupedByFromDate,
|
||||
GroupedByFromSize, GroupedBySizeRange, GroupedBySpendableType, GroupedByTerm,
|
||||
GroupedByUpToDate, GroupedByUpToSize, HalvingEpoch, Height, Result, Timestamp, UTXOGroups,
|
||||
Version,
|
||||
};
|
||||
use brk_core::{CheckedSub, Dollars, HalvingEpoch, Height, Result, Timestamp, Version};
|
||||
use brk_exit::Exit;
|
||||
use brk_state::{BlockState, Transacted};
|
||||
use brk_vec::{Computation, Format, StoredIndex};
|
||||
use derive_deref::{Deref, DerefMut};
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::vecs::{
|
||||
Indexes, fetched,
|
||||
stateful::{r#trait::CohortVecs, utxo_cohort},
|
||||
use crate::{
|
||||
GroupFilter, GroupedByDateRange, GroupedByEpoch, GroupedByFromDate, GroupedByFromSize,
|
||||
GroupedBySizeRange, GroupedBySpendableType, GroupedByTerm, GroupedByUpToDate,
|
||||
GroupedByUpToSize, UTXOGroups,
|
||||
states::{BlockState, Transacted},
|
||||
vecs::{Indexes, fetched},
|
||||
};
|
||||
|
||||
use super::{r#trait::CohortVecs, utxo_cohort};
|
||||
|
||||
const VERSION: Version = Version::new(0);
|
||||
|
||||
#[derive(Clone, Deref, DerefMut)]
|
||||
@@ -1107,10 +1106,10 @@ impl Vecs {
|
||||
.timestamp
|
||||
.difference_in_days_between_float(last_timestamp);
|
||||
|
||||
let older_than_hour =
|
||||
jiff::Timestamp::from(last_timestamp.checked_sub(block_state.timestamp).unwrap())
|
||||
.as_second()
|
||||
>= 60 * 60;
|
||||
let older_than_hour = last_timestamp
|
||||
.checked_sub(block_state.timestamp)
|
||||
.unwrap()
|
||||
.is_more_than_hour();
|
||||
|
||||
time_based_vecs
|
||||
.iter_mut()
|
||||
@@ -1289,4 +1288,10 @@ impl Vecs {
|
||||
vecs.compute_from_stateful(starting_indexes, &stateful, exit)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn safe_flush_stateful_vecs(&mut self, height: Height, exit: &Exit) -> Result<()> {
|
||||
self.as_mut_separate_vecs()
|
||||
.par_iter_mut()
|
||||
.try_for_each(|(_, v)| v.safe_flush_stateful_vecs(height, exit))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user