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:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user