@@ -15,8 +15,11 @@ import
1515 results,
1616 std/ [tables, algorithm],
1717 ../../ common,
18- ../../ db/ core_db,
19- ../../ db/ fcu_db,
18+ ../../ db/ [core_db, fcu_db],
19+ ../../ evm/ types,
20+ ../../ evm/ state,
21+ ../ validate,
22+ ../../ portal/ portal,
2023 ./ forked_chain/ [
2124 chain_desc,
2225 chain_branch,
@@ -666,6 +669,12 @@ func txRecords*(c: ForkedChainRef, txHash: Hash32): (Hash32, uint64) =
666669func isInMemory * (c: ForkedChainRef , blockHash: Hash32 ): bool =
667670 c.hashToBlock.hasKey (blockHash)
668671
672+ func isHistoryExpiryActive * (c: ForkedChainRef ): bool =
673+ not c.portal.isNil
674+
675+ func isPortalActive (c: ForkedChainRef ): bool =
676+ (not c.portal.isNil) and c.portal.portalEnabled
677+
669678func memoryBlock * (c: ForkedChainRef , blockHash: Hash32 ): BlockDesc =
670679 c.hashToBlock.withValue (blockHash, loc):
671680 return loc.branch.blocks[loc.index]
@@ -702,15 +711,20 @@ proc headerByNumber*(c: ForkedChainRef, number: BlockNumber): Result[Header, str
702711 return err (" Requested block number not exists: " & $ number)
703712
704713 if number < c.baseBranch.tailNumber:
705- return c.baseTxFrame.getBlockHeader (number)
714+ let hdr = c.baseTxFrame.getBlockHeader (number).valueOr:
715+ if c.isPortalActive:
716+ return c.portal.getHeaderByNumber (number)
717+ else :
718+ return err (" Portal inactive, block not found, number = " & $ number)
719+ return ok (hdr)
706720
707721 var branch = c.activeBranch
708722 while not branch.isNil:
709723 if number >= branch.tailNumber:
710724 return ok (branch.blocks[number - branch.tailNumber].blk.header)
711725 branch = branch.parent
712726
713- err (" Header not found, number = " & $ number)
727+ err (" Block not found, number = " & $ number)
714728
715729func finalizedHeader * (c: ForkedChainRef ): Header =
716730 c.hashToBlock.withValue (c.pendingFCU, loc):
@@ -739,7 +753,12 @@ func safeBlock*(c: ForkedChainRef): Block =
739753proc headerByHash * (c: ForkedChainRef , blockHash: Hash32 ): Result [Header , string ] =
740754 c.hashToBlock.withValue (blockHash, loc):
741755 return ok (loc[].header)
742- c.baseTxFrame.getBlockHeader (blockHash)
756+ let hdr = c.baseTxFrame.getBlockHeader (blockHash).valueOr:
757+ if c.isPortalActive:
758+ return c.portal.getHeaderByHash (blockHash)
759+ else :
760+ return err (" Block header not found" )
761+ ok (hdr)
743762
744763proc txDetailsByTxHash * (c: ForkedChainRef , txHash: Hash32 ): Result [(Hash32 , uint64 ), string ] =
745764 if c.txRecords.hasKey (txHash):
@@ -751,15 +770,9 @@ proc txDetailsByTxHash*(c: ForkedChainRef, txHash: Hash32): Result[(Hash32, uint
751770 header = ? c.headerByNumber (txDetails.blockNumber)
752771 blockHash = header.computeBlockHash
753772 return ok ((blockHash, txDetails.index))
754-
755- proc blockByHash * (c: ForkedChainRef , blockHash: Hash32 ): Result [Block , string ] =
756- # used by getPayloadBodiesByHash
757- # https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#specification-3
758- # 4. Client software MAY NOT respond to requests for finalized blocks by hash.
759- c.hashToBlock.withValue (blockHash, loc):
760- return ok (loc[].blk)
761- c.baseTxFrame.getEthBlock (blockHash)
762-
773+
774+ # TODO : Doesn't fetch data from portal
775+ # Aristo returns empty txs for both non-existent blocks and existing blocks with no txs [ Solve ? ]
763776proc blockBodyByHash * (c: ForkedChainRef , blockHash: Hash32 ): Result [BlockBody , string ] =
764777 c.hashToBlock.withValue (blockHash, loc):
765778 let blk = loc[].blk
@@ -770,12 +783,32 @@ proc blockBodyByHash*(c: ForkedChainRef, blockHash: Hash32): Result[BlockBody, s
770783 ))
771784 c.baseTxFrame.getBlockBody (blockHash)
772785
786+ proc blockByHash * (c: ForkedChainRef , blockHash: Hash32 ): Result [Block , string ] =
787+ # used by getPayloadBodiesByHash
788+ # https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/shanghai.md#specification-3
789+ # 4. Client software MAY NOT respond to requests for finalized blocks by hash.
790+ c.hashToBlock.withValue (blockHash, loc):
791+ return ok (loc[].blk)
792+ let blk = c.baseTxFrame.getEthBlock (blockHash)
793+ # Serves portal data if block not found in db
794+ if blk.isErr or (blk.get.transactions.len == 0 and blk.get.header.transactionsRoot != zeroHash32):
795+ if c.isPortalActive:
796+ return c.portal.getBlockByHash (blockHash)
797+ blk
798+
773799proc blockByNumber * (c: ForkedChainRef , number: BlockNumber ): Result [Block , string ] =
774800 if number > c.activeBranch.headNumber:
775801 return err (" Requested block number not exists: " & $ number)
776802
777803 if number <= c.baseBranch.tailNumber:
778- return c.baseTxFrame.getEthBlock (number)
804+ let blk = c.baseTxFrame.getEthBlock (number)
805+ # Txs not there in db - Happens during era1/era import, when we don't store txs and receipts
806+ if blk.isErr or (blk.get.transactions.len == 0 and blk.get.header.transactionsRoot != emptyRoot):
807+ # Serves portal data if block not found in database
808+ if c.isPortalActive:
809+ return c.portal.getBlockByNumber (number)
810+ else :
811+ return blk
779812
780813 var branch = c.activeBranch
781814 while not branch.isNil:
0 commit comments