computer + kibo: part 10

This commit is contained in:
nym21
2025-04-11 19:21:35 +02:00
parent f32b6daa51
commit 1c75ea046c
11 changed files with 374 additions and 101 deletions

View File

@@ -2,7 +2,7 @@ use core::error;
use std::{
cmp::Ordering,
fmt::Debug,
ops::{Add, Sub},
ops::Add,
path::{Path, PathBuf},
};
@@ -390,22 +390,26 @@ where
max_from: I,
first_indexes: &mut StoredVec<I, T2>,
last_indexes: &mut StoredVec<I, T2>,
source: &mut StoredVec<T2, T>,
exit: &Exit,
) -> Result<()>
where
T: From<T2>,
T2: StoredType + Copy + Add<usize, Output = T2> + Sub<T2, Output = T2> + TryInto<T>,
<T2 as TryInto<T>>::Error: error::Error + 'static,
T: From<usize> + Add<T, Output = T>,
T2: StoredIndex + StoredType,
{
self.validate_computed_version_or_reset_file(
Version::ZERO + self.version() + first_indexes.version() + last_indexes.version(),
)?;
let index = max_from.min(I::from(self.len()));
first_indexes.iter_from(index, |(index, first_index, ..)| {
let last_index = last_indexes.cached_get(index)?.unwrap();
let count = *last_index + 1_usize - first_index;
self.forced_push_at(index, count.into(), exit)
first_indexes.iter_from(index, |(i, first_index, ..)| {
let last_index = last_indexes.cached_get(i)?.unwrap().into_inner();
let range = first_index.to_usize().unwrap()..=last_index.to_usize().unwrap();
let mut sum = T::from(0_usize);
range.into_iter().for_each(|i| {
sum = sum.clone() + source.cached_get_(i).unwrap().unwrap().into_inner();
});
self.forced_push_at(i, sum, exit)
})?;
self.safe_flush(exit)

View File

@@ -92,7 +92,8 @@ impl Vecs {
indexer_vecs.height_to_timestamp.mut_vec(),
|(height, timestamp, _, height_to_timestamp)| {
let interval = height.decremented().map_or(Timestamp::ZERO, |prev_h| {
let prev_timestamp = *height_to_timestamp.get(prev_h).unwrap().unwrap();
let prev_timestamp =
*height_to_timestamp.cached_get(prev_h).unwrap().unwrap();
timestamp
.checked_sub(prev_timestamp)
.unwrap_or(Timestamp::ZERO)

View File

@@ -18,7 +18,6 @@ where
T: ComputedType + PartialOrd,
{
pub txindex: Option<ComputedVec<Txindex, T>>,
pub txindex_extra: ComputedVecBuilder<Txindex, T>,
pub height: ComputedVecBuilder<Height, T>,
pub dateindex: ComputedVecBuilder<Dateindex, T>,
pub weekindex: ComputedVecBuilder<Weekindex, T>,
@@ -52,23 +51,14 @@ where
.unwrap()
});
let txindex_extra = ComputedVecBuilder::forced_import(
path,
name,
compressed,
StorableVecGeneatorOptions::default(),
)?;
let height = ComputedVecBuilder::forced_import(path, name, compressed, options)?;
let dateindex = ComputedVecBuilder::forced_import(path, name, compressed, options)?;
let options = options.remove_percentiles();
Ok(Self {
txindex,
txindex_extra,
height,
dateindex,
dateindex: ComputedVecBuilder::forced_import(path, name, compressed, options)?,
weekindex: ComputedVecBuilder::forced_import(path, name, compressed, options)?,
difficultyepoch: ComputedVecBuilder::forced_import(path, name, compressed, options)?,
monthindex: ComputedVecBuilder::forced_import(path, name, compressed, options)?,
@@ -119,9 +109,6 @@ where
) -> color_eyre::Result<()> {
let txindex = txindex.unwrap_or_else(|| self.txindex.as_mut().unwrap().mut_vec());
self.txindex_extra
.extend(starting_indexes.txindex, txindex, exit)?;
self.height.compute(
starting_indexes.height,
txindex,
@@ -192,7 +179,6 @@ where
pub fn any_vecs(&self) -> Vec<&dyn AnyStoredVec> {
[
self.txindex.as_ref().map_or(vec![], |v| vec![v.any_vec()]),
self.txindex_extra.any_vecs(),
self.height.any_vecs(),
self.dateindex.any_vecs(),
self.weekindex.any_vecs(),

View File

@@ -1,8 +1,12 @@
use std::{fs, path::Path};
use brk_core::{Sats, StoredU32, StoredU64, TxVersion, Txindex, Txinindex, Txoutindex};
use brk_core::{
CheckedSub, Feerate, Sats, StoredU32, StoredU64, StoredUsize, TxVersion, Txindex, Txinindex,
Txoutindex, Weight,
};
use brk_exit::Exit;
use brk_indexer::Indexer;
use brk_parser::bitcoin;
use brk_vec::{Compressed, DynamicVec, Version};
use super::{
@@ -21,19 +25,19 @@ pub struct Vecs {
// pub height_to_outputcount: ComputedVec<Height, u32>,
// pub height_to_subsidy: ComputedVec<Height, u32>,
// pub height_to_totalfees: ComputedVec<Height, Sats>,
// pub txindex_to_fee: ComputedVec<Txindex, Sats>,
// pub txindex_to_feerate: ComputedVec<Txindex, Feerate>,
// pub txindex_to_input_sum: ComputedVec<Txindex, Sats>,
// pub txindex_to_output_sum: ComputedVec<Txindex, Sats>,
// pub txindex_to_output_value: ComputedVecsFromTxindex<Sats>,
pub height_to_tx_count: ComputedVecsFromHeight<StoredU64>,
pub indexes_to_fee: ComputedVecsFromTxindex<Sats>,
pub indexes_to_feerate: ComputedVecsFromTxindex<Feerate>,
pub indexes_to_input_value: ComputedVecsFromTxindex<Sats>,
pub indexes_to_output_value: ComputedVecsFromTxindex<Sats>,
pub indexes_to_tx_v1: ComputedVecsFromHeight<StoredU32>,
pub indexes_to_tx_v2: ComputedVecsFromHeight<StoredU32>,
pub indexes_to_tx_v3: ComputedVecsFromHeight<StoredU32>,
// pub txinindex_to_value: ComputedVec<Txinindex, Sats>,
pub height_to_tx_count: ComputedVecsFromHeight<StoredU64>,
pub txindex_to_input_count: ComputedVecsFromTxindex<StoredU64>,
pub txindex_to_is_coinbase: ComputedVec<Txindex, bool>,
pub txindex_to_output_count: ComputedVecsFromTxindex<StoredU64>,
pub txindex_to_vsize: ComputedVec<Txindex, StoredUsize>,
pub txindex_to_weight: ComputedVec<Txindex, Weight>,
/// Value == 0 when Coinbase
pub txinindex_to_value: ComputedVec<Txinindex, Sats>,
}
@@ -66,10 +70,6 @@ impl Vecs {
// height_to_subsidy: StorableVec::forced_import(&path.join("height_to_subsidy"), Version::ZERO)?,
// height_to_totalfees: StorableVec::forced_import(&path.join("height_to_totalfees"), Version::ZERO)?,
// height_to_txcount: StorableVec::forced_import(&path.join("height_to_txcount"), Version::ZERO)?,
// txindex_to_fee: StorableVec::forced_import(
// &path.join("txindex_to_fee"),
// Version::ZERO,
// )?,
txindex_to_is_coinbase: ComputedVec::forced_import(
&path.join("txindex_to_is_coinbase"),
Version::ZERO,
@@ -128,6 +128,51 @@ impl Vecs {
compressed,
StorableVecGeneatorOptions::default().add_sum().add_total(),
)?,
indexes_to_input_value: ComputedVecsFromTxindex::forced_import(
path,
"input_value",
true,
Version::ZERO,
compressed,
StorableVecGeneatorOptions::default().add_sum().add_total(),
)?,
indexes_to_output_value: ComputedVecsFromTxindex::forced_import(
path,
"output_value",
true,
Version::ZERO,
compressed,
StorableVecGeneatorOptions::default().add_sum().add_total(),
)?,
indexes_to_fee: ComputedVecsFromTxindex::forced_import(
path,
"fee",
true,
Version::ZERO,
compressed,
StorableVecGeneatorOptions::default().add_sum().add_total(),
)?,
indexes_to_feerate: ComputedVecsFromTxindex::forced_import(
path,
"feerate",
true,
Version::ZERO,
compressed,
StorableVecGeneatorOptions::default()
.add_percentiles()
.add_minmax()
.add_average(),
)?,
txindex_to_weight: ComputedVec::forced_import(
&path.join("txindex_to_weight"),
Version::ZERO,
compressed,
)?,
txindex_to_vsize: ComputedVec::forced_import(
&path.join("txindex_to_vsize"),
Version::ZERO,
compressed,
)?,
})
}
@@ -231,6 +276,38 @@ impl Vecs {
exit,
)?;
self.txindex_to_weight.compute_transform(
starting_indexes.txindex,
indexer_vecs.txindex_to_base_size.mut_vec(),
|(txindex, base_size, ..)| {
let total_size = indexer_vecs
.txindex_to_total_size
.mut_vec()
.cached_get(txindex)
.unwrap()
.unwrap()
.into_inner();
// This is the exact definition of a weight unit, as defined by BIP-141 (quote above).
let wu = base_size * 3 + total_size;
let weight = Weight::from(bitcoin::Weight::from_wu_usize(wu));
(txindex, weight)
},
exit,
)?;
self.txindex_to_vsize.compute_transform(
starting_indexes.txindex,
self.txindex_to_weight.mut_vec(),
|(txindex, weight, ..)| {
let vbytes =
StoredUsize::from(bitcoin::Weight::from(weight).to_vbytes_ceil() as usize);
(txindex, vbytes)
},
exit,
)?;
self.txinindex_to_value.compute_transform(
starting_indexes.txinindex,
indexer_vecs.txinindex_to_txoutindex.mut_vec(),
@@ -252,10 +329,100 @@ impl Vecs {
exit,
)?;
// self.txindex_to_fee.compute_transform(
// &mut self.vecs.txindex_to_height,
// &mut indexer.vecs().height_to_first_txindex,
// )?;
self.indexes_to_output_value.compute_all(
indexer,
indexes,
starting_indexes,
exit,
|vec, indexer, indexes, starting_indexes, exit| {
let indexer_vecs = indexer.mut_vecs();
vec.compute_sum_from_indexes(
starting_indexes.txindex,
indexer_vecs.txindex_to_first_txoutindex.mut_vec(),
indexes.txindex_to_last_txoutindex.mut_vec(),
indexer_vecs.txoutindex_to_value.mut_vec(),
exit,
)
},
)?;
self.indexes_to_input_value.compute_all(
indexer,
indexes,
starting_indexes,
exit,
|vec, indexer, indexes, starting_indexes, exit| {
let indexer_vecs = indexer.mut_vecs();
vec.compute_sum_from_indexes(
starting_indexes.txindex,
indexer_vecs.txindex_to_first_txinindex.mut_vec(),
indexes.txindex_to_last_txinindex.mut_vec(),
self.txinindex_to_value.mut_vec(),
exit,
)
},
)?;
self.indexes_to_fee.compute_all(
indexer,
indexes,
starting_indexes,
exit,
|vec, _, _, starting_indexes, exit| {
let txindex_to_output_value = self
.indexes_to_output_value
.txindex
.as_mut()
.unwrap()
.mut_vec();
vec.compute_transform(
starting_indexes.txindex,
self.indexes_to_input_value
.txindex
.as_mut()
.unwrap()
.mut_vec(),
|(txindex, input_value, ..)| {
if input_value.is_zero() {
(txindex, input_value)
} else {
let output_value = txindex_to_output_value
.cached_get(txindex)
.unwrap()
.unwrap()
.into_inner();
(txindex, input_value.checked_sub(output_value).unwrap())
}
},
exit,
)
},
)?;
self.indexes_to_feerate.compute_all(
indexer,
indexes,
starting_indexes,
exit,
|vec, _, _, starting_indexes, exit| {
vec.compute_transform(
starting_indexes.txindex,
self.indexes_to_fee.txindex.as_mut().unwrap().mut_vec(),
|(txindex, fee, ..)| {
let vsize = self
.txindex_to_vsize
.mut_vec()
.cached_get(txindex)
.unwrap()
.unwrap()
.into_inner();
(txindex, Feerate::from((fee, vsize)))
},
exit,
)
},
)?;
Ok(())
}
@@ -265,13 +432,19 @@ impl Vecs {
vec![
self.txindex_to_is_coinbase.any_vec(),
self.txinindex_to_value.any_vec(),
self.txindex_to_weight.any_vec(),
self.txindex_to_vsize.any_vec(),
],
self.height_to_tx_count.any_vecs(),
self.txindex_to_output_count.any_vecs(),
self.txindex_to_input_count.any_vecs(),
self.indexes_to_input_value.any_vecs(),
self.indexes_to_output_value.any_vecs(),
self.indexes_to_tx_v1.any_vecs(),
self.indexes_to_tx_v2.any_vecs(),
self.indexes_to_tx_v3.any_vecs(),
self.indexes_to_fee.any_vecs(),
self.indexes_to_feerate.any_vecs(),
self.txindex_to_input_count.any_vecs(),
self.txindex_to_output_count.any_vecs(),
]
.concat()
}