From a02a9150393b2639a1e8dd0fa6886eead5a49822 Mon Sep 17 00:00:00 2001 From: Jordan Hrycaj Date: Fri, 23 Feb 2024 09:17:24 +0000 Subject: [PATCH] Provide public default db symbol (#2050) * CoreDb: Provide default db backend symbols why: Handy for running `Aristo` against standard tests note: These defaults are currently set to legacy DB types. The must be enabled manually in `db/core_db.nim`. * Provide `Aristo` for macro assembler related tests caveat: Some tests use `initStorageTrie()` which lets `Aristo` bail out. The test need to run on `distinct_ledgers` (or something like) rather than `distinct_tries`. * Tests: Misc modules that can run on `Aristo` as well * NoHive: Module that can run on `Aristo` as well * Fix copyright year * ditto --- .../nodocker/consensus/consensus_sim.nim | 4 ++-- nimbus/common/common.nim | 18 ++++++++++----- nimbus/db/core_db.nim | 18 ++++++++++++++- tests/macro_assembler.nim | 22 +++++++++++++++++-- tests/persistBlockTestGen.nim | 4 ++-- tests/test_forkid.nim | 2 +- tests/test_genesis.nim | 18 +++++++-------- tests/test_op_memory.nim | 5 ++++- 8 files changed, 68 insertions(+), 23 deletions(-) diff --git a/hive_integration/nodocker/consensus/consensus_sim.nim b/hive_integration/nodocker/consensus/consensus_sim.nim index 82378d903..056ba0a01 100644 --- a/hive_integration/nodocker/consensus/consensus_sim.nim +++ b/hive_integration/nodocker/consensus/consensus_sim.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2021 Status Research & Development GmbH +# Copyright (c) 2021-2024 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) # * MIT license ([LICENSE-MIT](LICENSE-MIT)) @@ -19,7 +19,7 @@ import proc processChainData(cd: ChainData): TestStatus = let networkId = NetworkId(cd.params.config.chainId) - com = CommonRef.new(newCoreDbRef LegacyDbMemory, + com = CommonRef.new(newCoreDbRef DefaultDbMemory, pruneTrie = false, networkId, cd.params diff --git a/nimbus/common/common.nim b/nimbus/common/common.nim index b13616e4e..344aba50d 100644 --- a/nimbus/common/common.nim +++ b/nimbus/common/common.nim @@ -97,8 +97,9 @@ type ## Optional suggestion for the ledger cache to be used as state DB const - CommonLedgerTypeDefault* = LegacyAccountsCache - ## Default ledger type to use, see `ldgType` above + CommonLegacyDbLedgerTypeDefault = LegacyAccountsCache + ## Default ledger type to use, see `ldgType` above. This default will be + ## superseded by `LedgerCache` as default for `Aristo` type deb backend. # ------------------------------------------------------------------------------ # Forward declarations @@ -156,7 +157,14 @@ proc init(com : CommonRef, com.forkTransitionTable = config.toForkTransitionTable() com.networkId = networkId com.syncProgress= SyncProgress() - com.ldgType = ldgType + com.ldgType = block: + if ldgType != LedgerType(0): + ldgType + elif db.dbType in {AristoDbMemory,AristoDbRocks,AristoDbVoid}: + # The `Aristo` backend does not work well with the `LegacyAccountsCache` + LedgerCache + else: + CommonLegacyDbLedgerTypeDefault # Initalise the PoA state regardless of whether it is needed on the current # network. For non-PoA networks this descriptor is ignored. @@ -223,7 +231,7 @@ proc new*( pruneTrie: bool = true; networkId: NetworkId = MainNet; params = networkParams(MainNet); - ldgType = CommonLedgerTypeDefault; + ldgType = LedgerType(0); ): CommonRef {.gcsafe, raises: [CatchableError].} = @@ -244,7 +252,7 @@ proc new*( config: ChainConfig; pruneTrie: bool = true; networkId: NetworkId = MainNet; - ldgType = CommonLedgerTypeDefault; + ldgType = LedgerType(0); ): CommonRef {.gcsafe, raises: [CatchableError].} = diff --git a/nimbus/db/core_db.nim b/nimbus/db/core_db.nim index c5401f155..e3059fc9a 100644 --- a/nimbus/db/core_db.nim +++ b/nimbus/db/core_db.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2023 Status Research & Development GmbH +# 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) @@ -25,4 +25,20 @@ import export memory_only +# Default database backend selection. Note that an `Aristo` type backend +# should run on a `LedgerCache` type ledger (will not work with +# `LegacyAccountsCache`.) The `common` module automatically sets that up +# (unless overridden.) Practically, these constants are mainly used for +# setting up DB agnostic unit/integration tests. +# +# Uncomment the below symbols in order to activate the `Aristo` database. +#const DefaultDbMemory* = AristoDbMemory +#const DefaultDbPersistent* = AristoDbRocks + +# Catch undefined symbols and set them to the legacy database. +when not declared(DefaultDbMemory): + const DefaultDbMemory* = LegacyDbMemory +when not declared(DefaultDbPersistent): + const DefaultDbPersistent* = LegacyDbPersistent + # End diff --git a/tests/macro_assembler.nim b/tests/macro_assembler.nim index 7624abd8e..03bb466d1 100644 --- a/tests/macro_assembler.nim +++ b/tests/macro_assembler.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2019-2023 Status Research & Development GmbH +# Copyright (c) 2019-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) @@ -63,6 +63,15 @@ type const idToOpcode = CacheTable"NimbusMacroAssembler" +var + coreDbType* = DefaultDbMemory + ## This variable needs to be accessible for unit tests like + ## `test_op_memory` which implicitely uses the `initStorageTrie()` call + ## from the `distinct_tries` module. The `Aristo` API cannot handle that + ## because it needs the account addressfor accessing the storage trie. + ## + ## This problem can be fixed here in the `verifyAsmResult()` function once + ## there is the time to do it ... static: for n in Op: @@ -269,8 +278,14 @@ const proc initVMEnv*(network: string): BaseVMState = let conf = getChainConfig(network) + cdb = block: + # Need static binding + case coreDbType: + of AristoDbMemory: newCoreDbRef AristoDbMemory + of LegacyDbMemory: newCoreDbRef LegacyDbMemory + else: raiseAssert "unsupported: " & $coreDbType com = CommonRef.new( - newCoreDbRef LegacyDbMemory, + cdb, conf, false, conf.chainId.NetworkId) @@ -286,6 +301,9 @@ proc initVMEnv*(network: string): BaseVMState = gasLimit: 100_000 ) + when DefaultDbMemory in {AristoDbMemory, AristoDbRocks}: + # Disable opportunistic DB layer features for `Aristo` + com.db.localDbOnly = true com.initializeEmptyDb() BaseVMState.new(parent, header, com) diff --git a/tests/persistBlockTestGen.nim b/tests/persistBlockTestGen.nim index 10c4b5cf1..ea945c907 100644 --- a/tests/persistBlockTestGen.nim +++ b/tests/persistBlockTestGen.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2019-2023 Status Research & Development GmbH +# Copyright (c) 2019-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) @@ -58,7 +58,7 @@ proc main() {.used.} = # nimbus --rpcapi: eth, debug --prune: archive var conf = makeConfig() - let db = newCoreDbRef(LegacyDbPersistent, string conf.dataDir) + let db = newCoreDbRef(DefaultDbPersistent, string conf.dataDir) let com = CommonRef.new(db, false) com.dumpTest(97) diff --git a/tests/test_forkid.nim b/tests/test_forkid.nim index c6972d4f1..423f6b49c 100644 --- a/tests/test_forkid.nim +++ b/tests/test_forkid.nim @@ -82,7 +82,7 @@ template runTest(network: untyped, name: string) = test name: var params = networkParams(network) - com = CommonRef.new(newCoreDbRef LegacyDbMemory, true, network, params) + com = CommonRef.new(newCoreDbRef DefaultDbMemory, true, network, params) for i, x in `network IDs`: let id = com.forkId(x.number, x.time) diff --git a/tests/test_genesis.nim b/tests/test_genesis.nim index 2c0376873..be173880b 100644 --- a/tests/test_genesis.nim +++ b/tests/test_genesis.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2019-2023 Status Research & Development GmbH +# Copyright (c) 2019-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) @@ -27,7 +27,7 @@ proc findFilePath(file: string): string = return path proc makeGenesis(networkId: NetworkId): BlockHeader = - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = networkParams(networkId)) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = networkParams(networkId)) com.genesisHeader proc genesisTest() = @@ -63,7 +63,7 @@ proc customGenesisTest() = test "calaveras.json": var cg: NetworkParams check loadNetworkParams("calaveras.json".findFilePath, cg) - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = cg) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = cg) let stateRoot = "664c93de37eb4a72953ea42b8c046cdb64c9f0b0bca5505ade8d970d49ebdb8c".toDigest let genesisHash = "eb9233d066c275efcdfed8037f4fc082770176aefdbcb7691c71da412a5670f2".toDigest check com.genesisHeader.stateRoot == stateRoot @@ -75,7 +75,7 @@ proc customGenesisTest() = test "Devnet4.json (aka Kintsugi in all but chainId)": var cg: NetworkParams check loadNetworkParams("devnet4.json".findFilePath, cg) - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = cg) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = cg) let stateRoot = "3b84f313bfd49c03cc94729ade2e0de220688f813c0c895a99bd46ecc9f45e1e".toDigest let genesisHash = "a28d8d73e087a01d09d8cb806f60863652f30b6b6dfa4e0157501ff07d422399".toDigest check com.genesisHeader.stateRoot == stateRoot @@ -85,7 +85,7 @@ proc customGenesisTest() = test "Devnet5.json (aka Kiln in all but chainId and TTD)": var cg: NetworkParams check loadNetworkParams("devnet5.json".findFilePath, cg) - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = cg) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = cg) let stateRoot = "52e628c7f35996ba5a0402d02b34535993c89ff7fc4c430b2763ada8554bee62".toDigest let genesisHash = "51c7fe41be669f69c45c33a56982cbde405313342d9e2b00d7c91a7b284dd4f8".toDigest check com.genesisHeader.stateRoot == stateRoot @@ -95,7 +95,7 @@ proc customGenesisTest() = test "Mainnet shadow fork 1": var cg: NetworkParams check loadNetworkParams("mainshadow1.json".findFilePath, cg) - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = cg) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = cg) let stateRoot = "d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".toDigest let genesisHash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3".toDigest let ttd = "46_089_003_871_917_200_000_000".parse(Uint256) @@ -108,7 +108,7 @@ proc customGenesisTest() = # parse using geth format should produce the same result with nimbus format var cg: NetworkParams check loadNetworkParams("geth_mainshadow1.json".findFilePath, cg) - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = cg) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = cg) let stateRoot = "d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".toDigest let genesisHash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3".toDigest let ttd = "46_089_003_871_917_200_000_000".parse(Uint256) @@ -123,7 +123,7 @@ proc customGenesisTest() = test "Holesky": var cg: NetworkParams check loadNetworkParams("holesky.json".findFilePath, cg) - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = cg) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = cg) let stateRoot = "69D8C9D72F6FA4AD42D4702B433707212F90DB395EB54DC20BC85DE253788783".toDigest let genesisHash = "b5f7f912443c940f21fd611f12828d75b534364ed9e95ca4e307729a4661bde4".toDigest check com.genesisHeader.stateRoot == stateRoot @@ -134,7 +134,7 @@ proc customGenesisTest() = # parse using geth format should produce the same result with nimbus format var cg: NetworkParams check loadNetworkParams("geth_holesky.json".findFilePath, cg) - let com = CommonRef.new(newCoreDbRef LegacyDbMemory, params = cg) + let com = CommonRef.new(newCoreDbRef DefaultDbMemory, params = cg) let stateRoot = "69D8C9D72F6FA4AD42D4702B433707212F90DB395EB54DC20BC85DE253788783".toDigest let genesisHash = "b5f7f912443c940f21fd611f12828d75b534364ed9e95ca4e307729a4661bde4".toDigest check com.genesisHeader.stateRoot == stateRoot diff --git a/tests/test_op_memory.nim b/tests/test_op_memory.nim index 3bc841c5b..17eb45473 100644 --- a/tests/test_op_memory.nim +++ b/tests/test_op_memory.nim @@ -1,5 +1,5 @@ # Nimbus -# Copyright (c) 2019-2023 Status Research & Development GmbH +# Copyright (c) 2019-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) @@ -12,6 +12,9 @@ import std/[macros, strutils], macro_assembler, unittest2 +# Currently fails on `AristoDb*` +macro_assembler.coreDbType = LegacyDbMemory + proc opMemoryMain*() = suite "Memory Opcodes": assembler: # PUSH1 OP