From d3319ba92baeac99b5abab9fb62714a78b467b20 Mon Sep 17 00:00:00 2001 From: Arya Date: Fri, 2 Aug 2024 02:53:45 -0400 Subject: [PATCH] marks db as upgraded, maintains a copy of the value balance in the old format --- .../disk_format/tests/snapshot.rs | 12 ++++++- ...p_chain_value_pool_raw_data@mainnet_1.snap | 8 +++++ ...p_chain_value_pool_raw_data@mainnet_2.snap | 8 +++++ ...p_chain_value_pool_raw_data@testnet_1.snap | 8 +++++ ...p_chain_value_pool_raw_data@testnet_2.snap | 8 +++++ ...p_chain_value_pool_raw_data@mainnet_1.snap | 4 +++ ...p_chain_value_pool_raw_data@mainnet_2.snap | 4 +++ ...p_chain_value_pool_raw_data@testnet_1.snap | 4 +++ ...p_chain_value_pool_raw_data@testnet_2.snap | 4 +++ .../finalized_state/disk_format/upgrade.rs | 8 +++++ .../service/finalized_state/zebra_db/chain.rs | 31 +++++++++++-------- 11 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_1.snap create mode 100644 zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_2.snap create mode 100644 zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_1.snap create mode 100644 zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_2.snap diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs b/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs index eb12cf41f..56cb7bbff 100644 --- a/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs @@ -37,7 +37,7 @@ use crate::{ disk_format::{tests::KV, RawBytes}, FinalizedState, }, - Config, ReadDisk, + Config, IntoDisk, ReadDisk, }; /// Snapshot test for RocksDB column families, and their key-value data. @@ -145,6 +145,16 @@ fn snapshot_raw_rocksdb_column_family_data(db: &DiskDb, original_cf_names: &[Str // because those roots are used to populate the anchor column families. insta::assert_ron_snapshot!(format!("{cf_name}_raw_data"), cf_data); } + + if cf_name == "tip_chain_value_pool" && !cf_data.is_empty() { + let chain_value_pool_cf = db.cf_handle("tip_chain_value_pool").unwrap(); + let value_pool: RawBytes = db + .zs_get(&chain_value_pool_cf, &()) + .expect("should have value at &()"); + + let cf_data = KV::new(().as_bytes(), value_pool.raw_bytes()); + insta::assert_ron_snapshot!(format!("legacy_tip_chain_value_pool_raw_data"), cf_data); + } } insta::assert_ron_snapshot!("empty_column_families", empty_column_families); diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_1.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_1.snap new file mode 100644 index 000000000..65e2cbbea --- /dev/null +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_1.snap @@ -0,0 +1,8 @@ +--- +source: zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs +expression: cf_data +--- +KV( + k: "", + v: "24f4000000000000000000000000000000000000000000000000000000000000", +) diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_2.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_2.snap new file mode 100644 index 000000000..55f06f617 --- /dev/null +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@mainnet_2.snap @@ -0,0 +1,8 @@ +--- +source: zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs +expression: cf_data +--- +KV( + k: "", + v: "6cdc020000000000000000000000000000000000000000000000000000000000", +) diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_1.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_1.snap new file mode 100644 index 000000000..65e2cbbea --- /dev/null +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_1.snap @@ -0,0 +1,8 @@ +--- +source: zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs +expression: cf_data +--- +KV( + k: "", + v: "24f4000000000000000000000000000000000000000000000000000000000000", +) diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_2.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_2.snap new file mode 100644 index 000000000..55f06f617 --- /dev/null +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/legacy_tip_chain_value_pool_raw_data@testnet_2.snap @@ -0,0 +1,8 @@ +--- +source: zebra-state/src/service/finalized_state/disk_format/tests/snapshot.rs +expression: cf_data +--- +KV( + k: "", + v: "6cdc020000000000000000000000000000000000000000000000000000000000", +) diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_1.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_1.snap index e7e7e2c29..2778f1de5 100644 --- a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_1.snap +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_1.snap @@ -7,4 +7,8 @@ expression: cf_data k: "", v: "24f4000000000000000000000000000000000000000000000000000000000000", ), + KV( + k: "01", + v: "24f4000000000000000000000000000000000000000000000000000000000000", + ), ] diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_2.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_2.snap index 5b213a0b5..a825d257d 100644 --- a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_2.snap +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@mainnet_2.snap @@ -7,4 +7,8 @@ expression: cf_data k: "", v: "6cdc020000000000000000000000000000000000000000000000000000000000", ), + KV( + k: "01", + v: "6cdc020000000000000000000000000000000000000000000000000000000000", + ), ] diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_1.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_1.snap index e7e7e2c29..2778f1de5 100644 --- a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_1.snap +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_1.snap @@ -7,4 +7,8 @@ expression: cf_data k: "", v: "24f4000000000000000000000000000000000000000000000000000000000000", ), + KV( + k: "01", + v: "24f4000000000000000000000000000000000000000000000000000000000000", + ), ] diff --git a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_2.snap b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_2.snap index 5b213a0b5..a825d257d 100644 --- a/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_2.snap +++ b/zebra-state/src/service/finalized_state/disk_format/tests/snapshots/tip_chain_value_pool_raw_data@testnet_2.snap @@ -7,4 +7,8 @@ expression: cf_data k: "", v: "6cdc020000000000000000000000000000000000000000000000000000000000", ), + KV( + k: "01", + v: "6cdc020000000000000000000000000000000000000000000000000000000000", + ), ] diff --git a/zebra-state/src/service/finalized_state/disk_format/upgrade.rs b/zebra-state/src/service/finalized_state/disk_format/upgrade.rs index 7dc157b2f..7f9d3d8c1 100644 --- a/zebra-state/src/service/finalized_state/disk_format/upgrade.rs +++ b/zebra-state/src/service/finalized_state/disk_format/upgrade.rs @@ -541,6 +541,14 @@ impl DbFormatChange { timer.finish(module_path!(), line!(), "tree keys and caches upgrade"); } + let version_for_value_balance_format = + Version::parse("25.4.0").expect("hard-coded version string should be valid."); + + // Check if we need to do the upgrade. + if older_disk_version < &version_for_value_balance_format { + Self::mark_as_upgraded_to(db, &version_for_value_balance_format); + } + // # New Upgrades Usually Go Here // // New code goes above this comment! diff --git a/zebra-state/src/service/finalized_state/zebra_db/chain.rs b/zebra-state/src/service/finalized_state/zebra_db/chain.rs index 5653af1c3..0e6a476d6 100644 --- a/zebra-state/src/service/finalized_state/zebra_db/chain.rs +++ b/zebra-state/src/service/finalized_state/zebra_db/chain.rs @@ -17,7 +17,10 @@ use std::{ }; use zebra_chain::{ - amount::NonNegative, block::Height, history_tree::HistoryTree, transparent, + amount::{Amount, NonNegative}, + block::Height, + history_tree::HistoryTree, + transparent, value_balance::ValueBalance, }; @@ -60,7 +63,7 @@ pub const CHAIN_VALUE_POOLS: &str = "tip_chain_value_pool"; /// /// This constant should be used so the compiler can detect incorrectly typed accesses to the /// column family. -pub type ChainValuePoolsCf<'cf> = TypedColumnFamily<'cf, (), ValueBalance>; +pub type ChainValuePoolsCf<'cf> = TypedColumnFamily<'cf, RawBytes, ValueBalance>; impl ZebraDb { // Column family convenience methods @@ -159,7 +162,8 @@ impl ZebraDb { let chain_value_pools_cf = self.chain_value_pools_cf(); chain_value_pools_cf - .zs_get(&()) + .zs_get(&RawBytes::new_raw_bytes(vec![1])) + .or_else(|| chain_value_pools_cf.zs_get(&RawBytes::new_raw_bytes(vec![]))) .unwrap_or_else(ValueBalance::zero) } } @@ -227,18 +231,19 @@ impl DiskWriteBatch { utxos_spent_by_block: HashMap, value_pool: ValueBalance, ) -> Result<(), BoxError> { - let _ = db + let mut value_balance = value_pool.add_chain_value_pool_change( + finalized + .block + .chain_value_pool_change(&utxos_spent_by_block, finalized.deferred_balance)?, + )?; + + let batch = db .chain_value_pools_cf() .with_batch_for_writing(self) - .zs_insert( - &(), - &value_pool.add_chain_value_pool_change( - finalized.block.chain_value_pool_change( - &utxos_spent_by_block, - finalized.deferred_balance, - )?, - )?, - ); + .zs_insert(&RawBytes::new_raw_bytes(vec![1]), &value_balance); + + value_balance.set_deferred_amount(Amount::zero()); + let _ = batch.zs_insert(&RawBytes::new_raw_bytes(vec![]), &value_balance); Ok(()) }