Skip to content

Commit 70d7181

Browse files
committed
fix txpool usage at the beginning of process
1 parent 4d207e4 commit 70d7181

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

hive_integration/nodocker/engine/test_env.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ proc setupELClient*(t: TestEnv, chainFile: string, enableAuth: bool) =
8888
t.com.initializeEmptyDb()
8989
let txPool = TxPoolRef.new(t.com, t.conf.engineSigner)
9090

91+
# txPool must be informed of active head
92+
# so it can know the latest account state
93+
let head = t.com.db.getCanonicalHead()
94+
doAssert txPool.smartHead(head)
95+
9196
var key: JwtSharedKey
9297
let kr = key.fromHex(jwtSecret)
9398
if kr.isErr:

hive_integration/nodocker/graphql/graphql_sim.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ proc main() =
9292
var stat: SimStat
9393
let start = getTime()
9494

95-
#let fileName = caseFolder & "/37_eth_sendRawTransaction_nonceTooLow.json"
96-
#block:
95+
# txPool must be informed of active head
96+
# so it can know the latest account state
97+
# e.g. "sendRawTransaction Nonce too low" case
98+
let head = com.db.getCanonicalHead()
99+
doAssert txPool.smartHead(head)
97100

98101
for fileName in walkDirRec(
99102
caseFolder, yieldFilter = {pcFile,pcLinkToFile}):

hive_integration/nodocker/rpc/test_env.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ proc setupEnv*(): TestEnv =
105105

106106
let chainRef = newChain(com)
107107
let txPool = TxPoolRef.new(com, conf.engineSigner)
108+
109+
# txPool must be informed of active head
110+
# so it can know the latest account state
111+
let head = com.db.getCanonicalHead()
112+
doAssert txPool.smartHead(head)
113+
108114
let sealingEngine = SealingEngineRef.new(
109115
chainRef, ethCtx, conf.engineSigner,
110116
txPool, EngineStopped

nimbus/nimbus.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ proc basicServices(nimbus: NimbusNode,
8080
# the engineSigner is zero.
8181
nimbus.txPool = TxPoolRef.new(com, conf.engineSigner)
8282

83+
# txPool must be informed of active head
84+
# so it can know the latest account state
85+
# e.g. sender nonce, etc
86+
let head = com.db.getCanonicalHead()
87+
doAssert nimbus.txPool.smartHead(head)
88+
8389
# chainRef: some name to avoid module-name/filed/function misunderstandings
8490
nimbus.chainRef = newChain(com)
8591
if conf.verifyFrom.isSome:

tests/test_txpool.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ proc runTxPackerTests(noisy = true) =
853853
# even though the difficulty or the blocknumber is lower than
854854
# previous canonical head
855855
check hdr.blockHash == xq.chain.com.db.getCanonicalHead.blockHash
856-
856+
857857
# Is the withdrawals persisted and loaded properly?
858858
var blockBody: BlockBody
859859
check xq.chain.com.db.getBlockBody(hdr, blockBody)
@@ -882,6 +882,7 @@ proc txPoolMain*(noisy = defined(debug)) =
882882
noisy.runTxPackerTests
883883
runTxPoolCliqueTest()
884884
runTxPoolPosTest()
885+
runTxPoolBlobhashTest()
885886
noisy.runTxHeadDelta
886887

887888
when isMainModule:

tests/test_txpool2.nim

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import
77
../nimbus/core/clique/[clique_sealer, clique_desc],
88
../nimbus/[config, transaction, constants],
99
../nimbus/core/tx_pool,
10+
../nimbus/core/tx_pool/tx_item,
1011
../nimbus/core/casper,
1112
../nimbus/core/executor,
1213
../nimbus/common/common,
@@ -70,6 +71,11 @@ proc makeTx*(t: var TestEnv, recipient: EthAddress, amount: UInt256, payload: op
7071
inc t.nonce
7172
signTransaction(tx, t.vaultKey, t.chainId, eip155 = true)
7273

74+
proc signTxWithNonce(t: TestEnv, tx: Transaction, nonce: AccountNonce): Transaction =
75+
var tx = tx
76+
tx.nonce = nonce
77+
signTransaction(tx, t.vaultKey, t.chainId, eip155 = true)
78+
7379
proc initEnv(envFork: HardFork): TestEnv =
7480
var
7581
conf = makeConfig(@[
@@ -265,12 +271,18 @@ proc runTxPoolPosTest*() =
265271
let bal = sdb.getBalance(feeRecipient)
266272
check not bal.isZero
267273

274+
proc inPoolAndOk(txPool: TxPoolRef, txHash: Hash256): bool =
275+
let res = txPool.getItem(txHash)
276+
if res.isErr: return false
277+
res.get().reject == txInfoOk
278+
268279
proc runTxPoolBlobhashTest*() =
269280
var
270281
env = initEnv(Cancun)
271282

272283
var
273-
tx = env.makeTx(recipient, amount)
284+
tx1 = env.makeTx(recipient, amount)
285+
tx2 = env.makeTx(recipient, amount)
274286
xp = env.xp
275287
com = env.com
276288
chain = env.chain
@@ -279,14 +291,16 @@ proc runTxPoolBlobhashTest*() =
279291

280292
suite "Test TxPool with blobhash block":
281293
test "TxPool addLocal":
282-
let res = xp.addLocal(tx, force = true)
294+
let res = xp.addLocal(tx1, force = true)
283295
check res.isOk
284296
if res.isErr:
285297
debugEcho res.error
286298
return
299+
let res2 = xp.addLocal(tx2, force = true)
300+
check res2.isOk
287301

288302
test "TxPool jobCommit":
289-
check xp.nItems.total == 1
303+
check xp.nItems.total == 2
290304

291305
test "TxPool ethBlock":
292306
com.pos.prevRandao = prevRandao
@@ -302,7 +316,7 @@ proc runTxPoolBlobhashTest*() =
302316
uncles: blk.uncles,
303317
withdrawals: some[seq[Withdrawal]](@[])
304318
)
305-
check blk.txs.len == 1
319+
check blk.txs.len == 2
306320

307321
test "Blobhash persistBlocks":
308322
let rr = chain.persistBlocks([blk.header], [body])
@@ -321,6 +335,21 @@ proc runTxPoolBlobhashTest*() =
321335
let bal = sdb.getBalance(feeRecipient)
322336
check not bal.isZero
323337

338+
test "add tx with nonce too low":
339+
let
340+
tx3 = env.makeTx(recipient, amount)
341+
tx4 = env.signTxWithNonce(tx3, AccountNonce(env.nonce-2))
342+
xp = env.xp
343+
344+
check xp.smartHead(blk.header)
345+
let res = xp.addLocal(tx4, force = true)
346+
check res.isOk
347+
if res.isErr:
348+
debugEcho res.error
349+
return
350+
351+
check inPoolAndOk(xp, rlpHash(tx4)) == false
352+
324353
proc runTxHeadDelta*(noisy = true) =
325354
## see github.com/status-im/nimbus-eth1/issues/1031
326355

@@ -399,6 +428,6 @@ when isMainModule:
399428
runTxPoolCliqueTest()
400429
runTxPoolPosTest()
401430
runTxPoolBlobhashTest()
402-
noisy.runTxHeadDelta
431+
#noisy.runTxHeadDelta
403432

404433
# End

0 commit comments

Comments
 (0)