Просмотр исходного кода

blockchain/contract_store: added auxilliary fn get_all() to WasmStore and ContractStateStore

skoupidi 2 лет назад
Родитель
Сommit
74ed38a7e6
1 измененных файлов с 29 добавлено и 1 удалено
  1. 29 1
      src/blockchain/contract_store.rs

+ 29 - 1
src/blockchain/contract_store.rs

@@ -28,7 +28,7 @@ use crate::{
     Error, Result,
 };
 
-use super::SledDbOverlayPtr;
+use super::{parse_record, SledDbOverlayPtr};
 
 const SLED_CONTRACTS_TREE: &[u8] = b"_contracts";
 const SLED_BINCODE_TREE: &[u8] = b"_wasm_bincode";
@@ -62,6 +62,21 @@ impl WasmStore {
 
         Err(Error::WasmBincodeNotFound)
     }
+
+    /// Retrieve all wasm bincodes from the `WasmStore` in the form of a tuple
+    /// (`contract_id`, `bincode`).
+    /// Be careful as this will try to load everything in memory.
+    pub fn get_all(&self) -> Result<Vec<(ContractId, Vec<u8>)>> {
+        let mut bincodes = vec![];
+
+        for bincode in self.0.iter() {
+            let bincode = bincode.unwrap();
+            let contract_id = deserialize(&bincode.0)?;
+            bincodes.push((contract_id, bincode.1.to_vec()));
+        }
+
+        Ok(bincodes)
+    }
 }
 
 /// Overlay structure over a [`WasmStore`] instance.
@@ -221,6 +236,19 @@ impl ContractStateStore {
 
         Ok((zkbin, vk))
     }
+
+    /// Retrieve all contract states from the `ContractStateStore` in the form of a tuple
+    /// (`contract_id`, `state_hashes`).
+    /// Be careful as this will try to load everything in memory.
+    pub fn get_all(&self) -> Result<Vec<(ContractId, Vec<blake3::Hash>)>> {
+        let mut contracts = vec![];
+
+        for contract in self.0.iter() {
+            contracts.push(parse_record(contract.unwrap())?);
+        }
+
+        Ok(contracts)
+    }
 }
 
 /// Overlay structure over a [`ContractStateStore`] instance.