Skip to content

Commit

Permalink
changes requested in review
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Dec 3, 2024
1 parent 38ed44c commit 5ac90f4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 54 deletions.
24 changes: 16 additions & 8 deletions tests/gas_price_suggestion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ func TestGasPrice_UnderpricedTransactionsAreRejected(t *testing.T) {
for _, extra := range []int{-10, 0, baseFee / 100, 4 * baseFee / 100} {
feeCap := int64(baseFee + extra)

err = send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap))
err = send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap, 0))
require.ErrorContains(err, "transaction underpriced")

err = send(factory.makeAccessListTransactionWithPrice(t, nonce, feeCap))
err = send(factory.makeAccessListTransactionWithPrice(t, nonce, feeCap, 0))
require.ErrorContains(err, "transaction underpriced")

err = send(factory.makeDynamicFeeTransactionWithPrice(t, nonce, feeCap))
err = send(factory.makeDynamicFeeTransactionWithPrice(t, nonce, feeCap, 0))
require.ErrorContains(err, "transaction underpriced")

err = send(factory.makeBlobTransactionWithPrice(t, nonce, feeCap))
err = send(factory.makeBlobTransactionWithPrice(t, nonce, feeCap, 0))
require.ErrorContains(err, "transaction underpriced")
}

// Everything over ~5% above the base fee should be accepted.
feeCap := int64(baseFee + 7*baseFee/100)
require.NoError(send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap)))
require.NoError(send(factory.makeAccessListTransactionWithPrice(t, nonce+1, feeCap)))
require.NoError(send(factory.makeDynamicFeeTransactionWithPrice(t, nonce+2, feeCap)))
require.NoError(send(factory.makeBlobTransactionWithPrice(t, nonce+3, feeCap)))
require.NoError(send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap, 0)))
require.NoError(send(factory.makeAccessListTransactionWithPrice(t, nonce+1, feeCap, 0)))
require.NoError(send(factory.makeDynamicFeeTransactionWithPrice(t, nonce+2, feeCap, 0)))
require.NoError(send(factory.makeBlobTransactionWithPrice(t, nonce+3, feeCap, 0)))
}

