Skip to content

Commit

Permalink
cmd/livefuzzer: add proper error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed Oct 9, 2023
1 parent 241bebe commit e385650
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
12 changes: 6 additions & 6 deletions cmd/livefuzzer/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/ethereum/go-ethereum/log"
)

func SendBasicTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Filler) {
func SendBasicTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Filler) error {
backend := ethclient.NewClient(config.backend)
sender := crypto.PubkeyToAddress(key.PublicKey)
chainID, err := backend.ChainID(context.Background())
Expand All @@ -29,21 +29,20 @@ func SendBasicTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Fill
for i := uint64(0); i < config.n; i++ {
nonce, err := backend.NonceAt(context.Background(), sender, big.NewInt(-1))
if err != nil {
log.Warn("Could not get nonce: %v", nonce)
continue
return err
}
tx, err := txfuzz.RandomValidTx(config.backend, f, sender, nonce, nil, nil, config.accessList)
if err != nil {
log.Warn("Could not create valid tx: %v", nonce)
continue
return err
}
signedTx, err := types.SignTx(tx, types.NewCancunSigner(chainID), key)
if err != nil {
panic(err)
return err
}
if err := backend.SendTransaction(context.Background(), signedTx); err != nil {
log.Warn("Could not submit transaction: %v", err)
continue
return err
}
lastTx = signedTx
time.Sleep(10 * time.Millisecond)
Expand All @@ -55,4 +54,5 @@ func SendBasicTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Fill
fmt.Printf("Wait mined failed for SendBaikalTransactions: %v\n", err.Error())
}
}
return nil
}
14 changes: 7 additions & 7 deletions cmd/livefuzzer/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/ethereum/go-ethereum/log"
)

func SendBlobTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Filler) {
func SendBlobTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Filler) error {
backend := ethclient.NewClient(config.backend)
sender := crypto.PubkeyToAddress(key.PublicKey)
chainID, err := backend.ChainID(context.Background())
Expand All @@ -31,30 +31,29 @@ func SendBlobTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Fille
for i := uint64(0); i < config.n; i++ {
nonce, err := backend.NonceAt(context.Background(), sender, big.NewInt(-1))
if err != nil {
log.Warn("Could not get nonce: %v", nonce)
continue
return err
}
tx, err := txfuzz.RandomBlobTx(config.backend, f, sender, nonce, nil, nil, config.accessList)
if err != nil {
log.Warn("Could not create valid tx: %v", nonce)
continue
return err
}
signedTx, err := types.SignTx(tx.Transaction, types.NewCancunSigner(chainID), key)
if err != nil {
panic(err)
return err
}
tx.Transaction = signedTx
rlpData, err := tx.MarshalBinary()
if err != nil {
panic(err)
return err
}
if err := config.backend.CallContext(context.Background(), nil, "eth_sendRawTransaction", hexutil.Encode(rlpData)); err != nil {
if strings.Contains(err.Error(), "account limit exceeded") {
// Back off for a bit if we send a lot of transactions at once
time.Sleep(1 * time.Minute)
continue
} else {
panic(err)
return err
}
}
lastTx = signedTx
Expand All @@ -68,4 +67,5 @@ func SendBlobTransactions(config *Config, key *ecdsa.PrivateKey, f *filler.Fille
fmt.Printf("Wait mined failed for blob transactions: %v\n", err.Error())
}
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/livefuzzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func runBlobSpam(c *cli.Context) error {
}
airdropValue := new(big.Int).Mul(big.NewInt(int64((1+config.n)*1000000)), big.NewInt(params.GWei))
airdropValue = airdropValue.Mul(airdropValue, big.NewInt(100))
return spam(config, SendBasicTransactions, airdropValue)
return spam(config, SendBlobTransactions, airdropValue)
}

func runUnstuck(c *cli.Context) error {
Expand Down
13 changes: 10 additions & 3 deletions cmd/livefuzzer/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/MariusVanDerWijden/FuzzyVM/filler"
)

type Spam func(*Config, *ecdsa.PrivateKey, *filler.Filler)
type Spam func(*Config, *ecdsa.PrivateKey, *filler.Filler) error

func SpamTransactions(config *Config, fun Spam) {
func SpamTransactions(config *Config, fun Spam) error {
fmt.Printf("Spamming %v transactions per account on %v accounts with seed: 0x%x\n", config.n, len(config.keys), config.seed)

errCh := make(chan error, len(config.keys))
var wg sync.WaitGroup
wg.Add(len(config.keys))
for _, key := range config.keys {
Expand All @@ -34,8 +35,14 @@ func SpamTransactions(config *Config, fun Spam) {
// Start a fuzzing thread
go func(key *ecdsa.PrivateKey, filler *filler.Filler) {
defer wg.Done()
fun(config, key, f)
errCh <- fun(config, key, f)
}(key, f)
}
wg.Wait()
select {
case err := <-errCh:
return err
default:
return nil
}
}

0 comments on commit e385650

Please sign in to comment.