Skip to content

Commit 8e3730f

Browse files
authored
Update the eip to store in ring buffer instead of serial storage and add eip 158 handling strategy (#3)
* Update the eip to store in ring buffer instead of serial storage and add eip 158 handling strategy * address feedback * typo * more typos * fix a reference * further cleanup * apply feedback * apply feedback * fix typo
1 parent 941d3be commit 8e3730f

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

EIPS/eip-2935.md

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,90 +12,98 @@ created: 2020-09-03
1212

1313
## Abstract
1414

15-
Store historical block hashes in a contract, and modify the `BLOCKHASH (0x40)` opcode to read this contract.
15+
Store last 256 historical block hashes in a contract, and modify the `BLOCKHASH (0x40)` opcode to read and serve from this contract storage.
1616

1717
## Motivation
1818

19-
There is increasingly a desire to remove the need for most clients to store history older than some relatively short duration (often between 1 week and 1 year) to save disk space. This requires some form of layer-2 network to help clients access historical information. These protocols can be made much simpler if blocks contained a quick Merkle path to historical blocks.
19+
Currently BLOCKHASH opcode accesses history to resolve hash of the block number in EVM. However a more stateless client friendly way is to maintain and serve these hashes from state.
2020

21-
Additional secondary motivations include:
21+
Although this is possible even in Merkle trie state, but Verkle trie state further allows bundling the BLOCKHASH witnesses (along with other witnesses) in an efficient manner making it worthwhile to have these in state.
2222

23-
* The protocol can be used to make more secure efficient light clients with flyclient-like technology (while the "optimal" flyclient protocol is fairly complex, large security gains over the status quo (trusted "canonical hash trees") can be made cheaply)
24-
* Improving cleanness of the protocol, as the BLOCKHASH opcode would then access state and not history.
23+
A side benefit of this approach could be that it allows building/validating proofs related to last 256 ancestors directly against the current state.
2524

2625
## Specification
2726

2827
| Parameter | Value |
2928
| - | - |
3029
| `FORK_TIMESTAMP` | TBD |
3130
| `HISTORY_STORAGE_ADDRESS` | `0xfffffffffffffffffffffffffffffffffffffffe`|
32-
| `MIN_HISTORY_SERVE_WINDOW` | `256` |
33-
| `MAX_HISTORY_SERVE_WINDOW` | `2**256` |
31+
| `HISTORY_SERVE_WINDOW` | `256` |
32+
33+
This EIP specifies for storing last `HISTORY_SERVE_WINDOW` block hashes in a ring buffer storage of `HISTORY_SERVE_WINDOW` length.
34+
3435

3536
At the start of processing any block where `block.timestamp >= FORK_TIMESTAMP` (ie. before processing any transactions), update the history in the following way:
3637

3738
```python
38-
def process_block_hash_history(block :Block, state: State):
39+
def process_block_hash_history(block: Block, state: State):
3940
if block.timestamp >= FORK_TIMESTAMP:
40-
state.insert_slot(HISTORY_STORAGE_ADDRESS, block.number-1, block.parent.hash)
41+
state.insert_slot(HISTORY_STORAGE_ADDRESS, (block.number-1) % HISTORY_SERVE_WINDOW , block.parent.hash)
4142

4243
# If this is the first fork block, add the parent's direct 255 ancestors as well
4344
if block.parent.timestamp < FORK_TIMESTAMP:
4445
ancestor = block.parent
45-
for i in range(MIN_HISTORY_SERVE_WINDOW - 1):
46+
for i in range(HISTORY_SERVE_WINDOW - 1):
4647
# stop at genesis block
4748
if ancestor.number == 0:
4849
break
4950

5051
ancestor = ancestor.parent
51-
state.insert_slot(HISTORY_STORAGE_ADDRESS, ancestor.number, ancestor.hash)
52+
state.insert_slot(HISTORY_STORAGE_ADDRESS, ancestor.number % HISTORY_SERVE_WINDOW, ancestor.hash)
5253
```
5354

54-
Note that if this is the fork block, then it persists the additional requisite history that could be needed while resolving `BLOCKHASH` opcode for all of the `MIN_HISTORY_SERVE_WINDOW` ancestors (up until genesis).
55+
Note that if this is the fork block, then it persists the additional requisite history that could be needed while resolving `BLOCKHASH` opcode for all of the `HISTORY_SERVE_WINDOW` ancestors (up until genesis).
5556

5657
For resolving the `BLOCKHASH` opcode this fork onwards (`block.timestamp >= FORK_TIMESTAMP`), switch the logic to:
5758

5859
```python
5960
def resolve_blockhash(block: Block, state: State, arg: uint64):
6061
# check the wrap around range
61-
if arg >= block.number or arg < max(block.number - MAX_HISTORY_SERVE_WINDOW, 0)
62+
if arg >= block.number or arg < max(block.number - HISTORY_SERVE_WINDOW, 0)
6263
return 0
6364

64-
return state.load_slot(HISTORY_STORAGE_ADDRESS, arg)
65+
return state.load_slot(HISTORY_STORAGE_ADDRESS, arg % HISTORY_SERVE_WINDOW)
6566
```
6667

67-
Note that the above logic allows access deeper than `MIN_HISTORY_SERVE_WINDOW` if it exists all the way upto `MAX_HISTORY_SERVE_WINDOW`.
68-
69-
Edge cases:
68+
Some activation scenarios:
7069

7170
* For the fork to be activated at genesis, no history is written to the genesis state, and at the start of block `1`, genesis hash will be written as a normal operation to slot `0`.
7271
* for activation at block `1`, only genesis hash will be written at slot `0` as there is no additional history that needs to be persisted.
7372
* for activation at block `32`, block `31`'s hash will be written to slot `31` and additonal history for `0..30`'s hashes will be persisted, so all in all `0..31`'s hashes.
74-
* for activation at block `1000`, block `744-999`'s hashes will be presisted in the slot and `BLOCKHASH` for `733` or less would resolve to `0` as only `MIN_HISTORY_SERVE_WINDOW` is persisted.
73+
* for activation at block `1000`, block `744-999`'s hashes will be presisted in the slot and `BLOCKHASH` for `743` or less would resolve to `0` as only `HISTORY_SERVE_WINDOW` can be served.
74+
75+
### [EIP-158](./eip-158.md) handling
7576

76-
### [EIP-158](./eip-158.md) exception
77+
This address is currently exempt from [EIP-158](./eip-158.md) cleanup in Kaustinen Verkle Testnet but we plan to address this in the following way:
7778

78-
This address is currently exempt from [EIP-158](./eip-158.md) cleanup in Kaustinen Verkle Testnet but there are two ways this could be addressed before this EIP is adopted by ACD:
79+
* Deploy a contract à la [EIP-4788](./eip-4788.md) which just supports `get` method to resolve the BLOCKHASH as per the logic defined in `resolve_blockhash` (and use the generated address as the BLOCKHASH contract address).
80+
* While the clients are expected to directly read from state (or maintain and serve from memory) to resolve BLOCKHASH opcode, this contract's `get` could be invoked by transaction (via another contract or directly) leading to a normal contract execution (and gas consumption) as per the semantics of the contract call.
7981

80-
* Update the nonce to 1 in the fork block, or
81-
* Deploy a contract à la [EIP-4788](./eip-4788.md) with `BLOCKHASH` opcode delegating call to this contract with appropriate args.
8282

83-
While the second option looks more elegant, it has a higher complexity as well as gas consumption considerations.
83+
### Gas costs and witnesses
84+
85+
We propose not to modify any gas costs since clients can directly resolve `BLOCKHASH` from an in-memory maintained structure or do a direct actual `SLOAD` or even a "system" execution of the deployed contract's `get`. However, for purposes of bundling block witnesses for stateless clients (for e.g. in Verkle activated networks), client should record corresponding witness accesses and bundle in the witnesses along with the corresponding proofs.
8486

8587
## Rationale
8688

8789
Very similar ideas were proposed before in [EIP-210](./eip-210.md) et al. This EIP is a simplification, removing two sources of needless complexity:
8890

8991
1. Having a tree-like structure with multiple layers as opposed to a single list
9092
2. Writing the EIP in EVM code
93+
3. Serial unbounded storage of hashes for a deep access to the history
94+
95+
However after weighing pros and cons, we decided to go with just a limited ring buffer to only serve the requisite `HISTORY_SERVE_WINDOW` as [EIP-4788](./eip-4788.md) and beacon state accumulators allow (albeit a bit more complex) proof against any ancestor since merge.
96+
97+
Second concern was how to best transition the BLOCKHASH resolution logic post fork by:
9198

92-
The former was intended to save space. Since then, however, storage usage has increased massively, to the point where even eg. 5 million new storage slots are fairly negligible compared to existing usage. The latter was intended as a first step toward "writing the Ethereum protocol in EVM" as much as possible, but this goal has since been de-facto abandoned.
99+
1. Either waiting for `HISTORY_SERVE_WINDOW` blocks for the entire relevant history to persist
100+
2. Storing of all last `HISTORY_SERVE_WINDOW` block hashes on the fork block.
93101

94-
Storing of all last `MIN_HISTORY_SERVE_WINDOW` block hashes alleviates the need to detect fork activation height to transition to the new logic in backward compatible manner as the entire requisite history will be available from the first block of the fork itself. The cost of doing so is marginal considering the `MIN_HISTORY_SERVE_WINDOW` being small.
102+
We choose to go with later as it alleviates the need to detect fork activation height to transition to the new logic in backward compatible manner as the entire requisite history will be available from the first block of the fork itself. The cost of doing so is marginal considering the `HISTORY_SERVE_WINDOW` being small.
95103

96104
## Backwards Compatibility
97105

98-
The range of `BLOCKHASH` is increased by this opcode, but behavior within the previous `MIN_HISTORY_SERVE_WINDOW`-block range remains unchanged.
106+
The behavior of `BLOCKHASH` opcode remains same and this EIP doesn't affect backward compatibility with the contracts deployed or the gas consumption costs as the resolution of the opcode is handled "directly" by the clients.
99107

100108
## Test Cases
101109

@@ -104,11 +112,11 @@ TBD
104112
## Reference Implementation
105113

106114
* PR 28878 of go-ethereum
107-
* Active on verkle-gen-devet-3 for its verkle implementation
115+
* Active on verkle-gen-devnet-5 for its verkle implementation
108116

109117
## Security Considerations
110118

111-
Adding ~2.5 million storage slots per year bloats the state somewhat but not much relative to the hundreds of millions of existing state objects.
119+
Having contracts (system or otherwise) with hot update paths (branches) poses a risk of "branch" poisioning attacks where attacker could sprinkle trivial amounts of eth around these hot paths (branches). But it has been deemed that cost of attack would escalate significantly to cause any meaningful slow down of state root updates.
112120

113121
## Copyright
114122

0 commit comments

Comments
 (0)