Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encapsulate SubmitDelegatedFallbackDirect func #2244

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions node/pkg/chain/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,48 +243,63 @@ func (t *ChainHelper) PublicAddressString() (string, error) {
}

func (t *ChainHelper) SubmitDelegatedFallbackDirect(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
}

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
}
err, tryDirect := t.submitDelegated(ctx, contractAddress, functionString, maxRetrial, nonce, args...)
if !tryDirect {
return err
}
}

tx, err = t.GetSignedFromDelegator(tx)
if err != nil {
break // if delegator signing fails, try direct transaction
return t.submitDirect(ctx, contractAddress, functionString, maxRetrial, nonce, args...)
}

func (t *ChainHelper) submitDelegated(ctx context.Context, contractAddress string, functionString string, maxRetrial int, nonce uint64, args ...interface{}) (error, bool) {
var err error
var tx *types.Transaction
clientIndex := 0

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 {
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 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, nonce uint64, args ...interface{}) error {
var err error
var tx *types.Transaction
clientIndex := 0

for i := 0; i < maxRetrial; i++ {
tx, err = utils.MakeDirectTx(ctx, t.clients[clientIndex], contractAddress, t.wallet, functionString, t.chainID, nonce, args...)
Expand All @@ -311,7 +326,6 @@ func (t *ChainHelper) SubmitDelegatedFallbackDirect(ctx context.Context, contrac
}
return nil
}

return err
}

Expand Down