2022-04-11 10:00:39 +00:00
|
|
|
import
|
2023-09-06 09:18:26 +00:00
|
|
|
chronicles,
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/keys,
|
2023-09-06 09:18:26 +00:00
|
|
|
json_rpc/rpcclient,
|
|
|
|
../../../nimbus/config,
|
|
|
|
../../../nimbus/common,
|
|
|
|
./clmock,
|
|
|
|
./engine_client,
|
|
|
|
./client_pool,
|
|
|
|
./engine_env
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
export
|
2023-09-06 09:18:26 +00:00
|
|
|
clmock,
|
|
|
|
engine_client,
|
|
|
|
client_pool,
|
|
|
|
engine_env
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TestEnv* = ref object
|
2023-09-06 09:18:26 +00:00
|
|
|
conf : NimbusConf
|
|
|
|
chainFile : string
|
|
|
|
enableAuth: bool
|
|
|
|
port : int
|
|
|
|
rpcPort : int
|
|
|
|
clients : ClientPool
|
|
|
|
clMock* : CLMocker
|
|
|
|
vaultKey : PrivateKey
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
vaultKeyHex = "63b508a03c3b5937ceb903af8b1b0c191012ef6eb7e9c3fb7afa94e5d214d376"
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc makeEnv(conf: NimbusConf): TestEnv =
|
|
|
|
let env = TestEnv(
|
|
|
|
conf : conf,
|
|
|
|
port : 30303,
|
|
|
|
rpcPort: 8545,
|
|
|
|
clients: ClientPool(),
|
|
|
|
)
|
2022-06-17 00:53:33 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
env.vaultKey = PrivateKey.fromHex(vaultKeyHex).valueOr:
|
|
|
|
echo error
|
2022-04-11 10:00:39 +00:00
|
|
|
quit(QuitFailure)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
env
|
2022-07-18 04:34:42 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc addEngine(env: TestEnv, conf: var NimbusConf): EngineEnv =
|
|
|
|
conf.tcpPort = Port env.port
|
|
|
|
conf.udpPort = Port env.port
|
|
|
|
conf.rpcPort = Port env.rpcPort
|
|
|
|
let engine = newEngineEnv(conf, env.chainFile, env.enableAuth)
|
|
|
|
env.clients.add engine
|
|
|
|
inc env.port
|
|
|
|
inc env.rpcPort
|
|
|
|
engine
|
2022-07-18 04:34:42 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc setup(env: TestEnv, conf: var NimbusConf, chainFile: string, enableAuth: bool) =
|
|
|
|
env.chainFile = chainFile
|
|
|
|
env.enableAuth = enableAuth
|
|
|
|
env.conf = conf
|
|
|
|
discard env.addEngine(conf)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc new*(_: type TestEnv, conf: NimbusConf): TestEnv =
|
|
|
|
let env = makeEnv(conf)
|
|
|
|
env.setup(env.conf, "", false)
|
|
|
|
env
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc new*(_: type TestEnv, conf: ChainConfig): TestEnv =
|
|
|
|
let env = makeEnv(envConfig(conf))
|
|
|
|
env.setup(env.conf, "", false)
|
|
|
|
env
|
2022-06-17 00:53:33 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc new*(_: type TestEnv, chainFile: string, enableAuth: bool): TestEnv =
|
|
|
|
let env = makeEnv(envConfig())
|
|
|
|
env.setup(env.conf, chainFile, enableAuth)
|
|
|
|
env
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc close*(env: TestEnv) =
|
|
|
|
for eng in env.clients:
|
|
|
|
eng.close()
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
func client*(env: TestEnv): RpcHttpClient =
|
|
|
|
env.clients.first.client
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
func engine*(env: TestEnv): EngineEnv =
|
|
|
|
env.clients.first
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc setupCLMock*(env: TestEnv) =
|
|
|
|
env.clmock = newCLMocker(env.clients, env.engine.com)
|
2023-08-08 03:44:29 +00:00
|
|
|
|
2023-08-14 10:26:38 +00:00
|
|
|
proc addEngine*(env: TestEnv): EngineEnv =
|
|
|
|
var conf = env.conf # clone the conf
|
|
|
|
let eng = env.addEngine(conf)
|
|
|
|
eng.connect(env.engine.node)
|
|
|
|
eng
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc makeTx*(env: TestEnv, eng: EngineEnv, tc: BaseTx, nonce: AccountNonce): Transaction =
|
|
|
|
eng.makeTx(env.vaultKey, tc, nonce)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc makeTx*(env: TestEnv, eng: EngineEnv, tc: var BigInitcodeTx, nonce: AccountNonce): Transaction =
|
|
|
|
eng.makeTx(env.vaultKey, tc, nonce)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc sendNextTx*(env: TestEnv, eng: EngineEnv, tc: BaseTx): bool =
|
|
|
|
eng.sendNextTx(env.vaultKey, tc)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc sendTx*(env: TestEnv, eng: EngineEnv, tc: BaseTx, nonce: AccountNonce): bool =
|
|
|
|
eng.sendTx(env.vaultKey, tc, nonce)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc sendTx*(env: TestEnv, eng: EngineEnv, tc: BigInitcodeTx, nonce: AccountNonce): bool =
|
|
|
|
eng.sendTx(env.vaultKey, tc, nonce)
|
|
|
|
|
|
|
|
|
|
|
|
proc makeTx*(env: TestEnv, tc: BaseTx, nonce: AccountNonce): Transaction =
|
|
|
|
env.engine.makeTx(env.vaultKey, tc, nonce)
|
|
|
|
|
|
|
|
proc makeTx*(env: TestEnv, tc: var BigInitcodeTx, nonce: AccountNonce): Transaction =
|
|
|
|
env.engine.makeTx(env.vaultKey, tc, nonce)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc sendNextTx*(env: TestEnv, tc: BaseTx): bool =
|
|
|
|
env.engine.sendNextTx(env.vaultKey, tc)
|
|
|
|
|
|
|
|
proc sendTx*(env: TestEnv, tc: BaseTx, nonce: AccountNonce): bool =
|
|
|
|
env.engine.sendTx(env.vaultKey, tc, nonce)
|
|
|
|
|
|
|
|
proc sendTx*(env: TestEnv, tc: BigInitcodeTx, nonce: AccountNonce): bool =
|
|
|
|
env.engine.sendTx(env.vaultKey, tc, nonce)
|
|
|
|
|
|
|
|
proc sendTx*(env: TestEnv, tx: Transaction): bool =
|
|
|
|
env.engine.sendTx(tx)
|
|
|
|
|
|
|
|
proc verifyPoWProgress*(env: TestEnv, lastBlockHash: common.Hash256): bool =
|
|
|
|
let res = waitFor env.client.verifyPoWProgress(lastBlockHash)
|
2022-05-29 04:23:03 +00:00
|
|
|
if res.isErr:
|
|
|
|
error "verify PoW Progress error", msg=res.error
|
|
|
|
return false
|
2022-06-14 06:04:12 +00:00
|
|
|
|
2022-05-29 04:23:03 +00:00
|
|
|
true
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
proc slotsToSafe*(env: TestEnv, x: int) =
|
|
|
|
env.clMock.slotsToSafe = x
|
|
|
|
|
|
|
|
proc slotsToFinalized*(env: TestEnv, x: int) =
|
|
|
|
env.clMock.slotsToFinalized = x
|