Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 34 additions & 31 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ pub struct ExecStep {
/// overflow", this value will **not** be the actual Gas cost of the
/// step.
pub gas_cost: GasCost,
/// Accumulated gas refund
pub gas_refund: Gas,
/// Call index within the [`Transaction`]
pub call_index: usize,
/// The global counter when this step was executed.
Expand Down Expand Up @@ -204,6 +206,7 @@ impl ExecStep {
memory_size: step.memory.0.len(),
gas_left: step.gas,
gas_cost: step.gas_cost,
gas_refund: Gas(0),
call_index,
rwc,
reversible_write_counter,
Expand All @@ -223,6 +226,7 @@ impl Default for ExecStep {
memory_size: 0,
gas_left: Gas(0),
gas_cost: GasCost(0),
gas_refund: Gas(0),
call_index: 0,
rwc: RWCounter(0),
reversible_write_counter: 0,
Expand Down Expand Up @@ -805,6 +809,9 @@ impl<'a> CircuitInputStateRef<'a> {
rw: RW,
op: T,
) -> Result<(), Error> {
if matches!(rw, RW::WRITE) {
self.apply_op(&op.clone().into_enum());
}
let op_ref = self.block.container.insert(Operation::new_reversible(
self.block_ctx.rwc.inc_pre(),
rw,
Expand Down Expand Up @@ -1076,38 +1083,29 @@ impl<'a> CircuitInputStateRef<'a> {
}
}

/// Apply reverted op to state and push to container.
fn apply_reverted_op(&mut self, op: OpEnum) -> OperationRef {
match op {
/// Apply op to state.
fn apply_op(&mut self, op: &OpEnum) {
match &op {
OpEnum::Storage(op) => {
let (_, account) = self.sdb.get_storage_mut(&op.address, &op.key);
*account = op.value;
self.block.container.insert(Operation::new(
self.block_ctx.rwc.inc_pre(),
RW::WRITE,
op,
))
self.sdb.set_storage(&op.address, &op.key, &op.value);
}
OpEnum::TxAccessListAccount(op) => {
if !op.value {
if !op.value_prev && op.value {
self.sdb.add_account_to_access_list(op.address);
}
if op.value_prev && !op.value {
self.sdb.remove_account_from_access_list(&op.address);
}
self.block.container.insert(Operation::new(
self.block_ctx.rwc.inc_pre(),
RW::WRITE,
op,
))
}
OpEnum::TxAccessListAccountStorage(op) => {
if !op.value {
if !op.value_prev && op.value {
self.sdb
.add_account_storage_to_access_list((op.address, op.key));
}
if op.value_prev && !op.value {
self.sdb
.remove_account_storage_from_access_list(&(op.address, op.key));
}
self.block.container.insert(Operation::new(
self.block_ctx.rwc.inc_pre(),
RW::WRITE,
op,
))
}
OpEnum::Account(op) => {
let (_, account) = self.sdb.get_account_mut(&op.address);
Expand All @@ -1118,16 +1116,13 @@ impl<'a> CircuitInputStateRef<'a> {
account.code_hash = op.value.to_be_bytes().into();
}
}
self.block.container.insert(Operation::new(
self.block_ctx.rwc.inc_pre(),
RW::WRITE,
op,
))
}
OpEnum::TxRefund(_) => unimplemented!(),
OpEnum::TxRefund(op) => {
self.sdb.set_refund(op.value);
}
OpEnum::AccountDestructed(_) => unimplemented!(),
_ => unreachable!(),
}
};
}

/// Handle a reversion group
Expand All @@ -1141,7 +1136,13 @@ impl<'a> CircuitInputStateRef<'a> {
// Apply reversions
for (step_index, op_ref) in reversion_group.op_refs.into_iter().rev() {
if let Some(op) = self.get_rev_op_by_ref(&op_ref) {
let rev_op_ref = self.apply_reverted_op(op);
self.apply_op(&op);
let rev_op_ref = self.block.container.insert_op_enum(
self.block_ctx.rwc.inc_pre(),
RW::WRITE,
false,
op,
);
self.tx.steps[step_index]
.bus_mapping_instance
.push(rev_op_ref);
Expand Down Expand Up @@ -1473,6 +1474,7 @@ impl<'a> CircuitInputBuilder {

for (index, geth_step) in geth_trace.struct_logs.iter().enumerate() {
let mut state_ref = self.state_ref(&mut tx, &mut tx_ctx);
log::trace!("handle {}th opcode {:?} ", index, geth_step.op);
let exec_steps = gen_associated_ops(
&geth_step.op,
&mut state_ref,
Expand All @@ -1488,8 +1490,8 @@ impl<'a> CircuitInputBuilder {
let end_tx_step = gen_end_tx_ops(&mut self.state_ref(&mut tx, &mut tx_ctx))?;
tx.steps.push(end_tx_step);

self.sdb.commit_tx();
self.block.txs.push(tx);
self.sdb.clear_access_list_and_refund();

Ok(())
}
Expand Down Expand Up @@ -2004,6 +2006,7 @@ mod tracer_tests {
&GethExecTrace {
gas: Gas(0),
failed: false,
return_value: "".to_owned(),
struct_logs: vec![geth_step.clone()],
},
false,
Expand Down
4 changes: 3 additions & 1 deletion bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod number;
mod origin;
mod selfbalance;
mod sload;
mod sstore;
mod stackonlyop;
mod stop;
mod swap;
Expand All @@ -48,6 +49,7 @@ use mstore::Mstore;
use origin::Origin;
use selfbalance::Selfbalance;
use sload::Sload;
use sstore::Sstore;
use stackonlyop::StackOnlyOpcode;
use stop::Stop;
use swap::Swap;
Expand Down Expand Up @@ -138,7 +140,7 @@ fn fn_gen_associated_ops(opcode_id: &OpcodeId) -> FnGenAssociatedOps {
OpcodeId::MSTORE => Mstore::<false>::gen_associated_ops,
OpcodeId::MSTORE8 => Mstore::<true>::gen_associated_ops,
OpcodeId::SLOAD => Sload::gen_associated_ops,
// OpcodeId::SSTORE => {},
OpcodeId::SSTORE => Sstore::gen_associated_ops,
OpcodeId::JUMP => StackOnlyOpcode::<1, 0>::gen_associated_ops,
OpcodeId::JUMPI => StackOnlyOpcode::<2, 0>::gen_associated_ops,
OpcodeId::PC => StackOnlyOpcode::<0, 1>::gen_associated_ops,
Expand Down
Loading