nimbus-eth1/tests/test_merge.nim

181 lines
4.9 KiB
Nim
Raw Normal View History

# Nimbus
Core db and aristo updates for destructor and tx logic (#1894) * Disable `TransactionID` related functions from `state_db.nim` why: Functions `getCommittedStorage()` and `updateOriginalRoot()` from the `state_db` module are nowhere used. The emulation of a legacy `TransactionID` type functionality is administratively expensive to provide by `Aristo` (the legacy DB version is only partially implemented, anyway). As there is no other place where `TransactionID`s are used, they will not be provided by the `Aristo` variant of the `CoreDb`. For the legacy DB API, nothing will change. * Fix copyright headers in source code * Get rid of compiler warning * Update Aristo code, remove unused `merge()` variant, export `hashify()` why: Adapt to upcoming `CoreDb` wrapper * Remove synced tx feature from `Aristo` why: + This feature allowed to synchronise transaction methods like begin, commit, and rollback for a group of descriptors. + The feature is over engineered and not needed for `CoreDb`, neither is it complete (some convergence features missing.) * Add debugging helpers to `Kvt` also: Update database iterator, add count variable yield argument similar to `Aristo`. * Provide optional destructors for `CoreDb` API why; For the upcoming Aristo wrapper, this allows to control when certain smart destruction and update can take place. The auto destructor works fine in general when the storage/cache strategy is known and acceptable when creating descriptors. * Add update option for `CoreDb` API function `hash()` why; The hash function is typically used to get the state root of the MPT. Due to lazy hashing, this might be not available on the `Aristo` DB. So the `update` function asks for re-hashing the gurrent state changes if needed. * Update API tracking log mode: `info` => `debug * Use shared `Kvt` descriptor in new Ledger API why: No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
# Copyright (c) 2018-2023 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/[json, os, strutils, typetraits],
2022-12-02 04:39:12 +00:00
unittest2,
json_rpc/[rpcserver, rpcclient],
web3/[engine_api_types],
../nimbus/sync/protocol,
2022-12-02 04:39:12 +00:00
../nimbus/rpc,
../nimbus/common,
../nimbus/config,
../nimbus/core/[sealer, tx_pool, chain],
../nimbus/beacon/[beacon_engine, payload_queue],
./test_helpers
const
baseDir = "tests" / "merge"
paramsFile = baseDir / "params.json"
stepsFile = baseDir / "steps.json"
type
Step = ref object
name: string
meth: string
params: JSonNode
expect: JsonNode
error : bool
Steps = ref object
list: seq[Step]
proc parseStep(s: Step, node: JsonNode) =
for k, v in node:
case k
of "name": s.name = v.getStr()
of "method": s.meth = v.getStr()
of "params": s.params = v
of "expect": s.expect = v
of "error": s.error = true
else:
doAssert(false, "unknown key: " & k)
proc parseSteps(node: JsonNode): Steps =
let ss = Steps(list: @[])
for n in node:
let s = Step()
s.parseStep(n)
ss.list.add s
ss
proc forkChoiceUpdate(step: Step, client: RpcClient, testStatusIMPL: var TestStatus) =
let arg = step.params[1]
if arg.kind == JNull:
step.params.elems.setLen(1)
let res = waitFor client.call(step.meth, step.params)
check toLowerAscii($res) == toLowerAscii($step.expect)
proc getPayload(step: Step, client: RpcClient, testStatusIMPL: var TestStatus) =
try:
let res = waitFor client.call(step.meth, step.params)
check toLowerAscii($res) == toLowerAscii($step.expect)
except CatchableError:
check step.error == true
proc newPayload(step: Step, client: RpcClient, testStatusIMPL: var TestStatus) =
let res = waitFor client.call(step.meth, step.params)
check toLowerAscii($res) == toLowerAscii($step.expect)
proc runTest(steps: Steps) =
let
conf = makeConfig(@["--custom-network:" & paramsFile])
ctx = newEthContext()
ethNode = setupEthNode(conf, ctx, eth)
2022-12-02 04:39:12 +00:00
com = CommonRef.new(
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
newCoreDbRef LegacyDbMemory,
conf.pruneMode == PruneMode.Full,
conf.networkId,
conf.networkParams
)
2022-12-02 04:39:12 +00:00
chainRef = newChain(com)
2022-12-02 04:39:12 +00:00
com.initializeEmptyDb()
var
rpcServer = newRpcSocketServer(["127.0.0.1:" & $conf.rpcPort])
client = newRpcSocketClient()
2022-12-02 04:39:12 +00:00
txPool = TxPoolRef.new(com, conf.engineSigner)
sealingEngine = SealingEngineRef.new(
chainRef, ctx, conf.engineSigner,
txPool, EnginePostMerge
)
beaconEngine = BeaconEngineRef.new(txPool, chainRef)
2022-12-02 04:39:12 +00:00
setupEthRpc(ethNode, ctx, com, txPool, rpcServer)
setupEngineAPI(beaconEngine, rpcServer)
sealingEngine.start()
rpcServer.start()
waitFor client.connect("127.0.0.1", conf.rpcPort)
suite "Engine API tests":
for i, step in steps.list:
test $i & " " & step.name:
case step.meth
of "engine_forkchoiceUpdatedV1":
forkChoiceUpdate(step, client, testStatusIMPL)
of "engine_getPayloadV1":
getPayload(step, client, testStatusIMPL)
of "engine_newPayloadV1":
newPayload(step, client, testStatusIMPL)
else:
doAssert(false, "unknown method: " & step.meth)
waitFor client.close()
waitFor sealingEngine.stop()
rpcServer.stop()
waitFor rpcServer.closeWait()
proc testEngineAPI() =
let node = parseJSON(readFile(stepsFile))
let steps = parseSteps(node)
runTest(steps)
proc toId(x: int): PayloadId =
var id: distinctBase PayloadId
id[^1] = x.byte
PayloadId(id)
proc `==`(a, b: Quantity): bool =
uint64(a) == uint64(b)
2022-03-11 09:25:23 +00:00
proc testEngineApiSupport() =
var api = PayloadQueue()
let
id1 = toId(1)
id2 = toId(2)
ep1 = ExecutionPayloadV1(gasLimit: Quantity 100)
ep2 = ExecutionPayloadV1(gasLimit: Quantity 101)
hdr1 = common.BlockHeader(gasLimit: 100)
hdr2 = common.BlockHeader(gasLimit: 101)
hash1 = hdr1.blockHash
hash2 = hdr2.blockHash
suite "Test engine api support":
test "test payload queue":
2023-08-18 07:49:11 +00:00
api.put(id1, 123.u256, ep1)
api.put(id2, 456.u256, ep2)
var eep1, eep2: ExecutionPayloadV1
2023-08-18 07:49:11 +00:00
var bv1, bv2: UInt256
check api.get(id1, bv1, eep1)
check api.get(id2, bv2, eep2)
check eep1.gasLimit == ep1.gasLimit
check eep2.gasLimit == ep2.gasLimit
2023-08-18 07:49:11 +00:00
check bv1 == 123.u256
check bv2 == 456.u256
test "test header queue":
api.put(hash1, hdr1)
api.put(hash2, hdr2)
var eh1, eh2: common.BlockHeader
check api.get(hash1, eh1)
check api.get(hash2, eh2)
check eh1.gasLimit == hdr1.gasLimit
check eh2.gasLimit == hdr2.gasLimit
proc mergeMain*() =
2022-03-11 09:25:23 +00:00
# temporary disable it until engine API more stable
2022-03-17 05:54:04 +00:00
testEngineAPI()
2022-03-11 09:25:23 +00:00
testEngineApiSupport()
when isMainModule:
mergeMain()