Skip to content

Commit

Permalink
Merge pull request #124 from dfinance/current-time
Browse files Browse the repository at this point in the history
allow to specify current time
  • Loading branch information
mkurnikov authored Oct 23, 2020
2 parents 925d610 + 2bf3dc7 commit 1e2992b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
14 changes: 11 additions & 3 deletions executor/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use vm::file_format::{CompiledScript, FunctionDefinitionIndex};

use crate::explain::{explain_effects, explain_error, StepExecutionResult};
use crate::meta::ExecutionMeta;
use crate::oracles::oracle_coins_module;
use crate::oracles::{oracle_coins_module, time_metadata};

#[derive(Debug, Default, Clone)]
pub struct FakeRemoteCache {
Expand Down Expand Up @@ -144,6 +144,7 @@ pub fn execute_script(
let ExecutionMeta {
signers,
oracle_prices,
current_time,
..
} = meta;
if !oracle_prices.is_empty() {
Expand All @@ -154,10 +155,17 @@ pub fn execute_script(
));
}
}
let std_addr = AccountAddress::from_hex_literal("0x1").expect("Standart address");

if let Some(current_time) = current_time {
ds.resources.insert(
(std_addr, time_metadata()),
lcs::to_bytes(&current_time).unwrap(),
);
}
for (price_tag, val) in oracle_prices {
let addr = AccountAddress::from_hex_literal("0x1").expect("Standart address");
ds.resources
.insert((addr, price_tag), lcs::to_bytes(&val).unwrap());
.insert((std_addr, price_tag), lcs::to_bytes(&val).unwrap());
}

let res = execute_script_with_runtime_session(
Expand Down
23 changes: 17 additions & 6 deletions executor/src/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ pub fn explain_effects(
// pub const ERROR_DESCRIPTIONS: &[u8] = include_bytes!("./error_descriptions/error_descriptions.errmap");

fn explain_type_error(
text_representation: &mut String,
script: &CompiledScript,
signers: &[AccountAddress],
txn_args: &[TransactionArgument],
) {
use vm::file_format::SignatureToken::*;

let script_params = script.signature_at(script.as_inner().parameters);
let expected_num_signers = script_params
.0
Expand All @@ -191,30 +193,37 @@ fn explain_type_error(
})
.count();
if expected_num_signers != signers.len() {
println!(
writeln!(
text_representation,
"Execution failed with incorrect number of signers: script expected {:?}, but found \
{:?}",
expected_num_signers,
signers.len()
);
)
.unwrap();
return;
}

// TODO: printing type(s) of missing arguments could be useful
let expected_num_args = script_params.len() - signers.len();
if expected_num_args != txn_args.len() {
println!(
writeln!(
text_representation,
"Execution failed with incorrect number of arguments: script expected {:?}, but found \
{:?}",
expected_num_args,
txn_args.len()
);
).unwrap();
return;
}

// TODO: print more helpful error message pinpointing the (argument, type)
// pair that didn't match
println!("Execution failed with type error when binding type arguments to type parameters")
writeln!(
text_representation,
"Execution failed with type error when binding type arguments to type parameters"
)
.unwrap();
}

/// Explain an execution error
Expand Down Expand Up @@ -282,7 +291,9 @@ pub fn explain_error(
)
.unwrap();
}
VMStatus::Error(StatusCode::TYPE_MISMATCH) => explain_type_error(script, signers, &[]),
VMStatus::Error(StatusCode::TYPE_MISMATCH) => {
explain_type_error(&mut text_representation, script, signers, &[])
}
VMStatus::Error(status_code) => write!(
&mut text_representation,
"Execution failed with unexpected error {:?}",
Expand Down
2 changes: 2 additions & 0 deletions executor/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ExecutionMeta {
pub signers: Vec<AccountAddress>,
pub max_gas: u64,
pub oracle_prices: Vec<(StructTag, u128)>,
pub current_time: Option<u64>,
}

impl ExecutionMeta {
Expand Down Expand Up @@ -45,6 +46,7 @@ impl ExecutionMeta {
self.oracle_prices
.push((price_struct_tag, value.parse().unwrap()))
}
"current_time" => self.current_time = Some(val.parse().unwrap()),
_ => eprintln!("Unimplemented meta key, {:?}", key),
}
}
Expand Down
9 changes: 9 additions & 0 deletions executor/src/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ pub fn oracle_metadata(first: &str, second: &str) -> StructTag {
type_params: vec![currency_type(first), currency_type(second)],
}
}

pub fn time_metadata() -> StructTag {
StructTag {
address: CORE_CODE_ADDRESS,
name: Identifier::new("CurrentTimestamp").expect("Valid module name."),
module: Identifier::new("Time").expect("Valid module name."),
type_params: vec![],
}
}

0 comments on commit 1e2992b

Please sign in to comment.