Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix txpool usage at the beginning of a process #1672

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hive_integration/nodocker/engine/test_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ proc setupELClient*(t: TestEnv, chainFile: string, enableAuth: bool) =
t.com.initializeEmptyDb()
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
let kr = key.fromHex(jwtSecret)
if kr.isErr:
Expand Down
7 changes: 5 additions & 2 deletions hive_integration/nodocker/graphql/graphql_sim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ proc main() =
var stat: SimStat
let start = getTime()

#let fileName = caseFolder & "/37_eth_sendRawTransaction_nonceTooLow.json"
#block:
# txPool must be informed of active head
# 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(
caseFolder, yieldFilter = {pcFile,pcLinkToFile}):
Expand Down
6 changes: 6 additions & 0 deletions hive_integration/nodocker/rpc/test_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ proc setupEnv*(): TestEnv =

let chainRef = newChain(com)
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(
chainRef, ethCtx, conf.engineSigner,
txPool, EngineStopped
Expand Down
6 changes: 6 additions & 0 deletions nimbus/nimbus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ proc basicServices(nimbus: NimbusNode,
# the engineSigner is zero.
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
nimbus.chainRef = newChain(com)
if conf.verifyFrom.isSome:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_txpool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ proc runTxPackerTests(noisy = true) =
# even though the difficulty or the blocknumber is lower than
# previous canonical head
check hdr.blockHash == xq.chain.com.db.getCanonicalHead.blockHash

# Is the withdrawals persisted and loaded properly?
var blockBody: BlockBody
check xq.chain.com.db.getBlockBody(hdr, blockBody)
Expand Down Expand Up @@ -882,6 +882,7 @@ proc txPoolMain*(noisy = defined(debug)) =
noisy.runTxPackerTests
runTxPoolCliqueTest()
runTxPoolPosTest()
runTxPoolBlobhashTest()
noisy.runTxHeadDelta

when isMainModule:
Expand Down
39 changes: 34 additions & 5 deletions tests/test_txpool2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import
../nimbus/core/clique/[clique_sealer, clique_desc],
../nimbus/[config, transaction, constants],
../nimbus/core/tx_pool,
../nimbus/core/tx_pool/tx_item,
../nimbus/core/casper,
../nimbus/core/executor,
../nimbus/common/common,
Expand Down Expand Up @@ -70,6 +71,11 @@ proc makeTx*(t: var TestEnv, recipient: EthAddress, amount: UInt256, payload: op
inc t.nonce
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 =
var
conf = makeConfig(@[
Expand Down Expand Up @@ -265,12 +271,18 @@ proc runTxPoolPosTest*() =
let bal = sdb.getBalance(feeRecipient)
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*() =
var
env = initEnv(Cancun)

var
tx = env.makeTx(recipient, amount)
tx1 = env.makeTx(recipient, amount)
tx2 = env.makeTx(recipient, amount)
xp = env.xp
com = env.com
chain = env.chain
Expand All @@ -279,14 +291,16 @@ proc runTxPoolBlobhashTest*() =

suite "Test TxPool with blobhash block":
test "TxPool addLocal":
let res = xp.addLocal(tx, force = true)
let res = xp.addLocal(tx1, force = true)
check res.isOk
if res.isErr:
debugEcho res.error
return
let res2 = xp.addLocal(tx2, force = true)
check res2.isOk

test "TxPool jobCommit":
check xp.nItems.total == 1
check xp.nItems.total == 2

test "TxPool ethBlock":
com.pos.prevRandao = prevRandao
Expand All @@ -302,7 +316,7 @@ proc runTxPoolBlobhashTest*() =
uncles: blk.uncles,
withdrawals: some[seq[Withdrawal]](@[])
)
check blk.txs.len == 1
check blk.txs.len == 2

test "Blobhash persistBlocks":
let rr = chain.persistBlocks([blk.header], [body])
Expand All @@ -321,6 +335,21 @@ proc runTxPoolBlobhashTest*() =
let bal = sdb.getBalance(feeRecipient)
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) =
## see github.com/status-im/nimbus-eth1/issues/1031

Expand Down Expand Up @@ -399,6 +428,6 @@ when isMainModule:
runTxPoolCliqueTest()
runTxPoolPosTest()
runTxPoolBlobhashTest()
noisy.runTxHeadDelta
#noisy.runTxHeadDelta

# End
Loading