-
Notifications
You must be signed in to change notification settings - Fork 175
/
event.go
173 lines (142 loc) · 4.66 KB
/
event.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
package handler
import (
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/model/flow"
)
// EventHandler collect events, separates out service events, and enforces event size limits
type EventHandler struct {
chain flow.Chain
eventCollectionEnabled bool
serviceEventCollectionEnabled bool
eventCollectionByteSizeLimit uint64
eventCollection *EventCollection
}
// NewEventHandler constructs a new EventHandler
func NewEventHandler(chain flow.Chain,
eventCollectionEnabled bool,
serviceEventCollectionEnabled bool,
eventCollectionByteSizeLimit uint64) *EventHandler {
return &EventHandler{
chain: chain,
eventCollectionEnabled: eventCollectionEnabled,
serviceEventCollectionEnabled: serviceEventCollectionEnabled,
eventCollectionByteSizeLimit: eventCollectionByteSizeLimit,
eventCollection: NewEventCollection(),
}
}
func (h *EventHandler) EventCollection() *EventCollection {
return h.eventCollection
}
func (h *EventHandler) EmitEvent(event cadence.Event,
txID flow.Identifier,
txIndex uint32,
payer flow.Address) error {
if !h.eventCollectionEnabled {
return nil
}
payload, err := jsoncdc.Encode(event)
if err != nil {
return errors.NewEncodingFailuref("failed to json encode a cadence event: %w", err)
}
payloadSize := uint64(len(payload))
// skip limit if payer is service account
if payer != h.chain.ServiceAddress() {
if h.eventCollection.TotalByteSize()+payloadSize > h.eventCollectionByteSizeLimit {
return errors.NewEventLimitExceededError(h.eventCollection.TotalByteSize()+payloadSize, h.eventCollectionByteSizeLimit)
}
}
flowEvent := flow.Event{
Type: flow.EventType(event.EventType.ID()),
TransactionID: txID,
TransactionIndex: txIndex,
EventIndex: h.eventCollection.eventCounter,
Payload: payload,
}
if h.serviceEventCollectionEnabled && IsServiceEvent(event, h.chain) {
h.eventCollection.AppendServiceEvent(flowEvent, payloadSize)
// we don't return and append the service event into event collection as well.
}
h.eventCollection.AppendEvent(flowEvent, payloadSize)
return nil
}
func (h *EventHandler) Events() []flow.Event {
return h.eventCollection.events
}
func (h *EventHandler) ServiceEvents() []flow.Event {
return h.eventCollection.serviceEvents
}
type EventCollection struct {
events []flow.Event
serviceEvents []flow.Event
totalByteSize uint64
eventCounter uint32
}
func NewEventCollection() *EventCollection {
return &EventCollection{
events: make([]flow.Event, 0, 10),
serviceEvents: make([]flow.Event, 0, 10),
totalByteSize: uint64(0),
eventCounter: uint32(0),
}
}
func (e *EventCollection) Child() *EventCollection {
res := NewEventCollection()
res.eventCounter = e.eventCounter
return res
}
// Merge merges another event collection into this event collection
func (e *EventCollection) Merge(other *EventCollection) {
e.events = append(e.events, other.events...)
e.serviceEvents = append(e.serviceEvents, other.serviceEvents...)
e.totalByteSize = e.totalByteSize + other.totalByteSize
e.eventCounter = e.eventCounter + other.eventCounter
}
func (e *EventCollection) Events() []flow.Event {
return e.events
}
func (e *EventCollection) AppendEvent(event flow.Event, size uint64) {
e.events = append(e.events, event)
e.totalByteSize += size
e.eventCounter++
}
func (e *EventCollection) ServiceEvents() []flow.Event {
return e.serviceEvents
}
func (e *EventCollection) AppendServiceEvent(event flow.Event, size uint64) {
e.serviceEvents = append(e.serviceEvents, event)
e.totalByteSize += size
e.eventCounter++
}
func (e *EventCollection) TotalByteSize() uint64 {
return e.totalByteSize
}
// TODO refactor this
// Keep serviceEventWhitelist module-only to prevent accidental modifications
var serviceEventWhitelist = map[string]struct{}{
"EpochManager.EpochSetup": {},
}
var serviceEventWhitelistFlat []string
func init() {
for s := range serviceEventWhitelist {
serviceEventWhitelistFlat = append(serviceEventWhitelistFlat, s)
}
}
func GetServiceEventWhitelist() []string {
return serviceEventWhitelistFlat
}
func IsServiceEvent(event cadence.Event, chain flow.Chain) bool {
serviceAccount := chain.ServiceAddress()
addressLocation, casted := event.EventType.Location.(common.AddressLocation)
if !casted {
return false
}
flowAddress := flow.BytesToAddress(addressLocation.Address.Bytes())
if flowAddress != serviceAccount {
return false
}
_, has := serviceEventWhitelist[event.EventType.QualifiedIdentifier]
return has
}