-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implements for PeerMembershipQuery (#132)
fix #125 --------- Signed-off-by: 1gezhanghao <[email protected]>
- Loading branch information
1 parent
d4028a0
commit 8a2f81e
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |