-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
db.go
222 lines (199 loc) · 6.57 KB
/
db.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
package persistence
import (
"bytes"
"context"
"database/sql"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
ocr2vrftypes "github.com/smartcontractkit/ocr2vrf/types"
"github.com/smartcontractkit/ocr2vrf/types/hash"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
)
var (
_ ocr2vrftypes.DKGSharePersistence = &shareDB{}
zeroHash hash.Hash
buckets = []float64{
float64(100 * time.Millisecond),
float64(200 * time.Millisecond),
float64(500 * time.Millisecond),
float64(1 * time.Second),
float64(2 * time.Second),
float64(5 * time.Second),
float64(10 * time.Second),
float64(30 * time.Second),
}
labels = []string{
"chainType", "chainID",
}
promWriteShareRecords = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "write_share_records_time",
Help: "The duration of the DKG WriteShareRecords call",
Buckets: buckets,
}, labels)
promReadShareRecords = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "read_share_records_time",
Help: "The duration of the DKG ReadShareRecords call.",
Buckets: buckets,
}, labels)
)
type shareDB struct {
q pg.Q
lggr logger.Logger
chainID *big.Int
chainType string
}
// NewShareDB creates a new DKG share database.
func NewShareDB(db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig, chainID *big.Int, chainType relay.Network) ocr2vrftypes.DKGSharePersistence {
return &shareDB{
q: pg.NewQ(db, lggr, cfg),
lggr: lggr,
chainID: chainID,
chainType: string(chainType),
}
}
// WriteShareRecords writes the provided (already encrypted)
// share records to the Chainlink database.
func (s *shareDB) WriteShareRecords(
ctx context.Context,
cfgDgst ocrtypes.ConfigDigest,
keyID [32]byte,
shareRecords []ocr2vrftypes.PersistentShareSetRecord,
) error {
lggr := s.lggr.With(
"configDigest", hexutil.Encode(cfgDgst[:]),
"keyID", hexutil.Encode(keyID[:]))
start := time.Now()
defer func() {
duration := time.Since(start)
promWriteShareRecords.WithLabelValues(s.chainType, s.chainID.String()).Observe(float64(duration))
// lggr.Debugw("Inserted DKG shares into DB", "duration", duration) // see ocr2vrf code for logs
}()
var named []dkgShare
for _, record := range shareRecords {
if bytes.Equal(record.Hash[:], zeroHash[:]) {
// see ocr2vrf for logging
// lggr.Warnw("skipping record with zero hash",
// "player", record.Dealer.String(),
// "hash", hexutil.Encode(record.Hash[:]),
// )
continue
}
// XXX: this might be expensive, but is a good sanity check.
localHash := hash.GetHash(record.MarshaledShareRecord)
if !bytes.Equal(record.Hash[:], localHash[:]) {
return fmt.Errorf("local hash doesn't match given hash in record, expected: %x, got: %x",
localHash[:], record.Hash[:])
}
var h hash.Hash
if copied := copy(h[:], record.Hash[:]); copied != 32 {
return fmt.Errorf("wrong number of bytes copied in hash (dealer:%s) %x: %d",
record.Dealer.String(), record.Hash[:], copied)
}
named = append(named, dkgShare{
ConfigDigest: cfgDgst[:],
KeyID: keyID[:],
Dealer: record.Dealer.Marshal(),
MarshaledShareRecord: record.MarshaledShareRecord,
/* TODO/WTF: can't do "record.Hash[:]": this leads to store the last record's hash for all the records! */
RecordHash: h[:],
})
}
if len(named) == 0 {
lggr.Infow("No valid share records to insert")
return nil
}
// see ocr2vrf for logging
// lggr.Infow("Inserting DKG shares into DB",
// "shareHashes", shareHashes(shareRecords),
// "numRecords", len(shareRecords),
// "numNamed", len(named))
// Always upsert because we want the number of rows in the table to match
// the number of members of the committee.
query := `
INSERT INTO dkg_shares (config_digest, key_id, dealer, marshaled_share_record, record_hash)
VALUES (:config_digest, :key_id, :dealer, :marshaled_share_record, :record_hash)
ON CONFLICT ON CONSTRAINT dkg_shares_pkey
DO UPDATE SET marshaled_share_record = EXCLUDED.marshaled_share_record, record_hash = EXCLUDED.record_hash
`
return s.q.ExecQNamed(query, named[:])
}
// ReadShareRecords retrieves any share records in the database that correspond
// to the provided config digest and DKG key ID.
func (s *shareDB) ReadShareRecords(
cfgDgst ocrtypes.ConfigDigest,
keyID [32]byte,
) (
retrievedShares []ocr2vrftypes.PersistentShareSetRecord,
err error,
) {
lggr := s.lggr.With(
"configDigest", hexutil.Encode(cfgDgst[:]),
"keyID", hexutil.Encode(keyID[:]))
start := time.Now()
defer func() {
duration := time.Since(start)
promReadShareRecords.WithLabelValues(s.chainType, s.chainID.String()).Observe(float64(duration))
lggr.Debugw("Finished reading DKG shares from DB", "duration", duration)
}()
a := map[string]any{
"config_digest": cfgDgst[:],
"key_id": keyID[:],
}
query, args, err := sqlx.Named(
`
SELECT *
FROM dkg_shares
WHERE config_digest = :config_digest
AND key_id = :key_id
`, a)
if err != nil {
return nil, errors.Wrap(err, "sqlx Named")
}
query = s.q.Rebind(query)
var dkgShares []dkgShare
err = s.q.Select(&dkgShares, query, args...)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, err
}
for _, share := range dkgShares {
playerIdx, _, err := ocr2vrftypes.UnmarshalPlayerIdx(share.Dealer)
if err != nil {
return nil, errors.Wrapf(err, "unmarshalling %x", share.Dealer)
}
var h hash.Hash
if copied := copy(h[:], share.RecordHash); copied != 32 {
return nil, fmt.Errorf("wrong number of bytes copied in hash %x: %d", share.RecordHash, copied)
}
// NOTE: no integrity check on share.MarshaledShareRecord
// because caller will do it anyways, so it'd be wasteful.
retrievedShares = append(retrievedShares, ocr2vrftypes.PersistentShareSetRecord{
Dealer: *playerIdx,
MarshaledShareRecord: share.MarshaledShareRecord,
Hash: h,
})
}
lggr.Debugw("Read DKG shares from DB",
"shareRecords", shareHashes(retrievedShares),
"numRecords", len(dkgShares),
)
return retrievedShares, nil
}
func shareHashes(shareRecords []ocr2vrftypes.PersistentShareSetRecord) []string {
r := make([]string, len(shareRecords))
for i, record := range shareRecords {
r[i] = hexutil.Encode(record.Hash[:])
}
return r
}