Add a page which proves the best share PoW
This commit is contained in:
13 files changed
+2708
-10
No files matched your search
@@ -5,6 +5,8 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -118,6 +120,158 @@ func TestHandleLine_Unrelated(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleLine_PowData(t *testing.T) {
|
||||
tl := New("/unused", discardLog())
|
||||
line := `[2026-08-31 10:11:12.123] Best share PoW data {"sdiff":123456789.5,"netdiff":136597951737045.0,"height":911111,` +
|
||||
`"hash":"0000000000abcdef0000000000000000000000000000000000000000deadbeef",` +
|
||||
`"header":"20000000" + "aa",` + "\n"
|
||||
// Malformed JSON must not emit an event.
|
||||
tl.handleLine(line)
|
||||
select {
|
||||
case ev := <-tl.Shares:
|
||||
t.Errorf("unexpected ShareEvent from malformed PoW line: %+v", ev)
|
||||
default:
|
||||
}
|
||||
|
||||
good := `[2026-08-31 10:11:12.123] Best share PoW data {"sdiff":123456789.5,"netdiff":136597951737045.0,"height":911111,` +
|
||||
`"hash":"0000000000abcdef0000000000000000000000000000000000000000deadbeef",` +
|
||||
`"header":"0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00000000000000000000000000000000000000000000000000000000000000006859a06817e0377c00000000",` +
|
||||
`"coinbase":"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff00ffffffff0100f2052a01000000160014000000000000000000000000000000000000000000000000",` +
|
||||
`"cb1len":41,"merklebranches":["ab"],"enonce1":"a1b2c3d4","nonce2":"0011223344556677","workername":"bc1qexample.axe"}` + "\n"
|
||||
tl.handleLine(good)
|
||||
select {
|
||||
case ev := <-tl.Shares:
|
||||
if ev.Pow == nil {
|
||||
t.Fatalf("ShareEvent.Pow = nil, want parsed PoW data (event %+v)", ev)
|
||||
}
|
||||
if ev.Pow.SDiff != 123456789.5 {
|
||||
t.Errorf("SDiff = %v, want 123456789.5", ev.Pow.SDiff)
|
||||
}
|
||||
if ev.Pow.Height != 911111 {
|
||||
t.Errorf("Height = %d, want 911111", ev.Pow.Height)
|
||||
}
|
||||
if len(ev.Pow.Header) != 160 {
|
||||
t.Errorf("Header length = %d, want 160", len(ev.Pow.Header))
|
||||
}
|
||||
if ev.Pow.CB1Len != 41 {
|
||||
t.Errorf("CB1Len = %d, want 41", ev.Pow.CB1Len)
|
||||
}
|
||||
if len(ev.Pow.MerkleBranches) != 1 || ev.Pow.MerkleBranches[0] != "ab" {
|
||||
t.Errorf("MerkleBranches = %v, want [ab]", ev.Pow.MerkleBranches)
|
||||
}
|
||||
if ev.Pow.Workername != "bc1qexample.axe" {
|
||||
t.Errorf("Workername = %q", ev.Pow.Workername)
|
||||
}
|
||||
default:
|
||||
t.Fatal("no ShareEvent emitted for PoW data line")
|
||||
}
|
||||
}
|
||||
|
||||
func powLine(hash string, sdiff float64) string {
|
||||
return `[2026-08-30 09:15:42.123] Best share PoW data {"sdiff":` +
|
||||
strconv.FormatFloat(sdiff, 'f', -1, 64) +
|
||||
`,"netdiff":1.3e14,"height":911111,"hash":"` + hash +
|
||||
`","header":"00000020aabb","coinbase":"0100","cb1len":41,` +
|
||||
`"merklebranches":[],"enonce1":"a1b2c3d4","nonce2":"0011223344556677","workername":"bc1q.axe"}` + "\n"
|
||||
}
|
||||
|
||||
func acceptedLine(hash string, sdiff float64) string {
|
||||
return `[2026-08-30 09:15:42.124] Accepted client 7 share diff ` +
|
||||
strconv.FormatFloat(sdiff, 'f', -1, 64) + `/42/1.0K: ` + hash + "\n"
|
||||
}
|
||||
|
||||
// FindBestPowData must return the highest-diff record that was
|
||||
// confirmed accepted, skip unconfirmed ones, and recover the log
|
||||
// line's timestamp.
|
||||
func TestFindBestPowData(t *testing.T) {
|
||||
path := tempLog(t)
|
||||
appendLines(t, path,
|
||||
powLine("aaaa", 100),
|
||||
acceptedLine("aaaa", 100),
|
||||
// Never confirmed (e.g. rejected below vardiff): must be ignored
|
||||
// even though it carries the highest diff.
|
||||
powLine("bbbb", 99999),
|
||||
powLine("cccc", 500),
|
||||
acceptedLine("cccc", 500),
|
||||
)
|
||||
|
||||
pow, at, seen, unparseable, err := FindBestPowData(path)
|
||||
if err != nil {
|
||||
t.Fatalf("scan error: %v", err)
|
||||
}
|
||||
if pow == nil {
|
||||
t.Fatal("FindBestPowData returned nil, want the confirmed 500-diff record")
|
||||
}
|
||||
if pow.Hash != "cccc" || pow.SDiff != 500 {
|
||||
t.Errorf("got hash %s sdiff %v, want cccc/500", pow.Hash, pow.SDiff)
|
||||
}
|
||||
if seen != 3 || unparseable != 0 {
|
||||
t.Errorf("seen=%d unparseable=%d, want 3/0", seen, unparseable)
|
||||
}
|
||||
want := time.Date(2026, 8, 30, 9, 15, 42, 0, time.Local)
|
||||
if !at.Equal(want) {
|
||||
t.Errorf("timestamp = %v, want %v", at, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Production ckpool logs have carried bytes after the JSON's closing
|
||||
// brace (observed in the field: an end-of-line-anchored regex matched 0
|
||||
// of 54 real PoW lines while synthetic lines passed). The parser must
|
||||
// decode the JSON value and ignore ANY trailing bytes — NULs, \r,
|
||||
// or an interleaved second message on the same line.
|
||||
func TestHandleLine_PowDataTrailingGarbage(t *testing.T) {
|
||||
suffixes := []string{
|
||||
"\x00\x00\n", // NUL padding
|
||||
"\r\n", // CR before LF
|
||||
" [2026-08-31 18:00:01.125] Accepted client 3 share diff 1.0/1/1: aa\n", // interleaved write
|
||||
}
|
||||
for _, suffix := range suffixes {
|
||||
tl := New("/unused", discardLog())
|
||||
tl.handleLine(strings.TrimRight(powLine("cafe", 777), "\n") + suffix)
|
||||
select {
|
||||
case ev := <-tl.Shares:
|
||||
if ev.Pow == nil || ev.Pow.Hash != "cafe" || ev.Pow.SDiff != 777 {
|
||||
t.Errorf("suffix %q: got %+v, want cafe/777 PoW event", suffix, ev.Pow)
|
||||
}
|
||||
default:
|
||||
t.Errorf("suffix %q: no PoW event emitted", suffix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A pathologically long line earlier in the log must not abort the scan
|
||||
// before it reaches the PoW records — bufio.Scanner did exactly that,
|
||||
// silently, which left the backfill permanently empty on real logs.
|
||||
func TestFindBestPowData_SurvivesOversizedLine(t *testing.T) {
|
||||
path := tempLog(t)
|
||||
appendLines(t, path,
|
||||
"junk "+strings.Repeat("x", 600*1024)+"\n", // > any fixed buffer
|
||||
powLine("abab", 250),
|
||||
acceptedLine("abab", 250),
|
||||
)
|
||||
pow, _, seen, _, err := FindBestPowData(path)
|
||||
if err != nil {
|
||||
t.Fatalf("scan error: %v", err)
|
||||
}
|
||||
if pow == nil || pow.Hash != "abab" || seen != 1 {
|
||||
t.Fatalf("pow=%+v seen=%d, want abab record found past the oversized line", pow, seen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindBestPowData_Empty(t *testing.T) {
|
||||
path := tempLog(t)
|
||||
appendLines(t, path,
|
||||
"Stratifier started\n",
|
||||
acceptedLine("dddd", 10), // accepted share without PoW data (pre-patch line)
|
||||
)
|
||||
if pow, _, _, _, err := FindBestPowData(path); pow != nil || err != nil {
|
||||
t.Errorf("got %+v err=%v, want nil/nil for log without confirmed PoW records", pow, err)
|
||||
}
|
||||
if pow, _, _, _, err := FindBestPowData(path + ".missing"); pow != nil || err == nil {
|
||||
t.Errorf("got %+v err=%v, want nil record and an error for missing file", pow, err)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Tailer.Run integration tests (filesystem + goroutines) ----
|
||||
|
||||
func tempLog(t *testing.T) string {
|
||||
|
||||
Reference in new issue
Block a user