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],
|
2023-09-30 12:20:29 +00:00
|
|
|
stew/[results],
|
2023-09-06 09:18:26 +00:00
|
|
|
../../../nimbus/[
|
|
|
|
config,
|
|
|
|
constants,
|
|
|
|
core/sealer,
|
|
|
|
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
|
|
|
|
sealer : SealingEngineRef
|
|
|
|
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(
|
|
|
|
newCoreDbRef LegacyDbMemory,
|
2024-04-17 18:09:55 +00:00
|
|
|
conf.chainDbMode == ChainDbMode.Prune,
|
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()
|
|
|
|
let txPool = TxPoolRef.new(com, conf.engineSigner)
|
|
|
|
|
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-06 09:18:26 +00:00
|
|
|
sealer = SealingEngineRef.new(
|
|
|
|
chain, ctx, conf.engineSigner,
|
|
|
|
txPool, EngineStopped)
|
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)
|
|
|
|
elif not enableAuth:
|
|
|
|
sealer.start()
|
|
|
|
|
|
|
|
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,
|
|
|
|
sealer : sealer,
|
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.sealer.stop()
|
|
|
|
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
|
|
|
|
|
|
|
proc setBlock*(env: EngineEnv, header: common.BlockHeader, body: common.BlockBody): bool =
|
|
|
|
env.chain.setBlock(header, body) == ValidationResult.OK
|