2022-01-18 14:40:02 +00:00
|
|
|
# Nimbus
|
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
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-01-18 14:40:02 +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.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[algorithm, os, sequtils, strformat, tables, times],
|
2023-08-04 11:10:09 +00:00
|
|
|
../../nimbus/core/[chain, tx_pool], # must be early (compilation annoyance)
|
|
|
|
../../nimbus/common/common,
|
2022-12-02 04:39:12 +00:00
|
|
|
../../nimbus/[config, constants],
|
|
|
|
../../nimbus/utils/ec_recover,
|
|
|
|
../../nimbus/core/tx_pool/[tx_chain, tx_item],
|
2022-01-18 14:40:02 +00:00
|
|
|
./helpers,
|
|
|
|
./sign_helper,
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/[keys, p2p],
|
|
|
|
stew/[keyed_queue]
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc setStatus(xp: TxPoolRef; item: TxItemRef; status: TxItemStatus)
|
2023-05-10 16:04:35 +00:00
|
|
|
{.gcsafe,raises: [CatchableError].} =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Change/update the status of the transaction item.
|
|
|
|
if status != item.status:
|
|
|
|
discard xp.txDB.reassign(item, status)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
proc importBlocks(c: ChainRef; h: seq[BlockHeader]; b: seq[BlockBody]): int =
|
2022-01-18 14:40:02 +00:00
|
|
|
if c.persistBlocks(h,b) != ValidationResult.OK:
|
|
|
|
raiseAssert "persistBlocks() failed at block #" & $h[0].blockNumber
|
|
|
|
for body in b:
|
|
|
|
result += body.transactions.len
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
proc blockChainForTesting*(network: NetworkID): CommonRef =
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
result = CommonRef.new(
|
2023-08-04 11:10:09 +00:00
|
|
|
newCoreDbRef LegacyDbMemory,
|
2022-12-02 04:39:12 +00:00
|
|
|
networkId = network,
|
2022-01-18 14:40:02 +00:00
|
|
|
params = network.networkParams)
|
|
|
|
|
|
|
|
result.initializeEmptyDB
|
|
|
|
|
|
|
|
proc toTxPool*(
|
2022-12-02 04:39:12 +00:00
|
|
|
com: CommonRef; ## to be modified
|
2022-01-18 14:40:02 +00:00
|
|
|
file: string; ## input, file and transactions
|
|
|
|
getStatus: proc(): TxItemStatus; ## input, random function
|
|
|
|
loadBlocks: int; ## load at most this many blocks
|
|
|
|
minBlockTxs: int; ## load at least this many txs in blocks
|
|
|
|
loadTxs: int; ## load at most this many transactions
|
|
|
|
baseFee = 0.GasPrice; ## initalise with `baseFee` (unless 0)
|
|
|
|
noisy: bool): (TxPoolRef, int) =
|
|
|
|
|
|
|
|
var
|
2022-12-02 04:39:12 +00:00
|
|
|
txCount = 0
|
|
|
|
chainNo = 0
|
|
|
|
chainRef = com.newChain
|
2022-01-18 14:40:02 +00:00
|
|
|
nTxs = 0
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
doAssert not com.isNil
|
|
|
|
result[0] = TxPoolRef.new(com,testAddress)
|
2022-01-18 14:40:02 +00:00
|
|
|
result[0].baseFee = baseFee
|
|
|
|
|
2023-04-21 21:11:04 +00:00
|
|
|
for chain in file.undumpBlocks:
|
2022-01-18 14:40:02 +00:00
|
|
|
let leadBlkNum = chain[0][0].blockNumber
|
|
|
|
chainNo.inc
|
|
|
|
|
|
|
|
if loadTxs <= txCount:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Verify Genesis
|
|
|
|
if leadBlkNum == 0.u256:
|
2022-12-02 04:39:12 +00:00
|
|
|
doAssert chain[0][0] == com.db.getBlockHeader(0.u256)
|
2022-01-18 14:40:02 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if leadBlkNum < loadBlocks.u256 or nTxs < minBlockTxs:
|
2022-12-02 04:39:12 +00:00
|
|
|
nTxs += chainRef.importBlocks(chain[0],chain[1])
|
2022-01-18 14:40:02 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Import transactions
|
|
|
|
for inx in 0 ..< chain[0].len:
|
|
|
|
let
|
|
|
|
num = chain[0][inx].blockNumber
|
|
|
|
txs = chain[1][inx].transactions
|
|
|
|
|
|
|
|
# Continue importing up until first non-trivial block
|
|
|
|
if txCount == 0 and txs.len == 0:
|
2022-12-02 04:39:12 +00:00
|
|
|
nTxs += chainRef.importBlocks(@[chain[0][inx]],@[chain[1][inx]])
|
2022-01-18 14:40:02 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Load transactions, one-by-one
|
|
|
|
for n in 0 ..< min(txs.len, loadTxs - txCount):
|
|
|
|
txCount.inc
|
|
|
|
let
|
|
|
|
status = statusInfo[getStatus()]
|
|
|
|
info = &"{txCount} #{num}({chainNo}) {n}/{txs.len} {status}"
|
|
|
|
noisy.showElapsed(&"insert: {info}"):
|
2022-04-08 14:05:30 +00:00
|
|
|
result[0].add(txs[n], info)
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
if loadTxs <= txCount:
|
|
|
|
break
|
|
|
|
|
|
|
|
result[1] = nTxs
|
|
|
|
|
|
|
|
|
|
|
|
proc toTxPool*(
|
2022-12-02 04:39:12 +00:00
|
|
|
com: CommonRef; ## to be modified, initialisier for `TxPool`
|
2022-03-31 08:21:36 +00:00
|
|
|
itList: seq[TxItemRef]; ## import items into new `TxPool` (read only)
|
|
|
|
baseFee = 0.GasPrice; ## initalise with `baseFee` (unless 0)
|
|
|
|
local: seq[EthAddress] = @[]; ## local addresses
|
2022-01-18 14:40:02 +00:00
|
|
|
noisy = true): TxPoolRef =
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
doAssert not com.isNil
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
result = TxPoolRef.new(com,testAddress)
|
2022-01-18 14:40:02 +00:00
|
|
|
result.baseFee = baseFee
|
|
|
|
result.maxRejects = itList.len
|
|
|
|
|
2022-03-31 08:21:36 +00:00
|
|
|
let noLocals = local.len == 0
|
|
|
|
var localAddr: Table[EthAddress,bool]
|
|
|
|
for a in local:
|
|
|
|
localAddr[a] = true
|
|
|
|
|
2022-01-18 14:40:02 +00:00
|
|
|
noisy.showElapsed(&"Loading {itList.len} transactions"):
|
|
|
|
for item in itList:
|
2022-03-31 08:21:36 +00:00
|
|
|
if noLocals:
|
2022-04-08 14:05:30 +00:00
|
|
|
result.add(item.tx, item.info)
|
2022-03-31 08:21:36 +00:00
|
|
|
elif localAddr.hasKey(item.sender):
|
|
|
|
doAssert result.addLocal(item.tx, true).isOk
|
|
|
|
else:
|
|
|
|
doAssert result.addRemote(item.tx, true).isOk
|
2022-01-18 14:40:02 +00:00
|
|
|
doAssert result.nItems.total == itList.len
|
|
|
|
|
|
|
|
|
|
|
|
proc toTxPool*(
|
2022-12-02 04:39:12 +00:00
|
|
|
com: CommonRef; ## to be modified, initialisier for `TxPool`
|
2022-03-31 08:21:36 +00:00
|
|
|
timeGap: var Time; ## to be set, time in the middle of time gap
|
|
|
|
nGapItems: var int; ## to be set, # items before time gap
|
|
|
|
itList: var seq[TxItemRef]; ## import items into new `TxPool` (read only)
|
|
|
|
baseFee = 0.GasPrice; ## initalise with `baseFee` (unless 0)
|
|
|
|
itemsPC = 30; ## % number if items befor time gap
|
|
|
|
delayMSecs = 200; ## size of time vap
|
|
|
|
local: seq[EthAddress] = @[]; ## local addresses
|
2022-01-18 14:40:02 +00:00
|
|
|
noisy = true): TxPoolRef =
|
|
|
|
## Variant of `toTxPoolFromSeq()` with a time gap between consecutive
|
|
|
|
## items on the `remote` queue
|
2022-12-02 04:39:12 +00:00
|
|
|
doAssert not com.isNil
|
2022-01-18 14:40:02 +00:00
|
|
|
doAssert 0 < itemsPC and itemsPC < 100
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
result = TxPoolRef.new(com,testAddress)
|
2022-01-18 14:40:02 +00:00
|
|
|
result.baseFee = baseFee
|
|
|
|
result.maxRejects = itList.len
|
|
|
|
|
2022-03-31 08:21:36 +00:00
|
|
|
let noLocals = local.len == 0
|
|
|
|
var localAddr: Table[EthAddress,bool]
|
|
|
|
for a in local:
|
|
|
|
localAddr[a] = true
|
|
|
|
|
2022-01-18 14:40:02 +00:00
|
|
|
let
|
|
|
|
delayAt = itList.len * itemsPC div 100
|
|
|
|
middleOfTimeGap = initDuration(milliSeconds = delayMSecs div 2)
|
2022-04-08 14:05:30 +00:00
|
|
|
const
|
|
|
|
tFmt = "yyyy-MM-dd'T'HH-mm-ss'.'fff"
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
noisy.showElapsed(&"Loading {itList.len} transactions"):
|
|
|
|
for n in 0 ..< itList.len:
|
|
|
|
let item = itList[n]
|
2022-03-31 08:21:36 +00:00
|
|
|
if noLocals:
|
2022-04-08 14:05:30 +00:00
|
|
|
result.add(item.tx, item.info)
|
2022-03-31 08:21:36 +00:00
|
|
|
elif localAddr.hasKey(item.sender):
|
|
|
|
doAssert result.addLocal(item.tx, true).isOk
|
|
|
|
else:
|
|
|
|
doAssert result.addRemote(item.tx, true).isOk
|
2022-04-08 14:05:30 +00:00
|
|
|
if n < 3 or delayAt-3 <= n and n <= delayAt+3 or itList.len-4 < n:
|
|
|
|
let t = result.getItem(item.itemID).value.timeStamp.format(tFmt, utc())
|
|
|
|
noisy.say &"added item {n} time={t}"
|
2022-01-18 14:40:02 +00:00
|
|
|
if delayAt == n:
|
|
|
|
nGapItems = n # pass back value
|
|
|
|
let itemID = item.itemID
|
|
|
|
doAssert result.nItems.disposed == 0
|
|
|
|
timeGap = result.getItem(itemID).value.timeStamp + middleOfTimeGap
|
2022-04-08 14:05:30 +00:00
|
|
|
let t = timegap.format(tFmt, utc())
|
|
|
|
noisy.say &"{delayMSecs}ms time gap centered around {t}"
|
2022-01-18 14:40:02 +00:00
|
|
|
delayMSecs.sleep
|
|
|
|
|
|
|
|
doAssert result.nItems.total == itList.len
|
|
|
|
doAssert result.nItems.disposed == 0
|
|
|
|
|
|
|
|
|
|
|
|
proc toItems*(xp: TxPoolRef): seq[TxItemRef] =
|
|
|
|
toSeq(xp.txDB.byItemID.nextValues)
|
|
|
|
|
|
|
|
proc toItems*(xp: TxPoolRef; label: TxItemStatus): seq[TxItemRef] =
|
|
|
|
for (_,nonceList) in xp.txDB.decAccount(label):
|
|
|
|
result.add toSeq(nonceList.incNonce)
|
|
|
|
|
|
|
|
proc setItemStatusFromInfo*(xp: TxPoolRef) =
|
|
|
|
## Re-define status from last character of info field. Note that this might
|
|
|
|
## violate boundary conditions regarding nonces.
|
|
|
|
for item in xp.toItems:
|
|
|
|
let w = TxItemStatus.toSeq.filterIt(statusInfo[it][0] == item.info[^1])[0]
|
|
|
|
xp.setStatus(item, w)
|
|
|
|
|
|
|
|
|
|
|
|
proc getBackHeader*(xp: TxPoolRef; nTxs, nAccounts: int):
|
|
|
|
(BlockHeader, seq[Transaction], seq[EthAddress]) {.inline.} =
|
|
|
|
## back track the block chain for at least `nTxs` transactions and
|
|
|
|
## `nAccounts` sender accounts
|
|
|
|
var
|
|
|
|
accTab: Table[EthAddress,bool]
|
|
|
|
txsLst: seq[Transaction]
|
|
|
|
backHash = xp.head.blockHash
|
|
|
|
backHeader = xp.head
|
2022-12-02 04:39:12 +00:00
|
|
|
backBody = xp.chain.com.db.getBlockBody(backHash)
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
while true:
|
|
|
|
# count txs and step behind last block
|
|
|
|
txsLst.add backBody.transactions
|
|
|
|
backHash = backHeader.parentHash
|
2022-12-02 04:39:12 +00:00
|
|
|
if not xp.chain.com.db.getBlockHeader(backHash, backHeader) or
|
|
|
|
not xp.chain.com.db.getBlockBody(backHash, backBody):
|
2022-01-18 14:40:02 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# collect accounts unless max reached
|
|
|
|
if accTab.len < nAccounts:
|
|
|
|
for tx in backBody.transactions:
|
|
|
|
let rc = tx.ecRecover
|
|
|
|
if rc.isOK:
|
|
|
|
if xp.txDB.bySender.eq(rc.value).isOk:
|
|
|
|
accTab[rc.value] = true
|
|
|
|
if nAccounts <= accTab.len:
|
|
|
|
break
|
|
|
|
|
|
|
|
if nTxs <= txsLst.len and nAccounts <= accTab.len:
|
|
|
|
break
|
|
|
|
# otherwise get next block
|
|
|
|
|
|
|
|
(backHeader, txsLst.reversed, toSeq(accTab.keys))
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|