github.com/okex/exchain@v1.8.0/libs/tendermint/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/okex/exchain/libs/tendermint/abci/types"
     9  	tmpubsub "github.com/okex/exchain/libs/tendermint/libs/pubsub"
    10  	tmquery "github.com/okex/exchain/libs/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  	EventPendingTx           = "PendingTx"
    24  	EventRmPendingTx         = "RmPendingTx"
    25  	EventValidatorSetUpdates = "ValidatorSetUpdates"
    26  	EventBlockTime           = "BlockTime"
    27  	EventTxs                 = "Txs"
    28  
    29  	// Internal consensus events.
    30  	// These are used for testing the consensus state machine.
    31  	// They can also be used to build real-time consensus visualizers.
    32  	EventCompleteProposal = "CompleteProposal"
    33  	EventLock             = "Lock"
    34  	EventNewRound         = "NewRound"
    35  	EventNewRoundStep     = "NewRoundStep"
    36  	EventPolka            = "Polka"
    37  	EventRelock           = "Relock"
    38  	EventTimeoutPropose   = "TimeoutPropose"
    39  	EventTimeoutWait      = "TimeoutWait"
    40  	EventUnlock           = "Unlock"
    41  	EventValidBlock       = "ValidBlock"
    42  	EventVote             = "Vote"
    43  	EventSignVote         = "SignVote"
    44  	EventBlockPart        = "BlockPart"
    45  	EventProposeRequest   = "ProposeRequest"
    46  )
    47  
    48  type RmPendingTxReason int
    49  
    50  const (
    51  	Recheck RmPendingTxReason = iota
    52  	MinGasPrice
    53  	Confirmed
    54  )
    55  
    56  var EnableEventBlockTime = false
    57  
    58  ///////////////////////////////////////////////////////////////////////////////
    59  // ENCODING / DECODING
    60  ///////////////////////////////////////////////////////////////////////////////
    61  
    62  // TMEventData implements events.EventData.
    63  type TMEventData interface {
    64  	// empty interface
    65  }
    66  
    67  func RegisterEventDatas(cdc *amino.Codec) {
    68  	cdc.RegisterInterface((*TMEventData)(nil), nil)
    69  	cdc.RegisterConcrete(CM40EventDataNewBlock{}, "tendermint/event/NewBlock", nil)
    70  	cdc.RegisterConcrete(EventDataNewBlockHeader{}, "tendermint/event/NewBlockHeader", nil)
    71  	cdc.RegisterConcrete(EventDataTx{}, "tendermint/event/Tx", nil)
    72  	cdc.RegisterConcrete(EventDataRoundState{}, "tendermint/event/RoundState", nil)
    73  	cdc.RegisterConcrete(EventDataNewRound{}, "tendermint/event/NewRound", nil)
    74  	cdc.RegisterConcrete(EventDataCompleteProposal{}, "tendermint/event/CompleteProposal", nil)
    75  	cdc.RegisterConcrete(EventDataVote{}, "tendermint/event/Vote", nil)
    76  	cdc.RegisterConcrete(EventDataValidatorSetUpdates{}, "tendermint/event/ValidatorSetUpdates", nil)
    77  	cdc.RegisterConcrete(EventDataString(""), "tendermint/event/ProposalString", nil)
    78  }
    79  
    80  // Most event messages are basic types (a block, a transaction)
    81  // but some (an input to a call tx or a receive) are more exotic
    82  
    83  type EventDataNewBlock struct {
    84  	Block *Block `json:"block"`
    85  
    86  	ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
    87  	ResultEndBlock   abci.ResponseEndBlock   `json:"result_end_block"`
    88  }
    89  
    90  func (e EventDataNewBlock) Upgrade() interface{} {
    91  	ret := CM40EventDataNewBlock{}
    92  	return ret.From(e)
    93  }
    94  
    95  type EventDataNewBlockHeader struct {
    96  	Header Header `json:"header"`
    97  
    98  	NumTxs           int64                   `json:"num_txs"` // Number of txs in a block
    99  	ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"`
   100  	ResultEndBlock   abci.ResponseEndBlock   `json:"result_end_block"`
   101  }
   102  
   103  // All txs fire EventDataTx
   104  type EventDataTx struct {
   105  	TxResult
   106  	Nonce uint64
   107  }
   108  
   109  type EventDataTxs struct {
   110  	Height int64
   111  	//Txs     Txs
   112  	Results []*abci.ResponseDeliverTx
   113  }
   114  
   115  type EventDataRmPendingTx struct {
   116  	Hash   []byte
   117  	From   string
   118  	Nonce  uint64
   119  	Reason RmPendingTxReason
   120  }
   121  
   122  // latest blockTime
   123  type EventDataBlockTime struct {
   124  	Height    int64
   125  	TimeNow   int64
   126  	TxNum     int
   127  	Available bool
   128  }
   129  
   130  // NOTE: This goes into the replay WAL
   131  type EventDataRoundState struct {
   132  	Height int64  `json:"height"`
   133  	Round  int    `json:"round"`
   134  	Step   string `json:"step"`
   135  }
   136  
   137  type ValidatorInfo struct {
   138  	Address Address `json:"address"`
   139  	Index   int     `json:"index"`
   140  }
   141  
   142  type EventDataNewRound struct {
   143  	Height int64  `json:"height"`
   144  	Round  int    `json:"round"`
   145  	Step   string `json:"step"`
   146  
   147  	Proposer ValidatorInfo `json:"proposer"`
   148  }
   149  
   150  type EventDataCompleteProposal struct {
   151  	Height int64  `json:"height"`
   152  	Round  int    `json:"round"`
   153  	Step   string `json:"step"`
   154  
   155  	BlockID BlockID `json:"block_id"`
   156  }
   157  
   158  type EventDataVote struct {
   159  	Vote *Vote
   160  }
   161  
   162  type EventDataString string
   163  
   164  type EventDataValidatorSetUpdates struct {
   165  	ValidatorUpdates []*Validator `json:"validator_updates"`
   166  }
   167  
   168  ///////////////////////////////////////////////////////////////////////////////
   169  // PUBSUB
   170  ///////////////////////////////////////////////////////////////////////////////
   171  
   172  const (
   173  	// EventTypeKey is a reserved composite key for event name.
   174  	EventTypeKey = "tm.event"
   175  	// TxHashKey is a reserved key, used to specify transaction's hash.
   176  	// see EventBus#PublishEventTx
   177  	TxHashKey = "tx.hash"
   178  	// TxHeightKey is a reserved key, used to specify transaction block's height.
   179  	// see EventBus#PublishEventTx
   180  	TxHeightKey = "tx.height"
   181  
   182  	// BlockHeightKey is a reserved key used for indexing BeginBlock and Endblock
   183  	// events.
   184  	BlockHeightKey = "block.height"
   185  )
   186  
   187  var (
   188  	EventQueryCompleteProposal    = QueryForEvent(EventCompleteProposal)
   189  	EventQueryLock                = QueryForEvent(EventLock)
   190  	EventQueryNewBlock            = QueryForEvent(EventNewBlock)
   191  	EventQueryNewBlockHeader      = QueryForEvent(EventNewBlockHeader)
   192  	EventQueryNewRound            = QueryForEvent(EventNewRound)
   193  	EventQueryNewRoundStep        = QueryForEvent(EventNewRoundStep)
   194  	EventQueryPolka               = QueryForEvent(EventPolka)
   195  	EventQueryRelock              = QueryForEvent(EventRelock)
   196  	EventQueryTimeoutPropose      = QueryForEvent(EventTimeoutPropose)
   197  	EventQueryTimeoutWait         = QueryForEvent(EventTimeoutWait)
   198  	EventQueryTx                  = QueryForEvent(EventTx)
   199  	EventQueryUnlock              = QueryForEvent(EventUnlock)
   200  	EventQueryValidatorSetUpdates = QueryForEvent(EventValidatorSetUpdates)
   201  	EventQueryValidBlock          = QueryForEvent(EventValidBlock)
   202  	EventQueryVote                = QueryForEvent(EventVote)
   203  )
   204  
   205  func EventQueryTxFor(tx Tx, height int64) tmpubsub.Query {
   206  	return tmquery.MustParse(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTx, TxHashKey, tx.Hash(height)))
   207  }
   208  
   209  func QueryForEvent(eventType string) tmpubsub.Query {
   210  	return tmquery.MustParse(fmt.Sprintf("%s='%s'", EventTypeKey, eventType))
   211  }
   212  
   213  // BlockEventPublisher publishes all block related events
   214  type BlockEventPublisher interface {
   215  	PublishEventNewBlock(block EventDataNewBlock) error
   216  	PublishEventNewBlockHeader(header EventDataNewBlockHeader) error
   217  	PublishEventTx(EventDataTx) error
   218  	PublishEventTxs(EventDataTxs) error
   219  	PublishEventPendingTx(EventDataTx) error
   220  	PublishEventValidatorSetUpdates(EventDataValidatorSetUpdates) error
   221  	PublishEventLatestBlockTime(time EventDataBlockTime) error
   222  	PublishEventRmPendingTx(EventDataRmPendingTx) error
   223  }
   224  
   225  type TxEventPublisher interface {
   226  	PublishEventTx(EventDataTx) error
   227  	PublishEventPendingTx(EventDataTx) error
   228  	PublishEventRmPendingTx(EventDataRmPendingTx) error
   229  }