Skip to content

Commit

Permalink
implements for PeerMembershipQuery (#132)
Browse files Browse the repository at this point in the history
fix #125

---------

Signed-off-by: 1gezhanghao <[email protected]>
  • Loading branch information
1gezhanghao authored Jul 4, 2023
1 parent d4028a0 commit 8a2f81e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package discovery

import (
"context"

"github.com/hyperledger/fabric-admin-sdk/pkg/identity"
"github.com/hyperledger/fabric-protos-go-apiv2/discovery"
"github.com/hyperledger/fabric-protos-go-apiv2/msp"
"github.com/hyperledger/fabric-protos-go-apiv2/peer"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)

func PeerMembershipQuery(ctx context.Context, conn *grpc.ClientConn, signer identity.SigningIdentity, channel string, filter *peer.ChaincodeInterest) (*discovery.PeerMembershipResult, error) {
id := &msp.SerializedIdentity{
Mspid: signer.MspID(),
IdBytes: signer.Credentials(),
}

idBytes, err := proto.Marshal(id)
if err != nil {
return nil, err
}

querys := []*discovery.Query{
{
Channel: channel,
Query: &discovery.Query_PeerQuery{
PeerQuery: &discovery.PeerMembershipQuery{
Filter: filter,
},
},
},
}

request := &discovery.Request{
Authentication: &discovery.AuthInfo{
ClientIdentity: idBytes,
ClientTlsCertHash: signer.Credentials(),
},
Queries: querys,
}

payload, err := proto.Marshal(request)
if err != nil {
return nil, err
}

sig, err := signer.Sign(payload)
if err != nil {
return nil, err
}

signedRequest := discovery.SignedRequest{
Payload: payload,
Signature: sig,
}

cli := discovery.NewDiscoveryClient(conn)

rs, err := cli.Discover(ctx, &signedRequest)
if err != nil {
return nil, err
}

for _, qrs := range rs.Results {
return qrs.GetMembers(), nil
}
return nil, nil
}
44 changes: 44 additions & 0 deletions test/discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package test

import (
"context"
"fmt"
"time"

"github.com/hyperledger/fabric-admin-sdk/internal/network"
"github.com/hyperledger/fabric-admin-sdk/pkg/discovery"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("discovery", func() {
Context("query peer membership", func() {
It("should work", func(specCtx SpecContext) {
var peerAddr = "localhost:7051"
var TLSCACert = "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt"
var PrivKeyPath = "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
var SignCert = "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]"
var MSPID = "Org1MSP"
var channelID = "mychannel"

peer0 := network.Node{
Addr: peerAddr,
TLSCACert: TLSCACert,
}
err := peer0.LoadConfig()
Expect(err).NotTo(HaveOccurred())
peerConnection, err := network.DialConnection(peer0)
Expect(err).NotTo(HaveOccurred())

id, err := CreateSigner(PrivKeyPath, SignCert, MSPID)
Expect(err).NotTo(HaveOccurred())

ctx, cancel := context.WithTimeout(specCtx, 30*time.Second)
defer cancel()
peerMembershipResult, err := discovery.PeerMembershipQuery(ctx, peerConnection, id, channelID, nil)
Expect(err).NotTo(HaveOccurred())
fmt.Println("peerMembershipResult", peerMembershipResult)
})
})
})

0 comments on commit 8a2f81e

Please sign in to comment.