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,
|
2023-09-06 09:18:26 +00:00
|
|
|
eth/keys,
|
|
|
|
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,
|
|
|
|
sync/protocol,
|
2023-08-14 10:26:38 +00:00
|
|
|
sync/beacon,
|
|
|
|
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-08-14 10:26:38 +00:00
|
|
|
sync : BeaconSyncRef
|
2023-10-23 13:59:57 +00:00
|
|
|
txPool : TxPoolRef
|
2024-05-19 03:08:05 +00:00
|
|
|
chain : ChainRef
|
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(@[
|
|
|
|
"--engine-signer:658bdf435d810c91414ec09147daa6db62406379",
|
|
|
|
"--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 =
|
|
|
|
if chainFile.len > 0:
|
|
|
|
# disable clique if we are using PoW chain
|
|
|
|
conf.networkParams.config.consensusType = ConsensusType.POW
|
|
|
|
|
|
|
|
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)
|
|
|
|
chain = newChain(com)
|
|
|
|
|
|
|
|
com.initializeEmptyDb()
|
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
|
|
|
|
let head = com.db.getCanonicalHead()
|
|
|
|
doAssert txPool.smartHead(head)
|
|
|
|
|
|
|
|
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-28 06:20:12 +00:00
|
|
|
sync = if com.ttd().isSome:
|
|
|
|
BeaconSyncRef.init(node, chain, ctx.rng, conf.maxPeers, id=conf.tcpPort.int)
|
|
|
|
else:
|
|
|
|
BeaconSyncRef(nil)
|
2023-09-06 09:18:26 +00:00
|
|
|
beaconEngine = BeaconEngineRef.new(txPool, chain)
|
2024-04-16 01:02:42 +00:00
|
|
|
oracle = Oracle.new(com)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2024-04-16 01:02:42 +00:00
|
|
|
setupEthRpc(node, ctx, com, txPool, oracle, server)
|
2023-09-06 09:18:26 +00:00
|
|
|
setupEngineAPI(beaconEngine, server)
|
2024-03-22 16:40:16 +00:00
|
|
|
setupDebugRpc(com, txPool, server)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
# Do not start clique sealing engine if we are using a Proof of Work chain file
|
|
|
|
if chainFile.len > 0:
|
|
|
|
if not importRlpBlock(chainFolder / chainFile, com):
|
|
|
|
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-09-28 06:20:12 +00:00
|
|
|
if com.ttd().isSome:
|
|
|
|
sync.start()
|
|
|
|
|
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,
|
2023-10-23 13:59:57 +00:00
|
|
|
sync : sync,
|
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-28 06:20:12 +00:00
|
|
|
if not env.sync.isNil:
|
|
|
|
env.sync.stop()
|
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
|
2023-09-06 09:18:26 +00:00
|
|
|
env.com.setTTD some(realTTD)
|
|
|
|
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.node.listeningAddress
|
|
|
|
$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
|
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc getTxsInPool*(env: EngineEnv, txHashes: openArray[common.Hash256]): 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 =
|
|
|
|
env.chain.setBlock(blk) == ValidationResult.OK
|