This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
/
disseminator.go
226 lines (180 loc) · 7.18 KB
/
disseminator.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package dissemination
import (
"hash/fnv"
"sort"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/common/privdata"
"github.com/pkg/errors"
"github.com/trustbloc/fabric-peer-ext/pkg/common/discovery"
"github.com/trustbloc/fabric-peer-ext/pkg/roles"
)
var logger = flogging.MustGetLogger("transientdata")
// Disseminator disseminates transient data to a deterministic set of endorsers
type Disseminator struct {
*discovery.Discovery
namespace string
collection string
policy privdata.CollectionAccessPolicy
}
// New returns a new transient data disseminator
func New(channelID, namespace, collection string, policy privdata.CollectionAccessPolicy, gossip gossipAdapter) *Disseminator {
return &Disseminator{
Discovery: discovery.New(channelID, gossip),
namespace: namespace,
collection: collection,
policy: policy,
}
}
// ResolveEndorsers resolves to a set of endorsers to which transient data should be disseminated
func (d *Disseminator) ResolveEndorsers(key string) (discovery.PeerGroup, error) {
h, err := getHash32(key)
if err != nil {
return nil, errors.WithMessage(err, "error computing int32 hash of key")
}
orgs := d.chooseOrgs(h)
if len(orgs) == 0 {
logger.Warnf("[%s] No orgs for key [%s:%s:%s] using hash32 [%d]", d.ChannelID(), d.namespace, d.collection, key, h)
return nil, errors.Errorf("no orgs for key [%s:%s:%s]", d.namespace, d.collection, key)
}
logger.Debugf("[%s] Chosen orgs for key [%s:%s:%s] using hash32 [%d]: %s", d.ChannelID(), d.namespace, d.collection, key, h, orgs)
endorsers, err := d.chooseEndorsers(h, orgs)
if err != nil {
return nil, err
}
logger.Debugf("[%s] Chosen endorsers for key [%s:%s:%s] using hash32 [%d] from orgs %s: %s", d.ChannelID(), d.namespace, d.collection, key, h, orgs, endorsers)
return endorsers, nil
}
// ResolveAllEndorsersInOrgsForKey resolves all endorsers from within the orgs to which transient data should be disseminated,
// excluding the peers in the given exclude list.
func (d *Disseminator) ResolveAllEndorsersInOrgsForKey(key string, excludePeers ...*discovery.Member) (discovery.PeerGroup, error) {
h, err := getHash32(key)
if err != nil {
return nil, errors.WithMessage(err, "error computing int32 hash of key")
}
orgs := d.chooseOrgs(h)
if len(orgs) == 0 {
logger.Warnf("[%s] No orgs for key [%s:%s:%s] using hash32 [%d]", d.ChannelID(), d.namespace, d.collection, key, h)
return nil, errors.Errorf("no orgs for key [%s:%s:%s]", d.namespace, d.collection, key)
}
logger.Debugf("[%s] Orgs for key [%s:%s:%s] using hash32 [%d]: %s", d.ChannelID(), d.namespace, d.collection, key, h, orgs)
endorsers := d.getEndorsers(orgs, excludePeers...)
logger.Debugf("[%s] Chosen endorsers for key [%s:%s:%s]: %s", d.ChannelID(), d.namespace, d.collection, key, endorsers)
return endorsers, nil
}
// ResolveAllEndorsers resolves all endorsers that are eligible for the transient data, excluding the peers in the given exclude list.
func (d *Disseminator) ResolveAllEndorsers(excludePeers ...*discovery.Member) (discovery.PeerGroup, error) {
orgs := keys(d.policy.MemberOrgs())
if len(orgs) == 0 {
logger.Warnf("[%s] No orgs for [%s:%s]", d.ChannelID(), d.namespace, d.collection)
return nil, errors.Errorf("no orgs for [%s:%s]", d.namespace, d.collection)
}
logger.Debugf("[%s] Orgs for [%s:%s]: %s", d.ChannelID(), d.namespace, d.collection, orgs)
endorsers := d.getEndorsers(orgs, excludePeers...)
logger.Debugf("[%s] Chosen endorsers for [%s:%s]: %s", d.ChannelID(), d.namespace, d.collection, endorsers)
return endorsers, nil
}
func (d *Disseminator) chooseEndorsers(h uint32, orgs []string) (discovery.PeerGroup, error) {
allEndorsers := d.getEndorsers(orgs)
logger.Debugf("[%s] All endorsers for orgs %s: %s", d.ChannelID(), orgs, allEndorsers)
if len(allEndorsers) == 0 {
logger.Warnf("[%s] No endorsers for orgs %s", d.ChannelID(), orgs)
return nil, errors.Errorf("no endorsers for orgs %s", orgs)
}
var endorsers discovery.PeerGroup
for i := 0; len(endorsers) < d.policy.MaximumPeerCount(); i++ {
// Choose a set of endorsers from the set of orgs, incrementing the hash value
// so that different peers are chosen each time through the loop
endorsers = d.appendEndorsersFromOrgs(endorsers, int(h)+i, orgs)
if len(endorsers) >= len(allEndorsers) {
logger.Debugf("[%s] Stopping after %d endorsers were chosen using hash32 [%d] since all endorsers from orgs %s have already been added", d.ChannelID(), len(endorsers), h, orgs)
break
}
}
return endorsers, nil
}
func (d *Disseminator) appendEndorsersFromOrgs(endorsers discovery.PeerGroup, h int, orgs []string) discovery.PeerGroup {
for _, org := range orgs {
if len(endorsers) == d.policy.MaximumPeerCount() {
// We have enough endorsers
break
}
// Get a sorted list of endorsers for the org
endorsersForOrg := d.getEndorsers([]string{org}).Sort()
if len(endorsersForOrg) == 0 {
logger.Debugf("[%s] There are no endorsers in org [%s]", d.ChannelID(), org)
continue
}
logger.Debugf("[%s] Endorsers for [%s] using hash32 [%d]: %s", d.ChannelID(), org, h, endorsersForOrg)
// Deterministically choose an endorser
endorserForOrg := endorsersForOrg[h%len(endorsersForOrg)]
if endorsers.Contains(endorserForOrg) {
logger.Debugf("[%s] Will not add endorser [%s] from org [%s] using hash32 [%d] since it is already added", d.ChannelID(), endorserForOrg, org, h)
continue
}
endorsers = append(endorsers, endorserForOrg)
}
return endorsers
}
func (d *Disseminator) getEndorsers(mspIDs []string, excludePeers ...*discovery.Member) discovery.PeerGroup {
return d.GetMembers(func(m *discovery.Member) bool {
if discovery.PeerGroup(excludePeers).Contains(m) {
logger.Debugf("[%s] Not adding peer [%s] as an endorser since it is in the exclude list %s", d.ChannelID(), m.Endpoint, excludePeers)
return false
}
if !contains(mspIDs, m.MSPID) {
logger.Debugf("[%s] Not adding peer [%s] as an endorser since it is not in org %s", d.ChannelID(), m.Endpoint, mspIDs)
return false
}
if !m.HasRole(roles.EndorserRole) {
logger.Debugf("[%s] Not adding peer [%s] as an endorser since it does not have the endorser role", d.ChannelID(), m.Endpoint)
return false
}
return true
})
}
func (d *Disseminator) chooseOrgs(h uint32) []string {
memberOrgs := keys(d.policy.MemberOrgs())
numOrgs := min(d.policy.MaximumPeerCount(), len(memberOrgs))
// Copy and sort the orgs
var sortedOrgs []string
sortedOrgs = append(sortedOrgs, memberOrgs...)
sort.Strings(sortedOrgs)
var chosenOrgs []string
for i := 0; i < numOrgs; i++ {
chosenOrgs = append(chosenOrgs, sortedOrgs[(int(h)+i)%len(sortedOrgs)])
}
return chosenOrgs
}
func getHash32(key string) (uint32, error) {
h := fnv.New32a()
_, err := h.Write([]byte(key))
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
func min(i, j int) int {
if i < j {
return i
}
return j
}
func contains(strs []string, str string) bool {
for _, s := range strs {
if s == str {
return true
}
}
return false
}
func keys(m map[string]struct{}) []string {
var orgs []string
for org := range m {
orgs = append(orgs, org)
}
return orgs
}