-
Notifications
You must be signed in to change notification settings - Fork 14
/
events.go
352 lines (288 loc) · 11.8 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package types
import (
"fmt"
"strings"
abci "github.com/dashpay/tenderdash/abci/types"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/internal/jsontypes"
tmquery "github.com/dashpay/tenderdash/internal/pubsub/query"
)
// Reserved event types (alphabetically sorted).
const (
// Block level events for mass consumption by users.
// These events are triggered from the state package,
// after a block has been committed.
// These are also used by the tx indexer for async indexing.
// All of this data can be fetched through the rpc.
EventNewBlockValue = "NewBlock"
EventNewBlockHeaderValue = "NewBlockHeader"
EventNewEvidenceValue = "NewEvidence"
EventTxValue = "Tx"
EventValidatorSetUpdateValue = "ValidatorSetUpdate"
// Internal consensus events.
// These are used for testing the consensus state machine.
// They can also be used to build real-time consensus visualizers.
EventCompleteProposalValue = "CompleteProposal"
// The BlockSyncStatus event will be emitted when the node switching
// state sync mechanism between the consensus reactor and the blocksync reactor.
EventBlockSyncStatusValue = "BlockSyncStatus"
EventLockValue = "Lock"
EventNewRoundValue = "NewRound"
EventNewRoundStepValue = "NewRoundStep"
EventPolkaValue = "Polka"
EventRelockValue = "Relock"
EventStateSyncStatusValue = "StateSyncStatus"
EventTimeoutProposeValue = "TimeoutPropose"
EventTimeoutWaitValue = "TimeoutWait"
EventValidBlockValue = "ValidBlock"
EventVoteValue = "Vote"
EventCommitValue = "Commit"
// Events emitted by the evidence reactor when evidence is validated
// and before it is committed
EventEvidenceValidatedValue = "EvidenceValidated"
)
// Pre-populated ABCI Tendermint-reserved events
var (
EventNewBlock = abci.Event{
Type: strings.Split(EventTypeKey, ".")[0],
Attributes: []abci.EventAttribute{
{
Key: strings.Split(EventTypeKey, ".")[1],
Value: EventNewBlockValue,
},
},
}
EventNewBlockHeader = abci.Event{
Type: strings.Split(EventTypeKey, ".")[0],
Attributes: []abci.EventAttribute{
{
Key: strings.Split(EventTypeKey, ".")[1],
Value: EventNewBlockHeaderValue,
},
},
}
EventNewEvidence = abci.Event{
Type: strings.Split(EventTypeKey, ".")[0],
Attributes: []abci.EventAttribute{
{
Key: strings.Split(EventTypeKey, ".")[1],
Value: EventNewEvidenceValue,
},
},
}
EventTx = abci.Event{
Type: strings.Split(EventTypeKey, ".")[0],
Attributes: []abci.EventAttribute{
{
Key: strings.Split(EventTypeKey, ".")[1],
Value: EventTxValue,
},
},
}
)
// ENCODING / DECODING
// EventData is satisfied by types that can be published as event data.
//
// Implementations of this interface that contain ABCI event metadata should
// also implement the eventlog.ABCIEventer extension interface to expose those
// metadata to the event log machinery. Event data that do not contain ABCI
// metadata can safely omit this.
type EventData interface {
// The value must support encoding as a type-tagged JSON object.
jsontypes.Tagged
}
func init() {
jsontypes.MustRegister(EventDataBlockSyncStatus{})
jsontypes.MustRegister(EventDataCompleteProposal{})
jsontypes.MustRegister(EventDataNewBlock{})
jsontypes.MustRegister(EventDataNewBlockHeader{})
jsontypes.MustRegister(EventDataNewEvidence{})
jsontypes.MustRegister(EventDataNewRound{})
jsontypes.MustRegister(EventDataRoundState{})
jsontypes.MustRegister(EventDataStateSyncStatus{})
jsontypes.MustRegister(EventDataTx{})
jsontypes.MustRegister(EventDataVote{})
jsontypes.MustRegister(EventDataValidatorSetUpdate{})
jsontypes.MustRegister(EventDataEvidenceValidated{})
jsontypes.MustRegister(EventDataString(""))
}
// Most event messages are basic types (a block, a transaction)
// but some (an input to a call tx or a receive) are more exotic
type EventDataNewBlock struct {
Block *Block `json:"block"`
BlockID BlockID `json:"block_id"`
ResultFinalizeBlock abci.ResponseFinalizeBlock `json:"result_finalize_block"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataNewBlock) TypeTag() string { return "tendermint/event/NewBlock" }
// ABCIEvents implements the eventlog.ABCIEventer interface.
func (e EventDataNewBlock) ABCIEvents() []abci.Event {
base := []abci.Event{eventWithAttr(BlockHeightKey, fmt.Sprint(e.Block.Header.Height))}
return append(base, e.ResultFinalizeBlock.Events...)
}
type EventDataNewBlockHeader struct {
Header Header `json:"header"`
NumTxs int64 `json:"num_txs,string"` // Number of txs in a block
ResultProcessProposal abci.ResponseProcessProposal `json:"result_process_proposal"`
ResultFinalizeBlock abci.ResponseFinalizeBlock `json:"result_finalize_block"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataNewBlockHeader) TypeTag() string { return "tendermint/event/NewBlockHeader" }
// ABCIEvents implements the eventlog.ABCIEventer interface.
func (e EventDataNewBlockHeader) ABCIEvents() []abci.Event {
base := []abci.Event{eventWithAttr(BlockHeightKey, fmt.Sprint(e.Header.Height))}
return append(base, e.ResultFinalizeBlock.Events...)
}
type EventDataNewEvidence struct {
Evidence Evidence `json:"evidence"`
Height int64 `json:"height,string"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataNewEvidence) TypeTag() string { return "tendermint/event/NewEvidence" }
// All txs fire EventDataTx
type EventDataTx struct {
abci.TxResult
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataTx) TypeTag() string { return "tendermint/event/Tx" }
// ABCIEvents implements the eventlog.ABCIEventer interface.
func (e EventDataTx) ABCIEvents() []abci.Event {
base := []abci.Event{
eventWithAttr(TxHashKey, fmt.Sprintf("%X", Tx(e.Tx).Hash())),
eventWithAttr(TxHeightKey, fmt.Sprintf("%d", e.Height)),
}
return append(base, e.Result.Events...)
}
// NOTE: This goes into the replay WAL
type EventDataRoundState struct {
Height int64 `json:"height,string"`
Round int32 `json:"round"`
Step string `json:"step"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataRoundState) TypeTag() string { return "tendermint/event/RoundState" }
type ValidatorInfo struct {
ProTxHash ProTxHash `json:"pro_tx_hash"`
Index int32 `json:"index"`
}
type EventDataNewRound struct {
Height int64 `json:"height,string"`
Round int32 `json:"round"`
Step string `json:"step"`
Proposer ValidatorInfo `json:"proposer"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataNewRound) TypeTag() string { return "tendermint/event/NewRound" }
type EventDataCompleteProposal struct {
Height int64 `json:"height,string"`
Round int32 `json:"round"`
Step string `json:"step"`
BlockID BlockID `json:"block_id"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataCompleteProposal) TypeTag() string { return "tendermint/event/CompleteProposal" }
type EventDataVote struct {
Vote *Vote
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataVote) TypeTag() string { return "tendermint/event/Vote" }
type EventDataCommit struct {
Commit *Commit
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataCommit) TypeTag() string { return "tendermint/event/Commit" }
type EventDataString string
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataString) TypeTag() string { return "tendermint/event/ProposalString" }
type EventDataValidatorSetUpdate struct {
ValidatorSetUpdates []*Validator `json:"validator_updates"`
ThresholdPublicKey crypto.PubKey `json:"threshold_public_key"`
QuorumHash crypto.QuorumHash `json:"quorum_hash"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataValidatorSetUpdate) TypeTag() string { return "tendermint/event/ValidatorSetUpdates" }
// EventDataBlockSyncStatus shows the fastsync status and the
// height when the node state sync mechanism changes.
type EventDataBlockSyncStatus struct {
Complete bool `json:"complete"`
Height int64 `json:"height,string"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataBlockSyncStatus) TypeTag() string { return "tendermint/event/FastSyncStatus" }
// EventDataStateSyncStatus shows the statesync status and the
// height when the node state sync mechanism changes.
type EventDataStateSyncStatus struct {
Complete bool `json:"complete"`
Height int64 `json:"height,string"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataStateSyncStatus) TypeTag() string { return "tendermint/event/StateSyncStatus" }
type EventDataEvidenceValidated struct {
Evidence Evidence `json:"evidence"`
Height int64 `json:"height,string"`
}
// TypeTag implements the required method of jsontypes.Tagged.
func (EventDataEvidenceValidated) TypeTag() string { return "tendermint/event/EvidenceValidated" }
// PUBSUB
const (
// EventTypeKey is a reserved composite key for event name.
EventTypeKey = "tm.event"
// TxHashKey is a reserved key, used to specify transaction's hash.
// see EventBus#PublishEventTx
TxHashKey = "tx.hash"
// TxHeightKey is a reserved key, used to specify transaction block's height.
// see EventBus#PublishEventTx
TxHeightKey = "tx.height"
// BlockHeightKey is a reserved key used for indexing FinalizeBlock events.
BlockHeightKey = "block.height"
)
var (
EventQueryCompleteProposal = QueryForEvent(EventCompleteProposalValue)
EventQueryLock = QueryForEvent(EventLockValue)
EventQueryNewBlock = QueryForEvent(EventNewBlockValue)
EventQueryNewBlockHeader = QueryForEvent(EventNewBlockHeaderValue)
EventQueryNewEvidence = QueryForEvent(EventNewEvidenceValue)
EventQueryNewRound = QueryForEvent(EventNewRoundValue)
EventQueryNewRoundStep = QueryForEvent(EventNewRoundStepValue)
EventQueryPolka = QueryForEvent(EventPolkaValue)
EventQueryRelock = QueryForEvent(EventRelockValue)
EventQueryTimeoutPropose = QueryForEvent(EventTimeoutProposeValue)
EventQueryTimeoutWait = QueryForEvent(EventTimeoutWaitValue)
EventQueryTx = QueryForEvent(EventTxValue)
EventQueryValidatorSetUpdates = QueryForEvent(EventValidatorSetUpdateValue)
EventQueryValidBlock = QueryForEvent(EventValidBlockValue)
EventQueryVote = QueryForEvent(EventVoteValue)
EventQueryBlockSyncStatus = QueryForEvent(EventBlockSyncStatusValue)
EventQueryStateSyncStatus = QueryForEvent(EventStateSyncStatusValue)
EventQueryEvidenceValidated = QueryForEvent(EventEvidenceValidatedValue)
)
func EventQueryTxFor(tx Tx) *tmquery.Query {
return tmquery.MustCompile(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTxValue, TxHashKey, tx.Hash()))
}
func QueryForEvent(eventValue string) *tmquery.Query {
return tmquery.MustCompile(fmt.Sprintf("%s='%s'", EventTypeKey, eventValue))
}
//go:generate ../scripts/mockery_generate.sh BlockEventPublisher
// BlockEventPublisher publishes all block related events
type BlockEventPublisher interface {
TxEventPublisher
PublishEventNewBlock(EventDataNewBlock) error
PublishEventNewBlockHeader(EventDataNewBlockHeader) error
PublishEventNewEvidence(EventDataNewEvidence) error
PublishEventValidatorSetUpdates(EventDataValidatorSetUpdate) error
}
type TxEventPublisher interface {
PublishEventTx(EventDataTx) error
}
// eventWithAttr constructs a single abci.Event with a single attribute.
// The type of the event and the name of the attribute are obtained by
// splitting the event type on period (e.g., "foo.bar").
func eventWithAttr(etype, value string) abci.Event {
parts := strings.SplitN(etype, ".", 2)
return abci.Event{
Type: parts[0],
Attributes: []abci.EventAttribute{{
Key: parts[1], Value: value,
}},
}
}