From 074edff1b4cdbc9989e2ee9f996437a28a3ba182 Mon Sep 17 00:00:00 2001 From: jangko Date: Fri, 18 Aug 2023 17:52:55 +0700 Subject: [PATCH] EIP-4788: Add parentBeaconBlockRoot field to BlockHeader --- eth/common/eth_types.nim | 1 + tests/common/test_eth_types.nim | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/eth/common/eth_types.nim b/eth/common/eth_types.nim index 7ebd89b1..ba942bbe 100644 --- a/eth/common/eth_types.nim +++ b/eth/common/eth_types.nim @@ -143,6 +143,7 @@ type withdrawalsRoot*: Option[Hash256] # EIP-4895 blobGasUsed*: Option[uint64] # EIP-4844 excessBlobGas*: Option[uint64] # EIP-4844 + parentBeaconBlockRoot*: Option[Hash256] # EIP-4788 BlockBody* = object transactions*: seq[Transaction] diff --git a/tests/common/test_eth_types.nim b/tests/common/test_eth_types.nim index 6e04628c..fe8f1ab6 100644 --- a/tests/common/test_eth_types.nim +++ b/tests/common/test_eth_types.nim @@ -4,7 +4,9 @@ import unittest2, nimcrypto/hash, serialization/testing/generic_suite, - ../../eth/common/[eth_types, eth_types_json_serialization] + ../../eth/common/[eth_types, eth_types_json_serialization], + ../../eth/common/eth_types_rlp, + ../../eth/rlp func `==`*(lhs, rhs: BlockHashOrNumber): bool = if lhs.isHash != rhs.isHash: @@ -15,6 +17,9 @@ func `==`*(lhs, rhs: BlockHashOrNumber): bool = else: lhs.number == rhs.number +const + testHash = Hash256.fromHex "0x7a64245f7f95164f6176d90bd4903dbdd3e5433d555dd1385e81787f9672c588" + suite "BlockHashOrNumber": test "construction": expect ValueError: @@ -53,8 +58,20 @@ suite "BlockHashOrNumber": "\"1209231231\"" test "EIP-4399 prevRandao field": - let hash = Hash256.fromHex "0x7a64245f7f95164f6176d90bd4903dbdd3e5433d555dd1385e81787f9672c588" var blk: BlockHeader - blk.prevRandao = hash + blk.prevRandao = testHash let res = blk.prevRandao - check hash == res + check testHash == res + + test "EIP-4788 parentBeaconBlockRoot field": + let header = BlockHeader( + fee: some(0.u256), + withdrawalsRoot: some(testHash), + blobGasUsed: some(1'u64), + excessBlobGas: some(2'u64), + parentBeaconBlockRoot: some(testHash), + ) + let rlpBytes = rlp.encode(header) + let dh = rlp.decode(rlpBytes, BlockHeader) + check dh.parentBeaconBlockRoot.isSome + check dh.parentBeaconBlockRoot.get == testHash