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

runtime: replaced timekeeper with verifying block height directly

skoupidi 2 лет назад
Родитель
Сommit
3355575721

+ 2 - 2
src/runtime/import/util.rs

@@ -225,7 +225,7 @@ pub(crate) fn get_verifying_block_height(mut ctx: FunctionEnvMut<Env>) -> u64 {
     // u64 is 8 bytes.
     env.subtract_gas(&mut store, 8);
 
-    env.time_keeper.verifying_block_height
+    env.verifying_block_height
 }
 
 /// Will return current runtime configured verifying block height epoch number
@@ -236,7 +236,7 @@ pub(crate) fn get_verifying_block_height_epoch(mut ctx: FunctionEnvMut<Env>) ->
     // u64 is 8 bytes.
     env.subtract_gas(&mut store, 8);
 
-    darkfi_sdk::blockchain::block_epoch(env.time_keeper.verifying_block_height)
+    darkfi_sdk::blockchain::block_epoch(env.verifying_block_height)
 }
 
 /// Will return current blockchain timestamp,

+ 4 - 5
src/runtime/vm_runtime.rs

@@ -37,7 +37,6 @@ use wasmer_middlewares::{
 use super::{import, import::db::DbHandle, memory::MemoryManipulation};
 use crate::{
     blockchain::{contract_store::SMART_CONTRACT_ZKAS_DB_NAME, BlockchainOverlayPtr},
-    util::time::TimeKeeper,
     Error, Result,
 };
 
@@ -95,8 +94,8 @@ pub struct Env {
     pub memory: Option<Memory>,
     /// Object store for transferring memory from the host to VM
     pub objects: RefCell<Vec<Vec<u8>>>,
-    /// Helper structure to calculate time related operations
-    pub time_keeper: TimeKeeper,
+    /// Block height number runtime verifys against
+    pub verifying_block_height: u64,
     /// Parent `Instance`
     pub instance: Option<Arc<Instance>>,
 }
@@ -151,7 +150,7 @@ impl Runtime {
         wasm_bytes: &[u8],
         blockchain: BlockchainOverlayPtr,
         contract_id: ContractId,
-        time_keeper: TimeKeeper,
+        verifying_block_height: u64,
     ) -> Result<Self> {
         info!(target: "runtime::vm_runtime", "[WASM] Instantiating a new runtime");
         // This function will be called for each `Operator` encountered during
@@ -192,7 +191,7 @@ impl Runtime {
                 logs,
                 memory: None,
                 objects: RefCell::new(vec![]),
-                time_keeper,
+                verifying_block_height,
                 instance: None,
             },
         );

+ 8 - 2
src/validator/mod.rs

@@ -148,7 +148,12 @@ impl Validator {
         let overlay = BlockchainOverlay::new(&blockchain)?;
 
         // Deploy native wasm contracts
-        deploy_native_contracts(&overlay, &config.time_keeper, &config.faucet_pubkeys).await?;
+        deploy_native_contracts(
+            &overlay,
+            config.time_keeper.verifying_block_height,
+            &config.faucet_pubkeys,
+        )
+        .await?;
 
         // Add genesis block if blockchain is empty
         if blockchain.genesis().is_err() {
@@ -598,7 +603,8 @@ impl Validator {
         let mut module = PoWModule::new(blockchain.clone(), pow_target, pow_fixed_difficulty)?;
 
         // Deploy native wasm contracts
-        deploy_native_contracts(&overlay, &time_keeper, &faucet_pubkeys).await?;
+        deploy_native_contracts(&overlay, time_keeper.verifying_block_height, &faucet_pubkeys)
+            .await?;
 
         // Validate genesis block
         verify_genesis_block(&overlay, &time_keeper, previous, genesis_txs_total).await?;

+ 2 - 3
src/validator/utils.rs

@@ -32,7 +32,6 @@ use crate::{
     error::TxVerifyFailed,
     runtime::vm_runtime::Runtime,
     tx::Transaction,
-    util::time::TimeKeeper,
     validator::consensus::{Fork, Proposal},
     Error, Result,
 };
@@ -49,7 +48,7 @@ use crate::{
 /// the actual contract, so make sure the native contracts handle this well.
 pub async fn deploy_native_contracts(
     overlay: &BlockchainOverlayPtr,
-    time_keeper: &TimeKeeper,
+    verifying_block_height: u64,
     faucet_pubkeys: &Vec<PublicKey>,
 ) -> Result<()> {
     info!(target: "validator::utils::deploy_native_contracts", "Deploying native WASM contracts");
@@ -88,7 +87,7 @@ pub async fn deploy_native_contracts(
     for nc in native_contracts {
         info!(target: "validator::utils::deploy_native_contracts", "Deploying {} with ContractID {}", nc.0, nc.1);
 
-        let mut runtime = Runtime::new(&nc.2[..], overlay.clone(), nc.1, time_keeper.clone())?;
+        let mut runtime = Runtime::new(&nc.2[..], overlay.clone(), nc.1, verifying_block_height)?;
 
         runtime.deploy(&nc.3)?;
 

+ 13 - 5
src/validator/verification.rs

@@ -247,8 +247,12 @@ pub async fn verify_producer_transaction(
     debug!(target: "validator::verification::verify_producer_transaction", "Instantiating WASM runtime");
     let wasm = overlay.lock().unwrap().wasm_bincode.get(call.data.contract_id)?;
 
-    let mut runtime =
-        Runtime::new(&wasm, overlay.clone(), call.data.contract_id, time_keeper.clone())?;
+    let mut runtime = Runtime::new(
+        &wasm,
+        overlay.clone(),
+        call.data.contract_id,
+        time_keeper.verifying_block_height,
+    )?;
 
     debug!(target: "validator::verification::verify_producer_transaction", "Executing \"metadata\" call");
     let metadata = runtime.metadata(&payload)?;
@@ -404,8 +408,12 @@ pub async fn verify_transaction(
         debug!(target: "validator::verification::verify_transaction", "Instantiating WASM runtime");
         let wasm = overlay.lock().unwrap().wasm_bincode.get(call.data.contract_id)?;
 
-        let mut runtime =
-            Runtime::new(&wasm, overlay.clone(), call.data.contract_id, time_keeper.clone())?;
+        let mut runtime = Runtime::new(
+            &wasm,
+            overlay.clone(),
+            call.data.contract_id,
+            time_keeper.verifying_block_height,
+        )?;
 
         debug!(target: "validator::verification::verify_transaction", "Executing \"metadata\" call");
         let metadata = runtime.metadata(&payload)?;
@@ -477,7 +485,7 @@ pub async fn verify_transaction(
                 &deploy_params.wasm_bincode,
                 overlay.clone(),
                 deploy_cid,
-                time_keeper.clone(),
+                time_keeper.verifying_block_height,
             )?;
 
             deploy_runtime.deploy(&deploy_params.ix)?;