On Efficient Ethereum Storage

0age
0age
Mar 4 · 11 min read

RSTORE: Save More (Sometimes)

There’s Always a Trade-off

RLOAD: A Reading Rainbow 🌈

Working Remote — No Pants Required

The Raw Reality of RSTORE & RLOAD

Crafting a Metamorphic Storage Contract

Metamorphic Storage Contract Initialization Code (69 bytes)0x5860008158601c335a630c85c0028752fa153d602090039150607381533360601b600152653318585733ff60d01b601552602080808403918260d81b601b52602001903ef3
pc op operation_name   [stack_items]                 <memory>00 58 PC               [0]
01 60 PUSH1 0x00 [0, 0]
03 81 DUP2 [0, 0, 0]
04 58 PC [0, 0, 0, 4]
05 60 PUSH1 0x1c [0, 0, 0, 4, 28]
07 33 CALLER [0, 0, 0, 4, 28, caller]
08 5A GAS [0, 0, 0, 4, 28, caller, gas]
09 63 PUSH4 0x0c85c002 [0, 0, 0, 4, 28, caller, gas, selector]
0E 87 DUP8 [0, 0, 0, 4, 28, caller, gas, selector, 0]
0F 52 MSTORE [0, 0, 0, 4, 28, caller, gas] <selector>
10 FA STATICCALL [0, success => 1]
pc    op    operation_name    [stack_items]11    15    ISZERO            [0, 0]
12 3D RETURNDATASIZE [0, 0, size]
13 60 PUSH1 0x20 [0, 0, size, 32]
15 90 SWAP1 [0, 0, 32, size]
16 03 SUB [0, 0, size - 32]
17 91 SWAP2 [size - 32, 0, 0]
18 50 POP [size - 32, 0]
op operation_name73 PUSH20 *factory_address*        // Hard-code the factory address
33 CALLER // Get the caller's address
18 XOR // Equals 0 iff address is caller
58 PC // no jumpdest to this PC, so:
57 JUMPI // revert if caller != address
33 CALLER // send any funds to the caller
ff SELFDESTRUCT // selfdestruct the contract!
op operation_name       [stack_items]  <memory>60 PUSH1 0x73           [size - 32, 0, *push20*]
81 DUP2 [size - 32, 0, *push20*, 0]
53 MSTORE8 [size - 32, 0] <*push20*>
33 CALLER [size - 32, 0, caller]
60 PUSH1 0x60 [size - 32, 0, caller, 12_byte_offset]
1B SHL [size - 32, 0, caller_with_12_byte_offset]
60 PUSH1 0x01 [size - 32, 0, caller_with_offset, 1]
52 MSTORE [size - 32, 0] <*push20_caller*>
65 PUSH6 0x3318585733ff [size - 32, 0, opcodes]
60 PUSH1 0xd0 [size - 32, 0, opcodes, 26_byte_offset]
1B SHL [size - 32, 0, opcodes_with_26_byte_offset]
60 PUSH1 0x15 [size - 32, 0, opcodes_with_offset, 21]
52 MSTORE [size - 32, 0] <*push20_caller_opcodes*>
op operation   [stack_items]                     <memory>60 PUSH1 0x20  [size - 32, 0, 32]
80 DUP1 [size - 32, 0, 32, 32]
80 DUP1 [size - 32, 0, 32, 32, 32]
84 DUP5 [size - 32, 0, 32, 32, 32, size - 32]
03 SUB [size - 32, 0, 32, 32, size - 64]
91 SWAP2 [size - 32, 0, size - 64, 32, 32]
82 DUP3 [size - 32, 0, size - 64, 32, 32, size - 64]
60 PUSH1 0xd8 [size - 32, 0, size - 64, 32, 32, size - 64, offset]
1B SHL [size - 32, 0, size - 64, 32, 32, size-64_offset]
60 PUSH1 0x1b [size - 32, 0, size - 64, 32, 32, size-64_offset, 27]
52 MSTORE [size - 32, 0, size - 64, 32, 32] <*control_word*>
op operation_name       [stack_items] <memory> {deploy_runtime_code}60 PUSH1 0x20           [size - 32, 0, size - 64, 32, 32, 32]
01 ADD [size - 32, 0, size - 64, 32, 64]
90 SWAP1 [size - 32, 0, size - 64, 64, 32]
3E RETURNDATACOPY [size - 32, 0] <*control_word+data*>
F3 RETURN {*control_word+data*}

A Wrinkle in Transaction Time

Additional Homework for Extra-Debit

Coinmonks

Coinmonks is a technology-focused publication embracing decentralize technologies. We are Non-profit and education is our core value. Learn, Build and thrive. Our other project— https://coinmonks.com, https://cryptofi.co, https://coincodecap.com

0age

Written by

0age

EVM Security & Upgradeability Researcher

Coinmonks

Coinmonks

Coinmonks is a technology-focused publication embracing decentralize technologies. We are Non-profit and education is our core value. Learn, Build and thrive. Our other project— https://coinmonks.com, https://cryptofi.co, https://coincodecap.com