// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z1 // A real-time match: two programs play off-chain, anchor hashes during the // match, submit their logs afterwards, and the chain replays them. Covers an // honest match, a doctored log, an anchor that is too early, a late settle. package z1 import ( "strings" "testing" rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v4" "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/live/v1" ) var ( alice = address("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") bob = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") ) func as(cur realm, who address) { testing.SetRealm(testing.NewUserRealm(who)) } // play is what the two programs do off-chain: read the state, send inputs. func play(seed uint32, ca, cb, ticks int) (string, string) { g := rules.NewLength(seed, ca, cb, ticks) a, b := rules.NewBot(rules.Hard, seed+1), rules.NewBot(rules.Normal, seed+2) var la, lb []byte for i := 0; i < ticks && !g.Over(); i++ { m0, a0 := a.Act(g, 0) m1, a1 := b.Act(g, 1) la = append(la, rules.Encode(m0, a0)) lb = append(lb, rules.Encode(m1, a1)) g.Step(m0, a0, m1, a1) } return string(la), string(lb) } func chainOf(log string) []string { var out []string prev := "" for k := 0; k < len(log)/live.AnchorTicks; k++ { prev = live.AnchorHash(prev, log[k*live.AnchorTicks:(k+1)*live.AnchorTicks]) out = append(out, prev) } return out } func field(js, name string) string { i := strings.Index(js, `"`+name+`":`) rest := js[i+len(name)+3:] if strings.HasPrefix(rest, "[") { return rest[:strings.Index(rest, "]")+1] } return rest[:strings.IndexAny(rest, ",}")] } func main(cur realm) { as(cur, alice) id := live.Create(cross(cur), "Alice", rules.Reader, 60) as(cur, bob) live.Join(cross(cur), id, "Bob", rules.Hunter) st := live.StateJSON(id) seed := uint32(0) for _, c := range field(st, "seed") { seed = seed*10 + uint32(c-'0') } la, lb := play(seed, rules.Reader, rules.Hunter, 600) ca, cb := chainOf(la), chainOf(lb) // an anchor can not land before its time: the whole match can't be committed up front as(cur, alice) println("early anchor refused:", revive(func() { live.Anchor(cross(cur), id, ca[0]) }) != nil) // anchors land as the match goes on, 2 blocks apart for k := 0; k < len(ca); k++ { testing.SkipHeights(2) as(cur, alice) live.Anchor(cross(cur), id, ca[k]) as(cur, bob) live.Anchor(cross(cur), id, cb[k]) } println("anchors:", field(live.StateJSON(id), "players") != "") // a late anchor is refused too: a second match where bob waits too long as(cur, alice) idL := live.Create(cross(cur), "Alice", rules.Brawler, 60) as(cur, bob) live.Join(cross(cur), idL, "Bob", rules.Coward) testing.SkipHeights(2 + live.AnchorSlack + 1) println("late anchor refused:", revive(func() { live.Anchor(cross(cur), idL, ca[0]) }) != nil) as(cur, bob) // bob tries to rewrite one tick of his log after seeing alice's forged := []byte(lb) if forged[300] == 'A' { forged[300] = 'B' } else { forged[300] = 'A' } println("doctored log refused:", revive(func() { live.Submit(cross(cur), id, string(forged)) }) != nil) println("short log refused:", revive(func() { live.Submit(cross(cur), id, lb[:599]) }) != nil) // honest logs go through and the chain replays them live.Submit(cross(cur), id, lb) as(cur, alice) live.Submit(cross(cur), id, la) st = live.StateJSON(id) println("phase:", field(st, "phase"), "gold:", field(st, "gold")) g := rules.NewLength(seed, rules.Reader, rules.Hunter, 600) for i := 0; i < 600 && !g.Over(); i++ { m0, a0, _ := rules.Decode(la[i]) m1, a1, _ := rules.Decode(lb[i]) g.Step(m0, a0, m1, a1) } println("same as an independent replay:", g.F[0].Gold, g.F[1].Gold) println("logs kept:", live.Inputs(id, 0) == la, live.Inputs(id, 1) == lb) // a second match: bob never delivers, so he forfeits after the deadline as(cur, alice) id2 := live.Create(cross(cur), "Alice", rules.Brawler, 60) as(cur, bob) live.Join(cross(cur), id2, "Bob", rules.Coward) la2, _ := play(seed, rules.Brawler, rules.Coward, 600) c2 := chainOf(la2) for k := 0; k < len(c2); k++ { testing.SkipHeights(2) as(cur, alice) live.Anchor(cross(cur), id2, c2[k]) } as(cur, alice) live.Submit(cross(cur), id2, la2) println("settle too soon refused:", revive(func() { live.Settle(cross(cur), id2) }) != nil) testing.SkipHeights(200) live.Settle(cross(cur), id2) st = live.StateJSON(id2) println("forfeit:", field(st, "phase"), "winner:", field(st, "winner")) println(strings.Contains(live.Render(""), "Alice vs Bob")) } // Output: // early anchor refused: true // anchors: true // late anchor refused: true // doctored log refused: true // short log refused: true // phase: "done" gold: [35,14] // same as an independent replay: 35 14 // logs kept: true true // settle too soon refused: true // forfeit: "forfeit" winner: 0 // true