Skip to content

Commit

Permalink
feat: new syscalls snapshot and revert
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Nov 1, 2022
1 parent dc3a216 commit 8e57571
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ pub struct SyscallCyclesConfig {
pub sys_get_block_hash_cycles: u64,
pub sys_recover_account_cycles: u64,
pub sys_log_cycles: u64,
pub sys_snapshot_cycles: u64,
pub sys_revert_cycles: u64,
}

impl SyscallCyclesConfig {
Expand All @@ -583,6 +585,8 @@ impl SyscallCyclesConfig {
sys_get_block_hash_cycles: 0,
sys_recover_account_cycles: 0,
sys_log_cycles: 0,
sys_snapshot_cycles: 0,
sys_revert_cycles: 0,
}
}
}
23 changes: 23 additions & 0 deletions crates/generator/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const SYS_GET_BLOCK_HASH: u64 = 3404;
const SYS_PAY_FEE: u64 = 3501;
const SYS_LOG: u64 = 3502;
const SYS_RECOVER_ACCOUNT: u64 = 3503;
/* Syscall state revert */
const SYS_SNAPSHOT: u64 = 3601;
const SYS_REVERT: u64 = 3602;
/* CKB compatible syscalls */
const DEBUG_PRINT_SYSCALL_NUMBER: u64 = 2177;

Expand Down Expand Up @@ -545,6 +548,24 @@ impl<'a, 'b, S: State + CodeStore + JournalDB, C: ChainView, Mac: SupportMachine
machine.set_register(A0, Mac::REG::from_u8(SUCCESS));
Ok(true)
}
SYS_SNAPSHOT => {
let snapshot_addr = machine.registers()[A0].clone();
// create snapshot
let snapshot_id = self.state.snapshot() as u32;
machine
.memory_mut()
.store32(&snapshot_addr, &Mac::REG::from_u32(snapshot_id))?;
machine.set_register(A0, Mac::REG::from_u8(SUCCESS));
Ok(true)
}
SYS_REVERT => {
let snapshot_id = machine.registers()[A0].to_u32();
self.state
.revert(snapshot_id as usize)
.map_err(|err| VMError::Unexpected(format!("revert: {}", err)))?;
machine.set_register(A0, Mac::REG::from_u8(SUCCESS));
Ok(true)
}
DEBUG_PRINT_SYSCALL_NUMBER => {
self.output_debug(machine)?;
Ok(true)
Expand Down Expand Up @@ -616,6 +637,8 @@ impl<'a, 'b, S: State, C: ChainView> L2Syscalls<'a, 'b, S, C> {
SYS_GET_BLOCK_HASH => cycles_config.sys_get_block_hash_cycles,
SYS_RECOVER_ACCOUNT => cycles_config.sys_recover_account_cycles,
SYS_LOG => cycles_config.sys_log_cycles,
SYS_SNAPSHOT => cycles_config.sys_snapshot_cycles,
SYS_REVERT => cycles_config.sys_revert_cycles,
_ => 0,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/generator/src/tests/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_init_genesis() {
let db = &store.begin_transaction();
// check init values
assert_ne!(db.get_block_smt_root().unwrap(), H256::zero());
let mut tree = BlockStateDB::from_store(db, RWConfig::readonly()).unwrap();
let tree = BlockStateDB::from_store(db, RWConfig::readonly()).unwrap();
assert!(tree.get_account_count().unwrap() > 0);

// check prev txs state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ async fn test_block_max_cycles_limit() {
sys_get_block_hash_cycles: MAX_CYCLES_LIMIT,
sys_recover_account_cycles: MAX_CYCLES_LIMIT,
sys_log_cycles: MAX_CYCLES_LIMIT,
sys_revert_cycles: MAX_CYCLES_LIMIT,
sys_snapshot_cycles: MAX_CYCLES_LIMIT,
},
..Default::default()
},
Expand Down

0 comments on commit 8e57571

Please sign in to comment.