forked from solarlabsteam/missed-blocks-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_generator.go
203 lines (177 loc) · 5.63 KB
/
report_generator.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
package main
import (
"fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/rs/zerolog"
)
type ReportGenerator struct {
Params Params
Config *AppConfig
gRPC *TendermintGRPC
Logger zerolog.Logger
State ValidatorsState
Registry codectypes.InterfaceRegistry
}
func NewReportGenerator(
params Params,
grpc *TendermintGRPC,
config *AppConfig,
logger *zerolog.Logger,
registry codectypes.InterfaceRegistry,
) *ReportGenerator {
return &ReportGenerator{
Params: params,
gRPC: grpc,
Config: config,
Logger: logger.With().Str("component", "report_generator").Logger(),
Registry: registry,
}
}
func (g *ReportGenerator) GetNewState() (ValidatorsState, error) {
g.Logger.Debug().Msg("Querying for signing infos...")
state, err := g.gRPC.GetValidatorsState()
if err != nil {
g.Logger.Error().Err(err).Msg("Could not query for signing infos")
return nil, err
}
return FilterMap(state, func(v ValidatorState) bool {
return g.Config.IsValidatorMonitored(v.Address)
}), nil
}
func (g *ReportGenerator) GetValidatorReportEntry(oldState, newState ValidatorState) (*ReportEntry, bool) {
g.Logger.Trace().
Str("oldState", fmt.Sprintf("%+v", oldState)).
Str("newState", fmt.Sprintf("%+v", newState)).
Msg("Processing validator report entry")
// 1. If validator's tombstoned, but wasn't - set tombstoned report entry.
if newState.Tombstoned && !oldState.Tombstoned {
g.Logger.Debug().
Str("address", oldState.Address).
Msg("Validator is tombstoned")
return &ReportEntry{
ValidatorAddress: newState.Address,
ValidatorMoniker: newState.Moniker,
Emoji: TombstonedEmoji,
Description: TombstonedDesc,
Direction: TOMBSTONED,
}, true
}
// 2. If validator's jailed, but wasn't - set jailed report entry.
if newState.Jailed && !oldState.Jailed {
g.Logger.Debug().
Str("address", oldState.Address).
Msg("Validator is jailed")
return &ReportEntry{
ValidatorAddress: newState.Address,
ValidatorMoniker: newState.Moniker,
Emoji: JailedEmoju,
Description: JailedDesc,
Direction: JAILED,
}, true
}
// 3. If validator's not jailed, but was - set unjailed report entry.
if !newState.Jailed && oldState.Jailed {
g.Logger.Debug().
Str("address", oldState.Address).
Msg("Validator is unjailed")
return &ReportEntry{
ValidatorAddress: newState.Address,
ValidatorMoniker: newState.Moniker,
Emoji: UnjailedEmoji,
Description: UnjailedDesc,
Direction: UNJAILED,
}, true
}
// 4. If validator is and was jailed - do nothing.
if newState.Jailed && oldState.Jailed {
g.Logger.Debug().
Str("address", oldState.Address).
Msg("Validator is and was jailed - no need to send report")
return nil, false
}
// 5. Validator isn't and wasn't jailed.
//
// First, check if old and new groups are the same - if they have different start,
// they are different. If they don't - they aren't so no need to send a notification.
oldGroup, oldGroupErr := g.Config.MissedBlocksGroups.GetGroup(oldState.MissedBlocks)
if oldGroupErr != nil {
g.Logger.Error().Err(oldGroupErr).Msg("Could not get old group")
return nil, false
}
newGroup, newGroupErr := g.Config.MissedBlocksGroups.GetGroup(newState.MissedBlocks)
if newGroupErr != nil {
g.Logger.Error().Err(newGroupErr).Msg("Could not get new group")
return nil, false
}
if oldGroup.Start == newGroup.Start {
g.Logger.Debug().
Str("address", oldState.Address).
Int64("before", oldState.MissedBlocks).
Int64("after", newState.MissedBlocks).
Msg("Validator didn't change group - no need to send report")
return nil, false
}
// Validator switched from one MissedBlockGroup to another, 2 cases how that may happen
// 1) validator is skipping blocks
// 2) validator skipped some blocks in the past, but recovered, is now signing, and the window
// moves - the amount of missed blocks is decreasing.
// Need to understand which one it is: if old missed blocks < new missed blocks -
// it's 1), if vice versa, then 2)
entry := &ReportEntry{
ValidatorAddress: newState.Address,
ValidatorMoniker: newState.Moniker,
MissingBlocks: newState.MissedBlocks,
}
if oldState.MissedBlocks < newState.MissedBlocks {
// skipping blocks
g.Logger.Debug().
Str("address", oldState.Address).
Int64("before", oldState.MissedBlocks).
Int64("after", newState.MissedBlocks).
Msg("Validator's missed blocks increasing")
entry.Direction = INCREASING
entry.Emoji = newGroup.EmojiStart
entry.Description = newGroup.DescStart
} else {
// restoring
g.Logger.Debug().
Str("address", oldState.Address).
Int64("before", oldState.MissedBlocks).
Int64("after", newState.MissedBlocks).
Msg("Validator's missed blocks decreasing")
entry.Direction = DECREASING
entry.Emoji = newGroup.EmojiEnd
entry.Description = newGroup.DescEnd
}
return entry, true
}
func (g *ReportGenerator) GenerateReport() *Report {
newState, err := g.GetNewState()
if err != nil {
g.Logger.Error().Err(err).Msg("Error getting new state")
return &Report{}
}
if len(g.State) == 0 {
g.Logger.Info().Msg("No previous state, skipping.")
g.State = newState
return &Report{}
}
entries := []ReportEntry{}
for address, info := range newState {
oldState, ok := g.State[address]
if !ok {
g.Logger.Warn().Str("address", address).Msg("No old state present for address")
continue
}
entry, present := g.GetValidatorReportEntry(oldState, info)
if !present {
g.Logger.Trace().
Str("address", address).
Msg("No report entry present")
continue
}
entries = append(entries, *entry)
}
g.State = newState
return &Report{Entries: entries}
}