-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathevents.go
More file actions
261 lines (224 loc) · 8.16 KB
/
events.go
File metadata and controls
261 lines (224 loc) · 8.16 KB
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
package models
import (
"fmt"
"sort"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go/fvm/evm/events"
evmTypes "github.com/onflow/flow-go/fvm/evm/types"
errs "github.com/onflow/flow-evm-gateway/models/errors"
)
const (
BlockExecutedQualifiedIdentifier = string(events.EventTypeBlockExecuted)
TransactionExecutedQualifiedIdentifier = string(events.EventTypeTransactionExecuted)
)
// isBlockExecutedEvent checks whether the given event contains block executed data.
func isBlockExecutedEvent(event cadence.Event) bool {
if event.EventType == nil {
return false
}
return event.EventType.QualifiedIdentifier == BlockExecutedQualifiedIdentifier
}
// isTransactionExecutedEvent checks whether the given event contains transaction executed data.
func isTransactionExecutedEvent(event cadence.Event) bool {
if event.EventType == nil {
return false
}
return event.EventType.QualifiedIdentifier == TransactionExecutedQualifiedIdentifier
}
// CadenceEvents contains Flow emitted events containing one or zero evm block executed event,
// and multiple or zero evm transaction events.
type CadenceEvents struct {
events flow.BlockEvents // Flow events for a specific flow block
block *Block // EVM block (at most one per Flow block)
blockEventPayload *events.BlockEventPayload // EVM.BlockExecuted event payload (at most one per Flow block)
transactions []Transaction // transactions in the EVM block
txEventPayloads []events.TransactionEventPayload // EVM.TransactionExecuted event payloads
receipts []*Receipt // receipts for transactions
}
// NewCadenceEvents decodes the events into evm types.
func NewCadenceEvents(events flow.BlockEvents) (*CadenceEvents, error) {
e, err := decodeCadenceEvents(events)
if err != nil {
return nil, err
}
// if cadence event is empty don't calculate any dynamic values
if e.Empty() {
return e, nil
}
// calculate dynamic values
cumulativeGasUsed := uint64(0)
blockHash, err := e.block.Hash()
if err != nil {
return nil, err
}
// Log index field holds the index position in the entire block
logIndex := uint(0)
for i, rcp := range e.receipts {
// add transaction hashes to the block
e.block.TransactionHashes = append(e.block.TransactionHashes, rcp.TxHash)
// calculate cumulative gas used up to that point
cumulativeGasUsed += rcp.GasUsed
rcp.CumulativeGasUsed = cumulativeGasUsed
// set the transaction index
rcp.TransactionIndex = uint(i)
// set calculate block hash
rcp.BlockHash = blockHash
// dynamically add missing log fields
for _, l := range rcp.Logs {
l.BlockNumber = rcp.BlockNumber.Uint64()
l.BlockHash = rcp.BlockHash
l.TxHash = rcp.TxHash
l.TxIndex = rcp.TransactionIndex
l.Index = logIndex
l.Removed = false
logIndex++
}
}
return e, nil
}
// decodeCadenceEvents accepts Flow Cadence event and decodes it into CadenceEvents
// collection. It also performs safety checks on the provided event data.
func decodeCadenceEvents(events flow.BlockEvents) (*CadenceEvents, error) {
e := &CadenceEvents{events: events}
// decode and cache block and transactions
for _, event := range events.Events {
val := event.Value
if isBlockExecutedEvent(val) {
if e.block != nil { // safety check, we can only have 1 or 0 evm blocks per one flow block
return nil, fmt.Errorf("EVM block was already set for Flow block: %d", events.Height)
}
block, blockEventPayload, err := decodeBlockEvent(val)
if err != nil {
return nil, err
}
e.block = block
e.blockEventPayload = blockEventPayload
continue
}
if isTransactionExecutedEvent(val) {
tx, receipt, txEventPayload, err := decodeTransactionEvent(val)
if err != nil {
return nil, err
}
e.transactions = append(e.transactions, tx)
e.txEventPayloads = append(e.txEventPayloads, *txEventPayload)
e.receipts = append(e.receipts, receipt)
}
}
// safety check, we have a missing block in the events
if e.block == nil && len(e.transactions) > 0 {
return nil, fmt.Errorf(
"%w EVM block nil at flow block: %d",
errs.ErrMissingBlock,
events.Height,
)
}
if e.block != nil {
txHashes := evmTypes.TransactionHashes{}
for _, tx := range e.transactions {
txHashes = append(txHashes, tx.Hash())
}
if e.block.TransactionHashRoot != txHashes.RootHash() {
return nil, fmt.Errorf(
"%w EVM block %d references missing transaction/s",
errs.ErrMissingTransactions,
e.block.Height,
)
}
}
return e, nil
}
// BlockEvents returns the Flow block events.
func (c *CadenceEvents) BlockEvents() flow.BlockEvents {
return c.events
}
// Block evm block. If event doesn't contain EVM block the return value is nil.
func (c *CadenceEvents) Block() *Block {
return c.block
}
// BlockEventPayload returns the EVM.BlockExecuted event payload. If the Flow block
// events do not contain an EVM block, the return value is nil.
func (c *CadenceEvents) BlockEventPayload() *events.BlockEventPayload {
return c.blockEventPayload
}
// Transactions included in the EVM block, if event doesn't
// contain EVM transactions the return value is nil.
func (c *CadenceEvents) Transactions() []Transaction {
return c.transactions
}
// TxEventPayloads returns the EVM.TransactionExecuted event payloads for the
// current EVM block. If the Flow block events do not contain any EVM transactions
// the return value is nil.
func (c *CadenceEvents) TxEventPayloads() []events.TransactionEventPayload {
return c.txEventPayloads
}
// Receipts included in the EVM block, if event doesn't
// contain EVM transactions the return value is nil.
func (c *CadenceEvents) Receipts() []*Receipt {
return c.receipts
}
// Empty checks if there is an EVM block included in the events.
// If there are no evm block or transactions events this is a heartbeat event.
func (c *CadenceEvents) Empty() bool {
return c.block == nil
}
// CadenceHeight returns the Flow Cadence height at which the events
// were emitted.
func (c *CadenceEvents) CadenceHeight() uint64 {
return c.events.Height
}
// CadenceBlockID returns the Flow Cadence block ID.
func (c *CadenceEvents) CadenceBlockID() flow.Identifier {
return c.events.BlockID
}
// Length of the Cadence events emitted.
func (c *CadenceEvents) Length() int {
return len(c.events.Events)
}
// BlockEvents is a wrapper around events streamed, and it also contains an error
type BlockEvents struct {
Events *CadenceEvents
Err error
}
// NewMultiBlockEvents will decode any possible `EVM.TransactionExecuted` &
// `EVM.BlockExecuted` events and populate the resulting `Block`, `Transaction` &
// `Receipt` values.
// The `EVM.TransactionExecuted` events are expected to be properly sorted by
// the caller.
// Use this method when dealing with `flow.BlockEvents` from multiple Flow blocks.
// The `EVM.TransactionExecuted` events could be produced at a Flow block, that
// comes prior to the Flow block that produced the `EVM.BlockExecuted` event.
func NewMultiBlockEvents(events flow.BlockEvents) BlockEvents {
cdcEvents, err := NewCadenceEvents(events)
return BlockEvents{
Events: cdcEvents,
Err: err,
}
}
// NewSingleBlockEvents will decode any possible `EVM.TransactionExecuted` &
// `EVM.BlockExecuted` events and populate the resulting `Block`, `Transaction` &
// `Receipt` values.
// The `EVM.TransactionExecuted` events will be sorted by `TransactionIndex` &
// `EventIndex`, prior to decoding.
// Use this method when dealing with `flow.BlockEvents` from a single Flow block.
func NewSingleBlockEvents(events flow.BlockEvents) BlockEvents {
// first we sort all the events in the block, by their TransactionIndex,
// and then we also sort events in the same transaction, by their EventIndex.
sort.Slice(events.Events, func(i, j int) bool {
if events.Events[i].TransactionIndex != events.Events[j].TransactionIndex {
return events.Events[i].TransactionIndex < events.Events[j].TransactionIndex
}
return events.Events[i].EventIndex < events.Events[j].EventIndex
})
cdcEvents, err := NewCadenceEvents(events)
return BlockEvents{
Events: cdcEvents,
Err: err,
}
}
func NewBlockEventsError(err error) BlockEvents {
return BlockEvents{
Err: err,
}
}