Skip to content

Commit 11bc521

Browse files
authored
Fix invalid result error access + move empty txs check (#3649)
* Fix invalid result error access + move empty txs check Check for empty txs in getBlockTransactionData and getExecutionPayloadBodyV1. First step as more locations potentially require this on usage of also getBlockTransactions and getBlockTransactionHashes. Can also add additionally check for rootHash calc. * Fix era import by adjusting getUncleHashes to not use getBlockBody When era based import does not store block bodies (transactions here specifically), then this retrieval will fail when using getBlockBody. Rework instead to just get the uncles and not care about the other parts of the block body. * Fix txRoot check bug as zeroHash32 instead of emptyRoot was used zeroHash32 check was used originally and copied in this PR. This is incorrect as the root of an empty tree is not the same.
1 parent c35e3bd commit 11bc521

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

execution_chain/core/chain/forked_chain.nim

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -900,16 +900,15 @@ proc blockByHash*(c: ForkedChainRef, blockHash: Hash32): Result[Block, string] =
900900
c.hashToBlock.withValue(blockHash, loc):
901901
return ok(loc[].blk)
902902
var header = ?c.baseTxFrame.getBlockHeader(blockHash)
903-
var blockBody = c.baseTxFrame.getBlockBody(header)
904-
# Serves portal data if block not found in db
905-
if blockBody.isErr or (blockBody.get.transactions.len == 0 and header.transactionsRoot != zeroHash32):
903+
var blockBody = c.baseTxFrame.getBlockBody(header).valueOr:
904+
# Serve portal data if block not found in db
906905
if c.isPortalActive:
907906
var blockBodyPortal = ?c.portal.getBlockBodyByHeader(header)
908907
return ok(EthBlock.init(move(header), move(blockBodyPortal)))
909908
else:
910-
return err(blockBody.error)
909+
return err(error)
911910

912-
ok(EthBlock.init(move(header), move(blockBody.get())))
911+
ok(EthBlock.init(move(header), move(blockBody)))
913912

914913
proc payloadBodyV1ByHash*(c: ForkedChainRef, blockHash: Hash32): Result[ExecutionPayloadBodyV1, string] =
915914
c.hashToBlock.withValue(blockHash, loc):
@@ -918,8 +917,8 @@ proc payloadBodyV1ByHash*(c: ForkedChainRef, blockHash: Hash32): Result[Executio
918917
var header = ?c.baseTxFrame.getBlockHeader(blockHash)
919918
var blk = c.baseTxFrame.getExecutionPayloadBodyV1(header)
920919

921-
# Serves portal data if block not found in db
922-
if blk.isErr or (blk.get.transactions.len == 0 and header.transactionsRoot != zeroHash32):
920+
if blk.isErr:
921+
# Serve portal data if block not found in db
923922
if c.isPortalActive:
924923
var blockBodyPortal = ?c.portal.getBlockBodyByHeader(header)
925924
# Same as above
@@ -935,9 +934,8 @@ proc payloadBodyV1ByNumber*(c: ForkedChainRef, number: BlockNumber): Result[Exec
935934
var header = ?c.baseTxFrame.getBlockHeader(number)
936935
let blk = c.baseTxFrame.getExecutionPayloadBodyV1(header)
937936

938-
# Txs not there in db - Happens during era1/era import, when we don't store txs and receipts
939-
if blk.isErr or (blk.get.transactions.len == 0 and header.transactionsRoot != emptyRoot):
940-
# Serves portal data if block not found in database
937+
if blk.isErr:
938+
# Serve portal data if block not found in db
941939
if c.isPortalActive:
942940
var blockBodyPortal = ?c.portal.getBlockBodyByHeader(header)
943941
# same as above
@@ -957,17 +955,15 @@ proc blockByNumber*(c: ForkedChainRef, number: BlockNumber): Result[Block, strin
957955

958956
if number <= c.base.number:
959957
var header = ?c.baseTxFrame.getBlockHeader(number)
960-
var blockBody = c.baseTxFrame.getBlockBody(header)
961-
# Txs not there in db - Happens during era1/era import, when we don't store txs and receipts
962-
if blockBody.isErr or (blockBody.get.transactions.len == 0 and header.transactionsRoot != emptyRoot):
963-
# Serves portal data if block not found in database
958+
var blockBody = c.baseTxFrame.getBlockBody(header).valueOr:
959+
# Serve portal data if block not found in db
964960
if c.isPortalActive:
965961
var blockBodyPortal = ?c.portal.getBlockBodyByHeader(header)
966962
return ok(EthBlock.init(move(header), move(blockBodyPortal)))
967963
else:
968-
return err(blockBody.error)
969-
else:
970-
return ok(EthBlock.init(move(header), move(blockBody.get())))
964+
return err(error)
965+
966+
return ok(EthBlock.init(move(header), move(blockBody)))
971967

972968
loopIt(c.latest):
973969
if number >= it.number:

execution_chain/db/core_db/core_apps.nim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ proc getTransactions*(
402402
var res: seq[Transaction]
403403
for encodedTx in db.getBlockTransactionData(txRoot):
404404
res.add(rlp.decode(encodedTx, Transaction))
405+
406+
# Txs not there in db - Happens during era1/era import, when we don't store txs and receipts
407+
if (res.len == 0 and txRoot != emptyRoot):
408+
return err("No transactions found in db for txRoot " & $txRoot)
409+
405410
return ok(move(res))
406411

407412
proc getBlockBody*(
@@ -450,8 +455,10 @@ proc getUncleHashes*(
450455
): Result[seq[Hash32], string] =
451456
var res: seq[Hash32]
452457
for blockHash in blockHashes:
453-
let body = ?db.getBlockBody(blockHash)
454-
res &= body.uncles.mapIt(it.computeRlpHash)
458+
let header = ?db.getBlockHeader(blockHash)
459+
let uncles = ?db.getUncles(header.ommersHash)
460+
461+
res &= uncles.mapIt(it.computeRlpHash)
455462
ok(res)
456463

457464
proc getUncleHashes*(

execution_chain/db/payload_body_db.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ proc getExecutionPayloadBodyV1*(
4848
for encodedTx in db.getBlockTransactionData(header.txRoot):
4949
body.transactions.add TypedTransaction(encodedTx)
5050

51+
# Txs not there in db - Happens during era1/era import, when we don't store txs and receipts
52+
if (body.transactions.len == 0 and header.txRoot != emptyRoot):
53+
return err("No transactions found in db for txRoot " & $header.txRoot)
54+
5155
if header.withdrawalsRoot.isSome:
5256
let withdrawalsRoot = header.withdrawalsRoot.value
5357
if withdrawalsRoot == emptyRoot:

0 commit comments

Comments
 (0)