P1 audits / fixes:
* Bitcoin Core RPC now retries up to 3 times with linear backoff on
transport errors, 5xx responses, and warm-up/loading RPC errors
(code -28). Hard "no" answers (block-not-found etc.) bubble up
immediately so we don't mask real errors.
* WebSocket hub disconnects clients that miss 6 consecutive broadcasts
(~30s with the default poll cadence). Stuck readers no longer hold
stale snapshots indefinitely or freeze hub state.
* ZMQ subscriber freshness: aggregator records the last-event
timestamp, surfaces zmq_enabled / has_last_zmq_event /
last_zmq_event_age in the snapshot. /healthz flags zmq_stale when
the gap exceeds 30 minutes.
* /healthz expanded with submit_attempts / submits_confirmed /
submit_gap, fallback_submits_total + last_fallback_*, and the zmq
staleness check. Now usable as a real-world ops dashboard signal.
Block-broadcast fallback (new feature):
* ckpool patch 0004: hooks local_block_submit to write the raw block
hex to <logdir>/pending-blocks/<height>-<hash16>.hex right before
invoking generator_submitblock. Unlinks on success. ckpool's normal
flow is otherwise untouched.
* api/internal/blocksubmit: watcher polls the dir every 5s. Files
sitting longer than the grace window (default 30s, configurable)
are re-broadcast through operator-supplied backup RPC URLs in
sequence. Treats both null and any "duplicate*" reject reason as
success (the block landed). Pre-checks the primary chain first so
a stale file from a successful-but-unlinked submit gets cleaned
up without bothering fallbacks.
* Aggregator records each successful fallback submission as a
persistent counter and surfaces it in the snapshot so the UI can
show a "primary bitcoind isn't accepting submits" alert.
* Config: BACKUP_RPC_URLS (comma- or newline-separated, with
optional inline credentials) plus PENDING_BLOCKS_DIR and
PENDING_BLOCKS_GRACE. URLs are parsed via net/url so
https://user:pass@host:port/ works cleanly.
The fallback is opt-in and disabled by default. Once enabled with at
least one URL, a primary bitcoind outage at the moment of solving no
longer means a lost block — kamado-api re-broadcasts via whichever
backup the operator trusts (a second self-hosted node, an
authenticated public RPC service, etc.).
201 lines
6.1 KiB
Go
201 lines
6.1 KiB
Go
// kamado-api is the middleware that sits between ckpool-solo and the
|
|
// Kamado dashboard. It polls ckpool's Unix socket API, calls bitcoind
|
|
// over JSON-RPC, and serves the merged state over REST (WebSocket push,
|
|
// SQLite persistence, ZMQ and log-tailer-based block detection land in
|
|
// a follow-up commit).
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/signal"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/kamadopool/kamado-api/internal/bitcoind"
|
|
"github.com/kamadopool/kamado-api/internal/blocksubmit"
|
|
"github.com/kamadopool/kamado-api/internal/ckpool"
|
|
"github.com/kamadopool/kamado-api/internal/config"
|
|
"github.com/kamadopool/kamado-api/internal/httpapi"
|
|
"github.com/kamadopool/kamado-api/internal/logmon"
|
|
"github.com/kamadopool/kamado-api/internal/state"
|
|
"github.com/kamadopool/kamado-api/internal/store"
|
|
"github.com/kamadopool/kamado-api/internal/zmqmon"
|
|
)
|
|
|
|
func main() {
|
|
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
|
slog.SetDefault(log)
|
|
|
|
cfg, err := config.FromEnv()
|
|
if err != nil {
|
|
log.Error("config", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
log.Info("kamado-api starting",
|
|
"listen", cfg.ListenAddr,
|
|
"sockdir", cfg.CKPoolSockDir,
|
|
"bitcoind", cfg.BitcoinRPCURL,
|
|
"poll_interval", cfg.PollInterval,
|
|
)
|
|
|
|
ck := ckpool.New(cfg.CKPoolSockDir)
|
|
rpc := bitcoind.NewRPC(cfg.BitcoinRPCURL, cfg.BitcoinRPCUser, cfg.BitcoinRPCPassword, cfg.BitcoinRPCTimeout)
|
|
|
|
// Persistent block history. Non-fatal if it can't be opened — the
|
|
// aggregator falls back to an in-memory ring so the pool keeps
|
|
// running even with a broken data volume.
|
|
var blockStore *store.BlockStore
|
|
if cfg.DBPath != "" {
|
|
if s, err := store.Open(cfg.DBPath); err != nil {
|
|
log.Warn("block store open failed, running without persistence", "path", cfg.DBPath, "err", err)
|
|
} else {
|
|
blockStore = s
|
|
defer blockStore.Close()
|
|
log.Info("block store opened", "path", cfg.DBPath)
|
|
}
|
|
}
|
|
|
|
agg := state.New(ck, rpc, cfg.PollInterval, log)
|
|
agg.Store = blockStore
|
|
agg.MempoolBaseURL = cfg.MempoolBaseURL
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
api := httpapi.New(agg, log)
|
|
|
|
// Wire snapshot refreshes into the WebSocket hub so subscribers get
|
|
// real-time updates without polling.
|
|
agg.OnRefresh = api.Hub.Broadcast
|
|
|
|
// Optional: bitcoind hashblock ZMQ subscription for sub-second
|
|
// chain-tip refreshes. Empty endpoint disables it.
|
|
zmq := zmqmon.New(cfg.BitcoinZMQBlock, log)
|
|
go zmq.Run(ctx)
|
|
|
|
go agg.Run(ctx, zmq.Events)
|
|
|
|
// Background reconciliation: retry hash/reward enrichment for blocks
|
|
// the initial RPC lookup couldn't fetch, and detect chain reorgs by
|
|
// comparing recorded hashes against the canonical chain.
|
|
go agg.ReconcileBlocks(ctx)
|
|
|
|
// Tail the ckpool log for block-solve events (our own solves).
|
|
tailer := logmon.New(cfg.CKPoolLogFile, log)
|
|
if blockStore != nil {
|
|
const cursorKey = "logmon_cursor"
|
|
tailer.LoadCursor = func() (uint64, int64, bool) {
|
|
v, err := blockStore.GetKV(cursorKey)
|
|
if err != nil || v == "" {
|
|
return 0, 0, false
|
|
}
|
|
parts := strings.SplitN(v, ":", 2)
|
|
if len(parts) != 2 {
|
|
return 0, 0, false
|
|
}
|
|
ino, err1 := strconv.ParseUint(parts[0], 10, 64)
|
|
off, err2 := strconv.ParseInt(parts[1], 10, 64)
|
|
if err1 != nil || err2 != nil {
|
|
return 0, 0, false
|
|
}
|
|
return ino, off, true
|
|
}
|
|
tailer.SaveCursor = func(ino uint64, off int64) {
|
|
if err := blockStore.SetKV(cursorKey, fmt.Sprintf("%d:%d", ino, off)); err != nil {
|
|
log.Warn("logmon cursor persist failed", "err", err)
|
|
}
|
|
}
|
|
}
|
|
go tailer.Run(ctx)
|
|
go agg.IngestBlockEvents(ctx, tailer.Events)
|
|
go agg.IngestAttemptEvents(ctx, tailer.Attempts)
|
|
|
|
// Fallback block submitter: if ckpool's primary bitcoind doesn't
|
|
// accept a block, the patched local_block_submit leaves the raw
|
|
// hex sitting in PENDING_BLOCKS_DIR. This watcher re-broadcasts it
|
|
// via the operator-configured BACKUP_RPC_URLS list.
|
|
if cfg.PendingBlocksDir != "" {
|
|
fallbacks := parseBackupRPCs(cfg.BackupRPCURLs, log)
|
|
sub := &blocksubmit.Submitter{
|
|
Dir: cfg.PendingBlocksDir,
|
|
Grace: cfg.PendingBlocksGrace,
|
|
Primary: rpc,
|
|
Fallbacks: fallbacks,
|
|
Log: log,
|
|
OnSuccess: func(height int64, viaURL, viaLabel string) {
|
|
agg.RecordFallbackSubmit(height, viaLabel)
|
|
},
|
|
}
|
|
go sub.Run(ctx, 5*time.Second)
|
|
} else {
|
|
log.Info("blocksubmit fallback disabled (PENDING_BLOCKS_DIR not set)")
|
|
}
|
|
|
|
srv := &http.Server{
|
|
Addr: cfg.ListenAddr,
|
|
Handler: api.Handler(),
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
}
|
|
|
|
// Shutdown on ctx cancel
|
|
go func() {
|
|
<-ctx.Done()
|
|
log.Info("shutting down")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
_ = srv.Shutdown(shutdownCtx)
|
|
}()
|
|
|
|
log.Info("http listening", "addr", cfg.ListenAddr)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Error("http server", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// parseBackupRPCs splits BACKUP_RPC_URLS (newline- or comma-separated)
|
|
// into FallbackTargets. Credentials are accepted inline as
|
|
// https://user:pass@host:port/. Lines beginning with '#' are comments.
|
|
// Whitespace and empty lines are ignored.
|
|
func parseBackupRPCs(raw string, log *slog.Logger) []blocksubmit.FallbackTarget {
|
|
if raw == "" {
|
|
return nil
|
|
}
|
|
separated := strings.NewReplacer(",", "\n").Replace(raw)
|
|
var out []blocksubmit.FallbackTarget
|
|
for _, line := range strings.Split(separated, "\n") {
|
|
line = strings.TrimSpace(line)
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
u, err := url.Parse(line)
|
|
if err != nil {
|
|
log.Warn("blocksubmit: invalid fallback URL, skipping", "raw", line, "err", err)
|
|
continue
|
|
}
|
|
var user, pass string
|
|
if u.User != nil {
|
|
user = u.User.Username()
|
|
pass, _ = u.User.Password()
|
|
u.User = nil
|
|
}
|
|
clean := u.String()
|
|
out = append(out, blocksubmit.FallbackTarget{
|
|
URL: clean,
|
|
User: user,
|
|
Password: pass,
|
|
Label: u.Host,
|
|
})
|
|
log.Info("blocksubmit fallback registered", "url", clean, "host", u.Host, "auth", user != "")
|
|
}
|
|
return out
|
|
}
|