From c9fcab8052747a6465cf44ca21954fcdf75949bd Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 13 Dec 2022 20:53:05 +0100 Subject: [PATCH] extend `BlockHeader` for EIP-4844 (#570) Extends `BlockHeader` with `excessDataGas` according to EIP-4844 (used by Nimbus-CL in empty block prod fallback). --- eth/common/eth_types.nim | 1 + eth/common/eth_types_rlp.nim | 14 ++++++++++---- tests/rlp/test_common.nim | 24 ++++++++++++++++++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/eth/common/eth_types.nim b/eth/common/eth_types.nim index 5e59c96..a9b72d1 100644 --- a/eth/common/eth_types.nim +++ b/eth/common/eth_types.nim @@ -121,6 +121,7 @@ type # `baseFee` is the get/set of `fee` fee*: Option[UInt256] # EIP-1559 withdrawalsRoot*: Option[Hash256] # EIP-4895 + excessDataGas*: Option[GasInt] # EIP-4844 BlockBody* = object transactions*: seq[Transaction] diff --git a/eth/common/eth_types_rlp.nim b/eth/common/eth_types_rlp.nim index b6cb4aa..4746196 100644 --- a/eth/common/eth_types_rlp.nim +++ b/eth/common/eth_types_rlp.nim @@ -318,25 +318,28 @@ proc append*(w: var RlpWriter, h: BlockHeader) = var len = 15 if h.fee.isSome: inc len if h.withdrawalsRoot.isSome: inc len + if h.excessDataGas.isSome: inc len w.startList(len) for k, v in fieldPairs(h): - when k notin ["fee", "withdrawalsRoot"]: + when v isnot Option: w.append(v) if h.fee.isSome: w.append(h.fee.get()) if h.withdrawalsRoot.isSome: w.append(h.withdrawalsRoot.get()) + if h.excessDataGas.isSome: + w.append(h.excessDataGas.get()) proc read*(rlp: var Rlp, T: type BlockHeader): T = let len = rlp.listLen - if len notin {15, 16, 17}: + if len notin {15, 16, 17, 18}: 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() for k, v in fieldPairs(result): - when k notin ["fee", "withdrawalsRoot"]: + when v isnot Option: v = rlp.read(type v) if len >= 16: @@ -345,6 +348,9 @@ proc read*(rlp: var Rlp, T: type BlockHeader): T = if len >= 17: # EIP-4895 result.withdrawalsRoot = some rlp.read(Hash256) + if len >= 18: + # EIP-4844 + result.excessDataGas = some rlp.read(GasInt) proc rlpHash*[T](v: T): Hash256 = keccakHash(rlp.encode(v)) diff --git a/tests/rlp/test_common.nim b/tests/rlp/test_common.nim index 44ebfa4..2c0179a 100644 --- a/tests/rlp/test_common.nim +++ b/tests/rlp/test_common.nim @@ -97,11 +97,27 @@ proc suite2() = for i in 0..<10: loadFile(i) - test "rlp roundtrip EIP1559": + test "rlp roundtrip EIP1559 / EIP4895 / EIP4844": + proc doTest(h: BlockHeader) = + let xy = rlp.encode(h) + let hh = rlp.decode(xy, BlockHeader) + check h == hh + var h: BlockHeader - let xy = rlp.encode(h) - let hh = rlp.decode(xy, BlockHeader) - check h == hh + 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() suite2()