nimbus-eth1/nimbus/db/core_db
Jordan Hrycaj 2c35390bdf
Core db and aristo maintenance update (#2014)
* Aristo: Update error return code

why:
  Failing of `Aristo` function `delete()` might fail because there is
  no such data item on the db. This must return a single error code
  as is done with `fetch()`.

* Ledger: Better error handling

why:
  The `expect()` clauses have been replaced by raising asserts indicating
  the error from the database backend.

   Also, `delete()` failures are legitimate if the item to delete does not
   exist.

* Aristo: Delete function must always leave a label on DB for `hashify()`

why:
  The `hashify()` uses the labels left bu `merge()` and `delete()` to
  compile (and optimise) a scheduler for subsequent hashing.

  Originally, the labels were not used for deleted entries and `delete()`
  still had some edge case where the deletion label was not properly
  handled.

* Aristo: Update `hashify()` scheduler, remove buggy optimisation

why:
  Was left over from version without virtual state roots which did not
  know about account payload leaf vertices referring to storage roots.

* Aristo: Label storage trie account in `delete()` similar to `merge()`

details;
  The `delete()` function applied to a non-static state root (assumed
  to be a storage root) will check the payload of an accounts leaf
  and mark its Merkle keys to be re-checked when runninh `hashify()`

* Aristo: Clean up and re-org recycled vertex IDs in `hashify()`

why:
  Re-organising the recycled vertex IDs list intends to reduce the size of the
  list.

  This list is organised as a LIFO (or stack.) By reorganising it in a way
  so that the least vertex ID numbers are on top, the list will be kept
  smaller as observed on some examples (less than 30%.)

* CoreDb: Accept storage trie deletion requests in non-initialised state

why:
  Due to lazy initialisation, the root vertex ID might not yet exist. So
  the `Aristo` database handlers would reject this call with an error and
  this condition needs to be handled by the API (which realises the lazy
  feature.)

* Cosmetics & code massage, prettify logging

* fix missing import
2024-02-08 16:32:16 +00:00
..
backend Core db and aristo maintenance update (#2014) 2024-02-08 16:32:16 +00:00
base Core db+ledger aristo backend update (#2006) 2024-02-02 20:23:04 +00:00
.gitignore Unified database frontend (#1661) 2023-07-31 14:43:38 +01:00
README.md Unified database frontend integration (#1670) 2023-08-04 12:10:09 +01:00
base.nim Core db+ledger aristo backend update (#2006) 2024-02-02 20:23:04 +00:00
base_iterators.nim CoreDb: Re-implemented closure iterators as inline iterators (#2005) 2024-02-02 10:58:35 +00:00
base_iterators_persistent.nim CoreDb: Re-implemented closure iterators as inline iterators (#2005) 2024-02-02 10:58:35 +00:00
core_apps_legacy.nim Core db and aristo updates for destructor and tx logic (#1894) 2023-11-16 19:35:03 +00:00
core_apps_newapi.nim Core db+ledger aristo backend update (#2006) 2024-02-02 20:23:04 +00:00
memory_only.nim CoreDb: Re-implemented closure iterators as inline iterators (#2005) 2024-02-02 10:58:35 +00:00
persistent.nim Remove obsolete select_backend and fix simulators CI (#2007) 2024-02-04 21:28:20 +07:00

README.md

Core database replacement wrapper object

This wrapper replaces the TrieDatabaseRef and its derivatives by the new object CoreDbRef.

Relations to current TrieDatabaseRef implementation

Here are some incomplete translations for objects and constructors.

Object types:

Legacy notation CoreDbRef based replacement
ChainDB (don't use/avoid)
ChainDbRef CoreDbRef
TrieDatabaseRef CoreDbKvtRef
HexaryTrie CoreDbMptRef
SecureHexaryTrie CoreDbPhkRef
DbTransaction CoreDbTxRef
TransactionID CoreDbTxID

Constructors:

Legacy notation CoreDbRef based replacement
trieDB newChainDB("..") newCoreDbRef(LegacyDbPersistent,"..")
newMemoryDB() newCoreDbRef(LegacyDbMemory)
--
initHexaryTrie(db,..) db.mpt(..) (no pruning)
db.mptPrune(..) (w/pruning true/false)
--
initSecureHexaryTrie(db,..) db.phk(..) (no pruning)
db.phkPrune(..) (w/pruning true/false)
--
newCaptureDB(db,memDB) db.capture() (see below)

Usage of the replacement wrapper

Objects pedigree:

    CoreDbRef                   -- base descriptor
     | | | |
     | | | +-- CoreDbMptRef     -- hexary trie instance
     | | | |    :                    :
     | | | +-- CoreDbMptRef     -- hexary trie instance
     | | |
     | | |
     | | +---- CoreDbPhkRef     -- pre-hashed key hexary trie instance
     | | |      :                    :
     | | +---- CoreDbPhkRef     -- pre-hashed key hexary trie instance
     | |
     | |
     | +------ CoreDbKvtRef     -- single static key-value table
     |
     |
     +-------- CoreDbCaptRef    -- tracer support descriptor

Instantiating standard database object descriptors works as follows:

    let
      db = newCoreDbRef(..)           # new base descriptor
      mpt = db.mpt(..)                # hexary trie/Merkle Patricia Tree
      phk = db.phk(..)                # pre-hashed key hexary trie/MPT
      kvt = db.kvt                    # key-value table

Tracer support setup by hiding the current CoreDbRef behind a replacement:

    let
      capture = db.capture()
      db = capture.recorder           # use the recorder in place of db
    ...

    for key,value in capture.recorder.kvt:
     ...                              # process recorded data