github.com/Finschia/ostracon@v1.1.5/types/events.go (about) 1 package types 2 3 import ( 4 "fmt" 5 6 abci "github.com/tendermint/tendermint/abci/types" 7 8 tmjson "github.com/Finschia/ostracon/libs/json" 9 tmpubsub "github.com/Finschia/ostracon/libs/pubsub" 10 tmquery "github.com/Finschia/ostracon/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 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 // OCEventData implements events.EventData. 45 type OCEventData interface { 46 // empty interface 47 } 48 49 func init() { 50 tmjson.RegisterType(EventDataNewBlock{}, "ostracon/event/NewBlock") 51 tmjson.RegisterType(EventDataNewBlockHeader{}, "ostracon/event/NewBlockHeader") 52 tmjson.RegisterType(EventDataNewEvidence{}, "ostracon/event/NewEvidence") 53 tmjson.RegisterType(EventDataTx{}, "ostracon/event/Tx") 54 tmjson.RegisterType(EventDataRoundState{}, "ostracon/event/RoundState") 55 tmjson.RegisterType(EventDataNewRound{}, "ostracon/event/NewRound") 56 tmjson.RegisterType(EventDataCompleteProposal{}, "ostracon/event/CompleteProposal") 57 tmjson.RegisterType(EventDataVote{}, "ostracon/event/Vote") 58 tmjson.RegisterType(EventDataValidatorSetUpdates{}, "ostracon/event/ValidatorSetUpdates") 59 tmjson.RegisterType(EventDataString(""), "ostracon/event/ProposalString") 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 type EventDataNewBlockHeader struct { 73 Header Header `json:"header"` 74 75 NumTxs int64 `json:"num_txs"` // Number of txs in a block 76 ResultBeginBlock abci.ResponseBeginBlock `json:"result_begin_block"` 77 ResultEndBlock abci.ResponseEndBlock `json:"result_end_block"` 78 } 79 80 type EventDataNewEvidence struct { 81 Evidence Evidence `json:"evidence"` 82 83 Height int64 `json:"height"` 84 } 85 86 // All txs fire EventDataTx 87 type EventDataTx struct { 88 abci.TxResult 89 } 90 91 // NOTE: This goes into the replay WAL 92 type EventDataRoundState struct { 93 Height int64 `json:"height"` 94 Round int32 `json:"round"` 95 Step string `json:"step"` 96 } 97 98 type ValidatorInfo struct { 99 Address Address `json:"address"` 100 Index int32 `json:"index"` 101 } 102 103 type EventDataNewRound struct { 104 Height int64 `json:"height"` 105 Round int32 `json:"round"` 106 Step string `json:"step"` 107 108 Proposer ValidatorInfo `json:"proposer"` 109 } 110 111 type EventDataCompleteProposal struct { 112 Height int64 `json:"height"` 113 Round int32 `json:"round"` 114 Step string `json:"step"` 115 116 BlockID BlockID `json:"block_id"` 117 } 118 119 type EventDataVote struct { 120 Vote *Vote 121 } 122 123 type EventDataString string 124 125 type EventDataValidatorSetUpdates struct { 126 ValidatorUpdates []*Validator `json:"validator_updates"` 127 } 128 129 // PUBSUB 130 131 const ( 132 // EventTypeKey is a reserved composite key for event name. 133 EventTypeKey = "tm.event" 134 // TxHashKey is a reserved key, used to specify transaction's hash. 135 // see EventBus#PublishEventTx 136 TxHashKey = "tx.hash" 137 // TxHeightKey is a reserved key, used to specify transaction block's height. 138 // see EventBus#PublishEventTx 139 TxHeightKey = "tx.height" 140 141 // BlockHeightKey is a reserved key used for indexing BeginBlock and Endblock 142 // events. 143 BlockHeightKey = "block.height" 144 ) 145 146 var ( 147 EventQueryCompleteProposal = QueryForEvent(EventCompleteProposal) 148 EventQueryLock = QueryForEvent(EventLock) 149 EventQueryNewBlock = QueryForEvent(EventNewBlock) 150 EventQueryNewBlockHeader = QueryForEvent(EventNewBlockHeader) 151 EventQueryNewEvidence = QueryForEvent(EventNewEvidence) 152 EventQueryNewRound = QueryForEvent(EventNewRound) 153 EventQueryNewRoundStep = QueryForEvent(EventNewRoundStep) 154 EventQueryPolka = QueryForEvent(EventPolka) 155 EventQueryRelock = QueryForEvent(EventRelock) 156 EventQueryTimeoutPropose = QueryForEvent(EventTimeoutPropose) 157 EventQueryTimeoutWait = QueryForEvent(EventTimeoutWait) 158 EventQueryTx = QueryForEvent(EventTx) 159 EventQueryUnlock = QueryForEvent(EventUnlock) 160 EventQueryValidatorSetUpdates = QueryForEvent(EventValidatorSetUpdates) 161 EventQueryValidBlock = QueryForEvent(EventValidBlock) 162 EventQueryVote = QueryForEvent(EventVote) 163 ) 164 165 func EventQueryTxFor(tx Tx) tmpubsub.Query { 166 return tmquery.MustParse(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTx, TxHashKey, tx.Hash())) 167 } 168 169 func QueryForEvent(eventType string) tmpubsub.Query { 170 return tmquery.MustParse(fmt.Sprintf("%s='%s'", EventTypeKey, eventType)) 171 } 172 173 // BlockEventPublisher publishes all block related events 174 type BlockEventPublisher interface { 175 PublishEventNewBlock(block EventDataNewBlock) error 176 PublishEventNewBlockHeader(header EventDataNewBlockHeader) error 177 PublishEventNewEvidence(evidence EventDataNewEvidence) error 178 PublishEventTx(EventDataTx) error 179 PublishEventValidatorSetUpdates(EventDataValidatorSetUpdates) error 180 } 181 182 type TxEventPublisher interface { 183 PublishEventTx(EventDataTx) error 184 }