|
| 1 | +from eth.constants import ( |
| 2 | + UINT256 |
| 3 | +) |
| 4 | +from eth.vm.forks.constantinople import ( |
| 5 | + constants |
| 6 | +) |
| 7 | + |
| 8 | +from eth.utils.hexadecimal import ( |
| 9 | + encode_hex, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def sstore_eip1283(computation): |
| 14 | + slot, value = computation.stack_pop(num_items=2, type_hint=UINT256) |
| 15 | + |
| 16 | + current_value = computation.state.account_db.get_storage( |
| 17 | + address=computation.msg.storage_address, |
| 18 | + slot=slot, |
| 19 | + ) |
| 20 | + |
| 21 | + original_value = computation.state.account_db.get_storage( |
| 22 | + address=computation.msg.storage_address, |
| 23 | + slot=slot, |
| 24 | + from_journal=False |
| 25 | + ) |
| 26 | + |
| 27 | + gas_refund = 0 |
| 28 | + |
| 29 | + if current_value == value: |
| 30 | + gas_cost = constants.GAS_SSTORE_EIP1283_NOOP |
| 31 | + else: |
| 32 | + if original_value == current_value: |
| 33 | + if original_value == 0: |
| 34 | + gas_cost = constants.GAS_SSTORE_EIP1283_INIT |
| 35 | + else: |
| 36 | + gas_cost = constants.GAS_SSTORE_EIP1283_CLEAN |
| 37 | + |
| 38 | + if value == 0: |
| 39 | + gas_refund += constants.GAS_SSTORE_EIP1283_CLEAR_REFUND |
| 40 | + else: |
| 41 | + gas_cost = constants.GAS_SSTORE_EIP1283_NOOP |
| 42 | + |
| 43 | + if original_value != 0: |
| 44 | + if current_value == 0: |
| 45 | + gas_refund -= constants.GAS_SSTORE_EIP1283_CLEAR_REFUND |
| 46 | + if value == 0: |
| 47 | + gas_refund += constants.GAS_SSTORE_EIP1283_CLEAR_REFUND |
| 48 | + |
| 49 | + if original_value == value: |
| 50 | + if original_value == 0: |
| 51 | + gas_refund += constants.GAS_SSTORE_EIP1283_RESET_CLEAR_REFUND |
| 52 | + else: |
| 53 | + gas_refund += constants.GAS_SSTORE_EIP1283_RESET_REFUND |
| 54 | + |
| 55 | + computation.consume_gas( |
| 56 | + gas_cost, |
| 57 | + reason="SSTORE: {0}[{1}] -> {2} (current: {3} / original: {4})".format( |
| 58 | + encode_hex(computation.msg.storage_address), |
| 59 | + slot, |
| 60 | + value, |
| 61 | + current_value, |
| 62 | + original_value, |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + if gas_refund: |
| 67 | + computation.refund_gas(gas_refund) |
| 68 | + |
| 69 | + computation.state.account_db.set_storage( |
| 70 | + address=computation.msg.storage_address, |
| 71 | + slot=slot, |
| 72 | + value=value, |
| 73 | + ) |
0 commit comments