Skip to content

Commit 221e6c9

Browse files
authored
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)
1 parent 70d7181 commit 221e6c9

File tree

115 files changed

+888
-1401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+888
-1401
lines changed

fluffy/conf.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import
1111
std/os,
1212
uri, confutils, confutils/std/net, chronicles,
13-
eth/keys, eth/net/nat, eth/p2p/discoveryv5/[enr, node, routing_table],
13+
eth/keys, eth/p2p/discoveryv5/[enr, node, routing_table],
1414
json_rpc/rpcproxy,
1515
nimcrypto/hash,
1616
stew/byteutils,
17+
eth/net/nat, # must be late (compilation annoyance)
1718
./logging,
1819
./network/wire/portal_protocol_config
1920

fluffy/network/history/history_network.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
import
1111
stew/results, chronos, chronicles,
12-
eth/[common/eth_types_rlp, rlp, trie, trie/db],
12+
eth/[common/eth_types_rlp, rlp],
1313
eth/p2p/discoveryv5/[protocol, enr],
1414
../../common/common_types,
1515
../../content_db,
1616
../../network_metadata,
17-
../../../nimbus/constants,
17+
../../../nimbus/[constants, db/core_db],
1818
../wire/[portal_protocol, portal_stream, portal_protocol_config],
1919
"."/[history_content, accumulator]
2020

@@ -184,7 +184,7 @@ func encode*(receipts: seq[Receipt]): seq[byte] =
184184
# for if/when peer scoring/banning is added.
185185

186186
proc calcRootHash(items: Transactions | PortalReceipts | Withdrawals): Hash256 =
187-
var tr = initHexaryTrie(newMemoryDB())
187+
var tr = newCoreDbRef(LegacyDbMemory).mptPrune
188188
for i, item in items:
189189
try:
190190
tr.put(rlp.encode(i), item.asSeq())

fluffy/tests/test_state_network.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
import
99
std/os,
1010
testutils/unittests, chronos,
11-
eth/[common/eth_hash, keys, trie/db, trie/hexary],
11+
eth/[common/eth_hash, keys],
1212
eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/discoveryv5/routing_table,
13-
../../nimbus/[config, db/db_chain, db/state_db],
13+
../../nimbus/[config, db/core_db, db/state_db],
1414
../../nimbus/common/[chain_config, genesis],
1515
../network/wire/[portal_protocol, portal_stream],
1616
../network/state/[state_content, state_network],
1717
../content_db,
1818
./test_helpers
1919

20-
proc genesisToTrie(filePath: string): HexaryTrie =
20+
proc genesisToTrie(filePath: string): CoreDbMptRef =
2121
# TODO: Doing our best here with API that exists, to be improved.
2222
var cn: NetworkParams
2323
if not loadNetworkParams(filePath, cn):
2424
quit(1)
2525

26-
let sdb = newStateDB(newMemoryDB(), false)
26+
let sdb = newStateDB(newCoreDbRef LegacyDbMemory, false)
2727
let map = toForkTransitionTable(cn.config)
2828
let fork = map.toHardFork(forkDeterminationInfo(0.toBlockNumber, cn.genesis.timestamp))
2929
discard toGenesisHeader(cn.genesis, sdb, fork)

fluffy/tools/beacon_lc_bridge/beacon_lc_bridge.nim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
import
6969
std/[os, strutils, options],
7070
chronicles, chronos, confutils,
71-
eth/[keys, rlp], eth/[trie, trie/db],
71+
eth/[keys, rlp],
7272
# Need to rename this because of web3 ethtypes and ambigious indentifier mess
7373
# for `BlockHeader`.
7474
eth/common/eth_types as etypes,
@@ -87,6 +87,7 @@ import
8787
../../network/history/[history_content, history_network],
8888
../../network/beacon_light_client/beacon_light_client_content,
8989
../../common/common_types,
90+
../../nimbus/db/core_db,
9091
./beacon_lc_bridge_conf
9192

9293
from stew/objects import checkedEnumAssign
@@ -189,7 +190,7 @@ proc calculateTransactionData(
189190
items: openArray[TypedTransaction]):
190191
Hash256 {.raises: [].} =
191192

