z0_full_round_filetest.gno
2.95 Kb · 63 lines
1// PKGPATH: gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/main
2package main
3
4import (
5 "chain"
6 "chain/banker"
7 "testing"
8
9 "gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet"
10 "gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/rev1"
11)
12
13// A full round: a poster funds a bounty, a solver commits, deploys and
14// reveals, the realm grades the code on-chain and pays out.
15//
16// IssueCoins plus SetOriginSend stand in for what `gnokey maketx call -send`
17// does on chain: the coins land at the realm's address and the realm reads
18// the envelope with OriginSend.
19func main(cur realm) {
20 poster := address("g1q646ctzhvn60v392yy4ldq5zyr8q5hnxee3zqe")
21 solver := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
22 boardAddr := chain.PackageAddress("gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet")
23 bk := banker.NewReadonlyBanker()
24
25 // The poster funds a 2 GNOT bounty: 12 hours, 0.5 GNOT bond, 30 minute window.
26 fund(boardAddr, 2_000_000)
27 testing.SetRealm(testing.NewUserRealm(poster))
28 id := gauntlet.Post(cross(cur), "Reverse a string",
29 "Return the input reversed.", "abc => cba\nhello => olleh", 12, 500_000, 30)
30 println("challenge:", id)
31 println("board holds:", bk.GetCoin(boardAddr, "ugnot"))
32
33 // The solver commits the hash of their realm path and a secret salt.
34 hash := gauntlet.HashFor("gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/rev1", "my-secret")
35 fund(boardAddr, 500_000)
36 testing.SetRealm(testing.NewUserRealm(solver))
37 gauntlet.Commit(cross(cur), id, hash)
38 println("board holds after commit:", bk.GetCoin(boardAddr, "ugnot"))
39
40 // The solver's deployed realm reveals. Solve runs here, on-chain.
41 testing.SetOriginSend(chain.Coins{})
42 rev1.Submit(cross(cur), id, "my-secret")
43
44 println("board holds after payout:", bk.GetCoin(boardAddr, "ugnot"))
45 println("solver holds:", bk.GetCoin(solver, "ugnot"))
46 println(gauntlet.ChallengeJSON(id))
47}
48
49// fund delivers coins to the realm and sets the send envelope, the way
50// `gnokey maketx call -send` does.
51func fund(to address, amount int64) {
52 coins := chain.Coins{{Denom: "ugnot", Amount: amount}}
53 testing.IssueCoins(to, coins)
54 testing.SetOriginSend(coins)
55}
56
57// Output:
58// challenge: 1
59// board holds: 2000000
60// board holds after commit: 2500000
61// board holds after payout: 0
62// solver holds: 2500000
63// {"id":1,"title":"Reverse a string","spec":"Return the input reversed.","status":"won","poster":"g1q646ctzhvn60v392yy4ldq5zyr8q5hnxee3zqe","reward":2000000,"pot":2000000,"bond":500000,"boosts":0,"forfeited":0,"created":1234567890,"deadline":1234611090,"window":1800,"judge":"","cases":[{"in":"abc","want":"cba"},{"in":"hello","want":"olleh"}],"attempts":[{"seq":1,"solver":"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5","hash":"507b14b79bff05d8d070068c833d2fbc865a5accd85e4fc45f9cd16edc4802d1","committed":1234567890,"state":"passed","pkgpath":"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/solvers/rev1","passed":2,"total":2,"failIn":"","failGot":"","failWant":"","winner":true}]}