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

node/state: Implement temporary dummy state function.

Luther Blissett 3 лет назад
Родитель
Сommit
4adb6d1c67
3 измененных файлов с 20 добавлено и 17 удалено
  1. 1 1
      bin/drk/src/deploy_contract.rs
  2. 1 15
      example/smart-contract/tests/runtime.rs
  3. 18 1
      src/node/state.rs

+ 1 - 1
bin/drk/src/deploy_contract.rs

@@ -128,7 +128,7 @@ pub fn create_deploy_data(path: &Path) -> Result<()> {
     eprintln!("Inspecting wasm binary in \"{}\"", CONTRACT_FILE_NAME);
     eprintln!("Inspecting wasm binary in \"{}\"", CONTRACT_FILE_NAME);
     let wasm_bytes = read(CONTRACT_FILE_NAME)?;
     let wasm_bytes = read(CONTRACT_FILE_NAME)?;
     eprintln!("Initializing moch wasm runtime to check validity");
     eprintln!("Initializing moch wasm runtime to check validity");
-    let runtime = match Runtime::new(&wasm_bytes) {
+    let runtime = match Runtime::new(&wasm_bytes, MemoryState::new(State::dummy()?)) {
         Ok(v) => {
         Ok(v) => {
             eprintln!("Found {} wasm binary", fg_green("valid"));
             eprintln!("Found {} wasm binary", fg_green("valid"));
             v
             v

+ 1 - 15
example/smart-contract/tests/runtime.rs

@@ -28,21 +28,7 @@ fn run_contract() -> Result<()> {
     // ============================================================
     // ============================================================
     // Build a ledger state so the runtime has something to work on
     // Build a ledger state so the runtime has something to work on
     // ============================================================
     // ============================================================
-    let sled_db = sled::Config::new().temporary(true).open()?;
-    let blockchain =
-        Blockchain::new(&sled_db, *TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES)?;
-
-    let merkle_tree = BridgeTree::<MerkleNode, 32>::new(100);
-
-    let state_machine = State {
-        tree: merkle_tree,
-        merkle_roots: blockchain.merkle_roots,
-        nullifiers: blockchain.nullifiers,
-        cashier_pubkeys: vec![],
-        faucet_pubkeys: vec![],
-        mint_vk: Lazy::new(),
-        burn_vk: Lazy::new(),
-    };
+    let state_machine = MemoryState::new(State::dummy())?;
 
 
     // We check if this nullifier is in the set from the contract
     // We check if this nullifier is in the set from the contract
     state_machine.nullifiers.insert(&[Nullifier::from(pallas::Base::from(0x10))])?;
     state_machine.nullifiers.insert(&[Nullifier::from(pallas::Base::from(0x10))])?;

+ 18 - 1
src/node/state.rs

@@ -3,7 +3,8 @@ use lazy_init::Lazy;
 use log::{debug, error};
 use log::{debug, error};
 
 
 use crate::{
 use crate::{
-    blockchain::{nfstore::NullifierStore, rootstore::RootStore},
+    blockchain::{nfstore::NullifierStore, rootstore::RootStore, Blockchain},
+    consensus::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
     crypto::{
     crypto::{
         coin::{Coin, OwnCoin},
         coin::{Coin, OwnCoin},
         constants::MERKLE_DEPTH,
         constants::MERKLE_DEPTH,
@@ -133,6 +134,22 @@ pub struct State {
 }
 }
 
 
 impl State {
 impl State {
+    /// Create a dummy state
+    pub fn dummy() -> Result<Self> {
+        let db = sled::Config::new().temporary(true).open()?;
+        let bc = Blockchain::new(&db, *TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES)?;
+        let mt = BridgeTree::<MerkleNode, 32>::new(100);
+        Ok(State {
+            tree: mt,
+            merkle_roots: bc.merkle_roots,
+            nullifiers: bc.nullifiers,
+            cashier_pubkeys: vec![],
+            faucet_pubkeys: vec![],
+            mint_vk: Lazy::new(),
+            burn_vk: Lazy::new(),
+        })
+    }
+
     /// Apply a [`StateUpdate`] to some state.
     /// Apply a [`StateUpdate`] to some state.
     pub async fn apply(
     pub async fn apply(
         &mut self,
         &mut self,