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