-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockdiscovery.go
71 lines (54 loc) · 2.08 KB
/
mockdiscovery.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"github.com/aiguo186/fabric-sdk-go-gm/pkg/common/providers/fab"
)
/**
* Mock Discovery Provider is used to mock peers on the network
*/
// MockStaticDiscoveryProvider implements mock discovery provider
type MockStaticDiscoveryProvider struct {
Error error
Peers []fab.Peer
customDiscoveryService fab.DiscoveryService
}
// MockStaticDiscoveryService implements mock discovery service
type MockStaticDiscoveryService struct {
Error error
Peers []fab.Peer
}
// NewMockDiscoveryProvider returns mock discovery provider
func NewMockDiscoveryProvider(err error, peers []fab.Peer) *MockStaticDiscoveryProvider {
return &MockStaticDiscoveryProvider{Error: err, Peers: peers}
}
// CreateLocalDiscoveryService return local discovery service
func (dp *MockStaticDiscoveryProvider) CreateLocalDiscoveryService(mspID string) (fab.DiscoveryService, error) {
if dp.customDiscoveryService != nil {
return dp.customDiscoveryService, nil
}
return NewMockDiscoveryService(dp.Error, dp.Peers...), nil
}
//SetCustomDiscoveryService sets custom discoveryService
func (dp *MockStaticDiscoveryProvider) SetCustomDiscoveryService(customDiscoveryService fab.DiscoveryService) {
dp.customDiscoveryService = customDiscoveryService
}
//NewMockDiscoveryService returns a new MockStaticDiscoveryService
func NewMockDiscoveryService(err error, peers ...fab.Peer) *MockStaticDiscoveryService {
return &MockStaticDiscoveryService{Error: err, Peers: peers}
}
// GetPeers is used to discover eligible peers for chaincode
func (ds *MockStaticDiscoveryService) GetPeers() ([]fab.Peer, error) {
if ds.Error != nil {
return nil, ds.Error
}
if ds.Peers == nil {
mockPeer := MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP"}
peers := make([]fab.Peer, 0)
peers = append(peers, &mockPeer)
ds.Peers = peers
}
return ds.Peers, nil
}