github.com/ewagmig/fabric@v2.1.1+incompatible/gossip/protoext/message.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  	"fmt"
    11  
    12  	"github.com/hyperledger/fabric-protos-go/gossip"
    13  )
    14  
    15  // IsAliveMsg returns whether this GossipMessage is an AliveMessage
    16  func IsAliveMsg(m *gossip.GossipMessage) bool {
    17  	return m.GetAliveMsg() != nil
    18  }
    19  
    20  // IsDataMsg returns whether this GossipMessage is a data message
    21  func IsDataMsg(m *gossip.GossipMessage) bool {
    22  	return m.GetDataMsg() != nil
    23  }
    24  
    25  // IsStateInfoPullRequestMsg returns whether this GossipMessage is a stateInfoPullRequest
    26  func IsStateInfoPullRequestMsg(m *gossip.GossipMessage) bool {
    27  	return m.GetStateInfoPullReq() != nil
    28  }
    29  
    30  // IsStateInfoSnapshot returns whether this GossipMessage is a stateInfo snapshot
    31  func IsStateInfoSnapshot(m *gossip.GossipMessage) bool {
    32  	return m.GetStateSnapshot() != nil
    33  }
    34  
    35  // IsStateInfoMsg returns whether this GossipMessage is a stateInfo message
    36  func IsStateInfoMsg(m *gossip.GossipMessage) bool {
    37  	return m.GetStateInfo() != nil
    38  }
    39  
    40  // IsPullMsg returns whether this GossipMessage is a message that belongs
    41  // to the pull mechanism
    42  func IsPullMsg(m *gossip.GossipMessage) bool {
    43  	return m.GetDataReq() != nil || m.GetDataUpdate() != nil ||
    44  		m.GetHello() != nil || m.GetDataDig() != nil
    45  }
    46  
    47  // IsRemoteStateMessage returns whether this GossipMessage is related to state synchronization
    48  func IsRemoteStateMessage(m *gossip.GossipMessage) bool {
    49  	return m.GetStateRequest() != nil || m.GetStateResponse() != nil
    50  }
    51  
    52  // GetPullMsgType returns the phase of the pull mechanism this GossipMessage belongs to
    53  // for example: Hello, Digest, etc.
    54  // If this isn't a pull message, PullMsgType_UNDEFINED is returned.
    55  func GetPullMsgType(m *gossip.GossipMessage) gossip.PullMsgType {
    56  	if helloMsg := m.GetHello(); helloMsg != nil {
    57  		return helloMsg.MsgType
    58  	}
    59  
    60  	if digMsg := m.GetDataDig(); digMsg != nil {
    61  		return digMsg.MsgType
    62  	}
    63  
    64  	if reqMsg := m.GetDataReq(); reqMsg != nil {
    65  		return reqMsg.MsgType
    66  	}
    67  
    68  	if resMsg := m.GetDataUpdate(); resMsg != nil {
    69  		return resMsg.MsgType
    70  	}
    71  
    72  	return gossip.PullMsgType_UNDEFINED
    73  }
    74  
    75  // IsChannelRestricted returns whether this GossipMessage should be routed
    76  // only in its channel
    77  func IsChannelRestricted(m *gossip.GossipMessage) bool {
    78  	return m.Tag == gossip.GossipMessage_CHAN_AND_ORG || m.Tag == gossip.GossipMessage_CHAN_ONLY || m.Tag == gossip.GossipMessage_CHAN_OR_ORG
    79  }
    80  
    81  // IsOrgRestricted returns whether this GossipMessage should be routed only
    82  // inside the organization
    83  func IsOrgRestricted(m *gossip.GossipMessage) bool {
    84  	return m.Tag == gossip.GossipMessage_CHAN_AND_ORG || m.Tag == gossip.GossipMessage_ORG_ONLY
    85  }
    86  
    87  // IsIdentityMsg returns whether this GossipMessage is an identity message
    88  func IsIdentityMsg(m *gossip.GossipMessage) bool {
    89  	return m.GetPeerIdentity() != nil
    90  }
    91  
    92  // IsDataReq returns whether this GossipMessage is a data request message
    93  func IsDataReq(m *gossip.GossipMessage) bool {
    94  	return m.GetDataReq() != nil
    95  }
    96  
    97  // IsPrivateDataMsg returns whether this message is related to private data
    98  func IsPrivateDataMsg(m *gossip.GossipMessage) bool {
    99  	return m.GetPrivateReq() != nil || m.GetPrivateRes() != nil || m.GetPrivateData() != nil
   100  }
   101  
   102  // IsAck returns whether this GossipMessage is an acknowledgement
   103  func IsAck(m *gossip.GossipMessage) bool {
   104  	return m.GetAck() != nil
   105  }
   106  
   107  // IsDataUpdate returns whether this GossipMessage is a data update message
   108  func IsDataUpdate(m *gossip.GossipMessage) bool {
   109  	return m.GetDataUpdate() != nil
   110  }
   111  
   112  // IsHelloMsg returns whether this GossipMessage is a hello message
   113  func IsHelloMsg(m *gossip.GossipMessage) bool {
   114  	return m.GetHello() != nil
   115  }
   116  
   117  // IsDigestMsg returns whether this GossipMessage is a digest message
   118  func IsDigestMsg(m *gossip.GossipMessage) bool {
   119  	return m.GetDataDig() != nil
   120  }
   121  
   122  // IsLeadershipMsg returns whether this GossipMessage is a leadership (leader election) message
   123  func IsLeadershipMsg(m *gossip.GossipMessage) bool {
   124  	return m.GetLeadershipMsg() != nil
   125  }
   126  
   127  // IsTagLegal checks the GossipMessage tags and inner type
   128  // and returns an error if the tag doesn't match the type.
   129  func IsTagLegal(m *gossip.GossipMessage) error {
   130  	if m.Tag == gossip.GossipMessage_UNDEFINED {
   131  		return fmt.Errorf("Undefined tag")
   132  	}
   133  	if IsDataMsg(m) {
   134  		if m.Tag != gossip.GossipMessage_CHAN_AND_ORG {
   135  			return fmt.Errorf("Tag should be %s", gossip.GossipMessage_Tag_name[int32(gossip.GossipMessage_CHAN_AND_ORG)])
   136  		}
   137  		return nil
   138  	}
   139  
   140  	if IsAliveMsg(m) || m.GetMemReq() != nil || m.GetMemRes() != nil {
   141  		if m.Tag != gossip.GossipMessage_EMPTY {
   142  			return fmt.Errorf("Tag should be %s", gossip.GossipMessage_Tag_name[int32(gossip.GossipMessage_EMPTY)])
   143  		}
   144  		return nil
   145  	}
   146  
   147  	if IsIdentityMsg(m) {
   148  		if m.Tag != gossip.GossipMessage_ORG_ONLY {
   149  			return fmt.Errorf("Tag should be %s", gossip.GossipMessage_Tag_name[int32(gossip.GossipMessage_ORG_ONLY)])
   150  		}
   151  		return nil
   152  	}
   153  
   154  	if IsPullMsg(m) {
   155  		switch GetPullMsgType(m) {
   156  		case gossip.PullMsgType_BLOCK_MSG:
   157  			if m.Tag != gossip.GossipMessage_CHAN_AND_ORG {
   158  				return fmt.Errorf("Tag should be %s", gossip.GossipMessage_Tag_name[int32(gossip.GossipMessage_CHAN_AND_ORG)])
   159  			}
   160  			return nil
   161  		case gossip.PullMsgType_IDENTITY_MSG:
   162  			if m.Tag != gossip.GossipMessage_EMPTY {
   163  				return fmt.Errorf("Tag should be %s", gossip.GossipMessage_Tag_name[int32(gossip.GossipMessage_EMPTY)])
   164  			}
   165  			return nil
   166  		default:
   167  			return fmt.Errorf("Invalid PullMsgType: %s", gossip.PullMsgType_name[int32(GetPullMsgType(m))])
   168  		}
   169  	}
   170  
   171  	if IsStateInfoMsg(m) || IsStateInfoPullRequestMsg(m) || IsStateInfoSnapshot(m) || IsRemoteStateMessage(m) {
   172  		if m.Tag != gossip.GossipMessage_CHAN_OR_ORG {
   173  			return fmt.Errorf("Tag should be %s", gossip.GossipMessage_Tag_name[int32(gossip.GossipMessage_CHAN_OR_ORG)])
   174  		}
   175  		return nil
   176  	}
   177  
   178  	if IsLeadershipMsg(m) {
   179  		if m.Tag != gossip.GossipMessage_CHAN_AND_ORG {
   180  			return fmt.Errorf("Tag should be %s", gossip.GossipMessage_Tag_name[int32(gossip.GossipMessage_CHAN_AND_ORG)])
   181  		}
   182  		return nil
   183  	}
   184  
   185  	return fmt.Errorf("Unknown message type: %v", m)
   186  }