-
Notifications
You must be signed in to change notification settings - Fork 1
/
groupsimpl.go
458 lines (396 loc) · 9.04 KB
/
groupsimpl.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pgresolver
import (
"fmt"
"reflect"
"github.com/xuchengli/fabric-sdk-go/pkg/common/providers/fab"
"github.com/pkg/errors"
)
// MSPPeerRetriever is a function that retrieves peers by MSPID
type MSPPeerRetriever func(mspID string) []fab.Peer
// NewGroupOfGroups returns a new group of groups
func NewGroupOfGroups(groups []Group) GroupOfGroups {
items := make([]Item, len(groups))
for i, g := range groups {
items[i] = g
}
return &groupsImpl{groupImpl: groupImpl{Itms: items}}
}
// NewGroup creates a new Group
func NewGroup(items []Item) Group {
return &groupImpl{Itms: items}
}
// NewPeerGroup returns a new PeerGroup
func NewPeerGroup(peers ...fab.Peer) PeerGroup {
return &peerGroup{peers: asPeerWrappers(peers)}
}
// NewMSPPeerGroup returns a new MSP PeerGroup
func NewMSPPeerGroup(mspID string, peerRetriever MSPPeerRetriever) PeerGroup {
return &mspPeerGroup{
mspID: mspID,
peerRetriever: peerRetriever,
}
}
type groupImpl struct {
Itms []Item
}
func (g *groupImpl) Items() []Item {
return g.Itms
}
func (g *groupImpl) Reduce() []Group {
grps := asGroupsOrPanic(g.Items())
if len(grps) == 1 {
return grps[0].Reduce()
}
// Reduce each item
var reduced []Group
for _, g := range grps {
reduced = append(reduced, NewGroupOfGroups(g.Reduce()))
}
// Collapse each group
var collapsed []Group
for _, g := range and(reduced) {
if c, ok := g.(Collapsable); ok {
g = c.Collapse()
}
collapsed = append(collapsed, g)
}
// Get rid of duplicates
var pruned []Group
for _, g := range collapsed {
if !containsGroup(pruned, g) {
pruned = append(pruned, g)
}
}
return pruned
}
func (g *groupImpl) Collapse() Group {
var collapsable []Item
var nonCollapsable []Item
for _, item := range g.Items() {
if c, ok := item.(Collapsable); ok {
for _, ci := range c.Collapse().Items() {
if containsItem(collapsable, ci) {
continue
}
collapsable = append(collapsable, ci)
}
} else {
if !containsItem(nonCollapsable, item) {
nonCollapsable = append(nonCollapsable, item)
}
}
}
if len(collapsable) == 0 {
return NewGroup(nonCollapsable)
}
cg := NewGroup(collapsable)
if len(nonCollapsable) == 0 {
return cg
}
return NewGroup(append(nonCollapsable, cg))
}
func (g *groupImpl) Equals(other Group) bool {
if len(g.Items()) != len(other.Items()) {
return false
}
for _, i1 := range g.Items() {
if !containsItem(other.Items(), i1) {
return false
}
}
return true
}
func (g *groupImpl) String() string {
items := g.Items()
str := ""
if len(items) > 1 {
str = "["
}
for i, item := range items {
str = str + fmt.Sprintf("%s", item)
if i+1 < len(items) {
str += ", "
}
}
if len(items) > 1 {
str += "]"
}
return str
}
type groupsImpl struct {
groupImpl
}
func (g *groupsImpl) Groups() []Group {
groups := make([]Group, len(g.Items()))
for i, item := range g.Items() {
if group, ok := item.(Group); ok {
groups[i] = group
} else {
// This shouldn't happen since we have control over how the items are set.
panic("unexpected: item is not a Group")
}
}
return groups
}
func (g *groupsImpl) Reduce() []Group {
var result []Group
for _, grp := range g.Groups() {
result = append(result, grp.Reduce()...)
}
return result
}
func (g *groupsImpl) Collapse() Group {
return g
}
func (g *groupsImpl) Nof(threshold int32) (GroupOfGroups, error) {
if int(threshold) > len(g.Items()) {
return nil, errors.New("N is greater than length of the group")
}
if threshold <= 0 {
return nil, errors.New("N must be greater than 0")
}
return getCombinations(g.Items(), threshold, 0)
}
func (g *groupsImpl) String() string {
groups := g.Groups()
str := ""
if len(groups) > 1 {
str = "("
}
for i, pg := range groups {
str = str + fmt.Sprintf("%s", pg)
if i+1 < len(groups) {
str += ", "
}
}
if len(groups) > 1 {
str += ")"
}
return str
}
// peerWrapper wraps a Peer and implements the String() function (to help in debugging).
type peerWrapper struct {
target fab.Peer
}
func (pw *peerWrapper) String() string {
return pw.target.URL()
}
type peerGroup struct {
peers []*peerWrapper
}
func (pg *peerGroup) Items() []Item {
items := make([]Item, len(pg.peers))
for i, peer := range pg.peers {
items[i] = peer
}
return items
}
func (pg *peerGroup) Peers() []fab.Peer {
peers := make([]fab.Peer, len(pg.peers))
for i, pw := range pg.peers {
peers[i] = pw.target
}
return peers
}
func (pg *peerGroup) Equals(other Group) bool {
if len(pg.Items()) != len(other.Items()) {
return false
}
for _, item := range pg.Items() {
if !containsItem(other.Items(), item) {
return false
}
}
return true
}
func (pg *peerGroup) String() string {
items := pg.Items()
str := ""
if len(items) > 1 {
str = "["
}
for i, item := range items {
str = str + fmt.Sprintf("%s", item)
if i+1 < len(items) {
str += " AND "
}
}
if len(items) > 1 {
str += "]"
}
return str
}
func (pg *peerGroup) Reduce() []Group {
return []Group{pg}
}
func (pg *peerGroup) Collapse() Group {
return NewGroup([]Item{pg})
}
type mspPeerGroup struct {
mspID string
peerRetriever MSPPeerRetriever
}
func (pg *mspPeerGroup) Items() []Item {
peers := pg.Peers()
items := make([]Item, len(peers))
for i, peer := range peers {
items[i] = peer
}
return items
}
func (pg *mspPeerGroup) Peers() []fab.Peer {
return pg.peerRetriever(pg.mspID)
}
func (pg *mspPeerGroup) Equals(other Group) bool {
if otherPG, ok := other.(*mspPeerGroup); ok {
return otherPG.GetName() == pg.GetName()
}
return false
}
func (pg *mspPeerGroup) Reduce() []Group {
return []Group{pg}
}
func (pg *mspPeerGroup) Collapse() Group {
return NewGroup([]Item{pg})
}
func (pg *mspPeerGroup) String() string {
return pg.GetName()
}
func (pg *mspPeerGroup) GetName() string {
return pg.mspID
}
func asPeerWrappers(peers []fab.Peer) []*peerWrapper {
items := make([]*peerWrapper, len(peers))
for i, peer := range peers {
items[i] = &peerWrapper{target: peer}
}
return items
}
// asGroupsOrPanic converts the given array of Item into an array of Group.
// Each of the given items in the array must also be a Group or else a panic results.
func asGroupsOrPanic(items []Item) []Group {
groups := make([]Group, len(items))
for i, item := range items {
if grp, ok := item.(Group); ok {
groups[i] = grp
} else {
panic(fmt.Sprintf("item is not a group: %s", reflect.TypeOf(item)))
}
}
return groups
}
func getCombinations(items []Item, length int32, r int) (GroupOfGroups, error) {
if length == 1 {
// Create an item group for each item, containing a single item
var groups []Group
for _, item := range items {
groups = append(groups, NewGroup([]Item{item}))
}
combinations := NewGroupOfGroups(groups)
return combinations, nil
}
var groups []Group
for i := 0; i < len(items)-int(length)+1; i++ {
leftItem := items[i]
rightCombinations, err := getCombinations(items[i+1:], length-1, r+1)
if err != nil {
return nil, err
}
// Add the leftItem to each of the groups that came back
for _, g := range rightCombinations.Groups() {
var newItems []Item
newItems = append(newItems, leftItem)
newItems = append(newItems, g.Items()...)
groups = append(groups, NewGroup(newItems))
}
}
return NewGroupOfGroups(groups), nil
}
// and performs an 'and' operation of the given set of groups
// For example, given the set of groups, G=[(A,B),(C,D)],
// then and(G) = [(A,C),(A,D),(B,C),(B,D)]
func and(groups []Group) []Group {
op := &andOperation{stack: &stack{}}
op.and(groups, 0)
return op.result
}
type andOperation struct {
stack *stack
result []Group
}
func (o *andOperation) and(grps []Group, index int) {
if index >= len(grps) {
var items []Item
for _, c := range o.stack.Groups() {
groupItems := c.group.Items()
if c.index < len(groupItems) {
items = append(items, groupItems[c.index])
} else {
logger.Warnf("Expecting index to be less than %d but got %d", len(groupItems), c.index)
}
}
if len(items) > 0 {
o.result = append(o.result, NewGroup(items))
}
} else {
grp := grps[index]
items := grp.Items()
for j := 0; j < len(items); j++ {
o.stack.Push(grps[index], j)
o.and(grps, index+1)
o.stack.Pop()
}
}
}
type stack struct {
items []*entry
}
func (s *stack) Push(group Group, index int) {
s.items = append(s.items, &entry{
group: group,
index: index,
})
}
func (s *stack) Pop() {
lastIndex := len(s.items) - 1
if lastIndex >= 0 {
s.items = s.items[0:lastIndex]
}
}
func (s *stack) Groups() []*entry {
return s.items
}
type entry struct {
group Group
index int
}
func containsItem(items []Item, item Item) bool {
if grp, ok := item.(Group); ok {
for _, item2 := range items {
if ogrp, ok2 := item2.(Group); ok2 {
if grp.Equals(ogrp) {
return true
}
}
}
} else {
for _, itm := range items {
if itm == item {
return true
}
}
}
return false
}
func containsGroup(groups []Group, group Group) bool {
for _, g := range groups {
if g.Equals(group) {
return true
}
}
return false
}