hyperledger / fabric-admin-sdk

Fabric SDK for Admin Capability services
Apache License 2.0
31 stars 19 forks source link

How to list peers that are not anchor nodes on the channel #125

Closed 1gezhanghao closed 1 year ago

1gezhanghao commented 1 year ago

we can list anchor peers from channel‘s ConfigBlock but how to list other joined peers that are not anchor nodes

SamYuan1990 commented 1 year ago

may I know more about the use case? as channel config is "open" for everyone join this channel to get. Hence the anchor peers' information are published through channel config. For example, org1 has 2 peers. peer1 is published to other orgs for endorsement usage as anchor peer? peer2 is not listed in the channel config. Hence, for org1, as org1 admin knows those two peers, is it necessary to org1 admin to have this feature? and maybe for security reason, org1 don't want to expose peer2 to org2. Hence ... I suppose by default, there some limitations about topic discussed in this issue.

bestbeforetoday commented 1 year ago

A PeerMembershipQuery using the discovery service can return all the network organizations and their member peers.

1gezhanghao commented 1 year ago

may I know more about the use case? as channel config is "open" for everyone join this channel to get. Hence the anchor peers' information are published through channel config. For example, org1 has 2 peers. peer1 is published to other orgs for endorsement usage as anchor peer? peer2 is not listed in the channel config. Hence, for org1, as org1 admin knows those two peers, is it necessary to org1 admin to have this feature? and maybe for security reason, org1 don't want to expose peer2 to org2. Hence ... I suppose by default, there some limitations about topic discussed in this issue.

For security reasons, it is reasonable for Org 1 not to expose its non-anchor nodes to other Org. Org 1 should have a simple one-time acquisition method to know which of its nodes are in the same ledger, instead of querying each peer node to confirm.

SamYuan1990 commented 1 year ago

may I know more about the use case? as channel config is "open" for everyone join this channel to get. Hence the anchor peers' information are published through channel config. For example, org1 has 2 peers. peer1 is published to other orgs for endorsement usage as anchor peer? peer2 is not listed in the channel config. Hence, for org1, as org1 admin knows those two peers, is it necessary to org1 admin to have this feature? and maybe for security reason, org1 don't want to expose peer2 to org2. Hence ... I suppose by default, there some limitations about topic discussed in this issue.

For security reasons, it is reasonable for Org 1 not to expose its non-anchor nodes to other Org. Org 1 should have a simple one-time acquisition method to know which of its nodes are in the same ledger, instead of querying each peer node to confirm.

could you please try the api as @bestbeforetoday's suggestion?

1gezhanghao commented 1 year ago

may I know more about the use case? as channel config is "open" for everyone join this channel to get. Hence the anchor peers' information are published through channel config. For example, org1 has 2 peers. peer1 is published to other orgs for endorsement usage as anchor peer? peer2 is not listed in the channel config. Hence, for org1, as org1 admin knows those two peers, is it necessary to org1 admin to have this feature? and maybe for security reason, org1 don't want to expose peer2 to org2. Hence ... I suppose by default, there some limitations about topic discussed in this issue.

For security reasons, it is reasonable for Org 1 not to expose its non-anchor nodes to other Org. Org 1 should have a simple one-time acquisition method to know which of its nodes are in the same ledger, instead of querying each peer node to confirm.

could you please try the api as @bestbeforetoday's suggestion?

yes, it worked, the result json as follows.

{
    "peers_by_org":{
        "CqMSP":{
            "peers":[
                {
                    "state_info":{
                        "payload":"GAV6bxIUCKjFnMn364myFxCUgJ/K9+uJshcaIOOqEE3pENasJBeEcKePpyd4CewXtmPPwnO0v7GwPrX4IiAuUxNhlDl6sh6uN/3g+ai/xa7gaOcSn1ebTGtxs9fBfCoTCAEaDwoKX2xpZmVjeWNsZRIBMQ==",
                        "signature":"MEUCIQDGQY0QL7JyAwDLn6sIfuCemdOXH6nD7ICd8X8fkjo0LgIgQkz86KFEIXa3WvbjKjlXtqqbNO3G5SgihRAQO7eEPzY="
                    },
                    "membership_info":{
                        "payload":"GAEqTQo9ChlwZWVyMS5jcS5leGFtcGxlLmNvbTo3MDUxGiDjqhBN6RDWrCQXhHCnj6cneAnsF7Zjz8JztL+xsD61+BIMCNrR3bXP64myFxAb",
                        "signature":"MEQCICQ63RER7207ig9oEf6XZOHSENuPozK1TKpUP53ahqMFAiBAoTv9WRyMKCTGDSM4AKtxIdaJ2cyLlHDVarCsuhOPZQ=="
                    },
                    "identity":"CgVDcU1TUBLNBy0tLS0tQkVHSU4gQ0VSVElGSUNBVEUtLS0tLQpNSUlDb0RDQ0FrZWdBd0lCQWdJVVlBS0Z5L2JZWlNmMWR4UGxjODROUnhIYW1OTXdDZ1lJS29aSXpqMEVBd0l3CmFERUxNQWtHQTFVRUJoTUNRMDR4RVR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N0V1UG01N2NLZE5uUEZLOVRHbG4rS1BtdSsxOEwwWDhUZlgKUlZkZ2t3PT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo="
                }
            ]
        }
    }
}

