forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockselection.go
72 lines (59 loc) · 1.8 KB
/
mockselection.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
64
65
66
67
68
69
70
71
72
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
selectopts "github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"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/mocks"
)
// MockSelectionService implements mock selection service
type MockSelectionService struct {
Error error
Peers []fab.Peer
ChannelContext context.Channel
}
// NewMockSelectionService returns mock selection service
func NewMockSelectionService(err error, peers ...fab.Peer) *MockSelectionService {
return &MockSelectionService{Error: err, Peers: peers}
}
// GetEndorsersForChaincode mockcore retrieving endorsing peers
func (ds *MockSelectionService) GetEndorsersForChaincode(chaincodes []*fab.ChaincodeCall, opts ...options.Opt) ([]fab.Peer, error) {
if ds.Error != nil {
return nil, ds.Error
}
params := selectopts.NewParams(opts)
var peers []fab.Peer
if ds.ChannelContext != nil {
var err error
discovery, err := ds.ChannelContext.ChannelService().Discovery()
if err != nil {
return nil, err
}
peers, err = discovery.GetPeers()
if err != nil {
return nil, err
}
} else if ds.Peers == nil {
mockPeer := mocks.NewMockPeer("Peer1", "http://peer1.com")
peers = append(peers, mockPeer)
}
if params.PeerFilter != nil {
for _, p := range ds.Peers {
if params.PeerFilter(p) {
peers = append(peers, p)
}
}
} else {
peers = ds.Peers
}
if params.PeerSorter != nil {
sortedPeers := make([]fab.Peer, len(peers))
copy(sortedPeers, peers)
peers = params.PeerSorter(sortedPeers)
}
return peers, nil
}