Ver código fonte

runtime/import/util: minor optimizations retrieving block info stuff

skoupidi 2 anos atrás
pai
commit
f9a58ca5ad
2 arquivos alterados com 21 adições e 10 exclusões
  1. 12 1
      src/blockchain/mod.rs
  2. 9 9
      src/runtime/import/util.rs

+ 12 - 1
src/blockchain/mod.rs

@@ -23,7 +23,7 @@ use sled::Transactional;
 
 use darkfi_serial::{deserialize, serialize, Decodable};
 
-use crate::{tx::Transaction, Error, Result};
+use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
 
 /// Block related definitions and storage implementations
 pub mod block_store;
@@ -411,6 +411,17 @@ impl BlockchainOverlay {
         Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
     }
 
+    /// Retrieve the last block height.
+    pub fn last_block_height(&self) -> Result<u64> {
+        Ok(self.last()?.0)
+    }
+
+    /// Retrieve the last block timestamp.
+    pub fn last_block_timestamp(&self) -> Result<Timestamp> {
+        let (_, hash) = self.last()?;
+        Ok(self.get_blocks_by_hash(&[hash])?[0].header.timestamp)
+    }
+
     /// Insert a given [`BlockInfo`] into the overlay.
     /// This functions wraps all the logic of separating the block into specific
     /// data that can be fed into the different trees of the overlay.

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

@@ -244,7 +244,7 @@ pub(crate) fn get_blockchain_time(mut ctx: FunctionEnvMut<Env>) -> i64 {
     let cid = &env.contract_id;
 
     // Grab current last block
-    let block = match env.blockchain.lock().unwrap().last_block() {
+    let timestamp = match env.blockchain.lock().unwrap().last_block_timestamp() {
         Ok(b) => b,
         Err(e) => {
             error!(
@@ -261,7 +261,7 @@ pub(crate) fn get_blockchain_time(mut ctx: FunctionEnvMut<Env>) -> i64 {
 
     // Create the return object
     let mut ret = Vec::with_capacity(8);
-    ret.extend_from_slice(&block.header.timestamp.inner().to_be_bytes());
+    ret.extend_from_slice(&timestamp.inner().to_be_bytes());
 
     // Copy Vec<u8> to the VM
     let mut objects = env.objects.borrow_mut();
@@ -285,19 +285,19 @@ pub(crate) fn get_last_block_height(mut ctx: FunctionEnvMut<Env>) -> i64 {
     // Enforce function ACL
     if let Err(e) = acl_allow(env, &[ContractSection::Exec]) {
         error!(
-            target: "runtime::db::get_last_block_info",
-            "[WASM] [{}] get_last_block_info(): Called in unauthorized section: {}", cid, e,
+            target: "runtime::db::get_last_block_height",
+            "[WASM] [{}] get_last_block_height(): Called in unauthorized section: {}", cid, e,
         );
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
 
-    // Grab current last block
-    let block = match env.blockchain.lock().unwrap().last_block() {
+    // Grab current last block height
+    let height = match env.blockchain.lock().unwrap().last_block_height() {
         Ok(b) => b,
         Err(e) => {
             error!(
-                target: "runtime::db::get_last_block_info",
-                "[WASM] [{}] get_last_block_info(): Internal error getting from blocks tree: {}", cid, e,
+                target: "runtime::db::get_last_block_height",
+                "[WASM] [{}] get_last_block_height(): Internal error getting from blocks tree: {}", cid, e,
             );
             return darkfi_sdk::error::DB_GET_FAILED
         }
@@ -309,7 +309,7 @@ pub(crate) fn get_last_block_height(mut ctx: FunctionEnvMut<Env>) -> i64 {
 
     // Create the return object
     let mut ret = Vec::with_capacity(8);
-    ret.extend_from_slice(&darkfi_serial::serialize(&block.header.height));
+    ret.extend_from_slice(&darkfi_serial::serialize(&height));
 
     // Copy Vec<u8> to the VM
     let mut objects = env.objects.borrow_mut();