mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-27 10:48:11 -07:00
global: snapshot
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use brk_error::Result;
|
||||
use brk_types::{Height, Indexes, Sats};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableVec, WritableVec, VecIndex};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableVec, VecIndex, WritableVec};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{blocks, mining, prices, scripts};
|
||||
@@ -18,8 +18,12 @@ impl Vecs {
|
||||
let window_starts = count_vecs.window_starts();
|
||||
|
||||
// 1. Compute opreturn supply - copy per-block opreturn values from scripts
|
||||
self.opreturn
|
||||
.compute(starting_indexes.height, &window_starts, prices, exit, |height_vec| {
|
||||
self.opreturn.compute(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
prices,
|
||||
exit,
|
||||
|height_vec| {
|
||||
// Validate computed versions against dependencies
|
||||
|
||||
let opreturn_dep_version = scripts.value.opreturn.base.sats.height.version();
|
||||
@@ -37,7 +41,9 @@ impl Vecs {
|
||||
let start = starting_height.to_usize();
|
||||
let end = target_height.to_usize() + 1;
|
||||
scripts.value.opreturn.base.sats.height.fold_range_at(
|
||||
start, end, start,
|
||||
start,
|
||||
end,
|
||||
start,
|
||||
|idx, value| {
|
||||
height_vec.truncate_push(Height::from(idx), value).unwrap();
|
||||
idx + 1
|
||||
@@ -48,15 +54,20 @@ impl Vecs {
|
||||
|
||||
height_vec.write()?;
|
||||
Ok(())
|
||||
})?;
|
||||
},
|
||||
)?;
|
||||
|
||||
// 2. Compute unspendable supply = opreturn + unclaimed_rewards + genesis (at height 0)
|
||||
// Get reference to opreturn height vec for computing unspendable
|
||||
let opreturn_height = &self.opreturn.base.sats.height;
|
||||
let unclaimed_height = &mining.rewards.unclaimed_rewards.base.sats.height;
|
||||
|
||||
self.unspendable
|
||||
.compute(starting_indexes.height, &window_starts, prices, exit, |height_vec| {
|
||||
self.unspendable.compute(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
prices,
|
||||
exit,
|
||||
|height_vec| {
|
||||
let unspendable_dep_version =
|
||||
opreturn_height.version() + unclaimed_height.version();
|
||||
height_vec.validate_computed_version_or_reset(unspendable_dep_version)?;
|
||||
@@ -72,22 +83,26 @@ impl Vecs {
|
||||
let start = starting_height.to_usize();
|
||||
let end = target_height.to_usize() + 1;
|
||||
let unclaimed_data = unclaimed_height.collect_range_at(start, end);
|
||||
opreturn_height.fold_range_at(
|
||||
start, end, start,
|
||||
|idx, opreturn| {
|
||||
let unclaimed = unclaimed_data[idx - start];
|
||||
let genesis = if idx == 0 { Sats::FIFTY_BTC } else { Sats::ZERO };
|
||||
let unspendable = genesis + opreturn + unclaimed;
|
||||
height_vec.truncate_push(Height::from(idx), unspendable).unwrap();
|
||||
idx + 1
|
||||
},
|
||||
);
|
||||
opreturn_height.fold_range_at(start, end, start, |idx, opreturn| {
|
||||
let unclaimed = unclaimed_data[idx - start];
|
||||
let genesis = if idx == 0 {
|
||||
Sats::FIFTY_BTC
|
||||
} else {
|
||||
Sats::ZERO
|
||||
};
|
||||
let unspendable = genesis + opreturn + unclaimed;
|
||||
height_vec
|
||||
.truncate_push(Height::from(idx), unspendable)
|
||||
.unwrap();
|
||||
idx + 1
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
height_vec.write()?;
|
||||
Ok(())
|
||||
})?;
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::ValueFromHeightCumulativeSum;
|
||||
|
||||
/// Burned/unspendable supply metrics
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub opreturn: ValueFromHeightCumulativeSum<M>,
|
||||
|
||||
@@ -19,17 +19,26 @@ impl Vecs {
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// 1. Compute burned/unspendable supply
|
||||
self.burned
|
||||
.compute(scripts, mining, &blocks.count, prices, starting_indexes, exit)?;
|
||||
self.burned.compute(
|
||||
scripts,
|
||||
mining,
|
||||
&blocks.count,
|
||||
prices,
|
||||
starting_indexes,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// 2. Compute inflation rate: (supply[h] / supply[1y_ago]) - 1
|
||||
let circulating_supply = &distribution.utxo_cohorts.all.metrics.supply.total.sats;
|
||||
self.inflation_rate.bps.height.compute_rolling_ratio_change(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1y_ago,
|
||||
&circulating_supply.height,
|
||||
exit,
|
||||
)?;
|
||||
self.inflation_rate
|
||||
.bps
|
||||
.height
|
||||
.compute_rolling_ratio_change(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1y_ago,
|
||||
&circulating_supply.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// 3. Compute velocity at height level
|
||||
self.velocity
|
||||
@@ -52,17 +61,25 @@ impl Vecs {
|
||||
.compute_rolling_ratio_change(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1y_ago,
|
||||
&distribution.utxo_cohorts.all.metrics.realized.realized_cap.height,
|
||||
&distribution
|
||||
.utxo_cohorts
|
||||
.all
|
||||
.metrics
|
||||
.realized
|
||||
.realized_cap
|
||||
.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// 5. Compute cap growth rate diff: market - realized
|
||||
self.market_minus_realized_cap_growth_rate.height.compute_subtract(
|
||||
starting_indexes.height,
|
||||
&self.market_cap_growth_rate.bps.height,
|
||||
&self.realized_cap_growth_rate.bps.height,
|
||||
exit,
|
||||
)?;
|
||||
self.market_minus_realized_cap_growth_rate
|
||||
.height
|
||||
.compute_subtract(
|
||||
starting_indexes.height,
|
||||
&self.market_cap_growth_rate.bps.height,
|
||||
&self.realized_cap_growth_rate.bps.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let _lock = exit.lock();
|
||||
self.db.compact()?;
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Cents, Dollars, Sats, Version};
|
||||
use vecdb::{Database, PAGE_SIZE};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
distribution, indexes,
|
||||
internal::{
|
||||
ComputedFromHeight, Identity, LazyFromHeight,
|
||||
LazyValueFromHeight, PercentFromHeight, SatsToBitcoin,
|
||||
ComputedFromHeight, Identity, LazyFromHeight, LazyValueFromHeight, PercentFromHeight,
|
||||
SatsToBitcoin, finalize_db, open_db,
|
||||
},
|
||||
};
|
||||
|
||||
use super::Vecs;
|
||||
|
||||
const VERSION: Version = Version::ONE;
|
||||
|
||||
impl Vecs {
|
||||
@@ -23,8 +22,7 @@ impl Vecs {
|
||||
indexes: &indexes::Vecs,
|
||||
distribution: &distribution::Vecs,
|
||||
) -> Result<Self> {
|
||||
let db = Database::open(&parent.join(super::DB_NAME))?;
|
||||
db.set_min_len(PAGE_SIZE * 10_000_000)?;
|
||||
let db = open_db(parent, super::DB_NAME, 10_000_000)?;
|
||||
|
||||
let version = parent_version + VERSION;
|
||||
let supply_metrics = &distribution.utxo_cohorts.all.metrics.supply;
|
||||
@@ -67,8 +65,12 @@ impl Vecs {
|
||||
version + Version::ONE,
|
||||
indexes,
|
||||
)?;
|
||||
let market_minus_realized_cap_growth_rate =
|
||||
ComputedFromHeight::forced_import(&db, "market_minus_realized_cap_growth_rate", version, indexes)?;
|
||||
let market_minus_realized_cap_growth_rate = ComputedFromHeight::forced_import(
|
||||
&db,
|
||||
"market_minus_realized_cap_growth_rate",
|
||||
version,
|
||||
indexes,
|
||||
)?;
|
||||
|
||||
let this = Self {
|
||||
db,
|
||||
@@ -81,14 +83,7 @@ impl Vecs {
|
||||
realized_cap_growth_rate,
|
||||
market_minus_realized_cap_growth_rate,
|
||||
};
|
||||
|
||||
this.db.retain_regions(
|
||||
this.iter_any_exportable()
|
||||
.flat_map(|v| v.region_names())
|
||||
.collect(),
|
||||
)?;
|
||||
this.db.compact()?;
|
||||
|
||||
finalize_db(&this.db, &this)?;
|
||||
Ok(this)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,7 @@ use brk_types::{BasisPointsSigned32, Dollars};
|
||||
use vecdb::{Database, Rw, StorageMode};
|
||||
|
||||
use super::{burned, velocity};
|
||||
use crate::internal::{
|
||||
ComputedFromHeight, LazyFromHeight, LazyValueFromHeight, PercentFromHeight,
|
||||
};
|
||||
use crate::internal::{ComputedFromHeight, LazyFromHeight, LazyValueFromHeight, PercentFromHeight};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
|
||||
@@ -4,7 +4,6 @@ use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::ComputedFromHeight;
|
||||
|
||||
/// Velocity metrics (annualized volume / circulating supply)
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub btc: ComputedFromHeight<StoredF64, M>,
|
||||
|
||||
Reference in New Issue
Block a user