Small cleanup/refactor on Portal history network (#2521)

This commit is contained in:
Kim De Mey 2024-07-24 20:42:12 +02:00 committed by GitHub
parent 1841fe7c4a
commit 3a7f025fd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 20 deletions

View File

@ -9,7 +9,6 @@
import std/math, nimcrypto/hash, ssz_serialization import std/math, nimcrypto/hash, ssz_serialization
from beacon_chain/spec/datatypes/capella import Withdrawal
from beacon_chain/spec/presets/mainnet import MAX_WITHDRAWALS_PER_PAYLOAD from beacon_chain/spec/presets/mainnet import MAX_WITHDRAWALS_PER_PAYLOAD
export ssz_serialization, hash export ssz_serialization, hash
@ -21,7 +20,7 @@ const
MAX_TRANSACTION_LENGTH = 2 ^ 24 # ~= 16 million MAX_TRANSACTION_LENGTH = 2 ^ 24 # ~= 16 million
MAX_TRANSACTION_COUNT = 2 ^ 14 # ~= 16k MAX_TRANSACTION_COUNT = 2 ^ 14 # ~= 16k
MAX_RECEIPT_LENGTH = 2 ^ 27 # ~= 134 million MAX_RECEIPT_LENGTH = 2 ^ 27 # ~= 134 million
MAX_HEADER_LENGTH = 2 ^ 13 # = 8192 MAX_HEADER_LENGTH = 2 ^ 11 # = 2048
MAX_ENCODED_UNCLES_LENGTH = MAX_HEADER_LENGTH * 2 ^ 4 # = 2 ^ 17 ~= 131k MAX_ENCODED_UNCLES_LENGTH = MAX_HEADER_LENGTH * 2 ^ 4 # = 2 ^ 17 ~= 131k
MAX_WITHDRAWAL_LENGTH = 64 MAX_WITHDRAWAL_LENGTH = 64
MAX_WITHDRAWALS_COUNT = MAX_WITHDRAWALS_PER_PAYLOAD MAX_WITHDRAWALS_COUNT = MAX_WITHDRAWALS_PER_PAYLOAD
@ -42,31 +41,31 @@ type
accumulatorProof*: AccumulatorProof accumulatorProof*: AccumulatorProof
BlockHeaderWithProof* = object BlockHeaderWithProof* = object
header*: List[byte, 2048] # RLP data header*: ByteList[MAX_HEADER_LENGTH] # RLP data
proof*: BlockHeaderProof proof*: BlockHeaderProof
## BlockBody types ## BlockBody types
TransactionByteList* = List[byte, MAX_TRANSACTION_LENGTH] # RLP data TransactionByteList* = ByteList[MAX_TRANSACTION_LENGTH] # RLP data
Transactions* = List[TransactionByteList, MAX_TRANSACTION_COUNT] Transactions* = List[TransactionByteList, MAX_TRANSACTION_COUNT]
Uncles* = List[byte, MAX_ENCODED_UNCLES_LENGTH] # RLP data Uncles* = ByteList[MAX_ENCODED_UNCLES_LENGTH] # RLP data
WithdrawalByteList* = List[byte, MAX_WITHDRAWAL_LENGTH] # RLP data WithdrawalByteList* = ByteList[MAX_WITHDRAWAL_LENGTH] # RLP data
Withdrawals* = List[WithdrawalByteList, MAX_WITHDRAWALS_COUNT] Withdrawals* = List[WithdrawalByteList, MAX_WITHDRAWALS_COUNT]
# Pre-shanghai block body # Pre-shanghai block body
PortalBlockBodyLegacy* = object PortalBlockBodyLegacy* = object
transactions*: Transactions transactions*: Transactions
uncles*: Uncles # Post Paris/TheMerge, this list is required to be empty uncles*: Uncles # Post Paris/TheMerge, this RLP list must be empty
# Post-shanghai block body # Post-shanghai block body
PortalBlockBodyShanghai* = object PortalBlockBodyShanghai* = object
transactions*: Transactions transactions*: Transactions
uncles*: Uncles # Must be empty list uncles*: Uncles # Must be empty RLP list
withdrawals*: Withdrawals # new field withdrawals*: Withdrawals # new field
## Receipts types ## Receipts types
ReceiptByteList* = List[byte, MAX_RECEIPT_LENGTH] # RLP data ReceiptByteList* = ByteList[MAX_RECEIPT_LENGTH] # RLP data
PortalReceipts* = List[ReceiptByteList, MAX_TRANSACTION_COUNT] PortalReceipts* = List[ReceiptByteList, MAX_TRANSACTION_COUNT]
func init*(T: type BlockHeaderProof, proof: AccumulatorProof): T = func init*(T: type BlockHeaderProof, proof: AccumulatorProof): T =

View File

@ -72,7 +72,7 @@ func fromPortalBlockBody*(
ok( ok(
BlockBody( BlockBody(
transactions: transactions, transactions: transactions,
uncles: @[], # Uncles must be empty: TODO where validation? uncles: @[], # Uncles must be empty, this is verified in `validateBlockBody`
withdrawals: Opt.some(withdrawals), withdrawals: Opt.some(withdrawals),
) )
) )
@ -85,7 +85,6 @@ func fromPortalBlockBodyOrRaise*(
## Get the EL BlockBody from one of the SSZ-decoded Portal BlockBody types. ## Get the EL BlockBody from one of the SSZ-decoded Portal BlockBody types.
## Will raise Assertion in case of invalid RLP encodings. Only use of data ## Will raise Assertion in case of invalid RLP encodings. Only use of data
## has been validated before! ## has been validated before!
# TODO: Using ValueOr here gives compile error
let res = BlockBody.fromPortalBlockBody(body) let res = BlockBody.fromPortalBlockBody(body)
if res.isOk(): if res.isOk():
res.get() res.get()
@ -149,11 +148,6 @@ func encode*(blockBody: BlockBody): seq[byte] =
else: else:
SSZ.encode(PortalBlockBodyLegacy.fromBlockBody(blockBody)) SSZ.encode(PortalBlockBodyLegacy.fromBlockBody(blockBody))
func encode*(blockBody: BlockBody, T: type PortalBlockBodyShanghai): seq[byte] =
let portalBlockBody = PortalBlockBodyShanghai.fromBlockBody(blockBody)
SSZ.encode(portalBlockBody)
func encode*(receipts: seq[Receipt]): seq[byte] = func encode*(receipts: seq[Receipt]): seq[byte] =
let portalReceipts = PortalReceipts.fromReceipts(receipts) let portalReceipts = PortalReceipts.fromReceipts(receipts)
@ -168,9 +162,9 @@ proc calcRootHash(items: Transactions | PortalReceipts | Withdrawals): Hash256 =
for i, item in items: for i, item in items:
try: try:
tr.put(rlp.encode(i.uint), item.asSeq()) tr.put(rlp.encode(i.uint), item.asSeq())
except CatchableError as e: except RlpError as e:
# tr.put now is a generic interface to whatever underlying db # RlpError should not occur
# and it can raise exception if the backend db is something like aristo # TODO: trace down why it might raise this
raiseAssert(e.msg) raiseAssert(e.msg)
return tr.rootHash return tr.rootHash
@ -310,7 +304,8 @@ proc validateReceipts*(
proc validateReceiptsBytes*( proc validateReceiptsBytes*(
bytes: openArray[byte], receiptsRoot: KeccakHash bytes: openArray[byte], receiptsRoot: KeccakHash
): Result[seq[Receipt], string] = ): Result[seq[Receipt], string] =
## Fully decode the SSZ Block Body and validate it against the header. ## Fully decode the SSZ encoded receipts and validate it against the header's
## receipts root.
let receipts = ?decodeSsz(bytes, PortalReceipts) let receipts = ?decodeSsz(bytes, PortalReceipts)
?validateReceipts(receipts, receiptsRoot) ?validateReceipts(receipts, receiptsRoot)