|
| 1 | +use super::Opcode; |
| 2 | +use crate::{ |
| 3 | + circuit_input_builder::{CircuitInputStateRef, ExecStep}, |
| 4 | + operation::{CallContextField, TransientStorageOp}, |
| 5 | + Error, |
| 6 | +}; |
| 7 | +use eth_types::{GethExecStep, ToWord, Word}; |
| 8 | + |
| 9 | +/// Placeholder structure used to implement [`Opcode`] trait over it |
| 10 | +/// corresponding to the [`OpcodeId::TSTORE`](crate::evm::OpcodeId::TSTORE) |
| 11 | +/// `OpcodeId`. |
| 12 | +#[derive(Debug, Copy, Clone)] |
| 13 | +pub(crate) struct Tstore; |
| 14 | + |
| 15 | +impl Opcode for Tstore { |
| 16 | + fn gen_associated_ops( |
| 17 | + state: &mut CircuitInputStateRef, |
| 18 | + geth_steps: &[GethExecStep], |
| 19 | + ) -> Result<Vec<ExecStep>, Error> { |
| 20 | + let geth_step = &geth_steps[0]; |
| 21 | + let mut exec_step = state.new_step(geth_step)?; |
| 22 | + |
| 23 | + let contract_addr = state.call()?.address; |
| 24 | + |
| 25 | + state.call_context_read( |
| 26 | + &mut exec_step, |
| 27 | + state.call()?.call_id, |
| 28 | + CallContextField::TxId, |
| 29 | + Word::from(state.tx_ctx.id()), |
| 30 | + )?; |
| 31 | + state.call_context_read( |
| 32 | + &mut exec_step, |
| 33 | + state.call()?.call_id, |
| 34 | + CallContextField::IsStatic, |
| 35 | + Word::from(state.call()?.is_static as u8), |
| 36 | + )?; |
| 37 | + |
| 38 | + state.call_context_read( |
| 39 | + &mut exec_step, |
| 40 | + state.call()?.call_id, |
| 41 | + CallContextField::RwCounterEndOfReversion, |
| 42 | + Word::from(state.call()?.rw_counter_end_of_reversion), |
| 43 | + )?; |
| 44 | + |
| 45 | + state.call_context_read( |
| 46 | + &mut exec_step, |
| 47 | + state.call()?.call_id, |
| 48 | + CallContextField::IsPersistent, |
| 49 | + Word::from(state.call()?.is_persistent as u8), |
| 50 | + )?; |
| 51 | + |
| 52 | + state.call_context_read( |
| 53 | + &mut exec_step, |
| 54 | + state.call()?.call_id, |
| 55 | + CallContextField::CalleeAddress, |
| 56 | + state.call()?.address.to_word(), |
| 57 | + )?; |
| 58 | + |
| 59 | + let key = geth_step.stack.nth_last(0)?; |
| 60 | + let key_stack_position = geth_step.stack.nth_last_filled(0); |
| 61 | + let value = geth_step.stack.nth_last(1)?; |
| 62 | + let value_stack_position = geth_step.stack.nth_last_filled(1); |
| 63 | + |
| 64 | + state.stack_read(&mut exec_step, key_stack_position, key)?; |
| 65 | + state.stack_read(&mut exec_step, value_stack_position, value)?; |
| 66 | + |
| 67 | + let (_, value_prev) = state.sdb.get_transient_storage(&contract_addr, &key); |
| 68 | + let value_prev = *value_prev; |
| 69 | + |
| 70 | + state.push_op_reversible( |
| 71 | + &mut exec_step, |
| 72 | + TransientStorageOp::new( |
| 73 | + state.call()?.address, |
| 74 | + key, |
| 75 | + value, |
| 76 | + value_prev, |
| 77 | + state.tx_ctx.id(), |
| 78 | + ), |
| 79 | + )?; |
| 80 | + |
| 81 | + Ok(vec![exec_step]) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tstore_tests { |
| 87 | + use super::*; |
| 88 | + use crate::{ |
| 89 | + circuit_input_builder::ExecState, |
| 90 | + mock::BlockData, |
| 91 | + operation::{CallContextOp, StackOp, RW}, |
| 92 | + }; |
| 93 | + use eth_types::{ |
| 94 | + bytecode, |
| 95 | + evm_types::{OpcodeId, StackAddress}, |
| 96 | + geth_types::GethData, |
| 97 | + Word, |
| 98 | + }; |
| 99 | + use mock::{test_ctx::helpers::tx_from_1_to_0, TestContext, MOCK_ACCOUNTS}; |
| 100 | + use pretty_assertions::assert_eq; |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn tstore_opcode() { |
| 104 | + let code = bytecode! { |
| 105 | + // Write 0x6f to storage slot 0 |
| 106 | + PUSH1(0x6fu64) |
| 107 | + PUSH1(0x00u64) |
| 108 | + TSTORE |
| 109 | + PUSH1(0x00u64) |
| 110 | + TLOAD |
| 111 | + STOP |
| 112 | + }; |
| 113 | + let expected_prev_value = 0x00u64; |
| 114 | + |
| 115 | + // Get the execution steps from the external tracer |
| 116 | + let block: GethData = TestContext::<2, 1>::new( |
| 117 | + None, |
| 118 | + |accs| { |
| 119 | + accs[0] |
| 120 | + .address(MOCK_ACCOUNTS[0]) |
| 121 | + .balance(Word::from(10u64.pow(19))) |
| 122 | + .code(code) |
| 123 | + .storage(vec![(0x00u64.into(), 0x6fu64.into())].into_iter()); |
| 124 | + accs[1] |
| 125 | + .address(MOCK_ACCOUNTS[1]) |
| 126 | + .balance(Word::from(10u64.pow(19))); |
| 127 | + }, |
| 128 | + tx_from_1_to_0, |
| 129 | + |block, _tx| block.number(0xcafeu64), |
| 130 | + ) |
| 131 | + .unwrap() |
| 132 | + .into(); |
| 133 | + |
| 134 | + let builder = BlockData::new_from_geth_data(block.clone()).new_circuit_input_builder(); |
| 135 | + let builder = builder |
| 136 | + .handle_block(&block.eth_block, &block.geth_traces) |
| 137 | + .unwrap(); |
| 138 | + |
| 139 | + let step = builder.block.txs()[0] |
| 140 | + .steps() |
| 141 | + .iter() |
| 142 | + .rev() // find last tstore |
| 143 | + .find(|step| step.exec_state == ExecState::Op(OpcodeId::TSTORE)) |
| 144 | + .unwrap(); |
| 145 | + |
| 146 | + assert_eq!( |
| 147 | + [0, 1, 2, 3, 4] |
| 148 | + .map(|idx| &builder.block.container.call_context |
| 149 | + [step.bus_mapping_instance[idx].as_usize()]) |
| 150 | + .map(|operation| (operation.rw(), operation.op())), |
| 151 | + [ |
| 152 | + ( |
| 153 | + RW::READ, |
| 154 | + &CallContextOp::new(1, CallContextField::TxId, Word::from(0x01)), |
| 155 | + ), |
| 156 | + ( |
| 157 | + RW::READ, |
| 158 | + &CallContextOp::new(1, CallContextField::IsStatic, Word::from(0x00)), |
| 159 | + ), |
| 160 | + ( |
| 161 | + RW::READ, |
| 162 | + &CallContextOp::new( |
| 163 | + 1, |
| 164 | + CallContextField::RwCounterEndOfReversion, |
| 165 | + Word::from(0x00) |
| 166 | + ), |
| 167 | + ), |
| 168 | + ( |
| 169 | + RW::READ, |
| 170 | + &CallContextOp::new(1, CallContextField::IsPersistent, Word::from(0x01)), |
| 171 | + ), |
| 172 | + ( |
| 173 | + RW::READ, |
| 174 | + &CallContextOp::new( |
| 175 | + 1, |
| 176 | + CallContextField::CalleeAddress, |
| 177 | + MOCK_ACCOUNTS[0].to_word(), |
| 178 | + ), |
| 179 | + ), |
| 180 | + ] |
| 181 | + ); |
| 182 | + |
| 183 | + assert_eq!( |
| 184 | + [5, 6] |
| 185 | + .map(|idx| &builder.block.container.stack[step.bus_mapping_instance[idx].as_usize()]) |
| 186 | + .map(|operation| (operation.rw(), operation.op())), |
| 187 | + [ |
| 188 | + ( |
| 189 | + RW::READ, |
| 190 | + &StackOp::new(1, StackAddress::from(1022), Word::from(0x0u32)) |
| 191 | + ), |
| 192 | + ( |
| 193 | + RW::READ, |
| 194 | + &StackOp::new(1, StackAddress::from(1023), Word::from(0x6fu32)) |
| 195 | + ), |
| 196 | + ] |
| 197 | + ); |
| 198 | + |
| 199 | + let storage_op = |
| 200 | + &builder.block.container.transient_storage[step.bus_mapping_instance[7].as_usize()]; |
| 201 | + assert_eq!( |
| 202 | + (storage_op.rw(), storage_op.op()), |
| 203 | + ( |
| 204 | + RW::WRITE, |
| 205 | + &TransientStorageOp::new( |
| 206 | + MOCK_ACCOUNTS[0], |
| 207 | + Word::from(0x0u32), |
| 208 | + Word::from(0x6fu32), |
| 209 | + Word::from(expected_prev_value), |
| 210 | + 1, |
| 211 | + ) |
| 212 | + ) |
| 213 | + ); |
| 214 | + } |
| 215 | +} |
0 commit comments