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_test.gno

10.17 Kb · 321 lines
  1package gauntlet
  2
  3import (
  4	"strings"
  5	"testing"
  6	"time"
  7)
  8
  9var (
 10	alice  = address("g1alice000000000000000000000000000000000")
 11	bob    = address("g1bob00000000000000000000000000000000000")
 12	carol  = address("g1carol00000000000000000000000000000000")
 13	poster = address("g1poster0000000000000000000000000000000")
 14	t0     = time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC)
 15)
 16
 17func reverse(s string) string {
 18	r := []rune(s)
 19	for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
 20		r[i], r[j] = r[j], r[i]
 21	}
 22	return string(r)
 23}
 24
 25func wrong(s string) string { return s }
 26
 27func hardcoded(s string) string {
 28	if s == "abc" {
 29		return "cba"
 30	}
 31	return s
 32}
 33
 34func newBoard(t *testing.T) (*Board, *Challenge) {
 35	b := &Board{}
 36	cases, err := ParseCases("abc => cba\nhello => olleh\n# comment\n\na\\nb => b\\na")
 37	if err != nil {
 38		t.Fatal(err)
 39	}
 40	c, err := b.Post(PostParams{
 41		Title: "Reverse", Poster: poster, Reward: 1000, Bond: 10,
 42		Deadline: t0.Add(24 * time.Hour), Window: time.Hour, Cases: cases,
 43	}, t0)
 44	if err != nil {
 45		t.Fatal(err)
 46	}
 47	return b, c
 48}
 49
 50func TestParseCases(t *testing.T) {
 51	cs, err := ParseCases("a\\tb => x\\\\y")
 52	if err != nil {
 53		t.Fatal(err)
 54	}
 55	if cs[0].In != "a\tb" || cs[0].Want != `x\y` {
 56		t.Fatalf("got %q %q", cs[0].In, cs[0].Want)
 57	}
 58	if _, err := ParseCases("no arrow"); err != ErrBadCaseLine {
 59		t.Fatalf("want ErrBadCaseLine, got %v", err)
 60	}
 61	if _, err := ParseCases("  \n# x"); err != ErrNoCases {
 62		t.Fatalf("want ErrNoCases, got %v", err)
 63	}
 64	if Unescape(Escape("a\n\tb\\")) != "a\n\tb\\" {
 65		t.Fatal("escape roundtrip")
 66	}
 67}
 68
 69func TestPostValidation(t *testing.T) {
 70	b := &Board{}
 71	base := PostParams{Title: "x", Poster: poster, Reward: 1, Deadline: t0.Add(time.Hour), Window: time.Hour, Cases: []Case{{"a", "b"}}}
 72	bad := []struct {
 73		mut  func(p *PostParams)
 74		want error
 75	}{
 76		{func(p *PostParams) { p.Title = "" }, ErrBadTitle},
 77		{func(p *PostParams) { p.Reward = 0 }, ErrBadReward},
 78		{func(p *PostParams) { p.Bond = -1 }, ErrBadBond},
 79		{func(p *PostParams) { p.Deadline = t0 }, ErrBadDeadline},
 80		{func(p *PostParams) { p.Window = time.Second }, ErrBadWindow},
 81		{func(p *PostParams) { p.Cases = nil }, ErrNoTests},
 82	}
 83	for i, tc := range bad {
 84		p := base
 85		tc.mut(&p)
 86		if _, err := b.Post(p, t0); err != tc.want {
 87			t.Fatalf("case %d: want %v, got %v", i, tc.want, err)
 88		}
 89	}
 90	if b.Size() != 0 {
 91		t.Fatal("nothing should be posted")
 92	}
 93}
 94
 95func TestHappyPath(t *testing.T) {
 96	b, c := newBoard(t)
 97	path := "gno.land/r/g1alice/rev"
 98	if _, err := b.Commit(c.ID, alice, HashSolution(path, "s1"), 10, t0.Add(time.Minute)); err != nil {
 99		t.Fatal(err)
100	}
101	a, pay, err := b.Reveal(c.ID, path, "s1", reverse, t0.Add(2*time.Minute))
102	if err != nil {
103		t.Fatal(err)
104	}
105	if a.State != Passed || a.Report.Passed != 3 {
106		t.Fatalf("state %v report %+v", a.State, a.Report)
107	}
108	if c.Status != Won || c.Winner != a {
109		t.Fatal("alice should have won")
110	}
111	if len(pay) != 2 || pay[0].Amount != 10 || pay[1].To != alice || pay[1].Amount != 1000 {
112		t.Fatalf("payouts %+v", pay)
113	}
114}
115
116func TestFailingSolutionKeepsChallengeOpen(t *testing.T) {
117	b, c := newBoard(t)
118	path := "gno.land/r/g1bob/rev"
119	b.Commit(c.ID, bob, HashSolution(path, "x"), 10, t0)
120	a, pay, err := b.Reveal(c.ID, path, "x", hardcoded, t0.Add(time.Minute))
121	if err != nil {
122		t.Fatal(err)
123	}
124	if a.State != Failed || a.Report.Passed != 1 || a.Report.FailIn != "hello" {
125		t.Fatalf("report %+v", a.Report)
126	}
127	if c.Status != Open || len(pay) != 1 {
128		t.Fatalf("status %v payouts %+v", c.Status, pay)
129	}
130	// Bob can commit again after a failure.
131	if _, err := b.Commit(c.ID, bob, HashSolution(path+"2", "y"), 10, t0.Add(2*time.Minute)); err != nil {
132		t.Fatal(err)
133	}
134}
135
136// The core fairness property: a copier who commits later never wins, even
137// if they reveal first.
138func TestEarlierCommitWinsOverFasterCopier(t *testing.T) {
139	b, c := newBoard(t)
140	honest := "gno.land/r/g1alice/rev"
141	copier := "gno.land/r/g1carol/rev"
142	b.Commit(c.ID, alice, HashSolution(honest, "a"), 10, t0)
143	b.Commit(c.ID, carol, HashSolution(copier, "c"), 10, t0.Add(time.Minute))
144
145	// Carol reveals first and passes, but Alice is earlier in line.
146	_, pay, err := b.Reveal(c.ID, copier, "c", reverse, t0.Add(2*time.Minute))
147	if err != nil {
148		t.Fatal(err)
149	}
150	if c.Status != Open || len(pay) != 1 {
151		t.Fatalf("must wait for alice: status %v payouts %+v", c.Status, pay)
152	}
153	_, pay, _ = b.Reveal(c.ID, honest, "a", reverse, t0.Add(3*time.Minute))
154	if c.Status != Won || c.Winner.Solver != alice {
155		t.Fatal("alice should win")
156	}
157	if pay[1].To != alice || pay[1].Amount != 1000 {
158		t.Fatalf("payouts %+v", pay)
159	}
160}
161
162func TestExpiredCommitForfeitsBondAndUnblocks(t *testing.T) {
163	b, c := newBoard(t)
164	slow := "gno.land/r/g1bob/rev"
165	fast := "gno.land/r/g1alice/rev"
166	b.Commit(c.ID, bob, HashSolution(slow, "b"), 10, t0)
167	b.Commit(c.ID, alice, HashSolution(fast, "a"), 10, t0.Add(time.Minute))
168	b.Reveal(c.ID, fast, "a", reverse, t0.Add(2*time.Minute))
169	if c.Status != Open {
170		t.Fatal("bob's commit is still in its window")
171	}
172	// After bob's window, anyone can settle.
173	pay, _ := b.Settle(c.ID, t0.Add(61*time.Minute))
174	if c.Status != Won || c.Winner.Solver != alice {
175		t.Fatal("alice should win after bob expires")
176	}
177	if c.Attempts[0].State != Expired || c.Forfeited != 10 || pay[0].Amount != 1010 {
178		t.Fatalf("forfeit not applied: %+v", pay)
179	}
180	// Bob is too late to reveal now.
181	if _, _, err := b.Reveal(c.ID, slow, "b", reverse, t0.Add(62*time.Minute)); err != ErrNoCommit {
182		t.Fatalf("want ErrNoCommit, got %v", err)
183	}
184}
185
186func TestLaterPendingCommitsAreRefundedWhenWon(t *testing.T) {
187	b, c := newBoard(t)
188	b.Commit(c.ID, alice, HashSolution("gno.land/r/a/x", "1"), 10, t0)
189	b.Commit(c.ID, bob, HashSolution("gno.land/r/b/x", "2"), 10, t0)
190	_, pay, _ := b.Reveal(c.ID, "gno.land/r/a/x", "1", reverse, t0)
191	if c.Attempts[1].State != Refunded {
192		t.Fatal("bob should be refunded")
193	}
194	last := pay[len(pay)-1]
195	if last.To != bob || last.Amount != 10 {
196		t.Fatalf("payouts %+v", pay)
197	}
198}
199
200func TestNoWinnerReturnsRewardToPoster(t *testing.T) {
201	b, c := newBoard(t)
202	b.Commit(c.ID, bob, HashSolution("gno.land/r/b/x", "1"), 10, t0)
203	b.Reveal(c.ID, "gno.land/r/b/x", "1", wrong, t0.Add(time.Minute))
204	if pay, _ := b.Settle(c.ID, t0.Add(time.Hour)); pay != nil {
205		t.Fatal("too early to close")
206	}
207	pay, _ := b.Settle(c.ID, t0.Add(25*time.Hour))
208	if c.Status != Closed || len(pay) != 1 || pay[0].To != poster || pay[0].Amount != 1000 {
209		t.Fatalf("status %v payouts %+v", c.Status, pay)
210	}
211	if _, err := b.Commit(c.ID, alice, HashSolution("gno.land/r/a/x", "1"), 10, t0.Add(26*time.Hour)); err != ErrNotOpen {
212		t.Fatalf("want ErrNotOpen, got %v", err)
213	}
214}
215
216func TestCommitRules(t *testing.T) {
217	b, c := newBoard(t)
218	h := HashSolution("gno.land/r/a/x", "1")
219	if _, err := b.Commit(c.ID, alice, h, 9, t0); err != ErrWrongBond {
220		t.Fatalf("want ErrWrongBond, got %v", err)
221	}
222	if _, err := b.Commit(c.ID, alice, "zz", 10, t0); err != ErrBadHash {
223		t.Fatalf("want ErrBadHash, got %v", err)
224	}
225	if _, err := b.Commit(c.ID, alice, strings.ToUpper(h), 10, t0); err != ErrBadHash {
226		t.Fatalf("uppercase hash should be rejected, got %v", err)
227	}
228	b.Commit(c.ID, alice, h, 10, t0)
229	if _, err := b.Commit(c.ID, bob, h, 10, t0); err != ErrDuplicateHash {
230		t.Fatalf("want ErrDuplicateHash, got %v", err)
231	}
232	if _, err := b.Commit(c.ID, alice, HashSolution("gno.land/r/a/y", "1"), 10, t0); err != ErrHasPending {
233		t.Fatalf("want ErrHasPending, got %v", err)
234	}
235	if _, err := b.Commit(c.ID, bob, HashSolution("gno.land/r/b/x", "1"), 10, t0.Add(24*time.Hour)); err != ErrPastDeadline {
236		t.Fatalf("want ErrPastDeadline, got %v", err)
237	}
238	if _, err := b.Commit(99, bob, h, 10, t0); err != ErrNotFound {
239		t.Fatalf("want ErrNotFound, got %v", err)
240	}
241}
242
243func TestRevealRules(t *testing.T) {
244	b, c := newBoard(t)
245	b.Commit(c.ID, alice, HashSolution("gno.land/r/a/x", "1"), 10, t0)
246	if _, _, err := b.Reveal(c.ID, "gno.land/e/a/run", "1", reverse, t0); err != ErrNotRealm {
247		t.Fatalf("want ErrNotRealm, got %v", err)
248	}
249	if _, _, err := b.Reveal(c.ID, "gno.land/r/a/x", "wrong-salt", reverse, t0); err != ErrNoCommit {
250		t.Fatalf("want ErrNoCommit, got %v", err)
251	}
252	// Someone else's realm cannot claim Alice's commit.
253	if _, _, err := b.Reveal(c.ID, "gno.land/r/c/x", "1", reverse, t0); err != ErrNoCommit {
254		t.Fatalf("want ErrNoCommit, got %v", err)
255	}
256	if _, _, err := b.Reveal(c.ID, "gno.land/r/a/x", "1", reverse, t0.Add(2*time.Hour)); err != ErrRevealTooLate {
257		t.Fatalf("want ErrRevealTooLate, got %v", err)
258	}
259}
260
261func TestJudgeDefeatsLookupTables(t *testing.T) {
262	b := &Board{}
263	judge := &Judge{
264		Inputs: func(seed uint64) []string {
265			out := []string{}
266			for i := 0; i < 5; i++ {
267				out = append(out, "x"+string(rune('a'+int((seed>>(i*8))%26))))
268			}
269			return out
270		},
271		Check: func(in, out string) bool { return out == reverse(in) },
272	}
273	c, err := b.Post(PostParams{
274		Title: "Judged reverse", Poster: poster, Reward: 50, Deadline: t0.Add(time.Hour),
275		Window: time.Hour, Judge: judge, Cases: []Case{{"ab", "ba"}},
276	}, t0)
277	if err != nil {
278		t.Fatal(err)
279	}
280	b.Commit(c.ID, bob, HashSolution("gno.land/r/b/x", "1"), 0, t0)
281	a, _, _ := b.Reveal(c.ID, "gno.land/r/b/x", "1", hardcoded, t0)
282	if a.State != Failed {
283		t.Fatal("lookup table must fail generated inputs")
284	}
285	b.Commit(c.ID, alice, HashSolution("gno.land/r/a/x", "1"), 0, t0)
286	a, pay, _ := b.Reveal(c.ID, "gno.land/r/a/x", "1", reverse, t0)
287	if a.State != Passed || a.Report.Total != 6 || c.Winner != a {
288		t.Fatalf("state %v report %+v", a.State, a.Report)
289	}
290	if len(pay) != 1 || pay[0].Amount != 50 {
291		t.Fatalf("no bond, only the prize: %+v", pay)
292	}
293}
294
295func TestBoost(t *testing.T) {
296	b, c := newBoard(t)
297	if err := b.Boost(c.ID, 500, t0); err != nil {
298		t.Fatal(err)
299	}
300	if c.Reward != 1500 || c.Boosts != 500 {
301		t.Fatal("boost not applied")
302	}
303	if err := b.Boost(c.ID, 0, t0); err != ErrBadBoostAmount {
304		t.Fatal("zero boost should fail")
305	}
306	if err := b.Boost(c.ID, 1, t0.Add(48*time.Hour)); err != ErrNotOpen {
307		t.Fatal("boost after deadline should fail")
308	}
309}
310
311func TestIterateNewestFirst(t *testing.T) {
312	b := &Board{}
313	for i := 0; i < 3; i++ {
314		b.Post(PostParams{Title: "c", Poster: poster, Reward: 1, Deadline: t0.Add(time.Hour), Window: time.Hour, Cases: []Case{{"a", "a"}}}, t0)
315	}
316	var ids []uint64
317	b.Iterate(func(c *Challenge) bool { ids = append(ids, c.ID); return false })
318	if len(ids) != 3 || ids[0] != 3 || ids[2] != 1 {
319		t.Fatalf("ids %v", ids)
320	}
321}