gauntlet source pure
Package gauntlet is a library for coding bounties that are graded by code, on-chain, instead of by people.
Package gauntlet is a library for coding bounties that are graded by code, on-chain, instead of by people.
A Board holds challenges. A challenge is either a set of fixed test cases (input -> exact expected output) or a Judge: two functions written by the poster, one that generates fresh inputs from a seed and one that checks an answer. Judged challenges cannot be gamed with a lookup table, because the inputs are only known at the moment of grading.
Solvers compete with a commit-reveal scheme:
- Commit: the solver posts sha256(solutionPkgPath + ":" + salt). This fixes their place in the queue before anyone can see their code.
- Deploy: the solver deploys their solution realm.
- Reveal: the solution realm calls the board with the salt and its Solve function. The board recomputes the hash from the caller's package path, grades the function, and records pass or fail.
The winner is the earliest commit that passes. A later commit can never overtake an earlier one, so copying someone's deployed code does not pay. A commit that is not revealed within the reveal window expires, and its bond is added to the reward.
This package moves no coins. It returns Payouts that the hosting realm sends with its banker, so any realm can import it and run bounties in any token.
4
const MaxCases, MaxFieldLen
const Pending, Passed, Failed, Expired, Refunded
const Open, Won, Closed
2
var ErrNotFound, ErrNotOpen, ErrPastDeadline, ErrBadTitle, ErrBadSpec, ErrBadReward, ErrBadBond, ErrBadDeadline, ErrBadWindow, ErrNoTests, ErrBadHash, ErrWrongBond, ErrHasPending, ErrDuplicateHash, ErrFull, ErrNoCommit, ErrRevealTooLate, ErrNotRealm, ErrBadBoostAmount
1var (
2 ErrNotFound = errors.New("gauntlet: challenge not found")
3 ErrNotOpen = errors.New("gauntlet: challenge is not open")
4 ErrPastDeadline = errors.New("gauntlet: commits are closed for this challenge")
5 ErrBadTitle = errors.New("gauntlet: title must be 1-120 characters")
6 ErrBadSpec = errors.New("gauntlet: spec is too long")
7 ErrBadReward = errors.New("gauntlet: reward must be positive")
8 ErrBadBond = errors.New("gauntlet: bond cannot be negative")
9 ErrBadDeadline = errors.New("gauntlet: deadline must be in the future and within 90 days")
10 ErrBadWindow = errors.New("gauntlet: reveal window must be between 1 minute and 7 days")
11 ErrNoTests = errors.New("gauntlet: a challenge needs test cases or a judge")
12 ErrBadHash = errors.New("gauntlet: hash must be 64 hex characters")
13 ErrWrongBond = errors.New("gauntlet: wrong bond amount")
14 ErrHasPending = errors.New("gauntlet: you already have a pending commit on this challenge")
15 ErrDuplicateHash = errors.New("gauntlet: this hash was already committed")
16 ErrFull = errors.New("gauntlet: too many attempts on this challenge")
17 ErrNoCommit = errors.New("gauntlet: no pending commit matches this package path and salt")
18 ErrRevealTooLate = errors.New("gauntlet: reveal window has passed for this commit")
19 ErrNotRealm = errors.New("gauntlet: solutions must be revealed from a deployed /r/ realm")
20 ErrBadBoostAmount = errors.New("gauntlet: boost must be positive")
21)var ErrNoCases, ErrTooManyCase, ErrBadCaseLine, ErrFieldTooBig
6
func Escape
Escape makes a value safe to place on one case line.
func HashSolution
HashSolution is the commitment a solver posts: sha256(pkgPath + ":" + salt) as lowercase hex.
func Unescape
Unescape reverses Escape.
func ParseCases
ParseCases reads one case per line in the form `input => want`. Inside a field, `\n` is a newline, `\t` a tab and `\\` a backslash. Blank lines and lines starting with `#` are ignored.
func GradeCases
GradeCases runs solve against fixed cases. It stops at the first failure: a solution either passes everything or it does not.
func GradeJudge
GradeJudge runs solve against inputs generated from seed and checked by the judge.
11
type Attempt
structAttempt is one solver's commit, and its result once revealed.
type Board
structBoard is a set of challenges. The zero value is ready to use.
Methods on Board
func Boost
method on BoardBoost adds to the reward of an open challenge.
func Commit
method on Board1func (b *Board) Commit(id uint64, solver address, hash string, bond int64, now time.Time) (*Attempt, error)Commit records a solver's place in line.
func Get
method on BoardGet returns a challenge or nil.
func Iterate
method on BoardIterate visits challenges newest first. Return true to stop.
func Post
method on BoardPost adds a challenge. The caller must already hold p.Reward in escrow.
func Reveal
method on Board1func (b *Board) Reveal(id uint64, pkgPath, salt string, solve Solver, now time.Time) (*Attempt, []Payout, error)Reveal grades a solution. pkgPath must be the package path of the realm that is calling, as reported by the runtime, never a value a user typed.
func Settle
method on BoardSettle moves a challenge forward: it expires late commits and, when the result is certain, pays the winner or returns the reward to the poster. Anyone may call it; it is also run after every reveal.
func Size
method on BoardSize is the number of challenges ever posted.
type Case
structCase is one fixed test: Solve(In) must return exactly Want.
type Challenge
struct 1type Challenge struct {
2 ID uint64
3 Title string
4 Spec string // markdown
5 Poster address
6 Reward int64 // escrowed by the hosting realm
7 Bond int64 // required with every commit, refunded on reveal
8 Created time.Time
9 Deadline time.Time // last moment to commit
10 Window time.Duration // time allowed between commit and reveal
11
12 Cases []Case // fixed tests, may be empty if Judge is set
13 Judge *Judge
14 JudgePath string // package path of the judge's code, for display
15
16 Status Status
17 Winner *Attempt
18 Attempts []*Attempt
19 Forfeited int64 // expired bonds, added to the reward
20 Boosts int64 // reward added after posting
21}Challenge is one bounty.
type Judge
structJudge lets a poster define correctness with code instead of fixed answers. Inputs returns fresh inputs for a seed; Check reports whether out is a correct answer for in. Checking is usually far cheaper than solving.
type Payout
structPayout is an instruction for the hosting realm to send coins.
type PostParams
structPostParams describes a new challenge.
type Report
structReport is the result of grading one solution.
type Solver
funcSolver is the function a solution exposes.
type State
identState of an attempt.
type Status
identStatus of a challenge.