github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/types/events.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	abci "github.com/tendermint/tendermint/abci/types"
     7  	tmjson "github.com/tendermint/tendermint/libs/json"
     8  	tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
     9  	tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
    10  )
    11  
    12  // Reserved event types (alphabetically sorted).
    13  const (
    14  	// Block level events for mass consumption by users.
    15  	// These events are triggered from the state package,
    16  	// after a block has been committed.
    17  	// These are also used by the tx indexer for async indexing.
    18  	// All of this data can be fetched through the rpc.
    19  	EventNewBlock            = "NewBlock"
    20  	EventNewBlockHeader      = "NewBlockHeader"
    21  	EventNewEvidence         = "NewEvidence"
    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 init() {
    51  	tmjson.RegisterType(EventDataNewBlock{}, "tendermint/event/NewBlock")
    52  	tmjson.RegisterType(EventDataNewBlockHeader{}, "tendermint/event/NewBlockHeader")
    53  	tmjson.RegisterType(EventDataNewEvidence{}, "tendermint/event/NewEvidence")
    54  	tmjson.RegisterType(EventDataTx{}, "tendermint/event/Tx")
    55  	tmjson.RegisterType(EventDataRoundState{}, "tendermint/event/RoundState")
    56  	tmjson.RegisterType(EventDataNewRound{}, "tendermint/event/NewRound")
    57  	tmjson.RegisterType(EventDataCompleteProposal{}, "tendermint/event/CompleteProposal")
    58  	tmjson.RegisterType(EventDataVote{}, "tendermint/event/Vote")
    59  	tmjson.RegisterType(EventDataValidatorSetUpdates{}, "tendermint/event/ValidatorSetUpdates")
    60  	tmjson.RegisterType(EventDataString(""), "tendermint/event/ProposalString")
    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  type EventDataNewEvidence struct {
    82  	Evidence Evidence `json:"evidence"`
    83  
    84  	Height int64 `json:"height"`
    85  }
    86  
    87  // All txs fire EventDataTx
    88  type EventDataTx struct {
    89  	abci.TxResult
    90  }
    91  
    92  // NOTE: This goes into the replay WAL
    93  type EventDataRoundState struct {
    94  	Height int64  `json:"height"`
    95  	Round  int32  `json:"round"`
    96  	Step   string `json:"step"`
    97  }
    98  
    99  type ValidatorInfo struct {
   100  	Address Address `json:"address"`
   101  	Index   int32   `json:"index"`
   102  }
   103  
   104  type EventDataNewRound struct {
   105  	Height int64  `json:"height"`
   106  	Round  int32  `json:"round"`
   107  	Step   string `json:"step"`
   108  
   109  	Proposer ValidatorInfo `json:"proposer"`
   110  }
   111  
   112  type EventDataCompleteProposal struct {
   113  	Height int64  `json:"height"`
   114  	Round  int32  `json:"round"`
   115  	Step   string `json:"step"`
   116  
   117  	BlockID BlockID `json:"block_id"`
   118  }
   119  
   120  type EventDataVote struct {
   121  	Vote *Vote
   122  }
   123  
   124  type EventDataString string
   125  
   126  type EventDataValidatorSetUpdates struct {
   127  	ValidatorUpdates []*Validator `json:"validator_updates"`
   128  }
   129  
   130  ///////////////////////////////////////////////////////////////////////////////
   131  // PUBSUB
   132  ///////////////////////////////////////////////////////////////////////////////
   133  
   134  const (
   135  	// EventTypeKey is a reserved composite key for event name.
   136  	EventTypeKey = "tm.event"
   137  	// TxHashKey is a reserved key, used to specify transaction's hash.
   138  	// see EventBus#PublishEventTx
   139  	TxHashKey = "tx.hash"
   140  	// TxHeightKey is a reserved key, used to specify transaction block's height.
   141  	// see EventBus#PublishEventTx
   142  	TxHeightKey = "tx.height"
   143  )
   144  
   145  var (
   146  	EventQueryCompleteProposal    = QueryForEvent(EventCompleteProposal)
   147  	EventQueryLock                = QueryForEvent(EventLock)
   148  	EventQueryNewBlock            = QueryForEvent(EventNewBlock)
   149  	EventQueryNewBlockHeader      = QueryForEvent(EventNewBlockHeader)
   150  	EventQueryNewEvidence         = QueryForEvent(EventNewEvidence)
   151  	EventQueryNewRound            = QueryForEvent(EventNewRound)
   152  	EventQueryNewRoundStep        = QueryForEvent(EventNewRoundStep)
   153  	EventQueryPolka               = QueryForEvent(EventPolka)
   154  	EventQueryRelock              = QueryForEvent(EventRelock)
   155  	EventQueryTimeoutPropose      = QueryForEvent(EventTimeoutPropose)
   156  	EventQueryTimeoutWait         = QueryForEvent(EventTimeoutWait)
   157  	EventQueryTx                  = QueryForEvent(EventTx)
   158  	EventQueryUnlock              = QueryForEvent(EventUnlock)
   159  	EventQueryValidatorSetUpdates = QueryForEvent(EventValidatorSetUpdates)
   160  	EventQueryValidBlock          = QueryForEvent(EventValidBlock)
   161  	EventQueryVote                = QueryForEvent(EventVote)
   162  )
   163  
   164  func EventQueryTxFor(tx Tx) tmpubsub.Query {
   165  	return tmquery.MustParse(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTx, TxHashKey, tx.Hash()))
   166  }
   167  
   168  func QueryForEvent(eventType string) tmpubsub.Query {
   169  	return tmquery.MustParse(fmt.Sprintf("%s='%s'", EventTypeKey, eventType))
   170  }
   171  
   172  // BlockEventPublisher publishes all block related events
   173  type BlockEventPublisher interface {
   174  	PublishEventNewBlock(block EventDataNewBlock) error
   175  	PublishEventNewBlockHeader(header EventDataNewBlockHeader) error
   176  	PublishEventNewEvidence(evidence EventDataNewEvidence) error
   177  	PublishEventTx(EventDataTx) error
   178  	PublishEventValidatorSetUpdates(EventDataValidatorSetUpdates) error
   179  }
   180  
   181  type TxEventPublisher interface {
   182  	PublishEventTx(EventDataTx) error
   183  }