func makeNetAndClient(t *testing.T) (*IntegrationTestNet, *ethclient.Client) {
Expand All @@ -113,12 +113,14 @@ func (f *txFactory) makeLegacyTransactionWithPrice(
t *testing.T,
nonce uint64,
price int64,
value int64,
) *types.Transaction {
transaction, err := types.SignTx(types.NewTx(&types.LegacyTx{
Gas: 21_000,
GasPrice: big.NewInt(price),
To: &common.Address{},
Nonce: nonce,
Value: big.NewInt(value),
}), types.NewEIP155Signer(f.chainId), f.senderKey)
require.NoError(t, err, "failed to sign transaction")
return transaction
Expand All @@ -128,13 +130,15 @@ func (f *txFactory) makeAccessListTransactionWithPrice(
t *testing.T,
nonce uint64,
price int64,
value int64,
) *types.Transaction {
transaction, err := types.SignTx(types.NewTx(&types.AccessListTx{
ChainID: f.chainId,
Gas: 21_000,
GasPrice: big.NewInt(price),
To: &common.Address{},
Nonce: nonce,
Value: big.NewInt(value),
}), types.NewEIP2930Signer(f.chainId), f.senderKey)
require.NoError(t, err, "failed to sign transaction:")
return transaction
Expand All @@ -144,6 +148,7 @@ func (f *txFactory) makeDynamicFeeTransactionWithPrice(
t *testing.T,
nonce uint64,
price int64,
value int64,
) *types.Transaction {
transaction, err := types.SignTx(types.NewTx(&types.DynamicFeeTx{
ChainID: f.chainId,
Expand All @@ -152,6 +157,7 @@ func (f *txFactory) makeDynamicFeeTransactionWithPrice(
GasTipCap: big.NewInt(0),
To: &common.Address{},
Nonce: nonce,
Value: big.NewInt(value),
}), types.NewLondonSigner(f.chainId), f.senderKey)
require.NoError(t, err, "failed to sign transaction:")
return transaction
Expand All @@ -161,13 +167,15 @@ func (f *txFactory) makeBlobTransactionWithPrice(
t *testing.T,
nonce uint64,
price int64,
value int64,
) *types.Transaction {
transaction, err := types.SignTx(types.NewTx(&types.BlobTx{
ChainID: uint256.MustFromBig(f.chainId),
Gas: 21_000,
GasFeeCap: uint256.MustFromBig(big.NewInt(price)),
GasTipCap: uint256.MustFromBig(big.NewInt(0)),
Nonce: nonce,
Value: uint256.MustFromBig(big.NewInt(value)),
BlobFeeCap: uint256.NewInt(3e10), // fee cap for the blob data
BlobHashes: nil, // blob hashes in the transaction
Sidecar: nil, // sidecar data in the transaction
Expand Down
97 changes: 51 additions & 46 deletions tests/rejected_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"context"
"fmt"
"math/big"
"testing"

Expand All @@ -22,53 +23,57 @@ func TestRejectedTx(t *testing.T) {
// create a client
client, err := net.GetClient()
require.NoError(err, "failed to get client")
defer client.Close()

chainId := getChainId(t, net)

testCases := []struct {
name string
txMaker func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction
}{
{name: "LegacyTx",
txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{
senderKey: account.PrivateKey,
chainId: chainId,
}
return factory.makeLegacyTransactionWithPrice(t, nonce, price)
},
}, {name: "AccessListTx",
txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{
senderKey: account.PrivateKey,
chainId: chainId,
}
return factory.makeAccessListTransactionWithPrice(t, nonce, price)
},
}, {name: "DynamicTx",
txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{
senderKey: account.PrivateKey,
chainId: chainId,
}
return factory.makeDynamicFeeTransactionWithPrice(t, nonce, price)
chainId := getChainId(t, client)

type testCase struct {
makeTx func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction
}

makeTestCaseWithAllTypesOfTx := func(value int64) map[string]testCase {
valueStr := fmt.Sprint(value)
cases := map[string]testCase{
"LegacyTxWithValue" + valueStr: {
makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId}
return factory.makeLegacyTransactionWithPrice(t, nonce, price, value)
},
},
}, {name: "BlobTx",
txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{
senderKey: account.PrivateKey,
chainId: chainId,
}
return factory.makeBlobTransactionWithPrice(t, nonce, price)
"AccessListTxWithValue" + valueStr: {
makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId}
return factory.makeAccessListTransactionWithPrice(t, nonce, price, value)
},
}, "DynamicTxWithValue" + valueStr: {
makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId}
return factory.makeDynamicFeeTransactionWithPrice(t, nonce, price, value)
},
}, "BlobTxWithValue" + valueStr: {
makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction {
factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId}
return factory.makeBlobTransactionWithPrice(t, nonce, price, value)
},
},
},
}
return cases
}

testCases := map[string]testCase{}
for name, test := range makeTestCaseWithAllTypesOfTx(0) {
testCases[name] = test
}
for name, test := range makeTestCaseWithAllTypesOfTx(42) {
testCases[name] = test
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
maxFeeCap := getMaxFee(t, client)
newAccount := NewAccount()
nonce := getNonce(t, client, newAccount.Address())
testRejectedTx(t, net, newAccount.Address(), tc.txMaker(t, newAccount, nonce, maxFeeCap))
testRejectedTx(t, net, newAccount.Address(), tc.makeTx(t, newAccount, nonce, maxFeeCap))
})
}
}
Expand All @@ -78,7 +83,10 @@ func testRejectedTx(t *testing.T, net *IntegrationTestNet, account common.Addres

// verify estimated cost
estimatedCost := tx.Gas()*tx.GasFeeCap().Uint64() + tx.Value().Uint64()
require.Equal(tx.Cost().Uint64(), estimatedCost, "cost of transaction is not equal to balance")
if tx.Type() == types.BlobTxType {
estimatedCost += tx.BlobGasFeeCap().Uint64() * tx.BlobGas()
}
require.Equal(tx.Cost().Int64(), int64(estimatedCost), "transaction estimation is not equal to balance")

// provide just enough balance to NOT cover the cost
_, err := net.EndowAccount(account, int64(estimatedCost-1))
Expand All @@ -104,10 +112,7 @@ func testRejectedTx(t *testing.T, net *IntegrationTestNet, account common.Addres
require.GreaterOrEqual(tx.Cost().Uint64(), receipt.EffectiveGasPrice.Uint64()*receipt.GasUsed)
}

func getChainId(t *testing.T, net *IntegrationTestNet) *big.Int {
client, err := net.GetClient()
require.NoError(t, err, "failed to get client")

func getChainId(t *testing.T, client *ethclient.Client) *big.Int {
chainId, err := client.ChainID(context.Background())
require.NoError(t, err, "failed to get chain ID::")
return chainId
Expand All @@ -117,7 +122,7 @@ func getMaxFee(t *testing.T, client *ethclient.Client) (maxFeeCap int64) {
block, err := client.BlockByNumber(context.Background(), nil)
require.NoError(t, err, "failed to get block by number")
baseFee := block.BaseFee().Int64()
maxFeeCap = int64(float64(baseFee) * 1.06)
maxFeeCap = int64(float64(baseFee) * 1.05)
return
}

Expand Down

0 comments on commit 5ac90f4

Please sign in to comment.