-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
593 lines (525 loc) · 16.1 KB
/
db.go
File metadata and controls
593 lines (525 loc) · 16.1 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
const pgSchema = `
CREATE TABLE IF NOT EXISTS blocks (
slot BIGINT PRIMARY KEY,
epoch INTEGER NOT NULL,
block_hash TEXT NOT NULL,
vrf_output BYTEA NOT NULL,
nonce_value BYTEA NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_blocks_epoch ON blocks(epoch);
CREATE TABLE IF NOT EXISTS epoch_nonces (
epoch INTEGER PRIMARY KEY,
evolving_nonce BYTEA NOT NULL,
candidate_nonce BYTEA,
final_nonce BYTEA,
block_count INTEGER DEFAULT 0,
source TEXT DEFAULT 'chain_sync',
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS leader_schedules (
epoch INTEGER PRIMARY KEY,
pool_stake BIGINT NOT NULL,
total_stake BIGINT NOT NULL,
epoch_nonce TEXT NOT NULL,
sigma DOUBLE PRECISION,
ideal_slots DOUBLE PRECISION,
slot_count INTEGER DEFAULT 0,
performance DOUBLE PRECISION,
slots JSONB DEFAULT '[]',
posted BOOLEAN DEFAULT FALSE,
history_classified BOOLEAN DEFAULT FALSE,
calculated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS slot_outcomes (
epoch INTEGER NOT NULL,
slot BIGINT NOT NULL,
outcome TEXT NOT NULL,
opponent TEXT,
PRIMARY KEY (epoch, slot)
);
CREATE INDEX IF NOT EXISTS idx_slot_outcomes_epoch ON slot_outcomes(epoch);
`
// PgStore implements Store using PostgreSQL via pgx.
type PgStore struct {
pool *pgxpool.Pool
}
// NewPgStore connects to PostgreSQL and creates tables if they don't exist.
func NewPgStore(connString string) (*PgStore, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
pool, err := pgxpool.New(ctx, connString)
if err != nil {
return nil, fmt.Errorf("connecting to database: %w", err)
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, fmt.Errorf("pinging database: %w", err)
}
if _, err := pool.Exec(ctx, pgSchema); err != nil {
pool.Close()
return nil, fmt.Errorf("creating schema: %w", err)
}
// Migrations for existing databases
pool.Exec(ctx, `ALTER TABLE leader_schedules ADD COLUMN IF NOT EXISTS history_classified BOOLEAN DEFAULT FALSE`)
log.Println("PostgreSQL connected and schema initialized")
return &PgStore{pool: pool}, nil
}
func (s *PgStore) Close() error {
s.pool.Close()
return nil
}
func (s *PgStore) InsertBlock(ctx context.Context, slot uint64, epoch int, blockHash string, vrfOutput, nonceValue []byte) (bool, error) {
result, err := s.pool.Exec(ctx,
`INSERT INTO blocks (slot, epoch, block_hash, vrf_output, nonce_value)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (slot) DO NOTHING`,
int64(slot), epoch, blockHash, vrfOutput, nonceValue,
)
if err != nil {
return false, err
}
return result.RowsAffected() > 0, nil
}
func (s *PgStore) UpsertEvolvingNonce(ctx context.Context, epoch int, nonce []byte, blockCount int) error {
_, err := s.pool.Exec(ctx,
`INSERT INTO epoch_nonces (epoch, evolving_nonce, block_count, updated_at)
VALUES ($1, $2, $3, NOW())
ON CONFLICT (epoch) DO UPDATE SET
evolving_nonce = EXCLUDED.evolving_nonce,
block_count = EXCLUDED.block_count,
updated_at = NOW()`,
epoch, nonce, blockCount,
)
return err
}
func (s *PgStore) SetCandidateNonce(ctx context.Context, epoch int, nonce []byte) error {
_, err := s.pool.Exec(ctx,
`INSERT INTO epoch_nonces (epoch, evolving_nonce, candidate_nonce, updated_at)
VALUES ($1, $2, $2, NOW())
ON CONFLICT (epoch) DO UPDATE SET
candidate_nonce = EXCLUDED.candidate_nonce,
updated_at = NOW()`,
epoch, nonce,
)
return err
}
func (s *PgStore) DeleteCandidateNonce(ctx context.Context, epoch int) error {
_, err := s.pool.Exec(ctx,
`UPDATE epoch_nonces SET candidate_nonce = NULL, updated_at = NOW() WHERE epoch = $1`,
epoch,
)
return err
}
func (s *PgStore) SetFinalNonce(ctx context.Context, epoch int, nonce []byte, source string) error {
_, err := s.pool.Exec(ctx,
`INSERT INTO epoch_nonces (epoch, evolving_nonce, final_nonce, source, updated_at)
VALUES ($1, $2, $2, $3, NOW())
ON CONFLICT (epoch) DO UPDATE SET
final_nonce = EXCLUDED.final_nonce,
source = EXCLUDED.source,
updated_at = NOW()`,
epoch, nonce, source,
)
return err
}
func (s *PgStore) GetFinalNonce(ctx context.Context, epoch int) ([]byte, error) {
var nonce []byte
err := s.pool.QueryRow(ctx,
`SELECT final_nonce FROM epoch_nonces WHERE epoch = $1 AND final_nonce IS NOT NULL`,
epoch,
).Scan(&nonce)
if err != nil {
return nil, err
}
return nonce, nil
}
func (s *PgStore) GetEvolvingNonce(ctx context.Context, epoch int) ([]byte, int, error) {
var nonce []byte
var blockCount int
err := s.pool.QueryRow(ctx,
`SELECT evolving_nonce, block_count FROM epoch_nonces WHERE epoch = $1`,
epoch,
).Scan(&nonce, &blockCount)
if err != nil {
return nil, 0, err
}
return nonce, blockCount, nil
}
func (s *PgStore) InsertLeaderSchedule(ctx context.Context, schedule *LeaderSchedule) error {
slotsJSON, err := json.Marshal(schedule.AssignedSlots)
if err != nil {
return fmt.Errorf("marshaling slots: %w", err)
}
performance := 0.0
if schedule.IdealSlots > 0 {
performance = float64(len(schedule.AssignedSlots)) / schedule.IdealSlots * 100
}
_, err = s.pool.Exec(ctx,
`INSERT INTO leader_schedules (epoch, pool_stake, total_stake, epoch_nonce, sigma, ideal_slots, slot_count, performance, slots, calculated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT (epoch) DO UPDATE SET
pool_stake = EXCLUDED.pool_stake,
total_stake = EXCLUDED.total_stake,
epoch_nonce = EXCLUDED.epoch_nonce,
sigma = EXCLUDED.sigma,
ideal_slots = EXCLUDED.ideal_slots,
slot_count = EXCLUDED.slot_count,
performance = EXCLUDED.performance,
slots = EXCLUDED.slots,
calculated_at = EXCLUDED.calculated_at`,
schedule.Epoch,
int64(schedule.PoolStake),
int64(schedule.TotalStake),
schedule.EpochNonce,
schedule.Sigma,
schedule.IdealSlots,
len(schedule.AssignedSlots),
performance,
slotsJSON,
schedule.CalculatedAt,
)
return err
}
func (s *PgStore) IsSchedulePosted(ctx context.Context, epoch int) bool {
var posted bool
err := s.pool.QueryRow(ctx,
`SELECT posted FROM leader_schedules WHERE epoch = $1`,
epoch,
).Scan(&posted)
if err != nil {
return false
}
return posted
}
func (s *PgStore) MarkSchedulePosted(ctx context.Context, epoch int) error {
_, err := s.pool.Exec(ctx,
`UPDATE leader_schedules SET posted = TRUE WHERE epoch = $1`,
epoch,
)
return err
}
func (s *PgStore) GetLastSyncedSlot(ctx context.Context) (uint64, error) {
var slot *int64
err := s.pool.QueryRow(ctx,
`SELECT MAX(slot) FROM blocks`,
).Scan(&slot)
if err != nil {
return 0, err
}
if slot == nil {
return 0, nil
}
return uint64(*slot), nil
}
func (s *PgStore) GetBlockHash(ctx context.Context, slot uint64) (string, error) {
var hash string
err := s.pool.QueryRow(ctx,
`SELECT block_hash FROM blocks WHERE slot = $1`,
int64(slot),
).Scan(&hash)
return hash, err
}
func (s *PgStore) GetForgedSlots(ctx context.Context, epoch int) ([]uint64, error) {
rows, err := s.pool.Query(ctx,
`SELECT slot FROM blocks WHERE epoch = $1 ORDER BY slot`, epoch)
if err != nil {
return nil, err
}
defer rows.Close()
var slots []uint64
for rows.Next() {
var slot int64
if err := rows.Scan(&slot); err != nil {
return nil, err
}
slots = append(slots, uint64(slot))
}
return slots, rows.Err()
}
func (s *PgStore) InsertBlockBatch(ctx context.Context, blocks []BlockData) (int, error) {
tx, err := s.pool.Begin(ctx)
if err != nil {
return 0, err
}
defer tx.Rollback(ctx)
// Temp table with no constraints — CopyFrom always succeeds even with duplicates
_, err = tx.Exec(ctx, `CREATE TEMP TABLE blocks_staging (
slot BIGINT, epoch INT, block_hash TEXT, vrf_output BYTEA, nonce_value BYTEA
) ON COMMIT DROP`)
if err != nil {
return 0, err
}
rows := make([][]interface{}, len(blocks))
for i, b := range blocks {
nonceValue := vrfNonceValueForEpoch(b.VrfOutput, b.Epoch, b.NetworkMagic)
rows[i] = []interface{}{int64(b.Slot), b.Epoch, b.BlockHash, b.VrfOutput, nonceValue}
}
// COPY into staging (no constraints = no failures on duplicate keys)
_, err = tx.CopyFrom(ctx,
pgx.Identifier{"blocks_staging"},
[]string{"slot", "epoch", "block_hash", "vrf_output", "nonce_value"},
pgx.CopyFromRows(rows),
)
if err != nil {
return 0, err
}
// Merge into blocks — duplicates silently skipped, count actually inserted
result, err := tx.Exec(ctx, `INSERT INTO blocks (slot, epoch, block_hash, vrf_output, nonce_value)
SELECT slot, epoch, block_hash, vrf_output, nonce_value FROM blocks_staging
ON CONFLICT (slot) DO NOTHING`)
if err != nil {
return 0, err
}
inserted := int(result.RowsAffected())
return inserted, tx.Commit(ctx)
}
func (s *PgStore) GetBlockByHash(ctx context.Context, hashPrefix string) ([]BlockRecord, error) {
rows, err := s.pool.Query(ctx,
`SELECT slot, epoch, block_hash FROM blocks WHERE block_hash LIKE $1 ORDER BY slot`,
hashPrefix+"%",
)
if err != nil {
return nil, err
}
defer rows.Close()
var records []BlockRecord
for rows.Next() {
var r BlockRecord
if err := rows.Scan(&r.Slot, &r.Epoch, &r.BlockHash); err != nil {
return nil, err
}
records = append(records, r)
}
if err := rows.Err(); err != nil {
return nil, err
}
return records, nil
}
func (s *PgStore) GetLeaderSchedule(ctx context.Context, epoch int) (*LeaderSchedule, error) {
var (
poolStake int64
totalStake int64
epochNonce string
sigma float64
idealSlots float64
slotsJSON []byte
calculatedAt time.Time
)
err := s.pool.QueryRow(ctx,
`SELECT pool_stake, total_stake, epoch_nonce, sigma, ideal_slots, slots, calculated_at
FROM leader_schedules WHERE epoch = $1`,
epoch,
).Scan(&poolStake, &totalStake, &epochNonce, &sigma, &idealSlots, &slotsJSON, &calculatedAt)
if err != nil {
return nil, err
}
var slots []LeaderSlot
if err := json.Unmarshal(slotsJSON, &slots); err != nil {
return nil, fmt.Errorf("unmarshaling slots: %w", err)
}
return &LeaderSchedule{
Epoch: epoch,
EpochNonce: epochNonce,
PoolStake: uint64(poolStake),
TotalStake: uint64(totalStake),
Sigma: sigma,
IdealSlots: idealSlots,
AssignedSlots: slots,
CalculatedAt: calculatedAt,
}, nil
}
func (s *PgStore) StreamBlockVrfOutputs(ctx context.Context) (BlockVrfRows, error) {
rows, err := s.pool.Query(ctx,
`SELECT epoch, slot, vrf_output, nonce_value, block_hash FROM blocks ORDER BY slot`,
)
if err != nil {
return nil, err
}
return &pgBlockVrfRows{rows: rows}, nil
}
type pgBlockVrfRows struct {
rows pgx.Rows
epoch int
slot uint64
vrfOutput []byte
nonceValue []byte
blockHash string
err error
closed bool
}
func (r *pgBlockVrfRows) Next() bool {
if r.closed {
return false
}
if !r.rows.Next() {
r.err = r.rows.Err()
r.closed = true
return false
}
var slotInt64 int64
r.err = r.rows.Scan(&r.epoch, &slotInt64, &r.vrfOutput, &r.nonceValue, &r.blockHash)
r.slot = uint64(slotInt64)
return r.err == nil
}
func (r *pgBlockVrfRows) Scan() (epoch int, slot uint64, vrfOutput []byte, nonceValue []byte, blockHash string, err error) {
return r.epoch, r.slot, r.vrfOutput, r.nonceValue, r.blockHash, r.err
}
func (r *pgBlockVrfRows) Close() {
if !r.closed {
r.rows.Close()
r.closed = true
}
}
func (r *pgBlockVrfRows) Err() error {
return r.err
}
func (s *PgStore) GetLastNBlocks(ctx context.Context, n int) ([]BlockRecord, error) {
rows, err := s.pool.Query(ctx,
`SELECT slot, epoch, block_hash FROM blocks ORDER BY slot DESC LIMIT $1`, n)
if err != nil {
return nil, err
}
defer rows.Close()
var records []BlockRecord
for rows.Next() {
var r BlockRecord
if err := rows.Scan(&r.Slot, &r.Epoch, &r.BlockHash); err != nil {
return nil, err
}
records = append(records, r)
}
return records, rows.Err()
}
func (s *PgStore) GetBlockCountForEpoch(ctx context.Context, epoch int) (int, error) {
var count int
err := s.pool.QueryRow(ctx,
`SELECT COUNT(*) FROM blocks WHERE epoch = $1`, epoch).Scan(&count)
return count, err
}
func (s *PgStore) GetVrfOutputsForEpoch(ctx context.Context, epoch int) ([]VrfBlock, error) {
rows, err := s.pool.Query(ctx,
`SELECT epoch, vrf_output FROM blocks WHERE epoch = $1 ORDER BY slot`, epoch)
if err != nil {
return nil, err
}
defer rows.Close()
var blocks []VrfBlock
for rows.Next() {
var b VrfBlock
if err := rows.Scan(&b.Epoch, &b.VrfOutput); err != nil {
return nil, err
}
blocks = append(blocks, b)
}
return blocks, rows.Err()
}
func (s *PgStore) GetCandidateNonce(ctx context.Context, epoch int) ([]byte, error) {
var nonce []byte
err := s.pool.QueryRow(ctx,
`SELECT candidate_nonce FROM epoch_nonces WHERE epoch = $1 AND candidate_nonce IS NOT NULL`, epoch).Scan(&nonce)
if err != nil {
return nil, err
}
return nonce, nil
}
// GetPrevHashOfLastBlock returns the block hash of the second-to-last block
// in the given epoch. This is the prevHash of the last block, which is what
// the Cardano node uses for praosStateLabNonce (η_ph in the TICKN rule).
func (s *PgStore) GetPrevHashOfLastBlock(ctx context.Context, epoch int) (string, error) {
var hash string
err := s.pool.QueryRow(ctx,
`SELECT block_hash FROM blocks WHERE epoch = $1 ORDER BY slot DESC LIMIT 1 OFFSET 1`, epoch).Scan(&hash)
if err != nil {
return "", err
}
return hash, nil
}
func (s *PgStore) UpsertSlotOutcomes(ctx context.Context, epoch int, outcomes []SlotOutcome) error {
tx, err := s.pool.Begin(ctx)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback(ctx)
for _, o := range outcomes {
_, err := tx.Exec(ctx,
`INSERT INTO slot_outcomes (epoch, slot, outcome, opponent)
VALUES ($1, $2, $3, $4)
ON CONFLICT (epoch, slot) DO UPDATE SET
outcome = EXCLUDED.outcome,
opponent = EXCLUDED.opponent`,
o.Epoch, int64(o.Slot), o.Outcome, o.Opponent)
if err != nil {
return fmt.Errorf("upsert slot %d: %w", o.Slot, err)
}
}
return tx.Commit(ctx)
}
func (s *PgStore) GetSlotOutcomes(ctx context.Context, epoch int) ([]SlotOutcome, error) {
rows, err := s.pool.Query(ctx,
`SELECT epoch, slot, outcome, COALESCE(opponent, '') FROM slot_outcomes WHERE epoch = $1 ORDER BY slot`, epoch)
if err != nil {
return nil, err
}
defer rows.Close()
var outcomes []SlotOutcome
for rows.Next() {
var o SlotOutcome
if err := rows.Scan(&o.Epoch, &o.Slot, &o.Outcome, &o.Opponent); err != nil {
return nil, err
}
outcomes = append(outcomes, o)
}
return outcomes, rows.Err()
}
func (s *PgStore) IsEpochClassified(ctx context.Context, epoch int) bool {
var classified bool
err := s.pool.QueryRow(ctx,
`SELECT history_classified FROM leader_schedules WHERE epoch = $1`, epoch).Scan(&classified)
if err != nil {
return false
}
return classified
}
func (s *PgStore) MarkEpochClassified(ctx context.Context, epoch int) error {
_, err := s.pool.Exec(ctx,
`UPDATE leader_schedules SET history_classified = TRUE WHERE epoch = $1`, epoch)
return err
}
func (s *PgStore) DeleteSlotOutcomesBefore(ctx context.Context, epoch int) (int64, error) {
tag, err := s.pool.Exec(ctx,
`DELETE FROM slot_outcomes WHERE epoch < $1`, epoch)
if err != nil {
return 0, err
}
// Also unmark those epochs as classified so they won't be skipped
_, _ = s.pool.Exec(ctx,
`UPDATE leader_schedules SET history_classified = FALSE WHERE epoch < $1`, epoch)
return tag.RowsAffected(), nil
}
func (s *PgStore) HasBlockAtSlot(ctx context.Context, slot uint64) (bool, error) {
var exists bool
err := s.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM blocks WHERE slot = $1)`, slot).Scan(&exists)
return exists, err
}
func (s *PgStore) DeleteBlocksAfterSlot(ctx context.Context, slot uint64) (int64, error) {
tag, err := s.pool.Exec(ctx, `DELETE FROM blocks WHERE slot > $1`, slot)
if err != nil {
return 0, err
}
return tag.RowsAffected(), nil
}
func (s *PgStore) TruncateAll(ctx context.Context) error {
_, err := s.pool.Exec(ctx, `TRUNCATE blocks, epoch_nonces, leader_schedules, slot_outcomes`)
return err
}