Skip to content

Commit

Permalink
Fix/retry tx on nonce error (#1322)
Browse files Browse the repository at this point in the history
* Revert "experiment"

This reverts commit 050270f.

* Revert "experiment"

This reverts commit d4821f8.

* Revert "experiment"

This reverts commit 9f65b2e.

* Revert "experiment"

This reverts commit 6313cb8.

* Revert "experiment"

This reverts commit b6be0b5.

* Revert "experiment"

This reverts commit 68d4190.

* Revert "experiment"

This reverts commit 3a23f3f.

* Revert "experiment"

This reverts commit 6b6578b.

* Revert "experiment"

This reverts commit 65a0152.

* Revert "experiment"

This reverts commit 6834b05.

* Revert "experiment"

This reverts commit 8446894.

* Revert "hotfix"

This reverts commit 48cf793.

* Revert "hotfix"

This reverts commit 895c7f0.

* Revert "added logging"

This reverts commit fafce3c.

* Revert "added logging"

This reverts commit 10b6a4a.

* retry failed transaction in zbox with nonce error
  • Loading branch information
dabasov authored Dec 11, 2023
1 parent 9b9b8e0 commit aa9db0a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
25 changes: 21 additions & 4 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,39 @@ func (t *Transaction) VerifyTransaction(verifyHandler VerifyFunc) (bool, error)
func SendTransactionSync(txn *Transaction, miners []string) error {
wg := sync.WaitGroup{}
wg.Add(len(miners))

failureCount := 0
fails := make(chan error, len(miners))

for _, miner := range miners {
url := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL)
go func() {
_, err := sendTransactionToURL(url, txn, &wg)
if err != nil {
failureCount++
fails <- err
}
}() //nolint
}
wg.Wait()
close(fails)

failureCount := 0
messages := make(map[string]int)
for e := range fails {
if e != nil {
failureCount++
messages[e.Error()] += 1
}
}

max := 0
dominant := ""
for m, s := range messages {
if s > max {
dominant = m
}
}

if failureCount == len(miners) {
return errors.New("transaction_send_error", "failed to send transaction to all miners")
return errors.New("transaction_send_error", dominant)
}

return nil
Expand Down
25 changes: 18 additions & 7 deletions zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"net/http"
"strconv"
"strings"
"time"

"github.com/0chain/common/core/currency"
Expand Down Expand Up @@ -223,7 +224,7 @@ func ReadPoolLock(tokens, fee uint64) (hash string, nonce int64, err error) {
Name: transaction.STORAGESC_READ_POOL_LOCK,
InputArgs: nil,
}
hash, _, nonce, _, err = smartContractTxnValueFee(sn, tokens, fee)
hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, tokens, fee)
return
}

Expand All @@ -237,7 +238,7 @@ func ReadPoolUnlock(fee uint64) (hash string, nonce int64, err error) {
Name: transaction.STORAGESC_READ_POOL_UNLOCK,
InputArgs: nil,
}
hash, _, nonce, _, err = smartContractTxnValueFee(sn, 0, fee)
hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, 0, fee)
return
}

Expand Down Expand Up @@ -379,7 +380,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint
Name: transaction.STORAGESC_STAKE_POOL_LOCK,
InputArgs: &spr,
}
hash, _, nonce, _, err = smartContractTxnValueFee(sn, value, fee)
hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, value, fee)
return
}

Expand Down Expand Up @@ -422,7 +423,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (
}

var out string
if _, out, nonce, _, err = smartContractTxnValueFee(sn, 0, fee); err != nil {
if _, out, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, 0, fee); err != nil {
return // an error
}

Expand Down Expand Up @@ -455,7 +456,7 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64
Name: transaction.STORAGESC_WRITE_POOL_LOCK,
InputArgs: &req,
}
hash, _, nonce, _, err = smartContractTxnValueFee(sn, tokens, fee)
hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, tokens, fee)
return
}

Expand All @@ -476,7 +477,7 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err
Name: transaction.STORAGESC_WRITE_POOL_UNLOCK,
InputArgs: &req,
}
hash, _, nonce, _, err = smartContractTxnValueFee(sn, 0, fee)
hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, 0, fee)
return
}

Expand Down Expand Up @@ -1415,7 +1416,17 @@ func smartContractTxnValue(sn transaction.SmartContractTxnData, value uint64) (
hash, out string, nonce int64, txn *transaction.Transaction, err error) {

// Fee is set during sdk initialization.
return smartContractTxnValueFee(sn, value, client.TxnFee())
return smartContractTxnValueFeeWithRetry(sn, value, client.TxnFee())
}

func smartContractTxnValueFeeWithRetry(sn transaction.SmartContractTxnData,
value, fee uint64) (hash, out string, nonce int64, t *transaction.Transaction, err error) {
hash, out, nonce, t, err = smartContractTxnValueFee(sn, value, fee)

if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") {
return smartContractTxnValueFee(sn, value, fee)
}
return
}

func smartContractTxnValueFee(sn transaction.SmartContractTxnData,
Expand Down

0 comments on commit aa9db0a

Please sign in to comment.