-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
685 lines (610 loc) · 19.9 KB
/
store.go
File metadata and controls
685 lines (610 loc) · 19.9 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"time"
_ "modernc.org/sqlite"
)
// BlockData holds block information for batch processing during chain sync.
type BlockData struct {
Slot uint64
Epoch int
BlockHash string
VrfOutput []byte
NetworkMagic int
}
// BlockVrfRows is an iterator over blocks returning raw VRF output for integrity checking.
type BlockVrfRows interface {
Next() bool
Scan() (epoch int, slot uint64, vrfOutput []byte, nonceValue []byte, blockHash string, err error)
Close()
Err() error
}
// Store is the database abstraction layer for goduckbot.
// Both SQLite and PostgreSQL backends implement this interface.
type Store interface {
InsertBlock(ctx context.Context, slot uint64, epoch int, blockHash string, vrfOutput, nonceValue []byte) (bool, error)
InsertBlockBatch(ctx context.Context, blocks []BlockData) (int, error)
UpsertEvolvingNonce(ctx context.Context, epoch int, nonce []byte, blockCount int) error
SetCandidateNonce(ctx context.Context, epoch int, nonce []byte) error
DeleteCandidateNonce(ctx context.Context, epoch int) error
SetFinalNonce(ctx context.Context, epoch int, nonce []byte, source string) error
GetFinalNonce(ctx context.Context, epoch int) ([]byte, error)
GetEvolvingNonce(ctx context.Context, epoch int) ([]byte, int, error)
GetBlockHash(ctx context.Context, slot uint64) (string, error)
GetBlockByHash(ctx context.Context, hashPrefix string) ([]BlockRecord, error)
InsertLeaderSchedule(ctx context.Context, schedule *LeaderSchedule) error
GetLeaderSchedule(ctx context.Context, epoch int) (*LeaderSchedule, error)
IsSchedulePosted(ctx context.Context, epoch int) bool
MarkSchedulePosted(ctx context.Context, epoch int) error
GetForgedSlots(ctx context.Context, epoch int) ([]uint64, error)
GetLastSyncedSlot(ctx context.Context) (uint64, error)
StreamBlockVrfOutputs(ctx context.Context) (BlockVrfRows, error)
GetLastNBlocks(ctx context.Context, n int) ([]BlockRecord, error)
GetBlockCountForEpoch(ctx context.Context, epoch int) (int, error)
GetVrfOutputsForEpoch(ctx context.Context, epoch int) ([]VrfBlock, error)
GetCandidateNonce(ctx context.Context, epoch int) ([]byte, error)
GetPrevHashOfLastBlock(ctx context.Context, epoch int) (string, error)
UpsertSlotOutcomes(ctx context.Context, epoch int, outcomes []SlotOutcome) error
GetSlotOutcomes(ctx context.Context, epoch int) ([]SlotOutcome, error)
IsEpochClassified(ctx context.Context, epoch int) bool
MarkEpochClassified(ctx context.Context, epoch int) error
DeleteSlotOutcomesBefore(ctx context.Context, epoch int) (int64, error)
HasBlockAtSlot(ctx context.Context, slot uint64) (bool, error)
DeleteBlocksAfterSlot(ctx context.Context, slot uint64) (int64, error)
TruncateAll(ctx context.Context) error
Close() error
}
// BlockRecord holds a block's on-chain data for validation queries.
type BlockRecord struct {
Slot uint64
Epoch int
BlockHash string
}
// VrfBlock holds the raw VRF output and epoch for a block, used for nonce recomputation.
type VrfBlock struct {
Epoch int
VrfOutput []byte
}
// SlotOutcome records the classification of an assigned leader slot.
type SlotOutcome struct {
Epoch int `json:"epoch"`
Slot uint64 `json:"slot"`
Outcome string `json:"outcome"` // "forged", "battle", "missed"
Opponent string `json:"opponent"` // bech32 pool ID (battle only)
}
// SqliteStore implements Store using SQLite via modernc.org/sqlite (pure Go, no CGO).
type SqliteStore struct {
db *sql.DB
}
const sqliteSchema = `
CREATE TABLE IF NOT EXISTS blocks (
slot INTEGER PRIMARY KEY,
epoch INTEGER NOT NULL,
block_hash TEXT NOT NULL,
vrf_output BLOB NOT NULL,
nonce_value BLOB NOT NULL,
created_at TEXT DEFAULT (datetime('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 BLOB NOT NULL,
candidate_nonce BLOB,
final_nonce BLOB,
block_count INTEGER DEFAULT 0,
source TEXT DEFAULT 'chain_sync',
updated_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS leader_schedules (
epoch INTEGER PRIMARY KEY,
pool_stake INTEGER NOT NULL,
total_stake INTEGER NOT NULL,
epoch_nonce TEXT NOT NULL,
sigma REAL,
ideal_slots REAL,
slot_count INTEGER DEFAULT 0,
performance REAL,
slots TEXT DEFAULT '[]',
posted INTEGER DEFAULT 0,
history_classified INTEGER DEFAULT 0,
calculated_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS slot_outcomes (
epoch INTEGER NOT NULL,
slot INTEGER 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);
`
// NewSqliteStore opens (or creates) a SQLite database at the given path.
func NewSqliteStore(path string) (*SqliteStore, error) {
dsn := fmt.Sprintf("file:%s?_journal_mode=WAL&_busy_timeout=5000", path)
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, fmt.Errorf("opening sqlite: %w", err)
}
db.SetMaxOpenConns(1) // SQLite is single-writer
if err := db.Ping(); err != nil {
db.Close()
return nil, fmt.Errorf("pinging sqlite: %w", err)
}
if _, err := db.Exec(sqliteSchema); err != nil {
db.Close()
return nil, fmt.Errorf("creating sqlite schema: %w", err)
}
// Migrations for existing databases
db.Exec(`ALTER TABLE leader_schedules ADD COLUMN history_classified INTEGER DEFAULT 0`)
log.Printf("SQLite database opened at %s", path)
return &SqliteStore{db: db}, nil
}
func (s *SqliteStore) Close() error {
return s.db.Close()
}
func (s *SqliteStore) InsertBlock(ctx context.Context, slot uint64, epoch int, blockHash string, vrfOutput, nonceValue []byte) (bool, error) {
result, err := s.db.ExecContext(ctx,
`INSERT INTO blocks (slot, epoch, block_hash, vrf_output, nonce_value)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT (slot) DO NOTHING`,
int64(slot), epoch, blockHash, vrfOutput, nonceValue,
)
if err != nil {
return false, err
}
rows, err := result.RowsAffected()
if err != nil {
return false, err
}
return rows > 0, nil
}
func (s *SqliteStore) UpsertEvolvingNonce(ctx context.Context, epoch int, nonce []byte, blockCount int) error {
_, err := s.db.ExecContext(ctx,
`INSERT INTO epoch_nonces (epoch, evolving_nonce, block_count, updated_at)
VALUES (?, ?, ?, datetime('now'))
ON CONFLICT (epoch) DO UPDATE SET
evolving_nonce = excluded.evolving_nonce,
block_count = excluded.block_count,
updated_at = datetime('now')`,
epoch, nonce, blockCount,
)
return err
}
func (s *SqliteStore) SetCandidateNonce(ctx context.Context, epoch int, nonce []byte) error {
_, err := s.db.ExecContext(ctx,
`INSERT INTO epoch_nonces (epoch, evolving_nonce, candidate_nonce, updated_at)
VALUES (?, ?, ?, datetime('now'))
ON CONFLICT (epoch) DO UPDATE SET
candidate_nonce = excluded.candidate_nonce,
updated_at = datetime('now')`,
epoch, nonce, nonce,
)
return err
}
func (s *SqliteStore) DeleteCandidateNonce(ctx context.Context, epoch int) error {
_, err := s.db.ExecContext(ctx,
`UPDATE epoch_nonces SET candidate_nonce = NULL, updated_at = datetime('now') WHERE epoch = ?`,
epoch,
)
return err
}
func (s *SqliteStore) SetFinalNonce(ctx context.Context, epoch int, nonce []byte, source string) error {
_, err := s.db.ExecContext(ctx,
`INSERT INTO epoch_nonces (epoch, evolving_nonce, final_nonce, source, updated_at)
VALUES (?, ?, ?, ?, datetime('now'))
ON CONFLICT (epoch) DO UPDATE SET
final_nonce = excluded.final_nonce,
source = excluded.source,
updated_at = datetime('now')`,
epoch, nonce, nonce, source,
)
return err
}
func (s *SqliteStore) GetFinalNonce(ctx context.Context, epoch int) ([]byte, error) {
var nonce []byte
err := s.db.QueryRowContext(ctx,
`SELECT final_nonce FROM epoch_nonces WHERE epoch = ? AND final_nonce IS NOT NULL`,
epoch,
).Scan(&nonce)
if err != nil {
return nil, err
}
return nonce, nil
}
func (s *SqliteStore) GetEvolvingNonce(ctx context.Context, epoch int) ([]byte, int, error) {
var nonce []byte
var blockCount int
err := s.db.QueryRowContext(ctx,
`SELECT evolving_nonce, block_count FROM epoch_nonces WHERE epoch = ?`,
epoch,
).Scan(&nonce, &blockCount)
if err != nil {
return nil, 0, err
}
return nonce, blockCount, nil
}
func (s *SqliteStore) 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.db.ExecContext(ctx,
`INSERT INTO leader_schedules (epoch, pool_stake, total_stake, epoch_nonce, sigma, ideal_slots, slot_count, performance, slots, calculated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
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,
string(slotsJSON),
schedule.CalculatedAt.Format(time.RFC3339),
)
return err
}
func (s *SqliteStore) IsSchedulePosted(ctx context.Context, epoch int) bool {
var posted int
err := s.db.QueryRowContext(ctx,
`SELECT posted FROM leader_schedules WHERE epoch = ?`,
epoch,
).Scan(&posted)
if err != nil {
return false
}
return posted != 0
}
func (s *SqliteStore) MarkSchedulePosted(ctx context.Context, epoch int) error {
_, err := s.db.ExecContext(ctx,
`UPDATE leader_schedules SET posted = 1 WHERE epoch = ?`,
epoch,
)
return err
}
func (s *SqliteStore) GetLastSyncedSlot(ctx context.Context) (uint64, error) {
var slot *int64
err := s.db.QueryRowContext(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 *SqliteStore) GetBlockHash(ctx context.Context, slot uint64) (string, error) {
var hash string
err := s.db.QueryRowContext(ctx,
`SELECT block_hash FROM blocks WHERE slot = ?`,
int64(slot),
).Scan(&hash)
return hash, err
}
func (s *SqliteStore) GetForgedSlots(ctx context.Context, epoch int) ([]uint64, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT slot FROM blocks WHERE epoch = ? 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 *SqliteStore) InsertBlockBatch(ctx context.Context, blocks []BlockData) (int, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return 0, fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback()
stmt, err := tx.PrepareContext(ctx,
`INSERT INTO blocks (slot, epoch, block_hash, vrf_output, nonce_value)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT (slot) DO NOTHING`,
)
if err != nil {
return 0, fmt.Errorf("prepare: %w", err)
}
defer stmt.Close()
inserted := 0
for _, b := range blocks {
nonceValue := vrfNonceValueForEpoch(b.VrfOutput, b.Epoch, b.NetworkMagic)
result, err := stmt.ExecContext(ctx, int64(b.Slot), b.Epoch, b.BlockHash, b.VrfOutput, nonceValue)
if err != nil {
return 0, fmt.Errorf("insert slot %d: %w", b.Slot, err)
}
if n, _ := result.RowsAffected(); n > 0 {
inserted++
}
}
return inserted, tx.Commit()
}
func (s *SqliteStore) GetBlockByHash(ctx context.Context, hashPrefix string) ([]BlockRecord, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT slot, epoch, block_hash FROM blocks WHERE block_hash LIKE ? ORDER BY slot`,
hashPrefix+"%",
)
if err != nil {
return nil, err
}
defer rows.Close()
var records []BlockRecord
for rows.Next() {
var r BlockRecord
var slotInt64 int64
if err := rows.Scan(&slotInt64, &r.Epoch, &r.BlockHash); err != nil {
return nil, err
}
r.Slot = uint64(slotInt64)
records = append(records, r)
}
if err := rows.Err(); err != nil {
return nil, err
}
return records, nil
}
func (s *SqliteStore) GetLeaderSchedule(ctx context.Context, epoch int) (*LeaderSchedule, error) {
var (
poolStake int64
totalStake int64
epochNonce string
sigma float64
idealSlots float64
slotsJSON string
calculatedAtStr string
)
err := s.db.QueryRowContext(ctx,
`SELECT pool_stake, total_stake, epoch_nonce, sigma, ideal_slots, slots, calculated_at
FROM leader_schedules WHERE epoch = ?`,
epoch,
).Scan(&poolStake, &totalStake, &epochNonce, &sigma, &idealSlots, &slotsJSON, &calculatedAtStr)
if err != nil {
return nil, err
}
var slots []LeaderSlot
if err := json.Unmarshal([]byte(slotsJSON), &slots); err != nil {
return nil, fmt.Errorf("unmarshaling slots: %w", err)
}
calculatedAt, err := time.Parse(time.RFC3339, calculatedAtStr)
if err != nil {
return nil, fmt.Errorf("parsing calculated_at: %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 *SqliteStore) StreamBlockVrfOutputs(ctx context.Context) (BlockVrfRows, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT epoch, slot, vrf_output, nonce_value, block_hash FROM blocks ORDER BY slot`,
)
if err != nil {
return nil, err
}
return &sqliteBlockVrfRows{rows: rows}, nil
}
type sqliteBlockVrfRows struct {
rows *sql.Rows
epoch int
slot uint64
vrfOutput []byte
nonceValue []byte
blockHash string
err error
closed bool
}
func (r *sqliteBlockVrfRows) 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 *sqliteBlockVrfRows) 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 *sqliteBlockVrfRows) Close() {
if !r.closed {
r.rows.Close()
r.closed = true
}
}
func (r *sqliteBlockVrfRows) Err() error {
return r.err
}
func (s *SqliteStore) GetLastNBlocks(ctx context.Context, n int) ([]BlockRecord, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT slot, epoch, block_hash FROM blocks ORDER BY slot DESC LIMIT ?`, n)
if err != nil {
return nil, err
}
defer rows.Close()
var records []BlockRecord
for rows.Next() {
var r BlockRecord
var slotInt64 int64
if err := rows.Scan(&slotInt64, &r.Epoch, &r.BlockHash); err != nil {
return nil, err
}
r.Slot = uint64(slotInt64)
records = append(records, r)
}
return records, rows.Err()
}
func (s *SqliteStore) GetBlockCountForEpoch(ctx context.Context, epoch int) (int, error) {
var count int
err := s.db.QueryRowContext(ctx,
`SELECT COUNT(*) FROM blocks WHERE epoch = ?`, epoch).Scan(&count)
return count, err
}
func (s *SqliteStore) GetVrfOutputsForEpoch(ctx context.Context, epoch int) ([]VrfBlock, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT epoch, vrf_output FROM blocks WHERE epoch = ? 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 *SqliteStore) GetCandidateNonce(ctx context.Context, epoch int) ([]byte, error) {
var nonce []byte
err := s.db.QueryRowContext(ctx,
`SELECT candidate_nonce FROM epoch_nonces WHERE epoch = ? 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 *SqliteStore) GetPrevHashOfLastBlock(ctx context.Context, epoch int) (string, error) {
var hash string
err := s.db.QueryRowContext(ctx,
`SELECT block_hash FROM blocks WHERE epoch = ? ORDER BY slot DESC LIMIT 1 OFFSET 1`, epoch).Scan(&hash)
if err != nil {
return "", err
}
return hash, nil
}
func (s *SqliteStore) UpsertSlotOutcomes(ctx context.Context, epoch int, outcomes []SlotOutcome) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback()
stmt, err := tx.PrepareContext(ctx,
`INSERT INTO slot_outcomes (epoch, slot, outcome, opponent)
VALUES (?, ?, ?, ?)
ON CONFLICT (epoch, slot) DO UPDATE SET
outcome = excluded.outcome,
opponent = excluded.opponent`)
if err != nil {
return fmt.Errorf("prepare: %w", err)
}
defer stmt.Close()
for _, o := range outcomes {
if _, err := stmt.ExecContext(ctx, o.Epoch, int64(o.Slot), o.Outcome, o.Opponent); err != nil {
return fmt.Errorf("upsert slot %d: %w", o.Slot, err)
}
}
return tx.Commit()
}
func (s *SqliteStore) GetSlotOutcomes(ctx context.Context, epoch int) ([]SlotOutcome, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT epoch, slot, outcome, COALESCE(opponent, '') FROM slot_outcomes WHERE epoch = ? ORDER BY slot`, epoch)
if err != nil {
return nil, err
}
defer rows.Close()
var outcomes []SlotOutcome
for rows.Next() {
var o SlotOutcome
var slot int64
if err := rows.Scan(&o.Epoch, &slot, &o.Outcome, &o.Opponent); err != nil {
return nil, err
}
o.Slot = uint64(slot)
outcomes = append(outcomes, o)
}
return outcomes, rows.Err()
}
func (s *SqliteStore) IsEpochClassified(ctx context.Context, epoch int) bool {
var classified int
err := s.db.QueryRowContext(ctx,
`SELECT history_classified FROM leader_schedules WHERE epoch = ?`, epoch).Scan(&classified)
if err != nil {
return false
}
return classified != 0
}
func (s *SqliteStore) MarkEpochClassified(ctx context.Context, epoch int) error {
_, err := s.db.ExecContext(ctx,
`UPDATE leader_schedules SET history_classified = 1 WHERE epoch = ?`, epoch)
return err
}
func (s *SqliteStore) DeleteSlotOutcomesBefore(ctx context.Context, epoch int) (int64, error) {
res, err := s.db.ExecContext(ctx,
`DELETE FROM slot_outcomes WHERE epoch < ?`, epoch)
if err != nil {
return 0, err
}
n, _ := res.RowsAffected()
// Also unmark those epochs as classified so they won't be skipped
_, _ = s.db.ExecContext(ctx,
`UPDATE leader_schedules SET history_classified = 0 WHERE epoch < ?`, epoch)
return n, nil
}
func (s *SqliteStore) HasBlockAtSlot(ctx context.Context, slot uint64) (bool, error) {
var exists bool
err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM blocks WHERE slot = ?)`, slot).Scan(&exists)
return exists, err
}
func (s *SqliteStore) DeleteBlocksAfterSlot(ctx context.Context, slot uint64) (int64, error) {
result, err := s.db.ExecContext(ctx, `DELETE FROM blocks WHERE slot > ?`, slot)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
func (s *SqliteStore) TruncateAll(ctx context.Context) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback()
for _, table := range []string{"blocks", "epoch_nonces", "leader_schedules", "slot_outcomes"} {
if _, err := tx.ExecContext(ctx, "DELETE FROM "+table); err != nil {
return fmt.Errorf("clearing %s: %w", table, err)
}
}
return tx.Commit()
}