github.com/vipernet-xyz/tm@v0.34.24/types/events.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	abci "github.com/vipernet-xyz/tm/abci/types"
     7  	tmjson "github.com/vipernet-xyz/tm/libs/json"
     8  	tmpubsub "github.com/vipernet-xyz/tm/libs/pubsub"
     9  	tmquery "github.com/vipernet-xyz/tm/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  // ENCODING / DECODING
    42  
    43  // TMEventData implements events.EventData.
    44  type TMEventData interface {
    45  	// empty interface
    46  }
    47  
    48  func init() {
    49  	tmjson.RegisterType(EventDataNewBlock{}, "tendermint/event/NewBlock")
    50  	tmjson.RegisterType(EventDataNewBlockHeader{}, "tendermint/event/NewBlockHeader")
    51  	tmjson.RegisterType(EventDataNewEvidence{}, "tendermint/event/NewEvidence")
    52  	tmjson.RegisterType(EventDataTx{}, "tendermint/event/Tx")
    53  	tmjson.RegisterType(EventDataRoundState{}, "tendermint/event/RoundState")
    54  	tmjson.RegisterType(EventDataNewRound{}, "tendermint/event/NewRound")
    55  	tmjson.RegisterType(EventDataCompleteProposal{}, "tendermint/event/CompleteProposal")
    56  	tmjson.RegisterType(EventDataVote{}, "tendermint/event/Vote")
    57  	tmjson.RegisterType(EventDataValidatorSetUpdates{}, "tendermint/event/ValidatorSetUpdates")
    58  	tmjson.RegisterType(EventDataString(""), "tendermint/event/ProposalString")
    59  }
    60  
    61  // Most event messages are basic types (a block, a transaction)
    62  // but some (an input to a call tx or a receive) are more exotic
    63  
    64  type EventDataNewBlock struct {
    65  	Block *Block `json:"block"`
    66  
    67  	ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
    68  	ResultEndBlock   abci.ResponseEndBlock   `json:"result_end_block"`
    69  }
    70  
    71  type EventDataNewBlockHeader struct {
    72  	Header Header `json:"header"`
    73  
    74  	NumTxs           int64                   `json:"num_txs"` // Number of txs in a block
    75  	ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
    76  	ResultEndBlock   abci.ResponseEndBlock   `json:"result_end_block"`
    77  }
    78  
    79  type EventDataNewEvidence struct {
    80  	Evidence Evidence `json:"evidence"`
    81  
    82  	Height int64 `json:"height"`
    83  }
    84  
    85  // All txs fire EventDataTx
    86  type EventDataTx struct {
    87  	abci.TxResult
    88  }
    89  
    90  // NOTE: This goes into the replay WAL
    91  type EventDataRoundState struct {
    92  	Height int64  `json:"height"`
    93  	Round  int32  `json:"round"`
    94  	Step   string `json:"step"`
    95  }
    96  
    97  type ValidatorInfo struct {
    98  	Address Address `json:"address"`
    99  	Index   int32   `json:"index"`
   100  }
   101  
   102  type EventDataNewRound struct {
   103  	Height int64  `json:"height"`
   104  	Round  int32  `json:"round"`
   105  	Step   string `json:"step"`
   106  
   107  	Proposer ValidatorInfo `json:"proposer"`
   108  }
   109  
   110  type EventDataCompleteProposal struct {
   111  	Height int64  `json:"height"`
   112  	Round  int32  `json:"round"`
   113  	Step   string `json:"step"`
   114  
   115  	BlockID BlockID `json:"block_id"`
   116  }
   117  
   118  type EventDataVote struct {
   119  	Vote *Vote
   120  }
   121  
   122  type EventDataString string
   123  
   124  type EventDataValidatorSetUpdates struct {
   125  	ValidatorUpdates []*Validator `json:"validator_updates"`
   126  }
   127  
   128  // PUBSUB
   129  
   130  const (
   131  	// EventTypeKey is a reserved composite key for event name.
   132  	EventTypeKey = "tm.event"
   133  	// TxHashKey is a reserved key, used to specify transaction's hash.
   134  	// see EventBus#PublishEventTx
   135  	TxHashKey = "tx.hash"
   136  	// TxHeightKey is a reserved key, used to specify transaction block's height.
   137  	// see EventBus#PublishEventTx
   138  	TxHeightKey = "tx.height"
   139  
   140  	// BlockHeightKey is a reserved key used for indexing BeginBlock and Endblock
   141  	// events.
   142  	BlockHeightKey = "block.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  }