mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: snapshot pre cached change
This commit is contained in:
@@ -9,9 +9,7 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{Database, EagerVec, PcoVec, Rw, StorageMode};
|
||||
|
||||
use vecdb::CachedVec;
|
||||
use vecdb::{CachedVec, Database, EagerVec, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
|
||||
@@ -31,6 +31,7 @@ impl BlockProcessor<'_> {
|
||||
self.vecs
|
||||
.blocks
|
||||
.blockhash
|
||||
.inner
|
||||
.checked_push(height, blockhash.clone())?;
|
||||
self.vecs
|
||||
.blocks
|
||||
@@ -43,6 +44,7 @@ impl BlockProcessor<'_> {
|
||||
self.vecs
|
||||
.blocks
|
||||
.timestamp
|
||||
.inner
|
||||
.checked_push(height, Timestamp::from(self.block.header.time))?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -14,17 +14,13 @@ use crate::parallel_import;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct BlocksVecs<M: StorageMode = Rw> {
|
||||
pub blockhash: M::Stored<BytesVec<Height, BlockHash>>,
|
||||
#[traversable(skip)]
|
||||
pub cached_blockhash: CachedVec<Height, BlockHash>,
|
||||
pub blockhash: CachedVec<M::Stored<BytesVec<Height, BlockHash>>>,
|
||||
pub coinbase_tag: M::Stored<BytesVec<Height, CoinbaseTag>>,
|
||||
#[traversable(wrap = "difficulty", rename = "value")]
|
||||
pub difficulty: M::Stored<PcoVec<Height, StoredF64>>,
|
||||
/// Doesn't guarantee continuity due to possible reorgs and more generally the nature of mining
|
||||
#[traversable(wrap = "time")]
|
||||
pub timestamp: M::Stored<PcoVec<Height, Timestamp>>,
|
||||
#[traversable(skip)]
|
||||
pub cached_timestamp: CachedVec<Height, Timestamp>,
|
||||
pub timestamp: CachedVec<M::Stored<PcoVec<Height, Timestamp>>>,
|
||||
#[traversable(wrap = "size", rename = "base")]
|
||||
pub total: M::Stored<PcoVec<Height, StoredU64>>,
|
||||
#[traversable(wrap = "weight", rename = "base")]
|
||||
@@ -61,16 +57,11 @@ impl BlocksVecs {
|
||||
segwit_size = PcoVec::forced_import(db, "segwit_size", version),
|
||||
segwit_weight = PcoVec::forced_import(db, "segwit_weight", version),
|
||||
};
|
||||
let cached_blockhash = CachedVec::new(&blockhash);
|
||||
let cached_timestamp = CachedVec::new(×tamp);
|
||||
|
||||
Ok(Self {
|
||||
blockhash,
|
||||
cached_blockhash,
|
||||
blockhash: CachedVec::wrap(blockhash),
|
||||
coinbase_tag,
|
||||
difficulty,
|
||||
timestamp,
|
||||
cached_timestamp,
|
||||
timestamp: CachedVec::wrap(timestamp),
|
||||
total,
|
||||
weight,
|
||||
position,
|
||||
@@ -82,12 +73,14 @@ impl BlocksVecs {
|
||||
|
||||
pub fn truncate(&mut self, height: Height, stamp: Stamp) -> Result<()> {
|
||||
self.blockhash
|
||||
.inner
|
||||
.truncate_if_needed_with_stamp(height, stamp)?;
|
||||
self.coinbase_tag
|
||||
.truncate_if_needed_with_stamp(height, stamp)?;
|
||||
self.difficulty
|
||||
.truncate_if_needed_with_stamp(height, stamp)?;
|
||||
self.timestamp
|
||||
.inner
|
||||
.truncate_if_needed_with_stamp(height, stamp)?;
|
||||
self.total.truncate_if_needed_with_stamp(height, stamp)?;
|
||||
self.weight.truncate_if_needed_with_stamp(height, stamp)?;
|
||||
@@ -103,10 +96,10 @@ impl BlocksVecs {
|
||||
|
||||
pub fn par_iter_mut_any(&mut self) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
[
|
||||
&mut self.blockhash as &mut dyn AnyStoredVec,
|
||||
&mut self.blockhash.inner as &mut dyn AnyStoredVec,
|
||||
&mut self.coinbase_tag,
|
||||
&mut self.difficulty,
|
||||
&mut self.timestamp,
|
||||
&mut self.timestamp.inner,
|
||||
&mut self.total,
|
||||
&mut self.weight,
|
||||
&mut self.position,
|
||||
|
||||
@@ -60,7 +60,7 @@ impl Query {
|
||||
let blockhash = indexer
|
||||
.vecs
|
||||
.blocks
|
||||
.cached_blockhash
|
||||
.blockhash
|
||||
.collect_one(height)
|
||||
.data()?;
|
||||
|
||||
|
||||
@@ -65,8 +65,8 @@ impl Query {
|
||||
#[inline]
|
||||
pub(crate) fn block_hash_and_time(&self, height: Height) -> Result<(BlockHash, Timestamp)> {
|
||||
let indexer = self.indexer();
|
||||
let hash = indexer.vecs.blocks.cached_blockhash.collect_one(height).data()?;
|
||||
let time = indexer.vecs.blocks.cached_timestamp.collect_one(height).data()?;
|
||||
let hash = indexer.vecs.blocks.blockhash.collect_one(height).data()?;
|
||||
let time = indexer.vecs.blocks.timestamp.collect_one(height).data()?;
|
||||
Ok((hash, time))
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,10 @@ pub use brk_traversable_derive::Traversable;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Serialize;
|
||||
use vecdb::{
|
||||
AggFold, AnyExportableVec, AnyVec, BytesVec, BytesVecValue, CompressionStrategy, DeltaOp,
|
||||
EagerVec, Formattable, LazyAggVec, LazyDeltaVec, LazyVecFrom1, LazyVecFrom2, LazyVecFrom3,
|
||||
RawStrategy, ReadOnlyCompressedVec, ReadOnlyRawVec, StoredVec, VecIndex, VecValue,
|
||||
AggFold, AnyExportableVec, AnyVec, BytesVec, BytesVecValue, CachedVec, CompressionStrategy,
|
||||
DeltaOp, EagerVec, Formattable, LazyAggVec, LazyDeltaVec, LazyVecFrom1, LazyVecFrom2,
|
||||
LazyVecFrom3, RawStrategy, ReadOnlyCompressedVec, ReadOnlyRawVec, StoredVec, TypedVec,
|
||||
VecIndex, VecValue,
|
||||
};
|
||||
|
||||
pub trait Traversable {
|
||||
@@ -256,6 +257,20 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: TypedVec + Traversable> Traversable for CachedVec<V> {
|
||||
fn to_tree_node(&self) -> TreeNode {
|
||||
self.inner.to_tree_node()
|
||||
}
|
||||
|
||||
fn iter_any_exportable(&self) -> impl Iterator<Item = &dyn AnyExportableVec> {
|
||||
self.inner.iter_any_exportable()
|
||||
}
|
||||
|
||||
fn iter_any_visible(&self) -> impl Iterator<Item = &dyn AnyExportableVec> {
|
||||
self.inner.iter_any_visible()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Traversable + ?Sized> Traversable for Box<T> {
|
||||
fn to_tree_node(&self) -> TreeNode {
|
||||
(**self).to_tree_node()
|
||||
|
||||
Reference in New Issue
Block a user