nimbus-eth1/hive_integration/nodocker/rpc/test_env.nim

137 lines
3.8 KiB
Nim

# Nimbus
# Copyright (c) 2021 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import
std/[os],
eth/[trie/db],
eth/common as ethcommon,
eth/p2p as ethp2p,
stew/shims/net as stewNet,
stew/results,
chronos, json_rpc/[rpcserver, rpcclient],
../../../nimbus/db/db_chain,
../../../nimbus/sync/protocol,
../../../nimbus/[config, context, genesis, utils/tx_pool, sealer],
../../../nimbus/rpc/[common, p2p, debug],
../../../nimbus/p2p/chain,
../../../tests/test_helpers,
./vault
type
StopServerProc = proc(srv: RpcServer)
TestEnv* = ref object
vault*: Vault
rpcClient*: RpcClient
rpcServer: RpcServer
sealingEngine: SealingEngineRef
stopServer: StopServerProc
const
initPath = "hive_integration" / "nodocker" / "rpc" / "init"
gasPrice* = 30000000000 # 30 Gwei or 30 * pow(10, 9)
chainID* = ChainID(7)
proc manageAccounts(ctx: EthContext, conf: NimbusConf) =
if string(conf.importKey).len > 0:
let res = ctx.am.importPrivateKey(string conf.importKey)
if res.isErr:
echo res.error()
quit(QuitFailure)
proc setupRpcServer(ctx: EthContext, chainDB: BaseChainDB,
ethNode: EthereumNode, txPool: TxPoolRef,
conf: NimbusConf): RpcServer =
let rpcServer = newRpcHttpServer([initTAddress(conf.rpcAddress, conf.rpcPort)])
setupCommonRpc(ethNode, conf, rpcServer)
setupEthRpc(ethNode, ctx, chainDB, txPool, rpcServer)
rpcServer.start()
rpcServer
proc setupWsRpcServer(ctx: EthContext, chainDB: BaseChainDB,
ethNode: EthereumNode, txPool: TxPoolRef,
conf: NimbusConf): RpcServer =
let rpcServer = newRpcWebSocketServer(initTAddress(conf.wsAddress, conf.wsPort))
setupCommonRpc(ethNode, conf, rpcServer)
setupEthRpc(ethNode, ctx, chainDB, txPool, rpcServer)
rpcServer.start()
rpcServer
proc stopRpcHttpServer(srv: RpcServer) =
let rpcServer = RpcHttpServer(srv)
waitFor rpcServer.stop()
waitFor rpcServer.closeWait()
proc stopRpcWsServer(srv: RpcServer) =
let rpcServer = RpcWebSocketServer(srv)
rpcServer.stop()
waitFor rpcServer.closeWait()
proc setupEnv*(): TestEnv =
let conf = makeConfig(@[
"--prune-mode:archive",
# "--nat:extip:0.0.0.0",
"--network:7",
"--import-key:" & initPath / "private-key",
"--engine-signer:658bdf435d810c91414ec09147daa6db62406379",
"--custom-network:" & initPath / "genesis.json",
"--rpc",
"--rpc-api:eth,debug",
# "--rpc-address:0.0.0.0",
"--rpc-port:8545",
"--ws",
"--ws-api:eth,debug",
# "--ws-address:0.0.0.0",
"--ws-port:8546"
])
let
ethCtx = newEthContext()
ethNode = setupEthNode(conf, ethCtx, eth)
chainDB = newBaseChainDB(newMemoryDb(),
conf.pruneMode == PruneMode.Full,
conf.networkId,
conf.networkParams
)
manageAccounts(ethCtx, conf)
chainDB.populateProgress()
chainDB.initializeEmptyDb()
let chainRef = newChain(chainDB)
let txPool = TxPoolRef.new(chainDB, conf.engineSigner)
let sealingEngine = SealingEngineRef.new(
chainRef, ethCtx, conf.engineSigner,
txPool, EngineStopped
)
let rpcServer = setupRpcServer(ethCtx, chainDB, ethNode, txPool, conf)
let rpcClient = newRpcHttpClient()
waitFor rpcClient.connect("127.0.0.1", Port(8545), false)
let stopServer = stopRpcHttpServer
sealingEngine.start()
let t = TestEnv(
rpcClient: rpcClient,
sealingEngine: sealingEngine,
rpcServer: rpcServer,
vault : newVault(chainID, gasPrice, rpcClient),
stopServer: stopServer
)
result = t
proc stopEnv*(t: TestEnv) =
waitFor t.rpcClient.close()
waitFor t.sealingEngine.stop()
t.stopServer(t.rpcServer)