mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-03 23:33:40 -07:00
global: snapshot
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::{Indexes, StoredF64, StoredU32};
|
||||
use brk_types::{Indexes, StoredU32};
|
||||
use vecdb::Exit;
|
||||
|
||||
use super::super::TARGET_BLOCKS_PER_DAY_F32;
|
||||
use super::Vecs;
|
||||
use crate::indexes;
|
||||
|
||||
@@ -15,18 +14,6 @@ impl Vecs {
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// raw is fully lazy from indexer height source — no compute needed
|
||||
|
||||
// Compute difficulty as hash rate equivalent
|
||||
let multiplier = 2.0_f64.powi(32) / 600.0;
|
||||
self.as_hash.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.difficulty,
|
||||
|(i, v, ..)| (i, StoredF64::from(*v * multiplier)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute difficulty adjustment ratio: (current - previous) / previous
|
||||
self.adjustment.bps.height.compute_ratio_change(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.difficulty,
|
||||
@@ -34,7 +21,6 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute epoch by height
|
||||
self.epoch.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexes.height.epoch,
|
||||
@@ -42,21 +28,10 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute blocks before next adjustment
|
||||
self.blocks_before_next
|
||||
.height
|
||||
.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexes.height.identity,
|
||||
|(h, ..)| (h, StoredU32::from(h.left_before_next_diff_adj())),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute days before next adjustment
|
||||
self.days_before_next.height.compute_transform(
|
||||
self.blocks_before_next.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.blocks_before_next.height,
|
||||
|(h, blocks, ..)| (h, (*blocks as f32 / TARGET_BLOCKS_PER_DAY_F32).into()),
|
||||
&indexes.height.identity,
|
||||
|(h, ..)| (h, StoredU32::from(h.left_before_next_diff_adj())),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@ use vecdb::{Database, ReadableCloneableVec};
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedPerBlock, Resolutions, PercentPerBlock},
|
||||
internal::{
|
||||
BlocksToDaysF32, DifficultyToHashF64, LazyPerBlock, PerBlock, PercentPerBlock, Resolutions,
|
||||
},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -18,33 +20,39 @@ impl Vecs {
|
||||
) -> Result<Self> {
|
||||
let v2 = Version::TWO;
|
||||
|
||||
let as_hash = LazyPerBlock::from_height_source::<DifficultyToHashF64>(
|
||||
"difficulty_as_hash",
|
||||
version,
|
||||
indexer.vecs.blocks.difficulty.read_only_boxed_clone(),
|
||||
indexes,
|
||||
);
|
||||
|
||||
let blocks_before_next = PerBlock::forced_import(
|
||||
db,
|
||||
"blocks_before_next_difficulty_adjustment",
|
||||
version + v2,
|
||||
indexes,
|
||||
)?;
|
||||
|
||||
let days_before_next = LazyPerBlock::from_computed::<BlocksToDaysF32>(
|
||||
"days_before_next_difficulty_adjustment",
|
||||
version + v2,
|
||||
blocks_before_next.height.read_only_boxed_clone(),
|
||||
&blocks_before_next,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
raw: Resolutions::forced_import(
|
||||
base: Resolutions::forced_import(
|
||||
"difficulty",
|
||||
indexer.vecs.blocks.difficulty.read_only_boxed_clone(),
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
as_hash: ComputedPerBlock::forced_import(db, "difficulty_as_hash", version, indexes)?,
|
||||
adjustment: PercentPerBlock::forced_import(
|
||||
db,
|
||||
"difficulty_adjustment",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
epoch: ComputedPerBlock::forced_import(db, "difficulty_epoch", version, indexes)?,
|
||||
blocks_before_next: ComputedPerBlock::forced_import(
|
||||
db,
|
||||
"blocks_before_next_difficulty_adjustment",
|
||||
version + v2,
|
||||
indexes,
|
||||
)?,
|
||||
days_before_next: ComputedPerBlock::forced_import(
|
||||
db,
|
||||
"days_before_next_difficulty_adjustment",
|
||||
version + v2,
|
||||
indexes,
|
||||
)?,
|
||||
as_hash,
|
||||
adjustment: PercentPerBlock::forced_import(db, "difficulty_adjustment", version, indexes)?,
|
||||
epoch: PerBlock::forced_import(db, "difficulty_epoch", version, indexes)?,
|
||||
blocks_before_next,
|
||||
days_before_next,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPointsSigned32, Epoch, StoredF32, StoredF64, StoredU32};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedPerBlock, Resolutions, PercentPerBlock};
|
||||
use crate::internal::{LazyPerBlock, PerBlock, Resolutions, PercentPerBlock};
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub raw: Resolutions<StoredF64>,
|
||||
pub as_hash: ComputedPerBlock<StoredF64, M>,
|
||||
pub base: Resolutions<StoredF64>,
|
||||
pub as_hash: LazyPerBlock<StoredF64>,
|
||||
pub adjustment: PercentPerBlock<BasisPointsSigned32, M>,
|
||||
pub epoch: ComputedPerBlock<Epoch, M>,
|
||||
pub blocks_before_next: ComputedPerBlock<StoredU32, M>,
|
||||
pub days_before_next: ComputedPerBlock<StoredF32, M>,
|
||||
pub epoch: PerBlock<Epoch, M>,
|
||||
pub blocks_before_next: PerBlock<StoredU32, M>,
|
||||
pub days_before_next: LazyPerBlock<StoredF32, StoredU32>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user