Ver Fonte

blockchain/contract_store: minor fixes and cleanups

skoupidi há 1 ano atrás
pai
commit
15f35629e5

+ 3 - 2
bin/darkfid/src/tests/harness.rs

@@ -245,8 +245,9 @@ impl Harness {
             &mut MerkleTree::new(1),
         )
         .await?;
-        block.header.state_root =
-            overlay.lock().unwrap().contracts.get_state_monotree()?.get_headroot()?.unwrap();
+        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();
 
         // Attach signature
         block.sign(&keypair.secret);

+ 6 - 4
src/blockchain/contract_store.rs

@@ -294,7 +294,7 @@ impl ContractStore {
             if !state_pointers.contains(&state_monotree_ptr) {
                 return Err(Error::ContractStateNotFound)
             }
-            if !self.state_trees.contains_key(&state_monotree_ptr)? {
+            if !self.state_trees.contains_key(state_monotree_ptr)? {
                 return Err(Error::ContractStateNotFound)
             }
 
@@ -490,6 +490,8 @@ 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 generate 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<Monotree<MemoryDb>> {
@@ -604,7 +606,7 @@ impl ContractStoreOverlay {
 
                 // Remove dropped records
                 for key in &state_cache.state.removed {
-                    let key = blake3::hash(&key);
+                    let key = blake3::hash(key);
                     debug!(target: "blockchain::contractstore::update_state_monotree", "Removed key: {key}");
                     state_monotree_root =
                         state_monotree.remove(state_monotree_root.as_ref(), key.as_bytes())?;
@@ -612,8 +614,8 @@ impl ContractStoreOverlay {
 
                 // Update or insert new records
                 for (key, value) in &state_cache.state.cache {
-                    let key = blake3::hash(&key);
-                    let value = blake3::hash(&value);
+                    let key = blake3::hash(key);
+                    let value = blake3::hash(value);
                     debug!(target: "blockchain::contractstore::update_state_monotree", "Updating key {key} with value: {value}");
                     state_monotree_root = state_monotree.insert(
                         state_monotree_root.as_ref(),

+ 3 - 2
src/contract/test-harness/src/money_pow_reward.rs

@@ -145,8 +145,9 @@ impl TestHarness {
             &mut MerkleTree::new(1),
         )
         .await?;
-        block.header.state_root =
-            overlay.lock().unwrap().contracts.get_state_monotree()?.get_headroot()?.unwrap();
+        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();
 
         // Attach signature
         block.sign(&wallet.keypair.secret);