Spend a hashcash stamp only when the request succeeds (closes #158)
check / check (push) Waiting to run
check / check (push) Waiting to run
A session request refused with 400 or 409, or a +H channel message refused for membership, a ban or +m, used to spend its stamp, so a retry with the same stamp was refused as already used. hashcash.Validator.Validate is split into Check, which spends nothing, and Claim, which records the stamp; Release undoes a claim. Session creation checks the stamp first, claims it just before CreateSession and releases it if CreateSession fails. SendChannelMessage checks the stamp after the membership, ban and +m checks. Model: opus-5-5
This commit is contained in:
@@ -77,9 +77,13 @@ func NewValidator(resource string) *Validator {
|
||||
return validator
|
||||
}
|
||||
|
||||
// Validate checks a hashcash stamp. It returns nil if the
|
||||
// stamp is valid and has not been seen before.
|
||||
func (v *Validator) Validate(
|
||||
// Check returns nil if requiredBits is not positive, or if
|
||||
// the stamp is a well-formed, recently dated proof of work
|
||||
// for this resource with at least requiredBits. It does not
|
||||
// look at or change the set of spent stamps, so a request
|
||||
// can check its stamp first and Claim it after its other
|
||||
// checks.
|
||||
func (v *Validator) Check(
|
||||
stamp string,
|
||||
requiredBits int,
|
||||
) error {
|
||||
@@ -87,9 +91,49 @@ func (v *Validator) Validate(
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := v.check(stamp, requiredBits)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Claim checks the stamp as Check does and, when
|
||||
// requiredBits is positive, records it as spent until it
|
||||
// expires. It returns an error if the stamp is already
|
||||
// spent. Release gives a claimed stamp back.
|
||||
func (v *Validator) Claim(
|
||||
stamp string,
|
||||
requiredBits int,
|
||||
) error {
|
||||
if requiredBits <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
stampTime, err := v.check(stamp, requiredBits)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return v.checkAndRecordStamp(stamp, stampTime)
|
||||
}
|
||||
|
||||
// Release forgets a claimed stamp so that it can be claimed
|
||||
// again, for a request that failed after its Claim.
|
||||
// Releasing a stamp that is not claimed does nothing.
|
||||
func (v *Validator) Release(stamp string) {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
|
||||
delete(v.spent, stamp)
|
||||
}
|
||||
|
||||
// check returns the stamp's date if Check accepts it.
|
||||
func (v *Validator) check(
|
||||
stamp string,
|
||||
requiredBits int,
|
||||
) (time.Time, error) {
|
||||
parts := strings.Split(stamp, ":")
|
||||
if len(parts) != stampFields {
|
||||
return fmt.Errorf(
|
||||
return time.Time{}, fmt.Errorf(
|
||||
"%w: expected %d, got %d",
|
||||
errInvalidFields, stampFields, len(parts),
|
||||
)
|
||||
@@ -104,25 +148,25 @@ func (v *Validator) Validate(
|
||||
version, bitsStr, resource, requiredBits,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
stampTime, err := parseStampDate(dateStr)
|
||||
if err != nil {
|
||||
return err
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
err = validateTime(stampTime)
|
||||
if err != nil {
|
||||
return err
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
err = validateProof(stamp, requiredBits)
|
||||
if err != nil {
|
||||
return err
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
return v.checkAndRecordStamp(stamp, stampTime)
|
||||
return stampTime, nil
|
||||
}
|
||||
|
||||
func (v *Validator) validateHeader(
|
||||
|
||||
@@ -85,7 +85,7 @@ func TestMintAndValidate(t *testing.T) {
|
||||
t, testBits, "test-resource", todayDate(),
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("valid stamp rejected: %v", err)
|
||||
}
|
||||
@@ -99,17 +99,68 @@ func TestReplayDetection(t *testing.T) {
|
||||
t, testBits, "test-resource", todayDate(),
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("first use failed: %v", err)
|
||||
}
|
||||
|
||||
err = validator.Validate(stamp, testBits)
|
||||
err = validator.Claim(stamp, testBits)
|
||||
if err == nil {
|
||||
t.Fatal("replay not detected")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckDoesNotSpendStamp checks that a stamp Check
|
||||
// accepts can still be claimed, and only once.
|
||||
func TestCheckDoesNotSpendStamp(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
validator := hashcash.NewValidator("test-resource")
|
||||
stamp := mintStampWithDate(
|
||||
t, testBits, "test-resource", todayDate(),
|
||||
)
|
||||
|
||||
for range 2 {
|
||||
err := validator.Check(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("valid stamp rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("claim after check failed: %v", err)
|
||||
}
|
||||
|
||||
err = validator.Claim(stamp, testBits)
|
||||
if err == nil {
|
||||
t.Fatal("replay not detected")
|
||||
}
|
||||
}
|
||||
|
||||
// TestReleasedStampCanBeClaimedAgain checks that Release
|
||||
// gives a claimed stamp back.
|
||||
func TestReleasedStampCanBeClaimedAgain(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
validator := hashcash.NewValidator("test-resource")
|
||||
stamp := mintStampWithDate(
|
||||
t, testBits, "test-resource", todayDate(),
|
||||
)
|
||||
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("first claim failed: %v", err)
|
||||
}
|
||||
|
||||
validator.Release(stamp)
|
||||
|
||||
err = validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("claim after release failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceMismatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -118,7 +169,7 @@ func TestResourceMismatch(t *testing.T) {
|
||||
t, testBits, "wrong-resource", todayDate(),
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err == nil {
|
||||
t.Fatal("expected resource mismatch error")
|
||||
}
|
||||
@@ -129,7 +180,7 @@ func TestInvalidStampFormat(t *testing.T) {
|
||||
|
||||
validator := hashcash.NewValidator("test-resource")
|
||||
|
||||
err := validator.Validate(
|
||||
err := validator.Claim(
|
||||
"not:a:valid:stamp", testBits,
|
||||
)
|
||||
if err == nil {
|
||||
@@ -146,7 +197,7 @@ func TestBadVersion(t *testing.T) {
|
||||
testBits, todayDate(), "test-resource",
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err == nil {
|
||||
t.Fatal("expected bad version error")
|
||||
}
|
||||
@@ -162,7 +213,7 @@ func TestInsufficientDifficulty(t *testing.T) {
|
||||
todayDate(), "test-resource",
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err == nil {
|
||||
t.Fatal("expected insufficient bits error")
|
||||
}
|
||||
@@ -178,7 +229,7 @@ func TestExpiredStamp(t *testing.T) {
|
||||
t, testBits, "test-resource", oldDate,
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err == nil {
|
||||
t.Fatal("expected expired stamp error")
|
||||
}
|
||||
@@ -189,9 +240,14 @@ func TestZeroBitsSkipsValidation(t *testing.T) {
|
||||
|
||||
validator := hashcash.NewValidator("test-resource")
|
||||
|
||||
err := validator.Validate("garbage", 0)
|
||||
err := validator.Check("garbage", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("zero bits should skip: %v", err)
|
||||
t.Fatalf("zero bits should skip the check: %v", err)
|
||||
}
|
||||
|
||||
err = validator.Claim("garbage", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("zero bits should skip the claim: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,7 +260,7 @@ func TestLongDateFormat(t *testing.T) {
|
||||
t, testBits, "test-resource", longDate,
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("long date stamp rejected: %v", err)
|
||||
}
|
||||
@@ -219,7 +275,7 @@ func TestBadDateFormat(t *testing.T) {
|
||||
testBits, "test-resource",
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err == nil {
|
||||
t.Fatal("expected bad date error")
|
||||
}
|
||||
@@ -235,7 +291,7 @@ func TestMultipleUniqueStamps(t *testing.T) {
|
||||
t, testBits, "test-resource", todayDate(),
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf("unique stamp rejected: %v", err)
|
||||
}
|
||||
@@ -251,7 +307,7 @@ func TestHigherBitsStillValid(t *testing.T) {
|
||||
t, 4, "test-resource", todayDate(),
|
||||
)
|
||||
|
||||
err := validator.Validate(stamp, testBits)
|
||||
err := validator.Claim(stamp, testBits)
|
||||
if err != nil {
|
||||
t.Fatalf(
|
||||
"higher-difficulty stamp rejected: %v",
|
||||
|
||||
Reference in New Issue
Block a user