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,5 +1,6 @@
|
||||
use brk_error::Result;
|
||||
use brk_types::StoredU32;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::{StoredF32, StoredU32};
|
||||
use vecdb::{Exit, TypedVecIterator};
|
||||
|
||||
use super::super::TARGET_BLOCKS_PER_DAY_F32;
|
||||
@@ -9,10 +10,45 @@ use crate::{ComputeIndexes, indexes};
|
||||
impl Vecs {
|
||||
pub fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// Derive dateindex/period stats from raw difficulty
|
||||
self.raw.derive_from(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
&indexer.vecs.blocks.difficulty,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute difficulty as hash rate equivalent
|
||||
self.as_hash
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
let multiplier = 2.0_f64.powi(32) / 600.0;
|
||||
v.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.difficulty,
|
||||
|(i, v, ..)| (i, StoredF32::from(*v * multiplier)),
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Compute difficulty adjustment percentage
|
||||
self.adjustment
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
v.compute_percentage_change(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.difficulty,
|
||||
1,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Compute epoch by dateindex
|
||||
let mut height_to_difficultyepoch_iter = indexes.height.difficultyepoch.into_iter();
|
||||
self.epoch.compute_all(starting_indexes, exit, |vec| {
|
||||
let mut height_count_iter = indexes.dateindex.height_count.into_iter();
|
||||
@@ -31,7 +67,8 @@ impl Vecs {
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
self.blocks_before_next_difficulty_adjustment.compute_all(
|
||||
// Compute blocks before next adjustment
|
||||
self.blocks_before_next_adjustment.compute_all(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
exit,
|
||||
@@ -46,14 +83,15 @@ impl Vecs {
|
||||
},
|
||||
)?;
|
||||
|
||||
self.days_before_next_difficulty_adjustment.compute_all(
|
||||
// Compute days before next adjustment
|
||||
self.days_before_next_adjustment.compute_all(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
exit,
|
||||
|v| {
|
||||
v.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.blocks_before_next_difficulty_adjustment.height,
|
||||
&self.blocks_before_next_adjustment.height,
|
||||
|(h, blocks, ..)| (h, (*blocks as f32 / TARGET_BLOCKS_PER_DAY_F32).into()),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -1,26 +1,44 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::Version;
|
||||
use vecdb::Database;
|
||||
use vecdb::{Database, IterableCloneableVec};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedBlockLast, ComputedDateLast},
|
||||
internal::{
|
||||
ComputedFromHeightLast, ComputedFromHeightSum, ComputedFromDateLast,
|
||||
ComputedHeightDerivedLast,
|
||||
},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
pub fn forced_import(db: &Database, version: Version, indexes: &indexes::Vecs) -> Result<Self> {
|
||||
pub fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let v2 = Version::TWO;
|
||||
|
||||
Ok(Self {
|
||||
epoch: ComputedDateLast::forced_import(db, "difficultyepoch", version, indexes)?,
|
||||
blocks_before_next_difficulty_adjustment: ComputedBlockLast::forced_import(
|
||||
raw: ComputedHeightDerivedLast::forced_import(
|
||||
db,
|
||||
"difficulty",
|
||||
indexer.vecs.blocks.difficulty.boxed_clone(),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
as_hash: ComputedFromHeightLast::forced_import(db, "difficulty_as_hash", version, indexes)?,
|
||||
adjustment: ComputedFromHeightSum::forced_import(db, "difficulty_adjustment", version, indexes)?,
|
||||
epoch: ComputedFromDateLast::forced_import(db, "difficultyepoch", version, indexes)?,
|
||||
blocks_before_next_adjustment: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
"blocks_before_next_difficulty_adjustment",
|
||||
version + v2,
|
||||
indexes,
|
||||
)?,
|
||||
days_before_next_difficulty_adjustment: ComputedBlockLast::forced_import(
|
||||
days_before_next_adjustment: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
"days_before_next_difficulty_adjustment",
|
||||
version + v2,
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{DifficultyEpoch, StoredF32, StoredU32};
|
||||
use brk_types::{DifficultyEpoch, StoredF32, StoredF64, StoredU32};
|
||||
|
||||
use crate::internal::{ComputedBlockLast, ComputedDateLast};
|
||||
use crate::internal::{
|
||||
ComputedFromHeightLast, ComputedFromHeightSum, ComputedFromDateLast, ComputedHeightDerivedLast,
|
||||
};
|
||||
|
||||
/// Difficulty epoch metrics and countdown
|
||||
/// Difficulty metrics: raw difficulty, derived stats, adjustment, and countdown
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct Vecs {
|
||||
pub epoch: ComputedDateLast<DifficultyEpoch>,
|
||||
pub blocks_before_next_difficulty_adjustment: ComputedBlockLast<StoredU32>,
|
||||
pub days_before_next_difficulty_adjustment: ComputedBlockLast<StoredF32>,
|
||||
/// Raw difficulty with dateindex/period stats - merges with indexer's raw
|
||||
pub raw: ComputedHeightDerivedLast<StoredF64>,
|
||||
pub as_hash: ComputedFromHeightLast<StoredF32>,
|
||||
pub adjustment: ComputedFromHeightSum<StoredF32>,
|
||||
pub epoch: ComputedFromDateLast<DifficultyEpoch>,
|
||||
pub blocks_before_next_adjustment: ComputedFromHeightLast<StoredU32>,
|
||||
pub days_before_next_adjustment: ComputedFromHeightLast<StoredF32>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user