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