diff --git a/Cargo.toml b/Cargo.toml index cd4cb67a6..2bdc0db34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ package.rust-version = "1.89" [profile.dev] lto = "thin" debug = true -codegen-units = 2 +codegen-units = 1 opt-level = 3 overflow-checks = true diff --git a/crates/brk_computer/src/chain.rs b/crates/brk_computer/src/chain.rs index da1562b71..e03275936 100644 --- a/crates/brk_computer/src/chain.rs +++ b/crates/brk_computer/src/chain.rs @@ -23,6 +23,7 @@ use super::{Indexes, indexes, price}; const VERSION: Version = Version::ZERO; const TARGET_BLOCKS_PER_DAY_F64: f64 = 144.0; +const TARGET_BLOCKS_PER_DAY_F32: f32 = 144.0; const TARGET_BLOCKS_PER_DAY: u64 = 144; const TARGET_BLOCKS_PER_WEEK: u64 = 7 * TARGET_BLOCKS_PER_DAY; const TARGET_BLOCKS_PER_MONTH: u64 = 30 * TARGET_BLOCKS_PER_DAY; @@ -112,6 +113,9 @@ pub struct Vecs { pub indexes_to_hash_rate_2m_sma: ComputedVecsFromDateIndex, pub indexes_to_hash_rate_1y_sma: ComputedVecsFromDateIndex, pub indexes_to_difficulty_as_hash: ComputedVecsFromDateIndex, + pub indexes_to_difficulty_adjustment: ComputedVecsFromHeight, + pub indexes_to_blocks_before_next_difficulty_adjustment: ComputedVecsFromHeight, + pub indexes_to_days_before_next_difficulty_adjustment: ComputedVecsFromHeight, } impl Vecs { @@ -887,6 +891,32 @@ impl Vecs { indexes, VecBuilderOptions::default().add_last(), )?, + indexes_to_difficulty_adjustment: ComputedVecsFromHeight::forced_import( + &db, + "difficulty_adjustment", + Source::Compute, + version + VERSION + Version::ZERO, + indexes, + VecBuilderOptions::default().add_sum(), + )?, + indexes_to_blocks_before_next_difficulty_adjustment: + ComputedVecsFromHeight::forced_import( + &db, + "blocks_before_next_difficulty_adjustment", + Source::Compute, + version + VERSION + Version::TWO, + indexes, + VecBuilderOptions::default().add_last(), + )?, + indexes_to_days_before_next_difficulty_adjustment: + ComputedVecsFromHeight::forced_import( + &db, + "days_before_next_difficulty_adjustment", + Source::Compute, + version + VERSION + Version::TWO, + indexes, + VecBuilderOptions::default().add_last(), + )?, txindex_to_is_coinbase, inputindex_to_value, @@ -1643,6 +1673,46 @@ impl Vecs { })?; } + self.indexes_to_difficulty_adjustment.compute_all( + indexes, + starting_indexes, + exit, + |v| { + v.compute_percentage_change( + starting_indexes.height, + &indexer.vecs.height_to_difficulty, + 1, + exit, + )?; + Ok(()) + }, + )?; + + self.indexes_to_blocks_before_next_difficulty_adjustment + .compute_all(indexes, starting_indexes, exit, |v| { + v.compute_transform( + starting_indexes.height, + &indexes.height_to_height, + |(h, ..)| (h, StoredU32::from(h.left_before_next_diff_adj())), + exit, + )?; + Ok(()) + })?; + + self.indexes_to_days_before_next_difficulty_adjustment + .compute_all(indexes, starting_indexes, exit, |v| { + v.compute_transform( + starting_indexes.height, + self.indexes_to_blocks_before_next_difficulty_adjustment + .height + .as_ref() + .unwrap(), + |(h, blocks, ..)| (h, (*blocks as f32 / TARGET_BLOCKS_PER_DAY_F32).into()), + exit, + )?; + Ok(()) + })?; + Ok(()) } @@ -1691,9 +1761,22 @@ impl Vecs { iter = Box::new(iter.chain(self.indexes_to_block_weight.iter_any_collectable())); iter = Box::new(iter.chain(self.indexes_to_difficulty.iter_any_collectable())); + iter = Box::new(iter.chain(self.indexes_to_difficulty_adjustment.iter_any_collectable())); iter = Box::new(iter.chain(self.indexes_to_difficultyepoch.iter_any_collectable())); iter = Box::new(iter.chain(self.indexes_to_halvingepoch.iter_any_collectable())); iter = Box::new(iter.chain(self.indexes_to_difficulty_as_hash.iter_any_collectable())); + iter = Box::new( + iter.chain( + self.indexes_to_blocks_before_next_difficulty_adjustment + .iter_any_collectable(), + ), + ); + iter = Box::new( + iter.chain( + self.indexes_to_days_before_next_difficulty_adjustment + .iter_any_collectable(), + ), + ); iter = Box::new(iter.chain(self.indexes_to_coinbase.iter_any_collectable())); iter = Box::new(iter.chain(self.indexes_to_fee.iter_any_collectable())); diff --git a/crates/brk_structs/src/structs/difficultyepoch.rs b/crates/brk_structs/src/structs/difficultyepoch.rs index dd2f4f7d4..b6ad7f045 100644 --- a/crates/brk_structs/src/structs/difficultyepoch.rs +++ b/crates/brk_structs/src/structs/difficultyepoch.rs @@ -10,6 +10,8 @@ use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use super::Height; +pub const BLOCKS_PER_DIFF_EPOCHS: u32 = 2016; + #[derive( Debug, Clone, @@ -79,7 +81,7 @@ impl Div for DifficultyEpoch { impl From for DifficultyEpoch { fn from(value: Height) -> Self { - Self((u32::from(value) / 2016) as u16) + Self((u32::from(value) / BLOCKS_PER_DIFF_EPOCHS) as u16) } } diff --git a/crates/brk_structs/src/structs/height.rs b/crates/brk_structs/src/structs/height.rs index d90e1d146..3c2ce2a2a 100644 --- a/crates/brk_structs/src/structs/height.rs +++ b/crates/brk_structs/src/structs/height.rs @@ -12,7 +12,7 @@ use vecdb::{CheckedSub, Printable, Stamp, StoredCompressed}; use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; -use crate::copy_first_4bytes; +use crate::{BLOCKS_PER_DIFF_EPOCHS, copy_first_4bytes}; use super::StoredU64; @@ -72,6 +72,10 @@ impl Height { pub fn is_not_zero(self) -> bool { self != Self::ZERO } + + pub fn left_before_next_diff_adj(self) -> u32 { + BLOCKS_PER_DIFF_EPOCHS - (*self % 2016) + } } impl PartialEq for Height { diff --git a/websites/bitview/packages/lightweight-charts/wrapper.js b/websites/bitview/packages/lightweight-charts/wrapper.js index bbaf9f165..b094002e3 100644 --- a/websites/bitview/packages/lightweight-charts/wrapper.js +++ b/websites/bitview/packages/lightweight-charts/wrapper.js @@ -691,6 +691,9 @@ function createChartElement({ visible: defaultActive !== false, priceLineVisible: false, color: color(), + // lineVisible: false, + // pointMarkersVisible: true, + // pointMarkersRadius: 1.375, ...options, }, paneIndex, diff --git a/websites/bitview/scripts/main.js b/websites/bitview/scripts/main.js index 1ae520e30..c01e54d3b 100644 --- a/websites/bitview/scripts/main.js +++ b/websites/bitview/scripts/main.js @@ -39,6 +39,7 @@ * "Bool" | * "Days" | * "%mcap" | + * "blocks" | * "%cmcap" | * "%cp+l" | * "%rcap" | @@ -57,7 +58,6 @@ * "sd" | * "Epoch" | * "Height" | - * "Type" | * "Bytes" * } Unit */ @@ -768,6 +768,9 @@ function createUtils() { if ((!unit || thoroughUnitCheck) && id === "chain") { setUnit("block"); } + if ((!unit || thoroughUnitCheck) && id.startsWith("blocks_before")) { + setUnit("blocks"); + } if ( (!unit || thoroughUnitCheck) && (id === "emptyaddressdata" || id === "loadedaddressdata") @@ -835,6 +838,7 @@ function createUtils() { if ( (!unit || thoroughUnitCheck) && (id === "price_drawdown" || + id === "difficulty_adjustment" || id.endsWith("oscillator") || id.endsWith("dominance") || id.endsWith("returns") || @@ -978,7 +982,9 @@ function createUtils() { } if ( (!unit || thoroughUnitCheck) && - (id.includes("days_between") || id.includes("days_since")) + (id.includes("days_between") || + id.includes("days_since") || + id.startsWith("days_before")) ) { setUnit("Days"); } diff --git a/websites/bitview/scripts/options.js b/websites/bitview/scripts/options.js index 22f12a4df..44864b3d2 100644 --- a/websites/bitview/scripts/options.js +++ b/websites/bitview/scripts/options.js @@ -3764,6 +3764,19 @@ function createPartialOptions({ env, colors, vecIdToIndexes, pools }) { key: "difficultyepoch", name: "Epoch", }), + { + key: "difficulty_adjustment", + title: "Adjustment", + type: "Baseline", + }, + createBaseSeries({ + key: "blocks_before_next_difficulty_adjustment", + name: "Before next", + }), + createBaseSeries({ + key: "days_before_next_difficulty_adjustment", + name: "Before next", + }), ], }, {