# Nimbus # Copyright (c) 2023-2024 Status Research & Development GmbH # 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. import chronicles, eth/common/keys, results, json_rpc/rpcclient, ../../../nimbus/config, ../../../nimbus/common, ./clmock, ./engine_client, ./client_pool, ./engine_env, ./tx_sender, ./types, ./cancun/customizer export clmock, engine_client, client_pool, engine_env, tx_sender type TestEnv* = ref object conf : NimbusConf chainFile : string enableAuth: bool port : int httpPort : int clients : ClientPool sender : TxSender clMock* : CLMocker proc makeEnv(conf: NimbusConf): TestEnv = TestEnv( conf : conf, port : 30303, httpPort: 8545, clients : ClientPool(), sender : TxSender.new(conf.networkParams), ) proc addEngine(env: TestEnv, conf: var NimbusConf): EngineEnv = conf.tcpPort = Port env.port conf.udpPort = Port env.port conf.httpPort = Port env.httpPort let engine = newEngineEnv(conf, env.chainFile, env.enableAuth) env.clients.add engine inc env.port inc env.httpPort engine proc setup(env: TestEnv, conf: var NimbusConf, chainFile: string, enableAuth: bool) = env.chainFile = chainFile env.enableAuth = enableAuth env.conf = conf discard env.addEngine(conf) proc new*(_: type TestEnv, conf: NimbusConf): TestEnv = let env = makeEnv(conf) env.setup(env.conf, "", false) env proc new*(_: type TestEnv, conf: ChainConfig): TestEnv = let env = makeEnv(envConfig(conf)) env.setup(env.conf, "", false) env proc new*(_: type TestEnv, chainFile: string, enableAuth: bool): TestEnv = let env = makeEnv(envConfig()) env.setup(env.conf, chainFile, enableAuth) env proc close*(env: TestEnv) = for eng in env.clients: eng.close() func client*(env: TestEnv): RpcHttpClient = env.clients.first.client func engine*(env: TestEnv): EngineEnv = env.clients.first func sender*(env: TestEnv): TxSender = env.sender proc setupCLMock*(env: TestEnv) = env.clMock = newClMocker(env.engine, env.engine.com) proc addEngine*(env: TestEnv, addToCL: bool = true, connectBootNode: bool = true): EngineEnv = doAssert(env.clMock.isNil.not) var conf = env.conf # clone the conf let eng = env.addEngine(conf) if connectBootNode: eng.connect(env.engine.node) if addToCL: env.clMock.addEngine(eng) eng func engines*(env: TestEnv, idx: int): EngineEnv = env.clients[idx] func numEngines*(env: TestEnv): int = env.clients.len func accounts*(env: TestEnv, idx: int): TestAccount = env.sender.getAccount(idx) proc makeTx*( env: TestEnv, tc: BaseTx, nonce: AccountNonce): PooledTransaction = env.sender.makeTx(tc, nonce) proc makeTx*( env: TestEnv, tc: BigInitcodeTx, nonce: AccountNonce): PooledTransaction = env.sender.makeTx(tc, nonce) proc makeTxs*(env: TestEnv, tc: BaseTx, num: int): seq[PooledTransaction] = result = newSeqOfCap[PooledTransaction](num) for _ in 0..