Skip to content

Commit

Permalink
[FAB-5209] Remove dependency on channel from resource
Browse files Browse the repository at this point in the history
Change-Id: I3f74819f2a0ef24393efb6b27a4a2a04a2fe1dbb
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Feb 5, 2018
1 parent 42d6b16 commit 4c64194
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
55 changes: 43 additions & 12 deletions pkg/fabric-client/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
fcutils "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/util"
ccomm "github.com/hyperledger/fabric-sdk-go/pkg/config/comm"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/txn"
"github.com/hyperledger/fabric-sdk-go/pkg/logging"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -244,7 +243,11 @@ func (c *Resource) QueryChannels(peer fab.Peer) (*pb.ChannelQueryResponse, error
return nil, errors.New("peer required")
}

payload, err := c.queryBySystemChaincodeByTarget("cscc", "GetChannels", [][]byte{}, peer)
request := fab.ChaincodeInvokeRequest{
ChaincodeID: "cscc",
Fcn: "GetChannels",
}
payload, err := c.queryByChaincode(request, peer)
if err != nil {
return nil, errors.WithMessage(err, "cscc.GetChannels failed")
}
Expand All @@ -264,10 +267,16 @@ func (c *Resource) QueryInstalledChaincodes(peer fab.Peer) (*pb.ChaincodeQueryRe
if peer == nil {
return nil, errors.New("peer required")
}
payload, err := c.queryBySystemChaincodeByTarget("lscc", "getinstalledchaincodes", [][]byte{}, peer)

request := fab.ChaincodeInvokeRequest{
ChaincodeID: "lscc",
Fcn: "getinstalledchaincodes",
}
payload, err := c.queryByChaincode(request, peer)
if err != nil {
return nil, errors.WithMessage(err, "lscc.getinstalledchaincodes failed")
}

response := new(pb.ChaincodeQueryResponse)
err = proto.Unmarshal(payload, response)
if err != nil {
Expand Down Expand Up @@ -340,24 +349,46 @@ func (c *Resource) InstallChaincode(req fab.InstallChaincodeRequest) ([]*fab.Tra
return transactionProposalResponse, txID, err
}

func (c *Resource) queryBySystemChaincodeByTarget(chaincodeID string, fcn string, args [][]byte, target fab.ProposalProcessor) ([]byte, error) {
func (c *Resource) queryByChaincode(request fab.ChaincodeInvokeRequest, target fab.ProposalProcessor) ([]byte, error) {
const systemChannel = ""

targets := []fab.ProposalProcessor{target}
request := fab.ChaincodeInvokeRequest{
ChaincodeID: chaincodeID,
Fcn: fcn,
Args: args,
Targets: targets,

tp, err := txn.NewProposal(c.clientContext, systemChannel, request)
if err != nil {
return nil, errors.WithMessage(err, "NewProposal failed")
}
responses, err := channel.QueryBySystemChaincode(request, c.clientContext)

tpr, err := txn.SendProposal(tp, targets)
if err != nil {
return nil, errors.WithMessage(err, "QueryBySystemChaincode failed")
return nil, errors.WithMessage(err, "SendProposal failed")
}

responses, err := filterProposalResponses(tpr)
if err != nil {
return nil, errors.WithMessage(err, "response filtering failed")
}
// we are only querying one peer hence one result
if len(responses) != 1 {
return nil, errors.Errorf("QueryBySystemChaincode should have one result only - actual result count: %d", len(responses))
return nil, errors.Errorf("should have one result, but actual result count is: %d", len(responses))
}

return responses[0], nil
}

func filterProposalResponses(tpr []*fab.TransactionProposalResponse) ([][]byte, error) {
var responses [][]byte
errMsg := ""
for _, response := range tpr {
if response.Err != nil {
errMsg = errMsg + response.Err.Error() + "\n"
} else {
responses = append(responses, response.ProposalResponse.GetResponse().Payload)
}
}

if len(errMsg) > 0 {
return responses, errors.New(errMsg)
}
return responses, nil
}
21 changes: 21 additions & 0 deletions pkg/fabric-client/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package resource

import (
"bytes"
"fmt"
"io/ioutil"
"path"
Expand Down Expand Up @@ -80,3 +81,23 @@ func setupTestClient() *Resource {
ctx := mocks.NewMockContext(user)
return New(ctx)
}

func TestQueryBySystemChaincode(t *testing.T) {
c := setupTestClient()

peer := mocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}, MockCert: nil, Payload: []byte("A")}

request := fab.ChaincodeInvokeRequest{
ChaincodeID: "cc",
Fcn: "Hello",
}
resp, err := c.queryByChaincode(request, &peer)
if err != nil {
t.Fatalf("Failed to query: %s", err)
}
expectedResp := []byte("A")

if !bytes.Equal(resp, expectedResp) {
t.Fatalf("Unexpected transaction proposal response: %v (expected %v)", resp, expectedResp)
}
}

0 comments on commit 4c64194

Please sign in to comment.