hash-of-wisdom/internal/pow/types.go

54 lines
1.9 KiB
Go

package pow
import (
"errors"
"time"
)
// PoW package specific errors
var (
ErrInvalidHMAC = errors.New("challenge HMAC signature is invalid")
ErrExpiredChallenge = errors.New("challenge has expired")
ErrInvalidSolution = errors.New("proof of work solution is invalid")
ErrInvalidDifficulty = errors.New("difficulty level is invalid")
ErrMalformedChallenge = errors.New("challenge format is malformed")
)
// Challenge represents a Proof of Work challenge with HMAC authentication
type Challenge struct {
Timestamp int64 `json:"timestamp"`
Difficulty int `json:"difficulty"`
Resource string `json:"resource"`
Random string `json:"random"`
HMAC string `json:"hmac"`
}
// Solution represents a client's solution to a PoW challenge
type Solution struct {
Challenge Challenge `json:"challenge"`
Nonce string `json:"nonce"`
}
// ChallengeRequest represents a request for a new challenge
type ChallengeRequest struct{}
// SolutionRequest represents a client's submission of a solved challenge
type SolutionRequest struct {
Challenge Challenge `json:"challenge"`
Nonce string `json:"nonce"`
}
// Config holds configuration for the PoW system
type Config struct {
DefaultDifficulty int // Default difficulty in bits (e.g., 4)
MaxDifficulty int // Maximum allowed difficulty (e.g., 10)
MinDifficulty int // Minimum allowed difficulty (e.g., 3)
ChallengeTTL time.Duration // Time-to-live for challenges (e.g., 5 minutes)
HMACSecret []byte // Secret key for HMAC signing
Resource string // Resource identifier (e.g., "quotes")
LoadAdjustmentBits int // Extra difficulty bits when server under load
FailurePenaltyBits int // Extra difficulty bits per failure group
MaxFailurePenaltyBits int // Maximum failure penalty bits
}