Skip to content

Commit

Permalink
Merge pull request #1648 from 0chain/fix/verifyTxn
Browse files Browse the repository at this point in the history
Fix/verify txn
  • Loading branch information
dabasov authored Oct 23, 2024
2 parents d5bab14 + 8d5ec2f commit 4d99ba6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 63 deletions.
4 changes: 4 additions & 0 deletions core/client/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"github.com/0chain/gosdk/core/logger"
"sync"
)

Expand Down Expand Up @@ -38,6 +39,9 @@ func (nc *NonceCache) GetNextNonce(clientId string) int64 {
}

nc.cache[clientId] += 1

logger.Log.Info("GetNextNonce", "clientId", clientId, "nonce", nc.cache[clientId])

return nc.cache[clientId]
}

Expand Down
76 changes: 40 additions & 36 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,29 +486,29 @@ func GetFeesTable(miners []string, reqPercent ...float32) (map[string]map[string
return nil, errors.New("failed to get fees table", strings.Join(errs, ","))
}

func SmartContractTxn(scAddress string, sn SmartContractTxnData, clients ...string) (
func SmartContractTxn(scAddress string, sn SmartContractTxnData, verifyTxn bool, clients ...string) (
hash, out string, nonce int64, txn *Transaction, err error) {
return SmartContractTxnValue(scAddress, sn, 0, clients...)
return SmartContractTxnValue(scAddress, sn, 0, verifyTxn, clients...)
}

func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64, clients ...string) (
func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64, verifyTxn bool, clients ...string) (
hash, out string, nonce int64, txn *Transaction, err error) {

return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee(), clients...)
return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee(), verifyTxn, clients...)
}

func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData,
value, fee uint64, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) {
hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee, clients...)
value, fee uint64, verifyTxn bool, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) {
hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee, verifyTxn, clients...)

if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") {
return SmartContractTxnValueFee(scAddress, sn, value, fee, clients...)
if err != nil && (strings.Contains(err.Error(), "invalid transaction nonce") || strings.Contains(err.Error(), "invalid future transaction")) {
return SmartContractTxnValueFee(scAddress, sn, value, fee, verifyTxn, clients...)
}
return
}

func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,
value, fee uint64, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) {
value, fee uint64, verifyTxn bool, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) {

clientId := client.Id()
if len(clients) > 0 && clients[0] != "" {
Expand Down Expand Up @@ -577,40 +577,44 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,
return
}

var (
querySleepTime = time.Duration(cfg.QuerySleepTime) * time.Second
retries = 0
)
if verifyTxn {
var (
querySleepTime = time.Duration(cfg.QuerySleepTime) * time.Second
retries = 0
)

sys.Sleep(querySleepTime)
sys.Sleep(querySleepTime)

for retries < cfg.MaxTxnQuery {
t, err = VerifyTransaction(txn.Hash)
if err == nil {
break
for retries < cfg.MaxTxnQuery {
t, err = VerifyTransaction(txn.Hash)
if err == nil {
break
}
retries++
sys.Sleep(querySleepTime)
}
retries++
sys.Sleep(querySleepTime)
}

if err != nil {
Logger.Error("Error verifying the transaction", err.Error(), txn.Hash)
client.Cache.Evict(txn.ClientID)
return
}
if err != nil {
Logger.Error("Error verifying the transaction", err.Error(), txn.Hash)
client.Cache.Evict(txn.ClientID)
return
}

if t == nil {
return "", "", 0, txn, errors.New("transaction_validation_failed",
"Failed to get the transaction confirmation")
}
if t == nil {
return "", "", 0, txn, errors.New("transaction_validation_failed",
"Failed to get the transaction confirmation")
}

if t.Status == TxnFail {
return t.Hash, t.TransactionOutput, 0, t, errors.New("", t.TransactionOutput)
}
if t.Status == TxnFail {
return t.Hash, t.TransactionOutput, 0, t, errors.New("", t.TransactionOutput)
}

if t.Status == TxnChargeableError {
return t.Hash, t.TransactionOutput, t.TransactionNonce, t, errors.New("", t.TransactionOutput)
}

if t.Status == TxnChargeableError {
return t.Hash, t.TransactionOutput, t.TransactionNonce, t, errors.New("", t.TransactionOutput)
return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil
}

return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil
return txn.Hash, "", txn.TransactionNonce, txn, nil
}
8 changes: 4 additions & 4 deletions zboxcore/sdk/blobber_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint
return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType)
}

hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee)
hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee, true)
return
}

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

var out string
if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee); err != nil {
if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee, true); err != nil {
return // an error
}

Expand Down Expand Up @@ -279,7 +279,7 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64
InputArgs: &req,
}

hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee)
hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee, true)
return
}

Expand All @@ -302,6 +302,6 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err
Name: transaction.STORAGESC_WRITE_POOL_UNLOCK,
InputArgs: &req,
}
hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee)
hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true)
return
}
4 changes: 2 additions & 2 deletions zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ func CollectRewards(providerId string, providerType ProviderType) (string, int64
return "", 0, fmt.Errorf("collect rewards provider type %v not implimented", providerType)
}

