Skip to content

Commit

Permalink
Fix for query executor when fetching the transaction hash
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandros Filios <[email protected]>
  • Loading branch information
alexandrosfilios committed Sep 2, 2024
1 parent a53473c commit 611b62d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
51 changes: 45 additions & 6 deletions token/services/network/orion/statusfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ package orion

import (
"encoding/base64"
"time"

errors2 "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/orion"
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/orion/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/keys"
"github.com/hyperledger-labs/orion-sdk-go/pkg/bcdb"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)

var runner = db.NewRetryRunner(3, 1*time.Second, true)

type StatusFetcher struct {
dbManager *DBManager
}
Expand All @@ -34,18 +38,37 @@ func (r *StatusFetcher) FetchStatus(network, namespace string, txID driver.TxID)
}

// fetch token request reference
qe, err := oSession.QueryExecutor(namespace)
if err != nil {
return nil, errors.Wrapf(err, "failed to get query executor [%s] for orion network [%s]", txID, network)
}
key, err := keys.CreateTokenRequestKey(txID)
if err != nil {
return nil, errors.Errorf("can't create for token request '%s'", txID)
}
trRef, err := qe.Get(orionKey(key))

reqKey := orionKey(key)
tx, err := oSession.DataTx(txID)
if err != nil {
return nil, errors.Wrapf(err, "data tx does not exist for [%s]", txID)
}
trRef, err := r.fetch(func() ([]byte, error) { return tx.Get(namespace, reqKey) }, code)
if err == nil {
logger.Infof("Found with DataTx(txID) [%s]", txID)
}

if err != nil {
var qe *orion.SessionQueryExecutor
qe, err = oSession.QueryExecutor(namespace)
if err != nil {
return nil, errors.Wrapf(err, "failed to get query executor")
}
trRef, err = r.fetch(func() ([]byte, error) { return qe.Get(reqKey) }, code)
if err == nil {
logger.Infof("Found with query executor: [%s]", txID)
}
}

if err != nil {
return nil, errors.Wrapf(err, "failed to get token request reference [%s] for orion network [%s]", txID, network)
return nil, errors.Wrapf(err, "could not find status for [%s]", txID)
}

if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("retrieved token request hash for [%s][%s]:[%s]", key, txID, base64.StdEncoding.EncodeToString(trRef))
}
Expand All @@ -55,6 +78,22 @@ func (r *StatusFetcher) FetchStatus(network, namespace string, txID driver.TxID)
}, nil
}

func (r *StatusFetcher) fetch(f func() ([]byte, error), code driver2.ValidationCode) ([]byte, error) {
var trRef []byte
err := runner.Run(func() error {
h, err := f()
if err != nil {
return errors.Wrapf(err, "data doesn't exist")
}
if code == driver2.Valid && len(h) == 0 {
return errors.New("hash not found for valid transaction")
}
trRef = h
return nil
})
return trRef, err
}

func (r *StatusFetcher) FetchCode(network string, txID driver.TxID) (driver2.ValidationCode, error) {
_, code, err := r.fetchCode(network, txID)
return code, err
Expand Down
9 changes: 7 additions & 2 deletions token/services/network/orion/txstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package orion

import (
errors2 "errors"

view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view"
session2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/session"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
Expand Down Expand Up @@ -48,7 +50,7 @@ func (r *RequestTxStatusView) Call(context view.Context) (interface{}, error) {
if err != nil {
return nil, errors.Wrapf(err, "failed to get session to custodian [%s]", custodian)
}
logger.Debugf("request tx status for [%s]", r.TxID)
logger.Infof("request tx status for [%s]", r.TxID)

// TODO: Should we sign the txStatus request?
request := &TxStatusRequest{
Expand All @@ -65,7 +67,7 @@ func (r *RequestTxStatusView) Call(context view.Context) (interface{}, error) {
if err := session.Receive(response); err != nil {
return nil, errors.Wrapf(err, "failed to receive response from custodian [%s]", custodian)
}
logger.Debugf("got tx status response for [%s]: [%d]", r.TxID, response.Status)
logger.Infof("got tx status response for [%s]: [%d]", r.TxID, response.Status)
return response, nil
}

Expand All @@ -89,6 +91,9 @@ func (r *RequestTxStatusResponderView) Call(context view.Context) (interface{},
span.AddEvent("process_tx_status_request")
response, err := r.process(context, request)
if err != nil {
if err2 := session.SendError(err.Error()); err2 != nil {
return nil, errors.Wrapf(errors2.Join(err, err2), "failed to process request")
}
return nil, errors.Wrapf(err, "failed to process request")
}
if response.Status == driver.Valid && len(response.TokenRequestReference) == 0 {
Expand Down

0 comments on commit 611b62d

Please sign in to comment.