forked from tw-bc-group/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
150 lines (139 loc) · 4.56 KB
/
discovery.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
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package helpers
import (
"github.com/hyperledger/fabric/protos/discovery"
)
type DiscoveredPeer struct {
MSPID string
LedgerHeight uint64
Endpoint string
Identity string
Chaincodes []string
}
type EndorsementDescriptor struct {
Chaincode string
EndorsersByGroups map[string][]DiscoveredPeer
Layouts []*discovery.Layout
}
// Verifies that each DiscveredPeer in expectedPeers exist in actualPeers.
// The check is done by comparing peer endpoint and mspid and ignores ledger height, identity and chaincodes.
func CheckPeersContainsExpectedPeers(expectedPeers []DiscoveredPeer, actualPeers []DiscoveredPeer) bool {
if len(actualPeers) != len(expectedPeers) {
return false
}
foundPeersCount := 0
for _, expectedPeer := range expectedPeers {
for _, actualPeer := range actualPeers {
if actualPeer.Endpoint == expectedPeer.Endpoint && actualPeer.MSPID == expectedPeer.MSPID {
foundPeersCount++
break
}
}
}
if foundPeersCount == len(expectedPeers) {
return true
}
return false
}
// Verifies that discovery service expectedConfig can be found in actualConfig.
// The check is done by comparing orderers (name, host and port) and by comparing msps(only name)
func CheckConfigContainsExpectedConfig(expectedConfig discovery.ConfigResult, actualConfig discovery.ConfigResult) bool {
if len(expectedConfig.Orderers) != len(actualConfig.Orderers) || len(expectedConfig.Msps) != len(actualConfig.Msps) {
return false
}
// compare Orderers
for expectedName, expectedOrderer := range expectedConfig.Orderers {
foundOrderer := false
for actualName, actualOrderer := range actualConfig.Orderers {
if expectedName != actualName {
continue
}
if len(expectedOrderer.Endpoint) != len(actualOrderer.Endpoint) {
continue
}
foundEndpoints := 0
for _, expectedEndpoint := range expectedOrderer.Endpoint {
for _, actualEndpoint := range actualOrderer.Endpoint {
if expectedEndpoint.Host == actualEndpoint.Host && expectedEndpoint.Port == actualEndpoint.Port {
foundEndpoints++
}
}
}
if len(expectedOrderer.Endpoint) == foundEndpoints {
foundOrderer = true
}
}
if !foundOrderer {
return false
}
}
// compare msps
for expectedName, expectedMsp := range expectedConfig.Msps {
foundMsp := false
for actualName, actualMsp := range actualConfig.Msps {
if expectedName != actualName {
continue
}
if expectedMsp.Name != actualMsp.Name {
continue
}
foundMsp = true
}
if !foundMsp {
return false
}
}
return true
}
// Verifies that actualEndorsement contains expectedEndorsement.
// The check is done by comparing the following fields:
// - Chaincode - identical in both endorsement descriptors
// - EndorsersByGroups - each group of DiscoveredPeer in expectedEndorsement can be found also in actualEndorsement. different calls to discovery service might generate different groups names
// - Layouts - all layouts that exist in expectedEndorsement can be found also in actualEndorsement. the function takes care of different names also in this check
func CheckEndorsementContainsExpectedEndorsement(expectedEndorsement EndorsementDescriptor, actualEndorsement EndorsementDescriptor) bool {
if expectedEndorsement.Chaincode != actualEndorsement.Chaincode {
return false
}
groupsMapping := map[string]string{} //mapping from dummy names to group names in the output
for dummyGroupName, expectedGroup := range expectedEndorsement.EndorsersByGroups {
foundGroup := false
for actualGroupName, actualGroup := range actualEndorsement.EndorsersByGroups {
if CheckPeersContainsExpectedPeers(expectedGroup, actualGroup) {
foundGroup = true
groupsMapping[actualGroupName] = dummyGroupName
break
}
}
if !foundGroup {
return false
}
}
for _, expectedLayout := range expectedEndorsement.Layouts {
foundLayout := false
for _, actualLayout := range actualEndorsement.Layouts {
if len(expectedLayout.QuantitiesByGroup) != len(actualLayout.QuantitiesByGroup) {
continue
}
foundGroupCount := 0
for expectedGroupName, expectedQuantity := range expectedLayout.QuantitiesByGroup {
for actualGroupName, actualQuantity := range actualLayout.QuantitiesByGroup {
if groupsMapping[actualGroupName] == expectedGroupName && expectedQuantity == actualQuantity {
foundGroupCount++
break
}
}
}
if len(expectedLayout.QuantitiesByGroup) == foundGroupCount {
foundLayout = true
break
}
}
if !foundLayout {
return false
}
}
return true
}