github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/types/events.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	abci "github.com/badrootd/celestia-core/abci/types"
     7  	cmtjson "github.com/badrootd/celestia-core/libs/json"
     8  	cmtpubsub "github.com/badrootd/celestia-core/libs/pubsub"
     9  	cmtquery "github.com/badrootd/celestia-core/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  	EventSignedBlock         = "NewSignedBlock"
    22  	EventNewEvidence         = "NewEvidence"
    23  	EventTx                  = "Tx"
    24  	EventValidatorSetUpdates = "ValidatorSetUpdates"
    25  
    26  	// Internal consensus events.
    27  	// These are used for testing the consensus state machine.
    28  	// They can also be used to build real-time consensus visualizers.
    29  	EventCompleteProposal = "CompleteProposal"
    30  	EventLock             = "Lock"
    31  	EventNewRound         = "NewRound"
    32  	EventNewRoundStep     = "NewRoundStep"
    33  	EventPolka            = "Polka"
    34  	EventRelock           = "Relock"
    35  	EventTimeoutPropose   = "TimeoutPropose"
    36  	EventTimeoutWait      = "TimeoutWait"
    37  	EventUnlock           = "Unlock"
    38  	EventValidBlock       = "ValidBlock"
    39  	EventVote             = "Vote"
    40  )
    41  
    42  // ENCODING / DECODING
    43  
    44  // TMEventData implements events.EventData.
    45  type TMEventData interface {
    46  	// empty interface
    47  }
    48  
    49  func init() {
    50  	cmtjson.RegisterType(EventDataNewBlock{}, "tendermint/event/NewBlock")
    51  	cmtjson.RegisterType(EventDataSignedBlock{}, "tendermint/event/NewSignedBlock")
    52  	cmtjson.RegisterType(EventDataNewBlockHeader{}, "tendermint/event/NewBlockHeader")
    53  	cmtjson.RegisterType(EventDataNewEvidence{}, "tendermint/event/NewEvidence")
    54  	cmtjson.RegisterType(EventDataTx{}, "tendermint/event/Tx")
    55  	cmtjson.RegisterType(EventDataRoundState{}, "tendermint/event/RoundState")
    56  	cmtjson.RegisterType(EventDataNewRound{}, "tendermint/event/NewRound")
    57  	cmtjson.RegisterType(EventDataCompleteProposal{}, "tendermint/event/CompleteProposal")
    58  	cmtjson.RegisterType(EventDataVote{}, "tendermint/event/Vote")
    59  	cmtjson.RegisterType(EventDataValidatorSetUpdates{}, "tendermint/event/ValidatorSetUpdates")
    60  	cmtjson.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  // EventDataSignedBlock contains all the information needed to verify
    74  // the data committed in a block.
    75  type EventDataSignedBlock struct {
    76  	Header       Header       `json:"header"`
    77  	Commit       Commit       `json:"commit"`
    78  	ValidatorSet ValidatorSet `json:"validator_set"`
    79  	Data         Data         `json:"data"`
    80  }
    81  
    82  type EventDataNewBlockHeader struct {
    83  	Header Header `json:"header"`
    84  
    85  	NumTxs           int64                   `json:"num_txs"` // Number of txs in a block
    86  	ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
    87  	ResultEndBlock   abci.ResponseEndBlock   `json:"result_end_block"`
    88  }
    89  
    90  type EventDataNewEvidence struct {
    91  	Evidence Evidence `json:"evidence"`
    92  
    93  	Height int64 `json:"height"`
    94  }
    95  
    96  // All txs fire EventDataTx
    97  type EventDataTx struct {
    98  	abci.TxResult
    99  }
   100  
   101  // NOTE: This goes into the replay WAL
   102  type EventDataRoundState struct {
   103  	Height int64  `json:"height"`
   104  	Round  int32  `json:"round"`
   105  	Step   string `json:"step"`
   106  }
   107  
   108  type ValidatorInfo struct {
   109  	Address Address `json:"address"`
   110  	Index   int32   `json:"index"`
   111  }
   112  
   113  type EventDataNewRound struct {
   114  	Height int64  `json:"height"`
   115  	Round  int32  `json:"round"`
   116  	Step   string `json:"step"`
   117  
   118  	Proposer ValidatorInfo `json:"proposer"`
   119  }
   120  
   121  type EventDataCompleteProposal struct {
   122  	Height int64  `json:"height"`
   123  	Round  int32  `json:"round"`
   124  	Step   string `json:"step"`
   125  
   126  	BlockID BlockID `json:"block_id"`
   127  }
   128  
   129  type EventDataVote struct {
   130  	Vote *Vote
   131  }
   132  
   133  type EventDataString string
   134  
   135  type EventDataValidatorSetUpdates struct {
   136  	ValidatorUpdates []*Validator `json:"validator_updates"`
   137  }
   138  
   139  // PUBSUB
   140  
   141  const (
   142  	// EventTypeKey is a reserved composite key for event name.
   143  	EventTypeKey = "tm.event"
   144  	// TxHashKey is a reserved key, used to specify transaction's hash.
   145  	// see EventBus#PublishEventTx
   146  	TxHashKey = "tx.hash"
   147  	// TxHeightKey is a reserved key, used to specify transaction block's height.
   148  	// see EventBus#PublishEventTx
   149  	TxHeightKey = "tx.height"
   150  
   151  	// BlockHeightKey is a reserved key used for indexing BeginBlock and Endblock
   152  	// events.
   153  	BlockHeightKey = "block.height"
   154  
   155  	// MatchEventsKey is a reserved key used to indicate to the indexer that the
   156  	// conditions in the query have to have occurred both on the same height
   157  	// as well as in the same event
   158  	MatchEventKey = "match.events"
   159  )
   160  
   161  var (
   162  	EventQueryCompleteProposal    = QueryForEvent(EventCompleteProposal)
   163  	EventQueryLock                = QueryForEvent(EventLock)
   164  	EventQueryNewBlock            = QueryForEvent(EventNewBlock)
   165  	EventQueryNewBlockHeader      = QueryForEvent(EventNewBlockHeader)
   166  	EventQueryNewEvidence         = QueryForEvent(EventNewEvidence)
   167  	EventQueryNewRound            = QueryForEvent(EventNewRound)
   168  	EventQueryNewRoundStep        = QueryForEvent(EventNewRoundStep)
   169  	EventQueryNewSignedBlock      = QueryForEvent(EventSignedBlock)
   170  	EventQueryPolka               = QueryForEvent(EventPolka)
   171  	EventQueryRelock              = QueryForEvent(EventRelock)
   172  	EventQueryTimeoutPropose      = QueryForEvent(EventTimeoutPropose)
   173  	EventQueryTimeoutWait         = QueryForEvent(EventTimeoutWait)
   174  	EventQueryTx                  = QueryForEvent(EventTx)
   175  	EventQueryUnlock              = QueryForEvent(EventUnlock)
   176  	EventQueryValidatorSetUpdates = QueryForEvent(EventValidatorSetUpdates)
   177  	EventQueryValidBlock          = QueryForEvent(EventValidBlock)
   178  	EventQueryVote                = QueryForEvent(EventVote)
   179  )
   180  
   181  func EventQueryTxFor(tx Tx) cmtpubsub.Query {
   182  	return cmtquery.MustParse(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTx, TxHashKey, tx.Hash()))
   183  }
   184  
   185  func QueryForEvent(eventType string) cmtpubsub.Query {
   186  	return cmtquery.MustParse(fmt.Sprintf("%s='%s'", EventTypeKey, eventType))
   187  }
   188  
   189  // BlockEventPublisher publishes all block related events
   190  type BlockEventPublisher interface {
   191  	PublishEventNewBlock(block EventDataNewBlock) error
   192  	PublishEventNewSignedBlock(event EventDataSignedBlock) error
   193  	PublishEventNewBlockHeader(header EventDataNewBlockHeader) error
   194  	PublishEventNewEvidence(evidence EventDataNewEvidence) error
   195  	PublishEventTx(EventDataTx) error
   196  	PublishEventValidatorSetUpdates(EventDataValidatorSetUpdates) error
   197  }
   198  
   199  type TxEventPublisher interface {
   200  	PublishEventTx(EventDataTx) error
   201  }