mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: snapshot
This commit is contained in:
@@ -42,14 +42,19 @@ impl Vecs {
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
base: Resolutions::forced_import(
|
||||
value: Resolutions::forced_import(
|
||||
"difficulty",
|
||||
indexer.vecs.blocks.difficulty.read_only_boxed_clone(),
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
as_hash,
|
||||
adjustment: PercentPerBlock::forced_import(db, "difficulty_adjustment", version, indexes)?,
|
||||
adjustment: PercentPerBlock::forced_import(
|
||||
db,
|
||||
"difficulty_adjustment",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
epoch: PerBlock::forced_import(db, "difficulty_epoch", version, indexes)?,
|
||||
blocks_before_next,
|
||||
days_before_next,
|
||||
|
||||
@@ -297,7 +297,7 @@ impl CostBasisFenwick {
|
||||
if total <= 0 {
|
||||
0
|
||||
} else {
|
||||
(range as f64 / total as f64 * 10000.0) as u16
|
||||
(range as f64 / total as f64 * 10000.0).round() as u16
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -461,4 +461,34 @@ impl RealizedState {
|
||||
}
|
||||
Cents::new((self.peak_regret_raw / Sats::ONE_BTC_U128) as u64)
|
||||
}
|
||||
|
||||
/// Raw profit value created for lossless aggregation.
|
||||
#[inline]
|
||||
pub(crate) fn profit_value_created_raw(&self) -> u128 {
|
||||
self.profit_value_created_raw
|
||||
}
|
||||
|
||||
/// Raw profit value destroyed for lossless aggregation.
|
||||
#[inline]
|
||||
pub(crate) fn profit_value_destroyed_raw(&self) -> u128 {
|
||||
self.profit_value_destroyed_raw
|
||||
}
|
||||
|
||||
/// Raw loss value created for lossless aggregation.
|
||||
#[inline]
|
||||
pub(crate) fn loss_value_created_raw(&self) -> u128 {
|
||||
self.loss_value_created_raw
|
||||
}
|
||||
|
||||
/// Raw loss value destroyed for lossless aggregation.
|
||||
#[inline]
|
||||
pub(crate) fn loss_value_destroyed_raw(&self) -> u128 {
|
||||
self.loss_value_destroyed_raw
|
||||
}
|
||||
|
||||
/// Raw peak regret for lossless aggregation.
|
||||
#[inline]
|
||||
pub(crate) fn peak_regret_raw(&self) -> u128 {
|
||||
self.peak_regret_raw
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use brk_indexer::Indexer;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
EmptyOutputIndex, OpReturnIndex, P2AAddressIndex, P2ABytes, P2MSOutputIndex,
|
||||
P2PK33AddressIndex, P2PK33Bytes, P2PK65AddressIndex, P2PK65Bytes, P2PKHAddressIndex,
|
||||
P2PKHBytes, P2SHAddressIndex, P2SHBytes, P2TRAddressIndex, P2TRBytes, P2WPKHAddressIndex,
|
||||
P2WPKHBytes, P2WSHAddressIndex, P2WSHBytes, TxIndex, UnknownOutputIndex, Version,
|
||||
Address, AddressBytes, EmptyOutputIndex, OpReturnIndex, P2AAddressIndex, P2ABytes,
|
||||
P2MSOutputIndex, P2PK33AddressIndex, P2PK33Bytes, P2PK65AddressIndex, P2PK65Bytes,
|
||||
P2PKHAddressIndex, P2PKHBytes, P2SHAddressIndex, P2SHBytes, P2TRAddressIndex, P2TRBytes,
|
||||
P2WPKHAddressIndex, P2WPKHBytes, P2WSHAddressIndex, P2WSHBytes, TxIndex, UnknownOutputIndex,
|
||||
Version,
|
||||
};
|
||||
use vecdb::{LazyVecFrom1, ReadableCloneableVec};
|
||||
|
||||
@@ -21,50 +22,58 @@ pub struct Vecs {
|
||||
pub p2ms: P2MSVecs,
|
||||
pub empty: EmptyVecs,
|
||||
pub unknown: UnknownVecs,
|
||||
pub opreturn: OpReturnVecs,
|
||||
pub op_return: OpReturnVecs,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2PK33Vecs {
|
||||
pub identity:
|
||||
LazyVecFrom1<P2PK33AddressIndex, P2PK33AddressIndex, P2PK33AddressIndex, P2PK33Bytes>,
|
||||
pub address: LazyVecFrom1<P2PK33AddressIndex, Address, P2PK33AddressIndex, P2PK33Bytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2PK65Vecs {
|
||||
pub identity:
|
||||
LazyVecFrom1<P2PK65AddressIndex, P2PK65AddressIndex, P2PK65AddressIndex, P2PK65Bytes>,
|
||||
pub address: LazyVecFrom1<P2PK65AddressIndex, Address, P2PK65AddressIndex, P2PK65Bytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2PKHVecs {
|
||||
pub identity: LazyVecFrom1<P2PKHAddressIndex, P2PKHAddressIndex, P2PKHAddressIndex, P2PKHBytes>,
|
||||
pub address: LazyVecFrom1<P2PKHAddressIndex, Address, P2PKHAddressIndex, P2PKHBytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2SHVecs {
|
||||
pub identity: LazyVecFrom1<P2SHAddressIndex, P2SHAddressIndex, P2SHAddressIndex, P2SHBytes>,
|
||||
pub address: LazyVecFrom1<P2SHAddressIndex, Address, P2SHAddressIndex, P2SHBytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2TRVecs {
|
||||
pub identity: LazyVecFrom1<P2TRAddressIndex, P2TRAddressIndex, P2TRAddressIndex, P2TRBytes>,
|
||||
pub address: LazyVecFrom1<P2TRAddressIndex, Address, P2TRAddressIndex, P2TRBytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2WPKHVecs {
|
||||
pub identity:
|
||||
LazyVecFrom1<P2WPKHAddressIndex, P2WPKHAddressIndex, P2WPKHAddressIndex, P2WPKHBytes>,
|
||||
pub address: LazyVecFrom1<P2WPKHAddressIndex, Address, P2WPKHAddressIndex, P2WPKHBytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2WSHVecs {
|
||||
pub identity: LazyVecFrom1<P2WSHAddressIndex, P2WSHAddressIndex, P2WSHAddressIndex, P2WSHBytes>,
|
||||
pub address: LazyVecFrom1<P2WSHAddressIndex, Address, P2WSHAddressIndex, P2WSHBytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct P2AVecs {
|
||||
pub identity: LazyVecFrom1<P2AAddressIndex, P2AAddressIndex, P2AAddressIndex, P2ABytes>,
|
||||
pub address: LazyVecFrom1<P2AAddressIndex, Address, P2AAddressIndex, P2ABytes>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
@@ -97,6 +106,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2pk33.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2pk33_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2pk33.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2pk65: P2PK65Vecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -105,6 +120,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2pk65.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2pk65_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2pk65.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2pkh: P2PKHVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -113,6 +134,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2pkh.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2pkh_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2pkh.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2sh: P2SHVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -121,6 +148,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2sh.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2sh_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2sh.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2tr: P2TRVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -129,6 +162,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2tr.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2tr_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2tr.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2wpkh: P2WPKHVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -137,6 +176,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2wpkh.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2wpkh_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2wpkh.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2wsh: P2WSHVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -145,6 +190,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2wsh.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2wsh_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2wsh.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2a: P2AVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -153,6 +204,12 @@ impl Vecs {
|
||||
indexer.vecs.addresses.p2a.bytes.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
address: LazyVecFrom1::init(
|
||||
"p2a_address",
|
||||
version,
|
||||
indexer.vecs.addresses.p2a.bytes.read_only_boxed_clone(),
|
||||
|_, bytes| Address::try_from(&AddressBytes::from(bytes)).unwrap(),
|
||||
),
|
||||
},
|
||||
p2ms: P2MSVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
@@ -186,14 +243,14 @@ impl Vecs {
|
||||
|index, _| index,
|
||||
),
|
||||
},
|
||||
opreturn: OpReturnVecs {
|
||||
op_return: OpReturnVecs {
|
||||
identity: LazyVecFrom1::init(
|
||||
"op_return_index",
|
||||
version,
|
||||
indexer
|
||||
.vecs
|
||||
.scripts
|
||||
.opreturn.to_tx_index
|
||||
.op_return.to_tx_index
|
||||
.read_only_boxed_clone(),
|
||||
|index, _| index,
|
||||
),
|
||||
|
||||
@@ -35,12 +35,12 @@ impl Vecs {
|
||||
starting_indexes.height,
|
||||
&self.total.full.cumulative,
|
||||
&inputs_count.full.cumulative,
|
||||
&scripts_count.opreturn.cumulative.height,
|
||||
|(h, output_count, input_count, opreturn_count, ..)| {
|
||||
&scripts_count.op_return.cumulative.height,
|
||||
|(h, output_count, input_count, op_return_count, ..)| {
|
||||
let block_count = u64::from(h + 1_usize);
|
||||
// -1 > genesis output is unspendable
|
||||
let mut utxo_count =
|
||||
*output_count - (*input_count - block_count) - *opreturn_count - 1;
|
||||
*output_count - (*input_count - block_count) - *op_return_count - 1;
|
||||
|
||||
// txid dup: e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468
|
||||
// Block 91_722 https://mempool.space/block/00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e
|
||||
|
||||
@@ -102,12 +102,12 @@ impl Vecs {
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.opreturn
|
||||
self.op_return
|
||||
.compute(starting_indexes.height, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.opreturn.first_index,
|
||||
&indexer.vecs.scripts.opreturn.to_tx_index,
|
||||
&indexer.vecs.scripts.op_return.first_index,
|
||||
&indexer.vecs.scripts.op_return.to_tx_index,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
@@ -46,9 +46,9 @@ impl Vecs {
|
||||
p2tr,
|
||||
p2wpkh,
|
||||
p2wsh,
|
||||
opreturn: PerBlockCumulativeWithSums::forced_import(
|
||||
op_return: PerBlockCumulativeWithSums::forced_import(
|
||||
db,
|
||||
"opreturn_count",
|
||||
"op_return_count",
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
|
||||
@@ -15,7 +15,7 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub p2tr: PerBlockCumulativeWithSums<StoredU64, StoredU64, M>,
|
||||
pub p2wpkh: PerBlockCumulativeWithSums<StoredU64, StoredU64, M>,
|
||||
pub p2wsh: PerBlockCumulativeWithSums<StoredU64, StoredU64, M>,
|
||||
pub opreturn: PerBlockCumulativeWithSums<StoredU64, StoredU64, M>,
|
||||
pub op_return: PerBlockCumulativeWithSums<StoredU64, StoredU64, M>,
|
||||
pub empty_output: PerBlockCumulativeWithSums<StoredU64, StoredU64, M>,
|
||||
pub unknown_output: PerBlockCumulativeWithSums<StoredU64, StoredU64, M>,
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ impl Vecs {
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.opreturn.compute_with(
|
||||
self.op_return.compute_with(
|
||||
starting_indexes.height,
|
||||
prices,
|
||||
exit,
|
||||
@@ -81,14 +81,14 @@ impl Vecs {
|
||||
.value
|
||||
.collect_range_into_at(out_start, out_end, &mut values_buf);
|
||||
|
||||
let mut opreturn_value = Sats::ZERO;
|
||||
let mut op_return_value = Sats::ZERO;
|
||||
for (ot, val) in output_types_buf.iter().zip(values_buf.iter()) {
|
||||
if *ot == OutputType::OpReturn {
|
||||
opreturn_value += *val;
|
||||
op_return_value += *val;
|
||||
}
|
||||
}
|
||||
|
||||
height_vec.truncate_push(height, opreturn_value)?;
|
||||
height_vec.truncate_push(height, op_return_value)?;
|
||||
}
|
||||
|
||||
height_vec.write()?;
|
||||
|
||||
@@ -12,9 +12,9 @@ impl Vecs {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
opreturn: AmountPerBlockCumulative::forced_import(
|
||||
op_return: AmountPerBlockCumulative::forced_import(
|
||||
db,
|
||||
"opreturn_value",
|
||||
"op_return_value",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
|
||||
@@ -5,5 +5,5 @@ use crate::internal::AmountPerBlockCumulative;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub opreturn: AmountPerBlockCumulative<M>,
|
||||
pub op_return: AmountPerBlockCumulative<M>,
|
||||
}
|
||||
|
||||
@@ -14,18 +14,18 @@ impl Vecs {
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.opreturn.compute(
|
||||
self.op_return.compute(
|
||||
starting_indexes.height,
|
||||
prices,
|
||||
exit,
|
||||
|height_vec| {
|
||||
// Validate computed versions against dependencies
|
||||
|
||||
let opreturn_dep_version = scripts.value.opreturn.base.sats.height.version();
|
||||
height_vec.validate_computed_version_or_reset(opreturn_dep_version)?;
|
||||
let op_return_dep_version = scripts.value.op_return.base.sats.height.version();
|
||||
height_vec.validate_computed_version_or_reset(op_return_dep_version)?;
|
||||
|
||||
// Copy per-block opreturn values from scripts
|
||||
let scripts_target = scripts.value.opreturn.base.sats.height.len();
|
||||
// Copy per-block op_return values from scripts
|
||||
let scripts_target = scripts.value.op_return.base.sats.height.len();
|
||||
if scripts_target > 0 {
|
||||
let target_height = Height::from(scripts_target - 1);
|
||||
let current_len = height_vec.len();
|
||||
@@ -35,7 +35,7 @@ impl Vecs {
|
||||
if starting_height <= target_height {
|
||||
let start = starting_height.to_usize();
|
||||
let end = target_height.to_usize() + 1;
|
||||
scripts.value.opreturn.base.sats.height.fold_range_at(
|
||||
scripts.value.op_return.base.sats.height.fold_range_at(
|
||||
start,
|
||||
end,
|
||||
start,
|
||||
@@ -52,9 +52,9 @@ impl Vecs {
|
||||
},
|
||||
)?;
|
||||
|
||||
// 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;
|
||||
// 2. Compute unspendable supply = op_return + unclaimed_rewards + genesis (at height 0)
|
||||
// Get reference to op_return height vec for computing unspendable
|
||||
let op_return_height = &self.op_return.base.sats.height;
|
||||
let unclaimed_height = &mining.rewards.unclaimed.base.sats.height;
|
||||
|
||||
self.unspendable.compute(
|
||||
@@ -63,12 +63,12 @@ impl Vecs {
|
||||
exit,
|
||||
|height_vec| {
|
||||
let unspendable_dep_version =
|
||||
opreturn_height.version() + unclaimed_height.version();
|
||||
op_return_height.version() + unclaimed_height.version();
|
||||
height_vec.validate_computed_version_or_reset(unspendable_dep_version)?;
|
||||
|
||||
let opreturn_target = opreturn_height.len();
|
||||
if opreturn_target > 0 {
|
||||
let target_height = Height::from(opreturn_target - 1);
|
||||
let op_return_target = op_return_height.len();
|
||||
if op_return_target > 0 {
|
||||
let target_height = Height::from(op_return_target - 1);
|
||||
let current_len = height_vec.len();
|
||||
let starting_height =
|
||||
Height::from(current_len.min(starting_indexes.height.to_usize()));
|
||||
@@ -77,14 +77,14 @@ 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| {
|
||||
op_return_height.fold_range_at(start, end, start, |idx, op_return| {
|
||||
let unclaimed = unclaimed_data[idx - start];
|
||||
let genesis = if idx == 0 {
|
||||
Sats::FIFTY_BTC
|
||||
} else {
|
||||
Sats::ZERO
|
||||
};
|
||||
let unspendable = genesis + opreturn + unclaimed;
|
||||
let unspendable = genesis + op_return + unclaimed;
|
||||
height_vec
|
||||
.truncate_push(Height::from(idx), unspendable)
|
||||
.unwrap();
|
||||
|
||||
@@ -13,9 +13,9 @@ impl Vecs {
|
||||
cached_starts: &CachedWindowStarts,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
opreturn: AmountPerBlockCumulativeWithSums::forced_import(
|
||||
op_return: AmountPerBlockCumulativeWithSums::forced_import(
|
||||
db,
|
||||
"opreturn_supply",
|
||||
"op_return_supply",
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
|
||||
@@ -5,6 +5,6 @@ use crate::internal::AmountPerBlockCumulativeWithSums;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub opreturn: AmountPerBlockCumulativeWithSums<M>,
|
||||
pub op_return: AmountPerBlockCumulativeWithSums<M>,
|
||||
pub unspendable: AmountPerBlockCumulativeWithSums<M>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user