2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-01-29 13:20:04 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
import
|
2023-09-30 12:20:29 +00:00
|
|
|
std/os,
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/keys,
|
2023-09-06 09:18:26 +00:00
|
|
|
eth/p2p as eth_p2p,
|
2023-08-14 10:26:38 +00:00
|
|
|
chronos,
|
2023-09-06 09:18:26 +00:00
|
|
|
json_rpc/[rpcserver, rpcclient],
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2023-09-06 09:18:26 +00:00
|
|
|
../../../nimbus/[
|
|
|
|
config,
|
|
|
|
constants,
|
|
|
|
core/chain,
|
|
|
|
core/tx_pool,
|
2023-10-23 13:59:57 +00:00
|
|
|
core/tx_pool/tx_item,
|
2023-09-06 09:18:26 +00:00
|
|
|
core/block_import,
|
|
|
|
rpc,
|
2024-10-06 10:11:44 +00:00
|
|
|
sync/handlers,
|
2023-09-06 09:18:26 +00:00
|
|
|
beacon/beacon_engine,
|
2023-12-08 09:35:50 +00:00
|
|
|
beacon/web3_eth_conv,
|
2023-09-06 09:18:26 +00:00
|
|
|
common
|
|
|
|
],
|
2023-10-31 03:18:37 +00:00
|
|
|
../../../tests/test_helpers,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2024-05-19 03:08:05 +00:00
|
|
|
from ./node import setBlock
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
export
|
|
|
|
results
|
|
|
|
|
|
|
|
type
|
|
|
|
EngineEnv* = ref object
|
|
|
|
conf : NimbusConf
|
|
|
|
com : CommonRef
|
2023-08-14 10:26:38 +00:00
|
|
|
node : EthereumNode
|
2023-09-06 09:18:26 +00:00
|
|
|
server : RpcHttpServer
|
|
|
|
ttd : DifficultyInt
|
|
|
|
client : RpcHttpClient
|
2023-10-23 13:59:57 +00:00
|
|
|
txPool : TxPoolRef
|
2024-09-04 09:54:54 +00:00
|
|
|
chain : ForkedChainRef
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
baseFolder = "hive_integration/nodocker/engine"
|
|
|
|
genesisFile = baseFolder & "/init/genesis.json"
|
|
|
|
sealerKey = baseFolder & "/init/sealer.key"
|
|
|
|
chainFolder = baseFolder & "/chains"
|
|
|
|
jwtSecret = "0x7365637265747365637265747365637265747365637265747365637265747365"
|
|
|
|
|
|
|
|
proc makeCom*(conf: NimbusConf): CommonRef =
|
|
|
|
CommonRef.new(
|
2024-05-20 10:17:51 +00:00
|
|
|
newCoreDbRef DefaultDbMemory,
|
2023-09-06 09:18:26 +00:00
|
|
|
conf.networkId,
|
|
|
|
conf.networkParams
|
|
|
|
)
|
|
|
|
|
|
|
|
proc envConfig*(): NimbusConf =
|
2023-08-14 10:26:38 +00:00
|
|
|
makeConfig(@[
|
|
|
|
"--custom-network:" & genesisFile,
|
|
|
|
"--listen-address: 127.0.0.1",
|
|
|
|
])
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
proc envConfig*(conf: ChainConfig): NimbusConf =
|
|
|
|
result = envConfig()
|
|
|
|
result.networkParams.config = conf
|
|
|
|
|
|
|
|
proc newEngineEnv*(conf: var NimbusConf, chainFile: string, enableAuth: bool): EngineEnv =
|
|
|
|
let ctx = newEthContext()
|
|
|
|
ctx.am.importPrivateKey(sealerKey).isOkOr:
|
|
|
|
echo error
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2023-09-09 06:54:58 +00:00
|
|
|
let
|
2023-08-14 10:26:38 +00:00
|
|
|
node = setupEthNode(conf, ctx)
|
2023-09-06 09:18:26 +00:00
|
|
|
com = makeCom(conf)
|
2024-09-04 09:54:54 +00:00
|
|
|
head = com.db.getCanonicalHead()
|
|
|
|
chain = newForkedChain(com, head)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
let txPool = TxPoolRef.new(com)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2023-08-14 10:26:38 +00:00
|
|
|
node.addEthHandlerCapability(
|
|
|
|
node.peerPool,
|
|
|
|
chain,
|
|
|
|
txPool)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
# txPool must be informed of active head
|
|
|
|
# so it can know the latest account state
|
2024-09-04 09:54:54 +00:00
|
|
|
doAssert txPool.smartHead(head, chain)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
var key: JwtSharedKey
|
|
|
|
key.fromHex(jwtSecret).isOkOr:
|
|
|
|
echo "JWT SECRET ERROR: ", error
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
let
|
2023-08-14 10:26:38 +00:00
|
|
|
hooks = if enableAuth: @[httpJwtAuth(key)]
|
|
|
|
else: @[]
|
2024-01-29 13:20:04 +00:00
|
|
|
server = newRpcHttpServerWithParams("127.0.0.1:" & $conf.httpPort, hooks).valueOr:
|
|
|
|
echo "Failed to create rpc server: ", error
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
beaconEngine = BeaconEngineRef.new(txPool, chain)
|
2024-10-02 03:56:39 +00:00
|
|
|
serverApi = newServerAPI(chain, txPool)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2024-09-04 09:54:54 +00:00
|
|
|
setupServerAPI(serverApi, server)
|
2023-09-06 09:18:26 +00:00
|
|
|
setupEngineAPI(beaconEngine, server)
|
2024-09-04 09:54:54 +00:00
|
|
|
# temporary disabled
|
|
|
|
#setupDebugRpc(com, txPool, server)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
if chainFile.len > 0:
|
2024-09-04 09:54:54 +00:00
|
|
|
importRlpBlocks(chainFolder / chainFile, chain, true).isOkOr:
|
|
|
|
echo "Failed to import RLP blocks: ", error
|
2023-09-06 09:18:26 +00:00
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2024-01-29 13:20:04 +00:00
|
|
|
waitFor client.connect("127.0.0.1", conf.httpPort, false)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2023-08-14 10:26:38 +00:00
|
|
|
node.startListening()
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
EngineEnv(
|
|
|
|
conf : conf,
|
|
|
|
com : com,
|
2023-08-14 10:26:38 +00:00
|
|
|
node : node,
|
2023-09-06 09:18:26 +00:00
|
|
|
server : server,
|
2023-08-14 10:26:38 +00:00
|
|
|
client : client,
|
2024-05-19 03:08:05 +00:00
|
|
|
txPool : txPool,
|
|
|
|
chain : chain
|
2023-09-06 09:18:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc close*(env: EngineEnv) =
|
2023-09-09 06:54:58 +00:00
|
|
|
waitFor env.node.closeWait()
|
2023-09-06 09:18:26 +00:00
|
|
|
waitFor env.client.close()
|
|
|
|
waitFor env.server.closeWait()
|
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc setRealTTD*(env: EngineEnv) =
|
2023-09-06 09:18:26 +00:00
|
|
|
let genesis = env.com.genesisHeader
|
2023-10-31 03:18:37 +00:00
|
|
|
let realTTD = genesis.difficulty
|
2024-06-14 07:31:08 +00:00
|
|
|
env.com.setTTD Opt.some(realTTD)
|
2023-09-06 09:18:26 +00:00
|
|
|
env.ttd = realTTD
|
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
func httpPort*(env: EngineEnv): Port =
|
|
|
|
env.conf.httpPort
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
func client*(env: EngineEnv): RpcHttpClient =
|
|
|
|
env.client
|
|
|
|
|
|
|
|
func ttd*(env: EngineEnv): UInt256 =
|
|
|
|
env.ttd
|
|
|
|
|
|
|
|
func com*(env: EngineEnv): CommonRef =
|
|
|
|
env.com
|
2023-08-14 10:26:38 +00:00
|
|
|
|
|
|
|
func node*(env: EngineEnv): ENode =
|
|
|
|
env.node.listeningAddress
|
|
|
|
|
|
|
|
proc connect*(env: EngineEnv, node: ENode) =
|
|
|
|
waitFor env.node.connectToNode(node)
|
2023-10-23 13:59:57 +00:00
|
|
|
|
|
|
|
func ID*(env: EngineEnv): string =
|
2024-05-15 16:22:03 +00:00
|
|
|
$env.conf.httpPort
|
2023-10-23 13:59:57 +00:00
|
|
|
|
|
|
|
proc peer*(env: EngineEnv): Peer =
|
|
|
|
doAssert(env.node.numPeers > 0)
|
|
|
|
for peer in env.node.peers:
|
|
|
|
return peer
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getTxsInPool*(env: EngineEnv, txHashes: openArray[common.Hash32]): seq[Transaction] =
|
2023-10-23 13:59:57 +00:00
|
|
|
result = newSeqOfCap[Transaction](txHashes.len)
|
|
|
|
for txHash in txHashes:
|
|
|
|
let res = env.txPool.getItem(txHash)
|
|
|
|
if res.isErr: continue
|
|
|
|
let item = res.get
|
|
|
|
if item.reject == txInfoOk:
|
|
|
|
result.add item.tx
|
|
|
|
|
|
|
|
proc numTxsInPool*(env: EngineEnv): int =
|
|
|
|
env.txPool.numTxs
|
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
func version*(env: EngineEnv, time: EthTime): Version =
|
|
|
|
if env.com.isCancunOrLater(time):
|
|
|
|
Version.V3
|
2024-03-20 07:35:38 +00:00
|
|
|
elif env.com.isShanghaiOrLater(time):
|
2023-10-31 03:18:37 +00:00
|
|
|
Version.V2
|
|
|
|
else:
|
|
|
|
Version.V1
|
|
|
|
|
|
|
|
func version*(env: EngineEnv, time: Web3Quantity): Version =
|
|
|
|
env.version(time.EthTime)
|
|
|
|
|
|
|
|
func version*(env: EngineEnv, time: uint64): Version =
|
|
|
|
env.version(time.EthTime)
|
2024-05-19 03:08:05 +00:00
|
|
|
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
proc setBlock*(env: EngineEnv, blk: common.EthBlock): bool =
|
2024-09-04 09:54:54 +00:00
|
|
|
# env.chain.setBlock(blk).isOk()
|
|
|
|
debugEcho "TODO: fix setBlock"
|
|
|
|
false
|