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.

z1_judged_filetest.gno

3.62 Kb · 71 lines
 1// PKGPATH: gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/main
 2package main
 3
 4import (
 5	"chain"
 6	"testing"
 7
 8	"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet"
 9	_ "gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/judges/sortnums"
10	"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/cheat"
11	"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/sort1"
12)
13
14// A judged challenge: the inputs are generated when the solution is revealed,
15// so a lookup table cannot pass, while a real sort can.
16func main(cur realm) {
17	poster := address("g1q646ctzhvn60v392yy4ldq5zyr8q5hnxee3zqe")
18	cheater := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
19	honest := address("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0")
20	boardAddr := chain.PackageAddress("gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet")
21
22	println(gauntlet.JudgesJSON())
23
24	fund(boardAddr, 1_000_000)
25	testing.SetRealm(testing.NewUserRealm(poster))
26	id := gauntlet.PostJudged(cross(cur), "Sort the numbers",
27		"Sort a comma-separated list of integers ascending.",
28		"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/judges/sortnums", "3,1,2 => 1,2,3", 6, 0, 30)
29
30	// The cheater commits first, so they would win if their code passed.
31	testing.SetOriginSend(chain.Coins{})
32	testing.SetRealm(testing.NewUserRealm(cheater))
33	gauntlet.Commit(cross(cur), id, gauntlet.HashFor("gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/cheat", "s1"))
34	testing.SetRealm(testing.NewUserRealm(honest))
35	gauntlet.Commit(cross(cur), id, gauntlet.HashFor("gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/sort1", "s2"))
36
37	cheat.Submit(cross(cur), id, "s1")
38	println("after the cheat reveals, status:", status(id))
39
40	sort1.Submit(cross(cur), id, "s2")
41	println("after the honest solution reveals, status:", status(id))
42	println(gauntlet.ChallengeJSON(id))
43}
44
45func status(id uint64) string {
46	j := gauntlet.ChallengeJSON(id)
47	i := indexOf(j, `"status":"`)
48	rest := j[i+10:]
49	return rest[:indexOf(rest, `"`)]
50}
51
52func indexOf(s, sub string) int {
53	for i := 0; i+len(sub) <= len(s); i++ {
54		if s[i:i+len(sub)] == sub {
55			return i
56		}
57	}
58	return -1
59}
60
61func fund(to address, amount int64) {
62	coins := chain.Coins{{Denom: "ugnot", Amount: amount}}
63	testing.IssueCoins(to, coins)
64	testing.SetOriginSend(coins)
65}
66
67// Output:
68// [{"path":"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/judges/sortnums","name":"Sort numbers","about":"Sort comma-separated integers ascending. 8 random lists of 1-40 numbers.","uses":0}]
69// after the cheat reveals, status: open
70// after the honest solution reveals, status: won
71// {"id":1,"title":"Sort the numbers","spec":"Sort a comma-separated list of integers ascending.","status":"won","poster":"g1q646ctzhvn60v392yy4ldq5zyr8q5hnxee3zqe","reward":1000000,"pot":1000000,"bond":0,"boosts":0,"forfeited":0,"created":1234567890,"deadline":1234589490,"window":1800,"judge":"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/judges/sortnums","cases":[{"in":"3,1,2","want":"1,2,3"}],"attempts":[{"seq":1,"solver":"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5","hash":"14a14753d77e6078e2b710c61f8ac68ccb3ef12b868f87dac8acc9840ab493a1","committed":1234567890,"state":"failed","pkgpath":"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/cheat","passed":0,"total":8,"failIn":"-229,-517,520,-505","failGot":"0","failWant":"","winner":false},{"seq":2,"solver":"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0","hash":"d043c782ab6328d7842e56e3c51d35d8950f9dd155f7efa4df3d735afc1c91a7","committed":1234567890,"state":"passed","pkgpath":"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/sort1","passed":9,"total":9,"failIn":"","failGot":"","failWant":"","winner":true}]}