global: opreturn part 4

This commit is contained in:
nym21
2026-07-19 09:34:03 +02:00
parent 5161ae2012
commit 0fc61d7932
19 changed files with 510 additions and 108 deletions
+1 -1
View File
@@ -362,7 +362,7 @@ impl Computer {
let op_return = scope.spawn(|| {
timed("Computed OP_RETURN", || {
self.op_return.compute(indexer, exit)
self.op_return.compute(indexer, &self.blocks, exit)
})
});
+38 -2
View File
@@ -1,9 +1,13 @@
use brk_error::Result;
use brk_indexer::Indexer;
use brk_types::{OpReturnKind, VSize};
use brk_types::{BasisPoints16, Height, OpReturnKind, StoredU64, VSize};
use vecdb::{AnyVec, Exit, ReadableVec, VecIndex};
use super::{Vecs, vecs::Totals};
use crate::{
blocks,
internal::{PercentPerBlock, RatioU64Bp16},
};
const KIND_COUNT: usize = OpReturnKind::Unknown as usize + 1;
const OLD_STANDARD_MAX_POST_OP_RETURN_BYTES: u64 = 82;
@@ -40,7 +44,12 @@ impl Carrier {
}
impl Vecs {
pub(crate) fn compute(&mut self, indexer: &Indexer, exit: &Exit) -> Result<()> {
pub(crate) fn compute(
&mut self,
indexer: &Indexer,
blocks: &blocks::Vecs,
exit: &Exit,
) -> Result<()> {
self.db.sync_bg_tasks()?;
let starting_lengths = indexer.safe_lengths();
@@ -132,6 +141,23 @@ impl Vecs {
}
self.compute_cumulative(starting_lengths.height, exit)?;
let block_size = &blocks.size.size.cumulative.height;
compute_data_share(
starting_lengths.height,
&mut self.total.data_share,
&self.total.metrics.post_op_return_bytes.cumulative.height,
block_size,
exit,
)?;
for policy in self.policy.iter_mut() {
compute_data_share(
starting_lengths.height,
&mut policy.data_share,
&policy.metrics.total.post_op_return_bytes.cumulative.height,
block_size,
exit,
)?;
}
let exit = exit.clone();
self.db.run_bg(move |db| {
@@ -142,6 +168,16 @@ impl Vecs {
}
}
fn compute_data_share(
max_from: Height,
target: &mut PercentPerBlock<BasisPoints16>,
data: &impl ReadableVec<Height, StoredU64>,
block_size: &impl ReadableVec<Height, StoredU64>,
exit: &Exit,
) -> Result<()> {
target.compute_binary::<StoredU64, StoredU64, RatioU64Bp16>(max_from, data, block_size, exit)
}
fn finalize_transaction(
total: &mut Totals,
by_kind: &mut [Totals; KIND_COUNT],
+2 -2
View File
@@ -3,7 +3,7 @@ use std::path::Path;
use brk_error::Result;
use brk_types::Version;
use super::{ByKind, Metrics, Policy, TotalMetrics, Vecs};
use super::{ByKind, Metrics, Policy, Total, Vecs};
use crate::{
indexes,
internal::{
@@ -20,7 +20,7 @@ impl Vecs {
cached_starts: &Windows<&WindowStartVec>,
) -> Result<Self> {
let db = open_db(parent_path, super::DB_NAME, 1_000_000)?;
let total = TotalMetrics::forced_import(&db, "op_return", version, indexes, cached_starts)?;
let total = Total::forced_import(&db, "op_return", version, indexes, cached_starts)?;
let by_kind = ByKind::try_new(|_, name| {
Metrics::forced_import(
&db,
+1 -1
View File
@@ -4,6 +4,6 @@ mod import;
mod vecs;
pub use by_kind::ByKind;
pub use vecs::{Metrics, Policy, TotalMetrics, Vecs};
pub use vecs::{Metrics, Policy, Total, Vecs};
pub const DB_NAME: &str = "op_return";
+76 -13
View File
@@ -1,12 +1,13 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, StoredU64, VSize, Version};
use brk_types::{BasisPoints16, Height, StoredU64, VSize, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{AnyStoredVec, AnyVec, Database, Exit, Rw, StorageMode, WritableVec};
use super::ByKind;
use crate::{
indexes,
internal::{PerBlockCumulativeRolling, WindowStartVec, Windows},
internal::{PerBlockCumulativeRolling, PercentPerBlock, WindowStartVec, Windows},
};
pub type Series<T, M = Rw> = PerBlockCumulativeRolling<T, T, M>;
@@ -112,6 +113,35 @@ impl TotalMetrics {
}
}
#[derive(Deref, DerefMut, Traversable)]
pub struct Total<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub metrics: TotalMetrics<M>,
pub data_share: PercentPerBlock<BasisPoints16, M>,
}
impl Total {
pub(crate) fn forced_import(
db: &Database,
prefix: &str,
version: Version,
indexes: &indexes::Vecs,
cached_starts: &Windows<&WindowStartVec>,
) -> Result<Self> {
Ok(Self {
metrics: TotalMetrics::forced_import(db, prefix, version, indexes, cached_starts)?,
data_share: PercentPerBlock::forced_import(
db,
&format!("{prefix}_data_share"),
version,
indexes,
)?,
})
}
}
#[derive(Traversable)]
pub struct Metrics<M: StorageMode = Rw> {
pub output_count: Series<StoredU64, M>,
@@ -175,12 +205,41 @@ impl Metrics {
}
}
#[derive(Deref, DerefMut, Traversable)]
pub struct PolicyMetrics<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub metrics: Metrics<M>,
pub data_share: PercentPerBlock<BasisPoints16, M>,
}
impl PolicyMetrics {
fn forced_import(
db: &Database,
prefix: &str,
version: Version,
indexes: &indexes::Vecs,
cached_starts: &Windows<&WindowStartVec>,
) -> Result<Self> {
Ok(Self {
metrics: Metrics::forced_import(db, prefix, version, indexes, cached_starts)?,
data_share: PercentPerBlock::forced_import(
db,
&format!("{prefix}_data_share"),
version,
indexes,
)?,
})
}
}
#[derive(Traversable)]
pub struct Policy<M: StorageMode = Rw> {
pub standard: Metrics<M>,
pub oversized: Metrics<M>,
pub multiple: Metrics<M>,
pub pre_v30_nonstandard: Metrics<M>,
pub standard: PolicyMetrics<M>,
pub oversized: PolicyMetrics<M>,
pub multiple: PolicyMetrics<M>,
pub pre_v30_nonstandard: PolicyMetrics<M>,
}
impl Policy {
@@ -191,7 +250,7 @@ impl Policy {
cached_starts: &Windows<&WindowStartVec>,
) -> Result<Self> {
let import = |name| {
Metrics::forced_import(
PolicyMetrics::forced_import(
db,
&format!("op_return_policy_{name}"),
version,
@@ -208,7 +267,7 @@ impl Policy {
})
}
fn iter(&self) -> impl Iterator<Item = &Metrics> {
fn iter(&self) -> impl Iterator<Item = &PolicyMetrics> {
[
&self.standard,
&self.oversized,
@@ -218,7 +277,7 @@ impl Policy {
.into_iter()
}
fn iter_mut(&mut self) -> impl Iterator<Item = &mut Metrics> {
pub(super) fn iter_mut(&mut self) -> impl Iterator<Item = &mut PolicyMetrics> {
[
&mut self.standard,
&mut self.oversized,
@@ -233,18 +292,22 @@ impl Policy {
pub struct Vecs<M: StorageMode = Rw> {
#[traversable(skip)]
pub(crate) db: Database,
pub total: TotalMetrics<M>,
pub total: Total<M>,
pub by_kind: ByKind<Metrics<M>>,
pub policy: Policy<M>,
}
impl Vecs {
pub(crate) fn min_len(&self) -> usize {
self.by_kind
let len = self
.by_kind
.iter()
.chain(self.policy.iter())
.map(Metrics::len)
.fold(self.total.len(), usize::min)
.fold(self.total.len(), usize::min);
self.policy
.iter()
.map(|metrics| metrics.len())
.fold(len, usize::min)
}
pub(crate) fn validate_and_truncate(&mut self, version: Version, height: Height) -> Result<()> {