mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
runtime/import/util: minor optimizations retrieving block info stuff
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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(×tamp.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();
|
||||
|
||||
Reference in New Issue
Block a user