/
pgresolverimpl.go
287 lines (237 loc) · 8.06 KB
/
pgresolverimpl.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pgresolver
import (
"fmt"
"reflect"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
common "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
mb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
"github.com/pkg/errors"
)
const loggerModule = "fabsdk/client"
var logger = logging.NewLogger(loggerModule)
// GroupRetriever is a function that returns groups of peers
type GroupRetriever func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error)
type peerGroupResolver struct {
groupRetriever GroupRetriever
lbp LoadBalancePolicy
}
// NewRoundRobinPeerGroupResolver returns a PeerGroupResolver that chooses peers in a round-robin fashion
func NewRoundRobinPeerGroupResolver(sigPolicyEnv *common.SignaturePolicyEnvelope) (PeerGroupResolver, error) {
groupRetriever, err := CompileSignaturePolicy(sigPolicyEnv)
if err != nil {
return nil, errors.WithMessage(err, "error evaluating signature policy")
}
return NewPeerGroupResolver(groupRetriever, NewRoundRobinLBP())
}
// NewRandomPeerGroupResolver returns a PeerGroupResolver that chooses peers in a round-robin fashion
func NewRandomPeerGroupResolver(sigPolicyEnv *common.SignaturePolicyEnvelope) (PeerGroupResolver, error) {
groupRetriever, err := CompileSignaturePolicy(sigPolicyEnv)
if err != nil {
return nil, errors.WithMessage(err, "error evaluating signature policy")
}
return NewPeerGroupResolver(groupRetriever, NewRandomLBP())
}
// NewPeerGroupResolver returns a new PeerGroupResolver
func NewPeerGroupResolver(groupRetriever GroupRetriever, lbp LoadBalancePolicy) (PeerGroupResolver, error) {
return &peerGroupResolver{
groupRetriever: groupRetriever,
lbp: lbp,
}, nil
}
func (c *peerGroupResolver) Resolve(peers []fab.Peer) (PeerGroup, error) {
peerRetriever := func(mspID string) []fab.Peer {
var mspPeers []fab.Peer
for _, peer := range peers {
if mspID == "" || peer.MSPID() == mspID {
mspPeers = append(mspPeers, peer)
}
}
return mspPeers
}
peerGroups, err := c.getPeerGroups(peerRetriever)
if err != nil {
return nil, err
}
if logging.IsEnabledFor(loggerModule, logging.DEBUG) {
var s string
if len(peerGroups) == 0 {
s = "\n\n***** No Available Peer Groups\n"
} else {
s = "\n\n***** Available Peer Groups:\n"
for i, grp := range peerGroups {
s += fmt.Sprintf("%d - %+v", i, grp)
if i+1 < len(peerGroups) {
s += fmt.Sprintf(" OR\n")
}
}
s += fmt.Sprintf("\n")
}
logger.Debugf(s)
}
return c.lbp.Choose(peerGroups), nil
}
func (c *peerGroupResolver) getPeerGroups(peerRetriever MSPPeerRetriever) ([]PeerGroup, error) {
groupHierarchy, err := c.groupRetriever(peerRetriever)
if err != nil {
return nil, err
}
logger.Debugf("***** Policy: %s", groupHierarchy)
mspGroups := groupHierarchy.Reduce()
if logging.IsEnabledFor(loggerModule, logging.DEBUG) {
s := "\n***** Org Groups:\n"
for i, g := range mspGroups {
s += fmt.Sprintf("%+v", g)
if i+1 < len(mspGroups) {
s += fmt.Sprintf(" OR\n")
}
}
s += fmt.Sprintf("\n")
logger.Debugf(s)
}
var allPeerGroups []PeerGroup
for _, g := range mspGroups {
allPeerGroups = append(allPeerGroups, mustGetPeerGroups(g)...)
}
return allPeerGroups, nil
}
func mustGetPeerGroups(group Group) []PeerGroup {
items := group.Items()
if len(items) == 0 {
return nil
}
if _, ok := items[0].(Group); !ok {
group = NewGroup([]Item{group})
}
groups := make([]Group, len(group.Items()))
for i, item := range group.Items() {
if grp, ok := item.(PeerGroup); ok {
groups[i] = grp
} else {
panic(fmt.Sprintf("unexpected: expecting item to be a PeerGroup but found: %s", reflect.TypeOf(item)))
}
}
andedGroups := and(groups)
peerGroups := make([]PeerGroup, len(andedGroups))
for i, g := range andedGroups {
peerGroups[i] = mustGetPeerGroup(g)
}
return peerGroups
}
func mustGetPeerGroup(g Group) PeerGroup {
if pg, ok := g.(PeerGroup); ok {
return pg
}
var peers []fab.Peer
for _, item := range g.Items() {
if pg, ok := item.(fab.Peer); ok {
peers = append(peers, pg)
} else {
panic(fmt.Sprintf("expecting item to be a Peer but found: %s", reflect.TypeOf(item)))
}
}
return NewPeerGroup(peers...)
}
// CompileSignaturePolicy compiles the given signature policy and returns a GroupRetriever
func CompileSignaturePolicy(sigPolicyEnv *common.SignaturePolicyEnvelope) (GroupRetriever, error) {
compiler := &signaturePolicyCompiler{}
return compiler.Compile(sigPolicyEnv)
}
type signaturePolicyCompiler struct {
}
func (c *signaturePolicyCompiler) Compile(sigPolicyEnv *common.SignaturePolicyEnvelope) (GroupRetriever, error) {
policFunc, err := c.compile(sigPolicyEnv.Rule, sigPolicyEnv.Identities)
if err != nil {
return nil, errors.WithMessage(err, "error compiling chaincode signature policy")
}
return policFunc, nil
}
func (c *signaturePolicyCompiler) compile(sigPolicy *common.SignaturePolicy, identities []*mb.MSPPrincipal) (GroupRetriever, error) {
if sigPolicy == nil {
return nil, errors.New("nil signature policy")
}
switch t := sigPolicy.Type.(type) {
case *common.SignaturePolicy_SignedBy:
return signaturePolicySignedBy(t, identities)
case *common.SignaturePolicy_NOutOf_:
return c.signaturePolicyNOutOf(t, identities)
default:
errMsg := fmt.Sprintf("unsupported signature policy type: %v", t)
return nil, errors.New(errMsg)
}
}
func (c *signaturePolicyCompiler) signaturePolicyNOutOf(t *common.SignaturePolicy_NOutOf_, identities []*mb.MSPPrincipal) (GroupRetriever, error) {
nOutOfPolicy := t.NOutOf
if nOutOfPolicy.N == 0 {
return signaturePolicySignedByAny()
}
var pfuncs []GroupRetriever
for _, policy := range nOutOfPolicy.Rules {
f, err := c.compile(policy, identities)
if err != nil {
return nil, err
}
pfuncs = append(pfuncs, f)
}
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
var groups []Group
for _, f := range pfuncs {
grps, err := f(peerRetriever)
if err != nil {
return nil, err
}
groups = append(groups, grps)
}
itemGroups, err := NewGroupOfGroups(groups).Nof(nOutOfPolicy.N)
if err != nil {
return nil, err
}
return itemGroups, nil
}, nil
}
func signaturePolicySignedBy(t *common.SignaturePolicy_SignedBy, identities []*mb.MSPPrincipal) (GroupRetriever, error) {
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
mspID, err := mspPrincipalToString(identities[t.SignedBy])
if err != nil {
return nil, errors.WithMessage(err, "error getting MSP ID from MSP principal")
}
return NewGroupOfGroups([]Group{NewMSPPeerGroup(mspID, peerRetriever)}), nil
}, nil
}
func signaturePolicySignedByAny() (GroupRetriever, error) {
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
return NewGroupOfGroups([]Group{NewMSPPeerGroup("", peerRetriever)}), nil
}, nil
}
func mspPrincipalToString(principal *mb.MSPPrincipal) (string, error) {
switch principal.PrincipalClassification {
case mb.MSPPrincipal_ROLE:
// Principal contains the msp role
mspRole := &mb.MSPRole{}
err := proto.Unmarshal(principal.Principal, mspRole)
if err != nil {
return "", errors.WithMessage(err, "unmarshal of principal failed")
}
return mspRole.MspIdentifier, nil
case mb.MSPPrincipal_ORGANIZATION_UNIT:
// Principal contains the OrganizationUnit
unit := &mb.OrganizationUnit{}
err := proto.Unmarshal(principal.Principal, unit)
if err != nil {
return "", errors.WithMessage(err, "unmarshal of principal failed")
}
return unit.MspIdentifier, nil
case mb.MSPPrincipal_IDENTITY:
// TODO: Do we need to support this?
errMsg := fmt.Sprintf("unsupported PrincipalClassification type: %s", reflect.TypeOf(principal.PrincipalClassification))
return "", errors.New(errMsg)
default:
errMsg := fmt.Sprintf("unknown PrincipalClassification type: %s", reflect.TypeOf(principal.PrincipalClassification))
return "", errors.New(errMsg)
}
}