github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/_deprecated_chains/irisnet/structsTendermint.go (about)

     1  package irisnet
     2  
     3  import (
     4  	"net"
     5  	"bufio"
     6  	"sync"
     7  	"fmt"
     8  	"time"
     9  	"errors"
    10  
    11  	amino "github.com/tendermint/go-amino"
    12  	cmn "github.com/supragya/TendermintConnector/chains/irisnet/libs/common"
    13  	flow "github.com/supragya/TendermintConnector/chains/irisnet/libs/flowrate"
    14  	"github.com/tendermint/tendermint/crypto/merkle"
    15  )
    16  
    17  type ProtocolVersion struct {
    18  	P2P   uint64 `json:"p2p"`
    19  	Block uint64 `json:"block"`
    20  	App   uint64 `json:"app"`
    21  }
    22  
    23  type DefaultNodeInfo struct {
    24  	ProtocolVersion ProtocolVersion `json:"protocol_version"`
    25  
    26  	ID_        string `json:"id"`          // authenticated identifier
    27  	ListenAddr string `json:"listen_addr"` // accepting incoming
    28  
    29  	// Check compatibility.
    30  	// Channels are HexBytes so easier to read as JSON
    31  	Network  string       `json:"network"`  // network/chain ID
    32  	Version  string       `json:"version"`  // major.minor.revision
    33  	Channels cmn.HexBytes `json:"channels"` // channels this node knows about
    34  
    35  	// ASCIIText fields
    36  	Moniker string               `json:"moniker"` // arbitrary moniker
    37  	Other   DefaultNodeInfoOther `json:"other"`   // other application specific data
    38  }
    39  
    40  // DefaultNodeInfoOther is the misc. applcation specific data
    41  type DefaultNodeInfoOther struct {
    42  	TxIndex    string `json:"tx_index"`
    43  	RPCAddress string `json:"rpc_address"`
    44  }
    45  
    46  type P2PConnection struct {
    47  	conn          net.Conn
    48  	bufConnReader *bufio.Reader
    49  	bufConnWriter *bufio.Writer
    50  	sendMonitor   *flow.Monitor
    51  	recvMonitor   *flow.Monitor
    52  	send          chan struct{}
    53  	pong          chan struct{}
    54  	// channels      []*Channel
    55  	// channelsIdx   map[byte]*Channel
    56  	errored uint32
    57  
    58  	// Closing quitSendRoutine will cause the sendRoutine to eventually quit.
    59  	// doneSendRoutine is closed when the sendRoutine actually quits.
    60  	quitSendRoutine chan struct{}
    61  	doneSendRoutine chan struct{}
    62  
    63  	// Closing quitRecvRouting will cause the recvRouting to eventually quit.
    64  	quitRecvRoutine chan struct{}
    65  
    66  	// used to ensure FlushStop and OnStop
    67  	// are safe to call concurrently.
    68  	stopMtx sync.Mutex
    69  
    70  	flushTimer *cmn.ThrottleTimer // flush writes as necessary but throttled.
    71  	pingTimer  *time.Ticker       // send pings periodically
    72  
    73  	// close conn if pong is not received in pongTimeout
    74  	pongTimer     *time.Timer
    75  	pongTimeoutCh chan bool // true - timeout, false - peer sent pong
    76  
    77  	chStatsTimer *time.Ticker // update channel stats periodically
    78  
    79  	created time.Time // time of creation
    80  
    81  	_maxPacketMsgSize int
    82  }
    83  
    84  //----------------------------------------
    85  // Packet
    86  
    87  type Packet interface {
    88  	AssertIsPacket()
    89  }
    90  
    91  func RegisterPacket(cdc *amino.Codec) {
    92  	cdc.RegisterInterface((*Packet)(nil), nil)
    93  	cdc.RegisterConcrete(PacketPing{}, "tendermint/p2p/PacketPing", nil)
    94  	cdc.RegisterConcrete(PacketPong{}, "tendermint/p2p/PacketPong", nil)
    95  	cdc.RegisterConcrete(PacketMsg{}, "tendermint/p2p/PacketMsg", nil)
    96  }
    97  
    98  func (_ PacketPing) AssertIsPacket() {}
    99  func (_ PacketPong) AssertIsPacket() {}
   100  func (_ PacketMsg) AssertIsPacket()  {}
   101  
   102  type PacketPing struct {
   103  }
   104  
   105  type PacketPong struct {
   106  }
   107  
   108  type PacketMsg struct {
   109  	ChannelID byte
   110  	EOF       byte // 1 means message ends here.
   111  	Bytes     []byte
   112  }
   113  
   114  func (mp PacketMsg) String() string {
   115  	return fmt.Sprintf("PacketMsg{%X:%X T:%X}", mp.ChannelID, mp.Bytes, mp.EOF)
   116  }
   117  
   118  // Consensus Message
   119  type ConsensusMessage interface {
   120  	ValidateBasic() error
   121  }
   122  
   123  func RegisterConsensusMessages(cdc *amino.Codec) {
   124  	cdc.RegisterInterface((*ConsensusMessage)(nil), nil)
   125  	cdc.RegisterConcrete(&NewRoundStepMessage{}, "tendermint/NewRoundStepMessage", nil)
   126  	cdc.RegisterConcrete(&NewValidBlockMessage{}, "tendermint/NewValidBlockMessage", nil)
   127  	cdc.RegisterConcrete(&ProposalMessage{}, "tendermint/Proposal", nil)
   128  	cdc.RegisterConcrete(&ProposalPOLMessage{}, "tendermint/ProposalPOL", nil)
   129  	cdc.RegisterConcrete(&BlockPartMessage{}, "tendermint/BlockPart", nil)
   130  	cdc.RegisterConcrete(&VoteMessage{}, "tendermint/Vote", nil)
   131  	cdc.RegisterConcrete(&HasVoteMessage{}, "tendermint/HasVote", nil)
   132  	cdc.RegisterConcrete(&VoteSetMaj23Message{}, "tendermint/VoteSetMaj23", nil)
   133  	cdc.RegisterConcrete(&VoteSetBitsMessage{}, "tendermint/VoteSetBits", nil)
   134  }
   135  
   136  //-------------------------------------
   137  
   138  // NewRoundStepMessage is sent for every step taken in the ConsensusState.
   139  // For every height/round/step transition
   140  type NewRoundStepMessage struct {
   141  	Height                int64
   142  	Round                 int
   143  	Step                  uint8
   144  	SecondsSinceStartTime int
   145  	LastCommitRound       int
   146  }
   147  
   148  // ValidateBasic performs basic validation.
   149  func (m *NewRoundStepMessage) ValidateBasic() error {
   150  	if m.Height < 0 {
   151  		return errors.New("Negative Height")
   152  	}
   153  	if m.Round < 0 {
   154  		return errors.New("Negative Round")
   155  	}
   156  	// if !m.Step.IsValid() {
   157  	// 	return errors.New("Invalid Step")
   158  	// }
   159  
   160  	// NOTE: SecondsSinceStartTime may be negative
   161  
   162  	if (m.Height == 1 && m.LastCommitRound != -1) ||
   163  		(m.Height > 1 && m.LastCommitRound < -1) {
   164  		return errors.New("Invalid LastCommitRound (for 1st block: -1, for others: >= 0)")
   165  	}
   166  	return nil
   167  }
   168  
   169  // String returns a string representation.
   170  func (m *NewRoundStepMessage) String() string {
   171  	return fmt.Sprintf("[NewRoundStep H:%v R:%v S:%v LCR:%v]",
   172  		m.Height, m.Round, m.Step, m.LastCommitRound)
   173  }
   174  
   175  //-------------------------------------
   176  
   177  // NewValidBlockMessage is sent when a validator observes a valid block B in some round r,
   178  //i.e., there is a Proposal for block B and 2/3+ prevotes for the block B in the round r.
   179  // In case the block is also committed, then IsCommit flag is set to true.
   180  type PartSetHeader struct {
   181  	Total int          `json:"total"`
   182  	Hash  cmn.HexBytes `json:"hash"`
   183  }
   184  
   185  type BitArray struct {
   186  	mtx   sync.Mutex
   187  	Bits  int      `json:"bits"`  // NOTE: persisted via reflect, must be exported
   188  	Elems []uint64 `json:"elems"` // NOTE: persisted via reflect, must be exported
   189  }
   190  
   191  // Size returns the number of bits in the bitarray
   192  func (bA *BitArray) Size() int {
   193  	if bA == nil {
   194  		return 0
   195  	}
   196  	return bA.Bits
   197  }
   198  
   199  type NewValidBlockMessage struct {
   200  	Height           int64
   201  	Round            int
   202  	BlockPartsHeader PartSetHeader
   203  	BlockParts       BitArray
   204  	IsCommit         bool
   205  }
   206  
   207  // ValidateBasic performs basic validation.
   208  func (m *NewValidBlockMessage) ValidateBasic() error {
   209  	if m.Height < 0 {
   210  		return errors.New("Negative Height")
   211  	}
   212  	if m.Round < 0 {
   213  		return errors.New("Negative Round")
   214  	}
   215  	// if err := m.BlockPartsHeader.ValidateBasic(); err != nil {
   216  	// 	return fmt.Errorf("Wrong BlockPartsHeader: %v", err)
   217  	// }
   218  	if m.BlockParts.Size() == 0 {
   219  		return errors.New("Empty BlockParts")
   220  	}
   221  	if m.BlockParts.Size() != m.BlockPartsHeader.Total {
   222  		return fmt.Errorf("BlockParts bit array size %d not equal to BlockPartsHeader.Total %d",
   223  			m.BlockParts.Size(),
   224  			m.BlockPartsHeader.Total)
   225  	}
   226  	// if m.BlockParts.Size() > types.MaxBlockPartsCount {
   227  	// 	return errors.Errorf("BlockParts bit array is too big: %d, max: %d", m.BlockParts.Size(), types.MaxBlockPartsCount)
   228  	// }
   229  	return nil
   230  }
   231  
   232  // String returns a string representation.
   233  func (m *NewValidBlockMessage) String() string {
   234  	return fmt.Sprintf("[ValidBlockMessage H:%v R:%v BP:%v BA:%v IsCommit:%v]",
   235  		m.Height, m.Round, m.BlockPartsHeader, m.BlockParts, m.IsCommit)
   236  }
   237  
   238  //-------------------------------------
   239  
   240  type Proposal struct {
   241  	Type      byte
   242  	Height    int64     `json:"height"`
   243  	Round     int       `json:"round"`
   244  	POLRound  int       `json:"pol_round"` // -1 if null.
   245  	BlockID   BlockID   `json:"block_id"`
   246  	Timestamp time.Time `json:"timestamp"`
   247  	Signature []byte    `json:"signature"`
   248  }
   249  
   250  type BlockID struct {
   251  	Hash        cmn.HexBytes  `json:"hash"`
   252  	PartsHeader PartSetHeader `json:"parts"`
   253  }
   254  
   255  // ProposalMessage is sent when a new block is proposed.
   256  type ProposalMessage struct {
   257  	Proposal Proposal
   258  }
   259  
   260  // ValidateBasic performs basic validation.
   261  func (m *ProposalMessage) ValidateBasic() error {
   262  	return nil
   263  }
   264  
   265  // String returns a string representation.
   266  func (m *ProposalMessage) String() string {
   267  	return fmt.Sprintf("[Proposal %v]", m.Proposal)
   268  }
   269  
   270  //-------------------------------------
   271  
   272  // ProposalPOLMessage is sent when a previous proposal is re-proposed.
   273  type ProposalPOLMessage struct {
   274  	Height           int64
   275  	ProposalPOLRound int
   276  	ProposalPOL      *cmn.BitArray
   277  }
   278  
   279  // ValidateBasic performs basic validation.
   280  func (m *ProposalPOLMessage) ValidateBasic() error {
   281  	if m.Height < 0 {
   282  		return errors.New("Negative Height")
   283  	}
   284  	if m.ProposalPOLRound < 0 {
   285  		return errors.New("Negative ProposalPOLRound")
   286  	}
   287  	if m.ProposalPOL.Size() == 0 {
   288  		return errors.New("Empty ProposalPOL bit array")
   289  	}
   290  	// if m.ProposalPOL.Size() > types.MaxVotesCount {
   291  	// 	return errors.Errorf("ProposalPOL bit array is too big: %d, max: %d", m.ProposalPOL.Size(), types.MaxVotesCount)
   292  	// }
   293  	return nil
   294  }
   295  
   296  // String returns a string representation.
   297  func (m *ProposalPOLMessage) String() string {
   298  	return fmt.Sprintf("[ProposalPOL H:%v POLR:%v POL:%v]", m.Height, m.ProposalPOLRound, m.ProposalPOL)
   299  }
   300  
   301  //-------------------------------------
   302  
   303  type Part struct {
   304  	Index int                `json:"index"`
   305  	Bytes cmn.HexBytes       `json:"bytes"`
   306  	Proof merkle.SimpleProof `json:"proof"`
   307  
   308  	// Cache
   309  	hash []byte
   310  }
   311  
   312  // BlockPartMessage is sent when gossipping a piece of the proposed block.
   313  type BlockPartMessage struct {
   314  	Height int64
   315  	Round  int
   316  	Part   Part
   317  }
   318  
   319  // ValidateBasic performs basic validation.
   320  func (m *BlockPartMessage) ValidateBasic() error {
   321  	if m.Height < 0 {
   322  		return errors.New("Negative Height")
   323  	}
   324  	if m.Round < 0 {
   325  		return errors.New("Negative Round")
   326  	}
   327  	// if err := m.Part.ValidateBasic(); err != nil {
   328  	// 	return fmt.Errorf("Wrong Part: %v", err)
   329  	// }
   330  	return nil
   331  }
   332  
   333  // String returns a string representation.
   334  func (m *BlockPartMessage) String() string {
   335  	return fmt.Sprintf("[BlockPart H:%v R:%v P:%v]", m.Height, m.Round, m.Part)
   336  }
   337  
   338  //-------------------------------------
   339  
   340  // Vote represents a prevote, precommit, or commit vote from validators for
   341  // consensus.
   342  type Vote struct {
   343  	Type             byte         `json:"type"`
   344  	Height           int64        `json:"height"`
   345  	Round            int          `json:"round"`
   346  	BlockID          BlockID      `json:"block_id"` // zero if vote is nil.
   347  	Timestamp        time.Time    `json:"timestamp"`
   348  	ValidatorAddress cmn.HexBytes `json:"validator_address"`
   349  	ValidatorIndex   int          `json:"validator_index"`
   350  	Signature        []byte       `json:"signature"`
   351  }
   352  
   353  // VoteMessage is sent when voting for a proposal (or lack thereof).
   354  type VoteMessage struct {
   355  	Vote Vote
   356  }
   357  
   358  // ValidateBasic performs basic validation.
   359  func (m *VoteMessage) ValidateBasic() error {
   360  	return nil
   361  }
   362  
   363  // String returns a string representation.
   364  func (m *VoteMessage) String() string {
   365  	return fmt.Sprintf("[Vote %v]", m.Vote)
   366  }
   367  
   368  //-------------------------------------
   369  
   370  // HasVoteMessage is sent to indicate that a particular vote has been received.
   371  
   372  type HasVoteMessage struct {
   373  	Height int64
   374  	Round  int
   375  	Type   byte
   376  	Index  int
   377  }
   378  
   379  // ValidateBasic performs basic validation.
   380  func (m *HasVoteMessage) ValidateBasic() error {
   381  	if m.Height < 0 {
   382  		return errors.New("Negative Height")
   383  	}
   384  	if m.Round < 0 {
   385  		return errors.New("Negative Round")
   386  	}
   387  	// if !types.IsVoteTypeValid(m.Type) {
   388  	// 	return errors.New("Invalid Type")
   389  	// }
   390  	if m.Index < 0 {
   391  		return errors.New("Negative Index")
   392  	}
   393  	return nil
   394  }
   395  
   396  // String returns a string representation.
   397  func (m *HasVoteMessage) String() string {
   398  	return fmt.Sprintf("[HasVote VI:%v V:{%v/%02d/%v}]", m.Index, m.Height, m.Round, m.Type)
   399  }
   400  
   401  //-------------------------------------
   402  
   403  // VoteSetMaj23Message is sent to indicate that a given BlockID has seen +2/3 votes.
   404  type VoteSetMaj23Message struct {
   405  	Height  int64
   406  	Round   int
   407  	Type    byte
   408  	BlockID BlockID
   409  }
   410  
   411  // ValidateBasic performs basic validation.
   412  func (m *VoteSetMaj23Message) ValidateBasic() error {
   413  	if m.Height < 0 {
   414  		return errors.New("Negative Height")
   415  	}
   416  	if m.Round < 0 {
   417  		return errors.New("Negative Round")
   418  	}
   419  	// if !types.IsVoteTypeValid(m.Type) {
   420  	// 	return errors.New("Invalid Type")
   421  	// }
   422  	// if err := m.BlockID.ValidateBasic(); err != nil {
   423  	// 	return fmt.Errorf("Wrong BlockID: %v", err)
   424  	// }
   425  	return nil
   426  }
   427  
   428  // String returns a string representation.
   429  func (m *VoteSetMaj23Message) String() string {
   430  	return fmt.Sprintf("[VSM23 %v/%02d/%v %v]", m.Height, m.Round, m.Type, m.BlockID)
   431  }
   432  
   433  //-------------------------------------
   434  
   435  // VoteSetBitsMessage is sent to communicate the bit-array of votes seen for the BlockID.
   436  type VoteSetBitsMessage struct {
   437  	Height  int64
   438  	Round   int
   439  	Type    byte
   440  	BlockID BlockID
   441  	Votes   *cmn.BitArray
   442  }
   443  
   444  // ValidateBasic performs basic validation.
   445  func (m *VoteSetBitsMessage) ValidateBasic() error {
   446  	if m.Height < 0 {
   447  		return errors.New("Negative Height")
   448  	}
   449  	if m.Round < 0 {
   450  		return errors.New("Negative Round")
   451  	}
   452  	// if !types.IsVoteTypeValid(m.Type) {
   453  	// 	return errors.New("Invalid Type")
   454  	// }
   455  	// if err := m.BlockID.ValidateBasic(); err != nil {
   456  	// 	return fmt.Errorf("Wrong BlockID: %v", err)
   457  	// }
   458  	// NOTE: Votes.Size() can be zero if the node does not have any
   459  	// if m.Votes.Size() > types.MaxVotesCount {
   460  	// 	return fmt.Errorf("Votes bit array is too big: %d, max: %d", m.Votes.Size(), types.MaxVotesCount)
   461  	// }
   462  	return nil
   463  }
   464  
   465  // String returns a string representation.
   466  func (m *VoteSetBitsMessage) String() string {
   467  	return fmt.Sprintf("[VSB %v/%02d/%v %v %v]", m.Height, m.Round, m.Type, m.BlockID, m.Votes)
   468  }
   469  
   470  const (
   471  	channelBc   = byte(0x40) //bc.BlockchainChannel,
   472  	channelCsSt = byte(0x20) //cs.StateChannel,
   473  	channelCsDc = byte(0x21) //cs.DataChannel,
   474  	channelCsVo = byte(0x22) //cs.VoteChannel,
   475  	channelCsVs = byte(0x23) //cs.VoteSetBitsChannel,
   476  	channelMm   = byte(0x30) //mempl.MempoolChannel,
   477  	channelEv   = byte(0x38) //evidence.EvidenceChannel,
   478  )