Skip to content

Commit

Permalink
Merge pull request #1682 from 0chain/fix/send-tokens
Browse files Browse the repository at this point in the history
Fix/send tokens response
  • Loading branch information
dabasov authored Nov 19, 2024
2 parents c6580ee + 00deaa8 commit eed527d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
8 changes: 7 additions & 1 deletion core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,10 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,

sys.Sleep(querySleepTime)

var confirmationResponse string

for retries < cfg.MaxTxnQuery {
t, err = VerifyTransaction(txn.Hash)
t, confirmationResponse, err = VerifyTransactionWithRes(txn.Hash)
if err == nil {
break
}
Expand All @@ -657,6 +659,10 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,
return t.Hash, t.TransactionOutput, t.TransactionNonce, t, errors.New("", t.TransactionOutput)
}

if t.TransactionType == TxnTypeSend {
t.TransactionOutput = confirmationResponse
}

return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/transaction/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestOptimisticVerificationLearning(t *testing.T) {
EthereumNode: "",
})
ov := NewOptimisticVerifier([]string{"https://dev.zus.network/sharder01", "https://dev.zus.network/sharder02"})
_, err := ov.VerifyTransactionOptimistic("a20360964c067b319d52b5cad71d771b0e1d2a80e76001da73009899b09ffa31")
_, _, err := ov.VerifyTransactionOptimistic("a20360964c067b319d52b5cad71d771b0e1d2a80e76001da73009899b09ffa31")

assert.NoError(t, err)

Expand Down
54 changes: 38 additions & 16 deletions core/transaction/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ func NewOptimisticVerifier(sharders []string) *OptimisticVerifier {
}
}

