Skip to content

Commit

Permalink
Replace nth_last(0) and nth_last_filled(0) with last() and `las…
Browse files Browse the repository at this point in the history
…t_filled()`. (scroll-tech#1021)
  • Loading branch information
silathdiir authored Nov 9, 2023
1 parent 5a82ce9 commit fbd0e8b
Show file tree
Hide file tree
Showing 24 changed files with 63 additions and 87 deletions.
8 changes: 4 additions & 4 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ impl<'a> CircuitInputBuilder {
} else if geth_step.op.is_call_without_value() {
format!(
"{:?} {:40x} {:?} {:?} {:?} {:?}",
geth_step.stack.nth_last(0),
geth_step.stack.last(),
geth_step.stack.nth_last(1).unwrap_or_default(),
geth_step.stack.nth_last(2),
geth_step.stack.nth_last(3),
Expand All @@ -637,7 +637,7 @@ impl<'a> CircuitInputBuilder {
} else if geth_step.op.is_call_with_value() {
format!(
"{:?} {:40x} {:?} {:?} {:?} {:?} {:?}",
geth_step.stack.nth_last(0),
geth_step.stack.last(),
geth_step.stack.nth_last(1).unwrap_or_default(),
geth_step.stack.nth_last(2),
geth_step.stack.nth_last(3),
Expand All @@ -648,7 +648,7 @@ impl<'a> CircuitInputBuilder {
} else if geth_step.op.is_create() {
format!(
"value {:?} offset {:?} size {:?} {}",
geth_step.stack.nth_last(0),
geth_step.stack.last(),
geth_step.stack.nth_last(1),
geth_step.stack.nth_last(2),
if geth_step.op == OpcodeId::CREATE2 {
Expand All @@ -661,7 +661,7 @@ impl<'a> CircuitInputBuilder {
format!(
"{:?} {:?} {:?}",
state_ref.call().map(|c| c.address),
geth_step.stack.nth_last(0),
geth_step.stack.last(),
geth_step.stack.nth_last(1),
)
} else {
Expand Down
16 changes: 8 additions & 8 deletions bus-mapping/src/circuit_input_builder/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn get_call_result(trace: &[GethExecStep]) -> Option<Word> {
trace[1..]
.iter()
.find(|s| s.depth == depth)
.and_then(|s| s.stack.nth_last(0).ok())
.and_then(|s| s.stack.last().ok())
}

/// State and Code Access set.
Expand Down Expand Up @@ -184,12 +184,12 @@ pub fn gen_state_access_trace<TX>(
match step.op {
OpcodeId::SSTORE => {
let address = contract_address;
let key = step.stack.nth_last(0)?;
let key = step.stack.last()?;
accs.push(Access::new(i, WRITE, Storage { address, key }));
}
OpcodeId::SLOAD => {
let address = contract_address;
let key = step.stack.nth_last(0)?;
let key = step.stack.last()?;
accs.push(Access::new(i, READ, Storage { address, key }));
}
OpcodeId::SELFBALANCE => {
Expand All @@ -207,25 +207,25 @@ pub fn gen_state_access_trace<TX>(
}
}
OpcodeId::BALANCE => {
let address = step.stack.nth_last(0)?.to_address();
let address = step.stack.last()?.to_address();
accs.push(Access::new(i, READ, Account { address }));
}
OpcodeId::EXTCODEHASH => {
let address = step.stack.nth_last(0)?.to_address();
let address = step.stack.last()?.to_address();
accs.push(Access::new(i, READ, Account { address }));
}
OpcodeId::EXTCODESIZE => {
let address = step.stack.nth_last(0)?.to_address();
let address = step.stack.last()?.to_address();
accs.push(Access::new(i, READ, Code { address }));
}
OpcodeId::EXTCODECOPY => {
let address = step.stack.nth_last(0)?.to_address();
let address = step.stack.last()?.to_address();
accs.push(Access::new(i, READ, Code { address }));
}
OpcodeId::SELFDESTRUCT => {
let address = contract_address;
accs.push(Access::new(i, WRITE, Account { address }));
let address = step.stack.nth_last(0)?.to_address();
let address = step.stack.last()?.to_address();
accs.push(Access::new(i, WRITE, Account { address }));
}
OpcodeId::CREATE => {
Expand Down
10 changes: 5 additions & 5 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ impl<'a> CircuitInputStateRef<'a> {

// Store deployed code if it's a successful create
if call_success_create {
let offset = step.stack.nth_last(0)?;
let offset = step.stack.last()?;
let length = step.stack.nth_last(1)?;
let code = callee_memory.read_chunk(MemoryRange::new_with_length(
offset.low_u64(),
Expand Down Expand Up @@ -1272,7 +1272,7 @@ impl<'a> CircuitInputStateRef<'a> {
&& step.error.is_none()
&& !call_success_create
{
step.stack.nth_last(0)?.low_u64()
step.stack.last()?.low_u64()
} else {
// common err, call empty, call precompile
0
Expand Down Expand Up @@ -1318,7 +1318,7 @@ impl<'a> CircuitInputStateRef<'a> {
[Word::zero(), return_data_length]
}
OpcodeId::REVERT | OpcodeId::RETURN => {
let offset = geth_step.stack.nth_last(0)?;
let offset = geth_step.stack.last()?;
let length = geth_step.stack.nth_last(1)?;
// This is the convention we are using for memory addresses so that there is no
// memory expansion cost when the length is 0.
Expand Down Expand Up @@ -1546,7 +1546,7 @@ impl<'a> CircuitInputStateRef<'a> {
// get value first if call/create
let value = match step.op {
OpcodeId::CALL | OpcodeId::CALLCODE => step.stack.nth_last(2)?,
OpcodeId::CREATE | OpcodeId::CREATE2 => step.stack.nth_last(0)?,
OpcodeId::CREATE | OpcodeId::CREATE2 => step.stack.last()?,
_ => Word::zero(),
};

Expand Down Expand Up @@ -1586,7 +1586,7 @@ impl<'a> CircuitInputStateRef<'a> {
} else {
// Return from a {CREATE, CREATE2} with a failure, via RETURN
if call.is_create() {
let offset = step.stack.nth_last(0)?;
let offset = step.stack.last()?;
let length = step.stack.nth_last(1)?;
if length > Word::from(MAX_CODE_SIZE) {
return Ok(Some(ExecError::MaxCodeSizeExceeded));
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/circuit_input_builder/tracer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ fn tracer_err_code_store_out_of_gas_tx_deploy() {
}

fn check_err_invalid_code(step: &GethExecStep, next_step: Option<&GethExecStep>) -> bool {
let offset = step.stack.nth_last(0).unwrap();
let offset = step.stack.last().unwrap();
let length = step.stack.nth_last(1).unwrap();
step.op == OpcodeId::RETURN
&& step.error.is_none()
Expand Down
6 changes: 3 additions & 3 deletions bus-mapping/src/evm/opcodes/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Opcode for Balance {
} else {
H256::zero()
};
debug_assert_eq!(balance, geth_steps[1].stack.nth_last(0)?);
debug_assert_eq!(balance, geth_steps[1].stack.last()?);
state.account_read(
&mut exec_step,
address,
Expand All @@ -78,8 +78,8 @@ impl Opcode for Balance {
// Write the BALANCE result to stack.
state.stack_write(
&mut exec_step,
geth_steps[1].stack.nth_last_filled(0),
geth_steps[1].stack.nth_last(0)?,
geth_steps[1].stack.last_filled(),
geth_steps[1].stack.last()?,
)?;

Ok(vec![exec_step])
Expand Down
8 changes: 2 additions & 6 deletions bus-mapping/src/evm/opcodes/blockhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ impl Opcode for Blockhash {
let geth_step = &geth_steps[0];
let mut exec_step = state.new_step(geth_step)?;

let block_number = geth_step.stack.nth_last(0)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(0),
block_number,
)?;
let block_number = geth_step.stack.last()?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), block_number)?;

let block_hash = geth_steps[1].stack.last()?;
state.stack_write(
Expand Down
10 changes: 3 additions & 7 deletions bus-mapping/src/evm/opcodes/calldatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ fn gen_calldatacopy_step(
geth_step: &GethExecStep,
) -> Result<ExecStep, Error> {
let mut exec_step = state.new_step(geth_step)?;
let memory_offset = geth_step.stack.nth_last(0)?;
let memory_offset = geth_step.stack.last()?;
let data_offset = geth_step.stack.nth_last(1)?;
let length = geth_step.stack.nth_last(2)?;

state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(0),
memory_offset,
)?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), memory_offset)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(1),
Expand Down Expand Up @@ -89,7 +85,7 @@ fn gen_copy_event(
) -> Result<CopyEvent, Error> {
let rw_counter_start = state.block_ctx.rwc;

let memory_offset = geth_step.stack.nth_last(0)?;
let memory_offset = geth_step.stack.last()?;
let data_offset = geth_step.stack.nth_last(1)?;
let length = geth_step.stack.nth_last(2)?;

Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/calldataload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl Opcode for Calldataload {

// fetch the top of the stack, i.e. offset in calldata to start reading 32-bytes
// from.
let offset = geth_step.stack.nth_last(0)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(0), offset)?;
let offset = geth_step.stack.last()?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), offset)?;

// Check if offset is Uint64 overflow.
let calldata_word = if let Ok(offset) = u64::try_from(offset) {
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<const N_ARGS: usize> Opcode for CallOpcode<N_ARGS> {
// panic with full info
let info1 = format!("callee_gas_left {callee_gas_left} gas_specified {gas_specified} gas_cost {gas_cost} is_warm {is_warm} has_value {has_value} current_memory_word_size {curr_memory_word_size} next_memory_word_size {next_memory_word_size}, memory_expansion_gas_cost {memory_expansion_gas_cost}");
let info2 = format!("args gas:{:?} addr:{:?} value:{:?} cd_pos:{:?} cd_len:{:?} rd_pos:{:?} rd_len:{:?}",
geth_step.stack.nth_last(0),
geth_step.stack.last(),
geth_step.stack.nth_last(1),
geth_step.stack.nth_last(2),
geth_step.stack.nth_last(3),
Expand Down
10 changes: 3 additions & 7 deletions bus-mapping/src/evm/opcodes/codecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ fn gen_codecopy_step(
) -> Result<ExecStep, Error> {
let mut exec_step = state.new_step(geth_step)?;

let dest_offset = geth_step.stack.nth_last(0)?;
let dest_offset = geth_step.stack.last()?;
let code_offset = geth_step.stack.nth_last(1)?;
let length = geth_step.stack.nth_last(2)?;

// stack reads
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(0),
dest_offset,
)?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), dest_offset)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(1),
Expand All @@ -58,7 +54,7 @@ fn gen_copy_event(
) -> Result<CopyEvent, Error> {
let rw_counter_start = state.block_ctx.rwc;

let dst_offset = geth_step.stack.nth_last(0)?;
let dst_offset = geth_step.stack.last()?;
let code_offset = geth_step.stack.nth_last(1)?;
let length = geth_step.stack.nth_last(2)?.as_u64();

Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/error_codestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl Opcode for ErrorCodeStore {
|| exec_step.error == Some(ExecError::MaxCodeSizeExceeded)
);

let offset = geth_step.stack.nth_last(0)?;
let offset = geth_step.stack.last()?;
let length = geth_step.stack.nth_last(1)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(0), offset)?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), offset)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(1), length)?;

// in internal call context
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/error_invalid_creation_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl Opcode for ErrorCreationCode {

exec_step.error = Some(ExecError::InvalidCreationCode);

let offset = geth_step.stack.nth_last(0)?;
let offset = geth_step.stack.last()?;
let length = geth_step.stack.nth_last(1)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(0), offset)?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), offset)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(1), length)?;

// in create context
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/error_oog_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl Opcode for ErrorOOGLog {
OpcodeId::LOG4
]
.contains(&geth_step.op));
let mstart = geth_step.stack.nth_last(0)?;
let mstart = geth_step.stack.last()?;
let msize = geth_step.stack.nth_last(1)?;

state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(0), mstart)?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), mstart)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(1), msize)?;
// read static call property
state.call_context_read(
Expand Down
8 changes: 2 additions & 6 deletions bus-mapping/src/evm/opcodes/error_return_data_outofbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ impl Opcode for ErrorReturnDataOutOfBound {
Some(ExecError::ReturnDataOutOfBounds)
);

let memory_offset = geth_step.stack.nth_last(0)?;
let memory_offset = geth_step.stack.last()?;
let data_offset = geth_step.stack.nth_last(1)?;
let length = geth_step.stack.nth_last(2)?;

state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(0),
memory_offset,
)?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), memory_offset)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(1),
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl Opcode for Exponentiation {
let geth_step = &geth_steps[0];
let mut exec_step = state.new_step(geth_step)?;

let base = geth_step.stack.nth_last(0)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(0), base)?;
let base = geth_step.stack.last()?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), base)?;
let exponent = geth_step.stack.nth_last(1)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(1), exponent)?;

Expand Down
6 changes: 3 additions & 3 deletions bus-mapping/src/evm/opcodes/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn gen_extcodecopy_step(
) -> Result<ExecStep, Error> {
let mut exec_step = state.new_step(geth_step)?;

let external_address_word = geth_step.stack.nth_last(0)?;
let external_address_word = geth_step.stack.last()?;
let external_address = external_address_word.to_address();
let dest_offset = geth_step.stack.nth_last(1)?;
let offset = geth_step.stack.nth_last(2)?;
Expand All @@ -40,7 +40,7 @@ fn gen_extcodecopy_step(
// stack reads
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(0),
geth_step.stack.last_filled(),
external_address_word,
)?;
state.stack_read(
Expand Down Expand Up @@ -100,7 +100,7 @@ fn gen_copy_event(
) -> Result<CopyEvent, Error> {
let rw_counter_start = state.block_ctx.rwc;

let external_address = geth_step.stack.nth_last(0)?.to_address();
let external_address = geth_step.stack.last()?.to_address();
let dst_offset = geth_step.stack.nth_last(1)?;
let code_offset = geth_step.stack.nth_last(2)?;
let length = geth_step.stack.nth_last(3)?.as_u64();
Expand Down
6 changes: 1 addition & 5 deletions bus-mapping/src/evm/opcodes/extcodesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ impl Opcode for Extcodesize {

// Write the EXTCODESIZE result to stack.
debug_assert_eq!(code_size, geth_steps[1].stack.last()?);
state.stack_write(
&mut exec_step,
geth_steps[1].stack.nth_last_filled(0),
code_size,
)?;
state.stack_write(&mut exec_step, geth_steps[1].stack.last_filled(), code_size)?;

Ok(vec![exec_step])
}
Expand Down
8 changes: 4 additions & 4 deletions bus-mapping/src/evm/opcodes/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Opcode for Log {
}

// reconstruction
let offset = geth_step.stack.nth_last(0)?;
let offset = geth_step.stack.last()?;
let length = geth_step.stack.nth_last(1)?.as_u64();

if length != 0 {
Expand All @@ -49,11 +49,11 @@ fn gen_log_step(
) -> Result<ExecStep, Error> {
let mut exec_step = state.new_step(geth_step)?;

let mstart = geth_step.stack.nth_last(0)?;
let mstart = geth_step.stack.last()?;
let msize = geth_step.stack.nth_last(1)?;

let call_id = state.call()?.call_id;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(0), mstart)?;
state.stack_read(&mut exec_step, geth_step.stack.last_filled(), mstart)?;
state.stack_read(&mut exec_step, geth_step.stack.nth_last_filled(1), msize)?;

state.call_context_read(
Expand Down Expand Up @@ -133,7 +133,7 @@ fn gen_copy_event(
// Get low Uint64 for memory start as below reference. Memory size must be
// within range of Uint64, otherwise returns ErrGasUintOverflow.
// https://github.com/ethereum/go-ethereum/blob/b80f05bde2c4e93ae64bb3813b6d67266b5fc0e6/core/vm/instructions.go#L850
let memory_start = geth_step.stack.nth_last(0)?.low_u64();
let memory_start = geth_step.stack.last()?.low_u64();
let msize = geth_step.stack.nth_last(1)?.as_u64();

let (src_addr, src_addr_end) = (memory_start, memory_start.checked_add(msize).unwrap());
Expand Down
Loading

0 comments on commit fbd0e8b

Please sign in to comment.