mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-10 11:06:49 +00:00
Tidy up test_txpool2 (#2968)
This commit is contained in:
parent
aba9b582db
commit
557a9605cf
@ -122,12 +122,15 @@ proc initEnv(envFork: HardFork): TestEnv =
|
|||||||
if envFork >= Cancun:
|
if envFork >= Cancun:
|
||||||
conf.networkParams.config.cancunTime = Opt.some(0.EthTime)
|
conf.networkParams.config.cancunTime = Opt.some(0.EthTime)
|
||||||
|
|
||||||
|
if envFork >= Prague:
|
||||||
|
conf.networkParams.config.pragueTime = Opt.some(0.EthTime)
|
||||||
|
|
||||||
let
|
let
|
||||||
com =
|
com =
|
||||||
CommonRef.new(newCoreDbRef DefaultDbMemory, nil, conf.networkId, conf.networkParams)
|
CommonRef.new(newCoreDbRef DefaultDbMemory, nil, conf.networkId, conf.networkParams)
|
||||||
chain = newForkedChain(com, com.genesisHeader)
|
chain = newForkedChain(com, com.genesisHeader)
|
||||||
|
|
||||||
result = TestEnv(
|
TestEnv(
|
||||||
conf: conf,
|
conf: conf,
|
||||||
com: com,
|
com: com,
|
||||||
chain: chain,
|
chain: chain,
|
||||||
@ -142,91 +145,73 @@ const
|
|||||||
slot = 0x11.u256
|
slot = 0x11.u256
|
||||||
prevRandao = Bytes32 EMPTY_UNCLE_HASH # it can be any valid hash
|
prevRandao = Bytes32 EMPTY_UNCLE_HASH # it can be any valid hash
|
||||||
|
|
||||||
proc runTxPoolPosTest() =
|
template runTxPoolPosTest() =
|
||||||
var env = initEnv(MergeFork)
|
test "Test TxPool with PoS block":
|
||||||
|
|
||||||
var
|
var
|
||||||
|
env = initEnv(MergeFork)
|
||||||
tx = env.makeTx(recipient, amount)
|
tx = env.makeTx(recipient, amount)
|
||||||
xp = env.xp
|
xp = env.xp
|
||||||
com = env.com
|
com = env.com
|
||||||
chain = env.chain
|
chain = env.chain
|
||||||
body: BlockBody
|
|
||||||
blk: EthBlock
|
|
||||||
|
|
||||||
suite "Test TxPool with PoS block":
|
|
||||||
test "TxPool add":
|
|
||||||
xp.add(PooledTransaction(tx: tx))
|
xp.add(PooledTransaction(tx: tx))
|
||||||
|
|
||||||
test "TxPool jobCommit":
|
|
||||||
check xp.nItems.total == 1
|
check xp.nItems.total == 1
|
||||||
|
|
||||||
test "TxPool ethBlock":
|
# generate block
|
||||||
com.pos.prevRandao = prevRandao
|
com.pos.prevRandao = prevRandao
|
||||||
com.pos.feeRecipient = feeRecipient
|
com.pos.feeRecipient = feeRecipient
|
||||||
com.pos.timestamp = EthTime.now()
|
com.pos.timestamp = EthTime.now()
|
||||||
|
|
||||||
let r = xp.assembleBlock()
|
let bundle = xp.assembleBlock().valueOr:
|
||||||
if r.isErr:
|
debugEcho error
|
||||||
debugEcho r.error
|
|
||||||
check false
|
check false
|
||||||
return
|
return
|
||||||
|
|
||||||
blk = r.get.blk
|
let blk = bundle.blk
|
||||||
body = BlockBody(transactions: blk.txs, uncles: blk.uncles)
|
check blk.transactions.len == 1
|
||||||
check blk.txs.len == 1
|
|
||||||
|
|
||||||
test "PoS persistBlocks":
|
# import block
|
||||||
let rr = chain.importBlock(EthBlock.init(blk.header, body))
|
chain.importBlock(blk).isOkOr:
|
||||||
check rr.isOk()
|
debugEcho error
|
||||||
|
check false
|
||||||
|
return
|
||||||
|
|
||||||
|
let
|
||||||
|
sdb = LedgerRef.init(com.db)
|
||||||
|
val = sdb.getStorage(recipient, slot)
|
||||||
|
randao = Bytes32(val.toBytesBE)
|
||||||
|
bal = sdb.getBalance(feeRecipient)
|
||||||
|
|
||||||
test "validate TxPool prevRandao setter":
|
|
||||||
var sdb = LedgerRef.init(com.db)
|
|
||||||
let val = sdb.getStorage(recipient, slot)
|
|
||||||
let randao = Bytes32(val.toBytesBE)
|
|
||||||
check randao == prevRandao
|
check randao == prevRandao
|
||||||
|
|
||||||
test "feeRecipient rewarded":
|
|
||||||
check blk.header.coinbase == feeRecipient
|
check blk.header.coinbase == feeRecipient
|
||||||
var sdb = LedgerRef.init(com.db)
|
|
||||||
let bal = sdb.getBalance(feeRecipient)
|
|
||||||
check not bal.isZero
|
check not bal.isZero
|
||||||
|
|
||||||
proc runTxPoolBlobhashTest() =
|
template runTxPoolBlobhashTest() =
|
||||||
var env = initEnv(Cancun)
|
test "Test TxPool with blobhash block":
|
||||||
|
|
||||||
var
|
var
|
||||||
|
env = initEnv(Cancun)
|
||||||
tx1 = env.createPooledTransactionWithBlob(recipient, amount)
|
tx1 = env.createPooledTransactionWithBlob(recipient, amount)
|
||||||
tx2 = env.createPooledTransactionWithBlob(recipient, amount)
|
tx2 = env.createPooledTransactionWithBlob(recipient, amount)
|
||||||
xp = env.xp
|
xp = env.xp
|
||||||
com = env.com
|
com = env.com
|
||||||
chain = env.chain
|
chain = env.chain
|
||||||
body: BlockBody
|
|
||||||
blk: EthBlock
|
|
||||||
|
|
||||||
suite "Test TxPool with blobhash block":
|
|
||||||
test "TxPool jobCommit":
|
|
||||||
xp.add(tx1)
|
xp.add(tx1)
|
||||||
xp.add(tx2)
|
xp.add(tx2)
|
||||||
check xp.nItems.total == 2
|
check xp.nItems.total == 2
|
||||||
|
|
||||||
test "TxPool ethBlock":
|
# generate block
|
||||||
com.pos.prevRandao = prevRandao
|
com.pos.prevRandao = prevRandao
|
||||||
com.pos.feeRecipient = feeRecipient
|
com.pos.feeRecipient = feeRecipient
|
||||||
com.pos.timestamp = EthTime.now()
|
com.pos.timestamp = EthTime.now()
|
||||||
|
|
||||||
let r = xp.assembleBlock()
|
let bundle = xp.assembleBlock().valueOr:
|
||||||
if r.isErr:
|
debugEcho error
|
||||||
debugEcho r.error
|
|
||||||
check false
|
check false
|
||||||
return
|
return
|
||||||
|
|
||||||
let bundle = r.get
|
let
|
||||||
blk = bundle.blk
|
blk = bundle.blk
|
||||||
body = BlockBody(
|
|
||||||
transactions: blk.txs,
|
|
||||||
uncles: blk.uncles,
|
|
||||||
withdrawals: Opt.some(newSeq[Withdrawal]()),
|
|
||||||
)
|
|
||||||
check blk.txs.len == 2
|
check blk.txs.len == 2
|
||||||
|
|
||||||
let
|
let
|
||||||
@ -240,38 +225,32 @@ proc runTxPoolBlobhashTest() =
|
|||||||
check blockValue == bundle.blockValue
|
check blockValue == bundle.blockValue
|
||||||
check totalBlobGasUsed == blk.header.blobGasUsed.get()
|
check totalBlobGasUsed == blk.header.blobGasUsed.get()
|
||||||
|
|
||||||
test "Blobhash persistBlocks":
|
chain.importBlock(blk).isOkOr:
|
||||||
let rr = chain.importBlock(EthBlock.init(blk.header, body))
|
debugEcho error
|
||||||
check rr.isOk()
|
check false
|
||||||
|
return
|
||||||
|
|
||||||
|
let
|
||||||
|
sdb = LedgerRef.init(com.db)
|
||||||
|
val = sdb.getStorage(recipient, slot)
|
||||||
|
randao = Bytes32(val.toBytesBE)
|
||||||
|
bal = sdb.getBalance(feeRecipient)
|
||||||
|
|
||||||
test "validate TxPool prevRandao setter":
|
|
||||||
var sdb = LedgerRef.init(com.db)
|
|
||||||
let val = sdb.getStorage(recipient, slot)
|
|
||||||
let randao = Bytes32(val.toBytesBE)
|
|
||||||
check randao == prevRandao
|
check randao == prevRandao
|
||||||
|
|
||||||
test "feeRecipient rewarded":
|
|
||||||
check blk.header.coinbase == feeRecipient
|
check blk.header.coinbase == feeRecipient
|
||||||
var sdb = LedgerRef.init(com.db)
|
|
||||||
let bal = sdb.getBalance(feeRecipient)
|
|
||||||
check not bal.isZero
|
check not bal.isZero
|
||||||
|
|
||||||
test "add tx with nonce too low":
|
|
||||||
let
|
let
|
||||||
tx3 = env.makeTx(recipient, amount)
|
tx3 = env.makeTx(recipient, amount)
|
||||||
tx4 = env.signTxWithNonce(tx3, AccountNonce(env.nonce - 2))
|
tx4 = env.signTxWithNonce(tx3, AccountNonce(env.nonce - 2))
|
||||||
xp = env.xp
|
|
||||||
|
|
||||||
check xp.smartHead(blk.header)
|
check xp.smartHead(blk.header)
|
||||||
xp.add(PooledTransaction(tx: tx4))
|
xp.add(PooledTransaction(tx: tx4))
|
||||||
|
|
||||||
check inPoolAndOk(xp, rlpHash(tx4)) == false
|
check inPoolAndOk(xp, rlpHash(tx4)) == false
|
||||||
|
|
||||||
proc runTxHeadDelta(noisy = true) =
|
template runTxHeadDelta() =
|
||||||
## see github.com/status-im/nimbus-eth1/issues/1031
|
## see github.com/status-im/nimbus-eth1/issues/1031
|
||||||
|
test "TxPool: Synthesising blocks (covers issue #1031)":
|
||||||
suite "TxPool: Synthesising blocks (covers issue #1031)":
|
|
||||||
test "Packing and adding multiple blocks to chain":
|
|
||||||
var
|
var
|
||||||
env = initEnv(MergeFork)
|
env = initEnv(MergeFork)
|
||||||
xp = env.xp
|
xp = env.xp
|
||||||
@ -284,36 +263,27 @@ proc runTxHeadDelta(noisy = true) =
|
|||||||
txPerblock = 20
|
txPerblock = 20
|
||||||
numBlocks = 10
|
numBlocks = 10
|
||||||
|
|
||||||
# setTraceLevel()
|
|
||||||
|
|
||||||
block:
|
|
||||||
for n in 0 ..< numBlocks:
|
for n in 0 ..< numBlocks:
|
||||||
for tn in 0 ..< txPerblock:
|
for tn in 0 ..< txPerblock:
|
||||||
let tx = env.makeTx(recipient, amount)
|
let tx = env.makeTx(recipient, amount)
|
||||||
xp.add(PooledTransaction(tx: tx))
|
xp.add(PooledTransaction(tx: tx))
|
||||||
|
|
||||||
noisy.say "***",
|
|
||||||
"txDB",
|
|
||||||
&" n={n}",
|
|
||||||
# pending/staged/packed : total/disposed
|
|
||||||
&" stats={xp.nItems.pp}"
|
|
||||||
|
|
||||||
timestamp = timestamp + 1
|
timestamp = timestamp + 1
|
||||||
com.pos.prevRandao = prevRandao
|
com.pos.prevRandao = prevRandao
|
||||||
com.pos.timestamp = timestamp
|
com.pos.timestamp = timestamp
|
||||||
com.pos.feeRecipient = feeRecipient
|
com.pos.feeRecipient = feeRecipient
|
||||||
|
|
||||||
let r = xp.assembleBlock()
|
let bundle = xp.assembleBlock().valueOr:
|
||||||
if r.isErr:
|
debugEcho error
|
||||||
debugEcho r.error
|
|
||||||
check false
|
check false
|
||||||
return
|
return
|
||||||
|
|
||||||
let blk = r.get.blk
|
let blk = bundle.blk
|
||||||
let body = BlockBody(transactions: blk.txs, uncles: blk.uncles)
|
|
||||||
|
|
||||||
# Commit to block chain
|
# Commit to block chain
|
||||||
check chain.importBlock(EthBlock.init(blk.header, body)).isOk
|
chain.importBlock(blk).isOkOr:
|
||||||
|
debugEcho error
|
||||||
|
check false
|
||||||
|
return
|
||||||
|
|
||||||
# Synchronise TxPool against new chain head, register txs differences.
|
# Synchronise TxPool against new chain head, register txs differences.
|
||||||
# In this particular case, these differences will simply flush the
|
# In this particular case, these differences will simply flush the
|
||||||
@ -324,8 +294,6 @@ proc runTxHeadDelta(noisy = true) =
|
|||||||
check xp.nItems.staged == 0
|
check xp.nItems.staged == 0
|
||||||
check xp.nItems.packed == 0
|
check xp.nItems.packed == 0
|
||||||
|
|
||||||
setErrorLevel() # in case we set trace level
|
|
||||||
|
|
||||||
check com.syncCurrent == 10.BlockNumber
|
check com.syncCurrent == 10.BlockNumber
|
||||||
head = chain.headerByNumber(com.syncCurrent).expect("block header exists")
|
head = chain.headerByNumber(com.syncCurrent).expect("block header exists")
|
||||||
let
|
let
|
||||||
@ -334,15 +302,14 @@ proc runTxHeadDelta(noisy = true) =
|
|||||||
balance = sdb.getBalance(recipient)
|
balance = sdb.getBalance(recipient)
|
||||||
check balance == expected
|
check balance == expected
|
||||||
|
|
||||||
proc runGetBlockBodyTest() =
|
template runGetBlockBodyTest() =
|
||||||
|
test "Test get parent transactions after persistBlock":
|
||||||
var
|
var
|
||||||
env = initEnv(Cancun)
|
env = initEnv(Cancun)
|
||||||
blockTime = EthTime.now()
|
blockTime = EthTime.now()
|
||||||
parentHeader: Header
|
parentHeader: Header
|
||||||
currentHeader: Header
|
currentHeader: Header
|
||||||
|
|
||||||
suite "Test get parent transactions after persistBlock":
|
|
||||||
test "TxPool create first block":
|
|
||||||
let
|
let
|
||||||
tx1 = env.makeTx(recipient, 1.u256)
|
tx1 = env.makeTx(recipient, 1.u256)
|
||||||
tx2 = env.makeTx(recipient, 2.u256)
|
tx2 = env.makeTx(recipient, 2.u256)
|
||||||
@ -354,54 +321,49 @@ proc runGetBlockBodyTest() =
|
|||||||
env.com.pos.feeRecipient = feeRecipient
|
env.com.pos.feeRecipient = feeRecipient
|
||||||
env.com.pos.timestamp = blockTime
|
env.com.pos.timestamp = blockTime
|
||||||
|
|
||||||
let r = env.xp.assembleBlock()
|
let bundle = env.xp.assembleBlock().valueOr:
|
||||||
if r.isErr:
|
debugEcho error
|
||||||
check false
|
check false
|
||||||
return
|
return
|
||||||
|
|
||||||
let blk = r.get.blk
|
let blk = bundle.blk
|
||||||
check env.chain.importBlock(blk).isOk
|
check env.chain.importBlock(blk).isOk
|
||||||
parentHeader = blk.header
|
parentHeader = blk.header
|
||||||
check env.xp.smartHead(parentHeader)
|
check env.xp.smartHead(parentHeader)
|
||||||
check blk.transactions.len == 2
|
check blk.transactions.len == 2
|
||||||
|
|
||||||
test "TxPool create second block":
|
|
||||||
let
|
let
|
||||||
tx1 = env.makeTx(recipient, 3.u256)
|
tx3 = env.makeTx(recipient, 3.u256)
|
||||||
tx2 = env.makeTx(recipient, 4.u256)
|
tx4 = env.makeTx(recipient, 4.u256)
|
||||||
tx3 = env.makeTx(recipient, 5.u256)
|
tx5 = env.makeTx(recipient, 5.u256)
|
||||||
|
|
||||||
env.xp.add(PooledTransaction(tx: tx1))
|
|
||||||
env.xp.add(PooledTransaction(tx: tx2))
|
|
||||||
env.xp.add(PooledTransaction(tx: tx3))
|
env.xp.add(PooledTransaction(tx: tx3))
|
||||||
|
env.xp.add(PooledTransaction(tx: tx4))
|
||||||
|
env.xp.add(PooledTransaction(tx: tx5))
|
||||||
|
|
||||||
env.com.pos.prevRandao = prevRandao
|
env.com.pos.prevRandao = prevRandao
|
||||||
env.com.pos.feeRecipient = feeRecipient
|
env.com.pos.feeRecipient = feeRecipient
|
||||||
env.com.pos.timestamp = blockTime + 1
|
env.com.pos.timestamp = blockTime + 1
|
||||||
|
|
||||||
let r = env.xp.assembleBlock()
|
let bundle2 = env.xp.assembleBlock().valueOr:
|
||||||
if r.isErr:
|
debugEcho error
|
||||||
check false
|
check false
|
||||||
return
|
return
|
||||||
|
|
||||||
let blk = r.get.blk
|
let blk2 = bundle2.blk
|
||||||
check env.chain.importBlock(blk).isOk
|
check env.chain.importBlock(blk2).isOk
|
||||||
currentHeader = blk.header
|
currentHeader = blk2.header
|
||||||
check env.xp.smartHead(currentHeader)
|
check env.xp.smartHead(currentHeader)
|
||||||
check blk.transactions.len == 3
|
check blk2.transactions.len == 3
|
||||||
let currHash = currentHeader.blockHash
|
let currHash = currentHeader.blockHash
|
||||||
check env.chain.forkChoice(currHash, currHash).isOk
|
check env.chain.forkChoice(currHash, currHash).isOk
|
||||||
|
|
||||||
proc txPool2Main*() =
|
proc txPool2Main*() =
|
||||||
const noisy = defined(debug)
|
suite "TxPool test suite":
|
||||||
|
loadKzgTrustedSetup().expect("KZG trusted setup loaded")
|
||||||
loadKzgTrustedSetup().expect("Failed to load KZG trusted setup")
|
|
||||||
|
|
||||||
setErrorLevel() # mute logger
|
|
||||||
|
|
||||||
runTxPoolPosTest()
|
runTxPoolPosTest()
|
||||||
runTxPoolBlobhashTest()
|
runTxPoolBlobhashTest()
|
||||||
noisy.runTxHeadDelta
|
runTxHeadDelta()
|
||||||
runGetBlockBodyTest()
|
runGetBlockBodyTest()
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user