-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_events.go
238 lines (209 loc) · 8.15 KB
/
process_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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/rs/zerolog/log"
)
const eventsRPCQuery = "/block_results?height="
type EventType string
const (
// RewardEvent represents a reward event
RewardEvent EventType = "reward"
// ScoreEvent represents a score event
ScoreEvent EventType = "score"
// NetworkLossEvent represents a network loss event
NetworkLossEvent EventType = "networkloss"
// ForecastTaskScoreEvent represents a forecast task score event
ForecastTaskScoreEvent EventType = "forecastTaskScore"
// ActorLastCommitEvent represents a last commit event
ActorLastCommitEvent EventType = "lastcommit"
// TopicRewardEvent represents a topic reward event
TopicRewardEvent EventType = "topicReward"
// EMAScoreEvent represents a ema score event
EMAScoreEvent EventType = "emascore"
// TokenomicsEvent represents a ema score event
TokenomicsEvent EventType = "tokenomics"
// EcosystemTokenMintEvent represents a ecosystem token mint event
EcosystemTokenMintEvent EventType = "ecosystemTokenMint"
// RewardCurrentBlockEmissionEvent represents a reward current block emission event
RewardCurrentBlockEmissionEvent EventType = "rewardCurrentBlockEmission"
// ListeningCoefficientsEvent represents a listening coefficients event
ListeningCoefficientsEvent EventType = "listeningCoefficients"
// InfererNetworkRegretEvent represents an inferer network regret event
InfererNetworkRegretEvent EventType = "infererNetworkRegret"
// ForecasterNetworkRegretEvent represents a forecaster network regret event
ForecasterNetworkRegretEvent EventType = "forecasterNetworkRegret"
// NaiveInfererNetworkRegretEvent represents a naive inferer network regret event
NaiveInfererNetworkRegretEvent EventType = "naiveInfererNetworkRegret"
// TopicInitialRegretEvent represents a topic initial regret event
TopicInitialRegretEvent EventType = "topicInitialRegret"
// Staking event types
AddStakeEvent EventType = "addStake"
RemoveStakeEvent EventType = "removeStake"
CancelRemoveStakeEvent EventType = "cancelRemoveStake"
DelegateStakeEvent EventType = "delegateStake"
RemoveDelegateStakeEvent EventType = "removeDelegateStake"
CancelRemoveDelegateStakeEvent EventType = "cancelRemoveDelegateStake"
// Validator commission events
ValidatorRewardsEvent EventType = "rewards"
ValidatorCommissionEvent EventType = "commission"
ValidatorWithdrawRewardsEvent EventType = "withdrawRewards"
// NoneEvent represents an event that doesn't need processing
NoneEvent EventType = "none"
// an invalid event type
InvalidType EventType = "invalid"
)
// EventProcessing defines the type of processing needed for an event
type EventProcessing struct {
Type EventType
}
var event_whitelist = map[string]EventProcessing{
"EventScoresSet": {Type: ScoreEvent},
"EventRewardsSettled": {Type: RewardEvent},
"EventNetworkLossSet": {Type: NetworkLossEvent},
"EventForecastTaskScoreSet": {Type: ForecastTaskScoreEvent},
"EventWorkerLastCommitSet": {Type: ActorLastCommitEvent},
"EventReputerLastCommitSet": {Type: ActorLastCommitEvent},
"EventTopicRewardsSet": {Type: TopicRewardEvent},
"EventEMAScoresSet": {Type: EMAScoreEvent},
"EventTokenomicsSet": {Type: TokenomicsEvent},
"EventEcosystemTokenMintSet": {Type: EcosystemTokenMintEvent},
"EventRewardCurrentBlockEmission": {Type: RewardCurrentBlockEmissionEvent},
"EventListeningCoefficientsSet": {Type: ListeningCoefficientsEvent},
"EventInfererNetworkRegretSet": {Type: InfererNetworkRegretEvent},
"EventForecasterNetworkRegretSet": {Type: ForecasterNetworkRegretEvent},
"EventNaiveInfererNetworkRegretSet": {Type: NaiveInfererNetworkRegretEvent},
"EventTopicInitialRegretSet": {Type: TopicInitialRegretEvent},
// Staking events
"AddStakeRequest": {Type: AddStakeEvent},
"RemoveStakeRequest": {Type: RemoveStakeEvent},
"CancelRemoveStakeRequest": {Type: CancelRemoveStakeEvent},
"DelegateStakeRequest": {Type: DelegateStakeEvent},
"RemoveDelegateStakeRequest": {Type: RemoveDelegateStakeEvent},
"CancelRemoveDelegateStakeRequest": {Type: CancelRemoveDelegateStakeEvent},
// Validator commission events
"rewards": {Type: ValidatorRewardsEvent},
"commission": {Type: ValidatorCommissionEvent},
"withdraw_rewards": {Type: ValidatorWithdrawRewardsEvent},
}
type BlockResult struct {
Result struct {
Height string `json:"height"`
FinalizeBlockEvents []Event `json:"finalize_block_events"`
TxsBlockEvents []TxEvent `json:"txs_results"`
} `json:"result"`
}
type TxEvent struct {
Code int `json:"code"`
Data string `json:"data"`
Log string `json:"log"`
Info string `json:"info"`
Gas_wanted string `json:"gas_wanted"`
Gas_used string `json:"gas_used"`
Events []Event `json:"events"`
Codespace string `json:"codespace"`
}
type Event struct {
Type string `json:"type"`
Attributes []Attribute `json:"attributes"`
}
type Attribute struct {
Key string `json:"key"`
Value string `json:"value"`
}
// FetchEventBlockData fetches block data for a given height
func FetchEventBlockData(config ClientConfig, height uint64) (*BlockResult, error) {
url := fmt.Sprintf("%s%s%d", config.Node, eventsRPCQuery, height)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var blockResult BlockResult
err = json.Unmarshal(body, &blockResult)
if err != nil {
return nil, err
}
return &blockResult, nil
}
// FilterEvents filters events based on a whitelist on its type
func FilterEvents(events *BlockResult, whitelist map[string]EventProcessing) []Event {
var filteredEvents []Event
for _, event := range events.Result.FinalizeBlockEvents {
baseType := getBaseEventType(event.Type)
if baseType == string(InvalidType) {
log.Debug().Str("type", event.Type).Msg("event type %s is invalid")
continue
}
if processing, ok := whitelist[baseType]; ok && processing.Type != NoneEvent {
filteredEvents = append(filteredEvents, event)
}
}
for _, blockevent := range events.Result.TxsBlockEvents {
for _, event := range blockevent.Events {
baseType := getBaseEventType(event.Type)
if baseType == string(InvalidType) {
log.Debug().Str("type", event.Type).Msg("ERROR: event type %s is invalid")
continue
}
if processing, ok := whitelist[baseType]; ok && processing.Type != NoneEvent {
filteredEvents = append(filteredEvents, event)
}
}
}
return filteredEvents
}
// getBaseEventType extracts the base event type without prefix
func getBaseEventType(eventType string) string {
parts := strings.Split(eventType, ".")
if len(parts) > 1 {
return parts[len(parts)-1] // Return the last part, e.g., "EventScoresSet"
} else if eventType == "rewards" || eventType == "commission" || eventType == "withdraw_rewards" {
// cosmos SDK events doesn't have the same prefix as allora events
return eventType
}
return string(InvalidType) // Return InvalidType for invalid types
}
// processes the events of a block
func processBlock(config ClientConfig, height uint64) error {
blockData, err := FetchEventBlockData(config, height)
if err != nil {
return fmt.Errorf("failed to fetch block data: %w", err)
}
filteredEvents := FilterEvents(blockData, event_whitelist)
var eventRecords []EventRecord
for _, event := range filteredEvents {
data, err := json.Marshal(event.Attributes)
if err != nil {
return fmt.Errorf("failed to marshal event attributes: %w", err)
}
var sender string
for _, attr := range event.Attributes {
if attr.Key == "sender" {
sender = attr.Value
break
}
}
eventRecords = append(eventRecords, EventRecord{
Height: height,
Type: event.Type,
Sender: sender,
Data: data,
})
}
err = insertEvents(eventRecords)
if err != nil {
return fmt.Errorf("failed to insert events: %w", err)
}
return nil
}