Add a page which proves the best share PoW

This commit is contained in:
satoshi committed 2026-09-01 23:23:51 +03:00
1 parent 8a35b02c7e
commit fc69410792
13 files changed
+2708 -10

No files matched your search

+106
View File
@@ -25,6 +25,15 @@ type HashratePoint struct {
V float64 `json:"v"` // H/s
}
// BestSharePow wraps the PoW reproduction record ckpool logs for a new
// best share (patch 0008) with the time we ingested it. Persisted as
// JSON in the kv store so the header of the best share survives
// restarts.
type BestSharePow struct {
logmon.PowData
SeenAt time.Time `json:"seen_at"`
}
// StratumServer describes one entry in ckpool's serverurl[] array. ckpool
// tags every client with the index of the bind it arrived on, but the index
// alone doesn't say what that bind *is* — that depends on how the deployment
@@ -64,6 +73,12 @@ type Snapshot struct {
BestShareHash string `json:"best_share_hash,omitempty"`
// Network difficulty at the time the best share was found.
BestShareNetDiff float64 `json:"best_share_net_diff,omitempty"`
// Full PoW reproduction data (raw block header, coinbase, merkle
// branches) for the best share seen since ckpool patch 0008 was
// deployed. May describe a lower-diff share than BestDiff when the
// all-time best predates the patch — the UI compares hashes and
// shows a disclaimer in that case.
BestSharePow *BestSharePow `json:"best_share_pow,omitempty"`
// Last best-diff value acknowledged by the user via the UI.
// The frontend shows a "new best" glow when best_diff > this.
AckedBestDiff float64 `json:"acked_best_diff"`
@@ -177,6 +192,7 @@ const (
kvBestDiff = "best_diff"
kvBestShareHash = "best_share_hash"
kvBestShareNetDiff = "best_share_net_diff"
kvBestSharePow = "best_share_pow"
kvAllTimeAccepted = "alltime_accepted"
kvAllTimeRejected = "alltime_rejected"
@@ -329,6 +345,19 @@ type Aggregator struct {
bestShareHash string
bestShareNetDiff float64 // network difficulty when best share was found
// PoW reproduction data for the best share seen since ckpool patch
// 0008 went live. Tracked as its own high-water mark (by SDiff)
// because the all-time best share may predate the patch and have no
// reconstructible header. Never mutated after assignment, so the
// pointer is safe to hand out in snapshots.
bestPow *BestSharePow
// PoW data lines arrive on the share channel just before their
// matching "Accepted client" line. Park them here keyed by header
// hash until the accepted event confirms the share, so data from a
// share that ends up rejected (e.g. below the client's vardiff
// right after a ckpool restart) is never persisted.
pendingPow map[string]*logmon.PowData
// Last best-diff value acknowledged by the user in the UI.
ackedBestDiff float64
@@ -352,6 +381,7 @@ func New(ck *ckpool.Client, rpc *bitcoind.RPC, interval time.Duration, log *slog
Log: log,
sessionRejectReasons: make(map[string]int64),
alltimeRejectReasons: make(map[string]int64),
pendingPow: make(map[string]*logmon.PowData),
ready: make(chan struct{}),
notifierDone: make(chan struct{}, 1),
}
@@ -572,6 +602,7 @@ func (a *Aggregator) refresh(ctx context.Context) {
next.BestDiff = a.bestDiff
next.BestShareHash = a.bestShareHash
next.BestShareNetDiff = a.bestShareNetDiff
next.BestSharePow = a.bestPow
next.AckedBestDiff = a.ackedBestDiff
now := time.Now()
@@ -821,6 +852,17 @@ func (a *Aggregator) IngestShareEvents(ctx context.Context, events <-chan logmon
return
}
a.mu.Lock()
if ev.Pow != nil {
// PoW reproduction line (patch 0008). Park it until the
// matching "Accepted client" event arrives; a bounded
// reset guards against unmatched entries piling up.
if len(a.pendingPow) >= 16 {
a.pendingPow = make(map[string]*logmon.PowData)
}
a.pendingPow[ev.Pow.Hash] = ev.Pow
a.mu.Unlock()
continue
}
if ev.Rejected {
reason := ev.Reason
if reason == "" {
@@ -836,6 +878,25 @@ func (a *Aggregator) IngestShareEvents(ctx context.Context, events <-chan logmon
a.alltimeDiffCount++
a.sessionDiffSum += ev.Diff
a.alltimeDiffSum += ev.Diff
// Promote parked PoW data once its share is confirmed
// accepted and beats the best PoW record we hold. This
// high-water mark is separate from bestDiff: the
// all-time best share may predate ckpool patch 0008,
// in which case this tracks the best share since the
// patch went live and the UI discloses the difference.
if pow, ok := a.pendingPow[ev.Hash]; ok {
delete(a.pendingPow, ev.Hash)
if a.bestPow == nil || pow.SDiff > a.bestPow.SDiff {
a.bestPow = &BestSharePow{PowData: *pow, SeenAt: time.Now()}
if a.Store != nil {
if data, err := json.Marshal(a.bestPow); err == nil {
_ = a.Store.SetKV(kvBestSharePow, string(data))
}
}
a.Log.Info("best share PoW data captured",
"sdiff", pow.SDiff, "height", pow.Height, "hash", pow.Hash)
}
}
// Track hash of the best share from log parsing.
if ev.Hash != "" && ev.Diff > a.bestDiff {
a.bestDiff = ev.Diff
@@ -971,6 +1032,16 @@ func (a *Aggregator) loadPersistedState() {
a.mu.Unlock()
}
}
if v, err := a.Store.GetKV(kvBestSharePow); err == nil && v != "" {
var pow BestSharePow
if uerr := json.Unmarshal([]byte(v), &pow); uerr == nil && pow.Header != "" {
a.mu.Lock()
a.bestPow = &pow
a.mu.Unlock()
} else if uerr != nil {
a.Log.Warn("best_share_pow load failed", "err", uerr)
}
}
// One-time backfill: if we have no hash (pre-upgrade), scan the
// ckpool log for the highest-difficulty accepted share and use its hash.
@@ -998,6 +1069,41 @@ func (a *Aggregator) loadPersistedState() {
}
}
// One-time backfill: if we hold no PoW record but the log already
// contains confirmed "Best share PoW data" lines (fresh database, or
// events dropped under load), adopt the best of them. Without this
// the PoW inspector would wait for a share beating ckpool's
// in-process best, which after a long ckpool uptime can take
// arbitrarily long.
a.mu.RLock()
needsPowBackfill := a.bestPow == nil && a.LogFilePath != ""
a.mu.RUnlock()
if needsPowBackfill {
pow, at, seen, unparseable, err := logmon.FindBestPowData(a.LogFilePath)
switch {
case err != nil:
a.Log.Warn("best share PoW backfill: log scan failed", "err", err)
case pow == nil:
// Deliberately loud: "no record" is indistinguishable from a
// broken pipeline without the counts.
a.Log.Info("best share PoW backfill: no confirmed record adopted",
"pow_lines_seen", seen, "unparseable", unparseable)
default:
if at.IsZero() {
at = time.Now()
}
best := &BestSharePow{PowData: *pow, SeenAt: at}
a.mu.Lock()
a.bestPow = best
a.mu.Unlock()
if data, jerr := json.Marshal(best); jerr == nil {
_ = a.Store.SetKV(kvBestSharePow, string(data))
}
a.Log.Info("backfilled best share PoW data from log",
"sdiff", pow.SDiff, "hash", pow.Hash, "pow_lines_seen", seen)
}
}
// Restore acknowledged best diff.
if v, err := a.Store.GetKV(kvAckedBestDiff); err == nil && v != "" {
if f, perr := strconv.ParseFloat(v, 64); perr == nil {