Skip to content

Commit 49f09d0

Browse files
authored
Portal: Add short paths in block body and receipts validation (#3720)
1 parent dd66afd commit 49f09d0

File tree

1 file changed

+55
-31
lines changed

1 file changed

+55
-31
lines changed

portal/network/history/history_validation.nim

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,76 @@ export history_content
1717
func validateBlockBody*(body: BlockBody, header: Header): Result[void, string] =
1818
## Validate the block body against the txRoot, ommersHash and withdrawalsRoot
1919
## from the header.
20-
## TODO: could add block number vs empty ommersHash + existing withdrawalsRoot check
21-
let calculatedOmmersHash = keccak256(rlp.encode(body.uncles))
22-
# TODO: avoid having to re-encode the uncles
23-
if calculatedOmmersHash != header.ommersHash:
24-
return err(
25-
"Invalid ommers hash: expected " & $header.ommersHash & " - got " &
26-
$calculatedOmmersHash
27-
)
20+
## The header is considered trusted, as in no checks are required whether the
21+
## header fields are valid according to the rules of the applicable forks.
2822

29-
let calculatedTxsRoot = orderedTrieRoot(body.transactions)
30-
if calculatedTxsRoot != header.txRoot:
31-
return err(
32-
"Invalid transactions root: expected " & $header.txRoot & " - got " &
33-
$calculatedTxsRoot
34-
)
23+
# Short-path in case of no uncles
24+
if header.ommersHash == EMPTY_UNCLE_HASH:
25+
if body.uncles.len > 0:
26+
return err("Invalid ommers: expected no uncles")
27+
else:
28+
let calculatedOmmersHash = keccak256(rlp.encode(body.uncles))
29+
# TODO: avoid having to re-encode the uncles
30+
if calculatedOmmersHash != header.ommersHash:
31+
return err(
32+
"Invalid ommers hash: expected " & $header.ommersHash & " - got " &
33+
$calculatedOmmersHash
34+
)
35+
36+
# Short-path in case of no transactions
37+
if header.txRoot == emptyRoot:
38+
if body.transactions.len > 0:
39+
return err("Invalid transactions: expected no transactions")
40+
else:
41+
let calculatedTxsRoot = orderedTrieRoot(body.transactions)
42+
if calculatedTxsRoot != header.txRoot:
43+
return err(
44+
"Invalid transactions root: expected " & $header.txRoot & " - got " &
45+
$calculatedTxsRoot
46+
)
3547

3648
if header.withdrawalsRoot.isSome() and body.withdrawals.isNone() or
3749
header.withdrawalsRoot.isNone() and body.withdrawals.isSome():
3850
return err("Invalid withdrawals")
3951

4052
if header.withdrawalsRoot.isSome() and body.withdrawals.isSome():
41-
let
42-
calculatedWithdrawalsRoot = orderedTrieRoot(body.withdrawals.value())
43-
headerWithdrawalsRoot = header.withdrawalsRoot.get()
44-
if calculatedWithdrawalsRoot != headerWithdrawalsRoot:
45-
return err(
46-
"Invalid withdrawals root: expected " & $headerWithdrawalsRoot & " - got " &
47-
$calculatedWithdrawalsRoot
48-
)
53+
let headerWithdrawalsRoot = header.withdrawalsRoot.value()
54+
# short-path in case of no withdrawals
55+
if headerWithdrawalsRoot == emptyRoot:
56+
if body.withdrawals.value().len > 0:
57+
return err("Invalid withdrawals: expected no withdrawals")
58+
else:
59+
let calculatedWithdrawalsRoot = orderedTrieRoot(body.withdrawals.value())
60+
if calculatedWithdrawalsRoot != headerWithdrawalsRoot:
61+
return err(
62+
"Invalid withdrawals root: expected " & $headerWithdrawalsRoot & " - got " &
63+
$calculatedWithdrawalsRoot
64+
)
4965

5066
ok()
5167

5268
func validateReceipts*(
5369
storedReceipts: StoredReceipts, header: Header
5470
): Result[void, string] =
55-
let receipts = storedReceipts.to(seq[Receipt])
56-
5771
## Validate the receipts against the receiptsRoot from the header.
58-
let calculatedReceiptsRoot = orderedTrieRoot(receipts)
59-
if calculatedReceiptsRoot != header.receiptsRoot:
60-
err(
61-
"Unexpected receipt root: expected " & $header.receiptsRoot & " - got " &
62-
$calculatedReceiptsRoot
63-
)
72+
73+
# Short-path in case of no receipts
74+
if header.receiptsRoot == emptyRoot:
75+
if storedReceipts.len > 0:
76+
err("Invalid receipts: expected no receipts")
77+
else:
78+
ok()
6479
else:
65-
ok()
80+
let receipts = storedReceipts.to(seq[Receipt])
81+
82+
let calculatedReceiptsRoot = orderedTrieRoot(receipts)
83+
if calculatedReceiptsRoot != header.receiptsRoot:
84+
err(
85+
"Unexpected receipt root: expected " & $header.receiptsRoot & " - got " &
86+
$calculatedReceiptsRoot
87+
)
88+
else:
89+
ok()
6690

6791
func validateContent*(
6892
content: BlockBody | StoredReceipts, header: Header

0 commit comments

Comments
 (0)