Skip to content

Commit 9949bdb

Browse files
committed
[FAB-14053] move discovery proto extensions
Change-Id: I07c1970189b01e9f5ff3d3b866760b0384bc0f75 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 7af149c commit 9949bdb

File tree

9 files changed

+201
-172
lines changed

9 files changed

+201
-172
lines changed

discovery/client/client.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ import (
1414
"time"
1515

1616
"github.com/golang/protobuf/proto"
17+
"github.com/hyperledger/fabric/discovery/protoext"
1718
"github.com/hyperledger/fabric/protos/discovery"
1819
"github.com/hyperledger/fabric/protos/gossip"
1920
"github.com/hyperledger/fabric/protos/msp"
2021
"github.com/pkg/errors"
2122
)
2223

23-
var (
24-
configTypes = []discovery.QueryType{discovery.ConfigQueryType, discovery.PeerMembershipQueryType, discovery.ChaincodeQueryType, discovery.LocalMembershipQueryType}
25-
)
24+
var configTypes = []protoext.QueryType{
25+
protoext.ConfigQueryType,
26+
protoext.PeerMembershipQueryType,
27+
protoext.ChaincodeQueryType,
28+
protoext.LocalMembershipQueryType,
29+
}
2630

2731
// Client interacts with the discovery server
2832
type Client struct {
@@ -34,7 +38,7 @@ type Client struct {
3438
func NewRequest() *Request {
3539
r := &Request{
3640
invocationChainMapping: make(map[int][]InvocationChain),
37-
queryMapping: make(map[discovery.QueryType]map[string]int),
41+
queryMapping: make(map[protoext.QueryType]map[string]int),
3842
Request: &discovery.Request{},
3943
}
4044
// pre-populate types
@@ -49,7 +53,7 @@ type Request struct {
4953
lastChannel string
5054
lastIndex int
5155
// map from query type to channel to expected index in response
52-
queryMapping map[discovery.QueryType]map[string]int
56+
queryMapping map[protoext.QueryType]map[string]int
5357
// map from expected index in response to invocation chains
5458
invocationChainMapping map[int][]InvocationChain
5559
*discovery.Request
@@ -65,7 +69,7 @@ func (req *Request) AddConfigQuery() *Request {
6569
Channel: ch,
6670
Query: q,
6771
})
68-
req.addQueryMapping(discovery.ConfigQueryType, ch)
72+
req.addQueryMapping(protoext.ConfigQueryType, ch)
6973
return req
7074
}
7175

@@ -91,7 +95,7 @@ func (req *Request) AddEndorsersQuery(interests ...*discovery.ChaincodeInterest)
9195
invocationChains = append(invocationChains, interest.Chaincodes)
9296
}
9397
req.addChaincodeQueryMapping(invocationChains)
94-
req.addQueryMapping(discovery.ChaincodeQueryType, ch)
98+
req.addQueryMapping(protoext.ChaincodeQueryType, ch)
9599
return req, nil
96100
}
97101

@@ -104,7 +108,7 @@ func (req *Request) AddLocalPeersQuery() *Request {
104108
Query: q,
105109
})
106110
var ic InvocationChain
107-
req.addQueryMapping(discovery.LocalMembershipQueryType, channnelAndInvocationChain("", ic))
111+
req.addQueryMapping(protoext.LocalMembershipQueryType, channnelAndInvocationChain("", ic))
108112
return req
109113
}
110114

@@ -127,7 +131,7 @@ func (req *Request) AddPeersQuery(invocationChain ...*discovery.ChaincodeCall) *
127131
ic = InvocationChain(invocationChain)
128132
}
129133
req.addChaincodeQueryMapping([]InvocationChain{ic})
130-
req.addQueryMapping(discovery.PeerMembershipQueryType, channnelAndInvocationChain(ch, ic))
134+
req.addQueryMapping(protoext.PeerMembershipQueryType, channnelAndInvocationChain(ch, ic))
131135
return req
132136
}
133137

@@ -145,7 +149,7 @@ func (req *Request) addChaincodeQueryMapping(invocationChains []InvocationChain)
145149
req.invocationChainMapping[req.lastIndex] = invocationChains
146150
}
147151

