Skip to content

Commit

Permalink
tests: add more dyn fee tx tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paologalligit committed Jan 24, 2025
1 parent 7c1d72a commit 27d9cd6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 43 deletions.
120 changes: 81 additions & 39 deletions api/transactions/transactions_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ import (
)

var (
cachedAccounts []genesis.DevAccount
once sync.Once
blockCount = 1_000
cachedAccounts []genesis.DevAccount
once sync.Once
blockCount = 1_000
createManyTxFuncs = []func(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain) (tx.Transactions, error){
createManyClausesPerTxLegacy,
createManyClausesPerTxDynFee,
}
)

func getCachedAccounts(b *testing.B) []genesis.DevAccount {
Expand All @@ -56,28 +60,29 @@ func BenchmarkFetchTx_RealDB_RandomSigners_ManyClausesPerTx(b *testing.B) {
// create state accounts
accounts := getCachedAccounts(b)

// randomly pick a signer for signing the transactions
randomSignerFunc := randomPickSignerFunc(accounts, createManyClausesPerTx)
for _, createManyClausesPerTx := range createManyTxFuncs { // randomly pick a signer for signing the transactions
randomSignerFunc := randomPickSignerFunc(accounts, createManyClausesPerTx)

// create test db - will be automagically removed when the benchmark ends
db, err := openTempMainDB(b.TempDir())
require.NoError(b, err)
// create test db - will be automagically removed when the benchmark ends
db, err := openTempMainDB(b.TempDir())
require.NoError(b, err)

// create blocks
newChain, transactions := createPackedChain(b, db, blockCount, accounts, randomSignerFunc)
// create blocks
newChain, transactions := createPackedChain(b, db, blockCount, accounts, randomSignerFunc)

// shuffle the transaction into a randomized order
randomizedTransactions := shuffleSlice(transactions)
b.Logf("About to process %d txs", len(randomizedTransactions))
// shuffle the transaction into a randomized order
randomizedTransactions := shuffleSlice(transactions)
b.Logf("About to process %d txs", len(randomizedTransactions))

// run the benchmarks
b.Run("getTransaction", func(b *testing.B) {
benchmarkGetTransaction(b, newChain, randomizedTransactions)
})
// run the benchmarks
b.Run("getTransaction", func(b *testing.B) {
benchmarkGetTransaction(b, newChain, randomizedTransactions)
})

b.Run("getReceipt", func(b *testing.B) {
benchmarkGetReceipt(b, newChain, randomizedTransactions)
})
b.Run("getReceipt", func(b *testing.B) {
benchmarkGetReceipt(b, newChain, randomizedTransactions)
})
}
}

func BenchmarkFetchTx_RealDB_RandomSigners_OneClausePerTx(b *testing.B) {
Expand Down Expand Up @@ -109,27 +114,27 @@ func BenchmarkFetchTx_RealDB_RandomSigners_OneClausePerTx(b *testing.B) {
}

func BenchmarkFetchTx_RandomSigners_ManyClausesPerTx(b *testing.B) {
// create state accounts
accounts := getCachedAccounts(b)
for _, createManyClausesPerTx := range createManyTxFuncs { // create state accounts
accounts := getCachedAccounts(b)
// randomly pick a signer for signing the transactions
randomSignerFunc := randomPickSignerFunc(accounts, createManyClausesPerTx)

// randomly pick a signer for signing the transactions
randomSignerFunc := randomPickSignerFunc(accounts, createManyClausesPerTx)
// create blocks
newChain, transactions := createPackedChain(b, muxdb.NewMem(), blockCount, accounts, randomSignerFunc)

// create blocks
newChain, transactions := createPackedChain(b, muxdb.NewMem(), blockCount, accounts, randomSignerFunc)
// shuffle the transaction into a randomized order
randomizedTransactions := shuffleSlice(transactions)
b.Logf("About to process %d txs", len(randomizedTransactions))

// shuffle the transaction into a randomized order
randomizedTransactions := shuffleSlice(transactions)
b.Logf("About to process %d txs", len(randomizedTransactions))

// run the benchmarks
b.Run("getTransaction", func(b *testing.B) {
benchmarkGetTransaction(b, newChain, randomizedTransactions)
})
// run the benchmarks
b.Run("getTransaction", func(b *testing.B) {
benchmarkGetTransaction(b, newChain, randomizedTransactions)
})

b.Run("getReceipt", func(b *testing.B) {
benchmarkGetReceipt(b, newChain, randomizedTransactions)
})
b.Run("getReceipt", func(b *testing.B) {
benchmarkGetReceipt(b, newChain, randomizedTransactions)
})
}
}

func BenchmarkFetchTx_RandomSigners_OneClausePerTx(b *testing.B) {
Expand Down Expand Up @@ -233,9 +238,15 @@ func createOneClausePerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain
for gasUsed < 9_500_000 {
toAddr := datagen.RandAddress()
cla := tx.NewClause(&toAddr).WithValue(big.NewInt(10000))
transaction := tx.NewTxBuilder(tx.LegacyTxType).
b := tx.NewTxBuilder(tx.LegacyTxType)
if gasUsed%2 == 0 {
b = tx.NewTxBuilder(tx.DynamicFeeTxType)
}
transaction := b.
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
MaxFeePerGas(big.NewInt(1000000)).
MaxPriorityFeePerGas(big.NewInt(100)).
Expiration(math.MaxUint32 - 1).
Gas(21_000).
Nonce(uint64(datagen.RandInt())).
Expand All @@ -255,7 +266,7 @@ func createOneClausePerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain
return transactions, nil
}

func createManyClausesPerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain) (tx.Transactions, error) {
func createManyClausesPerTxLegacy(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain) (tx.Transactions, error) {
var transactions tx.Transactions
gasUsed := uint64(0)
txGas := uint64(42_000)
Expand Down Expand Up @@ -285,6 +296,37 @@ func createManyClausesPerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Cha
return transactions, nil
}

func createManyClausesPerTxDynFee(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain) (tx.Transactions, error) {
var transactions tx.Transactions
gasUsed := uint64(0)
txGas := uint64(42_000)

transactionBuilder := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(1000000)).
MaxPriorityFeePerGas(big.NewInt(100)).
Expiration(math.MaxUint32 - 1).
Nonce(uint64(datagen.RandInt())).
BlockRef(tx.NewBlockRef(0))

for ; gasUsed < 9_500_000; gasUsed += txGas {
toAddr := datagen.RandAddress()
transactionBuilder.Clause(tx.NewClause(&toAddr).WithValue(big.NewInt(10000)))
}

transaction := transactionBuilder.Gas(gasUsed).MustBuild()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), signerPK)
if err != nil {
return nil, err
}
transaction = transaction.WithSignature(sig)

transactions = append(transactions, transaction)

return transactions, nil
}

func packTxsIntoBlock(thorChain *testchain.Chain, proposerAccount *genesis.DevAccount, parentBlk *block.Block, transactions tx.Transactions) (*block.Block, error) {
p := packer.New(thorChain.Repo(), thorChain.Stater(), proposerAccount.Address, &proposerAccount.Address, thorChain.GetForkConfig())

Expand Down
10 changes: 6 additions & 4 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,11 +763,13 @@ func TestExecuteTransactionFailure(t *testing.T) {
originEnergy.SetString("9000000000000000000000000000000000000", 10)
state.SetEnergy(origin.Address, originEnergy, 0)

tx := GetMockFailedTx(tx.LegacyTxType)
for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} {
tx := GetMockFailedTx(txType)

rt := runtime.New(repo.NewChain(b0.Header().ID()), state, &xenv.BlockContext{}, thor.NoFork)
rt := runtime.New(repo.NewChain(b0.Header().ID()), state, &xenv.BlockContext{}, thor.NoFork)

_, err = rt.ExecuteTransaction(tx)
_, err := rt.ExecuteTransaction(tx)

assert.NotNil(t, err)
assert.NotNil(t, err)
}
}

0 comments on commit 27d9cd6

Please sign in to comment.