Persist found blocks in SQLite (Phase 2b.5)

New internal/store package wraps modernc.org/sqlite (pure Go,
no CGO) with a BlockStore that exposes Open/Close/InsertBlock/
Recent. The aggregator now accepts an optional *store.BlockStore;
on Run() it loads up to maxBlockHistory rows from the store before
the first refresh, and each ingested block gets written to the
DB before being appended to the in-memory ring.

main.go opens the store at cfg.DBPath and logs a warning + falls
back to in-memory-only if the file can't be created — a broken
data volume shouldn't stop the pool from running.

InsertBlock uses INSERT OR IGNORE on the height primary key so
replayed log events after a restart are harmless.
This commit is contained in:
satoshi
2026-04-14 11:06:38 +03:00
parent c92a991e89
commit 01829746ac
6 changed files with 219 additions and 1 deletions
+5 -1
View File
@@ -12,6 +12,7 @@ import (
"github.com/kamadopool/kamado-api/internal/bitcoind"
"github.com/kamadopool/kamado-api/internal/ckpool"
"github.com/kamadopool/kamado-api/internal/store"
)
// Snapshot is the merged view served to the UI. All fields are safe to
@@ -49,6 +50,7 @@ type Snapshot struct {
type Aggregator struct {
CK *ckpool.Client
RPC *bitcoind.RPC
Store *store.BlockStore // optional; nil disables persistence
Interval time.Duration
Log *slog.Logger
@@ -78,8 +80,10 @@ func New(ck *ckpool.Client, rpc *bitcoind.RPC, interval time.Duration, log *slog
// Run blocks until ctx is cancelled, refreshing the snapshot every Interval.
// It runs one immediate refresh at startup so readers don't see an empty
// snapshot after ctx launches the goroutine.
// snapshot after ctx launches the goroutine. Persisted block history is
// loaded from the store before the first refresh.
func (a *Aggregator) Run(ctx context.Context) {
a.loadPersistedBlocks()
a.refresh(ctx)
t := time.NewTicker(a.Interval)
defer t.Stop()
+41
View File
@@ -5,6 +5,7 @@ import (
"time"
"github.com/kamadopool/kamado-api/internal/logmon"
"github.com/kamadopool/kamado-api/internal/store"
)
// BlockRecord is a found block, merged from a logmon event with bitcoind
@@ -44,6 +45,17 @@ func (a *Aggregator) IngestBlockEvents(ctx context.Context, events <-chan logmon
}
cancel()
}
if a.Store != nil {
if err := a.Store.InsertBlock(store.Block{
Height: rec.Height,
Hash: rec.Hash,
RewardBT: rec.RewardBT,
FoundAt: rec.FoundAt,
Source: rec.Source,
}); err != nil {
a.Log.Warn("block persist failed", "height", rec.Height, "err", err)
}
}
pushed := a.appendBlock(rec)
a.Log.Info("block recorded", "height", rec.Height, "hash", rec.Hash)
// Push immediately so WebSocket clients see the solve
@@ -77,6 +89,35 @@ func (a *Aggregator) appendBlock(rec BlockRecord) Snapshot {
return snap
}
// loadPersistedBlocks seeds a.blocks from the store so history survives
// restarts. Safe to call with a nil Store — becomes a no-op.
func (a *Aggregator) loadPersistedBlocks() {
if a.Store == nil {
return
}
rows, err := a.Store.Recent(maxBlockHistory)
if err != nil {
a.Log.Warn("block history load failed", "err", err)
return
}
// Store returns newest-first; a.blocks is newest-last.
recs := make([]BlockRecord, 0, len(rows))
for i := len(rows) - 1; i >= 0; i-- {
r := rows[i]
recs = append(recs, BlockRecord{
Height: r.Height,
Hash: r.Hash,
RewardBT: r.RewardBT,
FoundAt: r.FoundAt,
Source: r.Source,
})
}
a.mu.Lock()
a.blocks = recs
a.mu.Unlock()
a.Log.Info("block history loaded", "count", len(recs))
}
// Blocks returns a copy of the recent block history, newest last.
func (a *Aggregator) Blocks() []BlockRecord {
a.mu.RLock()
+99
View File
@@ -0,0 +1,99 @@
// Package store persists Kamado runtime data that must survive process
// restarts. Currently: the found-block history. Uses modernc.org/sqlite
// (pure Go, no CGO) so the kamado-api binary stays statically linkable.
package store
import (
"database/sql"
"errors"
"fmt"
"time"
_ "modernc.org/sqlite"
)
// BlockStore persists found blocks to a single SQLite file.
type BlockStore struct {
db *sql.DB
}
// Block mirrors state.BlockRecord without the import cycle. The store
// package is the lower layer; the state package converts to/from this
// shape when reading and writing.
type Block struct {
Height int64
Hash string
RewardBT float64
FoundAt time.Time
Source string
}
const schema = `
CREATE TABLE IF NOT EXISTS blocks (
height INTEGER PRIMARY KEY,
hash TEXT NOT NULL DEFAULT '',
reward_btc REAL NOT NULL DEFAULT 0,
found_at INTEGER NOT NULL,
source TEXT NOT NULL DEFAULT ''
);
CREATE INDEX IF NOT EXISTS blocks_found_at_idx ON blocks(found_at);
`
// Open initializes the store at path, creating the schema if needed.
// Callers are responsible for Close().
func Open(path string) (*BlockStore, error) {
db, err := sql.Open("sqlite", path+"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)")
if err != nil {
return nil, fmt.Errorf("store: open %s: %w", path, err)
}
if _, err := db.Exec(schema); err != nil {
_ = db.Close()
return nil, fmt.Errorf("store: schema: %w", err)
}
return &BlockStore{db: db}, nil
}
func (s *BlockStore) Close() error {
return s.db.Close()
}
// InsertBlock is idempotent — duplicate heights are ignored so replayed
// log events after a restart don't trip the primary key constraint.
func (s *BlockStore) InsertBlock(b Block) error {
_, err := s.db.Exec(
`INSERT OR IGNORE INTO blocks(height, hash, reward_btc, found_at, source)
VALUES (?, ?, ?, ?, ?)`,
b.Height, b.Hash, b.RewardBT, b.FoundAt.Unix(), b.Source,
)
return err
}
// Recent returns up to limit blocks, newest first.
func (s *BlockStore) Recent(limit int) ([]Block, error) {
if limit <= 0 {
limit = 256
}
rows, err := s.db.Query(
`SELECT height, hash, reward_btc, found_at, source
FROM blocks ORDER BY height DESC LIMIT ?`,
limit,
)
if err != nil {
return nil, err
}
defer rows.Close()
out := make([]Block, 0, limit)
for rows.Next() {
var b Block
var unix int64
if err := rows.Scan(&b.Height, &b.Hash, &b.RewardBT, &unix, &b.Source); err != nil {
return nil, err
}
b.FoundAt = time.Unix(unix, 0).UTC()
out = append(out, b)
}
if err := rows.Err(); err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, err
}
return out, nil
}