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: handle VM InvalidCycles by generate a polyjuice system log #729

Merged
merged 2 commits into from
Jun 21, 2022
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
34 changes: 30 additions & 4 deletions crates/generator/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use ckb_vm::{DefaultMachineBuilder, SupportMachine};

#[cfg(not(has_asm))]
use ckb_vm::TraceMachine;
use gw_utils::script_log::GW_LOG_POLYJUICE_SYSTEM;
use gw_utils::script_log::{generate_polyjuice_system_log, GW_LOG_POLYJUICE_SYSTEM};
use tracing::instrument;

pub struct ApplyBlockArgs {
Expand Down Expand Up @@ -127,10 +127,11 @@ impl Generator {
max_cycles: u64,
backend: Backend,
) -> Result<RunResult, TransactionError> {
const INVALID_CYCLES_EXIT_CODE: i8 = -1;

let mut run_result = RunResult::default();
let used_cycles;
let exit_code;

{
let t = Instant::now();
let global_vm_version = GLOBAL_VM_VERSION.load(SeqCst);
Expand Down Expand Up @@ -170,8 +171,20 @@ impl Generator {
let mut machine = TraceMachine::new(default_machine);

machine.load_program(&backend.generator, &[])?;
exit_code = machine.run()?;
used_cycles = machine.machine.cycles();
match machine.run() {
Ok(_exit_code) => {
Flouse marked this conversation as resolved.
Show resolved Hide resolved
exit_code = _exit_code;
used_cycles = machine.machine.cycles();
}
Err(ckb_vm::error::Error::InvalidCycles) => {
exit_code = INVALID_CYCLES_EXIT_CODE;
used_cycles = max_cycles;
}
Err(err) => {
// unexpected VM error
return Err(err.into());
}
}
log::debug!(
"[execute tx] VM machine_run time: {}ms, exit code: {} used_cycles: {}",
t.elapsed().as_millis(),
Expand Down Expand Up @@ -688,6 +701,19 @@ impl Generator {
.filter(|log| log.service_flag() == GW_LOG_POLYJUICE_SYSTEM.into())
{
run_result.write.logs.push(log);
} else {
// generate a system log for polyjuice tx
let polyjuice_tx =
crate::typed_transaction::types::PolyjuiceTx::new(raw_tx.to_owned());
let p = polyjuice_tx.parser().ok_or(TransactionError::NoCost)?;
let gas = p.gas();
run_result.write.logs.push(generate_polyjuice_system_log(
raw_tx.to_id().unpack(),
gas,
gas,
Default::default(),
0,
));
}
let parser = tx.parser().ok_or(TransactionError::NoCost)?;
let gas_used = match read_polyjuice_gas_used(&run_result) {
Expand Down
3 changes: 3 additions & 0 deletions crates/generator/src/typed_transaction/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ impl SimpleUDTTx {

pub struct PolyjuiceTx(RawL2Transaction);
impl PolyjuiceTx {
pub fn new(raw_tx: RawL2Transaction) -> Self {
Self(raw_tx)
}
pub fn parser(&self) -> Option<PolyjuiceParser> {
PolyjuiceParser::from_raw_l2_tx(&self.0)
}
Expand Down
20 changes: 20 additions & 0 deletions crates/utils/src/script_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,23 @@ pub fn parse_log(item: &LogItem) -> Result<GwLog> {
_ => Err(anyhow!("invalid log service flag: {}", service_flag)),
}
}

pub fn generate_polyjuice_system_log(
account_id: u32,
gas_used: u64,
cumulative_gas_used: u64,
created_address: [u8; 20],
status_code: u32,
) -> LogItem {
let service_flag: u8 = GW_LOG_POLYJUICE_SYSTEM;
let mut data = [0u8; 40];
data[0..8].copy_from_slice(&gas_used.to_le_bytes());
data[8..16].copy_from_slice(&cumulative_gas_used.to_le_bytes());
data[16..36].copy_from_slice(&created_address);
data[36..40].copy_from_slice(&status_code.to_le_bytes());
LogItem::new_builder()
.account_id(account_id.pack())
.service_flag(service_flag.into())
.data(data.pack())
.build()
}