nimbus-eth1/nimbus/db/incomplete_db.nim
Jordan Hrycaj 221e6c9e2f
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 12:10:09 +01:00

92 lines
3.8 KiB
Nim

#[
FIXME-Adam: I feel like this and distinct_tries should either be combined or more clearly separated.
The points of these two files are:
- Have distinct types for the two kinds of tries, because we really don't want to mix them up.
- Have an interface with functions like getAccountBytes rather than just get. (But still just a super-thin wrapper.)
- Have maybeGetWhatever instead of just getWhatever. (Also assertions.)
- Notice that this makes sense at both the bytes level and the Account/UInt256 level.
]#
import
chronicles,
eth/[common],
"."/[core_db, distinct_tries, storage_types, values_from_bytes]
# Useful for debugging.
const shouldDoAssertionsForMissingNodes* = false
proc ifNodesExistGetAccountBytes*(trie: AccountsTrie, address: EthAddress): Option[seq[byte]] =
trie.maybeGetAccountBytes(address)
proc ifNodesExistGetStorageBytesWithinAccount*(storageTrie: StorageTrie, slotAsKey: openArray[byte]): Option[seq[byte]] =
storageTrie.maybeGetSlotBytes(slotAsKey)
proc populateDbWithNodes*(db: CoreDbRef, nodes: seq[seq[byte]]) =
error("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG AARDVARK: populateDbWithNodes received nodes, about to populate", nodes) # AARDVARK not an error, I just want it to stand out
for nodeBytes in nodes:
let nodeHash = keccakHash(nodeBytes)
info("AARDVARK: populateDbWithNodes about to add node", nodeHash, nodeBytes)
db.kvt.put(nodeHash.data, nodeBytes)
# AARDVARK: just make the callers call populateDbWithNodes directly?
proc populateDbWithBranch*(db: CoreDbRef, branch: seq[seq[byte]]) =
for nodeBytes in branch:
let nodeHash = keccakHash(nodeBytes)
db.kvt.put(nodeHash.data, nodeBytes)
# Returns a none if there are missing nodes; if the account itself simply
# doesn't exist yet, that's fine and it returns some(newAccount()).
proc ifNodesExistGetAccount*(trie: AccountsTrie, address: EthAddress): Option[Account] =
ifNodesExistGetAccountBytes(trie, address).map(accountFromBytes)
proc maybeGetCode*(db: CoreDbRef, codeHash: Hash256): Option[seq[byte]] =
when defined(geth):
return db.kvt.maybeGet(codeHash.data)
else:
return db.kvt.maybeGet(contractHashKey(codeHash).toOpenArray)
proc maybeGetCode*(trie: AccountsTrie, address: EthAddress): Option[seq[byte]] =
let maybeAcc = trie.ifNodesExistGetAccount(address)
if maybeAcc.isNone:
none[seq[byte]]()
else:
maybeGetCode(trie.db, maybeAcc.get.codeHash)
proc checkingForMissingNodes_getCode*(trie: AccountsTrie, address: EthAddress): seq[byte] =
let m = maybeGetCode(trie, address)
doAssert(m.isSome, "missing code for account at " & $(address))
m.get
proc assertFetchedCode*(trie: AccountsTrie, address: EthAddress) =
if shouldDoAssertionsForMissingNodes:
let m = maybeGetCode(trie, address)
doAssert(m.isSome, "missing code for account at " & $(address))
proc ifNodesExistGetStorageWithinAccount*(storageTrie: StorageTrie, slot: UInt256): Option[UInt256] =
ifNodesExistGetStorageBytesWithinAccount(storageTrie, createTrieKeyFromSlot(slot)).map(slotValueFromBytes)
proc ifNodesExistGetStorage*(trie: AccountsTrie, address: EthAddress, slot: UInt256): Option[UInt256] =
let maybeAcc = ifNodesExistGetAccount(trie, address)
if maybeAcc.isNone:
none[UInt256]()
else:
ifNodesExistGetStorageWithinAccount(storageTrieForAccount(trie, maybeAcc.get), slot)
proc hasAllNodesForAccount*(trie: AccountsTrie, address: EthAddress): bool =
ifNodesExistGetAccountBytes(trie, address).isSome
proc hasAllNodesForCode*(trie: AccountsTrie, address: EthAddress): bool =
maybeGetCode(trie, address).isSome
proc hasAllNodesForStorageSlot*(trie: AccountsTrie, address: EthAddress, slot: UInt256): bool =
ifNodesExistGetStorage(trie, address, slot).isSome
proc assertFetchedStorage*(trie: AccountsTrie, address: EthAddress, slot: UInt256) =
doAssert(hasAllNodesForStorageSlot(trie, address, slot))