Stop a proof-of-work test failing once every few thousand runs
"an unsolved nonce is refused" issued a fresh challenge, then asserted that the literal nonce "definitely-not-it" does not satisfy 12 bits of work. A challenge carries 128 random bits, so that literal is not an unsolved nonce; it is a nonce with a 1-in-4096 chance of being the answer. Measured over 200,000 sampled challenges: 1 in 4,300. It came due today, on a documentation-only merge request, where a red `test` job is impossible to read as anything but a real break. It also sat in front of this branch on a single shared runner. The test now picks a nonce that demonstrably does not solve the challenge it was actually given, so it tests what its name says without depending on luck. It rides along with the change beside it because it can fail that change's own pipeline, and the runner these queue on takes them one at a time. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
eaf5e12227
commit
4488e67b83
@@ -26,11 +26,22 @@ struct SignupProofOfWorkTests {
|
||||
}
|
||||
|
||||
@Test("an unsolved nonce is refused")
|
||||
func unsolvedRefused() {
|
||||
func unsolvedRefused() throws {
|
||||
let issued = SignupChallenge.issue(secret: secret, now: now, bits: 12)
|
||||
// A challenge carries 128 random bits, so a nonce fixed in the source
|
||||
// is not an unsolved nonce, it is a nonce with a 1-in-4096 chance of
|
||||
// being the solution. That is a test which fails on its own every few
|
||||
// thousand runs, and it did. Pick one that demonstrably does not solve
|
||||
// THIS challenge instead.
|
||||
let unsolved = try #require(
|
||||
(0..<64).map(String.init).first { candidate in
|
||||
SignupChallenge.leadingZeroBits(
|
||||
of: SHA256.hash(
|
||||
data: Data("\(issued.challenge).\(candidate)".utf8))) < 12
|
||||
})
|
||||
#expect(throws: SignupChallenge.Refusal.insufficient) {
|
||||
try SignupChallenge.verify(
|
||||
challenge: issued.challenge, nonce: "definitely-not-it",
|
||||
challenge: issued.challenge, nonce: unsolved,
|
||||
secret: self.secret, now: self.now, bits: 12)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user