Skip to content

Commit

Permalink
fix: revert non polyjuice system logs when transaction failed
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed May 23, 2022
1 parent e603035 commit 6b7789a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
9 changes: 9 additions & 0 deletions crates/generator/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use ckb_vm::{DefaultMachineBuilder, SupportMachine};

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

pub struct ApplyBlockArgs {
Expand Down Expand Up @@ -641,6 +642,7 @@ impl Generator {
}
} else {
// revert tx
let last_run_result_log = run_result.write.logs.pop();
run_result.revert_write();

// sender address
Expand Down Expand Up @@ -683,6 +685,12 @@ impl Generator {
TypedRawTransaction::Meta(tx) => tx.consumed(),
TypedRawTransaction::SimpleUDT(tx) => tx.consumed(),
TypedRawTransaction::Polyjuice(ref tx) => {
// push polyjuice system log back to run_result
if let Some(log) = last_run_result_log
.filter(|log| log.service_flag() == GW_LOG_POLYJUICE_SYSTEM.into())
{
run_result.write.logs.push(log);
}
let args = tx.extract_tx_args().ok_or(TransactionError::NoCost)?;
let gas_used = match read_polyjuice_gas_used(&run_result) {
Some(gas_used) => gas_used,
Expand Down Expand Up @@ -769,6 +777,7 @@ fn build_challenge_target(
fn read_polyjuice_gas_used(run_result: &RunResult) -> Option<u64> {
// read polyjuice system log
match run_result
.write
.logs
.iter()
.find(|item| u8::from(item.service_flag()) == gw_utils::script_log::GW_LOG_POLYJUICE_SYSTEM)
Expand Down
2 changes: 1 addition & 1 deletion crates/generator/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl<'a, S: State, C: ChainView, Mac: SupportMachine> Syscalls<Mac> for L2Syscal
let data_addr = machine.registers()[A3].to_u64();

let data = load_bytes(machine, data_addr, data_len as usize)?;
self.result.logs.push(
self.result.write.logs.push(
LogItem::new_builder()
.account_id(account_id.pack())
.service_flag(service_flag.into())
Expand Down
4 changes: 2 additions & 2 deletions crates/jsonrpc-types/src/godwoken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,11 +1169,11 @@ pub struct RunResult {
impl From<offchain::RunResult> for RunResult {
fn from(data: offchain::RunResult) -> RunResult {
let offchain::RunResult {
return_data, logs, ..
return_data, write, ..
} = data;
RunResult {
return_data: JsonBytes::from_bytes(return_data),
logs: logs.into_iter().map(Into::into).collect(),
logs: write.logs.into_iter().map(Into::into).collect(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc-server/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ async fn execute_l2transaction(
tx_hash: tx_hash.into(),
block_number: number,
return_data: run_result.return_data,
last_log: run_result.logs.pop(),
last_log: run_result.write.logs.pop(),
exit_code: run_result.exit_code,
};

Expand Down Expand Up @@ -904,7 +904,7 @@ async fn execute_raw_l2transaction(
tx_hash,
block_number,
return_data: run_result.return_data,
last_log: run_result.logs.pop(),
last_log: run_result.write.logs.pop(),
exit_code: run_result.exit_code,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/types/src/offchain/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl TxReceipt {
.collect::<Vec<_>>()
.pack(),
)
.logs(run_result.logs.pack())
.logs(run_result.write.logs.pack())
.build()
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/types/src/offchain/run_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct RunResultWriteState {
pub account_count: Option<u32>,
pub new_scripts: HashMap<H256, Script>,
pub write_data: HashMap<H256, Bytes>,
// log data
pub logs: Vec<LogItem>,
}

#[derive(Debug, Clone, Default)]
Expand All @@ -26,8 +28,6 @@ pub struct RunResult {
pub get_scripts: HashMap<H256, Script>,
// data hash -> data full size
pub read_data: HashMap<H256, Bytes>,
// log data
pub logs: Vec<LogItem>,
// used cycles
pub used_cycles: u64,
pub exit_code: i8,
Expand Down

0 comments on commit 6b7789a

Please sign in to comment.