Skip to content

Commit 9df670d

Browse files
committed
Add query to get all the channels for a given peer
This change adds a query which returns information about all the channels for a given peer. The return value is a ChannelQueryResponse proto which contains an array of ChannelInfo protos, which each contain the channel id. In the future, more information about the channels will likely be added to the ChannelInfo proto. https://jira.hyperledger.org/browse/FAB-2237 Change-Id: I46b96b37cfa607b7996fa65790e3288e0d0983b2 Signed-off-by: Will Lahti <[email protected]>
1 parent 2ea7cf0 commit 9df670d

File tree

7 files changed

+131
-17
lines changed

7 files changed

+131
-17
lines changed

core/peer/peer.go

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/hyperledger/fabric/gossip/service"
3737
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
3838
"github.com/hyperledger/fabric/protos/common"
39+
pb "github.com/hyperledger/fabric/protos/peer"
3940
"github.com/hyperledger/fabric/protos/utils"
4041
"github.com/op/go-logging"
4142
"github.com/spf13/viper"
@@ -344,6 +345,24 @@ func NewPeerClientConnectionWithAddress(peerAddress string) (*grpc.ClientConn, e
344345
return comm.NewClientConnectionWithAddress(peerAddress, true, false, nil)
345346
}
346347

348+
// GetChannelsInfo returns an array with information about all channels for
349+
// this peer
350+
func GetChannelsInfo() []*pb.ChannelInfo {
351+
// array to store metadata for all channels
352+
var channelInfoArray []*pb.ChannelInfo
353+
354+
chains.RLock()
355+
defer chains.RUnlock()
356+
for key := range chains.list {
357+
channelInfo := &pb.ChannelInfo{ChannelId: key}
358+
359+
// add this specific chaincode's metadata to the array of all chaincodes
360+
channelInfoArray = append(channelInfoArray, channelInfo)
361+
}
362+
363+
return channelInfoArray
364+
}
365+
347366
// GetPolicyManagerMgmt returns a special PolicyManager whose
348367
// only function is to give access to the policy manager of
349368
// a given channel. If the channel does not exists then,

core/peer/peer_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ func TestCreateChainFromBlock(t *testing.T) {
124124
Initialize(nil)
125125

126126
SetCurrConfigBlock(block, testChainID)
127+
128+
channels := GetChannelsInfo()
129+
if len(channels) != 1 {
130+
t.Fatalf("incorrect number of channels")
131+
}
127132
}
128133

129134
func TestNewPeerClientConnection(t *testing.T) {

core/scc/cscc/configure.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package cscc
2424
import (
2525
"fmt"
2626

27+
"github.com/golang/protobuf/proto"
2728
"github.com/hyperledger/fabric/core/chaincode/shim"
2829
"github.com/hyperledger/fabric/core/peer"
2930
pb "github.com/hyperledger/fabric/protos/peer"
@@ -44,6 +45,7 @@ const (
4445
JoinChain string = "JoinChain"
4546
UpdateConfigBlock string = "UpdateConfigBlock"
4647
GetConfigBlock string = "GetConfigBlock"
48+
GetChannels string = "GetChannels"
4749
)
4850

4951
// Init is called once per chain when the chain is created.
@@ -67,11 +69,16 @@ func (e *PeerConfiger) Init(stub shim.ChaincodeStubInterface) pb.Response {
6769
func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
6870
args := stub.GetArgs()
6971

70-
if len(args) < 2 {
72+
if len(args) < 1 {
7173
return shim.Error(fmt.Sprintf("Incorrect number of arguments, %d", len(args)))
7274
}
75+
7376
fname := string(args[0])
7477

78+
if fname != GetChannels && len(args) < 2 {
79+
return shim.Error(fmt.Sprintf("Incorrect number of arguments, %d", len(args)))
80+
}
81+
7582
cnflogger.Debugf("Invoke function: %s", fname)
7683

7784
// TODO: Handle ACL
@@ -82,6 +89,8 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
8289
return getConfigBlock(args[1])
8390
} else if fname == UpdateConfigBlock {
8491
return updateConfigBlock(args[1])
92+
} else if fname == GetChannels {
93+
return getChannels()
8594
}
8695

8796
return shim.Error(fmt.Sprintf("Requested function %s not found.", fname))
@@ -151,3 +160,18 @@ func getConfigBlock(chainID []byte) pb.Response {
151160

152161
return shim.Success(blockBytes)
153162
}
163+
164+
// getChannels returns information about all channels for this peer
165+
func getChannels() pb.Response {
166+
channelInfoArray := peer.GetChannelsInfo()
167+
168+
// add array with info about all channels for this peer
169+
cqr := &pb.ChannelQueryResponse{Channels: channelInfoArray}
170+
171+
cqrbytes, err := proto.Marshal(cqr)
172+
if err != nil {
173+
return shim.Error(err.Error())
174+
}
175+
176+
return shim.Success(cqrbytes)
177+
}

core/scc/cscc/configure_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
170170
if res := stub.MockInvoke("1", args); res.Status != shim.OK {
171171
t.Fatalf("cscc invoke GetConfigBlock failed with: %v", err)
172172
}
173+
174+
// get channels for the peer
175+
args = [][]byte{[]byte(GetChannels)}
176+
res := stub.MockInvoke("1", args)
177+
if res.Status != shim.OK {
178+
t.FailNow()
179+
}
180+
181+
cqr := &pb.ChannelQueryResponse{}
182+
err = proto.Unmarshal(res.Payload, cqr)
183+
if err != nil {
184+
t.FailNow()
185+
}
186+
187+
// peer joined one channel so query should return an array with one channel
188+
if len(cqr.GetChannels()) != 1 {
189+
t.FailNow()
190+
}
173191
}
174192

175193
func TestConfigerInvokeUpdateConfigBlock(t *testing.T) {

protos/peer/admin.pb.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protos/peer/query.pb.go

+50-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protos/peer/query.proto

+12
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ message ChaincodeInfo {
4545
// blank if the query is returning information about installed chaincodes.
4646
string vscc = 6;
4747
}
48+
49+
// ChannelQueryResponse returns information about each channel that pertains
50+
// to a query in lccc.go, such as GetChannels (returns all channels for a
51+
// given peer)
52+
message ChannelQueryResponse {
53+
repeated ChannelInfo channels = 1;
54+
}
55+
56+
// ChannelInfo contains general information about channels
57+
message ChannelInfo {
58+
string channel_id = 1;
59+
}

0 commit comments

Comments
 (0)