diff --git a/bin/darkfid/src/tests/harness.rs b/bin/darkfid/src/tests/harness.rs index 7f9101ed0..7226e4bcd 100644 --- a/bin/darkfid/src/tests/harness.rs +++ b/bin/darkfid/src/tests/harness.rs @@ -89,9 +89,8 @@ impl Harness { vks::inject(&sled_db, &vks)?; let overlay = BlockchainOverlay::new(&Blockchain::new(&sled_db)?)?; deploy_native_contracts(&overlay, config.pow_target).await?; - let mut state_monotree = overlay.lock().unwrap().contracts.get_state_monotree()?; - overlay.lock().unwrap().contracts.update_state_monotree(&mut state_monotree)?; - genesis_block.header.state_root = state_monotree.get_headroot()?.unwrap(); + genesis_block.header.state_root = + overlay.lock().unwrap().get_state_monotree()?.get_headroot()?.unwrap(); // Generate validators configuration // NOTE: we are not using consensus constants here so we @@ -246,9 +245,8 @@ impl Harness { &mut MerkleTree::new(1), ) .await?; - let mut monotree = overlay.lock().unwrap().contracts.get_state_monotree()?; - overlay.lock().unwrap().contracts.update_state_monotree(&mut monotree)?; - block.header.state_root = monotree.get_headroot()?.unwrap(); + block.header.state_root = + overlay.lock().unwrap().get_state_monotree()?.get_headroot()?.unwrap(); // Attach signature block.sign(&keypair.secret); diff --git a/bin/darkfid/src/tests/mod.rs b/bin/darkfid/src/tests/mod.rs index 1fef42e01..98e0c09e2 100644 --- a/bin/darkfid/src/tests/mod.rs +++ b/bin/darkfid/src/tests/mod.rs @@ -259,15 +259,14 @@ fn darkfid_programmatic_control() -> Result<()> { ) .unwrap(); darkfi::validator::utils::deploy_native_contracts(&overlay, 20).await.unwrap(); - let mut state_monotree = - overlay.lock().unwrap().contracts.get_state_monotree().unwrap(); - overlay + genesis_block.header.state_root = overlay .lock() .unwrap() - .contracts - .update_state_monotree(&mut state_monotree) + .get_state_monotree() + .unwrap() + .get_headroot() + .unwrap() .unwrap(); - genesis_block.header.state_root = state_monotree.get_headroot().unwrap().unwrap(); let bootstrap = genesis_block.header.timestamp.inner(); let config = darkfi::validator::ValidatorConfig { confirmation_threshold: 1, diff --git a/script/research/gg/src/main.rs b/script/research/gg/src/main.rs index 8dfe11838..f6e754017 100644 --- a/script/research/gg/src/main.rs +++ b/script/research/gg/src/main.rs @@ -170,8 +170,7 @@ fn main() -> Result<()> { genesis_block.header.transactions_root = tree.root(0).unwrap(); // Grab the updated contracts states root - let mut state_monotree = overlay.lock().unwrap().get_state_monotree()?; - overlay.lock().unwrap().contracts.update_state_monotree(&mut state_monotree)?; + let state_monotree = overlay.lock().unwrap().get_state_monotree()?; let Some(state_root) = state_monotree.get_headroot()? else { return Err(Error::ContractsStatesRootNotFoundError); }; diff --git a/src/blockchain/contract_store.rs b/src/blockchain/contract_store.rs index b970aced8..e6fdd83d6 100644 --- a/src/blockchain/contract_store.rs +++ b/src/blockchain/contract_store.rs @@ -490,8 +490,6 @@ impl ContractStoreOverlay { /// Generate a Monotree(SMT) containing all contracts states /// roots, along with the wasm bincodes monotree roots. /// Be carefull as this will open all states monotrees in the overlay. - /// If overlay holds changes the generated monotree must be updated - /// using update_state_monotree(). /// /// Note: native contracts zkas tree and wasm bincodes are excluded. pub fn get_state_monotree(&self) -> Result> { @@ -566,6 +564,10 @@ impl ContractStoreOverlay { &wasm_monotree_root, )?; tree.set_headroot(root.as_ref()); + drop(lock); + + // Update the monotree to the latest overlay changes + self.update_state_monotree(&mut tree)?; Ok(tree) } diff --git a/src/blockchain/mod.rs b/src/blockchain/mod.rs index 8872bbdf4..624730060 100644 --- a/src/blockchain/mod.rs +++ b/src/blockchain/mod.rs @@ -425,7 +425,7 @@ impl Blockchain { } /// Generate a Monotree(SMT) containing all contracts states - /// checksums, along with the wasm bincodes checksum. + /// roots, along with the wasm bincodes monotree root. /// /// Note: native contracts zkas tree and wasm bincodes are excluded. pub fn get_state_monotree(&self) -> Result> { @@ -693,9 +693,9 @@ impl BlockchainOverlay { } /// Generate a Monotree(SMT) containing all contracts states - /// checksums, along with the wasm bincodes checksum. + /// roots, along with the wasm bincodes monotree root. /// A clone is used so we are not affected by the opened trees - /// during checksum computing. + /// during roots computing. /// /// Note: native contracts zkas tree and wasm bincodes are excluded. pub fn get_state_monotree(&self) -> Result> { diff --git a/src/contract/test-harness/src/lib.rs b/src/contract/test-harness/src/lib.rs index c2a55dcf9..27b8816c8 100644 --- a/src/contract/test-harness/src/lib.rs +++ b/src/contract/test-harness/src/lib.rs @@ -276,9 +276,8 @@ impl TestHarness { vks::inject(&sled_db, &vks)?; let overlay = BlockchainOverlay::new(&Blockchain::new(&sled_db)?)?; deploy_native_contracts(&overlay, 90).await?; - let mut state_monotree = overlay.lock().unwrap().contracts.get_state_monotree()?; - overlay.lock().unwrap().contracts.update_state_monotree(&mut state_monotree)?; - genesis_block.header.state_root = state_monotree.get_headroot()?.unwrap(); + genesis_block.header.state_root = + overlay.lock().unwrap().get_state_monotree()?.get_headroot()?.unwrap(); // Create `Wallet` instances let mut holders_map = HashMap::new(); diff --git a/src/contract/test-harness/src/money_pow_reward.rs b/src/contract/test-harness/src/money_pow_reward.rs index 6ab5c94a5..8181efc54 100644 --- a/src/contract/test-harness/src/money_pow_reward.rs +++ b/src/contract/test-harness/src/money_pow_reward.rs @@ -145,9 +145,8 @@ impl TestHarness { &mut MerkleTree::new(1), ) .await?; - let mut monotree = overlay.lock().unwrap().contracts.get_state_monotree()?; - overlay.lock().unwrap().contracts.update_state_monotree(&mut monotree)?; - block.header.state_root = monotree.get_headroot()?.unwrap(); + block.header.state_root = + overlay.lock().unwrap().get_state_monotree()?.get_headroot()?.unwrap(); // Attach signature block.sign(&wallet.keypair.secret); diff --git a/src/validator/consensus.rs b/src/validator/consensus.rs index 1af78e0e4..60263eefb 100644 --- a/src/validator/consensus.rs +++ b/src/validator/consensus.rs @@ -644,13 +644,13 @@ impl Consensus { Ok(()) } - /// Auxiliary function to check current contracts states checksums + /// Auxiliary function to check current contracts states /// Monotree(SMT) validity in all active forks and canonical. pub async fn healthcheck(&self) -> Result<()> { // Grab a lock over current forks let lock = self.forks.read().await; - // Rebuild current canonical contract states checksums monotree + // Rebuild current canonical contract states monotree let state_monotree = self.blockchain.get_state_monotree()?; // Check that the root matches last block header state root @@ -709,7 +709,7 @@ pub struct Fork { pub overlay: BlockchainOverlayPtr, /// Current PoW module state pub module: PoWModule, - /// Current contracts states checksums Monotree(SMT) + /// Current contracts states Monotree(SMT) pub state_monotree: Monotree, /// Fork proposal hashes sequence pub proposals: Vec, @@ -727,7 +727,7 @@ impl Fork { pub async fn new(blockchain: Blockchain, module: PoWModule) -> Result { let mempool = blockchain.get_pending_txs()?.iter().map(|tx| tx.hash()).collect(); let overlay = BlockchainOverlay::new(&blockchain)?; - // Build current contract states checksums monotree + // Build current contract states monotree let state_monotree = overlay.lock().unwrap().get_state_monotree()?; // Retrieve last block difficulty to access current ranks let last_difficulty = blockchain.last_block_difficulty()?; @@ -927,22 +927,21 @@ impl Fork { }) } - /// Build current contract states checksums monotree. + /// Build current contract states monotree. pub fn compute_monotree(&mut self) -> Result<()> { self.state_monotree = self.overlay.lock().unwrap().get_state_monotree()?; Ok(()) } - /// Auxiliary function to check current contracts states checksums + /// Auxiliary function to check current contracts states /// Monotree(SMT) validity. /// /// Note: This should be executed on fresh forks and/or when /// a fork doesn't contain changes over the last appended // proposal. pub fn healthcheck(&self) -> Result<()> { - // Rebuild current contract states checksums monotree - let mut state_monotree = self.overlay.lock().unwrap().get_state_monotree()?; - self.overlay.lock().unwrap().contracts.update_state_monotree(&mut state_monotree)?; + // Rebuild current contract states monotree + let state_monotree = self.overlay.lock().unwrap().get_state_monotree()?; // Check that it matches forks' tree let Some(state_root) = state_monotree.get_headroot()? else { diff --git a/src/validator/verification.rs b/src/validator/verification.rs index 1dd7ffec9..15ab2b56a 100644 --- a/src/validator/verification.rs +++ b/src/validator/verification.rs @@ -116,8 +116,7 @@ pub async fn verify_genesis_block( } // Verify header contracts states root - let mut state_monotree = overlay.lock().unwrap().get_state_monotree()?; - overlay.lock().unwrap().contracts.update_state_monotree(&mut state_monotree)?; + let state_monotree = overlay.lock().unwrap().contracts.get_state_monotree()?; let Some(state_root) = state_monotree.get_headroot()? else { return Err(Error::ContractsStatesRootNotFoundError); };