the peer endpoint is in membership_info's payload, the payload's proto struct is gossip.GossipMessage, the Content of gossip.GossipMessage is *gossip.GossipMessage_AliveMsg json result as follows

{
    "AliveMsg": {
        "membership": {
            "endpoint": "peer0.cq.example.com:7051",
            "pki_id": "k67w32SfrfzhaW7Gp/fz00gi2rh99K3HO3iOa63g17M="
        },
        "timestamp": {
            "inc_num": 1685515440265200767,
            "seq_num": 1903
        }
    }
}

i will give the implements code for this

1gezhanghao commented 1 year ago

implements code as follows

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"                                                                                                                                                                                                    
        "google.golang.org/grpc"                                                                                                                                                                                                                               
        "google.golang.org/protobuf/proto"                                                                                                                                                                                                                     
)                                                                                                                                                                                                                                                              

func PeerMembershipQuery(ctx context.Context, conn *grpc.ClientConn, signer identity.SigningIdentity, channel string) (*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{                                                                                                                                                                                                                          
                &discovery.Query{                                                                                                                                                                                                                              
                        Channel: channel,                                                                                                                                                                                                                      
                        Query: &discovery.Query_PeerQuery{                                                                                                                                                                                                     
                                PeerQuery: &discovery.PeerMembershipQuery{                                                                                                                                                                                     
                                        Filter: nil,                                                                                                                                                                                                           
                                },                                                                                                                                                                                                                             
                        },                                                                                                                                                                                                                                     
                },                                                                                                                                                                                                                                             
        }                                                                                                                                                                                                                                                      

        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                                                                                                                                                                                                                                        
}
SamYuan1990 commented 1 year ago

implements code as follows

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"                                                                                                                                                                                                    
        "google.golang.org/grpc"                                                                                                                                                                                                                               
        "google.golang.org/protobuf/proto"                                                                                                                                                                                                                     
)                                                                                                                                                                                                                                                              

func PeerMembershipQuery(ctx context.Context, conn *grpc.ClientConn, signer identity.SigningIdentity, channel string) (*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{                                                                                                                                                                                                                          
                &discovery.Query{                                                                                                                                                                                                                              
                        Channel: channel,                                                                                                                                                                                                                      
                        Query: &discovery.Query_PeerQuery{                                                                                                                                                                                                     
                                PeerQuery: &discovery.PeerMembershipQuery{                                                                                                                                                                                     
                                        Filter: nil,                                                                                                                                                                                                           
                                },                                                                                                                                                                                                                             
                        },                                                                                                                                                                                                                                     
                },                                                                                                                                                                                                                                             
        }                                                                                                                                                                                                                                                      

        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                                                                                                                                                                                                                                        
}

could you please open a PR with your contribution?

1gezhanghao commented 1 year ago

implements code as follows

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"                                                                                                                                                                                                    
        "google.golang.org/grpc"                                                                                                                                                                                                                               
        "google.golang.org/protobuf/proto"                                                                                                                                                                                                                     
)                                                                                                                                                                                                                                                              

func PeerMembershipQuery(ctx context.Context, conn *grpc.ClientConn, signer identity.SigningIdentity, channel string) (*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{                                                                                                                                                                                                                          
                &discovery.Query{                                                                                                                                                                                                                              
                        Channel: channel,                                                                                                                                                                                                                      
                        Query: &discovery.Query_PeerQuery{                                                                                                                                                                                                     
                                PeerQuery: &discovery.PeerMembershipQuery{                                                                                                                                                                                     
                                        Filter: nil,                                                                                                                                                                                                           
                                },                                                                                                                                                                                                                             
                        },                                                                                                                                                                                                                                     
                },                                                                                                                                                                                                                                             
        }                                                                                                                                                                                                                                                      

        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                                                                                                                                                                                                                                        
}

could you please open a PR with your contribution?

ok,i will do this.