Skip to content

Commit

Permalink
Generalise prune mode option 4 different db models (#2139)
Browse files Browse the repository at this point in the history
* Update README

* Nimbus-main: replaced `PruneMode` options by `ChainDbMode` options

details:
  For the legacy database, this changes the phrase
  - `conf.pruneMode == PruneMode.Full` to the expression
  + `conf.chainDbMode == ChainDbMode.Prune`.

* Fix issues moaned about by NIM compiler

* Fix copyright year
  • Loading branch information
mjfh authored Apr 17, 2024
1 parent c610053 commit e8eb326
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 62 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ available.)
Disable legacy chunked RLPx messages which are enabled by default for
synchronising against `Nethermind` nodes

* ENABLE_ETH_VERSION=66<br>
Enable legacy protocol `eth66` (or whatever other protocol version.)

* ENABLE_EVMC=1<br>
Enable mostly EVMC compliant wrapper around the native Nim VM

Expand All @@ -244,6 +241,12 @@ available.)
For these variables, using &lt;variable&gt;=0 is ignored and &lt;variable&gt;=2
has the same effect as &lt;variable&gt;=1 (ditto for other numbers.)

Other settings where the non-zero value matters:

* ENABLE_ETH_VERSION=66<br>
Enable legacy protocol `eth66` (or another available protocol version.)


### <a name="devel-tips"></a>Development tips

