diff --git a/crates/brk_computer/src/stores.rs b/crates/brk_computer/src/stores.rs index fa5f4df34..b400f7157 100644 --- a/crates/brk_computer/src/stores.rs +++ b/crates/brk_computer/src/stores.rs @@ -12,7 +12,9 @@ use brk_store::{AnyStore, Store}; use fjall::{PersistMode, TransactionalKeyspace}; use rayon::prelude::*; -use crate::vecs::stateful::AddressTypeToTypeIndexVec; +use crate::vecs::stateful::{ + AddressTypeToTypeIndexTree, AddressTypeToTypeIndexVec, WithAddressDataSource, +}; const VERSION: Version = Version::ZERO; @@ -572,43 +574,234 @@ impl Stores { }) } - // pub fn emptyaddressdata_store_increase_replaced(&mut self, address_type: OutputType) { - // match address_type { - // OutputType::P2A => self.p2aaddressindex_to_addressdata.increase_replaced(), - // OutputType::P2PK33 => self.p2pk33addressindex_to_addressdata.increase_replaced(), - // OutputType::P2PK65 => self.p2pk65addressindex_to_addressdata.increase_replaced(), - // OutputType::P2PKH => self.p2pkhaddressindex_to_addressdata.increase_replaced(), - // OutputType::P2SH => self.p2shaddressindex_to_addressdata.increase_replaced(), - // OutputType::P2TR => self.p2traddressindex_to_addressdata.increase_replaced(), - // OutputType::P2WPKH => self.p2wpkhaddressindex_to_addressdata.increase_replaced(), - // OutputType::P2WSH => self.p2wshaddressindex_to_addressdata.increase_replaced(), - // _ => unreachable!(), - // } - // } - pub fn commit( &mut self, height: Height, sent: AddressTypeToTypeIndexVec, received: AddressTypeToTypeIndexVec, + addresstype_to_typeindex_to_addressdata: AddressTypeToTypeIndexTree< + WithAddressDataSource, + >, + addresstype_to_typeindex_to_emptyaddressdata: AddressTypeToTypeIndexTree< + WithAddressDataSource, + >, ) -> Result<()> { - // &mut self.p2aaddressindex_to_addressdata, - // &mut self.p2pk33addressindex_to_addressdata, - // &mut self.p2pk65addressindex_to_addressdata, - // &mut self.p2pkhaddressindex_to_addressdata, - // &mut self.p2shaddressindex_to_addressdata, - // &mut self.p2traddressindex_to_addressdata, - // &mut self.p2wpkhaddressindex_to_addressdata, - // &mut self.p2wshaddressindex_to_addressdata, + let GroupedByAddressType { + p2pk65, + p2pk33, + p2pkh, + p2sh, + p2wpkh, + p2wsh, + p2tr, + p2a, + } = addresstype_to_typeindex_to_addressdata.unwrap(); - // &mut self.p2aaddressindex_to_emptyaddressdata, - // &mut self.p2pk33addressindex_to_emptyaddressdata, - // &mut self.p2pk65addressindex_to_emptyaddressdata, - // &mut self.p2pkhaddressindex_to_emptyaddressdata, - // &mut self.p2shaddressindex_to_emptyaddressdata, - // &mut self.p2traddressindex_to_emptyaddressdata, - // &mut self.p2wpkhaddressindex_to_emptyaddressdata, - // &mut self.p2wshaddressindex_to_emptyaddressdata, + let GroupedByAddressType { + p2pk65: empty_p2pk65, + p2pk33: empty_p2pk33, + p2pkh: empty_p2pkh, + p2sh: empty_p2sh, + p2wpkh: empty_p2wpkh, + p2wsh: empty_p2wsh, + p2tr: empty_p2tr, + p2a: empty_p2a, + } = addresstype_to_typeindex_to_emptyaddressdata.unwrap(); + + thread::scope(|s| { + s.spawn(|| { + self.p2aaddressindex_to_addressdata.commit_( + height, + empty_p2a + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2a.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + s.spawn(|| { + self.p2pk33addressindex_to_addressdata.commit_( + height, + empty_p2pk33 + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2pk33.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + s.spawn(|| { + self.p2pk65addressindex_to_addressdata.commit_( + height, + empty_p2pk65 + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2pk65.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + s.spawn(|| { + self.p2pkhaddressindex_to_addressdata.commit_( + height, + empty_p2pkh + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2pkh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + s.spawn(|| { + self.p2shaddressindex_to_addressdata.commit_( + height, + empty_p2sh + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2sh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + s.spawn(|| { + self.p2traddressindex_to_addressdata.commit_( + height, + empty_p2tr + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2tr.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + s.spawn(|| { + self.p2wpkhaddressindex_to_addressdata.commit_( + height, + empty_p2wpkh + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2wpkh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + s.spawn(|| { + self.p2wshaddressindex_to_addressdata.commit_( + height, + empty_p2wsh + .iter() + .filter(|(_, addressdata)| addressdata.is_from_addressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + p2wsh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + }); + + thread::scope(|scope| { + scope.spawn(|| { + self.p2aaddressindex_to_emptyaddressdata.commit_( + height, + p2a.iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2a.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + scope.spawn(|| { + self.p2pk33addressindex_to_emptyaddressdata.commit_( + height, + p2pk33 + .iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2pk33.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + scope.spawn(|| { + self.p2pk65addressindex_to_emptyaddressdata.commit_( + height, + p2pk65 + .iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2pk65.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + scope.spawn(|| { + self.p2pkhaddressindex_to_emptyaddressdata.commit_( + height, + p2pkh + .iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2pkh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + scope.spawn(|| { + self.p2shaddressindex_to_emptyaddressdata.commit_( + height, + p2sh.iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2sh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + scope.spawn(|| { + self.p2traddressindex_to_emptyaddressdata.commit_( + height, + p2tr.iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2tr.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + scope.spawn(|| { + self.p2wpkhaddressindex_to_emptyaddressdata.commit_( + height, + p2wpkh + .iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2wpkh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + scope.spawn(|| { + self.p2wshaddressindex_to_emptyaddressdata.commit_( + height, + p2wsh + .iter() + .filter(|(_, addressdata)| addressdata.is_from_emptyaddressdata()) + .map(|(typeindex, _)| (*typeindex).into()), + empty_p2wsh.iter().map(|(typeindex, addressdata)| { + ((*typeindex).into(), addressdata.deref().clone()) + }), + ) + }); + }); thread::scope(|s| { let GroupedByAddressType { diff --git a/crates/brk_computer/src/vecs/stateful/address_cohort.rs b/crates/brk_computer/src/vecs/stateful/address_cohort.rs index 19f37fb55..9ddbc6f0d 100644 --- a/crates/brk_computer/src/vecs/stateful/address_cohort.rs +++ b/crates/brk_computer/src/vecs/stateful/address_cohort.rs @@ -3,8 +3,10 @@ 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, CohortStateTrait}; -use brk_vec::{AnyCollectableVec, AnyIterableVec, AnyVec, Computation, EagerVec, Format}; +use brk_state::AddressCohortState; +use brk_vec::{ + AnyCollectableVec, AnyIterableVec, AnyVec, Computation, EagerVec, Format, VecIterator, +}; use crate::vecs::{ Indexes, fetched, indexes, market, @@ -68,6 +70,7 @@ impl CohortVecs for Vecs { fn starting_height(&self) -> Height { [ self.state + .inner .price_to_amount .height() .map_or(Height::MAX, |h| h.incremented()), @@ -86,7 +89,15 @@ impl CohortVecs for Vecs { self.starting_height = starting_height; - self.inner.init(&mut self.starting_height, &mut self.state); + if let Some(prev_height) = starting_height.decremented() { + self.state.address_count = *self + .height_to_address_count + .into_iter() + .unwrap_get_inner(prev_height); + } + + self.inner + .init(&mut self.starting_height, &mut self.state.inner); } fn validate_computed_versions(&mut self, base_version: Version) -> Result<()> { @@ -109,7 +120,7 @@ impl CohortVecs for Vecs { exit, )?; - self.inner.forced_pushed_at(height, exit, &self.state) + self.inner.forced_pushed_at(height, exit, &self.state.inner) } fn compute_then_force_push_unrealized_states( @@ -126,7 +137,7 @@ impl CohortVecs for Vecs { dateindex, date_price, exit, - &self.state, + &self.state.inner, ) } @@ -134,7 +145,7 @@ impl CohortVecs for Vecs { self.height_to_address_count.safe_flush(exit)?; self.inner - .safe_flush_stateful_vecs(height, exit, &mut self.state) + .safe_flush_stateful_vecs(height, exit, &mut self.state.inner) } fn compute_from_stateful( diff --git a/crates/brk_computer/src/vecs/stateful/address_cohorts.rs b/crates/brk_computer/src/vecs/stateful/address_cohorts.rs index 7e1b41de8..84685a783 100644 --- a/crates/brk_computer/src/vecs/stateful/address_cohorts.rs +++ b/crates/brk_computer/src/vecs/stateful/address_cohorts.rs @@ -1,18 +1,22 @@ use std::path::Path; use brk_core::{ - AddressGroups, GroupFilter, GroupedByFromSize, GroupedBySizeRange, GroupedByUpToSize, Version, + AddressGroups, GroupFilter, GroupedByFromSize, GroupedBySizeRange, GroupedByUpToSize, Result, + Version, }; +use brk_exit::Exit; use brk_vec::{Computation, Format}; +use derive_deref::{Deref, DerefMut}; +use rayon::prelude::*; use crate::vecs::{ - fetched, + Indexes, fetched, stateful::{address_cohort, r#trait::CohortVecs}, }; const VERSION: Version = Version::new(0); -#[derive(Clone)] +#[derive(Clone, Deref, DerefMut)] pub struct Vecs(AddressGroups<(GroupFilter, address_cohort::Vecs)>); impl Vecs { @@ -276,4 +280,50 @@ impl Vecs { .into(), )) } + + pub fn compute_overlapping_vecs( + &mut self, + starting_indexes: &Indexes, + exit: &Exit, + ) -> Result<()> { + let by_size_range = self.0.by_size_range.as_vec(); + + [ + self.0 + .by_from_size + .as_mut_vec() + .into_iter() + .map(|(filter, vecs)| { + ( + vecs, + by_size_range + .into_iter() + .filter(|(other, _)| filter.includes(other)) + .map(|(_, v)| v) + .collect::>(), + ) + }) + .collect::>(), + self.0 + .by_up_to_size + .as_mut_vec() + .into_iter() + .map(|(filter, vecs)| { + ( + vecs, + by_size_range + .into_iter() + .filter(|(other, _)| filter.includes(other)) + .map(|(_, v)| v) + .collect::>(), + ) + }) + .collect::>(), + ] + .into_par_iter() + .flatten() + .try_for_each(|(vecs, stateful)| { + vecs.compute_from_stateful(starting_indexes, &stateful, exit) + }) + } } diff --git a/crates/brk_computer/src/vecs/stateful/addresstype_to_typeindex_tree.rs b/crates/brk_computer/src/vecs/stateful/addresstype_to_typeindex_tree.rs index dd5fb9ec6..d136d7596 100644 --- a/crates/brk_computer/src/vecs/stateful/addresstype_to_typeindex_tree.rs +++ b/crates/brk_computer/src/vecs/stateful/addresstype_to_typeindex_tree.rs @@ -29,7 +29,7 @@ impl AddressTypeToTypeIndexTree { } } - pub fn inner(self) -> GroupedByAddressType> { + pub fn unwrap(self) -> GroupedByAddressType> { self.0 } } diff --git a/crates/brk_computer/src/vecs/stateful/anyaddressdata.rs b/crates/brk_computer/src/vecs/stateful/anyaddressdata.rs deleted file mode 100644 index 9fa18babc..000000000 --- a/crates/brk_computer/src/vecs/stateful/anyaddressdata.rs +++ /dev/null @@ -1,19 +0,0 @@ -use brk_core::{AddressData, EmptyAddressData}; - -#[derive(Debug)] -pub enum AnyAddressData { - AddressData(AddressData), - EmptyAddressData(EmptyAddressData), -} - -impl From for AnyAddressData { - fn from(value: AddressData) -> Self { - Self::AddressData(value) - } -} - -impl From for AnyAddressData { - fn from(value: EmptyAddressData) -> Self { - Self::EmptyAddressData(value) - } -} diff --git a/crates/brk_computer/src/vecs/stateful/common.rs b/crates/brk_computer/src/vecs/stateful/common.rs index 735b16202..eb7a88318 100644 --- a/crates/brk_computer/src/vecs/stateful/common.rs +++ b/crates/brk_computer/src/vecs/stateful/common.rs @@ -5,7 +5,7 @@ use brk_core::{ }; use brk_exit::Exit; use brk_indexer::Indexer; -use brk_state::{CohortState, CohortStateTrait}; +use brk_state::CohortState; use brk_vec::{ AnyCollectableVec, AnyIterableVec, AnyVec, Computation, EagerVec, Format, VecIterator, }; diff --git a/crates/brk_computer/src/vecs/stateful/mod.rs b/crates/brk_computer/src/vecs/stateful/mod.rs index b5b632384..19bdfadb7 100644 --- a/crates/brk_computer/src/vecs/stateful/mod.rs +++ b/crates/brk_computer/src/vecs/stateful/mod.rs @@ -13,7 +13,7 @@ use brk_vec::{ use log::info; use rayon::prelude::*; -use brk_state::{BlockState, CohortStateTrait, SupplyState, Transacted}; +use brk_state::{BlockState, SupplyState, Transacted}; use crate::{stores::Stores, vecs::market}; @@ -27,17 +27,16 @@ mod address_cohort; mod address_cohorts; mod addresstype_to_typeindex_tree; mod addresstype_to_typeindex_vec; -mod anyaddressdata; mod common; mod r#trait; mod utxo_cohort; mod utxo_cohorts; mod withaddressdatasource; -use addresstype_to_typeindex_tree::*; +pub use addresstype_to_typeindex_tree::*; pub use addresstype_to_typeindex_vec::*; use r#trait::CohortVecs; -use withaddressdatasource::WithAddressDataSource; +pub use withaddressdatasource::WithAddressDataSource; const VERSION: Version = Version::new(5); @@ -229,6 +228,7 @@ impl Vecs { let outputindex_to_typeindex_mmap = outputindex_to_typeindex.mmap().load(); 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 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(); @@ -378,6 +378,11 @@ impl Vecs { .iter_mut() .for_each(|(_, v)| v.state.reset_single_iteration_values()); + self.address_vecs + .as_mut_separate_vecs() + .iter_mut() + .for_each(|(_, v)| v.state.reset_single_iteration_values()); + info!("Processing chain at {height}..."); let timestamp = height_to_timestamp_fixed_iter.unwrap_get_inner(height); @@ -453,23 +458,27 @@ impl Vecs { .unwrap() .into_owned(); - (height, value, input_type, typeindex, outputindex, addressdata_opt) + let dollars_opt = height_to_close.map(|m| *m.get_or_read(height, height_to_close_mmap.as_ref().unwrap()).unwrap() + .unwrap() + .into_owned()); + + (height, value, input_type, typeindex, outputindex, addressdata_opt, dollars_opt) }) .fold( || { ( BTreeMap::::default(), AddressTypeToTypeIndexVec::::default(), - AddressTypeToTypeIndexVec::<(Sats, Option>)>::default(), + AddressTypeToTypeIndexVec::<(Sats, Option>, Option)>::default(), ) }, - |(mut tree, mut vecs, mut vecs2), (height, value, input_type, typeindex, outputindex, addressdata_opt)| { + |(mut tree, mut vecs, mut vecs2), (height, value, input_type, typeindex, outputindex, addressdata_opt, dollars_opt)| { 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))); + vec.push((typeindex, (value, addressdata_opt, dollars_opt))); } (tree, vecs, vecs2) }, @@ -478,7 +487,7 @@ impl Vecs { ( BTreeMap::::default(), AddressTypeToTypeIndexVec::::default(), - AddressTypeToTypeIndexVec::<(Sats, Option>)>::default(), + AddressTypeToTypeIndexVec::<(Sats, Option>, Option)>::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() { @@ -562,7 +571,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 addresstype_to_typeindex_to_addressdata, &mut addresstype_to_typeindex_to_emptyaddressdata, price); + 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_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 @@ -665,7 +676,8 @@ impl Vecs { stores.commit( height, mem::take(&mut addresstype_to_typeindex_to_sent_outputindex), - mem::take( &mut addresstype_to_typeindex_to_received_outputindex) + mem::take(&mut addresstype_to_typeindex_to_received_outputindex), + mem::take(&mut addresstype_to_typeindex_to_addressdata), mem::take(&mut addresstype_to_typeindex_to_emptyaddressdata) )?; exit.release(); } @@ -682,6 +694,8 @@ impl Vecs { height, mem::take(&mut addresstype_to_typeindex_to_sent_outputindex), mem::take(&mut addresstype_to_typeindex_to_received_outputindex), + mem::take(&mut addresstype_to_typeindex_to_addressdata), + mem::take(&mut addresstype_to_typeindex_to_emptyaddressdata), )?; } else { exit.block(); @@ -807,6 +821,7 @@ impl Vecs { impl AddressTypeToTypeIndexVec<(Sats, Option>)> { fn process_received( mut self, + vecs: &mut address_cohorts::Vecs, addresstype_to_typeindex_to_addressdata: &mut AddressTypeToTypeIndexTree< WithAddressDataSource, >, @@ -818,7 +833,7 @@ impl AddressTypeToTypeIndexVec<(Sats, Option> self.into_typed_vec().into_iter().for_each(|(_type, vec)| { vec.into_iter() .for_each(|(type_index, (value, addressdata_opt))| { - addresstype_to_typeindex_to_addressdata + let addressdata_withsource = addresstype_to_typeindex_to_addressdata .get_mut(_type) .unwrap() .entry(type_index) @@ -831,37 +846,61 @@ impl AddressTypeToTypeIndexVec<(Sats, Option> .unwrap() .into() }) - }) - .deref_mut() - .receive(value, price); + }); - // update vecs states if cohort changes + let addressdata = addressdata_withsource.deref_mut(); + + let prev_filter = vecs.by_size_range.get_mut(addressdata.amount()).0.clone(); + + addressdata.receive(value, price); + + let (filter, vecs) = vecs.by_size_range.get_mut(addressdata.amount()); + + if *filter != prev_filter { + // vecs.state.decrement(supply_state, price); + } + + // update vecs states if cohort changes groups + // + // empty addresses ? + // + // count change ? }); }); } +} +impl + AddressTypeToTypeIndexVec<( + Sats, + Option>, + Option, + )> +{ fn process_sent( mut self, + vecs: &mut address_cohorts::Vecs, addresstype_to_typeindex_to_addressdata: &mut AddressTypeToTypeIndexTree< WithAddressDataSource, >, - price: Option, ) -> Result<()> { self.into_typed_vec() .into_iter() .try_for_each(|(_type, vec)| { vec.into_iter() - .try_for_each(|(type_index, (value, addressdata_opt))| { - addresstype_to_typeindex_to_addressdata + .try_for_each(|(type_index, (value, addressdata_opt, price))| { + let addressdata_withsource = addresstype_to_typeindex_to_addressdata .get_mut(_type) .unwrap() .entry(type_index) - .or_insert(addressdata_opt.unwrap()) - .deref_mut() - .send(value, price)?; + .or_insert(addressdata_opt.unwrap()); + + addressdata_withsource.deref_mut().send(value, price)?; Ok(()) + // move to empty if empty + // update vecs states if cohort changes }) }) diff --git a/crates/brk_computer/src/vecs/stateful/utxo_cohort.rs b/crates/brk_computer/src/vecs/stateful/utxo_cohort.rs index f417a1853..e30a1d0eb 100644 --- a/crates/brk_computer/src/vecs/stateful/utxo_cohort.rs +++ b/crates/brk_computer/src/vecs/stateful/utxo_cohort.rs @@ -3,7 +3,7 @@ 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::{CohortStateTrait, UTXOCohortState}; +use brk_state::UTXOCohortState; use brk_vec::{AnyCollectableVec, AnyIterableVec, Computation, Format}; use crate::vecs::{ diff --git a/crates/brk_computer/src/vecs/stateful/utxo_cohorts.rs b/crates/brk_computer/src/vecs/stateful/utxo_cohorts.rs index 7d927eae2..e69a0f2ef 100644 --- a/crates/brk_computer/src/vecs/stateful/utxo_cohorts.rs +++ b/crates/brk_computer/src/vecs/stateful/utxo_cohorts.rs @@ -7,7 +7,7 @@ use brk_core::{ Version, }; use brk_exit::Exit; -use brk_state::{BlockState, CohortStateTrait, Transacted}; +use brk_state::{BlockState, Transacted}; use brk_vec::{Computation, Format, StoredIndex}; use derive_deref::{Deref, DerefMut}; use rayon::prelude::*; @@ -1103,7 +1103,7 @@ impl Vecs { .timestamp .difference_in_days_between(last_timestamp); - let days_old_foat = block_state + let days_old_float = block_state .timestamp .difference_in_days_between_float(last_timestamp); @@ -1127,7 +1127,7 @@ impl Vecs { current_price, prev_price, blocks_old, - days_old_foat, + days_old_float, older_than_hour, ); }); @@ -1139,7 +1139,7 @@ impl Vecs { current_price, prev_price, blocks_old, - days_old_foat, + days_old_float, older_than_hour, ) }, @@ -1154,7 +1154,7 @@ impl Vecs { current_price, prev_price, blocks_old, - days_old_foat, + days_old_float, older_than_hour, ); }); diff --git a/crates/brk_computer/src/vecs/stateful/withaddressdatasource.rs b/crates/brk_computer/src/vecs/stateful/withaddressdatasource.rs index 1c03a9bc1..92dcc1b56 100644 --- a/crates/brk_computer/src/vecs/stateful/withaddressdatasource.rs +++ b/crates/brk_computer/src/vecs/stateful/withaddressdatasource.rs @@ -12,6 +12,14 @@ impl WithAddressDataSource { matches!(self, Self::New(_)) } + pub fn is_from_addressdata(&self) -> bool { + matches!(self, Self::FromAddressDataStore(_)) + } + + pub fn is_from_emptyaddressdata(&self) -> bool { + matches!(self, Self::FromEmptyAddressDataStore(_)) + } + pub fn deref(&self) -> &T { match self { Self::New(v) => v, diff --git a/crates/brk_core/src/enums/mod.rs b/crates/brk_core/src/enums/mod.rs deleted file mode 100644 index 3925f93ff..000000000 --- a/crates/brk_core/src/enums/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod error; - -pub use error::*; diff --git a/crates/brk_core/src/enums/error.rs b/crates/brk_core/src/error.rs similarity index 100% rename from crates/brk_core/src/enums/error.rs rename to crates/brk_core/src/error.rs diff --git a/crates/brk_core/src/groups/filter.rs b/crates/brk_core/src/groups/filter.rs index e47c5d06e..0107d573c 100644 --- a/crates/brk_core/src/groups/filter.rs +++ b/crates/brk_core/src/groups/filter.rs @@ -2,7 +2,7 @@ use std::ops::Range; use crate::{HalvingEpoch, OutputType}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum GroupFilter { All, To(usize), diff --git a/crates/brk_core/src/lib.rs b/crates/brk_core/src/lib.rs index cb0989543..ffffbbe39 100644 --- a/crates/brk_core/src/lib.rs +++ b/crates/brk_core/src/lib.rs @@ -1,12 +1,12 @@ #![doc = include_str!("../README.md")] -mod enums; +mod error; mod groups; mod structs; mod traits; mod utils; -pub use enums::*; +pub use error::*; pub use groups::*; pub use structs::*; pub use traits::*; diff --git a/crates/brk_state/src/cohorts/address.rs b/crates/brk_state/src/cohorts/address.rs index 871ac8664..f3ce22575 100644 --- a/crates/brk_state/src/cohorts/address.rs +++ b/crates/brk_state/src/cohorts/address.rs @@ -5,7 +5,7 @@ use std::{ use brk_core::{Dollars, Height, Result}; -use crate::{CohortStateTrait, SupplyState, UnrealizedState}; +use crate::{SupplyState, UnrealizedState}; use super::CohortState; @@ -15,76 +15,77 @@ pub struct AddressCohortState { pub inner: CohortState, } -impl CohortStateTrait for AddressCohortState { - fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result { +impl AddressCohortState { + pub fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result { Ok(Self { address_count: 0, inner: CohortState::default_and_import(path, name, compute_dollars)?, }) } - fn reset_single_iteration_values(&mut self) { + pub fn reset_single_iteration_values(&mut self) { self.inner.reset_single_iteration_values(); } - - fn increment(&mut self, supply_state: &SupplyState, price: Option) { - self.inner.increment(supply_state, price); - } - - fn decrement(&mut self, supply_state: &SupplyState, price: Option) { - 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) { - self.inner.receive(supply_state, price); - } - - fn send( - &mut self, - supply_state: &SupplyState, - current_price: Option, - prev_price: Option, - blocks_old: usize, - days_old: f64, - older_than_hour: bool, - ) { - self.inner.send( - supply_state, - current_price, - prev_price, - blocks_old, - days_old, - older_than_hour, - ); - } - - fn compute_unrealized_states( - &self, - height_price: Dollars, - date_price: Option, - ) -> (UnrealizedState, Option) { - self.inner - .compute_unrealized_states(height_price, date_price) - } - - fn commit(&mut self, height: Height) -> Result<()> { - self.inner.commit(height) - } } -impl Deref for AddressCohortState { - type Target = CohortState; - fn deref(&self) -> &Self::Target { - &self.inner - } -} +// fn increment(&mut self, supply_state: &SupplyState, price: Option) { +// self.inner.increment(supply_state, price); +// } -impl DerefMut for AddressCohortState { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } -} +// fn decrement(&mut self, supply_state: &SupplyState, price: Option) { +// 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) { +// self.inner.receive(supply_state, price); +// } + +// fn send( +// &mut self, +// supply_state: &SupplyState, +// current_price: Option, +// prev_price: Option, +// blocks_old: usize, +// days_old: f64, +// older_than_hour: bool, +// ) { +// self.inner.send( +// supply_state, +// current_price, +// prev_price, +// blocks_old, +// days_old, +// older_than_hour, +// ); +// } + +// fn compute_unrealized_states( +// &self, +// height_price: Dollars, +// date_price: Option, +// ) -> (UnrealizedState, Option) { +// self.inner +// .compute_unrealized_states(height_price, date_price) +// } + +// fn commit(&mut self, height: Height) -> Result<()> { +// self.inner.commit(height) +// } +// } + +// 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 +// } +// } diff --git a/crates/brk_state/src/cohorts/common.rs b/crates/brk_state/src/cohorts/common.rs index 293199404..d0535a181 100644 --- a/crates/brk_state/src/cohorts/common.rs +++ b/crates/brk_state/src/cohorts/common.rs @@ -2,7 +2,7 @@ use std::{cmp::Ordering, path::Path}; use brk_core::{CheckedSub, Dollars, Height, Result, Sats}; -use crate::{CohortStateTrait, PriceToAmount, RealizedState, SupplyState, UnrealizedState}; +use crate::{PriceToAmount, RealizedState, SupplyState, UnrealizedState}; #[derive(Clone)] pub struct CohortState { @@ -13,8 +13,8 @@ pub struct CohortState { pub price_to_amount: PriceToAmount, } -impl CohortStateTrait for CohortState { - fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result { +impl CohortState { + pub fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result { Ok(Self { supply: SupplyState::default(), realized: compute_dollars.then_some(RealizedState::NAN), @@ -24,7 +24,7 @@ impl CohortStateTrait for CohortState { }) } - fn reset_single_iteration_values(&mut self) { + 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() { @@ -32,7 +32,7 @@ impl CohortStateTrait for CohortState { } } - fn increment(&mut self, supply_state: &SupplyState, price: Option) { + pub fn increment(&mut self, supply_state: &SupplyState, price: Option) { self.supply += supply_state; if supply_state.value > Sats::ZERO { @@ -44,7 +44,7 @@ impl CohortStateTrait for CohortState { } } - fn decrement(&mut self, supply_state: &SupplyState, price: Option) { + pub fn decrement(&mut self, supply_state: &SupplyState, price: Option) { self.supply -= supply_state; if supply_state.value > Sats::ZERO { @@ -64,7 +64,7 @@ impl CohortStateTrait for CohortState { } } - fn receive(&mut self, supply_state: &SupplyState, price: Option) { + pub fn receive(&mut self, supply_state: &SupplyState, price: Option) { self.supply += supply_state; if supply_state.value > Sats::ZERO { @@ -76,7 +76,7 @@ impl CohortStateTrait for CohortState { } } - fn send( + pub fn send( &mut self, supply_state: &SupplyState, current_price: Option, @@ -104,7 +104,7 @@ impl CohortStateTrait for CohortState { } } - fn compute_unrealized_states( + pub fn compute_unrealized_states( &self, height_price: Dollars, date_price: Option, @@ -166,7 +166,7 @@ impl CohortStateTrait for CohortState { (height_unrealized_state, date_unrealized_state) } - fn commit(&mut self, height: Height) -> Result<()> { + pub fn commit(&mut self, height: Height) -> Result<()> { self.price_to_amount.flush(height) } } diff --git a/crates/brk_state/src/cohorts/mod.rs b/crates/brk_state/src/cohorts/mod.rs index 392e08ad8..4c579d1a0 100644 --- a/crates/brk_state/src/cohorts/mod.rs +++ b/crates/brk_state/src/cohorts/mod.rs @@ -1,9 +1,9 @@ mod address; mod common; -mod r#trait; +// mod r#trait; mod utxo; pub use address::*; pub use common::*; -pub use r#trait::*; +// pub use r#trait::*; pub use utxo::*; diff --git a/crates/brk_state/src/cohorts/utxo.rs b/crates/brk_state/src/cohorts/utxo.rs index 905438297..bfe580d0d 100644 --- a/crates/brk_state/src/cohorts/utxo.rs +++ b/crates/brk_state/src/cohorts/utxo.rs @@ -3,70 +3,71 @@ use std::path::Path; use brk_core::{Dollars, Height, Result}; use derive_deref::{Deref, DerefMut}; -use crate::{CohortStateTrait, SupplyState, UnrealizedState}; +use crate::{SupplyState, UnrealizedState}; use super::CohortState; #[derive(Clone, Deref, DerefMut)] pub struct UTXOCohortState(CohortState); -impl CohortStateTrait for UTXOCohortState { - fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result { +impl UTXOCohortState { + pub fn default_and_import(path: &Path, name: &str, compute_dollars: bool) -> Result { Ok(Self(CohortState::default_and_import( path, name, compute_dollars, )?)) } - - fn reset_single_iteration_values(&mut self) { - self.0.reset_single_iteration_values(); - } - - fn increment(&mut self, supply_state: &SupplyState, price: Option) { - self.0.increment(supply_state, price); - } - - fn decrement(&mut self, supply_state: &SupplyState, price: Option) { - self.0.decrement(supply_state, price); - } - - fn decrement_price_to_amount(&mut self, supply_state: &SupplyState, price: Dollars) { - self.0.decrement_price_to_amount(supply_state, price); - } - - fn receive(&mut self, supply_state: &SupplyState, price: Option) { - self.0.receive(supply_state, price); - } - - fn send( - &mut self, - supply_state: &SupplyState, - current_price: Option, - prev_price: Option, - blocks_old: usize, - days_old: f64, - older_than_hour: bool, - ) { - self.0.send( - supply_state, - current_price, - prev_price, - blocks_old, - days_old, - older_than_hour, - ); - } - - fn compute_unrealized_states( - &self, - height_price: Dollars, - date_price: Option, - ) -> (UnrealizedState, Option) { - self.0.compute_unrealized_states(height_price, date_price) - } - - fn commit(&mut self, height: Height) -> Result<()> { - self.0.commit(height) - } } + +// fn reset_single_iteration_values(&mut self) { +// self.0.reset_single_iteration_values(); +// } + +// fn increment(&mut self, supply_state: &SupplyState, price: Option) { +// self.0.increment(supply_state, price); +// } + +// fn decrement(&mut self, supply_state: &SupplyState, price: Option) { +// self.0.decrement(supply_state, price); +// } + +// fn decrement_price_to_amount(&mut self, supply_state: &SupplyState, price: Dollars) { +// self.0.decrement_price_to_amount(supply_state, price); +// } + +// fn receive(&mut self, supply_state: &SupplyState, price: Option) { +// self.0.receive(supply_state, price); +// } + +// fn send( +// &mut self, +// supply_state: &SupplyState, +// current_price: Option, +// prev_price: Option, +// blocks_old: usize, +// days_old: f64, +// older_than_hour: bool, +// ) { +// self.0.send( +// supply_state, +// current_price, +// prev_price, +// blocks_old, +// days_old, +// older_than_hour, +// ); +// } + +// fn compute_unrealized_states( +// &self, +// height_price: Dollars, +// date_price: Option, +// ) -> (UnrealizedState, Option) { +// self.0.compute_unrealized_states(height_price, date_price) +// } + +// fn commit(&mut self, height: Height) -> Result<()> { +// self.0.commit(height) +// } +// }