mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-29 00:59:58 -07:00
global: MASSIVE snapshot
This commit is contained in:
@@ -1,66 +1,186 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::{Height, StoredU32};
|
||||
use vecdb::{EagerVec, Exit, PcoVec, TypedVecIterator};
|
||||
use brk_types::{Height, StoredU32, Timestamp};
|
||||
use vecdb::{EagerVec, Exit, PcoVec, ReadableVec, VecIndex};
|
||||
|
||||
use super::super::time;
|
||||
use super::Vecs;
|
||||
use crate::{ComputeIndexes, indexes, internal::ComputedFromHeightLast};
|
||||
use crate::ComputeIndexes;
|
||||
|
||||
use super::{super::time, Vecs};
|
||||
|
||||
impl Vecs {
|
||||
pub fn compute(
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
time: &time::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.block_count.height.compute_range(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.weight,
|
||||
|h| (h, StoredU32::from(1_u32)),
|
||||
exit,
|
||||
)?;
|
||||
self.block_count
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
v.compute_range(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.weight,
|
||||
|h| (h, StoredU32::from(1_u32)),
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
.compute_cumulative(starting_indexes, exit)?;
|
||||
|
||||
// Compute rolling window starts
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 1, |s| &mut s._24h_start)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 7, |s| &mut s._1w_start)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 30, |s| &mut s._1m_start)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 365, |s| &mut s._1y_start)?;
|
||||
// Compute rolling window starts (collect monotonic data once for all windows)
|
||||
let monotonic_data: Vec<Timestamp> = time.timestamp_monotonic.collect();
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 1, |s| {
|
||||
&mut s.height_24h_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 3, |s| {
|
||||
&mut s.height_3d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 7, |s| {
|
||||
&mut s.height_1w_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 8, |s| {
|
||||
&mut s.height_8d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 9, |s| {
|
||||
&mut s.height_9d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 12, |s| {
|
||||
&mut s.height_12d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 13, |s| {
|
||||
&mut s.height_13d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 14, |s| {
|
||||
&mut s.height_2w_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 21, |s| {
|
||||
&mut s.height_21d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 26, |s| {
|
||||
&mut s.height_26d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 30, |s| {
|
||||
&mut s.height_1m_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 34, |s| {
|
||||
&mut s.height_34d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 55, |s| {
|
||||
&mut s.height_55d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 2 * 30, |s| {
|
||||
&mut s.height_2m_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 89, |s| {
|
||||
&mut s.height_89d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 3 * 30, |s| {
|
||||
&mut s.height_3m_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 111, |s| {
|
||||
&mut s.height_111d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 144, |s| {
|
||||
&mut s.height_144d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 6 * 30, |s| {
|
||||
&mut s.height_6m_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 200, |s| {
|
||||
&mut s.height_200d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 350, |s| {
|
||||
&mut s.height_350d_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 365, |s| {
|
||||
&mut s.height_1y_ago
|
||||
})?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
2 * 365,
|
||||
|s| &mut s.height_2y_ago,
|
||||
)?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
200 * 7,
|
||||
|s| &mut s.height_200w_ago,
|
||||
)?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
3 * 365,
|
||||
|s| &mut s.height_3y_ago,
|
||||
)?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
4 * 365,
|
||||
|s| &mut s.height_4y_ago,
|
||||
)?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
5 * 365,
|
||||
|s| &mut s.height_5y_ago,
|
||||
)?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
6 * 365,
|
||||
|s| &mut s.height_6y_ago,
|
||||
)?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
8 * 365,
|
||||
|s| &mut s.height_8y_ago,
|
||||
)?;
|
||||
self.compute_rolling_start(
|
||||
&monotonic_data,
|
||||
time,
|
||||
starting_indexes,
|
||||
exit,
|
||||
10 * 365,
|
||||
|s| &mut s.height_10y_ago,
|
||||
)?;
|
||||
|
||||
// Compute rolling window block counts
|
||||
self.compute_rolling_block_count(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
self.block_count_24h_sum.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.height_24h_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
&self._24h_start.clone(),
|
||||
|s| &mut s._24h_block_count,
|
||||
)?;
|
||||
self.compute_rolling_block_count(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
self.block_count_1w_sum.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.height_1w_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
&self._1w_start.clone(),
|
||||
|s| &mut s._1w_block_count,
|
||||
)?;
|
||||
self.compute_rolling_block_count(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
self.block_count_1m_sum.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.height_1m_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
&self._1m_start.clone(),
|
||||
|s| &mut s._1m_block_count,
|
||||
)?;
|
||||
self.compute_rolling_block_count(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
self.block_count_1y_sum.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.height_1y_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
&self._1y_start.clone(),
|
||||
|s| &mut s._1y_block_count,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
@@ -68,6 +188,7 @@ impl Vecs {
|
||||
|
||||
fn compute_rolling_start<F>(
|
||||
&mut self,
|
||||
monotonic_data: &[Timestamp],
|
||||
time: &time::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
@@ -77,13 +198,12 @@ impl Vecs {
|
||||
where
|
||||
F: FnOnce(&mut Self) -> &mut EagerVec<PcoVec<Height, Height>>,
|
||||
{
|
||||
let mut iter = time.timestamp_monotonic.into_iter();
|
||||
let mut prev = Height::ZERO;
|
||||
Ok(get_field(self).compute_transform(
|
||||
starting_indexes.height,
|
||||
&time.timestamp_monotonic,
|
||||
|(h, t, ..)| {
|
||||
while t.difference_in_days_between(iter.get_unwrap(prev)) >= days {
|
||||
while t.difference_in_days_between(monotonic_data[prev.to_usize()]) >= days {
|
||||
prev.increment();
|
||||
if prev > h {
|
||||
unreachable!()
|
||||
@@ -94,26 +214,4 @@ impl Vecs {
|
||||
exit,
|
||||
)?)
|
||||
}
|
||||
|
||||
fn compute_rolling_block_count<F>(
|
||||
&mut self,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
start_height: &EagerVec<PcoVec<Height, Height>>,
|
||||
get_field: F,
|
||||
) -> Result<()>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> &mut ComputedFromHeightLast<StoredU32>,
|
||||
{
|
||||
get_field(self).compute_all(indexes, starting_indexes, exit, |v| {
|
||||
v.compute_transform(
|
||||
starting_indexes.height,
|
||||
start_height,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user