Skip to content

Commit

Permalink
Replace hardcoded tx costs with a call to eth_estimateGas (#46)
Browse files Browse the repository at this point in the history
* use estimate gas when sending legacy txs

* fix estimation gas limit + log estimate failure

* txfuzz: move gas estimation

* spammer: uppercase

---------

Co-authored-by: Marius van der Wijden <[email protected]>
  • Loading branch information
gballet and MariusVanDerWijden authored Jan 15, 2024
1 parent d8b943f commit 264171c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
18 changes: 17 additions & 1 deletion spammer/addresslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"math/big"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
)

var (
Expand Down Expand Up @@ -145,6 +148,7 @@ func CreateAddresses(N int) ([]string, []string) {
func Airdrop(config *Config, value *big.Int) error {
backend := ethclient.NewClient(config.backend)
sender := crypto.PubkeyToAddress(config.faucet.PublicKey)
fmt.Printf("Airdrop faucet is at %x\n", sender)
var tx *types.Transaction
chainid, err := backend.ChainID(context.Background())
if err != nil {
Expand All @@ -159,7 +163,19 @@ func Airdrop(config *Config, value *big.Int) error {
}
to := crypto.PubkeyToAddress(addr.PublicKey)
gp, _ := backend.SuggestGasPrice(context.Background())
tx2 := types.NewTransaction(nonce, to, value, 21000, gp, nil)
gas, err := backend.EstimateGas(context.Background(), ethereum.CallMsg{
From: crypto.PubkeyToAddress(config.faucet.PublicKey),
To: &to,
Gas: math.MaxInt64,
GasPrice: gp,
Value: value,
Data: nil,
})
if err != nil {
log.Error("error estimating gas: %v", err)
return err
}
tx2 := types.NewTransaction(nonce, to, value, gas, gp, nil)
signedTx, _ := types.SignTx(tx2, types.LatestSignerForChainID(chainid), config.faucet)
if err := backend.SendTransaction(context.Background(), signedTx); err != nil {
fmt.Printf("error sending transaction; could not airdrop: %v\n", err)
Expand Down
12 changes: 11 additions & 1 deletion spammer/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"math/big"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -32,7 +34,15 @@ func sendTxWithNonce(sk *ecdsa.PrivateKey, backend *ethclient.Client, to common.
return nil, err
}
gp, _ := backend.SuggestGasPrice(context.Background())
tx := types.NewTransaction(nonce, to, value, 500000, gp.Mul(gp, big.NewInt(100)), nil)
gas, _ := backend.EstimateGas(context.Background(), ethereum.CallMsg{
From: crypto.PubkeyToAddress(sk.PublicKey),
To: &to,
Gas: math.MaxUint64,
GasPrice: gp,
Value: value,
Data: nil,
})
tx := types.NewTransaction(nonce, to, value, gas, gp.Mul(gp, big.NewInt(100)), nil)
signedTx, _ := types.SignTx(tx, types.NewEIP155Signer(chainid), sk)
return signedTx, backend.SendTransaction(context.Background(), signedTx)
}
Expand Down
39 changes: 29 additions & 10 deletions transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package txfuzz
import (
"context"
"crypto/sha256"
"math"
"math/big"
"math/rand"

"github.com/MariusVanDerWijden/FuzzyVM/filler"
"github.com/MariusVanDerWijden/FuzzyVM/generator"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
Expand Down Expand Up @@ -44,6 +46,14 @@ type txConf struct {
}

func initDefaultTxConf(rpc *rpc.Client, f *filler.Filler, sender common.Address, nonce uint64, gasPrice, chainID *big.Int) *txConf {
// defaults
gasCost := uint64(100000)
to := randomAddress()
code := RandomCode(f)
value := big.NewInt(0)
if len(code) > 128 {
code = code[:128]
}
// Set fields if non-nil
if rpc != nil {
client := ethclient.NewClient(rpc)
Expand All @@ -60,21 +70,29 @@ func initDefaultTxConf(rpc *rpc.Client, f *filler.Filler, sender common.Address,
chainID = big.NewInt(1)
}
}
// Try to estimate gas
gas, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
From: sender,
To: &to,
Gas: math.MaxUint64,
GasPrice: gasPrice,
GasFeeCap: gasPrice,
GasTipCap: gasPrice,
Value: value,
Data: code,
})
if err == nil {
gasCost = gas
}
}
gas := uint64(100000)
to := randomAddress()
code := RandomCode(f)
value := big.NewInt(0)
if len(code) > 128 {
code = code[:128]
}

return &txConf{
rpc: rpc,
nonce: nonce,
sender: sender,
to: &to,
value: value,
gasLimit: gas,
gasLimit: gasCost,
gasPrice: gasPrice,
chainID: chainID,
code: code,
Expand All @@ -87,11 +105,12 @@ func initDefaultTxConf(rpc *rpc.Client, f *filler.Filler, sender common.Address,
// If chainID is not set, we will try to get it from the rpc
func RandomValidTx(rpc *rpc.Client, f *filler.Filler, sender common.Address, nonce uint64, gasPrice, chainID *big.Int, al bool) (*types.Transaction, error) {
conf := initDefaultTxConf(rpc, f, sender, nonce, gasPrice, chainID)
var index int
if al {
index := rand.Intn(len(alStrategies))
index = rand.Intn(len(alStrategies))
return alStrategies[index](conf)
} else {
index := rand.Intn(len(noAlStrategies))
index = rand.Intn(len(noAlStrategies))
return noAlStrategies[index](conf)
}
}
Expand Down

0 comments on commit 264171c

Please sign in to comment.