pow: a fast miner that hashes the bytes before the nonce once (about 100 times faster)
pow.Mine finds the smallest nonce, exactly what trying 0, 1, 2, ... finds, so every signed record and golden fixture stays the same. It saves the SHA-256 state after the whole blocks before the nonce, hashes only the number and what follows per attempt, and uses every core but one from difficulty 14 up. Listing, report, revocation and message mining use it; each re-checks the result before signing. One listing at difficulty 16 takes about 19 ms instead of about 2 s. docs/PROTOCOL.md says what proof-of-work can and cannot do against fast hardware. allmende-load fills a data directory with signed invented listings for capacity tests and measures mining time (-mine-only). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/pow"
|
||||
)
|
||||
|
||||
var testNow = time.Unix(1_800_000_000, 0)
|
||||
@@ -103,6 +105,32 @@ func TestRevocation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The fast miner must keep finding what the old loop found.
|
||||
func TestRevocationMiningFindsTheSmallestNonceThatTryingEachOneInOrderWouldFind(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, _ := Enroll(root, testNow, 24*time.Hour)
|
||||
const bits = 11
|
||||
r, err := Revoke(root, dev.Cert, testNow, bits)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
probe := *r
|
||||
probe.Sig = ""
|
||||
for n := uint64(0); ; n++ {
|
||||
probe.PoW = n
|
||||
b, err := probe.signingBytes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pow.LeadingZeroBits(b) >= bits {
|
||||
if n != r.PoW {
|
||||
t.Fatalf("Revoke chose nonce %d, the smallest that works is %d", r.PoW, n)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceSaveLoad(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, _ := Enroll(root, testNow, 24*time.Hour)
|
||||
|
||||
Reference in New Issue
Block a user