Skip to content

Commit

Permalink
add legacy tx test
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Nov 15, 2024
1 parent 2e583db commit 2e5f439
Showing 1 changed file with 132 additions and 27 deletions.
159 changes: 132 additions & 27 deletions tests/gas_price_suggestion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@ package tests

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

"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"
)

func TestGasPrice_GasEvolvesAsExpectedCalculates(t *testing.T) {
require := require.New(t)

net, err := StartIntegrationTestNet(t.TempDir())
require.NoError(err)
defer net.Stop()

/*
// Produce a few blocks on the network.
for range 10 {
_, err = net.EndowAccount(common.Address{42}, 100)
require.NoError(err)
}
*/

client, err := net.GetClient()
require.NoError(err)
defer client.Close()
net, client := makeNetAndClient(t)

suggestions := []uint64{}
prices := []uint64{}
Expand All @@ -44,25 +33,141 @@ func TestGasPrice_GasEvolvesAsExpectedCalculates(t *testing.T) {
lastBlock, err := client.BlockByNumber(context.Background(), receipt.BlockNumber)
require.NoError(err)

// store suggested and actuaaal prices.
suggestions = append(suggestions, suggestedPrice.Uint64())
prices = append(prices, lastBlock.BaseFee().Uint64())

diff, ok := within10Percent(suggestedPrice, lastBlock.BaseFee())
t.Logf("i: %v, last block's base fee (%v) ok:%v, suggested price %v, diff: %v", i, lastBlock.BaseFee(), ok, suggestedPrice, diff)
}

for i := range suggestions {
fmt.Printf("%d, %d, %d\n", i, suggestions[i], prices[i])
require.True(withinXPercent(suggestions[i], prices[i], 10), "i", i)
}
}

func within10Percent(a, b *big.Int) (*big.Int, bool) {
func TestGasPrice_UnderpricedTransactionIsRejected(t *testing.T) {
require := require.New(t)

net, client := makeNetAndClient(t)

// new block
receipt, err := net.EndowAccount(common.Address{42}, 100)
require.NoError(err)

lastBlock, err := client.BlockByNumber(context.Background(), receipt.BlockNumber)
require.NoError(err)

lastBlockBaseFee := lastBlock.BaseFee().Uint64()

// create and run a legacy transaction with a price lower than the suggested price
transaction := makeLegacyTransactionWithPrice(t, net, int64(lastBlockBaseFee))
receipt, err = net.Run(transaction)
require.ErrorContains(err, "transaction underpriced")

// check if the transaction was included in the block
require.Nil(receipt)

}

func makeNetAndClient(t *testing.T) (*IntegrationTestNet, *ethclient.Client) {
net, err := StartIntegrationTestNet(t.TempDir())
require.NoError(t, err)

client, err := net.GetClient()
require.NoError(t, err)

return net, client
}

func withinXPercent(a, b uint64, margin float64) bool {
// calculate the difference
diff := new(big.Int).Sub(a, b)
diff.Abs(diff)
// calculate 10% of a
tenPercent := new(big.Int).Mul(a, big.NewInt(10))
tenPercent.Div(tenPercent, big.NewInt(100))
diff := uint64(0)
if a > b {
diff = a - b
} else {
diff = b - a
}

percentage := uint64(math.Round(float64(a) * margin / 100))

// check if the difference is less than 10% of a
return diff.Div(diff, a), diff.Cmp(tenPercent) < 0
return diff <= percentage
}

func makeLegacyTransactionWithPrice(t *testing.T, net *IntegrationTestNet, price int64) *types.Transaction {
require := require.New(t)

chainId, nonce := getChainIDAndNonce(t, net)

transaction, err := types.SignTx(types.NewTx(&types.AccessListTx{
ChainID: chainId,
Gas: 21_000,
GasPrice: big.NewInt(price),
To: &common.Address{},
Nonce: nonce,
}), types.NewLondonSigner(chainId), net.validator.PrivateKey)
require.NoError(err, "failed to sign transaction:")
return transaction
}

func makeAccessListTransactionWithPrice(t *testing.T, net *IntegrationTestNet, price int64) *types.Transaction {

Check failure on line 111 in tests/gas_price_suggestion_test.go

View workflow job for this annotation

GitHub Actions / check-build

func makeAccessListTransactionWithPrice is unused (U1000)
require := require.New(t)

chainId, nonce := getChainIDAndNonce(t, net)

transaction, err := types.SignTx(types.NewTx(&types.AccessListTx{
ChainID: chainId,
Gas: 21_000,
GasPrice: big.NewInt(price),
To: &common.Address{},
Nonce: nonce,
}), types.NewLondonSigner(chainId), net.validator.PrivateKey)
require.NoError(err, "failed to sign transaction:")
return transaction
}

func makeDynamicFeeTransactionWithPrice(t *testing.T, net *IntegrationTestNet, price int64) *types.Transaction {

Check failure on line 127 in tests/gas_price_suggestion_test.go

View workflow job for this annotation

GitHub Actions / check-build

func makeDynamicFeeTransactionWithPrice is unused (U1000)
require := require.New(t)

chainId, nonce := getChainIDAndNonce(t, net)

transaction, err := types.SignTx(types.NewTx(&types.DynamicFeeTx{
ChainID: chainId,
Gas: 21_000,
GasFeeCap: big.NewInt(price),
GasTipCap: big.NewInt(price),
To: &common.Address{},
Nonce: nonce,
}), types.NewLondonSigner(chainId), net.validator.PrivateKey)
require.NoError(err, "failed to sign transaction:")
return transaction
}

func makeBlobTransactionWithPrice(t *testing.T, net *IntegrationTestNet, price int64) *types.Transaction {

Check failure on line 144 in tests/gas_price_suggestion_test.go

View workflow job for this annotation

GitHub Actions / check-build

func makeBlobTransactionWithPrice is unused (U1000)
require := require.New(t)

chainId, nonce := getChainIDAndNonce(t, net)

transaction, err := types.SignTx(types.NewTx(&types.BlobTx{
ChainID: uint256.MustFromBig(chainId),
Gas: 21_000,
GasFeeCap: uint256.MustFromBig(big.NewInt(price)),
GasTipCap: uint256.MustFromBig(big.NewInt(price)),
Nonce: nonce,
}), types.NewLondonSigner(chainId), net.validator.PrivateKey)
require.NoError(err, "failed to sign transaction:")
return transaction
}

func getChainIDAndNonce(t *testing.T, net *IntegrationTestNet) (*big.Int, uint64) {
require := require.New(t)
// these values are needed for the transaction but are irrelevant for the test
client, err := net.GetClient()
require.NoError(err, "failed to connect to the network:")
defer client.Close()

chainId, err := client.ChainID(context.Background())
require.NoError(err, "failed to get chain ID::")

nonce, err := client.NonceAt(context.Background(), net.validator.Address(), nil)
require.NoError(err, "failed to get nonce:")
return chainId, nonce
}

0 comments on commit 2e5f439

Please sign in to comment.