func (v *OptimisticVerifier) VerifyTransactionOptimistic(txnHash string) (*Transaction, error) {
func (v *OptimisticVerifier) VerifyTransactionOptimistic(txnHash string) (*Transaction, string, error) {
cfg, err := conf.GetClientConfig()
if err != nil {

return nil, err
return nil, "", err
}

//refresh sharders
Expand All @@ -60,7 +59,7 @@ func (v *OptimisticVerifier) VerifyTransactionOptimistic(txnHash string) (*Trans
//amount of sharders to query
minNumConfirmation := int(math.Ceil(float64(cfg.MinConfirmation*len(v.sharders)) / 100))
if minNumConfirmation > len(v.sharders) {
return nil, errors.New("verify_optimistic", "wrong number of min_confirmations")
return nil, "", errors.New("verify_optimistic", "wrong number of min_confirmations")
}
shuffled := util.Shuffle(v.sharders)[:minNumConfirmation]

Expand All @@ -76,6 +75,7 @@ func (v *OptimisticVerifier) VerifyTransactionOptimistic(txnHash string) (*Trans
var url string
var chain []*RoundBlockHeader
var txn *Transaction
var confirmationResponse string
r := resty.New(v.options...).Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error {
if err != nil { //network issue
return err
Expand Down Expand Up @@ -107,6 +107,15 @@ func (v *OptimisticVerifier) VerifyTransactionOptimistic(txnHash string) (*Trans
return err
}

// set objmap to confirmationResponse using json marshal
confirmationResponseByte, err := json.Marshal(map[string]map[string]json.RawMessage{
"confirmation": objmap,
})
if err != nil {
return err
}
confirmationResponse = string(confirmationResponseByte)

b := &RoundBlockHeader{}
err = json.Unmarshal(respBody, b)
if err != nil {
Expand Down Expand Up @@ -143,7 +152,7 @@ L:
}

if len(chain) == 0 {
return nil, errors.Newf("verify", "can't get confirmation after %v retries", retriesCount)
return nil, "", errors.Newf("verify", "can't get confirmation after %v retries", retriesCount)
}

//remove current sharder from the list to avoid building chain with it
Expand All @@ -157,10 +166,10 @@ L:

err = v.checkConfirmation(chain)
if err != nil {
return nil, err
return nil, "", err
}

return txn, err
return txn, confirmationResponse, err
}

func (v *OptimisticVerifier) checkConfirmation(chain []*RoundBlockHeader) error {
Expand Down Expand Up @@ -281,18 +290,23 @@ func validateBlockHash(b *RoundBlockHeader) error {
return nil
}

// VerifyTransaction query transaction status from sharders, and verify it by mininal confirmation
func VerifyTransaction(txnHash string) (*Transaction, error) {
txn, _, err := VerifyTransactionWithRes(txnHash)
return txn, err
}

// VerifyTransaction query transaction status from sharders, and verify it by mininal confirmation
func VerifyTransactionWithRes(txnHash string) (*Transaction, string, error) {
nodeClient, err := client.GetNode()
if err != nil {
return nil, err
return nil, "", err
}

sharders := nodeClient.Sharders().Healthy()

cfg, err := conf.GetClientConfig()
if err != nil {
return nil, err
return nil, "", err
}

if cfg.VerifyOptimistic {
Expand All @@ -304,18 +318,18 @@ func VerifyTransaction(txnHash string) (*Transaction, error) {
}

// VerifyTransaction query transaction status from sharders, and verify it by mininal confirmation
func VerifyTransactionTrusted(txnHash string, sharders []string) (*Transaction, error) {
func VerifyTransactionTrusted(txnHash string, sharders []string) (*Transaction, string, error) {

cfg, err := conf.GetClientConfig()
if err != nil {

return nil, err
return nil, "", err
}

numSharders := len(sharders)

if numSharders == 0 {
return nil, ErrNoAvailableSharder
return nil, "", ErrNoAvailableSharder
}

minNumConfirmation := int(math.Ceil(float64(cfg.MinConfirmation*numSharders) / 100))
Expand All @@ -338,6 +352,7 @@ func VerifyTransactionTrusted(txnHash string, sharders []string) (*Transaction,
numSuccess := 0

var retTxn *Transaction
var confirmationResponse string

//leave first item for ErrTooLessConfirmation
var msgList = make([]string, 1, numSharders)
Expand Down Expand Up @@ -393,6 +408,13 @@ func VerifyTransactionTrusted(txnHash string, sharders []string) (*Transaction,
}
if len(txn.Signature) > 0 {
retTxn = txn
confirmationResponseByte, err := json.Marshal(map[string]map[string]json.RawMessage{
"confirmation": objmap,
})
if err != nil {
return err
}
confirmationResponse = string(confirmationResponseByte)
}
numSuccess++

Expand Down Expand Up @@ -432,12 +454,12 @@ func VerifyTransactionTrusted(txnHash string, sharders []string) (*Transaction,

if numSuccess > 0 && numSuccess >= minNumConfirmation {
if retTxn == nil {
return nil, errors.Throw(ErrNoTxnDetail, strings.Join(msgList, "\r\n"))
return nil, "", errors.Throw(ErrNoTxnDetail, strings.Join(msgList, "\r\n"))
}
return retTxn, nil
return retTxn, confirmationResponse, nil
}

msgList[0] = fmt.Sprintf("min_confirmation is %v%%, but got %v/%v sharders", cfg.MinConfirmation, numSuccess, numSharders)
return nil, errors.Throw(ErrTooLessConfirmation, strings.Join(msgList, "\r\n"))
return nil, confirmationResponse, errors.Throw(ErrTooLessConfirmation, strings.Join(msgList, "\r\n"))

}
5 changes: 2 additions & 3 deletions wasmsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,9 @@ func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, erro
// - fee is the transaction fee
// - desc is the description of the transaction
func send(toClientID string, tokens uint64, fee uint64, desc string) (string, error) {
hash, _, _, _, err := zcncore.Send(toClientID, tokens, desc)
_, _, _, txn, err := zcncore.Send(toClientID, tokens, desc)
if err != nil {
return "", err
}

return hash, nil
return txn.TransactionOutput, nil
}

0 comments on commit eed527d

Please sign in to comment.