Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix current cycles syscall on chunk run with snapshot #3188

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 19 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,
wrapping_cycles_sub(used_cycles, current_group_used, &current_group)?,
&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 Expand Up @@ -1029,3 +1035,12 @@ fn wrapping_cycles_add(
lhs.checked_add(rhs)
.ok_or_else(|| ScriptError::CyclesOverflow(lhs, rhs).source(group))
}

fn wrapping_cycles_sub(
lhs: Cycle,
rhs: Cycle,
group: &ScriptGroup,
) -> Result<Cycle, TransactionScriptError> {
lhs.checked_sub(rhs)
.ok_or_else(|| ScriptError::CyclesOverflow(lhs, rhs).source(group))
}