Skip to content

Commit

Permalink
feat: update nonce error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Oct 3, 2024
1 parent 1b15fcd commit 9c15751
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions node/pkg/chain/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ func (t *ChainHelper) SubmitDelegatedFallbackDirect(ctx context.Context, contrac
} else if errors.Is(err, errorSentinel.ErrChainTransactionFail) {
return err // if transaction fails, the data will probably be too old to retry
} else if utils.IsNonceError(err) || err == context.DeadlineExceeded {
err = noncemanager.ResetNonce(ctx, t.wallet, t.clients[clientIndex])
if err != nil {
return err
}
nonce, err = noncemanager.GetAndIncrementNonce(t.wallet)
if err != nil {
return err
Expand Down Expand Up @@ -302,6 +306,10 @@ func (t *ChainHelper) SubmitDelegatedFallbackDirect(ctx context.Context, contrac
} else if errors.Is(err, errorSentinel.ErrChainTransactionFail) {
return err // if transaction fails, the data will probably be too old to retry
} else if utils.IsNonceError(err) || err == context.DeadlineExceeded {
err = noncemanager.ResetNonce(ctx, t.wallet, t.clients[clientIndex])
if err != nil {
return err
}
nonce, err = noncemanager.GetAndIncrementNonce(t.wallet)
if err != nil {
return err
Expand Down
18 changes: 18 additions & 0 deletions node/pkg/chain/noncemanager/noncemanager.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package noncemanager

import (
"context"
"fmt"
"sync"

"bisonai.com/miko/node/pkg/chain/utils"
)

type NonceManager struct {
Expand All @@ -25,6 +28,10 @@ func Get() *NonceManager {
return Manager
}

func ResetNonce(ctx context.Context, address string, client utils.ClientInterface) error {
return Get().ResetNonce(ctx, address, client)
}

func Set(address string, nonce uint64) {
Get().SetNonce(address, nonce)
}
Expand All @@ -33,6 +40,17 @@ func GetAndIncrementNonce(address string) (uint64, error) {
return Get().GetAndIncrementNonce(address)
}

func (m *NonceManager) ResetNonce(ctx context.Context, address string, client utils.ClientInterface) error {
m.mu.Lock()
defer m.mu.Unlock()
nonce, err := utils.GetNonceFromPk(ctx, address, client)
if err != nil {
return err
}
m.nonces[address] = nonce
return nil
}

func (m *NonceManager) SetNonce(address string, nonce uint64) {
m.mu.Lock()
defer m.mu.Unlock()
Expand Down

0 comments on commit 9c15751

Please sign in to comment.