Skip to content

Commit

Permalink
fix: streamline the nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalden committed Jan 16, 2025
1 parent 715e04e commit 5faceee
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
9 changes: 9 additions & 0 deletions relayer/chains/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type IClient interface {
SuggestGasPrice(ctx context.Context) (*big.Int, error)
SuggestGasTip(ctx context.Context) (*big.Int, error)
PendingNonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
GetLatestNonce(ctx context.Context, account common.Address) (*big.Int, error)
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*ethTypes.Receipt, error)
EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
Expand Down Expand Up @@ -135,6 +136,14 @@ func (c *Client) PendingNonceAt(ctx context.Context, account common.Address, blo
return new(big.Int).SetUint64(nonce), nil
}

func (c *Client) GetLatestNonce(ctx context.Context, account common.Address) (*big.Int, error) {
nonce, err := c.eth.NonceAt(ctx, account, nil)
if err != nil {
return nil, err
}
return new(big.Int).SetUint64(nonce), nil
}

func (cl *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*ethTypes.Receipt, error) {
return cl.eth.TransactionReceipt(ctx, txHash)
}
Expand Down
16 changes: 7 additions & 9 deletions relayer/chains/evm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
bridgeContract "github.com/icon-project/centralized-relay/relayer/chains/evm/abi"
"github.com/icon-project/centralized-relay/relayer/chains/evm/types"
"github.com/icon-project/centralized-relay/relayer/events"
"github.com/icon-project/centralized-relay/relayer/kms"
"github.com/icon-project/centralized-relay/relayer/provider"
Expand Down Expand Up @@ -61,7 +60,6 @@ type Provider struct {
wallet *keystore.Key
kms kms.KMS
contracts map[string]providerTypes.EventMap
NonceTracker types.NonceTrackerI
LastSavedHeightFunc func() uint64
routerMutex *sync.Mutex
}
Expand Down Expand Up @@ -142,7 +140,6 @@ func (p *Provider) Init(ctx context.Context, homePath string, kms kms.KMS) error
return fmt.Errorf("error occured when creating client: %v", err)
}
p.client = client
p.NonceTracker = types.NewNonceTracker(client.PendingNonceAt)
p.kms = kms
return nil
}
Expand All @@ -165,11 +162,6 @@ func (p *Provider) Wallet() (*keystore.Key, error) {
if err := p.RestoreKeystore(ctx); err != nil {
return nil, err
}
nonce, err := p.client.PendingNonceAt(ctx, p.wallet.Address, nil)
if err != nil {
return nil, err
}
p.NonceTracker.Set(p.wallet.Address, nonce)
}
return p.wallet, nil
}
Expand Down Expand Up @@ -262,7 +254,13 @@ func (p *Provider) GetTransationOpts(ctx context.Context) (*bind.TransactOpts, e
}
ctx, cancel := context.WithTimeout(ctx, defaultReadTimeout)
defer cancel()
txOpts.Nonce = p.NonceTracker.Get(wallet.Address)

latestNonce, err := p.client.GetLatestNonce(ctx, wallet.Address)
if err != nil {
return nil, err
}

txOpts.Nonce = latestNonce.Add(latestNonce, big.NewInt(1))
gasPrice, err := p.client.SuggestGasPrice(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get gas price: %w", err)
Expand Down
15 changes: 1 addition & 14 deletions relayer/chains/evm/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (p *Provider) Route(ctx context.Context, message *providerTypes.Message, ca
}

tx, err := p.SendTransaction(ctx, opts, message)
p.routerMutex.Unlock()
if err != nil {
return fmt.Errorf("failed to send transaction: %w", err)
}

p.WaitForTxResult(ctx, tx, message.MessageKey(), callback)
p.routerMutex.Unlock()
return nil
}

Expand Down Expand Up @@ -110,19 +110,6 @@ func (p *Provider) SendTransaction(ctx context.Context, opts *bind.TransactOpts,
default:
return nil, fmt.Errorf("unknown event type: %s", message.EventType)
}
if err != nil {
switch p.parseErr(err) {
case ErrNonceTooLow, ErrNonceTooHigh, ErrorLessGas:
nonce, err := p.client.PendingNonceAt(ctx, p.wallet.Address, nil)
if err != nil {
return nil, err
}
p.log.Info("nonce mismatch", zap.Uint64("tx", opts.Nonce.Uint64()), zap.Uint64("current", nonce.Uint64()), zap.Error(err))
p.NonceTracker.Set(p.wallet.Address, nonce)
default:
return nil, err
}
}
return tx, err
}

Expand Down

0 comments on commit 5faceee

Please sign in to comment.