github.com/franono/tendermint@v0.32.2-0.20200527150959-749313264ce9/types/events.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	amino "github.com/tendermint/go-amino"
     7  
     8  	abci "github.com/franono/tendermint/abci/types"
     9  	tmpubsub "github.com/franono/tendermint/libs/pubsub"
    10  	tmquery "github.com/franono/tendermint/libs/pubsub/query"
    11  )
    12  
    13  // Reserved event types (alphabetically sorted).
    14  const (
    15  	// Block level events for mass consumption by users.
    16  	// These events are triggered from the state package,
    17  	// after a block has been committed.
    18  	// These are also used by the tx indexer for async indexing.
    19  	// All of this data can be fetched through the rpc.
    20  	EventNewBlock            = "NewBlock"
    21  	EventNewBlockHeader      = "NewBlockHeader"
    22  	EventTx                  = "Tx"
    23  	EventValidatorSetUpdates = "ValidatorSetUpdates"
    24  
    25  	// Internal consensus events.
    26  	// These are used for testing the consensus state machine.
    27  	// They can also be used to build real-time consensus visualizers.
    28  	EventCompleteProposal = "CompleteProposal"
    29  	EventLock             = "Lock"
    30  	EventNewRound         = "NewRound"
    31  	EventNewRoundStep     = "NewRoundStep"
    32  	EventPolka            = "Polka"
    33  	EventRelock           = "Relock"
    34  	EventTimeoutPropose   = "TimeoutPropose"
    35  	EventTimeoutWait      = "TimeoutWait"
    36  	EventUnlock           = "Unlock"
    37  	EventValidBlock       = "ValidBlock"
    38  	EventVote             = "Vote"
    39  )
    40  
    41  ///////////////////////////////////////////////////////////////////////////////
    42  // ENCODING / DECODING
    43  ///////////////////////////////////////////////////////////////////////////////
    44  
    45  // TMEventData implements events.EventData.
    46  type TMEventData interface {
    47  	// empty interface
    48  }
    49  
    50  func RegisterEventDatas(cdc *amino.Codec) {
    51  	cdc.RegisterInterface((*TMEventData)(nil), nil)
    52  	cdc.RegisterConcrete(EventDataNewBlock{}, "tendermint/event/NewBlock", nil)
    53  	cdc.RegisterConcrete(EventDataNewBlockHeader{}, "tendermint/event/NewBlockHeader", nil)
    54  	cdc.RegisterConcrete(EventDataTx{}, "tendermint/event/Tx", nil)
    55  	cdc.RegisterConcrete(EventDataRoundState{}, "tendermint/event/RoundState", nil)
    56  	cdc.RegisterConcrete(EventDataNewRound{}, "tendermint/event/NewRound", nil)
    57  	cdc.RegisterConcrete(EventDataCompleteProposal{}, "tendermint/event/CompleteProposal", nil)
    58  	cdc.RegisterConcrete(EventDataVote{}, "tendermint/event/Vote", nil)
    59  	cdc.RegisterConcrete(EventDataValidatorSetUpdates{}, "tendermint/event/ValidatorSetUpdates", nil)
    60  	cdc.RegisterConcrete(EventDataString(""), "tendermint/event/ProposalString", nil)
    61  }
    62  
    63  // Most event messages are basic types (a block, a transaction)
    64  // but some (an input to a call tx or a receive) are more exotic
    65  
    66  type EventDataNewBlock struct {
    67  	Block *Block `json:"block"`
    68  
    69  	ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
    70  	ResultEndBlock   abci.ResponseEndBlock   `json:"result_end_block"`
    71  }
    72  
    73  type EventDataNewBlockHeader struct {
    74  	Header Header `json:"header"`
    75  
    76  	NumTxs           int64                   `json:"num_txs"` // Number of txs in a block
    77  	ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
    78  	ResultEndBlock   abci.ResponseEndBlock   `json:"result_end_block"`
    79  }
    80  
    81  // All txs fire EventDataTx
    82  type EventDataTx struct {
    83  	TxResult
    84  }
    85  
    86  // NOTE: This goes into the replay WAL
    87  type EventDataRoundState struct {
    88  	Height int64  `json:"height"`
    89  	Round  int    `json:"round"`
    90  	Step   string `json:"step"`
    91  }
    92  
    93  type ValidatorInfo struct {
    94  	Address Address `json:"address"`
    95  	Index   int     `json:"index"`
    96  }
    97  
    98  type EventDataNewRound struct {
    99  	Height int64  `json:"height"`
   100  	Round  int    `json:"round"`
   101  	Step   string `json:"step"`
   102  
   103  	Proposer ValidatorInfo `json:"proposer"`
   104  }
   105  
   106  type EventDataCompleteProposal struct {
   107  	Height int64  `json:"height"`
   108  	Round  int    `json:"round"`
   109  	Step   string `json:"step"`
   110  
   111  	BlockID BlockID `json:"block_id"`
   112  }
   113  
   114  type EventDataVote struct {
   115  	Vote *Vote
   116  }
   117  
   118  type EventDataString string
   119  
   120  type EventDataValidatorSetUpdates struct {
   121  	ValidatorUpdates []*Validator `json:"validator_updates"`
   122  }
   123  
   124  ///////////////////////////////////////////////////////////////////////////////
   125  // PUBSUB
   126  ///////////////////////////////////////////////////////////////////////////////
   127  
   128  const (
   129  	// EventTypeKey is a reserved composite key for event name.
   130  	EventTypeKey = "tm.event"
   131  	// TxHashKey is a reserved key, used to specify transaction's hash.
   132  	// see EventBus#PublishEventTx
   133  	TxHashKey = "tx.hash"
   134  	// TxHeightKey is a reserved key, used to specify transaction block's height.
   135  	// see EventBus#PublishEventTx
   136  	TxHeightKey = "tx.height"
   137  )
   138  
   139  var (
   140  	EventQueryCompleteProposal    = QueryForEvent(EventCompleteProposal)
   141  	EventQueryLock                = QueryForEvent(EventLock)
   142  	EventQueryNewBlock            = QueryForEvent(EventNewBlock)
   143  	EventQueryNewBlockHeader      = QueryForEvent(EventNewBlockHeader)
   144  	EventQueryNewRound            = QueryForEvent(EventNewRound)
   145  	EventQueryNewRoundStep        = QueryForEvent(EventNewRoundStep)
   146  	EventQueryPolka               = QueryForEvent(EventPolka)
   147  	EventQueryRelock              = QueryForEvent(EventRelock)
   148  	EventQueryTimeoutPropose      = QueryForEvent(EventTimeoutPropose)
   149  	EventQueryTimeoutWait         = QueryForEvent(EventTimeoutWait)
   150  	EventQueryTx                  = QueryForEvent(EventTx)
   151  	EventQueryUnlock              = QueryForEvent(EventUnlock)
   152  	EventQueryValidatorSetUpdates = QueryForEvent(EventValidatorSetUpdates)
   153  	EventQueryValidBlock          = QueryForEvent(EventValidBlock)
   154  	EventQueryVote                = QueryForEvent(EventVote)
   155  )
   156  
   157  func EventQueryTxFor(tx Tx) tmpubsub.Query {
   158  	return tmquery.MustParse(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTx, TxHashKey, tx.Hash()))
   159  }
   160  
   161  func QueryForEvent(eventType string) tmpubsub.Query {
   162  	return tmquery.MustParse(fmt.Sprintf("%s='%s'", EventTypeKey, eventType))
   163  }
   164  
   165  // BlockEventPublisher publishes all block related events
   166  type BlockEventPublisher interface {
   167  	PublishEventNewBlock(block EventDataNewBlock) error
   168  	PublishEventNewBlockHeader(header EventDataNewBlockHeader) error
   169  	PublishEventTx(EventDataTx) error
   170  	PublishEventValidatorSetUpdates(EventDataValidatorSetUpdates) error
   171  }
   172  
   173  type TxEventPublisher interface {
   174  	PublishEventTx(EventDataTx) error
   175  }