148-
func (req *Request) addQueryMapping(queryType discovery.QueryType, key string) {
152+
func (req *Request) addQueryMapping(queryType protoext.QueryType, key string) {
149153
req.queryMapping[queryType][key] = req.lastIndex
150154
req.lastIndex++
151155
}
@@ -193,7 +197,7 @@ type localResponse struct {
193197
}
194198

195199
func (cr *localResponse) Peers() ([]*Peer, error) {
196-
return parsePeers(discovery.LocalMembershipQueryType, cr.response, "")
200+
return parsePeers(protoext.LocalMembershipQueryType, cr.response, "")
197201
}
198202

199203
type channelResponse struct {
@@ -203,7 +207,7 @@ type channelResponse struct {
203207

204208
func (cr *channelResponse) Config() (*discovery.ConfigResult, error) {
205209
res, exists := cr.response[key{
206-
queryType: discovery.ConfigQueryType,
210+
queryType: protoext.ConfigQueryType,
207211
k: cr.channel,
208212
}]
209213

@@ -218,7 +222,7 @@ func (cr *channelResponse) Config() (*discovery.ConfigResult, error) {
218222
return nil, res.(error)
219223
}
220224

221-
func parsePeers(queryType discovery.QueryType, r response, channel string, invocationChain ...*discovery.ChaincodeCall) ([]*Peer, error) {
225+
func parsePeers(queryType protoext.QueryType, r response, channel string, invocationChain ...*discovery.ChaincodeCall) ([]*Peer, error) {
222226
peerKeys := key{
223227
queryType: queryType,
224228
k: fmt.Sprintf("%s %s", channel, InvocationChain(invocationChain).String()),
@@ -237,22 +241,22 @@ func parsePeers(queryType discovery.QueryType, r response, channel string, invoc
237241
}
238242

239243
func (cr *channelResponse) Peers(invocationChain ...*discovery.ChaincodeCall) ([]*Peer, error) {
240-
return parsePeers(discovery.PeerMembershipQueryType, cr.response, cr.channel, invocationChain...)
244+
return parsePeers(protoext.PeerMembershipQueryType, cr.response, cr.channel, invocationChain...)
241245
}
242246

243247
func (cr *channelResponse) Endorsers(invocationChain InvocationChain, f Filter) (Endorsers, error) {
244248
// If we have a key that has no chaincode field,
245249
// it means it's an error returned from the service
246250
if err, exists := cr.response[key{
247-
queryType: discovery.ChaincodeQueryType,
251+
queryType: protoext.ChaincodeQueryType,
248252
k: cr.channel,
249253
}]; exists {
250254
return nil, err.(error)
251255
}
252256

253257
// Else, the service returned a response that isn't an error
254258
res, exists := cr.response[key{
255-
queryType: discovery.ChaincodeQueryType,
259+
queryType: protoext.ChaincodeQueryType,
256260
k: cr.channel,
257261
invocationChain: invocationChain.String(),
258262
}]
@@ -328,7 +332,7 @@ func (resp response) ForChannel(ch string) ChannelResponse {
328332
}
329333

330334
type key struct {
331-
queryType discovery.QueryType
335+
queryType protoext.QueryType
332336
k string
333337
invocationChain string
334338
}
@@ -338,14 +342,14 @@ func (req *Request) computeResponse(r *discovery.Response) (response, error) {
338342
resp := make(response)
339343
for configType, channel2index := range req.queryMapping {
340344
switch configType {
341-
case discovery.ConfigQueryType:
345+
case protoext.ConfigQueryType:
342346
err = resp.mapConfig(channel2index, r)
343-
case discovery.ChaincodeQueryType:
347+
case protoext.ChaincodeQueryType:
344348
err = resp.mapEndorsers(channel2index, r, req.invocationChainMapping)
345-
case discovery.PeerMembershipQueryType:
346-
err = resp.mapPeerMembership(channel2index, r, discovery.PeerMembershipQueryType)
347-
case discovery.LocalMembershipQueryType:
348-
err = resp.mapPeerMembership(channel2index, r, discovery.LocalMembershipQueryType)
349+
case protoext.PeerMembershipQueryType:
350+
err = resp.mapPeerMembership(channel2index, r, protoext.PeerMembershipQueryType)
351+
case protoext.LocalMembershipQueryType:
352+
err = resp.mapPeerMembership(channel2index, r, protoext.LocalMembershipQueryType)
349353
}
350354
if err != nil {
351355
return nil, err
@@ -357,12 +361,12 @@ func (req *Request) computeResponse(r *discovery.Response) (response, error) {
357361

358362
func (resp response) mapConfig(channel2index map[string]int, r *discovery.Response) error {
359363
for ch, index := range channel2index {
360-
config, err := r.ConfigAt(index)
364+
config, err := protoext.ResponseConfigAt(r, index)
361365
if config == nil && err == nil {
362366
return errors.Errorf("expected QueryResult of either ConfigResult or Error but got %v instead", r.Results[index])
363367
}
364368
key := key{
365-
queryType: discovery.ConfigQueryType,
369+
queryType: protoext.ConfigQueryType,
366370
k: ch,
367371
}
368372

@@ -376,9 +380,9 @@ func (resp response) mapConfig(channel2index map[string]int, r *discovery.Respon
376380
return nil
377381
}
378382

379-
func (resp response) mapPeerMembership(key2Index map[string]int, r *discovery.Response, qt discovery.QueryType) error {
383+
func (resp response) mapPeerMembership(key2Index map[string]int, r *discovery.Response, qt protoext.QueryType) error {
380384
for k, index := range key2Index {
381-
membersRes, err := r.MembershipAt(index)
385+
membersRes, err := protoext.ResponseMembershipAt(r, index)
382386
if membersRes == nil && err == nil {
383387
return errors.Errorf("expected QueryResult of either PeerMembershipResult or Error but got %v instead", r.Results[index])
384388
}
@@ -403,7 +407,7 @@ func (resp response) mapPeerMembership(key2Index map[string]int, r *discovery.Re
403407
return nil
404408
}
405409

406-
func peersForChannel(membersRes *discovery.PeerMembershipResult, qt discovery.QueryType) ([]*Peer, error) {
410+
func peersForChannel(membersRes *discovery.PeerMembershipResult, qt protoext.QueryType) ([]*Peer, error) {
407411
var peers []*Peer
408412
for org, peersOfCurrentOrg := range membersRes.PeersByOrg {
409413
for _, peer := range peersOfCurrentOrg.Peers {
@@ -435,23 +439,23 @@ func peersForChannel(membersRes *discovery.PeerMembershipResult, qt discovery.Qu
435439
return peers, nil
436440
}
437441

438-
func isStateInfoExpected(qt discovery.QueryType) bool {
439-
return qt != discovery.LocalMembershipQueryType
442+
func isStateInfoExpected(qt protoext.QueryType) bool {
443+
return qt != protoext.LocalMembershipQueryType
440444
}
441445

442446
func (resp response) mapEndorsers(
443447
channel2index map[string]int,
444448
r *discovery.Response,
445449
chaincodeQueryMapping map[int][]InvocationChain) error {
446450
for ch, index := range channel2index {
447-
ccQueryRes, err := r.EndorsersAt(index)
451+
ccQueryRes, err := protoext.ResponseEndorsersAt(r, index)
448452
if ccQueryRes == nil && err == nil {
449453
return errors.Errorf("expected QueryResult of either ChaincodeQueryResult or Error but got %v instead", r.Results[index])
450454
}
451455

452456
if err != nil {
453457
key := key{
454-
queryType: discovery.ChaincodeQueryType,
458+
queryType: protoext.ChaincodeQueryType,
455459
k: ch,
456460
}
457461
resp[key] = errors.New(err.Content)
@@ -475,7 +479,7 @@ func (resp response) mapEndorsersOfChannel(ccRs *discovery.ChaincodeQueryResult,
475479
return errors.Errorf("expected chaincode %s but got endorsement descriptor for %s", expectedCCName, desc.Chaincode)
476480
}
477481
key := key{
478-
queryType: discovery.ChaincodeQueryType,
482+
queryType: protoext.ChaincodeQueryType,
479483
k: channel,
480484
invocationChain: invocationChain[i].String(),
481485
}

discovery/protoext/querytype.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package protoext
8+
9+
import "github.com/hyperledger/fabric/protos/discovery"
10+
11+
// QueryType defines the types of service discovery requests
12+
type QueryType uint8
13+
14+
const (
15+
InvalidQueryType QueryType = iota
16+
ConfigQueryType
17+
PeerMembershipQueryType
18+
ChaincodeQueryType
19+
LocalMembershipQueryType
20+
)
21+
22+
// GetType returns the type of the request
23+
func GetQueryType(q *discovery.Query) QueryType {
24+
switch {
25+
case q.GetCcQuery() != nil:
26+
return ChaincodeQueryType
27+
case q.GetConfigQuery() != nil:
28+
return ConfigQueryType
29+
case q.GetPeerQuery() != nil:
30+
return PeerMembershipQueryType
31+
case q.GetLocalPeers() != nil:
32+
return LocalMembershipQueryType
33+
default:
34+
return InvalidQueryType
35+
}
36+
}

discovery/protoext/querytype_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package protoext_test
8+
9+
import (
10+
"strconv"
11+
"testing"
12+
13+
"github.com/hyperledger/fabric/discovery/protoext"
14+
"github.com/hyperledger/fabric/protos/discovery"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestGetQueryType(t *testing.T) {
19+
tests := []struct {
20+
q *discovery.Query
21+
expected protoext.QueryType
22+
}{
23+
{q: &discovery.Query{Query: &discovery.Query_PeerQuery{PeerQuery: &discovery.PeerMembershipQuery{}}}, expected: protoext.PeerMembershipQueryType},
24+
{q: &discovery.Query{Query: &discovery.Query_ConfigQuery{ConfigQuery: &discovery.ConfigQuery{}}}, expected: protoext.ConfigQueryType},
25+
{q: &discovery.Query{Query: &discovery.Query_CcQuery{CcQuery: &discovery.ChaincodeQuery{}}}, expected: protoext.ChaincodeQueryType},
26+
{q: &discovery.Query{Query: &discovery.Query_LocalPeers{LocalPeers: &discovery.LocalPeerQuery{}}}, expected: protoext.LocalMembershipQueryType},
27+
{q: &discovery.Query{Query: &discovery.Query_CcQuery{}}, expected: protoext.InvalidQueryType},
28+
{q: nil, expected: protoext.InvalidQueryType},
29+
}
30+
31+
for i, tt := range tests {
32+
t.Run(strconv.Itoa(i), func(t *testing.T) {
33+
assert.Equal(t, tt.expected, protoext.GetQueryType(tt.q))
34+
})
35+
}
36+
}

discovery/protoext/response.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package protoext
8+
9+
import "github.com/hyperledger/fabric/protos/discovery"
10+
11+
// ResponseConfigAt returns the ConfigResult at a given index in the Response,
12+
// or an Error if present.
13+
func ResponseConfigAt(m *discovery.Response, i int) (*discovery.ConfigResult, *discovery.Error) {
14+
r := m.Results[i]
15+
return r.GetConfigResult(), r.GetError()
16+
}
17+
18+
// ResponseMembershipAt returns the PeerMembershipResult at a given index in the Response,
19+
// or an Error if present.
20+
func ResponseMembershipAt(m *discovery.Response, i int) (*discovery.PeerMembershipResult, *discovery.Error) {
21+
r := m.Results[i]
22+
return r.GetMembers(), r.GetError()
23+
}
24+
25+
// ResponseEndorsersAt returns the PeerMembershipResult at a given index in the Response,
26+
// or an Error if present.
27+
func ResponseEndorsersAt(m *discovery.Response, i int) (*discovery.ChaincodeQueryResult, *discovery.Error) {
28+
r := m.Results[i]
29+
return r.GetCcQueryRes(), r.GetError()
30+
}

discovery/protoext/signedreq.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package protoext
8+
9+
import (
10+
"github.com/gogo/protobuf/proto"
11+
"github.com/hyperledger/fabric/protos/discovery"
12+
)
13+
14+
// SignedRequestToRequest deserializes this SignedRequest's payload
15+
// and returns the serialized Request in its object form.
16+
// Returns an error in case the operation fails.
17+
func SignedRequestToRequest(sr *discovery.SignedRequest) (*discovery.Request, error) {
18+
req := &discovery.Request{}
19+
return req, proto.Unmarshal(sr.Payload, req)
20+
}

discovery/protoext/signedreq_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package protoext_test
8+
9+
import (
10+
"testing"
11+
12+
"github.com/gogo/protobuf/proto"
13+
"github.com/hyperledger/fabric/discovery/protoext"
14+
"github.com/hyperledger/fabric/protos/discovery"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestSignedRequestToRequest(t *testing.T) {
19+
sr := &discovery.SignedRequest{
20+
Payload: []byte{0},
21+
}
22+
r, err := protoext.SignedRequestToRequest(sr)
23+
assert.Error(t, err)
24+
25+
req := &discovery.Request{}
26+
b, _ := proto.Marshal(req)
27+
sr.Payload = b
28+
r, err = protoext.SignedRequestToRequest(sr)
29+
assert.NoError(t, err)
30+
assert.NotNil(t, r)
31+
}

0 commit comments

Comments
 (0)