/
ocr.go
267 lines (231 loc) · 7.74 KB
/
ocr.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package testreporters
import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
"sort"
"testing"
"time"
"github.com/rs/zerolog/log"
"github.com/slack-go/slack"
"github.com/smartcontractkit/chainlink-testing-framework/testreporters"
)
//TODO: This whole process can definitely be simplified and improved, but for some reason I'm getting brain block at the moment
// OCRSoakTestReporter collates all OCRAnswerUpdated events into a single report
type OCRSoakTestReporter struct {
StartTime time.Time
AnomaliesDetected bool
OCRVersion string
anomalies [][]string
timeLine [][]string
namespace string
csvLocation string
}
// TimeLineEvent represents a single event in the timeline
type TimeLineEvent interface {
Time() time.Time
CSV() [][]string
}
// TestIssue is a single RPC issue, either a disconnect or reconnect
type TestIssue struct {
StartTime time.Time `toml:"startTime"`
Message string `toml:"message"`
}
func (r *TestIssue) Time() time.Time {
return r.StartTime
}
func (r *TestIssue) CSV() [][]string {
return [][]string{{r.StartTime.Format("2006-01-02 15:04:05.00 MST"), "Test Issue!", r.Message}}
}
// OCRRoundState indicates that a round per contract should complete within this time with this answer
type OCRRoundState struct {
StartTime time.Time `toml:"startTime"`
EndTime time.Time `toml:"endTime"` // Time when the round should end, only used for analysis
Answer int64 `toml:"answer"`
Anomalous bool `toml:"anomalous"` // Whether the round was anomalous
FoundEvents map[string][]*FoundEvent `toml:"foundEvents"` // Address -> FoundEvents, possible to have multiple found events per round, and need to call it out
TimeLineEvents []TimeLineEvent `toml:"timeLineEvents"`
anomalies [][]string
}
func (e *OCRRoundState) Time() time.Time {
return e.StartTime
}
// CSV returns a CSV representation of the test state and all events
func (e *OCRRoundState) CSV() [][]string {
rows := [][]string{{e.StartTime.Format("2006-01-02 15:04:05.00 MST"), fmt.Sprintf("Expecting new Answer: %d", e.Answer)}}
rows = append(rows, e.anomalies...)
return rows
}
// Validate checks that
// 1. There is a FoundEvent for every address
// 2. There is only one FoundEvent for every address
// 3. The answer is correct
func (e *OCRRoundState) Validate() bool {
anomalies := [][]string{}
for address, eventList := range e.FoundEvents {
if len(eventList) == 0 {
e.Anomalous = true
anomalies = append(anomalies, []string{
e.StartTime.Format("2006-01-02 15:04:05.00 MST"), "Anomaly Found!", fmt.Sprintf("No AnswerUpdated for address '%s'", address),
})
} else if len(eventList) > 1 {
e.Anomalous = true
anomalies = append(anomalies, []string{e.StartTime.Format("2006-01-02 15:04:05.00 MST"), "Anomaly Found!",
fmt.Sprintf("Multiple AnswerUpdated for address '%s', possible double-transmission", address)},
)
} else {
event := eventList[0]
if event.Answer != e.Answer {
e.Anomalous = true
anomalies = append(e.anomalies, []string{e.StartTime.Format("2006-01-02 15:04:05.00 MST"), "Anomaly Found!",
fmt.Sprintf("FoundEvent for address '%s' has wrong answer '%d'", address, event.Answer)},
)
}
}
}
e.anomalies = anomalies
return e.Anomalous
}
// FoundEvent is a single round update event
type FoundEvent struct {
StartTime time.Time
BlockNumber uint64
Address string
Answer int64
RoundID uint64
}
func (a *FoundEvent) Time() time.Time {
return a.StartTime
}
// CSV returns a CSV representation of the event
func (a *FoundEvent) CSV() [][]string {
return [][]string{{
a.StartTime.Format("2006-01-02 15:04:05.00 MST"),
fmt.Sprintf("Address: %s", a.Address),
fmt.Sprintf("Round: %d", a.RoundID),
fmt.Sprintf("Answer: %d", a.Answer),
fmt.Sprintf("Block: %d", a.BlockNumber),
}}
}
// RecordEvents takes in a list of test states and RPC issues, orders them, and records them in the timeline
func (o *OCRSoakTestReporter) RecordEvents(testStates []*OCRRoundState, testIssues []*TestIssue) {
events := []TimeLineEvent{}
for _, expectedEvent := range testStates {
if expectedEvent.Validate() {
o.AnomaliesDetected = true
o.anomalies = append(o.anomalies, expectedEvent.anomalies...)
}
events = append(events, expectedEvent)
events = append(events, expectedEvent.TimeLineEvents...)
}
if len(testIssues) > 0 {
o.AnomaliesDetected = true
}
for _, testIssue := range testIssues {
events = append(events, testIssue)
o.anomalies = append(o.anomalies, testIssue.CSV()...)
}
sort.Slice(events, func(i, j int) bool {
return events[i].Time().Before(events[j].Time())
})
for _, event := range events {
o.timeLine = append(o.timeLine, event.CSV()...)
}
}
// SetNamespace sets the namespace of the report for clean reports
func (o *OCRSoakTestReporter) SetNamespace(namespace string) {
o.namespace = namespace
}
// WriteReport writes OCR Soak test report to a CSV file and final report
func (o *OCRSoakTestReporter) WriteReport(folderLocation string) error {
log.Debug().Msgf("Writing OCRv%s Soak Test Report", o.OCRVersion)
reportLocation := filepath.Join(folderLocation, "./ocr_soak_report.csv")
o.csvLocation = reportLocation
ocrReportFile, err := os.Create(reportLocation)
if err != nil {
return err
}
defer ocrReportFile.Close()
ocrReportWriter := csv.NewWriter(ocrReportFile)
err = ocrReportWriter.Write([]string{fmt.Sprintf("OCRv%s Soak Test Report", o.OCRVersion)})
if err != nil {
return err
}
err = ocrReportWriter.Write([]string{
"Namespace",
o.namespace,
"Started At",
o.StartTime.Format("2006-01-02 15:04:05.00 MST"),
"Test Duration",
time.Since(o.StartTime).String(),
})
if err != nil {
return err
}
err = ocrReportWriter.Write([]string{})
if err != nil {
return err
}
if len(o.anomalies) > 0 {
err = ocrReportWriter.Write([]string{"Anomalies Found"})
if err != nil {
return err
}
err = ocrReportWriter.WriteAll(o.anomalies)
if err != nil {
return err
}
err = ocrReportWriter.Write([]string{})
if err != nil {
return err
}
}
err = ocrReportWriter.Write([]string{"Timeline"})
if err != nil {
return err
}
err = ocrReportWriter.Write([]string{
"Time",
"Message",
})
if err != nil {
return err
}
err = ocrReportWriter.WriteAll(o.timeLine)
if err != nil {
return err
}
ocrReportWriter.Flush()
log.Info().Str("Location", reportLocation).Msg("Wrote CSV file")
return nil
}
// SendNotification sends a slack message to a slack webhook and uploads test artifacts
func (o *OCRSoakTestReporter) SendSlackNotification(t *testing.T, slackClient *slack.Client, _ testreporters.GrafanaURLProvider) error {
if slackClient == nil {
slackClient = slack.New(testreporters.SlackAPIKey)
}
testFailed := t.Failed()
headerText := fmt.Sprintf(":white_check_mark: OCRv%s Soak Test PASSED :white_check_mark:", o.OCRVersion)
if testFailed {
headerText = ":x: OCR Soak Test FAILED :x:"
} else if o.AnomaliesDetected {
headerText = ":warning: OCR Soak Test Found Anomalies :warning:"
}
messageBlocks := testreporters.CommonSlackNotificationBlocks(
headerText, fmt.Sprintf("%s | Test took: %s", o.namespace, time.Since(o.StartTime).Truncate(time.Second).String()), o.csvLocation,
)
ts, err := testreporters.SendSlackMessage(slackClient, slack.MsgOptionBlocks(messageBlocks...))
if err != nil {
return err
}
return testreporters.UploadSlackFile(slackClient, slack.FileUploadParameters{
Title: fmt.Sprintf("OCR Soak Test Report %s", o.namespace),
Filetype: "csv",
Filename: fmt.Sprintf("ocr_soak_%s.csv", o.namespace),
File: o.csvLocation,
InitialComment: fmt.Sprintf("OCR Soak Test Report %s.", o.namespace),
Channels: []string{testreporters.SlackChannel},
ThreadTimestamp: ts,
})
}