This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_events.go
149 lines (128 loc) · 3.84 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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
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"
// NoneEvent represents an event that doesn't need processing
NoneEvent EventType = "none"
)
// EventProcessing defines the type of processing needed for an event
type EventProcessing struct {
Type EventType
}
var event_whitelist = map[string]EventProcessing{
"emissions.v1.EventScoresSet": {Type: ScoreEvent},
"emissions.v1.EventRewardsSettled": {Type: RewardEvent},
"emissions.v1.EventNetworkLossSet": {Type: NetworkLossEvent},
"emissions.v2.EventScoresSet": {Type: ScoreEvent},
"emissions.v2.EventRewardsSettled": {Type: RewardEvent},
"emissions.v2.EventNetworkLossSet": {Type: NetworkLossEvent},
}
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 {
if processing, ok := whitelist[event.Type]; ok && processing.Type != NoneEvent {
filteredEvents = append(filteredEvents, event)
}
}
for _, blockevent := range events.Result.TxsBlockEvents {
for _, event := range blockevent.Events {
if processing, ok := whitelist[event.Type]; ok && processing.Type != NoneEvent {
filteredEvents = append(filteredEvents, event)
}
}
}
return filteredEvents
}
// 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
}