Skip to content

Commit

Permalink
Merge test_txpool and test_txpool2
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jan 5, 2025
1 parent 7672f0b commit 0dc31ab
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 369 deletions.
1 change: 1 addition & 0 deletions nimbus/core/chain/forked_chain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ proc isCanonicalAncestor*(c: ForkedChainRef,
canonHash == blockHash

iterator txHashInRange*(c: ForkedChainRef, fromHash: Hash32, toHash: Hash32): Hash32 =
## toHash should be ancestor of fromHash
## exclude base from iteration, new block produced by txpool
## should not reach base
var prevHash = fromHash
Expand Down
1 change: 0 additions & 1 deletion tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ cliBuilder:
#./test_graphql, -- fails
./test_configuration,
./test_txpool,
./test_txpool2,
./test_engine_api,
./test_getproof_json,
./test_aristo,
Expand Down
187 changes: 186 additions & 1 deletion tests/test_txpool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# according to those terms.

import
std/math,
eth/common/keys,
results,
unittest2,
Expand All @@ -22,13 +23,22 @@ import
../nimbus/core/tx_pool/tx_desc,
../nimbus/core/casper,
../nimbus/common/common,
../nimbus/utils/utils
../nimbus/utils/utils,
./macro_assembler

const
genesisFile = "tests/customgenesis/merge.json"
feeRecipient = address"0000000000000000000000000000000000000212"
recipient = address"0000000000000000000000000000000000000213"
recipient214 = address"0000000000000000000000000000000000000214"
prevRandao = Bytes32 EMPTY_UNCLE_HASH
contractCode = evmByteCode:
PrevRandao # VAL
Push1 "0x11" # KEY
Sstore # OP
Stop
slot = 0x11.u256
amount = 1000.u256

type
TestEnv = object
Expand Down Expand Up @@ -59,6 +69,8 @@ proc initEnv(envFork: HardFork): TestEnv =
if envFork >= Prague:
conf.networkParams.config.pragueTime = Opt.some(0.EthTime)

conf.networkParams.genesis.alloc[recipient] = GenesisAccount(code: contractCode)

let
# create the sender first, because it will modify networkParams
sender = TxSender.new(conf.networkParams, 30)
Expand Down Expand Up @@ -129,6 +141,41 @@ template checkImportBlock(xp: TxPoolRef, bundle: AssembledBlock, expRem: int) =
xp.removeNewBlockTxs(bundle.blk)
check xp.len == expRem

proc createPooledTransactionWithBlob(
mx: TxSender,
acc: TestAccount,
recipient: Address,
amount: UInt256,
nonce: AccountNonce
): PooledTransaction =
# Create the transaction
let
tc = BlobTx(
recipient: Opt.some(recipient),
gasLimit: 100000.GasInt,
gasTip: GasInt(10 ^ 9),
gasFee: GasInt(10 ^ 9),
blobGasFee: u256(1),
blobCount: 1,
blobID: 1,
)

mx.makeTx(tc, acc, nonce)

proc makeTx(
mx: TxSender,
acc: TestAccount,
recipient: Address,
amount: UInt256,
nonce: AccountNonce): PooledTransaction =
let
tc = BaseTx(
gasLimit: 75000,
recipient: Opt.some(recipient),
amount: amount,
)
mx.makeTx(tc, acc, nonce)

proc txPoolMain*() =
suite "TxPool test suite":
loadKzgTrustedSetup().expect("KZG trusted setup loaded")
Expand Down Expand Up @@ -385,6 +432,144 @@ proc txPoolMain*() =
gasPriceOrGasFeeCap: Opt.some(oldPrice*2)
))
xp.checkAddTx(ptx1, txErrorReplacementBlobGasTooLow)
xp.checkImportBlock(2, 0)

test "Test TxPool with PoS block":
let
acc = mx.getAccount(20)
tc = BaseTx(
txType: Opt.some(TxLegacy),
recipient: Opt.some(recipient),
gasLimit: 75000,
amount: amount,
)
ptx = mx.makeTx(tc, acc, 0)

