Replace exception with assertion in rlp.append of BlockHeader

Using exception in rlp.append turn out creates more problem than it solve.
`blockHash` operation is literally everywhere in nimbus-eth1, nimbus-eth2, and fluffy-code.
The compiler begins to complain about unlisted exception.
Rlp bytes coming from network and test vectors already verified by the decoder.
So, any invalid optional fields of blockheader that come into rlp.append means
programming error. Assertion will be more apropiate in this situation and less problematic.
This commit is contained in:
jangko 2023-05-25 15:27:37 +07:00
parent 7ff6de2367
commit 67bbd88616
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 8 additions and 11 deletions

View File

@ -318,14 +318,11 @@ 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: if h.withdrawalsRoot.isSome:
if h.fee.isNone: doAssert(h.fee.isSome, "baseFee expected")
raise newException(RlpError, "baseFee expected")
inc len inc len
if h.excessDataGas.isSome: if h.excessDataGas.isSome:
if h.fee.isNone: doAssert(h.fee.isSome, "baseFee expected")
raise newException(RlpError, "baseFee expected") doAssert(h.withdrawalsRoot.isSome, "withdrawalsRoot expected")
if h.withdrawalsRoot.isNone:
raise newException(RlpError, "withdrawalsRoot expected")
inc len inc len
w.startList(len) w.startList(len)
for k, v in fieldPairs(h): for k, v in fieldPairs(h):

View File

@ -2,7 +2,7 @@
import import
std/[os, strutils], std/[os, strutils],
stew/[io2, results], stew/[io2, results, shims/stddefects],
unittest2, unittest2,
../../eth/[rlp, common] ../../eth/[rlp, common]
@ -67,7 +67,7 @@ suite "BlockHeader roundtrip test":
test "Header + none(baseFee) + some(withdrawalsRoot)": test "Header + none(baseFee) + some(withdrawalsRoot)":
let h = BlockHeader(withdrawalsRoot: some(Hash256())) let h = BlockHeader(withdrawalsRoot: some(Hash256()))
expect RlpError: expect AssertionDefect:
roundTrip(h) roundTrip(h)
test "Header + none(baseFee) + some(withdrawalsRoot) + some(excessDataGas)": test "Header + none(baseFee) + some(withdrawalsRoot) + some(excessDataGas)":
@ -75,14 +75,14 @@ suite "BlockHeader roundtrip test":
withdrawalsRoot: some(Hash256()), withdrawalsRoot: some(Hash256()),
excessDataGas: some(1.u256) excessDataGas: some(1.u256)
) )
expect RlpError: expect AssertionDefect:
roundTrip(h) roundTrip(h)
test "Header + none(baseFee) + none(withdrawalsRoot) + some(excessDataGas)": test "Header + none(baseFee) + none(withdrawalsRoot) + some(excessDataGas)":
let h = BlockHeader( let h = BlockHeader(
excessDataGas: some(1.u256) excessDataGas: some(1.u256)
) )
expect RlpError: expect AssertionDefect:
roundTrip(h) roundTrip(h)
test "Header + some(baseFee) + none(withdrawalsRoot) + some(excessDataGas)": test "Header + some(baseFee) + none(withdrawalsRoot) + some(excessDataGas)":
@ -90,7 +90,7 @@ suite "BlockHeader roundtrip test":
fee: some(2.u256), fee: some(2.u256),
excessDataGas: some(1.u256) excessDataGas: some(1.u256)
) )
expect RlpError: expect AssertionDefect:
roundTrip(h) roundTrip(h)
test "Header + some(baseFee) + some(withdrawalsRoot)": test "Header + some(baseFee) + some(withdrawalsRoot)":