Skip to content

Commit

Permalink
add dry_run meta
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Nov 5, 2020
1 parent ab63b05 commit 056ab93
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

tests/_*
/*.move
execute.sh
execute.sh
test.sh
32 changes: 26 additions & 6 deletions executor/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::oracles::{oracle_coins_module, time_metadata};
use move_vm_runtime::logging::NoContextLog;
use crate::session::ConstsMap;

pub type SerializedTransactionEffects = Vec<((AccountAddress, StructTag), Option<Vec<u8>>)>;

#[derive(Debug, Default, Clone)]
pub struct FakeRemoteCache {
modules: HashMap<ModuleId, Vec<u8>>,
Expand Down Expand Up @@ -65,23 +67,36 @@ impl FakeRemoteCache {
.to_owned())
}

pub fn merge_transaction_effects(&mut self, effects: TransactionEffects) -> usize {
pub fn serialize_effects(
&self,
effects: TransactionEffects,
) -> (SerializedTransactionEffects, usize) {
let mut resources_write_size = 0;
let mut resources = vec![];
for (addr, changes) in effects.resources {
for (struct_tag, val) in changes {
match val {
Some((layout, val)) => {
let serialized = val.simple_serialize(&layout).expect("Valid value.");
resources_write_size += serialized.len();
self.resources.insert((addr, struct_tag), serialized);
resources.push(((addr, struct_tag), Some(serialized)));
}
None => {
self.resources.remove(&(addr, struct_tag));
resources.push(((addr, struct_tag), None));
}
}
}
}
resources_write_size
(resources, resources_write_size)
}

pub fn merge_effects(&mut self, serialized_effects: SerializedTransactionEffects) {
for ((addr, struct_tag), val) in serialized_effects {
match val {
Some(val) => self.resources.insert((addr, struct_tag), val),
None => self.resources.remove(&(addr, struct_tag)),
};
}
}
}

Expand Down Expand Up @@ -156,6 +171,7 @@ pub fn execute_script(
oracle_prices,
current_time,
aborts_with,
dry_run,
..
} = meta;
if !oracle_prices.is_empty() {
Expand Down Expand Up @@ -190,8 +206,12 @@ pub fn execute_script(
Ok(match res {
Ok(effects) => {
let mut explained = explain_effects(&effects, &ds)?;
let write_set_size = data_store.merge_transaction_effects(effects);
explained.set_write_set_size(write_set_size);
let (serialized_effects, effects_writeset_size) =
data_store.serialize_effects(effects);
explained.set_write_set_size(effects_writeset_size);
if !dry_run {
data_store.merge_effects(serialized_effects);
}
StepExecutionResult::Success(explained)
}
Err(vm_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 @@ -24,6 +24,7 @@ pub struct ExecutionMeta {
pub oracle_prices: Vec<(StructTag, u128)>,
pub current_time: Option<u64>,
pub aborts_with: Option<u64>,
pub dry_run: bool,
}

impl ExecutionMeta {
Expand Down Expand Up @@ -54,6 +55,7 @@ impl ExecutionMeta {
}
"current_time" => self.current_time = Some(val.parse().unwrap()),
"aborts_with" => self.aborts_with = Some(val.parse().unwrap()),
"dry_run" => self.dry_run = val.parse().unwrap(),
_ => eprintln!("Unimplemented meta key, {:?}", key),
}
}
Expand Down
47 changes: 47 additions & 0 deletions executor/tests/test_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,50 @@ fn test_extract_error_name_if_prefixed_with_err() {
"Execution aborted with code 101: ERR_RECORD_DOES_NOT_EXIST in module 0x2::Record."
);
}

#[test]
fn test_dry_run_do_not_apply_writeset_changes() {
let _pool = ConstPool::new();
let text = r"
script {
use 0x2::Record;
fun step_1(s: &signer) {
Record::create_record(s, 10);
}
}
/// dry_run: true
script {
use 0x2::Record;
fun step_2(s: &signer) {
Record::increment_record(s);
}
}
script {
use 0x2::Record;
fun step_3(s: &signer) {
Record::increment_record(s);
}
}
";

let effects = execute_script(
MoveFile::with_content(script_path(), text),
vec![stdlib_mod("signer.move"), modules_mod("record.move")],
"libra",
"0x3",
vec![],
)
.unwrap()
.last()
.unwrap()
.effects();
assert_eq!(
effects.resources()[0].changes[0].1,
ResourceChange("0x2::Record::T".to_string(), Some("[U8(11)]".to_string()))
);
}

0 comments on commit 056ab93

Please sign in to comment.