Skip to content

Commit

Permalink
[FAB-10649] Fix local integration tests for v1.2
Browse files Browse the repository at this point in the history
The Fabric Selection service now retrieves the URL of the
peer from endpoint config before applying filters.
Also, various integration tests needed to be changed in
order to work with local config/entity matchers.

Change-Id: I62ca6ad9e526d94e46ad09963ae29430d59f7569
Signed-off-by: Bob Stasyszyn <[email protected]>
  • Loading branch information
bstasyszyn committed Jun 14, 2018
1 parent a906355 commit 88455b4
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *Service) GetEndorsersForChaincode(chaincodes []*fab.ChaincodeCall, opts

params := options.NewParams(opts)

endpoints, err := chResponse.Endorsers(asInvocationChain(chaincodes), newSelector(params.PrioritySelector), newFilter(params.PeerFilter, peers))
endpoints, err := chResponse.Endorsers(asInvocationChain(chaincodes), newSelector(s.ctx, params.PrioritySelector), newFilter(s.ctx, params.PeerFilter, peers))
if err != nil {
return nil, errors.Wrap(err, "error getting endorsers from channel response")
}
Expand Down
46 changes: 26 additions & 20 deletions pkg/client/common/selection/fabricselection/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,13 @@ var (
peer1Org3 = mocks.NewMockPeer("p31", peer1Org3URL)
peer2Org3 = mocks.NewMockPeer("p32", peer2Org3URL)

peerConfigOrg1 = fab.NetworkPeer{
PeerConfig: fab.PeerConfig{
URL: peer1Org1URL,
},
MSPID: mspID1,
}
peerConfigOrg2 = fab.NetworkPeer{
PeerConfig: fab.PeerConfig{
URL: peer1Org2URL,
},
MSPID: mspID2,
}
channelPeers = []fab.ChannelPeer{
{
NetworkPeer: peerConfigOrg1,
},
{
NetworkPeer: peerConfigOrg2,
},
{NetworkPeer: newPeerConfig(peer1Org1URL, mspID1)},
{NetworkPeer: newPeerConfig(peer2Org1URL, mspID1)},
{NetworkPeer: newPeerConfig(peer1Org2URL, mspID2)},
{NetworkPeer: newPeerConfig(peer2Org2URL, mspID2)},
{NetworkPeer: newPeerConfig(peer1Org3URL, mspID3)},
{NetworkPeer: newPeerConfig(peer2Org3URL, mspID3)},
}

peer1Org1Endpoint = &discmocks.MockDiscoveryPeerEndpoint{
Expand Down Expand Up @@ -191,10 +179,10 @@ func TestSelection(t *testing.T) {
options.WithPrioritySelector(func(peer1, peer2 fab.Peer) int {
// Return peers in alphabetical order
if peer1.URL() < peer2.URL() {
return 1
return -1
}
if peer1.URL() > peer2.URL() {
return -1
return 1
}
return 0
}),
Expand Down Expand Up @@ -280,3 +268,21 @@ func (c *config) ChannelPeers(name string) ([]fab.ChannelPeer, bool) {
}
return c.peers, true
}

func (c *config) PeerConfig(nameOrURL string) (*fab.PeerConfig, bool) {
for _, peer := range c.peers {
if peer.URL == nameOrURL {
return &peer.NetworkPeer.PeerConfig, true
}
}
return nil, false
}

func newPeerConfig(url, mspID string) fab.NetworkPeer {
return fab.NetworkPeer{
PeerConfig: fab.PeerConfig{
URL: url,
},
MSPID: mspID,
}
}
27 changes: 20 additions & 7 deletions pkg/client/common/selection/fabricselection/selectionfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ import (

discclient "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/discovery/client"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/options"
contextAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)

type selectionFilter struct {
ctx contextAPI.Client
peers []fab.Peer
filter options.PeerFilter
}

func newFilter(filter options.PeerFilter, peers []fab.Peer) *selectionFilter {
func newFilter(ctx contextAPI.Client, filter options.PeerFilter, peers []fab.Peer) *selectionFilter {
return &selectionFilter{
ctx: ctx,
peers: peers,
filter: filter,
}
Expand All @@ -29,7 +32,7 @@ func newFilter(filter options.PeerFilter, peers []fab.Peer) *selectionFilter {
func (s *selectionFilter) Exclude(endpoint discclient.Peer) bool {
logger.Debugf("Calling peer filter on endpoint [%s]", endpoint.AliveMessage.GetAliveMsg().Membership.Endpoint)

peer := asPeerValue(&endpoint)
peer := asPeerValue(s.ctx, &endpoint)

// The peer must be included in the set of peers returned from fab.DiscoveryService.
// (Note that DiscoveryService may return a filtered set of peers, depending on how the
Expand All @@ -49,25 +52,35 @@ func (s *selectionFilter) Exclude(endpoint discclient.Peer) bool {
}

type prioritySelector struct {
ctx contextAPI.Client
selector options.PrioritySelector
}

func newSelector(selector options.PrioritySelector) discclient.PrioritySelector {
func newSelector(ctx contextAPI.Client, selector options.PrioritySelector) discclient.PrioritySelector {
if selector != nil {
return &prioritySelector{selector: selector}
return &prioritySelector{ctx: ctx, selector: selector}
}
return discclient.PrioritiesByHeight
}

func (s *prioritySelector) Compare(endpoint1, endpoint2 discclient.Peer) discclient.Priority {
logger.Debugf("Calling priority selector on endpoint1 [%s] and endpoint2 [%s]", endpoint1.AliveMessage.GetAliveMsg().Membership.Endpoint, endpoint2.AliveMessage.GetAliveMsg().Membership.Endpoint)
return discclient.Priority(s.selector(asPeerValue(&endpoint1), asPeerValue(&endpoint2)))
return discclient.Priority(s.selector(asPeerValue(s.ctx, &endpoint1), asPeerValue(s.ctx, &endpoint2)))
}

// asPeerValue converts the discovery endpoint into a light-weight peer value (i.e. without the GRPC config)
// so that it may used by a peer filter
func asPeerValue(endpoint *discclient.Peer) fab.Peer {
url := endpoint.AliveMessage.GetAliveMsg().Membership.Endpoint
func asPeerValue(ctx contextAPI.Client, endpoint *discclient.Peer) fab.Peer {
url := endpoint.AliveMessage.GetAliveMsg().GetMembership().Endpoint

// Get the mapped URL of the peer
peerConfig, found := ctx.EndpointConfig().PeerConfig(url)
if found {
url = peerConfig.URL
} else {
logger.Warnf("Peer config not found for url [%s]", url)
}

return &peerEndpointValue{
mspID: endpoint.MSPID,
url: url,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

entityMatchers:
peer:
- pattern: (\w+).org1.example.com:(\d+)
urlSubstitutionExp: localhost:$2
sslTargetOverrideUrlSubstitutionExp: $1.org1.example.com
mappedHost: local.peer0.org1.example.com

- pattern: peer0.org2.example.com:8051
urlSubstitutionExp: localhost:8051
sslTargetOverrideUrlSubstitutionExp: peer0.org2.example.com
mappedHost: local.peer0.org2.example.com

orderer:
- pattern: (\w+).example.(\w+)
urlSubstitutionExp: localhost:7050
sslTargetOverrideUrlSubstitutionExp: orderer.example.com
mappedHost: local.orderer.example.com

- pattern: (\w+).example.(\w+):(\d+)
urlSubstitutionExp: localhost:7050
sslTargetOverrideUrlSubstitutionExp: orderer.example.com
mappedHost: local.orderer.example.com

certificateAuthority:
- pattern: (\w+).org1.example.(\w+)
urlSubstitutionExp: https://localhost:7054
mappedHost: local.ca.org1.example.com

- pattern: (\w+).org2.example.(\w+)
urlSubstitutionExp: https://localhost:8054
mappedHost: local.ca.org2.example.com
183 changes: 183 additions & 0 deletions test/fixtures/config/overrides/local_orderers_peers_ca_bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
#
# The network connection profile provides client applications the information about the target
# blockchain network that are necessary for the applications to interact with it. These are all
# knowledge that must be acquired from out-of-band sources. This file provides such a source.
#

#
# list of participating organizations in this network
#
organizations:
org1:
mspid: Org1MSP

# This org's MSP store (absolute path or relative to client.cryptoconfig)
cryptoPath: peerOrganizations/org1.example.com/users/{username}@org1.example.com/msp

peers:
- local.peer0.org1.example.com

org2:
mspid: Org2MSP

# This org's MSP store (absolute path or relative to client.cryptoconfig)
cryptoPath: peerOrganizations/org2.example.com/users/{username}@org2.example.com/msp

peers:
- local.peer0.org2.example.com

# Orderer Org name
ordererorg:
# Membership Service Provider ID for this organization
mspID: "OrdererOrg"

# Needed to load users crypto keys and certs for this org (absolute path or relative to global crypto path, DEV mode)
cryptoPath: ordererOrganizations/example.com/users/{username}@example.com/msp


channels:
# multi-org test channel
orgchannel:

# anchor peers only for the bootstrap config is required, other org's peers will be discovered
peers:
local.peer0.org1.example.com:
endorsingPeer: true
chaincodeQuery: true
ledgerQuery: true
eventSource: true

local.peer0.org2.example.com:
endorsingPeer: true
chaincodeQuery: true
ledgerQuery: true
eventSource: true

policies:
queryChannelConfig:
minResponses: 1
maxTargets: 1
retryOpts:
attempts: 5
initialBackoff: 500ms
maxBackoff: 5s
backoffFactor: 2.0

#
# List of orderers to send transaction and channel create/update requests to. For the time
# being only one orderer is needed. If more than one is defined, which one get used by the
# SDK is implementation specific. Consult each SDK's documentation for its handling of orderers.
#
orderers:
local.orderer.example.com:
url: orderer.example.com:7050

# these are standard properties defined by the gRPC library
# they will be passed in as-is to gRPC client constructor
grpcOptions:
ssl-target-name-override: orderer.example.com
# These parameters should be set in coordination with the keepalive policy on the server,
# as incompatible settings can result in closing of connection.
# When duration of the 'keep-alive-time' is set to 0 or less the keep alive client parameters are disabled
keep-alive-time: 0s
keep-alive-timeout: 20s
keep-alive-permit: false
fail-fast: false
# allow-insecure will be taken into consideration if address has no protocol defined, if true then grpc or else grpcs
allow-insecure: false

tlsCACerts:
# Certificate location absolute path
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem

#
# List of peers to send various requests to, including endorsement, query
# and event listener registration.
#
peers:
local.peer0.org1.example.com:
# this URL is used to send endorsement and query requests
url: localhost:7051

grpcOptions:
ssl-target-name-override: peer0.org1.example.com
# These parameters should be set in coordination with the keepalive policy on the server,
# as incompatible settings can result in closing of connection.
# When duration of the 'keep-alive-time' is set to 0 or less the keep alive client parameters are disabled
keep-alive-time: 0s
keep-alive-timeout: 20s
keep-alive-permit: false
fail-fast: false
# allow-insecure will be taken into consideration if address has no protocol defined, if true then grpc or else grpcs
allow-insecure: false

tlsCACerts:
# Certificate location absolute path
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem

local.peer0.org2.example.com:
url: localhost:8051
grpcOptions:
ssl-target-name-override: peer0.org2.example.com
# These parameters should be set in coordination with the keepalive policy on the server,
# as incompatible settings can result in closing of connection.
# When duration of the 'keep-alive-time' is set to 0 or less the keep alive client parameters are disabled
keep-alive-time: 0s
keep-alive-timeout: 20s
keep-alive-permit: false
fail-fast: false
# allow-insecure will be taken into consideration if address has no protocol defined, if true then grpc or else grpcs
allow-insecure: false

tlsCACerts:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem

#
# Fabric-CA is a special kind of Certificate Authority provided by Hyperledger Fabric which allows
# certificate management to be done via REST APIs. Application may choose to use a standard
# Certificate Authority instead of Fabric-CA, in which case this section would not be specified.
#
certificateAuthorities:
local.ca.org1.example.com:
url: https://ca.org1.example.com:7054
tlsCACerts:
# Comma-Separated list of paths
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabricca/tls/certs/ca_root.pem
# Client key and cert for SSL handshake with Fabric CA
client:
key:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabricca/tls/certs/client/client_fabric_client-key.pem
cert:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabricca/tls/certs/client/client_fabric_client.pem

# Fabric-CA supports dynamic user enrollment via REST APIs. A "root" user, a.k.a registrar, is
# needed to enroll and invoke new users.
registrar:
enrollId: admin
enrollSecret: adminpw
# [Optional] The optional name of the CA.
caName: ca.org1.example.com
local.ca.org2.example.com:
url: https://ca.org2.example.com:8054
tlsCACerts:
# Comma-Separated list of paths
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabricca/tls/certs/ca_root.pem
# Client key and cert for SSL handshake with Fabric CA
client:
key:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabricca/tls/certs/client/client_fabric_client-key.pem
cert:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabricca/tls/certs/client/client_fabric_client.pem

# Fabric-CA supports dynamic user enrollment via REST APIs. A "root" user, a.k.a registrar, is
# needed to enroll and invoke new users.
registrar:
enrollId: admin
enrollSecret: adminpw
# [Optional] The optional name of the CA.
caName: ca.org2.example.com
Loading

0 comments on commit 88455b4

Please sign in to comment.