Commit 74f01e0b authored by Artem Barger's avatar Artem Barger Committed by Gerrit Code Review
Browse files

Merge "[FAB-11097] discover: No channel info in local peers"

parents df004fae 09ddf97e
......@@ -26,7 +26,7 @@ func NewPeerCmd(stub Stub, parser ResponseParser) *PeerCmd {
}
}
// PeerCmd executes peer listing command
// PeerCmd executes channelPeer listing command
type PeerCmd struct {
stub Stub
server *string
......@@ -72,7 +72,7 @@ func (pc *PeerCmd) Execute(conf common.Config) error {
return pc.parser.ParseResponse(channel, res)
}
// PeerResponseParser parses a peer response
// PeerResponseParser parses a channelPeer response
type PeerResponseParser struct {
io.Writer
}
......@@ -90,17 +90,28 @@ func (parser *PeerResponseParser) ParseResponse(channel string, res ServiceRespo
return err
}
var peerSlices []peer
for _, p := range peers {
peerSlices = append(peerSlices, rawPeerToPeer(p))
}
b, _ := json.MarshalIndent(peerSlices, "", "\t")
channelState := channel != ""
b, _ := json.MarshalIndent(assemblePeers(peers, channelState), "", "\t")
fmt.Fprintln(parser.Writer, string(b))
return nil
}
type peer struct {
func assemblePeers(peers []*discovery.Peer, withChannelState bool) interface{} {
if withChannelState {
var peerSlices []channelPeer
for _, p := range peers {
peerSlices = append(peerSlices, rawPeerToChannelPeer(p))
}
return peerSlices
}
var peerSlices []localPeer
for _, p := range peers {
peerSlices = append(peerSlices, rawPeerToLocalPeer(p))
}
return peerSlices
}
type channelPeer struct {
MSPID string
LedgerHeight uint64
Endpoint string
......@@ -108,6 +119,12 @@ type peer struct {
Chaincodes []string
}
type localPeer struct {
MSPID string
Endpoint string
Identity string
}
type peerLister interface {
Peers() ([]*discovery.Peer, error)
}
......@@ -120,7 +137,7 @@ func (scr *simpleChannelResponse) Peers() ([]*discovery.Peer, error) {
return scr.ChannelResponse.Peers()
}
func rawPeerToPeer(p *discovery.Peer) peer {
func rawPeerToChannelPeer(p *discovery.Peer) channelPeer {
var ledgerHeight uint64
var ccs []string
if p.StateInfoMessage != nil && p.StateInfoMessage.GetStateInfo() != nil && p.StateInfoMessage.GetStateInfo().Properties != nil {
......@@ -139,7 +156,7 @@ func rawPeerToPeer(p *discovery.Peer) peer {
}
sID := &msp.SerializedIdentity{}
proto.Unmarshal(p.Identity, sID)
return peer{
return channelPeer{
MSPID: p.MSPID,
Endpoint: endpoint,
LedgerHeight: ledgerHeight,
......@@ -147,3 +164,17 @@ func rawPeerToPeer(p *discovery.Peer) peer {
Chaincodes: ccs,
}
}
func rawPeerToLocalPeer(p *discovery.Peer) localPeer {
var endpoint string
if p.AliveMessage != nil && p.AliveMessage.GetAliveMsg() != nil && p.AliveMessage.GetAliveMsg().Membership != nil {
endpoint = p.AliveMessage.GetAliveMsg().Membership.Endpoint
}
sID := &msp.SerializedIdentity{}
proto.Unmarshal(p.Identity, sID)
return localPeer{
MSPID: p.MSPID,
Endpoint: endpoint,
Identity: string(sID.IdBytes),
}
}
......@@ -91,11 +91,15 @@ func TestParsePeers(t *testing.T) {
res.On("ForChannel", "mychannel").Return(chanRes)
res.On("ForLocal").Return(locRes)
for _, channel := range []string{"mychannel", ""} {
channel2expected := map[string]string{
"mychannel": "[\n\t{\n\t\t\"MSPID\": \"Org1MSP\",\n\t\t\"LedgerHeight\": 100,\n\t\t\"Endpoint\": \"p0\",\n\t\t\"Identity\": \"identity\",\n\t\t\"Chaincodes\": [\n\t\t\t\"mycc\",\n\t\t\t\"mycc2\"\n\t\t]\n\t},\n\t{\n\t\t\"MSPID\": \"Org2MSP\",\n\t\t\"LedgerHeight\": 0,\n\t\t\"Endpoint\": \"\",\n\t\t\"Identity\": \"\",\n\t\t\"Chaincodes\": null\n\t}\n]",
"": "[\n\t{\n\t\t\"MSPID\": \"Org1MSP\",\n\t\t\"Endpoint\": \"p0\",\n\t\t\"Identity\": \"identity\"\n\t},\n\t{\n\t\t\"MSPID\": \"Org2MSP\",\n\t\t\"Endpoint\": \"\",\n\t\t\"Identity\": \"\"\n\t}\n]",
}
for channel, expected := range channel2expected {
buff.Reset()
err := parser.ParseResponse(channel, res)
assert.NoError(t, err)
expected := "[\n\t{\n\t\t\"MSPID\": \"Org1MSP\",\n\t\t\"LedgerHeight\": 100,\n\t\t\"Endpoint\": \"p0\",\n\t\t\"Identity\": \"identity\",\n\t\t\"Chaincodes\": [\n\t\t\t\"mycc\",\n\t\t\t\"mycc2\"\n\t\t]\n\t},\n\t{\n\t\t\"MSPID\": \"Org2MSP\",\n\t\t\"LedgerHeight\": 0,\n\t\t\"Endpoint\": \"\",\n\t\t\"Identity\": \"\",\n\t\t\"Chaincodes\": null\n\t}\n]"
assert.Equal(t, fmt.Sprintf("%s\n", expected), buff.String())
}
}
......
......@@ -423,31 +423,23 @@ $ discover --peerTLSCA tls/ca.crt --userKey msp/keystore/cf31339d09e8311ac9ca5ed
[
{
"MSPID": "Org1MSP",
"LedgerHeight": 0,
"Endpoint": "peer1.org1.example.com:7051",
"Identity": "-----BEGIN CERTIFICATE-----\nMIICJzCCAc6gAwIBAgIQO7zMEHlMfRhnP6Xt65jwtDAKBggqhkjOPQQDAjBzMQsw\nCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy\nYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEcMBoGA1UEAxMTY2Eu\nb3JnMS5leGFtcGxlLmNvbTAeFw0xODA2MTcxMzQ1MjFaFw0yODA2MTQxMzQ1MjFa\nMGoxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1T\nYW4gRnJhbmNpc2NvMQ0wCwYDVQQLEwRwZWVyMR8wHQYDVQQDExZwZWVyMS5vcmcx\nLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEoII9k8db/Q2g\nRHw5rk3SYw+OMFw9jNbsJJyC5ttJRvc12Dn7lQ8ZR9hW1vLQ3NtqO/couccDJcHg\nt47iHBNadaNNMEswDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwK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crkwQ==\n-----END CERTIFICATE-----\n",
"Chaincodes": null
},
{
"MSPID": "Org2MSP",
"LedgerHeight": 0,
"Endpoint": "peer0.org2.example.com:7051",
"Identity": "-----BEGIN CERTIFICATE-----\nMIICKTCCAc+gAwIBAgIRANK4WBck5gKuzTxVQIwhYMUwCgYIKoZIzj0EAwIwczEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xGTAXBgNVBAoTEG9yZzIuZXhhbXBsZS5jb20xHDAaBgNVBAMTE2Nh\nLm9yZzIuZXhhbXBsZS5jb20wHhcNMTgwNjE3MTM0NTIxWhcNMjgwNjE0MTM0NTIx\nWjBqMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMN\nU2FuIEZyYW5jaXNjbzENMAsGA1UECxMEcGVlcjEfMB0GA1UEAxMWcGVlcjAub3Jn\nMi5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABJa0gkMRqJCi\nzmx+L9xy/ecJNvdAV2zmSx5Sf2qospVAH1MYCHyudDEvkiRuBPgmCdOdwJsE0g+h\nz0nZdKq6/X+jTTBLMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMCsGA1Ud\nIwQkMCKAIFZMuZfUtY6n2iyxaVr3rl+x5lU0CdG9x7KAeYydQGTMMAoGCCqGSM49\nBAMCA0gAMEUCIQC0M9/LJ7j3I9NEPQ/B1BpnJP+UNPnGO2peVrM/mJ1nVgIgS1ZA\nA1tsxuDyllaQuHx2P+P9NDFdjXx5T08lZhxuWYM=\n-----END CERTIFICATE-----\n",
"Chaincodes": null
},
{
"MSPID": "Org2MSP",
"LedgerHeight": 0,
"Endpoint": "peer1.org2.example.com:7051",
"Identity": "-----BEGIN CERTIFICATE-----\nMIICKDCCAc+gAwIBAgIRALnNJzplCrYy4Y8CjZtqL7AwCgYIKoZIzj0EAwIwczEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xGTAXBgNVBAoTEG9yZzIuZXhhbXBsZS5jb20xHDAaBgNVBAMTE2Nh\nLm9yZzIuZXhhbXBsZS5jb20wHhcNMTgwNjE3MTM0NTIxWhcNMjgwNjE0MTM0NTIx\nWjBqMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMN\nU2FuIEZyYW5jaXNjbzENMAsGA1UECxMEcGVlcjEfMB0GA1UEAxMWcGVlcjEub3Jn\nMi5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABNDopAkHlDdu\nq10HEkdxvdpkbs7EJyqv1clvCt/YMn1hS6sM+bFDgkJKalG7s9Hg3URF0aGpy51R\nU+4F9Muo+XajTTBLMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMCsGA1Ud\nIwQkMCKAIFZMuZfUtY6n2iyxaVr3rl+x5lU0CdG9x7KAeYydQGTMMAoGCCqGSM49\nBAMCA0cAMEQCIAR4fBmIBKW2jp0HbbabVepNtl1c7+6++riIrEBnoyIVAiBBvWmI\nyG02c5hu4wPAuVQMB7AU6tGSeYaWSAAo/ExunQ==\n-----END CERTIFICATE-----\n",
"Chaincodes": null
}
]
~~~~
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment