Documentation
¶
Overview ¶
Package pow creates proof-of-work challenges and validates their solutions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidSolution = errors.New("invalid solution") ErrExpiredSeed = errors.New("expired seed") )
Errors which may be produced by a Manager.
Functions ¶
Types ¶
type Challenge ¶
Challenge is a set of fields presented to a client, with which they must generate a solution.
Generating a solution is done by:
Collect up to len(Seed) random bytes. These will be the potential solution.
Calculate the sha512 of the concatenation of Seed and PotentialSolution.
Parse the first 4 bytes of the sha512 result as a big-endian uint32.
If the resulting number is _less_ than target, the solution has been found. Otherwise go back to step 1 and try again.
type Manager ¶
type Manager interface { NewChallenge() Challenge // Will produce ErrInvalidSolution if the solution is invalid, or // ErrExpiredSeed if the seed has expired. CheckSolution(seed, solution []byte) error }
Manager is used to both produce proof-of-work challenges and check their solutions.
func NewManager ¶
func NewManager(store Store, secret []byte, opts *ManagerOpts) Manager
NewManager initializes and returns a Manager instance using the given parameters.
The secret is used to sign the seed values and should never be shared with clients.
type ManagerOpts ¶
type ManagerOpts struct { // Target indicates how difficult each Challenge will be to solve. A _lower_ // Target value is more difficult than a higher one. // // Defaults to 0x00FFFFFF Target uint32 // ChallengeTimeout indicates how long before Challenges are considered // expired and cannot be solved. // // Defaults to 12 hours. ChallengeTimeout time.Duration // Clock is used for controlling the view of time. // // Defaults to clock.Realtime(). Clock clock.Clock }
ManagerOpts are optional parameters to the NewManager function. A nil value is equivalent to a zero value.
type ManagerParams ¶
type ManagerParams struct { Store Store // Secret is used to sign each Challenge's Seed, it should _not_ be shared // with clients. Secret []byte }
ManagerParams are used to initialize a new Manager instance. All fields are required unless otherwise noted.
type MemoryStoreOpts ¶
type MemoryStoreOpts struct { // Clock is used for controlling the view of time. // // Defaults to clock.Realtime(). Clock clock.Clock }
MemoryStoreOpts are optional parameters to NewMemoryStore. A nil value is equivalent to a zero value.
type SolutionChecker ¶
type SolutionChecker struct {
// contains filtered or unexported fields
}
SolutionChecker can be used to check possible Challenge solutions. It will cache certain values internally to save on allocations when used in a loop (e.g. when generating a solution).
SolutionChecker is not thread-safe.
func (SolutionChecker) Check ¶
func (s SolutionChecker) Check(challenge Challenge, solution []byte) bool
Check returns true if the given bytes are a solution to the given Challenge.
type Store ¶
type Store interface { // SetSolution stores that the given solution is valid for the seed. The // seed/solution combination will be cleared from the Store once the expiry // is reached. SetSolution(seed, solution []byte, expiresAt time.Time) error // IsSolution returns true if SetSolution has been called with the given // seed, and the expiry from that call has not yet elapsed. IsSolution(seed, solution []byte) bool Close() error }
Store is used to track information related to proof-of-work challenges and solutions.
func NewMemoryStore ¶
func NewMemoryStore(opts *MemoryStoreOpts) Store
NewMemoryStore initializes and returns an in-memory Store implementation.