Skip to content

Commit

Permalink
reduce tx naming overload
Browse files Browse the repository at this point in the history
* if it's a db function, use `txFrame...`
* if it's not a db function, don't use `txFrame...`
  • Loading branch information
arnetheduck committed Dec 18, 2024
1 parent 7bbb0f4 commit 4f4a30f
Show file tree
Hide file tree
Showing 25 changed files with 147 additions and 342 deletions.
4 changes: 2 additions & 2 deletions hive_integration/nodocker/engine/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ proc processBlock(
## implementations (but can be savely removed, as well.)
## variant of `processBlock()` where the `header` argument is explicitely set.
template header: Header = blk.header
var dbTx = vmState.com.db.ctx.newTransaction()
var dbTx = vmState.com.db.ctx.txFrameBegin()
defer: dbTx.dispose()

let com = vmState.com
Expand Down Expand Up @@ -89,7 +89,7 @@ proc getVmState(c: ChainRef, header: Header):
# intended to accepts invalid block
proc setBlock*(c: ChainRef; blk: Block): Result[void, string] =
template header: Header = blk.header
let dbTx = c.db.ctx.newTransaction()
let dbTx = c.db.ctx.txFrameBegin()
defer: dbTx.dispose()

# Needed for figuring out whether KVT cleanup is due (see at the end)
Expand Down
16 changes: 8 additions & 8 deletions nimbus/core/chain/forked_chain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ proc validateBlock(c: ForkedChainRef,
parent: Header,
blk: Block,
updateCursor: bool = true): Result[void, string] =
let dbTx = c.db.ctx.newTransaction()
let dbTx = c.db.ctx.txFrameBegin()
defer:
dbTx.dispose()

Expand Down Expand Up @@ -170,7 +170,7 @@ proc replaySegment*(c: ForkedChainRef, target: Hash32) =
prevHash = chain[^1].header.parentHash

c.stagingTx.rollback()
c.stagingTx = c.db.ctx.newTransaction()
c.stagingTx = c.db.ctx.txFrameBegin()
c.cursorHeader = c.baseHeader
for i in countdown(chain.high, chain.low):
c.validateBlock(c.cursorHeader, chain[i],
Expand Down Expand Up @@ -432,7 +432,7 @@ proc updateHeadIfNecessary(c: ForkedChainRef, pvarc: PivotArc) =
if c.cursorHash != pvarc.cursor.hash:
if not c.stagingTx.isNil:
c.stagingTx.rollback()
c.stagingTx = c.db.ctx.newTransaction()
c.stagingTx = c.db.ctx.txFrameBegin()
c.replaySegment(pvarc.pvHash)

c.trimCursorArc(pvarc)
Expand All @@ -442,7 +442,7 @@ proc updateHeadIfNecessary(c: ForkedChainRef, pvarc: PivotArc) =

if c.stagingTx.isNil:
# setHead below don't go straight to db
c.stagingTx = c.db.ctx.newTransaction()
c.stagingTx = c.db.ctx.txFrameBegin()

c.setHead(pvarc)

Expand Down Expand Up @@ -511,7 +511,7 @@ proc importBlock*(c: ForkedChainRef, blk: Block): Result[void, string] =
# Try to import block to canonical or side chain.
# return error if the block is invalid
if c.stagingTx.isNil:
c.stagingTx = c.db.ctx.newTransaction()
c.stagingTx = c.db.ctx.txFrameBegin()

template header(): Header =
blk.header
Expand All @@ -521,7 +521,7 @@ proc importBlock*(c: ForkedChainRef, blk: Block): Result[void, string] =

if header.parentHash == c.baseHash:
c.stagingTx.rollback()
c.stagingTx = c.db.ctx.newTransaction()
c.stagingTx = c.db.ctx.txFrameBegin()
return c.validateBlock(c.baseHeader, blk)

if header.parentHash notin c.blocks:
Expand Down Expand Up @@ -601,7 +601,7 @@ proc forkChoice*(c: ForkedChainRef,

# Write segment from base+1 to newBase into database
c.stagingTx.rollback()
c.stagingTx = c.db.ctx.newTransaction()
c.stagingTx = c.db.ctx.txFrameBegin()

if newBase.pvNumber > c.baseHeader.number:
c.replaySegment(newBase.pvHash)
Expand All @@ -616,7 +616,7 @@ proc forkChoice*(c: ForkedChainRef,
if c.stagingTx.isNil:
# replaySegment or setHead below don't
# go straight to db
c.stagingTx = c.db.ctx.newTransaction()
c.stagingTx = c.db.ctx.txFrameBegin()

# Move chain state forward to current head
if newBase.pvNumber < pvarc.pvNumber:
Expand Down
3 changes: 3 additions & 0 deletions nimbus/core/chain/persist_blocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ proc persistBlock*(p: var Persister, blk: Block): Result[void, string] =

let c = p.c

if p.dbTx == nil:
p.dbTx = p.c.db.ctx.txFrameBegin()

# Full validation means validating the state root at every block and
# performing the more expensive hash computations on the block itself, ie
# verifying that the transaction and receipts roots are valid - when not
Expand Down
2 changes: 1 addition & 1 deletion nimbus/core/tx_pool/tx_packer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ proc packerVmExec*(xp: TxPoolRef): Result[TxPacker, string]
## Rebuild `packed` bucket by selection items from the `staged` bucket
## after executing them in the VM.
let db = xp.vmState.com.db
let dbTx = db.ctx.newTransaction()
let dbTx = db.ctx.txFrameBegin()
defer: dbTx.dispose()

var pst = xp.vmExecInit.valueOr:
Expand Down
60 changes: 22 additions & 38 deletions nimbus/db/aristo/aristo_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ type
## Getter, returns `true` if the argument `tx` referes to the current
## top level transaction.

AristoApiLevelFn* =
AristoApiTxFrameLevelFn* =
proc(db: AristoDbRef;
): int
{.noRaise.}
Expand Down Expand Up @@ -310,7 +310,7 @@ type
## operations performed for this transactio. The previous transaction
## is returned if there was any.

AristoApiTxBeginFn* =
AristoApiTxFrameBeginFn* =
proc(db: AristoDbRef;
): Result[AristoTxRef,AristoError]
{.noRaise.}
Expand All @@ -324,13 +324,7 @@ type
## ... continue using db ...
## tx.commit()

AristoApiTxLevelFn* =
proc(tx: AristoTxRef;
): int
{.noRaise.}
## Getter, positive nesting level of transaction argument `tx`

AristoApiTxTopFn* =
AristoApiTxFrameTopFn* =
proc(db: AristoDbRef;
): Result[AristoTxRef,AristoError]
{.noRaise.}
Expand Down Expand Up @@ -358,7 +352,7 @@ type
hasStorageData*: AristoApiHasStorageDataFn

isTop*: AristoApiIsTopFn
level*: AristoApiLevelFn
txFrameLevel*: AristoApiTxFrameLevelFn

mergeAccountRecord*: AristoApiMergeAccountRecordFn
mergeStorageData*: AristoApiMergeStorageDataFn
Expand All @@ -373,9 +367,8 @@ type
pathAsBlob*: AristoApiPathAsBlobFn
persist*: AristoApiPersistFn
rollback*: AristoApiRollbackFn
txBegin*: AristoApiTxBeginFn
txLevel*: AristoApiTxLevelFn
txTop*: AristoApiTxTopFn
txFrameBegin*: AristoApiTxFrameBeginFn
txFrameTop*: AristoApiTxFrameTopFn


AristoApiProfNames* = enum
Expand Down Expand Up @@ -414,9 +407,8 @@ type
AristoApiProfPathAsBlobFn = "pathAsBlob"
AristoApiProfPersistFn = "persist"
AristoApiProfRollbackFn = "rollback"
AristoApiProfTxBeginFn = "txBegin"
AristoApiProfTxLevelFn = "txLevel"
AristoApiProfTxTopFn = "txTop"
AristoApiProfTxFrameBeginFn = "txFrameBegin"
AristoApiProfTxFrameTopFn = "txFrameTop"

AristoApiProfBeGetVtxFn = "be/getVtx"
AristoApiProfBeGetKeyFn = "be/getKey"
Expand Down Expand Up @@ -471,9 +463,8 @@ when AutoValidateApiHooks:
doAssert not api.pathAsBlob.isNil
doAssert not api.persist.isNil
doAssert not api.rollback.isNil
doAssert not api.txBegin.isNil
doAssert not api.txLevel.isNil
doAssert not api.txTop.isNil
doAssert not api.txFrameBegin.isNil
doAssert not api.txFrameTop.isNil

proc validate(prf: AristoApiProfRef) =
prf.AristoApiRef.validate
Expand Down Expand Up @@ -520,7 +511,7 @@ func init*(api: var AristoApiObj) =
api.hasStorageData = hasStorageData

api.isTop = isTop
api.level = level
api.txFrameLevel = txFrameLevel

api.mergeAccountRecord = mergeAccountRecord
api.mergeStorageData = mergeStorageData
Expand All @@ -533,9 +524,8 @@ func init*(api: var AristoApiObj) =
api.pathAsBlob = pathAsBlob
api.persist = persist
api.rollback = rollback
api.txBegin = txBegin
api.txLevel = txLevel
api.txTop = txTop
api.txFrameBegin = txFrameBegin
api.txFrameTop = txFrameTop
when AutoValidateApiHooks:
api.validate

Expand Down Expand Up @@ -564,7 +554,7 @@ func dup*(api: AristoApiRef): AristoApiRef =
hasStorageData: api.hasStorageData,

isTop: api.isTop,
level: api.level,
txFrameLevel: api.txFrameLevel,

mergeAccountRecord: api.mergeAccountRecord,
mergeStorageData: api.mergeStorageData,
Expand All @@ -577,9 +567,8 @@ func dup*(api: AristoApiRef): AristoApiRef =
pathAsBlob: api.pathAsBlob,
persist: api.persist,
rollback: api.rollback,
txBegin: api.txBegin,
txLevel: api.txLevel,
txTop: api.txTop)
txFrameBegin: api.txFrameBegin,
txFrameTop: api.txFrameTop)
when AutoValidateApiHooks:
result.validate

Expand Down Expand Up @@ -730,20 +719,15 @@ func init*(
AristoApiProfRollbackFn.profileRunner:
result = api.rollback(a)

profApi.txBegin =
profApi.txFrameBegin =
proc(a: AristoDbRef): auto =
AristoApiProfTxBeginFn.profileRunner:
result = api.txBegin(a)

profApi.txLevel =
proc(a: AristoTxRef): auto =
AristoApiProfTxLevelFn.profileRunner:
result = api.txLevel(a)
AristoApiProfTxFrameBeginFn.profileRunner:
result = api.txFrameBegin(a)

profApi.txTop =
profApi.txFrameTop =
proc(a: AristoDbRef): auto =
AristoApiProfTxTopFn.profileRunner:
result = api.txTop(a)
AristoApiProfTxFrameTopFn.profileRunner:
result = api.txFrameTop(a)

let beDup = be.dup()
if beDup.isNil:
Expand Down
4 changes: 2 additions & 2 deletions nimbus/db/aristo/aristo_desc/desc_error.nim
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ type
TxArgStaleTx
TxArgsUseless
TxBackendNotWritable
TxLevelTooDeep
TxLevelUseless
TxFrameLevelTooDeep
TxFrameLevelUseless
TxNoPendingTx
TxNotFound
TxNotTopTx
Expand Down
81 changes: 2 additions & 79 deletions nimbus/db/aristo/aristo_tx.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,7 @@ import
./aristo_tx/[tx_frame, tx_stow],
./aristo_desc

# ------------------------------------------------------------------------------
# Public functions, getters
# ------------------------------------------------------------------------------

func txTop*(db: AristoDbRef): Result[AristoTxRef,AristoError] =
## Getter, returns top level transaction if there is any.
db.txFrameTop()

func isTop*(tx: AristoTxRef): bool =
## Getter, returns `true` if the argument `tx` referes to the current top
## level transaction.
tx.txFrameIsTop()

func txLevel*(tx: AristoTxRef): int =
## Getter, positive nesting level of transaction argument `tx`
tx.txFrameLevel()

func level*(db: AristoDbRef): int =
## Getter, non-negative nesting level (i.e. number of pending transactions)
db.txFrameLevel()
export tx_frame

# ------------------------------------------------------------------------------
# Public functions
Expand All @@ -51,51 +32,6 @@ func to*(tx: AristoTxRef; T: type[AristoDbRef]): T =
# Public functions: Transaction frame
# ------------------------------------------------------------------------------

proc txBegin*(db: AristoDbRef): Result[AristoTxRef,AristoError] =
## Starts a new transaction.
##
## Example:
## ::
## proc doSomething(db: AristoDbRef) =
## let tx = db.begin
## defer: tx.rollback()
## ... continue using db ...
## tx.commit()
##
db.txFrameBegin()

proc rollback*(
tx: AristoTxRef; # Top transaction on database
): Result[void,AristoError] =
## Given a *top level* handle, this function discards all database operations
## performed for this transactio. The previous transaction is returned if
## there was any.
##
tx.txFrameRollback()

proc commit*(
tx: AristoTxRef; # Top transaction on database
): Result[void,AristoError] =
## Given a *top level* handle, this function accepts all database operations
## performed through this handle and merges it to the previous layer. The
## previous transaction is returned if there was any.
##
tx.txFrameCommit()

proc collapse*(
tx: AristoTxRef; # Top transaction on database
commit: bool; # Commit if `true`, otherwise roll back
): Result[void,AristoError] =
## Iterated application of `commit()` or `rollback()` performing the
## something similar to
## ::
## while true:
## discard tx.commit() # ditto for rollback()
## if db.txTop.isErr: break
## tx = db.txTop.value
##
tx.txFrameCollapse commit

# ------------------------------------------------------------------------------
# Public functions: save to database
# ------------------------------------------------------------------------------
Expand All @@ -119,20 +55,7 @@ proc persist*(
## next recovery journal record. If non-zero, this ID must be greater than
## all previous IDs (e.g. block number when stowing after block execution.)
##
db.txStow(nxtSid, persistent=true)

proc stow*(
db: AristoDbRef; # Database
): Result[void,AristoError] =
## This function is similar to `persist()` stopping short of performing the
## final step storing on the persistent database. It fails if there is a
## pending transaction.
##
## The function merges all staged data from the top layer cache onto the
## backend stage area and leaves it there. This function can be seen as
## a sort of a bottom level transaction `commit()`.
##
db.txStow(nxtSid=0u64, persistent=false)
db.txPersist(nxtSid)

# ------------------------------------------------------------------------------
# End
Expand Down
Loading

0 comments on commit 4f4a30f

Please sign in to comment.