fix txpool usage at the beginning of process

This commit is contained in:
jangko 2023-08-04 10:59:12 +07:00
parent 4d207e49ce
commit 70d718119e
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
6 changed files with 58 additions and 8 deletions

View File

@ -88,6 +88,11 @@ proc setupELClient*(t: TestEnv, chainFile: string, enableAuth: bool) =
t.com.initializeEmptyDb() t.com.initializeEmptyDb()
let txPool = TxPoolRef.new(t.com, t.conf.engineSigner) let txPool = TxPoolRef.new(t.com, t.conf.engineSigner)
# txPool must be informed of active head
# so it can know the latest account state
let head = t.com.db.getCanonicalHead()
doAssert txPool.smartHead(head)
var key: JwtSharedKey var key: JwtSharedKey
let kr = key.fromHex(jwtSecret) let kr = key.fromHex(jwtSecret)
if kr.isErr: if kr.isErr:

View File

@ -92,8 +92,11 @@ proc main() =
var stat: SimStat var stat: SimStat
let start = getTime() let start = getTime()
#let fileName = caseFolder & "/37_eth_sendRawTransaction_nonceTooLow.json" # txPool must be informed of active head
#block: # so it can know the latest account state
# e.g. "sendRawTransaction Nonce too low" case
let head = com.db.getCanonicalHead()
doAssert txPool.smartHead(head)
for fileName in walkDirRec( for fileName in walkDirRec(
caseFolder, yieldFilter = {pcFile,pcLinkToFile}): caseFolder, yieldFilter = {pcFile,pcLinkToFile}):

View File

