From 8a2f81e5804466a53d866a6b6f231274af99753f Mon Sep 17 00:00:00 2001 From: xvkong Date: Tue, 4 Jul 2023 21:36:57 +0800 Subject: [PATCH] implements for PeerMembershipQuery (#132) fix #125 --------- Signed-off-by: 1gezhanghao --- pkg/discovery/discovery.go | 70 ++++++++++++++++++++++++++++++++++++++ test/discovery_test.go | 44 ++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 pkg/discovery/discovery.go create mode 100644 test/discovery_test.go diff --git a/pkg/discovery/discovery.go b/pkg/discovery/discovery.go new file mode 100644 index 0000000..64816d7 --- /dev/null +++ b/pkg/discovery/discovery.go @@ -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 +} diff --git a/test/discovery_test.go b/test/discovery_test.go new file mode 100644 index 0000000..89a67e0 --- /dev/null +++ b/test/discovery_test.go @@ -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/Admin@org1.example.com/msp/keystore/priv_sk" + var SignCert = "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem" + 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) + }) + }) +})