Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend BlockHeader for Capella #562

Merged
merged 5 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions eth/common/eth_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,31 @@ type
status*: TransactionStatus
data*: Blob

Withdrawal* = object # EIP-4895
index* : uint64
validatorIndex*: uint64
address* : EthAddress
amount* : UInt256

BlockHeader* = object
parentHash*: Hash256
ommersHash*: Hash256
coinbase*: EthAddress
stateRoot*: Hash256
txRoot*: Hash256
receiptRoot*: Hash256
bloom*: BloomFilter
difficulty*: DifficultyInt
blockNumber*: BlockNumber
gasLimit*: GasInt
gasUsed*: GasInt
timestamp*: EthTime
extraData*: Blob
mixDigest*: Hash256
nonce*: BlockNonce
parentHash*: Hash256
ommersHash*: Hash256
coinbase*: EthAddress
stateRoot*: Hash256
txRoot*: Hash256
receiptRoot*: Hash256
bloom*: BloomFilter
difficulty*: DifficultyInt
blockNumber*: BlockNumber
gasLimit*: GasInt
gasUsed*: GasInt
timestamp*: EthTime
extraData*: Blob
mixDigest*: Hash256
nonce*: BlockNonce
# `baseFee` is the get/set of `fee`
fee*: Option[UInt256] # EIP-1559
fee*: Option[UInt256] # EIP-1559
withdrawalsRoot*: Option[Hash256] # EIP-4895

BlockBody* = object
transactions*: seq[Transaction]
Expand Down
27 changes: 21 additions & 6 deletions eth/common/eth_types_rlp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ proc append*(w: var RlpWriter, tx: Transaction) =
of TxEip1559:
w.appendTxEip1559(tx)

proc append*(w: var RlpWriter, withdrawal: Withdrawal) =
w.startList(4)
w.append(withdrawal.index)
w.append(withdrawal.validatorIndex)
w.append(withdrawal.address)
w.append(withdrawal.amount)

template read[T](rlp: var Rlp, val: var T)=
val = rlp.read(type val)

Expand Down Expand Up @@ -308,28 +315,36 @@ proc append*(rlpWriter: var RlpWriter, t: Time) {.inline.} =
rlpWriter.append(t.toUnix())

proc append*(w: var RlpWriter, h: BlockHeader) =
w.startList(if h.fee.isSome: 16 else: 15)
var len = 15
if h.fee.isSome: inc len
if h.withdrawalsRoot.isSome: inc len
w.startList(len)
for k, v in fieldPairs(h):
when k != "fee":
when k notin ["fee", "withdrawalsRoot"]:
w.append(v)
if h.fee.isSome:
w.append(h.fee.get())
if h.withdrawalsRoot.isSome:
w.append(h.withdrawalsRoot.get())

proc read*(rlp: var Rlp, T: type BlockHeader): T =
let len = rlp.listLen

if len notin {15, 16}:
if len notin {15, 16, 17}:
raise newException(UnsupportedRlpError,
"BlockHeader elems should be 15 or 16 got " & $len)
"BlockHeader elems should be 15, 16, or 17 got " & $len)

rlp.tryEnterList()
for k, v in fieldPairs(result):
when k != "fee":
when k notin ["fee", "withdrawalsRoot"]:
v = rlp.read(type v)

if len == 16:
if len >= 16:
# EIP-1559
result.baseFee = rlp.read(UInt256)
if len >= 17:
# EIP-4895
result.withdrawalsRoot = some rlp.read(Hash256)

proc rlpHash*[T](v: T): Hash256 =
keccakHash(rlp.encode(v))
Expand Down
13 changes: 13 additions & 0 deletions tests/rlp/test_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ proc suite1() =
check aa == a
check bb == b

test "EIP 4895 roundtrip":
let a = Withdrawal(
index: 1,
validatorIndex: 2,
address: EthAddress [
0.byte, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19],
amount: 4.u256 * 1_000_000_000.u256)

let abytes = rlp.encode(a)
let aa = rlp.decode(abytes, Withdrawal)
check aa == a

proc suite2() =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to remark to also add an additional test for a header with the withdrawalsRoot but it looks like we don't have header at all covered here (neither with the fee addition EIP in the past apparently), so never mind.
This probably (?) does get tested on nimbus-eth1 level, and you've got it covered on the consensus test part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, hence the additional test against the EF consensus-specs-tests, which includes withdrawals. It is a bit suboptimal as I wrote both sides (the implementation on consensus-specs, as well as the nimbus one), but this is a feature not currently being used, so problems would be discovered in internal devnets the latest.

suite "eip 2718 transaction":
for i in 0..<10:
Expand Down