Skip to content

Commit

Permalink
Extract TransactionScriptsVerifier::map_vm_internal_error
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Dec 26, 2024
1 parent 62af260 commit c02a7ce
Showing 1 changed file with 14 additions and 18 deletions.
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

0 comments on commit c02a7ce

Please sign in to comment.