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

122 lines
3.3 KiB
Nim
Raw Normal View History

2022-05-31 08:42:01 +00:00
# Nimbus
# Copyright (c) 2021-2024 Status Research & Development GmbH
2022-05-31 08:42:01 +00:00
# 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, net],
2022-05-31 08:42:01 +00:00
eth/p2p as ethp2p,
stew/results,
chronos, json_rpc/[rpcserver, rpcclient],
../../../nimbus/sync/protocol,
2022-12-02 04:39:12 +00:00
../../../nimbus/common,
../../../nimbus/config,
../../../nimbus/rpc,
../../../nimbus/utils/utils,
2022-12-02 04:39:12 +00:00
../../../nimbus/core/[chain, tx_pool, sealer],
2022-05-31 08:42:01 +00:00
../../../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* = 30.gwei
2022-05-31 08:42:01 +00:00
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)
2022-12-02 04:39:12 +00:00
proc setupRpcServer(ctx: EthContext, com: CommonRef,
2022-05-31 08:42:01 +00:00
ethNode: EthereumNode, txPool: TxPoolRef,
conf: NimbusConf): RpcServer =
let rpcServer = newRpcHttpServer([initTAddress(conf.httpAddress, conf.httpPort)])
let oracle = Oracle.new(com)
2022-05-31 08:42:01 +00:00
setupCommonRpc(ethNode, conf, rpcServer)
setupEthRpc(ethNode, ctx, com, txPool, oracle, rpcServer)
2022-05-31 08:42:01 +00:00
rpcServer.start()
rpcServer
proc stopRpcHttpServer(srv: RpcServer) =
let rpcServer = RpcHttpServer(srv)
waitFor rpcServer.stop()
waitFor rpcServer.closeWait()
proc setupEnv*(): TestEnv =
let conf = makeConfig(@[
"--chaindb:archive",
2022-05-31 08:42:01 +00:00
# "--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",
# "--http-address:0.0.0.0",
"--http-port:8545",
2022-05-31 08:42:01 +00:00
])
let
ethCtx = newEthContext()
ethNode = setupEthNode(conf, ethCtx, eth)
Unified database frontend integration (#1670) * Nimbus folder environment update details: * Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder. * The `nimbus` program does not compile yet as it needs the updates in the parallel `stateless` sub-folder. * Stateless environment update details: * Integrated `CoreDbRef` for the sources in the `stateless` sub-folder. * The `nimbus` program compiles now. * Premix environment update details: * Integrated `CoreDbRef` for the sources in the `premix` sub-folder. * Fluffy environment update details: * Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder. * Tools environment update details: * Integrated `CoreDbRef` for the sources in the `tools` sub-folder. * Nodocker environment update details: * Integrated `CoreDbRef` for the sources in the `hive_integration/nodocker` sub-folder. * Tests environment update details: * Integrated `CoreDbRef` for the sources in the `tests` sub-folder. * The unit tests compile and run cleanly now. * Generalise `CoreDbRef` to any `select_backend` supported database why: Generalisation was just missed due to overcoming some compiler oddity which was tied to rocksdb for testing. * Suppress compiler warning for `newChainDB()` why: Warning was added to this function which must be wrapped so that any `CatchableError` is re-raised as `Defect`. * Split off persistent `CoreDbRef` constructor into separate file why: This allows to compile a memory only database version without linking the backend library. * Use memory `CoreDbRef` database by default detail: Persistent DB constructor needs to import `db/core_db/persistent why: Most tests use memory DB anyway. This avoids linking `-lrocksdb` or any other backend by default. * fix `toLegacyBackend()` availability check why: got garbled after memory/persistent split. * Clarify raw access to MPT for snap sync handler why: Logically, `kvt` is not the raw access for the hexary trie (although this holds for the legacy database)
2023-08-04 11:10:09 +00:00
com = CommonRef.new(newCoreDbRef LegacyDbMemory,
conf.chainDbMode == ChainDbMode.Prune,
2022-05-31 08:42:01 +00:00
conf.networkId,
conf.networkParams
)
manageAccounts(ethCtx, conf)
2022-12-02 04:39:12 +00:00
com.initializeEmptyDb()
2022-05-31 08:42:01 +00:00
2022-12-02 04:39:12 +00:00
let chainRef = newChain(com)
let txPool = TxPoolRef.new(com, conf.engineSigner)
# txPool must be informed of active head
# so it can know the latest account state
let head = com.db.getCanonicalHead()
doAssert txPool.smartHead(head)
2022-05-31 08:42:01 +00:00
let sealingEngine = SealingEngineRef.new(
chainRef, ethCtx, conf.engineSigner,
txPool, EngineStopped
)
2022-12-02 04:39:12 +00:00
let rpcServer = setupRpcServer(ethCtx, com, ethNode, txPool, conf)
2022-05-31 08:42:01 +00:00
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)