-
Notifications
You must be signed in to change notification settings - Fork 20
/
local.go
123 lines (100 loc) · 3.33 KB
/
local.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package discovery
import (
"context"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric/common/policydsl"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/s7techlab/hlf-sdk-go/api"
"github.com/s7techlab/hlf-sdk-go/api/config"
)
// implementation of api.DiscoveryProvider interface
var _ api.DiscoveryProvider = (*LocalConfigProvider)(nil)
type LocalConfigProvider struct {
tlsMapper connectionMapper
channels []config.DiscoveryChannel `yaml:"channels"`
}
type opts struct {
Channels []config.DiscoveryChannel `yaml:"channels"`
}
func NewLocalConfigProvider(options config.DiscoveryConfigOpts, tlsMapper connectionMapper) (api.DiscoveryProvider, error) {
var opts opts
if err := mapstructure.Decode(options, &opts); err != nil {
return nil, fmt.Errorf(`decode params: %w`, err)
}
return &LocalConfigProvider{channels: opts.Channels, tlsMapper: tlsMapper}, nil
}
func (d *LocalConfigProvider) Chaincode(_ context.Context, channelName, ccName string) (api.ChaincodeDiscoverer, error) {
var channelFoundFlag bool
for _, ch := range d.channels {
if ch.Name == channelName {
channelFoundFlag = true
for _, cc := range ch.Chaincodes {
if cc.Name == ccName {
// TODO from where to get endorsers
// no endorsers in local cfg
// no peers
// endorsers := []*api.HostEndpoint{}
ccDTO := newChaincodeDTO(cc.Name, cc.Version, channelName)
for i := range ch.Orderers {
mspID := "" // TODO we have no MSPID from local cfg
ccDTO.addEndpointToOrderers(mspID, ch.Orderers[i].Host)
}
msps, err := getMSPsFromPolicy(cc.Policy)
if err != nil {
return nil, err
}
for i := range msps {
mspID := msps[i]
hostAddr := "" // no addr in channel config, peer must be already in pool
ccDTO.addEndpointToEndorsers(mspID, hostAddr)
}
return newChaincodeDiscovererTLSDecorator(ccDTO, d.tlsMapper), nil
}
}
}
}
if channelFoundFlag {
return nil, ErrNoChaincodes
}
return nil, ErrChannelNotFound
}
func (d *LocalConfigProvider) Channel(_ context.Context, channelName string) (api.ChannelDiscoverer, error) {
var channelFoundFlag bool
for _, ch := range d.channels {
if ch.Name == channelName {
channelFoundFlag = true
chanDTO := newChannelDTO(channelName)
for i := range ch.Orderers {
mspID := "" // TODO we have no MSPID from local cfg
chanDTO.addEndpointToOrderers(mspID, ch.Orderers[i].Host)
}
return newChannelDiscovererTLSDecorator(chanDTO, d.tlsMapper), nil
}
}
if channelFoundFlag {
return nil, ErrNoChaincodes
}
return nil, ErrChannelNotFound
}
func (d *LocalConfigProvider) LocalPeers(_ context.Context) (api.LocalPeersDiscoverer, error) {
return nil, fmt.Errorf("LocalPeers for LocalConfigProvider not implemented")
}
func getMSPsFromPolicy(policy string) ([]string, error) {
policyEnvelope, err := policydsl.FromString(policy)
if err != nil {
return nil, errors.Wrap(err, `failed to parse policy`)
}
mspIds := make([]string, 0)
for _, id := range policyEnvelope.Identities {
var mspIdentity msp.SerializedIdentity
if err = proto.Unmarshal(id.Principal, &mspIdentity); err != nil {
return nil, errors.Wrap(err, `failed to get MSP identity`)
} else {
mspIds = append(mspIds, mspIdentity.Mspid)
}
}
return mspIds, nil
}