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

Extract TransactionScriptsVerifier::map_vm_internal_error #4762

Merged
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
32 changes: 14 additions & 18 deletions script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,6 @@ where
} else {
Scheduler::new(tx_data, version, self.syscalls_generator.clone())
};
let map_vm_internal_error = |error: VMInternalError| match error {
VMInternalError::CyclesExceeded => ScriptError::ExceededMaximumCycles(max_cycles),
_ => ScriptError::VMInternalError(error),
};
let previous_cycles = scheduler.consumed_cycles();
let res = scheduler.run(RunMode::LimitCycles(max_cycles));
match res {
Expand All @@ -1068,10 +1064,12 @@ where
}
Err(error) => match error {
VMInternalError::CyclesExceeded | VMInternalError::Pause => {
let snapshot = scheduler.suspend().map_err(map_vm_internal_error)?;
let snapshot = scheduler
.suspend()
.map_err(|err| self.map_vm_internal_error(err, max_cycles))?;
Ok(ChunkState::suspended(snapshot))
}
_ => Err(map_vm_internal_error(error)),
_ => Err(self.map_vm_internal_error(error, max_cycles)),
},
}
}
Expand Down Expand Up @@ -1149,13 +1147,9 @@ where
max_cycles: Cycle,
) -> Result<(i8, Cycle), ScriptError> {
let mut scheduler = self.create_scheduler(script_group)?;
let map_vm_internal_error = |error: VMInternalError| match error {
VMInternalError::CyclesExceeded => ScriptError::ExceededMaximumCycles(max_cycles),
_ => ScriptError::VMInternalError(error),
};
scheduler
.run(RunMode::LimitCycles(max_cycles))
.map_err(map_vm_internal_error)
.map_err(|err| self.map_vm_internal_error(err, max_cycles))
}

fn run(&self, script_group: &ScriptGroup, max_cycles: Cycle) -> Result<Cycle, ScriptError> {
Expand All @@ -1168,6 +1162,14 @@ where
}
}

fn map_vm_internal_error(&self, error: VMInternalError, max_cycles: Cycle) -> ScriptError {
match error {
VMInternalError::CyclesExceeded => ScriptError::ExceededMaximumCycles(max_cycles),
VMInternalError::External(reason) if reason.eq("stopped") => ScriptError::Interrupts,
_ => ScriptError::VMInternalError(error),
}
}

#[cfg(not(target_family = "wasm"))]
async fn chunk_run_with_signal(
&self,
Expand All @@ -1184,12 +1186,6 @@ where
};
let version = self.select_version(&script_group.script)?;
let mut scheduler = Scheduler::new(tx_data, version, self.syscalls_generator.clone());
let map_vm_internal_error = |error: VMInternalError| match error {
VMInternalError::CyclesExceeded => ScriptError::ExceededMaximumCycles(max_cycles),
VMInternalError::External(reason) if reason.eq("stopped") => ScriptError::Interrupts,
_ => ScriptError::VMInternalError(error),
};

let mut pause = VMPause::new();
let child_pause = pause.clone();
let (finish_tx, mut finish_rx) = oneshot::channel::<Result<(i8, Cycle), ckb_vm::Error>>();
Expand Down Expand Up @@ -1264,7 +1260,7 @@ where
exit_code
))},
Err(err) => {
return Err(map_vm_internal_error(err));
return Err(self.map_vm_internal_error(err, max_cycles));
}
}

Expand Down
Loading