Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core db disable legacy api n remove distinct tries #2299

Merged
merged 9 commits into from
Jun 5, 2024
11 changes: 3 additions & 8 deletions hive_integration/nodocker/engine/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ proc processBlock(
## implementations (but can be savely removed, as well.)
## variant of `processBlock()` where the `header` argument is explicitely set.

var dbTx = vmState.com.db.beginTransaction()
var dbTx = vmState.com.db.newTransaction()
defer: dbTx.dispose()

if vmState.com.daoForkSupport and
Expand Down Expand Up @@ -76,12 +76,7 @@ proc processBlock(
let clearEmptyAccount = vmState.determineFork >= FkSpurious
db.persist(clearEmptyAccount)

# `applyDeletes = false`
# If the trie pruning activated, each of the block will have its own state
# trie keep intact, rather than destroyed by trie pruning. But the current
# block will still get a pruned trie. If trie pruning deactivated,
# `applyDeletes` have no effects.
dbTx.commit(applyDeletes = false)
dbTx.commit()

ValidationResult.OK

Expand All @@ -103,7 +98,7 @@ proc getVmState(c: ChainRef, header: BlockHeader):
proc setBlock*(c: ChainRef; header: BlockHeader;
body: BlockBody): ValidationResult
{.inline, raises: [CatchableError].} =
let dbTx = c.db.beginTransaction()
let dbTx = c.db.newTransaction()
defer: dbTx.dispose()

c.com.hardForkTransition(header)
Expand Down
9 changes: 6 additions & 3 deletions nimbus/beacon/merge_tracker.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2022-2023 Status Research & Development GmbH
# Copyright (c) 2022-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand All @@ -12,6 +12,7 @@
import
chronicles,
eth/rlp,
results,
../db/[core_db, storage_types]

type
Expand All @@ -37,10 +38,12 @@ type
# ------------------------------------------------------------------------------

proc writeStatus(db: CoreDbRef, status: TransitionStatus) =
db.kvt.put(transitionStatusKey().toOpenArray(), rlp.encode(status))
db.newKvt.put(transitionStatusKey().toOpenArray(), rlp.encode(status)).isOkOr:
raiseAssert "writeStatus(): put() failed " & $$error

proc readStatus(db: CoreDbRef): TransitionStatus =
var bytes = db.kvt.get(transitionStatusKey().toOpenArray())
var bytes = db.newKvt.get(transitionStatusKey().toOpenArray()).valueOr:
EmptyBlob
if bytes.len > 0:
try:
result = rlp.decode(bytes, typeof result)
Expand Down
4 changes: 3 additions & 1 deletion nimbus/common/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ proc consensus*(com: CommonRef, header: BlockHeader): ConsensusType

proc initializeEmptyDb*(com: CommonRef)
{.gcsafe, raises: [CatchableError].} =
let kvt = com.db.kvt()
let kvt = com.db.newKvt()
proc contains(kvt: CoreDxKvtRef; key: openArray[byte]): bool =
kvt.hasKey(key).expect "valid bool"
if canonicalHeadHashKey().toOpenArray notin kvt:
info "Writing genesis to DB"
doAssert(com.genesisHeader.blockNumber.isZero,
Expand Down
26 changes: 13 additions & 13 deletions nimbus/core/chain/persist_blocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import

when not defined(release):
import
../../tracer,
#../../tracer,
../../utils/utils

export results
Expand Down Expand Up @@ -80,7 +80,7 @@ proc persistBlocksImpl(c: ChainRef; headers: openArray[BlockHeader];
bodies: openArray[BlockBody],
flags: PersistBlockFlags = {}): Result[PersistStats, string]
{.raises: [CatchableError] .} =
let dbTx = c.db.beginTransaction()
let dbTx = c.db.newTransaction()
defer: dbTx.dispose()

c.com.hardForkTransition(headers[0])
Expand All @@ -95,10 +95,10 @@ proc persistBlocksImpl(c: ChainRef; headers: openArray[BlockHeader];
for i in 0 ..< headers.len:
let (header, body) = (headers[i], bodies[i])

# This transaction keeps the current state open for inspection
# if an error occurs (as needed for `Aristo`.).
let lapTx = c.db.beginTransaction()
defer: lapTx.dispose()
# # This transaction keeps the current state open for inspection
# # if an error occurs (as needed for `Aristo`.).
# let lapTx = c.db.newTransaction()
# defer: lapTx.dispose()

c.com.hardForkTransition(header)

Expand All @@ -125,17 +125,17 @@ proc persistBlocksImpl(c: ChainRef; headers: openArray[BlockHeader];
else:
ValidationResult.OK

when defined(nimbusDumpDebuggingMetaData):
if validationResult == ValidationResult.Error and
body.transactions.calcTxRoot == header.txRoot:
vmState.dumpDebuggingMetaData(header, body)
warn "Validation error. Debugging metadata dumped."
# when defined(nimbusDumpDebuggingMetaData):
# if validationResult == ValidationResult.Error and
# body.transactions.calcTxRoot == header.txRoot:
# vmState.dumpDebuggingMetaData(header, body)
# warn "Validation error. Debugging metadata dumped."

if validationResult != ValidationResult.OK:
return err("Failed to validate block")

if c.generateWitness:
let dbTx = c.db.beginTransaction()
let dbTx = c.db.newTransaction()
defer: dbTx.dispose()

let
Expand Down Expand Up @@ -167,7 +167,7 @@ proc persistBlocksImpl(c: ChainRef; headers: openArray[BlockHeader];
c.com.syncCurrent = header.blockNumber

# Done with this block
lapTx.commit()
# lapTx.commit()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjfh Was this a mistake?


txs += body.transactions.len

Expand Down
9 changes: 2 additions & 7 deletions nimbus/core/executor/process_block.nim
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ proc processBlock*(
## implementations (but can be savely removed, as well.)
## variant of `processBlock()` where the `header` argument is explicitely set.

var dbTx = vmState.com.db.beginTransaction()
var dbTx = vmState.com.db.newTransaction()
defer: dbTx.dispose()

if not vmState.procBlkPreamble(header, body):
Expand All @@ -177,12 +177,7 @@ proc processBlock*(
if not vmState.procBlkEpilogue(header, body):
return ValidationResult.Error

# `applyDeletes = false`
# If the trie pruning activated, each of the block will have its own state
# trie keep intact, rather than destroyed by trie pruning. But the current
# block will still get a pruned trie. If trie pruning deactivated,
# `applyDeletes` have no effects.
dbTx.commit(applyDeletes = false)
dbTx.commit()

ValidationResult.OK

Expand Down
12 changes: 7 additions & 5 deletions nimbus/core/tx_pool/tx_tasks/tx_packer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type

TxPackerStateRef = ref object
xp: TxPoolRef
tr: CoreDbMptRef
tr: CoreDxMptRef
cleanState: bool
balance: UInt256
blobGasUsed: uint64
Expand Down Expand Up @@ -139,7 +139,8 @@ proc runTxCommit(pst: TxPackerStateRef; item: TxItemRef; gasBurned: GasInt)
pst.blobGasUsed += item.tx.getTotalBlobGas

# Update txRoot
pst.tr.put(rlp.encode(inx), rlp.encode(item.tx))
pst.tr.merge(rlp.encode(inx), rlp.encode(item.tx)).isOkOr:
raiseAssert "runTxCommit(): merge failed, " & $$error

# Add the item to the `packed` bucket. This implicitely increases the
# receipts index `inx` at the next visit of this function.
Expand Down Expand Up @@ -173,7 +174,7 @@ proc vmExecInit(xp: TxPoolRef): Result[TxPackerStateRef, string]

let packer = TxPackerStateRef( # return value
xp: xp,
tr: AristoDbMemory.newCoreDbRef().mptPrune,
tr: AristoDbMemory.newCoreDbRef().ctx.getMpt CtGeneric,
balance: xp.chain.vmState.readOnlyStateDB.getBalance(xp.chain.feeRecipient),
numBlobPerBlock: 0,
)
Expand Down Expand Up @@ -257,7 +258,8 @@ proc vmExecCommit(pst: TxPackerStateRef)
vmState.receipts.setLen(nItems)

xp.chain.receipts = vmState.receipts
xp.chain.txRoot = pst.tr.rootHash
xp.chain.txRoot = pst.tr.getColumn.state.valueOr:
raiseAssert "vmExecCommit(): state() failed " & $$error
xp.chain.stateRoot = vmState.stateDB.rootHash

if vmState.com.forkGTE(Cancun):
Expand All @@ -281,7 +283,7 @@ proc packerVmExec*(xp: TxPoolRef): Result[void, string] {.gcsafe,raises: [Catcha
## Rebuild `packed` bucket by selection items from the `staged` bucket
## after executing them in the VM.
let db = xp.chain.com.db
let dbTx = db.beginTransaction
let dbTx = db.newTransaction
defer: dbTx.dispose()

var pst = xp.vmExecInit.valueOr:
Expand Down
52 changes: 28 additions & 24 deletions nimbus/db/core_db/backend/aristo_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,31 @@ proc txMethods(
levelFn: proc(): int =
aTx.level,

commitFn: proc(ignore: bool): CoreDbRc[void] =
commitFn: proc() =
const info = "commitFn()"
? adbApi.commit(aTx).toVoidRc(adbBase, info)
? kdbApi.commit(kTx).toVoidRc(kdbBase, info)
ok(),
adbApi.commit(aTx).isOkOr:
raiseAssert info & ": " & $error
kdbApi.commit(kTx).isOkOr:
raiseAssert info & ": " & $error
discard,

rollbackFn: proc(): CoreDbRc[void] =
rollbackFn: proc() =
const info = "rollbackFn()"
? adbApi.rollback(aTx).toVoidRc(adbBase, info)
? kdbApi.rollback(kTx).toVoidRc(kdbBase, info)
ok(),
adbApi.rollback(aTx).isOkOr:
raiseAssert info & ": " & $error
kdbApi.rollback(kTx).isOkOr:
raiseAssert info & ": " & $error
discard,

disposeFn: proc(): CoreDbRc[void] =
disposeFn: proc() =
const info = "disposeFn()"
if adbApi.isTop(aTx): ? adbApi.rollback(aTx).toVoidRc(adbBase, info)
if kdbApi.isTop(kTx): ? kdbApi.rollback(kTx).toVoidRc(kdbBase, info)
ok(),

safeDisposeFn: proc(): CoreDbRc[void] =
const info = "safeDisposeFn()"
if adbApi.isTop(aTx): ? adbApi.rollback(aTx).toVoidRc(adbBase, info)
if kdbApi.isTop(kTx): ? kdbApi.rollback(kTx).toVoidRc(kdbBase, info)
ok())
if adbApi.isTop(aTx):
adbApi.rollback(aTx).isOkOr:
raiseAssert info & ": " & $error
if kdbApi.isTop(kTx):
kdbApi.rollback(kTx).isOkOr:
raiseAssert info & ": " & $error
discard)

proc cptMethods(
tracer: AristoTracerRef;
Expand Down Expand Up @@ -158,8 +160,8 @@ proc baseMethods(db: AristoCoreDbRef): CoreDbBaseFns =
errorPrintFn: proc(e: CoreDbErrorRef): string =
e.errorPrint(),

newKvtFn: proc(offSite: bool): CoreDbRc[CoreDxKvtRef] =
kBase.newKvtHandler(offSite, "newKvtFn()"),
newKvtFn: proc(): CoreDbRc[CoreDxKvtRef] =
kBase.newKvtHandler("newKvtFn()"),

newCtxFn: proc(): CoreDbCtxRef =
aBase.ctx,
Expand All @@ -170,11 +172,13 @@ proc baseMethods(db: AristoCoreDbRef): CoreDbBaseFns =
swapCtxFn: proc(ctx: CoreDbCtxRef): CoreDbCtxRef =
aBase.swapCtx(ctx),

beginFn: proc(): CoreDbRc[CoreDxTxRef] =
beginFn: proc(): CoreDxTxRef =
const info = "beginFn()"
let dsc = CoreDxTxRef(
methods: db.txMethods(? aBase.txBegin info, ? kBase.txBegin info))
ok(db.bless dsc),
let
aTx = aBase.txBegin info
kTx = kBase.txBegin info
dsc = CoreDxTxRef(methods: db.txMethods(aTx, kTx))
db.bless(dsc),

newCaptureFn: proc(flags: set[CoreDbCaptFlags]): CoreDbRc[CoreDxCaptRef] =
ok(db.bless flags.tracerSetup()),
Expand Down
21 changes: 9 additions & 12 deletions nimbus/db/core_db/backend/aristo_db/handlers_aristo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,7 @@ proc mptMethods(cMpt: AristoCoreDxMptRef): CoreDbMptFns =
mptHasPath(k),

getColFn: proc(): CoreDbColRef =
mptColFn(),

isPruningFn: proc(): bool =
true)
mptColFn())

# ------------------------------------------------------------------------------
# Private account call back functions
Expand Down Expand Up @@ -419,10 +416,7 @@ proc accMethods(cAcc: AristoCoreDxAccRef): CoreDbAccFns =
accHasPath(address),

getColFn: proc(): CoreDbColRef =
getColFn(),

isPruningFn: proc(): bool =
true)
getColFn())

# ------------------------------------------------------------------------------
# Private context call back functions
Expand Down Expand Up @@ -543,10 +537,10 @@ proc ctxMethods(cCtx: AristoCoreDbCtxRef): CoreDbCtxFns =
): CoreDbRc[CoreDbColRef] =
ctxNewCol(col, colState, address),

getMptFn: proc(col: CoreDbColRef; prune: bool): CoreDbRc[CoreDxMptRef] =
getMptFn: proc(col: CoreDbColRef): CoreDbRc[CoreDxMptRef] =
ctxGetMpt(col),

getAccFn: proc(col: CoreDbColRef; prune: bool): CoreDbRc[CoreDxAccRef] =
getAccFn: proc(col: CoreDbColRef): CoreDbRc[CoreDxAccRef] =
ctxGetAcc(col),

forgetFn: proc() =
Expand Down Expand Up @@ -606,8 +600,11 @@ func txTop*(
proc txBegin*(
base: AristoBaseRef;
info: static[string];
): CoreDbRc[AristoTxRef] =
base.api.txBegin(base.ctx.mpt).toRc(base, info)
): AristoTxRef =
let rc = base.api.txBegin(base.ctx.mpt)
if rc.isErr:
raiseAssert info & ": " & $rc.error
rc.value

proc getLevel*(base: AristoBaseRef): int =
base.api.level(base.ctx.mpt)
Expand Down
Loading
Loading