mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-06-22 12:23:04 -07:00
Compare commits
129 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6c8afc942c | |||
| 1dcbbd801b | |||
| 76869ed2b6 | |||
| b24bfdc15c | |||
| e543e4a5db | |||
| 9b639ef7d1 | |||
| 07bc2d42b8 | |||
| 7a0b4b5890 | |||
| 2210443e37 | |||
| 8bf6570843 | |||
| 26a3b0f5e8 | |||
| 741c957f31 | |||
| e4496742a4 | |||
| ce00de5da8 | |||
| f5c50e69fc | |||
| 9709c2040d | |||
| 3faa989691 | |||
| 84e924b77e | |||
| c5b16e7048 | |||
| c1335cec31 | |||
| bdc3ba1df6 | |||
| 6afce0bbdc | |||
| 327873d010 | |||
| 08175009d2 | |||
| a5d3be465e | |||
| fd2b93367d | |||
| 2a93f51e81 | |||
| 008143ff00 | |||
| d340855c8b | |||
| 78d6d9d6f1 | |||
| 5cc85b0619 | |||
| 7433ce0d0e | |||
| 75a97b4da9 | |||
| c23e0f2a3c | |||
| 08ba4ad996 | |||
| 39da441d14 | |||
| 904ec93668 | |||
| 4cd8d9eb56 | |||
| 283baca848 | |||
| 765261648d | |||
| c3cef71aa3 | |||
| 18d9c166d8 | |||
| 286256ebf0 | |||
| 12aae503c9 | |||
| 95e5168244 | |||
| 5fd9fff9cf | |||
| db5b3887f9 | |||
| 5a3e1b4e6e | |||
| 21a0226a19 | |||
| c5c49f62d1 | |||
| dac66c988d | |||
| 303d168681 | |||
| 1ddb3385e2 | |||
| eb75274dbf | |||
| 3a7887348c | |||
| 0a4cb0601f | |||
| 861e29277c | |||
| c76b149ef9 | |||
| 4c4c6fc840 | |||
| 0c14dfe924 | |||
| 17e531b4ee | |||
| f022f62cce | |||
| e91f1386b1 | |||
| 02f543af38 | |||
| 20c96fb551 | |||
| acd3d6f425 | |||
| 2b15a24b6d | |||
| 7fac0bc613 | |||
| 62f51761ee | |||
| 5340cc288e | |||
| befe3c8fb7 | |||
| 41ec24c81e | |||
| 42b497ff65 | |||
| 01d908a560 | |||
| 42debcce80 | |||
| 8bc993eceb | |||
| 366ac33e23 | |||
| b5a7023bd3 | |||
| 883b38c77c | |||
| 59c767a9e2 | |||
| 9b5bb848f7 | |||
| 5bf06530ce | |||
| 768e6870cb | |||
| 79829ddd53 | |||
| 78082801c6 | |||
| 50771ddccc | |||
| 3a8a9ddecc | |||
| 6cd45c1f1f | |||
| 1a2db43cf5 | |||
| 4840e564f4 | |||
| 744dce932c | |||
| 8dfc1bc932 | |||
| d92cf43c57 | |||
| 099699872e | |||
| 5099903043 | |||
| 982fe47a33 | |||
| 65d5fadd13 | |||
| b55f5255ad | |||
| 83edef4806 | |||
| d4936d889a | |||
| c938cc8eae | |||
| 0558834eef | |||
| 098950fdde | |||
| 91e68a1d1e | |||
| 7172ddb247 | |||
| 96f2e058f7 | |||
| 8782944191 | |||
| ae26db6df2 | |||
| d038141a8a | |||
| f6960c61d6 | |||
| 07fa2d2c9a | |||
| 82c6d69a0b | |||
| d4dc1b9e49 | |||
| 24d2b7b142 | |||
| b6e56c4e9f | |||
| 45c77a4c3b | |||
| 09af190ac0 | |||
| d24f3691cb | |||
| daaaa15483 | |||
| 041652d85d | |||
| 17570e12b8 | |||
| 78172734db | |||
| 19d4a193ff | |||
| 66680368b6 | |||
| b4ded21ea3 | |||
| 7412373d8a | |||
| 259960b80b | |||
| 18bb4186a8 | |||
| 6d3307c0df |
@@ -39,6 +39,7 @@ flamegraph.svg
|
||||
|
||||
# AI
|
||||
.claude/settings*
|
||||
!CLAUDE.md
|
||||
|
||||
# Expand
|
||||
expand.rs
|
||||
|
||||
Generated
+231
-370
File diff suppressed because it is too large
Load Diff
+36
-36
@@ -4,7 +4,7 @@ members = ["crates/*"]
|
||||
package.description = "The Bitcoin Research Kit is a suite of tools designed to extract, compute and display data stored on a Bitcoin Core node"
|
||||
package.license = "MIT"
|
||||
package.edition = "2024"
|
||||
package.version = "0.2.2"
|
||||
package.version = "0.3.0-beta.7"
|
||||
package.homepage = "https://bitcoinresearchkit.org"
|
||||
package.repository = "https://github.com/bitcoinresearchkit/brk"
|
||||
package.readme = "README.md"
|
||||
@@ -36,58 +36,57 @@ inherits = "release"
|
||||
debug = true
|
||||
|
||||
[workspace.dependencies]
|
||||
aide = { version = "0.16.0-alpha.3", features = ["axum-json", "axum-query"] }
|
||||
axum = { version = "0.8.8", default-features = false, features = ["http1", "json", "query", "tokio", "tracing"] }
|
||||
aide = { version = "0.16.0-alpha.4", features = ["axum-json", "axum-query"] }
|
||||
axum = { version = "0.8.9", default-features = false, features = ["http1", "json", "query", "tokio", "tracing"] }
|
||||
bitcoin = { version = "0.32.8", features = ["serde"] }
|
||||
bitcoincore-rpc = "0.19.0"
|
||||
brk_alloc = { version = "0.2.2", path = "crates/brk_alloc" }
|
||||
brk_bencher = { version = "0.2.2", path = "crates/brk_bencher" }
|
||||
brk_bindgen = { version = "0.2.2", path = "crates/brk_bindgen" }
|
||||
brk_cli = { version = "0.2.2", path = "crates/brk_cli" }
|
||||
brk_client = { version = "0.2.2", path = "crates/brk_client" }
|
||||
brk_cohort = { version = "0.2.2", path = "crates/brk_cohort" }
|
||||
brk_computer = { version = "0.2.2", path = "crates/brk_computer" }
|
||||
brk_error = { version = "0.2.2", path = "crates/brk_error" }
|
||||
brk_fetcher = { version = "0.2.2", path = "crates/brk_fetcher" }
|
||||
brk_indexer = { version = "0.2.2", path = "crates/brk_indexer" }
|
||||
brk_iterator = { version = "0.2.2", path = "crates/brk_iterator" }
|
||||
brk_logger = { version = "0.2.2", path = "crates/brk_logger" }
|
||||
brk_mempool = { version = "0.2.2", path = "crates/brk_mempool" }
|
||||
brk_oracle = { version = "0.2.2", path = "crates/brk_oracle" }
|
||||
brk_query = { version = "0.2.2", path = "crates/brk_query", features = ["tokio"] }
|
||||
brk_reader = { version = "0.2.2", path = "crates/brk_reader" }
|
||||
brk_rpc = { version = "0.2.2", path = "crates/brk_rpc" }
|
||||
brk_server = { version = "0.2.2", path = "crates/brk_server" }
|
||||
brk_store = { version = "0.2.2", path = "crates/brk_store" }
|
||||
brk_traversable = { version = "0.2.2", path = "crates/brk_traversable", features = ["pco", "derive"] }
|
||||
brk_traversable_derive = { version = "0.2.2", path = "crates/brk_traversable_derive" }
|
||||
brk_types = { version = "0.2.2", path = "crates/brk_types" }
|
||||
brk_website = { version = "0.2.2", path = "crates/brk_website" }
|
||||
brk_alloc = { version = "0.3.0-beta.7", path = "crates/brk_alloc" }
|
||||
brk_bencher = { version = "0.3.0-beta.7", path = "crates/brk_bencher" }
|
||||
brk_bindgen = { version = "0.3.0-beta.7", path = "crates/brk_bindgen" }
|
||||
brk_cli = { version = "0.3.0-beta.7", path = "crates/brk_cli" }
|
||||
brk_client = { version = "0.3.0-beta.7", path = "crates/brk_client" }
|
||||
brk_cohort = { version = "0.3.0-beta.7", path = "crates/brk_cohort" }
|
||||
brk_computer = { version = "0.3.0-beta.7", path = "crates/brk_computer" }
|
||||
brk_error = { version = "0.3.0-beta.7", path = "crates/brk_error" }
|
||||
brk_fetcher = { version = "0.3.0-beta.7", path = "crates/brk_fetcher" }
|
||||
brk_indexer = { version = "0.3.0-beta.7", path = "crates/brk_indexer" }
|
||||
brk_iterator = { version = "0.3.0-beta.7", path = "crates/brk_iterator" }
|
||||
brk_logger = { version = "0.3.0-beta.7", path = "crates/brk_logger" }
|
||||
brk_mempool = { version = "0.3.0-beta.7", path = "crates/brk_mempool" }
|
||||
brk_oracle = { version = "0.3.0-beta.7", path = "crates/brk_oracle" }
|
||||
brk_query = { version = "0.3.0-beta.7", path = "crates/brk_query", features = ["tokio"] }
|
||||
brk_reader = { version = "0.3.0-beta.7", path = "crates/brk_reader" }
|
||||
brk_rpc = { version = "0.3.0-beta.7", path = "crates/brk_rpc" }
|
||||
brk_server = { version = "0.3.0-beta.7", path = "crates/brk_server" }
|
||||
brk_store = { version = "0.3.0-beta.7", path = "crates/brk_store" }
|
||||
brk_traversable = { version = "0.3.0-beta.7", path = "crates/brk_traversable", features = ["pco", "derive"] }
|
||||
brk_traversable_derive = { version = "0.3.0-beta.7", path = "crates/brk_traversable_derive" }
|
||||
brk_types = { version = "0.3.0-beta.7", path = "crates/brk_types" }
|
||||
brk_website = { version = "0.3.0-beta.7", path = "crates/brk_website" }
|
||||
byteview = "0.10.1"
|
||||
color-eyre = "0.6.5"
|
||||
corepc-client = { package = "brk-corepc-client", version = "0.11.0", features = ["client-sync"] }
|
||||
corepc-jsonrpc = { package = "brk-corepc-jsonrpc", version = "0.19.0", features = ["simple_http"], default-features = false }
|
||||
corepc-jsonrpc = { package = "jsonrpc", version = "0.19.0", features = ["simple_http"], default-features = false }
|
||||
corepc-types = { version = "0.12.0", features = ["std"], default-features = false }
|
||||
derive_more = { version = "2.1.1", features = ["deref", "deref_mut"] }
|
||||
fjall = "3.1.2"
|
||||
indexmap = { version = "2.13.0", features = ["serde"] }
|
||||
jiff = { version = "0.2.23", features = ["perf-inline", "tz-system"], default-features = false }
|
||||
fjall = "=3.0.4"
|
||||
indexmap = { version = "2.14.0", features = ["serde"] }
|
||||
jiff = { version = "0.2.24", features = ["perf-inline", "tz-system"], default-features = false }
|
||||
owo-colors = "4.3.0"
|
||||
parking_lot = "0.12.5"
|
||||
pco = "1.0.1"
|
||||
rayon = "1.11.0"
|
||||
rustc-hash = "2.1.1"
|
||||
rayon = "1.12.0"
|
||||
rustc-hash = "2.1.2"
|
||||
schemars = { version = "1.2.1", features = ["indexmap2"] }
|
||||
serde = "1.0.228"
|
||||
serde_bytes = "0.11.19"
|
||||
serde_derive = "1.0.228"
|
||||
serde_json = { version = "1.0.149", features = ["float_roundtrip", "preserve_order"] }
|
||||
smallvec = "1.15.1"
|
||||
tokio = { version = "1.50.0", features = ["rt-multi-thread"] }
|
||||
tokio = { version = "1.52.1", features = ["rt-multi-thread"] }
|
||||
tower-http = { version = "0.6.8", features = ["catch-panic", "compression-br", "compression-gzip", "compression-zstd", "cors", "normalize-path", "timeout", "trace"] }
|
||||
tower-layer = "0.3"
|
||||
tracing = { version = "0.1", default-features = false, features = ["std"] }
|
||||
ureq = { version = "3.3.0", features = ["json"] }
|
||||
vecdb = { version = "0.7.2", features = ["derive", "serde_json", "pco", "schemars"] }
|
||||
vecdb = { version = "0.10.2", features = ["derive", "serde_json", "pco", "schemars"] }
|
||||
# vecdb = { path = "../anydb/crates/vecdb", features = ["derive", "serde_json", "pco", "schemars"] }
|
||||
|
||||
[workspace.metadata.release]
|
||||
@@ -95,6 +94,7 @@ shared-version = true
|
||||
tag-name = "v{{version}}"
|
||||
pre-release-commit-message = "release: v{{version}}"
|
||||
tag-message = "release: v{{version}}"
|
||||
allow-branch = ["main", "next"]
|
||||
|
||||
[workspace.metadata.dist]
|
||||
cargo-dist-version = "0.30.2"
|
||||
|
||||
@@ -64,7 +64,7 @@ brk_mempool = { workspace = true, optional = true }
|
||||
brk_oracle = { workspace = true, optional = true }
|
||||
brk_query = { workspace = true, optional = true }
|
||||
brk_reader = { workspace = true, optional = true }
|
||||
brk_rpc = { workspace = true, optional = true, features = ["corepc"] }
|
||||
brk_rpc = { workspace = true, optional = true }
|
||||
brk_server = { workspace = true, optional = true }
|
||||
brk_store = { workspace = true, optional = true }
|
||||
brk_traversable = { workspace = true, optional = true }
|
||||
|
||||
@@ -8,5 +8,5 @@ homepage.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
libmimalloc-sys = { version = "0.1.44", features = ["extended"] }
|
||||
mimalloc = { version = "0.1.48", features = ["v3"] }
|
||||
libmimalloc-sys = { version = "0.1.47", features = ["extended"] }
|
||||
mimalloc = { version = "0.1.50" }
|
||||
|
||||
@@ -12,6 +12,6 @@ brk_cohort = { workspace = true }
|
||||
brk_query = { workspace = true }
|
||||
brk_types = { workspace = true }
|
||||
indexmap = { workspace = true }
|
||||
oas3 = "0.20"
|
||||
oas3 = "0.21"
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
//! This module detects repeating tree structures and analyzes them
|
||||
//! using the bottom-up name deconstruction algorithm.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::{
|
||||
cmp::Reverse,
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
};
|
||||
|
||||
use brk_types::{TreeNode, extract_json_type};
|
||||
|
||||
@@ -111,7 +114,7 @@ pub fn detect_structural_patterns(
|
||||
// Also collects node bases for each tree path
|
||||
let node_bases = analyze_pattern_modes(tree, &mut patterns, &pattern_lookup);
|
||||
|
||||
patterns.sort_by(|a, b| b.fields.len().cmp(&a.fields.len()));
|
||||
patterns.sort_by_key(|p| Reverse(p.fields.len()));
|
||||
(patterns, concrete_to_pattern, type_mappings, node_bases)
|
||||
}
|
||||
|
||||
|
||||
@@ -108,7 +108,13 @@ fn fill_mixed_empty_field_parts(
|
||||
// Recurse first (bottom-up)
|
||||
for (field_name, child_node) in children {
|
||||
let child_path = build_child_path(path, field_name);
|
||||
fill_mixed_empty_field_parts(child_node, &child_path, pattern_lookup, patterns, node_bases);
|
||||
fill_mixed_empty_field_parts(
|
||||
child_node,
|
||||
&child_path,
|
||||
pattern_lookup,
|
||||
patterns,
|
||||
node_bases,
|
||||
);
|
||||
}
|
||||
|
||||
// Check if this node has mixed empty/non-empty field_parts
|
||||
@@ -351,16 +357,18 @@ fn try_embedded_disc(
|
||||
}
|
||||
|
||||
/// Strategy 2: suffix discriminator (e.g., all field_parts differ by `_4y` suffix)
|
||||
fn try_suffix_disc(
|
||||
majority: &[&InstanceAnalysis],
|
||||
fields: &[PatternField],
|
||||
) -> Option<PatternMode> {
|
||||
fn try_suffix_disc(majority: &[&InstanceAnalysis], fields: &[PatternField]) -> Option<PatternMode> {
|
||||
let first = &majority[0];
|
||||
|
||||
// Use a non-empty field to detect the suffix
|
||||
let ref_field = fields
|
||||
.iter()
|
||||
.find(|f| first.field_parts.get(&f.name).is_some_and(|v| !v.is_empty()))
|
||||
.find(|f| {
|
||||
first
|
||||
.field_parts
|
||||
.get(&f.name)
|
||||
.is_some_and(|v| !v.is_empty())
|
||||
})
|
||||
.map(|f| &f.name)?;
|
||||
let ref_first = first.field_parts.get(ref_field)?;
|
||||
|
||||
@@ -763,19 +771,51 @@ mod tests {
|
||||
fn test_embedded_disc_percentile_bands() {
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "bps".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "price".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "ratio".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "bps".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "price".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "ratio".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
];
|
||||
let pct99 = InstanceAnalysis {
|
||||
base: "realized_price".into(),
|
||||
field_parts: [("bps".into(), "ratio_pct99_bps".into()), ("price".into(), "pct99".into()), ("ratio".into(), "ratio_pct99".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [
|
||||
("bps".into(), "ratio_pct99_bps".into()),
|
||||
("price".into(), "pct99".into()),
|
||||
("ratio".into(), "ratio_pct99".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let pct1 = InstanceAnalysis {
|
||||
base: "realized_price".into(),
|
||||
field_parts: [("bps".into(), "ratio_pct1_bps".into()), ("price".into(), "pct1".into()), ("ratio".into(), "ratio_pct1".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [
|
||||
("bps".into(), "ratio_pct1_bps".into()),
|
||||
("price".into(), "pct1".into()),
|
||||
("ratio".into(), "ratio_pct1".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[pct99, pct1], &fields);
|
||||
assert!(mode.is_some());
|
||||
@@ -793,19 +833,51 @@ mod tests {
|
||||
fn test_suffix_disc_period_windows() {
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "p1sd".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "sd".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "zscore".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "p1sd".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "sd".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "zscore".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
];
|
||||
let all_time = InstanceAnalysis {
|
||||
base: "realized_price".into(),
|
||||
field_parts: [("p1sd".into(), "p1sd".into()), ("sd".into(), "ratio_sd".into()), ("zscore".into(), "ratio_zscore".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [
|
||||
("p1sd".into(), "p1sd".into()),
|
||||
("sd".into(), "ratio_sd".into()),
|
||||
("zscore".into(), "ratio_zscore".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let four_year = InstanceAnalysis {
|
||||
base: "realized_price".into(),
|
||||
field_parts: [("p1sd".into(), "p1sd_4y".into()), ("sd".into(), "ratio_sd_4y".into()), ("zscore".into(), "ratio_zscore_4y".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [
|
||||
("p1sd".into(), "p1sd_4y".into()),
|
||||
("sd".into(), "ratio_sd_4y".into()),
|
||||
("zscore".into(), "ratio_zscore_4y".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[all_time, four_year], &fields);
|
||||
assert!(mode.is_some());
|
||||
@@ -823,18 +895,39 @@ mod tests {
|
||||
fn test_suffix_disc_with_empty_fields() {
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "band".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "sd".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "band".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "sd".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
];
|
||||
let all_time = InstanceAnalysis {
|
||||
base: "price".into(),
|
||||
field_parts: [("band".into(), "".into()), ("sd".into(), "ratio_sd".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [("band".into(), "".into()), ("sd".into(), "ratio_sd".into())]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let four_year = InstanceAnalysis {
|
||||
base: "price".into(),
|
||||
field_parts: [("band".into(), "".into()), ("sd".into(), "ratio_sd_4y".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [
|
||||
("band".into(), "".into()),
|
||||
("sd".into(), "ratio_sd_4y".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[all_time, four_year], &fields);
|
||||
assert!(mode.is_some());
|
||||
@@ -851,18 +944,39 @@ mod tests {
|
||||
fn test_suffix_disc_empty_to_nonempty() {
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "all".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "sth".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "all".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "sth".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
];
|
||||
let regular = InstanceAnalysis {
|
||||
base: "supply".into(),
|
||||
field_parts: [("all".into(), "".into()), ("sth".into(), "sth_".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [("all".into(), "".into()), ("sth".into(), "sth_".into())]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let profitability = InstanceAnalysis {
|
||||
base: "utxos_in_profit".into(),
|
||||
field_parts: [("all".into(), "supply".into()), ("sth".into(), "sth_supply".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [
|
||||
("all".into(), "supply".into()),
|
||||
("sth".into(), "sth_supply".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[regular, profitability], &fields);
|
||||
assert!(mode.is_some());
|
||||
@@ -879,43 +993,91 @@ mod tests {
|
||||
fn test_outlier_rejects_pattern() {
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "ratio".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "value".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "ratio".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "value".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
];
|
||||
// SOPR case: one instance has outlier naming (no common prefix)
|
||||
let normal = InstanceAnalysis {
|
||||
base: "series".into(),
|
||||
field_parts: [("ratio".into(), "ratio".into()), ("value".into(), "value".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [
|
||||
("ratio".into(), "ratio".into()),
|
||||
("value".into(), "value".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let outlier = InstanceAnalysis {
|
||||
base: "".into(),
|
||||
field_parts: [("ratio".into(), "asopr".into()), ("value".into(), "adj_value".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: true,
|
||||
field_parts: [
|
||||
("ratio".into(), "asopr".into()),
|
||||
("value".into(), "adj_value".into()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: true,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[normal, outlier], &fields);
|
||||
assert!(mode.is_some(), "Outlier should be filtered out, leaving a valid pattern from non-outlier instances");
|
||||
assert!(
|
||||
mode.is_some(),
|
||||
"Outlier should be filtered out, leaving a valid pattern from non-outlier instances"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unanimity_rejects_disagreeing_instances() {
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "a".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "b".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "a".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "b".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
];
|
||||
let inst1 = InstanceAnalysis {
|
||||
base: "x".into(),
|
||||
field_parts: [("a".into(), "foo".into()), ("b".into(), "bar".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [("a".into(), "foo".into()), ("b".into(), "bar".into())]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let inst2 = InstanceAnalysis {
|
||||
base: "y".into(),
|
||||
field_parts: [("a".into(), "baz".into()), ("b".into(), "qux".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [("a".into(), "baz".into()), ("b".into(), "qux".into())]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[inst1, inst2], &fields);
|
||||
assert!(mode.is_none(), "Should be non-parameterizable when no pattern detected");
|
||||
assert!(
|
||||
mode.is_none(),
|
||||
"Should be non-parameterizable when no pattern detected"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -925,20 +1087,43 @@ mod tests {
|
||||
// Should keep identity (empty parts) so both children receive acc unchanged.
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "absolute".into(), rust_type: "TypeA".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "rate".into(), rust_type: "TypeB".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "absolute".into(),
|
||||
rust_type: "TypeA".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "rate".into(),
|
||||
rust_type: "TypeB".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
];
|
||||
let inst = InstanceAnalysis {
|
||||
base: "supply_delta".into(),
|
||||
field_parts: [("absolute".into(), "".into()), ("rate".into(), "".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: false,
|
||||
field_parts: [("absolute".into(), "".into()), ("rate".into(), "".into())]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: false,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[inst], &fields);
|
||||
assert!(mode.is_some());
|
||||
match mode.unwrap() {
|
||||
PatternMode::Suffix { relatives } => {
|
||||
assert_eq!(relatives.get("absolute"), Some(&"".to_string()), "absolute should be identity");
|
||||
assert_eq!(relatives.get("rate"), Some(&"".to_string()), "rate should be identity");
|
||||
assert_eq!(
|
||||
relatives.get("absolute"),
|
||||
Some(&"".to_string()),
|
||||
"absolute should be identity"
|
||||
);
|
||||
assert_eq!(
|
||||
relatives.get("rate"),
|
||||
Some(&"".to_string()),
|
||||
"rate should be identity"
|
||||
);
|
||||
}
|
||||
other => panic!("Expected Suffix with identity, got {:?}", other),
|
||||
}
|
||||
@@ -975,16 +1160,26 @@ mod tests {
|
||||
// Parent patterns containing non-parameterizable children should also
|
||||
// be detected via metadata.is_parameterizable (recursive check).
|
||||
use std::collections::BTreeSet;
|
||||
let fields = vec![
|
||||
PatternField { name: "a".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
];
|
||||
let fields = vec![PatternField {
|
||||
name: "a".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
}];
|
||||
let inst = InstanceAnalysis {
|
||||
base: "".into(),
|
||||
field_parts: [("a".into(), "standalone_name".into())].into_iter().collect(),
|
||||
is_suffix_mode: true, has_outlier: true,
|
||||
field_parts: [("a".into(), "standalone_name".into())]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
is_suffix_mode: true,
|
||||
has_outlier: true,
|
||||
};
|
||||
let mode = determine_pattern_mode(&[inst], &fields);
|
||||
assert!(mode.is_none(), "Pattern with outlier should be non-parameterizable");
|
||||
assert!(
|
||||
mode.is_none(),
|
||||
"Pattern with outlier should be non-parameterizable"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -998,9 +1193,27 @@ mod tests {
|
||||
let pattern = StructuralPattern {
|
||||
name: "TestPattern".into(),
|
||||
fields: vec![
|
||||
PatternField { name: "_0sd".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "p1sd".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField { name: "sd".into(), rust_type: "T".into(), json_type: "n".into(), indexes: BTreeSet::new(), type_param: None },
|
||||
PatternField {
|
||||
name: "_0sd".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "p1sd".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
PatternField {
|
||||
name: "sd".into(),
|
||||
rust_type: "T".into(),
|
||||
json_type: "n".into(),
|
||||
indexes: BTreeSet::new(),
|
||||
type_param: None,
|
||||
},
|
||||
],
|
||||
mode: Some(PatternMode::Templated {
|
||||
templates: [
|
||||
@@ -1059,9 +1272,15 @@ mod tests {
|
||||
assert_eq!(analysis.field_parts.get("loss"), Some(&"".to_string()));
|
||||
assert_eq!(analysis.field_parts.get("supply"), Some(&"".to_string()));
|
||||
// others should be non-empty
|
||||
assert_eq!(analysis.field_parts.get("cap"), Some(&"realized_cap".to_string()));
|
||||
assert_eq!(
|
||||
analysis.field_parts.get("cap"),
|
||||
Some(&"realized_cap".to_string())
|
||||
);
|
||||
assert_eq!(analysis.field_parts.get("mvrv"), Some(&"mvrv".to_string()));
|
||||
assert_eq!(analysis.field_parts.get("price"), Some(&"realized_price".to_string()));
|
||||
assert_eq!(
|
||||
analysis.field_parts.get("price"),
|
||||
Some(&"realized_price".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1111,12 +1330,20 @@ mod tests {
|
||||
&mut path_to_pattern,
|
||||
);
|
||||
|
||||
let result = node_bases.get("test").expect("should have node_bases entry");
|
||||
let result = node_bases
|
||||
.get("test")
|
||||
.expect("should have node_bases entry");
|
||||
assert_eq!(result.base, "utxos");
|
||||
assert!(!result.has_outlier);
|
||||
assert_eq!(result.field_parts.get("cap"), Some(&"realized_cap".to_string()));
|
||||
assert_eq!(
|
||||
result.field_parts.get("cap"),
|
||||
Some(&"realized_cap".to_string())
|
||||
);
|
||||
assert_eq!(result.field_parts.get("mvrv"), Some(&"mvrv".to_string()));
|
||||
// loss branch returns base "utxos_realized_loss" which yields field_part "realized_loss"
|
||||
assert_eq!(result.field_parts.get("loss"), Some(&"realized_loss".to_string()));
|
||||
assert_eq!(
|
||||
result.field_parts.get("loss"),
|
||||
Some(&"realized_loss".to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use brk_cohort::{
|
||||
AGE_RANGE_NAMES, AMOUNT_RANGE_NAMES, CLASS_NAMES, EPOCH_NAMES, LOSS_NAMES,
|
||||
OVER_AGE_NAMES, OVER_AMOUNT_NAMES, PROFITABILITY_RANGE_NAMES, PROFIT_NAMES,
|
||||
SPENDABLE_TYPE_NAMES, TERM_NAMES, UNDER_AGE_NAMES, UNDER_AMOUNT_NAMES,
|
||||
AGE_RANGE_NAMES, AMOUNT_RANGE_NAMES, CLASS_NAMES, EPOCH_NAMES, LOSS_NAMES, OVER_AGE_NAMES,
|
||||
OVER_AMOUNT_NAMES, PROFIT_NAMES, PROFITABILITY_RANGE_NAMES, SPENDABLE_TYPE_NAMES, TERM_NAMES,
|
||||
UNDER_AGE_NAMES, UNDER_AMOUNT_NAMES,
|
||||
};
|
||||
use brk_types::{Index, PoolSlug, pools};
|
||||
use serde::Serialize;
|
||||
@@ -31,7 +31,7 @@ impl ClientConstants {
|
||||
|
||||
let pools = pools();
|
||||
let mut sorted_pools: Vec<_> = pools.iter().collect();
|
||||
sorted_pools.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||||
sorted_pools.sort_by_key(|p| p.name.to_lowercase());
|
||||
let pool_map: BTreeMap<PoolSlug, &'static str> =
|
||||
sorted_pools.iter().map(|p| (p.slug(), p.name)).collect();
|
||||
|
||||
@@ -64,7 +64,10 @@ impl CohortConstants {
|
||||
("AMOUNT_RANGE_NAMES", to_value(&AMOUNT_RANGE_NAMES)),
|
||||
("OVER_AMOUNT_NAMES", to_value(&OVER_AMOUNT_NAMES)),
|
||||
("UNDER_AMOUNT_NAMES", to_value(&UNDER_AMOUNT_NAMES)),
|
||||
("PROFITABILITY_RANGE_NAMES", to_value(&PROFITABILITY_RANGE_NAMES)),
|
||||
(
|
||||
"PROFITABILITY_RANGE_NAMES",
|
||||
to_value(&PROFITABILITY_RANGE_NAMES),
|
||||
),
|
||||
("PROFIT_NAMES", to_value(&PROFIT_NAMES)),
|
||||
("LOSS_NAMES", to_value(&LOSS_NAMES)),
|
||||
]
|
||||
|
||||
@@ -8,7 +8,9 @@ use std::fmt::Write;
|
||||
|
||||
use brk_types::SeriesLeafWithSchema;
|
||||
|
||||
use crate::{ClientMetadata, LanguageSyntax, PatternBaseResult, PatternField, PatternMode, StructuralPattern};
|
||||
use crate::{
|
||||
ClientMetadata, LanguageSyntax, PatternBaseResult, PatternField, PatternMode, StructuralPattern,
|
||||
};
|
||||
|
||||
/// Create a path suffix from a name.
|
||||
fn path_suffix(name: &str) -> String {
|
||||
@@ -33,9 +35,7 @@ fn compute_parameterized_value<S: LanguageSyntax>(
|
||||
if let Some(child_pattern) = metadata.find_pattern(&field.rust_type)
|
||||
&& child_pattern.is_templated()
|
||||
{
|
||||
let disc_template = pattern
|
||||
.get_field_part(&field.name)
|
||||
.unwrap_or(&field.name);
|
||||
let disc_template = pattern.get_field_part(&field.name).unwrap_or(&field.name);
|
||||
let disc_arg = syntax.disc_arg_expr(disc_template);
|
||||
let acc_arg = syntax.owned_expr("acc");
|
||||
return syntax.constructor(&field.rust_type, &format!("{acc_arg}, {disc_arg}"));
|
||||
|
||||
@@ -125,8 +125,8 @@ pub fn prepare_tree_node<'a>(
|
||||
p.is_suffix_mode() == base_result.is_suffix_mode
|
||||
&& p.field_parts_match(&base_result.field_parts)
|
||||
});
|
||||
let is_parameterizable = matching_pattern
|
||||
.is_none_or(|p| metadata.is_parameterizable(&p.name));
|
||||
let is_parameterizable =
|
||||
matching_pattern.is_none_or(|p| metadata.is_parameterizable(&p.name));
|
||||
|
||||
// should_inline determines if we generate an inline struct type
|
||||
let should_inline = !is_leaf
|
||||
|
||||
@@ -69,31 +69,49 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
writeln!(
|
||||
output,
|
||||
" * @param {{{{ signal?: AbortSignal, onUpdate?: (value: {}) => void }}}} [options]",
|
||||
return_type
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(output, " * @returns {{Promise<{}>}}", return_type).unwrap();
|
||||
writeln!(output, " */").unwrap();
|
||||
|
||||
let params = build_method_params(endpoint);
|
||||
writeln!(output, " async {}({}) {{", method_name, params).unwrap();
|
||||
let params_with_opts = if params.is_empty() {
|
||||
"{ signal, onUpdate } = {}".to_string()
|
||||
} else {
|
||||
format!("{}, {{ signal, onUpdate }} = {{}}", params)
|
||||
};
|
||||
writeln!(output, " async {}({}) {{", method_name, params_with_opts).unwrap();
|
||||
|
||||
let path = build_path_template(&endpoint.path, &endpoint.path_params);
|
||||
|
||||
let fetch_call = if endpoint.returns_json() {
|
||||
"this.getJson(path, { signal, onUpdate })"
|
||||
} else {
|
||||
"this.getText(path, { signal, onUpdate })"
|
||||
};
|
||||
|
||||
if endpoint.query_params.is_empty() {
|
||||
writeln!(output, " return this.getJson(`{}`);", path).unwrap();
|
||||
writeln!(output, " const path = `{}`;", path).unwrap();
|
||||
} else {
|
||||
writeln!(output, " const params = new URLSearchParams();").unwrap();
|
||||
for param in &endpoint.query_params {
|
||||
let ident = sanitize_ident(¶m.name);
|
||||
if param.required {
|
||||
writeln!(
|
||||
output,
|
||||
" params.set('{}', String({}));",
|
||||
param.name, param.name
|
||||
param.name, ident
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
writeln!(
|
||||
output,
|
||||
" if ({} !== undefined) params.set('{}', String({}));",
|
||||
param.name, param.name, param.name
|
||||
ident, param.name, ident
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
@@ -105,17 +123,17 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
path
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
if endpoint.supports_csv {
|
||||
writeln!(output, " if (format === 'csv') {{").unwrap();
|
||||
writeln!(output, " return this.getText(path);").unwrap();
|
||||
writeln!(output, " }}").unwrap();
|
||||
writeln!(output, " return this.getJson(path);").unwrap();
|
||||
} else {
|
||||
writeln!(output, " return this.getJson(path);").unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
if endpoint.supports_csv {
|
||||
writeln!(
|
||||
output,
|
||||
" if (format === 'csv') return this.getText(path, {{ signal, onUpdate }});"
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
writeln!(output, " return {};", fetch_call).unwrap();
|
||||
|
||||
writeln!(output, " }}\n").unwrap();
|
||||
}
|
||||
}
|
||||
@@ -127,14 +145,19 @@ fn endpoint_to_method_name(endpoint: &Endpoint) -> String {
|
||||
fn build_method_params(endpoint: &Endpoint) -> String {
|
||||
let mut params = Vec::new();
|
||||
for param in &endpoint.path_params {
|
||||
params.push(param.name.clone());
|
||||
params.push(sanitize_ident(¶m.name));
|
||||
}
|
||||
for param in &endpoint.query_params {
|
||||
params.push(param.name.clone());
|
||||
params.push(sanitize_ident(¶m.name));
|
||||
}
|
||||
params.join(", ")
|
||||
}
|
||||
|
||||
/// Strip characters invalid in JS identifiers (e.g. `[]` from `txId[]`).
|
||||
fn sanitize_ident(name: &str) -> String {
|
||||
name.replace(['[', ']'], "")
|
||||
}
|
||||
|
||||
fn build_path_template(path: &str, path_params: &[Parameter]) -> String {
|
||||
let mut result = path.to_string();
|
||||
for param in path_params {
|
||||
|
||||
@@ -22,6 +22,20 @@ pub fn generate_base_client(output: &mut String) {
|
||||
const _isBrowser = typeof window !== 'undefined' && 'caches' in window;
|
||||
const _runIdle = (/** @type {{VoidFunction}} */ fn) => (globalThis.requestIdleCallback ?? setTimeout)(fn);
|
||||
const _defaultCacheName = '__BRK_CLIENT__';
|
||||
/** @param {{*}} v */
|
||||
const _addCamelGetters = (v) => {{
|
||||
if (Array.isArray(v)) {{ v.forEach(_addCamelGetters); return v; }}
|
||||
if (v && typeof v === 'object' && v.constructor === Object) {{
|
||||
for (const k in v) {{
|
||||
if (k.includes('_')) {{
|
||||
const c = k.replace(/_([a-z])/g, (_, l) => l.toUpperCase());
|
||||
if (!(c in v)) Object.defineProperty(v, c, {{ get() {{ return this[k]; }} }});
|
||||
}}
|
||||
_addCamelGetters(v[k]);
|
||||
}}
|
||||
}}
|
||||
return v;
|
||||
}};
|
||||
|
||||
/**
|
||||
* @param {{string|boolean|undefined}} cache
|
||||
@@ -390,23 +404,28 @@ class BrkClientBase {{
|
||||
|
||||
/**
|
||||
* @param {{string}} path
|
||||
* @param {{{{ signal?: AbortSignal }}}} [options]
|
||||
* @returns {{Promise<Response>}}
|
||||
*/
|
||||
async get(path) {{
|
||||
async get(path, {{ signal }} = {{}}) {{
|
||||
const url = `${{this.baseUrl}}${{path}}`;
|
||||
const res = await fetch(url, {{ signal: AbortSignal.timeout(this.timeout) }});
|
||||
const signals = [AbortSignal.timeout(this.timeout)];
|
||||
if (signal) signals.push(signal);
|
||||
const res = await fetch(url, {{ signal: AbortSignal.any(signals) }});
|
||||
if (!res.ok) throw new BrkError(`HTTP ${{res.status}}: ${{url}}`, res.status);
|
||||
return res;
|
||||
}}
|
||||
|
||||
/**
|
||||
* Make a GET request - races cache vs network, first to resolve calls onUpdate
|
||||
* Make a GET request - races cache vs network, first to resolve calls onUpdate.
|
||||
* Shared implementation backing `getJson` and `getText`.
|
||||
* @template T
|
||||
* @param {{string}} path
|
||||
* @param {{(value: T) => void}} [onUpdate] - Called when data is available (may be called twice: cache then network)
|
||||
* @param {{(res: Response) => Promise<T>}} parse - Response body reader
|
||||
* @param {{{{ onUpdate?: (value: T) => void, signal?: AbortSignal }}}} [options]
|
||||
* @returns {{Promise<T>}}
|
||||
*/
|
||||
async getJson(path, onUpdate) {{
|
||||
async _getCached(path, parse, {{ onUpdate, signal }} = {{}}) {{
|
||||
const url = `${{this.baseUrl}}${{path}}`;
|
||||
const cache = this._cache ?? await this._cachePromise;
|
||||
|
||||
@@ -418,51 +437,61 @@ class BrkClientBase {{
|
||||
const cachePromise = cache?.match(url).then(async (res) => {{
|
||||
cachedRes = res ?? null;
|
||||
if (!res) return null;
|
||||
const json = await res.json();
|
||||
const value = await parse(res);
|
||||
if (!resolved && onUpdate) {{
|
||||
resolved = true;
|
||||
onUpdate(json);
|
||||
onUpdate(value);
|
||||
}}
|
||||
return json;
|
||||
return value;
|
||||
}});
|
||||
|
||||
const networkPromise = this.get(path).then(async (res) => {{
|
||||
const networkPromise = this.get(path, {{ signal }}).then(async (res) => {{
|
||||
const cloned = res.clone();
|
||||
const json = await res.json();
|
||||
const value = await parse(res);
|
||||
// Skip update if ETag matches and cache already delivered
|
||||
if (cachedRes?.headers.get('ETag') === res.headers.get('ETag')) {{
|
||||
if (!resolved && onUpdate) {{
|
||||
resolved = true;
|
||||
onUpdate(json);
|
||||
onUpdate(value);
|
||||
}}
|
||||
return json;
|
||||
return value;
|
||||
}}
|
||||
resolved = true;
|
||||
if (onUpdate) {{
|
||||
onUpdate(json);
|
||||
}}
|
||||
if (onUpdate) onUpdate(value);
|
||||
if (cache) _runIdle(() => cache.put(url, cloned));
|
||||
return json;
|
||||
return value;
|
||||
}});
|
||||
|
||||
try {{
|
||||
return await networkPromise;
|
||||
}} catch (e) {{
|
||||
// Network failed - wait for cache
|
||||
const cachedJson = await cachePromise?.catch(() => null);
|
||||
if (cachedJson) return cachedJson;
|
||||
const cachedValue = await cachePromise?.catch(() => null);
|
||||
if (cachedValue != null) return cachedValue;
|
||||
throw e;
|
||||
}}
|
||||
}}
|
||||
|
||||
/**
|
||||
* Make a GET request and return raw text (for CSV responses)
|
||||
* Make a GET request expecting a JSON response. Cached and supports `onUpdate`.
|
||||
* @template T
|
||||
* @param {{string}} path
|
||||
* @param {{{{ onUpdate?: (value: T) => void, signal?: AbortSignal }}}} [options]
|
||||
* @returns {{Promise<T>}}
|
||||
*/
|
||||
getJson(path, options) {{
|
||||
return this._getCached(path, async (res) => _addCamelGetters(await res.json()), options);
|
||||
}}
|
||||
|
||||
/**
|
||||
* Make a GET request expecting a text response (text/plain, text/csv, ...).
|
||||
* Cached and supports `onUpdate`, same as `getJson`.
|
||||
* @param {{string}} path
|
||||
* @param {{{{ onUpdate?: (value: string) => void, signal?: AbortSignal }}}} [options]
|
||||
* @returns {{Promise<string>}}
|
||||
*/
|
||||
async getText(path) {{
|
||||
const res = await this.get(path);
|
||||
return res.text();
|
||||
getText(path, options) {{
|
||||
return this._getCached(path, (res) => res.text(), options);
|
||||
}}
|
||||
|
||||
/**
|
||||
@@ -474,7 +503,7 @@ class BrkClientBase {{
|
||||
*/
|
||||
async _fetchSeriesData(path, onUpdate) {{
|
||||
const wrappedOnUpdate = onUpdate ? (/** @type {{SeriesData<T>}} */ raw) => onUpdate(_wrapSeriesData(raw)) : undefined;
|
||||
const raw = await this.getJson(path, wrappedOnUpdate);
|
||||
const raw = await this.getJson(path, {{ onUpdate: wrappedOnUpdate }});
|
||||
return _wrapSeriesData(raw);
|
||||
}}
|
||||
}}
|
||||
@@ -726,7 +755,12 @@ pub fn generate_structural_patterns(
|
||||
writeln!(output, " */").unwrap();
|
||||
|
||||
if pattern.is_templated() {
|
||||
writeln!(output, "function create{}(client, acc, disc) {{", pattern.name).unwrap();
|
||||
writeln!(
|
||||
output,
|
||||
"function create{}(client, acc, disc) {{",
|
||||
pattern.name
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
writeln!(output, "function create{}(client, acc) {{", pattern.name).unwrap();
|
||||
}
|
||||
|
||||
@@ -108,15 +108,14 @@ pub fn generate_main_client(
|
||||
writeln!(output, " constructor(options) {{").unwrap();
|
||||
writeln!(output, " super(options);").unwrap();
|
||||
writeln!(output, " /** @type {{SeriesTree}} */").unwrap();
|
||||
writeln!(output, " this.series = this._buildTree('');").unwrap();
|
||||
writeln!(output, " this.series = this._buildTree();").unwrap();
|
||||
writeln!(output, " }}\n").unwrap();
|
||||
|
||||
writeln!(output, " /**").unwrap();
|
||||
writeln!(output, " * @private").unwrap();
|
||||
writeln!(output, " * @param {{string}} basePath").unwrap();
|
||||
writeln!(output, " * @returns {{SeriesTree}}").unwrap();
|
||||
writeln!(output, " */").unwrap();
|
||||
writeln!(output, " _buildTree(basePath) {{").unwrap();
|
||||
writeln!(output, " _buildTree() {{").unwrap();
|
||||
writeln!(output, " return {{").unwrap();
|
||||
let mut generated = BTreeSet::new();
|
||||
generate_tree_initializer(
|
||||
|
||||
@@ -56,11 +56,7 @@ pub fn generate_main_client(output: &mut String, endpoints: &[Endpoint]) {
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(output, " \"\"\"").unwrap();
|
||||
writeln!(
|
||||
output,
|
||||
" return SeriesEndpoint(self, series, index)"
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(output, " return SeriesEndpoint(self, series, index)").unwrap();
|
||||
writeln!(output).unwrap();
|
||||
|
||||
// Generate helper methods
|
||||
@@ -105,7 +101,7 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
.response_type
|
||||
.as_deref()
|
||||
.map(js_type_to_python)
|
||||
.unwrap_or_else(|| "Any".to_string()),
|
||||
.unwrap_or_else(|| "str".to_string()),
|
||||
);
|
||||
|
||||
let return_type = if endpoint.supports_csv {
|
||||
@@ -163,11 +159,17 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
// Build path
|
||||
let path = build_path_template(&endpoint.path, &endpoint.path_params);
|
||||
|
||||
let fetch_method = if endpoint.returns_json() {
|
||||
"get_json"
|
||||
} else {
|
||||
"get_text"
|
||||
};
|
||||
|
||||
if endpoint.query_params.is_empty() {
|
||||
if endpoint.path_params.is_empty() {
|
||||
writeln!(output, " return self.get_json('{}')", path).unwrap();
|
||||
writeln!(output, " return self.{}('{}')", fetch_method, path).unwrap();
|
||||
} else {
|
||||
writeln!(output, " return self.get_json(f'{}')", path).unwrap();
|
||||
writeln!(output, " return self.{}(f'{}')", fetch_method, path).unwrap();
|
||||
}
|
||||
} else {
|
||||
writeln!(output, " params = []").unwrap();
|
||||
@@ -201,9 +203,9 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
if endpoint.supports_csv {
|
||||
writeln!(output, " if format == 'csv':").unwrap();
|
||||
writeln!(output, " return self.get_text(path)").unwrap();
|
||||
writeln!(output, " return self.get_json(path)").unwrap();
|
||||
writeln!(output, " return self.{}(path)", fetch_method).unwrap();
|
||||
} else {
|
||||
writeln!(output, " return self.get_json(path)").unwrap();
|
||||
writeln!(output, " return self.{}(path)", fetch_method).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -684,7 +684,6 @@ pub fn generate_structural_patterns(
|
||||
writeln!(output, "# Reusable structural pattern classes\n").unwrap();
|
||||
|
||||
for pattern in patterns {
|
||||
|
||||
// Generate class
|
||||
if pattern.is_generic {
|
||||
writeln!(output, "class {}(Generic[T]):", pattern.name).unwrap();
|
||||
|
||||
@@ -110,8 +110,9 @@ fn topological_sort_schemas(schemas: &TypeSchemas) -> Vec<String> {
|
||||
for (name, schema) in schemas {
|
||||
let mut type_deps = BTreeSet::new();
|
||||
collect_schema_refs(schema, &mut type_deps);
|
||||
// Only keep deps that are in our schemas
|
||||
type_deps.retain(|d| schemas.contains_key(d));
|
||||
// Only keep deps that are in our schemas, and drop self-references
|
||||
// (handled at emit time by quoting via current_type)
|
||||
type_deps.retain(|d| schemas.contains_key(d) && d != name);
|
||||
deps.insert(name.clone(), type_deps);
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
.response_type
|
||||
.as_deref()
|
||||
.map(js_type_to_rust)
|
||||
.unwrap_or_else(|| "serde_json::Value".to_string());
|
||||
.unwrap_or_else(|| "String".to_string());
|
||||
|
||||
let return_type = if endpoint.supports_csv {
|
||||
format!("FormatResponse<{}>", base_return_type)
|
||||
@@ -132,29 +132,43 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
.unwrap();
|
||||
|
||||
let (path, index_arg) = build_path_template(endpoint);
|
||||
let fetch_method = if endpoint.returns_json() {
|
||||
"get_json"
|
||||
} else {
|
||||
"get_text"
|
||||
};
|
||||
|
||||
if endpoint.query_params.is_empty() {
|
||||
writeln!(
|
||||
output,
|
||||
" self.base.get_json(&format!(\"{}\"{}))",
|
||||
path, index_arg
|
||||
" self.base.{}(&format!(\"{}\"{}))",
|
||||
fetch_method, path, index_arg
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
writeln!(output, " let mut query = Vec::new();").unwrap();
|
||||
for param in &endpoint.query_params {
|
||||
if param.required {
|
||||
let ident = sanitize_ident(¶m.name);
|
||||
let is_array = param.param_type.ends_with("[]");
|
||||
if is_array {
|
||||
writeln!(
|
||||
output,
|
||||
" for v in {} {{ query.push(format!(\"{}={{}}\", v)); }}",
|
||||
ident, param.name
|
||||
)
|
||||
.unwrap();
|
||||
} else if param.required {
|
||||
writeln!(
|
||||
output,
|
||||
" query.push(format!(\"{}={{}}\", {}));",
|
||||
param.name, param.name
|
||||
param.name, ident
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
writeln!(
|
||||
output,
|
||||
" if let Some(v) = {} {{ query.push(format!(\"{}={{}}\", v)); }}",
|
||||
param.name, param.name
|
||||
ident, param.name
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
@@ -177,12 +191,13 @@ pub fn generate_api_methods(output: &mut String, endpoints: &[Endpoint]) {
|
||||
writeln!(output, " }} else {{").unwrap();
|
||||
writeln!(
|
||||
output,
|
||||
" self.base.get_json(&path).map(FormatResponse::Json)"
|
||||
" self.base.{}(&path).map(FormatResponse::Json)",
|
||||
fetch_method
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(output, " }}").unwrap();
|
||||
} else {
|
||||
writeln!(output, " self.base.get_json(&path)").unwrap();
|
||||
writeln!(output, " self.base.{}(&path)", fetch_method).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,26 +213,35 @@ fn build_method_params(endpoint: &Endpoint) -> String {
|
||||
let mut params = Vec::new();
|
||||
for param in &endpoint.path_params {
|
||||
let rust_type = param_type_to_rust(¶m.param_type);
|
||||
params.push(format!(", {}: {}", param.name, rust_type));
|
||||
params.push(format!(", {}: {}", sanitize_ident(¶m.name), rust_type));
|
||||
}
|
||||
for param in &endpoint.query_params {
|
||||
let rust_type = param_type_to_rust(¶m.param_type);
|
||||
let name = sanitize_ident(¶m.name);
|
||||
if param.required {
|
||||
params.push(format!(", {}: {}", param.name, rust_type));
|
||||
params.push(format!(", {}: {}", name, rust_type));
|
||||
} else {
|
||||
params.push(format!(", {}: Option<{}>", param.name, rust_type));
|
||||
params.push(format!(", {}: Option<{}>", name, rust_type));
|
||||
}
|
||||
}
|
||||
params.join("")
|
||||
}
|
||||
|
||||
/// Strip characters invalid in Rust identifiers (e.g. `[]` from `txId[]`).
|
||||
fn sanitize_ident(name: &str) -> String {
|
||||
name.replace(['[', ']'], "")
|
||||
}
|
||||
|
||||
/// Convert parameter type to Rust type for function signatures.
|
||||
fn param_type_to_rust(param_type: &str) -> String {
|
||||
if let Some(inner) = param_type.strip_suffix("[]") {
|
||||
return format!("&[{}]", param_type_to_rust(inner));
|
||||
}
|
||||
match param_type {
|
||||
"string" | "*" => "&str".to_string(),
|
||||
"integer" | "number" => "i64".to_string(),
|
||||
"boolean" => "bool".to_string(),
|
||||
other => other.to_string(), // Domain types like Index, SeriesName, Format
|
||||
other => other.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,11 @@ impl Endpoint {
|
||||
self.method == "GET" && !self.deprecated
|
||||
}
|
||||
|
||||
/// Returns true if this endpoint returns JSON (has a response_type extracted from application/json).
|
||||
pub fn returns_json(&self) -> bool {
|
||||
self.response_type.is_some()
|
||||
}
|
||||
|
||||
/// Returns the operation ID or generates one from the path.
|
||||
/// The returned string uses the raw case from the spec (typically camelCase).
|
||||
pub fn operation_name(&self) -> String {
|
||||
|
||||
@@ -74,6 +74,9 @@ pub fn escape_python_keyword(name: &str) -> String {
|
||||
"try", "while", "with", "yield",
|
||||
];
|
||||
|
||||
// Strip characters invalid in identifiers (e.g. `[]` from `txId[]`)
|
||||
let name = name.replace(['[', ']'], "");
|
||||
|
||||
// Prefix with underscore if starts with digit
|
||||
let name = if name.starts_with(|c: char| c.is_ascii_digit()) {
|
||||
format!("_{}", name)
|
||||
|
||||
@@ -13,12 +13,11 @@ brk_alloc = { workspace = true }
|
||||
brk_computer = { workspace = true }
|
||||
brk_error = { workspace = true, features = ["tokio", "vecdb"] }
|
||||
brk_indexer = { workspace = true }
|
||||
brk_iterator = { workspace = true }
|
||||
brk_logger = { workspace = true }
|
||||
brk_mempool = { workspace = true }
|
||||
brk_query = { workspace = true }
|
||||
brk_reader = { workspace = true }
|
||||
brk_rpc = { workspace = true, features = ["corepc"] }
|
||||
brk_rpc = { workspace = true }
|
||||
brk_server = { workspace = true }
|
||||
brk_types = { workspace = true }
|
||||
lexopt = "0.3"
|
||||
@@ -26,7 +25,7 @@ owo-colors = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
toml = "1.0.7"
|
||||
toml = "1.1.2"
|
||||
vecdb = { workspace = true }
|
||||
|
||||
[[bin]]
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
# BRK CLI
|
||||
|
||||
Command-line interface for running a Bitcoin Research Kit instance.
|
||||
Run your own Bitcoin Research Kit instance. One binary, one command. Full sync in ~4-7h depending on hardware. ~44% disk overhead vs 250% for mempool/electrs.
|
||||
|
||||
## Demo
|
||||
|
||||
- [bitview.space](https://bitview.space) - web interface
|
||||
- [bitview.space/api](https://bitview.space/api) - API docs
|
||||
[bitview.space](https://bitview.space) is the official free hosted instance.
|
||||
|
||||
## Requirements
|
||||
|
||||
|
||||
+102
-28
@@ -1,47 +1,62 @@
|
||||
use std::{
|
||||
fs,
|
||||
fs, io,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use brk_error::{Error, Result};
|
||||
use brk_rpc::{Auth, Client};
|
||||
use brk_server::Website;
|
||||
use brk_server::{
|
||||
CdnCacheMode, DEFAULT_CACHE_SIZE, DEFAULT_MAX_WEIGHT, DEFAULT_MAX_WEIGHT_LOCALHOST, Website,
|
||||
};
|
||||
use brk_types::Port;
|
||||
use owo_colors::OwoColorize;
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{default_brk_path, dot_brk_path, fix_user_path};
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
brkdir: Option<String>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
brkport: Option<Port>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
website: Option<Website>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
cdn: Option<bool>,
|
||||
|
||||
#[serde(default)]
|
||||
maxweight: Option<usize>,
|
||||
|
||||
#[serde(default)]
|
||||
maxweightlocal: Option<usize>,
|
||||
|
||||
#[serde(default)]
|
||||
cachesize: Option<usize>,
|
||||
|
||||
#[serde(default)]
|
||||
bitcoindir: Option<String>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
blocksdir: Option<String>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
rpcconnect: Option<String>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
rpcport: Option<u16>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
rpccookiefile: Option<String>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
rpcuser: Option<String>,
|
||||
|
||||
#[serde(default, deserialize_with = "default_on_error")]
|
||||
#[serde(default)]
|
||||
rpcpassword: Option<String>,
|
||||
}
|
||||
|
||||
@@ -66,6 +81,18 @@ impl Config {
|
||||
if let Some(v) = config_args.website {
|
||||
config.website = Some(v);
|
||||
}
|
||||
if let Some(v) = config_args.cdn {
|
||||
config.cdn = Some(v);
|
||||
}
|
||||
if let Some(v) = config_args.maxweight {
|
||||
config.maxweight = Some(v);
|
||||
}
|
||||
if let Some(v) = config_args.maxweightlocal {
|
||||
config.maxweightlocal = Some(v);
|
||||
}
|
||||
if let Some(v) = config_args.cachesize {
|
||||
config.cachesize = Some(v);
|
||||
}
|
||||
if let Some(v) = config_args.bitcoindir {
|
||||
config.bitcoindir = Some(v);
|
||||
}
|
||||
@@ -112,6 +139,16 @@ impl Config {
|
||||
Long("brkdir") => config.brkdir = Some(parser.value().unwrap().parse().unwrap()),
|
||||
Long("brkport") => config.brkport = Some(parser.value().unwrap().parse().unwrap()),
|
||||
Long("website") => config.website = Some(parser.value().unwrap().parse().unwrap()),
|
||||
Long("cdn") => config.cdn = Some(parser.value().unwrap().parse().unwrap()),
|
||||
Long("maxweight") => {
|
||||
config.maxweight = Some(parser.value().unwrap().parse().unwrap())
|
||||
}
|
||||
Long("maxweightlocal") => {
|
||||
config.maxweightlocal = Some(parser.value().unwrap().parse().unwrap())
|
||||
}
|
||||
Long("cachesize") => {
|
||||
config.cachesize = Some(parser.value().unwrap().parse().unwrap())
|
||||
}
|
||||
Long("bitcoindir") => {
|
||||
config.bitcoindir = Some(parser.value().unwrap().parse().unwrap())
|
||||
}
|
||||
@@ -171,6 +208,26 @@ impl Config {
|
||||
"<BOOL|PATH>".bright_black(),
|
||||
"[true]".bright_black()
|
||||
);
|
||||
println!(
|
||||
" --cdn {} Aggressive CDN cache, requires purge on deploy {}",
|
||||
"<BOOL>".bright_black(),
|
||||
"[false]".bright_black()
|
||||
);
|
||||
println!(
|
||||
" --maxweight {} Max series response weight in bytes for external clients {}",
|
||||
"<BYTES>".bright_black(),
|
||||
format!("[{}]", DEFAULT_MAX_WEIGHT).bright_black()
|
||||
);
|
||||
println!(
|
||||
" --maxweightlocal {} Max series response weight in bytes for loopback clients {}",
|
||||
"<BYTES>".bright_black(),
|
||||
format!("[{}]", DEFAULT_MAX_WEIGHT_LOCALHOST).bright_black()
|
||||
);
|
||||
println!(
|
||||
" --cachesize {} LRU capacity for the in-process response cache {}",
|
||||
"<ENTRIES>".bright_black(),
|
||||
format!("[{}]", DEFAULT_CACHE_SIZE).bright_black()
|
||||
);
|
||||
println!();
|
||||
println!(
|
||||
" --bitcoindir {} Bitcoin directory {}",
|
||||
@@ -263,10 +320,18 @@ Finally, you can run the program with '-h' for help."
|
||||
}
|
||||
|
||||
fn read(path: &Path) -> Self {
|
||||
fs::read_to_string(path).map_or_else(
|
||||
|_| Config::default(),
|
||||
|contents| toml::from_str(&contents).unwrap_or_default(),
|
||||
)
|
||||
let contents = match fs::read_to_string(path) {
|
||||
Ok(contents) => contents,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => return Config::default(),
|
||||
Err(e) => {
|
||||
eprintln!("Cannot read {}: {e}", path.display());
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
toml::from_str(&contents).unwrap_or_else(|e| {
|
||||
eprintln!("Invalid {}:\n{e}", path.display());
|
||||
std::process::exit(1);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn rpc(&self) -> Result<Client> {
|
||||
@@ -333,18 +398,27 @@ Finally, you can run the program with '-h' for help."
|
||||
self.website.clone().unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn cdn_cache_mode(&self) -> CdnCacheMode {
|
||||
if self.cdn.unwrap_or(false) {
|
||||
CdnCacheMode::Aggressive
|
||||
} else {
|
||||
CdnCacheMode::Live
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max_weight(&self) -> usize {
|
||||
self.maxweight.unwrap_or(DEFAULT_MAX_WEIGHT)
|
||||
}
|
||||
|
||||
pub fn max_weight_localhost(&self) -> usize {
|
||||
self.maxweightlocal.unwrap_or(DEFAULT_MAX_WEIGHT_LOCALHOST)
|
||||
}
|
||||
|
||||
pub fn cache_size(&self) -> usize {
|
||||
self.cachesize.unwrap_or(DEFAULT_CACHE_SIZE)
|
||||
}
|
||||
|
||||
pub fn brkport(&self) -> Option<Port> {
|
||||
self.brkport
|
||||
}
|
||||
}
|
||||
|
||||
fn default_on_error<'de, D, T>(deserializer: D) -> Result<T, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
T: Deserialize<'de> + Default,
|
||||
{
|
||||
match T::deserialize(deserializer) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => Ok(T::default()),
|
||||
}
|
||||
}
|
||||
|
||||
+20
-15
@@ -10,11 +10,10 @@ use brk_alloc::Mimalloc;
|
||||
use brk_computer::Computer;
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_iterator::Blocks;
|
||||
use brk_mempool::Mempool;
|
||||
use brk_query::AsyncQuery;
|
||||
use brk_reader::Reader;
|
||||
use brk_server::Server;
|
||||
use brk_server::{Server, ServerConfig};
|
||||
use tracing::info;
|
||||
use vecdb::Exit;
|
||||
|
||||
@@ -37,8 +36,6 @@ pub fn main() -> anyhow::Result<()> {
|
||||
|
||||
let reader = Reader::new(config.blocksdir(), &client);
|
||||
|
||||
let blocks = Blocks::new(&client, &reader);
|
||||
|
||||
let mut indexer = Indexer::forced_import(&config.brkdir())?;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
@@ -52,7 +49,7 @@ pub fn main() -> anyhow::Result<()> {
|
||||
info!("Indexing {blocks_behind} blocks before starting server...");
|
||||
info!("---");
|
||||
sleep(Duration::from_secs(10));
|
||||
indexer.index(&blocks, &client, &exit)?;
|
||||
indexer.index(&reader, &client, &exit)?;
|
||||
drop(indexer);
|
||||
Mimalloc::collect();
|
||||
indexer = Indexer::forced_import(&config.brkdir())?;
|
||||
@@ -63,21 +60,29 @@ pub fn main() -> anyhow::Result<()> {
|
||||
|
||||
let mempool = Mempool::new(&client);
|
||||
|
||||
let query = AsyncQuery::build(&reader, &indexer, &computer, Some(mempool.clone()));
|
||||
|
||||
let mempool_clone = mempool.clone();
|
||||
let query_clone = query.clone();
|
||||
thread::spawn(move || {
|
||||
mempool_clone.start();
|
||||
mempool_clone.start_with(|| {
|
||||
query_clone.sync(|q| q.fill_mempool_prevouts());
|
||||
});
|
||||
});
|
||||
|
||||
let query = AsyncQuery::build(&reader, &indexer, &computer, Some(mempool));
|
||||
|
||||
let data_path = config.brkdir();
|
||||
|
||||
let website = config.website();
|
||||
let server_config = ServerConfig {
|
||||
data_path: config.brkdir(),
|
||||
website: config.website(),
|
||||
cdn_cache_mode: config.cdn_cache_mode(),
|
||||
max_weight: config.max_weight(),
|
||||
max_weight_localhost: config.max_weight_localhost(),
|
||||
cache_size: config.cache_size(),
|
||||
};
|
||||
|
||||
let port = config.brkport();
|
||||
|
||||
let future = async move {
|
||||
let server = Server::new(&query, data_path, website);
|
||||
let server = Server::new(&query, server_config);
|
||||
|
||||
tokio::spawn(async move {
|
||||
server.serve(port).await.unwrap();
|
||||
@@ -102,14 +107,14 @@ pub fn main() -> anyhow::Result<()> {
|
||||
let total_start = Instant::now();
|
||||
|
||||
let starting_indexes = if cfg!(debug_assertions) {
|
||||
indexer.checked_index(&blocks, &client, &exit)?
|
||||
indexer.checked_index(&reader, &client, &exit)?
|
||||
} else {
|
||||
indexer.index(&blocks, &client, &exit)?
|
||||
indexer.index(&reader, &client, &exit)?
|
||||
};
|
||||
|
||||
Mimalloc::collect();
|
||||
|
||||
computer.compute(&indexer, starting_indexes, &reader, &exit)?;
|
||||
computer.compute(&indexer, starting_indexes, &exit)?;
|
||||
|
||||
info!("Total time: {:?}", total_start.elapsed());
|
||||
info!("Waiting for new blocks...");
|
||||
|
||||
+6866
-1757
File diff suppressed because it is too large
Load Diff
@@ -14,3 +14,6 @@ brk_traversable = { workspace = true }
|
||||
vecdb = { workspace = true }
|
||||
rayon = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
[package.metadata.cargo-machete]
|
||||
ignored = ["vecdb"]
|
||||
|
||||
@@ -75,7 +75,8 @@ impl<T> AddrGroups<T> {
|
||||
}
|
||||
|
||||
pub fn iter_overlapping_mut(&mut self) -> impl Iterator<Item = &mut T> {
|
||||
self.under_amount.iter_mut().chain(self.over_amount.iter_mut())
|
||||
self.under_amount
|
||||
.iter_mut()
|
||||
.chain(self.over_amount.iter_mut())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +201,29 @@ impl<T> AgeRange<T> {
|
||||
}
|
||||
|
||||
pub fn from_array(arr: [T; 21]) -> Self {
|
||||
let [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20] = arr;
|
||||
let [
|
||||
a0,
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14,
|
||||
a15,
|
||||
a16,
|
||||
a17,
|
||||
a18,
|
||||
a19,
|
||||
a20,
|
||||
] = arr;
|
||||
Self {
|
||||
under_1h: a0,
|
||||
_1h_to_1d: a1,
|
||||
|
||||
@@ -84,7 +84,11 @@ pub const AMOUNT_RANGE_NAMES: AmountRange<CohortName> = AmountRange {
|
||||
_10sats_to_100sats: CohortName::new("10sats_to_100sats", "10-100 sats", "10-100 Sats"),
|
||||
_100sats_to_1k_sats: CohortName::new("100sats_to_1k_sats", "100-1k sats", "100-1K Sats"),
|
||||
_1k_sats_to_10k_sats: CohortName::new("1k_sats_to_10k_sats", "1k-10k sats", "1K-10K Sats"),
|
||||
_10k_sats_to_100k_sats: CohortName::new("10k_sats_to_100k_sats", "10k-100k sats", "10K-100K Sats"),
|
||||
_10k_sats_to_100k_sats: CohortName::new(
|
||||
"10k_sats_to_100k_sats",
|
||||
"10k-100k sats",
|
||||
"10K-100K Sats",
|
||||
),
|
||||
_100k_sats_to_1m_sats: CohortName::new("100k_sats_to_1m_sats", "100k-1M sats", "100K-1M Sats"),
|
||||
_1m_sats_to_10m_sats: CohortName::new("1m_sats_to_10m_sats", "1M-10M sats", "1M-10M Sats"),
|
||||
_10m_sats_to_1btc: CohortName::new("10m_sats_to_1btc", "0.1-1 BTC", "0.1-1 BTC"),
|
||||
|
||||
@@ -1,16 +1,34 @@
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::OutputType;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use super::{SpendableType, UnspendableType};
|
||||
use super::{Filter, SpendableType, UnspendableType};
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub const OP_RETURN: &str = "op_return";
|
||||
|
||||
#[derive(Default, Clone, Debug, Traversable)]
|
||||
pub struct ByType<T> {
|
||||
#[traversable(flatten)]
|
||||
pub spendable: SpendableType<T>,
|
||||
#[traversable(flatten)]
|
||||
pub unspendable: UnspendableType<T>,
|
||||
}
|
||||
|
||||
impl<T> ByType<T> {
|
||||
pub fn try_new<F, E>(mut create: F) -> Result<Self, E>
|
||||
where
|
||||
F: FnMut(Filter, &'static str) -> Result<T, E>,
|
||||
{
|
||||
Ok(Self {
|
||||
spendable: SpendableType::try_new(&mut create)?,
|
||||
unspendable: UnspendableType {
|
||||
op_return: create(Filter::Type(OutputType::OpReturn), OP_RETURN)?,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get(&self, output_type: OutputType) -> &T {
|
||||
match output_type {
|
||||
OutputType::P2PK65 => &self.spendable.p2pk65,
|
||||
@@ -44,6 +62,45 @@ impl<T> ByType<T> {
|
||||
OutputType::OpReturn => &mut self.unspendable.op_return,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||
self.spendable
|
||||
.iter()
|
||||
.chain(std::iter::once(&self.unspendable.op_return))
|
||||
}
|
||||
|
||||
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
|
||||
self.spendable
|
||||
.iter_mut()
|
||||
.chain(std::iter::once(&mut self.unspendable.op_return))
|
||||
}
|
||||
|
||||
pub fn par_iter_mut(&mut self) -> impl ParallelIterator<Item = &mut T>
|
||||
where
|
||||
T: Send + Sync,
|
||||
{
|
||||
let Self {
|
||||
spendable,
|
||||
unspendable,
|
||||
} = self;
|
||||
spendable
|
||||
.par_iter_mut()
|
||||
.chain([&mut unspendable.op_return].into_par_iter())
|
||||
}
|
||||
|
||||
pub fn iter_typed(&self) -> impl Iterator<Item = (OutputType, &T)> {
|
||||
self.spendable.iter_typed().chain(std::iter::once((
|
||||
OutputType::OpReturn,
|
||||
&self.unspendable.op_return,
|
||||
)))
|
||||
}
|
||||
|
||||
pub fn iter_typed_mut(&mut self) -> impl Iterator<Item = (OutputType, &mut T)> {
|
||||
self.spendable.iter_typed_mut().chain(std::iter::once((
|
||||
OutputType::OpReturn,
|
||||
&mut self.unspendable.op_return,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Add for ByType<T>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
mod addr;
|
||||
mod amount_filter;
|
||||
mod age_range;
|
||||
mod amount_filter;
|
||||
mod amount_range;
|
||||
mod by_addr_type;
|
||||
mod by_any_addr;
|
||||
@@ -30,8 +30,8 @@ mod utxo;
|
||||
pub use brk_types::{Age, Term};
|
||||
|
||||
pub use addr::*;
|
||||
pub use amount_filter::*;
|
||||
pub use age_range::*;
|
||||
pub use amount_filter::*;
|
||||
pub use amount_range::*;
|
||||
pub use by_addr_type::*;
|
||||
pub use by_any_addr::*;
|
||||
|
||||
@@ -153,6 +153,6 @@ impl<T> Loss<T> {
|
||||
.into_iter()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.map(move |(n, threshold)| (threshold, &ranges[len - 1 - n..]))
|
||||
.map(move |(n, threshold)| (threshold, &ranges[len - 2 - n..]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,19 +43,31 @@ pub const OVER_AMOUNT_NAMES: OverAmount<CohortName> = OverAmount {
|
||||
pub const OVER_AMOUNT_FILTERS: OverAmount<Filter> = OverAmount {
|
||||
_1sat: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._1sat)),
|
||||
_10sats: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._10sats)),
|
||||
_100sats: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._100sats)),
|
||||
_1k_sats: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._1k_sats)),
|
||||
_10k_sats: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._10k_sats)),
|
||||
_100sats: Filter::Amount(AmountFilter::GreaterOrEqual(
|
||||
OVER_AMOUNT_THRESHOLDS._100sats,
|
||||
)),
|
||||
_1k_sats: Filter::Amount(AmountFilter::GreaterOrEqual(
|
||||
OVER_AMOUNT_THRESHOLDS._1k_sats,
|
||||
)),
|
||||
_10k_sats: Filter::Amount(AmountFilter::GreaterOrEqual(
|
||||
OVER_AMOUNT_THRESHOLDS._10k_sats,
|
||||
)),
|
||||
_100k_sats: Filter::Amount(AmountFilter::GreaterOrEqual(
|
||||
OVER_AMOUNT_THRESHOLDS._100k_sats,
|
||||
)),
|
||||
_1m_sats: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._1m_sats)),
|
||||
_10m_sats: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._10m_sats)),
|
||||
_1m_sats: Filter::Amount(AmountFilter::GreaterOrEqual(
|
||||
OVER_AMOUNT_THRESHOLDS._1m_sats,
|
||||
)),
|
||||
_10m_sats: Filter::Amount(AmountFilter::GreaterOrEqual(
|
||||
OVER_AMOUNT_THRESHOLDS._10m_sats,
|
||||
)),
|
||||
_1btc: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._1btc)),
|
||||
_10btc: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._10btc)),
|
||||
_100btc: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._100btc)),
|
||||
_1k_btc: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._1k_btc)),
|
||||
_10k_btc: Filter::Amount(AmountFilter::GreaterOrEqual(OVER_AMOUNT_THRESHOLDS._10k_btc)),
|
||||
_10k_btc: Filter::Amount(AmountFilter::GreaterOrEqual(
|
||||
OVER_AMOUNT_THRESHOLDS._10k_btc,
|
||||
)),
|
||||
};
|
||||
|
||||
#[derive(Default, Clone, Traversable, Serialize)]
|
||||
|
||||
@@ -16,10 +16,26 @@ pub const PROFIT_NAMES: Profit<CohortName> = Profit {
|
||||
_70pct: CohortName::new("utxos_over_70pct_in_profit", ">=70%", "Over 70% in Profit"),
|
||||
_80pct: CohortName::new("utxos_over_80pct_in_profit", ">=80%", "Over 80% in Profit"),
|
||||
_90pct: CohortName::new("utxos_over_90pct_in_profit", ">=90%", "Over 90% in Profit"),
|
||||
_100pct: CohortName::new("utxos_over_100pct_in_profit", ">=100%", "Over 100% in Profit"),
|
||||
_200pct: CohortName::new("utxos_over_200pct_in_profit", ">=200%", "Over 200% in Profit"),
|
||||
_300pct: CohortName::new("utxos_over_300pct_in_profit", ">=300%", "Over 300% in Profit"),
|
||||
_500pct: CohortName::new("utxos_over_500pct_in_profit", ">=500%", "Over 500% in Profit"),
|
||||
_100pct: CohortName::new(
|
||||
"utxos_over_100pct_in_profit",
|
||||
">=100%",
|
||||
"Over 100% in Profit",
|
||||
),
|
||||
_200pct: CohortName::new(
|
||||
"utxos_over_200pct_in_profit",
|
||||
">=200%",
|
||||
"Over 200% in Profit",
|
||||
),
|
||||
_300pct: CohortName::new(
|
||||
"utxos_over_300pct_in_profit",
|
||||
">=300%",
|
||||
"Over 300% in Profit",
|
||||
),
|
||||
_500pct: CohortName::new(
|
||||
"utxos_over_500pct_in_profit",
|
||||
">=500%",
|
||||
"Over 500% in Profit",
|
||||
),
|
||||
};
|
||||
|
||||
/// Number of profit thresholds.
|
||||
@@ -192,6 +208,6 @@ impl<T> Profit<T> {
|
||||
.into_iter()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.map(move |(n, threshold)| (threshold, &ranges[..n + 1]))
|
||||
.map(move |(n, threshold)| (threshold, &ranges[..n + 2]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,31 +83,131 @@ pub fn compute_profitability_boundaries(spot: Cents) -> [Cents; PROFITABILITY_BO
|
||||
|
||||
/// Profitability range names (25 ranges, from most profitable to most in loss)
|
||||
pub const PROFITABILITY_RANGE_NAMES: ProfitabilityRange<CohortName> = ProfitabilityRange {
|
||||
over_1000pct_in_profit: CohortName::new("utxos_over_1000pct_in_profit", "+>1000%", "Over 1000% in Profit"),
|
||||
_500pct_to_1000pct_in_profit: CohortName::new("utxos_500pct_to_1000pct_in_profit", "+500-1000%", "500-1000% in Profit"),
|
||||
_300pct_to_500pct_in_profit: CohortName::new("utxos_300pct_to_500pct_in_profit", "+300-500%", "300-500% in Profit"),
|
||||
_200pct_to_300pct_in_profit: CohortName::new("utxos_200pct_to_300pct_in_profit", "+200-300%", "200-300% in Profit"),
|
||||
_100pct_to_200pct_in_profit: CohortName::new("utxos_100pct_to_200pct_in_profit", "+100-200%", "100-200% in Profit"),
|
||||
_90pct_to_100pct_in_profit: CohortName::new("utxos_90pct_to_100pct_in_profit", "+90-100%", "90-100% in Profit"),
|
||||
_80pct_to_90pct_in_profit: CohortName::new("utxos_80pct_to_90pct_in_profit", "+80-90%", "80-90% in Profit"),
|
||||
_70pct_to_80pct_in_profit: CohortName::new("utxos_70pct_to_80pct_in_profit", "+70-80%", "70-80% in Profit"),
|
||||
_60pct_to_70pct_in_profit: CohortName::new("utxos_60pct_to_70pct_in_profit", "+60-70%", "60-70% in Profit"),
|
||||
_50pct_to_60pct_in_profit: CohortName::new("utxos_50pct_to_60pct_in_profit", "+50-60%", "50-60% in Profit"),
|
||||
_40pct_to_50pct_in_profit: CohortName::new("utxos_40pct_to_50pct_in_profit", "+40-50%", "40-50% in Profit"),
|
||||
_30pct_to_40pct_in_profit: CohortName::new("utxos_30pct_to_40pct_in_profit", "+30-40%", "30-40% in Profit"),
|
||||
_20pct_to_30pct_in_profit: CohortName::new("utxos_20pct_to_30pct_in_profit", "+20-30%", "20-30% in Profit"),
|
||||
_10pct_to_20pct_in_profit: CohortName::new("utxos_10pct_to_20pct_in_profit", "+10-20%", "10-20% in Profit"),
|
||||
_0pct_to_10pct_in_profit: CohortName::new("utxos_0pct_to_10pct_in_profit", "+0-10%", "0-10% in Profit"),
|
||||
_0pct_to_10pct_in_loss: CohortName::new("utxos_0pct_to_10pct_in_loss", "-0-10%", "0-10% in Loss"),
|
||||
_10pct_to_20pct_in_loss: CohortName::new("utxos_10pct_to_20pct_in_loss", "-10-20%", "10-20% in Loss"),
|
||||
_20pct_to_30pct_in_loss: CohortName::new("utxos_20pct_to_30pct_in_loss", "-20-30%", "20-30% in Loss"),
|
||||
_30pct_to_40pct_in_loss: CohortName::new("utxos_30pct_to_40pct_in_loss", "-30-40%", "30-40% in Loss"),
|
||||
_40pct_to_50pct_in_loss: CohortName::new("utxos_40pct_to_50pct_in_loss", "-40-50%", "40-50% in Loss"),
|
||||
_50pct_to_60pct_in_loss: CohortName::new("utxos_50pct_to_60pct_in_loss", "-50-60%", "50-60% in Loss"),
|
||||
_60pct_to_70pct_in_loss: CohortName::new("utxos_60pct_to_70pct_in_loss", "-60-70%", "60-70% in Loss"),
|
||||
_70pct_to_80pct_in_loss: CohortName::new("utxos_70pct_to_80pct_in_loss", "-70-80%", "70-80% in Loss"),
|
||||
_80pct_to_90pct_in_loss: CohortName::new("utxos_80pct_to_90pct_in_loss", "-80-90%", "80-90% in Loss"),
|
||||
_90pct_to_100pct_in_loss: CohortName::new("utxos_90pct_to_100pct_in_loss", "-90-100%", "90-100% in Loss"),
|
||||
over_1000pct_in_profit: CohortName::new(
|
||||
"utxos_over_1000pct_in_profit",
|
||||
"+>1000%",
|
||||
"Over 1000% in Profit",
|
||||
),
|
||||
_500pct_to_1000pct_in_profit: CohortName::new(
|
||||
"utxos_500pct_to_1000pct_in_profit",
|
||||
"+500-1000%",
|
||||
"500-1000% in Profit",
|
||||
),
|
||||
_300pct_to_500pct_in_profit: CohortName::new(
|
||||
"utxos_300pct_to_500pct_in_profit",
|
||||
"+300-500%",
|
||||
"300-500% in Profit",
|
||||
),
|
||||
_200pct_to_300pct_in_profit: CohortName::new(
|
||||
"utxos_200pct_to_300pct_in_profit",
|
||||
"+200-300%",
|
||||
"200-300% in Profit",
|
||||
),
|
||||
_100pct_to_200pct_in_profit: CohortName::new(
|
||||
"utxos_100pct_to_200pct_in_profit",
|
||||
"+100-200%",
|
||||
"100-200% in Profit",
|
||||
),
|
||||
_90pct_to_100pct_in_profit: CohortName::new(
|
||||
"utxos_90pct_to_100pct_in_profit",
|
||||
"+90-100%",
|
||||
"90-100% in Profit",
|
||||
),
|
||||
_80pct_to_90pct_in_profit: CohortName::new(
|
||||
"utxos_80pct_to_90pct_in_profit",
|
||||
"+80-90%",
|
||||
"80-90% in Profit",
|
||||
),
|
||||
_70pct_to_80pct_in_profit: CohortName::new(
|
||||
"utxos_70pct_to_80pct_in_profit",
|
||||
"+70-80%",
|
||||
"70-80% in Profit",
|
||||
),
|
||||
_60pct_to_70pct_in_profit: CohortName::new(
|
||||
"utxos_60pct_to_70pct_in_profit",
|
||||
"+60-70%",
|
||||
"60-70% in Profit",
|
||||
),
|
||||
_50pct_to_60pct_in_profit: CohortName::new(
|
||||
"utxos_50pct_to_60pct_in_profit",
|
||||
"+50-60%",
|
||||
"50-60% in Profit",
|
||||
),
|
||||
_40pct_to_50pct_in_profit: CohortName::new(
|
||||
"utxos_40pct_to_50pct_in_profit",
|
||||
"+40-50%",
|
||||
"40-50% in Profit",
|
||||
),
|
||||
_30pct_to_40pct_in_profit: CohortName::new(
|
||||
"utxos_30pct_to_40pct_in_profit",
|
||||
"+30-40%",
|
||||
"30-40% in Profit",
|
||||
),
|
||||
_20pct_to_30pct_in_profit: CohortName::new(
|
||||
"utxos_20pct_to_30pct_in_profit",
|
||||
"+20-30%",
|
||||
"20-30% in Profit",
|
||||
),
|
||||
_10pct_to_20pct_in_profit: CohortName::new(
|
||||
"utxos_10pct_to_20pct_in_profit",
|
||||
"+10-20%",
|
||||
"10-20% in Profit",
|
||||
),
|
||||
_0pct_to_10pct_in_profit: CohortName::new(
|
||||
"utxos_0pct_to_10pct_in_profit",
|
||||
"+0-10%",
|
||||
"0-10% in Profit",
|
||||
),
|
||||
_0pct_to_10pct_in_loss: CohortName::new(
|
||||
"utxos_0pct_to_10pct_in_loss",
|
||||
"-0-10%",
|
||||
"0-10% in Loss",
|
||||
),
|
||||
_10pct_to_20pct_in_loss: CohortName::new(
|
||||
"utxos_10pct_to_20pct_in_loss",
|
||||
"-10-20%",
|
||||
"10-20% in Loss",
|
||||
),
|
||||
_20pct_to_30pct_in_loss: CohortName::new(
|
||||
"utxos_20pct_to_30pct_in_loss",
|
||||
"-20-30%",
|
||||
"20-30% in Loss",
|
||||
),
|
||||
_30pct_to_40pct_in_loss: CohortName::new(
|
||||
"utxos_30pct_to_40pct_in_loss",
|
||||
"-30-40%",
|
||||
"30-40% in Loss",
|
||||
),
|
||||
_40pct_to_50pct_in_loss: CohortName::new(
|
||||
"utxos_40pct_to_50pct_in_loss",
|
||||
"-40-50%",
|
||||
"40-50% in Loss",
|
||||
),
|
||||
_50pct_to_60pct_in_loss: CohortName::new(
|
||||
"utxos_50pct_to_60pct_in_loss",
|
||||
"-50-60%",
|
||||
"50-60% in Loss",
|
||||
),
|
||||
_60pct_to_70pct_in_loss: CohortName::new(
|
||||
"utxos_60pct_to_70pct_in_loss",
|
||||
"-60-70%",
|
||||
"60-70% in Loss",
|
||||
),
|
||||
_70pct_to_80pct_in_loss: CohortName::new(
|
||||
"utxos_70pct_to_80pct_in_loss",
|
||||
"-70-80%",
|
||||
"70-80% in Loss",
|
||||
),
|
||||
_80pct_to_90pct_in_loss: CohortName::new(
|
||||
"utxos_80pct_to_90pct_in_loss",
|
||||
"-80-90%",
|
||||
"80-90% in Loss",
|
||||
),
|
||||
_90pct_to_100pct_in_loss: CohortName::new(
|
||||
"utxos_90pct_to_100pct_in_loss",
|
||||
"-90-100%",
|
||||
"90-100% in Loss",
|
||||
),
|
||||
};
|
||||
|
||||
impl ProfitabilityRange<CohortName> {
|
||||
|
||||
@@ -116,6 +116,23 @@ impl<T> SpendableType<T> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get(&self, output_type: OutputType) -> &T {
|
||||
match output_type {
|
||||
OutputType::P2PK65 => &self.p2pk65,
|
||||
OutputType::P2PK33 => &self.p2pk33,
|
||||
OutputType::P2PKH => &self.p2pkh,
|
||||
OutputType::P2MS => &self.p2ms,
|
||||
OutputType::P2SH => &self.p2sh,
|
||||
OutputType::P2WPKH => &self.p2wpkh,
|
||||
OutputType::P2WSH => &self.p2wsh,
|
||||
OutputType::P2TR => &self.p2tr,
|
||||
OutputType::P2A => &self.p2a,
|
||||
OutputType::Unknown => &self.unknown,
|
||||
OutputType::Empty => &self.empty,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, output_type: OutputType) -> &mut T {
|
||||
match output_type {
|
||||
OutputType::P2PK65 => &mut self.p2pk65,
|
||||
|
||||
@@ -2,8 +2,8 @@ use brk_traversable::Traversable;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::{
|
||||
AgeRange, AmountRange, ByEpoch, OverAmount, UnderAmount, UnderAge, OverAge,
|
||||
Class, SpendableType, ByTerm, Filter,
|
||||
AgeRange, AmountRange, ByEpoch, ByTerm, Class, Filter, OverAge, OverAmount, SpendableType,
|
||||
UnderAge, UnderAmount,
|
||||
};
|
||||
|
||||
#[derive(Default, Clone, Traversable)]
|
||||
|
||||
@@ -14,11 +14,8 @@ brk_error = { workspace = true, features = ["vecdb"] }
|
||||
brk_cohort = { workspace = true }
|
||||
brk_indexer = { workspace = true }
|
||||
brk_oracle = { workspace = true }
|
||||
brk_iterator = { workspace = true }
|
||||
brk_logger = { workspace = true }
|
||||
brk_reader = { workspace = true }
|
||||
brk_rpc = { workspace = true, features = ["corepc"] }
|
||||
brk_store = { workspace = true }
|
||||
brk_rpc = { workspace = true }
|
||||
brk_traversable = { workspace = true }
|
||||
brk_types = { workspace = true }
|
||||
derive_more = { workspace = true }
|
||||
@@ -33,6 +30,7 @@ smallvec = { workspace = true }
|
||||
vecdb = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
brk_reader = { workspace = true }
|
||||
brk_alloc = { workspace = true }
|
||||
brk_bencher = { workspace = true }
|
||||
color-eyre = { workspace = true }
|
||||
|
||||
@@ -8,7 +8,6 @@ use std::{
|
||||
use brk_alloc::Mimalloc;
|
||||
use brk_computer::Computer;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_iterator::Blocks;
|
||||
use brk_reader::Reader;
|
||||
use brk_rpc::{Auth, Client};
|
||||
use vecdb::Exit;
|
||||
@@ -31,8 +30,6 @@ pub fn main() -> color_eyre::Result<()> {
|
||||
|
||||
let reader = Reader::new(bitcoin_dir.join("blocks"), &client);
|
||||
|
||||
let blocks = Blocks::new(&client, &reader);
|
||||
|
||||
let mut indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
|
||||
let exit = Exit::new();
|
||||
@@ -42,7 +39,7 @@ pub fn main() -> color_eyre::Result<()> {
|
||||
let chain_height = client.get_last_height()?;
|
||||
let indexed_height = indexer.vecs.starting_height();
|
||||
if u32::from(chain_height).saturating_sub(u32::from(indexed_height)) > 1000 {
|
||||
indexer.checked_index(&blocks, &client, &exit)?;
|
||||
indexer.checked_index(&reader, &client, &exit)?;
|
||||
drop(indexer);
|
||||
Mimalloc::collect();
|
||||
indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
@@ -52,11 +49,11 @@ pub fn main() -> color_eyre::Result<()> {
|
||||
|
||||
loop {
|
||||
let i = Instant::now();
|
||||
let starting_indexes = indexer.checked_index(&blocks, &client, &exit)?;
|
||||
let starting_indexes = indexer.checked_index(&reader, &client, &exit)?;
|
||||
|
||||
Mimalloc::collect();
|
||||
|
||||
computer.compute(&indexer, starting_indexes, &reader, &exit)?;
|
||||
computer.compute(&indexer, starting_indexes, &exit)?;
|
||||
dbg!(i.elapsed());
|
||||
sleep(Duration::from_secs(10));
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ use brk_bencher::Bencher;
|
||||
use brk_computer::Computer;
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_iterator::Blocks;
|
||||
use brk_reader::Reader;
|
||||
use brk_rpc::{Auth, Client};
|
||||
use tracing::{debug, info};
|
||||
@@ -28,8 +27,6 @@ pub fn main() -> Result<()> {
|
||||
|
||||
let reader = Reader::new(bitcoin_dir.join("blocks"), &client);
|
||||
|
||||
let blocks = Blocks::new(&client, &reader);
|
||||
|
||||
let mut indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
|
||||
let mut computer = Computer::forced_import(&outputs_benches_dir, &indexer)?;
|
||||
@@ -47,13 +44,13 @@ pub fn main() -> Result<()> {
|
||||
});
|
||||
|
||||
let i = Instant::now();
|
||||
let starting_indexes = indexer.index(&blocks, &client, &exit)?;
|
||||
let starting_indexes = indexer.index(&reader, &client, &exit)?;
|
||||
info!("Done in {:?}", i.elapsed());
|
||||
|
||||
Mimalloc::collect();
|
||||
|
||||
let i = Instant::now();
|
||||
computer.compute(&indexer, starting_indexes, &reader, &exit)?;
|
||||
computer.compute(&indexer, starting_indexes, &exit)?;
|
||||
info!("Done in {:?}", i.elapsed());
|
||||
|
||||
// We want to benchmark the drop too
|
||||
|
||||
@@ -9,7 +9,6 @@ use brk_alloc::Mimalloc;
|
||||
use brk_bencher::Bencher;
|
||||
use brk_computer::Computer;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_iterator::Blocks;
|
||||
use brk_reader::Reader;
|
||||
use brk_rpc::{Auth, Client};
|
||||
use tracing::{debug, info};
|
||||
@@ -45,15 +44,13 @@ pub fn main() -> color_eyre::Result<()> {
|
||||
|
||||
let reader = Reader::new(bitcoin_dir.join("blocks"), &client);
|
||||
|
||||
let blocks = Blocks::new(&client, &reader);
|
||||
|
||||
let mut indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
|
||||
// Pre-run indexer if too far behind, then drop and reimport to reduce memory
|
||||
let chain_height = client.get_last_height()?;
|
||||
let indexed_height = indexer.vecs.starting_height();
|
||||
if chain_height.saturating_sub(*indexed_height) > 1000 {
|
||||
indexer.index(&blocks, &client, &exit)?;
|
||||
indexer.index(&reader, &client, &exit)?;
|
||||
drop(indexer);
|
||||
Mimalloc::collect();
|
||||
indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
@@ -63,13 +60,13 @@ pub fn main() -> color_eyre::Result<()> {
|
||||
|
||||
loop {
|
||||
let i = Instant::now();
|
||||
let starting_indexes = indexer.index(&blocks, &client, &exit)?;
|
||||
let starting_indexes = indexer.index(&reader, &client, &exit)?;
|
||||
info!("Done in {:?}", i.elapsed());
|
||||
|
||||
Mimalloc::collect();
|
||||
|
||||
let i = Instant::now();
|
||||
computer.compute(&indexer, starting_indexes, &reader, &exit)?;
|
||||
computer.compute(&indexer, starting_indexes, &exit)?;
|
||||
info!("Done in {:?}", i.elapsed());
|
||||
|
||||
sleep(Duration::from_secs(60));
|
||||
|
||||
@@ -17,12 +17,10 @@ impl Vecs {
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// Sequential: time → lookback (dependency chain)
|
||||
self.time
|
||||
.timestamp
|
||||
.compute(indexer, indexes, starting_indexes, exit)?;
|
||||
self.lookback
|
||||
.compute(&self.time, starting_indexes, exit)?;
|
||||
self.db.sync_bg_tasks()?;
|
||||
|
||||
// lookback depends on indexes.timestamp.monotonic
|
||||
self.lookback.compute(indexes, starting_indexes, exit)?;
|
||||
|
||||
// Parallel: remaining sub-modules are independent of each other.
|
||||
// size depends on lookback (already computed above).
|
||||
@@ -40,8 +38,7 @@ impl Vecs {
|
||||
let r1 = s.spawn(|| count.compute(indexer, starting_indexes, exit));
|
||||
let r2 = s.spawn(|| interval.compute(indexer, starting_indexes, exit));
|
||||
let r3 = s.spawn(|| weight.compute(indexer, starting_indexes, exit));
|
||||
let r4 =
|
||||
s.spawn(|| difficulty.compute(indexer, indexes, starting_indexes, exit));
|
||||
let r4 = s.spawn(|| difficulty.compute(indexer, indexes, starting_indexes, exit));
|
||||
let r5 = s.spawn(|| halving.compute(indexes, starting_indexes, exit));
|
||||
size.compute(indexer, &*lookback, starting_indexes, exit)?;
|
||||
r1.join().unwrap()?;
|
||||
@@ -52,8 +49,11 @@ impl Vecs {
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let _lock = exit.lock();
|
||||
self.db.compact()?;
|
||||
let exit = exit.clone();
|
||||
self.db.run_bg(move |db| {
|
||||
let _lock = exit.lock();
|
||||
db.compact_deferred_default()
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
BlockCountTarget24h, BlockCountTarget1w, BlockCountTarget1m, BlockCountTarget1y,
|
||||
CachedWindowStarts, PerBlockCumulativeRolling, ConstantVecs, Windows,
|
||||
BlockCountTarget1m, BlockCountTarget1w, BlockCountTarget1y, BlockCountTarget24h,
|
||||
ConstantVecs, PerBlockCumulativeRolling, WindowStartVec, Windows,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -16,14 +16,30 @@ impl Vecs {
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
target: Windows {
|
||||
_24h: ConstantVecs::new::<BlockCountTarget24h>("block_count_target_24h", version, indexes),
|
||||
_1w: ConstantVecs::new::<BlockCountTarget1w>("block_count_target_1w", version, indexes),
|
||||
_1m: ConstantVecs::new::<BlockCountTarget1m>("block_count_target_1m", version, indexes),
|
||||
_1y: ConstantVecs::new::<BlockCountTarget1y>("block_count_target_1y", version, indexes),
|
||||
_24h: ConstantVecs::new::<BlockCountTarget24h>(
|
||||
"block_count_target_24h",
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
_1w: ConstantVecs::new::<BlockCountTarget1w>(
|
||||
"block_count_target_1w",
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
_1m: ConstantVecs::new::<BlockCountTarget1m>(
|
||||
"block_count_target_1m",
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
_1y: ConstantVecs::new::<BlockCountTarget1y>(
|
||||
"block_count_target_1y",
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
},
|
||||
total: PerBlockCumulativeRolling::forced_import(
|
||||
db,
|
||||
|
||||
@@ -2,7 +2,7 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{StoredU32, StoredU64};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{PerBlockCumulativeRolling, ConstantVecs, Windows};
|
||||
use crate::internal::{ConstantVecs, PerBlockCumulativeRolling, Windows};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
|
||||
@@ -30,7 +30,7 @@ impl Vecs {
|
||||
|
||||
self.blocks_to_retarget.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexes.height.identity,
|
||||
&indexes.height.epoch,
|
||||
|(h, ..)| (h, StoredU32::from(h.left_before_next_diff_adj())),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -20,19 +20,15 @@ impl Vecs {
|
||||
) -> Result<Self> {
|
||||
let v2 = Version::TWO;
|
||||
|
||||
let hashrate = LazyPerBlock::from_height_source::<DifficultyToHashF64>(
|
||||
let hashrate = LazyPerBlock::from_height_source::<DifficultyToHashF64, _>(
|
||||
"difficulty_hashrate",
|
||||
version,
|
||||
indexer.vecs.blocks.difficulty.read_only_boxed_clone(),
|
||||
indexer.vecs.blocks.difficulty.read_only_clone(),
|
||||
indexes,
|
||||
);
|
||||
|
||||
let blocks_to_retarget = PerBlock::forced_import(
|
||||
db,
|
||||
"blocks_to_retarget",
|
||||
version + v2,
|
||||
indexes,
|
||||
)?;
|
||||
let blocks_to_retarget =
|
||||
PerBlock::forced_import(db, "blocks_to_retarget", version + v2, indexes)?;
|
||||
|
||||
let days_to_retarget = LazyPerBlock::from_computed::<BlocksToDaysF32>(
|
||||
"days_to_retarget",
|
||||
@@ -44,7 +40,7 @@ impl Vecs {
|
||||
Ok(Self {
|
||||
value: Resolutions::forced_import(
|
||||
"difficulty",
|
||||
indexer.vecs.blocks.difficulty.read_only_boxed_clone(),
|
||||
indexer.vecs.blocks.difficulty.read_only_clone(),
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
|
||||
@@ -2,7 +2,7 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPointsSigned32, Epoch, StoredF32, StoredF64, StoredU32};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{LazyPerBlock, PerBlock, Resolutions, PercentPerBlock};
|
||||
use crate::internal::{LazyPerBlock, PerBlock, PercentPerBlock, Resolutions};
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub value: Resolutions<StoredF64>,
|
||||
|
||||
@@ -21,7 +21,7 @@ impl Vecs {
|
||||
|
||||
self.blocks_to_halving.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexes.height.identity,
|
||||
&indexes.height.halving,
|
||||
|(h, ..)| (h, StoredU32::from(h.left_before_next_halving())),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -16,9 +16,8 @@ impl Vecs {
|
||||
) -> Result<Self> {
|
||||
let v2 = Version::TWO;
|
||||
|
||||
let blocks_to_halving = PerBlock::forced_import(
|
||||
db, "blocks_to_halving", version + v2, indexes,
|
||||
)?;
|
||||
let blocks_to_halving =
|
||||
PerBlock::forced_import(db, "blocks_to_halving", version + v2, indexes)?;
|
||||
|
||||
let days_to_halving = LazyPerBlock::from_computed::<BlocksToDaysF32>(
|
||||
"days_to_halving",
|
||||
|
||||
@@ -10,8 +10,7 @@ use crate::{
|
||||
};
|
||||
|
||||
use super::{
|
||||
CountVecs, DifficultyVecs, HalvingVecs, IntervalVecs, LookbackVecs, SizeVecs, TimeVecs, Vecs,
|
||||
WeightVecs,
|
||||
CountVecs, DifficultyVecs, HalvingVecs, IntervalVecs, LookbackVecs, SizeVecs, Vecs, WeightVecs,
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -25,12 +24,11 @@ impl Vecs {
|
||||
let version = parent_version;
|
||||
|
||||
let lookback = LookbackVecs::forced_import(&db, version)?;
|
||||
let cached_starts = &lookback.cached_window_starts;
|
||||
let count = CountVecs::forced_import(&db, version, indexes, cached_starts)?;
|
||||
let interval = IntervalVecs::forced_import(&db, version, indexes, cached_starts)?;
|
||||
let size = SizeVecs::forced_import(&db, version, indexes, cached_starts)?;
|
||||
let weight = WeightVecs::forced_import(&db, version, indexes, cached_starts, &size)?;
|
||||
let time = TimeVecs::forced_import(&db, version, indexes)?;
|
||||
let cached_starts = lookback.cached_window_starts();
|
||||
let count = CountVecs::forced_import(&db, version, indexes, &cached_starts)?;
|
||||
let interval = IntervalVecs::forced_import(&db, version, indexes, &cached_starts)?;
|
||||
let size = SizeVecs::forced_import(&db, version, indexes, &cached_starts)?;
|
||||
let weight = WeightVecs::forced_import(&db, version, indexes, &cached_starts, &size)?;
|
||||
let difficulty = DifficultyVecs::forced_import(&db, version, indexer, indexes)?;
|
||||
let halving = HalvingVecs::forced_import(&db, version, indexes)?;
|
||||
|
||||
@@ -41,7 +39,6 @@ impl Vecs {
|
||||
interval,
|
||||
size,
|
||||
weight,
|
||||
time,
|
||||
difficulty,
|
||||
halving,
|
||||
};
|
||||
|
||||
@@ -13,27 +13,26 @@ impl Vecs {
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let mut prev_timestamp = None;
|
||||
self.0
|
||||
.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.timestamp,
|
||||
|(h, timestamp, ..)| {
|
||||
let interval = if let Some(prev_h) = h.decremented() {
|
||||
let prev = prev_timestamp.unwrap_or_else(|| {
|
||||
indexer.vecs.blocks.timestamp.collect_one(prev_h).unwrap()
|
||||
});
|
||||
timestamp.checked_sub(prev).unwrap_or(Timestamp::ZERO)
|
||||
} else {
|
||||
Timestamp::ZERO
|
||||
};
|
||||
prev_timestamp = Some(timestamp);
|
||||
(h, interval)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.0.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.timestamp,
|
||||
|(h, timestamp, ..)| {
|
||||
let interval = if let Some(prev_h) = h.decremented() {
|
||||
let prev = prev_timestamp.unwrap_or_else(|| {
|
||||
indexer.vecs.blocks.timestamp.collect_one(prev_h).unwrap()
|
||||
});
|
||||
timestamp.checked_sub(prev).unwrap_or(Timestamp::ZERO)
|
||||
} else {
|
||||
Timestamp::ZERO
|
||||
};
|
||||
prev_timestamp = Some(timestamp);
|
||||
(h, interval)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -3,14 +3,17 @@ use brk_types::Version;
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{indexes, internal::{CachedWindowStarts, PerBlockRollingAverage}};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{PerBlockRollingAverage, WindowStartVec, Windows},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
let interval = PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
|
||||
@@ -8,5 +8,5 @@ use crate::internal::PerBlockRollingAverage;
|
||||
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub PerBlockRollingAverage<Timestamp, M>,
|
||||
#[traversable(flatten)] pub PerBlockRollingAverage<Timestamp, Timestamp, M>,
|
||||
);
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Indexes, Timestamp, Version};
|
||||
use vecdb::{AnyVec, CachedVec, Cursor, Database, EagerVec, Exit, ImportableVec, PcoVec, ReadableVec, Rw, StorageMode, VecIndex};
|
||||
use vecdb::{
|
||||
AnyVec, CachedVec, Cursor, Database, EagerVec, Exit, ImportableVec, PcoVec, ReadableVec, Rw,
|
||||
StorageMode, VecIndex,
|
||||
};
|
||||
|
||||
use crate::internal::{CachedWindowStarts, Windows, WindowStarts};
|
||||
|
||||
use super::time;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{WindowStartVec, WindowStarts, Windows},
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
#[traversable(skip)]
|
||||
pub cached_window_starts: CachedWindowStarts,
|
||||
pub _1h: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _24h: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 1d
|
||||
pub _24h: CachedVec<M::Stored<EagerVec<PcoVec<Height, Height>>>>, // 1d
|
||||
pub _3d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _1w: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 7d
|
||||
pub _1w: CachedVec<M::Stored<EagerVec<PcoVec<Height, Height>>>>, // 7d
|
||||
pub _8d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _9d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _12d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
@@ -22,7 +24,7 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub _2w: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 14d
|
||||
pub _21d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _26d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _1m: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 30d
|
||||
pub _1m: CachedVec<M::Stored<EagerVec<PcoVec<Height, Height>>>>, // 30d
|
||||
pub _34d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _55d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _2m: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 60d
|
||||
@@ -39,7 +41,7 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub _9m: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 270d
|
||||
pub _350d: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub _12m: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 360d
|
||||
pub _1y: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 365d
|
||||
pub _1y: CachedVec<M::Stored<EagerVec<PcoVec<Height, Height>>>>, // 365d
|
||||
pub _14m: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 420d
|
||||
pub _2y: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 730d
|
||||
pub _26m: M::Stored<EagerVec<PcoVec<Height, Height>>>, // 780d
|
||||
@@ -102,24 +104,55 @@ impl Vecs {
|
||||
let _14y = ImportableVec::forced_import(db, "height_14y_ago", version)?;
|
||||
let _26y = ImportableVec::forced_import(db, "height_26y_ago", version)?;
|
||||
|
||||
let cached_window_starts = CachedWindowStarts(Windows {
|
||||
_24h: CachedVec::new(&_24h),
|
||||
_1w: CachedVec::new(&_1w),
|
||||
_1m: CachedVec::new(&_1m),
|
||||
_1y: CachedVec::new(&_1y),
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
cached_window_starts,
|
||||
_1h, _24h, _3d, _1w, _8d, _9d, _12d, _13d, _2w, _21d, _26d,
|
||||
_1m, _34d, _55d, _2m, _9w, _12w, _89d, _3m, _14w, _111d, _144d,
|
||||
_6m, _26w, _200d, _9m, _350d, _12m, _1y, _14m, _2y, _26m, _3y,
|
||||
_200w, _4y, _5y, _6y, _8y, _9y, _10y, _12y, _14y, _26y,
|
||||
_1h,
|
||||
_24h: CachedVec::wrap(_24h),
|
||||
_3d,
|
||||
_1w: CachedVec::wrap(_1w),
|
||||
_8d,
|
||||
_9d,
|
||||
_12d,
|
||||
_13d,
|
||||
_2w,
|
||||
_21d,
|
||||
_26d,
|
||||
_1m: CachedVec::wrap(_1m),
|
||||
_34d,
|
||||
_55d,
|
||||
_2m,
|
||||
_9w,
|
||||
_12w,
|
||||
_89d,
|
||||
_3m,
|
||||
_14w,
|
||||
_111d,
|
||||
_144d,
|
||||
_6m,
|
||||
_26w,
|
||||
_200d,
|
||||
_9m,
|
||||
_350d,
|
||||
_12m,
|
||||
_1y: CachedVec::wrap(_1y),
|
||||
_14m,
|
||||
_2y,
|
||||
_26m,
|
||||
_3y,
|
||||
_200w,
|
||||
_4y,
|
||||
_5y,
|
||||
_6y,
|
||||
_8y,
|
||||
_9y,
|
||||
_10y,
|
||||
_12y,
|
||||
_14y,
|
||||
_26y,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn window_starts(&self) -> WindowStarts<'_> {
|
||||
WindowStarts {
|
||||
pub fn cached_window_starts(&self) -> Windows<&WindowStartVec> {
|
||||
Windows {
|
||||
_24h: &self._24h,
|
||||
_1w: &self._1w,
|
||||
_1m: &self._1m,
|
||||
@@ -127,12 +160,20 @@ impl Vecs {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_starts(&self) -> WindowStarts<'_> {
|
||||
WindowStarts {
|
||||
_24h: &self._24h.inner,
|
||||
_1w: &self._1w.inner,
|
||||
_1m: &self._1m.inner,
|
||||
_1y: &self._1y.inner,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_vec(&self, days: usize) -> &EagerVec<PcoVec<Height, Height>> {
|
||||
match days {
|
||||
1 => &self._24h,
|
||||
1 => &self._24h.inner,
|
||||
3 => &self._3d,
|
||||
7 => &self._1w,
|
||||
7 => &self._1w.inner,
|
||||
8 => &self._8d,
|
||||
9 => &self._9d,
|
||||
12 => &self._12d,
|
||||
@@ -140,7 +181,7 @@ impl Vecs {
|
||||
14 => &self._2w,
|
||||
21 => &self._21d,
|
||||
26 => &self._26d,
|
||||
30 => &self._1m,
|
||||
30 => &self._1m.inner,
|
||||
34 => &self._34d,
|
||||
55 => &self._55d,
|
||||
60 => &self._2m,
|
||||
@@ -157,7 +198,7 @@ impl Vecs {
|
||||
270 => &self._9m,
|
||||
350 => &self._350d,
|
||||
360 => &self._12m,
|
||||
365 => &self._1y,
|
||||
365 => &self._1y.inner,
|
||||
420 => &self._14m,
|
||||
730 => &self._2y,
|
||||
780 => &self._26m,
|
||||
@@ -178,80 +219,60 @@ impl Vecs {
|
||||
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
time: &time::Vecs,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.compute_rolling_start_hours(time, starting_indexes, exit, 1, |s| {
|
||||
&mut s._1h
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 1, |s| &mut s._24h)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 3, |s| &mut s._3d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 7, |s| &mut s._1w)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 8, |s| &mut s._8d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 9, |s| &mut s._9d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 12, |s| &mut s._12d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 13, |s| &mut s._13d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 14, |s| &mut s._2w)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 21, |s| &mut s._21d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 26, |s| &mut s._26d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 30, |s| &mut s._1m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 34, |s| &mut s._34d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 55, |s| &mut s._55d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 60, |s| &mut s._2m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 63, |s| &mut s._9w)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 84, |s| &mut s._12w)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 89, |s| &mut s._89d)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 90, |s| &mut s._3m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 98, |s| &mut s._14w)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 111, |s| {
|
||||
&mut s._111d
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 144, |s| {
|
||||
&mut s._144d
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 180, |s| &mut s._6m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 182, |s| &mut s._26w)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 200, |s| {
|
||||
&mut s._200d
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 270, |s| &mut s._9m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 350, |s| {
|
||||
&mut s._350d
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 360, |s| &mut s._12m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 365, |s| &mut s._1y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 420, |s| &mut s._14m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 730, |s| &mut s._2y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 780, |s| &mut s._26m)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 1095, |s| &mut s._3y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 1400, |s| {
|
||||
&mut s._200w
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 1460, |s| &mut s._4y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 1825, |s| &mut s._5y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 2190, |s| &mut s._6y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 2920, |s| &mut s._8y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 3285, |s| &mut s._9y)?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 3650, |s| {
|
||||
&mut s._10y
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 4380, |s| {
|
||||
&mut s._12y
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 5110, |s| {
|
||||
&mut s._14y
|
||||
})?;
|
||||
self.compute_rolling_start(time, starting_indexes, exit, 9490, |s| {
|
||||
&mut s._26y
|
||||
})?;
|
||||
self.compute_rolling_start_hours(indexes, starting_indexes, exit, 1, |s| &mut s._1h)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 1, |s| &mut s._24h.inner)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 3, |s| &mut s._3d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 7, |s| &mut s._1w.inner)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 8, |s| &mut s._8d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 9, |s| &mut s._9d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 12, |s| &mut s._12d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 13, |s| &mut s._13d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 14, |s| &mut s._2w)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 21, |s| &mut s._21d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 26, |s| &mut s._26d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 30, |s| &mut s._1m.inner)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 34, |s| &mut s._34d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 55, |s| &mut s._55d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 60, |s| &mut s._2m)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 63, |s| &mut s._9w)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 84, |s| &mut s._12w)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 89, |s| &mut s._89d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 90, |s| &mut s._3m)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 98, |s| &mut s._14w)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 111, |s| &mut s._111d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 144, |s| &mut s._144d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 180, |s| &mut s._6m)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 182, |s| &mut s._26w)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 200, |s| &mut s._200d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 270, |s| &mut s._9m)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 350, |s| &mut s._350d)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 360, |s| &mut s._12m)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 365, |s| &mut s._1y.inner)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 420, |s| &mut s._14m)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 730, |s| &mut s._2y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 780, |s| &mut s._26m)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 1095, |s| &mut s._3y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 1400, |s| &mut s._200w)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 1460, |s| &mut s._4y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 1825, |s| &mut s._5y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 2190, |s| &mut s._6y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 2920, |s| &mut s._8y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 3285, |s| &mut s._9y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 3650, |s| &mut s._10y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 4380, |s| &mut s._12y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 5110, |s| &mut s._14y)?;
|
||||
self.compute_rolling_start(indexes, starting_indexes, exit, 9490, |s| &mut s._26y)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_rolling_start<F>(
|
||||
&mut self,
|
||||
time: &time::Vecs,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
days: usize,
|
||||
@@ -260,14 +281,18 @@ impl Vecs {
|
||||
where
|
||||
F: FnOnce(&mut Self) -> &mut EagerVec<PcoVec<Height, Height>>,
|
||||
{
|
||||
self.compute_rolling_start_inner(time, starting_indexes, exit, get_field, |t, prev_ts| {
|
||||
t.difference_in_days_between(prev_ts) >= days
|
||||
})
|
||||
self.compute_rolling_start_inner(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
exit,
|
||||
get_field,
|
||||
|t, prev_ts| t.difference_in_days_between(prev_ts) >= days,
|
||||
)
|
||||
}
|
||||
|
||||
fn compute_rolling_start_hours<F>(
|
||||
&mut self,
|
||||
time: &time::Vecs,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
hours: usize,
|
||||
@@ -276,14 +301,18 @@ impl Vecs {
|
||||
where
|
||||
F: FnOnce(&mut Self) -> &mut EagerVec<PcoVec<Height, Height>>,
|
||||
{
|
||||
self.compute_rolling_start_inner(time, starting_indexes, exit, get_field, |t, prev_ts| {
|
||||
t.difference_in_hours_between(prev_ts) >= hours
|
||||
})
|
||||
self.compute_rolling_start_inner(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
exit,
|
||||
get_field,
|
||||
|t, prev_ts| t.difference_in_hours_between(prev_ts) >= hours,
|
||||
)
|
||||
}
|
||||
|
||||
fn compute_rolling_start_inner<F, D>(
|
||||
&mut self,
|
||||
time: &time::Vecs,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
get_field: F,
|
||||
@@ -300,12 +329,12 @@ impl Vecs {
|
||||
} else {
|
||||
Height::ZERO
|
||||
};
|
||||
let mut cursor = Cursor::new(&time.timestamp_monotonic);
|
||||
let mut cursor = Cursor::new(&indexes.timestamp.monotonic);
|
||||
cursor.advance(prev.to_usize());
|
||||
let mut prev_ts = cursor.next().unwrap();
|
||||
Ok(field.compute_transform(
|
||||
starting_indexes.height,
|
||||
&time.timestamp_monotonic,
|
||||
&indexes.timestamp.monotonic,
|
||||
|(h, t, ..)| {
|
||||
while expired(t, prev_ts) {
|
||||
prev.increment();
|
||||
|
||||
@@ -4,7 +4,6 @@ pub mod halving;
|
||||
pub mod interval;
|
||||
pub mod lookback;
|
||||
pub mod size;
|
||||
pub mod time;
|
||||
pub mod weight;
|
||||
|
||||
mod compute;
|
||||
@@ -19,7 +18,6 @@ pub use halving::Vecs as HalvingVecs;
|
||||
pub use interval::Vecs as IntervalVecs;
|
||||
pub use lookback::Vecs as LookbackVecs;
|
||||
pub use size::Vecs as SizeVecs;
|
||||
pub use time::Vecs as TimeVecs;
|
||||
pub use weight::Vecs as WeightVecs;
|
||||
|
||||
pub const DB_NAME: &str = "blocks";
|
||||
@@ -37,7 +35,7 @@ pub(crate) const ONE_TERA_HASH: f64 = 1_000_000_000_000.0;
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
#[traversable(skip)]
|
||||
pub(crate) db: Database,
|
||||
pub db: Database,
|
||||
|
||||
pub count: CountVecs<M>,
|
||||
pub lookback: LookbackVecs<M>,
|
||||
@@ -46,7 +44,6 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub size: SizeVecs<M>,
|
||||
#[traversable(flatten)]
|
||||
pub weight: WeightVecs<M>,
|
||||
pub time: TimeVecs<M>,
|
||||
pub difficulty: DifficultyVecs<M>,
|
||||
pub halving: HalvingVecs<M>,
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use vecdb::Database;
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{CachedWindowStarts, PerBlockFull, PerBlockRolling},
|
||||
internal::{PerBlockFull, PerBlockRolling, WindowStartVec, Windows},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -13,7 +13,7 @@ impl Vecs {
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
vbytes: PerBlockFull::forced_import(
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use vecdb::{Exit, ReadableVec};
|
||||
|
||||
use super::Vecs;
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
starting_height: brk_types::Height,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let mut prev_timestamp_monotonic = None;
|
||||
self.timestamp_monotonic.compute_transform(
|
||||
starting_height,
|
||||
&indexer.vecs.blocks.timestamp,
|
||||
|(h, timestamp, this)| {
|
||||
if prev_timestamp_monotonic.is_none()
|
||||
&& let Some(prev_h) = h.decremented()
|
||||
{
|
||||
prev_timestamp_monotonic.replace(this.collect_one(prev_h).unwrap());
|
||||
}
|
||||
let timestamp_monotonic =
|
||||
prev_timestamp_monotonic.map_or(timestamp, |prev_d| prev_d.max(timestamp));
|
||||
prev_timestamp_monotonic.replace(timestamp_monotonic);
|
||||
(h, timestamp_monotonic)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
use brk_error::Result;
|
||||
use brk_types::{Date, Height, Version};
|
||||
use vecdb::{Database, EagerVec, ImportableVec, LazyVecFrom1, ReadableCloneableVec};
|
||||
|
||||
use super::{TimestampIndexes, Vecs};
|
||||
use crate::indexes;
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let timestamp_monotonic = EagerVec::forced_import(db, "timestamp_monotonic", version)?;
|
||||
|
||||
Ok(Self {
|
||||
date: LazyVecFrom1::init(
|
||||
"date",
|
||||
version,
|
||||
timestamp_monotonic.read_only_boxed_clone(),
|
||||
|_height: Height, timestamp| Date::from(timestamp),
|
||||
),
|
||||
timestamp_monotonic,
|
||||
timestamp: TimestampIndexes::forced_import(db, version, indexes)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TimestampIndexes {
|
||||
fn forced_import(db: &Database, version: Version, indexes: &indexes::Vecs) -> Result<Self> {
|
||||
macro_rules! period {
|
||||
($field:ident) => {
|
||||
LazyVecFrom1::init(
|
||||
"timestamp",
|
||||
version,
|
||||
indexes.$field.first_height.read_only_boxed_clone(),
|
||||
|idx, _: Height| idx.to_timestamp(),
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! epoch {
|
||||
($field:ident) => {
|
||||
ImportableVec::forced_import(db, "timestamp", version)?
|
||||
};
|
||||
}
|
||||
|
||||
Ok(Self(crate::internal::PerResolution {
|
||||
minute10: period!(minute10),
|
||||
minute30: period!(minute30),
|
||||
hour1: period!(hour1),
|
||||
hour4: period!(hour4),
|
||||
hour12: period!(hour12),
|
||||
day1: period!(day1),
|
||||
day3: period!(day3),
|
||||
week1: period!(week1),
|
||||
month1: period!(month1),
|
||||
month3: period!(month3),
|
||||
month6: period!(month6),
|
||||
year1: period!(year1),
|
||||
year10: period!(year10),
|
||||
halving: epoch!(halving),
|
||||
epoch: epoch!(difficulty),
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
mod compute;
|
||||
mod import;
|
||||
mod vecs;
|
||||
|
||||
pub use vecs::{TimestampIndexes, Vecs};
|
||||
@@ -1,80 +0,0 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
Date, Day1, Day3, Epoch, Halving, Height, Hour1, Hour4, Hour12, Indexes,
|
||||
Minute10, Minute30, Month1, Month3, Month6, Timestamp, Week1, Year1, Year10,
|
||||
};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{EagerVec, Exit, LazyVecFrom1, PcoVec, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{indexes, internal::PerResolution};
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub date: LazyVecFrom1<Height, Date, Height, Timestamp>,
|
||||
pub timestamp_monotonic: M::Stored<EagerVec<PcoVec<Height, Timestamp>>>,
|
||||
pub timestamp: TimestampIndexes<M>,
|
||||
}
|
||||
|
||||
/// Per-period timestamp indexes.
|
||||
///
|
||||
/// Time-based periods (minute10–year10) are lazy: `idx.to_timestamp()` is a pure
|
||||
/// function of the index, so no storage or decompression is needed.
|
||||
/// Epoch-based periods (halving, difficulty) are eager: their timestamps
|
||||
/// come from block data via `compute_indirect_sequential`.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
#[traversable(transparent)]
|
||||
pub struct TimestampIndexes<M: StorageMode = Rw>(
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub PerResolution<
|
||||
LazyVecFrom1<Minute10, Timestamp, Minute10, Height>,
|
||||
LazyVecFrom1<Minute30, Timestamp, Minute30, Height>,
|
||||
LazyVecFrom1<Hour1, Timestamp, Hour1, Height>,
|
||||
LazyVecFrom1<Hour4, Timestamp, Hour4, Height>,
|
||||
LazyVecFrom1<Hour12, Timestamp, Hour12, Height>,
|
||||
LazyVecFrom1<Day1, Timestamp, Day1, Height>,
|
||||
LazyVecFrom1<Day3, Timestamp, Day3, Height>,
|
||||
LazyVecFrom1<Week1, Timestamp, Week1, Height>,
|
||||
LazyVecFrom1<Month1, Timestamp, Month1, Height>,
|
||||
LazyVecFrom1<Month3, Timestamp, Month3, Height>,
|
||||
LazyVecFrom1<Month6, Timestamp, Month6, Height>,
|
||||
LazyVecFrom1<Year1, Timestamp, Year1, Height>,
|
||||
LazyVecFrom1<Year10, Timestamp, Year10, Height>,
|
||||
M::Stored<EagerVec<PcoVec<Halving, Timestamp>>>,
|
||||
M::Stored<EagerVec<PcoVec<Epoch, Timestamp>>>,
|
||||
>,
|
||||
);
|
||||
|
||||
impl TimestampIndexes {
|
||||
/// Compute epoch timestamps via indirect lookup from block timestamps.
|
||||
/// Time-based periods are lazy (idx.to_timestamp()) and need no compute.
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &brk_indexer::Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let prev_height = starting_indexes.height.decremented().unwrap_or_default();
|
||||
self.halving.compute_indirect_sequential(
|
||||
indexes
|
||||
.height
|
||||
.halving
|
||||
.collect_one(prev_height)
|
||||
.unwrap_or_default(),
|
||||
&indexes.halving.first_height,
|
||||
&indexer.vecs.blocks.timestamp,
|
||||
exit,
|
||||
)?;
|
||||
self.epoch.compute_indirect_sequential(
|
||||
indexes
|
||||
.height
|
||||
.epoch
|
||||
.collect_one(prev_height)
|
||||
.unwrap_or_default(),
|
||||
&indexes.epoch.first_height,
|
||||
&indexer.vecs.blocks.timestamp,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use super::Vecs;
|
||||
use crate::{
|
||||
blocks::SizeVecs,
|
||||
indexes,
|
||||
internal::{CachedWindowStarts, LazyPerBlockRolling, PercentVec, VBytesToWeight},
|
||||
internal::{LazyPerBlockRolling, PercentVec, VBytesToWeight, WindowStartVec, Windows},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -14,7 +14,7 @@ impl Vecs {
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
size: &SizeVecs,
|
||||
) -> Result<Self> {
|
||||
let weight = LazyPerBlockRolling::from_per_block_full::<VBytesToWeight>(
|
||||
|
||||
@@ -6,7 +6,7 @@ use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
CachedWindowStarts, LazyPerBlock, OneMinusF64, PerBlock, PerBlockCumulativeRolling,
|
||||
LazyPerBlock, OneMinusF64, PerBlock, PerBlockCumulativeRolling, WindowStartVec, Windows,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -15,7 +15,7 @@ impl Vecs {
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
let liveliness = PerBlock::forced_import(db, "liveliness", version, indexes)?;
|
||||
|
||||
@@ -28,10 +28,18 @@ impl Vecs {
|
||||
|
||||
Ok(Self {
|
||||
coinblocks_created: PerBlockCumulativeRolling::forced_import(
|
||||
db, "coinblocks_created", version, indexes, cached_starts,
|
||||
db,
|
||||
"coinblocks_created",
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
coinblocks_stored: PerBlockCumulativeRolling::forced_import(
|
||||
db, "coinblocks_stored", version, indexes, cached_starts,
|
||||
db,
|
||||
"coinblocks_stored",
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
liveliness,
|
||||
vaultedness,
|
||||
|
||||
@@ -17,6 +17,8 @@ impl Vecs {
|
||||
distribution: &distribution::Vecs,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.db.sync_bg_tasks()?;
|
||||
|
||||
// Activity computes first (liveliness, vaultedness, etc.)
|
||||
self.activity
|
||||
.compute(starting_indexes, distribution, exit)?;
|
||||
@@ -80,8 +82,11 @@ impl Vecs {
|
||||
r3?;
|
||||
r4?;
|
||||
|
||||
let _lock = exit.lock();
|
||||
self.db.compact()?;
|
||||
let exit = exit.clone();
|
||||
self.db.run_bg(move |db| {
|
||||
let _lock = exit.lock();
|
||||
db.compact_deferred_default()
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,14 +13,14 @@ use super::{
|
||||
ValueVecs, Vecs,
|
||||
};
|
||||
|
||||
use crate::internal::CachedWindowStarts;
|
||||
use crate::internal::{WindowStartVec, Windows};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
parent_path: &Path,
|
||||
parent_version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
let db = open_db(parent_path, DB_NAME, 250_000)?;
|
||||
let version = parent_version;
|
||||
|
||||
@@ -22,11 +22,8 @@ impl Vecs {
|
||||
let circulating_supply = &all_metrics.supply.total.btc.height;
|
||||
let realized_price = &all_metrics.realized.price.cents.height;
|
||||
|
||||
self.vaulted.compute_all(
|
||||
prices,
|
||||
starting_indexes,
|
||||
exit,
|
||||
|v| {
|
||||
self.vaulted
|
||||
.compute_all(prices, starting_indexes, exit, |v| {
|
||||
Ok(v.compute_transform2(
|
||||
starting_indexes.height,
|
||||
realized_price,
|
||||
@@ -36,14 +33,10 @@ impl Vecs {
|
||||
},
|
||||
exit,
|
||||
)?)
|
||||
},
|
||||
)?;
|
||||
})?;
|
||||
|
||||
self.active.compute_all(
|
||||
prices,
|
||||
starting_indexes,
|
||||
exit,
|
||||
|v| {
|
||||
self.active
|
||||
.compute_all(prices, starting_indexes, exit, |v| {
|
||||
Ok(v.compute_transform2(
|
||||
starting_indexes.height,
|
||||
realized_price,
|
||||
@@ -53,14 +46,10 @@ impl Vecs {
|
||||
},
|
||||
exit,
|
||||
)?)
|
||||
},
|
||||
)?;
|
||||
})?;
|
||||
|
||||
self.true_market_mean.compute_all(
|
||||
prices,
|
||||
starting_indexes,
|
||||
exit,
|
||||
|v| {
|
||||
self.true_market_mean
|
||||
.compute_all(prices, starting_indexes, exit, |v| {
|
||||
Ok(v.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&cap.investor.cents.height,
|
||||
@@ -70,14 +59,10 @@ impl Vecs {
|
||||
},
|
||||
exit,
|
||||
)?)
|
||||
},
|
||||
)?;
|
||||
})?;
|
||||
|
||||
self.cointime.compute_all(
|
||||
prices,
|
||||
starting_indexes,
|
||||
exit,
|
||||
|v| {
|
||||
self.cointime
|
||||
.compute_all(prices, starting_indexes, exit, |v| {
|
||||
Ok(v.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&cap.cointime.cents.height,
|
||||
@@ -87,8 +72,7 @@ impl Vecs {
|
||||
},
|
||||
exit,
|
||||
)?)
|
||||
},
|
||||
)?;
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -3,10 +3,7 @@ use brk_types::Version;
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::PriceWithRatioExtendedPerBlock,
|
||||
};
|
||||
use crate::{indexes, internal::PriceWithRatioExtendedPerBlock};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
|
||||
@@ -38,7 +38,8 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.vaulted.compute(prices, starting_indexes.height, exit)?;
|
||||
self.vaulted
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.active.compute(prices, starting_indexes.height, exit)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -3,7 +3,7 @@ use brk_types::Version;
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{indexes, internal::AmountPerBlock};
|
||||
use crate::{indexes, internal::ValuePerBlock};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
@@ -12,13 +12,8 @@ impl Vecs {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
vaulted: AmountPerBlock::forced_import(
|
||||
db,
|
||||
"vaulted_supply",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
active: AmountPerBlock::forced_import(db, "active_supply", version, indexes)?,
|
||||
vaulted: ValuePerBlock::forced_import(db, "vaulted_supply", version, indexes)?,
|
||||
active: ValuePerBlock::forced_import(db, "active_supply", version, indexes)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use brk_traversable::Traversable;
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::AmountPerBlock;
|
||||
use crate::internal::ValuePerBlock;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub vaulted: AmountPerBlock<M>,
|
||||
pub active: AmountPerBlock<M>,
|
||||
pub vaulted: ValuePerBlock<M>,
|
||||
pub active: ValuePerBlock<M>,
|
||||
}
|
||||
|
||||
@@ -31,52 +31,49 @@ impl Vecs {
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
self.created
|
||||
.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_multiply(
|
||||
starting_indexes.height,
|
||||
&prices.spot.usd.height,
|
||||
&activity.coinblocks_created.block,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.created.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_multiply(
|
||||
starting_indexes.height,
|
||||
&prices.spot.usd.height,
|
||||
&activity.coinblocks_created.block,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
self.stored
|
||||
.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_multiply(
|
||||
starting_indexes.height,
|
||||
&prices.spot.usd.height,
|
||||
&activity.coinblocks_stored.block,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.stored.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_multiply(
|
||||
starting_indexes.height,
|
||||
&prices.spot.usd.height,
|
||||
&activity.coinblocks_stored.block,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// VOCDD: Value of Coin Days Destroyed = price × (CDD / circulating_supply)
|
||||
// Supply-adjusted to account for growing supply over time
|
||||
// This is a key input for Reserve Risk / HODL Bank calculation
|
||||
self.vocdd
|
||||
.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&prices.spot.usd.height,
|
||||
&coindays_destroyed.block,
|
||||
circulating_supply,
|
||||
|(i, price, cdd, supply, _): (_, Dollars, StoredF64, Bitcoin, _)| {
|
||||
let supply_f64 = f64::from(supply);
|
||||
if supply_f64 == 0.0 {
|
||||
(i, StoredF64::from(0.0))
|
||||
} else {
|
||||
// VOCDD = price × (CDD / supply)
|
||||
let vocdd = f64::from(price) * f64::from(cdd) / supply_f64;
|
||||
(i, StoredF64::from(vocdd))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.vocdd.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&prices.spot.usd.height,
|
||||
&coindays_destroyed.block,
|
||||
circulating_supply,
|
||||
|(i, price, cdd, supply, _): (_, Dollars, StoredF64, Bitcoin, _)| {
|
||||
let supply_f64 = f64::from(supply);
|
||||
if supply_f64 == 0.0 {
|
||||
(i, StoredF64::from(0.0))
|
||||
} else {
|
||||
// VOCDD = price × (CDD / supply)
|
||||
let vocdd = f64::from(price) * f64::from(cdd) / supply_f64;
|
||||
(i, StoredF64::from(vocdd))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use vecdb::Database;
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{CachedWindowStarts, PerBlockCumulativeRolling},
|
||||
internal::{PerBlockCumulativeRolling, WindowStartVec, Windows},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -13,7 +13,7 @@ impl Vecs {
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
destroyed: PerBlockCumulativeRolling::forced_import(
|
||||
|
||||
@@ -7,19 +7,20 @@
|
||||
//! | `receiving` | Unique addresses that received this block |
|
||||
//! | `sending` | Unique addresses that sent this block |
|
||||
//! | `reactivated` | Addresses that were empty and now have funds |
|
||||
//! | `both` | Addresses that both sent AND received same block |
|
||||
//! | `bidirectional` | Addresses that both sent AND received in same block |
|
||||
//! | `active` | Distinct addresses involved (sent ∪ received) |
|
||||
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, StoredU32, Version};
|
||||
use brk_types::{Height, StoredU32, StoredU64, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, AnyVec, Database, Exit, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{CachedWindowStarts, PerBlockRollingAverage},
|
||||
internal::{PerBlockRollingAverage, WindowStartVec, Windows},
|
||||
};
|
||||
|
||||
/// Per-block activity counts - reset each block.
|
||||
@@ -28,7 +29,7 @@ pub struct BlockActivityCounts {
|
||||
pub reactivated: u32,
|
||||
pub sending: u32,
|
||||
pub receiving: u32,
|
||||
pub both: u32,
|
||||
pub bidirectional: u32,
|
||||
}
|
||||
|
||||
impl BlockActivityCounts {
|
||||
@@ -56,7 +57,7 @@ impl AddrTypeToActivityCounts {
|
||||
total.reactivated += counts.reactivated;
|
||||
total.sending += counts.sending;
|
||||
total.receiving += counts.receiving;
|
||||
total.both += counts.both;
|
||||
total.bidirectional += counts.bidirectional;
|
||||
}
|
||||
total
|
||||
}
|
||||
@@ -65,45 +66,61 @@ impl AddrTypeToActivityCounts {
|
||||
/// Activity count vectors for a single category (e.g., one address type or "all").
|
||||
#[derive(Traversable)]
|
||||
pub struct ActivityCountVecs<M: StorageMode = Rw> {
|
||||
pub reactivated: PerBlockRollingAverage<StoredU32, M>,
|
||||
pub sending: PerBlockRollingAverage<StoredU32, M>,
|
||||
pub receiving: PerBlockRollingAverage<StoredU32, M>,
|
||||
pub both: PerBlockRollingAverage<StoredU32, M>,
|
||||
pub reactivated: PerBlockRollingAverage<StoredU32, StoredU64, M>,
|
||||
pub sending: PerBlockRollingAverage<StoredU32, StoredU64, M>,
|
||||
pub receiving: PerBlockRollingAverage<StoredU32, StoredU64, M>,
|
||||
pub bidirectional: PerBlockRollingAverage<StoredU32, StoredU64, M>,
|
||||
/// Distinct addresses involved in this block (sent ∪ received),
|
||||
/// computed at push time as `sending + receiving - bidirectional`
|
||||
/// via inclusion-exclusion. For per-type instances this is
|
||||
/// per-type. For the `all` aggregate it's the cross-type total.
|
||||
pub active: PerBlockRollingAverage<StoredU32, StoredU64, M>,
|
||||
}
|
||||
|
||||
impl ActivityCountVecs {
|
||||
/// `prefix` is prepended to each field's disk name. Use `""` for the
|
||||
/// "all" aggregate and `"{type}_"` for per-address-type instances.
|
||||
/// Field names are suffixed with `_addrs` so the final disk series
|
||||
/// are e.g. `active_addrs`, `p2tr_bidirectional_addrs`.
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
prefix: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
reactivated: PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
&format!("{name}_reactivated"),
|
||||
&format!("{prefix}reactivated_addrs"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
sending: PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
&format!("{name}_sending"),
|
||||
&format!("{prefix}sending_addrs"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
receiving: PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
&format!("{name}_receiving"),
|
||||
&format!("{prefix}receiving_addrs"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
both: PerBlockRollingAverage::forced_import(
|
||||
bidirectional: PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
&format!("{name}_both"),
|
||||
&format!("{prefix}bidirectional_addrs"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
active: PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
&format!("{prefix}active_addrs"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
@@ -117,7 +134,8 @@ impl ActivityCountVecs {
|
||||
.len()
|
||||
.min(self.sending.block.len())
|
||||
.min(self.receiving.block.len())
|
||||
.min(self.both.block.len())
|
||||
.min(self.bidirectional.block.len())
|
||||
.min(self.active.block.len())
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_height_mut(
|
||||
@@ -125,9 +143,10 @@ impl ActivityCountVecs {
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
[
|
||||
&mut self.reactivated.block as &mut dyn AnyStoredVec,
|
||||
&mut self.sending.block as &mut dyn AnyStoredVec,
|
||||
&mut self.receiving.block as &mut dyn AnyStoredVec,
|
||||
&mut self.both.block as &mut dyn AnyStoredVec,
|
||||
&mut self.sending.block,
|
||||
&mut self.receiving.block,
|
||||
&mut self.bidirectional.block,
|
||||
&mut self.active.block,
|
||||
]
|
||||
.into_par_iter()
|
||||
}
|
||||
@@ -136,7 +155,8 @@ impl ActivityCountVecs {
|
||||
self.reactivated.block.reset()?;
|
||||
self.sending.block.reset()?;
|
||||
self.receiving.block.reset()?;
|
||||
self.both.block.reset()?;
|
||||
self.bidirectional.block.reset()?;
|
||||
self.active.block.reset()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -145,18 +165,17 @@ impl ActivityCountVecs {
|
||||
self.reactivated.block.push(counts.reactivated.into());
|
||||
self.sending.block.push(counts.sending.into());
|
||||
self.receiving.block.push(counts.receiving.into());
|
||||
self.both.block.push(counts.both.into());
|
||||
self.bidirectional.block.push(counts.bidirectional.into());
|
||||
let active = counts.sending + counts.receiving - counts.bidirectional;
|
||||
self.active.block.push(active.into());
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
pub(crate) fn compute_rest(&mut self, max_from: Height, exit: &Exit) -> Result<()> {
|
||||
self.reactivated.compute_rest(max_from, exit)?;
|
||||
self.sending.compute_rest(max_from, exit)?;
|
||||
self.receiving.compute_rest(max_from, exit)?;
|
||||
self.both.compute_rest(max_from, exit)?;
|
||||
self.bidirectional.compute_rest(max_from, exit)?;
|
||||
self.active.compute_rest(max_from, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -175,22 +194,21 @@ impl From<ByAddrType<ActivityCountVecs>> for AddrTypeToActivityCountVecs {
|
||||
impl AddrTypeToActivityCountVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self::from(
|
||||
ByAddrType::<ActivityCountVecs>::new_with_name(|type_name| {
|
||||
Ok(Self::from(ByAddrType::<ActivityCountVecs>::new_with_name(
|
||||
|type_name| {
|
||||
ActivityCountVecs::forced_import(
|
||||
db,
|
||||
&format!("{type_name}_{name}"),
|
||||
&format!("{type_name}_"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)
|
||||
})?,
|
||||
))
|
||||
},
|
||||
)?))
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
@@ -209,7 +227,8 @@ impl AddrTypeToActivityCountVecs {
|
||||
vecs.push(&mut type_vecs.reactivated.block);
|
||||
vecs.push(&mut type_vecs.sending.block);
|
||||
vecs.push(&mut type_vecs.receiving.block);
|
||||
vecs.push(&mut type_vecs.both.block);
|
||||
vecs.push(&mut type_vecs.bidirectional.block);
|
||||
vecs.push(&mut type_vecs.active.block);
|
||||
}
|
||||
vecs.into_par_iter()
|
||||
}
|
||||
@@ -221,11 +240,7 @@ impl AddrTypeToActivityCountVecs {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
pub(crate) fn compute_rest(&mut self, max_from: Height, exit: &Exit) -> Result<()> {
|
||||
for type_vecs in self.0.values_mut() {
|
||||
type_vecs.compute_rest(max_from, exit)?;
|
||||
}
|
||||
@@ -251,15 +266,17 @@ pub struct AddrActivityVecs<M: StorageMode = Rw> {
|
||||
impl AddrActivityVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
all: ActivityCountVecs::forced_import(db, name, version, indexes, cached_starts)?,
|
||||
all: ActivityCountVecs::forced_import(db, "", version, indexes, cached_starts)?,
|
||||
by_addr_type: AddrTypeToActivityCountVecs::forced_import(
|
||||
db, name, version, indexes, cached_starts,
|
||||
db,
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
@@ -284,11 +301,7 @@ impl AddrActivityVecs {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
pub(crate) fn compute_rest(&mut self, max_from: Height, exit: &Exit) -> Result<()> {
|
||||
self.all.compute_rest(max_from, exit)?;
|
||||
self.by_addr_type.compute_rest(max_from, exit)?;
|
||||
Ok(())
|
||||
|
||||
@@ -1,213 +0,0 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Indexes, StoredU64, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{
|
||||
AnyStoredVec, AnyVec, Database, EagerVec, Exit, PcoVec, ReadableVec, Rw, StorageMode,
|
||||
WritableVec,
|
||||
};
|
||||
|
||||
use crate::{indexes, internal::PerBlock};
|
||||
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct AddrCountVecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub PerBlock<StoredU64, M>,
|
||||
);
|
||||
|
||||
impl AddrCountVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self(PerBlock::forced_import(
|
||||
db, name, version, indexes,
|
||||
)?))
|
||||
}
|
||||
}
|
||||
|
||||
/// Address count per address type (runtime state).
|
||||
#[derive(Debug, Default, Deref, DerefMut)]
|
||||
pub struct AddrTypeToAddrCount(ByAddrType<u64>);
|
||||
|
||||
impl AddrTypeToAddrCount {
|
||||
#[inline]
|
||||
pub(crate) fn sum(&self) -> u64 {
|
||||
self.0.values().sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&AddrTypeToAddrCountVecs, Height)> for AddrTypeToAddrCount {
|
||||
#[inline]
|
||||
fn from((groups, starting_height): (&AddrTypeToAddrCountVecs, Height)) -> Self {
|
||||
if let Some(prev_height) = starting_height.decremented() {
|
||||
Self(ByAddrType {
|
||||
p2pk65: groups
|
||||
.p2pk65
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
p2pk33: groups
|
||||
.p2pk33
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
p2pkh: groups
|
||||
.p2pkh
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
p2sh: groups
|
||||
.p2sh
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
p2wpkh: groups
|
||||
.p2wpkh
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
p2wsh: groups
|
||||
.p2wsh
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
p2tr: groups
|
||||
.p2tr
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
p2a: groups
|
||||
.p2a
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap()
|
||||
.into(),
|
||||
})
|
||||
} else {
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Address count per address type, with height + derived indexes.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct AddrTypeToAddrCountVecs<M: StorageMode = Rw>(ByAddrType<AddrCountVecs<M>>);
|
||||
|
||||
impl From<ByAddrType<AddrCountVecs>> for AddrTypeToAddrCountVecs {
|
||||
#[inline]
|
||||
fn from(value: ByAddrType<AddrCountVecs>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddrTypeToAddrCountVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self::from(ByAddrType::<AddrCountVecs>::new_with_name(
|
||||
|type_name| {
|
||||
AddrCountVecs::forced_import(db, &format!("{type_name}_{name}"), version, indexes)
|
||||
},
|
||||
)?))
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.0.values().map(|v| v.height.len()).min().unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_height_mut(
|
||||
&mut self,
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
self.0
|
||||
.par_values_mut()
|
||||
.map(|v| &mut v.height as &mut dyn AnyStoredVec)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_height(&mut self, addr_counts: &AddrTypeToAddrCount) {
|
||||
for (vecs, &count) in self.0.values_mut().zip(addr_counts.values()) {
|
||||
vecs.height.push(count.into());
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reset_height(&mut self) -> Result<()> {
|
||||
for v in self.0.values_mut() {
|
||||
v.height.reset()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn by_height(&self) -> Vec<&EagerVec<PcoVec<Height, StoredU64>>> {
|
||||
self.0.values().map(|v| &v.height).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct AddrCountsVecs<M: StorageMode = Rw> {
|
||||
pub all: AddrCountVecs<M>,
|
||||
#[traversable(flatten)]
|
||||
pub by_addr_type: AddrTypeToAddrCountVecs<M>,
|
||||
}
|
||||
|
||||
impl AddrCountsVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
all: AddrCountVecs::forced_import(db, name, version, indexes)?,
|
||||
by_addr_type: AddrTypeToAddrCountVecs::forced_import(db, name, version, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.all.height.len().min(self.by_addr_type.min_stateful_len())
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_height_mut(
|
||||
&mut self,
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
rayon::iter::once(&mut self.all.height as &mut dyn AnyStoredVec)
|
||||
.chain(self.by_addr_type.par_iter_height_mut())
|
||||
}
|
||||
|
||||
pub(crate) fn reset_height(&mut self) -> Result<()> {
|
||||
self.all.height.reset()?;
|
||||
self.by_addr_type.reset_height()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_height(&mut self, total: u64, addr_counts: &AddrTypeToAddrCount) {
|
||||
self.all.height.push(total.into());
|
||||
self.by_addr_type.push_height(addr_counts);
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let sources = self.by_addr_type.by_height();
|
||||
self.all
|
||||
.height
|
||||
.compute_sum_of_others(starting_indexes.height, &sources, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{StoredU64, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Database, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{PerBlock, WithAddrTypes},
|
||||
};
|
||||
|
||||
use super::AddrTypeToAddrCount;
|
||||
|
||||
/// Per-block `StoredU64` counts with an aggregate `all` plus a per-address-type
|
||||
/// breakdown. Shared primitive backing addr-count, empty-addr-count, and the
|
||||
/// funded/total pairs used by exposed, reused, and respent.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct AddrCountsVecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub WithAddrTypes<PerBlock<StoredU64, M>>,
|
||||
);
|
||||
|
||||
impl AddrCountsVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self(WithAddrTypes::<PerBlock<StoredU64>>::forced_import(
|
||||
db, name, version, indexes,
|
||||
)?))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_counts(&mut self, counts: &AddrTypeToAddrCount) {
|
||||
self.push_height(counts.sum(), counts.values().copied());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Indexes, Version};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, Database, Exit, Rw, StorageMode};
|
||||
|
||||
use crate::indexes;
|
||||
|
||||
use super::{AddrCountsVecs, AddrTypeToAddrCount};
|
||||
|
||||
/// Paired funded + cumulative-total address counts, used by exposed, reused,
|
||||
/// and respent. On-disk naming: `"{name}_addr_count"` (funded) and
|
||||
/// `"total_{name}_addr_count"` (total).
|
||||
#[derive(Traversable)]
|
||||
pub struct AddrCountFundedTotalVecs<M: StorageMode = Rw> {
|
||||
pub funded: AddrCountsVecs<M>,
|
||||
pub total: AddrCountsVecs<M>,
|
||||
}
|
||||
|
||||
impl AddrCountFundedTotalVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
funded: AddrCountsVecs::forced_import(
|
||||
db,
|
||||
&format!("{name}_addr_count"),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
total: AddrCountsVecs::forced_import(
|
||||
db,
|
||||
&format!("total_{name}_addr_count"),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.funded
|
||||
.min_stateful_len()
|
||||
.min(self.total.min_stateful_len())
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_height_mut(
|
||||
&mut self,
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
self.funded
|
||||
.par_iter_height_mut()
|
||||
.chain(self.total.par_iter_height_mut())
|
||||
}
|
||||
|
||||
pub(crate) fn reset_height(&mut self) -> Result<()> {
|
||||
self.funded.reset_height()?;
|
||||
self.total.reset_height()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_counts(
|
||||
&mut self,
|
||||
funded: &AddrTypeToAddrCount,
|
||||
total: &AddrTypeToAddrCount,
|
||||
) {
|
||||
self.funded.push_counts(funded);
|
||||
self.total.push_counts(total);
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(&mut self, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.funded.compute_rest(starting_indexes, exit)?;
|
||||
self.total.compute_rest(starting_indexes, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
mod all_vecs;
|
||||
mod funded_total_vecs;
|
||||
mod state;
|
||||
|
||||
pub use all_vecs::AddrCountsVecs;
|
||||
pub use funded_total_vecs::AddrCountFundedTotalVecs;
|
||||
pub use state::AddrTypeToAddrCount;
|
||||
@@ -0,0 +1,38 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_types::Height;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::ReadableVec;
|
||||
|
||||
use super::AddrCountsVecs;
|
||||
|
||||
/// Per-addr-type address-count running total. Shared runtime state across
|
||||
/// funded / empty / exposed / reused / respent counters; paired with
|
||||
/// [`AddrCountsVecs`] on disk.
|
||||
#[derive(Debug, Default, Deref, DerefMut)]
|
||||
pub struct AddrTypeToAddrCount(ByAddrType<u64>);
|
||||
|
||||
impl AddrTypeToAddrCount {
|
||||
#[inline]
|
||||
pub(crate) fn sum(&self) -> u64 {
|
||||
self.0.values().sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ByAddrType<u64>> for AddrTypeToAddrCount {
|
||||
#[inline]
|
||||
fn from(value: ByAddrType<u64>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&AddrCountsVecs, Height)> for AddrTypeToAddrCount {
|
||||
#[inline]
|
||||
fn from((vecs, starting_height): (&AddrCountsVecs, Height)) -> Self {
|
||||
let Some(prev_height) = starting_height.decremented() else {
|
||||
return Self::default();
|
||||
};
|
||||
vecs.by_addr_type
|
||||
.map_with_name(|_, v| v.height.collect_one(prev_height).unwrap().into())
|
||||
.into()
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
EmptyAddrData, EmptyAddrIndex, FundedAddrData, FundedAddrIndex, Height,
|
||||
};
|
||||
use brk_types::{EmptyAddrData, EmptyAddrIndex, FundedAddrData, FundedAddrIndex, Height};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, BytesVec, Rw, Stamp, StorageMode, WritableVec};
|
||||
|
||||
|
||||
@@ -1,28 +1,24 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPointsSigned32, StoredI64, StoredU64, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{CachedWindowStarts, LazyRollingDeltasFromHeight},
|
||||
internal::{LazyRollingDeltasFromHeight, WindowStartVec, Windows, WithAddrTypes},
|
||||
};
|
||||
|
||||
use super::AddrCountsVecs;
|
||||
|
||||
type AddrDelta = LazyRollingDeltasFromHeight<StoredU64, StoredI64, BasisPointsSigned32>;
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct DeltaVecs {
|
||||
pub all: AddrDelta,
|
||||
#[traversable(flatten)]
|
||||
pub by_addr_type: ByAddrType<AddrDelta>,
|
||||
}
|
||||
#[derive(Clone, Deref, DerefMut, Traversable)]
|
||||
pub struct DeltaVecs(#[traversable(flatten)] pub WithAddrTypes<AddrDelta>);
|
||||
|
||||
impl DeltaVecs {
|
||||
pub(crate) fn new(
|
||||
version: Version,
|
||||
addr_count: &AddrCountsVecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Self {
|
||||
let version = version + Version::TWO;
|
||||
@@ -30,7 +26,7 @@ impl DeltaVecs {
|
||||
let all = LazyRollingDeltasFromHeight::new(
|
||||
"addr_count",
|
||||
version,
|
||||
&addr_count.all.0.height,
|
||||
&addr_count.all.height,
|
||||
cached_starts,
|
||||
indexes,
|
||||
);
|
||||
@@ -39,15 +35,12 @@ impl DeltaVecs {
|
||||
LazyRollingDeltasFromHeight::new(
|
||||
&format!("{name}_addr_count"),
|
||||
version,
|
||||
&addr.0.height,
|
||||
&addr.height,
|
||||
cached_starts,
|
||||
indexes,
|
||||
)
|
||||
});
|
||||
|
||||
Self {
|
||||
all,
|
||||
by_addr_type,
|
||||
}
|
||||
Self(WithAddrTypes { all, by_addr_type })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
//! Exposed address tracking (quantum / pubkey-exposure sense).
|
||||
//!
|
||||
//! An address is "exposed" once its public key is in the blockchain. Once
|
||||
//! exposed, any funds at that address are at cryptographic risk (e.g. from
|
||||
//! a quantum attacker capable of recovering the private key from the pubkey).
|
||||
//!
|
||||
//! When the pubkey gets exposed depends on the address type:
|
||||
//!
|
||||
//! - **P2PK33, P2PK65, P2TR**: the pubkey (or P2TR's tweaked output key) is
|
||||
//! directly in the locking script of the funding output. These addresses are
|
||||
//! exposed the moment they receive any funds.
|
||||
//! - **P2PKH, P2SH, P2WPKH, P2WSH**: the locking script contains a hash of
|
||||
//! the pubkey/script. The pubkey is only revealed when spending. Note that
|
||||
//! even the spending tx itself exposes the pubkey while the address still
|
||||
//! holds funds, during the mempool window between broadcast and confirmation,
|
||||
//! the pubkey is visible while the UTXO being spent is still unspent on-chain.
|
||||
//! So every spent address of these types has had at least one moment with
|
||||
//! funds at quantum risk.
|
||||
//! - **P2A**: anyone-can-spend, no pubkey at all. Excluded from both counters.
|
||||
//!
|
||||
//! Formally, with `is_funding_exposed` = `output_type.pubkey_exposed_at_funding()`:
|
||||
//! - `funded` (count): `(utxo_count > 0) AND (is_funding_exposed OR spent_txo_count >= 1)`
|
||||
//! - `total` (count): `(is_funding_exposed AND ever received) OR spent_txo_count >= 1`
|
||||
//! - `supply` (sats): sum of balances of addresses currently in the funded set
|
||||
//!
|
||||
//! For P2PK/P2TR types this means `total ≡ total_addr_count` and
|
||||
//! `funded ≡ funded_addr_count` (every address of those types is exposed by
|
||||
//! virtue of existing). For P2PKH/P2SH/P2WPKH/P2WSH it's the strict subset of
|
||||
//! addresses that have been spent from. The aggregate `all` exposed counter
|
||||
//! sums these, giving "Bitcoin addresses currently with funds at quantum risk".
|
||||
//!
|
||||
//! All metrics are tracked as running counters and require no extra fields
|
||||
//! on the address data. They're maintained via delta detection in
|
||||
//! `process_received` and `process_sent`.
|
||||
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Indexes, Sats, Version};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, Database, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use super::{
|
||||
count::AddrCountFundedTotalVecs,
|
||||
supply::{AddrSupplyShareVecs, AddrSupplyVecs},
|
||||
};
|
||||
use crate::{indexes, prices};
|
||||
|
||||
mod state;
|
||||
|
||||
pub use state::ExposedAddrState;
|
||||
|
||||
/// Top-level container for all exposed address tracking: counts (funded +
|
||||
/// total), the funded supply, and share of supply.
|
||||
#[derive(Traversable)]
|
||||
pub struct ExposedAddrVecs<M: StorageMode = Rw> {
|
||||
pub count: AddrCountFundedTotalVecs<M>,
|
||||
pub supply: AddrSupplyVecs<M>,
|
||||
#[traversable(wrap = "supply", rename = "share")]
|
||||
pub supply_share: AddrSupplyShareVecs<M>,
|
||||
}
|
||||
|
||||
impl ExposedAddrVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
count: AddrCountFundedTotalVecs::forced_import(db, "exposed", version, indexes)?,
|
||||
supply: AddrSupplyVecs::forced_import(db, "exposed", version, indexes)?,
|
||||
supply_share: AddrSupplyShareVecs::forced_import(db, "exposed", version, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.count
|
||||
.min_stateful_len()
|
||||
.min(self.supply.min_stateful_len())
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_height_mut(
|
||||
&mut self,
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
self.count
|
||||
.par_iter_height_mut()
|
||||
.chain(self.supply.par_iter_height_mut())
|
||||
}
|
||||
|
||||
pub(crate) fn reset_height(&mut self) -> Result<()> {
|
||||
self.count.reset_height()?;
|
||||
self.supply.reset_height()?;
|
||||
self.supply_share.reset_height()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_height(&mut self, state: &ExposedAddrState) {
|
||||
self.count.push_counts(&state.funded, &state.total);
|
||||
self.supply.push_supply(&state.supply);
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
prices: &prices::Vecs,
|
||||
all_supply_sats: &impl ReadableVec<Height, Sats>,
|
||||
type_supply_sats: &ByAddrType<&impl ReadableVec<Height, Sats>>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.count.compute_rest(starting_indexes, exit)?;
|
||||
self.supply
|
||||
.compute_rest(starting_indexes.height, prices, exit)?;
|
||||
self.supply_share.compute_rest(
|
||||
starting_indexes.height,
|
||||
&self.supply,
|
||||
all_supply_sats,
|
||||
type_supply_sats,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
use brk_types::{FundedAddrData, Height, OutputType};
|
||||
|
||||
use crate::distribution::{
|
||||
addr::{AddrReceivePreState, AddrSendPreState, AddrTypeToAddrCount, AddrTypeToSupply},
|
||||
block::TrackingStatus,
|
||||
};
|
||||
|
||||
use super::ExposedAddrVecs;
|
||||
|
||||
/// Runtime running totals for exposed-addr tracking. Mirrors the persistent
|
||||
/// fields of [`ExposedAddrVecs`]: funded count, total count, funded supply.
|
||||
/// Recovered from disk at the start of a block-loop run.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ExposedAddrState {
|
||||
pub funded: AddrTypeToAddrCount,
|
||||
pub total: AddrTypeToAddrCount,
|
||||
pub supply: AddrTypeToSupply,
|
||||
}
|
||||
|
||||
impl ExposedAddrState {
|
||||
/// Apply exposed-addr updates for a received output, AFTER the receive
|
||||
/// has mutated `addr_data`. `pre` is the snapshot taken before the mutation.
|
||||
#[inline]
|
||||
pub(crate) fn on_receive(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrReceivePreState,
|
||||
status: TrackingStatus,
|
||||
) {
|
||||
// Pubkey-exposure state is unchanged by a receive, so `pre.was_pubkey_exposed`
|
||||
// equals the post-receive value.
|
||||
if !pre.was_funded && pre.was_pubkey_exposed {
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
// Total for pk-exposed-at-funding types fires here on first receive.
|
||||
// Other types fire on first spend in [`Self::on_send`].
|
||||
if output_type.pubkey_exposed_at_funding() && matches!(status, TrackingStatus::New) {
|
||||
*self.total.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
let after = addr_data.exposed_supply_contribution(output_type);
|
||||
self.supply
|
||||
.apply_delta(output_type, pre.exposed_contribution, after);
|
||||
}
|
||||
|
||||
/// Apply exposed-addr updates for a spent UTXO, AFTER the send has mutated
|
||||
/// `addr_data`. `pre` is the snapshot taken before the mutation.
|
||||
#[inline]
|
||||
pub(crate) fn on_send(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrSendPreState,
|
||||
will_be_empty: bool,
|
||||
) {
|
||||
let after = addr_data.exposed_supply_contribution(output_type);
|
||||
self.supply
|
||||
.apply_delta(output_type, pre.exposed_contribution, after);
|
||||
// First-ever pubkey exposure. Non-pk-exposed types fire on first spend.
|
||||
// Pk-exposed types never fire here (was already exposed at first receive).
|
||||
if !pre.was_pubkey_exposed {
|
||||
*self.total.get_mut_unwrap(output_type) += 1;
|
||||
if !will_be_empty {
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
}
|
||||
// Leaving the funded exposed set: was in it iff pubkey was exposed pre-spend.
|
||||
if will_be_empty && pre.was_pubkey_exposed {
|
||||
*self.funded.get_mut_unwrap(output_type) -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&ExposedAddrVecs, Height)> for ExposedAddrState {
|
||||
#[inline]
|
||||
fn from((vecs, starting_height): (&ExposedAddrVecs, Height)) -> Self {
|
||||
Self {
|
||||
funded: AddrTypeToAddrCount::from((&vecs.count.funded, starting_height)),
|
||||
total: AddrTypeToAddrCount::from((&vecs.count.total, starting_height)),
|
||||
supply: AddrTypeToSupply::from((&vecs.supply, starting_height)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,8 @@ use brk_error::{Error, Result};
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
AnyAddrIndex, Height, OutputType, P2AAddrIndex, P2PK33AddrIndex, P2PK65AddrIndex,
|
||||
P2PKHAddrIndex, P2SHAddrIndex, P2TRAddrIndex, P2WPKHAddrIndex, P2WSHAddrIndex,
|
||||
TypeIndex, Version,
|
||||
P2PKHAddrIndex, P2SHAddrIndex, P2TRAddrIndex, P2WPKHAddrIndex, P2WSHAddrIndex, TypeIndex,
|
||||
Version,
|
||||
};
|
||||
use rayon::prelude::*;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
mod activity;
|
||||
mod addr_count;
|
||||
mod count;
|
||||
mod data;
|
||||
mod delta;
|
||||
mod exposed;
|
||||
mod indexes;
|
||||
mod new_addr_count;
|
||||
mod reused;
|
||||
mod state;
|
||||
mod supply;
|
||||
mod total_addr_count;
|
||||
mod type_map;
|
||||
|
||||
pub use activity::{AddrActivityVecs, AddrTypeToActivityCounts};
|
||||
pub use addr_count::{AddrCountsVecs, AddrTypeToAddrCount};
|
||||
pub use count::{AddrCountsVecs, AddrTypeToAddrCount};
|
||||
pub use data::AddrsDataVecs;
|
||||
pub use delta::DeltaVecs;
|
||||
pub use exposed::{ExposedAddrState, ExposedAddrVecs};
|
||||
pub use indexes::AnyAddrIndexesVecs;
|
||||
pub use new_addr_count::NewAddrCountVecs;
|
||||
pub use reused::{ReusedAddrState, ReusedAddrVecs};
|
||||
pub use state::{AddrMetricsState, AddrReceivePreState, AddrSendPreState};
|
||||
pub use supply::AddrTypeToSupply;
|
||||
pub use total_addr_count::TotalAddrCountVecs;
|
||||
pub use type_map::{AddrTypeToTypeIndexMap, AddrTypeToVec, HeightToAddrTypeToVec};
|
||||
|
||||
@@ -1,53 +1,38 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, StoredU64, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Database, Exit, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{CachedWindowStarts, PerBlockCumulativeRolling},
|
||||
internal::{PerBlockCumulativeRolling, WindowStartVec, Windows, WithAddrTypes},
|
||||
};
|
||||
|
||||
use super::TotalAddrCountVecs;
|
||||
|
||||
/// New address count per block (global + per-type)
|
||||
#[derive(Traversable)]
|
||||
pub struct NewAddrCountVecs<M: StorageMode = Rw> {
|
||||
pub all: PerBlockCumulativeRolling<StoredU64, StoredU64, M>,
|
||||
#[traversable(flatten)]
|
||||
pub by_addr_type: ByAddrType<PerBlockCumulativeRolling<StoredU64, StoredU64, M>>,
|
||||
}
|
||||
/// New address count per block (global + per-type).
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct NewAddrCountVecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub WithAddrTypes<PerBlockCumulativeRolling<StoredU64, StoredU64, M>>,
|
||||
);
|
||||
|
||||
impl NewAddrCountVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &CachedWindowStarts,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
let all = PerBlockCumulativeRolling::forced_import(
|
||||
Ok(Self(WithAddrTypes::<
|
||||
PerBlockCumulativeRolling<StoredU64, StoredU64>,
|
||||
>::forced_import(
|
||||
db,
|
||||
"new_addr_count",
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?;
|
||||
|
||||
let by_addr_type = ByAddrType::new_with_name(|name| {
|
||||
PerBlockCumulativeRolling::forced_import(
|
||||
db,
|
||||
&format!("{name}_new_addr_count"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(Self {
|
||||
all,
|
||||
by_addr_type,
|
||||
})
|
||||
)?))
|
||||
}
|
||||
|
||||
pub(crate) fn compute(
|
||||
@@ -56,11 +41,12 @@ impl NewAddrCountVecs {
|
||||
total_addr_count: &TotalAddrCountVecs,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.all.compute(max_from, exit, |height_vec| {
|
||||
self.0.all.compute(max_from, exit, |height_vec| {
|
||||
Ok(height_vec.compute_change(max_from, &total_addr_count.all.height, 1, exit)?)
|
||||
})?;
|
||||
|
||||
for ((_, new), (_, total)) in self
|
||||
.0
|
||||
.by_addr_type
|
||||
.iter_mut()
|
||||
.zip(total_addr_count.by_addr_type.iter())
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
//! Per-block address-reuse event tracking. Holds both the output-side
|
||||
//! ("an output landed on a previously-used address") and input-side
|
||||
//! ("an input spent from an address in the reused set") event counters.
|
||||
//! Shared between reused (receive-based) and respent (spend-based) flavors.
|
||||
//! See [`vecs::AddrEventsVecs`] for the full description of each metric.
|
||||
|
||||
mod state;
|
||||
mod vecs;
|
||||
|
||||
pub use state::AddrTypeToAddrEventCount;
|
||||
pub use vecs::AddrEventsVecs;
|
||||
@@ -0,0 +1,27 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
|
||||
/// Per-block running counter of address-reuse events, per address type. Shared
|
||||
/// across reused (receive-based) and respent (spend-based) flavors, and
|
||||
/// across output-side ("output landed on a previously-used address") and
|
||||
/// input-side ("input spent from an address in the set") event kinds.
|
||||
///
|
||||
/// Reset at the start of each block; no disk recovery needed since per-block
|
||||
/// flow is reconstructed deterministically from `process_received` /
|
||||
/// `process_sent`.
|
||||
#[derive(Debug, Default, Deref, DerefMut)]
|
||||
pub struct AddrTypeToAddrEventCount(ByAddrType<u64>);
|
||||
|
||||
impl AddrTypeToAddrEventCount {
|
||||
#[inline]
|
||||
pub(crate) fn sum(&self) -> u64 {
|
||||
self.0.values().sum()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn reset(&mut self) {
|
||||
for v in self.0.values_mut() {
|
||||
*v = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, Indexes, OutputType, StoredF32, StoredU32, StoredU64, Version};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, AnyVec, Database, Exit, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
indexes, inputs,
|
||||
internal::{
|
||||
PerBlockCumulativeRolling, PerBlockRollingAverage, PercentCumulativeRolling,
|
||||
WindowStartVec, Windows, WithAddrTypes,
|
||||
},
|
||||
outputs,
|
||||
};
|
||||
|
||||
use super::state::AddrTypeToAddrEventCount;
|
||||
|
||||
/// Per-block reused-address event metrics. Holds three families of
|
||||
/// signals: output-level (use), input-level (spend), and address-level
|
||||
/// (active in block).
|
||||
///
|
||||
/// `output_to_reused_addr_count`: every output landing on an address that had
|
||||
/// already received at least one prior output anywhere in its lifetime,
|
||||
/// i.e. an output-level reuse event. Outputs are not deduplicated per
|
||||
/// address within a block: an address receiving N outputs in one block
|
||||
/// that had `before` lifetime outputs contributes
|
||||
/// `max(0, N - max(0, 1 - before))` events. Only the very first output
|
||||
/// an address ever sees is excluded. Every subsequent output counts,
|
||||
/// matching the standard "% of outputs to previously-used addresses"
|
||||
/// reuse ratio reported by external sources. `output_to_reused_addr_share`
|
||||
/// uses `outputs::ByTypeVecs::output_count` (all 12 output types) as
|
||||
/// denominator. `spendable_output_to_reused_addr_share` uses the
|
||||
/// op_return-excluded 11-type aggregate (`spendable_output_count`).
|
||||
///
|
||||
/// `input_from_reused_addr_count`: every input spending from an address
|
||||
/// whose lifetime `funded_txo_count > 1` at the time of the spend (i.e.
|
||||
/// the address is in the same reused set tracked by
|
||||
/// `reused_addr_count`). Every input is checked independently. If a
|
||||
/// single address has multiple inputs in one block each one counts.
|
||||
/// This is a *stable-predicate* signal about the sending address, not
|
||||
/// an output-level repeat event: the first spend from a reused address
|
||||
/// counts just as much as the tenth. Denominator
|
||||
/// (`input_from_reused_addr_share`): `inputs::ByTypeVecs::input_count` (11
|
||||
/// spendable types, where `p2ms`, `unknown`, `empty` count as true
|
||||
/// negatives).
|
||||
///
|
||||
/// `active_reused_addr_count` / `active_reused_addr_share`: block-level
|
||||
/// *address* signals (single aggregate, not per-type).
|
||||
/// `active_reused_addr_count` is the count of distinct addresses
|
||||
/// involved in this block (sent ∪ received) that satisfy `is_reused()`
|
||||
/// after the block's events, populated inline in `process_received`
|
||||
/// (each receiver, post-receive) and in `process_sent` (each
|
||||
/// first-encounter sender, deduped against `received_addrs` so
|
||||
/// addresses that did both aren't double-counted).
|
||||
/// `active_reused_addr_share` is the per-block ratio
|
||||
/// `reused / active * 100` as a percentage in `[0, 100]` (or `0.0` for
|
||||
/// empty blocks). The denominator (distinct active addrs per block)
|
||||
/// lives on `ActivityCountVecs::active` (`addrs.activity.all.active`),
|
||||
/// derived from `sending + receiving - bidirectional`. Both fields
|
||||
/// use `PerBlockRollingAverage` so their lazy 24h/1w/1m/1y series are
|
||||
/// rolling *averages* of the per-block values. Sums and cumulatives of
|
||||
/// distinct-address counts would be misleading because the same
|
||||
/// address can appear in multiple blocks.
|
||||
#[derive(Traversable)]
|
||||
pub struct AddrEventsVecs<M: StorageMode = Rw> {
|
||||
pub output_to_reused_addr_count:
|
||||
WithAddrTypes<PerBlockCumulativeRolling<StoredU64, StoredU64, M>>,
|
||||
pub output_to_reused_addr_share: WithAddrTypes<PercentCumulativeRolling<BasisPoints16, M>>,
|
||||
pub spendable_output_to_reused_addr_share: PercentCumulativeRolling<BasisPoints16, M>,
|
||||
pub input_from_reused_addr_count:
|
||||
WithAddrTypes<PerBlockCumulativeRolling<StoredU64, StoredU64, M>>,
|
||||
pub input_from_reused_addr_share: WithAddrTypes<PercentCumulativeRolling<BasisPoints16, M>>,
|
||||
pub active_reused_addr_count: PerBlockRollingAverage<StoredU32, StoredU64, M>,
|
||||
pub active_reused_addr_share: PerBlockRollingAverage<StoredF32, StoredF32, M>,
|
||||
}
|
||||
|
||||
impl AddrEventsVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
let import_count = |name: &str| {
|
||||
WithAddrTypes::<PerBlockCumulativeRolling<StoredU64, StoredU64>>::forced_import(
|
||||
db,
|
||||
name,
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)
|
||||
};
|
||||
let import_percent =
|
||||
|name: &str| -> Result<WithAddrTypes<PercentCumulativeRolling<BasisPoints16>>> {
|
||||
Ok(WithAddrTypes {
|
||||
all: PercentCumulativeRolling::forced_import(db, name, version, indexes)?,
|
||||
by_addr_type: ByAddrType::new_with_name(|type_name| {
|
||||
PercentCumulativeRolling::forced_import(
|
||||
db,
|
||||
&format!("{type_name}_{name}"),
|
||||
version,
|
||||
indexes,
|
||||
)
|
||||
})?,
|
||||
})
|
||||
};
|
||||
|
||||
let output_to_reused_addr_count = import_count(&format!("output_to_{name}_addr_count"))?;
|
||||
let output_to_reused_addr_share = import_percent(&format!("output_to_{name}_addr_share"))?;
|
||||
let spendable_output_to_reused_addr_share = PercentCumulativeRolling::forced_import(
|
||||
db,
|
||||
&format!("spendable_output_to_{name}_addr_share"),
|
||||
version,
|
||||
indexes,
|
||||
)?;
|
||||
let input_from_reused_addr_count = import_count(&format!("input_from_{name}_addr_count"))?;
|
||||
let input_from_reused_addr_share =
|
||||
import_percent(&format!("input_from_{name}_addr_share"))?;
|
||||
|
||||
let active_reused_addr_count = PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
&format!("active_{name}_addr_count"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?;
|
||||
let active_reused_addr_share = PerBlockRollingAverage::forced_import(
|
||||
db,
|
||||
&format!("active_{name}_addr_share"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
output_to_reused_addr_count,
|
||||
output_to_reused_addr_share,
|
||||
spendable_output_to_reused_addr_share,
|
||||
input_from_reused_addr_count,
|
||||
input_from_reused_addr_share,
|
||||
active_reused_addr_count,
|
||||
active_reused_addr_share,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.output_to_reused_addr_count
|
||||
.min_stateful_len()
|
||||
.min(self.input_from_reused_addr_count.min_stateful_len())
|
||||
.min(self.active_reused_addr_count.block.len())
|
||||
.min(self.active_reused_addr_share.block.len())
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_height_mut(
|
||||
&mut self,
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
self.output_to_reused_addr_count
|
||||
.par_iter_height_mut()
|
||||
.chain(self.input_from_reused_addr_count.par_iter_height_mut())
|
||||
.chain([
|
||||
&mut self.active_reused_addr_count.block as &mut dyn AnyStoredVec,
|
||||
&mut self.active_reused_addr_share.block as &mut dyn AnyStoredVec,
|
||||
])
|
||||
}
|
||||
|
||||
pub(crate) fn reset_height(&mut self) -> Result<()> {
|
||||
self.output_to_reused_addr_count.reset_height()?;
|
||||
self.input_from_reused_addr_count.reset_height()?;
|
||||
self.active_reused_addr_count.block.reset()?;
|
||||
self.active_reused_addr_share.block.reset()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_height(
|
||||
&mut self,
|
||||
uses: &AddrTypeToAddrEventCount,
|
||||
spends: &AddrTypeToAddrEventCount,
|
||||
active_addr_count: u32,
|
||||
active_reused_addr_count: u32,
|
||||
) {
|
||||
self.output_to_reused_addr_count
|
||||
.push_height(uses.sum(), uses.values().copied());
|
||||
self.input_from_reused_addr_count
|
||||
.push_height(spends.sum(), spends.values().copied());
|
||||
self.active_reused_addr_count
|
||||
.block
|
||||
.push(StoredU32::from(active_reused_addr_count));
|
||||
// Stored as a percentage in [0, 100] to match the rest of the
|
||||
// codebase (Unit.percentage on the website expects 0..100). The
|
||||
// `active_addr_count` denominator lives on `ActivityCountVecs`
|
||||
// (`addrs.activity.all.active`), passed in here so we can
|
||||
// compute the per-block ratio inline.
|
||||
let share = if active_addr_count > 0 {
|
||||
100.0 * (active_reused_addr_count as f32 / active_addr_count as f32)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
self.active_reused_addr_share
|
||||
.block
|
||||
.push(StoredF32::from(share));
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
outputs_by_type: &outputs::ByTypeVecs,
|
||||
inputs_by_type: &inputs::ByTypeVecs,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.output_to_reused_addr_count
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
self.input_from_reused_addr_count
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
self.active_reused_addr_count
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
self.active_reused_addr_share
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
|
||||
self.output_to_reused_addr_share.all.compute_count_ratio(
|
||||
&self.output_to_reused_addr_count.all,
|
||||
&outputs_by_type.output_count.all,
|
||||
starting_indexes.height,
|
||||
exit,
|
||||
)?;
|
||||
self.spendable_output_to_reused_addr_share
|
||||
.compute_count_ratio(
|
||||
&self.output_to_reused_addr_count.all,
|
||||
&outputs_by_type.spendable_output_count,
|
||||
starting_indexes.height,
|
||||
exit,
|
||||
)?;
|
||||
self.input_from_reused_addr_share.all.compute_count_ratio(
|
||||
&self.input_from_reused_addr_count.all,
|
||||
&inputs_by_type.input_count.all,
|
||||
starting_indexes.height,
|
||||
exit,
|
||||
)?;
|
||||
for otype in OutputType::ADDR_TYPES {
|
||||
self.output_to_reused_addr_share
|
||||
.by_addr_type
|
||||
.get_mut_unwrap(otype)
|
||||
.compute_count_ratio(
|
||||
self.output_to_reused_addr_count
|
||||
.by_addr_type
|
||||
.get_unwrap(otype),
|
||||
outputs_by_type.output_count.by_type.get(otype),
|
||||
starting_indexes.height,
|
||||
exit,
|
||||
)?;
|
||||
self.input_from_reused_addr_share
|
||||
.by_addr_type
|
||||
.get_mut_unwrap(otype)
|
||||
.compute_count_ratio(
|
||||
self.input_from_reused_addr_count
|
||||
.by_addr_type
|
||||
.get_unwrap(otype),
|
||||
inputs_by_type.input_count.by_type.get(otype),
|
||||
starting_indexes.height,
|
||||
exit,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
//! Reused address tracking.
|
||||
//!
|
||||
//! An address is "reused" if its lifetime `funded_txo_count > 1`, i.e.
|
||||
//! it has received more than one output across its lifetime. This is
|
||||
//! the simplest output-multiplicity proxy for address linkability.
|
||||
//!
|
||||
//! Two facets are tracked here:
|
||||
//! - [`count`]: how many distinct addresses are currently reused
|
||||
//! (funded) and how many have *ever* been reused (total). Per address
|
||||
//! type plus an aggregated `all`.
|
||||
//! - [`events`]: per-block address-reuse event counts on both sides.
|
||||
//! Output-side (`output_to_reused_addr_count`, outputs landing on
|
||||
//! addresses that had already received ≥ 1 prior output) and
|
||||
//! input-side (`input_from_reused_addr_count`, inputs spending from
|
||||
//! addresses with lifetime `funded_txo_count > 1`). Each count is
|
||||
//! paired with a percent over the matching block-level output/input
|
||||
//! total.
|
||||
|
||||
mod events;
|
||||
|
||||
pub use events::{AddrEventsVecs, AddrTypeToAddrEventCount};
|
||||
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Indexes, Sats, Version};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, Database, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use super::{
|
||||
count::AddrCountFundedTotalVecs,
|
||||
supply::{AddrSupplyShareVecs, AddrSupplyVecs},
|
||||
};
|
||||
use crate::{
|
||||
indexes, inputs,
|
||||
internal::{WindowStartVec, Windows},
|
||||
outputs, prices,
|
||||
};
|
||||
|
||||
mod state;
|
||||
|
||||
pub use state::ReusedAddrState;
|
||||
|
||||
/// Top-level container for all reused address tracking: counts (funded +
|
||||
/// total), per-block reuse events (output-side + input-side), and funded
|
||||
/// supply + share.
|
||||
#[derive(Traversable)]
|
||||
pub struct ReusedAddrVecs<M: StorageMode = Rw> {
|
||||
pub count: AddrCountFundedTotalVecs<M>,
|
||||
pub events: AddrEventsVecs<M>,
|
||||
pub supply: AddrSupplyVecs<M>,
|
||||
#[traversable(wrap = "supply", rename = "share")]
|
||||
pub supply_share: AddrSupplyShareVecs<M>,
|
||||
}
|
||||
|
||||
impl ReusedAddrVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
count: AddrCountFundedTotalVecs::forced_import(db, name, version, indexes)?,
|
||||
events: AddrEventsVecs::forced_import(db, name, version, indexes, cached_starts)?,
|
||||
supply: AddrSupplyVecs::forced_import(db, name, version, indexes)?,
|
||||
supply_share: AddrSupplyShareVecs::forced_import(db, name, version, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_len(&self) -> usize {
|
||||
self.count
|
||||
.min_stateful_len()
|
||||
.min(self.events.min_stateful_len())
|
||||
.min(self.supply.min_stateful_len())
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_height_mut(
|
||||
&mut self,
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
self.count
|
||||
.par_iter_height_mut()
|
||||
.chain(self.events.par_iter_height_mut())
|
||||
.chain(self.supply.par_iter_height_mut())
|
||||
}
|
||||
|
||||
pub(crate) fn reset_height(&mut self) -> Result<()> {
|
||||
self.count.reset_height()?;
|
||||
self.events.reset_height()?;
|
||||
self.supply.reset_height()?;
|
||||
self.supply_share.reset_height()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_height(&mut self, state: &ReusedAddrState, active_addr_count: u32) {
|
||||
self.count.push_counts(&state.funded, &state.total);
|
||||
self.supply.push_supply(&state.supply);
|
||||
self.events.push_height(
|
||||
&state.output_events,
|
||||
&state.input_events,
|
||||
active_addr_count,
|
||||
u32::try_from(state.active.sum()).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
outputs_by_type: &outputs::ByTypeVecs,
|
||||
inputs_by_type: &inputs::ByTypeVecs,
|
||||
prices: &prices::Vecs,
|
||||
all_supply_sats: &impl ReadableVec<Height, Sats>,
|
||||
type_supply_sats: &ByAddrType<&impl ReadableVec<Height, Sats>>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.count.compute_rest(starting_indexes, exit)?;
|
||||
self.events
|
||||
.compute_rest(starting_indexes, outputs_by_type, inputs_by_type, exit)?;
|
||||
self.supply
|
||||
.compute_rest(starting_indexes.height, prices, exit)?;
|
||||
self.supply_share.compute_rest(
|
||||
starting_indexes.height,
|
||||
&self.supply,
|
||||
all_supply_sats,
|
||||
type_supply_sats,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
use brk_types::{FundedAddrData, Height, OutputType};
|
||||
|
||||
use crate::distribution::addr::{
|
||||
AddrReceivePreState, AddrSendPreState, AddrTypeToAddrCount, AddrTypeToSupply,
|
||||
};
|
||||
|
||||
use super::{AddrTypeToAddrEventCount, ReusedAddrVecs};
|
||||
|
||||
/// Runtime state for receive-based (reused) or spend-based (respent)
|
||||
/// address tracking. Mirrors the persistent fields of [`ReusedAddrVecs`]
|
||||
/// (funded + total counts, funded supply) plus per-block event counters
|
||||
/// that reset every block.
|
||||
///
|
||||
/// `output_events`, `input_events`, and `active` are cleared via
|
||||
/// [`Self::reset_per_block`] at the start of each block. The three running
|
||||
/// totals (`funded`, `total`, `supply`) are recovered from disk at the start
|
||||
/// of a run via [`From<(&ReusedAddrVecs, Height)>`].
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ReusedAddrState {
|
||||
pub funded: AddrTypeToAddrCount,
|
||||
pub total: AddrTypeToAddrCount,
|
||||
pub supply: AddrTypeToSupply,
|
||||
pub output_events: AddrTypeToAddrEventCount,
|
||||
pub input_events: AddrTypeToAddrEventCount,
|
||||
pub active: AddrTypeToAddrEventCount,
|
||||
}
|
||||
|
||||
impl ReusedAddrState {
|
||||
#[inline]
|
||||
pub(crate) fn reset_per_block(&mut self) {
|
||||
self.output_events.reset();
|
||||
self.input_events.reset();
|
||||
self.active.reset();
|
||||
}
|
||||
|
||||
/// Apply reused-flavor (receive-based: `funded_txo_count > 1`) updates
|
||||
/// for a received output, AFTER the receive has mutated `addr_data`.
|
||||
#[inline]
|
||||
pub(crate) fn on_receive_as_reused(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrReceivePreState,
|
||||
output_count: u32,
|
||||
) {
|
||||
let is_now_reused = addr_data.is_reused();
|
||||
|
||||
// Threshold crossing: the 2nd lifetime receive lands here. The address
|
||||
// is always funded post-receive.
|
||||
if is_now_reused && !pre.was_reused {
|
||||
*self.total.get_mut_unwrap(output_type) += 1;
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
} else if pre.was_reused && !pre.was_funded {
|
||||
// Reactivation: already-reused address was empty, now funded.
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
|
||||
// output-to-reused events: outputs landing on addresses that had
|
||||
// already received >= 1 prior output, i.e. every output except the
|
||||
// very first one the address ever sees. With `before =
|
||||
// prev_funded_txo_count` and `N = output_count`: events = N - max(0, 1 - before).
|
||||
let skip_first = 1u32.saturating_sub(pre.prev_funded_txo_count.min(1));
|
||||
let reused_events = output_count.saturating_sub(skip_first);
|
||||
if reused_events > 0 {
|
||||
*self.output_events.get_mut_unwrap(output_type) += u64::from(reused_events);
|
||||
}
|
||||
|
||||
if is_now_reused {
|
||||
*self.active.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
|
||||
let after = addr_data.reused_supply_contribution();
|
||||
self.supply
|
||||
.apply_delta(output_type, pre.reused_contribution, after);
|
||||
}
|
||||
|
||||
/// Apply respent-flavor (spend-based: `spent_txo_count > 1`) updates for a
|
||||
/// received output, AFTER the receive has mutated `addr_data`. Receives
|
||||
/// don't cross the respent threshold. The only transition is an
|
||||
/// already-respent empty address reactivating into the funded set.
|
||||
#[inline]
|
||||
pub(crate) fn on_receive_as_respent(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrReceivePreState,
|
||||
output_count: u32,
|
||||
) {
|
||||
if pre.was_respent && !pre.was_funded {
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
// Respent status is stable across a receive, so every output lands on
|
||||
// a respent address iff the address was already respent.
|
||||
if pre.was_respent {
|
||||
*self.output_events.get_mut_unwrap(output_type) += u64::from(output_count);
|
||||
*self.active.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
let after = addr_data.respent_supply_contribution();
|
||||
self.supply
|
||||
.apply_delta(output_type, pre.respent_contribution, after);
|
||||
}
|
||||
|
||||
/// Apply reused-flavor updates for a spent UTXO, AFTER the send has
|
||||
/// mutated `addr_data`. Sends don't change the reused predicate, so
|
||||
/// `pre.was_reused == is_reused` post-spend.
|
||||
#[inline]
|
||||
pub(crate) fn on_send_as_reused(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrSendPreState,
|
||||
is_first_encounter: bool,
|
||||
also_received: bool,
|
||||
will_be_empty: bool,
|
||||
) {
|
||||
if pre.was_reused {
|
||||
*self.input_events.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
// Active reused: first-encounter sender, currently reused, and not
|
||||
// already counted on the receive side.
|
||||
if is_first_encounter && pre.was_reused && !also_received {
|
||||
*self.active.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
if will_be_empty && pre.was_reused {
|
||||
*self.funded.get_mut_unwrap(output_type) -= 1;
|
||||
}
|
||||
let after = addr_data.reused_supply_contribution();
|
||||
self.supply
|
||||
.apply_delta(output_type, pre.reused_contribution, after);
|
||||
}
|
||||
|
||||
/// Apply respent-flavor updates for a spent UTXO, AFTER the send has
|
||||
/// mutated `addr_data`. Sends CAN cross the respent threshold on the
|
||||
/// 2nd lifetime spend.
|
||||
#[inline]
|
||||
pub(crate) fn on_send_as_respent(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrSendPreState,
|
||||
is_first_encounter: bool,
|
||||
also_received: bool,
|
||||
will_be_empty: bool,
|
||||
) {
|
||||
if pre.was_respent {
|
||||
*self.input_events.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
|
||||
let is_now_respent = addr_data.is_respent();
|
||||
|
||||
// Threshold crossing: the 2nd spend ever on this address. Always
|
||||
// bumps the monotonic total. Bumps the funded count iff the address
|
||||
// still has a balance. If the crossing spend also empties the
|
||||
// address, the `will_be_empty` branch below doesn't decrement
|
||||
// (was_respent is false), so the funded count stays correct.
|
||||
if is_now_respent && !pre.was_respent {
|
||||
*self.total.get_mut_unwrap(output_type) += 1;
|
||||
if !will_be_empty {
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Active respent splits cleanly into two disjoint branches (gated on
|
||||
// `pre.was_respent`):
|
||||
// - was already respent + active this block, and not also counted
|
||||
// on the receive side: pure senders on first spend.
|
||||
// - crosses the respent threshold this block: fires once per
|
||||
// address ever, on the exact crossing spend.
|
||||
if (is_first_encounter && pre.was_respent && !also_received)
|
||||
|| (is_now_respent && !pre.was_respent)
|
||||
{
|
||||
*self.active.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
|
||||
// Leaving the funded respent set on empty uses pre-spend state: a
|
||||
// threshold-crossing spend that also empties the address never
|
||||
// entered the funded set above (gated on !will_be_empty), so we
|
||||
// don't double-decrement.
|
||||
if will_be_empty && pre.was_respent {
|
||||
*self.funded.get_mut_unwrap(output_type) -= 1;
|
||||
}
|
||||
|
||||
let after = addr_data.respent_supply_contribution();
|
||||
self.supply
|
||||
.apply_delta(output_type, pre.respent_contribution, after);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&ReusedAddrVecs, Height)> for ReusedAddrState {
|
||||
#[inline]
|
||||
fn from((vecs, starting_height): (&ReusedAddrVecs, Height)) -> Self {
|
||||
Self {
|
||||
funded: AddrTypeToAddrCount::from((&vecs.count.funded, starting_height)),
|
||||
total: AddrTypeToAddrCount::from((&vecs.count.total, starting_height)),
|
||||
supply: AddrTypeToSupply::from((&vecs.supply, starting_height)),
|
||||
output_events: AddrTypeToAddrEventCount::default(),
|
||||
input_events: AddrTypeToAddrEventCount::default(),
|
||||
active: AddrTypeToAddrEventCount::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
use brk_types::{FundedAddrData, Height, OutputType, Sats};
|
||||
|
||||
use crate::distribution::{block::TrackingStatus, vecs::AddrMetricsVecs};
|
||||
|
||||
use super::{AddrTypeToActivityCounts, AddrTypeToAddrCount, ExposedAddrState, ReusedAddrState};
|
||||
|
||||
/// Bundle of per-block runtime state for the full address-metrics pipeline.
|
||||
/// Feeds `process_received` / `process_sent` and is pushed to [`AddrMetricsVecs`]
|
||||
/// once per block.
|
||||
///
|
||||
/// Recovery: [`From<(&AddrMetricsVecs, Height)>`] reads the prior block from
|
||||
/// disk to seed all persistent running totals. Per-block counters (activity,
|
||||
/// and event counts inside each [`ReusedAddrState`]) default to zero and are
|
||||
/// cleared at the top of each block via [`Self::reset_per_block`].
|
||||
#[derive(Debug, Default)]
|
||||
pub struct AddrMetricsState {
|
||||
pub funded: AddrTypeToAddrCount,
|
||||
pub empty: AddrTypeToAddrCount,
|
||||
pub activity: AddrTypeToActivityCounts,
|
||||
pub reused: ReusedAddrState,
|
||||
pub respent: ReusedAddrState,
|
||||
pub exposed: ExposedAddrState,
|
||||
}
|
||||
|
||||
/// Snapshot of [`FundedAddrData`] taken BEFORE a receive mutates it.
|
||||
/// Feeds delta-based updates in [`AddrMetricsState::on_receive_applied`].
|
||||
#[derive(Debug)]
|
||||
pub struct AddrReceivePreState {
|
||||
pub was_funded: bool,
|
||||
pub was_reused: bool,
|
||||
pub was_respent: bool,
|
||||
pub was_pubkey_exposed: bool,
|
||||
pub prev_funded_txo_count: u32,
|
||||
pub exposed_contribution: Sats,
|
||||
pub reused_contribution: Sats,
|
||||
pub respent_contribution: Sats,
|
||||
}
|
||||
|
||||
impl AddrReceivePreState {
|
||||
#[inline]
|
||||
pub fn capture(addr_data: &FundedAddrData, output_type: OutputType) -> Self {
|
||||
Self {
|
||||
was_funded: addr_data.is_funded(),
|
||||
was_reused: addr_data.is_reused(),
|
||||
was_respent: addr_data.is_respent(),
|
||||
was_pubkey_exposed: addr_data.is_pubkey_exposed(output_type),
|
||||
prev_funded_txo_count: addr_data.funded_txo_count,
|
||||
exposed_contribution: addr_data.exposed_supply_contribution(output_type),
|
||||
reused_contribution: addr_data.reused_supply_contribution(),
|
||||
respent_contribution: addr_data.respent_supply_contribution(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Snapshot of [`FundedAddrData`] taken BEFORE a spend mutates it.
|
||||
/// Feeds delta-based updates in [`AddrMetricsState::on_send_applied`].
|
||||
#[derive(Debug)]
|
||||
pub struct AddrSendPreState {
|
||||
pub was_reused: bool,
|
||||
pub was_respent: bool,
|
||||
pub was_pubkey_exposed: bool,
|
||||
pub exposed_contribution: Sats,
|
||||
pub reused_contribution: Sats,
|
||||
pub respent_contribution: Sats,
|
||||
}
|
||||
|
||||
impl AddrSendPreState {
|
||||
#[inline]
|
||||
pub fn capture(addr_data: &FundedAddrData, output_type: OutputType) -> Self {
|
||||
Self {
|
||||
was_reused: addr_data.is_reused(),
|
||||
was_respent: addr_data.is_respent(),
|
||||
was_pubkey_exposed: addr_data.is_pubkey_exposed(output_type),
|
||||
exposed_contribution: addr_data.exposed_supply_contribution(output_type),
|
||||
reused_contribution: addr_data.reused_supply_contribution(),
|
||||
respent_contribution: addr_data.respent_supply_contribution(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddrMetricsState {
|
||||
#[inline]
|
||||
pub(crate) fn reset_per_block(&mut self) {
|
||||
self.activity.reset();
|
||||
self.reused.reset_per_block();
|
||||
self.respent.reset_per_block();
|
||||
}
|
||||
|
||||
/// Apply all state updates for a received output, AFTER the cohort and
|
||||
/// `addr_data` have been mutated. `pre` is the snapshot captured before
|
||||
/// the mutation, `addr_data` is the post-receive view.
|
||||
#[inline]
|
||||
pub(crate) fn on_receive_applied(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
status: TrackingStatus,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrReceivePreState,
|
||||
output_count: u32,
|
||||
) {
|
||||
let activity = self.activity.get_mut_unwrap(output_type);
|
||||
activity.receiving += 1;
|
||||
match status {
|
||||
TrackingStatus::New => {
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
TrackingStatus::WasEmpty => {
|
||||
activity.reactivated += 1;
|
||||
*self.funded.get_mut_unwrap(output_type) += 1;
|
||||
*self.empty.get_mut_unwrap(output_type) -= 1;
|
||||
}
|
||||
TrackingStatus::Tracked => {}
|
||||
}
|
||||
self.reused
|
||||
.on_receive_as_reused(output_type, addr_data, pre, output_count);
|
||||
self.respent
|
||||
.on_receive_as_respent(output_type, addr_data, pre, output_count);
|
||||
self.exposed.on_receive(output_type, addr_data, pre, status);
|
||||
}
|
||||
|
||||
/// Apply all state updates for a spent UTXO, AFTER the cohort and
|
||||
/// `addr_data` have been mutated. `pre` is the snapshot captured before
|
||||
/// the mutation. `is_first_encounter` / `also_received` come from the
|
||||
/// caller's per-block seen/received tracking. `will_be_empty` is from
|
||||
/// the pre-mutation `addr_data.has_1_utxos()`.
|
||||
#[inline]
|
||||
pub(crate) fn on_send_applied(
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
addr_data: &FundedAddrData,
|
||||
pre: &AddrSendPreState,
|
||||
is_first_encounter: bool,
|
||||
also_received: bool,
|
||||
will_be_empty: bool,
|
||||
) {
|
||||
if is_first_encounter {
|
||||
let activity = self.activity.get_mut_unwrap(output_type);
|
||||
activity.sending += 1;
|
||||
if also_received {
|
||||
activity.bidirectional += 1;
|
||||
}
|
||||
}
|
||||
if will_be_empty {
|
||||
*self.funded.get_mut_unwrap(output_type) -= 1;
|
||||
*self.empty.get_mut_unwrap(output_type) += 1;
|
||||
}
|
||||
self.reused.on_send_as_reused(
|
||||
output_type,
|
||||
addr_data,
|
||||
pre,
|
||||
is_first_encounter,
|
||||
also_received,
|
||||
will_be_empty,
|
||||
);
|
||||
self.respent.on_send_as_respent(
|
||||
output_type,
|
||||
addr_data,
|
||||
pre,
|
||||
is_first_encounter,
|
||||
also_received,
|
||||
will_be_empty,
|
||||
);
|
||||
self.exposed
|
||||
.on_send(output_type, addr_data, pre, will_be_empty);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&AddrMetricsVecs, Height)> for AddrMetricsState {
|
||||
#[inline]
|
||||
fn from((vecs, starting_height): (&AddrMetricsVecs, Height)) -> Self {
|
||||
Self {
|
||||
funded: AddrTypeToAddrCount::from((&vecs.funded, starting_height)),
|
||||
empty: AddrTypeToAddrCount::from((&vecs.empty, starting_height)),
|
||||
activity: AddrTypeToActivityCounts::default(),
|
||||
reused: ReusedAddrState::from((&vecs.reused, starting_height)),
|
||||
respent: ReusedAddrState::from((&vecs.respent, starting_height)),
|
||||
exposed: ExposedAddrState::from((&vecs.exposed, starting_height)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//! Generic per-address-type supply tracking, shared across predicate-based
|
||||
//! supply categories (exposed, reused, respent). A "category supply" is the
|
||||
//! running sum of balances held by addresses currently in the funded subset
|
||||
//! defined by some predicate.
|
||||
|
||||
mod share;
|
||||
mod state;
|
||||
mod vecs;
|
||||
|
||||
pub use share::AddrSupplyShareVecs;
|
||||
pub use state::AddrTypeToSupply;
|
||||
pub use vecs::AddrSupplyVecs;
|
||||
@@ -0,0 +1,69 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, Height, Sats, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{PercentPerBlock, RatioSatsBp16, WithAddrTypes},
|
||||
};
|
||||
|
||||
use super::vecs::AddrSupplyVecs;
|
||||
|
||||
/// Share of a predicate-based supply category relative to total supply.
|
||||
///
|
||||
/// - `all`: category supply / circulating supply
|
||||
/// - Per-type: type's category supply / type's total supply
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct AddrSupplyShareVecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub WithAddrTypes<PercentPerBlock<BasisPoints16, M>>,
|
||||
);
|
||||
|
||||
impl AddrSupplyShareVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self(
|
||||
WithAddrTypes::<PercentPerBlock<BasisPoints16>>::forced_import(
|
||||
db,
|
||||
&format!("{name}_addr_supply_share"),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
supply: &AddrSupplyVecs,
|
||||
all_supply_sats: &impl ReadableVec<Height, Sats>,
|
||||
type_supply_sats: &ByAddrType<&impl ReadableVec<Height, Sats>>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.all.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&supply.all.sats.height,
|
||||
all_supply_sats,
|
||||
exit,
|
||||
)?;
|
||||
for ((_, share), ((_, cat), (_, denom))) in self
|
||||
.by_addr_type
|
||||
.iter_mut()
|
||||
.zip(supply.by_addr_type.iter().zip(type_supply_sats.iter()))
|
||||
{
|
||||
share.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&cat.sats.height,
|
||||
*denom,
|
||||
exit,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_types::{Height, OutputType, Sats};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::ReadableVec;
|
||||
|
||||
use super::vecs::AddrSupplyVecs;
|
||||
|
||||
/// Per-addr-type running-total of a supply category (sats). Shared across
|
||||
/// predicate-based supply categories (exposed, reused, respent).
|
||||
#[derive(Debug, Default, Deref, DerefMut)]
|
||||
pub struct AddrTypeToSupply(ByAddrType<Sats>);
|
||||
|
||||
impl AddrTypeToSupply {
|
||||
#[inline]
|
||||
pub(crate) fn sum(&self) -> Sats {
|
||||
self.0.values().copied().sum()
|
||||
}
|
||||
|
||||
/// Apply a signed `after - before` delta to the slot for `output_type`.
|
||||
/// Sats is unsigned, so branch on sign.
|
||||
#[inline]
|
||||
pub(crate) fn apply_delta(&mut self, output_type: OutputType, before: Sats, after: Sats) {
|
||||
let slot = self.get_mut_unwrap(output_type);
|
||||
if after >= before {
|
||||
*slot += after - before;
|
||||
} else {
|
||||
*slot -= before - after;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ByAddrType<Sats>> for AddrTypeToSupply {
|
||||
#[inline]
|
||||
fn from(value: ByAddrType<Sats>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&AddrSupplyVecs, Height)> for AddrTypeToSupply {
|
||||
#[inline]
|
||||
fn from((vecs, starting_height): (&AddrSupplyVecs, Height)) -> Self {
|
||||
let Some(prev_height) = starting_height.decremented() else {
|
||||
return Self::default();
|
||||
};
|
||||
vecs.by_addr_type
|
||||
.map_with_name(|_, v| v.sats.height.collect_one(prev_height).unwrap())
|
||||
.into()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::Version;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Database, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ValuePerBlock, WithAddrTypes},
|
||||
};
|
||||
|
||||
use super::AddrTypeToSupply;
|
||||
|
||||
/// Per-addr-type running supply (sats/btc/cents/usd) with an aggregated `all`.
|
||||
/// Shared across predicate-based supply categories (exposed, reused, respent).
|
||||
/// Sats are pushed stateful per block; cents/usd are derived post-hoc from
|
||||
/// sats × spot price.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct AddrSupplyVecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub WithAddrTypes<ValuePerBlock<M>>,
|
||||
);
|
||||
|
||||
impl AddrSupplyVecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self(WithAddrTypes::<ValuePerBlock>::forced_import(
|
||||
db,
|
||||
&format!("{name}_addr_supply"),
|
||||
version,
|
||||
indexes,
|
||||
)?))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn push_supply(&mut self, supply: &AddrTypeToSupply) {
|
||||
self.push_height(supply.sum(), supply.values().copied());
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,21 @@
|
||||
use brk_cohort::ByAddrType;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, StoredU64, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Database, Exit, Rw, StorageMode};
|
||||
|
||||
use crate::{indexes, internal::PerBlock};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{PerBlock, WithAddrTypes},
|
||||
};
|
||||
|
||||
use super::AddrCountsVecs;
|
||||
|
||||
/// Total address count (global + per-type) with all derived indexes
|
||||
#[derive(Traversable)]
|
||||
pub struct TotalAddrCountVecs<M: StorageMode = Rw> {
|
||||
pub all: PerBlock<StoredU64, M>,
|
||||
#[traversable(flatten)]
|
||||
pub by_addr_type: ByAddrType<PerBlock<StoredU64, M>>,
|
||||
}
|
||||
/// Total address count (global + per-type) with all derived indexes.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct TotalAddrCountVecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub WithAddrTypes<PerBlock<StoredU64, M>>,
|
||||
);
|
||||
|
||||
impl TotalAddrCountVecs {
|
||||
pub(crate) fn forced_import(
|
||||
@@ -22,22 +23,12 @@ impl TotalAddrCountVecs {
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let all = PerBlock::forced_import(db, "total_addr_count", version, indexes)?;
|
||||
|
||||
let by_addr_type: ByAddrType<PerBlock<StoredU64>> =
|
||||
ByAddrType::new_with_name(|name| {
|
||||
PerBlock::forced_import(
|
||||
db,
|
||||
&format!("{name}_total_addr_count"),
|
||||
version,
|
||||
indexes,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(Self {
|
||||
all,
|
||||
by_addr_type,
|
||||
})
|
||||
Ok(Self(WithAddrTypes::<PerBlock<StoredU64>>::forced_import(
|
||||
db,
|
||||
"total_addr_count",
|
||||
version,
|
||||
indexes,
|
||||
)?))
|
||||
}
|
||||
|
||||
/// Eagerly compute total = addr_count + empty_addr_count.
|
||||
|
||||
@@ -103,9 +103,7 @@ pub(crate) fn load_uncached_addr_data(
|
||||
// Check if this is a new address (type_index >= first for this height)
|
||||
let first = *first_addr_indexes.get(addr_type).unwrap();
|
||||
if first <= type_index {
|
||||
return Ok(Some(WithAddrDataSource::New(
|
||||
FundedAddrData::default(),
|
||||
)));
|
||||
return Ok(Some(WithAddrDataSource::New(FundedAddrData::default())));
|
||||
}
|
||||
|
||||
// Skip if already in cache
|
||||
|
||||
@@ -26,10 +26,7 @@ impl<'a> AddrLookup<'a> {
|
||||
&mut self,
|
||||
output_type: OutputType,
|
||||
type_index: TypeIndex,
|
||||
) -> (
|
||||
&mut WithAddrDataSource<FundedAddrData>,
|
||||
TrackingStatus,
|
||||
) {
|
||||
) -> (&mut WithAddrDataSource<FundedAddrData>, TrackingStatus) {
|
||||
use std::collections::hash_map::Entry;
|
||||
|
||||
let map = self.funded.get_mut(output_type).unwrap();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use brk_error::Result;
|
||||
use brk_types::{
|
||||
AnyAddrIndex, EmptyAddrData, EmptyAddrIndex, FundedAddrData, FundedAddrIndex,
|
||||
OutputType, TypeIndex,
|
||||
AnyAddrIndex, EmptyAddrData, EmptyAddrIndex, FundedAddrData, FundedAddrIndex, OutputType,
|
||||
TypeIndex,
|
||||
};
|
||||
use vecdb::AnyVec;
|
||||
|
||||
@@ -67,14 +67,13 @@ pub(crate) fn process_funded_addrs(
|
||||
|
||||
// Pure pushes - no holes remain
|
||||
addrs_data.funded.reserve_pushed(pushes_iter.len());
|
||||
let mut next_index = addrs_data.funded.len();
|
||||
for (addr_type, type_index, data) in pushes_iter {
|
||||
for (next_index, (addr_type, type_index, data)) in (addrs_data.funded.len()..).zip(pushes_iter)
|
||||
{
|
||||
addrs_data.funded.push(data);
|
||||
result.get_mut(addr_type).unwrap().insert(
|
||||
type_index,
|
||||
AnyAddrIndex::from(FundedAddrIndex::from(next_index)),
|
||||
);
|
||||
next_index += 1;
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
@@ -138,14 +137,12 @@ pub(crate) fn process_empty_addrs(
|
||||
|
||||
// Pure pushes - no holes remain
|
||||
addrs_data.empty.reserve_pushed(pushes_iter.len());
|
||||
let mut next_index = addrs_data.empty.len();
|
||||
for (addr_type, type_index, data) in pushes_iter {
|
||||
for (next_index, (addr_type, type_index, data)) in (addrs_data.empty.len()..).zip(pushes_iter) {
|
||||
addrs_data.empty.push(data);
|
||||
result.get_mut(addr_type).unwrap().insert(
|
||||
type_index,
|
||||
AnyAddrIndex::from(EmptyAddrIndex::from(next_index)),
|
||||
);
|
||||
next_index += 1;
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use brk_cohort::{AmountBucket, ByAddrType};
|
||||
use brk_cohort::AmountBucket;
|
||||
use brk_types::{Cents, Sats, TypeIndex};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::distribution::{
|
||||
addr::{AddrTypeToActivityCounts, AddrTypeToVec},
|
||||
addr::{AddrMetricsState, AddrReceivePreState, AddrTypeToVec},
|
||||
cohorts::AddrCohorts,
|
||||
};
|
||||
|
||||
@@ -16,17 +16,18 @@ struct AggregatedReceive {
|
||||
output_count: u32,
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn process_received(
|
||||
received_data: AddrTypeToVec<(TypeIndex, Sats)>,
|
||||
cohorts: &mut AddrCohorts,
|
||||
lookup: &mut AddrLookup<'_>,
|
||||
price: Cents,
|
||||
addr_count: &mut ByAddrType<u64>,
|
||||
empty_addr_count: &mut ByAddrType<u64>,
|
||||
activity_counts: &mut AddrTypeToActivityCounts,
|
||||
state: &mut AddrMetricsState,
|
||||
) {
|
||||
let max_type_len = received_data.iter().map(|(_, v)| v.len()).max().unwrap_or(0);
|
||||
let max_type_len = received_data
|
||||
.iter()
|
||||
.map(|(_, v)| v.len())
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
let mut aggregated: FxHashMap<TypeIndex, AggregatedReceive> =
|
||||
FxHashMap::with_capacity_and_hasher(max_type_len, Default::default());
|
||||
|
||||
@@ -35,12 +36,7 @@ pub(crate) fn process_received(
|
||||
continue;
|
||||
}
|
||||
|
||||
// Cache mutable refs for this address type
|
||||
let type_addr_count = addr_count.get_mut(output_type).unwrap();
|
||||
let type_empty_count = empty_addr_count.get_mut(output_type).unwrap();
|
||||
let type_activity = activity_counts.get_mut_unwrap(output_type);
|
||||
|
||||
// Aggregate receives by address - each address processed exactly once
|
||||
// Aggregate per address so each address is processed exactly once.
|
||||
for (type_index, value) in vec {
|
||||
let entry = aggregated.entry(type_index).or_default();
|
||||
entry.total_value += value;
|
||||
@@ -49,32 +45,13 @@ pub(crate) fn process_received(
|
||||
|
||||
for (type_index, recv) in aggregated.drain() {
|
||||
let (addr_data, status) = lookup.get_or_create_for_receive(output_type, type_index);
|
||||
let pre = AddrReceivePreState::capture(addr_data, output_type);
|
||||
|
||||
// Track receiving activity - each address in receive aggregation
|
||||
type_activity.receiving += 1;
|
||||
|
||||
match status {
|
||||
TrackingStatus::New => {
|
||||
*type_addr_count += 1;
|
||||
}
|
||||
TrackingStatus::WasEmpty => {
|
||||
*type_addr_count += 1;
|
||||
*type_empty_count -= 1;
|
||||
// Reactivated - was empty, now has funds
|
||||
type_activity.reactivated += 1;
|
||||
}
|
||||
TrackingStatus::Tracked => {}
|
||||
}
|
||||
|
||||
let is_new_entry = matches!(status, TrackingStatus::New | TrackingStatus::WasEmpty);
|
||||
|
||||
if is_new_entry {
|
||||
// New/was-empty address - just add to cohort
|
||||
if matches!(status, TrackingStatus::New | TrackingStatus::WasEmpty) {
|
||||
addr_data.receive_outputs(recv.total_value, price, recv.output_count);
|
||||
let new_bucket = AmountBucket::from(recv.total_value);
|
||||
cohorts
|
||||
.amount_range
|
||||
.get_mut_by_bucket(new_bucket)
|
||||
.get_mut_by_bucket(AmountBucket::from(recv.total_value))
|
||||
.state
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
@@ -86,7 +63,6 @@ pub(crate) fn process_received(
|
||||
let new_bucket = AmountBucket::from(new_balance);
|
||||
|
||||
if let Some((old_bucket, new_bucket)) = prev_bucket.transition_to(new_bucket) {
|
||||
// Crossing cohort boundary - subtract from old, add to new
|
||||
let cohort_state = cohorts
|
||||
.amount_range
|
||||
.get_mut_by_bucket(old_bucket)
|
||||
@@ -94,7 +70,6 @@ pub(crate) fn process_received(
|
||||
.as_mut()
|
||||
.unwrap();
|
||||
|
||||
// Debug info for tracking down underflow issues
|
||||
if cohort_state.inner.supply.utxo_count < addr_data.utxo_count() as u64 {
|
||||
panic!(
|
||||
"process_received: cohort underflow detected!\n\
|
||||
@@ -120,7 +95,6 @@ pub(crate) fn process_received(
|
||||
.unwrap()
|
||||
.add(addr_data);
|
||||
} else {
|
||||
// Staying in same cohort - just receive
|
||||
cohorts
|
||||
.amount_range
|
||||
.get_mut_by_bucket(new_bucket)
|
||||
@@ -130,6 +104,8 @@ pub(crate) fn process_received(
|
||||
.receive_outputs(addr_data, recv.total_value, price, recv.output_count);
|
||||
}
|
||||
}
|
||||
|
||||
state.on_receive_applied(output_type, status, addr_data, &pre, recv.output_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user