@ -105,6 +105,12 @@ proc setupEnv*(): TestEnv =
let chainRef = newChain(com) let chainRef = newChain(com)
let txPool = TxPoolRef.new(com, conf.engineSigner) let txPool = TxPoolRef.new(com, conf.engineSigner)
# txPool must be informed of active head
# so it can know the latest account state
let head = com.db.getCanonicalHead()
doAssert txPool.smartHead(head)
let sealingEngine = SealingEngineRef.new( let sealingEngine = SealingEngineRef.new(
chainRef, ethCtx, conf.engineSigner, chainRef, ethCtx, conf.engineSigner,
txPool, EngineStopped txPool, EngineStopped

View File

@ -80,6 +80,12 @@ proc basicServices(nimbus: NimbusNode,
# the engineSigner is zero. # the engineSigner is zero.
nimbus.txPool = TxPoolRef.new(com, conf.engineSigner) nimbus.txPool = TxPoolRef.new(com, conf.engineSigner)
# txPool must be informed of active head
# so it can know the latest account state
# e.g. sender nonce, etc
let head = com.db.getCanonicalHead()
doAssert nimbus.txPool.smartHead(head)
# chainRef: some name to avoid module-name/filed/function misunderstandings # chainRef: some name to avoid module-name/filed/function misunderstandings
nimbus.chainRef = newChain(com) nimbus.chainRef = newChain(com)
if conf.verifyFrom.isSome: if conf.verifyFrom.isSome:

View File

@ -882,6 +882,7 @@ proc txPoolMain*(noisy = defined(debug)) =
noisy.runTxPackerTests noisy.runTxPackerTests
runTxPoolCliqueTest() runTxPoolCliqueTest()
runTxPoolPosTest() runTxPoolPosTest()
runTxPoolBlobhashTest()
noisy.runTxHeadDelta noisy.runTxHeadDelta
when isMainModule: when isMainModule:

View File

@ -7,6 +7,7 @@ import
../nimbus/core/clique/[clique_sealer, clique_desc], ../nimbus/core/clique/[clique_sealer, clique_desc],
../nimbus/[config, transaction, constants], ../nimbus/[config, transaction, constants],
../nimbus/core/tx_pool, ../nimbus/core/tx_pool,
../nimbus/core/tx_pool/tx_item,
../nimbus/core/casper, ../nimbus/core/casper,
../nimbus/core/executor, ../nimbus/core/executor,
../nimbus/common/common, ../nimbus/common/common,
@ -70,6 +71,11 @@ proc makeTx*(t: var TestEnv, recipient: EthAddress, amount: UInt256, payload: op
inc t.nonce inc t.nonce
signTransaction(tx, t.vaultKey, t.chainId, eip155 = true) signTransaction(tx, t.vaultKey, t.chainId, eip155 = true)
proc signTxWithNonce(t: TestEnv, tx: Transaction, nonce: AccountNonce): Transaction =
var tx = tx
tx.nonce = nonce
signTransaction(tx, t.vaultKey, t.chainId, eip155 = true)
proc initEnv(envFork: HardFork): TestEnv = proc initEnv(envFork: HardFork): TestEnv =
var var
conf = makeConfig(@[ conf = makeConfig(@[
@ -265,12 +271,18 @@ proc runTxPoolPosTest*() =
let bal = sdb.getBalance(feeRecipient) let bal = sdb.getBalance(feeRecipient)
check not bal.isZero check not bal.isZero
proc inPoolAndOk(txPool: TxPoolRef, txHash: Hash256): bool =
let res = txPool.getItem(txHash)
if res.isErr: return false
res.get().reject == txInfoOk
proc runTxPoolBlobhashTest*() = proc runTxPoolBlobhashTest*() =
var var
env = initEnv(Cancun) env = initEnv(Cancun)
var var
tx = env.makeTx(recipient, amount) tx1 = env.makeTx(recipient, amount)
tx2 = env.makeTx(recipient, amount)
xp = env.xp xp = env.xp
com = env.com com = env.com
chain = env.chain chain = env.chain
@ -279,14 +291,16 @@ proc runTxPoolBlobhashTest*() =
suite "Test TxPool with blobhash block": suite "Test TxPool with blobhash block":
test "TxPool addLocal": test "TxPool addLocal":
let res = xp.addLocal(tx, force = true) let res = xp.addLocal(tx1, force = true)
check res.isOk check res.isOk
if res.isErr: if res.isErr:
debugEcho res.error debugEcho res.error
return return
let res2 = xp.addLocal(tx2, force = true)
check res2.isOk
test "TxPool jobCommit": test "TxPool jobCommit":
check xp.nItems.total == 1 check xp.nItems.total == 2
test "TxPool ethBlock": test "TxPool ethBlock":
com.pos.prevRandao = prevRandao com.pos.prevRandao = prevRandao
@ -302,7 +316,7 @@ proc runTxPoolBlobhashTest*() =
uncles: blk.uncles, uncles: blk.uncles,
withdrawals: some[seq[Withdrawal]](@[]) withdrawals: some[seq[Withdrawal]](@[])
) )
check blk.txs.len == 1 check blk.txs.len == 2
test "Blobhash persistBlocks": test "Blobhash persistBlocks":
let rr = chain.persistBlocks([blk.header], [body]) let rr = chain.persistBlocks([blk.header], [body])
@ -321,6 +335,21 @@ proc runTxPoolBlobhashTest*() =
let bal = sdb.getBalance(feeRecipient) let bal = sdb.getBalance(feeRecipient)
check not bal.isZero check not bal.isZero
test "add tx with nonce too low":
let
tx3 = env.makeTx(recipient, amount)
tx4 = env.signTxWithNonce(tx3, AccountNonce(env.nonce-2))
xp = env.xp
check xp.smartHead(blk.header)
let res = xp.addLocal(tx4, force = true)
check res.isOk
if res.isErr:
debugEcho res.error
return
check inPoolAndOk(xp, rlpHash(tx4)) == false
proc runTxHeadDelta*(noisy = true) = proc runTxHeadDelta*(noisy = true) =
## see github.com/status-im/nimbus-eth1/issues/1031 ## see github.com/status-im/nimbus-eth1/issues/1031
@ -399,6 +428,6 @@ when isMainModule:
runTxPoolCliqueTest() runTxPoolCliqueTest()
runTxPoolPosTest() runTxPoolPosTest()
runTxPoolBlobhashTest() runTxPoolBlobhashTest()
noisy.runTxHeadDelta #noisy.runTxHeadDelta
# End # End