From ce5ebf004c372e41cac7e249bd1b898878005de6 Mon Sep 17 00:00:00 2001 From: Intizar Tashov Date: Wed, 28 Aug 2024 14:40:04 +0900 Subject: [PATCH] encapsulate SubmitDelegatedFallbackDirect func (#2244) * encapsulate SubmitDelegatedFallbackDirect func * fix getting new nonce in delegated and direct funcs * dont get new nonce in the last iteration --- node/pkg/chain/helper/helper.go | 79 +++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/node/pkg/chain/helper/helper.go b/node/pkg/chain/helper/helper.go index 757f2bd60..b658a2854 100644 --- a/node/pkg/chain/helper/helper.go +++ b/node/pkg/chain/helper/helper.go @@ -243,47 +243,69 @@ func (t *ChainHelper) PublicAddressString() (string, error) { } func (t *ChainHelper) SubmitDelegatedFallbackDirect(ctx context.Context, contractAddress string, functionString string, maxRetrial int, args ...interface{}) error { + if t.delegatorUrl != "" { + err, tryDirect := t.submitDelegated(ctx, contractAddress, functionString, maxRetrial, args...) + if !tryDirect { + return err + } + } + + return t.submitDirect(ctx, contractAddress, functionString, maxRetrial, args...) +} + +func (t *ChainHelper) submitDelegated(ctx context.Context, contractAddress string, functionString string, maxRetrial int, args ...interface{}) (error, bool) { var err error var tx *types.Transaction - clientIndex := 0 nonce, err := noncemanager.GetAndIncrementNonce(t.wallet) if err != nil { - return err + return err, false } - if t.delegatorUrl != "" { - for i := 0; i < maxRetrial; i++ { - tx, err = utils.MakeFeeDelegatedTx(ctx, t.clients[clientIndex], contractAddress, t.wallet, functionString, t.chainID, nonce, args...) - if err != nil { - if utils.ShouldRetryWithSwitchedJsonRPC(err) { - clientIndex = (clientIndex + 1) % len(t.clients) - } - continue + for i := 0; i < maxRetrial; i++ { + tx, err = utils.MakeFeeDelegatedTx(ctx, t.clients[clientIndex], contractAddress, t.wallet, functionString, t.chainID, nonce, args...) + if err != nil { + if utils.ShouldRetryWithSwitchedJsonRPC(err) { + clientIndex = (clientIndex + 1) % len(t.clients) } + continue + } - tx, err = t.GetSignedFromDelegator(tx) - if err != nil { - break // if delegator signing fails, try direct transaction - } + tx, err = t.GetSignedFromDelegator(tx) + if err != nil { + return nil, true // if delegator signing fails, try direct transaction + } - err = utils.SubmitRawTx(ctx, t.clients[clientIndex], tx) - if err != nil { - if utils.ShouldRetryWithSwitchedJsonRPC(err) { - clientIndex = (clientIndex + 1) % len(t.clients) - } 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 { - nonce, err = noncemanager.GetAndIncrementNonce(t.wallet) - if err != nil { - return err - } + err = utils.SubmitRawTx(ctx, t.clients[clientIndex], tx) + if err != nil { + if utils.ShouldRetryWithSwitchedJsonRPC(err) { + clientIndex = (clientIndex + 1) % len(t.clients) + } else if errors.Is(err, errorSentinel.ErrChainTransactionFail) { + return err, false // if transaction fails, the data will probably be too old to retry + } else if i == maxRetrial-1 { + return err, true // dont get new nonce if it's the last iteration, instead try direct transaction + } else if utils.IsNonceError(err) || err == context.DeadlineExceeded { + nonce, err = noncemanager.GetAndIncrementNonce(t.wallet) + if err != nil { + return err, false } - continue } - return nil + continue } + return nil, false + } + return err, true +} + +func (t *ChainHelper) submitDirect(ctx context.Context, contractAddress string, functionString string, maxRetrial int, args ...interface{}) error { + var err error + var tx *types.Transaction + clientIndex := 0 + + nonce, err := noncemanager.GetAndIncrementNonce(t.wallet) + if err != nil { + return err } for i := 0; i < maxRetrial; i++ { @@ -301,6 +323,8 @@ func (t *ChainHelper) SubmitDelegatedFallbackDirect(ctx context.Context, contrac clientIndex = (clientIndex + 1) % len(t.clients) } else if errors.Is(err, errorSentinel.ErrChainTransactionFail) { return err // if transaction fails, the data will probably be too old to retry + } else if i == maxRetrial-1 { + return err // dont get new nonce if it's the last iteration } else if utils.IsNonceError(err) || err == context.DeadlineExceeded { nonce, err = noncemanager.GetAndIncrementNonce(t.wallet) if err != nil { @@ -311,7 +335,6 @@ func (t *ChainHelper) SubmitDelegatedFallbackDirect(ctx context.Context, contrac } return nil } - return err }