Skip to content

Commit

Permalink
restructure test
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Nov 26, 2024
1 parent 266ea65 commit 538529c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 118 deletions.
4 changes: 2 additions & 2 deletions tests/gas_price_suggestion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (f *txFactory) makeLegacyTransactionWithPrice(
GasPrice: big.NewInt(price),
To: &common.Address{},
Nonce: nonce,
}), types.NewLondonSigner(f.chainId), f.senderKey)
}), types.NewEIP155Signer(f.chainId), f.senderKey)
require.NoError(t, err, "failed to sign transaction")
return transaction
}
Expand All @@ -135,7 +135,7 @@ func (f *txFactory) makeAccessListTransactionWithPrice(
GasPrice: big.NewInt(price),
To: &common.Address{},
Nonce: nonce,
}), types.NewLondonSigner(f.chainId), f.senderKey)
}), types.NewEIP2930Signer(f.chainId), f.senderKey)
require.NoError(t, err, "failed to sign transaction:")
return transaction
}
Expand Down
180 changes: 64 additions & 116 deletions tests/rejected_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,134 +23,85 @@ func TestRejectedTx(t *testing.T) {
client, err := net.GetClient()
require.NoError(err, "failed to get client")

// make a dynamic tx that cannot be afford
value := big.NewInt(42)
gas := int64(21000)
chainId := getChainId(t, net)

testCases := map[string]struct {
nonce uint64
txMaker func(t *testing.T, net *IntegrationTestNet, tc testConfig) *types.Transaction
testCases := []struct {
name string
txMaker func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction
}{
"LegacyTx": {
nonce: 0,
txMaker: makeLegacyTxWithValue,
},
"AccessListTx": {
nonce: 1,
txMaker: makeAccessListTxWithValue,
},
"DynamicTx": {
nonce: 2,
txMaker: makeDynamicTxWithValue,
},
"BlobTx": {
nonce: 3,
txMaker: makeBlobTxWithValue,
{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)
},
}, {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)
},
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
maxFeeCap := getMaxFee(t, client)
newAccount := NewAccount()
testConfig := testConfig{
account: newAccount,
value: value,
gas: uint64(gas),
maxFeeCap: big.NewInt(maxFeeCap),
nonce: tc.nonce,
}
testRejectedTx(t, net, testConfig, tc.txMaker(t, net, testConfig))
nonce := getNonce(t, client, newAccount.Address())
testRejectedTx(t, net, newAccount.Address(), tc.txMaker(t, newAccount, nonce, maxFeeCap))
})
}
}

func testRejectedTx(t *testing.T, net *IntegrationTestNet, testConfig testConfig, tx *types.Transaction) {
func testRejectedTx(t *testing.T, net *IntegrationTestNet, account common.Address, tx *types.Transaction) {
require := require.New(t)

// create a client
client, err := net.GetClient()
require.NoError(err, "failed to get client")

balance := tx.Gas()*tx.GasFeeCap().Uint64() + tx.Value().Uint64()
// 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")

_, err = net.EndowAccount(testConfig.account.Address(), int64(balance-1))
// provide just enough balance to NOT cover the cost
_, err := net.EndowAccount(account, int64(estimatedCost-1))
require.NoError(err, "failed to endow account")

err = client.SendTransaction(context.Background(), tx)
// run transaction to be rejected
receipt, err := net.Run(tx)
require.ErrorContains(err, "insufficient funds")
require.Nil(receipt)

_, err = net.EndowAccount(testConfig.account.Address(), int64(1))
// provide enough balance to cover the cost
_, err = net.EndowAccount(account, int64(1))
require.NoError(err, "failed to endow account")

err = client.SendTransaction(context.Background(), tx)
// run transaction to be successful
receipt, err = net.Run(tx)
require.NoError(err)
}

func makeLegacyTxWithValue(
t *testing.T,
net *IntegrationTestNet,
tc testConfig,
) *types.Transaction {
chainId := getChainId(t, net)
transaction, err := types.SignTx(types.NewTx(&types.LegacyTx{
Gas: tc.gas,
GasPrice: tc.maxFeeCap,
To: &common.Address{},
Nonce: tc.nonce,
Value: tc.value,
}), types.NewLondonSigner(chainId), tc.account.PrivateKey)
require.NoError(t, err, "failed to sign transaction:")
return transaction
}

func makeAccessListTxWithValue(
t *testing.T,
net *IntegrationTestNet,
tc testConfig,
) *types.Transaction {
chainId := getChainId(t, net)
transaction, err := types.SignTx(types.NewTx(&types.AccessListTx{
ChainID: chainId,
Gas: tc.gas,
GasPrice: tc.maxFeeCap,
To: &common.Address{},
Nonce: tc.nonce,
Value: tc.value,
}), types.NewLondonSigner(chainId), tc.account.PrivateKey)
require.NoError(t, err, "failed to sign transaction:")
return transaction
}

func makeDynamicTxWithValue(t *testing.T, net *IntegrationTestNet, tc testConfig) *types.Transaction {
chainId := getChainId(t, net)
transaction, err := types.SignTx(types.NewTx(&types.DynamicFeeTx{
ChainID: chainId,
Gas: tc.gas,
GasFeeCap: tc.maxFeeCap,
To: &common.Address{},
Nonce: tc.nonce,
Value: tc.value,
}), types.NewLondonSigner(chainId), tc.account.PrivateKey)
require.NoError(t, err, "failed to sign transaction:")
return transaction
}

func makeBlobTxWithValue(t *testing.T, net *IntegrationTestNet, tc testConfig) *types.Transaction {
chainId := getChainId(t, net)
transaction, err := types.SignTx(types.NewTx(&types.BlobTx{
ChainID: uint256.MustFromBig(chainId),
Gas: tc.gas,
GasFeeCap: uint256.MustFromBig(tc.maxFeeCap),
GasTipCap: uint256.MustFromBig(big.NewInt(0)),
Nonce: tc.nonce,
Value: uint256.MustFromBig(tc.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
}), types.NewCancunSigner(chainId), tc.account.PrivateKey)
require.NoError(t, err, "failed to sign transaction:")
return transaction
// verify receipt
require.Equal(receipt.Status, uint64(1))
require.Equal(tx.Gas(), receipt.GasUsed)
require.GreaterOrEqual(tx.GasFeeCap().Uint64(), receipt.EffectiveGasPrice.Uint64())
require.GreaterOrEqual(tx.Cost().Uint64(), receipt.EffectiveGasPrice.Uint64()*receipt.GasUsed)
}

func getChainId(t *testing.T, net *IntegrationTestNet) *big.Int {
Expand All @@ -166,15 +116,13 @@ func getChainId(t *testing.T, net *IntegrationTestNet) *big.Int {
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 := int64(block.BaseFee().Uint64())
maxFeeCap = baseFee + int64(float64(baseFee)*0.06)
baseFee := block.BaseFee().Int64()
maxFeeCap = int64(float64(baseFee) * 1.06)
return
}

type testConfig struct {
account *Account
value *big.Int
gas uint64
maxFeeCap *big.Int
nonce uint64
func getNonce(t *testing.T, client *ethclient.Client, account common.Address) (nonce uint64) {
nonce, err := client.NonceAt(context.Background(), account, nil)
require.NoError(t, err, "failed to get nonce:")
return
}

0 comments on commit 538529c

Please sign in to comment.