mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-23 17:08:10 -07:00
computer: store part 8
This commit is contained in:
Generated
+1
@@ -1020,6 +1020,7 @@ dependencies = [
|
|||||||
"brk_core",
|
"brk_core",
|
||||||
"byteview",
|
"byteview",
|
||||||
"fjall",
|
"fjall",
|
||||||
|
"log",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -20,31 +20,80 @@ impl AddressCohortState {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn height(&self) -> Option<Height> {
|
||||||
|
self.inner.height()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset_price_to_amount(&mut self) -> Result<()> {
|
||||||
|
self.inner.reset_price_to_amount()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reset_single_iteration_values(&mut self) {
|
pub fn reset_single_iteration_values(&mut self) {
|
||||||
self.inner.reset_single_iteration_values();
|
self.inner.reset_single_iteration_values();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn send(
|
pub fn send(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
addressdata: &mut AddressData,
|
||||||
value: Sats,
|
value: Sats,
|
||||||
current_price: Option<Dollars>,
|
current_price: Option<Dollars>,
|
||||||
prev_price: Option<Dollars>,
|
prev_price: Option<Dollars>,
|
||||||
blocks_old: usize,
|
blocks_old: usize,
|
||||||
days_old: f64,
|
days_old: f64,
|
||||||
older_than_hour: bool,
|
older_than_hour: bool,
|
||||||
) {
|
) -> Result<()> {
|
||||||
self.inner.send(
|
let compute_price = current_price.is_some();
|
||||||
|
|
||||||
|
let prev_realized_price = compute_price.then(|| addressdata.realized_price());
|
||||||
|
let prev_supply_state = SupplyState {
|
||||||
|
utxos: addressdata.outputs_len as usize,
|
||||||
|
value: addressdata.amount(),
|
||||||
|
};
|
||||||
|
|
||||||
|
addressdata.send(value, prev_price)?;
|
||||||
|
|
||||||
|
let supply_state = SupplyState {
|
||||||
|
utxos: addressdata.outputs_len as usize,
|
||||||
|
value: addressdata.amount(),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.inner.send_(
|
||||||
&SupplyState { utxos: 1, value },
|
&SupplyState { utxos: 1, value },
|
||||||
current_price,
|
current_price,
|
||||||
prev_price,
|
prev_price,
|
||||||
blocks_old,
|
blocks_old,
|
||||||
days_old,
|
days_old,
|
||||||
older_than_hour,
|
older_than_hour,
|
||||||
|
compute_price.then(|| (addressdata.realized_price(), &supply_state)),
|
||||||
|
prev_realized_price.map(|prev_price| (prev_price, &prev_supply_state)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn receive(&mut self, value: Sats, price: Option<Dollars>) {
|
pub fn receive(&mut self, address_data: &mut AddressData, value: Sats, price: Option<Dollars>) {
|
||||||
self.inner.receive(&SupplyState { utxos: 1, value }, price);
|
let compute_price = price.is_some();
|
||||||
|
|
||||||
|
let prev_realized_price = compute_price.then(|| address_data.realized_price());
|
||||||
|
let prev_supply_state = SupplyState {
|
||||||
|
utxos: address_data.outputs_len as usize,
|
||||||
|
value: address_data.amount(),
|
||||||
|
};
|
||||||
|
|
||||||
|
address_data.receive(value, price);
|
||||||
|
|
||||||
|
let supply_state = SupplyState {
|
||||||
|
utxos: address_data.outputs_len as usize,
|
||||||
|
value: address_data.amount(),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.inner.receive_(
|
||||||
|
&SupplyState { utxos: 1, value },
|
||||||
|
price,
|
||||||
|
compute_price.then(|| (address_data.realized_price(), &supply_state)),
|
||||||
|
prev_realized_price.map(|prev_price| (prev_price, &prev_supply_state)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, addressdata: &AddressData) {
|
pub fn add(&mut self, addressdata: &AddressData) {
|
||||||
@@ -54,7 +103,7 @@ impl AddressCohortState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn subtract(&mut self, addressdata: &AddressData) {
|
pub fn subtract(&mut self, addressdata: &AddressData) {
|
||||||
self.address_count.checked_sub(1).unwrap();
|
self.address_count = self.address_count.checked_sub(1).unwrap();
|
||||||
self.inner
|
self.inner
|
||||||
.decrement_(&addressdata.into(), addressdata.realized_cap);
|
.decrement_(&addressdata.into(), addressdata.realized_cap);
|
||||||
}
|
}
|
||||||
@@ -63,39 +112,3 @@ impl AddressCohortState {
|
|||||||
self.inner.commit(height)
|
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
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ pub struct CohortState {
|
|||||||
pub realized: Option<RealizedState>,
|
pub realized: Option<RealizedState>,
|
||||||
pub satblocks_destroyed: Sats,
|
pub satblocks_destroyed: Sats,
|
||||||
pub satdays_destroyed: Sats,
|
pub satdays_destroyed: Sats,
|
||||||
pub price_to_amount: PriceToAmount,
|
|
||||||
|
price_to_amount: PriceToAmount,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CohortState {
|
impl CohortState {
|
||||||
@@ -24,6 +25,22 @@ impl CohortState {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn height(&self) -> Option<Height> {
|
||||||
|
self.price_to_amount.height()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset_price_to_amount(&mut self) -> Result<()> {
|
||||||
|
self.price_to_amount.reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn price_to_amount_first_key_value(&self) -> Option<(&Dollars, &Sats)> {
|
||||||
|
self.price_to_amount.first_key_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn price_to_amount_last_key_value(&self) -> Option<(&Dollars, &Sats)> {
|
||||||
|
self.price_to_amount.last_key_value()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reset_single_iteration_values(&mut self) {
|
pub fn reset_single_iteration_values(&mut self) {
|
||||||
self.satdays_destroyed = Sats::ZERO;
|
self.satdays_destroyed = Sats::ZERO;
|
||||||
self.satblocks_destroyed = Sats::ZERO;
|
self.satblocks_destroyed = Sats::ZERO;
|
||||||
@@ -39,7 +56,7 @@ impl CohortState {
|
|||||||
if let Some(realized) = self.realized.as_mut() {
|
if let Some(realized) = self.realized.as_mut() {
|
||||||
let price = price.unwrap();
|
let price = price.unwrap();
|
||||||
realized.increment(supply_state, price);
|
realized.increment(supply_state, price);
|
||||||
*self.price_to_amount.entry(price).or_default() += supply_state.value;
|
self.price_to_amount.increment(price, supply_state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,10 +67,10 @@ impl CohortState {
|
|||||||
if supply_state.value > Sats::ZERO {
|
if supply_state.value > Sats::ZERO {
|
||||||
if let Some(realized) = self.realized.as_mut() {
|
if let Some(realized) = self.realized.as_mut() {
|
||||||
realized.increment_(realized_cap);
|
realized.increment_(realized_cap);
|
||||||
*self
|
self.price_to_amount.increment(
|
||||||
.price_to_amount
|
realized_cap / Bitcoin::from(supply_state.value),
|
||||||
.entry(realized_cap / Bitcoin::from(supply_state.value))
|
supply_state,
|
||||||
.or_default() += supply_state.value;
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,7 +82,7 @@ impl CohortState {
|
|||||||
if let Some(realized) = self.realized.as_mut() {
|
if let Some(realized) = self.realized.as_mut() {
|
||||||
let price = price.unwrap();
|
let price = price.unwrap();
|
||||||
realized.decrement(supply_state, price);
|
realized.decrement(supply_state, price);
|
||||||
self.decrement_price_to_amount(supply_state, price);
|
self.price_to_amount.decrement(price, supply_state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,30 +93,47 @@ impl CohortState {
|
|||||||
if supply_state.value > Sats::ZERO {
|
if supply_state.value > Sats::ZERO {
|
||||||
if let Some(realized) = self.realized.as_mut() {
|
if let Some(realized) = self.realized.as_mut() {
|
||||||
realized.decrement_(realized_cap);
|
realized.decrement_(realized_cap);
|
||||||
self.decrement_price_to_amount(
|
self.price_to_amount.decrement(
|
||||||
|
(realized_cap / Bitcoin::from(supply_state.value)).round_nearest_cent(),
|
||||||
supply_state,
|
supply_state,
|
||||||
realized_cap / Bitcoin::from(supply_state.value),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decrement_price_to_amount(&mut self, supply_state: &SupplyState, price: Dollars) {
|
pub fn receive(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
|
||||||
let amount = self.price_to_amount.get_mut(&price).unwrap();
|
self.receive_(
|
||||||
*amount -= supply_state.value;
|
supply_state,
|
||||||
if *amount == Sats::ZERO {
|
price,
|
||||||
self.price_to_amount.remove(&price);
|
price.map(|price| (price, supply_state)),
|
||||||
}
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn receive(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
|
pub fn receive_(
|
||||||
|
&mut self,
|
||||||
|
supply_state: &SupplyState,
|
||||||
|
price: Option<Dollars>,
|
||||||
|
price_to_amount_increment: Option<(Dollars, &SupplyState)>,
|
||||||
|
price_to_amount_decrement: Option<(Dollars, &SupplyState)>,
|
||||||
|
) {
|
||||||
self.supply += supply_state;
|
self.supply += supply_state;
|
||||||
|
|
||||||
if supply_state.value > Sats::ZERO {
|
if supply_state.value > Sats::ZERO {
|
||||||
if let Some(realized) = self.realized.as_mut() {
|
if let Some(realized) = self.realized.as_mut() {
|
||||||
let price = price.unwrap();
|
let price = price.unwrap();
|
||||||
realized.receive(supply_state, price);
|
realized.receive(supply_state, price);
|
||||||
*self.price_to_amount.entry(price).or_default() += supply_state.value;
|
|
||||||
|
if let Some((price, supply)) = price_to_amount_increment
|
||||||
|
&& supply.value.is_not_zero()
|
||||||
|
{
|
||||||
|
self.price_to_amount.increment(price, supply);
|
||||||
|
}
|
||||||
|
if let Some((price, supply)) = price_to_amount_decrement
|
||||||
|
&& supply.value.is_not_zero()
|
||||||
|
{
|
||||||
|
self.price_to_amount.decrement(price, supply);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -112,6 +146,30 @@ impl CohortState {
|
|||||||
blocks_old: usize,
|
blocks_old: usize,
|
||||||
days_old: f64,
|
days_old: f64,
|
||||||
older_than_hour: bool,
|
older_than_hour: bool,
|
||||||
|
) {
|
||||||
|
self.send_(
|
||||||
|
supply_state,
|
||||||
|
current_price,
|
||||||
|
prev_price,
|
||||||
|
blocks_old,
|
||||||
|
days_old,
|
||||||
|
older_than_hour,
|
||||||
|
None,
|
||||||
|
prev_price.map(|prev_price| (prev_price, supply_state)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
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,
|
||||||
|
price_to_amount_increment: Option<(Dollars, &SupplyState)>,
|
||||||
|
price_to_amount_decrement: Option<(Dollars, &SupplyState)>,
|
||||||
) {
|
) {
|
||||||
if supply_state.utxos == 0 {
|
if supply_state.utxos == 0 {
|
||||||
return;
|
return;
|
||||||
@@ -129,7 +187,16 @@ impl CohortState {
|
|||||||
let current_price = current_price.unwrap();
|
let current_price = current_price.unwrap();
|
||||||
let prev_price = prev_price.unwrap();
|
let prev_price = prev_price.unwrap();
|
||||||
realized.send(supply_state, current_price, prev_price, older_than_hour);
|
realized.send(supply_state, current_price, prev_price, older_than_hour);
|
||||||
self.decrement_price_to_amount(supply_state, prev_price);
|
if let Some((price, supply)) = price_to_amount_increment
|
||||||
|
&& supply.value.is_not_zero()
|
||||||
|
{
|
||||||
|
self.price_to_amount.increment(price, supply);
|
||||||
|
}
|
||||||
|
if let Some((price, supply)) = price_to_amount_decrement
|
||||||
|
&& supply.value.is_not_zero()
|
||||||
|
{
|
||||||
|
self.price_to_amount.decrement(price, supply);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use brk_core::Result;
|
use brk_core::{Height, Result};
|
||||||
use derive_deref::{Deref, DerefMut};
|
use derive_deref::{Deref, DerefMut};
|
||||||
|
|
||||||
use super::CohortState;
|
use super::CohortState;
|
||||||
@@ -16,4 +16,12 @@ impl UTXOCohortState {
|
|||||||
compute_dollars,
|
compute_dollars,
|
||||||
)?))
|
)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn height(&self) -> Option<Height> {
|
||||||
|
self.0.height()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset_price_to_amount(&mut self) -> Result<()> {
|
||||||
|
self.0.reset_price_to_amount()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ use std::{
|
|||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::{BufReader, BufWriter},
|
io::{BufReader, BufWriter},
|
||||||
ops::{Deref, DerefMut},
|
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -11,6 +10,8 @@ use brk_core::{Dollars, Height, Result, Sats};
|
|||||||
use derive_deref::{Deref, DerefMut};
|
use derive_deref::{Deref, DerefMut};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::states::SupplyState;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct PriceToAmount {
|
pub struct PriceToAmount {
|
||||||
pathbuf: PathBuf,
|
pathbuf: PathBuf,
|
||||||
@@ -46,8 +47,36 @@ impl PriceToAmount {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (&Dollars, &Sats)> {
|
||||||
|
self.state.iter()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.state.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn first_key_value(&self) -> Option<(&Dollars, &Sats)> {
|
||||||
|
self.state.first_key_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last_key_value(&self) -> Option<(&Dollars, &Sats)> {
|
||||||
|
self.state.last_key_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn increment(&mut self, price: Dollars, supply_state: &SupplyState) {
|
||||||
|
*self.state.entry(price).or_default() += supply_state.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decrement(&mut self, price: Dollars, supply_state: &SupplyState) {
|
||||||
|
let amount = self.state.get_mut(&price).unwrap();
|
||||||
|
*amount -= supply_state.value;
|
||||||
|
if *amount == Sats::ZERO {
|
||||||
|
self.state.remove(&price);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self) -> Result<()> {
|
pub fn reset(&mut self) -> Result<()> {
|
||||||
self.clear();
|
self.state.clear();
|
||||||
self.height = None;
|
self.height = None;
|
||||||
fs::remove_dir_all(&self.pathbuf)?;
|
fs::remove_dir_all(&self.pathbuf)?;
|
||||||
fs::create_dir_all(&self.pathbuf)?;
|
fs::create_dir_all(&self.pathbuf)?;
|
||||||
@@ -89,16 +118,3 @@ impl PriceToAmount {
|
|||||||
path.join("height")
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use brk_core::{
|
|||||||
};
|
};
|
||||||
use brk_store::{AnyStore, Store};
|
use brk_store::{AnyStore, Store};
|
||||||
use fjall::{PersistMode, TransactionalKeyspace};
|
use fjall::{PersistMode, TransactionalKeyspace};
|
||||||
use rayon::prelude::*;
|
use log::info;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
GroupedByAddressType,
|
GroupedByAddressType,
|
||||||
@@ -484,10 +484,15 @@ impl Stores {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self) -> Result<()> {
|
pub fn reset(&mut self) -> Result<()> {
|
||||||
|
info!("Resetting stores...");
|
||||||
|
info!("> If it gets stuck here, stop the program and start it again");
|
||||||
|
|
||||||
self.as_mut_slice()
|
self.as_mut_slice()
|
||||||
.into_par_iter()
|
.into_iter()
|
||||||
.try_for_each(|store| store.reset())?;
|
.try_for_each(|store| store.reset())?;
|
||||||
|
|
||||||
|
info!("Persisting stores...");
|
||||||
|
|
||||||
self.keyspace
|
self.keyspace
|
||||||
.persist(PersistMode::SyncAll)
|
.persist(PersistMode::SyncAll)
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
|
|||||||
@@ -71,11 +71,7 @@ impl CohortVecs for Vecs {
|
|||||||
|
|
||||||
fn starting_height(&self) -> Height {
|
fn starting_height(&self) -> Height {
|
||||||
[
|
[
|
||||||
self.state
|
self.state.height().map_or(Height::MAX, |h| h.incremented()),
|
||||||
.inner
|
|
||||||
.price_to_amount
|
|
||||||
.height()
|
|
||||||
.map_or(Height::MAX, |h| h.incremented()),
|
|
||||||
self.height_to_address_count.len().into(),
|
self.height_to_address_count.len().into(),
|
||||||
self.inner.starting_height(),
|
self.inner.starting_height(),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use brk_core::TypeIndex;
|
use brk_core::{AddressData, Dollars, OutputIndex, Sats, TypeIndex};
|
||||||
use derive_deref::{Deref, DerefMut};
|
use derive_deref::{Deref, DerefMut};
|
||||||
|
|
||||||
|
use crate::vecs::stateful::WithAddressDataSource;
|
||||||
|
|
||||||
use super::GroupedByAddressType;
|
use super::GroupedByAddressType;
|
||||||
|
|
||||||
#[derive(Debug, Default, Deref, DerefMut)]
|
#[derive(Debug, Default, Deref, DerefMut)]
|
||||||
@@ -33,3 +35,86 @@ impl<T> AddressTypeToTypeIndexVec<T> {
|
|||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AddressTypeToTypeIndexVec<OutputIndex> {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
pub fn extend_from_sent(
|
||||||
|
&mut self,
|
||||||
|
other: &AddressTypeToTypeIndexVec<(
|
||||||
|
OutputIndex,
|
||||||
|
Sats,
|
||||||
|
Option<WithAddressDataSource<AddressData>>,
|
||||||
|
Option<Dollars>,
|
||||||
|
usize,
|
||||||
|
f64,
|
||||||
|
bool,
|
||||||
|
)>,
|
||||||
|
) {
|
||||||
|
Self::extend_from_sent_(&mut self.p2pk33, &other.p2pk33);
|
||||||
|
Self::extend_from_sent_(&mut self.p2pkh, &other.p2pkh);
|
||||||
|
Self::extend_from_sent_(&mut self.p2sh, &other.p2sh);
|
||||||
|
Self::extend_from_sent_(&mut self.p2wpkh, &other.p2wpkh);
|
||||||
|
Self::extend_from_sent_(&mut self.p2wsh, &other.p2wsh);
|
||||||
|
Self::extend_from_sent_(&mut self.p2tr, &other.p2tr);
|
||||||
|
Self::extend_from_sent_(&mut self.p2a, &other.p2a);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
fn extend_from_sent_(
|
||||||
|
own: &mut Vec<(TypeIndex, OutputIndex)>,
|
||||||
|
other: &[(
|
||||||
|
TypeIndex,
|
||||||
|
(
|
||||||
|
OutputIndex,
|
||||||
|
Sats,
|
||||||
|
Option<WithAddressDataSource<AddressData>>,
|
||||||
|
Option<Dollars>,
|
||||||
|
usize,
|
||||||
|
f64,
|
||||||
|
bool,
|
||||||
|
),
|
||||||
|
)],
|
||||||
|
) {
|
||||||
|
own.extend(
|
||||||
|
other
|
||||||
|
.iter()
|
||||||
|
.map(|(type_index, (output_index, ..))| (*type_index, *output_index)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extend_from_received(
|
||||||
|
&mut self,
|
||||||
|
other: &AddressTypeToTypeIndexVec<(
|
||||||
|
OutputIndex,
|
||||||
|
Sats,
|
||||||
|
Option<WithAddressDataSource<AddressData>>,
|
||||||
|
)>,
|
||||||
|
) {
|
||||||
|
Self::extend_from_received_(&mut self.p2pk33, &other.p2pk33);
|
||||||
|
Self::extend_from_received_(&mut self.p2pkh, &other.p2pkh);
|
||||||
|
Self::extend_from_received_(&mut self.p2sh, &other.p2sh);
|
||||||
|
Self::extend_from_received_(&mut self.p2wpkh, &other.p2wpkh);
|
||||||
|
Self::extend_from_received_(&mut self.p2wsh, &other.p2wsh);
|
||||||
|
Self::extend_from_received_(&mut self.p2tr, &other.p2tr);
|
||||||
|
Self::extend_from_received_(&mut self.p2a, &other.p2a);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
fn extend_from_received_(
|
||||||
|
own: &mut Vec<(TypeIndex, OutputIndex)>,
|
||||||
|
other: &[(
|
||||||
|
TypeIndex,
|
||||||
|
(
|
||||||
|
OutputIndex,
|
||||||
|
Sats,
|
||||||
|
Option<WithAddressDataSource<AddressData>>,
|
||||||
|
),
|
||||||
|
)],
|
||||||
|
) {
|
||||||
|
own.extend(
|
||||||
|
other
|
||||||
|
.iter()
|
||||||
|
.map(|(type_index, (output_index, ..))| (*type_index, *output_index)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1341,8 +1341,7 @@ impl Vecs {
|
|||||||
.forced_push_at(
|
.forced_push_at(
|
||||||
height,
|
height,
|
||||||
state
|
state
|
||||||
.price_to_amount
|
.price_to_amount_first_key_value()
|
||||||
.first_key_value()
|
|
||||||
.map(|(&dollars, _)| dollars)
|
.map(|(&dollars, _)| dollars)
|
||||||
.unwrap_or(Dollars::NAN),
|
.unwrap_or(Dollars::NAN),
|
||||||
exit,
|
exit,
|
||||||
@@ -1353,8 +1352,7 @@ impl Vecs {
|
|||||||
.forced_push_at(
|
.forced_push_at(
|
||||||
height,
|
height,
|
||||||
state
|
state
|
||||||
.price_to_amount
|
.price_to_amount_last_key_value()
|
||||||
.last_key_value()
|
|
||||||
.map(|(&dollars, _)| dollars)
|
.map(|(&dollars, _)| dollars)
|
||||||
.unwrap_or(Dollars::NAN),
|
.unwrap_or(Dollars::NAN),
|
||||||
exit,
|
exit,
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ pub use addresstype_to_typeindex_vec::*;
|
|||||||
use r#trait::CohortVecs;
|
use r#trait::CohortVecs;
|
||||||
pub use withaddressdatasource::WithAddressDataSource;
|
pub use withaddressdatasource::WithAddressDataSource;
|
||||||
|
|
||||||
const VERSION: Version = Version::new(5);
|
const VERSION: Version = Version::new(9);
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Vecs {
|
pub struct Vecs {
|
||||||
@@ -401,13 +401,17 @@ impl Vecs {
|
|||||||
|
|
||||||
stores.reset()?;
|
stores.reset()?;
|
||||||
|
|
||||||
|
info!("Resetting utxo price maps...");
|
||||||
|
|
||||||
separate_utxo_vecs
|
separate_utxo_vecs
|
||||||
.par_iter_mut()
|
.par_iter_mut()
|
||||||
.try_for_each(|(_, v)| v.state.price_to_amount.reset())?;
|
.try_for_each(|(_, v)| v.state.reset_price_to_amount())?;
|
||||||
|
|
||||||
|
info!("Resetting address price maps...");
|
||||||
|
|
||||||
separate_address_vecs
|
separate_address_vecs
|
||||||
.par_iter_mut()
|
.par_iter_mut()
|
||||||
.try_for_each(|(_, v)| v.state.inner.price_to_amount.reset())?;
|
.try_for_each(|(_, v)| v.state.reset_price_to_amount())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if starting_height < Height::from(height_to_date_fixed.len()) {
|
if starting_height < Height::from(height_to_date_fixed.len()) {
|
||||||
@@ -485,11 +489,13 @@ impl Vecs {
|
|||||||
let output_count = height_to_output_count_iter.unwrap_get_inner(height);
|
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 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_data), (mut received, new_addresstype_to_typedindex_to_received_outputindex, addresstype_to_typedindex_to_received_data)) = thread::scope(|s| {
|
let (
|
||||||
|
(mut height_to_sent, addresstype_to_typedindex_to_sent_outputindex, addresstype_to_typedindex_to_sent_data),
|
||||||
|
(mut received, addresstype_to_typedindex_to_received_outputindex, addresstype_to_typedindex_to_received_data),
|
||||||
|
) = thread::scope(|s| {
|
||||||
if chain_state_starting_height <= height {
|
if chain_state_starting_height <= height {
|
||||||
s.spawn(|| {
|
s.spawn(|| {
|
||||||
self.utxo_vecs
|
self.utxo_vecs.tick_tock_next_block(&chain_state, timestamp);
|
||||||
.tick_tock_next_block(&chain_state, timestamp);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -527,8 +533,17 @@ impl Vecs {
|
|||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
|
||||||
let addressdata_opt = if input_type.is_address() && !addresstype_to_typeindex_to_addressdata.get(input_type).unwrap().contains_key(&typeindex) {
|
let addressdata_opt = if input_type.is_address()
|
||||||
Some(WithAddressDataSource::FromAddressDataStore( stores.get_addressdata(input_type, typeindex).unwrap().unwrap()))
|
&& !addresstype_to_typeindex_to_addressdata
|
||||||
|
.get(input_type)
|
||||||
|
.unwrap()
|
||||||
|
.contains_key(&typeindex)
|
||||||
|
&& let Some(address_data) =
|
||||||
|
stores.get_addressdata(input_type, typeindex).unwrap()
|
||||||
|
{
|
||||||
|
Some(WithAddressDataSource::FromAddressDataStore(
|
||||||
|
address_data,
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -545,65 +560,126 @@ impl Vecs {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.into_owned();
|
.into_owned();
|
||||||
|
|
||||||
let prev_price = height_to_close.map(|m| *m.get_or_read(prev_height, height_to_close_mmap.as_ref().unwrap()).unwrap()
|
let prev_price = height_to_close.map(|m| {
|
||||||
.unwrap()
|
*m.get_or_read(
|
||||||
.into_owned());
|
prev_height,
|
||||||
|
height_to_close_mmap.as_ref().unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap()
|
||||||
|
.into_owned()
|
||||||
|
});
|
||||||
|
|
||||||
let prev_timestamp = height_to_timestamp_fixed.get_or_read(prev_height, &height_to_timestamp_fixed_mmap)
|
let prev_timestamp = height_to_timestamp_fixed
|
||||||
.unwrap()
|
.get_or_read(prev_height, &height_to_timestamp_fixed_mmap)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_owned();
|
.unwrap()
|
||||||
|
.into_owned();
|
||||||
|
|
||||||
let blocks_old = height.unwrap_to_usize() - prev_height.unwrap_to_usize();
|
let blocks_old =
|
||||||
|
height.unwrap_to_usize() - prev_height.unwrap_to_usize();
|
||||||
|
|
||||||
let days_old = prev_timestamp
|
let days_old =
|
||||||
.difference_in_days_between_float(timestamp);
|
prev_timestamp.difference_in_days_between_float(timestamp);
|
||||||
|
|
||||||
let older_than_hour = timestamp
|
let older_than_hour = timestamp
|
||||||
.checked_sub(prev_timestamp)
|
.checked_sub(prev_timestamp)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.is_more_than_hour();
|
.is_more_than_hour();
|
||||||
|
|
||||||
(prev_height, value, input_type, typeindex, outputindex, addressdata_opt, prev_price, blocks_old, days_old, older_than_hour)
|
(
|
||||||
|
prev_height,
|
||||||
|
value,
|
||||||
|
input_type,
|
||||||
|
typeindex,
|
||||||
|
outputindex,
|
||||||
|
addressdata_opt,
|
||||||
|
prev_price,
|
||||||
|
blocks_old,
|
||||||
|
days_old,
|
||||||
|
older_than_hour,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.fold(
|
.fold(
|
||||||
|| {
|
|| {
|
||||||
(
|
(
|
||||||
BTreeMap::<Height, Transacted>::default(),
|
BTreeMap::<Height, Transacted>::default(),
|
||||||
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
||||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>, Option<Dollars>, usize, f64, bool)>::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, prev_price, blocks_old, days_old, older_than_hour)| {
|
|(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);
|
tree.entry(height).or_default().iterate(value, input_type);
|
||||||
if let Some(vec) = vecs.get_mut(input_type) {
|
if let Some(vec) = vecs.get_mut(input_type) {
|
||||||
vec.push((typeindex, outputindex));
|
vec.push((typeindex, outputindex));
|
||||||
}
|
}
|
||||||
if let Some(vec) = vecs2.get_mut(input_type) {
|
if let Some(vec) = vecs2.get_mut(input_type) {
|
||||||
vec.push((typeindex, (value, addressdata_opt, prev_price, blocks_old, days_old, older_than_hour)));
|
vec.push((
|
||||||
|
typeindex,
|
||||||
|
(
|
||||||
|
value,
|
||||||
|
addressdata_opt,
|
||||||
|
prev_price,
|
||||||
|
blocks_old,
|
||||||
|
days_old,
|
||||||
|
older_than_hour,
|
||||||
|
),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
(tree, vecs, vecs2)
|
(tree, vecs, vecs2)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.reduce( || {
|
.reduce(
|
||||||
(
|
|| {
|
||||||
BTreeMap::<Height, Transacted>::default(),
|
(
|
||||||
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
BTreeMap::<Height, Transacted>::default(),
|
||||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>, Option<Dollars>, usize, f64, bool)>::default(),
|
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
||||||
)
|
AddressTypeToTypeIndexVec::<(
|
||||||
}, |(first_tree, mut source_vecs,mut source_vecs2), (second_tree, other_vecs, other_vecs2)| {
|
Sats,
|
||||||
let (mut tree_source, tree_to_consume) = if first_tree.len() > second_tree.len() {
|
Option<WithAddressDataSource<AddressData>>,
|
||||||
(first_tree, second_tree)
|
Option<Dollars>,
|
||||||
} else {
|
usize,
|
||||||
(second_tree, first_tree)
|
f64,
|
||||||
};
|
bool,
|
||||||
tree_to_consume.into_iter().for_each(|(k, v)| {
|
)>::default(
|
||||||
*tree_source.entry(k).or_default() += v;
|
),
|
||||||
});
|
)
|
||||||
source_vecs.merge(other_vecs);
|
},
|
||||||
source_vecs2.merge(other_vecs2);
|
|(first_tree, mut source_vecs, mut source_vecs2), (second_tree, other_vecs, other_vecs2)| {
|
||||||
(tree_source, source_vecs, source_vecs2)
|
let (mut tree_source, tree_to_consume) =
|
||||||
})
|
if first_tree.len() > second_tree.len() {
|
||||||
|
(first_tree, second_tree)
|
||||||
|
} else {
|
||||||
|
(second_tree, first_tree)
|
||||||
|
};
|
||||||
|
tree_to_consume.into_iter().for_each(|(k, v)| {
|
||||||
|
*tree_source.entry(k).or_default() += v;
|
||||||
|
});
|
||||||
|
source_vecs.merge(other_vecs);
|
||||||
|
source_vecs2.merge(other_vecs2);
|
||||||
|
(tree_source, source_vecs, source_vecs2)
|
||||||
|
},
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let received_handle = s.spawn(|| {
|
let received_handle = s.spawn(|| {
|
||||||
@@ -629,14 +705,35 @@ impl Vecs {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.into_owned();
|
.into_owned();
|
||||||
|
|
||||||
let addressdata_opt = if output_type.is_address() && !addresstype_to_typeindex_to_addressdata.get(output_type).unwrap().contains_key(&typeindex) && !addresstype_to_typeindex_to_emptyaddressdata.get(output_type).unwrap().contains_key(&typeindex) {
|
let addressdata_opt = if output_type.is_address()
|
||||||
Some(if let Some(addressdata) = stores.get_addressdata(output_type, typeindex).unwrap() {
|
&& !addresstype_to_typeindex_to_addressdata
|
||||||
WithAddressDataSource::FromAddressDataStore(addressdata)
|
.get(output_type)
|
||||||
} else if let Some(emptyaddressdata) = stores.get_emptyaddressdata(output_type, typeindex).unwrap() {
|
.unwrap()
|
||||||
WithAddressDataSource::FromEmptyAddressDataStore(emptyaddressdata.into())
|
.contains_key(&typeindex)
|
||||||
} else {
|
&& !addresstype_to_typeindex_to_emptyaddressdata
|
||||||
WithAddressDataSource::New(AddressData::default())
|
.get(output_type)
|
||||||
})
|
.unwrap()
|
||||||
|
.contains_key(&typeindex)
|
||||||
|
{
|
||||||
|
Some(
|
||||||
|
if let Some(addressdata) = stores
|
||||||
|
.get_addressdata(output_type, typeindex)
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
|
WithAddressDataSource::FromAddressDataStore(
|
||||||
|
addressdata,
|
||||||
|
)
|
||||||
|
} else if let Some(emptyaddressdata) = stores
|
||||||
|
.get_emptyaddressdata(output_type, typeindex)
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
|
WithAddressDataSource::FromEmptyAddressDataStore(
|
||||||
|
emptyaddressdata.into(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
WithAddressDataSource::New(AddressData::default())
|
||||||
|
},
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -644,52 +741,68 @@ impl Vecs {
|
|||||||
(value, output_type, typeindex, outputindex, addressdata_opt)
|
(value, output_type, typeindex, outputindex, addressdata_opt)
|
||||||
})
|
})
|
||||||
.fold(
|
.fold(
|
||||||
|| (Transacted::default(), AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
|| {
|
||||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>)>::default(),
|
(
|
||||||
),
|
Transacted::default(),
|
||||||
|(mut transacted, mut vecs, mut vecs2), (value, output_type, typeindex, outputindex, addressdata_opt)| {
|
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
||||||
|
AddressTypeToTypeIndexVec::<(
|
||||||
|
Sats,
|
||||||
|
Option<WithAddressDataSource<AddressData>>,
|
||||||
|
)>::default(
|
||||||
|
),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|(mut transacted, mut vecs, mut vecs2),
|
||||||
|
(
|
||||||
|
value,
|
||||||
|
output_type,
|
||||||
|
typeindex,
|
||||||
|
outputindex,
|
||||||
|
addressdata_opt,
|
||||||
|
)| {
|
||||||
transacted.iterate(value, output_type);
|
transacted.iterate(value, output_type);
|
||||||
if let Some(vec) = vecs.get_mut(output_type) {
|
if let Some(vec) = vecs.get_mut(output_type) {
|
||||||
vec.push((typeindex, outputindex));
|
vec.push((
|
||||||
|
typeindex,
|
||||||
|
outputindex,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
if let Some(vec) = vecs2.get_mut(output_type) {
|
if let Some(vec) = vecs2.get_mut(output_type) {
|
||||||
vec.push((typeindex, (value, addressdata_opt)));
|
vec.push((
|
||||||
|
typeindex,
|
||||||
|
(value, addressdata_opt),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
(transacted, vecs, vecs2)
|
(transacted, vecs, vecs2)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.reduce(|| (Transacted::default(), AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
.reduce(
|
||||||
AddressTypeToTypeIndexVec::<(Sats, Option<WithAddressDataSource<AddressData>>)>::default(),
|
|| {
|
||||||
), |(transacted, mut vecs, mut vecs2), (other_transacted, other_vecs, other_vecs2)| {
|
(
|
||||||
vecs.merge(other_vecs);
|
Transacted::default(),
|
||||||
vecs2.merge(other_vecs2);
|
AddressTypeToTypeIndexVec::<OutputIndex>::default(),
|
||||||
(transacted + other_transacted, vecs, vecs2)
|
AddressTypeToTypeIndexVec::<(
|
||||||
})
|
Sats,
|
||||||
|
Option<WithAddressDataSource<AddressData>>,
|
||||||
|
)>::default(
|
||||||
|
),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|(transacted, mut vecs, mut vecs2), (other_transacted, other_vecs, other_vecs2)| {
|
||||||
|
vecs.merge(other_vecs);
|
||||||
|
vecs2.merge(other_vecs2);
|
||||||
|
(transacted + other_transacted, vecs, vecs2)
|
||||||
|
},
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
(sent_handle.join().unwrap(), received_handle.join().unwrap())
|
(sent_handle.join().unwrap(), received_handle.join().unwrap())
|
||||||
});
|
});
|
||||||
|
|
||||||
addresstype_to_typeindex_to_sent_outputindex.merge(new_addresstype_to_typedindex_to_sent_outputindex);
|
if chain_state_starting_height > height {
|
||||||
addresstype_to_typeindex_to_received_outputindex.merge(new_addresstype_to_typedindex_to_received_outputindex);
|
dbg!(chain_state_starting_height, height);
|
||||||
|
panic!("temp, just making sure")
|
||||||
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,
|
|
||||||
&mut addresstype_to_address_count,
|
|
||||||
&mut addresstype_to_empty_address_count
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
|
||||||
&mut addresstype_to_address_count,
|
|
||||||
&mut addresstype_to_empty_address_count
|
|
||||||
)?;
|
|
||||||
|
|
||||||
unspendable_supply += received
|
unspendable_supply += received
|
||||||
.by_type
|
.by_type
|
||||||
@@ -715,28 +828,53 @@ impl Vecs {
|
|||||||
.iterate(Sats::FIFTY_BTC, OutputType::P2PK65);
|
.iterate(Sats::FIFTY_BTC, OutputType::P2PK65);
|
||||||
};
|
};
|
||||||
|
|
||||||
if chain_state_starting_height <= height {
|
thread::scope(|scope| -> Result<()> {
|
||||||
// Push current block state before processing sends and receives
|
scope.spawn(|| addresstype_to_typeindex_to_sent_outputindex
|
||||||
chain_state.push(BlockState {
|
.merge(addresstype_to_typedindex_to_sent_outputindex));
|
||||||
supply: received.spendable_supply.clone(),
|
|
||||||
|
scope.spawn(|| addresstype_to_typeindex_to_received_outputindex
|
||||||
|
.merge(addresstype_to_typedindex_to_received_outputindex));
|
||||||
|
|
||||||
|
scope.spawn(|| {
|
||||||
|
// Push current block state before processing sends and receives
|
||||||
|
chain_state.push(BlockState {
|
||||||
|
supply: received.spendable_supply.clone(),
|
||||||
|
price,
|
||||||
|
timestamp,
|
||||||
|
});
|
||||||
|
|
||||||
|
self.utxo_vecs.receive(received, height, price);
|
||||||
|
|
||||||
|
let unsafe_chain_state = UnsafeSlice::new(&mut chain_state);
|
||||||
|
|
||||||
|
height_to_sent.par_iter().for_each(|(height, sent)| unsafe {
|
||||||
|
(*unsafe_chain_state.get(height.unwrap_to_usize())).supply -=
|
||||||
|
&sent.spendable_supply;
|
||||||
|
});
|
||||||
|
|
||||||
|
self.utxo_vecs.send(height_to_sent, chain_state.as_slice());
|
||||||
|
});
|
||||||
|
|
||||||
|
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,
|
price,
|
||||||
timestamp,
|
&mut addresstype_to_address_count,
|
||||||
});
|
&mut addresstype_to_empty_address_count,
|
||||||
|
);
|
||||||
|
|
||||||
self.utxo_vecs.receive(received, height, price);
|
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,
|
||||||
|
&mut addresstype_to_address_count,
|
||||||
|
&mut addresstype_to_empty_address_count,
|
||||||
|
)?;
|
||||||
|
|
||||||
let unsafe_chain_state = UnsafeSlice::new(&mut chain_state);
|
Ok(())
|
||||||
|
})?;
|
||||||
height_to_sent.par_iter().for_each(|(height, sent)| unsafe {
|
|
||||||
(*unsafe_chain_state.get(height.unwrap_to_usize())).supply -=
|
|
||||||
&sent.spendable_supply;
|
|
||||||
});
|
|
||||||
|
|
||||||
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.utxo_vecs.as_mut_separate_vecs();
|
let mut separate_utxo_vecs = self.utxo_vecs.as_mut_separate_vecs();
|
||||||
|
|
||||||
@@ -759,9 +897,14 @@ impl Vecs {
|
|||||||
self.height_to_opreturn_supply
|
self.height_to_opreturn_supply
|
||||||
.forced_push_at(height, opreturn_supply, exit)?;
|
.forced_push_at(height, opreturn_supply, exit)?;
|
||||||
|
|
||||||
self.addresstype_to_height_to_address_count.forced_push_at(height, &addresstype_to_address_count, exit)?;
|
self.addresstype_to_height_to_address_count.forced_push_at(
|
||||||
|
height,
|
||||||
|
&addresstype_to_address_count,
|
||||||
|
exit,
|
||||||
|
)?;
|
||||||
|
|
||||||
self.addresstype_to_height_to_empty_address_count.forced_push_at(height, &addresstype_to_empty_address_count, exit)?;
|
self.addresstype_to_height_to_empty_address_count
|
||||||
|
.forced_push_at(height, &addresstype_to_empty_address_count, exit)?;
|
||||||
|
|
||||||
let date = height_to_date_fixed_iter.unwrap_get_inner(height);
|
let date = height_to_date_fixed_iter.unwrap_get_inner(height);
|
||||||
let dateindex = DateIndex::try_from(date).unwrap();
|
let dateindex = DateIndex::try_from(date).unwrap();
|
||||||
@@ -776,45 +919,47 @@ impl Vecs {
|
|||||||
.as_mut()
|
.as_mut()
|
||||||
.map(|v| is_date_last_height.then(|| *v.unwrap_get_inner(dateindex)));
|
.map(|v| is_date_last_height.then(|| *v.unwrap_get_inner(dateindex)));
|
||||||
|
|
||||||
// thread::scope(|scope| {
|
thread::scope(|scope| {
|
||||||
// scope.spawn(|| {
|
scope.spawn(|| {
|
||||||
separate_utxo_vecs.par_iter_mut().try_for_each(|(_, v)| {
|
separate_utxo_vecs
|
||||||
v.compute_then_force_push_unrealized_states(
|
.par_iter_mut()
|
||||||
height,
|
.try_for_each(|(_, v)| {
|
||||||
price,
|
v.compute_then_force_push_unrealized_states(
|
||||||
is_date_last_height.then_some(dateindex),
|
height,
|
||||||
date_price,
|
price,
|
||||||
exit,
|
is_date_last_height.then_some(dateindex),
|
||||||
)
|
date_price,
|
||||||
})?;
|
exit,
|
||||||
// });
|
)
|
||||||
// scope.spawn(|| {
|
})
|
||||||
separate_address_vecs.par_iter_mut().try_for_each(|(_, v)| {
|
.unwrap();
|
||||||
v.compute_then_force_push_unrealized_states(
|
});
|
||||||
height,
|
scope.spawn(|| {
|
||||||
price,
|
separate_address_vecs
|
||||||
is_date_last_height.then_some(dateindex),
|
.par_iter_mut()
|
||||||
date_price,
|
.try_for_each(|(_, v)| {
|
||||||
exit,
|
v.compute_then_force_push_unrealized_states(
|
||||||
)
|
height,
|
||||||
})?;
|
price,
|
||||||
// });
|
is_date_last_height.then_some(dateindex),
|
||||||
// });
|
date_price,
|
||||||
|
exit,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if height != Height::ZERO && height.unwrap_to_usize() % 10_000 == 0 {
|
if height != Height::ZERO && height.unwrap_to_usize() % 10_000 == 0 {
|
||||||
info!("Flushing...");
|
info!("Flushing...");
|
||||||
exit.block();
|
exit.block();
|
||||||
self.flush_states(
|
self.flush_states(height, &chain_state, exit)?;
|
||||||
height,
|
|
||||||
&chain_state,
|
|
||||||
exit,
|
|
||||||
)?;
|
|
||||||
stores.commit(
|
stores.commit(
|
||||||
height,
|
height,
|
||||||
mem::take(&mut addresstype_to_typeindex_to_sent_outputindex),
|
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)
|
mem::take(&mut addresstype_to_typeindex_to_addressdata),
|
||||||
|
mem::take(&mut addresstype_to_typeindex_to_emptyaddressdata),
|
||||||
)?;
|
)?;
|
||||||
exit.release();
|
exit.release();
|
||||||
}
|
}
|
||||||
@@ -840,39 +985,41 @@ impl Vecs {
|
|||||||
|
|
||||||
info!("Computing overlapping...");
|
info!("Computing overlapping...");
|
||||||
|
|
||||||
// thread::scope(|scope| {
|
thread::scope(|scope| {
|
||||||
// scope.spawn(|| {
|
scope.spawn(|| {
|
||||||
self.utxo_vecs
|
self.utxo_vecs
|
||||||
.compute_overlapping_vecs(starting_indexes, exit)?;
|
.compute_overlapping_vecs(starting_indexes, exit)
|
||||||
// });
|
.unwrap();
|
||||||
// scope.spawn(|| {
|
});
|
||||||
self.address_vecs
|
scope.spawn(|| {
|
||||||
.compute_overlapping_vecs(starting_indexes, exit)?;
|
self.address_vecs
|
||||||
// });
|
.compute_overlapping_vecs(starting_indexes, exit)
|
||||||
// });
|
.unwrap();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
info!("Computing rest part 1...");
|
info!("Computing rest part 1...");
|
||||||
|
|
||||||
// thread::scope(|scope| {
|
thread::scope(|scope| {
|
||||||
// scope.spawn(|| {
|
scope.spawn(|| {
|
||||||
self.utxo_vecs
|
self.utxo_vecs
|
||||||
.as_mut_vecs()
|
.as_mut_vecs()
|
||||||
.par_iter_mut()
|
.par_iter_mut()
|
||||||
.try_for_each(|(_, v)| {
|
.try_for_each(|(_, v)| {
|
||||||
v.compute_rest_part1(indexer, indexes, fetched, starting_indexes, exit)
|
v.compute_rest_part1(indexer, indexes, fetched, starting_indexes, exit)
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// });
|
});
|
||||||
// scope.spawn(|| {
|
scope.spawn(|| {
|
||||||
self.address_vecs
|
self.address_vecs
|
||||||
.as_mut_vecs()
|
.as_mut_vecs()
|
||||||
.par_iter_mut()
|
.par_iter_mut()
|
||||||
.try_for_each(|(_, v)| {
|
.try_for_each(|(_, v)| {
|
||||||
v.compute_rest_part1(indexer, indexes, fetched, starting_indexes, exit)
|
v.compute_rest_part1(indexer, indexes, fetched, starting_indexes, exit)
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// });
|
});
|
||||||
// });
|
});
|
||||||
|
|
||||||
info!("Computing rest part 2...");
|
info!("Computing rest part 2...");
|
||||||
|
|
||||||
@@ -894,46 +1041,48 @@ impl Vecs {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|v| v.dateindex.unwrap_last().clone());
|
.map(|v| v.dateindex.unwrap_last().clone());
|
||||||
|
|
||||||
// thread::scope(|scope| {
|
thread::scope(|scope| {
|
||||||
// scope.spawn(|| {
|
scope.spawn(|| {
|
||||||
self.utxo_vecs
|
self.utxo_vecs
|
||||||
.as_mut_vecs()
|
.as_mut_vecs()
|
||||||
.par_iter_mut()
|
.par_iter_mut()
|
||||||
.try_for_each(|(_, v)| {
|
.try_for_each(|(_, v)| {
|
||||||
v.compute_rest_part2(
|
v.compute_rest_part2(
|
||||||
indexer,
|
indexer,
|
||||||
indexes,
|
indexes,
|
||||||
fetched,
|
fetched,
|
||||||
starting_indexes,
|
starting_indexes,
|
||||||
market,
|
market,
|
||||||
&height_to_supply,
|
&height_to_supply,
|
||||||
dateindex_to_supply.as_ref().unwrap(),
|
dateindex_to_supply.as_ref().unwrap(),
|
||||||
height_to_realized_cap.as_ref(),
|
height_to_realized_cap.as_ref(),
|
||||||
dateindex_to_realized_cap.as_ref(),
|
dateindex_to_realized_cap.as_ref(),
|
||||||
exit,
|
exit,
|
||||||
)
|
)
|
||||||
})?;
|
})
|
||||||
// });
|
.unwrap();
|
||||||
// scope.spawn(|| {
|
});
|
||||||
self.address_vecs
|
scope.spawn(|| {
|
||||||
.as_mut_vecs()
|
self.address_vecs
|
||||||
.par_iter_mut()
|
.as_mut_vecs()
|
||||||
.try_for_each(|(_, v)| {
|
.par_iter_mut()
|
||||||
v.compute_rest_part2(
|
.try_for_each(|(_, v)| {
|
||||||
indexer,
|
v.compute_rest_part2(
|
||||||
indexes,
|
indexer,
|
||||||
fetched,
|
indexes,
|
||||||
starting_indexes,
|
fetched,
|
||||||
market,
|
starting_indexes,
|
||||||
&height_to_supply,
|
market,
|
||||||
dateindex_to_supply.as_ref().unwrap(),
|
&height_to_supply,
|
||||||
height_to_realized_cap.as_ref(),
|
dateindex_to_supply.as_ref().unwrap(),
|
||||||
dateindex_to_realized_cap.as_ref(),
|
height_to_realized_cap.as_ref(),
|
||||||
exit,
|
dateindex_to_realized_cap.as_ref(),
|
||||||
)
|
exit,
|
||||||
})?;
|
)
|
||||||
// });
|
})
|
||||||
// });
|
.unwrap();
|
||||||
|
});
|
||||||
|
});
|
||||||
self.indexes_to_unspendable_supply.compute_rest(
|
self.indexes_to_unspendable_supply.compute_rest(
|
||||||
indexer,
|
indexer,
|
||||||
indexes,
|
indexes,
|
||||||
@@ -1084,28 +1233,27 @@ impl AddressTypeToTypeIndexVec<(Sats, Option<WithAddressDataSource<AddressData>>
|
|||||||
let amount = prev_amount + value;
|
let amount = prev_amount + value;
|
||||||
|
|
||||||
if is_new
|
if is_new
|
||||||
|
|| from_any_empty
|
||||||
|| vecs.by_size_range.get_mut(amount).0.clone()
|
|| 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).0.clone()
|
||||||
{
|
{
|
||||||
if !is_new {
|
// dbg!((prev_amount, amount, is_new));
|
||||||
vecs.by_size_range
|
|
||||||
.get_mut(prev_amount)
|
if !is_new && !from_any_empty {
|
||||||
.1
|
let state = &mut vecs.by_size_range.get_mut(prev_amount).1.state;
|
||||||
.state
|
// dbg!((prev_amount, &state.address_count, &addressdata));
|
||||||
.subtract(addressdata);
|
state.subtract(addressdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
addressdata.receive(value, price);
|
addressdata.receive(value, price);
|
||||||
|
|
||||||
vecs.by_size_range.get_mut(amount).1.state.add(addressdata);
|
vecs.by_size_range.get_mut(amount).1.state.add(addressdata);
|
||||||
} else {
|
} else {
|
||||||
addressdata.receive(value, price);
|
vecs.by_size_range.get_mut(amount).1.state.receive(
|
||||||
|
addressdata,
|
||||||
vecs.by_size_range
|
value,
|
||||||
.get_mut(amount)
|
price,
|
||||||
.1
|
);
|
||||||
.state
|
|
||||||
.receive(value, price);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1149,7 +1297,7 @@ impl
|
|||||||
|
|
||||||
let addressdata_withsource = typeindex_to_addressdata
|
let addressdata_withsource = typeindex_to_addressdata
|
||||||
.entry(type_index)
|
.entry(type_index)
|
||||||
.or_insert(addressdata_opt.unwrap());
|
.or_insert_with(|| addressdata_opt.unwrap());
|
||||||
|
|
||||||
let addressdata = addressdata_withsource.deref_mut();
|
let addressdata = addressdata_withsource.deref_mut();
|
||||||
|
|
||||||
@@ -1159,6 +1307,8 @@ impl
|
|||||||
|
|
||||||
let will_be_empty = addressdata.outputs_len - 1 == 0;
|
let will_be_empty = addressdata.outputs_len - 1 == 0;
|
||||||
|
|
||||||
|
// dbg!((prev_amount, amount, will_be_empty));
|
||||||
|
|
||||||
if will_be_empty
|
if will_be_empty
|
||||||
|| vecs.by_size_range.get_mut(amount).0.clone()
|
|| 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).0.clone()
|
||||||
@@ -1172,6 +1322,10 @@ impl
|
|||||||
addressdata.send(value, prev_price)?;
|
addressdata.send(value, prev_price)?;
|
||||||
|
|
||||||
if will_be_empty {
|
if will_be_empty {
|
||||||
|
if amount.is_not_zero() {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
|
||||||
(*addresstype_to_address_count.get_mut(_type).unwrap()) -= 1;
|
(*addresstype_to_address_count.get_mut(_type).unwrap()) -= 1;
|
||||||
(*addresstype_to_empty_address_count.get_mut(_type).unwrap()) += 1;
|
(*addresstype_to_empty_address_count.get_mut(_type).unwrap()) += 1;
|
||||||
|
|
||||||
@@ -1186,16 +1340,15 @@ impl
|
|||||||
vecs.by_size_range.get_mut(amount).1.state.add(addressdata);
|
vecs.by_size_range.get_mut(amount).1.state.add(addressdata);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
addressdata.send(value, prev_price)?;
|
|
||||||
|
|
||||||
vecs.by_size_range.get_mut(amount).1.state.send(
|
vecs.by_size_range.get_mut(amount).1.state.send(
|
||||||
|
addressdata,
|
||||||
value,
|
value,
|
||||||
price,
|
price,
|
||||||
prev_price,
|
prev_price,
|
||||||
blocks_old,
|
blocks_old,
|
||||||
days_old,
|
days_old,
|
||||||
older_than_hour,
|
older_than_hour,
|
||||||
);
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -59,10 +59,7 @@ impl CohortVecs for Vecs {
|
|||||||
|
|
||||||
fn starting_height(&self) -> Height {
|
fn starting_height(&self) -> Height {
|
||||||
[
|
[
|
||||||
self.state
|
self.state.height().map_or(Height::MAX, |h| h.incremented()),
|
||||||
.price_to_amount
|
|
||||||
.height()
|
|
||||||
.map_or(Height::MAX, |h| h.incremented()),
|
|
||||||
self.inner.starting_height(),
|
self.inner.starting_height(),
|
||||||
]
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
use byteview::ByteView;
|
use byteview::ByteView;
|
||||||
use zerocopy::{FromBytes, IntoBytes};
|
use zerocopy::{FromBytes, IntoBytes};
|
||||||
use zerocopy_derive::{FromBytes, Immutable, KnownLayout};
|
|
||||||
|
|
||||||
use crate::{CheckedSub, Dollars, EmptyAddressData, Error, Result, Sats};
|
use crate::{Bitcoin, CheckedSub, Dollars, EmptyAddressData, Error, Result, Sats};
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, FromBytes, Immutable, KnownLayout)]
|
#[derive(Debug, Default, Clone)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct AddressData {
|
pub struct AddressData {
|
||||||
pub sent: Sats,
|
pub sent: Sats,
|
||||||
@@ -18,6 +17,10 @@ impl AddressData {
|
|||||||
(u64::from(self.received) - u64::from(self.sent)).into()
|
(u64::from(self.received) - u64::from(self.sent)).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn realized_price(&self) -> Dollars {
|
||||||
|
(self.realized_cap / Bitcoin::from(self.amount())).round_nearest_cent()
|
||||||
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn has_0_sats(&self) -> bool {
|
pub fn has_0_sats(&self) -> bool {
|
||||||
self.amount() == Sats::ZERO
|
self.amount() == Sats::ZERO
|
||||||
@@ -70,7 +73,13 @@ impl From<&EmptyAddressData> for AddressData {
|
|||||||
|
|
||||||
impl From<ByteView> for AddressData {
|
impl From<ByteView> for AddressData {
|
||||||
fn from(value: ByteView) -> Self {
|
fn from(value: ByteView) -> Self {
|
||||||
Self::read_from_bytes(&value).unwrap()
|
Self {
|
||||||
|
// MUST be same order as impl From<&AddressData> for ByteView
|
||||||
|
sent: Sats::read_from_bytes(&value[..8]).unwrap(),
|
||||||
|
received: Sats::read_from_bytes(&value[8..16]).unwrap(),
|
||||||
|
realized_cap: Dollars::read_from_bytes(&value[16..24]).unwrap(),
|
||||||
|
outputs_len: u32::read_from_bytes(&value[24..]).unwrap(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<AddressData> for ByteView {
|
impl From<AddressData> for ByteView {
|
||||||
|
|||||||
@@ -38,6 +38,10 @@ impl Dollars {
|
|||||||
pub const fn mint(dollars: f64) -> Self {
|
pub const fn mint(dollars: f64) -> Self {
|
||||||
Self(dollars)
|
Self(dollars)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn round_nearest_cent(self) -> Self {
|
||||||
|
Dollars((self.0 * 100.0).round() / 100.0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<f32> for Dollars {
|
impl From<f32> for Dollars {
|
||||||
|
|||||||
@@ -278,6 +278,8 @@ impl Stores {
|
|||||||
self.txidprefix_to_txindex.reset()?;
|
self.txidprefix_to_txindex.reset()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.keyspace.persist(PersistMode::SyncAll)?;
|
||||||
|
|
||||||
self.commit(starting_indexes.height.decremented().unwrap_or_default())?;
|
self.commit(starting_indexes.height.decremented().unwrap_or_default())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -85,9 +85,19 @@ impl<'a> Interface<'a> {
|
|||||||
vecs: Vec<(String, &&dyn AnyCollectableVec)>,
|
vecs: Vec<(String, &&dyn AnyCollectableVec)>,
|
||||||
params: &ParamsOpt,
|
params: &ParamsOpt,
|
||||||
) -> color_eyre::Result<Output> {
|
) -> color_eyre::Result<Output> {
|
||||||
let from = params.from();
|
let from = params.from().map(|from| {
|
||||||
let to = params.to();
|
vecs.iter()
|
||||||
let format = params.format();
|
.map(|(_, v)| v.i64_to_usize(from))
|
||||||
|
.min()
|
||||||
|
.unwrap_or_default()
|
||||||
|
});
|
||||||
|
|
||||||
|
let to = params.to().map(|to| {
|
||||||
|
vecs.iter()
|
||||||
|
.map(|(_, v)| v.i64_to_usize(to))
|
||||||
|
.min()
|
||||||
|
.unwrap_or_default()
|
||||||
|
});
|
||||||
|
|
||||||
let mut values = vecs
|
let mut values = vecs
|
||||||
.iter()
|
.iter()
|
||||||
@@ -96,6 +106,8 @@ impl<'a> Interface<'a> {
|
|||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>>>()?;
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
|
||||||
|
let format = params.format();
|
||||||
|
|
||||||
if values.is_empty() {
|
if values.is_empty() {
|
||||||
return Ok(Output::default(format));
|
return Ok(Output::default(format));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ use axum::{
|
|||||||
http::{HeaderMap, StatusCode},
|
http::{HeaderMap, StatusCode},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
use brk_core::DateIndex;
|
|
||||||
use brk_interface::{Format, Output, Params};
|
use brk_interface::{Format, Output, Params};
|
||||||
use brk_vec::{CollectableVec, StoredVec};
|
|
||||||
use color_eyre::eyre::eyre;
|
use color_eyre::eyre::eyre;
|
||||||
|
|
||||||
use crate::traits::{HeaderMapExtended, ResponseExtended};
|
use crate::traits::{HeaderMapExtended, ResponseExtended};
|
||||||
@@ -52,11 +50,7 @@ fn req_to_response_res(
|
|||||||
|
|
||||||
let weight = vecs
|
let weight = vecs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, v)| {
|
.map(|(_, v)| v.range_weight(from, to))
|
||||||
let len = v.len();
|
|
||||||
let count = StoredVec::<DateIndex, usize>::range_count(from, to, len);
|
|
||||||
count * v.value_type_to_size_of()
|
|
||||||
})
|
|
||||||
.sum::<usize>();
|
.sum::<usize>();
|
||||||
|
|
||||||
if weight > MAX_WEIGHT {
|
if weight > MAX_WEIGHT {
|
||||||
|
|||||||
@@ -13,3 +13,4 @@ repository.workspace = true
|
|||||||
brk_core = { workspace = true }
|
brk_core = { workspace = true }
|
||||||
byteview = { workspace = true }
|
byteview = { workspace = true }
|
||||||
fjall = { workspace = true }
|
fjall = { workspace = true }
|
||||||
|
log = { workspace = true }
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ use std::{
|
|||||||
use brk_core::{Height, Result, Version};
|
use brk_core::{Height, Result, Version};
|
||||||
use byteview::ByteView;
|
use byteview::ByteView;
|
||||||
use fjall::{
|
use fjall::{
|
||||||
PartitionCreateOptions, PersistMode, ReadTransaction, TransactionalKeyspace,
|
PartitionCreateOptions, ReadTransaction, TransactionalKeyspace, TransactionalPartitionHandle,
|
||||||
TransactionalPartitionHandle,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mod meta;
|
mod meta;
|
||||||
mod r#trait;
|
mod r#trait;
|
||||||
|
|
||||||
|
use log::info;
|
||||||
use meta::*;
|
use meta::*;
|
||||||
pub use r#trait::*;
|
pub use r#trait::*;
|
||||||
|
|
||||||
@@ -241,12 +241,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn reset(&mut self) -> Result<()> {
|
fn reset(&mut self) -> Result<()> {
|
||||||
|
info!("Resetting {}...", self.name);
|
||||||
|
|
||||||
let partition: TransactionalPartitionHandle = self.partition.take().unwrap();
|
let partition: TransactionalPartitionHandle = self.partition.take().unwrap();
|
||||||
|
|
||||||
self.keyspace.delete_partition(partition)?;
|
self.keyspace.delete_partition(partition)?;
|
||||||
|
|
||||||
self.keyspace.persist(PersistMode::SyncAll)?;
|
|
||||||
|
|
||||||
self.meta.reset();
|
self.meta.reset();
|
||||||
|
|
||||||
let partition =
|
let partition =
|
||||||
|
|||||||
@@ -2,6 +2,15 @@ use brk_core::Version;
|
|||||||
|
|
||||||
use super::{BoxedVecIterator, StoredIndex, StoredType};
|
use super::{BoxedVecIterator, StoredIndex, StoredType};
|
||||||
|
|
||||||
|
pub fn i64_to_usize(i: i64, len: usize) -> usize {
|
||||||
|
if i >= 0 {
|
||||||
|
(i as usize).min(len)
|
||||||
|
} else {
|
||||||
|
let v = len as i64 + i;
|
||||||
|
if v < 0 { 0 } else { v as usize }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait AnyVec: Send + Sync {
|
pub trait AnyVec: Send + Sync {
|
||||||
fn version(&self) -> Version;
|
fn version(&self) -> Version;
|
||||||
fn name(&self) -> &str;
|
fn name(&self) -> &str;
|
||||||
@@ -26,6 +35,12 @@ pub trait AnyVec: Send + Sync {
|
|||||||
self.version()
|
self.version()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn i64_to_usize(&self, i: i64) -> usize {
|
||||||
|
let len = self.len();
|
||||||
|
i64_to_usize(i, len)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AnyIterableVec<I, T>: AnyVec {
|
pub trait AnyIterableVec<I, T>: AnyVec {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use brk_core::{Error, Result};
|
use brk_core::{Error, Result};
|
||||||
|
|
||||||
|
use crate::i64_to_usize;
|
||||||
|
|
||||||
use super::{AnyIterableVec, AnyVec, StoredIndex, StoredType};
|
use super::{AnyIterableVec, AnyVec, StoredIndex, StoredType};
|
||||||
|
|
||||||
pub trait CollectableVec<I, T>: AnyVec + AnyIterableVec<I, T>
|
pub trait CollectableVec<I, T>: AnyVec + AnyIterableVec<I, T>
|
||||||
@@ -25,7 +27,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn i64_to_usize(i: i64, len: usize) -> usize {
|
fn i64_to_usize_(i: i64, len: usize) -> usize {
|
||||||
if i >= 0 {
|
if i >= 0 {
|
||||||
(i as usize).min(len)
|
(i as usize).min(len)
|
||||||
} else {
|
} else {
|
||||||
@@ -34,27 +36,19 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn range_count(from: Option<i64>, to: Option<i64>, len: usize) -> usize {
|
|
||||||
let from = from.map(|i| Self::i64_to_usize(i, len));
|
|
||||||
let to = to.map(|i| Self::i64_to_usize(i, len));
|
|
||||||
(from.unwrap_or_default()..to.unwrap_or(len)).count()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
fn collect_signed_range(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<T>> {
|
fn collect_signed_range(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<T>> {
|
||||||
let len = self.len();
|
let from = from.map(|i| self.i64_to_usize(i));
|
||||||
let from = from.map(|i| Self::i64_to_usize(i, len));
|
let to = to.map(|i| self.i64_to_usize(i));
|
||||||
let to = to.map(|i| Self::i64_to_usize(i, len));
|
|
||||||
self.collect_range(from, to)
|
self.collect_range(from, to)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
self.collect_signed_range(from, to)?
|
self.collect_range(from, to)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|v| serde_json::to_value(v).map_err(Error::from))
|
.map(|v| serde_json::to_value(v).map_err(Error::from))
|
||||||
.collect::<Result<Vec<_>>>()
|
.collect::<Result<Vec<_>>>()
|
||||||
@@ -72,7 +66,18 @@ where
|
|||||||
pub trait AnyCollectableVec: AnyVec {
|
pub trait AnyCollectableVec: AnyVec {
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>>;
|
) -> Result<Vec<serde_json::Value>>;
|
||||||
|
|
||||||
|
fn range_count(&self, from: Option<i64>, to: Option<i64>) -> usize {
|
||||||
|
let len = self.len();
|
||||||
|
let from = from.map(|i| i64_to_usize(i, len));
|
||||||
|
let to = to.map(|i| i64_to_usize(i, len));
|
||||||
|
(from.unwrap_or_default()..to.unwrap_or(len)).count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn range_weight(&self, from: Option<i64>, to: Option<i64>) -> usize {
|
||||||
|
self.range_count(from, to) * self.value_type_to_size_of()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -528,8 +528,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -397,8 +397,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1336,8 +1336,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -166,8 +166,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,8 +179,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -230,8 +230,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -272,8 +272,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -359,8 +359,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -272,8 +272,8 @@ where
|
|||||||
{
|
{
|
||||||
fn collect_range_serde_json(
|
fn collect_range_serde_json(
|
||||||
&self,
|
&self,
|
||||||
from: Option<i64>,
|
from: Option<usize>,
|
||||||
to: Option<i64>,
|
to: Option<usize>,
|
||||||
) -> Result<Vec<serde_json::Value>> {
|
) -> Result<Vec<serde_json::Value>> {
|
||||||
CollectableVec::collect_range_serde_json(self, from, to)
|
CollectableVec::collect_range_serde_json(self, from, to)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user