mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-26 02:08:10 -07:00
computer: simplified a bunch of things
This commit is contained in:
@@ -3,19 +3,23 @@ use brk_types::{Height, Sats};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableVec, WritableVec, VecIndex};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{ComputeIndexes, mining, scripts};
|
||||
use crate::{ComputeIndexes, blocks, mining, prices, scripts};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
scripts: &scripts::Vecs,
|
||||
mining: &mining::Vecs,
|
||||
count_vecs: &blocks::CountVecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let window_starts = count_vecs.window_starts();
|
||||
|
||||
// 1. Compute opreturn supply - copy per-block opreturn values from scripts
|
||||
self.opreturn
|
||||
.compute(starting_indexes, exit, |height_vec| {
|
||||
.compute(starting_indexes.height, &window_starts, prices, exit, |height_vec| {
|
||||
// Validate computed versions against dependencies
|
||||
|
||||
let opreturn_dep_version = scripts.value.opreturn.sats.height.version();
|
||||
@@ -52,7 +56,7 @@ impl Vecs {
|
||||
let unclaimed_height = &mining.rewards.unclaimed_rewards.sats.height;
|
||||
|
||||
self.unspendable
|
||||
.compute(starting_indexes, exit, |height_vec| {
|
||||
.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)?;
|
||||
|
||||
@@ -3,29 +3,26 @@ use brk_types::Version;
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{indexes, internal::ValueFromHeightSumCum, prices};
|
||||
use crate::{indexes, internal::ValueFromHeightSumCumulative};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
opreturn: ValueFromHeightSumCum::forced_import(
|
||||
opreturn: ValueFromHeightSumCumulative::forced_import(
|
||||
db,
|
||||
"opreturn_supply",
|
||||
version,
|
||||
indexes,
|
||||
prices,
|
||||
)?,
|
||||
unspendable: ValueFromHeightSumCum::forced_import(
|
||||
unspendable: ValueFromHeightSumCumulative::forced_import(
|
||||
db,
|
||||
"unspendable_supply",
|
||||
version,
|
||||
indexes,
|
||||
prices,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use brk_traversable::Traversable;
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::ValueFromHeightSumCum;
|
||||
use crate::internal::ValueFromHeightSumCumulative;
|
||||
|
||||
/// Burned/unspendable supply metrics
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub opreturn: ValueFromHeightSumCum<M>,
|
||||
pub unspendable: ValueFromHeightSumCum<M>,
|
||||
pub opreturn: ValueFromHeightSumCumulative<M>,
|
||||
pub unspendable: ValueFromHeightSumCumulative<M>,
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use brk_error::Result;
|
||||
use brk_types::StoredF32;
|
||||
use vecdb::Exit;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{ComputeIndexes, blocks, distribution, mining, scripts, transactions};
|
||||
use crate::{ComputeIndexes, blocks, distribution, mining, prices, scripts, transactions};
|
||||
|
||||
impl Vecs {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@@ -12,13 +13,14 @@ impl Vecs {
|
||||
blocks: &blocks::Vecs,
|
||||
mining: &mining::Vecs,
|
||||
transactions: &transactions::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
distribution: &distribution::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// 1. Compute burned/unspendable supply
|
||||
self.burned
|
||||
.compute(scripts, mining, starting_indexes, exit)?;
|
||||
.compute(scripts, mining, &blocks.count, prices, starting_indexes, exit)?;
|
||||
|
||||
// 2. Compute inflation rate at height level: (supply[h] - supply[1y_ago]) / supply[1y_ago] * 100
|
||||
let circulating_supply = &distribution.utxo_cohorts.all.metrics.supply.total.sats;
|
||||
@@ -52,7 +54,14 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Note: circulating, market_cap, cap_growth_rate_diff are lazy
|
||||
// 5. Compute cap growth rate diff: market_cap_growth_rate - realized_cap_growth_rate
|
||||
self.cap_growth_rate_diff.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.market_cap_growth_rate.height,
|
||||
&self.realized_cap_growth_rate.height,
|
||||
|(h, a, b, ..)| (h, StoredF32::from(*a - *b)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let _lock = exit.lock();
|
||||
self.db.compact()?;
|
||||
|
||||
@@ -9,11 +9,10 @@ use super::Vecs;
|
||||
use crate::{
|
||||
distribution, indexes,
|
||||
internal::{
|
||||
ComputedFromHeightLast, DifferenceF32, DollarsIdentity,
|
||||
LazyBinaryComputedFromHeightLast, LazyFromHeightLast, LazyValueFromHeightLast,
|
||||
SatsIdentity,
|
||||
ComputedFromHeightLast, DollarsIdentity,
|
||||
LazyFromHeightLast, LazyValueFromHeightLast,
|
||||
SatsIdentity, SatsToBitcoin,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
|
||||
const VERSION: Version = Version::ONE;
|
||||
@@ -23,7 +22,6 @@ impl Vecs {
|
||||
parent: &Path,
|
||||
parent_version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
distribution: &distribution::Vecs,
|
||||
) -> Result<Self> {
|
||||
let db = Database::open(&parent.join(super::DB_NAME))?;
|
||||
@@ -33,14 +31,14 @@ impl Vecs {
|
||||
let supply_metrics = &distribution.utxo_cohorts.all.metrics.supply;
|
||||
|
||||
// Circulating supply - lazy refs to distribution
|
||||
let circulating = LazyValueFromHeightLast::from_block_source::<SatsIdentity, DollarsIdentity>(
|
||||
let circulating = LazyValueFromHeightLast::from_block_source::<SatsIdentity, SatsToBitcoin, DollarsIdentity>(
|
||||
"circulating_supply",
|
||||
&supply_metrics.total,
|
||||
version,
|
||||
);
|
||||
|
||||
// Burned/unspendable supply - computed from scripts
|
||||
let burned = super::burned::Vecs::forced_import(&db, version, indexes, prices)?;
|
||||
let burned = super::burned::Vecs::forced_import(&db, version, indexes)?;
|
||||
|
||||
// Inflation rate
|
||||
let inflation =
|
||||
@@ -51,7 +49,7 @@ impl Vecs {
|
||||
super::velocity::Vecs::forced_import(&db, version, indexes)?;
|
||||
|
||||
// Market cap - lazy identity from distribution supply in USD
|
||||
let market_cap = LazyFromHeightLast::from_lazy_binary_computed::<DollarsIdentity, _, _>(
|
||||
let market_cap = LazyFromHeightLast::from_computed::<DollarsIdentity>(
|
||||
"market_cap",
|
||||
version,
|
||||
supply_metrics.total.usd.height.read_only_boxed_clone(),
|
||||
@@ -72,13 +70,7 @@ impl Vecs {
|
||||
indexes,
|
||||
)?;
|
||||
let cap_growth_rate_diff =
|
||||
LazyBinaryComputedFromHeightLast::forced_import::<DifferenceF32>(
|
||||
"cap_growth_rate_diff",
|
||||
version,
|
||||
market_cap_growth_rate.height.read_only_boxed_clone(),
|
||||
realized_cap_growth_rate.height.read_only_boxed_clone(),
|
||||
indexes,
|
||||
);
|
||||
ComputedFromHeightLast::forced_import(&db, "cap_growth_rate_diff", version, indexes)?;
|
||||
|
||||
let this = Self {
|
||||
db,
|
||||
|
||||
@@ -4,8 +4,7 @@ use vecdb::{Database, Rw, StorageMode};
|
||||
|
||||
use super::{burned, velocity};
|
||||
use crate::internal::{
|
||||
ComputedFromHeightLast, LazyBinaryComputedFromHeightLast, LazyFromHeightLast,
|
||||
LazyValueFromHeightLast,
|
||||
ComputedFromHeightLast, LazyFromHeightLast, LazyValueFromHeightLast,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
@@ -20,5 +19,5 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub market_cap: LazyFromHeightLast<Dollars>,
|
||||
pub market_cap_growth_rate: ComputedFromHeightLast<StoredF32, M>,
|
||||
pub realized_cap_growth_rate: ComputedFromHeightLast<StoredF32, M>,
|
||||
pub cap_growth_rate_diff: LazyBinaryComputedFromHeightLast<StoredF32>,
|
||||
pub cap_growth_rate_diff: ComputedFromHeightLast<StoredF32, M>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user