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.
|
|
|
|
|
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,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2023-09-06 09:18:26 +00:00
|
|
|
json_rpc/rpcclient,
|
|
|
|
../../../nimbus/config,
|
|
|
|
../../../nimbus/common,
|
|
|
|
./clmock,
|
|
|
|
./engine_client,
|
|
|
|
./client_pool,
|
2023-09-30 12:20:29 +00:00
|
|
|
./engine_env,
|
2023-10-31 03:18:37 +00:00
|
|
|
./tx_sender,
|
|
|
|
./types,
|
|
|
|
./cancun/customizer
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
export
|
2023-09-06 09:18:26 +00:00
|
|
|
clmock,
|
|
|
|
engine_client,
|
|
|
|
client_pool,
|
2023-09-30 12:20:29 +00:00
|
|
|
engine_env,
|
|
|
|
tx_sender
|
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
|
2024-01-29 13:20:04 +00:00
|
|
|
httpPort : int
|
2023-09-06 09:18:26 +00:00
|
|
|
clients : ClientPool
|
2023-09-30 12:20:29 +00:00
|
|
|
sender : TxSender
|
2023-09-06 09:18:26 +00:00
|
|
|
clMock* : CLMocker
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc makeEnv(conf: NimbusConf): TestEnv =
|
2023-09-30 12:20:29 +00:00
|
|
|
TestEnv(
|
2024-01-29 13:20:04 +00:00
|
|
|
conf : conf,
|
|
|
|
port : 30303,
|
|
|
|
httpPort: 8545,
|
|
|
|
clients : ClientPool(),
|
|
|
|
sender : TxSender.new(conf.networkParams),
|
2023-09-06 09:18:26 +00:00
|
|
|
)
|
2022-06-17 00:53:33 +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
|
2024-01-29 13:20:04 +00:00
|
|
|
conf.httpPort = Port env.httpPort
|
2023-09-06 09:18:26 +00:00
|
|
|
let engine = newEngineEnv(conf, env.chainFile, env.enableAuth)
|
|
|
|
env.clients.add engine
|
|
|
|
inc env.port
|
2024-01-29 13:20:04 +00:00
|
|
|
inc env.httpPort
|
2023-09-06 09:18:26 +00:00
|
|
|
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
|
|
|
|
2024-03-20 07:35:38 +00:00
|
|
|
func sender*(env: TestEnv): TxSender =
|
2023-10-31 03:18:37 +00:00
|
|
|
env.sender
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc setupCLMock*(env: TestEnv) =
|
2024-03-20 07:35:38 +00:00
|
|
|
env.clMock = newClMocker(env.engine, env.engine.com)
|
2023-08-08 03:44:29 +00:00
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
proc addEngine*(env: TestEnv, addToCL: bool = true, connectBootNode: bool = true): EngineEnv =
|
2023-09-30 12:20:29 +00:00
|
|
|
doAssert(env.clMock.isNil.not)
|
2023-08-14 10:26:38 +00:00
|
|
|
var conf = env.conf # clone the conf
|
|
|
|
let eng = env.addEngine(conf)
|
2023-10-23 13:59:57 +00:00
|
|
|
if connectBootNode:
|
|
|
|
eng.connect(env.engine.node)
|
2023-09-30 12:20:29 +00:00
|
|
|
if addToCL:
|
|
|
|
env.clMock.addEngine(eng)
|
2023-08-14 10:26:38 +00:00
|
|
|
eng
|
|
|
|
|
2023-10-19 03:28:52 +00:00
|
|
|
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)
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc makeTx*(
|
|
|
|
env: TestEnv, tc: BaseTx, nonce: AccountNonce): PooledTransaction =
|
2023-09-30 12:20:29 +00:00
|
|
|
env.sender.makeTx(tc, nonce)
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc makeTx*(
|
|
|
|
env: TestEnv, tc: BigInitcodeTx, nonce: AccountNonce): PooledTransaction =
|
2023-09-30 12:20:29 +00:00
|
|
|
env.sender.makeTx(tc, nonce)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc makeTxs*(env: TestEnv, tc: BaseTx, num: int): seq[PooledTransaction] =
|
|
|
|
result = newSeqOfCap[PooledTransaction](num)
|
2023-09-30 12:20:29 +00:00
|
|
|
for _ in 0..<num:
|
|
|
|
result.add env.sender.makeNextTx(tc)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc makeNextTx*(env: TestEnv, tc: BaseTx): PooledTransaction =
|
2023-11-01 11:09:49 +00:00
|
|
|
env.sender.makeNextTx(tc)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc sendNextTx*(env: TestEnv, eng: EngineEnv, tc: BaseTx): bool =
|
2023-09-30 12:20:29 +00:00
|
|
|
env.sender.sendNextTx(eng.client, tc)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-11-03 02:24:51 +00:00
|
|
|
proc sendNextTxs*(env: TestEnv, eng: EngineEnv, tc: BaseTx, num: int): bool =
|
|
|
|
for i in 0..<num:
|
|
|
|
if not env.sender.sendNextTx(eng.client, tc):
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc sendTx*(env: TestEnv, eng: EngineEnv, tc: BaseTx, nonce: AccountNonce): bool =
|
2023-09-30 12:20:29 +00:00
|
|
|
env.sender.sendTx(eng.client, 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 =
|
2023-09-30 12:20:29 +00:00
|
|
|
env.sender.sendTx(eng.client, tc, nonce)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc sendTxs*(
|
|
|
|
env: TestEnv, eng: EngineEnv, txs: openArray[PooledTransaction]): bool =
|
2023-09-30 12:20:29 +00:00
|
|
|
for tx in txs:
|
|
|
|
if not sendTx(eng.client, tx):
|
|
|
|
return false
|
|
|
|
true
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc sendNextTx*(env: TestEnv, tc: BaseTx): bool =
|
2023-09-30 12:20:29 +00:00
|
|
|
let client = env.engine.client
|
|
|
|
env.sender.sendNextTx(client, tc)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
proc sendTx*(env: TestEnv, tc: BaseTx, nonce: AccountNonce): bool =
|
2023-09-30 12:20:29 +00:00
|
|
|
let client = env.engine.client
|
|
|
|
env.sender.sendTx(client, tc, nonce)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
|
|
|
proc sendTx*(env: TestEnv, tc: BigInitcodeTx, nonce: AccountNonce): bool =
|
2023-09-30 12:20:29 +00:00
|
|
|
let client = env.engine.client
|
|
|
|
env.sender.sendTx(client, tc, nonce)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc sendTx*(env: TestEnv, tx: PooledTransaction): bool =
|
2023-09-30 12:20:29 +00:00
|
|
|
let client = env.engine.client
|
|
|
|
sendTx(client, tx)
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc sendTx*(
|
|
|
|
env: TestEnv,
|
|
|
|
sender: TestAccount,
|
|
|
|
eng: EngineEnv,
|
|
|
|
tc: BlobTx): Result[PooledTransaction, void] =
|
2023-10-19 03:28:52 +00:00
|
|
|
env.sender.sendTx(sender, eng.client, tc)
|
2023-10-23 13:59:57 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc replaceTx*(
|
|
|
|
env: TestEnv,
|
|
|
|
sender: TestAccount,
|
|
|
|
eng: EngineEnv,
|
|
|
|
tc: BlobTx): Result[PooledTransaction, void] =
|
2023-10-19 03:28:52 +00:00
|
|
|
env.sender.replaceTx(sender, eng.client, tc)
|
2023-10-23 13:59:57 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc makeTx*(
|
|
|
|
env: TestEnv,
|
|
|
|
tc: BaseTx,
|
|
|
|
sender: TestAccount,
|
|
|
|
nonce: AccountNonce): PooledTransaction =
|
2023-10-31 03:18:37 +00:00
|
|
|
env.sender.makeTx(tc, sender, nonce)
|
|
|
|
|
|
|
|
proc customizeTransaction*(env: TestEnv,
|
|
|
|
acc: TestAccount,
|
|
|
|
baseTx: Transaction,
|
|
|
|
custTx: CustomTransactionData): Transaction =
|
|
|
|
env.sender.customizeTransaction(acc, baseTx, custTx)
|
|
|
|
|
|
|
|
proc generateInvalidPayload*(env: TestEnv,
|
|
|
|
data: ExecutableData,
|
|
|
|
payloadField: InvalidPayloadBlockField): ExecutableData =
|
|
|
|
env.sender.generateInvalidPayload(data, payloadField)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
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
|