forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
staticselection.go
63 lines (51 loc) · 1.65 KB
/
staticselection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package staticselection
import (
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
copts "github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)
const loggerModule = "fabsdk/client"
var logger = logging.NewLogger(loggerModule)
// SelectionService implements static selection service
type SelectionService struct {
discoveryService fab.DiscoveryService
}
// NewService creates a static selection service
func NewService(discovery fab.DiscoveryService) (fab.SelectionService, error) {
return &SelectionService{discoveryService: discovery}, nil
}
// GetEndorsersForChaincode returns a set of endorsing peers
func (s *SelectionService) GetEndorsersForChaincode(chaincodes []*fab.ChaincodeCall, opts ...copts.Opt) ([]fab.Peer, error) {
params := options.NewParams(opts)
channelPeers, err := s.discoveryService.GetPeers()
if err != nil {
logger.Errorf("Error retrieving peers from discovery service: %s", err)
return nil, nil
}
// Apply peer filter if provided
if params.PeerFilter != nil {
var peers []fab.Peer
for _, peer := range channelPeers {
if params.PeerFilter(peer) {
peers = append(peers, peer)
}
}
channelPeers = peers
}
if logging.IsEnabledFor(loggerModule, logging.DEBUG) {
str := ""
for i, peer := range channelPeers {
str += peer.URL()
if i+1 < len(channelPeers) {
str += ","
}
}
logger.Debugf("Available peers: [%s]", str)
}
return channelPeers, nil
}