Skip to content

Commit

Permalink
Merge "[FAB-9312] Resolve metalinter warnings"
Browse files Browse the repository at this point in the history
  • Loading branch information
fqutishat authored and Gerrit Code Review committed Apr 3, 2018
2 parents 1796d39 + fbae5b7 commit 3ddebb2
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 266 deletions.
9 changes: 4 additions & 5 deletions pkg/client/channel/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ type Request struct {

//Response contains response parameters for query and execute an invocation transaction
type Response struct {
Payload []byte
Proposal *fab.TransactionProposal
Responses []*fab.TransactionProposalResponse
TransactionID fab.TransactionID
TxValidationCode pb.TxValidationCode
Proposal *fab.TransactionProposal
// ChaincodeStatus is the status returned by Chaincode
ChaincodeStatus int32
Responses []*fab.TransactionProposalResponse
ChaincodeStatus int32
Payload []byte
}

//WithTargets encapsulates ProposalProcessors to Option
Expand Down
5 changes: 1 addition & 4 deletions pkg/client/channel/chclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/greylist"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/pkg/errors"
)

var logger = logging.NewLogger("fabsdk/client")

// Client enables access to a channel on a Fabric network.
//
// A channel client instance provides a handler to interact with peers on specified channel.
Expand Down Expand Up @@ -133,7 +130,7 @@ func (cc *Client) InvokeHandler(handler invoke.Handler, request Request, options

complete := make(chan bool)
go func() {
invoker.Invoke(
_, _ = invoker.Invoke(
func() (interface{}, error) {
handler.Handle(requestContext, clientContext)
return nil, requestContext.Error
Expand Down
9 changes: 4 additions & 5 deletions pkg/client/channel/invoke/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ type Request struct {

//Response contains response parameters for query and execute transaction
type Response struct {
Payload []byte
Proposal *fab.TransactionProposal
Responses []*fab.TransactionProposalResponse
TransactionID fab.TransactionID
TxValidationCode pb.TxValidationCode
Proposal *fab.TransactionProposal
// ChaincodeStatus is the status returned by Chaincode
ChaincodeStatus int32
Responses []*fab.TransactionProposalResponse
ChaincodeStatus int32
Payload []byte
}

//Handler for chaining transaction executions
Expand Down
38 changes: 28 additions & 10 deletions pkg/client/channel/invoke/txnhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestResponseValidation(t *testing.T) {
assert.EqualValues(t, int32(status.EndorsementMismatch), s.Code, "expected endorsement mismatch")
}

func TestProposalProcessorHandler(t *testing.T) {
func TestProposalProcessorHandlerError(t *testing.T) {
peer1 := fcmocks.NewMockPeer("p1", "peer1:7051")
peer2 := fcmocks.NewMockPeer("p2", "peer2:7051")
discoveryPeers := []fab.Peer{peer1, peer2}
Expand All @@ -204,29 +204,47 @@ func TestProposalProcessorHandler(t *testing.T) {
if requestContext.Error == nil || !strings.Contains(requestContext.Error.Error(), selectionErr.Error()) {
t.Fatal("Expected error: ", selectionErr, ", Received error:", requestContext.Error)
}
}

func TestProposalProcessorHandlerPassDirectly(t *testing.T) {
peer1 := fcmocks.NewMockPeer("p1", "peer1:7051")
peer2 := fcmocks.NewMockPeer("p2", "peer2:7051")
discoveryPeers := []fab.Peer{peer1, peer2}

//Get query handler
handler := NewProposalProcessorHandler()

requestContext = prepareRequestContext(request, Opts{}, t)
request := Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("query"), []byte("b")}}
// Directly pass in the proposal processors. In this case it should use those directly
requestContext := prepareRequestContext(request, Opts{Targets: []fab.Peer{peer2}}, t)
handler.Handle(requestContext, setupChannelClientContext(nil, nil, discoveryPeers, t))
if requestContext.Error != nil {
t.Fatalf("Got error: %s", requestContext.Error)
}
if len(requestContext.Opts.Targets) != len(discoveryPeers) {
t.Fatalf("Expecting %d proposal processors but got %d", len(discoveryPeers), len(requestContext.Opts.Targets))
if len(requestContext.Opts.Targets) != 1 {
t.Fatalf("Expecting 1 proposal processor but got %d", len(requestContext.Opts.Targets))
}
if requestContext.Opts.Targets[0] != peer1 || requestContext.Opts.Targets[1] != peer2 {
if requestContext.Opts.Targets[0] != peer2 {
t.Fatalf("Didn't get expected peers")
}
}

// Directly pass in the proposal processors. In this case it should use those directly
requestContext = prepareRequestContext(request, Opts{Targets: []fab.Peer{peer2}}, t)
func TestProposalProcessorHandler(t *testing.T) {
peer1 := fcmocks.NewMockPeer("p1", "peer1:7051")
peer2 := fcmocks.NewMockPeer("p2", "peer2:7051")
discoveryPeers := []fab.Peer{peer1, peer2}

handler := NewProposalProcessorHandler()
request := Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("query"), []byte("b")}}
requestContext := prepareRequestContext(request, Opts{}, t)
handler.Handle(requestContext, setupChannelClientContext(nil, nil, discoveryPeers, t))
if requestContext.Error != nil {
t.Fatalf("Got error: %s", requestContext.Error)
}
if len(requestContext.Opts.Targets) != 1 {
t.Fatalf("Expecting 1 proposal processor but got %d", len(requestContext.Opts.Targets))
if len(requestContext.Opts.Targets) != len(discoveryPeers) {
t.Fatalf("Expecting %d proposal processors but got %d", len(discoveryPeers), len(requestContext.Opts.Targets))
}
if requestContext.Opts.Targets[0] != peer2 {
if requestContext.Opts.Targets[0] != peer1 || requestContext.Opts.Targets[1] != peer2 {
t.Fatalf("Didn't get expected peers")
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/client/common/discovery/discoveryfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func TestDiscoveryFilter(t *testing.T) {
t.Fatalf(err.Error())
}

_, config, _, err := config.FromBackend(configBackend)()
_, config1, _, err := config.FromBackend(configBackend)()
if err != nil {
t.Fatalf(err.Error())
}

peerCreator := defPeerCreator{config: config}
discoveryProvider, err := staticdiscovery.New(config, &peerCreator)
peerCreator := defPeerCreator{defPeerConfig: config1}
discoveryProvider, err := staticdiscovery.New(config1, &peerCreator)
if err != nil {
t.Fatalf("Failed to setup discovery provider: %s", err)
}
Expand Down Expand Up @@ -70,9 +70,9 @@ func TestDiscoveryFilter(t *testing.T) {
}

type defPeerCreator struct {
config fab.EndpointConfig
defPeerConfig fab.EndpointConfig
}

func (pc *defPeerCreator) CreatePeerFromConfig(peerCfg *fab.NetworkPeer) (fab.Peer, error) {
return peer.New(pc.config, peer.FromPeerConfig(peerCfg))
return peer.New(pc.defPeerConfig, peer.FromPeerConfig(peerCfg))
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func TestStaticDiscovery(t *testing.T) {
t.Fatalf(err.Error())
}

_, config, _, err := config.FromBackend(configBackend)()
_, config1, _, err := config.FromBackend(configBackend)()
if err != nil {
t.Fatalf(err.Error())
}

peerCreator := defPeerCreator{config: config}
discoveryProvider, err := New(config, &peerCreator)
peerCreator := defPeerCreator{config: config1}
discoveryProvider, err := New(config1, &peerCreator)
if err != nil {
t.Fatalf("Failed to setup discovery provider: %s", err)
}
Expand All @@ -53,23 +53,37 @@ func TestStaticDiscovery(t *testing.T) {
t.Fatalf("Expecting %d, got %d peers", expectedNumOfPeeers, len(peers))
}

}

func TestStaticDiscoveryWhenChannelIsEmpty(t *testing.T) {
configBackend, err := config.FromFile("../../../../../test/fixtures/config/config_test.yaml")()
if err != nil {
t.Fatalf(err.Error())
}

_, config1, _, err := config.FromBackend(configBackend)()
if err != nil {
t.Fatalf(err.Error())
}

peerCreator := defPeerCreator{config: config1}
discoveryProvider, _ := New(config1, &peerCreator)
// If channel is empty discovery service will return all configured network peers
discoveryService, err = discoveryProvider.CreateDiscoveryService("")
discoveryService, err := discoveryProvider.CreateDiscoveryService("")
if err != nil {
t.Fatalf("Failed to setup discovery service: %s", err)
}

peers, err = discoveryService.GetPeers()
peers, err := discoveryService.GetPeers()
if err != nil {
t.Fatalf("Failed to get peers from discovery service: %s", err)
}

// Two peers are configured at network level
expectedNumOfPeeers = 2
expectedNumOfPeeers := 2
if len(peers) != expectedNumOfPeeers {
t.Fatalf("Expecting %d, got %d peers", expectedNumOfPeeers, len(peers))
}

}

type defPeerCreator struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@ func TestCCPolicyProvider(t *testing.T) {
t.Fatal("Failed to create context")
}

// All good
ccPolicyProvider, err := newCCPolicyProvider(context, "mychannel", "User1", "Org1")
if err != nil {
t.Fatalf("Failed to setup cc policy provider: %s", err)
}

// Empty chaincode ID
_, err = ccPolicyProvider.GetChaincodePolicy("")
if err == nil {
t.Fatalf("Should have failed to retrieve chaincode policy for empty chaincode id")
}

// Non-existent chaincode ID
_, err = ccPolicyProvider.GetChaincodePolicy("abc")
if err == nil {
t.Fatalf("Should have failed to retrieve non-existent cc policy")
}

}

func TestCCPolicyProviderNegative(t *testing.T) {
// Create SDK setup for channel client with dynamic selection
sdk, err := fabsdk.New(config.FromFile("../../../../../test/fixtures/config/config_test.yaml"))
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}
defer sdk.Close()

clientContext := sdk.Context(fabsdk.WithUser("User1"), fabsdk.WithOrg("Org1"))

context, err := clientContext()
if err != nil {
t.Fatal("Failed to create context")
}
// Nil sdk
_, err = newCCPolicyProvider(nil, "mychannel", "User1", "Org1")
if err == nil {
Expand Down Expand Up @@ -59,23 +93,6 @@ func TestCCPolicyProvider(t *testing.T) {
t.Fatalf("Should have failed for invalid channel name")
}

// All good
ccPolicyProvider, err := newCCPolicyProvider(context, "mychannel", "User1", "Org1")
if err != nil {
t.Fatalf("Failed to setup cc policy provider: %s", err)
}

// Empty chaincode ID
_, err = ccPolicyProvider.GetChaincodePolicy("")
if err == nil {
t.Fatalf("Should have failed to retrieve chaincode policy for empty chaincode id")
}

// Non-existent chaincode ID
_, err = ccPolicyProvider.GetChaincodePolicy("abc")
if err == nil {
t.Fatalf("Should have failed to retrieve non-existent cc policy")
}
}

func TestBadClient(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,7 @@ func (c *signaturePolicyCompiler) compile(sigPolicy *common.SignaturePolicy, ide

switch t := sigPolicy.Type.(type) {
case *common.SignaturePolicy_SignedBy:
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
mspID, err := mspPrincipalToString(identities[t.SignedBy])
if err != nil {
return nil, errors.WithMessage(err, "error getting MSP ID from MSP principal")
}
return NewGroupOfGroups([]Group{NewMSPPeerGroup(mspID, peerRetriever)}), nil
}, nil

return signaturePolicySignedBy(t, identities)
case *common.SignaturePolicy_NOutOf_:
nOutOfPolicy := t.NOutOf
var pfuncs []GroupRetriever
Expand Down Expand Up @@ -231,6 +224,16 @@ func (c *signaturePolicyCompiler) compile(sigPolicy *common.SignaturePolicy, ide
}
}

func signaturePolicySignedBy(t *common.SignaturePolicy_SignedBy, identities []*mb.MSPPrincipal) (GroupRetriever, error) {
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
mspID, err := mspPrincipalToString(identities[t.SignedBy])
if err != nil {
return nil, errors.WithMessage(err, "error getting MSP ID from MSP principal")
}
return NewGroupOfGroups([]Group{NewMSPPeerGroup(mspID, peerRetriever)}), nil
}, nil
}

func mspPrincipalToString(principal *mb.MSPPrincipal) (string, error) {
switch principal.PrincipalClassification {
case mb.MSPPrincipal_ROLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func TestStaticSelection(t *testing.T) {
t.Fatalf(err.Error())
}

_, config, _, err := config.FromBackend(configBackend)()
configProv := config.FromBackend(configBackend)

_, config, _, err := configProv()
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/client/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ SPDX-License-Identifier: Apache-2.0
package event

import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/events/client"
"github.com/pkg/errors"
)

var logger = logging.NewLogger("fabsdk/client")

// Client enables access to a channel events on a Fabric network.
type Client struct {
eventService fab.EventService
Expand All @@ -42,8 +39,8 @@ func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client
eventClient := Client{}

for _, param := range opts {
err := param(&eventClient)
if err != nil {
err1 := param(&eventClient)
if err1 != nil {
return nil, errors.WithMessage(err, "option failed")
}
}
Expand Down
Loading

0 comments on commit 3ddebb2

Please sign in to comment.