Add PoW challenge structure and types

This commit is contained in:
Savely Krendelhoff 2025-08-22 16:40:03 +07:00
parent 501e5d8bc3
commit ba0df856fe
No known key found for this signature in database
GPG key ID: F70DFD34F40238DE

53
internal/pow/types.go Normal file
View file

@ -0,0 +1,53 @@
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
}