You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: EIPS/eip-4788.md
+44-16
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ restaking constructions, smart contract bridges, MEV mitigations and more.
29
29
|--- |--- |---
30
30
|`FORK_TIMESTAMP`| TBD |
31
31
|`HISTORY_STORAGE_ADDRESS`|`Bytes20(0xB)`|
32
-
| `G_beacon_root` | 2100 | gas
32
+
| `G_beacon_root` | 4200 | gas
33
33
|`HISTORICAL_ROOTS_LENGTH`| 98304 |
34
34
35
35
### Background
@@ -53,47 +53,65 @@ Validity is guaranteed from the consensus layer, much like how withdrawals are h
53
53
At the start of processing any execution block where `block.timestamp >= FORK_TIMESTAMP` (i.e. before processing any transactions),
54
54
write the parent beacon root provided in the block header into the storage of the contract at `HISTORY_STORAGE_ADDRESS`.
55
55
56
-
The timestamp (a 64-bit unsigned integer value) of the header is used as a key into the contract's storage.
57
-
To map the timestamp to the correct key, the timestamp as a number is reduced modulo `HISTORICAL_ROOTS_LENGTH` and
58
-
this resulting 64-bit unsigned integer should be encoded as 32 bytes in big-endian format when writing to the storage.
56
+
In order to bound the storage used by this precompile, two ring buffers are used: one to track the latest root at a given index and another to track
57
+
the latest timestamp at a given index.
59
58
60
-
The 32 bytes of the `parent_beacon_block_root` (as provided) are the
61
-
value to write in the contract's storage.
59
+
To derive the index `root_index` into the root ring buffer, the timestamp (a 64-bit unsigned integer value) is reduced modulo `HISTORICAL_ROOTS_LENGTH`.
60
+
To derive the index `timestamp_index` into the timestamp ring buffer, add `HISTORICAL_ROOTS_LENGTH` to the index into the root ring buffer.
61
+
Both resulting 64-bit unsigned integers should be encoded as 32 bytes in big-endian format when writing to the storage.
62
+
63
+
The 32 bytes of the `parent_beacon_block_root` (as provided) are the value to write behind the `root_index`.
64
+
The timestamp from the header, encoded as 32 bytes in big-endian format, is the value to write behind the `timestamp_index`.
If there is no timestamp stored at the given root, the opcode follows the existing EVM semantics of `SLOAD` returning `0`.
111
+
Alongside the existing gas for calling the precompile, there is an additional gas cost of `G_beacon_root` cost to reflect the two (2) implicit `SLOAD`s from
112
+
the precompile's state.
113
+
114
+
If there is no root stored at the given timestamp, the opcode follows the existing EVM semantics of `SLOAD` returning `0`.
97
115
98
116
## Rationale
99
117
@@ -115,6 +133,16 @@ be nonfavorable conditions.
115
133
Use of block root over state root does mean proofs will require a few additional nodes but this cost is negligible (and could be amortized across all consumers,
116
134
e.g. with a singleton state root contract that caches the proof per slot).
117
135
136
+
### Why two ring buffers?
137
+
138
+
The first ring buffer only tracks `HISTORICAL_ROOTS_LENGTH` worth of roots and so for all possible timestamp values would consume a constant amount of storage.
139
+
However, this design opens the precompile to an attack where a skipped slot that has the same value modulo the ring buffer length would return an old root value,
140
+
rather than the most recent one.
141
+
142
+
To nullify this attack, this EIP keeps track of the pair of data `(parent_beacon_block_root, timestamp)` for each index into the
143
+
ring buffer and verifies the timestamp matches the one originally used to write the root data when being read. Given the fixed size of storage slots (only 32 bytes), the requirement
144
+
to store a pair of values necessitates two ring buffers, rather than just one.
0 commit comments