Skip to content

Commit

Permalink
[FAB-8321] Resource Mgmt: Query Instantiated CCs
Browse files Browse the repository at this point in the history
Change-Id: I61375d3fc1f4aa1edc9fb595a83748171ebcecca
Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
  • Loading branch information
sandrask committed Mar 2, 2018
1 parent 6b39e3a commit 1d43fc8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
8 changes: 8 additions & 0 deletions pkg/client/resmgmt/opts.go
Expand Up @@ -20,6 +20,14 @@ func WithTargets(targets ...fab.Peer) RequestOption {
}
}

//WithTarget encapsulates fab.Peer target to RequestOption
func WithTarget(target fab.Peer) RequestOption {
return func(opts *Opts) error {
opts.Targets = []fab.Peer{target}
return nil
}
}

//WithTargetFilter encapsulates resmgmtclient TargetFilter targets to resmgmtclient RequestOption
func WithTargetFilter(targetFilter TargetFilter) RequestOption {
return func(opts *Opts) error {
Expand Down
64 changes: 57 additions & 7 deletions pkg/client/resmgmt/resmgmt.go
Expand Up @@ -9,10 +9,12 @@ package resmgmt

import (
"io/ioutil"
"math/rand"
"time"

config "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/channel"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"

"github.com/hyperledger/fabric-sdk-go/pkg/context"
Expand Down Expand Up @@ -413,6 +415,54 @@ func (rc *Client) QueryInstalledChaincodes(proposalProcessor fab.ProposalProcess
return rc.resource.QueryInstalledChaincodes(proposalProcessor)
}

// QueryInstantiatedChaincodes queries the instantiated chaincodes on a peer for specific channel.
// Valid option is WithTarget. If not specified it will query any peer on this channel
func (rc *Client) QueryInstantiatedChaincodes(channelID string, options ...RequestOption) (*pb.ChaincodeQueryResponse, error) {

opts, err := rc.prepareRequestOpts(options...)
if err != nil {
return nil, err
}

ctx := &fabContext{
ProviderContext: rc.provider,
IdentityContext: rc.identity,
}

var target fab.ProposalProcessor
if len(opts.Targets) >= 1 {
target = opts.Targets[0]
} else {
// discover peers on this channel
discovery, err := rc.discoveryProvider.NewDiscoveryService(channelID)
if err != nil {
return nil, errors.WithMessage(err, "failed to create channel discovery service")
}
// default filter will be applied (if any)
targets, err := rc.getDefaultTargets(discovery)
if err != nil {
return nil, errors.WithMessage(err, "failed to get default target for query instantiated chaincodes")
}

// select random channel peer
r := rand.New(rand.NewSource(time.Now().Unix()))
randomNumber := r.Intn(len(targets))
target = targets[randomNumber]
}

l, err := channel.NewLedger(ctx, channelID)
if err != nil {
return nil, err
}

responses, err := l.QueryInstantiatedChaincodes([]fab.ProposalProcessor{target})
if err != nil {
return nil, err
}

return responses[0], nil
}

// QueryChannels queries the names of all the channels that a peer has joined.
// Returns the details of all channels that peer has joined.
func (rc *Client) QueryChannels(proposalProcessor fab.ProposalProcessor) (*pb.ChannelQueryResponse, error) {
Expand Down Expand Up @@ -579,7 +629,7 @@ func peersToTxnProcessors(peers []fab.Peer) []fab.ProposalProcessor {
// SaveChannel creates or updates channel
func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption) error {

opts, err := rc.prepareSaveChannelOpts(options...)
opts, err := rc.prepareRequestOpts(options...)
if err != nil {
return err
}
Expand Down Expand Up @@ -658,14 +708,14 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
return nil
}

//prepareSaveChannelOpts Reads chmgmt.Opts from chmgmt.Option array
func (rc *Client) prepareSaveChannelOpts(options ...RequestOption) (Opts, error) {
saveChannelOpts := Opts{}
//prepareRequestOpts prepares rrequest options
func (rc *Client) prepareRequestOpts(options ...RequestOption) (Opts, error) {
opts := Opts{}
for _, option := range options {
err := option(&saveChannelOpts)
err := option(&opts)
if err != nil {
return saveChannelOpts, errors.WithMessage(err, "Failed to read save channel opts")
return opts, errors.WithMessage(err, "Failed to read opts")
}
}
return saveChannelOpts, nil
return opts, nil
}
17 changes: 17 additions & 0 deletions test/integration/orgs/multiple_orgs_test.go
Expand Up @@ -134,6 +134,23 @@ func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK) int {
// Load specific targets for move funds test
loadOrgPeers(t, sdk)

// Verify that example CC is instantiated on Org1 peer
chaincodeQueryResponse, err := org1ResMgmt.QueryInstantiatedChaincodes("orgchannel")
if err != nil {
t.Fatalf("QueryInstantiatedChaincodes return error: %v", err)
}

found := false
for _, chaincode := range chaincodeQueryResponse.Chaincodes {
if chaincode.Name == "exampleCC" {
found = true
}
}

if !found {
t.Fatalf("QueryInstantiatedChaincodes failed to find instantiated exampleCC chaincode")
}

// Org1 user connects to 'orgchannel'
chClientOrg1User, err := sdk.NewClient(fabsdk.WithUser("User1"), fabsdk.WithOrg(org1)).Channel("orgchannel")
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions test/integration/sdk/resmgmt_queries_test.go
Expand Up @@ -55,9 +55,30 @@ func TestResMgmtClientQueries(t *testing.T) {

testInstalledChaincodes(t, ccID, target, client)

testInstantiatedChaincodes(t, testSetup.ChannelID, ccID, target, client)

testQueryChannels(t, testSetup.ChannelID, target, client)

}
func testInstantiatedChaincodes(t *testing.T, channelID string, ccID string, target fab.ProposalProcessor, client *resmgmt.Client) {

chaincodeQueryResponse, err := client.QueryInstantiatedChaincodes(channelID, resmgmt.WithTarget(target.(fab.Peer)))
if err != nil {
t.Fatalf("QueryInstantiatedChaincodes return error: %v", err)
}

found := false
for _, chaincode := range chaincodeQueryResponse.Chaincodes {
t.Logf("**InstantiatedCC: %s", chaincode)
if chaincode.Name == ccID {
found = true
}
}

if !found {
t.Fatalf("QueryInstantiatedChaincodes failed to find instantiated %s chaincode", ccID)
}
}

func testInstalledChaincodes(t *testing.T, ccID string, target fab.ProposalProcessor, client *resmgmt.Client) {

Expand Down

0 comments on commit 1d43fc8

Please sign in to comment.