-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
database.go
376 lines (316 loc) · 9.54 KB
/
database.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package ocr2
import (
"context"
"database/sql"
"encoding/binary"
"time"
"github.com/lib/pq"
"github.com/pkg/errors"
ocrcommon "github.com/smartcontractkit/libocr/commontypes"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)
type db struct {
q pg.Q
oracleSpecID int32
pluginID int32
lggr logger.SugaredLogger
}
var (
_ ocrtypes.Database = &db{}
)
// NewDB returns a new DB scoped to this oracleSpecID
func NewDB(sqlxDB *sqlx.DB, oracleSpecID int32, pluginID int32, lggr logger.Logger, cfg pg.QConfig) *db {
namedLogger := lggr.Named("OCR2.DB")
return &db{
q: pg.NewQ(sqlxDB, namedLogger, cfg),
oracleSpecID: oracleSpecID,
pluginID: pluginID,
lggr: logger.Sugared(lggr),
}
}
func (d *db) ReadState(ctx context.Context, cd ocrtypes.ConfigDigest) (ps *ocrtypes.PersistentState, err error) {
stmt := `
SELECT epoch, highest_sent_epoch, highest_received_epoch
FROM ocr2_persistent_states
WHERE ocr2_oracle_spec_id = $1 AND config_digest = $2
LIMIT 1`
ps = new(ocrtypes.PersistentState)
var tmp []int64
var highestSentEpochTmp int64
err = d.q.QueryRowxContext(ctx, stmt, d.oracleSpecID, cd).Scan(&ps.Epoch, &highestSentEpochTmp, pq.Array(&tmp))
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, errors.Wrap(err, "ReadState failed")
}
ps.HighestSentEpoch = uint32(highestSentEpochTmp)
for _, v := range tmp {
ps.HighestReceivedEpoch = append(ps.HighestReceivedEpoch, uint32(v))
}
return ps, nil
}
func (d *db) WriteState(ctx context.Context, cd ocrtypes.ConfigDigest, state ocrtypes.PersistentState) error {
var highestReceivedEpoch []int64
for _, v := range state.HighestReceivedEpoch {
highestReceivedEpoch = append(highestReceivedEpoch, int64(v))
}
stmt := `
INSERT INTO ocr2_persistent_states (
ocr2_oracle_spec_id,
config_digest,
epoch,
highest_sent_epoch,
highest_received_epoch,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
ON CONFLICT (ocr2_oracle_spec_id, config_digest)
DO UPDATE SET (
epoch,
highest_sent_epoch,
highest_received_epoch,
updated_at
) = (
EXCLUDED.epoch,
EXCLUDED.highest_sent_epoch,
EXCLUDED.highest_received_epoch,
NOW()
)`
_, err := d.q.WithOpts(pg.WithLongQueryTimeout()).ExecContext(
ctx, stmt, d.oracleSpecID, cd, state.Epoch, state.HighestSentEpoch, pq.Array(&highestReceivedEpoch),
)
return errors.Wrap(err, "WriteState failed")
}
func (d *db) ReadConfig(ctx context.Context) (c *ocrtypes.ContractConfig, err error) {
stmt := `
SELECT
config_digest,
config_count,
signers,
transmitters,
f,
onchain_config,
offchain_config_version,
offchain_config
FROM ocr2_contract_configs
WHERE ocr2_oracle_spec_id = $1 AND plugin_id = $2
LIMIT 1`
c = new(ocrtypes.ContractConfig)
digest := []byte{}
signers := [][]byte{}
transmitters := [][]byte{}
err = d.q.QueryRowx(stmt, d.oracleSpecID, d.pluginID).Scan(
&digest,
&c.ConfigCount,
(*pq.ByteaArray)(&signers),
(*pq.ByteaArray)(&transmitters),
&c.F,
&c.OnchainConfig,
&c.OffchainConfigVersion,
&c.OffchainConfig,
)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, errors.Wrap(err, "ReadConfig failed")
}
copy(c.ConfigDigest[:], digest)
c.Signers = []ocrtypes.OnchainPublicKey{}
for _, s := range signers {
c.Signers = append(c.Signers, s)
}
c.Transmitters = []ocrtypes.Account{}
for _, t := range transmitters {
transmitter := ocrtypes.Account(t)
c.Transmitters = append(c.Transmitters, transmitter)
}
return
}
func (d *db) WriteConfig(ctx context.Context, c ocrtypes.ContractConfig) error {
var signers [][]byte
for _, s := range c.Signers {
signers = append(signers, []byte(s))
}
stmt := `
INSERT INTO ocr2_contract_configs (
ocr2_oracle_spec_id,
plugin_id,
config_digest,
config_count,
signers,
transmitters,
f,
onchain_config,
offchain_config_version,
offchain_config,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, NOW(), NOW())
ON CONFLICT (ocr2_oracle_spec_id, plugin_id) DO UPDATE SET
config_digest = EXCLUDED.config_digest,
config_count = EXCLUDED.config_count,
signers = EXCLUDED.signers,
transmitters = EXCLUDED.transmitters,
f = EXCLUDED.f,
onchain_config = EXCLUDED.onchain_config,
offchain_config_version = EXCLUDED.offchain_config_version,
offchain_config = EXCLUDED.offchain_config,
updated_at = NOW()
`
_, err := d.q.ExecContext(ctx, stmt,
d.oracleSpecID,
d.pluginID,
c.ConfigDigest,
c.ConfigCount,
pq.ByteaArray(signers),
c.Transmitters,
c.F,
c.OnchainConfig,
c.OffchainConfigVersion,
c.OffchainConfig,
)
return errors.Wrap(err, "WriteConfig failed")
}
func (d *db) StorePendingTransmission(ctx context.Context, t ocrtypes.ReportTimestamp, tx ocrtypes.PendingTransmission) error {
var signatures [][]byte
for _, s := range tx.AttributedSignatures {
signatures = append(signatures, s.Signature)
buffer := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(buffer, int64(s.Signer))
signatures = append(signatures, buffer)
}
digest := make([]byte, 32)
copy(digest, t.ConfigDigest[:])
extraHash := make([]byte, 32)
copy(extraHash[:], tx.ExtraHash[:])
stmt := `
INSERT INTO ocr2_pending_transmissions (
ocr2_oracle_spec_id,
config_digest,
epoch,
round,
time,
extra_hash,
report,
attributed_signatures,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), NOW())
ON CONFLICT (ocr2_oracle_spec_id, config_digest, epoch, round) DO UPDATE SET
ocr2_oracle_spec_id = EXCLUDED.ocr2_oracle_spec_id,
config_digest = EXCLUDED.config_digest,
epoch = EXCLUDED.epoch,
round = EXCLUDED.round,
time = EXCLUDED.time,
extra_hash = EXCLUDED.extra_hash,
report = EXCLUDED.report,
attributed_signatures = EXCLUDED.attributed_signatures,
updated_at = NOW()
`
_, err := d.q.ExecContext(ctx, stmt,
d.oracleSpecID,
digest,
t.Epoch,
t.Round,
tx.Time,
extraHash,
tx.Report,
pq.ByteaArray(signatures),
)
return errors.Wrap(err, "StorePendingTransmission failed")
}
func (d *db) PendingTransmissionsWithConfigDigest(ctx context.Context, cd ocrtypes.ConfigDigest) (map[ocrtypes.ReportTimestamp]ocrtypes.PendingTransmission, error) {
stmt := `
SELECT
config_digest,
epoch,
round,
time,
extra_hash,
report,
attributed_signatures
FROM ocr2_pending_transmissions
WHERE ocr2_oracle_spec_id = $1 AND config_digest = $2
`
rows, err := d.q.QueryxContext(ctx, stmt, d.oracleSpecID, cd)
if err != nil {
return nil, errors.Wrap(err, "PendingTransmissionsWithConfigDigest failed to query rows")
}
defer d.lggr.ErrorIfFn(rows.Close, "Error closing ocr2_pending_transmissions rows")
m := make(map[ocrtypes.ReportTimestamp]ocrtypes.PendingTransmission)
for rows.Next() {
k := ocrtypes.ReportTimestamp{}
p := ocrtypes.PendingTransmission{}
signatures := [][]byte{}
digest := []byte{}
extraHash := []byte{}
report := []byte{}
if err := rows.Scan(&digest, &k.Epoch, &k.Round, &p.Time, &extraHash, &report, (*pq.ByteaArray)(&signatures)); err != nil {
return nil, errors.Wrap(err, "PendingTransmissionsWithConfigDigest failed to scan row")
}
copy(k.ConfigDigest[:], digest)
copy(p.ExtraHash[:], extraHash)
p.Report = make([]byte, len(report))
copy(p.Report[:], report)
for index := 0; index < len(signatures); index += 2 {
signature := signatures[index]
signer, _ := binary.Varint(signatures[index+1])
sig := ocrtypes.AttributedOnchainSignature{
Signature: signature,
Signer: ocrcommon.OracleID(signer),
}
p.AttributedSignatures = append(p.AttributedSignatures, sig)
}
m[k] = p
}
if err := rows.Err(); err != nil {
return m, err
}
return m, nil
}
func (d *db) DeletePendingTransmission(ctx context.Context, t ocrtypes.ReportTimestamp) (err error) {
_, err = d.q.WithOpts(pg.WithLongQueryTimeout()).ExecContext(ctx, `
DELETE FROM ocr2_pending_transmissions
WHERE ocr2_oracle_spec_id = $1 AND config_digest = $2 AND epoch = $3 AND round = $4
`, d.oracleSpecID, t.ConfigDigest, t.Epoch, t.Round)
err = errors.Wrap(err, "DeletePendingTransmission failed")
return
}
func (d *db) DeletePendingTransmissionsOlderThan(ctx context.Context, t time.Time) (err error) {
_, err = d.q.WithOpts(pg.WithLongQueryTimeout()).ExecContext(ctx, `
DELETE FROM ocr2_pending_transmissions
WHERE ocr2_oracle_spec_id = $1 AND time < $2
`, d.oracleSpecID, t)
err = errors.Wrap(err, "DeletePendingTransmissionsOlderThan failed")
return
}
func (d *db) ReadProtocolState(ctx context.Context, configDigest ocrtypes.ConfigDigest, key string) (value []byte, err error) {
err = d.q.GetContext(ctx, &value, `
SELECT value FROM ocr_protocol_states
WHERE config_digest = $1 AND key = $2;
`, configDigest, key)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
err = errors.Wrapf(err, "ReadProtocolState failed for job %d", d.oracleSpecID)
return
}
func (d *db) WriteProtocolState(ctx context.Context, configDigest ocrtypes.ConfigDigest, key string, value []byte) (err error) {
if value == nil {
_, err = d.q.ExecContext(ctx, `DELETE FROM ocr_protocol_states WHERE config_digest = $1 AND key = $2;`, configDigest, key)
} else {
_, err = d.q.ExecContext(ctx, `
INSERT INTO ocr_protocol_states (config_digest, key, value) VALUES ($1, $2, $3)
ON CONFLICT (config_digest, key) DO UPDATE SET value = $3;`, configDigest, key, value)
}
err = errors.Wrapf(err, "WriteProtocolState failed for job %d", d.oracleSpecID)
return
}