192-
var tr = initHexaryTrie(newMemoryDB())
193+
var tr = newCoreDbRef(LegacyDbMemory).mptPrune
193194
for i, t in items:
194195
try:
195196
let tx = distinctBase(t)
@@ -207,7 +208,7 @@ proc calculateWithdrawalsRoot(
207208
items: openArray[WithdrawalV1]):
208209
Hash256 {.raises: [].} =
209210

210-
var tr = initHexaryTrie(newMemoryDB())
211+
var tr = newCoreDbRef(LegacyDbMemory).mptPrune
211212
for i, w in items:
212213
try:
213214
let withdrawal = etypes.Withdrawal(

hive_integration/nodocker/consensus/consensus_sim.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import
1919
proc processChainData(cd: ChainData): TestStatus =
2020
let
2121
networkId = NetworkId(cd.params.config.chainId)
22-
com = CommonRef.new(newMemoryDB(),
22+
com = CommonRef.new(newCoreDbRef LegacyDbMemory,
2323
pruneTrie = false,
2424
networkId,
2525
cd.params

hive_integration/nodocker/engine/test_env.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ proc setupELClient*(t: TestEnv, chainFile: string, enableAuth: bool) =
7878

7979
t.ethNode = setupEthNode(t.conf, t.ctx, eth)
8080
t.com = CommonRef.new(
81-
newMemoryDb(),
81+
newCoreDbRef LegacyDbMemory,
8282
t.conf.pruneMode == PruneMode.Full,
8383
t.conf.networkId,
8484
t.conf.networkParams

hive_integration/nodocker/graphql/graphql_sim.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ proc main() =
7878
conf = makeConfig(@["--custom-network:" & genesisFile])
7979
ethCtx = newEthContext()
8080
ethNode = setupEthNode(conf, ethCtx, eth)
81-
com = CommonRef.new(newMemoryDB(),
81+
com = CommonRef.new(newCoreDbRef LegacyDbMemory,
8282
pruneTrie = false,
8383
conf.networkId,
8484
conf.networkParams

hive_integration/nodocker/pyspec/test_env.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ proc genesisHeader(node: JsonNode): BlockHeader =
3939
rlp.decode(genesisRLP, EthBlock).header
4040

4141
proc setupELClient*(t: TestEnv, conf: ChainConfig, node: JsonNode) =
42-
let memDB = newMemoryDb()
42+
let memDB = newCoreDbRef LegacyDbMemory
4343
t.ctx = newEthContext()
4444
t.ethNode = setupEthNode(t.conf, t.ctx, eth)
4545
t.com = CommonRef.new(

hive_integration/nodocker/rpc/test_env.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ proc setupEnv*(): TestEnv =
9494
let
9595
ethCtx = newEthContext()
9696
ethNode = setupEthNode(conf, ethCtx, eth)
97-
com = CommonRef.new(newMemoryDb(),
97+
com = CommonRef.new(newCoreDbRef LegacyDbMemory,
9898
conf.pruneMode == PruneMode.Full,
9999
conf.networkId,
100100
conf.networkParams

nimbus/common/common.nim

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import
1313
std/[options, times],
1414
chronicles,
1515
eth/trie/trie_defs,
16-
./chain_config,
17-
./hardforks,
18-
./evmforks,
19-
./genesis,
16+
../core/[pow, clique, casper],
17+
../db/[core_db, storage_types],
2018
../utils/[utils, ec_recover],
21-
../db/[db_chain, storage_types],
22-
../core/[pow, clique, casper]
19+
".."/[constants, errors],
20+
"."/[chain_config, evmforks, genesis, hardforks]
2321

2422
export
2523
chain_config,
26-
db_chain,
24+
core_db,
25+
constants,
26+
errors,
2727
options,
2828
evmforks,
2929
hardforks,
@@ -41,7 +41,7 @@ type
4141

4242
CommonRef* = ref object
4343
# all purpose storage
44-
db: ChainDBRef
44+
db: CoreDbRef
4545

4646
# prune underlying state db?
4747
pruneTrie: bool
@@ -130,15 +130,15 @@ proc daoCheck(conf: ChainConfig) =
130130
conf.daoForkBlock = conf.homesteadBlock
131131

132132
proc init(com : CommonRef,
133-
db : TrieDatabaseRef,
133+
db : CoreDbRef,
134134
pruneTrie: bool,
135135
networkId: NetworkId,
136136
config : ChainConfig,
137137
genesis : Genesis) {.gcsafe, raises: [CatchableError].} =
138138

139139
config.daoCheck()
140140

141-
com.db = ChainDBRef.new(db)
141+
com.db = db
142142
com.pruneTrie = pruneTrie
143143
com.config = config
144144
com.forkTransitionTable = config.toForkTransitionTable()
@@ -157,7 +157,7 @@ proc init(com : CommonRef,
157157
# by setForkId
158158
if genesis.isNil.not:
159159
com.genesisHeader = toGenesisHeader(genesis,
160-
com.currentFork, com.db.db)
160+
com.currentFork, com.db)
161161
com.setForkId(com.genesisHeader)
162162

163163
# Initalise the PoA state regardless of whether it is needed on the current
@@ -194,7 +194,7 @@ proc getTdIfNecessary(com: CommonRef, blockHash: Hash256): Option[DifficultyInt]
194194
# ------------------------------------------------------------------------------
195195
196196
proc new*(_: type CommonRef,
197-
db: TrieDatabaseRef,
197+
db: CoreDbRef,
198198
pruneTrie: bool = true,
199199
networkId: NetworkId = MainNet,
200200
params = networkParams(MainNet)): CommonRef
@@ -211,7 +211,7 @@ proc new*(_: type CommonRef,
211211
params.genesis)
212212
213213
proc new*(_: type CommonRef,
214-
db: TrieDatabaseRef,
214+
db: CoreDbRef,
215215
config: ChainConfig,
216216
pruneTrie: bool = true,
217217
networkId: NetworkId = MainNet): CommonRef
@@ -227,11 +227,11 @@ proc new*(_: type CommonRef,
227227
config,
228228
nil)
229229
230-
proc clone*(com: CommonRef, db: TrieDatabaseRef): CommonRef =
230+
proc clone*(com: CommonRef, db: CoreDbRef): CommonRef =
231231
## clone but replace the db
232232
## used in EVM tracer whose db is CaptureDB
233233
CommonRef(
234-
db : ChainDBRef.new(db),
234+
db : db,
235235
pruneTrie : com.pruneTrie,
236236
config : com.config,
237237
forkTransitionTable: com.forkTransitionTable,
@@ -248,7 +248,7 @@ proc clone*(com: CommonRef, db: TrieDatabaseRef): CommonRef =
248248
)
249249
250250
proc clone*(com: CommonRef): CommonRef =
251-
com.clone(com.db.db)
251+
com.clone(com.db)
252252
253253
# ------------------------------------------------------------------------------
254254
# Public functions
@@ -361,14 +361,14 @@ proc consensus*(com: CommonRef, header: BlockHeader): ConsensusType
361361
362362
proc initializeEmptyDb*(com: CommonRef)
363363
{.gcsafe, raises: [CatchableError].} =
364-
let trieDB = com.db.db
365-
if canonicalHeadHashKey().toOpenArray notin trieDB:
364+
let kvt = com.db.kvt()
365+
if canonicalHeadHashKey().toOpenArray notin kvt:
366366
trace "Writing genesis to DB"
367367
doAssert(com.genesisHeader.blockNumber.isZero,
368368
"can't commit genesis block with number > 0")
369369
discard com.db.persistHeaderToDb(com.genesisHeader,
370370
com.consensusType == ConsensusType.POS)
371-
doAssert(canonicalHeadHashKey().toOpenArray in trieDB)
371+
doAssert(canonicalHeadHashKey().toOpenArray in kvt)
372372
373373
proc syncReqNewHead*(com: CommonRef; header: BlockHeader)
374374
{.gcsafe, raises: [].} =
@@ -396,7 +396,7 @@ func pos*(com: CommonRef): CasperRef =
396396
## Getter
397397
com.pos
398398
399-
func db*(com: CommonRef): ChainDBRef =
399+
func db*(com: CommonRef): CoreDbRef =
400400
com.db
401401
402402
func consensus*(com: CommonRef): ConsensusType =

0 commit comments

Comments
 (0)