github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/types/events.go (about) 1 package types 2 3 import ( 4 // for registering TMEventData as events.EventData 5 ethTypes "github.com/intfoundation/intchain/core/types" 6 . "github.com/intfoundation/go-common" 7 "github.com/intfoundation/go-events" 8 "github.com/intfoundation/go-wire" 9 ) 10 11 // Functions to generate eventId strings 12 13 // Reserved 14 func EventStringBond() string { return "Bond" } 15 func EventStringUnbond() string { return "Unbond" } 16 func EventStringRebond() string { return "Rebond" } 17 func EventStringDupeout() string { return "Dupeout" } 18 func EventStringFork() string { return "Fork" } 19 func EventStringTx(tx Tx) string { return Fmt("Tx:%X", tx.Hash()) } 20 21 func EventStringNewBlock() string { return "NewBlock" } 22 func EventStringNewBlockHeader() string { return "NewBlockHeader" } 23 func EventStringNewRound() string { return "NewRound" } 24 func EventStringNewRoundStep() string { return "NewRoundStep" } 25 func EventStringTimeoutPropose() string { return "TimeoutPropose" } 26 func EventStringCompleteProposal() string { return "CompleteProposal" } 27 func EventStringPolka() string { return "Polka" } 28 func EventStringUnlock() string { return "Unlock" } 29 func EventStringLock() string { return "Lock" } 30 func EventStringRelock() string { return "Relock" } 31 func EventStringTimeoutWait() string { return "TimeoutWait" } 32 func EventStringVote() string { return "Vote" } 33 func EventStringSignAggr() string { return "SignAggr" } 34 func EventStringVote2Proposer() string { return "Vote2Proposer" } 35 func EventStringProposal() string { return "Proposal" } 36 func EventStringBlockPart() string { return "BlockPart" } 37 func EventStringProposalBlockParts() string { return "Proposal_BlockParts" } 38 39 func EventStringRequest() string { return "Request" } 40 func EventStringMessage() string { return "Message" } 41 func EventStringFinalCommitted() string { return "FinalCommitted" } 42 43 //---------------------------------------- 44 45 // implements events.EventData 46 type TMEventData interface { 47 events.EventData 48 AssertIsTMEventData() 49 } 50 51 const ( 52 EventDataTypeNewBlock = byte(0x01) 53 EventDataTypeFork = byte(0x02) 54 EventDataTypeTx = byte(0x03) 55 EventDataTypeNewBlockHeader = byte(0x04) 56 57 EventDataTypeRoundState = byte(0x11) 58 EventDataTypeVote = byte(0x12) 59 EventDataTypeSignAggr = byte(0x13) 60 EventDataTypeVote2Proposer = byte(0x14) 61 62 EventDataTypeRequest = byte(0x21) 63 EventDataTypeMessage = byte(0x22) 64 EventDataTypeFinalCommitted = byte(0x23) 65 ) 66 67 var _ = wire.RegisterInterface( 68 struct{ TMEventData }{}, 69 wire.ConcreteType{EventDataNewBlock{}, EventDataTypeNewBlock}, 70 wire.ConcreteType{EventDataNewBlockHeader{}, EventDataTypeNewBlockHeader}, 71 // wire.ConcreteType{EventDataFork{}, EventDataTypeFork }, 72 wire.ConcreteType{EventDataTx{}, EventDataTypeTx}, 73 wire.ConcreteType{EventDataRoundState{}, EventDataTypeRoundState}, 74 wire.ConcreteType{EventDataVote{}, EventDataTypeVote}, 75 wire.ConcreteType{EventDataSignAggr{}, EventDataTypeSignAggr}, 76 wire.ConcreteType{EventDataVote2Proposer{}, EventDataTypeVote2Proposer}, 77 78 wire.ConcreteType{EventDataRequest{}, EventDataTypeRequest}, 79 wire.ConcreteType{EventDataMessage{}, EventDataTypeMessage}, 80 wire.ConcreteType{EventDataFinalCommitted{}, EventDataTypeFinalCommitted}, 81 ) 82 83 // Most event messages are basic types (a block, a transaction) 84 // but some (an input to a call tx or a receive) are more exotic 85 86 type EventDataNewBlock struct { 87 Block *TdmBlock `json:"block"` 88 } 89 90 // light weight event for benchmarking 91 type EventDataNewBlockHeader struct { 92 Height int `json:"height"` 93 } 94 95 // All txs fire EventDataTx 96 type EventDataTx struct { 97 Height int `json:"height"` 98 Tx Tx `json:"tx"` 99 Data []byte `json:"data"` 100 Log string `json:"log"` 101 Error string `json:"error"` // this is redundant information for now 102 } 103 104 // NOTE: This goes into the replay WAL 105 type EventDataRoundState struct { 106 Height uint64 `json:"height"` 107 Round int `json:"round"` 108 Step string `json:"step"` 109 110 // private, not exposed to websockets 111 RoundState interface{} `json:"-"` 112 } 113 114 type EventDataVote struct { 115 Vote *Vote 116 } 117 118 type EventDataSignAggr struct { 119 SignAggr *SignAggr 120 } 121 122 type EventDataVote2Proposer struct { 123 Vote *Vote 124 ProposerKey string 125 } 126 127 // EventDataRequest is posted to propose a proposal 128 type EventDataRequest struct { 129 Proposal *ethTypes.Block `json:"proposal"` 130 } 131 132 // EventDataMessage is posted for Istanbul engine communication 133 type EventDataMessage struct { 134 Payload []byte `json:"payload"` 135 } 136 137 // FinalCommittedEvent is posted when a proposal is committed 138 type EventDataFinalCommitted struct { 139 BlockNumber uint64 140 } 141 142 func (_ EventDataNewBlock) AssertIsTMEventData() {} 143 func (_ EventDataNewBlockHeader) AssertIsTMEventData() {} 144 func (_ EventDataTx) AssertIsTMEventData() {} 145 func (_ EventDataRoundState) AssertIsTMEventData() {} 146 func (_ EventDataVote) AssertIsTMEventData() {} 147 func (_ EventDataSignAggr) AssertIsTMEventData() {} 148 func (_ EventDataVote2Proposer) AssertIsTMEventData() {} 149 150 func (_ EventDataRequest) AssertIsTMEventData() {} 151 func (_ EventDataMessage) AssertIsTMEventData() {} 152 func (_ EventDataFinalCommitted) AssertIsTMEventData() {} 153 154 //---------------------------------------- 155 // Wrappers for type safety 156 157 type Fireable interface { 158 events.Fireable 159 } 160 161 type Eventable interface { 162 SetEventSwitch(EventSwitch) 163 } 164 165 type EventSwitch interface { 166 events.EventSwitch 167 } 168 169 type EventCache interface { 170 Fireable 171 Flush() 172 } 173 174 func NewEventSwitch() EventSwitch { 175 return events.NewEventSwitch() 176 } 177 178 func NewEventCache(evsw EventSwitch) EventCache { 179 return events.NewEventCache(evsw) 180 } 181 182 // All events should be based on this FireEvent to ensure they are TMEventData 183 func fireEvent(fireable events.Fireable, event string, data TMEventData) { 184 if fireable != nil { 185 fireable.FireEvent(event, data) 186 } 187 } 188 189 func AddListenerForEvent(evsw EventSwitch, id, event string, cb func(data TMEventData)) { 190 evsw.AddListenerForEvent(id, event, func(data events.EventData) { 191 cb(data.(TMEventData)) 192 }) 193 194 } 195 196 //--- block, tx, and vote events 197 198 func FireEventNewBlock(fireable events.Fireable, block EventDataNewBlock) { 199 fireEvent(fireable, EventStringNewBlock(), block) 200 } 201 202 func FireEventNewBlockHeader(fireable events.Fireable, header EventDataNewBlockHeader) { 203 fireEvent(fireable, EventStringNewBlockHeader(), header) 204 } 205 206 func FireEventVote(fireable events.Fireable, vote EventDataVote) { 207 fireEvent(fireable, EventStringVote(), vote) 208 } 209 210 func FireEventSignAggr(fireable events.Fireable, sign EventDataSignAggr) { 211 fireEvent(fireable, EventStringSignAggr(), sign) 212 } 213 214 func FireEventVote2Proposer(fireable events.Fireable, vote EventDataVote2Proposer) { 215 fireEvent(fireable, EventStringVote2Proposer(), vote) 216 } 217 218 func FireEventTx(fireable events.Fireable, tx EventDataTx) { 219 fireEvent(fireable, EventStringTx(tx.Tx), tx) 220 } 221 222 //--- EventDataRoundState events 223 224 func FireEventNewRoundStep(fireable events.Fireable, rs EventDataRoundState) { 225 fireEvent(fireable, EventStringNewRoundStep(), rs) 226 } 227 228 func FireEventTimeoutPropose(fireable events.Fireable, rs EventDataRoundState) { 229 fireEvent(fireable, EventStringTimeoutPropose(), rs) 230 } 231 232 func FireEventTimeoutWait(fireable events.Fireable, rs EventDataRoundState) { 233 fireEvent(fireable, EventStringTimeoutWait(), rs) 234 } 235 236 func FireEventNewRound(fireable events.Fireable, rs EventDataRoundState) { 237 fireEvent(fireable, EventStringNewRound(), rs) 238 } 239 240 func FireEventCompleteProposal(fireable events.Fireable, rs EventDataRoundState) { 241 fireEvent(fireable, EventStringCompleteProposal(), rs) 242 } 243 244 func FireEventPolka(fireable events.Fireable, rs EventDataRoundState) { 245 fireEvent(fireable, EventStringPolka(), rs) 246 } 247 248 func FireEventUnlock(fireable events.Fireable, rs EventDataRoundState) { 249 fireEvent(fireable, EventStringUnlock(), rs) 250 } 251 252 func FireEventRelock(fireable events.Fireable, rs EventDataRoundState) { 253 fireEvent(fireable, EventStringRelock(), rs) 254 } 255 256 func FireEventLock(fireable events.Fireable, rs EventDataRoundState) { 257 fireEvent(fireable, EventStringLock(), rs) 258 } 259 260 func FireEventRequest(fireable events.Fireable, rs EventDataRequest) { 261 fireEvent(fireable, EventStringRequest(), rs) 262 } 263 264 func FireEventMessage(fireable events.Fireable, rs EventDataMessage) { 265 fireEvent(fireable, EventStringMessage(), rs) 266 } 267 268 func FireEventFinalCommitted(fireable events.Fireable, rs EventDataFinalCommitted) { 269 fireEvent(fireable, EventStringFinalCommitted(), rs) 270 }