Skip to content

Commit

Permalink
test(script): vm_version should be correct after resuming from snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Nov 17, 2021
1 parent d987413 commit bd24f17
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
72 changes: 72 additions & 0 deletions script/src/verify/tests/ckb_latest/features_since_v2021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,78 @@ fn check_vm_version() {
assert_eq!(result.is_ok(), script_version >= ScriptVersion::V1);
}

#[test]
fn check_vm_version_with_snapshot() {
let script_version = SCRIPT_VERSION;

let (vm_version_cell, vm_version_data_hash) =
load_cell_from_path("testdata/vm_version_with_snapshot");

let vm_version_script = Script::new_builder()
.hash_type(script_version.data_hash_type().into())
.code_hash(vm_version_data_hash)
.build();
let output = CellOutputBuilder::default()
.capacity(capacity_bytes!(100).pack())
.lock(vm_version_script)
.build();
let input = CellInput::new(OutPoint::null(), 0);

let transaction = TransactionBuilder::default().input(input).build();
let dummy_cell = create_dummy_cell(output);

let rtx = ResolvedTransaction {
transaction,
resolved_cell_deps: vec![vm_version_cell],
resolved_inputs: vec![dummy_cell],
resolved_dep_groups: vec![],
};

let verifier = TransactionScriptsVerifierWithEnv::new();

let max_cycles = Cycle::MAX;

let result = verifier.verify_map(script_version, &rtx, |mut verifier| {
verifier.set_skip_pause(true);
verifier.verify(max_cycles)
});
assert_eq!(result.is_ok(), script_version >= ScriptVersion::V1);

if script_version < ScriptVersion::V1 {
return;
}

let cycles_once = result.unwrap();
let mut cycles = 0;

verifier.verify_map(script_version, &rtx, |verifier| {
let mut init_snap: Option<TransactionSnapshot> = None;

if let VerifyResult::Suspended(state) = verifier.resumable_verify(max_cycles).unwrap() {
init_snap = Some(state.try_into().unwrap());
}

loop {
let snap = init_snap.take().unwrap();
match verifier.resume_from_snap(&snap, max_cycles).unwrap() {
VerifyResult::Suspended(state) => {
init_snap = Some(state.try_into().unwrap());
}
VerifyResult::Completed(cycle) => {
assert!(
verifier.tracing_data_as_code_pages.borrow().is_empty(),
"Any group execution is complete, this must be empty"
);
cycles = cycle;
break;
}
}
}
});

assert_eq!(cycles, cycles_once);
}

#[test]
fn check_exec_from_cell_data() {
let script_version = SCRIPT_VERSION;
Expand Down
2 changes: 2 additions & 0 deletions script/testdata/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ALL_BINS := jalr_zero \
mop_adc_lock \
current_cycles \
vm_version \
vm_version_with_snapshot \
exec_callee \
exec_caller_from_cell_data \
exec_caller_from_witness \
Expand Down Expand Up @@ -81,6 +82,7 @@ cpop_lock: cpop_lock.c
mop_adc_lock: mop_adc_lock.S
current_cycles: current_cycles.c
vm_version: vm_version.c
vm_version_with_snapshot: vm_version_with_snapshot.c
exec_callee: exec_callee.c
exec_caller_from_cell_data: exec_caller_from_cell_data.c
exec_caller_from_witness: exec_caller_from_witness.c
Expand Down
Binary file added script/testdata/vm_version_with_snapshot
Binary file not shown.
34 changes: 34 additions & 0 deletions script/testdata/vm_version_with_snapshot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "ckb_syscalls.h"

#ifdef DEBUG
#include <stdio.h>
#else
#define ckb_debug(...)
#define sprintf(...)
#endif

void try_pause() {
syscall(2178, 0, 0, 0, 0, 0, 0);
}

int vm_version() {
return syscall(2041, 0, 0, 0, 0, 0, 0);
}

int main() {
#ifdef DEBUG
char message[2048];
#endif
int ver;
for (int i=0; i<4096; i++) {
ver = vm_version();
sprintf(message, "version = %d", ver); ckb_debug(message);
if (i > 16) {
try_pause();
}
if (ver != 1) {
return -1;
}
}
return CKB_SUCCESS;
}

0 comments on commit bd24f17

Please sign in to comment.