hash, _, n, _, err := transaction.SmartContractTxn(scAddress, sn)
hash, _, n, _, err := transaction.SmartContractTxn(scAddress, sn, true)
return hash, n, err
}

Expand Down Expand Up @@ -1227,7 +1227,7 @@ func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uin
hash, out string, nonce int64, txn *transaction.Transaction, err error) {

// Fee is set during sdk initialization.
return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee())
return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee(), true)
}

func CommitToFabric(metaTxnData, fabricConfigJSON string) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions zcnbridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func (b *BridgeClient) MintZCN(payload *zcnsc.MintPayload) (string, error) {
hash, _, _, _, err := coreTransaction.SmartContractTxn(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{
Name: wallet.MintFunc,
InputArgs: payload,
})
}, true)

if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash))
Expand Down Expand Up @@ -663,7 +663,7 @@ func (b *BridgeClient) BurnZCN(amount uint64) (string, string, error) {
hash, out, _, _, err := coreTransaction.SmartContractTxnValue(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{
Name: wallet.BurnFunc,
InputArgs: payload,
}, amount)
}, amount, true)
if err != nil {
Logger.Error("Burn ZCN transaction FAILED", zap.Error(err))
return hash, out, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash))
Expand Down
38 changes: 19 additions & 19 deletions zcncore/execute_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func MinerSCLock(providerId string, providerType Provider, lock uint64, client .
ProviderID: providerId,
ProviderType: providerType,
},
}, lock, client...)
}, lock, true, client...)

}

Expand All @@ -50,7 +50,7 @@ func MinerSCUnlock(providerId string, providerType Provider, client ...string) (
ProviderID: providerId,
ProviderType: providerType,
},
}, client...)
}, true, client...)
}

func MinerSCCollectReward(providerId string, providerType Provider, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
Expand All @@ -60,7 +60,7 @@ func MinerSCCollectReward(providerId string, providerType Provider, client ...st
ProviderId: providerId,
ProviderType: int(providerType),
},
}, client...)
}, true, client...)

}

Expand All @@ -82,7 +82,7 @@ func MinerSCKill(providerId string, providerType Provider, client ...string) (ha
return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{
Name: name,
InputArgs: pr,
}, client...)
}, true, client...)

}

Expand All @@ -93,95 +93,95 @@ func StorageSCCollectReward(providerId string, providerType Provider, client ...
ProviderId: providerId,
ProviderType: int(providerType),
},
}, client...)
}, true, client...)

}

func MinerScUpdateConfig(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.MINERSC_UPDATE_SETTINGS,
InputArgs: input,
}, client...)
}, true, client...)

}

func MinerScUpdateGlobals(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.MINERSC_UPDATE_GLOBALS,
InputArgs: input,
}, client...)
}, true, client...)

}

func StorageScUpdateConfig(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.STORAGESC_UPDATE_SETTINGS,
InputArgs: input,
}, client...)
}, true, client...)

}

func AddHardfork(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.ADD_HARDFORK,
InputArgs: input,
}, client...)
}, true, client...)

}

func ZCNSCUpdateGlobalConfig(input *InputMap, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.ZCNSC_UPDATE_GLOBAL_CONFIG,
InputArgs: input,
}, client...)
}, true, client...)

}

func MinerSCMinerSettings(input *MinerSCMinerInfo, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.MINERSC_MINER_SETTINGS,
InputArgs: input,
}, client...)
}, true, client...)

}

func MinerSCSharderSettings(input *MinerSCMinerInfo, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.MINERSC_SHARDER_SETTINGS,
InputArgs: input,
}, client...)
}, true, client...)

}

func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG,
InputArgs: input,
}, client...)
}, true, client...)

}

func ZCNSCAddAuthorizer(input *AddAuthorizerPayload, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.ZCNSC_ADD_AUTHORIZER,
InputArgs: input,
}, client...)
}, true, client...)

}

func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK,
InputArgs: input,
}, client...)
}, true, client...)

}

func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.ZCNSC_DELETE_AUTHORIZER,
InputArgs: input,
}, client...)
}, true, client...)

}

Expand All @@ -194,7 +194,7 @@ func ZCNSCCollectReward(providerId string, providerType Provider, client ...stri
return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{
Name: transaction.ZCNSC_COLLECT_REWARD,
InputArgs: pr,
}, client...)
}, true, client...)
}

type SendTxnData struct {
Expand All @@ -211,12 +211,12 @@ func Send(toClientID string, tokens uint64, desc string, client ...string) (hash
return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{
Name: "transfer",
InputArgs: SendTxnData{Note: desc},
}, tokens, client...)
}, tokens, true, client...)
}

func Faucet(tokens uint64, input string, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) {
return transaction.SmartContractTxnValue(FaucetSmartContractAddress, transaction.SmartContractTxnData{
Name: "pour",
InputArgs: input,
}, tokens, client...)
}, tokens, true, client...)
}

0 comments on commit 4d99ba6

Please sign in to comment.