From d1474cdaa089282e53d4fae4443df54c3d51ff78 Mon Sep 17 00:00:00 2001 From: Maksim Kurnikov Date: Tue, 10 Nov 2020 13:57:35 +0300 Subject: [PATCH] add meta block: for block height --- executor/src/execution.rs | 8 ++++- executor/src/meta.rs | 2 ++ executor/src/oracles.rs | 11 +++++++ executor/tests/test_executor.rs | 53 ++++++++++++++++++++++++++++++ resources/assets/stdlib/block.move | 15 +++++++++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 resources/assets/stdlib/block.move diff --git a/executor/src/execution.rs b/executor/src/execution.rs index bba39194..e3fb5f68 100644 --- a/executor/src/execution.rs +++ b/executor/src/execution.rs @@ -19,7 +19,7 @@ use crate::explain::{ explain_type_error, }; use crate::meta::ExecutionMeta; -use crate::oracles::{oracle_coins_module, time_metadata, coin_balance_metadata}; +use crate::oracles::{oracle_coins_module, time_metadata, coin_balance_metadata, block_metadata}; use move_vm_runtime::logging::NoContextLog; use crate::session::ConstsMap; @@ -176,6 +176,7 @@ pub fn execute_script( current_time, aborts_with, status, + block, dry_run, } = meta; if !oracle_prices.is_empty() { @@ -194,6 +195,11 @@ pub fn execute_script( lcs::to_bytes(¤t_time).unwrap(), ); } + let block_height = block.unwrap_or(100); + ds.resources.insert( + (std_addr, block_metadata()), + lcs::to_bytes(&block_height).unwrap(), + ); for (price_tag, val) in oracle_prices { ds.resources .insert((std_addr, price_tag), lcs::to_bytes(&val).unwrap()); diff --git a/executor/src/meta.rs b/executor/src/meta.rs index 7f416c7d..659e394c 100644 --- a/executor/src/meta.rs +++ b/executor/src/meta.rs @@ -33,6 +33,7 @@ pub struct ExecutionMeta { pub accounts_balance: Vec<(AccountAddress, String, u128)>, pub oracle_prices: Vec<(StructTag, u128)>, pub current_time: Option, + pub block: Option, pub aborts_with: Option, pub status: Option, pub dry_run: bool, @@ -86,6 +87,7 @@ impl ExecutionMeta { eprintln!("Unknown status code name: {:?}", val); } } + "block" => self.block = Some(val.parse().unwrap()), "dry_run" => self.dry_run = val.parse().unwrap(), _ => eprintln!("Unimplemented meta key, {:?}", key), } diff --git a/executor/src/oracles.rs b/executor/src/oracles.rs index a6feabf6..c64156b2 100644 --- a/executor/src/oracles.rs +++ b/executor/src/oracles.rs @@ -11,6 +11,8 @@ const ACCOUNT_BALANCE_STRUCT: &str = "Balance"; const XFI_MODULE: &str = "XFI"; const XFI_RESOURCE: &str = "T"; +const BLOCK_RESOURCE: &str = "BlockMetadata"; + /// Currency price. #[derive(Debug, PartialEq, Eq)] pub struct Price { @@ -52,6 +54,15 @@ pub fn oracle_metadata(first: &str, second: &str) -> StructTag { } } +pub fn block_metadata() -> StructTag { + StructTag { + address: CORE_CODE_ADDRESS, + name: Identifier::new(BLOCK_RESOURCE).expect("Valid module name."), + module: Identifier::new("Block").expect("Valid module name."), + type_params: vec![], + } +} + pub fn time_metadata() -> StructTag { StructTag { address: CORE_CODE_ADDRESS, diff --git a/executor/tests/test_executor.rs b/executor/tests/test_executor.rs index bd0d006b..7e175c1d 100644 --- a/executor/tests/test_executor.rs +++ b/executor/tests/test_executor.rs @@ -1037,3 +1037,56 @@ script { "Expected error: Execution failed with an arithmetic error (i.e., integer overflow/underflow, div/mod by zero, or invalid shift) in script at code offset 2" ); } + +#[test] +fn test_set_block_height() { + let _pool = ConstPool::new(); + + let text = r" +/// block: 1024 +script { + use 0x1::Block; + + fun success() { + assert(Block::get_current_block_height() == 1024, 1); + } +} "; + + execute_script( + MoveFile::with_content(script_path(), text), + vec![stdlib_mod("block.move")], + "libra", + "0x3", + vec![], + ) + .unwrap() + .last() + .unwrap() + .effects(); +} + +#[test] +fn test_block_height_default_100() { + let _pool = ConstPool::new(); + + let text = r" +script { + use 0x1::Block; + + fun success() { + assert(Block::get_current_block_height() == 100, 1); + } +} "; + + execute_script( + MoveFile::with_content(script_path(), text), + vec![stdlib_mod("block.move")], + "libra", + "0x3", + vec![], + ) + .unwrap() + .last() + .unwrap() + .effects(); +} diff --git a/resources/assets/stdlib/block.move b/resources/assets/stdlib/block.move new file mode 100644 index 00000000..b4e47f20 --- /dev/null +++ b/resources/assets/stdlib/block.move @@ -0,0 +1,15 @@ +address 0x1 { + +module Block { + + resource struct BlockMetadata { + // height of the current block + height: u64, + } + + // Get the current block height + public fun get_current_block_height(): u64 acquires BlockMetadata { + borrow_global(0x1).height + } +} +}