github.com/lzy4123/fabric@v2.1.1+incompatible/gossip/protoext/stringers.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package protoext
     8  
     9  import (
    10  	"encoding/hex"
    11  	"fmt"
    12  
    13  	"github.com/golang/protobuf/proto"
    14  	"github.com/hyperledger/fabric-protos-go/gossip"
    15  	"github.com/hyperledger/fabric-protos-go/msp"
    16  )
    17  
    18  // MemberToString prints Endpoint and PKI-id
    19  func MemberToString(m *gossip.Member) string {
    20  	return fmt.Sprint("Membership: Endpoint:", m.Endpoint, " PKI-id:", hex.EncodeToString(m.PkiId))
    21  }
    22  
    23  // MembershipResponseToString of MembershipResponse prints number of Alive and number of Dead
    24  func MembershipResponseToString(mr *gossip.MembershipResponse) string {
    25  	return fmt.Sprintf("MembershipResponse with Alive: %d, Dead: %d", len(mr.Alive), len(mr.Dead))
    26  }
    27  
    28  // AliveMessageToString of AliveMessage prints Alive Message, Identity and Timestamp
    29  func AliveMessageToString(am *gossip.AliveMessage) string {
    30  	if am.Membership == nil {
    31  		return "nil Membership"
    32  	}
    33  	var sI string
    34  	serializeIdentity := &msp.SerializedIdentity{}
    35  	if err := proto.Unmarshal(am.Identity, serializeIdentity); err == nil {
    36  		sI = serializeIdentity.Mspid + string(serializeIdentity.IdBytes)
    37  	}
    38  	return fmt.Sprint("Alive Message:", MemberToString(am.Membership), "Identity:", sI, "Timestamp:", am.Timestamp)
    39  }
    40  
    41  // PayloadToString prints Block message: Data and seq
    42  func PayloadToString(p *gossip.Payload) string {
    43  	return fmt.Sprintf("Block message: {Data: %d bytes, seq: %d}", len(p.Data), p.SeqNum)
    44  }
    45  
    46  // DataUpdateToString prints Type, items and nonce
    47  func DataUpdateToString(du *gossip.DataUpdate) string {
    48  	mType := gossip.PullMsgType_name[int32(du.MsgType)]
    49  	return fmt.Sprintf("Type: %s, items: %d, nonce: %d", mType, len(du.Data), du.Nonce)
    50  }
    51  
    52  // StateInfoSnapshotToString prints items
    53  func StateInfoSnapshotToString(sis *gossip.StateInfoSnapshot) string {
    54  	return fmt.Sprintf("StateInfoSnapshot with %d items", len(sis.Elements))
    55  }
    56  
    57  // MembershipRequestToString prints self information
    58  func MembershipRequestToString(mr *gossip.MembershipRequest) string {
    59  	if mr.SelfInformation == nil {
    60  		return ""
    61  	}
    62  	signGM, err := EnvelopeToGossipMessage(mr.SelfInformation)
    63  	if err != nil {
    64  		return ""
    65  	}
    66  	return fmt.Sprintf("Membership Request with self information of %s ", signGM.String())
    67  }
    68  
    69  // StateInfoPullRequestToString prints Channel MAC
    70  func StateInfoPullRequestToString(sipr *gossip.StateInfoPullRequest) string {
    71  	return fmt.Sprint("state_info_pull_req: Channel MAC:", hex.EncodeToString(sipr.Channel_MAC))
    72  }
    73  
    74  // StateInfoToString prints Timestamp and PKI-id
    75  func StateInfoToString(si *gossip.StateInfo) string {
    76  	return fmt.Sprint("state_info_message: Timestamp:", si.Timestamp, "PKI-id:", hex.EncodeToString(si.PkiId),
    77  		" channel MAC:", hex.EncodeToString(si.Channel_MAC), " properties:", si.Properties)
    78  }
    79  
    80  // formatDigests formats digest byte arrays into strings depending on the message type
    81  func formatDigests(msgType gossip.PullMsgType, givenDigests [][]byte) []string {
    82  	var digests []string
    83  	switch msgType {
    84  	case gossip.PullMsgType_BLOCK_MSG:
    85  		for _, digest := range givenDigests {
    86  			digests = append(digests, string(digest))
    87  		}
    88  	case gossip.PullMsgType_IDENTITY_MSG:
    89  		for _, digest := range givenDigests {
    90  			digests = append(digests, hex.EncodeToString(digest))
    91  		}
    92  
    93  	}
    94  	return digests
    95  }
    96  
    97  // DataDigestToString prints nonce, msg_type and digests
    98  func DataDigestToString(dig *gossip.DataDigest) string {
    99  	var digests []string
   100  	digests = formatDigests(dig.MsgType, dig.Digests)
   101  	return fmt.Sprintf("data_dig: nonce: %d , Msg_type: %s, digests: %v", dig.Nonce, dig.MsgType, digests)
   102  }
   103  
   104  // DataRequestToString prints nonce, msg_type and digests
   105  func DataRequestToString(dataReq *gossip.DataRequest) string {
   106  	var digests []string
   107  	digests = formatDigests(dataReq.MsgType, dataReq.Digests)
   108  	return fmt.Sprintf("data request: nonce: %d , Msg_type: %s, digests: %v", dataReq.Nonce, dataReq.MsgType, digests)
   109  }
   110  
   111  // LeadershipMessageToString prints PKI-id, Timestamp and Is Declaration
   112  func LeadershipMessageToString(lm *gossip.LeadershipMessage) string {
   113  	return fmt.Sprint("Leadership Message: PKI-id:", hex.EncodeToString(lm.PkiId), " Timestamp:", lm.Timestamp,
   114  		"Is Declaration ", lm.IsDeclaration)
   115  }
   116  
   117  // RemovePvtDataResponseToString returns a string representation of this RemotePvtDataResponse
   118  func RemovePvtDataResponseToString(res *gossip.RemotePvtDataResponse) string {
   119  	a := make([]string, len(res.Elements))
   120  	for i, el := range res.Elements {
   121  		a[i] = fmt.Sprintf("%s with %d elements", el.Digest.String(), len(el.Payload))
   122  	}
   123  	return fmt.Sprintf("%v", a)
   124  }