xp.checkAddTx(ptx)
let bundle = xp.checkAssembleBlock(1)
xp.checkImportBlock(bundle, 0)

let
sdb = LedgerRef.init(com.db)
val = sdb.getStorage(recipient, slot)
randao = Bytes32(val.toBytesBE)
fee = sdb.getBalance(feeRecipient)
bal = sdb.getBalance(recipient)

check randao == prevRandao
check bundle.blk.header.coinbase == feeRecipient
check not fee.isZero
check bal >= 1000.u256

test "Test TxPool with blobhash block":
let
acc = mx.getAccount(21)
tx1 = mx.createPooledTransactionWithBlob(acc, recipient, amount, 0)
tx2 = mx.createPooledTransactionWithBlob(acc, recipient, amount, 1)

xp.checkAddTx(tx1)
xp.checkAddTx(tx2)

template header(): Header =
bundle.blk.header

let
bundle = xp.checkAssembleBlock(2)
gasUsed1 = xp.vmState.receipts[0].cumulativeGasUsed
gasUsed2 = xp.vmState.receipts[1].cumulativeGasUsed - gasUsed1
totalBlobGasUsed = tx1.tx.getTotalBlobGas + tx2.tx.getTotalBlobGas
blockValue =
gasUsed1.u256 * tx1.tx.effectiveGasTip(header.baseFeePerGas).u256 +
gasUsed2.u256 * tx2.tx.effectiveGasTip(header.baseFeePerGas).u256

check blockValue == bundle.blockValue
check totalBlobGasUsed == header.blobGasUsed.get()

xp.checkImportBlock(bundle, 0)

let
sdb = LedgerRef.init(com.db)
val = sdb.getStorage(recipient, slot)
randao = Bytes32(val.toBytesBE)
bal = sdb.getBalance(feeRecipient)

check randao == prevRandao
check header.coinbase == feeRecipient
check not bal.isZero

## see github.com/status-im/nimbus-eth1/issues/1031
test "TxPool: Synthesising blocks (covers issue #1031)":
const
txPerblock = 20
numBlocks = 10

let
lastNumber = chain.latestNumber
tc = BaseTx(
gasLimit: 75000,
recipient: Opt.some(recipient214),
amount: amount,
)

for n in 0 ..< numBlocks:
for tn in 0 ..< txPerblock:
let tx = mx.makeNextTx(tc)
xp.checkAddTx(tx)

xp.checkImportBlock(txPerblock, 0)

check com.syncCurrent == lastNumber + numBlocks
let
head = chain.headerByNumber(com.syncCurrent).expect("block header exists")
sdb = LedgerRef.init(com.db)
expected = u256(txPerblock * numBlocks) * amount
balance = sdb.getBalance(recipient214)
check balance == expected
discard head

test "Test get parent transactions after persistBlock":
let
acc = mx.getAccount(22)
tx1 = mx.makeTx(acc, recipient, 1.u256, 0)
tx2 = mx.makeTx(acc, recipient, 2.u256, 1)

xp.checkAddTx(tx1)
xp.checkAddTx(tx2)

xp.checkImportBlock(2, 0)

let
tx3 = mx.makeTx(acc, recipient, 3.u256, 2)
tx4 = mx.makeTx(acc, recipient, 4.u256, 3)
tx5 = mx.makeTx(acc, recipient, 5.u256, 4)

xp.checkAddTx(tx3)
xp.checkAddTx(tx4)
xp.checkAddTx(tx5)

xp.checkImportBlock(3, 0)
let latestHash = chain.latestHash
check env.chain.forkChoice(latestHash, latestHash).isOk

let hs = [
rlpHash(tx1),
rlpHash(tx2),
rlpHash(tx3),
rlpHash(tx4),
rlpHash(tx5),
]

let res = chain.blockByNumber(chain.latestHeader.number - 1)
if res.isErr:
debugEcho res.error
check false

let parent = res.get
var count = 0
for txh in chain.txHashInRange(latestHash, parent.header.parentHash):
check txh in hs
inc count
check count == hs.len

when isMainModule:
txPoolMain()
Loading

0 comments on commit 0dc31ab

Please sign in to comment.