Skip to content

Commit

Permalink
add meta block: for block height
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Nov 10, 2020
1 parent b46e958 commit d1474cd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
8 changes: 7 additions & 1 deletion executor/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -176,6 +176,7 @@ pub fn execute_script(
current_time,
aborts_with,
status,
block,
dry_run,
} = meta;
if !oracle_prices.is_empty() {
Expand All @@ -194,6 +195,11 @@ pub fn execute_script(
lcs::to_bytes(&current_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());
Expand Down
2 changes: 2 additions & 0 deletions executor/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct ExecutionMeta {
pub accounts_balance: Vec<(AccountAddress, String, u128)>,
pub oracle_prices: Vec<(StructTag, u128)>,
pub current_time: Option<u64>,
pub block: Option<u64>,
pub aborts_with: Option<u64>,
pub status: Option<u64>,
pub dry_run: bool,
Expand Down Expand Up @@ -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),
}
Expand Down
11 changes: 11 additions & 0 deletions executor/src/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
53 changes: 53 additions & 0 deletions executor/tests/test_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
15 changes: 15 additions & 0 deletions resources/assets/stdlib/block.move
Original file line number Diff line number Diff line change
@@ -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<BlockMetadata>(0x1).height
}
}
}

0 comments on commit d1474cd

Please sign in to comment.