diff --git a/src/blockchain/mod.rs b/src/blockchain/mod.rs index 0f122c4bd..0a71e90eb 100644 --- a/src/blockchain/mod.rs +++ b/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 { + Ok(self.last()?.0) + } + + /// Retrieve the last block timestamp. + pub fn last_block_timestamp(&self) -> Result { + 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. diff --git a/src/runtime/import/util.rs b/src/runtime/import/util.rs index 82bbf25bf..610ae9aa8 100644 --- a/src/runtime/import/util.rs +++ b/src/runtime/import/util.rs @@ -244,7 +244,7 @@ pub(crate) fn get_blockchain_time(mut ctx: FunctionEnvMut) -> 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) -> 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(×tamp.inner().to_be_bytes()); // Copy Vec to the VM let mut objects = env.objects.borrow_mut(); @@ -285,19 +285,19 @@ pub(crate) fn get_last_block_height(mut ctx: FunctionEnvMut) -> 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) -> 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 to the VM let mut objects = env.objects.borrow_mut();