z1_live_filetest.gno
4.81 Kb · 152 lines
1// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z1
2
3// A real-time match: two programs play off-chain, anchor hashes during the
4// match, submit their logs afterwards, and the chain replays them. Covers an
5// honest match, a doctored log, an anchor that is too early, a late settle.
6package z1
7
8import (
9 "strings"
10 "testing"
11
12 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v4"
13 "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/live/v1"
14)
15
16var (
17 alice = address("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm")
18 bob = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
19)
20
21func as(cur realm, who address) { testing.SetRealm(testing.NewUserRealm(who)) }
22
23// play is what the two programs do off-chain: read the state, send inputs.
24func play(seed uint32, ca, cb, ticks int) (string, string) {
25 g := rules.NewLength(seed, ca, cb, ticks)
26 a, b := rules.NewBot(rules.Hard, seed+1), rules.NewBot(rules.Normal, seed+2)
27 var la, lb []byte
28 for i := 0; i < ticks && !g.Over(); i++ {
29 m0, a0 := a.Act(g, 0)
30 m1, a1 := b.Act(g, 1)
31 la = append(la, rules.Encode(m0, a0))
32 lb = append(lb, rules.Encode(m1, a1))
33 g.Step(m0, a0, m1, a1)
34 }
35 return string(la), string(lb)
36}
37
38func chainOf(log string) []string {
39 var out []string
40 prev := ""
41 for k := 0; k < len(log)/live.AnchorTicks; k++ {
42 prev = live.AnchorHash(prev, log[k*live.AnchorTicks:(k+1)*live.AnchorTicks])
43 out = append(out, prev)
44 }
45 return out
46}
47
48func field(js, name string) string {
49 i := strings.Index(js, `"`+name+`":`)
50 rest := js[i+len(name)+3:]
51 if strings.HasPrefix(rest, "[") {
52 return rest[:strings.Index(rest, "]")+1]
53 }
54 return rest[:strings.IndexAny(rest, ",}")]
55}
56
57func main(cur realm) {
58 as(cur, alice)
59 id := live.Create(cross(cur), "Alice", rules.Reader, 60)
60 as(cur, bob)
61 live.Join(cross(cur), id, "Bob", rules.Hunter)
62 st := live.StateJSON(id)
63 seed := uint32(0)
64 for _, c := range field(st, "seed") {
65 seed = seed*10 + uint32(c-'0')
66 }
67 la, lb := play(seed, rules.Reader, rules.Hunter, 600)
68 ca, cb := chainOf(la), chainOf(lb)
69
70 // an anchor can not land before its time: the whole match can't be committed up front
71 as(cur, alice)
72 println("early anchor refused:", revive(func() { live.Anchor(cross(cur), id, ca[0]) }) != nil)
73
74 // anchors land as the match goes on, 2 blocks apart
75 for k := 0; k < len(ca); k++ {
76 testing.SkipHeights(2)
77 as(cur, alice)
78 live.Anchor(cross(cur), id, ca[k])
79 as(cur, bob)
80 live.Anchor(cross(cur), id, cb[k])
81 }
82 println("anchors:", field(live.StateJSON(id), "players") != "")
83
84 // a late anchor is refused too: a second match where bob waits too long
85 as(cur, alice)
86 idL := live.Create(cross(cur), "Alice", rules.Brawler, 60)
87 as(cur, bob)
88 live.Join(cross(cur), idL, "Bob", rules.Coward)
89 testing.SkipHeights(2 + live.AnchorSlack + 1)
90 println("late anchor refused:", revive(func() { live.Anchor(cross(cur), idL, ca[0]) }) != nil)
91 as(cur, bob)
92
93 // bob tries to rewrite one tick of his log after seeing alice's
94 forged := []byte(lb)
95 if forged[300] == 'A' {
96 forged[300] = 'B'
97 } else {
98 forged[300] = 'A'
99 }
100 println("doctored log refused:", revive(func() { live.Submit(cross(cur), id, string(forged)) }) != nil)
101 println("short log refused:", revive(func() { live.Submit(cross(cur), id, lb[:599]) }) != nil)
102
103 // honest logs go through and the chain replays them
104 live.Submit(cross(cur), id, lb)
105 as(cur, alice)
106 live.Submit(cross(cur), id, la)
107 st = live.StateJSON(id)
108 println("phase:", field(st, "phase"), "gold:", field(st, "gold"))
109
110 g := rules.NewLength(seed, rules.Reader, rules.Hunter, 600)
111 for i := 0; i < 600 && !g.Over(); i++ {
112 m0, a0, _ := rules.Decode(la[i])
113 m1, a1, _ := rules.Decode(lb[i])
114 g.Step(m0, a0, m1, a1)
115 }
116 println("same as an independent replay:", g.F[0].Gold, g.F[1].Gold)
117 println("logs kept:", live.Inputs(id, 0) == la, live.Inputs(id, 1) == lb)
118
119 // a second match: bob never delivers, so he forfeits after the deadline
120 as(cur, alice)
121 id2 := live.Create(cross(cur), "Alice", rules.Brawler, 60)
122 as(cur, bob)
123 live.Join(cross(cur), id2, "Bob", rules.Coward)
124 la2, _ := play(seed, rules.Brawler, rules.Coward, 600)
125 c2 := chainOf(la2)
126 for k := 0; k < len(c2); k++ {
127 testing.SkipHeights(2)
128 as(cur, alice)
129 live.Anchor(cross(cur), id2, c2[k])
130 }
131 as(cur, alice)
132 live.Submit(cross(cur), id2, la2)
133 println("settle too soon refused:", revive(func() { live.Settle(cross(cur), id2) }) != nil)
134 testing.SkipHeights(200)
135 live.Settle(cross(cur), id2)
136 st = live.StateJSON(id2)
137 println("forfeit:", field(st, "phase"), "winner:", field(st, "winner"))
138 println(strings.Contains(live.Render(""), "Alice vs Bob"))
139}
140
141// Output:
142// early anchor refused: true
143// anchors: true
144// late anchor refused: true
145// doctored log refused: true
146// short log refused: true
147// phase: "done" gold: [35,14]
148// same as an independent replay: 35 14
149// logs kept: true true
150// settle too soon refused: true
151// forfeit: "forfeit" winner: 0
152// true