mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 07:09:59 -07:00
brk: first commit
This commit is contained in:
199
_src/parser/datasets/utxo/dataset.rs
Normal file
199
_src/parser/datasets/utxo/dataset.rs
Normal file
@@ -0,0 +1,199 @@
|
||||
use allocative::Allocative;
|
||||
use struct_iterable::Iterable;
|
||||
|
||||
use crate::{
|
||||
parser::{
|
||||
datasets::{AnyDataset, ComputeData, InsertData, MinInitialStates, SubDataset},
|
||||
states::UTXOCohortId,
|
||||
},
|
||||
structs::{BiMap, Config, Date, Height, MapPath},
|
||||
};
|
||||
|
||||
#[derive(Allocative, Iterable)]
|
||||
pub struct UTXODataset {
|
||||
id: UTXOCohortId,
|
||||
|
||||
min_initial_states: MinInitialStates,
|
||||
|
||||
pub subs: SubDataset,
|
||||
}
|
||||
|
||||
impl UTXODataset {
|
||||
pub fn import(
|
||||
parent_path: &MapPath,
|
||||
id: UTXOCohortId,
|
||||
config: &Config,
|
||||
) -> color_eyre::Result<Self> {
|
||||
let name = id.name().to_owned();
|
||||
|
||||
let mut s = Self {
|
||||
min_initial_states: MinInitialStates::default(),
|
||||
id,
|
||||
subs: SubDataset::import(parent_path, &Some(name), config)?,
|
||||
};
|
||||
|
||||
s.min_initial_states
|
||||
.consume(MinInitialStates::compute_from_dataset(&s, config));
|
||||
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, insert_data: &InsertData) {
|
||||
let &InsertData {
|
||||
states,
|
||||
utxo_cohorts_one_shot_states,
|
||||
// utxo_cohorts_received_states,
|
||||
utxo_cohorts_sent_states,
|
||||
..
|
||||
} = insert_data;
|
||||
|
||||
if self.needs_insert_supply(insert_data.height, insert_data.date) {
|
||||
self.subs.supply.insert(
|
||||
insert_data,
|
||||
&states
|
||||
.utxo_cohorts_durable_states
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.get(&self.id)
|
||||
.durable_states
|
||||
.supply_state,
|
||||
);
|
||||
}
|
||||
|
||||
if self.needs_insert_utxo(insert_data.height, insert_data.date) {
|
||||
self.subs.utxo.insert(
|
||||
insert_data,
|
||||
&states
|
||||
.utxo_cohorts_durable_states
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.get(&self.id)
|
||||
.durable_states
|
||||
.utxo_state,
|
||||
);
|
||||
}
|
||||
|
||||
if self.needs_insert_capitalization(insert_data.height, insert_data.date) {
|
||||
self.subs.capitalization.insert(
|
||||
insert_data,
|
||||
&states
|
||||
.utxo_cohorts_durable_states
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.get(&self.id)
|
||||
.durable_states
|
||||
.capitalization_state,
|
||||
);
|
||||
}
|
||||
|
||||
if self.needs_insert_unrealized(insert_data.height, insert_data.date) {
|
||||
self.subs.unrealized.insert(
|
||||
insert_data,
|
||||
&utxo_cohorts_one_shot_states
|
||||
.get(&self.id)
|
||||
.unrealized_block_state,
|
||||
&utxo_cohorts_one_shot_states
|
||||
.get(&self.id)
|
||||
.unrealized_date_state,
|
||||
);
|
||||
}
|
||||
|
||||
if self.needs_insert_price_paid(insert_data.height, insert_data.date) {
|
||||
self.subs.price_paid.insert(
|
||||
insert_data,
|
||||
&utxo_cohorts_one_shot_states.get(&self.id).price_paid_state,
|
||||
);
|
||||
}
|
||||
|
||||
if self.needs_insert_realized(insert_data.height, insert_data.date) {
|
||||
self.subs.realized.insert(
|
||||
insert_data,
|
||||
&utxo_cohorts_sent_states.get(&self.id).realized,
|
||||
);
|
||||
}
|
||||
|
||||
if self.needs_insert_input(insert_data.height, insert_data.date) {
|
||||
self.subs
|
||||
.input
|
||||
.insert(insert_data, &utxo_cohorts_sent_states.get(&self.id).input);
|
||||
}
|
||||
|
||||
// TODO: move output from common to address
|
||||
// if self.subs.output.needs_insert(insert_data) {
|
||||
// self.subs
|
||||
// .output
|
||||
// .insert(insert_data, utxo_cohorts_received_states.get(&self.id));
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn needs_insert_utxo(&self, height: Height, date: Date) -> bool {
|
||||
self.subs.utxo.needs_insert(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_insert_capitalization(&self, height: Height, date: Date) -> bool {
|
||||
self.subs.capitalization.needs_insert(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_insert_supply(&self, height: Height, date: Date) -> bool {
|
||||
self.subs.supply.needs_insert(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_insert_price_paid(&self, height: Height, date: Date) -> bool {
|
||||
self.subs.price_paid.needs_insert(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_insert_realized(&self, height: Height, date: Date) -> bool {
|
||||
self.subs.realized.needs_insert(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_insert_unrealized(&self, height: Height, date: Date) -> bool {
|
||||
self.subs.unrealized.needs_insert(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_insert_input(&self, height: Height, date: Date) -> bool {
|
||||
self.subs.input.needs_insert(height, date)
|
||||
}
|
||||
|
||||
pub fn compute(
|
||||
&mut self,
|
||||
compute_data: &ComputeData,
|
||||
closes: &mut BiMap<f32>,
|
||||
circulating_supply: &mut BiMap<f64>,
|
||||
market_cap: &mut BiMap<f32>,
|
||||
) {
|
||||
if self.subs.supply.should_compute(compute_data) {
|
||||
self.subs.supply.compute(compute_data, circulating_supply);
|
||||
}
|
||||
|
||||
if self.subs.unrealized.should_compute(compute_data) {
|
||||
self.subs.unrealized.compute(
|
||||
compute_data,
|
||||
&mut self.subs.supply.supply,
|
||||
circulating_supply,
|
||||
market_cap,
|
||||
);
|
||||
}
|
||||
|
||||
if self.subs.realized.should_compute(compute_data) {
|
||||
self.subs.realized.compute(compute_data, market_cap);
|
||||
}
|
||||
|
||||
if self.subs.capitalization.should_compute(compute_data) {
|
||||
self.subs
|
||||
.capitalization
|
||||
.compute(compute_data, closes, &mut self.subs.supply.supply);
|
||||
}
|
||||
|
||||
// if self.subs.output.should_compute(compute_data) {
|
||||
// self.subs
|
||||
// .output
|
||||
// .compute(compute_data, &mut self.subs.supply.total);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
impl AnyDataset for UTXODataset {
|
||||
fn get_min_initial_states(&self) -> &MinInitialStates {
|
||||
&self.min_initial_states
|
||||
}
|
||||
}
|
||||
164
_src/parser/datasets/utxo/mod.rs
Normal file
164
_src/parser/datasets/utxo/mod.rs
Normal file
@@ -0,0 +1,164 @@
|
||||
mod dataset;
|
||||
|
||||
use allocative::Allocative;
|
||||
use dataset::*;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{
|
||||
parser::datasets::AnyDatasets,
|
||||
parser::states::{SplitByUTXOCohort, UTXOCohortId},
|
||||
structs::{BiMap, Config, Date, Height},
|
||||
};
|
||||
|
||||
use super::{AnyDataset, ComputeData, InsertData, MinInitialStates};
|
||||
|
||||
#[derive(Allocative)]
|
||||
pub struct UTXODatasets {
|
||||
min_initial_states: MinInitialStates,
|
||||
|
||||
cohorts: SplitByUTXOCohort<UTXODataset>,
|
||||
}
|
||||
|
||||
impl UTXODatasets {
|
||||
pub fn import(config: &Config) -> color_eyre::Result<Self> {
|
||||
let mut cohorts = SplitByUTXOCohort::<Option<UTXODataset>>::default();
|
||||
|
||||
let path_dataset = config.path_datasets();
|
||||
|
||||
cohorts
|
||||
.as_vec()
|
||||
.into_par_iter()
|
||||
.map(|(_, id)| (id, UTXODataset::import(&path_dataset, id, config)))
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.try_for_each(|(id, dataset)| -> color_eyre::Result<()> {
|
||||
cohorts.get_mut(&id).replace(dataset?);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let mut s = Self {
|
||||
min_initial_states: MinInitialStates::default(),
|
||||
|
||||
cohorts: cohorts.unwrap(),
|
||||
};
|
||||
|
||||
s.min_initial_states
|
||||
.consume(MinInitialStates::compute_from_datasets(&s, config));
|
||||
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, insert_data: &InsertData) {
|
||||
self.cohorts
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.for_each(|(cohort, _)| cohort.insert(insert_data))
|
||||
}
|
||||
|
||||
pub fn needs_durable_states(&self, height: Height, date: Date) -> bool {
|
||||
let needs_insert_utxo = self.needs_insert_utxo(height, date);
|
||||
let needs_insert_capitalization = self.needs_insert_capitalization(height, date);
|
||||
let needs_insert_supply = self.needs_insert_supply(height, date);
|
||||
let needs_one_shot_states = self.needs_one_shot_states(height, date);
|
||||
|
||||
needs_insert_utxo
|
||||
|| needs_insert_capitalization
|
||||
|| needs_insert_supply
|
||||
|| needs_one_shot_states
|
||||
}
|
||||
|
||||
pub fn needs_one_shot_states(&self, height: Height, date: Date) -> bool {
|
||||
self.needs_insert_price_paid(height, date) || self.needs_insert_unrealized(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_sent_states(&self, height: Height, date: Date) -> bool {
|
||||
self.needs_insert_input(height, date) || self.needs_insert_realized(height, date)
|
||||
}
|
||||
|
||||
pub fn needs_insert_utxo(&self, height: Height, date: Date) -> bool {
|
||||
self.as_vec()
|
||||
.iter()
|
||||
.any(|(dataset, _)| dataset.needs_insert_utxo(height, date))
|
||||
}
|
||||
|
||||
pub fn needs_insert_capitalization(&self, height: Height, date: Date) -> bool {
|
||||
self.as_vec()
|
||||
.iter()
|
||||
.any(|(dataset, _)| dataset.needs_insert_capitalization(height, date))
|
||||
}
|
||||
|
||||
pub fn needs_insert_supply(&self, height: Height, date: Date) -> bool {
|
||||
self.as_vec()
|
||||
.iter()
|
||||
.any(|(dataset, _)| dataset.needs_insert_supply(height, date))
|
||||
}
|
||||
|
||||
pub fn needs_insert_price_paid(&self, height: Height, date: Date) -> bool {
|
||||
self.as_vec()
|
||||
.iter()
|
||||
.any(|(dataset, _)| dataset.needs_insert_price_paid(height, date))
|
||||
}
|
||||
|
||||
pub fn needs_insert_realized(&self, height: Height, date: Date) -> bool {
|
||||
self.as_vec()
|
||||
.iter()
|
||||
.any(|(dataset, _)| dataset.needs_insert_realized(height, date))
|
||||
}
|
||||
|
||||
pub fn needs_insert_unrealized(&self, height: Height, date: Date) -> bool {
|
||||
self.as_vec()
|
||||
.iter()
|
||||
.any(|(dataset, _)| dataset.needs_insert_unrealized(height, date))
|
||||
}
|
||||
|
||||
pub fn needs_insert_input(&self, height: Height, date: Date) -> bool {
|
||||
self.as_vec()
|
||||
.iter()
|
||||
.any(|(dataset, _)| dataset.needs_insert_input(height, date))
|
||||
}
|
||||
|
||||
pub fn compute(
|
||||
&mut self,
|
||||
compute_data: &ComputeData,
|
||||
closes: &mut BiMap<f32>,
|
||||
circulating_supply: &mut BiMap<f64>,
|
||||
market_cap: &mut BiMap<f32>,
|
||||
) {
|
||||
self.cohorts
|
||||
.as_mut_vec()
|
||||
.into_iter()
|
||||
.for_each(|(cohort, _)| {
|
||||
cohort.compute(compute_data, closes, circulating_supply, market_cap)
|
||||
})
|
||||
}
|
||||
|
||||
fn as_vec(&self) -> Vec<(&UTXODataset, UTXOCohortId)> {
|
||||
self.cohorts.as_vec()
|
||||
}
|
||||
|
||||
fn as_mut_vec(&mut self) -> Vec<(&mut UTXODataset, UTXOCohortId)> {
|
||||
self.cohorts.as_mut_vec()
|
||||
}
|
||||
}
|
||||
|
||||
impl AnyDatasets for UTXODatasets {
|
||||
fn get_min_initial_states(&self) -> &MinInitialStates {
|
||||
&self.min_initial_states
|
||||
}
|
||||
|
||||
fn to_any_dataset_vec(&self) -> Vec<&(dyn AnyDataset + Send + Sync)> {
|
||||
self.as_vec()
|
||||
.into_iter()
|
||||
.map(|(dataset, _)| dataset as &(dyn AnyDataset + Send + Sync))
|
||||
.collect_vec()
|
||||
}
|
||||
|
||||
fn to_mut_any_dataset_vec(&mut self) -> Vec<&mut dyn AnyDataset> {
|
||||
self.as_mut_vec()
|
||||
.into_iter()
|
||||
.map(|(dataset, _)| dataset as &mut dyn AnyDataset)
|
||||
.collect_vec()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user