Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

gauntlet source pure

Package gauntlet is a library for coding bounties that are graded by code, on-chain, instead of by people.

Overview

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:

  1. Commit: the solver posts sha256(solutionPkgPath + ":" + salt). This fixes their place in the queue before anyone can see their code.
  2. Deploy: the solver deploys their solution realm.
  3. 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.

Constants 4

const Pending, Passed, Failed, Expired, Refunded

1const (
2	Pending  State = iota // committed, not yet revealed
3	Passed                // revealed and passed every test
4	Failed                // revealed and failed a test
5	Expired               // not revealed in time; bond forfeited
6	Refunded              // challenge ended before it was revealed; bond returned
7)
source

const Open, Won, Closed

1const (
2	Open   Status = iota // accepting commits until the deadline
3	Won                  // paid out to the earliest passing commit
4	Closed               // nobody passed; reward returned to the poster
5)
source

Variables 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)
source

Functions 6

func Escape

1func Escape(s string) string
source

Escape makes a value safe to place on one case line.

func HashSolution

1func HashSolution(pkgPath, salt string) string
source

HashSolution is the commitment a solver posts: sha256(pkgPath + ":" + salt) as lowercase hex.

func Unescape

1func Unescape(s string) string
source

Unescape reverses Escape.

func ParseCases

1func ParseCases(src string) ([]Case, error)
source

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

1func GradeCases(cases []Case, solve Solver) Report
source

GradeCases runs solve against fixed cases. It stops at the first failure: a solution either passes everything or it does not.

func GradeJudge

1func GradeJudge(j *Judge, seed uint64, solve Solver) Report
source

GradeJudge runs solve against inputs generated from seed and checked by the judge.

Types 11

type Attempt

struct
 1type Attempt struct {
 2	Seq         uint64 // global order of commits on the board
 3	Solver      address
 4	Hash        string
 5	CommittedAt time.Time
 6	State       State
 7	PkgPath     string // the solution realm, set on reveal
 8	RevealedAt  time.Time
 9	Report      Report
10}
source

Attempt is one solver's commit, and its result once revealed.

type Board

struct
1type Board struct {
2	challenges avl.Tree // key(id) -> *Challenge
3	lastID     uint64
4	seq        uint64
5}
source

Board is a set of challenges. The zero value is ready to use.

Methods on Board

func Boost

method on Board
1func (b *Board) Boost(id uint64, amount int64, now time.Time) error
source

Boost adds to the reward of an open challenge.

func Commit

method on Board
1func (b *Board) Commit(id uint64, solver address, hash string, bond int64, now time.Time) (*Attempt, error)
source

Commit records a solver's place in line.

func Get

method on Board
1func (b *Board) Get(id uint64) *Challenge
source

Get returns a challenge or nil.

func Iterate

method on Board
1func (b *Board) Iterate(cb func(c *Challenge) bool)
source

Iterate visits challenges newest first. Return true to stop.

func Post

method on Board
1func (b *Board) Post(p PostParams, now time.Time) (*Challenge, error)
source

Post adds a challenge. The caller must already hold p.Reward in escrow.

func Reveal

method on Board
1func (b *Board) Reveal(id uint64, pkgPath, salt string, solve Solver, now time.Time) (*Attempt, []Payout, error)
source

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 Board
1func (b *Board) Settle(id uint64, now time.Time) ([]Payout, error)
source

Settle 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 Board
1func (b *Board) Size() int
source

Size is the number of challenges ever posted.

type Case

struct
1type Case struct {
2	In   string
3	Want string
4}
source

Case 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}
source

Challenge is one bounty.

Methods on Challenge

func Pot

method on Challenge
1func (c *Challenge) Pot() int64
source

Pot is what the winner (or, if nobody wins, the poster) receives.

type Judge

struct
1type Judge struct {
2	Inputs func(seed uint64) []string
3	Check  func(in, out string) bool
4}
source

Judge 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

struct
1type Payout struct {
2	To     address
3	Amount int64
4	Reason string
5}
source

Payout is an instruction for the hosting realm to send coins.

type PostParams

struct
 1type PostParams struct {
 2	Title     string
 3	Spec      string
 4	Poster    address
 5	Reward    int64
 6	Bond      int64
 7	Deadline  time.Time
 8	Window    time.Duration
 9	Cases     []Case
10	Judge     *Judge
11	JudgePath string
12}
source

PostParams describes a new challenge.

type Report

struct
1type Report struct {
2	Passed int
3	Total  int
4	// Details of the first failing test, if any.
5	FailIn   string
6	FailGot  string
7	FailWant string
8}
source

Report is the result of grading one solution.

Methods on Report

func OK

method on Report
1func (r Report) OK() bool
source

OK reports whether every test passed.

type Solver

func
1type Solver func(in string) string
source

Solver is the function a solution exposes.

type State

ident
1type State int
source

State of an attempt.

Methods on State

func String

method on State
1func (s State) String() string
source

type Status

ident
1type Status int
source

Status of a challenge.

Methods on Status

func String

method on Status
1func (s Status) String() string
source

Dependencies

Source Files 6