Skip to content

Commit

Permalink
Add "resolve" argument to readAsset and search txs
Browse files Browse the repository at this point in the history
  • Loading branch information
bandreghetti committed Jan 21, 2021
1 parent 2ea69d5 commit e8dea62
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
29 changes: 24 additions & 5 deletions transactions/readAsset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,40 @@ var ReadAsset = Transaction{
DataType: "@key",
Required: true,
},
{
Tag: "resolve",
Description: "Resolve references recursively.",
DataType: "boolean",
},
},
ReadOnly: true,
Routine: func(stub shim.ChaincodeStubInterface, req map[string]interface{}) ([]byte, errors.ICCError) {
var assetJSON []byte
var err error

// This is safe to do because validation is done before calling routine
key := req["key"].(assets.Key)

asset, err := key.GetRecursive(stub)
if err != nil {
return nil, errors.WrapError(err, "failed to read asset from blockchain")
resolve, ok := req["resolve"].(bool)

if ok && resolve {
var asset *assets.Asset
asset, err = key.GetRecursive(stub)
if err != nil {
return nil, errors.WrapError(err, "failed to read asset from blockchain")
}

assetJSON, err = json.Marshal(*asset)
if err != nil {
return nil, errors.WrapErrorWithStatus(err, "failed to serialize asset", 500)
}
} else {
assetJSON, err = key.GetBytes(stub)
if err != nil {
return nil, errors.WrapError(err, "failed to get asset state")
}
}

assetJSON, err := json.Marshal(*asset)

return assetJSON, nil
},
}
19 changes: 19 additions & 0 deletions transactions/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transactions
import (
"encoding/json"

"github.com/goledgerdev/cc-tools/assets"
"github.com/goledgerdev/cc-tools/errors"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
Expand All @@ -27,6 +28,11 @@ var Search = Transaction{
Description: "Name of the private collection to be searched.",
DataType: "string",
},
{
Tag: "resolve",
Description: "Resolve references recursively.",
DataType: "boolean",
},
},
ReadOnly: true,
Routine: func(stub shim.ChaincodeStubInterface, req map[string]interface{}) ([]byte, errors.ICCError) {
Expand Down Expand Up @@ -121,6 +127,19 @@ var Search = Transaction{
return nil, errors.WrapErrorWithStatus(err, "failed to unmarshal queryResponse values", 500)
}

resolve, ok := req["resolve"].(bool)
if ok && resolve {
key, err := assets.NewKey(data)
if err != nil {
return nil, errors.WrapError(err, "failed to create key object to resolve result")
}
asset, err := key.GetRecursive(stub)
if err != nil {
return nil, errors.WrapError(err, "failed to resolve result")
}
data = *asset
}

searchResult = append(searchResult, data)
}

Expand Down

0 comments on commit e8dea62

Please sign in to comment.