Skip to content

Commit

Permalink
Fix and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Dec 26, 2024
1 parent 5f0d96b commit 8073bd1
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 242 deletions.
11 changes: 6 additions & 5 deletions hive_integration/nodocker/engine/tx_sender.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ proc createAccount(idx: int): TestAccount =
quit(QuitFailure)
result.address = toAddress(result.key)

proc createAccounts(sender: TxSender) =
for i in 0..<TestAccountCount:
proc createAccounts(sender: TxSender, numAccounts: int) =
for i in 0..<numAccounts:
sender.accounts.add createAccount(i.int)

proc getNextAccount*(sender: TxSender): TestAccount =
Expand All @@ -113,14 +113,15 @@ proc getLastNonce(sender: TxSender, address: Address): uint64 =
sender.nonceMap[address] - 1

proc fillBalance(sender: TxSender, params: NetworkParams) =
const balance = UInt256.fromHex("0x123450000000000000000")
for x in sender.accounts:
params.genesis.alloc[x.address] = GenesisAccount(
balance: UInt256.fromHex("0x123450000000000000000"),
balance: balance,
)

proc new*(_: type TxSender, params: NetworkParams): TxSender =
proc new*(_: type TxSender, params: NetworkParams, numAccounts = TestAccountCount): TxSender =
result = TxSender(chainId: params.config.chainId)
result.createAccounts()
result.createAccounts(numAccounts)
result.fillBalance(params)

proc getTxType(tc: BaseTx, nonce: uint64): TxType =
Expand Down
50 changes: 27 additions & 23 deletions nimbus/core/tx_pool/tx_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,29 @@ proc updateVmState*(xp: TxPoolRef) =
# Public functions
# ------------------------------------------------------------------------------

proc getItem*(xp: TxPoolRef, id: Hash32): Result[TxItemRef, TxError] =
let item = xp.idTab.getOrDefault(id)
if item.isNil:
return err(txErrorItemNotFound)
ok(item)

proc removeTx*(xp: TxPoolRef, id: Hash32) =
let item = xp.getItem(id).valueOr:
return
xp.removeFromSenderTab(item)
xp.idTab.del(id)

proc removeExpiredTxs*(xp: TxPoolRef, lifeTime: Duration = TX_ITEM_LIFETIME) =
var expired = newSeqOfCap[Hash32](xp.idTab.len div 4)
let now = utcNow()

for txHash, item in xp.idTab:
if now - item.time > lifeTime:
expired.add txHash

for txHash in expired:
xp.removeTx(txHash)

proc addTx*(xp: TxPoolRef, ptx: PooledTransaction): Result[void, TxError] =
if not ptx.tx.validateChainId(xp.chain.com.chainId):
return err(txErrorChainIdMismatch)
Expand Down Expand Up @@ -276,6 +299,9 @@ proc addTx*(xp: TxPoolRef, ptx: PooledTransaction): Result[void, TxError] =
if not xp.classifyValid(ptx.tx, sender):
return err(txErrorTxInvalid)

if xp.idTab.len >= MAX_POOL_SIZE:
xp.removeExpiredTxs()

if xp.idTab.len >= MAX_POOL_SIZE:
return err(txErrorPoolIsFull)

Expand All @@ -287,30 +313,8 @@ proc addTx*(xp: TxPoolRef, ptx: PooledTransaction): Result[void, TxError] =
proc addTx*(xp: TxPoolRef, tx: Transaction): Result[void, TxError] =
xp.addTx(PooledTransaction(tx: tx))

proc getItem*(xp: TxPoolRef, id: Hash32): Result[TxItemRef, TxError] =
let item = xp.idTab.getOrDefault(id)
if item.isNil:
return err(txErrorItemNotFound)
ok(item)

proc removeTx*(xp: TxPoolRef, id: Hash32) =
let item = xp.getItem(id).valueOr:
return
xp.removeFromSenderTab(item)
xp.idTab.del(id)

proc removeExpiredTxs*(xp: TxPoolRef, lifeTime: Duration = TX_ITEM_LIFETIME) =
var expired = newSeqOfCap[Hash32](xp.idTab.len div 4)
let now = utcNow()

for txHash, item in xp.idTab:
if now - item.time > lifeTime:
expired.add txHash

for txHash in expired:
xp.removeTx(txHash)

iterator byPriceAndNonce*(xp: TxPoolRef): TxItemRef =
for item in byPriceAndNonce(xp.senderTab,
for item in byPriceAndNonce(xp.senderTab, xp.idTab,
xp.vmState.ledger, xp.baseFee):
yield item
26 changes: 17 additions & 9 deletions nimbus/core/tx_pool/tx_tabs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func len*(sn: TxSenderNonceRef): auto =
sn.list.len

iterator byPriceAndNonce*(senderTab: TxSenderTab,
idTab: var TxIdTab,
ledger: LedgerRef,
baseFee: GasInt): TxItemRef =
template removeFirstAndPushTo(sn, byPrice) =
Expand All @@ -54,25 +55,32 @@ iterator byPriceAndNonce*(senderTab: TxSenderTab,
byPrice.push(rc.data)

var byNonce: TxSenderTab
for address, sn in senderTab:
# Check if the account nonce matches the lowest known tx nonce
for address, sn in senderTab:
var
nonce = ledger.getNonce(address)
rc = sn.list.eq(nonce)
nonce = ledger.getNonce(address)
sortedByNonce: TxSenderNonceRef


# Remove item with nonce lower than current account.
# Happen when proposed block rejected.
var rc = sn.list.lt(nonce)
while rc.isOk:
let item = rc.get.data
idTab.del(item.id)
discard sn.list.delete(item.nonce)
rc = sn.list.lt(nonce)

# Check if the account nonce matches the lowest known tx nonce
rc = sn.list.ge(nonce)
while rc.isOk:
let item = rc.get.data
item.calculatePrice(baseFee)
if item.nonce != nonce:
# a gap in nonce, stop collecting
break


if sortedByNonce.isNil:
sortedByNonce = TxSenderNonceRef.init()
byNonce[address] = sortedByNonce

sortedByNonce.insertOrReplace(item)
# If there is a gap, sn.list.eq will return isErr
nonce = item.nonce + 1
rc = sn.list.eq(nonce)

Expand Down
2 changes: 1 addition & 1 deletion nimbus/core/validate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ proc validateTxBasic*(

# The total must be the larger of the two
if tx.maxFeePerGasNorm < tx.maxPriorityFeePerGasNorm:
return err(&"invalid tx: maxFee is smaller than maPriorityFee. maxFee={tx.maxFeePerGas}, maxPriorityFee={tx.maxPriorityFeePerGasNorm}")
return err(&"invalid tx: maxFee is smaller than maxPriorityFee. maxFee={tx.maxFeePerGas}, maxPriorityFee={tx.maxPriorityFeePerGasNorm}")

if tx.gasLimit < tx.intrinsicGas(fork):
return err(&"invalid tx: not enough gas to perform calculation. avail={tx.gasLimit}, require={tx.intrinsicGas(fork)}")
Expand Down
Loading

0 comments on commit 8073bd1

Please sign in to comment.