Skip to content

Commit fd761ec

Browse files
authored
Eip4444 - Portal Integration (#3087)
* initial structuring of portal integration * prelim single point integration * remove fluffy taskpool + add only rpc layer * fix ci * remove unwanted changes * reorder * mainnet detection and limit setup * eip4444 limit * resolve impl change eth68 * fix initializer * seperate history expiry from portal * remove extra type * better warn message * simplify return * hide configureable params * fix: merge conflict * fix: remove return * remove unnecessary copy of the blk.body * fix with suggested changes * remove func export * resolve merge conflicts * fix
1 parent 626a233 commit fd761ec

File tree

9 files changed

+348
-22
lines changed

9 files changed

+348
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# Ignore dynamic, static libs and libtool archive files
1616
*.so
1717
*.dylib
18+
*.dSYM
1819
*.a
1920
*.la
2021
*.exe

execution_chain/common/common.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ func eip150Hash*(com: CommonRef): Hash32 =
362362
func daoForkBlock*(com: CommonRef): Opt[BlockNumber] =
363363
com.config.daoForkBlock
364364

365+
func posBlock*(com: CommonRef): Opt[BlockNumber] =
366+
com.config.posBlock
367+
365368
func daoForkSupport*(com: CommonRef): bool =
366369
com.config.daoForkSupport
367370

execution_chain/config.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,21 @@ type
445445
defaultValueDesc: $RpcFlag.Eth
446446
name: "ws-api" }: seq[string]
447447

448+
historyExpiry* {.
449+
desc: "Enable the data from Portal Network"
450+
defaultValue: false
451+
name: "history-expiry" }: bool
452+
453+
historyExpiryLimit* {.
454+
hidden
455+
desc: "Limit the number of blocks to be kept in history"
456+
name: "debug-history-expiry-limit" }: Option[BlockNumber]
457+
458+
portalUrl* {.
459+
desc: "URL of the Portal Network"
460+
defaultValue: ""
461+
name: "portal-url" }: string
462+
448463
engineApiEnabled* {.
449464
desc: "Enable the Engine API"
450465
defaultValue: false

execution_chain/core/chain/forked_chain.nim

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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) =
666669
func 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+
669678
func 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

715729
func finalizedHeader*(c: ForkedChainRef): Header =
716730
c.hashToBlock.withValue(c.pendingFCU, loc):
@@ -739,7 +753,12 @@ func safeBlock*(c: ForkedChainRef): Block =
739753
proc 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

744763
proc 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 ? ]
763776
proc 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+
773799
proc 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:

execution_chain/core/chain/forked_chain/chain_desc.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
{.push raises: [].}
1212

1313
import
14-
std/[tables],
14+
std/tables,
1515
../../../common,
16-
../../../db/core_db,
17-
../../../db/fcu_db,
16+
../../../db/[core_db, fcu_db],
17+
../../../portal/portal,
1818
./block_quarantine,
1919
./chain_branch
2020

@@ -66,6 +66,9 @@ type
6666
# When move forward, this is the minimum distance
6767
# to move the base. And the bulk writing can works
6868
# efficiently.
69+
70+
portal*: HistoryExpiryRef
71+
# History Expiry tracker and portal access entry point
6972

7073
fcuHead*: FcuHashAndNumber
7174
fcuSafe*: FcuHashAndNumber

execution_chain/nimbus_execution_client.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import
2828
./db/core_db/persistent,
2929
./db/storage_types,
3030
./sync/wire_protocol,
31-
./common/chain_config_hash
31+
./common/chain_config_hash,
32+
./portal/portal
3233

3334
from beacon_chain/nimbus_binary_common import setupFileLimits
3435

@@ -40,11 +41,14 @@ from beacon_chain/nimbus_binary_common import setupFileLimits
4041
proc basicServices(nimbus: NimbusNode,
4142
conf: NimbusConf,
4243
com: CommonRef) =
44+
# Setup the chain
4345
let fc = ForkedChainRef.init(com)
4446
fc.deserialize().isOkOr:
4547
warn "FC.deserialize", msg=error
4648

4749
nimbus.fc = fc
50+
# Setup history expiry and portal
51+
nimbus.fc.portal = HistoryExpiryRef.init(conf, com)
4852
# txPool must be informed of active head
4953
# so it can know the latest account state
5054
# e.g. sender nonce, etc

0 commit comments

Comments
 (0)