Skip to content

Commit

Permalink
[EVM] Update loadtest client receipt polling (#1330)
Browse files Browse the repository at this point in the history
* update load test receipt polling logic

* Fix amount value and remove GetTxReceipt

* Fix lint

* Fix lint

* Remove mutex

* Fix

---------

Co-authored-by: yzang2019 <[email protected]>
Co-authored-by: Yiming Zang <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2024
1 parent 5691dd5 commit e1ba63d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 41 deletions.
7 changes: 0 additions & 7 deletions evmrpc/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package evmrpc
import (
"context"
"errors"
"sync"
"time"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -24,7 +23,6 @@ type SendAPI struct {
tmClient rpcclient.Client
txConfig client.TxConfig
sendConfig *SendConfig
slowMu *sync.Mutex
keeper *keeper.Keeper
ctxProvider func(int64) sdk.Context
homeDir string
Expand All @@ -40,7 +38,6 @@ func NewSendAPI(tmClient rpcclient.Client, txConfig client.TxConfig, sendConfig
tmClient: tmClient,
txConfig: txConfig,
sendConfig: sendConfig,
slowMu: &sync.Mutex{},
keeper: k,
ctxProvider: ctxProvider,
homeDir: homeDir,
Expand All @@ -51,10 +48,6 @@ func NewSendAPI(tmClient rpcclient.Client, txConfig client.TxConfig, sendConfig
func (s *SendAPI) SendRawTransaction(ctx context.Context, input hexutil.Bytes) (hash common.Hash, err error) {
startTime := time.Now()
defer recordMetrics("eth_sendRawTransaction", startTime, err == nil)
if s.sendConfig.slow {
s.slowMu.Lock()
defer s.slowMu.Unlock()
}
tx := new(ethtypes.Transaction)
if err = tx.UnmarshalBinary(input); err != nil {
return
Expand Down
39 changes: 5 additions & 34 deletions loadtest/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"math"
"math/big"
"math/rand"
"sync"
Expand Down Expand Up @@ -84,8 +83,8 @@ func (txClient *EvmTxClient) GenerateEvmSignedTx() *ethtypes.Transaction {

// Generate random amount to send
rand.Seed(time.Now().Unix())
value := big.NewInt(rand.Int63n(math.MaxInt64 - 1))
gasLimit := uint64(200000)
value := big.NewInt(rand.Int63n(9000000) * 1000000000000)
gasLimit := uint64(21000)
tx := ethtypes.NewTransaction(nextNonce, txClient.accountAddress, value, gasLimit, txClient.gasPrice, nil)
signedTx, err := ethtypes.SignTx(tx, ethtypes.NewEIP155Signer(txClient.chainId), privateKey)
if err != nil {
Expand All @@ -100,20 +99,10 @@ func (txClient *EvmTxClient) SendEvmTx(signedTx *ethtypes.Transaction, onSuccess
err := GetNextEthClient(txClient.ethClients).SendTransaction(context.Background(), signedTx)
if err != nil {
fmt.Printf("Failed to send evm transaction: %v \n", err)
} else {
// We choose not to GetTxReceipt because we assume the EVM RPC would be running with broadcast mode = block
onSuccess()
}

go func() {
success, errs := withRetry(func() error {
return txClient.GetTxReceipt(signedTx.Hash())
})
if success {
onSuccess()
} else {
fmt.Printf("Failed to get evm transaction receipt: %v \n", errs)
_ = txClient.ResetNonce()
}
}()

}

// GetNextEthClient return the next available eth client randomly
Expand All @@ -139,7 +128,6 @@ func (txClient *EvmTxClient) GetTxReceipt(txHash common.Hash) error {

// ResetNonce need to be called when tx failed
func (txClient *EvmTxClient) ResetNonce() error {

txClient.mtx.Lock()
defer txClient.mtx.Unlock()
client := GetNextEthClient(txClient.ethClients)
Expand All @@ -151,20 +139,3 @@ func (txClient *EvmTxClient) ResetNonce() error {
fmt.Printf("Resetting nonce to %d for addr: %s\n ", newNonce, txClient.accountAddress.String())
return nil
}

func withRetry(callFunc func() error) (bool, error) {
retryCount := 0
for {
err := callFunc()
if err != nil {
retryCount++
if retryCount >= 5 {
return false, err
}
time.Sleep(1 * time.Second)
continue
} else {
return true, nil
}
}
}

0 comments on commit e1ba63d

Please sign in to comment.