extend `BlockHeader` for EIP-4844 (#570)

Extends `BlockHeader` with `excessDataGas` according to EIP-4844
(used by Nimbus-CL in empty block prod fallback).
This commit is contained in:
Etan Kissling 2022-12-13 20:53:05 +01:00 committed by GitHub
parent 5c46220e72
commit c9fcab8052
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 8 deletions

View File

@ -121,6 +121,7 @@ type
# `baseFee` is the get/set of `fee` # `baseFee` is the get/set of `fee`
fee*: Option[UInt256] # EIP-1559 fee*: Option[UInt256] # EIP-1559
withdrawalsRoot*: Option[Hash256] # EIP-4895 withdrawalsRoot*: Option[Hash256] # EIP-4895
excessDataGas*: Option[GasInt] # EIP-4844
BlockBody* = object BlockBody* = object
transactions*: seq[Transaction] transactions*: seq[Transaction]

View File

@ -318,25 +318,28 @@ proc append*(w: var RlpWriter, h: BlockHeader) =
var len = 15 var len = 15
if h.fee.isSome: inc len if h.fee.isSome: inc len
if h.withdrawalsRoot.isSome: inc len if h.withdrawalsRoot.isSome: inc len
if h.excessDataGas.isSome: inc len
w.startList(len) w.startList(len)
for k, v in fieldPairs(h): for k, v in fieldPairs(h):
when k notin ["fee", "withdrawalsRoot"]: when v isnot Option:
w.append(v) w.append(v)
if h.fee.isSome: if h.fee.isSome:
w.append(h.fee.get()) w.append(h.fee.get())
if h.withdrawalsRoot.isSome: if h.withdrawalsRoot.isSome:
w.append(h.withdrawalsRoot.get()) w.append(h.withdrawalsRoot.get())
if h.excessDataGas.isSome:
w.append(h.excessDataGas.get())
proc read*(rlp: var Rlp, T: type BlockHeader): T = proc read*(rlp: var Rlp, T: type BlockHeader): T =
let len = rlp.listLen let len = rlp.listLen
if len notin {15, 16, 17}: if len notin {15, 16, 17, 18}:
raise newException(UnsupportedRlpError, raise newException(UnsupportedRlpError,
"BlockHeader elems should be 15, 16, or 17 got " & $len) "BlockHeader elems should be 15, 16, 17, or 18 got " & $len)
rlp.tryEnterList() rlp.tryEnterList()
for k, v in fieldPairs(result): for k, v in fieldPairs(result):
when k notin ["fee", "withdrawalsRoot"]: when v isnot Option:
v = rlp.read(type v) v = rlp.read(type v)
if len >= 16: if len >= 16:
@ -345,6 +348,9 @@ proc read*(rlp: var Rlp, T: type BlockHeader): T =
if len >= 17: if len >= 17:
# EIP-4895 # EIP-4895
result.withdrawalsRoot = some rlp.read(Hash256) result.withdrawalsRoot = some rlp.read(Hash256)
if len >= 18:
# EIP-4844
result.excessDataGas = some rlp.read(GasInt)
proc rlpHash*[T](v: T): Hash256 = proc rlpHash*[T](v: T): Hash256 =
keccakHash(rlp.encode(v)) keccakHash(rlp.encode(v))

View File

@ -97,11 +97,27 @@ proc suite2() =
for i in 0..<10: for i in 0..<10:
loadFile(i) loadFile(i)
test "rlp roundtrip EIP1559": test "rlp roundtrip EIP1559 / EIP4895 / EIP4844":
var h: BlockHeader proc doTest(h: BlockHeader) =
let xy = rlp.encode(h) let xy = rlp.encode(h)
let hh = rlp.decode(xy, BlockHeader) let hh = rlp.decode(xy, BlockHeader)
check h == hh check h == hh
var h: BlockHeader
doTest h
# EIP-1559
h.fee = some 1234.u256
doTest h
# EIP-4895
h.withdrawalsRoot = some Hash256.fromHex(
"0x7a64245f7f95164f6176d90bd4903dbdd3e5433d555dd1385e81787f9672c588")
doTest h
# EIP-4844
h.excessDataGas = some GasInt(1337)
doTest h
suite1() suite1()
suite2() suite2()