Skip to content

Commit

Permalink
fix: fix current cycles syscall on chunk run with snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Nov 18, 2021
1 parent 2d1ebfc commit 615b899
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
15 changes: 8 additions & 7 deletions script/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub struct TransactionSnapshot {
/// remain script groups to verify
pub remain: Vec<(ScriptGroupType, Byte32)>,
/// vm snapshot
pub snap: Option<Snapshot>,
pub snap: Option<(Snapshot, Cycle)>,
/// current consumed cycle
pub current_cycles: Cycle,
/// limit cycles when snapshot create
Expand Down Expand Up @@ -300,12 +300,13 @@ impl TryFrom<TransactionState<'_>> for TransactionSnapshot {
}
}
(
Some(make_snapshot(&mut vm.machine.machine).map_err(|e| {
ScriptError::VMInternalError(format!("{:?}", e)).unknown_source()
})?),
current_cycles.checked_add(vm_cycles).ok_or_else(|| {
ScriptError::CyclesOverflow(current_cycles, vm_cycles).unknown_source()
})?,
Some((
make_snapshot(&mut vm.machine.machine).map_err(|e| {
ScriptError::VMInternalError(format!("{:?}", e)).unknown_source()
})?,
vm_cycles,
)),
current_cycles,
)
} else {
(None, current_cycles)
Expand Down
14 changes: 10 additions & 4 deletions script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ impl<'a, DL: CellDataProvider + HeaderProvider> TransactionScriptsVerifier<'a, D
snap: &TransactionSnapshot,
limit_cycles: Cycle,
) -> Result<VerifyResult, Error> {
let current_group_used = snap.snap.as_ref().map(|s| s.1).unwrap_or_default();
let mut cycles = snap.current_cycles;
let mut current_used = 0;

Expand All @@ -569,7 +570,11 @@ impl<'a, DL: CellDataProvider + HeaderProvider> TransactionScriptsVerifier<'a, D
// continue snapshot current script
match self.verify_group_with_chunk(&current_group, limit_cycles, &snap.snap) {
Ok(ChunkState::Completed(used_cycles)) => {
current_used = wrapping_cycles_add(current_used, used_cycles, &current_group)?;
current_used = wrapping_cycles_add(
current_used,
used_cycles - current_group_used,
&current_group,
)?;
cycles = wrapping_cycles_add(cycles, used_cycles, &current_group)?;
}
Ok(ChunkState::Suspended(vm)) => {
Expand Down Expand Up @@ -849,7 +854,7 @@ impl<'a, DL: CellDataProvider + HeaderProvider> TransactionScriptsVerifier<'a, D
&'a self,
group: &'a ScriptGroup,
max_cycles: Cycle,
snap: &Option<Snapshot>,
snap: &Option<(Snapshot, Cycle)>,
) -> Result<ChunkState<'a>, ScriptError> {
if group.script.code_hash() == TYPE_ID_CODE_HASH.pack()
&& Into::<u8>::into(group.script.hash_type()) == Into::<u8>::into(ScriptHashType::Type)
Expand Down Expand Up @@ -975,7 +980,7 @@ impl<'a, DL: CellDataProvider + HeaderProvider> TransactionScriptsVerifier<'a, D
&'a self,
script_group: &'a ScriptGroup,
max_cycles: Cycle,
snap: &Option<Snapshot>,
snap: &Option<(Snapshot, Cycle)>,
) -> Result<ChunkState<'a>, ScriptError> {
let mut machine = self.build_machine(script_group, max_cycles)?;

Expand All @@ -985,8 +990,9 @@ impl<'a, DL: CellDataProvider + HeaderProvider> TransactionScriptsVerifier<'a, D
};

// we should not capture snapshot if load program failed by exceeded cycles
if let Some(sp) = snap {
if let Some((sp, current_cycle)) = snap {
resume(&mut machine.machine, sp).map_err(map_vm_internal_error)?;
machine.machine.set_cycles(*current_cycle)
} else {
let program = self.extract_script(&script_group.script)?;
let bytes = machine
Expand Down

0 comments on commit 615b899

Please sign in to comment.