Core db and aristo updates for destructor and tx logic (#1894)
* Disable `TransactionID` related functions from `state_db.nim`
why:
Functions `getCommittedStorage()` and `updateOriginalRoot()` from
the `state_db` module are nowhere used. The emulation of a legacy
`TransactionID` type functionality is administratively expensive to
provide by `Aristo` (the legacy DB version is only partially
implemented, anyway).
As there is no other place where `TransactionID`s are used, they will
not be provided by the `Aristo` variant of the `CoreDb`. For the
legacy DB API, nothing will change.
* Fix copyright headers in source code
* Get rid of compiler warning
* Update Aristo code, remove unused `merge()` variant, export `hashify()`
why:
Adapt to upcoming `CoreDb` wrapper
* Remove synced tx feature from `Aristo`
why:
+ This feature allowed to synchronise transaction methods like begin,
commit, and rollback for a group of descriptors.
+ The feature is over engineered and not needed for `CoreDb`, neither
is it complete (some convergence features missing.)
* Add debugging helpers to `Kvt`
also:
Update database iterator, add count variable yield argument similar
to `Aristo`.
* Provide optional destructors for `CoreDb` API
why;
For the upcoming Aristo wrapper, this allows to control when certain
smart destruction and update can take place. The auto destructor works
fine in general when the storage/cache strategy is known and acceptable
when creating descriptors.
* Add update option for `CoreDb` API function `hash()`
why;
The hash function is typically used to get the state root of the MPT.
Due to lazy hashing, this might be not available on the `Aristo` DB.
So the `update` function asks for re-hashing the gurrent state changes
if needed.
* Update API tracking log mode: `info` => `debug
* Use shared `Kvt` descriptor in new Ledger API
why:
No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
|
|
|
# Nimbus
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-11 14:25:29 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or
|
|
|
|
# distributed except according to those terms.
|
|
|
|
|
|
|
|
## Aristo (aka Patricia) DB trancoder test
|
|
|
|
|
|
|
|
import
|
2024-06-03 20:10:35 +00:00
|
|
|
std/[sequtils, sets],
|
2023-05-11 14:25:29 +00:00
|
|
|
eth/common,
|
2023-08-21 14:58:30 +00:00
|
|
|
results,
|
2023-05-11 14:25:29 +00:00
|
|
|
stew/byteutils,
|
2023-09-13 02:32:38 +00:00
|
|
|
stew/endians2,
|
2023-05-11 14:25:29 +00:00
|
|
|
unittest2,
|
2023-08-21 14:58:30 +00:00
|
|
|
../../nimbus/db/aristo,
|
2023-08-25 22:53:59 +00:00
|
|
|
../../nimbus/db/aristo/[
|
2023-12-19 12:39:23 +00:00
|
|
|
aristo_check, aristo_debug, aristo_desc, aristo_blobify, aristo_layers,
|
|
|
|
aristo_vid],
|
2023-10-11 19:09:11 +00:00
|
|
|
../replay/xcheck,
|
2023-08-21 14:58:30 +00:00
|
|
|
./test_helpers
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TesterDesc = object
|
|
|
|
prng: uint32 ## random state
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc posixPrngRand(state: var uint32): byte =
|
|
|
|
## POSIX.1-2001 example of a rand() implementation, see manual page rand(3).
|
|
|
|
state = state * 1103515245 + 12345;
|
|
|
|
let val = (state shr 16) and 32767 # mod 2^31
|
|
|
|
(val shr 8).byte # Extract second byte
|
|
|
|
|
|
|
|
proc rand[W: SomeInteger|VertexID](ap: var TesterDesc; T: type W): T =
|
|
|
|
var a: array[sizeof T,byte]
|
|
|
|
for n in 0 ..< sizeof T:
|
|
|
|
a[n] = ap.prng.posixPrngRand().byte
|
|
|
|
when sizeof(T) == 1:
|
|
|
|
let w = uint8.fromBytesBE(a).T
|
|
|
|
when sizeof(T) == 2:
|
|
|
|
let w = uint16.fromBytesBE(a).T
|
|
|
|
when sizeof(T) == 4:
|
|
|
|
let w = uint32.fromBytesBE(a).T
|
|
|
|
else:
|
|
|
|
let w = uint64.fromBytesBE(a).T
|
|
|
|
when T is SomeUnsignedInt:
|
|
|
|
# That way, `fromBytesBE()` can be applied to `uint`
|
|
|
|
result = w
|
|
|
|
else:
|
|
|
|
# That way the result is independent of endianness
|
|
|
|
(addr result).copyMem(unsafeAddr w, sizeof w)
|
|
|
|
|
|
|
|
proc vidRand(td: var TesterDesc; bits = 19): VertexID =
|
|
|
|
if bits < 64:
|
|
|
|
let
|
|
|
|
mask = (1u64 shl max(1,bits)) - 1
|
|
|
|
rval = td.rand uint64
|
|
|
|
(rval and mask).VertexID
|
|
|
|
else:
|
|
|
|
td.rand VertexID
|
|
|
|
|
|
|
|
proc init(T: type TesterDesc; seed: int): TesterDesc =
|
|
|
|
result.prng = (seed and 0x7fffffff).uint32
|
|
|
|
|
|
|
|
proc `+`(a: VertexID, b: int): VertexID =
|
|
|
|
(a.uint64 + b.uint64).VertexID
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public test function
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
proc testVidRecycleLists*(noisy = true; seed = 42): bool =
|
2023-05-11 14:25:29 +00:00
|
|
|
## Transcode VID lists held in `AristoDb` descriptor
|
2023-08-25 22:53:59 +00:00
|
|
|
##
|
2023-05-11 14:25:29 +00:00
|
|
|
var td = TesterDesc.init seed
|
2023-09-15 15:23:53 +00:00
|
|
|
let db = AristoDbRef.init()
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Add some randum numbers
|
|
|
|
block:
|
|
|
|
let first = td.vidRand()
|
2023-05-14 17:43:01 +00:00
|
|
|
db.vidDispose first
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
expectedVids = 1
|
|
|
|
count = 1
|
|
|
|
# Feed some numbers used and some discaded
|
|
|
|
while expectedVids < 5 or count < 5 + expectedVids:
|
|
|
|
count.inc
|
|
|
|
let vid = td.vidRand()
|
|
|
|
expectedVids += (vid < first).ord
|
2023-05-14 17:43:01 +00:00
|
|
|
db.vidDispose vid
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
xCheck db.vGen.len == expectedVids:
|
|
|
|
noisy.say "***", "vids=", db.vGen.len, " discarded=", count-expectedVids
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Serialise/deserialise
|
|
|
|
block:
|
2023-12-19 12:39:23 +00:00
|
|
|
let dbBlob = db.vGen.blobify
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Deserialise
|
2023-06-20 13:26:25 +00:00
|
|
|
let
|
2023-09-15 15:23:53 +00:00
|
|
|
db1 = AristoDbRef.init()
|
2023-06-20 13:26:25 +00:00
|
|
|
rc = dbBlob.deblobify seq[VertexID]
|
2023-09-05 13:57:20 +00:00
|
|
|
xCheckRc rc.error == 0
|
2024-06-03 20:10:35 +00:00
|
|
|
db1.top.delta.vGen = rc.value
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
xCheck db.vGen == db1.vGen
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Make sure that recycled numbers are fetched first
|
2023-12-19 12:39:23 +00:00
|
|
|
let topVid = db.vGen[^1]
|
|
|
|
while 1 < db.vGen.len:
|
2023-05-14 17:43:01 +00:00
|
|
|
let w = db.vidFetch()
|
2023-08-30 17:08:39 +00:00
|
|
|
xCheck w < topVid
|
2023-12-19 12:39:23 +00:00
|
|
|
xCheck db.vGen.len == 1 and db.vGen[0] == topVid
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Get some consecutive vertex IDs
|
|
|
|
for n in 0 .. 5:
|
2023-05-14 17:43:01 +00:00
|
|
|
let w = db.vidFetch()
|
2023-08-30 17:08:39 +00:00
|
|
|
xCheck w == topVid + n
|
2023-12-19 12:39:23 +00:00
|
|
|
xCheck db.vGen.len == 1
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Repeat last test after clearing the cache
|
2024-06-03 20:10:35 +00:00
|
|
|
db.top.delta.vGen.setLen(0)
|
2023-05-11 14:25:29 +00:00
|
|
|
for n in 0 .. 5:
|
2023-05-14 17:43:01 +00:00
|
|
|
let w = db.vidFetch()
|
2024-02-01 21:27:48 +00:00
|
|
|
xCheck w == VertexID(LEAST_FREE_VID) + n # VertexID(1) is default root ID
|
2023-12-19 12:39:23 +00:00
|
|
|
xCheck db.vGen.len == 1
|
2023-06-09 11:17:37 +00:00
|
|
|
|
|
|
|
# Recycling and re-org tests
|
2024-02-01 21:27:48 +00:00
|
|
|
func toVQ(a: seq[int]): seq[VertexID] = a.mapIt(VertexID(LEAST_FREE_VID+it))
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2024-02-22 08:24:58 +00:00
|
|
|
# Heuristic prevents from re-org
|
|
|
|
xCheck @[8, 7, 3, 4, 5, 9] .toVQ.vidReorg == @[8, 7, 3, 4, 5, 9] .toVQ
|
|
|
|
xCheck @[8, 7, 6, 3, 4, 5, 9] .toVQ.vidReorg == @[8, 7, 6, 3, 4, 5, 9].toVQ
|
|
|
|
xCheck @[5, 4, 3, 7] .toVQ.vidReorg == @[5, 4, 3, 7] .toVQ
|
|
|
|
xCheck @[5] .toVQ.vidReorg == @[5] .toVQ
|
|
|
|
xCheck @[3, 5] .toVQ.vidReorg == @[3, 5] .toVQ
|
|
|
|
xCheck @[4, 5] .toVQ.vidReorg == @[4, 5] .toVQ
|
|
|
|
|
|
|
|
# performing re-org
|
|
|
|
xCheck @[5, 7, 3, 4, 8, 9] .toVQ.vidReorg == @[5, 4, 3, 7] .toVQ
|
|
|
|
xCheck @[5, 7, 6, 3, 4, 8, 9] .toVQ.vidReorg == @[3] .toVQ
|
|
|
|
xCheck @[3, 4, 5, 7] .toVQ.vidReorg == @[5, 4, 3, 7] .toVQ
|
2023-08-25 22:53:59 +00:00
|
|
|
|
2023-08-30 17:08:39 +00:00
|
|
|
xCheck newSeq[VertexID](0).vidReorg().len == 0
|
2023-08-25 22:53:59 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
proc testShortKeys*(
|
|
|
|
noisy = true;
|
|
|
|
): bool =
|
|
|
|
## Check for some pathological cases
|
|
|
|
func x(s: string): Blob = s.hexToSeqByte
|
|
|
|
func k(s: string): HashKey = HashKey.fromBytes(s.x).value
|
|
|
|
|
|
|
|
let samples = [
|
|
|
|
# From InvalidBlocks/bc4895-withdrawals/twoIdenticalIndex.json
|
|
|
|
[("80".x,
|
|
|
|
"da808094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710".x,
|
|
|
|
"27f166f1d7c789251299535cb176ba34116e44894476a7886fe5d73d9be5c973".k),
|
|
|
|
("01".x,
|
|
|
|
"da028094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710".x,
|
|
|
|
"81eac5f476f48feb289af40ee764015f6b49036760438ea45df90d5342b6ae61".k),
|
|
|
|
("02".x,
|
|
|
|
"da018094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710".x,
|
|
|
|
"463769ae507fcc6d6231c8888425191c5622f330fdd4b78a7b24c4521137b573".k),
|
|
|
|
("03".x,
|
|
|
|
"da028094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710".x,
|
|
|
|
"a95b9a7b58a6b3cb4001eb0be67951c5517141cb0183a255b5cae027a7b10b36".k)]]
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
let gossip = false # or noisy
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
for n,sample in samples:
|
|
|
|
let sig = merkleSignBegin()
|
|
|
|
var inx = -1
|
|
|
|
for (k,v,r) in sample:
|
|
|
|
inx.inc
|
|
|
|
sig.merkleSignAdd(k,v)
|
2023-12-12 17:47:41 +00:00
|
|
|
gossip.say "*** testShortkeys (1)", "n=", n, " inx=", inx,
|
2023-11-08 12:18:32 +00:00
|
|
|
"\n k=", k.toHex, " v=", v.toHex,
|
|
|
|
"\n r=", r.pp(sig),
|
|
|
|
"\n ", sig.pp(),
|
|
|
|
"\n"
|
|
|
|
let w = sig.merkleSignCommit().value
|
2023-12-12 17:47:41 +00:00
|
|
|
gossip.say "*** testShortkeys (2)", "n=", n, " inx=", inx,
|
2023-11-08 12:18:32 +00:00
|
|
|
"\n k=", k.toHex, " v=", v.toHex,
|
|
|
|
"\n r=", r.pp(sig),
|
|
|
|
"\n R=", w.pp(sig),
|
|
|
|
"\n ", sig.pp(),
|
|
|
|
"\n ----------------",
|
|
|
|
"\n"
|
|
|
|
let rc = sig.db.check
|
|
|
|
xCheckRc rc.error == (0,0)
|
|
|
|
xCheck r == w
|
|
|
|
|
|
|
|
true
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|