/
endorserdiscover.go
192 lines (155 loc) · 6.19 KB
/
endorserdiscover.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
package dynamicdiscovery
import (
"fmt"
"github.com/golang/protobuf/proto"
discclient "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/discovery/client"
fabdiscovery "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/discovery"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
fabcontext "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/discovery"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
"github.com/pkg/errors"
"strings"
"time"
)
const (
peer0Org1 = "peer0.org1.example.com"
orgChannelID = "mychannel"
)
func newCCCall(ccID string, collections ...string) *fabdiscovery.ChaincodeCall {
return &fabdiscovery.ChaincodeCall{
Name: ccID,
CollectionNames: collections,
}
}
func GetEndorsers(ctx fabcontext.Client, client *discovery.Client, peerConfig fab.PeerConfig) (discclient.Endorsers, error) {
interest := newInterest(newCCCall("mycc"))
endorsers, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
chanResp, err := sendEndorserQuery(ctx, client, interest, peerConfig)
if err != nil && strings.Contains(err.Error(), "failed constructing descriptor for chaincodes") {
return nil, errors.WithMessage(err, "failed constructing descriptor for chaincodes")
} else if err != nil {
return nil, errors.WithMessage(err, "Got error from discovery")
}
return chanResp, nil
},
)
if err != nil {
return nil, fmt.Errorf("==== sendEndorserQuery faild! err:%s\n", err)
}
endorser := endorsers.(discclient.Endorsers)
return endorser, err
}
func sendEndorserQuery(ctx fabcontext.Client, client *discovery.Client, interest *fabdiscovery.ChaincodeInterest, peerConfig fab.PeerConfig) (discclient.Endorsers, error) {
req, err := discclient.NewRequest().OfChannel(orgChannelID).AddEndorsersQuery(interest)
reqCtx, cancel := context.NewRequest(ctx, context.WithTimeout(10*time.Second))
defer cancel()
responses, err := client.Send(reqCtx, req, peerConfig)
chanResp := responses[0].ForChannel(orgChannelID)
endorsers, err := chanResp.Endorsers(interest.Chaincodes, discclient.NewFilter(discclient.NoPriorities, discclient.NoExclusion))
if err != nil {
return nil, err
}
//asURLs(endorsers)
return endorsers, nil
}
func PrintConfig(peers []*discclient.Peer, result *fabdiscovery.ConfigResult) {
for _, endorser := range peers {
fmt.Printf("===========================================\n")
aliveMsg := endorser.AliveMessage.GetAliveMsg()
fmt.Printf("---- aliveMsg.Membership.Endpoint:[%s]\n", aliveMsg.Membership.Endpoint)
sID := &msp.SerializedIdentity{}
if err := proto.Unmarshal(endorser.Identity, sID); err != nil {
panic(fmt.Errorf("failed unmarshaling peer's identity, err:%s \n", err))
}
fmt.Printf("---- endorser.Mspid: %s \n", sID.Mspid)
fmt.Printf("---- endorser.IdentityCert: \n")
fmt.Printf("%s \n", string(sID.IdBytes))
org := result.Msps[sID.Mspid]
fmt.Printf("---- endorser belong to organization Name : %s \n", org.Name)
for _, tlscacert := range org.TlsRootCerts {
fmt.Printf("---- endorser belong to organization TlsRootCerts: \n", string(tlscacert))
fmt.Printf("%s \n", string(tlscacert))
}
for _, cacert := range org.RootCerts {
fmt.Printf("---- endorser belong to organization RootCerts: \n")
fmt.Printf("%s \n", string(cacert))
}
stateInfo := endorser.StateInfoMessage.GetStateInfo()
fmt.Printf("--- Ledger Height: %d \n", stateInfo.Properties.LedgerHeight)
fmt.Printf("--- Chaincodes: \n")
for _, cc := range stateInfo.Properties.Chaincodes {
fmt.Printf("------ %s:%s \n", cc.Name, cc.Version)
}
}
}
func PrintPeerInfo(peers []*discclient.Peer) {
for _, endorser := range peers {
fmt.Printf("===========================================\n")
aliveMsg := endorser.AliveMessage.GetAliveMsg()
fmt.Printf("---- aliveMsg.Membership.Endpoint:[%s]\n", aliveMsg.Membership.Endpoint)
sID := &msp.SerializedIdentity{}
if err := proto.Unmarshal(endorser.Identity, sID); err != nil {
panic(fmt.Errorf("failed unmarshaling peer's identity, err:%s \n", err))
}
fmt.Printf("---- endorser.Mspid: %s \n", sID.Mspid)
fmt.Printf("---- endorser.cert: %s \n", string(sID.IdBytes))
stateInfo := endorser.StateInfoMessage.GetStateInfo()
fmt.Printf("--- Ledger Height: %d \n", stateInfo.Properties.LedgerHeight)
fmt.Printf("--- Chaincodes: \n")
for _, cc := range stateInfo.Properties.Chaincodes {
fmt.Printf("------ %s:%s \n", cc.Name, cc.Version)
}
}
}
func asURLs(endorsers discclient.Endorsers) []string {
var urls []string
fmt.Printf("len(endorsers):[%d]\n", len(endorsers))
for _, endorser := range endorsers {
aliveMsg := endorser.AliveMessage.GetAliveMsg()
urls = append(urls, aliveMsg.Membership.Endpoint)
fmt.Printf("---- aliveMsg.Membership.Endpoint:[%s]\n", aliveMsg.Membership.Endpoint)
sID := &msp.SerializedIdentity{}
if err := proto.Unmarshal(endorser.Identity, sID); err != nil {
fmt.Errorf("failed unmarshaling peer's identity, err:%s \n", err)
return nil
}
fmt.Printf(" endorser.Identity:[%#+v] \n", sID.Mspid)
fmt.Printf("---- endorser.AliveMessage.Content:[%#+v]\n", endorser.AliveMessage.Content)
}
return urls
}
func checkEndorsers(endorsers []string, expectedGroups [][]string) {
for _, group := range expectedGroups {
if containsAll(endorsers, group) {
fmt.Printf("Found matching endorser group: %#v", group)
return
}
}
}
func containsAll(endorsers []string, expectedEndorserGroup []string) bool {
if len(endorsers) != len(expectedEndorserGroup) {
return false
}
for _, endorser := range endorsers {
fmt.Printf("Checking endpoint: %s ...", endorser)
if !contains(expectedEndorserGroup, endorser) {
return false
}
}
return true
}
func contains(group []string, endorser string) bool {
for _, e := range group {
if e == endorser {
return true
}
}
return false
}
func newInterest(ccCalls ...*fabdiscovery.ChaincodeCall) *fabdiscovery.ChaincodeInterest {
return &fabdiscovery.ChaincodeInterest{Chaincodes: ccCalls}
}