Skip to content

Commit f71a380

Browse files
committed
[FAB-11028] discover: Remove nil chaincodes from output
The endorsers always outputs peer chaincodes as nil due to using a shared struct and not populating the chaincodes. Addressed by defining a separate struct. Change-Id: I40687b3a9363455272263e6ba9ac960ae6c99faa Signed-off-by: yacovm <yacovm@il.ibm.com>
1 parent 40b6969 commit f71a380

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

discovery/cmd/endorsers.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (ec *chaincodesAndCollections) parseInput() (map[string][]string, error) {
172172
func parseEndorsementDescriptors(descriptors []*EndorsementDescriptor) []endorsermentDescriptor {
173173
var res []endorsermentDescriptor
174174
for _, desc := range descriptors {
175-
endorsersByGroups := make(map[string][]peer)
175+
endorsersByGroups := make(map[string][]endorser)
176176
for grp, endorsers := range desc.EndorsersByGroups {
177177
for _, p := range endorsers.Peers {
178178
endorsersByGroups[grp] = append(endorsersByGroups[grp], endorserFromRaw(p))
@@ -187,16 +187,23 @@ func parseEndorsementDescriptors(descriptors []*EndorsementDescriptor) []endorse
187187
return res
188188
}
189189

190+
type endorser struct {
191+
MSPID string
192+
LedgerHeight uint64
193+
Endpoint string
194+
Identity string
195+
}
196+
190197
type endorsermentDescriptor struct {
191198
Chaincode string
192-
EndorsersByGroups map[string][]peer
199+
EndorsersByGroups map[string][]endorser
193200
Layouts []*Layout
194201
}
195202

196-
func endorserFromRaw(p *Peer) peer {
203+
func endorserFromRaw(p *Peer) endorser {
197204
sId := &msp.SerializedIdentity{}
198205
proto.Unmarshal(p.Identity, sId)
199-
return peer{
206+
return endorser{
200207
MSPID: sId.Mspid,
201208
Endpoint: endpointFromEnvelope(p.MembershipInfo),
202209
LedgerHeight: ledgerHeightFromEnvelope(p.StateInfo),

discovery/cmd/endorsers_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/hyperledger/fabric/discovery/cmd"
1616
"github.com/hyperledger/fabric/discovery/cmd/mocks"
1717
discprotos "github.com/hyperledger/fabric/protos/discovery"
18+
"github.com/hyperledger/fabric/protos/msp"
19+
"github.com/hyperledger/fabric/protos/utils"
1820
"github.com/pkg/errors"
1921
"github.com/stretchr/testify/assert"
2022
"github.com/stretchr/testify/mock"
@@ -148,12 +150,14 @@ func TestParseEndorsementResponse(t *testing.T) {
148150
res := &mocks.ServiceResponse{}
149151

150152
t.Run("Server returns empty response", func(t *testing.T) {
153+
defer buff.Reset()
151154
res.On("Raw").Return(&discprotos.Response{}).Once()
152155
err := parser.ParseResponse("mychannel", res)
153156
assert.Contains(t, err.Error(), "empty results")
154157
})
155158

156159
t.Run("Server returns an error", func(t *testing.T) {
160+
defer buff.Reset()
157161
res.On("Raw").Return(&discprotos.Response{
158162
Results: []*discprotos.QueryResult{
159163
{
@@ -170,6 +174,7 @@ func TestParseEndorsementResponse(t *testing.T) {
170174
})
171175

172176
t.Run("Server returns a response with the wrong type", func(t *testing.T) {
177+
defer buff.Reset()
173178
res.On("Raw").Return(&discprotos.Response{
174179
Results: []*discprotos.QueryResult{
175180
{
@@ -186,6 +191,7 @@ func TestParseEndorsementResponse(t *testing.T) {
186191
})
187192

188193
t.Run("Server returns a proper response", func(t *testing.T) {
194+
defer buff.Reset()
189195
res.On("Raw").Return(&discprotos.Response{
190196
Results: []*discprotos.QueryResult{
191197
{
@@ -195,18 +201,23 @@ func TestParseEndorsementResponse(t *testing.T) {
195201
}).Once()
196202
err := parser.ParseResponse("mychannel", res)
197203
assert.NoError(t, err)
204+
assert.Equal(t, expectedEndorsersOutput, buff.String())
198205
})
199206
}
200207

201208
var endorsersResponse = &discprotos.QueryResult_CcQueryRes{
202209
CcQueryRes: &discprotos.ChaincodeQueryResult{
203210
Content: []*discprotos.EndorsementDescriptor{
204211
{
212+
Chaincode: "mycc",
205213
EndorsersByGroups: map[string]*discprotos.Peers{
206214
"Org1MSP": {
207215
Peers: []*discprotos.Peer{
208216
{
209-
Identity: []byte("identity"),
217+
Identity: utils.MarshalOrPanic(&msp.SerializedIdentity{
218+
Mspid: "Org1MSP",
219+
IdBytes: []byte("identity"),
220+
}),
210221
StateInfo: stateInfoMessage(100).Envelope,
211222
MembershipInfo: aliveMessage(0).Envelope,
212223
},
@@ -224,3 +235,27 @@ var endorsersResponse = &discprotos.QueryResult_CcQueryRes{
224235
},
225236
},
226237
}
238+
239+
const expectedEndorsersOutput = `[
240+
{
241+
"Chaincode": "mycc",
242+
"EndorsersByGroups": {
243+
"Org1MSP": [
244+
{
245+
"MSPID": "Org1MSP",
246+
"LedgerHeight": 100,
247+
"Endpoint": "p0",
248+
"Identity": "identity"
249+
}
250+
]
251+
},
252+
"Layouts": [
253+
{
254+
"quantities_by_group": {
255+
"Org1MSP": 2
256+
}
257+
}
258+
]
259+
}
260+
]
261+
`

0 commit comments

Comments
 (0)