Interesting Make variables and targets are documented in the [nimbus-build-system](https://github.com/status-im/nimbus-build-system) repo.
Expand Down
2 changes: 1 addition & 1 deletion fluffy/docs/the_fluffy_book/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus Fluffy book
# Copyright (c) 2023 Status Research & Development GmbH
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
Expand Down
2 changes: 1 addition & 1 deletion hive_integration/nodocker/engine/engine_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const
proc makeCom*(conf: NimbusConf): CommonRef =
CommonRef.new(
newCoreDbRef LegacyDbMemory,
conf.pruneMode == PruneMode.Full,
conf.chainDbMode == ChainDbMode.Prune,
conf.networkId,
conf.networkParams
)
Expand Down
4 changes: 2 additions & 2 deletions hive_integration/nodocker/pyspec/test_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ proc setupELClient*(t: TestEnv, conf: ChainConfig, node: JsonNode) =
t.com = CommonRef.new(
memDB,
conf,
t.conf.pruneMode == PruneMode.Full
t.conf.chainDbMode == ChainDbMode.Prune
)
t.chainRef = newChain(t.com, extraValidation = true)
let
stateDB = AccountsCache.init(memDB, emptyRlpHash, t.conf.pruneMode == PruneMode.Full)
stateDB = AccountsCache.init(memDB, emptyRlpHash, t.conf.chainDbMode == ChainDbMode.Prune)
genesisHeader = node.genesisHeader

setupStateDB(node["pre"], stateDB)
Expand Down
4 changes: 2 additions & 2 deletions hive_integration/nodocker/rpc/test_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ proc stopRpcHttpServer(srv: RpcServer) =

proc setupEnv*(): TestEnv =
let conf = makeConfig(@[
"--prune-mode:archive",
"--chaindb:archive",
# "--nat:extip:0.0.0.0",
"--network:7",
"--import-key:" & initPath / "private-key",
Expand All @@ -77,7 +77,7 @@ proc setupEnv*(): TestEnv =
ethCtx = newEthContext()
ethNode = setupEthNode(conf, ethCtx, eth)
com = CommonRef.new(newCoreDbRef LegacyDbMemory,
conf.pruneMode == PruneMode.Full,
conf.chainDbMode == ChainDbMode.Prune,
conf.networkId,
conf.networkParams
)
Expand Down
20 changes: 13 additions & 7 deletions nimbus/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ const sharedLibText = if defined(linux): " (*.so, *.so.N)"
else: ""

type
PruneMode* {.pure.} = enum
Full
ChainDbMode* {.pure.} = enum
Prune
Archive
Aristo

NimbusCmd* {.pure.} = enum
noCommand
Expand Down Expand Up @@ -154,12 +155,17 @@ type
abbr: "k"
name: "key-store" }: OutDir

pruneMode* {.
desc: "Blockchain prune mode (Full or Archive)"
defaultValue: PruneMode.Full
defaultValueDesc: $PruneMode.Full
chainDbMode* {.
desc: "Blockchain database"
longDesc:
"- Prune -- Legacy/reference database, full pruning\n" &
"- Archive -- Legacy/reference database without pruning\n" &
"- Aristo -- Experimental single state DB\n" &
""
defaultValue: ChainDbMode.Prune
defaultValueDesc: $ChainDbMode.Prune
abbr : "p"
name: "prune-mode" }: PruneMode
name: "chaindb" }: ChainDbMode

syncMode* {.
desc: "Specify particular blockchain sync mode."
Expand Down
4 changes: 0 additions & 4 deletions nimbus/db/aristo/aristo_init/rocks_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ when extraTraceMessages:
# Private helpers
# ------------------------------------------------------------------------------

template logTxt(info: static[string]): static[string] =
"RocksDB " & info


proc newSession(db: RdbBackendRef): RdbPutHdlRef =
new result
result.TypedPutHdlRef.beginSession db
Expand Down
7 changes: 0 additions & 7 deletions nimbus/db/aristo/aristo_init/rocks_db/rdb_put.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@

import
eth/common,
rocksdb/lib/librocksdb,
rocksdb,
results,
../../aristo_desc,
../init_common,
./rdb_desc

type
RdbPutSession = object
writer: ptr rocksdb_sstfilewriter_t
sstPath: string
nRecords: int

const
extraTraceMessages = false
## Enable additional logging noise
Expand Down
5 changes: 1 addition & 4 deletions nimbus/db/core_db/backend/aristo_db/handlers_trace.nim
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,12 @@ when EnableDebugLog:

# -------------------------

func getOrVoid(tab: TableRef[Blob,Blob]; w: openArray[byte]): Blob =
tab.getOrDefault(@w, EmptyBlob)

func leafTie(
root: VertexID;
path: openArray[byte];
): Result[LeafTie,(VertexID,AristoError)] =
let tag = path.pathToTag.valueOr:
return err((VertexID(root), error))
return err((root, error))
ok LeafTie(root: root, path: tag)

proc blobify(
Expand Down
6 changes: 4 additions & 2 deletions nimbus/db/kvstore_rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ proc contains*(store: RocksStoreRef, key: openArray[byte]): KvResult[bool] =

proc del*(store: RocksStoreRef, key: openArray[byte]): KvResult[bool] =

let exists = ? store.db.keyExists(key)
if not exists:
let rc = store.db.keyExists(key)
if rc.isErr:
return rc
if not rc.value:
return ok(false)

let res = store.db.delete(key)
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/kvt/kvt_init/rocks_db/rdb_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func dataDir*(rdb: RdbInst): string =
rdb.baseDir / DataFolder


template logTxt(info: static[string]): static[string] =
template logTxt*(info: static[string]): static[string] =
"RocksDB/" & info

# ------------------------------------------------------------------------------
Expand Down
12 changes: 7 additions & 5 deletions nimbus/db/kvt/kvt_init/rocks_db/rdb_put.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import
eth/common,
stew/byteutils,
rocksdb,
results,
../../kvt_desc,
Expand All @@ -26,7 +25,9 @@ const
## Enable additional logging noise

when extraTraceMessages:
import chronicles
import
chronicles,
stew/byteutils

logScope:
topics = "kvt-rocksdb"
Expand All @@ -39,9 +40,10 @@ proc disposeSession(rdb: var RdbInst) =
rdb.session.close()
rdb.session = WriteBatchRef(nil)

proc `$`(a: Blob): string =
a.toHex

when extraTraceMessages:
proc `$`(a: Blob): string =
a.toHex

# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion nimbus/db/ledger/distinct_ledgers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ proc init*(
## https://github.com/status-im/nimbus-eth1/issues/932.)
const
info = "StorageLedger/init(): "
noisy = true
let
db = al.distinctBase.parent
stt = account.stoTrie
Expand Down
14 changes: 9 additions & 5 deletions nimbus/nimbus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,16 @@ proc start(nimbus: NimbusNode, conf: NimbusConf) =
evmcSetLibraryPath(conf.evm)

createDir(string conf.dataDir)
let coreDB =
# Resolve statically for database type
case conf.chainDbMode:
of Prune,Archive: LegacyDbPersistent.newCoreDbRef(string conf.dataDir)
of Aristo: AristoDbRocks.newCoreDbRef(string conf.dataDir)
let com = CommonRef.new(
newCoreDbRef(LegacyDbPersistent, string conf.dataDir),
conf.pruneMode == PruneMode.Full,
conf.networkId,
conf.networkParams
)
db = coreDB,
pruneTrie = (conf.chainDbMode == ChainDbMode.Prune),
networkId = conf.networkId,
params = conf.networkParams)

com.initializeEmptyDb()
com.db.compensateLegacySetup()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_beacon/setup_env.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2023 Status Research & Development GmbH
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at
# https://opensource.org/licenses/MIT).
Expand Down Expand Up @@ -64,7 +64,7 @@ proc setupEnv*(extraValidation: bool = false, ccm: CCModify = nil): TestEnv =
let
com = CommonRef.new(
newCoreDbRef LegacyDbMemory,
conf.pruneMode == PruneMode.Full,
conf.chainDbMode == ChainDbMode.Prune,
conf.networkId,
conf.networkParams
)
Expand Down
17 changes: 10 additions & 7 deletions tests/test_configuration.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ proc configurationMain*() =
check dd.dataDir.string == "apple\\bin"
check dd.keyStore.string == "banana/bin"

test "prune-mode":
test "chaindb-mode":
let aa = makeTestConfig()
check aa.pruneMode == PruneMode.Full
check aa.chainDbMode == ChainDbMode.Prune

let bb = makeConfig(@["--prune-mode:full"])
check bb.pruneMode == PruneMode.Full
let bb = makeConfig(@["--chaindb:prune"])
check bb.chainDbMode == ChainDbMode.Prune

let cc = makeConfig(@["--prune-mode:archive"])
check cc.pruneMode == PruneMode.Archive
let cc = makeConfig(@["--chaindb:archive"])
check cc.chainDbMode == ChainDbMode.Archive

let dd = makeConfig(@["-p:archive"])
check dd.pruneMode == PruneMode.Archive
check dd.chainDbMode == ChainDbMode.Archive

let ee = makeConfig(@["--chaindb:aristo"])
check ee.chainDbMode == ChainDbMode.Aristo

test "import":
let aa = makeTestConfig()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_merge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ proc runTest(steps: Steps) =
ethNode = setupEthNode(conf, ctx, eth)
com = CommonRef.new(
newCoreDbRef LegacyDbMemory,
conf.pruneMode == PruneMode.Full,
conf.chainDbMode == ChainDbMode.Prune,
conf.networkId,
conf.networkParams
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rpc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ proc rpcMain*() =
ethNode = setupEthNode(conf, ctx, eth)
com = CommonRef.new(
newCoreDbRef LegacyDbMemory,
conf.pruneMode == PruneMode.Full,
conf.chainDbMode == ChainDbMode.Prune,
conf.networkId,
conf.networkParams
)
Expand Down
14 changes: 9 additions & 5 deletions tests/test_txpool/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import
../replay/[pp, undump_blocks],
chronicles,
eth/[common, keys],
stew/[keyed_queue, sorted_set],
stew/[byteutils,keyed_queue, sorted_set],
stint

# Make sure that the runner can stay on public view without the need
Expand Down Expand Up @@ -76,6 +76,10 @@ const
# Helpers
# ------------------------------------------------------------------------------

proc toHex2(a: byte): string =
# Avoids compiler warning with `toHex(2)`
[a].toHex

proc joinXX(s: string): string =
if s.len <= 30:
return s
Expand All @@ -92,13 +96,13 @@ proc toXX[T](s: T): string =
s.toHex.strip(leading=true,chars={'0'}).toLowerAscii

proc toXX(q: Blob): string =
q.mapIt(it.toHex(2)).join(":")
q.mapIt(it.toHex2).join(":")

proc toXX(a: EthAddress): string =
a.mapIt(it.toHex(2)).joinXX
a.mapIt(it.toHex2).joinXX

proc toXX(h: Hash256): string =
h.data.mapIt(it.toHex(2)).joinXX
h.data.mapIt(it.toHex2).joinXX

proc toXX(v: int64; r,s: UInt256): string =
v.toXX & ":" & ($r).joinXX & ":" & ($s).joinXX
Expand Down Expand Up @@ -184,7 +188,7 @@ proc isOk*(rc: ValidationResult): bool =
rc == ValidationResult.OK

proc toHex*(acc: EthAddress): string =
acc.toSeq.mapIt(it.toHex(2)).join
acc.toHex

proc say*(noisy = false; pfx = "***"; args: varargs[string, `$`]) =
if noisy:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_txpool2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ proc initEnv(envFork: HardFork): TestEnv =
let
com = CommonRef.new(
newCoreDbRef LegacyDbMemory,
conf.pruneMode == PruneMode.Full,
conf.chainDbMode == ChainDbMode.Prune,
conf.networkId,
conf.networkParams
)
Expand Down

0 comments on commit e8eb326

Please sign in to comment.