{"jsonrpc":"2.0","id":"","result":{"genesis":{"genesis_time":"2026-07-16T15:48:57.341496514Z","chain_id":"dev","consensus_params":{"Block":{"MaxTxBytes":"1000000","MaxDataBytes":"2000000","MaxBlockBytes":"0","MaxGas":"3000000000","TimeIotaMS":"100"},"Validator":{"PubKeyTypeURLs":["/tm.PubKeyEd25519","/tm.PubKeySecp256k1"]}},"validators":[{"address":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"power":"10","name":"testvalidator"}],"app_hash":null,"app_state":{"@type":"/gno.GenesisState","auth":{"params":{"max_memo_bytes":"65536","tx_sig_limit":"7","tx_size_cost_per_byte":"10","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000","gas_price_change_compressor":"10","target_gas_ratio":"70","initial_gasprice":{"gas":"1000","price":"1ugnot"},"unrestricted_addrs":null,"fee_collector":"g17xpfvakm2amg962yls6f84z3kell8c5lr9lr2e"}},"bank":{"params":{"restricted_denoms":[]}},"vm":{"params":{"sysnames_pkgpath":"gno.land/r/sys/names","syscla_pkgpath":"gno.land/r/sys/cla","chain_domain":"gno.land","default_deposit":"600000000ugnot","storage_price":"100ugnot","storage_fee_collector":"g1c9stkafpvcwez2efq3qtfuezw4zpaux3tvxggk","min_get_read_depth_100":"300","min_set_read_depth_100":"200","min_write_depth_100":"440","fixed_get_read_depth_100":"300","fixed_set_read_depth_100":"200","fixed_write_depth_100":"440","iter_next_cost_flat":"1000"},"realm_params":null},"balances":["g1qpymzwx4l4cy6cerdyajp9ksvjsf20rk5y9rtt=10000000000ugnot","g1q6jrp203fq0239pv38sdq3y3urvd6vt5azacpv=1000000000000ugnot","g1pfldkplz9puq0v82lu9vqcve9nwrxuq9qe5ttv=10000000000ugnot","g1z629z04f85k4t5gnkk5egpxw9tqxeec435esap=10000000000ugnot","g1rrf8s5mrmu00sx04fzfsvc399fklpeg2x0a7mz=10000000000ugnot","g1yqndt8xx92l9h494jfruz2w79swzjes3n4wqjc=10000000000ugnot","g19p5ntfvpt4lwq4jqsmnxsnelhf3tff9scy3w8w=10000000000ugnot","g19t3n89slfemgd3mwuat4lajwcp0yxrkadgeg7a=10000000000ugnot","g19w2488ntfgpduzqq3sk4j5x387zynwknqdvjqf=10000000000ugnot","g19wwhkmqlns70604ksp6rkuuu42qhtvyh05lffz=10000000000ugnot","g19uxluuecjlsqvwmwu8sp6pxaaqfhk972q975xd=10000000000ugnot","g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u=665700000ugnot","g1xhccdjcscuhgmt3quww6qdy3j3czqt3urc2eac=10000000000ugnot","g18pmaskasz7mxj6rmgrl3al58xu45a7w0l5nmc0=10000000000ugnot","g187982000zsc493znqt828s90cmp6hcp2erhu6m=10000000000ugnot","g18l9us6trqaljw39j94wzf5ftxmd9qqkvrxghd2=1000000000000ugnot","g1f977l6wxdh3qu60kzl75vx2wmzswu68l03r8su=10000000000ugnot","g1fj9jccm3zjnqspq7lp2g7lj4czyfq0s35600g9=10000000000ugnot","g1f4v282mwyhu29afke4vq5r2xzcm6z3ftnugcnv=1000000000000ugnot","g127jydsh6cms3lrtdenydxsckh23a8d6emqcvfa=1000000000000ugnot","g1trkzq75ntamsnw9xnrav2v7gy2lt5g6p29yhdr=10000000000ugnot","g1tjdpptuk9eysq6z38nscqyycr998xjyx3w8jvw=10000000000ugnot","g1tue8l73d6rq4vhqdsp2sr3zhuzpure3k2rnwpz=10000000000ugnot","g1ds24jj9kqjcskd0gzu24r9e4n62ggye230zuv5=10000000000ugnot","g1wg88rhzlwxjd2z4j5de5v5xq30dcf6rjq3dhsj=10000000000ugnot","g1wwppuzdns5u6c6jqpkzua24zh6ppsus6399cea=10000000000ugnot","g1026p54q0j902059sm2zsv37krf0ghcl7gmhyv7=10000000000ugnot","g13278z0a5ufeg80ffqxpda9dlp599t7ekregcy6=10000000000ugnot","g13d7jc32adhc39erm5me38w5v7ej7lpvlnqjk73=1000000000000ugnot","g13sm84nuqed3fuank8huh7x9mupgw22uft3lcl8=10000000000ugnot","g13m7f2e6r3lh3ykxupacdt9sem2tlvmaamwjhll=10000000000ugnot","g1j80fpcsumfkxypvydvtwtz3j4sdwr8c2u0lr64=10000000000ugnot","g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5=10000000000000ugnot","g1ndpsnrspdnauckytvkfv8s823t3gmpqmtky8pl=10000000000ugnot","g1n4yvwnv77frq2ccuw27dmtjkd7u4p4jg0pgm7k=10000000000ugnot","g1589c8cekvmjfmy0qrd4f3z52r7fn7rgk02667s=10000000000ugnot","g15gdm49ktawvkrl88jadqpucng37yxutucuwaef=10000000000ugnot","g15fa8kyjhu88t9dr8zzua8fwdvkngv5n8yqsm0n=10000000000ugnot","g152pn0g5qfgxr7yx8zlwjq48hytkafd8x7egsfv=10000000000ugnot","g14qekdkj2nmmwea4ufg9n002a3pud23y8k7ugs5=10000000000ugnot","g1495y3z7zrej4rendysnw5kaeu4g3d7x7w0734g=10000000000ugnot","g14da4n9hcynyzz83q607uu8keuh9hwlv42ra6fa=10000000000ugnot","g14hhsss4ngx5kq77je5g0tl4vftg8qp45ceadk3=10000000000ugnot","g1k8pjnguyu36pkc8hy0ufzgpzfmj2jl78la7ek3=10000000000ugnot","g1hygx8ga9qakhkczyrzs9drm8j8tu4qds9y5e3r=10000000000ugnot","g1ht236wjd83x96uqwh9rh3fq6pylyn78mtwq9v6=10000000000ugnot","g1cf2ye686ke38vjyqakreprljum4xu6rwf5jskq=10000000000ugnot","g1c5shztyaj4gjrc5zlwmh9xhex5w7l4asffs2w6=10000000000ugnot","g1e8umkzumtxgs8399lw0us4rclea3xl5gxy9spp=10000000000ugnot","g16ja66d65emkr0zxd2tu7xjvm7utthyhpej0037=10000000000ugnot","g1644qje5rx6jsdqfkzmgnfcegx4dxkjh6rwqd69=10000000000ugnot","g1mzjajymvmtksdwh3wkrndwj6zls2awl9q83dh6=10000000000ugnot","g1m6732pkrngu9vrt0g7056lvr9kcqc4mv83xl5q=10000000000ugnot","g1manfred47kzduec920z88wfr64ylksmdcedlf5=10000000000ugnot","g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj=10000000000000ugnot","g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq=10000000000ugnot","g1768hvkh7anhd40ch4h7jdh6j3mpcs7hrat4gl0=10000000000ugnot","g1lhpx2ktk0ha3qw42raxq4m24a4c4xqxyrgv54q=10000000000ugnot"],"txs":[{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ufmt","path":"gno.land/p/nt/ufmt/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# ufmt\n\nPackage `ufmt` provides utility functions for formatting strings, similarly to the Go package `fmt`, of which only a subset is currently supported.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package ufmt provides utility functions for formatting strings, similarly to\n// the Go package \"fmt\", of which only a subset is currently supported.\npackage ufmt\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/ufmt/v0\"\ngno = \"0.9\"\n"},{"name":"ufmt.gno","body":"// Package ufmt provides utility functions for formatting strings, similarly to\n// the Go package \"fmt\", of which only a subset is currently supported (hence\n// the name µfmt - micro fmt). It includes functions like Printf, Sprintf,\n// Fprintf, and Errorf.\n// Supported formatting verbs are documented in the Sprintf function.\npackage ufmt\n\nimport (\n\t\"errors\"\n\t\"io\"\n\t\"strconv\"\n\t\"strings\"\n\t\"unicode/utf8\"\n)\n\n// buffer accumulates formatted output as a byte slice.\ntype buffer []byte\n\nfunc (b *buffer) write(p []byte) {\n\t*b = append(*b, p...)\n}\n\nfunc (b *buffer) writeString(s string) {\n\t*b = append(*b, s...)\n}\n\nfunc (b *buffer) writeByte(c byte) {\n\t*b = append(*b, c)\n}\n\nfunc (b *buffer) writeRune(r rune) {\n\t*b = utf8.AppendRune(*b, r)\n}\n\n// printer holds state for formatting operations.\ntype printer struct {\n\tbuf buffer\n}\n\nfunc newPrinter() *printer {\n\treturn \u0026printer{}\n}\n\n// Sprint formats using the default formats for its operands and returns the resulting string.\n// Sprint writes the given arguments with spaces between arguments.\nfunc Sprint(a ...any) string {\n\tp := newPrinter()\n\tp.doPrint(a)\n\treturn string(p.buf)\n}\n\n// doPrint formats arguments using default formats and writes to printer's buffer.\n// Spaces are added between arguments.\nfunc (p *printer) doPrint(args []any) {\n\tfor argNum, arg := range args {\n\t\tif argNum \u003e 0 {\n\t\t\tp.buf.writeRune(' ')\n\t\t}\n\n\t\tswitch v := arg.(type) {\n\t\tcase string:\n\t\t\tp.buf.writeString(v)\n\t\tcase (interface{ String() string }):\n\t\t\tp.buf.writeString(v.String())\n\t\tcase error:\n\t\t\tp.buf.writeString(v.Error())\n\t\tcase float64:\n\t\t\tp.buf.writeString(Sprintf(\"%f\", v))\n\t\tcase int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:\n\t\t\tp.buf.writeString(Sprintf(\"%d\", v))\n\t\tcase bool:\n\t\t\tif v {\n\t\t\t\tp.buf.writeString(\"true\")\n\t\t\t} else {\n\t\t\t\tp.buf.writeString(\"false\")\n\t\t\t}\n\t\tcase nil:\n\t\t\tp.buf.writeString(\"\u003cnil\u003e\")\n\t\tdefault:\n\t\t\tp.buf.writeString(\"(unhandled)\")\n\t\t}\n\t}\n}\n\n// doPrintln appends a newline after formatting arguments with doPrint.\nfunc (p *printer) doPrintln(a []any) {\n\tp.doPrint(a)\n\tp.buf.writeByte('\\n')\n}\n\n// Sprintf offers similar functionality to Go's fmt.Sprintf, or the sprintf\n// equivalent available in many languages, including C/C++.\n// The number of args passed must exactly match the arguments consumed by the format.\n// A limited number of formatting verbs and features are currently supported.\n//\n// Supported verbs:\n//\n//\t%s: Places a string value directly.\n//\t    If the value implements the interface interface{ String() string },\n//\t    the String() method is called to retrieve the value. Same about Error()\n//\t    string.\n//\t%c: Formats the character represented by Unicode code point\n//\t%d: Formats an integer value using package \"strconv\".\n//\t    Currently supports only uint, uint64, int, int64.\n//\t%f: Formats a float value, with a default precision of 6.\n//\t%e: Formats a float with scientific notation; 1.23456e+78\n//\t%E: Formats a float with scientific notation; 1.23456E+78\n//\t%F: The same as %f\n//\t%g: Formats a float value with %e for large exponents, and %f with full precision for smaller numbers\n//\t%G: Formats a float value with %G for large exponents, and %F with full precision for smaller numbers\n//\t%t: Formats a boolean value to \"true\" or \"false\".\n//\t%x: Formats an integer value as a hexadecimal string.\n//\t    Currently supports only uint8, []uint8, [32]uint8.\n//\t%c: Formats a rune value as a string.\n//\t    Currently supports only rune, int.\n//\t%q: Formats a string value as a quoted string.\n//\t%T: Formats the type of the value.\n//\t%v: Formats the value with a default representation appropriate for the value's type\n//\t    - nil: \u003cnil\u003e\n//\t    - bool: true/false\n//\t    - integers: base 10\n//\t    - float64: %g format\n//\t    - string: verbatim\n//\t    - types with String()/Error(): method result\n//\t    - others: (unhandled)\n//\t%%: Outputs a literal %. Does not consume an argument.\n//\n// Unsupported verbs or type mismatches produce error strings like \"%!d(string=foo)\".\nfunc Sprintf(format string, a ...any) string {\n\tp := newPrinter()\n\tp.doPrintf(format, a)\n\treturn string(p.buf)\n}\n\n// doPrintf parses the format string and writes formatted arguments to the buffer.\nfunc (p *printer) doPrintf(format string, args []any) {\n\tsTor := []rune(format)\n\tend := len(sTor)\n\targNum := 0\n\targLen := len(args)\n\n\tfor i := 0; i \u003c end; {\n\t\tisLast := i == end-1\n\t\tc := sTor[i]\n\n\t\tif isLast || c != '%' {\n\t\t\t// we don't check for invalid format like a one ending with \"%\"\n\t\t\tp.buf.writeRune(c)\n\t\t\ti++\n\t\t\tcontinue\n\t\t}\n\n\t\tlength := -1\n\t\tprecision := -1\n\t\ti++ // skip '%'\n\n\t\tdigits := func() string {\n\t\t\tstart := i\n\t\t\tfor i \u003c end \u0026\u0026 sTor[i] \u003e= '0' \u0026\u0026 sTor[i] \u003c= '9' {\n\t\t\t\ti++\n\t\t\t}\n\t\t\tif i \u003e start {\n\t\t\t\treturn string(sTor[start:i])\n\t\t\t}\n\t\t\treturn \"\"\n\t\t}\n\n\t\tif l := digits(); l != \"\" {\n\t\t\tvar err error\n\t\t\tlength, err = strconv.Atoi(l)\n\t\t\tif err != nil {\n\t\t\t\tpanic(\"ufmt: invalid length specification\")\n\t\t\t}\n\t\t}\n\n\t\tif i \u003c end \u0026\u0026 sTor[i] == '.' {\n\t\t\ti++ // skip '.'\n\t\t\tif l := digits(); l != \"\" {\n\t\t\t\tvar err error\n\t\t\t\tprecision, err = strconv.Atoi(l)\n\t\t\t\tif err != nil {\n\t\t\t\t\tpanic(\"ufmt: invalid precision specification\")\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif i \u003e= end {\n\t\t\tpanic(\"ufmt: invalid format string\")\n\t\t}\n\n\t\tverb := sTor[i]\n\t\tif verb == '%' {\n\t\t\tp.buf.writeRune('%')\n\t\t\ti++\n\t\t\tcontinue\n\t\t}\n\n\t\tif argNum \u003e= argLen {\n\t\t\tpanic(\"ufmt: not enough arguments\")\n\t\t}\n\t\targ := args[argNum]\n\t\targNum++\n\n\t\tswitch verb {\n\t\tcase 'v':\n\t\t\twriteValue(p, verb, arg)\n\t\tcase 's':\n\t\t\twriteStringWithLength(p, verb, arg, length)\n\t\tcase 'c':\n\t\t\twriteChar(p, verb, arg)\n\t\tcase 'd':\n\t\t\twriteInt(p, verb, arg)\n\t\tcase 'e', 'E', 'f', 'F', 'g', 'G':\n\t\t\twriteFloatWithPrecision(p, verb, arg, precision)\n\t\tcase 't':\n\t\t\twriteBool(p, verb, arg)\n\t\tcase 'x':\n\t\t\twriteHex(p, verb, arg)\n\t\tcase 'q':\n\t\t\twriteQuotedString(p, verb, arg)\n\t\tcase 'T':\n\t\t\twriteType(p, arg)\n\t\t// % handled before, as it does not consume an argument\n\t\tdefault:\n\t\t\tp.buf.writeString(\"(unhandled verb: %\" + string(verb) + \")\")\n\t\t}\n\n\t\ti++\n\t}\n\n\tif argNum \u003c argLen {\n\t\tpanic(\"ufmt: too many arguments\")\n\t}\n}\n\n// writeValue handles %v formatting\nfunc writeValue(p *printer, verb rune, arg any) {\n\tswitch v := arg.(type) {\n\tcase nil:\n\t\tp.buf.writeString(\"\u003cnil\u003e\")\n\tcase bool:\n\t\twriteBool(p, verb, v)\n\tcase int:\n\t\tp.buf.writeString(strconv.Itoa(v))\n\tcase int8:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase int16:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase int32:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase int64:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase uint:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint8:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint16:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint32:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint64:\n\t\tp.buf.writeString(strconv.FormatUint(v, 10))\n\tcase float64:\n\t\tp.buf.writeString(strconv.FormatFloat(v, 'g', -1, 64))\n\tcase string:\n\t\tp.buf.writeString(v)\n\tcase []byte:\n\t\tp.buf.write(v)\n\tcase []rune:\n\t\tp.buf.writeString(string(v))\n\tcase (interface{ String() string }):\n\t\tp.buf.writeString(v.String())\n\tcase error:\n\t\tp.buf.writeString(v.Error())\n\tdefault:\n\t\tp.buf.writeString(fallback(verb, v))\n\t}\n}\n\n// writeStringWithLength handles %s formatting with length specification\nfunc writeStringWithLength(p *printer, verb rune, arg any, length int) {\n\tvar s string\n\tswitch v := arg.(type) {\n\tcase (interface{ String() string }):\n\t\ts = v.String()\n\tcase error:\n\t\ts = v.Error()\n\tcase string:\n\t\ts = v\n\tdefault:\n\t\ts = fallback(verb, v)\n\t}\n\n\tif length \u003e 0 \u0026\u0026 len(s) \u003c length {\n\t\ts = strings.Repeat(\" \", length-len(s)) + s\n\t}\n\tp.buf.writeString(s)\n}\n\n// writeChar handles %c formatting\nfunc writeChar(p *printer, verb rune, arg any) {\n\tswitch v := arg.(type) {\n\t// rune is int32. Exclude overflowing numeric types and dups (byte, int32):\n\tcase rune:\n\t\tp.buf.writeString(string(v))\n\tcase int:\n\t\tp.buf.writeRune(rune(v))\n\tcase int8:\n\t\tp.buf.writeRune(rune(v))\n\tcase int16:\n\t\tp.buf.writeRune(rune(v))\n\tcase uint:\n\t\tp.buf.writeRune(rune(v))\n\tcase uint8:\n\t\tp.buf.writeRune(rune(v))\n\tcase uint16:\n\t\tp.buf.writeRune(rune(v))\n\tdefault:\n\t\tp.buf.writeString(fallback(verb, v))\n\t}\n}\n\n// writeInt handles %d formatting\nfunc writeInt(p *printer, verb rune, arg any) {\n\tswitch v := arg.(type) {\n\tcase int:\n\t\tp.buf.writeString(strconv.Itoa(v))\n\tcase int8:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase int16:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase int32:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase int64:\n\t\tp.buf.writeString(strconv.Itoa(int(v)))\n\tcase uint:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint8:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint16:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint32:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 10))\n\tcase uint64:\n\t\tp.buf.writeString(strconv.FormatUint(v, 10))\n\tdefault:\n\t\tp.buf.writeString(fallback(verb, v))\n\t}\n}\n\n// writeFloatWithPrecision handles floating-point formatting with precision\nfunc writeFloatWithPrecision(p *printer, verb rune, arg any, precision int) {\n\tswitch v := arg.(type) {\n\tcase float64:\n\t\tformat := byte(verb)\n\t\tif format == 'F' {\n\t\t\tformat = 'f'\n\t\t}\n\t\tif precision \u003c 0 {\n\t\t\tswitch format {\n\t\t\tcase 'e', 'E':\n\t\t\t\tprecision = 2\n\t\t\tdefault:\n\t\t\t\tprecision = 6\n\t\t\t}\n\t\t}\n\t\tp.buf = strconv.AppendFloat(p.buf, v, format, precision, 64)\n\tdefault:\n\t\tp.buf.writeString(fallback(verb, v))\n\t}\n}\n\n// writeBool handles %t formatting\nfunc writeBool(p *printer, verb rune, arg any) {\n\tswitch v := arg.(type) {\n\tcase bool:\n\t\tif v {\n\t\t\tp.buf.writeString(\"true\")\n\t\t} else {\n\t\t\tp.buf.writeString(\"false\")\n\t\t}\n\tdefault:\n\t\tp.buf.writeString(fallback(verb, v))\n\t}\n}\n\n// writeHex handles %x formatting\nfunc writeHex(p *printer, verb rune, arg any) {\n\tswitch v := arg.(type) {\n\tcase uint8:\n\t\tp.buf.writeString(strconv.FormatUint(uint64(v), 16))\n\tdefault:\n\t\tp.buf.writeString(\"(unhandled)\")\n\t}\n}\n\n// writeQuotedString handles %q formatting\nfunc writeQuotedString(p *printer, verb rune, arg any) {\n\tswitch v := arg.(type) {\n\tcase string:\n\t\tp.buf.writeString(strconv.Quote(v))\n\tdefault:\n\t\tp.buf.writeString(\"(unhandled)\")\n\t}\n}\n\n// writeType handles %T formatting\nfunc writeType(p *printer, arg any) {\n\tswitch arg.(type) {\n\tcase bool:\n\t\tp.buf.writeString(\"bool\")\n\tcase int:\n\t\tp.buf.writeString(\"int\")\n\tcase int8:\n\t\tp.buf.writeString(\"int8\")\n\tcase int16:\n\t\tp.buf.writeString(\"int16\")\n\tcase int32:\n\t\tp.buf.writeString(\"int32\")\n\tcase int64:\n\t\tp.buf.writeString(\"int64\")\n\tcase uint:\n\t\tp.buf.writeString(\"uint\")\n\tcase uint8:\n\t\tp.buf.writeString(\"uint8\")\n\tcase uint16:\n\t\tp.buf.writeString(\"uint16\")\n\tcase uint32:\n\t\tp.buf.writeString(\"uint32\")\n\tcase uint64:\n\t\tp.buf.writeString(\"uint64\")\n\tcase string:\n\t\tp.buf.writeString(\"string\")\n\tcase []byte:\n\t\tp.buf.writeString(\"[]byte\")\n\tcase []rune:\n\t\tp.buf.writeString(\"[]rune\")\n\tdefault:\n\t\tp.buf.writeString(\"unknown\")\n\t}\n}\n\n// Fprintf formats according to a format specifier and writes to w.\n// Returns the number of bytes written and any write error encountered.\nfunc Fprintf(w io.Writer, format string, a ...any) (n int, err error) {\n\tp := newPrinter()\n\tp.doPrintf(format, a)\n\treturn w.Write(p.buf)\n}\n\n// Printf formats according to a format specifier and writes to standard output.\n// Returns the number of bytes written and any write error encountered.\n//\n// XXX: Replace with os.Stdout handling when available.\nfunc Printf(format string, a ...any) (n int, err error) {\n\tvar out strings.Builder\n\tn, err = Fprintf(\u0026out, format, a...)\n\tprint(out.String())\n\treturn n, err\n}\n\n// Appendf formats according to a format specifier, appends the result to the byte\n// slice, and returns the updated slice.\nfunc Appendf(b []byte, format string, a ...any) []byte {\n\tp := newPrinter()\n\tp.doPrintf(format, a)\n\treturn append(b, p.buf...)\n}\n\n// Fprint formats using default formats and writes to w.\n// Spaces are added between arguments.\n// Returns the number of bytes written and any write error encountered.\nfunc Fprint(w io.Writer, a ...any) (n int, err error) {\n\tp := newPrinter()\n\tp.doPrint(a)\n\treturn w.Write(p.buf)\n}\n\n// Print formats using default formats and writes to standard output.\n// Spaces are added between arguments.\n// Returns the number of bytes written and any write error encountered.\n//\n// XXX: Replace with os.Stdout handling when available.\nfunc Print(a ...any) (n int, err error) {\n\tvar out strings.Builder\n\tn, err = Fprint(\u0026out, a...)\n\tprint(out.String())\n\treturn n, err\n}\n\n// Append formats using default formats, appends to b, and returns the updated slice.\n// Spaces are added between arguments.\nfunc Append(b []byte, a ...any) []byte {\n\tp := newPrinter()\n\tp.doPrint(a)\n\treturn append(b, p.buf...)\n}\n\n// Fprintln formats using default formats and writes to w with newline.\n// Returns the number of bytes written and any write error encountered.\nfunc Fprintln(w io.Writer, a ...any) (n int, err error) {\n\tp := newPrinter()\n\tp.doPrintln(a)\n\treturn w.Write(p.buf)\n}\n\n// Println formats using default formats and writes to standard output with newline.\n// Returns the number of bytes written and any write error encountered.\n//\n// XXX: Replace with os.Stdout handling when available.\nfunc Println(a ...any) (n int, err error) {\n\tvar out strings.Builder\n\tn, err = Fprintln(\u0026out, a...)\n\tprint(out.String())\n\treturn n, err\n}\n\n// Sprintln formats using default formats and returns the string with newline.\n// Spaces are always added between arguments.\nfunc Sprintln(a ...any) string {\n\tp := newPrinter()\n\tp.doPrintln(a)\n\treturn string(p.buf)\n}\n\n// Appendln formats using default formats, appends to b, and returns the updated slice.\n// Appends a newline after the last argument.\nfunc Appendln(b []byte, a ...any) []byte {\n\tp := newPrinter()\n\tp.doPrintln(a)\n\treturn append(b, p.buf...)\n}\n\n// This function is used to mimic Go's fmt.Sprintf\n// specific behaviour of showing verb/type mismatches,\n// where for example:\n//\n//\tfmt.Sprintf(\"%d\", \"foo\") gives \"%!d(string=foo)\"\n//\n// Here:\n//\n//\tfallback(\"s\", 8) -\u003e \"%!s(int=8)\"\n//\tfallback(\"d\", nil) -\u003e \"%!d(\u003cnil\u003e)\", and so on.f\nfunc fallback(verb rune, arg any) string {\n\tvar s string\n\tswitch v := arg.(type) {\n\tcase string:\n\t\ts = \"string=\" + v\n\tcase (interface{ String() string }):\n\t\ts = \"string=\" + v.String()\n\tcase error:\n\t\t// note: also \"string=\" in Go fmt\n\t\ts = \"string=\" + v.Error()\n\tcase float64:\n\t\ts = \"float64=\" + Sprintf(\"%f\", v)\n\tcase int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:\n\t\t// note: rune, byte would be dups, being aliases\n\t\tif typename, e := typeToString(v); e == nil {\n\t\t\ts = typename + \"=\" + Sprintf(\"%d\", v)\n\t\t} else {\n\t\t\tpanic(\"ufmt: unexpected type error\")\n\t\t}\n\tcase bool:\n\t\ts = \"bool=\" + strconv.FormatBool(v)\n\tcase nil:\n\t\ts = \"\u003cnil\u003e\"\n\tdefault:\n\t\ts = \"(unhandled)\"\n\t}\n\treturn \"%!\" + string(verb) + \"(\" + s + \")\"\n}\n\n// typeToString returns the name of basic Go types as string.\nfunc typeToString(v any) (string, error) {\n\tswitch v.(type) {\n\tcase string:\n\t\treturn \"string\", nil\n\tcase int:\n\t\treturn \"int\", nil\n\tcase int8:\n\t\treturn \"int8\", nil\n\tcase int16:\n\t\treturn \"int16\", nil\n\tcase int32:\n\t\treturn \"int32\", nil\n\tcase int64:\n\t\treturn \"int64\", nil\n\tcase uint:\n\t\treturn \"uint\", nil\n\tcase uint8:\n\t\treturn \"uint8\", nil\n\tcase uint16:\n\t\treturn \"uint16\", nil\n\tcase uint32:\n\t\treturn \"uint32\", nil\n\tcase uint64:\n\t\treturn \"uint64\", nil\n\tcase float32:\n\t\treturn \"float32\", nil\n\tcase float64:\n\t\treturn \"float64\", nil\n\tcase bool:\n\t\treturn \"bool\", nil\n\tdefault:\n\t\treturn \"\", errors.New(\"unsupported type\")\n\t}\n}\n\n// errMsg implements the error interface for formatted error strings.\ntype errMsg struct {\n\tmsg string\n}\n\n// Error returns the formatted error message.\nfunc (e *errMsg) Error() string {\n\treturn e.msg\n}\n\n// Errorf formats according to a format specifier and returns an error value.\n// Supports the same verbs as Sprintf. See Sprintf documentation for details.\nfunc Errorf(format string, args ...any) error {\n\treturn \u0026errMsg{Sprintf(format, args...)}\n}\n"},{"name":"ufmt_test.gno","body":"package ufmt\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"fmt\"\n\t\"testing\"\n)\n\ntype stringer struct{}\n\nfunc (stringer) String() string {\n\treturn \"I'm a stringer\"\n}\n\nfunc TestSprintf(t *testing.T) {\n\ttru := true\n\tcases := []struct {\n\t\tformat         string\n\t\tvalues         []any\n\t\texpectedOutput string\n\t}{\n\t\t{\"hello %s!\", []any{\"planet\"}, \"hello planet!\"},\n\t\t{\"hello %v!\", []any{\"planet\"}, \"hello planet!\"},\n\t\t{\"hi %%%s!\", []any{\"worl%d\"}, \"hi %worl%d!\"},\n\t\t{\"%s %c %d %t\", []any{\"foo\", 'α', 421, true}, \"foo α 421 true\"},\n\t\t{\"string [%s]\", []any{\"foo\"}, \"string [foo]\"},\n\t\t{\"int [%d]\", []any{int(42)}, \"int [42]\"},\n\t\t{\"int [%v]\", []any{int(42)}, \"int [42]\"},\n\t\t{\"int8 [%d]\", []any{int8(8)}, \"int8 [8]\"},\n\t\t{\"int8 [%v]\", []any{int8(8)}, \"int8 [8]\"},\n\t\t{\"int16 [%d]\", []any{int16(16)}, \"int16 [16]\"},\n\t\t{\"int16 [%v]\", []any{int16(16)}, \"int16 [16]\"},\n\t\t{\"int32 [%d]\", []any{int32(32)}, \"int32 [32]\"},\n\t\t{\"int32 [%v]\", []any{int32(32)}, \"int32 [32]\"},\n\t\t{\"int64 [%d]\", []any{int64(64)}, \"int64 [64]\"},\n\t\t{\"int64 [%v]\", []any{int64(64)}, \"int64 [64]\"},\n\t\t{\"uint [%d]\", []any{uint(42)}, \"uint [42]\"},\n\t\t{\"uint [%v]\", []any{uint(42)}, \"uint [42]\"},\n\t\t{\"uint8 [%d]\", []any{uint8(8)}, \"uint8 [8]\"},\n\t\t{\"uint8 [%v]\", []any{uint8(8)}, \"uint8 [8]\"},\n\t\t{\"uint16 [%d]\", []any{uint16(16)}, \"uint16 [16]\"},\n\t\t{\"uint16 [%v]\", []any{uint16(16)}, \"uint16 [16]\"},\n\t\t{\"uint32 [%d]\", []any{uint32(32)}, \"uint32 [32]\"},\n\t\t{\"uint32 [%v]\", []any{uint32(32)}, \"uint32 [32]\"},\n\t\t{\"uint64 [%d]\", []any{uint64(64)}, \"uint64 [64]\"},\n\t\t{\"uint64 [%v]\", []any{uint64(64)}, \"uint64 [64]\"},\n\t\t{\"float64 [%e]\", []any{float64(64.1)}, \"float64 [6.41e+01]\"},\n\t\t{\"float64 [%E]\", []any{float64(64.1)}, \"float64 [6.41E+01]\"},\n\t\t{\"float64 [%f]\", []any{float64(64.1)}, \"float64 [64.100000]\"},\n\t\t{\"float64 [%F]\", []any{float64(64.1)}, \"float64 [64.100000]\"},\n\t\t{\"float64 [%g]\", []any{float64(64.1)}, \"float64 [64.1]\"},\n\t\t{\"float64 [%G]\", []any{float64(64.1)}, \"float64 [64.1]\"},\n\t\t{\"bool [%t]\", []any{true}, \"bool [true]\"},\n\t\t{\"bool [%v]\", []any{true}, \"bool [true]\"},\n\t\t{\"bool [%t]\", []any{false}, \"bool [false]\"},\n\t\t{\"bool [%v]\", []any{false}, \"bool [false]\"},\n\t\t{\"no args\", nil, \"no args\"},\n\t\t{\"finish with %\", nil, \"finish with %\"},\n\t\t{\"stringer [%s]\", []any{stringer{}}, \"stringer [I'm a stringer]\"},\n\t\t{\"â\", nil, \"â\"},\n\t\t{\"Hello, World! 😊\", nil, \"Hello, World! 😊\"},\n\t\t{\"unicode formatting: %s\", []any{\"😊\"}, \"unicode formatting: 😊\"},\n\t\t{\"invalid hex [%x]\", []any{\"invalid\"}, \"invalid hex [(unhandled)]\"},\n\t\t{\"rune as character [%c]\", []any{rune('A')}, \"rune as character [A]\"},\n\t\t{\"int as character [%c]\", []any{int('B')}, \"int as character [B]\"},\n\t\t{\"quoted string [%q]\", []any{\"hello\"}, \"quoted string [\\\"hello\\\"]\"},\n\t\t{\"quoted string with escape [%q]\", []any{\"\\thello\\nworld\\\\\"}, \"quoted string with escape [\\\"\\\\thello\\\\nworld\\\\\\\\\\\"]\"},\n\t\t{\"invalid quoted string [%q]\", []any{123}, \"invalid quoted string [(unhandled)]\"},\n\t\t{\"type of bool [%T]\", []any{true}, \"type of bool [bool]\"},\n\t\t{\"type of int [%T]\", []any{123}, \"type of int [int]\"},\n\t\t{\"type of string [%T]\", []any{\"hello\"}, \"type of string [string]\"},\n\t\t{\"type of []byte [%T]\", []any{[]byte{1, 2, 3}}, \"type of []byte [[]byte]\"},\n\t\t{\"type of []rune [%T]\", []any{[]rune{'a', 'b', 'c'}}, \"type of []rune [[]rune]\"},\n\t\t{\"type of unknown [%T]\", []any{struct{}{}}, \"type of unknown [unknown]\"},\n\t\t// mismatch printing\n\t\t{\"%s\", []any{nil}, \"%!s(\u003cnil\u003e)\"},\n\t\t{\"%s\", []any{421}, \"%!s(int=421)\"},\n\t\t{\"%s\", []any{\"z\"}, \"z\"},\n\t\t{\"%s\", []any{tru}, \"%!s(bool=true)\"},\n\t\t{\"%s\", []any{'z'}, \"%!s(int32=122)\"},\n\n\t\t{\"%c\", []any{nil}, \"%!c(\u003cnil\u003e)\"},\n\t\t{\"%c\", []any{421}, \"ƥ\"},\n\t\t{\"%c\", []any{\"z\"}, \"%!c(string=z)\"},\n\t\t{\"%c\", []any{tru}, \"%!c(bool=true)\"},\n\t\t{\"%c\", []any{'z'}, \"z\"},\n\n\t\t{\"%d\", []any{nil}, \"%!d(\u003cnil\u003e)\"},\n\t\t{\"%d\", []any{421}, \"421\"},\n\t\t{\"%d\", []any{\"z\"}, \"%!d(string=z)\"},\n\t\t{\"%d\", []any{tru}, \"%!d(bool=true)\"},\n\t\t{\"%d\", []any{'z'}, \"122\"},\n\n\t\t{\"%t\", []any{nil}, \"%!t(\u003cnil\u003e)\"},\n\t\t{\"%t\", []any{421}, \"%!t(int=421)\"},\n\t\t{\"%t\", []any{\"z\"}, \"%!t(string=z)\"},\n\t\t{\"%t\", []any{tru}, \"true\"},\n\t\t{\"%t\", []any{'z'}, \"%!t(int32=122)\"},\n\n\t\t{\"%.2f\", []any{3.14159}, \"3.14\"},\n\t\t{\"%.4f\", []any{3.14159}, \"3.1416\"},\n\t\t{\"%.0f\", []any{3.14159}, \"3\"},\n\t\t{\"%.1f\", []any{3.0}, \"3.0\"},\n\t\t{\"%.3F\", []any{3.14159}, \"3.142\"},\n\t\t{\"%.2e\", []any{314.159}, \"3.14e+02\"},\n\t\t{\"%.3E\", []any{314.159}, \"3.142E+02\"},\n\t\t{\"%.3g\", []any{3.14159}, \"3.14\"},\n\t\t{\"%.5G\", []any{3.14159}, \"3.1416\"},\n\t\t{\"%.0f\", []any{3.6}, \"4\"},\n\t\t{\"%.0f\", []any{3.4}, \"3\"},\n\t\t{\"%.1f\", []any{0.0}, \"0.0\"},\n\t\t{\"%.2f\", []any{1e6}, \"1000000.00\"},\n\t\t{\"%.2f\", []any{1e-6}, \"0.00\"},\n\n\t\t{\"%5s\", []any{\"Hello World\"}, \"Hello World\"},\n\t\t{\"%3s\", []any{\"Hi\"}, \" Hi\"},\n\t\t{\"%2s\", []any{\"Hello\"}, \"Hello\"},\n\t\t{\"%1s\", []any{\"A\"}, \"A\"},\n\t\t{\"%0s\", []any{\"Test\"}, \"Test\"},\n\t\t{\"%5s!\", []any{\"Hello World\"}, \"Hello World!\"},\n\t\t{\"_%5s_\", []any{\"abc\"}, \"_  abc_\"},\n\t\t{\"%2s%4s\", []any{\"ab\", \"cde\"}, \"ab cde\"},\n\t\t{\"%5s\", []any{\"\"}, \"     \"},\n\t\t{\"%3s\", []any{nil}, \"%!s(\u003cnil\u003e)\"},\n\t\t{\"%2s\", []any{123}, \"%!s(int=123)\"},\n\t}\n\n\tfor _, tc := range cases {\n\t\tname := fmt.Sprintf(tc.format, tc.values...)\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tgot := Sprintf(tc.format, tc.values...)\n\t\t\tif got != tc.expectedOutput {\n\t\t\t\tt.Errorf(\"got %q, want %q.\", got, tc.expectedOutput)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestErrorf(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tformat   string\n\t\targs     []any\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"simple string\",\n\t\t\tformat:   \"error: %s\",\n\t\t\targs:     []any{\"something went wrong\"},\n\t\t\texpected: \"error: something went wrong\",\n\t\t},\n\t\t{\n\t\t\tname:     \"integer value\",\n\t\t\tformat:   \"value: %d\",\n\t\t\targs:     []any{42},\n\t\t\texpected: \"value: 42\",\n\t\t},\n\t\t{\n\t\t\tname:     \"boolean value\",\n\t\t\tformat:   \"success: %t\",\n\t\t\targs:     []any{true},\n\t\t\texpected: \"success: true\",\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple values\",\n\t\t\tformat:   \"error %d: %s (success=%t)\",\n\t\t\targs:     []any{123, \"failure occurred\", false},\n\t\t\texpected: \"error 123: failure occurred (success=false)\",\n\t\t},\n\t\t{\n\t\t\tname:     \"literal percent\",\n\t\t\tformat:   \"literal %%\",\n\t\t\targs:     []any{},\n\t\t\texpected: \"literal %\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\terr := Errorf(tt.format, tt.args...)\n\t\t\tif err.Error() != tt.expected {\n\t\t\t\tt.Errorf(\"Errorf(%q, %v) = %q, expected %q\", tt.format, tt.args, err.Error(), tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestPrintErrors(t *testing.T) {\n\tgot := Sprintf(\"error: %s\", errors.New(\"can I be printed?\"))\n\texpectedOutput := \"error: can I be printed?\"\n\tif got != expectedOutput {\n\t\tt.Errorf(\"got %q, want %q.\", got, expectedOutput)\n\t}\n}\n\nfunc TestSprint(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\targs     []any\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"Empty args\",\n\t\t\targs:     []any{},\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:     \"String args\",\n\t\t\targs:     []any{\"Hello\", \"World\"},\n\t\t\texpected: \"Hello World\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Integer args\",\n\t\t\targs:     []any{1, 2, 3},\n\t\t\texpected: \"1 2 3\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Mixed args\",\n\t\t\targs:     []any{\"Hello\", 42, true, false, \"World\"},\n\t\t\texpected: \"Hello 42 true false World\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Unhandled type\",\n\t\t\targs:     []any{\"Hello\", 3.14, []int{1, 2, 3}},\n\t\t\texpected: \"Hello 3.140000 (unhandled)\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tgot := Sprint(tc.args...)\n\t\t\tif got != tc.expected {\n\t\t\t\tt.Errorf(\"got %q, want %q.\", got, tc.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestFprintf(t *testing.T) {\n\tvar buf bytes.Buffer\n\tn, err := Fprintf(\u0026buf, \"Count: %d, Message: %s\", 42, \"hello\")\n\tif err != nil {\n\t\tt.Fatalf(\"Fprintf failed: %v\", err)\n\t}\n\n\tconst expected = \"Count: 42, Message: hello\"\n\tif buf.String() != expected {\n\t\tt.Errorf(\"Expected %q, got %q\", expected, buf.String())\n\t}\n\tif n != len(expected) {\n\t\tt.Errorf(\"Expected %d bytes written, got %d\", len(expected), n)\n\t}\n}\n\n// TODO: replace os.Stdout with a buffer to capture the output and test it.\nfunc TestPrintf(t *testing.T) {\n\tn, err := Printf(\"The answer is %d\", 42)\n\tif err != nil {\n\t\tt.Fatalf(\"Printf failed: %v\", err)\n\t}\n\n\tconst expected = \"The answer is 42\"\n\tif n != len(expected) {\n\t\tt.Errorf(\"Expected 14 bytes written, got %d\", n)\n\t}\n}\n\nfunc TestAppendf(t *testing.T) {\n\tb := []byte(\"Header: \")\n\tresult := Appendf(b, \"Value %d\", 7)\n\tconst expected = \"Header: Value 7\"\n\tif string(result) != expected {\n\t\tt.Errorf(\"Expected %q, got %q\", expected, string(result))\n\t}\n}\n\nfunc TestFprint(t *testing.T) {\n\tvar buf bytes.Buffer\n\tn, err := Fprint(\u0026buf, \"Hello\", 42, true)\n\tif err != nil {\n\t\tt.Fatalf(\"Fprint failed: %v\", err)\n\t}\n\n\tconst expected = \"Hello 42 true\"\n\tif buf.String() != expected {\n\t\tt.Errorf(\"Expected %q, got %q\", expected, buf.String())\n\t}\n\tif n != len(expected) {\n\t\tt.Errorf(\"Expected %d bytes written, got %d\", len(expected), n)\n\t}\n}\n\n// TODO: replace os.Stdout with a buffer to capture the output and test it.\nfunc TestPrint(t *testing.T) {\n\tn, err := Print(\"Mixed\", 3.14, false)\n\tif err != nil {\n\t\tt.Fatalf(\"Print failed: %v\", err)\n\t}\n\n\tconst expected = \"Mixed 3.140000 false\"\n\tif n != len(expected) {\n\t\tt.Errorf(\"Expected 12 bytes written, got %d\", n)\n\t}\n}\n\nfunc TestAppend(t *testing.T) {\n\tb := []byte{0x01, 0x02}\n\tresult := Append(b, \"Test\", 99)\n\n\tconst expected = \"\\x01\\x02Test 99\"\n\tif string(result) != expected {\n\t\tt.Errorf(\"Expected %q, got %q\", expected, string(result))\n\t}\n}\n\nfunc TestFprintln(t *testing.T) {\n\tvar buf bytes.Buffer\n\tn, err := Fprintln(\u0026buf, \"Line\", 1)\n\tif err != nil {\n\t\tt.Fatalf(\"Fprintln failed: %v\", err)\n\t}\n\n\tconst expected = \"Line 1\\n\"\n\tif buf.String() != expected {\n\t\tt.Errorf(\"Expected %q, got %q\", expected, buf.String())\n\t}\n\tif n != len(expected) {\n\t\tt.Errorf(\"Expected %d bytes written, got %d\", len(expected), n)\n\t}\n}\n\n// TODO: replace os.Stdout with a buffer to capture the output and test it.\nfunc TestPrintln(t *testing.T) {\n\tn, err := Println(\"Output\", \"test\")\n\tif err != nil {\n\t\tt.Fatalf(\"Println failed: %v\", err)\n\t}\n\n\tconst expected = \"Output test\\n\"\n\tif n != len(expected) {\n\t\tt.Errorf(\"Expected 12 bytes written, got %d\", n)\n\t}\n}\n\nfunc TestSprintln(t *testing.T) {\n\tresult := Sprintln(\"Item\", 42)\n\n\tconst expected = \"Item 42\\n\"\n\tif result != expected {\n\t\tt.Errorf(\"Expected %q, got %q\", expected, result)\n\t}\n}\n\nfunc TestAppendln(t *testing.T) {\n\tb := []byte(\"Start:\")\n\tresult := Appendln(b, \"End\")\n\n\tconst expected = \"Start:End\\n\"\n\tif string(result) != expected {\n\t\tt.Errorf(\"Expected %q, got %q\", expected, string(result))\n\t}\n}\n\nfunc assertNoError(t *testing.T, err error) {\n\tt.Helper()\n\tif err != nil {\n\t\tt.Fatalf(\"Unexpected error: %v\", err)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"JsGt4JaXXP4F+BA/tu7uRtAQKvj0u+QpI6CtkyL27vC2t4sS/NBVNUNx6tcYBouTSAIZ/6MY+YjULrEo5nSjAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"diff","path":"gno.land/p/onbloc/diff","files":[{"name":"diff.gno","body":"// The diff package implements the Myers diff algorithm to compute the edit distance\n// and generate a minimal edit script between two strings.\n//\n// Edit distance, also known as Levenshtein distance, is a measure of the similarity\n// between two strings. It is defined as the minimum number of single-character edits (insertions,\n// deletions, or substitutions) required to change one string into the other.\npackage diff\n\nimport (\n\t\"strings\"\n)\n\n// EditType represents the type of edit operation in a diff.\ntype EditType uint8\n\nconst (\n\t// EditKeep indicates that a character is unchanged in both strings.\n\tEditKeep EditType = iota\n\n\t// EditInsert indicates that a character was inserted in the new string.\n\tEditInsert\n\n\t// EditDelete indicates that a character was deleted from the old string.\n\tEditDelete\n)\n\n// Edit represent a single edit operation in a diff.\ntype Edit struct {\n\t// Type is the kind of edit operation.\n\tType EditType\n\n\t// Char is the character involved in the edit operation.\n\tChar rune\n}\n\n// MyersDiff computes the difference between two strings using Myers' diff algorithm.\n// It returns a slice of Edit operations that transform the old string into the new string.\n// This implementation finds the shortest edit script (SES) that represents the minimal\n// set of operations to transform one string into the other.\n//\n// The function handles both ASCII and non-ASCII characters correctly.\n//\n// Time complexity: O((N+M)D), where N and M are the lengths of the input strings,\n// and D is the size of the minimum edit script.\n//\n// Space complexity: O((N+M)D)\n//\n// In the worst case, where the strings are completely different, D can be as large as N+M,\n// leading to a time and space complexity of O((N+M)^2). However, for strings with many\n// common substrings, the performance is much better, often closer to O(N+M).\n//\n// Parameters:\n//   - old: the original string.\n//   - new: the modified string.\n//\n// Returns:\n//   - A slice of Edit operations representing the minimum difference between the two strings.\nfunc MyersDiff(old, new string) []Edit {\n\toldRunes, newRunes := []rune(old), []rune(new)\n\tn, m := len(oldRunes), len(newRunes)\n\n\tif n == 0 \u0026\u0026 m == 0 {\n\t\treturn []Edit{}\n\t}\n\n\t// old is empty\n\tif n == 0 {\n\t\tedits := make([]Edit, m)\n\t\tfor i, r := range newRunes {\n\t\t\tedits[i] = Edit{Type: EditInsert, Char: r}\n\t\t}\n\t\treturn edits\n\t}\n\n\tif m == 0 {\n\t\tedits := make([]Edit, n)\n\t\tfor i, r := range oldRunes {\n\t\t\tedits[i] = Edit{Type: EditDelete, Char: r}\n\t\t}\n\t\treturn edits\n\t}\n\n\tmax := n + m\n\tv := make([]int, 2*max+1)\n\tvar trace [][]int\nsearch:\n\tfor d := 0; d \u003c= max; d++ {\n\t\t// iterate through diagonals\n\t\tfor k := -d; k \u003c= d; k += 2 {\n\t\t\tvar x int\n\t\t\tif k == -d || (k != d \u0026\u0026 v[max+k-1] \u003c v[max+k+1]) {\n\t\t\t\tx = v[max+k+1] // move down\n\t\t\t} else {\n\t\t\t\tx = v[max+k-1] + 1 // move right\n\t\t\t}\n\t\t\ty := x - k\n\n\t\t\t// extend the path as far as possible with matching characters\n\t\t\tfor x \u003c n \u0026\u0026 y \u003c m \u0026\u0026 oldRunes[x] == newRunes[y] {\n\t\t\t\tx++\n\t\t\t\ty++\n\t\t\t}\n\n\t\t\tv[max+k] = x\n\n\t\t\t// check if we've reached the end of both strings\n\t\t\tif x == n \u0026\u0026 y == m {\n\t\t\t\ttrace = append(trace, append([]int(nil), v...))\n\t\t\t\tbreak search\n\t\t\t}\n\t\t}\n\t\ttrace = append(trace, append([]int(nil), v...))\n\t}\n\n\t// backtrack to construct the edit script\n\tedits := make([]Edit, 0, n+m)\n\tx, y := n, m\n\tfor d := len(trace) - 1; d \u003e= 0; d-- {\n\t\tvPrev := trace[d]\n\t\tk := x - y\n\t\tvar prevK int\n\t\tif k == -d || (k != d \u0026\u0026 vPrev[max+k-1] \u003c vPrev[max+k+1]) {\n\t\t\tprevK = k + 1\n\t\t} else {\n\t\t\tprevK = k - 1\n\t\t}\n\t\tprevX := vPrev[max+prevK]\n\t\tprevY := prevX - prevK\n\n\t\t// add keep edits for matching characters\n\t\tfor x \u003e prevX \u0026\u0026 y \u003e prevY {\n\t\t\tif x \u003e 0 \u0026\u0026 y \u003e 0 {\n\t\t\t\tedits = append([]Edit{{Type: EditKeep, Char: oldRunes[x-1]}}, edits...)\n\t\t\t}\n\t\t\tx--\n\t\t\ty--\n\t\t}\n\t\tif y \u003e prevY {\n\t\t\tif y \u003e 0 {\n\t\t\t\tedits = append([]Edit{{Type: EditInsert, Char: newRunes[y-1]}}, edits...)\n\t\t\t}\n\t\t\ty--\n\t\t} else if x \u003e prevX {\n\t\t\tif x \u003e 0 {\n\t\t\t\tedits = append([]Edit{{Type: EditDelete, Char: oldRunes[x-1]}}, edits...)\n\t\t\t}\n\t\t\tx--\n\t\t}\n\t}\n\n\treturn edits\n}\n\n// Format converts a slice of Edit operations into a human-readable string representation.\n// It groups consecutive edits of the same type and formats them as follows:\n//   - Unchanged characters are left as-is\n//   - Inserted characters are wrapped in [+...]\n//   - Deleted characters are wrapped in [-...]\n//\n// This function is useful for visualizing the differences between two strings\n// in a compact and intuitive format.\n//\n// Parameters:\n//   - edits: A slice of Edit operations, typically produced by MyersDiff\n//\n// Returns:\n//   - A formatted string representing the diff\n//\n// Example output:\n//\n//\tFor the diff between \"abcd\" and \"acbd\", the output might be:\n//\t\"a[-b]c[+b]d\"\n//\n// Note:\n//\n//\tThe function assumes that the input slice of edits is in the correct order.\n//\tAn empty input slice will result in an empty string.\nfunc Format(edits []Edit) string {\n\tif len(edits) == 0 {\n\t\treturn \"\"\n\t}\n\n\tvar (\n\t\tresult       strings.Builder\n\t\tcurrentType  EditType\n\t\tcurrentChars strings.Builder\n\t)\n\n\tflushCurrent := func() {\n\t\tif currentChars.Len() \u003e 0 {\n\t\t\tswitch currentType {\n\t\t\tcase EditKeep:\n\t\t\t\tresult.WriteString(currentChars.String())\n\t\t\tcase EditInsert:\n\t\t\t\tresult.WriteString(\"[+\")\n\t\t\t\tresult.WriteString(currentChars.String())\n\t\t\t\tresult.WriteByte(']')\n\t\t\tcase EditDelete:\n\t\t\t\tresult.WriteString(\"[-\")\n\t\t\t\tresult.WriteString(currentChars.String())\n\t\t\t\tresult.WriteByte(']')\n\t\t\t}\n\t\t\tcurrentChars.Reset()\n\t\t}\n\t}\n\n\tfor _, edit := range edits {\n\t\tif edit.Type != currentType {\n\t\t\tflushCurrent()\n\t\t\tcurrentType = edit.Type\n\t\t}\n\t\tcurrentChars.WriteRune(edit.Char)\n\t}\n\tflushCurrent()\n\n\treturn result.String()\n}\n"},{"name":"diff_test.gno","body":"package diff\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestMyersDiff(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\told      string\n\t\tnew      string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"No difference\",\n\t\t\told:      \"abc\",\n\t\t\tnew:      \"abc\",\n\t\t\texpected: \"abc\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Simple insertion\",\n\t\t\told:      \"ac\",\n\t\t\tnew:      \"abc\",\n\t\t\texpected: \"a[+b]c\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Simple deletion\",\n\t\t\told:      \"abc\",\n\t\t\tnew:      \"ac\",\n\t\t\texpected: \"a[-b]c\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Simple substitution\",\n\t\t\told:      \"abc\",\n\t\t\tnew:      \"abd\",\n\t\t\texpected: \"ab[-c][+d]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Multiple changes\",\n\t\t\told:      \"The quick brown fox jumps over the lazy dog\",\n\t\t\tnew:      \"The quick brown cat jumps over the lazy dog\",\n\t\t\texpected: \"The quick brown [-fox][+cat] jumps over the lazy dog\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Prefix and suffix\",\n\t\t\told:      \"Hello, world!\",\n\t\t\tnew:      \"Hello, beautiful world!\",\n\t\t\texpected: \"Hello, [+beautiful ]world!\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Complete change\",\n\t\t\told:      \"abcdef\",\n\t\t\tnew:      \"ghijkl\",\n\t\t\texpected: \"[-abcdef][+ghijkl]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Empty strings\",\n\t\t\told:      \"\",\n\t\t\tnew:      \"\",\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Old empty\",\n\t\t\told:      \"\",\n\t\t\tnew:      \"abc\",\n\t\t\texpected: \"[+abc]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"New empty\",\n\t\t\told:      \"abc\",\n\t\t\tnew:      \"\",\n\t\t\texpected: \"[-abc]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"non-ascii (Korean characters)\",\n\t\t\told:      \"ASCII 문자가 아닌 것도 되나?\",\n\t\t\tnew:      \"ASCII 문자가 아닌 것도 됨.\",\n\t\t\texpected: \"ASCII 문자가 아닌 것도 [-되나?][+됨.]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Emoji diff\",\n\t\t\told:      \"Hello 👋 World 🌍\",\n\t\t\tnew:      \"Hello 👋 Beautiful 🌸 World 🌍\",\n\t\t\texpected: \"Hello 👋 [+Beautiful 🌸 ]World 🌍\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Mixed multibyte and ASCII\",\n\t\t\told:      \"こんにちは World\",\n\t\t\tnew:      \"こんばんは World\",\n\t\t\texpected: \"こん[-にち][+ばん]は World\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Chinese characters\",\n\t\t\told:      \"我喜欢编程\",\n\t\t\tnew:      \"我喜欢看书和编程\",\n\t\t\texpected: \"我喜欢[+看书和]编程\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Combining characters\",\n\t\t\told:      \"e\\u0301\", // é (e + ´)\n\t\t\tnew:      \"e\\u0300\", // è (e + `)\n\t\t\texpected: \"e[-\\u0301][+\\u0300]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Right-to-Left languages\",\n\t\t\told:      \"שלום\",\n\t\t\tnew:      \"שלום עולם\",\n\t\t\texpected: \"שלום[+ עולם]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Normalization NFC and NFD\",\n\t\t\told:      \"e\\u0301\", // NFD (decomposed)\n\t\t\tnew:      \"\\u00e9\",  // NFC (precomposed)\n\t\t\texpected: \"[-e\\u0301][+\\u00e9]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Case sensitivity\",\n\t\t\told:      \"abc\",\n\t\t\tnew:      \"Abc\",\n\t\t\texpected: \"[-a][+A]bc\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Surrogate pairs\",\n\t\t\told:      \"Hello 🌍\",\n\t\t\tnew:      \"Hello 🌎\",\n\t\t\texpected: \"Hello [-🌍][+🌎]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Control characters\",\n\t\t\told:      \"Line1\\nLine2\",\n\t\t\tnew:      \"Line1\\r\\nLine2\",\n\t\t\texpected: \"Line1[+\\r]\\nLine2\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Mixed scripts\",\n\t\t\told:      \"Hello नमस्ते こんにちは\",\n\t\t\tnew:      \"Hello สวัสดี こんにちは\",\n\t\t\texpected: \"Hello [-नमस्ते][+สวัสดี] こんにちは\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Unicode normalization\",\n\t\t\told:      \"é\",       // U+00E9 (precomposed)\n\t\t\tnew:      \"e\\u0301\", // U+0065 U+0301 (decomposed)\n\t\t\texpected: \"[-é][+e\\u0301]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Directional marks\",\n\t\t\told:      \"Hello\\u200Eworld\", // LTR mark\n\t\t\tnew:      \"Hello\\u200Fworld\", // RTL mark\n\t\t\texpected: \"Hello[-\\u200E][+\\u200F]world\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Zero-width characters\",\n\t\t\told:      \"ab\\u200Bc\", // Zero-width space\n\t\t\tnew:      \"abc\",\n\t\t\texpected: \"ab[-\\u200B]c\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Worst-case scenario (completely different strings)\",\n\t\t\told:      strings.Repeat(\"a\", 1000),\n\t\t\tnew:      strings.Repeat(\"b\", 1000),\n\t\t\texpected: \"[-\" + strings.Repeat(\"a\", 1000) + \"][+\" + strings.Repeat(\"b\", 1000) + \"]\",\n\t\t},\n\t\t//{ // disabled for testing performance\n\t\t// XXX: consider adding a flag to run such tests, not like `-short`, or switching to a `-bench`, maybe.\n\t\t//\tname:     \"Very long strings\",\n\t\t//\told:      strings.Repeat(\"a\", 10000) + \"b\" + strings.Repeat(\"a\", 10000),\n\t\t//\tnew:      strings.Repeat(\"a\", 10000) + \"c\" + strings.Repeat(\"a\", 10000),\n\t\t//\texpected: strings.Repeat(\"a\", 10000) + \"[-b][+c]\" + strings.Repeat(\"a\", 10000),\n\t\t//},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdiff := MyersDiff(tc.old, tc.new)\n\t\t\tresult := Format(diff)\n\t\t\tif result != tc.expected {\n\t\t\t\tt.Errorf(\"Expected: %s, got: %s\", tc.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/onbloc/diff\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Pxk1pNdYJ814DgeoYHewPGgrzXCRMbn0R0c9wKanW1RVrrkPlSU/sJPKIzspQVmymvC2dMVvI9c2ln8k8IOzCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"nestedpkg","path":"gno.land/p/demo/nestedpkg","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/nestedpkg\"\ngno = \"0.9\"\n"},{"name":"nestedpkg.gno","body":"// Package nestedpkg provides helpers for package-path based access control.\n// It is useful for upgrade patterns relying on namespaces.\n//\n// SECURITY: every exported helper takes `rlm realm` and reads both\n// `rlm.PkgPath()` and `rlm.Previous().PkgPath()` to make an\n// authorization decision. To close Class-2 designation forgery (a\n// hostile realm stashes a captured realm value and passes it back to\n// spoof identity), every helper gates on `rlm.IsCurrent()` first. The\n// Is* predicates return false on stale rlm (fail-closed); the Assert*\n// helpers panic. See docs/resources/gno-security.md.\npackage nestedpkg\n\nimport \"strings\"\n\n// IsCallerSubPath checks if the caller realm is located in a subfolder of the current realm.\nfunc IsCallerSubPath(_ int, rlm realm) bool {\n\tif !rlm.IsCurrent() {\n\t\treturn false\n\t}\n\tvar (\n\t\tcurPath  = rlm.PkgPath() + \"/\"\n\t\tprevPath = rlm.Previous().PkgPath() + \"/\"\n\t)\n\treturn strings.HasPrefix(prevPath, curPath)\n}\n\n// AssertCallerIsSubPath panics if IsCallerSubPath returns false.\nfunc AssertCallerIsSubPath(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tvar (\n\t\tcurPath  = rlm.PkgPath() + \"/\"\n\t\tprevPath = rlm.Previous().PkgPath() + \"/\"\n\t)\n\tif !strings.HasPrefix(prevPath, curPath) {\n\t\tpanic(\"call restricted to nested packages. current realm is \" + curPath + \", previous realm is \" + prevPath)\n\t}\n}\n\n// IsCallerParentPath checks if the caller realm is located in a parent location of the current realm.\nfunc IsCallerParentPath(_ int, rlm realm) bool {\n\tif !rlm.IsCurrent() {\n\t\treturn false\n\t}\n\tvar (\n\t\tcurPath  = rlm.PkgPath() + \"/\"\n\t\tprevPath = rlm.Previous().PkgPath() + \"/\"\n\t)\n\treturn strings.HasPrefix(curPath, prevPath)\n}\n\n// AssertCallerIsParentPath panics if IsCallerParentPath returns false.\nfunc AssertCallerIsParentPath(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tvar (\n\t\tcurPath  = rlm.PkgPath() + \"/\"\n\t\tprevPath = rlm.Previous().PkgPath() + \"/\"\n\t)\n\tif !strings.HasPrefix(curPath, prevPath) {\n\t\tpanic(\"call restricted to parent packages. current realm is \" + curPath + \", previous realm is \" + prevPath)\n\t}\n}\n\n// IsSameNamespace checks if the caller realm and the current realm are in the same namespace.\nfunc IsSameNamespace(_ int, rlm realm) bool {\n\tif !rlm.IsCurrent() {\n\t\treturn false\n\t}\n\tvar (\n\t\tcurNs  = nsFromPath(rlm.PkgPath()) + \"/\"\n\t\tprevNs = nsFromPath(rlm.Previous().PkgPath()) + \"/\"\n\t)\n\treturn curNs == prevNs\n}\n\n// AssertIsSameNamespace panics if IsSameNamespace returns false.\nfunc AssertIsSameNamespace(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tvar (\n\t\tcurNs  = nsFromPath(rlm.PkgPath()) + \"/\"\n\t\tprevNs = nsFromPath(rlm.Previous().PkgPath()) + \"/\"\n\t)\n\tif curNs != prevNs {\n\t\tpanic(\"call restricted to packages from the same namespace. current realm is \" + curNs + \", previous realm is \" + prevNs)\n\t}\n}\n\n// nsFromPath extracts the namespace from a package path.\nfunc nsFromPath(pkgpath string) string {\n\tparts := strings.Split(pkgpath, \"/\")\n\n\t// Specifically for gno.land, potential paths are in the form of DOMAIN/r/NAMESPACE/...\n\t// XXX: Consider extra checks.\n\t// XXX: Support non gno.land domains, where p/ and r/ won't be enforced.\n\tif len(parts) \u003e= 3 {\n\t\treturn parts[2]\n\t}\n\treturn \"\"\n}\n\n// XXX: Consider adding IsCallerDirectlySubPath\n// XXX: Consider adding IsCallerDirectlyParentPath\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"cXGtPo5/Z7PESWzBGmBjTwfOVyXMELRL73d6JMbYy4s2lWDSb5skIC2qS9/wnQk/rhEsUOvsm8WSKJefCpugAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"testutils","path":"gno.land/p/nt/testutils/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# testutils\n\nPackage `testutils` provides testing utilities for Gno packages and realms, including test address generation and cryptographic helpers.\n"},{"name":"access.gno","body":"package testutils\n\n// for testing access. see tests/files/access*.go\n\n// NOTE: non-package variables cannot be overridden, except during init().\nvar (\n\tTestVar1 int\n\ttestVar2 int\n)\n\nfunc init() {\n\tTestVar1 = 123\n\ttestVar2 = 456\n}\n\ntype TestAccessStruct struct {\n\tPublicField  string\n\tprivateField string\n}\n\nfunc (tas TestAccessStruct) PublicMethod() string {\n\treturn tas.PublicField + \"/\" + tas.privateField\n}\n\nfunc (tas TestAccessStruct) privateMethod() string {\n\treturn tas.PublicField + \"/\" + tas.privateField\n}\n\nfunc NewTestAccessStruct(pub, priv string) TestAccessStruct {\n\treturn TestAccessStruct{\n\t\tPublicField:  pub,\n\t\tprivateField: priv,\n\t}\n}\n\n// see access6.g0 etc.\ntype PrivateInterface interface {\n\tprivateMethod() string\n}\n\nfunc PrintPrivateInterface(pi PrivateInterface) {\n\tprintln(\"testutils.PrintPrivateInterface\", pi.privateMethod())\n}\n"},{"name":"crypto.gno","body":"package testutils\n\nimport \"crypto/bech32\"\n\nfunc TestAddress(name string) address {\n\tif len(name) \u003e 20 {\n\t\tpanic(\"address name cannot be greater than 20 bytes\")\n\t}\n\taddr := []byte(\"____________________\")\n\tcopy(addr[:], name)\n\tconverted, err := bech32.ConvertBits(addr, 8, 5, true)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tenc, err := bech32.Encode(\"g\", converted)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn address(enc)\n}\n"},{"name":"crypto_test.gno","body":"package testutils\n\nimport (\n\t\"testing\"\n)\n\nfunc TestTestAddress(t *testing.T) {\n\ttestAddr := TestAddress(\"author1\")\n\tif string(testAddr) != \"g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6\" {\n\t\tpanic(\"not equal\")\n\t}\n}\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package testutils provides testing utilities for Gno packages and realms.\npackage testutils\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/testutils/v0\"\ngno = \"0.9\"\n"},{"name":"misc.gno","body":"package testutils\n\n// WrapCall adds a frame to the call stack, for testing call stack depth.\nfunc WrapCall(fn func()) {\n\tfn()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"eC3QFAzcFZuA/p1E4xz2ioLvDpoI3Ub5dormYCi2ujMmaAiXVkDtDMn4WUoPn5FSxIMtd2PgcV0V3pLb86RhDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"subtests","path":"gno.land/r/tests/vm/subtests","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/subtests\"\ngno = \"0.9\"\n"},{"name":"subtests.gno","body":"package subtests\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n)\n\nfunc GetCurrentRealm(cur realm) runtime.Realm {\n\treturn unsafe.CurrentRealm()\n}\n\nfunc GetPreviousRealm(cur realm) runtime.Realm {\n\treturn unsafe.PreviousRealm()\n}\n\nfunc Exec(fn func()) {\n\tfn()\n}\n\nfunc CallAssertOriginCall(cur realm) {\n\truntime.AssertOriginCall()\n}\n\nfunc CallIsOriginCall(cur realm) bool {\n\treturn unsafe.PreviousRealm().IsUser()\n}\n\nfunc BankerOriginSend(cur realm) string {\n\treturn unsafe.OriginSend().String()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"f2yEUlxifUOtsaQmAPBizZjzjFI8ao7CMJKSkIjXL/BFh2ykkPLF8+uubxWF8u4yHRvyTDUEkZPlitnYf5DeAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"vm","path":"gno.land/r/tests/vm","files":[{"name":"README.md","body":"Modules here are only useful for file realm tests.\nThey can be safely ignored for other purposes.\n"},{"name":"exploit.gno","body":"package vm\n\nvar MyFoo *Foo\n\ntype Foo struct {\n\tA int\n\tB *Foo\n}\n\n// method to mutate\n\nfunc (f *Foo) UpdateFoo(x int) {\n\tf.A = x\n}\n\nfunc init() {\n\tMyFoo = \u0026Foo{\n\t\tA: 1,\n\t\tB: \u0026Foo{\n\t\t\tA: 2,\n\t\t},\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm\"\ngno = \"0.9\"\n"},{"name":"interfaces.gno","body":"package vm\n\nimport (\n\t\"strconv\"\n)\n\ntype Stringer interface {\n\tString() string\n}\n\nvar stringers []Stringer\n\nfunc AddStringer(cur realm, str Stringer) {\n\t// NOTE: this is ridiculous, a slice that will become too long\n\t// eventually.  Don't do this in production programs; use\n\t// gno.land/p/nt/avl/v0 or similar structures.\n\tstringers = append(stringers, str)\n}\n\nfunc Render(path string) string {\n\tres := \"\"\n\t// NOTE: like the function above, this function too will eventually\n\t// become too expensive to call.\n\tfor i, stringer := range stringers {\n\t\tres += strconv.Itoa(i) + \": \" + stringer.String() + \"\\n\"\n\t}\n\treturn res\n}\n"},{"name":"nestedpkg_test.gno","body":"package vm\n\nimport (\n\t\"testing\"\n)\n\nfunc TestNestedPkg(cur realm, t *testing.T) {\n\t// direct child\n\tcurPath := \"gno.land/r/tests/vm/foo\"\n\ttesting.SetRealm(testing.NewCodeRealm(curPath))\n\tif !IsCallerSubPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should be a sub path\")\n\t}\n\tif IsCallerParentPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a parent path\")\n\t}\n\tif !HasCallerSameNamespace(cross(cur)) {\n\t\tt.Errorf(curPath + \" should be from the same namespace\")\n\t}\n\n\t// grand-grand-child\n\tcurPath = \"gno.land/r/tests/vm/foo/bar/baz\"\n\ttesting.SetRealm(testing.NewCodeRealm(curPath))\n\tif !IsCallerSubPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should be a sub path\")\n\t}\n\tif IsCallerParentPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a parent path\")\n\t}\n\tif !HasCallerSameNamespace(cross(cur)) {\n\t\tt.Errorf(curPath + \" should be from the same namespace\")\n\t}\n\n\t// NOTE: This is now back in the gno.land/r/tests/vm structure,\n\t// so the direct parent test case is valid again.\n\n\t// direct parent (was previously fake parent)\n\tcurPath = \"gno.land/r/test\" // without the 's' at the end\n\ttesting.SetRealm(testing.NewCodeRealm(curPath))\n\tif IsCallerSubPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a sub path\")\n\t}\n\tif IsCallerParentPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a parent path\")\n\t}\n\tif HasCallerSameNamespace(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be from the same namespace\")\n\t}\n\n\t// fake parent (prefix)\n\tcurPath = \"gno.land/r/dem\"\n\ttesting.SetRealm(testing.NewCodeRealm(curPath))\n\tif IsCallerSubPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a sub path\")\n\t}\n\tif IsCallerParentPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a parent path\")\n\t}\n\tif HasCallerSameNamespace(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be from the same namespace\")\n\t}\n\n\t// different namespace\n\tcurPath = \"gno.land/r/foo\"\n\ttesting.SetRealm(testing.NewCodeRealm(curPath))\n\tif IsCallerSubPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a sub path\")\n\t}\n\tif IsCallerParentPath(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be a parent path\")\n\t}\n\tif HasCallerSameNamespace(cross(cur)) {\n\t\tt.Errorf(curPath + \" should not be from the same namespace\")\n\t}\n}\n"},{"name":"realm_compositelit.gno","body":"package vm\n\ntype (\n\tWord uint\n\tnat  []Word\n)\n\nvar zero = \u0026Int{\n\tneg: true,\n\tabs: []Word{0},\n}\n\n// structLit\ntype Int struct {\n\tneg bool\n\tabs nat\n}\n\nfunc GetZeroType() nat {\n\ta := zero.abs\n\treturn a\n}\n"},{"name":"realm_method38d.gno","body":"package vm\n\nvar abs nat\n\nfunc (n nat) Add() nat {\n\treturn []Word{0}\n}\n\nfunc GetAbs(cur realm) nat {\n\tabs = []Word{0}\n\treturn abs\n}\n\nfunc AbsAdd(cur realm) nat {\n\trt := GetAbs(cur).Add()\n\treturn rt\n}\n"},{"name":"tests.gno","body":"package vm\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/demo/nestedpkg\"\n\trsubtests \"gno.land/r/tests/vm/subtests\"\n)\n\nvar counter int\n\nfunc IncCounter(cur realm) {\n\tcounter++\n}\n\nfunc Counter(cur realm) int {\n\treturn counter\n}\n\nfunc CurrentRealmPath(cur realm) string {\n\treturn unsafe.CurrentRealm().PkgPath()\n}\n\nvar initOriginCaller = unsafe.OriginCaller()\n\nfunc InitOriginCaller(cur realm) address {\n\treturn initOriginCaller\n}\n\nfunc CallAssertOriginCall(cur realm) {\n\truntime.AssertOriginCall()\n}\n\nfunc CallIsOriginCall(cur realm) bool {\n\t// XXX: consider return !unsafe.PreviousRealm().IsCode()\n\treturn unsafe.PreviousRealm().IsUser()\n}\n\nfunc CallSubtestsAssertOriginCall(cur realm) {\n\trsubtests.CallAssertOriginCall(cross(cur))\n}\n\nfunc CallSubtestsIsOriginCall(cur realm) bool {\n\treturn rsubtests.CallIsOriginCall(cross(cur))\n}\n\n//----------------------------------------\n// Test structure to ensure cross-realm modification is prevented.\n\ntype TestRealmObject struct {\n\tField string\n}\n\nvar TestRealmObjectValue TestRealmObject\n\n// NewTestRealmObject returns a fresh heap-allocated TestRealmObject.\n// Non-crossing — relies on borrow rule #1 to set m.Realm = /r/tests/vm\n// inside the body so the composite literal passes checkConstructionTime.\nfunc NewTestRealmObject() *TestRealmObject {\n\treturn \u0026TestRealmObject{Field: \"initial\"}\n}\n\nfunc ModifyTestRealmObject(cur realm, t *TestRealmObject) {\n\tt.Field += \"_modified\"\n}\n\nfunc (t *TestRealmObject) Modify() {\n\tt.Field += \"_modified\"\n}\n\n//----------------------------------------\n// Test helpers to test a particular realm bug.\n\ntype TestNode struct {\n\tName  string\n\tChild *TestNode\n}\n\nvar (\n\tgTestNode1 *TestNode\n\tgTestNode2 *TestNode\n\tgTestNode3 *TestNode\n)\n\nfunc InitTestNodes(cur realm) {\n\tgTestNode1 = \u0026TestNode{Name: \"first\"}\n\tgTestNode2 = \u0026TestNode{Name: \"second\", Child: \u0026TestNode{Name: \"second's child\"}}\n}\n\nfunc ModTestNodes(cur realm) {\n\ttmp := \u0026TestNode{}\n\ttmp.Child = gTestNode2.Child\n\tgTestNode3 = tmp // set to new-real\n\t// gTestNode1 = tmp.Child // set back to original is-real\n\tgTestNode3 = nil // delete.\n}\n\nfunc PrintTestNodes() {\n\tprintln(gTestNode2.Child.Name)\n}\n\nfunc GetPreviousRealm(cur realm) runtime.Realm {\n\treturn unsafe.PreviousRealm()\n}\n\nfunc GetRSubtestsPreviousRealm(cur realm) runtime.Realm {\n\treturn rsubtests.GetPreviousRealm(cross(cur))\n}\n\nfunc Exec(fn func()) {\n\t// no realm switching.\n\tfn()\n}\n\n// ExecRlm mirrors Exec but threads the caller's rlm into the callback\n// so the callback can use `cross(rlm)` instead of bare `cross`.\nfunc ExecRlm(_ int, rlm realm, fn func(_ int, rlm realm)) {\n\tfn(0, rlm)\n}\n\nfunc ExecSwitch(cur realm, fn func()) {\n\tfn()\n}\n\n// ExecSwitchRlm is the rlm-threaded variant of ExecSwitch — crosses\n// into this realm and passes the callee's cur to the callback so\n// the callback can `cross(rlm)` against the switched realm.\nfunc ExecSwitchRlm(cur realm, fn func(_ int, rlm realm)) {\n\tfn(0, cur)\n}\n\nfunc IsCallerSubPath(cur realm) bool {\n\treturn nestedpkg.IsCallerSubPath(0, cur)\n}\n\nfunc IsCallerParentPath(cur realm) bool {\n\treturn nestedpkg.IsCallerParentPath(0, cur)\n}\n\nfunc HasCallerSameNamespace(cur realm) bool {\n\treturn nestedpkg.IsSameNamespace(0, cur)\n}\n\nfunc BankerOriginSend(cur realm) string {\n\treturn unsafe.OriginSend().String()\n}\n\nfunc RTestsOriginSend(cur realm) string {\n\treturn rsubtests.BankerOriginSend(cross(cur))\n}\n"},{"name":"tests_test.gno","body":"package vm_test\n\nimport (\n\t\"chain\"\n\t\"fmt\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\ttests \"gno.land/r/tests/vm\"\n)\n\nfunc TestAssertOriginCall(cur realm, t *testing.T) {\n\t// CallAssertOriginCall(): no panic\n\tcaller := testutils.TestAddress(\"caller\")\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\ttests.CallAssertOriginCall(cross(cur))\n\tif !tests.CallIsOriginCall(cross(cur)) {\n\t\tt.Errorf(\"expected IsOriginCall=true but got false\")\n\t}\n\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/tests/vm\"))\n\t// CallAssertOriginCall() from a block: abort\n\tr := revive(func() {\n\t\t// if called inside a function literal, this is no longer an origin call\n\t\t// because there's one additional frame (the function literal block).\n\t\tif tests.CallIsOriginCall(cross(cur)) {\n\t\t\tt.Errorf(\"expected IsOriginCall=false but got true\")\n\t\t}\n\t\ttests.CallAssertOriginCall(cross(cur)) // \u003c---\n\t})\n\tif fmt.Sprintf(\"%v\", r) != \"invalid non-origin call\" {\n\t\tt.Error(\"expected abort but did not\")\n\t}\n\t// CallSubtestsAssertOriginCall(): abort\n\tr = revive(func() {\n\t\t// if called inside a function literal, this is no longer an origin call\n\t\t// because there's one additional frame (the function literal block).\n\t\tif tests.CallSubtestsIsOriginCall(cross(cur)) {\n\t\t\tt.Errorf(\"expected IsOriginCall=false but got true\")\n\t\t}\n\t\ttests.CallSubtestsAssertOriginCall(cross(cur))\n\t})\n\tif fmt.Sprintf(\"%v\", r) != \"invalid non-origin call\" {\n\t\tt.Error(\"expected abort but did not\")\n\t}\n}\n\nfunc TestPreviousRealm(cur realm, t *testing.T) {\n\tvar (\n\t\tfirstRealm = chain.PackageAddress(\"gno.land/r/tests/vm_test\")\n\t\trTestsAddr = chain.PackageAddress(\"gno.land/r/tests/vm\")\n\t)\n\t// When only one realm in the frames, PreviousRealm returns the same realm\n\tif addr := tests.GetPreviousRealm(cross(cur)).Address(); addr != firstRealm {\n\t\tprintln(tests.GetPreviousRealm(cross(cur)))\n\t\tt.Errorf(\"want GetPreviousRealm().Address==%s, got %s\", firstRealm, addr)\n\t}\n\t// When 2 or more realms in the frames, PreviousRealm returns the second to last\n\tif addr := tests.GetRSubtestsPreviousRealm(cross(cur)).Address(); addr != rTestsAddr {\n\t\tt.Errorf(\"want GetRSubtestsPreviousRealm().Address==%s, got %s\", rTestsAddr, addr)\n\t}\n}\n"},{"name":"z0_filetest.gno","body":"// PKGPATH: gno.land/r/tests/vm/filetests/z0_filetest\n\npackage z0_filetest\n\nimport (\n\ttests \"gno.land/r/tests/vm\"\n)\n\nfunc main(cur realm) {\n\tprintln(\"tests.CallIsOriginCall:\", tests.CallIsOriginCall(cross(cur)))\n\ttests.CallAssertOriginCall(cross(cur))\n\tprintln(\"tests.CallAssertOriginCall doesn't panic when called directly\")\n\n\t{\n\t\t// if called inside a block, this is no longer an origin call because\n\t\t// there's one additional frame (the block).\n\t\tprintln(\"tests.CallIsOriginCall:\", tests.CallIsOriginCall(cross(cur)))\n\t\tdefer func() {\n\t\t\tr := recover()\n\t\t\tprintln(\"tests.AssertOriginCall panics if when called inside a function literal:\", r)\n\t\t}()\n\t\ttests.CallAssertOriginCall(cross(cur))\n\t}\n}\n\n// Output:\n// tests.CallIsOriginCall: false\n// tests.CallAssertOriginCall doesn't panic when called directly\n// tests.CallIsOriginCall: false\n// tests.AssertOriginCall panics if when called inside a function literal: undefined\n"},{"name":"z1_filetest.gno","body":"// PKGPATH: gno.land/r/tests/vm/filetests/z1_filetest\n\npackage z1_filetest\n\nimport (\n\ttests \"gno.land/r/tests/vm\"\n)\n\nfunc main(cur realm) {\n\tprintln(tests.Counter(cross(cur)))\n\ttests.IncCounter(cross(cur))\n\tprintln(tests.Counter(cross(cur)))\n}\n\n// Output:\n// 0\n// 1\n"},{"name":"z2_filetest.gno","body":"// PKGPATH: gno.land/r/tests/vm/filetests/z2_filetest\n\npackage z2_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\ttests \"gno.land/r/tests/vm\"\n)\n\n// When a single realm in the frames, PreviousRealm returns the user\n// When 2 or more realms in the frames, PreviousRealm returns the second to last\nfunc main(cur realm) {\n\tvar (\n\t\teoa = testutils.TestAddress(\"someone\")\n\t\t_   = chain.PackageAddress(\"gno.land/r/tests/vm\")\n\t)\n\ttesting.SetOriginCaller(eoa)\n\tprintln(\"tests.GetPreviousRealm().Address(): \", tests.GetPreviousRealm(cross(cur)).Address())\n\tprintln(\"tests.GetRSubtestsPreviousRealm().Address(): \", tests.GetRSubtestsPreviousRealm(cross(cur)).Address())\n}\n\n// Output:\n// tests.GetPreviousRealm().Address():  g1wdhk6et0dej47h6lta047h6lta047h6lrnerlk\n// tests.GetRSubtestsPreviousRealm().Address():  g1dhh6vhw9f5lmmpfz52rkf5dsk8lqzmad3fmpw7\n"},{"name":"z3_filetest.gno","body":"// PKGPATH: gno.land/r/tests_z3\npackage tests_z3\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/tests/vm\"\n)\n\nfunc main(cur realm) {\n\tvar (\n\t\teoa        = testutils.TestAddress(\"someone\")\n\t\trTestsAddr = chain.PackageAddress(\"gno.land/r/tests/vm\")\n\t)\n\ttesting.SetOriginCaller(eoa)\n\t// Contrarily to z2_filetest.gno we EXPECT GetPreviousRealms != eoa (#1704)\n\tif addr := vm.GetPreviousRealm(cross(cur)).Address(); addr != eoa {\n\t\tprintln(\"want vm.GetPreviousRealm().Address ==\", eoa, \"got\", addr)\n\t}\n\t// When 2 or more realms in the frames, it is also different\n\tif addr := vm.GetRSubtestsPreviousRealm(cross(cur)).Address(); addr != rTestsAddr {\n\t\tprintln(\"want GetRSubtestsPreviousRealm().Address ==\", rTestsAddr, \"got\", addr)\n\t}\n\tprintln(\"Done.\")\n}\n\n// Output:\n// Done.\n"},{"name":"z4_filetest.gno","body":"// PKGPATH: gno.land/r/tests/vm/z4_filetest\n\npackage z4_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\ttests \"gno.land/r/tests/vm\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetOriginSend(chain.Coins{{Denom: \"foo\", Amount: 42}, {Denom: \"ugnot\", Amount: 100}})\n\tprintln(\"tests.BankerOriginSend: \" + tests.BankerOriginSend(cross(cur)))\n\n\t// unsafe.OriginSend() always returns the origin send regardless of call\n\t// depth.\n\tprintln(\"tests.RTestsOriginSend: \" + tests.RTestsOriginSend(cross(cur)))\n}\n\n// Output:\n// tests.BankerOriginSend: 42foo,100ugnot\n// tests.RTestsOriginSend: 42foo,100ugnot\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"M1G29viujJkAXkKlQPJpgEM14p5AUaIO5pQ1QBOeG+F9cn6m2AenCKY7GUczFwlQ01eUNShs0rRN8zwgXbcsDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"uassert","path":"gno.land/p/nt/uassert/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# uassert\n\nPackage `uassert` provides assertion helpers for testing Gno packages and realms.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\npackage uassert // import \"gno.land/p/nt/uassert/v0\"\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/uassert/v0\"\ngno = \"0.9\"\n"},{"name":"helpers.gno","body":"package uassert\n\nimport \"strings\"\n\nfunc fail(t TestingT, customMsgs []string, failureMessage string, args ...any) bool {\n\tcustomMsg := \"\"\n\tif len(customMsgs) \u003e 0 {\n\t\tcustomMsg = strings.Join(customMsgs, \" \")\n\t}\n\tif customMsg != \"\" {\n\t\tfailureMessage += \" - \" + customMsg\n\t}\n\tt.Errorf(failureMessage, args...)\n\treturn false\n}\n\nfunc checkDidPanic(f any, rlm realm) (didPanic bool, message string) {\n\tdidPanic = true\n\tdefer func() {\n\t\tr := recover()\n\n\t\tif r == nil {\n\t\t\tmessage = \"nil\"\n\t\t\treturn\n\t\t}\n\n\t\terr, ok := r.(error)\n\t\tif ok {\n\t\t\tmessage = err.Error()\n\t\t\treturn\n\t\t}\n\n\t\terrStr, ok := r.(string)\n\t\tif ok {\n\t\t\tmessage = errStr\n\t\t\treturn\n\t\t}\n\n\t\tmessage = \"recover: unsupported type\"\n\t}()\n\tswitch f := f.(type) {\n\tcase func():\n\t\tf()\n\tcase func(realm):\n\t\tf(cross(rlm))\n\tdefault:\n\t\tpanic(\"f must be of type func() or func(realm)\")\n\t}\n\tdidPanic = false\n\treturn\n}\n"},{"name":"mock_test.gno","body":"package uassert_test\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\ntype mockTestingT struct {\n\tfmt  string\n\targs []any\n}\n\n// --- interface mock\n\nvar _ uassert.TestingT = (*mockTestingT)(nil)\n\nfunc (mockT *mockTestingT) Helper()                      { /* noop */ }\nfunc (mockT *mockTestingT) Skip(args ...any)             { /* not implmented */ }\nfunc (mockT *mockTestingT) Fail()                        { /* not implmented */ }\nfunc (mockT *mockTestingT) FailNow()                     { /* not implmented */ }\nfunc (mockT *mockTestingT) Logf(fmt string, args ...any) { /* noop */ }\n\nfunc (mockT *mockTestingT) Fatalf(fmt string, args ...any) {\n\tmockT.fmt = \"fatal: \" + fmt\n\tmockT.args = args\n}\n\nfunc (mockT *mockTestingT) Errorf(fmt string, args ...any) {\n\tmockT.fmt = \"error: \" + fmt\n\tmockT.args = args\n}\n\n// --- helpers\n\nfunc (mockT *mockTestingT) actualString() string {\n\tres := fmt.Sprintf(mockT.fmt, mockT.args...)\n\tmockT.reset()\n\treturn res\n}\n\nfunc (mockT *mockTestingT) reset() {\n\tmockT.fmt = \"\"\n\tmockT.args = nil\n}\n\nfunc (mockT *mockTestingT) equals(t *testing.T, expected string) {\n\tactual := mockT.actualString()\n\n\tif expected != actual {\n\t\tt.Errorf(\"mockT differs:\\n- expected: %s\\n- actual:   %s\\n\", expected, actual)\n\t}\n}\n\nfunc (mockT *mockTestingT) empty(t *testing.T) {\n\tif mockT.fmt != \"\" || mockT.args != nil {\n\t\tactual := mockT.actualString()\n\t\tt.Errorf(\"mockT should be empty, got %s\", actual)\n\t}\n}\n"},{"name":"types.gno","body":"package uassert\n\ntype TestingT interface {\n\tHelper()\n\tSkip(args ...any)\n\tFatalf(fmt string, args ...any)\n\tErrorf(fmt string, args ...any)\n\tLogf(fmt string, args ...any)\n\tFail()\n\tFailNow()\n}\n"},{"name":"uassert.gno","body":"// uassert is an adapted lighter version of https://github.com/stretchr/testify/assert.\npackage uassert\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/onbloc/diff\"\n)\n\n// NoError asserts that a function returned no error (i.e. `nil`).\nfunc NoError(t TestingT, err error, msgs ...string) bool {\n\tt.Helper()\n\tif err != nil {\n\t\treturn fail(t, msgs, \"unexpected error: %s\", err.Error())\n\t}\n\treturn true\n}\n\n// Error asserts that a function returned an error (i.e. not `nil`).\nfunc Error(t TestingT, err error, msgs ...string) bool {\n\tt.Helper()\n\tif err == nil {\n\t\treturn fail(t, msgs, \"an error is expected but got nil\")\n\t}\n\treturn true\n}\n\n// ErrorContains asserts that a function returned an error (i.e. not `nil`)\n// and that the error contains the specified substring.\nfunc ErrorContains(t TestingT, err error, contains string, msgs ...string) bool {\n\tt.Helper()\n\n\tif !Error(t, err, msgs...) {\n\t\treturn false\n\t}\n\n\tactual := err.Error()\n\tif !strings.Contains(actual, contains) {\n\t\treturn fail(t, msgs, \"error %q does not contain %q\", actual, contains)\n\t}\n\n\treturn true\n}\n\n// True asserts that the specified value is true.\nfunc True(t TestingT, value bool, msgs ...string) bool {\n\tt.Helper()\n\tif !value {\n\t\treturn fail(t, msgs, \"should be true\")\n\t}\n\treturn true\n}\n\n// False asserts that the specified value is false.\nfunc False(t TestingT, value bool, msgs ...string) bool {\n\tt.Helper()\n\tif value {\n\t\treturn fail(t, msgs, \"should be false\")\n\t}\n\treturn true\n}\n\n// ErrorIs asserts the given error matches the target error using errors.Is,\n// which traverses the error chain looking for a match.\nfunc ErrorIs(t TestingT, err, target error, msgs ...string) bool {\n\tt.Helper()\n\n\tif !errors.Is(err, target) {\n\t\treturn fail(t, msgs, \"error mismatch, expected %s, got %s\", target, err)\n\t}\n\n\treturn true\n}\n\n// AbortsWithMessage asserts that the code inside the specified func aborts\n// (panics when crossing another realm).\n// Use PanicsWithMessage for asserting local panics within the same realm.\n//\n// `rlm` is threaded into the callback via cross(rlm) when f is func(realm).\n// It is ignored for func() callbacks. /p/ production code cannot declare\n// crossing functions, so rlm is taken as the second (non-first) parameter\n// — callers pass `cur` directly.\n//\n// NOTE: This relies on gno's `revive` mechanism to catch aborts.\nfunc AbortsWithMessage(t TestingT, rlm realm, msg string, f any, msgs ...string) bool {\n\tt.Helper()\n\n\tvar didAbort bool\n\tvar abortValue any\n\tvar r any\n\n\tswitch f := f.(type) {\n\tcase func():\n\t\tr = revive(f) // revive() captures the value passed to panic()\n\tcase func(realm):\n\t\tr = revive(func() { f(cross(rlm)) })\n\tdefault:\n\t\tpanic(\"f must be of type func() or func(realm)\")\n\t}\n\tif r != nil {\n\t\tdidAbort = true\n\t\tabortValue = r\n\t}\n\n\tif !didAbort {\n\t\t// If the function didn't abort as expected\n\t\treturn fail(t, msgs, \"func should abort\")\n\t}\n\n\t// Check if the abort value matches the expected message string\n\tabortStr := ufmt.Sprintf(\"%v\", abortValue)\n\tif abortStr != msg {\n\t\treturn fail(t, msgs, \"func should abort with message:\\t%q\\n\\tActual abort value:\\t%q\", msg, abortStr)\n\t}\n\n\t// Success: function aborted with the expected message\n\treturn true\n}\n\n// AbortsContains asserts that the code inside the specified func aborts\n// (panics when crossing another realm) and the abort message contains the specified substring.\n// See AbortsWithMessage for `rlm` semantics.\nfunc AbortsContains(t TestingT, rlm realm, substr string, f any, msgs ...string) bool {\n\tt.Helper()\n\n\tvar didAbort bool\n\tvar abortValue any\n\tvar r any\n\n\tif fn, ok := f.(func()); ok {\n\t\tr = revive(fn)\n\t} else if fn, ok := f.(func(realm)); ok {\n\t\tr = revive(func() { fn(cross(rlm)) })\n\t} else {\n\t\tpanic(\"f must be of type func() or func(realm)\")\n\t}\n\tif r != nil {\n\t\tdidAbort = true\n\t\tabortValue = r\n\t}\n\n\tif !didAbort {\n\t\treturn fail(t, msgs, \"func should abort\")\n\t}\n\n\tabortStr := ufmt.Sprintf(\"%v\", abortValue)\n\tif !strings.Contains(abortStr, substr) {\n\t\treturn fail(t, msgs, \"func should abort with message containing:\\t%q\\n\\tActual abort value:\\t%q\", substr, abortStr)\n\t}\n\n\treturn true\n}\n\n// NotAborts asserts that the code inside the specified func does NOT abort\n// when crossing an execution boundary.\n// Note: Consider using NotPanics which checks for both panics and aborts.\n// See AbortsWithMessage for `rlm` semantics.\nfunc NotAborts(t TestingT, rlm realm, f any, msgs ...string) bool {\n\tt.Helper()\n\n\tvar didAbort bool\n\tvar abortValue any\n\tvar r any\n\n\tswitch f := f.(type) {\n\tcase func():\n\t\tr = revive(f) // revive() captures the value passed to panic()\n\tcase func(realm):\n\t\tr = revive(func() { f(cross(rlm)) })\n\tdefault:\n\t\tpanic(\"f must be of type func() or func(realm)\")\n\t}\n\tif r != nil {\n\t\tdidAbort = true\n\t\tabortValue = r\n\t}\n\n\tif didAbort {\n\t\t// Fail if the function aborted when it shouldn't have\n\t\t// Attempt to format the abort value in the error message\n\t\treturn fail(t, msgs, \"func should not abort\\\\n\\\\tAbort value:\\\\t%v\", abortValue)\n\t}\n\n\t// Success: function did not abort\n\treturn true\n}\n\n// PanicsWithMessage asserts that the code inside the specified func panics\n// locally within the same execution realm.\n// Use AbortsWithMessage for asserting panics that cross execution boundaries (aborts).\n// See AbortsWithMessage for `rlm` semantics.\nfunc PanicsWithMessage(t TestingT, rlm realm, msg string, f any, msgs ...string) bool {\n\tt.Helper()\n\n\tdidPanic, panicValue := checkDidPanic(f, rlm)\n\tif !didPanic {\n\t\treturn fail(t, msgs, \"func should panic\\n\\tPanic value:\\t%v\", panicValue)\n\t}\n\n\t// Check if the abort value matches the expected message string\n\tpanicStr := ufmt.Sprintf(\"%v\", panicValue)\n\tif panicStr != msg {\n\t\treturn fail(t, msgs, \"func should panic with message:\\t%q\\n\\tActual panic value:\\t%q\", msg, panicStr)\n\t}\n\treturn true\n}\n\n// PanicsContains asserts that the code inside the specified func panics\n// locally within the same execution realm and the panic message contains the specified substring.\n// See AbortsWithMessage for `rlm` semantics.\nfunc PanicsContains(t TestingT, rlm realm, substr string, f any, msgs ...string) bool {\n\tt.Helper()\n\n\tdidPanic, panicValue := checkDidPanic(f, rlm)\n\tif !didPanic {\n\t\treturn fail(t, msgs, \"func should panic\\n\\tPanic value:\\t%v\", panicValue)\n\t}\n\n\tpanicStr := ufmt.Sprintf(\"%v\", panicValue)\n\tif !strings.Contains(panicStr, substr) {\n\t\treturn fail(t, msgs, \"func should panic with message containing:\\t%q\\n\\tActual panic value:\\t%q\", substr, panicStr)\n\t}\n\treturn true\n}\n\n// NotPanics asserts that the code inside the specified func does NOT panic\n// (within the same realm) or abort (due to a cross-realm panic).\n// See AbortsWithMessage for `rlm` semantics.\nfunc NotPanics(t TestingT, rlm realm, f any, msgs ...string) bool {\n\tt.Helper()\n\n\tvar panicVal any\n\tvar didPanic bool\n\tvar abortVal any\n\n\t// Use revive to catch cross-realm aborts\n\tabortVal = revive(func() {\n\t\t// Use defer+recover to catch same-realm panics\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tdidPanic = true\n\t\t\t\tpanicVal = r\n\t\t\t}\n\t\t}()\n\t\t// Execute the function\n\t\tswitch f := f.(type) {\n\t\tcase func():\n\t\t\tf()\n\t\tcase func(realm):\n\t\t\tf(cross(rlm))\n\t\tdefault:\n\t\t\tpanic(\"f must be of type func() or func(realm)\")\n\t\t}\n\t})\n\n\t// Check if revive caught an abort\n\tif abortVal != nil {\n\t\treturn fail(t, msgs, \"func should not abort\\n\\tAbort value:\\t%+v\", abortVal)\n\t}\n\n\t// Check if recover caught a panic\n\tif didPanic {\n\t\t// Format panic value for message\n\t\tpanicMsg := \"\"\n\t\tif panicVal == nil {\n\t\t\tpanicMsg = \"nil\"\n\t\t} else if err, ok := panicVal.(error); ok {\n\t\t\tpanicMsg = err.Error()\n\t\t} else if str, ok := panicVal.(string); ok {\n\t\t\tpanicMsg = str\n\t\t} else {\n\t\t\t// Fallback for other types\n\t\t\tpanicMsg = \"panic: unsupported type\"\n\t\t}\n\t\treturn fail(t, msgs, \"func should not panic\\n\\tPanic value:\\t%s\", panicMsg)\n\t}\n\n\treturn true // No panic or abort occurred\n}\n\n// Equal asserts that two objects are equal.\nfunc Equal(t TestingT, expected, actual any, msgs ...string) bool {\n\tt.Helper()\n\n\tif expected == nil || actual == nil {\n\t\treturn expected == actual\n\t}\n\n\t// XXX: errors\n\t// XXX: slices\n\t// XXX: pointers\n\n\tequal := false\n\tok_ := false\n\tes, as := \"unsupported type\", \"unsupported type\"\n\n\tswitch ev := expected.(type) {\n\tcase string:\n\t\tif av, ok := actual.(string); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = ev, av\n\t\t\tif !equal {\n\t\t\t\tdif := diff.MyersDiff(ev, av)\n\t\t\t\treturn fail(t, msgs, \"uassert.Equal: strings are different\\n\\tDiff: %s\", diff.Format(dif))\n\t\t\t}\n\t\t}\n\tcase address:\n\t\tif av, ok := actual.(address); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = string(ev), string(av)\n\t\t}\n\tcase int:\n\t\tif av, ok := actual.(int); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(ev), strconv.Itoa(av)\n\t\t}\n\tcase int8:\n\t\tif av, ok := actual.(int8); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase int16:\n\t\tif av, ok := actual.(int16); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase int32:\n\t\tif av, ok := actual.(int32); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase int64:\n\t\tif av, ok := actual.(int64); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase uint:\n\t\tif av, ok := actual.(uint); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint8:\n\t\tif av, ok := actual.(uint8); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint16:\n\t\tif av, ok := actual.(uint16); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint32:\n\t\tif av, ok := actual.(uint32); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint64:\n\t\tif av, ok := actual.(uint64); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(ev, 10), strconv.FormatUint(av, 10)\n\t\t}\n\tcase bool:\n\t\tif av, ok := actual.(bool); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t\tif ev {\n\t\t\t\tes, as = \"true\", \"false\"\n\t\t\t} else {\n\t\t\t\tes, as = \"false\", \"true\"\n\t\t\t}\n\t\t}\n\tcase float32:\n\t\tif av, ok := actual.(float32); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t}\n\tcase float64:\n\t\tif av, ok := actual.(float64); ok {\n\t\t\tequal = ev == av\n\t\t\tok_ = true\n\t\t}\n\tdefault:\n\t\treturn fail(t, msgs, \"uassert.Equal: unsupported type\")\n\t}\n\n\t/*\n\t\t// XXX: implement stringer and other well known similar interfaces\n\t\ttype stringer interface{ String() string }\n\t\tif ev, ok := expected.(stringer); ok {\n\t\t\tif av, ok := actual.(stringer); ok {\n\t\t\t\tequal = ev.String() == av.String()\n\t\t\t\tok_ = true\n\t\t\t}\n\t\t}\n\t*/\n\n\tif !ok_ {\n\t\treturn fail(t, msgs, \"uassert.Equal: different types\") // XXX: display the types\n\t}\n\tif !equal {\n\t\treturn fail(t, msgs, \"uassert.Equal: same type but different value\\n\\texpected: %s\\n\\tactual:   %s\", es, as)\n\t}\n\n\treturn true\n}\n\n// NotEqual asserts that two objects are not equal.\nfunc NotEqual(t TestingT, expected, actual any, msgs ...string) bool {\n\tt.Helper()\n\n\tif expected == nil || actual == nil {\n\t\treturn expected != actual\n\t}\n\n\t// XXX: errors\n\t// XXX: slices\n\t// XXX: pointers\n\n\tnotEqual := false\n\tok_ := false\n\tes, as := \"unsupported type\", \"unsupported type\"\n\n\tswitch ev := expected.(type) {\n\tcase string:\n\t\tif av, ok := actual.(string); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = ev, av\n\t\t}\n\tcase address:\n\t\tif av, ok := actual.(address); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = string(ev), string(av)\n\t\t}\n\tcase int:\n\t\tif av, ok := actual.(int); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(ev), strconv.Itoa(av)\n\t\t}\n\tcase int8:\n\t\tif av, ok := actual.(int8); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase int16:\n\t\tif av, ok := actual.(int16); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase int32:\n\t\tif av, ok := actual.(int32); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase int64:\n\t\tif av, ok := actual.(int64); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.Itoa(int(ev)), strconv.Itoa(int(av))\n\t\t}\n\tcase uint:\n\t\tif av, ok := actual.(uint); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint8:\n\t\tif av, ok := actual.(uint8); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint16:\n\t\tif av, ok := actual.(uint16); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint32:\n\t\tif av, ok := actual.(uint32); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(uint64(ev), 10), strconv.FormatUint(uint64(av), 10)\n\t\t}\n\tcase uint64:\n\t\tif av, ok := actual.(uint64); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tes, as = strconv.FormatUint(ev, 10), strconv.FormatUint(av, 10)\n\t\t}\n\tcase bool:\n\t\tif av, ok := actual.(bool); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t\tif ev {\n\t\t\t\tes, as = \"true\", \"false\"\n\t\t\t} else {\n\t\t\t\tes, as = \"false\", \"true\"\n\t\t\t}\n\t\t}\n\tcase float32:\n\t\tif av, ok := actual.(float32); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t}\n\tcase float64:\n\t\tif av, ok := actual.(float64); ok {\n\t\t\tnotEqual = ev != av\n\t\t\tok_ = true\n\t\t}\n\tdefault:\n\t\treturn fail(t, msgs, \"uassert.NotEqual: unsupported type\")\n\t}\n\n\t/*\n\t\t// XXX: implement stringer and other well known similar interfaces\n\t\ttype stringer interface{ String() string }\n\t\tif ev, ok := expected.(stringer); ok {\n\t\t\tif av, ok := actual.(stringer); ok {\n\t\t\t\tnotEqual = ev.String() != av.String()\n\t\t\t\tok_ = true\n\t\t\t}\n\t\t}\n\t*/\n\n\tif !ok_ {\n\t\treturn fail(t, msgs, \"uassert.NotEqual: different types\") // XXX: display the types\n\t}\n\tif !notEqual {\n\t\treturn fail(t, msgs, \"uassert.NotEqual: same type and same value\\n\\texpected: %s\\n\\tactual:   %s\", es, as)\n\t}\n\n\treturn true\n}\n\nfunc isNumberEmpty(n any) (isNumber, isEmpty bool) {\n\tswitch n := n.(type) {\n\t// NOTE: the cases are split individually, so that n becomes of the\n\t// asserted type; the type of '0' was correctly inferred and converted\n\t// to the corresponding type, int, int8, etc.\n\tcase int:\n\t\treturn true, n == 0\n\tcase int8:\n\t\treturn true, n == 0\n\tcase int16:\n\t\treturn true, n == 0\n\tcase int32:\n\t\treturn true, n == 0\n\tcase int64:\n\t\treturn true, n == 0\n\tcase uint:\n\t\treturn true, n == 0\n\tcase uint8:\n\t\treturn true, n == 0\n\tcase uint16:\n\t\treturn true, n == 0\n\tcase uint32:\n\t\treturn true, n == 0\n\tcase uint64:\n\t\treturn true, n == 0\n\tcase float32:\n\t\treturn true, n == 0\n\tcase float64:\n\t\treturn true, n == 0\n\t}\n\treturn false, false\n}\n\nfunc Empty(t TestingT, obj any, msgs ...string) bool {\n\tt.Helper()\n\n\tisNumber, isEmpty := isNumberEmpty(obj)\n\tif isNumber {\n\t\tif !isEmpty {\n\t\t\treturn fail(t, msgs, \"uassert.Empty: not empty number: %d\", obj)\n\t\t}\n\t} else {\n\t\tswitch val := obj.(type) {\n\t\tcase string:\n\t\t\tif val != \"\" {\n\t\t\t\treturn fail(t, msgs, \"uassert.Empty: not empty string: %s\", val)\n\t\t\t}\n\t\tcase address:\n\t\t\tvar zeroAddr address\n\t\t\tif val != zeroAddr {\n\t\t\t\treturn fail(t, msgs, \"uassert.Empty: not empty address: %s\", string(val))\n\t\t\t}\n\t\tdefault:\n\t\t\treturn fail(t, msgs, \"uassert.Empty: unsupported type\")\n\t\t}\n\t}\n\treturn true\n}\n\nfunc NotEmpty(t TestingT, obj any, msgs ...string) bool {\n\tt.Helper()\n\tisNumber, isEmpty := isNumberEmpty(obj)\n\tif isNumber {\n\t\tif isEmpty {\n\t\t\treturn fail(t, msgs, \"uassert.NotEmpty: empty number: %d\", obj)\n\t\t}\n\t} else {\n\t\tswitch val := obj.(type) {\n\t\tcase string:\n\t\t\tif val == \"\" {\n\t\t\t\treturn fail(t, msgs, \"uassert.NotEmpty: empty string: %s\", val)\n\t\t\t}\n\t\tcase address:\n\t\t\tvar zeroAddr address\n\t\t\tif val == zeroAddr {\n\t\t\t\treturn fail(t, msgs, \"uassert.NotEmpty: empty address: %s\", string(val))\n\t\t\t}\n\t\tdefault:\n\t\t\treturn fail(t, msgs, \"uassert.NotEmpty: unsupported type\")\n\t\t}\n\t}\n\treturn true\n}\n\n// Nil asserts that the value is nil.\nfunc Nil(t TestingT, value any, msgs ...string) bool {\n\tt.Helper()\n\tif value != nil {\n\t\treturn fail(t, msgs, \"should be nil\")\n\t}\n\treturn true\n}\n\n// NotNil asserts that the value is not nil.\nfunc NotNil(t TestingT, value any, msgs ...string) bool {\n\tt.Helper()\n\tif value == nil {\n\t\treturn fail(t, msgs, \"should not be nil\")\n\t}\n\treturn true\n}\n\n// TypedNil asserts that the value is a typed-nil (nil pointer) value.\nfunc TypedNil(t TestingT, value any, msgs ...string) bool {\n\tt.Helper()\n\tif value == nil {\n\t\treturn fail(t, msgs, \"should be typed-nil but got nil instead\")\n\t}\n\tif !istypednil(value) {\n\t\treturn fail(t, msgs, \"should be typed-nil\")\n\t}\n\treturn true\n}\n\n// NotTypedNil asserts that the value is not a typed-nil (nil pointer) value.\nfunc NotTypedNil(t TestingT, value any, msgs ...string) bool {\n\tt.Helper()\n\tif istypednil(value) {\n\t\treturn fail(t, msgs, \"should not be typed-nil\")\n\t}\n\treturn true\n}\n"},{"name":"uassert_test.gno","body":"package uassert_test\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\ttests \"gno.land/r/tests/vm\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar _ uassert.TestingT = (*testing.T)(nil)\n\nfunc TestMock(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tmockT.empty(t)\n\tuassert.NoError(mockT, errors.New(\"foo\"))\n\tmockT.equals(t, \"error: unexpected error: foo\")\n\tuassert.NoError(mockT, errors.New(\"foo\"), \"custom message\")\n\tmockT.equals(t, \"error: unexpected error: foo - custom message\")\n\tuassert.NoError(mockT, errors.New(\"foo\"), \"custom\", \"message\")\n\tmockT.equals(t, \"error: unexpected error: foo - custom message\")\n}\n\nfunc TestNoError(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tuassert.True(t, uassert.NoError(mockT, nil))\n\tmockT.empty(t)\n\tuassert.False(t, uassert.NoError(mockT, errors.New(\"foo bar\")))\n\tmockT.equals(t, \"error: unexpected error: foo bar\")\n}\n\nfunc TestError(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tuassert.True(t, uassert.Error(mockT, errors.New(\"foo bar\")))\n\tmockT.empty(t)\n\tuassert.False(t, uassert.Error(mockT, nil))\n\tmockT.equals(t, \"error: an error is expected but got nil\")\n}\n\nfunc TestErrorContains(t *testing.T) {\n\tmockT := new(mockTestingT)\n\n\t// nil error\n\tvar err error\n\tuassert.False(t, uassert.ErrorContains(mockT, err, \"\"), \"ErrorContains should return false for nil arg\")\n}\n\nfunc TestTrue(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.True(mockT, true) {\n\t\tt.Error(\"True should return true\")\n\t}\n\tmockT.empty(t)\n\tif uassert.True(mockT, false) {\n\t\tt.Error(\"True should return false\")\n\t}\n\tmockT.equals(t, \"error: should be true\")\n}\n\nfunc TestFalse(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.False(mockT, false) {\n\t\tt.Error(\"False should return true\")\n\t}\n\tmockT.empty(t)\n\tif uassert.False(mockT, true) {\n\t\tt.Error(\"False should return false\")\n\t}\n\tmockT.equals(t, \"error: should be false\")\n}\n\nfunc TestPanicsWithMessage(cur realm, t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.PanicsWithMessage(mockT, cur, \"panic\", func() {\n\t\tpanic(errors.New(\"panic\"))\n\t}) {\n\t\tt.Error(\"PanicsWithMessage should return true\")\n\t}\n\tmockT.empty(t)\n\n\tif uassert.PanicsWithMessage(mockT, cur, \"Panic!\", func() {\n\t\t// noop\n\t}) {\n\t\tt.Error(\"PanicsWithMessage should return false\")\n\t}\n\tmockT.equals(t, \"error: func should panic\\n\\tPanic value:\\tnil\")\n\n\tif uassert.PanicsWithMessage(mockT, cur, \"at the disco\", func() {\n\t\tpanic(errors.New(\"panic\"))\n\t}) {\n\t\tt.Error(\"PanicsWithMessage should return false\")\n\t}\n\tmockT.equals(t, \"error: func should panic with message:\\t\\\"at the disco\\\"\\n\\tActual panic value:\\t\\\"panic\\\"\")\n\n\tif uassert.PanicsWithMessage(mockT, cur, \"Panic!\", func() {\n\t\tpanic(\"panic\")\n\t}) {\n\t\tt.Error(\"PanicsWithMessage should return false\")\n\t}\n\tmockT.equals(t, \"error: func should panic with message:\\t\\\"Panic!\\\"\\n\\tActual panic value:\\t\\\"panic\\\"\")\n}\n\nfunc TestPanicsContains(cur realm, t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.PanicsContains(mockT, cur, \"panic\", func() {\n\t\tpanic(errors.New(\"panic: something happened\"))\n\t}) {\n\t\tt.Error(\"PanicsContains should return true for substring match\")\n\t}\n\tmockT.empty(t)\n\n\tif uassert.PanicsContains(mockT, cur, \"notfound\", func() {\n\t\tpanic(errors.New(\"panic: something happened\"))\n\t}) {\n\t\tt.Error(\"PanicsContains should return false for missing substring\")\n\t}\n\tmockT.equals(t, \"error: func should panic with message containing:\\t\\\"notfound\\\"\\n\\tActual panic value:\\t\\\"panic: something happened\\\"\")\n\n\tif uassert.PanicsContains(mockT, cur, \"panic\", func() {\n\t\t// noop\n\t}) {\n\t\tt.Error(\"PanicsContains should return false when no panic occurs\")\n\t}\n\tmockT.equals(t, \"error: func should panic\\n\\tPanic value:\\tnil\")\n}\n\nfunc TestAbortsWithMessage(cur realm, t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.AbortsWithMessage(mockT, cur, \"abort message\", func() {\n\t\ttests.ExecSwitch(cross(cur), func() {\n\t\t\tpanic(\"abort message\")\n\t\t})\n\t\tpanic(\"dontcare\")\n\t}) {\n\t\tt.Error(\"AbortsWithMessage should return true\")\n\t}\n\tmockT.empty(t)\n\n\tif uassert.AbortsWithMessage(mockT, cur, \"Abort!\", func() {\n\t\t// noop\n\t}) {\n\t\tt.Error(\"AbortsWithMessage should return false\")\n\t}\n\tmockT.equals(t, \"error: func should abort\")\n\n\tif uassert.AbortsWithMessage(mockT, cur, \"at the disco\", func() {\n\t\ttests.ExecSwitch(cross(cur), func() {\n\t\t\tpanic(\"abort message\")\n\t\t})\n\t\tpanic(\"dontcare\")\n\t}) {\n\t\tt.Error(\"AbortsWithMessage should return false (wrong message)\")\n\t}\n\tmockT.equals(t, \"error: func should abort with message:\\t\\\"at the disco\\\"\\n\\tActual abort value:\\t\\\"abort message\\\"\")\n\n\t// Test that non-crossing panics don't count as abort.\n\tuassert.PanicsWithMessage(mockT, cur, \"non-abort panic\", func() {\n\t\tuassert.AbortsWithMessage(mockT, cur, \"dontcare2\", func() {\n\t\t\tpanic(\"non-abort panic\")\n\t\t})\n\t\tt.Error(\"AbortsWithMessage should not have caught non-abort panic\")\n\t}, \"non-abort panic\")\n\tmockT.empty(t)\n\n\t// Test case where abort value is not a string\n\tif uassert.AbortsWithMessage(mockT, cur, \"doesn't matter\", func() {\n\t\ttests.ExecSwitch(cross(cur), func() {\n\t\t\tpanic(123) // abort with an integer\n\t\t})\n\t\tpanic(\"dontcare\")\n\t}) {\n\t\tt.Error(\"AbortsWithMessage should return false when abort value is not a string\")\n\t}\n\tmockT.equals(t, \"error: func should abort with message:\\t\\\"doesn't matter\\\"\\n\\tActual abort value:\\t\\\"123\\\"\")\n\n\t// XXX: test with Error\n}\n\nfunc TestAbortsContains(cur realm, t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.AbortsContains(mockT, cur, \"abort\", func() {\n\t\ttests.ExecSwitch(cross(cur), func() {\n\t\t\tpanic(\"abort message: something happened\")\n\t\t})\n\t\tpanic(\"dontcare\")\n\t}) {\n\t\tt.Error(\"AbortsContains should return true for substring match\")\n\t}\n\tmockT.empty(t)\n\n\tif uassert.AbortsContains(mockT, cur, \"notfound\", func() {\n\t\ttests.ExecSwitch(cross(cur), func() {\n\t\t\tpanic(\"abort message: something happened\")\n\t\t})\n\t\tpanic(\"dontcare\")\n\t}) {\n\t\tt.Error(\"AbortsContains should return false for missing substring\")\n\t}\n\tmockT.equals(t, \"error: func should abort with message containing:\\t\\\"notfound\\\"\\n\\tActual abort value:\\t\\\"abort message: something happened\\\"\")\n\n\tif uassert.AbortsContains(mockT, cur, \"abort\", func() {\n\t\t// noop\n\t}) {\n\t\tt.Error(\"AbortsContains should return false when no abort occurs\")\n\t}\n\tmockT.equals(t, \"error: func should abort\")\n}\n\nfunc TestNotAborts(cur realm, t *testing.T) {\n\tmockT := new(mockTestingT)\n\n\tif !uassert.NotPanics(mockT, cur, func() {\n\t\t// noop\n\t}) {\n\t\tt.Error(\"NotAborts should return true\")\n\t}\n\tmockT.empty(t)\n\n\tif uassert.NotPanics(mockT, cur, func() {\n\t\ttests.ExecSwitch(cross(cur), func() {\n\t\t\tpanic(\"Abort!\")\n\t\t})\n\t\tpanic(\"dontcare\")\n\t}) {\n\t\tt.Error(\"NotAborts should return false\")\n\t}\n\tmockT.equals(t, \"error: func should not abort\\n\\tAbort value:\\tAbort!\")\n}\n\nfunc TestNotPanics(cur realm, t *testing.T) {\n\tmockT := new(mockTestingT)\n\n\tif !uassert.NotPanics(mockT, cur, func() {\n\t\t// noop\n\t}) {\n\t\tt.Error(\"NotPanics should return true\")\n\t}\n\tmockT.empty(t)\n\n\tif uassert.NotPanics(mockT, cur, func() {\n\t\tpanic(\"Panic!\")\n\t}) {\n\t\tt.Error(\"NotPanics should return false\")\n\t}\n\tmockT.equals(t, \"error: func should not panic\\n\\tPanic value:\\tPanic!\")\n}\n\nfunc TestEqual(t *testing.T) {\n\tmockT := new(mockTestingT)\n\n\tcases := []struct {\n\t\texpected any\n\t\tactual   any\n\t\tresult   bool\n\t\tremark   string\n\t}{\n\t\t// expected to be equal\n\t\t{\"Hello World\", \"Hello World\", true, \"\"},\n\t\t{123, 123, true, \"\"},\n\t\t{123.5, 123.5, true, \"\"},\n\t\t{nil, nil, true, \"\"},\n\t\t{int32(123), int32(123), true, \"\"},\n\t\t{uint64(123), uint64(123), true, \"\"},\n\t\t{address(\"g12345\"), address(\"g12345\"), true, \"\"},\n\t\t// XXX: continue\n\n\t\t// not expected to be equal\n\t\t{\"Hello World\", 42, false, \"\"},\n\t\t{41, 42, false, \"\"},\n\t\t{10, uint(10), false, \"\"},\n\t\t// XXX: continue\n\n\t\t// expected to raise errors\n\t\t// XXX: todo\n\t}\n\n\tfor _, c := range cases {\n\t\tname := fmt.Sprintf(\"Equal(%v, %v)\", c.expected, c.actual)\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tres := uassert.Equal(mockT, c.expected, c.actual)\n\n\t\t\tif res != c.result {\n\t\t\t\tt.Errorf(\"%s should return %v: %s - %s\", name, c.result, c.remark, mockT.actualString())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNotEqual(t *testing.T) {\n\tmockT := new(mockTestingT)\n\n\tcases := []struct {\n\t\texpected any\n\t\tactual   any\n\t\tresult   bool\n\t\tremark   string\n\t}{\n\t\t// expected to be not equal\n\t\t{\"Hello World\", \"Hello\", true, \"\"},\n\t\t{123, 124, true, \"\"},\n\t\t{123.5, 123.6, true, \"\"},\n\t\t{nil, 123, true, \"\"},\n\t\t{int32(123), int32(124), true, \"\"},\n\t\t{uint64(123), uint64(124), true, \"\"},\n\t\t{address(\"g12345\"), address(\"g67890\"), true, \"\"},\n\t\t// XXX: continue\n\n\t\t// not expected to be not equal\n\t\t{\"Hello World\", \"Hello World\", false, \"\"},\n\t\t{123, 123, false, \"\"},\n\t\t{123.5, 123.5, false, \"\"},\n\t\t{nil, nil, false, \"\"},\n\t\t{int32(123), int32(123), false, \"\"},\n\t\t{uint64(123), uint64(123), false, \"\"},\n\t\t{address(\"g12345\"), address(\"g12345\"), false, \"\"},\n\t\t// XXX: continue\n\n\t\t// expected to raise errors\n\t\t// XXX: todo\n\t}\n\n\tfor _, c := range cases {\n\t\tname := fmt.Sprintf(\"NotEqual(%v, %v)\", c.expected, c.actual)\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tres := uassert.NotEqual(mockT, c.expected, c.actual)\n\n\t\t\tif res != c.result {\n\t\t\t\tt.Errorf(\"%s should return %v: %s - %s\", name, c.result, c.remark, mockT.actualString())\n\t\t\t}\n\t\t})\n\t}\n}\n\ntype myStruct struct {\n\tS string\n\tI int\n}\n\nfunc TestEmpty(t *testing.T) {\n\tmockT := new(mockTestingT)\n\n\tcases := []struct {\n\t\tobj           any\n\t\texpectedEmpty bool\n\t}{\n\t\t// expected to be empty\n\t\t{\"\", true},\n\t\t{0, true},\n\t\t{int(0), true},\n\t\t{int32(0), true},\n\t\t{int64(0), true},\n\t\t{uint(0), true},\n\t\t// XXX: continue\n\n\t\t// not expected to be empty\n\t\t{\"Hello World\", false},\n\t\t{1, false},\n\t\t{int32(1), false},\n\t\t{uint64(1), false},\n\t\t{address(\"g12345\"), false},\n\n\t\t// unsupported\n\t\t{nil, false},\n\t\t{myStruct{}, false},\n\t\t{\u0026myStruct{}, false},\n\t}\n\n\tfor _, c := range cases {\n\t\tname := fmt.Sprintf(\"Empty(%v)\", c.obj)\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tres := uassert.Empty(mockT, c.obj)\n\n\t\t\tif res != c.expectedEmpty {\n\t\t\t\tt.Errorf(\"%s should return %v: %s\", name, c.expectedEmpty, mockT.actualString())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestEqualWithStringDiff(t *testing.T) {\n\tcases := []struct {\n\t\tname        string\n\t\texpected    string\n\t\tactual      string\n\t\tshouldPass  bool\n\t\texpectedMsg string\n\t}{\n\t\t{\n\t\t\tname:        \"Identical strings\",\n\t\t\texpected:    \"Hello, world!\",\n\t\t\tactual:      \"Hello, world!\",\n\t\t\tshouldPass:  true,\n\t\t\texpectedMsg: \"\",\n\t\t},\n\t\t{\n\t\t\tname:        \"Different strings - simple\",\n\t\t\texpected:    \"Hello, world!\",\n\t\t\tactual:      \"Hello, World!\",\n\t\t\tshouldPass:  false,\n\t\t\texpectedMsg: \"error: uassert.Equal: strings are different\\n\\tDiff: Hello, [-w][+W]orld!\",\n\t\t},\n\t\t{\n\t\t\tname:        \"Different strings - complex\",\n\t\t\texpected:    \"The quick brown fox jumps over the lazy dog\",\n\t\t\tactual:      \"The quick brown cat jumps over the lazy dog\",\n\t\t\tshouldPass:  false,\n\t\t\texpectedMsg: \"error: uassert.Equal: strings are different\\n\\tDiff: The quick brown [-fox][+cat] jumps over the lazy dog\",\n\t\t},\n\t\t{\n\t\t\tname:        \"Different strings - prefix\",\n\t\t\texpected:    \"prefix_string\",\n\t\t\tactual:      \"string\",\n\t\t\tshouldPass:  false,\n\t\t\texpectedMsg: \"error: uassert.Equal: strings are different\\n\\tDiff: [-prefix_]string\",\n\t\t},\n\t\t{\n\t\t\tname:        \"Different strings - suffix\",\n\t\t\texpected:    \"string\",\n\t\t\tactual:      \"string_suffix\",\n\t\t\tshouldPass:  false,\n\t\t\texpectedMsg: \"error: uassert.Equal: strings are different\\n\\tDiff: string[+_suffix]\",\n\t\t},\n\t\t{\n\t\t\tname:        \"Empty string vs non-empty string\",\n\t\t\texpected:    \"\",\n\t\t\tactual:      \"non-empty\",\n\t\t\tshouldPass:  false,\n\t\t\texpectedMsg: \"error: uassert.Equal: strings are different\\n\\tDiff: [+non-empty]\",\n\t\t},\n\t\t{\n\t\t\tname:        \"Non-empty string vs empty string\",\n\t\t\texpected:    \"non-empty\",\n\t\t\tactual:      \"\",\n\t\t\tshouldPass:  false,\n\t\t\texpectedMsg: \"error: uassert.Equal: strings are different\\n\\tDiff: [-non-empty]\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tmockT := \u0026mockTestingT{}\n\t\t\tresult := uassert.Equal(mockT, tc.expected, tc.actual)\n\n\t\t\tif result != tc.shouldPass {\n\t\t\t\tt.Errorf(\"Expected Equal to return %v, but got %v\", tc.shouldPass, result)\n\t\t\t}\n\n\t\t\tif tc.shouldPass {\n\t\t\t\tmockT.empty(t)\n\t\t\t} else {\n\t\t\t\tmockT.equals(t, tc.expectedMsg)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNotEmpty(t *testing.T) {\n\tmockT := new(mockTestingT)\n\n\tcases := []struct {\n\t\tobj              any\n\t\texpectedNotEmpty bool\n\t}{\n\t\t// expected to be empty\n\t\t{\"\", false},\n\t\t{0, false},\n\t\t{int(0), false},\n\t\t{int32(0), false},\n\t\t{int64(0), false},\n\t\t{uint(0), false},\n\t\t{address(\"\"), false},\n\n\t\t// not expected to be empty\n\t\t{\"Hello World\", true},\n\t\t{1, true},\n\t\t{int32(1), true},\n\t\t{uint64(1), true},\n\t\t{address(\"g12345\"), true},\n\n\t\t// unsupported\n\t\t{nil, false},\n\t\t{myStruct{}, false},\n\t\t{\u0026myStruct{}, false},\n\t}\n\n\tfor _, c := range cases {\n\t\tname := fmt.Sprintf(\"NotEmpty(%v)\", c.obj)\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tres := uassert.NotEmpty(mockT, c.obj)\n\n\t\t\tif res != c.expectedNotEmpty {\n\t\t\t\tt.Errorf(\"%s should return %v: %s\", name, c.expectedNotEmpty, mockT.actualString())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNil(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.Nil(mockT, nil) {\n\t\tt.Error(\"Nil should return true\")\n\t}\n\tmockT.empty(t)\n\tif uassert.Nil(mockT, 0) {\n\t\tt.Error(\"Nil should return false\")\n\t}\n\tmockT.equals(t, \"error: should be nil\")\n\tif uassert.Nil(mockT, (*int)(nil)) {\n\t\tt.Error(\"Nil should return false\")\n\t}\n\tmockT.equals(t, \"error: should be nil\")\n}\n\nfunc TestNotNil(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif uassert.NotNil(mockT, nil) {\n\t\tt.Error(\"NotNil should return false\")\n\t}\n\tmockT.equals(t, \"error: should not be nil\")\n\tif !uassert.NotNil(mockT, 0) {\n\t\tt.Error(\"NotNil should return true\")\n\t}\n\tmockT.empty(t)\n\tif !uassert.NotNil(mockT, (*int)(nil)) {\n\t\tt.Error(\"NotNil should return true\")\n\t}\n\tmockT.empty(t)\n}\n\nfunc TestTypedNil(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif uassert.TypedNil(mockT, nil) {\n\t\tt.Error(\"TypedNil should return false\")\n\t}\n\tmockT.equals(t, \"error: should be typed-nil but got nil instead\")\n\tif uassert.TypedNil(mockT, 0) {\n\t\tt.Error(\"TypedNil should return false\")\n\t}\n\tmockT.equals(t, \"error: should be typed-nil\")\n\tif !uassert.TypedNil(mockT, (*int)(nil)) {\n\t\tt.Error(\"TypedNil should return true\")\n\t}\n\tmockT.empty(t)\n}\n\nfunc TestNotTypedNil(t *testing.T) {\n\tmockT := new(mockTestingT)\n\tif !uassert.NotTypedNil(mockT, nil) {\n\t\tt.Error(\"NotTypedNil should return true\")\n\t}\n\tmockT.empty(t)\n\tif !uassert.NotTypedNil(mockT, 0) {\n\t\tt.Error(\"NotTypedNil should return true\")\n\t}\n\tmockT.empty(t)\n\tif uassert.NotTypedNil(mockT, (*int)(nil)) {\n\t\tt.Error(\"NotTypedNil should return false\")\n\t}\n\tmockT.equals(t, \"error: should not be typed-nil\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"vlBFZIuCGdnRKFrEhWCuOEmjPOK3CucVuYL2qQAOglyHGaL3KwKyJ+4oA5ndUeEzOMtJTHqw9JKBbuMVVZ4UDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"avl","path":"gno.land/p/nt/avl/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n\n# AVL Tree Package\n\nThe `avl` package provides a gas-efficient AVL tree implementation for storing key-value data in Gno realms.\n\n## Basic Usage\n\n```go\npackage myrealm\n\nimport \"gno.land/p/nt/avl/v0\"\n\n// This AVL tree will be persisted after transaction calls\nvar tree *avl.Tree\n\nfunc Set(key string, value int) {\n\t// tree.Set takes in a string key, and a value that can be of any type\n\ttree.Set(key, value)\n}\n\nfunc Get(key string) int {\n\t// tree.Get returns the value at given key, or nil if the key does not exist.\n\t// Use a type assertion to convert the raw value into the proper type.\n\trawValue := tree.Get(key)\n\tif rawValue == nil {\n\t\tpanic(\"value at given key does not exist\")\n\t}\n\n\treturn rawValue.(int)\n}\n\nfunc Exists(key string) bool {\n\t// tree.Has returns true if the key exists, without retrieving the value.\n\treturn tree.Has(key)\n}\n```\n\n## Storage Architecture: AVL Tree vs Map\n\nIn Gno, the choice between `avl.Tree` and `map` is fundamentally about how data is persisted in storage.\n\n**Maps** are stored as a single, monolithic object. When you access *any* value in a map, Gno must load the *entire* map into memory. For a map with 1,000 entries, accessing one value means loading all 1,000 entries.\n\n**AVL trees** store each node as a separate object. When you access a value, Gno only loads the nodes along the search path (typically log2(n) nodes). For a tree with 1,000 entries, accessing one value loads ~10 nodes; but a tree with 1,000,000 entries only needs to load ~20 nodes.\n\n## Storage Comparison Example\n\nConsider a realm with 1,000 key-value pairs. Here's what happens when you access a single value:\n\n**Map storage:**\n\n```\nObject :4 = map{\n  (\"0\" string):(\"123\" string),\n  (\"1\" string):(\"123\" string),\n  ...\n  (\"999\" string):(\"123\" string)\n}\n```\n- Accessing `map[\"100\"]` loads object `:4` (contains **all 1,000 pairs**)\n- Gas cost is proportional to total map size (1,000 entries)\n- **1 object fetch, but massive data load**\n\n**AVL tree storage:**\n\n```\nObject :6 = Node{key=\"4\", height=10, size=1000, left=:7, right=...}\nObject :9 = Node{key=\"2\", height=9, size=334, left=:10, right=...}\nObject :11 = Node{key=\"14\", height=8, size=112, left=:12, right=...}\nObject :13 = Node{key=\"12\", height=6, size=46, left=:14, right=...}\nObject :15 = Node{key=\"11\", height=5, size=24, left=:16, right=...}\nObject :17 = Node{key=\"102\", height=4, size=13, left=:18, right=...}\nObject :19 = Node{key=\"100\", height=3, size=5, left=:30, right=...}\nObject :31 = Node{key=\"101\", height=1, size=2, left=:32, right=...}\nObject :33 = Node{key=\"100\", value=\"123\", height=0, size=1}\n```\n- Accessing `tree.Get(\"100\")` loads ~10 objects (the search path)\n- Gas cost is proportional to log2(n) ≈ 10 nodes\n- **10 object fetches, each containing only a single node**\n\n## Further Reading\n\n- [Why should you use an AVL tree instead of a map?](https://howl.moe/posts/2024-09-19-gno-avl-over-maps/) - Howl detailed analysis\n- [Berty's AVL scalability report](https://github.com/gnolang/hackerspace/issues/67) - Real-world testing with up to 20M entries\n- [Wikipedia - AVL tree](https://en.wikipedia.org/wiki/AVL_tree) - Algorithm details and balancing\n- [Effective Gno](https://docs.gno.land/resources/effective-gno#prefer-avltree-over-map-for-scalable-storage) - High-level usage guidance\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package avl provides a gas-efficient AVL tree implementation for storing\n// key-value data in Gno realms.\npackage avl\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/avl/v0\"\ngno = \"0.9\"\n"},{"name":"node.gno","body":"package avl\n\n//----------------------------------------\n// Node\n\n// Node represents a node in an AVL tree.\ntype Node struct {\n\tkey       string // key is the unique identifier for the node.\n\tvalue     any    // value is the data stored in the node.\n\theight    int8   // height is the height of the node in the tree.\n\tsize      int    // size is the number of leaf nodes (key-value pairs) in the subtree rooted at this node.\n\tleftNode  *Node  // leftNode is the left child of the node.\n\trightNode *Node  // rightNode is the right child of the node.\n}\n\n// NewNode creates a new node with the given key and value.\nfunc NewNode(key string, value any) *Node {\n\treturn \u0026Node{\n\t\tkey:    key,\n\t\tvalue:  value,\n\t\theight: 0,\n\t\tsize:   1,\n\t}\n}\n\n// Size returns the size of the subtree rooted at the node.\nfunc (node *Node) Size() int {\n\tif node == nil {\n\t\treturn 0\n\t}\n\treturn node.size\n}\n\n// IsLeaf checks if the node is a leaf node (has no children).\nfunc (node *Node) IsLeaf() bool {\n\treturn node.height == 0\n}\n\n// Key returns the key of the node.\nfunc (node *Node) Key() string {\n\treturn node.key\n}\n\n// Value returns the value of the node.\nfunc (node *Node) Value() any {\n\treturn node.value\n}\n\nfunc (node *Node) _copy() *Node {\n\tif node.height == 0 {\n\t\tpanic(\"Why are you copying a value node?\")\n\t}\n\treturn \u0026Node{\n\t\tkey:       node.key,\n\t\theight:    node.height,\n\t\tsize:      node.size,\n\t\tleftNode:  node.leftNode,\n\t\trightNode: node.rightNode,\n\t}\n}\n\n// Has checks if a node with the given key exists in the subtree rooted at the node.\nfunc (node *Node) Has(key string) (has bool) {\n\tif node == nil {\n\t\treturn false\n\t}\n\tif node.key == key {\n\t\treturn true\n\t}\n\tif node.height == 0 {\n\t\treturn false\n\t} else {\n\t\tif key \u003c node.key {\n\t\t\treturn node.getLeftNode().Has(key)\n\t\t} else {\n\t\t\treturn node.getRightNode().Has(key)\n\t\t}\n\t}\n}\n\n// Get searches for a node with the given key in the subtree rooted at the node\n// and returns its index, value, and whether it exists.\nfunc (node *Node) Get(key string) (index int, value any, exists bool) {\n\tif node == nil {\n\t\treturn 0, nil, false\n\t}\n\n\tif node.height == 0 {\n\t\tif node.key == key {\n\t\t\treturn 0, node.value, true\n\t\t} else if node.key \u003c key {\n\t\t\treturn 1, nil, false\n\t\t} else {\n\t\t\treturn 0, nil, false\n\t\t}\n\t} else {\n\t\tif key \u003c node.key {\n\t\t\treturn node.getLeftNode().Get(key)\n\t\t} else {\n\t\t\trightNode := node.getRightNode()\n\t\t\tindex, value, exists = rightNode.Get(key)\n\t\t\tindex += node.size - rightNode.size\n\t\t\treturn index, value, exists\n\t\t}\n\t}\n}\n\n// GetByIndex retrieves the key-value pair of the node at the given index\n// in the subtree rooted at the node.\nfunc (node *Node) GetByIndex(index int) (key string, value any) {\n\tif index \u003c 0 {\n\t\tpanic(\"GetByIndex: negative index not allowed\")\n\t}\n\n\tif node.height == 0 {\n\t\tif index != 0 {\n\t\t\tpanic(\"GetByIndex asked for invalid index\")\n\t\t}\n\t\treturn node.key, node.value\n\t} else {\n\t\t// TODO: could improve this by storing the sizes\n\t\tleftNode := node.getLeftNode()\n\t\tif index \u003c leftNode.size {\n\t\t\treturn leftNode.GetByIndex(index)\n\t\t} else {\n\t\t\treturn node.getRightNode().GetByIndex(index - leftNode.size)\n\t\t}\n\t}\n}\n\n// Set inserts a new node with the given key-value pair into the subtree rooted at the node,\n// and returns the new root of the subtree and whether an existing node was updated.\n//\n// XXX consider a better way to do this... perhaps split Node from Node.\nfunc (node *Node) Set(key string, value any) (newSelf *Node, updated bool) {\n\tif node == nil {\n\t\treturn NewNode(key, value), false\n\t}\n\tif node.height == 0 {\n\t\tif key \u003c node.key {\n\t\t\treturn \u0026Node{\n\t\t\t\tkey:       node.key,\n\t\t\t\theight:    1,\n\t\t\t\tsize:      2,\n\t\t\t\tleftNode:  NewNode(key, value),\n\t\t\t\trightNode: node,\n\t\t\t}, false\n\t\t} else if key == node.key {\n\t\t\treturn NewNode(key, value), true\n\t\t} else {\n\t\t\treturn \u0026Node{\n\t\t\t\tkey:       key,\n\t\t\t\theight:    1,\n\t\t\t\tsize:      2,\n\t\t\t\tleftNode:  node,\n\t\t\t\trightNode: NewNode(key, value),\n\t\t\t}, false\n\t\t}\n\t} else {\n\t\tnode = node._copy()\n\t\tif key \u003c node.key {\n\t\t\tnode.leftNode, updated = node.getLeftNode().Set(key, value)\n\t\t} else {\n\t\t\tnode.rightNode, updated = node.getRightNode().Set(key, value)\n\t\t}\n\t\tif updated {\n\t\t\treturn node, updated\n\t\t} else {\n\t\t\tnode.calcHeightAndSize()\n\t\t\treturn node.balance(), updated\n\t\t}\n\t}\n}\n\n// Remove deletes the node with the given key from the subtree rooted at the node.\n// returns the new root of the subtree, the new leftmost leaf key (if changed),\n// the removed value and the removal was successful.\nfunc (node *Node) Remove(key string) (\n\tnewNode *Node, newKey string, value any, removed bool,\n) {\n\tif node == nil {\n\t\treturn nil, \"\", nil, false\n\t}\n\tif node.height == 0 {\n\t\tif key == node.key {\n\t\t\treturn nil, \"\", node.value, true\n\t\t} else {\n\t\t\treturn node, \"\", nil, false\n\t\t}\n\t} else {\n\t\tif key \u003c node.key {\n\t\t\tvar newLeftNode *Node\n\t\t\tnewLeftNode, newKey, value, removed = node.getLeftNode().Remove(key)\n\t\t\tif !removed {\n\t\t\t\treturn node, \"\", value, false\n\t\t\t} else if newLeftNode == nil { // left node held value, was removed\n\t\t\t\treturn node.rightNode, node.key, value, true\n\t\t\t}\n\t\t\tnode = node._copy()\n\t\t\tnode.leftNode = newLeftNode\n\t\t\tnode.calcHeightAndSize()\n\t\t\tnode = node.balance()\n\t\t\treturn node, newKey, value, true\n\t\t} else {\n\t\t\tvar newRightNode *Node\n\t\t\tnewRightNode, newKey, value, removed = node.getRightNode().Remove(key)\n\t\t\tif !removed {\n\t\t\t\treturn node, \"\", value, false\n\t\t\t} else if newRightNode == nil { // right node held value, was removed\n\t\t\t\treturn node.leftNode, \"\", value, true\n\t\t\t}\n\t\t\tnode = node._copy()\n\t\t\tnode.rightNode = newRightNode\n\t\t\tif newKey != \"\" {\n\t\t\t\tnode.key = newKey\n\t\t\t}\n\t\t\tnode.calcHeightAndSize()\n\t\t\tnode = node.balance()\n\t\t\treturn node, \"\", value, true\n\t\t}\n\t}\n}\n\nfunc (node *Node) getLeftNode() *Node {\n\treturn node.leftNode\n}\n\nfunc (node *Node) getRightNode() *Node {\n\treturn node.rightNode\n}\n\n// rotateRight performs a right rotation on the node and returns the new root.\n// NOTE: overwrites node\n// TODO: optimize balance \u0026 rotate\nfunc (node *Node) rotateRight() *Node {\n\tnode = node._copy()\n\tl := node.getLeftNode()\n\t_l := l._copy()\n\n\t_lrCached := _l.rightNode\n\t_l.rightNode = node\n\tnode.leftNode = _lrCached\n\n\tnode.calcHeightAndSize()\n\t_l.calcHeightAndSize()\n\n\treturn _l\n}\n\n// rotateLeft performs a left rotation on the node and returns the new root.\n// NOTE: overwrites node\n// TODO: optimize balance \u0026 rotate\nfunc (node *Node) rotateLeft() *Node {\n\tnode = node._copy()\n\tr := node.getRightNode()\n\t_r := r._copy()\n\n\t_rlCached := _r.leftNode\n\t_r.leftNode = node\n\tnode.rightNode = _rlCached\n\n\tnode.calcHeightAndSize()\n\t_r.calcHeightAndSize()\n\n\treturn _r\n}\n\n// calcHeightAndSize updates the height and size of the node based on its children.\n// NOTE: mutates height and size\nfunc (node *Node) calcHeightAndSize() {\n\tnode.height = maxInt8(node.getLeftNode().height, node.getRightNode().height) + 1\n\tnode.size = node.getLeftNode().size + node.getRightNode().size\n}\n\n// calcBalance calculates the balance factor of the node.\nfunc (node *Node) calcBalance() int {\n\treturn int(node.getLeftNode().height) - int(node.getRightNode().height)\n}\n\n// balance balances the subtree rooted at the node and returns the new root.\n// NOTE: assumes that node can be modified\n// TODO: optimize balance \u0026 rotate\nfunc (node *Node) balance() (newSelf *Node) {\n\tbalance := node.calcBalance()\n\tif balance \u003e 1 {\n\t\tif node.getLeftNode().calcBalance() \u003e= 0 {\n\t\t\t// Left Left Case\n\t\t\treturn node.rotateRight()\n\t\t} else {\n\t\t\t// Left Right Case\n\t\t\tleft := node.getLeftNode()\n\t\t\tnode.leftNode = left.rotateLeft()\n\t\t\treturn node.rotateRight()\n\t\t}\n\t}\n\tif balance \u003c -1 {\n\t\tif node.getRightNode().calcBalance() \u003c= 0 {\n\t\t\t// Right Right Case\n\t\t\treturn node.rotateLeft()\n\t\t} else {\n\t\t\t// Right Left Case\n\t\t\tright := node.getRightNode()\n\t\t\tnode.rightNode = right.rotateRight()\n\t\t\treturn node.rotateLeft()\n\t\t}\n\t}\n\t// Nothing changed\n\treturn node\n}\n\n// Shortcut for TraverseInRange.\nfunc (node *Node) Iterate(start, end string, cb func(*Node) bool) bool {\n\treturn node.TraverseInRange(start, end, true, true, cb)\n}\n\n// Shortcut for TraverseInRange.\nfunc (node *Node) ReverseIterate(start, end string, cb func(*Node) bool) bool {\n\treturn node.TraverseInRange(start, end, false, true, cb)\n}\n\n// TraverseInRange traverses all nodes, including inner nodes.\n// Start is inclusive and end is exclusive when ascending,\n// Start and end are inclusive when descending.\n// Empty start and empty end denote no start and no end.\n// If leavesOnly is true, only visit leaf nodes.\n// NOTE: To simulate an exclusive reverse traversal,\n// just append 0x00 to start.\nfunc (node *Node) TraverseInRange(start, end string, ascending bool, leavesOnly bool, cb func(*Node) bool) bool {\n\tif node == nil {\n\t\treturn false\n\t}\n\tafterStart := (start == \"\" || start \u003c node.key)\n\tstartOrAfter := (start == \"\" || start \u003c= node.key)\n\tbeforeEnd := false\n\tif ascending {\n\t\tbeforeEnd = (end == \"\" || node.key \u003c end)\n\t} else {\n\t\tbeforeEnd = (end == \"\" || node.key \u003c= end)\n\t}\n\n\t// Run callback per inner/leaf node.\n\tstop := false\n\tif (!node.IsLeaf() \u0026\u0026 !leavesOnly) ||\n\t\t(node.IsLeaf() \u0026\u0026 startOrAfter \u0026\u0026 beforeEnd) {\n\t\tstop = cb(node)\n\t\tif stop {\n\t\t\treturn stop\n\t\t}\n\t}\n\tif node.IsLeaf() {\n\t\treturn stop\n\t}\n\n\tif ascending {\n\t\t// check lower nodes, then higher\n\t\tif afterStart {\n\t\t\tstop = node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t\tif stop {\n\t\t\treturn stop\n\t\t}\n\t\tif beforeEnd {\n\t\t\tstop = node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t} else {\n\t\t// check the higher nodes first\n\t\tif beforeEnd {\n\t\t\tstop = node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t\tif stop {\n\t\t\treturn stop\n\t\t}\n\t\tif afterStart {\n\t\t\tstop = node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t}\n\n\treturn stop\n}\n\n// TraverseByOffset traverses all nodes, including inner nodes.\n// A limit of math.MaxInt means no limit.\nfunc (node *Node) TraverseByOffset(offset, limit int, ascending bool, leavesOnly bool, cb func(*Node) bool) bool {\n\tif node == nil {\n\t\treturn false\n\t}\n\n\t// Clamp negative offset to 0; otherwise `delta := first.size - offset`\n\t// over-counts and silently drops nodes.\n\tif offset \u003c 0 {\n\t\toffset = 0\n\t}\n\n\t// fast paths. these happen only if TraverseByOffset is called directly on a leaf.\n\tif limit \u003c= 0 || offset \u003e= node.size {\n\t\treturn false\n\t}\n\tif node.IsLeaf() {\n\t\tif offset \u003e 0 {\n\t\t\treturn false\n\t\t}\n\t\treturn cb(node)\n\t}\n\n\t// go to the actual recursive function.\n\treturn node.traverseByOffset(offset, limit, ascending, leavesOnly, cb)\n}\n\n// TraverseByOffset traverses the subtree rooted at the node by offset and limit,\n// in either ascending or descending order, and applies the callback function to each traversed node.\n// If leavesOnly is true, only leaf nodes are visited.\nfunc (node *Node) traverseByOffset(offset, limit int, ascending bool, leavesOnly bool, cb func(*Node) bool) bool {\n\t// caller guarantees: offset \u003c node.size; limit \u003e 0.\n\tif !leavesOnly {\n\t\tif cb(node) {\n\t\t\treturn true // Stop traversal if callback returns true\n\t\t}\n\t}\n\tfirst, second := node.getLeftNode(), node.getRightNode()\n\tif !ascending {\n\t\tfirst, second = second, first\n\t}\n\tif first.IsLeaf() {\n\t\t// either run or skip, based on offset\n\t\tif offset \u003e 0 {\n\t\t\toffset--\n\t\t} else {\n\t\t\tif cb(first) {\n\t\t\t\treturn true // Stop traversal if callback returns true\n\t\t\t}\n\t\t\tlimit--\n\t\t\tif limit \u003c= 0 {\n\t\t\t\treturn true // Stop traversal when limit is reached\n\t\t\t}\n\t\t}\n\t} else {\n\t\t// possible cases:\n\t\t// 1 the offset given skips the first node entirely\n\t\t// 2 the offset skips none or part of the first node, but the limit requires some of the second node.\n\t\t// 3 the offset skips none or part of the first node, and the limit stops our search on the first node.\n\t\tif offset \u003e= first.size {\n\t\t\toffset -= first.size // 1\n\t\t} else {\n\t\t\tif first.traverseByOffset(offset, limit, ascending, leavesOnly, cb) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\t// number of leaves which could actually be called from inside\n\t\t\tdelta := first.size - offset\n\t\t\toffset = 0\n\t\t\tif delta \u003e= limit {\n\t\t\t\treturn true // 3\n\t\t\t}\n\t\t\tlimit -= delta // 2\n\t\t}\n\t}\n\n\t// because of the caller guarantees and the way we handle the first node,\n\t// at this point we know that limit \u003e 0 and there must be some values in\n\t// this second node that we include.\n\n\t// =\u003e if the second node is a leaf, it has to be included.\n\tif second.IsLeaf() {\n\t\treturn cb(second)\n\t}\n\t// =\u003e if it is not a leaf, it will still be enough to recursively call this\n\t// function with the updated offset and limit\n\treturn second.traverseByOffset(offset, limit, ascending, leavesOnly, cb)\n}\n\n// Only used in testing...\nfunc (node *Node) lmd() *Node {\n\tif node.height == 0 {\n\t\treturn node\n\t}\n\treturn node.getLeftNode().lmd()\n}\n\n// Only used in testing...\nfunc (node *Node) rmd() *Node {\n\tif node.height == 0 {\n\t\treturn node\n\t}\n\treturn node.getRightNode().rmd()\n}\n\nfunc maxInt8(a, b int8) int8 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n"},{"name":"node_test.gno","body":"package avl\n\nimport (\n\t\"sort\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestTraverseByOffset(t *testing.T) {\n\tconst testStrings = `Alfa\nAlfred\nAlpha\nAlphabet\nBeta\nBeth\nBook\nBrowser`\n\ttt := []struct {\n\t\tname string\n\t\tasc  bool\n\t}{\n\t\t{\"ascending\", true},\n\t\t{\"descending\", false},\n\t}\n\n\tfor _, tt := range tt {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// use sl to insert the values, and reversed to match the values\n\t\t\t// we do this to ensure that the order of TraverseByOffset is independent\n\t\t\t// from the insertion order\n\t\t\tsl := strings.Split(testStrings, \"\\n\")\n\t\t\tsort.Strings(sl)\n\t\t\treversed := append([]string{}, sl...)\n\t\t\treverseSlice(reversed)\n\n\t\t\tif !tt.asc {\n\t\t\t\tsl, reversed = reversed, sl\n\t\t\t}\n\n\t\t\tr := NewNode(reversed[0], nil)\n\t\t\tfor _, v := range reversed[1:] {\n\t\t\t\tr, _ = r.Set(v, nil)\n\t\t\t}\n\n\t\t\tvar result []string\n\t\t\tfor i := 0; i \u003c len(sl); i++ {\n\t\t\t\tr.TraverseByOffset(i, 1, tt.asc, true, func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tif !slicesEqual(sl, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", sl, result)\n\t\t\t}\n\n\t\t\tfor l := 2; l \u003c= len(sl); l++ {\n\t\t\t\t// \"slices\"\n\t\t\t\tfor i := 0; i \u003c= len(sl); i++ {\n\t\t\t\t\tmax := i + l\n\t\t\t\t\tif max \u003e len(sl) {\n\t\t\t\t\t\tmax = len(sl)\n\t\t\t\t\t}\n\t\t\t\t\texp := sl[i:max]\n\t\t\t\t\tactual := []string{}\n\n\t\t\t\t\tr.TraverseByOffset(i, l, tt.asc, true, func(tr *Node) bool {\n\t\t\t\t\t\tactual = append(actual, tr.Key())\n\t\t\t\t\t\treturn false\n\t\t\t\t\t})\n\t\t\t\t\tif !slicesEqual(exp, actual) {\n\t\t\t\t\t\tt.Errorf(\"want %v got %v\", exp, actual)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestTraverseByOffsetNegativeOffset(t *testing.T) {\n\t// a negative offset must be treated as 0, not silently drop the right\n\t// subtree. building {\"a\",\"b\",\"c\",\"d\"} yields a tree whose root has two\n\t// inner children, exercising the traverseByOffset delta computation.\n\tkeys := []string{\"a\", \"b\", \"c\", \"d\"}\n\tr := NewNode(keys[0], nil)\n\tfor _, k := range keys[1:] {\n\t\tr, _ = r.Set(k, nil)\n\t}\n\n\tfor _, offset := range []int{-1, -2, -100} {\n\t\tvar got []string\n\t\tr.TraverseByOffset(offset, len(keys), true, true, func(n *Node) bool {\n\t\t\tgot = append(got, n.Key())\n\t\t\treturn false\n\t\t})\n\t\tif !slicesEqual(keys, got) {\n\t\t\tt.Errorf(\"offset %d: want %v got %v\", offset, keys, got)\n\t\t}\n\t}\n}\n\nfunc TestHas(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\thasKey   string\n\t\texpected bool\n\t}{\n\t\t{\n\t\t\t\"has key in non-empty tree\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"B\",\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"does not have key in non-empty tree\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"F\",\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"has key in single-node tree\",\n\t\t\t[]string{\"A\"},\n\t\t\t\"A\",\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"does not have key in single-node tree\",\n\t\t\t[]string{\"A\"},\n\t\t\t\"B\",\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"does not have key in empty tree\",\n\t\t\t[]string{},\n\t\t\t\"A\",\n\t\t\tfalse,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tresult := tree.Has(tt.hasKey)\n\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"Expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGet(t *testing.T) {\n\ttests := []struct {\n\t\tname         string\n\t\tinput        []string\n\t\tgetKey       string\n\t\texpectIdx    int\n\t\texpectVal    any\n\t\texpectExists bool\n\t}{\n\t\t{\n\t\t\t\"get existing key\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"B\",\n\t\t\t1,\n\t\t\tnil,\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"get non-existent key (smaller)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"@\",\n\t\t\t0,\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get non-existent key (larger)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"F\",\n\t\t\t5,\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get from empty tree\",\n\t\t\t[]string{},\n\t\t\t\"A\",\n\t\t\t0,\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tidx, val, exists := tree.Get(tt.getKey)\n\n\t\t\tif idx != tt.expectIdx {\n\t\t\t\tt.Errorf(\"Expected index %d, got %d\", tt.expectIdx, idx)\n\t\t\t}\n\n\t\t\tif val != tt.expectVal {\n\t\t\t\tt.Errorf(\"Expected value %v, got %v\", tt.expectVal, val)\n\t\t\t}\n\n\t\t\tif exists != tt.expectExists {\n\t\t\t\tt.Errorf(\"Expected exists %t, got %t\", tt.expectExists, exists)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetByIndex(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tinput       []string\n\t\tidx         int\n\t\texpectKey   string\n\t\texpectVal   any\n\t\texpectPanic bool\n\t}{\n\t\t{\n\t\t\t\"get by valid index\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t2,\n\t\t\t\"C\",\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get by valid index (smallest)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t0,\n\t\t\t\"A\",\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get by valid index (largest)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t4,\n\t\t\t\"E\",\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get by invalid index (negative)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t-1,\n\t\t\t\"\",\n\t\t\tnil,\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"get by invalid index (out of range)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t5,\n\t\t\t\"\",\n\t\t\tnil,\n\t\t\ttrue,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tif tt.expectPanic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r == nil {\n\t\t\t\t\t\tt.Errorf(\"Expected a panic but didn't get one\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\n\t\t\tkey, val := tree.GetByIndex(tt.idx)\n\n\t\t\tif !tt.expectPanic {\n\t\t\t\tif key != tt.expectKey {\n\t\t\t\t\tt.Errorf(\"Expected key %s, got %s\", tt.expectKey, key)\n\t\t\t\t}\n\n\t\t\t\tif val != tt.expectVal {\n\t\t\t\t\tt.Errorf(\"Expected value %v, got %v\", tt.expectVal, val)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRemove(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinput     []string\n\t\tremoveKey string\n\t\texpected  []string\n\t}{\n\t\t{\n\t\t\t\"remove leaf node\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"D\"},\n\t\t\t\"B\",\n\t\t\t[]string{\"A\", \"C\", \"D\"},\n\t\t},\n\t\t{\n\t\t\t\"remove node with one child\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"D\"},\n\t\t\t\"A\",\n\t\t\t[]string{\"B\", \"C\", \"D\"},\n\t\t},\n\t\t{\n\t\t\t\"remove node with two children\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"C\",\n\t\t\t[]string{\"A\", \"B\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"remove root node\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"C\",\n\t\t\t[]string{\"A\", \"B\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"remove non-existent key\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"F\",\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\ttree, _, _, _ = tree.Remove(tt.removeKey)\n\n\t\t\tresult := make([]string, 0)\n\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\tresult = append(result, n.Key())\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestTraverse(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\t\"empty tree\",\n\t\t\t[]string{},\n\t\t\t[]string{},\n\t\t},\n\t\t{\n\t\t\t\"single node tree\",\n\t\t\t[]string{\"A\"},\n\t\t\t[]string{\"A\"},\n\t\t},\n\t\t{\n\t\t\t\"small tree\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"large tree\",\n\t\t\t[]string{\"H\", \"D\", \"L\", \"B\", \"F\", \"J\", \"N\", \"A\", \"C\", \"E\", \"G\", \"I\", \"K\", \"M\", \"O\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tt.Run(\"iterate\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t\t}\n\t\t\t})\n\n\t\t\tt.Run(\"ReverseIterate\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\ttree.ReverseIterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\texpected := make([]string, len(tt.expected))\n\t\t\t\tcopy(expected, tt.expected)\n\t\t\t\tfor i, j := 0, len(expected)-1; i \u003c j; i, j = i+1, j-1 {\n\t\t\t\t\texpected[i], expected[j] = expected[j], expected[i]\n\t\t\t\t}\n\t\t\t\tif !slicesEqual(expected, result) {\n\t\t\t\t\tt.Errorf(\"want %v got %v\", expected, result)\n\t\t\t\t}\n\t\t\t})\n\n\t\t\tt.Run(\"TraverseInRange\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\tstart, end := \"C\", \"M\"\n\t\t\t\ttree.TraverseInRange(start, end, true, true, func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\texpected := make([]string, 0)\n\t\t\t\tfor _, key := range tt.expected {\n\t\t\t\t\tif key \u003e= start \u0026\u0026 key \u003c end {\n\t\t\t\t\t\texpected = append(expected, key)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif !slicesEqual(expected, result) {\n\t\t\t\t\tt.Errorf(\"want %v got %v\", expected, result)\n\t\t\t\t}\n\t\t\t})\n\n\t\t\tt.Run(\"early termination\", func(t *testing.T) {\n\t\t\t\tif len(tt.input) == 0 {\n\t\t\t\t\treturn // Skip for empty tree\n\t\t\t\t}\n\n\t\t\t\tvar result []string\n\t\t\t\tvar count int\n\t\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\t\tcount++\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn true // Stop after first item\n\t\t\t\t})\n\n\t\t\t\tif count != 1 {\n\t\t\t\t\tt.Errorf(\"Expected callback to be called exactly once, got %d calls\", count)\n\t\t\t\t}\n\t\t\t\tif len(result) != 1 {\n\t\t\t\t\tt.Errorf(\"Expected exactly one result, got %d items\", len(result))\n\t\t\t\t}\n\t\t\t\tif len(result) \u003e 0 \u0026\u0026 result[0] != tt.expected[0] {\n\t\t\t\t\tt.Errorf(\"Expected first item to be %v, got %v\", tt.expected[0], result[0])\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestRotateWhenHeightDiffers(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\t\"right rotation when left subtree is higher\",\n\t\t\t[]string{\"E\", \"C\", \"A\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"left rotation when right subtree is higher\",\n\t\t\t[]string{\"A\", \"C\", \"E\", \"D\", \"F\"},\n\t\t\t[]string{\"A\", \"C\", \"D\", \"E\", \"F\"},\n\t\t},\n\t\t{\n\t\t\t\"left-right rotation\",\n\t\t\t[]string{\"E\", \"A\", \"C\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"right-left rotation\",\n\t\t\t[]string{\"A\", \"E\", \"C\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\t// perform rotation or balance\n\t\t\ttree = tree.balance()\n\n\t\t\t// check tree structure\n\t\t\tvar result []string\n\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\tresult = append(result, n.Key())\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRotateAndBalance(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\t\"right rotation\",\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"left rotation\",\n\t\t\t[]string{\"E\", \"D\", \"C\", \"B\", \"A\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"left-right rotation\",\n\t\t\t[]string{\"C\", \"A\", \"E\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"right-left rotation\",\n\t\t\t[]string{\"C\", \"E\", \"A\", \"D\", \"B\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\ttree = tree.balance()\n\n\t\t\tvar result []string\n\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\tresult = append(result, n.Key())\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRemoveFromEmptyTree(t *testing.T) {\n\tvar tree *Node\n\tnewTree, _, val, removed := tree.Remove(\"NonExistent\")\n\tif newTree != nil {\n\t\tt.Errorf(\"Removing from an empty tree should still be nil tree.\")\n\t}\n\tif val != nil || removed {\n\t\tt.Errorf(\"Expected no value and removed=false when removing from empty tree.\")\n\t}\n}\n\nfunc TestBalanceAfterRemoval(t *testing.T) {\n\ttests := []struct {\n\t\tname            string\n\t\tinsertKeys      []string\n\t\tremoveKey       string\n\t\texpectedBalance int\n\t}{\n\t\t{\n\t\t\tname:            \"balance after removing right node\",\n\t\t\tinsertKeys:      []string{\"B\", \"A\", \"D\", \"C\", \"E\"},\n\t\t\tremoveKey:       \"E\",\n\t\t\texpectedBalance: 0,\n\t\t},\n\t\t{\n\t\t\tname:            \"balance after removing left node\",\n\t\t\tinsertKeys:      []string{\"D\", \"B\", \"E\", \"A\", \"C\"},\n\t\t\tremoveKey:       \"A\",\n\t\t\texpectedBalance: 0,\n\t\t},\n\t\t{\n\t\t\tname:            \"ensure no lean after removal\",\n\t\t\tinsertKeys:      []string{\"C\", \"B\", \"E\", \"A\", \"D\", \"F\"},\n\t\t\tremoveKey:       \"F\",\n\t\t\texpectedBalance: -1,\n\t\t},\n\t\t{\n\t\t\tname:            \"descending order insert, remove middle node\",\n\t\t\tinsertKeys:      []string{\"E\", \"D\", \"C\", \"B\", \"A\"},\n\t\t\tremoveKey:       \"C\",\n\t\t\texpectedBalance: 0,\n\t\t},\n\t\t{\n\t\t\tname:            \"ascending order insert, remove middle node\",\n\t\t\tinsertKeys:      []string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t\tremoveKey:       \"C\",\n\t\t\texpectedBalance: 0,\n\t\t},\n\t\t{\n\t\t\tname:            \"duplicate key insert, remove the duplicated key\",\n\t\t\tinsertKeys:      []string{\"C\", \"B\", \"C\", \"A\", \"D\"},\n\t\t\tremoveKey:       \"C\",\n\t\t\texpectedBalance: 1,\n\t\t},\n\t\t{\n\t\t\tname:            \"complex rotation case\",\n\t\t\tinsertKeys:      []string{\"H\", \"B\", \"A\", \"C\", \"E\", \"D\", \"F\", \"G\"},\n\t\t\tremoveKey:       \"B\",\n\t\t\texpectedBalance: 0,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.insertKeys {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\ttree, _, _, _ = tree.Remove(tt.removeKey)\n\n\t\t\tbalance := tree.calcBalance()\n\t\t\tif balance != tt.expectedBalance {\n\t\t\t\tt.Errorf(\"Expected balance factor %d, got %d\", tt.expectedBalance, balance)\n\t\t\t}\n\n\t\t\tif balance \u003c -1 || balance \u003e 1 {\n\t\t\t\tt.Errorf(\"Tree is unbalanced with factor %d\", balance)\n\t\t\t}\n\n\t\t\tif errMsg := checkSubtreeBalance(t, tree); errMsg != \"\" {\n\t\t\t\tt.Errorf(\"AVL property violation after removal: %s\", errMsg)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBSTProperty(t *testing.T) {\n\tvar tree *Node\n\tkeys := []string{\"D\", \"B\", \"F\", \"A\", \"C\", \"E\", \"G\"}\n\tfor _, key := range keys {\n\t\ttree, _ = tree.Set(key, nil)\n\t}\n\n\tvar result []string\n\tinorderTraversal(t, tree, \u0026result)\n\n\tfor i := 1; i \u003c len(result); i++ {\n\t\tif result[i] \u003c result[i-1] {\n\t\t\tt.Errorf(\"BST property violated: %s \u003c %s (index %d)\",\n\t\t\t\tresult[i], result[i-1], i)\n\t\t}\n\t}\n}\n\n// inorderTraversal performs an inorder traversal of the tree and returns the keys in a list.\nfunc inorderTraversal(t *testing.T, node *Node, result *[]string) {\n\tt.Helper()\n\n\tif node == nil {\n\t\treturn\n\t}\n\t// leaf\n\tif node.height == 0 {\n\t\t*result = append(*result, node.key)\n\t\treturn\n\t}\n\tinorderTraversal(t, node.leftNode, result)\n\tinorderTraversal(t, node.rightNode, result)\n}\n\n// checkSubtreeBalance checks if all nodes under the given node satisfy the AVL tree conditions.\n// The balance factor of all nodes must be ∈ [-1, +1]\nfunc checkSubtreeBalance(t *testing.T, node *Node) string {\n\tt.Helper()\n\n\tif node == nil {\n\t\treturn \"\"\n\t}\n\n\tif node.IsLeaf() {\n\t\t// leaf node must be height=0, size=1\n\t\tif node.height != 0 {\n\t\t\treturn ufmt.Sprintf(\"Leaf node %s has height %d, expected 0\", node.Key(), node.height)\n\t\t}\n\t\tif node.size != 1 {\n\t\t\treturn ufmt.Sprintf(\"Leaf node %s has size %d, expected 1\", node.Key(), node.size)\n\t\t}\n\t\treturn \"\"\n\t}\n\n\t// check balance factor for current node\n\tbalanceFactor := node.calcBalance()\n\tif balanceFactor \u003c -1 || balanceFactor \u003e 1 {\n\t\treturn ufmt.Sprintf(\"Node %s is unbalanced: balanceFactor=%d\", node.Key(), balanceFactor)\n\t}\n\n\t// check height / size relationship for children\n\tleft, right := node.getLeftNode(), node.getRightNode()\n\texpectedHeight := maxInt8(left.height, right.height) + 1\n\tif node.height != expectedHeight {\n\t\treturn ufmt.Sprintf(\"Node %s has incorrect height %d, expected %d\", node.Key(), node.height, expectedHeight)\n\t}\n\texpectedSize := left.Size() + right.Size()\n\tif node.size != expectedSize {\n\t\treturn ufmt.Sprintf(\"Node %s has incorrect size %d, expected %d\", node.Key(), node.size, expectedSize)\n\t}\n\n\t// recursively check the left/right subtree\n\tif errMsg := checkSubtreeBalance(t, left); errMsg != \"\" {\n\t\treturn errMsg\n\t}\n\tif errMsg := checkSubtreeBalance(t, right); errMsg != \"\" {\n\t\treturn errMsg\n\t}\n\n\treturn \"\"\n}\n\nfunc slicesEqual(w1, w2 []string) bool {\n\tif len(w1) != len(w2) {\n\t\treturn false\n\t}\n\tfor i := 0; i \u003c len(w1); i++ {\n\t\tif w1[i] != w2[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc reverseSlice(ss []string) {\n\tfor i := 0; i \u003c len(ss)/2; i++ {\n\t\tj := len(ss) - 1 - i\n\t\tss[i], ss[j] = ss[j], ss[i]\n\t}\n}\n"},{"name":"tree.gno","body":"package avl\n\ntype ITree interface {\n\t// read operations\n\n\tSize() int\n\tHas(key string) bool\n\tGet(key string) any\n\tGetByIndex(index int) (key string, value any)\n\tIterate(start, end string, cb IterCbFn) bool\n\tReverseIterate(start, end string, cb IterCbFn) bool\n\tIterateByOffset(offset int, count int, cb IterCbFn) bool\n\tReverseIterateByOffset(offset int, count int, cb IterCbFn) bool\n\n\t// write operations\n\n\tSet(key string, value any) (updated bool)\n\tRemove(key string) (value any, removed bool)\n}\n\ntype IterCbFn func(key string, value any) bool\n\n//----------------------------------------\n// Tree\n\n// The zero struct can be used as an empty tree.\ntype Tree struct {\n\tnode *Node\n}\n\n// NewTree creates a new empty AVL tree.\nfunc NewTree() *Tree {\n\treturn \u0026Tree{\n\t\tnode: nil,\n\t}\n}\n\n// Size returns the number of key-value pair in the tree.\nfunc (tree *Tree) Size() int {\n\treturn tree.node.Size()\n}\n\n// Has checks whether a key exists in the tree.\n// It returns true if the key exists, otherwise false.\nfunc (tree *Tree) Has(key string) (has bool) {\n\treturn tree.node.Has(key)\n}\n\n// Get retrieves the value associated with the given key.\n// It returns the value if the key exists, or nil if it doesn't.\n// Note that a key stored with a nil value is indistinguishable\n// from an absent key; use Has to check for existence.\n// This allows for a simpler usage pattern with type assertions:\n//\n//\tif value, ok := tree.Get(\"key\").(MyType); ok {\n//\t    // use value\n//\t}\nfunc (tree *Tree) Get(key string) any {\n\t_, value, _ := tree.node.Get(key)\n\treturn value\n}\n\n// GetByIndex retrieves the key-value pair at the specified index in the tree.\n// It returns the key and value at the given index.\nfunc (tree *Tree) GetByIndex(index int) (key string, value any) {\n\treturn tree.node.GetByIndex(index)\n}\n\n// Set inserts a key-value pair into the tree.\n// If the key already exists, the value will be updated.\n// It returns a boolean indicating whether the key was newly inserted or updated.\nfunc (tree *Tree) Set(key string, value any) (updated bool) {\n\tnewnode, updated := tree.node.Set(key, value)\n\ttree.node = newnode\n\treturn updated\n}\n\n// Remove removes a key-value pair from the tree.\n// It returns the removed value and a boolean indicating whether the key was found and removed.\nfunc (tree *Tree) Remove(key string) (value any, removed bool) {\n\tnewnode, _, value, removed := tree.node.Remove(key)\n\ttree.node = newnode\n\treturn value, removed\n}\n\n// Iterate performs an in-order traversal of the tree within the specified key range.\n// It calls the provided callback function for each key-value pair encountered.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) Iterate(start, end string, cb IterCbFn) bool {\n\treturn tree.node.TraverseInRange(start, end, true, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// ReverseIterate performs a reverse in-order traversal of the tree within the specified key range.\n// It calls the provided callback function for each key-value pair encountered.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) ReverseIterate(start, end string, cb IterCbFn) bool {\n\treturn tree.node.TraverseInRange(start, end, false, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// IterateByOffset performs an in-order traversal of the tree starting from the specified offset.\n// It calls the provided callback function for each key-value pair encountered, up to the specified count.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) IterateByOffset(offset int, count int, cb IterCbFn) bool {\n\treturn tree.node.TraverseByOffset(offset, count, true, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// ReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset.\n// It calls the provided callback function for each key-value pair encountered, up to the specified count.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) ReverseIterateByOffset(offset int, count int, cb IterCbFn) bool {\n\treturn tree.node.TraverseByOffset(offset, count, false, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// Verify that Tree implements TreeInterface\nvar _ ITree = (*Tree)(nil)\n"},{"name":"tree_test.gno","body":"package avl\n\nimport \"testing\"\n\nfunc TestNewTree(t *testing.T) {\n\ttree := NewTree()\n\tif tree.node != nil {\n\t\tt.Error(\"Expected tree.node to be nil\")\n\t}\n}\n\nfunc TestTreeSize(t *testing.T) {\n\ttree := NewTree()\n\tif tree.Size() != 0 {\n\t\tt.Error(\"Expected empty tree size to be 0\")\n\t}\n\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\tif tree.Size() != 2 {\n\t\tt.Error(\"Expected tree size to be 2\")\n\t}\n}\n\nfunc TestTreeHas(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\n\tif !tree.Has(\"key1\") {\n\t\tt.Error(\"Expected tree to have key1\")\n\t}\n\n\tif tree.Has(\"key2\") {\n\t\tt.Error(\"Expected tree to not have key2\")\n\t}\n}\n\nfunc TestTreeGet(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\n\tvalue := tree.Get(\"key1\")\n\tif value != \"value1\" {\n\t\tt.Error(\"Expected Get to return value1\")\n\t}\n\n\tvalue = tree.Get(\"key2\")\n\tif value != nil {\n\t\tt.Error(\"Expected Get to return nil for non-existent key\")\n\t}\n}\n\nfunc TestTreeGetByIndex(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\n\tkey, value := tree.GetByIndex(0)\n\tif key != \"key1\" || value != \"value1\" {\n\t\tt.Error(\"Expected GetByIndex(0) to return key1 and value1\")\n\t}\n\n\tkey, value = tree.GetByIndex(1)\n\tif key != \"key2\" || value != \"value2\" {\n\t\tt.Error(\"Expected GetByIndex(1) to return key2 and value2\")\n\t}\n\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Error(\"Expected GetByIndex to panic for out-of-range index\")\n\t\t}\n\t}()\n\ttree.GetByIndex(2)\n}\n\nfunc TestTreeRemove(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\n\tvalue, removed := tree.Remove(\"key1\")\n\tif !removed || value != \"value1\" || tree.Size() != 0 {\n\t\tt.Error(\"Expected Remove to remove key-value pair\")\n\t}\n\n\t_, removed = tree.Remove(\"key2\")\n\tif removed {\n\t\tt.Error(\"Expected Remove to return false for non-existent key\")\n\t}\n}\n\nfunc TestTreeIterate(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key1\", \"key2\", \"key3\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\nfunc TestTreeReverseIterate(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key3\", \"key2\", \"key1\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\nfunc TestTreeIterateByOffset(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.IterateByOffset(1, 2, func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key2\", \"key3\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\nfunc TestTreeReverseIterateByOffset(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.ReverseIterateByOffset(1, 2, func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key2\", \"key1\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\nfunc TestTreeReverseIterateByOffsetVaried(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// All keys in reverse: e d c b a\n\tcases := []struct {\n\t\toffset int\n\t\tlimit  int\n\t\twant   []string\n\t}{\n\t\t{0, 5, []string{\"e\", \"d\", \"c\", \"b\", \"a\"}},\n\t\t{0, 1, []string{\"e\"}},\n\t\t{0, 3, []string{\"e\", \"d\", \"c\"}},\n\t\t{1, 2, []string{\"d\", \"c\"}},\n\t\t{2, 2, []string{\"c\", \"b\"}},\n\t\t{3, 5, []string{\"b\", \"a\"}},\n\t\t{4, 1, []string{\"a\"}},\n\t\t{4, 10, []string{\"a\"}},\n\t\t{5, 1, nil},\n\t\t{0, 0, nil},\n\t\t{10, 1, nil},\n\t}\n\n\tfor _, tc := range cases {\n\t\tvar got []string\n\t\ttree.ReverseIterateByOffset(tc.offset, tc.limit, func(key string, value any) bool {\n\t\t\tgot = append(got, key)\n\t\t\treturn false\n\t\t})\n\t\tif !slicesEqual(got, tc.want) {\n\t\t\tt.Errorf(\"ReverseIterateByOffset(%d, %d): got %v, want %v\",\n\t\t\t\ttc.offset, tc.limit, got, tc.want)\n\t\t}\n\t}\n\n\t// Early termination: stop after 2 items from offset 1.\n\tvar got []string\n\ttree.ReverseIterateByOffset(1, 5, func(key string, value any) bool {\n\t\tgot = append(got, key)\n\t\treturn len(got) \u003e= 2\n\t})\n\twant := []string{\"d\", \"c\"}\n\tif !slicesEqual(got, want) {\n\t\tt.Errorf(\"ReverseIterateByOffset early stop: got %v, want %v\", got, want)\n\t}\n}\n"},{"name":"z_0_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar node *avl.Node\n\nfunc init() {\n\tnode = avl.NewNode(\"key0\", \"value0\")\n\t// node, _ = node.Set(\"key0\", \"value0\")\n}\n\nfunc main(cur realm) {\n\tvar updated bool\n\tnode, updated = node.Set(\"key1\", \"value1\")\n\t// println(node, updated)\n\tprintln(updated, node.Size())\n}\n\n// Output:\n// false 2\n"},{"name":"z_1_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar node *avl.Node\n\nfunc init() {\n\tnode = avl.NewNode(\"key0\", \"value0\")\n\tnode, _ = node.Set(\"key1\", \"value1\")\n}\n\nfunc main(cur realm) {\n\tvar updated bool\n\tnode, updated = node.Set(\"key2\", \"value2\")\n\t// println(node, updated)\n\tprintln(updated, node.Size())\n}\n\n// Output:\n// false 3\n"},{"name":"z_2_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar tree avl.Tree\n\nfunc init() {\n\ttree.Set(\"key0\", \"value0\")\n\ttree.Set(\"key1\", \"value1\")\n}\n\nfunc main(cur realm) {\n\tvar updated bool\n\tupdated = tree.Set(\"key2\", \"value2\")\n\tprintln(updated, tree.Size())\n}\n\n// Output:\n// false 3\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ZT8ttDRqZR/0mpUTqDld1uInkuTlLD7HJGK25J2kIr/0+9JuCBmMlHVj13wA1N0B3nsyoGXJ8QdYMpU49PXcDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"urequire","path":"gno.land/p/nt/urequire/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# urequire\n\nPackage `urequire` provides test assertion functions that immediately fail the test on error, complementing the `uassert` package.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package urequire provides test assertion functions that immediately fail the\n// test on error, complementing the uassert package.\npackage urequire\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/urequire/v0\"\ngno = \"0.9\"\n"},{"name":"urequire.gno","body":"// urequire is a sister package for uassert.\n// XXX: codegen the package.\npackage urequire\n\nimport \"gno.land/p/nt/uassert/v0\"\n\n// type TestingT = uassert.TestingT // XXX: bug, should work\n\n// NoError requires that a function returned no error (i.e. `nil`).\nfunc NoError(t uassert.TestingT, err error, msgs ...string) {\n\tt.Helper()\n\tif uassert.NoError(t, err, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// Error requires that a function returned an error (i.e. not `nil`).\nfunc Error(t uassert.TestingT, err error, msgs ...string) {\n\tt.Helper()\n\tif uassert.Error(t, err, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// ErrorContains requires that a function returned an error (i.e. not `nil`)\n// and that the error contains the specified substring.\nfunc ErrorContains(t uassert.TestingT, err error, contains string, msgs ...string) {\n\tt.Helper()\n\tif uassert.ErrorContains(t, err, contains, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// True requires that the specified value is true.\nfunc True(t uassert.TestingT, value bool, msgs ...string) {\n\tt.Helper()\n\tif uassert.True(t, value, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// False requires that the specified value is false.\nfunc False(t uassert.TestingT, value bool, msgs ...string) {\n\tt.Helper()\n\tif uassert.False(t, value, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// ErrorIs requires that the given error matches the target error.\nfunc ErrorIs(t uassert.TestingT, err, target error, msgs ...string) {\n\tt.Helper()\n\tif uassert.ErrorIs(t, err, target, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// AbortsWithMessage requires that the code inside the specified func aborts\n// (panics when crossing another realm).\n// Use PanicsWithMessage for requiring local panics within the same realm.\n// Note: This relies on gno's `revive` mechanism to catch aborts.\n// See uassert.AbortsWithMessage for `rlm` semantics.\nfunc AbortsWithMessage(t uassert.TestingT, rlm realm, msg string, f any, msgs ...string) {\n\tt.Helper()\n\tif uassert.AbortsWithMessage(t, rlm, msg, f, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// AbortsContains requires that the code inside the specified func aborts\n// (panics when crossing another realm) and the abort message contains the specified substring.\n// See uassert.AbortsWithMessage for `rlm` semantics.\nfunc AbortsContains(t uassert.TestingT, rlm realm, substr string, f any, msgs ...string) {\n\tt.Helper()\n\tif uassert.AbortsContains(t, rlm, substr, f, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// NotAborts requires that the code inside the specified func does NOT abort\n// when crossing an execution boundary (e.g., VM call).\n// Use NotPanics for requiring the absence of local panics within the same realm.\n// Note: This relies on Gno's `revive` mechanism.\n// See uassert.AbortsWithMessage for `rlm` semantics.\nfunc NotAborts(t uassert.TestingT, rlm realm, f any, msgs ...string) {\n\tt.Helper()\n\tif uassert.NotAborts(t, rlm, f, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// PanicsWithMessage requires that the code inside the specified func panics\n// locally within the same execution realm.\n// Use AbortsWithMessage for requiring panics that cross execution boundaries (aborts).\n// See uassert.AbortsWithMessage for `rlm` semantics.\nfunc PanicsWithMessage(t uassert.TestingT, rlm realm, msg string, f any, msgs ...string) {\n\tt.Helper()\n\tif uassert.PanicsWithMessage(t, rlm, msg, f, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// PanicsContains requires that the code inside the specified func panics\n// locally within the same execution realm and the panic message contains the specified substring.\n// See uassert.AbortsWithMessage for `rlm` semantics.\nfunc PanicsContains(t uassert.TestingT, rlm realm, substr string, f any, msgs ...string) {\n\tt.Helper()\n\tif uassert.PanicsContains(t, rlm, substr, f, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// NotPanics requires that the code inside the specified func does NOT panic\n// locally within the same execution realm.\n// Use NotAborts for requiring the absence of panics that cross execution boundaries (aborts).\n// See uassert.AbortsWithMessage for `rlm` semantics.\nfunc NotPanics(t uassert.TestingT, rlm realm, f any, msgs ...string) {\n\tt.Helper()\n\tif uassert.NotPanics(t, rlm, f, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// Equal requires that two objects are equal.\nfunc Equal(t uassert.TestingT, expected, actual any, msgs ...string) {\n\tt.Helper()\n\tif uassert.Equal(t, expected, actual, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// NotEqual requires that two objects are not equal.\nfunc NotEqual(t uassert.TestingT, expected, actual any, msgs ...string) {\n\tt.Helper()\n\tif uassert.NotEqual(t, expected, actual, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// Empty requires that the specified object is empty\n// (zero value, empty string/slice/map, or nil).\nfunc Empty(t uassert.TestingT, obj any, msgs ...string) {\n\tt.Helper()\n\tif uassert.Empty(t, obj, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// NotEmpty requires that the specified object is not empty.\nfunc NotEmpty(t uassert.TestingT, obj any, msgs ...string) {\n\tt.Helper()\n\tif uassert.NotEmpty(t, obj, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// Nil requires that the value is nil.\nfunc Nil(t uassert.TestingT, value any, msgs ...string) {\n\tt.Helper()\n\tif uassert.Nil(t, value, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// NotNil requires that the value is not nil.\nfunc NotNil(t uassert.TestingT, value any, msgs ...string) {\n\tt.Helper()\n\tif uassert.NotNil(t, value, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// TypedNil requires that the value is a typed-nil (nil pointer) value.\nfunc TypedNil(t uassert.TestingT, value any, msgs ...string) {\n\tt.Helper()\n\tif uassert.TypedNil(t, value, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n\n// NotTypedNil requires that the value is not a typed-nil (nil pointer) value.\nfunc NotTypedNil(t uassert.TestingT, value any, msgs ...string) {\n\tt.Helper()\n\tif uassert.NotTypedNil(t, value, msgs...) {\n\t\treturn\n\t}\n\tt.FailNow()\n}\n"},{"name":"urequire_test.gno","body":"package urequire\n\nimport \"testing\"\n\nfunc TestPackage(t *testing.T) {\n\tEqual(t, 42, 42)\n\n\t// XXX: find a way to unit test this package thoroughly,\n\t// especially the t.FailNow() behavior on assertion failure.\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"DZIYWJoiMblJX4L0iYfZIgkFrSnq8+i3XwWcETEVENfrSqEev4P1ut1PH7xlyOkH5+bFW+KFjWou2gJ47LccDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"grc20","path":"gno.land/p/demo/tokens/grc20","files":[{"name":"examples_test.gno","body":"package grc20\n\n// XXX: write Examples\n\nfunc ExampleInit()                            {}\nfunc ExampleExposeBankForMaketxRunOrImports() {}\nfunc ExampleCustomTellerImpl()                {}\nfunc ExampleAllowance()                       {}\nfunc ExampleRealmBanker()                     {}\nfunc ExamplePreviousRealmBanker()             {}\nfunc ExampleAccountBanker()                   {}\nfunc ExampleTransfer()                        {}\nfunc ExampleApprove()                         {}\nfunc ExampleTransferFrom()                    {}\nfunc ExampleMint()                            {}\nfunc ExampleBurn()                            {}\n\n// ...\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tokens/grc20\"\ngno = \"0.9\"\n"},{"name":"mock.gno","body":"package grc20\n\n// XXX: func Mock(t *Token)\n"},{"name":"tellers.gno","body":"package grc20\n\nimport (\n\t\"chain\"\n)\n\n// CallerTeller returns a GRC20 compatible teller that, at each write call,\n// resolves the caller as rlm.Previous() — the realm that crossed into the\n// caller. rlm must be the caller's own captured cur (asserted via\n// rlm.IsCurrent() inside the Teller methods).\nfunc (tok *Token) CallerTeller() Teller {\n\tif tok == nil {\n\t\tpanic(\"Token cannot be nil\")\n\t}\n\n\treturn \u0026fnTeller{\n\t\taccountFn: func(_ int, rlm realm) address {\n\t\t\treturn rlm.Previous().Address()\n\t\t},\n\t\tToken: tok,\n\t}\n}\n\n// ReadonlyTeller is a GRC20 compatible teller that panics for any write operation.\nfunc (tok *Token) ReadonlyTeller() Teller {\n\tif tok == nil {\n\t\tpanic(\"Token cannot be nil\")\n\t}\n\n\treturn \u0026fnTeller{\n\t\taccountFn: nil,\n\t\tToken:     tok,\n\t}\n}\n\n// RealmTeller returns a GRC20 compatible teller that will store the\n// caller realm permanently. Calling anything through this teller will\n// result in allowance or balance changes for the realm that initialized the teller.\n// The initializer of this teller should usually never share the resulting Teller from\n// this method except maybe for advanced delegation flows such as a DAO treasury\n// management.\n//\n// rlm must be the caller's own captured cur (asserted via rlm.IsCurrent()).\n// The address is frozen eagerly at construction.\nfunc (tok *Token) RealmTeller(_ int, rlm realm) Teller {\n\tif tok == nil {\n\t\tpanic(\"Token cannot be nil\")\n\t}\n\tif !rlm.IsCurrent() {\n\t\tpanic(ErrSpoofedRealm)\n\t}\n\n\tcaller := rlm.Address()\n\n\treturn \u0026fnTeller{\n\t\taccountFn: func(_ int, _ realm) address {\n\t\t\treturn caller\n\t\t},\n\t\tToken: tok,\n\t}\n}\n\n// RealmSubTeller is like RealmTeller but uses the provided slug to derive a\n// subaccount.\n//\n// rlm must be the caller's own captured cur (asserted via rlm.IsCurrent()).\n// The subaccount address is frozen eagerly at construction.\nfunc (tok *Token) RealmSubTeller(_ int, rlm realm, slug string) Teller {\n\tif tok == nil {\n\t\tpanic(\"Token cannot be nil\")\n\t}\n\tif !rlm.IsCurrent() {\n\t\tpanic(ErrSpoofedRealm)\n\t}\n\n\taccount := accountSlugAddr(rlm.Address(), slug)\n\n\treturn \u0026fnTeller{\n\t\taccountFn: func(_ int, _ realm) address {\n\t\t\treturn account\n\t\t},\n\t\tToken: tok,\n\t}\n}\n\n// ImpersonateTeller returns a GRC20 compatible teller that impersonates as a\n// specified address. This allows operations to be performed as if they were\n// executed by the given address, enabling the caller to manipulate tokens on\n// behalf of that address.\n//\n// It is particularly useful in scenarios where a contract needs to perform\n// actions on behalf of a user or another account, without exposing the\n// underlying logic or requiring direct access to the user's account. The\n// returned teller will use the provided address for all operations, effectively\n// masking the original caller.\n//\n// This method should be used with caution, as it allows for potentially\n// sensitive operations to be performed under the guise of another address.\nfunc (ledger *PrivateLedger) ImpersonateTeller(addr address) Teller {\n\tif ledger == nil {\n\t\tpanic(\"Ledger cannot be nil\")\n\t}\n\n\treturn \u0026fnTeller{\n\t\taccountFn: func(_ int, _ realm) address {\n\t\t\treturn addr\n\t\t},\n\t\tToken: ledger.token,\n\t}\n}\n\n// generic tellers methods.\n//\n\nfunc (ft *fnTeller) Transfer(_ int, rlm realm, to address, amount int64) error {\n\tif ft.accountFn == nil {\n\t\treturn ErrReadonly\n\t}\n\tif !rlm.IsCurrent() {\n\t\treturn ErrSpoofedRealm\n\t}\n\tcaller := ft.accountFn(0, rlm)\n\treturn ft.Token.ledger.Transfer(caller, to, amount)\n}\n\nfunc (ft *fnTeller) Approve(_ int, rlm realm, spender address, amount int64) error {\n\tif ft.accountFn == nil {\n\t\treturn ErrReadonly\n\t}\n\tif !rlm.IsCurrent() {\n\t\treturn ErrSpoofedRealm\n\t}\n\tcaller := ft.accountFn(0, rlm)\n\treturn ft.Token.ledger.Approve(caller, spender, amount)\n}\n\nfunc (ft *fnTeller) TransferFrom(_ int, rlm realm, owner, to address, amount int64) error {\n\tif ft.accountFn == nil {\n\t\treturn ErrReadonly\n\t}\n\tif !rlm.IsCurrent() {\n\t\treturn ErrSpoofedRealm\n\t}\n\tspender := ft.accountFn(0, rlm)\n\treturn ft.Token.ledger.TransferFrom(owner, spender, to, amount)\n}\n\n// helpers\n//\n\n// accountSlugAddr returns the address derived from the specified address and slug.\nfunc accountSlugAddr(addr address, slug string) address {\n\t// XXX: use a new `std.XXX` call for this.\n\tif slug == \"\" {\n\t\treturn addr\n\t}\n\tkey := addr.String() + \"/\" + slug\n\treturn chain.PackageAddress(key) // temporarily using this helper\n}\n"},{"name":"tellers_test.gno","body":"package grc20\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestCallerTellerImpl(cur realm, t *testing.T) {\n\ttok, _ := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 4)\n\tteller := tok.CallerTeller()\n\turequire.False(t, tok == nil)\n\tvar _ Teller = teller\n}\n\n// evilWrap embeds Teller; the seal-marker pattern (if we had one) would be\n// satisfied via promotion, but IsCanonicalTeller rejects it because *evilWrap\n// is nominally distinct from *fnTeller.\ntype evilWrap struct {\n\tTeller\n}\n\nfunc TestIsCanonicalTeller(cur realm, t *testing.T) {\n\ttok, _ := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 4)\n\tlegit := tok.CallerTeller()\n\n\tuassert.True(t, IsCanonicalTeller(legit),\n\t\t\"canonical *fnTeller from CallerTeller must pass\")\n\n\tevil := \u0026evilWrap{Teller: legit}\n\tuassert.False(t, IsCanonicalTeller(evil),\n\t\t\"foreign type embedding a canonical Teller must be rejected\")\n}\n\nfunc TestTeller(cur realm, t *testing.T) {\n\tvar (\n\t\talice = testutils.TestAddress(\"alice\")\n\t\tbob   = testutils.TestAddress(\"bob\")\n\t\tcarl  = testutils.TestAddress(\"carl\")\n\t)\n\n\ttoken, ledger := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 6)\n\n\tcheckBalances := func(aliceEB, bobEB, carlEB int64) {\n\t\tt.Helper()\n\t\texp := ufmt.Sprintf(\"alice=%d bob=%d carl=%d\", aliceEB, bobEB, carlEB)\n\t\taliceGB := token.BalanceOf(alice)\n\t\tbobGB := token.BalanceOf(bob)\n\t\tcarlGB := token.BalanceOf(carl)\n\t\tgot := ufmt.Sprintf(\"alice=%d bob=%d carl=%d\", aliceGB, bobGB, carlGB)\n\t\tuassert.Equal(t, got, exp, \"invalid balances\")\n\t}\n\tcheckAllowances := func(abEB, acEB, baEB, bcEB, caEB, cbEB int64) {\n\t\tt.Helper()\n\t\texp := ufmt.Sprintf(\"ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s\", abEB, acEB, baEB, bcEB, caEB, cbEB)\n\t\tabGB := token.Allowance(alice, bob)\n\t\tacGB := token.Allowance(alice, carl)\n\t\tbaGB := token.Allowance(bob, alice)\n\t\tbcGB := token.Allowance(bob, carl)\n\t\tcaGB := token.Allowance(carl, alice)\n\t\tcbGB := token.Allowance(carl, bob)\n\t\tgot := ufmt.Sprintf(\"ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s\", abGB, acGB, baGB, bcGB, caGB, cbGB)\n\t\tuassert.Equal(t, got, exp, \"invalid allowances\")\n\t}\n\n\tcheckBalances(0, 0, 0)\n\tcheckAllowances(0, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, ledger.Mint(alice, 1000))\n\turequire.NoError(t, ledger.Mint(alice, 100))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(0, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, ledger.Approve(alice, bob, 99999999))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(99999999, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, ledger.Approve(alice, bob, 400))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(400, 0, 0, 0, 0, 0)\n\n\turequire.Error(t, ledger.TransferFrom(alice, bob, carl, 100000000))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(400, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, ledger.TransferFrom(alice, bob, carl, 100))\n\tcheckBalances(1000, 0, 100)\n\tcheckAllowances(300, 0, 0, 0, 0, 0)\n\n\turequire.Error(t, ledger.SpendAllowance(alice, bob, 2000000))\n\tcheckBalances(1000, 0, 100)\n\tcheckAllowances(300, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, ledger.SpendAllowance(alice, bob, 100))\n\tcheckBalances(1000, 0, 100)\n\tcheckAllowances(200, 0, 0, 0, 0, 0)\n}\n\nfunc TestCallerTeller(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tbob := testutils.TestAddress(\"bob\")\n\tcarl := testutils.TestAddress(\"carl\")\n\n\ttoken, ledger := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 6)\n\tteller := token.CallerTeller()\n\n\tcheckBalances := func(aliceEB, bobEB, carlEB int64) {\n\t\tt.Helper()\n\t\texp := ufmt.Sprintf(\"alice=%d bob=%d carl=%d\", aliceEB, bobEB, carlEB)\n\t\taliceGB := token.BalanceOf(alice)\n\t\tbobGB := token.BalanceOf(bob)\n\t\tcarlGB := token.BalanceOf(carl)\n\t\tgot := ufmt.Sprintf(\"alice=%d bob=%d carl=%d\", aliceGB, bobGB, carlGB)\n\t\tuassert.Equal(t, got, exp, \"invalid balances\")\n\t}\n\tcheckAllowances := func(abEB, acEB, baEB, bcEB, caEB, cbEB int64) {\n\t\tt.Helper()\n\t\texp := ufmt.Sprintf(\"ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s\", abEB, acEB, baEB, bcEB, caEB, cbEB)\n\t\tabGB := token.Allowance(alice, bob)\n\t\tacGB := token.Allowance(alice, carl)\n\t\tbaGB := token.Allowance(bob, alice)\n\t\tbcGB := token.Allowance(bob, carl)\n\t\tcaGB := token.Allowance(carl, alice)\n\t\tcbGB := token.Allowance(carl, bob)\n\t\tgot := ufmt.Sprintf(\"ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s\", abGB, acGB, baGB, bcGB, caGB, cbGB)\n\t\tuassert.Equal(t, got, exp, \"invalid allowances\")\n\t}\n\n\turequire.NoError(t, ledger.Mint(alice, 1000))\n\tcheckBalances(1000, 0, 0)\n\tcheckAllowances(0, 0, 0, 0, 0, 0)\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tfunc(cur realm) { urequire.NoError(t, teller.Approve(0, cur, bob, 600)) }(cross(cur))\n\tcheckBalances(1000, 0, 0)\n\tcheckAllowances(600, 0, 0, 0, 0, 0)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) { urequire.Error(t, teller.TransferFrom(0, cur, alice, carl, 700)) }(cross(cur))\n\tcheckBalances(1000, 0, 0)\n\tcheckAllowances(600, 0, 0, 0, 0, 0)\n\tfunc(cur realm) { urequire.NoError(t, teller.TransferFrom(0, cur, alice, carl, 400)) }(cross(cur))\n\tcheckBalances(600, 0, 400)\n\tcheckAllowances(200, 0, 0, 0, 0, 0)\n}\n"},{"name":"token.gno","body":"package grc20\n\nimport (\n\t\"chain\"\n\t\"math\"\n\t\"math/overflow\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// NewToken creates a Token whose origRealm is bound to the calling realm.\n// rlm must be the caller's own captured cur (asserted via rlm.IsCurrent()),\n// and rlm.PkgPath() — the calling realm itself — becomes the Token's\n// origRealm. Token.ID() returns origRealm + \".\" + symbol.\n//\n// Because IsCurrent runtime-validates that rlm came from the live\n// crossing frame, origRealm is unforgeable: an external realm cannot\n// fabricate a Token claiming to belong to a different package.\n//\n// Typical call from a realm's init(cur realm) or other crossing function:\n//\n//\tToken, ledger := grc20.NewToken(0, cur, \"Foo\", \"FOO\", 4)\n//\n// If the Token should be discoverable, follow up with\n// grc20reg.Register(cross, Token, slug).\nfunc NewToken(_ int, rlm realm, name, symbol string, decimals int) (*Token, *PrivateLedger) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(ErrSpoofedRealm)\n\t}\n\tpkgPath := rlm.PkgPath()\n\tif pkgPath == \"\" {\n\t\tpanic(ErrNotRealm)\n\t}\n\tif !validName(name) {\n\t\tpanic(ErrInvalidName)\n\t}\n\tif !validSymbol(symbol) {\n\t\tpanic(ErrInvalidSymbol)\n\t}\n\tif decimals \u003c 0 || decimals \u003e MaxDecimals {\n\t\tpanic(ErrInvalidDecimals)\n\t}\n\tledger := \u0026PrivateLedger{}\n\ttoken := \u0026Token{\n\t\tname:      name,\n\t\tsymbol:    symbol,\n\t\tdecimals:  decimals,\n\t\torigRealm: pkgPath,\n\t\tledger:    ledger,\n\t}\n\tledger.token = token\n\treturn token, ledger\n}\n\n// validName reports whether name is a valid display name: non-empty,\n// within MaxNameLen, and contains no control characters (any rune\n// below 0x20 or 0x7f). Permits Unicode letters, digits, punctuation,\n// and spaces — name is purely a display field.\nfunc validName(name string) bool {\n\tif name == \"\" || len(name) \u003e MaxNameLen {\n\t\treturn false\n\t}\n\tfor _, c := range name {\n\t\tif c \u003c 0x20 || c == 0x7f {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// validSymbol reports whether symbol is a valid token symbol: non-empty,\n// within MaxSymbolLen, and consists only of [A-Za-z0-9_-]. Matches\n// grc20reg.validateSlug so the symbol round-trips cleanly through\n// Token.ID() and any registry binding.\nfunc validSymbol(symbol string) bool {\n\tif symbol == \"\" || len(symbol) \u003e MaxSymbolLen {\n\t\treturn false\n\t}\n\tfor _, c := range symbol {\n\t\tif !isAlnum(c) \u0026\u0026 c != '_' \u0026\u0026 c != '-' {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc isAlnum(c rune) bool {\n\treturn (c \u003e= 'a' \u0026\u0026 c \u003c= 'z') || (c \u003e= 'A' \u0026\u0026 c \u003c= 'Z') || (c \u003e= '0' \u0026\u0026 c \u003c= '9')\n}\n\n// GetName returns the name of the token.\nfunc (tok Token) GetName() string { return tok.name }\n\n// GetSymbol returns the symbol of the token.\nfunc (tok Token) GetSymbol() string { return tok.symbol }\n\n// GetDecimals returns the number of decimals used to get the token's precision.\nfunc (tok Token) GetDecimals() int { return tok.decimals }\n\n// TotalSupply returns the total supply of the token.\nfunc (tok Token) TotalSupply() int64 { return tok.ledger.totalSupply }\n\n// KnownAccounts returns the number of known accounts in the bank.\nfunc (tok Token) KnownAccounts() int { return tok.ledger.balances.Size() }\n\n// ID returns the Identifier of the token.\n// It is composed of the original realm and the provided symbol.\nfunc (tok *Token) ID() string {\n\treturn tok.origRealm + \".\" + tok.symbol\n}\n\n// HasAddr checks if the specified address is a known account in the bank.\nfunc (tok Token) HasAddr(addr address) bool {\n\treturn tok.ledger.hasAddr(addr)\n}\n\n// BalanceOf returns the balance of the specified address.\nfunc (tok Token) BalanceOf(addr address) int64 {\n\treturn tok.ledger.balanceOf(addr)\n}\n\n// Allowance returns the allowance of the specified owner and spender.\nfunc (tok Token) Allowance(owner, spender address) int64 {\n\treturn tok.ledger.allowance(owner, spender)\n}\n\nfunc (tok Token) RenderHome() string {\n\tstr := \"\"\n\tstr += ufmt.Sprintf(\"# %s ($%s)\\n\\n\", tok.name, tok.symbol)\n\tstr += ufmt.Sprintf(\"* **Decimals**: %d\\n\", tok.decimals)\n\tstr += ufmt.Sprintf(\"* **Total supply**: %d\\n\", tok.ledger.totalSupply)\n\tstr += ufmt.Sprintf(\"* **Known accounts**: %d\\n\", tok.KnownAccounts())\n\treturn str\n}\n\n// SpendAllowance decreases the allowance of the specified owner and spender.\nfunc (led *PrivateLedger) SpendAllowance(owner, spender address, amount int64) error {\n\tif !owner.IsValid() || !spender.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\n\tif amount \u003c 0 {\n\t\treturn ErrInvalidAmount\n\t}\n\t// do nothing\n\tif amount == 0 {\n\t\treturn nil\n\t}\n\n\tcurrentAllowance := led.allowance(owner, spender)\n\tif currentAllowance \u003c amount {\n\t\treturn ErrInsufficientAllowance\n\t}\n\n\tkey := allowanceKey(owner, spender)\n\tnewAllowance := overflow.Sub64p(currentAllowance, amount)\n\n\tif newAllowance == 0 {\n\t\tled.allowances.Remove(key)\n\t} else {\n\t\tled.allowances.Set(key, newAllowance)\n\t}\n\n\treturn nil\n}\n\n// Transfer transfers tokens from the specified from address to the specified to address.\nfunc (led *PrivateLedger) Transfer(from, to address, amount int64) error {\n\tif !from.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tif !to.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tif from == to {\n\t\treturn ErrCannotTransferToSelf\n\t}\n\tif amount \u003c 0 {\n\t\treturn ErrInvalidAmount\n\t}\n\n\tvar (\n\t\ttoBalance   = led.balanceOf(to)\n\t\tfromBalance = led.balanceOf(from)\n\t)\n\n\tif fromBalance \u003c amount {\n\t\treturn ErrInsufficientBalance\n\t}\n\n\tvar (\n\t\tnewToBalance   = overflow.Add64p(toBalance, amount)\n\t\tnewFromBalance = overflow.Sub64p(fromBalance, amount)\n\t)\n\n\tled.balances.Set(string(to), newToBalance)\n\n\tif newFromBalance == 0 {\n\t\tled.balances.Remove(string(from))\n\t} else {\n\t\tled.balances.Set(string(from), newFromBalance)\n\t}\n\n\tchain.Emit(\n\t\tTransferEvent,\n\t\t\"token\", led.token.ID(),\n\t\t\"from\", from.String(),\n\t\t\"to\", to.String(),\n\t\t\"value\", strconv.Itoa(int(amount)),\n\t)\n\n\treturn nil\n}\n\n// TransferFrom transfers tokens from the specified owner to the specified to address.\n// It first checks if the owner has sufficient balance and then decreases the allowance.\nfunc (led *PrivateLedger) TransferFrom(owner, spender, to address, amount int64) error {\n\tif amount \u003c 0 {\n\t\treturn ErrInvalidAmount\n\t}\n\n\tif !owner.IsValid() || !to.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\n\tif led.balanceOf(owner) \u003c amount {\n\t\treturn ErrInsufficientBalance\n\t}\n\n\t// The check above guarantees that Transfer will succeed, ensuring\n\t// atomicity for the subsequent operations.\n\tif err := led.SpendAllowance(owner, spender, amount); err != nil {\n\t\treturn err\n\t}\n\n\tif err := led.Transfer(owner, to, amount); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// Approve sets the allowance of the specified owner and spender.\nfunc (led *PrivateLedger) Approve(owner, spender address, amount int64) error {\n\tif !owner.IsValid() || !spender.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tif amount \u003c 0 {\n\t\treturn ErrInvalidAmount\n\t}\n\n\tled.allowances.Set(allowanceKey(owner, spender), amount)\n\n\tchain.Emit(\n\t\tApprovalEvent,\n\t\t\"token\", led.token.ID(),\n\t\t\"owner\", string(owner),\n\t\t\"spender\", string(spender),\n\t\t\"value\", strconv.Itoa(int(amount)),\n\t)\n\n\treturn nil\n}\n\n// Mint increases the total supply of the token and adds the specified amount to the specified address.\nfunc (led *PrivateLedger) Mint(addr address, amount int64) error {\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tif amount \u003c 0 {\n\t\treturn ErrInvalidAmount\n\t}\n\n\t// limit amount to MaxInt64 - totalSupply\n\tif amount \u003e overflow.Sub64p(math.MaxInt64, led.totalSupply) {\n\t\treturn ErrMintOverflow\n\t}\n\n\tled.totalSupply += amount\n\tcurrentBalance := led.balanceOf(addr)\n\tnewBalance := overflow.Add64p(currentBalance, amount)\n\n\tled.balances.Set(string(addr), newBalance)\n\n\tchain.Emit(\n\t\tTransferEvent,\n\t\t\"token\", led.token.ID(),\n\t\t\"from\", \"\",\n\t\t\"to\", string(addr),\n\t\t\"value\", strconv.Itoa(int(amount)),\n\t)\n\n\treturn nil\n}\n\n// Burn decreases the total supply of the token and subtracts the specified amount from the specified address.\nfunc (led *PrivateLedger) Burn(addr address, amount int64) error {\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tif amount \u003c 0 {\n\t\treturn ErrInvalidAmount\n\t}\n\n\tcurrentBalance := led.balanceOf(addr)\n\tif currentBalance \u003c amount {\n\t\treturn ErrInsufficientBalance\n\t}\n\n\tled.totalSupply = overflow.Sub64p(led.totalSupply, amount)\n\tnewBalance := overflow.Sub64p(currentBalance, amount)\n\n\tif newBalance == 0 {\n\t\tled.balances.Remove(string(addr))\n\t} else {\n\t\tled.balances.Set(string(addr), newBalance)\n\t}\n\n\tchain.Emit(\n\t\tTransferEvent,\n\t\t\"token\", led.token.ID(),\n\t\t\"from\", string(addr),\n\t\t\"to\", \"\",\n\t\t\"value\", strconv.Itoa(int(amount)),\n\t)\n\n\treturn nil\n}\n\n// hasAddr checks if the specified address is a known account in the ledger.\nfunc (led PrivateLedger) hasAddr(addr address) bool {\n\treturn led.balances.Has(addr.String())\n}\n\n// balanceOf returns the balance of the specified address.\nfunc (led PrivateLedger) balanceOf(addr address) int64 {\n\tbalance := led.balances.Get(addr.String())\n\tif balance == nil {\n\t\treturn 0\n\t}\n\treturn balance.(int64)\n}\n\n// allowance returns the allowance of the specified owner and spender.\nfunc (led PrivateLedger) allowance(owner, spender address) int64 {\n\tallowance := led.allowances.Get(allowanceKey(owner, spender))\n\tif allowance == nil {\n\t\treturn 0\n\t}\n\treturn allowance.(int64)\n}\n\n// allowanceKey returns the key for the allowance of the specified owner and spender.\nfunc allowanceKey(owner, spender address) string {\n\treturn owner.String() + \":\" + spender.String()\n}\n"},{"name":"token_test.gno","body":"package grc20\n\nimport (\n\t\"errors\"\n\t\"math\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// newTestToken constructs a Token. The IIFE same-realm cross\n// promotes the test's EOA-origin cur into a fresh /p/grc20\n// CodeRealm cur so NewToken's IsCurrent + non-empty rlm.PkgPath()\n// checks pass. cross(rlm) is the explicit-rlm form of bare `cross`.\nfunc newTestToken(_ int, rlm realm, name, symbol string, decimals int) (tok *Token, adm *PrivateLedger) {\n\tfunc(cur realm) {\n\t\ttok, adm = NewToken(0, cur, name, symbol, decimals)\n\t}(cross(rlm))\n\treturn\n}\n\nfunc TestTestImpl(cur realm, t *testing.T) {\n\tbank, _ := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 4)\n\turequire.False(t, bank == nil, \"dummy should not be nil\")\n}\n\nfunc TestNewTokenValidation(cur realm, t *testing.T) {\n\t// Each case wraps NewToken in a crossing closure (same as newTestToken)\n\t// to satisfy IsCurrent + non-empty pkgPath. The validation under test\n\t// is the name/symbol/decimals checks that run after.\n\tmustPanic := func(name, sym string, dec int, want error, label string) {\n\t\tt.Helper()\n\t\t// newTestToken wraps NewToken in cross(...), so the panic from\n\t\t// NewToken's validators crosses a realm boundary — use revive()\n\t\t// (defer-recover doesn't see cross-realm panics).\n\t\tr := revive(func() {\n\t\t\tnewTestToken(0, cur, name, sym, dec)\n\t\t})\n\t\tif r == nil {\n\t\t\tt.Errorf(\"%s: expected panic, got none\", label)\n\t\t\treturn\n\t\t}\n\t\tif r != want {\n\t\t\tt.Errorf(\"%s: expected %v, got %v\", label, want, r)\n\t\t}\n\t}\n\n\t// Empty name / symbol.\n\tmustPanic(\"\", \"OK\", 4, ErrInvalidName, \"empty name\")\n\tmustPanic(\"Name\", \"\", 4, ErrInvalidSymbol, \"empty symbol\")\n\n\t// Length caps.\n\tmustPanic(strings.Repeat(\"a\", MaxNameLen+1), \"OK\", 4, ErrInvalidName, \"name too long\")\n\tmustPanic(\"Name\", strings.Repeat(\"A\", MaxSymbolLen+1), 4, ErrInvalidSymbol, \"symbol too long\")\n\n\t// Name control characters.\n\tmustPanic(\"bad\\x01name\", \"OK\", 4, ErrInvalidName, \"name with control char\")\n\tmustPanic(\"bad\\nname\", \"OK\", 4, ErrInvalidName, \"name with newline\")\n\n\t// Symbol charset — disallowed delimiters and whitespace.\n\tmustPanic(\"Name\", \"BA.D\", 4, ErrInvalidSymbol, \"symbol with dot\")\n\tmustPanic(\"Name\", \"BA/D\", 4, ErrInvalidSymbol, \"symbol with slash\")\n\tmustPanic(\"Name\", \"BA D\", 4, ErrInvalidSymbol, \"symbol with space\")\n\tmustPanic(\"Name\", \"BA\\\"D\", 4, ErrInvalidSymbol, \"symbol with quote\")\n\n\t// Decimals out of range.\n\tmustPanic(\"Name\", \"OK\", -1, ErrInvalidDecimals, \"negative decimals\")\n\tmustPanic(\"Name\", \"OK\", MaxDecimals+1, ErrInvalidDecimals, \"decimals over cap\")\n\n\t// Boundary positives — should NOT panic.\n\ttok, _ := newTestToken(0, cur, strings.Repeat(\"a\", MaxNameLen), strings.Repeat(\"A\", MaxSymbolLen), MaxDecimals)\n\turequire.True(t, tok != nil, \"boundary name+symbol+decimals should succeed\")\n\n\t// UTF-8 name with non-ASCII is allowed.\n\ttok2, _ := newTestToken(0, cur, \"Доллар\", \"RUB\", 2)\n\turequire.True(t, tok2 != nil, \"UTF-8 name should be allowed\")\n}\n\nfunc TestToken(cur realm, t *testing.T) {\n\tvar (\n\t\talice = testutils.TestAddress(\"alice\")\n\t\tbob   = testutils.TestAddress(\"bob\")\n\t\tcarl  = testutils.TestAddress(\"carl\")\n\t)\n\n\tbank, adm := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 6)\n\n\tcheckBalances := func(aliceEB, bobEB, carlEB int64) {\n\t\tt.Helper()\n\t\texp := ufmt.Sprintf(\"alice=%d bob=%d carl=%d\", aliceEB, bobEB, carlEB)\n\t\taliceGB := bank.BalanceOf(alice)\n\t\tbobGB := bank.BalanceOf(bob)\n\t\tcarlGB := bank.BalanceOf(carl)\n\t\tgot := ufmt.Sprintf(\"alice=%d bob=%d carl=%d\", aliceGB, bobGB, carlGB)\n\t\tuassert.Equal(t, got, exp, \"invalid balances\")\n\t}\n\tcheckAllowances := func(abEB, acEB, baEB, bcEB, caEB, cbEB int64) {\n\t\tt.Helper()\n\t\texp := ufmt.Sprintf(\"ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s\", abEB, acEB, baEB, bcEB, caEB, cbEB)\n\t\tabGB := bank.Allowance(alice, bob)\n\t\tacGB := bank.Allowance(alice, carl)\n\t\tbaGB := bank.Allowance(bob, alice)\n\t\tbcGB := bank.Allowance(bob, carl)\n\t\tcaGB := bank.Allowance(carl, alice)\n\t\tcbGB := bank.Allowance(carl, bob)\n\t\tgot := ufmt.Sprintf(\"ab=%d ac=%d ba=%d bc=%d ca=%d cb=%s\", abGB, acGB, baGB, bcGB, caGB, cbGB)\n\t\tuassert.Equal(t, got, exp, \"invalid allowances\")\n\t}\n\n\tcheckBalances(0, 0, 0)\n\tcheckAllowances(0, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, adm.Mint(alice, 1000))\n\turequire.NoError(t, adm.Mint(alice, 100))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(0, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, adm.Approve(alice, bob, 99999999))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(99999999, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, adm.Approve(alice, bob, 400))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(400, 0, 0, 0, 0, 0)\n\n\turequire.Error(t, adm.TransferFrom(alice, bob, carl, 100000000))\n\tcheckBalances(1100, 0, 0)\n\tcheckAllowances(400, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, adm.TransferFrom(alice, bob, carl, 100))\n\tcheckBalances(1000, 0, 100)\n\tcheckAllowances(300, 0, 0, 0, 0, 0)\n\n\turequire.Error(t, adm.SpendAllowance(alice, bob, 2000000))\n\tcheckBalances(1000, 0, 100)\n\tcheckAllowances(300, 0, 0, 0, 0, 0)\n\n\turequire.NoError(t, adm.SpendAllowance(alice, bob, 100))\n\tcheckBalances(1000, 0, 100)\n\tcheckAllowances(200, 0, 0, 0, 0, 0)\n}\n\nfunc TestMintOverflow(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tbob := testutils.TestAddress(\"bob\")\n\ttok, adm := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 6)\n\n\tsafeValue := int64(1 \u003c\u003c 62)\n\turequire.NoError(t, adm.Mint(alice, safeValue))\n\turequire.Equal(t, tok.BalanceOf(alice), safeValue)\n\n\terr := adm.Mint(bob, safeValue)\n\tuassert.Error(t, err, \"expected ErrMintOverflow\")\n}\n\nfunc TestTransferFromAtomicity(cur realm, t *testing.T) {\n\tvar (\n\t\towner   = testutils.TestAddress(\"owner\")\n\t\tspender = testutils.TestAddress(\"spender\")\n\n\t\tinvalidRecipient = address(\"\")\n\t\trecipient        = testutils.TestAddress(\"to\")\n\t)\n\n\ttoken, admin := newTestToken(0, cur, \"Test\", \"TEST\", 6)\n\n\t// owner has 100 tokens, spender has 50 allowance\n\tinitialBalance := int64(100)\n\tinitialAllowance := int64(50)\n\n\turequire.NoError(t, admin.Mint(owner, initialBalance))\n\turequire.NoError(t, admin.Approve(owner, spender, initialAllowance))\n\n\t// transfer to an invalid address to force a transfer failure\n\ttransferAmount := int64(30)\n\terr := admin.TransferFrom(owner, spender, invalidRecipient, transferAmount)\n\tuassert.Error(t, err, \"transfer should fail due to invalid address\")\n\n\townerBalance := token.BalanceOf(owner)\n\tuassert.Equal(t, ownerBalance, initialBalance, \"owner balance should remain unchanged\")\n\n\t// check if allowance was incorrectly reduced\n\tremainingAllowance := token.Allowance(owner, spender)\n\tuassert.Equal(t, remainingAllowance, initialAllowance,\n\t\t\"allowance should not be reduced when transfer fails\")\n\n\t// transfer all tokens\n\tadmin.Transfer(owner, recipient, 100)\n\tremainingBalance := token.BalanceOf(owner)\n\tuassert.Equal(t, remainingBalance, int64(0),\n\t\t\"balance should be zero\")\n\n\terr = admin.TransferFrom(owner, spender, recipient, transferAmount)\n\tuassert.Error(t, err, \"transfer should fail due to insufficient balance\")\n}\n\nfunc TestMintUntilOverflow(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tbob := testutils.TestAddress(\"bob\")\n\ttok, adm := newTestToken(0, cur, \"Dummy\", \"DUMMY\", 6)\n\n\ttests := []struct {\n\t\tname           string\n\t\taddr           address\n\t\tamount         int64\n\t\texpectedError  error\n\t\texpectedSupply int64\n\t\tdescription    string\n\t}{\n\t\t{\n\t\t\tname:           \"mint negative value\",\n\t\t\taddr:           alice,\n\t\t\tamount:         -1,\n\t\t\texpectedError:  ErrInvalidAmount,\n\t\t\texpectedSupply: 0,\n\t\t\tdescription:    \"minting a negative number should fail with ErrInvalidAmount\",\n\t\t},\n\t\t{\n\t\t\tname:           \"mint MaxInt64\",\n\t\t\taddr:           alice,\n\t\t\tamount:         math.MaxInt64 - 1000,\n\t\t\texpectedError:  nil,\n\t\t\texpectedSupply: math.MaxInt64 - 1000,\n\t\t\tdescription:    \"minting almost MaxInt64 should succeed\",\n\t\t},\n\t\t{\n\t\t\tname:           \"mint small value\",\n\t\t\taddr:           bob,\n\t\t\tamount:         1000,\n\t\t\texpectedError:  nil,\n\t\t\texpectedSupply: math.MaxInt64,\n\t\t\tdescription:    \"minting a small value when close to MaxInt64 should succeed\",\n\t\t},\n\t\t{\n\t\t\tname:           \"mint value that would exceed MaxInt64\",\n\t\t\taddr:           bob,\n\t\t\tamount:         1,\n\t\t\texpectedError:  ErrMintOverflow,\n\t\t\texpectedSupply: math.MaxInt64,\n\t\t\tdescription:    \"minting any value when at MaxInt64 should fail with ErrMintOverflow\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\terr := adm.Mint(tt.addr, tt.amount)\n\n\t\t\tif tt.expectedError != nil {\n\t\t\t\tuassert.Error(t, err, tt.description)\n\t\t\t\tif !errors.Is(err, tt.expectedError) {\n\t\t\t\t\tt.Errorf(\"expected error %v, got %v\", tt.expectedError, err)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tuassert.NoError(t, err, tt.description)\n\t\t\t}\n\n\t\t\ttotalSupply := tok.TotalSupply()\n\t\t\tuassert.Equal(t, totalSupply, tt.expectedSupply, \"totalSupply should match expected value\")\n\t\t})\n\t}\n}\n"},{"name":"types.gno","body":"package grc20\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// Teller interface defines the methods that a GRC20 token must implement. It\n// extends the TokenMetadata interface to include methods for managing token\n// transfers, allowances, and querying balances.\n//\n// The Teller interface is designed to ensure that any token adhering to this\n// standard provides a consistent API for interacting with fungible tokens.\n//\n// SECURITY: Transfer/Approve/TransferFrom take (_ int, rlm realm, ...), so\n// handing a Teller value to untrusted code yields a capability token to\n// whatever Transfer/Approve/TransferFrom impl that code dispatches into.\n// Any /p/ or /r/ function that accepts a Teller as a parameter from external\n// callers MUST type-assert against the canonical concrete type (*fnTeller)\n// via IsCanonicalTeller and reject otherwise. An unexported-marker \"seal\"\n// does NOT defend against this — see\n// p/test/seal/filetests/z_seal_iface_embedding_filetest.gno\n// for the realistic embedding-bypass attack. Reference impl for the\n// canonical-allowlist pattern: p/jaekwon/allowancesender.\ntype Teller interface {\n\t// Returns the name of the token.\n\tGetName() string\n\n\t// Returns the symbol of the token, usually a shorter version of the\n\t// name.\n\tGetSymbol() string\n\n\t// Returns the decimals places of the token.\n\tGetDecimals() int\n\n\t// Returns the amount of tokens in existence.\n\tTotalSupply() int64\n\n\t// Returns the amount of tokens owned by `account`.\n\tBalanceOf(account address) int64\n\n\t// Moves `amount` tokens from the caller's account to `to`. rlm must\n\t// be the caller's own captured cur — verified via rlm.IsCurrent().\n\t//\n\t// Returns an error if the operation failed.\n\tTransfer(_ int, rlm realm, to address, amount int64) error\n\n\t// Returns the remaining number of tokens that `spender` will be\n\t// allowed to spend on behalf of `owner` through {transferFrom}. This is\n\t// zero by default.\n\t//\n\t// This value changes when {approve} or {transferFrom} are called.\n\tAllowance(owner, spender address) int64\n\n\t// Sets `amount` as the allowance of `spender` over the caller's tokens.\n\t//\n\t// Returns an error if the operation failed.\n\t//\n\t// IMPORTANT: Beware that changing an allowance with this method brings\n\t// the risk that someone may use both the old and the new allowance by\n\t// unfortunate transaction ordering. One possible solution to mitigate\n\t// this race condition is to first reduce the spender's allowance to 0\n\t// and set the desired value afterwards:\n\t// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\n\tApprove(_ int, rlm realm, spender address, amount int64) error\n\n\t// Moves `amount` tokens from `from` to `to` using the\n\t// allowance mechanism. `amount` is then deducted from the caller's\n\t// allowance.\n\t//\n\t// Returns an error if the operation failed.\n\tTransferFrom(_ int, rlm realm, from, to address, amount int64) error\n}\n\n// Token represents a fungible token with a name, symbol, and a certain number\n// of decimal places. It maintains a ledger for tracking balances and allowances\n// of addresses.\n//\n// The Token struct provides methods for retrieving token metadata, such as the\n// name, symbol, and decimals, as well as methods for interacting with the\n// ledger, including checking balances and allowances.\ntype Token struct {\n\t// Name of the token (e.g., \"Dummy Token\").\n\tname string\n\t// Symbol of the token (e.g., \"DUMMY\").\n\tsymbol string\n\t// Number of decimal places used for the token's precision.\n\tdecimals int\n\t// Original realm of the token (e.g., \"gno.land/r/demo/foo20\").\n\torigRealm string\n\t// Pointer to the PrivateLedger that manages balances and allowances.\n\tledger *PrivateLedger\n}\n\n// PrivateLedger is a struct that holds the balances and allowances for the\n// token. It provides administrative functions for minting, burning,\n// transferring tokens, and managing allowances.\n//\n// The PrivateLedger is not safe to expose publicly, as it contains sensitive\n// information regarding token balances and allowances, and allows direct,\n// unrestricted access to all administrative functions.\ntype PrivateLedger struct {\n\t// Total supply of the token managed by this ledger.\n\ttotalSupply int64\n\t// chain.Address -\u003e int64\n\tbalances avl.Tree\n\t// owner.(chain.Address)+\":\"+spender.(chain.Address)) -\u003e int64\n\tallowances avl.Tree\n\t// Pointer to the associated Token struct\n\ttoken *Token\n}\n\nvar (\n\tErrInsufficientBalance   = errors.New(\"insufficient balance\")\n\tErrInsufficientAllowance = errors.New(\"insufficient allowance\")\n\tErrInvalidAddress        = errors.New(\"invalid address\")\n\tErrCannotTransferToSelf  = errors.New(\"cannot send transfer to self\")\n\tErrReadonly              = errors.New(\"banker is readonly\")\n\tErrRestrictedTokenOwner  = errors.New(\"restricted to bank owner\")\n\tErrMintOverflow          = errors.New(\"mint overflow\")\n\tErrInvalidAmount         = errors.New(\"invalid amount\")\n\tErrSpoofedRealm          = errors.New(\"rlm does not match the current crossing frame\")\n\tErrNotRealm              = errors.New(\"rlm must be a realm (got EOA/origin)\")\n\tErrInvalidName           = errors.New(\"invalid token name (empty, too long, or contains control chars)\")\n\tErrInvalidSymbol         = errors.New(\"invalid token symbol (empty, too long, or contains chars outside [A-Za-z0-9_-])\")\n\tErrInvalidDecimals       = errors.New(\"invalid decimals (must be 0..18)\")\n)\n\n// Construction limits. Symbol is restricted to the same charset as\n// grc20reg.validateSlug because Token.ID() = origRealm + \".\" + symbol is\n// emitted in events and frequently used as a registry slug; banning `.`\n// `/` and whitespace here prevents downstream parsers from being fooled\n// by ambiguous IDs (e.g. an attacker emitting tokens whose ID parses as\n// belonging to a victim realm). Name is for display only and allows\n// any valid UTF-8 except control characters.\nconst (\n\tMaxNameLen   = 64\n\tMaxSymbolLen = 11\n\tMaxDecimals  = 18\n)\n\nconst (\n\tMintEvent     = \"Mint\"\n\tBurnEvent     = \"Burn\"\n\tTransferEvent = \"Transfer\"\n\tApprovalEvent = \"Approval\"\n)\n\ntype fnTeller struct {\n\taccountFn func(_ int, rlm realm) address\n\t*Token\n}\n\nvar _ Teller = (*fnTeller)(nil)\n\n// IsCanonicalTeller reports whether t is the canonical *fnTeller produced by\n// Token.CallerTeller / RealmTeller / RealmSubTeller / ReadonlyTeller /\n// ImpersonateTeller. Use this at any public entry point that accepts a\n// Teller from an external caller before invoking its methods.\n//\n// Foreign types — including embedding-based wrappers like\n// `type Evil struct { grc20.Teller }` — are rejected because type\n// assertions are nominal: *Evil is not *fnTeller, regardless of method\n// promotion. This is the reliable defense; the unexported-marker \"seal\"\n// pattern is bypassable via embedding (see\n// p/test/seal/filetests/z_seal_iface_embedding_filetest.gno).\n//\n// Mirrors the precedent of chain/banker.IsCanonical and\n// p/jaekwon/allowancesender's canonical-impl check.\nfunc IsCanonicalTeller(t Teller) bool {\n\t_, ok := t.(*fnTeller)\n\treturn ok\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"PD+RQRUYfI05vCXupe9352CIlkuq/jsxmfvbFQL8GgNnJjIrDpt1VcFMsuGEEzDepgEbJKeoGwtueKcSkmqJBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"sanitize","path":"gno.land/p/nt/markdown/sanitize/v0","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/markdown/sanitize/v0\"\ngno = \"0.9\"\n"},{"name":"sanitize.gno","body":"// Package sanitize provides input-cleaning primitives and safe-emit\n// builders for each markdown lexical slot. Realm authors wrap user-\n// supplied strings with these helpers before flowing them into rendered\n// markdown output. Each helper targets one specific slot (link text,\n// heading text, URL href, table cell, HTML attribute, fenced code block,\n// blockquote, footnote definition, link-reference definition, etc.) and\n// neutralizes the bytes that would otherwise let user content break out\n// of that slot or inject new top-level structure.\n//\n// Pick the right helper from the table under \"Picking the right helper\"\n// below, then wrap each user-supplied argument exactly once at the call\n// site (see \"The audit rule\").\n//\n// # Wrap once\n//\n// Most escapers and safe-emit builders in this package are NOT\n// idempotent — applying them twice re-escapes bytes the first pass\n// added (`\\*` becomes `\\\\\\*`, `\u0026amp;` becomes `\u0026amp;amp;`, a fenced\n// block gets re-fenced). Wrap each user-derived string with at most\n// one sanitize.* call. Block and BlockRich are exceptions —\n// idempotent by design — but the at-most-once rule is still the\n// safest default. See the \"Idempotence classes\" enumeration below\n// for the full breakdown.\n//\n// Some markdown-builder packages (e.g. p/moul/md) sanitize the args of\n// specific helpers internally — see each builder's package doc for the\n// per-helper contract. If the builder sanitizes for you, pass the raw\n// user input; if it doesn't, wrap the input with the right sanitize.*\n// helper at the call site.\n//\n// # Picking the right helper\n//\n// Match the helper to the slot the user content lands in:\n//\n//\tslot                                       helper\n//\t-------------------------------------------------------------\n//\t[text](url)                                InlineText (text)\n//\t# Heading text                             InlineText\n//\t**bold** _italic_                          InlineText\n//\t![alt](src)                                InlineText (alt)\n//\t\u003e [!NOTE] one-line title                   InlineText\n//\tmulti-paragraph post body                  Block\n//\tmulti-paragraph post body w/ rich block    BlockRich\n//\t  structure (headings, lists, tables, etc.)\n//\tmulti-line blockquote (`\u003e ` prefixed)      Blockquote\n//\tmulti-line blockquote w/ rich block body   BlockquoteRich\n//\t[text](url \"title\")                        LinkTitle (title)\n//\t| cell |                                   TableCell\n//\t\u003cgno-card caption=\"X\"\u003e                     HTMLEscape\n//\t\u003ch5\u003eX\u003c/h5\u003e                                 HTMLEscape\n//\tany URL going into ](X)                    URL\n//\tany image src going into (X)               ImageURL\n//\t`inline code` inside running prose         InlineCode\n//\tmulti-line fenced code block               CodeBlock\n//\tmulti-line fenced code with language tag   LanguageCodeBlock\n//\t[^name]: footnote body                     FootnoteDefinition\n//\t[label]: url \"title\" reference def         LinkReferenceDefinition\n//\tr/sys/users handle                         UserName       (validator)\n//\tg1.../gpub1... etc.                        BechString     (validator)\n//\tfootnote / LRD label / {#id} anchor name   FootnoteLabel  (validator)\n//\tfenced-code language tag                   LanguageName   (validator)\n//\tprefix arg to md.Nested                    NestedPrefix   (validator)\n//\n// # Invariants\n//\n// All helpers in this package are panic-free for any string input and\n// run in O(len(input)) time with bounded allocation.\n//\n// Idempotence classes:\n//\n//\tIdempotent (calling twice == calling once):\n//\t  StripBidiAndZeroWidth, NormalizeBreaks\n//\t  UserName, BechString, FootnoteLabel, LanguageName, NestedPrefix\n//\t  URL, ImageURL              (accept→identity; reject→\"\")\n//\t  Block                      (bracket walker treats \\[/\\] as ordinary;\n//\t                              line-leader escapes don't re-fire on\n//\t                              already-escaped `\\#` etc.)\n//\t  BlockRich                  (TrimLeft/TrimRight + \"\\n\\n\" wrap is stable)\n//\n//\tNOT idempotent — never wrap an already-sanitized string:\n//\t  InlineText, LinkTitle, TableCell   (re-escape backslashes)\n//\t  HTMLEscape                         (re-escapes `\u0026` → `\u0026amp;`)\n//\t  Blockquote, BlockquoteRich         (re-prefixes `\u003e `, nesting the quote each pass)\n//\t  InlineCode, CodeBlock,\n//\t  LanguageCodeBlock                  (wrap with a fence — calling twice double-wraps)\n//\t  FootnoteDefinition,\n//\t  LinkReferenceDefinition            (compose Block/InlineText/URL internally —\n//\t                                      passing already-sanitized strings double-escapes)\n//\n//\tCodeFence is pure: same inputs always give the same output.\n//\n// Validators (UserName / BechString / FootnoteLabel / LanguageName /\n// NestedPrefix) return either the cleaned input verbatim or \"\". They\n// never partially-sanitize: if the input doesn't match the slot's\n// charset/shape, the answer is rejection.\n//\n// # Composition rules\n//\n// Direct sanitize use (when emitting markdown without a builder package):\n//\n//\tout := \"# \" + sanitize.InlineText(userTitle) + \"\\n\\n\" +\n//\t       sanitize.Block(userBody)\n//\tout += sanitize.Blockquote(userQuote)\n//\tout += sanitize.LanguageCodeBlock(realmLang, userCode)\n//\n// Use with a builder package (e.g. p/moul/md): pass raw user input to\n// the builder helpers that sanitize internally — do NOT pre-wrap with\n// sanitize.*, or the input gets double-escaped (escapers are not\n// idempotent). See the builder's package doc for the per-helper\n// contract. For example, with p/moul/md:\n//\n//\tmd.Blockquote(userProse)                 // good — md.Blockquote sanitizes\n//\tmd.LanguageCodeBlock(realmLang, userCode) // good — sanitizes both args\n//\tmd.Link(userText, userURL)               // good — sanitizes both slots\n//\n//\tmd.Blockquote(sanitize.Block(userProse))         // BAD: double-wrap\n//\tmd.Link(sanitize.InlineText(t), sanitize.URL(u)) // BAD: double-wrap\n//\n// Wrong (across all callers):\n//\n//\tsanitize.InlineText(sanitize.InlineText(s))   double-wrap (re-escape)\n//\tsanitize.TableCell(sanitize.InlineText(s))    TableCell already calls InlineText\n//\tsanitize.URL(sanitize.InlineText(href))       inline-escape backslash-escapes `.` `-` `_`\n//\t                                              inside the URL, corrupting the host/path\n//\tsanitize.Blockquote(sanitize.Blockquote(s))   double-wrap — outer would escape the\n//\t                                              inner `\u003e ` prefixes\n//\tsanitize.Block(sanitize.BlockRich(s))         double-sanitize — strict Block re-escapes\n//\t                                              the markers BlockRich preserved (headings,\n//\t                                              lists, tables); BlockRich's rich structure\n//\t                                              renders as literal text after Block escapes\n//\t                                              its line-leaders\n//\tsanitize.BlockRich(sanitize.Block(s))         pointless double-sanitize — Block already\n//\t                                              escaped every line-leader to `\\#`/`\\\u003e`/etc.;\n//\t                                              BlockRich preserves the backslash escapes\n//\t                                              as visible artifacts in user prose\n//\tsanitize.Blockquote(sanitize.BlockRich(s))    double-sanitize — Blockquote's Block step\n//\t                                              re-escapes the markers BlockRich preserved\n//\tsanitize.BlockRich(sanitize.Blockquote(s))    nonsense — Blockquote already line-prefixed\n//\t                                              with `\u003e `; BlockRich expects raw user content\n//\tsanitize.BlockquoteRich(sanitize.BlockRich(s)) double-wrap — Rich + Rich nests twice\n//\tsanitize.BlockRich(sanitize.TableCell(s))     wrong slot — use TableCell for cell content,\n//\t                                              BlockRich for multi-paragraph block content\n//\tsanitize.TableCell(multiParagraphProse)       newlines fold to space silently; use a\n//\t                                              non-table layout for multi-paragraph text\n//\n// # Threat model\n//\n// Sanitizers in this package defend against:\n//\n//   - bidi/zero-width injection: invisible characters that make\n//     displayed text disagree with stored bytes (e.g. an address `g1abc...`\n//     that renders as `g1xyz...`, or a username that visually collides\n//     with another). Stripped by StripBidiAndZeroWidth, which runs as\n//     the first step of every text-shaped helper.\n//   - line-ending homoglyphs: CR-only and Unicode separators\n//     (U+0085 NEL, U+2028, U+2029) that some renderers treat as line\n//     breaks. Folded uniformly.\n//   - markdown-structure injection: user content opening a heading,\n//     blockquote, list, code fence, link-reference def, setext underline,\n//     gnoweb extension delimiter, or GFM table row at document level.\n//     Strict Block escapes the line-leading `|` of any GFM table row so\n//     user content cannot inject `\u003ctable\u003e`-shaped structure; permissive\n//     BlockRich preserves table rows so authors can compose `\u003ctable\u003e`\n//     elements (gnoweb loads extension.Table per render_config.go).\n//   - HTML block type 1-5 absorption: CommonMark §4.6 HTML block types 1\n//     (`\u003cscript\u003e`, `\u003cpre\u003e`, `\u003cstyle\u003e`, `\u003ctextarea\u003e`), 2 (`\u003c!--`), 3\n//     (`\u003c?`), 4 (`\u003c!UPPER`), and 5 (`\u003c![CDATA[`) do NOT close on a blank\n//     line — they only close on a type-specific token (`\u003c/tag\u003e`, `--\u003e`,\n//     `?\u003e`, `\u003e`, `]]\u003e`) or EOF. Without a defense, user content opening\n//     any of these would swallow realm chrome appended afterward. Both\n//     Block and BlockRich line-escape the openers (prepend `\\`) so the\n//     block never opens; this defense is unconditional in both modes.\n//     Types 6 and 7 close on a blank line, so BlockRich's `\\n\\n`\n//     paragraph envelope already bounds them and no escape is needed.\n//   - realm-discipline boundary (caller's responsibility, not enforced):\n//     callers should emit realm chrome at flush-left column 0 around\n//     `BlockRich(user)`. Indented chrome (4+ leading spaces, list-item\n//     continuations, footnote-definition body, or an unclosed Type 1\n//     HTML tag in realm chrome before the call) can extend across blank\n//     lines into user content or vice versa. The sanitizer cannot\n//     defend against malformed realm chrome — only against user input.\n//   - footnote / link-reference namespace pollution: user content\n//     containing `[^name]` or `[text][label]` syntax that would otherwise\n//     resolve against realm-defined footnote definitions or link\n//     reference definitions elsewhere on the page. Block escapes the\n//     opening `[` in both shapes.\n//   - reference-link / footnote-ref / shortcut-ref collisions:\n//     `[text][label]`, `[^name]`, and bare `[label]` shortcut forms\n//     are ALL neutralized by Block's bracket walk, which preserves\n//     only inline `[text](url)` and `![alt](src)` syntax — everything\n//     else has both `[` and `]` backslash-escaped, so the parser sees\n//     literal text and can't resolve against realm-defined LRDs or\n//     footnote definitions.\n//   - multi-line LRD evasion: Block's walker recognises `[lab\\nel]: url`\n//     across newlines (single `\\n` OK, blank line aborts) and strips\n//     the whole region. `\\]` inside the label is honored as an escaped\n//     literal, so `[label\\]: url` is NOT treated as an LRD (renders as\n//     literal text).\n//   - URL scheme abuse: javascript:, data:text/html, vbscript:, blob:,\n//     protocol-relative //, mailto: with prefill phishing parameters.\n//     Allowlist-only (URL / ImageURL).\n//   - HTML attribute / element breakout: `\"`, `\u003c`, `\u003e`, `\u0026`, `'` inside\n//     HTML lexical slots. Handled by HTMLEscape.\n//   - CommonMark §2.3 NUL: replaced with U+FFFD by Block, InlineText,\n//     LinkTitle, TableCell, HTMLEscape, InlineCode, CodeBlock, and\n//     LanguageCodeBlock.\n//   - code-fence leakage: a user-opened ``` ``` ``` fence that runs to EOF\n//     with no closing fence, which would otherwise swallow every realm-\n//     emitted line that follows. Block auto-closes any open fence at EOF.\n//   - table-alignment drift: tabs inside table cells expanding to variable\n//     widths (1-4 spaces depending on column position) and shifting cell\n//     boundaries unpredictably. TableCell replaces tabs with single spaces.\n//\n// What this package does NOT do:\n//\n//   - It does not store state. Every helper is a pure function.\n//   - It does not validate semantic correctness. sanitize.URL accepts\n//     a syntactically valid https:// URL even if the host is malicious;\n//     URL reputation is a separate layer.\n//   - It does not enforce CSS containment. ImageURL admits data:image/*\n//     URIs on the assumption that the deploying gnoweb instance caps\n//     rendered image dimensions via CSS. Without that cap, a malicious\n//     image can blow out the page layout or exhaust memory.\n//   - It does not perform structural sandboxing of foreign markdown.\n//     If a realm concatenates an opaque markdown blob returned from a\n//     polymorphic interface (`someThing.Render()`), it needs a structural\n//     sandbox primitive (e.g. a `\u003cgno-card\u003e` extension), not just leaf\n//     sanitization.\n//\n// # When to use Block vs BlockRich\n//\n// Both are safe sanitizers; both run identical realm-binding defenses.\n// They differ in what user-authored block structure survives:\n//\n//   - Block — paragraph-shaped only. Escapes `#`, `\u003e`, list markers,\n//     `---`/`***`/`___` thematic breaks, and `===`/`---` setext\n//     underlines. Use for leaf slots — footnote definition bodies,\n//     table cells, blockquote bodies (Blockquote uses Block), single-\n//     paragraph prose, any slot where richer structure has no benefit\n//     or where richer structure could visually impersonate realm chrome.\n//\n//   - BlockRich — full-richness. Preserves user-authored headings,\n//     lists, quotes, HR, setext. Use for user content the realm intends\n//     to compose with full block-level structure, typically inside a\n//     sandbox container (`\u003cgno-card\u003e`, `\u003cgno-foreign\u003e`) or a CSS-demoted\n//     region. BlockRich's qualifying-setext defense prevents the\n//     cross-boundary attack (user content reaching back to promote\n//     realm chrome to a heading), but inner-heading visual containment\n//     is the realm's CSS responsibility. gnoweb does not yet ship CSS\n//     rules that demote headings inside sandbox containers — until they\n//     land, BlockRich + sandbox renders inner headings at literal size.\n//\n// Do NOT compose Block and BlockRich in either direction. Pick one\n// helper at the right level.\n//\n// # Extending\n//\n// A new helper added to this package MUST:\n//\n//  1. Be panic-free for any string input.\n//  2. Strip bidi+zero-width before any other transform (so display\n//     equals storage end-to-end).\n//  3. Declare its idempotence class in the table above.\n//  4. Document the markdown / HTML lexical slot it targets.\n//  5. Reject rather than partially-sanitize when input is structurally\n//     invalid (return \"\" — never half-process an address or URL).\n//  6. Pick exactly one of the two return-value contracts and stick to\n//     it: escapers always return a transformed string and never reject\n//     (any input is OK — the transformation makes it safe); validators\n//     return the cleaned input verbatim on accept or \"\" on reject and\n//     never half-process. Mixing the contracts within one helper is a\n//     bug — callers can't reason about whether \"\" means \"input was\n//     already empty\" or \"input was rejected\".\npackage sanitize\n\nimport (\n\t\"chain/markdown\"\n\t\"html\"\n\t\"strings\"\n)\n\n// ----- Re-exports of the public chain/markdown natives -----\n//\n// These are general-purpose data-hygiene primitives, not markdown-specific.\n// The other helpers in this package call them internally, so realms emitting\n// markdown rarely need to call them directly. Reach for these when you have\n// a non-markdown use case — e.g. normalizing a username before storage,\n// canonicalizing a search query, or stripping invisible characters from\n// any user string that will be displayed or compared.\n\n// StripBidiAndZeroWidth removes Unicode bidi controls and zero-width\n// characters (U+200B-D, U+200E-F, U+202A-E, U+2066-9, U+FEFF) from s.\n// Use it when storing or comparing user-supplied strings outside of a\n// markdown context — for example, before saving a display name to state,\n// or before hashing a search query. Idempotent: calling twice gives the\n// same result.\n//\n// Thin wrapper over chain/markdown.StripBidiAndZeroWidth.\nfunc StripBidiAndZeroWidth(s string) string {\n\treturn markdown.StripBidiAndZeroWidth(s)\n}\n\n// NormalizeBreaks unifies CR-LF and lone CR to LF (CommonMark §2.2 line\n// endings only — does NOT touch U+2028/U+2029). Use it when comparing\n// or hashing user input that may have been authored on different\n// platforms (Windows CRLF vs. Unix LF), so equivalent strings normalize\n// to the same bytes. Idempotent.\n//\n// Thin wrapper over chain/markdown.NormalizeBreaks.\nfunc NormalizeBreaks(s string) string {\n\treturn markdown.NormalizeBreaks(s)\n}\n\n// ----- Escapers -----\n\n// InlineText prepares an arbitrary user string for an INLINE markdown\n// slot — anywhere the rendered output stays on a single line and lives\n// inside a larger markdown construct.\n//\n// Use for:\n//   - link text:        [InlineText(label)](url)\n//   - heading text:     # InlineText(title)\n//   - bold/italic body: **InlineText(name)**\n//   - image alt text:   ![InlineText(alt)](src)\n//   - single-line block-context slots:\n//     \u003e [!NOTE] InlineText(title)\n//     \u003e Author: InlineText(name)\n//\n// Multi-paragraph prose belongs in Block, not InlineText. InlineText\n// folds every newline to a single space (so paragraph structure is\n// erased) and escapes inline-active CommonMark punctuation:\n//\n//\t\\ * _ [ ] ( ) ~ \u003e - + . ! ` # \u003c \u0026\n//\n// Two characters are intentionally NOT escaped:\n//\n//   - `|` — only meaningful in GFM table rows. Leaving it literal here\n//     lets TableCell (which calls InlineText then escapes `|` itself)\n//     avoid double-escaping pipes into `\\\\|`.\n//   - `=` — only meaningful as a setext heading underline, which is a\n//     line-level construct. Escaping `=` inline would mangle expressions\n//     like `x = 1` for no benefit.\n//\n// Not idempotent (see package doc).\nfunc InlineText(s string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\ts = markdown.NormalizeBreaks(s)       // CM §2.2 \\r\\n / \\r → \\n\n\ts = foldNewlinesAndSeparators(s, ' ') // \\n + NEL + U+2028/U+2029 → space\n\treturn markdown.EscapeInline(s)\n}\n\n// Block prepares user content for a top-level BLOCK markdown context\n// where paragraphs, line breaks, code blocks, and other block structure\n// should survive — but where the content must NOT be able to inject\n// new top-level constructs (headings, lists, blockquotes,\n// link-reference definitions, setext underlines, gnoweb extension\n// delimiters, GFM table rows).\n//\n// Output shape: every non-empty result begins AND ends with \"\\n\\n\" —\n// CM §4.8 blank lines on both sides — so user content is guaranteed\n// to occupy its own paragraph(s), isolated from any realm chrome that\n// precedes OR follows it. This bounds CM §4.6 HTML block types 6 and\n// 7 (`\u003cdiv\u003e`, `\u003ctable\u003e`, `\u003cform\u003e`, arbitrary `\u003cfoo\u003e` tags) which\n// close on a blank line and are NOT escaped in any mode, and it\n// defeats first-line setext promotion (`===`/`---`) that strict-mode\n// escapes miss when the previous line is blank in the user input but\n// non-blank in the concatenated realm output. Empty input (or input\n// that strips entirely, e.g. a lone LRD) returns \"\" — no envelope is\n// emitted.\n//\n// Use for any multi-paragraph user-supplied prose that the realm\n// concatenates into its rendered output:\n//   - post bodies, comments, replies\n//   - profile bios, About sections\n//   - proposal descriptions, governance motions\n//   - changelog entries, release notes\n//\n// What Block does with each kind of attacker input:\n//\n//\tUser attempt                                          | Block's response\n//\t------------------------------------------------------|----------------------------------------------------\n//\t  --- preserved verbatim ---                          |\n//\t[text](url) inline link, ![alt](src) image            | preserved verbatim\n//\t------------------------------------------------------|----------------------------------------------------\n//\t  --- escaped / stripped / folded ---                 |\n//\t# heading at line-start                               | escaped → literal `# heading`\n//\t\u003e quoted at line-start                                | escaped → literal `\u003e`\n//\t- item, * item, + item, 1. item at line-start         | escaped\n//\t---, ***, ___ (3+) at line-start                      | escaped\n//\t=== or --- on its own line after non-blank text       | escaped (no setext promotion of the line above)\n//\t\u003cgno-card\u003e, \u003cgno-columns\u003e, any \u003cgno-…\u003e/\u003c/gno-…\u003e at    | escaped (wildcard match) → literal text\n//\t  line-start                                          |\n//\t| a | b | GFM table row (line-leading `|`)            | escaped → literal `| a | b |`\n//\t\u003c!--, \u003cscript\u003e, \u003cpre\u003e, \u003cstyle\u003e, \u003ctextarea\u003e, \u003c?…?\u003e,    | escaped (\\\u003c…) → literal text;\n//\t  \u003c!DOCTYPE…\u003e, \u003c![CDATA[…]]\u003e at line-start            |   blocks goldmark from opening a\n//\t  (CM §4.6 HTML block types 1-5)                      |   blank-line-NON-terminating HTML block\n//\t[text][realm-label] ref-link USE                      | both bracket pairs escaped → \\[text\\]\\[realm-label\\]\n//\t[^name] footnote-ref                                  | both brackets escaped → \\[^name\\]\n//\t[label] bare shortcut-ref                             | both brackets escaped → \\[label\\]\n//\t[label]: url link-reference definition                | whole region stripped (incl. multi-line label\n//\t  (incl. [lab\\nel]: url multi-line)                   |   `[lab\\nel]: url` and any title continuation)\n//\t[label\\]: url (backslash-escaped `]`)                 | NOT stripped; brackets escaped → paragraph text\n//\tcode fence opened without close                       | autoclosed at end of input\n//\tNUL byte (\\x00)                                       | replaced with U+FFFD\n//\tU+2028 / U+2029 / U+0085 (NEL)                        | folded to `\\n`\n//\tbidi/zero-width controls                              | stripped\n//\n// COMPOSITION GOTCHA: Block's EOF fence-autoclose appends a final\n// fence line. If you wrap Block's output with a line-prefixing\n// builder like md.Blockquote (which prepends `\u003e ` per line) or\n// md.Nested, that closing fence becomes a prefixed line. The output\n// is still safe (the fence still closes correctly) but may render\n// awkwardly. If pixel-perfect output matters, strip a trailing blank\n// fence line after Block.\n//\n// Why backslash and not a space for `\u003cgno-…\u003e` lines: gnoweb's\n// extension parsers call `util.TrimLeftSpace` on the line before tag\n// matching, which would strip a leading space and let the tag match\n// anyway. A leading `\\` survives the trim (only ASCII whitespace +\n// form-feed are stripped) and is consumed by the inline escape phase\n// before Type-7 HTML block detection can fire (Type-7 requires the\n// first non-whitespace char to be `\u003c`).\n//\n// Inline emphasis, code spans, inline links, and soft line breaks\n// within a paragraph are PRESERVED — users can format. Pipes that\n// are NOT at line-start stay literal so prose can still write things\n// like `a | b`.\n//\n// Idempotent: Block(Block(s)) is byte-identical to Block(s). The\n// bracket walker strips LRDs on the first pass; remaining `[`/`]`\n// outside inline-link/image spans are escaped to `\\[`/`\\]`, and\n// already-escaped brackets are preserved on subsequent passes\n// (pass-2 backslash-parity tracking). Still, wrap each user-supplied\n// string exactly once — chained sanitization adds no value and\n// burns gas.\nfunc Block(s string) string {\n\ts = markdown.NormalizeBreaks(s)\n\ts = markdown.StripBidiAndZeroWidth(s)\n\ts = replaceNULWithFFFD(s)\n\ts = markdown.EscapeBlockHazards(s)\n\t// Symmetric \"\\n\\n\" envelope — same pattern BlockRich uses for the\n\t// same reasons (see BlockRich docstring \"Cross-paragraph safety\").\n\t// Strict mode escapes most line-leading hazards (setext, GFM table\n\t// row, CM §4.6 HTML types 1-5, list/heading/HR markers), but two\n\t// hazards remain that only a blank-line break can close:\n\t//\n\t//   - CM §4.6 HTML block types 6 and 7 (`\u003cdiv\u003e`, `\u003ctable\u003e`,\n\t//     `\u003cform\u003e`, arbitrary `\u003cfoo\u003e` tags) are NOT escaped in any mode\n\t//     — they close on a blank line per CM. Without a trailing\n\t//     \"\\n\\n\", a `\u003cdiv\u003e` at the end of user content extends into\n\t//     appended realm chrome.\n\t//\n\t//   - First-line setext: strict mode's setext escape only fires\n\t//     when the previous line is non-blank IN THE USER'S INPUT.\n\t//     A user whose first line is `===` slips past, and concatenated\n\t//     after `chrome\\n` would promote chrome to H1. The leading\n\t//     \"\\n\\n\" forces a paragraph break so chrome cannot be merged.\n\t//\n\t// TrimLeft/TrimRight + fixed wrap is idempotent: Block(Block(s)) is\n\t// byte-identical to Block(s). Empty post-escape result short-\n\t// circuits to \"\" so realm concatenation doesn't leak stray blank\n\t// lines for trivially empty inputs (e.g. lone LRD that strips\n\t// entirely).\n\ts = strings.TrimLeft(s, \"\\n\")\n\ts = strings.TrimRight(s, \"\\n\")\n\tif s == \"\" {\n\t\treturn \"\"\n\t}\n\treturn \"\\n\\n\" + s + \"\\n\\n\"\n}\n\n// BlockRich is the permissive counterpart of Block. Both are safe\n// sanitizers — the distinction is what markdown structure survives:\n//\n//   - Block escapes line-leading block markers (`#`, `\u003e`, `-`, `*`,\n//     `+`, `1.`), thematic breaks (`---`/`***`/`___`), and setext\n//     underlines (`===`/`---`). User content becomes paragraph-shaped.\n//   - BlockRich PRESERVES all of those, so user content can compose\n//     headings, lists, quotes, horizontal rules, and setext-styled\n//     headings. Realm-binding defenses stay on (extension delimiters\n//     `\u003cgno-…\u003e`, the bracket walker for link / LRD / ref /\n//     footnote / shortcut, fence autoclose, NUL / bidi /\n//     Unicode-separator folding). GFM table-row openers are\n//     PRESERVED (see \"Tables\" below).\n//\n// Cross-paragraph safety: BlockRich's output begins with \"\\n\\n\"\n// AND ends with \"\\n\\n\" — CM §4.8 blank lines on both sides — so\n// user content is guaranteed to occupy its own paragraph(s),\n// isolated from anything the realm emits before OR after. Symmetric\n// isolation closes four distinct attacks:\n//\n//   - Cross-paragraph setext promotion (backward). User content\n//     `body\\n===\\nmore` concatenated after realm chrome (no\n//     trailing `\\n`) would, without paragraph isolation, place\n//     \"chrome\\nbody\" in one paragraph; the `===` setext underline\n//     would then promote that merged paragraph to H1, hijacking\n//     realm chrome. The leading \"\\n\\n\" forces a paragraph break.\n//\n//   - Cross-paragraph GFM table promotion (backward). User content\n//     beginning with `|---|---|` (a table delimiter row) would,\n//     without a blank-line break, retroactively turn the preceding\n//     realm line into a `\u003cthead\u003e`. Paragraph isolation prevents\n//     the table-detection scan from crossing the boundary.\n//\n//   - Cross-paragraph GFM table promotion (forward). Realm chrome\n//     appended immediately after BlockRich(user) that begins with\n//     `|---|` would, without a trailing blank line, extend user's\n//     last line into a table header and pull realm chrome into the\n//     body row. The trailing \"\\n\\n\" prevents the merge.\n//\n//   - Lazy paragraph continuation (forward). Paragraph-shaped realm\n//     chrome appended immediately after BlockRich(user) would, via\n//     CM §5.2, merge into user's trailing paragraph and inherit any\n//     block-level decoration it carries.\n//\n// First-line qualifying-setext escape (the\n// `neuterLeadingSetextIfQualifying` pre-pass) remains in place as\n// belt-and-suspenders: if the first non-blank line of user input\n// matches the CM §4.3 setext-underline pattern (run of `=` or `-`\n// with 0-3 leading spaces and only trailing whitespace), BlockRich\n// inserts `\\` before the first `=`/`-`. This is redundant given\n// paragraph isolation but harmless and inexpensive.\n//\n// # Tables\n//\n// BlockRich preserves line-leading `|` so user content can\n// compose GFM tables:\n//\n//\t| Header A | Header B |\n//\t|----------|----------|\n//\t| cell a   | cell b   |\n//\n// renders as a real `\u003ctable\u003e` element. Strict Block continues to\n// escape line-leading `|` (each row becomes literal `\\| a | b |`\n// text). When the realm authors the table itself and inserts user\n// content into a specific cell, use TableCell — NOT BlockRich —\n// to sanitize that cell value.\n//\n// What attacker input produces what (full table, same rows as Block\n// except where marked CHANGED):\n//\n//\tUser attempt                                  | BlockRich response\n//\t----------------------------------------------|--------------------------------------------------\n//\t  --- preserved (compose freely) ---          |\n//\t# heading at line-start                       | preserved [CHANGED from Block]\n//\t\u003e quoted at line-start                        | preserved [CHANGED]\n//\t- item, * item, + item, 1. item               | preserved [CHANGED]\n//\t---, ***, ___ thematic break                  | preserved [CHANGED]\n//\t=== or --- setext underline                   | preserved when preceded by user text;\n//\t                                              | escaped (\\===/\\---) if the first non-blank\n//\t                                              | line of input [CHANGED]\n//\t| a | b | GFM table row (line-leading |)      | preserved → renders as \u003ctable\u003e when followed by\n//\t                                              | a delimiter row [CHANGED]\n//\t[text](url), ![alt](src)                      | preserved verbatim [SAME]\n//\t----------------------------------------------|--------------------------------------------------\n//\t  --- escaped / stripped / folded ---         |\n//\t\u003cgno-card\u003e, any \u003cgno-…\u003e/\u003c/gno-…\u003e at line-start| escaped (wildcard match) [SAME]\n//\t\u003c!--, \u003cscript\u003e, \u003cpre\u003e, \u003cstyle\u003e, \u003ctextarea\u003e,   | escaped (\\\u003c…) [SAME] — Types 1-5 don't close\n//\t  \u003c?…?\u003e, \u003c!DOCTYPE…\u003e, \u003c![CDATA[…]]\u003e           |   on blank lines, so `\\n\\n` envelope\n//\t  at line-start (CM §4.6 HTML block types 1-5)|   doesn't isolate them; explicit escape\n//\t[text][realm-label] ref-link USE              | both pairs escaped [SAME]\n//\t[^name] footnote-ref                          | both brackets escaped [SAME]\n//\t[label] bare shortcut-ref                     | both brackets escaped [SAME]\n//\t[label]: url link-reference definition        | whole region stripped [SAME]\n//\t[label\\]: url (escaped `]`)                   | not stripped; brackets escaped [SAME]\n//\tcode fence opened without close               | autoclosed at end of input [SAME]\n//\tNUL byte (\\x00)                               | replaced with U+FFFD [SAME]\n//\tU+2028 / U+2029 / U+0085 (NEL)                | folded to `\\n` [SAME]\n//\tbidi/zero-width controls                      | stripped [SAME]\n//\n// Use BlockRich for user content the realm intends to compose with\n// full block-level richness — typically inside a sandbox container\n// (`\u003cgno-card\u003e`, `\u003cgno-foreign\u003e`) or a CSS-demoted region where inner\n// headings render visually distinct from realm chrome. The realm\n// must own the visual containment: concatenating BlockRich's output\n// directly into a top-level page still lets the user write `# heading`\n// at document level. BlockRich's cross-boundary setext defense prevents\n// the worst case (reaching backwards into realm bytes), but visual\n// containment of inner headings is the realm's CSS responsibility.\n// gnoweb does not yet ship CSS rules that demote inner headings inside\n// `\u003cgno-card\u003e` / `\u003cgno-foreign\u003e` — until those rules land, realms\n// using BlockRich + a sandbox should be aware that inner headings\n// render at their literal level.\n//\n// Idempotent: BlockRich(BlockRich(s)) is byte-identical to\n// BlockRich(s). The TrimLeft-then-\"\\n\\n\"-prepend pattern strips\n// any leading newlines and reapplies exactly two, so the leading\n// shape is stable across passes; the qualifying-setext escape is\n// stable (a line beginning with `\\` no longer matches the setext\n// pattern); and the bracket walker treats already-escaped\n// `\\[`/`\\]` as ordinary bytes. Empty input (or input that strips\n// to empty, e.g. a lone link-reference definition) returns \"\" —\n// realm concatenation doesn't get a stray blank line.\n// Still, wrap each user-supplied string exactly once — chained\n// sanitization adds no value and burns gas.\n//\n// Realm-discipline boundary: BlockRich defends user input against\n// every cross-paragraph attack listed above, but it CANNOT defend\n// against malformed REALM chrome. Specifically, callers should emit\n// realm chrome at flush-left column 0 around `BlockRich(user)`. If\n// the realm chrome BEFORE the call contains an unclosed CM §4.6\n// Type 1 HTML tag (`\u003cscript\u003e`, `\u003cpre\u003e`, `\u003cstyle\u003e`, `\u003ctextarea\u003e`),\n// the `\\n\\n` envelope does NOT close it (Type 1 closes only on the\n// matching close tag), and user-controlled `\u003c/tag\u003e` content can\n// then prematurely terminate it. Indented chrome (4+ leading\n// spaces, list-item continuations, footnote-definition body) can\n// likewise extend across the envelope into user content. Keep\n// chrome flush-left and Type 1 tags closed within the chrome.\n//\n// PREVIEW: BlockquoteRich is currently the only in-tree caller of\n// BlockRich; the API and the `\"\\n\\n\"` output shape may evolve once\n// direct callers emerge.\nfunc BlockRich(s string) string {\n\ts = markdown.NormalizeBreaks(s)\n\ts = markdown.StripBidiAndZeroWidth(s)\n\ts = replaceNULWithFFFD(s)\n\t// Fold Unicode separators (U+2028, U+2029, U+0085 NEL) to '\\n'\n\t// BEFORE the setext-qualifying check. The native\n\t// EscapeBlockHazardsRich also folds them internally, but the Gno\n\t// helper below needs to see the folded form to correctly identify\n\t// the first non-blank line — otherwise an attacker can hide the\n\t// `===` setext underline behind a U+2028 / U+2029 / U+0085 and\n\t// reach back to promote realm chrome above it.\n\ts = foldSeparatorsToNewline(s)\n\ts = neuterLeadingSetextIfQualifying(s)\n\ts = markdown.EscapeBlockHazardsRich(s)\n\t// Ensure the output BOTH starts AND ends with \"\\n\\n\" — CM §4.8\n\t// blank lines on each side — so user content is GUARANTEED to\n\t// occupy its own paragraph(s), isolated from anything the realm\n\t// emits before OR after. Symmetric isolation closes four attacks:\n\t//\n\t//  Backward (closed by leading \"\\n\\n\"):\n\t//   1. Deeper-setext: user content `body\\n===\\nmore` concatenated\n\t//      after realm chrome (no trailing `\\n`) would otherwise place\n\t//      \"chrome\\nbody\" in one paragraph; the `===` setext underline\n\t//      would then promote that merged paragraph to H1, hijacking\n\t//      realm chrome.\n\t//   2. GFM table-row promotion: user content beginning with\n\t//      `|---|---|` (a table delimiter row) would, without a blank-\n\t//      line break, retroactively promote the preceding realm line\n\t//      into a `\u003cthead\u003e` cell.\n\t//\n\t//  Forward (closed by trailing \"\\n\\n\"):\n\t//   3. GFM table-row promotion in reverse: realm appending its own\n\t//      chrome immediately after BlockRich(user), where chrome\n\t//      starts with `|---|`, would extend user's last line into a\n\t//      table header and pull realm chrome into the body row.\n\t//   4. Lazy paragraph continuation: realm appending paragraph-\n\t//      shaped chrome immediately after BlockRich(user) would, via\n\t//      CM §5.2 lazy-continuation, merge into user's trailing\n\t//      paragraph and inherit any block-level decoration it carries.\n\t//\n\t// `neuterLeadingSetextIfQualifying` above is now belt-and-\n\t// suspenders for the first-line setext case: even if the blank-\n\t// line guarantee were somehow defeated by an exotic CM consumer,\n\t// the first-line escape still blocks the simplest setext shape.\n\t//\n\t// Empty post-escape result short-circuits to \"\" so realm\n\t// concatenation doesn't leak stray blank lines for trivially empty\n\t// inputs (e.g. a lone link-reference definition that strips\n\t// entirely).\n\t//\n\t// Idempotency: TrimLeft and TrimRight strip ALL leading/trailing\n\t// \"\\n\"s, then the wrap adds exactly two on each side. Stable\n\t// across passes.\n\ts = strings.TrimLeft(s, \"\\n\")\n\ts = strings.TrimRight(s, \"\\n\")\n\tif s == \"\" {\n\t\treturn \"\"\n\t}\n\treturn \"\\n\\n\" + s + \"\\n\\n\"\n}\n\n// foldSeparatorsToNewline replaces U+0085 NEL (0xC2 0x85),\n// U+2028 (0xE2 0x80 0xA8), and U+2029 (0xE2 0x80 0xA9) with '\\n'.\n// Leaves '\\n' bytes alone. Used by BlockRich so the qualifying-setext\n// pre-pass and the native both see the same line structure.\nfunc foldSeparatorsToNewline(s string) string {\n\t// Cheap pre-check: only the 0xC2 / 0xE2 lead bytes can trigger.\n\tif !containsAnyByteForFold(s) {\n\t\treturn s\n\t}\n\tout := make([]byte, 0, len(s))\n\tfor i := 0; i \u003c len(s); {\n\t\tc := s[i]\n\t\tif c == 0xC2 \u0026\u0026 i+1 \u003c len(s) \u0026\u0026 s[i+1] == 0x85 {\n\t\t\tout = append(out, '\\n')\n\t\t\ti += 2\n\t\t\tcontinue\n\t\t}\n\t\tif c == 0xE2 \u0026\u0026 i+2 \u003c len(s) \u0026\u0026 s[i+1] == 0x80 \u0026\u0026 (s[i+2] == 0xA8 || s[i+2] == 0xA9) {\n\t\t\tout = append(out, '\\n')\n\t\t\ti += 3\n\t\t\tcontinue\n\t\t}\n\t\tout = append(out, c)\n\t\ti++\n\t}\n\treturn string(out)\n}\n\nfunc containsAnyByteForFold(s string) bool {\n\tfor i := 0; i \u003c len(s); i++ {\n\t\tif s[i] == 0xC2 || s[i] == 0xE2 {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// neuterLeadingSetextIfQualifying scans s for the first non-blank\n// line. If that line matches the CommonMark §4.3 setext-underline\n// pattern (0-3 leading spaces, then a run of all `=` or all `-`,\n// then optional trailing whitespace, then `\\n` or EOF), the function\n// returns s with a `\\` inserted before the first `=`/`-`. Otherwise\n// returns s unchanged. The escape prevents a realm-emitted line above\n// BlockRich's output from being retroactively promoted to a heading.\nfunc neuterLeadingSetextIfQualifying(s string) string {\n\tpos := 0\n\tfor pos \u003c len(s) {\n\t\t// Walk to the first non-whitespace byte of the current line.\n\t\tlineStart := pos\n\t\ti := pos\n\t\tfor i \u003c len(s) \u0026\u0026 (s[i] == ' ' || s[i] == '\\t') {\n\t\t\ti++\n\t\t}\n\t\tif i \u003e= len(s) || s[i] == '\\n' {\n\t\t\t// Blank line; advance to next line.\n\t\t\tif i \u003e= len(s) {\n\t\t\t\treturn s\n\t\t\t}\n\t\t\tpos = i + 1\n\t\t\tcontinue\n\t\t}\n\t\t// First non-blank line. Check setext-underline shape.\n\t\tif i-lineStart \u003e 3 {\n\t\t\treturn s // 4+ leading spaces = indented code, not setext\n\t\t}\n\t\tc := s[i]\n\t\tif c != '=' \u0026\u0026 c != '-' {\n\t\t\treturn s // not a setext underline candidate\n\t\t}\n\t\tj := i + 1\n\t\tfor j \u003c len(s) \u0026\u0026 s[j] == c {\n\t\t\tj++\n\t\t}\n\t\tfor j \u003c len(s) \u0026\u0026 (s[j] == ' ' || s[j] == '\\t') {\n\t\t\tj++\n\t\t}\n\t\tif j \u003c len(s) \u0026\u0026 s[j] != '\\n' {\n\t\t\treturn s // mixed content on the line — not setext\n\t\t}\n\t\treturn s[:i] + \"\\\\\" + s[i:]\n\t}\n\treturn s\n}\n\n// Blockquote wraps user content as a CommonMark blockquote: each line\n// of the cleaned content gets a \"\u003e \" prefix so the renderer displays\n// it inside a `\u003cblockquote\u003e` element.\n//\n// Use for any multi-paragraph user-supplied text that the realm wants\n// to render as a quotation: cited posts, attached responses, error\n// snapshots that should visually stand out.\n//\n// The content is first cleaned by Block (bidi-strip, line-ending\n// normalize, NUL→U+FFFD, bracket walker for link/image/LRD spans,\n// block-marker escape, code-fence auto-close at EOF, Unicode-separator\n// fold). Block's \"\\n\\n\" cross-paragraph envelope is then stripped —\n// the `\u003e ` marker creates the container boundary, so the envelope\n// would only line-prefix to empty `\u003e ` lines top and bottom — and\n// every remaining line is prefixed with \"\u003e \". The user content can\n// still use inline emphasis, code spans, and nested fenced code blocks\n// inside the quote; what it cannot do is open new top-level structure\n// (heading, list, blockquote, GFM table row, etc.) or escape the\n// quote.\n//\n// Output shape — every non-empty result begins with \"\\n\" and ends\n// with \"\\n\\n\" (same shape as BlockquoteRich):\n//\n//   - Leading \"\\n\" guarantees a clean blockquote opener even when the\n//     realm concatenates `chrome + Blockquote(user)` without its own\n//     newline separator.\n//   - Trailing \"\\n\\n\" (blank line) cleanly ends the blockquote so a\n//     realm appending `Blockquote(user) + chrome` cannot pull chrome\n//     bytes into the quote via CommonMark §5.2 lazy continuation.\n//\n// Empty input (or input that strips entirely, e.g. a lone LRD)\n// returns \"\" — no blockquote is emitted.\n//\n// Composition gotcha: Block's EOF code-fence auto-close (added when\n// user content opens a ``` ``` ``` fence without closing it) becomes a\n// \"\u003e ```\" line at the end of the blockquote. Goldmark parses this\n// correctly as the close of a fenced block inside the quote — the\n// output is structurally safe — but the markdown source looks unusual\n// to a human reviewer. If aesthetic output matters, ensure user\n// content closes its own fences.\n//\n// Not idempotent (see package doc): wraps with `\u003e ` per line, so\n// calling twice double-wraps and the outer call's Block step escapes\n// the inner `\u003e` prefixes.\n//\n// Do NOT compose with BlockRich in either direction:\n//   - Blockquote(BlockRich(s)) double-sanitizes: BlockRich preserves\n//     `#`/`\u003e`/etc., then Blockquote's Block step escapes them again.\n//   - BlockRich(Blockquote(s)) doesn't make sense: Blockquote already\n//     line-prefixed with `\u003e `; BlockRich expects raw user content.\n//\n// For a quoted body that can contain headings, lists, nested quotes,\n// or thematic breaks, use BlockquoteRich.\nfunc Blockquote(text string) string {\n\ttext = Block(text)\n\t// Block wraps its output with \"\\n\\n\" on each side for cross-\n\t// paragraph isolation. Inside a blockquote both wraps are redundant\n\t// — the `\u003e ` marker creates the container boundary — and they\n\t// would line-prefix to two useless `\u003e ` empty quoted lines top and\n\t// bottom. Strip ALL leading and trailing \"\\n\"s so the body starts\n\t// and ends clean; this helper re-wraps with `\\n` + body + `\\n\\n`\n\t// below (same shape as BlockquoteRich).\n\ttext = strings.TrimLeft(text, \"\\n\")\n\tif text == \"\" {\n\t\treturn \"\"\n\t}\n\ttext = strings.TrimRight(text, \"\\n\")\n\tif text == \"\" {\n\t\treturn \"\"\n\t}\n\tvar sb strings.Builder\n\tsb.WriteByte('\\n')\n\tfor _, line := range strings.Split(text, \"\\n\") {\n\t\tsb.WriteString(\"\u003e \")\n\t\tsb.WriteString(line)\n\t\tsb.WriteByte('\\n')\n\t}\n\tsb.WriteByte('\\n')\n\treturn sb.String()\n}\n\n// BlockquoteRich is the permissive counterpart of Blockquote. Both\n// wrap user content as a CommonMark blockquote (each line prefixed\n// with `\u003e `), but they differ in what block-level structure inside\n// the quote survives:\n//\n//   - Blockquote escapes line-leading block markers, so the quoted\n//     body is paragraph-shaped — `# x` inside a Blockquote stays a\n//     literal `#`.\n//   - BlockquoteRich PRESERVES line-leading block markers, so the\n//     quoted body can compose ATX headings, lists, thematic breaks,\n//     nested blockquotes (`\u003e \u003e nested`), and other block-level\n//     structure. Realm-binding defenses stay on (extension delimiters,\n//     GFM table-row openers, bracket walker, fence autoclose,\n//     NUL / bidi / Unicode-separator folding).\n//\n// Output shape — every non-empty result begins with \"\\n\" and ends\n// with \"\\n\\n\":\n//\n//   - Leading \"\\n\" guarantees a clean blockquote opener even when the\n//     realm concatenates `chrome + BlockquoteRich(user)` without its\n//     own newline separator. Without the leading \"\\n\", chrome ending\n//     mid-line followed by \"\u003e quoted\" would render `\u003e` as literal\n//     paragraph text instead of opening a blockquote.\n//   - Trailing \"\\n\\n\" (blank line) cleanly ends the blockquote so a\n//     realm appending `BlockquoteRich(user) + chrome` cannot pull\n//     chrome bytes into the quote via CommonMark §5.2 lazy\n//     continuation. Without the trailing blank line, paragraph chrome\n//     immediately after BlockquoteRich would render inside the quote.\n//   - BlockRich's own leading \"\\n\\n\" (paragraph-isolation blank line)\n//     is stripped before line-prefixing — otherwise the output would\n//     carry one or two redundant empty `\u003e ` quoted lines at the top.\n//     A single \"\\n\" is then re-prepended at the BlockquoteRich\n//     boundary so `chrome + BlockquoteRich(user)` still lands the\n//     first `\u003e` at column 0.\n//   - The cross-boundary setext defense BlockRich provides is\n//     redundant inside a blockquote: a setext underline inside `\u003e `\n//     content can only promote a line in the same blockquote, never\n//     reach realm bytes (different CM container). BlockRich still\n//     applies it, harmlessly.\n//\n// What attacker input produces what (rows that differ from\n// Blockquote are marked CHANGED):\n//\n//\tUser attempt                                  | BlockquoteRich response\n//\t----------------------------------------------|------------------------------------------------\n//\t  --- preserved inside `\u003e ` quote ---         |\n//\t# heading                                     | preserved as `\u003e # heading` [CHANGED]\n//\t\u003e nested quote                                | preserved as `\u003e \u003e nested quote` [CHANGED]\n//\t- item, * item, + item, 1. item               | preserved as `\u003e - item` etc. [CHANGED]\n//\t---, ***, ___ thematic break                  | preserved [CHANGED]\n//\t=== or --- setext underline                   | preserved when preceded by user text;\n//\t                                              | escaped (\\===/\\---) if first non-blank\n//\t                                              | line of input [CHANGED]\n//\t| a | b | GFM table row (line-leading |)      | preserved → renders as \u003ctable\u003e inside the\n//\t                                              | blockquote when followed by a delimiter row [CHANGED]\n//\t[text](url), ![alt](src)                      | preserved verbatim [SAME]\n//\t----------------------------------------------|------------------------------------------------\n//\t  --- escaped / stripped / folded ---         |\n//\t\u003cgno-card\u003e, any \u003cgno-…\u003e/\u003c/gno-…\u003e at line-start| escaped (wildcard match) [SAME]\n//\t\u003c!--, \u003cscript\u003e, \u003cpre\u003e, \u003cstyle\u003e, \u003ctextarea\u003e,   | escaped (\\\u003c…) [SAME] — CM §4.6 Types 1-5\n//\t  \u003c?…?\u003e, \u003c!DOCTYPE…\u003e, \u003c![CDATA[…]]\u003e           |   don't close on blank lines; without escape\n//\t  at line-start                               |   they would swallow chrome past the `\u003e ` quote\n//\t[text][realm-label] ref-link USE              | both pairs escaped [SAME]\n//\t[^name] footnote-ref                          | both brackets escaped [SAME]\n//\t[label] bare shortcut-ref                     | both brackets escaped [SAME]\n//\t[label]: url link-reference definition        | whole region stripped [SAME]\n//\tcode fence opened without close               | autoclosed at end of input [SAME]\n//\tNUL byte (\\x00)                               | replaced with U+FFFD [SAME]\n//\tU+2028 / U+2029 / U+0085 (NEL)                | folded to `\\n` [SAME]\n//\tbidi/zero-width controls                      | stripped [SAME]\n//\n// Use BlockquoteRich when the realm wants to render user content as\n// a quotation that itself reads like authored markdown — the visual\n// CSS containment of `\u003cblockquote\u003e` already demotes inner headings\n// relative to realm chrome, so the \"inner headings need a sandbox\"\n// caveat that applies to BlockRich at top level does not apply here.\n//\n// Not idempotent: like Blockquote, calling twice double-wraps —\n// `BlockquoteRich(BlockquoteRich(s))` produces `\u003e \u003e content`,\n// nesting the quote a level deeper each pass.\n//\n// Empty input (or input that reduces to nothing after BlockRich,\n// e.g. a lone link-reference definition) returns \"\" — no blockquote\n// is emitted and neither the leading \"\\n\" nor the trailing \"\\n\\n\"\n// shape applies.\nfunc BlockquoteRich(text string) string {\n\ttext = BlockRich(text)\n\t// BlockRich wraps user content with \"\\n\\n\" on each side for\n\t// cross-paragraph isolation. Inside a blockquote both wraps are\n\t// redundant — the `\u003e ` marker creates the container boundary —\n\t// and they would line-prefix to two useless `\u003e ` empty quoted\n\t// lines top and bottom. Strip ALL leading and trailing \"\\n\"s so\n\t// the body starts and ends clean; this helper re-wraps with `\\n`\n\t// + body + `\\n\\n` below.\n\ttext = strings.TrimLeft(text, \"\\n\")\n\tif text == \"\" {\n\t\treturn \"\"\n\t}\n\t// Strip ALL trailing newlines so the loop produces exactly one\n\t// `\u003e line` per content line, then append `\\n\\n` at the end so the\n\t// blockquote terminates cleanly (see \"Output shape\" above).\n\ttext = strings.TrimRight(text, \"\\n\")\n\tif text == \"\" {\n\t\treturn \"\"\n\t}\n\tvar sb strings.Builder\n\t// Leading \"\\n\" so `chrome + BlockquoteRich(user)` cannot land the\n\t// first `\u003e` mid-line.\n\tsb.WriteByte('\\n')\n\tfor _, line := range strings.Split(text, \"\\n\") {\n\t\tsb.WriteString(\"\u003e \")\n\t\tsb.WriteString(line)\n\t\tsb.WriteByte('\\n')\n\t}\n\t// Trailing blank line so `BlockquoteRich(user) + chrome` cannot\n\t// pull chrome into the quote via lazy continuation.\n\tsb.WriteByte('\\n')\n\treturn sb.String()\n}\n\n// LinkTitle prepares user content for a CommonMark link-title or\n// image-title slot — the optional quoted text after the URL in any of\n// these forms:\n//\n//\t[text](url \"TITLE\")\n//\t![alt](src \"TITLE\")\n//\t[label]: url \"TITLE\"\n//\n// Escapes the inline-active set plus `\"` and `'` (the title delimiters\n// that aren't already in the inline set; `(` and `)` are), so the\n// caller can choose any of the three title-quote styles safely.\n//\n// Pick the right helper for the slot — markdown title and HTML\n// attribute share the look but use different escape rules:\n//\n//\t[text](url \"X\")              → LinkTitle      (markdown title)\n//\t\u003ca title=\"X\"\u003e                → HTMLEscape     (HTML attribute)\n//\t\u003ch5\u003eX\u003c/h5\u003e                   → HTMLEscape     (HTML element body)\n//\n// Swapping HTMLEscape for LinkTitle is wrong: HTML's `\u0026amp;` written\n// inside a markdown title renders as the literal characters `\u0026amp;`.\n// Swapping LinkTitle for HTMLEscape is wrong: markdown's `\\\"` survives\n// into the rendered HTML as a literal backslash-quote.\n//\n// Not idempotent (see package doc).\nfunc LinkTitle(s string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\ts = markdown.NormalizeBreaks(s)\n\ts = foldNewlinesAndSeparators(s, ' ')\n\treturn markdown.EscapeTitle(s)\n}\n\n// TableCell prepares user content for a GFM table cell — the bytes\n// between two `|` column delimiters in a table row like\n// `| cell-a | cell-b | cell-c |`. An unescaped `|` inside cell\n// content would open a new column, letting a malicious user shift\n// every column to its right.\n//\n// On top of InlineText's behavior, TableCell:\n//   - escapes `|` to `\\|` so user content can't end the cell early.\n//   - replaces tabs with single spaces. CommonMark expands tabs to\n//     the next multiple-of-4 column boundary (variable 1-4 spaces),\n//     which would shift the displayed cell-content width unpredictably\n//     and confuse table alignment.\n//\n// Not idempotent (see package doc).\nfunc TableCell(s string) string {\n\ts = InlineText(s)\n\ts = strings.ReplaceAll(s, \"\\t\", \" \")\n\ts = strings.ReplaceAll(s, \"|\", `\\|`)\n\treturn s\n}\n\n// HTMLEscape prepares user content for an HTML lexical slot inside\n// markdown — covers attribute values, element bodies, and HTML\n// comment bodies:\n//\n//\t\u003cgno-card type=\"...\" caption=\"X\"\u003e         attribute value\n//\t\u003cgno-alert title=\"X\"\u003e                     attribute value\n//\t\u003ch5\u003eX\u003c/h5\u003e                                element body\n//\t\u003cdetails\u003e\u003csummary\u003eX\u003c/summary\u003e...          element body\n//\t\u003c!-- X --\u003e                                comment body (safe: `\u003e`\n//\t                                          becomes `\u0026gt;`, so user\n//\t                                          cannot inject `--\u003e`)\n//\n// HTMLEscape escapes the union of attribute-breaking and body-breaking\n// characters (`\u003c`, `\u003e`, `\u0026`, `\"`, `'`), so one function safely serves\n// every HTML lexical context. Callers don't have to remember which\n// subset to use for which slot.\n//\n// Pick the right helper — markdown title and HTML attribute share\n// the look but use different escape rules:\n//\n//\t[text](url \"X\")              → LinkTitle      (markdown title)\n//\t\u003cspan title=\"X\"\u003e             → HTMLEscape     (HTML attribute)\n//\t\u003ch5\u003eX\u003c/h5\u003e                   → HTMLEscape     (HTML element body)\n//\n// Swapping InlineText for HTMLEscape is wrong: markdown's backslash\n// escapes survive into the rendered HTML as literal `\\*`. Swapping\n// LinkTitle for HTMLEscape is also wrong: `\u0026amp;` written inside a\n// markdown title renders as the literal characters `\u0026amp;`.\n//\n// Not idempotent (see package doc): calling twice produces\n// `\u0026amp;` → `\u0026amp;amp;`.\nfunc HTMLEscape(s string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\ts = markdown.NormalizeBreaks(s)\n\ts = foldNewlinesAndSeparators(s, ' ')\n\ts = replaceNULWithFFFD(s)\n\treturn html.EscapeString(s)\n}\n\n// ----- URL filters -----\n\n// URL validates a URL for use as a link href, percent-encodes unsafe\n// bytes, and rejects anything outside the allowlist of schemes.\n//\n// Allowlist:\n//   - http, https\n//   - mailto (rejected if it carries any query: prefill phishing via\n//     body, subject, cc, etc. Both '?' and '\u0026' are rejected; see\n//     linkSchemeAllowed for why '\u0026' counts.)\n//   - any URL WITHOUT a scheme — relative paths (`/path`, `./rel`,\n//     `bare-path`), query-only (`?q=v`), fragment-only (`#anchor`).\n//     A `:` appearing inside the URL (e.g. `/path:foo`, `?q=a:b`) is\n//     NOT a scheme separator per RFC 3986 — only `:` immediately after\n//     a leading `[a-zA-Z][a-zA-Z0-9+.-]*` counts.\n//\n// Rejected (have an unknown scheme):\n//   - javascript:, data:, vbscript:, blob:, file:, etc.\n//   - `//host/...` (protocol-relative — tracking-pixel vector)\n//\n// Returns \"\" if the URL is empty after trim or fails the allowlist.\nfunc URL(s string) string {\n\ts = strings.TrimSpace(s)\n\tif s == \"\" {\n\t\treturn \"\"\n\t}\n\tif !linkSchemeAllowed(s) {\n\t\treturn \"\"\n\t}\n\treturn markdown.PercentEncodeURL(s)\n}\n\n// ImageURL validates a URL for use as an image src. Kept separate from\n// URL — not a parameterized variant — because the allowlist shapes\n// differ qualitatively (data:image/* vs. mailto:) and a single boolean\n// flag would invite callers to pass the wrong default.\n//\n// Allowlist:\n//   - http, https\n//   - schemeless relative URLs starting with /, ./, or ..\n//     (rejects // protocol-relative — tracking-pixel vector)\n//   - data:image/svg+xml, data:image/png, data:image/jpeg,\n//     data:image/gif, data:image/webp\n//\n// Any other data: subtype is rejected — data:text/html etc. would\n// render as inline HTML and execute embedded scripts.\n//\n// DEPLOYMENT PRECONDITION: data: URIs encode the bytes of the image\n// directly into the markup, so a malicious sender can construct an\n// image whose pixel dimensions are arbitrarily large at minimal byte\n// cost. The deploying gnoweb instance MUST clamp rendered image\n// dimensions via CSS (e.g. `max-width: 100%; max-height: \u003cbound\u003e`).\n// Without that cap, a single image can blow out the page layout or\n// exhaust the browser's memory.\n//\n// Returns \"\" if the URL is empty after trim or fails the allowlist.\nfunc ImageURL(s string) string {\n\ts = strings.TrimSpace(s)\n\tif s == \"\" {\n\t\treturn \"\"\n\t}\n\tif !imageSchemeAllowed(s) {\n\t\treturn \"\"\n\t}\n\treturn markdown.PercentEncodeURL(s)\n}\n\n// ----- Validators -----\n\n// userNameCharsets builds the [2]uint64 bitmaps for the r/sys/users\n// charset: first [a-z], rest [a-z0-9_-]. Initialized once at package\n// init.\nvar (\n\tuserNameFirstLo, userNameFirstHi           uint64\n\tuserNameRestLo, userNameRestHi             uint64\n\tfootnoteLabelFirstLo, footnoteLabelFirstHi uint64\n\tfootnoteLabelRestLo, footnoteLabelRestHi   uint64\n\tlangFirstLo, langFirstHi                   uint64\n\tlangRestLo, langRestHi                     uint64\n\tbechHrpFirstLo, bechHrpFirstHi             uint64\n\tbechHrpRestLo, bechHrpRestHi               uint64\n\tbechDataFirstLo, bechDataFirstHi           uint64\n\tbechDataRestLo, bechDataRestHi             uint64\n)\n\nfunc init() {\n\t// UserName: first [a-z], rest [a-z0-9_-].\n\tfor c := byte('a'); c \u003c= 'z'; c++ {\n\t\tsetBit(\u0026userNameFirstLo, \u0026userNameFirstHi, c)\n\t\tsetBit(\u0026userNameRestLo, \u0026userNameRestHi, c)\n\t}\n\tfor c := byte('0'); c \u003c= '9'; c++ {\n\t\tsetBit(\u0026userNameRestLo, \u0026userNameRestHi, c)\n\t}\n\tsetBit(\u0026userNameRestLo, \u0026userNameRestHi, '_')\n\tsetBit(\u0026userNameRestLo, \u0026userNameRestHi, '-')\n\n\t// FootnoteLabel: [A-Za-z0-9_-] for both first and rest.\n\tfor c := byte('A'); c \u003c= 'Z'; c++ {\n\t\tsetBit(\u0026footnoteLabelFirstLo, \u0026footnoteLabelFirstHi, c)\n\t\tsetBit(\u0026footnoteLabelRestLo, \u0026footnoteLabelRestHi, c)\n\t}\n\tfor c := byte('a'); c \u003c= 'z'; c++ {\n\t\tsetBit(\u0026footnoteLabelFirstLo, \u0026footnoteLabelFirstHi, c)\n\t\tsetBit(\u0026footnoteLabelRestLo, \u0026footnoteLabelRestHi, c)\n\t}\n\tfor c := byte('0'); c \u003c= '9'; c++ {\n\t\tsetBit(\u0026footnoteLabelFirstLo, \u0026footnoteLabelFirstHi, c)\n\t\tsetBit(\u0026footnoteLabelRestLo, \u0026footnoteLabelRestHi, c)\n\t}\n\tfor _, c := range []byte{'_', '-'} {\n\t\tsetBit(\u0026footnoteLabelFirstLo, \u0026footnoteLabelFirstHi, c)\n\t\tsetBit(\u0026footnoteLabelRestLo, \u0026footnoteLabelRestHi, c)\n\t}\n\n\t// LanguageName: [a-zA-Z0-9_+-] for both first and rest.\n\tfor c := byte('A'); c \u003c= 'Z'; c++ {\n\t\tsetBit(\u0026langFirstLo, \u0026langFirstHi, c)\n\t\tsetBit(\u0026langRestLo, \u0026langRestHi, c)\n\t}\n\tfor c := byte('a'); c \u003c= 'z'; c++ {\n\t\tsetBit(\u0026langFirstLo, \u0026langFirstHi, c)\n\t\tsetBit(\u0026langRestLo, \u0026langRestHi, c)\n\t}\n\tfor c := byte('0'); c \u003c= '9'; c++ {\n\t\tsetBit(\u0026langFirstLo, \u0026langFirstHi, c)\n\t\tsetBit(\u0026langRestLo, \u0026langRestHi, c)\n\t}\n\tfor _, c := range []byte{'_', '+', '-'} {\n\t\tsetBit(\u0026langFirstLo, \u0026langFirstHi, c)\n\t\tsetBit(\u0026langRestLo, \u0026langRestHi, c)\n\t}\n\n\t// Bech HRP (when prefix==\"\"): [a-z], 1-16 chars.\n\tfor c := byte('a'); c \u003c= 'z'; c++ {\n\t\tsetBit(\u0026bechHrpFirstLo, \u0026bechHrpFirstHi, c)\n\t\tsetBit(\u0026bechHrpRestLo, \u0026bechHrpRestHi, c)\n\t}\n\n\t// Bech data part: [a-z0-9], 6-90 chars.\n\tfor c := byte('a'); c \u003c= 'z'; c++ {\n\t\tsetBit(\u0026bechDataFirstLo, \u0026bechDataFirstHi, c)\n\t\tsetBit(\u0026bechDataRestLo, \u0026bechDataRestHi, c)\n\t}\n\tfor c := byte('0'); c \u003c= '9'; c++ {\n\t\tsetBit(\u0026bechDataFirstLo, \u0026bechDataFirstHi, c)\n\t\tsetBit(\u0026bechDataRestLo, \u0026bechDataRestHi, c)\n\t}\n}\n\nfunc setBit(lo, hi *uint64, c byte) {\n\tif c \u003c 64 {\n\t\t*lo |= 1 \u003c\u003c c\n\t} else {\n\t\t*hi |= 1 \u003c\u003c (c - 64)\n\t}\n}\n\n// UserName validates the r/sys/users-registration charset:\n// ^[a-z][a-z0-9]*([_-][a-z0-9]+)*$ length ≤ 64.\n//\n// The native MatchCharsetN enforces the leading-letter + tail-charset\n// shape and length bound; this helper also performs the bidi-strip\n// pre-pass. The \"no consecutive [_-]\" rule from r/sys/users is NOT\n// enforced here (it's a registration-policy rule, not a sanitization\n// concern — registrations go through r/sys/users itself).\n//\n// Returns the (bidi-stripped) input if valid, \"\" otherwise. On a \"\"\n// return, do not emit the user-mention markup at all (e.g. skip the\n// `[@user](/u/user)` link); falling back to the raw user-supplied\n// string would defeat the validation.\nfunc UserName(s string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\tif markdown.MatchCharsetN(s, userNameFirstLo, userNameFirstHi, userNameRestLo, userNameRestHi, 1, 64) {\n\t\treturn s\n\t}\n\treturn \"\"\n}\n\n// BechString validates a bech32-style address-like string.\n//\n// A bech32 string has the shape `\u003chrp\u003e1\u003cdata\u003e`: a human-readable\n// prefix (HRP) that names the family (e.g. `g` for gno addresses,\n// `gpub` for gno pubkeys, `cosmos` for cosmos addresses), the\n// separator character `1`, then a data part carrying the encoded\n// payload as lowercase alphanumerics.\n//\n// If prefix != \"\", requires s to start with prefix+\"1\" exactly, and the\n// data part to match ^[a-z0-9]{6,90}$. Use this when you know the\n// expected family:\n//\n//\tsanitize.BechString(addr, \"g\")     // only g1...     (addresses)\n//\tsanitize.BechString(pk,   \"gpub\")  // only gpub1...  (pubkeys)\n//\n// If prefix == \"\", accepts any reasonable bech32 shape:\n// ^[a-z]{1,16}1[a-z0-9]{6,90}$.\n//\n// Syntactic only — does NOT verify the bech32 checksum. Use a true\n// bech32 decoder if you need that. Returns the cleaned input on\n// accept, \"\" on reject; on \"\" return, do not emit the address-link\n// markup (the user-supplied bytes have failed shape validation and\n// should not appear unmodified in output).\nfunc BechString(s, prefix string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\tif s == \"\" {\n\t\treturn \"\"\n\t}\n\tif prefix != \"\" {\n\t\t// HRP must be lowercase ASCII letters.\n\t\tfor i := 0; i \u003c len(prefix); i++ {\n\t\t\tc := prefix[i]\n\t\t\tif c \u003c 'a' || c \u003e 'z' {\n\t\t\t\treturn \"\"\n\t\t\t}\n\t\t}\n\t\tneed := prefix + \"1\"\n\t\tif !strings.HasPrefix(s, need) {\n\t\t\treturn \"\"\n\t\t}\n\t\tdata := s[len(need):]\n\t\tif markdown.MatchCharsetN(data, bechDataFirstLo, bechDataFirstHi, bechDataRestLo, bechDataRestHi, 6, 90) {\n\t\t\treturn s\n\t\t}\n\t\treturn \"\"\n\t}\n\t// prefix == \"\" — accept any 1-16 char lowercase HRP, then '1', then data.\n\tsep := strings.IndexByte(s, '1')\n\tif sep \u003c 1 || sep \u003e 16 {\n\t\treturn \"\"\n\t}\n\thrp := s[:sep]\n\tif !markdown.MatchCharsetN(hrp, bechHrpFirstLo, bechHrpFirstHi, bechHrpRestLo, bechHrpRestHi, 1, 16) {\n\t\treturn \"\"\n\t}\n\tdata := s[sep+1:]\n\tif markdown.MatchCharsetN(data, bechDataFirstLo, bechDataFirstHi, bechDataRestLo, bechDataRestHi, 6, 90) {\n\t\treturn s\n\t}\n\treturn \"\"\n}\n\n// FootnoteLabel validates an identifier used as a footnote name, link-\n// reference-definition label, or {#id} anchor: ^[A-Za-z0-9_-]{1,64}$.\n// Strips bidi/zero-width first. Returns s if valid, \"\" otherwise.\n//\n// Use for every shape where a markdown identifier is treated as an\n// opaque key by the parser:\n//\n//   - footnote-definition labels:        [^FootnoteLabel(name)]: body\n//   - footnote-reference labels:         see [^FootnoteLabel(name)]\n//   - link-reference-definition labels:  [FootnoteLabel(label)]: url\n//   - reference-link USE labels:         [text][FootnoteLabel(label)]\n//   - goldmark auto-anchor {#id}:        # Heading {#FootnoteLabel(id)}\n//\n// The shared validator name reflects the shared charset and shared\n// security goal — keep untrusted bytes out of any parser-managed\n// identifier slot.\n//\n// On \"\" return, omit the footnote / LRD / anchor entirely rather than\n// emitting it with raw user bytes.\nfunc FootnoteLabel(s string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\tif markdown.MatchCharsetN(s, footnoteLabelFirstLo, footnoteLabelFirstHi, footnoteLabelRestLo, footnoteLabelRestHi, 1, 64) {\n\t\treturn s\n\t}\n\treturn \"\"\n}\n\n// LanguageName validates the language tag (a.k.a. \"info string\") for\n// a fenced code block — the `go` in:\n//\n//\t```go\n//\tfmt.Println(\"hi\")\n//\t```\n//\n// Charset: ^[a-zA-Z0-9_+-]{1,32}$ — letters, digits, `_`, `+`, `-`,\n// up to 32 bytes. Strips bidi/zero-width first.\n//\n// Returns the cleaned input if valid, \"\" otherwise. A \"\" return means\n// the caller should emit a language-less fence (``` without a tag)\n// rather than letting the user pick the syntax highlighter — which\n// could otherwise be used to inject newlines or block markers into\n// what becomes the opening fence line.\nfunc LanguageName(s string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\tif markdown.MatchCharsetN(s, langFirstLo, langFirstHi, langRestLo, langRestHi, 1, 32) {\n\t\treturn s\n\t}\n\treturn \"\"\n}\n\n// NestedPrefix validates a prefix string for line-prefixing builders\n// like md.Nested, which prepends `prefix` to every line of content\n// to render the content as a nested/indented sub-block.\n//\n// Allowed: any string matching `^[ \\t\u003e]*$` — spaces, tabs, blockquote\n// `\u003e` chars only. Anything else (a `#`, a `-`, a letter) would let a\n// caller turn benign sub-content into a heading, list, or paragraph\n// at the wrong nesting level.\n//\n// Returns s if valid, \"\" otherwise. Strips bidi/zero-width first —\n// otherwise an invisible character hidden inside a `\u003e` prefix would\n// be replicated on every nested content line, producing per-line\n// display-vs-storage divergence.\n//\n// On \"\" return, fall back to a known-safe prefix literal (e.g.\n// `\"\u003e \"`) or skip the nesting entirely. Do not emit the raw\n// user-supplied prefix.\nfunc NestedPrefix(s string) string {\n\ts = markdown.StripBidiAndZeroWidth(s)\n\tfor i := 0; i \u003c len(s); i++ {\n\t\tc := s[i]\n\t\tif c != ' ' \u0026\u0026 c != '\\t' \u0026\u0026 c != '\u003e' {\n\t\t\treturn \"\"\n\t\t}\n\t}\n\treturn s\n}\n\n// ----- Primitive -----\n\n// CodeFence returns a string of backticks long enough to wrap content\n// as a CommonMark fenced code block without the content's own backticks\n// closing the fence prematurely.\n//\n// Returned length N = max(minCount, longestBacktickRunInContent + 1).\n// Use N backticks both before and after the content:\n//\n//\tfence := sanitize.CodeFence(userCode, 3)\n//\tout += fence + \"\\n\" + userCode + \"\\n\" + fence + \"\\n\"\n//\n// Typical minCount values:\n//   - 1 for inline code spans (`x`)\n//   - 3 for block fenced code (CommonMark §4.5 requires ≥3)\n//\n// `minCount \u003c 1` is clamped to 1. Empty content returns\n// strings.Repeat(\"`\", max(minCount, 1)). Never panics.\n//\n// Most realms should reach for InlineCode / CodeBlock /\n// LanguageCodeBlock below, which call CodeFence internally and emit\n// the full code block for you. Call CodeFence directly only when\n// you're rolling a custom fence emitter (e.g. a renderer that needs\n// the fence length but emits the body differently).\nfunc CodeFence(content string, minCount int) string {\n\treturn markdown.CodeFence(content, minCount)\n}\n\n// InlineCode wraps user content as a CommonMark inline code span — the\n// `code` in “ `code` “. Use for any user-derived token, identifier,\n// or short literal that should render in monospace inside running\n// prose: variable names, hashes, hex addresses, token symbols, error\n// codes, package paths, transaction IDs.\n//\n// Inline code spans cannot span lines (a `\\n` inside the content would\n// end the span and leave the surrounding backticks as literal text),\n// so all line breaks — CR / CRLF / LF, NEL (U+0085), U+2028, U+2029 —\n// are folded to a single space. If you want each line of user content\n// on its own row, use CodeBlock instead.\n//\n// Behavior:\n//   - Bidi/zero-width controls are stripped (browsers honor bidi marks\n//     inside `\u003ccode\u003e`, so leaving them would let stored bytes display\n//     as something different).\n//   - NUL is replaced with U+FFFD.\n//   - The wrapping fence is one backtick longer than the longest\n//     backtick run in the content, so internal backticks can never\n//     close the span prematurely.\n//   - A single space pad is added on each side when content starts or\n//     ends with “ ` “ or space, so leading/trailing backticks render\n//     literally rather than fusing with the fence (the renderer\n//     strips one space from each side per CommonMark spec).\n//\n// Empty input returns \"\" rather than a literal two-backtick string\n// (which CommonMark parses as text, not as an empty code span). If\n// you use InlineCode as link text and it returns \"\", omit the link\n// entirely.\n//\n// Not idempotent (see package doc): wraps with a fence, so calling\n// twice double-wraps.\nfunc InlineCode(content string) string {\n\tcontent = markdown.StripBidiAndZeroWidth(content)\n\tcontent = markdown.NormalizeBreaks(content)\n\tcontent = foldNewlinesAndSeparators(content, ' ')\n\tcontent = replaceNULWithFFFD(content)\n\tif content == \"\" {\n\t\treturn \"\"\n\t}\n\tfence := markdown.CodeFence(content, 1)\n\tpad := \"\"\n\tif content[0] == '`' || content[0] == ' ' ||\n\t\tcontent[len(content)-1] == '`' || content[len(content)-1] == ' ' {\n\t\tpad = \" \"\n\t}\n\treturn fence + pad + content + pad + fence\n}\n\n// CodeBlock wraps user content as a CommonMark fenced code block.\n// Use for any user-derived multi-line snippet that should render as a\n// code block: log excerpts, JSON dumps, error backtraces, config\n// snippets, posted code samples.\n//\n// Behavior:\n//   - Bidi/zero-width controls are stripped.\n//   - CR/CRLF line endings are normalized to LF; Unicode separators\n//     (NEL U+0085, U+2028, U+2029) are folded to LF for line-count\n//     consistency.\n//   - NUL is replaced with U+FFFD per CM §2.3.\n//   - The wrapping fence is at least 3 backticks (CM §4.5 minimum) and\n//     sized to outscan internal backticks — an attacker cannot embed\n//     a closing fence in the content.\n//\n// Empty content emits an empty fenced block (\"```\\n\\n```\\n\"), which is\n// valid CommonMark and renders as an empty `\u003cpre\u003e\u003ccode\u003e\u003c/code\u003e\u003c/pre\u003e`.\n//\n// Not idempotent (see package doc).\nfunc CodeBlock(content string) string {\n\tcontent = markdown.StripBidiAndZeroWidth(content)\n\tcontent = markdown.NormalizeBreaks(content)\n\tcontent = foldNewlinesAndSeparators(content, '\\n')\n\tcontent = replaceNULWithFFFD(content)\n\tfence := markdown.CodeFence(content, 3)\n\treturn fence + \"\\n\" + content + \"\\n\" + fence + \"\\n\"\n}\n\n// LanguageCodeBlock wraps user content as a fenced code block tagged\n// with a programming-language hint (the \"info string\" after the\n// opening fence, e.g. `go` in ```` ```go ````) so the renderer can\n// apply syntax highlighting.\n//\n// An invalid `language` tag silently falls back to a tagless fence —\n// the helper never returns an error or panics. If a realm author is\n// debugging \"why is my Go highlighting gone?\", the input failed the\n// language validator (charset ^[a-zA-Z0-9_+-]{1,32}$ after bidi-strip).\n// This fallback exists because an unvalidated tag could contain a\n// newline that injects content (e.g. a heading) onto what becomes the\n// opening fence line.\n//\n// Content is cleaned exactly as in CodeBlock (bidi-strip, CR/CRLF\n// normalize to LF, NEL/U+2028/U+2029 fold to LF, NUL→U+FFFD, fence\n// sized to outscan internal backticks).\n//\n// Not idempotent (see package doc).\nfunc LanguageCodeBlock(language, content string) string {\n\tcontent = markdown.StripBidiAndZeroWidth(content)\n\tcontent = markdown.NormalizeBreaks(content)\n\tcontent = foldNewlinesAndSeparators(content, '\\n')\n\tcontent = replaceNULWithFFFD(content)\n\tfence := markdown.CodeFence(content, 3)\n\tlang := LanguageName(language) // \"\" on reject\n\treturn fence + lang + \"\\n\" + content + \"\\n\" + fence + \"\\n\"\n}\n\n// ----- Reference-style definitions -----\n\n// FootnoteDefinition emits a GFM footnote definition — the\n// `[^name]: body` form that introduces a footnote whose body is rendered\n// in the page footer (or wherever the renderer chooses to place it).\n// Other parts of the markdown reference the footnote by writing\n// `[^name]` inline.\n//\n// Use for any realm-rendered footnote where the body text comes from\n// user input. The realm picks the footnote name (passed as `name`,\n// validated by FootnoteLabel — failure here returns \"\"); the user's\n// content goes in `text`, which is sanitized via Block.\n//\n// Contract:\n//   - `name`: passed raw, validated as a FootnoteLabel\n//     (^[A-Za-z0-9_-]{1,64}$). Reject → return \"\".\n//   - `text`: passed raw multi-paragraph user prose, cleaned via Block\n//     (bidi-strip, line-ending normalize, LRD strip, block-marker\n//     escape, ref-link USE escape, fence auto-close).\n//\n// Empty body → returns \"\" (a label without body is not a valid\n// footnote definition; the markdown would parse as a paragraph\n// containing the label).\n//\n// Output shape:\n//\n//\t[^name]:\n//\t    line 1 of body\n//\t    line 2 of body\n//\t    ...\n//\n// The label sits on its own line and each body line gets a 4-space\n// indent — the GFM continuation rule that keeps multi-paragraph body\n// text bound to the footnote rather than detaching as a new paragraph.\n//\n// Not idempotent (see package doc): composes Block internally; passing\n// already-sanitized body text double-escapes.\nfunc FootnoteDefinition(name, text string) string {\n\tlabel := FootnoteLabel(name)\n\tif label == \"\" {\n\t\treturn \"\"\n\t}\n\t// Block now wraps with \"\\n\\n\" on both sides for cross-paragraph\n\t// isolation; inside a footnote-definition's 4-space-indented body\n\t// the wrap would line-prefix to blank padding lines, so strip ALL\n\t// leading and trailing \"\\n\"s before continuation-indenting.\n\tbody := strings.Trim(Block(text), \"\\n\")\n\tif body == \"\" {\n\t\treturn \"\"\n\t}\n\tvar b strings.Builder\n\tb.WriteString(\"[^\")\n\tb.WriteString(label)\n\tb.WriteString(\"]:\\n\")\n\tfor _, line := range strings.Split(body, \"\\n\") {\n\t\tif line == \"\" {\n\t\t\tb.WriteByte('\\n')\n\t\t} else {\n\t\t\tb.WriteString(\"    \")\n\t\t\tb.WriteString(line)\n\t\t\tb.WriteByte('\\n')\n\t\t}\n\t}\n\treturn b.String()\n}\n\n// LinkReferenceDefinition emits a CommonMark link reference definition\n// (CM §4.7) — the `[label]: url \"title\"` form that other parts of the\n// markdown reference by writing `[text][label]` or `[label]` (shortcut).\n//\n// Use for any realm-rendered LRD where the realm owns the label but\n// any of the URL or title come from user input. The user content for\n// the URL goes through URL (allowlist-based — reject → \"\"); the title\n// goes through LinkTitle (escape).\n//\n// Contract:\n//   - `label`: passed raw, validated as a FootnoteLabel\n//     (^[A-Za-z0-9_-]{1,64}$). Realms should choose a namespaced label\n//     using dashes (e.g. `r-myrealm-help`) so shortcut-reference\n//     invocations from user content can't collide with bare prose\n//     (`[help]`, `[click here]`). `/` is not in the FootnoteLabel\n//     charset; reject → return \"\".\n//   - `url`: passed raw, sanitized via URL. If URL rejects, the LRD is\n//     skipped (return \"\").\n//   - `title`: passed raw, sanitized via LinkTitle. Empty title → no\n//     title clause emitted.\n//\n// The output is framed with leading and trailing blank lines so that\n// the definition cannot accidentally fuse with adjacent paragraph\n// content into a setext underline or a continuation line.\n//\n// Not idempotent (see package doc).\nfunc LinkReferenceDefinition(label, url, title string) string {\n\tlbl := FootnoteLabel(label)\n\tif lbl == \"\" {\n\t\treturn \"\"\n\t}\n\tsafeURL := URL(url)\n\tif safeURL == \"\" {\n\t\treturn \"\"\n\t}\n\tvar b strings.Builder\n\tb.WriteString(\"\\n\\n[\")\n\tb.WriteString(lbl)\n\tb.WriteString(\"]: \")\n\tb.WriteString(safeURL)\n\tif title != \"\" {\n\t\tb.WriteString(\" \\\"\")\n\t\tb.WriteString(LinkTitle(title))\n\t\tb.WriteString(\"\\\"\")\n\t}\n\tb.WriteString(\"\\n\\n\")\n\treturn b.String()\n}\n\n// ----- internal helpers -----\n\n// linkSchemeAllowed returns true if s passes the URL helper's scheme\n// allowlist. See URL's doc for the policy.\nfunc linkSchemeAllowed(s string) bool {\n\tif strings.HasPrefix(s, \"http://\") || strings.HasPrefix(s, \"https://\") {\n\t\treturn true\n\t}\n\tif strings.HasPrefix(s, \"mailto:\") {\n\t\t// Reject any query: RFC 6068 headers (body, subject, cc, bcc, ...)\n\t\t// prefill the composed message and are a phishing vector. '?' opens\n\t\t// the header section (covering percent-encoded names like ?%62ody=).\n\t\t// '\u0026' is rejected because gnoweb's renderer decodes HTML character\n\t\t// references ('\u0026#63;', '\u0026#x3f;', '\u0026quest;') back into '?' after this\n\t\t// check, reconstituting a query. Percent-encoded '%3f' stays encoded\n\t\t// and reads as a literal '?' in the address, so it's allowed.\n\t\tif strings.ContainsAny(s, \"?\u0026\") {\n\t\t\treturn false\n\t\t}\n\t\treturn true\n\t}\n\tif strings.HasPrefix(s, \"//\") {\n\t\t// Protocol-relative — reject (tracking-pixel vector).\n\t\treturn false\n\t}\n\t// Any URL with an unknown scheme (RFC 3986: `^[a-zA-Z][a-zA-Z0-9+.-]*:`)\n\t// is rejected — this blocks `javascript:`, `data:`, `vbscript:`, `blob:`,\n\t// and anything else not handled above. URLs without a scheme are\n\t// treated as relative and accepted (bare path, query-only, fragment).\n\tif hasURLScheme(s) {\n\t\treturn false\n\t}\n\treturn true\n}\n\n// hasURLScheme reports whether s begins with a scheme followed by ':'\n// per RFC 3986 (^[a-zA-Z][a-zA-Z0-9+.-]*:). A `:` appearing later in\n// the URL (e.g. `/path:foo` or `?q=a:b`) does not count.\nfunc hasURLScheme(s string) bool {\n\tif len(s) == 0 {\n\t\treturn false\n\t}\n\tc := s[0]\n\tif !((c \u003e= 'a' \u0026\u0026 c \u003c= 'z') || (c \u003e= 'A' \u0026\u0026 c \u003c= 'Z')) {\n\t\treturn false\n\t}\n\tfor i := 1; i \u003c len(s); i++ {\n\t\tc := s[i]\n\t\tif c == ':' {\n\t\t\treturn true\n\t\t}\n\t\tif !((c \u003e= 'a' \u0026\u0026 c \u003c= 'z') || (c \u003e= 'A' \u0026\u0026 c \u003c= 'Z') ||\n\t\t\t(c \u003e= '0' \u0026\u0026 c \u003c= '9') || c == '+' || c == '.' || c == '-') {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn false\n}\n\n// imageSchemeAllowed returns true if s passes the ImageURL helper's\n// scheme allowlist. Tighter than linkSchemeAllowed: no mailto/tel,\n// only data:image/\u003csubset\u003e.\nfunc imageSchemeAllowed(s string) bool {\n\tif strings.HasPrefix(s, \"http://\") || strings.HasPrefix(s, \"https://\") {\n\t\treturn true\n\t}\n\tif strings.HasPrefix(s, \"//\") {\n\t\treturn false\n\t}\n\tif strings.HasPrefix(s, \"/\") || strings.HasPrefix(s, \"./\") || strings.HasPrefix(s, \"../\") {\n\t\treturn true\n\t}\n\tif strings.HasPrefix(s, \"data:\") {\n\t\t// Only the curated image/* subset. CSS must enforce sizing.\n\t\tfor _, p := range []string{\n\t\t\t\"data:image/svg+xml\",\n\t\t\t\"data:image/png\",\n\t\t\t\"data:image/jpeg\",\n\t\t\t\"data:image/gif\",\n\t\t\t\"data:image/webp\",\n\t\t} {\n\t\t\tif strings.HasPrefix(s, p) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n\treturn false\n}\n\n// foldNewlinesAndSeparators replaces \\n, U+0085 NEL, U+2028 LINE SEPARATOR,\n// U+2029 PARAGRAPH SEPARATOR with the given replacement byte (typically\n// space for inline-context helpers).\n//\n// NormalizeBreaks has already folded \\r\\n and \\r to \\n before this runs,\n// so \\n is the canonical break byte to substitute.\nfunc foldNewlinesAndSeparators(s string, replacement byte) string {\n\tif !needsSeparatorFold(s) {\n\t\treturn s\n\t}\n\tout := make([]byte, 0, len(s))\n\tfor i := 0; i \u003c len(s); {\n\t\tc := s[i]\n\t\tif c == '\\n' {\n\t\t\tout = append(out, replacement)\n\t\t\ti++\n\t\t\tcontinue\n\t\t}\n\t\t// U+0085 NEL: 0xC2 0x85\n\t\tif c == 0xC2 \u0026\u0026 i+1 \u003c len(s) \u0026\u0026 s[i+1] == 0x85 {\n\t\t\tout = append(out, replacement)\n\t\t\ti += 2\n\t\t\tcontinue\n\t\t}\n\t\t// U+2028 (0xE2 0x80 0xA8) or U+2029 (0xE2 0x80 0xA9)\n\t\tif c == 0xE2 \u0026\u0026 i+2 \u003c len(s) \u0026\u0026 s[i+1] == 0x80 \u0026\u0026 (s[i+2] == 0xA8 || s[i+2] == 0xA9) {\n\t\t\tout = append(out, replacement)\n\t\t\ti += 3\n\t\t\tcontinue\n\t\t}\n\t\tout = append(out, c)\n\t\ti++\n\t}\n\treturn string(out)\n}\n\nfunc needsSeparatorFold(s string) bool {\n\tfor i := 0; i \u003c len(s); i++ {\n\t\tc := s[i]\n\t\tif c == '\\n' || c == 0xC2 || c == 0xE2 {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// replaceNULWithFFFD substitutes any NUL byte with the UTF-8 encoding\n// of U+FFFD REPLACEMENT CHARACTER per CM §2.3.\nfunc replaceNULWithFFFD(s string) string {\n\tif !strings.ContainsRune(s, 0) {\n\t\treturn s\n\t}\n\treturn strings.ReplaceAll(s, \"\\x00\", \"\\ufffd\")\n}\n"},{"name":"sanitize_test.gno","body":"package sanitize\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestStripBidiAndZeroWidthRe(t *testing.T) {\n\t// Re-export sanity check.\n\tif got := StripBidiAndZeroWidth(\"a\\u200Bb\"); got != \"ab\" {\n\t\tt.Errorf(\"re-export StripBidiAndZeroWidth failed: got %q\", got)\n\t}\n}\n\nfunc TestNormalizeBreaksRe(t *testing.T) {\n\tif got := NormalizeBreaks(\"a\\r\\nb\"); got != \"a\\nb\" {\n\t\tt.Errorf(\"re-export NormalizeBreaks failed: got %q\", got)\n\t}\n}\n\nfunc TestInlineText(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"plain\", \"plain\"},\n\t\t{\"a*b\", `a\\*b`},\n\t\t{\"a|b\", \"a|b\"},                    // | NOT escaped\n\t\t{\"a=b\", \"a=b\"},                    // = NOT escaped\n\t\t{\"line1\\nline2\", \"line1 line2\"},   // newline folded\n\t\t{\"line1\\r\\nline2\", \"line1 line2\"}, // CRLF normalized then folded\n\t\t{\"a\\u2028b\", \"a b\"},               // U+2028 folded\n\t\t{\"a\\u0085b\", \"a b\"},               // NEL folded\n\t\t{\"a\\u200Bb\", \"ab\"},                // ZWSP stripped\n\t\t{\"hello *world*\", `hello \\*world\\*`},\n\t}\n\tfor _, c := range cases {\n\t\tif got := InlineText(c.in); got != c.want {\n\t\t\tt.Errorf(\"InlineText(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestBlock(t *testing.T) {\n\t// Block wraps every non-empty output with \"\\n\\n\" — CM §4.8 blank\n\t// lines — on BOTH sides, so user content is paragraph-isolated\n\t// from any realm chrome that precedes OR follows it. Bounds CM\n\t// §4.6 HTML block types 6/7 (`\u003cdiv\u003e`, `\u003ctable\u003e`, …) which are not\n\t// escaped in any mode and would otherwise consume appended chrome.\n\tcases := []struct{ in, want string }{\n\t\t{\"hello world\\n\", \"\\n\\nhello world\\n\\n\"},\n\t\t{\"# heading\\n\", \"\\n\\n\\\\# heading\\n\\n\"},\n\t\t{\"\u003e quote\\n\", \"\\n\\n\\\\\u003e quote\\n\\n\"},\n\t\t{\"| a | b |\\n\", \"\\n\\n\\\\| a | b |\\n\\n\"}, // GFM table-row escaped in strict mode\n\t\t// CM §4.6 HTML block types 1-5 — escaped (blank-line-NON-terminating).\n\t\t{\"\u003cscript\u003ex\u003c/script\u003e\\n\", \"\\n\\n\\\\\u003cscript\u003ex\u003c/script\u003e\\n\\n\"},\n\t\t{\"\u003c!-- comment --\u003e\\n\", \"\\n\\n\\\\\u003c!-- comment --\u003e\\n\\n\"},\n\t\t{\"\u003c?php x ?\u003e\\n\", \"\\n\\n\\\\\u003c?php x ?\u003e\\n\\n\"},\n\t\t{\"\u003c!DOCTYPE html\u003e\\n\", \"\\n\\n\\\\\u003c!DOCTYPE html\u003e\\n\\n\"},\n\t\t{\"```\\ncode\\n\", \"\\n\\n```\\ncode\\n```\\n\\n\"}, // fence auto-close at EOF\n\t\t// Empty / strip-to-empty inputs short-circuit (no stray blank line).\n\t\t{\"[x]: y\\n\", \"\"},\n\t\t{\"\", \"\"},\n\t}\n\tfor _, c := range cases {\n\t\tgot := Block(c.in)\n\t\tif got != c.want {\n\t\t\tt.Errorf(\"Block(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t\t// Idempotency: Block(Block(in)) must be byte-identical to\n\t\t// Block(in) for every input in the table.\n\t\ttwice := Block(got)\n\t\tif twice != got {\n\t\t\tt.Errorf(\"Block not idempotent for %q: Block(once)=%q, Block(twice)=%q\", c.in, got, twice)\n\t\t}\n\t}\n}\n\nfunc TestBlockRich(t *testing.T) {\n\t// BlockRich wraps every non-empty output with \"\\n\\n\" — CM §4.8\n\t// blank lines — on BOTH sides, so user content is paragraph-\n\t// isolated from any realm chrome that precedes OR follows it.\n\tcases := []struct{ in, want string }{\n\t\t// Block-level markdown that Block escapes — preserved by BlockRich.\n\t\t{\"hello world\\n\", \"\\n\\nhello world\\n\\n\"},\n\t\t{\"# heading\\n\", \"\\n\\n# heading\\n\\n\"},\n\t\t{\"\u003e quote\\n\", \"\\n\\n\u003e quote\\n\\n\"},\n\t\t{\"- item\\n\", \"\\n\\n- item\\n\\n\"},\n\t\t{\"1. item\\n\", \"\\n\\n1. item\\n\\n\"},\n\t\t{\"---\\n\", \"\\n\\n\\\\---\\n\\n\"},                       // first-line setext-h2 → escaped by qualifying-setext pre-pass\n\t\t{\"***\\n\", \"\\n\\n***\\n\\n\"},                         // thematic break NOT setext-h2 — preserved\n\t\t{\"text\\n===\\n\", \"\\n\\ntext\\n===\\n\\n\"},             // deeper setext preserved (user-authored above)\n\t\t{\"body\\n===\\nmore\\n\", \"\\n\\nbody\\n===\\nmore\\n\\n\"}, // cross-paragraph backward attack: realm `chrome\\n\\nbody\\n===\\nmore` keeps realm in its own paragraph\n\t\t// GFM tables PRESERVED in Rich mode.\n\t\t{\"| a | b |\\n\", \"\\n\\n| a | b |\\n\\n\"},\n\t\t{\"| H |\\n|---|\\n| a |\\n\", \"\\n\\n| H |\\n|---|\\n| a |\\n\\n\"}, // full table renders as \u003ctable\u003e\n\t\t// Realm-binding defenses STILL ON.\n\t\t{\"\u003cgno-card\u003e\\n\", \"\\n\\n\\\\\u003cgno-card\u003e\\n\\n\"},\n\t\t// CM §4.6 HTML block types 1-5 — escaped in Rich mode too\n\t\t// (defense is mode-independent).\n\t\t{\"\u003cscript\u003ex\u003c/script\u003e\\n\", \"\\n\\n\\\\\u003cscript\u003ex\u003c/script\u003e\\n\\n\"},\n\t\t{\"\u003c!-- comment --\u003e\\n\", \"\\n\\n\\\\\u003c!-- comment --\u003e\\n\\n\"},\n\t\t{\"\u003c?php x ?\u003e\\n\", \"\\n\\n\\\\\u003c?php x ?\u003e\\n\\n\"},\n\t\t{\"\u003c!DOCTYPE html\u003e\\n\", \"\\n\\n\\\\\u003c!DOCTYPE html\u003e\\n\\n\"},\n\t\t{\"[t][l]\\n\", \"\\n\\n\\\\[t\\\\]\\\\[l\\\\]\\n\\n\"},\n\t\t{\"[^name]\\n\", \"\\n\\n\\\\[^name\\\\]\\n\\n\"},\n\t\t// LRD-only input strips to nothing; empty-after-escape short-\n\t\t// circuits to \"\" so realm concatenation doesn't leak a stray\n\t\t// blank line.\n\t\t{\"[x]: y\\n\", \"\"},\n\t\t{\"\", \"\"}, // empty input → empty output, no stray blank line\n\t\t{\"```\\ncode\\n\", \"\\n\\n```\\ncode\\n```\\n\\n\"},\n\t}\n\tfor _, c := range cases {\n\t\tgot := BlockRich(c.in)\n\t\tif got != c.want {\n\t\t\tt.Errorf(\"BlockRich(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t\t// Idempotency: BlockRich(BlockRich(in)) must be byte-identical\n\t\t// to BlockRich(in) for every input in the table.\n\t\ttwice := BlockRich(got)\n\t\tif twice != got {\n\t\t\tt.Errorf(\"BlockRich not idempotent for %q: BlockRich(once)=%q, BlockRich(twice)=%q\", c.in, got, twice)\n\t\t}\n\t}\n}\n\nfunc TestBlockRich_LeadingBlankLineGuaranteed(t *testing.T) {\n\t// Every non-empty result begins with \"\\n\\n\" so `chrome +\n\t// BlockRich(user)` cannot place the user's first line in the same\n\t// paragraph as the realm's last line (which would let a deeper\n\t// `===` or `|---|` in user content retroactively promote realm\n\t// chrome).\n\tfor _, in := range []string{\"x\", \"x\\n\", \"# h\", \"- i\\n- j\", \"| a |\\n|---|\\n| 1 |\"} {\n\t\tgot := BlockRich(in)\n\t\tif got == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tif !strings.HasPrefix(got, \"\\n\\n\") {\n\t\t\tt.Errorf(\"BlockRich(%q) = %q; expected leading '\\\\n\\\\n'\", in, got)\n\t\t}\n\t}\n}\n\nfunc TestBlockRich_TrailingBlankLineGuaranteed(t *testing.T) {\n\t// Every non-empty result ends with \"\\n\\n\" so `BlockRich(user) +\n\t// chrome` cannot extend user's last paragraph into the realm's\n\t// next line (CM §5.2 lazy continuation, or a realm-supplied\n\t// `|---|` row retroactively promoting user's last line into a\n\t// `\u003cthead\u003e` header).\n\tfor _, in := range []string{\"x\", \"x\\n\", \"# h\", \"- i\\n- j\", \"| H |\\n|---|\\n| a |\"} {\n\t\tgot := BlockRich(in)\n\t\tif got == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tif !strings.HasSuffix(got, \"\\n\\n\") {\n\t\t\tt.Errorf(\"BlockRich(%q) = %q; expected trailing '\\\\n\\\\n'\", in, got)\n\t\t}\n\t}\n}\n\nfunc TestBlockquoteRich(t *testing.T) {\n\t// BlockquoteRich strips BlockRich's leading \"\\n\", line-prefixes\n\t// each remaining line with \"\u003e \", and emits \"\\n\" on both ends:\n\t// leading \"\\n\" prevents `chrome + BlockquoteRich(user)` from\n\t// landing the first `\u003e` mid-line; trailing \"\\n\\n\" (blank line)\n\t// prevents `BlockquoteRich(user) + chrome` from pulling chrome\n\t// into the quote via CM §5.2 lazy continuation.\n\tcases := []struct{ name, in, want string }{\n\t\t{\"plain text\", \"hello world\\n\", \"\\n\u003e hello world\\n\\n\"},\n\t\t{\"atx heading preserved\", \"# heading\\n\", \"\\n\u003e # heading\\n\\n\"},\n\t\t{\"nested blockquote\", \"\u003e nested\\n\", \"\\n\u003e \u003e nested\\n\\n\"},\n\t\t{\"list item preserved\", \"- item\\n\", \"\\n\u003e - item\\n\\n\"},\n\t\t{\"ordered list preserved\", \"1. item\\n\", \"\\n\u003e 1. item\\n\\n\"},\n\t\t{\"first-line setext escaped\", \"---\\n\", \"\\n\u003e \\\\---\\n\\n\"},\n\t\t{\"deeper setext preserved\", \"text\\n===\\n\", \"\\n\u003e text\\n\u003e ===\\n\\n\"},\n\t\t{\"thematic break asterisks preserved\", \"***\\n\", \"\\n\u003e ***\\n\\n\"},\n\t\t// Realm-binding defenses still on.\n\t\t{\"gno extension escaped\", \"\u003cgno-card\u003e\\n\", \"\\n\u003e \\\\\u003cgno-card\u003e\\n\\n\"},\n\t\t{\"ref-link use escaped\", \"[t][l]\\n\", \"\\n\u003e \\\\[t\\\\]\\\\[l\\\\]\\n\\n\"},\n\t\t{\"footnote escaped\", \"[^name]\\n\", \"\\n\u003e \\\\[^name\\\\]\\n\\n\"},\n\t\t// LRDs are stripped by BlockRich entirely; trimming the\n\t\t// resulting bare \"\\n\" leaves empty input and the helper\n\t\t// returns \"\" without emitting a blockquote.\n\t\t{\"lrd alone strips to empty\", \"[x]: y\\n\", \"\"},\n\t\t// Code fence autoclose at EOF lands inside the quote.\n\t\t{\"unclosed fence autoclosed\", \"```\\ncode\\n\", \"\\n\u003e ```\\n\u003e code\\n\u003e ```\\n\\n\"},\n\t\t// Multi-paragraph preserves the inner blank line (rendered\n\t\t// as a quoted blank line `\u003e \\n`).\n\t\t{\"multi-paragraph preserves blank\", \"a\\n\\nb\\n\", \"\\n\u003e a\\n\u003e \\n\u003e b\\n\\n\"},\n\t\t// Empty / blank-only input collapses cleanly to \"\".\n\t\t{\"empty input\", \"\", \"\"},\n\t\t// CRLF normalized through BlockRich.\n\t\t{\"crlf normalized\", \"a\\r\\nb\\n\", \"\\n\u003e a\\n\u003e b\\n\\n\"},\n\t\t// NUL replaced with U+FFFD by BlockRich.\n\t\t{\"nul replaced\", \"x\\x00y\\n\", \"\\n\u003e x\\uFFFDy\\n\\n\"},\n\t\t// Multiple trailing newlines collapse to the single trailing\n\t\t// blank line shape — output never ends with more than `\\n\\n`.\n\t\t{\"multiple trailing newlines collapse\", \"foo\\n\\n\\n\", \"\\n\u003e foo\\n\\n\"},\n\t\t// Internal tabs are preserved (BlockRich does not touch them).\n\t\t{\"internal tab preserved\", \"a\\tb\\n\", \"\\n\u003e a\\tb\\n\\n\"},\n\t\t// Whitespace-only input still yields a blockquote (the user\n\t\t// wrote literal spaces). The line-prefix loop emits `\u003e ` plus\n\t\t// the original two spaces.\n\t\t{\"whitespace-only input\", \"  \", \"\\n\u003e   \\n\\n\"},\n\t}\n\tfor _, c := range cases {\n\t\tgot := BlockquoteRich(c.in)\n\t\tif got != c.want {\n\t\t\tt.Errorf(\"%s: BlockquoteRich(%q) = %q, want %q\", c.name, c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestBlockquoteRich_DoubleWrapNestsQuote(t *testing.T) {\n\t// Not idempotent: calling twice nests the quote one level\n\t// deeper. The aggressive TrimRight in BlockquoteRich also\n\t// collapses the inner trailing blank line, so the second pass\n\t// produces a clean `\u003e \u003e foo` nesting (no `\u003e ` quoted blank in\n\t// the middle) plus the outer trailing blank line.\n\tonce := BlockquoteRich(\"foo\\n\")\n\ttwice := BlockquoteRich(once)\n\tif want := \"\\n\u003e \u003e foo\\n\\n\"; twice != want {\n\t\tt.Errorf(\"BlockquoteRich(BlockquoteRich(%q)) = %q, want %q\", \"foo\\n\", twice, want)\n\t}\n}\n\nfunc TestBlockquoteRich_LeadingNewlineGuaranteed(t *testing.T) {\n\t// Every non-empty result starts with \"\\n\" so realm concatenation\n\t// like `chrome + BlockquoteRich(user)` doesn't place `\u003e` mid-line.\n\tfor _, in := range []string{\"x\", \"x\\n\", \"# h\", \"- i\\n- j\"} {\n\t\tgot := BlockquoteRich(in)\n\t\tif got == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tif got[0] != '\\n' {\n\t\t\tt.Errorf(\"BlockquoteRich(%q) = %q; expected leading '\\\\n'\", in, got)\n\t\t}\n\t}\n}\n\nfunc TestBlockquoteRich_TrailingBlankLineGuaranteed(t *testing.T) {\n\t// Every non-empty result ends with \"\\n\\n\" so realm concatenation\n\t// like `BlockquoteRich(user) + \"more text\"` doesn't pull \"more\n\t// text\" into the quote via CM §5.2 lazy continuation.\n\tfor _, in := range []string{\"x\", \"x\\n\", \"# h\", \"- i\\n- j\", \"para\\n\\nmore\"} {\n\t\tgot := BlockquoteRich(in)\n\t\tif got == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tif !strings.HasSuffix(got, \"\\n\\n\") {\n\t\t\tt.Errorf(\"BlockquoteRich(%q) = %q; expected trailing '\\\\n\\\\n'\", in, got)\n\t\t}\n\t}\n}\n\nfunc TestBlockquoteRich_NoStrayEmptyQuotedLine(t *testing.T) {\n\t// BlockRich's own leading \"\\n\" must be stripped before\n\t// line-prefixing — otherwise the output would start with a\n\t// useless `\u003e \\n` empty quoted line.\n\tgot := BlockquoteRich(\"foo\\n\")\n\tif strings.HasPrefix(got, \"\\n\u003e \\n\") {\n\t\tt.Errorf(\"BlockquoteRich leaked BlockRich's leading '\\\\n' as `\u003e \\\\n`: %q\", got)\n\t}\n}\n\nfunc TestNeuterLeadingSetextIfQualifying(t *testing.T) {\n\tcases := []struct{ name, in, want string }{\n\t\t{\"leading-setext-h1\", \"===\\nbody\\n\", \"\\\\===\\nbody\\n\"},\n\t\t{\"leading-setext-h2\", \"---\\nbody\\n\", \"\\\\---\\nbody\\n\"},\n\t\t{\"leading-setext-indented\", \"   ===\\nbody\\n\", \"   \\\\===\\nbody\\n\"},\n\t\t{\"leading-setext-indented-4\", \"    ===\\nbody\\n\", \"    ===\\nbody\\n\"}, // indented code\n\t\t{\"leading-setext-trailing-ws\", \"===   \\nbody\\n\", \"\\\\===   \\nbody\\n\"},\n\t\t{\"leading-setext-mixed-chars\", \"=-=-\\nbody\\n\", \"=-=-\\nbody\\n\"},         // mixed; not setext\n\t\t{\"leading-setext-has-content\", \"=== text\\nbody\\n\", \"=== text\\nbody\\n\"}, // mixed content\n\t\t{\"setext-deeper-untouched\", \"title\\n===\\nfoo\\n\", \"title\\n===\\nfoo\\n\"},\n\t\t{\"leading-thematic-asterisk\", \"***\\nbody\\n\", \"***\\nbody\\n\"}, // not setext, untouched\n\t\t{\"leading-thematic-underscore\", \"___\\nbody\\n\", \"___\\nbody\\n\"},\n\t\t{\"leading-blank-then-setext\", \"\\n===\\nbody\\n\", \"\\n\\\\===\\nbody\\n\"},\n\t\t{\"leading-blank-ws-then-setext\", \"   \\n===\\nbody\\n\", \"   \\n\\\\===\\nbody\\n\"},\n\t\t{\"text-first\", \"hello\\n===\\n\", \"hello\\n===\\n\"}, // text before === — user-authored\n\t\t{\"empty\", \"\", \"\"},\n\t\t{\"all-blank\", \"   \\n\\n\", \"   \\n\\n\"},\n\t\t{\"only-equals\", \"===\", \"\\\\===\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := neuterLeadingSetextIfQualifying(c.in); got != c.want {\n\t\t\tt.Errorf(\"%s: neuterLeadingSetextIfQualifying(%q) = %q, want %q\", c.name, c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestLinkTitle(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{`he said \"hi\"`, `he said \\\"hi\\\"`},\n\t\t{`it's nice`, `it\\'s nice`},\n\t\t{\"line1\\nline2\", \"line1 line2\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := LinkTitle(c.in); got != c.want {\n\t\t\tt.Errorf(\"LinkTitle(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestTableCell(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"plain cell\", \"plain cell\"},\n\t\t{\"a|b\", `a\\|b`},\n\t\t{\"a\\tb\", \"a b\"},\n\t\t{\"a*b|c\", `a\\*b\\|c`},\n\t}\n\tfor _, c := range cases {\n\t\tif got := TableCell(c.in); got != c.want {\n\t\t\tt.Errorf(\"TableCell(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestHTMLEscape(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"plain\", \"plain\"},\n\t\t{\"\u003cscript\u003e\", \"\u0026lt;script\u0026gt;\"},\n\t\t{`a \u0026 b`, \"a \u0026amp; b\"},\n\t\t{`\"quoted\"`, \"\u0026#34;quoted\u0026#34;\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := HTMLEscape(c.in); got != c.want {\n\t\t\tt.Errorf(\"HTMLEscape(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestURL(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"https://example.com/x\", \"https://example.com/x\"},\n\t\t{\"http://example.com\", \"http://example.com\"},\n\t\t{\"mailto:a@b.com\", \"mailto:a@b.com\"},\n\t\t{\"mailto:a@b.com?body=phish\", \"\"},            // any query is a prefill-phishing vector\n\t\t{\"mailto:a@b.com?BODY=phish\", \"\"},            // header name case is irrelevant\n\t\t{\"mailto:a@b.com?Body=phish\", \"\"},            // mixed case\n\t\t{\"mailto:a@b.com?%62ody=phish\", \"\"},          // percent-encoded header (%62 == b)\n\t\t{\"mailto:a@b.com?subject=hi\u0026body=phish\", \"\"}, // body as second param\n\t\t{\"mailto:a@b.com?subject=hi\u0026BODY=phish\", \"\"}, // mixed case second param\n\t\t{\"mailto:a@b.com?subject=hello\", \"\"},         // subject alone still rejected\n\t\t{\"mailto:a@b.com?cc=x@y.com\", \"\"},            // cc rejected\n\t\t{\"mailto:a@b.com?bcc=x@y.com\", \"\"},           // bcc rejected\n\t\t{\"mailto:a@b.com?\", \"\"},                      // bare trailing query marker rejected\n\t\t{\"mailto:a@b.com\u0026#x3f;body=phish\", \"\"},       // hex char-ref ? — renderer decodes to ?body=\n\t\t{\"mailto:a@b.com\u0026#63;body=phish\", \"\"},        // decimal char-ref ? — same bypass\n\t\t{\"mailto:a@b.com\u0026quest;body=phish\", \"\"},      // named char-ref ? — same bypass\n\t\t{\"mailto:a@b.com\u0026body=phish\", \"\"},            // bare \u0026 gateway rejected\n\t\t{\"//evil.com\", \"\"},                           // protocol-relative rejected\n\t\t{\"javascript:alert(1)\", \"\"},                  // bad scheme\n\t\t{\"/r/foo\", \"/r/foo\"},                         // relative\n\t\t{\"./local\", \"./local\"},\n\t\t{\"#section\", \"#section\"}, // fragment-only\n\t\t{\"\", \"\"},\n\t\t{\"   \", \"\"},\n\t\t{\"https://a.com/path with space\", \"https://a.com/path%20with%20space\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := URL(c.in); got != c.want {\n\t\t\tt.Errorf(\"URL(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestImageURL(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"https://example.com/img.png\", \"https://example.com/img.png\"},\n\t\t{\"mailto:a@b.com\", \"\"}, // mailto rejected for images\n\t\t{\"data:image/svg+xml,\u003csvg/\u003e\", \"data:image/svg+xml,%3Csvg/%3E\"},\n\t\t{\"data:image/png;base64,XXX\", \"data:image/png;base64,XXX\"},\n\t\t{\"data:text/html,\u003cscript\u003e\", \"\"}, // bad data subset\n\t\t{\"javascript:alert(1)\", \"\"},\n\t\t{\"\", \"\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := ImageURL(c.in); got != c.want {\n\t\t\tt.Errorf(\"ImageURL(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestUserName(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"alice\", \"alice\"},\n\t\t{\"alice123\", \"alice123\"},\n\t\t{\"alice_bob-cat\", \"alice_bob-cat\"},\n\t\t{\"Alice\", \"\"},  // uppercase first\n\t\t{\"1alice\", \"\"}, // digit first\n\t\t{\"\", \"\"},\n\t\t{\"a\\u200Blice\", \"alice\"}, // bidi stripped, then matches\n\t}\n\tfor _, c := range cases {\n\t\tif got := UserName(c.in); got != c.want {\n\t\t\tt.Errorf(\"UserName(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestBechString(t *testing.T) {\n\taddrG := \"g1abc123def456ghi789jkl012mno345p\"\n\tcases := []struct {\n\t\ts, prefix string\n\t\twant      string\n\t}{\n\t\t{addrG, \"g\", addrG},\n\t\t{addrG, \"\", addrG},  // any prefix\n\t\t{addrG, \"gpub\", \"\"}, // wrong prefix\n\t\t{\"gpub1abc123def456ghijklmn\", \"gpub\", \"gpub1abc123def456ghijklmn\"},\n\t\t{\"gpub1abc123def456ghijklmn\", \"\", \"gpub1abc123def456ghijklmn\"},\n\t\t{\"b1xyz789abc123def456\", \"\", \"b1xyz789abc123def456\"}, // any-prefix mode allows b1...\n\t\t{\"g1ABC\", \"g\", \"\"}, // uppercase rejected\n\t\t{\"x\", \"g\", \"\"},\n\t\t{\"\", \"g\", \"\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := BechString(c.s, c.prefix); got != c.want {\n\t\t\tt.Errorf(\"BechString(%q,%q) = %q, want %q\", c.s, c.prefix, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestFootnoteLabel(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"note1\", \"note1\"},\n\t\t{\"Note_A-1\", \"Note_A-1\"},\n\t\t{\"with space\", \"\"},\n\t\t{\"\", \"\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := FootnoteLabel(c.in); got != c.want {\n\t\t\tt.Errorf(\"FootnoteLabel(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestLanguageName(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"go\", \"go\"},\n\t\t{\"c++\", \"c++\"},\n\t\t{\"python3\", \"python3\"},\n\t\t{\"objective-c\", \"objective-c\"},\n\t\t{\"with space\", \"\"},\n\t\t{\"\", \"\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := LanguageName(c.in); got != c.want {\n\t\t\tt.Errorf(\"LanguageName(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestNestedPrefix(t *testing.T) {\n\tcases := []struct{ in, want string }{\n\t\t{\"\", \"\"},\n\t\t{\"  \", \"  \"},\n\t\t{\"\\t\", \"\\t\"},\n\t\t{\"\u003e \", \"\u003e \"},\n\t\t{\"\u003e \u003e \", \"\u003e \u003e \"},\n\t\t{\"## \", \"\"}, // markdown-active prefix rejected\n\t\t{\"- \", \"\"},\n\t}\n\tfor _, c := range cases {\n\t\tif got := NestedPrefix(c.in); got != c.want {\n\t\t\tt.Errorf(\"NestedPrefix(%q) = %q, want %q\", c.in, got, c.want)\n\t\t}\n\t}\n}\n\nfunc TestCodeFence(t *testing.T) {\n\tif got := CodeFence(\"```\", 3); got != \"````\" {\n\t\tt.Errorf(\"CodeFence: got %q, want %q\", got, \"````\")\n\t}\n\tif got := CodeFence(\"\", 3); got != \"```\" {\n\t\tt.Errorf(\"CodeFence empty: got %q, want %q\", got, \"```\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"A9smfYEptuFBaY8zyWDtIrNlxi/tZqUclqW2cWYdSRS4CfJkCEQzqXNTUrpeEgtA2WUqTYwf93Sj81SpzpLJBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"md","path":"gno.land/p/moul/md","files":[{"name":"example_test.gno","body":"package md\n\nimport \"strings\"\n\nfunc ExampleHeaders() {\n\tprintln(H1(\"Header 1\"))\n\tprintln(H2(\"Header 2\"))\n\tprintln(H3(\"Header 3\"))\n\tprintln(H4(\"Header 4\"))\n\tprintln(H5(\"Header 5\"))\n\tprintln(H6(\"Header 6\"))\n\n\t// Output:\n\t// # Header 1\n\t//\n\t// ## Header 2\n\t//\n\t// ### Header 3\n\t//\n\t// #### Header 4\n\t//\n\t// ##### Header 5\n\t//\n\t// ###### Header 6\n}\n\nfunc ExampleStyles() {\n\tprintln(Bold(\"bold\"))\n\tprintln(Italic(\"italic\"))\n\tprintln(Strikethrough(\"strikethrough\"))\n\n\t// Output:\n\t// **bold**\n\t// *italic*\n\t// ~~strikethrough~~\n}\n\nfunc ExampleLists() {\n\tprintln(BulletList([]string{\n\t\t\"Item 1\",\n\t\t\"Item 2\\nMore details for item 2\",\n\t}))\n\tprintln(OrderedList([]string{\"Step 1\", \"Step 2\"}))\n\tprintln(TodoList([]string{\"Task 1\", \"Task 2\\nSubtask 2\"}, []bool{true, false}))\n\tprintln(Nested(BulletList([]string{\"Parent Item\", OrderedList([]string{\"Child 1\", \"Child 2\"})}), \"  \"))\n\n\t// Output:\n\t// - Item 1\n\t// - Item 2\n\t//   More details for item 2\n\t//\n\t// 1. Step 1\n\t// 2. Step 2\n\t//\n\t// - [x] Task 1\n\t// - [ ] Task 2\n\t//   Subtask 2\n\t//\n\t//   - Parent Item\n\t//   - 1. Child 1\n\t//     2. Child 2\n}\n\nfunc ExampleBlocks() {\n\tprint(Paragraph(\"This is a paragraph.\"))\n\t// Blockquote adds multiple newlines which Output doesn't allow, so trim\n\tprintln(strings.TrimSpace(Blockquote(\"This is a blockquote\\nSpanning multiple lines\")))\n\n\t// Output:\n\t// This is a paragraph.\n\t//\n\t// \u003e This is a blockquote\n\t// \u003e Spanning multiple lines\n}\n\nfunc ExampleCode() {\n\tprintln(InlineCode(\"inline `code`\"))\n\tprintln(CodeBlock(\"line1\\nline2\"))\n\tprintln(LanguageCodeBlock(\"go\", \"func main() {\\nprintln(\\\"Hello, world!\\\")\\n}\"))\n\tprintln(HorizontalRule())\n\n\t// Output:\n\t// `` inline `code` ``\n\t// ```\n\t// line1\n\t// line2\n\t// ```\n\t//\n\t// ```go\n\t// func main() {\n\t// println(\"Hello, world!\")\n\t// }\n\t// ```\n\t//\n\t// ---\n}\n\nfunc ExampleReferences() {\n\tprintln(Link(\"Gno\", \"http://gno.land\"))\n\tprintln(Image(\"Alt Text\", \"http://example.com/image.png\"))\n\tprintln(InlineImageWithLink(\"Alt Text\", \"http://example.com/image.png\", \"http://example.com\"))\n\tprintln(FootnoteDefinition(\"ref\", \"This is a footnote\"))\n\t// LinkReferenceDefinition adds multiple newlines which Output doesn't allow, so trim\n\tprintln(strings.TrimSpace(LinkReferenceDefinition(\"r-example\", \"/r/example\", \"\")))\n\n\t// Output:\n\t//\n\t// [Gno](http://gno.land)\n\t// ![Alt Text](http://example.com/image.png)\n\t// [![Alt Text](http://example.com/image.png)](http://example.com)\n\t// [^ref]:\n\t//     This is a footnote\n\t//\n\t// [r-example]: /r/example\n}\n\nfunc ExampleColumns() {\n\tprintln(\"4 columns in one gno-columns tag:\")\n\tprintln(Columns([]string{\n\t\t\"Column1\\ncontent1\",\n\t\t\"Column2\\ncontent2\",\n\t\t\"Column3\\ncontent3\",\n\t\t\"Column4\\ncontent4\",\n\t}, true))\n\n\t// Should be automatically placed in multiple column tags\n\tprintln(\"3 cols per row without padding:\")\n\tprintln(ColumnsN([]string{\n\t\t\"Row1Column1\\ncontent1\",\n\t\t\"Row1Column2\\ncontent2\",\n\t\t\"Row1Column3\\ncontent3\",\n\t\t\"Row2Column1\\ncontent1\",\n\t\t\"Row2Column2\\ncontent2\",\n\t\t\"Row2Column3\\ncontent3\",\n\t\t\"Row3Column1\\ncontent1\",\n\t\t\"Row3Column2\\ncontent2\",\n\t\t\"Row3Column3\\ncontent3\",\n\t}, 3, false))\n\n\t// Should be padded, up to 4 cols\n\tprintln(\"2 padded to 4:\")\n\tprintln(ColumnsN([]string{\n\t\t\"Column1\\ncontent1\",\n\t\t\"Column2\\ncontent2\",\n\t}, 4, true))\n\n\t// Output:\n\t// 4 columns in one gno-columns tag:\n\t// \u003cgno-columns\u003e\n\t// Column1\n\t// content1\n\t// \u003cgno-columns-sep\u003e\n\t// Column2\n\t// content2\n\t// \u003cgno-columns-sep\u003e\n\t// Column3\n\t// content3\n\t// \u003cgno-columns-sep\u003e\n\t// Column4\n\t// content4\n\t// \u003c/gno-columns\u003e\n\t//\n\t// 3 cols per row without padding:\n\t// \u003cgno-columns\u003e\n\t// Row1Column1\n\t// content1\n\t// \u003cgno-columns-sep\u003e\n\t// Row1Column2\n\t// content2\n\t// \u003cgno-columns-sep\u003e\n\t// Row1Column3\n\t// content3\n\t// \u003c/gno-columns\u003e\n\t// \u003cgno-columns\u003e\n\t// Row2Column1\n\t// content1\n\t// \u003cgno-columns-sep\u003e\n\t// Row2Column2\n\t// content2\n\t// \u003cgno-columns-sep\u003e\n\t// Row2Column3\n\t// content3\n\t// \u003c/gno-columns\u003e\n\t// \u003cgno-columns\u003e\n\t// Row3Column1\n\t// content1\n\t// \u003cgno-columns-sep\u003e\n\t// Row3Column2\n\t// content2\n\t// \u003cgno-columns-sep\u003e\n\t// Row3Column3\n\t// content3\n\t// \u003c/gno-columns\u003e\n\t//\n\t// 2 padded to 4:\n\t// \u003cgno-columns\u003e\n\t// Column1\n\t// content1\n\t// \u003cgno-columns-sep\u003e\n\t// Column2\n\t// content2\n\t// \u003cgno-columns-sep\u003e\n\t//\n\t// \u003cgno-columns-sep\u003e\n\t//\n\t// \u003c/gno-columns\u003e\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/md\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"md.gno","body":"// Package md provides helper functions for generating Markdown content programmatically.\n//\n// It includes utilities for text formatting, creating lists, blockquotes, code blocks,\n// links, images, and more.\n//\n// Highlights:\n// - Supports basic Markdown syntax such as bold, italic, strikethrough, headers, and lists.\n// - Manages multiline support in lists (e.g., bullet, ordered, and todo lists).\n// - Includes advanced helpers like inline images with links and nested list prefixes.\n//\n// For a comprehensive example of how to use these helpers, see:\n// https://gno.land/r/docs/moul_md\n//\n// # Sanitization contract\n//\n// Some helpers in this package sanitize their user-derived arguments\n// INTERNALLY (via p/nt/markdown/sanitize/v0). When using these, pass\n// raw user input — do NOT pre-wrap with sanitize.*, or you will\n// double-wrap (the escapers are not idempotent and double-wrap is a\n// bug):\n//\n//\tLink, UserLink, Image, InlineImageWithLink, FootnoteDefinition,\n//\tLinkReferenceDefinition, CollapsibleSection (title only),\n//\tInlineCode, CodeBlock, LanguageCodeBlock, Blockquote\n//\n// The other helpers DO NOT sanitize — they are pure builders that\n// wrap their input in markdown chrome. User-derived input reaches\n// the output unmodified, so callers MUST wrap with sanitize.* at the\n// call site:\n//\n//\tBold, Italic, Strikethrough, H1-H6, BulletList, BulletItem,\n//\tOrderedList, TodoList, TodoItem, Nested, Paragraph, Columns,\n//\tColumnsN, HorizontalRule\n//\n// Examples:\n//\n//\t// Sanitizing helper — pass raw:\n//\tout += md.Link(post.Title, post.URL)                                 // good\n//\tout += md.Link(sanitize.InlineText(post.Title), sanitize.URL(post.URL)) // BAD: double-wrap\n//\n//\t// Non-sanitizing helper — wrap once:\n//\tout += md.H2(sanitize.InlineText(post.Title))                        // good\n//\tout += md.H2(post.Title)                                             // BAD: raw user input\n//\tout += md.H2(sanitize.InlineText(sanitize.InlineText(post.Title)))   // BAD: double-wrap\n//\n// Composition: outputs of sanitizing helpers are safe markdown chrome\n// and can be embedded inside non-sanitizing helpers freely:\n//\n//\tout += md.H2(md.Link(post.Title, post.URL))   // good — H2 doesn't re-escape Link's output\n//\n// The reverse is unsafe: do NOT embed a non-sanitizing helper's\n// markdown chrome inside a sanitizing helper's arg, or the inner\n// markdown gets re-escaped:\n//\n//\tout += md.Link(md.Bold(post.Title), post.URL) // BAD: Link's internal sanitize\n//\t                                              // escapes the ** chars from md.Bold\npackage md\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/markdown/sanitize/v0\"\n)\n\n// Bold returns bold text for markdown.\n// Example: Bold(\"foo\") =\u003e \"**foo**\"\nfunc Bold(text string) string {\n\treturn \"**\" + text + \"**\"\n}\n\n// Italic returns italicized text for markdown.\n// Example: Italic(\"foo\") =\u003e \"*foo*\"\nfunc Italic(text string) string {\n\treturn \"*\" + text + \"*\"\n}\n\n// Strikethrough returns strikethrough text for markdown.\n// Example: Strikethrough(\"foo\") =\u003e \"~~foo~~\"\nfunc Strikethrough(text string) string {\n\treturn \"~~\" + text + \"~~\"\n}\n\n// H1 returns a level 1 header for markdown.\n// Example: H1(\"foo\") =\u003e \"# foo\\n\"\nfunc H1(text string) string {\n\treturn \"# \" + text + \"\\n\"\n}\n\n// H2 returns a level 2 header for markdown.\n// Example: H2(\"foo\") =\u003e \"## foo\\n\"\nfunc H2(text string) string {\n\treturn \"## \" + text + \"\\n\"\n}\n\n// H3 returns a level 3 header for markdown.\n// Example: H3(\"foo\") =\u003e \"### foo\\n\"\nfunc H3(text string) string {\n\treturn \"### \" + text + \"\\n\"\n}\n\n// H4 returns a level 4 header for markdown.\n// Example: H4(\"foo\") =\u003e \"#### foo\\n\"\nfunc H4(text string) string {\n\treturn \"#### \" + text + \"\\n\"\n}\n\n// H5 returns a level 5 header for markdown.\n// Example: H5(\"foo\") =\u003e \"##### foo\\n\"\nfunc H5(text string) string {\n\treturn \"##### \" + text + \"\\n\"\n}\n\n// H6 returns a level 6 header for markdown.\n// Example: H6(\"foo\") =\u003e \"###### foo\\n\"\nfunc H6(text string) string {\n\treturn \"###### \" + text + \"\\n\"\n}\n\n// BulletList returns a bullet list for markdown.\n// Example: BulletList([]string{\"foo\", \"bar\"}) =\u003e \"- foo\\n- bar\\n\"\nfunc BulletList(items []string) string {\n\tvar sb strings.Builder\n\tfor _, item := range items {\n\t\tsb.WriteString(BulletItem(item))\n\t}\n\treturn sb.String()\n}\n\n// BulletItem returns a bullet item for markdown.\n// Example: BulletItem(\"foo\") =\u003e \"- foo\\n\"\nfunc BulletItem(item string) string {\n\tvar sb strings.Builder\n\tlines := strings.Split(item, \"\\n\")\n\tsb.WriteString(\"- \" + lines[0] + \"\\n\")\n\tfor _, line := range lines[1:] {\n\t\tsb.WriteString(\"  \" + line + \"\\n\")\n\t}\n\treturn sb.String()\n}\n\n// OrderedList returns an ordered list for markdown.\n// Example: OrderedList([]string{\"foo\", \"bar\"}) =\u003e \"1. foo\\n2. bar\\n\"\nfunc OrderedList(items []string) string {\n\tvar sb strings.Builder\n\tfor i, item := range items {\n\t\tlines := strings.Split(item, \"\\n\")\n\t\tsb.WriteString(strconv.Itoa(i+1) + \". \" + lines[0] + \"\\n\")\n\t\tfor _, line := range lines[1:] {\n\t\t\tsb.WriteString(\"   \" + line + \"\\n\")\n\t\t}\n\t}\n\treturn sb.String()\n}\n\n// TodoList returns a list of todo items with checkboxes for markdown.\n// Example: TodoList([]string{\"foo\", \"bar\\nmore bar\"}, []bool{true, false}) =\u003e \"- [x] foo\\n- [ ] bar\\n  more bar\\n\"\nfunc TodoList(items []string, done []bool) string {\n\tvar sb strings.Builder\n\tfor i, item := range items {\n\t\tsb.WriteString(TodoItem(item, done[i]))\n\t}\n\treturn sb.String()\n}\n\n// TodoItem returns a todo item with checkbox for markdown.\n// Example: TodoItem(\"foo\", true) =\u003e \"- [x] foo\\n\"\nfunc TodoItem(item string, done bool) string {\n\tvar sb strings.Builder\n\tcheckbox := \" \"\n\tif done {\n\t\tcheckbox = \"x\"\n\t}\n\tlines := strings.Split(item, \"\\n\")\n\tsb.WriteString(\"- [\" + checkbox + \"] \" + lines[0] + \"\\n\")\n\tfor _, line := range lines[1:] {\n\t\tsb.WriteString(\"  \" + line + \"\\n\")\n\t}\n\treturn sb.String()\n}\n\n// Nested prefixes each line with a given prefix, enabling nested lists.\n// Example: Nested(\"- foo\\n- bar\", \"  \") =\u003e \"  - foo\\n  - bar\\n\"\nfunc Nested(content, prefix string) string {\n\tlines := strings.Split(content, \"\\n\")\n\tfor i := range lines {\n\t\tif strings.TrimSpace(lines[i]) != \"\" {\n\t\t\tlines[i] = prefix + lines[i]\n\t\t}\n\t}\n\treturn strings.Join(lines, \"\\n\")\n}\n\n// Blockquote returns the text as a CommonMark blockquote.\n// Example: Blockquote(\"foo\\nbar\") =\u003e \"\\n\u003e foo\\n\u003e bar\\n\\n\"\n//\n// Delegates to sanitize.Blockquote, which cleans the content (bidi-strip,\n// CR/CRLF/U+2028/U+2029/NEL line-ending normalize, LRD strip, ref-link\n// escape, block-marker escape, fence auto-close), line-prefixes each\n// line with \"\u003e \", and wraps with \"\\n\" / \"\\n\\n\" so the quote opens\n// cleanly and cannot pull appended chrome into the quote via CM §5.2\n// lazy continuation. Callers do NOT need to pre-wrap the input.\nfunc Blockquote(text string) string {\n\treturn sanitize.Blockquote(text)\n}\n\n// InlineCode wraps the given text as a CommonMark inline code span.\n// Example: InlineCode(\"foo\") =\u003e \"`foo`\"\n//\n// Delegates to sanitize.InlineCode, which cleans the input (bidi-strip,\n// CR/CRLF + NEL + U+2028/U+2029 folded to single space, NUL→U+FFFD)\n// and picks a backtick-run length that outscans any internal backticks.\n// Callers do NOT need to pre-wrap the input.\nfunc InlineCode(code string) string {\n\treturn sanitize.InlineCode(code)\n}\n\n// CodeBlock creates a markdown code block.\n// Example: CodeBlock(\"foo\") =\u003e \"```\\nfoo\\n```\"\n//\n// Delegates to sanitize.CodeBlock, which cleans the content (bidi-strip,\n// CR/CRLF normalize, NEL/U+2028/U+2029 fold, NUL→U+FFFD) and picks a\n// fence wide enough to outscan any backticks in content. Callers do NOT\n// need to pre-wrap content with another sanitize helper.\nfunc CodeBlock(content string) string {\n\treturn sanitize.CodeBlock(content)\n}\n\n// LanguageCodeBlock creates a markdown code block with language-specific syntax highlighting.\n// Example: LanguageCodeBlock(\"go\", \"foo\") =\u003e \"```go\\nfoo\\n```\"\n//\n// Delegates to sanitize.LanguageCodeBlock, which validates the language\n// tag (charset ^[a-zA-Z0-9_+-]{1,32}$, falling back to a tagless fence\n// if invalid) and cleans the content as CodeBlock does. Callers do NOT\n// need to pre-wrap either argument.\nfunc LanguageCodeBlock(language, content string) string {\n\treturn sanitize.LanguageCodeBlock(language, content)\n}\n\n// HorizontalRule returns a horizontal rule for markdown.\n// Example: HorizontalRule() =\u003e \"---\\n\"\nfunc HorizontalRule() string {\n\treturn \"---\\n\"\n}\n\n// Link returns a hyperlink for markdown.\n// Example: Link(\"foo\", \"http://example.com\") =\u003e \"[foo](http://example.com)\"\n//\n// The text and url args are sanitized internally — text via\n// sanitize.InlineText, url via sanitize.URL (allowlists http/https/\n// mailto/relative/fragment; rejects javascript:, data:, blob:, etc.).\n// Callers do NOT need to pre-wrap either argument; double-wrapping is\n// a bug (the inline-text escaper is non-idempotent).\n//\n// If the URL fails the scheme allowlist, the href is rendered empty —\n// the link becomes inert rather than carrying a malicious destination.\nfunc Link(text, url string) string {\n\treturn \"[\" + sanitize.InlineText(text) + \"](\" + sanitize.URL(url) + \")\"\n}\n\n// UserLink returns a user profile link for markdown.\n// Example: UserLink(\"moul\") =\u003e \"[@moul](/u/moul)\"\n// Example: UserLink(\"g1blah...\") =\u003e \"[g1blah...](/u/g1blah...)\"\n//\n// Validates the user identifier — if it matches the gno bech32 address\n// pattern (g1...), produces an address-style link; otherwise tries the\n// r/sys/users-charset username and produces an @-style link. Returns\n// \"\" if the identifier matches neither — callers should treat \"\" as\n// \"skip the user mention\" rather than emit a broken link.\nfunc UserLink(user string) string {\n\tif addr := sanitize.BechString(user, \"g\"); addr != \"\" {\n\t\treturn \"[\" + addr + \"](/u/\" + addr + \")\"\n\t}\n\tif name := sanitize.UserName(user); name != \"\" {\n\t\treturn \"[@\" + name + \"](/u/\" + name + \")\"\n\t}\n\treturn \"\"\n}\n\n// InlineImageWithLink creates an inline image wrapped in a hyperlink for markdown.\n// Example: InlineImageWithLink(\"alt text\", \"image-url\", \"link-url\") =\u003e \"[![alt text](image-url)](link-url)\"\n//\n// altText and imageUrl are sanitized via Image (sanitize.InlineText +\n// sanitize.ImageURL); linkUrl is sanitized via sanitize.URL. Callers\n// do NOT need to pre-wrap any argument.\nfunc InlineImageWithLink(altText, imageUrl, linkUrl string) string {\n\treturn \"[\" + Image(altText, imageUrl) + \"](\" + sanitize.URL(linkUrl) + \")\"\n}\n\n// Image returns an image for markdown.\n// Example: Image(\"foo\", \"http://example.com\") =\u003e \"![foo](http://example.com)\"\n//\n// altText is sanitized via sanitize.InlineText, url via\n// sanitize.ImageURL (allowlists http/https/relative + data:image/*;\n// rejects mailto:, javascript:, data:text/html, etc.). Callers do NOT\n// need to pre-wrap either argument.\nfunc Image(altText, url string) string {\n\treturn \"![\" + sanitize.InlineText(altText) + \"](\" + sanitize.ImageURL(url) + \")\"\n}\n\n// FootnoteDefinition emits a GFM footnote definition — `[^name]: body` —\n// for a footnote that is referenced elsewhere in the document by\n// `[^name]`.\n//\n// Example: FootnoteDefinition(\"note1\", \"Long form of the citation.\")\n// renders as:\n//\n//\t[^note1]:\n//\t    Long form of the citation.\n//\n// The `name` is validated as a FootnoteLabel (^[A-Za-z0-9_-]{1,64}$);\n// `text` is user-supplied multi-paragraph prose, sanitized via Block.\n// An invalid name or empty body returns \"\".\n//\n// Delegates to sanitize.FootnoteDefinition. Callers do NOT need to\n// pre-wrap either argument.\nfunc FootnoteDefinition(name, text string) string {\n\treturn sanitize.FootnoteDefinition(name, text)\n}\n\n// LinkReferenceDefinition emits a CommonMark link reference definition\n// (CM §4.7) — `[label]: url \"title\"` — for a reference link that is\n// invoked elsewhere by `[text][label]` or by the shortcut form\n// `[label]`.\n//\n// Example: LinkReferenceDefinition(\"r/docs/help\", \"/r/docs/help\", \"\")\n// renders as:\n//\n//\t[r/docs/help]: /r/docs/help\n//\n// The `label` is validated as a FootnoteLabel (^[A-Za-z0-9_-]{1,64}$);\n// `url` is sanitized via URL (allowlist); `title` is sanitized via\n// LinkTitle. An invalid label or rejected URL returns \"\".\n//\n// Realms should choose a namespaced label using dashes\n// (e.g. `r-myrealm-help`) so that shortcut-reference invocations from\n// user content can't collide with bare words a user is likely to write.\n// `/` is not in the FootnoteLabel charset.\n//\n// Delegates to sanitize.LinkReferenceDefinition. Callers do NOT need to\n// pre-wrap any argument.\nfunc LinkReferenceDefinition(label, url, title string) string {\n\treturn sanitize.LinkReferenceDefinition(label, url, title)\n}\n\n// Paragraph wraps the given text in a Markdown paragraph.\n// Example: Paragraph(\"foo\") =\u003e \"foo\\n\"\nfunc Paragraph(content string) string {\n\treturn content + \"\\n\\n\"\n}\n\n// CollapsibleSection creates a collapsible section for markdown using\n// HTML \u003cdetails\u003e and \u003csummary\u003e tags.\n// Example:\n// CollapsibleSection(\"Click to expand\", \"Hidden content\")\n// =\u003e\n// \u003cdetails\u003e\u003csummary\u003eClick to expand\u003c/summary\u003e\n//\n// Hidden content\n// \u003c/details\u003e\n//\n// The title argument is sanitized via sanitize.HTMLEscape (it lands in\n// an HTML element body inside \u003csummary\u003e, not a markdown context — so\n// HTML entity escaping is the correct policy, not markdown backslash\n// escaping). The content argument is passed through unchanged because\n// \u003cdetails\u003e with a blank-line-separated body allows markdown inside\n// (CM §4.6); callers must pre-wrap content with sanitize.Block if it\n// derives from user input.\nfunc CollapsibleSection(title, content string) string {\n\treturn \"\u003cdetails\u003e\u003csummary\u003e\" + sanitize.HTMLEscape(title) + \"\u003c/summary\u003e\\n\\n\" + content + \"\\n\u003c/details\u003e\\n\"\n}\n\n// EscapeURL escapes characters in a URL for use in markdown link syntax.\n//\n// Deprecated: use sanitize.URL (for link href) or sanitize.ImageURL\n// (for image src) directly. EscapeURL previously only percent-encoded\n// ( and ) and did not validate the URL scheme — a security footgun\n// (EscapeURL(\"javascript:alert(1)\") returned a working XSS payload).\n// It now delegates to sanitize.URL, which allowlists schemes (rejects\n// javascript:, data:text/html, vbscript:, blob:, etc.) and\n// percent-encodes all unsafe bytes (including the ( ) this function\n// handled). The other helpers in this package (Link, UserLink, Image,\n// InlineImageWithLink) sanitize their URL args internally now, so you\n// rarely need to call any URL-escape helper directly.\n//\n// Behavior change vs. the original implementation: invalid schemes\n// now return \"\" instead of passing through with ( ) escaped. Non-ASCII\n// bytes get percent-encoded to standard RFC 3986 wire form instead of\n// passing through as raw UTF-8.\nfunc EscapeURL(url string) string {\n\treturn sanitize.URL(url)\n}\n\n// EscapeText escapes special Markdown characters in regular text for\n// use in inline contexts.\n//\n// Deprecated: use sanitize.InlineText directly. EscapeText was\n// INCOMPLETE — it missed \\, #, \u003c, \u0026 and did not strip bidi/zero-width\n// characters, replace NUL with U+FFFD, or normalize line endings.\n// User input could inject backslash escapes (\\* cancels neighboring\n// escapes), autolinks (\u003chttps://x\u003e), raw HTML (\u003cscript\u003e), HTML entity\n// references (\u0026amp;), and bidi spoofing (RLO before an address). It\n// now delegates to sanitize.InlineText. The other helpers in this\n// package (Link, UserLink, Image, InlineImageWithLink, CollapsibleSection)\n// sanitize their text args internally now, so you rarely need to call\n// any inline-text-escape helper directly.\n//\n// Behavior change vs. the original implementation: \\, #, \u003c, \u0026 are now\n// escaped; | is no longer escaped (the original was over-escaping —\n// outside GFM table-cell context, | is markdown-inert; for table\n// cells use sanitize.TableCell which adds the | escape on top of the\n// inline-text set). Bidi controls are stripped, NUL becomes U+FFFD,\n// and line endings normalize.\nfunc EscapeText(text string) string {\n\treturn sanitize.InlineText(text)\n}\n\n// Columns returns a formatted row of columns using the Gno syntax.\n// If you want a specific number of columns per row (\u003c=4), use ColumnsN.\n// Check /r/docs/markdown#columns for more info.\n// If padded=true \u0026 the final \u003cgno-columns\u003e tag is missing column content, an empty\n// column element will be placed to keep the cols per row constant.\n// Padding works only with colsPerRow \u003e 0.\n//\n// Example:\n//\n//\tColumns([]string{\"A\", \"B\"}, false)\n//\t// Returns:\n//\t// \u003cgno-columns\u003e\n//\t// A\n//\t// \u003cgno-columns-sep\u003e\n//\t// B\n//\t// \u003c/gno-columns\u003e\nfunc Columns(contentByColumn []string, padded bool) string {\n\tif len(contentByColumn) == 0 {\n\t\treturn \"\"\n\t}\n\tmaxCols := 4\n\tif padded \u0026\u0026 len(contentByColumn)%maxCols != 0 {\n\t\tmissing := maxCols - len(contentByColumn)%maxCols\n\t\tcontentByColumn = append(contentByColumn, make([]string, missing)...)\n\t}\n\n\tvar sb strings.Builder\n\tsb.WriteString(\"\u003cgno-columns\u003e\\n\")\n\n\tfor i, column := range contentByColumn {\n\t\tif i \u003e 0 {\n\t\t\tsb.WriteString(\"\u003cgno-columns-sep\u003e\\n\")\n\t\t}\n\t\tsb.WriteString(column + \"\\n\")\n\t}\n\n\tsb.WriteString(\"\u003c/gno-columns\u003e\\n\")\n\treturn sb.String()\n}\n\nconst maxColumnsPerRow = 4\n\n// ColumnsN splits content into multiple rows of N columns each and formats them.\n// If colsPerRow \u003c= 0, all items are placed in one \u003cgno-columns\u003e block.\n// If padded=true \u0026 the final \u003cgno-columns\u003e tag is missing column content, an empty\n// column element will be placed to keep the cols per row constant.\n// Padding works only with colsPerRow \u003e 0.\n// Note: On standard-size screens, gnoweb handles a max of 4 cols per row.\n//\n// Example:\n//\n//\tColumnsN([]string{\"A\", \"B\", \"C\"}, 2, false)\n//\t// Returns:\n//\t// \u003cgno-columns\u003e\n//\t// A\n//\t// \u003cgno-columns-sep\u003e\n//\t// B\n//\t// \u003c/gno-columns\u003e\n//\t// \u003cgno-columns\u003e\n//\t// C\n//\t// \u003c/gno-columns\u003e\nfunc ColumnsN(content []string, colsPerRow int, padded bool) string {\n\tif len(content) == 0 {\n\t\treturn \"\"\n\t}\n\tif colsPerRow \u003c= 0 {\n\t\treturn Columns(content, padded)\n\t}\n\n\tvar sb strings.Builder\n\t// Case 2: Multiple blocks with max 4 columns\n\tfor i := 0; i \u003c len(content); i += colsPerRow {\n\t\tend := i + colsPerRow\n\t\tif end \u003e len(content) {\n\t\t\tend = len(content)\n\t\t}\n\t\trow := content[i:end]\n\n\t\t// Add padding if needed\n\t\tif padded \u0026\u0026 len(row) \u003c colsPerRow {\n\t\t\trow = append(row, make([]string, colsPerRow-len(row))...)\n\t\t}\n\n\t\tsb.WriteString(Columns(row, false))\n\t}\n\treturn sb.String()\n}\n"},{"name":"md_test.gno","body":"package md_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/md\"\n)\n\nfunc TestHelpers(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tfunction func() string\n\t\texpected string\n\t}{\n\t\t{\"Bold\", func() string { return md.Bold(\"foo\") }, \"**foo**\"},\n\t\t{\"Italic\", func() string { return md.Italic(\"foo\") }, \"*foo*\"},\n\t\t{\"Strikethrough\", func() string { return md.Strikethrough(\"foo\") }, \"~~foo~~\"},\n\t\t{\"H1\", func() string { return md.H1(\"foo\") }, \"# foo\\n\"},\n\t\t{\"HorizontalRule\", md.HorizontalRule, \"---\\n\"},\n\t\t{\"InlineCode\", func() string { return md.InlineCode(\"foo\") }, \"`foo`\"},\n\t\t{\"CodeBlock\", func() string { return md.CodeBlock(\"foo\") }, \"```\\nfoo\\n```\\n\"},\n\t\t{\"LanguageCodeBlock\", func() string { return md.LanguageCodeBlock(\"go\", \"foo\") }, \"```go\\nfoo\\n```\\n\"},\n\t\t{\"Link\", func() string { return md.Link(\"foo\", \"http://example.com\") }, \"[foo](http://example.com)\"},\n\t\t{\"UserLink\", func() string { return md.UserLink(\"moul\") }, \"[@moul](/u/moul)\"},\n\t\t{\"Image\", func() string { return md.Image(\"foo\", \"http://example.com\") }, \"![foo](http://example.com)\"},\n\t\t{\"InlineImageWithLink\", func() string {\n\t\t\treturn md.InlineImageWithLink(\"alt\", \"http://img.example.com/x.png\", \"http://link.example.com\")\n\t\t}, \"[![alt](http://img.example.com/x.png)](http://link.example.com)\"},\n\t\t{\"FootnoteDefinition\", func() string { return md.FootnoteDefinition(\"foo\", \"bar\") }, \"[^foo]:\\n    bar\\n\"},\n\t\t{\"LinkReferenceDefinition\", func() string {\n\t\t\treturn md.LinkReferenceDefinition(\"foo\", \"http://example.com\", \"\")\n\t\t}, \"\\n\\n[foo]: http://example.com\\n\\n\"},\n\t\t{\"Paragraph\", func() string { return md.Paragraph(\"foo\") }, \"foo\\n\\n\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := tt.function()\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"%s() = %q, want %q\", tt.name, result, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestLists(t *testing.T) {\n\tt.Run(\"BulletList\", func(t *testing.T) {\n\t\titems := []string{\"foo\", \"bar\"}\n\t\texpected := \"- foo\\n- bar\\n\"\n\t\tresult := md.BulletList(items)\n\t\tif result != expected {\n\t\t\tt.Errorf(\"BulletList(%q) = %q, want %q\", items, result, expected)\n\t\t}\n\t})\n\n\tt.Run(\"OrderedList\", func(t *testing.T) {\n\t\titems := []string{\"foo\", \"bar\"}\n\t\texpected := \"1. foo\\n2. bar\\n\"\n\t\tresult := md.OrderedList(items)\n\t\tif result != expected {\n\t\t\tt.Errorf(\"OrderedList(%q) = %q, want %q\", items, result, expected)\n\t\t}\n\t})\n\n\tt.Run(\"TodoList\", func(t *testing.T) {\n\t\titems := []string{\"foo\", \"bar\\nmore bar\"}\n\t\tdone := []bool{true, false}\n\t\texpected := \"- [x] foo\\n- [ ] bar\\n  more bar\\n\"\n\t\tresult := md.TodoList(items, done)\n\t\tif result != expected {\n\t\t\tt.Errorf(\"TodoList(%q, %q) = %q, want %q\", items, done, result, expected)\n\t\t}\n\t})\n}\n\nfunc TestUserLink(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    string\n\t\texpected string\n\t}{\n\t\t{\"username\", \"moul\", \"[@moul](/u/moul)\"},\n\t\t// \"g1blah\" data part is too short for a bech32 address (min 6\n\t\t// chars after \"1\"); UserLink validates as a username instead.\n\t\t{\"short g1-prefixed string treated as username\", \"g1blah\", \"[@g1blah](/u/g1blah)\"},\n\t\t// \"user_name\" is a valid username (charset includes _ and -);\n\t\t// the validator returns it verbatim, so no markdown escape of _.\n\t\t{\"username with underscore\", \"user_name\", \"[@user_name](/u/user_name)\"},\n\t\t// \"g1abc123\" has a 6-char data part — valid bech32 shape.\n\t\t{\"address with 6-char data\", \"g1abc123\", \"[g1abc123](/u/g1abc123)\"},\n\t\t// invalid identifier (spaces, uppercase, etc.) → empty.\n\t\t{\"rejected: spaces\", \"hello world\", \"\"},\n\t\t{\"rejected: uppercase first char\", \"Alice\", \"\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := md.UserLink(tt.input)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"UserLink(%q) = %q, want %q\", tt.input, result, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNested(t *testing.T) {\n\tt.Run(\"Nested Single Level\", func(t *testing.T) {\n\t\tcontent := \"- foo\\n- bar\"\n\t\texpected := \"  - foo\\n  - bar\"\n\t\tresult := md.Nested(content, \"  \")\n\t\tif result != expected {\n\t\t\tt.Errorf(\"Nested(%q) = %q, want %q\", content, result, expected)\n\t\t}\n\t})\n\n\tt.Run(\"Nested Double Level\", func(t *testing.T) {\n\t\tcontent := \"  - foo\\n  - bar\"\n\t\texpected := \"    - foo\\n    - bar\"\n\t\tresult := md.Nested(content, \"  \")\n\t\tif result != expected {\n\t\t\tt.Errorf(\"Nested(%q) = %q, want %q\", content, result, expected)\n\t\t}\n\t})\n}\n\nfunc TestColumns(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\tpadded   bool\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"no columns\",\n\t\t\tinput:    []string{},\n\t\t\tpadded:   false,\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:     \"no columns padded\",\n\t\t\tinput:    []string{},\n\t\t\tpadded:   true,\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:   \"one column\",\n\t\t\tinput:  []string{\"Column 1\"},\n\t\t\tpadded: false,\n\t\t\texpected: `\u003cgno-columns\u003e\nColumn 1\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:   \"one column padded\",\n\t\t\tinput:  []string{\"Column 1\"},\n\t\t\tpadded: true,\n\t\t\texpected: `\u003cgno-columns\u003e\nColumn 1\n\u003cgno-columns-sep\u003e\n\n\u003cgno-columns-sep\u003e\n\n\u003cgno-columns-sep\u003e\n\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:   \"two columns\",\n\t\t\tinput:  []string{\"Column 1\", \"Column 2\"},\n\t\t\tpadded: false,\n\t\t\texpected: `\u003cgno-columns\u003e\nColumn 1\n\u003cgno-columns-sep\u003e\nColumn 2\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:   \"two columns padded\",\n\t\t\tinput:  []string{\"Column 1\", \"Column 2\"},\n\t\t\tpadded: true,\n\t\t\texpected: `\u003cgno-columns\u003e\nColumn 1\n\u003cgno-columns-sep\u003e\nColumn 2\n\u003cgno-columns-sep\u003e\n\n\u003cgno-columns-sep\u003e\n\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:   \"four columns\",\n\t\t\tinput:  []string{\"A\", \"B\", \"C\", \"D\"},\n\t\t\tpadded: false,\n\t\t\texpected: `\u003cgno-columns\u003e\nA\n\u003cgno-columns-sep\u003e\nB\n\u003cgno-columns-sep\u003e\nC\n\u003cgno-columns-sep\u003e\nD\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:   \"four columns padded\",\n\t\t\tinput:  []string{\"A\", \"B\", \"C\", \"D\"},\n\t\t\tpadded: true,\n\t\t\texpected: `\u003cgno-columns\u003e\nA\n\u003cgno-columns-sep\u003e\nB\n\u003cgno-columns-sep\u003e\nC\n\u003cgno-columns-sep\u003e\nD\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:   \"more than four columns\",\n\t\t\tinput:  []string{\"1\", \"2\", \"3\", \"4\", \"5\"},\n\t\t\tpadded: false,\n\t\t\texpected: `\u003cgno-columns\u003e\n1\n\u003cgno-columns-sep\u003e\n2\n\u003cgno-columns-sep\u003e\n3\n\u003cgno-columns-sep\u003e\n4\n\u003cgno-columns-sep\u003e\n5\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:   \"more than four columns padded\",\n\t\t\tinput:  []string{\"1\", \"2\", \"3\", \"4\", \"5\"},\n\t\t\tpadded: true,\n\t\t\texpected: `\u003cgno-columns\u003e\n1\n\u003cgno-columns-sep\u003e\n2\n\u003cgno-columns-sep\u003e\n3\n\u003cgno-columns-sep\u003e\n4\n\u003cgno-columns-sep\u003e\n5\n\u003cgno-columns-sep\u003e\n\n\u003cgno-columns-sep\u003e\n\n\u003cgno-columns-sep\u003e\n\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := md.Columns(tt.input, tt.padded)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"Columns(%v, %v) =\\n%q\\nwant:\\n%q\", tt.input, tt.padded, result, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestColumnsN(t *testing.T) {\n\ttests := []struct {\n\t\tname       string\n\t\tcontent    []string\n\t\tcolsPerRow int\n\t\tpadded     bool\n\t\texpected   string\n\t}{\n\t\t{\n\t\t\tname:       \"empty input\",\n\t\t\tcontent:    []string{},\n\t\t\tcolsPerRow: 2,\n\t\t\tpadded:     false,\n\t\t\texpected:   \"\",\n\t\t},\n\t\t{\n\t\t\tname:       \"colsPerRow \u003c= 0\",\n\t\t\tcontent:    []string{\"A\", \"B\", \"C\"},\n\t\t\tcolsPerRow: 0,\n\t\t\tpadded:     false,\n\t\t\texpected: `\u003cgno-columns\u003e\nA\n\u003cgno-columns-sep\u003e\nB\n\u003cgno-columns-sep\u003e\nC\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:       \"exact full row, no padding\",\n\t\t\tcontent:    []string{\"A\", \"B\"},\n\t\t\tcolsPerRow: 2,\n\t\t\tpadded:     false,\n\t\t\texpected: `\u003cgno-columns\u003e\nA\n\u003cgno-columns-sep\u003e\nB\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:       \"partial last row, no padding\",\n\t\t\tcontent:    []string{\"A\", \"B\", \"C\"},\n\t\t\tcolsPerRow: 2,\n\t\t\tpadded:     false,\n\t\t\texpected: `\u003cgno-columns\u003e\nA\n\u003cgno-columns-sep\u003e\nB\n\u003c/gno-columns\u003e\n\u003cgno-columns\u003e\nC\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:       \"partial last row, with padding\",\n\t\t\tcontent:    []string{\"A\", \"B\", \"C\"},\n\t\t\tcolsPerRow: 2,\n\t\t\tpadded:     true,\n\t\t\texpected: `\u003cgno-columns\u003e\nA\n\u003cgno-columns-sep\u003e\nB\n\u003c/gno-columns\u003e\n\u003cgno-columns\u003e\nC\n\u003cgno-columns-sep\u003e\n\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t\t{\n\t\t\tname:       \"padded with more empty cells\",\n\t\t\tcontent:    []string{\"X\"},\n\t\t\tcolsPerRow: 3,\n\t\t\tpadded:     true,\n\t\t\texpected: `\u003cgno-columns\u003e\nX\n\u003cgno-columns-sep\u003e\n\n\u003cgno-columns-sep\u003e\n\n\u003c/gno-columns\u003e\n`,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := md.ColumnsN(tt.content, tt.colsPerRow, tt.padded)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"ColumnsN(%v, %d, %v) =\\n%q\\nwant:\\n%q\", tt.content, tt.colsPerRow, tt.padded, result, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"qSQHp7VdckeNlQkUHptTGTuPFtnXGD8MZWEEkytT3LWTIAnJRy522xdA3LVh3Ot9xgFsk/xk/bt22t7itoZABw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"rotree","path":"gno.land/p/nt/avl/v0/rotree","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/avl/v0/rotree\"\ngno = \"0.9\"\n"},{"name":"rotree.gno","body":"// Package rotree provides a read-only wrapper for avl.Tree with safe value transformation.\n//\n// It is useful when you want to expose a read-only view of a tree while ensuring that\n// the sensitive data cannot be modified.\n//\n// Example:\n//\n//\t// Define a user structure with sensitive data\n//\ttype User struct {\n//\t\tName     string\n//\t\tBalance  int\n//\t\tInternal string // sensitive field\n//\t}\n//\n//\t// Create and populate the original tree\n//\tprivateTree := avl.NewTree()\n//\tprivateTree.Set(\"alice\", \u0026User{\n//\t\tName:     \"Alice\",\n//\t\tBalance:  100,\n//\t\tInternal: \"sensitive\",\n//\t})\n//\n//\t// Create a safe transformation function that copies the struct\n//\t// while excluding sensitive data\n//\tmakeEntrySafeFn := func(v any) any {\n//\t\tu := v.(*User)\n//\t\treturn \u0026User{\n//\t\t\tName:     u.Name,\n//\t\t\tBalance:  u.Balance,\n//\t\t\tInternal: \"\", // omit sensitive data\n//\t\t}\n//\t}\n//\n//\t// Create a read-only view of the tree\n//\tPublicTree := rotree.Wrap(tree, makeEntrySafeFn)\n//\n//\t// Safely access the data\n//\tvalue := roTree.Get(\"alice\")\n//\tuser := value.(*User)\n//\t// user.Name == \"Alice\"\n//\t// user.Balance == 100\n//\t// user.Internal == \"\" (sensitive data is filtered)\npackage rotree\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// Wrap creates a new ReadOnlyTree from an existing avl.Tree and a safety transformation function.\n// If makeEntrySafeFn is nil, values will be returned as-is without transformation.\n//\n// makeEntrySafeFn is a function that transforms a tree entry into a safe version that can be exposed to external users.\n// This function should be implemented based on the specific safety requirements of your use case:\n//\n//  1. No-op transformation: For primitive types (int, string, etc.) or already safe objects,\n//     simply pass nil as the makeEntrySafeFn to return values as-is.\n//\n//  2. Defensive copying: For mutable types like slices or maps, you should create a deep copy\n//     to prevent modification of the original data.\n//     Example: func(v any) any { return append([]int{}, v.([]int)...) }\n//\n//  3. Read-only wrapper: Return a read-only version of the object that implements\n//     a limited interface.\n//     Example: func(v any) any { return NewReadOnlyObject(v) }\n//\n//  4. DAO transformation: Transform the object into a data access object that\n//     controls how the underlying data can be accessed.\n//     Example: func(v any) any { return NewDAO(v) }\n//\n// The function ensures that the returned object is safe to expose to untrusted code,\n// preventing unauthorized modifications to the original data structure.\nfunc Wrap(tree *avl.Tree, makeEntrySafeFn func(any) any) *ReadOnlyTree {\n\treturn \u0026ReadOnlyTree{\n\t\ttree:            tree,\n\t\tmakeEntrySafeFn: makeEntrySafeFn,\n\t}\n}\n\n// ReadOnlyTree wraps an avl.Tree and provides read-only access.\ntype ReadOnlyTree struct {\n\ttree            *avl.Tree\n\tmakeEntrySafeFn func(any) any\n}\n\n// IReadOnlyTree defines the read-only operations available on a tree.\ntype IReadOnlyTree interface {\n\tSize() int\n\tHas(key string) bool\n\tGet(key string) any\n\tGetByIndex(index int) (string, any)\n\tIterate(start, end string, cb avl.IterCbFn) bool\n\tReverseIterate(start, end string, cb avl.IterCbFn) bool\n\tIterateByOffset(offset int, count int, cb avl.IterCbFn) bool\n\tReverseIterateByOffset(offset int, count int, cb avl.IterCbFn) bool\n}\n\n// Verify that ReadOnlyTree implements both ITree and IReadOnlyTree\nvar (\n\t_ avl.ITree     = (*ReadOnlyTree)(nil)\n\t_ IReadOnlyTree = (*ReadOnlyTree)(nil)\n)\n\n// getSafeValue applies the makeEntrySafeFn if it exists, otherwise returns the original value\nfunc (roTree *ReadOnlyTree) getSafeValue(value any) any {\n\tif roTree.makeEntrySafeFn == nil {\n\t\treturn value\n\t}\n\treturn roTree.makeEntrySafeFn(value)\n}\n\n// Size returns the number of key-value pairs in the tree.\nfunc (roTree *ReadOnlyTree) Size() int {\n\treturn roTree.tree.Size()\n}\n\n// Has checks whether a key exists in the tree.\nfunc (roTree *ReadOnlyTree) Has(key string) bool {\n\treturn roTree.tree.Has(key)\n}\n\n// Get retrieves the value associated with the given key, converted to a safe format.\n// It returns the value if the key exists, or nil if it doesn't.\n// Note that a key stored with a nil value is indistinguishable\n// from an absent key; use Has to check for existence.\nfunc (roTree *ReadOnlyTree) Get(key string) any {\n\tvalue := roTree.tree.Get(key)\n\tif value == nil {\n\t\treturn nil\n\t}\n\treturn roTree.getSafeValue(value)\n}\n\n// GetByIndex retrieves the key-value pair at the specified index in the tree, with the value converted to a safe format.\nfunc (roTree *ReadOnlyTree) GetByIndex(index int) (string, any) {\n\tkey, value := roTree.tree.GetByIndex(index)\n\treturn key, roTree.getSafeValue(value)\n}\n\n// Iterate performs an in-order traversal of the tree within the specified key range.\nfunc (roTree *ReadOnlyTree) Iterate(start, end string, cb avl.IterCbFn) bool {\n\treturn roTree.tree.Iterate(start, end, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// ReverseIterate performs a reverse in-order traversal of the tree within the specified key range.\nfunc (roTree *ReadOnlyTree) ReverseIterate(start, end string, cb avl.IterCbFn) bool {\n\treturn roTree.tree.ReverseIterate(start, end, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// IterateByOffset performs an in-order traversal of the tree starting from the specified offset.\nfunc (roTree *ReadOnlyTree) IterateByOffset(offset int, count int, cb avl.IterCbFn) bool {\n\treturn roTree.tree.IterateByOffset(offset, count, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// ReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset.\nfunc (roTree *ReadOnlyTree) ReverseIterateByOffset(offset int, count int, cb avl.IterCbFn) bool {\n\treturn roTree.tree.ReverseIterateByOffset(offset, count, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// Set is not supported on ReadOnlyTree and will panic.\nfunc (roTree *ReadOnlyTree) Set(key string, value any) bool {\n\tpanic(\"Set operation not supported on ReadOnlyTree\")\n}\n\n// Remove is not supported on ReadOnlyTree and will panic.\nfunc (roTree *ReadOnlyTree) Remove(key string) (value any, removed bool) {\n\tpanic(\"Remove operation not supported on ReadOnlyTree\")\n}\n\n// RemoveByIndex is not supported on ReadOnlyTree and will panic.\nfunc (roTree *ReadOnlyTree) RemoveByIndex(index int) (key string, value any) {\n\tpanic(\"RemoveByIndex operation not supported on ReadOnlyTree\")\n}\n"},{"name":"rotree_test.gno","body":"package rotree\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nfunc TestExample(t *testing.T) {\n\t// User represents our internal data structure\n\ttype User struct {\n\t\tID       string\n\t\tName     string\n\t\tBalance  int\n\t\tInternal string // sensitive internal data\n\t}\n\n\t// Create and populate the original tree with user pointers\n\ttree := avl.NewTree()\n\ttree.Set(\"alice\", \u0026User{\n\t\tID:       \"1\",\n\t\tName:     \"Alice\",\n\t\tBalance:  100,\n\t\tInternal: \"sensitive_data_1\",\n\t})\n\ttree.Set(\"bob\", \u0026User{\n\t\tID:       \"2\",\n\t\tName:     \"Bob\",\n\t\tBalance:  200,\n\t\tInternal: \"sensitive_data_2\",\n\t})\n\n\t// Define a makeEntrySafeFn that:\n\t// 1. Creates a defensive copy of the User struct\n\t// 2. Omits sensitive internal data\n\tmakeEntrySafeFn := func(v any) any {\n\t\toriginalUser := v.(*User)\n\t\treturn \u0026User{\n\t\t\tID:       originalUser.ID,\n\t\t\tName:     originalUser.Name,\n\t\t\tBalance:  originalUser.Balance,\n\t\t\tInternal: \"\", // Omit sensitive data\n\t\t}\n\t}\n\n\t// Create a read-only view of the tree\n\troTree := Wrap(tree, makeEntrySafeFn)\n\n\t// Test retrieving and verifying a user\n\tt.Run(\"Get User\", func(t *testing.T) {\n\t\t// Get user from read-only tree\n\t\tvalue := roTree.Get(\"alice\")\n\t\tif value == nil {\n\t\t\tt.Fatal(\"User 'alice' not found\")\n\t\t}\n\n\t\tuser := value.(*User)\n\n\t\t// Verify user data is correct\n\t\tif user.Name != \"Alice\" || user.Balance != 100 {\n\t\t\tt.Errorf(\"Unexpected user data: got name=%s balance=%d\", user.Name, user.Balance)\n\t\t}\n\n\t\t// Verify sensitive data is not exposed\n\t\tif user.Internal != \"\" {\n\t\t\tt.Error(\"Sensitive data should not be exposed\")\n\t\t}\n\n\t\t// Verify it's a different instance than the original\n\t\toriginalValue := tree.Get(\"alice\")\n\t\toriginalUser := originalValue.(*User)\n\t\tif user == originalUser {\n\t\t\tt.Error(\"Read-only tree should return a copy, not the original pointer\")\n\t\t}\n\t})\n\n\t// Test iterating over users\n\tt.Run(\"Iterate Users\", func(t *testing.T) {\n\t\tcount := 0\n\t\troTree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tuser := value.(*User)\n\t\t\t// Verify each user has empty Internal field\n\t\t\tif user.Internal != \"\" {\n\t\t\t\tt.Error(\"Sensitive data exposed during iteration\")\n\t\t\t}\n\t\t\tcount++\n\t\t\treturn false\n\t\t})\n\n\t\tif count != 2 {\n\t\t\tt.Errorf(\"Expected 2 users, got %d\", count)\n\t\t}\n\t})\n\n\t// Verify that modifications to the returned user don't affect the original\n\tt.Run(\"Modification Safety\", func(t *testing.T) {\n\t\tvalue := roTree.Get(\"alice\")\n\t\tuser := value.(*User)\n\n\t\t// Try to modify the returned user\n\t\tuser.Balance = 999\n\t\tuser.Internal = \"hacked\"\n\n\t\t// Verify original is unchanged\n\t\toriginalValue := tree.Get(\"alice\")\n\t\toriginalUser := originalValue.(*User)\n\t\tif originalUser.Balance != 100 || originalUser.Internal != \"sensitive_data_1\" {\n\t\t\tt.Error(\"Original user data was modified\")\n\t\t}\n\t})\n}\n\nfunc TestReadOnlyTree(t *testing.T) {\n\t// Example of a makeEntrySafeFn that appends \"_readonly\" to demonstrate transformation\n\tmakeEntrySafeFn := func(value any) any {\n\t\treturn value.(string) + \"_readonly\"\n\t}\n\n\ttree := avl.NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\troTree := Wrap(tree, makeEntrySafeFn)\n\n\ttests := []struct {\n\t\tname     string\n\t\tkey      string\n\t\texpected any\n\t}{\n\t\t{\"ExistingKey1\", \"key1\", \"value1_readonly\"},\n\t\t{\"ExistingKey2\", \"key2\", \"value2_readonly\"},\n\t\t{\"NonExistingKey\", \"key4\", nil},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvalue := roTree.Get(tt.key)\n\t\t\tif value != tt.expected {\n\t\t\t\tt.Errorf(\"For key %s, expected %v, got %v\", tt.key, tt.expected, value)\n\t\t\t}\n\t\t})\n\t}\n}\n\n// Add example tests showing different makeEntrySafeFn implementations\nfunc TestMakeEntrySafeFnVariants(t *testing.T) {\n\ttree := avl.NewTree()\n\ttree.Set(\"slice\", []int{1, 2, 3})\n\ttree.Set(\"map\", map[string]int{\"a\": 1})\n\n\ttests := []struct {\n\t\tname            string\n\t\tmakeEntrySafeFn func(any) any\n\t\tkey             string\n\t\tvalidate        func(t *testing.T, value any)\n\t}{\n\t\t{\n\t\t\tname: \"Defensive Copy Slice\",\n\t\t\tmakeEntrySafeFn: func(v any) any {\n\t\t\t\toriginal := v.([]int)\n\t\t\t\treturn append([]int{}, original...)\n\t\t\t},\n\t\t\tkey: \"slice\",\n\t\t\tvalidate: func(t *testing.T, value any) {\n\t\t\t\tslice := value.([]int)\n\t\t\t\t// Modify the returned slice\n\t\t\t\tslice[0] = 999\n\t\t\t\t// Verify original is unchanged\n\t\t\t\toriginalValue := tree.Get(\"slice\")\n\t\t\t\toriginal := originalValue.([]int)\n\t\t\t\tif original[0] != 1 {\n\t\t\t\t\tt.Error(\"Original slice was modified\")\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\t// Add more test cases for different makeEntrySafeFn implementations\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\troTree := Wrap(tree, tt.makeEntrySafeFn)\n\t\t\tvalue := roTree.Get(tt.key)\n\t\t\tif value == nil {\n\t\t\t\tt.Fatal(\"Key not found\")\n\t\t\t}\n\t\t\ttt.validate(t, value)\n\t\t})\n\t}\n}\n\nfunc TestNilMakeEntrySafeFn(t *testing.T) {\n\t// Create a tree with some test data\n\ttree := avl.NewTree()\n\toriginalValue := []int{1, 2, 3}\n\ttree.Set(\"test\", originalValue)\n\n\t// Create a ReadOnlyTree with nil makeEntrySafeFn\n\troTree := Wrap(tree, nil)\n\n\t// Test that we get back the original value\n\tvalue := roTree.Get(\"test\")\n\tif value == nil {\n\t\tt.Fatal(\"Key not found\")\n\t}\n\n\t// Verify it's the exact same slice (not a copy)\n\tretrievedSlice := value.([]int)\n\tif \u0026retrievedSlice[0] != \u0026originalValue[0] {\n\t\tt.Error(\"Expected to get back the original slice reference\")\n\t}\n\n\t// Test through iteration as well\n\troTree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tretrievedSlice := value.([]int)\n\t\tif \u0026retrievedSlice[0] != \u0026originalValue[0] {\n\t\t\tt.Error(\"Expected to get back the original slice reference in iteration\")\n\t\t}\n\t\treturn false\n\t})\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"E9A1LHYqEgO+E2XM0vmRcbcFFSGNt0rjg8stSz839i3aJDrd77zhqQb/yBmnyJ3ROTDObzEvUVWnTMmYHZuDDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"cford32","path":"gno.land/p/nt/cford32/v0","files":[{"name":"LICENSE","body":"Copyright (c) 2009 The Go Authors. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n   * Redistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n   * Redistributions in binary form must reproduce the above\ncopyright notice, this list of conditions and the following disclaimer\nin the documentation and/or other materials provided with the\ndistribution.\n   * Neither the name of Google Inc. nor the names of its\ncontributors may be used to endorse or promote products derived from\nthis software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"},{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n\n# cford32\n\n```\npackage cford32 // import \"gno.land/p/nt/cford32/v0\"\n\nPackage cford32 implements a base32-like encoding/decoding package, with the\nencoding scheme specified by Douglas Crockford.\n\nFrom the website, the requirements of said encoding scheme are to:\n\n  - Be human readable and machine readable.\n  - Be compact. Humans have difficulty in manipulating long strings of arbitrary\n    symbols.\n  - Be error resistant. Entering the symbols must not require keyboarding\n    gymnastics.\n  - Be pronounceable. Humans should be able to accurately transmit the symbols\n    to other humans using a telephone.\n\nThis is slightly different from a simple difference in encoding table from\nthe Go's stdlib `encoding/base32`, as when decoding the characters i I l L are\nparsed as 1, and o O is parsed as 0.\n\nThis package additionally provides ways to encode uint64's efficiently, as well\nas efficient encoding to a lowercase variation of the encoding. The encodings\nnever use paddings.\n\n# Uint64 Encoding\n\nAside from lower/uppercase encoding, there is a compact encoding, allowing to\nencode all values in [0,2^34), and the full encoding, allowing all values in\n[0,2^64). The compact encoding uses 7 characters, and the full encoding uses 13\ncharacters. Both are parsed unambiguously by the Uint64 decoder.\n\nThe compact encodings have the first character between ['0','f'], while the\nfull encoding's first character ranges between ['g','z']. Practically, in your\nusage of the package, you should consider which one to use and stick with it,\nwhile considering that the compact encoding, once it reaches 2^34, automatically\nswitches to the full encoding. The properties of the generated strings are still\nmaintained: for instance, any two encoded uint64s x,y consistently generated\nwith the compact encoding, if the numeric value is x \u003c y, will also be x \u003c y in\nlexical ordering. However, values [0,2^34) have a \"double encoding\", which if\nmixed together lose the lexical ordering property.\n\nThe Uint64 encoding is most useful for generating string versions of Uint64 IDs.\nPractically, it allows you to retain sleek and compact IDs for your application\nfor the first 2^34 (\u003e17 billion) entities, while seamlessly rolling over to the\nfull encoding should you exceed that. You are encouraged to use it unless you\nhave a requirement or preferences for IDs consistently being always the same\nsize.\n\nTo use the cford32 encoding for IDs, you may want to consider using package\ngno.land/p/nt/seqid/v0.\n\n[specified by Douglas Crockford]: https://www.crockford.com/base32.html\n\nfunc AppendCompact(id uint64, b []byte) []byte\nfunc AppendDecode(dst, src []byte) ([]byte, error)\nfunc AppendEncode(dst, src []byte) []byte\nfunc AppendEncodeLower(dst, src []byte) []byte\nfunc Decode(dst, src []byte) (n int, err error)\nfunc DecodeString(s string) ([]byte, error)\nfunc DecodedLen(n int) int\nfunc Encode(dst, src []byte)\nfunc EncodeLower(dst, src []byte)\nfunc EncodeToString(src []byte) string\nfunc EncodeToStringLower(src []byte) string\nfunc EncodedLen(n int) int\nfunc NewDecoder(r io.Reader) io.Reader\nfunc NewEncoder(w io.Writer) io.WriteCloser\nfunc NewEncoderLower(w io.Writer) io.WriteCloser\nfunc PutCompact(id uint64) []byte\nfunc PutUint64(id uint64) [13]byte\nfunc PutUint64Lower(id uint64) [13]byte\nfunc Uint64(b []byte) (uint64, error)\ntype CorruptInputError int64\n```\n"},{"name":"cford32.gno","body":"// Modified from the Go Source code for encoding/base32.\n// Copyright 2009 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\n// Package cford32 implements a base32-like encoding/decoding package, with the\n// encoding scheme [specified by Douglas Crockford].\n//\n// From the website, the requirements of said encoding scheme are to:\n//\n//   - Be human readable and machine readable.\n//   - Be compact. Humans have difficulty in manipulating long strings of arbitrary symbols.\n//   - Be error resistant. Entering the symbols must not require keyboarding gymnastics.\n//   - Be pronounceable. Humans should be able to accurately transmit the symbols to other humans using a telephone.\n//\n// This is slightly different from a simple difference in encoding table from\n// the Go's stdlib `encoding/base32`, as when decoding the characters i I l L are\n// parsed as 1, and o O is parsed as 0.\n//\n// This package additionally provides ways to encode uint64's efficiently,\n// as well as efficient encoding to a lowercase variation of the encoding.\n// The encodings never use paddings.\n//\n// # Uint64 Encoding\n//\n// Aside from lower/uppercase encoding, there is a compact encoding, allowing\n// to encode all values in [0,2^34), and the full encoding, allowing all\n// values in [0,2^64). The compact encoding uses 7 characters, and the full\n// encoding uses 13 characters. Both are parsed unambiguously by the Uint64\n// decoder.\n//\n// The compact encodings have the first character between ['0','f'], while the\n// full encoding's first character ranges between ['g','z']. Practically, in\n// your usage of the package, you should consider which one to use and stick\n// with it, while considering that the compact encoding, once it reaches 2^34,\n// automatically switches to the full encoding. The properties of the generated\n// strings are still maintained: for instance, any two encoded uint64s x,y\n// consistently generated with the compact encoding, if the numeric value is\n// x \u003c y, will also be x \u003c y in lexical ordering. However, values [0,2^34) have a\n// \"double encoding\", which if mixed together lose the lexical ordering property.\n//\n// The Uint64 encoding is most useful for generating string versions of Uint64\n// IDs. Practically, it allows you to retain sleek and compact IDs for your\n// application for the first 2^34 (\u003e17 billion) entities, while seamlessly\n// rolling over to the full encoding should you exceed that. You are encouraged\n// to use it unless you have a requirement or preferences for IDs consistently\n// being always the same size.\n//\n// To use the cford32 encoding for IDs, you may want to consider using package\n// [gno.land/p/nt/seqid/v0].\n//\n// [specified by Douglas Crockford]: https://www.crockford.com/base32.html\npackage cford32\n\nimport (\n\t\"io\"\n\t\"strconv\"\n)\n\nconst (\n\tencTable      = \"0123456789ABCDEFGHJKMNPQRSTVWXYZ\"\n\tencTableLower = \"0123456789abcdefghjkmnpqrstvwxyz\"\n\n\t// each line is 16 bytes\n\tdecTable = \"\" +\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" + // 00-0f\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" + // 10-1f\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" + // 20-2f\n\t\t\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\xff\\xff\\xff\\xff\\xff\\xff\" + // 30-3f\n\t\t\"\\xff\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\\x10\\x11\\x01\\x12\\x13\\x01\\x14\\x15\\x00\" + // 40-4f\n\t\t\"\\x16\\x17\\x18\\x19\\x1a\\xff\\x1b\\x1c\\x1d\\x1e\\x1f\\xff\\xff\\xff\\xff\\xff\" + // 50-5f\n\t\t\"\\xff\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\\x10\\x11\\x01\\x12\\x13\\x01\\x14\\x15\\x00\" + // 60-6f\n\t\t\"\\x16\\x17\\x18\\x19\\x1a\\xff\\x1b\\x1c\\x1d\\x1e\\x1f\\xff\\xff\\xff\\xff\\xff\" + // 70-7f\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" + // 80-ff (not ASCII)\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" +\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" +\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" +\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" +\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" +\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" +\n\t\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\"\n)\n\n// CorruptInputError is returned by parsing functions when an invalid character\n// in the input is found. The integer value represents the byte index where\n// the error occurred.\n//\n// This is typically because the given character does not exist in the encoding.\ntype CorruptInputError int64\n\nfunc (e CorruptInputError) Error() string {\n\treturn \"illegal cford32 data at input byte \" + strconv.FormatInt(int64(e), 10)\n}\n\n// Uint64 parses a cford32-encoded byte slice into a uint64.\n//\n//   - The parser requires all provided character to be valid cford32 characters.\n//   - The parser disregards case.\n//   - If the first character is '0' \u003c= c \u003c= 'f', then the passed value is assumed\n//     encoded in the compact encoding, and must be 7 characters long.\n//   - If the first character is 'g' \u003c= c \u003c= 'z',  then the passed value is\n//     assumed encoded in the full encoding, and must be 13 characters long.\n//\n// If any of these requirements fail, a CorruptInputError will be returned.\nfunc Uint64(b []byte) (uint64, error) {\n\tif len(b) == 0 {\n\t\treturn 0, CorruptInputError(0)\n\t}\n\tb0 := decTable[b[0]]\n\tswitch {\n\tdefault:\n\t\treturn 0, CorruptInputError(0)\n\tcase len(b) == 7 \u0026\u0026 b0 \u003c 16:\n\t\tdecVals := [7]byte{\n\t\t\tdecTable[b[0]],\n\t\t\tdecTable[b[1]],\n\t\t\tdecTable[b[2]],\n\t\t\tdecTable[b[3]],\n\t\t\tdecTable[b[4]],\n\t\t\tdecTable[b[5]],\n\t\t\tdecTable[b[6]],\n\t\t}\n\t\tfor idx, v := range decVals {\n\t\t\tif v \u003e= 32 {\n\t\t\t\treturn 0, CorruptInputError(idx)\n\t\t\t}\n\t\t}\n\n\t\treturn 0 +\n\t\t\tuint64(decVals[0])\u003c\u003c30 |\n\t\t\tuint64(decVals[1])\u003c\u003c25 |\n\t\t\tuint64(decVals[2])\u003c\u003c20 |\n\t\t\tuint64(decVals[3])\u003c\u003c15 |\n\t\t\tuint64(decVals[4])\u003c\u003c10 |\n\t\t\tuint64(decVals[5])\u003c\u003c5 |\n\t\t\tuint64(decVals[6]), nil\n\tcase len(b) == 13 \u0026\u0026 b0 \u003e= 16 \u0026\u0026 b0 \u003c 32:\n\t\tdecVals := [13]byte{\n\t\t\tdecTable[b[0]] \u0026 0x0F, // disregard high bit\n\t\t\tdecTable[b[1]],\n\t\t\tdecTable[b[2]],\n\t\t\tdecTable[b[3]],\n\t\t\tdecTable[b[4]],\n\t\t\tdecTable[b[5]],\n\t\t\tdecTable[b[6]],\n\t\t\tdecTable[b[7]],\n\t\t\tdecTable[b[8]],\n\t\t\tdecTable[b[9]],\n\t\t\tdecTable[b[10]],\n\t\t\tdecTable[b[11]],\n\t\t\tdecTable[b[12]],\n\t\t}\n\t\tfor idx, v := range decVals {\n\t\t\tif v \u003e= 32 {\n\t\t\t\treturn 0, CorruptInputError(idx)\n\t\t\t}\n\t\t}\n\n\t\treturn 0 +\n\t\t\tuint64(decVals[0])\u003c\u003c60 |\n\t\t\tuint64(decVals[1])\u003c\u003c55 |\n\t\t\tuint64(decVals[2])\u003c\u003c50 |\n\t\t\tuint64(decVals[3])\u003c\u003c45 |\n\t\t\tuint64(decVals[4])\u003c\u003c40 |\n\t\t\tuint64(decVals[5])\u003c\u003c35 |\n\t\t\tuint64(decVals[6])\u003c\u003c30 |\n\t\t\tuint64(decVals[7])\u003c\u003c25 |\n\t\t\tuint64(decVals[8])\u003c\u003c20 |\n\t\t\tuint64(decVals[9])\u003c\u003c15 |\n\t\t\tuint64(decVals[10])\u003c\u003c10 |\n\t\t\tuint64(decVals[11])\u003c\u003c5 |\n\t\t\tuint64(decVals[12]), nil\n\t}\n}\n\nconst mask = 31\n\n// PutUint64 returns a cford32-encoded byte slice.\nfunc PutUint64(id uint64) [13]byte {\n\treturn [13]byte{\n\t\tencTable[id\u003e\u003e60\u0026mask|0x10], // specify full encoding\n\t\tencTable[id\u003e\u003e55\u0026mask],\n\t\tencTable[id\u003e\u003e50\u0026mask],\n\t\tencTable[id\u003e\u003e45\u0026mask],\n\t\tencTable[id\u003e\u003e40\u0026mask],\n\t\tencTable[id\u003e\u003e35\u0026mask],\n\t\tencTable[id\u003e\u003e30\u0026mask],\n\t\tencTable[id\u003e\u003e25\u0026mask],\n\t\tencTable[id\u003e\u003e20\u0026mask],\n\t\tencTable[id\u003e\u003e15\u0026mask],\n\t\tencTable[id\u003e\u003e10\u0026mask],\n\t\tencTable[id\u003e\u003e5\u0026mask],\n\t\tencTable[id\u0026mask],\n\t}\n}\n\n// PutUint64Lower returns a cford32-encoded byte array, swapping uppercase\n// letters with lowercase.\n//\n// For more information on how the value is encoded, see [Uint64].\nfunc PutUint64Lower(id uint64) [13]byte {\n\treturn [13]byte{\n\t\tencTableLower[id\u003e\u003e60\u0026mask|0x10],\n\t\tencTableLower[id\u003e\u003e55\u0026mask],\n\t\tencTableLower[id\u003e\u003e50\u0026mask],\n\t\tencTableLower[id\u003e\u003e45\u0026mask],\n\t\tencTableLower[id\u003e\u003e40\u0026mask],\n\t\tencTableLower[id\u003e\u003e35\u0026mask],\n\t\tencTableLower[id\u003e\u003e30\u0026mask],\n\t\tencTableLower[id\u003e\u003e25\u0026mask],\n\t\tencTableLower[id\u003e\u003e20\u0026mask],\n\t\tencTableLower[id\u003e\u003e15\u0026mask],\n\t\tencTableLower[id\u003e\u003e10\u0026mask],\n\t\tencTableLower[id\u003e\u003e5\u0026mask],\n\t\tencTableLower[id\u0026mask],\n\t}\n}\n\n// PutCompact returns a cford32-encoded byte slice, using the compact\n// representation of cford32 described in the package documentation where\n// possible (all values of id \u003c 1\u003c\u003c34). The lowercase encoding is used.\n//\n// The resulting byte slice will be 7 bytes long for all compact values,\n// and 13 bytes long for\nfunc PutCompact(id uint64) []byte {\n\treturn AppendCompact(id, nil)\n}\n\n// AppendCompact works like [PutCompact] but appends to the given byte slice\n// instead of allocating one anew.\nfunc AppendCompact(id uint64, b []byte) []byte {\n\tconst maxCompact = 1 \u003c\u003c 34\n\tif id \u003c maxCompact {\n\t\treturn append(b,\n\t\t\tencTableLower[id\u003e\u003e30\u0026mask],\n\t\t\tencTableLower[id\u003e\u003e25\u0026mask],\n\t\t\tencTableLower[id\u003e\u003e20\u0026mask],\n\t\t\tencTableLower[id\u003e\u003e15\u0026mask],\n\t\t\tencTableLower[id\u003e\u003e10\u0026mask],\n\t\t\tencTableLower[id\u003e\u003e5\u0026mask],\n\t\t\tencTableLower[id\u0026mask],\n\t\t)\n\t}\n\treturn append(b,\n\t\tencTableLower[id\u003e\u003e60\u0026mask|0x10],\n\t\tencTableLower[id\u003e\u003e55\u0026mask],\n\t\tencTableLower[id\u003e\u003e50\u0026mask],\n\t\tencTableLower[id\u003e\u003e45\u0026mask],\n\t\tencTableLower[id\u003e\u003e40\u0026mask],\n\t\tencTableLower[id\u003e\u003e35\u0026mask],\n\t\tencTableLower[id\u003e\u003e30\u0026mask],\n\t\tencTableLower[id\u003e\u003e25\u0026mask],\n\t\tencTableLower[id\u003e\u003e20\u0026mask],\n\t\tencTableLower[id\u003e\u003e15\u0026mask],\n\t\tencTableLower[id\u003e\u003e10\u0026mask],\n\t\tencTableLower[id\u003e\u003e5\u0026mask],\n\t\tencTableLower[id\u0026mask],\n\t)\n}\n\nfunc DecodedLen(n int) int {\n\treturn n/8*5 + n%8*5/8\n}\n\nfunc EncodedLen(n int) int {\n\treturn n/5*8 + (n%5*8+4)/5\n}\n\n// Encode encodes src using the encoding enc,\n// writing [EncodedLen](len(src)) bytes to dst.\n//\n// The encoding does not contain any padding, unlike Go's base32.\nfunc Encode(dst, src []byte) {\n\t// Copied from encoding/base32/base32.go (go1.22)\n\tif len(src) == 0 {\n\t\treturn\n\t}\n\n\tdi, si := 0, 0\n\tn := (len(src) / 5) * 5\n\tfor si \u003c n {\n\t\t// Combining two 32 bit loads allows the same code to be used\n\t\t// for 32 and 64 bit platforms.\n\t\thi := uint32(src[si+0])\u003c\u003c24 | uint32(src[si+1])\u003c\u003c16 | uint32(src[si+2])\u003c\u003c8 | uint32(src[si+3])\n\t\tlo := hi\u003c\u003c8 | uint32(src[si+4])\n\n\t\tdst[di+0] = encTable[(hi\u003e\u003e27)\u00260x1F]\n\t\tdst[di+1] = encTable[(hi\u003e\u003e22)\u00260x1F]\n\t\tdst[di+2] = encTable[(hi\u003e\u003e17)\u00260x1F]\n\t\tdst[di+3] = encTable[(hi\u003e\u003e12)\u00260x1F]\n\t\tdst[di+4] = encTable[(hi\u003e\u003e7)\u00260x1F]\n\t\tdst[di+5] = encTable[(hi\u003e\u003e2)\u00260x1F]\n\t\tdst[di+6] = encTable[(lo\u003e\u003e5)\u00260x1F]\n\t\tdst[di+7] = encTable[(lo)\u00260x1F]\n\n\t\tsi += 5\n\t\tdi += 8\n\t}\n\n\t// Add the remaining small block\n\tremain := len(src) - si\n\tif remain == 0 {\n\t\treturn\n\t}\n\n\t// Encode the remaining bytes in reverse order.\n\tval := uint32(0)\n\tswitch remain {\n\tcase 4:\n\t\tval |= uint32(src[si+3])\n\t\tdst[di+6] = encTable[val\u003c\u003c3\u00260x1F]\n\t\tdst[di+5] = encTable[val\u003e\u003e2\u00260x1F]\n\t\tfallthrough\n\tcase 3:\n\t\tval |= uint32(src[si+2]) \u003c\u003c 8\n\t\tdst[di+4] = encTable[val\u003e\u003e7\u00260x1F]\n\t\tfallthrough\n\tcase 2:\n\t\tval |= uint32(src[si+1]) \u003c\u003c 16\n\t\tdst[di+3] = encTable[val\u003e\u003e12\u00260x1F]\n\t\tdst[di+2] = encTable[val\u003e\u003e17\u00260x1F]\n\t\tfallthrough\n\tcase 1:\n\t\tval |= uint32(src[si+0]) \u003c\u003c 24\n\t\tdst[di+1] = encTable[val\u003e\u003e22\u00260x1F]\n\t\tdst[di+0] = encTable[val\u003e\u003e27\u00260x1F]\n\t}\n}\n\n// EncodeLower is like [Encode], but uses the lowercase\nfunc EncodeLower(dst, src []byte) {\n\t// Copied from encoding/base32/base32.go (go1.22)\n\tif len(src) == 0 {\n\t\treturn\n\t}\n\n\tdi, si := 0, 0\n\tn := (len(src) / 5) * 5\n\tfor si \u003c n {\n\t\t// Combining two 32 bit loads allows the same code to be used\n\t\t// for 32 and 64 bit platforms.\n\t\thi := uint32(src[si+0])\u003c\u003c24 | uint32(src[si+1])\u003c\u003c16 | uint32(src[si+2])\u003c\u003c8 | uint32(src[si+3])\n\t\tlo := hi\u003c\u003c8 | uint32(src[si+4])\n\n\t\tdst[di+0] = encTableLower[(hi\u003e\u003e27)\u00260x1F]\n\t\tdst[di+1] = encTableLower[(hi\u003e\u003e22)\u00260x1F]\n\t\tdst[di+2] = encTableLower[(hi\u003e\u003e17)\u00260x1F]\n\t\tdst[di+3] = encTableLower[(hi\u003e\u003e12)\u00260x1F]\n\t\tdst[di+4] = encTableLower[(hi\u003e\u003e7)\u00260x1F]\n\t\tdst[di+5] = encTableLower[(hi\u003e\u003e2)\u00260x1F]\n\t\tdst[di+6] = encTableLower[(lo\u003e\u003e5)\u00260x1F]\n\t\tdst[di+7] = encTableLower[(lo)\u00260x1F]\n\n\t\tsi += 5\n\t\tdi += 8\n\t}\n\n\t// Add the remaining small block\n\tremain := len(src) - si\n\tif remain == 0 {\n\t\treturn\n\t}\n\n\t// Encode the remaining bytes in reverse order.\n\tval := uint32(0)\n\tswitch remain {\n\tcase 4:\n\t\tval |= uint32(src[si+3])\n\t\tdst[di+6] = encTableLower[val\u003c\u003c3\u00260x1F]\n\t\tdst[di+5] = encTableLower[val\u003e\u003e2\u00260x1F]\n\t\tfallthrough\n\tcase 3:\n\t\tval |= uint32(src[si+2]) \u003c\u003c 8\n\t\tdst[di+4] = encTableLower[val\u003e\u003e7\u00260x1F]\n\t\tfallthrough\n\tcase 2:\n\t\tval |= uint32(src[si+1]) \u003c\u003c 16\n\t\tdst[di+3] = encTableLower[val\u003e\u003e12\u00260x1F]\n\t\tdst[di+2] = encTableLower[val\u003e\u003e17\u00260x1F]\n\t\tfallthrough\n\tcase 1:\n\t\tval |= uint32(src[si+0]) \u003c\u003c 24\n\t\tdst[di+1] = encTableLower[val\u003e\u003e22\u00260x1F]\n\t\tdst[di+0] = encTableLower[val\u003e\u003e27\u00260x1F]\n\t}\n}\n\n// AppendEncode appends the cford32 encoded src to dst\n// and returns the extended buffer.\nfunc AppendEncode(dst, src []byte) []byte {\n\tn := EncodedLen(len(src))\n\tdst = grow(dst, n)\n\tEncode(dst[len(dst):][:n], src)\n\treturn dst[:len(dst)+n]\n}\n\n// AppendEncodeLower appends the lowercase cford32 encoded src to dst\n// and returns the extended buffer.\nfunc AppendEncodeLower(dst, src []byte) []byte {\n\tn := EncodedLen(len(src))\n\tdst = grow(dst, n)\n\tEncodeLower(dst[len(dst):][:n], src)\n\treturn dst[:len(dst)+n]\n}\n\nfunc grow(s []byte, n int) []byte {\n\t// slices.Grow\n\tif n -= cap(s) - len(s); n \u003e 0 {\n\t\tnews := make([]byte, cap(s)+n)\n\t\tcopy(news[:cap(s)], s[:cap(s)])\n\t\treturn news[:len(s)]\n\t}\n\treturn s\n}\n\n// EncodeToString returns the cford32 encoding of src.\nfunc EncodeToString(src []byte) string {\n\tbuf := make([]byte, EncodedLen(len(src)))\n\tEncode(buf, src)\n\treturn string(buf)\n}\n\n// EncodeToStringLower returns the cford32 lowercase encoding of src.\nfunc EncodeToStringLower(src []byte) string {\n\tbuf := make([]byte, EncodedLen(len(src)))\n\tEncodeLower(buf, src)\n\treturn string(buf)\n}\n\nfunc decode(dst, src []byte) (n int, err error) {\n\tdsti := 0\n\tolen := len(src)\n\n\tfor len(src) \u003e 0 {\n\t\t// Decode quantum using the base32 alphabet\n\t\tvar dbuf [8]byte\n\t\tdlen := 8\n\n\t\tfor j := 0; j \u003c 8; {\n\t\t\tif len(src) == 0 {\n\t\t\t\t// We have reached the end and are not expecting any padding\n\t\t\t\tdlen = j\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tin := src[0]\n\t\t\tsrc = src[1:]\n\t\t\tdbuf[j] = decTable[in]\n\t\t\tif dbuf[j] == 0xFF {\n\t\t\t\treturn n, CorruptInputError(olen - len(src) - 1)\n\t\t\t}\n\t\t\tj++\n\t\t}\n\n\t\t// Pack 8x 5-bit source blocks into 5 byte destination\n\t\t// quantum\n\t\tswitch dlen {\n\t\tcase 8:\n\t\t\tdst[dsti+4] = dbuf[6]\u003c\u003c5 | dbuf[7]\n\t\t\tn++\n\t\t\tfallthrough\n\t\tcase 7:\n\t\t\tdst[dsti+3] = dbuf[4]\u003c\u003c7 | dbuf[5]\u003c\u003c2 | dbuf[6]\u003e\u003e3\n\t\t\tn++\n\t\t\tfallthrough\n\t\tcase 5:\n\t\t\tdst[dsti+2] = dbuf[3]\u003c\u003c4 | dbuf[4]\u003e\u003e1\n\t\t\tn++\n\t\t\tfallthrough\n\t\tcase 4:\n\t\t\tdst[dsti+1] = dbuf[1]\u003c\u003c6 | dbuf[2]\u003c\u003c1 | dbuf[3]\u003e\u003e4\n\t\t\tn++\n\t\t\tfallthrough\n\t\tcase 2:\n\t\t\tdst[dsti+0] = dbuf[0]\u003c\u003c3 | dbuf[1]\u003e\u003e2\n\t\t\tn++\n\t\t}\n\t\tdsti += 5\n\t}\n\treturn n, nil\n}\n\ntype encoder struct {\n\terr  error\n\tw    io.Writer\n\tenc  func(dst, src []byte)\n\tbuf  [5]byte    // buffered data waiting to be encoded\n\tnbuf int        // number of bytes in buf\n\tout  [1024]byte // output buffer\n}\n\nfunc NewEncoder(w io.Writer) io.WriteCloser {\n\treturn \u0026encoder{w: w, enc: Encode}\n}\n\nfunc NewEncoderLower(w io.Writer) io.WriteCloser {\n\treturn \u0026encoder{w: w, enc: EncodeLower}\n}\n\nfunc (e *encoder) Write(p []byte) (n int, err error) {\n\tif e.err != nil {\n\t\treturn 0, e.err\n\t}\n\n\t// Leading fringe.\n\tif e.nbuf \u003e 0 {\n\t\tvar i int\n\t\tfor i = 0; i \u003c len(p) \u0026\u0026 e.nbuf \u003c 5; i++ {\n\t\t\te.buf[e.nbuf] = p[i]\n\t\t\te.nbuf++\n\t\t}\n\t\tn += i\n\t\tp = p[i:]\n\t\tif e.nbuf \u003c 5 {\n\t\t\treturn\n\t\t}\n\t\te.enc(e.out[0:], e.buf[0:])\n\t\tif _, e.err = e.w.Write(e.out[0:8]); e.err != nil {\n\t\t\treturn n, e.err\n\t\t}\n\t\te.nbuf = 0\n\t}\n\n\t// Large interior chunks.\n\tfor len(p) \u003e= 5 {\n\t\tnn := len(e.out) / 8 * 5\n\t\tif nn \u003e len(p) {\n\t\t\tnn = len(p)\n\t\t\tnn -= nn % 5\n\t\t}\n\t\te.enc(e.out[0:], p[0:nn])\n\t\tif _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil {\n\t\t\treturn n, e.err\n\t\t}\n\t\tn += nn\n\t\tp = p[nn:]\n\t}\n\n\t// Trailing fringe.\n\tcopy(e.buf[:], p)\n\te.nbuf = len(p)\n\tn += len(p)\n\treturn\n}\n\n// Close flushes any pending output from the encoder.\n// It is an error to call Write after calling Close.\nfunc (e *encoder) Close() error {\n\t// If there's anything left in the buffer, flush it out\n\tif e.err == nil \u0026\u0026 e.nbuf \u003e 0 {\n\t\te.enc(e.out[0:], e.buf[0:e.nbuf])\n\t\tencodedLen := EncodedLen(e.nbuf)\n\t\te.nbuf = 0\n\t\t_, e.err = e.w.Write(e.out[0:encodedLen])\n\t}\n\treturn e.err\n}\n\n// Decode decodes src using cford32. It writes at most\n// [DecodedLen](len(src)) bytes to dst and returns the number of bytes\n// written. If src contains invalid cford32 data, it will return the\n// number of bytes successfully written and [CorruptInputError].\n// Newline characters (\\r and \\n) are ignored.\nfunc Decode(dst, src []byte) (n int, err error) {\n\tbuf := make([]byte, len(src))\n\tl := stripNewlines(buf, src)\n\treturn decode(dst, buf[:l])\n}\n\n// AppendDecode appends the cford32 decoded src to dst\n// and returns the extended buffer.\n// If the input is malformed, it returns the partially decoded src and an error.\nfunc AppendDecode(dst, src []byte) ([]byte, error) {\n\tn := DecodedLen(len(src))\n\n\tdst = grow(dst, n)\n\tdstsl := dst[len(dst) : len(dst)+n]\n\tn, err := Decode(dstsl, src)\n\treturn dst[:len(dst)+n], err\n}\n\n// DecodeString returns the bytes represented by the cford32 string s.\nfunc DecodeString(s string) ([]byte, error) {\n\tbuf := []byte(s)\n\tl := stripNewlines(buf, buf)\n\tn, err := decode(buf, buf[:l])\n\treturn buf[:n], err\n}\n\n// stripNewlines removes newline characters and returns the number\n// of non-newline characters copied to dst.\nfunc stripNewlines(dst, src []byte) int {\n\toffset := 0\n\tfor _, b := range src {\n\t\tif b == '\\r' || b == '\\n' {\n\t\t\tcontinue\n\t\t}\n\t\tdst[offset] = b\n\t\toffset++\n\t}\n\treturn offset\n}\n\ntype decoder struct {\n\terr    error\n\tr      io.Reader\n\tbuf    [1024]byte // leftover input\n\tnbuf   int\n\tout    []byte // leftover decoded output\n\toutbuf [1024 / 8 * 5]byte\n}\n\n// NewDecoder constructs a new base32 stream decoder.\nfunc NewDecoder(r io.Reader) io.Reader {\n\treturn \u0026decoder{r: \u0026newlineFilteringReader{r}}\n}\n\nfunc readEncodedData(r io.Reader, buf []byte) (n int, err error) {\n\tfor n \u003c 1 \u0026\u0026 err == nil {\n\t\tvar nn int\n\t\tnn, err = r.Read(buf[n:])\n\t\tn += nn\n\t}\n\treturn\n}\n\nfunc (d *decoder) Read(p []byte) (n int, err error) {\n\t// Use leftover decoded output from last read.\n\tif len(d.out) \u003e 0 {\n\t\tn = copy(p, d.out)\n\t\td.out = d.out[n:]\n\t\tif len(d.out) == 0 {\n\t\t\treturn n, d.err\n\t\t}\n\t\treturn n, nil\n\t}\n\n\tif d.err != nil {\n\t\treturn 0, d.err\n\t}\n\n\t// Read nn bytes from input, bounded [8,len(d.buf)]\n\tnn := (len(p)/5 + 1) * 8\n\tif nn \u003e len(d.buf) {\n\t\tnn = len(d.buf)\n\t}\n\n\tnn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn])\n\td.nbuf += nn\n\tif d.nbuf \u003c 1 {\n\t\treturn 0, d.err\n\t}\n\n\t// Decode chunk into p, or d.out and then p if p is too small.\n\tnr := d.nbuf\n\tif d.err != io.EOF \u0026\u0026 nr%8 != 0 {\n\t\tnr -= nr % 8\n\t}\n\tnw := DecodedLen(d.nbuf)\n\n\tif nw \u003e len(p) {\n\t\tnw, err = decode(d.outbuf[0:], d.buf[0:nr])\n\t\td.out = d.outbuf[0:nw]\n\t\tn = copy(p, d.out)\n\t\td.out = d.out[n:]\n\t} else {\n\t\tn, err = decode(p, d.buf[0:nr])\n\t}\n\td.nbuf -= nr\n\tfor i := 0; i \u003c d.nbuf; i++ {\n\t\td.buf[i] = d.buf[i+nr]\n\t}\n\n\tif err != nil \u0026\u0026 (d.err == nil || d.err == io.EOF) {\n\t\td.err = err\n\t}\n\n\tif len(d.out) \u003e 0 {\n\t\t// We cannot return all the decoded bytes to the caller in this\n\t\t// invocation of Read, so we return a nil error to ensure that Read\n\t\t// will be called again.  The error stored in d.err, if any, will be\n\t\t// returned with the last set of decoded bytes.\n\t\treturn n, nil\n\t}\n\n\treturn n, d.err\n}\n\ntype newlineFilteringReader struct {\n\twrapped io.Reader\n}\n\nfunc (r *newlineFilteringReader) Read(p []byte) (int, error) {\n\tn, err := r.wrapped.Read(p)\n\tfor n \u003e 0 {\n\t\ts := p[0:n]\n\t\toffset := stripNewlines(s, s)\n\t\tif err != nil || offset \u003e 0 {\n\t\t\treturn offset, err\n\t\t}\n\t\t// Previous buffer entirely whitespace, read again\n\t\tn, err = r.wrapped.Read(p)\n\t}\n\treturn n, err\n}\n"},{"name":"cford32_test.gno","body":"package cford32\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"math\"\n\t\"math/rand\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestCompactRoundtrip(t *testing.T) {\n\tbuf := make([]byte, 13)\n\tprev := make([]byte, 13)\n\tfor i := uint64(0); i \u003c (1 \u003c\u003c 10); i++ {\n\t\tres := AppendCompact(i, buf[:0])\n\t\tback, err := Uint64(res)\n\t\ttestEqual(t, \"Uint64(%q) = (%d, %v), want %v\", string(res), back, err, nil)\n\t\ttestEqual(t, \"Uint64(%q) = %d, want %v\", string(res), back, i)\n\n\t\ttestEqual(t, \"bytes.Compare(prev, res) = %d, want %d\", bytes.Compare(prev, res), -1)\n\t\tprev, buf = res, prev\n\t}\n\tfor i := uint64(1\u003c\u003c34 - 1024); i \u003c (1\u003c\u003c34 + 1024); i++ {\n\t\tres := AppendCompact(i, buf[:0])\n\t\tback, err := Uint64(res)\n\t\t// println(string(res))\n\t\ttestEqual(t, \"Uint64(%q) = (%d, %v), want %v\", string(res), back, err, nil)\n\t\ttestEqual(t, \"Uint64(%q) = %d, want %v\", string(res), back, i)\n\n\t\ttestEqual(t, \"bytes.Compare(prev, res) = %d, want %d\", bytes.Compare(prev, res), -1)\n\t\tprev, buf = res, prev\n\t}\n\tfor i := uint64(1\u003c\u003c64 - 5000); i != 0; i++ {\n\t\tres := AppendCompact(i, buf[:0])\n\t\tback, err := Uint64(res)\n\t\ttestEqual(t, \"Uint64(%q) = (%d, %v), want %v\", string(res), back, err, nil)\n\t\ttestEqual(t, \"Uint64(%q) = %d, want %v\", string(res), back, i)\n\n\t\ttestEqual(t, \"bytes.Compare(prev, res) = %d, want %d\", bytes.Compare(prev, res), -1)\n\t\tprev, buf = res, prev\n\t}\n}\n\nfunc BenchmarkCompact(b *testing.B) {\n\tbuf := make([]byte, 13)\n\tfor i := 0; i \u003c b.N; i++ {\n\t\t_ = AppendCompact(uint64(i), buf[:0])\n\t}\n}\n\nfunc TestUint64(t *testing.T) {\n\ttt := []struct {\n\t\tval    string\n\t\toutput uint64\n\t\terr    string\n\t}{\n\t\t{\"0000001\", 1, \"\"},\n\t\t{\"OoOoOoL\", 1, \"\"},\n\t\t{\"OoUoOoL\", 0, CorruptInputError(2).Error()},\n\t\t{\"!123123\", 0, CorruptInputError(0).Error()},\n\t\t{\"Loooooo\", 1073741824, \"\"},\n\t\t{\"goooooo\", 0, CorruptInputError(0).Error()},\n\t\t{\"goooooooooooo\", 0, \"\"},\n\t\t{\"goooooooooolo\", 32, \"\"},\n\t\t{\"fzzzzzz\", (1 \u003c\u003c 34) - 1, \"\"},\n\t\t{\"g00000fzzzzzz\", (1 \u003c\u003c 34) - 1, \"\"},\n\t\t{\"g000000\", 0, CorruptInputError(0).Error()},\n\t\t{\"g00000g000000\", (1 \u003c\u003c 34), \"\"},\n\t}\n\n\tfor _, tc := range tt {\n\t\tt.Run(tc.val, func(t *testing.T) {\n\t\t\tres, err := Uint64([]byte(tc.val))\n\t\t\tif tc.err != \"\" {\n\t\t\t\t_ = uassert.Error(t, err) \u0026\u0026\n\t\t\t\t\tuassert.Equal(t, tc.err, err.Error())\n\t\t\t} else {\n\t\t\t\t_ = uassert.NoError(t, err) \u0026\u0026\n\t\t\t\t\tuassert.Equal(t, tc.output, res)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRandomCompactRoundtrip(t *testing.T) {\n\tfor i := 0; i \u003c 1\u003c\u003c12; i++ {\n\t\tvalue := rand.Uint64()\n\t\tencoded := PutCompact(value)\n\t\tdecoded, err := Uint64(encoded)\n\t\tuassert.NoError(t, err)\n\t\tuassert.Equal(t, value, decoded)\n\t}\n}\n\ntype testpair struct {\n\tdecoded, encoded string\n}\n\nvar pairs = []testpair{\n\t{\"\", \"\"},\n\t{\"f\", \"CR\"},\n\t{\"fo\", \"CSQG\"},\n\t{\"foo\", \"CSQPY\"},\n\t{\"foob\", \"CSQPYRG\"},\n\t{\"fooba\", \"CSQPYRK1\"},\n\t{\"foobar\", \"CSQPYRK1E8\"},\n\n\t{\"sure.\", \"EDTQ4S9E\"},\n\t{\"sure\", \"EDTQ4S8\"},\n\t{\"sur\", \"EDTQ4\"},\n\t{\"su\", \"EDTG\"},\n\t{\"leasure.\", \"DHJP2WVNE9JJW\"},\n\t{\"easure.\", \"CNGQ6XBJCMQ0\"},\n\t{\"asure.\", \"C5SQAWK55R\"},\n}\n\nvar bigtest = testpair{\n\t\"Twas brillig, and the slithy toves\",\n\t\"AHVP2WS0C9S6JV3CD5KJR831DSJ20X38CMG76V39EHM7J83MDXV6AWR\",\n}\n\nfunc testEqual(t *testing.T, msg string, args ...any) bool {\n\tt.Helper()\n\tif args[len(args)-2] != args[len(args)-1] {\n\t\tt.Errorf(msg, args...)\n\t\treturn false\n\t}\n\treturn true\n}\n\nfunc TestEncode(t *testing.T) {\n\tfor _, p := range pairs {\n\t\tgot := EncodeToString([]byte(p.decoded))\n\t\ttestEqual(t, \"Encode(%q) = %q, want %q\", p.decoded, got, p.encoded)\n\t\tdst := AppendEncode([]byte(\"lead\"), []byte(p.decoded))\n\t\ttestEqual(t, `AppendEncode(\"lead\", %q) = %q, want %q`, p.decoded, string(dst), \"lead\"+p.encoded)\n\t}\n}\n\nfunc TestEncoder(t *testing.T) {\n\tfor _, p := range pairs {\n\t\tbb := \u0026strings.Builder{}\n\t\tencoder := NewEncoder(bb)\n\t\tencoder.Write([]byte(p.decoded))\n\t\tencoder.Close()\n\t\ttestEqual(t, \"Encode(%q) = %q, want %q\", p.decoded, bb.String(), p.encoded)\n\t}\n}\n\nfunc TestEncoderBuffering(t *testing.T) {\n\tinput := []byte(bigtest.decoded)\n\tfor bs := 1; bs \u003c= 12; bs++ {\n\t\tbb := \u0026strings.Builder{}\n\t\tencoder := NewEncoder(bb)\n\t\tfor pos := 0; pos \u003c len(input); pos += bs {\n\t\t\tend := pos + bs\n\t\t\tif end \u003e len(input) {\n\t\t\t\tend = len(input)\n\t\t\t}\n\t\t\tn, err := encoder.Write(input[pos:end])\n\t\t\ttestEqual(t, \"Write(%q) gave error %v, want %v\", input[pos:end], err, error(nil))\n\t\t\ttestEqual(t, \"Write(%q) gave length %v, want %v\", input[pos:end], n, end-pos)\n\t\t}\n\t\terr := encoder.Close()\n\t\ttestEqual(t, \"Close gave error %v, want %v\", err, error(nil))\n\t\ttestEqual(t, \"Encoding/%d of %q = %q, want %q\", bs, bigtest.decoded, bb.String(), bigtest.encoded)\n\t}\n}\n\nfunc TestDecode(t *testing.T) {\n\tfor _, p := range pairs {\n\t\tdbuf := make([]byte, DecodedLen(len(p.encoded)))\n\t\tcount, err := decode(dbuf, []byte(p.encoded))\n\t\ttestEqual(t, \"Decode(%q) = error %v, want %v\", p.encoded, err, error(nil))\n\t\ttestEqual(t, \"Decode(%q) = length %v, want %v\", p.encoded, count, len(p.decoded))\n\t\ttestEqual(t, \"Decode(%q) = %q, want %q\", p.encoded, string(dbuf[0:count]), p.decoded)\n\n\t\tdbuf, err = DecodeString(p.encoded)\n\t\ttestEqual(t, \"DecodeString(%q) = error %v, want %v\", p.encoded, err, error(nil))\n\t\ttestEqual(t, \"DecodeString(%q) = %q, want %q\", p.encoded, string(dbuf), p.decoded)\n\n\t\t// XXX: https://github.com/gnolang/gno/issues/1570\n\t\tdst, err := AppendDecode(append([]byte(nil), []byte(\"lead\")...), []byte(p.encoded))\n\t\ttestEqual(t, \"AppendDecode(%q) = error %v, want %v\", p.encoded, err, error(nil))\n\t\ttestEqual(t, `AppendDecode(\"lead\", %q) = %q, want %q`, p.encoded, string(dst), \"lead\"+p.decoded)\n\n\t\tdst2, err := AppendDecode(dst[:0:len(p.decoded)], []byte(p.encoded))\n\t\ttestEqual(t, \"AppendDecode(%q) = error %v, want %v\", p.encoded, err, error(nil))\n\t\ttestEqual(t, `AppendDecode(\"\", %q) = %q, want %q`, p.encoded, string(dst2), p.decoded)\n\t\t// XXX: https://github.com/gnolang/gno/issues/1569\n\t\t// old used \u0026dst2[0] != \u0026dst[0] as a check.\n\t\tif len(dst) \u003e 0 \u0026\u0026 len(dst2) \u003e 0 \u0026\u0026 cap(dst2) != len(p.decoded) {\n\t\t\tt.Errorf(\"unexpected capacity growth: got %d, want %d\", cap(dst2), len(p.decoded))\n\t\t}\n\t}\n}\n\n// A minimal variation on strings.Reader.\n// Here, we return a io.EOF immediately on Read if the read has reached the end\n// of the reader. It's used to simplify TestDecoder.\ntype stringReader struct {\n\ts string\n\ti int64\n}\n\nfunc (r *stringReader) Read(b []byte) (n int, err error) {\n\tif r.i \u003e= int64(len(r.s)) {\n\t\treturn 0, io.EOF\n\t}\n\tn = copy(b, r.s[r.i:])\n\tr.i += int64(n)\n\tif r.i \u003e= int64(len(r.s)) {\n\t\treturn n, io.EOF\n\t}\n\treturn\n}\n\nfunc TestDecoder(t *testing.T) {\n\tfor _, p := range pairs {\n\t\tdecoder := NewDecoder(\u0026stringReader{p.encoded, 0})\n\t\tdbuf := make([]byte, DecodedLen(len(p.encoded)))\n\t\tcount, err := decoder.Read(dbuf)\n\t\tif err != nil \u0026\u0026 err != io.EOF {\n\t\t\tt.Fatal(\"Read failed\", err)\n\t\t}\n\t\ttestEqual(t, \"Read from %q = length %v, want %v\", p.encoded, count, len(p.decoded))\n\t\ttestEqual(t, \"Decoding of %q = %q, want %q\", p.encoded, string(dbuf[0:count]), p.decoded)\n\t\tif err != io.EOF {\n\t\t\t_, err = decoder.Read(dbuf)\n\t\t}\n\t\ttestEqual(t, \"Read from %q = %v, want %v\", p.encoded, err, io.EOF)\n\t}\n}\n\ntype badReader struct {\n\tdata   []byte\n\terrs   []error\n\tcalled int\n\tlimit  int\n}\n\n// Populates p with data, returns a count of the bytes written and an\n// error.  The error returned is taken from badReader.errs, with each\n// invocation of Read returning the next error in this slice, or io.EOF,\n// if all errors from the slice have already been returned.  The\n// number of bytes returned is determined by the size of the input buffer\n// the test passes to decoder.Read and will be a multiple of 8, unless\n// badReader.limit is non zero.\nfunc (b *badReader) Read(p []byte) (int, error) {\n\tlim := len(p)\n\tif b.limit != 0 \u0026\u0026 b.limit \u003c lim {\n\t\tlim = b.limit\n\t}\n\tif len(b.data) \u003c lim {\n\t\tlim = len(b.data)\n\t}\n\tfor i := range p[:lim] {\n\t\tp[i] = b.data[i]\n\t}\n\tb.data = b.data[lim:]\n\terr := io.EOF\n\tif b.called \u003c len(b.errs) {\n\t\terr = b.errs[b.called]\n\t}\n\tb.called++\n\treturn lim, err\n}\n\n// TestIssue20044 tests that decoder.Read behaves correctly when the caller\n// supplied reader returns an error.\nfunc TestIssue20044(t *testing.T) {\n\tbadErr := errors.New(\"bad reader error\")\n\ttestCases := []struct {\n\t\tr       badReader\n\t\tres     string\n\t\terr     error\n\t\tdbuflen int\n\t}{\n\t\t// Check valid input data accompanied by an error is processed and the error is propagated.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"d1jprv3fexqq4v34\"), errs: []error{badErr}},\n\t\t\tres: \"helloworld\", err: badErr,\n\t\t},\n\t\t// Check a read error accompanied by input data consisting of newlines only is propagated.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"\\n\\n\\n\\n\\n\\n\\n\\n\"), errs: []error{badErr, nil}},\n\t\t\tres: \"\", err: badErr,\n\t\t},\n\t\t// Reader will be called twice.  The first time it will return 8 newline characters.  The\n\t\t// second time valid base32 encoded data and an error.  The data should be decoded\n\t\t// correctly and the error should be propagated.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"\\n\\n\\n\\n\\n\\n\\n\\nd1jprv3fexqq4v34\"), errs: []error{nil, badErr}},\n\t\t\tres: \"helloworld\", err: badErr, dbuflen: 8,\n\t\t},\n\t\t// Reader returns invalid input data (too short) and an error.  Verify the reader\n\t\t// error is returned.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"c\"), errs: []error{badErr}},\n\t\t\tres: \"\", err: badErr,\n\t\t},\n\t\t// Reader returns invalid input data (too short) but no error.  Verify io.ErrUnexpectedEOF\n\t\t// is returned.\n\t\t// NOTE(thehowl): I don't think this should applyto us?\n\t\t/* {\n\t\t\tr:   badReader{data: []byte(\"c\"), errs: []error{nil}},\n\t\t\tres: \"\", err: io.ErrUnexpectedEOF,\n\t\t},*/\n\t\t// Reader returns invalid input data and an error.  Verify the reader and not the\n\t\t// decoder error is returned.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"cu\"), errs: []error{badErr}},\n\t\t\tres: \"\", err: badErr,\n\t\t},\n\t\t// Reader returns valid data and io.EOF.  Check data is decoded and io.EOF is propagated.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"csqpyrk1\"), errs: []error{io.EOF}},\n\t\t\tres: \"fooba\", err: io.EOF,\n\t\t},\n\t\t// Check errors are properly reported when decoder.Read is called multiple times.\n\t\t// decoder.Read will be called 8 times, badReader.Read will be called twice, returning\n\t\t// valid data both times but an error on the second call.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"dhjp2wvne9jjwc9g\"), errs: []error{nil, badErr}},\n\t\t\tres: \"leasure.10\", err: badErr, dbuflen: 1,\n\t\t},\n\t\t// Check io.EOF is properly reported when decoder.Read is called multiple times.\n\t\t// decoder.Read will be called 8 times, badReader.Read will be called twice, returning\n\t\t// valid data both times but io.EOF on the second call.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"dhjp2wvne9jjw\"), errs: []error{nil, io.EOF}},\n\t\t\tres: \"leasure.\", err: io.EOF, dbuflen: 1,\n\t\t},\n\t\t// The following two test cases check that errors are propagated correctly when more than\n\t\t// 8 bytes are read at a time.\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"dhjp2wvne9jjw\"), errs: []error{io.EOF}},\n\t\t\tres: \"leasure.\", err: io.EOF, dbuflen: 11,\n\t\t},\n\t\t{\n\t\t\tr:   badReader{data: []byte(\"dhjp2wvne9jjwc9g\"), errs: []error{badErr}},\n\t\t\tres: \"leasure.10\", err: badErr, dbuflen: 11,\n\t\t},\n\t\t// Check that errors are correctly propagated when the reader returns valid bytes in\n\t\t// groups that are not divisible by 8.  The first read will return 11 bytes and no\n\t\t// error.  The second will return 7 and an error.  The data should be decoded correctly\n\t\t// and the error should be propagated.\n\t\t// NOTE(thehowl): again, this is on the assumption that this is padded, and it's not.\n\t\t/* {\n\t\t\tr:   badReader{data: []byte(\"dhjp2wvne9jjw\"), errs: []error{nil, badErr}, limit: 11},\n\t\t\tres: \"leasure.\", err: badErr,\n\t\t}, */\n\t}\n\n\tfor idx, tc := range testCases {\n\t\tt.Run(fmt.Sprintf(\"%d-%s\", idx, string(tc.res)), func(t *testing.T) {\n\t\t\tinput := tc.r.data\n\t\t\tdecoder := NewDecoder(\u0026tc.r)\n\t\t\tvar dbuflen int\n\t\t\tif tc.dbuflen \u003e 0 {\n\t\t\t\tdbuflen = tc.dbuflen\n\t\t\t} else {\n\t\t\t\tdbuflen = DecodedLen(len(input))\n\t\t\t}\n\t\t\tdbuf := make([]byte, dbuflen)\n\t\t\tvar err error\n\t\t\tvar res []byte\n\t\t\tfor err == nil {\n\t\t\t\tvar n int\n\t\t\t\tn, err = decoder.Read(dbuf)\n\t\t\t\tif n \u003e 0 {\n\t\t\t\t\tres = append(res, dbuf[:n]...)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttestEqual(t, \"Decoding of %q = %q, want %q\", string(input), string(res), tc.res)\n\t\t\ttestEqual(t, \"Decoding of %q err = %v, expected %v\", string(input), err, tc.err)\n\t\t})\n\t}\n}\n\n// TestDecoderError verifies decode errors are propagated when there are no read\n// errors.\nfunc TestDecoderError(t *testing.T) {\n\tfor _, readErr := range []error{io.EOF, nil} {\n\t\tinput := \"ucsqpyrk1u\"\n\t\tdbuf := make([]byte, DecodedLen(len(input)))\n\t\tbr := badReader{data: []byte(input), errs: []error{readErr}}\n\t\tdecoder := NewDecoder(\u0026br)\n\t\tn, err := decoder.Read(dbuf)\n\t\ttestEqual(t, \"Read after EOF, n = %d, expected %d\", n, 0)\n\t\tif _, ok := err.(CorruptInputError); !ok {\n\t\t\tt.Errorf(\"Corrupt input error expected.  Found %T\", err)\n\t\t}\n\t}\n}\n\n// TestReaderEOF ensures decoder.Read behaves correctly when input data is\n// exhausted.\nfunc TestReaderEOF(t *testing.T) {\n\tfor _, readErr := range []error{io.EOF, nil} {\n\t\tinput := \"MZXW6YTB\"\n\t\tbr := badReader{data: []byte(input), errs: []error{nil, readErr}}\n\t\tdecoder := NewDecoder(\u0026br)\n\t\tdbuf := make([]byte, DecodedLen(len(input)))\n\t\tn, err := decoder.Read(dbuf)\n\t\ttestEqual(t, \"Decoding of %q err = %v, expected %v\", input, err, error(nil))\n\t\tn, err = decoder.Read(dbuf)\n\t\ttestEqual(t, \"Read after EOF, n = %d, expected %d\", n, 0)\n\t\ttestEqual(t, \"Read after EOF, err = %v, expected %v\", err, io.EOF)\n\t\tn, err = decoder.Read(dbuf)\n\t\ttestEqual(t, \"Read after EOF, n = %d, expected %d\", n, 0)\n\t\ttestEqual(t, \"Read after EOF, err = %v, expected %v\", err, io.EOF)\n\t}\n}\n\nfunc TestDecoderBuffering(t *testing.T) {\n\tfor bs := 1; bs \u003c= 12; bs++ {\n\t\tdecoder := NewDecoder(strings.NewReader(bigtest.encoded))\n\t\tbuf := make([]byte, len(bigtest.decoded)+12)\n\t\tvar total int\n\t\tvar n int\n\t\tvar err error\n\t\tfor total = 0; total \u003c len(bigtest.decoded) \u0026\u0026 err == nil; {\n\t\t\tn, err = decoder.Read(buf[total : total+bs])\n\t\t\ttotal += n\n\t\t}\n\t\tif err != nil \u0026\u0026 err != io.EOF {\n\t\t\tt.Errorf(\"Read from %q at pos %d = %d, unexpected error %v\", bigtest.encoded, total, n, err)\n\t\t}\n\t\ttestEqual(t, \"Decoding/%d of %q = %q, want %q\", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)\n\t}\n}\n\nfunc TestDecodeCorrupt(t *testing.T) {\n\ttestCases := []struct {\n\t\tinput  string\n\t\toffset int // -1 means no corruption.\n\t}{\n\t\t{\"\", -1},\n\t\t{\"iIoOlL\", -1},\n\t\t{\"!!!!\", 0},\n\t\t{\"uxp10\", 0},\n\t\t{\"x===\", 1},\n\t\t{\"AA=A====\", 2},\n\t\t{\"AAA=AAAA\", 3},\n\t\t// Much fewer cases compared to Go as there are much fewer cases where input\n\t\t// can be \"corrupted\".\n\t}\n\tfor _, tc := range testCases {\n\t\tdbuf := make([]byte, DecodedLen(len(tc.input)))\n\t\t_, err := Decode(dbuf, []byte(tc.input))\n\t\tif tc.offset == -1 {\n\t\t\tif err != nil {\n\t\t\t\tt.Error(\"Decoder wrongly detected corruption in\", tc.input)\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t\tswitch err := err.(type) {\n\t\tcase CorruptInputError:\n\t\t\ttestEqual(t, \"Corruption in %q at offset %v, want %v\", tc.input, int(err), tc.offset)\n\t\tdefault:\n\t\t\tt.Error(\"Decoder failed to detect corruption in\", tc)\n\t\t}\n\t}\n}\n\nfunc TestBig(t *testing.T) {\n\tn := 3*1000 + 1\n\traw := make([]byte, n)\n\tconst alpha = \"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n\tfor i := 0; i \u003c n; i++ {\n\t\traw[i] = alpha[i%len(alpha)]\n\t}\n\tencoded := new(bytes.Buffer)\n\tw := NewEncoder(encoded)\n\tnn, err := w.Write(raw)\n\tif nn != n || err != nil {\n\t\tt.Fatalf(\"Encoder.Write(raw) = %d, %v want %d, nil\", nn, err, n)\n\t}\n\terr = w.Close()\n\tif err != nil {\n\t\tt.Fatalf(\"Encoder.Close() = %v want nil\", err)\n\t}\n\tdecoded, err := io.ReadAll(NewDecoder(encoded))\n\tif err != nil {\n\t\tt.Fatalf(\"io.ReadAll(NewDecoder(...)): %v\", err)\n\t}\n\n\tif !bytes.Equal(raw, decoded) {\n\t\tvar i int\n\t\tfor i = 0; i \u003c len(decoded) \u0026\u0026 i \u003c len(raw); i++ {\n\t\t\tif decoded[i] != raw[i] {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tt.Errorf(\"Decode(Encode(%d-byte string)) failed at offset %d\", n, i)\n\t}\n}\n\nfunc testStringEncoding(t *testing.T, expected string, examples []string) {\n\tfor _, e := range examples {\n\t\tbuf, err := DecodeString(e)\n\t\tif err != nil {\n\t\t\tt.Errorf(\"Decode(%q) failed: %v\", e, err)\n\t\t\tcontinue\n\t\t}\n\t\tif s := string(buf); s != expected {\n\t\t\tt.Errorf(\"Decode(%q) = %q, want %q\", e, s, expected)\n\t\t}\n\t}\n}\n\nfunc TestNewLineCharacters(t *testing.T) {\n\t// Each of these should decode to the string \"sure\", without errors.\n\texamples := []string{\n\t\t\"EDTQ4S8\",\n\t\t\"EDTQ4S8\\r\",\n\t\t\"EDTQ4S8\\n\",\n\t\t\"EDTQ4S8\\r\\n\",\n\t\t\"EDTQ4S\\r\\n8\",\n\t\t\"EDT\\rQ4S\\n8\",\n\t\t\"edt\\nq4s\\r8\",\n\t\t\"edt\\nq4s8\",\n\t\t\"EDTQ4S\\n8\",\n\t}\n\ttestStringEncoding(t, \"sure\", examples)\n}\n\nfunc BenchmarkEncode(b *testing.B) {\n\tdata := make([]byte, 8192)\n\tbuf := make([]byte, EncodedLen(len(data)))\n\tb.SetBytes(int64(len(data)))\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tEncode(buf, data)\n\t}\n}\n\nfunc BenchmarkEncodeToString(b *testing.B) {\n\tdata := make([]byte, 8192)\n\tb.SetBytes(int64(len(data)))\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tEncodeToString(data)\n\t}\n}\n\nfunc BenchmarkDecode(b *testing.B) {\n\tdata := make([]byte, EncodedLen(8192))\n\tEncode(data, make([]byte, 8192))\n\tbuf := make([]byte, 8192)\n\tb.SetBytes(int64(len(data)))\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tDecode(buf, data)\n\t}\n}\n\nfunc BenchmarkDecodeString(b *testing.B) {\n\tdata := EncodeToString(make([]byte, 8192))\n\tb.SetBytes(int64(len(data)))\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tDecodeString(data)\n\t}\n}\n\n/* TODO: rewrite without using goroutines\nfunc TestBufferedDecodingSameError(t *testing.T) {\n\ttestcases := []struct {\n\t\tprefix            string\n\t\tchunkCombinations [][]string\n\t\texpected          error\n\t}{\n\t\t// Normal case, this is valid input\n\t\t{\"helloworld\", [][]string{\n\t\t\t{\"D1JP\", \"RV3F\", \"EXQQ\", \"4V34\"},\n\t\t\t{\"D1JPRV3FEXQQ4V34\"},\n\t\t\t{\"D1J\", \"PRV\", \"3FE\", \"XQQ\", \"4V3\", \"4\"},\n\t\t\t{\"D1JPRV3FEXQQ4V\", \"34\"},\n\t\t}, nil},\n\n\t\t// Normal case, this is valid input\n\t\t{\"fooba\", [][]string{\n\t\t\t{\"CSQPYRK1\"},\n\t\t\t{\"CSQPYRK\", \"1\"},\n\t\t\t{\"CSQPYR\", \"K1\"},\n\t\t\t{\"CSQPY\", \"RK1\"},\n\t\t\t{\"CSQPY\", \"RK\", \"1\"},\n\t\t\t{\"CSQPY\", \"RK1\"},\n\t\t\t{\"CSQP\", \"YR\", \"K1\"},\n\t\t}, nil},\n\n\t\t// NOTE: many test cases have been removed as we don't return ErrUnexpectedEOF.\n\t}\n\n\tfor _, testcase := range testcases {\n\t\tfor _, chunks := range testcase.chunkCombinations {\n\t\t\tpr, pw := io.Pipe()\n\n\t\t\t// Write the encoded chunks into the pipe\n\t\t\tgo func() {\n\t\t\t\tfor _, chunk := range chunks {\n\t\t\t\t\tpw.Write([]byte(chunk))\n\t\t\t\t}\n\t\t\t\tpw.Close()\n\t\t\t}()\n\n\t\t\tdecoder := NewDecoder(pr)\n\t\t\tback, err := io.ReadAll(decoder)\n\n\t\t\tif err != testcase.expected {\n\t\t\t\tt.Errorf(\"Expected %v, got %v; case %s %+v\", testcase.expected, err, testcase.prefix, chunks)\n\t\t\t}\n\t\t\tif testcase.expected == nil {\n\t\t\t\ttestEqual(t, \"Decode from NewDecoder(chunkReader(%v)) = %q, want %q\", chunks, string(back), testcase.prefix)\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n\nfunc TestEncodedLen(t *testing.T) {\n\ttype test struct {\n\t\tn    int\n\t\twant int64\n\t}\n\ttests := []test{\n\t\t{0, 0},\n\t\t{1, 2},\n\t\t{2, 4},\n\t\t{3, 5},\n\t\t{4, 7},\n\t\t{5, 8},\n\t\t{6, 10},\n\t\t{7, 12},\n\t\t{10, 16},\n\t\t{11, 18},\n\t}\n\t// check overflow\n\ttests = append(tests, test{(math.MaxInt-4)/8 + 1, 1844674407370955162})\n\ttests = append(tests, test{math.MaxInt/8*5 + 4, math.MaxInt})\n\tfor _, tt := range tests {\n\t\tif got := EncodedLen(tt.n); int64(got) != tt.want {\n\t\t\tt.Errorf(\"EncodedLen(%d): got %d, want %d\", tt.n, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestDecodedLen(t *testing.T) {\n\ttype test struct {\n\t\tn    int\n\t\twant int64\n\t}\n\ttests := []test{\n\t\t{0, 0},\n\t\t{2, 1},\n\t\t{4, 2},\n\t\t{5, 3},\n\t\t{7, 4},\n\t\t{8, 5},\n\t\t{10, 6},\n\t\t{12, 7},\n\t\t{16, 10},\n\t\t{18, 11},\n\t}\n\t// check overflow\n\ttests = append(tests, test{math.MaxInt/5 + 1, 1152921504606846976})\n\ttests = append(tests, test{math.MaxInt, 5764607523034234879})\n\tfor _, tt := range tests {\n\t\tif got := DecodedLen(tt.n); int64(got) != tt.want {\n\t\t\tt.Errorf(\"DecodedLen(%d): got %d, want %d\", tt.n, got, tt.want)\n\t\t}\n\t}\n}\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package cford32 implements a modified base32 encoding based on Douglas\n// Crockford's base32 encoding.\npackage cford32\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/cford32/v0\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"gHVgn1zRZXX1ankvUsToto+0++CbHQw3H4W/vlisTsoI7muIJP/8qimzOkdFZmgLNl/b2t+oineO3pBgGENMAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"seqid","path":"gno.land/p/nt/seqid/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n\n# seqid\n\n```\npackage seqid // import \"gno.land/p/nt/seqid/v0\"\n\nPackage seqid provides a simple way to have sequential IDs which will be ordered\ncorrectly when inserted in an AVL tree.\n\nSample usage:\n\n    var id seqid.ID\n    var users avl.Tree\n\n    func NewUser() {\n    \tusers.Set(id.Next().Binary(), \u0026User{ ... })\n    }\n\nTYPES\n\ntype ID uint64\n    An ID is a simple sequential ID generator.\n\nfunc FromBinary(b string) (ID, bool)\n    FromBinary creates a new ID from the given string.\n\nfunc (i ID) Binary() string\n    Binary returns a big-endian binary representation of the ID, suitable to be\n    used as an AVL key.\n\nfunc (i *ID) Next() ID\n    Next advances the ID i. It will panic if increasing ID would overflow.\n\nfunc (i *ID) TryNext() (ID, bool)\n    TryNext increases i by 1 and returns its value. It returns true if\n    successful, or false if the increment would result in an overflow.\n```\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package seqid provides a simple way to have sequential IDs which will be\n// ordered correctly when inserted in an AVL tree.\npackage seqid\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/seqid/v0\"\ngno = \"0.9\"\n"},{"name":"seqid.gno","body":"// Package seqid provides a simple way to have sequential IDs which will be\n// ordered correctly when inserted in an AVL tree.\n//\n// Sample usage:\n//\n//\tvar id seqid.ID\n//\tvar users avl.Tree\n//\n//\tfunc NewUser() {\n//\t\tusers.Set(id.Next().String(), \u0026User{ ... })\n//\t}\npackage seqid\n\nimport (\n\t\"encoding/binary\"\n\n\t\"gno.land/p/nt/cford32/v0\"\n)\n\n// An ID is a simple sequential ID generator.\ntype ID uint64\n\n// Next advances the ID i.\n// It will panic if increasing ID would overflow.\nfunc (i *ID) Next() ID {\n\tnext, ok := i.TryNext()\n\tif !ok {\n\t\tpanic(\"seqid: next ID overflows uint64\")\n\t}\n\treturn next\n}\n\nconst maxID ID = 1\u003c\u003c64 - 1\n\n// TryNext increases i by 1 and returns its value.\n// It returns true if successful, or false if the increment would result in\n// an overflow.\nfunc (i *ID) TryNext() (ID, bool) {\n\tif *i == maxID {\n\t\t// Addition will overflow.\n\t\treturn 0, false\n\t}\n\t*i++\n\treturn *i, true\n}\n\n// Binary returns a big-endian binary representation of the ID,\n// suitable to be used as an AVL key.\nfunc (i ID) Binary() string {\n\tbuf := make([]byte, 8)\n\tbinary.BigEndian.PutUint64(buf, uint64(i))\n\treturn string(buf)\n}\n\n// String encodes i using cford32's compact encoding. For more information,\n// see the documentation for package [gno.land/p/nt/cford32/v0].\n//\n// The result of String will be a 7-byte string for IDs [0,2^34), and a\n// 13-byte string for all values following that. All generated string IDs\n// follow the same lexicographic order as their number values; that is, for any\n// two IDs (x, y) such that x \u003c y, x.String() \u003c y.String().\n// As such, this string representation is suitable to be used as an AVL key.\nfunc (i ID) String() string {\n\treturn string(cford32.PutCompact(uint64(i)))\n}\n\n// FromBinary creates a new ID from the given string, expected to be a binary\n// big-endian encoding of an ID (such as that of [ID.Binary]).\n// The second return value is true if the conversion was successful.\nfunc FromBinary(b string) (ID, bool) {\n\tif len(b) != 8 {\n\t\treturn 0, false\n\t}\n\treturn ID(binary.BigEndian.Uint64([]byte(b))), true\n}\n\n// FromString creates a new ID from the given string, expected to be a string\n// representation using cford32, such as that returned by [ID.String].\n//\n// The encoding scheme used by cford32 allows the same ID to have many\n// different representations (though the one returned by [ID.String] is only\n// one, deterministic and safe to be used in AVL). The encoding scheme is\n// \"human-centric\" and is thus case insensitive, and maps some ambiguous\n// characters to be the same, ie. L = I = 1, O = 0. For this reason, when\n// parsing user input to retrieve a key (encoded as a string), always sanitize\n// it first using FromString, then run String(), instead of using the user's\n// input directly.\nfunc FromString(b string) (ID, error) {\n\tn, err := cford32.Uint64([]byte(b))\n\treturn ID(n), err\n}\n"},{"name":"seqid_test.gno","body":"package seqid\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestID(t *testing.T) {\n\tvar i ID\n\n\tfor j := 0; j \u003c 100; j++ {\n\t\ti.Next()\n\t}\n\tif i != 100 {\n\t\tt.Fatalf(\"invalid: wanted %d got %d\", 100, i)\n\t}\n}\n\nfunc TestID_Overflow(t *testing.T) {\n\ti := ID(maxID)\n\n\tdefer func() {\n\t\terr := recover()\n\t\tif !strings.Contains(fmt.Sprint(err), \"next ID overflows\") {\n\t\t\tt.Errorf(\"did not overflow\")\n\t\t}\n\t}()\n\n\ti.Next()\n}\n\nfunc TestID_Binary(cur realm, t *testing.T) {\n\tvar i ID\n\tprev := i.Binary()\n\n\tfor j := 0; j \u003c 1000; j++ {\n\t\tcur := i.Next().Binary()\n\t\tif cur \u003c= prev {\n\t\t\tt.Fatalf(\"cur %x \u003e prev %x\", cur, prev)\n\t\t}\n\t\tprev = cur\n\t}\n}\n\nfunc TestID_String(cur realm, t *testing.T) {\n\tvar i ID\n\tprev := i.String()\n\n\tfor j := 0; j \u003c 1000; j++ {\n\t\tcur := i.Next().String()\n\t\tif cur \u003c= prev {\n\t\t\tt.Fatalf(\"cur %s \u003e prev %s\", cur, prev)\n\t\t}\n\t\tprev = cur\n\t}\n\n\t// Test for when cford32 switches over to the long encoding.\n\ti = 1\u003c\u003c34 - 512\n\tfor j := 0; j \u003c 1024; j++ {\n\t\tcur := i.Next().String()\n\t\t// println(cur)\n\t\tif cur \u003c= prev {\n\t\t\tt.Fatalf(\"cur %s \u003e prev %s\", cur, prev)\n\t\t}\n\t\tprev = cur\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"pos+5SpSo7KlXdr/saKjUS89HIRho/oKPvaqoqW/ok8F2R37WdtEDSsz9frEhjnGmjlm71VtSQh3nDXgpg3jAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"pager","path":"gno.land/p/nt/avl/v0/pager","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/avl/v0/pager\"\ngno = \"0.9\"\n"},{"name":"pager.gno","body":"package pager\n\nimport (\n\t\"math\"\n\t\"net/url\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0/rotree\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Pager is a struct that holds the AVL tree and pagination parameters.\ntype Pager struct {\n\tTree            rotree.IReadOnlyTree\n\tPageQueryParam  string\n\tSizeQueryParam  string\n\tDefaultPageSize int\n\tReversed        bool\n}\n\n// Page represents a single page of results.\ntype Page struct {\n\tItems      []Item\n\tPageNumber int\n\tPageSize   int\n\tTotalItems int\n\tTotalPages int\n\tHasPrev    bool\n\tHasNext    bool\n\tPager      *Pager // Reference to the parent Pager\n}\n\n// Item represents a key-value pair in the AVL tree.\ntype Item struct {\n\tKey   string\n\tValue any\n}\n\n// NewPager creates a new Pager with default values.\nfunc NewPager(tree rotree.IReadOnlyTree, defaultPageSize int, reversed bool) *Pager {\n\treturn \u0026Pager{\n\t\tTree:            tree,\n\t\tPageQueryParam:  \"page\",\n\t\tSizeQueryParam:  \"size\",\n\t\tDefaultPageSize: defaultPageSize,\n\t\tReversed:        reversed,\n\t}\n}\n\n// GetPage retrieves a page of results from the AVL tree.\nfunc (p *Pager) GetPage(pageNumber int) *Page {\n\treturn p.GetPageWithSize(pageNumber, p.DefaultPageSize)\n}\n\nfunc (p *Pager) GetPageWithSize(pageNumber, pageSize int) *Page {\n\tif pageSize \u003c= 0 {\n\t\tpanic(\"GetPageWithSize: invalid page size\")\n\t}\n\n\ttotalItems := p.Tree.Size()\n\ttotalPages := int(math.Ceil(float64(totalItems) / float64(pageSize)))\n\n\tpage := \u0026Page{\n\t\tTotalItems: totalItems,\n\t\tTotalPages: totalPages,\n\t\tPageSize:   pageSize,\n\t\tPager:      p,\n\t}\n\n\t// page number provided is not available\n\tif pageNumber \u003c 1 {\n\t\tpage.HasNext = totalPages \u003e 0\n\t\treturn page\n\t}\n\n\t// page number provided is outside the range of total pages\n\tif pageNumber \u003e totalPages {\n\t\tpage.PageNumber = pageNumber\n\t\tpage.HasPrev = pageNumber \u003e 0\n\t\treturn page\n\t}\n\n\tstartIndex := (pageNumber - 1) * pageSize\n\tendIndex := startIndex + pageSize\n\tif endIndex \u003e totalItems {\n\t\tendIndex = totalItems\n\t}\n\n\titems := []Item{}\n\n\tif p.Reversed {\n\t\tp.Tree.ReverseIterateByOffset(startIndex, endIndex-startIndex, func(key string, value any) bool {\n\t\t\titems = append(items, Item{Key: key, Value: value})\n\t\t\treturn false\n\t\t})\n\t} else {\n\t\tp.Tree.IterateByOffset(startIndex, endIndex-startIndex, func(key string, value any) bool {\n\t\t\titems = append(items, Item{Key: key, Value: value})\n\t\t\treturn false\n\t\t})\n\t}\n\n\tpage.Items = items\n\tpage.PageNumber = pageNumber\n\tpage.HasPrev = pageNumber \u003e 1\n\tpage.HasNext = pageNumber \u003c totalPages\n\treturn page\n}\n\nfunc (p *Pager) MustGetPageByPath(rawURL string) *Page {\n\tpage, err := p.GetPageByPath(rawURL)\n\tif err != nil {\n\t\tpanic(\"invalid path\")\n\t}\n\treturn page\n}\n\n// GetPageByPath retrieves a page of results based on the query parameters in the URL path.\nfunc (p *Pager) GetPageByPath(rawURL string) (*Page, error) {\n\tpageNumber, pageSize, err := p.ParseQuery(rawURL)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn p.GetPageWithSize(pageNumber, pageSize), nil\n}\n\n// Picker generates the Markdown UI for the page Picker\nfunc (p *Page) Picker(path string) string {\n\tpageNumber := p.PageNumber\n\tpageNumber = max(pageNumber, 1)\n\n\tif p.TotalPages \u003c= 1 {\n\t\treturn \"\"\n\t}\n\n\tu, _ := url.Parse(path)\n\tquery := u.Query()\n\n\t// Remove existing page query parameter\n\tquery.Del(p.Pager.PageQueryParam)\n\n\t// Encode remaining query parameters\n\tbaseQuery := query.Encode()\n\tif baseQuery != \"\" {\n\t\tbaseQuery = \"\u0026\" + baseQuery\n\t}\n\tmd := \"\"\n\n\tif p.HasPrev {\n\t\tmd += ufmt.Sprintf(\"[%d](?%s=%d%s) | \", 1, p.Pager.PageQueryParam, 1, baseQuery)\n\n\t\tif p.PageNumber \u003e 4 {\n\t\t\tmd += \"… | \"\n\t\t}\n\n\t\tif p.PageNumber \u003e 3 {\n\t\t\tmd += ufmt.Sprintf(\"[%d](?%s=%d%s) | \", p.PageNumber-2, p.Pager.PageQueryParam, p.PageNumber-2, baseQuery)\n\t\t}\n\n\t\tif p.PageNumber \u003e 2 {\n\t\t\tmd += ufmt.Sprintf(\"[%d](?%s=%d%s) | \", p.PageNumber-1, p.Pager.PageQueryParam, p.PageNumber-1, baseQuery)\n\t\t}\n\t}\n\n\tif p.PageNumber \u003e 0 \u0026\u0026 p.PageNumber \u003c= p.TotalPages {\n\t\tmd += ufmt.Sprintf(\"**%d**\", p.PageNumber)\n\t} else {\n\t\tmd += ufmt.Sprintf(\"_%d_\", p.PageNumber)\n\t}\n\n\tif p.HasNext {\n\t\tif p.PageNumber \u003c p.TotalPages-1 {\n\t\t\tmd += ufmt.Sprintf(\" | [%d](?%s=%d%s)\", p.PageNumber+1, p.Pager.PageQueryParam, p.PageNumber+1, baseQuery)\n\t\t}\n\n\t\tif p.PageNumber \u003c p.TotalPages-2 {\n\t\t\tmd += ufmt.Sprintf(\" | [%d](?%s=%d%s)\", p.PageNumber+2, p.Pager.PageQueryParam, p.PageNumber+2, baseQuery)\n\t\t}\n\n\t\tif p.PageNumber \u003c p.TotalPages-3 {\n\t\t\tmd += \" | …\"\n\t\t}\n\n\t\tmd += ufmt.Sprintf(\" | [%d](?%s=%d%s)\", p.TotalPages, p.Pager.PageQueryParam, p.TotalPages, baseQuery)\n\t}\n\n\treturn md\n}\n\n// ParseQuery parses the URL to extract the page number and page size.\nfunc (p *Pager) ParseQuery(rawURL string) (int, int, error) {\n\tu, err := url.Parse(rawURL)\n\tif err != nil {\n\t\treturn 1, p.DefaultPageSize, err\n\t}\n\n\tquery := u.Query()\n\tpageNumber := 1\n\tpageSize := p.DefaultPageSize\n\n\tif p.PageQueryParam != \"\" {\n\t\tif pageStr := query.Get(p.PageQueryParam); pageStr != \"\" {\n\t\t\tpageNumber, err = strconv.Atoi(pageStr)\n\t\t\tif err != nil || pageNumber \u003c 1 {\n\t\t\t\tpageNumber = 1\n\t\t\t}\n\t\t}\n\t}\n\n\tif p.SizeQueryParam != \"\" {\n\t\tif sizeStr := query.Get(p.SizeQueryParam); sizeStr != \"\" {\n\t\t\tpageSize, err = strconv.Atoi(sizeStr)\n\t\t\tif err != nil || pageSize \u003c 1 {\n\t\t\t\tpageSize = p.DefaultPageSize\n\t\t\t}\n\t\t}\n\t}\n\n\treturn pageNumber, pageSize, nil\n}\n\nfunc max(a, b int) int {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n"},{"name":"pager_test.gno","body":"package pager\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestPager_GetPage(t *testing.T) {\n\t// Create a new AVL tree and populate it with some key-value pairs.\n\ttree := avl.NewTree()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\tt.Run(\"normal ordering\", func(t *testing.T) {\n\t\t// Create a new pager.\n\t\tpager := NewPager(tree, 10, false)\n\n\t\t// Define test cases.\n\t\ttests := []struct {\n\t\t\tpageNumber int\n\t\t\tpageSize   int\n\t\t\texpected   []Item\n\t\t}{\n\t\t\t{1, 2, []Item{{Key: \"a\", Value: 1}, {Key: \"b\", Value: 2}}},\n\t\t\t{2, 2, []Item{{Key: \"c\", Value: 3}, {Key: \"d\", Value: 4}}},\n\t\t\t{3, 2, []Item{{Key: \"e\", Value: 5}}},\n\t\t\t{1, 3, []Item{{Key: \"a\", Value: 1}, {Key: \"b\", Value: 2}, {Key: \"c\", Value: 3}}},\n\t\t\t{2, 3, []Item{{Key: \"d\", Value: 4}, {Key: \"e\", Value: 5}}},\n\t\t\t{1, 5, []Item{{Key: \"a\", Value: 1}, {Key: \"b\", Value: 2}, {Key: \"c\", Value: 3}, {Key: \"d\", Value: 4}, {Key: \"e\", Value: 5}}},\n\t\t\t{2, 5, []Item{}},\n\t\t}\n\n\t\tfor _, tt := range tests {\n\t\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\t\tuassert.Equal(t, len(tt.expected), len(page.Items))\n\n\t\t\tfor i, item := range page.Items {\n\t\t\t\tuassert.Equal(t, tt.expected[i].Key, item.Key)\n\t\t\t\tuassert.Equal(t, tt.expected[i].Value, item.Value)\n\t\t\t}\n\t\t}\n\t})\n\n\tt.Run(\"reversed ordering\", func(t *testing.T) {\n\t\t// Create a new pager.\n\t\tpager := NewPager(tree, 10, true)\n\n\t\t// Define test cases.\n\t\ttests := []struct {\n\t\t\tpageNumber int\n\t\t\tpageSize   int\n\t\t\texpected   []Item\n\t\t}{\n\t\t\t{1, 2, []Item{{Key: \"e\", Value: 5}, {Key: \"d\", Value: 4}}},\n\t\t\t{2, 2, []Item{{Key: \"c\", Value: 3}, {Key: \"b\", Value: 2}}},\n\t\t\t{3, 2, []Item{{Key: \"a\", Value: 1}}},\n\t\t\t{1, 3, []Item{{Key: \"e\", Value: 5}, {Key: \"d\", Value: 4}, {Key: \"c\", Value: 3}}},\n\t\t\t{2, 3, []Item{{Key: \"b\", Value: 2}, {Key: \"a\", Value: 1}}},\n\t\t\t{1, 5, []Item{{Key: \"e\", Value: 5}, {Key: \"d\", Value: 4}, {Key: \"c\", Value: 3}, {Key: \"b\", Value: 2}, {Key: \"a\", Value: 1}}},\n\t\t\t{2, 5, []Item{}},\n\t\t}\n\n\t\tfor _, tt := range tests {\n\t\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\t\tuassert.Equal(t, len(tt.expected), len(page.Items))\n\n\t\t\tfor i, item := range page.Items {\n\t\t\t\tuassert.Equal(t, tt.expected[i].Key, item.Key)\n\t\t\t\tuassert.Equal(t, tt.expected[i].Value, item.Value)\n\t\t\t}\n\t\t}\n\t})\n}\n\nfunc TestPager_GetPageByPath(t *testing.T) {\n\t// Create a new AVL tree and populate it with some key-value pairs.\n\ttree := avl.NewTree()\n\tfor i := 0; i \u003c 50; i++ {\n\t\ttree.Set(ufmt.Sprintf(\"key%d\", i), i)\n\t}\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases.\n\ttests := []struct {\n\t\trawURL       string\n\t\texpectedPage int\n\t\texpectedSize int\n\t}{\n\t\t{\"/r/foo:bar/baz?size=10\u0026page=1\", 1, 10},\n\t\t{\"/r/foo:bar/baz?size=10\u0026page=2\", 2, 10},\n\t\t{\"/r/foo:bar/baz?page=3\", 3, pager.DefaultPageSize},\n\t\t{\"/r/foo:bar/baz?size=20\", 1, 20},\n\t\t{\"/r/foo:bar/baz\", 1, pager.DefaultPageSize},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage, err := pager.GetPageByPath(tt.rawURL)\n\t\turequire.NoError(t, err, ufmt.Sprintf(\"GetPageByPath(%s) returned error: %v\", tt.rawURL, err))\n\n\t\tuassert.Equal(t, tt.expectedPage, page.PageNumber)\n\t\tuassert.Equal(t, tt.expectedSize, page.PageSize)\n\t}\n}\n\nfunc TestPage_Picker(t *testing.T) {\n\t// Create a new AVL tree and populate it with some key-value pairs.\n\ttree := avl.NewTree()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases.\n\ttests := []struct {\n\t\tpageNumber int\n\t\tpageSize   int\n\t\tpath       string\n\t\texpected   string\n\t}{\n\t\t{1, 2, \"/test\", \"**1** | [2](?page=2) | [3](?page=3)\"},\n\t\t{2, 2, \"/test\", \"[1](?page=1) | **2** | [3](?page=3)\"},\n\t\t{3, 2, \"/test\", \"[1](?page=1) | [2](?page=2) | **3**\"},\n\t\t{1, 2, \"/test?foo=bar\", \"**1** | [2](?page=2\u0026foo=bar) | [3](?page=3\u0026foo=bar)\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\tui := page.Picker(tt.path)\n\t\tuassert.Equal(t, tt.expected, ui)\n\t}\n}\n\nfunc TestPager_UI_WithManyPages(t *testing.T) {\n\t// Create a new AVL tree and populate it with many key-value pairs.\n\ttree := avl.NewTree()\n\tfor i := 0; i \u003c 100; i++ {\n\t\ttree.Set(ufmt.Sprintf(\"key%d\", i), i)\n\t}\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases for a large number of pages.\n\ttests := []struct {\n\t\tpageNumber int\n\t\tpageSize   int\n\t\tpath       string\n\t\texpected   string\n\t}{\n\t\t{1, 10, \"/test\", \"**1** | [2](?page=2) | [3](?page=3) | … | [10](?page=10)\"},\n\t\t{2, 10, \"/test\", \"[1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | … | [10](?page=10)\"},\n\t\t{3, 10, \"/test\", \"[1](?page=1) | [2](?page=2) | **3** | [4](?page=4) | [5](?page=5) | … | [10](?page=10)\"},\n\t\t{4, 10, \"/test\", \"[1](?page=1) | [2](?page=2) | [3](?page=3) | **4** | [5](?page=5) | [6](?page=6) | … | [10](?page=10)\"},\n\t\t{5, 10, \"/test\", \"[1](?page=1) | … | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6) | [7](?page=7) | … | [10](?page=10)\"},\n\t\t{6, 10, \"/test\", \"[1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6** | [7](?page=7) | [8](?page=8) | … | [10](?page=10)\"},\n\t\t{7, 10, \"/test\", \"[1](?page=1) | … | [5](?page=5) | [6](?page=6) | **7** | [8](?page=8) | [9](?page=9) | [10](?page=10)\"},\n\t\t{8, 10, \"/test\", \"[1](?page=1) | … | [6](?page=6) | [7](?page=7) | **8** | [9](?page=9) | [10](?page=10)\"},\n\t\t{9, 10, \"/test\", \"[1](?page=1) | … | [7](?page=7) | [8](?page=8) | **9** | [10](?page=10)\"},\n\t\t{10, 10, \"/test\", \"[1](?page=1) | … | [8](?page=8) | [9](?page=9) | **10**\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\tui := page.Picker(tt.path)\n\t\tuassert.Equal(t, tt.expected, ui)\n\t}\n}\n\nfunc TestPager_ParseQuery(t *testing.T) {\n\t// Create a new AVL tree and populate it with some key-value pairs.\n\ttree := avl.NewTree()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases.\n\ttests := []struct {\n\t\trawURL        string\n\t\texpectedPage  int\n\t\texpectedSize  int\n\t\texpectedError bool\n\t}{\n\t\t{\"/r/foo:bar/baz?size=2\u0026page=1\", 1, 2, false},\n\t\t{\"/r/foo:bar/baz?size=3\u0026page=2\", 2, 3, false},\n\t\t{\"/r/foo:bar/baz?size=5\u0026page=3\", 3, 5, false},\n\t\t{\"/r/foo:bar/baz?page=2\", 2, pager.DefaultPageSize, false},\n\t\t{\"/r/foo:bar/baz?size=3\", 1, 3, false},\n\t\t{\"/r/foo:bar/baz\", 1, pager.DefaultPageSize, false},\n\t\t{\"/r/foo:bar/baz?size=0\u0026page=0\", 1, pager.DefaultPageSize, false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage, size, err := pager.ParseQuery(tt.rawURL)\n\t\tif tt.expectedError {\n\t\t\tuassert.Error(t, err, ufmt.Sprintf(\"ParseQuery(%s) expected error but got none\", tt.rawURL))\n\t\t} else {\n\t\t\turequire.NoError(t, err, ufmt.Sprintf(\"ParseQuery(%s) returned error: %v\", tt.rawURL, err))\n\t\t\tuassert.Equal(t, tt.expectedPage, page, ufmt.Sprintf(\"ParseQuery(%s) returned page %d, expected %d\", tt.rawURL, page, tt.expectedPage))\n\t\t\tuassert.Equal(t, tt.expectedSize, size, ufmt.Sprintf(\"ParseQuery(%s) returned size %d, expected %d\", tt.rawURL, size, tt.expectedSize))\n\t\t}\n\t}\n}\n\nfunc TestPage_PickerQueryParamPreservation(t *testing.T) {\n\ttree := avl.NewTree()\n\tfor i := 1; i \u003c= 6; i++ {\n\t\ttree.Set(ufmt.Sprintf(\"key%d\", i), i)\n\t}\n\n\tpager := NewPager(tree, 2, false)\n\n\ttests := []struct {\n\t\tname       string\n\t\tpageNumber int\n\t\tpath       string\n\t\texpected   string\n\t}{\n\t\t{\n\t\t\tname:       \"single query param\",\n\t\t\tpageNumber: 1,\n\t\t\tpath:       \"/test?foo=bar\",\n\t\t\texpected:   \"**1** | [2](?page=2\u0026foo=bar) | [3](?page=3\u0026foo=bar)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"multiple query params\",\n\t\t\tpageNumber: 2,\n\t\t\tpath:       \"/test?foo=bar\u0026baz=qux\",\n\t\t\texpected:   \"[1](?page=1\u0026baz=qux\u0026foo=bar) | **2** | [3](?page=3\u0026baz=qux\u0026foo=bar)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"overwrite existing page param\",\n\t\t\tpageNumber: 1,\n\t\t\tpath:       \"/test?param1=value1\u0026page=999\u0026param2=value2\",\n\t\t\texpected:   \"**1** | [2](?page=2\u0026param1=value1\u0026param2=value2) | [3](?page=3\u0026param1=value1\u0026param2=value2)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"empty query string\",\n\t\t\tpageNumber: 2,\n\t\t\tpath:       \"/test\",\n\t\t\texpected:   \"[1](?page=1) | **2** | [3](?page=3)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"query string with only page param\",\n\t\t\tpageNumber: 2,\n\t\t\tpath:       \"/test?page=2\",\n\t\t\texpected:   \"[1](?page=1) | **2** | [3](?page=3)\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tpage := pager.GetPageWithSize(tt.pageNumber, 2)\n\t\t\tresult := page.Picker(tt.path)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"\\nwant: %s\\ngot:  %s\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"z_filetest.gno","body":"package main\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc main() {\n\t// Create a new AVL tree and populate it with some key-value pairs.\n\tvar id seqid.ID\n\ttree := avl.NewTree()\n\tfor i := 0; i \u003c 42; i++ {\n\t\ttree.Set(id.Next().String(), i)\n\t}\n\n\t// Create a new pager.\n\tpager := pager.NewPager(tree, 7, false)\n\n\tfor pn := -1; pn \u003c 8; pn++ {\n\t\tpage := pager.GetPage(pn)\n\n\t\tprintln(ufmt.Sprintf(\"## Page %d of %d\", page.PageNumber, page.TotalPages))\n\t\tfor idx, item := range page.Items {\n\t\t\tprintln(ufmt.Sprintf(\"- idx=%d key=%s value=%d\", idx, item.Key, item.Value))\n\t\t}\n\t\tprintln(page.Picker(\"/\"))\n\t\tprintln()\n\t}\n}\n\n// Output:\n// ## Page 0 of 6\n// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)\n//\n// ## Page 0 of 6\n// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)\n//\n// ## Page 1 of 6\n// - idx=0 key=0000001 value=0\n// - idx=1 key=0000002 value=1\n// - idx=2 key=0000003 value=2\n// - idx=3 key=0000004 value=3\n// - idx=4 key=0000005 value=4\n// - idx=5 key=0000006 value=5\n// - idx=6 key=0000007 value=6\n// **1** | [2](?page=2) | [3](?page=3) | … | [6](?page=6)\n//\n// ## Page 2 of 6\n// - idx=0 key=0000008 value=7\n// - idx=1 key=0000009 value=8\n// - idx=2 key=000000a value=9\n// - idx=3 key=000000b value=10\n// - idx=4 key=000000c value=11\n// - idx=5 key=000000d value=12\n// - idx=6 key=000000e value=13\n// [1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | … | [6](?page=6)\n//\n// ## Page 3 of 6\n// - idx=0 key=000000f value=14\n// - idx=1 key=000000g value=15\n// - idx=2 key=000000h value=16\n// - idx=3 key=000000j value=17\n// - idx=4 key=000000k value=18\n// - idx=5 key=000000m value=19\n// - idx=6 key=000000n value=20\n// [1](?page=1) | [2](?page=2) | **3** | [4](?page=4) | [5](?page=5) | [6](?page=6)\n//\n// ## Page 4 of 6\n// - idx=0 key=000000p value=21\n// - idx=1 key=000000q value=22\n// - idx=2 key=000000r value=23\n// - idx=3 key=000000s value=24\n// - idx=4 key=000000t value=25\n// - idx=5 key=000000v value=26\n// - idx=6 key=000000w value=27\n// [1](?page=1) | [2](?page=2) | [3](?page=3) | **4** | [5](?page=5) | [6](?page=6)\n//\n// ## Page 5 of 6\n// - idx=0 key=000000x value=28\n// - idx=1 key=000000y value=29\n// - idx=2 key=000000z value=30\n// - idx=3 key=0000010 value=31\n// - idx=4 key=0000011 value=32\n// - idx=5 key=0000012 value=33\n// - idx=6 key=0000013 value=34\n// [1](?page=1) | … | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6)\n//\n// ## Page 6 of 6\n// - idx=0 key=0000014 value=35\n// - idx=1 key=0000015 value=36\n// - idx=2 key=0000016 value=37\n// - idx=3 key=0000017 value=38\n// - idx=4 key=0000018 value=39\n// - idx=5 key=0000019 value=40\n// - idx=6 key=000001a value=41\n// [1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6**\n//\n// ## Page 7 of 6\n// [1](?page=1) | … | [5](?page=5) | [6](?page=6) | _7_\n//\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"WymM/H6mdRGgP9KPVhMs5YHQg1OEE9cepUQXcbsl5/ZlPEnMS5QHNX+4DuttSR7lJfP1inW/xan86QG1o4aaAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"mux","path":"gno.land/p/nt/mux/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# mux\n\nPackage `mux` provides a simple routing and rendering library for handling dynamic path-based requests in Gno contracts, similar to `http.ServeMux` in Go.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package mux provides a simple routing and rendering library for handling dynamic path-based requests in Gno contracts.\n//\n// The `mux` package aims to offer similar functionality to `http.ServeMux` in Go, but for Gno's Render() requests.\n// It allows you to define routes with dynamic parts and associate them with corresponding handler functions for rendering outputs.\n//\n// Usage:\n// 1. Create a new Router instance using `NewRouter()` to handle routing and rendering logic.\n// 2. Register routes and their associated handler functions using the `Handle(route, handler)` method.\n// 3. Implement the rendering logic within the handler functions, utilizing the `Request` and `ResponseWriter` types.\n// 4. Use the `Render(path)` method to process a given path and execute the corresponding handler function to obtain the rendered output.\n//\n// Route Patterns:\n// Routes can include dynamic parts enclosed in braces, such as \"users/{id}\" or \"hello/{name}\". The `Request` object's `GetVar(key)`\n// method allows you to extract the value of a specific variable from the path based on routing rules.\n//\n// Example:\n//\n//\trouter := mux.NewRouter()\n//\n//\t// Define a route with a variable and associated handler function\n//\trouter.HandleFunc(\"hello/{name}\", func(res *mux.ResponseWriter, req *mux.Request) {\n//\t\tname := req.GetVar(\"name\")\n//\t\tif name != \"\" {\n//\t\t\tres.Write(\"Hello, \" + name + \"!\")\n//\t\t} else {\n//\t\t\tres.Write(\"Hello, world!\")\n//\t\t}\n//\t})\n//\n//\t// Render the output for the \"/hello/Alice\" path\n//\toutput := router.Render(\"hello/Alice\")\n//\t// Output: \"Hello, Alice!\"\n//\n// Note: The `mux` package provides a basic routing and rendering mechanism for simple use cases. For more advanced routing features,\n// consider using more specialized libraries or frameworks.\npackage mux\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/mux/v0\"\ngno = \"0.9\"\n"},{"name":"handler.gno","body":"package mux\n\n// Handler stores a route pattern with one of two handler shapes.\n// Fn (HandlerFunc, no rlm) is set by HandleFunc; FnRlm (HandlerFuncRlm,\n// rlm-aware non-crossing) is set by HandleFuncRlm. Exactly one is set\n// per route. RenderRlm dispatches FnRlm with the supplied rlm; Render\n// dispatches Fn and panics if the matched route was registered with\n// HandleFuncRlm (caller used the wrong dispatch method).\ntype Handler struct {\n\tPattern string\n\tFn      HandlerFunc\n\tFnRlm   HandlerFuncRlm\n}\n\ntype HandlerFunc func(*ResponseWriter, *Request)\n\n// HandlerFuncRlm is the rlm-aware handler shape — non-crossing\n// (`_ int, rlm realm` first params) so callers thread cur as data\n// for the handler to forward to downstream crossing functions.\ntype HandlerFuncRlm func(_ int, rlm realm, res *ResponseWriter, req *Request)\n\ntype ErrHandlerFunc func(*ResponseWriter, *Request) error\n\ntype NotFoundHandler func(*ResponseWriter, *Request)\n\n// TODO: AutomaticIndex\n"},{"name":"helpers.gno","body":"package mux\n\nfunc defaultNotFoundHandler(res *ResponseWriter, req *Request) {\n\tres.Write(\"404\")\n}\n"},{"name":"request.gno","body":"package mux\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n)\n\n// Request represents an incoming request.\ntype Request struct {\n\t// Path is request path name.\n\t//\n\t// Note: use RawPath to obtain a raw path with query string.\n\tPath string\n\n\t// RawPath contains a whole request path, including query string.\n\tRawPath string\n\n\t// HandlerPath is handler rule that matches a request.\n\tHandlerPath string\n\n\t// Query contains the parsed URL query parameters.\n\tQuery url.Values\n}\n\n// GetVar retrieves a variable from the path based on routing rules.\nfunc (r *Request) GetVar(key string) string {\n\thandlerParts := strings.Split(r.HandlerPath, \"/\")\n\treqParts := strings.Split(r.Path, \"/\")\n\treqIndex := 0\n\tfor handlerIndex := 0; handlerIndex \u003c len(handlerParts); handlerIndex++ {\n\t\thandlerPart := handlerParts[handlerIndex]\n\t\tswitch {\n\t\tcase handlerPart == \"*\":\n\t\t\t// If a wildcard \"*\" is found, consume all remaining segments\n\t\t\twildcardParts := reqParts[reqIndex:]\n\t\t\treqIndex = len(reqParts)                // Consume all remaining segments\n\t\t\treturn strings.Join(wildcardParts, \"/\") // Return all remaining segments as a string\n\t\tcase strings.HasPrefix(handlerPart, \"{\") \u0026\u0026 strings.HasSuffix(handlerPart, \"}\"):\n\t\t\t// If a variable of the form {param} is found we compare it with the key\n\t\t\tparameter := handlerPart[1 : len(handlerPart)-1]\n\t\t\tif parameter == key {\n\t\t\t\treturn reqParts[reqIndex]\n\t\t\t}\n\t\t\treqIndex++\n\t\tdefault:\n\t\t\tif reqIndex \u003e= len(reqParts) || handlerPart != reqParts[reqIndex] {\n\t\t\t\treturn \"\"\n\t\t\t}\n\t\t\treqIndex++\n\t\t}\n\t}\n\n\treturn \"\"\n}\n"},{"name":"request_test.gno","body":"package mux\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestRequest_GetVar(t *testing.T) {\n\tcases := []struct {\n\t\thandlerPath    string\n\t\treqPath        string\n\t\tgetVarKey      string\n\t\texpectedOutput string\n\t}{\n\n\t\t{\"users/{id}\", \"users/123\", \"id\", \"123\"},\n\t\t{\"users/123\", \"users/123\", \"id\", \"\"},\n\t\t{\"users/{id}\", \"users/123\", \"nonexistent\", \"\"},\n\t\t{\"users/{userId}/posts/{postId}\", \"users/123/posts/456\", \"userId\", \"123\"},\n\t\t{\"users/{userId}/posts/{postId}\", \"users/123/posts/456\", \"postId\", \"456\"},\n\n\t\t// Wildcards\n\t\t{\"*\", \"users/123\", \"*\", \"users/123\"},\n\t\t{\"*\", \"users/123/posts/456\", \"*\", \"users/123/posts/456\"},\n\t\t{\"*\", \"users/123/posts/456/comments/789\", \"*\", \"users/123/posts/456/comments/789\"},\n\t\t{\"users/*\", \"users/john/posts\", \"*\", \"john/posts\"},\n\t\t{\"users/*/comments\", \"users/jane/comments\", \"*\", \"jane/comments\"},\n\t\t{\"api/*/posts/*\", \"api/v1/posts/123\", \"*\", \"v1/posts/123\"},\n\n\t\t// wildcards and parameters\n\t\t{\"api/{version}/*\", \"api/v1/user/settings\", \"version\", \"v1\"},\n\t}\n\tfor _, tt := range cases {\n\t\tname := ufmt.Sprintf(\"%s-%s\", tt.handlerPath, tt.reqPath)\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\treq := \u0026Request{\n\t\t\t\tHandlerPath: tt.handlerPath,\n\t\t\t\tPath:        tt.reqPath,\n\t\t\t}\n\t\t\toutput := req.GetVar(tt.getVarKey)\n\t\t\tuassert.Equal(t, tt.expectedOutput, output,\n\t\t\t\t\"handler: %q, path: %q, key: %q\",\n\t\t\t\ttt.handlerPath, tt.reqPath, tt.getVarKey)\n\t\t})\n\t}\n}\n"},{"name":"response.gno","body":"package mux\n\nimport \"strings\"\n\n// ResponseWriter represents the response writer.\ntype ResponseWriter struct {\n\toutput strings.Builder\n}\n\n// Write appends data to the response output.\nfunc (rw *ResponseWriter) Write(data string) {\n\trw.output.WriteString(data)\n}\n\n// Output returns the final response output.\nfunc (rw *ResponseWriter) Output() string {\n\treturn rw.output.String()\n}\n\n// TODO: func (rw *ResponseWriter) Header()...\n"},{"name":"router.gno","body":"package mux\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n)\n\n// Router handles the routing and rendering logic.\ntype Router struct {\n\troutes          []Handler\n\tNotFoundHandler NotFoundHandler\n}\n\n// NewRouter creates a new Router instance.\nfunc NewRouter() *Router {\n\treturn \u0026Router{\n\t\troutes:          make([]Handler, 0),\n\t\tNotFoundHandler: defaultNotFoundHandler,\n\t}\n}\n\n// Render renders the output for the given path using the registered route handler.\nfunc (r *Router) Render(reqPath string) string {\n\tclearPath, rawQuery, _ := strings.Cut(reqPath, \"?\")\n\tquery, _ := url.ParseQuery(rawQuery)\n\treqParts := strings.Split(clearPath, \"/\")\n\n\tfor _, route := range r.routes {\n\t\tpatParts := strings.Split(route.Pattern, \"/\")\n\t\twildcard := false\n\t\tfor _, part := range patParts {\n\t\t\tif part == \"*\" {\n\t\t\t\twildcard = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tif !wildcard \u0026\u0026 len(patParts) != len(reqParts) {\n\t\t\tcontinue\n\t\t}\n\n\t\tmatch := true\n\t\tfor i := 0; i \u003c len(patParts); i++ {\n\t\t\tpatPart := patParts[i]\n\t\t\treqPart := reqParts[i]\n\n\t\t\tif patPart == \"*\" {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tif strings.HasPrefix(patPart, \"{\") \u0026\u0026 strings.HasSuffix(patPart, \"}\") {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif patPart != reqPart {\n\t\t\t\tmatch = false\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tif match {\n\t\t\treq := \u0026Request{\n\t\t\t\tPath:        clearPath,\n\t\t\t\tRawPath:     reqPath,\n\t\t\t\tHandlerPath: route.Pattern,\n\t\t\t\tQuery:       query,\n\t\t\t}\n\t\t\tres := \u0026ResponseWriter{}\n\t\t\tif route.Fn == nil {\n\t\t\t\tpanic(\"Router.Render: route \" + route.Pattern + \" was registered via HandleFuncRlm; use RenderRlm to dispatch\")\n\t\t\t}\n\t\t\troute.Fn(res, req)\n\t\t\treturn res.Output()\n\t\t}\n\t}\n\n\t// not found\n\treq := \u0026Request{Path: reqPath, Query: query}\n\tres := \u0026ResponseWriter{}\n\tr.NotFoundHandler(res, req)\n\treturn res.Output()\n}\n\n// RenderRlm is the rlm-aware counterpart of Render. Dispatches matched\n// routes registered via HandleFuncRlm with the supplied rlm; routes\n// registered via the legacy HandleFunc still work — rlm is ignored.\n// Use this when the router carries any rlm-aware handlers.\nfunc (r *Router) RenderRlm(_ int, rlm realm, reqPath string) string {\n\tclearPath, rawQuery, _ := strings.Cut(reqPath, \"?\")\n\tquery, _ := url.ParseQuery(rawQuery)\n\treqParts := strings.Split(clearPath, \"/\")\n\n\tfor _, route := range r.routes {\n\t\tpatParts := strings.Split(route.Pattern, \"/\")\n\t\twildcard := false\n\t\tfor _, part := range patParts {\n\t\t\tif part == \"*\" {\n\t\t\t\twildcard = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tif !wildcard \u0026\u0026 len(patParts) != len(reqParts) {\n\t\t\tcontinue\n\t\t}\n\n\t\tmatch := true\n\t\tfor i := 0; i \u003c len(patParts); i++ {\n\t\t\tpatPart := patParts[i]\n\t\t\treqPart := reqParts[i]\n\n\t\t\tif patPart == \"*\" {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tif strings.HasPrefix(patPart, \"{\") \u0026\u0026 strings.HasSuffix(patPart, \"}\") {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif patPart != reqPart {\n\t\t\t\tmatch = false\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tif match {\n\t\t\treq := \u0026Request{\n\t\t\t\tPath:        clearPath,\n\t\t\t\tRawPath:     reqPath,\n\t\t\t\tHandlerPath: route.Pattern,\n\t\t\t\tQuery:       query,\n\t\t\t}\n\t\t\tres := \u0026ResponseWriter{}\n\t\t\tif route.FnRlm != nil {\n\t\t\t\troute.FnRlm(0, rlm, res, req)\n\t\t\t} else {\n\t\t\t\troute.Fn(res, req)\n\t\t\t}\n\t\t\treturn res.Output()\n\t\t}\n\t}\n\n\t// not found\n\treq := \u0026Request{Path: reqPath, Query: query}\n\tres := \u0026ResponseWriter{}\n\tr.NotFoundHandler(res, req)\n\treturn res.Output()\n}\n\n// HandleFunc registers a route and its handler function.\nfunc (r *Router) HandleFunc(pattern string, fn HandlerFunc) {\n\troute := Handler{Pattern: pattern, Fn: fn}\n\tr.routes = append(r.routes, route)\n}\n\n// HandleFuncRlm registers a route with a rlm-aware handler. Dispatch\n// must use Router.RenderRlm — calling Router.Render on a route registered\n// via HandleFuncRlm panics (no rlm to supply).\nfunc (r *Router) HandleFuncRlm(pattern string, fn HandlerFuncRlm) {\n\troute := Handler{Pattern: pattern, FnRlm: fn}\n\tr.routes = append(r.routes, route)\n}\n\n// HandleErrFunc registers a route and its error handler function.\nfunc (r *Router) HandleErrFunc(pattern string, fn ErrHandlerFunc) {\n\t// Convert ErrHandlerFunc to regular HandlerFunc\n\thandler := func(res *ResponseWriter, req *Request) {\n\t\tif err := fn(res, req); err != nil {\n\t\t\tres.Write(\"Error: \" + err.Error())\n\t\t}\n\t}\n\n\tr.HandleFunc(pattern, handler)\n}\n\n// SetNotFoundHandler sets custom message for 404 defaultNotFoundHandler.\nfunc (r *Router) SetNotFoundHandler(handler NotFoundHandler) {\n\tr.NotFoundHandler = handler\n}\n"},{"name":"router_test.gno","body":"package mux\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestRouter_Render(t *testing.T) {\n\tcases := []struct {\n\t\tlabel          string\n\t\tpath           string\n\t\texpectedOutput string\n\t\tsetupHandler   func(t *testing.T, r *Router)\n\t}{\n\t\t{\n\t\t\tlabel:          \"route with named parameter\",\n\t\t\tpath:           \"hello/Alice\",\n\t\t\texpectedOutput: \"Hello, Alice!\",\n\t\t\tsetupHandler: func(t *testing.T, r *Router) {\n\t\t\t\tr.HandleFunc(\"hello/{name}\", func(rw *ResponseWriter, req *Request) {\n\t\t\t\t\tname := req.GetVar(\"name\")\n\t\t\t\t\tuassert.Equal(t, \"Alice\", name)\n\t\t\t\t\trw.Write(\"Hello, \" + name + \"!\")\n\t\t\t\t})\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tlabel:          \"static route\",\n\t\t\tpath:           \"hi\",\n\t\t\texpectedOutput: \"Hi, earth!\",\n\t\t\tsetupHandler: func(t *testing.T, r *Router) {\n\t\t\t\tr.HandleFunc(\"hi\", func(rw *ResponseWriter, req *Request) {\n\t\t\t\t\tuassert.Equal(t, req.Path, \"hi\")\n\t\t\t\t\trw.Write(\"Hi, earth!\")\n\t\t\t\t})\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tlabel:          \"route with named parameter and query string\",\n\t\t\tpath:           \"hello/foo/bar?foo=bar\u0026baz\",\n\t\t\texpectedOutput: \"foo bar\",\n\t\t\tsetupHandler: func(t *testing.T, r *Router) {\n\t\t\t\tr.HandleFunc(\"hello/{key}/{val}\", func(rw *ResponseWriter, req *Request) {\n\t\t\t\t\tkey := req.GetVar(\"key\")\n\t\t\t\t\tval := req.GetVar(\"val\")\n\t\t\t\t\tuassert.Equal(t, \"foo\", key)\n\t\t\t\t\tuassert.Equal(t, \"bar\", val)\n\t\t\t\t\tuassert.Equal(t, \"hello/foo/bar?foo=bar\u0026baz\", req.RawPath)\n\t\t\t\t\tuassert.Equal(t, \"hello/foo/bar\", req.Path)\n\t\t\t\t\tuassert.Equal(t, \"bar\", req.Query.Get(\"foo\"))\n\t\t\t\t\tuassert.Empty(t, req.Query.Get(\"baz\"))\n\t\t\t\t\trw.Write(key + \" \" + val)\n\t\t\t\t})\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\t// TODO: finalize how router should behave with double slash in path.\n\t\t\tlabel:          \"double slash in nested route\",\n\t\t\tpath:           \"a/foo//\",\n\t\t\texpectedOutput: \"test foo\",\n\t\t\tsetupHandler: func(t *testing.T, r *Router) {\n\t\t\t\tr.HandleFunc(\"a/{key}\", func(rw *ResponseWriter, req *Request) {\n\t\t\t\t\t// Assert not called\n\t\t\t\t\tuassert.False(t, true, \"unexpected handler called\")\n\t\t\t\t})\n\n\t\t\t\tr.HandleFunc(\"a/{key}/{val}/\", func(rw *ResponseWriter, req *Request) {\n\t\t\t\t\tkey := req.GetVar(\"key\")\n\t\t\t\t\tval := req.GetVar(\"val\")\n\t\t\t\t\tuassert.Equal(t, key, \"foo\")\n\t\t\t\t\tuassert.Empty(t, val)\n\t\t\t\t\trw.Write(\"test \" + key)\n\t\t\t\t})\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tlabel:          \"wildcard in route\",\n\t\t\tpath:           \"hello/Alice/Bob\",\n\t\t\texpectedOutput: \"Matched: Alice/Bob\",\n\t\t\tsetupHandler: func(t *testing.T, r *Router) {\n\t\t\t\tr.HandleFunc(\"hello/*\", func(rw *ResponseWriter, req *Request) {\n\t\t\t\t\tpath := req.GetVar(\"*\")\n\t\t\t\t\tuassert.Equal(t, \"Alice/Bob\", path)\n\t\t\t\t\tuassert.Equal(t, \"hello/Alice/Bob\", req.Path)\n\t\t\t\t\trw.Write(\"Matched: \" + path)\n\t\t\t\t})\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tlabel:          \"wildcard in route with query string\",\n\t\t\tpath:           \"hello/Alice/Bob?foo=bar\",\n\t\t\texpectedOutput: \"Matched: Alice/Bob\",\n\t\t\tsetupHandler: func(t *testing.T, r *Router) {\n\t\t\t\tr.HandleFunc(\"hello/*\", func(rw *ResponseWriter, req *Request) {\n\t\t\t\t\tpath := req.GetVar(\"*\")\n\t\t\t\t\tuassert.Equal(t, \"Alice/Bob\", path)\n\t\t\t\t\tuassert.Equal(t, \"hello/Alice/Bob?foo=bar\", req.RawPath)\n\t\t\t\t\tuassert.Equal(t, \"hello/Alice/Bob\", req.Path)\n\t\t\t\t\tuassert.Equal(t, \"bar\", req.Query.Get(\"foo\"))\n\t\t\t\t\trw.Write(\"Matched: \" + path)\n\t\t\t\t})\n\t\t\t},\n\t\t},\n\t\t// TODO: {\"hello\", \"Hello, world!\"},\n\t\t// TODO: hello/, /hello, hello//Alice, hello/Alice/, hello/Alice/Bob, etc\n\t}\n\tfor _, tt := range cases {\n\t\tt.Run(tt.label, func(t *testing.T) {\n\t\t\trouter := NewRouter()\n\t\t\ttt.setupHandler(t, router)\n\t\t\toutput := router.Render(tt.path)\n\t\t\tif output != tt.expectedOutput {\n\t\t\t\tt.Errorf(\"Expected output %q, but got %q\", tt.expectedOutput, output)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"QAWmnGxomRvtI9yhvNQIFpo+tKshXxTt7l+KkpjUX+9vOf6qaNy/lMKxeh53ll31tLVIqhwwTNzGk8zO6QwlBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ownable","path":"gno.land/p/nt/ownable/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# ownable\n\nPackage `ownable` provides an ownership pattern for Gno realms, allowing contracts to restrict access to privileged operations to a designated owner.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package ownable provides an ownership pattern for Gno realms, allowing\n// contracts to restrict access to privileged operations to a designated owner.\npackage ownable\n"},{"name":"errors.gno","body":"package ownable\n\nimport \"errors\"\n\nvar (\n\tErrUnauthorized   = errors.New(\"ownable: caller is not owner\")\n\tErrInvalidAddress = errors.New(\"ownable: new owner address is invalid\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/ownable/v0\"\ngno = \"0.9\"\n"},{"name":"ownable.gno","body":"package ownable\n\nimport \"chain\"\n\nconst OwnershipTransferEvent = \"OwnershipTransfer\"\n\n// Ownable is meant to be used as a top-level object to make your contract\n// ownable OR being embedded in a Gno object to manage per-object ownership.\n// Ownable is safe to export as a top-level object.\n//\n// Authority-mutating methods (TransferOwnership, DropOwnership) take\n// (_ int, rlm realm). The caller threads its own cur; the method\n// asserts rlm.IsCurrent() and identifies the principal as\n// rlm.Previous().Address() — which must equal the current owner.\n//\n//\to.TransferOwnership(0, cur, newOwner)\n//\to.DropOwnership(0, cur)\n//\n// Read methods (OwnedBy, AssertOwnedBy) keep the bare-address shape;\n// callers extract the address themselves (e.g. cur.Previous().Address()).\ntype Ownable struct {\n\towner address\n}\n\n// NewWithAddress creates an Ownable with the given address as owner.\n// This is the only constructor — the previous New/NewWithOrigin/\n// NewWithAddressByPrevious sugar baked runtime walks and an auth-mode\n// flag into the struct; the realm using this package now picks the\n// owner address explicitly (e.g. cur.Previous().Address() after\n// verifying cur.Previous().IsUserCall() in init).\nfunc NewWithAddress(addr address) *Ownable {\n\treturn \u0026Ownable{\n\t\towner: addr,\n\t}\n}\n\n// OwnedBy reports whether addr is the current owner.\nfunc (o *Ownable) OwnedBy(addr address) bool {\n\tif o == nil {\n\t\treturn false\n\t}\n\treturn addr == o.owner\n}\n\n// AssertOwnedBy panics with ErrUnauthorized if addr is not the owner.\nfunc (o *Ownable) AssertOwnedBy(addr address) {\n\tif !o.OwnedBy(addr) {\n\t\tpanic(ErrUnauthorized)\n\t}\n}\n\n// TransferOwnership transfers ownership of the Ownable to newOwner. rlm\n// must be the caller's own captured cur (asserted via rlm.IsCurrent()).\n// The principal is rlm.Previous().Address() — the realm that crossed\n// into the caller — which must equal the current owner.\n//\n// IsCurrent + rlm.Previous() makes the principal unforgeable: an\n// attacker calling TransferOwnership on a foreign Ownable cannot supply\n// an arbitrary caller address; rlm comes from a runtime-validated\n// crossing frame.\nfunc (o *Ownable) TransferOwnership(_ int, rlm realm, newOwner address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrUnauthorized\n\t}\n\tcaller := rlm.Previous().Address()\n\tif !o.OwnedBy(caller) {\n\t\treturn ErrUnauthorized\n\t}\n\tif !newOwner.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tprevOwner := o.owner\n\to.owner = newOwner\n\tchain.Emit(\n\t\tOwnershipTransferEvent,\n\t\t\"from\", prevOwner.String(),\n\t\t\"to\", newOwner.String(),\n\t)\n\treturn nil\n}\n\n// DropOwnership removes the owner, disabling any owner-related actions.\n// rlm must be the caller's own captured cur; rlm.Previous().Address()\n// must equal the current owner.\nfunc (o *Ownable) DropOwnership(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrUnauthorized\n\t}\n\tcaller := rlm.Previous().Address()\n\tif !o.OwnedBy(caller) {\n\t\treturn ErrUnauthorized\n\t}\n\tprevOwner := o.owner\n\to.owner = \"\"\n\tchain.Emit(\n\t\tOwnershipTransferEvent,\n\t\t\"from\", prevOwner.String(),\n\t\t\"to\", \"\",\n\t)\n\treturn nil\n}\n\n// Owner returns the owner address.\nfunc (o *Ownable) Owner() address {\n\tif o == nil {\n\t\treturn address(\"\")\n\t}\n\treturn o.owner\n}\n"},{"name":"ownable_test.gno","body":"package ownable\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nvar (\n\talice = testutils.TestAddress(\"alice\")\n\tbob   = testutils.TestAddress(\"bob\")\n)\n\nfunc TestNewWithAddress(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\n\tgot := o.Owner()\n\tuassert.Equal(t, got, alice)\n}\n\nfunc TestTransferOwnership(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.TransferOwnership(0, cur, bob)\n\t\turequire.NoError(t, err)\n\t}(cross(cur))\n\n\tgot := o.Owner()\n\tuassert.Equal(t, got, bob)\n}\n\nfunc TestTransferOwnershipUnauthorized(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) {\n\t\tuassert.ErrorContains(t, o.TransferOwnership(0, cur, bob), ErrUnauthorized.Error())\n\t}(cross(cur))\n}\n\nfunc TestDropOwnership(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.DropOwnership(0, cur)\n\t\turequire.NoError(t, err, \"DropOwnership failed\")\n\t}(cross(cur))\n\n\towner := o.Owner()\n\tuassert.Empty(t, owner, \"owner should be empty\")\n}\n\nfunc TestDropOwnershipUnauthorized(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) {\n\t\tuassert.ErrorContains(t, o.DropOwnership(0, cur), ErrUnauthorized.Error())\n\t}(cross(cur))\n}\n\nfunc TestOwnedBy(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\tuassert.True(t, o.OwnedBy(alice))\n\tuassert.False(t, o.OwnedBy(bob))\n}\n\nfunc TestAssertOwnedBy(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\n\t// Should not panic.\n\to.AssertOwnedBy(alice)\n\n\tuassert.PanicsWithMessage(t, cur, ErrUnauthorized.Error(), func() {\n\t\to.AssertOwnedBy(bob)\n\t})\n}\n\nfunc TestErrInvalidAddress(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.TransferOwnership(0, cur, \"\")\n\t\tuassert.ErrorContains(t, err, ErrInvalidAddress.Error())\n\n\t\terr = o.TransferOwnership(0, cur, \"10000000001000000000100000000010000000001000000000\")\n\t\tuassert.ErrorContains(t, err, ErrInvalidAddress.Error())\n\t}(cross(cur))\n}\n\nfunc TestNilReceiver(cur realm, t *testing.T) {\n\tvar o *Ownable\n\n\towner := o.Owner()\n\tif owner != address(\"\") {\n\t\tt.Errorf(\"expected empty address but got %v\", owner)\n\t}\n\n\tisOwner := o.OwnedBy(alice)\n\tuassert.False(t, isOwner)\n\n\tdefer func() {\n\t\tr := recover()\n\t\tif r == nil {\n\t\t\tt.Error(\"expected panic but got none\")\n\t\t}\n\t\tif r != ErrUnauthorized {\n\t\t\tt.Errorf(\"expected ErrUnauthorized but got %v\", r)\n\t\t}\n\t}()\n\to.AssertOwnedBy(alice)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"eAS3KmQ1Iim3x9ZDHxiicoymxpQI5TNBJc+ICHe+L8nmAcBzatKSjcmnh9KCqkv/Okt5pkFxy8KzUFuBV+NwBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"fqname","path":"gno.land/p/nt/fqname/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# fqname\n\nPackage `fqname` provides utilities for handling fully qualified identifiers in Gno, typically a package path followed by a dot and a symbol name.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package fqname provides utilities for handling fully qualified identifiers\n// in Gno, typically a package path followed by a dot and a symbol name.\npackage fqname\n"},{"name":"fqname.gno","body":"// Package fqname provides utilities for handling fully qualified identifiers in\n// Gno. A fully qualified identifier typically includes a package path followed\n// by a dot (.) and then the name of a variable, function, type, or other\n// package-level declaration.\npackage fqname\n\nimport (\n\t\"strings\"\n)\n\n// Parse splits a fully qualified identifier into its package path and name\n// components. It handles cases with and without slashes in the package path.\n//\n//\tpkgpath, name := fqname.Parse(\"gno.land/p/nt/avl/v0.Tree\")\n//\tufmt.Sprintf(\"Package: %s, Name: %s\\n\", id.Package, id.Name)\n//\t// Output: Package: gno.land/p/nt/avl/v0, Name: Tree\nfunc Parse(fqname string) (pkgpath, name string) {\n\t// Find the index of the last slash.\n\tlastSlashIndex := strings.LastIndex(fqname, \"/\")\n\tif lastSlashIndex == -1 {\n\t\t// No slash found, handle it as a simple package name with dot notation.\n\t\tdotIndex := strings.LastIndex(fqname, \".\")\n\t\tif dotIndex == -1 {\n\t\t\treturn fqname, \"\"\n\t\t}\n\t\treturn fqname[:dotIndex], fqname[dotIndex+1:]\n\t}\n\n\t// Get the part after the last slash.\n\tafterSlash := fqname[lastSlashIndex+1:]\n\n\t// Check for a dot in the substring after the last slash.\n\tdotIndex := strings.Index(afterSlash, \".\")\n\tif dotIndex == -1 {\n\t\t// No dot found after the last slash\n\t\treturn fqname, \"\"\n\t}\n\n\t// Split at the dot to separate the base and the suffix.\n\tbase := fqname[:lastSlashIndex+1+dotIndex]\n\tsuffix := afterSlash[dotIndex+1:]\n\n\treturn base, suffix\n}\n\n// Construct a qualified identifier.\n//\n//\tfqName := fqname.Construct(\"gno.land/r/demo/foo20\", \"Token\")\n//\tfmt.Println(\"Fully Qualified Name:\", fqName)\n//\t// Output: gno.land/r/demo/foo20.Token\nfunc Construct(pkgpath, name string) string {\n\t// TODO: ensure pkgpath is valid - and as such last part does not contain a dot.\n\tif name == \"\" {\n\t\treturn pkgpath\n\t}\n\treturn pkgpath + \".\" + name\n}\n\n// RenderLink creates a formatted link for a fully qualified identifier.\n// If the package path starts with \"gno.land\", it converts it to a markdown link.\n// If the domain is different or missing, it returns the input as is.\nfunc RenderLink(pkgPath, slug string) string {\n\tif strings.HasPrefix(pkgPath, \"gno.land\") {\n\t\tpkgLink := strings.TrimPrefix(pkgPath, \"gno.land\")\n\t\tif slug != \"\" {\n\t\t\tsafeSlug := escapeMarkdown(slug)\n\t\t\treturn \"[\" + pkgPath + \"](\" + pkgLink + \").\" + safeSlug\n\t\t}\n\n\t\treturn \"[\" + pkgPath + \"](\" + pkgLink + \")\"\n\t}\n\n\tif slug != \"\" {\n\t\tsafeSlug := escapeMarkdown(slug)\n\t\treturn pkgPath + \".\" + safeSlug\n\t}\n\n\treturn pkgPath\n}\n\n// escapeMarkdown escapes characters that could break markdown link syntax.\nfunc escapeMarkdown(s string) string {\n\tr := strings.NewReplacer(\n\t\t\"[\", `\\[`,\n\t\t\"]\", `\\]`,\n\t\t\"(\", `\\(`,\n\t\t\")\", `\\)`,\n\t)\n\treturn r.Replace(s)\n}\n"},{"name":"fqname_test.gno","body":"package fqname\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestParse(t *testing.T) {\n\ttests := []struct {\n\t\tinput           string\n\t\texpectedPkgPath string\n\t\texpectedName    string\n\t}{\n\t\t{\"gno.land/p/nt/avl/v0.Tree\", \"gno.land/p/nt/avl/v0\", \"Tree\"},\n\t\t{\"gno.land/p/nt/avl/v0\", \"gno.land/p/nt/avl/v0\", \"\"},\n\t\t{\"gno.land/p/nt/avl/v0.Tree.Node\", \"gno.land/p/nt/avl/v0\", \"Tree.Node\"},\n\t\t{\"gno.land/p/nt/avl/v0/nested.Package.Func\", \"gno.land/p/nt/avl/v0/nested\", \"Package.Func\"},\n\t\t{\"path/filepath.Split\", \"path/filepath\", \"Split\"},\n\t\t{\"path.Split\", \"path\", \"Split\"},\n\t\t{\"path/filepath\", \"path/filepath\", \"\"},\n\t\t{\"path\", \"path\", \"\"},\n\t\t{\"\", \"\", \"\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpkgpath, name := Parse(tt.input)\n\t\tuassert.Equal(t, tt.expectedPkgPath, pkgpath, \"Package path did not match\")\n\t\tuassert.Equal(t, tt.expectedName, name, \"Name did not match\")\n\t}\n}\n\nfunc TestConstruct(t *testing.T) {\n\ttests := []struct {\n\t\tpkgpath  string\n\t\tname     string\n\t\texpected string\n\t}{\n\t\t{\"gno.land/r/demo/foo20\", \"Token\", \"gno.land/r/demo/foo20.Token\"},\n\t\t{\"gno.land/r/demo/foo20\", \"\", \"gno.land/r/demo/foo20\"},\n\t\t{\"path\", \"\", \"path\"},\n\t\t{\"path\", \"Split\", \"path.Split\"},\n\t\t{\"path/filepath\", \"\", \"path/filepath\"},\n\t\t{\"path/filepath\", \"Split\", \"path/filepath.Split\"},\n\t\t{\"\", \"JustName\", \".JustName\"},\n\t\t{\"\", \"\", \"\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tresult := Construct(tt.pkgpath, tt.name)\n\t\tuassert.Equal(t, tt.expected, result, \"Constructed FQName did not match expected\")\n\t}\n}\n\nfunc TestRenderLink(t *testing.T) {\n\ttests := []struct {\n\t\tpkgPath  string\n\t\tslug     string\n\t\texpected string\n\t}{\n\t\t{\"gno.land/p/nt/avl/v0\", \"Tree\", \"[gno.land/p/nt/avl/v0](/p/nt/avl/v0).Tree\"},\n\t\t{\"gno.land/p/nt/avl/v0\", \"\", \"[gno.land/p/nt/avl/v0](/p/nt/avl/v0)\"},\n\t\t{\"github.com/a/b\", \"C\", \"github.com/a/b.C\"},\n\t\t{\"example.com/pkg\", \"Func\", \"example.com/pkg.Func\"},\n\t\t{\"gno.land/r/demo/foo20\", \"Token\", \"[gno.land/r/demo/foo20](/r/demo/foo20).Token\"},\n\t\t{\"gno.land/r/demo/foo20\", \"\", \"[gno.land/r/demo/foo20](/r/demo/foo20)\"},\n\t\t{\"\", \"\", \"\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tresult := RenderLink(tt.pkgPath, tt.slug)\n\t\tuassert.Equal(t, tt.expected, result, \"Rendered link did not match expected\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/fqname/v0\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"B3DfUD0vTSJs12ATg7mgBtwDmOE7eULI63WgJDMa/IZPt6zQq8yuSkcDeZ6G46TtdbAeEz37nA2LkutAVxQUBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"grc20reg","path":"gno.land/r/demo/defi/grc20reg","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/defi/grc20reg\"\ngno = \"0.9\"\n"},{"name":"grc20reg.gno","body":"package grc20reg\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/rotree\"\n\t\"gno.land/p/nt/fqname/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar registry = avl.NewTree() // rlmPath[.slug] -\u003e *Token (slug is optional)\n\n// Construction lives in grc20.NewToken — it now takes (_ int, rlm realm)\n// and binds origRealm from rlm.PkgPath() under an IsCurrent assertion.\n// Use this package's Register to make a token discoverable:\n//\n//\tToken, ledger := grc20.NewToken(0, cur, name, symbol, decimals)\n//\tgrc20reg.Register(cross, Token, \"\")\n\nfunc Register(cur realm, token *grc20.Token, slug string) {\n\tif slug != \"\" {\n\t\tvalidateSlug(slug)\n\t}\n\trlmPath := cur.Previous().PkgPath()\n\tkey := fqname.Construct(rlmPath, slug)\n\tregistry.Set(key, token)\n\tchain.Emit(\n\t\tregisterEvent,\n\t\t\"pkgpath\", rlmPath,\n\t\t\"slug\", slug,\n\t)\n}\n\nfunc Get(key string) *grc20.Token {\n\ttoken := registry.Get(key)\n\tif token == nil {\n\t\treturn nil\n\t}\n\treturn token.(*grc20.Token)\n}\n\nfunc MustGet(key string) *grc20.Token {\n\ttoken := Get(key)\n\tif token == nil {\n\t\tpanic(\"unknown token: \" + key)\n\t}\n\treturn token\n}\n\nfunc Render(path string) string {\n\tswitch {\n\tcase path == \"\": // home\n\t\t// TODO: add pagination\n\t\ts := \"\"\n\t\tcount := 0\n\t\tregistry.Iterate(\"\", \"\", func(key string, tokenI any) bool {\n\t\t\tcount++\n\t\t\ttoken := tokenI.(*grc20.Token)\n\t\t\trlmPath, slug := fqname.Parse(key)\n\t\t\trlmLink := fqname.RenderLink(rlmPath, slug)\n\t\t\tinfoLink := \"/r/demo/grc20reg:\" + key\n\t\t\ts += \"- \" + md.Bold(md.EscapeText(token.GetName())) + \" - \" + rlmLink + \" - \" + md.Link(\"info\", infoLink) + \"\\n\"\n\t\t\treturn false\n\t\t})\n\t\tif count == 0 {\n\t\t\treturn \"No registered token.\"\n\t\t}\n\t\treturn s\n\tdefault: // specific token\n\t\tkey := path\n\t\ttoken := MustGet(key)\n\t\trlmPath, slug := fqname.Parse(key)\n\t\trlmLink := fqname.RenderLink(rlmPath, slug)\n\t\ts := ufmt.Sprintf(\"# %s\\n\", md.EscapeText(token.GetName()))\n\t\ts += \"- symbol: \" + md.Bold(md.EscapeText(token.GetSymbol())) + \"\\n\"\n\t\ts += ufmt.Sprintf(\"- realm: %s\\n\", rlmLink)\n\t\ts += ufmt.Sprintf(\"- decimals: %d\\n\", token.GetDecimals())\n\t\ts += ufmt.Sprintf(\"- total supply: %d\\n\", token.TotalSupply())\n\t\treturn s\n\t}\n}\n\nconst (\n\tregisterEvent = \"register\"\n\tmaxSlugLen    = 128\n)\n\nfunc GetRegistry() *rotree.ReadOnlyTree {\n\treturn rotree.Wrap(registry, nil)\n}\n\n// validateSlug panics if the slug is too long or contains non-alphanumeric characters.\n// Only letters, digits, dashes, and underscores are allowed.\nfunc validateSlug(slug string) {\n\tif len(slug) \u003e maxSlugLen {\n\t\tpanic(\"grc20reg: slug too long\")\n\t}\n\tfor _, c := range slug {\n\t\tif !isAlphanumeric(c) \u0026\u0026 c != '_' \u0026\u0026 c != '-' {\n\t\t\tpanic(\"grc20reg: invalid slug character: \" + string(c))\n\t\t}\n\t}\n}\n\nfunc isAlphanumeric(c rune) bool {\n\treturn (c \u003e= 'a' \u0026\u0026 c \u003c= 'z') || (c \u003e= 'A' \u0026\u0026 c \u003c= 'Z') || (c \u003e= '0' \u0026\u0026 c \u003c= '9')\n}\n"},{"name":"grc20reg_test.gno","body":"package grc20reg\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestRegistry(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/foo\"))\n\trealmAddr := cur.PkgPath()\n\ttoken, ledger := grc20.NewToken(0, cur, \"TestToken\", \"TST\", 4)\n\tledger.Mint(cur.Address(), 1234567)\n\t// register\n\tRegister(cross(cur), token, \"\")\n\tregToken := Get(realmAddr)\n\turequire.True(t, regToken != nil, \"expected to find a token\") // fixme: use urequire.NotNil\n\turequire.Equal(t, regToken.GetSymbol(), \"TST\")\n\n\texpected := `- **TestToken** - [gno.land/r/demo/foo](/r/demo/foo) - [info](/r/demo/grc20reg:gno.land/r/demo/foo)\n`\n\tgot := Render(\"\")\n\turequire.True(t, strings.Contains(got, expected))\n\t// 404\n\tinvalidToken := Get(\"0xdeadbeef\")\n\turequire.True(t, invalidToken == nil)\n\n\t// register with a slug\n\tRegister(cross(cur), token, \"mySlug\")\n\tregToken = Get(realmAddr + \".mySlug\")\n\turequire.True(t, regToken != nil, \"expected to find a token\") // fixme: use urequire.NotNil\n\turequire.Equal(t, regToken.GetSymbol(), \"TST\")\n\n\t// override\n\tRegister(cross(cur), token, \"\")\n\tregToken = Get(realmAddr + \"\")\n\turequire.True(t, regToken != nil, \"expected to find a token\") // fixme: use urequire.NotNil\n\turequire.Equal(t, regToken.GetSymbol(), \"TST\")\n\n\tgot = Render(\"\")\n\turequire.True(t, strings.Contains(got, `- **TestToken** - [gno.land/r/demo/foo](/r/demo/foo) - [info](/r/demo/grc20reg:gno.land/r/demo/foo)`))\n\turequire.True(t, strings.Contains(got, `- **TestToken** - [gno.land/r/demo/foo](/r/demo/foo).mySlug - [info](/r/demo/grc20reg:gno.land/r/demo/foo.mySlug)`))\n\n\texpected = `# TestToken\n- symbol: **TST**\n- realm: [gno.land/r/demo/foo](/r/demo/foo).mySlug\n- decimals: 4\n- total supply: 1234567\n`\n\tgot = Render(\"gno.land/r/demo/foo.mySlug\")\n\turequire.Equal(t, expected, got)\n}\n\nfunc TestValidateSlug(cur realm, t *testing.T) {\n\t// Valid slugs — should not panic\n\tvalid := []string{\"mytoken\", \"my-token\", \"my_token\", \"Token123\", \"a\", \"A-B_c\", strings.Repeat(\"a\", maxSlugLen)}\n\tfor _, slug := range valid {\n\t\tvalidateSlug(slug) // no panic = pass\n\t}\n}\n\nfunc TestValidateSlugPanicsOnTooLong(cur realm, t *testing.T) {\n\tdefer func() { recover() }()\n\tvalidateSlug(strings.Repeat(\"a\", maxSlugLen+1))\n\tt.Errorf(\"should have panicked\")\n}\n\nfunc TestValidateSlugPanicsOnSpace(cur realm, t *testing.T) {\n\tdefer func() { recover() }()\n\tvalidateSlug(\"has space\")\n\tt.Errorf(\"should have panicked\")\n}\n\nfunc TestValidateSlugPanicsOnDot(cur realm, t *testing.T) {\n\tdefer func() { recover() }()\n\tvalidateSlug(\"has.dot\")\n\tt.Errorf(\"should have panicked\")\n}\n\nfunc TestValidateSlugPanicsOnSlash(cur realm, t *testing.T) {\n\tdefer func() { recover() }()\n\tvalidateSlug(\"has/slash\")\n\tt.Errorf(\"should have panicked\")\n}\n\nfunc TestValidateSlugPanicsOnBrackets(cur realm, t *testing.T) {\n\tdefer func() { recover() }()\n\tvalidateSlug(\"[brackets]\")\n\tt.Errorf(\"should have panicked\")\n}\n\nfunc TestValidateSlugPanicsOnParens(cur realm, t *testing.T) {\n\tdefer func() { recover() }()\n\tvalidateSlug(\"(parens)\")\n\tt.Errorf(\"should have panicked\")\n}\n\nfunc TestValidateSlugPanicsOnInjection(cur realm, t *testing.T) {\n\tdefer func() { recover() }()\n\tvalidateSlug(`) [Claim](https://evil.com`)\n\tt.Errorf(\"should have panicked\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"cn8tsSzPP7o/gYM+4Hl02lQNcvnpuqGqz/FKskTxOgeiMI/Vfn1dAUJC6Q4+eCSrjFwRrbcpwW5WHPKEP7RtBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"grc20factory","path":"gno.land/r/demo/defi/grc20factory","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/defi/grc20factory\"\ngno = \"0.9\"\n"},{"name":"grc20factory.gno","body":"package grc20factory\n\nimport (\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0\"\n\tp \"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/avl/v0/rotree\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n)\n\nvar (\n\tinstances avl.Tree // symbol -\u003e *instance\n\tpager     = p.NewPager(rotree.Wrap(\u0026instances, nil), 20, false)\n)\n\ntype instance struct {\n\ttoken  *grc20.Token\n\tledger *grc20.PrivateLedger\n\tadmin  *ownable.Ownable\n\tfaucet int64 // per-request amount. disabled if 0.\n}\n\nfunc New(cur realm, name, symbol string, decimals int, initialMint, faucet int64) {\n\tcaller := cur.Previous().Address()\n\tNewWithAdmin(cur, name, symbol, decimals, initialMint, faucet, caller)\n}\n\nfunc NewWithAdmin(cur realm, name, symbol string, decimals int, initialMint, faucet int64, admin address) {\n\texists := instances.Has(symbol)\n\tif exists {\n\t\tpanic(\"token already exists\")\n\t}\n\n\ttoken, ledger := grc20.NewToken(0, cur, name, symbol, decimals)\n\tif initialMint \u003e 0 {\n\t\tledger.Mint(admin, initialMint)\n\t}\n\n\tinst := instance{\n\t\ttoken:  token,\n\t\tledger: ledger,\n\t\tadmin:  ownable.NewWithAddress(admin),\n\t\tfaucet: faucet,\n\t}\n\tinstances.Set(symbol, \u0026inst)\n\n\tgrc20reg.Register(cross(cur), token, symbol)\n}\n\nfunc Bank(symbol string) *grc20.Token {\n\tinst := mustGetInstance(symbol)\n\treturn inst.token\n}\n\nfunc TotalSupply(symbol string) int64 {\n\tinst := mustGetInstance(symbol)\n\treturn inst.token.ReadonlyTeller().TotalSupply()\n}\n\nfunc HasAddr(symbol string, owner address) bool {\n\tinst := mustGetInstance(symbol)\n\treturn inst.token.HasAddr(owner)\n}\n\nfunc BalanceOf(symbol string, owner address) int64 {\n\tinst := mustGetInstance(symbol)\n\treturn inst.token.ReadonlyTeller().BalanceOf(owner)\n}\n\nfunc Allowance(symbol string, owner, spender address) int64 {\n\tinst := mustGetInstance(symbol)\n\treturn inst.token.ReadonlyTeller().Allowance(owner, spender)\n}\n\nfunc Transfer(cur realm, symbol string, to address, amount int64) {\n\tinst := mustGetInstance(symbol)\n\tcaller := cur.Previous().Address()\n\tteller := inst.ledger.ImpersonateTeller(caller)\n\tcheckErr(teller.Transfer(0, cur, to, amount))\n}\n\nfunc Approve(cur realm, symbol string, spender address, amount int64) {\n\tinst := mustGetInstance(symbol)\n\tcaller := cur.Previous().Address()\n\tteller := inst.ledger.ImpersonateTeller(caller)\n\tcheckErr(teller.Approve(0, cur, spender, amount))\n}\n\nfunc TransferFrom(cur realm, symbol string, from, to address, amount int64) {\n\tinst := mustGetInstance(symbol)\n\tcaller := cur.Previous().Address()\n\tteller := inst.ledger.ImpersonateTeller(caller)\n\tcheckErr(teller.TransferFrom(0, cur, from, to, amount))\n}\n\n// faucet.\nfunc Faucet(cur realm, symbol string) {\n\tinst := mustGetInstance(symbol)\n\tif inst.faucet == 0 {\n\t\tpanic(\"faucet disabled for this token\")\n\t}\n\t// FIXME: add limits?\n\t// FIXME: add payment in gnot?\n\tcaller := cur.Previous().Address()\n\tcheckErr(inst.ledger.Mint(caller, inst.faucet))\n}\n\nfunc Mint(cur realm, symbol string, to address, amount int64) {\n\tinst := mustGetInstance(symbol)\n\tinst.admin.AssertOwnedBy(cur.Previous().Address())\n\tcheckErr(inst.ledger.Mint(to, amount))\n}\n\nfunc Burn(cur realm, symbol string, from address, amount int64) {\n\tinst := mustGetInstance(symbol)\n\tinst.admin.AssertOwnedBy(cur.Previous().Address())\n\tcheckErr(inst.ledger.Burn(from, amount))\n}\n\n// instance admin functionality\nfunc DropInstanceOwnership(cur realm, symbol string) {\n\tinst := mustGetInstance(symbol)\n\tcheckErr(inst.admin.DropOwnership(0, cur))\n}\n\nfunc TransferInstanceOwnership(cur realm, symbol string, newOwner address) {\n\tinst := mustGetInstance(symbol)\n\tcheckErr(inst.admin.TransferOwnership(0, cur, newOwner))\n}\n\nfunc ListTokens(pageNumber, pageSize int) []*grc20.Token {\n\tpage := pager.GetPageWithSize(pageNumber, pageSize)\n\n\ttokens := make([]*grc20.Token, len(page.Items))\n\tfor i := range page.Items {\n\t\ttokens[i] = page.Items[i].Value.(*instance).token\n\t}\n\n\treturn tokens\n}\n\nfunc Render(path string) string {\n\trouter := mux.NewRouter()\n\trouter.HandleFunc(\"\", renderHome)\n\trouter.HandleFunc(\"{symbol}\", renderToken)\n\trouter.HandleFunc(\"{symbol}/balance/{address}\", renderBalance)\n\treturn router.Render(path)\n}\n\nfunc renderHome(res *mux.ResponseWriter, req *mux.Request) {\n\tout := md.H1(ufmt.Sprintf(\"GRC20 Tokens (%d)\", instances.Size()))\n\n\t// Get the current page of tokens based on the request path.\n\tpage := pager.MustGetPageByPath(req.RawPath)\n\n\t// Render the list of tokens.\n\tfor _, item := range page.Items {\n\t\ttoken := item.Value.(*instance).token\n\t\tout += md.BulletItem(\n\t\t\tmd.Link(\n\t\t\t\tufmt.Sprintf(\"%s ($%s)\", token.GetName(), token.GetSymbol()),\n\t\t\t\tufmt.Sprintf(\"/r/demo/grc20factory:%s\", token.GetSymbol()),\n\t\t\t),\n\t\t)\n\t}\n\tout += \"\\n\"\n\n\t// Add the page picker.\n\tout += md.Paragraph(page.Picker(req.Path))\n\n\tres.Write(out)\n}\n\nfunc renderToken(res *mux.ResponseWriter, req *mux.Request) {\n\t// Get the token symbol from the request.\n\tsymbol := req.GetVar(\"symbol\")\n\tinst := mustGetInstance(symbol)\n\n\t// Render the token details.\n\tout := inst.token.RenderHome()\n\tout += md.BulletItem(\n\t\tufmt.Sprintf(\"%s: %s\", md.Bold(\"Admin\"), inst.admin.Owner()),\n\t)\n\n\tres.Write(out)\n}\n\nfunc renderBalance(res *mux.ResponseWriter, req *mux.Request) {\n\tvar (\n\t\tsymbol = req.GetVar(\"symbol\")\n\t\taddr   = req.GetVar(\"address\")\n\t)\n\n\t// Get the balance of the specified address for the token.\n\tinst := mustGetInstance(symbol)\n\tbalance := inst.token.CallerTeller().BalanceOf(address(addr))\n\n\t// Render the balance information.\n\tout := md.Paragraph(\n\t\tufmt.Sprintf(\"%s balance: %d\", md.Bold(addr), balance),\n\t)\n\n\tres.Write(out)\n}\n\nfunc mustGetInstance(symbol string) *instance {\n\tt := instances.Get(symbol)\n\tif t == nil {\n\t\tpanic(\"token instance does not exist\")\n\t}\n\treturn t.(*instance)\n}\n\nfunc checkErr(err error) {\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n}\n"},{"name":"grc20factory_test.gno","body":"package grc20factory\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestReadOnlyPublicMethods(cur realm, t *testing.T) {\n\tadmin := testutils.TestAddress(\"admin\")\n\tbob := testutils.TestAddress(\"bob\")\n\tcarl := testutils.TestAddress(\"carl\")\n\n\ttype test struct {\n\t\tname    string\n\t\tbalance int64\n\t\tfn      func() int64\n\t}\n\n\tcheckBalances := func(step string, totSup, balAdm, balBob, allowAdmBob, balCarl int64) {\n\t\ttests := []test{\n\t\t\t{\"TotalSupply\", totSup, func() int64 { return TotalSupply(\"FOO\") }},\n\t\t\t{\"BalanceOf(admin)\", balAdm, func() int64 { return BalanceOf(\"FOO\", admin) }},\n\t\t\t{\"BalanceOf(bob)\", balBob, func() int64 { return BalanceOf(\"FOO\", bob) }},\n\t\t\t{\"Allowance(admin, bob)\", allowAdmBob, func() int64 { return Allowance(\"FOO\", admin, bob) }},\n\t\t\t{\"BalanceOf(carl)\", balCarl, func() int64 { return BalanceOf(\"FOO\", carl) }},\n\t\t}\n\t\tfor _, tc := range tests {\n\t\t\treason := ufmt.Sprintf(\"%s.%s - %s\", step, tc.name, \"balances do not match\")\n\t\t\tuassert.Equal(t, tc.balance, tc.fn(), reason)\n\t\t}\n\t}\n\n\t// admin creates FOO and BAR.\n\ttesting.SetOriginCaller(admin)\n\tNewWithAdmin(cross(cur), \"Foo\", \"FOO\", 3, 1_111_111_000, 5_555, admin)\n\tNewWithAdmin(cross(cur), \"Bar\", \"BAR\", 3, 2_222_000, 6_666, admin)\n\tcheckBalances(\"step1\", 1_111_111_000, 1_111_111_000, 0, 0, 0)\n\n\t// admin mints to bob.\n\tmustGetInstance(\"FOO\").ledger.Mint(bob, 333_333_000)\n\tcheckBalances(\"step2\", 1_444_444_000, 1_111_111_000, 333_333_000, 0, 0)\n\n\t// carl uses the faucet.\n\ttesting.SetOriginCaller(carl)\n\tFaucet(cross(cur), \"FOO\")\n\tcheckBalances(\"step3\", 1_444_449_555, 1_111_111_000, 333_333_000, 0, 5_555)\n\n\t// admin gives to bob some allowance.\n\ttesting.SetOriginCaller(admin)\n\tApprove(cross(cur), \"FOO\", bob, 1_000_000)\n\tcheckBalances(\"step4\", 1_444_449_555, 1_111_111_000, 333_333_000, 1_000_000, 5_555)\n\n\t// bob uses a part of the allowance.\n\ttesting.SetOriginCaller(bob)\n\tTransferFrom(cross(cur), \"FOO\", admin, carl, 400_000)\n\tcheckBalances(\"step5\", 1_444_449_555, 1_110_711_000, 333_333_000, 600_000, 405_555)\n\n\t// bob uses a part of the allowance.\n\ttesting.SetOriginCaller(bob)\n\tTransferFrom(cross(cur), \"FOO\", admin, carl, 600_000)\n\tcheckBalances(\"step6\", 1_444_449_555, 1_110_111_000, 333_333_000, 0, 1_005_555)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"5L82ftaeGrBI+zPY4rkL5i6dIVKuUBJIhOOZYA6gxtfbn0mDTByGG7EVOreiJwn63b3mi9IH/c3Lyfl/Zw6SDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"panictoerr","path":"gno.land/p/aeddi/panictoerr","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/aeddi/panictoerr\"\ngno = \"0.9\"\n"},{"name":"panictoerr.gno","body":"package panictoerr\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// PanicToError executes a function that might panic and, if it does,\n// recovers the panic and converts it to an error.\nfunc PanicToError(mightPanic func()) (err error) {\n\t// Catch any panic that might occur and convert it to an error.\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\terr = anyToError(r)\n\t\t}\n\t}()\n\n\t// Execute the function that might panic.\n\tmightPanic()\n\n\treturn nil\n}\n\n// AbortToError executes a function that might abort, and if it does,\n// revives the abort and converts it to an error.\nfunc AbortToError(mightAbort func()) error {\n\t// Catch any abort that might occur and convert it to an error.\n\tif r := revive(mightAbort); r != nil {\n\t\treturn anyToError(r)\n\t}\n\n\treturn nil\n}\n\n// PanicAbortToError executes a function that might either panic or abort,\n// and if it does, it recovers the panic or revives the abort and converts\n// it to an error.\nfunc PanicAbortToError(mightPanicOrAbort func()) error {\n\tvar panicErr error\n\n\t// Catch any panic or abort that might occur and convert it to an error.\n\tif abortErr := AbortToError(func() {\n\t\tpanicErr = PanicToError(mightPanicOrAbort)\n\t}); abortErr != nil {\n\t\treturn abortErr\n\t}\n\n\treturn panicErr\n}\n\n// anyToError converts any value to an error.\nfunc anyToError(v any) error {\n\tswitch v := v.(type) {\n\tcase string:\n\t\treturn errors.New(v)\n\tcase error:\n\t\treturn v\n\tdefault:\n\t\treturn errors.New(ufmt.Sprint(v))\n\t}\n}\n"},{"name":"panictoerr_test.gno","body":"package panictoerr_test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\n\tpte \"gno.land/p/aeddi/panictoerr\"\n\t\"gno.land/p/nt/uassert/v0\"\n\tgrc20 \"gno.land/r/demo/defi/grc20factory\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\n// Test PanicToError with different types as panic value.\nfunc TestSimplePanicToError(t *testing.T) {\n\terr := pte.PanicToError(func() {\n\t\tpanic(\"string\")\n\t})\n\tuassert.Equal(t, err.Error(), \"string\")\n\n\terr = pte.PanicToError(func() {\n\t\tpanic(errors.New(\"error\"))\n\t})\n\tuassert.Equal(t, err.Error(), \"error\")\n\n\terr = pte.PanicToError(func() {\n\t\tpanic(42)\n\t})\n\tuassert.Equal(t, err.Error(), \"42\")\n}\n\nfunc TestRealmPanicToError(cur realm, t *testing.T) {\n\t// Set a test realm to be able to call a realm.\n\ttestRealm := testing.NewCodeRealm(\"gno.land/r/aeddi/panictoerr/test\")\n\ttesting.SetRealm(testRealm)\n\n\tconst message = \"token instance does not exist\"\n\tvar err error\n\n\t// Define a panicking function (local panic; does not cross any\n\t// realm boundary). Under the unified declaring-realm borrow,\n\t// calling grc20.Bank() would borrow into /r/demo/defi/grc20factory\n\t// and any panic there would cross a realm boundary, becoming an\n\t// abort catchable only by revive() — see the `aborting` case\n\t// below. To exercise the pure recover() path, panic locally.\n\tpanicking := func() {\n\t\tpanic(message)\n\t}\n\n\t// panicking function should panic.\n\tuassert.PanicsWithMessage(t, cur, message, panicking)\n\n\t// panicking function should panic when wrapped in AbortToError.\n\tuassert.PanicsWithMessage(t, cur, message, func() { pte.AbortToError(panicking) })\n\n\t// panicking function should not panic when wrapped in PanicToError.\n\tuassert.NotPanics(\n\t\tt, cur,\n\t\tfunc() { err = pte.PanicToError(panicking) },\n\t\t\"panicking function should not panic when wrapped in PanicToError\",\n\t)\n\tuassert.Equal(t, err.Error(), message)\n\n\t// panicking function should not panic when wrapped in PanicAbortToError.\n\tuassert.NotPanics(\n\t\tt, cur,\n\t\tfunc() { err = pte.PanicAbortToError(panicking) },\n\t\t\"panicking function should not panic when wrapped in PanicAbortToError\",\n\t)\n\tuassert.Equal(t, err.Error(), message)\n\n\t// Define an aborting function (crossing).\n\taborting := func() {\n\t\tgrc20.Faucet(cross(cur), \"unknown\")\n\t}\n\n\t// aborting function should abort.\n\tuassert.AbortsWithMessage(t, cur, message, aborting)\n\n\t// aborting function should abort when wrapped in PanicToError.\n\tuassert.AbortsWithMessage(t, cur, message, func() { pte.PanicToError(aborting) })\n\n\t// aborting function should not abort when wrapped in AbortToError.\n\tuassert.NotAborts(\n\t\tt, cur,\n\t\tfunc() { err = pte.AbortToError(aborting) },\n\t\t\"aborting function should not abort when wrapped in AbortToError\",\n\t)\n\tuassert.Equal(t, err.Error(), message)\n\n\t// aborting function should not abort when wrapped in PanicAbortToError.\n\tuassert.NotAborts(\n\t\tt, cur,\n\t\tfunc() { err = pte.PanicAbortToError(aborting) },\n\t\t\"aborting function should not abort when wrapped in PanicAbortToError\",\n\t)\n\tuassert.Equal(t, err.Error(), message)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Z2g+5y7Kq2vDHmBUBmNTW1fW2b78iYCF4AGMx2kNBMLah8F/OSVwzUOqc7gpwW8QE4LXVnZPMLI7kMX9GUVLBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"dom","path":"gno.land/p/archive/dom","files":[{"name":"dom.gno","body":"// XXX This is only used for testing in ./tests.\n// Otherwise this package is deprecated.\n// TODO: replace with a package that is supported, and delete this.\n\npackage dom\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\ntype Plot struct {\n\tName     string\n\tPosts    avl.Tree // postsCtr -\u003e *Post\n\tPostsCtr int\n}\n\nfunc (plot *Plot) AddPost(title string, body string) {\n\tctr := plot.PostsCtr\n\tplot.PostsCtr++\n\tkey := strconv.Itoa(ctr)\n\tpost := \u0026Post{\n\t\tTitle: title,\n\t\tBody:  body,\n\t}\n\tplot.Posts.Set(key, post)\n}\n\nfunc (plot *Plot) String() string {\n\tstr := \"# [plot] \" + plot.Name + \"\\n\"\n\tif plot.Posts.Size() \u003e 0 {\n\t\tplot.Posts.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tstr += \"\\n\"\n\t\t\tstr += value.(*Post).String()\n\t\t\treturn false\n\t\t})\n\t}\n\treturn str\n}\n\ntype Post struct {\n\tTitle    string\n\tBody     string\n\tComments avl.Tree\n}\n\nfunc (post *Post) String() string {\n\tstr := \"## \" + post.Title + \"\\n\"\n\tstr += \"\"\n\tstr += post.Body\n\tif post.Comments.Size() \u003e 0 {\n\t\tpost.Comments.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tstr += \"\\n\"\n\t\t\tstr += value.(*Comment).String()\n\t\t\treturn false\n\t\t})\n\t}\n\treturn str\n}\n\ntype Comment struct {\n\tCreator string\n\tBody    string\n}\n\nfunc (cmm Comment) String() string {\n\treturn cmm.Body + \" - @\" + cmm.Creator + \"\\n\"\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/archive/dom\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"SpupxuPN8rtdQAn3q/WKVwP9C1WsTNmyxNQauy55YEMWcPxQZ1AbHPL8IqHy2yD0MY7WMCEOLWWT1cF0ke51AA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"blog","path":"gno.land/p/demo/blog","files":[{"name":"blog.gno","body":"package blog\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Blog struct {\n\tTitle             string\n\tPrefix            string   // i.e. r/gnoland/blog:\n\tPosts             avl.Tree // slug -\u003e *Post\n\tPostsPublished    avl.Tree // published-date -\u003e *Post\n\tPostsAlphabetical avl.Tree // title -\u003e *Post\n\tNoBreadcrumb      bool\n}\n\nfunc (b Blog) RenderLastPostsWidget(limit int) string {\n\tif b.PostsPublished.Size() == 0 {\n\t\treturn \"No posts.\"\n\t}\n\n\toutput := \"\"\n\ti := 0\n\tb.PostsPublished.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\tp := value.(*Post)\n\t\toutput += ufmt.Sprintf(\"- [%s](%s)\\n\", p.Title, p.URL())\n\t\ti++\n\t\treturn i \u003e= limit\n\t})\n\treturn output\n}\n\nfunc (b Blog) RenderHome(res *mux.ResponseWriter, _ *mux.Request) {\n\tif !b.NoBreadcrumb {\n\t\tres.Write(breadcrumb([]string{b.Title}))\n\t}\n\n\tif b.Posts.Size() == 0 {\n\t\tres.Write(\"No posts.\")\n\t\treturn\n\t}\n\n\tconst maxCol = 3\n\tvar rowItems []string\n\n\tb.PostsPublished.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\tpost := value.(*Post)\n\t\trowItems = append(rowItems, post.RenderListItem())\n\n\t\tif len(rowItems) == maxCol {\n\t\t\tres.Write(\"\u003cgno-columns\u003e\" + strings.Join(rowItems, \"\u003cgno-columns-sep\u003e\") + \"\u003c/gno-columns\u003e\\n\")\n\t\t\trowItems = []string{}\n\t\t}\n\t\treturn false\n\t})\n\n\t// Pad and flush any remaining items\n\tif len(rowItems) \u003e 0 {\n\t\tfor len(rowItems) \u003c maxCol {\n\t\t\trowItems = append(rowItems, \"\")\n\t\t}\n\t\tres.Write(\"\u003cgno-columns\u003e\" + strings.Join(rowItems, \"\\n\u003cgno-columns-sep\u003e\\n\") + \"\u003c/gno-columns\u003e\\n\")\n\t}\n}\n\nfunc (b Blog) RenderPost(res *mux.ResponseWriter, req *mux.Request) {\n\tslug := req.GetVar(\"slug\")\n\n\tpost := b.Posts.Get(slug)\n\tif post == nil {\n\t\tres.Write(\"404\")\n\t\treturn\n\t}\n\tp := post.(*Post)\n\n\tres.Write(\"\u003cmain class='gno-tmpl-page'\u003e\" + \"\\n\\n\")\n\n\tres.Write(\"# \" + p.Title + \"\\n\\n\")\n\tres.Write(p.Body + \"\\n\\n\")\n\tres.Write(\"---\\n\\n\")\n\n\tres.Write(p.RenderTagList() + \"\\n\\n\")\n\tres.Write(p.RenderAuthorList() + \"\\n\\n\")\n\tres.Write(p.RenderPublishData() + \"\\n\\n\")\n\n\tres.Write(\"---\\n\")\n\tres.Write(\"\u003cdetails\u003e\u003csummary\u003eComment section\u003c/summary\u003e\\n\\n\")\n\n\t// comments\n\tp.Comments.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\tcomment := value.(*Comment)\n\t\tres.Write(comment.RenderListItem())\n\t\treturn false\n\t})\n\n\tres.Write(\"\u003c/details\u003e\\n\")\n\tres.Write(\"\u003c/main\u003e\")\n}\n\nfunc (b Blog) RenderTag(res *mux.ResponseWriter, req *mux.Request) {\n\tslug := req.GetVar(\"slug\")\n\n\tif slug == \"\" {\n\t\tres.Write(\"404\")\n\t\treturn\n\t}\n\n\tif !b.NoBreadcrumb {\n\t\tbreadStr := breadcrumb([]string{\n\t\t\tufmt.Sprintf(\"[%s](%s)\", b.Title, b.Prefix),\n\t\t\t\"t\",\n\t\t\tslug,\n\t\t})\n\t\tres.Write(breadStr)\n\t}\n\n\tnb := 0\n\tb.Posts.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tpost := value.(*Post)\n\t\tif !post.HasTag(slug) {\n\t\t\treturn false\n\t\t}\n\t\tres.Write(post.RenderListItem())\n\t\tnb++\n\t\treturn false\n\t})\n\tif nb == 0 {\n\t\tres.Write(\"No posts.\")\n\t}\n}\n\nfunc (b Blog) Render(path string) string {\n\trouter := mux.NewRouter()\n\trouter.HandleFunc(\"\", b.RenderHome)\n\trouter.HandleFunc(\"p/{slug}\", b.RenderPost)\n\trouter.HandleFunc(\"t/{slug}\", b.RenderTag)\n\treturn router.Render(path)\n}\n\nfunc (b *Blog) NewPost(publisher address, slug, title, body, pubDate string, authors, tags []string) error {\n\tif b.Posts.Has(slug) {\n\t\treturn ErrPostSlugExists\n\t}\n\n\tvar parsedTime time.Time\n\tvar err error\n\tif pubDate != \"\" {\n\t\tparsedTime, err = time.Parse(time.RFC3339, pubDate)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t} else {\n\t\t// If no publication date was passed in by caller, take current block time\n\t\tparsedTime = time.Now()\n\t}\n\n\tpost := \u0026Post{\n\t\tPublisher: publisher,\n\t\tAuthors:   authors,\n\t\tSlug:      slug,\n\t\tTitle:     title,\n\t\tBody:      body,\n\t\tTags:      tags,\n\t\tCreatedAt: parsedTime,\n\t}\n\n\treturn b.prepareAndSetPost(post, false)\n}\n\nfunc (b *Blog) prepareAndSetPost(post *Post, edit bool) error {\n\tpost.Title = strings.TrimSpace(post.Title)\n\tpost.Body = strings.TrimSpace(post.Body)\n\n\tif post.Title == \"\" {\n\t\treturn ErrPostTitleMissing\n\t}\n\tif post.Body == \"\" {\n\t\treturn ErrPostBodyMissing\n\t}\n\tif post.Slug == \"\" {\n\t\treturn ErrPostSlugMissing\n\t}\n\n\tpost.Blog = b\n\tpost.UpdatedAt = time.Now()\n\n\ttrimmedTitleKey := getTitleKey(post.Title)\n\tpubDateKey := getPublishedKey(post.CreatedAt)\n\n\tif !edit {\n\t\t// Cannot have two posts with same title key\n\t\tif b.PostsAlphabetical.Has(trimmedTitleKey) {\n\t\t\treturn ErrPostTitleExists\n\t\t}\n\t\t// Cannot have two posts with *exact* same timestamp\n\t\tif b.PostsPublished.Has(pubDateKey) {\n\t\t\treturn ErrPostPubDateExists\n\t\t}\n\t}\n\n\t// Store post under keys\n\tb.PostsAlphabetical.Set(trimmedTitleKey, post)\n\tb.PostsPublished.Set(pubDateKey, post)\n\tb.Posts.Set(post.Slug, post)\n\n\treturn nil\n}\n\nfunc (b *Blog) RemovePost(slug string) {\n\tp := b.Posts.Get(slug)\n\tif p == nil {\n\t\tpanic(\"post with specified slug doesn't exist\")\n\t}\n\n\tpost := p.(*Post)\n\n\ttitleKey := getTitleKey(post.Title)\n\tpublishedKey := getPublishedKey(post.CreatedAt)\n\n\t_, _ = b.Posts.Remove(slug)\n\t_, _ = b.PostsAlphabetical.Remove(titleKey)\n\t_, _ = b.PostsPublished.Remove(publishedKey)\n}\n\nfunc (b *Blog) GetPost(slug string) *Post {\n\tpost := b.Posts.Get(slug)\n\tif post == nil {\n\t\treturn nil\n\t}\n\treturn post.(*Post)\n}\n\ntype Post struct {\n\tBlog         *Blog\n\tSlug         string // FIXME: save space?\n\tTitle        string\n\tBody         string\n\tCreatedAt    time.Time\n\tUpdatedAt    time.Time\n\tComments     avl.Tree\n\tAuthors      []string\n\tPublisher    address\n\tTags         []string\n\tCommentIndex int\n}\n\nfunc (p *Post) Update(title, body, publicationDate string, authors, tags []string) error {\n\tp.Title = title\n\tp.Body = body\n\tp.Tags = tags\n\tp.Authors = authors\n\n\tparsedTime, err := time.Parse(time.RFC3339, publicationDate)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tp.CreatedAt = parsedTime\n\treturn p.Blog.prepareAndSetPost(p, true)\n}\n\nfunc (p *Post) AddComment(author address, comment string) error {\n\tif p == nil {\n\t\treturn ErrNoSuchPost\n\t}\n\tp.CommentIndex++\n\tcommentKey := strconv.Itoa(p.CommentIndex)\n\tcomment = strings.TrimSpace(comment)\n\tp.Comments.Set(commentKey, \u0026Comment{\n\t\tPost:      p,\n\t\tCreatedAt: time.Now(),\n\t\tAuthor:    author,\n\t\tComment:   comment,\n\t})\n\n\treturn nil\n}\n\nfunc (p *Post) DeleteComment(index int) error {\n\tif p == nil {\n\t\treturn ErrNoSuchPost\n\t}\n\tcommentKey := strconv.Itoa(index)\n\tp.Comments.Remove(commentKey)\n\treturn nil\n}\n\nfunc (p *Post) HasTag(tag string) bool {\n\tif p == nil {\n\t\treturn false\n\t}\n\tfor _, t := range p.Tags {\n\t\tif t == tag {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc (p *Post) RenderListItem() string {\n\tif p == nil {\n\t\treturn \"error: no such post\\n\"\n\t}\n\toutput := ufmt.Sprintf(\"\\n### [%s](%s)\\n\", p.Title, p.URL())\n\t// output += ufmt.Sprintf(\"**[Learn More](%s)**\\n\\n\", p.URL())\n\n\toutput += p.CreatedAt.Format(\"02 Jan 2006\")\n\t// output += p.Summary() + \"\\n\\n\"\n\t// output += p.RenderTagList() + \"\\n\\n\"\n\toutput += \"\\n\"\n\treturn output\n}\n\n// Render post tags\nfunc (p *Post) RenderTagList() string {\n\tif p == nil {\n\t\treturn \"error: no such post\\n\"\n\t}\n\tif len(p.Tags) == 0 {\n\t\treturn \"\"\n\t}\n\n\toutput := \"Tags: \"\n\tfor idx, tag := range p.Tags {\n\t\tif idx \u003e 0 {\n\t\t\toutput += \" \"\n\t\t}\n\t\ttagURL := p.Blog.Prefix + \"t/\" + tag\n\t\toutput += ufmt.Sprintf(\"[#%s](%s)\", tag, tagURL)\n\n\t}\n\treturn output\n}\n\n// Render authors if there are any\nfunc (p *Post) RenderAuthorList() string {\n\tout := \"Written\"\n\tif len(p.Authors) != 0 {\n\t\tout += \" by \"\n\n\t\tfor idx, author := range p.Authors {\n\t\t\tout += author\n\t\t\tif idx \u003c len(p.Authors)-1 {\n\t\t\t\tout += \", \"\n\t\t\t}\n\t\t}\n\t}\n\tout += \" on \" + p.CreatedAt.Format(\"02 Jan 2006\")\n\n\treturn out\n}\n\nfunc (p *Post) RenderPublishData() string {\n\tout := \"Published \"\n\tif p.Publisher != \"\" {\n\t\tout += \"by \" + p.Publisher.String() + \" \"\n\t}\n\tout += \"to \" + p.Blog.Title\n\n\treturn out\n}\n\nfunc (p *Post) URL() string {\n\tif p == nil {\n\t\treturn p.Blog.Prefix + \"404\"\n\t}\n\treturn p.Blog.Prefix + \"p/\" + p.Slug\n}\n\nfunc (p *Post) Summary() string {\n\tif p == nil {\n\t\treturn \"error: no such post\\n\"\n\t}\n\n\t// FIXME: better summary.\n\tlines := strings.Split(p.Body, \"\\n\")\n\tif len(lines) \u003c= 3 {\n\t\treturn p.Body\n\t}\n\treturn strings.Join(lines[0:3], \"\\n\") + \"...\"\n}\n\ntype Comment struct {\n\tPost      *Post\n\tCreatedAt time.Time\n\tAuthor    address\n\tComment   string\n}\n\nfunc (c Comment) RenderListItem() string {\n\toutput := \"\u003ch5\u003e\"\n\toutput += c.Comment + \"\\n\\n\"\n\toutput += \"\u003c/h5\u003e\"\n\n\toutput += \"\u003ch6\u003e\"\n\toutput += ufmt.Sprintf(\"by %s on %s\", c.Author, c.CreatedAt.Format(time.RFC822))\n\toutput += \"\u003c/h6\u003e\\n\\n\"\n\n\toutput += \"---\\n\\n\"\n\n\treturn output\n}\n"},{"name":"blog_test.gno","body":"package blog\n\n// TODO: add generic tests here.\n//       right now, you can checkout r/gnoland/blog/*_test.gno.\n"},{"name":"errors.gno","body":"package blog\n\nimport \"errors\"\n\nvar (\n\tErrPostTitleMissing  = errors.New(\"post title is missing\")\n\tErrPostSlugMissing   = errors.New(\"post slug is missing\")\n\tErrPostBodyMissing   = errors.New(\"post body is missing\")\n\tErrPostSlugExists    = errors.New(\"post with specified slug already exists\")\n\tErrPostPubDateExists = errors.New(\"post with specified publication date exists\")\n\tErrPostTitleExists   = errors.New(\"post with specified title already exists\")\n\tErrNoSuchPost        = errors.New(\"no such post\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/blog\"\ngno = \"0.9\"\n"},{"name":"util.gno","body":"package blog\n\nimport (\n\t\"strings\"\n\t\"time\"\n)\n\nfunc breadcrumb(parts []string) string {\n\treturn \"# \" + strings.Join(parts, \" / \") + \"\\n\\n\"\n}\n\nfunc getTitleKey(title string) string {\n\treturn strings.ReplaceAll(title, \" \", \"\")\n}\n\nfunc getPublishedKey(t time.Time) string {\n\treturn t.Format(time.RFC3339)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"bLV3LiiTuNQU0fLOGG97oySweDBDLZghqavWXn0z/WdZ+1Xv7N0qkqdgNiB7KZqm+QTwXMviRiKZyPGe4SLtAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"agent","path":"gno.land/p/demo/gnorkle/agent","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/agent\"\ngno = \"0.9\"\n"},{"name":"whitelist.gno","body":"package agent\n\nimport \"gno.land/p/nt/avl/v0\"\n\n// Whitelist manages whitelisted agent addresses.\ntype Whitelist struct {\n\tstore *avl.Tree\n}\n\n// ClearAddresses removes all addresses from the whitelist and puts into a state\n// that indicates it is moot and has no whitelist defined.\nfunc (m *Whitelist) ClearAddresses() {\n\tm.store = nil\n}\n\n// AddAddresses adds the given addresses to the whitelist.\nfunc (m *Whitelist) AddAddresses(addresses []string) {\n\tif m.store == nil {\n\t\tm.store = avl.NewTree()\n\t}\n\n\tfor _, address_XXX := range addresses {\n\t\tm.store.Set(address_XXX, struct{}{})\n\t}\n}\n\n// RemoveAddress removes the given address from the whitelist if it exists.\nfunc (m *Whitelist) RemoveAddress(address_XXX string) {\n\tif m.store == nil {\n\t\treturn\n\t}\n\n\tm.store.Remove(address_XXX)\n}\n\n// HasDefinition returns true if the whitelist has a definition. It retuns false if\n// `ClearAddresses` has been called without any subsequent `AddAddresses` calls, or\n// if `AddAddresses` has never been called.\nfunc (m Whitelist) HasDefinition() bool {\n\treturn m.store != nil\n}\n\n// HasAddress returns true if the given address is in the whitelist.\nfunc (m Whitelist) HasAddress(address_XXX string) bool {\n\tif m.store == nil {\n\t\treturn false\n\t}\n\n\treturn m.store.Has(address_XXX)\n}\n"},{"name":"whitelist_test.gno","body":"package agent_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/gnorkle/agent\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestWhitelist(t *testing.T) {\n\tvar whitelist agent.Whitelist\n\n\tuassert.False(t, whitelist.HasDefinition(), \"whitelist should not be defined initially\")\n\n\twhitelist.AddAddresses([]string{\"a\", \"b\"})\n\tuassert.True(t, whitelist.HasAddress(\"a\"), `whitelist should have address \"a\"`)\n\tuassert.True(t, whitelist.HasAddress(\"b\"), `whitelist should have address \"b\"`)\n\tuassert.True(t, whitelist.HasDefinition(), \"whitelist should be defined after adding addresses\")\n\n\twhitelist.RemoveAddress(\"a\")\n\tuassert.False(t, whitelist.HasAddress(\"a\"), `whitelist should not have address \"a\"`)\n\tuassert.True(t, whitelist.HasAddress(\"b\"), `whitelist should still have address \"b\"`)\n\n\twhitelist.ClearAddresses()\n\tuassert.False(t, whitelist.HasAddress(\"a\"), `whitelist cleared; should not have address \"a\"`)\n\tuassert.False(t, whitelist.HasAddress(\"b\"), `whitelist cleared; should still have address \"b\"`)\n\tuassert.False(t, whitelist.HasDefinition(), \"whitelist cleared; should not be defined\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"AwUUpjV79lcwRmZRqtPNHgPD3ltUGh3xtpBvAJcAmmVCQzR1gIqOPspmW9eUYygUhH9h4sj+LfTsmY0YV1P9DA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"feed","path":"gno.land/p/demo/gnorkle/feed","files":[{"name":"errors.gno","body":"package feed\n\nimport \"errors\"\n\nvar ErrUndefined = errors.New(\"undefined feed\")\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/feed\"\ngno = \"0.9\"\n"},{"name":"task.gno","body":"package feed\n\n// Task is a unit of work that can be part of a `Feed` definition. Tasks\n// are executed by agents.\ntype Task interface {\n\tMarshalJSON() ([]byte, error)\n}\n"},{"name":"type.gno","body":"package feed\n\n// Type indicates the type of a feed.\ntype Type int\n\nconst (\n\t// TypeStatic indicates a feed cannot be changed once the first value is committed.\n\tTypeStatic Type = iota\n\t// TypeContinuous indicates a feed can continuously ingest values and will publish\n\t// a new value on request using the values it has ingested.\n\tTypeContinuous\n\t// TypePeriodic indicates a feed can accept one or more values within a certain period\n\t// and will proceed to commit these values at the end up each period to produce an\n\t// aggregate value before starting a new period.\n\tTypePeriodic\n)\n"},{"name":"value.gno","body":"package feed\n\nimport \"time\"\n\n// Value represents a value published by a feed. The `Time` is when the value was published.\ntype Value struct {\n\tString string\n\tTime   time.Time\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"f6rKufR5aiIV4Vm5eFe9fM0eW5ebWcma6QdOmtLIM9HKZFYn91A90kphsRU9PjYQ1cVOCrSCLJMz2gUYxDExAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ingester","path":"gno.land/p/demo/gnorkle/ingester","files":[{"name":"errors.gno","body":"package ingester\n\nimport \"errors\"\n\nvar ErrUndefined = errors.New(\"ingester undefined\")\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/ingester\"\ngno = \"0.9\"\n"},{"name":"type.gno","body":"package ingester\n\n// Type indicates an ingester type.\ntype Type int\n\nconst (\n\t// TypeSingle indicates an ingester that can only ingest a single within a given period or no period.\n\tTypeSingle Type = iota\n\t// TypeMulti indicates an ingester that can ingest multiple within a given period or no period\n\tTypeMulti\n)\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"3SN0zd6q521TFLyvDCgRoe3BcGRdqZPYgh6v+s15TJEqzeI4zif7kuqWBZAGbdL7Vw2aQu6VXJQHvhL8SBmEDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"message","path":"gno.land/p/demo/gnorkle/message","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/message\"\ngno = \"0.9\"\n"},{"name":"parse.gno","body":"package message\n\nimport \"strings\"\n\n// ParseFunc parses a raw message and returns the message function\n// type extracted from the remainder of the message.\nfunc ParseFunc(rawMsg string) (FuncType, string) {\n\tfuncType, remainder := parseFirstToken(rawMsg)\n\treturn FuncType(funcType), remainder\n}\n\n// ParseID parses a raw message and returns the ID extracted from\n// the remainder of the message.\nfunc ParseID(rawMsg string) (string, string) {\n\treturn parseFirstToken(rawMsg)\n}\n\nfunc parseFirstToken(rawMsg string) (string, string) {\n\tmsgParts := strings.SplitN(rawMsg, \",\", 2)\n\tif len(msgParts) \u003c 2 {\n\t\treturn msgParts[0], \"\"\n\t}\n\n\treturn msgParts[0], msgParts[1]\n}\n"},{"name":"parse_test.gno","body":"package message_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/gnorkle/message\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestParseFunc(t *testing.T) {\n\ttests := []struct {\n\t\tname         string\n\t\tinput        string\n\t\texpFuncType  message.FuncType\n\t\texpRemainder string\n\t}{\n\t\t{\n\t\t\tname: \"empty\",\n\t\t},\n\t\t{\n\t\t\tname:        \"func only\",\n\t\t\tinput:       \"ingest\",\n\t\t\texpFuncType: message.FuncTypeIngest,\n\t\t},\n\t\t{\n\t\t\tname:         \"func with short remainder\",\n\t\t\tinput:        \"commit,asdf\",\n\t\t\texpFuncType:  message.FuncTypeCommit,\n\t\t\texpRemainder: \"asdf\",\n\t\t},\n\t\t{\n\t\t\tname:         \"func with long remainder\",\n\t\t\tinput:        \"request,hello,world,goodbye\",\n\t\t\texpFuncType:  message.FuncTypeRequest,\n\t\t\texpRemainder: \"hello,world,goodbye\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tfuncType, remainder := message.ParseFunc(tt.input)\n\n\t\t\tuassert.Equal(t, string(tt.expFuncType), string(funcType))\n\t\t\tuassert.Equal(t, tt.expRemainder, remainder)\n\t\t})\n\t}\n}\n"},{"name":"type.gno","body":"package message\n\n// FuncType is the type of function that is being called by the agent.\ntype FuncType string\n\nconst (\n\t// FuncTypeIngest means the agent is sending data for ingestion.\n\tFuncTypeIngest FuncType = \"ingest\"\n\t// FuncTypeCommit means the agent is requesting a feed commit the transitive data\n\t// being held by its ingester.\n\tFuncTypeCommit FuncType = \"commit\"\n\t// FuncTypeRequest means the agent is requesting feed definitions for all those\n\t// that it is whitelisted to provide data for.\n\tFuncTypeRequest FuncType = \"request\"\n)\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"UsDpA+9mLINxDFQS8AiuTtWgsw4dx53bYDjoV24bRkwV4HGDLElPKGO4aveDZus4sTSadeVHnKziZikK9je/Ag=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"gnorkle","path":"gno.land/p/demo/gnorkle/gnorkle","files":[{"name":"feed.gno","body":"package gnorkle\n\nimport (\n\t\"gno.land/p/demo/gnorkle/feed\"\n\t\"gno.land/p/demo/gnorkle/message\"\n)\n\n// Feed is an abstraction used by a gnorkle `Instance` to ingest data from\n// agents and provide data feeds to consumers.\ntype Feed interface {\n\tID() string\n\tType() feed.Type\n\tValue() (value feed.Value, dataType string, consumable bool)\n\tIngest(funcType message.FuncType, rawMessage, providerAddress string) error\n\tMarshalJSON() ([]byte, error)\n\tTasks() []feed.Task\n\tIsActive() bool\n}\n\n// FeedWithWhitelist associates a `Whitelist` with a `Feed`.\ntype FeedWithWhitelist struct {\n\tFeed\n\tWhitelist\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/gnorkle\"\ngno = \"0.9\"\n"},{"name":"ingester.gno","body":"package gnorkle\n\nimport \"gno.land/p/demo/gnorkle/ingester\"\n\n// Ingester is the abstraction that allows a `Feed` to ingest data from agents\n// and commit it to storage using zero or more intermediate aggregation steps.\ntype Ingester interface {\n\tType() ingester.Type\n\tIngest(value, providerAddress string) (canAutoCommit bool, err error)\n\tCommitValue(storage Storage, providerAddress string) error\n}\n"},{"name":"instance.gno","body":"package gnorkle\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/demo/gnorkle/agent\"\n\t\"gno.land/p/demo/gnorkle/feed\"\n\t\"gno.land/p/demo/gnorkle/message\"\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// Instance is a single instance of an oracle.\ntype Instance struct {\n\tfeeds     *avl.Tree\n\twhitelist agent.Whitelist\n}\n\n// NewInstance creates a new instance of an oracle.\nfunc NewInstance() *Instance {\n\treturn \u0026Instance{\n\t\tfeeds: avl.NewTree(),\n\t}\n}\n\nfunc assertValidID(id string) error {\n\tif len(id) == 0 {\n\t\treturn errors.New(\"feed ids cannot be empty\")\n\t}\n\n\tif strings.Contains(id, \",\") {\n\t\treturn errors.New(\"feed ids cannot contain commas\")\n\t}\n\n\treturn nil\n}\n\nfunc (i *Instance) assertFeedDoesNotExist(id string) error {\n\tif i.feeds.Has(id) {\n\t\treturn errors.New(\"feed already exists\")\n\t}\n\n\treturn nil\n}\n\n// AddFeeds adds feeds to the instance with empty whitelists.\nfunc (i *Instance) AddFeeds(feeds ...Feed) error {\n\tfor _, feed := range feeds {\n\t\tif err := assertValidID(feed.ID()); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif err := i.assertFeedDoesNotExist(feed.ID()); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\ti.feeds.Set(\n\t\t\tfeed.ID(),\n\t\t\tFeedWithWhitelist{\n\t\t\t\tWhitelist: new(agent.Whitelist),\n\t\t\t\tFeed:      feed,\n\t\t\t},\n\t\t)\n\t}\n\n\treturn nil\n}\n\n// AddFeedsWithWhitelists adds feeds to the instance with the given whitelists.\nfunc (i *Instance) AddFeedsWithWhitelists(feeds ...FeedWithWhitelist) error {\n\tfor _, feed := range feeds {\n\t\tif err := i.assertFeedDoesNotExist(feed.ID()); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := assertValidID(feed.ID()); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\ti.feeds.Set(\n\t\t\tfeed.ID(),\n\t\t\tFeedWithWhitelist{\n\t\t\t\tWhitelist: feed.Whitelist,\n\t\t\t\tFeed:      feed,\n\t\t\t},\n\t\t)\n\t}\n\n\treturn nil\n}\n\n// RemoveFeed removes a feed from the instance.\nfunc (i *Instance) RemoveFeed(id string) {\n\ti.feeds.Remove(id)\n}\n\n// PostMessageHandler is a type that allows for post-processing of feed state after a feed\n// ingests a message from an agent.\ntype PostMessageHandler interface {\n\tHandle(i *Instance, funcType message.FuncType, feed Feed) error\n}\n\n// HandleMessage handles a message from an agent and routes to either the logic that returns\n// feed definitions or the logic that allows a feed to ingest a message.\n//\n// TODO: Consider further message types that could allow administrative action such as modifying\n// a feed's whitelist without the owner of this oracle having to maintain a reference to it.\nfunc (i *Instance) HandleMessage(msg string, postHandler PostMessageHandler) (string, error) {\n\tcaller := string(unsafe.OriginCaller())\n\n\tfuncType, msg := message.ParseFunc(msg)\n\n\tswitch funcType {\n\tcase message.FuncTypeRequest:\n\t\treturn i.GetFeedDefinitions(caller)\n\n\tdefault:\n\t\tid, msg := message.ParseID(msg)\n\t\tif err := assertValidID(id); err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\n\t\tfeedWithWhitelist, err := i.getFeedWithWhitelist(id)\n\t\tif err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\n\t\tif !addressIsWhitelisted(\u0026i.whitelist, feedWithWhitelist, caller, nil) {\n\t\t\treturn \"\", errors.New(\"caller not whitelisted\")\n\t\t}\n\n\t\tif err := feedWithWhitelist.Ingest(funcType, msg, caller); err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\n\t\tif postHandler != nil {\n\t\t\tpostHandler.Handle(i, funcType, feedWithWhitelist)\n\t\t}\n\t}\n\n\treturn \"\", nil\n}\n\nfunc (i *Instance) getFeed(id string) (Feed, error) {\n\tuntypedFeed := i.feeds.Get(id)\n\tif untypedFeed == nil {\n\t\treturn nil, errors.New(\"invalid ingest id: \" + id)\n\t}\n\n\tfeed, ok := untypedFeed.(Feed)\n\tif !ok {\n\t\treturn nil, errors.New(\"invalid feed type\")\n\t}\n\n\treturn feed, nil\n}\n\nfunc (i *Instance) getFeedWithWhitelist(id string) (FeedWithWhitelist, error) {\n\tuntypedFeedWithWhitelist := i.feeds.Get(id)\n\tif untypedFeedWithWhitelist == nil {\n\t\treturn FeedWithWhitelist{}, errors.New(\"invalid ingest id: \" + id)\n\t}\n\n\tfeedWithWhitelist, ok := untypedFeedWithWhitelist.(FeedWithWhitelist)\n\tif !ok {\n\t\treturn FeedWithWhitelist{}, errors.New(\"invalid feed with whitelist type\")\n\t}\n\n\treturn feedWithWhitelist, nil\n}\n\n// GetFeedValue returns the most recently published value of a feed along with a string\n// representation of the value's type and boolean indicating whether the value is\n// okay for consumption.\nfunc (i *Instance) GetFeedValue(id string) (feed.Value, string, bool, error) {\n\tfoundFeed, err := i.getFeed(id)\n\tif err != nil {\n\t\treturn feed.Value{}, \"\", false, err\n\t}\n\n\tvalue, valueType, consumable := foundFeed.Value()\n\treturn value, valueType, consumable, nil\n}\n\n// GetFeedDefinitions returns a JSON string representing the feed definitions for which the given\n// agent address is whitelisted to provide values for ingestion.\nfunc (i *Instance) GetFeedDefinitions(forAddress string) (string, error) {\n\tinstanceHasAddressWhitelisted := !i.whitelist.HasDefinition() || i.whitelist.HasAddress(forAddress)\n\n\tbuf := new(strings.Builder)\n\tbuf.WriteString(\"[\")\n\tfirst := true\n\tvar err error\n\n\t// The boolean value returned by this callback function indicates whether to stop iterating.\n\ti.feeds.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tfeedWithWhitelist, ok := value.(FeedWithWhitelist)\n\t\tif !ok {\n\t\t\terr = errors.New(\"invalid feed type\")\n\t\t\treturn true\n\t\t}\n\n\t\t// Don't give agents the ability to try to publish to inactive feeds.\n\t\tif !feedWithWhitelist.IsActive() {\n\t\t\treturn false\n\t\t}\n\n\t\t// Skip feeds the address is not whitelisted for.\n\t\tif !addressIsWhitelisted(\u0026i.whitelist, feedWithWhitelist, forAddress, \u0026instanceHasAddressWhitelisted) {\n\t\t\treturn false\n\t\t}\n\n\t\tvar taskBytes []byte\n\t\tif taskBytes, err = feedWithWhitelist.Feed.MarshalJSON(); err != nil {\n\t\t\treturn true\n\t\t}\n\n\t\t// Guard against any tasks that shouldn't be returned; maybe they are not active because they have\n\t\t// already been completed.\n\t\tif len(taskBytes) == 0 {\n\t\t\treturn false\n\t\t}\n\n\t\tif !first {\n\t\t\tbuf.WriteString(\",\")\n\t\t}\n\n\t\tfirst = false\n\t\tbuf.Write(taskBytes)\n\t\treturn false\n\t})\n\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tbuf.WriteString(\"]\")\n\treturn buf.String(), nil\n}\n"},{"name":"storage.gno","body":"package gnorkle\n\nimport \"gno.land/p/demo/gnorkle/feed\"\n\n// Storage defines how published feed values should be read\n// and written.\ntype Storage interface {\n\tPut(value string) error\n\tGetLatest() feed.Value\n\tGetHistory() []feed.Value\n}\n"},{"name":"whitelist.gno","body":"package gnorkle\n\n// Whitelist is used to manage which agents are allowed to interact.\ntype Whitelist interface {\n\tClearAddresses()\n\tAddAddresses(addresses []string)\n\tRemoveAddress(address_XXX string)\n\tHasDefinition() bool\n\tHasAddress(address_XXX string) bool\n}\n\n// ClearWhitelist clears the whitelist of the instance or feed depending on the feed ID.\nfunc (i *Instance) ClearWhitelist(feedID string) error {\n\tif feedID == \"\" {\n\t\ti.whitelist.ClearAddresses()\n\t\treturn nil\n\t}\n\n\tfeedWithWhitelist, err := i.getFeedWithWhitelist(feedID)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfeedWithWhitelist.ClearAddresses()\n\treturn nil\n}\n\n// AddToWhitelist adds the given addresses to the whitelist of the instance or feed depending on the feed ID.\nfunc (i *Instance) AddToWhitelist(feedID string, addresses []string) error {\n\tif feedID == \"\" {\n\t\ti.whitelist.AddAddresses(addresses)\n\t\treturn nil\n\t}\n\n\tfeedWithWhitelist, err := i.getFeedWithWhitelist(feedID)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfeedWithWhitelist.AddAddresses(addresses)\n\treturn nil\n}\n\n// RemoveFromWhitelist removes the given address from the whitelist of the instance or feed depending on the feed ID.\nfunc (i *Instance) RemoveFromWhitelist(feedID string, address_XXX string) error {\n\tif feedID == \"\" {\n\t\ti.whitelist.RemoveAddress(address_XXX)\n\t\treturn nil\n\t}\n\n\tfeedWithWhitelist, err := i.getFeedWithWhitelist(feedID)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfeedWithWhitelist.RemoveAddress(address_XXX)\n\treturn nil\n}\n\n// addressWhiteListed returns true if:\n// - the feed has a white list and the address is whitelisted, or\n// - the feed has no white list and the instance has a white list and the address is whitelisted, or\n// - the feed has no white list and the instance has no white list.\nfunc addressIsWhitelisted(instanceWhitelist, feedWhitelist Whitelist, address_XXX string, instanceWhitelistedOverride *bool) bool {\n\t// A feed whitelist takes priority, so it will return false if the feed has a whitelist and the caller is\n\t// not a part of it. An empty whitelist defers to the instance whitelist.\n\tif feedWhitelist != nil {\n\t\tif feedWhitelist.HasDefinition() \u0026\u0026 !feedWhitelist.HasAddress(address_XXX) {\n\t\t\treturn false\n\t\t}\n\n\t\t// Getting to this point means that one of the following is true:\n\t\t// - the feed has no defined whitelist (so it can't possibly have the address whitelisted)\n\t\t// - the feed has a defined whitelist and the caller is a part of it\n\t\t//\n\t\t// In this case, we can be sure that the boolean indicating whether the feed has this address whitelisted\n\t\t// is equivalent to the boolean indicating whether the feed has a defined whitelist.\n\t\tif feedWhitelist.HasDefinition() {\n\t\t\treturn true\n\t\t}\n\t}\n\n\tif instanceWhitelistedOverride != nil {\n\t\treturn *instanceWhitelistedOverride\n\t}\n\n\t// We were unable able to determine whether this address is allowed after looking at the feed whitelist,\n\t// so fall back to the instance whitelist. A complete absence of values in the instance whitelist means\n\t// that the instance has no whitelist so we can return true because everything is allowed by default.\n\tif instanceWhitelist == nil || !instanceWhitelist.HasDefinition() {\n\t\treturn true\n\t}\n\n\t// The instance whitelist is defined so if the address is present then it is allowed.\n\treturn instanceWhitelist.HasAddress(address_XXX)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"0yUUnmHfBS8Q4ZAp6aUZUcwf1zjb3yUca7SIZhPWuMl4ZPMtAfJafu7Bq+CLNBsPHP1HN3rhazf8wppO+XGeBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"storage","path":"gno.land/p/demo/gnorkle/storage","files":[{"name":"errors.gno","body":"package storage\n\nimport \"errors\"\n\nvar ErrUndefined = errors.New(\"undefined storage\")\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/storage\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"PuYQhb9V9/2dgEFH+O3pIgyXic5t/VRRxIx+bcSkJD2MvY8MfuZXAwqOEgU2EjKWhbtq7SVRIb+FxlFxlT0mAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"simple","path":"gno.land/p/demo/gnorkle/storage/simple","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/storage/simple\"\ngno = \"0.9\"\n"},{"name":"storage.gno","body":"package simple\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/demo/gnorkle/feed\"\n\t\"gno.land/p/demo/gnorkle/storage\"\n)\n\n// Storage is simple, bounded storage for published feed values.\ntype Storage struct {\n\tvalues    []feed.Value\n\tmaxValues uint\n}\n\n// NewStorage creates a new Storage with the given maximum number of values.\n// If maxValues is 0, the storage is bounded to a size of one. If this is not desirable,\n// then don't provide a value of 0.\nfunc NewStorage(maxValues uint) *Storage {\n\tif maxValues == 0 {\n\t\tmaxValues = 1\n\t}\n\n\treturn \u0026Storage{\n\t\tmaxValues: maxValues,\n\t}\n}\n\n// Put adds a new value to the storage. If the storage is full, the oldest value\n// is removed. If maxValues is 0, the storage is bounded to a size of one.\nfunc (s *Storage) Put(value string) error {\n\tif s == nil {\n\t\treturn storage.ErrUndefined\n\t}\n\n\ts.values = append(s.values, feed.Value{String: value, Time: time.Now()})\n\tif uint(len(s.values)) \u003e s.maxValues {\n\t\ts.values = s.values[1:]\n\t}\n\n\treturn nil\n}\n\n// GetLatest returns the most recently added value, or an empty value if none exist.\nfunc (s Storage) GetLatest() feed.Value {\n\tif len(s.values) == 0 {\n\t\treturn feed.Value{}\n\t}\n\n\treturn s.values[len(s.values)-1]\n}\n\n// GetHistory returns all values in the storage, from oldest to newest.\nfunc (s Storage) GetHistory() []feed.Value {\n\treturn s.values\n}\n"},{"name":"storage_test.gno","body":"package simple_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/gnorkle/storage\"\n\t\"gno.land/p/demo/gnorkle/storage/simple\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestStorage(t *testing.T) {\n\tvar undefinedStorage *simple.Storage\n\terr := undefinedStorage.Put(\"\")\n\tuassert.ErrorIs(t, err, storage.ErrUndefined, \"expected storage.ErrUndefined on undefined storage\")\n\n\ttests := []struct {\n\t\tname                      string\n\t\tvaluesToPut               []string\n\t\texpLatestValueString      string\n\t\texpLatestValueTimeIsZero  bool\n\t\texpHistoricalValueStrings []string\n\t}{\n\t\t{\n\t\t\tname:                     \"empty\",\n\t\t\texpLatestValueTimeIsZero: true,\n\t\t},\n\t\t{\n\t\t\tname:                      \"one value\",\n\t\t\tvaluesToPut:               []string{\"one\"},\n\t\t\texpLatestValueString:      \"one\",\n\t\t\texpHistoricalValueStrings: []string{\"one\"},\n\t\t},\n\t\t{\n\t\t\tname:                      \"two values\",\n\t\t\tvaluesToPut:               []string{\"one\", \"two\"},\n\t\t\texpLatestValueString:      \"two\",\n\t\t\texpHistoricalValueStrings: []string{\"one\", \"two\"},\n\t\t},\n\t\t{\n\t\t\tname:                      \"three values\",\n\t\t\tvaluesToPut:               []string{\"one\", \"two\", \"three\"},\n\t\t\texpLatestValueString:      \"three\",\n\t\t\texpHistoricalValueStrings: []string{\"two\", \"three\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tsimpleStorage := simple.NewStorage(2)\n\t\t\tfor _, value := range tt.valuesToPut {\n\t\t\t\terr := simpleStorage.Put(value)\n\t\t\t\turequire.NoError(t, err, \"unexpected error putting value in storage\")\n\t\t\t}\n\n\t\t\tlatestValue := simpleStorage.GetLatest()\n\t\t\tuassert.Equal(t, tt.expLatestValueString, latestValue.String)\n\t\t\tuassert.Equal(t, tt.expLatestValueTimeIsZero, latestValue.Time.IsZero())\n\n\t\t\thistoricalValues := simpleStorage.GetHistory()\n\t\t\turequire.Equal(t, len(tt.expHistoricalValueStrings), len(historicalValues), \"historical values length does not match\")\n\n\t\t\tfor i, expValue := range tt.expHistoricalValueStrings {\n\t\t\t\tuassert.Equal(t, historicalValues[i].String, expValue)\n\t\t\t\turequire.False(t, historicalValues[i].Time.IsZero(), ufmt.Sprintf(\"unexpeced zero time for historical value at index %d\", i))\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"JNHKAu6k1Qosxge4glLOiGifnqXQmXIISK3YwwJUWYh2wwhwg8yKHjRAFOTlS/o11N0ZFiCmKLiZv209cWq6CA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"single","path":"gno.land/p/demo/gnorkle/ingesters/single","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/ingesters/single\"\ngno = \"0.9\"\n"},{"name":"ingester.gno","body":"package single\n\nimport (\n\t\"gno.land/p/demo/gnorkle/gnorkle\"\n\t\"gno.land/p/demo/gnorkle/ingester\"\n)\n\n// ValueIngester is an ingester that ingests a single value.\ntype ValueIngester struct {\n\tvalue string\n}\n\n// Type returns the type of the ingester.\nfunc (i *ValueIngester) Type() ingester.Type {\n\treturn ingester.TypeSingle\n}\n\n// Ingest ingests a value provided by the given agent address.\nfunc (i *ValueIngester) Ingest(value, providerAddress string) (bool, error) {\n\tif i == nil {\n\t\treturn false, ingester.ErrUndefined\n\t}\n\n\ti.value = value\n\treturn true, nil\n}\n\n// CommitValue commits the ingested value to the given storage instance.\nfunc (i *ValueIngester) CommitValue(valueStorer gnorkle.Storage, providerAddress string) error {\n\tif i == nil {\n\t\treturn ingester.ErrUndefined\n\t}\n\n\treturn valueStorer.Put(i.value)\n}\n"},{"name":"ingester_test.gno","body":"package single_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/gnorkle/ingester\"\n\t\"gno.land/p/demo/gnorkle/ingesters/single\"\n\t\"gno.land/p/demo/gnorkle/storage/simple\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestValueIngester(t *testing.T) {\n\tstorage := simple.NewStorage(1)\n\n\tvar undefinedIngester *single.ValueIngester\n\t_, err := undefinedIngester.Ingest(\"asdf\", \"gno11111\")\n\tuassert.ErrorIs(t, err, ingester.ErrUndefined, \"undefined ingester call to Ingest should return ingester.ErrUndefined\")\n\n\terr = undefinedIngester.CommitValue(storage, \"gno11111\")\n\tuassert.ErrorIs(t, err, ingester.ErrUndefined, \"undefined ingester call to CommitValue should return ingester.ErrUndefined\")\n\n\tvar valueIngester single.ValueIngester\n\ttyp := valueIngester.Type()\n\tuassert.Equal(t, int(ingester.TypeSingle), int(typ), \"single value ingester should return type ingester.TypeSingle\")\n\n\tingestValue := \"value\"\n\tautocommit, err := valueIngester.Ingest(ingestValue, \"gno11111\")\n\tuassert.True(t, autocommit, \"single value ingester should return autocommit true\")\n\tuassert.NoError(t, err)\n\n\terr = valueIngester.CommitValue(storage, \"gno11111\")\n\tuassert.NoError(t, err)\n\n\tlatestValue := storage.GetLatest()\n\tuassert.Equal(t, ingestValue, latestValue.String)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"uwEs8LTi0zgy42GLLUVCdSE1nvGbFur4xoainq2qPFOJrA1mcdcHZkzNynOviSzWDLHKdqHr6FcAB+dCJ7AtDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"static","path":"gno.land/p/demo/gnorkle/feeds/static","files":[{"name":"feed.gno","body":"package static\n\nimport (\n\t\"bufio\"\n\t\"bytes\"\n\t\"errors\"\n\n\t\"gno.land/p/demo/gnorkle/feed\"\n\t\"gno.land/p/demo/gnorkle/gnorkle\"\n\t\"gno.land/p/demo/gnorkle/ingesters/single\"\n\t\"gno.land/p/demo/gnorkle/message\"\n\t\"gno.land/p/demo/gnorkle/storage/simple\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Feed is a static feed.\ntype Feed struct {\n\tid            string\n\tisLocked      bool\n\tvalueDataType string\n\tingester      gnorkle.Ingester\n\tstorage       gnorkle.Storage\n\ttasks         []feed.Task\n}\n\n// NewFeed creates a new static feed.\nfunc NewFeed(\n\tid string,\n\tvalueDataType string,\n\tingester gnorkle.Ingester,\n\tstorage gnorkle.Storage,\n\ttasks ...feed.Task,\n) *Feed {\n\treturn \u0026Feed{\n\t\tid:            id,\n\t\tvalueDataType: valueDataType,\n\t\tingester:      ingester,\n\t\tstorage:       storage,\n\t\ttasks:         tasks,\n\t}\n}\n\n// NewSingleValueFeed is a convenience function  for creating a static feed\n// that autocommits a value after a single ingestion.\nfunc NewSingleValueFeed(\n\tid string,\n\tvalueDataType string,\n\ttasks ...feed.Task,\n) *Feed {\n\treturn NewFeed(\n\t\tid,\n\t\tvalueDataType,\n\t\t\u0026single.ValueIngester{},\n\t\tsimple.NewStorage(1),\n\t\ttasks...,\n\t)\n}\n\n// ID returns the feed's ID.\nfunc (f Feed) ID() string {\n\treturn f.id\n}\n\n// Type returns the feed's type.\nfunc (f Feed) Type() feed.Type {\n\treturn feed.TypeStatic\n}\n\n// Ingest ingests a message into the feed. It either adds the value to the ingester's\n// pending values or commits the value to the storage.\nfunc (f *Feed) Ingest(funcType message.FuncType, msg, providerAddress string) error {\n\tif f == nil {\n\t\treturn feed.ErrUndefined\n\t}\n\n\tif f.isLocked {\n\t\treturn errors.New(\"feed locked\")\n\t}\n\n\tswitch funcType {\n\tcase message.FuncTypeIngest:\n\t\t// Autocommit the ingester's value if it's a single value ingester\n\t\t// because this is a static feed and this is the only value it will ever have.\n\t\tif canAutoCommit, err := f.ingester.Ingest(msg, providerAddress); canAutoCommit \u0026\u0026 err == nil {\n\t\t\tif err := f.ingester.CommitValue(f.storage, providerAddress); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tf.isLocked = true\n\t\t} else if err != nil {\n\t\t\treturn err\n\t\t}\n\n\tcase message.FuncTypeCommit:\n\t\tif err := f.ingester.CommitValue(f.storage, providerAddress); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tf.isLocked = true\n\n\tdefault:\n\t\treturn errors.New(\"invalid message function \" + string(funcType))\n\t}\n\n\treturn nil\n}\n\n// Value returns the feed's latest value, it's data type, and whether or not it can\n// be safely consumed. In this case it uses `f.isLocked` because, this being a static\n// feed, it will only ever have one value; once that value is committed the feed is locked\n// and there is a valid, non-empty value to consume.\nfunc (f Feed) Value() (feed.Value, string, bool) {\n\treturn f.storage.GetLatest(), f.valueDataType, f.isLocked\n}\n\n// MarshalJSON marshals the components of the feed that are needed for\n// an agent to execute tasks and send values for ingestion.\nfunc (f Feed) MarshalJSON() ([]byte, error) {\n\tbuf := new(bytes.Buffer)\n\tw := bufio.NewWriter(buf)\n\n\tw.Write([]byte(\n\t\t`{\"id\":\"` + f.id +\n\t\t\t`\",\"type\":\"` + ufmt.Sprintf(\"%d\", int(f.Type())) +\n\t\t\t`\",\"value_type\":\"` + f.valueDataType +\n\t\t\t`\",\"tasks\":[`),\n\t)\n\n\tfirst := true\n\tfor _, task := range f.tasks {\n\t\tif !first {\n\t\t\tw.WriteString(\",\")\n\t\t}\n\n\t\ttaskJSON, err := task.MarshalJSON()\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tw.Write(taskJSON)\n\t\tfirst = false\n\t}\n\n\tw.Write([]byte(\"]}\"))\n\tw.Flush()\n\n\treturn buf.Bytes(), nil\n}\n\n// Tasks returns the feed's tasks. This allows task consumers to extract task\n// contents without having to marshal the entire feed.\nfunc (f Feed) Tasks() []feed.Task {\n\treturn f.tasks\n}\n\n// IsActive returns true if the feed is accepting ingestion requests from agents.\nfunc (f Feed) IsActive() bool {\n\treturn !f.isLocked\n}\n"},{"name":"feed_test.gno","body":"package static_test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\n\t\"gno.land/p/demo/gnorkle/feed\"\n\t\"gno.land/p/demo/gnorkle/feeds/static\"\n\t\"gno.land/p/demo/gnorkle/gnorkle\"\n\t\"gno.land/p/demo/gnorkle/ingester\"\n\t\"gno.land/p/demo/gnorkle/message\"\n\t\"gno.land/p/demo/gnorkle/storage/simple\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\ntype mockIngester struct {\n\tcanAutoCommit   bool\n\tingestErr       error\n\tcommitErr       error\n\tvalue           string\n\tproviderAddress string\n}\n\nfunc (i mockIngester) Type() ingester.Type {\n\treturn ingester.Type(0)\n}\n\nfunc (i *mockIngester) Ingest(value, providerAddress string) (bool, error) {\n\tif i.ingestErr != nil {\n\t\treturn false, i.ingestErr\n\t}\n\n\ti.value = value\n\ti.providerAddress = providerAddress\n\treturn i.canAutoCommit, nil\n}\n\nfunc (i *mockIngester) CommitValue(storage gnorkle.Storage, providerAddress string) error {\n\tif i.commitErr != nil {\n\t\treturn i.commitErr\n\t}\n\n\treturn storage.Put(i.value)\n}\n\nfunc TestNewSingleValueFeed(t *testing.T) {\n\tstaticFeed := static.NewSingleValueFeed(\"1\", \"\")\n\n\tuassert.Equal(t, \"1\", staticFeed.ID())\n\tuassert.Equal(t, int(feed.TypeStatic), int(staticFeed.Type()))\n}\n\nfunc TestFeed_Ingest(t *testing.T) {\n\tvar undefinedFeed *static.Feed\n\terr := undefinedFeed.Ingest(\"\", \"\", \"\")\n\tuassert.ErrorIs(t, err, feed.ErrUndefined)\n\n\ttests := []struct {\n\t\tname               string\n\t\tingester           *mockIngester\n\t\tverifyIsLocked     bool\n\t\tdoCommit           bool\n\t\tfuncType           message.FuncType\n\t\tmsg                string\n\t\tproviderAddress    string\n\t\texpFeedValueString string\n\t\texpErrText         string\n\t\texpIsActive        bool\n\t}{\n\t\t{\n\t\t\tname:        \"func invalid error\",\n\t\t\tingester:    \u0026mockIngester{},\n\t\t\tfuncType:    message.FuncType(\"derp\"),\n\t\t\texpErrText:  \"invalid message function derp\",\n\t\t\texpIsActive: true,\n\t\t},\n\t\t{\n\t\t\tname: \"func ingest ingest error\",\n\t\t\tingester: \u0026mockIngester{\n\t\t\t\tingestErr: errors.New(\"ingest error\"),\n\t\t\t},\n\t\t\tfuncType:    message.FuncTypeIngest,\n\t\t\texpErrText:  \"ingest error\",\n\t\t\texpIsActive: true,\n\t\t},\n\t\t{\n\t\t\tname: \"func ingest commit error\",\n\t\t\tingester: \u0026mockIngester{\n\t\t\t\tcommitErr:     errors.New(\"commit error\"),\n\t\t\t\tcanAutoCommit: true,\n\t\t\t},\n\t\t\tfuncType:    message.FuncTypeIngest,\n\t\t\texpErrText:  \"commit error\",\n\t\t\texpIsActive: true,\n\t\t},\n\t\t{\n\t\t\tname: \"func commit commit error\",\n\t\t\tingester: \u0026mockIngester{\n\t\t\t\tcommitErr:     errors.New(\"commit error\"),\n\t\t\t\tcanAutoCommit: true,\n\t\t\t},\n\t\t\tfuncType:    message.FuncTypeCommit,\n\t\t\texpErrText:  \"commit error\",\n\t\t\texpIsActive: true,\n\t\t},\n\t\t{\n\t\t\tname:            \"only ingest\",\n\t\t\tingester:        \u0026mockIngester{},\n\t\t\tfuncType:        message.FuncTypeIngest,\n\t\t\tmsg:             \"still active feed\",\n\t\t\tproviderAddress: \"gno1234\",\n\t\t\texpIsActive:     true,\n\t\t},\n\t\t{\n\t\t\tname:               \"ingest autocommit\",\n\t\t\tingester:           \u0026mockIngester{canAutoCommit: true},\n\t\t\tfuncType:           message.FuncTypeIngest,\n\t\t\tmsg:                \"still active feed\",\n\t\t\tproviderAddress:    \"gno1234\",\n\t\t\texpFeedValueString: \"still active feed\",\n\t\t\tverifyIsLocked:     true,\n\t\t},\n\t\t{\n\t\t\tname:           \"commit no value\",\n\t\t\tingester:       \u0026mockIngester{},\n\t\t\tfuncType:       message.FuncTypeCommit,\n\t\t\tmsg:            \"shouldn't be stored\",\n\t\t\tverifyIsLocked: true,\n\t\t},\n\t\t{\n\t\t\tname:               \"ingest then commmit\",\n\t\t\tingester:           \u0026mockIngester{},\n\t\t\tfuncType:           message.FuncTypeIngest,\n\t\t\tmsg:                \"blahblah\",\n\t\t\tdoCommit:           true,\n\t\t\texpFeedValueString: \"blahblah\",\n\t\t\tverifyIsLocked:     true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tstaticFeed := static.NewFeed(\n\t\t\t\t\"1\",\n\t\t\t\t\"string\",\n\t\t\t\ttt.ingester,\n\t\t\t\tsimple.NewStorage(1),\n\t\t\t\tnil,\n\t\t\t)\n\n\t\t\tvar errText string\n\t\t\tif err := staticFeed.Ingest(tt.funcType, tt.msg, tt.providerAddress); err != nil {\n\t\t\t\terrText = err.Error()\n\t\t\t}\n\n\t\t\turequire.Equal(t, tt.expErrText, errText)\n\n\t\t\tif tt.doCommit {\n\t\t\t\terr := staticFeed.Ingest(message.FuncTypeCommit, \"\", \"\")\n\t\t\t\turequire.NoError(t, err, \"follow up commit failed\")\n\t\t\t}\n\n\t\t\tif tt.verifyIsLocked {\n\t\t\t\terrText = \"\"\n\t\t\t\tif err := staticFeed.Ingest(tt.funcType, tt.msg, tt.providerAddress); err != nil {\n\t\t\t\t\terrText = err.Error()\n\t\t\t\t}\n\n\t\t\t\turequire.Equal(t, \"feed locked\", errText)\n\t\t\t}\n\n\t\t\tuassert.Equal(t, tt.providerAddress, tt.ingester.providerAddress)\n\n\t\t\tfeedValue, dataType, isLocked := staticFeed.Value()\n\t\t\tuassert.Equal(t, tt.expFeedValueString, feedValue.String)\n\t\t\tuassert.Equal(t, \"string\", dataType)\n\t\t\tuassert.Equal(t, tt.verifyIsLocked, isLocked)\n\t\t\tuassert.Equal(t, tt.expIsActive, staticFeed.IsActive())\n\t\t})\n\t}\n}\n\ntype mockTask struct {\n\terr   error\n\tvalue string\n}\n\nfunc (t mockTask) MarshalJSON() ([]byte, error) {\n\tif t.err != nil {\n\t\treturn nil, t.err\n\t}\n\n\treturn []byte(`{\"value\":\"` + t.value + `\"}`), nil\n}\n\nfunc TestFeed_Tasks(t *testing.T) {\n\tid := \"99\"\n\tvalueDataType := \"int\"\n\n\ttests := []struct {\n\t\tname       string\n\t\ttasks      []feed.Task\n\t\texpErrText string\n\t\texpJSON    string\n\t}{\n\t\t{\n\t\t\tname:    \"no tasks\",\n\t\t\texpJSON: `{\"id\":\"99\",\"type\":\"0\",\"value_type\":\"int\",\"tasks\":[]}`,\n\t\t},\n\t\t{\n\t\t\tname: \"marshal error\",\n\t\t\ttasks: []feed.Task{\n\t\t\t\tmockTask{err: errors.New(\"marshal error\")},\n\t\t\t},\n\t\t\texpErrText: \"marshal error\",\n\t\t},\n\t\t{\n\t\t\tname: \"one task\",\n\t\t\ttasks: []feed.Task{\n\t\t\t\tmockTask{value: \"single\"},\n\t\t\t},\n\t\t\texpJSON: `{\"id\":\"99\",\"type\":\"0\",\"value_type\":\"int\",\"tasks\":[{\"value\":\"single\"}]}`,\n\t\t},\n\t\t{\n\t\t\tname: \"two tasks\",\n\t\t\ttasks: []feed.Task{\n\t\t\t\tmockTask{value: \"first\"},\n\t\t\t\tmockTask{value: \"second\"},\n\t\t\t},\n\t\t\texpJSON: `{\"id\":\"99\",\"type\":\"0\",\"value_type\":\"int\",\"tasks\":[{\"value\":\"first\"},{\"value\":\"second\"}]}`,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tstaticFeed := static.NewSingleValueFeed(\n\t\t\t\tid,\n\t\t\t\tvalueDataType,\n\t\t\t\ttt.tasks...,\n\t\t\t)\n\n\t\t\turequire.Equal(t, len(tt.tasks), len(staticFeed.Tasks()))\n\n\t\t\tvar errText string\n\t\t\tjson, err := staticFeed.MarshalJSON()\n\t\t\tif err != nil {\n\t\t\t\terrText = err.Error()\n\t\t\t}\n\n\t\t\turequire.Equal(t, tt.expErrText, errText)\n\t\t\turequire.Equal(t, tt.expJSON, string(json))\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/gnorkle/feeds/static\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"s5NVBCahDSVR65acBWzvxs6BaBCMHcdX2xUGswiEUDnwf6h6z91Z367sNoT8iLOPonYA7sO3YItoPsvZWQ6FAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"svg","path":"gno.land/p/demo/svg","files":[{"name":"doc.gno","body":"/*\nPackage svg is a minimalist and extensible SVG generation library for Gno.\n\nIt provides a structured way to create and compose SVG elements such as rectangles, circles, text, paths, and more. The package is designed to be modular and developer-friendly, enabling optional attributes and method chaining for ease of use.\n\nEach SVG element embeds a BaseAttrs struct, which supports common SVG attributes like `id`, `class`, `style`, `fill`, `stroke`, and `transform`.\n\nCanvas objects represent the root SVG container and support global dimensions, viewBox configuration, embedded styles, and element composition.\n\nExample:\n\n\timport \"gno.land/p/demo/svg\"\n\n\tfunc Foo() string {\n\t\tcanvas := svg.NewCanvas(200, 200).WithViewBox(0, 0, 200, 200)\n\t\tcanvas.AddStyle(\".my-rect\", \"stroke:black;stroke-width:2\")\n\t\tcanvas.Append(\n\t\t\tsvg.NewRectangle(60, 40, 100, 50, \"red\").WithClass(\"my-rect\"),\n\t\t\tsvg.NewCircle(50, 80, 40, \"blue\"),\n\t\t\t\u0026svg.Path{D: `M 10,30\n\t\t\tA 20,20 0,0,1 50,30\n\t\t\t\tA 20,20 0,0,1  90,30\n\t\t\t\tQ 90,60 50,90\n\t\t\t\tQ 10,60 10,30 z`, Fill: \"magenta\"},\n\t\t\tsvg.NewText(20, 50, \"Hello SVG\", \"black\"),\n\t\t)\n\t\tmysvg := canvas.Base64()\n\t}\n*/\npackage svg // import \"gno.land/p/demo/svg\"\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/svg\"\ngno = \"0.9\"\n"},{"name":"svg.gno","body":"package svg\n\nimport (\n\t\"encoding/base64\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Canvas struct {\n\tWidth, Height int\n\tViewBox       string\n\tElems         []Elem\n\tStyle         *avl.Tree\n}\n\ntype Elem interface{ String() string }\n\nfunc NewCanvas(width, height int) *Canvas {\n\treturn \u0026Canvas{\n\t\tWidth:  width,\n\t\tHeight: height,\n\t\tStyle:  nil,\n\t}\n}\n\nfunc (c *Canvas) AddStyle(key, value string) *Canvas {\n\tif c.Style == nil {\n\t\tc.Style = avl.NewTree()\n\t}\n\tc.Style.Set(key, value)\n\treturn c\n}\n\nfunc (c *Canvas) WithViewBox(x, y, width, height int) *Canvas {\n\tc.ViewBox = ufmt.Sprintf(\"%d %d %d %d\", x, y, width, height)\n\treturn c\n}\n\n// Render renders your canvas\nfunc (c Canvas) Render(alt string) string {\n\tbase64SVG := base64.StdEncoding.EncodeToString([]byte(c.String()))\n\treturn ufmt.Sprintf(\"![%s](data:image/svg+xml;base64,%s)\", alt, base64SVG)\n}\n\nfunc (c Canvas) String() string {\n\tout := \"\"\n\tout += ufmt.Sprintf(`\u003csvg xmlns=\"http://www.w3.org/2000/svg\" width=\"%d\" height=\"%d\" viewBox=\"%s\"\u003e`, c.Width, c.Height, c.ViewBox)\n\tif c.Style != nil {\n\t\tout += \"\u003cstyle\u003e\"\n\t\tc.Style.Iterate(\"\", \"\", func(k string, val interface{}) bool {\n\t\t\tv := val.(string)\n\t\t\tout += ufmt.Sprintf(\"%s{%s}\", k, v)\n\t\t\treturn false\n\t\t})\n\t\tout += \"\u003c/style\u003e\"\n\t}\n\tfor _, elem := range c.Elems {\n\t\tout += elem.String()\n\t}\n\tout += \"\u003c/svg\u003e\"\n\treturn out\n}\n\nfunc (c Canvas) Base64() string {\n\tout := c.String()\n\treturn base64.StdEncoding.EncodeToString([]byte(out))\n}\n\nfunc (c *Canvas) Append(elem ...Elem) {\n\tc.Elems = append(c.Elems, elem...)\n}\n\ntype BaseAttrs struct {\n\tID          string\n\tClass       string\n\tStyle       string\n\tStroke      string\n\tStrokeWidth string\n\tOpacity     string\n\tTransform   string\n\tVisibility  string\n}\n\nfunc (b BaseAttrs) String() string {\n\tvar elems []string\n\n\tif b.ID != \"\" {\n\t\telems = append(elems, `id=\"`+b.ID+`\"`)\n\t}\n\tif b.Class != \"\" {\n\t\telems = append(elems, `class=\"`+b.Class+`\"`)\n\t}\n\tif b.Style != \"\" {\n\t\telems = append(elems, `style=\"`+b.Style+`\"`)\n\t}\n\tif b.Stroke != \"\" {\n\t\telems = append(elems, `stroke=\"`+b.Stroke+`\"`)\n\t}\n\tif b.StrokeWidth != \"\" {\n\t\telems = append(elems, `stroke-width=\"`+b.StrokeWidth+`\"`)\n\t}\n\tif b.Opacity != \"\" {\n\t\telems = append(elems, `opacity=\"`+b.Opacity+`\"`)\n\t}\n\tif b.Transform != \"\" {\n\t\telems = append(elems, `transform=\"`+b.Transform+`\"`)\n\t}\n\tif b.Visibility != \"\" {\n\t\telems = append(elems, `visibility=\"`+b.Visibility+`\"`)\n\t}\n\tif len(elems) == 0 {\n\t\treturn \"\"\n\t}\n\treturn strings.Join(elems, \" \")\n}\n\ntype Circle struct {\n\tCX   int // center X\n\tCY   int // center Y\n\tR    int // radius\n\tFill string\n\tAttr BaseAttrs\n}\n\nfunc (c Circle) String() string {\n\treturn ufmt.Sprintf(`\u003ccircle cx=\"%d\" cy=\"%d\" r=\"%d\" fill=\"%s\" %s/\u003e`, c.CX, c.CY, c.R, c.Fill, c.Attr.String())\n}\n\nfunc NewCircle(cx, cy, r int, fill string) *Circle {\n\treturn \u0026Circle{\n\t\tCX:   cx,\n\t\tCY:   cy,\n\t\tR:    r,\n\t\tFill: fill,\n\t}\n}\n\nfunc (c *Circle) WithClass(class string) *Circle {\n\tc.Attr.Class = class\n\treturn c\n}\n\ntype Ellipse struct {\n\tCX   int // center X\n\tCY   int // center Y\n\tRX   int // radius X\n\tRY   int // radius Y\n\tFill string\n\tAttr BaseAttrs\n}\n\nfunc (e Ellipse) String() string {\n\treturn ufmt.Sprintf(`\u003cellipse cx=\"%d\" cy=\"%d\" rx=\"%d\" ry=\"%d\" fill=\"%s\" %s/\u003e`, e.CX, e.CY, e.RX, e.RY, e.Fill, e.Attr.String())\n}\n\nfunc NewEllipse(cx, cy int, fill string) *Ellipse {\n\treturn \u0026Ellipse{\n\t\tCX:   cx,\n\t\tCY:   cy,\n\t\tFill: fill,\n\t}\n}\n\nfunc (e *Ellipse) WithClass(class string) *Ellipse {\n\te.Attr.Class = class\n\treturn e\n}\n\ntype Rectangle struct {\n\tX, Y, Width, Height int\n\tRX, RY              int // corner radiuses\n\tFill                string\n\tAttr                BaseAttrs\n}\n\nfunc (r Rectangle) String() string {\n\treturn ufmt.Sprintf(`\u003crect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" rx=\"%d\" ry=\"%d\" fill=\"%s\" %s/\u003e`, r.X, r.Y, r.Width, r.Height, r.RX, r.RY, r.Fill, r.Attr.String())\n}\n\nfunc NewRectangle(x, y, width, height int, fill string) *Rectangle {\n\treturn \u0026Rectangle{\n\t\tX:      x,\n\t\tY:      y,\n\t\tWidth:  width,\n\t\tHeight: height,\n\t\tFill:   fill,\n\t}\n}\n\nfunc (r *Rectangle) WithClass(class string) *Rectangle {\n\tr.Attr.Class = class\n\treturn r\n}\n\ntype Path struct {\n\tD    string\n\tFill string\n\tAttr BaseAttrs\n}\n\nfunc (p Path) String() string {\n\treturn ufmt.Sprintf(`\u003cpath d=\"%s\" fill=\"%s\" %s/\u003e`, p.D, p.Fill, p.Attr.String())\n}\n\nfunc NewPath(d, fill string) *Path {\n\treturn \u0026Path{\n\t\tD:    d,\n\t\tFill: fill,\n\t}\n}\n\nfunc (p *Path) WithClass(class string) *Path {\n\tp.Attr.Class = class\n\treturn p\n}\n\ntype Polygon struct { // closed shape\n\tPoints string\n\tFill   string\n\tAttr   BaseAttrs\n}\n\nfunc (p Polygon) String() string {\n\treturn ufmt.Sprintf(`\u003cpolygon points=\"%s\" fill=\"%s\" %s/\u003e`, p.Points, p.Fill, p.Attr.String())\n}\n\nfunc NewPolygon(points, fill string) *Polygon {\n\treturn \u0026Polygon{\n\t\tPoints: points,\n\t\tFill:   fill,\n\t}\n}\n\nfunc (p *Polygon) WithClass(class string) *Polygon {\n\tp.Attr.Class = class\n\treturn p\n}\n\ntype Polyline struct { // polygon but not necessarily closed\n\tPoints string\n\tFill   string\n\tAttr   BaseAttrs\n}\n\nfunc (p Polyline) String() string {\n\treturn ufmt.Sprintf(`\u003cpolyline points=\"%s\" fill=\"%s\" %s/\u003e`, p.Points, p.Fill, p.Attr.String())\n}\n\nfunc NewPolyline(points, fill string) *Polyline {\n\treturn \u0026Polyline{\n\t\tPoints: points,\n\t\tFill:   fill,\n\t}\n}\n\nfunc (p *Polyline) WithClass(class string) *Polyline {\n\tp.Attr.Class = class\n\treturn p\n}\n\ntype Text struct {\n\tX, Y       int\n\tDX, DY     int // shift text pos horizontally/ vertically\n\tRotate     string\n\tText, Fill string\n\tAttr       BaseAttrs\n}\n\nfunc (c Text) String() string {\n\treturn ufmt.Sprintf(`\u003ctext x=\"%d\" y=\"%d\" dx=\"%d\" dy=\"%d\" rotate=\"%s\" fill=\"%s\" %s\u003e%s\u003c/text\u003e`, c.X, c.Y, c.DX, c.DY, c.Rotate, c.Fill, c.Attr.String(), c.Text)\n}\n\nfunc NewText(x, y int, text, fill string) *Text {\n\treturn \u0026Text{\n\t\tX:    x,\n\t\tY:    y,\n\t\tText: text,\n\t\tFill: fill,\n\t}\n}\n\nfunc (c *Text) WithClass(class string) *Text {\n\tc.Attr.Class = class\n\treturn c\n}\n\ntype Group struct {\n\tElems []Elem\n\tFill  string\n\tAttr  BaseAttrs\n}\n\nfunc (g Group) String() string {\n\tout := \"\"\n\tfor _, e := range g.Elems {\n\t\tout += e.String()\n\t}\n\treturn ufmt.Sprintf(`\u003cg fill=\"%s\" %s\u003e%s\u003c/g\u003e`, g.Fill, g.Attr.String(), out)\n}\n\nfunc NewGroup(fill string) *Group {\n\treturn \u0026Group{\n\t\tFill: fill,\n\t}\n}\n\nfunc (g *Group) Append(elem ...Elem) {\n\tg.Elems = append(g.Elems, elem...)\n}\n\nfunc (g *Group) WithClass(class string) *Group {\n\tg.Attr.Class = class\n\treturn g\n}\n"},{"name":"z0_filetest.gno","body":"// PKGPATH: gno.land/p/demo/svg_test\npackage svg_test\n\nimport \"gno.land/p/demo/svg\"\n\nfunc main() {\n\tcanvas := svg.Canvas{Width: 500, Height: 500}\n\tcanvas.Append(\n\t\tsvg.Rectangle{X: 50, Y: 50, Width: 100, Height: 100, Fill: \"red\"},\n\t\tsvg.Circle{CX: 100, CY: 100, R: 50, Fill: \"blue\"},\n\t\tsvg.Text{X: 100, Y: 100, Text: \"hello world!\", Fill: \"magenta\"},\n\t)\n\tcanvas.Append(\n\t\tsvg.NewCircle(100, 100, 50, \"blue\").WithClass(\"toto\"),\n\t)\n\tprintln(canvas)\n}\n\n// Output:\n// \u003csvg xmlns=\"http://www.w3.org/2000/svg\" width=\"500\" height=\"500\" viewBox=\"\"\u003e\u003crect x=\"50\" y=\"50\" width=\"100\" height=\"100\" rx=\"0\" ry=\"0\" fill=\"red\" /\u003e\u003ccircle cx=\"100\" cy=\"100\" r=\"50\" fill=\"blue\" /\u003e\u003ctext x=\"100\" y=\"100\" dx=\"0\" dy=\"0\" rotate=\"\" fill=\"magenta\" \u003ehello world!\u003c/text\u003e\u003ccircle cx=\"100\" cy=\"100\" r=\"50\" fill=\"blue\" class=\"toto\"/\u003e\u003c/svg\u003e\n"},{"name":"z1_filetest.gno","body":"// PKGPATH: gno.land/p/demo/svg_test\npackage svg_test\n\nimport \"gno.land/p/demo/svg\"\n\nfunc main() {\n\tcanvas := svg.Canvas{\n\t\tWidth: 500, Height: 500,\n\t\tElems: []svg.Elem{\n\t\t\tsvg.Rectangle{X: 50, Y: 50, Width: 100, Height: 100, Fill: \"red\"},\n\t\t\tsvg.Circle{CX: 50, CY: 50, R: 100, Fill: \"red\"},\n\t\t\tsvg.Text{X: 100, Y: 100, Text: \"hello world!\", Fill: \"magenta\"},\n\t\t},\n\t}\n\tprintln(canvas)\n}\n\n// Output:\n// \u003csvg xmlns=\"http://www.w3.org/2000/svg\" width=\"500\" height=\"500\" viewBox=\"\"\u003e\u003crect x=\"50\" y=\"50\" width=\"100\" height=\"100\" rx=\"0\" ry=\"0\" fill=\"red\" /\u003e\u003ccircle cx=\"50\" cy=\"50\" r=\"100\" fill=\"red\" /\u003e\u003ctext x=\"100\" y=\"100\" dx=\"0\" dy=\"0\" rotate=\"\" fill=\"magenta\" \u003ehello world!\u003c/text\u003e\u003c/svg\u003e\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"h2dhizJkTOwi7a4x2OG0UoPzl/oG1J571bIx5AieVqRwNWjgcE150YKw00ix2Ie/IE4836UjTzbfFG3c0hUkBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"subtests","path":"gno.land/p/demo/tests/subtests","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tests/subtests\"\ngno = \"0.9\"\n"},{"name":"subtests.gno","body":"package subtests\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n)\n\nfunc GetCurrentRealm() runtime.Realm {\n\treturn unsafe.CurrentRealm()\n}\n\nfunc GetPreviousRealm() runtime.Realm {\n\treturn unsafe.PreviousRealm()\n}\n\nfunc Exec(fn func()) {\n\tfn()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"g3bykLOQIuOQfT1hANXnzR/gaYIVLln2JlfXEp+IRtCyXkQZgv4SvfEt7gD+61Yd5HpeN/yFoRGbznXoARQXDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"tests","path":"gno.land/p/demo/tests","files":[{"name":"README.md","body":"Modules here are only useful for file realm tests.\nThey can be safely ignored for other purposes.\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tests\"\ngno = \"0.9\"\n"},{"name":"tests.gno","body":"package tests\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\n\tpsubtests \"gno.land/p/demo/tests/subtests\"\n)\n\nconst World = \"world\"\n\nfunc CurrentRealmPath() string {\n\treturn unsafe.CurrentRealm().PkgPath()\n}\n\n//----------------------------------------\n// cross realm test vars\n\ntype TestRealmObject2 struct {\n\tField string\n}\n\nfunc (o2 *TestRealmObject2) Modify() {\n\to2.Field = \"modified\"\n}\n\n// Value-receiver mutator. By Go/Gno semantics this only mutates the\n// method's local copy. Used by readonly-taint filetests to probe\n// whether a receiver carrying the sticky N_Readonly bit propagates\n// the bit onto the method-frame copy.\nfunc (o2 TestRealmObject2) ModifyVal() {\n\to2.Field = \"modified-val\"\n}\n\n// Unexported mutator. Used by the method-expression visibility filetest:\n// `(*tests.TestRealmObject2).clearField` from outside this package must\n// be rejected at preprocess time.\nfunc (o2 *TestRealmObject2) clearField() {\n\to2.Field = \"\"\n}\n\nvar (\n\tsomevalue1 TestRealmObject2\n\tSomeValue2 TestRealmObject2\n\tSomeValue3 *TestRealmObject2\n)\n\nfunc init() {\n\tsomevalue1 = TestRealmObject2{Field: \"init\"}\n\tSomeValue2 = TestRealmObject2{Field: \"init\"}\n\tSomeValue3 = \u0026TestRealmObject2{Field: \"init\"}\n}\n\nfunc ModifyTestRealmObject2a() {\n\tsomevalue1.Field = \"modified\"\n}\n\nfunc ModifyTestRealmObject2b() {\n\tSomeValue2.Field = \"modified\"\n}\n\nfunc ModifyTestRealmObject2c() {\n\tSomeValue3.Field = \"modified\"\n}\n\nfunc GetPreviousRealm() runtime.Realm {\n\treturn unsafe.PreviousRealm()\n}\n\nfunc GetPSubtestsPreviousRealm() runtime.Realm {\n\treturn psubtests.GetPreviousRealm()\n}\n\n// Warning: unsafe pattern.\nfunc Exec(fn func()) {\n\tfn()\n}\n\n// ExecRlm mirrors Exec but threads the caller's rlm into the callback\n// so the callback can use `cross(rlm)` instead of bare `cross`.\nfunc ExecRlm(_ int, rlm realm, fn func(_ int, rlm realm)) {\n\tfn(0, rlm)\n}\n"},{"name":"tests_test.gno","body":"package tests_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/tests\"\n)\n\nvar World = \"WORLD\"\n\nfunc TestGetHelloWorld(t *testing.T) {\n\t// tests.World is 'world'\n\ts := \"hello \" + tests.World + World\n\tconst want = \"hello worldWORLD\"\n\tif s != want {\n\t\tt.Error(\"not the same\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"dhsuouxZnEINtPaPyJ3fQcG5JtnIRj++bGcG0lCYWovu6UIsuzBC2WXIbUGEGQDmxv9J1AK+579wEYS6gBEwBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"launderpkg","path":"gno.land/p/demo/tests/launderpkg","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tests/launderpkg\"\ngno = \"0.9\"\n"},{"name":"launderpkg.gno","body":"// Package launderpkg defines a struct type that the laundervictim\n// realm uses as the type of its package-level state. The Set method\n// is the \"innocent /p/-helper\" that an attacker tries to weaponize\n// via the receiver-borrow rule (PushFrameCall borrow rule 2):\n// when the receiver is owned by /r/X, calling Set borrows m.Realm to\n// /r/X, and the write inside Set runs with /r/X authority.\npackage launderpkg\n\ntype Object struct {\n\tField string\n}\n\n// PInitData is a /p/-init-allocated package-level data var. Its\n// StructValue carries ObjectInfo.PkgID = /p/demo/tests/launderpkg.\n// Used by zrealm_launder_pdata_* filetests to probe the /p/-source\n// read patterns that the existing 62 /r/-source launder filetests\n// don't cover. Empirically, direct value-read and pointer-deref of\n// PInitData from a /r/-caller both panic readonly tainted — the\n// /p/-source bytes are not adoptable into /r/-authority via these\n// patterns.\nvar PInitData = Object{Field: \"p-init\"}\n\n// Set is the canonical \"/p/ helper that mutates /r/-owned state\"\n// pattern. Looks benign — like list.Set, avl.Set, etc. — but if a\n// foreign caller obtains a pointer to a victim realm's Object, they\n// can invoke Set on it and the borrow rule grants them victim\n// authority for the call. Exposing a *Object out of a realm is\n// equivalent to consenting to mutation by any caller that holds the\n// pointer.\nfunc (o *Object) Set(s string) {\n\to.Field = s\n}\n\n// Read is a read-only accessor.\nfunc (o *Object) Read() string {\n\treturn o.Field\n}\n\n// Mutator is an interface — callers can pass any implementation. A\n// realistic pattern: /p/orig defines a hook interface and exposes a\n// method that lets callers register an impl for some operation.\ntype Mutator interface {\n\tRun(*Immutable)\n}\n\n// UseMutator is a /p/-method on *Object that dispatches an interface\n// method (Mutator.Run) with target as the argument. This is the\n// realistic shape — a /p/-library invoking a user-supplied hook on\n// /r/-owned data.\nfunc (o *Object) UseMutator(target *Immutable, m Mutator) {\n\tm.Run(target)\n}\n\n// SliceMutator's Run takes a SLICE of pointers — exercises the\n// \"non-pointer parameter that still conveys writable foreign data\"\n// shape that Attack K explores.\ntype SliceMutator interface {\n\tRun([]*Immutable)\n}\n\n// UseSliceMutator dispatches a SliceMutator on a single-element\n// slice carrying target.\nfunc (o *Object) UseSliceMutator(target *Immutable, m SliceMutator) {\n\tm.Run([]*Immutable{target})\n}\n\n// AnyMutator's Run takes `any` — interface-typed parameter. The\n// caller can box a *Immutable into the interface and an attacker\n// impl can type-assert back to write. Tests whether the predicate\n// needs to treat interface-typed params as potentially foreign.\ntype AnyMutator interface {\n\tRun(any)\n}\n\n// UseAnyMutator boxes target into `any` before dispatching.\nfunc (o *Object) UseAnyMutator(target *Immutable, m AnyMutator) {\n\tm.Run(target)\n}\n\n// Bare is a /p/-declared struct type with NO methods. Used to test\n// whether embedding/fielding a methods-less /p/-type inside an\n// /r/-declared container exposes any laundering vector through\n// DIRECT field writes (no method dispatch, no Apply callback). The\n// expectation: readonly taint on the /r/-container propagates to\n// inner /p/-typed fields and direct writes panic.\ntype Bare struct {\n\tField string\n}\n\n// Immutable is a deliberately read-only /p/ type: same struct layout\n// as Object, but no mutator method. A realm using Immutable as the\n// type of an exposed field intends \"read-only API.\" The launder game\n// variation explores whether an attacker /p/ package can convert\n// *Immutable to a type with a mutator method declared elsewhere.\ntype Immutable struct {\n\tField string\n}\n\n// Read is a read-only accessor.\nfunc (i *Immutable) Read() string {\n\treturn i.Field\n}\n\n// Apply is a higher-order helper that hands the *Immutable to a\n// caller-supplied callback. The signature looks read-only (no\n// mutator method on *Immutable itself), but Apply hands out an\n// addressable pointer to victim-owned memory while m.Realm is\n// borrowed to the victim. A /p/-declared callback substituted by\n// the caller therefore runs with victim authority. This is the\n// avl.Tree.Iterate / list.ForEach shape — common in /p/ libraries.\nfunc (i *Immutable) Apply(fn func(*Immutable)) {\n\tfn(i)\n}\n\n// BumpToPwn is a no-arg, no-return /p/-method that mutates the\n// receiver. Used by stored-bound-method-value laundering probes:\n// `mv := victimImmPtr.BumpToPwn` has type `func()`, which fits\n// PlainHook = func(). When the bound method value is stored and\n// invoked later from /r/-victim context, recv-borrow borrow rule #2 fires on\n// the /r/-victim-stamped recv → m.Realm = /r/-victim → write\n// commits.\nfunc (i *Immutable) BumpToPwn() {\n\ti.Field = \"pwnd-via-bound-mv\"\n}\n\n// DeferApply is the defer-variant of Apply: schedules fn(i) as a\n// defer instead of calling synchronously. Used to probe whether\n// the borrow rules apply the same when a callback is invoked from\n// inside a /p/-function's defer queue.\nfunc (i *Immutable) DeferApply(fn func(*Immutable)) {\n\tdefer fn(i)\n}\n\n// PanicAfterApply: invokes fn(i) synchronously, then panics after\n// return. If fn ran without panicking (write succeeded), the panic\n// here is /p/-realm panic propagating up.\nfunc (i *Immutable) PanicAfterApply(fn func(*Immutable)) {\n\tfn(i)\n\tpanic(\"post-apply panic\")\n}\n\n// RecoverApply: defers a recover(), then calls fn(i). If fn panics\n// with readonly, recover catches it (within the same /p/-pkg\n// frame). Returns the recovered value.\nfunc (i *Immutable) RecoverApply(fn func(*Immutable)) (rec any) {\n\tdefer func() {\n\t\trec = recover()\n\t}()\n\tfn(i)\n\treturn\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Vmylzls9uwfY4+b/ZrF5rbe0ZZ6rNjfy+x0LsuY8Zy4HJjOntv/z/OrtiXhdP1IfldK52+oJEDjZq+OzMCoXBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"launderattack","path":"gno.land/p/demo/tests/launderattack","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tests/launderattack\"\ngno = \"0.9\"\n"},{"name":"launderattack.gno","body":"// Package launderattack imports launderpkg and provides functions\n// that attempt to mutate a /p/-typed value passed by pointer. This is\n// the \"/p/attack imports /p/orig\" variation: the goal is for code\n// declared in this /p/ package to mutate a /r/-victim's instance of\n// launderpkg.Object.\npackage launderattack\n\nimport \"gno.land/p/demo/tests/launderpkg\"\n\n// TamperDirect writes through the pointer in its own body. The body\n// runs under whatever m.Realm the caller had at PushFrameCall — for a\n// top-level /p/ function with no receiver, that's the caller's realm.\n// If the caller is attacker-realm, m.Realm at the write is\n// attacker-realm and the readonly check fires on a foreign-stamped\n// base.\nfunc TamperDirect(o *launderpkg.Object, s string) {\n\to.Field = s\n}\n\n// TamperViaMethod dispatches through the launderpkg.Object's own /p/\n// method. PushFrameCall for Set sees a receiver stamped with the\n// victim's realm (the pointer aliases victim's persisted state),\n// triggering borrow rule 2: m.Realm becomes victim for the duration\n// of Set. The write succeeds — but this requires launderpkg.Object\n// to expose a Set method to begin with.\nfunc TamperViaMethod(o *launderpkg.Object, s string) {\n\to.Set(s)\n}\n\n// Tamper has the SAME underlying struct layout as launderpkg.Immutable.\n// /p/launderpkg deliberately gave Immutable no mutator method —\n// callers were supposed to be unable to write its Field. /p/attack\n// declares its own type with the same layout and adds a mutator. An\n// attacker that holds a *launderpkg.Immutable can convert it to\n// *Tamper and invoke Tamper.Set — the conversion is purely a\n// type-tag change, the pointer still aliases victim's persisted\n// memory. PushFrameCall for Tamper.Set then sees recv stamped with\n// victim's realm (the underlying object is unchanged) and\n// borrow-routes m.Realm to victim. The write succeeds.\ntype Tamper struct {\n\tField string\n}\n\n// Set is the mutator that launderpkg.Immutable deliberately did NOT\n// expose. /p/attack adds it via the parallel-type trick.\nfunc (t *Tamper) Set(s string) {\n\tt.Field = s\n}\n\n// Convert is the attacker's helper that does the type punning so the\n// caller doesn't have to write the conversion inline.\nfunc Convert(p *launderpkg.Immutable) *Tamper {\n\treturn (*Tamper)(p)\n}\n\n// EvilMutator implements launderpkg.Mutator with a PRIMITIVE\n// underlying type. Underlying-type matters: a struct/array/etc.\n// receiver has a *StructValue that gets PkgID-stamped at allocation,\n// triggering PushFrameCall's receiver-borrow rule to shift m.Realm\n// back to /p/launderattack. A primitive-underlying type has no\n// *StructValue and no PkgID — `recv.GetFirstObject` returns nil, so\n// the borrow rule's `if obj != nil { ... }` branch is skipped and\n// m.Realm stays at whatever the caller had it set to.\n//\n// When Run is dispatched via interface from inside a /p/-method\n// body that was receiver-borrowed to the victim, m.Realm at Run's\n// entry is the victim's — and stays the victim's, because EvilMutator\n// (an int underneath) carries no PkgID to borrow against. The write\n// inside Run commits under victim authority.\ntype EvilMutator int\n\nfunc (EvilMutator) Run(i *launderpkg.Immutable) {\n\ti.Field = \"pwnd-via-iface\"\n}\n\n// EvilNilRecv tests the nil-pointer-receiver variant. Calling a method\n// on a nil *EvilNilRecv is legal in Gno when the body doesn't deref\n// the receiver. recv = PointerValue{Base: nil} → GetBase returns nil\n// → GetFirstObject returns nil. Same \"no anchor\" gap as the\n// primitive-receiver case, reachable through *T receivers.\ntype EvilNilRecv struct {\n\tX int // unused\n}\n\nfunc (n *EvilNilRecv) Run(i *launderpkg.Immutable) {\n\t// Does NOT dereference n. Just writes through target.\n\ti.Field = \"pwnd-via-nilrecv\"\n}\n\n// EvilFunc / EvilSlice / EvilMap — additional nil-anchor shapes.\n// nil-valued receivers of defined types whose underlying is a\n// reference type (slice/map/func) also have GetFirstObject == nil.\n// The Attack H/I fix should cover all of them via the\n// recvDeclaredTypePkgPath helper.\ntype EvilFunc func()\n\nfunc (EvilFunc) Run(i *launderpkg.Immutable) { i.Field = \"pwnd-via-func\" }\n\ntype EvilSlice []int\n\nfunc (EvilSlice) Run(i *launderpkg.Immutable) { i.Field = \"pwnd-via-slice\" }\n\ntype EvilMap map[string]int\n\nfunc (EvilMap) Run(i *launderpkg.Immutable) { i.Field = \"pwnd-via-map\" }\n\n// EvilSliceMutator's Run takes a slice — NOT a pointer parameter, so\n// the Attack H/I fix's `hasForeignPPtrParam` predicate skips it.\n// Tests whether the anchor predicate needs to look INSIDE composite\n// parameter types for foreign-/p/ pointers.\ntype EvilSliceMutator int\n\nfunc (EvilSliceMutator) Run(s []*launderpkg.Immutable) {\n\ts[0].Field = \"pwnd-via-slice-arg\"\n}\n\n// EvilAnyMutator's Run takes `any` and type-asserts to *Immutable.\n// The signature reveals NO foreign-/p/-pointer statically — the\n// pointer is hidden inside the interface box. Probes whether the\n// predicate needs to fire on interface-typed params too.\ntype EvilAnyMutator int\n\nfunc (EvilAnyMutator) Run(x any) {\n\tt := x.(*launderpkg.Immutable)\n\tt.Field = \"pwnd-via-any-arg\"\n}\n\n// EvilWrite is a top-level /p/-declared function value matching\n// `func(*launderpkg.Immutable)`. Top-level /p/ functions trigger\n// neither borrow rule #1 (not /r/-declared) nor borrow rule #2 (no receiver), so\n// when EvilWrite is invoked as a callback from inside a\n// borrowed-to-victim /p/-method body (e.g. Immutable.Apply, or any\n// avl.Tree.Iterate-style hook), it inherits the victim's m.Realm\n// and the write commits under victim authority.\nfunc EvilWrite(i *launderpkg.Immutable) {\n\ti.Field = \"pwnd-via-apply\"\n}\n\n// EvilObjectWrite is the same shape but for *Object — used by tests\n// that exercise Object's mutator surface through Apply-style callbacks.\nfunc EvilObjectWrite(o *launderpkg.Object) {\n\to.Field = \"pwnd-via-apply-obj\"\n}\n\n// StoredHook is a /p/attack package-level closure: a FuncLit evaluated\n// during /p/launderattack init, so its ObjectInfo.PkgID is stamped\n// /p/launderattack. Unlike a top-level FuncDecl (EvilWrite/EvilObjectWrite,\n// IsClosure=false), invoking a closure triggers PushFrameCall's borrow rule\n// #3, which borrows m.Realm to the closure's construction realm\n// (/p/launderattack). Used to probe rule #3: a write through it to a foreign\n// /r/ object must still be rejected. (If rule #3 ever regressed to a nil\n// borrow, m.Realm would go nil and the write would silently succeed.)\nvar StoredHook = func(o *launderpkg.Object) {\n\to.Field = \"pwnd-via-stored-closure\"\n}\n\n// Tamperer is a stamped /p/attack value (constructed at init under\n// /p/attack's realm context, stamped /p/attack, persisted Frozen).\n// Methods on Tamperer get receiver-borrowed to /p/attack at call\n// time — useful as a control to compare against attacks where the\n// receiver is victim-stamped.\ntype Tamperer struct{}\n\n// TamperMethod is a /p/attack-defined method. When the caller invokes\n// Tamperer{}.TamperMethod(o, s), the receiver Tamperer{} is\n// constructed in the caller's realm, so receiver-borrow lands at the\n// caller's realm, not /p/attack — the receiver carries the caller's\n// stamp, not /p/attack's.\nfunc (Tamperer) TamperMethod(o *launderpkg.Object, s string) {\n\to.Field = s\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"qnw5R9ZM70rRyRSnRn8H0Pen7PsXdr3E2c+K/uM1CZumAx7Xm8qSNYWCKDG4cib2MrRSxh4sKnGtsWFfQ6LHAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"p_closurecap","path":"gno.land/p/demo/tests/p_closurecap","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tests/p_closurecap\"\ngno = \"0.9\"\n"},{"name":"p_closurecap.gno","body":"// Package p_closurecap exercises the \"var stopped bool in\n// memberStorage.IterateByOffset\" pattern from boards2/commondao:\n// a method declares a local var that an inline closure captures and\n// writes to. Without the unreal-HIV exception in the readonly check,\n// this failed across borrow-realm transitions because the HIV's PkgID\n// stamp records the alloc-site realm, but the closure body may run\n// under a different borrowed realm — the check would fire on a write\n// the closure itself made to its own captured slot.\npackage p_closurecap\n\n// Inner is the receiver of loop(). When Inner lives in a different\n// realm than the Outer-method's caller realm, PushFrameCall's borrow\n// rule 2 shifts m.Realm to Inner's realm for the entire body of loop()\n// (and the synchronously-invoked closure). That shift is what makes\n// HIV.PkgID (stamped at var stopped's alloc) differ from m.Realm at\n// the closure's write site.\ntype Inner struct {\n\tN int\n}\n\n// Outer is the var-stopped pattern. The inline closure captures\n// `stopped` and writes to it. Returns true if `fn` ever returned true.\n//\n// The key shape: Outer is a top-level /p/ func (no receiver, so no\n// borrow on entry — HIV stamps with caller's realm), but it delegates\n// to `inn.loop(...)`, where `inn` is supplied by the caller and may\n// live in a different realm. inn.loop's borrow flips m.Realm to inn's\n// realm; the inline closure then writes to `stopped` under that\n// borrowed realm.\nfunc Outer(inn *Inner, count int, fn func(i int) bool) bool {\n\tvar stopped bool\n\tinn.loop(count, func(i int) bool {\n\t\tstopped = fn(i)\n\t\treturn stopped\n\t})\n\treturn stopped\n}\n\n// loop is a /p/ method on *Inner. As a receiver-method on a /p/ type,\n// PushFrameCall's borrow rule 2 fires here: m.Realm becomes\n// Inner.PkgID's realm for the duration of loop().\nfunc (in *Inner) loop(count int, cb func(i int) bool) {\n\tfor i := 0; i \u003c count; i++ {\n\t\tif cb(i) {\n\t\t\treturn\n\t\t}\n\t}\n}\n\n// MakeCounter returns a closure that captures a local int. The\n// returned closure can be stored in /r/ state — verifying that a\n// persisted closure-capture HIV (whose FuncLit lives in /p/) is still\n// writable when invoked from a foreign realm context.\nfunc MakeCounter(start int) func() int {\n\tc := start\n\treturn func() int {\n\t\tc++\n\t\treturn c\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"oIFzZ93byj0gGd7OROr56rvoNIAbklKvy55V7QJfA+JewvgCBnSVfWkgsaoas5gT1dDNeBPi3HNRytBKqcfOBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"p_crossrealm","path":"gno.land/p/demo/tests/p_crossrealm","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tests/p_crossrealm\"\ngno = \"0.9\"\n"},{"name":"p_crossrealm.gno","body":"package p_crossrealm\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n)\n\ntype Stringer interface {\n\tString() string\n}\n\ntype Container struct {\n\tA int\n\tB Stringer\n}\n\nfunc (c *Container) Touch() *Container {\n\tc.A += 1\n\treturn c\n}\n\nfunc (c *Container) Print() {\n\tprintln(\"A:\", c.A)\n\tif c.B == nil {\n\t\tprintln(\"B: undefined\")\n\t} else {\n\t\tprintln(\"B:\", c.B.String())\n\t}\n}\n\nfunc CurrentRealm() runtime.Realm {\n\treturn unsafe.CurrentRealm()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"OuR5j0FR0ynqdLIgYJxREoyTdvYn7pO61UHYaKn9/D5m6fZGBdVWQXTMQFjQ91m02CD4ngcyCZyDmgN2cQ5pCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"grc721","path":"gno.land/p/demo/tokens/grc721","files":[{"name":"basic_nft.gno","body":"package grc721\n\nimport (\n\t\"chain\"\n\t\"math/overflow\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype BasicNFT struct {\n\tname              string\n\tsymbol            string\n\torigRealm         string   // owning realm's package path\n\towners            avl.Tree // tokenId -\u003e OwnerAddress\n\tbalances          avl.Tree // OwnerAddress -\u003e TokenCount\n\ttokenApprovals    avl.Tree // TokenId -\u003e ApprovedAddress\n\ttokenURIs         avl.Tree // TokenId -\u003e URIs\n\toperatorApprovals avl.Tree // \"OwnerAddress:OperatorAddress\" -\u003e bool\n}\n\nfunc NewBasicNFT(_ int, rlm realm, name, symbol string) *BasicNFT {\n\tif !rlm.IsCurrent() {\n\t\tpanic(ErrSpoofedRealm)\n\t}\n\tpkgPath := rlm.PkgPath()\n\tif pkgPath == \"\" {\n\t\tpanic(ErrNotRealm)\n\t}\n\tif !validName(name) {\n\t\tpanic(ErrInvalidName)\n\t}\n\tif !validSymbol(symbol) {\n\t\tpanic(ErrInvalidSymbol)\n\t}\n\n\treturn \u0026BasicNFT{\n\t\tname:      name,\n\t\tsymbol:    symbol,\n\t\torigRealm: pkgPath,\n\n\t\towners:            avl.Tree{},\n\t\tbalances:          avl.Tree{},\n\t\ttokenApprovals:    avl.Tree{},\n\t\ttokenURIs:         avl.Tree{},\n\t\toperatorApprovals: avl.Tree{},\n\t}\n}\n\nfunc (s *BasicNFT) Name() string      { return s.name }\nfunc (s *BasicNFT) Symbol() string    { return s.symbol }\nfunc (s *BasicNFT) TokenCount() int64 { return int64(s.owners.Size()) }\n\nfunc (s *BasicNFT) ID() string {\n\treturn s.origRealm + \".\" + s.symbol\n}\n\n// BalanceOf returns balance of input address\nfunc (s *BasicNFT) BalanceOf(addr address) (int64, error) {\n\tif err := isValidAddress(addr); err != nil {\n\t\treturn 0, err\n\t}\n\n\tbalance := s.balances.Get(addr.String())\n\tif balance == nil {\n\t\treturn 0, nil\n\t}\n\n\treturn balance.(int64), nil\n}\n\n// OwnerOf returns owner of input token id\nfunc (s *BasicNFT) OwnerOf(tid TokenID) (address, error) {\n\towner := s.owners.Get(string(tid))\n\tif owner == nil {\n\t\treturn \"\", ErrInvalidTokenId\n\t}\n\n\treturn owner.(address), nil\n}\n\n// TokenURI returns the URI of input token id\nfunc (s *BasicNFT) TokenURI(tid TokenID) (string, error) {\n\turi := s.tokenURIs.Get(tid.String())\n\tif uri == nil {\n\t\treturn \"\", ErrInvalidTokenId\n\t}\n\n\treturn uri.(string), nil\n}\n\n// SetTokenURI sets the URI of a token. caller must equal the token's\n// owner. The owning realm's public wrapper is responsible for deriving\n// caller from rlm.Previous().Address() under an rlm.IsCurrent() guard\n// before invoking this method; this method trusts the supplied caller.\nfunc (s *BasicNFT) SetTokenURI(caller address, tid TokenID, tURI TokenURI) (bool, error) {\n\t// check for invalid TokenID\n\tif !s.exists(tid) {\n\t\treturn false, ErrInvalidTokenId\n\t}\n\n\t// check for the right owner\n\towner, err := s.OwnerOf(tid)\n\tif err != nil {\n\t\treturn false, err\n\t}\n\tif caller != owner {\n\t\treturn false, ErrCallerIsNotOwner\n\t}\n\ts.tokenURIs.Set(tid.String(), tURI.String())\n\n\tchain.Emit(\n\t\tTokenURIUpdateEvent,\n\t\t\"token\", s.ID(),\n\t\t\"tokenId\", tid.String(),\n\t)\n\n\treturn true, nil\n}\n\n// IsApprovedForAll returns true if operator is approved for all by the owner.\n// Otherwise, returns false\nfunc (s *BasicNFT) IsApprovedForAll(owner, operator address) bool {\n\tkey := owner.String() + \":\" + operator.String()\n\tapproved := s.operatorApprovals.Get(key)\n\tif approved == nil {\n\t\treturn false\n\t}\n\n\treturn approved.(bool)\n}\n\n// Approve approves the input address for a particular token. caller\n// must be the owner OR an operator approved for all on owner's behalf.\n// The owning realm's wrapper validates IsCurrent and derives caller\n// from rlm.Previous().Address() before calling.\nfunc (s *BasicNFT) Approve(caller, to address, tid TokenID) error {\n\tif err := isValidAddress(to); err != nil {\n\t\treturn err\n\t}\n\n\towner, err := s.OwnerOf(tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif owner == to {\n\t\treturn ErrApprovalToCurrentOwner\n\t}\n\n\tif caller != owner \u0026\u0026 !s.IsApprovedForAll(owner, caller) {\n\t\treturn ErrCallerIsNotOwnerOrApproved\n\t}\n\n\ttidStr := tid.String()\n\ts.tokenApprovals.Set(tidStr, to)\n\n\tchain.Emit(\n\t\tApprovalEvent,\n\t\t\"token\", s.ID(),\n\t\t\"owner\", owner.String(),\n\t\t\"to\", to.String(),\n\t\t\"tokenId\", tidStr,\n\t)\n\n\treturn nil\n}\n\n// GetApproved return the approved address for token\nfunc (s *BasicNFT) GetApproved(tid TokenID) (address, error) {\n\taddr := s.tokenApprovals.Get(tid.String())\n\tif addr == nil {\n\t\treturn zeroAddress, ErrTokenIdNotHasApproved\n\t}\n\n\treturn addr.(address), nil\n}\n\n// SetApprovalForAll grants/revokes operator permission across all of\n// the caller's tokens. caller is the owner whose approvals are mutated;\n// the owning realm's wrapper derives it from rlm.Previous().Address()\n// under an IsCurrent() guard.\nfunc (s *BasicNFT) SetApprovalForAll(caller, operator address, approved bool) error {\n\tif err := isValidAddress(operator); err != nil {\n\t\treturn ErrInvalidAddress\n\t}\n\treturn s.setApprovalForAll(caller, operator, approved)\n}\n\n// SafeTransferFrom transfers a token from `from` to `to`, checking that\n// contract recipients are aware of the GRC721 protocol to prevent\n// tokens from being forever locked. caller must be the owner or an\n// approved operator. The owning realm's wrapper derives caller from\n// rlm.Previous().Address() under an IsCurrent() guard.\nfunc (s *BasicNFT) SafeTransferFrom(caller, from, to address, tid TokenID) error {\n\tif !s.isApprovedOrOwner(caller, tid) {\n\t\treturn ErrCallerIsNotOwnerOrApproved\n\t}\n\n\terr := s.transfer(from, to, tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !s.checkOnGRC721Received(from, to, tid) {\n\t\treturn ErrTransferToNonGRC721Receiver\n\t}\n\n\treturn nil\n}\n\n// TransferFrom transfers a token from `from` to `to`. Same caller\n// contract as SafeTransferFrom.\nfunc (s *BasicNFT) TransferFrom(caller, from, to address, tid TokenID) error {\n\tif !s.isApprovedOrOwner(caller, tid) {\n\t\treturn ErrCallerIsNotOwnerOrApproved\n\t}\n\n\terr := s.transfer(from, to, tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// Mints `tokenId` and transfers it to `to`.\nfunc (s *BasicNFT) Mint(to address, tid TokenID) error {\n\treturn s.mint(to, tid)\n}\n\n// Mints `tokenId` and transfers it to `to`. Also checks that\n// contract recipients are using GRC721 protocol\nfunc (s *BasicNFT) SafeMint(to address, tid TokenID) error {\n\terr := s.mint(to, tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !s.checkOnGRC721Received(zeroAddress, to, tid) {\n\t\treturn ErrTransferToNonGRC721Receiver\n\t}\n\n\treturn nil\n}\n\nfunc (s *BasicNFT) Burn(tid TokenID) error {\n\towner, err := s.OwnerOf(tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\ts.beforeTokenTransfer(owner, zeroAddress, tid, 1)\n\n\ttidStr := tid.String()\n\ts.tokenApprovals.Remove(tidStr)\n\tbalance, err := s.BalanceOf(owner)\n\tif err != nil {\n\t\treturn err\n\t}\n\tbalance = overflow.Sub64p(balance, 1)\n\n\townerStr := owner.String()\n\ts.balances.Set(ownerStr, balance)\n\ts.owners.Remove(tidStr)\n\n\tchain.Emit(\n\t\tBurnEvent,\n\t\t\"token\", s.ID(),\n\t\t\"from\", ownerStr,\n\t\t\"tokenId\", tidStr,\n\t)\n\n\ts.afterTokenTransfer(owner, zeroAddress, tid, 1)\n\n\treturn nil\n}\n\n/* Helper methods */\n\n// Helper for SetApprovalForAll()\nfunc (s *BasicNFT) setApprovalForAll(owner, operator address, approved bool) error {\n\tif owner == operator {\n\t\treturn ErrApprovalToCurrentOwner\n\t}\n\n\tkey := owner.String() + \":\" + operator.String()\n\ts.operatorApprovals.Set(key, approved)\n\n\tchain.Emit(\n\t\tApprovalForAllEvent,\n\t\t\"token\", s.ID(),\n\t\t\"owner\", owner.String(),\n\t\t\"to\", operator.String(),\n\t\t\"approved\", strconv.FormatBool(approved),\n\t)\n\n\treturn nil\n}\n\n// Helper for TransferFrom() and SafeTransferFrom()\nfunc (s *BasicNFT) transfer(from, to address, tid TokenID) error {\n\tif err := isValidAddress(from); err != nil {\n\t\treturn ErrInvalidAddress\n\t}\n\tif err := isValidAddress(to); err != nil {\n\t\treturn ErrInvalidAddress\n\t}\n\n\tif from == to {\n\t\treturn ErrCannotTransferToSelf\n\t}\n\n\towner, err := s.OwnerOf(tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif owner != from {\n\t\treturn ErrTransferFromIncorrectOwner\n\t}\n\n\ts.beforeTokenTransfer(from, to, tid, 1)\n\n\t// Check that tokenId was not transferred by `beforeTokenTransfer`\n\towner, err = s.OwnerOf(tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif owner != from {\n\t\treturn ErrTransferFromIncorrectOwner\n\t}\n\n\ttidStr := tid.String()\n\ts.tokenApprovals.Remove(tidStr)\n\tfromBalance, err := s.BalanceOf(from)\n\tif err != nil {\n\t\treturn err\n\t}\n\ttoBalance, err := s.BalanceOf(to)\n\tif err != nil {\n\t\treturn err\n\t}\n\tfromBalance = overflow.Sub64p(fromBalance, 1)\n\ttoBalance = overflow.Add64p(toBalance, 1)\n\n\tfromStr := from.String()\n\ttoStr := to.String()\n\n\ts.balances.Set(fromStr, fromBalance)\n\ts.balances.Set(toStr, toBalance)\n\ts.owners.Set(tidStr, to)\n\n\tchain.Emit(\n\t\tTransferEvent,\n\t\t\"token\", s.ID(),\n\t\t\"from\", fromStr,\n\t\t\"to\", toStr,\n\t\t\"tokenId\", tidStr,\n\t)\n\n\ts.afterTokenTransfer(from, to, tid, 1)\n\n\treturn nil\n}\n\n// Helper for Mint() and SafeMint()\nfunc (s *BasicNFT) mint(to address, tid TokenID) error {\n\tif err := isValidAddress(to); err != nil {\n\t\treturn err\n\t}\n\n\tif s.exists(tid) {\n\t\treturn ErrTokenIdAlreadyExists\n\t}\n\n\ts.beforeTokenTransfer(zeroAddress, to, tid, 1)\n\n\t// Check that tokenId was not minted by `beforeTokenTransfer`\n\tif s.exists(tid) {\n\t\treturn ErrTokenIdAlreadyExists\n\t}\n\n\ttoBalance, err := s.BalanceOf(to)\n\tif err != nil {\n\t\treturn err\n\t}\n\ttoBalance = overflow.Add64p(toBalance, 1)\n\ttoStr := to.String()\n\ttidStr := tid.String()\n\ts.balances.Set(toStr, toBalance)\n\ts.owners.Set(tidStr, to)\n\n\tchain.Emit(\n\t\tMintEvent,\n\t\t\"token\", s.ID(),\n\t\t\"to\", toStr,\n\t\t\"tokenId\", tidStr,\n\t)\n\n\ts.afterTokenTransfer(zeroAddress, to, tid, 1)\n\n\treturn nil\n}\n\nfunc (s *BasicNFT) isApprovedOrOwner(addr address, tid TokenID) bool {\n\towner := s.owners.Get(tid.String())\n\tif owner == nil {\n\t\treturn false\n\t}\n\n\townerAddr := owner.(address)\n\tif addr == ownerAddr || s.IsApprovedForAll(ownerAddr, addr) {\n\t\treturn true\n\t}\n\n\tapproved, err := s.GetApproved(tid)\n\tif err != nil {\n\t\treturn false\n\t}\n\n\treturn approved == addr\n}\n\n// Checks if token id already exists\nfunc (s *BasicNFT) exists(tid TokenID) bool {\n\treturn s.owners.Has(tid.String())\n}\n\nfunc (s *BasicNFT) beforeTokenTransfer(from, to address, firstTokenId TokenID, batchSize int64) {\n\t// TODO: Implementation\n}\n\nfunc (s *BasicNFT) afterTokenTransfer(from, to address, firstTokenId TokenID, batchSize int64) {\n\t// TODO: Implementation\n}\n\nfunc (s *BasicNFT) checkOnGRC721Received(from, to address, tid TokenID) bool {\n\t// TODO: Implementation\n\treturn true\n}\n\nfunc (s *BasicNFT) RenderHome() (str string) {\n\tstr += ufmt.Sprintf(\"# %s ($%s)\\n\\n\", s.name, s.symbol)\n\tstr += ufmt.Sprintf(\"* **Total supply**: %d\\n\", s.TokenCount())\n\tstr += ufmt.Sprintf(\"* **Known accounts**: %d\\n\", s.balances.Size())\n\n\treturn\n}\n\n// Getter returns an NFTGetter that yields a reader-only view of the NFT.\n// Safe to register with cross-realm aggregators like tokenhub — readers\n// only, no rlm-typed methods, so no cur can be captured via this surface.\nfunc (n *BasicNFT) Getter() NFTGetter {\n\treturn func() IGRC721Reader {\n\t\treturn n\n\t}\n}\n"},{"name":"basic_nft_test.gno","body":"package grc721\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nvar (\n\tdummyNFTName   = \"DummyNFT\"\n\tdummyNFTSymbol = \"DNFT\"\n)\n\n// The IIFE same-realm cross promotes the test's EOA-origin cur into a\n// /p/grc721 CodeRealm cur so NewBasicNFT's IsCurrent + non-empty PkgPath\n// checks pass.\nfunc newDummyNFT(_ int, rlm realm) *BasicNFT {\n\tvar nft *BasicNFT\n\tfunc(cur realm) {\n\t\tnft = NewBasicNFT(0, cur, dummyNFTName, dummyNFTSymbol)\n\t}(cross(rlm))\n\treturn nft\n}\n\nfunc TestNewBasicNFTValidation(cur realm, t *testing.T) {\n\tconst (\n\t\tnameErr   = \"invalid token name\"\n\t\tsymbolErr = \"invalid token symbol\"\n\t)\n\n\tuassert.AbortsContains(t, cur, nameErr, func(cur realm) {\n\t\tNewBasicNFT(0, cur, \"\", \"OK\")\n\t}, \"empty name\")\n\tuassert.AbortsContains(t, cur, symbolErr, func(cur realm) {\n\t\tNewBasicNFT(0, cur, \"Name\", \"\")\n\t}, \"empty symbol\")\n\tuassert.AbortsContains(t, cur, nameErr, func(cur realm) {\n\t\tNewBasicNFT(0, cur, strings.Repeat(\"a\", MaxNameLen+1), \"OK\")\n\t}, \"name too long\")\n\tuassert.AbortsContains(t, cur, symbolErr, func(cur realm) {\n\t\tNewBasicNFT(0, cur, \"Name\", strings.Repeat(\"A\", MaxSymbolLen+1))\n\t}, \"symbol too long\")\n\tuassert.AbortsContains(t, cur, nameErr, func(cur realm) {\n\t\tNewBasicNFT(0, cur, \"bad\\x01name\", \"OK\")\n\t}, \"name with control char\")\n\tuassert.AbortsContains(t, cur, symbolErr, func(cur realm) {\n\t\tNewBasicNFT(0, cur, \"Name\", \"BA.D\")\n\t}, \"symbol with dot\")\n\tuassert.AbortsContains(t, cur, symbolErr, func(cur realm) {\n\t\tNewBasicNFT(0, cur, \"Name\", \"BA D\")\n\t}, \"symbol with space\")\n\n\tuassert.NotAborts(t, cur, func(cur realm) {\n\t\tNewBasicNFT(0, cur, strings.Repeat(\"a\", MaxNameLen), strings.Repeat(\"A\", MaxSymbolLen))\n\t}, \"boundary name+symbol should succeed\")\n}\n\nfunc TestNewBasicNFT(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n}\n\nfunc TestName(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tname := dummy.Name()\n\tuassert.Equal(t, dummyNFTName, name)\n}\n\nfunc TestSymbol(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tsymbol := dummy.Symbol()\n\tuassert.Equal(t, dummyNFTSymbol, symbol)\n}\n\nfunc TestTokenCount(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tcount := dummy.TokenCount()\n\tuassert.Equal(t, int64(0), count)\n\n\tdummy.mint(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\", TokenID(\"1\"))\n\tdummy.mint(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\", TokenID(\"2\"))\n\n\tcount = dummy.TokenCount()\n\tuassert.Equal(t, int64(2), count)\n}\n\nfunc TestBalanceOf(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\tbalanceAddr1, err := dummy.BalanceOf(addr1)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(0), balanceAddr1)\n\n\tdummy.mint(addr1, TokenID(\"1\"))\n\tdummy.mint(addr1, TokenID(\"2\"))\n\tdummy.mint(addr2, TokenID(\"3\"))\n\n\tbalanceAddr1, err = dummy.BalanceOf(addr1)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tbalanceAddr2, err := dummy.BalanceOf(addr2)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tuassert.Equal(t, int64(2), balanceAddr1)\n\tuassert.Equal(t, int64(1), balanceAddr2)\n}\n\nfunc TestOwnerOf(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\towner, err := dummy.OwnerOf(TokenID(\"invalid\"))\n\tuassert.Error(t, err, \"should not result in error\")\n\n\tdummy.mint(addr1, TokenID(\"1\"))\n\tdummy.mint(addr2, TokenID(\"2\"))\n\n\t// Checking for token id \"1\"\n\towner, err = dummy.OwnerOf(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, addr1.String(), owner.String())\n\n\t// Checking for token id \"2\"\n\towner, err = dummy.OwnerOf(TokenID(\"2\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, addr2.String(), owner.String())\n}\n\nfunc TestIsApprovedForAll(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\tisApprovedForAll := dummy.IsApprovedForAll(addr1, addr2)\n\tuassert.False(t, isApprovedForAll)\n}\n\nfunc TestSetApprovalForAll(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tcaller := testutils.TestAddress(\"caller\")\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\t// Test setting approval to true\n\tisApprovedForAll := dummy.IsApprovedForAll(caller, addr)\n\tuassert.False(t, isApprovedForAll)\n\n\terr := dummy.SetApprovalForAll(caller, addr, true)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tisApprovedForAll = dummy.IsApprovedForAll(caller, addr)\n\tuassert.True(t, isApprovedForAll)\n\n\t// Test setting approval to false\n\terr = dummy.SetApprovalForAll(caller, addr, false)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tisApprovedForAll = dummy.IsApprovedForAll(caller, addr)\n\tuassert.False(t, isApprovedForAll)\n}\n\nfunc TestGetApproved(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\t_, err := dummy.GetApproved(TokenID(\"invalid\"))\n\tuassert.Error(t, err, \"should result in error\")\n}\n\nfunc TestApprove(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\terr := dummy.mint(alice, TokenID(\"1\"))\n\tuassert.NoError(t, err, \"failed to mint\")\n\n\t_, err = dummy.GetApproved(TokenID(\"1\"))\n\tuassert.Error(t, err, \"should result in error\")\n\n\terr = dummy.Approve(alice, addr, TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tapprovedAddr, err := dummy.GetApproved(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should result in error\")\n\tuassert.Equal(t, addr.String(), approvedAddr.String())\n}\n\nfunc TestTransferFrom(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\tdummy.mint(alice, TokenID(\"1\"))\n\tdummy.mint(alice, TokenID(\"2\"))\n\n\terr := dummy.TransferFrom(alice, alice, addr, TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should result in error\")\n\n\t// Check balance of alice after transfer\n\tbalanceOfAlice, err := dummy.BalanceOf(alice)\n\tuassert.NoError(t, err, \"should result in error\")\n\tuassert.Equal(t, int64(1), balanceOfAlice)\n\n\t// Check balance of addr after transfer\n\tbalanceOfAddr, err := dummy.BalanceOf(addr)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(1), balanceOfAddr)\n\n\t// Check Owner of transferred Token id\n\towner, err := dummy.OwnerOf(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should result in error\")\n\tuassert.Equal(t, addr.String(), owner.String())\n}\n\nfunc TestSafeTransferFrom(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\tdummy.mint(alice, TokenID(\"1\"))\n\tdummy.mint(alice, TokenID(\"2\"))\n\n\terr := dummy.SafeTransferFrom(alice, alice, addr, TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\t// Check balance of alice after transfer\n\tbalanceOfAlice, err := dummy.BalanceOf(alice)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(1), balanceOfAlice)\n\n\t// Check balance of addr after transfer\n\tbalanceOfAddr, err := dummy.BalanceOf(addr)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(1), balanceOfAddr)\n\n\t// Check Owner of transferred Token id\n\towner, err := dummy.OwnerOf(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, addr.String(), owner.String())\n}\n\nfunc TestMint(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\terr := dummy.Mint(addr1, TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\terr = dummy.Mint(addr1, TokenID(\"2\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\terr = dummy.Mint(addr2, TokenID(\"3\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\t// Try minting duplicate token id\n\terr = dummy.Mint(addr2, TokenID(\"1\"))\n\tuassert.Error(t, err, \"should not result in error\")\n\n\t// Check Owner of Token id\n\towner, err := dummy.OwnerOf(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, addr1.String(), owner.String())\n}\n\nfunc TestBurn(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\tdummy.mint(addr, TokenID(\"1\"))\n\tdummy.mint(addr, TokenID(\"2\"))\n\n\terr := dummy.Burn(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\t// Check Owner of Token id\n\t_, err = dummy.OwnerOf(TokenID(\"1\"))\n\tuassert.Error(t, err, \"should result in error\")\n}\n\nfunc TestSetTokenURI(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\ttokenURI := \"http://example.com/token\"\n\n\tdummy.mint(addr1, TokenID(\"1\"))\n\n\t_, derr := dummy.SetTokenURI(addr1, TokenID(\"1\"), TokenURI(tokenURI))\n\tuassert.NoError(t, derr, \"should not result in error\")\n\n\t// Test case: Invalid token ID\n\t_, err := dummy.SetTokenURI(addr1, TokenID(\"3\"), TokenURI(tokenURI))\n\tuassert.ErrorIs(t, err, ErrInvalidTokenId)\n\n\t// addr2 trying to set URI for token 1 (not owner)\n\t_, cerr := dummy.SetTokenURI(addr2, TokenID(\"1\"), TokenURI(tokenURI))\n\tuassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)\n\n\tdummyTokenURI, err := dummy.TokenURI(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"TokenURI error\")\n\tuassert.Equal(t, string(tokenURI), string(dummyTokenURI))\n}\n\nfunc TestIsApprovedOrOwner(cur realm, t *testing.T) {\n\tdummy := newDummyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tvar (\n\t\towner    = testutils.TestAddress(\"owner\")\n\t\toperator = testutils.TestAddress(\"operator\")\n\t\tapproved = testutils.TestAddress(\"approved\")\n\t\tother    = testutils.TestAddress(\"other\")\n\t)\n\n\ttid := TokenID(\"1\")\n\n\terr := dummy.mint(owner, tid)\n\tuassert.NoError(t, err)\n\n\t// check owner\n\tisApprovedOrOwner := dummy.isApprovedOrOwner(owner, tid)\n\tuassert.True(t, isApprovedOrOwner, \"owner should be approved\")\n\n\t// check operator\n\terr = dummy.SetApprovalForAll(owner, operator, true)\n\tuassert.NoError(t, err)\n\tisApprovedOrOwner = dummy.isApprovedOrOwner(operator, tid)\n\tuassert.True(t, isApprovedOrOwner, \"operator should be approved\")\n\n\t// check approved\n\terr = dummy.Approve(owner, approved, tid)\n\tuassert.NoError(t, err)\n\tisApprovedOrOwner = dummy.isApprovedOrOwner(approved, tid)\n\tuassert.True(t, isApprovedOrOwner, \"approved address should be approved\")\n\n\t// check other\n\tisApprovedOrOwner = dummy.isApprovedOrOwner(other, tid)\n\tuassert.False(t, isApprovedOrOwner, \"other address should not be approved\")\n\n\t// check non-existent token\n\tisApprovedOrOwner = dummy.isApprovedOrOwner(owner, TokenID(\"999\"))\n\tuassert.False(t, isApprovedOrOwner, \"non-existent token should not be approved\")\n}\n"},{"name":"errors.gno","body":"package grc721\n\nimport \"errors\"\n\nvar (\n\tErrInvalidTokenId              = errors.New(\"invalid token id\")\n\tErrInvalidAddress              = errors.New(\"invalid address\")\n\tErrTokenIdNotHasApproved       = errors.New(\"token id not approved for anyone\")\n\tErrApprovalToCurrentOwner      = errors.New(\"approval to current owner\")\n\tErrCallerIsNotOwner            = errors.New(\"caller is not token owner\")\n\tErrCallerNotApprovedForAll     = errors.New(\"caller is not approved for all\")\n\tErrCannotTransferToSelf        = errors.New(\"cannot send transfer to self\")\n\tErrTransferFromIncorrectOwner  = errors.New(\"transfer from incorrect owner\")\n\tErrTransferToNonGRC721Receiver = errors.New(\"transfer to non GRC721Receiver implementer\")\n\tErrCallerIsNotOwnerOrApproved  = errors.New(\"caller is not token owner or approved\")\n\tErrTokenIdAlreadyExists        = errors.New(\"token id already exists\")\n\n\t// NewBasicNFT realm binding\n\tErrSpoofedRealm  = errors.New(\"rlm does not match the current crossing frame\")\n\tErrNotRealm      = errors.New(\"rlm must be a realm (got EOA/origin)\")\n\tErrInvalidName   = errors.New(\"invalid token name (empty, too long, or contains control chars)\")\n\tErrInvalidSymbol = errors.New(\"invalid token symbol (empty, too long, or contains chars outside [A-Za-z0-9_-])\")\n\n\t// ERC721Royalty\n\tErrInvalidRoyaltyPercentage     = errors.New(\"invalid royalty percentage\")\n\tErrInvalidRoyaltyPaymentAddress = errors.New(\"invalid royalty paymentAddress\")\n\tErrCannotCalculateRoyaltyAmount = errors.New(\"cannot calculate royalty amount\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tokens/grc721\"\ngno = \"0.9\"\n"},{"name":"grc721_metadata.gno","body":"package grc721\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// metadataNFT represents an NFT with metadata extensions.\ntype metadataNFT struct {\n\t*BasicNFT\n\textensions *avl.Tree // AVL tree for storing metadata extensions\n}\n\n// Ensure that metadataNFT implements the IGRC721MetadataOnchain interface.\nvar _ IGRC721MetadataOnchain = (*metadataNFT)(nil)\n\n// NewNFTWithMetadata creates a new basic NFT with metadata extensions.\nfunc NewNFTWithMetadata(_ int, rlm realm, name, symbol string) *metadataNFT {\n\treturn \u0026metadataNFT{\n\t\tBasicNFT:   NewBasicNFT(0, rlm, name, symbol),\n\t\textensions: avl.NewTree(),\n\t}\n}\n\n// SetTokenMetadata sets metadata for a given token ID. The token must exist and\n// caller must equal its owner; the owning realm's wrapper derives caller from\n// rlm.Previous().Address() under IsCurrent (mirrors SetTokenRoyalty).\nfunc (s *metadataNFT) SetTokenMetadata(caller address, tid TokenID, metadata Metadata) error {\n\t// Check that the token exists and the caller is its owner.\n\towner, err := s.OwnerOf(tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif caller != owner {\n\t\treturn ErrCallerIsNotOwner\n\t}\n\n\t// Set the metadata for the token ID in the extensions AVL tree\n\ts.extensions.Set(tid.String(), metadata)\n\n\tchain.Emit(\n\t\tMetadataUpdateEvent,\n\t\t\"token\", s.ID(),\n\t\t\"tokenId\", tid.String(),\n\t)\n\n\treturn nil\n}\n\n// TokenMetadata retrieves metadata for a given token ID.\nfunc (s *metadataNFT) TokenMetadata(tid TokenID) (Metadata, error) {\n\t// Retrieve metadata from the extensions AVL tree\n\tmetadata := s.extensions.Get(tid.String())\n\tif metadata == nil {\n\t\treturn Metadata{}, ErrInvalidTokenId\n\t}\n\n\treturn metadata.(Metadata), nil\n}\n\n// Basic NFT methods forwarded to embedded BasicNFT\n\nfunc (s *metadataNFT) Name() string {\n\treturn s.BasicNFT.Name()\n}\n\nfunc (s *metadataNFT) Symbol() string {\n\treturn s.BasicNFT.Symbol()\n}\n\nfunc (s *metadataNFT) TokenCount() int64 {\n\treturn s.BasicNFT.TokenCount()\n}\n\nfunc (s *metadataNFT) BalanceOf(addr address) (int64, error) {\n\treturn s.BasicNFT.BalanceOf(addr)\n}\n\nfunc (s *metadataNFT) OwnerOf(tid TokenID) (address, error) {\n\treturn s.BasicNFT.OwnerOf(tid)\n}\n\nfunc (s *metadataNFT) TokenURI(tid TokenID) (string, error) {\n\treturn s.BasicNFT.TokenURI(tid)\n}\n\nfunc (s *metadataNFT) SetTokenURI(caller address, tid TokenID, tURI TokenURI) (bool, error) {\n\treturn s.BasicNFT.SetTokenURI(caller, tid, tURI)\n}\n\nfunc (s *metadataNFT) IsApprovedForAll(owner, operator address) bool {\n\treturn s.BasicNFT.IsApprovedForAll(owner, operator)\n}\n\nfunc (s *metadataNFT) Approve(caller, to address, tid TokenID) error {\n\treturn s.BasicNFT.Approve(caller, to, tid)\n}\n\nfunc (s *metadataNFT) GetApproved(tid TokenID) (address, error) {\n\treturn s.BasicNFT.GetApproved(tid)\n}\n\nfunc (s *metadataNFT) SetApprovalForAll(caller, operator address, approved bool) error {\n\treturn s.BasicNFT.SetApprovalForAll(caller, operator, approved)\n}\n\nfunc (s *metadataNFT) SafeTransferFrom(caller, from, to address, tid TokenID) error {\n\treturn s.BasicNFT.SafeTransferFrom(caller, from, to, tid)\n}\n\nfunc (s *metadataNFT) TransferFrom(caller, from, to address, tid TokenID) error {\n\treturn s.BasicNFT.TransferFrom(caller, from, to, tid)\n}\n\nfunc (s *metadataNFT) Mint(to address, tid TokenID) error {\n\treturn s.BasicNFT.Mint(to, tid)\n}\n\nfunc (s *metadataNFT) SafeMint(to address, tid TokenID) error {\n\treturn s.BasicNFT.SafeMint(to, tid)\n}\n\nfunc (s *metadataNFT) Burn(tid TokenID) error {\n\treturn s.BasicNFT.Burn(tid)\n}\n\nfunc (s *metadataNFT) RenderHome() string {\n\treturn s.BasicNFT.RenderHome()\n}\n"},{"name":"grc721_metadata_test.gno","body":"package grc721\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc newDummyMetadataNFT(_ int, rlm realm) *metadataNFT {\n\tvar nft *metadataNFT\n\tfunc(cur realm) {\n\t\tnft = NewNFTWithMetadata(0, cur, dummyNFTName, dummyNFTSymbol)\n\t}(cross(rlm))\n\treturn nft\n}\n\nfunc TestSetMetadata(cur realm, t *testing.T) {\n\t// Create a new dummy NFT with metadata\n\tdummy := newDummyMetadataNFT(0, cur)\n\tif dummy == nil {\n\t\tt.Errorf(\"should not be nil\")\n\t}\n\n\t// Define addresses for testing purposes\n\taddr1 := testutils.TestAddress(\"alice\")\n\taddr2 := testutils.TestAddress(\"bob\")\n\n\t// Define metadata attributes\n\tname := \"test\"\n\tdescription := \"test\"\n\timage := \"test\"\n\timageData := \"test\"\n\texternalURL := \"test\"\n\tattributes := []Trait{}\n\tbackgroundColor := \"test\"\n\tanimationURL := \"test\"\n\tyoutubeURL := \"test\"\n\n\t// Mint a new token for addr1\n\tdummy.mint(addr1, TokenID(\"1\"))\n\n\t// Set metadata for token 1 as its owner (addr1)\n\tderr := dummy.SetTokenMetadata(addr1, TokenID(\"1\"), Metadata{\n\t\tName:            name,\n\t\tDescription:     description,\n\t\tImage:           image,\n\t\tImageData:       imageData,\n\t\tExternalURL:     externalURL,\n\t\tAttributes:      attributes,\n\t\tBackgroundColor: backgroundColor,\n\t\tAnimationURL:    animationURL,\n\t\tYoutubeURL:      youtubeURL,\n\t})\n\n\t// Check if there was an error setting metadata\n\tuassert.NoError(t, derr, \"Should not result in error\")\n\n\t// Test case: invalid token ID. Setting metadata for a non-existent token fails.\n\terr := dummy.SetTokenMetadata(addr1, TokenID(\"3\"), Metadata{\n\t\tName:            name,\n\t\tDescription:     description,\n\t\tImage:           image,\n\t\tImageData:       imageData,\n\t\tExternalURL:     externalURL,\n\t\tAttributes:      attributes,\n\t\tBackgroundColor: backgroundColor,\n\t\tAnimationURL:    animationURL,\n\t\tYoutubeURL:      youtubeURL,\n\t})\n\n\tuassert.ErrorIs(t, err, ErrInvalidTokenId)\n\n\t// Test case: caller is not the owner. Setting metadata from a non-owner (addr2) fails.\n\tcerr := dummy.SetTokenMetadata(addr2, TokenID(\"1\"), Metadata{\n\t\tName:            name,\n\t\tDescription:     description,\n\t\tImage:           image,\n\t\tImageData:       imageData,\n\t\tExternalURL:     externalURL,\n\t\tAttributes:      attributes,\n\t\tBackgroundColor: backgroundColor,\n\t\tAnimationURL:    animationURL,\n\t\tYoutubeURL:      youtubeURL,\n\t})\n\n\tuassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)\n\n\t// Retrieve metadata for token 1\n\tdummyMetadata, err := dummy.TokenMetadata(TokenID(\"1\"))\n\tuassert.NoError(t, err, \"Metadata error\")\n\n\t// Check if metadata attributes match expected values\n\tuassert.Equal(t, image, dummyMetadata.Image)\n\tuassert.Equal(t, imageData, dummyMetadata.ImageData)\n\tuassert.Equal(t, externalURL, dummyMetadata.ExternalURL)\n\tuassert.Equal(t, description, dummyMetadata.Description)\n\tuassert.Equal(t, name, dummyMetadata.Name)\n\tuassert.Equal(t, len(attributes), len(dummyMetadata.Attributes))\n\tuassert.Equal(t, backgroundColor, dummyMetadata.BackgroundColor)\n\tuassert.Equal(t, animationURL, dummyMetadata.AnimationURL)\n\tuassert.Equal(t, youtubeURL, dummyMetadata.YoutubeURL)\n}\n"},{"name":"grc721_royalty.gno","body":"package grc721\n\nimport (\n\t\"math/overflow\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// royaltyNFT represents a non-fungible token (NFT) with royalty functionality.\ntype royaltyNFT struct {\n\t*metadataNFT                   // Embedding metadataNFT for NFT functionality\n\ttokenRoyaltyInfo     *avl.Tree // AVL tree to store royalty information for each token\n\tmaxRoyaltyPercentage int64     // maxRoyaltyPercentage represents the maximum royalty percentage that can be charged every sale\n}\n\n// Ensure that royaltyNFT implements the IGRC2981 interface.\nvar _ IGRC2981 = (*royaltyNFT)(nil)\n\n// NewNFTWithRoyalty creates a new royalty NFT with the specified name, symbol, and royalty calculator.\nfunc NewNFTWithRoyalty(_ int, rlm realm, name, symbol string) *royaltyNFT {\n\treturn \u0026royaltyNFT{\n\t\tmetadataNFT:          NewNFTWithMetadata(0, rlm, name, symbol),\n\t\ttokenRoyaltyInfo:     avl.NewTree(),\n\t\tmaxRoyaltyPercentage: 100,\n\t}\n}\n\n// SetTokenRoyalty sets the royalty information for a specific token ID.\n// caller must equal the token's owner; the owning realm's wrapper\n// derives caller from rlm.Previous().Address() under IsCurrent.\nfunc (r *royaltyNFT) SetTokenRoyalty(caller address, tid TokenID, royaltyInfo RoyaltyInfo) error {\n\t// Validate the payment address\n\tif err := isValidAddress(royaltyInfo.PaymentAddress); err != nil {\n\t\treturn ErrInvalidRoyaltyPaymentAddress\n\t}\n\n\t// Check if royalty percentage exceeds maxRoyaltyPercentage\n\tif royaltyInfo.Percentage \u003e r.maxRoyaltyPercentage {\n\t\treturn ErrInvalidRoyaltyPercentage\n\t}\n\n\t// Check if the caller is the owner of the token\n\towner, err := r.metadataNFT.OwnerOf(tid)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif caller != owner {\n\t\treturn ErrCallerIsNotOwner\n\t}\n\n\t// Set royalty information for the token\n\tr.tokenRoyaltyInfo.Set(tid.String(), royaltyInfo)\n\n\treturn nil\n}\n\n// RoyaltyInfo returns the royalty information for the given token ID and sale price.\nfunc (r *royaltyNFT) RoyaltyInfo(tid TokenID, salePrice int64) (address, int64, error) {\n\t// Retrieve royalty information for the token\n\tval := r.tokenRoyaltyInfo.Get(tid.String())\n\tif val == nil {\n\t\treturn \"\", 0, ErrInvalidTokenId\n\t}\n\n\troyaltyInfo := val.(RoyaltyInfo)\n\n\t// Calculate royalty amount\n\troyaltyAmount, _ := r.calculateRoyaltyAmount(salePrice, royaltyInfo.Percentage)\n\n\treturn royaltyInfo.PaymentAddress, royaltyAmount, nil\n}\n\nfunc (r *royaltyNFT) calculateRoyaltyAmount(salePrice, percentage int64) (int64, error) {\n\troyaltyAmount := overflow.Mul64p(salePrice, percentage) / 100\n\treturn royaltyAmount, nil\n}\n"},{"name":"grc721_royalty_test.gno","body":"package grc721\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc newDummyRoyaltyNFT(_ int, rlm realm) *royaltyNFT {\n\tvar nft *royaltyNFT\n\tfunc(cur realm) {\n\t\tnft = NewNFTWithRoyalty(0, cur, dummyNFTName, dummyNFTSymbol)\n\t}(cross(rlm))\n\treturn nft\n}\n\nfunc TestSetTokenRoyalty(cur realm, t *testing.T) {\n\tdummy := newDummyRoyaltyNFT(0, cur)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := testutils.TestAddress(\"alice\")\n\taddr2 := testutils.TestAddress(\"bob\")\n\n\tpaymentAddress := testutils.TestAddress(\"john\")\n\tpercentage := int64(10) // 10%\n\n\tsalePrice := int64(1000)\n\texpectRoyaltyAmount := int64(100)\n\n\tdummy.mint(addr1, TokenID(\"1\"))\n\n\tderr := dummy.SetTokenRoyalty(addr1, TokenID(\"1\"), RoyaltyInfo{\n\t\tPaymentAddress: paymentAddress,\n\t\tPercentage:     percentage,\n\t})\n\tuassert.NoError(t, derr, \"Should not result in error\")\n\n\t// Test case: Invalid token ID\n\terr := dummy.SetTokenRoyalty(addr1, TokenID(\"3\"), RoyaltyInfo{\n\t\tPaymentAddress: paymentAddress,\n\t\tPercentage:     percentage,\n\t})\n\tuassert.ErrorIs(t, err, ErrInvalidTokenId)\n\n\t// Test case: Invalid payment address\n\taerr := dummy.SetTokenRoyalty(addr1, TokenID(\"4\"), RoyaltyInfo{\n\t\tPaymentAddress: address(\"###\"), // invalid address\n\t\tPercentage:     percentage,\n\t})\n\tuassert.ErrorIs(t, aerr, ErrInvalidRoyaltyPaymentAddress)\n\n\t// Test case: Invalid percentage\n\tperr := dummy.SetTokenRoyalty(addr1, TokenID(\"5\"), RoyaltyInfo{\n\t\tPaymentAddress: paymentAddress,\n\t\tPercentage:     int64(200), // over maxRoyaltyPercentage\n\t})\n\tuassert.ErrorIs(t, perr, ErrInvalidRoyaltyPercentage)\n\n\t// addr2 is not the owner.\n\tcerr := dummy.SetTokenRoyalty(addr2, TokenID(\"1\"), RoyaltyInfo{\n\t\tPaymentAddress: paymentAddress,\n\t\tPercentage:     percentage,\n\t})\n\tuassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)\n\n\tdummyPaymentAddress, dummyRoyaltyAmount, rerr := dummy.RoyaltyInfo(TokenID(\"1\"), salePrice)\n\tuassert.NoError(t, rerr, \"RoyaltyInfo error\")\n\tuassert.Equal(t, paymentAddress, dummyPaymentAddress)\n\tuassert.Equal(t, expectRoyaltyAmount, dummyRoyaltyAmount)\n}\n"},{"name":"igrc721.gno","body":"package grc721\n\n// IGRC721Reader is the read-only view of an NFT. Safe to receive across\n// realm boundaries — has no rlm-typed methods, so a malicious impl can\n// only lie about read results (data-integrity issue), not capture cur.\n//\n// Writes are concrete methods on *BasicNFT / *metadataNFT / *royaltyNFT\n// only — there is no IGRC721 writer interface. The owning realm should\n// hold the concrete *BasicNFT in an unexported package var and expose\n// public rlm-validating wrappers like:\n//\n//\tfunc TransferFrom(cur realm, from, to address, tid TokenID) error {\n//\t\tcaller := cur.Previous().Address()\n//\t\treturn nft.TransferFrom(caller, from, to, tid)\n//\t}\n//\n// This is the Reader/Writer split — stronger than the Authority-pattern\n// because the writer interface doesn't exist at all, so no realm author\n// can accidentally expose it. See `r/demo/foo721` for the canonical\n// wrapper pattern.\ntype IGRC721Reader interface {\n\tName() string\n\tSymbol() string\n\tTokenCount() int64\n\tBalanceOf(owner address) (int64, error)\n\tOwnerOf(tid TokenID) (address, error)\n\tGetApproved(tid TokenID) (address, error)\n\tIsApprovedForAll(owner, operator address) bool\n}\n\ntype (\n\tTokenID  string\n\tTokenURI string\n)\n\nfunc (t TokenID) String() string  { return string(t) }\nfunc (t TokenURI) String() string { return string(t) }\n\nconst (\n\tMintEvent           = \"Mint\"\n\tBurnEvent           = \"Burn\"\n\tTransferEvent       = \"Transfer\"\n\tApprovalEvent       = \"Approval\"\n\tApprovalForAllEvent = \"ApprovalForAll\"\n\tTokenURIUpdateEvent = \"TokenUriUpdate\"\n\tMetadataUpdateEvent = \"MetadataUpdate\"\n)\n\n// NFTGetter returns a reader-only view of an NFT. Aggregators (such as\n// tokenhub) register and dispatch NFTGetters; the reader-only return\n// type means even a malicious aggregator can't be used to leak cur.\ntype NFTGetter func() IGRC721Reader\n"},{"name":"igrc721_metadata.gno","body":"package grc721\n\n// IGRC721CollectionMetadata describes basic information about an NFT collection.\ntype IGRC721CollectionMetadata interface {\n\tName() string   // Name returns the name of the collection.\n\tSymbol() string // Symbol returns the symbol of the collection.\n}\n\n// IGRC721Metadata follows the Ethereum standard\ntype IGRC721Metadata interface {\n\tIGRC721CollectionMetadata\n\tTokenURI(tid TokenID) (string, error) // TokenURI returns the URI of a specific token.\n}\n\n// IGRC721Metadata follows the OpenSea metadata standard\ntype IGRC721MetadataOnchain interface {\n\tIGRC721CollectionMetadata\n\tTokenMetadata(tid TokenID) (Metadata, error)\n}\n\ntype Trait struct {\n\tDisplayType string\n\tTraitType   string\n\tValue       string\n}\n\n// see: https://docs.opensea.io/docs/metadata-standards\ntype Metadata struct {\n\tImage           string  // URL to the image of the item. Can be any type of image (including SVGs, which will be cached into PNGs by OpenSea), IPFS or Arweave URLs or paths. We recommend using a minimum 3000 x 3000 image.\n\tImageData       string  // Raw SVG image data, if you want to generate images on the fly (not recommended). Only use this if you're not including the image parameter.\n\tExternalURL     string  // URL that will appear below the asset's image on OpenSea and will allow users to leave OpenSea and view the item on your site.\n\tDescription     string  // Human-readable description of the item. Markdown is supported.\n\tName            string  // Name of the item.\n\tAttributes      []Trait // Attributes for the item, which will show up on the OpenSea page for the item.\n\tBackgroundColor string  // Background color of the item on OpenSea. Must be a six-character hexadecimal without a pre-pended #\n\tAnimationURL    string  // URL to a multimedia attachment for the item. Supported file extensions: GLTF, GLB, WEBM, MP4, M4V, OGV, OGG, MP3, WAV, OGA, HTML (for rich experiences and interactive NFTs using JavaScript canvas, WebGL, etc.). Scripts and relative paths within the HTML page are now supported. Access to browser extensions is not supported.\n\tYoutubeURL      string  // URL to a YouTube video (only used if animation_url is not provided).\n}\n"},{"name":"igrc721_royalty.gno","body":"package grc721\n\n// IGRC2981 follows the Ethereum standard\ntype IGRC2981 interface {\n\t// RoyaltyInfo retrieves royalty information for a tokenID and salePrice.\n\t// It returns the payment address, royalty amount, and an error if any.\n\tRoyaltyInfo(tokenID TokenID, salePrice int64) (address, int64, error)\n}\n\n// RoyaltyInfo represents royalty information for a token.\ntype RoyaltyInfo struct {\n\tPaymentAddress address // PaymentAddress is the address where royalty payment should be sent.\n\tPercentage     int64   // Percentage is the royalty percentage. It indicates the percentage of royalty to be paid for each sale. For example : Percentage = 10 =\u003e 10%\n}\n"},{"name":"util.gno","body":"package grc721\n\nconst (\n\tMaxNameLen   = 64\n\tMaxSymbolLen = 11\n)\n\nvar zeroAddress = address(\"\")\n\nfunc isValidAddress(addr address) error {\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\treturn nil\n}\n\nfunc validName(name string) bool {\n\tif name == \"\" || len(name) \u003e MaxNameLen {\n\t\treturn false\n\t}\n\tfor _, c := range name {\n\t\tif c \u003c 0x20 || c == 0x7f {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// symbol is emitted as part of the event identifier, so bounding it\n// keeps events under MaxEventAttrLen.\nfunc validSymbol(symbol string) bool {\n\tif symbol == \"\" || len(symbol) \u003e MaxSymbolLen {\n\t\treturn false\n\t}\n\tfor _, c := range symbol {\n\t\tif !isAlnum(c) \u0026\u0026 c != '_' \u0026\u0026 c != '-' {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc isAlnum(c rune) bool {\n\treturn (c \u003e= 'a' \u0026\u0026 c \u003c= 'z') || (c \u003e= 'A' \u0026\u0026 c \u003c= 'Z') || (c \u003e= '0' \u0026\u0026 c \u003c= '9')\n}\n\nfunc emit(event any) {\n\t// TODO: setup a pubsub system here?\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"SzfA4wvgxpO/EP5/iPZ9Hmn27AG+njIOpqvbcgkMm2F1/6P8cfjQE2lLxv0btTAJRNewhee53Ko9EH3o1fZVBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"bptree","path":"gno.land/p/nt/bptree/v0","files":[{"name":"PLAN.md","body":"# Mutable B+ Tree for Gno\n\n## Goal\n\nA mutable (in-place) B+ tree with the same API as `gno.land/p/nt/avl/v0`.\nNo merkle hashing, no versioning, no persistence — just a simple, efficient\nordered map with configurable fanout.\n\n## API\n\n```go\n// Constructors\nfunc NewBPTreeN(fanout int) *BPTree   // arbitrary fanout (minimum 4)\nfunc NewBPTree32() *BPTree            // convenience: fanout 32\n\n// ITree interface (same as avl.ITree)\ntype ITree interface {\n    Size() int\n    Has(key string) bool\n    Get(key string) any\n    GetByIndex(index int) (key string, value any)\n    Iterate(start, end string, cb IterCbFn) bool\n    ReverseIterate(start, end string, cb IterCbFn) bool\n    IterateByOffset(offset int, count int, cb IterCbFn) bool\n    ReverseIterateByOffset(offset int, count int, cb IterCbFn) bool\n    Set(key string, value any) (updated bool)\n    Remove(key string) (value any, removed bool)\n}\n\ntype IterCbFn func(key string, value any) bool\n```\n\n`var _ ITree = (*BPTree)(nil)` enforces the interface at compile time.\n\n## Semantics (matching avl exactly)\n\n- `Set`: insert or update. Returns true if key already existed.\n- `Remove`: delete key. Returns (old value, true) if found.\n- `GetByIndex`: 0-based index into sorted keys. Panics on invalid index.\n- `Iterate(start, end, cb)`: ascending, start inclusive, end exclusive.\n  Empty string means no bound.\n- `ReverseIterate(start, end, cb)`: descending, start inclusive, end inclusive.\n  Empty string means no bound.\n- `IterateByOffset(offset, count, cb)`: ascending from the offset-th leaf entry\n  (0-indexed from the smallest key). Returns false if offset \u003e= size or count \u003c= 0.\n- `ReverseIterateByOffset(offset, count, cb)`: descending, where offset is\n  0-indexed from the largest key. offset=0 starts at the largest key,\n  offset=1 skips the largest and starts at the second-largest, etc.\n  Equivalent to: visit entries in descending order, skip `offset`, take `count`.\n- All iteration callbacks return true to stop early.\n\n## Node types\n\n### leafNode\n\n```go\ntype leafNode struct {\n    keys   []string  // sorted, len \u003c= fanout\n    values []*any    // parallel to keys\n}\n```\n\nLeaf nodes store all key-value data. No sibling pointers — iteration\nuses stack-based traversal to avoid ref-count \u003e= 2 (see\n[Ref-Count Safety](#ref-count-safety)).\n\nCapacity: up to `fanout` entries. Splits when full after insert.\nMinimum occupancy enforced during deletion: `fanout/2` (except the root leaf).\nNote: the 90/10 split optimization intentionally creates a right leaf with\nonly 2 entries, which may be below `fanout/2` for large fanouts. This is\nstandard B+ tree practice — if a subsequent Remove causes underflow, the\nnormal rebalance logic (redistribute or merge) handles it.\n\n### innerNode\n\n```go\ntype innerNode struct {\n    keys     []string // separator keys, len = len(children)-1\n    children []node   // child pointers, len \u003c= fanout\n    sizes    []int    // sizes[i] = total leaf count in children[i] subtree\n}\n```\n\n`keys[i]` = minimum key of `children[i+1]` (standard B+ tree convention).\nAn inner node with k separator keys has k+1 children.\n\nCapacity: up to `fanout` children (= `fanout-1` keys). Splits when full.\nMinimum occupancy: `fanout/2` children (except root, which may have as few as 2).\n\n### node interface\n\n```go\ntype node interface {\n    isLeaf() bool\n    nodeSize() int    // total leaf entries in subtree\n    minKey() string   // leftmost key in subtree\n}\n```\n\n`nodeSize()`: leafNode returns `len(keys)` (O(1)), innerNode sums `sizes[]` (O(fanout)).\n\nUsed internally so tree methods can handle both node types polymorphically.\nType assertions to `*leafNode` or `*innerNode` are used when accessing\ntype-specific fields (children, sizes).\n\n## BPTree struct\n\n```go\ntype BPTree struct {\n    root   node\n    size   int       // total number of key-value pairs\n    fanout int       // max children per inner node / max entries per leaf\n}\n```\n\nNo `first`/`last` pointers — they would create ref-count \u003e= 2 on leaves.\nFull-range iteration descends from the root (O(height) to find the first\nor last leaf, amortized O(1) per entry thereafter).\n\n## Key algorithms\n\n### Search (Get, Has)\n\nDescend from root. At each inner node, binary search `keys` to find the\nchild index. At the leaf, binary search `keys` for exact match.\n\n### Insert (Set)\n\n1. Descend root-to-leaf, recording the path (stack of `(innerNode, childIdx)` pairs).\n2. Binary search in the leaf for the key.\n3. If found: update value in place, return `updated=true`. No structural change.\n4. If not found: insert key-value at the sorted position.\n   - Increment `size` on the tree and all ancestor `sizes[childIdx]` entries.\n   - If the leaf now has `fanout+1` entries, **split**:\n     - **90/10 split** (append pattern): if the new key was inserted at\n       position `fanout` in the overflowed leaf (i.e., it is greater than\n       all pre-existing keys), split asymmetrically: left gets `fanout-1` entries, right\n       gets 2 entries (the last existing key + the new key). This keeps left\n       leaves ~97% full for sequential inserts.\n     - **50/50 split** (random pattern): otherwise, left gets `(fanout+1)/2`\n       (floor), right gets the rest.\n     - Create new right leaf node.\n     - Promote `right.keys[0]` as separator to the parent.\n     - Update parent's `sizes` for both children.\n     - If the parent now has `fanout+1` children, split the parent recursively.\n   - If the root splits, create a new inner root with 2 children.\n\n### Remove\n\n1. Descend root-to-leaf, recording the path.\n2. Binary search in the leaf for the key.\n3. If not found: return `(nil, false)`.\n4. If found: remove entry, decrement `size` and ancestor `sizes`.\n   - If the leaf is the root and now empty: set root to nil.\n   - If the leaf is the root and non-empty: done (root has no minimum).\n   - Otherwise, if leaf has fewer than `fanout/2` entries, **rebalance**:\n     - Try to **redistribute** from left sibling (if it has more than `fanout/2`).\n     - Try to **redistribute** from right sibling (if it has more than `fanout/2`).\n     - Otherwise **merge** with a sibling:\n       - Concatenate into the left node, remove the right child and its\n         separator from parent.\n       - Update parent's `sizes` and `size`.\n       - If parent is the root and drops to 1 child, replace root with that child.\n       - If parent is not the root and has fewer than `fanout/2` children,\n         rebalance recursively (the root is exempt — it may have as few as 2 children).\n   - If the minimum key was removed (pos == 0), update ancestor separator keys\n     before rebalancing. Rebalance operations also fix separators as needed.\n\n### Redistribute detail\n\n**Redistribute from left sibling to deficient child (both leaves):**\n1. Move left sibling's last key-value to the front of the deficient child.\n2. Update parent `keys[childIdx-1]` = deficient child's new `keys[0]` (its min key changed).\n3. Adjust parent `sizes`: decrement left's size, increment child's size.\n\n**Redistribute from right sibling to deficient child (both leaves):**\n1. Move right sibling's first key-value to the end of the deficient child.\n2. Update parent `keys[childIdx]` = right sibling's new `keys[0]` (its min key changed).\n3. Adjust parent `sizes`: decrement right's size, increment child's size.\n\n**Redistribute from left sibling to deficient child (both inner nodes):**\n1. Pull down parent's separator `keys[childIdx-1]` — **prepend** it to deficient child's keys\n   (insert at position 0, since the moved child goes to the front).\n2. Move left sibling's last child to the **front** of deficient child's children.\n   Move left sibling's last `sizes` entry to the front of deficient's sizes too.\n3. Push up left sibling's last key to replace parent's `keys[childIdx-1]`.\n4. Remove left sibling's last key, last child, and last size entry.\n5. Update parent's `sizes[childIdx-1]` and `sizes[childIdx]` to reflect\n   the new child sizes. (Parent's total size is unchanged since we just\n   moved entries between siblings.)\n\n**Redistribute from right sibling to deficient child (both inner nodes):**\n1. Pull down parent's separator `keys[childIdx]` — **append** it to deficient child's keys\n   (insert at the end, since the moved child goes to the end).\n2. Move right sibling's first child to the **end** of deficient child's children.\n   Move right sibling's first `sizes` entry to the end of deficient's sizes too.\n3. Push up right sibling's first key to replace parent's `keys[childIdx]`.\n4. Remove right sibling's first key, first child, and first size entry.\n5. Update parent's `sizes[childIdx]` and `sizes[childIdx+1]` to reflect\n   the new child sizes. (Parent's total size is unchanged.)\n\n**Merge two inner nodes:**\n1. Pull down the parent's separator between them into the left node's keys.\n2. Append all of right node's keys, children, and sizes to left node.\n3. Remove right child and its separator from parent.\n4. Update parent's `sizes` for the merged left child.\n\n### GetByIndex\n\nUse `sizes[]` at each inner node to find which child contains the i-th\nleaf entry, then descend. At the leaf, index directly into `keys[i]`/`values[i]`.\nPanics if index is out of range (matching avl behavior).\n\n### Stack-based iteration\n\nAll iteration uses a stack of `(innerNode, childIndex)` pairs representing\nthe path from root to the current leaf. When a leaf is exhausted, pop the\nstack, advance (or retreat) the child index, and descend to the next leaf.\nAmortized O(1) per entry — each node is pushed/popped at most once across\nthe full traversal.\n\nThe stack is a local slice built during iteration and discarded after —\nit creates no persistent references to nodes and does not affect ref-counts.\n\n### Iterate / ReverseIterate (key-range)\n\n**Ascending (Iterate):**\n1. Descend from root using separator keys to find the leaf containing `start`\n   (or descend to the leftmost leaf if start is \"\"), recording the path.\n2. Within the leaf, find the first key \u003e= start.\n3. Visit entries from that position forward.\n4. When the leaf is exhausted, advance to the next leaf via the stack:\n   pop the stack, increment childIdx. If childIdx is now past the last\n   child, pop again (repeat until a valid childIdx is found or the stack\n   is empty — if empty, iteration is done). Then descend to the leftmost\n   leaf of that child, pushing each inner node onto the stack.\n5. Stop when key \u003e= end (if end != \"\") or tree is exhausted.\n6. Call `cb(key, value)` for each entry; stop if cb returns true.\n\n**Descending (ReverseIterate):**\n1. Descend from root to find the leaf containing `end` (or the rightmost\n   leaf if end is \"\"), recording the path.\n2. Within the leaf, find the last key \u003c= end.\n3. Visit entries from that position backward.\n4. When the leaf is exhausted going backward, retreat to the previous leaf\n   via the stack: pop the stack, decrement childIdx. If childIdx \u003c 0, pop\n   again (repeat until a valid childIdx is found or the stack is empty —\n   if empty, iteration is done). Then descend to the rightmost leaf of\n   that child, pushing each inner node onto the stack.\n5. Stop when key \u003c start (if start != \"\") or tree is exhausted.\n6. Call `cb(key, value)` for each entry; stop if cb returns true.\n\n### IterateByOffset / ReverseIterateByOffset\n\n**Ascending:**\n1. Use `sizes[]` to descend to the leaf containing the offset-th entry,\n   recording the path. Maintain a running offset counter: at each inner node,\n   subtract `sizes[i]` for each skipped child. When `offset \u003c sizes[i]`,\n   descend into that child. Upon reaching a leaf, the remaining offset is\n   the position within the leaf.\n2. Visit entries from that position forward, advancing through leaves via\n   the stack (same as Iterate), counting up to `count`.\n\n**Descending:**\nThe descending view is: entries in reverse sorted order, 0-indexed from\nthe largest key. offset=0 is the largest, offset=1 is the second-largest, etc.\n\n1. Compute the ascending index of the starting entry:\n   `ascIdx = size - 1 - offset` (the entry at position `offset` in descending order).\n2. Use `sizes[]` to descend to the leaf containing entry `ascIdx`,\n   recording the path.\n3. Visit entries from that position backward, retreating through leaves via\n   the stack (same as ReverseIterate), counting up to `count`.\n4. If `ascIdx \u003c 0` or `offset \u003e= size` or `count \u003c= 0`, return false.\n\n## Split details\n\n### Leaf split\n\nGiven a leaf with `fanout+1` entries (one over capacity):\n\n**Detection:** after inserting the new key, if it ended up at position\n`fanout` in the overflowed `fanout+1`-entry leaf, it is an append-pattern\ninsert (the new key is greater than all pre-existing keys).\n\n**90/10 split (append pattern):**\n- `mid = fanout - 1`\n- Left leaf keeps entries `[0, fanout-1)` = `fanout-1` entries.\n- New right leaf gets entries `[fanout-1, fanout+1)` = 2 entries.\n- Left has `fanout-1` entries (~97% full), right has 2.\n- For large fanouts, the right leaf may be below the `fanout/2` deletion\n  threshold. This is intentional — the 90/10 split prioritizes fill factor\n  for append-heavy workloads. If a subsequent Remove causes the right leaf\n  to underflow, the standard rebalance logic handles it.\n\n**50/50 split (random pattern):**\n- `mid = (fanout + 1) / 2`\n- Left leaf keeps entries `[0, mid)`.\n- New right leaf gets entries `[mid, fanout+1)`.\n\nIn both cases:\n- Separator promoted to parent = `right.keys[0]`.\n- No linked list updates needed (no sibling pointers).\n\n### Inner split\n\nGiven an inner node with `fanout+1` children:\n- `mid = (fanout + 1) / 2`\n- Left keeps children `[0, mid)` with keys `[0, mid-1)` and sizes `[0, mid)`.\n- Right gets children `[mid, fanout+1)` with keys `[mid, fanout)` and sizes `[mid, fanout+1)`.\n- The separator at `keys[mid-1]` is **promoted** to the parent (not kept in either child).\n- Parent's sizes entry for the original child is replaced by the sum of left's sizes,\n  and a new entry is inserted for the right child with the sum of right's sizes.\n\n## Minimum fanout\n\nFanout must be \u003e= 4. With fanout 3, a leaf splits into (2, 2) and\nthe minimum occupancy is 1, which makes merge logic degenerate. Fanout 4\ngives minimum occupancy 2 and clean split/merge behavior.\n\n`NewBPTreeN` panics if fanout \u003c 4.\n\n## File structure\n\n```\nexamples/gno.land/p/nt/bptree/v0/\n  gnomod.toml\n  doc.gno          — package doc\n  node.gno         — leafNode, innerNode, node interface, binary search\n  tree.gno         — BPTree struct, constructors, ITree methods,\n                     insert/split, remove/merge/redistribute, iteration\n  tree_test.gno    — comprehensive tests (mirroring avl tests + B+ tree specifics)\n```\n\nTwo source files (`node.gno` + `tree.gno`) plus one test file. The node\ntypes and tree logic are tightly coupled, so fewer files is better than\nspreading thin.\n\n## Ref-count safety\n\nIn Gno's persistence model, objects with ref-count \u003e= 2 \"escape\" — they are\npersisted separately in an iavl tree rather than inlined in their parent's\nserialized form. Once escaped, they are forever escaped. This is expensive\nand should be avoided.\n\n**Design constraint: every node must have exactly one persistent reference.**\n\nThis means:\n- **No sibling pointers** on leaf nodes (a leaf would be referenced by both\n  its parent and its neighbor → ref-count \u003e= 2).\n- **No `first`/`last` pointers** on BPTree (a leaf would be referenced by\n  both BPTree and its parent inner node → ref-count \u003e= 2).\n- **No shared subtrees** (each child is owned by exactly one parent).\n\nThe tree structure is a pure tree (not a graph) — every node has exactly\none parent reference. The `BPTree.root` is the sole reference to the root\nnode. Each `innerNode.children[i]` is the sole reference to child `i`.\n\nIteration uses an ephemeral stack (local slice) that is built and discarded\nwithin a single method call. It does not create any persistent references.\n\n## Edge cases (must match avl behavior exactly)\n\n### Values and keys\n- `nil` is a valid value. `Set(\"foo\", nil)` stores it; `Get(\"foo\")` returns\n  `nil`; `Has(\"foo\")` returns `true`. `Remove(\"foo\")` returns `(nil, true)`.\n- `\"\"` is a valid key. Stored, retrieved, removed like any other key.\n- `Get` on missing key returns `nil` (use `Has` to distinguish from a stored\n  nil value).\n- `Remove` on missing key returns `(nil, false)`.\n- `Set` same key twice replaces value, returns `updated=true`.\n\n### Zero-value and structural\n- `var t BPTree` must work — zero-value tree is usable without a constructor.\n  All methods work on it immediately. This means `root == nil` must be handled\n  gracefully everywhere, and the default `fanout` (0) must be promoted to 32\n  on first use. `Set` promotes on first call: `if t.fanout == 0 { t.fanout = 32 }`.\n  All other methods that read `t.fanout` guard with `if t.root == nil` first, so\n  fanout is always initialized before it is read. Once set, fanout never resets\n  (even if tree becomes empty again).\n- Remove last key → tree returns to empty state (root = nil).\n- Insert after removing everything works normally.\n- `Size()` on empty tree returns 0.\n- `GetByIndex` on empty tree panics. Negative index or index \u003e= size also panics.\n- Single entry: root is a leafNode with 1 entry.\n- Root is a leaf: no inner nodes until first split.\n- Root inner node collapses: after merge leaves root with 1 child,\n  replace root with that child.\n\n### Separator key maintenance\n- When the minimum key of a subtree changes (deletion of leftmost key,\n  or redistribution), the parent's separator key must be updated.\n  After modifying a child, check if `keys[childIdx-1]` still equals\n  `children[childIdx].minKey()`.\n\n### Iteration — key range\n- All iteration on an empty tree returns `false` without calling cb.\n- `Iterate(\"\", \"\", cb)` visits ALL entries ascending (canonical pattern).\n- `ReverseIterate(\"\", \"\", cb)` visits ALL entries descending.\n- `Iterate(\"a\", \"a\", cb)` → empty. [a,a) = nothing (start inclusive, end exclusive).\n- `ReverseIterate(\"a\", \"a\", cb)` → visits \"a\". [a,a] = one entry (both inclusive).\n- `Iterate(\"z\", \"a\", cb)` → empty (no validation, logic just excludes everything).\n- `ReverseIterate(\"z\", \"a\", cb)` → empty (bounds don't swap).\n- `Iterate(\"\", \"a\", cb)` → visits all keys \u003c \"a\".\n- `ReverseIterate(\"a\", \"\", cb)` → visits all keys \u003e= \"a\", in descending order.\n- Return value = true if callback stopped iteration early, false otherwise.\n\n### Iteration — offset\n- `IterateByOffset(0, 0, cb)` → nothing (count \u003c= 0).\n- `IterateByOffset(size, 1, cb)` → nothing (offset \u003e= size).\n- `ReverseIterateByOffset(0, N, cb)` → starts at largest key, takes N descending.\n- `ReverseIterateByOffset(1, 2, cb)` on [a,b,c,d,e] → [d, c].\n- Negative count → treated as count \u003c= 0 (no iteration).\n- Negative offset → clamped to 0 (avl silently treats negative as 0; we do the same explicitly).\n"},{"name":"doc.gno","body":"// Package bptree provides a mutable B+ tree implementation for storing\n// key-value data in Gno realms. It implements the same ITree interface\n// as the avl package but uses a B+ tree internally for better cache\n// locality and fewer pointer dereferences per operation.\n//\n// The fanout (maximum number of children per inner node, and maximum\n// number of entries per leaf node) is configurable:\n//\n//\ttree := bptree.NewBPTree32()    // fanout 32\n//\ttree := bptree.NewBPTreeN(64)   // fanout 64\n//\n// The zero value is usable as an empty tree with fanout 32:\n//\n//\tvar tree bptree.BPTree\n//\ttree.Set(\"key\", \"value\")\npackage bptree\n"},{"name":"example_test.gno","body":"package bptree\n\n// ExampleNew shows NewBPTree32() and adding a value\nfunc ExampleNew() {\n\tvar tree *BPTree\n\ttree = NewBPTree32()\n\ttree.Set(\"key0\", \"value0\")\n\n\tvar updated bool\n\tupdated = tree.Set(\"key1\", \"value1\")\n\tprintln(updated, tree.Size())\n\n\t// Output:\n\t// false 2\n}\n\n// ExampleUpdate shows NewBPTree32() and updating a value\nfunc ExampleUpdate() {\n\tvar tree *BPTree\n\ttree = NewBPTree32()\n\ttree.Set(\"key0\", \"value0\")\n\n\tvar updated bool\n\tupdated = tree.Set(\"key0\", \"new_value0\")\n\tprintln(updated, tree.Size())\n\n\t// Output:\n\t// true 1\n}\n\n// ExampleZeroValue shows updating the zero value of a BPTree without NewBPTree32()\nfunc ExampleZeroValue() {\n\tvar tree BPTree\n\ttree.Set(\"key0\", \"value0\")\n\ttree.Set(\"key1\", \"value1\")\n\n\tvar updated bool\n\tupdated = tree.Set(\"key2\", \"value2\")\n\tprintln(updated, tree.Size())\n\n\t// Output:\n\t// false 3\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/bptree/v0\"\ngno = \"0.9\"\n"},{"name":"node.gno","body":"package bptree\n\n// node is the interface satisfied by both inner and leaf nodes.\ntype node interface {\n\tisLeaf() bool\n\tnodeSize() int // total leaf entries in subtree\n\tminKey() string\n}\n\n//----------------------------------------\n// leafNode\n\ntype leafNode struct {\n\tkeys   []string\n\tvalues []*any // each value is a separate object for lazy loading\n}\n\nfunc newLeafNode(fanout int) *leafNode {\n\treturn \u0026leafNode{\n\t\tkeys:   make([]string, 0, fanout),\n\t\tvalues: make([]*any, 0, fanout),\n\t}\n}\n\nfunc (n *leafNode) isLeaf() bool   { return true }\nfunc (n *leafNode) nodeSize() int  { return len(n.keys) }\nfunc (n *leafNode) minKey() string { return n.keys[0] }\n\n// find returns the index where key is or would be inserted,\n// and whether an exact match was found.\nfunc (n *leafNode) find(key string) (int, bool) {\n\tlo, hi := 0, len(n.keys)\n\tfor lo \u003c hi {\n\t\tmid := lo + (hi-lo)/2\n\t\tif n.keys[mid] \u003c key {\n\t\t\tlo = mid + 1\n\t\t} else {\n\t\t\thi = mid\n\t\t}\n\t}\n\tif lo \u003c len(n.keys) \u0026\u0026 n.keys[lo] == key {\n\t\treturn lo, true\n\t}\n\treturn lo, false\n}\n\n// insertAt inserts a key-value pair at the given position.\nfunc (n *leafNode) insertAt(pos int, key string, value *any) {\n\tn.keys = append(n.keys, \"\")\n\tcopy(n.keys[pos+1:], n.keys[pos:])\n\tn.keys[pos] = key\n\n\tn.values = append(n.values, nil)\n\tcopy(n.values[pos+1:], n.values[pos:])\n\tn.values[pos] = value\n}\n\n// removeAt removes the entry at the given position and returns the removed key and value.\nfunc (n *leafNode) removeAt(pos int) (string, *any) {\n\tkey := n.keys[pos]\n\tvalue := n.values[pos]\n\n\tcopy(n.keys[pos:], n.keys[pos+1:])\n\tn.keys[len(n.keys)-1] = \"\"\n\tn.keys = n.keys[:len(n.keys)-1]\n\n\tcopy(n.values[pos:], n.values[pos+1:])\n\tn.values[len(n.values)-1] = nil\n\tn.values = n.values[:len(n.values)-1]\n\n\treturn key, value\n}\n\n//----------------------------------------\n// innerNode\n\ntype innerNode struct {\n\tkeys     []string // separator keys; keys[i] = minKey of children[i+1]\n\tchildren []node\n\tsizes    []int // sizes[i] = total leaf entries in children[i]\n}\n\nfunc newInnerNode(fanout int) *innerNode {\n\treturn \u0026innerNode{\n\t\tkeys:     make([]string, 0, fanout-1),\n\t\tchildren: make([]node, 0, fanout),\n\t\tsizes:    make([]int, 0, fanout),\n\t}\n}\n\nfunc (n *innerNode) isLeaf() bool { return false }\nfunc (n *innerNode) nodeSize() int {\n\tsum := 0\n\tfor _, s := range n.sizes {\n\t\tsum += s\n\t}\n\treturn sum\n}\nfunc (n *innerNode) minKey() string { return n.children[0].minKey() }\n\n// findChild returns the child index for the given key.\nfunc (n *innerNode) findChild(key string) int {\n\t// Binary search: find the rightmost i where keys[i] \u003c= key.\n\tlo, hi := 0, len(n.keys)\n\tfor lo \u003c hi {\n\t\tmid := lo + (hi-lo)/2\n\t\tif n.keys[mid] \u003c= key {\n\t\t\tlo = mid + 1\n\t\t} else {\n\t\t\thi = mid\n\t\t}\n\t}\n\treturn lo\n}\n\n// insertChildAt inserts a new child with its separator key at the given position.\n// The separator key is placed at keys[pos-1] (since keys[i] = minKey of children[i+1]).\nfunc (n *innerNode) insertChildAt(pos int, sep string, child node, sz int) {\n\t// Insert separator at keys[pos-1].\n\tkeyPos := pos - 1\n\tn.keys = append(n.keys, \"\")\n\tcopy(n.keys[keyPos+1:], n.keys[keyPos:])\n\tn.keys[keyPos] = sep\n\n\t// Insert child at children[pos].\n\tn.children = append(n.children, nil)\n\tcopy(n.children[pos+1:], n.children[pos:])\n\tn.children[pos] = child\n\n\t// Insert size at sizes[pos].\n\tn.sizes = append(n.sizes, 0)\n\tcopy(n.sizes[pos+1:], n.sizes[pos:])\n\tn.sizes[pos] = sz\n}\n\n// removeChildAt removes the child at pos and its associated separator key.\nfunc (n *innerNode) removeChildAt(pos int) {\n\t// Determine which separator to remove.\n\t// keys[i] separates children[i] from children[i+1].\n\t// Removing children[pos]: if pos \u003e 0, remove keys[pos-1]; else remove keys[0].\n\tkeyPos := pos\n\tif pos \u003e 0 {\n\t\tkeyPos = pos - 1\n\t}\n\tif len(n.keys) \u003e 0 {\n\t\tcopy(n.keys[keyPos:], n.keys[keyPos+1:])\n\t\tn.keys[len(n.keys)-1] = \"\"\n\t\tn.keys = n.keys[:len(n.keys)-1]\n\t}\n\n\tcopy(n.children[pos:], n.children[pos+1:])\n\tn.children[len(n.children)-1] = nil\n\tn.children = n.children[:len(n.children)-1]\n\n\tcopy(n.sizes[pos:], n.sizes[pos+1:])\n\tn.sizes = n.sizes[:len(n.sizes)-1]\n}\n"},{"name":"tree.gno","body":"package bptree\n\ntype ITree interface {\n\tSize() int\n\tHas(key string) bool\n\tGet(key string) any\n\tGetByIndex(index int) (key string, value any)\n\tIterate(start, end string, cb IterCbFn) bool\n\tReverseIterate(start, end string, cb IterCbFn) bool\n\tIterateByOffset(offset int, count int, cb IterCbFn) bool\n\tReverseIterateByOffset(offset int, count int, cb IterCbFn) bool\n\tSet(key string, value any) (updated bool)\n\tRemove(key string) (value any, removed bool)\n}\n\ntype IterCbFn func(key string, value any) bool\n\n// Verify BPTree implements ITree.\nvar _ ITree = (*BPTree)(nil)\n\n// The zero value is usable as an empty tree with fanout 32.\ntype BPTree struct {\n\troot   node\n\tsize   int\n\tfanout int\n}\n\n// NewBPTreeN creates a new empty B+ tree with the given fanout.\n// It panics when fanout is lower than 4.\nfunc NewBPTreeN(fanout int) *BPTree {\n\tif fanout \u003c 4 {\n\t\tpanic(\"bptree: fanout must be \u003e= 4\")\n\t}\n\treturn \u0026BPTree{fanout: fanout}\n}\n\n// NewBPTree32 creates a new empty B+ tree with fanout 32.\nfunc NewBPTree32() *BPTree {\n\treturn NewBPTreeN(32)\n}\n\nfunc (t *BPTree) Size() int {\n\treturn t.size\n}\n\nfunc (t *BPTree) Has(key string) bool {\n\tif t.root == nil {\n\t\treturn false\n\t}\n\tn := t.root\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tidx := inner.findChild(key)\n\t\tn = inner.children[idx]\n\t}\n\tleaf := n.(*leafNode)\n\t_, found := leaf.find(key)\n\treturn found\n}\n\n// Get retrieves the value associated with the given key.\n// It returns the value if the key exists, or nil if it doesn't.\n// This allows for a simpler usage pattern with type assertions:\n//\n//\tif value, ok := tree.Get(\"key\").(MyType); ok {\n//\t    // use value\n//\t}\n//\n// Use Has to distinguish a stored nil value from a missing key.\nfunc (t *BPTree) Get(key string) any {\n\tif t.root == nil {\n\t\treturn nil\n\t}\n\tn := t.root\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tidx := inner.findChild(key)\n\t\tn = inner.children[idx]\n\t}\n\tleaf := n.(*leafNode)\n\tpos, found := leaf.find(key)\n\tif !found {\n\t\treturn nil\n\t}\n\treturn *leaf.values[pos]\n}\n\n// GetByIndex returns the key-value pair at the given 0-based index.\n// Panics if index is out of range.\nfunc (t *BPTree) GetByIndex(index int) (key string, value any) {\n\tif t.root == nil || index \u003c 0 || index \u003e= t.size {\n\t\tpanic(\"GetByIndex asked for invalid index\")\n\t}\n\tn := t.root\n\trem := index\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tfound := false\n\t\tfor i, s := range inner.sizes {\n\t\t\tif rem \u003c s {\n\t\t\t\tn = inner.children[i]\n\t\t\t\tfound = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t\trem -= s\n\t\t}\n\t\tif !found {\n\t\t\tpanic(\"GetByIndex asked for invalid index\")\n\t\t}\n\t}\n\tleaf := n.(*leafNode)\n\treturn leaf.keys[rem], *leaf.values[rem]\n}\n\n//----------------------------------------\n// Set\n\n// pathEntry records a step in the root-to-leaf descent.\ntype pathEntry struct {\n\tinner    *innerNode\n\tchildIdx int\n}\n\n// Set inserts or updates a key-value pair. Returns true if the key already existed.\nfunc (t *BPTree) Set(key string, value any) (updated bool) {\n\tif t.fanout == 0 {\n\t\tt.fanout = 32\n\t}\n\tfanout := t.fanout\n\n\tvp := \u0026value // wrap value in *any for lazy loading\n\n\t// Empty tree: create a single leaf.\n\tif t.root == nil {\n\t\tleaf := newLeafNode(fanout)\n\t\tleaf.keys = append(leaf.keys, key)\n\t\tleaf.values = append(leaf.values, vp)\n\t\tt.root = leaf\n\t\tt.size = 1\n\t\treturn false\n\t}\n\n\t// Descend to the leaf, recording the path.\n\tvar path []pathEntry\n\tn := t.root\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tidx := inner.findChild(key)\n\t\tpath = append(path, pathEntry{inner, idx})\n\t\tn = inner.children[idx]\n\t}\n\tleaf := n.(*leafNode)\n\n\t// Check if key already exists.\n\tpos, found := leaf.find(key)\n\tif found {\n\t\t*leaf.values[pos] = value\n\t\treturn true\n\t}\n\n\t// Insert new key-value.\n\tleaf.insertAt(pos, key, vp)\n\tt.size++\n\n\t// Increment sizes up the path.\n\tfor i := range path {\n\t\tpath[i].inner.sizes[path[i].childIdx]++\n\t}\n\n\t// Split if leaf overflows.\n\tif len(leaf.keys) \u003e fanout {\n\t\tt.splitLeaf(leaf, pos, path)\n\t}\n\n\treturn false\n}\n\n// splitLeaf splits an overflowed leaf node. Uses 90/10 split for append\n// patterns (insertPos == fanout) and 50/50 otherwise.\nfunc (t *BPTree) splitLeaf(leaf *leafNode, insertPos int, path []pathEntry) {\n\tfanout := t.fanout\n\n\t// 90/10 split for append pattern: new key ended up at position fanout\n\t// (greater than all pre-existing keys).\n\tvar mid int\n\tif insertPos == fanout {\n\t\tmid = fanout - 1\n\t} else {\n\t\tmid = (fanout + 1) / 2\n\t}\n\n\t// Create right leaf with entries [mid, fanout+1).\n\tright := newLeafNode(fanout)\n\tright.keys = append(right.keys, leaf.keys[mid:]...)\n\tright.values = append(right.values, leaf.values[mid:]...)\n\n\t// Truncate left leaf to [0, mid).\n\tfor i := mid; i \u003c len(leaf.keys); i++ {\n\t\tleaf.keys[i] = \"\"\n\t\tleaf.values[i] = nil\n\t}\n\tleaf.keys = leaf.keys[:mid]\n\tleaf.values = leaf.values[:mid]\n\n\t// Promote separator to parent.\n\tsep := right.keys[0]\n\tleftSize := len(leaf.keys)\n\trightSize := len(right.keys)\n\n\tif len(path) == 0 {\n\t\t// Root was the leaf; create a new inner root.\n\t\tnewRoot := newInnerNode(fanout)\n\t\tnewRoot.keys = append(newRoot.keys, sep)\n\t\tnewRoot.children = append(newRoot.children, leaf, right)\n\t\tnewRoot.sizes = append(newRoot.sizes, leftSize, rightSize)\n\t\tt.root = newRoot\n\t\treturn\n\t}\n\n\t// Insert into parent.\n\tpe := path[len(path)-1]\n\tparent := pe.inner\n\tchildIdx := pe.childIdx\n\n\tparent.sizes[childIdx] = leftSize\n\tparent.insertChildAt(childIdx+1, sep, right, rightSize)\n\n\tif len(parent.children) \u003e fanout {\n\t\tt.splitInner(parent, path[:len(path)-1])\n\t}\n}\n\n// splitInner splits an overflowed inner node (always 50/50).\nfunc (t *BPTree) splitInner(inner *innerNode, path []pathEntry) {\n\tfanout := t.fanout\n\tmid := (fanout + 1) / 2\n\n\tpromotedKey := inner.keys[mid-1]\n\n\tright := newInnerNode(fanout)\n\tright.keys = append(right.keys, inner.keys[mid:]...)\n\tright.children = append(right.children, inner.children[mid:]...)\n\tright.sizes = append(right.sizes, inner.sizes[mid:]...)\n\n\tfor i := mid - 1; i \u003c len(inner.keys); i++ {\n\t\tinner.keys[i] = \"\"\n\t}\n\tfor i := mid; i \u003c len(inner.children); i++ {\n\t\tinner.children[i] = nil\n\t}\n\tinner.keys = inner.keys[:mid-1]\n\tinner.children = inner.children[:mid]\n\tinner.sizes = inner.sizes[:mid]\n\n\tif len(path) == 0 {\n\t\tnewRoot := newInnerNode(fanout)\n\t\tnewRoot.keys = append(newRoot.keys, promotedKey)\n\t\tnewRoot.children = append(newRoot.children, inner, right)\n\t\tnewRoot.sizes = append(newRoot.sizes, inner.nodeSize(), right.nodeSize())\n\t\tt.root = newRoot\n\t\treturn\n\t}\n\n\tpe := path[len(path)-1]\n\tparent := pe.inner\n\tchildIdx := pe.childIdx\n\n\tparent.sizes[childIdx] = inner.nodeSize()\n\tparent.insertChildAt(childIdx+1, promotedKey, right, right.nodeSize())\n\n\tif len(parent.children) \u003e fanout {\n\t\tt.splitInner(parent, path[:len(path)-1])\n\t}\n}\n\n//----------------------------------------\n// Remove\n\n// Remove deletes a key. Returns the old value and true if the key was found.\nfunc (t *BPTree) Remove(key string) (value any, removed bool) {\n\tif t.root == nil {\n\t\treturn nil, false\n\t}\n\tfanout := t.fanout\n\n\tvar path []pathEntry\n\tn := t.root\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tidx := inner.findChild(key)\n\t\tpath = append(path, pathEntry{inner, idx})\n\t\tn = inner.children[idx]\n\t}\n\tleaf := n.(*leafNode)\n\n\tpos, found := leaf.find(key)\n\tif !found {\n\t\treturn nil, false\n\t}\n\n\tvar vp *any\n\t_, vp = leaf.removeAt(pos)\n\tvalue = *vp\n\tt.size--\n\n\tfor i := range path {\n\t\tpath[i].inner.sizes[path[i].childIdx]--\n\t}\n\n\t// Handle root leaf.\n\tif len(path) == 0 {\n\t\tif len(leaf.keys) == 0 {\n\t\t\tt.root = nil\n\t\t}\n\t\treturn value, true\n\t}\n\n\t// Check underflow.\n\tminKeys := fanout / 2\n\tif len(leaf.keys) \u003e= minKeys {\n\t\tif pos == 0 {\n\t\t\tt.updateSeparator(path)\n\t\t}\n\t\treturn value, true\n\t}\n\n\t// If the minimum key was removed, fix ancestor separators before rebalancing.\n\tif pos == 0 {\n\t\tt.updateSeparator(path)\n\t}\n\n\t// Rebalance.\n\tt.rebalanceLeaf(leaf, path)\n\treturn value, true\n}\n\n// updateSeparator fixes the parent's separator key if the child's min key changed\n// (e.g., after removing the leftmost entry).\nfunc (t *BPTree) updateSeparator(path []pathEntry) {\n\tfor i := len(path) - 1; i \u003e= 0; i-- {\n\t\tpe := path[i]\n\t\tif pe.childIdx \u003e 0 {\n\t\t\tchild := pe.inner.children[pe.childIdx]\n\t\t\tpe.inner.keys[pe.childIdx-1] = child.minKey()\n\t\t\treturn\n\t\t}\n\t}\n}\n\n// rebalanceLeaf handles a leaf that has underflowed (\u003c fanout/2 entries).\n// Tries redistribute from left, then right, then merges with a sibling.\nfunc (t *BPTree) rebalanceLeaf(leaf *leafNode, path []pathEntry) {\n\tfanout := t.fanout\n\tminKeys := fanout / 2\n\tpe := path[len(path)-1]\n\tparent := pe.inner\n\tchildIdx := pe.childIdx\n\n\t// Try redistribute from left sibling.\n\tif childIdx \u003e 0 {\n\t\tleftSib := parent.children[childIdx-1].(*leafNode)\n\t\tif len(leftSib.keys) \u003e minKeys {\n\t\t\tk, v := leftSib.removeAt(len(leftSib.keys) - 1)\n\t\t\tleaf.insertAt(0, k, v)\n\t\t\tparent.keys[childIdx-1] = leaf.keys[0]\n\t\t\tparent.sizes[childIdx-1] = len(leftSib.keys)\n\t\t\tparent.sizes[childIdx] = len(leaf.keys)\n\t\t\treturn\n\t\t}\n\t}\n\n\t// Try redistribute from right sibling.\n\tif childIdx \u003c len(parent.children)-1 {\n\t\trightSib := parent.children[childIdx+1].(*leafNode)\n\t\tif len(rightSib.keys) \u003e minKeys {\n\t\t\tk, v := rightSib.removeAt(0)\n\t\t\tleaf.insertAt(len(leaf.keys), k, v)\n\t\t\tparent.keys[childIdx] = rightSib.keys[0]\n\t\t\tparent.sizes[childIdx] = len(leaf.keys)\n\t\t\tparent.sizes[childIdx+1] = len(rightSib.keys)\n\t\t\treturn\n\t\t}\n\t}\n\n\t// Merge.\n\tif childIdx \u003e 0 {\n\t\tleftSib := parent.children[childIdx-1].(*leafNode)\n\t\tmergeLeaves(leftSib, leaf, parent, childIdx)\n\t} else {\n\t\trightSib := parent.children[childIdx+1].(*leafNode)\n\t\tmergeLeaves(leaf, rightSib, parent, childIdx+1)\n\t}\n\n\tt.rebalanceInner(path[:len(path)-1])\n}\n\n// mergeLeaves merges right leaf into left and removes right from parent.\nfunc mergeLeaves(left, right *leafNode, parent *innerNode, rightIdx int) {\n\tleft.keys = append(left.keys, right.keys...)\n\tleft.values = append(left.values, right.values...)\n\tparent.removeChildAt(rightIdx)\n\tparent.sizes[rightIdx-1] = len(left.keys)\n}\n\n// rebalanceInner handles an inner node that has underflowed after a child merge.\nfunc (t *BPTree) rebalanceInner(path []pathEntry) {\n\tif len(path) == 0 {\n\t\troot := t.root.(*innerNode)\n\t\tif len(root.children) == 1 {\n\t\t\tt.root = root.children[0]\n\t\t}\n\t\treturn\n\t}\n\n\tpe := path[len(path)-1]\n\tparent := pe.inner\n\tchildIdx := pe.childIdx\n\tchild := parent.children[childIdx].(*innerNode)\n\tfanout := t.fanout\n\tminChildren := fanout / 2\n\n\tif len(child.children) \u003e= minChildren {\n\t\tif childIdx \u003e 0 {\n\t\t\tparent.keys[childIdx-1] = child.minKey()\n\t\t}\n\t\treturn\n\t}\n\n\t// Try redistribute from left sibling.\n\tif childIdx \u003e 0 {\n\t\tleftSib := parent.children[childIdx-1].(*innerNode)\n\t\tif len(leftSib.children) \u003e minChildren {\n\t\t\tredistributeInnerLeft(parent, childIdx, leftSib, child)\n\t\t\treturn\n\t\t}\n\t}\n\n\t// Try redistribute from right sibling.\n\tif childIdx \u003c len(parent.children)-1 {\n\t\trightSib := parent.children[childIdx+1].(*innerNode)\n\t\tif len(rightSib.children) \u003e minChildren {\n\t\t\tredistributeInnerRight(parent, childIdx, child, rightSib)\n\t\t\treturn\n\t\t}\n\t}\n\n\t// Merge.\n\tif childIdx \u003e 0 {\n\t\tleftSib := parent.children[childIdx-1].(*innerNode)\n\t\tmergeInner(leftSib, child, parent, childIdx)\n\t} else {\n\t\trightSib := parent.children[childIdx+1].(*innerNode)\n\t\tmergeInner(child, rightSib, parent, childIdx+1)\n\t}\n\n\tgrandPath := path[:len(path)-1]\n\tif len(grandPath) == 0 {\n\t\troot := t.root.(*innerNode)\n\t\tif len(root.children) == 1 {\n\t\t\tt.root = root.children[0]\n\t\t}\n\t\treturn\n\t}\n\n\tgpe := grandPath[len(grandPath)-1]\n\tgparent := gpe.inner\n\tgchildIdx := gpe.childIdx\n\tgchild := gparent.children[gchildIdx].(*innerNode)\n\tminChildren = t.fanout / 2\n\n\tif len(gchild.children) \u003c minChildren {\n\t\tt.rebalanceInner(grandPath)\n\t} else if gchildIdx \u003e 0 {\n\t\tgparent.keys[gchildIdx-1] = gchild.minKey()\n\t}\n}\n\n// redistributeInnerLeft moves the last child from left sibling to the\n// deficient child, pulling down the parent separator and pushing up a new one.\nfunc redistributeInnerLeft(parent *innerNode, childIdx int, leftSib, child *innerNode) {\n\tsep := parent.keys[childIdx-1]\n\tchild.keys = append(child.keys, \"\")\n\tcopy(child.keys[1:], child.keys)\n\tchild.keys[0] = sep\n\n\tlastChild := leftSib.children[len(leftSib.children)-1]\n\tlastSize := leftSib.sizes[len(leftSib.sizes)-1]\n\tchild.children = append(child.children, nil)\n\tcopy(child.children[1:], child.children)\n\tchild.children[0] = lastChild\n\tchild.sizes = append(child.sizes, 0)\n\tcopy(child.sizes[1:], child.sizes)\n\tchild.sizes[0] = lastSize\n\n\tparent.keys[childIdx-1] = leftSib.keys[len(leftSib.keys)-1]\n\n\tleftSib.keys[len(leftSib.keys)-1] = \"\"\n\tleftSib.keys = leftSib.keys[:len(leftSib.keys)-1]\n\tleftSib.children[len(leftSib.children)-1] = nil\n\tleftSib.children = leftSib.children[:len(leftSib.children)-1]\n\tleftSib.sizes = leftSib.sizes[:len(leftSib.sizes)-1]\n\n\tparent.sizes[childIdx-1] = leftSib.nodeSize()\n\tparent.sizes[childIdx] = child.nodeSize()\n}\n\n// redistributeInnerRight moves the first child from right sibling to the\n// deficient child, pulling down the parent separator and pushing up a new one.\nfunc redistributeInnerRight(parent *innerNode, childIdx int, child, rightSib *innerNode) {\n\tsep := parent.keys[childIdx]\n\tchild.keys = append(child.keys, sep)\n\n\tfirstChild := rightSib.children[0]\n\tfirstSize := rightSib.sizes[0]\n\tchild.children = append(child.children, firstChild)\n\tchild.sizes = append(child.sizes, firstSize)\n\n\tparent.keys[childIdx] = rightSib.keys[0]\n\n\tcopy(rightSib.keys, rightSib.keys[1:])\n\trightSib.keys[len(rightSib.keys)-1] = \"\"\n\trightSib.keys = rightSib.keys[:len(rightSib.keys)-1]\n\tcopy(rightSib.children, rightSib.children[1:])\n\trightSib.children[len(rightSib.children)-1] = nil\n\trightSib.children = rightSib.children[:len(rightSib.children)-1]\n\tcopy(rightSib.sizes, rightSib.sizes[1:])\n\trightSib.sizes = rightSib.sizes[:len(rightSib.sizes)-1]\n\n\tparent.sizes[childIdx] = child.nodeSize()\n\tparent.sizes[childIdx+1] = rightSib.nodeSize()\n}\n\n// mergeInner merges right inner node into left, pulling down the parent\n// separator, and removes right from parent.\nfunc mergeInner(left, right *innerNode, parent *innerNode, rightIdx int) {\n\tsep := parent.keys[rightIdx-1]\n\tleft.keys = append(left.keys, sep)\n\tleft.keys = append(left.keys, right.keys...)\n\tleft.children = append(left.children, right.children...)\n\tleft.sizes = append(left.sizes, right.sizes...)\n\tparent.removeChildAt(rightIdx)\n\tparent.sizes[rightIdx-1] = left.nodeSize()\n}\n\n//----------------------------------------\n// Stack-based iteration\n\n// iterStack is a stack of (innerNode, childIdx) for traversal.\n// It is ephemeral — built during iteration, discarded after.\ntype iterStack []pathEntry\n\n// descendLeft descends to the leftmost leaf, pushing inner nodes onto the stack.\nfunc descendLeft(n node, stack *iterStack) *leafNode {\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\t*stack = append(*stack, pathEntry{inner, 0})\n\t\tn = inner.children[0]\n\t}\n\treturn n.(*leafNode)\n}\n\n// descendRight descends to the rightmost leaf, pushing inner nodes onto the stack.\nfunc descendRight(n node, stack *iterStack) *leafNode {\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tlast := len(inner.children) - 1\n\t\t*stack = append(*stack, pathEntry{inner, last})\n\t\tn = inner.children[last]\n\t}\n\treturn n.(*leafNode)\n}\n\n// advanceLeaf moves to the next leaf in ascending order via the stack.\n// Returns nil if there is no next leaf.\nfunc advanceLeaf(stack *iterStack) *leafNode {\n\tfor len(*stack) \u003e 0 {\n\t\ttop := \u0026(*stack)[len(*stack)-1]\n\t\ttop.childIdx++\n\t\tif top.childIdx \u003c len(top.inner.children) {\n\t\t\tn := top.inner.children[top.childIdx]\n\t\t\treturn descendLeft(n, stack)\n\t\t}\n\t\t*stack = (*stack)[:len(*stack)-1]\n\t}\n\treturn nil\n}\n\n// retreatLeaf moves to the previous leaf in descending order via the stack.\n// Returns nil if there is no previous leaf.\nfunc retreatLeaf(stack *iterStack) *leafNode {\n\tfor len(*stack) \u003e 0 {\n\t\ttop := \u0026(*stack)[len(*stack)-1]\n\t\ttop.childIdx--\n\t\tif top.childIdx \u003e= 0 {\n\t\t\tn := top.inner.children[top.childIdx]\n\t\t\treturn descendRight(n, stack)\n\t\t}\n\t\t*stack = (*stack)[:len(*stack)-1]\n\t}\n\treturn nil\n}\n\n//----------------------------------------\n// Iterate / ReverseIterate\n\n// Iterate calls cb for each key-value pair in [start, end) ascending order.\n// Empty start/end means no bound. Returns true if stopped early by cb.\n// The tree must not be modified during iteration (no Set or Remove from the callback).\nfunc (t *BPTree) Iterate(start, end string, cb IterCbFn) bool {\n\tif t.root == nil {\n\t\treturn false\n\t}\n\n\tvar stack iterStack\n\tvar leaf *leafNode\n\tvar pos int\n\n\tif start == \"\" {\n\t\tleaf = descendLeft(t.root, \u0026stack)\n\t\tpos = 0\n\t} else {\n\t\tleaf, pos, stack = t.descendToGE(start)\n\t\tif leaf == nil {\n\t\t\treturn false\n\t\t}\n\t}\n\n\tfor leaf != nil {\n\t\tfor pos \u003c len(leaf.keys) {\n\t\t\tk := leaf.keys[pos]\n\t\t\tif end != \"\" \u0026\u0026 k \u003e= end {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tif cb(k, *leaf.values[pos]) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\tpos++\n\t\t}\n\t\tleaf = advanceLeaf(\u0026stack)\n\t\tpos = 0\n\t}\n\treturn false\n}\n\n// ReverseIterate calls cb for each key-value pair in [start, end] descending order.\n// Empty start/end means no bound. Returns true if stopped early by cb.\n// The tree must not be modified during iteration (no Set or Remove from the callback).\nfunc (t *BPTree) ReverseIterate(start, end string, cb IterCbFn) bool {\n\tif t.root == nil {\n\t\treturn false\n\t}\n\n\tvar stack iterStack\n\tvar leaf *leafNode\n\tvar pos int\n\n\tif end == \"\" {\n\t\tleaf = descendRight(t.root, \u0026stack)\n\t\tpos = len(leaf.keys) - 1\n\t} else {\n\t\tleaf, pos, stack = t.descendToLE(end)\n\t\tif leaf == nil {\n\t\t\treturn false\n\t\t}\n\t}\n\n\tfor leaf != nil {\n\t\tfor pos \u003e= 0 {\n\t\t\tk := leaf.keys[pos]\n\t\t\tif start != \"\" \u0026\u0026 k \u003c start {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tif cb(k, *leaf.values[pos]) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\tpos--\n\t\t}\n\t\tleaf = retreatLeaf(\u0026stack)\n\t\tif leaf != nil {\n\t\t\tpos = len(leaf.keys) - 1\n\t\t}\n\t}\n\treturn false\n}\n\n// IterateByOffset calls cb for count entries starting at the offset-th entry\n// in ascending order. Returns true if stopped early by cb.\n// The tree must not be modified during iteration (no Set or Remove from the callback).\nfunc (t *BPTree) IterateByOffset(offset int, count int, cb IterCbFn) bool {\n\tif t.root == nil || offset \u003e= t.size || count \u003c= 0 {\n\t\treturn false\n\t}\n\tif offset \u003c 0 {\n\t\toffset = 0\n\t}\n\n\tleaf, pos, stack := t.descendToOffset(offset)\n\n\tvisited := 0\n\tfor leaf != nil \u0026\u0026 visited \u003c count {\n\t\tfor pos \u003c len(leaf.keys) \u0026\u0026 visited \u003c count {\n\t\t\tif cb(leaf.keys[pos], *leaf.values[pos]) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\tvisited++\n\t\t\tpos++\n\t\t}\n\t\tleaf = advanceLeaf(\u0026stack)\n\t\tpos = 0\n\t}\n\treturn false\n}\n\n// ReverseIterateByOffset calls cb for count entries starting at the offset-th\n// entry from the end, in descending order. Returns true if stopped early by cb.\n// The tree must not be modified during iteration (no Set or Remove from the callback).\nfunc (t *BPTree) ReverseIterateByOffset(offset int, count int, cb IterCbFn) bool {\n\tif t.root == nil || offset \u003e= t.size || count \u003c= 0 {\n\t\treturn false\n\t}\n\tif offset \u003c 0 {\n\t\toffset = 0\n\t}\n\n\tascIdx := t.size - 1 - offset\n\tleaf, pos, stack := t.descendToOffset(ascIdx)\n\n\tvisited := 0\n\tfor leaf != nil \u0026\u0026 visited \u003c count {\n\t\tfor pos \u003e= 0 \u0026\u0026 visited \u003c count {\n\t\t\tif cb(leaf.keys[pos], *leaf.values[pos]) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\tvisited++\n\t\t\tpos--\n\t\t}\n\t\tleaf = retreatLeaf(\u0026stack)\n\t\tif leaf != nil {\n\t\t\tpos = len(leaf.keys) - 1\n\t\t}\n\t}\n\treturn false\n}\n\n//----------------------------------------\n// Descent helpers for iteration\n\n// descendToGE descends to the first key \u003e= key, returning the leaf, position, and stack.\nfunc (t *BPTree) descendToGE(key string) (*leafNode, int, iterStack) {\n\tvar stack iterStack\n\tn := t.root\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tidx := inner.findChild(key)\n\t\tstack = append(stack, pathEntry{inner, idx})\n\t\tn = inner.children[idx]\n\t}\n\tleaf := n.(*leafNode)\n\tpos, _ := leaf.find(key)\n\tif pos \u003e= len(leaf.keys) {\n\t\tnext := advanceLeaf(\u0026stack)\n\t\tif next == nil {\n\t\t\treturn nil, 0, nil\n\t\t}\n\t\treturn next, 0, stack\n\t}\n\treturn leaf, pos, stack\n}\n\n// descendToLE descends to the last key \u003c= key, returning the leaf, position, and stack.\nfunc (t *BPTree) descendToLE(key string) (*leafNode, int, iterStack) {\n\tvar stack iterStack\n\tn := t.root\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tidx := inner.findChild(key)\n\t\tstack = append(stack, pathEntry{inner, idx})\n\t\tn = inner.children[idx]\n\t}\n\tleaf := n.(*leafNode)\n\tpos, found := leaf.find(key)\n\tif found {\n\t\treturn leaf, pos, stack\n\t}\n\tif pos \u003e 0 {\n\t\treturn leaf, pos - 1, stack\n\t}\n\tprev := retreatLeaf(\u0026stack)\n\tif prev == nil {\n\t\treturn nil, 0, nil\n\t}\n\treturn prev, len(prev.keys) - 1, stack\n}\n\n// descendToOffset descends to the offset-th entry using sizes[],\n// returning the leaf, position within leaf, and stack.\nfunc (t *BPTree) descendToOffset(offset int) (*leafNode, int, iterStack) {\n\tvar stack iterStack\n\tn := t.root\n\trem := offset\n\tfor !n.isLeaf() {\n\t\tinner := n.(*innerNode)\n\t\tfound := false\n\t\tfor i, s := range inner.sizes {\n\t\t\tif rem \u003c s {\n\t\t\t\tstack = append(stack, pathEntry{inner, i})\n\t\t\t\tn = inner.children[i]\n\t\t\t\tfound = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t\trem -= s\n\t\t}\n\t\tif !found {\n\t\t\tpanic(\"descendToOffset: offset out of range\")\n\t\t}\n\t}\n\treturn n.(*leafNode), rem, stack\n}\n"},{"name":"tree_test.gno","body":"package bptree\n\nimport (\n\t\"math/rand\"\n\t\"sort\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n//----------------------------------------\n// Basic operations\n\nfunc TestNewTree(t *testing.T) {\n\ttree := NewBPTree32()\n\tif tree.Size() != 0 {\n\t\tt.Error(\"Expected empty tree size to be 0\")\n\t}\n}\n\nfunc TestZeroValue(t *testing.T) {\n\tvar tree BPTree\n\tif tree.Size() != 0 {\n\t\tt.Error(\"Expected zero-value tree size to be 0\")\n\t}\n\tif tree.Has(\"x\") {\n\t\tt.Error(\"Expected Has to return false on zero-value tree\")\n\t}\n\tif v := tree.Get(\"x\"); v != nil {\n\t\tt.Errorf(\"Expected Get to return nil on zero-value tree, got %v\", v)\n\t}\n\tif _, ok := tree.Remove(\"x\"); ok {\n\t\tt.Error(\"Expected Remove to return false on zero-value tree\")\n\t}\n\n\t// Set should work on zero-value tree.\n\tif updated := tree.Set(\"a\", 1); updated {\n\t\tt.Error(\"Expected Set to return false for new key\")\n\t}\n\tif tree.Size() != 1 {\n\t\tt.Errorf(\"Expected size 1, got %d\", tree.Size())\n\t}\n\tif v := tree.Get(\"a\"); v != 1 {\n\t\tt.Errorf(\"Expected Get(a) = 1, got %v\", v)\n\t}\n}\n\nfunc TestSetAndGet(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\n\tif tree.Size() != 2 {\n\t\tt.Errorf(\"Expected size 2, got %d\", tree.Size())\n\t}\n\n\tif v := tree.Get(\"key1\"); v != \"value1\" {\n\t\tt.Errorf(\"Expected value1, got %v\", v)\n\t}\n\n\tif v := tree.Get(\"missing\"); v != nil {\n\t\tt.Errorf(\"Expected Get to return nil for missing key, got %v\", v)\n\t}\n}\n\nfunc TestSetUpdate(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\tif updated := tree.Set(\"k\", \"v1\"); updated {\n\t\tt.Error(\"Expected false for new key\")\n\t}\n\tif updated := tree.Set(\"k\", \"v2\"); !updated {\n\t\tt.Error(\"Expected true for existing key\")\n\t}\n\tif tree.Size() != 1 {\n\t\tt.Errorf(\"Expected size 1 after update, got %d\", tree.Size())\n\t}\n\tif v := tree.Get(\"k\"); v != \"v2\" {\n\t\tt.Errorf(\"Expected v2, got %v\", v)\n\t}\n}\n\nfunc TestHas(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\tif !tree.Has(\"a\") {\n\t\tt.Error(\"Expected Has(a) = true\")\n\t}\n\tif tree.Has(\"b\") {\n\t\tt.Error(\"Expected Has(b) = false\")\n\t}\n}\n\nfunc TestRemove(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\n\tv, ok := tree.Remove(\"a\")\n\tif !ok || v != 1 {\n\t\tt.Errorf(\"Expected (1, true), got (%v, %v)\", v, ok)\n\t}\n\tif tree.Size() != 1 {\n\t\tt.Errorf(\"Expected size 1, got %d\", tree.Size())\n\t}\n\tif tree.Has(\"a\") {\n\t\tt.Error(\"Expected Has(a) = false after remove\")\n\t}\n\n\tv, ok = tree.Remove(\"missing\")\n\tif ok || v != nil {\n\t\tt.Errorf(\"Expected (nil, false), got (%v, %v)\", v, ok)\n\t}\n}\n\nfunc TestRemoveLastKey(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Remove(\"a\")\n\tif tree.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0, got %d\", tree.Size())\n\t}\n\n\t// Insert after removing everything.\n\ttree.Set(\"b\", 2)\n\tif tree.Size() != 1 {\n\t\tt.Errorf(\"Expected size 1, got %d\", tree.Size())\n\t}\n\tif v := tree.Get(\"b\"); v != 2 {\n\t\tt.Errorf(\"Expected 2, got %v\", v)\n\t}\n}\n\nfunc TestNilValue(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"k\", nil)\n\t// Has distinguishes a nil-valued entry from a missing key (Get returns\n\t// nil for both cases, by design).\n\tif !tree.Has(\"k\") {\n\t\tt.Error(\"Expected Has(k) = true for nil-valued entry\")\n\t}\n\tif v := tree.Get(\"k\"); v != nil {\n\t\tt.Errorf(\"Expected nil value, got %v\", v)\n\t}\n\tv, ok := tree.Remove(\"k\")\n\tif !ok {\n\t\tt.Error(\"Expected remove to succeed\")\n\t}\n\tif v != nil {\n\t\tt.Errorf(\"Expected nil removed value, got %v\", v)\n\t}\n}\n\nfunc TestEmptyStringKey(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"\", \"empty\")\n\ttree.Set(\"a\", \"alpha\")\n\ttree.Set(\"b\", \"beta\")\n\n\tif !tree.Has(\"\") {\n\t\tt.Error(\"Expected Has('') = true for empty string key\")\n\t}\n\tif v := tree.Get(\"\"); v != \"empty\" {\n\t\tt.Errorf(\"Expected empty, got %v\", v)\n\t}\n\tif tree.Size() != 3 {\n\t\tt.Errorf(\"Expected size 3, got %d\", tree.Size())\n\t}\n\n\t// Remove \"\" key.\n\tv, ok := tree.Remove(\"\")\n\tif !ok || v != \"empty\" {\n\t\tt.Errorf(\"Remove('') = (%v, %v), want (empty, true)\", v, ok)\n\t}\n\tif tree.Size() != 2 {\n\t\tt.Errorf(\"Expected size 2, got %d\", tree.Size())\n\t}\n\tif tree.Has(\"\") {\n\t\tt.Error(\"Expected Has('') = false after remove\")\n\t}\n}\n\nfunc TestGetMissingReturnsNil(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\tif v := tree.Get(\"missing\"); v != nil {\n\t\tt.Errorf(\"Expected nil value for missing key, got %v\", v)\n\t}\n}\n\nfunc TestRemoveMissingReturnsNilFalse(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\tv, ok := tree.Remove(\"missing\")\n\tif v != nil {\n\t\tt.Errorf(\"Expected nil value for missing remove, got %v\", v)\n\t}\n\tif ok {\n\t\tt.Error(\"Expected false for missing remove\")\n\t}\n\n\t// Also on empty tree.\n\tvar empty BPTree\n\tv, ok = empty.Remove(\"x\")\n\tif v != nil || ok {\n\t\tt.Errorf(\"Remove on empty tree: got (%v, %v), want (nil, false)\", v, ok)\n\t}\n}\n\nfunc TestFanoutNeverResets(t *testing.T) {\n\tvar tree BPTree\n\ttree.Set(\"a\", 1)\n\ttree.Remove(\"a\")\n\t// Tree is empty again, but fanout should still be 32.\n\ttree.Set(\"b\", 2)\n\tif tree.Size() != 1 {\n\t\tt.Errorf(\"Expected size 1, got %d\", tree.Size())\n\t}\n\tif tree.fanout != 32 {\n\t\tt.Errorf(\"Expected fanout 32 after re-insert, got %d\", tree.fanout)\n\t}\n}\n\nfunc TestSingleEntry(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"x\", 42)\n\n\t// Root should be a leaf for single entry.\n\tif tree.root == nil {\n\t\tt.Fatal(\"root is nil\")\n\t}\n\tif !tree.root.isLeaf() {\n\t\tt.Error(\"root should be a leaf for single entry\")\n\t}\n\tif tree.Size() != 1 {\n\t\tt.Errorf(\"Expected size 1, got %d\", tree.Size())\n\t}\n\tk, v := tree.GetByIndex(0)\n\tif k != \"x\" || v != 42 {\n\t\tt.Errorf(\"GetByIndex(0) = (%v, %v), want (x, 42)\", k, v)\n\t}\n}\n\n//----------------------------------------\n// GetByIndex\n\nfunc TestGetByIndex(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\n\tk, v := tree.GetByIndex(0)\n\tif k != \"a\" || v != 1 {\n\t\tt.Errorf(\"GetByIndex(0) = (%v, %v), want (a, 1)\", k, v)\n\t}\n\tk, v = tree.GetByIndex(1)\n\tif k != \"b\" || v != 2 {\n\t\tt.Errorf(\"GetByIndex(1) = (%v, %v), want (b, 2)\", k, v)\n\t}\n\tk, v = tree.GetByIndex(2)\n\tif k != \"c\" || v != 3 {\n\t\tt.Errorf(\"GetByIndex(2) = (%v, %v), want (c, 3)\", k, v)\n\t}\n}\n\nfunc TestGetByIndexPanics(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\n\tassertPanics(t, \"empty tree\", func() {\n\t\tvar empty BPTree\n\t\tempty.GetByIndex(0)\n\t})\n\tassertPanics(t, \"negative index\", func() {\n\t\ttree.GetByIndex(-1)\n\t})\n\tassertPanics(t, \"index == size\", func() {\n\t\ttree.GetByIndex(1)\n\t})\n}\n\n//----------------------------------------\n// Iterate\n\nfunc TestIterate(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Full iteration.\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\", \"b\", \"c\", \"d\", \"e\"})\n\n\t// Bounded iteration [b, d) → b, c.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"b\", \"d\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"b\", \"c\"})\n\n\t// start == end → empty.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"b\", \"b\", cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// start \u003e end → empty.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"z\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// No lower bound.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"c\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\", \"b\"})\n\n\t// Empty tree.\n\tvar empty BPTree\n\tstopped := empty.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tt.Error(\"should not be called\")\n\t\treturn false\n\t})\n\tif stopped {\n\t\tt.Error(\"Expected false from Iterate on empty tree\")\n\t}\n}\n\nfunc TestIterateEarlyStop(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\n\tcount := 0\n\tstopped := tree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tcount++\n\t\treturn true\n\t})\n\tif !stopped {\n\t\tt.Error(\"Expected true from early-stopped Iterate\")\n\t}\n\tif count != 1 {\n\t\tt.Errorf(\"Expected 1 callback, got %d\", count)\n\t}\n}\n\nfunc TestIterateAllEdgeCases(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Iterate(\"a\", \"a\") → empty [a,a).\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"a\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// Iterate(\"z\", \"a\") → empty (start \u003e end).\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"z\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// Iterate(\"\", \"a\") → nothing \u003c \"a\".\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// Iterate(\"\", \"b\") → just \"a\".\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"b\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\"})\n\n\t// ReverseIterate(\"a\", \"a\") → visits \"a\" (both inclusive).\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"a\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\"})\n\n\t// ReverseIterate(\"z\", \"a\") → empty (bounds don't swap).\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"z\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// ReverseIterate(\"c\", \"\") → keys \u003e= \"c\" descending.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"c\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"e\", \"d\", \"c\"})\n\n\t// Iterate(\"\", \"a\") on tree with key \"\" stored.\n\ttree2 := NewBPTreeN(4)\n\ttree2.Set(\"\", \"empty\")\n\ttree2.Set(\"a\", \"alpha\")\n\ttree2.Set(\"b\", \"beta\")\n\tgot = collectKeys(tree2, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"\"})\n\n\t// Iterate(\"\", \"\") visits all including \"\" key.\n\tgot = collectKeys(tree2, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"\", \"a\", \"b\"})\n}\n\nfunc TestAllIterationEmptyTree(t *testing.T) {\n\tvar tree BPTree\n\tnoop := func(k string, v any) bool {\n\t\tt.Error(\"should not be called\")\n\t\treturn false\n\t}\n\tif tree.Iterate(\"\", \"\", noop) {\n\t\tt.Error(\"Iterate on empty tree should return false\")\n\t}\n\tif tree.ReverseIterate(\"\", \"\", noop) {\n\t\tt.Error(\"ReverseIterate on empty tree should return false\")\n\t}\n\tif tree.IterateByOffset(0, 1, noop) {\n\t\tt.Error(\"IterateByOffset on empty tree should return false\")\n\t}\n\tif tree.ReverseIterateByOffset(0, 1, noop) {\n\t\tt.Error(\"ReverseIterateByOffset on empty tree should return false\")\n\t}\n}\n\nfunc TestReverseIterate(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Full reverse.\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"e\", \"d\", \"c\", \"b\", \"a\"})\n\n\t// Bounded [b, d] inclusive → d, c, b.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"b\", \"d\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"d\", \"c\", \"b\"})\n\n\t// start == end → visits that one key.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"c\", \"c\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"c\"})\n\n\t// start \u003e end → empty.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"z\", \"a\", cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// No upper bound.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"c\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"e\", \"d\", \"c\"})\n\n\t// end \u003e max key → e, d, c, b, a.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"a\", \"z\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"e\", \"d\", \"c\", \"b\", \"a\"})\n}\n\nfunc TestIterateByOffset(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.IterateByOffset(1, 3, cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"b\", \"c\", \"d\"})\n\n\t// offset=0, count=0 → nothing.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.IterateByOffset(0, 0, cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// offset=size → nothing.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.IterateByOffset(5, 1, cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// count exceeds remaining.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.IterateByOffset(3, 100, cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"d\", \"e\"})\n}\n\nfunc TestReverseIterateByOffset(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Full reverse.\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(0, 5, cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"e\", \"d\", \"c\", \"b\", \"a\"})\n\n\t// offset=1, count=2 → [d, c].\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(1, 2, cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"d\", \"c\"})\n\n\t// offset=4, count=1 → [a].\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(4, 1, cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\"})\n\n\t// offset=4, count=10 → [a].\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(4, 10, cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\"})\n\n\t// offset \u003e= size → nothing.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(5, 1, cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// count=0 → nothing.\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(0, 0, cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\t// negative offset\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(-1, 3, cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"e\", \"d\", \"c\"})\n}\n\nfunc TestReverseIterateEarlyStop(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\tcount := 0\n\tstopped := tree.ReverseIterate(\"\", \"\", func(k string, v any) bool {\n\t\tcount++\n\t\treturn true\n\t})\n\tif !stopped {\n\t\tt.Error(\"Expected true from early-stopped ReverseIterate\")\n\t}\n\tif count != 1 {\n\t\tt.Errorf(\"Expected 1 callback, got %d\", count)\n\t}\n}\n\n//----------------------------------------\n// Splits and merges (use small fanout to trigger them)\n\nfunc TestSplitAndMerge(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Insert enough keys to cause multiple splits.\n\tkeys := []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\"}\n\tfor _, k := range keys {\n\t\ttree.Set(k, k)\n\t}\n\tif tree.Size() != len(keys) {\n\t\tt.Errorf(\"Expected size %d, got %d\", len(keys), tree.Size())\n\t}\n\n\t// Verify all keys present and in order.\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, keys)\n\n\t// Verify GetByIndex works for all positions.\n\tfor i, k := range keys {\n\t\tgk, gv := tree.GetByIndex(i)\n\t\tif gk != k || gv != k {\n\t\t\tt.Errorf(\"GetByIndex(%d) = (%v, %v), want (%v, %v)\", i, gk, gv, k, k)\n\t\t}\n\t}\n\n\t// Remove keys one by one and verify.\n\tfor _, k := range keys {\n\t\tv, ok := tree.Remove(k)\n\t\tif !ok || v != k {\n\t\t\tt.Errorf(\"Remove(%v) = (%v, %v), want (%v, true)\", k, v, ok, k)\n\t\t}\n\t}\n\tif tree.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0 after removing all, got %d\", tree.Size())\n\t}\n}\n\nfunc TestRemoveFromMiddle(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"} {\n\t\ttree.Set(k, k)\n\t}\n\n\t// Remove from the middle to trigger redistributions and merges.\n\ttree.Remove(\"d\")\n\ttree.Remove(\"e\")\n\ttree.Remove(\"b\")\n\ttree.Remove(\"g\")\n\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\", \"c\", \"f\", \"h\"})\n\tif tree.Size() != 4 {\n\t\tt.Errorf(\"Expected size 4, got %d\", tree.Size())\n\t}\n}\n\nfunc TestSequentialInsertRemove(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Sequential insert.\n\tn := 100\n\tfor i := 0; i \u003c n; i++ {\n\t\tk := intToKey(i)\n\t\ttree.Set(k, i)\n\t}\n\tif tree.Size() != n {\n\t\tt.Errorf(\"Expected size %d, got %d\", n, tree.Size())\n\t}\n\n\t// Verify sorted order.\n\tprev := \"\"\n\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tif k \u003c= prev \u0026\u0026 prev != \"\" {\n\t\t\tt.Errorf(\"Keys not in order: %q after %q\", k, prev)\n\t\t}\n\t\tprev = k\n\t\treturn false\n\t})\n\n\t// Remove all in reverse order.\n\tfor i := n - 1; i \u003e= 0; i-- {\n\t\tk := intToKey(i)\n\t\t_, ok := tree.Remove(k)\n\t\tif !ok {\n\t\t\tt.Errorf(\"Remove(%v) failed\", k)\n\t\t}\n\t}\n\tif tree.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0, got %d\", tree.Size())\n\t}\n}\n\nfunc TestRandomInsertRemove(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Insert in \"random\" order (shuffled via simple hash).\n\tkeys := make([]string, 50)\n\tfor i := range keys {\n\t\tkeys[i] = intToKey((i*37 + 13) % 50)\n\t}\n\tfor _, k := range keys {\n\t\ttree.Set(k, k)\n\t}\n\tif tree.Size() != 50 {\n\t\tt.Errorf(\"Expected size 50, got %d\", tree.Size())\n\t}\n\n\t// Remove half.\n\tfor i := 0; i \u003c 25; i++ {\n\t\ttree.Remove(keys[i])\n\t}\n\tif tree.Size() != 25 {\n\t\tt.Errorf(\"Expected size 25, got %d\", tree.Size())\n\t}\n\n\t// Verify remaining keys are in sorted order.\n\tprev := \"\"\n\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tif k \u003c= prev \u0026\u0026 prev != \"\" {\n\t\t\tt.Errorf(\"Keys not in order: %q after %q\", k, prev)\n\t\t}\n\t\tprev = k\n\t\treturn false\n\t})\n}\n\nfunc TestDifferentFanouts(t *testing.T) {\n\tfor _, fanout := range []int{4, 5, 8, 16, 32} {\n\t\ttree := NewBPTreeN(fanout)\n\t\tn := 100\n\t\tfor i := 0; i \u003c n; i++ {\n\t\t\ttree.Set(intToKey(i), i)\n\t\t}\n\t\tif tree.Size() != n {\n\t\t\tt.Errorf(\"fanout=%d: expected size %d, got %d\", fanout, n, tree.Size())\n\t\t}\n\n\t\t// Verify order.\n\t\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\t\treturn tr.Iterate(\"\", \"\", cb)\n\t\t})\n\t\tfor i := 1; i \u003c len(got); i++ {\n\t\t\tif got[i] \u003c= got[i-1] {\n\t\t\t\tt.Errorf(\"fanout=%d: keys not in order at %d\", fanout, i)\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\t// Remove all.\n\t\tfor i := 0; i \u003c n; i++ {\n\t\t\ttree.Remove(intToKey(i))\n\t\t}\n\t\tif tree.Size() != 0 {\n\t\t\tt.Errorf(\"fanout=%d: expected size 0 after remove all, got %d\", fanout, tree.Size())\n\t\t}\n\t}\n}\n\nfunc TestNegativeCount(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\ttree.Set(\"a\", 1)\n\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.IterateByOffset(0, -1, cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterateByOffset(0, -1, cb)\n\t})\n\tassertSliceEqual(t, got, nil)\n}\n\nfunc TestRootCollapse(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\t// Insert enough to create inner nodes.\n\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\"} {\n\t\ttree.Set(k, k)\n\t}\n\tif tree.root.isLeaf() {\n\t\tt.Error(\"Expected inner root after 5 inserts with fanout 4\")\n\t}\n\n\t// Remove enough to trigger merges and root collapse.\n\ttree.Remove(\"a\")\n\ttree.Remove(\"b\")\n\ttree.Remove(\"c\")\n\t// With only \"d\" and \"e\" left, root should collapse to a leaf.\n\tif !tree.root.isLeaf() {\n\t\tt.Error(\"Expected leaf root after removing down to 2 entries\")\n\t}\n\tif tree.Size() != 2 {\n\t\tt.Errorf(\"Expected size 2, got %d\", tree.Size())\n\t}\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"d\", \"e\"})\n}\n\nfunc TestSeparatorKeyAfterLeftmostDeletion(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\t// Insert keys to create a split.\n\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"} {\n\t\ttree.Set(k, k)\n\t}\n\t// Remove leftmost key \"a\" — should trigger separator update.\n\ttree.Remove(\"a\")\n\n\t// Verify all remaining keys are accessible.\n\tfor _, k := range []string{\"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"} {\n\t\tif !tree.Has(k) {\n\t\t\tt.Errorf(\"Expected Has(%s) = true after removing 'a'\", k)\n\t\t}\n\t}\n\tif tree.Has(\"a\") {\n\t\tt.Error(\"Expected Has(a) = false\")\n\t}\n\n\t// Verify sorted order.\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"})\n\n\t// Verify GetByIndex still works.\n\tk, _ := tree.GetByIndex(0)\n\tif k != \"b\" {\n\t\tt.Errorf(\"GetByIndex(0) = %v, want b\", k)\n\t}\n}\n\nfunc TestNinetyTenSplit(t *testing.T) {\n\t// Sequential inserts should trigger 90/10 splits, keeping left leaves ~97% full.\n\ttree := NewBPTreeN(4)\n\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\"} {\n\t\ttree.Set(k, k)\n\t}\n\t// After inserting a,b,c,d (leaf full), then e (append → 90/10 split):\n\t// Left should have fanout-1=3 entries [a,b,c], right should have 2 entries [d,e].\n\tif !tree.root.isLeaf() == true {\n\t\t// Root should be an inner node after split.\n\t}\n\tinner := tree.root.(*innerNode)\n\tleft := inner.children[0].(*leafNode)\n\tright := inner.children[1].(*leafNode)\n\tif len(left.keys) != 3 {\n\t\tt.Errorf(\"90/10 split: left leaf has %d entries, want 3\", len(left.keys))\n\t}\n\tif len(right.keys) != 2 {\n\t\tt.Errorf(\"90/10 split: right leaf has %d entries, want 2\", len(right.keys))\n\t}\n\n\t// Verify all keys accessible.\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\", \"b\", \"c\", \"d\", \"e\"})\n}\n\nfunc TestNinetyTenSplitLargeFanout(t *testing.T) {\n\t// With fanout=32, sequential inserts should produce high fill factor.\n\ttree := NewBPTree32()\n\tn := 200\n\tfor i := 0; i \u003c n; i++ {\n\t\ttree.Set(intToKey(i), i)\n\t}\n\tif tree.Size() != n {\n\t\tt.Errorf(\"Expected size %d, got %d\", n, tree.Size())\n\t}\n\n\t// Verify all keys in order.\n\tprev := \"\"\n\tcount := 0\n\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tif k \u003c= prev \u0026\u0026 prev != \"\" {\n\t\t\tt.Errorf(\"Keys not in order: %q after %q\", k, prev)\n\t\t}\n\t\tprev = k\n\t\tcount++\n\t\treturn false\n\t})\n\tif count != n {\n\t\tt.Errorf(\"Iterate visited %d entries, want %d\", count, n)\n\t}\n\n\t// Remove all and verify.\n\tfor i := 0; i \u003c n; i++ {\n\t\t_, ok := tree.Remove(intToKey(i))\n\t\tif !ok {\n\t\t\tt.Errorf(\"Remove(%s) failed\", intToKey(i))\n\t\t}\n\t}\n\tif tree.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0, got %d\", tree.Size())\n\t}\n}\n\nfunc TestMixedSplitTypes(t *testing.T) {\n\t// Sequential inserts trigger 90/10, then a middle insert triggers 50/50.\n\ttree := NewBPTreeN(4)\n\t// Sequential: triggers 90/10 split.\n\tfor _, k := range []string{\"b\", \"c\", \"d\", \"e\"} {\n\t\ttree.Set(k, k)\n\t}\n\t// Insert \"a\" at the beginning of the left leaf — not an append, so if\n\t// that leaf overflows it will use 50/50.\n\ttree.Set(\"a\", \"a\")\n\n\t// Verify all keys present and sorted.\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"a\", \"b\", \"c\", \"d\", \"e\"})\n}\n\nfunc TestSizeCacheConsistency(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\tkeys := []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\"}\n\n\t// Check after each insert.\n\tfor _, k := range keys {\n\t\ttree.Set(k, k)\n\t\tverifySizeCache(t, tree.root)\n\t}\n\n\t// Check after each remove.\n\tfor _, k := range keys {\n\t\ttree.Remove(k)\n\t\tverifySizeCache(t, tree.root)\n\t}\n\n\t// Also check with large fanout.\n\ttree2 := NewBPTree32()\n\tfor i := 0; i \u003c 100; i++ {\n\t\ttree2.Set(intToKey(i), i)\n\t}\n\tverifySizeCache(t, tree2.root)\n\tfor i := 0; i \u003c 100; i++ {\n\t\ttree2.Remove(intToKey(i))\n\t\tverifySizeCache(t, tree2.root)\n\t}\n}\n\nfunc verifySizeCache(t *testing.T, n node) {\n\tt.Helper()\n\tif n == nil || n.isLeaf() {\n\t\treturn\n\t}\n\tinner := n.(*innerNode)\n\t// Verify each sizes[i] matches the actual child subtree size.\n\tfor i, child := range inner.children {\n\t\tactual := child.nodeSize()\n\t\tif inner.sizes[i] != actual {\n\t\t\tt.Errorf(\"innerNode.sizes[%d]=%d but child.nodeSize()=%d\", i, inner.sizes[i], actual)\n\t\t}\n\t\tverifySizeCache(t, child)\n\t}\n}\n\nfunc TestValueIndirection(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Nil value through *any.\n\ttree.Set(\"nil\", nil)\n\tif !tree.Has(\"nil\") {\n\t\tt.Error(\"Expected key 'nil' to exist\")\n\t}\n\tif v := tree.Get(\"nil\"); v != nil {\n\t\tt.Errorf(\"Expected nil value, got %v\", v)\n\t}\n\n\t// Various types.\n\ttree.Set(\"int\", 42)\n\ttree.Set(\"str\", \"hello\")\n\ttree.Set(\"slice\", []byte{1, 2, 3})\n\n\tif v := tree.Get(\"int\"); v != 42 {\n\t\tt.Errorf(\"Expected 42, got %v\", v)\n\t}\n\tif v := tree.Get(\"str\"); v != \"hello\" {\n\t\tt.Errorf(\"Expected hello, got %v\", v)\n\t}\n\tif v := tree.Get(\"slice\"); len(v.([]byte)) != 3 {\n\t\tt.Errorf(\"Expected 3-byte slice, got %v\", v)\n\t}\n\n\t// Update value in place (should reuse the *any pointer).\n\ttree.Set(\"int\", 99)\n\tif v := tree.Get(\"int\"); v != 99 {\n\t\tt.Errorf(\"Expected 99 after update, got %v\", v)\n\t}\n\n\t// Remove nil value.\n\tv, ok := tree.Remove(\"nil\")\n\tif !ok || v != nil {\n\t\tt.Errorf(\"Remove nil: got (%v, %v), want (nil, true)\", v, ok)\n\t}\n\n\t// Remove typed value.\n\tv, ok = tree.Remove(\"int\")\n\tif !ok || v != 99 {\n\t\tt.Errorf(\"Remove int: got (%v, %v), want (99, true)\", v, ok)\n\t}\n\n\t// Large string values — each stored as separate *any object.\n\tbigVal := make([]byte, 10000)\n\tfor i := range bigVal {\n\t\tbigVal[i] = byte(i % 256)\n\t}\n\tfor i := 0; i \u003c 10; i++ {\n\t\ttree.Set(intToKey(i), string(bigVal))\n\t}\n\tif tree.Size() != 12 { // 10 new + \"str\" + \"slice\" remaining\n\t\tt.Errorf(\"Expected size 12, got %d\", tree.Size())\n\t}\n\n\t// Verify large values round-trip correctly.\n\tfor i := 0; i \u003c 10; i++ {\n\t\tv := tree.Get(intToKey(i))\n\t\tif v == nil {\n\t\t\tt.Errorf(\"Missing key %s\", intToKey(i))\n\t\t\tcontinue\n\t\t}\n\t\ts := v.(string)\n\t\tif len(s) != 10000 {\n\t\t\tt.Errorf(\"Key %s: expected 10000-byte string, got %d\", intToKey(i), len(s))\n\t\t}\n\t}\n\n\t// Iteration with mixed values.\n\tcount := 0\n\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tcount++\n\t\treturn false\n\t})\n\tif count != 12 {\n\t\tt.Errorf(\"Iterate counted %d, want 12\", count)\n\t}\n\n\t// Remove all and verify clean.\n\tfor i := 0; i \u003c 10; i++ {\n\t\ttree.Remove(intToKey(i))\n\t}\n\ttree.Remove(\"str\")\n\ttree.Remove(\"slice\")\n\tif tree.Size() != 0 {\n\t\tt.Errorf(\"Expected empty tree, got size %d\", tree.Size())\n\t}\n}\n\nfunc TestFanoutPanics(t *testing.T) {\n\tassertPanics(t, \"fanout 3\", func() {\n\t\tNewBPTreeN(3)\n\t})\n\tassertPanics(t, \"fanout 0\", func() {\n\t\tNewBPTreeN(0)\n\t})\n}\n\n//----------------------------------------\n// Ported from avl/v0 node_test.gno\n\nfunc TestHasTableDriven(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\thasKey   string\n\t\texpected bool\n\t}{\n\t\t{\"has key in non-empty tree\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, \"B\", true},\n\t\t{\"does not have key in non-empty tree\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, \"F\", false},\n\t\t{\"has key in single-node tree\", []string{\"A\"}, \"A\", true},\n\t\t{\"does not have key in single-node tree\", []string{\"A\"}, \"B\", false},\n\t\t{\"does not have key in empty tree\", []string{}, \"A\", false},\n\t}\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ttree := NewBPTreeN(4)\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree.Set(key, nil)\n\t\t\t}\n\t\t\tresult := tree.Has(tt.hasKey)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"Expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetByIndexTableDriven(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tinput       []string\n\t\tidx         int\n\t\texpectKey   string\n\t\texpectPanic bool\n\t}{\n\t\t{\"get by valid index\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, 2, \"C\", false},\n\t\t{\"get by valid index (smallest)\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, 0, \"A\", false},\n\t\t{\"get by valid index (largest)\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, 4, \"E\", false},\n\t\t{\"get by invalid index (negative)\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, -1, \"\", true},\n\t\t{\"get by invalid index (out of range)\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, 5, \"\", true},\n\t}\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ttree := NewBPTreeN(4)\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree.Set(key, nil)\n\t\t\t}\n\t\t\tif tt.expectPanic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r == nil {\n\t\t\t\t\t\tt.Errorf(\"Expected a panic but didn't get one\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\t\t\tkey, _ := tree.GetByIndex(tt.idx)\n\t\t\tif !tt.expectPanic \u0026\u0026 key != tt.expectKey {\n\t\t\t\tt.Errorf(\"Expected key %s, got %s\", tt.expectKey, key)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRemoveTableDriven(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinput     []string\n\t\tremoveKey string\n\t\texpected  []string\n\t}{\n\t\t{\"remove from middle\", []string{\"C\", \"A\", \"B\", \"D\"}, \"B\", []string{\"A\", \"C\", \"D\"}},\n\t\t{\"remove first key\", []string{\"C\", \"A\", \"B\", \"D\"}, \"A\", []string{\"B\", \"C\", \"D\"}},\n\t\t{\"remove last key\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, \"E\", []string{\"A\", \"B\", \"C\", \"D\"}},\n\t\t{\"remove root-equivalent key\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, \"C\", []string{\"A\", \"B\", \"D\", \"E\"}},\n\t\t{\"remove non-existent key\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, \"F\", []string{\"A\", \"B\", \"C\", \"D\", \"E\"}},\n\t}\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ttree := NewBPTreeN(4)\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree.Set(key, nil)\n\t\t\t}\n\t\t\ttree.Remove(tt.removeKey)\n\t\t\tvar result []string\n\t\t\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\t\tresult = append(result, key)\n\t\t\t\treturn false\n\t\t\t})\n\t\t\tif len(result) == 0 {\n\t\t\t\tresult = []string{}\n\t\t\t}\n\t\t\tassertSliceEqual(t, result, tt.expected)\n\t\t})\n\t}\n}\n\nfunc TestTraverse(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\texpected []string\n\t}{\n\t\t{\"empty tree\", []string{}, []string{}},\n\t\t{\"single node tree\", []string{\"A\"}, []string{\"A\"}},\n\t\t{\"small tree\", []string{\"C\", \"A\", \"B\", \"E\", \"D\"}, []string{\"A\", \"B\", \"C\", \"D\", \"E\"}},\n\t\t{\"large tree\", []string{\"H\", \"D\", \"L\", \"B\", \"F\", \"J\", \"N\", \"A\", \"C\", \"E\", \"G\", \"I\", \"K\", \"M\", \"O\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\"}},\n\t}\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ttree := NewBPTreeN(4)\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree.Set(key, nil)\n\t\t\t}\n\n\t\t\tt.Run(\"iterate\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\t\t\tresult = append(result, key)\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\tif len(result) == 0 {\n\t\t\t\t\tresult = []string{}\n\t\t\t\t}\n\t\t\t\tassertSliceEqual(t, result, tt.expected)\n\t\t\t})\n\n\t\t\tt.Run(\"ReverseIterate\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\ttree.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\t\t\t\tresult = append(result, key)\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\texpected := make([]string, len(tt.expected))\n\t\t\t\tcopy(expected, tt.expected)\n\t\t\t\tfor i, j := 0, len(expected)-1; i \u003c j; i, j = i+1, j-1 {\n\t\t\t\t\texpected[i], expected[j] = expected[j], expected[i]\n\t\t\t\t}\n\t\t\t\tif len(result) == 0 {\n\t\t\t\t\tresult = []string{}\n\t\t\t\t}\n\t\t\t\tassertSliceEqual(t, result, expected)\n\t\t\t})\n\n\t\t\tt.Run(\"TraverseInRange\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\tstart, end := \"C\", \"M\"\n\t\t\t\ttree.Iterate(start, end, func(key string, value any) bool {\n\t\t\t\t\tresult = append(result, key)\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\texpected := make([]string, 0)\n\t\t\t\tfor _, key := range tt.expected {\n\t\t\t\t\tif key \u003e= start \u0026\u0026 key \u003c end {\n\t\t\t\t\t\texpected = append(expected, key)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif len(result) == 0 {\n\t\t\t\t\tresult = []string{}\n\t\t\t\t}\n\t\t\t\tassertSliceEqual(t, result, expected)\n\t\t\t})\n\n\t\t\tt.Run(\"early termination\", func(t *testing.T) {\n\t\t\t\tif len(tt.input) == 0 {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tvar result []string\n\t\t\t\tvar count int\n\t\t\t\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\t\t\tcount++\n\t\t\t\t\tresult = append(result, key)\n\t\t\t\t\treturn true\n\t\t\t\t})\n\t\t\t\tif count != 1 {\n\t\t\t\t\tt.Errorf(\"Expected callback to be called exactly once, got %d calls\", count)\n\t\t\t\t}\n\t\t\t\tif len(result) != 1 {\n\t\t\t\t\tt.Errorf(\"Expected exactly one result, got %d items\", len(result))\n\t\t\t\t}\n\t\t\t\tif len(result) \u003e 0 \u0026\u0026 result[0] != tt.expected[0] {\n\t\t\t\t\tt.Errorf(\"Expected first item to be %v, got %v\", tt.expected[0], result[0])\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestTraverseByOffset(t *testing.T) {\n\tsl := []string{\"Alfa\", \"Alfred\", \"Alpha\", \"Alphabet\", \"Beta\", \"Beth\", \"Book\", \"Browser\"}\n\n\t// Insert in reverse order to ensure ordering is independent of insertion order.\n\treversed := make([]string, len(sl))\n\tcopy(reversed, sl)\n\tfor i, j := 0, len(reversed)-1; i \u003c j; i, j = i+1, j-1 {\n\t\treversed[i], reversed[j] = reversed[j], reversed[i]\n\t}\n\n\tt.Run(\"ascending\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, v := range reversed {\n\t\t\ttree.Set(v, nil)\n\t\t}\n\n\t\t// Single-element offset traversal.\n\t\tvar result []string\n\t\tfor i := 0; i \u003c len(sl); i++ {\n\t\t\ttree.IterateByOffset(i, 1, func(key string, value any) bool {\n\t\t\t\tresult = append(result, key)\n\t\t\t\treturn false\n\t\t\t})\n\t\t}\n\t\tassertSliceEqual(t, result, sl)\n\n\t\t// Sliding window.\n\t\tfor l := 2; l \u003c= len(sl); l++ {\n\t\t\tfor i := 0; i \u003c= len(sl); i++ {\n\t\t\t\tmax := i + l\n\t\t\t\tif max \u003e len(sl) {\n\t\t\t\t\tmax = len(sl)\n\t\t\t\t}\n\t\t\t\texp := sl[i:max]\n\t\t\t\tvar actual []string\n\t\t\t\ttree.IterateByOffset(i, l, func(key string, value any) bool {\n\t\t\t\t\tactual = append(actual, key)\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\tif len(actual) == 0 {\n\t\t\t\t\tactual = []string{}\n\t\t\t\t}\n\t\t\t\tassertSliceEqual(t, actual, exp)\n\t\t\t}\n\t\t}\n\t})\n\n\tt.Run(\"descending\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, v := range reversed {\n\t\t\ttree.Set(v, nil)\n\t\t}\n\n\t\t// The descending order.\n\t\tdesc := make([]string, len(sl))\n\t\tcopy(desc, sl)\n\t\tfor i, j := 0, len(desc)-1; i \u003c j; i, j = i+1, j-1 {\n\t\t\tdesc[i], desc[j] = desc[j], desc[i]\n\t\t}\n\n\t\t// Single-element offset traversal in reverse.\n\t\tvar result []string\n\t\tfor i := 0; i \u003c len(desc); i++ {\n\t\t\ttree.ReverseIterateByOffset(i, 1, func(key string, value any) bool {\n\t\t\t\tresult = append(result, key)\n\t\t\t\treturn false\n\t\t\t})\n\t\t}\n\t\tassertSliceEqual(t, result, desc)\n\n\t\t// Sliding window in descending.\n\t\tfor l := 2; l \u003c= len(desc); l++ {\n\t\t\tfor i := 0; i \u003c= len(desc); i++ {\n\t\t\t\tmax := i + l\n\t\t\t\tif max \u003e len(desc) {\n\t\t\t\t\tmax = len(desc)\n\t\t\t\t}\n\t\t\t\texp := desc[i:max]\n\t\t\t\tvar actual []string\n\t\t\t\ttree.ReverseIterateByOffset(i, l, func(key string, value any) bool {\n\t\t\t\t\tactual = append(actual, key)\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\tif len(actual) == 0 {\n\t\t\t\t\tactual = []string{}\n\t\t\t\t}\n\t\t\t\tassertSliceEqual(t, actual, exp)\n\t\t\t}\n\t\t}\n\t})\n}\n\nfunc TestBSTProperty(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\tkeys := []string{\"D\", \"B\", \"F\", \"A\", \"C\", \"E\", \"G\"}\n\tfor _, key := range keys {\n\t\ttree.Set(key, nil)\n\t}\n\n\tvar result []string\n\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tresult = append(result, key)\n\t\treturn false\n\t})\n\n\tfor i := 1; i \u003c len(result); i++ {\n\t\tif result[i] \u003c result[i-1] {\n\t\t\tt.Errorf(\"Sorted property violated: %s \u003c %s (index %d)\",\n\t\t\t\tresult[i], result[i-1], i)\n\t\t}\n\t}\n}\n\nfunc TestRemoveFromEmptyTree(t *testing.T) {\n\tvar tree BPTree\n\tval, removed := tree.Remove(\"NonExistent\")\n\tif val != nil || removed {\n\t\tt.Errorf(\"Expected no value and removed=false when removing from empty tree\")\n\t}\n}\n\n// Ported from avl tree_test.gno\n\nfunc TestPortedTreeSize(t *testing.T) {\n\ttree := NewBPTree32()\n\tif tree.Size() != 0 {\n\t\tt.Error(\"Expected empty tree size to be 0\")\n\t}\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\tif tree.Size() != 2 {\n\t\tt.Error(\"Expected tree size to be 2\")\n\t}\n}\n\nfunc TestPortedTreeHas(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\tif !tree.Has(\"key1\") {\n\t\tt.Error(\"Expected tree to have key1\")\n\t}\n\tif tree.Has(\"key2\") {\n\t\tt.Error(\"Expected tree to not have key2\")\n\t}\n}\n\nfunc TestPortedTreeGet(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\tif value := tree.Get(\"key1\"); value != \"value1\" {\n\t\tt.Error(\"Expected Get to return value1\")\n\t}\n\tif value := tree.Get(\"key2\"); value != nil {\n\t\tt.Error(\"Expected Get to return nil for non-existent key\")\n\t}\n}\n\nfunc TestPortedTreeGetByIndex(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\tkey, value := tree.GetByIndex(0)\n\tif key != \"key1\" || value != \"value1\" {\n\t\tt.Error(\"Expected GetByIndex(0) to return key1 and value1\")\n\t}\n\tkey, value = tree.GetByIndex(1)\n\tif key != \"key2\" || value != \"value2\" {\n\t\tt.Error(\"Expected GetByIndex(1) to return key2 and value2\")\n\t}\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Error(\"Expected GetByIndex to panic for out-of-range index\")\n\t\t}\n\t}()\n\ttree.GetByIndex(2)\n}\n\nfunc TestPortedTreeRemove(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\tvalue, removed := tree.Remove(\"key1\")\n\tif !removed || value != \"value1\" || tree.Size() != 0 {\n\t\tt.Error(\"Expected Remove to remove key-value pair\")\n\t}\n\t_, removed = tree.Remove(\"key2\")\n\tif removed {\n\t\tt.Error(\"Expected Remove to return false for non-existent key\")\n\t}\n}\n\nfunc TestPortedTreeIterate(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\tvar keys []string\n\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\tassertSliceEqual(t, keys, []string{\"key1\", \"key2\", \"key3\"})\n}\n\nfunc TestPortedTreeReverseIterate(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\tvar keys []string\n\ttree.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\tassertSliceEqual(t, keys, []string{\"key3\", \"key2\", \"key1\"})\n}\n\nfunc TestPortedTreeIterateByOffset(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\tvar keys []string\n\ttree.IterateByOffset(1, 2, func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\tassertSliceEqual(t, keys, []string{\"key2\", \"key3\"})\n}\n\nfunc TestPortedTreeReverseIterateByOffset(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\tvar keys []string\n\ttree.ReverseIterateByOffset(1, 2, func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\tassertSliceEqual(t, keys, []string{\"key2\", \"key1\"})\n}\n\nfunc TestPortedReverseIterateByOffsetVaried(t *testing.T) {\n\ttree := NewBPTree32()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\tcases := []struct {\n\t\toffset int\n\t\tlimit  int\n\t\twant   []string\n\t}{\n\t\t{0, 5, []string{\"e\", \"d\", \"c\", \"b\", \"a\"}},\n\t\t{0, 1, []string{\"e\"}},\n\t\t{0, 3, []string{\"e\", \"d\", \"c\"}},\n\t\t{1, 2, []string{\"d\", \"c\"}},\n\t\t{2, 2, []string{\"c\", \"b\"}},\n\t\t{3, 5, []string{\"b\", \"a\"}},\n\t\t{4, 1, []string{\"a\"}},\n\t\t{4, 10, []string{\"a\"}},\n\t\t{5, 1, nil},\n\t\t{0, 0, nil},\n\t\t{10, 1, nil},\n\t}\n\n\tfor _, tc := range cases {\n\t\tvar got []string\n\t\ttree.ReverseIterateByOffset(tc.offset, tc.limit, func(key string, value any) bool {\n\t\t\tgot = append(got, key)\n\t\t\treturn false\n\t\t})\n\t\tif !slicesEqual(got, tc.want) {\n\t\t\tt.Errorf(\"ReverseIterateByOffset(%d, %d): got %v, want %v\",\n\t\t\t\ttc.offset, tc.limit, got, tc.want)\n\t\t}\n\t}\n\n\t// Early termination.\n\tvar got []string\n\ttree.ReverseIterateByOffset(1, 5, func(key string, value any) bool {\n\t\tgot = append(got, key)\n\t\treturn len(got) \u003e= 2\n\t})\n\tassertSliceEqual(t, got, []string{\"d\", \"c\"})\n}\n\nfunc TestPortedBalanceAfterRemoval(t *testing.T) {\n\t// This tests the behavioral equivalence: after various insert/remove\n\t// patterns, the tree maintains correct sorted order and all keys are\n\t// accessible. (AVL tests checked balance factors; we check correctness.)\n\ttests := []struct {\n\t\tname       string\n\t\tinsertKeys []string\n\t\tremoveKey  string\n\t}{\n\t\t{\"remove right node\", []string{\"B\", \"A\", \"D\", \"C\", \"E\"}, \"E\"},\n\t\t{\"remove left node\", []string{\"D\", \"B\", \"E\", \"A\", \"C\"}, \"A\"},\n\t\t{\"remove after complex insert\", []string{\"C\", \"B\", \"E\", \"A\", \"D\", \"F\"}, \"F\"},\n\t\t{\"descending insert, remove middle\", []string{\"E\", \"D\", \"C\", \"B\", \"A\"}, \"C\"},\n\t\t{\"ascending insert, remove middle\", []string{\"A\", \"B\", \"C\", \"D\", \"E\"}, \"C\"},\n\t\t{\"duplicate insert, remove key\", []string{\"C\", \"B\", \"C\", \"A\", \"D\"}, \"C\"},\n\t\t{\"complex case\", []string{\"H\", \"B\", \"A\", \"C\", \"E\", \"D\", \"F\", \"G\"}, \"B\"},\n\t}\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ttree := NewBPTreeN(4)\n\t\t\tfor _, key := range tt.insertKeys {\n\t\t\t\ttree.Set(key, nil)\n\t\t\t}\n\t\t\ttree.Remove(tt.removeKey)\n\n\t\t\t// Verify sorted order.\n\t\t\tvar result []string\n\t\t\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\t\tresult = append(result, key)\n\t\t\t\treturn false\n\t\t\t})\n\t\t\tfor i := 1; i \u003c len(result); i++ {\n\t\t\t\tif result[i] \u003c= result[i-1] {\n\t\t\t\t\tt.Errorf(\"Sorted property violated: %s \u003c= %s\", result[i], result[i-1])\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Verify all expected keys present.\n\t\t\tfor _, key := range tt.insertKeys {\n\t\t\t\tif key == tt.removeKey {\n\t\t\t\t\tif tree.Has(key) {\n\t\t\t\t\t\t// Only check if the key was unique (not duplicated).\n\t\t\t\t\t\t// With duplicates, Set overwrites, so there's only one copy.\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\n//----------------------------------------\n// Structural invariant verification\n\n// verifyInvariants checks all B+ tree structural invariants.\nfunc verifyInvariants(t *testing.T, tree *BPTree) {\n\tt.Helper()\n\tif tree.root == nil {\n\t\tif tree.size != 0 {\n\t\t\tt.Errorf(\"nil root but size=%d\", tree.size)\n\t\t}\n\t\treturn\n\t}\n\n\t// Check leaf depth uniformity.\n\tdepth := leafDepth(tree.root, 0)\n\tif depth == -1 {\n\t\tt.Error(\"leaves are at different depths\")\n\t}\n\n\t// Check separator keys, child count bounds, sizes, ref-count.\n\tseen := make(map[node]bool)\n\tverifyNode(t, tree.root, tree.fanout, true, seen)\n\n\t// Check tree.size matches actual leaf count.\n\tactual := tree.root.nodeSize()\n\tif tree.size != actual {\n\t\tt.Errorf(\"tree.size=%d but root.nodeSize()=%d\", tree.size, actual)\n\t}\n}\n\nfunc leafDepth(n node, depth int) int {\n\tif n.isLeaf() {\n\t\treturn depth\n\t}\n\tinner := n.(*innerNode)\n\td := -1\n\tfor _, child := range inner.children {\n\t\tcd := leafDepth(child, depth+1)\n\t\tif d == -1 {\n\t\t\td = cd\n\t\t} else if cd != d {\n\t\t\treturn -1\n\t\t}\n\t}\n\treturn d\n}\n\nfunc verifyNode(t *testing.T, n node, fanout int, isRoot bool, seen map[node]bool) {\n\tt.Helper()\n\tif seen[n] {\n\t\tt.Errorf(\"node reachable by multiple paths (ref-count \u003e= 2)\")\n\t\treturn\n\t}\n\tseen[n] = true\n\n\tif n.isLeaf() {\n\t\tleaf := n.(*leafNode)\n\t\tif len(leaf.keys) == 0 \u0026\u0026 !isRoot {\n\t\t\tt.Errorf(\"non-root leaf has 0 keys\")\n\t\t}\n\t\tif len(leaf.keys) \u003e fanout {\n\t\t\tt.Errorf(\"leaf has %d keys, max=%d\", len(leaf.keys), fanout)\n\t\t}\n\t\treturn\n\t}\n\n\tinner := n.(*innerNode)\n\n\t// Child count bounds.\n\tminC := fanout / 2\n\tif isRoot {\n\t\tminC = 2\n\t}\n\tif len(inner.children) \u003c minC \u0026\u0026 !isRoot {\n\t\tt.Errorf(\"inner node has %d children, min=%d\", len(inner.children), minC)\n\t}\n\tif len(inner.children) \u003e fanout {\n\t\tt.Errorf(\"inner node has %d children, max=%d\", len(inner.children), fanout)\n\t}\n\n\t// keys/children/sizes length consistency.\n\tif len(inner.keys) != len(inner.children)-1 {\n\t\tt.Errorf(\"len(keys)=%d but len(children)=%d\", len(inner.keys), len(inner.children))\n\t}\n\tif len(inner.sizes) != len(inner.children) {\n\t\tt.Errorf(\"len(sizes)=%d but len(children)=%d\", len(inner.sizes), len(inner.children))\n\t}\n\n\t// Separator key correctness: keys[i] == children[i+1].minKey().\n\tfor i, k := range inner.keys {\n\t\texpected := inner.children[i+1].minKey()\n\t\tif k != expected {\n\t\t\tt.Errorf(\"separator keys[%d]=%q but children[%d].minKey()=%q\", i, k, i+1, expected)\n\t\t}\n\t}\n\n\t// sizes[i] matches child.\n\tfor i, child := range inner.children {\n\t\tactual := child.nodeSize()\n\t\tif inner.sizes[i] != actual {\n\t\t\tt.Errorf(\"sizes[%d]=%d but child.nodeSize()=%d\", i, inner.sizes[i], actual)\n\t\t}\n\t\tverifyNode(t, child, fanout, false, seen)\n\t}\n}\n\nfunc TestInvariantsAfterEveryOperation(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\tkeys := []string{\"m\", \"f\", \"t\", \"b\", \"i\", \"p\", \"w\", \"a\", \"d\", \"g\", \"k\", \"n\", \"r\", \"u\", \"y\"}\n\n\tfor _, k := range keys {\n\t\ttree.Set(k, k)\n\t\tverifyInvariants(t, tree)\n\t}\n\tfor _, k := range keys {\n\t\ttree.Remove(k)\n\t\tverifyInvariants(t, tree)\n\t}\n}\n\n//----------------------------------------\n// Behavioral parity: GetByIndex == IterateByOffset\n\nfunc TestGetByIndexMatchesIterateByOffset(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\tfor _, k := range []string{\"h\", \"d\", \"l\", \"b\", \"f\", \"j\", \"n\", \"a\", \"c\", \"e\", \"g\"} {\n\t\ttree.Set(k, k)\n\t}\n\n\tfor i := 0; i \u003c tree.Size(); i++ {\n\t\tk1, v1 := tree.GetByIndex(i)\n\t\tvar k2 string\n\t\tvar v2 any\n\t\ttree.IterateByOffset(i, 1, func(k string, v any) bool {\n\t\t\tk2 = k\n\t\t\tv2 = v\n\t\t\treturn true\n\t\t})\n\t\tif k1 != k2 || v1 != v2 {\n\t\t\tt.Errorf(\"index %d: GetByIndex=(%q,%v) but IterateByOffset=(%q,%v)\", i, k1, v1, k2, v2)\n\t\t}\n\t}\n}\n\n//----------------------------------------\n// Stress: oscillating tree size\n\nfunc TestOscillatingSize(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Insert 100.\n\tfor i := 0; i \u003c 100; i++ {\n\t\ttree.Set(intToKey(i), i)\n\t}\n\tverifyInvariants(t, tree)\n\n\t// Remove 50.\n\tfor i := 0; i \u003c 50; i++ {\n\t\ttree.Remove(intToKey(i))\n\t}\n\tverifyInvariants(t, tree)\n\tif tree.Size() != 50 {\n\t\tt.Errorf(\"Expected size 50, got %d\", tree.Size())\n\t}\n\n\t// Insert 50 new.\n\tfor i := 100; i \u003c 150; i++ {\n\t\ttree.Set(intToKey(i), i)\n\t}\n\tverifyInvariants(t, tree)\n\tif tree.Size() != 100 {\n\t\tt.Errorf(\"Expected size 100, got %d\", tree.Size())\n\t}\n\n\t// Remove all.\n\tfor i := 50; i \u003c 150; i++ {\n\t\ttree.Remove(intToKey(i))\n\t}\n\tverifyInvariants(t, tree)\n\tif tree.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0, got %d\", tree.Size())\n\t}\n\n\t// Insert again from empty.\n\tfor i := 0; i \u003c 20; i++ {\n\t\ttree.Set(intToKey(i), i)\n\t}\n\tverifyInvariants(t, tree)\n\tif tree.Size() != 20 {\n\t\tt.Errorf(\"Expected size 20, got %d\", tree.Size())\n\t}\n}\n\n//----------------------------------------\n// All 6 rebalance paths\n\nfunc TestAllRebalancePaths(t *testing.T) {\n\t// We use fanout=4 so min=2 for leaves.\n\t// Each sub-test constructs a specific tree state and triggers one rebalance path.\n\n\tt.Run(\"leaf redistribute from left\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\"} {\n\t\t\ttree.Set(k, k)\n\t\t}\n\t\t// After 90/10 split: left=[a,b,c], right=[d,e].\n\t\t// Remove \"d\" → right=[e] (1 \u003c min=2). Left has 3 \u003e 2 → redistribute from left.\n\t\ttree.Remove(\"d\")\n\t\tverifyInvariants(t, tree)\n\t\tif !tree.Has(\"e\") {\n\t\t\tt.Error(\"Missing key 'e' after redistribute\")\n\t\t}\n\t})\n\n\tt.Run(\"leaf redistribute from right\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\"} {\n\t\t\ttree.Set(k, k)\n\t\t}\n\t\t// Remove from leftmost leaf until it underflows and must redistribute from right.\n\t\ttree.Remove(\"a\")\n\t\ttree.Remove(\"b\")\n\t\tverifyInvariants(t, tree)\n\t})\n\n\tt.Run(\"leaf redistribute from right, no stale root separator keys\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\", \"m\", \"n\"} {\n\t\t\ttree.Set(k, k)\n\t\t}\n\t\ttree.Remove(\"g\") // no underflow\n\t\ttree.Remove(\"h\") // leaf [h,i] underflows and must redistribute from right\n\t\tverifyInvariants(t, tree)\n\t})\n\n\tt.Run(\"leaf redistribute from right, no stale inner node separator keys\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\", \"m\", \"n\", \"o\", \"p\", \"q\"} {\n\t\t\ttree.Set(k, k)\n\t\t}\n\t\ttree.Remove(\"g\") // no underflow\n\t\ttree.Remove(\"j\") // no underflow\n\t\ttree.Remove(\"k\") // leaf [k,l] underflows and must redistribute from right\n\t\tverifyInvariants(t, tree)\n\t})\n\n\tt.Run(\"stale separator, middle leaf pos0 redistribute from right\", func(t *testing.T) {\n\t\t// Exercises the core bug: remove pos=0 from a middle leaf (childIdx \u003e 0),\n\t\t// where left sibling is at minimum (can't redistribute left) and right\n\t\t// sibling has surplus (redistribute right fires). Without the fix,\n\t\t// parent.keys[childIdx-1] remains stale.\n\t\t//\n\t\t// After inserting a-k with fanout=4, tree is:\n\t\t//   root keys=[\"d\",\"g\",\"j\"]\n\t\t//   children=[[\"a\",\"b\",\"c\"], [\"d\",\"e\",\"f\"], [\"g\",\"h\",\"i\"], [\"j\",\"k\"]]\n\t\t//\n\t\t// Remove \"a\" thins children[0] to [\"b\",\"c\"] (at minimum).\n\t\t// Remove \"f\" thins children[1] to [\"d\",\"e\"] (at minimum).\n\t\t// Remove \"d\" is pos=0 from children[1], causing underflow to [\"e\"].\n\t\t//   Left sibling [\"b\",\"c\"] has 2 = minKeys, can't spare.\n\t\t//   Right sibling [\"g\",\"h\",\"i\"] has 3 \u003e minKeys, redistribute right fires.\n\t\t//   Without fix: keys[0] stays \"d\" (stale). With fix: updated to \"e\".\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\"} {\n\t\t\ttree.Set(k, k)\n\t\t}\n\t\ttree.Remove(\"a\") // thin left sibling to minimum\n\t\ttree.Remove(\"f\") // thin target leaf to minimum\n\t\ttree.Remove(\"d\") // pos=0, underflow, triggers redistribute from right\n\t\tverifyInvariants(t, tree)\n\t})\n\n\tt.Run(\"leaf merge\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\", \"e\"} {\n\t\t\ttree.Set(k, k)\n\t\t}\n\t\t// Remove enough to make both siblings at minimum, then one more triggers merge.\n\t\ttree.Remove(\"a\")\n\t\ttree.Remove(\"b\") // left=[c], right=[d,e]. left underflows. right has 2=min. merge.\n\t\tverifyInvariants(t, tree)\n\t\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\t\treturn tr.Iterate(\"\", \"\", cb)\n\t\t})\n\t\tassertSliceEqual(t, got, []string{\"c\", \"d\", \"e\"})\n\t})\n\n\tt.Run(\"inner redistribute from left\", func(t *testing.T) {\n\t\t// Build a tree with enough inner nodes, then remove to trigger inner rebalance.\n\t\ttree := NewBPTreeN(4)\n\t\tfor i := 0; i \u003c 30; i++ {\n\t\t\ttree.Set(intToKey(i), i)\n\t\t}\n\t\t// Remove from the right side to underflow a right inner node.\n\t\tfor i := 25; i \u003c 30; i++ {\n\t\t\ttree.Remove(intToKey(i))\n\t\t}\n\t\tverifyInvariants(t, tree)\n\t})\n\n\tt.Run(\"inner redistribute from right\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor i := 0; i \u003c 30; i++ {\n\t\t\ttree.Set(intToKey(i), i)\n\t\t}\n\t\t// Remove from the left side to underflow a left inner node.\n\t\tfor i := 0; i \u003c 5; i++ {\n\t\t\ttree.Remove(intToKey(i))\n\t\t}\n\t\tverifyInvariants(t, tree)\n\t})\n\n\tt.Run(\"inner merge\", func(t *testing.T) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor i := 0; i \u003c 20; i++ {\n\t\t\ttree.Set(intToKey(i), i)\n\t\t}\n\t\t// Remove enough to trigger inner merge.\n\t\tfor i := 0; i \u003c 15; i++ {\n\t\t\ttree.Remove(intToKey(i))\n\t\t}\n\t\tverifyInvariants(t, tree)\n\t\tif tree.Size() != 5 {\n\t\t\tt.Errorf(\"Expected size 5, got %d\", tree.Size())\n\t\t}\n\t})\n}\n\n//----------------------------------------\n// Value mutation after Get should not affect tree\n\nfunc TestValueMutationIndependence(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Store a slice value.\n\torig := []int{1, 2, 3}\n\ttree.Set(\"k\", orig)\n\n\t// Get the value and mutate it.\n\tv := tree.Get(\"k\")\n\tgot := v.([]int)\n\tgot[0] = 999\n\n\t// The tree's stored value should also be affected (since slices are reference types\n\t// and *any holds the same interface). This is the expected Go behavior — not a copy.\n\tv2 := tree.Get(\"k\")\n\tgot2 := v2.([]int)\n\tif got2[0] != 999 {\n\t\tt.Errorf(\"Expected slice mutation to be visible (reference semantics), got %v\", got2)\n\t}\n\n\t// But replacing the value via Set should not affect previous Get results.\n\ttree.Set(\"k\", []int{10, 20, 30})\n\tv3 := tree.Get(\"k\")\n\tgot3 := v3.([]int)\n\tif got3[0] != 10 {\n\t\tt.Errorf(\"Expected new value after Set, got %v\", got3)\n\t}\n\t// The old reference should still have 999.\n\tif got[0] != 999 {\n\t\tt.Errorf(\"Old reference should still be 999, got %v\", got)\n\t}\n}\n\n//----------------------------------------\n// Comprehensive invariant check across fanouts and sizes\n\nfunc TestInvariantsAcrossFanouts(t *testing.T) {\n\tfor _, fanout := range []int{4, 5, 7, 8, 16, 32} {\n\t\ttree := NewBPTreeN(fanout)\n\t\tn := 200\n\n\t\t// Insert all.\n\t\tfor i := 0; i \u003c n; i++ {\n\t\t\ttree.Set(intToKey(i), i)\n\t\t}\n\t\tverifyInvariants(t, tree)\n\n\t\t// Remove every 3rd.\n\t\tfor i := 0; i \u003c n; i += 3 {\n\t\t\ttree.Remove(intToKey(i))\n\t\t}\n\t\tverifyInvariants(t, tree)\n\n\t\t// Remove remaining.\n\t\tfor i := 0; i \u003c n; i++ {\n\t\t\ttree.Remove(intToKey(i))\n\t\t}\n\t\tverifyInvariants(t, tree)\n\t\tif tree.Size() != 0 {\n\t\t\tt.Errorf(\"fanout=%d: expected size 0, got %d\", fanout, tree.Size())\n\t\t}\n\t}\n}\n\n//----------------------------------------\n// Deep stack unwinding\n\nfunc TestDeepStackUnwinding(t *testing.T) {\n\t// With fanout=4 and 500 keys, the tree is 4-5 levels deep.\n\t// Iterating across subtree boundaries forces advanceLeaf/retreatLeaf\n\t// to pop multiple stack levels.\n\ttree := NewBPTreeN(4)\n\tn := 500\n\tfor i := 0; i \u003c n; i++ {\n\t\ttree.Set(intToKey(i), i)\n\t}\n\n\t// Full ascending iteration — every leaf boundary is crossed.\n\tcount := 0\n\tprev := \"\"\n\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tif k \u003c= prev \u0026\u0026 prev != \"\" {\n\t\t\tt.Errorf(\"ascending order broken: %q after %q\", k, prev)\n\t\t}\n\t\tprev = k\n\t\tcount++\n\t\treturn false\n\t})\n\tif count != n {\n\t\tt.Errorf(\"ascending: visited %d, want %d\", count, n)\n\t}\n\n\t// Full descending iteration.\n\tcount = 0\n\tprev = \"\"\n\ttree.ReverseIterate(\"\", \"\", func(k string, v any) bool {\n\t\tif prev != \"\" \u0026\u0026 k \u003e= prev {\n\t\t\tt.Errorf(\"descending order broken: %q after %q\", k, prev)\n\t\t}\n\t\tprev = k\n\t\tcount++\n\t\treturn false\n\t})\n\tif count != n {\n\t\tt.Errorf(\"descending: visited %d, want %d\", count, n)\n\t}\n\n\t// Offset-based iteration crossing deep boundaries.\n\t// Start from the middle, iterate to the end.\n\tcount = 0\n\ttree.IterateByOffset(250, 250, func(k string, v any) bool {\n\t\tcount++\n\t\treturn false\n\t})\n\tif count != 250 {\n\t\tt.Errorf(\"IterateByOffset(250,250): visited %d, want 250\", count)\n\t}\n\n\t// Reverse offset from middle.\n\tcount = 0\n\ttree.ReverseIterateByOffset(250, 250, func(k string, v any) bool {\n\t\tcount++\n\t\treturn false\n\t})\n\tif count != 250 {\n\t\tt.Errorf(\"ReverseIterateByOffset(250,250): visited %d, want 250\", count)\n\t}\n\n\tverifyInvariants(t, tree)\n}\n\n//----------------------------------------\n// Height invariant\n\nfunc treeHeight(n node) int {\n\tif n == nil {\n\t\treturn 0\n\t}\n\tif n.isLeaf() {\n\t\treturn 1\n\t}\n\treturn 1 + treeHeight(n.(*innerNode).children[0])\n}\n\nfunc TestHeightInvariant(t *testing.T) {\n\t// B+ tree with fanout F should have height \u003c= 1 + log_{ceil(F/2)}(n).\n\tfor _, fanout := range []int{4, 8, 32} {\n\t\ttree := NewBPTreeN(fanout)\n\t\tn := 1000\n\t\tfor i := 0; i \u003c n; i++ {\n\t\t\ttree.Set(intToKey(i), i)\n\t\t}\n\n\t\th := treeHeight(tree.root)\n\t\t// Compute max height: log base ceil(fanout/2) of n, plus 1 for root.\n\t\tminFill := fanout / 2\n\t\tif minFill \u003c 2 {\n\t\t\tminFill = 2\n\t\t}\n\t\tmaxH := 1\n\t\tcapacity := 1\n\t\tfor capacity \u003c n {\n\t\t\tcapacity *= minFill\n\t\t\tmaxH++\n\t\t}\n\n\t\tif h \u003e maxH {\n\t\t\tt.Errorf(\"fanout=%d, n=%d: height=%d exceeds max=%d\", fanout, n, h, maxH)\n\t\t}\n\t}\n}\n\n//----------------------------------------\n// Empty string as separator key\n\nfunc TestEmptyStringSeparator(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Insert \"\" first, then other keys. With sequential inserts,\n\t// \"\" will end up in the leftmost leaf and could become a separator.\n\ttree.Set(\"\", \"empty\")\n\ttree.Set(\"a\", \"a\")\n\ttree.Set(\"b\", \"b\")\n\ttree.Set(\"c\", \"c\")\n\ttree.Set(\"d\", \"d\") // triggers split; \"\" should be in left leaf\n\n\tverifyInvariants(t, tree)\n\n\t// Verify all keys accessible.\n\tif v := tree.Get(\"\"); v != \"empty\" {\n\t\tt.Errorf(\"Get('')=%v, want empty\", v)\n\t}\n\tfor _, k := range []string{\"a\", \"b\", \"c\", \"d\"} {\n\t\tif !tree.Has(k) {\n\t\t\tt.Errorf(\"missing key %q\", k)\n\t\t}\n\t}\n\n\t// Iterate should include \"\".\n\tgot := collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"\", \"a\", \"b\", \"c\", \"d\"})\n\n\t// ReverseIterate should include \"\".\n\tgot = collectKeys(tree, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.ReverseIterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"d\", \"c\", \"b\", \"a\", \"\"})\n\n\t// Remove \"\" and verify tree is still valid.\n\tv, ok := tree.Remove(\"\")\n\tif !ok || v != \"empty\" {\n\t\tt.Errorf(\"Remove('')=(%v,%v), want (empty,true)\", v, ok)\n\t}\n\tverifyInvariants(t, tree)\n\tif tree.Has(\"\") {\n\t\tt.Error(\"'' should be gone after remove\")\n\t}\n\n\t// Now insert more keys with \"\" to force it into a separator position.\n\t// Build a tree where \"\" is between two inner node children.\n\ttree2 := NewBPTreeN(4)\n\t// Insert keys that will sort before and after \"\".\n\t// In ASCII, \"\" \u003c everything. So \"\" is always the smallest key.\n\t// To make \"\" a separator, we need it to be the minKey of a right child.\n\t// That happens when the leaf containing \"\" splits and \"\" ends up\n\t// as right.keys[0] (the promoted separator).\n\t// With 50/50 split: left gets lower half, right gets upper half.\n\t// Since \"\" is the smallest, it will always be in the left leaf.\n\t// With 90/10 on append: \"\" would be in the left leaf too.\n\t// So \"\" can become a separator only if it's the minKey of a right child\n\t// after a split where \"\" is in the right half.\n\t// Insert in reverse order so \"\" ends up in the right half of a split:\n\ttree2.Set(\"d\", \"d\")\n\ttree2.Set(\"c\", \"c\")\n\ttree2.Set(\"b\", \"b\")\n\ttree2.Set(\"a\", \"a\")\n\ttree2.Set(\"\", \"empty\") // inserted at position 0 (not append), 50/50 split\n\n\tverifyInvariants(t, tree2)\n\tgot = collectKeys(tree2, func(tr *BPTree, cb IterCbFn) bool {\n\t\treturn tr.Iterate(\"\", \"\", cb)\n\t})\n\tassertSliceEqual(t, got, []string{\"\", \"a\", \"b\", \"c\", \"d\"})\n}\n\n//----------------------------------------\n// Pointer independence after split\n\nfunc TestPointerIndependenceAfterSplit(t *testing.T) {\n\ttree := NewBPTreeN(4)\n\n\t// Insert 5 keys to trigger a split.\n\ttree.Set(\"a\", \"val_a\")\n\ttree.Set(\"b\", \"val_b\")\n\ttree.Set(\"c\", \"val_c\")\n\ttree.Set(\"d\", \"val_d\")\n\ttree.Set(\"e\", \"val_e\") // triggers split\n\n\t// Get values from both halves of the split.\n\tvLeft := tree.Get(\"a\")\n\tvRight := tree.Get(\"d\")\n\n\tif vLeft != \"val_a\" {\n\t\tt.Errorf(\"left value: got %v, want val_a\", vLeft)\n\t}\n\tif vRight != \"val_d\" {\n\t\tt.Errorf(\"right value: got %v, want val_d\", vRight)\n\t}\n\n\t// Update a value in the left half — should not affect right half.\n\ttree.Set(\"a\", \"new_a\")\n\tvLeft2 := tree.Get(\"a\")\n\tvRight2 := tree.Get(\"d\")\n\tif vLeft2 != \"new_a\" {\n\t\tt.Errorf(\"updated left: got %v, want new_a\", vLeft2)\n\t}\n\tif vRight2 != \"val_d\" {\n\t\tt.Errorf(\"right should be unchanged: got %v, want val_d\", vRight2)\n\t}\n\n\t// Update a value in the right half — should not affect left half.\n\ttree.Set(\"d\", \"new_d\")\n\tvLeft3 := tree.Get(\"a\")\n\tvRight3 := tree.Get(\"d\")\n\tif vLeft3 != \"new_a\" {\n\t\tt.Errorf(\"left should be unchanged: got %v, want new_a\", vLeft3)\n\t}\n\tif vRight3 != \"new_d\" {\n\t\tt.Errorf(\"updated right: got %v, want new_d\", vRight3)\n\t}\n\n\t// Verify that the *any pointers in the two leaves are different objects.\n\t// Access internals to check.\n\tinner := tree.root.(*innerNode)\n\tleftLeaf := inner.children[0].(*leafNode)\n\trightLeaf := inner.children[1].(*leafNode)\n\n\t// Each value pointer should be unique.\n\tseen := make(map[*any]bool)\n\tfor _, vp := range leftLeaf.values {\n\t\tif seen[vp] {\n\t\t\tt.Error(\"duplicate *any pointer in left leaf\")\n\t\t}\n\t\tseen[vp] = true\n\t}\n\tfor _, vp := range rightLeaf.values {\n\t\tif seen[vp] {\n\t\t\tt.Error(\"*any pointer shared between left and right leaves after split\")\n\t\t}\n\t\tseen[vp] = true\n\t}\n}\n\n//----------------------------------------\n// Helpers\n\nfunc slicesEqual(a, b []string) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc intToKey(i int) string {\n\t// Zero-padded 4-digit string for correct lexicographic ordering.\n\ts := \"0000\"\n\tn := i\n\tb := []byte(s)\n\tfor j := 3; j \u003e= 0; j-- {\n\t\tb[j] = byte('0' + n%10)\n\t\tn /= 10\n\t}\n\treturn string(b)\n}\n\nfunc collectKeys(tree *BPTree, fn func(*BPTree, IterCbFn) bool) []string {\n\tvar keys []string\n\tfn(tree, func(k string, v any) bool {\n\t\tkeys = append(keys, k)\n\t\treturn false\n\t})\n\treturn keys\n}\n\nfunc assertSliceEqual(t *testing.T, got, want []string) {\n\tt.Helper()\n\tif len(got) != len(want) {\n\t\tt.Errorf(\"got %v, want %v\", got, want)\n\t\treturn\n\t}\n\tfor i := range got {\n\t\tif got[i] != want[i] {\n\t\t\tt.Errorf(\"got %v, want %v\", got, want)\n\t\t\treturn\n\t\t}\n\t}\n}\n\nfunc assertPanics(t *testing.T, name string, fn func()) {\n\tt.Helper()\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Errorf(\"%s: expected panic, got none\", name)\n\t\t}\n\t}()\n\tfn()\n}\n\n//----------------------------------------\n// Stress tests\n\n// TestAVLCrossValidation runs identical random operations on both avl.Tree\n// and BPTree and compares every return value.\nfunc TestAVLCrossValidation(t *testing.T) {\n\trng := rand.New(rand.NewPCG(12345, 0))\n\tat := avl.NewTree()\n\tbt := NewBPTree32()\n\n\tconst nOps = 5000\n\tconst keyRange = 200\n\n\tfor i := 0; i \u003c nOps; i++ {\n\t\tkey := intToKey(rng.IntN(keyRange))\n\t\top := rng.IntN(3)\n\t\tswitch op {\n\t\tcase 0: // Set\n\t\t\taUpd := at.Set(key, i)\n\t\t\tbUpd := bt.Set(key, i)\n\t\t\tif aUpd != bUpd {\n\t\t\t\tt.Fatalf(\"op %d: Set(%q) updated avl=%v bpt=%v\", i, key, aUpd, bUpd)\n\t\t\t}\n\t\tcase 1: // Remove\n\t\t\taVal, aOk := at.Remove(key)\n\t\t\tbVal, bOk := bt.Remove(key)\n\t\t\tif aOk != bOk {\n\t\t\t\tt.Fatalf(\"op %d: Remove(%q) avl=%v bpt=%v\", i, key, aOk, bOk)\n\t\t\t}\n\t\t\tif aOk \u0026\u0026 aVal != bVal {\n\t\t\t\tt.Fatalf(\"op %d: Remove(%q) value avl=%v bpt=%v\", i, key, aVal, bVal)\n\t\t\t}\n\t\tcase 2: // Get\n\t\t\taVal := at.Get(key)\n\t\t\taOk := at.Has(key)\n\t\t\tbVal := bt.Get(key)\n\t\t\tbOk := bt.Has(key)\n\t\t\tif aOk != bOk {\n\t\t\t\tt.Fatalf(\"op %d: exists avl=%v bpt=%v\", i, aOk, bOk)\n\t\t\t}\n\t\t\tif aOk \u0026\u0026 aVal != bVal {\n\t\t\t\tt.Fatalf(\"op %d: Get(%q) value avl=%v bpt=%v\", i, key, aVal, bVal)\n\t\t\t}\n\t\t}\n\t\tif at.Size() != bt.Size() {\n\t\t\tt.Fatalf(\"op %d: size avl=%d bpt=%d\", i, at.Size(), bt.Size())\n\t\t}\n\t}\n\n\t// Compare full iteration.\n\tvar aKeys, bKeys []string\n\tat.Iterate(\"\", \"\", func(k string, v any) bool { aKeys = append(aKeys, k); return false })\n\tbt.Iterate(\"\", \"\", func(k string, v any) bool { bKeys = append(bKeys, k); return false })\n\tassertSliceEqual(t, bKeys, aKeys)\n}\n\n// TestExhaustiveRemovalPermutations inserts N keys then tries all N!\n// permutations of removal order, verifying invariants after each remove.\nfunc TestExhaustiveRemovalPermutations(t *testing.T) {\n\tkeys := []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"}\n\tn := len(keys)\n\n\tperm := make([]int, n)\n\tfor i := range perm {\n\t\tperm[i] = i\n\t}\n\n\tcount := 0\n\tpermute(perm, 0, func(order []int) {\n\t\ttree := NewBPTreeN(4)\n\t\tfor _, k := range keys {\n\t\t\ttree.Set(k, k)\n\t\t}\n\t\tfor _, idx := range order {\n\t\t\ttree.Remove(keys[idx])\n\t\t\tverifyInvariants(t, tree)\n\t\t}\n\t\tif tree.Size() != 0 {\n\t\t\tt.Fatalf(\"perm %d: tree not empty after removing all keys\", count)\n\t\t}\n\t\tcount++\n\t})\n}\n\n// permute generates all permutations of arr[start:] and calls fn for each.\nfunc permute(arr []int, start int, fn func([]int)) {\n\tif start == len(arr) {\n\t\tfn(arr)\n\t\treturn\n\t}\n\tfor i := start; i \u003c len(arr); i++ {\n\t\tarr[start], arr[i] = arr[i], arr[start]\n\t\tpermute(arr, start+1, fn)\n\t\tarr[start], arr[i] = arr[i], arr[start]\n\t}\n}\n\n// TestMultiFanoutStress runs the same operation sequence across different\n// fanouts and verifies all produce identical results.\nfunc TestMultiFanoutStress(t *testing.T) {\n\tfanouts := []int{4, 5, 6, 7, 8, 16, 32}\n\tconst nOps = 2000\n\tconst keyRange = 100\n\n\t// Record expected results from the first fanout.\n\ttype result struct {\n\t\tsetUpdated bool\n\t\tgetVal     any\n\t\tgetOk      bool\n\t\trmVal      any\n\t\trmOk       bool\n\t}\n\n\trng0 := rand.New(rand.NewPCG(99999, 0))\n\ttype op struct {\n\t\tkind int // 0=set, 1=remove, 2=get\n\t\tkey  string\n\t\tval  int\n\t}\n\tops := make([]op, nOps)\n\tfor i := range ops {\n\t\tops[i] = op{\n\t\t\tkind: rng0.IntN(3),\n\t\t\tkey:  intToKey(rng0.IntN(keyRange)),\n\t\t\tval:  i,\n\t\t}\n\t}\n\n\t// Run on each fanout and collect final keys.\n\tvar referenceKeys []string\n\tfor fi, fanout := range fanouts {\n\t\ttree := NewBPTreeN(fanout)\n\t\tfor _, o := range ops {\n\t\t\tswitch o.kind {\n\t\t\tcase 0:\n\t\t\t\ttree.Set(o.key, o.val)\n\t\t\tcase 1:\n\t\t\t\ttree.Remove(o.key)\n\t\t\tcase 2:\n\t\t\t\ttree.Get(o.key)\n\t\t\t}\n\t\t}\n\t\tverifyInvariants(t, tree)\n\n\t\tvar keys []string\n\t\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\t\tkeys = append(keys, k)\n\t\t\treturn false\n\t\t})\n\n\t\tif fi == 0 {\n\t\t\treferenceKeys = keys\n\t\t} else {\n\t\t\tassertSliceEqual(t, keys, referenceKeys)\n\t\t}\n\t}\n}\n\n// TestSequentialInsertReverseRemove inserts keys 0..N-1 sequentially\n// (triggering 90/10 splits) then removes them in reverse order\n// (triggering cascading merges from the right).\nfunc TestSequentialInsertReverseRemove(t *testing.T) {\n\tfor _, fanout := range []int{4, 6, 8, 32} {\n\t\ttree := NewBPTreeN(fanout)\n\t\tn := 500\n\t\tfor i := 0; i \u003c n; i++ {\n\t\t\ttree.Set(intToKey(i), i)\n\t\t}\n\t\tverifyInvariants(t, tree)\n\t\tif tree.Size() != n {\n\t\t\tt.Errorf(\"fanout=%d: expected size %d, got %d\", fanout, n, tree.Size())\n\t\t}\n\n\t\tfor i := n - 1; i \u003e= 0; i-- {\n\t\t\tval, ok := tree.Remove(intToKey(i))\n\t\t\tif !ok {\n\t\t\t\tt.Fatalf(\"fanout=%d: Remove(%s) returned false\", fanout, intToKey(i))\n\t\t\t}\n\t\t\tif val != i {\n\t\t\t\tt.Fatalf(\"fanout=%d: Remove(%s) value=%v want %d\", fanout, intToKey(i), val, i)\n\t\t\t}\n\t\t\tverifyInvariants(t, tree)\n\t\t}\n\t\tif tree.Size() != 0 {\n\t\t\tt.Errorf(\"fanout=%d: expected empty tree, got size %d\", fanout, tree.Size())\n\t\t}\n\t}\n}\n\n// TestRandomOpsWithPeriodicVerification does 10K random Set/Remove calls,\n// verifying invariants and sorted iteration every 100 ops.\nfunc TestRandomOpsWithPeriodicVerification(t *testing.T) {\n\trng := rand.New(rand.NewPCG(54321, 0))\n\ttree := NewBPTreeN(4)\n\n\tconst nOps = 10000\n\tconst keyRange = 300\n\tconst checkEvery = 100\n\n\t// Track expected keys in a sorted slice for comparison.\n\tpresent := make(map[string]bool)\n\n\tfor i := 0; i \u003c nOps; i++ {\n\t\tkey := intToKey(rng.IntN(keyRange))\n\t\tif rng.IntN(3) == 0 { // ~33% removes\n\t\t\t_, ok := tree.Remove(key)\n\t\t\tif ok {\n\t\t\t\tdelete(present, key)\n\t\t\t}\n\t\t} else { // ~67% sets\n\t\t\ttree.Set(key, i)\n\t\t\tpresent[key] = true\n\t\t}\n\n\t\tif (i+1)%checkEvery == 0 {\n\t\t\tverifyInvariants(t, tree)\n\n\t\t\t// Check size.\n\t\t\tif tree.Size() != len(present) {\n\t\t\t\tt.Fatalf(\"op %d: size mismatch tree=%d map=%d\", i, tree.Size(), len(present))\n\t\t\t}\n\n\t\t\t// Check iteration matches sorted keys.\n\t\t\tvar expected []string\n\t\t\tfor k := range present {\n\t\t\t\texpected = append(expected, k)\n\t\t\t}\n\t\t\tsort.Strings(expected)\n\n\t\t\tvar got []string\n\t\t\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\t\t\tgot = append(got, k)\n\t\t\t\treturn false\n\t\t\t})\n\t\t\tassertSliceEqual(t, got, expected)\n\t\t}\n\t}\n}\n\n// TestGetByIndexIterateByOffsetConsistency verifies that GetByIndex(i)\n// matches IterateByOffset(i, 1) for every valid index.\nfunc TestGetByIndexIterateByOffsetConsistency(t *testing.T) {\n\trng := rand.New(rand.NewPCG(77777, 0))\n\ttree := NewBPTreeN(4)\n\n\t// Build a tree with random insertions and removals.\n\tconst nOps = 1000\n\tconst keyRange = 200\n\tfor i := 0; i \u003c nOps; i++ {\n\t\tkey := intToKey(rng.IntN(keyRange))\n\t\tif rng.IntN(4) == 0 {\n\t\t\ttree.Remove(key)\n\t\t} else {\n\t\t\ttree.Set(key, i)\n\t\t}\n\t}\n\n\tn := tree.Size()\n\tfor i := 0; i \u003c n; i++ {\n\t\tgKey, gVal := tree.GetByIndex(i)\n\n\t\tvar iKey string\n\t\tvar iVal any\n\t\ttree.IterateByOffset(i, 1, func(k string, v any) bool {\n\t\t\tiKey = k\n\t\t\tiVal = v\n\t\t\treturn true\n\t\t})\n\n\t\tif gKey != iKey {\n\t\t\tt.Fatalf(\"index %d: GetByIndex key=%q IterateByOffset key=%q\", i, gKey, iKey)\n\t\t}\n\t\tif gVal != iVal {\n\t\t\tt.Fatalf(\"index %d: GetByIndex val=%v IterateByOffset val=%v\", i, gVal, iVal)\n\t\t}\n\t}\n\n\t// Also check ReverseIterateByOffset consistency.\n\tfor i := 0; i \u003c n; i++ {\n\t\tgKey, gVal := tree.GetByIndex(n - 1 - i)\n\n\t\tvar rKey string\n\t\tvar rVal any\n\t\ttree.ReverseIterateByOffset(i, 1, func(k string, v any) bool {\n\t\t\trKey = k\n\t\t\trVal = v\n\t\t\treturn true\n\t\t})\n\n\t\tif gKey != rKey {\n\t\t\tt.Fatalf(\"rev index %d: GetByIndex key=%q ReverseIterateByOffset key=%q\", i, gKey, rKey)\n\t\t}\n\t\tif gVal != rVal {\n\t\t\tt.Fatalf(\"rev index %d: GetByIndex val=%v ReverseIterateByOffset val=%v\", i, gVal, rVal)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"CRgzZ75K7gSJHh8+LcrQGx67IZn0QdJ62s6ktkGu7QOQWQtrRee1tU4BdWqXZ6JrPCdimpYZT9DPBCpotzoZBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"boards","path":"gno.land/p/gnoland/boards","files":[{"name":"board.gno","body":"package boards\n\nimport \"time\"\n\n// Board defines a type for boards.\ntype Board struct {\n\t// ID is the unique identifier of the board.\n\tID ID\n\n\t// Name is the current name of the board.\n\tName string\n\n\t// Aliases contains a list of alternative names for the board.\n\tAliases []string\n\n\t// Readonly indicates that the board is readonly.\n\tReadonly bool\n\n\t// Threads contains board threads.\n\tThreads PostStorage\n\n\t// ThreadsSequence generates sequential ID for new threads.\n\tThreadsSequence IdentifierGenerator\n\n\t// Permissions enables support for permissioned boards.\n\t// This type of boards allows managing members with roles and permissions.\n\t// It also enables the implementation of permissioned execution of board related features.\n\tPermissions Permissions\n\n\t// Creator is the account address that created the board.\n\tCreator address\n\n\t// Meta allows storing board metadata.\n\tMeta any\n\n\t// CreatedAt is the board's creation time.\n\tCreatedAt time.Time\n\n\t// UpdatedAt is the board's update time.\n\tUpdatedAt time.Time\n}\n\n// New creates a new basic non permissioned board.\nfunc New(id ID) *Board {\n\treturn \u0026Board{\n\t\tID:              id,\n\t\tThreads:         NewPostStorage(),\n\t\tThreadsSequence: NewIdentifierGenerator(),\n\t\tCreatedAt:       time.Now(),\n\t}\n}\n\n// SetID sets board ID value.\nfunc (board *Board) SetID(v ID) {\n\tboard.ID = v\n}\n\n// SetName sets name value.\nfunc (board *Board) SetName(v string) {\n\tboard.Name = v\n}\n\n// SetAliases sets board name aliases.\nfunc (board *Board) SetAliases(v []string) {\n\tboard.Aliases = v\n}\n\n// SetReadonly sets readonly value.\nfunc (board *Board) SetReadonly(v bool) {\n\tboard.Readonly = v\n}\n\n// SetThreadStorage sets the storage where board threads are stored.\nfunc (board *Board) SetThreadStorage(v PostStorage) {\n\tboard.Threads = v\n}\n\n// SetThreadsSequence sets the sequential thread ID generator.\nfunc (board *Board) SetThreadsSequence(v IdentifierGenerator) {\n\tboard.ThreadsSequence = v\n}\n\n// SetPermissions sets permissions value.\nfunc (board *Board) SetPermissions(v Permissions) {\n\tboard.Permissions = v\n}\n\n// SetCreator sets the address of the account that created the board.\nfunc (board *Board) SetCreator(v address) {\n\tboard.Creator = v\n}\n\n// SetCreatedAt sets the time when board was created.\nfunc (board *Board) SetCreatedAt(v time.Time) {\n\tboard.CreatedAt = v\n}\n\n// SetUpdatedAt sets the time when a board value was updated.\nfunc (board *Board) SetUpdatedAt(v time.Time) {\n\tboard.UpdatedAt = v\n}\n\n// SetMeta sets board metadata.\nfunc (board *Board) SetMeta(v any) {\n\tboard.Meta = v\n}\n"},{"name":"board_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestNew(t *testing.T) {\n\tboard := boards.New(42)\n\n\turequire.Equal(t, 42, int(board.ID), \"expect board ID to match\")\n\turequire.True(t, board.Threads != nil, \"expect board to support threads\")\n\turequire.True(t, board.ThreadsSequence != nil, \"expect board to initialize a thread ID generator\")\n\turequire.False(t, board.CreatedAt.IsZero(), \"expect board to have a creation date\")\n}\n"},{"name":"flag_storage.gno","body":"package boards\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype (\n\t// Flag defines a type for post flags\n\tFlag struct {\n\t\t// User is the user that flagged the post.\n\t\tUser address\n\n\t\t// Reason is the reason that describes why post is flagged.\n\t\tReason string\n\t}\n\n\t// FlagIterFn defines a function type to iterate post flags.\n\tFlagIterFn func(Flag) bool\n\n\t// FlagStorage defines an interface for storing posts flagging information.\n\tFlagStorage interface {\n\t\t// Exists checks if a flag from a user exists\n\t\tExists(address) bool\n\n\t\t// Add adds a new flag from a user.\n\t\tAdd(Flag) error\n\n\t\t// Remove removes a user flag.\n\t\tRemove(address) (removed bool)\n\n\t\t// Size returns the number of flags in the storage.\n\t\tSize() int\n\n\t\t// Iterate iterates post flags.\n\t\t// To reverse iterate flags use a negative count.\n\t\t// If the callback returns true, the iteration is stopped.\n\t\tIterate(start, count int, fn FlagIterFn) bool\n\t}\n)\n\n// NewFlagStorage creates a new storage for post flags.\n// The new storage uses an AVL tree to store flagging info.\nfunc NewFlagStorage() FlagStorage {\n\treturn \u0026flagStorage{bptree.NewBPTree32()}\n}\n\ntype flagStorage struct {\n\tflags *bptree.BPTree // address -\u003e string(reason)\n}\n\n// Exists checks if a flag from a user exists\nfunc (s flagStorage) Exists(addr address) bool {\n\treturn s.flags.Has(addr.String())\n}\n\n// Add adds a new flag from a user.\n// It fails if a flag from the same user exists.\nfunc (s *flagStorage) Add(f Flag) error {\n\tif !f.User.IsValid() {\n\t\treturn ufmt.Errorf(\"post flagging error, invalid user address: %s\", f.User)\n\t}\n\n\tk := f.User.String()\n\tif s.flags.Has(k) {\n\t\treturn ufmt.Errorf(\"flag from user already exists: %s\", f.User)\n\t}\n\n\ts.flags.Set(k, strings.TrimSpace(f.Reason))\n\treturn nil\n}\n\n// Remove removes a user flag.\nfunc (s *flagStorage) Remove(addr address) bool {\n\t_, removed := s.flags.Remove(addr.String())\n\treturn removed\n}\n\n// Size returns the number of flags in the storage.\nfunc (s flagStorage) Size() int {\n\treturn s.flags.Size()\n}\n\n// Iterate iterates post flags.\n// To reverse iterate flags use a negative count.\n// If the callback returns true, the iteration is stopped.\nfunc (s flagStorage) Iterate(start, count int, fn FlagIterFn) bool {\n\tif count \u003c 0 {\n\t\treturn s.flags.ReverseIterateByOffset(start, -count, func(k string, v any) bool {\n\t\t\treturn fn(Flag{\n\t\t\t\tUser:   address(k),\n\t\t\t\tReason: v.(string),\n\t\t\t})\n\t\t})\n\t}\n\n\treturn s.flags.IterateByOffset(start, count, func(k string, v any) bool {\n\t\treturn fn(Flag{\n\t\t\tUser:   address(k),\n\t\t\tReason: v.(string),\n\t\t})\n\t})\n}\n"},{"name":"flag_storage_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestFlagStorageExists(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func() boards.FlagStorage\n\t\tuser   address\n\t\texists bool\n\t}{\n\t\t{\n\t\t\tname: \"found\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\ts := boards.NewFlagStorage()\n\t\t\t\ts.Add(boards.Flag{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tuser:   \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\texists: true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\treturn boards.NewFlagStorage()\n\t\t\t},\n\t\t\tuser:   \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\texists: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\turequire.Equal(t, tt.exists, s.Exists(tt.user))\n\t\t})\n\t}\n}\n\nfunc TestFlagStorageAdd(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func() boards.FlagStorage\n\t\tflag   boards.Flag\n\t\terrMsg string\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\treturn boards.NewFlagStorage()\n\t\t\t},\n\t\t\tflag: boards.Flag{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t},\n\t\t{\n\t\t\tname: \"flag exists\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\ts := boards.NewFlagStorage()\n\t\t\t\ts.Add(boards.Flag{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tflag:   boards.Flag{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t\terrMsg: \"flag from user already exists: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t\t{\n\t\t\tname: \"invalid user address\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\treturn boards.NewFlagStorage()\n\t\t\t},\n\t\t\tflag:   boards.Flag{User: \"foo\"},\n\t\t\terrMsg: \"post flagging error, invalid user address: foo\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\terr := s.Add(tt.flag)\n\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\turequire.Error(t, err, \"expect an error\")\n\t\t\t\turequire.ErrorContains(t, err, tt.errMsg, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\turequire.True(t, s.Exists(tt.flag.User), \"expect flag to be added\")\n\t\t})\n\t}\n}\n\nfunc TestFlagStorageRemove(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.FlagStorage\n\t\taddress address\n\t\tremoved bool\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\ts := boards.NewFlagStorage()\n\t\t\t\ts.Add(boards.Flag{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\taddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tremoved: true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\treturn boards.NewFlagStorage()\n\t\t\t},\n\t\t\taddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\turequire.Equal(t, tt.removed, s.Remove(tt.address))\n\t\t})\n\t}\n}\n\nfunc TestFlagStorageSize(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func() boards.FlagStorage\n\t\tsize  int\n\t}{\n\t\t{\n\t\t\tname: \"empty\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\treturn boards.NewFlagStorage()\n\t\t\t},\n\t\t\tsize: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"one flag\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\ts := boards.NewFlagStorage()\n\t\t\t\ts.Add(boards.Flag{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple flags\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\ts := boards.NewFlagStorage()\n\t\t\t\ts.Add(boards.Flag{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"})\n\t\t\t\ts.Add(boards.Flag{User: \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 2,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\turequire.Equal(t, tt.size, s.Size())\n\t\t})\n\t}\n}\n\nfunc TestFlagStorageIterate(t *testing.T) {\n\tflags := []boards.Flag{\n\t\t{\n\t\t\tUser:   \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\tReason: \"a\",\n\t\t},\n\t\t{\n\t\t\tUser:   \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tReason: \"b\",\n\t\t},\n\t}\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.FlagStorage\n\t\treverse bool\n\t\tflags   []boards.Flag\n\t}{\n\t\t{\n\t\t\tname: \"default\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\ts := boards.NewFlagStorage()\n\t\t\t\ts.Add(flags[0])\n\t\t\t\ts.Add(flags[1])\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tflags: flags,\n\t\t},\n\t\t{\n\t\t\tname: \"reverse\",\n\t\t\tsetup: func() boards.FlagStorage {\n\t\t\t\ts := boards.NewFlagStorage()\n\t\t\t\ts.Add(flags[0])\n\t\t\t\ts.Add(flags[1])\n\t\t\t\treturn s\n\t\t\t},\n\t\t\treverse: true,\n\t\t\tflags:   []boards.Flag{flags[1], flags[0]},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\t\t\tcount := s.Size()\n\t\t\tif tt.reverse {\n\t\t\t\tcount = -count\n\t\t\t}\n\n\t\t\tvar i int\n\t\t\ts.Iterate(0, count, func(f boards.Flag) bool {\n\t\t\t\turequire.Equal(t, tt.flags[i].User, f.User, \"expect user to match\")\n\t\t\t\turequire.Equal(t, tt.flags[i].Reason, f.Reason, \"expect reason to match\")\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/gnoland/boards\"\ngno = \"0.9\"\n"},{"name":"id.gno","body":"package boards\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\nconst paddedStringLen = 10\n\n// ID defines a type for unique identifiers.\ntype ID uint64\n\n// String returns the ID as a string.\nfunc (id ID) String() string {\n\treturn strconv.FormatUint(uint64(id), 10)\n}\n\n// PaddedString returns the ID as a 10 character string padded with zeroes.\n// This value can be used for indexing by ID.\nfunc (id ID) PaddedString() string {\n\ts := id.String()\n\treturn strings.Repeat(\"0\", paddedStringLen-len(s)) + s\n}\n\n// Key returns the ID as a string which can be used to index by ID.\nfunc (id ID) Key() string {\n\treturn seqid.ID(id).String()\n}\n\n// IdentifierGenerator defines an interface for sequential unique identifier generators.\ntype IdentifierGenerator interface {\n\t// Current returns the last generated ID.\n\tLast() ID\n\n\t// Next generates a new ID or panics if increasing ID overflows.\n\tNext() ID\n}\n\n// NewIdentifierGenerator creates a new sequential unique identifier generator.\nfunc NewIdentifierGenerator() IdentifierGenerator {\n\treturn \u0026idGenerator{}\n}\n\ntype idGenerator struct {\n\tlast seqid.ID\n}\n\n// Current returns the last generated ID.\nfunc (g idGenerator) Last() ID {\n\treturn ID(g.last)\n}\n\n// Next generates a new ID or panics if increasing ID overflows.\nfunc (g *idGenerator) Next() ID {\n\treturn ID(g.last.Next())\n}\n"},{"name":"id_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestID(t *testing.T) {\n\tid := boards.ID(42)\n\n\turequire.Equal(t, \"42\", id.String(), \"expect string to match\")\n\turequire.Equal(t, \"0000000042\", id.PaddedString(), \"expect padded string to match\")\n\turequire.Equal(t, \"000001a\", id.Key(), \"expect key to match\")\n}\n\nfunc TestIdentifierGenerator(t *testing.T) {\n\tg := boards.NewIdentifierGenerator()\n\n\turequire.Equal(t, uint64(0), uint64(g.Last()), \"expect default to be 0\")\n\turequire.Equal(t, uint64(1), uint64(g.Next()), \"expect next to be 1\")\n\turequire.Equal(t, uint64(1), uint64(g.Last()), \"expect last to be 1\")\n\turequire.Equal(t, uint64(2), uint64(g.Next()), \"expect next to be 2\")\n\turequire.Equal(t, uint64(2), uint64(g.Last()), \"expect last to be 2\")\n}\n"},{"name":"permission_set.gno","body":"package boards\n\n// PermissionSet defines a type to store any number of permissions.\ntype PermissionSet []uint64\n\n// NewPermissionSet creates a new PermissionSet containing the given permissions.\nfunc NewPermissionSet(perms ...Permission) PermissionSet {\n\tif len(perms) == 0 {\n\t\treturn nil\n\t}\n\n\t// Find max permission value to calculate slice size.\n\t// This allows any number of permissions to be assigned in any order.\n\tvar max Permission\n\tfor _, p := range perms {\n\t\tif p \u003e max {\n\t\t\tmax = p\n\t\t}\n\t}\n\n\ts := make(PermissionSet, int(max)/64+1)\n\tfor _, p := range perms {\n\t\t// Calculate the index within the set where the permission should be defined.\n\t\t// Each item in the set can contain 64 permissions, for example:\n\t\t// - Item 0: permissions 0 to 63\n\t\t// - Item 1: permissions 64 to 127\n\t\tidx := int(p) / 64\n\n\t\t// Turn on the bit that matches the permission, ranging from bit 0 to 63\n\t\ts[idx] |= 1 \u003c\u003c (uint(p) % 64)\n\t}\n\treturn s\n}\n\n// Has checks if a permission is in the set.\nfunc (s PermissionSet) Has(p Permission) bool {\n\tidx := int(p) / 64\n\tif idx \u003e= len(s) {\n\t\treturn false\n\t}\n\n\t// Check if the bit for the current permission is on\n\treturn s[idx]\u0026(1\u003c\u003c(uint(p)%64)) != 0\n}\n\n// IsEmpty reports whether the set contains no permissions.\nfunc (s PermissionSet) IsEmpty() bool {\n\tfor _, v := range s {\n\t\tif v != 0 {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"},{"name":"permission_set_test.gno","body":"package boards\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNewPermissionSet(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tperms []Permission\n\t\tcheck []Permission\n\t\twant  bool\n\t}{\n\t\t{\n\t\t\tname:  \"empty\",\n\t\t\tcheck: []Permission{0},\n\t\t\twant:  false,\n\t\t},\n\t\t{\n\t\t\tname:  \"single permission\",\n\t\t\tperms: []Permission{0},\n\t\t\tcheck: []Permission{0},\n\t\t\twant:  true,\n\t\t},\n\t\t{\n\t\t\tname:  \"multiple permissions\",\n\t\t\tperms: []Permission{1, 3, 5},\n\t\t\tcheck: []Permission{1, 3, 5},\n\t\t\twant:  true,\n\t\t},\n\t\t{\n\t\t\tname:  \"high permission value\",\n\t\t\tperms: []Permission{100},\n\t\t\tcheck: []Permission{100},\n\t\t\twant:  true,\n\t\t},\n\t\t{\n\t\t\tname:  \"missing permission\",\n\t\t\tperms: []Permission{100},\n\t\t\tcheck: []Permission{0},\n\t\t\twant:  false,\n\t\t},\n\t\t{\n\t\t\tname:  \"multiple missing permissions\",\n\t\t\tperms: []Permission{1, 3, 5},\n\t\t\tcheck: []Permission{0, 2, 4},\n\t\t\twant:  false,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\ts := NewPermissionSet(tc.perms...)\n\n\t\t\tfor _, p := range tc.check {\n\t\t\t\tuassert.Equal(t, tc.want, s.Has(p))\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestPermissionSetHas(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tset   PermissionSet\n\t\tcheck Permission\n\t\twant  bool\n\t}{\n\t\t{\n\t\t\tname:  \"out of range\",\n\t\t\tset:   NewPermissionSet(0),\n\t\t\tcheck: 100,\n\t\t\twant:  false,\n\t\t},\n\t\t{\n\t\t\tname:  \"nil set\",\n\t\t\tcheck: 0,\n\t\t\twant:  false,\n\t\t},\n\t\t{\n\t\t\tname:  \"permission present\",\n\t\t\tset:   NewPermissionSet(5),\n\t\t\tcheck: 5,\n\t\t\twant:  true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tuassert.Equal(t, tc.want, tc.set.Has(tc.check))\n\t\t})\n\t}\n}\n\nfunc TestPermissionSetIsEmpty(t *testing.T) {\n\tcases := []struct {\n\t\tname string\n\t\tset  PermissionSet\n\t\twant bool\n\t}{\n\t\t{\n\t\t\tname: \"nil set\",\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname: \"non-empty set\",\n\t\t\tset:  NewPermissionSet(0),\n\t\t\twant: false,\n\t\t},\n\t\t{\n\t\t\tname: \"empty allocated set\",\n\t\t\tset:  make(PermissionSet, 1),\n\t\t\twant: true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tuassert.Equal(t, tc.want, tc.set.IsEmpty())\n\t\t})\n\t}\n}\n"},{"name":"permissions.gno","body":"package boards\n\nimport \"strconv\"\n\ntype (\n\t// Role defines the type for user roles.\n\tRole string\n\n\t// Args is a list of generic arguments.\n\tArgs []interface{}\n\n\t// User contains user info.\n\tUser struct {\n\t\tAddress address\n\t\tRoles   []Role\n\t}\n\n\t// UsersIterFn defines a function type to iterate users.\n\tUsersIterFn func(User) bool\n\n\t// Permissions define an interface to for permissioned execution.\n\tPermissions interface {\n\t\t// HasRole checks if a user has a specific role assigned.\n\t\tHasRole(address, Role) bool\n\n\t\t// HasPermission checks if a user has a specific permission.\n\t\tHasPermission(address, Permission) bool\n\n\t\t// WithPermission calls a callback when a user has a specific permission.\n\t\t// It panics on error.\n\t\t//\n\t\t// An inline crossing function call can be used by the implementation if\n\t\t// crossing is required to update its internal state, for example to create\n\t\t// proposals that when approved execute the callback:\n\t\t//\n\t\t//  func(realm) {\n\t\t//    // Update internal realm state\n\t\t//    // ...\n\t\t//  }(cross)\n\t\tWithPermission(address, Permission, Args, func())\n\n\t\t// SetUserRoles adds a new user when it doesn't exist and sets its roles.\n\t\t// Method can also be called to change the roles of an existing user.\n\t\t// It panics on error.\n\t\tSetUserRoles(address, ...Role)\n\n\t\t// RemoveUser removes a user from the permissioner.\n\t\t// It panics on error.\n\t\tRemoveUser(address) (removed bool)\n\n\t\t// HasUser checks if a user exists.\n\t\tHasUser(address) bool\n\n\t\t// UsersCount returns the total number of users the permissioner contains.\n\t\tUsersCount() int\n\n\t\t// IterateUsers iterates permissions' users.\n\t\tIterateUsers(start, count int, fn UsersIterFn) bool\n\t}\n)\n\n// Permission defines the type for permissions.\ntype Permission uint16\n\n// String returns the string representation of a permission value.\nfunc (p Permission) String() string {\n\treturn strconv.FormatUint(uint64(p), 10)\n}\n"},{"name":"post.gno","body":"package boards\n\nimport (\n\t\"strings\"\n\t\"time\"\n)\n\n// Post defines a generic type for posts.\n// A post can be either a thread or a reply.\ntype Post struct {\n\t// ID is the unique identifier of the post.\n\tID ID\n\n\t// ParentID is the ID of the parent post.\n\tParentID ID\n\n\t// ThreadID contains the post ID of the thread where current post is created.\n\t// If current post is a thread it contains post's ID.\n\t// It should be used when current post is a thread or reply.\n\tThreadID ID\n\n\t// OriginalBoardID contains the board ID of the original post when current post is a repost.\n\tOriginalBoardID ID\n\n\t// Board contains the board where post is created.\n\tBoard *Board\n\n\t// Title contains the post's title.\n\tTitle string\n\n\t// Body contains content of the post.\n\tBody string\n\n\t// Hidden indicates that the post is hidden.\n\tHidden bool\n\n\t// Readonly indicates that the post is readonly.\n\tReadonly bool\n\n\t// Replies stores post replies.\n\tReplies PostStorage\n\n\t// Reposts stores reposts of the current post.\n\t// It should be used when post is a thread.\n\tReposts RepostStorage\n\n\t// Flags stores users flags for the current post.\n\tFlags FlagStorage\n\n\t// Creator is the account address that created the post.\n\tCreator address\n\n\t// Meta allows storing post metadata.\n\tMeta any\n\n\t// CreatedAt is the post's creation time.\n\tCreatedAt time.Time\n\n\t// UpdatedAt is the post's update time.\n\tUpdatedAt time.Time\n}\n\n// Summary return a summary of the post's body.\n// It returns the body making sure that the length is limited to 80 characters.\nfunc (post Post) Summary() string {\n\treturn SummaryOf(post.Body, 80)\n}\n\n// SetID sets post ID value.\nfunc (post *Post) SetID(v ID) {\n\tpost.ID = v\n}\n\n// SetParentID sets post's parent ID value.\nfunc (post *Post) SetParentID(v ID) {\n\tpost.ParentID = v\n}\n\n// SetThreadID sets thread ID value.\nfunc (post *Post) SetThreadID(v ID) {\n\tpost.ThreadID = v\n}\n\n// SetOriginalBoardID sets the board ID of the original post when current post is a repost.\nfunc (post *Post) SetOriginalBoardID(v ID) {\n\tpost.OriginalBoardID = v\n}\n\n// SetBoard sets the board where post was created.\nfunc (post *Post) SetBoard(v *Board) {\n\tpost.Board = v\n}\n\n// SetTitle sets title value.\nfunc (post *Post) SetTitle(v string) {\n\tpost.Title = v\n}\n\n// SetBody sets post's content.\nfunc (post *Post) SetBody(v string) {\n\tpost.Body = v\n}\n\n// SetHidden sets hidden value.\nfunc (post *Post) SetHidden(v bool) {\n\tpost.Hidden = v\n}\n\n// SetReadonly sets readonly value.\nfunc (post *Post) SetReadonly(v bool) {\n\tpost.Readonly = v\n}\n\n// SetReplyStorage sets the storage where post replies are stored.\nfunc (post *Post) SetReplyStorage(v PostStorage) {\n\tpost.Replies = v\n}\n\n// SetRepostStorage sets the storage where thread reposts are stored.\nfunc (post *Post) SetRepostStorage(v RepostStorage) {\n\tpost.Reposts = v\n}\n\n// SetFlagStorage sets the storage where post flags are stored.\nfunc (post *Post) SetFlagStorage(v FlagStorage) {\n\tpost.Flags = v\n}\n\n// SetCreator sets the address of the account that created the post.\nfunc (post *Post) SetCreator(v address) {\n\tpost.Creator = v\n}\n\n// SetCreatedAt sets the time when post was created.\nfunc (post *Post) SetCreatedAt(v time.Time) {\n\tpost.CreatedAt = v\n}\n\n// SetUpdatedAt sets the time when a post value was updated.\nfunc (post *Post) SetUpdatedAt(v time.Time) {\n\tpost.UpdatedAt = v\n}\n\n// IsThread checks if a post is a thread.\n// When a post is not a thread it's considered a thread's reply/comment.\nfunc IsThread(p *Post) bool {\n\tif p == nil {\n\t\treturn false\n\t}\n\treturn p.ThreadID == p.ID\n}\n\n// IsRepost checks if a thread is a repost.\nfunc IsRepost(thread *Post) bool {\n\tif thread == nil {\n\t\treturn false\n\t}\n\treturn thread.OriginalBoardID != 0\n}\n\n// SummaryOf returns a summary of a text.\nfunc SummaryOf(text string, length int) string {\n\ttext = strings.TrimSpace(text)\n\tif text == \"\" {\n\t\treturn \"\"\n\t}\n\n\tlines := strings.SplitN(text, \"\\n\", 2)\n\tline := lines[0]\n\tif len(line) \u003e length {\n\t\tline = line[:(length-3)] + \"...\"\n\t} else if len(lines) \u003e 1 {\n\t\tline = line + \"...\"\n\t}\n\treturn line\n}\n"},{"name":"post_storage.gno","body":"package boards\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\ntype (\n\t// PostIterFn defines a function type to iterate posts.\n\tPostIterFn func(*Post) bool\n\n\t// PostStorage defines an interface for posts storage.\n\tPostStorage interface {\n\t\t// Get retruns a post that matches an ID.\n\t\tGet(ID) (_ *Post, found bool)\n\n\t\t// Remove removes a post from the storage.\n\t\tRemove(ID) (_ *Post, removed bool)\n\n\t\t// Add adds a post in the storage.\n\t\tAdd(*Post) error\n\n\t\t// Size returns the number of posts in the storage.\n\t\tSize() int\n\n\t\t// Iterate iterates posts.\n\t\t// To reverse iterate posts use a negative count.\n\t\t// If the callback returns true, the iteration is stopped.\n\t\tIterate(start, count int, fn PostIterFn) bool\n\t}\n)\n\n// NewPostStorage creates a new storage for posts.\n// The new storage uses an AVL tree to store posts.\nfunc NewPostStorage() PostStorage {\n\treturn \u0026postStorage{bptree.NewBPTree32()}\n}\n\ntype postStorage struct {\n\tposts *bptree.BPTree // string(Post.ID) -\u003e *Post\n}\n\n// Get retruns a post that matches an ID.\nfunc (s postStorage) Get(id ID) (*Post, bool) {\n\tk := makePostKey(id)\n\tv := s.posts.Get(k)\n\tif v == nil {\n\t\treturn nil, false\n\t}\n\treturn v.(*Post), true\n}\n\n// Remove removes a post from the storage.\nfunc (s *postStorage) Remove(id ID) (*Post, bool) {\n\tk := makePostKey(id)\n\tv, removed := s.posts.Remove(k)\n\tif !removed {\n\t\treturn nil, false\n\t}\n\treturn v.(*Post), true\n}\n\n// Add adds a post in the storage.\n// It updates existing posts when storage contains one with the same ID.\nfunc (s *postStorage) Add(p *Post) error {\n\tif p == nil {\n\t\treturn errors.New(\"saving nil posts is not allowed\")\n\t}\n\n\ts.posts.Set(makePostKey(p.ID), p)\n\treturn nil\n}\n\n// Size returns the number of posts in the storage.\nfunc (s postStorage) Size() int {\n\treturn s.posts.Size()\n}\n\n// Iterate iterates posts.\n// To reverse iterate posts use a negative count.\n// If the callback returns true, the iteration is stopped.\nfunc (s postStorage) Iterate(start, count int, fn PostIterFn) bool {\n\tif count \u003c 0 {\n\t\treturn s.posts.ReverseIterateByOffset(start, -count, func(_ string, v any) bool {\n\t\t\treturn fn(v.(*Post))\n\t\t})\n\t}\n\n\treturn s.posts.IterateByOffset(start, count, func(_ string, v any) bool {\n\t\treturn fn(v.(*Post))\n\t})\n}\n\nfunc makePostKey(postID ID) string {\n\treturn postID.PaddedString()\n}\n"},{"name":"post_storage_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestPostStorageGet(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func() boards.PostStorage\n\t\tpostID boards.ID\n\t\tfound  bool\n\t}{\n\t\t{\n\t\t\tname: \"single post\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\ts := boards.NewPostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{ID: 1})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tpostID: 1,\n\t\t\tfound:  true,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple posts\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\ts := boards.NewPostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 2})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 3})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tpostID: 2,\n\t\t\tfound:  true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\treturn boards.NewPostStorage()\n\t\t\t},\n\t\t\tpostID: 404,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\tpost, found := s.Get(tt.postID)\n\n\t\t\tif !tt.found {\n\t\t\t\turequire.False(t, found, \"expect post not to be found\")\n\t\t\t\turequire.True(t, post == nil, \"expect post to be nil\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.True(t, found, \"expect post to be found\")\n\t\t\turequire.False(t, post == nil, \"expect post not to be nil\")\n\t\t\turequire.Equal(t, tt.postID.String(), post.ID.String(), \"expect post ID to match\")\n\t\t})\n\t}\n}\n\nfunc TestPostStorageRemove(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.PostStorage\n\t\tpostID  boards.ID\n\t\tremoved bool\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\ts := boards.NewPostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 2})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tpostID:  2,\n\t\t\tremoved: true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\treturn boards.NewPostStorage()\n\t\t\t},\n\t\t\tpostID: 404,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\tpost, removed := s.Remove(tt.postID)\n\n\t\t\tif !tt.removed {\n\t\t\t\turequire.False(t, removed, \"expect post not to be removed\")\n\t\t\t\turequire.True(t, post == nil, \"expect post to be nil\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.True(t, removed, \"expect post to be removed\")\n\t\t\turequire.False(t, post == nil, \"expect post not to be nil\")\n\t\t\turequire.Equal(t, tt.postID.String(), post.ID.String(), \"expect post ID to match\")\n\n\t\t\t_, found := s.Get(tt.postID)\n\t\t\turequire.False(t, found, \"expect post not to be found\")\n\t\t})\n\t}\n}\n\nfunc TestPostStorageAdd(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tpost   *boards.Post\n\t\terrMsg string\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tpost: \u0026boards.Post{ID: 1},\n\t\t},\n\t\t{\n\t\t\tname:   \"nil post\",\n\t\t\tpost:   nil,\n\t\t\terrMsg: \"saving nil posts is not allowed\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := boards.NewPostStorage()\n\n\t\t\terr := s.Add(tt.post)\n\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\turequire.Error(t, err, \"expect an error\")\n\t\t\t\turequire.ErrorContains(t, err, tt.errMsg, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\n\t\t\t_, found := s.Get(tt.post.ID)\n\t\t\turequire.True(t, found, \"expect post to be found\")\n\t\t})\n\t}\n}\n\nfunc TestPostStorageSize(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func() boards.PostStorage\n\t\tsize  int\n\t}{\n\t\t{\n\t\t\tname: \"empty\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\treturn boards.NewPostStorage()\n\t\t\t},\n\t\t\tsize: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"one post\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\ts := boards.NewPostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{ID: 1})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple posts\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\ts := boards.NewPostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 2})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 2,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\turequire.Equal(t, tt.size, s.Size())\n\t\t})\n\t}\n}\n\nfunc TestPostStorageIterate(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.PostStorage\n\t\treverse bool\n\t\tids     []boards.ID\n\t}{\n\t\t{\n\t\t\tname: \"default\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\ts := boards.NewPostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 2})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 3})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tids: []boards.ID{1, 2, 3},\n\t\t},\n\t\t{\n\t\t\tname: \"reverse\",\n\t\t\tsetup: func() boards.PostStorage {\n\t\t\t\ts := boards.NewPostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 2})\n\t\t\t\ts.Add(\u0026boards.Post{ID: 3})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\treverse: true,\n\t\t\tids:     []boards.ID{3, 2, 1},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\t\t\tcount := s.Size()\n\t\t\tif tt.reverse {\n\t\t\t\tcount = -count\n\t\t\t}\n\n\t\t\tvar i int\n\t\t\ts.Iterate(0, count, func(p *boards.Post) bool {\n\t\t\t\turequire.True(t, tt.ids[i] == p.ID, \"expect post ID to match\")\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"},{"name":"post_test.gno","body":"package boards_test\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestPostSummary(t *testing.T) {\n\tpost := \u0026boards.Post{ID: 1, Body: strings.Repeat(\"X\", 900)}\n\tsummary := post.Summary()\n\turequire.True(t, strings.HasSuffix(summary, \"...\"), \"expect dotted suffix\")\n\turequire.True(t, len(summary) == 80, \"expect summary length to match\")\n}\n\nfunc TestIsThread(t *testing.T) {\n\tpost := \u0026boards.Post{ID: 1, ThreadID: 1} // IDs match\n\turequire.True(t, boards.IsThread(post), \"expect post to be a thread\")\n\turequire.False(t, boards.IsThread(nil), \"expect nil not to be a thread\")\n\n\tpost = \u0026boards.Post{ID: 2, ThreadID: 1} // IDs doesn't match\n\turequire.False(t, boards.IsThread(post), \"expect post not to be a thread\")\n}\n\nfunc TestIsRepost(t *testing.T) {\n\tpost := \u0026boards.Post{ID: 1, OriginalBoardID: 1} // Original board ID available\n\turequire.True(t, boards.IsRepost(post), \"expect post to be a repost\")\n\turequire.False(t, boards.IsRepost(nil), \"expect nil not to be a repost\")\n\n\tpost = \u0026boards.Post{ID: 1} // Original board ID not available\n\turequire.False(t, boards.IsRepost(post), \"expect post not to be a repost\")\n}\n\nfunc TestSummaryOf(t *testing.T) {\n\tsummary := boards.SummaryOf(strings.Repeat(\"X\", 90), 80)\n\turequire.True(t, strings.HasSuffix(summary, \"...\"), \"expect dotted suffix\")\n\turequire.True(t, len(summary) == 80, \"expect summary length to match\")\n\n\tsummary = boards.SummaryOf(strings.Repeat(\" \", 90), 80)\n\turequire.Empty(t, summary, \"expect summary to be empty\")\n}\n"},{"name":"reply.gno","body":"package boards\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// NewReply creates a new reply to a thread or another reply.\nfunc NewReply(parent *Post, creator address, body string) (*Post, error) {\n\tif parent == nil {\n\t\treturn nil, errors.New(\"reply requires a parent thread or reply\")\n\t}\n\n\tif parent.ThreadID == 0 {\n\t\treturn nil, errors.New(\"parent has no thread ID assigned\")\n\t}\n\n\tif parent.Board == nil {\n\t\treturn nil, errors.New(\"parent has no board assigned\")\n\t}\n\n\tif !creator.IsValid() {\n\t\treturn nil, ufmt.Errorf(\"invalid reply creator address: %s\", creator)\n\t}\n\n\tbody = strings.TrimSpace(body)\n\tif body == \"\" {\n\t\treturn nil, errors.New(\"reply body is required\")\n\t}\n\n\tid := parent.Board.ThreadsSequence.Next()\n\treturn \u0026Post{\n\t\tID:        id,\n\t\tParentID:  parent.ID,\n\t\tThreadID:  parent.ThreadID,\n\t\tBoard:     parent.Board,\n\t\tBody:      body,\n\t\tReplies:   NewPostStorage(),\n\t\tFlags:     NewFlagStorage(),\n\t\tCreator:   creator,\n\t\tCreatedAt: time.Now(),\n\t}, nil\n}\n\n// MustNewReply creates a new reply or panics on error.\nfunc MustNewReply(parent *Post, creator address, body string) *Post {\n\tp, err := NewReply(parent, creator, body)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn p\n}\n"},{"name":"reply_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestNewReply(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tparent  func() *boards.Post\n\t\tcreator address\n\t\tbody    string\n\t\terrMsg  string\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tparent: func() *boards.Post {\n\t\t\t\tboard := boards.New(1)\n\t\t\t\tid := board.ThreadsSequence.Next()\n\t\t\t\treturn \u0026boards.Post{\n\t\t\t\t\tID:       id,\n\t\t\t\t\tThreadID: id,\n\t\t\t\t\tBoard:    board,\n\t\t\t\t}\n\t\t\t},\n\t\t\tcreator: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tbody:    \"Foo\",\n\t\t},\n\t\t{\n\t\t\tname:   \"nil parent\",\n\t\t\tparent: func() *boards.Post { return nil },\n\t\t\terrMsg: \"reply requires a parent thread or reply\",\n\t\t},\n\t\t{\n\t\t\tname: \"parent without thread ID\",\n\t\t\tparent: func() *boards.Post {\n\t\t\t\treturn \u0026boards.Post{ID: 1}\n\t\t\t},\n\t\t\terrMsg: \"parent has no thread ID assigned\",\n\t\t},\n\t\t{\n\t\t\tname: \"parent without board\",\n\t\t\tparent: func() *boards.Post {\n\t\t\t\treturn \u0026boards.Post{ID: 1, ThreadID: 1}\n\t\t\t},\n\t\t\terrMsg: \"parent has no board assigned\",\n\t\t},\n\t\t{\n\t\t\tname: \"invalid creator\",\n\t\t\tparent: func() *boards.Post {\n\t\t\t\treturn \u0026boards.Post{ID: 1, ThreadID: 1, Board: boards.New(1)}\n\t\t\t},\n\t\t\tcreator: \"foo\",\n\t\t\terrMsg:  \"invalid reply creator address: foo\",\n\t\t},\n\t\t{\n\t\t\tname: \"empty body\",\n\t\t\tparent: func() *boards.Post {\n\t\t\t\treturn \u0026boards.Post{ID: 1, ThreadID: 1, Board: boards.New(1)}\n\t\t\t},\n\t\t\tcreator: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tbody:    \"\",\n\t\t\terrMsg:  \"reply body is required\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tparent := tt.parent()\n\n\t\t\treply, err := boards.NewReply(parent, tt.creator, tt.body)\n\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\turequire.Error(t, err, \"expect an error\")\n\t\t\t\turequire.ErrorContains(t, err, tt.errMsg, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\turequire.True(t, parent.Board.ThreadsSequence.Last() == reply.ID, \"expect ID to match\")\n\t\t\turequire.True(t, parent.ID == reply.ParentID, \"expect parent ID to match\")\n\t\t\turequire.True(t, parent.ThreadID == reply.ThreadID, \"expect thread ID to match\")\n\t\t\turequire.False(t, reply.Board == nil, \"expect board to be assigned\")\n\t\t\turequire.True(t, parent.Board.ID == reply.Board.ID, \"expect board ID to match\")\n\t\t\turequire.Equal(t, tt.body, reply.Body, \"expect body to match\")\n\t\t\turequire.True(t, reply.Replies != nil, \"expect reply to support sub-replies\")\n\t\t\turequire.True(t, reply.Flags != nil, \"expect reply to support flagging\")\n\t\t\turequire.Equal(t, tt.creator, reply.Creator, \"expect creator to match\")\n\t\t\turequire.False(t, reply.CreatedAt.IsZero(), \"expect creation date to be assigned\")\n\t\t})\n\t}\n}\n"},{"name":"repost_storage.gno","body":"package boards\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\ntype (\n\t// RepostIterFn defines a function type to iterate reposts.\n\tRepostIterFn func(board, repost ID) bool\n\n\t// RepostStorage defines an interface for storing reposts.\n\tRepostStorage interface {\n\t\t// Get returns the repost ID for a board.\n\t\tGet(board ID) (repost ID, found bool)\n\n\t\t// Add adds a new repost to the storage.\n\t\tAdd(repost *Post) error\n\n\t\t// Remove removes repost for a board.\n\t\tRemove(board ID) (removed bool)\n\n\t\t// Size returns the number of reposts in the storage.\n\t\tSize() int\n\n\t\t// Iterate iterates reposts.\n\t\t// To reverse iterate reposts use a negative count.\n\t\t// If the callback returns true, the iteration is stopped.\n\t\tIterate(start, count int, fn RepostIterFn) bool\n\t}\n)\n\n// NewRepostStorage creates a new storage for reposts.\n// The new storage uses an AVL tree to store reposts.\nfunc NewRepostStorage() RepostStorage {\n\treturn \u0026repostStorage{bptree.NewBPTree32()}\n}\n\ntype repostStorage struct {\n\treposts *bptree.BPTree // string(Board.ID) -\u003e Post.ID\n}\n\n// Get returns the repost ID for a board.\nfunc (s repostStorage) Get(boardID ID) (ID, bool) {\n\tv := s.reposts.Get(boardID.Key())\n\tif v == nil {\n\t\treturn 0, false\n\t}\n\treturn v.(ID), true\n}\n\n// Add adds a new repost to the storage.\nfunc (s *repostStorage) Add(repost *Post) error {\n\tif repost == nil {\n\t\treturn errors.New(\"saving nil reposts is not allowed\")\n\t}\n\n\ts.reposts.Set(repost.Board.ID.Key(), repost.ID)\n\treturn nil\n}\n\n// Remove removes repost for a board.\nfunc (s *repostStorage) Remove(boardID ID) bool {\n\t_, removed := s.reposts.Remove(boardID.Key())\n\treturn removed\n}\n\n// Size returns the number of reposts in the storage.\nfunc (s repostStorage) Size() int {\n\treturn s.reposts.Size()\n}\n\n// Iterate iterates reposts.\n// To reverse iterate reposts use a negative count.\n// If the callback returns true, the iteration is stopped.\nfunc (s repostStorage) Iterate(start, count int, fn RepostIterFn) bool {\n\tif count \u003c 0 {\n\t\treturn s.reposts.ReverseIterateByOffset(start, -count, func(k string, v any) bool {\n\t\t\tid, err := seqid.FromString(k)\n\t\t\tif err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\n\t\t\treturn fn(ID(id), v.(ID))\n\t\t})\n\t}\n\n\treturn s.reposts.IterateByOffset(start, count, func(k string, v any) bool {\n\t\tid, err := seqid.FromString(k)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\treturn fn(ID(id), v.(ID))\n\t})\n}\n"},{"name":"repost_storage_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestRepostStorageGet(t *testing.T) {\n\ttests := []struct {\n\t\tname              string\n\t\tsetup             func() boards.RepostStorage\n\t\tboardID, repostID boards.ID\n\t\tfound             bool\n\t}{\n\t\t{\n\t\t\tname: \"single repost\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\ts := boards.NewRepostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    1,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t\t})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardID:  1,\n\t\t\trepostID: 1,\n\t\t\tfound:    true,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple reposts\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\ts := boards.NewRepostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    2,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t\t})\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    5,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 2},\n\t\t\t\t})\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    10,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 3},\n\t\t\t\t})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardID:  1,\n\t\t\trepostID: 2,\n\t\t\tfound:    true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\treturn boards.NewRepostStorage()\n\t\t\t},\n\t\t\tboardID: 404,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\trepostID, found := s.Get(tt.boardID)\n\n\t\t\tif !tt.found {\n\t\t\t\turequire.False(t, found, \"expect repost not to be found\")\n\t\t\t\turequire.True(t, int(repostID) == 0, \"expect repost ID to be 0\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.True(t, found, \"expect post to be found\")\n\t\t\turequire.Equal(t, tt.repostID.String(), repostID.String(), \"expect repost ID to match\")\n\t\t})\n\t}\n}\n\nfunc TestRepostStorageAdd(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\trepost *boards.Post\n\t\terrMsg string\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\trepost: \u0026boards.Post{\n\t\t\t\tID:    1,\n\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"nil repost\",\n\t\t\trepost: nil,\n\t\t\terrMsg: \"saving nil reposts is not allowed\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := boards.NewRepostStorage()\n\n\t\t\terr := s.Add(tt.repost)\n\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\turequire.Error(t, err, \"expect an error\")\n\t\t\t\turequire.ErrorContains(t, err, tt.errMsg, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\n\t\t\t_, found := s.Get(tt.repost.ID)\n\t\t\turequire.True(t, found, \"expect repost to be found\")\n\t\t})\n\t}\n}\n\nfunc TestRepostStorageRemove(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.RepostStorage\n\t\tboardID boards.ID\n\t\tremoved bool\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\ts := boards.NewRepostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    100,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t\t})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardID: 1,\n\t\t\tremoved: true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\treturn boards.NewRepostStorage()\n\t\t\t},\n\t\t\tboardID: 404,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\tremoved := s.Remove(tt.boardID)\n\n\t\t\tif !tt.removed {\n\t\t\t\turequire.False(t, removed, \"expect repost not to be removed\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.True(t, removed, \"expect repost to be removed\")\n\n\t\t\t_, found := s.Get(tt.boardID)\n\t\t\turequire.False(t, found, \"expect repost not to be found\")\n\t\t})\n\t}\n}\n\nfunc TestRepostStorageSize(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func() boards.RepostStorage\n\t\tsize  int\n\t}{\n\t\t{\n\t\t\tname: \"empty\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\treturn boards.NewRepostStorage()\n\t\t\t},\n\t\t\tsize: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"one repost\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\ts := boards.NewRepostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    1,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t\t})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple reposts\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\ts := boards.NewRepostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    1,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t\t})\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    1,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 2},\n\t\t\t\t})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 2,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\turequire.Equal(t, tt.size, s.Size())\n\t\t})\n\t}\n}\n\nfunc TestRepostStorageIterate(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.RepostStorage\n\t\treverse bool\n\t\tids     [][2]boards.ID\n\t}{\n\t\t{\n\t\t\tname: \"default\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\ts := boards.NewRepostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    10,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t\t})\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    20,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 2},\n\t\t\t\t})\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    30,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 3},\n\t\t\t\t})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tids: [][2]boards.ID{\n\t\t\t\t{1, 10},\n\t\t\t\t{2, 20},\n\t\t\t\t{3, 30},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"reverse\",\n\t\t\tsetup: func() boards.RepostStorage {\n\t\t\t\ts := boards.NewRepostStorage()\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    10,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 1},\n\t\t\t\t})\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    20,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 2},\n\t\t\t\t})\n\t\t\t\ts.Add(\u0026boards.Post{\n\t\t\t\t\tID:    30,\n\t\t\t\t\tBoard: \u0026boards.Board{ID: 3},\n\t\t\t\t})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\treverse: true,\n\t\t\tids: [][2]boards.ID{\n\t\t\t\t{3, 30},\n\t\t\t\t{2, 20},\n\t\t\t\t{1, 10},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\t\t\tcount := s.Size()\n\t\t\tif tt.reverse {\n\t\t\t\tcount = -count\n\t\t\t}\n\n\t\t\tvar i int\n\t\t\ts.Iterate(0, count, func(boardID, repostID boards.ID) bool {\n\t\t\t\turequire.True(t, tt.ids[i][0] == boardID, \"expect board ID to match\")\n\t\t\t\turequire.True(t, tt.ids[i][1] == repostID, \"expect repost ID to match\")\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"},{"name":"storage.gno","body":"package boards\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\ntype (\n\t// BoardIterFn defines a function type to iterate boards.\n\tBoardIterFn func(*Board) bool\n\n\t// Storage defines an interface for boards storage.\n\tStorage interface {\n\t\t// Get retruns a boards that matches an ID.\n\t\tGet(ID) (_ *Board, found bool)\n\n\t\t// GetByName retruns a boards that matches a name.\n\t\tGetByName(name string) (_ *Board, found bool)\n\n\t\t// Remove removes a board from the storage.\n\t\tRemove(ID) (_ *Board, removed bool)\n\n\t\t// Add adds a board to the storage.\n\t\tAdd(*Board) error\n\n\t\t// Size returns the number of boards in the storage.\n\t\tSize() int\n\n\t\t// Iterate iterates boards.\n\t\t// To reverse iterate boards use a negative count.\n\t\t// If the callback returns true, the iteration is stopped.\n\t\tIterate(start, count int, fn BoardIterFn) bool\n\t}\n)\n\n// NewStorage creates a new boards storage.\nfunc NewStorage() Storage {\n\treturn \u0026storage{\n\t\tbyID:   bptree.NewBPTree32(),\n\t\tbyName: bptree.NewBPTree32(),\n\t}\n}\n\ntype storage struct {\n\tbyID   *bptree.BPTree // string(Board.ID) -\u003e *Board\n\tbyName *bptree.BPTree // Board.Name -\u003e Board.ID\n}\n\n// Get returns a board for a specific ID.\nfunc (s storage) Get(boardID ID) (*Board, bool) {\n\tkey := makeBoardKey(boardID)\n\tv := s.byID.Get(key)\n\tif v == nil {\n\t\treturn nil, false\n\t}\n\treturn v.(*Board), true\n}\n\n// Get returns a board for a specific name.\nfunc (s storage) GetByName(name string) (*Board, bool) {\n\tkey := makeBoardNameKey(name)\n\tv := s.byName.Get(key)\n\tif v == nil {\n\t\treturn nil, false\n\t}\n\treturn s.Get(v.(ID))\n}\n\n// Remove removes a board from the storage.\n// It returns false when board is not found.\nfunc (s *storage) Remove(boardID ID) (*Board, bool) {\n\tboard, found := s.Get(boardID)\n\tif !found {\n\t\treturn nil, false\n\t}\n\n\t// Remove indexes for current and previous board names\n\tnames := append([]string{board.Name}, board.Aliases...)\n\tfor _, name := range names {\n\t\tkey := makeBoardNameKey(name)\n\n\t\t// Make sure that name is indexed to the board being removed\n\t\tv := s.byName.Get(key)\n\t\tif v != nil \u0026\u0026 v.(ID) == boardID {\n\t\t\ts.byName.Remove(key)\n\t\t}\n\t}\n\n\tkey := makeBoardKey(board.ID)\n\t_, removed := s.byID.Remove(key)\n\treturn board, removed\n}\n\n// Add adds a board to the storage.\n// If board already exists it updates storage by reindexing the board by ID and name.\n// When board name changes it's indexed so it can be found with the new and previous names.\nfunc (s *storage) Add(board *Board) error {\n\tif board == nil {\n\t\treturn errors.New(\"adding nil boards to the storage is not allowed\")\n\t}\n\n\tkey := makeBoardKey(board.ID)\n\ts.byID.Set(key, board)\n\n\t// Index by name when the optional board name is not empty\n\tif key = makeBoardNameKey(board.Name); key != \"\" {\n\t\ts.byName.Set(key, board.ID)\n\t}\n\treturn nil\n}\n\n// Size returns the number of boards in the storage.\nfunc (s storage) Size() int {\n\treturn s.byID.Size()\n}\n\n// Iterate iterates boards.\n// To reverse iterate boards use a negative count.\n// If the callback returns true, the iteration is stopped.\nfunc (s storage) Iterate(start, count int, fn BoardIterFn) bool {\n\tif count \u003c 0 {\n\t\treturn s.byID.ReverseIterateByOffset(start, -count, func(_ string, v any) bool {\n\t\t\treturn fn(v.(*Board))\n\t\t})\n\t}\n\n\treturn s.byID.IterateByOffset(start, count, func(_ string, v any) bool {\n\t\treturn fn(v.(*Board))\n\t})\n}\n\nfunc makeBoardKey(boardID ID) string {\n\treturn boardID.Key()\n}\n\nfunc makeBoardNameKey(name string) string {\n\tname = strings.TrimSpace(name)\n\treturn strings.ToLower(name)\n}\n"},{"name":"storage_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestStorageGet(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.Storage\n\t\tboardID boards.ID\n\t\tfound   bool\n\t}{\n\t\t{\n\t\t\tname: \"single board\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardID: 1,\n\t\t\tfound:   true,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple boards\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 2})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 3})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardID: 2,\n\t\t\tfound:   true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\treturn boards.NewStorage()\n\t\t\t},\n\t\t\tboardID: 404,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\tboard, found := s.Get(tt.boardID)\n\n\t\t\tif !tt.found {\n\t\t\t\turequire.False(t, found, \"expect board not to be found\")\n\t\t\t\turequire.True(t, board == nil, \"expect board to be nil\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.True(t, found, \"expect board to be found\")\n\t\t\turequire.False(t, board == nil, \"expect board not to be nil\")\n\t\t\turequire.Equal(t, tt.boardID.String(), board.ID.String(), \"expect board ID to match\")\n\t\t})\n\t}\n}\n\nfunc TestStorageGetByName(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tsetup     func() boards.Storage\n\t\tboardName string\n\t\tfound     bool\n\t}{\n\t\t{\n\t\t\tname: \"single board\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1, Name: \"A\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardName: \"A\",\n\t\t\tfound:     true,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple boards\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1, Name: \"A\"})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 2, Name: \"B\"})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 3, Name: \"C\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardName: \"B\",\n\t\t\tfound:     true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\treturn boards.NewStorage()\n\t\t\t},\n\t\t\tboardName: \"foo\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\tboard, found := s.GetByName(tt.boardName)\n\n\t\t\tif !tt.found {\n\t\t\t\turequire.False(t, found, \"expect board not to be found\")\n\t\t\t\turequire.True(t, board == nil, \"expect board to be nil\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.True(t, found, \"expect board to be found\")\n\t\t\turequire.False(t, board == nil, \"expect board not to be nil\")\n\t\t\turequire.Equal(t, tt.boardName, board.Name, \"expect board name to match\")\n\t\t})\n\t}\n}\n\nfunc TestStorageRemove(t *testing.T) {\n\ttests := []struct {\n\t\tname       string\n\t\tsetup      func() boards.Storage\n\t\tboardID    boards.ID\n\t\tboardNames []string\n\t\tremoved    bool\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1, Name: \"A\"})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 2, Name: \"B\"})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardID:    2,\n\t\t\tboardNames: []string{\"B\"},\n\t\t\tremoved:    true,\n\t\t},\n\t\t{\n\t\t\tname: \"ok with aliases\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1, Name: \"A\"})\n\n\t\t\t\tb := \u0026boards.Board{ID: 2, Name: \"B\"}\n\t\t\t\ts.Add(b)\n\n\t\t\t\tb.Aliases = []string{\"A\"}\n\t\t\t\tb.Name = \"C\"\n\t\t\t\ts.Add(b)\n\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tboardID:    2,\n\t\t\tboardNames: []string{\"B\", \"C\"},\n\t\t\tremoved:    true,\n\t\t},\n\t\t{\n\t\t\tname: \"not found\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\treturn boards.NewStorage()\n\t\t\t},\n\t\t\tboardID: 404,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\tboard, removed := s.Remove(tt.boardID)\n\n\t\t\tif !tt.removed {\n\t\t\t\turequire.False(t, removed, \"expect board not to be removed\")\n\t\t\t\turequire.True(t, board == nil, \"expect board to be nil\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.True(t, removed, \"expect board to be removed\")\n\t\t\turequire.False(t, board == nil, \"expect board not to be nil\")\n\t\t\turequire.Equal(t, tt.boardID.String(), board.ID.String(), \"expect board ID to match\")\n\n\t\t\t_, found := s.Get(tt.boardID)\n\t\t\turequire.False(t, found, \"expect board not to be found by ID\")\n\n\t\t\tfor _, name := range tt.boardNames {\n\t\t\t\t_, found = s.GetByName(name)\n\t\t\t\turequire.False(t, found, \"expect board not to be found by name: \"+name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestStorageAdd(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func() boards.Storage\n\t\tboard  *boards.Board\n\t\terrMsg string\n\t}{\n\t\t{\n\t\t\tname:  \"ok\",\n\t\t\tboard: \u0026boards.Board{ID: 1, Name: \"A\"},\n\t\t},\n\t\t{\n\t\t\tname:   \"nil board\",\n\t\t\tboard:  nil,\n\t\t\terrMsg: \"adding nil boards to the storage is not allowed\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := boards.NewStorage()\n\n\t\t\terr := s.Add(tt.board)\n\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\turequire.Error(t, err, \"expect an error\")\n\t\t\t\turequire.ErrorContains(t, err, tt.errMsg, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\n\t\t\t_, found := s.Get(tt.board.ID)\n\t\t\turequire.True(t, found, \"expect board to be found by ID\")\n\n\t\t\t_, found = s.GetByName(tt.board.Name)\n\t\t\turequire.True(t, found, \"expect board to be found by name\")\n\t\t})\n\t}\n}\n\nfunc TestStorageSize(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func() boards.Storage\n\t\tsize  int\n\t}{\n\t\t{\n\t\t\tname: \"empty\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\treturn boards.NewStorage()\n\t\t\t},\n\t\t\tsize: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"one board\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple boards\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 2})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 2,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\n\t\t\turequire.Equal(t, tt.size, s.Size())\n\t\t})\n\t}\n}\n\nfunc TestStorageIterate(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() boards.Storage\n\t\treverse bool\n\t\tids     []boards.ID\n\t}{\n\t\t{\n\t\t\tname: \"default\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 2})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 3})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tids: []boards.ID{1, 2, 3},\n\t\t},\n\t\t{\n\t\t\tname: \"reverse\",\n\t\t\tsetup: func() boards.Storage {\n\t\t\t\ts := boards.NewStorage()\n\t\t\t\ts.Add(\u0026boards.Board{ID: 1})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 2})\n\t\t\t\ts.Add(\u0026boards.Board{ID: 3})\n\t\t\t\treturn s\n\t\t\t},\n\t\t\treverse: true,\n\t\t\tids:     []boards.ID{3, 2, 1},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ts := tt.setup()\n\t\t\tcount := s.Size()\n\t\t\tif tt.reverse {\n\t\t\t\tcount = -count\n\t\t\t}\n\n\t\t\tvar i int\n\t\t\ts.Iterate(0, count, func(p *boards.Board) bool {\n\t\t\t\turequire.True(t, tt.ids[i] == p.ID, \"expect board ID to match\")\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"},{"name":"thread.gno","body":"package boards\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// NewThread creates a new board thread.\nfunc NewThread(b *Board, creator address, title, body string) (*Post, error) {\n\tif b == nil {\n\t\treturn nil, errors.New(\"thread requires a parent board\")\n\t}\n\n\tif !creator.IsValid() {\n\t\treturn nil, ufmt.Errorf(\"invalid thread creator address: %s\", creator)\n\t}\n\n\ttitle = strings.TrimSpace(title)\n\tif title == \"\" {\n\t\treturn nil, errors.New(\"thread title is required\")\n\t}\n\n\tbody = strings.TrimSpace(body)\n\tif body == \"\" {\n\t\treturn nil, errors.New(\"thread body is required\")\n\t}\n\n\tid := b.ThreadsSequence.Next()\n\treturn \u0026Post{\n\t\tID:        id,\n\t\tThreadID:  id,\n\t\tBoard:     b,\n\t\tTitle:     title,\n\t\tBody:      body,\n\t\tReplies:   NewPostStorage(),\n\t\tReposts:   NewRepostStorage(),\n\t\tFlags:     NewFlagStorage(),\n\t\tCreator:   creator,\n\t\tCreatedAt: time.Now(),\n\t}, nil\n}\n\n// MustNewThread creates a new thread or panics on error.\nfunc MustNewThread(b *Board, creator address, title, body string) *Post {\n\tt, err := NewThread(b, creator, title, body)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn t\n}\n\n// NewRepost creates a new thread that is a repost of a thread from another board.\nfunc NewRepost(thread *Post, dst *Board, creator address) (*Post, error) {\n\tif thread == nil {\n\t\treturn nil, errors.New(\"thread to repost is required\")\n\t}\n\n\tif thread.Board == nil {\n\t\treturn nil, errors.New(\"original thread has no board assigned\")\n\t}\n\n\tif dst == nil {\n\t\treturn nil, errors.New(\"thread repost requires a destination board\")\n\t}\n\n\tif IsRepost(thread) {\n\t\treturn nil, errors.New(\"reposting a thread that is a repost is not allowed\")\n\t}\n\n\tif !IsThread(thread) {\n\t\treturn nil, errors.New(\"post must be a thread to be reposted to another board\")\n\t}\n\n\tif !creator.IsValid() {\n\t\treturn nil, ufmt.Errorf(\"invalid thread repost creator address: %s\", creator)\n\t}\n\n\tid := dst.ThreadsSequence.Next()\n\treturn \u0026Post{\n\t\tID:              id,\n\t\tThreadID:        id,\n\t\tParentID:        thread.ID,\n\t\tOriginalBoardID: thread.Board.ID,\n\t\tBoard:           dst,\n\t\tReplies:         NewPostStorage(),\n\t\tReposts:         NewRepostStorage(),\n\t\tFlags:           NewFlagStorage(),\n\t\tCreator:         creator,\n\t\tCreatedAt:       time.Now(),\n\t}, nil\n}\n\n// MustNewRepost creates a new thread that is a repost of a thread from another board or panics on error.\nfunc MustNewRepost(thread *Post, dst *Board, creator address) *Post {\n\tr, err := NewRepost(thread, dst, creator)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn r\n}\n"},{"name":"thread_test.gno","body":"package boards_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc TestNewThread(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tboard       *boards.Board\n\t\tcreator     address\n\t\ttitle, body string\n\t\terrMsg      string\n\t}{\n\t\t{\n\t\t\tname:    \"ok\",\n\t\t\tboard:   boards.New(1),\n\t\t\tcreator: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\ttitle:   \"Test\",\n\t\t\tbody:    \"Foo\",\n\t\t},\n\t\t{\n\t\t\tname:   \"nil board\",\n\t\t\tboard:  nil,\n\t\t\terrMsg: \"thread requires a parent board\",\n\t\t},\n\t\t{\n\t\t\tname:    \"invalid creator\",\n\t\t\tboard:   boards.New(1),\n\t\t\tcreator: \"foo\",\n\t\t\terrMsg:  \"invalid thread creator address: foo\",\n\t\t},\n\t\t{\n\t\t\tname:    \"empty title\",\n\t\t\tboard:   boards.New(1),\n\t\t\tcreator: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\ttitle:   \"\",\n\t\t\terrMsg:  \"thread title is required\",\n\t\t},\n\t\t{\n\t\t\tname:    \"empty body\",\n\t\t\tboard:   boards.New(1),\n\t\t\tcreator: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\ttitle:   \"Test\",\n\t\t\tbody:    \"\",\n\t\t\terrMsg:  \"thread body is required\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tthread, err := boards.NewThread(tt.board, tt.creator, tt.title, tt.body)\n\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\turequire.Error(t, err, \"expect an error\")\n\t\t\t\turequire.ErrorContains(t, err, tt.errMsg, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\turequire.True(t, tt.board.ThreadsSequence.Last() == thread.ID, \"expect ID to match\")\n\t\t\turequire.True(t, thread.ThreadID == thread.ID, \"expect thread ID to match\")\n\t\t\turequire.False(t, thread.Board == nil, \"expect board to be assigned\")\n\t\t\turequire.True(t, tt.board.ID == thread.Board.ID, \"expect board ID to match\")\n\t\t\turequire.Equal(t, tt.title, thread.Title, \"expect title to match\")\n\t\t\turequire.Equal(t, tt.body, thread.Body, \"expect body to match\")\n\t\t\turequire.True(t, thread.Replies != nil, \"expect thread to support sub-replies\")\n\t\t\turequire.True(t, thread.Reposts != nil, \"expect thread to support reposts\")\n\t\t\turequire.True(t, thread.Flags != nil, \"expect thread to support flagging\")\n\t\t\turequire.Equal(t, tt.creator, thread.Creator, \"expect creator to match\")\n\t\t\turequire.False(t, thread.CreatedAt.IsZero(), \"expect creation date to be assigned\")\n\t\t})\n\t}\n}\n\nfunc TestNewRepost(t *testing.T) {\n\ttests := []struct {\n\t\tname       string\n\t\torigThread *boards.Post\n\t\tdstBoard   *boards.Board\n\t\tcreator    address\n\t\terrMsg     string\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\torigThread: boards.MustNewThread(\n\t\t\t\tboards.New(1),\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"Title\",\n\t\t\t\t\"Body\",\n\t\t\t),\n\t\t\tdstBoard: boards.New(2),\n\t\t\tcreator:  \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t\t{\n\t\t\tname:       \"nil original thread\",\n\t\t\torigThread: nil,\n\t\t\terrMsg:     \"thread to repost is required\",\n\t\t},\n\t\t{\n\t\t\tname:       \"original thread without board\",\n\t\t\torigThread: \u0026boards.Post{ID: 1},\n\t\t\terrMsg:     \"original thread has no board assigned\",\n\t\t},\n\t\t{\n\t\t\tname: \"nil destination board\",\n\t\t\torigThread: boards.MustNewThread(\n\t\t\t\tboards.New(1),\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"Title\",\n\t\t\t\t\"Body\",\n\t\t\t),\n\t\t\tdstBoard: nil,\n\t\t\terrMsg:   \"thread repost requires a destination board\",\n\t\t},\n\t\t{\n\t\t\tname: \"original thread is not a thread\",\n\t\t\torigThread: \u0026boards.Post{\n\t\t\t\tID:       1,\n\t\t\t\tThreadID: 2,\n\t\t\t\tBoard:    boards.New(1),\n\t\t\t},\n\t\t\tdstBoard: boards.New(2),\n\t\t\terrMsg:   \"post must be a thread to be reposted to another board\",\n\t\t},\n\t\t{\n\t\t\tname: \"original thread is a repost\",\n\t\t\torigThread: \u0026boards.Post{\n\t\t\t\tID:              1,\n\t\t\t\tThreadID:        1,\n\t\t\t\tOriginalBoardID: 1,\n\t\t\t\tBoard:           boards.New(1),\n\t\t\t},\n\t\t\tdstBoard: boards.New(2),\n\t\t\terrMsg:   \"reposting a thread that is a repost is not allowed\",\n\t\t},\n\t\t{\n\t\t\tname: \"invalid creator\",\n\t\t\torigThread: boards.MustNewThread(\n\t\t\t\tboards.New(1),\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"Title\",\n\t\t\t\t\"Body\",\n\t\t\t),\n\t\t\tdstBoard: boards.New(2),\n\t\t\tcreator:  \"foo\",\n\t\t\terrMsg:   \"invalid thread repost creator address: foo\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tthread, err := boards.NewRepost(tt.origThread, tt.dstBoard, tt.creator)\n\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\turequire.Error(t, err, \"expect an error\")\n\t\t\t\turequire.ErrorContains(t, err, tt.errMsg, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\turequire.True(t, thread.ID == tt.dstBoard.ThreadsSequence.Last(), \"expect ID to match\")\n\t\t\turequire.True(t, thread.ThreadID == thread.ID, \"expect thread ID to match\")\n\t\t\turequire.True(t, thread.ParentID == tt.origThread.ID, \"expect parent ID to match\")\n\t\t\turequire.True(t, thread.OriginalBoardID == tt.origThread.Board.ID, \"expect original board ID to match\")\n\t\t\turequire.False(t, thread.Board == nil, \"expect board to be assigned\")\n\t\t\turequire.True(t, thread.Board.ID == tt.dstBoard.ID, \"expect board ID to match\")\n\t\t\turequire.Empty(t, thread.Title, \"expect title to be empty\")\n\t\t\turequire.Empty(t, thread.Body, \"expect body to be empty\")\n\t\t\turequire.True(t, thread.Replies != nil, \"expect thread to support sub-replies\")\n\t\t\turequire.True(t, thread.Reposts != nil, \"expect thread to support reposts\")\n\t\t\turequire.True(t, thread.Flags != nil, \"expect thread to support flagging\")\n\t\t\turequire.Equal(t, tt.creator, thread.Creator, \"expect creator to match\")\n\t\t\turequire.False(t, thread.CreatedAt.IsZero(), \"expect creation date to be assigned\")\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"qnWjuPldlYBAbfsNsZuay+hgpixpBqsxYENl54lBUyvGa1HIGXqD452A1unmIGrIBjMB8Rc63E1LqNZ/u0CnDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"addrset","path":"gno.land/p/moul/addrset","files":[{"name":"addrset.gno","body":"// Package addrset provides a specialized set data structure for managing unique Gno addresses.\n//\n// It is built on top of an AVL tree for efficient operations and maintains addresses in sorted order.\n// This package is particularly useful when you need to:\n//   - Track a collection of unique addresses (e.g., for whitelists, participants, etc.)\n//   - Efficiently check address membership\n//   - Support pagination when displaying addresses\n//\n// Example usage:\n//\n//\timport (\n//\t    \"gno.land/p/moul/addrset\"\n//\t)\n//\n//\tfunc MyHandler() {\n//\t    // Create a new address set\n//\t    var set addrset.Set\n//\n//\t    // Add some addresses\n//\t    addr1 := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n//\t    addr2 := address(\"g1sss5g0rkqr88k4u648yd5d3l9t4d8vvqwszqth\")\n//\n//\t    set.Add(addr1)  // returns true (newly added)\n//\t    set.Add(addr2)  // returns true (newly added)\n//\t    set.Add(addr1)  // returns false (already exists)\n//\n//\t    // Check membership\n//\t    if set.Has(addr1) {\n//\t        // addr1 is in the set\n//\t    }\n//\n//\t    // Get size\n//\t    size := set.Size()  // returns 2\n//\n//\t    // Iterate with pagination (10 items per page, starting at offset 0)\n//\t    set.IterateByOffset(0, 10, func(addr address) bool {\n//\t        // Process addr\n//\t        return false  // continue iteration\n//\t    })\n//\n//\t    // Remove an address\n//\t    set.Remove(addr1)  // returns true (was present)\n//\t    set.Remove(addr1)  // returns false (not present)\n//\t}\npackage addrset\n\nimport \"gno.land/p/nt/avl/v0\"\n\ntype Set struct {\n\ttree avl.Tree\n}\n\n// Add inserts an address into the set.\n// Returns true if the address was newly added, false if it already existed.\nfunc (s *Set) Add(addr address) bool {\n\treturn !s.tree.Set(string(addr), nil)\n}\n\n// Remove deletes an address from the set.\n// Returns true if the address was found and removed, false if it didn't exist.\nfunc (s *Set) Remove(addr address) bool {\n\t_, removed := s.tree.Remove(string(addr))\n\treturn removed\n}\n\n// Has checks if an address exists in the set.\nfunc (s *Set) Has(addr address) bool {\n\treturn s.tree.Has(string(addr))\n}\n\n// Size returns the number of addresses in the set.\nfunc (s *Set) Size() int {\n\treturn s.tree.Size()\n}\n\n// IterateByOffset walks through addresses starting at the given offset.\n// The callback should return true to stop iteration.\nfunc (s *Set) IterateByOffset(offset int, count int, cb func(addr address) bool) {\n\ts.tree.IterateByOffset(offset, count, func(key string, _ any) bool {\n\t\treturn cb(address(key))\n\t})\n}\n\n// ReverseIterateByOffset walks through addresses in reverse order starting at the given offset.\n// The callback should return true to stop iteration.\nfunc (s *Set) ReverseIterateByOffset(offset int, count int, cb func(addr address) bool) {\n\ts.tree.ReverseIterateByOffset(offset, count, func(key string, _ any) bool {\n\t\treturn cb(address(key))\n\t})\n}\n\n// Tree returns the underlying AVL tree for advanced usage.\nfunc (s *Set) Tree() avl.ITree {\n\treturn \u0026s.tree\n}\n"},{"name":"addrset_test.gno","body":"package addrset\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestSet(t *testing.T) {\n\taddr1 := address(\"addr1\")\n\taddr2 := address(\"addr2\")\n\taddr3 := address(\"addr3\")\n\n\ttests := []struct {\n\t\tname    string\n\t\tactions func(s *Set)\n\t\tsize    int\n\t\thas     map[address]bool\n\t\taddrs   []address // for iteration checks\n\t}{\n\t\t{\n\t\t\tname:    \"empty set\",\n\t\t\tactions: func(s *Set) {},\n\t\t\tsize:    0,\n\t\t\thas:     map[address]bool{addr1: false},\n\t\t},\n\t\t{\n\t\t\tname: \"single address\",\n\t\t\tactions: func(s *Set) {\n\t\t\t\ts.Add(addr1)\n\t\t\t},\n\t\t\tsize: 1,\n\t\t\thas: map[address]bool{\n\t\t\t\taddr1: true,\n\t\t\t\taddr2: false,\n\t\t\t},\n\t\t\taddrs: []address{addr1},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple addresses\",\n\t\t\tactions: func(s *Set) {\n\t\t\t\ts.Add(addr1)\n\t\t\t\ts.Add(addr2)\n\t\t\t\ts.Add(addr3)\n\t\t\t},\n\t\t\tsize: 3,\n\t\t\thas: map[address]bool{\n\t\t\t\taddr1: true,\n\t\t\t\taddr2: true,\n\t\t\t\taddr3: true,\n\t\t\t},\n\t\t\taddrs: []address{addr1, addr2, addr3},\n\t\t},\n\t\t{\n\t\t\tname: \"remove address\",\n\t\t\tactions: func(s *Set) {\n\t\t\t\ts.Add(addr1)\n\t\t\t\ts.Add(addr2)\n\t\t\t\ts.Remove(addr1)\n\t\t\t},\n\t\t\tsize: 1,\n\t\t\thas: map[address]bool{\n\t\t\t\taddr1: false,\n\t\t\t\taddr2: true,\n\t\t\t},\n\t\t\taddrs: []address{addr2},\n\t\t},\n\t\t{\n\t\t\tname: \"duplicate adds\",\n\t\t\tactions: func(s *Set) {\n\t\t\t\tuassert.True(t, s.Add(addr1))     // first add returns true\n\t\t\t\tuassert.False(t, s.Add(addr1))    // second add returns false\n\t\t\t\tuassert.True(t, s.Remove(addr1))  // remove existing returns true\n\t\t\t\tuassert.False(t, s.Remove(addr1)) // remove non-existing returns false\n\t\t\t},\n\t\t\tsize: 0,\n\t\t\thas: map[address]bool{\n\t\t\t\taddr1: false,\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar set Set\n\n\t\t\t// Execute test actions\n\t\t\ttt.actions(\u0026set)\n\n\t\t\t// Check size\n\t\t\tuassert.Equal(t, tt.size, set.Size())\n\n\t\t\t// Check existence\n\t\t\tfor addr, expected := range tt.has {\n\t\t\t\tuassert.Equal(t, expected, set.Has(addr))\n\t\t\t}\n\n\t\t\t// Check iteration if addresses are specified\n\t\t\tif tt.addrs != nil {\n\t\t\t\tcollected := []address{}\n\t\t\t\tset.IterateByOffset(0, 10, func(addr address) bool {\n\t\t\t\t\tcollected = append(collected, addr)\n\t\t\t\t\treturn false\n\t\t\t\t})\n\n\t\t\t\t// Check length\n\t\t\t\tuassert.Equal(t, len(tt.addrs), len(collected))\n\n\t\t\t\t// Check each address\n\t\t\t\tfor i, addr := range tt.addrs {\n\t\t\t\t\tuassert.Equal(t, addr, collected[i])\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSetIterationLimits(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\taddrs    []address\n\t\toffset   int\n\t\tlimit    int\n\t\texpected int\n\t}{\n\t\t{\n\t\t\tname:     \"zero offset full list\",\n\t\t\taddrs:    []address{\"a1\", \"a2\", \"a3\"},\n\t\t\toffset:   0,\n\t\t\tlimit:    10,\n\t\t\texpected: 3,\n\t\t},\n\t\t{\n\t\t\tname:     \"offset with limit\",\n\t\t\taddrs:    []address{\"a1\", \"a2\", \"a3\", \"a4\"},\n\t\t\toffset:   1,\n\t\t\tlimit:    2,\n\t\t\texpected: 2,\n\t\t},\n\t\t{\n\t\t\tname:     \"offset beyond size\",\n\t\t\taddrs:    []address{\"a1\", \"a2\"},\n\t\t\toffset:   3,\n\t\t\tlimit:    1,\n\t\t\texpected: 0,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar set Set\n\t\t\tfor _, addr := range tt.addrs {\n\t\t\t\tset.Add(addr)\n\t\t\t}\n\n\t\t\t// Test forward iteration\n\t\t\tcount := 0\n\t\t\tset.IterateByOffset(tt.offset, tt.limit, func(addr address) bool {\n\t\t\t\tcount++\n\t\t\t\treturn false\n\t\t\t})\n\t\t\tuassert.Equal(t, tt.expected, count)\n\n\t\t\t// Test reverse iteration\n\t\t\tcount = 0\n\t\t\tset.ReverseIterateByOffset(tt.offset, tt.limit, func(addr address) bool {\n\t\t\t\tcount++\n\t\t\t\treturn false\n\t\t\t})\n\t\t\tuassert.Equal(t, tt.expected, count)\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/addrset\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"SwtUhovpxFFVfWg3XudIY/bFSvys+J8f3G/Vdn3Zyvu85DZcBg5L/Ra0gj+mnLSR0Kb2ndjAA4bqXy6eeWNbCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"rotree","path":"gno.land/p/nt/bptree/v0/rotree","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/bptree/v0/rotree\"\ngno = \"0.9\"\n"},{"name":"rotree.gno","body":"// Package rotree provides a read-only wrapper for bptree.BPTree with safe value transformation.\n//\n// It is useful when you want to expose a read-only view of a tree while ensuring that\n// the sensitive data cannot be modified.\n//\n// Example:\n//\n//\t// Define a user structure with sensitive data\n//\ttype User struct {\n//\t\tName     string\n//\t\tBalance  int\n//\t\tInternal string // sensitive field\n//\t}\n//\n//\t// Create and populate the original tree\n//\tprivateTree := bptree.NewBPTree32()\n//\tprivateTree.Set(\"alice\", \u0026User{\n//\t\tName:     \"Alice\",\n//\t\tBalance:  100,\n//\t\tInternal: \"sensitive\",\n//\t})\n//\n//\t// Create a safe transformation function that copies the struct\n//\t// while excluding sensitive data\n//\tmakeEntrySafeFn := func(v any) any {\n//\t\tu := v.(*User)\n//\t\treturn \u0026User{\n//\t\t\tName:     u.Name,\n//\t\t\tBalance:  u.Balance,\n//\t\t\tInternal: \"\", // omit sensitive data\n//\t\t}\n//\t}\n//\n//\t// Create a read-only view of the tree\n//\tPublicTree := rotree.Wrap(tree, makeEntrySafeFn)\n//\n//\t// Safely access the data\n//\tvalue := roTree.Get(\"alice\")\n//\tuser := value.(*User)\n//\t// user.Name == \"Alice\"\n//\t// user.Balance == 100\n//\t// user.Internal == \"\" (sensitive data is filtered)\npackage rotree\n\nimport (\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\n// Wrap creates a new ReadOnlyTree from an existing bptree.BPTree and a safety transformation function.\n// If makeEntrySafeFn is nil, values will be returned as-is without transformation.\nfunc Wrap(tree *bptree.BPTree, makeEntrySafeFn func(any) any) *ReadOnlyTree {\n\treturn \u0026ReadOnlyTree{\n\t\ttree:            tree,\n\t\tmakeEntrySafeFn: makeEntrySafeFn,\n\t}\n}\n\n// ReadOnlyTree wraps a bptree.BPTree and provides read-only access.\ntype ReadOnlyTree struct {\n\ttree            *bptree.BPTree\n\tmakeEntrySafeFn func(any) any\n}\n\n// IReadOnlyTree defines the read-only operations available on a tree.\ntype IReadOnlyTree interface {\n\tSize() int\n\tHas(key string) bool\n\tGet(key string) any\n\tGetByIndex(index int) (string, any)\n\tIterate(start, end string, cb bptree.IterCbFn) bool\n\tReverseIterate(start, end string, cb bptree.IterCbFn) bool\n\tIterateByOffset(offset int, count int, cb bptree.IterCbFn) bool\n\tReverseIterateByOffset(offset int, count int, cb bptree.IterCbFn) bool\n}\n\n// Verify that ReadOnlyTree implements both ITree and IReadOnlyTree\nvar (\n\t_ bptree.ITree  = (*ReadOnlyTree)(nil)\n\t_ IReadOnlyTree = (*ReadOnlyTree)(nil)\n)\n\n// getSafeValue applies the makeEntrySafeFn if it exists, otherwise returns the original value\nfunc (roTree *ReadOnlyTree) getSafeValue(value any) any {\n\tif roTree.makeEntrySafeFn == nil {\n\t\treturn value\n\t}\n\treturn roTree.makeEntrySafeFn(value)\n}\n\n// Size returns the number of key-value pairs in the tree.\nfunc (roTree *ReadOnlyTree) Size() int {\n\treturn roTree.tree.Size()\n}\n\n// Has checks whether a key exists in the tree.\nfunc (roTree *ReadOnlyTree) Has(key string) bool {\n\treturn roTree.tree.Has(key)\n}\n\n// Get retrieves the value associated with the given key, converted to a safe format.\n// It returns the value if the key exists, or nil if it doesn't.\nfunc (roTree *ReadOnlyTree) Get(key string) any {\n\tvalue := roTree.tree.Get(key)\n\tif value == nil {\n\t\treturn nil\n\t}\n\treturn roTree.getSafeValue(value)\n}\n\n// GetByIndex retrieves the key-value pair at the specified index in the tree, with the value converted to a safe format.\nfunc (roTree *ReadOnlyTree) GetByIndex(index int) (string, any) {\n\tkey, value := roTree.tree.GetByIndex(index)\n\treturn key, roTree.getSafeValue(value)\n}\n\n// Iterate performs an in-order traversal of the tree within the specified key range.\nfunc (roTree *ReadOnlyTree) Iterate(start, end string, cb bptree.IterCbFn) bool {\n\treturn roTree.tree.Iterate(start, end, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// ReverseIterate performs a reverse in-order traversal of the tree within the specified key range.\nfunc (roTree *ReadOnlyTree) ReverseIterate(start, end string, cb bptree.IterCbFn) bool {\n\treturn roTree.tree.ReverseIterate(start, end, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// IterateByOffset performs an in-order traversal of the tree starting from the specified offset.\nfunc (roTree *ReadOnlyTree) IterateByOffset(offset int, count int, cb bptree.IterCbFn) bool {\n\treturn roTree.tree.IterateByOffset(offset, count, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// ReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset.\nfunc (roTree *ReadOnlyTree) ReverseIterateByOffset(offset int, count int, cb bptree.IterCbFn) bool {\n\treturn roTree.tree.ReverseIterateByOffset(offset, count, func(key string, value any) bool {\n\t\treturn cb(key, roTree.getSafeValue(value))\n\t})\n}\n\n// Set is not supported on ReadOnlyTree and will panic.\nfunc (roTree *ReadOnlyTree) Set(key string, value any) bool {\n\tpanic(\"Set operation not supported on ReadOnlyTree\")\n}\n\n// Remove is not supported on ReadOnlyTree and will panic.\nfunc (roTree *ReadOnlyTree) Remove(key string) (value any, removed bool) {\n\tpanic(\"Remove operation not supported on ReadOnlyTree\")\n}\n"},{"name":"rotree_test.gno","body":"package rotree\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\nfunc TestExample(t *testing.T) {\n\t// User represents our internal data structure\n\ttype User struct {\n\t\tID       string\n\t\tName     string\n\t\tBalance  int\n\t\tInternal string // sensitive internal data\n\t}\n\n\t// Create and populate the original tree with user pointers\n\ttree := bptree.NewBPTree32()\n\ttree.Set(\"alice\", \u0026User{\n\t\tID:       \"1\",\n\t\tName:     \"Alice\",\n\t\tBalance:  100,\n\t\tInternal: \"sensitive_data_1\",\n\t})\n\ttree.Set(\"bob\", \u0026User{\n\t\tID:       \"2\",\n\t\tName:     \"Bob\",\n\t\tBalance:  200,\n\t\tInternal: \"sensitive_data_2\",\n\t})\n\n\t// Define a makeEntrySafeFn that:\n\t// 1. Creates a defensive copy of the User struct\n\t// 2. Omits sensitive internal data\n\tmakeEntrySafeFn := func(v any) any {\n\t\toriginalUser := v.(*User)\n\t\treturn \u0026User{\n\t\t\tID:       originalUser.ID,\n\t\t\tName:     originalUser.Name,\n\t\t\tBalance:  originalUser.Balance,\n\t\t\tInternal: \"\", // Omit sensitive data\n\t\t}\n\t}\n\n\t// Create a read-only view of the tree\n\troTree := Wrap(tree, makeEntrySafeFn)\n\n\t// Test retrieving and verifying a user\n\tt.Run(\"Get User\", func(t *testing.T) {\n\t\t// Get user from read-only tree\n\t\tvalue := roTree.Get(\"alice\")\n\t\tif value == nil {\n\t\t\tt.Fatal(\"User 'alice' not found\")\n\t\t}\n\n\t\tuser := value.(*User)\n\n\t\t// Verify user data is correct\n\t\tif user.Name != \"Alice\" || user.Balance != 100 {\n\t\t\tt.Errorf(\"Unexpected user data: got name=%s balance=%d\", user.Name, user.Balance)\n\t\t}\n\n\t\t// Verify sensitive data is not exposed\n\t\tif user.Internal != \"\" {\n\t\t\tt.Error(\"Sensitive data should not be exposed\")\n\t\t}\n\n\t\t// Verify it's a different instance than the original\n\t\toriginalValue := tree.Get(\"alice\")\n\t\toriginalUser := originalValue.(*User)\n\t\tif user == originalUser {\n\t\t\tt.Error(\"Read-only tree should return a copy, not the original pointer\")\n\t\t}\n\t})\n\n\t// Test iterating over users\n\tt.Run(\"Iterate Users\", func(t *testing.T) {\n\t\tcount := 0\n\t\troTree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tuser := value.(*User)\n\t\t\t// Verify each user has empty Internal field\n\t\t\tif user.Internal != \"\" {\n\t\t\t\tt.Error(\"Sensitive data exposed during iteration\")\n\t\t\t}\n\t\t\tcount++\n\t\t\treturn false\n\t\t})\n\n\t\tif count != 2 {\n\t\t\tt.Errorf(\"Expected 2 users, got %d\", count)\n\t\t}\n\t})\n\n\t// Verify that modifications to the returned user don't affect the original\n\tt.Run(\"Modification Safety\", func(t *testing.T) {\n\t\tvalue := roTree.Get(\"alice\")\n\t\tuser := value.(*User)\n\n\t\t// Try to modify the returned user\n\t\tuser.Balance = 999\n\t\tuser.Internal = \"hacked\"\n\n\t\t// Verify original is unchanged\n\t\toriginalValue := tree.Get(\"alice\")\n\t\toriginalUser := originalValue.(*User)\n\t\tif originalUser.Balance != 100 || originalUser.Internal != \"sensitive_data_1\" {\n\t\t\tt.Error(\"Original user data was modified\")\n\t\t}\n\t})\n}\n\nfunc TestReadOnlyTree(t *testing.T) {\n\t// Example of a makeEntrySafeFn that appends \"_readonly\" to demonstrate transformation\n\tmakeEntrySafeFn := func(value any) any {\n\t\treturn value.(string) + \"_readonly\"\n\t}\n\n\ttree := bptree.NewBPTree32()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\troTree := Wrap(tree, makeEntrySafeFn)\n\n\ttests := []struct {\n\t\tname     string\n\t\tkey      string\n\t\texpected any\n\t}{\n\t\t{\"ExistingKey1\", \"key1\", \"value1_readonly\"},\n\t\t{\"ExistingKey2\", \"key2\", \"value2_readonly\"},\n\t\t{\"NonExistingKey\", \"key4\", nil},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvalue := roTree.Get(tt.key)\n\t\t\tif value != tt.expected {\n\t\t\t\tt.Errorf(\"For key %s, expected %v, got %v\", tt.key, tt.expected, value)\n\t\t\t}\n\t\t})\n\t}\n}\n\n// Add example tests showing different makeEntrySafeFn implementations\nfunc TestMakeEntrySafeFnVariants(t *testing.T) {\n\ttree := bptree.NewBPTree32()\n\ttree.Set(\"slice\", []int{1, 2, 3})\n\ttree.Set(\"map\", map[string]int{\"a\": 1})\n\n\ttests := []struct {\n\t\tname            string\n\t\tmakeEntrySafeFn func(any) any\n\t\tkey             string\n\t\tvalidate        func(t *testing.T, value any)\n\t}{\n\t\t{\n\t\t\tname: \"Defensive Copy Slice\",\n\t\t\tmakeEntrySafeFn: func(v any) any {\n\t\t\t\toriginal := v.([]int)\n\t\t\t\treturn append([]int{}, original...)\n\t\t\t},\n\t\t\tkey: \"slice\",\n\t\t\tvalidate: func(t *testing.T, value any) {\n\t\t\t\tslice := value.([]int)\n\t\t\t\t// Modify the returned slice\n\t\t\t\tslice[0] = 999\n\t\t\t\t// Verify original is unchanged\n\t\t\t\toriginalValue := tree.Get(\"slice\")\n\t\t\t\toriginal := originalValue.([]int)\n\t\t\t\tif original[0] != 1 {\n\t\t\t\t\tt.Error(\"Original slice was modified\")\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\t// Add more test cases for different makeEntrySafeFn implementations\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\troTree := Wrap(tree, tt.makeEntrySafeFn)\n\t\t\tvalue := roTree.Get(tt.key)\n\t\t\tif value == nil {\n\t\t\t\tt.Fatal(\"Key not found\")\n\t\t\t}\n\t\t\ttt.validate(t, value)\n\t\t})\n\t}\n}\n\nfunc TestNilMakeEntrySafeFn(t *testing.T) {\n\t// Create a tree with some test data\n\ttree := bptree.NewBPTree32()\n\toriginalValue := []int{1, 2, 3}\n\ttree.Set(\"test\", originalValue)\n\n\t// Create a ReadOnlyTree with nil makeEntrySafeFn\n\troTree := Wrap(tree, nil)\n\n\t// Test that we get back the original value\n\tvalue := roTree.Get(\"test\")\n\tif value == nil {\n\t\tt.Fatal(\"Key not found\")\n\t}\n\n\t// Verify it's the exact same slice (not a copy)\n\tretrievedSlice := value.([]int)\n\tif \u0026retrievedSlice[0] != \u0026originalValue[0] {\n\t\tt.Error(\"Expected to get back the original slice reference\")\n\t}\n\n\t// Test through iteration as well\n\troTree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tretrievedSlice := value.([]int)\n\t\tif \u0026retrievedSlice[0] != \u0026originalValue[0] {\n\t\t\tt.Error(\"Expected to get back the original slice reference in iteration\")\n\t\t}\n\t\treturn false\n\t})\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"3eBm2P6DQKxE2ehFsVgQgj6DfEpXx/NmGX0vhzb4NvZCiYTcz3+k5s0KxYjiHERNSUX/ciL8e8WWeb5kCDuzDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"list","path":"gno.land/p/nt/bptree/v0/list","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/bptree/v0/list\"\ngno = \"0.9\"\n"},{"name":"list.gno","body":"// Package list implements a dynamic list data structure backed by a B+ tree.\n// It provides O(log n) operations for most list operations while maintaining\n// order stability.\n//\n// The list supports various operations including append, get, set, delete,\n// range queries, and iteration. It can store values of any type.\n//\n// Example usage:\n//\n//\t// Create a new list and add elements\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\n//\t// Get and set elements\n//\tvalue, _ := l.Get(1)  // returns 2\n//\tl.Set(1, 42)      // updates index 1 to 42\n//\n//\t// Delete elements\n//\tl.Delete(0)       // removes first element\n//\n//\t// Iterate over elements\n//\tl.ForEach(func(index int, value any) bool {\n//\t    ufmt.Printf(\"index %d: %v\\n\", index, value)\n//\t    return false  // continue iteration\n//\t})\n//\t// Output:\n//\t// index 0: 42\n//\t// index 1: 3\n//\n//\t// Create a list using a variable declaration\n//\tvar l2 list.List\n//\tl2.Append(4, 5, 6)\n//\tprintln(l2.Len())  // Output: 3\npackage list\n\nimport (\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/bptree/v0/rotree\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// IList defines the interface for list operations\ntype IList interface {\n\tLen() int\n\tAppend(values ...any)\n\tGet(index int) (any, bool)\n\tSet(index int, value any) bool\n\tDelete(index int) (any, bool)\n\tSlice(startIndex, endIndex int) []any\n\tForEach(fn func(index int, value any) bool)\n\tClone() *List\n\tDeleteRange(startIndex, endIndex int) int\n}\n\n// Verify List implements IList interface\nvar _ IList = (*List)(nil)\n\n// List represents an ordered sequence of items backed by a B+ tree\ntype List struct {\n\ttree  bptree.BPTree\n\tidGen seqid.ID\n}\n\n// Len returns the number of elements in the list.\nfunc (l *List) Len() int {\n\treturn l.tree.Size()\n}\n\n// Append adds one or more values to the end of the list.\nfunc (l *List) Append(values ...any) {\n\tfor _, v := range values {\n\t\tl.tree.Set(l.idGen.Next().String(), v)\n\t}\n}\n\n// Get returns the value at the specified index and true if the index is valid.\n// Returns (nil, false) if index is out of bounds.\nfunc (l *List) Get(index int) (any, bool) {\n\tif index \u003c 0 || index \u003e= l.tree.Size() {\n\t\treturn nil, false\n\t}\n\t_, value := l.tree.GetByIndex(index)\n\treturn value, true\n}\n\n// Set updates or appends a value at the specified index.\n// Returns true if the operation was successful, false otherwise.\n// For empty lists, only index 0 is valid (append case).\nfunc (l *List) Set(index int, value any) bool {\n\tsize := l.tree.Size()\n\n\t// Handle empty list case - only allow index 0\n\tif size == 0 {\n\t\tif index == 0 {\n\t\t\tl.Append(value)\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tif index \u003c 0 || index \u003e size {\n\t\treturn false\n\t}\n\n\t// If setting at the end (append case)\n\tif index == size {\n\t\tl.Append(value)\n\t\treturn true\n\t}\n\n\t// Get the key at the specified index\n\tkey, _ := l.tree.GetByIndex(index)\n\tif key == \"\" {\n\t\treturn false\n\t}\n\n\t// Update the value at the existing key\n\tl.tree.Set(key, value)\n\treturn true\n}\n\n// Delete removes the element at the specified index.\n// Returns the deleted value and true if successful, nil and false otherwise.\nfunc (l *List) Delete(index int) (any, bool) {\n\tsize := l.tree.Size()\n\t// Always return nil, false for empty list\n\tif size == 0 {\n\t\treturn nil, false\n\t}\n\n\tif index \u003c 0 || index \u003e= size {\n\t\treturn nil, false\n\t}\n\n\tkey, value := l.tree.GetByIndex(index)\n\tif key == \"\" {\n\t\treturn nil, false\n\t}\n\n\tl.tree.Remove(key)\n\treturn value, true\n}\n\n// Slice returns a slice of values from startIndex (inclusive) to endIndex (exclusive).\n// Returns nil if the range is invalid.\nfunc (l *List) Slice(startIndex, endIndex int) []any {\n\tsize := l.tree.Size()\n\n\t// Normalize bounds\n\tif startIndex \u003c 0 {\n\t\tstartIndex = 0\n\t}\n\tif endIndex \u003e size {\n\t\tendIndex = size\n\t}\n\tif startIndex \u003e= endIndex {\n\t\treturn nil\n\t}\n\n\tcount := endIndex - startIndex\n\tresult := make([]any, count)\n\n\ti := 0\n\tl.tree.IterateByOffset(startIndex, count, func(_ string, value any) bool {\n\t\tresult[i] = value\n\t\ti++\n\t\treturn false\n\t})\n\treturn result\n}\n\n// ForEach iterates through all elements in the list.\nfunc (l *List) ForEach(fn func(index int, value any) bool) {\n\tif l.tree.Size() == 0 {\n\t\treturn\n\t}\n\n\tindex := 0\n\tl.tree.IterateByOffset(0, l.tree.Size(), func(_ string, value any) bool {\n\t\tresult := fn(index, value)\n\t\tindex++\n\t\treturn result\n\t})\n}\n\n// Clone creates a shallow copy of the list.\nfunc (l *List) Clone() *List {\n\tnewList := \u0026List{\n\t\ttree:  bptree.BPTree{},\n\t\tidGen: l.idGen,\n\t}\n\n\tsize := l.tree.Size()\n\tif size == 0 {\n\t\treturn newList\n\t}\n\n\tl.tree.IterateByOffset(0, size, func(_ string, value any) bool {\n\t\tnewList.Append(value)\n\t\treturn false\n\t})\n\n\treturn newList\n}\n\n// DeleteRange removes elements from startIndex (inclusive) to endIndex (exclusive).\n// Returns the number of elements deleted.\nfunc (l *List) DeleteRange(startIndex, endIndex int) int {\n\tsize := l.tree.Size()\n\n\t// Normalize bounds\n\tif startIndex \u003c 0 {\n\t\tstartIndex = 0\n\t}\n\tif endIndex \u003e size {\n\t\tendIndex = size\n\t}\n\tif startIndex \u003e= endIndex {\n\t\treturn 0\n\t}\n\n\t// Collect keys to delete\n\tkeysToDelete := make([]string, 0, endIndex-startIndex)\n\tl.tree.IterateByOffset(startIndex, endIndex-startIndex, func(key string, _ any) bool {\n\t\tkeysToDelete = append(keysToDelete, key)\n\t\treturn false\n\t})\n\n\t// Delete collected keys\n\tfor _, key := range keysToDelete {\n\t\tl.tree.Remove(key)\n\t}\n\n\treturn len(keysToDelete)\n}\n\n// Tree returns a read-only pointer to the underlying B+ tree.\nfunc (l *List) Tree() *rotree.ReadOnlyTree {\n\treturn rotree.Wrap(\u0026l.tree, nil)\n}\n"},{"name":"list_test.gno","body":"package list\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestList_Basic(t *testing.T) {\n\tvar l List\n\n\t// Test empty list\n\tif l.Len() != 0 {\n\t\tt.Errorf(\"new list should be empty, got len %d\", l.Len())\n\t}\n\n\t// Test append and length\n\tl.Append(1, 2, 3)\n\tif l.Len() != 3 {\n\t\tt.Errorf(\"expected len 3, got %d\", l.Len())\n\t}\n\n\t// Test get\n\tif v, _ := l.Get(0); v != 1 {\n\t\tt.Errorf(\"expected 1 at index 0, got %v\", v)\n\t}\n\tif v, _ := l.Get(1); v != 2 {\n\t\tt.Errorf(\"expected 2 at index 1, got %v\", v)\n\t}\n\tif v, _ := l.Get(2); v != 3 {\n\t\tt.Errorf(\"expected 3 at index 2, got %v\", v)\n\t}\n\n\t// Test out of bounds\n\tif _, ok := l.Get(-1); ok {\n\t\tt.Error(\"expected ok=false for negative index\")\n\t}\n\tif _, ok := l.Get(3); ok {\n\t\tt.Error(\"expected ok=false for out of bounds index\")\n\t}\n}\n\nfunc TestList_Set(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\t// Test valid set within bounds\n\tif ok := l.Set(1, 42); !ok {\n\t\tt.Error(\"Set should return true for valid index\")\n\t}\n\tif v, _ := l.Get(1); v != 42 {\n\t\tt.Errorf(\"expected 42 after Set, got %v\", v)\n\t}\n\n\t// Test set at size (append)\n\tif ok := l.Set(3, 4); !ok {\n\t\tt.Error(\"Set should return true when appending at size\")\n\t}\n\tif v, _ := l.Get(3); v != 4 {\n\t\tt.Errorf(\"expected 4 after Set at size, got %v\", v)\n\t}\n\n\t// Test invalid sets\n\tif ok := l.Set(-1, 10); ok {\n\t\tt.Error(\"Set should return false for negative index\")\n\t}\n\tif ok := l.Set(5, 10); ok {\n\t\tt.Error(\"Set should return false for index \u003e size\")\n\t}\n\n\t// Verify list state hasn't changed after invalid operations\n\texpected := []any{1, 42, 3, 4}\n\tfor i, want := range expected {\n\t\tif got, _ := l.Get(i); got != want {\n\t\t\tt.Errorf(\"index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n}\n\nfunc TestList_Delete(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\t// Test valid delete\n\tif v, ok := l.Delete(1); !ok || v != 2 {\n\t\tt.Errorf(\"Delete(1) = %v, %v; want 2, true\", v, ok)\n\t}\n\tif l.Len() != 2 {\n\t\tt.Errorf(\"expected len 2 after delete, got %d\", l.Len())\n\t}\n\tif v, _ := l.Get(1); v != 3 {\n\t\tt.Errorf(\"expected 3 at index 1 after delete, got %v\", v)\n\t}\n\n\t// Test invalid delete\n\tif v, ok := l.Delete(-1); ok || v != nil {\n\t\tt.Errorf(\"Delete(-1) = %v, %v; want nil, false\", v, ok)\n\t}\n\tif v, ok := l.Delete(2); ok || v != nil {\n\t\tt.Errorf(\"Delete(2) = %v, %v; want nil, false\", v, ok)\n\t}\n}\n\nfunc TestList_Slice(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3, 4, 5)\n\n\t// Test valid ranges\n\tvalues := l.Slice(1, 4)\n\texpected := []any{2, 3, 4}\n\tif !sliceEqual(values, expected) {\n\t\tt.Errorf(\"Slice(1,4) = %v; want %v\", values, expected)\n\t}\n\n\t// Test edge cases\n\tif values := l.Slice(-1, 2); !sliceEqual(values, []any{1, 2}) {\n\t\tt.Errorf(\"Slice(-1,2) = %v; want [1 2]\", values)\n\t}\n\tif values := l.Slice(3, 10); !sliceEqual(values, []any{4, 5}) {\n\t\tt.Errorf(\"Slice(3,10) = %v; want [4 5]\", values)\n\t}\n\tif values := l.Slice(3, 2); values != nil {\n\t\tt.Errorf(\"Slice(3,2) = %v; want nil\", values)\n\t}\n}\n\nfunc TestList_ForEach(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\tsum := 0\n\tl.ForEach(func(index int, value any) bool {\n\t\tsum += value.(int)\n\t\treturn false\n\t})\n\n\tif sum != 6 {\n\t\tt.Errorf(\"ForEach sum = %d; want 6\", sum)\n\t}\n\n\t// Test early termination\n\tcount := 0\n\tl.ForEach(func(index int, value any) bool {\n\t\tcount++\n\t\treturn true // stop after first item\n\t})\n\n\tif count != 1 {\n\t\tt.Errorf(\"ForEach early termination count = %d; want 1\", count)\n\t}\n}\n\nfunc TestList_Clone(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\tclone := l.Clone()\n\n\t// Test same length\n\tif clone.Len() != l.Len() {\n\t\tt.Errorf(\"clone.Len() = %d; want %d\", clone.Len(), l.Len())\n\t}\n\n\t// Test same values\n\tfor i := 0; i \u003c l.Len(); i++ {\n\t\tcv, _ := clone.Get(i)\n\t\tlv, _ := l.Get(i)\n\t\tif cv != lv {\n\t\t\tt.Errorf(\"clone.Get(%d) = %v; want %v\", i, cv, lv)\n\t\t}\n\t}\n\n\t// Test independence\n\tl.Set(0, 42)\n\tcv, _ := clone.Get(0)\n\tlv, _ := l.Get(0)\n\tif cv == lv {\n\t\tt.Error(\"clone should be independent of original\")\n\t}\n\n\t// Test that appending to both after clone works independently.\n\tvar l2 List\n\tl2.Append(\"a\", \"b\", \"c\")\n\tclone2 := l2.Clone()\n\n\tl2.Append(\"d\", \"e\")\n\tclone2.Append(\"x\", \"y\")\n\n\tif l2.Len() != 5 {\n\t\tt.Errorf(\"original after append: Len() = %d; want 5\", l2.Len())\n\t}\n\tif clone2.Len() != 5 {\n\t\tt.Errorf(\"clone after append: Len() = %d; want 5\", clone2.Len())\n\t}\n\torigExpected := []any{\"a\", \"b\", \"c\", \"d\", \"e\"}\n\tcloneExpected := []any{\"a\", \"b\", \"c\", \"x\", \"y\"}\n\tfor i := 0; i \u003c 5; i++ {\n\t\tov, _ := l2.Get(i)\n\t\tif ov != origExpected[i] {\n\t\t\tt.Errorf(\"original.Get(%d) = %v; want %v\", i, ov, origExpected[i])\n\t\t}\n\t\tcv, _ := clone2.Get(i)\n\t\tif cv != cloneExpected[i] {\n\t\t\tt.Errorf(\"clone.Get(%d) = %v; want %v\", i, cv, cloneExpected[i])\n\t\t}\n\t}\n}\n\nfunc TestList_DeleteRange(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3, 4, 5)\n\n\t// Test valid range delete\n\tdeleted := l.DeleteRange(1, 4)\n\tif deleted != 3 {\n\t\tt.Errorf(\"DeleteRange(1,4) deleted %d elements; want 3\", deleted)\n\t}\n\tif l.Len() != 2 {\n\t\tt.Errorf(\"after DeleteRange(1,4) len = %d; want 2\", l.Len())\n\t}\n\texpected := []any{1, 5}\n\tfor i, want := range expected {\n\t\tif got, _ := l.Get(i); got != want {\n\t\t\tt.Errorf(\"after DeleteRange(1,4) index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n\n\t// Test edge cases\n\tl = List{}\n\tl.Append(1, 2, 3)\n\n\t// Delete with negative start\n\tif deleted := l.DeleteRange(-1, 2); deleted != 2 {\n\t\tt.Errorf(\"DeleteRange(-1,2) deleted %d elements; want 2\", deleted)\n\t}\n\n\t// Delete with end \u003e length\n\tl = List{}\n\tl.Append(1, 2, 3)\n\tif deleted := l.DeleteRange(1, 5); deleted != 2 {\n\t\tt.Errorf(\"DeleteRange(1,5) deleted %d elements; want 2\", deleted)\n\t}\n\n\t// Delete invalid range\n\tif deleted := l.DeleteRange(2, 1); deleted != 0 {\n\t\tt.Errorf(\"DeleteRange(2,1) deleted %d elements; want 0\", deleted)\n\t}\n\n\t// Delete empty range\n\tif deleted := l.DeleteRange(1, 1); deleted != 0 {\n\t\tt.Errorf(\"DeleteRange(1,1) deleted %d elements; want 0\", deleted)\n\t}\n}\n\nfunc TestList_Tree(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\trotree := l.Tree()\n\texpected := 3\n\n\t// Reverse iterate through the ReadOnlyTree\n\trotree.ReverseIterateByOffset(0, rotree.Size(), func(key string, value any) bool {\n\t\tintValue := value.(int)\n\t\tif intValue != expected {\n\t\t\tt.Errorf(\"ReadOnlyTree expected %d, got %d\", expected, intValue)\n\t\t}\n\t\texpected--\n\t\treturn false\n\t})\n}\n\nfunc TestList_EmptyOperations(t *testing.T) {\n\tvar l List\n\n\t// Operations on empty list\n\tif _, ok := l.Get(0); ok {\n\t\tt.Error(\"Get(0) on empty list should return ok=false\")\n\t}\n\n\t// Set should work at index 0 for empty list (append case)\n\tif ok := l.Set(0, 1); !ok {\n\t\tt.Error(\"Set(0,1) on empty list = false; want true\")\n\t}\n\tif v, _ := l.Get(0); v != 1 {\n\t\tt.Errorf(\"Get(0) after Set = %v; want 1\", v)\n\t}\n\n\tl = List{} // Reset to empty list\n\tif v, ok := l.Delete(0); ok || v != nil {\n\t\tt.Errorf(\"Delete(0) on empty list = %v, %v; want nil, false\", v, ok)\n\t}\n\tif values := l.Slice(0, 1); values != nil {\n\t\tt.Errorf(\"Range(0,1) on empty list = %v; want nil\", values)\n\t}\n}\n\nfunc TestList_DifferentTypes(t *testing.T) {\n\tvar l List\n\n\t// Test with different types\n\tl.Append(42, \"hello\", true, 3.14)\n\n\tv0, _ := l.Get(0)\n\tif v := v0.(int); v != 42 {\n\t\tt.Errorf(\"Get(0) = %v; want 42\", v)\n\t}\n\tv1, _ := l.Get(1)\n\tif v := v1.(string); v != \"hello\" {\n\t\tt.Errorf(\"Get(1) = %v; want 'hello'\", v)\n\t}\n\tv2, _ := l.Get(2)\n\tif v := v2.(bool); !v {\n\t\tt.Errorf(\"Get(2) = %v; want true\", v)\n\t}\n\tv3, _ := l.Get(3)\n\tif v := v3.(float64); v != 3.14 {\n\t\tt.Errorf(\"Get(3) = %v; want 3.14\", v)\n\t}\n}\n\nfunc TestList_LargeOperations(t *testing.T) {\n\tvar l List\n\n\t// Test with larger number of elements\n\tn := 1000\n\tfor i := 0; i \u003c n; i++ {\n\t\tl.Append(i)\n\t}\n\n\tif l.Len() != n {\n\t\tt.Errorf(\"Len() = %d; want %d\", l.Len(), n)\n\t}\n\n\t// Test range on large list\n\tvalues := l.Slice(n-3, n)\n\texpected := []any{n - 3, n - 2, n - 1}\n\tif !sliceEqual(values, expected) {\n\t\tt.Errorf(\"Range(%d,%d) = %v; want %v\", n-3, n, values, expected)\n\t}\n\n\t// Test large range deletion\n\tdeleted := l.DeleteRange(100, 900)\n\tif deleted != 800 {\n\t\tt.Errorf(\"DeleteRange(100,900) = %d; want 800\", deleted)\n\t}\n\tif l.Len() != 200 {\n\t\tt.Errorf(\"Len() after large delete = %d; want 200\", l.Len())\n\t}\n}\n\nfunc TestList_ChainedOperations(t *testing.T) {\n\tvar l List\n\n\t// Test sequence of operations\n\tl.Append(1, 2, 3)\n\tl.Delete(1)\n\tl.Append(4)\n\tl.Set(1, 5)\n\n\texpected := []any{1, 5, 4}\n\tfor i, want := range expected {\n\t\tif got, _ := l.Get(i); got != want {\n\t\t\tt.Errorf(\"index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n}\n\nfunc TestList_RangeEdgeCases(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3, 4, 5)\n\n\t// Test various edge cases for Range\n\tcases := []struct {\n\t\tstart, end int\n\t\twant       []any\n\t}{\n\t\t{-10, 2, []any{1, 2}},\n\t\t{3, 10, []any{4, 5}},\n\t\t{0, 0, nil},\n\t\t{5, 5, nil},\n\t\t{4, 3, nil},\n\t\t{-1, -1, nil},\n\t}\n\n\tfor _, tc := range cases {\n\t\tgot := l.Slice(tc.start, tc.end)\n\t\tif !sliceEqual(got, tc.want) {\n\t\t\tt.Errorf(\"Slice(%d,%d) = %v; want %v\", tc.start, tc.end, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestList_IndexConsistency(t *testing.T) {\n\tvar l List\n\n\t// Initial additions\n\tl.Append(1, 2, 3, 4, 5) // [1,2,3,4,5]\n\n\t// Delete from middle\n\tl.Delete(2) // [1,2,4,5]\n\n\t// Add more elements\n\tl.Append(6, 7) // [1,2,4,5,6,7]\n\n\t// Delete range from middle\n\tl.DeleteRange(1, 4) // [1,6,7]\n\n\t// Add more elements\n\tl.Append(8, 9, 10) // [1,6,7,8,9,10]\n\n\t// Verify sequence is continuous\n\texpected := []any{1, 6, 7, 8, 9, 10}\n\tfor i, want := range expected {\n\t\tif got, _ := l.Get(i); got != want {\n\t\t\tt.Errorf(\"index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n\n\t// Verify no extra elements exist\n\tif l.Len() != len(expected) {\n\t\tt.Errorf(\"length = %d; want %d\", l.Len(), len(expected))\n\t}\n\n\t// Verify all indices are accessible\n\tallValues := l.Slice(0, l.Len())\n\tif !sliceEqual(allValues, expected) {\n\t\tt.Errorf(\"Slice(0, Len()) = %v; want %v\", allValues, expected)\n\t}\n\n\t// Verify no gaps in iteration\n\tvar iteratedValues []any\n\tvar indices []int\n\tl.ForEach(func(index int, value any) bool {\n\t\titeratedValues = append(iteratedValues, value)\n\t\tindices = append(indices, index)\n\t\treturn false\n\t})\n\n\t// Check values from iteration\n\tif !sliceEqual(iteratedValues, expected) {\n\t\tt.Errorf(\"ForEach values = %v; want %v\", iteratedValues, expected)\n\t}\n\n\t// Check indices are sequential\n\tfor i, idx := range indices {\n\t\tif idx != i {\n\t\t\tt.Errorf(\"ForEach index %d = %d; want %d\", i, idx, i)\n\t\t}\n\t}\n}\n\nfunc TestList_RecursiveSafety(t *testing.T) {\n\t// Create a new list\n\tl := \u0026List{}\n\n\t// Add some initial values\n\tl.Append(\"id1\")\n\tl.Append(\"id2\")\n\tl.Append(\"id3\")\n\n\t// Test deep list traversal\n\tfound := false\n\tl.ForEach(func(i int, v any) bool {\n\t\tif str, ok := v.(string); ok {\n\t\t\tif str == \"id2\" {\n\t\t\t\tfound = true\n\t\t\t\treturn true // stop iteration\n\t\t\t}\n\t\t}\n\t\treturn false // continue iteration\n\t})\n\n\tif !found {\n\t\tt.Error(\"Failed to find expected value in list\")\n\t}\n\n\tshort := testing.Short()\n\n\t// Test recursive safety by performing multiple operations\n\tfor i := 0; i \u003c 1000; i++ {\n\t\t// Add new value\n\t\tl.Append(ufmt.Sprintf(\"id%d\", i+4))\n\n\t\tif !short {\n\t\t\t// Search for a value\n\t\t\tvar lastFound bool\n\t\t\tl.ForEach(func(j int, v any) bool {\n\t\t\t\tif str, ok := v.(string); ok {\n\t\t\t\t\tif str == ufmt.Sprintf(\"id%d\", i+3) {\n\t\t\t\t\t\tlastFound = true\n\t\t\t\t\t\treturn true\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !lastFound {\n\t\t\t\tt.Errorf(\"Failed to find value id%d after insertion\", i+3)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Verify final length\n\texpectedLen := 1003 // 3 initial + 1000 added\n\tif l.Len() != expectedLen {\n\t\tt.Errorf(\"Expected length %d, got %d\", expectedLen, l.Len())\n\t}\n\n\tif short {\n\t\tt.Skip(\"skipping extended recursive safety test in short mode\")\n\t}\n}\n\nfunc TestList_NilValueDistinguishable(t *testing.T) {\n\tvar l List\n\tl.Append(nil)\n\tl.Append(\"hello\")\n\n\t// Stored nil is distinguishable from out-of-bounds.\n\tv, ok := l.Get(0)\n\tif !ok {\n\t\tt.Error(\"Get(0) should return ok=true for stored nil\")\n\t}\n\tif v != nil {\n\t\tt.Errorf(\"Get(0) should return nil value, got %v\", v)\n\t}\n\n\t// Out of bounds returns false.\n\t_, ok = l.Get(5)\n\tif ok {\n\t\tt.Error(\"Get(5) should return ok=false for out of bounds\")\n\t}\n\n\t// Normal value still works.\n\tv, ok = l.Get(1)\n\tif !ok || v != \"hello\" {\n\t\tt.Errorf(\"Get(1) = (%v, %v); want (hello, true)\", v, ok)\n\t}\n}\n\n// Helper function to compare slices\nfunc sliceEqual(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Kjj10Ujvisfzi/nBCS9GZDC14gimHJ9FIFE3Og2AXbn8l6RFAS3HCRxMv7GYd+2EnJsMXJezj+QewlWQ3Y4UCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l","package":{"name":"commondao","path":"gno.land/p/nt/commondao/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n\n# CommonDAO Package\n\nCommonDAO is a general-purpose package that provides support to implement\ncustom Decentralized Autonomous Organizations (DAO) on Gno.land.\n\nIt offers a minimal and flexible framework for building DAOs, with customizable\noptions that adapt across multiple use cases.\n\n## Core Types\n\nPackage contains some core types which are important in any DAO implementation,\nthese are **CommonDAO**, **ProposalDefinition**, **Proposal** and **Vote**.\n\n### 1. CommonDAO Type\n\nCommonDAO type is the main type used to define DAOs, allowing standalone DAO\ncreation or hierarchical tree based ones.\n\nDuring creation, it accepts many optional arguments some of which are handy\ndepending on the DAO type. For example, standalone DAOs might use IDs, a name\nand description to uniquely identify individual DAOs; Hierarchical ones might\nchoose to use slugs instead of IDs, or even a mix of both.\n\n#### DAO Creation Examples\n\nStandalone DAO:\n\n```go\nimport \"gno.land/p/nt/commondao/v0\"\n\ndao := commondao.New(\n    commondao.WithID(1),\n    commondao.WithName(\"MyDAO\"),\n    commondao.WithDescription(\"An example DAO\"),\n    commondao.WithMember(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n    commondao.WithMember(\"g1hy6zry03hg5d8le9s2w4fxme6236hkgd928dun\"),\n)\n```\n\nHierarchical DAO:\n\n```go\nimport \"gno.land/p/nt/commondao/v0\"\n\ndao := commondao.New(\n    commondao.WithSlug(\"parent\"),\n    commondao.WithName(\"ParentDAO\"),\n    commondao.WithMember(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n)\n\nsubDAO := commondao.New(\n    commondao.WithSlug(\"child\"),\n    commondao.WithName(\"ChildDAO\"),\n    commondao.WithParent(dao),\n)\n```\n\n### 2. ProposalDefinition Type\n\nProposal definitions are the way proposal types are implemented in `commondao`.\nDefinitions are required when creating a new proposal because they define the\nbehavior of the proposal.\n\nGenerally speaking, proposals can be divided in two types, one are the\n*general* (a.k.a. *text proposals*), and the other are the *executable* ones.\nThe difference is that *executable* ones modify the blockchain state when they\nare executed after they have been approved, while *general* ones don't, they\nare usually used to signal or measure sentiment, for example regarding a\nrelevant issue.\n\nCreating a new proposal type requires implementing the following interface:\n\n```go\ntype ProposalDefinition interface {\n    // Title returns proposal title.\n    Title() string\n\n    // Body returns proposal's body.\n    // It usually contains description or values that are specific to\n    // the proposal, like a description of the proposal's motivation\n    // or the list of values that would be applied when the proposal\n    // is approved.\n    Body() string\n\n    // VotingPeriod returns the period where votes are allowed after\n    // proposal creation. It's used to calculate the voting deadline\n    // from the proposal's creationd date.\n    VotingPeriod() time.Duration\n\n    // Tally counts the number of votes and verifies if proposal passes.\n    // It receives a voting context containing a readonly record with the votes\n    // that has been submitted for the proposal and also the list of DAO members.\n    Tally(VotingContext) (passes bool, _ error)\n}\n```\n\nThis minimal interface is the one required for *general proposal types*. Here\nthe most important method is the `Tally()` one. It's used to check whether a\nproposal passes or not.\n\nWithin `Tally()` votes can be counted using different rules depending on the\nproposal type, some proposal types might decide if there is consensus by using\nsuper majority while others might decide using plurality for example, or even\njust counting that a minimum number of certain positive votes have been\nsubmitted to approve a proposal.\n\nCommonDAO provides a couple of helpers for this, to cover some cases:\n- `SelectChoiceByAbsoluteMajority()`\n- `SelectChoiceBySuperMajority()` (using a 2/3s threshold)\n- `SelectChoiceByPlurality()`\n\n#### 2.1. Executable Proposals\n\nProposal definitions have optional features that could be implemented to extend\nthe proposal type behaviour. One of those is required to enable execution\nsupport.\n\nA proposal can be executable implementing the **Executable** interface as part\nof the new proposal definition:\n\n```go\ntype Executable interface {\n    // Executor returns a function to execute the proposal.\n    Executor() func(realm) error\n}\n```\n\nThe crossing function returned by the `Executor()` method is where the realm\nchanges are made once the proposal is executed.\n\nOther features can be enabled by implementing the **Validable** interface and\nthe **CustomizableVoteChoices** one, as a way to separate pre-execution\nvalidation and to support proposal voting choices different than the default\nones (YES, NO and ABSTAIN).\n\n### 3. Proposal Type\n\nProposals are key for governance, they are the main mechanic that allows DAO\nmembers to engage on governance.\n\nThey are usually not created directly but though **CommonDAO** instances, by\ncalling the `CommonDAO.Propose()` or `CommonDAO.MustPropose()` methods. Though,\nalternatively, proposals could be added to CommonDAO's active proposals storage\nusing `CommonDAO.ActiveProposals().Add()`.\n\n```go\nimport (\n    \"gno.land/p/nt/commondao/v0\"\n    \"gno.land/r/example/mydao\"\n)\n\ndao := commondao.New()\ncreator := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\npropDef := mydao.NewGeneralProposalDefinition(\"Title\", \"Description\")\nproposal := dao.MustPropose(creator, propDef)\n```\n\n#### 3.1. Voting on Proposals\n\nThe preferred way to submit a vote, once a proposal is created, is by calling\nthe `CommonDAO.Vote()` method because it performs sanity checks before a vote\nis considered valid; Alternatively votes can be directly added without sanity\nchecks to the proposal's voting record by calling\n`Proposal.VotingRecord().AddVote()`.\n\n#### 3.2. Voting Record\n\nEach proposal keeps track of their submitted votes within an internal voting\nrecord. CommonDAO package defines it as a **VotingRecord** type.\n\nThe voting record of a proposal can be getted by calling its\n`Proposal.VotingRecord()` method.\n\nRight now proposals have a single voting record but the plan is to support\nmultiple voting records per proposal as an optional feature, which could be\nused in cases where a proposal must track votes in multiple independent\nrecords, for example in cases where a proposal could be promoted to a different\nDAO with a different set of members.\n\n#### 4. Vote Type\n\nVote type defines the structure to store information for individual proposal\nvotes. Apart from the normally mandatory `Address` and voting `Choice` fields,\nthere are two optional fields that can be useful in different use cases; These\nfields are `Reason` which can store a string with the reason for the vote, and\n`Context` which can be used to store generic values related to the vote, for\nexample vote weight information.\n\nIt's *very important* to be careful when using the `Context` field, in case\nreferences/pointers are assigned to it because they could potentially be\naccessed anywhere, which could lead to unwanted indirect modifications.\n\nVote type is defined as:\n\n```go\ntype Vote struct {\n    // Address is the address of the user that this vote belons to.\n    Address address\n\n    // Choice contains the voted choice.\n    Choice VoteChoice\n\n    // Reason contains an optional reason for the vote.\n    Reason string\n\n    // Context can store any custom voting values related to the vote.\n    Context any\n}\n```\n\n## Secondary Types\n\nThere are other types which can be handy for some implementations which might\nrequire to store DAO members or proposals in a custom location, or that might\nneed member grouping support.\n\n### 1. MemberStorage and ProposalStorage Types\n\nThese two types allows storing and iterating DAO members and proposals. They\nsupport DAO implementations that might require storing either members or\nproposals in an external realm other than the DAO realm.\n\nCommonDAO package provides implementations that use AVL trees under the hood\nfor storage and lookup.\n\nCustom implementations are supported though the **MemberStorage** and\n**ProposalStorage** interfaces:\n\n```go\ntype MemberStorage interface {\n\t// Size returns the number of members in the storage.\n\tSize() int\n\n\t// Has checks if a member exists in the storage.\n\tHas(address) bool\n\n\t// Add adds a member to the storage.\n\tAdd(address) bool\n\n\t// Remove removes a member from the storage.\n\tRemove(address) bool\n\n\t// Grouping returns member groups when supported.\n\tGrouping() MemberGrouping\n\n\t// IterateByOffset iterates members starting at the given offset.\n\tIterateByOffset(offset, count int, fn func(address) bool)\n}\n\ntype ProposalStorage interface {\n\t// Has checks if a proposal exists.\n\tHas(id uint64) bool\n\n\t// Get returns a proposal or nil when proposal doesn't exist.\n\tGet(id uint64) *Proposal\n\n\t// Add adds a proposal to the storage.\n\tAdd(*Proposal)\n\n\t// Remove removes a proposal from the storage.\n\tRemove(id uint64)\n\n\t// Size returns the number of proposals that the storage contains.\n\tSize() int\n\n\t// Iterate iterates proposals.\n\tIterate(offset, count int, reverse bool, fn func(*Proposal) bool) bool\n}\n```\n\n### 2. MemberGrouping and MemberGroup Types\n\nMembers grouping is an optional feature that provides support for DAO members\ngrouping.\n\nGrouping can be useful for DAOs that require grouping users by roles or tiers\nfor example.\n\nThe **MemberGrouping** type is a collection of member groups, while the\n**MemberGroup** is a group of members with metadata.\n\n#### Grouping by Role Example\n\n```go\nimport \"gno.land/p/nt/commondao/v0\"\n\nstorage := commondao.NewMemberStorageWithGrouping()\n\n// Add a member that doesn't belong to any group\nstorage.Add(\"g1...a\")\n\n// Create a member group for owners\nowners, err := storage.Grouping().Add(\"owners\")\nif err != nil {\n  panic(err)\n}\n\n// Add a member to the owners group\nowners.Members().Add(\"g1...b\")\n\n// Add voting power to owners group metadata\nowners.SetMeta(3)\n\n// Create a member group for moderators\nmoderators, err := storage.Grouping().Add(\"moderators\")\nif err != nil {\n  panic(err)\n}\n\n// Add voting power to moderators group metadata\nmoderators.SetMeta(1)\n\n// Add members to the moderators group\nmoderators.Members().Add(\"g1...c\")\nmoderators.Members().Add(\"g1...d\")\n```\n"},{"name":"commondao.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0/list\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// PathSeparator is the separator character used in DAO paths.\nconst PathSeparator = \"/\"\n\nvar (\n\tErrExecutionNotAllowed  = errors.New(\"proposal must pass before execution\")\n\tErrInvalidVoteChoice    = errors.New(\"invalid vote choice\")\n\tErrNotMember            = errors.New(\"account is not a member of the DAO\")\n\tErrOverflow             = errors.New(\"next ID overflows uint64\")\n\tErrProposalNotFound     = errors.New(\"proposal not found\")\n\tErrVotingDeadlineNotMet = errors.New(\"voting deadline not met\")\n\tErrVotingDeadlinePassed = errors.New(\"voting deadline has passed\")\n\tErrWithdrawalNotAllowed = errors.New(\"withdrawal not allowed for proposals with votes\")\n)\n\n// CommonDAO defines a DAO.\ntype CommonDAO struct {\n\tid                         uint64\n\tslug                       string\n\tname                       string\n\tdescription                string\n\tparent                     *CommonDAO\n\tchildren                   list.IList\n\tmembers                    MemberStorage\n\tgenID                      seqid.ID\n\tactiveProposals            ProposalStorage\n\tfinishedProposals          ProposalStorage\n\tdeleted                    bool // Soft delete\n\tdisableVotingDeadlineCheck bool\n}\n\n// New creates a new common DAO.\nfunc New(options ...Option) *CommonDAO {\n\tdao := \u0026CommonDAO{\n\t\tchildren:          \u0026list.List{},\n\t\tmembers:           NewMemberStorage(),\n\t\tactiveProposals:   NewProposalStorage(),\n\t\tfinishedProposals: NewProposalStorage(),\n\t}\n\tfor _, apply := range options {\n\t\tapply(dao)\n\t}\n\treturn dao\n}\n\n// ID returns DAO's unique identifier.\nfunc (dao CommonDAO) ID() uint64 {\n\treturn dao.id\n}\n\n// Slug returns DAO's URL slug.\nfunc (dao CommonDAO) Slug() string {\n\treturn dao.slug\n}\n\n// Name returns DAO's name.\nfunc (dao CommonDAO) Name() string {\n\treturn dao.name\n}\n\n// Description returns DAO's description.\nfunc (dao CommonDAO) Description() string {\n\treturn dao.description\n}\n\n// Path returns the full path to the DAO.\n// Paths are normally used when working with hierarchical\n// DAOs and is created by concatenating DAO slugs.\nfunc (dao CommonDAO) Path() string {\n\t// NOTE: Path could be a value but there might be use cases where dynamic path is useful (?)\n\tparent := dao.Parent()\n\tif parent != nil {\n\t\tprefix := parent.Path()\n\t\tif prefix != \"\" {\n\t\t\treturn prefix + PathSeparator + dao.slug\n\t\t}\n\t}\n\treturn dao.slug\n}\n\n// Parent returns the parent DAO.\n// Null can be returned when DAO has no parent assigned.\nfunc (dao CommonDAO) Parent() *CommonDAO {\n\treturn dao.parent\n}\n\n// Children returns a list with the direct DAO children.\n// Each item in the list is a reference to a CommonDAO instance.\nfunc (dao CommonDAO) Children() list.IList {\n\treturn dao.children\n}\n\n// TopParent returns the topmost parent DAO.\n// The top parent is the root of the DAO tree.\nfunc (dao *CommonDAO) TopParent() *CommonDAO {\n\tparent := dao.Parent()\n\tif parent != nil {\n\t\treturn parent.TopParent()\n\t}\n\treturn dao\n}\n\n// Members returns the list of DAO members.\nfunc (dao CommonDAO) Members() MemberStorage {\n\treturn dao.members\n}\n\n// ActiveProposals returns active DAO proposals.\nfunc (dao CommonDAO) ActiveProposals() ProposalStorage {\n\treturn dao.activeProposals\n}\n\n// FinishedProposalsi returns finished DAO proposals.\nfunc (dao CommonDAO) FinishedProposals() ProposalStorage {\n\treturn dao.finishedProposals\n}\n\n// IsDeleted returns true when DAO has been soft deleted.\nfunc (dao CommonDAO) IsDeleted() bool {\n\treturn dao.deleted\n}\n\n// SetDeleted changes DAO's soft delete flag.\nfunc (dao *CommonDAO) SetDeleted(deleted bool) {\n\tdao.deleted = deleted\n}\n\n// Propose creates a new DAO proposal.\nfunc (dao *CommonDAO) Propose(creator address, d ProposalDefinition) (*Proposal, error) {\n\tid, ok := dao.genID.TryNext()\n\tif !ok {\n\t\treturn nil, ErrOverflow\n\t}\n\n\tp, err := NewProposal(uint64(id), creator, d)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tdao.activeProposals.Add(p)\n\treturn p, nil\n}\n\n// MustPropose creates a new DAO proposal or panics on error.\nfunc (dao *CommonDAO) MustPropose(creator address, d ProposalDefinition) *Proposal {\n\tp, err := dao.Propose(creator, d)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn p\n}\n\n// GetProposal returns a proposal or nil when proposal is not found.\nfunc (dao CommonDAO) GetProposal(proposalID uint64) *Proposal {\n\tp := dao.activeProposals.Get(proposalID)\n\tif p != nil {\n\t\treturn p\n\t}\n\treturn dao.finishedProposals.Get(proposalID)\n}\n\n// Withdraw withdraws a proposal that has no votes.\n// Only proposals without votes can be withdrawn, and once\n// withdrawn they are considered finished.\nfunc (dao *CommonDAO) Withdraw(proposalID uint64) error {\n\tp := dao.activeProposals.Get(proposalID)\n\tif p == nil {\n\t\treturn ErrProposalNotFound\n\t}\n\n\tif p.VotingRecord().Size() \u003e 0 {\n\t\treturn ErrWithdrawalNotAllowed\n\t}\n\n\tp.status = StatusWithdrawn\n\tdao.activeProposals.Remove(p.id)\n\tdao.finishedProposals.Add(p)\n\treturn nil\n}\n\n// Vote submits a new vote for a proposal.\n//\n// By default votes are only allowed to members of the DAO when the proposal is active,\n// and within the voting period. No votes are allowed once the voting deadline passes.\n// DAO deadline checks can optionally be disabled using the `DisableVotingDeadlineCheck` option.\nfunc (dao *CommonDAO) Vote(member address, proposalID uint64, c VoteChoice, reason string) error {\n\tif !dao.Members().Has(member) {\n\t\treturn ErrNotMember\n\t}\n\n\tp := dao.activeProposals.Get(proposalID)\n\tif p == nil {\n\t\treturn ErrProposalNotFound\n\t}\n\n\tif !dao.disableVotingDeadlineCheck \u0026\u0026 p.HasVotingDeadlinePassed() {\n\t\treturn ErrVotingDeadlinePassed\n\t}\n\n\tif !p.IsVoteChoiceValid(c) {\n\t\treturn ErrInvalidVoteChoice\n\t}\n\n\tp.record.AddVote(Vote{\n\t\tAddress: member,\n\t\tChoice:  c,\n\t\tReason:  reason,\n\t})\n\treturn nil\n}\n\n// Execute executes a proposal.\n//\n// By default active proposals can only be executed after their voting deadline passes.\n// DAO deadline checks can optionally be disabled using the `DisableVotingDeadlineCheck` option.\n//\n// rlm is the realm authority threaded into the executor via cross(rlm).\n// Execute itself is not a crossing function (rlm sits in a non-first\n// parameter slot) because /p/ production code cannot declare crossing\n// functions — callers pass `cur` directly as the rlm argument.\nfunc (dao *CommonDAO) Execute(proposalID uint64, rlm realm) error {\n\tp := dao.activeProposals.Get(proposalID)\n\tif p == nil {\n\t\treturn ErrProposalNotFound\n\t}\n\n\t// Proposal must be active or have passed to be executed\n\tif p.status != StatusActive \u0026\u0026 p.status != StatusPassed {\n\t\treturn ErrExecutionNotAllowed\n\t}\n\n\t// Execution must be done after voting deadline\n\tif !dao.disableVotingDeadlineCheck \u0026\u0026 !p.HasVotingDeadlinePassed() {\n\t\treturn ErrVotingDeadlineNotMet\n\t}\n\n\t// IMPORTANT, from this point on, any error is going to result\n\t// in a proposal failure and execute will succeed.\n\n\t// Validate proposal before execution\n\terr := p.Validate()\n\n\t// Tally votes and update proposal status to \"passed\" or \"rejected\"\n\tif err == nil {\n\t\terr = p.Tally(dao.Members())\n\t\tif err == nil \u0026\u0026 p.Status() == StatusRejected {\n\t\t\t// Don't try to execute proposal if it's been rejected\n\t\t\treturn nil\n\t\t}\n\t}\n\n\t// Execute proposal only if it's executable\n\tif err == nil {\n\t\tif e, ok := p.Definition().(Executable); ok {\n\t\t\tif fn := e.Executor(); fn != nil {\n\t\t\t\terr = fn(cross(rlm))\n\t\t\t}\n\t\t}\n\t}\n\n\t// Proposal fails if there is any error during validation and execution process\n\tif err != nil {\n\t\tp.status = StatusFailed\n\t\tp.statusReason = err.Error()\n\t} else {\n\t\tp.status = StatusExecuted\n\t}\n\n\t// Whichever the outcome of the validation, tallying\n\t// and execution consider the proposal finished.\n\tdao.activeProposals.Remove(p.id)\n\tdao.finishedProposals.Add(p)\n\treturn nil\n}\n"},{"name":"commondao_options.gno","body":"package commondao\n\n// Option configures the CommonDAO.\ntype Option func(*CommonDAO)\n\n// WithID assigns a unique identifier to the DAO.\nfunc WithID(id uint64) Option {\n\treturn func(dao *CommonDAO) {\n\t\tdao.id = id\n\t}\n}\n\n// WithName assigns a name to the DAO.\nfunc WithName(name string) Option {\n\treturn func(dao *CommonDAO) {\n\t\tdao.name = name\n\t}\n}\n\n// WithDescription assigns a description to the DAO.\nfunc WithDescription(description string) Option {\n\treturn func(dao *CommonDAO) {\n\t\tdao.description = description\n\t}\n}\n\n// WithSlug assigns a URL slug to the DAO.\nfunc WithSlug(slug string) Option {\n\treturn func(dao *CommonDAO) {\n\t\tdao.slug = slug\n\t}\n}\n\n// WithParent assigns a parent DAO.\nfunc WithParent(p *CommonDAO) Option {\n\treturn func(dao *CommonDAO) {\n\t\tdao.parent = p\n\t}\n}\n\n// WithChildren assigns one or more direct child SubDAOs to the DAO.\nfunc WithChildren(children ...*CommonDAO) Option {\n\treturn func(dao *CommonDAO) {\n\t\tfor _, subDAO := range children {\n\t\t\tdao.children.Append(subDAO)\n\t\t}\n\t}\n}\n\n// WithMember assigns a member to the DAO.\nfunc WithMember(addr address) Option {\n\treturn func(dao *CommonDAO) {\n\t\tdao.members.Add(addr)\n\t}\n}\n\n// WithMemberStorage assigns a custom member storage to the DAO.\n// An empty member storage is used by default if the specified storage is nil.\nfunc WithMemberStorage(s MemberStorage) Option {\n\treturn func(dao *CommonDAO) {\n\t\tif s == nil {\n\t\t\ts = NewMemberStorage()\n\t\t}\n\t\tdao.members = s\n\t}\n}\n\n// WithActiveProposalStorage assigns a custom storage for active proposals.\n// A default empty proposal storage is used when the custopm storage is nil.\n// Custom storage implementations can be used to store proposals in a different location.\nfunc WithActiveProposalStorage(s ProposalStorage) Option {\n\treturn func(dao *CommonDAO) {\n\t\tif s == nil {\n\t\t\ts = NewProposalStorage()\n\t\t}\n\t\tdao.activeProposals = s\n\t}\n}\n\n// WithFinishedProposalStorage assigns a custom storage for finished proposals.\n// A default empty proposal storage is used when the custopm storage is nil.\n// Custom storage implementations can be used to store proposals in a different location.\nfunc WithFinishedProposalStorage(s ProposalStorage) Option {\n\treturn func(dao *CommonDAO) {\n\t\tif s == nil {\n\t\t\ts = NewProposalStorage()\n\t\t}\n\t\tdao.finishedProposals = s\n\t}\n}\n\n// DisableVotingDeadlineCheck disables voting deadline check when voting or executing proposals.\n// By default CommonDAO checks that the proposal voting deadline has not been met when a new vote\n// is submitted, before registering the vote, and on proposal execution it also checks that voting\n// deadline has been met before executing a proposal.\n//\n// Disabling these checks can be useful in different use cases, moving the responsibility to check\n// the deadline to the commondao package caller. One example where this could be useful would be\n// in case where 100% or a required number of members of a DAO vote on a proposal and reach consensus\n// before the deadline is met, otherwise proposal would have to wait until deadline to be executed.\nfunc DisableVotingDeadlineCheck() Option {\n\treturn func(dao *CommonDAO) {\n\t\tdao.disableVotingDeadlineCheck = true\n\t}\n}\n"},{"name":"commondao_test.gno","body":"package commondao_test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc TestNew(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tparent  *commondao.CommonDAO\n\t\tmembers []address\n\t}{\n\t\t{\n\t\t\tname:    \"with parent\",\n\t\t\tparent:  commondao.New(),\n\t\t\tmembers: []address{\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t},\n\t\t{\n\t\t\tname:    \"without parent\",\n\t\t\tmembers: []address{\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple members\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"no members\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tmembersCount := len(tc.members)\n\t\t\toptions := []commondao.Option{commondao.WithParent(tc.parent)}\n\t\t\tfor _, m := range tc.members {\n\t\t\t\toptions = append(options, commondao.WithMember(m))\n\t\t\t}\n\n\t\t\tdao := commondao.New(options...)\n\n\t\t\tif tc.parent == nil {\n\t\t\t\tuassert.Equal(t, nil, dao.Parent())\n\t\t\t} else {\n\t\t\t\tuassert.NotEqual(t, nil, dao.Parent())\n\t\t\t}\n\n\t\t\tuassert.False(t, dao.IsDeleted(), \"expect DAO not to be soft deleted by default\")\n\t\t\turequire.Equal(t, membersCount, dao.Members().Size(), \"dao members\")\n\n\t\t\tvar i int\n\t\t\tdao.Members().IterateByOffset(0, membersCount, func(addr address) bool {\n\t\t\t\tuassert.Equal(t, tc.members[i], addr)\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestCommonDAOMembersAdd(t *testing.T) {\n\tmember := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tdao := commondao.New(commondao.WithMember(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\"))\n\n\tadded := dao.Members().Add(member)\n\turequire.True(t, added)\n\n\tuassert.Equal(t, 2, dao.Members().Size())\n\tuassert.True(t, dao.Members().Has(member))\n\n\tadded = dao.Members().Add(member)\n\turequire.False(t, added)\n}\n\nfunc TestCommonDAOMembersRemove(t *testing.T) {\n\tmember := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tdao := commondao.New(commondao.WithMember(member))\n\n\tremoved := dao.Members().Remove(member)\n\turequire.True(t, removed)\n\n\tremoved = dao.Members().Remove(member)\n\turequire.False(t, removed)\n}\n\nfunc TestCommonDAOMembersHas(t *testing.T) {\n\tcases := []struct {\n\t\tname   string\n\t\tmember address\n\t\tdao    *commondao.CommonDAO\n\t\twant   bool\n\t}{\n\t\t{\n\t\t\tname:   \"member\",\n\t\t\tmember: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tdao:    commondao.New(commondao.WithMember(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")),\n\t\t\twant:   true,\n\t\t},\n\t\t{\n\t\t\tname:   \"not a dao member\",\n\t\t\tmember: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tdao:    commondao.New(commondao.WithMember(\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\")),\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tgot := tc.dao.Members().Has(tc.member)\n\t\t\tuassert.Equal(t, got, tc.want)\n\t\t})\n\t}\n}\n\nfunc TestCommonDAOPropose(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tsetup   func() *commondao.CommonDAO\n\t\tcreator address\n\t\tdef     commondao.ProposalDefinition\n\t\terr     error\n\t}{\n\t\t{\n\t\t\tname:    \"success\",\n\t\t\tsetup:   func() *commondao.CommonDAO { return commondao.New() },\n\t\t\tcreator: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tdef:     testPropDef{},\n\t\t},\n\t\t{\n\t\t\tname:  \"nil definition\",\n\t\t\tsetup: func() *commondao.CommonDAO { return commondao.New() },\n\t\t\terr:   commondao.ErrProposalDefinitionRequired,\n\t\t},\n\t\t{\n\t\t\tname:  \"invalid creator address\",\n\t\t\tsetup: func() *commondao.CommonDAO { return commondao.New() },\n\t\t\tdef:   testPropDef{},\n\t\t\terr:   commondao.ErrInvalidCreatorAddress,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdao := tc.setup()\n\n\t\t\tp, err := dao.Propose(tc.creator, tc.def)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err)\n\n\t\t\tfound := dao.ActiveProposals().Has(p.ID())\n\t\t\turequire.True(t, found, \"proposal not found\")\n\t\t\tuassert.Equal(t, p.Creator(), tc.creator)\n\t\t})\n\t}\n}\n\nfunc TestCommonDAOWithdraw(t *testing.T) {\n\tcases := []struct {\n\t\tname       string\n\t\tproposalID uint64\n\t\tsetup      func() *commondao.CommonDAO\n\t\terr        error\n\t}{\n\t\t{\n\t\t\tname:       \"success\",\n\t\t\tproposalID: 1,\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tmember := address(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{votingPeriod: time.Hour})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:       \"proposal not found\",\n\t\t\tproposalID: 404,\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\treturn commondao.New(commondao.WithMember(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\"))\n\t\t\t},\n\t\t\terr: commondao.ErrProposalNotFound,\n\t\t},\n\t\t{\n\t\t\tname:       \"withdrawal not allowed\",\n\t\t\tproposalID: 1,\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tmember := address(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tp, _ := dao.Propose(member, testPropDef{votingPeriod: time.Hour})\n\t\t\t\tdao.Vote(member, p.ID(), commondao.ChoiceYes, \"\")\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\terr: commondao.ErrWithdrawalNotAllowed,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdao := tc.setup()\n\n\t\t\terr := dao.Withdraw(tc.proposalID)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err)\n\t\t})\n\t}\n}\n\nfunc TestCommonDAOVote(t *testing.T) {\n\tcases := []struct {\n\t\tname       string\n\t\tsetup      func() *commondao.CommonDAO\n\t\tmember     address\n\t\tchoice     commondao.VoteChoice\n\t\tproposalID uint64\n\t\terr        error\n\t}{\n\t\t{\n\t\t\tname: \"success\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tmember := address(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{votingPeriod: time.Hour})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tmember:     \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tchoice:     commondao.ChoiceYes,\n\t\t\tproposalID: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"success with custom vote choice\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tmember := address(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{\n\t\t\t\t\tvotingPeriod: time.Hour,\n\t\t\t\t\tvoteChoices:  []commondao.VoteChoice{\"FOO\", \"BAR\"},\n\t\t\t\t})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tmember:     \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tchoice:     commondao.VoteChoice(\"BAR\"),\n\t\t\tproposalID: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"success with deadline check disabled\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tmember := address(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\t\t\t\tdao := commondao.New(\n\t\t\t\t\tcommondao.WithMember(member),\n\t\t\t\t\tcommondao.DisableVotingDeadlineCheck(),\n\t\t\t\t)\n\t\t\t\tdao.Propose(member, testPropDef{})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tmember:     \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tchoice:     commondao.ChoiceYes,\n\t\t\tproposalID: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"invalid vote choice\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tmember := address(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{votingPeriod: time.Hour})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tmember:     \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tchoice:     commondao.VoteChoice(\"invalid\"),\n\t\t\tproposalID: 1,\n\t\t\terr:        commondao.ErrInvalidVoteChoice,\n\t\t},\n\t\t{\n\t\t\tname:   \"not a member\",\n\t\t\tsetup:  func() *commondao.CommonDAO { return commondao.New() },\n\t\t\tmember: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tchoice: commondao.ChoiceAbstain,\n\t\t\terr:    commondao.ErrNotMember,\n\t\t},\n\t\t{\n\t\t\tname: \"proposal not found\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\treturn commondao.New(commondao.WithMember(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\"))\n\t\t\t},\n\t\t\tmember:     \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\tchoice:     commondao.ChoiceAbstain,\n\t\t\tproposalID: 42,\n\t\t\terr:        commondao.ErrProposalNotFound,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdao := tc.setup()\n\n\t\t\terr := dao.Vote(tc.member, tc.proposalID, tc.choice, \"\")\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err)\n\n\t\t\tp := dao.ActiveProposals().Get(tc.proposalID)\n\t\t\turequire.NotEqual(t, nil, p, \"proposal not found\")\n\n\t\t\trecord := p.VotingRecord()\n\t\t\tuassert.True(t, record.HasVoted(tc.member))\n\t\t\tuassert.Equal(t, record.VoteCount(tc.choice), 1)\n\t\t})\n\t}\n}\n\nfunc TestCommonDAOExecute(cur realm, t *testing.T) {\n\terrTest := errors.New(\"test\")\n\tmember := address(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\tcases := []struct {\n\t\tname         string\n\t\tsetup        func() *commondao.CommonDAO\n\t\tproposalID   uint64\n\t\tstatus       commondao.ProposalStatus\n\t\tstatusReason string\n\t\terr          error\n\t}{\n\t\t{\n\t\t\tname: \"success\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{tallyResult: true}) // Non crossing definition\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tstatus:     commondao.StatusExecuted,\n\t\t\tproposalID: 1,\n\t\t},\n\t\t{\n\t\t\tname:       \"proposal not found\",\n\t\t\tsetup:      func() *commondao.CommonDAO { return commondao.New() },\n\t\t\tproposalID: 1,\n\t\t\terr:        commondao.ErrProposalNotFound,\n\t\t},\n\t\t{\n\t\t\tname: \"execution not allowed\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tp, _ := dao.Propose(member, testPropDef{tallyResult: false})\n\t\t\t\tp.Tally(dao.Members())\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tproposalID: 1,\n\t\t\terr:        commondao.ErrExecutionNotAllowed,\n\t\t},\n\t\t{\n\t\t\tname: \"voting deadline not met\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{votingPeriod: time.Minute * 5})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tproposalID: 1,\n\t\t\terr:        commondao.ErrVotingDeadlineNotMet,\n\t\t},\n\t\t{\n\t\t\tname: \"validation error\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{\n\t\t\t\t\tvalidationErr: errTest,\n\t\t\t\t\ttallyResult:   true,\n\t\t\t\t})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tproposalID:   1,\n\t\t\tstatus:       commondao.StatusFailed,\n\t\t\tstatusReason: errTest.Error(),\n\t\t},\n\t\t{\n\t\t\tname: \"tally error\",\n\t\t\tsetup: func() *commondao.CommonDAO {\n\t\t\t\tdao := commondao.New(commondao.WithMember(member))\n\t\t\t\tdao.Propose(member, testPropDef{tallyErr: errTest})\n\t\t\t\treturn dao\n\t\t\t},\n\t\t\tproposalID:   1,\n\t\t\tstatus:       commondao.StatusFailed,\n\t\t\tstatusReason: errTest.Error(),\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdao := tc.setup()\n\n\t\t\terr := dao.Execute(tc.proposalID, cur)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.Error(t, err, \"expected an error\")\n\t\t\t\turequire.ErrorIs(t, err, tc.err, \"expect error to match\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\n\t\t\tfound := dao.ActiveProposals().Has(tc.proposalID)\n\t\t\turequire.False(t, found, \"proposal should not be active\")\n\n\t\t\tp := dao.FinishedProposals().Get(tc.proposalID)\n\t\t\turequire.NotEqual(t, nil, p, \"proposal must be found\")\n\t\t\tuassert.Equal(t, string(p.Status()), string(tc.status), \"status must match\")\n\t\t\tuassert.Equal(t, string(p.StatusReason()), string(tc.statusReason), \"status reason must match\")\n\t\t})\n\t}\n}\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package provides support to implement custom Decentralized Autonomous Organizations (DAO).\n// It aims to be minimal and flexible, allowing the implementation of multiple DAO use cases,\n// like standalone or hierarchical tree based DAOs.\npackage commondao\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/commondao/v0\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l\"\n"},{"name":"member_group.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\t\"strings\"\n)\n\n// MemberGroup defines an interface for a group of members.\ntype MemberGroup interface {\n\t// Name returns the name of the group.\n\tName() string\n\n\t// Members returns the members that belong to the group.\n\tMembers() MemberStorage\n\n\t// SetMeta sets any metadata relevant to the group.\n\t// Metadata can be used to store data which is specific to the group.\n\t// Usually can be used to store parameter values which would be useful\n\t// during proposal voting or tallying to resolve things like voting\n\t// weights or rights for example.\n\tSetMeta(any)\n\n\t// GetMeta returns the group metadata.\n\tGetMeta() any\n}\n\n// NewMemberGroup creates a new group of members.\nfunc NewMemberGroup(name string, members MemberStorage) (MemberGroup, error) {\n\tif members == nil {\n\t\treturn nil, errors.New(\"member storage is required\")\n\t}\n\n\tname = strings.TrimSpace(name)\n\tif name == \"\" {\n\t\treturn nil, errors.New(\"member group name is required\")\n\t}\n\n\treturn \u0026memberGroup{\n\t\tname:    name,\n\t\tmembers: members,\n\t}, nil\n}\n\n// MustNewMemberGroup creates a new group of members or panics on error.\nfunc MustNewMemberGroup(name string, members MemberStorage) MemberGroup {\n\tg, err := NewMemberGroup(name, members)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn g\n}\n\ntype memberGroup struct {\n\tname    string\n\tmembers MemberStorage\n\tmeta    any\n}\n\n// Name returns the name of the group.\nfunc (g memberGroup) Name() string {\n\treturn g.name\n}\n\n// Members returns the members that belong to the group.\nfunc (g memberGroup) Members() MemberStorage {\n\treturn g.members\n}\n\n// SetMeta sets any metadata relevant to the group.\nfunc (g *memberGroup) SetMeta(meta any) {\n\tg.meta = meta\n}\n\n// GetMeta returns the group metadata.\nfunc (g memberGroup) GetMeta() any {\n\treturn g.meta\n}\n\n// NewReadonlyMemberGroup creates a new readonly member group.\nfunc NewReadonlyMemberGroup(g MemberGroup) (*ReadonlyMemberGroup, error) {\n\tif g == nil {\n\t\treturn nil, errors.New(\"member group is required\")\n\t}\n\treturn \u0026ReadonlyMemberGroup{g}, nil\n}\n\n// MustNewReadonlyMemberGroup creates a new readonly member group or panics on error.\nfunc MustNewReadonlyMemberGroup(g MemberGroup) *ReadonlyMemberGroup {\n\tgroup, err := NewReadonlyMemberGroup(g)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn group\n}\n\n// ReadonlyMemberGroup defines a readonly member group.\ntype ReadonlyMemberGroup struct {\n\tgroup MemberGroup\n}\n\n// Name returns the name of the group.\nfunc (g ReadonlyMemberGroup) Name() string {\n\tif g.group == nil {\n\t\treturn \"\"\n\t}\n\treturn g.group.Name()\n}\n\n// Members returns the members that belong to the group.\nfunc (g ReadonlyMemberGroup) Members() *ReadonlyMemberStorage {\n\tif g.group == nil {\n\t\treturn nil\n\t}\n\treturn MustNewReadonlyMemberStorage(g.group.Members())\n}\n\n// GetMeta returns the group metadata.\nfunc (g ReadonlyMemberGroup) GetMeta() any {\n\tif g.group == nil {\n\t\treturn nil\n\t}\n\treturn g.group.GetMeta()\n}\n"},{"name":"member_group_test.gno","body":"package commondao_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc TestMemberGroupNew(t *testing.T) {\n\tg, err := commondao.NewMemberGroup(\"\", nil)\n\turequire.ErrorContains(t, err, \"member storage is required\")\n\n\tstorage := commondao.NewMemberStorage()\n\tg, err = commondao.NewMemberGroup(\"\", storage)\n\turequire.ErrorContains(t, err, \"member group name is required\")\n\n\tname := \"Tier 1\"\n\tg, err = commondao.NewMemberGroup(name, storage)\n\turequire.NoError(t, err, \"expect no error\")\n\tuassert.Equal(t, name, g.Name(), \"expect group name to match\")\n\tuassert.NotNil(t, g.Members(), \"expect members to be not nil\")\n\tuassert.Nil(t, g.GetMeta(), \"expect default group meta to be nil\")\n}\n\nfunc TestMemberGroupMeta(t *testing.T) {\n\tg, err := commondao.NewMemberGroup(\"Test\", commondao.NewMemberStorage())\n\turequire.NoError(t, err, \"expect no error\")\n\n\tg.SetMeta(42)\n\tv := g.GetMeta()\n\turequire.NotEqual(t, nil, v, \"expect metadata to be not nil\")\n\n\tmeta, ok := v.(int)\n\turequire.True(t, ok, \"expect meta type to be int\")\n\tuassert.Equal(t, 42, meta, \"expect metadata to match\")\n}\n"},{"name":"member_grouping.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\ntype (\n\t// MemberGroupIterFn defines a callback to iterate DAO members groups.\n\tMemberGroupIterFn func(MemberGroup) bool\n\n\t// MemberGrouping defines an interface for storing multiple member groups.\n\t// Member grouping can be used by implementations that require grouping users\n\t// by roles or by tiers for example.\n\tMemberGrouping interface {\n\t\t// Size returns the number of groups that grouping contains.\n\t\tSize() int\n\n\t\t// Has checks if a group exists.\n\t\tHas(name string) bool\n\n\t\t// Add adds an new member group if it doesn't exists.\n\t\tAdd(name string) (MemberGroup, error)\n\n\t\t// Get returns a member group.\n\t\tGet(name string) (_ MemberGroup, found bool)\n\n\t\t// Delete deletes a member group.\n\t\tDelete(name string) error\n\n\t\t// IterateByOffset iterates all member groups.\n\t\t// The callback can return true to stop iteration.\n\t\tIterateByOffset(offset, count int, fn MemberGroupIterFn) (stopped bool)\n\t}\n)\n\n// NewMemberGrouping creates a new members grouping.\nfunc NewMemberGrouping(options ...MemberGroupingOption) MemberGrouping {\n\tg := \u0026memberGrouping{\n\t\tcreateStorage: func(string) MemberStorage { return NewMemberStorage() },\n\t}\n\n\tfor _, apply := range options {\n\t\tapply(g)\n\t}\n\treturn g\n}\n\ntype memberGrouping struct {\n\tgroups        bptree.BPTree // string(name) -\u003e MemberGroup\n\tcreateStorage func(group string) MemberStorage\n}\n\n// Size returns the number of groups that grouping contains.\nfunc (g memberGrouping) Size() int {\n\treturn g.groups.Size()\n}\n\n// Has checks if a group exists.\nfunc (g memberGrouping) Has(name string) bool {\n\treturn g.groups.Has(name)\n}\n\n// Add adds an new member group if it doesn't exists.\nfunc (g *memberGrouping) Add(name string) (MemberGroup, error) {\n\tif g.groups.Has(name) {\n\t\treturn nil, errors.New(\"member group already exists: \" + name)\n\t}\n\n\tmg, err := NewMemberGroup(name, g.createStorage(name))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tg.groups.Set(name, mg)\n\treturn mg, nil\n}\n\n// Get returns a member group.\nfunc (g memberGrouping) Get(name string) (_ MemberGroup, found bool) {\n\tv := g.groups.Get(name)\n\tif v == nil {\n\t\treturn nil, false\n\t}\n\treturn v.(MemberGroup), true\n}\n\n// Delete deletes a member group.\nfunc (g *memberGrouping) Delete(name string) error {\n\tg.groups.Remove(name)\n\treturn nil\n}\n\n// IterateByOffset iterates all member groups.\nfunc (g memberGrouping) IterateByOffset(offset, count int, fn MemberGroupIterFn) bool {\n\treturn g.groups.IterateByOffset(offset, count, func(_ string, v any) bool {\n\t\treturn fn(v.(MemberGroup))\n\t})\n}\n\n// NewReadonlyMemberGrouping creates a new grouping if member.\nfunc NewReadonlyMemberGrouping(g MemberGrouping) (*ReadonlyMemberGrouping, error) {\n\tif g == nil {\n\t\treturn nil, errors.New(\"member grouping is required\")\n\t}\n\treturn \u0026ReadonlyMemberGrouping{g}, nil\n}\n\n// MustNewReadonlyMemberGrouping creates a new grouping if member or panics on error.\nfunc MustNewReadonlyMemberGrouping(g MemberGrouping) *ReadonlyMemberGrouping {\n\tgrouping, err := NewReadonlyMemberGrouping(g)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn grouping\n}\n\n// ReadonlyMemberGrouping defines a type for storing multiple readonly member groups.\ntype ReadonlyMemberGrouping struct {\n\tgrouping MemberGrouping\n}\n\n// Size returns the number of groups that grouping contains.\nfunc (g ReadonlyMemberGrouping) Size() int {\n\tif g.grouping == nil {\n\t\treturn 0\n\t}\n\treturn g.grouping.Size()\n}\n\n// Has checks if a group exists.\nfunc (g ReadonlyMemberGrouping) Has(name string) bool {\n\tif g.grouping == nil {\n\t\treturn false\n\t}\n\treturn g.grouping.Has(name)\n}\n\n// Get returns a member group.\nfunc (g ReadonlyMemberGrouping) Get(name string) (_ *ReadonlyMemberGroup, found bool) {\n\tif g.grouping == nil {\n\t\treturn nil, false\n\t}\n\n\tgroup, found := g.grouping.Get(name)\n\tif !found {\n\t\treturn nil, false\n\t}\n\treturn MustNewReadonlyMemberGroup(group), true\n}\n\n// IterateByOffset iterates all member groups.\nfunc (g ReadonlyMemberGrouping) IterateByOffset(offset, count int, fn func(*ReadonlyMemberGroup) bool) bool {\n\tif g.grouping == nil {\n\t\treturn false\n\t}\n\n\treturn g.grouping.IterateByOffset(offset, count, func(group MemberGroup) bool {\n\t\treturn fn(MustNewReadonlyMemberGroup(group))\n\t})\n}\n"},{"name":"member_grouping_options.gno","body":"package commondao\n\n// MemberGroupingOption configures member groupings.\ntype MemberGroupingOption func(MemberGrouping)\n\n// SECURITY XXX: UseStorageFactory captures a caller-supplied factory\n// that returns the MemberStorage used to track who is a DAO member.\n// A malicious factory can return a MemberStorage impl that lies about\n// membership — always-true (everyone is a member, votes pass trivially)\n// or always-false (legitimate members are locked out). This isn't a\n// cur-leak; it's a behavior-substitution risk on the data layer the\n// DAO's voting and proposal logic depends on.\n// Fix: document that the factory must come from trusted code (a\n// hardcoded constructor in the DAO realm itself), and never accept\n// a UseStorageFactory option from external-realm input. Or restrict\n// to a canonical-impl allowlist (type-switch on known MemberStorage\n// types) in commondao itself.\n//\n// UseStorageFactory assigns a custom member storage creation function to the grouping.\n// Creation function is called each time a member group is added, with the name of the\n// group as the only argument, to create a storage where the new group stores its members.\nfunc UseStorageFactory(fn func(group string) MemberStorage) MemberGroupingOption {\n\tif fn == nil {\n\t\tpanic(\"storage factory function must not be nil\")\n\t}\n\n\treturn func(g MemberGrouping) {\n\t\tgrouping, ok := g.(*memberGrouping)\n\t\tif !ok {\n\t\t\tpanic(\"storage factory not supported by member grouping\")\n\t\t}\n\n\t\tgrouping.createStorage = fn\n\t}\n}\n\n// WithGroups creates multiple members groups.\n// To use a custom member storage factory to create the groups make sure that this option\n// comes after the `UseStorageFactory()` option, otherwise groups are created using the\n// default factory which is `commondao.NewMemberStorage()`.\nfunc WithGroups(names ...string) MemberGroupingOption {\n\treturn func(g MemberGrouping) {\n\t\tfor _, name := range names {\n\t\t\tif _, err := g.Add(name); err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\t\t}\n\t}\n}\n"},{"name":"member_grouping_test.gno","body":"package commondao_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc TestMemberGroupingAdd(t *testing.T) {\n\tt.Run(\"defauls\", func(t *testing.T) {\n\t\tname := \"Foo\"\n\t\tg := commondao.NewMemberGrouping()\n\n\t\tuassert.False(t, g.Has(name), \"expect grouping group not to be found\")\n\t\tuassert.Equal(t, 0, g.Size(), \"expect grouping to be empty\")\n\t})\n\n\tt.Run(\"success\", func(t *testing.T) {\n\t\tname := \"Foo\"\n\t\tg := commondao.NewMemberGrouping()\n\t\tmg, err := g.Add(name)\n\n\t\turequire.NoError(t, err, \"expect no error\")\n\t\tuassert.True(t, g.Has(name), \"expect grouping group to be found\")\n\t\tuassert.Equal(t, 1, g.Size(), \"expect grouping to have a single group\")\n\n\t\turequire.True(t, mg != nil, \"expected grouping group to be not nil\")\n\t\tuassert.Equal(t, name, mg.Name(), \"expect group to have the right name\")\n\t})\n\n\tt.Run(\"duplicated name\", func(t *testing.T) {\n\t\tname := \"Foo\"\n\t\tg := commondao.NewMemberGrouping()\n\t\t_, err := g.Add(name)\n\t\turequire.NoError(t, err, \"expect no error\")\n\n\t\t_, err = g.Add(name)\n\t\tuassert.ErrorContains(t, err, \"member group already exists: Foo\", \"expect duplication error\")\n\t})\n}\n\nfunc TestMemberGroupingGet(t *testing.T) {\n\tt.Run(\"success\", func(t *testing.T) {\n\t\tname := \"Foo\"\n\t\tg := commondao.NewMemberGrouping()\n\t\tg.Add(name)\n\n\t\tmg, found := g.Get(name)\n\n\t\turequire.True(t, found, \"expect grouping group to be found\")\n\t\turequire.True(t, mg != nil, \"expect grouping group to be not nil\")\n\t\tuassert.Equal(t, name, mg.Name(), \"expect group to have the right name\")\n\t})\n\n\tt.Run(\"group not found\", func(t *testing.T) {\n\t\tg := commondao.NewMemberGrouping()\n\n\t\t_, found := g.Get(\"Foo\")\n\n\t\turequire.False(t, found, \"expect grouping group to be not found\")\n\t})\n}\n\nfunc TestMemberGroupingDelete(t *testing.T) {\n\tname := \"Foo\"\n\tg := commondao.NewMemberGrouping()\n\tg.Add(name)\n\n\terr := g.Delete(name)\n\n\turequire.NoError(t, err, \"expect no error\")\n\tuassert.False(t, g.Has(name), \"expect grouping group not to be found\")\n}\n\nfunc TestMemberGroupingIterate(t *testing.T) {\n\tgroups := []string{\"Tier 1\", \"Tier 2\", \"Tier 3\"}\n\tg := commondao.NewMemberGrouping()\n\tfor _, name := range groups {\n\t\tg.Add(name)\n\t}\n\n\tvar i int\n\tg.IterateByOffset(0, g.Size(), func(mg commondao.MemberGroup) bool {\n\t\turequire.True(t, mg != nil, \"expect member group not to be nil\")\n\t\turequire.Equal(t, groups[i], mg.Name(), \"expect group to be iterated in order\")\n\n\t\ti++\n\t\treturn false\n\t})\n\n\tuassert.Equal(t, len(groups), i, \"expect all groups to be iterated\")\n}\n\nfunc TestMemberGroupingUseStorageFactory(t *testing.T) {\n\tvar (\n\t\tgroupName     string\n\t\tdefaultMember = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t)\n\n\tfn := func(group string) commondao.MemberStorage {\n\t\tgroupName = group\n\n\t\t// Create storage and add a default member\n\t\ts := commondao.NewMemberStorage()\n\t\ts.Add(defaultMember)\n\t\treturn s\n\t}\n\n\tgrouping := commondao.NewMemberGrouping(commondao.UseStorageFactory(fn))\n\tgroup, err := grouping.Add(\"foo\")\n\n\turequire.NoError(t, err, \"expect no group creation error\")\n\tuassert.True(t, group.Members().Has(defaultMember), \"expect storage to have a default member\")\n\tuassert.Equal(t, \"foo\", groupName, \"expect group name to match\")\n}\n\nfunc TestMemberGroupingWithGroups(t *testing.T) {\n\tdefaultGroups := []string{\"foo\", \"bar\", \"baz\"}\n\tgrouping := commondao.NewMemberGrouping(commondao.WithGroups(defaultGroups...))\n\n\turequire.Equal(t, len(defaultGroups), grouping.Size(), \"expect groups to be created\")\n\tfor _, group := range defaultGroups {\n\t\tuassert.True(t, grouping.Has(group), \"expect group to be found: \"+group)\n\t}\n}\n"},{"name":"member_storage.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/moul/addrset\"\n)\n\ntype (\n\t// MemberIterFn defines a callback to iterate DAO members.\n\tMemberIterFn func(address) bool\n\n\t// MemberStorage defines an interface for member storages.\n\tMemberStorage interface {\n\t\t// Size returns the number of members in the storage.\n\t\tSize() int\n\n\t\t// Has checks if a member exists in the storage.\n\t\tHas(address) bool\n\n\t\t// Add adds a member to the storage.\n\t\t// Returns true if the member is added, or false if it already existed.\n\t\tAdd(address) bool\n\n\t\t// Remove removes a member from the storage.\n\t\t// Returns true if member was removed, or false if it was not found.\n\t\tRemove(address) bool\n\n\t\t// Grouping returns member groups when supported.\n\t\t// When nil is returned it means that grouping of members is not supported.\n\t\t// Member groups can be used by implementations that require grouping users\n\t\t// by roles or by tiers for example.\n\t\tGrouping() MemberGrouping\n\n\t\t// IterateByOffset iterates members starting at the given offset.\n\t\t// The callback can return true to stop iteration.\n\t\tIterateByOffset(offset, count int, fn MemberIterFn) (stopped bool)\n\t}\n)\n\n// NewMemberStorage creates a new member storage.\n// Function returns a new member storage that doesn't support member groups.\n// This type of storage is useful when there is no need to group members.\nfunc NewMemberStorage() MemberStorage {\n\treturn \u0026memberStorage{}\n}\n\n// NewMemberStorageWithGrouping a new member storage with support for member groups.\n// Member groups can be used by implementations that require grouping users by roles\n// or by tiers for example.\nfunc NewMemberStorageWithGrouping(options ...MemberGroupingOption) MemberStorage {\n\treturn \u0026memberStorage{grouping: NewMemberGrouping(options...)}\n}\n\ntype memberStorage struct {\n\taddrset.Set\n\n\tgrouping MemberGrouping\n}\n\n// Size returns the number of members in the storage.\n// The result is the number of members within the base underlying storage,\n// grouped members are not included, they must be counted separately.\nfunc (s memberStorage) Size() int {\n\treturn s.Set.Size()\n}\n\n// Has checks if a member exists in the storage.\n// Member is also searched within all defined member groups.\nfunc (s memberStorage) Has(member address) bool {\n\t// Check underlying member address storage\n\tif s.Set.Has(member) {\n\t\treturn true\n\t}\n\n\tif s.grouping == nil {\n\t\treturn false\n\t}\n\n\t// Check groups when member is not found in underlying storage\n\treturn s.grouping.IterateByOffset(0, s.grouping.Size(), func(g MemberGroup) bool {\n\t\treturn g.Members().Has(member)\n\t})\n}\n\n// Grouping returns member groups.\nfunc (s memberStorage) Grouping() MemberGrouping {\n\treturn s.grouping\n}\n\n// IterateByOffset iterates members starting at the given offset.\n// The callback can return true to stop iteration.\nfunc (s memberStorage) IterateByOffset(offset, count int, fn MemberIterFn) bool {\n\tvar stopped bool\n\ts.Set.IterateByOffset(offset, count, func(member address) bool {\n\t\tstopped = fn(member)\n\t\treturn stopped\n\t})\n\treturn stopped\n}\n\n// NewReadonlyMemberStorage creates a new readonly member storage.\nfunc NewReadonlyMemberStorage(s MemberStorage) (*ReadonlyMemberStorage, error) {\n\tif s == nil {\n\t\treturn nil, errors.New(\"member storage is required\")\n\t}\n\treturn \u0026ReadonlyMemberStorage{s}, nil\n}\n\n// MustNewReadonlyMemberStorage creates a new readonly member storage or panics on error.\nfunc MustNewReadonlyMemberStorage(s MemberStorage) *ReadonlyMemberStorage {\n\tstorage, err := NewReadonlyMemberStorage(s)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn storage\n}\n\n// ReadonlyMemberStorage defines a readonly member storage.\ntype ReadonlyMemberStorage struct {\n\tstorage MemberStorage\n}\n\n// Size returns the number of members in the storage.\nfunc (s ReadonlyMemberStorage) Size() int {\n\tif s.storage == nil {\n\t\treturn 0\n\t}\n\treturn s.storage.Size()\n}\n\n// Has checks if a member exists in the storage.\nfunc (s ReadonlyMemberStorage) Has(member address) bool {\n\tif s.storage == nil {\n\t\treturn false\n\t}\n\treturn s.storage.Has(member)\n}\n\n// Grouping returns member groups.\nfunc (s ReadonlyMemberStorage) Grouping() *ReadonlyMemberGrouping {\n\tif s.storage == nil {\n\t\treturn nil\n\t}\n\n\tif g := s.storage.Grouping(); g != nil {\n\t\treturn MustNewReadonlyMemberGrouping(g)\n\t}\n\treturn nil\n}\n\n// IterateByOffset iterates members starting at the given offset.\n// The callback can return true to stop iteration.\nfunc (s ReadonlyMemberStorage) IterateByOffset(offset, count int, fn MemberIterFn) bool {\n\tif s.storage != nil {\n\t\treturn s.storage.IterateByOffset(offset, count, fn)\n\t}\n\treturn false\n}\n\n// CountStorageMembers returns the total number of members in the storage.\n// It counts all members in each group and the ones without group.\nfunc CountStorageMembers(s *ReadonlyMemberStorage) int {\n\tif s == nil {\n\t\treturn 0\n\t}\n\n\tc := s.Size()\n\ts.Grouping().IterateByOffset(0, s.Grouping().Size(), func(g *ReadonlyMemberGroup) bool {\n\t\tc += g.Members().Size()\n\t\treturn false\n\t})\n\treturn c\n}\n"},{"name":"member_storage_test.gno","body":"package commondao_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc TestMemberStorageWithGrouping(t *testing.T) {\n\t// Prepare\n\ttiers := []struct {\n\t\tName    string\n\t\tWeight  int\n\t\tMembers []address\n\t}{\n\t\t{\n\t\t\tName:   \"Tier 1\",\n\t\t\tWeight: 3,\n\t\t\tMembers: []address{\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tName:   \"Tier 2\",\n\t\t\tWeight: 2,\n\t\t\tMembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t},\n\t\t},\n\t}\n\n\tstorage := commondao.NewMemberStorageWithGrouping()\n\tfor _, tier := range tiers {\n\t\tmg, err := storage.Grouping().Add(tier.Name)\n\t\turequire.NoError(t, err, \"expect no error adding tier\")\n\n\t\tmg.SetMeta(tier.Weight)\n\n\t\tfor _, addr := range tier.Members {\n\t\t\tok := mg.Members().Add(addr)\n\t\t\turequire.True(t, ok, \"expect member to be added\")\n\t\t}\n\t}\n\n\t// Assert\n\tfor i := 0; i \u003c len(tiers); i++ {\n\t\ttier := tiers[i]\n\t\tmg, found := storage.Grouping().Get(tier.Name)\n\t\turequire.True(t, found, \"expect member group to be found\")\n\n\t\tv := mg.GetMeta()\n\t\turequire.True(t, v != nil, \"expect meta to be not nil\")\n\n\t\tweight, ok := v.(int)\n\t\turequire.True(t, ok, \"expect group metadata to be an integer\")\n\t\tuassert.Equal(t, tier.Weight, weight, \"expect group weight to match\")\n\n\t\tvar i int\n\t\tmg.Members().IterateByOffset(0, len(tier.Members), func(addr address) bool {\n\t\t\tuassert.Equal(t, tier.Members[i], addr, \"expect tier member to match\")\n\n\t\t\ti++\n\t\t\treturn false\n\t\t})\n\n\t\tuassert.Equal(t, len(tier.Members), i, \"expect all tier members to be iterated\")\n\t}\n}\n\nfunc TestMemberStorageHas(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tfound bool\n\t\tsetup func() commondao.MemberStorage\n\t}{\n\t\t{\n\t\t\tname:  \"found\",\n\t\t\tfound: true,\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := commondao.NewMemberStorage()\n\t\t\t\ts.Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:  \"not found\",\n\t\t\tfound: false,\n\t\t\tsetup: func() commondao.MemberStorage { return commondao.NewMemberStorage() },\n\t\t},\n\t\t{\n\t\t\tname:  \"found in base storage with grouping\",\n\t\t\tfound: true,\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := commondao.NewMemberStorageWithGrouping()\n\t\t\t\ts.Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\n\t\t\t\tg, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tg.Members().Add(\"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:  \"found with grouping\",\n\t\t\tfound: true,\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := commondao.NewMemberStorageWithGrouping()\n\t\t\t\tg, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tg.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:  \"not found with grouping\",\n\t\t\tfound: false,\n\t\t\tsetup: func() commondao.MemberStorage { return commondao.NewMemberStorageWithGrouping() },\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tstorage := tc.setup()\n\n\t\t\t// Act\n\t\t\tfound := storage.Has(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\n\t\t\t// Assert\n\t\t\turequire.Equal(t, tc.found, found)\n\t\t})\n\t}\n}\n\nfunc TestCountStorageMembers(t *testing.T) {\n\tstorage := commondao.NewMemberStorageWithGrouping()\n\tstorage.Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\n\tg, err := storage.Grouping().Add(\"A\")\n\turequire.NoError(t, err, \"expect no error creating member group A\")\n\n\tg.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tg.Members().Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\tg, err = storage.Grouping().Add(\"B\")\n\turequire.NoError(t, err, \"expect no error creating member group B\")\n\n\tg.Members().Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\") // Add a member that exists in other group\n\n\ts := commondao.MustNewReadonlyMemberStorage(storage)\n\tuassert.Equal(t, 4, commondao.CountStorageMembers(s))\n}\n"},{"name":"proposal.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\nconst (\n\tStatusActive    ProposalStatus = \"active\"\n\tStatusPassed                   = \"passed\"\n\tStatusRejected                 = \"rejected\"\n\tStatusExecuted                 = \"executed\"\n\tStatusFailed                   = \"failed\"\n\tStatusWithdrawn                = \"withdrawn\"\n)\n\nconst (\n\tChoiceNone       VoteChoice = \"\"\n\tChoiceYes                   = \"YES\"\n\tChoiceNo                    = \"NO\"\n\tChoiceNoWithVeto            = \"NO WITH VETO\"\n\tChoiceAbstain               = \"ABSTAIN\"\n)\n\nconst (\n\tQuorumOneThird     float64 = 0.33 // percentage, checked as \u003e= than quorum\n\tQuorumMoreThanHalf         = 0.51\n\tQuorumTwoThirds            = 0.66\n\tQuorumThreeFourths         = 0.75\n\tQuorumFull                 = 1\n)\n\n// MaxCustomVoteChoices defines the maximum number of custom\n// vote choices that a proposal definition can define.\nconst MaxCustomVoteChoices = 10\n\nvar (\n\tErrInvalidCreatorAddress      = errors.New(\"invalid proposal creator address\")\n\tErrMaxCustomVoteChoices       = errors.New(\"max number of custom vote choices exceeded\")\n\tErrProposalDefinitionRequired = errors.New(\"proposal definition is required\")\n\tErrNoQuorum                   = errors.New(\"no quorum\")\n\tErrStatusIsNotActive          = errors.New(\"proposal status is not active\")\n)\n\ntype (\n\t// ProposalStatus defines a type for different proposal states.\n\tProposalStatus string\n\n\t// VoteChoice defines a type for proposal vote choices.\n\tVoteChoice string\n\n\t// ExecFunc defines a type for functions that executes proposals.\n\tExecFunc func(realm) error\n\n\t// Proposal defines a DAO proposal.\n\tProposal struct {\n\t\tid             uint64\n\t\tstatus         ProposalStatus\n\t\tdefinition     ProposalDefinition\n\t\tcreator        address\n\t\trecord         *VotingRecord // TODO: Add support for multiple voting records\n\t\tstatusReason   string\n\t\tvoteChoices    *bptree.BPTree // string(VoteChoice) -\u003e struct{}\n\t\tvotingDeadline time.Time\n\t\tcreatedAt      time.Time\n\t}\n\n\t// ProposalDefinition defines an interface for custom proposal definitions.\n\t// These definitions define proposal content and behavior, they esentially\n\t// allow the definition for different proposal types.\n\tProposalDefinition interface {\n\t\t// Title returns the proposal title.\n\t\tTitle() string\n\n\t\t// Body returns proposal's body.\n\t\t// It usually contains description or values that are specific to the proposal,\n\t\t// like a description of the proposal's motivation or the list of values that\n\t\t// would be applied when the proposal is approved.\n\t\tBody() string\n\n\t\t// VotingPeriod returns the period where votes are allowed after proposal creation.\n\t\t// It is used to calculate the voting deadline from the proposal's creationd date.\n\t\tVotingPeriod() time.Duration\n\n\t\t// Tally counts the number of votes and verifies if proposal passes.\n\t\t// It receives a voting context containing a readonly record with the votes\n\t\t// that has been submitted for the proposal and also the list of DAO members.\n\t\tTally(VotingContext) (passes bool, _ error)\n\t}\n\n\t// Validable defines an interface for proposal definitions that require state validation.\n\t// Validation is done before execution and normally also during proposal rendering.\n\tValidable interface {\n\t\t// Validate validates that the proposal is valid for the current state.\n\t\tValidate() error\n\t}\n\n\t// Executable defines an interface for proposal definitions that modify state on approval.\n\t// Once proposals are executed they are archived and considered finished.\n\tExecutable interface {\n\t\t// Executor returns a function to execute the proposal.\n\t\tExecutor() ExecFunc\n\t}\n\n\t// CustomizableVoteChoices defines an interface for proposal definitions that want\n\t// to customize the list of allowed voting choices.\n\tCustomizableVoteChoices interface {\n\t\t// CustomVoteChoices returns a list of valid voting choices.\n\t\t// Choices are considered valid only when there are at least two possible choices\n\t\t// otherwise proposal defaults to using YES, NO and ABSTAIN as valid choices.\n\t\tCustomVoteChoices() []VoteChoice\n\t}\n)\n\n// MustValidate validates that a proposal is valid for the current state or panics on error.\nfunc MustValidate(v Validable) {\n\tif v == nil {\n\t\tpanic(\"validable proposal definition is nil\")\n\t}\n\n\tif err := v.Validate(); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// MustExecute executes an executable proposal or panics on error.\n//\n// rlm is the realm authority threaded into the executor via cross(rlm).\n// MustExecute itself is not a crossing function (rlm sits in a non-first\n// parameter slot) because /p/ production code cannot declare crossing\n// functions — callers pass `cur` directly as the rlm argument.\nfunc MustExecute(e Executable, rlm realm) {\n\tif e == nil {\n\t\tpanic(\"executable proposal definition is nil\")\n\t}\n\n\tfn := e.Executor()\n\tif fn == nil {\n\t\treturn\n\t}\n\n\tif err := fn(cross(rlm)); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// NewProposal creates a new DAO proposal.\nfunc NewProposal(id uint64, creator address, d ProposalDefinition) (*Proposal, error) {\n\tif d == nil {\n\t\treturn nil, ErrProposalDefinitionRequired\n\t}\n\n\tif !creator.IsValid() {\n\t\treturn nil, ErrInvalidCreatorAddress\n\t}\n\n\tnow := time.Now()\n\tp := \u0026Proposal{\n\t\tid:             id,\n\t\tstatus:         StatusActive,\n\t\tdefinition:     d,\n\t\tcreator:        creator,\n\t\trecord:         \u0026VotingRecord{},\n\t\tvoteChoices:    bptree.NewBPTree32(),\n\t\tvotingDeadline: now.Add(d.VotingPeriod()),\n\t\tcreatedAt:      now,\n\t}\n\n\tif v, ok := d.(CustomizableVoteChoices); ok {\n\t\tchoices := v.CustomVoteChoices()\n\t\tif len(choices) \u003e MaxCustomVoteChoices {\n\t\t\treturn nil, ErrMaxCustomVoteChoices\n\t\t}\n\n\t\tfor _, c := range choices {\n\t\t\tp.voteChoices.Set(string(c), struct{}{})\n\t\t}\n\t}\n\n\t// Use default voting choices when the definition returns none or a single vote choice\n\tif p.voteChoices.Size() \u003c 2 {\n\t\tp.voteChoices.Set(string(ChoiceYes), struct{}{})\n\t\tp.voteChoices.Set(string(ChoiceNo), struct{}{})\n\t\tp.voteChoices.Set(string(ChoiceAbstain), struct{}{})\n\t}\n\treturn p, nil\n}\n\n// ID returns the unique proposal identifies.\nfunc (p Proposal) ID() uint64 {\n\treturn p.id\n}\n\n// Definition returns the proposal definition.\n// Proposal definitions define proposal content and behavior.\nfunc (p Proposal) Definition() ProposalDefinition {\n\treturn p.definition\n}\n\n// Status returns the current proposal status.\nfunc (p Proposal) Status() ProposalStatus {\n\treturn p.status\n}\n\n// Creator returns the address of the account that created the proposal.\nfunc (p Proposal) Creator() address {\n\treturn p.creator\n}\n\n// CreatedAt returns the time that proposal was created.\nfunc (p Proposal) CreatedAt() time.Time {\n\treturn p.createdAt\n}\n\n// VotingRecord returns a record that contains all the votes submitted for the proposal.\nfunc (p Proposal) VotingRecord() *VotingRecord {\n\treturn p.record\n}\n\n// StatusReason returns an optional reason that lead to the current proposal status.\n// Reason is mostyl useful when a proposal fails.\nfunc (p Proposal) StatusReason() string {\n\treturn p.statusReason\n}\n\n// VotingDeadline returns the deadline after which no more votes should be allowed.\nfunc (p Proposal) VotingDeadline() time.Time {\n\treturn p.votingDeadline\n}\n\n// VoteChoices returns the list of vote choices allowed for the proposal.\nfunc (p Proposal) VoteChoices() []VoteChoice {\n\tchoices := make([]VoteChoice, 0, p.voteChoices.Size())\n\tp.voteChoices.Iterate(\"\", \"\", func(c string, _ any) bool {\n\t\tchoices = append(choices, VoteChoice(c))\n\t\treturn false\n\t})\n\treturn choices\n}\n\n// HasVotingDeadlinePassed checks if the voting deadline has been met.\nfunc (p Proposal) HasVotingDeadlinePassed() bool {\n\treturn !time.Now().Before(p.VotingDeadline())\n}\n\n// Validate validates that a proposal is valid for the current state.\n// Validation is done when proposal status is active and when the definition supports validation.\nfunc (p Proposal) Validate() error {\n\tif p.status != StatusActive {\n\t\treturn nil\n\t}\n\n\tif v, ok := p.definition.(Validable); ok {\n\t\treturn v.Validate()\n\t}\n\treturn nil\n}\n\n// IsVoteChoiceValid checks if a vote choice is valid for the proposal.\nfunc (p Proposal) IsVoteChoiceValid(c VoteChoice) bool {\n\treturn p.voteChoices.Has(string(c))\n}\n\n// Tally counts votes and updates proposal status with the current outcome.\n// Proposal status is updated to \"passed\" when proposal is approved\n// or to \"rejected\" if proposal doesn't pass.\nfunc (p *Proposal) Tally(members MemberStorage) error {\n\tif p.status != StatusActive {\n\t\treturn ErrStatusIsNotActive\n\t}\n\n\tctx := MustNewVotingContext(p.VotingRecord(), members)\n\tpasses, err := p.Definition().Tally(ctx)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif passes {\n\t\tp.status = StatusPassed\n\t} else {\n\t\tp.status = StatusRejected\n\t}\n\treturn nil\n}\n\n// IsQuorumReached checks if a participation quorum is reach.\nfunc IsQuorumReached(quorum float64, r ReadonlyVotingRecord, members ReadonlyMemberStorage) bool {\n\tif members.Size() \u003c= 0 || quorum \u003c= 0 {\n\t\treturn false\n\t}\n\n\tvar totalCount int\n\tr.IterateVotesCount(func(c VoteChoice, voteCount int) bool {\n\t\t// Don't count explicit abstentions or invalid votes\n\t\tif c != ChoiceNone \u0026\u0026 c != ChoiceAbstain {\n\t\t\ttotalCount += r.VoteCount(c)\n\t\t}\n\t\treturn false\n\t})\n\n\tpercentage := float64(totalCount) / float64(members.Size())\n\treturn percentage \u003e= quorum\n}\n"},{"name":"proposal_storage.gno","body":"package commondao\n\nimport (\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// ProposalStorage defines an interface for proposal storages.\ntype ProposalStorage interface {\n\t// Has checks if a proposal exists.\n\tHas(id uint64) bool\n\n\t// Get returns a proposal or nil when proposal doesn't exist.\n\tGet(id uint64) *Proposal\n\n\t// Add adds a proposal to the storage.\n\tAdd(*Proposal)\n\n\t// Remove removes a proposal from the storage.\n\tRemove(id uint64)\n\n\t// Size returns the number of proposals that the storage contains.\n\tSize() int\n\n\t// Iterate iterates proposals.\n\tIterate(offset, count int, reverse bool, fn func(*Proposal) bool) bool\n}\n\n// NewProposalStorage creates a new proposal storage.\nfunc NewProposalStorage() ProposalStorage {\n\treturn \u0026proposalStorage{bptree.NewBPTree32()}\n}\n\ntype proposalStorage struct {\n\tstorage *bptree.BPTree // string(proposal ID) -\u003e *Proposal\n}\n\n// Has checks if a proposal exists.\nfunc (s proposalStorage) Has(id uint64) bool {\n\treturn s.storage.Has(makeProposalKey(id))\n}\n\n// Get returns a proposal or nil when proposal doesn't exist.\nfunc (s proposalStorage) Get(id uint64) *Proposal {\n\tif v := s.storage.Get(makeProposalKey(id)); v != nil {\n\t\treturn v.(*Proposal)\n\t}\n\treturn nil\n}\n\n// Add adds a proposal to the storage.\nfunc (s *proposalStorage) Add(p *Proposal) {\n\tif p == nil {\n\t\treturn\n\t}\n\n\ts.storage.Set(makeProposalKey(p.ID()), p)\n}\n\n// Remove removes a proposal from the storage.\nfunc (s *proposalStorage) Remove(id uint64) {\n\ts.storage.Remove(makeProposalKey(id))\n}\n\n// Size returns the number of proposals that the storage contains.\nfunc (s proposalStorage) Size() int {\n\treturn s.storage.Size()\n}\n\n// Iterate iterates proposals.\nfunc (s proposalStorage) Iterate(offset, count int, reverse bool, fn func(*Proposal) bool) bool {\n\tif fn == nil {\n\t\treturn false\n\t}\n\n\tcb := func(_ string, v any) bool { return fn(v.(*Proposal)) }\n\n\tif reverse {\n\t\treturn s.storage.ReverseIterateByOffset(offset, count, cb)\n\t}\n\treturn s.storage.IterateByOffset(offset, count, cb)\n}\n\nfunc makeProposalKey(id uint64) string {\n\treturn seqid.ID(id).String()\n}\n"},{"name":"proposal_storage_test.gno","body":"package commondao_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc TestProposalStorageAdd(t *testing.T) {\n\tp, _ := commondao.NewProposal(1, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", testPropDef{})\n\ts := commondao.NewProposalStorage()\n\tinitialSize := s.Size()\n\n\ts.Add(p)\n\n\tuassert.Equal(t, 0, initialSize, \"expect initial storage to be empty\")\n\tuassert.Equal(t, 1, s.Size(), \"expect storage to have one proposal\")\n\tuassert.True(t, s.Has(p.ID()), \"expect proposal to be found\")\n}\n\nfunc TestProposalStorageGet(t *testing.T) {\n\tp, _ := commondao.NewProposal(1, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", testPropDef{})\n\ts := commondao.NewProposalStorage()\n\ts.Add(p)\n\n\tp2 := s.Get(p.ID())\n\n\turequire.NotEqual(t, nil, p2, \"expect proposal to be found\")\n\tuassert.Equal(t, p.ID(), p2.ID(), \"expect proposal ID to match\")\n}\n\nfunc TestProposalStorageRemove(t *testing.T) {\n\tp, _ := commondao.NewProposal(1, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", testPropDef{})\n\ts := commondao.NewProposalStorage()\n\ts.Add(p)\n\n\ts.Remove(p.ID())\n\n\tuassert.Equal(t, 0, s.Size(), \"expect storage to be empty\")\n\tuassert.False(t, s.Has(p.ID()), \"expect proposal to be not found\")\n}\n\nfunc TestProposalStorageIterate(t *testing.T) {\n\tvar (\n\t\ti   int\n\t\tids = []uint64{22, 33, 44}\n\t\ts   = commondao.NewProposalStorage()\n\t)\n\n\tfor _, id := range ids {\n\t\tp, _ := commondao.NewProposal(id, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", testPropDef{})\n\t\ts.Add(p)\n\t}\n\n\ts.Iterate(0, s.Size(), false, func(p *commondao.Proposal) bool {\n\t\tuassert.Equal(t, ids[i], p.ID(), \"expect proposal ID to match\")\n\n\t\ti++\n\t\treturn i == s.Size()\n\t})\n\n\tuassert.Equal(t, len(ids), i, \"expect storage to iterate all proposals\")\n\n\ti = s.Size() - 1\n\ts.Iterate(0, s.Size(), true, func(p *commondao.Proposal) bool {\n\t\tuassert.Equal(t, ids[i], p.ID(), \"expect proposal ID to match\")\n\n\t\ti--\n\t\treturn i == -1\n\t})\n\n\tuassert.Equal(t, -1, i, \"expect storage to iterate all proposals in reverse order\")\n}\n"},{"name":"proposal_test.gno","body":"package commondao_test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestProposalNew(t *testing.T) {\n\tcases := []struct {\n\t\tname       string\n\t\tcreator    address\n\t\tdefinition commondao.ProposalDefinition\n\t\terr        error\n\t}{\n\t\t{\n\t\t\tname:       \"success\",\n\t\t\tcreator:    \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tdefinition: testPropDef{votingPeriod: time.Minute * 10},\n\t\t},\n\t\t{\n\t\t\tname:       \"invalid creator address\",\n\t\t\tcreator:    \"invalid\",\n\t\t\tdefinition: testPropDef{},\n\t\t\terr:        commondao.ErrInvalidCreatorAddress,\n\t\t},\n\t\t{\n\t\t\tname:    \"max custom vote choices exceeded\",\n\t\t\tcreator: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tdefinition: testPropDef{\n\t\t\t\tvoteChoices: make([]commondao.VoteChoice, commondao.MaxCustomVoteChoices+1),\n\t\t\t},\n\t\t\terr: commondao.ErrMaxCustomVoteChoices,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tid := uint64(1)\n\n\t\t\tp, err := commondao.NewProposal(id, tc.creator, tc.definition)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err, \"expected an error\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"unexpected error\")\n\t\t\tuassert.Equal(t, p.ID(), id)\n\t\t\tuassert.NotEqual(t, p.Definition(), nil)\n\t\t\tuassert.True(t, p.Status() == commondao.StatusActive)\n\t\t\tuassert.Equal(t, p.Creator(), tc.creator)\n\t\t\tuassert.False(t, p.CreatedAt().IsZero())\n\t\t\tuassert.NotEqual(t, p.VotingRecord(), nil)\n\t\t\tuassert.Empty(t, p.StatusReason())\n\t\t\tuassert.True(t, p.VotingDeadline() == p.CreatedAt().Add(tc.definition.VotingPeriod()))\n\t\t})\n\t}\n}\n\nfunc TestProposalVoteChoices(t *testing.T) {\n\tcases := []struct {\n\t\tname       string\n\t\tdefinition commondao.ProposalDefinition\n\t\tchoices    []commondao.VoteChoice\n\t}{\n\t\t{\n\t\t\tname:       \"custom choices\",\n\t\t\tdefinition: testPropDef{voteChoices: []commondao.VoteChoice{\"FOO\", \"BAR\", \"BAZ\"}},\n\t\t\tchoices: []commondao.VoteChoice{\n\t\t\t\t\"BAR\",\n\t\t\t\t\"BAZ\",\n\t\t\t\t\"FOO\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:       \"defaults because of empty custom choice list\",\n\t\t\tdefinition: testPropDef{voteChoices: []commondao.VoteChoice{}},\n\t\t\tchoices: []commondao.VoteChoice{\n\t\t\t\tcommondao.ChoiceAbstain,\n\t\t\t\tcommondao.ChoiceNo,\n\t\t\t\tcommondao.ChoiceYes,\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:       \"defaults because of single custom choice list\",\n\t\t\tdefinition: testPropDef{voteChoices: []commondao.VoteChoice{\"FOO\"}},\n\t\t\tchoices: []commondao.VoteChoice{\n\t\t\t\tcommondao.ChoiceAbstain,\n\t\t\t\tcommondao.ChoiceNo,\n\t\t\t\tcommondao.ChoiceYes,\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tp, _ := commondao.NewProposal(1, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", testPropDef{\n\t\t\t\tvoteChoices: tc.choices,\n\t\t\t})\n\n\t\t\tchoices := p.VoteChoices()\n\n\t\t\turequire.Equal(t, len(choices), len(tc.choices), \"expect vote choice count to match\")\n\t\t\tfor i, c := range choices {\n\t\t\t\turequire.True(t, tc.choices[i] == c, \"expect vote choice to match\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestIsQuorumReached(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tquorum  float64\n\t\tmembers []address\n\t\tvotes   []commondao.Vote\n\t\tfail    bool\n\t}{\n\t\t{\n\t\t\tname:   \"one third\",\n\t\t\tquorum: commondao.QuorumOneThird,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"one third no quorum\",\n\t\t\tquorum: commondao.QuorumOneThird,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t},\n\t\t\tfail: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"simple majority\",\n\t\t\tquorum: commondao.QuorumMoreThanHalf,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"simple majority no quorum\",\n\t\t\tquorum: commondao.QuorumMoreThanHalf,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tfail: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"two thirds\",\n\t\t\tquorum: commondao.QuorumTwoThirds,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"two thirds no quorum\",\n\t\t\tquorum: commondao.QuorumTwoThirds,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t\tfail: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"three fourths\",\n\t\t\tquorum: commondao.QuorumThreeFourths,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"three fourths no quorum\",\n\t\t\tquorum: commondao.QuorumThreeFourths,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\",\n\t\t\t\t\"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tfail: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"full\",\n\t\t\tquorum: commondao.QuorumFull,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"full no quorum\",\n\t\t\tquorum: commondao.QuorumFull,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t\tfail: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"no quorum with empty vote\",\n\t\t\tquorum: commondao.QuorumMoreThanHalf,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\tChoice:  commondao.ChoiceNone,\n\t\t\t\t},\n\t\t\t},\n\t\t\tfail: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"no quorum with abstention\",\n\t\t\tquorum: commondao.QuorumMoreThanHalf,\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\",\n\t\t\t\t\tChoice:  commondao.ChoiceAbstain,\n\t\t\t\t},\n\t\t\t},\n\t\t\tfail: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"invalid quorum percentage\",\n\t\t\tquorum: -1,\n\t\t\tfail:   true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tmembers := commondao.NewMemberStorage()\n\t\t\tstorage := commondao.MustNewReadonlyMemberStorage(members)\n\t\t\tfor _, m := range tc.members {\n\t\t\t\tmembers.Add(m)\n\t\t\t}\n\n\t\t\tvar record commondao.VotingRecord\n\t\t\tfor _, v := range tc.votes {\n\t\t\t\trecord.AddVote(v)\n\t\t\t}\n\n\t\t\tsuccess := commondao.IsQuorumReached(tc.quorum, record.Readonly(), *storage)\n\n\t\t\tif tc.fail {\n\t\t\t\tuassert.False(t, success, \"expect quorum to fail\")\n\t\t\t} else {\n\t\t\t\tuassert.True(t, success, \"expect quorum to succeed\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestProposalTally(t *testing.T) {\n\terrTest := errors.New(\"test\")\n\tcreator := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tcases := []struct {\n\t\tname   string\n\t\tsetup  func() *commondao.Proposal\n\t\tstatus commondao.ProposalStatus\n\t\terr    error\n\t}{\n\t\t{\n\t\t\tname: \"passed\",\n\t\t\tsetup: func() *commondao.Proposal {\n\t\t\t\tp, _ := commondao.NewProposal(1, creator, testPropDef{tallyResult: true})\n\t\t\t\treturn p\n\t\t\t},\n\t\t\tstatus: commondao.StatusPassed,\n\t\t},\n\t\t{\n\t\t\tname: \"rejected\",\n\t\t\tsetup: func() *commondao.Proposal {\n\t\t\t\tp, _ := commondao.NewProposal(1, creator, testPropDef{tallyResult: false})\n\t\t\t\treturn p\n\t\t\t},\n\t\t\tstatus: commondao.StatusRejected,\n\t\t},\n\t\t{\n\t\t\tname: \"proposal is not active\",\n\t\t\tsetup: func() *commondao.Proposal {\n\t\t\t\tp, _ := commondao.NewProposal(1, creator, testPropDef{tallyResult: true})\n\t\t\t\tp.Tally(commondao.NewMemberStorage())\n\t\t\t\treturn p\n\t\t\t},\n\t\t\terr: commondao.ErrStatusIsNotActive,\n\t\t},\n\t\t{\n\t\t\tname: \"tally error\",\n\t\t\tsetup: func() *commondao.Proposal {\n\t\t\t\tp, _ := commondao.NewProposal(1, creator, testPropDef{tallyErr: errTest})\n\t\t\t\treturn p\n\t\t\t},\n\t\t\terr: errTest,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tp := tc.setup()\n\t\t\tmembers := commondao.NewMemberStorage()\n\n\t\t\terr := p.Tally(members)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err)\n\t\t\turequire.Equal(t, string(tc.status), string(p.Status()))\n\t\t})\n\t}\n}\n\nfunc TestMustValidate(cur realm, t *testing.T) {\n\tuassert.NotPanics(t, cur, func() {\n\t\tcommondao.MustValidate(testPropDef{})\n\t}, \"expect validation to succeed\")\n\n\tuassert.PanicsWithMessage(t, cur, \"validable proposal definition is nil\", func() {\n\t\tcommondao.MustValidate(nil)\n\t}, \"expect validation to panic with nil definition\")\n\n\tuassert.PanicsWithMessage(t, cur, \"boom!\", func() {\n\t\tcommondao.MustValidate(testPropDef{validationErr: errors.New(\"boom!\")})\n\t}, \"expect validation to panic\")\n}\n\n// Executable non crossing proposal definition for unit tests\ntype testPropDef struct {\n\tvotingPeriod            time.Duration\n\ttallyResult             bool\n\tvalidationErr, tallyErr error\n\tvoteChoices             []commondao.VoteChoice\n}\n\nfunc (testPropDef) Title() string                 { return \"\" }\nfunc (testPropDef) Body() string                  { return \"\" }\nfunc (d testPropDef) VotingPeriod() time.Duration { return d.votingPeriod }\nfunc (d testPropDef) Validate() error             { return d.validationErr }\n\nfunc (d testPropDef) Tally(commondao.VotingContext) (bool, error) {\n\treturn d.tallyResult, d.tallyErr\n}\n\nfunc (d testPropDef) CustomVoteChoices() []commondao.VoteChoice {\n\tif len(d.voteChoices) \u003e 0 {\n\t\treturn d.voteChoices\n\t}\n\treturn []commondao.VoteChoice{commondao.ChoiceYes, commondao.ChoiceNo, commondao.ChoiceAbstain}\n}\n"},{"name":"record.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\t\"math\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\n// ErrVoteExists indicates that a user already voted.\nvar ErrVoteExists = errors.New(\"user already voted\")\n\ntype (\n\t// VoteIterFn defines a callback to iterate votes.\n\tVoteIterFn func(Vote) (stop bool)\n\n\t// VotesCountIterFn defines a callback to iterate voted choices.\n\tVotesCountIterFn func(_ VoteChoice, voteCount int) (stop bool)\n\n\t// Vote defines a single vote.\n\tVote struct {\n\t\t// Address is the address of the user that this vote belons to.\n\t\tAddress address\n\n\t\t// Choice contains the voted choice.\n\t\tChoice VoteChoice\n\n\t\t// Reason contains an optional reason for the vote.\n\t\tReason string\n\n\t\t// Context can store any custom voting values related to the vote.\n\t\t//\n\t\t// Warning: When using context be careful if references/pointers are\n\t\t// assigned to it because they could potentially be accessed anywhere,\n\t\t// which could lead to unwanted indirect modifications.\n\t\tContext any\n\t}\n)\n\n// ReadonlyVotingRecord defines an read only voting record.\ntype ReadonlyVotingRecord struct {\n\tvotes bptree.BPTree // string(address) -\u003e Vote\n\tcount bptree.BPTree // string(choice) -\u003e int\n}\n\n// Size returns the total number of votes that record contains.\nfunc (r ReadonlyVotingRecord) Size() int {\n\treturn r.votes.Size()\n}\n\n// Iterate iterates voting record votes.\nfunc (r ReadonlyVotingRecord) Iterate(offset, count int, reverse bool, fn VoteIterFn) bool {\n\tcb := func(_ string, v any) bool { return fn(v.(Vote)) }\n\tif reverse {\n\t\treturn r.votes.ReverseIterateByOffset(offset, count, cb)\n\t}\n\treturn r.votes.IterateByOffset(offset, count, cb)\n}\n\n// IterateVotesCount iterates voted choices with the amount of votes submited for each.\nfunc (r ReadonlyVotingRecord) IterateVotesCount(fn VotesCountIterFn) bool {\n\treturn r.count.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\treturn fn(VoteChoice(k), v.(int))\n\t})\n}\n\n// VoteCount returns the number of votes for a single voting choice.\nfunc (r ReadonlyVotingRecord) VoteCount(c VoteChoice) int {\n\tif v := r.count.Get(string(c)); v != nil {\n\t\treturn v.(int)\n\t}\n\treturn 0\n}\n\n// HasVoted checks if an account already voted.\nfunc (r ReadonlyVotingRecord) HasVoted(user address) bool {\n\treturn r.votes.Has(user.String())\n}\n\n// GetVote returns a vote.\nfunc (r ReadonlyVotingRecord) GetVote(user address) (_ Vote, found bool) {\n\tif v := r.votes.Get(user.String()); v != nil {\n\t\treturn v.(Vote), true\n\t}\n\treturn Vote{}, false\n}\n\n// VotingRecord stores accounts that voted and vote choices.\ntype VotingRecord struct {\n\tReadonlyVotingRecord\n}\n\n// Readonly returns a read only voting record.\nfunc (r VotingRecord) Readonly() ReadonlyVotingRecord {\n\treturn r.ReadonlyVotingRecord\n}\n\n// AddVote adds a vote to the voting record.\n// If a vote for the same user already exists is overwritten.\nfunc (r *VotingRecord) AddVote(vote Vote) (updated bool) {\n\t// Get previous member vote if it exists\n\tv := r.votes.Get(vote.Address.String())\n\n\t// When a previous vote exists update counter for the previous choice\n\tupdated = r.votes.Set(vote.Address.String(), vote)\n\tif updated {\n\t\tprev := v.(Vote)\n\t\tr.count.Set(string(prev.Choice), r.VoteCount(prev.Choice)-1)\n\t}\n\n\tr.count.Set(string(vote.Choice), r.VoteCount(vote.Choice)+1)\n\treturn\n}\n\n// FindMostVotedChoice returns the most voted choice.\n// ChoiceNone is returned when there is a tie between different\n// voting choices or when the voting record has are no votes.\nfunc FindMostVotedChoice(r ReadonlyVotingRecord) VoteChoice {\n\tvar (\n\t\tchoice                  VoteChoice\n\t\tcurrentCount, prevCount int\n\t)\n\n\tr.IterateVotesCount(func(c VoteChoice, count int) bool {\n\t\tif currentCount \u003c= count {\n\t\t\tchoice = c\n\t\t\tprevCount = currentCount\n\t\t\tcurrentCount = count\n\t\t}\n\t\treturn false\n\t})\n\n\tif prevCount \u003c currentCount {\n\t\treturn choice\n\t}\n\treturn ChoiceNone\n}\n\n// SelectChoiceByAbsoluteMajority select the vote choice by absolute majority.\n// Vote choice is a majority when chosen by more than half of the votes.\n// Absolute majority considers abstentions when counting votes.\nfunc SelectChoiceByAbsoluteMajority(r ReadonlyVotingRecord, membersCount int) (VoteChoice, bool) {\n\tchoice := FindMostVotedChoice(r)\n\tif choice != ChoiceNone \u0026\u0026 r.VoteCount(choice) \u003e int(membersCount/2) {\n\t\treturn choice, true\n\t}\n\treturn ChoiceNone, false\n}\n\n// SelectChoiceBySuperMajority select the vote choice by super majority using a 2/3s threshold.\n// Abstentions are considered when calculating the super majority choice.\nfunc SelectChoiceBySuperMajority(r ReadonlyVotingRecord, membersCount int) (VoteChoice, bool) {\n\tif membersCount \u003c 3 {\n\t\treturn ChoiceNone, false\n\t}\n\n\tchoice := FindMostVotedChoice(r)\n\tif choice != ChoiceNone \u0026\u0026 r.VoteCount(choice) \u003e= int(math.Ceil((2*float64(membersCount))/3)) {\n\t\treturn choice, true\n\t}\n\treturn ChoiceNone, false\n}\n\n// SelectChoiceByPlurality selects the vote choice by plurality.\n// The choice will be considered a majority if it has votes and if there is no other\n// choice with the same number of votes. A tie won't be considered majority.\nfunc SelectChoiceByPlurality(r ReadonlyVotingRecord) (VoteChoice, bool) {\n\tvar (\n\t\tchoice       VoteChoice\n\t\tcurrentCount int\n\t\tisMajority   bool\n\t)\n\n\tr.IterateVotesCount(func(c VoteChoice, count int) bool {\n\t\t// Don't consider explicit abstentions or invalid votes\n\t\tif c == ChoiceAbstain || c == ChoiceNone {\n\t\t\treturn false\n\t\t}\n\n\t\tif currentCount \u003c count {\n\t\t\tchoice = c\n\t\t\tcurrentCount = count\n\t\t\tisMajority = true\n\t\t} else if currentCount == count {\n\t\t\tisMajority = false\n\t\t}\n\t\treturn false\n\t})\n\n\tif isMajority {\n\t\treturn choice, true\n\t}\n\treturn ChoiceNone, false\n}\n\n// CollectVotes returns an voting record containing votes of members from one or more groups.\n// Returned tree uses member account address as key and `commondao.Vote` as value.\nfunc CollectVotes(ctx VotingContext, groups ...string) (*VotingRecord, error) {\n\tif len(groups) == 0 {\n\t\treturn nil, errors.New(\"one or more group names are required to collect votes\")\n\t}\n\n\tvar (\n\t\tvotes    VotingRecord\n\t\tgrouping = ctx.Members.Grouping()\n\t)\n\n\tfor _, name := range groups {\n\t\tgroup, found := grouping.Get(name)\n\t\tif !found {\n\t\t\treturn nil, errors.New(\"member group not found: \" + name)\n\t\t}\n\n\t\tgroup.Members().IterateByOffset(0, group.Members().Size(), func(member address) bool {\n\t\t\tv, found := ctx.VotingRecord.GetVote(member)\n\t\t\tif found {\n\t\t\t\tvotes.AddVote(v)\n\t\t\t}\n\t\t\treturn false\n\t\t})\n\t}\n\treturn \u0026votes, nil\n}\n"},{"name":"record_test.gno","body":"package commondao_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc TestVotingRecordDefaults(t *testing.T) {\n\tvar (\n\t\trecord commondao.VotingRecord\n\t\tuser   address = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\t)\n\n\tuassert.Equal(t, record.Size(), 0)\n\tuassert.Equal(t, record.VoteCount(commondao.ChoiceYes), 0)\n\tuassert.Equal(t, record.VoteCount(commondao.ChoiceNo), 0)\n\tuassert.Equal(t, record.VoteCount(commondao.ChoiceAbstain), 0)\n\tuassert.False(t, record.HasVoted(user))\n}\n\nfunc TestVotingRecordAddVote(t *testing.T) {\n\tcases := []struct {\n\t\tname                            string\n\t\tsetup                           func(*commondao.VotingRecord)\n\t\tvotes                           []commondao.Vote\n\t\tyesCount, noCount, abstainCount int\n\t\tupdated                         bool\n\t}{\n\t\t{\n\t\t\tname: \"single vote\",\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tyesCount: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple votes\",\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceAbstain,\n\t\t\t\t},\n\t\t\t},\n\t\t\tyesCount:     1,\n\t\t\tnoCount:      2,\n\t\t\tabstainCount: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"vote exists\",\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceAbstain,\n\t\t\t\t})\n\t\t\t},\n\t\t\tyesCount:     1,\n\t\t\tabstainCount: 0,\n\t\t\tupdated:      true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar (\n\t\t\t\trecord  commondao.VotingRecord\n\t\t\t\tupdated bool\n\t\t\t)\n\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026record)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.votes {\n\t\t\t\tupdated = updated || record.AddVote(v)\n\t\t\t}\n\n\t\t\turequire.Equal(t, updated, tc.updated, \"expect vote to be updated\")\n\t\t\turequire.Equal(t, record.Size(), len(tc.votes), \"expect record size to match\")\n\n\t\t\tvar i int\n\t\t\trecord.Iterate(0, record.Size(), false, func(v commondao.Vote) bool {\n\t\t\t\tuassert.Equal(t, v.Address, tc.votes[i].Address)\n\t\t\t\tuassert.Equal(t, string(v.Choice), string(tc.votes[i].Choice))\n\t\t\t\tuassert.True(t, record.HasVoted(v.Address))\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tuassert.Equal(t, record.VoteCount(commondao.ChoiceYes), tc.yesCount, \"expect YES vote count to match\")\n\t\t\tuassert.Equal(t, record.VoteCount(commondao.ChoiceNo), tc.noCount, \"expect NO vote count to match\")\n\t\t\tuassert.Equal(t, record.VoteCount(commondao.ChoiceAbstain), tc.abstainCount, \"expect ABSTAIN vote count to match\")\n\t\t})\n\t}\n}\n\nfunc TestFindMostVotedChoice(t *testing.T) {\n\tcases := []struct {\n\t\tname   string\n\t\tsetup  func(*commondao.VotingRecord)\n\t\tchoice commondao.VoteChoice\n\t}{\n\t\t{\n\t\t\tname:   \"no votes\",\n\t\t\tchoice: commondao.ChoiceNone,\n\t\t},\n\t\t{\n\t\t\tname: \"one vote\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice: commondao.ChoiceYes,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple votes\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice: commondao.ChoiceNo,\n\t\t},\n\t\t{\n\t\t\tname: \"tie\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice: commondao.ChoiceNone,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar record commondao.VotingRecord\n\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026record)\n\t\t\t}\n\n\t\t\tchoice := commondao.FindMostVotedChoice(record.Readonly())\n\n\t\t\tuassert.Equal(t, string(choice), string(tc.choice))\n\t\t})\n\t}\n}\n\nfunc TestSelectChoiceByAbsoluteMajority(t *testing.T) {\n\tcases := []struct {\n\t\tname         string\n\t\tsetup        func(*commondao.VotingRecord)\n\t\tchoice       commondao.VoteChoice\n\t\tmembersCount int\n\t\tsuccess      bool\n\t}{\n\t\t{\n\t\t\tname:         \"no votes\",\n\t\t\tchoice:       commondao.ChoiceNone,\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      false,\n\t\t},\n\t\t{\n\t\t\tname: \"majority\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:       commondao.ChoiceYes,\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      true,\n\t\t},\n\t\t{\n\t\t\tname: \"no majority\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:       \"\",\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      false,\n\t\t},\n\t\t{\n\t\t\tname: \"majority with abstain vote\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceAbstain,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:       commondao.ChoiceYes,\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar record commondao.VotingRecord\n\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026record)\n\t\t\t}\n\n\t\t\tchoice, success := commondao.SelectChoiceByAbsoluteMajority(record.Readonly(), tc.membersCount)\n\n\t\t\tuassert.Equal(t, string(tc.choice), string(choice), \"choice\")\n\t\t\tuassert.Equal(t, tc.success, success, \"success\")\n\t\t})\n\t}\n}\n\nfunc TestSelectChoiceBySuperMajority(t *testing.T) {\n\tcases := []struct {\n\t\tname         string\n\t\tsetup        func(*commondao.VotingRecord)\n\t\tchoice       commondao.VoteChoice\n\t\tmembersCount int\n\t\tsuccess      bool\n\t}{\n\t\t{\n\t\t\tname:         \"no votes\",\n\t\t\tchoice:       commondao.ChoiceNone,\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      false,\n\t\t},\n\t\t{\n\t\t\tname: \"majority\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:       commondao.ChoiceYes,\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      true,\n\t\t},\n\t\t{\n\t\t\tname: \"no majority\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:       \"\",\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      false,\n\t\t},\n\t\t{\n\t\t\tname: \"majority with abstain vote\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceAbstain,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:       commondao.ChoiceYes,\n\t\t\tmembersCount: 3,\n\t\t\tsuccess:      true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar record commondao.VotingRecord\n\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026record)\n\t\t\t}\n\n\t\t\tchoice, success := commondao.SelectChoiceBySuperMajority(record.Readonly(), tc.membersCount)\n\n\t\t\tuassert.Equal(t, string(tc.choice), string(choice), \"choice\")\n\t\t\tuassert.Equal(t, tc.success, success, \"success\")\n\t\t})\n\t}\n}\n\nfunc TestSelectChoiceByPlurality(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tsetup   func(*commondao.VotingRecord)\n\t\tchoice  commondao.VoteChoice\n\t\tsuccess bool\n\t}{\n\t\t{\n\t\t\tname:    \"no votes\",\n\t\t\tchoice:  commondao.ChoiceNone,\n\t\t\tsuccess: false,\n\t\t},\n\t\t{\n\t\t\tname: \"plurality\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:  commondao.ChoiceYes,\n\t\t\tsuccess: true,\n\t\t},\n\t\t{\n\t\t\tname: \"no plurality\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:  \"\",\n\t\t\tsuccess: false,\n\t\t},\n\t\t{\n\t\t\tname: \"plurality with abstain vote\",\n\t\t\tsetup: func(r *commondao.VotingRecord) {\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t})\n\t\t\t\tr.AddVote(commondao.Vote{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceAbstain,\n\t\t\t\t})\n\t\t\t},\n\t\t\tchoice:  commondao.ChoiceYes,\n\t\t\tsuccess: true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar record commondao.VotingRecord\n\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026record)\n\t\t\t}\n\n\t\t\tchoice, success := commondao.SelectChoiceByPlurality(record.Readonly())\n\n\t\t\tuassert.Equal(t, string(tc.choice), string(choice), \"choice\")\n\t\t\tuassert.Equal(t, tc.success, success, \"success\")\n\t\t})\n\t}\n}\n\nfunc TestCollectVotes(t *testing.T) {\n\tcases := []struct {\n\t\tname   string\n\t\tsetup  func() commondao.MemberStorage\n\t\tvotes  []commondao.Vote\n\t\tgroups []string\n\t\terror  string\n\t}{\n\t\t{\n\t\t\tname: \"one group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := commondao.NewMemberStorageWithGrouping()\n\t\t\t\tone, _ := s.Grouping().Add(\"one\")\n\t\t\t\tone.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tone.Members().Add(\"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroups: []string{\"one\"},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"two groups\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := commondao.NewMemberStorageWithGrouping()\n\t\t\t\tone, _ := s.Grouping().Add(\"one\")\n\t\t\t\tone.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tone.Members().Add(\"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\")\n\t\t\t\ttwo, _ := s.Grouping().Add(\"two\")\n\t\t\t\ttwo.Members().Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroups: []string{\"one\", \"two\"},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:  \"no group names\",\n\t\t\tsetup: func() commondao.MemberStorage { return commondao.NewMemberStorageWithGrouping() },\n\t\t\terror: \"one or more group names are required to collect votes\",\n\t\t},\n\t\t{\n\t\t\tname:   \"member group not found\",\n\t\t\tsetup:  func() commondao.MemberStorage { return commondao.NewMemberStorageWithGrouping() },\n\t\t\tgroups: []string{\"foo\"},\n\t\t\terror:  \"member group not found: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar r commondao.VotingRecord\n\t\t\tfor _, v := range tc.votes {\n\t\t\t\tr.AddVote(v)\n\t\t\t}\n\n\t\t\tstorage := tc.setup()\n\t\t\tctx := commondao.MustNewVotingContext(\u0026r, storage)\n\n\t\t\t// Act\n\t\t\tvotes, err := commondao.CollectVotes(ctx, tc.groups...)\n\n\t\t\t// Assert\n\t\t\tif tc.error != \"\" {\n\t\t\t\turequire.ErrorContains(t, err, tc.error)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"unexpected error\")\n\t\t\turequire.Equal(t, len(tc.votes), votes.Size(), \"expect number of votes to match\")\n\n\t\t\tvar i int\n\t\t\tvotes.Iterate(0, votes.Size(), false, func(v commondao.Vote) bool {\n\t\t\t\twant := tc.votes[i]\n\n\t\t\t\turequire.Equal(t, want.Address, v.Address, \"expect vote address to match\")\n\t\t\t\turequire.Equal(t, string(want.Choice), string(v.Choice), \"expect vote choice to match\")\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"},{"name":"voting_context.gno","body":"package commondao\n\nimport \"errors\"\n\n// VotingContext contains current voting context, which includes a voting\n// record for the votes that has been submitted for a proposal and also\n// the list of DAO members.\ntype VotingContext struct {\n\tVotingRecord ReadonlyVotingRecord\n\tMembers      ReadonlyMemberStorage\n}\n\n// NewVotingContext creates a new voting context.\nfunc NewVotingContext(r *VotingRecord, s MemberStorage) (VotingContext, error) {\n\tif r == nil {\n\t\treturn VotingContext{}, errors.New(\"voting record is required\")\n\t}\n\n\tif s == nil {\n\t\treturn VotingContext{}, errors.New(\"member storage is required\")\n\t}\n\n\tmembers := MustNewReadonlyMemberStorage(s)\n\treturn VotingContext{\n\t\tVotingRecord: r.Readonly(),\n\t\tMembers:      *members,\n\t}, nil\n}\n\n// MustNewVotingContext creates a new voting context or panics on error.\nfunc MustNewVotingContext(r *VotingRecord, s MemberStorage) VotingContext {\n\tctx, err := NewVotingContext(r, s)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn ctx\n}\n"},{"name":"z_commondao_execute_0_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nconst member address = \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\" // @devx\n\nvar (\n\tdao          *commondao.CommonDAO\n\tproposal     *commondao.Proposal\n\texecutorPath string\n)\n\ntype testPropDef struct{}\n\nfunc (testPropDef) Title() string                               { return \"\" }\nfunc (testPropDef) Body() string                                { return \"\" }\nfunc (testPropDef) VotingPeriod() time.Duration                 { return 0 }\nfunc (testPropDef) Tally(commondao.VotingContext) (bool, error) { return true, nil }\n\nfunc (testPropDef) Executor() commondao.ExecFunc {\n\treturn func(realm) error {\n\t\texecutorPath = unsafe.PreviousRealm().PkgPath()\n\t\treturn nil\n\t}\n}\n\nfunc init() {\n\tdao = commondao.New(commondao.WithMember(member))\n\tproposal = dao.MustPropose(member, testPropDef{})\n}\n\nfunc main(cur realm) {\n\t// Make sure proposal is executed in a realm different than the one where definition has been defined\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/testing/dao\"))\n\n\terr := dao.Execute(proposal.ID(), cur)\n\n\tprintln(err == nil)\n\tprintln(string(proposal.Status()))\n\tprintln(executorPath)\n}\n\n// Output:\n// true\n// executed\n// gno.land/r/testing/dao\n"},{"name":"z_commondao_execute_1_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nconst member address = \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\" // @devx\n\nvar (\n\tdao      *commondao.CommonDAO\n\tproposal *commondao.Proposal\n)\n\ntype testPropDef struct{}\n\nfunc (testPropDef) Title() string                               { return \"\" }\nfunc (testPropDef) Body() string                                { return \"\" }\nfunc (testPropDef) VotingPeriod() time.Duration                 { return 0 }\nfunc (testPropDef) Tally(commondao.VotingContext) (bool, error) { return true, nil }\n\nfunc (testPropDef) Executor() commondao.ExecFunc {\n\treturn func(realm) error {\n\t\treturn errors.New(\"test error\")\n\t}\n}\n\nfunc init() {\n\tdao = commondao.New(commondao.WithMember(member))\n\tproposal = dao.MustPropose(member, testPropDef{})\n}\n\nfunc main(cur realm) {\n\terr := dao.Execute(proposal.ID(), cur)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tprintln(string(proposal.Status()))\n\tprintln(proposal.StatusReason())\n}\n\n// Output:\n// failed\n// test error\n"},{"name":"z_commondao_execute_2_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nconst member address = \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\" // @devx\n\nvar (\n\tdao      *commondao.CommonDAO\n\tproposal *commondao.Proposal\n\texecuted bool\n)\n\ntype testPropDef struct{}\n\nfunc (testPropDef) Title() string                               { return \"\" }\nfunc (testPropDef) Body() string                                { return \"\" }\nfunc (testPropDef) VotingPeriod() time.Duration                 { return time.Hour } // Voting ends in 1 hour\nfunc (testPropDef) Tally(commondao.VotingContext) (bool, error) { return true, nil }\n\nfunc (testPropDef) Executor() commondao.ExecFunc {\n\treturn func(realm) error {\n\t\texecuted = true\n\t\treturn nil\n\t}\n}\n\nfunc init() {\n\tdao = commondao.New(\n\t\tcommondao.WithMember(member),\n\t\tcommondao.DisableVotingDeadlineCheck(), // Disable to be able to execute before voting deadline\n\t)\n\tproposal = dao.MustPropose(member, testPropDef{})\n}\n\nfunc main(cur realm) {\n\t// Make sure proposal is executed in a realm different than the one where definition has been defined\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/testing/dao\"))\n\n\t// Should be able to execute before voting deadline because deadline check is disabled\n\terr := dao.Execute(proposal.ID(), cur)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tprintln(executed)\n\tprintln(string(proposal.Status()))\n}\n\n// Output:\n// true\n// executed\n"},{"name":"z_commondao_must_execute_0_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\ntype testPropDef struct{}\n\nfunc (testPropDef) Title() string                               { return \"\" }\nfunc (testPropDef) Body() string                                { return \"\" }\nfunc (testPropDef) VotingPeriod() time.Duration                 { return 0 }\nfunc (testPropDef) Executor() commondao.ExecFunc                { return nil }\nfunc (testPropDef) Tally(commondao.VotingContext) (bool, error) { return true, nil }\n\nfunc main(cur realm) {\n\t// Make sure proposal is executed in a realm different than the one where definitions has been defined\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/testing/dao\"))\n\n\tcommondao.MustExecute(testPropDef{}, cur)\n\tprintln(\"ok\")\n}\n\n// Output:\n// ok\n"},{"name":"z_commondao_must_execute_1_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\ntype testPropDef struct{}\n\nfunc (testPropDef) Title() string                               { return \"\" }\nfunc (testPropDef) Body() string                                { return \"\" }\nfunc (testPropDef) VotingPeriod() time.Duration                 { return 0 }\nfunc (testPropDef) Tally(commondao.VotingContext) (bool, error) { return true, nil }\n\nfunc (testPropDef) Executor() commondao.ExecFunc {\n\treturn func(realm) error {\n\t\treturn errors.New(\"boom!\")\n\t}\n}\n\nfunc main(cur realm) {\n\t// Make sure proposal is executed in a realm different than the one where definition has been defined\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/testing/dao\"))\n\n\tcommondao.MustExecute(testPropDef{}, cur)\n}\n\n// Error:\n// boom!\n"},{"name":"z_commondao_must_execute_2_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc main(cur realm) {\n\tcommondao.MustExecute(nil, cur)\n}\n\n// Error:\n// executable proposal definition is nil\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"TqlsdxDfOwXxquqAai5SXBeqKhdst7Tk/DLy8bwmN8g7HvkHkuqiXPeowxiN+LOHzoEmVNWihtDPbo6+PQqkAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"expect","path":"gno.land/p/jeronimoalbi/expect","files":[{"name":"boolean.gno","body":"package expect\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// NewBooleanChecker creates a new checker of boolean values\nfunc NewBooleanChecker(ctx Context, value bool) BooleanChecker {\n\treturn BooleanChecker{ctx, value}\n}\n\n// BooleanChecker asserts boolean values.\ntype BooleanChecker struct {\n\tctx   Context\n\tvalue bool\n}\n\n// Not negates the next called expectation.\nfunc (c BooleanChecker) Not() BooleanChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// ToEqual asserts that current value is equal to an expected value.\nfunc (c BooleanChecker) ToEqual(v bool) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value == v, func(ctx Context) string {\n\t\tgot := formatBoolean(c.value)\n\t\tif !ctx.IsNegated() {\n\t\t\twant := formatBoolean(v)\n\t\t\treturn ufmt.Sprintf(\"Expected values to match\\nGot: %s\\nWant: %s\", got, want)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected values to be different\\nGot: %s\", got)\n\t})\n}\n\n// ToBeFalsy asserts that current value is falsy.\nfunc (c BooleanChecker) ToBeFalsy() {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(!c.value, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn \"Expected value to be falsy\"\n\t\t}\n\t\treturn \"Expected value not to be falsy\"\n\t})\n}\n\n// ToBeTruthy asserts that current value is truthy.\nfunc (c BooleanChecker) ToBeTruthy() {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn \"Expected value to be truthy\"\n\t\t}\n\t\treturn \"Expected value not to be truthy\"\n\t})\n}\n\nfunc asBoolean(value any) (bool, error) {\n\tif value == nil {\n\t\treturn false, nil\n\t}\n\n\tvar s string\n\tswitch v := value.(type) {\n\tcase bool:\n\t\treturn v, nil\n\tcase string:\n\t\ts = v\n\tcase []byte:\n\t\ts = string(v)\n\tcase Stringer:\n\t\ts = v.String()\n\tdefault:\n\t\treturn false, ErrIncompatibleType\n\t}\n\n\tif s != \"\" {\n\t\treturn strconv.ParseBool(s)\n\t}\n\treturn false, nil\n}\n\nfunc formatBoolean(value bool) string {\n\treturn strconv.FormatBool(value)\n}\n"},{"name":"boolean_test.gno","body":"package expect_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nfunc TestBooleanChecker(t *testing.T) {\n\tt.Run(\"to be truthy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewBooleanChecker(ctx, true).ToBeTruthy()\n\t})\n\n\tt.Run(\"not to be truthy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewBooleanChecker(ctx, false).Not().ToBeTruthy()\n\t})\n\n\tt.Run(\"to be falsy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewBooleanChecker(ctx, false).ToBeFalsy()\n\t})\n\n\tt.Run(\"not to be falsy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewBooleanChecker(ctx, true).Not().ToBeFalsy()\n\t})\n}\n"},{"name":"context.gno","body":"package expect\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst defaultAssertFailMsg = \"assert expectation failed\"\n\n// NewContext creates a new testing context.\nfunc NewContext(t TestingT) Context {\n\treturn Context{t: t}\n}\n\n// Context preserves the current testing context.\ntype Context struct {\n\tt       TestingT\n\tnegated bool\n\tprefix  string\n}\n\n// T returns context's testing T instance.\nfunc (c Context) T() TestingT {\n\tif c.t == nil {\n\t\tpanic(\"expect: context is not initialized\")\n\t}\n\treturn c.t\n}\n\n// Prefix returns context's error prefix.\nfunc (c Context) Prefix() string {\n\treturn c.prefix\n}\n\n// IsNegated checks if current context negates current assert expectations.\nfunc (c Context) IsNegated() bool {\n\treturn c.negated\n}\n\n// CheckExpectation checks an assert expectation and calls a callback on fail.\n// It returns true when the asserted expectation fails.\n// Callback is called when a negated assertion succeeds or when non negated assertion fails.\nfunc (c Context) CheckExpectation(success bool, cb func(Context) string) bool {\n\tfailed := (c.negated \u0026\u0026 success) || (!c.negated \u0026\u0026 !success)\n\tif failed {\n\t\tmsg := cb(c)\n\t\tif strings.TrimSpace(msg) == \"\" {\n\t\t\tmsg = defaultAssertFailMsg\n\t\t}\n\n\t\tc.Fail(msg)\n\t}\n\treturn failed\n}\n\n// Fail makes the current test fail with a custom message.\nfunc (c Context) Fail(msg string, args ...any) {\n\tif c.prefix != \"\" {\n\t\tmsg = c.prefix + \" - \" + msg\n\t}\n\n\tc.t.Fatalf(msg, args...)\n}\n\n// TestingT defines a minimal interface for `testing.T` instances.\ntype TestingT interface {\n\tHelper()\n\tFatal(args ...any)\n\tFatalf(format string, args ...any)\n}\n\n// MockTestingT creates a new testing mock that writes testing output to a string builder.\nfunc MockTestingT(output *strings.Builder) TestingT {\n\treturn \u0026testingT{output}\n}\n\ntype testingT struct{ buf *strings.Builder }\n\nfunc (testingT) Helper()                          {}\nfunc (t testingT) Fatal(args ...any)              { t.buf.WriteString(ufmt.Sprintln(args...)) }\nfunc (t testingT) Fatalf(fmt string, args ...any) { t.buf.WriteString(ufmt.Sprintf(fmt+\"\\n\", args...)) }\n"},{"name":"doc.gno","body":"// Package expect provides testing support for packages and realms.\n//\n// The opinionated approach taken on this package for testing is to use function chaining and\n// semanthics to hopefully make unit and file testing fun. Focus is not on speed as there are\n// other packages that would run tests faster like the official `uassert` or `urequire` packages.\n//\n// Values can be asserted using the `Value()` function, for example:\n//\n//\tfunc TestFoo(t *testing.T) {\n//\t  got := 42\n//\t  expect.Value(t, got).ToEqual(42)\n//\t  expect.Value(t, got).Not().ToEqual(0)\n//\n//\t  expect.Value(t, \"foo\").ToEqual(\"foo\")\n//\t  expect.Value(t, 42).AsInt().Not().ToBeGreaterThan(50)\n//\t  expect.Value(t, \"TRUE\").AsBoolean().ToBeTruthy()\n//\t}\n//\n// Functions can also be used to assert returned values, errors or panics.\n//\n// Package supports four type of functions:\n//\n//   - func()\n//   - func() any\n//   - func() error\n//   - func() (any, error)\n//\n// Functions can be asserted using the `Func()` function, for example:\n//\n//\tfunc TestFoo(t *testing.T) {\n//\t  expect.Func(t, func() {\n//\t    panic(\"Boom!\")\n//\t  }).ToPanic().WithMessage(\"Boom!\")\n//\n//\t  wantErr := errors.New(\"Boom!\")\n//\t  expect.Func(t, func() error {\n//\t    return wantErr\n//\t  }).ToFail().WithMessage(\"Boom!\")\n//\n//\t  expect.Func(t, func() error {\n//\t    return wantErr\n//\t  }).ToFail().WithError(wantErr)\n//\t}\npackage expect\n"},{"name":"error.gno","body":"package expect\n\nimport \"gno.land/p/nt/ufmt/v0\"\n\n// NewErrorChecker creates a new checker of errors.\nfunc NewErrorChecker(ctx Context, err error) ErrorChecker {\n\treturn ErrorChecker{ctx, err}\n}\n\n// ErrorChecker asserts error values.\ntype ErrorChecker struct {\n\tctx Context\n\terr error\n}\n\n// Not negates the next called expectation.\nfunc (c ErrorChecker) Not() ErrorChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// WithMessage asserts that current error contains an expected message.\nfunc (c ErrorChecker) WithMessage(msg string) {\n\tc.ctx.T().Helper()\n\n\tif c.err == nil {\n\t\tc.ctx.Fail(\"Expected an error with message\\nGot: nil\\nWant: %s\", msg)\n\t\treturn\n\t}\n\n\tNewMessageChecker(c.ctx, c.err.Error(), MessageTypeError).WithMessage(msg)\n}\n\n// WithError asserts that current error message is the same as an expected error.\nfunc (c ErrorChecker) WithError(err error) {\n\tc.ctx.T().Helper()\n\n\tif c.err == nil {\n\t\tif err != nil {\n\t\t\tc.ctx.Fail(\"Expected an error\\nGot: nil\\nWant: %s\", err.Error())\n\t\t}\n\t\treturn\n\t}\n\n\tgot := c.err.Error()\n\tc.ctx.CheckExpectation(got == err.Error(), func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected errors to match\\nGot: %s\\nWant: %s\", got, err.Error())\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected errors to be different\\nGot: %s\", got)\n\t})\n}\n"},{"name":"float.gno","body":"package expect\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// NewFloatChecker creates a new checker of float64 values.\nfunc NewFloatChecker(ctx Context, value float64) FloatChecker {\n\treturn FloatChecker{ctx, value}\n}\n\n// FloatChecker asserts float64 values.\ntype FloatChecker struct {\n\tctx   Context\n\tvalue float64\n}\n\n// Not negates the next called expectation.\nfunc (c FloatChecker) Not() FloatChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// ToEqual asserts that current value is equal to an expected value.\nfunc (c FloatChecker) ToEqual(value float64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value == value, func(ctx Context) string {\n\t\tgot := formatFloat(c.value)\n\t\tif !ctx.IsNegated() {\n\t\t\twant := formatFloat(value)\n\t\t\treturn ufmt.Sprintf(\"Expected values to match\\nGot: %s\\nWant: %s\", got, want)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to be different\\nGot: %s\", got)\n\t})\n}\n\n// ToBeGreaterThan asserts that current value is greater than an expected value.\nfunc (c FloatChecker) ToBeGreaterThan(value float64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003e value, func(ctx Context) string {\n\t\tgot := formatFloat(c.value)\n\t\twant := formatFloat(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be gerater than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be greater than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.\nfunc (c FloatChecker) ToBeGreaterOrEqualThan(value float64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003e= value, func(ctx Context) string {\n\t\tgot := formatFloat(c.value)\n\t\twant := formatFloat(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be greater or equal than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be greater or equal than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeLowerThan asserts that current value is lower than an expected value.\nfunc (c FloatChecker) ToBeLowerThan(value float64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003c value, func(ctx Context) string {\n\t\tgot := formatFloat(c.value)\n\t\twant := formatFloat(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be lower than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be lower than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.\nfunc (c FloatChecker) ToBeLowerOrEqualThan(value float64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003c= value, func(ctx Context) string {\n\t\tgot := formatFloat(c.value)\n\t\twant := formatFloat(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be lower or equal than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be lower or equal than %s\\nGot: %s\", want, got)\n\t})\n}\n\nfunc formatFloat(value float64) string {\n\treturn strconv.FormatFloat(value, 'g', -1, 64)\n}\n\nfunc asFloat(value any) (float64, error) {\n\tswitch v := value.(type) {\n\tcase float32:\n\t\treturn float64(v), nil\n\tcase float64:\n\t\treturn v, nil\n\tdefault:\n\t\treturn 0, ErrIncompatibleType\n\t}\n}\n"},{"name":"float_test.gno","body":"package expect_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nfunc TestFloatChecker(t *testing.T) {\n\tt.Run(\"to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).ToEqual(1.2)\n\t})\n\n\tt.Run(\"not to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).Not().ToEqual(3.4)\n\t})\n\n\tt.Run(\"to be greater than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).ToBeGreaterThan(1)\n\t})\n\n\tt.Run(\"not to be greater than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).Not().ToBeGreaterThan(1.3)\n\t})\n\n\tt.Run(\"to be greater or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).ToBeGreaterOrEqualThan(1.2)\n\t\texpect.NewFloatChecker(ctx, 1.2).ToBeGreaterOrEqualThan(1.1)\n\t})\n\n\tt.Run(\"not to be greater or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).Not().ToBeGreaterOrEqualThan(1.3)\n\t})\n\n\tt.Run(\"to be lower than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).ToBeLowerThan(1.3)\n\t})\n\n\tt.Run(\"not to be lower than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).Not().ToBeLowerThan(1)\n\t})\n\n\tt.Run(\"to be lower or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).ToBeLowerOrEqualThan(1.2)\n\t\texpect.NewFloatChecker(ctx, 1.2).ToBeLowerOrEqualThan(1.3)\n\t})\n\n\tt.Run(\"not to be lower or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewFloatChecker(ctx, 1.2).Not().ToBeLowerOrEqualThan(1.1)\n\t})\n}\n"},{"name":"func.gno","body":"package expect\n\nimport \"gno.land/p/nt/ufmt/v0\"\n\ntype (\n\t// Fn defines a type for generic functions.\n\tFn = func()\n\n\t// ErrorFn defines a type for generic functions that return an error.\n\tErrorFn = func() error\n\n\t// AnyFn defines a type for generic functions that returns a value.\n\tAnyFn = func() any\n\n\t// AnyErrorFn defines a type for generic functions that return a value and an error.\n\tAnyErrorFn = func() (any, error)\n)\n\n// Func creates a new checker for functions.\nfunc Func(t TestingT, fn any) FuncChecker {\n\treturn FuncChecker{\n\t\tctx: NewContext(t),\n\t\tfn:  fn,\n\t}\n}\n\n// FuncChecker asserts function panics, errors and returned value.\ntype FuncChecker struct {\n\tctx Context\n\tfn  any\n}\n\n// WithFailPrefix assigns a prefix that will be prefixed to testing errors when an assertion fails.\nfunc (c FuncChecker) WithFailPrefix(prefix string) FuncChecker {\n\tc.ctx.prefix = prefix\n\treturn c\n}\n\n// Not negates the next called expectation.\nfunc (c FuncChecker) Not() FuncChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// ToFail return an error checker to assert if current function returns an error.\nfunc (c FuncChecker) ToFail() ErrorChecker {\n\tc.ctx.T().Helper()\n\n\tvar err error\n\tswitch fn := c.fn.(type) {\n\tcase ErrorFn:\n\t\terr = fn()\n\tcase AnyErrorFn:\n\t\t_, err = fn()\n\tdefault:\n\t\tc.ctx.Fail(\"Unsupported error func type\\nGot: %T\", c.fn)\n\t\treturn ErrorChecker{}\n\t}\n\n\tc.ctx.CheckExpectation(err != nil, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn \"Expected func to return an error\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"Func failed with error\\nGot: %s\", err.Error())\n\t})\n\n\treturn NewErrorChecker(c.ctx, err)\n}\n\n// ToPanic return an message checker to assert if current function panicked.\n// This assertion is handled within the same realm, to assert panics when crossing\n// to another realm use the `ToAbort()` assertion.\n//\n// Example usage:\n//\n//\tfunc TestFoo(t *testing.T) {\n//\t  expect.Func(t, func() {\n//\t    Foo(cross)\n//\t  }).Not().ToCrossPanic()\n//\t}\nfunc (c FuncChecker) ToPanic() MessageChecker {\n\tc.ctx.T().Helper()\n\n\tvar (\n\t\tmsg      string\n\t\tpanicked bool\n\t)\n\n\t// TODO: Can't use a switch because it triggers the following VM error:\n\t// \"panic: should not happen, should be heapItemType: fn\u003c()~VPBlock(1,0)\u003e\"\n\t//\n\t// switch fn := c.fn.(type) {\n\t// case Fn:\n\t// \tmsg, panicked = handlePanic(fn)\n\t// case ErrorFn:\n\t// \tmsg, panicked = handlePanic(func() { _ = fn() })\n\t// case AnyFn:\n\t// \tmsg, panicked = handlePanic(func() { _ = fn() })\n\t// case AnyErrorFn:\n\t// \tmsg, panicked = handlePanic(func() { _, _ = fn() })\n\t// default:\n\t// \tc.ctx.Fail(\"Unsupported func type\\nGot: %T\", c.fn)\n\t// \treturn MessageChecker{}\n\t// }\n\n\tif fn, ok := c.fn.(Fn); ok {\n\t\tmsg, panicked = handlePanic(fn)\n\t} else if fn, ok := c.fn.(ErrorFn); ok {\n\t\tmsg, panicked = handlePanic(func() { _ = fn() })\n\t} else if fn, ok := c.fn.(AnyFn); ok {\n\t\tmsg, panicked = handlePanic(func() { _ = fn() })\n\t} else if fn, ok := c.fn.(AnyErrorFn); ok {\n\t\tmsg, panicked = handlePanic(func() { _, _ = fn() })\n\t} else {\n\t\tc.ctx.Fail(\"Unsupported func type\\nGot: %T\", c.fn)\n\t\treturn MessageChecker{}\n\t}\n\n\tc.ctx.CheckExpectation(panicked, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn \"Expected function to panic\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected func not to panic\\nGot: %s\", msg)\n\t})\n\n\treturn NewMessageChecker(c.ctx, msg, MessageTypePanic)\n}\n\n// ToCrossPanic return an message checker to assert if current function panicked when crossing.\n// This assertion is handled only when making a crossing call to another realm, when asserting\n// within the same realm use `ToPanic()`.\nfunc (c FuncChecker) ToCrossPanic() MessageChecker {\n\tc.ctx.T().Helper()\n\n\tvar (\n\t\tmsg      string\n\t\tpanicked bool\n\t)\n\n\t// TODO: Can't use a switch because it triggers the following VM error:\n\t// \"panic: should not happen, should be heapItemType: fn\u003c()~VPBlock(1,0)\u003e\"\n\t//\n\t// switch fn := c.fn.(type) {\n\t// case Fn:\n\t// \tmsg, panicked = handleCrossPanic(fn)\n\t// case ErrorFn:\n\t// \tmsg, panicked = handleCrossPanic(func() { _ = fn() })\n\t// case AnyFn:\n\t// \tmsg, panicked = handleCrossPanic(func() { _ = fn() })\n\t// case AnyErrorFn:\n\t// \tmsg, panicked = handleCrossPanic(func() { _, _ = fn() })\n\t// default:\n\t// \tc.ctx.Fail(\"Unsupported func type\\nGot: %T\", c.fn)\n\t// \treturn MessageChecker{}\n\t// }\n\n\tif fn, ok := c.fn.(Fn); ok {\n\t\tmsg, panicked = handleCrossPanic(fn)\n\t} else if fn, ok := c.fn.(ErrorFn); ok {\n\t\tmsg, panicked = handleCrossPanic(func() { _ = fn() })\n\t} else if fn, ok := c.fn.(AnyFn); ok {\n\t\tmsg, panicked = handleCrossPanic(func() { _ = fn() })\n\t} else if fn, ok := c.fn.(AnyErrorFn); ok {\n\t\tmsg, panicked = handleCrossPanic(func() { _, _ = fn() })\n\t} else {\n\t\tc.ctx.Fail(\"Unsupported func type\\nGot: %T\", c.fn)\n\t\treturn MessageChecker{}\n\t}\n\n\tc.ctx.CheckExpectation(panicked, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn \"Expected function to cross panic\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected func not to cross panic\\nGot: %s\", msg)\n\t})\n\n\treturn NewMessageChecker(c.ctx, msg, MessageTypeCrossPanic)\n}\n\n// ToReturn asserts that current function returned a value equal to an expected value.\nfunc (c FuncChecker) ToReturn(value any) {\n\tc.ctx.T().Helper()\n\n\tvar (\n\t\terr error\n\t\tv   any\n\t)\n\n\tif fn, ok := c.fn.(AnyFn); ok {\n\t\tv = fn()\n\t} else if fn, ok := c.fn.(AnyErrorFn); ok {\n\t\tv, err = fn()\n\t} else {\n\t\tc.ctx.Fail(\"Unsupported func type\\nGot: %T\", c.fn)\n\t\treturn\n\t}\n\n\tif err != nil {\n\t\tc.ctx.Fail(\"Function returned unexpected error\\nGot: %s\", err.Error())\n\t\treturn\n\t}\n\n\tif c.ctx.negated {\n\t\tValue(c.ctx.T(), v).Not().ToEqual(value)\n\t} else {\n\t\tValue(c.ctx.T(), v).ToEqual(value)\n\t}\n}\n\nfunc handlePanic(fn func()) (msg string, panicked bool) {\n\tdefer func() {\n\t\tr := recover()\n\t\tif r == nil {\n\t\t\treturn\n\t\t}\n\n\t\tpanicked = true\n\n\t\tif err, ok := r.(error); ok {\n\t\t\tmsg = err.Error()\n\t\t\treturn\n\t\t}\n\n\t\tif s, ok := r.(string); ok {\n\t\t\tmsg = s\n\t\t\treturn\n\t\t}\n\n\t\tmsg = \"unsupported panic type\"\n\t}()\n\n\tfn()\n\treturn\n}\n\nfunc handleCrossPanic(fn func()) (string, bool) {\n\tr := revive(fn)\n\tif r == nil {\n\t\treturn \"\", false\n\t}\n\n\tif err, ok := r.(error); ok {\n\t\treturn err.Error(), true\n\t}\n\n\tif s, ok := r.(string); ok {\n\t\treturn s, true\n\t}\n\n\treturn \"unsupported panic type\", true\n}\n"},{"name":"func_test.gno","body":"package expect_test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nfunc TestFunction(t *testing.T) {\n\tt.Run(\"not to fail\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\treturn nil\n\t\t}).Not().ToFail()\n\t})\n\n\tt.Run(\"to fail\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\treturn errors.New(\"Foo\")\n\t\t}).ToFail()\n\t})\n\n\tt.Run(\"to fail with mesasge\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\treturn errors.New(\"Foo\")\n\t\t}).ToFail().WithMessage(\"Foo\")\n\t})\n\n\tt.Run(\"to fail with different message\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\treturn errors.New(\"Bar\")\n\t\t}).ToFail().Not().WithMessage(\"Foo\")\n\t})\n\n\tt.Run(\"to fail with error\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\treturn errors.New(\"Foo\")\n\t\t}).ToFail().WithError(errors.New(\"Foo\"))\n\t})\n\n\tt.Run(\"to fail with different error\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\treturn errors.New(\"Bar\")\n\t\t}).ToFail().Not().WithError(errors.New(\"Foo\"))\n\t})\n\n\tt.Run(\"not to panic\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\treturn nil\n\t\t}).Not().ToPanic()\n\t})\n\n\tt.Run(\"to panic\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\tpanic(\"Foo\")\n\t\t}).ToPanic()\n\t})\n\n\tt.Run(\"to panic with message\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\tpanic(\"Foo\")\n\t\t}).ToPanic().WithMessage(\"Foo\")\n\t})\n\n\tt.Run(\"to panich with different message\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() error {\n\t\t\tpanic(\"Foo\")\n\t\t}).ToPanic().Not().WithMessage(\"Bar\")\n\t})\n\n\tt.Run(\"to return value\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() any {\n\t\t\treturn \"foo\"\n\t\t}).ToReturn(\"foo\")\n\n\t\texpect.Func(t, func() (any, error) {\n\t\t\treturn \"foo\", nil\n\t\t}).ToReturn(\"foo\")\n\t})\n\n\tt.Run(\"not to return value\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Func(t, func() any {\n\t\t\treturn \"foo\"\n\t\t}).Not().ToReturn(\"bar\")\n\n\t\texpect.Func(t, func() (any, error) {\n\t\t\treturn \"foo\", nil\n\t\t}).Not().ToReturn(\"bar\")\n\t})\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/expect\"\ngno = \"0.9\"\n"},{"name":"int.gno","body":"package expect\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// NewIntChecker creates a new checker of int64 values.\nfunc NewIntChecker(ctx Context, value int64) IntChecker {\n\treturn IntChecker{ctx, value}\n}\n\n// IntChecker asserts int64 values.\ntype IntChecker struct {\n\tctx   Context\n\tvalue int64\n}\n\n// Not negates the next called expectation.\nfunc (c IntChecker) Not() IntChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// ToEqual asserts that current value is equal to an expected value.\nfunc (c IntChecker) ToEqual(value int64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value == value, func(ctx Context) string {\n\t\tgot := formatInt(c.value)\n\t\tif !ctx.IsNegated() {\n\t\t\twant := formatInt(value)\n\t\t\treturn ufmt.Sprintf(\"Expected values to match\\nGot: %s\\nWant: %s\", got, want)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to be different\\nGot: %s\", got)\n\t})\n}\n\n// ToBeGreaterThan asserts that current value is greater than an expected value.\nfunc (c IntChecker) ToBeGreaterThan(value int64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003e value, func(ctx Context) string {\n\t\tgot := formatInt(c.value)\n\t\twant := formatInt(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be gerater than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be greater than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.\nfunc (c IntChecker) ToBeGreaterOrEqualThan(value int64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003e= value, func(ctx Context) string {\n\t\tgot := formatInt(c.value)\n\t\twant := formatInt(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be greater or equal than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be greater or equal than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeLowerThan asserts that current value is lower than an expected value.\nfunc (c IntChecker) ToBeLowerThan(value int64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003c value, func(ctx Context) string {\n\t\tgot := formatInt(c.value)\n\t\twant := formatInt(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be lower than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be lower than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.\nfunc (c IntChecker) ToBeLowerOrEqualThan(value int64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003c= value, func(ctx Context) string {\n\t\tgot := formatInt(c.value)\n\t\twant := formatInt(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be lower or equal than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be lower or equal than %s\\nGot: %s\", want, got)\n\t})\n}\n\nfunc formatInt(value int64) string {\n\treturn strconv.FormatInt(value, 10)\n}\n\nfunc asInt(value any) (int64, error) {\n\tswitch v := value.(type) {\n\tcase int:\n\t\treturn int64(v), nil\n\tcase int8:\n\t\treturn int64(v), nil\n\tcase int16:\n\t\treturn int64(v), nil\n\tcase int32:\n\t\treturn int64(v), nil\n\tcase int64:\n\t\treturn v, nil\n\tdefault:\n\t\treturn 0, ErrIncompatibleType\n\t}\n}\n"},{"name":"int_test.gno","body":"package expect_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nfunc TestIntChecker(t *testing.T) {\n\tt.Run(\"to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 1).ToEqual(1)\n\t})\n\n\tt.Run(\"not to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 1).Not().ToEqual(2)\n\t})\n\n\tt.Run(\"to be greater than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 2).ToBeGreaterThan(1)\n\t})\n\n\tt.Run(\"not to be greater than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 1).Not().ToBeGreaterThan(2)\n\t})\n\n\tt.Run(\"to be greater or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 2).ToBeGreaterOrEqualThan(2)\n\t\texpect.NewIntChecker(ctx, 2).ToBeGreaterOrEqualThan(1)\n\t})\n\n\tt.Run(\"not to be greater or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 1).Not().ToBeGreaterOrEqualThan(2)\n\t})\n\n\tt.Run(\"to be lower than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 1).ToBeLowerThan(2)\n\t})\n\n\tt.Run(\"not to be lower than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 1).Not().ToBeLowerThan(1)\n\t})\n\n\tt.Run(\"to be lower or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 1).ToBeLowerOrEqualThan(1)\n\t\texpect.NewIntChecker(ctx, 1).ToBeLowerOrEqualThan(2)\n\t})\n\n\tt.Run(\"not to be lower or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewIntChecker(ctx, 2).Not().ToBeLowerOrEqualThan(1)\n\t})\n}\n"},{"name":"message.gno","body":"package expect\n\nimport \"gno.land/p/nt/ufmt/v0\"\n\nconst (\n\tMessageTypeCrossPanic MessageType = \"cross panic\"\n\tMessageTypeError                  = \"error\"\n\tMessageTypePanic                  = \"panic\"\n)\n\n// MessageType defines a type for message checker errors.\ntype MessageType string\n\n// NewMessageChecker creates a new checker for text messages.\nfunc NewMessageChecker(ctx Context, msg string, t MessageType) MessageChecker {\n\treturn MessageChecker{ctx, msg, t}\n}\n\n// MessageChecker asserts text messages.\ntype MessageChecker struct {\n\tctx     Context\n\tmsg     string\n\tmsgType MessageType\n}\n\n// Not negates the next called expectation.\nfunc (c MessageChecker) Not() MessageChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// WithMessage asserts that a message is equal to an expected message.\nfunc (c MessageChecker) WithMessage(msg string) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.msg == msg, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected %s message to match\\nGot: %s\\nWant: %s\", string(c.msgType), c.msg, msg)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected %s message to be different\\nGot: %s\", string(c.msgType), c.msg)\n\t})\n}\n"},{"name":"string.gno","body":"package expect\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// ErrIncompatibleType indicates that a value can't be casted to a different type.\nvar ErrIncompatibleType = errors.New(\"incompatible type\")\n\n// NewStringChecker creates a new checker of string values.\nfunc NewStringChecker(ctx Context, value string) StringChecker {\n\treturn StringChecker{ctx, value}\n}\n\n// StringChecker asserts string values.\ntype StringChecker struct {\n\tctx   Context\n\tvalue string\n}\n\n// Not negates the next called expectation.\nfunc (c StringChecker) Not() StringChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// ToEqual asserts that current value is equal to an expected value.\nfunc (c StringChecker) ToEqual(v string) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value == v, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to match\\nGot: %s\\nWant: %s\", c.value, v)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected values to be different\\nGot: %s\", c.value)\n\t})\n}\n\n// ToBeEmpty asserts that current value is an empty string.\nfunc (c StringChecker) ToBeEmpty() {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value == \"\", func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected string to be empty\\nGot: %s\", c.value)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Unexpected empty string\")\n\t})\n}\n\n// ToHaveLength asserts that current value has an expected length.\nfunc (c StringChecker) ToHaveLength(length int) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(len(c.value) == length, func(ctx Context) string {\n\t\tgot := len(c.value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected string length to match\\nGot: %d\\nWant: %d\", got, length)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected string lengths to be different\\nGot: %d\", got)\n\t})\n}\n\n// Stringer defines an interface for values that has a String method.\ntype Stringer interface {\n\tString() string\n}\n\nfunc asString(value any) (string, error) {\n\tswitch v := value.(type) {\n\tcase string:\n\t\treturn v, nil\n\tcase []byte:\n\t\treturn string(v), nil\n\tcase Stringer:\n\t\treturn v.String(), nil\n\tcase address:\n\t\treturn v.String(), nil\n\tdefault:\n\t\treturn \"\", ErrIncompatibleType\n\t}\n}\n"},{"name":"string_test.gno","body":"package expect_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nfunc TestStringChecker(t *testing.T) {\n\tt.Run(\"to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewStringChecker(ctx, \"foo\").ToEqual(\"foo\")\n\t})\n\n\tt.Run(\"not to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewStringChecker(ctx, \"foo\").Not().ToEqual(\"bar\")\n\t})\n\n\tt.Run(\"to be empty\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewStringChecker(ctx, \"\").ToBeEmpty()\n\t})\n\n\tt.Run(\"not to be empty\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewStringChecker(ctx, \"foo\").Not().ToBeEmpty()\n\t})\n\n\tt.Run(\"same length\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewStringChecker(ctx, \"foo\").ToHaveLength(3)\n\t})\n\n\tt.Run(\"different length\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewStringChecker(ctx, \"foo\").Not().ToHaveLength(1)\n\t})\n}\n"},{"name":"uint.gno","body":"package expect\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// NewUintChecker creates a new checker of uint64 values.\nfunc NewUintChecker(ctx Context, value uint64) UintChecker {\n\treturn UintChecker{ctx, value}\n}\n\n// UintChecker asserts uint64 values.\ntype UintChecker struct {\n\tctx   Context\n\tvalue uint64\n}\n\n// Not negates the next called expectation.\nfunc (c UintChecker) Not() UintChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// ToEqual asserts that current value is equal to an expected value.\nfunc (c UintChecker) ToEqual(value uint64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value == value, func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\tgot := formatUint(c.value)\n\t\t\twant := formatUint(value)\n\t\t\treturn ufmt.Sprintf(\"Expected values to match\\nGot: %s\\nWant: %s\", got, want)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to be different\\nGot: %s\", formatUint(c.value))\n\t})\n}\n\n// ToBeGreaterThan asserts that current value is greater than an expected value.\nfunc (c UintChecker) ToBeGreaterThan(value uint64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003e value, func(ctx Context) string {\n\t\tgot := formatUint(c.value)\n\t\twant := formatUint(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be gerater than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be greater than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.\nfunc (c UintChecker) ToBeGreaterOrEqualThan(value uint64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003e= value, func(ctx Context) string {\n\t\tgot := formatUint(c.value)\n\t\twant := formatUint(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be greater or equal than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be greater or equal than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeLowerThan asserts that current value is lower than an expected value.\nfunc (c UintChecker) ToBeLowerThan(value uint64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003c value, func(ctx Context) string {\n\t\tgot := formatUint(c.value)\n\t\twant := formatUint(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be lower than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be lower than %s\\nGot: %s\", want, got)\n\t})\n}\n\n// ToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.\nfunc (c UintChecker) ToBeLowerOrEqualThan(value uint64) {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value \u003c= value, func(ctx Context) string {\n\t\tgot := formatUint(c.value)\n\t\twant := formatUint(value)\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected values to be lower or equal than %s\\nGot: %s\", want, got)\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected value to not to be lower or equal than %s\\nGot: %s\", want, got)\n\t})\n}\n\nfunc formatUint(value uint64) string {\n\treturn strconv.FormatUint(value, 10)\n}\n\nfunc asUint(value any) (uint64, error) {\n\tswitch v := value.(type) {\n\tcase uint:\n\t\treturn uint64(v), nil\n\tcase uint8:\n\t\treturn uint64(v), nil\n\tcase uint16:\n\t\treturn uint64(v), nil\n\tcase uint32:\n\t\treturn uint64(v), nil\n\tcase uint64:\n\t\treturn v, nil\n\tcase int:\n\t\tif v \u003c 0 {\n\t\t\treturn 0, ErrIncompatibleType\n\t\t}\n\t\treturn uint64(v), nil\n\tdefault:\n\t\treturn 0, ErrIncompatibleType\n\t}\n}\n"},{"name":"uint_test.gno","body":"package expect_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nfunc TestUintChecker(t *testing.T) {\n\tt.Run(\"to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 1).ToEqual(1)\n\t})\n\n\tt.Run(\"not to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 1).Not().ToEqual(2)\n\t})\n\n\tt.Run(\"to be greater than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 2).ToBeGreaterThan(1)\n\t})\n\n\tt.Run(\"not to be greater than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 1).Not().ToBeGreaterThan(2)\n\t})\n\n\tt.Run(\"to be greater or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 2).ToBeGreaterOrEqualThan(2)\n\t\texpect.NewUintChecker(ctx, 2).ToBeGreaterOrEqualThan(1)\n\t})\n\n\tt.Run(\"not to be greater or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 1).Not().ToBeGreaterOrEqualThan(2)\n\t})\n\n\tt.Run(\"to be lower than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 1).ToBeLowerThan(2)\n\t})\n\n\tt.Run(\"not to be lower than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 1).Not().ToBeLowerThan(1)\n\t})\n\n\tt.Run(\"to be lower or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 1).ToBeLowerOrEqualThan(1)\n\t\texpect.NewUintChecker(ctx, 1).ToBeLowerOrEqualThan(2)\n\t})\n\n\tt.Run(\"not to be lower or equal than\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tctx := expect.NewContext(t)\n\t\texpect.NewUintChecker(ctx, 2).Not().ToBeLowerOrEqualThan(1)\n\t})\n}\n"},{"name":"value.gno","body":"package expect\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Value creates a new checker values of different types.\nfunc Value(t TestingT, value any) ValueChecker {\n\treturn ValueChecker{\n\t\tctx:   NewContext(t),\n\t\tvalue: value,\n\t}\n}\n\n// ValueChecker asserts values of different types.\ntype ValueChecker struct {\n\tctx   Context\n\tvalue any\n}\n\n// WithFailPrefix assigns a prefix that will be prefixed to testing errors when an assertion fails.\nfunc (c ValueChecker) WithFailPrefix(prefix string) ValueChecker {\n\tc.ctx.prefix = prefix\n\treturn c\n}\n\n// Not negates the next called expectation.\nfunc (c ValueChecker) Not() ValueChecker {\n\tc.ctx.negated = !c.ctx.negated\n\treturn c\n}\n\n// ToBeNil asserts that current value is nil.\nfunc (c ValueChecker) ToBeNil() {\n\tc.ctx.T().Helper()\n\tc.ctx.CheckExpectation(c.value == nil || istypednil(c.value), func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected value to be nil\\nGot: %v\", c.value)\n\t\t}\n\t\treturn \"Expected a non nil value\"\n\t})\n}\n\n// ToEqual asserts that current value is equal to an expected value.\nfunc (c ValueChecker) ToEqual(value any) {\n\tc.ctx.T().Helper()\n\n\t// Assert error values first to allow comparing errors to string values\n\tif err, ok := c.value.(error); ok {\n\t\twant, ok := value.(error)\n\t\tif !ok {\n\t\t\tc.ctx.Fail(\"Failed: expected an error value\\nGot: %T\", value)\n\t\t\treturn\n\t\t}\n\n\t\tc.ctx.CheckExpectation(err.Error() == want.Error(), func(ctx Context) string {\n\t\t\tif !ctx.IsNegated() {\n\t\t\t\treturn ufmt.Sprintf(\"Expected errors to match\\nGot: %s\\nWant: %s\", err.Error(), want.Error())\n\t\t\t}\n\t\t\treturn ufmt.Sprintf(\"Expected errors to be different\\nGot: %s\", err.Error())\n\t\t})\n\n\t\treturn\n\t}\n\n\tswitch v := value.(type) {\n\tcase string:\n\t\tc.AsString().ToEqual(v)\n\tcase []byte:\n\t\tc.AsString().ToEqual(string(v))\n\tcase Stringer:\n\t\tc.AsString().ToEqual(v.String())\n\tcase bool:\n\t\tc.AsBoolean().ToEqual(v)\n\tcase float32:\n\t\tc.AsFloat().ToEqual(float64(v))\n\tcase float64:\n\t\tc.AsFloat().ToEqual(v)\n\tcase uint:\n\t\tc.AsUint().ToEqual(uint64(v))\n\tcase uint8:\n\t\tc.AsUint().ToEqual(uint64(v))\n\tcase uint16:\n\t\tc.AsUint().ToEqual(uint64(v))\n\tcase uint32:\n\t\tc.AsUint().ToEqual(uint64(v))\n\tcase uint64:\n\t\tc.AsUint().ToEqual(v)\n\tcase int:\n\t\tc.AsInt().ToEqual(int64(v))\n\tcase int8:\n\t\tc.AsInt().ToEqual(int64(v))\n\tcase int16:\n\t\tc.AsInt().ToEqual(int64(v))\n\tcase int32:\n\t\tc.AsInt().ToEqual(int64(v))\n\tcase int64:\n\t\tc.AsInt().ToEqual(v)\n\tcase error:\n\t\tc.ctx.Fail(\"Error is not equal to value\\nGot: %s\", v.Error())\n\tdefault:\n\t\tc.ctx.Fail(\"Unsupported type: %T\", value)\n\t}\n}\n\n// ToContainErrorString asserts that current error value contains an error string.\nfunc (c ValueChecker) ToContainErrorString(msg string) {\n\tc.ctx.T().Helper()\n\n\terr, ok := c.value.(error)\n\tif !ok {\n\t\tc.ctx.Fail(\"Failed: expected an error value\\nGot: %T\", c.value)\n\t\treturn\n\t}\n\n\tc.ctx.CheckExpectation(strings.Contains(err.Error(), msg), func(ctx Context) string {\n\t\tif !ctx.IsNegated() {\n\t\t\treturn ufmt.Sprintf(\"Expected error message to contain: %s\\nGot: %s\", msg, err.Error())\n\t\t}\n\t\treturn ufmt.Sprintf(\"Expected error message not to contain: %s\\nGot: %s\", msg, err.Error())\n\t})\n}\n\n// AsString returns a checker to assert current value as a string.\nfunc (c ValueChecker) AsString() StringChecker {\n\tc.ctx.T().Helper()\n\n\tv, err := asString(c.value)\n\tif err != nil {\n\t\tc.ctx.Fail(\"Failed: %s: expected a string value\\nGot: %T\", err.Error(), c.value)\n\t\treturn StringChecker{}\n\t}\n\n\treturn NewStringChecker(c.ctx, v)\n}\n\n// AsBoolean returns a checker to assert current value as a boolean.\nfunc (c ValueChecker) AsBoolean() BooleanChecker {\n\tc.ctx.T().Helper()\n\n\tv, err := asBoolean(c.value)\n\tif err != nil {\n\t\tc.ctx.Fail(\"Failed: %s: expected a boolean value\\nGot: %T\", err.Error(), c.value)\n\t\treturn BooleanChecker{}\n\t}\n\n\treturn NewBooleanChecker(c.ctx, v)\n}\n\n// AsFloat returns a checker to assert current value as a float64.\nfunc (c ValueChecker) AsFloat() FloatChecker {\n\tc.ctx.T().Helper()\n\n\tv, err := asFloat(c.value)\n\tif err != nil {\n\t\tc.ctx.Fail(\"%s: expected a float value\\nGot: %T\", err.Error(), c.value)\n\t\treturn FloatChecker{}\n\t}\n\n\treturn NewFloatChecker(c.ctx, v)\n}\n\n// AsUint returns a checker to assert current value as a uint64.\nfunc (c ValueChecker) AsUint() UintChecker {\n\tc.ctx.T().Helper()\n\n\tv, err := asUint(c.value)\n\tif err != nil {\n\t\tc.ctx.Fail(\"Failed: %s: expected a uint value\\nGot: %T\", err.Error(), c.value)\n\t\treturn UintChecker{}\n\t}\n\n\treturn NewUintChecker(c.ctx, v)\n}\n\n// AsInt returns a checker to assert current value as a int64.\nfunc (c ValueChecker) AsInt() IntChecker {\n\tc.ctx.T().Helper()\n\n\tv, err := asInt(c.value)\n\tif err != nil {\n\t\tc.ctx.Fail(\"Failed: %s: expected an int value\\nGot: %T\", err.Error(), c.value)\n\t\treturn IntChecker{}\n\t}\n\n\treturn NewIntChecker(c.ctx, v)\n}\n"},{"name":"value_test.gno","body":"package expect_test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nfunc TestValue(t *testing.T) {\n\tt.Run(\"equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, \"foo\").ToEqual(\"foo\")\n\t\texpect.Value(t, []byte(\"foo\")).ToEqual([]byte(\"foo\"))\n\t\texpect.Value(t, stringer(\"foo\")).ToEqual(stringer(\"foo\"))\n\t\texpect.Value(t, true).ToEqual(true)\n\t\texpect.Value(t, float32(1)).ToEqual(float32(1))\n\t\texpect.Value(t, float64(1)).ToEqual(float64(1))\n\t\texpect.Value(t, uint(1)).ToEqual(uint(1))\n\t\texpect.Value(t, uint8(1)).ToEqual(uint8(1))\n\t\texpect.Value(t, uint16(1)).ToEqual(uint16(1))\n\t\texpect.Value(t, uint32(1)).ToEqual(uint32(1))\n\t\texpect.Value(t, uint64(1)).ToEqual(uint64(1))\n\t\texpect.Value(t, int(1)).ToEqual(int(1))\n\t\texpect.Value(t, int8(1)).ToEqual(int8(1))\n\t\texpect.Value(t, int16(1)).ToEqual(int16(1))\n\t\texpect.Value(t, int32(1)).ToEqual(int32(1))\n\t\texpect.Value(t, int64(1)).ToEqual(int64(1))\n\t\texpect.Value(t, errors.New(\"foo\")).ToEqual(errors.New(\"foo\"))\n\t\texpect.Value(t, errors.New(\"foo bar\")).ToContainErrorString(\"foo\")\n\t})\n\n\tt.Run(\"not to equal\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, \"foo\").Not().ToEqual(\"bar\")\n\t\texpect.Value(t, []byte(\"foo\")).Not().ToEqual([]byte(\"bar\"))\n\t\texpect.Value(t, stringer(\"foo\")).Not().ToEqual(stringer(\"bar\"))\n\t\texpect.Value(t, true).Not().ToEqual(false)\n\t\texpect.Value(t, float32(1)).Not().ToEqual(float32(2))\n\t\texpect.Value(t, float64(1)).Not().ToEqual(float64(2))\n\t\texpect.Value(t, uint(1)).Not().ToEqual(uint(2))\n\t\texpect.Value(t, uint8(1)).Not().ToEqual(uint8(2))\n\t\texpect.Value(t, uint16(1)).Not().ToEqual(uint16(2))\n\t\texpect.Value(t, uint32(1)).Not().ToEqual(uint32(2))\n\t\texpect.Value(t, uint64(1)).Not().ToEqual(uint64(2))\n\t\texpect.Value(t, int(1)).Not().ToEqual(int(2))\n\t\texpect.Value(t, int8(1)).Not().ToEqual(int8(2))\n\t\texpect.Value(t, int16(1)).Not().ToEqual(int16(2))\n\t\texpect.Value(t, int32(1)).Not().ToEqual(int32(2))\n\t\texpect.Value(t, int64(1)).Not().ToEqual(int64(2))\n\t\texpect.Value(t, errors.New(\"foo\")).Not().ToEqual(errors.New(\"bar\"))\n\t\texpect.Value(t, errors.New(\"foo\")).Not().ToContainErrorString(\"bar\")\n\t})\n\n\tt.Run(\"to be nil\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\texpect.Value(t, nil).ToBeNil()\n\t\texpect.Value(t, (*int)(nil)).ToBeNil() // typed nil\n\t})\n\n\tt.Run(\"not to be nil\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\texpect.Value(t, \"\").Not().ToBeNil()\n\t})\n\n\tt.Run(\"to be truthy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, \"true\").AsBoolean().ToBeTruthy()\n\t\texpect.Value(t, \"TRUE\").AsBoolean().ToBeTruthy()\n\t\texpect.Value(t, \"t\").AsBoolean().ToBeTruthy()\n\t\texpect.Value(t, \"1\").AsBoolean().ToBeTruthy()\n\t\texpect.Value(t, []byte(\"true\")).AsBoolean().ToBeTruthy()\n\t})\n\n\tt.Run(\"not to be truthy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, \"\").AsBoolean().Not().ToBeTruthy()\n\t\texpect.Value(t, \"false\").AsBoolean().Not().ToBeTruthy()\n\t\texpect.Value(t, \"FALSE\").AsBoolean().Not().ToBeTruthy()\n\t\texpect.Value(t, \"f\").AsBoolean().Not().ToBeTruthy()\n\t\texpect.Value(t, \"0\").AsBoolean().Not().ToBeTruthy()\n\t\texpect.Value(t, []byte(nil)).AsBoolean().Not().ToBeTruthy()\n\t\texpect.Value(t, []byte(\"false\")).AsBoolean().Not().ToBeTruthy()\n\t})\n\n\tt.Run(\"to be falsy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, \"false\").AsBoolean().ToBeFalsy()\n\t\texpect.Value(t, \"FALSE\").AsBoolean().ToBeFalsy()\n\t\texpect.Value(t, \"f\").AsBoolean().ToBeFalsy()\n\t\texpect.Value(t, \"0\").AsBoolean().ToBeFalsy()\n\t\texpect.Value(t, \"\").AsBoolean().ToBeFalsy()\n\t\texpect.Value(t, []byte(nil)).AsBoolean().ToBeFalsy()\n\t})\n\n\tt.Run(\"not to be falsy\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, \"true\").AsBoolean().Not().ToBeFalsy()\n\t\texpect.Value(t, \"TRUE\").AsBoolean().Not().ToBeFalsy()\n\t\texpect.Value(t, \"t\").AsBoolean().Not().ToBeFalsy()\n\t\texpect.Value(t, \"1\").AsBoolean().Not().ToBeFalsy()\n\t\texpect.Value(t, []byte(\"true\")).AsBoolean().Not().ToBeFalsy()\n\t})\n\n\tt.Run(\"to equal stringer\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, address(\"foo\")).AsString().ToEqual(\"foo\")\n\t})\n\n\tt.Run(\"not to equal stringer\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\texpect.Value(t, address(\"foo\")).AsString().Not().ToEqual(\"bar\")\n\t})\n}\n\ntype stringer string\n\nfunc (s stringer) String() string { return string(s) }\n"},{"name":"z_boolean_0_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, true).AsBoolean().ToEqual(false)\n\texpect.Value(t, true).AsBoolean().Not().ToEqual(true)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to match\n// Got: true\n// Want: false\n// Expected values to be different\n// Got: true\n"},{"name":"z_boolean_1_filetest.gno","body":"package main\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\ntype intStringer struct{ value int }\n\nfunc (v intStringer) String() string {\n\treturn strconv.Itoa(v.value)\n}\n\nfunc main() {\n\texpect.Value(t, true).AsBoolean().ToBeFalsy()\n\texpect.Value(t, false).AsBoolean().Not().ToBeFalsy()\n\n\texpect.Value(t, \"TRUE\").AsBoolean().ToBeFalsy()\n\texpect.Value(t, \"FALSE\").AsBoolean().Not().ToBeFalsy()\n\n\texpect.Value(t, []byte(\"TRUE\")).AsBoolean().ToBeFalsy()\n\texpect.Value(t, []byte(\"FALSE\")).AsBoolean().Not().ToBeFalsy()\n\n\texpect.Value(t, intStringer{1}).AsBoolean().ToBeFalsy()\n\texpect.Value(t, intStringer{0}).AsBoolean().Not().ToBeFalsy()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected value to be falsy\n// Expected value not to be falsy\n// Expected value to be falsy\n// Expected value not to be falsy\n// Expected value to be falsy\n// Expected value not to be falsy\n// Expected value to be falsy\n// Expected value not to be falsy\n"},{"name":"z_boolean_2_filetest.gno","body":"package main\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\ntype intStringer struct{ value int }\n\nfunc (v intStringer) String() string {\n\treturn strconv.Itoa(v.value)\n}\n\nfunc main() {\n\texpect.Value(t, false).AsBoolean().ToBeTruthy()\n\texpect.Value(t, true).AsBoolean().Not().ToBeTruthy()\n\n\texpect.Value(t, \"FALSE\").AsBoolean().ToBeTruthy()\n\texpect.Value(t, \"TRUE\").AsBoolean().Not().ToBeTruthy()\n\n\texpect.Value(t, []byte(\"FALSE\")).AsBoolean().ToBeTruthy()\n\texpect.Value(t, []byte(\"TRUE\")).AsBoolean().Not().ToBeTruthy()\n\n\texpect.Value(t, intStringer{0}).AsBoolean().ToBeTruthy()\n\texpect.Value(t, intStringer{1}).AsBoolean().Not().ToBeTruthy()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected value to be truthy\n// Expected value not to be truthy\n// Expected value to be truthy\n// Expected value not to be truthy\n// Expected value to be truthy\n// Expected value not to be truthy\n// Expected value to be truthy\n// Expected value not to be truthy\n"},{"name":"z_error_0_filetest.gno","body":"package main\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput  strings.Builder\n\tt       = expect.MockTestingT(\u0026output)\n\ttestErr = errors.New(\"test\")\n)\n\nfunc main() {\n\texpect.Func(t, func() error {\n\t\treturn testErr\n\t}).ToFail().WithMessage(\"foo\")\n\n\texpect.Func(t, func() error {\n\t\treturn testErr\n\t}).ToFail().Not().WithMessage(\"test\")\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected error message to match\n// Got: test\n// Want: foo\n// Expected error message to be different\n// Got: test\n"},{"name":"z_error_1_filetest.gno","body":"package main\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput  strings.Builder\n\tt       = expect.MockTestingT(\u0026output)\n\ttestErr = errors.New(\"test\")\n)\n\nfunc main() {\n\texpect.Func(t, func() error {\n\t\treturn testErr\n\t}).ToFail().WithError(errors.New(\"foo\"))\n\n\texpect.Func(t, func() error {\n\t\treturn testErr\n\t}).ToFail().Not().WithError(testErr)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected errors to match\n// Got: test\n// Want: foo\n// Expected errors to be different\n// Got: test\n"},{"name":"z_float_0_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1.2).AsFloat().ToEqual(1.1)\n\texpect.Value(t, 1.2).AsFloat().Not().ToEqual(1.2)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to match\n// Got: 1.2\n// Want: 1.1\n// Expected value to be different\n// Got: 1.2\n"},{"name":"z_float_1_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1.2).AsFloat().ToBeGreaterThan(1.3)\n\texpect.Value(t, 1.2).AsFloat().Not().ToBeGreaterThan(1.1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be gerater than 1.3\n// Got: 1.2\n// Expected value to not to be greater than 1.1\n// Got: 1.2\n"},{"name":"z_float_2_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1.2).AsFloat().ToBeGreaterOrEqualThan(1.3)\n\texpect.Value(t, 1.2).AsFloat().Not().ToBeGreaterOrEqualThan(1.2)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be greater or equal than 1.3\n// Got: 1.2\n// Expected value to not to be greater or equal than 1.2\n// Got: 1.2\n"},{"name":"z_float_3_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1.2).AsFloat().ToBeLowerThan(1.1)\n\texpect.Value(t, 1.2).AsFloat().Not().ToBeLowerThan(1.3)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be lower than 1.1\n// Got: 1.2\n// Expected value to not to be lower than 1.3\n// Got: 1.2\n"},{"name":"z_float_4_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1.2).AsFloat().ToBeLowerOrEqualThan(1.1)\n\texpect.Value(t, 1.2).AsFloat().Not().ToBeLowerOrEqualThan(1.2)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be lower or equal than 1.1\n// Got: 1.2\n// Expected value to not to be lower or equal than 1.2\n// Got: 1.2\n"},{"name":"z_func_0_filetest.gno","body":"package main\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\tgotMsg := \"Boom!\"\n\tgotErr := errors.New(gotMsg)\n\twantMsg := \"Tick Tock\"\n\twantErr := errors.New(wantMsg)\n\n\texpect.Func(t, func() error { return nil }).ToFail()\n\texpect.Func(t, func() error { return gotErr }).ToFail().WithMessage(wantMsg)\n\texpect.Func(t, func() error { return gotErr }).ToFail().WithError(wantErr)\n\n\texpect.Func(t, func() (any, error) { return nil, nil }).ToFail()\n\texpect.Func(t, func() (any, error) { return nil, gotErr }).ToFail().WithMessage(wantMsg)\n\texpect.Func(t, func() (any, error) { return nil, gotErr }).ToFail().WithError(wantErr)\n\n\texpect.Func(t, func() int { return 0 }).ToFail()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected func to return an error\n// Expected error message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected errors to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected func to return an error\n// Expected error message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected errors to match\n// Got: Boom!\n// Want: Tick Tock\n// Unsupported error func type\n// Got: unknown\n"},{"name":"z_func_1_filetest.gno","body":"package main\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\tmsg := \"Boom!\"\n\terr := errors.New(msg)\n\n\texpect.Func(t, func() error { return err }).Not().ToFail()\n\texpect.Func(t, func() error { return err }).ToFail().Not().WithMessage(msg)\n\texpect.Func(t, func() error { return err }).ToFail().Not().WithError(err)\n\n\texpect.Func(t, func() (any, error) { return nil, err }).Not().ToFail()\n\texpect.Func(t, func() (any, error) { return nil, err }).ToFail().Not().WithMessage(msg)\n\texpect.Func(t, func() (any, error) { return nil, err }).ToFail().Not().WithError(err)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Func failed with error\n// Got: Boom!\n// Expected error message to be different\n// Got: Boom!\n// Expected errors to be different\n// Got: Boom!\n// Func failed with error\n// Got: Boom!\n// Expected error message to be different\n// Got: Boom!\n// Expected errors to be different\n// Got: Boom!\n"},{"name":"z_func_2_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\tgotMsg := \"Boom!\"\n\twantMsg := \"Tick Tock\"\n\n\texpect.Func(t, func() {}).ToPanic()\n\texpect.Func(t, func() { panic(gotMsg) }).ToPanic().WithMessage(wantMsg)\n\n\texpect.Func(t, func() error { return nil }).ToPanic()\n\texpect.Func(t, func() error { panic(gotMsg) }).ToPanic().WithMessage(wantMsg)\n\n\texpect.Func(t, func() any { return nil }).ToPanic()\n\texpect.Func(t, func() any { panic(gotMsg) }).ToPanic().WithMessage(wantMsg)\n\n\texpect.Func(t, func() (any, error) { return nil, nil }).ToPanic()\n\texpect.Func(t, func() (any, error) { panic(gotMsg) }).ToPanic().WithMessage(wantMsg)\n\n\texpect.Func(t, func() int { return 0 }).ToPanic()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected function to panic\n// Expected panic message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected function to panic\n// Expected panic message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected function to panic\n// Expected panic message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected function to panic\n// Expected panic message to match\n// Got: Boom!\n// Want: Tick Tock\n// Unsupported func type\n// Got: unknown\n"},{"name":"z_func_3_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\tmsg := \"Boom!\"\n\n\texpect.Func(t, func() { panic(msg) }).Not().ToPanic()\n\texpect.Func(t, func() { panic(msg) }).ToPanic().Not().WithMessage(msg)\n\n\texpect.Func(t, func() error { panic(msg) }).Not().ToPanic()\n\texpect.Func(t, func() error { panic(msg) }).ToPanic().Not().WithMessage(msg)\n\n\texpect.Func(t, func() any { panic(msg) }).Not().ToPanic()\n\texpect.Func(t, func() any { panic(msg) }).ToPanic().Not().WithMessage(msg)\n\n\texpect.Func(t, func() (any, error) { panic(msg) }).Not().ToPanic()\n\texpect.Func(t, func() (any, error) { panic(msg) }).ToPanic().Not().WithMessage(msg)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected func not to panic\n// Got: Boom!\n// Expected panic message to be different\n// Got: Boom!\n// Expected func not to panic\n// Got: Boom!\n// Expected panic message to be different\n// Got: Boom!\n// Expected func not to panic\n// Got: Boom!\n// Expected panic message to be different\n// Got: Boom!\n// Expected func not to panic\n// Got: Boom!\n// Expected panic message to be different\n// Got: Boom!\n"},{"name":"z_func_4_filetest.gno","body":"package main\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Func(t, func() any { return \"foo\" }).ToReturn(\"bar\")\n\texpect.Func(t, func() any { return []byte(\"foo\") }).ToReturn([]byte(\"bar\"))\n\texpect.Func(t, func() any { return true }).ToReturn(false)\n\texpect.Func(t, func() any { return float32(1) }).ToReturn(float32(2))\n\texpect.Func(t, func() any { return float64(1.1) }).ToReturn(float64(1.2))\n\texpect.Func(t, func() any { return uint(1) }).ToReturn(uint(2))\n\texpect.Func(t, func() any { return uint8(1) }).ToReturn(uint8(2))\n\texpect.Func(t, func() any { return uint16(1) }).ToReturn(uint16(2))\n\texpect.Func(t, func() any { return uint32(1) }).ToReturn(uint32(2))\n\texpect.Func(t, func() any { return uint64(1) }).ToReturn(uint64(2))\n\texpect.Func(t, func() any { return int(1) }).ToReturn(int(2))\n\texpect.Func(t, func() any { return int8(1) }).ToReturn(int8(2))\n\texpect.Func(t, func() any { return int16(1) }).ToReturn(int16(2))\n\texpect.Func(t, func() any { return int32(1) }).ToReturn(int32(2))\n\texpect.Func(t, func() any { return int64(1) }).ToReturn(int64(2))\n\n\texpect.Func(t, func() (any, error) { return \"foo\", nil }).ToReturn(\"bar\")\n\texpect.Func(t, func() (any, error) { return []byte(\"foo\"), nil }).ToReturn([]byte(\"bar\"))\n\texpect.Func(t, func() (any, error) { return true, nil }).ToReturn(false)\n\texpect.Func(t, func() (any, error) { return float32(1), nil }).ToReturn(float32(2))\n\texpect.Func(t, func() (any, error) { return float64(1.1), nil }).ToReturn(float64(1.2))\n\texpect.Func(t, func() (any, error) { return uint(1), nil }).ToReturn(uint(2))\n\texpect.Func(t, func() (any, error) { return uint8(1), nil }).ToReturn(uint8(2))\n\texpect.Func(t, func() (any, error) { return uint16(1), nil }).ToReturn(uint16(2))\n\texpect.Func(t, func() (any, error) { return uint32(1), nil }).ToReturn(uint32(2))\n\texpect.Func(t, func() (any, error) { return uint64(1), nil }).ToReturn(uint64(2))\n\texpect.Func(t, func() (any, error) { return int(1), nil }).ToReturn(int(2))\n\texpect.Func(t, func() (any, error) { return int8(1), nil }).ToReturn(int8(2))\n\texpect.Func(t, func() (any, error) { return int16(1), nil }).ToReturn(int16(2))\n\texpect.Func(t, func() (any, error) { return int32(1), nil }).ToReturn(int32(2))\n\texpect.Func(t, func() (any, error) { return int64(1), nil }).ToReturn(int64(2))\n\n\texpect.Func(t, func() (any, error) { return 0, errors.New(\"Boom!\") }).ToReturn(1)\n\texpect.Func(t, func() {}).ToReturn(1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to match\n// Got: true\n// Want: false\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1.1\n// Want: 1.2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to match\n// Got: true\n// Want: false\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1.1\n// Want: 1.2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Function returned unexpected error\n// Got: Boom!\n// Unsupported func type\n// Got: unknown\n"},{"name":"z_func_5_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Func(t, func() any { return \"foo\" }).Not().ToReturn(\"foo\")\n\texpect.Func(t, func() any { return []byte(\"foo\") }).Not().ToReturn([]byte(\"foo\"))\n\texpect.Func(t, func() any { return true }).Not().ToReturn(true)\n\texpect.Func(t, func() any { return float32(1) }).Not().ToReturn(float32(1))\n\texpect.Func(t, func() any { return float64(1.1) }).Not().ToReturn(float64(1.1))\n\texpect.Func(t, func() any { return uint(1) }).Not().ToReturn(uint(1))\n\texpect.Func(t, func() any { return uint8(1) }).Not().ToReturn(uint8(1))\n\texpect.Func(t, func() any { return uint16(1) }).Not().ToReturn(uint16(1))\n\texpect.Func(t, func() any { return uint32(1) }).Not().ToReturn(uint32(1))\n\texpect.Func(t, func() any { return uint64(1) }).Not().ToReturn(uint64(1))\n\texpect.Func(t, func() any { return int(1) }).Not().ToReturn(int(1))\n\texpect.Func(t, func() any { return int8(1) }).Not().ToReturn(int8(1))\n\texpect.Func(t, func() any { return int16(1) }).Not().ToReturn(int16(1))\n\texpect.Func(t, func() any { return int32(1) }).Not().ToReturn(int32(1))\n\texpect.Func(t, func() any { return int64(1) }).Not().ToReturn(int64(1))\n\n\texpect.Func(t, func() (any, error) { return \"foo\", nil }).Not().ToReturn(\"foo\")\n\texpect.Func(t, func() (any, error) { return []byte(\"foo\"), nil }).Not().ToReturn([]byte(\"foo\"))\n\texpect.Func(t, func() (any, error) { return true, nil }).Not().ToReturn(true)\n\texpect.Func(t, func() (any, error) { return float32(1), nil }).Not().ToReturn(float32(1))\n\texpect.Func(t, func() (any, error) { return float64(1.1), nil }).Not().ToReturn(float64(1.1))\n\texpect.Func(t, func() (any, error) { return uint(1), nil }).Not().ToReturn(uint(1))\n\texpect.Func(t, func() (any, error) { return uint8(1), nil }).Not().ToReturn(uint8(1))\n\texpect.Func(t, func() (any, error) { return uint16(1), nil }).Not().ToReturn(uint16(1))\n\texpect.Func(t, func() (any, error) { return uint32(1), nil }).Not().ToReturn(uint32(1))\n\texpect.Func(t, func() (any, error) { return uint64(1), nil }).Not().ToReturn(uint64(1))\n\texpect.Func(t, func() (any, error) { return int(1), nil }).Not().ToReturn(int(1))\n\texpect.Func(t, func() (any, error) { return int8(1), nil }).Not().ToReturn(int8(1))\n\texpect.Func(t, func() (any, error) { return int16(1), nil }).Not().ToReturn(int16(1))\n\texpect.Func(t, func() (any, error) { return int32(1), nil }).Not().ToReturn(int32(1))\n\texpect.Func(t, func() (any, error) { return int64(1), nil }).Not().ToReturn(int64(1))\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be different\n// Got: foo\n// Expected values to be different\n// Got: foo\n// Expected values to be different\n// Got: true\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1.1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected values to be different\n// Got: foo\n// Expected values to be different\n// Got: foo\n// Expected values to be different\n// Got: true\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1.1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n"},{"name":"z_func_6_filetest.gno","body":"// PKGPATH: gno.land/r/demo/test\npackage test\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nconst (\n\tcaller = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\tmsg    = \"Boom!\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc Fail(realm) {\n\tpanic(msg)\n}\n\nfunc Success(realm) {\n\t// No panic\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\n\texpect.Func(t, func() { Fail(cross(cur)) }).ToCrossPanic()\n\texpect.Func(t, func() { Fail(cross(cur)) }).ToCrossPanic().WithMessage(msg)\n\n\texpect.Func(t, func() error { Fail(cross(cur)); return nil }).ToCrossPanic()\n\texpect.Func(t, func() error { Fail(cross(cur)); return nil }).ToCrossPanic().WithMessage(msg)\n\n\texpect.Func(t, func() any { Fail(cross(cur)); return nil }).ToCrossPanic()\n\texpect.Func(t, func() any { Fail(cross(cur)); return nil }).ToCrossPanic().WithMessage(msg)\n\n\texpect.Func(t, func() (any, error) { Fail(cross(cur)); return nil, nil }).ToCrossPanic()\n\texpect.Func(t, func() (any, error) { Fail(cross(cur)); return nil, nil }).ToCrossPanic().WithMessage(msg)\n\n\texpect.Func(t, func() { Success(cross(cur)) }).Not().ToCrossPanic()\n\texpect.Func(t, func() error { Success(cross(cur)); return nil }).Not().ToCrossPanic()\n\texpect.Func(t, func() any { Success(cross(cur)); return nil }).Not().ToCrossPanic()\n\texpect.Func(t, func() (any, error) { Success(cross(cur)); return nil, nil }).Not().ToCrossPanic()\n\n\t// None should fail, output should be empty\n\tprint(output.String())\n}\n\n// Output:\n"},{"name":"z_func_7_filetest.gno","body":"// PKGPATH: gno.land/r/demo/test\npackage test\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nconst (\n\tcaller = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\tmsg    = \"Boom!\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc Fail(realm) {\n\tpanic(msg)\n}\n\nfunc Success(realm) {\n\t// No panic\n}\n\nfunc main(cur realm) {\n\twantMsg := \"Tick Tock\"\n\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\n\texpect.Func(t, func() { Success(cross(cur)) }).ToCrossPanic()\n\texpect.Func(t, func() { Fail(cross(cur)) }).ToCrossPanic().WithMessage(wantMsg)\n\n\texpect.Func(t, func() error { Success(cross(cur)); return nil }).ToCrossPanic()\n\texpect.Func(t, func() error { Fail(cross(cur)); return nil }).ToCrossPanic().WithMessage(wantMsg)\n\n\texpect.Func(t, func() any { Success(cross(cur)); return nil }).ToCrossPanic()\n\texpect.Func(t, func() any { Fail(cross(cur)); return nil }).ToCrossPanic().WithMessage(wantMsg)\n\n\texpect.Func(t, func() (any, error) { Success(cross(cur)); return nil, nil }).ToCrossPanic()\n\texpect.Func(t, func() (any, error) { Fail(cross(cur)); return nil, nil }).ToCrossPanic().WithMessage(wantMsg)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected function to cross panic\n// Expected cross panic message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected function to cross panic\n// Expected cross panic message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected function to cross panic\n// Expected cross panic message to match\n// Got: Boom!\n// Want: Tick Tock\n// Expected function to cross panic\n// Expected cross panic message to match\n// Got: Boom!\n// Want: Tick Tock\n"},{"name":"z_func_8_filetest.gno","body":"// PKGPATH: gno.land/r/demo/test\npackage test\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nconst (\n\tcaller = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\tmsg    = \"Boom!\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc Fail(realm) {\n\tpanic(msg)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\n\texpect.Func(t, func() { Fail(cross(cur)) }).Not().ToCrossPanic()\n\texpect.Func(t, func() { Fail(cross(cur)) }).ToCrossPanic().Not().WithMessage(msg)\n\n\texpect.Func(t, func() error { Fail(cross(cur)); return nil }).Not().ToCrossPanic()\n\texpect.Func(t, func() error { Fail(cross(cur)); return nil }).ToCrossPanic().Not().WithMessage(msg)\n\n\texpect.Func(t, func() any { Fail(cross(cur)); return nil }).Not().ToCrossPanic()\n\texpect.Func(t, func() any { Fail(cross(cur)); return nil }).ToCrossPanic().Not().WithMessage(msg)\n\n\texpect.Func(t, func() (any, error) { Fail(cross(cur)); return nil, nil }).Not().ToCrossPanic()\n\texpect.Func(t, func() (any, error) { Fail(cross(cur)); return nil, nil }).ToCrossPanic().Not().WithMessage(msg)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected func not to cross panic\n// Got: Boom!\n// Expected cross panic message to be different\n// Got: Boom!\n// Expected func not to cross panic\n// Got: Boom!\n// Expected cross panic message to be different\n// Got: Boom!\n// Expected func not to cross panic\n// Got: Boom!\n// Expected cross panic message to be different\n// Got: Boom!\n// Expected func not to cross panic\n// Got: Boom!\n// Expected cross panic message to be different\n// Got: Boom!\n"},{"name":"z_int_0_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsInt().ToEqual(2)\n\texpect.Value(t, 1).AsInt().Not().ToEqual(1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected value to be different\n// Got: 1\n"},{"name":"z_int_1_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsInt().ToBeGreaterThan(2)\n\texpect.Value(t, 1).AsInt().Not().ToBeGreaterThan(0)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be gerater than 2\n// Got: 1\n// Expected value to not to be greater than 0\n// Got: 1\n"},{"name":"z_int_2_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsInt().ToBeGreaterOrEqualThan(2)\n\texpect.Value(t, 1).AsInt().Not().ToBeGreaterOrEqualThan(1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be greater or equal than 2\n// Got: 1\n// Expected value to not to be greater or equal than 1\n// Got: 1\n"},{"name":"z_int_3_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsInt().ToBeLowerThan(1)\n\texpect.Value(t, 1).AsInt().Not().ToBeLowerThan(2)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be lower than 1\n// Got: 1\n// Expected value to not to be lower than 2\n// Got: 1\n"},{"name":"z_int_4_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsInt().ToBeLowerOrEqualThan(0)\n\texpect.Value(t, 1).AsInt().Not().ToBeLowerOrEqualThan(1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be lower or equal than 0\n// Got: 1\n// Expected value to not to be lower or equal than 1\n// Got: 1\n"},{"name":"z_string_0_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, \"foo\").AsString().ToEqual(\"bar\")\n\texpect.Value(t, \"foo\").AsString().Not().ToEqual(\"foo\")\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to be different\n// Got: foo\n"},{"name":"z_string_1_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, \"foo\").AsString().ToBeEmpty()\n\texpect.Value(t, \"\").AsString().Not().ToBeEmpty()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected string to be empty\n// Got: foo\n// Unexpected empty string\n"},{"name":"z_string_2_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, \"foo\").AsString().ToHaveLength(2)\n\texpect.Value(t, \"foo\").AsString().Not().ToHaveLength(3)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected string length to match\n// Got: 3\n// Want: 2\n// Expected string lengths to be different\n// Got: 3\n"},{"name":"z_uint_0_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsUint().ToEqual(2)\n\texpect.Value(t, 1).AsUint().Not().ToEqual(1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected value to be different\n// Got: 1\n"},{"name":"z_uint_1_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsUint().ToBeGreaterThan(2)\n\texpect.Value(t, 1).AsUint().Not().ToBeGreaterThan(0)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be gerater than 2\n// Got: 1\n// Expected value to not to be greater than 0\n// Got: 1\n"},{"name":"z_uint_2_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsUint().ToBeGreaterOrEqualThan(2)\n\texpect.Value(t, 1).AsUint().Not().ToBeGreaterOrEqualThan(1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be greater or equal than 2\n// Got: 1\n// Expected value to not to be greater or equal than 1\n// Got: 1\n"},{"name":"z_uint_3_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsUint().ToBeLowerThan(1)\n\texpect.Value(t, 1).AsUint().Not().ToBeLowerThan(2)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be lower than 1\n// Got: 1\n// Expected value to not to be lower than 2\n// Got: 1\n"},{"name":"z_uint_4_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsUint().ToBeLowerOrEqualThan(0)\n\texpect.Value(t, 1).AsUint().Not().ToBeLowerOrEqualThan(1)\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be lower or equal than 0\n// Got: 1\n// Expected value to not to be lower or equal than 1\n// Got: 1\n"},{"name":"z_value_0_filetest.gno","body":"package main\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\ntype stringer string\n\nfunc (s stringer) String() string { return string(s) }\n\nfunc main() {\n\texpect.Value(t, \"foo\").ToEqual(\"bar\")\n\texpect.Value(t, []byte(\"foo\")).ToEqual([]byte(\"bar\"))\n\texpect.Value(t, stringer(\"foo\")).ToEqual(stringer(\"bar\"))\n\texpect.Value(t, true).ToEqual(false)\n\texpect.Value(t, float32(1)).ToEqual(float32(2))\n\texpect.Value(t, float64(1.1)).ToEqual(float64(1.2))\n\texpect.Value(t, uint(1)).ToEqual(uint(2))\n\texpect.Value(t, uint8(1)).ToEqual(uint8(2))\n\texpect.Value(t, uint16(1)).ToEqual(uint16(2))\n\texpect.Value(t, uint32(1)).ToEqual(uint32(2))\n\texpect.Value(t, uint64(1)).ToEqual(uint64(2))\n\texpect.Value(t, int(1)).ToEqual(int(2))\n\texpect.Value(t, int8(1)).ToEqual(int8(2))\n\texpect.Value(t, int16(1)).ToEqual(int16(2))\n\texpect.Value(t, int32(1)).ToEqual(int32(2))\n\texpect.Value(t, int64(1)).ToEqual(int64(2))\n\texpect.Value(t, errors.New(\"foo\")).ToEqual(errors.New(\"bar\"))\n\texpect.Value(t, errors.New(\"foo\")).ToContainErrorString(\"bar\")\n\n\texpect.Value(t, 0).ToEqual(errors.New(\"foo\"))\n\texpect.Value(t, 0).ToEqual([]string{})\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to match\n// Got: foo\n// Want: bar\n// Expected values to match\n// Got: true\n// Want: false\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1.1\n// Want: 1.2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected values to match\n// Got: 1\n// Want: 2\n// Expected errors to match\n// Got: foo\n// Want: bar\n// Expected error message to contain: bar\n// Got: foo\n// Error is not equal to value\n// Got: foo\n// Unsupported type: unknown\n"},{"name":"z_value_1_filetest.gno","body":"package main\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\ntype stringer string\n\nfunc (s stringer) String() string { return string(s) }\n\nfunc main() {\n\texpect.Value(t, \"foo\").Not().ToEqual(\"foo\")\n\texpect.Value(t, []byte(\"foo\")).Not().ToEqual([]byte(\"foo\"))\n\texpect.Value(t, stringer(\"foo\")).Not().ToEqual(stringer(\"foo\"))\n\texpect.Value(t, true).Not().ToEqual(true)\n\texpect.Value(t, float32(1)).Not().ToEqual(float32(1))\n\texpect.Value(t, float64(1)).Not().ToEqual(float64(1))\n\texpect.Value(t, uint(1)).Not().ToEqual(uint(1))\n\texpect.Value(t, uint8(1)).Not().ToEqual(uint8(1))\n\texpect.Value(t, uint16(1)).Not().ToEqual(uint16(1))\n\texpect.Value(t, uint32(1)).Not().ToEqual(uint32(1))\n\texpect.Value(t, uint64(1)).Not().ToEqual(uint64(1))\n\texpect.Value(t, int(1)).Not().ToEqual(int(1))\n\texpect.Value(t, int8(1)).Not().ToEqual(int8(1))\n\texpect.Value(t, int16(1)).Not().ToEqual(int16(1))\n\texpect.Value(t, int32(1)).Not().ToEqual(int32(1))\n\texpect.Value(t, int64(1)).Not().ToEqual(int64(1))\n\texpect.Value(t, errors.New(\"foo\")).Not().ToEqual(errors.New(\"foo\"))\n\texpect.Value(t, errors.New(\"foo bar\")).Not().ToContainErrorString(\"bar\")\n\n\texpect.Value(t, 0).Not().ToEqual(errors.New(\"foo\"))\n\texpect.Value(t, 0).Not().ToEqual([]string{})\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected values to be different\n// Got: foo\n// Expected values to be different\n// Got: foo\n// Expected values to be different\n// Got: foo\n// Expected values to be different\n// Got: true\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected value to be different\n// Got: 1\n// Expected errors to be different\n// Got: foo\n// Expected error message not to contain: bar\n// Got: foo bar\n// Error is not equal to value\n// Got: foo\n// Unsupported type: unknown\n"},{"name":"z_value_2_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, \"foo\").ToBeNil()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected value to be nil\n// Got: foo\n"},{"name":"z_value_3_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, nil).Not().ToBeNil()\n\texpect.Value(t, (*int)(nil)).Not().ToBeNil()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Expected a non nil value\n// Expected a non nil value\n"},{"name":"z_value_4_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, nil).WithFailPrefix(\"Foo prefix\").Not().ToBeNil()\n\texpect.Value(t, (*int)(nil)).WithFailPrefix(\"Foo prefix\").Not().ToBeNil()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Foo prefix - Expected a non nil value\n// Foo prefix - Expected a non nil value\n"},{"name":"z_value_5_filetest.gno","body":"package main\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n)\n\nvar (\n\toutput strings.Builder\n\tt      = expect.MockTestingT(\u0026output)\n)\n\nfunc main() {\n\texpect.Value(t, 1).AsString()\n\texpect.Value(t, 1).AsBoolean()\n\texpect.Value(t, 1).AsFloat()\n\texpect.Value(t, 1).AsUint()\n\texpect.Value(t, \"\").AsInt()\n\n\tprintln(output.String())\n}\n\n// Output:\n// Failed: incompatible type: expected a string value\n// Got: int\n// Failed: incompatible type: expected a boolean value\n// Got: int\n// incompatible type: expected a float value\n// Got: int\n// Failed: incompatible type: expected an int value\n// Got: string\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"OXH2zXBZjo16MTAR7luxV01RRTHgnd4pNp4gNf/FbAAxJf+d7B5OEGEF3XdYmhLYNcxplbPm5q9EjlQWaYh6Cw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"typeutil","path":"gno.land/p/moul/typeutil","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/typeutil\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"typeutil.gno","body":"// Package typeutil provides utility functions for converting between different types\n// and checking their states. It aims to provide consistent behavior across different\n// types while remaining lightweight and dependency-free.\npackage typeutil\n\nimport (\n\t\"errors\"\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n)\n\n// stringer is the interface that wraps the String method.\ntype stringer interface {\n\tString() string\n}\n\n// ToString converts any value to its string representation.\n// It supports a wide range of Go types including:\n//   - Basic: string, bool\n//   - Numbers: int, int8-64, uint, uint8-64, float32, float64\n//   - Special: time.Time, address, []byte\n//   - Slices: []T for most basic types\n//   - Maps: map[string]string, map[string]any\n//   - Interface: types implementing String() string\n//\n// Example usage:\n//\n//\tstr := typeutil.ToString(42)               // \"42\"\n//\tstr = typeutil.ToString([]int{1, 2})      // \"[1 2]\"\n//\tstr = typeutil.ToString(map[string]string{ // \"map[a:1 b:2]\"\n//\t    \"a\": \"1\",\n//\t    \"b\": \"2\",\n//\t})\nfunc ToString(val any) string {\n\tif val == nil {\n\t\treturn \"\"\n\t}\n\n\t// First check if value implements Stringer interface\n\tif s, ok := val.(interface{ String() string }); ok {\n\t\treturn s.String()\n\t}\n\n\tswitch v := val.(type) {\n\t// Pointer types - dereference and recurse\n\tcase *string:\n\t\tif v == nil {\n\t\t\treturn \"\"\n\t\t}\n\t\treturn *v\n\tcase *int:\n\t\tif v == nil {\n\t\t\treturn \"\"\n\t\t}\n\t\treturn strconv.Itoa(*v)\n\tcase *bool:\n\t\tif v == nil {\n\t\t\treturn \"\"\n\t\t}\n\t\treturn strconv.FormatBool(*v)\n\tcase *time.Time:\n\t\tif v == nil {\n\t\t\treturn \"\"\n\t\t}\n\t\treturn v.String()\n\tcase *address:\n\t\tif v == nil {\n\t\t\treturn \"\"\n\t\t}\n\t\treturn string(*v)\n\n\t// String types\n\tcase string:\n\t\treturn v\n\tcase stringer:\n\t\treturn v.String()\n\n\t// Special types\n\tcase time.Time:\n\t\treturn v.String()\n\tcase address:\n\t\treturn string(v)\n\tcase []byte:\n\t\treturn string(v)\n\tcase struct{}:\n\t\treturn \"{}\"\n\n\t// Integer types\n\tcase int:\n\t\treturn strconv.Itoa(v)\n\tcase int8:\n\t\treturn strconv.FormatInt(int64(v), 10)\n\tcase int16:\n\t\treturn strconv.FormatInt(int64(v), 10)\n\tcase int32:\n\t\treturn strconv.FormatInt(int64(v), 10)\n\tcase int64:\n\t\treturn strconv.FormatInt(v, 10)\n\tcase uint:\n\t\treturn strconv.FormatUint(uint64(v), 10)\n\tcase uint8:\n\t\treturn strconv.FormatUint(uint64(v), 10)\n\tcase uint16:\n\t\treturn strconv.FormatUint(uint64(v), 10)\n\tcase uint32:\n\t\treturn strconv.FormatUint(uint64(v), 10)\n\tcase uint64:\n\t\treturn strconv.FormatUint(v, 10)\n\n\t// Float types\n\tcase float32:\n\t\treturn strconv.FormatFloat(float64(v), 'f', -1, 32)\n\tcase float64:\n\t\treturn strconv.FormatFloat(v, 'f', -1, 64)\n\n\t// Boolean\n\tcase bool:\n\t\tif v {\n\t\t\treturn \"true\"\n\t\t}\n\t\treturn \"false\"\n\n\t// Slice types\n\tcase []string:\n\t\treturn join(v)\n\tcase []int:\n\t\treturn join(v)\n\tcase []int32:\n\t\treturn join(v)\n\tcase []int64:\n\t\treturn join(v)\n\tcase []float32:\n\t\treturn join(v)\n\tcase []float64:\n\t\treturn join(v)\n\tcase []any:\n\t\treturn join(v)\n\tcase []time.Time:\n\t\treturn joinTimes(v)\n\tcase []stringer:\n\t\treturn join(v)\n\tcase []address:\n\t\treturn joinAddresses(v)\n\tcase [][]byte:\n\t\treturn joinBytes(v)\n\n\t// Map types with various key types\n\tcase map[any]any, map[string]any, map[string]string, map[string]int:\n\t\tvar b strings.Builder\n\t\tb.WriteString(\"map[\")\n\t\tfirst := true\n\n\t\tswitch m := v.(type) {\n\t\tcase map[any]any:\n\t\t\t// Convert all keys to strings for consistent ordering\n\t\t\tkeys := make([]string, 0)\n\t\t\tkeyMap := make(map[string]any)\n\n\t\t\tfor k := range m {\n\t\t\t\tkeyStr := ToString(k)\n\t\t\t\tkeys = append(keys, keyStr)\n\t\t\t\tkeyMap[keyStr] = k\n\t\t\t}\n\t\t\tsort.Strings(keys)\n\n\t\t\tfor _, keyStr := range keys {\n\t\t\t\tif !first {\n\t\t\t\t\tb.WriteString(\" \")\n\t\t\t\t}\n\t\t\t\torigKey := keyMap[keyStr]\n\t\t\t\tb.WriteString(keyStr)\n\t\t\t\tb.WriteString(\":\")\n\t\t\t\tb.WriteString(ToString(m[origKey]))\n\t\t\t\tfirst = false\n\t\t\t}\n\n\t\tcase map[string]any:\n\t\t\tkeys := make([]string, 0)\n\t\t\tfor k := range m {\n\t\t\t\tkeys = append(keys, k)\n\t\t\t}\n\t\t\tsort.Strings(keys)\n\n\t\t\tfor _, k := range keys {\n\t\t\t\tif !first {\n\t\t\t\t\tb.WriteString(\" \")\n\t\t\t\t}\n\t\t\t\tb.WriteString(k)\n\t\t\t\tb.WriteString(\":\")\n\t\t\t\tb.WriteString(ToString(m[k]))\n\t\t\t\tfirst = false\n\t\t\t}\n\n\t\tcase map[string]string:\n\t\t\tkeys := make([]string, 0)\n\t\t\tfor k := range m {\n\t\t\t\tkeys = append(keys, k)\n\t\t\t}\n\t\t\tsort.Strings(keys)\n\n\t\t\tfor _, k := range keys {\n\t\t\t\tif !first {\n\t\t\t\t\tb.WriteString(\" \")\n\t\t\t\t}\n\t\t\t\tb.WriteString(k)\n\t\t\t\tb.WriteString(\":\")\n\t\t\t\tb.WriteString(m[k])\n\t\t\t\tfirst = false\n\t\t\t}\n\n\t\tcase map[string]int:\n\t\t\tkeys := make([]string, 0)\n\t\t\tfor k := range m {\n\t\t\t\tkeys = append(keys, k)\n\t\t\t}\n\t\t\tsort.Strings(keys)\n\n\t\t\tfor _, k := range keys {\n\t\t\t\tif !first {\n\t\t\t\t\tb.WriteString(\" \")\n\t\t\t\t}\n\t\t\t\tb.WriteString(k)\n\t\t\t\tb.WriteString(\":\")\n\t\t\t\tb.WriteString(strconv.Itoa(m[k]))\n\t\t\t\tfirst = false\n\t\t\t}\n\t\t}\n\t\tb.WriteString(\"]\")\n\t\treturn b.String()\n\n\t// Default\n\tdefault:\n\t\treturn \"\u003cunknown\u003e\"\n\t}\n}\n\nfunc join(slice any) string {\n\tif IsZero(slice) {\n\t\treturn \"[]\"\n\t}\n\n\titems := ToInterfaceSlice(slice)\n\tif items == nil {\n\t\treturn \"[]\"\n\t}\n\n\tvar b strings.Builder\n\tb.WriteString(\"[\")\n\tfor i, item := range items {\n\t\tif i \u003e 0 {\n\t\t\tb.WriteString(\" \")\n\t\t}\n\t\tb.WriteString(ToString(item))\n\t}\n\tb.WriteString(\"]\")\n\treturn b.String()\n}\n\nfunc joinTimes(slice []time.Time) string {\n\tif len(slice) == 0 {\n\t\treturn \"[]\"\n\t}\n\tvar b strings.Builder\n\tb.WriteString(\"[\")\n\tfor i, t := range slice {\n\t\tif i \u003e 0 {\n\t\t\tb.WriteString(\" \")\n\t\t}\n\t\tb.WriteString(t.String())\n\t}\n\tb.WriteString(\"]\")\n\treturn b.String()\n}\n\nfunc joinAddresses(slice []address) string {\n\tif len(slice) == 0 {\n\t\treturn \"[]\"\n\t}\n\tvar b strings.Builder\n\tb.WriteString(\"[\")\n\tfor i, addr := range slice {\n\t\tif i \u003e 0 {\n\t\t\tb.WriteString(\" \")\n\t\t}\n\t\tb.WriteString(string(addr))\n\t}\n\tb.WriteString(\"]\")\n\treturn b.String()\n}\n\nfunc joinBytes(slice [][]byte) string {\n\tif len(slice) == 0 {\n\t\treturn \"[]\"\n\t}\n\tvar b strings.Builder\n\tb.WriteString(\"[\")\n\tfor i, bytes := range slice {\n\t\tif i \u003e 0 {\n\t\t\tb.WriteString(\" \")\n\t\t}\n\t\tb.WriteString(string(bytes))\n\t}\n\tb.WriteString(\"]\")\n\treturn b.String()\n}\n\n// ToBool converts any value to a boolean based on common programming conventions.\n// For example:\n//   - Numbers: 0 is false, any other number is true\n//   - Strings: \"\", \"0\", \"false\", \"f\", \"no\", \"n\", \"off\" are false, others are true\n//   - Slices/Maps: empty is false, non-empty is true\n//   - nil: always false\n//   - bool: direct value\nfunc ToBool(val any) bool {\n\tif IsZero(val) {\n\t\treturn false\n\t}\n\n\t// Handle special string cases\n\tif str, ok := val.(string); ok {\n\t\tstr = strings.ToLower(strings.TrimSpace(str))\n\t\treturn str != \"\" \u0026\u0026 str != \"0\" \u0026\u0026 str != \"false\" \u0026\u0026 str != \"f\" \u0026\u0026 str != \"no\" \u0026\u0026 str != \"n\" \u0026\u0026 str != \"off\"\n\t}\n\n\treturn true\n}\n\n// IsZero returns true if the value represents a \"zero\" or \"empty\" state for its type.\n// For example:\n//   - Numbers: 0\n//   - Strings: \"\"\n//   - Slices/Maps: empty\n//   - nil: true\n//   - bool: false\n//   - time.Time: IsZero()\n//   - address: empty string\nfunc IsZero(val any) bool {\n\tif val == nil {\n\t\treturn true\n\t}\n\n\tswitch v := val.(type) {\n\t// Pointer types - nil pointer is zero, otherwise check pointed value\n\tcase *bool:\n\t\treturn v == nil || !*v\n\tcase *string:\n\t\treturn v == nil || *v == \"\"\n\tcase *int:\n\t\treturn v == nil || *v == 0\n\tcase *time.Time:\n\t\treturn v == nil || v.IsZero()\n\tcase *address:\n\t\treturn v == nil || string(*v) == \"\"\n\n\t// Bool\n\tcase bool:\n\t\treturn !v\n\n\t// String types\n\tcase string:\n\t\treturn v == \"\"\n\tcase stringer:\n\t\treturn v.String() == \"\"\n\n\t// Integer types\n\tcase int:\n\t\treturn v == 0\n\tcase int8:\n\t\treturn v == 0\n\tcase int16:\n\t\treturn v == 0\n\tcase int32:\n\t\treturn v == 0\n\tcase int64:\n\t\treturn v == 0\n\tcase uint:\n\t\treturn v == 0\n\tcase uint8:\n\t\treturn v == 0\n\tcase uint16:\n\t\treturn v == 0\n\tcase uint32:\n\t\treturn v == 0\n\tcase uint64:\n\t\treturn v == 0\n\n\t// Float types\n\tcase float32:\n\t\treturn v == 0\n\tcase float64:\n\t\treturn v == 0\n\n\t// Special types\n\tcase []byte:\n\t\treturn len(v) == 0\n\tcase time.Time:\n\t\treturn v.IsZero()\n\tcase address:\n\t\treturn string(v) == \"\"\n\n\t// Slices (check if empty)\n\tcase []string:\n\t\treturn len(v) == 0\n\tcase []int:\n\t\treturn len(v) == 0\n\tcase []int32:\n\t\treturn len(v) == 0\n\tcase []int64:\n\t\treturn len(v) == 0\n\tcase []float32:\n\t\treturn len(v) == 0\n\tcase []float64:\n\t\treturn len(v) == 0\n\tcase []any:\n\t\treturn len(v) == 0\n\tcase []time.Time:\n\t\treturn len(v) == 0\n\tcase []address:\n\t\treturn len(v) == 0\n\tcase [][]byte:\n\t\treturn len(v) == 0\n\tcase []stringer:\n\t\treturn len(v) == 0\n\n\t// Maps (check if empty)\n\tcase map[string]string:\n\t\treturn len(v) == 0\n\tcase map[string]any:\n\t\treturn len(v) == 0\n\n\tdefault:\n\t\treturn false // non-nil unknown types are considered non-zero\n\t}\n}\n\n// ToInterfaceSlice converts various slice types to []any\nfunc ToInterfaceSlice(val any) []any {\n\tswitch v := val.(type) {\n\tcase []any:\n\t\treturn v\n\tcase []string:\n\t\tresult := make([]any, len(v))\n\t\tfor i, s := range v {\n\t\t\tresult[i] = s\n\t\t}\n\t\treturn result\n\tcase []int:\n\t\tresult := make([]any, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = n\n\t\t}\n\t\treturn result\n\tcase []int32:\n\t\tresult := make([]any, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = n\n\t\t}\n\t\treturn result\n\tcase []int64:\n\t\tresult := make([]any, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = n\n\t\t}\n\t\treturn result\n\tcase []float32:\n\t\tresult := make([]any, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = n\n\t\t}\n\t\treturn result\n\tcase []float64:\n\t\tresult := make([]any, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = n\n\t\t}\n\t\treturn result\n\tcase []bool:\n\t\tresult := make([]any, len(v))\n\t\tfor i, b := range v {\n\t\t\tresult[i] = b\n\t\t}\n\t\treturn result\n\tdefault:\n\t\treturn nil\n\t}\n}\n\n// ToMapStringInterface converts a map with string keys and any value type to map[string]any\nfunc ToMapStringInterface(m any) (map[string]any, error) {\n\tresult := make(map[string]any)\n\n\tswitch v := m.(type) {\n\tcase map[string]any:\n\t\treturn v, nil\n\tcase map[string]string:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[string]int:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[string]int64:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[string]float64:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[string]bool:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[string][]string:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = ToInterfaceSlice(val)\n\t\t}\n\tcase map[string][]int:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = ToInterfaceSlice(val)\n\t\t}\n\tcase map[string][]any:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[string]map[string]any:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[string]map[string]string:\n\t\tfor k, val := range v {\n\t\t\tif converted, err := ToMapStringInterface(val); err == nil {\n\t\t\t\tresult[k] = converted\n\t\t\t} else {\n\t\t\t\treturn nil, errors.New(\"failed to convert nested map at key: \" + k)\n\t\t\t}\n\t\t}\n\tdefault:\n\t\treturn nil, errors.New(\"unsupported map type: \" + ToString(m))\n\t}\n\n\treturn result, nil\n}\n\n// ToMapIntInterface converts a map with int keys and any value type to map[int]any\nfunc ToMapIntInterface(m any) (map[int]any, error) {\n\tresult := make(map[int]any)\n\n\tswitch v := m.(type) {\n\tcase map[int]any:\n\t\treturn v, nil\n\tcase map[int]string:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[int]int:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[int]int64:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[int]float64:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[int]bool:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[int][]string:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = ToInterfaceSlice(val)\n\t\t}\n\tcase map[int][]int:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = ToInterfaceSlice(val)\n\t\t}\n\tcase map[int][]any:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[int]map[string]any:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tcase map[int]map[int]any:\n\t\tfor k, val := range v {\n\t\t\tresult[k] = val\n\t\t}\n\tdefault:\n\t\treturn nil, errors.New(\"unsupported map type: \" + ToString(m))\n\t}\n\n\treturn result, nil\n}\n\n// ToStringSlice converts various slice types to []string\nfunc ToStringSlice(val any) []string {\n\tswitch v := val.(type) {\n\tcase []string:\n\t\treturn v\n\tcase []any:\n\t\tresult := make([]string, len(v))\n\t\tfor i, item := range v {\n\t\t\tresult[i] = ToString(item)\n\t\t}\n\t\treturn result\n\tcase []int:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.Itoa(n)\n\t\t}\n\t\treturn result\n\tcase []int32:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatInt(int64(n), 10)\n\t\t}\n\t\treturn result\n\tcase []int64:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatInt(n, 10)\n\t\t}\n\t\treturn result\n\tcase []float32:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatFloat(float64(n), 'f', -1, 32)\n\t\t}\n\t\treturn result\n\tcase []float64:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatFloat(n, 'f', -1, 64)\n\t\t}\n\t\treturn result\n\tcase []bool:\n\t\tresult := make([]string, len(v))\n\t\tfor i, b := range v {\n\t\t\tresult[i] = strconv.FormatBool(b)\n\t\t}\n\t\treturn result\n\tcase []time.Time:\n\t\tresult := make([]string, len(v))\n\t\tfor i, t := range v {\n\t\t\tresult[i] = t.String()\n\t\t}\n\t\treturn result\n\tcase []address:\n\t\tresult := make([]string, len(v))\n\t\tfor i, addr := range v {\n\t\t\tresult[i] = string(addr)\n\t\t}\n\t\treturn result\n\tcase [][]byte:\n\t\tresult := make([]string, len(v))\n\t\tfor i, b := range v {\n\t\t\tresult[i] = string(b)\n\t\t}\n\t\treturn result\n\tcase []stringer:\n\t\tresult := make([]string, len(v))\n\t\tfor i, s := range v {\n\t\t\tresult[i] = s.String()\n\t\t}\n\t\treturn result\n\tcase []uint:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatUint(uint64(n), 10)\n\t\t}\n\t\treturn result\n\tcase []uint8:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatUint(uint64(n), 10)\n\t\t}\n\t\treturn result\n\tcase []uint16:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatUint(uint64(n), 10)\n\t\t}\n\t\treturn result\n\tcase []uint32:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatUint(uint64(n), 10)\n\t\t}\n\t\treturn result\n\tcase []uint64:\n\t\tresult := make([]string, len(v))\n\t\tfor i, n := range v {\n\t\t\tresult[i] = strconv.FormatUint(n, 10)\n\t\t}\n\t\treturn result\n\tdefault:\n\t\t// Try to convert using reflection if it's a slice\n\t\tif slice := ToInterfaceSlice(val); slice != nil {\n\t\t\tresult := make([]string, len(slice))\n\t\t\tfor i, item := range slice {\n\t\t\t\tresult[i] = ToString(item)\n\t\t\t}\n\t\t\treturn result\n\t\t}\n\t\treturn nil\n\t}\n}\n"},{"name":"typeutil_test.gno","body":"package typeutil\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n)\n\ntype testStringer struct {\n\tvalue string\n}\n\nfunc (t testStringer) String() string {\n\treturn \"test:\" + t.value\n}\n\nfunc TestToString(t *testing.T) {\n\t// setup test data\n\tstr := \"hello\"\n\tnum := 42\n\tb := true\n\tnow := time.Now()\n\taddr := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tstringer := testStringer{value: \"hello\"}\n\n\ttype testCase struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected string\n\t}\n\n\ttests := []testCase{\n\t\t// basic types\n\t\t{\"string\", \"hello\", \"hello\"},\n\t\t{\"empty_string\", \"\", \"\"},\n\t\t{\"nil\", nil, \"\"},\n\n\t\t// integer types\n\t\t{\"int\", 42, \"42\"},\n\t\t{\"int8\", int8(8), \"8\"},\n\t\t{\"int16\", int16(16), \"16\"},\n\t\t{\"int32\", int32(32), \"32\"},\n\t\t{\"int64\", int64(64), \"64\"},\n\t\t{\"uint\", uint(42), \"42\"},\n\t\t{\"uint8\", uint8(8), \"8\"},\n\t\t{\"uint16\", uint16(16), \"16\"},\n\t\t{\"uint32\", uint32(32), \"32\"},\n\t\t{\"uint64\", uint64(64), \"64\"},\n\n\t\t// float types\n\t\t{\"float32\", float32(3.14), \"3.14\"},\n\t\t{\"float64\", 3.14159, \"3.14159\"},\n\n\t\t// boolean\n\t\t{\"bool_true\", true, \"true\"},\n\t\t{\"bool_false\", false, \"false\"},\n\n\t\t// special types\n\t\t{\"time\", now, now.String()},\n\t\t{\"address\", addr, string(addr)},\n\t\t{\"bytes\", []byte(\"hello\"), \"hello\"},\n\t\t{\"stringer\", stringer, \"test:hello\"},\n\n\t\t// slices\n\t\t{\"empty_slice\", []string{}, \"[]\"},\n\t\t{\"string_slice\", []string{\"a\", \"b\"}, \"[a b]\"},\n\t\t{\"int_slice\", []int{1, 2}, \"[1 2]\"},\n\t\t{\"int32_slice\", []int32{1, 2}, \"[1 2]\"},\n\t\t{\"int64_slice\", []int64{1, 2}, \"[1 2]\"},\n\t\t{\"float32_slice\", []float32{1.1, 2.2}, \"[1.1 2.2]\"},\n\t\t{\"float64_slice\", []float64{1.1, 2.2}, \"[1.1 2.2]\"},\n\t\t{\"bytes_slice\", [][]byte{[]byte(\"a\"), []byte(\"b\")}, \"[a b]\"},\n\t\t{\"time_slice\", []time.Time{now, now}, \"[\" + now.String() + \" \" + now.String() + \"]\"},\n\t\t{\"address_slice\", []address{addr, addr}, \"[\" + string(addr) + \" \" + string(addr) + \"]\"},\n\t\t{\"interface_slice\", []any{1, \"a\", true}, \"[1 a true]\"},\n\n\t\t// empty slices\n\t\t{\"empty_string_slice\", []string{}, \"[]\"},\n\t\t{\"empty_int_slice\", []int{}, \"[]\"},\n\t\t{\"empty_int32_slice\", []int32{}, \"[]\"},\n\t\t{\"empty_int64_slice\", []int64{}, \"[]\"},\n\t\t{\"empty_float32_slice\", []float32{}, \"[]\"},\n\t\t{\"empty_float64_slice\", []float64{}, \"[]\"},\n\t\t{\"empty_bytes_slice\", [][]byte{}, \"[]\"},\n\t\t{\"empty_time_slice\", []time.Time{}, \"[]\"},\n\t\t{\"empty_address_slice\", []address{}, \"[]\"},\n\t\t{\"empty_interface_slice\", []any{}, \"[]\"},\n\n\t\t// maps\n\t\t{\"empty_string_map\", map[string]string{}, \"map[]\"},\n\t\t{\"string_map\", map[string]string{\"a\": \"1\", \"b\": \"2\"}, \"map[a:1 b:2]\"},\n\t\t{\"empty_interface_map\", map[string]any{}, \"map[]\"},\n\t\t{\"interface_map\", map[string]any{\"a\": 1, \"b\": \"2\"}, \"map[a:1 b:2]\"},\n\n\t\t// edge cases\n\t\t{\"empty_bytes\", []byte{}, \"\"},\n\t\t{\"nil_interface\", any(nil), \"\"},\n\t\t{\"empty_struct\", struct{}{}, \"{}\"},\n\t\t{\"unknown_type\", struct{ foo string }{}, \"\u003cunknown\u003e\"},\n\n\t\t// pointer types\n\t\t{\"nil_string_ptr\", (*string)(nil), \"\"},\n\t\t{\"string_ptr\", \u0026str, \"hello\"},\n\t\t{\"nil_int_ptr\", (*int)(nil), \"\"},\n\t\t{\"int_ptr\", \u0026num, \"42\"},\n\t\t{\"nil_bool_ptr\", (*bool)(nil), \"\"},\n\t\t{\"bool_ptr\", \u0026b, \"true\"},\n\t\t// {\"nil_time_ptr\", (*time.Time)(nil), \"\"}, // TODO: fix this\n\t\t{\"time_ptr\", \u0026now, now.String()},\n\t\t// {\"nil_address_ptr\", (*address)(nil), \"\"}, // TODO: fix this\n\t\t{\"address_ptr\", \u0026addr, string(addr)},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := ToString(tt.input)\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"%s: ToString(%v) = %q, want %q\", tt.name, tt.input, got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestToBool(t *testing.T) {\n\tstr := \"true\"\n\tnum := 42\n\tb := true\n\tnow := time.Now()\n\taddr := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tzero := 0\n\tempty := \"\"\n\tfalseVal := false\n\n\ttype testCase struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected bool\n\t}\n\n\ttests := []testCase{\n\t\t// basic types\n\t\t{\"true\", true, true},\n\t\t{\"false\", false, false},\n\t\t{\"nil\", nil, false},\n\n\t\t// strings\n\t\t{\"empty_string\", \"\", false},\n\t\t{\"zero_string\", \"0\", false},\n\t\t{\"false_string\", \"false\", false},\n\t\t{\"f_string\", \"f\", false},\n\t\t{\"no_string\", \"no\", false},\n\t\t{\"n_string\", \"n\", false},\n\t\t{\"off_string\", \"off\", false},\n\t\t{\"space_string\", \" \", false},\n\t\t{\"true_string\", \"true\", true},\n\t\t{\"yes_string\", \"yes\", true},\n\t\t{\"random_string\", \"hello\", true},\n\n\t\t// numbers\n\t\t{\"zero_int\", 0, false},\n\t\t{\"positive_int\", 1, true},\n\t\t{\"negative_int\", -1, true},\n\t\t{\"zero_float\", 0.0, false},\n\t\t{\"positive_float\", 0.1, true},\n\t\t{\"negative_float\", -0.1, true},\n\n\t\t// special types\n\t\t{\"empty_bytes\", []byte{}, false},\n\t\t{\"non_empty_bytes\", []byte{1}, true},\n\t\t/*{\"zero_time\", time.Time{}, false},*/ // TODO: fix this\n\t\t{\"empty_address\", address(\"\"), false},\n\n\t\t// slices\n\t\t{\"empty_slice\", []string{}, false},\n\t\t{\"non_empty_slice\", []string{\"a\"}, true},\n\n\t\t// maps\n\t\t{\"empty_map\", map[string]string{}, false},\n\t\t{\"non_empty_map\", map[string]string{\"a\": \"b\"}, true},\n\n\t\t// pointer types\n\t\t{\"nil_bool_ptr\", (*bool)(nil), false},\n\t\t{\"true_ptr\", \u0026b, true},\n\t\t{\"false_ptr\", \u0026falseVal, false},\n\t\t{\"nil_string_ptr\", (*string)(nil), false},\n\t\t{\"string_ptr\", \u0026str, true},\n\t\t{\"empty_string_ptr\", \u0026empty, false},\n\t\t{\"nil_int_ptr\", (*int)(nil), false},\n\t\t{\"int_ptr\", \u0026num, true},\n\t\t{\"zero_int_ptr\", \u0026zero, false},\n\t\t// {\"nil_time_ptr\", (*time.Time)(nil), false}, // TODO: fix this\n\t\t{\"time_ptr\", \u0026now, true},\n\t\t// {\"nil_address_ptr\", (*address)(nil), false}, // TODO: fix this\n\t\t{\"address_ptr\", \u0026addr, true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := ToBool(tt.input)\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"%s: ToBool(%v) = %v, want %v\", tt.name, tt.input, got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestIsZero(t *testing.T) {\n\tstr := \"hello\"\n\tnum := 42\n\tb := true\n\tnow := time.Now()\n\taddr := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tzero := 0\n\tempty := \"\"\n\tfalseVal := false\n\n\ttype testCase struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected bool\n\t}\n\n\ttests := []testCase{\n\t\t// basic types\n\t\t{\"true\", true, false},\n\t\t{\"false\", false, true},\n\t\t{\"nil\", nil, true},\n\n\t\t// strings\n\t\t{\"empty_string\", \"\", true},\n\t\t{\"non_empty_string\", \"hello\", false},\n\n\t\t// numbers\n\t\t{\"zero_int\", 0, true},\n\t\t{\"non_zero_int\", 1, false},\n\t\t{\"zero_float\", 0.0, true},\n\t\t{\"non_zero_float\", 0.1, false},\n\n\t\t// special types\n\t\t{\"empty_bytes\", []byte{}, true},\n\t\t{\"non_empty_bytes\", []byte{1}, false},\n\t\t/*{\"zero_time\", time.Time{}, true},*/ // TODO: fix this\n\t\t{\"empty_address\", address(\"\"), true},\n\n\t\t// slices\n\t\t{\"empty_slice\", []string{}, true},\n\t\t{\"non_empty_slice\", []string{\"a\"}, false},\n\n\t\t// maps\n\t\t{\"empty_map\", map[string]string{}, true},\n\t\t{\"non_empty_map\", map[string]string{\"a\": \"b\"}, false},\n\n\t\t// pointer types\n\t\t{\"nil_bool_ptr\", (*bool)(nil), true},\n\t\t{\"false_ptr\", \u0026falseVal, true},\n\t\t{\"true_ptr\", \u0026b, false},\n\t\t{\"nil_string_ptr\", (*string)(nil), true},\n\t\t{\"empty_string_ptr\", \u0026empty, true},\n\t\t{\"string_ptr\", \u0026str, false},\n\t\t{\"nil_int_ptr\", (*int)(nil), true},\n\t\t{\"zero_int_ptr\", \u0026zero, true},\n\t\t{\"int_ptr\", \u0026num, false},\n\t\t// {\"nil_time_ptr\", (*time.Time)(nil), true}, // TODO: fix this\n\t\t{\"time_ptr\", \u0026now, false},\n\t\t// {\"nil_address_ptr\", (*address)(nil), true}, // TODO: fix this\n\t\t{\"address_ptr\", \u0026addr, false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := IsZero(tt.input)\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"%s: IsZero(%v) = %v, want %v\", tt.name, tt.input, got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestToInterfaceSlice(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected []any\n\t\tcompare  func([]any, []any) bool\n\t}{\n\t\t{\n\t\t\tname:     \"nil\",\n\t\t\tinput:    nil,\n\t\t\texpected: nil,\n\t\t\tcompare:  compareNil,\n\t\t},\n\t\t{\n\t\t\tname:     \"empty_interface_slice\",\n\t\t\tinput:    []any{},\n\t\t\texpected: []any{},\n\t\t\tcompare:  compareEmpty,\n\t\t},\n\t\t{\n\t\t\tname:     \"interface_slice\",\n\t\t\tinput:    []any{1, \"two\", true},\n\t\t\texpected: []any{1, \"two\", true},\n\t\t\tcompare:  compareInterfaces,\n\t\t},\n\t\t{\n\t\t\tname:     \"string_slice\",\n\t\t\tinput:    []string{\"a\", \"b\", \"c\"},\n\t\t\texpected: []any{\"a\", \"b\", \"c\"},\n\t\t\tcompare:  compareStrings,\n\t\t},\n\t\t{\n\t\t\tname:     \"int_slice\",\n\t\t\tinput:    []int{1, 2, 3},\n\t\t\texpected: []any{1, 2, 3},\n\t\t\tcompare:  compareInts,\n\t\t},\n\t\t{\n\t\t\tname:     \"int32_slice\",\n\t\t\tinput:    []int32{1, 2, 3},\n\t\t\texpected: []any{int32(1), int32(2), int32(3)},\n\t\t\tcompare:  compareInt32s,\n\t\t},\n\t\t{\n\t\t\tname:     \"int64_slice\",\n\t\t\tinput:    []int64{1, 2, 3},\n\t\t\texpected: []any{int64(1), int64(2), int64(3)},\n\t\t\tcompare:  compareInt64s,\n\t\t},\n\t\t{\n\t\t\tname:     \"float32_slice\",\n\t\t\tinput:    []float32{1.1, 2.2, 3.3},\n\t\t\texpected: []any{float32(1.1), float32(2.2), float32(3.3)},\n\t\t\tcompare:  compareFloat32s,\n\t\t},\n\t\t{\n\t\t\tname:     \"float64_slice\",\n\t\t\tinput:    []float64{1.1, 2.2, 3.3},\n\t\t\texpected: []any{1.1, 2.2, 3.3},\n\t\t\tcompare:  compareFloat64s,\n\t\t},\n\t\t{\n\t\t\tname:     \"bool_slice\",\n\t\t\tinput:    []bool{true, false, true},\n\t\t\texpected: []any{true, false, true},\n\t\t\tcompare:  compareBools,\n\t\t},\n\t\t/* {\n\t\t\tname:     \"time_slice\",\n\t\t\tinput:    []time.Time{now},\n\t\t\texpected: []any{now},\n\t\t\tcompare:  compareTimes,\n\t\t}, */ // TODO: fix this\n\t\t/* {\n\t\t\tname:     \"address_slice\",\n\t\t\tinput:    []address{addr},\n\t\t\texpected: []any{addr},\n\t\t\tcompare:  compareAddresses,\n\t\t},*/ // TODO: fix this\n\t\t/* {\n\t\t\tname:     \"bytes_slice\",\n\t\t\tinput:    [][]byte{[]byte(\"hello\"), []byte(\"world\")},\n\t\t\texpected: []any{[]byte(\"hello\"), []byte(\"world\")},\n\t\t\tcompare:  compareBytes,\n\t\t},*/ // TODO: fix this\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := ToInterfaceSlice(tt.input)\n\t\t\tif !tt.compare(got, tt.expected) {\n\t\t\t\tt.Errorf(\"ToInterfaceSlice() = %v, want %v\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc compareNil(a, b []any) bool {\n\treturn a == nil \u0026\u0026 b == nil\n}\n\nfunc compareEmpty(a, b []any) bool {\n\treturn len(a) == 0 \u0026\u0026 len(b) == 0\n}\n\nfunc compareInterfaces(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareStrings(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tas, ok1 := a[i].(string)\n\t\tbs, ok2 := b[i].(string)\n\t\tif !ok1 || !ok2 || as != bs {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareInts(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tai, ok1 := a[i].(int)\n\t\tbi, ok2 := b[i].(int)\n\t\tif !ok1 || !ok2 || ai != bi {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareInt32s(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tai, ok1 := a[i].(int32)\n\t\tbi, ok2 := b[i].(int32)\n\t\tif !ok1 || !ok2 || ai != bi {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareInt64s(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tai, ok1 := a[i].(int64)\n\t\tbi, ok2 := b[i].(int64)\n\t\tif !ok1 || !ok2 || ai != bi {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareFloat32s(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tai, ok1 := a[i].(float32)\n\t\tbi, ok2 := b[i].(float32)\n\t\tif !ok1 || !ok2 || ai != bi {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareFloat64s(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tai, ok1 := a[i].(float64)\n\t\tbi, ok2 := b[i].(float64)\n\t\tif !ok1 || !ok2 || ai != bi {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareBools(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tab, ok1 := a[i].(bool)\n\t\tbb, ok2 := b[i].(bool)\n\t\tif !ok1 || !ok2 || ab != bb {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareTimes(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tat, ok1 := a[i].(time.Time)\n\t\tbt, ok2 := b[i].(time.Time)\n\t\tif !ok1 || !ok2 || !at.Equal(bt) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareAddresses(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\taa, ok1 := a[i].(address)\n\t\tba, ok2 := b[i].(address)\n\t\tif !ok1 || !ok2 || aa != ba {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc compareBytes(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tab, ok1 := a[i].([]byte)\n\t\tbb, ok2 := b[i].([]byte)\n\t\tif !ok1 || !ok2 || string(ab) != string(bb) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// compareStringInterfaceMaps compares two map[string]any for equality\nfunc compareStringInterfaceMaps(a, b map[string]any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor k, v1 := range a {\n\t\tv2, ok := b[k]\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\t\t// Compare values based on their type\n\t\tswitch val1 := v1.(type) {\n\t\tcase string:\n\t\t\tval2, ok := v2.(string)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase int:\n\t\t\tval2, ok := v2.(int)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase float64:\n\t\t\tval2, ok := v2.(float64)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase bool:\n\t\t\tval2, ok := v2.(bool)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase []any:\n\t\t\tval2, ok := v2.([]any)\n\t\t\tif !ok || len(val1) != len(val2) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tfor i := range val1 {\n\t\t\t\tif val1[i] != val2[i] {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t}\n\t\tcase map[string]any:\n\t\t\tval2, ok := v2.(map[string]any)\n\t\t\tif !ok || !compareStringInterfaceMaps(val1, val2) {\n\t\t\t\treturn false\n\t\t\t}\n\t\tdefault:\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc TestToMapStringInterface(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected map[string]any\n\t\twantErr  bool\n\t}{\n\t\t{\n\t\t\tname: \"map[string]any\",\n\t\t\tinput: map[string]any{\n\t\t\t\t\"key1\": \"value1\",\n\t\t\t\t\"key2\": 42,\n\t\t\t},\n\t\t\texpected: map[string]any{\n\t\t\t\t\"key1\": \"value1\",\n\t\t\t\t\"key2\": 42,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[string]string\",\n\t\t\tinput: map[string]string{\n\t\t\t\t\"key1\": \"value1\",\n\t\t\t\t\"key2\": \"value2\",\n\t\t\t},\n\t\t\texpected: map[string]any{\n\t\t\t\t\"key1\": \"value1\",\n\t\t\t\t\"key2\": \"value2\",\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[string]int\",\n\t\t\tinput: map[string]int{\n\t\t\t\t\"key1\": 1,\n\t\t\t\t\"key2\": 2,\n\t\t\t},\n\t\t\texpected: map[string]any{\n\t\t\t\t\"key1\": 1,\n\t\t\t\t\"key2\": 2,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[string]float64\",\n\t\t\tinput: map[string]float64{\n\t\t\t\t\"key1\": 1.1,\n\t\t\t\t\"key2\": 2.2,\n\t\t\t},\n\t\t\texpected: map[string]any{\n\t\t\t\t\"key1\": 1.1,\n\t\t\t\t\"key2\": 2.2,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[string]bool\",\n\t\t\tinput: map[string]bool{\n\t\t\t\t\"key1\": true,\n\t\t\t\t\"key2\": false,\n\t\t\t},\n\t\t\texpected: map[string]any{\n\t\t\t\t\"key1\": true,\n\t\t\t\t\"key2\": false,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[string][]string\",\n\t\t\tinput: map[string][]string{\n\t\t\t\t\"key1\": {\"a\", \"b\"},\n\t\t\t\t\"key2\": {\"c\", \"d\"},\n\t\t\t},\n\t\t\texpected: map[string]any{\n\t\t\t\t\"key1\": []any{\"a\", \"b\"},\n\t\t\t\t\"key2\": []any{\"c\", \"d\"},\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"nested map[string]map[string]string\",\n\t\t\tinput: map[string]map[string]string{\n\t\t\t\t\"key1\": {\"nested1\": \"value1\"},\n\t\t\t\t\"key2\": {\"nested2\": \"value2\"},\n\t\t\t},\n\t\t\texpected: map[string]any{\n\t\t\t\t\"key1\": map[string]any{\"nested1\": \"value1\"},\n\t\t\t\t\"key2\": map[string]any{\"nested2\": \"value2\"},\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"unsupported type\",\n\t\t\tinput:    42, // not a map\n\t\t\texpected: nil,\n\t\t\twantErr:  true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot, err := ToMapStringInterface(tt.input)\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"ToMapStringInterface() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif !tt.wantErr {\n\t\t\t\tif !compareStringInterfaceMaps(got, tt.expected) {\n\t\t\t\t\tt.Errorf(\"ToMapStringInterface() = %v, expected %v\", got, tt.expected)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\n// Test error messages\nfunc TestToMapStringInterfaceErrors(t *testing.T) {\n\t_, err := ToMapStringInterface(42)\n\tif err == nil || !strings.Contains(err.Error(), \"unsupported map type\") {\n\t\tt.Errorf(\"Expected error containing 'unsupported map type', got %v\", err)\n\t}\n}\n\n// compareIntInterfaceMaps compares two map[int]any for equality\nfunc compareIntInterfaceMaps(a, b map[int]any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor k, v1 := range a {\n\t\tv2, ok := b[k]\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\t\t// Compare values based on their type\n\t\tswitch val1 := v1.(type) {\n\t\tcase string:\n\t\t\tval2, ok := v2.(string)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase int:\n\t\t\tval2, ok := v2.(int)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase float64:\n\t\t\tval2, ok := v2.(float64)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase bool:\n\t\t\tval2, ok := v2.(bool)\n\t\t\tif !ok || val1 != val2 {\n\t\t\t\treturn false\n\t\t\t}\n\t\tcase []any:\n\t\t\tval2, ok := v2.([]any)\n\t\t\tif !ok || len(val1) != len(val2) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tfor i := range val1 {\n\t\t\t\tif val1[i] != val2[i] {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t}\n\t\tcase map[string]any:\n\t\t\tval2, ok := v2.(map[string]any)\n\t\t\tif !ok || !compareStringInterfaceMaps(val1, val2) {\n\t\t\t\treturn false\n\t\t\t}\n\t\tdefault:\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc TestToMapIntInterface(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected map[int]any\n\t\twantErr  bool\n\t}{\n\t\t{\n\t\t\tname: \"map[int]any\",\n\t\t\tinput: map[int]any{\n\t\t\t\t1: \"value1\",\n\t\t\t\t2: 42,\n\t\t\t},\n\t\t\texpected: map[int]any{\n\t\t\t\t1: \"value1\",\n\t\t\t\t2: 42,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[int]string\",\n\t\t\tinput: map[int]string{\n\t\t\t\t1: \"value1\",\n\t\t\t\t2: \"value2\",\n\t\t\t},\n\t\t\texpected: map[int]any{\n\t\t\t\t1: \"value1\",\n\t\t\t\t2: \"value2\",\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[int]int\",\n\t\t\tinput: map[int]int{\n\t\t\t\t1: 10,\n\t\t\t\t2: 20,\n\t\t\t},\n\t\t\texpected: map[int]any{\n\t\t\t\t1: 10,\n\t\t\t\t2: 20,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[int]float64\",\n\t\t\tinput: map[int]float64{\n\t\t\t\t1: 1.1,\n\t\t\t\t2: 2.2,\n\t\t\t},\n\t\t\texpected: map[int]any{\n\t\t\t\t1: 1.1,\n\t\t\t\t2: 2.2,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[int]bool\",\n\t\t\tinput: map[int]bool{\n\t\t\t\t1: true,\n\t\t\t\t2: false,\n\t\t\t},\n\t\t\texpected: map[int]any{\n\t\t\t\t1: true,\n\t\t\t\t2: false,\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[int][]string\",\n\t\t\tinput: map[int][]string{\n\t\t\t\t1: {\"a\", \"b\"},\n\t\t\t\t2: {\"c\", \"d\"},\n\t\t\t},\n\t\t\texpected: map[int]any{\n\t\t\t\t1: []any{\"a\", \"b\"},\n\t\t\t\t2: []any{\"c\", \"d\"},\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname: \"map[int]map[string]any\",\n\t\t\tinput: map[int]map[string]any{\n\t\t\t\t1: {\"nested1\": \"value1\"},\n\t\t\t\t2: {\"nested2\": \"value2\"},\n\t\t\t},\n\t\t\texpected: map[int]any{\n\t\t\t\t1: map[string]any{\"nested1\": \"value1\"},\n\t\t\t\t2: map[string]any{\"nested2\": \"value2\"},\n\t\t\t},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"unsupported type\",\n\t\t\tinput:    42, // not a map\n\t\t\texpected: nil,\n\t\t\twantErr:  true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot, err := ToMapIntInterface(tt.input)\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"ToMapIntInterface() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif !tt.wantErr {\n\t\t\t\tif !compareIntInterfaceMaps(got, tt.expected) {\n\t\t\t\t\tt.Errorf(\"ToMapIntInterface() = %v, expected %v\", got, tt.expected)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestToStringSlice(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\tname:     \"nil input\",\n\t\t\tinput:    nil,\n\t\t\texpected: nil,\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tinput:    []string{},\n\t\t\texpected: []string{},\n\t\t},\n\t\t{\n\t\t\tname:     \"string slice\",\n\t\t\tinput:    []string{\"a\", \"b\", \"c\"},\n\t\t\texpected: []string{\"a\", \"b\", \"c\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"int slice\",\n\t\t\tinput:    []int{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"int32 slice\",\n\t\t\tinput:    []int32{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"int64 slice\",\n\t\t\tinput:    []int64{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"uint slice\",\n\t\t\tinput:    []uint{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"uint8 slice\",\n\t\t\tinput:    []uint8{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"uint16 slice\",\n\t\t\tinput:    []uint16{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"uint32 slice\",\n\t\t\tinput:    []uint32{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"uint64 slice\",\n\t\t\tinput:    []uint64{1, 2, 3},\n\t\t\texpected: []string{\"1\", \"2\", \"3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"float32 slice\",\n\t\t\tinput:    []float32{1.1, 2.2, 3.3},\n\t\t\texpected: []string{\"1.1\", \"2.2\", \"3.3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"float64 slice\",\n\t\t\tinput:    []float64{1.1, 2.2, 3.3},\n\t\t\texpected: []string{\"1.1\", \"2.2\", \"3.3\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"bool slice\",\n\t\t\tinput:    []bool{true, false, true},\n\t\t\texpected: []string{\"true\", \"false\", \"true\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"[]byte slice\",\n\t\t\tinput:    [][]byte{[]byte(\"hello\"), []byte(\"world\")},\n\t\t\texpected: []string{\"hello\", \"world\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"interface slice\",\n\t\t\tinput:    []any{1, \"hello\", true},\n\t\t\texpected: []string{\"1\", \"hello\", \"true\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"time slice\",\n\t\t\tinput:    []time.Time{{}, {}},\n\t\t\texpected: []string{\"0001-01-01 00:00:00 +0000 UTC\", \"0001-01-01 00:00:00 +0000 UTC\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"address slice\",\n\t\t\tinput:    []address{\"addr1\", \"addr2\"},\n\t\t\texpected: []string{\"addr1\", \"addr2\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"non-slice input\",\n\t\t\tinput:    42,\n\t\t\texpected: nil,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := ToStringSlice(tt.input)\n\t\t\tif !slicesEqual(result, tt.expected) {\n\t\t\t\tt.Errorf(\"ToStringSlice(%v) = %v, want %v\", tt.input, result, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\n// Helper function to compare string slices\nfunc slicesEqual(a, b []string) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc TestToStringAdvanced(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    any\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname: \"slice with mixed basic types\",\n\t\t\tinput: []any{\n\t\t\t\t42,\n\t\t\t\t\"hello\",\n\t\t\t\ttrue,\n\t\t\t\t3.14,\n\t\t\t},\n\t\t\texpected: \"[42 hello true 3.14]\",\n\t\t},\n\t\t{\n\t\t\tname: \"map with basic types\",\n\t\t\tinput: map[string]any{\n\t\t\t\t\"int\":   42,\n\t\t\t\t\"str\":   \"hello\",\n\t\t\t\t\"bool\":  true,\n\t\t\t\t\"float\": 3.14,\n\t\t\t},\n\t\t\texpected: \"map[bool:true float:3.14 int:42 str:hello]\",\n\t\t},\n\t\t{\n\t\t\tname: \"mixed types map\",\n\t\t\tinput: map[any]any{\n\t\t\t\t42:         \"number\",\n\t\t\t\t\"string\":   123,\n\t\t\t\ttrue:       []int{1, 2, 3},\n\t\t\t\tstruct{}{}: \"empty\",\n\t\t\t},\n\t\t\texpected: \"map[42:number string:123 true:[1 2 3] {}:empty]\",\n\t\t},\n\t\t{\n\t\t\tname: \"nested maps\",\n\t\t\tinput: map[string]any{\n\t\t\t\t\"a\": map[string]int{\n\t\t\t\t\t\"x\": 1,\n\t\t\t\t\t\"y\": 2,\n\t\t\t\t},\n\t\t\t\t\"b\": []any{1, \"two\", true},\n\t\t\t},\n\t\t\texpected: \"map[a:map[x:1 y:2] b:[1 two true]]\",\n\t\t},\n\t\t{\n\t\t\tname:     \"empty struct\",\n\t\t\tinput:    struct{}{},\n\t\t\texpected: \"{}\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := ToString(tt.input)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"\\nToString(%v) =\\n%v\\nwant:\\n%v\", tt.input, result, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"txT+fLHB6/v169XddCVbRSsqGsf1hJe/KacIz1wNhLZ54z4DIurlppy374/x4PPw0rSpQuoaiPS0CHc1Dqa7DQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"ulist","path":"gno.land/p/moul/ulist","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/ulist\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"ulist.gno","body":"// Package ulist provides an append-only list implementation using a binary tree structure,\n// optimized for scenarios requiring sequential inserts with auto-incrementing indices.\n//\n// The implementation uses a binary tree where new elements are added by following a path\n// determined by the binary representation of the index. This provides automatic balancing\n// for append operations without requiring any balancing logic.\n//\n// Unlike the AVL tree-based list implementation (p/demo/avl/list), ulist is specifically\n// designed for append-only operations and does not require rebalancing. This makes it more\n// efficient for sequential inserts but less flexible for general-purpose list operations.\n//\n// Key differences from AVL list:\n// * Append-only design (no arbitrary inserts)\n// * No tree rebalancing needed\n// * Simpler implementation\n// * More memory efficient for sequential operations\n// * Less flexible than AVL (no arbitrary inserts/reordering)\n//\n// Key characteristics:\n// * O(log n) append and access operations\n// * Perfect balance for power-of-2 sizes\n// * No balancing needed\n// * Memory efficient\n// * Natural support for range queries\n// * Support for soft deletion of elements\n// * Forward and reverse iteration capabilities\n// * Offset-based iteration with count control\npackage ulist\n\n// TODO: Make avl/pager compatible in some way. Explain the limitations (not always 10 items because of nil ones).\n// TODO: Use this ulist in moul/collection for the primary index.\n// TODO: Consider adding a \"compact\" method that removes nil nodes.\n// TODO: Benchmarks.\n\nimport (\n\t\"errors\"\n)\n\n// List represents an append-only binary tree list\ntype List struct {\n\troot       *treeNode\n\ttotalSize  int\n\tactiveSize int\n}\n\n// Entry represents a key-value pair in the list, where Index is the position\n// and Value is the stored data\ntype Entry struct {\n\tIndex int\n\tValue any\n}\n\n// treeNode represents a node in the binary tree\ntype treeNode struct {\n\tdata  any\n\tleft  *treeNode\n\tright *treeNode\n}\n\n// Error variables\nvar (\n\tErrOutOfBounds = errors.New(\"index out of bounds\")\n\tErrDeleted     = errors.New(\"element already deleted\")\n)\n\n// New creates a new empty List instance\nfunc New() *List {\n\treturn \u0026List{}\n}\n\n// Append adds one or more values to the end of the list.\n// Values are added sequentially, and the list grows automatically.\nfunc (l *List) Append(values ...any) {\n\tfor _, value := range values {\n\t\tindex := l.totalSize\n\t\tnode := l.findNode(index, true)\n\t\tnode.data = value\n\t\tl.totalSize++\n\t\tl.activeSize++\n\t}\n}\n\n// Get retrieves the value at the specified index.\n// Returns nil if the index is out of bounds or if the element was deleted.\nfunc (l *List) Get(index int) any {\n\tnode := l.findNode(index, false)\n\tif node == nil {\n\t\treturn nil\n\t}\n\treturn node.data\n}\n\n// Delete marks the elements at the specified indices as deleted.\n// Returns ErrOutOfBounds if any index is invalid or ErrDeleted if\n// the element was already deleted.\nfunc (l *List) Delete(indices ...int) error {\n\tif len(indices) == 0 {\n\t\treturn nil\n\t}\n\tif l == nil || l.totalSize == 0 {\n\t\treturn ErrOutOfBounds\n\t}\n\n\tfor _, index := range indices {\n\t\tif index \u003c 0 || index \u003e= l.totalSize {\n\t\t\treturn ErrOutOfBounds\n\t\t}\n\n\t\tnode := l.findNode(index, false)\n\t\tif node == nil || node.data == nil {\n\t\t\treturn ErrDeleted\n\t\t}\n\t\tnode.data = nil\n\t\tl.activeSize--\n\t}\n\n\treturn nil\n}\n\n// Set updates or restores a value at the specified index if within bounds\n// Returns ErrOutOfBounds if the index is invalid\nfunc (l *List) Set(index int, value any) error {\n\tif l == nil || index \u003c 0 || index \u003e= l.totalSize {\n\t\treturn ErrOutOfBounds\n\t}\n\n\tnode := l.findNode(index, false)\n\tif node == nil {\n\t\treturn ErrOutOfBounds\n\t}\n\n\t// If this is restoring a deleted element\n\tif value != nil \u0026\u0026 node.data == nil {\n\t\tl.activeSize++\n\t}\n\n\t// If this is deleting an element\n\tif value == nil \u0026\u0026 node.data != nil {\n\t\tl.activeSize--\n\t}\n\n\tnode.data = value\n\treturn nil\n}\n\n// Size returns the number of active (non-deleted) elements in the list\nfunc (l *List) Size() int {\n\tif l == nil {\n\t\treturn 0\n\t}\n\treturn l.activeSize\n}\n\n// TotalSize returns the total number of elements ever added to the list,\n// including deleted elements\nfunc (l *List) TotalSize() int {\n\tif l == nil {\n\t\treturn 0\n\t}\n\treturn l.totalSize\n}\n\n// IterCbFn is a callback function type used in iteration methods.\n// Return true to stop iteration, false to continue.\ntype IterCbFn func(index int, value any) bool\n\n// Iterator performs iteration between start and end indices, calling cb for each entry.\n// If start \u003e end, iteration is performed in reverse order.\n// Returns true if iteration was stopped early by the callback returning true.\n// Skips deleted elements.\nfunc (l *List) Iterator(start, end int, cb IterCbFn) bool {\n\t// For empty list or invalid range\n\tif l == nil || l.totalSize == 0 {\n\t\treturn false\n\t}\n\tif start \u003c 0 \u0026\u0026 end \u003c 0 {\n\t\treturn false\n\t}\n\tif start \u003e= l.totalSize \u0026\u0026 end \u003e= l.totalSize {\n\t\treturn false\n\t}\n\n\t// Normalize indices\n\tif start \u003c 0 {\n\t\tstart = 0\n\t}\n\tif end \u003c 0 {\n\t\tend = 0\n\t}\n\tif end \u003e= l.totalSize {\n\t\tend = l.totalSize - 1\n\t}\n\tif start \u003e= l.totalSize {\n\t\tstart = l.totalSize - 1\n\t}\n\n\t// Handle reverse iteration\n\tif start \u003e end {\n\t\tfor i := start; i \u003e= end; i-- {\n\t\t\tval := l.Get(i)\n\t\t\tif val != nil {\n\t\t\t\tif cb(i, val) {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n\n\t// Handle forward iteration\n\tfor i := start; i \u003c= end; i++ {\n\t\tval := l.Get(i)\n\t\tif val != nil {\n\t\t\tif cb(i, val) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\n// IteratorByOffset performs iteration starting from offset for count elements.\n// If count is positive, iterates forward; if negative, iterates backward.\n// The iteration stops after abs(count) elements or when reaching list bounds.\n// Skips deleted elements.\nfunc (l *List) IteratorByOffset(offset int, count int, cb IterCbFn) bool {\n\tif count == 0 || l == nil || l.totalSize == 0 {\n\t\treturn false\n\t}\n\n\t// Normalize offset\n\tif offset \u003c 0 {\n\t\toffset = 0\n\t}\n\tif offset \u003e= l.totalSize {\n\t\toffset = l.totalSize - 1\n\t}\n\n\t// Determine end based on count direction\n\tvar end int\n\tif count \u003e 0 {\n\t\tend = l.totalSize - 1\n\t} else {\n\t\tend = 0\n\t}\n\n\twrapperReturned := false\n\n\t// Wrap the callback to limit iterations\n\tremaining := abs(count)\n\twrapper := func(index int, value any) bool {\n\t\tif remaining \u003c= 0 {\n\t\t\twrapperReturned = true\n\t\t\treturn true\n\t\t}\n\t\tremaining--\n\t\treturn cb(index, value)\n\t}\n\tret := l.Iterator(offset, end, wrapper)\n\tif wrapperReturned {\n\t\treturn false\n\t}\n\treturn ret\n}\n\n// abs returns the absolute value of x\nfunc abs(x int) int {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\n// findNode locates or creates a node at the given index in the binary tree.\n// The tree is structured such that the path to a node is determined by the binary\n// representation of the index. For example, a tree with 15 elements would look like:\n//\n//\t          0\n//\t       /      \\\n//\t     1         2\n//\t   /   \\     /   \\\n//\t  3    4    5     6\n//\t / \\  / \\  / \\   / \\\n//\t7  8 9 10 11 12 13 14\n//\n// To find index 13 (binary 1101):\n// 1. Start at root (0)\n// 2. Calculate bits needed (4 bits for index 13)\n// 3. Skip the highest bit position and start from bits-2\n// 4. Read bits from left to right:\n//   - 1 -\u003e go right to 2\n//   - 1 -\u003e go right to 6\n//   - 0 -\u003e go left to 13\n//\n// Special cases:\n// - Index 0 always returns the root node\n// - For create=true, missing nodes are created along the path\n// - For create=false, returns nil if any node is missing\nfunc (l *List) findNode(index int, create bool) *treeNode {\n\t// For read operations, check bounds strictly\n\tif !create \u0026\u0026 (l == nil || index \u003c 0 || index \u003e= l.totalSize) {\n\t\treturn nil\n\t}\n\n\t// For create operations, allow index == totalSize for append\n\tif create \u0026\u0026 (l == nil || index \u003c 0 || index \u003e l.totalSize) {\n\t\treturn nil\n\t}\n\n\t// Initialize root if needed\n\tif l.root == nil {\n\t\tif !create {\n\t\t\treturn nil\n\t\t}\n\t\tl.root = \u0026treeNode{}\n\t\treturn l.root\n\t}\n\n\tnode := l.root\n\n\t// Special case for root node\n\tif index == 0 {\n\t\treturn node\n\t}\n\n\t// Calculate the number of bits needed (inline highestBit logic)\n\tbits := 0\n\tn := index + 1\n\tfor n \u003e 0 {\n\t\tn \u003e\u003e= 1\n\t\tbits++\n\t}\n\n\t// Start from the second highest bit\n\tfor level := bits - 2; level \u003e= 0; level-- {\n\t\tbit := (index \u0026 (1 \u003c\u003c uint(level))) != 0\n\n\t\tif bit {\n\t\t\tif node.right == nil {\n\t\t\t\tif !create {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tnode.right = \u0026treeNode{}\n\t\t\t}\n\t\t\tnode = node.right\n\t\t} else {\n\t\t\tif node.left == nil {\n\t\t\t\tif !create {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tnode.left = \u0026treeNode{}\n\t\t\t}\n\t\t\tnode = node.left\n\t\t}\n\t}\n\n\treturn node\n}\n\n// MustDelete deletes elements at the specified indices.\n// Panics if any index is invalid or if any element was already deleted.\nfunc (l *List) MustDelete(indices ...int) {\n\tif err := l.Delete(indices...); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// MustGet retrieves the value at the specified index.\n// Panics if the index is out of bounds or if the element was deleted.\nfunc (l *List) MustGet(index int) any {\n\tif l == nil || index \u003c 0 || index \u003e= l.totalSize {\n\t\tpanic(ErrOutOfBounds)\n\t}\n\tvalue := l.Get(index)\n\tif value == nil {\n\t\tpanic(ErrDeleted)\n\t}\n\treturn value\n}\n\n// MustSet updates or restores a value at the specified index.\n// Panics if the index is out of bounds.\nfunc (l *List) MustSet(index int, value any) {\n\tif err := l.Set(index, value); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// GetRange returns a slice of Entry containing elements between start and end indices.\n// If start \u003e end, elements are returned in reverse order.\n// Deleted elements are skipped.\nfunc (l *List) GetRange(start, end int) []Entry {\n\tvar entries []Entry\n\tl.Iterator(start, end, func(index int, value any) bool {\n\t\tentries = append(entries, Entry{Index: index, Value: value})\n\t\treturn false\n\t})\n\treturn entries\n}\n\n// GetByOffset returns a slice of Entry starting from offset for count elements.\n// If count is positive, returns elements forward; if negative, returns elements backward.\n// The operation stops after abs(count) elements or when reaching list bounds.\n// Deleted elements are skipped.\nfunc (l *List) GetByOffset(offset int, count int) []Entry {\n\tvar entries []Entry\n\tl.IteratorByOffset(offset, count, func(index int, value any) bool {\n\t\tentries = append(entries, Entry{Index: index, Value: value})\n\t\treturn false\n\t})\n\treturn entries\n}\n\n// IList defines the interface for an ulist.List compatible structure.\ntype IList interface {\n\t// Basic operations\n\tAppend(values ...any)\n\tGet(index int) any\n\tDelete(indices ...int) error\n\tSize() int\n\tTotalSize() int\n\tSet(index int, value any) error\n\n\t// Must variants that panic instead of returning errors\n\tMustDelete(indices ...int)\n\tMustGet(index int) any\n\tMustSet(index int, value any)\n\n\t// Range operations\n\tGetRange(start, end int) []Entry\n\tGetByOffset(offset int, count int) []Entry\n\n\t// Iterator operations\n\tIterator(start, end int, cb IterCbFn) bool\n\tIteratorByOffset(offset int, count int, cb IterCbFn) bool\n}\n\n// Verify that List implements IList\nvar _ IList = (*List)(nil)\n"},{"name":"ulist_test.gno","body":"package ulist\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/typeutil\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestNew(t *testing.T) {\n\tl := New()\n\tuassert.Equal(t, 0, l.Size())\n\tuassert.Equal(t, 0, l.TotalSize())\n}\n\nfunc TestListAppendAndGet(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tsetup    func() *List\n\t\tindex    int\n\t\texpected any\n\t}{\n\t\t{\n\t\t\tname: \"empty list\",\n\t\t\tsetup: func() *List {\n\t\t\t\treturn New()\n\t\t\t},\n\t\t\tindex:    0,\n\t\t\texpected: nil,\n\t\t},\n\t\t{\n\t\t\tname: \"single append and get\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(42)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    0,\n\t\t\texpected: 42,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple appends and get first\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\tl.Append(2)\n\t\t\t\tl.Append(3)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    0,\n\t\t\texpected: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple appends and get last\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\tl.Append(2)\n\t\t\t\tl.Append(3)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    2,\n\t\t\texpected: 3,\n\t\t},\n\t\t{\n\t\t\tname: \"get with invalid index\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    1,\n\t\t\texpected: nil,\n\t\t},\n\t\t{\n\t\t\tname: \"31 items get first\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tfor i := 0; i \u003c 31; i++ {\n\t\t\t\t\tl.Append(i)\n\t\t\t\t}\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    0,\n\t\t\texpected: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"31 items get last\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tfor i := 0; i \u003c 31; i++ {\n\t\t\t\t\tl.Append(i)\n\t\t\t\t}\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    30,\n\t\t\texpected: 30,\n\t\t},\n\t\t{\n\t\t\tname: \"31 items get middle\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tfor i := 0; i \u003c 31; i++ {\n\t\t\t\t\tl.Append(i)\n\t\t\t\t}\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    15,\n\t\t\texpected: 15,\n\t\t},\n\t\t{\n\t\t\tname: \"values around power of 2 boundary\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tfor i := 0; i \u003c 18; i++ {\n\t\t\t\t\tl.Append(i)\n\t\t\t\t}\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    15,\n\t\t\texpected: 15,\n\t\t},\n\t\t{\n\t\t\tname: \"values at power of 2\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tfor i := 0; i \u003c 18; i++ {\n\t\t\t\t\tl.Append(i)\n\t\t\t\t}\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    16,\n\t\t\texpected: 16,\n\t\t},\n\t\t{\n\t\t\tname: \"values after power of 2\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tfor i := 0; i \u003c 18; i++ {\n\t\t\t\t\tl.Append(i)\n\t\t\t\t}\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:    17,\n\t\t\texpected: 17,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tl := tt.setup()\n\t\t\tgot := l.Get(tt.index)\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"List.Get() = %v, want %v\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\n// generateSequence creates a slice of integers from 0 to n-1\nfunc generateSequence(n int) []any {\n\tresult := make([]any, n)\n\tfor i := 0; i \u003c n; i++ {\n\t\tresult[i] = i\n\t}\n\treturn result\n}\n\nfunc TestListDelete(t *testing.T) {\n\ttests := []struct {\n\t\tname          string\n\t\tsetup         func() *List\n\t\tdeleteIndices []int\n\t\texpectedErr   error\n\t\texpectedSize  int\n\t}{\n\t\t{\n\t\t\tname: \"delete single element\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tdeleteIndices: []int{1},\n\t\t\texpectedErr:   nil,\n\t\t\texpectedSize:  2,\n\t\t},\n\t\t{\n\t\t\tname: \"delete multiple elements\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3, 4, 5)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tdeleteIndices: []int{0, 2, 4},\n\t\t\texpectedErr:   nil,\n\t\t\texpectedSize:  2,\n\t\t},\n\t\t{\n\t\t\tname: \"delete with negative index\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tdeleteIndices: []int{-1},\n\t\t\texpectedErr:   ErrOutOfBounds,\n\t\t\texpectedSize:  1,\n\t\t},\n\t\t{\n\t\t\tname: \"delete beyond size\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tdeleteIndices: []int{1},\n\t\t\texpectedErr:   ErrOutOfBounds,\n\t\t\texpectedSize:  1,\n\t\t},\n\t\t{\n\t\t\tname: \"delete already deleted element\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\tl.Delete(0)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tdeleteIndices: []int{0},\n\t\t\texpectedErr:   ErrDeleted,\n\t\t\texpectedSize:  0,\n\t\t},\n\t\t{\n\t\t\tname: \"delete multiple elements in reverse\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3, 4, 5)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tdeleteIndices: []int{4, 2, 0},\n\t\t\texpectedErr:   nil,\n\t\t\texpectedSize:  2,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tl := tt.setup()\n\t\t\tinitialSize := l.Size()\n\t\t\terr := l.Delete(tt.deleteIndices...)\n\t\t\tif err != nil \u0026\u0026 tt.expectedErr != nil {\n\t\t\t\tuassert.ErrorIs(t, err, tt.expectedErr)\n\t\t\t} else {\n\t\t\t\tuassert.Equal(t, tt.expectedErr, err)\n\t\t\t}\n\t\t\tuassert.Equal(t, tt.expectedSize, l.Size(),\n\t\t\t\tufmt.Sprintf(\"Expected size %d after deleting %d elements from size %d, got %d\",\n\t\t\t\t\ttt.expectedSize, len(tt.deleteIndices), initialSize, l.Size()))\n\t\t})\n\t}\n}\n\nfunc TestListSizeAndTotalSize(t *testing.T) {\n\tt.Run(\"empty list\", func(t *testing.T) {\n\t\tlist := New()\n\t\tuassert.Equal(t, 0, list.Size())\n\t\tuassert.Equal(t, 0, list.TotalSize())\n\t})\n\n\tt.Run(\"list with elements\", func(t *testing.T) {\n\t\tlist := New()\n\t\tlist.Append(1)\n\t\tlist.Append(2)\n\t\tlist.Append(3)\n\t\tuassert.Equal(t, 3, list.Size())\n\t\tuassert.Equal(t, 3, list.TotalSize())\n\t})\n\n\tt.Run(\"list with deleted elements\", func(t *testing.T) {\n\t\tlist := New()\n\t\tlist.Append(1)\n\t\tlist.Append(2)\n\t\tlist.Append(3)\n\t\tlist.Delete(1)\n\t\tuassert.Equal(t, 2, list.Size())\n\t\tuassert.Equal(t, 3, list.TotalSize())\n\t})\n}\n\nfunc TestIterator(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tvalues    []any\n\t\tstart     int\n\t\tend       int\n\t\texpected  []Entry\n\t\twantStop  bool\n\t\tstopAfter int // stop after N elements, -1 for no stop\n\t}{\n\t\t{\n\t\t\tname:      \"empty list\",\n\t\t\tvalues:    []any{},\n\t\t\tstart:     0,\n\t\t\tend:       10,\n\t\t\texpected:  []Entry{},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:      \"nil list\",\n\t\t\tvalues:    nil,\n\t\t\tstart:     0,\n\t\t\tend:       0,\n\t\t\texpected:  []Entry{},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"single element forward\",\n\t\t\tvalues: []any{42},\n\t\t\tstart:  0,\n\t\t\tend:    0,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 42},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"multiple elements forward\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\tstart:  0,\n\t\t\tend:    4,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"multiple elements reverse\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\tstart:  4,\n\t\t\tend:    0,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"partial range forward\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\tstart:  1,\n\t\t\tend:    3,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"partial range reverse\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\tstart:  3,\n\t\t\tend:    1,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:      \"stop iteration early\",\n\t\t\tvalues:    []any{1, 2, 3, 4, 5},\n\t\t\tstart:     0,\n\t\t\tend:       4,\n\t\t\twantStop:  true,\n\t\t\tstopAfter: 2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"negative start\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\tstart:  -1,\n\t\t\tend:    2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"negative end\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\tstart:  0,\n\t\t\tend:    -2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:      \"start beyond size\",\n\t\t\tvalues:    []any{1, 2, 3},\n\t\t\tstart:     5,\n\t\t\tend:       6,\n\t\t\texpected:  []Entry{},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"end beyond size\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\tstart:  0,\n\t\t\tend:    5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"with deleted elements\",\n\t\t\tvalues: []any{1, 2, nil, 4, 5},\n\t\t\tstart:  0,\n\t\t\tend:    4,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:   \"with deleted elements reverse\",\n\t\t\tvalues: []any{1, nil, 3, nil, 5},\n\t\t\tstart:  4,\n\t\t\tend:    0,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t},\n\t\t\tstopAfter: -1,\n\t\t},\n\t\t{\n\t\t\tname:      \"start equals end\",\n\t\t\tvalues:    []any{1, 2, 3},\n\t\t\tstart:     1,\n\t\t\tend:       1,\n\t\t\texpected:  []Entry{{Index: 1, Value: 2}},\n\t\t\tstopAfter: -1,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tlist := New()\n\t\t\tlist.Append(tt.values...)\n\n\t\t\tvar result []Entry\n\t\t\tstopped := list.Iterator(tt.start, tt.end, func(index int, value any) bool {\n\t\t\t\tresult = append(result, Entry{Index: index, Value: value})\n\t\t\t\treturn tt.stopAfter \u003e= 0 \u0026\u0026 len(result) \u003e= tt.stopAfter\n\t\t\t})\n\n\t\t\tuassert.Equal(t, len(result), len(tt.expected), \"comparing length\")\n\n\t\t\tfor i := range result {\n\t\t\t\tuassert.Equal(t, result[i].Index, tt.expected[i].Index, \"comparing index\")\n\t\t\t\tuassert.Equal(t, typeutil.ToString(result[i].Value), typeutil.ToString(tt.expected[i].Value), \"comparing value\")\n\t\t\t}\n\n\t\t\tuassert.Equal(t, stopped, tt.wantStop, \"comparing stopped\")\n\t\t})\n\t}\n}\n\nfunc TestLargeListAppendGetAndDelete(t *testing.T) {\n\tl := New()\n\tsize := 100\n\n\t// Append values from 0 to 99\n\tfor i := 0; i \u003c size; i++ {\n\t\tl.Append(i)\n\t\tval := l.Get(i)\n\t\tuassert.Equal(t, i, val)\n\t}\n\n\t// Verify size\n\tuassert.Equal(t, size, l.Size())\n\tuassert.Equal(t, size, l.TotalSize())\n\n\t// Get and verify each value\n\tfor i := 0; i \u003c size; i++ {\n\t\tval := l.Get(i)\n\t\tuassert.Equal(t, i, val)\n\t}\n\n\t// Get and verify each value\n\tfor i := 0; i \u003c size; i++ {\n\t\terr := l.Delete(i)\n\t\tuassert.Equal(t, nil, err)\n\t}\n\n\t// Verify size\n\tuassert.Equal(t, 0, l.Size())\n\tuassert.Equal(t, size, l.TotalSize())\n\n\t// Get and verify each value\n\tfor i := 0; i \u003c size; i++ {\n\t\tval := l.Get(i)\n\t\tuassert.Equal(t, nil, val)\n\t}\n}\n\nfunc TestEdgeCases(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\ttest func(t *testing.T)\n\t}{\n\t\t{\n\t\t\tname: \"nil list operations\",\n\t\t\ttest: func(t *testing.T) {\n\t\t\t\tvar l *List\n\t\t\t\tuassert.Equal(t, 0, l.Size())\n\t\t\t\tuassert.Equal(t, 0, l.TotalSize())\n\t\t\t\tuassert.Equal(t, nil, l.Get(0))\n\t\t\t\terr := l.Delete(0)\n\t\t\t\tuassert.ErrorIs(t, err, ErrOutOfBounds)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"delete empty indices slice\",\n\t\t\ttest: func(t *testing.T) {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\terr := l.Delete()\n\t\t\t\tuassert.Equal(t, nil, err)\n\t\t\t\tuassert.Equal(t, 1, l.Size())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"append nil values\",\n\t\t\ttest: func(t *testing.T) {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(nil, nil)\n\t\t\t\tuassert.Equal(t, 2, l.Size())\n\t\t\t\tuassert.Equal(t, nil, l.Get(0))\n\t\t\t\tuassert.Equal(t, nil, l.Get(1))\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"delete same index multiple times\",\n\t\t\ttest: func(t *testing.T) {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3)\n\t\t\t\terr := l.Delete(1)\n\t\t\t\tuassert.Equal(t, nil, err)\n\t\t\t\terr = l.Delete(1)\n\t\t\t\tuassert.ErrorIs(t, err, ErrDeleted)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"iterator with all deleted elements\",\n\t\t\ttest: func(t *testing.T) {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3)\n\t\t\t\tl.Delete(0, 1, 2)\n\t\t\t\tvar count int\n\t\t\t\tl.Iterator(0, 2, func(index int, value any) bool {\n\t\t\t\t\tcount++\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\tuassert.Equal(t, 0, count)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"append after delete\",\n\t\t\ttest: func(t *testing.T) {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2)\n\t\t\t\tl.Delete(1)\n\t\t\t\tl.Append(3)\n\t\t\t\tuassert.Equal(t, 2, l.Size())\n\t\t\t\tuassert.Equal(t, 3, l.TotalSize())\n\t\t\t\tuassert.Equal(t, 1, l.Get(0))\n\t\t\t\tuassert.Equal(t, nil, l.Get(1))\n\t\t\t\tuassert.Equal(t, 3, l.Get(2))\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ttt.test(t)\n\t\t})\n\t}\n}\n\nfunc TestIteratorByOffset(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tvalues   []any\n\t\toffset   int\n\t\tcount    int\n\t\texpected []Entry\n\t\twantStop bool\n\t}{\n\t\t{\n\t\t\tname:     \"empty list\",\n\t\t\tvalues:   []any{},\n\t\t\toffset:   0,\n\t\t\tcount:    5,\n\t\t\texpected: []Entry{},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"positive count forward iteration\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\toffset: 1,\n\t\t\tcount:  2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"negative count backward iteration\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\toffset: 3,\n\t\t\tcount:  -2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"count exceeds available elements forward\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\toffset: 1,\n\t\t\tcount:  5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"count exceeds available elements backward\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\toffset: 1,\n\t\t\tcount:  -5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"zero count\",\n\t\t\tvalues:   []any{1, 2, 3},\n\t\t\toffset:   0,\n\t\t\tcount:    0,\n\t\t\texpected: []Entry{},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"negative offset\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\toffset: -1,\n\t\t\tcount:  2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"offset beyond size\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\toffset: 5,\n\t\t\tcount:  -2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"with deleted elements\",\n\t\t\tvalues: []any{1, nil, 3, nil, 5},\n\t\t\toffset: 0,\n\t\t\tcount:  3,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"early stop in forward iteration\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\toffset: 0,\n\t\t\tcount:  5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t\twantStop: true, // The callback will return true after 2 elements\n\t\t},\n\t\t{\n\t\t\tname:   \"early stop in backward iteration\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\toffset: 4,\n\t\t\tcount:  -5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t},\n\t\t\twantStop: true, // The callback will return true after 2 elements\n\t\t},\n\t\t{\n\t\t\tname:     \"nil list\",\n\t\t\tvalues:   nil,\n\t\t\toffset:   0,\n\t\t\tcount:    5,\n\t\t\texpected: []Entry{},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"single element forward\",\n\t\t\tvalues: []any{1},\n\t\t\toffset: 0,\n\t\t\tcount:  5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"single element backward\",\n\t\t\tvalues: []any{1},\n\t\t\toffset: 0,\n\t\t\tcount:  -5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t},\n\t\t\twantStop: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"all deleted elements\",\n\t\t\tvalues:   []any{nil, nil, nil},\n\t\t\toffset:   0,\n\t\t\tcount:    3,\n\t\t\texpected: []Entry{},\n\t\t\twantStop: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tlist := New()\n\t\t\tlist.Append(tt.values...)\n\n\t\t\tvar result []Entry\n\t\t\tvar cb IterCbFn\n\t\t\tif tt.wantStop {\n\t\t\t\tcb = func(index int, value any) bool {\n\t\t\t\t\tresult = append(result, Entry{Index: index, Value: value})\n\t\t\t\t\treturn len(result) \u003e= 2 // Stop after 2 elements for early stop tests\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tcb = func(index int, value any) bool {\n\t\t\t\t\tresult = append(result, Entry{Index: index, Value: value})\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tstopped := list.IteratorByOffset(tt.offset, tt.count, cb)\n\n\t\t\tuassert.Equal(t, len(tt.expected), len(result), \"comparing length\")\n\t\t\tfor i := range result {\n\t\t\t\tuassert.Equal(t, tt.expected[i].Index, result[i].Index, \"comparing index\")\n\t\t\t\tuassert.Equal(t, typeutil.ToString(tt.expected[i].Value), typeutil.ToString(result[i].Value), \"comparing value\")\n\t\t\t}\n\t\t\tuassert.Equal(t, tt.wantStop, stopped, \"comparing stopped\")\n\t\t})\n\t}\n}\n\nfunc TestMustDelete(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tsetup       func() *List\n\t\tindices     []int\n\t\tshouldPanic bool\n\t\tpanicMsg    string\n\t}{\n\t\t{\n\t\t\tname: \"successful delete\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindices:     []int{1},\n\t\t\tshouldPanic: false,\n\t\t},\n\t\t{\n\t\t\tname: \"out of bounds\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindices:     []int{1},\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrOutOfBounds.Error(),\n\t\t},\n\t\t{\n\t\t\tname: \"already deleted\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\tl.Delete(0)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindices:     []int{0},\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrDeleted.Error(),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tl := tt.setup()\n\t\t\tif tt.shouldPanic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tr := recover()\n\t\t\t\t\tif r == nil {\n\t\t\t\t\t\tt.Error(\"Expected panic but got none\")\n\t\t\t\t\t}\n\t\t\t\t\terr, ok := r.(error)\n\t\t\t\t\tif !ok {\n\t\t\t\t\t\tt.Errorf(\"Expected error but got %v\", r)\n\t\t\t\t\t}\n\t\t\t\t\tuassert.Equal(t, tt.panicMsg, err.Error())\n\t\t\t\t}()\n\t\t\t}\n\t\t\tl.MustDelete(tt.indices...)\n\t\t\tif tt.shouldPanic {\n\t\t\t\tt.Error(\"Expected panic\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMustGet(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tsetup       func() *List\n\t\tindex       int\n\t\texpected    any\n\t\tshouldPanic bool\n\t\tpanicMsg    string\n\t}{\n\t\t{\n\t\t\tname: \"successful get\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(42)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\texpected:    42,\n\t\t\tshouldPanic: false,\n\t\t},\n\t\t{\n\t\t\tname: \"out of bounds negative\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       -1,\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrOutOfBounds.Error(),\n\t\t},\n\t\t{\n\t\t\tname: \"out of bounds positive\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       1,\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrOutOfBounds.Error(),\n\t\t},\n\t\t{\n\t\t\tname: \"deleted element\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\tl.Delete(0)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrDeleted.Error(),\n\t\t},\n\t\t{\n\t\t\tname: \"nil list\",\n\t\t\tsetup: func() *List {\n\t\t\t\treturn nil\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrOutOfBounds.Error(),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tl := tt.setup()\n\t\t\tif tt.shouldPanic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tr := recover()\n\t\t\t\t\tif r == nil {\n\t\t\t\t\t\tt.Error(\"Expected panic but got none\")\n\t\t\t\t\t}\n\t\t\t\t\terr, ok := r.(error)\n\t\t\t\t\tif !ok {\n\t\t\t\t\t\tt.Errorf(\"Expected error but got %v\", r)\n\t\t\t\t\t}\n\t\t\t\t\tuassert.Equal(t, tt.panicMsg, err.Error())\n\t\t\t\t}()\n\t\t\t}\n\t\t\tresult := l.MustGet(tt.index)\n\t\t\tif tt.shouldPanic {\n\t\t\t\tt.Error(\"Expected panic\")\n\t\t\t}\n\t\t\tuassert.Equal(t, typeutil.ToString(tt.expected), typeutil.ToString(result))\n\t\t})\n\t}\n}\n\nfunc TestGetRange(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tvalues   []any\n\t\tstart    int\n\t\tend      int\n\t\texpected []Entry\n\t}{\n\t\t{\n\t\t\tname:     \"empty list\",\n\t\t\tvalues:   []any{},\n\t\t\tstart:    0,\n\t\t\tend:      10,\n\t\t\texpected: []Entry{},\n\t\t},\n\t\t{\n\t\t\tname:   \"single element\",\n\t\t\tvalues: []any{42},\n\t\t\tstart:  0,\n\t\t\tend:    0,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 42},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"multiple elements forward\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\tstart:  1,\n\t\t\tend:    3,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"multiple elements reverse\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\tstart:  3,\n\t\t\tend:    1,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"with deleted elements\",\n\t\t\tvalues: []any{1, nil, 3, nil, 5},\n\t\t\tstart:  0,\n\t\t\tend:    4,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"nil list\",\n\t\t\tvalues:   nil,\n\t\t\tstart:    0,\n\t\t\tend:      5,\n\t\t\texpected: []Entry{},\n\t\t},\n\t\t{\n\t\t\tname:     \"negative indices\",\n\t\t\tvalues:   []any{1, 2, 3},\n\t\t\tstart:    -1,\n\t\t\tend:      -2,\n\t\t\texpected: []Entry{},\n\t\t},\n\t\t{\n\t\t\tname:   \"indices beyond size\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\tstart:  1,\n\t\t\tend:    5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tlist := New()\n\t\t\tlist.Append(tt.values...)\n\n\t\t\tresult := list.GetRange(tt.start, tt.end)\n\n\t\t\tuassert.Equal(t, len(tt.expected), len(result), \"comparing length\")\n\t\t\tfor i := range result {\n\t\t\t\tuassert.Equal(t, tt.expected[i].Index, result[i].Index, \"comparing index\")\n\t\t\t\tuassert.Equal(t, typeutil.ToString(tt.expected[i].Value), typeutil.ToString(result[i].Value), \"comparing value\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetByOffset(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tvalues   []any\n\t\toffset   int\n\t\tcount    int\n\t\texpected []Entry\n\t}{\n\t\t{\n\t\t\tname:     \"empty list\",\n\t\t\tvalues:   []any{},\n\t\t\toffset:   0,\n\t\t\tcount:    5,\n\t\t\texpected: []Entry{},\n\t\t},\n\t\t{\n\t\t\tname:   \"positive count forward\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\toffset: 1,\n\t\t\tcount:  2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"negative count backward\",\n\t\t\tvalues: []any{1, 2, 3, 4, 5},\n\t\t\toffset: 3,\n\t\t\tcount:  -2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 3, Value: 4},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"count exceeds available elements\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\toffset: 1,\n\t\t\tcount:  5,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"zero count\",\n\t\t\tvalues:   []any{1, 2, 3},\n\t\t\toffset:   0,\n\t\t\tcount:    0,\n\t\t\texpected: []Entry{},\n\t\t},\n\t\t{\n\t\t\tname:   \"with deleted elements\",\n\t\t\tvalues: []any{1, nil, 3, nil, 5},\n\t\t\toffset: 0,\n\t\t\tcount:  3,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 4, Value: 5},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"negative offset\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\toffset: -1,\n\t\t\tcount:  2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 0, Value: 1},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:   \"offset beyond size\",\n\t\t\tvalues: []any{1, 2, 3},\n\t\t\toffset: 5,\n\t\t\tcount:  -2,\n\t\t\texpected: []Entry{\n\t\t\t\t{Index: 2, Value: 3},\n\t\t\t\t{Index: 1, Value: 2},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"nil list\",\n\t\t\tvalues:   nil,\n\t\t\toffset:   0,\n\t\t\tcount:    5,\n\t\t\texpected: []Entry{},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tlist := New()\n\t\t\tlist.Append(tt.values...)\n\n\t\t\tresult := list.GetByOffset(tt.offset, tt.count)\n\n\t\t\tuassert.Equal(t, len(tt.expected), len(result), \"comparing length\")\n\t\t\tfor i := range result {\n\t\t\t\tuassert.Equal(t, tt.expected[i].Index, result[i].Index, \"comparing index\")\n\t\t\t\tuassert.Equal(t, typeutil.ToString(tt.expected[i].Value), typeutil.ToString(result[i].Value), \"comparing value\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMustSet(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tsetup       func() *List\n\t\tindex       int\n\t\tvalue       any\n\t\tshouldPanic bool\n\t\tpanicMsg    string\n\t}{\n\t\t{\n\t\t\tname: \"successful set\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(42)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\tvalue:       99,\n\t\t\tshouldPanic: false,\n\t\t},\n\t\t{\n\t\t\tname: \"restore deleted element\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(42)\n\t\t\t\tl.Delete(0)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\tvalue:       99,\n\t\t\tshouldPanic: false,\n\t\t},\n\t\t{\n\t\t\tname: \"out of bounds negative\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       -1,\n\t\t\tvalue:       99,\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrOutOfBounds.Error(),\n\t\t},\n\t\t{\n\t\t\tname: \"out of bounds positive\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       1,\n\t\t\tvalue:       99,\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrOutOfBounds.Error(),\n\t\t},\n\t\t{\n\t\t\tname: \"nil list\",\n\t\t\tsetup: func() *List {\n\t\t\t\treturn nil\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\tvalue:       99,\n\t\t\tshouldPanic: true,\n\t\t\tpanicMsg:    ErrOutOfBounds.Error(),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tl := tt.setup()\n\t\t\tif tt.shouldPanic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tr := recover()\n\t\t\t\t\tif r == nil {\n\t\t\t\t\t\tt.Error(\"Expected panic but got none\")\n\t\t\t\t\t}\n\t\t\t\t\terr, ok := r.(error)\n\t\t\t\t\tif !ok {\n\t\t\t\t\t\tt.Errorf(\"Expected error but got %v\", r)\n\t\t\t\t\t}\n\t\t\t\t\tuassert.Equal(t, tt.panicMsg, err.Error())\n\t\t\t\t}()\n\t\t\t}\n\t\t\tl.MustSet(tt.index, tt.value)\n\t\t\tif tt.shouldPanic {\n\t\t\t\tt.Error(\"Expected panic\")\n\t\t\t}\n\t\t\t// Verify the value was set correctly for non-panic cases\n\t\t\tif !tt.shouldPanic {\n\t\t\t\tresult := l.Get(tt.index)\n\t\t\t\tuassert.Equal(t, typeutil.ToString(tt.value), typeutil.ToString(result))\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSet(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tsetup       func() *List\n\t\tindex       int\n\t\tvalue       any\n\t\texpectedErr error\n\t\tverify      func(t *testing.T, l *List)\n\t}{\n\t\t{\n\t\t\tname: \"set value in empty list\",\n\t\t\tsetup: func() *List {\n\t\t\t\treturn New()\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\tvalue:       42,\n\t\t\texpectedErr: ErrOutOfBounds,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 0, l.Size())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set value at valid index\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex: 0,\n\t\t\tvalue: 42,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 42, l.Get(0))\n\t\t\t\tuassert.Equal(t, 1, l.Size())\n\t\t\t\tuassert.Equal(t, 1, l.TotalSize())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set value at negative index\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       -1,\n\t\t\tvalue:       42,\n\t\t\texpectedErr: ErrOutOfBounds,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 1, l.Get(0))\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set value beyond size\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex:       1,\n\t\t\tvalue:       42,\n\t\t\texpectedErr: ErrOutOfBounds,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 1, l.Get(0))\n\t\t\t\tuassert.Equal(t, 1, l.Size())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set nil value\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex: 0,\n\t\t\tvalue: nil,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, nil, l.Get(0))\n\t\t\t\tuassert.Equal(t, 0, l.Size())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set value at deleted index\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3)\n\t\t\t\tl.Delete(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex: 1,\n\t\t\tvalue: 42,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 42, l.Get(1))\n\t\t\t\tuassert.Equal(t, 3, l.Size())\n\t\t\t\tuassert.Equal(t, 3, l.TotalSize())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set value in nil list\",\n\t\t\tsetup: func() *List {\n\t\t\t\treturn nil\n\t\t\t},\n\t\t\tindex:       0,\n\t\t\tvalue:       42,\n\t\t\texpectedErr: ErrOutOfBounds,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 0, l.Size())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set multiple values at same index\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex: 0,\n\t\t\tvalue: 42,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 42, l.Get(0))\n\t\t\t\terr := l.Set(0, 99)\n\t\t\t\tuassert.Equal(t, nil, err)\n\t\t\t\tuassert.Equal(t, 99, l.Get(0))\n\t\t\t\tuassert.Equal(t, 1, l.Size())\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"set value at last index\",\n\t\t\tsetup: func() *List {\n\t\t\t\tl := New()\n\t\t\t\tl.Append(1, 2, 3)\n\t\t\t\treturn l\n\t\t\t},\n\t\t\tindex: 2,\n\t\t\tvalue: 42,\n\t\t\tverify: func(t *testing.T, l *List) {\n\t\t\t\tuassert.Equal(t, 42, l.Get(2))\n\t\t\t\tuassert.Equal(t, 3, l.Size())\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tl := tt.setup()\n\t\t\terr := l.Set(tt.index, tt.value)\n\n\t\t\tif tt.expectedErr != nil {\n\t\t\t\tuassert.ErrorIs(t, err, tt.expectedErr)\n\t\t\t} else {\n\t\t\t\tuassert.Equal(t, nil, err)\n\t\t\t}\n\n\t\t\ttt.verify(t, l)\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"4BUahuXuyIb4TgMiJqN6oV4RAhj7HPIPuSElFR79O141jfBUeVOSdPsuFGfPPLrvNql5ojTpeNE5LprbfPf1Bw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"message","path":"gno.land/p/jeronimoalbi/message","files":[{"name":"broker.gno","body":"package message\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/ulist\"\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar (\n\t// ErrInvalidTopic is triggered when an invalid topic is used.\n\tErrInvalidTopic = errors.New(\"invalid topic\")\n\n\t// ErrRequiredCallback is triggered when subscribing without a callback.\n\tErrRequiredCallback = errors.New(\"message callback is required\")\n\n\t// ErrRequiredSubscriptionID is triggered when unsubscribing without an ID.\n\tErrRequiredSubscriptionID = errors.New(\"message sibscription ID is required\")\n\n\t// ErrRequiredTopic is triggered when (un)subscribing without a topic.\n\tErrRequiredTopic = errors.New(\"message topic is required\")\n)\n\n// NewBroker creates a new message broker.\nfunc NewBroker() *Broker {\n\treturn \u0026Broker{}\n}\n\n// Broker is a message broker that handles subscriptions and message publishing.\ntype Broker struct {\n\tcallbacks avl.Tree // string(topic) -\u003e *ulist.List(Callback)\n}\n\n// Topics returns the list of current subscription topics.\nfunc (b Broker) Topics() []Topic {\n\tvar topics []Topic\n\tb.callbacks.Iterate(\"\", \"\", func(k string, _ any) bool {\n\t\ttopic := Topic(k)\n\t\tif topic == TopicAll {\n\t\t\t// Skip catchall topic from the list\n\t\t\treturn false\n\t\t}\n\n\t\ttopics = append(topics, topic)\n\t\treturn false\n\t})\n\treturn topics\n}\n\n// Subscribe subscribes to messages published for a topic.\n// It returns the callback ID within the topic.\nfunc (b *Broker) Subscribe(topic Topic, cb Callback) (id int, _ error) {\n\tkey := strings.TrimSpace(string(topic))\n\tif key == \"\" {\n\t\treturn 0, ErrRequiredTopic\n\t}\n\n\tif cb == nil {\n\t\treturn 0, ErrRequiredCallback\n\t}\n\n\tv := b.callbacks.Get(key)\n\tcallbacks, _ := v.(*ulist.List)\n\tif callbacks == nil {\n\t\tcallbacks = ulist.New()\n\t}\n\n\tcallbacks.Append(cb)\n\tb.callbacks.Set(key, callbacks)\n\treturn callbacks.TotalSize(), nil\n}\n\n// Unsubscribe unsubscribes a callback from a message topic.\n// ID is the callback ID within the topic, returned on subscription.\nfunc (b *Broker) Unsubscribe(topic Topic, id int) (unsubscribed bool, _ error) {\n\tkey := strings.TrimSpace(string(topic))\n\tif key == \"\" {\n\t\treturn false, ErrRequiredTopic\n\t}\n\n\tif id == 0 {\n\t\treturn false, ErrRequiredSubscriptionID\n\t}\n\n\tv := b.callbacks.Get(key)\n\tif v == nil {\n\t\treturn false, errors.New(\"message topic not found: \" + key)\n\t}\n\n\tcallbacks := v.(*ulist.List)\n\ti := id - 1\n\treturn callbacks.Delete(i) == nil, nil\n}\n\n// Publish publishes a message for a topic.\nfunc (b Broker) Publish(topic Topic, data any) error {\n\tif topic == TopicAll {\n\t\treturn ErrInvalidTopic\n\t}\n\n\tkey := strings.TrimSpace(string(topic))\n\tif key == \"\" {\n\t\treturn ErrRequiredTopic\n\t}\n\n\titerCb := func(_ int, v any) bool {\n\t\tcb := v.(Callback)\n\t\tcb(Message{topic, data})\n\t\treturn false\n\t}\n\n\t// Trigger callbacks subscribed to current topic\n\tv := b.callbacks.Get(key)\n\tif v != nil {\n\t\tcallbacks := v.(*ulist.List)\n\t\tcallbacks.Iterator(0, callbacks.Size(), iterCb)\n\t}\n\n\t// Trigger callbacks subscribed to all topics\n\tv = b.callbacks.Get(string(TopicAll))\n\tif v != nil {\n\t\tcallbacks := v.(*ulist.List)\n\t\tcallbacks.Iterator(0, callbacks.Size(), iterCb)\n\t}\n\treturn nil\n}\n"},{"name":"broker_filetest.gno","body":"package main\n\nimport (\n\t\"gno.land/p/jeronimoalbi/message\"\n)\n\nfunc main() {\n\t// Create a message broker and a generic message callback\n\tbroker := message.NewBroker()\n\tcb := func(m message.Message) {\n\t\tprintln(\"topic triggered: \" + string(m.Topic))\n\t\tprintln(\"topic data: \" + m.Data.(string))\n\t}\n\n\t// Subscribe to a couple of events\n\t_, err := broker.Subscribe(\"eventA\", cb)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t_, err = broker.Subscribe(\"eventB\", cb)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Subscribe to an event and then unsubscribe from it\n\tid, err := broker.Subscribe(\"eventC\", cb)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t_, err = broker.Unsubscribe(\"eventC\", id)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Subscribe to all events\n\t_, err = broker.Subscribe(message.TopicAll, func(m message.Message) {\n\t\tprintln(\"catchall topic triggered: \" + string(m.Topic))\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// List broker topics\n\tprintln(\"topics:\")\n\tfor _, topic := range broker.Topics() {\n\t\tprintln(\"- \" + string(topic))\n\t}\n\n\t// Publish events\n\tprintln()\n\tif err = broker.Publish(\"eventA\", \"A\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tprintln()\n\tif err = broker.Publish(\"eventB\", \"B\"); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Output:\n// topics:\n// - eventA\n// - eventB\n// - eventC\n//\n// topic triggered: eventA\n// topic data: A\n// catchall topic triggered: eventA\n//\n// topic triggered: eventB\n// topic data: B\n// catchall topic triggered: eventB\n"},{"name":"broker_test.gno","body":"package message_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/expect\"\n\t\"gno.land/p/jeronimoalbi/message\"\n)\n\nvar (\n\t_ message.Subscriber = (*message.Broker)(nil)\n\t_ message.Publisher  = (*message.Broker)(nil)\n)\n\nfunc TestBrokerTopics(t *testing.T) {\n\tbroker := message.NewBroker()\n\texpect.\n\t\tValue(t, len(broker.Topics())).\n\t\tAsInt().\n\t\tToEqual(0)\n\n\tcb := func(message.Message) {}\n\tbroker.Subscribe(\"foo\", cb)\n\tbroker.Subscribe(\"bar\", cb)\n\tbroker.Subscribe(\"baz\", cb)\n\tbroker.Subscribe(message.TopicAll, cb)\n\ttopics := broker.Topics()\n\n\texpect.\n\t\tValue(t, len(topics)).\n\t\tAsInt().\n\t\tToEqual(3)\n\texpect.\n\t\tValue(t, string(topics[0])).\n\t\tAsString().\n\t\tToEqual(\"bar\")\n\texpect.\n\t\tValue(t, string(topics[1])).\n\t\tAsString().\n\t\tToEqual(\"baz\")\n\texpect.\n\t\tValue(t, string(topics[2])).\n\t\tAsString().\n\t\tToEqual(\"foo\")\n}\n\nfunc TestBrokerPublish(t *testing.T) {\n\ttests := []struct {\n\t\tname               string\n\t\tsubscribe, publish message.Topic\n\t\tdata               any\n\t\tmessage            *message.Message\n\t\terr                error\n\t}{\n\t\t{\n\t\t\tname:      \"publishes subscribed topic\",\n\t\t\tsubscribe: \"foo\",\n\t\t\tpublish:   \"foo\",\n\t\t\tdata:      \"foo's data\",\n\t\t\tmessage: \u0026message.Message{\n\t\t\t\tTopic: \"foo\",\n\t\t\t\tData:  \"foo's data\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:      \"publishes all topics\",\n\t\t\tsubscribe: message.TopicAll,\n\t\t\tpublish:   \"foo\",\n\t\t\tdata:      \"foo's data\",\n\t\t\tmessage: \u0026message.Message{\n\t\t\t\tTopic: \"foo\",\n\t\t\t\tData:  \"foo's data\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:      \"invalid topic\",\n\t\t\tsubscribe: \"foo\",\n\t\t\tpublish:   message.TopicAll,\n\t\t\terr:       message.ErrInvalidTopic,\n\t\t},\n\t\t{\n\t\t\tname:      \"no topic\",\n\t\t\tsubscribe: \"foo\",\n\t\t\tpublish:   \"\",\n\t\t\terr:       message.ErrRequiredTopic,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar msg *message.Message\n\t\t\tbroker := message.NewBroker()\n\t\t\tbroker.Subscribe(tt.subscribe, func(m message.Message) { msg = \u0026m })\n\n\t\t\t// Act\n\t\t\terr := broker.Publish(tt.publish, tt.data)\n\n\t\t\t// Assert\n\t\t\tif tt.err != nil {\n\t\t\t\texpect.\n\t\t\t\t\tFunc(t, func() error { return err }).\n\t\t\t\t\tWithFailPrefix(\"expect a publish error\").\n\t\t\t\t\tToFail().\n\t\t\t\t\tWithError(tt.err)\n\t\t\t\texpect.\n\t\t\t\t\tValue(t, istypednil(msg)).\n\t\t\t\t\tWithFailPrefix(\"expect callback not to be called\").\n\t\t\t\t\tAsBoolean().\n\t\t\t\t\tToBeTruthy()\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\texpect.\n\t\t\t\tValue(t, err).\n\t\t\t\tWithFailPrefix(\"expect no publish error\").\n\t\t\t\tToBeNil()\n\t\t\texpect.\n\t\t\t\tValue(t, msg).\n\t\t\t\tWithFailPrefix(\"expect callback to be called\").\n\t\t\t\tNot().ToBeNil()\n\t\t\texpect.\n\t\t\t\tValue(t, string(msg.Topic)).\n\t\t\t\tWithFailPrefix(\"expect message topic to match\").\n\t\t\t\tAsString().\n\t\t\t\tToEqual(string(tt.message.Topic))\n\t\t\texpect.\n\t\t\t\tValue(t, msg.Data).\n\t\t\t\tWithFailPrefix(\"expect message data to match\").\n\t\t\t\tAsString().\n\t\t\t\tToEqual(tt.message.Data.(string))\n\t\t})\n\t}\n}\n\nfunc TestBrokerSubscribe(t *testing.T) {\n\tcb := func(message.Message) {}\n\ttests := []struct {\n\t\tname     string\n\t\tsetup    func(*message.Broker)\n\t\ttopic    message.Topic\n\t\tid       int64\n\t\tcallback message.Callback\n\t\terr      error\n\t}{\n\t\t{\n\t\t\tname:     \"single subscription\",\n\t\t\ttopic:    \"foo\",\n\t\t\tid:       1,\n\t\t\tcallback: cb,\n\t\t},\n\t\t{\n\t\t\tname: \"existing subscriptions\",\n\t\t\tsetup: func(b *message.Broker) {\n\t\t\t\tb.Subscribe(\"foo\", cb)\n\t\t\t},\n\t\t\ttopic:    \"foo\",\n\t\t\tid:       2,\n\t\t\tcallback: cb,\n\t\t},\n\t\t{\n\t\t\tname: \"other subscription topics\",\n\t\t\tsetup: func(b *message.Broker) {\n\t\t\t\tb.Subscribe(\"foo\", cb)\n\t\t\t},\n\t\t\ttopic:    \"bar\",\n\t\t\tid:       1,\n\t\t\tcallback: cb,\n\t\t},\n\t\t{\n\t\t\tname:  \"no topic\",\n\t\t\ttopic: \"\",\n\t\t\terr:   message.ErrRequiredTopic,\n\t\t},\n\t\t{\n\t\t\tname:     \"no callback\",\n\t\t\ttopic:    \"foo\",\n\t\t\tcallback: nil,\n\t\t\terr:      message.ErrRequiredCallback,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tbroker := message.NewBroker()\n\t\t\tif tt.setup != nil {\n\t\t\t\ttt.setup(broker)\n\t\t\t}\n\n\t\t\t// Act\n\t\t\tid, err := broker.Subscribe(tt.topic, tt.callback)\n\n\t\t\t// Assert\n\t\t\tif tt.err != nil {\n\t\t\t\texpect.\n\t\t\t\t\tFunc(t, func() error { return err }).\n\t\t\t\t\tWithFailPrefix(\"expect a subscribe error\").\n\t\t\t\t\tToFail().\n\t\t\t\t\tWithError(tt.err)\n\t\t\t\texpect.\n\t\t\t\t\tValue(t, id).\n\t\t\t\t\tWithFailPrefix(\"expect zero ID\").\n\t\t\t\t\tAsInt().\n\t\t\t\t\tToEqual(0)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\texpect.\n\t\t\t\tValue(t, err).\n\t\t\t\tWithFailPrefix(\"expect no subscribe error\").\n\t\t\t\tToBeNil()\n\t\t\texpect.\n\t\t\t\tValue(t, id).\n\t\t\t\tWithFailPrefix(\"expect ID to match\").\n\t\t\t\tAsInt().\n\t\t\t\tToEqual(tt.id)\n\t\t})\n\t}\n}\n\nfunc TestBrokerUnsubscribe(t *testing.T) {\n\tcb := func(message.Message) {}\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func(*message.Broker)\n\t\ttopic  message.Topic\n\t\tid     int\n\t\terrMsg string\n\t}{\n\t\t{\n\t\t\tname: \"single subscription\",\n\t\t\tsetup: func(b *message.Broker) {\n\t\t\t\tb.Subscribe(\"foo\", cb)\n\t\t\t},\n\t\t\ttopic: \"foo\",\n\t\t\tid:    1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple subscriptions\",\n\t\t\tsetup: func(b *message.Broker) {\n\t\t\t\tb.Subscribe(\"foo\", cb)\n\t\t\t\tb.Subscribe(\"foo\", cb)\n\t\t\t},\n\t\t\ttopic: \"foo\",\n\t\t\tid:    2,\n\t\t},\n\t\t{\n\t\t\tname: \"other subscription topics\",\n\t\t\tsetup: func(b *message.Broker) {\n\t\t\t\tb.Subscribe(\"foo\", cb)\n\t\t\t\tb.Subscribe(\"bar\", cb)\n\t\t\t},\n\t\t\ttopic: \"foo\",\n\t\t\tid:    1,\n\t\t},\n\t\t{\n\t\t\tname:   \"not found\",\n\t\t\ttopic:  \"foo\",\n\t\t\tid:     1,\n\t\t\terrMsg: \"message topic not found: foo\",\n\t\t},\n\t\t{\n\t\t\tname:   \"no topic\",\n\t\t\ttopic:  \"\",\n\t\t\terrMsg: message.ErrRequiredTopic.Error(),\n\t\t},\n\t\t{\n\t\t\tname:   \"no subscription ID\",\n\t\t\ttopic:  \"foo\",\n\t\t\tid:     0,\n\t\t\terrMsg: message.ErrRequiredSubscriptionID.Error(),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tbroker := message.NewBroker()\n\t\t\tif tt.setup != nil {\n\t\t\t\ttt.setup(broker)\n\t\t\t}\n\n\t\t\t// Act\n\t\t\tunsubscribed, err := broker.Unsubscribe(tt.topic, tt.id)\n\n\t\t\t// Assert\n\t\t\tif tt.errMsg != \"\" {\n\t\t\t\texpect.\n\t\t\t\t\tFunc(t, func() error { return err }).\n\t\t\t\t\tWithFailPrefix(\"expect a subscribe error\").\n\t\t\t\t\tToFail().\n\t\t\t\t\tWithMessage(tt.errMsg)\n\t\t\t\texpect.\n\t\t\t\t\tValue(t, unsubscribed).\n\t\t\t\t\tWithFailPrefix(\"expect unsubscribe to fail\").\n\t\t\t\t\tAsBoolean().\n\t\t\t\t\tToBeFalsy()\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\texpect.\n\t\t\t\tValue(t, err).\n\t\t\t\tWithFailPrefix(\"expect no unsubscribe error\").\n\t\t\t\tToBeNil()\n\t\t\texpect.\n\t\t\t\tValue(t, unsubscribed).\n\t\t\t\tWithFailPrefix(\"expect unsubscribe to succeed\").\n\t\t\t\tAsBoolean().\n\t\t\t\tToBeTruthy()\n\t\t})\n\t}\n}\n"},{"name":"doc.gno","body":"// Package message provides a simple message broker implementation.\n//\n// The message broker is a Pub/Sub one. It implements two different interfaces,\n// `Publisher` and `Subscriber`, which are also defined within this package.\n//\n// Published messages contain the topic where they are published and optional\n// message data.\n//\n// Subscribe to an event:\n//\n//\tbroker := message.NewBroker()\n//\tsubID, err := broker.Subscribe(\"EventName\", func(msg message.Message) {\n//\t   println(\"EventName has been triggered\")\n//\t   println(msg.Data)\n//\t})\n//\tif err != nil {\n//\t   panic(err)\n//\t}\n//\n// Unsubscribe from an event:\n//\n//\tunsubscribed, err := broker.Unsubscribe(\"EventName\", subID)\n//\tif err != nil {\n//\t   panic(err)\n//\t}\n//\n//\tif !unsubscribed {\n//\t   panic(\"subscription not found\")\n//\t}\n//\n// Publish an event:\n//\n//\terr := broker.Publish(\"EventName\", \"Example event data\")\n//\tif err != nil {\n//\t   panic(err)\n//\t}\npackage message\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/message\"\ngno = \"0.9\"\n"},{"name":"message.gno","body":"package message\n\n// TopicAll defines a topic for all types of message.\n// This topic can be used to subscribe to message for all topics.\nconst TopicAll Topic = \"*\"\n\ntype (\n\t// Topic defines a type for message topics.\n\tTopic string\n\n\t// Callback defines a type for message callbacks.\n\tCallback func(Message)\n\n\t// Message defines a type for published messages.\n\tMessage struct {\n\t\t// Topic is the message topic.\n\t\tTopic Topic\n\n\t\t// Data contains optional message data.\n\t\tData any\n\t}\n\n\t// Publisher defines an interface for message publishers.\n\tPublisher interface {\n\t\t// Publish publishes a message for a topic.\n\t\tPublish(_ Topic, data any) error\n\t}\n\n\t// Subscriber defines an interface for message subscribers.\n\tSubscriber interface {\n\t\t// Subscribe subscribes to messages published for a topic.\n\t\t// It returns the callback ID within the topic.\n\t\tSubscribe(Topic, Callback) (id int, _ error)\n\n\t\t// Unsubscribe unsubscribes a callback from a message topic.\n\t\t// ID is the callback ID within the topic, returned on subscription.\n\t\tUnsubscribe(_ Topic, id int) (unsubscribed bool, _ error)\n\t}\n)\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"RQJlmzSui2IBbsu8t/jK5kW2wGisn2m+AkUPqWeBiyeEFrZpD0c7FjtbwD9meZmMbaIEvGsMM0jlwRoZsz1pBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"storage","path":"gno.land/p/nt/commondao/v0/exts/storage","files":[{"name":"README.md","body":"# CommonDAO Package Storage Extension\n\nStorage package is an extension of `gno.land/p/nt/commondao/v0` that provides\nalternative storage implementations.\n\n## Member Storage\n\nThis custom implementation of `MemberStorage` is an implementation with\ngrouping support that automatically adds or removes members from the storage\nwhen members are added or removed from any of the member groups.\n\nThe implementation provided by the `commondao` package doesn't automatically\nadd members to the storage when any of the groups change, if need then users\nhave to be added explicitly.\n\nAdding or removing users automatically could be beneficial in some cases where\nimplementation requires iterating all unique users within the storage and\nwithin each group, or counting all unique users within them. It also makes it\ncheaper to iterate because having all users within the same storage doesn't\nrequire to iterate each group.\n\nPackage also provide a `GetMemberGroups()` function that takes advantage of\nthis storage which can be used to return the names of the groups that an\naccount is a member of.\n\nExample usage:\n\n```go\nimport (\n  \"gno.land/p/nt/commondao/v0\"\n  \"gno.land/p/nt/commondao/v0/exts/storage\"\n)\n\nfunc main() {\n  // Create a new member storage with grouping\n  s := storage.NewMemberStorage()\n  \n  // Create a member group for moderators\n  moderators, err := s.Grouping().Add(\"moderators\")\n  if err != nil {\n    panic(err)\n  }\n\n  // Add members to the moderators group\n  moderators.Members().Add(\"g1...a\")\n  moderators.Members().Add(\"g1...b\")\n\n  // Create a DAO that uses the new member storage\n  dao := commondao.New(commondao.WithMemberStorage(s))\n\n  // Output: 2\n  println(dao.Members().Size())\n}\n```\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/commondao/v0/exts/storage\"\ngno = \"0.9\"\n"},{"name":"member_storage.gno","body":"package storage\n\nimport (\n\t\"gno.land/p/jeronimoalbi/message\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nconst (\n\tmsgMemberAdd    message.Topic = \"MemberAdd\"\n\tmsgMemberRemove               = \"MemberRemove\"\n)\n\n// GetMemberGroups returns the groups that a member belongs to.\n// It returns no groups if member storage was not created using this package.\nfunc GetMemberGroups(s commondao.MemberStorage, member address) []string {\n\tstorage, ok := s.(*memberStorage)\n\tif !ok {\n\t\treturn nil\n\t}\n\n\tv := storage.memberGroups.Get(member.String())\n\tif v == nil {\n\t\treturn nil\n\t}\n\n\ttree := v.(*bptree.BPTree)\n\tgroups := make([]string, 0, tree.Size())\n\ttree.Iterate(\"\", \"\", func(group string, _ any) bool {\n\t\tgroups = append(groups, group)\n\t\treturn false\n\t})\n\treturn groups\n}\n\n// NewMemberStorage creates a new CommonDAO member storage with grouping support.\n//\n// This is a custom storage that automatically adds or removes members that are added\n// or removed from any of the member groups. This allows for quick and inexpensive\n// checks for the number of total unique storage users, including users added to groups,\n// and also to iterate all of them without needing to iterate individual groups.\nfunc NewMemberStorage() commondao.MemberStorage {\n\t// Create a new broker to allow storages to publish and subscribe to messages\n\t// It is used to add/remove users from the storage each time one or more groups change.\n\tbroker := message.NewBroker()\n\n\t// Define a factory for creating custom member storages when groups are created.\n\t// Custom storage publishes when a member is added or removed from a group.\n\tinnerFactory := func(group string) commondao.MemberStorage {\n\t\treturn \u0026groupMemberStorage{\n\t\t\tMemberStorage: commondao.NewMemberStorage(),\n\t\t\tmessages:      broker,\n\t\t\tgroup:         group,\n\t\t}\n\t}\n\n\t// Create a member storage that automatically adds or removes members each time\n\t// a member group changes. This allows the storage to keep all members within\n\t// the same \"root\" storage for easier iteration.\n\tstorage := \u0026memberStorage{\n\t\tMemberStorage: commondao.NewMemberStorageWithGrouping(\n\t\t\tcommondao.UseStorageFactory(innerFactory),\n\t\t),\n\t\tmessages: broker,\n\t}\n\n\t// Subscribe to messages published by member groups\n\tstorage.messages.Subscribe(msgMemberAdd, storage.handleMemberAddMsg)\n\tstorage.messages.Subscribe(msgMemberRemove, storage.handleMemberRemoveMsg)\n\treturn storage\n}\n\ntype memberStorage struct {\n\tcommondao.MemberStorage\n\n\tmemberGroups bptree.BPTree // string(address) -\u003e *bptree.BPTree(group -\u003e struct{})\n\tmessages     message.Subscriber\n}\n\nfunc (s *memberStorage) handleMemberAddMsg(msg message.Message) {\n\tdata := msg.Data.(groupMemberUpdateData)\n\tkey := data.Member.String()\n\tv := s.memberGroups.Get(key)\n\tgroups, ok := v.(*bptree.BPTree)\n\tif !ok {\n\t\t// Create a new tree to track member's groups\n\t\tgroups = bptree.NewBPTree32()\n\t\ts.memberGroups.Set(key, groups)\n\n\t\t// Add the new member to the storage\n\t\ts.MemberStorage.Add(data.Member)\n\t}\n\n\t// Keep track of the new member group\n\tgroups.Set(data.Group, struct{}{})\n}\n\nfunc (s *memberStorage) handleMemberRemoveMsg(msg message.Message) {\n\tdata := msg.Data.(groupMemberUpdateData)\n\tkey := data.Member.String()\n\tv := s.memberGroups.Get(key)\n\tif v == nil {\n\t\t// Member should always be found\n\t\treturn\n\t}\n\n\t// Remove the group from the list of groups member belongs\n\tgroups := v.(*bptree.BPTree)\n\tgroups.Remove(data.Group)\n\n\t// Remove the member from the storage when it doesn't belong to any group\n\tif groups.Size() == 0 {\n\t\ts.memberGroups.Remove(key)\n\t\ts.MemberStorage.Remove(data.Member)\n\t}\n}\n\n// Size returns the number of members in the storage.\n// It also includes unique members that belong to any number of member groups.\nfunc (s memberStorage) Size() int {\n\treturn s.MemberStorage.Size()\n}\n\n// IterateByOffset iterates members starting at the given offset.\n// The callback can return true to stop iteration.\n// It also iterates unique members that belong to any of the member groups.\nfunc (s memberStorage) IterateByOffset(offset, count int, fn commondao.MemberIterFn) bool {\n\treturn s.MemberStorage.IterateByOffset(offset, count, fn)\n}\n\n// groupMemberUpdateData defines a data type for group member updates.\ntype groupMemberUpdateData struct {\n\tGroup  string\n\tMember address\n}\n\n// groupMemberStorage defines a member storage for member groups.\n// This type of storage publishes messages when a member is added or removed from a group.\ntype groupMemberStorage struct {\n\tcommondao.MemberStorage\n\n\tgroup    string\n\tmessages message.Publisher\n}\n\n// Add adds a member to the storage.\n// Returns true if the member is added, or false if it already existed.\nfunc (s *groupMemberStorage) Add(member address) bool {\n\ts.messages.Publish(msgMemberAdd, groupMemberUpdateData{\n\t\tGroup:  s.group,\n\t\tMember: member,\n\t})\n\treturn s.MemberStorage.Add(member)\n}\n\n// Remove removes a member from the storage.\n// Returns true if member was removed, or false if it was not found.\nfunc (s *groupMemberStorage) Remove(member address) bool {\n\ts.messages.Publish(msgMemberRemove, groupMemberUpdateData{\n\t\tGroup:  s.group,\n\t\tMember: member,\n\t})\n\treturn s.MemberStorage.Remove(member)\n}\n"},{"name":"member_storage_test.gno","body":"package storage_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/p/nt/commondao/v0/exts/storage\"\n)\n\nfunc TestMemberStorageGroupingMemberAdd(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func() commondao.MemberStorage\n\t\tgroup  string\n\t\tmember address\n\t}{\n\t\t{\n\t\t\tname: \"empty group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\ts.Grouping().Add(\"foo\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroup:  \"foo\",\n\t\t\tmember: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t\t{\n\t\t\tname: \"group with members\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\tgroup.Members().Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroup:  \"foo\",\n\t\t\tmember: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t\t{\n\t\t\tname: \"multiple groups with members\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\tgroup, _ = s.Grouping().Add(\"bar\")\n\t\t\t\tgroup.Members().Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroup:  \"bar\",\n\t\t\tmember: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\ts := tt.setup()\n\t\t\tgroup, _ := s.Grouping().Get(tt.group)\n\n\t\t\t// Act\n\t\t\tgroup.Members().Add(tt.member)\n\n\t\t\t// Assert\n\t\t\turequire.True(t, s.Has(tt.member), \"expect member to also be added to parent storage\")\n\t\t})\n\t}\n}\n\nfunc TestMemberStorageGroupingMemberRemove(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func() commondao.MemberStorage\n\t\tgroup  string\n\t\tmember address\n\t}{\n\t\t{\n\t\t\tname: \"one group one member\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroup:  \"foo\",\n\t\t\tmember: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t\t{\n\t\t\tname: \"one group multiple members\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tgroup.Members().Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroup:  \"foo\",\n\t\t\tmember: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t\t{\n\t\t\tname: \"multiple groups multiple members\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tgroup, _ = s.Grouping().Add(\"bar\")\n\t\t\t\tgroup.Members().Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tgroup:  \"foo\",\n\t\t\tmember: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\ts := tt.setup()\n\t\t\tgroup, _ := s.Grouping().Get(tt.group)\n\n\t\t\t// Act\n\t\t\tgroup.Members().Remove(tt.member)\n\n\t\t\t// Assert\n\t\t\turequire.False(t, s.Has(tt.member), \"expect member to also be removed from parent storage\")\n\t\t})\n\t}\n}\n\nfunc TestMemberStorageSize(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func() commondao.MemberStorage\n\t\tsize  int\n\t}{\n\t\t{\n\t\t\tname: \"empty\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\treturn storage.NewMemberStorage()\n\t\t\t},\n\t\t\tsize: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"member without group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\ts.Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple members without group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\ts.Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\ts.Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 2,\n\t\t},\n\t\t{\n\t\t\tname: \"member in group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple members in group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 2,\n\t\t},\n\t\t{\n\t\t\tname: \"multiple members in different groups\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tgroup, _ = s.Grouping().Add(\"bar\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tsize: 2,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\ts := tt.setup()\n\n\t\t\t// Act\n\t\t\tsize := s.Size()\n\n\t\t\t// Assert\n\t\t\turequire.Equal(t, tt.size, size)\n\t\t})\n\t}\n}\n\nfunc TestMemberStorageIterateByOffset(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func() commondao.MemberStorage\n\t\tmembers []address\n\t}{\n\t\t{\n\t\t\tname: \"empty\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\treturn storage.NewMemberStorage()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"without group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\ts.Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\ts.Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tmembers: []address{\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"single group\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tmembers: []address{\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple groups\",\n\t\t\tsetup: func() commondao.MemberStorage {\n\t\t\t\ts := storage.NewMemberStorage()\n\t\t\t\tgroup, _ := s.Grouping().Add(\"foo\")\n\t\t\t\tgroup.Members().Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tgroup, _ = s.Grouping().Add(\"bar\")\n\t\t\t\tgroup.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\t\t\t\treturn s\n\t\t\t},\n\t\t\tmembers: []address{\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar i int\n\t\t\ts := tt.setup()\n\t\t\tmembers := make([]address, s.Size())\n\n\t\t\t// Act\n\t\t\ts.IterateByOffset(0, s.Size(), func(addr address) bool {\n\t\t\t\tmembers[i] = addr\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\t// Assert\n\t\t\turequire.Equal(t, len(tt.members), len(members), \"expect iterated members to match\")\n\n\t\t\tfor i, member := range members {\n\t\t\t\turequire.Equal(t, tt.members[i], member, \"expect member to match\")\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ogyCyG09owI0E80Lam+351M98KqoivyEDzawjDwy4lJKdvezKPRqx09n9EzB8Sp2tLG3vw9Sl9xpJu/s9RT/Aw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"permissions","path":"gno.land/p/gnoland/boards/exts/permissions","files":[{"name":"README.md","body":"# Boards Permissions Extension\n\nThis is a `gno.land/p/gnoland/boards` package extension that provides a custom\n`Permissions` implementation that uses an underlying DAO to manage users and\nroles.\n\nIt also supports optionally setting validation functions to be triggered by the\n`WithPermission()` method before a callback is called. Validators allows adding\ncustom checks and requirements before the callback is called.\n\nUsage Example:\n\n[embedmd]:# (example_test.gno go)\n```go\npackage permissions\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// Example user account\nconst user address = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\n// Define a role\nconst RoleExample boards.Role = \"example\"\n\n// Define a permission\nconst PermissionFoo boards.Permission = 42\n\nfunc ExamplePermission() {\n\t// Define a custom foo permission validation function\n\tvalidateFoo := func(_ boards.Permissions, args boards.Args) error {\n\t\t// Check that the first argument is the string \"bob\"\n\t\tif name, ok := args[0].(string); !ok || name != \"bob\" {\n\t\t\treturn errors.New(\"unauthorized\")\n\t\t}\n\t\treturn nil\n\t}\n\n\t// Create a permissions instance and assign the custom validator to it\n\tperms := New()\n\tperms.ValidateFunc(PermissionFoo, validateFoo)\n\n\t// Add foo permission to example role\n\tperms.AddRole(RoleExample, PermissionFoo)\n\n\t// Add a guest user\n\tperms.SetUserRoles(user, RoleExample)\n\n\t// Call a permissioned callback\n\targs := boards.Args{\"bob\"}\n\tperms.WithPermission(user, PermissionFoo, args, func() {\n\t\tprintln(\"Hello Bob!\")\n\t})\n\n\t// Output:\n\t// Hello Bob!\n}\n```\n"},{"name":"example_test.gno","body":"package permissions\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// Example user account\nconst user address = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\n// Define a role\nconst RoleExample boards.Role = \"example\"\n\n// Define a permission\nconst PermissionFoo boards.Permission = 42\n\nfunc ExamplePermission() {\n\t// Define a custom foo permission validation function\n\tvalidateFoo := func(_ boards.Permissions, args boards.Args) error {\n\t\t// Check that the first argument is the string \"bob\"\n\t\tif name, ok := args[0].(string); !ok || name != \"bob\" {\n\t\t\treturn errors.New(\"unauthorized\")\n\t\t}\n\t\treturn nil\n\t}\n\n\t// Create a permissions instance and assign the custom validator to it\n\tperms := New()\n\tperms.ValidateFunc(PermissionFoo, validateFoo)\n\n\t// Add foo permission to example role\n\tperms.AddRole(RoleExample, PermissionFoo)\n\n\t// Add a guest user\n\tperms.SetUserRoles(user, RoleExample)\n\n\t// Call a permissioned callback\n\targs := boards.Args{\"bob\"}\n\tperms.WithPermission(user, PermissionFoo, args, func() {\n\t\tprintln(\"Hello Bob!\")\n\t})\n\n\t// Output:\n\t// Hello Bob!\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/gnoland/boards/exts/permissions\"\ngno = \"0.9\"\n"},{"name":"options.gno","body":"package permissions\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// Option configures permissions.\ntype Option func(*Permissions)\n\n// UseSingleUserRole configures permissions to only allow one role per user.\nfunc UseSingleUserRole() Option {\n\treturn func(p *Permissions) {\n\t\tp.singleUserRole = true\n\t}\n}\n\n// WithSuperRole configures permissions to have a super role.\n// A super role is the one that have all permissions.\n// This type of role doesn't need to be mapped to any permission.\nfunc WithSuperRole(r boards.Role) Option {\n\treturn func(p *Permissions) {\n\t\tif p.superRole != \"\" {\n\t\t\tpanic(\"permissions super role can be assigned only once\")\n\t\t}\n\n\t\tname := string(r)\n\t\tp.dao.Members().Grouping().Add(name)\n\t\tp.superRole = r\n\t}\n}\n"},{"name":"permissions.gno","body":"package permissions\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/commondao/v0/exts/storage\"\n)\n\n// ValidatorFunc defines a function type for permissions validators.\ntype ValidatorFunc func(boards.Permissions, boards.Args) error\n\n// Permissions manages users, roles and permissions.\n//\n// This type is a default `gno.land/p/gnoland/boards` package `Permissions` implementation\n// that handles boards users, roles and permissions using an underlying DAO. It also supports\n// optionally setting validation functions to be triggered within `WithPermission()` method\n// before a permissioned callback is called.\n//\n// No permissions validation is done by default.\n//\n// Users are allowed to have multiple roles at the same time by default, but permissions can\n// be configured to only allow one role per user.\ntype Permissions struct {\n\tsuperRole      boards.Role\n\tdao            *commondao.CommonDAO\n\tpublic         boards.PermissionSet\n\tvalidators     *bptree.BPTree // string(boards.Permission) -\u003e ValidatorFunc\n\tsingleUserRole bool\n}\n\n// New creates a new permissions type.\nfunc New(options ...Option) *Permissions {\n\ts := storage.NewMemberStorage()\n\tps := \u0026Permissions{\n\t\tvalidators: bptree.NewBPTree32(),\n\t\tdao:        commondao.New(commondao.WithMemberStorage(s)),\n\t}\n\n\tfor _, apply := range options {\n\t\tapply(ps)\n\t}\n\treturn ps\n}\n\n// DAO returns the underlying permissions DAO.\nfunc (ps Permissions) DAO() *commondao.CommonDAO {\n\treturn ps.dao\n}\n\n// ValidateFunc adds a custom permission validator function.\n// If an existing permission function exists it's ovewritten by the new one.\nfunc (ps *Permissions) ValidateFunc(p boards.Permission, fn ValidatorFunc) {\n\tps.validators.Set(p.String(), fn)\n}\n\n// SetPublicPermissions assigns permissions that are available to anyone.\n// It removes previous public permissions and assigns the new ones.\n// By default there are no public permissions.\nfunc (ps *Permissions) SetPublicPermissions(permissions ...boards.Permission) {\n\tps.public = boards.NewPermissionSet(permissions...)\n}\n\n// AddRole adds a role with one or more assigned permissions.\n// If role exists its permissions are overwritten with the new ones.\nfunc (ps *Permissions) AddRole(r boards.Role, p boards.Permission, extra ...boards.Permission) {\n\t// If role is the super role it already has all permissions\n\tif ps.superRole == r {\n\t\treturn\n\t}\n\n\t// Get member group for the role if it exists or otherwise create a new group\n\tgrouping := ps.dao.Members().Grouping()\n\tname := string(r)\n\tgroup, found := grouping.Get(name)\n\tif !found {\n\t\tvar err error\n\t\tgroup, err = grouping.Add(name)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\n\t// Save permissions within the member group overwritting any existing permissions\n\tpermissions := append([]boards.Permission{p}, extra...)\n\tgroup.SetMeta(boards.NewPermissionSet(permissions...))\n}\n\n// RoleExists checks if a role exists.\nfunc (ps Permissions) RoleExists(r boards.Role) bool {\n\treturn r == ps.superRole || ps.dao.Members().Grouping().Has(string(r))\n}\n\n// GetUserRoles returns the list of roles assigned to a user.\nfunc (ps Permissions) GetUserRoles(user address) []boards.Role {\n\tgroups := storage.GetMemberGroups(ps.dao.Members(), user)\n\tif groups == nil {\n\t\treturn nil\n\t}\n\n\troles := make([]boards.Role, len(groups))\n\tfor i, name := range groups {\n\t\troles[i] = boards.Role(name)\n\t}\n\treturn roles\n}\n\n// HasRole checks if a user has a specific role assigned.\nfunc (ps Permissions) HasRole(user address, r boards.Role) bool {\n\tname := string(r)\n\tgroup, found := ps.dao.Members().Grouping().Get(name)\n\tif !found {\n\t\treturn false\n\t}\n\treturn group.Members().Has(user)\n}\n\n// HasPermission checks if a user has a specific permission.\nfunc (ps Permissions) HasPermission(user address, perm boards.Permission) bool {\n\tif ps.public.Has(perm) {\n\t\treturn true\n\t}\n\n\tgroups := storage.GetMemberGroups(ps.dao.Members(), user)\n\tif groups == nil {\n\t\treturn false\n\t}\n\n\tgrouping := ps.dao.Members().Grouping()\n\tfor _, name := range groups {\n\t\trole := boards.Role(name)\n\t\tif ps.superRole == role {\n\t\t\treturn true\n\t\t}\n\n\t\tgroup, found := grouping.Get(name)\n\t\tif !found {\n\t\t\tcontinue\n\t\t}\n\n\t\tmeta := group.GetMeta()\n\t\tif perms, ok := meta.(boards.PermissionSet); ok \u0026\u0026 perms.Has(perm) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// SetUserRoles adds a new user when it doesn't exist and sets its roles.\n// Method can also be called to change the roles of an existing user.\n// It removes any existing user roles before assigning new ones.\n// All user's roles can be removed by calling this method without roles.\nfunc (ps *Permissions) SetUserRoles(user address, roles ...boards.Role) {\n\tif len(roles) \u003e 1 \u0026\u0026 ps.singleUserRole {\n\t\tpanic(\"user can only have one role\")\n\t}\n\n\tgroups := storage.GetMemberGroups(ps.dao.Members(), user)\n\tisGuest := len(roles) == 0\n\n\t// Clear current user roles\n\tgrouping := ps.dao.Members().Grouping()\n\tfor _, name := range groups {\n\t\tgroup, found := grouping.Get(name)\n\t\tif !found {\n\t\t\tcontinue\n\t\t}\n\n\t\tgroup.Members().Remove(user)\n\t}\n\n\t// Add user to the storage as guest when no roles are assigned\n\tif isGuest {\n\t\tps.dao.Members().Add(user)\n\t\treturn\n\t}\n\n\t// Add user to role groups\n\tfor _, r := range roles {\n\t\tname := string(r)\n\t\tgroup, found := grouping.Get(name)\n\t\tif !found {\n\t\t\tpanic(\"invalid role: \" + name)\n\t\t}\n\n\t\tgroup.Members().Add(user)\n\t}\n}\n\n// RemoveUser removes a user from permissions.\nfunc (ps *Permissions) RemoveUser(user address) bool {\n\tgroups := storage.GetMemberGroups(ps.dao.Members(), user)\n\tif groups == nil {\n\t\treturn ps.dao.Members().Remove(user)\n\t}\n\n\tgrouping := ps.dao.Members().Grouping()\n\tfor _, name := range groups {\n\t\tgroup, found := grouping.Get(name)\n\t\tif !found {\n\t\t\tcontinue\n\t\t}\n\n\t\tgroup.Members().Remove(user)\n\t}\n\treturn true\n}\n\n// HasUser checks if a user exists.\nfunc (ps Permissions) HasUser(user address) bool {\n\treturn ps.dao.Members().Has(user)\n}\n\n// UsersCount returns the total number of users the permissioner contains.\nfunc (ps Permissions) UsersCount() int {\n\treturn ps.dao.Members().Size()\n}\n\n// IterateUsers iterates permissions' users.\nfunc (ps Permissions) IterateUsers(start, count int, fn boards.UsersIterFn) (stopped bool) {\n\tps.dao.Members().IterateByOffset(start, count, func(addr address) bool {\n\t\tuser := boards.User{Address: addr}\n\t\tgroups := storage.GetMemberGroups(ps.dao.Members(), addr)\n\t\tif groups != nil {\n\t\t\tuser.Roles = make([]boards.Role, len(groups))\n\t\t\tfor i, name := range groups {\n\t\t\t\tuser.Roles[i] = boards.Role(name)\n\t\t\t}\n\t\t}\n\n\t\treturn fn(user)\n\t})\n\treturn\n}\n\n// WithPermission calls a callback when a user has a specific permission.\n// It panics on error or when a permission validator fails.\n// Callbacks are by default called when there is no validator function registered for the permission.\n// If a permission validation function exists it's called before calling the callback.\nfunc (ps *Permissions) WithPermission(user address, p boards.Permission, args boards.Args, cb func()) {\n\tif !ps.HasPermission(user, p) {\n\t\tpanic(\"unauthorized, user \" + user.String() + \" doesn't have the required permission\")\n\t}\n\n\t// Execute custom validation before calling the callback\n\tif v := ps.validators.Get(p.String()); v != nil {\n\t\terr := v.(ValidatorFunc)(ps, args)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\n\tcb()\n}\n"},{"name":"permissions_test.gno","body":"package permissions\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\n// Test permission constants\nconst (\n\ttestPermA boards.Permission = iota\n\ttestPermB\n\ttestPermC\n)\n\nvar _ boards.Permissions = (*Permissions)(nil)\n\nfunc TestBasicPermissionsWithPermission(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname       string\n\t\tuser       address\n\t\tpermission boards.Permission\n\t\targs       boards.Args\n\t\tsetup      func() *Permissions\n\t\terr        string\n\t\tcalled     bool\n\t}{\n\t\t{\n\t\t\tname:       \"ok\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermA,\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"foo\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\tcalled: true,\n\t\t},\n\t\t{\n\t\t\tname:       \"ok with arguments\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermA,\n\t\t\targs:       boards.Args{\"a\", \"b\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"foo\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\tcalled: true,\n\t\t},\n\t\t{\n\t\t\tname:       \"no permission\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermA,\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\terr: \"unauthorized, user g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 doesn't have the required permission\",\n\t\t},\n\t\t{\n\t\t\tname:       \"is not a DAO member\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermA,\n\t\t\tsetup: func() *Permissions {\n\t\t\t\treturn New()\n\t\t\t},\n\t\t\terr: \"unauthorized, user g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 doesn't have the required permission\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar called bool\n\n\t\t\tperms := tc.setup()\n\t\t\ttestCaseFn := func() {\n\t\t\t\tperms.WithPermission(tc.user, tc.permission, tc.args, func() {\n\t\t\t\t\tcalled = true\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, testCaseFn, \"expect panic with message\")\n\t\t\t\treturn\n\t\t\t} else {\n\t\t\t\turequire.NotPanics(t, cur, testCaseFn, \"expect no panic\")\n\t\t\t}\n\n\t\t\turequire.Equal(t, tc.called, called, \"expect callback to be called\")\n\t\t})\n\t}\n}\n\nfunc TestBasicPermissionsSetPublicPermissions(t *testing.T) {\n\tuser := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tperms := New()\n\n\t// Add a new role with permissions\n\tperms.AddRole(\"adminRole\", testPermA, testPermB, testPermC)\n\turequire.False(t, perms.HasPermission(user, testPermA))\n\turequire.False(t, perms.HasPermission(user, testPermB))\n\turequire.False(t, perms.HasPermission(user, testPermC))\n\n\t// Assign a couple of public permissions\n\tperms.SetPublicPermissions(testPermA, testPermC)\n\turequire.True(t, perms.HasPermission(user, testPermA))\n\turequire.False(t, perms.HasPermission(user, testPermB))\n\turequire.True(t, perms.HasPermission(user, testPermC))\n\n\t// Clear all public permissions\n\tperms.SetPublicPermissions()\n\turequire.False(t, perms.HasPermission(user, testPermA))\n\turequire.False(t, perms.HasPermission(user, testPermB))\n\turequire.False(t, perms.HasPermission(user, testPermC))\n}\n\nfunc TestBasicPermissionsGetUserRoles(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tuser  address\n\t\troles []string\n\t\tsetup func() *Permissions\n\t}{\n\t\t{\n\t\t\tname:  \"single role\",\n\t\t\tuser:  \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\troles: []string{\"admin\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"admin\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"admin\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:  \"multiple roles\",\n\t\t\tuser:  \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\troles: []string{\"admin\", \"bar\", \"foo\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"admin\", testPermA)\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.AddRole(\"bar\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"admin\", \"foo\", \"bar\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"without roles\",\n\t\t\tuser: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"not a user\",\n\t\t\tuser: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tsetup: func() *Permissions {\n\t\t\t\treturn New()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:  \"multiple users\",\n\t\t\tuser:  \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\troles: []string{\"admin\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"admin\", testPermA)\n\t\t\t\tperms.AddRole(\"bar\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"admin\")\n\t\t\t\tperms.SetUserRoles(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\", \"admin\")\n\t\t\t\tperms.SetUserRoles(\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\", \"admin\", \"bar\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tperms := tc.setup()\n\t\t\troles := perms.GetUserRoles(tc.user)\n\n\t\t\turequire.Equal(t, len(tc.roles), len(roles), \"user role count\")\n\t\t\tfor i, r := range roles {\n\t\t\t\tuassert.Equal(t, tc.roles[i], string(r))\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBasicPermissionsHasRole(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tuser  address\n\t\trole  boards.Role\n\t\tsetup func() *Permissions\n\t\twant  bool\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tuser: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\trole: \"admin\",\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"admin\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"admin\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname: \"ok with multiple roles\",\n\t\t\tuser: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\trole: \"foo\",\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"admin\", testPermA)\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"admin\", \"foo\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname: \"user without roles\",\n\t\t\tuser: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"has no role\",\n\t\t\tuser: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\trole: \"bar\",\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"foo\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tperms := tc.setup()\n\t\t\tgot := perms.HasRole(tc.user, tc.role)\n\t\t\tuassert.Equal(t, got, tc.want)\n\t\t})\n\t}\n}\n\nfunc TestBasicPermissionsHasPermission(t *testing.T) {\n\tcases := []struct {\n\t\tname       string\n\t\tuser       address\n\t\tpermission boards.Permission\n\t\tsetup      func() *Permissions\n\t\twant       bool\n\t}{\n\t\t{\n\t\t\tname:       \"ok\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermA,\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"foo\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname:       \"ok with multiple users\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermA,\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"foo\")\n\t\t\t\tperms.SetUserRoles(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\", \"foo\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname:       \"ok with multiple roles\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermB,\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.AddRole(\"baz\", testPermB)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"foo\", \"baz\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname:       \"no permission\",\n\t\t\tuser:       \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tpermission: testPermB,\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"foo\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"foo\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tperms := tc.setup()\n\t\t\tgot := perms.HasPermission(tc.user, tc.permission)\n\t\t\tuassert.Equal(t, tc.want, got)\n\t\t})\n\t}\n}\n\nfunc TestBasicPermissionsSetUserRoles(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname          string\n\t\tuser          address\n\t\texpectedRoles []boards.Role\n\t\tsetup         func() *Permissions\n\t\terr           string\n\t}{\n\t\t{\n\t\t\tname:          \"add user\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"a\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:          \"add user with multiple roles\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"a\", \"b\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\tperms.AddRole(\"b\", testPermB)\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:          \"add when other users exists\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"a\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\tperms.SetUserRoles(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\", \"a\")\n\t\t\t\tperms.SetUserRoles(\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:          \"add user using single role\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"a\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New(UseSingleUserRole())\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:          \"update user roles\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"a\", \"b\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\tperms.AddRole(\"b\", testPermB)\n\t\t\t\tperms.AddRole(\"c\", testPermB)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"c\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:          \"update user roles using single role\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"b\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New(UseSingleUserRole())\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\tperms.AddRole(\"b\", testPermB)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"a\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:          \"clear user roles\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\tperms.AddRole(\"b\", testPermB)\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"a\", \"b\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:          \"set invalid role\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"a\", \"foo\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\terr: \"invalid role: foo\",\n\t\t},\n\t\t{\n\t\t\tname:          \"use single role error\",\n\t\t\tuser:          address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\texpectedRoles: []boards.Role{\"a\", \"b\"},\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New(UseSingleUserRole())\n\t\t\t\tperms.AddRole(\"a\", testPermA)\n\t\t\t\tperms.AddRole(\"b\", testPermB)\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\terr: \"user can only have one role\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tperms := tc.setup()\n\n\t\t\tsetUserRoles := func() {\n\t\t\t\tperms.SetUserRoles(tc.user, tc.expectedRoles...)\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, setUserRoles, \"expected an error\")\n\t\t\t\treturn\n\t\t\t} else {\n\t\t\t\turequire.NotPanics(t, cur, setUserRoles, \"expected no error\")\n\t\t\t}\n\n\t\t\troles := perms.GetUserRoles(tc.user)\n\t\t\tuassert.Equal(t, len(tc.expectedRoles), len(roles))\n\t\t\tfor i, r := range roles {\n\t\t\t\turequire.Equal(t, string(tc.expectedRoles[i]), string(r))\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBasicPermissionsRemoveUser(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tuser  address\n\t\tsetup func() *Permissions\n\t\twant  bool\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\tuser: address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\tsetup: func() *Permissions {\n\t\t\t\tperms := New()\n\t\t\t\tperms.SetUserRoles(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\treturn perms\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname: \"user not found\",\n\t\t\tuser: address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\tsetup: func() *Permissions {\n\t\t\t\treturn New()\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tperms := tc.setup()\n\t\t\tgot := perms.RemoveUser(tc.user)\n\t\t\tuassert.Equal(t, tc.want, got)\n\t\t})\n\t}\n}\n\nfunc TestBasicPermissionsIterateUsers(t *testing.T) {\n\tusers := []boards.User{\n\t\t{\n\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\tRoles:   []boards.Role{\"foo\"},\n\t\t},\n\t\t{\n\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\tRoles:   []boards.Role{\"bar\", \"foo\"},\n\t\t},\n\t\t{\n\t\t\tAddress: \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\",\n\t\t\tRoles:   []boards.Role{\"bar\"},\n\t\t},\n\t}\n\n\tperms := New()\n\tperms.AddRole(\"foo\", testPermA)\n\tperms.AddRole(\"bar\", testPermB)\n\tfor _, u := range users {\n\t\tperms.SetUserRoles(u.Address, u.Roles...)\n\t}\n\n\tcases := []struct {\n\t\tname               string\n\t\tstart, count, want int\n\t}{\n\t\t{\n\t\t\tname:  \"exceed users count\",\n\t\t\tcount: 50,\n\t\t\twant:  3,\n\t\t},\n\t\t{\n\t\t\tname:  \"exact users count\",\n\t\t\tcount: 3,\n\t\t\twant:  3,\n\t\t},\n\t\t{\n\t\t\tname:  \"two users\",\n\t\t\tstart: 1,\n\t\t\tcount: 2,\n\t\t\twant:  2,\n\t\t},\n\t\t{\n\t\t\tname:  \"one user\",\n\t\t\tstart: 1,\n\t\t\tcount: 1,\n\t\t\twant:  1,\n\t\t},\n\t\t{\n\t\t\tname:  \"no iteration\",\n\t\t\tstart: 50,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar i int\n\t\t\tperms.IterateUsers(0, len(users), func(u boards.User) bool {\n\t\t\t\turequire.True(t, i \u003c len(users), \"expect iterator to respect number of users\")\n\t\t\t\tuassert.Equal(t, users[i].Address, u.Address)\n\n\t\t\t\turequire.Equal(t, len(users[i].Roles), len(u.Roles), \"expect number of roles to match\")\n\t\t\t\tfor j := range u.Roles {\n\t\t\t\t\tuassert.Equal(t, string(users[i].Roles[j]), string(u.Roles[j]))\n\t\t\t\t}\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tuassert.Equal(t, i, len(users), \"expect iterator to iterate all users\")\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"3GsC5ZMauhMAi2EeP74spcERaaY+sqPe2J8PgiIThgcpD7dECJutEcgGbFssUE0EiciQoolbd5nxHc2M2PY/Cg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"allowancesender","path":"gno.land/p/jaekwon/allowancesender/v0","files":[{"name":"allowancesender.gno","body":"// Package allowancesender provides a bounded, granter-revocable\n// spending capability that wraps a banker.Banker source.\n//\n// # Use case\n//\n// Realm A wants to grant realm B the ability to spend up to some\n// bounded amount from A's address (or A's tx envelope) within a single\n// call, without giving B unbounded access. After A's call to B returns,\n// A revokes the capability via Close(); B cannot use it across tx\n// boundaries even if it persisted the reference.\n//\n// # Canonical pattern\n//\n//\tsrc := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n//\tal  := allowancesender.New(src, granterAddr,\n//\t           chain.NewCoins(chain.NewCoin(\"ugnot\", 1_000_000)))\n//\tdefer al.Close()\n//\tbRealm.DoSomething(cross, al)  // B can call al.Send up to cap\n//\n// On return (success or panic), `defer al.Close()` flips the closed\n// flag. If B persisted al, B's stored reference is now dead — any\n// later al.Send() panics with \"closed\".\n//\n// # Naming note\n//\n// Despite the underlying type wrapping a banker.Banker, *AllowanceSender\n// does NOT satisfy the banker.Banker interface. The other Banker methods\n// (GetCoins, TotalCoin, IssueCoin, RemoveCoin) have no meaningful\n// behavior on an allowance abstraction:\n//\n//   - GetCoins would either leak the granter realm's full balance\n//     (misleading) or return Remaining() (lying about the addr arg).\n//   - TotalCoin/IssueCoin/RemoveCoin are out of scope.\n//\n// Send is the only meaningful operation; hence the \"Sender\" name. If\n// you need to plug into a banker.Banker-shaped API, write an adapter\n// in your own package.\n//\n// # Security model\n//\n//   - Pointer-based API. All callees that hold a *AllowanceSender share\n//     the same underlying state. Close() flips a bool that every reader\n//     observes synchronously.\n//   - Inner banker is an unexported field. Callees cannot extract it\n//     to bypass the wrapper. Language-enforced.\n//   - Cap is enforced per-denom via chain.Coins.AmountOf. Any denom in\n//     amt that is missing from cap implicitly has capAmt=0; positive\n//     spend on such a denom panics with \"cap exceeded\".\n//   - Persistence is the kill switch's friend, not its enemy. The\n//     closed flag persists with the struct, so a callee that stored\n//     the reference for cross-tx use sees a closed allowance.\n//   - Defer-close survives panic. If B's call panics, the tx reverts\n//     entirely (so the close is irrelevant — state reverts anyway). On\n//     success, defer fires before granter's function returns,\n//     committing closed=true with the rest of the tx state.\n//   - Cap/Spent return defensive copies. Mutating the returned\n//     chain.Coins does not affect internal state.\n//   - Inner banker panic on SendCoins (e.g. bank-keeper \"insufficient\n//     funds\") rolls back the spent counter — caller's accounting stays\n//     accurate even if a defer-recover swallows the panic. (Without\n//     recovery, the entire tx reverts and the rollback is moot.)\n//   - Arithmetic uses math/overflow.Add64. Overflow on (spent+amount)\n//     panics rather than silently wrapping (which could bypass the cap\n//     check). Negative amounts are explicitly rejected.\n//\n// # Design choices\n//\n//   - Pointer type, not value: forces shared state across references.\n//     Value-copy AllowanceSender would have separate closed flags and\n//     defeat the kill switch.\n//   - One-shot Close, not graduated: simpler invariant. If you need\n//     \"resume later,\" create a fresh allowance.\n//   - No destination allowlist: out of scope. If the granter wants to\n//     restrict where funds go, wrap this further.\n//   - No time-based expiry: rely on Close. Block-height stamping or\n//     deadline checks would require runtime cooperation; this package\n//     is pure Gno.\n//   - Cap is multi-denom (chain.Coins, not int64): supports\n//     heterogeneous payment policies. Most callers use single-denom.\n//   - Idempotent Close: safe to defer-Close even if Close was called\n//     manually earlier in the function.\n//   - Does NOT satisfy banker.Banker: see \"Naming note\" above.\n//\n// # Limitations\n//\n//   - Granter retains direct access to the underlying source banker.\n//     The AllowanceSender only constrains the wrapped capability, not\n//     the granter's ability to spend its own funds via other means.\n//   - No cross-realm enforcement of granter identity. The source\n//     banker's own pkgAddr check (banker.gno) rejects mismatched\n//     froms. This package relies on banker's own protection there.\n//   - Re-entrancy: nested allowances work (each is independent), but\n//     do NOT re-use the same AllowanceSender pointer across grants —\n//     always create a fresh one. Re-using would mix spent counters.\n//   - Cap with duplicate denoms (constructed by hand, not via\n//     chain.NewCoins) will trigger chain.Coins.AmountOf to panic on\n//     read. Use chain.NewCoins(...) to construct cap; it deduplicates.\n//   - Persistence: granter is responsible for clearing references to\n//     closed AllowanceSender pointers from its own state if it wants\n//     them garbage-collected; otherwise they linger as dead structs.\n//\n// # What this package does NOT solve\n//\n//   - \"Allowance survives across N txs but not N+1 txs.\" Use a session\n//     counter pattern in the granter realm; this package's Close is\n//     binary, not deadline-based.\n//   - \"Fungible-token (grc20) allowances.\" Use the grc20 package's\n//     own Approve/Allowance/TransferFrom triple. AllowanceSender is\n//     for native (chain-coin) sends only.\n//\n// # Events\n//\n// AllowanceSender emits chain events at two points so off-chain\n// observers can track allowance lifecycles. Events are emitted from\n// the calling realm's package path (whichever realm holds the\n// AllowanceSender pointer when the method is called).\n//\n//   - \"AllowanceSenderSend\": on each successful Send, with attributes\n//     \"payer\", \"to\", \"amount\", \"spent_total\", \"remaining\".\n//   - \"AllowanceSenderClose\": on Close (only the first call; idempotent\n//     re-Closes do not re-emit).\n//\n// The underlying inner banker also emits its own bank-keeper events\n// for the actual coin movement; AllowanceSender's events sit on top of\n// those for allowance-level audit trails.\npackage allowancesender\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"math/overflow\"\n)\n\n// Event names emitted by AllowanceSender. Use these constants when\n// asserting on events from off-chain observers or test code.\nconst (\n\tEventSend  = \"AllowanceSenderSend\"\n\tEventClose = \"AllowanceSenderClose\"\n)\n\n// AllowanceSender is a bounded, revocable spending capability over a\n// source banker.Banker. Always pass *AllowanceSender (pointer); value\n// copies are not supported and would not share state.\n//\n// Does NOT satisfy banker.Banker — the other Banker methods (GetCoins,\n// TotalCoin, IssueCoin, RemoveCoin) have no meaningful behavior on an\n// allowance and are deliberately omitted.\ntype AllowanceSender struct {\n\tinner  banker.Banker // unexported — callees cannot extract\n\tpayer  address       // address debited; must match inner's source\n\tlimit  chain.Coins   // maximum cumulative spend, per-denom\n\tspent  chain.Coins   // running total of spends, per-denom\n\tclosed bool          // once true, all further Send calls panic\n}\n\n// New creates a new AllowanceSender wrapping inner with the given limit.\n// payer is the address that inner.SendCoins will draw from (must match\n// the realm address inner was created against; the banker enforces this\n// at SendCoins time).\n//\n// inner must be the canonical Banker produced by banker.NewBanker;\n// hand-rolled Banker implementations (no-op fakes, decorators) are\n// rejected via banker.IsCanonical. This guarantees that a callee\n// receiving a *AllowanceSender from a granter realm can rely on Send\n// actually moving real coins via the bank-keeper, not via a fake\n// banker that no-ops SendCoins.\n//\n// Panics if inner is not canonical or payer is invalid. limit may be\n// empty (zero allowance — every Send panics with \"cap exceeded\");\n// limit may also contain multiple denoms.\nfunc New(inner banker.Banker, payer address, limit chain.Coins) *AllowanceSender {\n\tif !banker.IsCanonical(inner) {\n\t\tpanic(\"allowancesender: inner banker is not the canonical chain/banker.Banker\")\n\t}\n\tif !payer.IsValid() {\n\t\tpanic(\"allowancesender: payer address is invalid\")\n\t}\n\treturn \u0026AllowanceSender{\n\t\tinner: inner,\n\t\tpayer: payer,\n\t\tlimit: limit,\n\t}\n}\n\n// Send debits up to (cap - spent) per denom from payer to to via the\n// inner banker. Updates spent on success. On any panic (including\n// inner.SendCoins panic for bank-level reasons like insufficient\n// balance), spent is rolled back — caller's accounting stays accurate\n// even if a caller wraps Send in a defer-recover.\n//\n// Arithmetic uses math/overflow.Add64; an overflow on (spent+amount)\n// panics with a distinct message rather than silently wrapping. This\n// closes the int64-overflow vector where a malicious callee passes a\n// MaxInt64-style amount to bypass the cap check.\n//\n// Negative amounts are rejected explicitly. chain.NewCoin permits\n// signed amounts at construction; we don't accept them.\n//\n// Emits \"AllowanceSenderSend\" event on success.\n//\n// Panics:\n//   - \"closed\" if Close has been called.\n//   - \"negative amount not allowed\" if any c.Amount \u003c 0.\n//   - \"amount overflow on spent+amt\" if int64 addition would overflow.\n//   - \"cap exceeded for denom \u003cdenom\u003e\" if any denom in amt would push\n//     spent over cap.\n//   - inner.SendCoins panics propagate (typically \"insufficient\n//     funds\" from the bank keeper); spent is rolled back first.\nfunc (a *AllowanceSender) Send(to address, amt chain.Coins) {\n\tif a.closed {\n\t\tpanic(\"allowancesender: closed\")\n\t}\n\n\t// Per-denom validation. Done in a separate pass before any state\n\t// mutation so a partial failure doesn't leak into spent.\n\tfor _, c := range amt {\n\t\tif c.Amount \u003c 0 {\n\t\t\tpanic(\"allowancesender: negative amount not allowed for denom \" + c.Denom)\n\t\t}\n\t\tcapAmt := a.limit.AmountOf(c.Denom)\n\t\tspentAmt := a.spent.AmountOf(c.Denom)\n\t\tsum, ok := overflow.Add64(spentAmt, c.Amount)\n\t\tif !ok {\n\t\t\tpanic(\"allowancesender: amount overflow on spent+amt for denom \" + c.Denom)\n\t\t}\n\t\tif sum \u003e capAmt {\n\t\t\tpanic(\"allowancesender: cap exceeded for denom \" + c.Denom)\n\t\t}\n\t}\n\n\t// Snapshot for rollback. If inner.SendCoins panics — even if a\n\t// caller's defer-recover swallows the panic — the deferred\n\t// rollback below restores spent before re-raising. Without this,\n\t// a recovered panic would leave spent inflated for a transfer\n\t// that didn't move funds at the bank-keeper level, allowing an\n\t// adversary to \"burn\" allowance against failed sends.\n\tprevSpent := a.spent\n\ta.spent = a.spent.Add(amt)\n\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\ta.spent = prevSpent\n\t\t\tpanic(r) // re-raise so caller learns of the failure\n\t\t}\n\t}()\n\n\ta.inner.SendCoins(a.payer, to, amt)\n\n\t// Reached only on successful inner.SendCoins. Emit the audit\n\t// event from the granter realm's perspective.\n\tchain.Emit(EventSend,\n\t\t\"payer\", a.payer.String(),\n\t\t\"to\", to.String(),\n\t\t\"amount\", amt.String(),\n\t\t\"spent_total\", a.spent.String(),\n\t\t\"remaining\", a.Remaining().String(),\n\t)\n}\n\n// Cap returns a defensive copy of the maximum cumulative spend.\n// Mutating the returned slice does not affect internal state.\nfunc (a *AllowanceSender) Cap() chain.Coins {\n\treturn cloneCoins(a.limit)\n}\n\n// Spent returns a defensive copy of the cumulative amount spent.\n// Mutating the returned slice does not affect internal state.\nfunc (a *AllowanceSender) Spent() chain.Coins {\n\treturn cloneCoins(a.spent)\n}\n\n// Remaining returns cap - spent, per denom. Denoms with non-positive\n// remainder are omitted.\nfunc (a *AllowanceSender) Remaining() chain.Coins {\n\tif len(a.limit) == 0 {\n\t\treturn nil\n\t}\n\tout := make(chain.Coins, 0, len(a.limit))\n\tfor _, c := range a.limit {\n\t\trem := c.Amount - a.spent.AmountOf(c.Denom)\n\t\tif rem \u003e 0 {\n\t\t\tout = append(out, chain.NewCoin(c.Denom, rem))\n\t\t}\n\t}\n\treturn out\n}\n\n// Closed reports whether Close has been called.\nfunc (a *AllowanceSender) Closed() bool {\n\treturn a.closed\n}\n\n// Close terminates the allowance. Subsequent Send calls panic.\n// Idempotent — calling twice is fine; the second call is a no-op and\n// does not re-emit the AllowanceSenderClose event.\nfunc (a *AllowanceSender) Close() {\n\tif a.closed {\n\t\treturn\n\t}\n\ta.closed = true\n\tchain.Emit(EventClose,\n\t\t\"payer\", a.payer.String(),\n\t\t\"spent_total\", a.spent.String(),\n\t)\n}\n\n// Payer returns the address that this allowance debits from.\nfunc (a *AllowanceSender) Payer() address {\n\treturn a.payer\n}\n\n// cloneCoins returns a fresh chain.Coins that does not share an\n// underlying array with the source. Used to defend internal state\n// from caller mutation.\nfunc cloneCoins(src chain.Coins) chain.Coins {\n\tif len(src) == 0 {\n\t\treturn nil\n\t}\n\tout := make(chain.Coins, len(src))\n\tcopy(out, src)\n\treturn out\n}\n"},{"name":"allowancesender_test.gno","body":"package allowancesender\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\n// mockBanker implements banker.Banker for unit testing without\n// touching real bank state. Records every SendCoins call.\n//\n// Since the public New constructor rejects non-canonical bankers via\n// banker.IsCanonical, mockBanker cannot flow through New. Tests that\n// need a controllable mock build the AllowanceSender via newWithMock,\n// which uses in-package struct-literal construction. Same-package\n// authorship is the trust boundary; the public API remains unforgeable\n// to other packages.\ntype mockBanker struct {\n\tsends    []sendCall\n\tfailNext bool   // if true, next SendCoins panics\n\tfailMsg  string // panic message for failNext\n}\n\ntype sendCall struct {\n\tfrom, to address\n\tamt      chain.Coins\n}\n\nfunc (m *mockBanker) GetCoins(addr address) chain.Coins { return nil }\nfunc (m *mockBanker) SendCoins(from, to address, amt chain.Coins) {\n\tif m.failNext {\n\t\tm.failNext = false\n\t\tpanic(m.failMsg)\n\t}\n\tm.sends = append(m.sends, sendCall{from, to, amt})\n}\nfunc (m *mockBanker) TotalCoin(denom string) int64                        { return 0 }\nfunc (m *mockBanker) IssueCoin(addr address, denom string, amount int64)  {}\nfunc (m *mockBanker) RemoveCoin(addr address, denom string, amount int64) {}\n\n// newWithMock constructs an *AllowanceSender wrapping a mockBanker via\n// struct-literal initialization, bypassing New (which would reject the\n// mock as non-canonical). Used only in this test file.\nfunc newWithMock(mb *mockBanker, p address, limit chain.Coins) *AllowanceSender {\n\treturn \u0026AllowanceSender{\n\t\tinner: mb,\n\t\tpayer: p,\n\t\tlimit: limit,\n\t}\n}\n\nvar (\n\tpayer = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tbob   = address(\"g1mtmrdmqfu0aryqfl4aw65n35haw2wdjkh5p4cp\")\n\tcarol = address(\"g16dverxg6s6vzha89k22c3zusx6sug5ycjyjq2j\")\n)\n\n// ---------------------------------------------------------------------\n// Constructor tests\n// ---------------------------------------------------------------------\n\nfunc TestNew_Valid(t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tlimit := chain.NewCoins(chain.NewCoin(\"ugnot\", 1000))\n\tal := newWithMock(mb, payer, limit)\n\n\tuassert.Equal(t, false, al.Closed())\n\tuassert.Equal(t, payer.String(), al.Payer().String())\n\tuassert.Equal(t, limit.String(), al.Cap().String())\n}\n\nfunc TestNew_NonCanonicalBanker(cur realm, t *testing.T) {\n\t// A hand-rolled banker.Banker (mockBanker) is not the canonical\n\t// chain/banker.Banker; New must reject it. This is the security\n\t// hole the IsCanonical gate closes — without it, a malicious caller\n\t// could pass a no-op banker and cause AllowanceSender.Send to\n\t// silently succeed without moving real coins.\n\tmb := \u0026mockBanker{}\n\tuassert.PanicsWithMessage(t, cur,\n\t\t\"allowancesender: inner banker is not the canonical chain/banker.Banker\",\n\t\tfunc() {\n\t\t\tNew(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1)))\n\t\t})\n}\n\nfunc TestNew_NilInner(cur realm, t *testing.T) {\n\t// Nil also fails IsCanonical (nil interface assertion is false).\n\tuassert.PanicsWithMessage(t, cur,\n\t\t\"allowancesender: inner banker is not the canonical chain/banker.Banker\",\n\t\tfunc() {\n\t\t\tNew(nil, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1)))\n\t\t})\n}\n\nfunc TestNew_EmptyCap(t *testing.T) {\n\t// Empty cap is valid at construction (every Send will panic with\n\t// \"cap exceeded\"). Useful for \"preallocated zero-allowance\" cases.\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.Coins{})\n\tuassert.Equal(t, false, al.Closed())\n}\n\n// ---------------------------------------------------------------------\n// Send happy-path tests\n// ---------------------------------------------------------------------\n\nfunc TestSend_WithinCap(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 400)))\n\t})\n\n\tuassert.Equal(t, 1, len(mb.sends))\n\tuassert.Equal(t, payer.String(), mb.sends[0].from.String())\n\tuassert.Equal(t, bob.String(), mb.sends[0].to.String())\n\tuassert.Equal(t, int64(400), mb.sends[0].amt.AmountOf(\"ugnot\"))\n}\n\nfunc TestSend_MultipleWithinCap(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 300)))\n\t})\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(carol, chain.NewCoins(chain.NewCoin(\"ugnot\", 600)))\n\t})\n\n\tuassert.Equal(t, 2, len(mb.sends))\n\tuassert.Equal(t, int64(900), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(100), al.Remaining().AmountOf(\"ugnot\"))\n}\n\nfunc TestSend_ExactlyAtCap(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\t})\n\tuassert.Equal(t, int64(1000), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, 0, len(al.Remaining()))\n}\n\n// ---------------------------------------------------------------------\n// Send rejection tests\n// ---------------------------------------------------------------------\n\nfunc TestSend_ExceedsCapInOneCall(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: cap exceeded for denom ugnot\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 1001)))\n\t})\n\t// State must not change on rejection.\n\tuassert.Equal(t, 0, len(mb.sends))\n\tuassert.Equal(t, int64(0), al.Spent().AmountOf(\"ugnot\"))\n}\n\nfunc TestSend_ExceedsCapAcrossCalls(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 700)))\n\t})\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: cap exceeded for denom ugnot\", func() {\n\t\tal.Send(carol, chain.NewCoins(chain.NewCoin(\"ugnot\", 400)))\n\t})\n\t// First send happened, second didn't.\n\tuassert.Equal(t, 1, len(mb.sends))\n\tuassert.Equal(t, int64(700), al.Spent().AmountOf(\"ugnot\"))\n}\n\nfunc TestSend_DenomNotInCap(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\t// \"atom\" is not in cap; capAmt=0; any positive amount panics.\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: cap exceeded for denom atom\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"atom\", 1)))\n\t})\n}\n\nfunc TestSend_ZeroCap(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.Coins{}) // empty cap\n\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: cap exceeded for denom ugnot\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 1)))\n\t})\n}\n\nfunc TestSend_AfterClose(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\tal.Close()\n\tuassert.True(t, al.Closed())\n\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: closed\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 100)))\n\t})\n\tuassert.Equal(t, 0, len(mb.sends))\n}\n\n// ---------------------------------------------------------------------\n// Multi-denom tests\n// ---------------------------------------------------------------------\n\nfunc TestSend_MultiDenomCap(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(\n\t\tchain.NewCoin(\"ugnot\", 1000),\n\t\tchain.NewCoin(\"foo\", 50),\n\t))\n\n\t// Spend on both denoms within their caps.\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(\n\t\t\tchain.NewCoin(\"ugnot\", 600),\n\t\t\tchain.NewCoin(\"foo\", 30),\n\t\t))\n\t})\n\n\tuassert.Equal(t, int64(600), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(30), al.Spent().AmountOf(\"foo\"))\n\n\trem := al.Remaining()\n\tuassert.Equal(t, int64(400), rem.AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(20), rem.AmountOf(\"foo\"))\n}\n\nfunc TestSend_OneDenomExceedsBlocksAll(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(\n\t\tchain.NewCoin(\"ugnot\", 1000),\n\t\tchain.NewCoin(\"foo\", 50),\n\t))\n\n\t// foo would exceed (51 \u003e 50); whole send rejected, ugnot doesn't go through.\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: cap exceeded for denom foo\", func() {\n\t\tal.Send(bob, chain.NewCoins(\n\t\t\tchain.NewCoin(\"ugnot\", 100),\n\t\t\tchain.NewCoin(\"foo\", 51),\n\t\t))\n\t})\n\n\t// Neither denom should have been spent.\n\tuassert.Equal(t, int64(0), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(0), al.Spent().AmountOf(\"foo\"))\n\tuassert.Equal(t, 0, len(mb.sends))\n}\n\n// ---------------------------------------------------------------------\n// Close behavior\n// ---------------------------------------------------------------------\n\nfunc TestClose_Idempotent(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\tal.Close()\n\turequire.NotPanics(t, cur, func() { al.Close() })\n\turequire.NotPanics(t, cur, func() { al.Close() })\n\tuassert.True(t, al.Closed())\n}\n\nfunc TestClose_DoesNotInterfereWithReadMethods(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tlimit := chain.NewCoins(chain.NewCoin(\"ugnot\", 1000))\n\tal := newWithMock(mb, payer, limit)\n\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 250)))\n\t})\n\tal.Close()\n\n\t// Read methods must keep working after close.\n\tuassert.Equal(t, limit.String(), al.Cap().String())\n\tuassert.Equal(t, int64(250), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(750), al.Remaining().AmountOf(\"ugnot\"))\n\tuassert.True(t, al.Closed())\n}\n\n// ---------------------------------------------------------------------\n// Inner-banker panic tests\n// ---------------------------------------------------------------------\n\nfunc TestSend_InnerPanicRollsBackSpent(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{failNext: true, failMsg: \"insufficient funds\"}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\tuassert.PanicsWithMessage(t, cur, \"insufficient funds\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 500)))\n\t})\n\n\t// On inner panic, spent must be rolled back.\n\tuassert.Equal(t, int64(0), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(1000), al.Remaining().AmountOf(\"ugnot\"))\n\n\t// And subsequent valid Send still works.\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 200)))\n\t})\n\tuassert.Equal(t, int64(200), al.Spent().AmountOf(\"ugnot\"))\n}\n\n// ---------------------------------------------------------------------\n// Remaining / Spent edge cases\n// ---------------------------------------------------------------------\n\nfunc TestRemaining_EmptyCap(t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.Coins{})\n\n\tuassert.Equal(t, 0, len(al.Remaining()))\n}\n\nfunc TestRemaining_FullySpent(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 100)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 100)))\n\t})\n\tuassert.Equal(t, 0, len(al.Remaining()))\n}\n\nfunc TestRemaining_PartialAcrossDenoms(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(\n\t\tchain.NewCoin(\"ugnot\", 1000),\n\t\tchain.NewCoin(\"foo\", 50),\n\t))\n\n\t// Spend all of foo, half of ugnot.\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(\n\t\t\tchain.NewCoin(\"ugnot\", 500),\n\t\t\tchain.NewCoin(\"foo\", 50),\n\t\t))\n\t})\n\n\trem := al.Remaining()\n\t// foo should be omitted (zero remainder); ugnot has 500 left.\n\tuassert.Equal(t, 1, len(rem))\n\tuassert.Equal(t, int64(500), rem.AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(0), rem.AmountOf(\"foo\"))\n}\n\n// ---------------------------------------------------------------------\n// Pointer semantics: shared state across copies\n// ---------------------------------------------------------------------\n\nfunc TestPointerSemantics_CloseVisibleViaAlias(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\t// Aliasing the pointer should share state.\n\talias := al\n\turequire.NotPanics(t, cur, func() {\n\t\talias.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 100)))\n\t})\n\tuassert.Equal(t, int64(100), al.Spent().AmountOf(\"ugnot\"))\n\n\t// Close via either reference.\n\talias.Close()\n\tuassert.True(t, al.Closed())\n\tuassert.True(t, alias.Closed())\n\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: closed\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 1)))\n\t})\n}\n\n// ---------------------------------------------------------------------\n// Defensive copy: mutating returned Coins must not leak into state\n// ---------------------------------------------------------------------\n\nfunc TestCap_ReturnsDefensiveCopy(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\t// Mutate the returned cap; internal state must not change.\n\tgot := al.Cap()\n\tif len(got) \u003e 0 {\n\t\tgot[0].Amount = 9_999_999_999\n\t}\n\n\t// Confirm internal state intact: re-read Cap.\n\tagain := al.Cap()\n\tuassert.Equal(t, int64(1000), again.AmountOf(\"ugnot\"))\n\n\t// Confirm enforcement still works: 1001 should still be rejected.\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: cap exceeded for denom ugnot\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 1001)))\n\t})\n}\n\nfunc TestSpent_ReturnsDefensiveCopy(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 300)))\n\t})\n\n\tgot := al.Spent()\n\tif len(got) \u003e 0 {\n\t\tgot[0].Amount = 0 // try to \"rewind\" spent\n\t}\n\n\t// Internal spent must remain accurate.\n\tuassert.Equal(t, int64(300), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(700), al.Remaining().AmountOf(\"ugnot\"))\n}\n\n// ---------------------------------------------------------------------\n// Independence of separate AllowanceSenders\n// ---------------------------------------------------------------------\n\nfunc TestMultipleAllowances_Independent(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\ta1 := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 100)))\n\ta2 := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 200)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\ta1.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 80)))\n\t})\n\turequire.NotPanics(t, cur, func() {\n\t\ta2.Send(carol, chain.NewCoins(chain.NewCoin(\"ugnot\", 150)))\n\t})\n\n\tuassert.Equal(t, int64(80), a1.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(150), a2.Spent().AmountOf(\"ugnot\"))\n\n\t// Closing one must not close the other.\n\ta1.Close()\n\tuassert.True(t, a1.Closed())\n\tuassert.False(t, a2.Closed())\n\n\turequire.NotPanics(t, cur, func() {\n\t\ta2.Send(carol, chain.NewCoins(chain.NewCoin(\"ugnot\", 50)))\n\t})\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: closed\", func() {\n\t\ta1.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 1)))\n\t})\n}\n\n// ---------------------------------------------------------------------\n// Overflow / negative-amount adversarial inputs\n// ---------------------------------------------------------------------\n\n// Adversary attempts to bypass the cap check by passing a value that\n// overflows int64 when added to an existing spent counter. Must panic\n// with the overflow-specific message rather than silently wrapping\n// (which would let the cap check pass and allow an absurd send).\nfunc TestSend_OverflowAttempt(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tconst maxInt64 = int64(9223372036854775807)\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", maxInt64)))\n\n\t// First spend uses most of the cap.\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", maxInt64-100)))\n\t})\n\n\t// Now spent ≈ MaxInt64 - 100. An adversary tries to pass amount\n\t// 200, which would overflow when added to spent. Must panic with\n\t// \"amount overflow\", not silently wrap to negative and bypass cap.\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: amount overflow on spent+amt for denom ugnot\", func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 200)))\n\t})\n\n\t// State must not have changed.\n\tuassert.Equal(t, maxInt64-100, al.Spent().AmountOf(\"ugnot\"))\n\t// Bank-side recorded only the first valid send.\n\tuassert.Equal(t, 1, len(mb.sends))\n}\n\nfunc TestSend_NegativeAmount(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\t// Spend something so we have a non-zero spent.\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 200)))\n\t})\n\n\t// Adversary tries to \"uncharge\" the allowance by passing a negative\n\t// amount. Without this guard, spent would decrease, allowing more\n\t// future spends than originally permitted.\n\tuassert.PanicsWithMessage(t, cur, \"allowancesender: negative amount not allowed for denom ugnot\", func() {\n\t\tal.Send(bob, chain.Coins{chain.Coin{Denom: \"ugnot\", Amount: -50}})\n\t})\n\n\t// Spent is unchanged after the rejected attempt.\n\tuassert.Equal(t, int64(200), al.Spent().AmountOf(\"ugnot\"))\n}\n\n// Adversary's defer-recover swallows the panic from inner.SendCoins to\n// hide a failed send. Verifies that even with recovery, spent has been\n// rolled back — i.e., a recovered panic doesn't burn allowance against\n// a transfer that never moved funds.\nfunc TestSend_RecoveredPanicDoesNotBurnAllowance(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{failNext: true, failMsg: \"insufficient funds\"}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\t// Caller deliberately recovers from the inner panic.\n\tfunc() {\n\t\tdefer func() {\n\t\t\t_ = recover() // swallow\n\t\t}()\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 500)))\n\t}()\n\n\t// After the recovered panic: spent must be 0 (no funds moved →\n\t// allowance must not be charged).\n\tuassert.Equal(t, int64(0), al.Spent().AmountOf(\"ugnot\"))\n\tuassert.Equal(t, int64(1000), al.Remaining().AmountOf(\"ugnot\"))\n\n\t// And subsequent valid Send (with non-failing inner) succeeds with\n\t// full cap available.\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.NewCoins(chain.NewCoin(\"ugnot\", 800)))\n\t})\n\tuassert.Equal(t, int64(800), al.Spent().AmountOf(\"ugnot\"))\n}\n\n// ---------------------------------------------------------------------\n// Empty/zero-amount edge cases\n// ---------------------------------------------------------------------\n\nfunc TestSend_EmptyAmt(cur realm, t *testing.T) {\n\tmb := \u0026mockBanker{}\n\tal := newWithMock(mb, payer, chain.NewCoins(chain.NewCoin(\"ugnot\", 1000)))\n\n\t// Sending zero coins should not panic; just no-op forwards to inner.\n\turequire.NotPanics(t, cur, func() {\n\t\tal.Send(bob, chain.Coins{})\n\t})\n\n\t// inner.SendCoins was still called (the wrapper doesn't filter\n\t// zero sends; that's the inner banker's choice). State unchanged.\n\tuassert.Equal(t, int64(0), al.Spent().AmountOf(\"ugnot\"))\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jaekwon/allowancesender/v0\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Ls537yr25fXtuJJc/iCCji+HsI6Z+2yedLNJIdM579at7V0+lA4zQNLY/439G/b7wtOC5XfBn91MoOEpvvDWAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"avlhelpers","path":"gno.land/p/jefft0/avlhelpers","files":[{"name":"avlhelpers.gno","body":"package avlhelpers\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// Iterate the keys in-order starting from the given prefix.\n// It calls the provided callback function for each key-value pair encountered.\n// If the callback returns true, the iteration is stopped.\n// The prefix and keys are treated as byte strings, ignoring possible multi-byte Unicode runes.\nfunc IterateByteStringKeysByPrefix(tree avl.ITree, prefix string, cb avl.IterCbFn) {\n\tend := \"\"\n\tn := len(prefix)\n\t// To make the end of the search, increment the final character ASCII by one.\n\tfor n \u003e 0 {\n\t\tif ascii := int(prefix[n-1]); ascii \u003c 0xff {\n\t\t\tend = prefix[0:n-1] + string(ascii+1)\n\t\t\tbreak\n\t\t}\n\n\t\t// The last character is 0xff. Try the previous character.\n\t\tn--\n\t}\n\n\ttree.Iterate(prefix, end, cb)\n}\n\n// Get a list of keys starting from the given prefix. Limit the\n// number of results to maxResults.\n// The prefix and keys are treated as byte strings, ignoring possible multi-byte Unicode runes.\nfunc ListByteStringKeysByPrefix(tree avl.ITree, prefix string, maxResults int) []string {\n\tresult := []string{}\n\tIterateByteStringKeysByPrefix(tree, prefix, func(key string, value any) bool {\n\t\tresult = append(result, key)\n\t\tif len(result) \u003e= maxResults {\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\treturn result\n}\n"},{"name":"example_test.gno","body":"package avlhelpers\n\nimport (\n\t\"encoding/hex\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc ExampleTextMatch() {\n\ttree := avl.NewTree()\n\n\t{\n\t\t// Empty tree.\n\t\tmatches := ListByteStringKeysByPrefix(tree, \"\", 10)\n\t\tprintln(ufmt.Sprintf(\"# matches: %d\", len(matches)))\n\t}\n\n\ttree.Set(\"alice\", \"\")\n\ttree.Set(\"andy\", \"\")\n\ttree.Set(\"bob\", \"\")\n\n\t{\n\t\t// Match only alice.\n\t\tmatches := ListByteStringKeysByPrefix(tree, \"al\", 10)\n\t\tprintln(ufmt.Sprintf(\"# matches: %d\", len(matches)))\n\t\tprintln(\"match: \" + matches[0])\n\t}\n\n\t{\n\t\t// Match alice and andy.\n\t\tmatches := ListByteStringKeysByPrefix(tree, \"a\", 10)\n\t\tprintln(ufmt.Sprintf(\"# matches: %d\", len(matches)))\n\t\tprintln(\"match: \" + matches[0])\n\t\tprintln(\"match: \" + matches[1])\n\t}\n\n\t{\n\t\t// Match alice and andy limited to 1.\n\t\tmatches := ListByteStringKeysByPrefix(tree, \"a\", 1)\n\t\tprintln(ufmt.Sprintf(\"# matches: %d\", len(matches)))\n\t\tprintln(\"match: \" + matches[0])\n\t}\n\n\t// Output:\n\t// # matches: 0\n\t// # matches: 1\n\t// match: alice\n\t// # matches: 2\n\t// match: alice\n\t// match: andy\n\t// # matches: 1\n\t// match: alice\n}\n\nfunc ExampleBinaryMatch() {\n\ttree := avl.NewTree()\n\ttree.Set(\"a\\xff\", \"\")\n\ttree.Set(\"a\\xff\\xff\", \"\")\n\ttree.Set(\"b\", \"\")\n\ttree.Set(\"\\xff\\xff\\x00\", \"\")\n\n\t{\n\t\t// Match only \"a\\xff\\xff\".\n\t\tmatches := ListByteStringKeysByPrefix(tree, \"a\\xff\\xff\", 10)\n\t\tprintln(ufmt.Sprintf(\"# matches: %d\", len(matches)))\n\t\tprintln(ufmt.Sprintf(\"match: %s\", hex.EncodeToString([]byte(matches[0]))))\n\t}\n\n\t{\n\t\t// Match \"a\\xff\" and \"a\\xff\\xff\".\n\t\tmatches := ListByteStringKeysByPrefix(tree, \"a\\xff\", 10)\n\t\tprintln(ufmt.Sprintf(\"# matches: %d\", len(matches)))\n\t\tprintln(ufmt.Sprintf(\"match: %s\", hex.EncodeToString([]byte(matches[0]))))\n\t\tprintln(ufmt.Sprintf(\"match: %s\", hex.EncodeToString([]byte(matches[1]))))\n\t}\n\n\t{\n\t\t// Edge case: Match only \"\\xff\\xff\\x00\".\n\t\tmatches := ListByteStringKeysByPrefix(tree, \"\\xff\\xff\", 10)\n\t\tprintln(ufmt.Sprintf(\"# matches: %d\", len(matches)))\n\t\tprintln(ufmt.Sprintf(\"match: %s\", hex.EncodeToString([]byte(matches[0]))))\n\t}\n\n\t// Output:\n\t// # matches: 1\n\t// match: 61ffff\n\t// # matches: 2\n\t// match: 61ff\n\t// match: 61ffff\n\t// # matches: 1\n\t// match: ffff00\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jefft0/avlhelpers\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"OKnyvrdDhVZ6mhATd+tZOVxdZtaLzvXJJr6l6fYQWM6OfHYzUIp/LHu37Qla2/0AuSEOcKkGSmJCRzZkRT57Bg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"mdform","path":"gno.land/p/jeronimoalbi/mdform","files":[{"name":"README.md","body":"# Markdown Form Package\n\nThe package provides a very simplistic [Gno-Flavored Markdown form](/r/docs/markdown#forms) generator.\n\nForms can be created by sequentially calling form methods to create each one of the form fields.\n\nExample usage:\n\n```go\nimport \"gno.land/p/jeronimoalbi/mdform\"\n\nfunc Render(string) string {\n    form := mdform.New()\n\n    // Add a text input field\n    form.Input(\n        \"name\",\n        \"placeholder\", \"Name\",\n        \"value\", \"John Doe\",\n    )\n\n    // Add a select field with three possible values\n    form.Select(\n        \"country\",\n        \"United States\",\n        \"description\", \"Select your country\",\n    )\n    form.Select(\n        \"country\",\n        \"Spain\",\n    )\n    form.Select(\n        \"country\",\n        \"Germany\",\n    )\n\n    // Add a checkbox group with two possible values\n    form.Checkbox(\n        \"interests\",\n        \"music\",\n        \"description\", \"What do you like to do?\",\n    )\n    form.Checkbox(\n        \"interests\",\n        \"tech\",\n        \"checked\", \"true\",\n    )\n\n    return form.String()\n}\n```\n\nForm output:\n\n```html\n\u003cgno-form exec=\"FunctionName\"\u003e\n    \u003cgno-input name=\"name\" placeholder=\"Name\" value=\"John Doe\" /\u003e\n    \u003cgno-select name=\"country\" value=\"United States\" description=\"Select your country\" /\u003e\n    \u003cgno-select name=\"country\" value=\"Spain\" /\u003e\n    \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n    \u003cgno-input type=\"checkbox\" name=\"interests\" value=\"music\" description=\"What do you like to do?\" /\u003e\n    \u003cgno-input type=\"checkbox\" name=\"interests\" value=\"tech\" checked=\"true\" /\u003e\n\u003c/gno-form\u003e\n```\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/mdform\"\ngno = \"0.9\"\n"},{"name":"mdform.gno","body":"package mdform\n\nimport (\n\t\"html\"\n\t\"strings\"\n)\n\nconst (\n\tInputTypeText     = \"text\"\n\tInputTypeNumber   = \"number\"\n\tInputTypeEmail    = \"email\"\n\tInputTypePhone    = \"tel\"\n\tInputTypePassword = \"password\"\n\tInputTypeRadio    = \"radio\"\n\tInputTypeCheckbox = \"checkbox\"\n)\n\nvar (\n\tformAttributes     = []string{\"exec\", \"path\"}\n\tinputAttributes    = []string{\"checked\", \"description\", \"placeholder\", \"readonly\", \"required\", \"type\", \"value\"}\n\ttextareaAttributes = []string{\"placeholder\", \"readonly\", \"required\", \"rows\", \"value\"}\n\tselectAttributes   = []string{\"description\", \"readonly\", \"required\", \"selected\"}\n)\n\n// New creates a new form.\nfunc New(attributes ...string) *Form {\n\tassertEvenAttributes(attributes)\n\n\tform := \u0026Form{}\n\tfor i := 0; i \u003c len(attributes); i += 2 {\n\t\tname, value := attributes[i], attributes[i+1]\n\n\t\tassertIsValidAttribute(name, formAttributes)\n\n\t\tform.attrs = append(form.attrs, formatAttribute(name, value))\n\t}\n\treturn form\n}\n\n// Form is a form that can be rendered to Gno-Flavored Markdown.\ntype Form struct {\n\tattrs  []string\n\tfields []string\n}\n\n// Input appends a new input to form fields.\n// Use `Form.Radio()` or `Form.Checkbox()` to append those types of inputs to the form.\n// Method panics when appending inputs of type radio or checkbox, or when attributes are not valid.\nfunc (f *Form) Input(name string, attributes ...string) *Form {\n\tname = strings.TrimSpace(name)\n\tif name == \"\" {\n\t\tpanic(\"form input name is required\")\n\t}\n\n\tassertEvenAttributes(attributes)\n\n\tattrs := []string{formatAttribute(\"name\", name)}\n\tfor i := 0; i \u003c len(attributes); i += 2 {\n\t\tname, value := attributes[i], attributes[i+1]\n\t\tif name == \"type\" {\n\t\t\tswitch value {\n\t\t\tcase InputTypeRadio:\n\t\t\t\tpanic(\"use form.Radio() to create inputs of type radio\")\n\t\t\tcase InputTypeCheckbox:\n\t\t\t\tpanic(\"use form.Checkbox() to create inputs of type checkbox\")\n\t\t\t}\n\t\t}\n\n\t\tassertIsValidAttribute(name, inputAttributes)\n\n\t\tattrs = append(attrs, formatAttribute(name, value))\n\t}\n\n\tf.fields = append(f.fields, \"\u003cgno-input \"+strings.Join(attrs, \" \")+\" /\u003e\")\n\treturn f\n}\n\n// Radio appends a new input of type radio to form fields.\n// Method panics when attributes are not valid.\nfunc (f *Form) Radio(name, value string, attributes ...string) *Form {\n\treturn f.appendInputType(InputTypeRadio, name, value, attributes...)\n}\n\n// Checkbox appends a new input of type checkbox to form fields.\n// Method panics when attributes are not valid.\nfunc (f *Form) Checkbox(name, value string, attributes ...string) *Form {\n\treturn f.appendInputType(InputTypeCheckbox, name, value, attributes...)\n}\n\n// Textarea appends a new textarea to form fields.\n// Method panics when attributes are not valid.\nfunc (f *Form) Textarea(name string, attributes ...string) *Form {\n\tname = strings.TrimSpace(name)\n\tif name == \"\" {\n\t\tpanic(\"form textarea name is required\")\n\t}\n\n\tassertEvenAttributes(attributes)\n\n\tattrs := []string{formatAttribute(\"name\", name)}\n\tfor i := 0; i \u003c len(attributes); i += 2 {\n\t\tname, value := attributes[i], attributes[i+1]\n\n\t\tassertIsValidAttribute(name, textareaAttributes)\n\n\t\tattrs = append(attrs, formatAttribute(name, value))\n\t}\n\n\tf.fields = append(f.fields, \"\u003cgno-textarea \"+strings.Join(attrs, \" \")+\" /\u003e\")\n\treturn f\n}\n\n// Select appends a new select to form fields.\n// Method panics when attributes are not valid.\nfunc (f *Form) Select(name, value string, attributes ...string) *Form {\n\tname = strings.TrimSpace(name)\n\tif name == \"\" {\n\t\tpanic(\"form select name is required\")\n\t}\n\n\tassertEvenAttributes(attributes)\n\n\tattrs := []string{\n\t\tformatAttribute(\"name\", name),\n\t\tformatAttribute(\"value\", value),\n\t}\n\n\tfor i := 0; i \u003c len(attributes); i += 2 {\n\t\tname, value := attributes[i], attributes[i+1]\n\n\t\tassertIsValidAttribute(name, selectAttributes)\n\n\t\tattrs = append(attrs, formatAttribute(name, value))\n\t}\n\n\tf.fields = append(f.fields, \"\u003cgno-select \"+strings.Join(attrs, \" \")+\" /\u003e\")\n\treturn f\n}\n\n// String returns the form as Gno-Flavored Markdown.\nfunc (f Form) String() string {\n\tfields := strings.Join(f.fields, \"\\n\")\n\tattrs := strings.Join(f.attrs, \" \")\n\tif len(attrs) \u003e 0 {\n\t\tattrs = \" \" + attrs\n\t}\n\n\treturn \"\u003cgno-form\" + attrs + \"\u003e\\n\" + fields + \"\\n\u003c/gno-form\u003e\\n\"\n}\n\nfunc (f *Form) appendInputType(typeName, name, value string, attributes ...string) *Form {\n\tname = strings.TrimSpace(name)\n\tif name == \"\" {\n\t\tpanic(\"form \" + typeName + \" input name is required\")\n\t}\n\n\tassertEvenAttributes(attributes)\n\n\tattrs := []string{\n\t\tformatAttribute(\"type\", typeName),\n\t\tformatAttribute(\"name\", name),\n\t\tformatAttribute(\"value\", value),\n\t}\n\n\tfor i := 0; i \u003c len(attributes); i += 2 {\n\t\tname, value := attributes[i], attributes[i+1]\n\t\tif name == \"type\" || name == \"value\" {\n\t\t\tcontinue\n\t\t}\n\n\t\tassertIsValidAttribute(name, inputAttributes)\n\n\t\tattrs = append(attrs, formatAttribute(name, value))\n\t}\n\n\tf.fields = append(f.fields, \"\u003cgno-input \"+strings.Join(attrs, \" \")+\" /\u003e\")\n\treturn f\n}\n\nfunc formatAttribute(name, value string) string {\n\treturn name + `=\"` + html.EscapeString(value) + `\"`\n}\n\nfunc assertEvenAttributes(attrs []string) {\n\tif len(attrs)%2 != 0 {\n\t\tpanic(\"expected an even number of attribute arguments\")\n\t}\n}\n\nfunc assertIsValidAttribute(attr string, attrs []string) {\n\tfor _, name := range attrs {\n\t\tif name == attr {\n\t\t\treturn\n\t\t}\n\t}\n\n\tpanic(\"invalid attribute: \" + attr)\n}\n"},{"name":"mdform_filetest.gno","body":"package main\n\nimport \"gno.land/p/jeronimoalbi/mdform\"\n\nfunc main() {\n\tform := mdform.\n\t\tNew(\n\t\t\t\"exec\", \"FunctionName\",\n\t\t).\n\t\tInput(\n\t\t\t\"name\",\n\t\t\t\"placeholder\", \"Name\",\n\t\t\t\"value\", \"John Doe\",\n\t\t).\n\t\tSelect(\n\t\t\t\"country\",\n\t\t\t\"United States\",\n\t\t\t\"description\", \"Select your country\",\n\t\t).\n\t\tSelect(\n\t\t\t\"country\",\n\t\t\t\"Spain\",\n\t\t).\n\t\tSelect(\n\t\t\t\"country\",\n\t\t\t\"Germany\",\n\t\t).\n\t\tCheckbox(\n\t\t\t\"interests\",\n\t\t\t\"music\",\n\t\t\t\"description\", \"What do you like to do?\",\n\t\t).\n\t\tCheckbox(\n\t\t\t\"interests\",\n\t\t\t\"tech\",\n\t\t\t\"checked\", \"true\",\n\t\t)\n\tprintln(form.String())\n}\n\n// Output:\n// \u003cgno-form exec=\"FunctionName\"\u003e\n// \u003cgno-input name=\"name\" placeholder=\"Name\" value=\"John Doe\" /\u003e\n// \u003cgno-select name=\"country\" value=\"United States\" description=\"Select your country\" /\u003e\n// \u003cgno-select name=\"country\" value=\"Spain\" /\u003e\n// \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n// \u003cgno-input type=\"checkbox\" name=\"interests\" value=\"music\" description=\"What do you like to do?\" /\u003e\n// \u003cgno-input type=\"checkbox\" name=\"interests\" value=\"tech\" checked=\"true\" /\u003e\n// \u003c/gno-form\u003e\n"},{"name":"mdform_test.gno","body":"package mdform_test\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/mdform\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestNew(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tattrs    []string\n\t\tmarkdown string\n\t\terr      string\n\t}{\n\t\t{\n\t\t\tname:     \"ok\",\n\t\t\tattrs:    []string{\"exec\", \"FunctionName\"},\n\t\t\tmarkdown: `\u003cgno-form exec=\"FunctionName\"\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:     \"no attributes\",\n\t\t\tmarkdown: `\u003cgno-form\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:  \"uneven attributes\",\n\t\t\tattrs: []string{\"exec\"},\n\t\t\terr:   \"expected an even number of attribute arguments\",\n\t\t},\n\t\t{\n\t\t\tname:  \"invalid attribute\",\n\t\t\tattrs: []string{\"foo\", \"\"},\n\t\t\terr:   \"invalid attribute: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar markdown string\n\t\t\tfn := func() {\n\t\t\t\tf := mdform.New(tc.attrs...)\n\t\t\t\tmarkdown = strings.ReplaceAll(f.String(), \"\\n\", \"\")\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, fn, \"expect form to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotPanics(t, cur, fn, \"expect form to be created\")\n\t\t\tuassert.Equal(t, tc.markdown, markdown, \"expected markdown to match\")\n\t\t})\n\t}\n}\n\nfunc TestFormInput(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinputName string\n\t\tattrs     []string\n\t\tmarkdown  string\n\t\terr       string\n\t}{\n\t\t{\n\t\t\tname:      \"ok\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"value\", \"foo\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input name=\"test\" value=\"foo\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"no attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input name=\"test\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"empty name\",\n\t\t\tinputName: \"  \",\n\t\t\terr:       \"form input name is required\",\n\t\t},\n\t\t{\n\t\t\tname:      \"radio type\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"type\", \"radio\"},\n\t\t\terr:       \"use form.Radio() to create inputs of type radio\",\n\t\t},\n\t\t{\n\t\t\tname:      \"checkbox type\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"type\", \"checkbox\"},\n\t\t\terr:       \"use form.Checkbox() to create inputs of type checkbox\",\n\t\t},\n\t\t{\n\t\t\tname:      \"uneven attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"value\"},\n\t\t\terr:       \"expected an even number of attribute arguments\",\n\t\t},\n\t\t{\n\t\t\tname:      \"invalid attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"foo\", \"\"},\n\t\t\terr:       \"invalid attribute: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar markdown string\n\t\t\tfn := func() {\n\t\t\t\tf := mdform.New()\n\t\t\t\tf.Input(tc.inputName, tc.attrs...)\n\t\t\t\tmarkdown = strings.ReplaceAll(f.String(), \"\\n\", \"\")\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, fn, \"expect form input to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotPanics(t, cur, fn, \"expect form input to be created\")\n\t\t\tuassert.Equal(t, tc.markdown, markdown, \"expected markdown to match\")\n\t\t})\n\t}\n}\n\nfunc TestFormRadio(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinputName string\n\t\tvalue     string\n\t\tattrs     []string\n\t\tmarkdown  string\n\t\terr       string\n\t}{\n\t\t{\n\t\t\tname:      \"ok\",\n\t\t\tinputName: \"test\",\n\t\t\tvalue:     \"foo\",\n\t\t\tattrs:     []string{\"readonly\", \"true\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"radio\" name=\"test\" value=\"foo\" readonly=\"true\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"no attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tvalue:     \"foo\",\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"radio\" name=\"test\" value=\"foo\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"empty name\",\n\t\t\tinputName: \"  \",\n\t\t\terr:       \"form radio input name is required\",\n\t\t},\n\t\t{\n\t\t\tname:      \"ignore type attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"type\", \"text\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"radio\" name=\"test\" value=\"\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"ignore value attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"value\", \"foo\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"radio\" name=\"test\" value=\"\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"uneven attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"readonly\"},\n\t\t\terr:       \"expected an even number of attribute arguments\",\n\t\t},\n\t\t{\n\t\t\tname:      \"invalid attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"foo\", \"\"},\n\t\t\terr:       \"invalid attribute: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar markdown string\n\t\t\tfn := func() {\n\t\t\t\tf := mdform.New()\n\t\t\t\tf.Radio(tc.inputName, tc.value, tc.attrs...)\n\t\t\t\tmarkdown = strings.ReplaceAll(f.String(), \"\\n\", \"\")\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, fn, \"expect form radio input to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotPanics(t, cur, fn, \"expect form radio input to be created\")\n\t\t\tuassert.Equal(t, tc.markdown, markdown, \"expected markdown to match\")\n\t\t})\n\t}\n}\n\nfunc TestFormCheckbox(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinputName string\n\t\tvalue     string\n\t\tattrs     []string\n\t\tmarkdown  string\n\t\terr       string\n\t}{\n\t\t{\n\t\t\tname:      \"ok\",\n\t\t\tinputName: \"test\",\n\t\t\tvalue:     \"foo\",\n\t\t\tattrs:     []string{\"readonly\", \"true\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"checkbox\" name=\"test\" value=\"foo\" readonly=\"true\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"no attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tvalue:     \"foo\",\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"checkbox\" name=\"test\" value=\"foo\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"empty name\",\n\t\t\tinputName: \"  \",\n\t\t\terr:       \"form checkbox input name is required\",\n\t\t},\n\t\t{\n\t\t\tname:      \"ignore type attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"type\", \"text\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"checkbox\" name=\"test\" value=\"\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"ignore value attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"value\", \"foo\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-input type=\"checkbox\" name=\"test\" value=\"\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"uneven attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"readonly\"},\n\t\t\terr:       \"expected an even number of attribute arguments\",\n\t\t},\n\t\t{\n\t\t\tname:      \"invalid attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"foo\", \"\"},\n\t\t\terr:       \"invalid attribute: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar markdown string\n\t\t\tfn := func() {\n\t\t\t\tf := mdform.New()\n\t\t\t\tf.Checkbox(tc.inputName, tc.value, tc.attrs...)\n\t\t\t\tmarkdown = strings.ReplaceAll(f.String(), \"\\n\", \"\")\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, fn, \"expect form checkbox input to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotPanics(t, cur, fn, \"expect form checkbox input to be created\")\n\t\t\tuassert.Equal(t, tc.markdown, markdown, \"expected markdown to match\")\n\t\t})\n\t}\n}\n\nfunc TestFormTextarea(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinputName string\n\t\tattrs     []string\n\t\tmarkdown  string\n\t\terr       string\n\t}{\n\t\t{\n\t\t\tname:      \"ok\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"value\", \"foo\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-textarea name=\"test\" value=\"foo\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"no attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-textarea name=\"test\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"empty name\",\n\t\t\tinputName: \"  \",\n\t\t\terr:       \"form textarea name is required\",\n\t\t},\n\t\t{\n\t\t\tname:      \"uneven attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"value\"},\n\t\t\terr:       \"expected an even number of attribute arguments\",\n\t\t},\n\t\t{\n\t\t\tname:      \"invalid attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"foo\", \"\"},\n\t\t\terr:       \"invalid attribute: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar markdown string\n\t\t\tfn := func() {\n\t\t\t\tf := mdform.New()\n\t\t\t\tf.Textarea(tc.inputName, tc.attrs...)\n\t\t\t\tmarkdown = strings.ReplaceAll(f.String(), \"\\n\", \"\")\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, fn, \"expect form textarea to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotPanics(t, cur, fn, \"expect form textarea to be created\")\n\t\t\tuassert.Equal(t, tc.markdown, markdown, \"expected markdown to match\")\n\t\t})\n\t}\n}\n\nfunc TestFormSelect(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinputName string\n\t\tvalue     string\n\t\tattrs     []string\n\t\tmarkdown  string\n\t\terr       string\n\t}{\n\t\t{\n\t\t\tname:      \"ok\",\n\t\t\tinputName: \"test\",\n\t\t\tvalue:     \"foo\",\n\t\t\tattrs:     []string{\"readonly\", \"true\"},\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-select name=\"test\" value=\"foo\" readonly=\"true\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"no attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tvalue:     \"foo\",\n\t\t\tmarkdown:  `\u003cgno-form\u003e\u003cgno-select name=\"test\" value=\"foo\" /\u003e\u003c/gno-form\u003e`,\n\t\t},\n\t\t{\n\t\t\tname:      \"empty name\",\n\t\t\tinputName: \"  \",\n\t\t\terr:       \"form select name is required\",\n\t\t},\n\t\t{\n\t\t\tname:      \"uneven attributes\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"value\"},\n\t\t\terr:       \"expected an even number of attribute arguments\",\n\t\t},\n\t\t{\n\t\t\tname:      \"invalid attribute\",\n\t\t\tinputName: \"test\",\n\t\t\tattrs:     []string{\"foo\", \"\"},\n\t\t\terr:       \"invalid attribute: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar markdown string\n\t\t\tfn := func() {\n\t\t\t\tf := mdform.New()\n\t\t\t\tf.Select(tc.inputName, tc.value, tc.attrs...)\n\t\t\t\tmarkdown = strings.ReplaceAll(f.String(), \"\\n\", \"\")\n\t\t\t}\n\n\t\t\tif tc.err != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.err, fn, \"expect form textarea to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotPanics(t, cur, fn, \"expect form textarea to be created\")\n\t\t\tuassert.Equal(t, tc.markdown, markdown, \"expected markdown to match\")\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"gmyAmNR9FIgN5SyF1umOFHxMOU2gMngyQ3+hClvKBHLWIvqI9kVPLW626/1hWqI50YtKDmxdqV1F/9g10+LWCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"pager","path":"gno.land/p/jeronimoalbi/pager","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/pager\"\ngno = \"0.9\"\n"},{"name":"pager.gno","body":"// Package pager provides pagination functionality through a generic pager implementation.\n//\n// Example usage:\n//\n//\timport (\n//\t    \"strconv\"\n//\t    \"strings\"\n//\n//\t    \"gno.land/p/jeronimoalbi/pager\"\n//\t)\n//\n//\tfunc Render(path string) string {\n//\t    // Define the items to paginate\n//\t    items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n//\n//\t    // Create a pager that paginates 4 items at a time\n//\t    p, err := pager.New(path, len(items), pager.WithPageSize(4))\n//\t    if err != nil {\n//\t        panic(err)\n//\t    }\n//\n//\t    // Render items for the current page\n//\t    var output strings.Builder\n//\t    p.Iterate(func(i int) bool {\n//\t        output.WriteString(\"- \" + strconv.Itoa(items[i]) + \"\\n\")\n//\t        return false\n//\t    })\n//\n//\t    // Render page picker\n//\t    if p.HasPages() {\n//\t        output.WriteString(\"\\n\" + pager.Picker(p))\n//\t    }\n//\n//\t    return output.String()\n//\t}\npackage pager\n\nimport (\n\t\"errors\"\n\t\"math\"\n\t\"net/url\"\n\t\"strconv\"\n\t\"strings\"\n)\n\nvar ErrInvalidPageNumber = errors.New(\"invalid page number\")\n\n// PagerIterFn defines a callback to iterate page items.\ntype PagerIterFn func(index int) (stop bool)\n\n// New creates a new pager.\nfunc New(rawURL string, totalItems int, options ...PagerOption) (Pager, error) {\n\tu, err := url.Parse(rawURL)\n\tif err != nil {\n\t\treturn Pager{}, err\n\t}\n\n\tp := Pager{\n\t\tquery:          u.RawQuery,\n\t\tpageQueryParam: DefaultPageQueryParam,\n\t\tpageSize:       DefaultPageSize,\n\t\tpage:           1,\n\t\ttotalItems:     totalItems,\n\t}\n\tfor _, apply := range options {\n\t\tapply(\u0026p)\n\t}\n\n\tp.pageCount = int(math.Ceil(float64(p.totalItems) / float64(p.pageSize)))\n\n\trawPage := u.Query().Get(p.pageQueryParam)\n\tif rawPage != \"\" {\n\t\tp.page, _ = strconv.Atoi(rawPage)\n\t\tif p.page == 0 || p.page \u003e p.pageCount {\n\t\t\treturn Pager{}, ErrInvalidPageNumber\n\t\t}\n\t}\n\n\treturn p, nil\n}\n\n// MustNew creates a new pager or panics if there is an error.\nfunc MustNew(rawURL string, totalItems int, options ...PagerOption) Pager {\n\tp, err := New(rawURL, totalItems, options...)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn p\n}\n\n// Pager allows paging items.\ntype Pager struct {\n\tquery, pageQueryParam                 string\n\tpageSize, page, pageCount, totalItems int\n}\n\n// TotalItems returns the total number of items to paginate.\nfunc (p Pager) TotalItems() int {\n\treturn p.totalItems\n}\n\n// PageSize returns the size of each page.\nfunc (p Pager) PageSize() int {\n\treturn p.pageSize\n}\n\n// Page returns the current page number.\nfunc (p Pager) Page() int {\n\treturn p.page\n}\n\n// PageCount returns the number pages.\nfunc (p Pager) PageCount() int {\n\treturn p.pageCount\n}\n\n// Offset returns the index of the first page item.\nfunc (p Pager) Offset() int {\n\treturn (p.page - 1) * p.pageSize\n}\n\n// HasPages checks if pager has more than one page.\nfunc (p Pager) HasPages() bool {\n\treturn p.pageCount \u003e 1\n}\n\n// GetPageURI returns the URI for a page.\n// An empty string is returned when page doesn't exist.\nfunc (p Pager) GetPageURI(page int) string {\n\tif page \u003c 1 || page \u003e p.PageCount() {\n\t\treturn \"\"\n\t}\n\n\tvalues, _ := url.ParseQuery(p.query)\n\tvalues.Set(p.pageQueryParam, strconv.Itoa(page))\n\treturn \"?\" + values.Encode()\n}\n\n// PrevPageURI returns the URI path to the previous page.\n// An empty string is returned when current page is the first page.\nfunc (p Pager) PrevPageURI() string {\n\tif p.page == 1 || !p.HasPages() {\n\t\treturn \"\"\n\t}\n\treturn p.GetPageURI(p.page - 1)\n}\n\n// NextPageURI returns the URI path to the next page.\n// An empty string is returned when current page is the last page.\nfunc (p Pager) NextPageURI() string {\n\tif p.page == p.pageCount {\n\t\t// Current page is the last page\n\t\treturn \"\"\n\t}\n\treturn p.GetPageURI(p.page + 1)\n}\n\n// Iterate allows iterating page items.\nfunc (p Pager) Iterate(fn PagerIterFn) bool {\n\tif p.totalItems == 0 {\n\t\treturn true\n\t}\n\n\tstart := p.Offset()\n\tend := start + p.PageSize()\n\tif end \u003e p.totalItems {\n\t\tend = p.totalItems\n\t}\n\n\tfor i := start; i \u003c end; i++ {\n\t\tif fn(i) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// TODO: Support different types of pickers (ex. with clickable page numbers)\n\n// Picker returns a string with the pager as Markdown.\n// An empty string is returned when the pager has no pages.\nfunc Picker(p Pager) string {\n\tif !p.HasPages() {\n\t\treturn \"\"\n\t}\n\n\tvar out strings.Builder\n\n\tif s := p.PrevPageURI(); s != \"\" {\n\t\tout.WriteString(\"[«](\" + s + \") | \")\n\t} else {\n\t\tout.WriteString(\"\\\\- | \")\n\t}\n\n\tout.WriteString(\"page \" + strconv.Itoa(p.Page()) + \" of \" + strconv.Itoa(p.PageCount()))\n\n\tif s := p.NextPageURI(); s != \"\" {\n\t\tout.WriteString(\" | [»](\" + s + \")\")\n\t} else {\n\t\tout.WriteString(\" | \\\\-\")\n\t}\n\n\treturn out.String()\n}\n"},{"name":"pager_options.gno","body":"package pager\n\nimport \"strings\"\n\nconst (\n\tDefaultPageSize       = 50\n\tDefaultPageQueryParam = \"page\"\n)\n\n// PagerOption configures the pager.\ntype PagerOption func(*Pager)\n\n// WithPageSize assigns a page size to a pager.\nfunc WithPageSize(size int) PagerOption {\n\treturn func(p *Pager) {\n\t\tif size \u003c 1 {\n\t\t\tp.pageSize = DefaultPageSize\n\t\t} else {\n\t\t\tp.pageSize = size\n\t\t}\n\t}\n}\n\n// WithPageQueryParam assigns the name of the URL query param for the page value.\nfunc WithPageQueryParam(name string) PagerOption {\n\treturn func(p *Pager) {\n\t\tname = strings.TrimSpace(name)\n\t\tif name == \"\" {\n\t\t\tname = DefaultPageQueryParam\n\t\t}\n\t\tp.pageQueryParam = name\n\t}\n}\n"},{"name":"pager_test.gno","body":"package pager\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestPager(t *testing.T) {\n\tcases := []struct {\n\t\tname, uri, prevPath, nextPath, param string\n\t\toffset, pageSize, page, pageCount    int\n\t\thasPages                             bool\n\t\titems                                []int\n\t\terr                                  error\n\t}{\n\t\t{\n\t\t\tname:      \"page 1\",\n\t\t\turi:       \"gno.land/r/demo/test:foo/bar?page=1\u0026foo=bar\",\n\t\t\titems:     []int{1, 2, 3, 4, 5, 6},\n\t\t\thasPages:  true,\n\t\t\tnextPath:  \"?foo=bar\u0026page=2\",\n\t\t\tpageSize:  5,\n\t\t\tpage:      1,\n\t\t\tpageCount: 2,\n\t\t},\n\t\t{\n\t\t\tname:      \"page 2\",\n\t\t\turi:       \"gno.land/r/demo/test:foo/bar?page=2\u0026foo=bar\",\n\t\t\titems:     []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},\n\t\t\thasPages:  true,\n\t\t\tprevPath:  \"?foo=bar\u0026page=1\",\n\t\t\tnextPath:  \"\",\n\t\t\toffset:    5,\n\t\t\tpageSize:  5,\n\t\t\tpage:      2,\n\t\t\tpageCount: 2,\n\t\t},\n\t\t{\n\t\t\tname:      \"custom query param\",\n\t\t\turi:       \"gno.land/r/demo/test:foo/bar?current=2\u0026foo=bar\",\n\t\t\titems:     []int{1, 2, 3},\n\t\t\tparam:     \"current\",\n\t\t\thasPages:  true,\n\t\t\tprevPath:  \"?current=1\u0026foo=bar\",\n\t\t\tnextPath:  \"\",\n\t\t\toffset:    2,\n\t\t\tpageSize:  2,\n\t\t\tpage:      2,\n\t\t\tpageCount: 2,\n\t\t},\n\t\t{\n\t\t\tname: \"missing page\",\n\t\t\turi:  \"gno.land/r/demo/test:foo/bar?page=3\u0026foo=bar\",\n\t\t\terr:  ErrInvalidPageNumber,\n\t\t},\n\t\t{\n\t\t\tname: \"invalid page zero\",\n\t\t\turi:  \"gno.land/r/demo/test:foo/bar?page=0\",\n\t\t\terr:  ErrInvalidPageNumber,\n\t\t},\n\t\t{\n\t\t\tname: \"invalid page number\",\n\t\t\turi:  \"gno.land/r/demo/test:foo/bar?page=foo\",\n\t\t\terr:  ErrInvalidPageNumber,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Act\n\t\t\tp, err := New(tc.uri, len(tc.items), WithPageSize(tc.pageSize), WithPageQueryParam(tc.param))\n\n\t\t\t// Assert\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err, \"expected an error\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\tuassert.Equal(t, len(tc.items), p.TotalItems(), \"total items\")\n\t\t\tuassert.Equal(t, tc.page, p.Page(), \"page number\")\n\t\t\tuassert.Equal(t, tc.pageCount, p.PageCount(), \"number of pages\")\n\t\t\tuassert.Equal(t, tc.pageSize, p.PageSize(), \"page size\")\n\t\t\tuassert.Equal(t, tc.prevPath, p.PrevPageURI(), \"prev URL page\")\n\t\t\tuassert.Equal(t, tc.nextPath, p.NextPageURI(), \"next URL page\")\n\t\t\tuassert.Equal(t, tc.hasPages, p.HasPages(), \"has pages\")\n\t\t\tuassert.Equal(t, tc.offset, p.Offset(), \"item offset\")\n\t\t})\n\t}\n}\n\nfunc TestPagerIterate(t *testing.T) {\n\tcases := []struct {\n\t\tname, uri   string\n\t\titems, page []int\n\t\tstop        bool\n\t}{\n\t\t{\n\t\t\tname:  \"page 1\",\n\t\t\turi:   \"gno.land/r/demo/test:foo/bar?page=1\",\n\t\t\titems: []int{1, 2, 3, 4, 5, 6, 7},\n\t\t\tpage:  []int{1, 2, 3},\n\t\t},\n\t\t{\n\t\t\tname:  \"page 2\",\n\t\t\turi:   \"gno.land/r/demo/test:foo/bar?page=2\",\n\t\t\titems: []int{1, 2, 3, 4, 5, 6, 7},\n\t\t\tpage:  []int{4, 5, 6},\n\t\t},\n\t\t{\n\t\t\tname:  \"page 3\",\n\t\t\turi:   \"gno.land/r/demo/test:foo/bar?page=3\",\n\t\t\titems: []int{1, 2, 3, 4, 5, 6, 7},\n\t\t\tpage:  []int{7},\n\t\t},\n\t\t{\n\t\t\tname:  \"stop iteration\",\n\t\t\turi:   \"gno.land/r/demo/test:foo/bar?page=1\",\n\t\t\titems: []int{1, 2, 3},\n\t\t\tstop:  true,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar (\n\t\t\t\titems []int\n\t\t\t\tp     = MustNew(tc.uri, len(tc.items), WithPageSize(3))\n\t\t\t)\n\n\t\t\t// Act\n\t\t\tstopped := p.Iterate(func(i int) bool {\n\t\t\t\tif tc.stop {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\n\t\t\t\titems = append(items, tc.items[i])\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\t// Assert\n\t\t\tuassert.Equal(t, tc.stop, stopped)\n\t\t\turequire.Equal(t, len(tc.page), len(items), \"expect iteration of the right number of items\")\n\n\t\t\tfor i, v := range items {\n\t\t\t\turequire.Equal(t, tc.page[i], v, \"expect iterated items to match\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestPicker(t *testing.T) {\n\tpageSize := 3\n\tcases := []struct {\n\t\tname, uri, output string\n\t\ttotalItems        int\n\t}{\n\t\t{\n\t\t\tname:       \"one page\",\n\t\t\turi:        \"gno.land/r/demo/test:foo/bar?page=1\",\n\t\t\ttotalItems: 3,\n\t\t\toutput:     \"\",\n\t\t},\n\t\t{\n\t\t\tname:       \"two pages\",\n\t\t\turi:        \"gno.land/r/demo/test:foo/bar?page=1\",\n\t\t\ttotalItems: 4,\n\t\t\toutput:     \"\\\\- | page 1 of 2 | [»](?page=2)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"three pages\",\n\t\t\turi:        \"gno.land/r/demo/test:foo/bar?page=1\",\n\t\t\ttotalItems: 7,\n\t\t\toutput:     \"\\\\- | page 1 of 3 | [»](?page=2)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"three pages second page\",\n\t\t\turi:        \"gno.land/r/demo/test:foo/bar?page=2\",\n\t\t\ttotalItems: 7,\n\t\t\toutput:     \"[«](?page=1) | page 2 of 3 | [»](?page=3)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"three pages third page\",\n\t\t\turi:        \"gno.land/r/demo/test:foo/bar?page=3\",\n\t\t\ttotalItems: 7,\n\t\t\toutput:     \"[«](?page=2) | page 3 of 3 | \\\\-\",\n\t\t},\n\t}\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tp := MustNew(tc.uri, tc.totalItems, WithPageSize(pageSize))\n\n\t\t\t// Act\n\t\t\toutput := Picker(p)\n\n\t\t\t// Assert\n\t\t\tuassert.Equal(t, tc.output, output)\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"YvsSvH0IjU17YWzKw4OxQ6a+lPwNyVscu6JTv8WZrPtgiNvmdiSNWG2kkkvfSO8Si5hACdnZzjQpW0gHcl89Bg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"coinsort","path":"gno.land/p/leon/coinsort","files":[{"name":"coinsort.gno","body":"// Package coinsort provides helpers to sort a slice of banker.Coins using the\n// classic sort.Sort API (without relying on sort.Slice).\n//\n// Usage examples:\n//\n//\tcoins := banker.GetCoins(\"g1....\")\n//\n//\t// Ascending by balance\n//\tcoinsort.SortByBalance(coins)\n//\n//\t// Custom order – largest balance first\n//\tcoinsort.SortBy(coins, func(a, b chain.Coin) bool {\n//\t    return a.Amount \u003e b.Amount // descending\n//\t})\n//\n// Note: when getting banker.Coins from the banker, it's sorted by denom by default.\npackage coinsort\n\nimport (\n\t\"chain\"\n\t\"sort\"\n)\n\ntype ByAmount struct{ chain.Coins }\n\nfunc (b ByAmount) Len() int           { return len(b.Coins) }\nfunc (b ByAmount) Swap(i, j int)      { b.Coins[i], b.Coins[j] = b.Coins[j], b.Coins[i] }\nfunc (b ByAmount) Less(i, j int) bool { return b.Coins[i].Amount \u003c b.Coins[j].Amount }\n\n// SortByBalance sorts c in ascending order by Amount.\n//\n//\tcoinsort.SortByBalance(myCoins)\nfunc SortByBalance(c chain.Coins) {\n\tsort.Sort(ByAmount{c})\n}\n\n// LessFunc defines the comparison function for SortBy. It must return true if\n// 'a' should come before 'b'.\n\ntype LessFunc func(a, b chain.Coin) bool\n\n// customSorter adapts a LessFunc to sort.Interface so we can keep using\n// sort.Sort (rather than sort.Slice).\n\ntype customSorter struct {\n\tcoins chain.Coins\n\tless  LessFunc\n}\n\nfunc (cs customSorter) Len() int      { return len(cs.coins) }\nfunc (cs customSorter) Swap(i, j int) { cs.coins[i], cs.coins[j] = cs.coins[j], cs.coins[i] }\nfunc (cs customSorter) Less(i, j int) bool {\n\treturn cs.less(cs.coins[i], cs.coins[j])\n}\n\n// SortBy sorts c in place using the provided LessFunc.\n//\n// Example – descending by Amount:\n//\n//\tcoinsort.SortBy(coins, func(a, b banker.Coin) bool {\n//\t    return a.Amount \u003e b.Amount\n//\t})\nfunc SortBy(c chain.Coins, less LessFunc) {\n\tif less == nil {\n\t\treturn // nothing to do; keep original order\n\t}\n\tsort.Sort(customSorter{coins: c, less: less})\n}\n"},{"name":"coinsort_test.gno","body":"package coinsort\n\nimport (\n\t\"chain\"\n\t\"testing\"\n)\n\nfunc TestSortByBalance(t *testing.T) {\n\tcoins := chain.Coins{\n\t\tchain.Coin{Denom: \"b\", Amount: 50},\n\t\tchain.Coin{Denom: \"c\", Amount: 10},\n\t\tchain.Coin{Denom: \"a\", Amount: 100},\n\t}\n\n\texpected := chain.Coins{\n\t\tchain.Coin{Denom: \"c\", Amount: 10},\n\t\tchain.Coin{Denom: \"b\", Amount: 50},\n\t\tchain.Coin{Denom: \"a\", Amount: 100},\n\t}\n\n\tSortByBalance(coins)\n\n\tfor i := range coins {\n\t\tif coins[i] != expected[i] {\n\t\t\tt.Errorf(\"SortByBalance failed at index %d: got %+v, want %+v\", i, coins[i], expected[i])\n\t\t}\n\t}\n}\n\nfunc TestSortByCustomDescendingAmount(t *testing.T) {\n\tcoins := chain.Coins{\n\t\tchain.Coin{Denom: \"a\", Amount: 2},\n\t\tchain.Coin{Denom: \"b\", Amount: 3},\n\t\tchain.Coin{Denom: \"c\", Amount: 1},\n\t}\n\n\texpected := chain.Coins{\n\t\tchain.Coin{Denom: \"b\", Amount: 3},\n\t\tchain.Coin{Denom: \"a\", Amount: 2},\n\t\tchain.Coin{Denom: \"c\", Amount: 1},\n\t}\n\n\tSortBy(coins, func(a, b chain.Coin) bool {\n\t\treturn a.Amount \u003e b.Amount // descending\n\t})\n\n\tfor i := range coins {\n\t\tif coins[i] != expected[i] {\n\t\t\tt.Errorf(\"SortBy custom descending failed at index %d: got %+v, want %+v\", i, coins[i], expected[i])\n\t\t}\n\t}\n}\n\nfunc TestSortByNilFunc(t *testing.T) {\n\tcoins := chain.Coins{\n\t\tchain.Coin{Denom: \"x\", Amount: 5},\n\t\tchain.Coin{Denom: \"z\", Amount: 20},\n\t\tchain.Coin{Denom: \"y\", Amount: 10},\n\t}\n\n\texpected := chain.Coins{\n\t\tchain.Coin{Denom: \"x\", Amount: 5},\n\t\tchain.Coin{Denom: \"z\", Amount: 20},\n\t\tchain.Coin{Denom: \"y\", Amount: 10},\n\t}\n\n\tSortBy(coins, nil)\n\n\t// should stay the same\n\tfor i := range coins {\n\t\tif coins[i] != expected[i] {\n\t\t\tt.Errorf(\"SortBy nil func failed at index %d: got %+v, want %+v\", i, coins[i], expected[i])\n\t\t}\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/leon/coinsort\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"gwF/kOodgFUEIJVaON3UqwGT0VRBuvjs1Jrc9gE3DwLtcYtg7q3HFkFK96kW+CeWcLVd2Lr9Xy+OTxshg5HXAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"ctg","path":"gno.land/p/leon/ctg","files":[{"name":"converter.gno","body":"// Package ctg is a simple utility package with helpers\n// for bech32 address conversions.\npackage ctg\n\nimport (\n\t\"crypto/bech32\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// ConvertCosmosToGno takes a Bech32 Cosmos address (prefix \"cosmos\")\n// and returns the same address re-encoded with the gno.land prefix \"g\".\nfunc ConvertCosmosToGno(addr string) (address, error) {\n\tprefix, decoded, err := bech32.Decode(addr)\n\tif err != nil {\n\t\treturn \"\", ufmt.Errorf(\"bech32 decode failed: %v\", err)\n\t}\n\n\tif prefix != \"cosmos\" {\n\t\treturn \"\", ufmt.Errorf(\"expected a cosmos address, got prefix %q\", prefix)\n\t}\n\n\treturn address(mustEncode(\"g\", decoded)), nil\n}\n\nfunc mustEncode(hrp string, data []byte) string {\n\tenc, err := bech32.Encode(hrp, data)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn enc\n}\n\n// ConvertAnyToGno converts *any* valid Bech32 address to its gno.land form\n// by preserving the underlying payload but replacing the prefix with \"g\".\n// No prefix check is performed; invalid Bech32 input still returns an error.\nfunc ConvertAnyToGno(addr string) (address, error) {\n\t_, decoded, err := bech32.Decode(addr)\n\tif err != nil {\n\t\treturn \"\", ufmt.Errorf(\"bech32 decode failed: %v\", err)\n\t}\n\treturn address(mustEncode(\"g\", decoded)), nil\n}\n\n// ConvertGnoToAny converts a gno.land address (prefixed with \"g\") to another Bech32\n// prefix given by prefix. The function ensures the source address really\n// is a gno.land address before proceeding.\n//\n// Example:\n//\n//\tcosmosAddr, _ := ConvertGnoToAny(\"cosmos\", \"g1k98jx9...\")\n//\tfmt.Println(cosmosAddr) // → cosmos1....\nfunc ConvertGnoToAny(prefix string, addr address) (string, error) {\n\torigPrefix, decoded, err := bech32.Decode(string(addr))\n\tif err != nil {\n\t\treturn \"\", ufmt.Errorf(\"bech32 decode failed: %v\", err)\n\t}\n\tif origPrefix != \"g\" {\n\t\treturn \"\", ufmt.Errorf(\"expected a gno address but got prefix %q\", origPrefix)\n\t}\n\treturn mustEncode(prefix, decoded), nil\n}\n"},{"name":"converter_test.gno","body":"package ctg\n\nimport (\n\t\"testing\"\n)\n\nfunc TestConvertKnownAddress(t *testing.T) {\n\tconst (\n\t\tcosmosAddr = \"cosmos1jg8mtutu9khhfwc4nxmuhcpftf0pajdh6svrgs\"\n\t\tgnoAddr    = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\t)\n\tgot, err := ConvertCosmosToGno(cosmosAddr)\n\tif err != nil {\n\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t}\n\tif got != gnoAddr {\n\t\tt.Fatalf(\"got %s, want %s\", got, gnoAddr)\n\t}\n}\n\nfunc TestConvertCosmosToGno(t *testing.T) {\n\tdecoded := []byte{\n\t\t0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,\n\t\t0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,\n\t\t0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,\n\t\t0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,\n\t}\n\n\tcosmosAddr := mustEncode(\"cosmos\", decoded)\n\twantGno := mustEncode(\"g\", decoded)\n\n\tgot, err := ConvertCosmosToGno(cosmosAddr)\n\tif err != nil {\n\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t}\n\tif string(got) != wantGno {\n\t\tt.Fatalf(\"got %s, want %s\", got, wantGno)\n\t}\n\n\t// invalid bech32\n\tif _, err := ConvertCosmosToGno(\"not-bech32\"); err == nil {\n\t\tt.Fatalf(\"expected error for invalid bech32\")\n\t}\n\n\t// wrong prefix\n\tgAddr := mustEncode(\"g\", decoded)\n\tif _, err := ConvertCosmosToGno(gAddr); err == nil {\n\t\tt.Fatalf(\"expected error for non-cosmos prefix\")\n\t}\n}\n\nfunc TestConvertAnyToGno(t *testing.T) {\n\tpayload := []byte{\n\t\t0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,\n\t\t0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,\n\t\t0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,\n\t\t0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,\n\t}\n\n\ttests := []struct {\n\t\tname    string\n\t\tinput   string\n\t\twant    string\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname:  \"cosmos→g\",\n\t\t\tinput: mustEncode(\"cosmos\", payload),\n\t\t\twant:  mustEncode(\"g\", payload),\n\t\t},\n\t\t{\n\t\t\tname:  \"osmo→g\",\n\t\t\tinput: mustEncode(\"osmo\", payload),\n\t\t\twant:  mustEncode(\"g\", payload),\n\t\t},\n\t\t{\n\t\t\tname:    \"invalid bech32\",\n\t\t\tinput:   \"xyz123\",\n\t\t\twantErr: true,\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tgot, err := ConvertAnyToGno(tc.input)\n\t\t\tif tc.wantErr {\n\t\t\t\tif err == nil {\n\t\t\t\t\tt.Fatalf(\"expected error, got nil\")\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif err != nil {\n\t\t\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t\t\t}\n\t\t\tif string(got) != tc.want {\n\t\t\t\tt.Fatalf(\"got %s, want %s\", got, tc.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestConvertGnoToAny(t *testing.T) {\n\tpayload := []byte{\n\t\t0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,\n\t\t0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,\n\t\t0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,\n\t\t0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,\n\t}\n\n\tgno := address(mustEncode(\"g\", payload))\n\n\tt.Run(\"g→cosmos\", func(t *testing.T) {\n\t\tgot, err := ConvertGnoToAny(\"cosmos\", gno)\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t\t}\n\t\tif got != mustEncode(\"cosmos\", payload) {\n\t\t\tt.Fatalf(\"conversion incorrect: %s\", got)\n\t\t}\n\t})\n\n\tt.Run(\"g→foobar\", func(t *testing.T) {\n\t\tgot, err := ConvertGnoToAny(\"foobar\", gno)\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t\t}\n\t\tif got != mustEncode(\"foobar\", payload) {\n\t\t\tt.Fatalf(\"conversion incorrect: %s\", got)\n\t\t}\n\t})\n\n\tt.Run(\"g→osmo\", func(t *testing.T) {\n\t\tgot, err := ConvertGnoToAny(\"osmo\", gno)\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t\t}\n\t\tif got != mustEncode(\"osmo\", payload) {\n\t\t\tt.Fatalf(\"conversion incorrect: %s\", got)\n\t\t}\n\t})\n\n\tt.Run(\"wrong source prefix\", func(t *testing.T) {\n\t\tcosmos := mustEncode(\"cosmos\", payload)\n\t\tif _, err := ConvertGnoToAny(\"g\", address(cosmos)); err == nil {\n\t\t\tt.Fatalf(\"expected error for non-g source prefix\")\n\t\t}\n\t})\n\n\tt.Run(\"invalid bech32\", func(t *testing.T) {\n\t\tif _, err := ConvertGnoToAny(\"cosmos\", address(\"nope\")); err == nil {\n\t\t\tt.Fatalf(\"expected error for invalid bech32\")\n\t\t}\n\t})\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/leon/ctg\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"EeoE3BEa+ybMWJoZnBJNDJqgvr2Ls9pTr+Cir9Q6DxS1asUeY92Q7hmBK5YLFfyKaYUAdj4mamBgTAs904GxDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"svgbtn","path":"gno.land/p/leon/svgbtn","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/leon/svgbtn\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"},{"name":"svgbtn.gno","body":"// Package svgbtn provides utilities for generating SVG-styled buttons as Markdown image links.\n//\n// Buttons are rendered as SVG images with customizable size, colors, labels, and links.\n// This package includes preconfigured styles such as Primary, Danger, Success, Small, Wide,\n// Text-like, and Icon buttons, as well as a factory method for dynamic button creation.\n//\n// Example usage:\n//\n//\tfunc Render(_ string) string {\n//\t\tbtn := svgbtn.PrimaryButton(120, 40, \"Click Me\", \"https://example.com\")\n//\t\treturn btn\n//\t}\n//\n// See more examples at gno.land/r/leon:buttons\n//\n// All buttons are returned as Markdown-compatible strings: [svg_data](link).\npackage svgbtn\n\nimport (\n\t\"gno.land/p/demo/svg\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Button creates a base SVG button with given size, colors, label, and link.\n// - `width`, `height`: size in pixels\n// - `btnColor`: background color (e.g. \"#007BFF\")\n// - `textColor`: label color (e.g. \"#FFFFFF\")\n// - `text`: visible button label\n// - `link`: URL to wrap the image in markdown-style [svg](link)\nfunc Button(width, height int, btnColor, textColor, text, link string) string {\n\treturn ButtonWithRadius(width, height, height/5, btnColor, textColor, text, link)\n}\n\n// ButtonWithRadius creates a base SVG button with custom border radius.\n// - `width`, `height`: size in pixels\n// - `radius`: border radius in pixels\n// - `btnColor`: background color (e.g. \"#007BFF\")\n// - `textColor`: label color (e.g. \"#FFFFFF\")\n// - `text`: visible button label\n// - `link`: URL to wrap the image in markdown-style [svg](link)\nfunc ButtonWithRadius(width, height, radius int, btnColor, textColor, text, link string) string {\n\tcanvas := svg.NewCanvas(width, height).\n\t\tWithViewBox(0, 0, width, height).\n\t\tAddStyle(\"text\", \"font-family:sans-serif;font-size:14px;text-anchor:middle;dominant-baseline:middle;\")\n\n\tbg := svg.NewRectangle(0, 0, width, height, btnColor)\n\tbg.RX = radius\n\tbg.RY = radius\n\n\tlabel := svg.NewText(width/2, height/2, text, textColor)\n\n\tcanvas.Append(bg, label)\n\n\treturn ufmt.Sprintf(\"[%s](%s)\", canvas.Render(text), link)\n}\n\n// PrimaryButton renders a blue button with white text.\nfunc PrimaryButton(width, height int, text, link string) string {\n\treturn Button(width, height, \"#007BFF\", \"#ffffff\", text, link)\n}\n\n// DangerButton renders a red button with white text.\nfunc DangerButton(width, height int, text, link string) string {\n\treturn Button(width, height, \"#DC3545\", \"#ffffff\", text, link)\n}\n\n// SuccessButton renders a green button with white text.\nfunc SuccessButton(width, height int, text, link string) string {\n\treturn Button(width, height, \"#28A745\", \"#ffffff\", text, link)\n}\n\n// SmallButton renders a compact gray button with white text.\nfunc SmallButton(width, height int, text, link string) string {\n\treturn Button(width, height, \"#6C757D\", \"#ffffff\", text, link)\n}\n\n// WideButton renders a wider cyan button with white text.\nfunc WideButton(width, height int, text, link string) string {\n\treturn Button(width, height, \"#17A2B8\", \"#ffffff\", text, link)\n}\n\n// TextButton renders a white button with colored text, like a hyperlink.\nfunc TextButton(width, height int, text, link string) string {\n\treturn Button(width, height, \"#ffffff\", \"#007BFF\", text, link)\n}\n\n// IconButton renders a square button with an icon character (e.g. emoji).\nfunc IconButton(width, height int, icon, link string) string {\n\treturn Button(width, height, \"#E0E0E0\", \"#000000\", icon, link)\n}\n\n// ButtonFactory provides a named-style constructor for buttons.\n// Supported kinds: \"primary\", \"danger\", \"success\", \"small\", \"wide\", \"text\", \"icon\".\nfunc ButtonFactory(kind string, width, height int, text, link string) string {\n\tswitch kind {\n\tcase \"primary\":\n\t\treturn PrimaryButton(width, height, text, link)\n\tcase \"danger\":\n\t\treturn DangerButton(width, height, text, link)\n\tcase \"success\":\n\t\treturn SuccessButton(width, height, text, link)\n\tcase \"small\":\n\t\treturn SmallButton(width, height, text, link)\n\tcase \"wide\":\n\t\treturn WideButton(width, height, text, link)\n\tcase \"text\":\n\t\treturn TextButton(width, height, text, link)\n\tcase \"icon\":\n\t\treturn IconButton(width, height, text, link)\n\tdefault:\n\t\treturn PrimaryButton(width, height, text, link)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"zRbK0JqgrAlW/6bHLRNutJh75w8UtNKKiNjTM3X1mQE5t3z7qwZoEWBEHzbxww5I3k0bbN+RCmTcHZTc33cRCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"md","path":"gno.land/p/mason/md","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/mason/md\"\ngno = \"0.9\"\n"},{"name":"md.gno","body":"package md\n\nimport (\n\t\"strings\"\n)\n\ntype MD struct {\n\telements []string\n}\n\nfunc New() *MD {\n\treturn \u0026MD{elements: []string{}}\n}\n\nfunc (m *MD) H1(text string) {\n\tm.elements = append(m.elements, \"# \"+text)\n}\n\nfunc (m *MD) H3(text string) {\n\tm.elements = append(m.elements, \"### \"+text)\n}\n\nfunc (m *MD) P(text string) {\n\tm.elements = append(m.elements, text)\n}\n\nfunc (m *MD) Code(text string) {\n\tm.elements = append(m.elements, \"  ```\\n\"+text+\"\\n```\\n\")\n}\n\nfunc (m *MD) Im(path string, caption string) {\n\tm.elements = append(m.elements, \"![\"+caption+\"](\"+path+\" \\\"\"+caption+\"\\\")\")\n}\n\nfunc (m *MD) Bullet(point string) {\n\tm.elements = append(m.elements, \"- \"+point)\n}\n\nfunc Link(text, url string, title ...string) string {\n\tif len(title) \u003e 0 \u0026\u0026 title[0] != \"\" {\n\t\treturn \"[\" + text + \"](\" + url + \" \\\"\" + title[0] + \"\\\")\"\n\t}\n\treturn \"[\" + text + \"](\" + url + \")\"\n}\n\nfunc (m *MD) Render() string {\n\treturn strings.Join(m.elements, \"\\n\\n\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"CTOz8JRnK2btQx4UFl4QNt2cTSfTMBRS1liT4FxZgj7jPg4mMddDjJsq1casBBo120OTr75+x81hbR3rCWPGBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"once","path":"gno.land/p/moul/once","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/once\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"once.gno","body":"// Package once provides utilities for one-time execution patterns.\n// It extends the concept of sync.Once with error handling and panic options.\npackage once\n\nimport (\n\t\"errors\"\n)\n\n// Once represents a one-time execution guard\ntype Once struct {\n\tdone    bool\n\terr     error\n\tpaniced bool\n\tvalue   any // stores the result of the execution\n}\n\n// New creates a new Once instance\nfunc New() *Once {\n\treturn \u0026Once{}\n}\n\n// Do executes fn only once and returns nil on subsequent calls\nfunc (o *Once) Do(fn func()) {\n\tif o.done {\n\t\treturn\n\t}\n\tdefer func() { o.done = true }()\n\tfn()\n}\n\n// DoErr executes fn only once and returns the same error on subsequent calls\nfunc (o *Once) DoErr(fn func() error) error {\n\tif o.done {\n\t\treturn o.err\n\t}\n\tdefer func() { o.done = true }()\n\to.err = fn()\n\treturn o.err\n}\n\n// DoOrPanic executes fn only once and panics on subsequent calls\nfunc (o *Once) DoOrPanic(fn func()) {\n\tif o.done {\n\t\tpanic(\"once: multiple execution attempted\")\n\t}\n\tdefer func() { o.done = true }()\n\tfn()\n}\n\n// DoValue executes fn only once and returns its value, subsequent calls return the cached value\nfunc (o *Once) DoValue(fn func() any) any {\n\tif o.done {\n\t\treturn o.value\n\t}\n\tdefer func() { o.done = true }()\n\to.value = fn()\n\treturn o.value\n}\n\n// DoValueErr executes fn only once and returns its value and error\n// Subsequent calls return the cached value and error\nfunc (o *Once) DoValueErr(fn func() (any, error)) (any, error) {\n\tif o.done {\n\t\treturn o.value, o.err\n\t}\n\tdefer func() { o.done = true }()\n\to.value, o.err = fn()\n\treturn o.value, o.err\n}\n\n// Reset resets the Once instance to its initial state\n// This is mainly useful for testing purposes\nfunc (o *Once) Reset() {\n\to.done = false\n\to.err = nil\n\to.paniced = false\n\to.value = nil\n}\n\n// IsDone returns whether the Once has been executed\nfunc (o *Once) IsDone() bool {\n\treturn o.done\n}\n\n// Error returns the error from the last execution if any\nfunc (o *Once) Error() error {\n\treturn o.err\n}\n\nvar (\n\tErrNotExecuted = errors.New(\"once: not executed yet\")\n)\n\n// Value returns the stored value and an error if not executed yet\nfunc (o *Once) Value() (any, error) {\n\tif !o.done {\n\t\treturn nil, ErrNotExecuted\n\t}\n\treturn o.value, nil\n}\n"},{"name":"once_test.gno","body":"package once\n\nimport (\n\t\"errors\"\n\t\"testing\"\n)\n\nfunc TestOnce_Do(t *testing.T) {\n\tcounter := 0\n\tonce := New()\n\n\tincrement := func() {\n\t\tcounter++\n\t}\n\n\t// First call should execute\n\tonce.Do(increment)\n\tif counter != 1 {\n\t\tt.Errorf(\"expected counter to be 1, got %d\", counter)\n\t}\n\n\t// Second call should not execute\n\tonce.Do(increment)\n\tif counter != 1 {\n\t\tt.Errorf(\"expected counter to still be 1, got %d\", counter)\n\t}\n}\n\nfunc TestOnce_DoErr(t *testing.T) {\n\tonce := New()\n\texpectedErr := errors.New(\"test error\")\n\n\tfn := func() error {\n\t\treturn expectedErr\n\t}\n\n\t// First call should return error\n\tif err := once.DoErr(fn); err != expectedErr {\n\t\tt.Errorf(\"expected error %v, got %v\", expectedErr, err)\n\t}\n\n\t// Second call should return same error\n\tif err := once.DoErr(fn); err != expectedErr {\n\t\tt.Errorf(\"expected error %v, got %v\", expectedErr, err)\n\t}\n}\n\nfunc TestOnce_DoOrPanic(t *testing.T) {\n\tonce := New()\n\texecuted := false\n\n\tfn := func() {\n\t\texecuted = true\n\t}\n\n\t// First call should execute\n\tonce.DoOrPanic(fn)\n\tif !executed {\n\t\tt.Error(\"function should have executed\")\n\t}\n\n\t// Second call should panic\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Error(\"expected panic on second execution\")\n\t\t}\n\t}()\n\tonce.DoOrPanic(fn)\n}\n\nfunc TestOnce_DoValue(t *testing.T) {\n\tonce := New()\n\texpected := \"test value\"\n\tcounter := 0\n\n\tfn := func() any {\n\t\tcounter++\n\t\treturn expected\n\t}\n\n\t// First call should return value\n\tif result := once.DoValue(fn); result != expected {\n\t\tt.Errorf(\"expected %v, got %v\", expected, result)\n\t}\n\n\t// Second call should return cached value\n\tif result := once.DoValue(fn); result != expected {\n\t\tt.Errorf(\"expected %v, got %v\", expected, result)\n\t}\n\n\tif counter != 1 {\n\t\tt.Errorf(\"function should have executed only once, got %d executions\", counter)\n\t}\n}\n\nfunc TestOnce_DoValueErr(t *testing.T) {\n\tonce := New()\n\texpectedVal := \"test value\"\n\texpectedErr := errors.New(\"test error\")\n\tcounter := 0\n\n\tfn := func() (any, error) {\n\t\tcounter++\n\t\treturn expectedVal, expectedErr\n\t}\n\n\t// First call should return value and error\n\tval, err := once.DoValueErr(fn)\n\tif val != expectedVal || err != expectedErr {\n\t\tt.Errorf(\"expected (%v, %v), got (%v, %v)\", expectedVal, expectedErr, val, err)\n\t}\n\n\t// Second call should return cached value and error\n\tval, err = once.DoValueErr(fn)\n\tif val != expectedVal || err != expectedErr {\n\t\tt.Errorf(\"expected (%v, %v), got (%v, %v)\", expectedVal, expectedErr, val, err)\n\t}\n\n\tif counter != 1 {\n\t\tt.Errorf(\"function should have executed only once, got %d executions\", counter)\n\t}\n}\n\nfunc TestOnce_Reset(t *testing.T) {\n\tonce := New()\n\tcounter := 0\n\n\tfn := func() {\n\t\tcounter++\n\t}\n\n\tonce.Do(fn)\n\tif counter != 1 {\n\t\tt.Errorf(\"expected counter to be 1, got %d\", counter)\n\t}\n\n\tonce.Reset()\n\tonce.Do(fn)\n\tif counter != 2 {\n\t\tt.Errorf(\"expected counter to be 2 after reset, got %d\", counter)\n\t}\n}\n\nfunc TestOnce_IsDone(t *testing.T) {\n\tonce := New()\n\n\tif once.IsDone() {\n\t\tt.Error(\"new Once instance should not be done\")\n\t}\n\n\tonce.Do(func() {})\n\n\tif !once.IsDone() {\n\t\tt.Error(\"Once instance should be done after execution\")\n\t}\n}\n\nfunc TestOnce_Error(t *testing.T) {\n\tonce := New()\n\texpectedErr := errors.New(\"test error\")\n\n\tif err := once.Error(); err != nil {\n\t\tt.Errorf(\"expected nil error, got %v\", err)\n\t}\n\n\tonce.DoErr(func() error {\n\t\treturn expectedErr\n\t})\n\n\tif err := once.Error(); err != expectedErr {\n\t\tt.Errorf(\"expected error %v, got %v\", expectedErr, err)\n\t}\n}\n\nfunc TestOnce_Value(t *testing.T) {\n\tonce := New()\n\n\t// Test unexecuted state\n\tval, err := once.Value()\n\tif err != ErrNotExecuted {\n\t\tt.Errorf(\"expected ErrNotExecuted, got %v\", err)\n\t}\n\tif val != nil {\n\t\tt.Errorf(\"expected nil value, got %v\", val)\n\t}\n\n\t// Test after execution\n\texpected := \"test value\"\n\tonce.DoValue(func() any {\n\t\treturn expected\n\t})\n\n\tval, err = once.Value()\n\tif err != nil {\n\t\tt.Errorf(\"expected nil error, got %v\", err)\n\t}\n\tif val != expected {\n\t\tt.Errorf(\"expected value %v, got %v\", expected, val)\n\t}\n}\n\nfunc TestOnce_DoValueErr_Panic_MarkedDone(t *testing.T) {\n\tonce := New()\n\tcount := 0\n\tfn := func() (any, error) {\n\t\tcount++\n\t\tpanic(\"panic\")\n\t}\n\tvar r any\n\tfunc() {\n\t\tdefer func() { r = recover() }()\n\t\tonce.DoValueErr(fn)\n\t}()\n\tif r == nil {\n\t\tt.Error(\"expected panic on first call\")\n\t}\n\tif !once.IsDone() {\n\t\tt.Error(\"expected once to be marked as done after panic\")\n\t}\n\tr = nil\n\tfunc() {\n\t\tdefer func() { r = recover() }()\n\t\tonce.DoValueErr(fn)\n\t}()\n\tif r != nil {\n\t\tt.Error(\"expected no panic on subsequent call\")\n\t}\n\tif count != 1 {\n\t\tt.Errorf(\"expected count to be 1, got %d\", count)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"JmvZMvId8FImofO+XHxeefYWY8UxaDjuqDf4sXEtH5zrfVcHHtiEjzyX5uFDE+EMWN4u3DIz9VnLsItsSKw8BA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"authz","path":"gno.land/p/moul/authz","files":[{"name":"authz.gno","body":"// Package authz provides flexible authorization control for privileged actions.\n//\n// # Authorization Strategies\n//\n// The package supports multiple authorization strategies:\n//   - Member-based: Single user or team of users\n//   - Contract-based: Async authorization (e.g., via DAO)\n//   - Auto-accept: Allow all actions\n//   - Drop: Deny all actions\n//\n// Core Components\n//\n//   - Authority interface: Base interface implemented by all authorities\n//   - Authorizer: Main wrapper object for authority management\n//   - MemberAuthority: Manages authorized addresses\n//   - ContractAuthority: Delegates to another contract\n//   - AutoAcceptAuthority: Accepts all actions\n//   - DroppedAuthority: Denies all actions\n//\n// Quick Start\n//\n//\t// Initialize with contract deployer as authority\n//\tvar member address(...)\n//\tvar auth = authz.NewWithMembers(member)\n//\n//\t// Create functions that require authorization\n//\tfunc UpdateConfig(cur realm, newValue string) error {\n//\t\treturn auth.DoByPrevious(0, cur, \"update_config\", func() error {\n//\t\t\tconfig = newValue\n//\t\t\treturn nil\n//\t\t})\n//\t}\n//\n// See example_test.gno for more usage examples.\npackage authz\n\nimport (\n\t\"chain\"\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/moul/once\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/rotree\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Authorizer is the main wrapper object that handles authority management.\n// It is configured with a replaceable Authority implementation.\ntype Authorizer struct {\n\tauth Authority\n}\n\n// Authority represents an entity that can authorize privileged actions.\n// It is implemented by MemberAuthority, ContractAuthority, AutoAcceptAuthority,\n// and DroppedAuthority.\n//\n// Authority is the canonical safe shape for cross-package authority\n// interfaces: methods are address-typed (no realm/cur crosses the interface\n// boundary), and consumers correctly derive `caller` from\n// `cur.Previous().Address()` under `rlm.IsCurrent()` before invoking\n// Authorize. No cur-leak (class 1) is possible through this interface.\n//\n// However, two RESIDUAL RISKS apply:\n//\n//   - Class-3 impl-substitution: NewWithAuthority and Authorizer.Transfer\n//     accept any Authority impl. A malicious Authority can always-approve\n//     (silent privilege escalation) or always-deny (denial-of-service).\n//     Consumers should pass canonical impls from this package\n//     (MemberAuthority, ContractAuthority, AutoAcceptAuthority,\n//     DroppedAuthority) unless they have explicit reason to register a\n//     foreign impl. We do not expose an IsCanonicalAuthority allowlist\n//     because the package is intentionally extensible — third-party impls\n//     are the design intent.\n//\n//   - Class-4 closed-over-authority: NewContractAuthority and\n//     NewRestrictedContractAuthority capture a caller-supplied\n//     PrivilegedActionHandler closure. The handler runs synchronously\n//     inside Authorize with the consumer's authority. A hostile handler\n//     can swallow actions, log the caller, or execute arbitrary code\n//     under the consumer's frame. Register only trusted handler functions.\n//     See r/gnops/valopers/init.gno for the realistic registration shape.\n//\n// We do NOT seal Authority via an unexported marker method — that pattern\n// is bypassable via embedding in Gno; see\n// p/test/seal/filetests/z_seal_*_filetest.gno for the four bypass tests.\ntype Authority interface {\n\t// Authorize executes a privileged action if the caller is authorized\n\t// Additional args can be provided for context (e.g., for proposal creation)\n\tAuthorize(caller address, title string, action PrivilegedAction, args ...any) error\n\n\t// String returns a human-readable description of the authority\n\tString() string\n}\n\n// PrivilegedAction defines a function that performs a privileged action.\ntype PrivilegedAction func() error\n\n// PrivilegedActionHandler is called by contract-based authorities to handle\n// privileged actions.\ntype PrivilegedActionHandler func(title string, action PrivilegedAction) error\n\n// NewWithMembers creates a new Authorizer whose authority is a\n// MemberAuthority containing the given addresses. Callers express\n// authority intent at the call site:\n//\n//\t// \"auth realm is the authority\"\n//\ta := authz.NewWithMembers(cur.Address())\n//\n//\t// \"previous realm is the authority\" (from a crossing function)\n//\ta := authz.NewWithMembers(cur.Previous().Address())\n//\n//\t// \"EOA caller is the authority\" (from init(cur realm))\n//\tif !cur.Previous().IsUserCall() {\n//\t    panic(\"realm must be initialized by EOA\")\n//\t}\n//\ta := authz.NewWithMembers(cur.Previous().Address())\n//\n// This replaces the previous NewWithCurrent / NewWithPrevious /\n// NewWithOrigin sugar — those baked runtime.{Current,Previous,Origin}\n// reads into the constructor, which (a) prevented use from package-\n// level var initializers, (b) made the EOA-origin check inside\n// NewWithOrigin an indirect address comparison rather than the\n// straightforward IsUserCall predicate, and (c) coupled the\n// constructor to the runtime walks the rest of the migration is\n// moving away from.\nfunc NewWithMembers(addrs ...address) *Authorizer {\n\treturn \u0026Authorizer{\n\t\tauth: NewMemberAuthority(addrs...),\n\t}\n}\n\n// NewWithAuthority creates a new Authorizer with a specific authority.\n//\n// SECURITY: `authority` is an open-interface input — any value satisfying\n// Authority is accepted. A malicious impl can always-approve (privilege\n// escalation) or always-deny (DoS). Prefer canonical impls from this\n// package (NewMemberAuthority, NewContractAuthority, NewAutoAcceptAuthority,\n// NewDroppedAuthority) unless you specifically need a foreign impl.\nfunc NewWithAuthority(authority Authority) *Authorizer {\n\treturn \u0026Authorizer{\n\t\tauth: authority,\n\t}\n}\n\n// Authority returns the auth authority implementation\nfunc (a *Authorizer) Authority() Authority {\n\treturn a.auth\n}\n\n// Transfer changes the auth authority after validation. rlm must be the\n// caller's own captured cur (asserted via rlm.IsCurrent()); the\n// principal is rlm.Previous().Address(). Closes the address-parameter\n// forgery: an external realm cannot supply Owner() as `caller` to\n// bypass the underlying Authority's check.\n//\n// SECURITY (runtime substitution): once the current authority approves a\n// Transfer, the new authority is installed and effective on the next call.\n// If an attacker ever becomes the authority — even briefly — they can\n// install a permanent DroppedAuthority (DoS) or an AutoAcceptAuthority\n// (privilege escalation). Consumers concerned about this should wrap\n// Transfer with a one-shot guard or a quorum/cooldown check.\n//\n// `newAuthority` is also an open-interface input — see NewWithAuthority's\n// Class-3 caveat. Pass canonical impls.\nfunc (a *Authorizer) Transfer(_ int, rlm realm, newAuthority Authority) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized\")\n\t}\n\tcaller := rlm.Previous().Address()\n\treturn a.auth.Authorize(caller, \"transfer_authority\", func() error {\n\t\ta.auth = newAuthority\n\t\treturn nil\n\t})\n}\n\n// DoByCurrent executes a privileged action authorized as `rlm`. `rlm`\n// must be the caller's own live cur (asserted via rlm.IsCurrent());\n// the authorized principal is `rlm.Address()`. To authorize as the\n// realm that called your function, use `DoByPrevious`.\n//\n//\tauth.DoByCurrent(0, cur, \"update_config\", func() error { ... })    // current realm authorizes\n//\tauth.DoByPrevious(0, cur, \"update_config\", func() error { ... })   // calling realm authorizes\n//\n// The `_ int` first parameter is a deliberate sentinel that pushes\n// `rlm realm` past the first-arg position so DoByCurrent stays a\n// non-crossing method — otherwise it would be a crossing method and\n// rlm.Previous() inside would resolve one realm deeper than the caller\n// intended.\n//\n// SECURITY: the IsCurrent guard closes Class-2 designation forgery (see\n// docs/resources/gno-security.md). A realm value's .Address() is set\n// when the value is minted at a crossing frame; the value can in\n// principle be stored and replayed. Without IsCurrent, a hostile realm\n// could capture a high-privilege realm's cur.Previous() (e.g., when\n// that realm called into it) and later pass the stored value here to\n// authorize actions as that realm. IsCurrent rejects stale captures by\n// requiring the value to match the topmost live crossing frame's cur.\nfunc (a *Authorizer) DoByCurrent(_ int, rlm realm, title string, action PrivilegedAction, args ...any) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized\")\n\t}\n\treturn a.auth.Authorize(rlm.Address(), title, action, args...)\n}\n\n// DoByPrevious executes a privileged action authorized as the realm\n// that called the function invoking DoByPrevious. `rlm` must be the\n// caller's own live cur; the principal is derived as\n// `rlm.Previous().Address()`. Mirrors the Transfer/AddMember pattern:\n// always take live cur, derive the caller-of-caller internally rather\n// than accepting a stored/forwarded realm value.\nfunc (a *Authorizer) DoByPrevious(_ int, rlm realm, title string, action PrivilegedAction, args ...any) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized\")\n\t}\n\treturn a.auth.Authorize(rlm.Previous().Address(), title, action, args...)\n}\n\n// String returns a string representation of the auth authority\nfunc (a *Authorizer) String() string {\n\tauthStr := a.auth.String()\n\n\tswitch a.auth.(type) {\n\tcase *MemberAuthority:\n\tcase *ContractAuthority:\n\tcase *AutoAcceptAuthority:\n\tcase *droppedAuthority:\n\tdefault:\n\t\t// this way official \"dropped\" is different from \"*custom*: dropped\" (autoclaimed).\n\t\treturn ufmt.Sprintf(\"custom_authority[%s]\", authStr)\n\t}\n\treturn authStr\n}\n\n// MemberAuthority is the default implementation using addrset for member\n// management.\ntype MemberAuthority struct {\n\tmembers addrset.Set\n}\n\nfunc NewMemberAuthority(members ...address) *MemberAuthority {\n\tauth := \u0026MemberAuthority{}\n\tfor _, addr := range members {\n\t\tauth.members.Add(addr)\n\t}\n\treturn auth\n}\n\nfunc (a *MemberAuthority) Authorize(caller address, title string, action PrivilegedAction, args ...any) error {\n\tif !a.members.Has(caller) {\n\t\treturn errors.New(\"unauthorized\")\n\t}\n\n\tif err := action(); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc (a *MemberAuthority) String() string {\n\taddrs := []string{}\n\ta.members.Tree().Iterate(\"\", \"\", func(key string, _ any) bool {\n\t\taddrs = append(addrs, key)\n\t\treturn false\n\t})\n\taddrsStr := strings.Join(addrs, \",\")\n\treturn ufmt.Sprintf(\"member_authority[%s]\", addrsStr)\n}\n\n// AddMember adds a new member to the authority. rlm must be the caller's\n// own captured cur; the principal is rlm.Previous().Address() and must\n// already be a member. The IsCurrent guard closes the forgery where an\n// external realm passes Owner() as caller to bypass members.Has(caller).\nfunc (a *MemberAuthority) AddMember(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized\")\n\t}\n\tcaller := rlm.Previous().Address()\n\treturn a.Authorize(caller, \"add_member\", func() error {\n\t\ta.members.Add(addr)\n\t\treturn nil\n\t})\n}\n\n// AddMembers adds a list of members to the authority. Same rlm contract\n// as AddMember.\nfunc (a *MemberAuthority) AddMembers(_ int, rlm realm, addrs ...address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized\")\n\t}\n\tcaller := rlm.Previous().Address()\n\treturn a.Authorize(caller, \"add_members\", func() error {\n\t\tfor _, addr := range addrs {\n\t\t\ta.members.Add(addr)\n\t\t}\n\t\treturn nil\n\t})\n}\n\n// RemoveMember removes a member from the authority. Same rlm contract\n// as AddMember.\nfunc (a *MemberAuthority) RemoveMember(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized\")\n\t}\n\tcaller := rlm.Previous().Address()\n\treturn a.Authorize(caller, \"remove_member\", func() error {\n\t\ta.members.Remove(addr)\n\t\treturn nil\n\t})\n}\n\n// Tree returns a read-only view of the members tree\nfunc (a *MemberAuthority) Tree() *rotree.ReadOnlyTree {\n\ttree := a.members.Tree().(*avl.Tree)\n\treturn rotree.Wrap(tree, nil)\n}\n\n// Has checks if the given address is a member of the authority\nfunc (a *MemberAuthority) Has(addr address) bool {\n\treturn a.members.Has(addr)\n}\n\n// ContractAuthority implements async contract-based authority\ntype ContractAuthority struct {\n\tcontractPath    string\n\tcontractAddr    address\n\tcontractHandler PrivilegedActionHandler\n\tproposer        Authority // controls who can create proposals\n}\n\n// NewContractAuthority creates a new contract-based authority.\n//\n// SECURITY (Class-4 captured callback): `handler` is a caller-supplied\n// closure that runs SYNCHRONOUSLY inside Authorize, with the consumer's\n// authority. A hostile handler can swallow actions, log the caller, or\n// execute arbitrary code under the consumer's frame. The package-internal\n// wrappedAction guards \"execute action only from contractAddr\" but the\n// handler can call wrappedAction however it likes (multiple times, never,\n// out of order). Register only trusted handler functions; treat handler\n// registration as the trust boundary.\nfunc NewContractAuthority(path string, handler PrivilegedActionHandler) *ContractAuthority {\n\treturn \u0026ContractAuthority{\n\t\tcontractPath:    path,\n\t\tcontractAddr:    chain.PackageAddress(path),\n\t\tcontractHandler: handler,\n\t\tproposer:        NewAutoAcceptAuthority(), // default: anyone can propose\n\t}\n}\n\n// NewRestrictedContractAuthority creates a new contract authority with a\n// proposer restriction.\n//\n// SECURITY:\n//   - `handler` is the same Class-4 captured-callback risk as\n//     NewContractAuthority — runs synchronously inside Authorize with the\n//     consumer's authority. Register only trusted handler functions.\n//   - `proposer` is an open-interface input (Class-3 impl-substitution).\n//     A hostile proposer Authority can always-approve creation of any\n//     proposal, defeating the restriction. Pass canonical impls only.\nfunc NewRestrictedContractAuthority(path string, handler PrivilegedActionHandler, proposer Authority) Authority {\n\tif path == \"\" {\n\t\tpanic(\"contract path cannot be empty\")\n\t}\n\tif handler == nil {\n\t\tpanic(\"contract handler cannot be nil\")\n\t}\n\tif proposer == nil {\n\t\tpanic(\"proposer cannot be nil\")\n\t}\n\treturn \u0026ContractAuthority{\n\t\tcontractPath:    path,\n\t\tcontractAddr:    chain.PackageAddress(path),\n\t\tcontractHandler: handler,\n\t\tproposer:        proposer,\n\t}\n}\n\nfunc (a *ContractAuthority) Authorize(caller address, title string, action PrivilegedAction, args ...any) error {\n\tif a.contractHandler == nil {\n\t\treturn errors.New(\"contract handler is not set\")\n\t}\n\n\t// setup a once instance to ensure the action is executed only once\n\texecutionOnce := once.Once{}\n\n\t// wrappedAction enforces at-most-once invocation. The previous\n\t// gate `unsafe.CurrentRealm() == contractAddr` is removed: it\n\t// was .Title()-bypassable (runtime.CurrentRealm walks past\n\t// non-crossing frames to the most-recent crossing ancestor) and\n\t// the trust boundary is now upstream — Authorizer.DoByCurrent /\n\t// DoByPrevious require rlm.IsCurrent() and pass a non-forgeable\n\t// principal to Authorize, while the consumer realm's handler\n\t// closure is the Class-4 trust root by lexical capture at\n\t// registration time.\n\twrappedAction := func() error {\n\t\treturn executionOnce.DoErr(func() error {\n\t\t\treturn action()\n\t\t})\n\t}\n\n\t// Use the proposer authority to control who can create proposals\n\treturn a.proposer.Authorize(caller, title+\"_proposal\", func() error {\n\t\tif err := a.contractHandler(title, wrappedAction); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}, args...)\n}\n\nfunc (a *ContractAuthority) String() string {\n\treturn ufmt.Sprintf(\"contract_authority[contract=%s]\", a.contractPath)\n}\n\n// AutoAcceptAuthority implements an authority that accepts all actions\n// AutoAcceptAuthority is a simple authority that automatically accepts all\n// actions.\n// It can be used as a proposer authority to allow anyone to create proposals.\ntype AutoAcceptAuthority struct{}\n\nfunc NewAutoAcceptAuthority() *AutoAcceptAuthority {\n\treturn \u0026AutoAcceptAuthority{}\n}\n\nfunc (a *AutoAcceptAuthority) Authorize(caller address, title string, action PrivilegedAction, args ...any) error {\n\treturn action()\n}\n\nfunc (a *AutoAcceptAuthority) String() string {\n\treturn \"auto_accept_authority\"\n}\n\n// droppedAuthority implements an authority that denies all actions\ntype droppedAuthority struct{}\n\nfunc NewDroppedAuthority() Authority {\n\treturn \u0026droppedAuthority{}\n}\n\nfunc (a *droppedAuthority) Authorize(caller address, title string, action PrivilegedAction, args ...any) error {\n\treturn errors.New(\"dropped authority: all actions are denied\")\n}\n\nfunc (a *droppedAuthority) String() string {\n\treturn \"dropped_authority\"\n}\n"},{"name":"authz_test.gno","body":"package authz\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNewWithCurrent(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\tauth := NewWithMembers(cur.Address())\n\n\t// Check that the current authority is a MemberAuthority\n\tmemberAuth, ok := auth.Authority().(*MemberAuthority)\n\tuassert.True(t, ok, \"expected MemberAuthority\")\n\n\t// Check that the caller is a member\n\tuassert.True(t, memberAuth.Has(alice), \"caller should be a member\")\n\n\t// Check string representation\n\tuassert.True(t, strings.Contains(auth.String(), alice.String()))\n}\n\nfunc TestNewWithAuthority(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tmemberAuth := NewMemberAuthority(alice)\n\n\tauth := NewWithAuthority(memberAuth)\n\n\t// Check that the current authority is the one we provided\n\tuassert.True(t, auth.Authority() == memberAuth, \"expected provided authority\")\n}\n\nfunc TestAuthorizerAuthorize(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\tauth := NewWithMembers(cur.Address())\n\n\t// Test successful action with args\n\texecuted := false\n\targs := []any{\"test_arg\", 123}\n\terr := auth.DoByCurrent(0, cur, \"test_action\", func() error {\n\t\texecuted = true\n\t\treturn nil\n\t}, args...)\n\n\tuassert.True(t, err == nil, \"expected no error\")\n\tuassert.True(t, executed, \"action should have been executed\")\n\n\t// Test unauthorized action with args\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"bob\")))\n\n\texecuted = false\n\terr = auth.DoByCurrent(0, cur, \"test_action\", func() error {\n\t\texecuted = true\n\t\treturn nil\n\t}, \"unauthorized_arg\")\n\n\tuassert.True(t, err != nil, \"expected error\")\n\tuassert.False(t, executed, \"action should not have been executed\")\n\n\t// Test action returning error\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\texpectedErr := errors.New(\"test error\")\n\n\terr = auth.DoByCurrent(0, cur, \"test_action\", func() error {\n\t\treturn expectedErr\n\t})\n\n\tuassert.True(t, err == expectedErr, \"expected specific error\")\n}\n\nfunc TestAuthorizerTransfer(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\tauth := NewWithMembers(cur.Address())\n\n\t// Test transfer to new member authority\n\tbob := testutils.TestAddress(\"bob\")\n\tnewAuth := NewMemberAuthority(bob)\n\n\tvar err error\n\tfunc(cur realm) { err = auth.Transfer(0, cur, newAuth) }(cross(cur))\n\tuassert.True(t, err == nil, \"expected no error\")\n\tuassert.True(t, auth.Authority() == newAuth, \"expected new authority\")\n\n\t// Test unauthorized transfer: principal is not a member of newAuth.\n\tcarol := testutils.TestAddress(\"carol\")\n\ttesting.SetRealm(testing.NewUserRealm(carol))\n\n\tfunc(cur realm) { err = auth.Transfer(0, cur, NewMemberAuthority(alice)) }(cross(cur))\n\tuassert.True(t, err != nil, \"expected error\")\n\n\t// Test transfer to contract authority — bob is the current authority.\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tcontractAuth := NewContractAuthority(\"gno.land/r/test\", func(title string, action PrivilegedAction) error {\n\t\treturn action()\n\t})\n\n\tfunc(cur realm) { err = auth.Transfer(0, cur, contractAuth) }(cross(cur))\n\tuassert.True(t, err == nil, \"expected no error\")\n\tuassert.True(t, auth.Authority() == contractAuth, \"expected contract authority\")\n}\n\nfunc TestAuthorizerTransferChain(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\t// Create a chain of transfers\n\tauth := NewWithMembers(cur.Address())\n\n\t// First transfer to a new member authority\n\tbob := testutils.TestAddress(\"bob\")\n\tmemberAuth := NewMemberAuthority(bob)\n\n\tvar err error\n\tfunc(cur realm) { err = auth.Transfer(0, cur, memberAuth) }(cross(cur))\n\tuassert.True(t, err == nil, \"unexpected error in first transfer\")\n\n\t// Then transfer to a contract authority — bob is now the authority.\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tcontractAuth := NewContractAuthority(\"gno.land/r/test\", func(title string, action PrivilegedAction) error {\n\t\treturn action()\n\t})\n\tfunc(cur realm) { err = auth.Transfer(0, cur, contractAuth) }(cross(cur))\n\tuassert.True(t, err == nil, \"unexpected error in second transfer\")\n\n\t// Finally transfer to an auto-accept authority — must come from the\n\t// contract realm so ContractAuthority's wrappedAction CurrentRealm\n\t// check passes. Use the test frame's cur directly (SetRealm mutates\n\t// it in place); a crossing closure would push a new frame whose cur\n\t// is the test package, breaking the runtime.CurrentRealm match.\n\tautoAuth := NewAutoAcceptAuthority()\n\tcodeRealm := testing.NewCodeRealm(\"gno.land/r/test\")\n\ttesting.SetRealm(codeRealm)\n\terr = auth.Transfer(0, cur, autoAuth)\n\tuassert.True(t, err == nil, \"unexpected error in final transfer\")\n\tuassert.True(t, auth.Authority() == autoAuth, \"expected auto-accept authority\")\n}\n\nfunc TestAuthorizerTransferUnauthorizedRejected(cur realm, t *testing.T) {\n\t// Regression for the address-parameter forgery: previously Transfer\n\t// took a caller-supplied `caller address` that an attacker realm could\n\t// set to the real owner. After the IsCurrent + rlm.Previous() fix, the\n\t// principal is the captured cur's previous and cannot be forged.\n\tadmin := testutils.TestAddress(\"admin\")\n\tattacker := testutils.TestAddress(\"attacker\")\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tauth := NewWithMembers(cur.Address()) // admin is the initial authority\n\n\tinitialAuth, ok := auth.Authority().(*MemberAuthority)\n\tuassert.True(t, ok)\n\tuassert.True(t, initialAuth.Has(admin))\n\tuassert.False(t, initialAuth.Has(attacker))\n\n\t// Attacker context: cur.Previous() inside the closure will be attacker.\n\ttesting.SetRealm(testing.NewUserRealm(attacker))\n\tattackerAuth := NewMemberAuthority(attacker)\n\tvar err error\n\tfunc(cur realm) { err = auth.Transfer(0, cur, attackerAuth) }(cross(cur))\n\n\tuassert.True(t, err != nil, \"attacker transfer must be rejected\")\n\n\t// Authority unchanged: still the initial admin-only MemberAuthority.\n\tfinalAuth, ok := auth.Authority().(*MemberAuthority)\n\tuassert.True(t, ok)\n\tuassert.True(t, finalAuth == initialAuth, \"authority must not have changed\")\n\tuassert.True(t, finalAuth.Has(admin))\n\tuassert.False(t, finalAuth.Has(attacker))\n}\n\nfunc TestAuthorizerWithDroppedAuthority(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\tauth := NewWithMembers(cur.Address())\n\n\t// Transfer to dropped authority\n\tvar err error\n\tfunc(cur realm) { err = auth.Transfer(0, cur, NewDroppedAuthority()) }(cross(cur))\n\tuassert.True(t, err == nil, \"expected no error\")\n\n\t// Try to execute action\n\terr = auth.DoByCurrent(0, cur, \"test_action\", func() error {\n\t\treturn nil\n\t})\n\tuassert.True(t, err != nil, \"expected error from dropped authority\")\n\n\t// Try to transfer again\n\tfunc(cur realm) { err = auth.Transfer(0, cur, NewMemberAuthority(alice)) }(cross(cur))\n\tuassert.True(t, err != nil, \"expected error when transferring from dropped authority\")\n}\n\nfunc TestContractAuthorityHandlerExecutionOnce(cur realm, t *testing.T) {\n\tattempts := 0\n\texecuted := 0\n\n\tcontractAuth := NewContractAuthority(\"gno.land/r/test\", func(title string, action PrivilegedAction) error {\n\t\t// Try to execute the action twice in the same handler\n\t\tif err := action(); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tattempts++\n\n\t\t// Second execution should fail\n\t\tif err := action(); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tattempts++\n\t\treturn nil\n\t})\n\n\t// Set caller to contract address\n\tcodeRealm := testing.NewCodeRealm(\"gno.land/r/test\")\n\ttesting.SetRealm(codeRealm)\n\tcode := codeRealm.Address()\n\n\ttestArgs := []any{\"proposal_id\", 42, \"metadata\", map[string]string{\"key\": \"value\"}}\n\terr := contractAuth.Authorize(code, \"test_action\", func() error {\n\t\texecuted++\n\t\treturn nil\n\t}, testArgs...)\n\n\tuassert.True(t, err == nil, \"handler execution should succeed\")\n\tuassert.True(t, attempts == 2, \"handler should have attempted execution twice\")\n\tuassert.True(t, executed == 1, \"handler should have executed once\")\n}\n\nfunc TestContractAuthorityExecutionTwice(cur realm, t *testing.T) {\n\texecuted := 0\n\n\tcontractAuth := NewContractAuthority(\"gno.land/r/test\", func(title string, action PrivilegedAction) error {\n\t\treturn action()\n\t})\n\n\t// Set caller to contract address\n\tcodeRealm := testing.NewCodeRealm(\"gno.land/r/test\")\n\ttesting.SetRealm(codeRealm)\n\tcode := codeRealm.Address()\n\ttestArgs := []any{\"proposal_id\", 42, \"metadata\", map[string]string{\"key\": \"value\"}}\n\n\terr := contractAuth.Authorize(code, \"test_action\", func() error {\n\t\texecuted++\n\t\treturn nil\n\t}, testArgs...)\n\n\tuassert.True(t, err == nil, \"handler execution should succeed\")\n\tuassert.True(t, executed == 1, \"handler should have executed once\")\n\n\t// A new action, even with the same title, should be executed\n\terr = contractAuth.Authorize(code, \"test_action\", func() error {\n\t\texecuted++\n\t\treturn nil\n\t}, testArgs...)\n\n\tuassert.True(t, err == nil, \"handler execution should succeed\")\n\tuassert.True(t, executed == 2, \"handler should have executed twice\")\n}\n\nfunc TestContractAuthorityWithProposer(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tmemberAuth := NewMemberAuthority(alice)\n\n\thandlerCalled := false\n\tactionExecuted := false\n\n\tcontractAuth := NewRestrictedContractAuthority(\"gno.land/r/test\", func(title string, action PrivilegedAction) error {\n\t\thandlerCalled = true\n\t\t// Set caller to contract address before executing action\n\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\t\treturn action()\n\t}, memberAuth)\n\n\t// Test authorized member\n\ttestArgs := []any{\"proposal_metadata\", \"test value\"}\n\terr := contractAuth.Authorize(alice, \"test_action\", func() error {\n\t\tactionExecuted = true\n\t\treturn nil\n\t}, testArgs...)\n\n\tuassert.True(t, err == nil, \"authorized member should be able to propose\")\n\tuassert.True(t, handlerCalled, \"contract handler should be called\")\n\tuassert.True(t, actionExecuted, \"action should be executed\")\n\n\t// Reset flags for unauthorized test\n\thandlerCalled = false\n\tactionExecuted = false\n\n\t// Test unauthorized proposer\n\tbob := testutils.TestAddress(\"bob\")\n\terr = contractAuth.Authorize(bob, \"test_action\", func() error {\n\t\tactionExecuted = true\n\t\treturn nil\n\t}, testArgs...)\n\n\tuassert.True(t, err != nil, \"unauthorized member should not be able to propose\")\n\tuassert.False(t, handlerCalled, \"contract handler should not be called for unauthorized proposer\")\n\tuassert.False(t, actionExecuted, \"action should not be executed for unauthorized proposer\")\n}\n\nfunc TestAutoAcceptAuthority(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tauth := NewAutoAcceptAuthority()\n\n\t// Test that any action is authorized\n\texecuted := false\n\terr := auth.Authorize(alice, \"test_action\", func() error {\n\t\texecuted = true\n\t\treturn nil\n\t})\n\n\tuassert.True(t, err == nil, \"auto-accept should not return error\")\n\tuassert.True(t, executed, \"action should have been executed\")\n\n\t// Test with different caller\n\trandom := testutils.TestAddress(\"random\")\n\texecuted = false\n\terr = auth.Authorize(random, \"test_action\", func() error {\n\t\texecuted = true\n\t\treturn nil\n\t})\n\n\tuassert.True(t, err == nil, \"auto-accept should not care about caller\")\n\tuassert.True(t, executed, \"action should have been executed\")\n}\n\nfunc TestAutoAcceptAuthorityWithArgs(cur realm, t *testing.T) {\n\tauth := NewAutoAcceptAuthority()\n\tanyuser := testutils.TestAddress(\"anyuser\")\n\n\t// Test that any action is authorized with args\n\texecuted := false\n\ttestArgs := []any{\"arg1\", 42, \"arg3\"}\n\terr := auth.Authorize(anyuser, \"test_action\", func() error {\n\t\texecuted = true\n\t\treturn nil\n\t}, testArgs...)\n\n\tuassert.True(t, err == nil, \"auto-accept should not return error\")\n\tuassert.True(t, executed, \"action should have been executed\")\n}\n\nfunc TestMemberAuthorityMultipleMembers(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tbob := testutils.TestAddress(\"bob\")\n\tcarol := testutils.TestAddress(\"carol\")\n\n\t// Create authority with multiple members\n\tauth := NewMemberAuthority(alice, bob)\n\n\t// Test that both members can execute actions\n\tfor _, member := range []address{alice, bob} {\n\t\terr := auth.Authorize(member, \"test_action\", func() error {\n\t\t\treturn nil\n\t\t})\n\t\tuassert.True(t, err == nil, \"member should be authorized\")\n\t}\n\n\t// Test that non-member cannot execute\n\terr := auth.Authorize(carol, \"test_action\", func() error {\n\t\treturn nil\n\t})\n\tuassert.True(t, err != nil, \"non-member should not be authorized\")\n\n\t// Test Tree() functionality\n\ttree := auth.Tree()\n\tuassert.True(t, tree.Size() == 2, \"tree should have 2 members\")\n\n\t// Verify both members are in the tree\n\tfound := make(map[address]bool)\n\ttree.Iterate(\"\", \"\", func(key string, _ any) bool {\n\t\tfound[address(key)] = true\n\t\treturn false\n\t})\n\tuassert.True(t, found[alice], \"alice should be in the tree\")\n\tuassert.True(t, found[bob], \"bob should be in the tree\")\n\tuassert.False(t, found[carol], \"carol should not be in the tree\")\n\n\t// Test read-only nature of the tree\n\tdefer func() {\n\t\tr := recover()\n\t\tuassert.True(t, r != nil, \"modifying read-only tree should panic\")\n\t}()\n\ttree.Set(string(carol), nil) // This should panic\n}\n\nfunc TestAuthorizerCurrentNeverNil(cur realm, t *testing.T) {\n\tauth := NewWithMembers(cur.Address())\n\n\t// Authority should never be nil after initialization\n\tuassert.True(t, auth.Authority() != nil, \"current authority should not be nil\")\n\n\t// Authority should not be nil after transfer\n\tvar err error\n\tfunc(cur realm) { err = auth.Transfer(0, cur, NewAutoAcceptAuthority()) }(cross(cur))\n\tuassert.True(t, err == nil, \"transfer should succeed\")\n\tuassert.True(t, auth.Authority() != nil, \"current authority should not be nil after transfer\")\n}\n\nfunc TestContractAuthorityValidation(cur realm, t *testing.T) {\n\t/*\n\t\t// Test empty path - should panic\n\t\tpanicked := false\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\t\t\tNewContractAuthority(\"\", nil)\n\t\t}()\n\t\tuassert.True(t, panicked, \"expected panic for empty path\")\n\t*/\n\n\t// Test nil handler - should return error on Authorize\n\tauth := NewContractAuthority(\"gno.land/r/test\", nil)\n\tcode := testing.NewCodeRealm(\"gno.land/r/test\").Address()\n\terr := auth.Authorize(code, \"test\", func() error {\n\t\treturn nil\n\t})\n\tuassert.True(t, err != nil, \"nil handler authority should fail to authorize\")\n\n\t// Test valid configuration\n\thandler := func(title string, action PrivilegedAction) error {\n\t\treturn nil\n\t}\n\tcontractAuth := NewContractAuthority(\"gno.land/r/test\", handler)\n\terr = contractAuth.Authorize(code, \"test\", func() error {\n\t\treturn nil\n\t})\n\tuassert.True(t, err == nil, \"valid contract authority should authorize successfully\")\n}\n\nfunc TestAuthorizerString(cur realm, t *testing.T) {\n\tauth := NewWithMembers(cur.Address())\n\taddr := cur.Address()\n\n\t// Test initial string representation\n\tstr := auth.String()\n\tuassert.Equal(t, str, \"member_authority[\"+string(addr)+\"]\")\n\n\t// Test string after transfer — caller is the current member (cur).\n\tautoAuth := NewAutoAcceptAuthority()\n\tvar err error\n\tfunc(cur realm) { err = auth.Transfer(0, cur, autoAuth) }(cross(cur))\n\tuassert.True(t, err == nil, \"transfer should succeed\")\n\tstr = auth.String()\n\tuassert.Equal(t, str, \"auto_accept_authority\")\n\n\t// Test custom authority — auto-accept lets anyone transfer.\n\tcustomAuth := \u0026mockAuthority{}\n\tfunc(cur realm) { err = auth.Transfer(0, cur, customAuth) }(cross(cur))\n\tuassert.True(t, err == nil, \"transfer should succeed\")\n\tstr = auth.String()\n\tuassert.Equal(t, str, \"custom_authority[mock]\")\n}\n\ntype mockAuthority struct{}\n\nfunc (c mockAuthority) String() string { return \"mock\" }\nfunc (a mockAuthority) Authorize(caller address, title string, action PrivilegedAction, args ...any) error {\n\t// autoaccept\n\treturn action()\n}\n\nfunc TestAuthorityString(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\n\t// MemberAuthority\n\tmemberAuth := NewMemberAuthority(alice)\n\tmemberStr := memberAuth.String()\n\texpectedMemberStr := \"member_authority[g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh]\"\n\tuassert.Equal(t, memberStr, expectedMemberStr)\n\n\t// ContractAuthority\n\tcontractAuth := NewContractAuthority(\"gno.land/r/test\", func(title string, action PrivilegedAction) error { return nil })\n\tcontractStr := contractAuth.String()\n\texpectedContractStr := \"contract_authority[contract=gno.land/r/test]\"\n\tuassert.Equal(t, contractStr, expectedContractStr)\n\n\t// AutoAcceptAuthority\n\tautoAuth := NewAutoAcceptAuthority()\n\tautoStr := autoAuth.String()\n\texpectedAutoStr := \"auto_accept_authority\"\n\tuassert.Equal(t, autoStr, expectedAutoStr)\n\n\t// DroppedAuthority\n\tdroppedAuth := NewDroppedAuthority()\n\tdroppedStr := droppedAuth.String()\n\texpectedDroppedStr := \"dropped_authority\"\n\tuassert.Equal(t, droppedStr, expectedDroppedStr)\n}\n\nfunc TestContractAuthorityUnauthorizedCaller(cur realm, t *testing.T) {\n\tcontractPath := \"gno.land/r/testcontract\"\n\tcontractAddr := chain.PackageAddress(contractPath)\n\tunauthorizedAddr := testutils.TestAddress(\"unauthorized\")\n\n\t// Handler that checks the caller before proceeding\n\thandlerExecutedCorrectly := false // Tracks if handler logic ran correctly\n\thandlerErrorMsg := \"handler: caller is not the contract\"\n\tcontractHandler := func(title string, action PrivilegedAction) error {\n\t\tcaller := unsafe.CurrentRealm().Address()\n\t\tif caller != contractAddr {\n\t\t\treturn errors.New(handlerErrorMsg)\n\t\t}\n\t\t// Only execute action and mark success if caller is correct\n\t\thandlerExecutedCorrectly = true\n\t\treturn action()\n\t}\n\n\tcontractAuth := NewContractAuthority(contractPath, contractHandler)\n\tauthorizer := NewWithAuthority(contractAuth) // Start with ContractAuthority\n\n\tactionExecuted := false\n\tprivilegedAction := func() error {\n\t\tactionExecuted = true\n\t\treturn nil\n\t}\n\n\t// 1. Attempt action from unauthorized user\n\ttesting.SetRealm(testing.NewUserRealm(unauthorizedAddr))\n\terr := authorizer.DoByCurrent(0, cur, \"test_action_unauthorized\", privilegedAction)\n\n\t// Assertions for unauthorized call\n\tuassert.Error(t, err, \"DoByCurrent should return an error for unauthorized caller\")\n\tuassert.ErrorContains(t, err, handlerErrorMsg, \"Error should originate from the handler check\")\n\tuassert.False(t, handlerExecutedCorrectly, \"Handler should not have executed successfully for unauthorized caller\")\n\tuassert.False(t, actionExecuted, \"Privileged action should not have executed for unauthorized caller\")\n\n\t// 2. Attempt action from the correct contract\n\thandlerExecutedCorrectly = false // Reset flag\n\tactionExecuted = false           // Reset flag\n\ttesting.SetRealm(testing.NewCodeRealm(contractPath))\n\terr = authorizer.DoByCurrent(0, cur, \"test_action_authorized\", privilegedAction)\n\n\t// Assertions for authorized call\n\tuassert.NoError(t, err, \"DoByCurrent should succeed for authorized contract caller\")\n\tuassert.True(t, handlerExecutedCorrectly, \"Handler should have executed successfully for authorized caller\")\n\tuassert.True(t, actionExecuted, \"Privileged action should have executed for authorized caller\")\n}\n\n// TestAuthorizerDoByPrevious verifies the \"calling realm authorizes\"\n// pattern: a function (the inner crossing closure) invokes\n// DoByPrevious so the authority check sees cur.Previous() — the realm\n// that crossed into it — not the function's own realm.\n//\n// Each scenario crosses into the inner closure via cross(cur) after\n// SetRealm — inside the closure, cur is the fresh live cur and\n// cur.Previous() is the SetRealm'd outer realm. This is the only way\n// to exercise DoByPrevious correctly under the IsCurrent guard, which\n// rejects synthetic realm values (testing.MakeRealm) and stored\n// stale captures.\nfunc TestAuthorizerDoByPrevious(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tbob := testutils.TestAddress(\"bob\")\n\n\tauth := NewWithMembers(alice)\n\n\t// alice (member) crosses in: cur.Previous() == alice inside the inner closure.\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\texecuted := false\n\targs := []any{\"test_arg\", 123}\n\tfunc(cur realm) {\n\t\terr := auth.DoByPrevious(0, cur, \"test_action\", func() error {\n\t\t\texecuted = true\n\t\t\treturn nil\n\t\t}, args...)\n\t\tuassert.NoError(t, err, \"expected no error\")\n\t\tuassert.True(t, executed, \"action should have been executed\")\n\t}(cross(cur))\n\n\texpectedErr := errors.New(\"test error\")\n\tfunc(cur realm) {\n\t\terr := auth.DoByPrevious(0, cur, \"test_action\", func() error {\n\t\t\treturn expectedErr\n\t\t})\n\t\tuassert.ErrorContains(t, err, expectedErr.Error(), \"expected error\")\n\t}(cross(cur))\n\n\t// bob (not a member) crosses in: Authorize must reject.\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\texecuted = false\n\tfunc(cur realm) {\n\t\terr := auth.DoByPrevious(0, cur, \"test_action\", func() error {\n\t\t\texecuted = true\n\t\t\treturn nil\n\t\t}, \"unauthorized_arg\")\n\t\tuassert.ErrorContains(t, err, \"unauthorized\", \"expected error\")\n\t\tuassert.False(t, executed, \"action should not have been executed\")\n\t}(cross(cur))\n}\n"},{"name":"example_test.gno","body":"package authz\n\n// Example_basic demonstrates initializing and using a basic member authority\nfunc Example_basic(cur realm) {\n\t// Initialize from the EOA caller (e.g. in init(cur realm) of a realm\n\t// being deployed): caller passes the EOA address; the realm itself\n\t// is responsible for verifying the caller is an EOA when needed.\n\tauth := NewWithMembers(cur.Previous().Address())\n\n\t// Use the authority to perform a privileged action\n\tauth.DoByCurrent(0, cur, \"update_config\", func() error {\n\t\t// config = newValue\n\t\treturn nil\n\t})\n}\n\n// Example_addingMembers demonstrates how to add new members to a member authority\nfunc Example_addingMembers(cur realm) {\n\t// Initialize with the calling realm as the initial authority\n\tauth := NewWithMembers(cur.Address())\n\n\t// Add a new member to the authority\n\tmemberAuth := auth.Authority().(*MemberAuthority)\n\tmemberAuth.AddMember(0, cur, address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"))\n}\n\n// Example_contractAuthority demonstrates using a contract-based authority\nfunc Example_contractAuthority(cur realm) {\n\t// Initialize with contract authority (e.g., DAO)\n\tauth := NewWithAuthority(\n\t\tNewContractAuthority(\n\t\t\t\"gno.land/r/demo/dao\",\n\t\t\tmockDAOHandler, // defined elsewhere for example\n\t\t),\n\t)\n\n\t// Privileged actions will be handled by the contract\n\tauth.DoByCurrent(0, cur, \"update_params\", func() error {\n\t\t// Executes after DAO approval\n\t\treturn nil\n\t})\n}\n\n// Example_restrictedContractAuthority demonstrates a contract authority with member-only proposals\nfunc Example_restrictedContractAuthority(cur realm) {\n\t// Initialize member authority for proposers\n\tproposerAuth := NewMemberAuthority(\n\t\taddress(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"), // admin1\n\t\taddress(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"), // admin2\n\t)\n\n\t// Create contract authority with restricted proposers\n\tauth := NewWithAuthority(\n\t\tNewRestrictedContractAuthority(\n\t\t\t\"gno.land/r/demo/dao\",\n\t\t\tmockDAOHandler,\n\t\t\tproposerAuth,\n\t\t),\n\t)\n\n\t// Only members can propose, and contract must approve\n\tauth.DoByCurrent(0, cur, \"update_params\", func() error {\n\t\t// Executes after:\n\t\t// 1. Proposer initiates\n\t\t// 2. DAO approves\n\t\treturn nil\n\t})\n}\n\n// Example_switchingAuthority demonstrates switching from member to contract authority\nfunc Example_switchingAuthority(cur realm) {\n\t// Start with member authority (the calling realm)\n\tauth := NewWithMembers(cur.Address())\n\n\t// Create and switch to contract authority\n\tdaoAuthority := NewContractAuthority(\n\t\t\"gno.land/r/demo/dao\",\n\t\tmockDAOHandler,\n\t)\n\tauth.Transfer(0, cur, daoAuthority)\n}\n\n// Mock handler for examples\nfunc mockDAOHandler(title string, action PrivilegedAction) error {\n\treturn action()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/authz\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"is4BYiosgCjkRKsgjeswT/0vT774cfp9PSQyE9XbdVUPy8HhgzDfoXuIC0ffsHcXXOHnd6e1bP9IH7oVtWXKAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"dynreplacer","path":"gno.land/p/moul/dynreplacer","files":[{"name":"dynreplacer.gno","body":"// Package dynreplacer provides a simple template engine for handling dynamic\n// content replacement. It is similar to strings.Replacer but with lazy\n// execution of replacements, making it more optimization-friendly in several\n// cases. While strings.Replacer requires all replacement values to be computed\n// upfront, dynreplacer only executes the callback functions for placeholders\n// that actually exist in the template, avoiding unnecessary computations.\n//\n// The package ensures efficient, non-recursive replacement of placeholders in a\n// single pass. This lazy evaluation approach is particularly beneficial when:\n// - Some replacement values are expensive to compute\n// - Not all placeholders are guaranteed to be present in the template\n// - Templates are reused with different content\n//\n// Example usage:\n//\n//\tr := dynreplacer.New(\n//\t    dynreplacer.Pair{\":name:\", func() string { return \"World\" }},\n//\t    dynreplacer.Pair{\":greeting:\", func() string { return \"Hello\" }},\n//\t)\n//\tresult := r.Replace(\"Hello :name:!\") // Returns \"Hello World!\"\n//\n// The replacer caches computed values, so subsequent calls with the same\n// placeholder will reuse the cached value instead of executing the callback\n// again:\n//\n//\tr := dynreplacer.New()\n//\tr.RegisterCallback(\":expensive:\", func() string { return \"computed\" })\n//\tr.Replace(\"Value1: :expensive:\") // Computes the value\n//\tr.Replace(\"Value2: :expensive:\") // Uses cached value\n//\tr.ClearCache()                   // Force re-computation on next use\npackage dynreplacer\n\nimport (\n\t\"strings\"\n)\n\n// Replacer manages dynamic placeholders, their associated functions, and cached\n// values.\ntype Replacer struct {\n\tcallbacks    map[string]func() string\n\tcachedValues map[string]string\n}\n\n// Pair represents a placeholder and its callback function\ntype Pair struct {\n\tPlaceholder string\n\tCallback    func() string\n}\n\n// New creates a new Replacer instance with optional initial replacements.\n// It accepts pairs where each pair consists of a placeholder string and\n// its corresponding callback function.\n//\n// Example:\n//\n//\tNew(\n//\t    Pair{\":name:\", func() string { return \"World\" }},\n//\t    Pair{\":greeting:\", func() string { return \"Hello\" }},\n//\t)\nfunc New(pairs ...Pair) *Replacer {\n\tr := \u0026Replacer{\n\t\tcallbacks:    make(map[string]func() string),\n\t\tcachedValues: make(map[string]string),\n\t}\n\n\tfor _, pair := range pairs {\n\t\tr.RegisterCallback(pair.Placeholder, pair.Callback)\n\t}\n\n\treturn r\n}\n\n// RegisterCallback associates a placeholder with a function to generate its\n// content.\nfunc (r *Replacer) RegisterCallback(placeholder string, callback func() string) {\n\tr.callbacks[placeholder] = callback\n}\n\n// Replace processes the given layout, replacing placeholders with cached or\n// newly computed values.\nfunc (r *Replacer) Replace(layout string) string {\n\treplacements := []string{}\n\n\t// Check for placeholders and compute/retrieve values\n\thasReplacements := false\n\tfor placeholder, callback := range r.callbacks {\n\t\tif strings.Contains(layout, placeholder) {\n\t\t\tvalue, exists := r.cachedValues[placeholder]\n\t\t\tif !exists {\n\t\t\t\tvalue = callback()\n\t\t\t\tr.cachedValues[placeholder] = value\n\t\t\t}\n\t\t\treplacements = append(replacements, placeholder, value)\n\t\t\thasReplacements = true\n\t\t}\n\t}\n\n\t// If no replacements were found, return the original layout\n\tif !hasReplacements {\n\t\treturn layout\n\t}\n\n\t// Create a strings.Replacer with all computed replacements\n\treplacer := strings.NewReplacer(replacements...)\n\treturn replacer.Replace(layout)\n}\n\n// ClearCache clears all cached values, forcing re-computation on next Replace.\nfunc (r *Replacer) ClearCache() {\n\tr.cachedValues = make(map[string]string)\n}\n"},{"name":"dynreplacer_test.gno","body":"package dynreplacer\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNew(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tpairs []Pair\n\t}{\n\t\t{\n\t\t\tname:  \"empty constructor\",\n\t\t\tpairs: []Pair{},\n\t\t},\n\t\t{\n\t\t\tname: \"single pair\",\n\t\t\tpairs: []Pair{\n\t\t\t\t{\":name:\", func() string { return \"World\" }},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple pairs\",\n\t\t\tpairs: []Pair{\n\t\t\t\t{\":greeting:\", func() string { return \"Hello\" }},\n\t\t\t\t{\":name:\", func() string { return \"World\" }},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tr := New(tt.pairs...)\n\t\t\tuassert.True(t, r.callbacks != nil, \"callbacks map should be initialized\")\n\t\t\tuassert.True(t, r.cachedValues != nil, \"cachedValues map should be initialized\")\n\n\t\t\t// Verify all callbacks were registered\n\t\t\tfor _, pair := range tt.pairs {\n\t\t\t\t_, exists := r.callbacks[pair.Placeholder]\n\t\t\t\tuassert.True(t, exists, \"callback should be registered for \"+pair.Placeholder)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestReplace(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tlayout   string\n\t\tsetup    func(*Replacer)\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"empty layout\",\n\t\t\tlayout:   \"\",\n\t\t\tsetup:    func(r *Replacer) {},\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:   \"single replacement\",\n\t\t\tlayout: \"Hello :name:!\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\":name:\", func() string { return \"World\" })\n\t\t\t},\n\t\t\texpected: \"Hello World!\",\n\t\t},\n\t\t{\n\t\t\tname:   \"multiple replacements\",\n\t\t\tlayout: \":greeting: :name:!\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\":greeting:\", func() string { return \"Hello\" })\n\t\t\t\tr.RegisterCallback(\":name:\", func() string { return \"World\" })\n\t\t\t},\n\t\t\texpected: \"Hello World!\",\n\t\t},\n\t\t{\n\t\t\tname:   \"no recursive replacement\",\n\t\t\tlayout: \":outer:\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\":outer:\", func() string { return \":inner:\" })\n\t\t\t\tr.RegisterCallback(\":inner:\", func() string { return \"content\" })\n\t\t\t},\n\t\t\texpected: \":inner:\",\n\t\t},\n\t\t{\n\t\t\tname:   \"unused callbacks\",\n\t\t\tlayout: \"Hello :name:!\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\":name:\", func() string { return \"World\" })\n\t\t\t\tr.RegisterCallback(\":unused:\", func() string { return \"Never Called\" })\n\t\t\t},\n\t\t\texpected: \"Hello World!\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tr := New()\n\t\t\ttt.setup(r)\n\t\t\tresult := r.Replace(tt.layout)\n\t\t\tuassert.Equal(t, tt.expected, result)\n\t\t})\n\t}\n}\n\nfunc TestCaching(t *testing.T) {\n\tr := New()\n\tcallCount := 0\n\tr.RegisterCallback(\":expensive:\", func() string {\n\t\tcallCount++\n\t\treturn \"computed\"\n\t})\n\n\tlayout := \"Value: :expensive:\"\n\n\t// First call should compute\n\tresult1 := r.Replace(layout)\n\tuassert.Equal(t, \"Value: computed\", result1)\n\tuassert.Equal(t, 1, callCount)\n\n\t// Second call should use cache\n\tresult2 := r.Replace(layout)\n\tuassert.Equal(t, \"Value: computed\", result2)\n\tuassert.Equal(t, 1, callCount)\n\n\t// After clearing cache, should recompute\n\tr.ClearCache()\n\tresult3 := r.Replace(layout)\n\tuassert.Equal(t, \"Value: computed\", result3)\n\tuassert.Equal(t, 2, callCount)\n}\n\nfunc TestComplexExample(t *testing.T) {\n\tlayout := `\n\t\t# Welcome to gno.land\n\n\t\t## Blog\n\t\t:latest-blogposts:\n\n\t\t## Events\n\t\t:next-events:\n\n\t\t## Awesome Gno\n\t\t:awesome-gno:\n\t`\n\n\tr := New(\n\t\tPair{\":latest-blogposts:\", func() string { return \"Latest blog posts content here\" }},\n\t\tPair{\":next-events:\", func() string { return \"Upcoming events listed here\" }},\n\t\tPair{\":awesome-gno:\", func() string { return \":latest-blogposts: (This should NOT be replaced again)\" }},\n\t)\n\n\tresult := r.Replace(layout)\n\n\t// Check that original placeholders are replaced\n\tuassert.True(t, !strings.Contains(result, \":latest-blogposts:\\n\"), \"':latest-blogposts:' placeholder should be replaced\")\n\tuassert.True(t, !strings.Contains(result, \":next-events:\\n\"), \"':next-events:' placeholder should be replaced\")\n\tuassert.True(t, !strings.Contains(result, \":awesome-gno:\\n\"), \"':awesome-gno:' placeholder should be replaced\")\n\n\t// Check that the replacement content is present\n\tuassert.True(t, strings.Contains(result, \"Latest blog posts content here\"), \"Blog posts content should be present\")\n\tuassert.True(t, strings.Contains(result, \"Upcoming events listed here\"), \"Events content should be present\")\n\tuassert.True(t, strings.Contains(result, \":latest-blogposts: (This should NOT be replaced again)\"),\n\t\t\"Nested placeholder should not be replaced\")\n}\n\nfunc TestEdgeCases(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tlayout   string\n\t\tsetup    func(*Replacer)\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:   \"empty string placeholder\",\n\t\t\tlayout: \"Hello :\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\"\", func() string { return \"World\" })\n\t\t\t},\n\t\t\texpected: \"WorldHWorldeWorldlWorldlWorldoWorld World:World\",\n\t\t},\n\t\t{\n\t\t\tname:   \"overlapping placeholders\",\n\t\t\tlayout: \"Hello :name::greeting:\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\":name:\", func() string { return \"World\" })\n\t\t\t\tr.RegisterCallback(\":greeting:\", func() string { return \"Hi\" })\n\t\t\t\tr.RegisterCallback(\":name::greeting:\", func() string { return \"Should not match\" })\n\t\t\t},\n\t\t\texpected: \"Hello WorldHi\",\n\t\t},\n\t\t{\n\t\t\tname:   \"replacement order\",\n\t\t\tlayout: \":a::b::c:\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\":c:\", func() string { return \"3\" })\n\t\t\t\tr.RegisterCallback(\":b:\", func() string { return \"2\" })\n\t\t\t\tr.RegisterCallback(\":a:\", func() string { return \"1\" })\n\t\t\t},\n\t\t\texpected: \"123\",\n\t\t},\n\t\t{\n\t\t\tname:   \"special characters in placeholders\",\n\t\t\tlayout: \"Hello :$name#123:!\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tr.RegisterCallback(\":$name#123:\", func() string { return \"World\" })\n\t\t\t},\n\t\t\texpected: \"Hello World!\",\n\t\t},\n\t\t{\n\t\t\tname:   \"multiple occurrences of same placeholder\",\n\t\t\tlayout: \":name: and :name: again\",\n\t\t\tsetup: func(r *Replacer) {\n\t\t\t\tcallCount := 0\n\t\t\t\tr.RegisterCallback(\":name:\", func() string {\n\t\t\t\t\tcallCount++\n\t\t\t\t\treturn \"World\"\n\t\t\t\t})\n\t\t\t},\n\t\t\texpected: \"World and World again\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tr := New()\n\t\t\ttt.setup(r)\n\t\t\tresult := r.Replace(tt.layout)\n\t\t\tuassert.Equal(t, tt.expected, result)\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/dynreplacer\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"KQehSOmOVMK1wpttM0PaY9iWr60SnqDfsGYrxxQCbhBBpPy9KD4RmFUz6Y7eoujBJ8dauGaAXwgIGGHeJWsrCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"fifo","path":"gno.land/p/moul/fifo","files":[{"name":"fifo.gno","body":"// Package fifo implements a fixed-size FIFO (First-In-First-Out) list data structure\n// using a singly-linked list. The implementation prioritizes storage efficiency by minimizing\n// storage operations - each add/remove operation only updates 1-2 pointers, regardless of\n// list size.\n//\n// Key features:\n// - Fixed-size with automatic removal of oldest entries when full\n// - Support for both prepend (add at start) and append (add at end) operations\n// - Constant storage usage through automatic pruning\n// - O(1) append operations and latest element access\n// - Iterator support for sequential access\n// - Dynamic size adjustment via SetMaxSize\n//\n// This implementation is optimized for frequent updates, as insertions and deletions only\n// require updating 1-2 pointers. However, random access operations are O(n) as they require\n// traversing the list. For use cases where writes are rare, a slice-based\n// implementation might be more suitable.\n//\n// The linked list structure is equally efficient for storing both small values (like pointers)\n// and larger data structures, as each node maintains a single next-pointer regardless of the\n// stored value's size.\n//\n// Example usage:\n//\n//\tlist := fifo.New(3)        // Create a new list with max size 3\n//\tlist.Append(\"a\")           // List: [a]\n//\tlist.Append(\"b\")           // List: [a b]\n//\tlist.Append(\"c\")           // List: [a b c]\n//\tlist.Append(\"d\")           // List: [b c d] (oldest element \"a\" was removed)\n//\tlatest := list.Latest()    // Returns \"d\"\n//\tall := list.Entries()      // Returns [\"b\", \"c\", \"d\"]\npackage fifo\n\n// node represents a single element in the linked list\ntype node struct {\n\tvalue any\n\tnext  *node\n}\n\n// List represents a fixed-size FIFO list\ntype List struct {\n\thead    *node\n\ttail    *node\n\tsize    int\n\tmaxSize int\n}\n\n// New creates a new FIFO list with the specified maximum size\nfunc New(maxSize int) *List {\n\treturn \u0026List{\n\t\tmaxSize: maxSize,\n\t}\n}\n\n// Prepend adds a new entry at the start of the list. If the list exceeds maxSize,\n// the last entry is automatically removed.\nfunc (l *List) Prepend(entry any) {\n\tif l.maxSize == 0 {\n\t\treturn\n\t}\n\n\tnewNode := \u0026node{value: entry}\n\n\tif l.head == nil {\n\t\tl.head = newNode\n\t\tl.tail = newNode\n\t\tl.size = 1\n\t\treturn\n\t}\n\n\tnewNode.next = l.head\n\tl.head = newNode\n\n\tif l.size \u003c l.maxSize {\n\t\tl.size++\n\t\treturn\n\t}\n\n\t// Remove last element by traversing to second-to-last\n\tif l.size == 1 {\n\t\t// Special case: if size is 1, just update both pointers\n\t\tl.head = newNode\n\t\tl.tail = newNode\n\t\tnewNode.next = nil\n\t\treturn\n\n\t}\n\n\t// Find second-to-last node\n\tcurrent := l.head\n\tfor current.next != l.tail {\n\t\tcurrent = current.next\n\t}\n\tcurrent.next = nil\n\tl.tail = current\n\n}\n\n// Append adds a new entry at the end of the list. If the list exceeds maxSize,\n// the first entry is automatically removed.\nfunc (l *List) Append(entry any) {\n\tif l.maxSize == 0 {\n\t\treturn\n\t}\n\n\tnewNode := \u0026node{value: entry}\n\n\tif l.head == nil {\n\t\tl.head = newNode\n\t\tl.tail = newNode\n\t\tl.size = 1\n\t\treturn\n\t}\n\n\tl.tail.next = newNode\n\tl.tail = newNode\n\n\tif l.size \u003c l.maxSize {\n\t\tl.size++\n\t} else {\n\t\tl.head = l.head.next\n\t}\n}\n\n// Get returns the entry at the specified index.\n// Index 0 is the oldest entry, Size()-1 is the newest.\nfunc (l *List) Get(index int) any {\n\tif index \u003c 0 || index \u003e= l.size {\n\t\treturn nil\n\t}\n\n\tcurrent := l.head\n\tfor i := 0; i \u003c index; i++ {\n\t\tcurrent = current.next\n\t}\n\treturn current.value\n}\n\n// Size returns the current number of entries in the list\nfunc (l *List) Size() int {\n\treturn l.size\n}\n\n// MaxSize returns the maximum size configured for this list\nfunc (l *List) MaxSize() int {\n\treturn l.maxSize\n}\n\n// Entries returns all current entries as a slice\nfunc (l *List) Entries() []any {\n\tentries := make([]any, l.size)\n\tcurrent := l.head\n\tfor i := 0; i \u003c l.size; i++ {\n\t\tentries[i] = current.value\n\t\tcurrent = current.next\n\t}\n\treturn entries\n}\n\n// Iterator returns a function that can be used to iterate over the entries\n// from oldest to newest. Returns nil when there are no more entries.\nfunc (l *List) Iterator() func() any {\n\tcurrent := l.head\n\treturn func() any {\n\t\tif current == nil {\n\t\t\treturn nil\n\t\t}\n\t\tvalue := current.value\n\t\tcurrent = current.next\n\t\treturn value\n\t}\n}\n\n// Latest returns the most recent entry.\n// Returns nil if the list is empty.\nfunc (l *List) Latest() any {\n\tif l.tail == nil {\n\t\treturn nil\n\t}\n\treturn l.tail.value\n}\n\n// SetMaxSize updates the maximum size of the list.\n// If the new maxSize is smaller than the current size,\n// the oldest entries are removed to fit the new size.\nfunc (l *List) SetMaxSize(maxSize int) {\n\tif maxSize \u003c 0 {\n\t\tmaxSize = 0\n\t}\n\n\t// If new maxSize is smaller than current size,\n\t// remove oldest entries until we fit\n\tif maxSize \u003c l.size {\n\t\t// Special case: if new maxSize is 0, clear the list\n\t\tif maxSize == 0 {\n\t\t\tl.head = nil\n\t\t\tl.tail = nil\n\t\t\tl.size = 0\n\t\t} else {\n\t\t\t// Keep the newest entries by moving head forward\n\t\t\tdiff := l.size - maxSize\n\t\t\tfor i := 0; i \u003c diff; i++ {\n\t\t\t\tl.head = l.head.next\n\t\t\t}\n\t\t\tl.size = maxSize\n\t\t}\n\t}\n\n\tl.maxSize = maxSize\n}\n\n// Delete removes the element at the specified index.\n// Returns true if an element was removed, false if the index was invalid.\nfunc (l *List) Delete(index int) bool {\n\tif index \u003c 0 || index \u003e= l.size {\n\t\treturn false\n\t}\n\n\t// Special case: deleting the only element\n\tif l.size == 1 {\n\t\tl.head = nil\n\t\tl.tail = nil\n\t\tl.size = 0\n\t\treturn true\n\t}\n\n\t// Special case: deleting first element\n\tif index == 0 {\n\t\tl.head = l.head.next\n\t\tl.size--\n\t\treturn true\n\t}\n\n\t// Find the node before the one to delete\n\tcurrent := l.head\n\tfor i := 0; i \u003c index-1; i++ {\n\t\tcurrent = current.next\n\t}\n\n\t// Special case: deleting last element\n\tif index == l.size-1 {\n\t\tl.tail = current\n\t\tcurrent.next = nil\n\t} else {\n\t\tcurrent.next = current.next.next\n\t}\n\n\tl.size--\n\treturn true\n}\n"},{"name":"fifo_test.gno","body":"package fifo\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNew(t *testing.T) {\n\tl := New(5)\n\tuassert.Equal(t, 5, l.MaxSize())\n\tuassert.Equal(t, 0, l.Size())\n}\n\nfunc TestAppend(t *testing.T) {\n\tl := New(3)\n\n\t// Test adding within capacity\n\tl.Append(1)\n\tl.Append(2)\n\tuassert.Equal(t, 2, l.Size())\n\tuassert.Equal(t, 1, l.Get(0))\n\tuassert.Equal(t, 2, l.Get(1))\n\n\t// Test overflow behavior\n\tl.Append(3)\n\tl.Append(4)\n\tuassert.Equal(t, 3, l.Size())\n\tuassert.Equal(t, 2, l.Get(0))\n\tuassert.Equal(t, 3, l.Get(1))\n\tuassert.Equal(t, 4, l.Get(2))\n}\n\nfunc TestPrepend(t *testing.T) {\n\tl := New(3)\n\n\t// Test adding within capacity\n\tl.Prepend(1)\n\tl.Prepend(2)\n\tuassert.Equal(t, 2, l.Size())\n\tuassert.Equal(t, 2, l.Get(0))\n\tuassert.Equal(t, 1, l.Get(1))\n\n\t// Test overflow behavior\n\tl.Prepend(3)\n\tl.Prepend(4)\n\tuassert.Equal(t, 3, l.Size())\n\tuassert.Equal(t, 4, l.Get(0))\n\tuassert.Equal(t, 3, l.Get(1))\n\tuassert.Equal(t, 2, l.Get(2))\n}\n\nfunc TestGet(t *testing.T) {\n\tl := New(3)\n\tl.Append(1)\n\tl.Append(2)\n\tl.Append(3)\n\n\t// Test valid indices\n\tuassert.Equal(t, 1, l.Get(0))\n\tuassert.Equal(t, 2, l.Get(1))\n\tuassert.Equal(t, 3, l.Get(2))\n\n\t// Test invalid indices\n\tuassert.True(t, l.Get(-1) == nil)\n\tuassert.True(t, l.Get(3) == nil)\n}\n\nfunc TestEntries(t *testing.T) {\n\tl := New(3)\n\tl.Append(1)\n\tl.Append(2)\n\tl.Append(3)\n\n\tentries := l.Entries()\n\tuassert.Equal(t, 3, len(entries))\n\tuassert.Equal(t, 1, entries[0])\n\tuassert.Equal(t, 2, entries[1])\n\tuassert.Equal(t, 3, entries[2])\n}\n\nfunc TestLatest(t *testing.T) {\n\tl := New(5)\n\n\t// Test empty list\n\tuassert.True(t, l.Latest() == nil)\n\n\t// Test single entry\n\tl.Append(1)\n\tuassert.Equal(t, 1, l.Latest())\n\n\t// Test multiple entries\n\tl.Append(2)\n\tl.Append(3)\n\tuassert.Equal(t, 3, l.Latest())\n\n\t// Test after overflow\n\tl.Append(4)\n\tl.Append(5)\n\tl.Append(6)\n\tuassert.Equal(t, 6, l.Latest())\n}\n\nfunc TestIterator(t *testing.T) {\n\tl := New(3)\n\tl.Append(1)\n\tl.Append(2)\n\tl.Append(3)\n\n\titer := l.Iterator()\n\tuassert.Equal(t, 1, iter())\n\tuassert.Equal(t, 2, iter())\n\tuassert.Equal(t, 3, iter())\n\tuassert.True(t, iter() == nil)\n}\n\nfunc TestMixedOperations(t *testing.T) {\n\tl := New(3)\n\n\t// Mix of append and prepend operations\n\tl.Append(1)  // [1]\n\tl.Prepend(2) // [2,1]\n\tl.Append(3)  // [2,1,3]\n\tl.Prepend(4) // [4,2,1]\n\n\tentries := l.Entries()\n\tuassert.Equal(t, 3, len(entries))\n\tuassert.Equal(t, 4, entries[0])\n\tuassert.Equal(t, 2, entries[1])\n\tuassert.Equal(t, 1, entries[2])\n}\n\nfunc TestEmptyList(t *testing.T) {\n\tl := New(3)\n\n\t// Test operations on empty list\n\tuassert.Equal(t, 0, l.Size())\n\tuassert.True(t, l.Get(0) == nil)\n\tuassert.Equal(t, 0, len(l.Entries()))\n\tuassert.True(t, l.Latest() == nil)\n\n\titer := l.Iterator()\n\tuassert.True(t, iter() == nil)\n}\n\nfunc TestEdgeCases(t *testing.T) {\n\t// Test zero-size list\n\tl := New(0)\n\tuassert.Equal(t, 0, l.MaxSize())\n\tl.Append(1) // Should be no-op\n\tuassert.Equal(t, 0, l.Size())\n\n\t// Test single-element list\n\tl = New(1)\n\tl.Append(1)\n\tl.Append(2) // Should replace 1\n\tuassert.Equal(t, 1, l.Size())\n\tuassert.Equal(t, 2, l.Latest())\n\n\t// Test rapid append/prepend alternation\n\tl = New(3)\n\tl.Append(1)  // [1]\n\tl.Prepend(2) // [2,1]\n\tl.Append(3)  // [2,1,3]\n\tl.Prepend(4) // [4,2,1]\n\tl.Append(5)  // [2,1,5]\n\tuassert.Equal(t, 3, l.Size())\n\tentries := l.Entries()\n\tuassert.Equal(t, 2, entries[0])\n\tuassert.Equal(t, 1, entries[1])\n\tuassert.Equal(t, 5, entries[2])\n\n\t// Test nil values\n\tl = New(2)\n\tl.Append(nil)\n\tl.Prepend(nil)\n\tuassert.Equal(t, 2, l.Size())\n\tuassert.True(t, l.Get(0) == nil)\n\tuassert.True(t, l.Get(1) == nil)\n\n\t// Test index bounds\n\tl = New(3)\n\tl.Append(1)\n\tuassert.True(t, l.Get(-1) == nil)\n\tuassert.True(t, l.Get(1) == nil)\n\n\t// Test iterator exhaustion\n\tl = New(2)\n\tl.Append(1)\n\tl.Append(2)\n\titer := l.Iterator()\n\tuassert.Equal(t, 1, iter())\n\tuassert.Equal(t, 2, iter())\n\tuassert.True(t, iter() == nil)\n\tuassert.True(t, iter() == nil)\n\n\t// Test prepend on full list\n\tl = New(2)\n\tl.Append(1)\n\tl.Append(2)  // [1,2]\n\tl.Prepend(3) // [3,1]\n\tuassert.Equal(t, 2, l.Size())\n\tentries = l.Entries()\n\tuassert.Equal(t, 3, entries[0])\n\tuassert.Equal(t, 1, entries[1])\n}\n\nfunc TestSetMaxSize(t *testing.T) {\n\tl := New(5)\n\n\t// Fill the list\n\tl.Append(1)\n\tl.Append(2)\n\tl.Append(3)\n\tl.Append(4)\n\tl.Append(5)\n\n\t// Test increasing maxSize\n\tl.SetMaxSize(7)\n\tuassert.Equal(t, 7, l.MaxSize())\n\tuassert.Equal(t, 5, l.Size())\n\n\t// Test reducing maxSize\n\tl.SetMaxSize(3)\n\tuassert.Equal(t, 3, l.Size())\n\tentries := l.Entries()\n\tuassert.Equal(t, 3, entries[0])\n\tuassert.Equal(t, 4, entries[1])\n\tuassert.Equal(t, 5, entries[2])\n\n\t// Test setting to zero\n\tl.SetMaxSize(0)\n\tuassert.Equal(t, 0, l.Size())\n\tuassert.True(t, l.head == nil)\n\tuassert.True(t, l.tail == nil)\n\n\t// Test negative maxSize\n\tl.SetMaxSize(-1)\n\tuassert.Equal(t, 0, l.MaxSize())\n\n\t// Test setting back to positive\n\tl.SetMaxSize(2)\n\tl.Append(1)\n\tl.Append(2)\n\tl.Append(3)\n\tuassert.Equal(t, 2, l.Size())\n\tentries = l.Entries()\n\tuassert.Equal(t, 2, entries[0])\n\tuassert.Equal(t, 3, entries[1])\n}\n\nfunc TestDelete(t *testing.T) {\n\tl := New(5)\n\n\t// Test delete on empty list\n\tuassert.False(t, l.Delete(0))\n\tuassert.False(t, l.Delete(-1))\n\n\t// Fill list\n\tl.Append(1)\n\tl.Append(2)\n\tl.Append(3)\n\tl.Append(4)\n\n\t// Test invalid indices\n\tuassert.False(t, l.Delete(-1))\n\tuassert.False(t, l.Delete(4))\n\n\t// Test deleting from middle\n\tuassert.True(t, l.Delete(1))\n\tuassert.Equal(t, 3, l.Size())\n\tentries := l.Entries()\n\tuassert.Equal(t, 1, entries[0])\n\tuassert.Equal(t, 3, entries[1])\n\tuassert.Equal(t, 4, entries[2])\n\n\t// Test deleting from head\n\tuassert.True(t, l.Delete(0))\n\tuassert.Equal(t, 2, l.Size())\n\tentries = l.Entries()\n\tuassert.Equal(t, 3, entries[0])\n\tuassert.Equal(t, 4, entries[1])\n\n\t// Test deleting from tail\n\tuassert.True(t, l.Delete(1))\n\tuassert.Equal(t, 1, l.Size())\n\tuassert.Equal(t, 3, l.Latest())\n\n\t// Test deleting last element\n\tuassert.True(t, l.Delete(0))\n\tuassert.Equal(t, 0, l.Size())\n\tuassert.True(t, l.head == nil)\n\tuassert.True(t, l.tail == nil)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/fifo\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"hNgM5So0ipZ8lEzVg3g6x75Yfgkp8OFB75+riYaulSK2WdvMCIfqnSjP0rTz3KaSQaRlm1DvvsdPhXknDYp3BQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"txlink","path":"gno.land/p/moul/txlink","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/txlink\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"txlink.gno","body":"// Package txlink provides utilities for creating transaction-related links\n// compatible with Gnoweb, Gnobro, and other clients within the Gno ecosystem.\n//\n// This package is optimized for generating lightweight transaction links with\n// flexible arguments, allowing users to build dynamic links that integrate\n// seamlessly with various Gno clients.\n//\n// The package offers a way to generate clickable transaction MD links\n// for the current \"relative realm\":\n//\n//  Using a builder pattern for more structured URLs:\n//     txlink.NewLink(\"MyFunc\").\n//         AddArgs(\"k1\", \"v1\", \"k2\", \"v2\"). // or multiple at once\n//         SetSend(\"1000000ugnot\").\n//         URL()\n//\n// The builder pattern (TxBuilder) provides a fluent interface for constructing\n// transaction URLs in the current \"relative realm\". Like Call, it supports both\n// local realm paths and fully qualified paths through the underlying Call\n// implementation.\n//\n// The Call function remains the core implementation, used both directly and\n// internally by the builder pattern to generate the final URLs.\n//\n// This package is a streamlined alternative to helplink, providing similar\n// functionality for transaction links without the full feature set of helplink.\n\npackage txlink\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"net/url\"\n\t\"strings\"\n)\n\nvar chainDomain = runtime.ChainDomain()\n\n// Realm represents a specific realm for generating tx links.\ntype Realm string\n\n// TxBuilder provides a fluent interface for building transaction URLs\ntype TxBuilder struct {\n\tfn        string   // function name\n\targs      []string // key-value pairs\n\tsend      string   // optional send amount\n\trealm_XXX Realm    // realm for the URL\n}\n\n// NewLink creates a transaction link builder for the specified function in the current realm.\nfunc NewLink(fn string) *TxBuilder {\n\treturn Realm(\"\").NewLink(fn)\n}\n\n// NewLink creates a transaction link builder for the specified function in this realm.\nfunc (r Realm) NewLink(fn string) *TxBuilder {\n\tif fn == \"\" {\n\t\treturn nil\n\t}\n\treturn \u0026TxBuilder{fn: fn, realm_XXX: r}\n}\n\n// addArg adds a key-value argument pair. Returns the builder for chaining.\nfunc (b *TxBuilder) addArg(key, value string) *TxBuilder {\n\tif b == nil {\n\t\treturn nil\n\t}\n\tif key == \"\" {\n\t\treturn b\n\t}\n\n\t// Special case: \".\" prefix is for reserved keywords.\n\tif strings.HasPrefix(key, \".\") {\n\t\tpanic(\"invalid key\")\n\t}\n\n\tb.args = append(b.args, key, value)\n\treturn b\n}\n\n// AddArgs adds multiple key-value pairs at once. Arguments should be provided\n// as pairs: AddArgs(\"key1\", \"value1\", \"key2\", \"value2\").\nfunc (b *TxBuilder) AddArgs(args ...string) *TxBuilder {\n\tif b == nil {\n\t\treturn nil\n\t}\n\tif len(args)%2 != 0 {\n\t\tpanic(\"odd number of arguments\")\n\t}\n\t// Add key-value pairs\n\tfor i := 0; i \u003c len(args); i += 2 {\n\t\tkey := args[i]\n\t\tvalue := args[i+1]\n\t\tb.addArg(key, value)\n\t}\n\treturn b\n}\n\n// SetSend adds a send amount. (Only one send amount can be specified.)\nfunc (b *TxBuilder) SetSend(amount string) *TxBuilder {\n\tif b == nil {\n\t\treturn nil\n\t}\n\tif amount == \"\" {\n\t\treturn b\n\t}\n\tb.send = amount\n\treturn b\n}\n\n// URL generates the final URL using the standard $help\u0026func=name format.\nfunc (b *TxBuilder) URL() string {\n\tif b == nil || b.fn == \"\" {\n\t\treturn \"\"\n\t}\n\targs := b.args\n\tif b.send != \"\" {\n\t\targs = append(args, \".send\", b.send)\n\t}\n\treturn b.realm_XXX.Call(b.fn, args...)\n}\n\n// Call returns a URL for the specified function with optional key-value\n// arguments, for the current realm.\nfunc Call(fn string, args ...string) string {\n\treturn Realm(\"\").Call(fn, args...)\n}\n\n// prefix returns the URL prefix for the realm.\nfunc (r Realm) prefix() string {\n\t// relative\n\tif r == \"\" {\n\t\tcurPath := unsafe.CurrentRealm().PkgPath()\n\t\treturn strings.TrimPrefix(curPath, chainDomain)\n\t}\n\n\t// local realm -\u003e /realm\n\trlm := string(r)\n\tif strings.HasPrefix(rlm, chainDomain) {\n\t\treturn strings.TrimPrefix(rlm, chainDomain)\n\t}\n\n\t// remote realm -\u003e https://remote.land/realm\n\treturn \"https://\" + string(r)\n}\n\n// Call returns a URL for the specified function with optional key-value\n// arguments.\nfunc (r Realm) Call(fn string, args ...string) string {\n\tif len(args) == 0 {\n\t\treturn r.prefix() + \"$help\u0026func=\" + fn\n\t}\n\n\t// Create url.Values to properly encode parameters.\n\t// But manage \u0026func=fn as a special case to keep it as the first argument.\n\tvalues := url.Values{}\n\n\t// Check if args length is even\n\tif len(args)%2 != 0 {\n\t\tpanic(\"odd number of arguments\")\n\t}\n\t// Add key-value pairs to values\n\tfor i := 0; i \u003c len(args); i += 2 {\n\t\tkey := args[i]\n\t\tvalue := args[i+1]\n\t\tvalues.Add(key, value)\n\t}\n\n\t// Build the base URL and append encoded query parameters\n\treturn r.prefix() + \"$help\u0026func=\" + fn + \"\u0026\" + values.Encode()\n}\n"},{"name":"txlink_test.gno","body":"package txlink\n\nimport (\n\t\"chain/runtime\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestCall(t *testing.T) {\n\tcd := runtime.ChainDomain()\n\n\ttests := []struct {\n\t\tfn        string\n\t\targs      []string\n\t\twant      string\n\t\trealm_XXX Realm\n\t}{\n\t\t{\"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"/p/moul/txlink$help\u0026func=foo\u0026bar=1\u0026baz=2\", \"\"},\n\t\t{\"testFunc\", []string{\"key\", \"value\"}, \"/p/moul/txlink$help\u0026func=testFunc\u0026key=value\", \"\"},\n\t\t{\"noArgsFunc\", []string{}, \"/p/moul/txlink$help\u0026func=noArgsFunc\", \"\"},\n\t\t{\"oddArgsFunc\", []string{\"key\"}, \"/p/moul/txlink$help\u0026func=oddArgsFunc\u0026error=odd+number+of+arguments\", \"\"},\n\t\t{\"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"/r/lorem/ipsum$help\u0026func=foo\u0026bar=1\u0026baz=2\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"testFunc\", []string{\"key\", \"value\"}, \"/r/lorem/ipsum$help\u0026func=testFunc\u0026key=value\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"noArgsFunc\", []string{}, \"/r/lorem/ipsum$help\u0026func=noArgsFunc\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"oddArgsFunc\", []string{\"key\"}, \"/r/lorem/ipsum$help\u0026func=oddArgsFunc\u0026error=odd+number+of+arguments\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"https://gno.world/r/lorem/ipsum$help\u0026func=foo\u0026bar=1\u0026baz=2\", \"gno.world/r/lorem/ipsum\"},\n\t\t{\"testFunc\", []string{\"key\", \"value\"}, \"https://gno.world/r/lorem/ipsum$help\u0026func=testFunc\u0026key=value\", \"gno.world/r/lorem/ipsum\"},\n\t\t{\"noArgsFunc\", []string{}, \"https://gno.world/r/lorem/ipsum$help\u0026func=noArgsFunc\", \"gno.world/r/lorem/ipsum\"},\n\t\t{\"oddArgsFunc\", []string{\"key\"}, \"https://gno.world/r/lorem/ipsum$help\u0026func=oddArgsFunc\u0026error=odd+number+of+arguments\", \"gno.world/r/lorem/ipsum\"},\n\t\t{\"test\", []string{\"key\", \"hello world\"}, \"/p/moul/txlink$help\u0026func=test\u0026key=hello+world\", \"\"},\n\t\t{\"test\", []string{\"key\", \"a\u0026b=c\"}, \"/p/moul/txlink$help\u0026func=test\u0026key=a%26b%3Dc\", \"\"},\n\t\t{\"test\", []string{\"key\", \"\"}, \"/p/moul/txlink$help\u0026func=test\u0026key=\", \"\"},\n\t\t{\"testSend\", []string{\"key\", \"hello world\", \".send\", \"1000000ugnot\"}, \"/p/moul/txlink$help\u0026func=testSend\u0026.send=1000000ugnot\u0026key=hello+world\", \"\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\ttitle := string(tt.realm_XXX) + \"_\" + tt.fn\n\t\tt.Run(title, func(t *testing.T) {\n\t\t\tif tt.fn == \"oddArgsFunc\" {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tif r != \"odd number of arguments\" {\n\t\t\t\t\t\t\tt.Errorf(\"expected panic with message 'odd number of arguments', got: %v\", r)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tt.Error(\"expected panic for odd number of arguments, but did not panic\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\t\t\tgot := tt.realm_XXX.Call(tt.fn, tt.args...)\n\t\t\turequire.Equal(t, tt.want, got)\n\t\t})\n\t}\n}\n\nfunc TestBuilder(t *testing.T) {\n\tcases := []struct {\n\t\tname     string\n\t\tbuild    func() string\n\t\texpected string\n\t}{\n\t\t// Basic functionality tests\n\t\t{\n\t\t\tname: \"empty_function\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"\").URL()\n\t\t\t},\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname: \"function_without_args\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"MyFunc\").URL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=MyFunc\",\n\t\t},\n\n\t\t// Realm tests\n\t\t{\n\t\t\tname: \"gnoland_realm\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn Realm(\"gno.land/r/demo\").\n\t\t\t\t\tNewLink(\"MyFunc\").\n\t\t\t\t\tAddArgs(\"key\", \"value\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/r/demo$help\u0026func=MyFunc\u0026key=value\",\n\t\t},\n\t\t{\n\t\t\tname: \"external_realm\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn Realm(\"gno.world/r/demo\").\n\t\t\t\t\tNewLink(\"MyFunc\").\n\t\t\t\t\tAddArgs(\"key\", \"value\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"https://gno.world/r/demo$help\u0026func=MyFunc\u0026key=value\",\n\t\t},\n\t\t{\n\t\t\tname: \"empty_realm\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn Realm(\"\").\n\t\t\t\t\tNewLink(\"func\").\n\t\t\t\t\tAddArgs(\"key\", \"value\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=func\u0026key=value\",\n\t\t},\n\n\t\t// URL encoding tests\n\t\t{\n\t\t\tname: \"url_encoding_with_spaces\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"test\").\n\t\t\t\t\tAddArgs(\"key\", \"hello world\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=test\u0026key=hello+world\",\n\t\t},\n\t\t{\n\t\t\tname: \"url_encoding_with_special_chars\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"test\").\n\t\t\t\t\tAddArgs(\"key\", \"a\u0026b=c\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=test\u0026key=a%26b%3Dc\",\n\t\t},\n\t\t{\n\t\t\tname: \"url_encoding_with_unicode\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"func\").\n\t\t\t\t\tAddArgs(\"key\", \"🌟\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=func\u0026key=%F0%9F%8C%9F\",\n\t\t},\n\t\t{\n\t\t\tname: \"url_encoding_with_special_chars_in_key\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"func\").\n\t\t\t\t\tAddArgs(\"my/key\", \"value\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=func\u0026my%2Fkey=value\",\n\t\t},\n\n\t\t// AddArgs tests\n\t\t{\n\t\t\tname: \"addargs_with_multiple_pairs\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"MyFunc\").\n\t\t\t\t\tAddArgs(\"key1\", \"value1\", \"key2\", \"value2\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=MyFunc\u0026key1=value1\u0026key2=value2\",\n\t\t},\n\t\t{\n\t\t\tname: \"addargs_with_odd_number_of_args\",\n\t\t\tbuild: func() string {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tif r != \"odd number of arguments\" {\n\t\t\t\t\t\t\tt.Errorf(\"expected panic with message 'odd number of arguments', got: %v\", r)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tt.Error(\"expected panic for odd number of arguments, but did not panic\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t\treturn NewLink(\"MyFunc\").\n\t\t\t\t\tAddArgs(\"key1\", \"value1\", \"orphan\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"\",\n\t\t},\n\n\t\t// Empty values tests\n\t\t{\n\t\t\tname: \"empty_key_should_be_ignored\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"func\").\n\t\t\t\t\tAddArgs(\"\", \"value\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=func\",\n\t\t},\n\t\t{\n\t\t\tname: \"empty_value_should_be_kept\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"func\").\n\t\t\t\t\tAddArgs(\"key\", \"\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=func\u0026key=\",\n\t\t},\n\n\t\t// Send tests\n\t\t{\n\t\t\tname: \"send_via_addsend_method\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"MyFunc\").\n\t\t\t\t\tAddArgs(\"key\", \"value\").\n\t\t\t\t\tSetSend(\"1000000ugnot\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=MyFunc\u0026.send=1000000ugnot\u0026key=value\",\n\t\t},\n\t\t{\n\t\t\tname: \"send_via_addarg_method_panic\",\n\t\t\tbuild: func() string {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tif r != \"invalid key\" {\n\t\t\t\t\t\t\tt.Errorf(\"expected panic with message 'invalid key', got: %v\", r)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tt.Errorf(\"expected panic for .send key, but did not panic\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t\tNewLink(\"MyFunc\").AddArgs(\".send\", \"1000000ugnot\")\n\t\t\t\treturn \"no panic occurred\"\n\t\t\t},\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname: \"addsend_should_override_previous_addsend\",\n\t\t\tbuild: func() string {\n\t\t\t\treturn NewLink(\"MyFunc\").\n\t\t\t\t\tSetSend(\"1000000ugnot\").\n\t\t\t\t\tSetSend(\"2000000ugnot\").\n\t\t\t\t\tURL()\n\t\t\t},\n\t\t\texpected: \"/p/moul/txlink$help\u0026func=MyFunc\u0026.send=2000000ugnot\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tgot := tc.build()\n\t\t\turequire.Equal(t, tc.expected, got)\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"H0GDv0gcfxZeAkcMt2/vSrfiiuq/yMrobPxmFBkgbX/5fjqKKkPUKxyjoJnchv1yJ98DB3SN3lBOzI/Xz/76CA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"helplink","path":"gno.land/p/moul/helplink","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/helplink\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"helplink.gno","body":"// Package helplink provides utilities for creating help page links compatible\n// with Gnoweb, Gnobro, and other clients that support the Gno contracts'\n// flavored Markdown format.\n//\n// This package simplifies the generation of dynamic, context-sensitive help\n// links, enabling users to navigate relevant documentation seamlessly within\n// the Gno ecosystem.\n//\n// For a more lightweight alternative, consider using p/moul/txlink.\n//\n// The primary functions — Func, FuncURL, and Home — are intended for use with\n// the \"relative realm\". When specifying a custom Realm, you can create links\n// that utilize either the current realm path or a fully qualified path to\n// another realm.\npackage helplink\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/txlink\"\n)\n\nvar chainDomain = runtime.ChainDomain()\n\n// Func returns a markdown link for the specific function with optional\n// key-value arguments, for the current realm.\nfunc Func(title string, fn string, args ...string) string {\n\treturn Realm(\"\").Func(title, fn, args...)\n}\n\n// FuncURL returns a URL for the specified function with optional key-value\n// arguments, for the current realm.\nfunc FuncURL(fn string, args ...string) string {\n\treturn Realm(\"\").FuncURL(fn, args...)\n}\n\n// Home returns the URL for the help homepage of the current realm.\nfunc Home() string {\n\treturn Realm(\"\").Home()\n}\n\n// Realm represents a specific realm for generating help links.\ntype Realm string\n\n// prefix returns the URL prefix for the realm.\nfunc (r Realm) prefix() string {\n\t// relative\n\tif r == \"\" {\n\t\tcurPath := unsafe.CurrentRealm().PkgPath()\n\t\treturn strings.TrimPrefix(curPath, chainDomain)\n\t}\n\n\t// local realm -\u003e /realm\n\trlmstr := string(r)\n\tif strings.HasPrefix(rlmstr, chainDomain) {\n\t\treturn strings.TrimPrefix(rlmstr, chainDomain)\n\t}\n\n\t// remote realm -\u003e https://remote.land/realm\n\treturn \"https://\" + rlmstr\n}\n\n// Func returns a markdown link for the specified function with optional\n// key-value arguments.\nfunc (r Realm) Func(title string, fn string, args ...string) string {\n\t// XXX: escape title\n\treturn \"[\" + title + \"](\" + r.FuncURL(fn, args...) + \")\"\n}\n\n// FuncURL returns a URL for the specified function with optional key-value\n// arguments.\nfunc (r Realm) FuncURL(fn string, args ...string) string {\n\ttlr := txlink.Realm(r)\n\treturn tlr.Call(fn, args...)\n}\n\n// Home returns the base help URL for the specified realm.\nfunc (r Realm) Home() string {\n\treturn r.prefix() + \"$help\"\n}\n"},{"name":"helplink_test.gno","body":"package helplink\n\nimport (\n\t\"chain/runtime\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestFunc(t *testing.T) {\n\tcd := runtime.ChainDomain()\n\ttests := []struct {\n\t\ttitle     string\n\t\tfn        string\n\t\targs      []string\n\t\twant      string\n\t\trealm_XXX Realm\n\t}{\n\t\t{\"Example\", \"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"[Example](/p/moul/helplink$help\u0026func=foo\u0026bar=1\u0026baz=2)\", \"\"},\n\t\t{\"Realm Example\", \"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"[Realm Example](/r/lorem/ipsum$help\u0026func=foo\u0026bar=1\u0026baz=2)\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"Single Arg\", \"testFunc\", []string{\"key\", \"value\"}, \"[Single Arg](/p/moul/helplink$help\u0026func=testFunc\u0026key=value)\", \"\"},\n\t\t{\"No Args\", \"noArgsFunc\", []string{}, \"[No Args](/p/moul/helplink$help\u0026func=noArgsFunc)\", \"\"},\n\t\t{\"Odd Args\", \"oddArgsFunc\", []string{\"key\"}, \"[Odd Args](/p/moul/helplink$help\u0026func=oddArgsFunc\u0026error=odd+number+of+arguments)\", \"\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.title, func(t *testing.T) {\n\t\t\tif tt.fn == \"oddArgsFunc\" {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tif r != \"odd number of arguments\" {\n\t\t\t\t\t\t\tt.Errorf(\"expected panic with message 'odd number of arguments', got: %v\", r)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tt.Error(\"expected panic for odd number of arguments, but did not panic\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\t\t\tgot := tt.realm_XXX.Func(tt.title, tt.fn, tt.args...)\n\t\t\turequire.Equal(t, tt.want, got)\n\t\t})\n\t}\n}\n\nfunc TestFuncURL(t *testing.T) {\n\tcd := runtime.ChainDomain()\n\ttests := []struct {\n\t\tfn        string\n\t\targs      []string\n\t\twant      string\n\t\trealm_XXX Realm\n\t}{\n\t\t{\"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"/p/moul/helplink$help\u0026func=foo\u0026bar=1\u0026baz=2\", \"\"},\n\t\t{\"testFunc\", []string{\"key\", \"value\"}, \"/p/moul/helplink$help\u0026func=testFunc\u0026key=value\", \"\"},\n\t\t{\"noArgsFunc\", []string{}, \"/p/moul/helplink$help\u0026func=noArgsFunc\", \"\"},\n\t\t{\"oddArgsFunc\", []string{\"key\"}, \"/p/moul/helplink$help\u0026func=oddArgsFunc\u0026error=odd+number+of+arguments\", \"\"},\n\t\t{\"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"/r/lorem/ipsum$help\u0026func=foo\u0026bar=1\u0026baz=2\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"testFunc\", []string{\"key\", \"value\"}, \"/r/lorem/ipsum$help\u0026func=testFunc\u0026key=value\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"noArgsFunc\", []string{}, \"/r/lorem/ipsum$help\u0026func=noArgsFunc\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"oddArgsFunc\", []string{\"key\"}, \"/r/lorem/ipsum$help\u0026func=oddArgsFunc\u0026error=odd+number+of+arguments\", Realm(cd + \"/r/lorem/ipsum\")},\n\t\t{\"foo\", []string{\"bar\", \"1\", \"baz\", \"2\"}, \"https://gno.world/r/lorem/ipsum$help\u0026func=foo\u0026bar=1\u0026baz=2\", \"gno.world/r/lorem/ipsum\"},\n\t\t{\"testFunc\", []string{\"key\", \"value\"}, \"https://gno.world/r/lorem/ipsum$help\u0026func=testFunc\u0026key=value\", \"gno.world/r/lorem/ipsum\"},\n\t\t{\"noArgsFunc\", []string{}, \"https://gno.world/r/lorem/ipsum$help\u0026func=noArgsFunc\", \"gno.world/r/lorem/ipsum\"},\n\t\t{\"oddArgsFunc\", []string{\"key\"}, \"https://gno.world/r/lorem/ipsum$help\u0026func=oddArgsFunc\u0026error=odd+number+of+arguments\", \"gno.world/r/lorem/ipsum\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\ttitle := tt.fn\n\t\tt.Run(title, func(t *testing.T) {\n\t\t\tif tt.fn == \"oddArgsFunc\" {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tif r != \"odd number of arguments\" {\n\t\t\t\t\t\t\tt.Errorf(\"expected panic with message 'odd number of arguments', got: %v\", r)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tt.Error(\"expected panic for odd number of arguments, but did not panic\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\t\t\tgot := tt.realm_XXX.FuncURL(tt.fn, tt.args...)\n\t\t\turequire.Equal(t, tt.want, got)\n\t\t})\n\t}\n}\n\nfunc TestHome(t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test/helplink\"))\n\tcd := runtime.ChainDomain()\n\ttests := []struct {\n\t\trealm_XXX Realm\n\t\twant      string\n\t}{\n\t\t{\"\", \"/r/test/helplink$help\"},\n\t\t{Realm(cd + \"/r/lorem/ipsum\"), \"/r/lorem/ipsum$help\"},\n\t\t{\"gno.world/r/lorem/ipsum\", \"https://gno.world/r/lorem/ipsum$help\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(string(tt.realm_XXX), func(t *testing.T) {\n\t\t\tgot := tt.realm_XXX.Home()\n\t\t\turequire.Equal(t, tt.want, got)\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"4DC5ZWLFZgr1AlRERTHfH/hyzSEyokqc8L2k9lX05hOzp1e0ouOpyCxU5w+E2m5i0Dv6OUjefNesavFrTUH3Bg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"mdtable","path":"gno.land/p/moul/mdtable","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/mdtable\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"mdtable.gno","body":"// Package mdtable provides a simple way to create Markdown tables.\n//\n// Example usage:\n//\n//\timport \"gno.land/p/moul/mdtable\"\n//\n//\tfunc Render(path string) string {\n//\t    table := mdtable.Table{\n//\t        Headers: []string{\"ID\", \"Title\", \"Status\", \"Date\"},\n//\t    }\n//\t    table.Append([]string{\"#1\", \"Add a new validator\", \"succeed\", \"2024-01-01\"})\n//\t    table.Append([]string{\"#2\", \"Change parameter\", \"timed out\", \"2024-01-02\"})\n//\t    return table.String()\n//\t}\n//\n// Output:\n//\n//\t| ID | Title | Status | Date |\n//\t| --- | --- | --- | --- |\n//\t| #1 | Add a new validator | succeed | 2024-01-01 |\n//\t| #2 | Change parameter | timed out | 2024-01-02 |\npackage mdtable\n\nimport (\n\t\"strings\"\n)\n\ntype Table struct {\n\tHeaders []string\n\tRows    [][]string\n\t// XXX: optional headers alignment.\n}\n\nfunc (t *Table) Append(row []string) {\n\tt.Rows = append(t.Rows, row)\n}\n\nfunc (t Table) String() string {\n\t// XXX: switch to using text/tabwriter when porting to Gno to support\n\t// better-formatted raw Markdown output.\n\n\tif len(t.Headers) == 0 \u0026\u0026 len(t.Rows) == 0 {\n\t\treturn \"\"\n\t}\n\n\tvar sb strings.Builder\n\n\tif len(t.Headers) == 0 {\n\t\tt.Headers = make([]string, len(t.Rows[0]))\n\t}\n\n\t// Print header.\n\tsb.WriteString(\"| \" + strings.Join(t.Headers, \" | \") + \" |\\n\")\n\tsb.WriteString(\"|\" + strings.Repeat(\" --- |\", len(t.Headers)) + \"\\n\")\n\n\t// Print rows.\n\tfor _, row := range t.Rows {\n\t\tescapedRow := make([]string, len(row))\n\t\tfor i, cell := range row {\n\t\t\tescapedRow[i] = strings.ReplaceAll(cell, \"|\", \"\u0026#124;\") // Escape pipe characters.\n\t\t}\n\t\tsb.WriteString(\"| \" + strings.Join(escapedRow, \" | \") + \" |\\n\")\n\t}\n\n\treturn sb.String()\n}\n"},{"name":"mdtable_test.gno","body":"package mdtable_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// XXX: switch to `func Example() {}` when supported.\nfunc TestExample(t *testing.T) {\n\ttable := mdtable.Table{\n\t\tHeaders: []string{\"ID\", \"Title\", \"Status\"},\n\t\tRows: [][]string{\n\t\t\t{\"#1\", \"Add a new validator\", \"succeed\"},\n\t\t\t{\"#2\", \"Change parameter\", \"timed out\"},\n\t\t\t{\"#3\", \"Fill pool\", \"active\"},\n\t\t},\n\t}\n\n\tgot := table.String()\n\texpected := `| ID | Title | Status |\n| --- | --- | --- |\n| #1 | Add a new validator | succeed |\n| #2 | Change parameter | timed out |\n| #3 | Fill pool | active |\n`\n\n\turequire.Equal(t, got, expected)\n}\n\nfunc TestTableString(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\ttable    mdtable.Table\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname: \"With Headers and Rows\",\n\t\t\ttable: mdtable.Table{\n\t\t\t\tHeaders: []string{\"ID\", \"Title\", \"Status\", \"Date\"},\n\t\t\t\tRows: [][]string{\n\t\t\t\t\t{\"#1\", \"Add a new validator\", \"succeed\", \"2024-01-01\"},\n\t\t\t\t\t{\"#2\", \"Change parameter\", \"timed out\", \"2024-01-02\"},\n\t\t\t\t},\n\t\t\t},\n\t\t\texpected: `| ID | Title | Status | Date |\n| --- | --- | --- | --- |\n| #1 | Add a new validator | succeed | 2024-01-01 |\n| #2 | Change parameter | timed out | 2024-01-02 |\n`,\n\t\t},\n\t\t{\n\t\t\tname: \"Without Headers\",\n\t\t\ttable: mdtable.Table{\n\t\t\t\tRows: [][]string{\n\t\t\t\t\t{\"#1\", \"Add a new validator\", \"succeed\", \"2024-01-01\"},\n\t\t\t\t\t{\"#2\", \"Change parameter\", \"timed out\", \"2024-01-02\"},\n\t\t\t\t},\n\t\t\t},\n\t\t\texpected: `|  |  |  |  |\n| --- | --- | --- | --- |\n| #1 | Add a new validator | succeed | 2024-01-01 |\n| #2 | Change parameter | timed out | 2024-01-02 |\n`,\n\t\t},\n\t\t{\n\t\t\tname: \"Without Rows\",\n\t\t\ttable: mdtable.Table{\n\t\t\t\tHeaders: []string{\"ID\", \"Title\", \"Status\", \"Date\"},\n\t\t\t},\n\t\t\texpected: `| ID | Title | Status | Date |\n| --- | --- | --- | --- |\n`,\n\t\t},\n\t\t{\n\t\t\tname: \"With Pipe Character in Content\",\n\t\t\ttable: mdtable.Table{\n\t\t\t\tHeaders: []string{\"ID\", \"Title\", \"Status\", \"Date\"},\n\t\t\t\tRows: [][]string{\n\t\t\t\t\t{\"#1\", \"Add a new | validator\", \"succeed\", \"2024-01-01\"},\n\t\t\t\t\t{\"#2\", \"Change parameter\", \"timed out\", \"2024-01-02\"},\n\t\t\t\t},\n\t\t\t},\n\t\t\texpected: `| ID | Title | Status | Date |\n| --- | --- | --- | --- |\n| #1 | Add a new \u0026#124; validator | succeed | 2024-01-01 |\n| #2 | Change parameter | timed out | 2024-01-02 |\n`,\n\t\t},\n\t\t{\n\t\t\tname: \"With Varying Row Sizes\", // XXX: should we have a different behavior?\n\t\t\ttable: mdtable.Table{\n\t\t\t\tHeaders: []string{\"ID\", \"Title\"},\n\t\t\t\tRows: [][]string{\n\t\t\t\t\t{\"#1\", \"Add a new validator\"},\n\t\t\t\t\t{\"#2\", \"Change parameter\", \"Extra Column\"},\n\t\t\t\t\t{\"#3\", \"Fill pool\"},\n\t\t\t\t},\n\t\t\t},\n\t\t\texpected: `| ID | Title |\n| --- | --- |\n| #1 | Add a new validator |\n| #2 | Change parameter | Extra Column |\n| #3 | Fill pool |\n`,\n\t\t},\n\t\t{\n\t\t\tname: \"With UTF-8 Characters\",\n\t\t\ttable: mdtable.Table{\n\t\t\t\tHeaders: []string{\"ID\", \"Title\", \"Status\", \"Date\"},\n\t\t\t\tRows: [][]string{\n\t\t\t\t\t{\"#1\", \"Café\", \"succeed\", \"2024-01-01\"},\n\t\t\t\t\t{\"#2\", \"München\", \"timed out\", \"2024-01-02\"},\n\t\t\t\t\t{\"#3\", \"São Paulo\", \"active\", \"2024-01-03\"},\n\t\t\t\t},\n\t\t\t},\n\t\t\texpected: `| ID | Title | Status | Date |\n| --- | --- | --- | --- |\n| #1 | Café | succeed | 2024-01-01 |\n| #2 | München | timed out | 2024-01-02 |\n| #3 | São Paulo | active | 2024-01-03 |\n`,\n\t\t},\n\t\t{\n\t\t\tname:     \"With no Headers and no Rows\",\n\t\t\ttable:    mdtable.Table{},\n\t\t\texpected: ``,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := tt.table.String()\n\t\t\turequire.Equal(t, got, tt.expected)\n\t\t})\n\t}\n}\n\nfunc TestTableAppend(t *testing.T) {\n\ttable := mdtable.Table{\n\t\tHeaders: []string{\"ID\", \"Title\", \"Status\", \"Date\"},\n\t}\n\n\t// Use the Append method to add rows to the table\n\ttable.Append([]string{\"#1\", \"Add a new validator\", \"succeed\", \"2024-01-01\"})\n\ttable.Append([]string{\"#2\", \"Change parameter\", \"timed out\", \"2024-01-02\"})\n\ttable.Append([]string{\"#3\", \"Fill pool\", \"active\", \"2024-01-03\"})\n\tgot := table.String()\n\n\texpected := `| ID | Title | Status | Date |\n| --- | --- | --- | --- |\n| #1 | Add a new validator | succeed | 2024-01-01 |\n| #2 | Change parameter | timed out | 2024-01-02 |\n| #3 | Fill pool | active | 2024-01-03 |\n`\n\turequire.Equal(t, got, expected)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Fxi+eGIBCmy7x3ZIaf3wCYasJMnRxkgkIDEFVh5ZxwzenY0fh6l1ZTH34zCE8uVDwBXgHBBSmebKJL96NgPDAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"realmpath","path":"gno.land/p/moul/realmpath","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/realmpath\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"realmpath.gno","body":"// Package realmpath is a lightweight Render.path parsing and link generation\n// library with an idiomatic API, closely resembling that of net/url.\n//\n// This package provides utilities for parsing request paths and query\n// parameters, allowing you to extract path segments and manipulate query\n// values.\n//\n// Example usage:\n//\n//\timport \"gno.land/p/moul/realmpath\"\n//\n//\tfunc Render(path string) string {\n//\t    // Parsing a sample path with query parameters\n//\t    path = \"hello/world?foo=bar\u0026baz=foobar\"\n//\t    req := realmpath.Parse(path)\n//\n//\t    // Accessing parsed path and query parameters\n//\t    println(req.Path)             // Output: hello/world\n//\t    println(req.PathPart(0))      // Output: hello\n//\t    println(req.PathPart(1))      // Output: world\n//\t    println(req.Query.Get(\"foo\")) // Output: bar\n//\t    println(req.Query.Get(\"baz\")) // Output: foobar\n//\n//\t    // Rebuilding the URL\n//\t    println(req.String())         // Output: /r/current/realm:hello/world?baz=foobar\u0026foo=bar\n//\t}\npackage realmpath\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"net/url\"\n\t\"strings\"\n)\n\nvar chainDomain = runtime.ChainDomain()\n\n// Request represents a parsed request.\ntype Request struct {\n\tPath  string     // The path of the request\n\tQuery url.Values // The parsed query parameters\n\tRealm string     // The realm associated with the request\n}\n\n// Parse takes a raw path string and returns a Request object.\n// It splits the path into its components and parses any query parameters.\nfunc Parse(rawPath string) *Request {\n\t// Split the raw path into path and query components\n\tpath, query := splitPathAndQuery(rawPath)\n\n\t// Parse the query string into url.Values\n\tqueryValues, _ := url.ParseQuery(query)\n\n\treturn \u0026Request{\n\t\tPath:  path,        // Set the path\n\t\tQuery: queryValues, // Set the parsed query values\n\t}\n}\n\n// PathParts returns the segments of the path as a slice of strings.\n// It trims leading and trailing slashes and splits the path by slashes.\nfunc (r *Request) PathParts() []string {\n\treturn strings.Split(strings.Trim(r.Path, \"/\"), \"/\")\n}\n\n// PathPart returns the specified part of the path.\n// If the index is out of bounds, it returns an empty string.\nfunc (r *Request) PathPart(index int) string {\n\tparts := r.PathParts() // Get the path segments\n\tif index \u003c 0 || index \u003e= len(parts) {\n\t\treturn \"\" // Return empty if index is out of bounds\n\t}\n\treturn parts[index] // Return the specified path part\n}\n\n// String rebuilds the URL from the path and query values.\n// If the Realm is not set, it automatically retrieves the current realm path.\n//\n// SECURITY (Class-2-shaped, intentionally accepted): unsafe.CurrentRealm()\n// inside a non-crossing /p/ method is .Title()-vulnerable — it walks past\n// non-crossing frames to the most-recent crossing ancestor, not the\n// immediate caller. The lazily-captured r.Realm is therefore NOT a reliable\n// identity claim under a contrived call chain.\n//\n// This is acceptable here because r.Realm is consumed only as a URL string\n// for rendering (the line below builds reconstructedPath for display); no\n// caller in /examples uses it for authorization. If you add a new consumer\n// that gates writes/auth on r.Realm, replace the lazy fill with an explicit\n// `req.Realm = cur.PkgPath()` set by the caller under rlm.IsCurrent(), or\n// switch to a sibling method that takes a realm parameter — see\n// docs/resources/gno-security.md for the threat-class taxonomy.\nfunc (r *Request) String() string {\n\t// Automatically set the Realm if it is not already defined\n\tif r.Realm == \"\" {\n\t\tr.Realm = unsafe.CurrentRealm().PkgPath() // Get the current realm path\n\t}\n\n\t// Rebuild the path using the realm and path parts\n\trelativePkgPath := strings.TrimPrefix(r.Realm, chainDomain) // Trim the chain domain prefix\n\treconstructedPath := relativePkgPath + \":\" + strings.Join(r.PathParts(), \"/\")\n\n\t// Rebuild the query string\n\tqueryString := r.Query.Encode() // Encode the query parameters\n\tif queryString != \"\" {\n\t\treturn reconstructedPath + \"?\" + queryString // Return the full URL with query\n\t}\n\treturn reconstructedPath // Return the path without query parameters\n}\n\nfunc splitPathAndQuery(rawPath string) (string, string) {\n\tif idx := strings.Index(rawPath, \"?\"); idx != -1 {\n\t\treturn rawPath[:idx], rawPath[idx+1:] // Split at the first '?' found\n\t}\n\treturn rawPath, \"\" // No query string present\n}\n"},{"name":"realmpath_test.gno","body":"package realmpath_test\n\nimport (\n\t\"chain/runtime\"\n\t\"net/url\"\n\t\"testing\"\n\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestExample(t *testing.T) {\n\tcd := runtime.ChainDomain()\n\ttesting.SetRealm(testing.NewCodeRealm(cd + \"/r/lorem/ipsum\"))\n\n\t// initial parsing\n\tpath := \"hello/world?foo=bar\u0026baz=foobar\"\n\treq := realmpath.Parse(path)\n\turequire.False(t, req == nil, \"req should not be nil\")\n\tuassert.Equal(t, req.Path, \"hello/world\")\n\tuassert.Equal(t, req.Query.Get(\"foo\"), \"bar\")\n\tuassert.Equal(t, req.Query.Get(\"baz\"), \"foobar\")\n\tuassert.Equal(t, req.String(), \"/r/lorem/ipsum:hello/world?baz=foobar\u0026foo=bar\")\n\n\t// alter query\n\treq.Query.Set(\"hey\", \"salut\")\n\tuassert.Equal(t, req.String(), \"/r/lorem/ipsum:hello/world?baz=foobar\u0026foo=bar\u0026hey=salut\")\n\n\t// alter path\n\treq.Path = \"bye/ciao\"\n\tuassert.Equal(t, req.String(), \"/r/lorem/ipsum:bye/ciao?baz=foobar\u0026foo=bar\u0026hey=salut\")\n}\n\nfunc TestParse(t *testing.T) {\n\tcd := runtime.ChainDomain()\n\ttesting.SetRealm(testing.NewCodeRealm(cd + \"/r/lorem/ipsum\"))\n\n\ttests := []struct {\n\t\trawPath        string\n\t\trealm_XXX      string // optional\n\t\texpectedPath   string\n\t\texpectedQuery  url.Values\n\t\texpectedString string\n\t}{\n\t\t{\n\t\t\trawPath:      \"hello/world?foo=bar\u0026baz=foobar\",\n\t\t\texpectedPath: \"hello/world\",\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"foo\": []string{\"bar\"},\n\t\t\t\t\"baz\": []string{\"foobar\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/lorem/ipsum:hello/world?baz=foobar\u0026foo=bar\",\n\t\t},\n\t\t{\n\t\t\trawPath:      \"api/v1/resource?search=test\u0026limit=10\",\n\t\t\texpectedPath: \"api/v1/resource\",\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"search\": []string{\"test\"},\n\t\t\t\t\"limit\":  []string{\"10\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/lorem/ipsum:api/v1/resource?limit=10\u0026search=test\",\n\t\t},\n\t\t{\n\t\t\trawPath:        \"singlepath\",\n\t\t\texpectedPath:   \"singlepath\",\n\t\t\texpectedQuery:  url.Values{},\n\t\t\texpectedString: \"/r/lorem/ipsum:singlepath\",\n\t\t},\n\t\t{\n\t\t\trawPath:        \"path/with/trailing/slash/\",\n\t\t\texpectedPath:   \"path/with/trailing/slash/\",\n\t\t\texpectedQuery:  url.Values{},\n\t\t\texpectedString: \"/r/lorem/ipsum:path/with/trailing/slash\",\n\t\t},\n\t\t{\n\t\t\trawPath:        \"emptyquery?\",\n\t\t\texpectedPath:   \"emptyquery\",\n\t\t\texpectedQuery:  url.Values{},\n\t\t\texpectedString: \"/r/lorem/ipsum:emptyquery\",\n\t\t},\n\t\t{\n\t\t\trawPath:      \"path/with/special/characters/?key=val%20ue\u0026anotherKey=with%21special%23chars\",\n\t\t\texpectedPath: \"path/with/special/characters/\",\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"key\":        []string{\"val ue\"},\n\t\t\t\t\"anotherKey\": []string{\"with!special#chars\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/lorem/ipsum:path/with/special/characters?anotherKey=with%21special%23chars\u0026key=val+ue\",\n\t\t},\n\t\t{\n\t\t\trawPath:      \"path/with/empty/key?keyEmpty\u0026=valueEmpty\",\n\t\t\texpectedPath: \"path/with/empty/key\",\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"keyEmpty\": []string{\"\"},\n\t\t\t\t\"\":         []string{\"valueEmpty\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/lorem/ipsum:path/with/empty/key?=valueEmpty\u0026keyEmpty=\",\n\t\t},\n\t\t{\n\t\t\trawPath:      \"path/with/multiple/empty/keys?=empty1\u0026=empty2\",\n\t\t\texpectedPath: \"path/with/multiple/empty/keys\",\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"\": []string{\"empty1\", \"empty2\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/lorem/ipsum:path/with/multiple/empty/keys?=empty1\u0026=empty2\",\n\t\t},\n\t\t{\n\t\t\trawPath:      \"path/with/percent-encoded/%20space?query=hello%20world\",\n\t\t\texpectedPath: \"path/with/percent-encoded/%20space\", // XXX: should we decode?\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"query\": []string{\"hello world\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/lorem/ipsum:path/with/percent-encoded/%20space?query=hello+world\",\n\t\t},\n\t\t{\n\t\t\trawPath:      \"path/with/very/long/query?key1=value1\u0026key2=value2\u0026key3=value3\u0026key4=value4\u0026key5=value5\u0026key6=value6\",\n\t\t\texpectedPath: \"path/with/very/long/query\",\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"key1\": []string{\"value1\"},\n\t\t\t\t\"key2\": []string{\"value2\"},\n\t\t\t\t\"key3\": []string{\"value3\"},\n\t\t\t\t\"key4\": []string{\"value4\"},\n\t\t\t\t\"key5\": []string{\"value5\"},\n\t\t\t\t\"key6\": []string{\"value6\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/lorem/ipsum:path/with/very/long/query?key1=value1\u0026key2=value2\u0026key3=value3\u0026key4=value4\u0026key5=value5\u0026key6=value6\",\n\t\t},\n\t\t{\n\t\t\trawPath:      \"custom/realm?foo=bar\u0026baz=foobar\",\n\t\t\trealm_XXX:    cd + \"/r/foo/bar\",\n\t\t\texpectedPath: \"custom/realm\",\n\t\t\texpectedQuery: url.Values{\n\t\t\t\t\"foo\": []string{\"bar\"},\n\t\t\t\t\"baz\": []string{\"foobar\"},\n\t\t\t},\n\t\t\texpectedString: \"/r/foo/bar:custom/realm?baz=foobar\u0026foo=bar\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.rawPath, func(t *testing.T) {\n\t\t\treq := realmpath.Parse(tt.rawPath)\n\t\t\treq.Realm = tt.realm_XXX // set optional realm\n\t\t\turequire.False(t, req == nil, \"req should not be nil\")\n\t\t\tuassert.Equal(t, req.Path, tt.expectedPath)\n\t\t\turequire.Equal(t, len(req.Query), len(tt.expectedQuery))\n\t\t\tuassert.Equal(t, req.Query.Encode(), tt.expectedQuery.Encode())\n\t\t\t// XXX: uassert.Equal(t, req.Query, tt.expectedQuery)\n\t\t\tuassert.Equal(t, req.String(), tt.expectedString)\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"F5xoJDLOMFqbuaDRIrKznBb3YPFjUM+jWY4DLo0L0jl3GZzCJ4QlC2d7FnNR154tz7P7HXKFFvwdbJTAEnwsDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"pager","path":"gno.land/p/nt/bptree/v0/pager","files":[{"name":"example_test.gno","body":"package pager\n\nimport (\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc ExamplePager() {\n\t// Create a new tree and populate it with some key-value pairs.\n\tvar id seqid.ID\n\ttree := bptree.NewBPTree32()\n\tfor i := 0; i \u003c 42; i++ {\n\t\ttree.Set(id.Next().String(), i)\n\t}\n\n\t// Create a new pager.\n\tpager1 := NewPager(tree, 7, false)\n\n\tfor pn := -1; pn \u003c 8; pn++ {\n\t\tpage := pager1.GetPage(pn)\n\n\t\tprintln(ufmt.Sprintf(\"## Page %d of %d\", page.PageNumber, page.TotalPages))\n\t\tfor idx, item := range page.Items {\n\t\t\tprintln(ufmt.Sprintf(\"- idx=%d key=%s value=%d\", idx, item.Key, item.Value))\n\t\t}\n\t\tprintln(page.Picker(\"/\"))\n\t\tprintln()\n\t}\n\n\t// Output:\n\t// ## Page 0 of 6\n\t// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)\n\t//\n\t// ## Page 0 of 6\n\t// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)\n\t//\n\t// ## Page 1 of 6\n\t// - idx=0 key=0000001 value=0\n\t// - idx=1 key=0000002 value=1\n\t// - idx=2 key=0000003 value=2\n\t// - idx=3 key=0000004 value=3\n\t// - idx=4 key=0000005 value=4\n\t// - idx=5 key=0000006 value=5\n\t// - idx=6 key=0000007 value=6\n\t// **1** | [2](?page=2) | [3](?page=3) | … | [6](?page=6)\n\t//\n\t// ## Page 2 of 6\n\t// - idx=0 key=0000008 value=7\n\t// - idx=1 key=0000009 value=8\n\t// - idx=2 key=000000a value=9\n\t// - idx=3 key=000000b value=10\n\t// - idx=4 key=000000c value=11\n\t// - idx=5 key=000000d value=12\n\t// - idx=6 key=000000e value=13\n\t// [1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | … | [6](?page=6)\n\t//\n\t// ## Page 3 of 6\n\t// - idx=0 key=000000f value=14\n\t// - idx=1 key=000000g value=15\n\t// - idx=2 key=000000h value=16\n\t// - idx=3 key=000000j value=17\n\t// - idx=4 key=000000k value=18\n\t// - idx=5 key=000000m value=19\n\t// - idx=6 key=000000n value=20\n\t// [1](?page=1) | [2](?page=2) | **3** | [4](?page=4) | [5](?page=5) | [6](?page=6)\n\t//\n\t// ## Page 4 of 6\n\t// - idx=0 key=000000p value=21\n\t// - idx=1 key=000000q value=22\n\t// - idx=2 key=000000r value=23\n\t// - idx=3 key=000000s value=24\n\t// - idx=4 key=000000t value=25\n\t// - idx=5 key=000000v value=26\n\t// - idx=6 key=000000w value=27\n\t// [1](?page=1) | [2](?page=2) | [3](?page=3) | **4** | [5](?page=5) | [6](?page=6)\n\t//\n\t// ## Page 5 of 6\n\t// - idx=0 key=000000x value=28\n\t// - idx=1 key=000000y value=29\n\t// - idx=2 key=000000z value=30\n\t// - idx=3 key=0000010 value=31\n\t// - idx=4 key=0000011 value=32\n\t// - idx=5 key=0000012 value=33\n\t// - idx=6 key=0000013 value=34\n\t// [1](?page=1) | … | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6)\n\t//\n\t// ## Page 6 of 6\n\t// - idx=0 key=0000014 value=35\n\t// - idx=1 key=0000015 value=36\n\t// - idx=2 key=0000016 value=37\n\t// - idx=3 key=0000017 value=38\n\t// - idx=4 key=0000018 value=39\n\t// - idx=5 key=0000019 value=40\n\t// - idx=6 key=000001a value=41\n\t// [1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6**\n\t//\n\t// ## Page 7 of 6\n\t// [1](?page=1) | … | [5](?page=5) | [6](?page=6) | _7_\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/bptree/v0/pager\"\ngno = \"0.9\"\n"},{"name":"pager.gno","body":"package pager\n\nimport (\n\t\"math\"\n\t\"net/url\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/bptree/v0/rotree\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Pager is a struct that holds the B+ tree and pagination parameters.\ntype Pager struct {\n\tTree            rotree.IReadOnlyTree\n\tPageQueryParam  string\n\tSizeQueryParam  string\n\tDefaultPageSize int\n\tReversed        bool\n}\n\n// Page represents a single page of results.\ntype Page struct {\n\tItems      []Item\n\tPageNumber int\n\tPageSize   int\n\tTotalItems int\n\tTotalPages int\n\tHasPrev    bool\n\tHasNext    bool\n\tPager      *Pager // Reference to the parent Pager\n}\n\n// Item represents a key-value pair in the B+ tree.\ntype Item struct {\n\tKey   string\n\tValue any\n}\n\n// NewPager creates a new Pager with default values.\nfunc NewPager(tree rotree.IReadOnlyTree, defaultPageSize int, reversed bool) *Pager {\n\treturn \u0026Pager{\n\t\tTree:            tree,\n\t\tPageQueryParam:  \"page\",\n\t\tSizeQueryParam:  \"size\",\n\t\tDefaultPageSize: defaultPageSize,\n\t\tReversed:        reversed,\n\t}\n}\n\n// GetPage retrieves a page of results from the B+ tree.\nfunc (p *Pager) GetPage(pageNumber int) *Page {\n\treturn p.GetPageWithSize(pageNumber, p.DefaultPageSize)\n}\n\nfunc (p *Pager) GetPageWithSize(pageNumber, pageSize int) *Page {\n\ttotalItems := p.Tree.Size()\n\ttotalPages := int(math.Ceil(float64(totalItems) / float64(pageSize)))\n\n\tpage := \u0026Page{\n\t\tTotalItems: totalItems,\n\t\tTotalPages: totalPages,\n\t\tPageSize:   pageSize,\n\t\tPager:      p,\n\t}\n\n\t// pages without content\n\tif pageSize \u003c 1 {\n\t\treturn page\n\t}\n\n\t// page number provided is not available\n\tif pageNumber \u003c 1 {\n\t\tpage.HasNext = totalPages \u003e 0\n\t\treturn page\n\t}\n\n\t// page number provided is outside the range of total pages\n\tif pageNumber \u003e totalPages {\n\t\tpage.PageNumber = pageNumber\n\t\tpage.HasPrev = pageNumber \u003e 0\n\t\treturn page\n\t}\n\n\tstartIndex := (pageNumber - 1) * pageSize\n\tendIndex := startIndex + pageSize\n\tif endIndex \u003e totalItems {\n\t\tendIndex = totalItems\n\t}\n\n\titems := []Item{}\n\n\tif p.Reversed {\n\t\tp.Tree.ReverseIterateByOffset(startIndex, endIndex-startIndex, func(key string, value any) bool {\n\t\t\titems = append(items, Item{Key: key, Value: value})\n\t\t\treturn false\n\t\t})\n\t} else {\n\t\tp.Tree.IterateByOffset(startIndex, endIndex-startIndex, func(key string, value any) bool {\n\t\t\titems = append(items, Item{Key: key, Value: value})\n\t\t\treturn false\n\t\t})\n\t}\n\n\tpage.Items = items\n\tpage.PageNumber = pageNumber\n\tpage.HasPrev = pageNumber \u003e 1\n\tpage.HasNext = pageNumber \u003c totalPages\n\treturn page\n}\n\nfunc (p *Pager) MustGetPageByPath(rawURL string) *Page {\n\tpage, err := p.GetPageByPath(rawURL)\n\tif err != nil {\n\t\tpanic(\"invalid path\")\n\t}\n\treturn page\n}\n\n// GetPageByPath retrieves a page of results based on the query parameters in the URL path.\nfunc (p *Pager) GetPageByPath(rawURL string) (*Page, error) {\n\tpageNumber, pageSize, err := p.ParseQuery(rawURL)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn p.GetPageWithSize(pageNumber, pageSize), nil\n}\n\n// Picker generates the Markdown UI for the page Picker\nfunc (p *Page) Picker(path string) string {\n\tpageNumber := p.PageNumber\n\tpageNumber = max(pageNumber, 1)\n\n\tif p.TotalPages \u003c= 1 {\n\t\treturn \"\"\n\t}\n\n\tu, _ := url.Parse(path)\n\tquery := u.Query()\n\n\t// Remove existing page query parameter\n\tquery.Del(p.Pager.PageQueryParam)\n\n\t// Encode remaining query parameters\n\tbaseQuery := query.Encode()\n\tif baseQuery != \"\" {\n\t\tbaseQuery = \"\u0026\" + baseQuery\n\t}\n\tmd := \"\"\n\n\tif p.HasPrev {\n\t\tmd += ufmt.Sprintf(\"[%d](?%s=%d%s) | \", 1, p.Pager.PageQueryParam, 1, baseQuery)\n\n\t\tif p.PageNumber \u003e 4 {\n\t\t\tmd += \"… | \"\n\t\t}\n\n\t\tif p.PageNumber \u003e 3 {\n\t\t\tmd += ufmt.Sprintf(\"[%d](?%s=%d%s) | \", p.PageNumber-2, p.Pager.PageQueryParam, p.PageNumber-2, baseQuery)\n\t\t}\n\n\t\tif p.PageNumber \u003e 2 {\n\t\t\tmd += ufmt.Sprintf(\"[%d](?%s=%d%s) | \", p.PageNumber-1, p.Pager.PageQueryParam, p.PageNumber-1, baseQuery)\n\t\t}\n\t}\n\n\tif p.PageNumber \u003e 0 \u0026\u0026 p.PageNumber \u003c= p.TotalPages {\n\t\tmd += ufmt.Sprintf(\"**%d**\", p.PageNumber)\n\t} else {\n\t\tmd += ufmt.Sprintf(\"_%d_\", p.PageNumber)\n\t}\n\n\tif p.HasNext {\n\t\tif p.PageNumber \u003c p.TotalPages-1 {\n\t\t\tmd += ufmt.Sprintf(\" | [%d](?%s=%d%s)\", p.PageNumber+1, p.Pager.PageQueryParam, p.PageNumber+1, baseQuery)\n\t\t}\n\n\t\tif p.PageNumber \u003c p.TotalPages-2 {\n\t\t\tmd += ufmt.Sprintf(\" | [%d](?%s=%d%s)\", p.PageNumber+2, p.Pager.PageQueryParam, p.PageNumber+2, baseQuery)\n\t\t}\n\n\t\tif p.PageNumber \u003c p.TotalPages-3 {\n\t\t\tmd += \" | …\"\n\t\t}\n\n\t\tmd += ufmt.Sprintf(\" | [%d](?%s=%d%s)\", p.TotalPages, p.Pager.PageQueryParam, p.TotalPages, baseQuery)\n\t}\n\n\treturn md\n}\n\n// ParseQuery parses the URL to extract the page number and page size.\nfunc (p *Pager) ParseQuery(rawURL string) (int, int, error) {\n\tu, err := url.Parse(rawURL)\n\tif err != nil {\n\t\treturn 1, p.DefaultPageSize, err\n\t}\n\n\tquery := u.Query()\n\tpageNumber := 1\n\tpageSize := p.DefaultPageSize\n\n\tif p.PageQueryParam != \"\" {\n\t\tif pageStr := query.Get(p.PageQueryParam); pageStr != \"\" {\n\t\t\tpageNumber, err = strconv.Atoi(pageStr)\n\t\t\tif err != nil || pageNumber \u003c 1 {\n\t\t\t\tpageNumber = 1\n\t\t\t}\n\t\t}\n\t}\n\n\tif p.SizeQueryParam != \"\" {\n\t\tif sizeStr := query.Get(p.SizeQueryParam); sizeStr != \"\" {\n\t\t\tpageSize, err = strconv.Atoi(sizeStr)\n\t\t\tif err != nil || pageSize \u003c 1 {\n\t\t\t\tpageSize = p.DefaultPageSize\n\t\t\t}\n\t\t}\n\t}\n\n\treturn pageNumber, pageSize, nil\n}\n\nfunc max(a, b int) int {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n"},{"name":"pager_test.gno","body":"package pager\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestPager_GetPage(t *testing.T) {\n\t// Create a new B+ tree and populate it with some key-value pairs.\n\ttree := bptree.NewBPTree32()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\tt.Run(\"normal ordering\", func(t *testing.T) {\n\t\t// Create a new pager.\n\t\tpager := NewPager(tree, 10, false)\n\n\t\t// Define test cases.\n\t\ttests := []struct {\n\t\t\tpageNumber int\n\t\t\tpageSize   int\n\t\t\texpected   []Item\n\t\t}{\n\t\t\t{1, 2, []Item{{Key: \"a\", Value: 1}, {Key: \"b\", Value: 2}}},\n\t\t\t{2, 2, []Item{{Key: \"c\", Value: 3}, {Key: \"d\", Value: 4}}},\n\t\t\t{3, 2, []Item{{Key: \"e\", Value: 5}}},\n\t\t\t{1, 3, []Item{{Key: \"a\", Value: 1}, {Key: \"b\", Value: 2}, {Key: \"c\", Value: 3}}},\n\t\t\t{2, 3, []Item{{Key: \"d\", Value: 4}, {Key: \"e\", Value: 5}}},\n\t\t\t{1, 5, []Item{{Key: \"a\", Value: 1}, {Key: \"b\", Value: 2}, {Key: \"c\", Value: 3}, {Key: \"d\", Value: 4}, {Key: \"e\", Value: 5}}},\n\t\t\t{2, 5, []Item{}},\n\t\t}\n\n\t\tfor _, tt := range tests {\n\t\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\t\tuassert.Equal(t, len(tt.expected), len(page.Items))\n\n\t\t\tfor i, item := range page.Items {\n\t\t\t\tuassert.Equal(t, tt.expected[i].Key, item.Key)\n\t\t\t\tuassert.Equal(t, tt.expected[i].Value, item.Value)\n\t\t\t}\n\t\t}\n\t})\n\n\tt.Run(\"reversed ordering\", func(t *testing.T) {\n\t\t// Create a new pager.\n\t\tpager := NewPager(tree, 10, true)\n\n\t\t// Define test cases.\n\t\ttests := []struct {\n\t\t\tpageNumber int\n\t\t\tpageSize   int\n\t\t\texpected   []Item\n\t\t}{\n\t\t\t{1, 2, []Item{{Key: \"e\", Value: 5}, {Key: \"d\", Value: 4}}},\n\t\t\t{2, 2, []Item{{Key: \"c\", Value: 3}, {Key: \"b\", Value: 2}}},\n\t\t\t{3, 2, []Item{{Key: \"a\", Value: 1}}},\n\t\t\t{1, 3, []Item{{Key: \"e\", Value: 5}, {Key: \"d\", Value: 4}, {Key: \"c\", Value: 3}}},\n\t\t\t{2, 3, []Item{{Key: \"b\", Value: 2}, {Key: \"a\", Value: 1}}},\n\t\t\t{1, 5, []Item{{Key: \"e\", Value: 5}, {Key: \"d\", Value: 4}, {Key: \"c\", Value: 3}, {Key: \"b\", Value: 2}, {Key: \"a\", Value: 1}}},\n\t\t\t{2, 5, []Item{}},\n\t\t}\n\n\t\tfor _, tt := range tests {\n\t\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\t\tuassert.Equal(t, len(tt.expected), len(page.Items))\n\n\t\t\tfor i, item := range page.Items {\n\t\t\t\tuassert.Equal(t, tt.expected[i].Key, item.Key)\n\t\t\t\tuassert.Equal(t, tt.expected[i].Value, item.Value)\n\t\t\t}\n\t\t}\n\t})\n}\n\nfunc TestPager_GetPageByPath(t *testing.T) {\n\t// Create a new B+ tree and populate it with some key-value pairs.\n\ttree := bptree.NewBPTree32()\n\tfor i := 0; i \u003c 50; i++ {\n\t\ttree.Set(ufmt.Sprintf(\"key%d\", i), i)\n\t}\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases.\n\ttests := []struct {\n\t\trawURL       string\n\t\texpectedPage int\n\t\texpectedSize int\n\t}{\n\t\t{\"/r/foo:bar/baz?size=10\u0026page=1\", 1, 10},\n\t\t{\"/r/foo:bar/baz?size=10\u0026page=2\", 2, 10},\n\t\t{\"/r/foo:bar/baz?page=3\", 3, pager.DefaultPageSize},\n\t\t{\"/r/foo:bar/baz?size=20\", 1, 20},\n\t\t{\"/r/foo:bar/baz\", 1, pager.DefaultPageSize},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage, err := pager.GetPageByPath(tt.rawURL)\n\t\turequire.NoError(t, err, ufmt.Sprintf(\"GetPageByPath(%s) returned error: %v\", tt.rawURL, err))\n\n\t\tuassert.Equal(t, tt.expectedPage, page.PageNumber)\n\t\tuassert.Equal(t, tt.expectedSize, page.PageSize)\n\t}\n}\n\nfunc TestPage_Picker(t *testing.T) {\n\t// Create a new B+ tree and populate it with some key-value pairs.\n\ttree := bptree.NewBPTree32()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases.\n\ttests := []struct {\n\t\tpageNumber int\n\t\tpageSize   int\n\t\tpath       string\n\t\texpected   string\n\t}{\n\t\t{1, 2, \"/test\", \"**1** | [2](?page=2) | [3](?page=3)\"},\n\t\t{2, 2, \"/test\", \"[1](?page=1) | **2** | [3](?page=3)\"},\n\t\t{3, 2, \"/test\", \"[1](?page=1) | [2](?page=2) | **3**\"},\n\t\t{1, 2, \"/test?foo=bar\", \"**1** | [2](?page=2\u0026foo=bar) | [3](?page=3\u0026foo=bar)\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\tui := page.Picker(tt.path)\n\t\tuassert.Equal(t, tt.expected, ui)\n\t}\n}\n\nfunc TestPager_UI_WithManyPages(t *testing.T) {\n\t// Create a new B+ tree and populate it with many key-value pairs.\n\ttree := bptree.NewBPTree32()\n\tfor i := 0; i \u003c 100; i++ {\n\t\ttree.Set(ufmt.Sprintf(\"key%d\", i), i)\n\t}\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases for a large number of pages.\n\ttests := []struct {\n\t\tpageNumber int\n\t\tpageSize   int\n\t\tpath       string\n\t\texpected   string\n\t}{\n\t\t{1, 10, \"/test\", \"**1** | [2](?page=2) | [3](?page=3) | … | [10](?page=10)\"},\n\t\t{2, 10, \"/test\", \"[1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | … | [10](?page=10)\"},\n\t\t{3, 10, \"/test\", \"[1](?page=1) | [2](?page=2) | **3** | [4](?page=4) | [5](?page=5) | … | [10](?page=10)\"},\n\t\t{4, 10, \"/test\", \"[1](?page=1) | [2](?page=2) | [3](?page=3) | **4** | [5](?page=5) | [6](?page=6) | … | [10](?page=10)\"},\n\t\t{5, 10, \"/test\", \"[1](?page=1) | … | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6) | [7](?page=7) | … | [10](?page=10)\"},\n\t\t{6, 10, \"/test\", \"[1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6** | [7](?page=7) | [8](?page=8) | … | [10](?page=10)\"},\n\t\t{7, 10, \"/test\", \"[1](?page=1) | … | [5](?page=5) | [6](?page=6) | **7** | [8](?page=8) | [9](?page=9) | [10](?page=10)\"},\n\t\t{8, 10, \"/test\", \"[1](?page=1) | … | [6](?page=6) | [7](?page=7) | **8** | [9](?page=9) | [10](?page=10)\"},\n\t\t{9, 10, \"/test\", \"[1](?page=1) | … | [7](?page=7) | [8](?page=8) | **9** | [10](?page=10)\"},\n\t\t{10, 10, \"/test\", \"[1](?page=1) | … | [8](?page=8) | [9](?page=9) | **10**\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage := pager.GetPageWithSize(tt.pageNumber, tt.pageSize)\n\n\t\tui := page.Picker(tt.path)\n\t\tuassert.Equal(t, tt.expected, ui)\n\t}\n}\n\nfunc TestPager_ParseQuery(t *testing.T) {\n\t// Create a new B+ tree and populate it with some key-value pairs.\n\ttree := bptree.NewBPTree32()\n\ttree.Set(\"a\", 1)\n\ttree.Set(\"b\", 2)\n\ttree.Set(\"c\", 3)\n\ttree.Set(\"d\", 4)\n\ttree.Set(\"e\", 5)\n\n\t// Create a new pager.\n\tpager := NewPager(tree, 10, false)\n\n\t// Define test cases.\n\ttests := []struct {\n\t\trawURL        string\n\t\texpectedPage  int\n\t\texpectedSize  int\n\t\texpectedError bool\n\t}{\n\t\t{\"/r/foo:bar/baz?size=2\u0026page=1\", 1, 2, false},\n\t\t{\"/r/foo:bar/baz?size=3\u0026page=2\", 2, 3, false},\n\t\t{\"/r/foo:bar/baz?size=5\u0026page=3\", 3, 5, false},\n\t\t{\"/r/foo:bar/baz?page=2\", 2, pager.DefaultPageSize, false},\n\t\t{\"/r/foo:bar/baz?size=3\", 1, 3, false},\n\t\t{\"/r/foo:bar/baz\", 1, pager.DefaultPageSize, false},\n\t\t{\"/r/foo:bar/baz?size=0\u0026page=0\", 1, pager.DefaultPageSize, false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tpage, size, err := pager.ParseQuery(tt.rawURL)\n\t\tif tt.expectedError {\n\t\t\tuassert.Error(t, err, ufmt.Sprintf(\"ParseQuery(%s) expected error but got none\", tt.rawURL))\n\t\t} else {\n\t\t\turequire.NoError(t, err, ufmt.Sprintf(\"ParseQuery(%s) returned error: %v\", tt.rawURL, err))\n\t\t\tuassert.Equal(t, tt.expectedPage, page, ufmt.Sprintf(\"ParseQuery(%s) returned page %d, expected %d\", tt.rawURL, page, tt.expectedPage))\n\t\t\tuassert.Equal(t, tt.expectedSize, size, ufmt.Sprintf(\"ParseQuery(%s) returned size %d, expected %d\", tt.rawURL, size, tt.expectedSize))\n\t\t}\n\t}\n}\n\nfunc TestPage_PickerQueryParamPreservation(t *testing.T) {\n\ttree := bptree.NewBPTree32()\n\tfor i := 1; i \u003c= 6; i++ {\n\t\ttree.Set(ufmt.Sprintf(\"key%d\", i), i)\n\t}\n\n\tpager := NewPager(tree, 2, false)\n\n\ttests := []struct {\n\t\tname       string\n\t\tpageNumber int\n\t\tpath       string\n\t\texpected   string\n\t}{\n\t\t{\n\t\t\tname:       \"single query param\",\n\t\t\tpageNumber: 1,\n\t\t\tpath:       \"/test?foo=bar\",\n\t\t\texpected:   \"**1** | [2](?page=2\u0026foo=bar) | [3](?page=3\u0026foo=bar)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"multiple query params\",\n\t\t\tpageNumber: 2,\n\t\t\tpath:       \"/test?foo=bar\u0026baz=qux\",\n\t\t\texpected:   \"[1](?page=1\u0026baz=qux\u0026foo=bar) | **2** | [3](?page=3\u0026baz=qux\u0026foo=bar)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"overwrite existing page param\",\n\t\t\tpageNumber: 1,\n\t\t\tpath:       \"/test?param1=value1\u0026page=999\u0026param2=value2\",\n\t\t\texpected:   \"**1** | [2](?page=2\u0026param1=value1\u0026param2=value2) | [3](?page=3\u0026param1=value1\u0026param2=value2)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"empty query string\",\n\t\t\tpageNumber: 2,\n\t\t\tpath:       \"/test\",\n\t\t\texpected:   \"[1](?page=1) | **2** | [3](?page=3)\",\n\t\t},\n\t\t{\n\t\t\tname:       \"query string with only page param\",\n\t\t\tpageNumber: 2,\n\t\t\tpath:       \"/test?page=2\",\n\t\t\texpected:   \"[1](?page=1) | **2** | [3](?page=3)\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tpage := pager.GetPageWithSize(tt.pageNumber, 2)\n\t\t\tresult := page.Picker(tt.path)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"\\nwant: %s\\ngot:  %s\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"crxF7AzJiSnDPJYe1teGIyULqXb8AuXk55YTHfxrv8J8oxwznwmLLhWlAcFheL4WMvrvoyRigU5qA0iuLyYACw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"combinederr","path":"gno.land/p/nt/combinederr/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# combinederr\n\nPackage `combinederr` provides a combined error type for aggregating multiple errors into a single error value.\n"},{"name":"combinederr.gno","body":"package combinederr\n\nimport \"strings\"\n\n// CombinedError is a combined execution error\ntype CombinedError struct {\n\terrors []error\n}\n\n// Error returns the combined execution error\nfunc (e *CombinedError) Error() string {\n\tif len(e.errors) == 0 {\n\t\treturn \"\"\n\t}\n\n\tvar sb strings.Builder\n\n\tfor _, err := range e.errors {\n\t\tsb.WriteString(err.Error() + \"; \")\n\t}\n\n\t// Remove the last semicolon and space\n\tresult := sb.String()\n\n\treturn result[:len(result)-2]\n}\n\n// Add adds a new error to the execution error\nfunc (e *CombinedError) Add(err error) {\n\tif err == nil {\n\t\treturn\n\t}\n\n\te.errors = append(e.errors, err)\n}\n\n// Size returns a\nfunc (e *CombinedError) Size() int {\n\treturn len(e.errors)\n}\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package combinederr provides a combined error type for aggregating multiple\n// errors into a single error value.\npackage combinederr\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/combinederr/v0\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"FN/+xDFlx7X0071CX4Cu2wKWHgRMgxhn7CX+3L1ysdp0vNC7ieqM8QC7H7MXVblO6gvkZTxhN8RfyfgH/WZHDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"foreign","path":"gno.land/p/nt/markdown/foreign/v0","files":[{"name":"foreign.gno","body":"// Package foreign provides the realm-side helper for emitting the\n// gno-foreign sandbox block. Realm authors wrap externally-built\n// markdown (markdown returned by an interface method on a foreign\n// realm, fetched from chain storage owned by another realm, etc.) in\n// Foreign before flowing it into rendered output, so gnoweb renders\n// the body inside its own goldmark sub-instance with structural\n// extensions selectively loaded.\n//\n// The renderer-side contract lives in\n// gno.land/pkg/gnoweb/markdown/ext_foreign.go. This helper produces\n// bytes that satisfy the parser's opener requirements (CommonMark\n// §4.6 Type-7 HTML block, no attribute fall-through) and neutralizes\n// any literal sentinel lines in the body so the foreign markdown\n// cannot terminate the outer block prematurely.\npackage foreign\n\nimport (\n\t\"chain/markdown\"\n\t\"strings\"\n)\n\n// Foreign wraps body in a `\u003cgno-foreign\u003e` ... `\u003c/gno-foreign\u003e` sandbox\n// block. The returned string is ready to concatenate into a larger\n// markdown document.\n//\n// Three normalization steps apply to body:\n//\n//  1. \\r\\n and bare \\r line endings are normalized to \\n. The parser\n//     uses byte-equal matching against the sentinel close tag, so\n//     mixed line endings would otherwise change the match boundary.\n//\n//  2. Any line whose trimmed content looks like a gno-foreign tag\n//     opener OR closer — bare (`\u003cgno-foreign\u003e`, `\u003c/gno-foreign\u003e`) or\n//     attribute-bearing (`\u003cgno-foreign label=\"x\"\u003e`, `\u003c/gno-foreign\n//     attr=\"…\"\u003e`, etc.) — is neutralized by HTML-escaping the leading\n//     `\u003c` to `\u0026lt;`. The parser tokenizes line bytes literally, so the\n//     escaped form is seen as text and cannot terminate the outer\n//     block or open an unintended inner block.\n//\n//     Crucially, BOTH open-tag and close-tag attribute-bearing forms\n//     are neutralized. The parser recognizes a bare `\u003cgno-foreign\u003e`\n//     opener, a labeled `\u003cgno-foreign label=\"x\"\u003e` opener, and ANY\n//     `\u003c/gno-foreign…\u003e` closer (golang.org/x/net/html drops attrs on\n//     end tags before our recognizer sees them, so attr-bearing\n//     closers are sentinel-equivalent). Leaving any of those forms\n//     un-neutralized in body bytes would let attacker-supplied\n//     markdown adjust the parser's framing-depth counter and either\n//     consume the helper's own close (capturing trailing realm\n//     content into the sandbox) or close the outer block early\n//     (escaping the sandbox entirely).\n//\n//     There is therefore NO nesting via the helper: Foreign(Foreign(x))\n//     escapes the inner call's own `\u003cgno-foreign\u003e`/`\u003c/gno-foreign\u003e`\n//     lines, so the inner block renders as visible literal text inside\n//     one sandbox, not as a nested sandbox. This is intended — wrapping\n//     foreign-built markdown that itself contains gno-foreign sentinels\n//     must neutralize them, not honor them.\n//\n//  3. A leading and trailing blank line are emitted around the\n//     opener / closer. CommonMark §4.6 forbids Type-7 HTML blocks\n//     from interrupting a paragraph; without the blank line, an\n//     opener following a non-blank line is absorbed into the\n//     preceding paragraph instead of opening a sandbox.\n//\n// The renderer caps cross-family nesting at 4 levels and per-Convert\n// foreign blocks at 256. Beyond those caps, the opener falls through\n// to raw HTML and is stripped by the renderer's safe mode.\nfunc Foreign(body string) string {\n\treturn wrapForeign(\"\", body)\n}\n\n// ForeignWithLabel wraps body like Foreign but emits an explicit\n// `label=\"…\"` attribute on the opener so the rendered sandbox carries\n// a caller-supplied label (e.g., \"Pulled from /r/foo\") shown as a\n// strip above the body. The label is sanitized so it cannot inject\n// HTML or break out of the attribute value:\n//\n//   - NUL bytes are dropped.\n//   - Other control characters (U+0000–U+001F, U+007F) become spaces.\n//   - `\u0026`, `\u003c`, `\u003e`, and `\"` are replaced with their HTML entities.\n//   - Leading/trailing whitespace is trimmed.\n//\n// A label that is empty after sanitization behaves identically to\n// Foreign: no attribute is emitted, and the renderer shows the sandbox\n// box with NO label strip (there is no default label text).\nfunc ForeignWithLabel(label, body string) string {\n\treturn wrapForeign(label, body)\n}\n\n// MaxBlocksPerRender is gnoweb's per-render cap on the number of\n// \u003cgno-foreign\u003e blocks a single page render admits; beyond it, later\n// blocks fall through to raw HTML and are dropped. A realm emitting\n// many foreign blocks (e.g. one per comment) should keep its rendered\n// total under this. Re-exports chain/markdown.MaxForeignBlocksPerConvert\n// — the single source of truth the gnoweb renderer also reads — so\n// callers get the cap without importing chain/markdown directly.\nfunc MaxBlocksPerRender() int {\n\treturn markdown.MaxForeignBlocksPerConvert()\n}\n\nfunc wrapForeign(rawLabel, body string) string {\n\tlabel := sanitizeLabel(rawLabel)\n\n\t// Normalize line endings (CR/CRLF → LF). The parser matches the\n\t// sentinel close against \\n-delimited lines, so mixed line endings\n\t// would otherwise shift the match boundary. CR/CRLF → LF ONLY: do\n\t// not fold Unicode separators here — they must stay verbatim in the\n\t// body so the inner renderer sees the foreign markdown unaltered.\n\tbody = markdown.NormalizeBreaks(body)\n\n\t// Mangle any line that would terminate the outer block or open\n\t// an inner one. Covers bare and attribute-bearing forms of both\n\t// the opener and the closer (see step 2 in the package doc).\n\tvar b strings.Builder\n\t// b accumulates only the body lines (the opener/closer envelope is\n\t// concatenated separately below), so len(body) is the exact size in\n\t// the common case. Sentinel lines that expand `\u003c`→`\u0026lt;` may force\n\t// one growth — rare enough not to pre-size for.\n\tb.Grow(len(body))\n\tlines := strings.Split(body, \"\\n\")\n\tfor i, line := range lines {\n\t\tif isForeignSentinelLine(trimSentinel(line)) {\n\t\t\t// Escape just the leading `\u003c` so the html tokenizer\n\t\t\t// sees this as text instead of a tag. Preserve any 0-3\n\t\t\t// leading spaces the parser's trim would have stripped.\n\t\t\tidx := strings.Index(line, \"\u003c\")\n\t\t\tif idx \u003e= 0 {\n\t\t\t\tline = line[:idx] + \"\u0026lt;\" + line[idx+1:]\n\t\t\t}\n\t\t}\n\t\tb.WriteString(line)\n\t\tif i \u003c len(lines)-1 {\n\t\t\tb.WriteByte('\\n')\n\t\t}\n\t}\n\n\topener := \"\u003cgno-foreign\u003e\"\n\tif label != \"\" {\n\t\topener = `\u003cgno-foreign label=\"` + label + `\"\u003e`\n\t}\n\treturn \"\\n\\n\" + opener + \"\\n\" + b.String() + \"\\n\u003c/gno-foreign\u003e\\n\\n\"\n}\n\n// sanitizeLabel makes a user-supplied label safe to splice into an\n// HTML attribute value on the gno-foreign opener line.\nfunc sanitizeLabel(s string) string {\n\t// Strip bidi-override and zero-width controls FIRST — same ordering\n\t// as the sanitize package's HTMLEscape — so invisible reordering or\n\t// zero-width payloads can't survive into the rendered label.\n\ts = markdown.StripBidiAndZeroWidth(s)\n\t// Drop NUL; map other ASCII controls AND the Unicode line/paragraph\n\t// separators (U+2028, U+2029, U+0085 NEL) to spaces. The opener is a\n\t// single line, so any of these surviving in the label would either\n\t// add a control payload or, for the separators, render as a stray\n\t// line break inside the attribute.\n\ts = strings.Map(func(r rune) rune {\n\t\tif r == 0 {\n\t\t\treturn -1\n\t\t}\n\t\tif r \u003c 0x20 || r == 0x7f || r == 0x2028 || r == 0x2029 || r == 0x0085 {\n\t\t\treturn ' '\n\t\t}\n\t\treturn r\n\t}, s)\n\t// Escape `\u0026` first so subsequent entity bytes don't get\n\t// re-escaped.\n\ts = strings.ReplaceAll(s, \"\u0026\", \"\u0026amp;\")\n\ts = strings.ReplaceAll(s, `\"`, \"\u0026quot;\")\n\ts = strings.ReplaceAll(s, \"\u003c\", \"\u0026lt;\")\n\ts = strings.ReplaceAll(s, \"\u003e\", \"\u0026gt;\")\n\treturn strings.TrimSpace(s)\n}\n\n// isForeignSentinelLine reports whether s (already trimmed via\n// trimSentinel) begins with the gno-foreign tag prefix and so must be\n// neutralized before it can reach the renderer-side parser.\n//\n// Deliberately OVER-INCLUSIVE: it matches any line whose trimmed form\n// starts (case-INSENSITIVELY) with `\u003cgno-foreign` or `\u003c/gno-foreign`,\n// regardless of what follows. This is a strict superset of every line\n// goldmark's html.Tokenizer can recognize as a \u003cgno-foreign\u003e opener or\n// closer, which is what makes it safe:\n//\n//   - The tokenizer lowercases tag names, so `\u003cGNO-FOREIGN\u003e` etc. are\n//     sentinels; the prefix match is case-folded to mirror that.\n//   - The tokenizer ends a tag name at ANY of several terminators\n//     (`\u003e`, space, tab, form-feed, `/`). A precise check that\n//     enumerates terminators keeps missing variants — e.g.\n//     `\u003c/gno-foreign/\u003e` and `\u003c/gno-foreign\\f\u003e` are both recognized as\n//     closers by the parser. Matching on the prefix alone cannot miss\n//     one: if a body line could be parsed as a sentinel, it starts with\n//     this prefix and is escaped here.\n//\n// The only cost is that an unrelated longer tag like `\u003cgno-foreignx\u003e`\n// (a different tag name, not a sentinel) is also escaped — rendered as\n// visible literal text instead of being raw-HTML-stripped — which is\n// harmless for foreign body bytes.\nfunc isForeignSentinelLine(s string) bool {\n\treturn hasASCIIFoldPrefix(s, \"\u003c/gno-foreign\") || hasASCIIFoldPrefix(s, \"\u003cgno-foreign\")\n}\n\n// hasASCIIFoldPrefix reports whether s begins with prefix, comparing\n// ASCII letters case-insensitively. prefix must be lowercase ASCII;\n// folding is ASCII-only on purpose (Unicode case folding would\n// over-match, and the sentinel envelope is pure ASCII anyway).\nfunc hasASCIIFoldPrefix(s, prefix string) bool {\n\tif len(s) \u003c len(prefix) {\n\t\treturn false\n\t}\n\tfor i := 0; i \u003c len(prefix); i++ {\n\t\tc := s[i]\n\t\tif c \u003e= 'A' \u0026\u0026 c \u003c= 'Z' {\n\t\t\tc += 'a' - 'A'\n\t\t}\n\t\tif c != prefix[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// trimSentinel returns line with the leading 0-3 spaces and trailing\n// ASCII whitespace that the parser's trimForeignLine strips. Mirrors\n// the byte-level trim the parser performs so this helper detects the\n// same sentinel match the parser would.\nfunc trimSentinel(s string) string {\n\ti := 0\n\tfor i \u003c len(s) \u0026\u0026 i \u003c 3 \u0026\u0026 s[i] == ' ' {\n\t\ti++\n\t}\n\ts = s[i:]\n\tfor len(s) \u003e 0 {\n\t\tc := s[len(s)-1]\n\t\tif c == ' ' || c == '\\t' || c == '\\n' || c == '\\v' || c == '\\f' || c == '\\r' {\n\t\t\ts = s[:len(s)-1]\n\t\t\tcontinue\n\t\t}\n\t\tbreak\n\t}\n\treturn s\n}\n"},{"name":"foreign_test.gno","body":"package foreign\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestForeign_BasicWrap(t *testing.T) {\n\tgot := Foreign(\"hello\")\n\twant := \"\\n\\n\u003cgno-foreign\u003e\\nhello\\n\u003c/gno-foreign\u003e\\n\\n\"\n\tif got != want {\n\t\tt.Errorf(\"got %q, want %q\", got, want)\n\t}\n}\n\nfunc TestForeign_LeadingTrailingBlankLines(t *testing.T) {\n\t// Parser requires a blank line before the opener (CM §4.6). Verify\n\t// the helper emits exactly that.\n\tgot := Foreign(\"body\")\n\tif !strings.HasPrefix(got, \"\\n\\n\u003cgno-foreign\u003e\\n\") {\n\t\tt.Errorf(\"missing blank line before opener: %q\", got)\n\t}\n\tif !strings.HasSuffix(got, \"\\n\u003c/gno-foreign\u003e\\n\\n\") {\n\t\tt.Errorf(\"missing blank line after closer: %q\", got)\n\t}\n}\n\nfunc TestForeign_NormalizesCRLF(t *testing.T) {\n\tgot := Foreign(\"line1\\r\\nline2\\r\\nline3\")\n\tif strings.Contains(got, \"\\r\") {\n\t\tt.Errorf(\"CRLF not normalized: %q\", got)\n\t}\n\tif !strings.Contains(got, \"line1\\nline2\\nline3\") {\n\t\tt.Errorf(\"body content lost: %q\", got)\n\t}\n}\n\nfunc TestForeign_NormalizesBareCR(t *testing.T) {\n\tgot := Foreign(\"line1\\rline2\")\n\tif strings.Contains(got, \"\\r\") {\n\t\tt.Errorf(\"bare CR not normalized: %q\", got)\n\t}\n\tif !strings.Contains(got, \"line1\\nline2\") {\n\t\tt.Errorf(\"body content lost: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesSentinelClose(t *testing.T) {\n\t// Unbalanced \u003c/gno-foreign\u003e in body would terminate the outer\n\t// block early. Verify it's escaped to literal text — only the\n\t// leading \"\u003c\" is replaced with \"\u0026lt;\"; the trailing \"\u003e\" stays\n\t// literal (rendering as visible \"\u003e\" in HTML).\n\tgot := Foreign(\"foo\\n\u003c/gno-foreign\u003e\\nbar\")\n\tif strings.Contains(got, \"\\n\u003c/gno-foreign\u003e\\nbar\") {\n\t\tt.Errorf(\"unbalanced close not escaped: %q\", got)\n\t}\n\tif !strings.Contains(got, \"\u0026lt;/gno-foreign\u003e\") {\n\t\tt.Errorf(\"expected escaped close: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesSentinelOpen(t *testing.T) {\n\t// A bare opener inside body (without matching close in body)\n\t// would be picked up as a nested inner foreign. Escape it.\n\tgot := Foreign(\"foo\\n\u003cgno-foreign\u003e\\nbar\")\n\tif !strings.Contains(got, \"\\n\u0026lt;gno-foreign\u003e\\n\") {\n\t\tt.Errorf(\"expected escaped inner opener: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesIndentedSentinel(t *testing.T) {\n\t// Parser strips 0-3 leading spaces before matching; helper\n\t// must mirror that to catch indented sentinels.\n\tgot := Foreign(\"foo\\n   \u003c/gno-foreign\u003e\\nbar\")\n\tif !strings.Contains(got, \"\u0026lt;/gno-foreign\u003e\") {\n\t\tt.Errorf(\"indented sentinel not escaped: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesAttributeBearingOpener(t *testing.T) {\n\t// `\u003cgno-foreign label=\"x\"\u003e` IS recognized by the parser as a\n\t// nested opener. If left unmangled, the parser bumps framingDepth\n\t// when it sees this line in body bytes, and then consumes the\n\t// helper's own outer-close as a fake \"inner close\" — capturing\n\t// realm content that follows the helper's output INTO the\n\t// sandbox. Verify the helper escapes the leading \"\u003c\".\n\tgot := Foreign(`\u003cgno-foreign label=\"x\"\u003e`)\n\tif strings.Contains(got, `\u003cgno-foreign label=\"x\"\u003e`) {\n\t\tt.Errorf(\"attribute-bearing opener was NOT mangled — escape vector:\\n%q\", got)\n\t}\n\tif !strings.Contains(got, `\u0026lt;gno-foreign label=\"x\"\u003e`) {\n\t\tt.Errorf(\"expected leading '\u003c' escaped: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesAttributeBearingCloser(t *testing.T) {\n\t// golang.org/x/net/html zeroes the Attr slice on end tags, so the\n\t// parser recognizes ANY `\u003c/gno-foreign attr…\u003e` form as a sentinel\n\t// close. Leaving such a line unmangled in body bytes would close\n\t// the outer block early (framingDepth=0 case in the parser's\n\t// Continue), letting body content that follows render OUTSIDE\n\t// the sandbox at top level. Verify the helper escapes it.\n\tgot := Foreign(`\u003c/gno-foreign label=\"x\"\u003e`)\n\tif strings.Contains(got, `\u003e \u003c/gno-foreign label=\"x\"\u003e \u003c/gno-foreign\u003e`) {\n\t\tt.Errorf(\"attribute-bearing closer was NOT mangled — escape vector:\\n%q\", got)\n\t}\n\tif !strings.Contains(got, `\u0026lt;/gno-foreign label=\"x\"\u003e`) {\n\t\tt.Errorf(\"expected leading '\u003c' escaped: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesGnoForeignWithExtraContent(t *testing.T) {\n\t// `\u003cgno-foreign\u003e extra` would not be recognized as a sentinel by\n\t// the parser (tokenizer yields 2 tokens). But the helper is\n\t// over-inclusive on purpose: anything that looks like a\n\t// gno-foreign tag opener or closer gets mangled. Predictable\n\t// behavior beats narrow matching that diverges from the parser\n\t// on attribute-bearing forms.\n\tgot := Foreign(\"\u003cgno-foreign\u003e extra text\")\n\tif !strings.Contains(got, \"\u0026lt;gno-foreign\u003e extra text\") {\n\t\tt.Errorf(\"expected over-inclusive mangle of `\u003cgno-foreign\u003e extra`: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesCaseVariantClose(t *testing.T) {\n\t// The renderer-side parser recognizes tags case-insensitively\n\t// (goldmark's tokenizer lowercases tag names). A case-variant close\n\t// left unescaped would terminate the outer block early and let\n\t// trailing body render OUTSIDE the sandbox. The helper must escape\n\t// `\u003c/GNO-FOREIGN\u003e` just like `\u003c/gno-foreign\u003e`.\n\tgot := Foreign(\"foo\\n\u003c/GNO-FOREIGN\u003e\\nbar\")\n\tif strings.Contains(got, \"\\n\u003c/GNO-FOREIGN\u003e\\nbar\") {\n\t\tt.Errorf(\"case-variant close not escaped — sandbox-escape vector: %q\", got)\n\t}\n\tif !strings.Contains(got, \"\u0026lt;/GNO-FOREIGN\u003e\") {\n\t\tt.Errorf(\"expected escaped case-variant close: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesCaseVariantOpen(t *testing.T) {\n\t// A case-variant opener left unescaped would be picked up by the\n\t// parser as a nested inner foreign (framingDepth++), causing the\n\t// helper's own outer-close to be consumed as a fake inner close and\n\t// capturing trailing realm content into the sandbox.\n\tgot := Foreign(\"foo\\n\u003cGNO-FOREIGN\u003e\\nbar\")\n\tif !strings.Contains(got, \"\\n\u0026lt;GNO-FOREIGN\u003e\\n\") {\n\t\tt.Errorf(\"case-variant opener not escaped — capture vector: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesMixedCaseSentinel(t *testing.T) {\n\t// Mixed-case forms (the most likely hand-crafted evasion) must also\n\t// be caught, including the attribute-bearing mixed-case opener.\n\tfor _, in := range []string{\n\t\t\"\u003cGno-Foreign\u003e\",\n\t\t\"\u003c/Gno-Foreign\u003e\",\n\t\t`\u003cGNO-foreign label=\"x\"\u003e`,\n\t\t\"\u003c/gNo-fOrEiGn\u003e\",\n\t} {\n\t\tgot := Foreign(in)\n\t\tif !strings.Contains(got, \"\u0026lt;\") {\n\t\t\tt.Errorf(\"mixed-case sentinel %q not escaped: %q\", in, got)\n\t\t}\n\t}\n}\n\nfunc TestForeignWithLabel_StripsBidiAndZeroWidth(t *testing.T) {\n\t// A bidi-override (U+202E) or zero-width (U+200C) run in the label\n\t// is invisible-spoof payload; it must be stripped before the label\n\t// is spliced into the opener attribute.\n\tgot := ForeignWithLabel(\"a\\u202eb\\u200cc\", \"body\")\n\tif !strings.Contains(got, `label=\"abc\"`) {\n\t\tt.Errorf(\"expected bidi/zero-width-stripped label: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_UnicodeSeparatorsBecomeSpaces(t *testing.T) {\n\t// U+2028 / U+2029 / U+0085 must not survive in the single-line\n\t// opener attribute; they fold to spaces like ASCII line breaks.\n\tgot := ForeignWithLabel(\"a\\u2028b\\u2029c\\u0085d\", \"body\")\n\tif !strings.Contains(got, `label=\"a b c d\"`) {\n\t\tt.Errorf(\"expected space-folded label: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesUnrelatedForeignPrefix(t *testing.T) {\n\t// `\u003cgno-foreignx\u003e` is a different tag the parser would NOT treat as\n\t// a sentinel, but the escaper is deliberately over-inclusive (it\n\t// matches the `\u003cgno-foreign` prefix alone), so it is escaped to\n\t// visible literal text. Harmless, and the safe side of the trade.\n\tgot := Foreign(\"\u003cgno-foreignx\u003e\")\n\tif !strings.Contains(got, \"\u0026lt;gno-foreignx\u003e\") {\n\t\tt.Errorf(\"expected over-inclusive escape of \u003cgno-foreignx\u003e: %q\", got)\n\t}\n}\n\nfunc TestForeign_EscapesSlashAndFormFeedClosers(t *testing.T) {\n\t// REGRESSION: goldmark's tokenizer ends a tag name at `/` and at\n\t// form-feed (both HTML tag-name terminators), so `\u003c/gno-foreign/\u003e`\n\t// and `\u003c/gno-foreign\\f\u003e` are recognized as CLOSERS by the parser. A\n\t// body line carrying one of these, if it reached the parser\n\t// unescaped, would close the outer block early — a sandbox escape.\n\t// The over-inclusive escaper must neutralize every such variant.\n\tfor _, in := range []string{\n\t\t\"foo\\n\u003c/gno-foreign/\u003e\\nbar\",\n\t\t\"foo\\n\u003c/gno-foreign/ \u003e\\nbar\",\n\t\t\"foo\\n\u003c/gno-foreign\\f\u003e\\nbar\",\n\t\t\"foo\\n\u003cgno-foreign/\u003e\\nbar\",\n\t} {\n\t\tgot := Foreign(in)\n\t\tif !strings.Contains(got, \"\u0026lt;\") {\n\t\t\tt.Errorf(\"variant not escaped (sandbox-escape vector): %q -\u003e %q\", in, got)\n\t\t}\n\t}\n}\n\nfunc TestForeign_EmptyBody(t *testing.T) {\n\tgot := Foreign(\"\")\n\twant := \"\\n\\n\u003cgno-foreign\u003e\\n\\n\u003c/gno-foreign\u003e\\n\\n\"\n\tif got != want {\n\t\tt.Errorf(\"got %q, want %q\", got, want)\n\t}\n}\n\nfunc TestForeign_PreservesInternalBlankLines(t *testing.T) {\n\tgot := Foreign(\"para1\\n\\npara2\")\n\tif !strings.Contains(got, \"para1\\n\\npara2\") {\n\t\tt.Errorf(\"internal blank line lost: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_BasicWrap(t *testing.T) {\n\tgot := ForeignWithLabel(\"My Label\", \"body\")\n\twant := \"\\n\\n\u003cgno-foreign label=\\\"My Label\\\"\u003e\\nbody\\n\u003c/gno-foreign\u003e\\n\\n\"\n\tif got != want {\n\t\tt.Errorf(\"got %q, want %q\", got, want)\n\t}\n}\n\nfunc TestForeignWithLabel_EmptyLabelFallsBack(t *testing.T) {\n\t// An empty label (or one that becomes empty after sanitization)\n\t// should be identical to Foreign — no attribute on the opener.\n\tgot := ForeignWithLabel(\"\", \"body\")\n\tif strings.Contains(got, \"label=\") {\n\t\tt.Errorf(\"empty label should not emit attribute: %q\", got)\n\t}\n\tif got != Foreign(\"body\") {\n\t\tt.Errorf(\"empty label should match Foreign: got %q vs %q\", got, Foreign(\"body\"))\n\t}\n}\n\nfunc TestForeignWithLabel_WhitespaceOnlyLabelFallsBack(t *testing.T) {\n\tgot := ForeignWithLabel(\"   \", \"body\")\n\tif strings.Contains(got, \"label=\") {\n\t\tt.Errorf(\"whitespace label should not emit attribute: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_EscapesAmpAndQuote(t *testing.T) {\n\tgot := ForeignWithLabel(`Tom \u0026 \"Jerry\"`, \"body\")\n\tif !strings.Contains(got, `label=\"Tom \u0026amp; \u0026quot;Jerry\u0026quot;\"`) {\n\t\tt.Errorf(\"expected \u0026 and \\\" escaped: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_EscapesAngleBrackets(t *testing.T) {\n\t// An unescaped `\u003e` would close the opener tag early. Must be\n\t// escaped.\n\tgot := ForeignWithLabel(`a\u003cb\u003ec`, \"body\")\n\tif !strings.Contains(got, `label=\"a\u0026lt;b\u0026gt;c\"`) {\n\t\tt.Errorf(\"expected \u003c and \u003e escaped: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_StripsNUL(t *testing.T) {\n\tgot := ForeignWithLabel(\"a\\x00b\\x00c\", \"body\")\n\tif strings.Contains(got, \"\\x00\") {\n\t\tt.Errorf(\"NUL not stripped: %q\", got)\n\t}\n\tif !strings.Contains(got, `label=\"abc\"`) {\n\t\tt.Errorf(\"expected NUL-stripped label: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_ControlCharsBecomeSpaces(t *testing.T) {\n\t// Newlines and tabs would break the single-line opener required\n\t// by the parser. They must become spaces.\n\tgot := ForeignWithLabel(\"line1\\nline2\\ttab\", \"body\")\n\tif strings.Contains(got, \"\\nline2\") || strings.Contains(got, \"\\t\") {\n\t\tt.Errorf(\"control chars not converted: %q\", got)\n\t}\n\tif !strings.Contains(got, \"line1 line2 tab\") {\n\t\tt.Errorf(\"expected space-separated label: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_TrimsLeadingTrailingSpace(t *testing.T) {\n\tgot := ForeignWithLabel(\"  hello  \", \"body\")\n\tif !strings.Contains(got, `label=\"hello\"`) {\n\t\tt.Errorf(\"expected trimmed label: %q\", got)\n\t}\n}\n\nfunc TestForeignWithLabel_PreservesBodyNormalization(t *testing.T) {\n\t// Body normalization (CRLF, sentinel escaping) must still apply\n\t// in the labeled variant.\n\tgot := ForeignWithLabel(\"L\", \"a\\r\\n\u003c/gno-foreign\u003e\\r\\nb\")\n\tif strings.Contains(got, \"\\r\") {\n\t\tt.Errorf(\"CRLF not normalized in labeled variant: %q\", got)\n\t}\n\tif !strings.Contains(got, \"\u0026lt;/gno-foreign\u003e\") {\n\t\tt.Errorf(\"sentinel not escaped in labeled variant: %q\", got)\n\t}\n}\n\nfunc TestMaxBlocksPerRender(t *testing.T) {\n\t// Surfaces gnoweb's per-render foreign-block cap to realms via the\n\t// chain/markdown native. Pin the contract value; bump here if the\n\t// renderer cap changes.\n\tif got := MaxBlocksPerRender(); got != 256 {\n\t\tt.Errorf(\"MaxBlocksPerRender() = %d, want 256\", got)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/markdown/foreign/v0\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"n+uemWZBbQMakwJrEvON7/9ZLQW3TQaPaP59NthIJ6WaEKBrd72I3hG/pXy759iUUHA8uhIBwepaugPgiKDjAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"mdalert","path":"gno.land/p/nt/mdalert/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# mdalert\n\nPackage `mdalert` provides support for creating Markdown alerts with standard alert types (note, tip, important, warning, caution).\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package mdalert provides support for creating Markdown alerts.\npackage mdalert\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/mdalert/v0\"\ngno = \"0.9\"\n"},{"name":"mdalert.gno","body":"// Package mdalert provides support for creating Markdown alerts.\n//\n// It defines supported alert types and helper functions that can be\n// called to generate Markdown for different alert types.\n//\n// The different alert types are documented in the Markdown docs realm:\n// https://gno.land/r/docs/markdown#alerts\npackage mdalert\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Types of alerts.\nconst (\n\tTypeCaution Type = \"CAUTION\"\n\tTypeInfo         = \"INFO\"\n\tTypeNote         = \"NOTE\"\n\tTypeSuccess      = \"SUCCESS\"\n\tTypeTip          = \"TIP\"\n\tTypeWarning      = \"WARNING\"\n)\n\ntype (\n\t// Type defines a type for the alert types.\n\tType string\n\n\t// Alert defines a type for alerts.\n\tAlert struct {\n\t\t// Type defines the type of alert.\n\t\tType Type\n\n\t\t// Title contains an optional title for the alert.\n\t\tTitle string\n\n\t\t// Message contains alerts's message.\n\t\tMessage string\n\n\t\t// Folded indicates that the alert must be folded on render.\n\t\t// Message is not initially visible when folded, only title is visible.\n\t\tFolded bool\n\t}\n)\n\n// String returns the alert as a Markdown string.\nfunc (a Alert) String() string {\n\talertType := string(a.Type)\n\tmsg := strings.TrimSpace(a.Message)\n\tif msg == \"\" || alertType == \"\" {\n\t\treturn \"\"\n\t}\n\n\t// Init alert fold marker\n\tvar fold string\n\tif a.Folded {\n\t\tfold = \"-\"\n\t}\n\n\t// Write alert header\n\tvar b strings.Builder\n\theader := ufmt.Sprintf(\"\u003e [!%s]%s %s\", alertType, fold, a.Title)\n\tb.WriteString(strings.TrimSpace(header) + \"\\n\")\n\n\t// Write alert message\n\tlines := strings.Split(msg, \"\\n\")\n\tfor _, line := range lines {\n\t\tb.WriteString(\"\u003e \" + line + \"\\n\")\n\t}\n\treturn b.String()\n}\n\n// New creates a new alert.\nfunc New(t Type, title, msg string, folded bool) Alert {\n\treturn Alert{\n\t\tType:    t,\n\t\tTitle:   title,\n\t\tMessage: msg,\n\t\tFolded:  folded,\n\t}\n}\n\n// Caution returns an alert Markdown of type caution.\nfunc Caution(title, msg string) string {\n\treturn New(TypeCaution, title, msg, false).String()\n}\n\n// Cautionf returns an alert Markdown of type caution with a formatted message.\nfunc Cautionf(title, format string, a ...any) string {\n\treturn New(TypeCaution, title, ufmt.Sprintf(format, a...), false).String()\n}\n\n// Info returns an alert Markdown of type info.\nfunc Info(title, msg string) string {\n\treturn New(TypeInfo, title, msg, false).String()\n}\n\n// Infof returns an alert Markdown of type info with a formatted message.\nfunc Infof(title, format string, a ...any) string {\n\treturn New(TypeInfo, title, ufmt.Sprintf(format, a...), false).String()\n}\n\n// Note returns an alert Markdown of type note.\nfunc Note(title, msg string) string {\n\treturn New(TypeNote, title, msg, false).String()\n}\n\n// Notef returns an alert Markdown of type note with a formatted message.\nfunc Notef(title, format string, a ...any) string {\n\treturn New(TypeNote, title, ufmt.Sprintf(format, a...), false).String()\n}\n\n// Success returns an alert Markdown of type success.\nfunc Success(title, msg string) string {\n\treturn New(TypeSuccess, title, msg, false).String()\n}\n\n// Notef returns an alert Markdown of type success with a formatted message.\nfunc Successf(title, format string, a ...any) string {\n\treturn New(TypeSuccess, title, ufmt.Sprintf(format, a...), false).String()\n}\n\n// Tip returns an alert Markdown of type tip.\nfunc Tip(title, msg string) string {\n\treturn New(TypeTip, title, msg, false).String()\n}\n\n// Tipf returns an alert Markdown of type tip with a formatted message.\nfunc Tipf(title, format string, a ...any) string {\n\treturn New(TypeTip, title, ufmt.Sprintf(format, a...), false).String()\n}\n\n// Warning returns an alert Markdown of type warning.\nfunc Warning(title, msg string) string {\n\treturn New(TypeWarning, title, msg, false).String()\n}\n\n// Warningf returns an alert Markdown of type warning with a formatted message.\nfunc Warningf(title, format string, a ...any) string {\n\treturn New(TypeWarning, title, ufmt.Sprintf(format, a...), false).String()\n}\n"},{"name":"mdalert_test.gno","body":"package mdalert_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/mdalert/v0\"\n)\n\nfunc TestAlertString(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\texpected string\n\t\talert    mdalert.Alert\n\t}{\n\t\t{\n\t\t\tname:     \"alert\",\n\t\t\texpected: \"\u003e [!INFO] Title\\n\u003e Message\\n\",\n\t\t\talert:    mdalert.New(mdalert.TypeInfo, \"Title\", \"Message\", false),\n\t\t},\n\t\t{\n\t\t\tname:     \"alert with empty title\",\n\t\t\texpected: \"\u003e [!INFO]\\n\u003e Message\\n\",\n\t\t\talert:    mdalert.New(mdalert.TypeInfo, \"\", \"Message\", false),\n\t\t},\n\t\t{\n\t\t\tname:     \"alert multiline\",\n\t\t\texpected: \"\u003e [!INFO]\\n\u003e Line1\\n\u003e Line2\\n\",\n\t\t\talert:    mdalert.New(mdalert.TypeInfo, \"\", \"Line1\\nLine2\", false),\n\t\t},\n\t\t{\n\t\t\tname:     \"folded alert\",\n\t\t\texpected: \"\u003e [!INFO]- Title\\n\u003e Message\\n\",\n\t\t\talert:    mdalert.New(mdalert.TypeInfo, \"Title\", \"Message\", true),\n\t\t},\n\t\t{\n\t\t\tname: \"empty alert\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := tt.alert.String()\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"Got:  %q\\nWant: %q\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestHelpers(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\texpected string\n\t\tfn       func() string\n\t}{\n\t\t// CAUTION\n\t\t{\"caution\", \"\u003e [!CAUTION] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Caution(\"Title\", \"Message\")\n\t\t}},\n\t\t{\"caution with empty title\", \"\u003e [!CAUTION]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Caution(\"\", \"Message\")\n\t\t}},\n\t\t{\"caution multiline\", \"\u003e [!CAUTION] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Caution(\"Title\", \"Line1\\nLine2\")\n\t\t}},\n\t\t{\"caution formatted\", \"\u003e [!CAUTION] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Cautionf(\"Title\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"caution formatted with empty title\", \"\u003e [!CAUTION]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Cautionf(\"\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"caution formatted multiline\", \"\u003e [!CAUTION] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Cautionf(\"Title\", \"%s\\n%s\", \"Line1\", \"Line2\")\n\t\t}},\n\n\t\t// INFO\n\t\t{\"info\", \"\u003e [!INFO] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Info(\"Title\", \"Message\")\n\t\t}},\n\t\t{\"info with empty title\", \"\u003e [!INFO]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Info(\"\", \"Message\")\n\t\t}},\n\t\t{\"info multiline\", \"\u003e [!INFO] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Info(\"Title\", \"Line1\\nLine2\")\n\t\t}},\n\t\t{\"info formatted\", \"\u003e [!INFO] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Infof(\"Title\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"info formatted with empty title\", \"\u003e [!INFO]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Infof(\"\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"info formatted multiline\", \"\u003e [!INFO] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Infof(\"Title\", \"%s\\n%s\", \"Line1\", \"Line2\")\n\t\t}},\n\n\t\t// NOTE\n\t\t{\"note\", \"\u003e [!NOTE] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Note(\"Title\", \"Message\")\n\t\t}},\n\t\t{\"note with empty title\", \"\u003e [!NOTE]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Note(\"\", \"Message\")\n\t\t}},\n\t\t{\"note multiline\", \"\u003e [!NOTE] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Note(\"Title\", \"Line1\\nLine2\")\n\t\t}},\n\t\t{\"note formatted\", \"\u003e [!NOTE] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Notef(\"Title\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"note formatted with empty title\", \"\u003e [!NOTE]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Notef(\"\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"note formatted multiline\", \"\u003e [!NOTE] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Notef(\"Title\", \"%s\\n%s\", \"Line1\", \"Line2\")\n\t\t}},\n\n\t\t// SUCCESS\n\t\t{\"success\", \"\u003e [!SUCCESS] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Success(\"Title\", \"Message\")\n\t\t}},\n\t\t{\"success with empty title\", \"\u003e [!SUCCESS]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Success(\"\", \"Message\")\n\t\t}},\n\t\t{\"success multiline\", \"\u003e [!SUCCESS] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Success(\"Title\", \"Line1\\nLine2\")\n\t\t}},\n\t\t{\"success formatted\", \"\u003e [!SUCCESS] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Successf(\"Title\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"success formatted with empty title\", \"\u003e [!SUCCESS]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Successf(\"\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"success formatted multiline\", \"\u003e [!SUCCESS] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Successf(\"Title\", \"%s\\n%s\", \"Line1\", \"Line2\")\n\t\t}},\n\n\t\t// TIP\n\t\t{\"tip\", \"\u003e [!TIP] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Tip(\"Title\", \"Message\")\n\t\t}},\n\t\t{\"tip with empty title\", \"\u003e [!TIP]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Tip(\"\", \"Message\")\n\t\t}},\n\t\t{\"tip multiline\", \"\u003e [!TIP] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Tip(\"Title\", \"Line1\\nLine2\")\n\t\t}},\n\t\t{\"tip formatted\", \"\u003e [!TIP] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Tipf(\"Title\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"tip formatted with empty title\", \"\u003e [!TIP]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Tipf(\"\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"tip formatted multiline\", \"\u003e [!TIP] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Tipf(\"Title\", \"%s\\n%s\", \"Line1\", \"Line2\")\n\t\t}},\n\n\t\t// WARNING\n\t\t{\"warning\", \"\u003e [!WARNING] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Warning(\"Title\", \"Message\")\n\t\t}},\n\t\t{\"warning with empty title\", \"\u003e [!WARNING]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Warning(\"\", \"Message\")\n\t\t}},\n\t\t{\"warning multiline\", \"\u003e [!WARNING] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Warning(\"Title\", \"Line1\\nLine2\")\n\t\t}},\n\t\t{\"warning formatted\", \"\u003e [!WARNING] Title\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Warningf(\"Title\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"warning formatted with empty title\", \"\u003e [!WARNING]\\n\u003e Message\\n\", func() string {\n\t\t\treturn mdalert.Warningf(\"\", \"%s\", \"Message\")\n\t\t}},\n\t\t{\"warning formatted multiline\", \"\u003e [!WARNING] Title\\n\u003e Line1\\n\u003e Line2\\n\", func() string {\n\t\t\treturn mdalert.Warningf(\"Title\", \"%s\\n%s\", \"Line1\", \"Line2\")\n\t\t}},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := tt.fn()\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"Got:  %q\\nWant: %q\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"OGJDapD+hQ97TT13ytmQhVTQe7aPiojeGClxb35mzalDcBww0X6BUAjBNqAtrSfvcLY2npGDzmc3fuqfp23QCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"authorizable","path":"gno.land/p/nt/ownable/v0/exts/authorizable","files":[{"name":"authorizable.gno","body":"// Package authorizable is an extension of p/nt/ownable;\n// It allows the user to instantiate an Authorizable struct, which extends\n// p/nt/ownable with a list of users that are authorized for something.\n// By using authorizable, you have a superuser (ownable), as well as another\n// authorization level, which can be used for adding moderators or similar to your realm.\npackage authorizable\n\nimport (\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Authorizable struct {\n\t*ownable.Ownable                // owner in ownable is superuser\n\tauthorized       *bptree.BPTree // chain.Addr \u003e struct{}{}\n}\n\n// New creates an Authorizable from an existing *ownable.Ownable.\n// The owner is automatically added to the auth list.\n//\n// Example construction:\n//\n//\tauthorizable.New(ownable.NewWithAddress(addr))\nfunc New(o *ownable.Ownable) *Authorizable {\n\ta := \u0026Authorizable{\n\t\tOwnable:    o,\n\t\tauthorized: bptree.NewBPTree32(),\n\t}\n\n\t// Add owner to auth list\n\ta.authorized.Set(a.Owner().String(), struct{}{})\n\treturn a\n}\n\n// AddToAuthList adds addr to the auth list. rlm must be the caller's\n// own captured cur; rlm.Previous().Address() must equal the superuser\n// (the underlying Ownable's owner).\nfunc (a *Authorizable) AddToAuthList(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotSuperuser\n\t}\n\tif !a.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ErrNotSuperuser\n\t}\n\treturn a.addToAuthList(addr)\n}\n\nfunc (a *Authorizable) addToAuthList(addr address) error {\n\tif a.authorized.Has(addr.String()) {\n\t\treturn ErrAlreadyInList\n\t}\n\n\ta.authorized.Set(addr.String(), struct{}{})\n\n\treturn nil\n}\n\n// DeleteFromAuthList removes addr from the auth list. rlm must be the\n// caller's own captured cur; rlm.Previous().Address() must equal the\n// superuser (the underlying Ownable's owner).\nfunc (a *Authorizable) DeleteFromAuthList(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotSuperuser\n\t}\n\tif !a.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ErrNotSuperuser\n\t}\n\treturn a.deleteFromAuthList(addr)\n}\n\nfunc (a *Authorizable) deleteFromAuthList(addr address) error {\n\tif !a.authorized.Has(addr.String()) {\n\t\treturn ErrNotInAuthList\n\t}\n\n\tif _, removed := a.authorized.Remove(addr.String()); !removed {\n\t\tstr := ufmt.Sprintf(\"authorizable: could not remove %s from auth list\", addr.String())\n\t\tpanic(str)\n\t}\n\n\treturn nil\n}\n\n// OnAuthList reports whether rlm.Address() is on the auth list. rlm\n// must be the caller's own captured cur (asserted via rlm.IsCurrent()).\n// Pre-migration shape used unsafe.CurrentRealm().Address() — vulnerable\n// to the .Title()-class read where a non-crossing wrapper made the walk\n// return the wrong realm. Explicit rlm closes that.\nfunc (a *Authorizable) OnAuthList(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotInAuthList\n\t}\n\treturn a.onAuthList(rlm.Address())\n}\n\n// PreviousOnAuthList reports whether rlm.Previous().Address() — the\n// realm that crossed into the caller — is on the auth list. Same rlm\n// contract as OnAuthList.\nfunc (a *Authorizable) PreviousOnAuthList(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotInAuthList\n\t}\n\treturn a.onAuthList(rlm.Previous().Address())\n}\n\nfunc (a *Authorizable) onAuthList(caller address) error {\n\tif !a.authorized.Has(caller.String()) {\n\t\treturn ErrNotInAuthList\n\t}\n\treturn nil\n}\n\nfunc (a Authorizable) AssertOnAuthList(_ int, rlm realm) {\n\tif err := a.OnAuthList(0, rlm); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc (a Authorizable) AssertPreviousOnAuthList(_ int, rlm realm) {\n\tif err := a.PreviousOnAuthList(0, rlm); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"authorizable_test.gno","body":"package authorizable\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar (\n\talice   = testutils.TestAddress(\"alice\")\n\tbob     = testutils.TestAddress(\"bob\")\n\tcharlie = testutils.TestAddress(\"charlie\")\n)\n\nfunc TestNew(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\ta := New(ownable.NewWithAddress(alice))\n\tgot := a.Owner()\n\n\tif alice != got {\n\t\tt.Fatalf(\"Expected %s, got: %s\", alice, got)\n\t}\n}\n\nfunc TestOnAuthList(cur realm, t *testing.T) {\n\ta := New(ownable.NewWithAddress(alice))\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\t// After SetRealm, cur is the test-frame HIV with addr=alice;\n\t// OnAuthList reports on rlm.Address() (i.e. cur.Address()).\n\tif err := a.OnAuthList(0, cur); err == ErrNotInAuthList {\n\t\tt.Fatalf(\"expected alice to be on the list\")\n\t}\n}\n\nfunc TestNotOnAuthList(cur realm, t *testing.T) {\n\ta := New(ownable.NewWithAddress(alice))\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\n\tif err := a.OnAuthList(0, cur); err == nil {\n\t\tt.Fatalf(\"expected bob to not be on the list\")\n\t}\n}\n\nfunc TestAddToAuthList(cur realm, t *testing.T) {\n\ta := New(ownable.NewWithAddress(alice))\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tvar err error\n\tfunc(cur realm) { err = a.AddToAuthList(0, cur, bob) }(cross(cur))\n\tif err != nil {\n\t\tt.Fatalf(\"Expected no error, got %v\", err)\n\t}\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) { err = a.AddToAuthList(0, cur, bob) }(cross(cur))\n\tif err == nil {\n\t\tt.Fatalf(\"Expected AddToAuth to error while bob called it, but it didn't\")\n\t}\n}\n\nfunc TestDeleteFromList(cur realm, t *testing.T) {\n\ta := New(ownable.NewWithAddress(alice))\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tvar err error\n\tfunc(cur realm) { err = a.AddToAuthList(0, cur, bob) }(cross(cur))\n\tif err != nil {\n\t\tt.Fatalf(\"Expected no error, got %v\", err)\n\t}\n\n\tfunc(cur realm) { err = a.AddToAuthList(0, cur, charlie) }(cross(cur))\n\tif err != nil {\n\t\tt.Fatalf(\"Expected no error, got %v\", err)\n\t}\n\n\t// Try an unauthorized deletion (bob is not the superuser).\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) { err = a.DeleteFromAuthList(0, cur, alice) }(cross(cur))\n\tif err == nil {\n\t\tt.Fatalf(\"Expected DelFromAuth to error with %v\", err)\n\t}\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tfunc(cur realm) { err = a.DeleteFromAuthList(0, cur, charlie) }(cross(cur))\n\tif err != nil {\n\t\tt.Fatalf(\"Expected no error, got %v\", err)\n\t}\n}\n\nfunc TestAssertOnList(cur realm, t *testing.T) {\n\ta := New(ownable.NewWithAddress(alice))\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\n\tuassert.PanicsWithMessage(t, cur, ErrNotInAuthList.Error(), func() {\n\t\ta.AssertOnAuthList(0, cur)\n\t})\n}\n"},{"name":"errors.gno","body":"package authorizable\n\nimport \"errors\"\n\nvar (\n\tErrNotInAuthList = errors.New(\"authorizable: caller is not in authorized list\")\n\tErrNotSuperuser  = errors.New(\"authorizable: caller is not superuser\")\n\tErrAlreadyInList = errors.New(\"authorizable: address is already in authorized list\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/ownable/v0/exts/authorizable\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"X7zIInQNiW6Ik4aSW8N0THuoK/6jvkcibGMLPtNMk7KVBnpycjwbvCyUpsMPEvAlVycrbRth44eP3u0s+TghAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"validators","path":"gno.land/p/sys/validators","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/sys/validators\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"types.gno","body":"package validators\n\nimport (\n\t\"errors\"\n)\n\n// ValsetProtocol defines the validator set protocol (PoA / PoS / PoC / ?)\ntype ValsetProtocol interface {\n\t// AddValidator adds a new validator to the validator set.\n\t// If the validator is already present, the method should error out\n\t//\n\t// TODO: This API is not ideal -- the address should be derived from\n\t// the public key, and not be passed in as such, but currently Gno\n\t// does not support crypto address derivation\n\tAddValidator(address_XXX address, pubKey string, power uint64) (Validator, error)\n\n\t// RemoveValidator removes the given validator from the set.\n\t// If the validator is not present in the set, the method should error out\n\tRemoveValidator(address_XXX address) (Validator, error)\n\n\t// IsValidator returns a flag indicating if the given\n\t// bech32 address is part of the validator set\n\tIsValidator(address_XXX address) bool\n\n\t// GetValidator returns the validator using the given address\n\tGetValidator(address_XXX address) (Validator, error)\n\n\t// GetValidators returns the currently active validator set\n\tGetValidators() []Validator\n}\n\n// Validator represents a single chain validator\ntype Validator struct {\n\tAddress     address // bech32 address\n\tPubKey      string  // bech32 representation of the public key\n\tVotingPower uint64\n}\n\nconst (\n\tValidatorAddedEvent   = \"ValidatorAdded\"   // emitted when a validator was added to the set\n\tValidatorRemovedEvent = \"ValidatorRemoved\" // emitted when a validator was removed from the set\n)\n\nvar (\n\t// ErrValidatorExists is returned when the validator is already in the set\n\tErrValidatorExists = errors.New(\"validator already exists\")\n\n\t// ErrValidatorMissing is returned when the validator is not in the set\n\tErrValidatorMissing = errors.New(\"validator doesn't exist\")\n)\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"rkJz2qnVibcAKHUiAI8WgApbquEMsYF509jjQNZ5sLoTNd8pEQamINkEdJGt6rLVzBPBToOx+rhf+ZOsSjD2Cg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l","package":{"name":"poa","path":"gno.land/p/nt/poa/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# poa\n\nPackage `poa` implements a Proof of Authority validator set management system with simple add/remove constraints.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package poa implements a Proof of Authority validator set management system.\npackage poa\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/poa/v0\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l\"\n"},{"name":"option.gno","body":"package poa\n\nimport \"gno.land/p/sys/validators\"\n\ntype Option func(*PoA)\n\n// WithInitialSet sets the initial PoA validator set\nfunc WithInitialSet(validators []validators.Validator) Option {\n\treturn func(p *PoA) {\n\t\tfor _, validator := range validators {\n\t\t\tp.validators.Set(validator.Address.String(), validator)\n\t\t}\n\t}\n}\n"},{"name":"poa.gno","body":"package poa\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/sys/validators\"\n)\n\nvar ErrInvalidVotingPower = errors.New(\"invalid voting power\")\n\n// PoA specifies the Proof of Authority validator set, with simple add / remove constraints.\n//\n// To add:\n// - proposed validator must not be part of the set already\n// - proposed validator voting power must be \u003e 0\n//\n// To remove:\n// - proposed validator must be part of the set already\ntype PoA struct {\n\tvalidators *bptree.BPTree // address -\u003e validators.Validator\n}\n\n// NewPoA creates a new empty Proof of Authority validator set\nfunc NewPoA(opts ...Option) *PoA {\n\t// Create the empty set\n\tp := \u0026PoA{\n\t\tvalidators: bptree.NewBPTree32(),\n\t}\n\n\t// Apply the options\n\tfor _, opt := range opts {\n\t\topt(p)\n\t}\n\n\treturn p\n}\n\nfunc (p *PoA) AddValidator(address_XXX address, pubKey string, power uint64) (validators.Validator, error) {\n\t// Validate that the operation is a valid call.\n\t// Check if the validator is already in the set\n\tif p.IsValidator(address_XXX) {\n\t\treturn validators.Validator{}, validators.ErrValidatorExists\n\t}\n\n\t// Make sure the voting power \u003e 0\n\tif power == 0 {\n\t\treturn validators.Validator{}, ErrInvalidVotingPower\n\t}\n\n\tv := validators.Validator{\n\t\tAddress:     address_XXX,\n\t\tPubKey:      pubKey, // TODO: in the future, verify the public key\n\t\tVotingPower: power,\n\t}\n\n\t// Add the validator to the set\n\tp.validators.Set(address_XXX.String(), v)\n\n\treturn v, nil\n}\n\nfunc (p *PoA) RemoveValidator(address_XXX address) (validators.Validator, error) {\n\t// Validate that the operation is a valid call\n\t// Fetch the validator\n\tvalidator, err := p.GetValidator(address_XXX)\n\tif err != nil {\n\t\treturn validators.Validator{}, err\n\t}\n\n\t// Remove the validator from the set\n\tp.validators.Remove(address_XXX.String())\n\n\treturn validator, nil\n}\n\nfunc (p *PoA) IsValidator(address_XXX address) bool {\n\treturn p.validators.Has(address_XXX.String())\n}\n\nfunc (p *PoA) GetValidator(address_XXX address) (validators.Validator, error) {\n\tvalidatorRaw := p.validators.Get(address_XXX.String())\n\tif validatorRaw == nil {\n\t\treturn validators.Validator{}, validators.ErrValidatorMissing\n\t}\n\n\tvalidator := validatorRaw.(validators.Validator)\n\n\treturn validator, nil\n}\n\nfunc (p *PoA) GetValidators() []validators.Validator {\n\tvals := make([]validators.Validator, 0, p.validators.Size())\n\n\tp.validators.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tvalidator := value.(validators.Validator)\n\t\tvals = append(vals, validator)\n\n\t\treturn false\n\t})\n\n\treturn vals\n}\n"},{"name":"poa_test.gno","body":"package poa\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\t\"gno.land/p/sys/validators\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// generateTestValidators generates a dummy validator set\nfunc generateTestValidators(count int) []validators.Validator {\n\tvals := make([]validators.Validator, 0, count)\n\n\tfor i := 0; i \u003c count; i++ {\n\t\tval := validators.Validator{\n\t\t\tAddress:     testutils.TestAddress(ufmt.Sprintf(\"%d\", i)),\n\t\t\tPubKey:      \"public-key\",\n\t\t\tVotingPower: 1,\n\t\t}\n\n\t\tvals = append(vals, val)\n\t}\n\n\treturn vals\n}\n\nfunc TestPoA_AddValidator_Invalid(t *testing.T) {\n\tt.Parallel()\n\n\tt.Run(\"validator already in set\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tvar (\n\t\t\tproposalAddress = testutils.TestAddress(\"caller\")\n\t\t\tproposalKey     = \"public-key\"\n\n\t\t\tinitialSet = generateTestValidators(1)\n\t\t)\n\n\t\tinitialSet[0].Address = proposalAddress\n\t\tinitialSet[0].PubKey = proposalKey\n\n\t\t// Create the protocol with an initial set\n\t\tp := NewPoA(WithInitialSet(initialSet))\n\n\t\t// Attempt to add the validator\n\t\t_, err := p.AddValidator(proposalAddress, proposalKey, 1)\n\t\tuassert.ErrorIs(t, err, validators.ErrValidatorExists)\n\t})\n\n\tt.Run(\"invalid voting power\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tvar (\n\t\t\tproposalAddress = testutils.TestAddress(\"caller\")\n\t\t\tproposalKey     = \"public-key\"\n\t\t)\n\n\t\t// Create the protocol with no initial set\n\t\tp := NewPoA()\n\n\t\t// Attempt to add the validator\n\t\t_, err := p.AddValidator(proposalAddress, proposalKey, 0)\n\t\tuassert.ErrorIs(t, err, ErrInvalidVotingPower)\n\t})\n}\n\nfunc TestPoA_AddValidator(t *testing.T) {\n\tt.Parallel()\n\n\tvar (\n\t\tproposalAddress = testutils.TestAddress(\"caller\")\n\t\tproposalKey     = \"public-key\"\n\t)\n\n\t// Create the protocol with no initial set\n\tp := NewPoA()\n\n\t// Attempt to add the validator\n\t_, err := p.AddValidator(proposalAddress, proposalKey, 1)\n\tuassert.NoError(t, err)\n\n\t// Make sure the validator is added\n\tif !p.IsValidator(proposalAddress) || p.validators.Size() != 1 {\n\t\tt.Fatal(\"address is not validator\")\n\t}\n}\n\nfunc TestPoA_RemoveValidator_Invalid(t *testing.T) {\n\tt.Parallel()\n\n\tt.Run(\"proposed removal not in set\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tvar (\n\t\t\tproposalAddress = testutils.TestAddress(\"caller\")\n\t\t\tinitialSet      = generateTestValidators(1)\n\t\t)\n\n\t\tinitialSet[0].Address = proposalAddress\n\n\t\t// Create the protocol with an initial set\n\t\tp := NewPoA(WithInitialSet(initialSet))\n\n\t\t// Attempt to remove the validator\n\t\t_, err := p.RemoveValidator(testutils.TestAddress(\"totally random\"))\n\t\tuassert.ErrorIs(t, err, validators.ErrValidatorMissing)\n\t})\n}\n\nfunc TestPoA_RemoveValidator(t *testing.T) {\n\tt.Parallel()\n\n\tvar (\n\t\tproposalAddress = testutils.TestAddress(\"caller\")\n\t\tinitialSet      = generateTestValidators(1)\n\t)\n\n\tinitialSet[0].Address = proposalAddress\n\n\t// Create the protocol with an initial set\n\tp := NewPoA(WithInitialSet(initialSet))\n\n\t// Attempt to remove the validator\n\t_, err := p.RemoveValidator(proposalAddress)\n\turequire.NoError(t, err)\n\n\t// Make sure the validator is removed\n\tif p.IsValidator(proposalAddress) || p.validators.Size() != 0 {\n\t\tt.Fatal(\"address is validator\")\n\t}\n}\n\nfunc TestPoA_GetValidator(t *testing.T) {\n\tt.Parallel()\n\n\tt.Run(\"validator not in set\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\t// Create the protocol with no initial set\n\t\tp := NewPoA()\n\n\t\t// Attempt to get the voting power\n\t\t_, err := p.GetValidator(testutils.TestAddress(\"caller\"))\n\t\tuassert.ErrorIs(t, err, validators.ErrValidatorMissing)\n\t})\n\n\tt.Run(\"validator fetched\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tvar (\n\t\t\taddress_XXX = testutils.TestAddress(\"caller\")\n\t\t\tpubKey      = \"public-key\"\n\t\t\tvotingPower = uint64(10)\n\n\t\t\tinitialSet = generateTestValidators(1)\n\t\t)\n\n\t\tinitialSet[0].Address = address_XXX\n\t\tinitialSet[0].PubKey = pubKey\n\t\tinitialSet[0].VotingPower = votingPower\n\n\t\t// Create the protocol with an initial set\n\t\tp := NewPoA(WithInitialSet(initialSet))\n\n\t\t// Get the validator\n\t\tval, err := p.GetValidator(address_XXX)\n\t\turequire.NoError(t, err)\n\n\t\t// Validate the address\n\t\tif val.Address != address_XXX {\n\t\t\tt.Fatal(\"invalid address\")\n\t\t}\n\n\t\t// Validate the voting power\n\t\tif val.VotingPower != votingPower {\n\t\t\tt.Fatal(\"invalid voting power\")\n\t\t}\n\n\t\t// Validate the public key\n\t\tif val.PubKey != pubKey {\n\t\t\tt.Fatal(\"invalid public key\")\n\t\t}\n\t})\n}\n\nfunc TestPoA_GetValidators(t *testing.T) {\n\tt.Parallel()\n\n\tt.Run(\"empty set\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\t// Create the protocol with no initial set\n\t\tp := NewPoA()\n\n\t\t// Attempt to get the voting power\n\t\tvals := p.GetValidators()\n\n\t\tif len(vals) != 0 {\n\t\t\tt.Fatal(\"validator set is not empty\")\n\t\t}\n\t})\n\n\tt.Run(\"validator set fetched\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tinitialSet := generateTestValidators(10)\n\n\t\t// Create the protocol with an initial set\n\t\tp := NewPoA(WithInitialSet(initialSet))\n\n\t\t// Get the validator set\n\t\tvals := p.GetValidators()\n\n\t\tif len(vals) != len(initialSet) {\n\t\t\tt.Fatal(\"returned validator set mismatch\")\n\t\t}\n\n\t\tfor _, val := range vals {\n\t\t\tfor _, initialVal := range initialSet {\n\t\t\t\tif val.Address != initialVal.Address {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\t// Validate the voting power\n\t\t\t\tuassert.Equal(t, val.VotingPower, initialVal.VotingPower)\n\n\t\t\t\t// Validate the public key\n\t\t\t\tuassert.Equal(t, val.PubKey, initialVal.PubKey)\n\t\t\t}\n\t\t}\n\t})\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"LtdUr4GTY8ru1+YK54mSim3JOlePUU+Ot2eyZR71fL18VKet8xt8TDdMt/paIJH9HsID3E4sYQvlL5IZdcRaAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"treasury","path":"gno.land/p/nt/treasury/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# treasury\n\nPackage `treasury` provides treasury management for handling coin and GRC20 token transfers in Gno realms.\n"},{"name":"banker_canonical_filetest.gno","body":"// PKGPATH: gno.land/r/treasury/canonicaltest\n\npackage canonicaltest\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\n\t\"gno.land/p/nt/treasury/v0\"\n)\n\n// evilBanker embeds *treasury.CoinsBanker so it inherits ID/Send/Balances/\n// Address via promotion. If treasury used a sealed-interface marker, this\n// type would satisfy the interface and bypass the gate. The canonical-impl\n// allowlist (IsCanonicalBanker / treasury.New's type switch) rejects it\n// because type assertions are nominal: *evilBanker is not *CoinsBanker.\ntype evilBanker struct {\n\t*treasury.CoinsBanker\n}\n\nfunc main(cur realm) {\n\townerAddr := chain.PackageAddress(\"gno.land/r/treasury/canonicaltest\")\n\n\tinner := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tlegit, err := treasury.NewCoinsBankerWithOwner(ownerAddr, inner)\n\tif err != nil {\n\t\tpanic(\"failed to construct canonical banker: \" + err.Error())\n\t}\n\n\t// Verify the helper accepts the canonical impl.\n\tif !treasury.IsCanonicalBanker(legit) {\n\t\tpanic(\"canonical *CoinsBanker must pass IsCanonicalBanker\")\n\t}\n\n\t// Verify the helper rejects an embedded-impl bypass attempt.\n\tevil := \u0026evilBanker{CoinsBanker: legit}\n\tif treasury.IsCanonicalBanker(evil) {\n\t\tpanic(\"embedded-impl wrapper must NOT pass IsCanonicalBanker\")\n\t}\n\n\t// Verify treasury.New rejects the same bypass attempt.\n\t_, err = treasury.New([]treasury.Banker{evil}, \"\")\n\tif err != treasury.ErrNonCanonicalBankerImpl {\n\t\tpanic(\"expected ErrNonCanonicalBankerImpl from treasury.New; got: \" + err.Error())\n\t}\n\n\t// And confirms the canonical banker still works.\n\t_, err = treasury.New([]treasury.Banker{legit}, \"\")\n\tif err != nil {\n\t\tpanic(\"canonical banker must be accepted: \" + err.Error())\n\t}\n\n\tprintln(\"canonical allowlist OK\")\n}\n\n// Output:\n// canonical allowlist OK\n"},{"name":"banker_coins.gno","body":"package treasury\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"errors\"\n\n\t\"gno.land/p/aeddi/panictoerr\"\n)\n\nvar ErrNonCanonicalBanker = errors.New(\"inner banker is not the canonical chain/banker.Banker\")\n\n// CoinsBanker is a Banker that sends banker.Coins.\ntype CoinsBanker struct {\n\towner  address       // The address of this coins banker owner.\n\tbanker banker.Banker // The underlying std banker, must be a BankerTypeRealmSend.\n}\n\nvar _ Banker = (*CoinsBanker)(nil)\n\n// ID implements Banker.\nfunc (CoinsBanker) ID() string {\n\treturn \"Coins\"\n}\n\n// Send implements Banker.\n//\n// rlm must be the caller's own captured cur (i.e. the cur of the\n// immediate crossing-function caller). Sending with rlm = cur.Previous()\n// or any other realm value is rejected: rlm.IsCurrent() asserts pointer\n// identity against the topmost crossing frame. Combined with the\n// rlm.Address() == cb.owner check, this restricts Send to the owning\n// realm acting in its own frame.\nfunc (cb *CoinsBanker) Send(_ int, rlm realm, p Payment) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrSpoofedRealm\n\t}\n\tif rlm.Address() != cb.owner {\n\t\treturn ErrCurrentRealmIsNotOwner\n\t}\n\t// Check if payment is of type coinsPayment.\n\tpayment, ok := p.(coinsPayment)\n\tif !ok {\n\t\treturn ErrInvalidPaymentType\n\t}\n\n\t// Send the coins.\n\treturn panictoerr.PanicToError(func() {\n\t\tcb.banker.SendCoins(cb.owner, payment.toAddress, payment.coins)\n\t})\n}\n\n// Balances implements Banker.\nfunc (cb *CoinsBanker) Balances() []Balance {\n\t// Get the coins from the banker.\n\tcoins := cb.banker.GetCoins(cb.owner)\n\n\t// Convert banker.Coins to []Balance.\n\tbalances := make([]Balance, len(coins))\n\tfor i := range coins {\n\t\tbalances[i] = Balance{\n\t\t\tDenom:  coins[i].Denom,\n\t\t\tAmount: coins[i].Amount,\n\t\t}\n\t}\n\n\treturn balances\n}\n\n// Address implements Banker.\nfunc (cb *CoinsBanker) Address() string {\n\treturn cb.owner.String()\n}\n\n// NewCoinsBankerWithOwner creates a new CoinsBanker with the given address.\n//\n// banker_ must be the canonical Banker produced by banker.NewBanker;\n// hand-rolled Banker implementations (no-op fakes, decorators) are\n// rejected via banker.IsCanonical. Without this check, a callee\n// receiving a *CoinsBanker constructed from a fake banker would not\n// be able to tell that Send is a no-op (no real coins move). The\n// pkgAddr-vs-owner mismatch is still surfaced lazily by the inner\n// banker's own SendCoins check.\nfunc NewCoinsBankerWithOwner(owner address, banker_ banker.Banker) (*CoinsBanker, error) {\n\tif owner == \"\" {\n\t\treturn nil, ErrNoOwnerProvided\n\t}\n\n\tif !banker.IsCanonical(banker_) {\n\t\treturn nil, ErrNonCanonicalBanker\n\t}\n\n\treturn \u0026CoinsBanker{\n\t\towner:  owner,\n\t\tbanker: banker_,\n\t}, nil\n}\n\n// coinsPayment represents a payment that is issued by a CoinsBanker.\ntype coinsPayment struct {\n\tcoins     chain.Coins // The coins being sent.\n\ttoAddress address     // The recipient of the payment.\n}\n\nvar _ Payment = (*coinsPayment)(nil)\n\n// BankerID implements Payment.\nfunc (coinsPayment) BankerID() string {\n\treturn CoinsBanker{}.ID()\n}\n\n// String implements Payment.\nfunc (cp coinsPayment) String() string {\n\treturn cp.coins.String() + \" to \" + cp.toAddress.String()\n}\n\n// NewCoinsPayment creates a new coinsPayment.\nfunc NewCoinsPayment(coins chain.Coins, toAddress address) Payment {\n\treturn coinsPayment{\n\t\tcoins:     coins,\n\t\ttoAddress: toAddress,\n\t}\n}\n"},{"name":"banker_coins_filetest.gno","body":"// PKGPATH: gno.land/r/treasury/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/treasury/v0\"\n)\n\nfunc main(cur realm) {\n\t// Define addresses for the sender (owner) and destination.\n\townerAddr := chain.PackageAddress(\"gno.land/r/treasury/main\")\n\tdestAddr := chain.PackageAddress(\"gno.land/r/dest/main\")\n\n\t// Create a CoinsBanker.\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tcbanker, err := treasury.NewCoinsBankerWithOwner(ownerAddr, banker_)\n\tif err != nil {\n\t\tpanic(\"failed to create CoinsBanker: \" + err.Error())\n\t}\n\n\tprintln(\"CoinsBanker ID:\", cbanker.ID())\n\tprintln(\"CoinsBanker Address:\", cbanker.Address())\n\n\t// Check if the CoinsBanker address matches the owner address.\n\tif cbanker.Address() != ownerAddr.String() {\n\t\tpanic(\"CoinsBanker address does not match current realm address\")\n\t}\n\n\tprintln(\"CoinsBanker Balances count:\", len(cbanker.Balances()))\n\n\t// Issue some coins to the owner address.\n\ttesting.IssueCoins(ownerAddr, chain.NewCoins(chain.NewCoin(\"ugnot\", 42)))\n\n\tprintln(\"CoinsBanker Balances count:\", len(cbanker.Balances()))\n\tprintln(\"Ugnot balance:\", cbanker.Balances()[0].Amount)\n\n\t// Send a valid payment.\n\tvalidPayment := treasury.NewCoinsPayment(\n\t\tchain.NewCoins(chain.NewCoin(\"ugnot\", 10)),\n\t\tdestAddr,\n\t)\n\terr = cbanker.Send(0, cur, validPayment)\n\tprintln(\"Valid payment error:\", err)\n\tif err != nil {\n\t\tpanic(\"failed to send valid payment: \" + err.Error())\n\t}\n\n\tprintln(\"Ugnot balance:\", cbanker.Balances()[0].Amount)\n\n\t// Send a payment with an invalid type.\n\tinvalidPaymentType := treasury.NewGRC20Payment(\"\", 0, destAddr)\n\terr = cbanker.Send(0, cur, invalidPaymentType)\n\tprintln(\"Invalid payment type error:\", err)\n\tif err == nil {\n\t\tpanic(\"expected error for invalid payment type, but got none\")\n\t}\n\n\t// Issue another coin to the owner address to test the Balances method.\n\ttesting.IssueCoins(ownerAddr, chain.NewCoins(chain.NewCoin(\"anothercoin\", 1337)))\n\n\tprintln(\"CoinsBanker Balances count:\", len(cbanker.Balances()))\n}\n\n// Output:\n// CoinsBanker ID: Coins\n// CoinsBanker Address: g1ynsdz5zaxhn9gnqtr6t40m5k4fueeutq7xy224\n// CoinsBanker Balances count: 0\n// CoinsBanker Balances count: 1\n// Ugnot balance: 42\n// Valid payment error: undefined\n// Ugnot balance: 32\n// Invalid payment type error: invalid payment type\n// CoinsBanker Balances count: 2\n"},{"name":"banker_grc20.gno","body":"package treasury\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tErrNoListerProvided   = errors.New(\"no lister provided\")\n\tErrGRC20TokenNotFound = errors.New(\"GRC20 token not found\")\n)\n\n// GRC20Banker is a Banker that sends GRC20 tokens listed using a getter\n// set during initialization.\ntype GRC20Banker struct {\n\towner  address         // The address of this GRC20 banker owner.\n\tlister TokenListerFunc // Allows to list tokens from methods that require it.\n}\n\n// TokenListerFunc is a function type that returns a map of GRC20 tokens.\ntype TokenListerFunc func() map[string]*grc20.Token\n\nvar _ Banker = (*GRC20Banker)(nil)\n\n// ID implements Banker.\nfunc (GRC20Banker) ID() string {\n\treturn \"GRC20\"\n}\n\n// Send implements Banker.\n//\n// rlm must be the caller's own captured cur (i.e. the cur of the\n// immediate crossing-function caller). Sending with rlm = cur.Previous()\n// or any other realm value is rejected: rlm.IsCurrent() asserts pointer\n// identity against the topmost crossing frame. Combined with the\n// rlm.Address() == gb.owner check, this restricts Send to the owning\n// realm acting in its own frame.\nfunc (gb *GRC20Banker) Send(_ int, rlm realm, p Payment) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrSpoofedRealm\n\t}\n\tif rlm.Address() != gb.owner {\n\t\treturn ErrCurrentRealmIsNotOwner\n\t}\n\n\tpayment, ok := p.(grc20Payment)\n\tif !ok {\n\t\treturn ErrInvalidPaymentType\n\t}\n\n\t// Get the GRC20 tokens using the lister.\n\ttokens := gb.lister()\n\n\t// Look for the token corresponding to the payment tokenKey.\n\ttoken, ok := tokens[payment.tokenKey]\n\tif !ok {\n\t\treturn ufmt.Errorf(\"%v: %s\", ErrGRC20TokenNotFound, payment.tokenKey)\n\t}\n\n\t// Send the token from the owner's balance.\n\treturn token.RealmTeller(0, rlm).Transfer(0, rlm, payment.toAddress, payment.amount)\n}\n\n// Balances implements Banker.\nfunc (gb *GRC20Banker) Balances() []Balance {\n\t// Get the GRC20 tokens from the lister.\n\ttokens := gb.lister()\n\n\t// Convert GRC20 tokens to []Balance.\n\tvar balances []Balance\n\tfor key, token := range tokens {\n\t\tbalances = append(balances, Balance{\n\t\t\tDenom:  key,\n\t\t\tAmount: token.BalanceOf(gb.owner),\n\t\t})\n\t}\n\treturn balances\n}\n\n// Address implements Banker.\nfunc (gb *GRC20Banker) Address() string {\n\treturn gb.owner.String()\n}\n\n// NewGRC20BankerWithOwner creates a new GRC20Banker with the given address.\nfunc NewGRC20BankerWithOwner(owner address, lister TokenListerFunc) (*GRC20Banker, error) {\n\tif owner == \"\" {\n\t\treturn nil, ErrNoOwnerProvided\n\t}\n\n\tif lister == nil {\n\t\treturn nil, ErrNoListerProvided\n\t}\n\n\treturn \u0026GRC20Banker{\n\t\towner:  owner,\n\t\tlister: lister,\n\t}, nil\n}\n\n// grc20Payment represents a payment that is issued by a GRC20Banker.\ntype grc20Payment struct {\n\ttokenKey  string  // The key associated with the GRC20 token.\n\tamount    int64   // The amount of token to send.\n\ttoAddress address // The recipient of the payment.\n}\n\nvar _ Payment = (*grc20Payment)(nil)\n\n// BankerID implements Payment.\nfunc (grc20Payment) BankerID() string {\n\treturn GRC20Banker{}.ID()\n}\n\n// String implements Payment.\nfunc (gp grc20Payment) String() string {\n\tamount := strconv.Itoa(int(gp.amount))\n\treturn amount + gp.tokenKey + \" to \" + gp.toAddress.String()\n}\n\n// NewGRC20Payment creates a new grc20Payment.\nfunc NewGRC20Payment(tokenKey string, amount int64, toAddress address) Payment {\n\treturn grc20Payment{\n\t\ttokenKey:  tokenKey,\n\t\tamount:    amount,\n\t\ttoAddress: toAddress,\n\t}\n}\n"},{"name":"banker_grc20_filetest.gno","body":"// PKGPATH: gno.land/r/treasury/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"strings\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/treasury/v0\"\n)\n\nconst amount = int64(1000)\n\nfunc createToken(_ int, rlm realm, name string, toMint address) *grc20.Token {\n\t// Create the token.\n\tsymbol := strings.ToUpper(name)\n\ttoken, ledger := grc20.NewToken(0, rlm, name, symbol, 0)\n\n\t// Mint the requested amount.\n\tledger.Mint(toMint, amount)\n\n\treturn token\n}\n\nfunc main(cur realm) {\n\t// Define addresses for the sender (owner) and destination.\n\townerAddr := chain.PackageAddress(\"gno.land/r/treasury/main\")\n\tdestAddr := chain.PackageAddress(\"gno.land/r/dest/main\")\n\n\t// Try to create a GRC20Banker using a nil lister.\n\tgbanker, err := treasury.NewGRC20BankerWithOwner(ownerAddr, nil)\n\tif err == nil {\n\t\tpanic(\"expected error when creating GRC20Banker with nil lister\")\n\t}\n\n\t// Define a list of token and the associated lister.\n\ttokens := []*grc20.Token{\n\t\tcreateToken(0, cur, \"TestToken0\", ownerAddr),\n\t\tcreateToken(0, cur, \"TestToken1\", ownerAddr),\n\t\tcreateToken(0, cur, \"TestToken2\", ownerAddr),\n\t}\n\n\tgrc20Lister := func() map[string]*grc20.Token {\n\t\ttokensMap := make(map[string]*grc20.Token, len(tokens))\n\n\t\tfor _, token := range tokens {\n\t\t\ttokensMap[token.GetSymbol()] = token\n\t\t}\n\n\t\treturn tokensMap\n\t}\n\n\t// Create a GRC20Banker.\n\tgbanker, err = treasury.NewGRC20BankerWithOwner(ownerAddr, grc20Lister)\n\tif err != nil {\n\t\tpanic(\"failed to create GRC20Banker: \" + err.Error())\n\t}\n\n\tprintln(\"GRC20Banker ID:\", gbanker.ID())\n\tprintln(\"GRC20Banker Address:\", gbanker.Address())\n\n\t// Check if the GRC20Banker address matches the owner address.\n\tif gbanker.Address() != ownerAddr.String() {\n\t\tpanic(\"GRC20Banker address does not match current realm address\")\n\t}\n\n\t// Check the balances of the GRC20Banker.\n\tprintln(\"GRC20Banker Balances count:\", len(gbanker.Balances()))\n\tfor _, balance := range gbanker.Balances() {\n\t\tif balance.Amount != amount {\n\t\t\tpanic(\"GRC20Banker balance does not match expected amount\")\n\t\t}\n\t}\n\n\t// Send a valid payment.\n\ttoken := tokens[len(tokens)-1]\n\tvalidPayment := treasury.NewGRC20Payment(\n\t\ttoken.GetSymbol(),\n\t\t100,\n\t\tdestAddr,\n\t)\n\terr = gbanker.Send(0, cur, validPayment)\n\tprintln(\"Valid payment error:\", err)\n\tif err != nil {\n\t\tpanic(\"failed to send valid payment: \" + err.Error())\n\t}\n\n\tprintln(\"Owner balance:\", token.BalanceOf(ownerAddr))\n\tprintln(\"Dest balance:\", token.BalanceOf(destAddr))\n\n\t// Send an unknown token payment.\n\tunknownPayment := treasury.NewGRC20Payment(\n\t\t\"unknown\",\n\t\t100,\n\t\tdestAddr,\n\t)\n\terr = gbanker.Send(0, cur, unknownPayment)\n\tprintln(\"Unknown token payment error:\", err)\n\tif err == nil {\n\t\tpanic(\"expected error for unknown token, but got none\")\n\t}\n\n\t// Send an unsufficient funds payment.\n\tunsufficientPayment := treasury.NewGRC20Payment(\n\t\ttokens[0].GetSymbol(),\n\t\tamount+1,\n\t\tdestAddr,\n\t)\n\terr = gbanker.Send(0, cur, unsufficientPayment)\n\tprintln(\"Unsufficient funds payment error:\", err)\n\tif err == nil {\n\t\tpanic(\"expected error for insufficient funds, but got none\")\n\t}\n\n\t// Send a payment with an invalid type.\n\tinvalidPaymentType := treasury.NewCoinsPayment(chain.Coins{}, destAddr)\n\terr = gbanker.Send(0, cur, invalidPaymentType)\n\tprintln(\"Invalid payment type error:\", err)\n\tif err == nil {\n\t\tpanic(\"expected error for invalid payment type, but got none\")\n\t}\n}\n\n// Output:\n// GRC20Banker ID: GRC20\n// GRC20Banker Address: g1ynsdz5zaxhn9gnqtr6t40m5k4fueeutq7xy224\n// GRC20Banker Balances count: 3\n// Valid payment error: undefined\n// Owner balance: 900\n// Dest balance: 100\n// Unknown token payment error: GRC20 token not found: unknown\n// Unsufficient funds payment error: insufficient balance\n// Invalid payment type error: invalid payment type\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package treasury provides treasury management for handling coin and GRC20\n// token transfers in Gno realms.\npackage treasury\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/treasury/v0\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package treasury\n\nimport (\n\t\"net/url\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/nt/bptree/v0/pager\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst (\n\tDefaultHistoryPreviewSize = 5  // Number of payments in the history preview.\n\tDefaultHistoryPageSize    = 20 // Number of payments per page in the history.\n)\n\n// Render renders content based on the given path.\nfunc (t *Treasury) Render(path string) string {\n\treturn t.router.Render(path)\n}\n\n// RenderLanding renders the landing page of the treasury.\nfunc (t *Treasury) RenderLanding(path string) string {\n\tvar out string\n\n\t// Render each banker.\n\tfor _, bankerID := range t.ListBankerIDs() {\n\t\tout += t.RenderBanker(bankerID, path)\n\t}\n\n\treturn out\n}\n\n// RenderBanker renders the details of a specific banker.\nfunc (t *Treasury) RenderBanker(bankerID string, path string) string {\n\t// Get the banker associated to this ID.\n\tbr := t.bankers.Get(bankerID)\n\tif br == nil {\n\t\treturn md.Paragraph(\"Banker not found: \" + bankerID)\n\t}\n\tbanker := br.(*bankerRecord).banker\n\n\t// Render banker title.\n\tout := md.H2(bankerID + \" Banker\")\n\n\t// Render address section.\n\tout += md.H3(\"Address\")\n\tout += md.Paragraph(banker.Address())\n\n\t// Render balances section.\n\tout += md.H3(\"Balances\")\n\tbalances := banker.Balances()\n\tif len(balances) == 0 {\n\t\tout += md.Paragraph(\"No balances found.\")\n\t} else {\n\t\ttable := mdtable.Table{Headers: []string{\"Denom\", \"Amount\"}}\n\t\tfor _, balance := range balances {\n\t\t\ttable.Append([]string{balance.Denom, strconv.FormatInt(balance.Amount, 10)})\n\t\t}\n\t\tout += table.String()\n\t}\n\n\thistorySize := DefaultHistoryPreviewSize\n\n\t// Check if the query parameter \"history_size\" is present and parse it.\n\tif req, err := url.Parse(path); err == nil \u0026\u0026 req.Query() != nil {\n\t\tsize, err := strconv.Atoi(req.Query().Get(\"history_size\"))\n\t\tif err == nil \u0026\u0026 size \u003e= 0 {\n\t\t\thistorySize = size\n\t\t}\n\t}\n\n\t// Skip history rendering if historySize is 0.\n\tif historySize == 0 {\n\t\treturn out\n\t}\n\n\t// Render history section.\n\tout += md.H3(\"History\")\n\thistory, _ := t.History(bankerID, 1, historySize)\n\tif len(history) == 0 {\n\t\tout += md.Paragraph(\"No payments sent yet.\")\n\t} else {\n\t\tif len(history) == 1 {\n\t\t\tout += md.Paragraph(\"Last payment:\")\n\t\t} else {\n\t\t\tcount := strconv.FormatInt(int64(len(history)), 10)\n\t\t\tout += md.Paragraph(\"Last \" + count + \" payments:\")\n\t\t}\n\n\t\t// Render each payment in the history.\n\t\tfor _, payment := range history {\n\t\t\tout += md.BulletItem(payment.String())\n\t\t}\n\t\tout += \"\\n\"\n\n\t\t// Build the \"See full history\" link from the owning realm's\n\t\t// path captured at New() time. Skipped if no path was supplied\n\t\t// (e.g. /p/ filetests that don't exercise rendering).\n\t\tif from := strings.IndexRune(t.realmPath, '/'); from \u003e= 0 {\n\t\t\tout += md.Link(\n\t\t\t\t\"See full history\",\n\t\t\t\tufmt.Sprintf(\"%s:%s/history\", t.realmPath[from:], bankerID),\n\t\t\t)\n\t\t}\n\t}\n\n\treturn out\n}\n\n// RenderBankerHistory renders the payment history of a specific banker.\nfunc (t *Treasury) RenderBankerHistory(bankerID string, path string) string {\n\t// Get the banker record corresponding to this ID if it exists.\n\tbr := t.bankers.Get(bankerID)\n\tif br == nil {\n\t\treturn md.Paragraph(\"Banker not found: \" + bankerID)\n\t}\n\thistory := br.(*bankerRecord).history\n\n\t// Render banker history title.\n\tout := md.H2(bankerID + \" Banker History\")\n\n\t// Get the current page of tokens based on the request path.\n\tp := pager.NewPager(history.Tree(), DefaultHistoryPageSize, true)\n\tpage, err := p.GetPageByPath(path)\n\tif err != nil {\n\t\treturn md.Paragraph(\"Error retrieving page: \" + err.Error())\n\t}\n\n\t// Render full history section.\n\tif history.Len() == 0 {\n\t\tout += md.Paragraph(\"No payments sent yet.\")\n\t} else {\n\t\tif history.Len() == 1 {\n\t\t\tout += md.Paragraph(\"1 payment:\")\n\t\t} else {\n\t\t\tcount := strconv.FormatInt(int64(history.Len()), 10)\n\t\t\tout += md.Paragraph(count + \" payments (sorted by latest, descending):\")\n\t\t}\n\t\tfor _, item := range page.Items {\n\t\t\tout += md.BulletItem(item.Value.(Payment).String())\n\t\t}\n\t}\n\tout += \"\\n\"\n\n\t// Add the page picker.\n\tout += md.Paragraph(page.Picker(path))\n\n\treturn out\n}\n\n// initRenderRouter registers the routes for rendering the treasury pages.\nfunc (t *Treasury) initRenderRouter() {\n\tt.router = mux.NewRouter()\n\n\t// Landing page.\n\tt.router.HandleFunc(\"\", func(res *mux.ResponseWriter, req *mux.Request) {\n\t\tres.Write(t.RenderLanding(req.RawPath))\n\t})\n\n\t// Banker details.\n\tt.router.HandleFunc(\"{banker}\", func(res *mux.ResponseWriter, req *mux.Request) {\n\t\tres.Write(t.RenderBanker(req.GetVar(\"banker\"), req.RawPath))\n\t})\n\n\t// Banker full history.\n\tt.router.HandleFunc(\"{banker}/history\", func(res *mux.ResponseWriter, req *mux.Request) {\n\t\tres.Write(t.RenderBankerHistory(req.GetVar(\"banker\"), req.RawPath))\n\t})\n}\n"},{"name":"treasury.gno","body":"package treasury\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/bptree/v0/pager\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tErrNoBankerProvided       = errors.New(\"no banker provided\")\n\tErrDuplicateBanker        = errors.New(\"duplicate banker\")\n\tErrBankerNotFound         = errors.New(\"banker not found\")\n\tErrSendPaymentFailed      = errors.New(\"failed to send payment\")\n\tErrNonCanonicalBankerImpl = errors.New(\"non-canonical Banker impl: only *CoinsBanker / *GRC20Banker accepted\")\n)\n\n// New creates a new Treasury instance with the given bankers.\n//\n// pkgPath should be the package path of the owning realm. It is\n// captured on the Treasury and used to build the \"See full history\"\n// link in RenderBanker. Pass `cur.PkgPath()` from the owning realm's\n// init (or another live-cur context). Passing \"\" disables the link.\n//\n// The path is stored as plain data — no IsCurrent guard inside this\n// /p/ constructor. The caller is the trust boundary; it must derive\n// the value from a real `cur realm` rather than accept it from\n// untrusted input. The captured path is consumed only for render\n// output (Class-2 designation-forgery shape, accepted as\n// display-only — see docs/resources/gno-security.md).\n//\n// Each banker must be one of treasury's canonical concrete impls\n// (*CoinsBanker, *GRC20Banker) per IsCanonicalBanker. Foreign-realm impls\n// (including embedded wrappers around canonical types) are rejected: a\n// malicious Send impl would receive a capability token via its rlm\n// parameter when treasury.Send dispatches into it.\n//\n// The allowlist validates type only, not captured state. Treasury operators\n// must construct their own bankers; never accept a pre-built *Banker value\n// from an external realm.\nfunc New(bankers []Banker, pkgPath string) (*Treasury, error) {\n\tif len(bankers) == 0 {\n\t\treturn nil, ErrNoBankerProvided\n\t}\n\n\t// Canonical-impl allowlist: reject foreign types (embedding-based\n\t// bypasses fail here because type assertions are nominal).\n\tfor _, b := range bankers {\n\t\tif !IsCanonicalBanker(b) {\n\t\t\treturn nil, ErrNonCanonicalBankerImpl\n\t\t}\n\t}\n\n\t// Create a new Treasury instance.\n\ttreasury := \u0026Treasury{bankers: bptree.NewBPTree32(), realmPath: pkgPath}\n\n\t// Register the bankers.\n\tfor _, banker := range bankers {\n\t\tif treasury.bankers.Has(banker.ID()) {\n\t\t\treturn nil, ufmt.Errorf(\"%v: %s\", ErrDuplicateBanker, banker.ID())\n\t\t}\n\n\t\ttreasury.bankers.Set(\n\t\t\tbanker.ID(),\n\t\t\t\u0026bankerRecord{banker: banker},\n\t\t)\n\t}\n\n\t// Register the Render routes.\n\ttreasury.initRenderRouter()\n\n\treturn treasury, nil\n}\n\n// Send sends a payment using the corresponding banker. rlm is threaded\n// to the banker's Send for IsCurrent + owner validation.\nfunc (t *Treasury) Send(_ int, rlm realm, p Payment) error {\n\t// Get the banker record corresponding to this Payment.\n\tbr := t.bankers.Get(p.BankerID())\n\tif br == nil {\n\t\treturn ufmt.Errorf(\"%v: %s\", ErrBankerNotFound, p.BankerID())\n\t}\n\trecord := br.(*bankerRecord)\n\n\t// Send the payment using the corresponding banker.\n\tif err := record.banker.Send(0, rlm, p); err != nil {\n\t\treturn ufmt.Errorf(\"%v: %s\", ErrSendPaymentFailed, err)\n\t}\n\n\t// Add the payment to the history of the banker.\n\trecord.history.Append(p)\n\n\treturn nil\n}\n\n// History returns the payment history sent by the banker with the given ID.\n// Payments are paginated, with the most recent payments first.\nfunc (t *Treasury) History(\n\tbankerID string,\n\tpageNumber int,\n\tpageSize int,\n) ([]Payment, error) {\n\t// Get the banker record corresponding to this ID.\n\tbr := t.bankers.Get(bankerID)\n\tif br == nil {\n\t\treturn nil, ufmt.Errorf(\"%v: %s\", ErrBankerNotFound, bankerID)\n\t}\n\thistory := br.(*bankerRecord).history\n\n\t// Get the page of payments from the history.\n\tp := pager.NewPager(history.Tree(), pageSize, true)\n\tpage := p.GetPage(pageNumber)\n\n\t// Convert the items in the page to a slice of Payments.\n\tpayments := make([]Payment, len(page.Items))\n\tfor i := range page.Items {\n\t\tpayments[i] = page.Items[i].Value.(Payment)\n\t}\n\n\treturn payments, nil\n}\n\n// Balances returns the balances of the banker with the given ID.\nfunc (t *Treasury) Balances(bankerID string) ([]Balance, error) {\n\t// Get the banker record corresponding to this ID.\n\tbr := t.bankers.Get(bankerID)\n\tif br == nil {\n\t\treturn nil, ufmt.Errorf(\"%v: %s\", ErrBankerNotFound, bankerID)\n\t}\n\n\t// Get the balances from the banker.\n\treturn br.(*bankerRecord).banker.Balances(), nil\n}\n\n// Address returns the address of the banker with the given ID.\nfunc (t *Treasury) Address(bankerID string) (string, error) {\n\t// Get the banker record corresponding to this ID.\n\tbr := t.bankers.Get(bankerID)\n\tif br == nil {\n\t\treturn \"\", ufmt.Errorf(\"%v: %s\", ErrBankerNotFound, bankerID)\n\t}\n\n\t// Get the address from the banker.\n\treturn br.(*bankerRecord).banker.Address(), nil\n}\n\n// HasBanker checks if a banker with the given ID is registered.\nfunc (t *Treasury) HasBanker(bankerID string) bool {\n\treturn t.bankers.Has(bankerID)\n}\n\n// ListBankerIDs returns a list of all registered banker IDs.\nfunc (t *Treasury) ListBankerIDs() []string {\n\tvar bankerIDs []string\n\n\tt.bankers.Iterate(\"\", \"\", func(bankerID string, _ any) bool {\n\t\tbankerIDs = append(bankerIDs, bankerID)\n\t\treturn false\n\t})\n\n\treturn bankerIDs\n}\n"},{"name":"treasury_filetest.gno","body":"// PKGPATH: gno.land/r/treasury/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/treasury/v0\"\n)\n\nfunc checkBalanceAndHistory(t *treasury.Treasury, bankerIDs []string) {\n\tfor _, bankerID := range bankerIDs {\n\t\tbalances, err := t.Balances(bankerID)\n\t\tif err != nil {\n\t\t\tpanic(\"failed to get banker balances: \" + err.Error())\n\t\t}\n\t\tprintln(\"Banker\", bankerID, \"Balance:\", balances[0].Amount)\n\n\t\thistory, err := t.History(bankerID, 1, 10)\n\t\tif err != nil {\n\t\t\tpanic(\"failed to get banker history: \" + err.Error())\n\t\t}\n\t\tprintln(\"Banker\", bankerID, \"History count:\", len(history))\n\t}\n}\n\nfunc main(cur realm) {\n\t// Define addresses for the sender (owner) and destination.\n\townerAddr := chain.PackageAddress(\"gno.land/r/treasury/main\")\n\tdestAddr := chain.PackageAddress(\"gno.land/r/dest/main\")\n\n\t// Try to create a Treasury instance with no bankers.\n\t_, err := treasury.New(nil, \"\")\n\tif err != treasury.ErrNoBankerProvided {\n\t\tpanic(\"expected error when creating Treasury with no bankers\")\n\t}\n\n\t// Define a token and the associated lister.\n\tconst amount = int64(1000)\n\ttoken, ledger := grc20.NewToken(0, cur, \"TestToken\", \"TEST\", 0)\n\tledger.Mint(ownerAddr, amount)\n\n\tgrc20Lister := func() map[string]*grc20.Token {\n\t\treturn map[string]*grc20.Token{\n\t\t\t\"TEST\": token,\n\t\t}\n\t}\n\n\t// Try to create a Treasury instance with a duplicate banker.\n\tvar (\n\t\tbanker_           = banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\t\tcoinsBanker, _    = treasury.NewCoinsBankerWithOwner(ownerAddr, banker_)\n\t\tgrc20Banker, _    = treasury.NewGRC20BankerWithOwner(ownerAddr, grc20Lister)\n\t\tgrc20BankerDup, _ = treasury.NewGRC20BankerWithOwner(ownerAddr, grc20Lister)\n\t)\n\n\t_, err = treasury.New([]treasury.Banker{coinsBanker, grc20Banker, grc20BankerDup}, \"\")\n\tif !strings.Contains(err.Error(), treasury.ErrDuplicateBanker.Error()) {\n\t\tpanic(\"expected error when creating Treasury with duplicate banker\")\n\t}\n\n\t// Create a Treasury instance with valid bankers.\n\tbankers := []treasury.Banker{coinsBanker, grc20Banker}\n\tt, err := treasury.New(bankers, \"\")\n\tif err != nil {\n\t\tpanic(\"failed to create Treasury: \" + err.Error())\n\t}\n\n\t// Test if the Treasury instance has the expected bankers.\n\tprintln(\"Treasury banker IDs:\", t.ListBankerIDs())\n\n\tconst unknownBankerID = \"unknown-banker-id\"\n\n\tif t.HasBanker(unknownBankerID) {\n\t\tpanic(\"expected banker not to be found\")\n\t}\n\n\t// Check if the addresses of the bankers matches the owner address.\n\tfor _, banker_ := range bankers {\n\t\taddr, err := t.Address(banker_.ID())\n\t\tif err != nil {\n\t\t\tpanic(\"failed to get banker address: \" + err.Error())\n\t\t}\n\t\tprintln(\"Banker\", banker_.ID(), \"Address:\", addr)\n\t}\n\n\t// Check if the balances and history of the bankers match the expected values.\n\ttesting.IssueCoins(ownerAddr, chain.NewCoins(chain.NewCoin(\"ugnot\", amount)))\n\tbankerIDs := []string{coinsBanker.ID(), grc20Banker.ID()}\n\tcheckBalanceAndHistory(t, bankerIDs)\n\n\t// Send 3 valid payments using the CoinsBanker.\n\tvalidCoinsPayment := treasury.NewCoinsPayment(\n\t\tchain.NewCoins(chain.NewCoin(\"ugnot\", 100)),\n\t\tdestAddr,\n\t)\n\tfor i := 0; i \u003c 3; i++ {\n\t\terr = t.Send(0, cur, validCoinsPayment)\n\t\tif err != nil {\n\t\t\tpanic(\"failed to send valid Coins payment: \" + err.Error())\n\t\t}\n\t}\n\n\t// Send 3 valid payments using the GRC20Banker.\n\tvalidGRC20Payment := treasury.NewGRC20Payment(\n\t\ttoken.GetSymbol(),\n\t\t100,\n\t\tdestAddr,\n\t)\n\tfor i := 0; i \u003c 3; i++ {\n\t\terr = t.Send(0, cur, validGRC20Payment)\n\t\tif err != nil {\n\t\t\tpanic(\"failed to send valid GRC20 payment: \" + err.Error())\n\t\t}\n\t}\n\n\t// Check if the balances and history of the bankers match the expected values.\n\tcheckBalanceAndHistory(t, bankerIDs)\n}\n\n// Output:\n// Treasury banker IDs: slice[(\"Coins\" string),(\"GRC20\" string)]\n// Banker Coins Address: g1ynsdz5zaxhn9gnqtr6t40m5k4fueeutq7xy224\n// Banker GRC20 Address: g1ynsdz5zaxhn9gnqtr6t40m5k4fueeutq7xy224\n// Banker Coins Balance: 1000\n// Banker Coins History count: 0\n// Banker GRC20 Balance: 1000\n// Banker GRC20 History count: 0\n// Banker Coins Balance: 700\n// Banker Coins History count: 3\n// Banker GRC20 Balance: 700\n// Banker GRC20 History count: 3\n"},{"name":"types.gno","body":"package treasury\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/bptree/v0/list\"\n\t\"gno.land/p/nt/mux/v0\"\n)\n\n// Treasury is the main structure that holds all bankers and their payment\n// history. It also provides a router for rendering the treasury pages.\ntype Treasury struct {\n\tbankers   *bptree.BPTree // string -\u003e *bankerRecord\n\trouter    *mux.Router\n\trealmPath string // owning realm's PkgPath, captured at New() (used for render links)\n}\n\n// bankerRecord holds a Banker and its payment history.\ntype bankerRecord struct {\n\tbanker  Banker\n\thistory list.List // List of Payment.\n}\n\n// Banker is an interface that allows for banking operations.\n//\n// SECURITY: Send takes (int, realm, Payment), so handing a Banker value to\n// untrusted code yields a capability token to whatever Send impl that code\n// dispatches into. The set of canonical impls is closed (*CoinsBanker,\n// *GRC20Banker); any public function that accepts a Banker as a parameter\n// from external callers MUST verify it via IsCanonicalBanker and reject\n// otherwise. treasury.New enforces this for its own intake; future\n// Banker-accepting APIs must do the same. An unexported-marker \"seal\" does\n// NOT defend against this — see\n// p/test/seal/filetests/z_seal_iface_embedding_filetest.gno.\n//\n// Note that IsCanonicalBanker validates dynamic TYPE only, not captured\n// STATE: a canonical *CoinsBanker constructed via NewCoinsBankerWithOwner\n// with a hostile owner argument passes the allowlist but its read methods\n// (Balances, Address) report data tied to that hostile address. Treasury\n// operators must construct their own bankers and NEVER accept pre-built\n// *Banker values from external realms.\ntype Banker interface {\n\tID() string                     // Get the ID of the banker.\n\tSend(int, realm, Payment) error // Send a payment to a recipient.\n\tBalances() []Balance            // Get the balances of the banker.\n\tAddress() string                // Get the address of the banker to receive payments.\n}\n\n// IsCanonicalBanker reports whether b is one of treasury's canonical\n// concrete Banker impls. Use this at any public entry point in /p/ or /r/\n// that accepts a Banker from an external caller before invoking its methods.\n//\n// Foreign types — including embedding-based wrappers like\n// `type Evil struct { *CoinsBanker }` — are rejected because type assertions\n// are nominal: *Evil is not *CoinsBanker, regardless of method promotion.\n//\n// To add a new canonical type: extend the switch below AND add a regression\n// test (under filetests/ in this package) that an embedded-impl bypass is\n// rejected.\n//\n// Mirrors the precedent of chain/banker.IsCanonical and\n// p/jaekwon/allowancesender's canonical-impl check.\nfunc IsCanonicalBanker(b Banker) bool {\n\tswitch b.(type) {\n\tcase *CoinsBanker, *GRC20Banker:\n\t\treturn true\n\tdefault:\n\t\treturn false\n\t}\n}\n\n// Payment is an interface that allows getting details about a payment.\ntype Payment interface {\n\tBankerID() string // Get the ID of the banker that can process this payment.\n\tString() string   // Get a string representation of the payment.\n}\n\n// Balance represents the balance of an asset held by a Banker.\ntype Balance struct {\n\tDenom  string // The denomination of the asset\n\tAmount int64  // The amount of the asset\n}\n\n// Common Banker errors.\nvar (\n\tErrCurrentRealmIsNotOwner = errors.New(\"current realm is not the owner of the banker\")\n\tErrNoOwnerProvided        = errors.New(\"no owner provided\")\n\tErrInvalidPaymentType     = errors.New(\"invalid payment type\")\n\tErrSpoofedRealm           = errors.New(\"rlm does not match the current crossing frame\")\n)\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"hYGvpeX9tm7tL50HluhwjVKj4JP6G/Hf8oOPODVsHvv1cKaI15zED+Yn3JIoVAcNKNeWalhDn1FltANZ/iVGDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"uint256","path":"gno.land/p/onbloc/uint256","files":[{"name":"LICENSE","body":"BSD 3-Clause License\n\nCopyright 2020 uint256 Authors\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n1. Redistributions of source code must retain the above copyright notice, this\n   list of conditions and the following disclaimer.\n\n2. Redistributions in binary form must reproduce the above copyright notice,\n   this list of conditions and the following disclaimer in the documentation\n   and/or other materials provided with the distribution.\n\n3. Neither the name of the copyright holder nor the names of its\n   contributors may be used to endorse or promote products derived from\n   this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"},{"name":"README.md","body":"# Fixed size 256-bit math library\n\nThis is a library specialized at replacing the `big.Int` library for math based on 256-bit types.\n\noriginal repository: [uint256](\u003chttps://github.com/holiman/uint256/tree/master\u003e)\n"},{"name":"arithmetic.gno","body":"// arithmetic provides arithmetic operations for Uint objects.\n// This includes basic binary operations such as addition, subtraction, multiplication, division, and modulo operations\n// as well as overflow checks, and negation. These functions are essential for numeric\n// calculations using 256-bit unsigned integers.\npackage uint256\n\nimport (\n\t\"math/bits\"\n)\n\n// Add sets z to the sum x+y\nfunc (z *Uint) Add(x, y *Uint) *Uint {\n\tvar carry uint64\n\tz.arr[0], carry = bits.Add64(x.arr[0], y.arr[0], 0)\n\tz.arr[1], carry = bits.Add64(x.arr[1], y.arr[1], carry)\n\tz.arr[2], carry = bits.Add64(x.arr[2], y.arr[2], carry)\n\tz.arr[3], _ = bits.Add64(x.arr[3], y.arr[3], carry)\n\treturn z\n}\n\n// AddOverflow sets z to the sum x+y, and returns z and whether overflow occurred\nfunc (z *Uint) AddOverflow(x, y *Uint) (*Uint, bool) {\n\tvar carry uint64\n\tz.arr[0], carry = bits.Add64(x.arr[0], y.arr[0], 0)\n\tz.arr[1], carry = bits.Add64(x.arr[1], y.arr[1], carry)\n\tz.arr[2], carry = bits.Add64(x.arr[2], y.arr[2], carry)\n\tz.arr[3], carry = bits.Add64(x.arr[3], y.arr[3], carry)\n\treturn z, carry != 0\n}\n\n// Sub sets z to the difference x-y\nfunc (z *Uint) Sub(x, y *Uint) *Uint {\n\tvar carry uint64\n\tz.arr[0], carry = bits.Sub64(x.arr[0], y.arr[0], 0)\n\tz.arr[1], carry = bits.Sub64(x.arr[1], y.arr[1], carry)\n\tz.arr[2], carry = bits.Sub64(x.arr[2], y.arr[2], carry)\n\tz.arr[3], _ = bits.Sub64(x.arr[3], y.arr[3], carry)\n\treturn z\n}\n\n// SubOverflow sets z to the difference x-y and returns z and true if the operation underflowed\nfunc (z *Uint) SubOverflow(x, y *Uint) (*Uint, bool) {\n\tvar carry uint64\n\tz.arr[0], carry = bits.Sub64(x.arr[0], y.arr[0], 0)\n\tz.arr[1], carry = bits.Sub64(x.arr[1], y.arr[1], carry)\n\tz.arr[2], carry = bits.Sub64(x.arr[2], y.arr[2], carry)\n\tz.arr[3], carry = bits.Sub64(x.arr[3], y.arr[3], carry)\n\treturn z, carry != 0\n}\n\n// Neg returns -x mod 2^256.\nfunc (z *Uint) Neg(x *Uint) *Uint {\n\treturn z.Sub(new(Uint), x)\n}\n\n// commented out for possible overflow\n// Mul sets z to the product x*y\nfunc (z *Uint) Mul(x, y *Uint) *Uint {\n\tvar (\n\t\tres              Uint\n\t\tcarry            uint64\n\t\tres1, res2, res3 uint64\n\t)\n\n\tcarry, res.arr[0] = bits.Mul64(x.arr[0], y.arr[0])\n\tcarry, res1 = umulHop(carry, x.arr[1], y.arr[0])\n\tcarry, res2 = umulHop(carry, x.arr[2], y.arr[0])\n\tres3 = x.arr[3]*y.arr[0] + carry\n\n\tcarry, res.arr[1] = umulHop(res1, x.arr[0], y.arr[1])\n\tcarry, res2 = umulStep(res2, x.arr[1], y.arr[1], carry)\n\tres3 = res3 + x.arr[2]*y.arr[1] + carry\n\n\tcarry, res.arr[2] = umulHop(res2, x.arr[0], y.arr[2])\n\tres3 = res3 + x.arr[1]*y.arr[2] + carry\n\n\tres.arr[3] = res3 + x.arr[0]*y.arr[3]\n\n\treturn z.Set(\u0026res)\n}\n\n// MulOverflow sets z to the product x*y, and returns z and  whether overflow occurred\nfunc (z *Uint) MulOverflow(x, y *Uint) (*Uint, bool) {\n\tp := umul(x, y)\n\tcopy(z.arr[:], p[:4])\n\treturn z, (p[4] | p[5] | p[6] | p[7]) != 0\n}\n\n// commented out for possible overflow\n// Div sets z to the quotient x/y for returns z.\n// If y == 0, z is set to 0\nfunc (z *Uint) Div(x, y *Uint) *Uint {\n\tif y.IsZero() || y.Gt(x) {\n\t\treturn z.Clear()\n\t}\n\tif x.Eq(y) {\n\t\treturn z.SetOne()\n\t}\n\t// Shortcut some cases\n\tif x.IsUint64() {\n\t\treturn z.SetUint64(x.Uint64() / y.Uint64())\n\t}\n\n\t// At this point, we know\n\t// x/y ; x \u003e y \u003e 0\n\n\tvar quot Uint\n\tudivrem(quot.arr[:], x.arr[:], y)\n\treturn z.Set(\u0026quot)\n}\n\n// MulMod calculates the modulo-m multiplication of x and y and\n// returns z.\n// If m == 0, z is set to 0 (OBS: differs from the big.Int)\nfunc (z *Uint) MulMod(x, y, m *Uint) *Uint {\n\tif x.IsZero() || y.IsZero() || m.IsZero() {\n\t\treturn z.Clear()\n\t}\n\tp := umul(x, y)\n\n\tif m.arr[3] != 0 {\n\t\tmu := Reciprocal(m)\n\t\tr := reduce4(p, m, mu)\n\t\treturn z.Set(\u0026r)\n\t}\n\n\tvar (\n\t\tpl Uint\n\t\tph Uint\n\t)\n\n\tpl = Uint{arr: [4]uint64{p[0], p[1], p[2], p[3]}}\n\tph = Uint{arr: [4]uint64{p[4], p[5], p[6], p[7]}}\n\n\t// If the multiplication is within 256 bits use Mod().\n\tif ph.IsZero() {\n\t\treturn z.Mod(\u0026pl, m)\n\t}\n\n\tvar quot [8]uint64\n\trem := udivrem(quot[:], p[:], m)\n\treturn z.Set(\u0026rem)\n}\n\n// Mod sets z to the modulus x%y for y != 0 and returns z.\n// If y == 0, z is set to 0 (OBS: differs from the big.Uint)\nfunc (z *Uint) Mod(x, y *Uint) *Uint {\n\tif x.IsZero() || y.IsZero() {\n\t\treturn z.Clear()\n\t}\n\tswitch x.Cmp(y) {\n\tcase -1:\n\t\t// x \u003c y\n\t\tcopy(z.arr[:], x.arr[:])\n\t\treturn z\n\tcase 0:\n\t\t// x == y\n\t\treturn z.Clear() // They are equal\n\t}\n\n\t// At this point:\n\t// x != 0\n\t// y != 0\n\t// x \u003e y\n\n\t// Shortcut trivial case\n\tif x.IsUint64() {\n\t\treturn z.SetUint64(x.Uint64() % y.Uint64())\n\t}\n\n\tvar quot Uint\n\t*z = udivrem(quot.arr[:], x.arr[:], y)\n\treturn z\n}\n\n// DivMod sets z to the quotient x div y and m to the modulus x mod y and returns the pair (z, m) for y != 0.\n// If y == 0, both z and m are set to 0 (OBS: differs from the big.Int)\nfunc (z *Uint) DivMod(x, y, m *Uint) (*Uint, *Uint) {\n\tif y.IsZero() {\n\t\treturn z.Clear(), m.Clear()\n\t}\n\tvar quot Uint\n\t*m = udivrem(quot.arr[:], x.arr[:], y)\n\t*z = quot\n\treturn z, m\n}\n\n// Exp sets z = base**exponent mod 2**256, and returns z.\nfunc (z *Uint) Exp(base, exponent *Uint) *Uint {\n\tres := Uint{arr: [4]uint64{1, 0, 0, 0}}\n\tmultiplier := *base\n\texpBitLen := exponent.BitLen()\n\n\tcurBit := 0\n\tword := exponent.arr[0]\n\tfor ; curBit \u003c expBitLen \u0026\u0026 curBit \u003c 64; curBit++ {\n\t\tif word\u00261 == 1 {\n\t\t\tres.Mul(\u0026res, \u0026multiplier)\n\t\t}\n\t\tmultiplier.squared()\n\t\tword \u003e\u003e= 1\n\t}\n\n\tword = exponent.arr[1]\n\tfor ; curBit \u003c expBitLen \u0026\u0026 curBit \u003c 128; curBit++ {\n\t\tif word\u00261 == 1 {\n\t\t\tres.Mul(\u0026res, \u0026multiplier)\n\t\t}\n\t\tmultiplier.squared()\n\t\tword \u003e\u003e= 1\n\t}\n\n\tword = exponent.arr[2]\n\tfor ; curBit \u003c expBitLen \u0026\u0026 curBit \u003c 192; curBit++ {\n\t\tif word\u00261 == 1 {\n\t\t\tres.Mul(\u0026res, \u0026multiplier)\n\t\t}\n\t\tmultiplier.squared()\n\t\tword \u003e\u003e= 1\n\t}\n\n\tword = exponent.arr[3]\n\tfor ; curBit \u003c expBitLen \u0026\u0026 curBit \u003c 256; curBit++ {\n\t\tif word\u00261 == 1 {\n\t\t\tres.Mul(\u0026res, \u0026multiplier)\n\t\t}\n\t\tmultiplier.squared()\n\t\tword \u003e\u003e= 1\n\t}\n\treturn z.Set(\u0026res)\n}\n\nfunc (z *Uint) squared() {\n\tvar (\n\t\tres                    Uint\n\t\tcarry0, carry1, carry2 uint64\n\t\tres1, res2             uint64\n\t)\n\n\tcarry0, res.arr[0] = bits.Mul64(z.arr[0], z.arr[0])\n\tcarry0, res1 = umulHop(carry0, z.arr[0], z.arr[1])\n\tcarry0, res2 = umulHop(carry0, z.arr[0], z.arr[2])\n\n\tcarry1, res.arr[1] = umulHop(res1, z.arr[0], z.arr[1])\n\tcarry1, res2 = umulStep(res2, z.arr[1], z.arr[1], carry1)\n\n\tcarry2, res.arr[2] = umulHop(res2, z.arr[0], z.arr[2])\n\n\tres.arr[3] = 2*(z.arr[0]*z.arr[3]+z.arr[1]*z.arr[2]) + carry0 + carry1 + carry2\n\n\tz.Set(\u0026res)\n}\n\n// udivrem divides u by d and produces both quotient and remainder.\n// The quotient is stored in provided quot - len(u)-len(d)+1 words.\n// It loosely follows the Knuth's division algorithm (sometimes referenced as \"schoolbook\" division) using 64-bit words.\n// See Knuth, Volume 2, section 4.3.1, Algorithm D.\nfunc udivrem(quot, u []uint64, d *Uint) (rem Uint) {\n\tvar dLen int\n\tfor i := len(d.arr) - 1; i \u003e= 0; i-- {\n\t\tif d.arr[i] != 0 {\n\t\t\tdLen = i + 1\n\t\t\tbreak\n\t\t}\n\t}\n\n\tshift := uint(bits.LeadingZeros64(d.arr[dLen-1]))\n\n\tvar dnStorage Uint\n\tdn := dnStorage.arr[:dLen]\n\tfor i := dLen - 1; i \u003e 0; i-- {\n\t\tdn[i] = (d.arr[i] \u003c\u003c shift) | (d.arr[i-1] \u003e\u003e (64 - shift))\n\t}\n\tdn[0] = d.arr[0] \u003c\u003c shift\n\n\tvar uLen int\n\tfor i := len(u) - 1; i \u003e= 0; i-- {\n\t\tif u[i] != 0 {\n\t\t\tuLen = i + 1\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif uLen \u003c dLen {\n\t\tcopy(rem.arr[:], u)\n\t\treturn rem\n\t}\n\n\tvar unStorage [9]uint64\n\tun := unStorage[:uLen+1]\n\tun[uLen] = u[uLen-1] \u003e\u003e (64 - shift)\n\tfor i := uLen - 1; i \u003e 0; i-- {\n\t\tun[i] = (u[i] \u003c\u003c shift) | (u[i-1] \u003e\u003e (64 - shift))\n\t}\n\tun[0] = u[0] \u003c\u003c shift\n\n\t// TODO: Skip the highest word of numerator if not significant.\n\n\tif dLen == 1 {\n\t\tr := udivremBy1(quot, un, dn[0])\n\t\trem.SetUint64(r \u003e\u003e shift)\n\t\treturn rem\n\t}\n\n\tudivremKnuth(quot, un, dn)\n\n\tfor i := 0; i \u003c dLen-1; i++ {\n\t\trem.arr[i] = (un[i] \u003e\u003e shift) | (un[i+1] \u003c\u003c (64 - shift))\n\t}\n\trem.arr[dLen-1] = un[dLen-1] \u003e\u003e shift\n\n\treturn rem\n}\n\n// umul computes full 256 x 256 -\u003e 512 multiplication.\nfunc umul(x, y *Uint) [8]uint64 {\n\tvar (\n\t\tres                           [8]uint64\n\t\tcarry, carry4, carry5, carry6 uint64\n\t\tres1, res2, res3, res4, res5  uint64\n\t)\n\n\tcarry, res[0] = bits.Mul64(x.arr[0], y.arr[0])\n\tcarry, res1 = umulHop(carry, x.arr[1], y.arr[0])\n\tcarry, res2 = umulHop(carry, x.arr[2], y.arr[0])\n\tcarry4, res3 = umulHop(carry, x.arr[3], y.arr[0])\n\n\tcarry, res[1] = umulHop(res1, x.arr[0], y.arr[1])\n\tcarry, res2 = umulStep(res2, x.arr[1], y.arr[1], carry)\n\tcarry, res3 = umulStep(res3, x.arr[2], y.arr[1], carry)\n\tcarry5, res4 = umulStep(carry4, x.arr[3], y.arr[1], carry)\n\n\tcarry, res[2] = umulHop(res2, x.arr[0], y.arr[2])\n\tcarry, res3 = umulStep(res3, x.arr[1], y.arr[2], carry)\n\tcarry, res4 = umulStep(res4, x.arr[2], y.arr[2], carry)\n\tcarry6, res5 = umulStep(carry5, x.arr[3], y.arr[2], carry)\n\n\tcarry, res[3] = umulHop(res3, x.arr[0], y.arr[3])\n\tcarry, res[4] = umulStep(res4, x.arr[1], y.arr[3], carry)\n\tcarry, res[5] = umulStep(res5, x.arr[2], y.arr[3], carry)\n\tres[7], res[6] = umulStep(carry6, x.arr[3], y.arr[3], carry)\n\n\treturn res\n}\n\n// umulStep computes (hi * 2^64 + lo) = z + (x * y) + carry.\nfunc umulStep(z, x, y, carry uint64) (hi, lo uint64) {\n\thi, lo = bits.Mul64(x, y)\n\tlo, carry = bits.Add64(lo, carry, 0)\n\thi, _ = bits.Add64(hi, 0, carry)\n\tlo, carry = bits.Add64(lo, z, 0)\n\thi, _ = bits.Add64(hi, 0, carry)\n\treturn hi, lo\n}\n\n// umulHop computes (hi * 2^64 + lo) = z + (x * y)\nfunc umulHop(z, x, y uint64) (hi, lo uint64) {\n\thi, lo = bits.Mul64(x, y)\n\tlo, carry := bits.Add64(lo, z, 0)\n\thi, _ = bits.Add64(hi, 0, carry)\n\treturn hi, lo\n}\n\n// udivremBy1 divides u by single normalized word d and produces both quotient and remainder.\n// The quotient is stored in provided quot.\nfunc udivremBy1(quot, u []uint64, d uint64) (rem uint64) {\n\treciprocal := reciprocal2by1(d)\n\trem = u[len(u)-1] // Set the top word as remainder.\n\tfor j := len(u) - 2; j \u003e= 0; j-- {\n\t\tquot[j], rem = udivrem2by1(rem, u[j], d, reciprocal)\n\t}\n\treturn rem\n}\n\n// udivremKnuth implements the division of u by normalized multiple word d from the Knuth's division algorithm.\n// The quotient is stored in provided quot - len(u)-len(d) words.\n// Updates u to contain the remainder - len(d) words.\nfunc udivremKnuth(quot, u, d []uint64) {\n\tdh := d[len(d)-1]\n\tdl := d[len(d)-2]\n\treciprocal := reciprocal2by1(dh)\n\n\tfor j := len(u) - len(d) - 1; j \u003e= 0; j-- {\n\t\tu2 := u[j+len(d)]\n\t\tu1 := u[j+len(d)-1]\n\t\tu0 := u[j+len(d)-2]\n\n\t\tvar qhat, rhat uint64\n\t\tif u2 \u003e= dh { // Division overflows.\n\t\t\tqhat = ^uint64(0)\n\t\t\t// TODO: Add \"qhat one to big\" adjustment (not needed for correctness, but helps avoiding \"add back\" case).\n\t\t} else {\n\t\t\tqhat, rhat = udivrem2by1(u2, u1, dh, reciprocal)\n\t\t\tph, pl := bits.Mul64(qhat, dl)\n\t\t\tif ph \u003e rhat || (ph == rhat \u0026\u0026 pl \u003e u0) {\n\t\t\t\tqhat--\n\t\t\t\t// TODO: Add \"qhat one to big\" adjustment (not needed for correctness, but helps avoiding \"add back\" case).\n\t\t\t}\n\t\t}\n\n\t\t// Multiply and subtract.\n\t\tborrow := subMulTo(u[j:], d, qhat)\n\t\tu[j+len(d)] = u2 - borrow\n\t\tif u2 \u003c borrow { // Too much subtracted, add back.\n\t\t\tqhat--\n\t\t\tu[j+len(d)] += addTo(u[j:], d)\n\t\t}\n\n\t\tquot[j] = qhat // Store quotient digit.\n\t}\n}\n\n// isBitSet returns true if bit n-th is set, where n = 0 is LSB.\n// The n must be \u003c= 255.\nfunc (z *Uint) isBitSet(n uint) bool {\n\treturn (z.arr[n/64] \u0026 (1 \u003c\u003c (n % 64))) != 0\n}\n\n// addTo computes x += y.\n// Requires len(x) \u003e= len(y).\nfunc addTo(x, y []uint64) uint64 {\n\tvar carry uint64\n\tfor i := 0; i \u003c len(y); i++ {\n\t\tx[i], carry = bits.Add64(x[i], y[i], carry)\n\t}\n\treturn carry\n}\n\n// subMulTo computes x -= y * multiplier.\n// Requires len(x) \u003e= len(y).\nfunc subMulTo(x, y []uint64, multiplier uint64) uint64 {\n\tvar borrow uint64\n\tfor i := 0; i \u003c len(y); i++ {\n\t\ts, carry1 := bits.Sub64(x[i], borrow, 0)\n\t\tph, pl := bits.Mul64(y[i], multiplier)\n\t\tt, carry2 := bits.Sub64(s, pl, 0)\n\t\tx[i] = t\n\t\tborrow = ph + carry1 + carry2\n\t}\n\treturn borrow\n}\n\n// reciprocal2by1 computes \u003c^d, ^0\u003e / d.\nfunc reciprocal2by1(d uint64) uint64 {\n\treciprocal, _ := bits.Div64(^d, ^uint64(0), d)\n\treturn reciprocal\n}\n\n// udivrem2by1 divides \u003cuh, ul\u003e / d and produces both quotient and remainder.\n// It uses the provided d's reciprocal.\n// Implementation ported from https://github.com/chfast/intx and is based on\n// \"Improved division by invariant integers\", Algorithm 4.\nfunc udivrem2by1(uh, ul, d, reciprocal uint64) (quot, rem uint64) {\n\tqh, ql := bits.Mul64(reciprocal, uh)\n\tql, carry := bits.Add64(ql, ul, 0)\n\tqh, _ = bits.Add64(qh, uh, carry)\n\tqh++\n\n\tr := ul - qh*d\n\n\tif r \u003e ql {\n\t\tqh--\n\t\tr += d\n\t}\n\n\tif r \u003e= d {\n\t\tqh++\n\t\tr -= d\n\t}\n\n\treturn qh, r\n}\n"},{"name":"arithmetic_test.gno","body":"package uint256\n\nimport (\n\t\"testing\"\n)\n\ntype binOp2Test struct {\n\tx, y, want string\n}\n\nfunc TestAdd(t *testing.T) {\n\ttests := []binOp2Test{\n\t\t{\"0\", \"1\", \"1\"},\n\t\t{\"1\", \"0\", \"1\"},\n\t\t{\"1\", \"1\", \"2\"},\n\t\t{\"1\", \"3\", \"4\"},\n\t\t{\"10\", \"10\", \"20\"},\n\t\t{\"18446744073709551615\", \"18446744073709551615\", \"36893488147419103230\"}, // uint64 overflow\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\n\t\twant := MustFromDecimal(tt.want)\n\t\tgot := new(Uint).Add(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Add(%s, %s) = %v, want %v\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestAddOverflow(t *testing.T) {\n\ttests := []struct {\n\t\tx, y     string\n\t\twant     string\n\t\toverflow bool\n\t}{\n\t\t{\"0\", \"1\", \"1\", false},\n\t\t{\"1\", \"0\", \"1\", false},\n\t\t{\"1\", \"1\", \"2\", false},\n\t\t{\"10\", \"10\", \"20\", false},\n\t\t{\"18446744073709551615\", \"18446744073709551615\", \"36893488147419103230\", false},                    // uint64 overflow, but not Uint256 overflow\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"1\", \"0\", true}, // 2^256 - 1 + 1, should overflow\n\t\t{\"57896044618658097711785492504343953926634992332820282019728792003956564819967\", \"57896044618658097711785492504343953926634992332820282019728792003956564819968\", \"115792089237316195423570985008687907853269984665640564039457584007913129639935\", false}, // (2^255 - 1) + 2^255, no overflow\n\t\t{\"57896044618658097711785492504343953926634992332820282019728792003956564819967\", \"57896044618658097711785492504343953926634992332820282019728792003956564819969\", \"0\", true},                                                                               // (2^255 - 1) + (2^255 + 1), should overflow\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\t\twant, _ := FromDecimal(tt.want)\n\n\t\tgot, overflow := new(Uint).AddOverflow(x, y)\n\n\t\tif got.Cmp(want) != 0 || overflow != tt.overflow {\n\t\t\tt.Errorf(\"AddOverflow(%s, %s) = (%s, %v), want (%s, %v)\",\n\t\t\t\ttt.x, tt.y, got.String(), overflow, tt.want, tt.overflow)\n\t\t}\n\t}\n}\n\nfunc TestSub(t *testing.T) {\n\ttests := []binOp2Test{\n\t\t{\"1\", \"0\", \"1\"},\n\t\t{\"1\", \"1\", \"0\"},\n\t\t{\"10\", \"10\", \"0\"},\n\t\t{\"31337\", \"1337\", \"30000\"},\n\t\t{\"2\", \"3\", twoPow256Sub1}, // underflow\n\t}\n\n\tfor _, tc := range tests {\n\t\tx := MustFromDecimal(tc.x)\n\t\ty := MustFromDecimal(tc.y)\n\n\t\twant := MustFromDecimal(tc.want)\n\n\t\tgot := new(Uint).Sub(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\n\t\t\t\t\"Sub(%s, %s) = %v, want %v\",\n\t\t\t\ttc.x, tc.y, got.String(), want.String(),\n\t\t\t)\n\t\t}\n\t}\n}\n\nfunc TestSubOverflow(t *testing.T) {\n\ttests := []struct {\n\t\tx, y     string\n\t\twant     string\n\t\toverflow bool\n\t}{\n\t\t{\"1\", \"0\", \"1\", false},\n\t\t{\"1\", \"1\", \"0\", false},\n\t\t{\"10\", \"10\", \"0\", false},\n\t\t{\"31337\", \"1337\", \"30000\", false},\n\t\t{\"0\", \"1\", \"115792089237316195423570985008687907853269984665640564039457584007913129639935\", true},                                                                                                                                                         // 0 - 1, should underflow\n\t\t{\"57896044618658097711785492504343953926634992332820282019728792003956564819968\", \"1\", \"57896044618658097711785492504343953926634992332820282019728792003956564819967\", false},                                                                             // 2^255 - 1, no underflow\n\t\t{\"57896044618658097711785492504343953926634992332820282019728792003956564819968\", \"57896044618658097711785492504343953926634992332820282019728792003956564819969\", \"115792089237316195423570985008687907853269984665640564039457584007913129639935\", true}, // 2^255 - (2^255 + 1), should underflow\n\t}\n\n\tfor _, tc := range tests {\n\t\tx := MustFromDecimal(tc.x)\n\t\ty := MustFromDecimal(tc.y)\n\t\twant := MustFromDecimal(tc.want)\n\n\t\tgot, overflow := new(Uint).SubOverflow(x, y)\n\n\t\tif got.Cmp(want) != 0 || overflow != tc.overflow {\n\t\t\tt.Errorf(\n\t\t\t\t\"SubOverflow(%s, %s) = (%s, %v), want (%s, %v)\",\n\t\t\t\ttc.x, tc.y, got.String(), overflow, tc.want, tc.overflow,\n\t\t\t)\n\t\t}\n\t}\n}\n\nfunc TestMul(t *testing.T) {\n\ttests := []binOp2Test{\n\t\t{\"1\", \"0\", \"0\"},\n\t\t{\"1\", \"1\", \"1\"},\n\t\t{\"10\", \"10\", \"100\"},\n\t\t{\"18446744073709551615\", \"2\", \"36893488147419103230\"}, // uint64 overflow\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\t\twant := MustFromDecimal(tt.want)\n\t\tgot := new(Uint).Mul(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Mul(%s, %s) = %v, want %v\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestMulOverflow(t *testing.T) {\n\ttests := []struct {\n\t\tx        string\n\t\ty        string\n\t\twantZ    string\n\t\twantOver bool\n\t}{\n\t\t{\"0x1\", \"0x1\", \"0x1\", false},\n\t\t{\"0x0\", \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0x0\", false},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0x2\", \"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe\", true},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0x1\", true},\n\t\t{\"0x8000000000000000000000000000000000000000000000000000000000000000\", \"0x2\", \"0x0\", true},\n\t\t{\"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0x2\", \"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe\", false},\n\t\t{\"0x100000000000000000\", \"0x100000000000000000\", \"0x10000000000000000000000000000000000\", false},\n\t\t{\"0x10000000000000000000000000000000\", \"0x10000000000000000000000000000000\", \"0x100000000000000000000000000000000000000000000000000000000000000\", false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromHex(tt.x)\n\t\ty := MustFromHex(tt.y)\n\t\twantZ := MustFromHex(tt.wantZ)\n\n\t\tgotZ, gotOver := new(Uint).MulOverflow(x, y)\n\n\t\tif gotZ.Neq(wantZ) {\n\t\t\tt.Errorf(\n\t\t\t\t\"MulOverflow(%s, %s) = %s, want %s\",\n\t\t\t\ttt.x, tt.y, gotZ.String(), wantZ.String(),\n\t\t\t)\n\t\t}\n\t\tif gotOver != tt.wantOver {\n\t\t\tt.Errorf(\"MulOverflow(%s, %s) = %v, want %v\", tt.x, tt.y, gotOver, tt.wantOver)\n\t\t}\n\t}\n}\n\nfunc TestDiv(t *testing.T) {\n\ttests := []binOp2Test{\n\t\t{\"31337\", \"3\", \"10445\"},\n\t\t{\"31337\", \"0\", \"0\"},\n\t\t{\"0\", \"31337\", \"0\"},\n\t\t{\"1\", \"1\", \"1\"},\n\t\t{\"1000000000000000000\", \"3\", \"333333333333333333\"},\n\t\t{twoPow256Sub1, \"2\", \"57896044618658097711785492504343953926634992332820282019728792003956564819967\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\t\twant := MustFromDecimal(tt.want)\n\n\t\tgot := new(Uint).Div(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Div(%s, %s) = %v, want %v\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestMod(t *testing.T) {\n\ttests := []binOp2Test{\n\t\t{\"31337\", \"3\", \"2\"},\n\t\t{\"31337\", \"0\", \"0\"},\n\t\t{\"0\", \"31337\", \"0\"},\n\t\t{\"2\", \"31337\", \"2\"},\n\t\t{\"1\", \"1\", \"0\"},\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"2\", \"1\"}, // 2^256 - 1 mod 2\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"3\", \"0\"}, // 2^256 - 1 mod 3\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"57896044618658097711785492504343953926634992332820282019728792003956564819968\", \"57896044618658097711785492504343953926634992332820282019728792003956564819967\"}, // 2^256 - 1 mod 2^255\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\t\twant := MustFromDecimal(tt.want)\n\n\t\tgot := new(Uint).Mod(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Mod(%s, %s) = %v, want %v\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestMulMod(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\ty    string\n\t\tm    string\n\t\twant string\n\t}{\n\t\t{\"0x1\", \"0x1\", \"0x2\", \"0x1\"},\n\t\t{\"0x10\", \"0x10\", \"0x7\", \"0x4\"},\n\t\t{\"0x100\", \"0x100\", \"0x17\", \"0x9\"},\n\t\t{\"0x31337\", \"0x31337\", \"0x31338\", \"0x1\"},\n\t\t{\"0x0\", \"0x31337\", \"0x31338\", \"0x0\"},\n\t\t{\"0x31337\", \"0x0\", \"0x31338\", \"0x0\"},\n\t\t{\"0x2\", \"0x3\", \"0x5\", \"0x1\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0x0\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe\", \"0x1\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"0xffffffffffffffffffffffffffffffff\", \"0x0\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromHex(tt.x)\n\t\ty := MustFromHex(tt.y)\n\t\tm := MustFromHex(tt.m)\n\t\twant := MustFromHex(tt.want)\n\n\t\tgot := new(Uint).MulMod(x, y, m)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\n\t\t\t\t\"MulMod(%s, %s, %s) = %s, want %s\",\n\t\t\t\ttt.x, tt.y, tt.m, got.String(), want.String(),\n\t\t\t)\n\t\t}\n\t}\n}\n\nfunc TestDivMod(t *testing.T) {\n\ttests := []struct {\n\t\tx       string\n\t\ty       string\n\t\twantDiv string\n\t\twantMod string\n\t}{\n\t\t{\"1\", \"1\", \"1\", \"0\"},\n\t\t{\"10\", \"10\", \"1\", \"0\"},\n\t\t{\"100\", \"10\", \"10\", \"0\"},\n\t\t{\"31337\", \"3\", \"10445\", \"2\"},\n\t\t{\"31337\", \"0\", \"0\", \"0\"},\n\t\t{\"0\", \"31337\", \"0\", \"0\"},\n\t\t{\"2\", \"31337\", \"0\", \"2\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\t\twantDiv := MustFromDecimal(tt.wantDiv)\n\t\twantMod := MustFromDecimal(tt.wantMod)\n\n\t\tgotDiv := new(Uint)\n\t\tgotMod := new(Uint)\n\t\tgotDiv.DivMod(x, y, gotMod)\n\n\t\tfor i := range gotDiv.arr {\n\t\t\tif gotDiv.arr[i] != wantDiv.arr[i] {\n\t\t\t\tt.Errorf(\"DivMod(%s, %s) got Div %v, want Div %v\", tt.x, tt.y, gotDiv, wantDiv)\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tfor i := range gotMod.arr {\n\t\t\tif gotMod.arr[i] != wantMod.arr[i] {\n\t\t\t\tt.Errorf(\"DivMod(%s, %s) got Mod %v, want Mod %v\", tt.x, tt.y, gotMod, wantMod)\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc TestNeg(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant string\n\t}{\n\t\t{\"31337\", \"115792089237316195423570985008687907853269984665640564039457584007913129608599\"},\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129608599\", \"31337\"},\n\t\t{\"0\", \"0\"},\n\t\t{\"2\", \"115792089237316195423570985008687907853269984665640564039457584007913129639934\"},\n\t\t{\"1\", twoPow256Sub1},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\twant := MustFromDecimal(tt.want)\n\n\t\tgot := new(Uint).Neg(x)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Neg(%s) = %v, want %v\", tt.x, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestExp(t *testing.T) {\n\ttests := []binOp2Test{\n\t\t{\"31337\", \"3\", \"30773171189753\"},\n\t\t{\"31337\", \"0\", \"1\"},\n\t\t{\"0\", \"31337\", \"0\"},\n\t\t{\"1\", \"1\", \"1\"},\n\t\t{\"2\", \"3\", \"8\"},\n\t\t{\"2\", \"64\", \"18446744073709551616\"},\n\t\t{\"2\", \"128\", \"340282366920938463463374607431768211456\"},\n\t\t{\"2\", \"255\", \"57896044618658097711785492504343953926634992332820282019728792003956564819968\"},\n\t\t{\"2\", \"256\", \"0\"}, // overflow\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\t\twant := MustFromDecimal(tt.want)\n\n\t\tgot := new(Uint).Exp(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\n\t\t\t\t\"Exp(%s, %s) = %v, want %v\",\n\t\t\t\ttt.x, tt.y, got.String(), want.String(),\n\t\t\t)\n\t\t}\n\t}\n}\n\nfunc TestExp_LargeExponent(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tbase     string\n\t\texponent string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"2^129\",\n\t\t\tbase:     \"2\",\n\t\t\texponent: \"680564733841876926926749214863536422912\",\n\t\t\texpected: \"0\",\n\t\t},\n\t\t{\n\t\t\tname:     \"2^193\",\n\t\t\tbase:     \"2\",\n\t\t\texponent: \"12379400392853802746563808384000000000000000000\",\n\t\t\texpected: \"0\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tbase := MustFromDecimal(tt.base)\n\t\t\texponent := MustFromDecimal(tt.exponent)\n\t\t\texpected := MustFromDecimal(tt.expected)\n\n\t\t\tresult := new(Uint).Exp(base, exponent)\n\n\t\t\tif result.Neq(expected) {\n\t\t\t\tt.Errorf(\n\t\t\t\t\t\"Test %s failed. Expected %s, got %s\",\n\t\t\t\t\ttt.name, expected.String(), result.String(),\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"bits_table.gno","body":"// Copyright 2017 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\n// Code generated by go run make_tables.go. DO NOT EDIT.\n\npackage uint256\n\nconst ntz8tab = \"\" +\n\t\"\\x08\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x05\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x06\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x05\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x07\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x05\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x06\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x05\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\" +\n\t\"\\x04\\x00\\x01\\x00\\x02\\x00\\x01\\x00\\x03\\x00\\x01\\x00\\x02\\x00\\x01\\x00\"\n\nconst pop8tab = \"\" +\n\t\"\\x00\\x01\\x01\\x02\\x01\\x02\\x02\\x03\\x01\\x02\\x02\\x03\\x02\\x03\\x03\\x04\" +\n\t\"\\x01\\x02\\x02\\x03\\x02\\x03\\x03\\x04\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\" +\n\t\"\\x01\\x02\\x02\\x03\\x02\\x03\\x03\\x04\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\" +\n\t\"\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\" +\n\t\"\\x01\\x02\\x02\\x03\\x02\\x03\\x03\\x04\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\" +\n\t\"\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\" +\n\t\"\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\" +\n\t\"\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\\x04\\x05\\x05\\x06\\x05\\x06\\x06\\x07\" +\n\t\"\\x01\\x02\\x02\\x03\\x02\\x03\\x03\\x04\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\" +\n\t\"\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\" +\n\t\"\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\" +\n\t\"\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\\x04\\x05\\x05\\x06\\x05\\x06\\x06\\x07\" +\n\t\"\\x02\\x03\\x03\\x04\\x03\\x04\\x04\\x05\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\" +\n\t\"\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\\x04\\x05\\x05\\x06\\x05\\x06\\x06\\x07\" +\n\t\"\\x03\\x04\\x04\\x05\\x04\\x05\\x05\\x06\\x04\\x05\\x05\\x06\\x05\\x06\\x06\\x07\" +\n\t\"\\x04\\x05\\x05\\x06\\x05\\x06\\x06\\x07\\x05\\x06\\x06\\x07\\x06\\x07\\x07\\x08\"\n\nconst rev8tab = \"\" +\n\t\"\\x00\\x80\\x40\\xc0\\x20\\xa0\\x60\\xe0\\x10\\x90\\x50\\xd0\\x30\\xb0\\x70\\xf0\" +\n\t\"\\x08\\x88\\x48\\xc8\\x28\\xa8\\x68\\xe8\\x18\\x98\\x58\\xd8\\x38\\xb8\\x78\\xf8\" +\n\t\"\\x04\\x84\\x44\\xc4\\x24\\xa4\\x64\\xe4\\x14\\x94\\x54\\xd4\\x34\\xb4\\x74\\xf4\" +\n\t\"\\x0c\\x8c\\x4c\\xcc\\x2c\\xac\\x6c\\xec\\x1c\\x9c\\x5c\\xdc\\x3c\\xbc\\x7c\\xfc\" +\n\t\"\\x02\\x82\\x42\\xc2\\x22\\xa2\\x62\\xe2\\x12\\x92\\x52\\xd2\\x32\\xb2\\x72\\xf2\" +\n\t\"\\x0a\\x8a\\x4a\\xca\\x2a\\xaa\\x6a\\xea\\x1a\\x9a\\x5a\\xda\\x3a\\xba\\x7a\\xfa\" +\n\t\"\\x06\\x86\\x46\\xc6\\x26\\xa6\\x66\\xe6\\x16\\x96\\x56\\xd6\\x36\\xb6\\x76\\xf6\" +\n\t\"\\x0e\\x8e\\x4e\\xce\\x2e\\xae\\x6e\\xee\\x1e\\x9e\\x5e\\xde\\x3e\\xbe\\x7e\\xfe\" +\n\t\"\\x01\\x81\\x41\\xc1\\x21\\xa1\\x61\\xe1\\x11\\x91\\x51\\xd1\\x31\\xb1\\x71\\xf1\" +\n\t\"\\x09\\x89\\x49\\xc9\\x29\\xa9\\x69\\xe9\\x19\\x99\\x59\\xd9\\x39\\xb9\\x79\\xf9\" +\n\t\"\\x05\\x85\\x45\\xc5\\x25\\xa5\\x65\\xe5\\x15\\x95\\x55\\xd5\\x35\\xb5\\x75\\xf5\" +\n\t\"\\x0d\\x8d\\x4d\\xcd\\x2d\\xad\\x6d\\xed\\x1d\\x9d\\x5d\\xdd\\x3d\\xbd\\x7d\\xfd\" +\n\t\"\\x03\\x83\\x43\\xc3\\x23\\xa3\\x63\\xe3\\x13\\x93\\x53\\xd3\\x33\\xb3\\x73\\xf3\" +\n\t\"\\x0b\\x8b\\x4b\\xcb\\x2b\\xab\\x6b\\xeb\\x1b\\x9b\\x5b\\xdb\\x3b\\xbb\\x7b\\xfb\" +\n\t\"\\x07\\x87\\x47\\xc7\\x27\\xa7\\x67\\xe7\\x17\\x97\\x57\\xd7\\x37\\xb7\\x77\\xf7\" +\n\t\"\\x0f\\x8f\\x4f\\xcf\\x2f\\xaf\\x6f\\xef\\x1f\\x9f\\x5f\\xdf\\x3f\\xbf\\x7f\\xff\"\n\nconst len8tab = \"\" +\n\t\"\\x00\\x01\\x02\\x02\\x03\\x03\\x03\\x03\\x04\\x04\\x04\\x04\\x04\\x04\\x04\\x04\" +\n\t\"\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\\x05\" +\n\t\"\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\" +\n\t\"\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\\x06\" +\n\t\"\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\" +\n\t\"\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\" +\n\t\"\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\" +\n\t\"\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\\x07\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\" +\n\t\"\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08\"\n"},{"name":"bitwise.gno","body":"// bitwise contains bitwise operations for Uint instances.\n// This file includes functions to perform bitwise AND, OR, XOR, and NOT operations, as well as bit shifting.\n// These operations are crucial for manipulating individual bits within a 256-bit unsigned integer.\npackage uint256\n\n// Or sets z = x | y and returns z.\nfunc (z *Uint) Or(x, y *Uint) *Uint {\n\tz.arr[0] = x.arr[0] | y.arr[0]\n\tz.arr[1] = x.arr[1] | y.arr[1]\n\tz.arr[2] = x.arr[2] | y.arr[2]\n\tz.arr[3] = x.arr[3] | y.arr[3]\n\treturn z\n}\n\n// And sets z = x \u0026 y and returns z.\nfunc (z *Uint) And(x, y *Uint) *Uint {\n\tz.arr[0] = x.arr[0] \u0026 y.arr[0]\n\tz.arr[1] = x.arr[1] \u0026 y.arr[1]\n\tz.arr[2] = x.arr[2] \u0026 y.arr[2]\n\tz.arr[3] = x.arr[3] \u0026 y.arr[3]\n\treturn z\n}\n\n// Not sets z = ^x and returns z.\nfunc (z *Uint) Not(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = ^x.arr[3], ^x.arr[2], ^x.arr[1], ^x.arr[0]\n\treturn z\n}\n\n// AndNot sets z = x \u0026^ y and returns z.\nfunc (z *Uint) AndNot(x, y *Uint) *Uint {\n\tz.arr[0] = x.arr[0] \u0026^ y.arr[0]\n\tz.arr[1] = x.arr[1] \u0026^ y.arr[1]\n\tz.arr[2] = x.arr[2] \u0026^ y.arr[2]\n\tz.arr[3] = x.arr[3] \u0026^ y.arr[3]\n\treturn z\n}\n\n// Xor sets z = x ^ y and returns z.\nfunc (z *Uint) Xor(x, y *Uint) *Uint {\n\tz.arr[0] = x.arr[0] ^ y.arr[0]\n\tz.arr[1] = x.arr[1] ^ y.arr[1]\n\tz.arr[2] = x.arr[2] ^ y.arr[2]\n\tz.arr[3] = x.arr[3] ^ y.arr[3]\n\treturn z\n}\n\n// Lsh sets z = x \u003c\u003c n and returns z.\nfunc (z *Uint) Lsh(x *Uint, n uint) *Uint {\n\t// n % 64 == 0\n\tif n\u00260x3f == 0 {\n\t\tswitch n {\n\t\tcase 0:\n\t\t\treturn z.Set(x)\n\t\tcase 64:\n\t\t\treturn z.lsh64(x)\n\t\tcase 128:\n\t\t\treturn z.lsh128(x)\n\t\tcase 192:\n\t\t\treturn z.lsh192(x)\n\t\tdefault:\n\t\t\treturn z.Clear()\n\t\t}\n\t}\n\tvar a, b uint64\n\t// Big swaps first\n\tswitch {\n\tcase n \u003e 192:\n\t\tif n \u003e 256 {\n\t\t\treturn z.Clear()\n\t\t}\n\t\tz.lsh192(x)\n\t\tn -= 192\n\t\tgoto sh192\n\tcase n \u003e 128:\n\t\tz.lsh128(x)\n\t\tn -= 128\n\t\tgoto sh128\n\tcase n \u003e 64:\n\t\tz.lsh64(x)\n\t\tn -= 64\n\t\tgoto sh64\n\tdefault:\n\t\tz.Set(x)\n\t}\n\n\t// remaining shifts\n\ta = z.arr[0] \u003e\u003e (64 - n)\n\tz.arr[0] = z.arr[0] \u003c\u003c n\n\nsh64:\n\tb = z.arr[1] \u003e\u003e (64 - n)\n\tz.arr[1] = (z.arr[1] \u003c\u003c n) | a\n\nsh128:\n\ta = z.arr[2] \u003e\u003e (64 - n)\n\tz.arr[2] = (z.arr[2] \u003c\u003c n) | b\n\nsh192:\n\tz.arr[3] = (z.arr[3] \u003c\u003c n) | a\n\n\treturn z\n}\n\n// Rsh sets z = x \u003e\u003e n and returns z.\nfunc (z *Uint) Rsh(x *Uint, n uint) *Uint {\n\t// n % 64 == 0\n\tif n\u00260x3f == 0 {\n\t\tswitch n {\n\t\tcase 0:\n\t\t\treturn z.Set(x)\n\t\tcase 64:\n\t\t\treturn z.rsh64(x)\n\t\tcase 128:\n\t\t\treturn z.rsh128(x)\n\t\tcase 192:\n\t\t\treturn z.rsh192(x)\n\t\tdefault:\n\t\t\treturn z.Clear()\n\t\t}\n\t}\n\tvar a, b uint64\n\t// Big swaps first\n\tswitch {\n\tcase n \u003e 192:\n\t\tif n \u003e 256 {\n\t\t\treturn z.Clear()\n\t\t}\n\t\tz.rsh192(x)\n\t\tn -= 192\n\t\tgoto sh192\n\tcase n \u003e 128:\n\t\tz.rsh128(x)\n\t\tn -= 128\n\t\tgoto sh128\n\tcase n \u003e 64:\n\t\tz.rsh64(x)\n\t\tn -= 64\n\t\tgoto sh64\n\tdefault:\n\t\tz.Set(x)\n\t}\n\n\t// remaining shifts\n\ta = z.arr[3] \u003c\u003c (64 - n)\n\tz.arr[3] = z.arr[3] \u003e\u003e n\n\nsh64:\n\tb = z.arr[2] \u003c\u003c (64 - n)\n\tz.arr[2] = (z.arr[2] \u003e\u003e n) | a\n\nsh128:\n\ta = z.arr[1] \u003c\u003c (64 - n)\n\tz.arr[1] = (z.arr[1] \u003e\u003e n) | b\n\nsh192:\n\tz.arr[0] = (z.arr[0] \u003e\u003e n) | a\n\n\treturn z\n}\n\n// SRsh (Signed/Arithmetic right shift)\n// considers z to be a signed integer, during right-shift\n// and sets z = x \u003e\u003e n and returns z.\nfunc (z *Uint) SRsh(x *Uint, n uint) *Uint {\n\t// If the MSB is 0, SRsh is same as Rsh.\n\tif !x.isBitSet(255) {\n\t\treturn z.Rsh(x, n)\n\t}\n\tif n%64 == 0 {\n\t\tswitch n {\n\t\tcase 0:\n\t\t\treturn z.Set(x)\n\t\tcase 64:\n\t\t\treturn z.srsh64(x)\n\t\tcase 128:\n\t\t\treturn z.srsh128(x)\n\t\tcase 192:\n\t\t\treturn z.srsh192(x)\n\t\tdefault:\n\t\t\treturn z.SetAllOne()\n\t\t}\n\t}\n\tvar a uint64 = MaxUint64 \u003c\u003c (64 - n%64)\n\t// Big swaps first\n\tswitch {\n\tcase n \u003e 192:\n\t\tif n \u003e 256 {\n\t\t\treturn z.SetAllOne()\n\t\t}\n\t\tz.srsh192(x)\n\t\tn -= 192\n\t\tgoto sh192\n\tcase n \u003e 128:\n\t\tz.srsh128(x)\n\t\tn -= 128\n\t\tgoto sh128\n\tcase n \u003e 64:\n\t\tz.srsh64(x)\n\t\tn -= 64\n\t\tgoto sh64\n\tdefault:\n\t\tz.Set(x)\n\t}\n\n\t// remaining shifts\n\tz.arr[3], a = (z.arr[3]\u003e\u003en)|a, z.arr[3]\u003c\u003c(64-n)\n\nsh64:\n\tz.arr[2], a = (z.arr[2]\u003e\u003en)|a, z.arr[2]\u003c\u003c(64-n)\n\nsh128:\n\tz.arr[1], a = (z.arr[1]\u003e\u003en)|a, z.arr[1]\u003c\u003c(64-n)\n\nsh192:\n\tz.arr[0] = (z.arr[0] \u003e\u003e n) | a\n\n\treturn z\n}\n\nfunc (z *Uint) lsh64(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = x.arr[2], x.arr[1], x.arr[0], 0\n\treturn z\n}\n\nfunc (z *Uint) lsh128(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = x.arr[1], x.arr[0], 0, 0\n\treturn z\n}\n\nfunc (z *Uint) lsh192(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = x.arr[0], 0, 0, 0\n\treturn z\n}\n\nfunc (z *Uint) rsh64(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, x.arr[3], x.arr[2], x.arr[1]\n\treturn z\n}\n\nfunc (z *Uint) rsh128(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, 0, x.arr[3], x.arr[2]\n\treturn z\n}\n\nfunc (z *Uint) rsh192(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, 0, 0, x.arr[3]\n\treturn z\n}\n\nfunc (z *Uint) srsh64(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = MaxUint64, x.arr[3], x.arr[2], x.arr[1]\n\treturn z\n}\n\nfunc (z *Uint) srsh128(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = MaxUint64, MaxUint64, x.arr[3], x.arr[2]\n\treturn z\n}\n\nfunc (z *Uint) srsh192(x *Uint) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = MaxUint64, MaxUint64, MaxUint64, x.arr[3]\n\treturn z\n}\n"},{"name":"bitwise_test.gno","body":"package uint256\n\nimport \"testing\"\n\ntype logicOpTest struct {\n\tname string\n\tx    Uint\n\ty    Uint\n\twant Uint\n}\n\nfunc TestOr(t *testing.T) {\n\ttests := []logicOpTest{\n\t\t{\n\t\t\tname: \"all zeros\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"one operand all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tres := new(Uint).Or(\u0026tt.x, \u0026tt.y)\n\t\t\tif *res != tt.want {\n\t\t\t\tt.Errorf(\n\t\t\t\t\t\"Or(%s, %s) = %s, want %s\",\n\t\t\t\t\ttt.x.String(), tt.y.String(), res.String(), (tt.want).String(),\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestAnd(t *testing.T) {\n\ttests := []logicOpTest{\n\t\t{\n\t\t\tname: \"all zeros\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed 2\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed 3\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"one operand zero\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"one operand all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000}},\n\t\t\twant: Uint{arr: [4]uint64{0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000}},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tres := new(Uint).And(\u0026tt.x, \u0026tt.y)\n\t\t\tif *res != tt.want {\n\t\t\t\tt.Errorf(\n\t\t\t\t\t\"And(%s, %s) = %s, want %s\",\n\t\t\t\t\ttt.x.String(), tt.y.String(), res.String(), (tt.want).String(),\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNot(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tx    Uint\n\t\twant Uint\n\t}{\n\t\t{\n\t\t\tname: \"all zeros\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, ^uint64(0), ^uint64(0)}},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tres := new(Uint).Not(\u0026tt.x)\n\t\t\tif *res != tt.want {\n\t\t\t\tt.Errorf(\n\t\t\t\t\t\"Not(%s) = %s, want %s\",\n\t\t\t\t\ttt.x.String(), res.String(), (tt.want).String(),\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestAndNot(t *testing.T) {\n\ttests := []logicOpTest{\n\t\t{\n\t\t\tname: \"all zeros\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed 2\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed 3\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"one operand zero\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"one operand all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000}},\n\t\t\twant: Uint{arr: [4]uint64{0xAAAAAAAAAAAAAAAA, 0x5555555555555555, 0x0000000000000000, ^uint64(0)}},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tres := new(Uint).AndNot(\u0026tt.x, \u0026tt.y)\n\t\t\tif *res != tt.want {\n\t\t\t\tt.Errorf(\n\t\t\t\t\t\"AndNot(%s, %s) = %s, want %s\",\n\t\t\t\t\ttt.x.String(), tt.y.String(), res.String(), (tt.want).String(),\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestXor(t *testing.T) {\n\ttests := []logicOpTest{\n\t\t{\n\t\t\tname: \"all zeros\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed 2\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"mixed 3\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0, 0, ^uint64(0), ^uint64(0)}},\n\t\t\twant: Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t},\n\t\t{\n\t\t\tname: \"one operand zero\",\n\t\t\tx:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\ty:    Uint{arr: [4]uint64{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}},\n\t\t\twant: Uint{arr: [4]uint64{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}},\n\t\t},\n\t\t{\n\t\t\tname: \"one operand all ones\",\n\t\t\tx:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\ty:    Uint{arr: [4]uint64{0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000}},\n\t\t\twant: Uint{arr: [4]uint64{0xAAAAAAAAAAAAAAAA, 0x5555555555555555, 0x0000000000000000, ^uint64(0)}},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tres := new(Uint).Xor(\u0026tt.x, \u0026tt.y)\n\t\t\tif *res != tt.want {\n\t\t\t\tt.Errorf(\n\t\t\t\t\t\"Xor(%s, %s) = %s, want %s\",\n\t\t\t\t\ttt.x.String(), tt.y.String(), res.String(), (tt.want).String(),\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestLsh(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\ty    uint\n\t\twant string\n\t}{\n\t\t{\"0\", 0, \"0\"},\n\t\t{\"0\", 1, \"0\"},\n\t\t{\"0\", 64, \"0\"},\n\t\t{\"1\", 0, \"1\"},\n\t\t{\"1\", 1, \"2\"},\n\t\t{\"1\", 64, \"18446744073709551616\"},\n\t\t{\"1\", 128, \"340282366920938463463374607431768211456\"},\n\t\t{\"1\", 192, \"6277101735386680763835789423207666416102355444464034512896\"},\n\t\t{\"1\", 255, \"57896044618658097711785492504343953926634992332820282019728792003956564819968\"},\n\t\t{\"1\", 256, \"0\"},\n\t\t{\"31337\", 0, \"31337\"},\n\t\t{\"31337\", 1, \"62674\"},\n\t\t{\"31337\", 64, \"578065619037836218990592\"},\n\t\t{\"31337\", 128, \"10663428532201448629551770073089320442396672\"},\n\t\t{\"31337\", 192, \"196705537081812415096322133155058642481399512563169449530621952\"},\n\t\t{\"31337\", 193, \"393411074163624830192644266310117284962799025126338899061243904\"},\n\t\t{\"31337\", 255, \"57896044618658097711785492504343953926634992332820282019728792003956564819968\"},\n\t\t{\"31337\", 256, \"0\"},\n\t\t// 64 \u003c n \u003c 128\n\t\t{\"1\", 65, \"36893488147419103232\"},\n\t\t{\"31337\", 100, \"39724366859352024754702188346867712\"},\n\n\t\t// 128 \u003c n \u003c 192\n\t\t{\"1\", 129, \"680564733841876926926749214863536422912\"},\n\t\t{\"31337\", 150, \"44725660946326664792723507424638829088826130956288\"},\n\n\t\t// 192 \u003c n \u003c 256\n\t\t{\"1\", 193, \"12554203470773361527671578846415332832204710888928069025792\"},\n\t\t{\"31337\", 200, \"50356617492943978264658466087695012475238275216171379079839219712\"},\n\n\t\t// n \u003e 256\n\t\t{\"1\", 257, \"0\"},\n\t\t{\"31337\", 300, \"0\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\twant := MustFromDecimal(tt.want)\n\n\t\tgot := new(Uint).Lsh(x, tt.y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Lsh(%s, %d) = %s, want %s\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestRsh(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\ty    uint\n\t\twant string\n\t}{\n\t\t{\"0\", 0, \"0\"},\n\t\t{\"0\", 1, \"0\"},\n\t\t{\"0\", 64, \"0\"},\n\t\t{\"1\", 0, \"1\"},\n\t\t{\"1\", 1, \"0\"},\n\t\t{\"1\", 64, \"0\"},\n\t\t{\"1\", 128, \"0\"},\n\t\t{\"1\", 192, \"0\"},\n\t\t{\"1\", 255, \"0\"},\n\t\t{\"57896044618658097711785492504343953926634992332820282019728792003956564819968\", 255, \"1\"},\n\t\t{\"6277101735386680763835789423207666416102355444464034512896\", 192, \"1\"},\n\t\t{\"340282366920938463463374607431768211456\", 128, \"1\"},\n\t\t{\"18446744073709551616\", 64, \"1\"},\n\t\t{\"393411074163624830192644266310117284962799025126338899061243904\", 193, \"31337\"},\n\t\t{\"196705537081812415096322133155058642481399512563169449530621952\", 192, \"31337\"},\n\t\t{\"10663428532201448629551770073089320442396672\", 128, \"31337\"},\n\t\t{\"578065619037836218990592\", 64, \"31337\"},\n\t\t{twoPow256Sub1, 256, \"0\"},\n\t\t// outliers\n\t\t{\"340282366920938463463374607431768211455\", 129, \"0\"},\n\t\t{\"18446744073709551615\", 65, \"0\"},\n\t\t{twoPow256Sub1, 1, \"57896044618658097711785492504343953926634992332820282019728792003956564819967\"},\n\n\t\t// n \u003e 256\n\t\t{\"1\", 257, \"0\"},\n\t\t{\"31337\", 300, \"0\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\n\t\twant := MustFromDecimal(tt.want)\n\t\tgot := new(Uint).Rsh(x, tt.y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Rsh(%s, %d) = %s, want %s\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestSRsh(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\ty    uint\n\t\twant string\n\t}{\n\t\t// Positive numbers (behaves like Rsh)\n\t\t{\"0x0\", 0, \"0x0\"},\n\t\t{\"0x0\", 1, \"0x0\"},\n\t\t{\"0x1\", 0, \"0x1\"},\n\t\t{\"0x1\", 1, \"0x0\"},\n\t\t{\"0x31337\", 0, \"0x31337\"},\n\t\t{\"0x31337\", 4, \"0x3133\"},\n\t\t{\"0x31337\", 8, \"0x313\"},\n\t\t{\"0x31337\", 16, \"0x3\"},\n\t\t{\"0x10000000000000000\", 64, \"0x1\"}, // 2^64 \u003e\u003e 64\n\n\t\t// // Numbers with MSB set (negative numbers in two's complement)\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 0, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 1, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 4, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 64, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 128, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 192, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 255, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\n\t\t// Large positive number close to max value\n\t\t{\"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 1, \"0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 2, \"0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 64, \"0x7fffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 128, \"0x7fffffffffffffffffffffffffffffff\"},\n\t\t{\"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 192, \"0x7fffffffffffffff\"},\n\t\t{\"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 255, \"0x0\"},\n\n\t\t// Specific cases\n\t\t{\"0x8000000000000000000000000000000000000000000000000000000000000000\", 1, \"0xc000000000000000000000000000000000000000000000000000000000000000\"},\n\t\t{\"0x8000000000000000000000000000000000000000000000000000000000000001\", 1, \"0xc000000000000000000000000000000000000000000000000000000000000000\"},\n\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 65, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 127, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 129, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 193, \"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"},\n\n\t\t// n \u003e 256\n\t\t{\"0x1\", 257, \"0x0\"},\n\t\t{\"0x31337\", 300, \"0x0\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromHex(tt.x)\n\t\twant := MustFromHex(tt.want)\n\n\t\tgot := new(Uint).SRsh(x, tt.y)\n\n\t\tif !got.Eq(want) {\n\t\t\tt.Errorf(\"SRsh(%s, %d) = %s, want %s\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n"},{"name":"cmp.gno","body":"// cmp (or, comparisons) includes methods for comparing Uint instances.\n// These comparison functions cover a range of operations including equality checks, less than/greater than\n// evaluations, and specialized comparisons such as signed greater than. These are fundamental for logical\n// decision making based on Uint values.\npackage uint256\n\nimport (\n\t\"math/bits\"\n)\n\n// Cmp compares z and x and returns:\n//\n//\t-1 if z \u003c  x\n//\t 0 if z == x\n//\t+1 if z \u003e  x\nfunc (z *Uint) Cmp(x *Uint) (r int) {\n\t// z \u003c x \u003c=\u003e z - x \u003c 0 i.e. when subtraction overflows.\n\td0, carry := bits.Sub64(z.arr[0], x.arr[0], 0)\n\td1, carry := bits.Sub64(z.arr[1], x.arr[1], carry)\n\td2, carry := bits.Sub64(z.arr[2], x.arr[2], carry)\n\td3, carry := bits.Sub64(z.arr[3], x.arr[3], carry)\n\tif carry == 1 {\n\t\treturn -1\n\t}\n\tif d0|d1|d2|d3 == 0 {\n\t\treturn 0\n\t}\n\treturn 1\n}\n\n// IsZero returns true if z == 0\nfunc (z *Uint) IsZero() bool {\n\treturn (z.arr[0] | z.arr[1] | z.arr[2] | z.arr[3]) == 0\n}\n\n// Sign returns:\n//\n//\t-1 if z \u003c  0\n//\t 0 if z == 0\n//\t+1 if z \u003e  0\n//\n// Where z is interpreted as a two's complement signed number\nfunc (z *Uint) Sign() int {\n\tif z.IsZero() {\n\t\treturn 0\n\t}\n\tif z.arr[3] \u003c 0x8000000000000000 {\n\t\treturn 1\n\t}\n\treturn -1\n}\n\n// LtUint64 returns true if z is smaller than n\nfunc (z *Uint) LtUint64(n uint64) bool {\n\treturn z.arr[0] \u003c n \u0026\u0026 (z.arr[1]|z.arr[2]|z.arr[3]) == 0\n}\n\n// GtUint64 returns true if z is larger than n\nfunc (z *Uint) GtUint64(n uint64) bool {\n\treturn z.arr[0] \u003e n || (z.arr[1]|z.arr[2]|z.arr[3]) != 0\n}\n\n// Lt returns true if z \u003c x\nfunc (z *Uint) Lt(x *Uint) bool {\n\t// z \u003c x \u003c=\u003e z - x \u003c 0 i.e. when subtraction overflows.\n\t_, carry := bits.Sub64(z.arr[0], x.arr[0], 0)\n\t_, carry = bits.Sub64(z.arr[1], x.arr[1], carry)\n\t_, carry = bits.Sub64(z.arr[2], x.arr[2], carry)\n\t_, carry = bits.Sub64(z.arr[3], x.arr[3], carry)\n\n\treturn carry != 0\n}\n\n// Gt returns true if z \u003e x\nfunc (z *Uint) Gt(x *Uint) bool {\n\treturn x.Lt(z)\n}\n\n// Lte returns true if z \u003c= x\nfunc (z *Uint) Lte(x *Uint) bool {\n\tcond1 := z.Lt(x)\n\tcond2 := z.Eq(x)\n\n\tif cond1 || cond2 {\n\t\treturn true\n\t}\n\treturn false\n}\n\n// Gte returns true if z \u003e= x\nfunc (z *Uint) Gte(x *Uint) bool {\n\tcond1 := z.Gt(x)\n\tcond2 := z.Eq(x)\n\n\tif cond1 || cond2 {\n\t\treturn true\n\t}\n\treturn false\n}\n\n// Eq returns true if z == x\nfunc (z *Uint) Eq(x *Uint) bool {\n\treturn (z.arr[0] == x.arr[0]) \u0026\u0026 (z.arr[1] == x.arr[1]) \u0026\u0026 (z.arr[2] == x.arr[2]) \u0026\u0026 (z.arr[3] == x.arr[3])\n}\n\n// Neq returns true if z != x\nfunc (z *Uint) Neq(x *Uint) bool {\n\treturn !z.Eq(x)\n}\n\n// Sgt interprets z and x as signed integers, and returns\n// true if z \u003e x\nfunc (z *Uint) Sgt(x *Uint) bool {\n\tzSign := z.Sign()\n\txSign := x.Sign()\n\n\tswitch {\n\tcase zSign \u003e= 0 \u0026\u0026 xSign \u003c 0:\n\t\treturn true\n\tcase zSign \u003c 0 \u0026\u0026 xSign \u003e= 0:\n\t\treturn false\n\tdefault:\n\t\treturn z.Gt(x)\n\t}\n}\n"},{"name":"cmp_test.gno","body":"package uint256\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestSign(t *testing.T) {\n\ttests := []struct {\n\t\tinput    *Uint\n\t\texpected int\n\t}{\n\t\t{\n\t\t\tinput:    NewUint(0),\n\t\t\texpected: 0,\n\t\t},\n\t\t{\n\t\t\tinput:    NewUint(1),\n\t\t\texpected: 1,\n\t\t},\n\t\t{\n\t\t\tinput:    NewUint(0x7fffffffffffffff),\n\t\t\texpected: 1,\n\t\t},\n\t\t{\n\t\t\tinput:    NewUint(0x8000000000000000),\n\t\t\texpected: 1,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.input.String(), func(t *testing.T) {\n\t\t\tresult := tt.input.Sign()\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"Sign() = %d; want %d\", result, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestCmp(t *testing.T) {\n\ttests := []struct {\n\t\tx, y string\n\t\twant int\n\t}{\n\t\t{\"0\", \"0\", 0},\n\t\t{\"0\", \"1\", -1},\n\t\t{\"1\", \"0\", 1},\n\t\t{\"1\", \"1\", 0},\n\t\t{\"10\", \"10\", 0},\n\t\t{\"10\", \"11\", -1},\n\t\t{\"11\", \"10\", 1},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx := MustFromDecimal(tc.x)\n\t\ty := MustFromDecimal(tc.y)\n\n\t\tgot := x.Cmp(y)\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"Cmp(%s, %s) = %v, want %v\", tc.x, tc.y, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestIsZero(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant bool\n\t}{\n\t\t{\"0\", true},\n\t\t{\"1\", false},\n\t\t{\"10\", false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\n\t\tgot := x.IsZero()\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"IsZero(%s) = %v, want %v\", tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestLtUint64(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\ty    uint64\n\t\twant bool\n\t}{\n\t\t{\"0\", 1, true},\n\t\t{\"1\", 0, false},\n\t\t{\"10\", 10, false},\n\t\t{\"0xffffffffffffffff\", 0, false},\n\t\t{\"0x10000000000000000\", 10000000000000000, false},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx := parseTestString(t, tc.x)\n\n\t\tgot := x.LtUint64(tc.y)\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"LtUint64(%s, %d) = %v, want %v\", tc.x, tc.y, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestUint_GtUint64(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tz    string\n\t\tn    uint64\n\t\twant bool\n\t}{\n\t\t{\n\t\t\tname: \"z \u003e n\",\n\t\t\tz:    \"1\",\n\t\t\tn:    0,\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname: \"z \u003c n\",\n\t\t\tz:    \"18446744073709551615\",\n\t\t\tn:    0xFFFFFFFFFFFFFFFF,\n\t\t\twant: false,\n\t\t},\n\t\t{\n\t\t\tname: \"z == n\",\n\t\t\tz:    \"18446744073709551615\",\n\t\t\tn:    0xFFFFFFFFFFFFFFFF,\n\t\t\twant: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tz := MustFromDecimal(tt.z)\n\n\t\t\tif got := z.GtUint64(tt.n); got != tt.want {\n\t\t\t\tt.Errorf(\"Uint.GtUint64() = %v, want %v\", got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSGT(t *testing.T) {\n\tx := MustFromHex(\"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe\")\n\ty := MustFromHex(\"0x0\")\n\tactual := x.Sgt(y)\n\tif actual {\n\t\tt.Fatalf(\"Expected %v false\", actual)\n\t}\n\n\tx = MustFromHex(\"0x0\")\n\ty = MustFromHex(\"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe\")\n\tactual = x.Sgt(y)\n\tif !actual {\n\t\tt.Fatalf(\"Expected %v true\", actual)\n\t}\n}\n\nfunc TestEq(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\ty    string\n\t\twant bool\n\t}{\n\t\t{\"0xffffffffffffffff\", \"18446744073709551615\", true},\n\t\t{\"0x10000000000000000\", \"18446744073709551616\", true},\n\t\t{\"0\", \"0\", true},\n\t\t{twoPow256Sub1, twoPow256Sub1, true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := parseTestString(t, tt.x)\n\n\t\ty, err := FromDecimal(tt.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.Eq(y)\n\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"Eq(%s, %s) = %v, want %v\", tt.x, tt.y, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestUint_Lte(t *testing.T) {\n\ttests := []struct {\n\t\tz, x string\n\t\twant bool\n\t}{\n\t\t{\"10\", \"20\", true},\n\t\t{\"20\", \"10\", false},\n\t\t{\"10\", \"10\", true},\n\t\t{\"0\", \"0\", true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz, err := FromDecimal(tt.z)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\t\tx, err := FromDecimal(tt.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\t\tif got := z.Lte(x); got != tt.want {\n\t\t\tt.Errorf(\"Uint.Lte(%v, %v) = %v, want %v\", tt.z, tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestUint_Gte(t *testing.T) {\n\ttests := []struct {\n\t\tz, x string\n\t\twant bool\n\t}{\n\t\t{\"20\", \"10\", true},\n\t\t{\"10\", \"20\", false},\n\t\t{\"10\", \"10\", true},\n\t\t{\"0\", \"0\", true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := parseTestString(t, tt.z)\n\t\tx := parseTestString(t, tt.x)\n\n\t\tif got := z.Gte(x); got != tt.want {\n\t\t\tt.Errorf(\"Uint.Gte(%v, %v) = %v, want %v\", tt.z, tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc parseTestString(_ *testing.T, s string) *Uint {\n\tvar x *Uint\n\n\tif strings.HasPrefix(s, \"0x\") {\n\t\tx = MustFromHex(s)\n\t} else {\n\t\tx = MustFromDecimal(s)\n\t}\n\n\treturn x\n}\n"},{"name":"conversion.gno","body":"// conversions contains methods for converting Uint instances to other types and vice versa.\n// This includes conversions to and from basic types such as uint64 and int32, as well as string representations\n// and byte slices. Additionally, it covers marshaling and unmarshaling for JSON and other text formats.\npackage uint256\n\nimport (\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"strconv\"\n\t\"strings\"\n)\n\n// Uint64 returns the lower 64-bits of z\nfunc (z *Uint) Uint64() uint64 {\n\treturn z.arr[0]\n}\n\n// Uint64WithOverflow returns the lower 64-bits of z and bool whether overflow occurred\nfunc (z *Uint) Uint64WithOverflow() (uint64, bool) {\n\treturn z.arr[0], (z.arr[1] | z.arr[2] | z.arr[3]) != 0\n}\n\n// SetUint64 sets z to the value x\nfunc (z *Uint) SetUint64(x uint64) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, 0, 0, x\n\treturn z\n}\n\n// IsUint64 reports whether z can be represented as a uint64.\nfunc (z *Uint) IsUint64() bool {\n\treturn (z.arr[1] | z.arr[2] | z.arr[3]) == 0\n}\n\n// Dec returns the decimal representation of z.\nfunc (z *Uint) Dec() string {\n\tif z.IsZero() {\n\t\treturn \"0\"\n\t}\n\tif z.IsUint64() {\n\t\treturn strconv.FormatUint(z.Uint64(), 10)\n\t}\n\n\t// The max uint64 value being 18446744073709551615, the largest\n\t// power-of-ten below that is 10000000000000000000.\n\t// When we do a DivMod using that number, the remainder that we\n\t// get back is the lower part of the output.\n\t//\n\t// The ascii-output of remainder will never exceed 19 bytes (since it will be\n\t// below 10000000000000000000).\n\t//\n\t// Algorithm example using 100 as divisor\n\t//\n\t// 12345 % 100 = 45   (rem)\n\t// 12345 / 100 = 123  (quo)\n\t// -\u003e output '45', continue iterate on 123\n\tvar (\n\t\t// out is 98 bytes long: 78 (max size of a string without leading zeroes,\n\t\t// plus slack so we can copy 19 bytes every iteration).\n\t\t// We init it with zeroes, because when strconv appends the ascii representations,\n\t\t// it will omit leading zeroes.\n\t\tout     = []byte(\"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\")\n\t\tdivisor = NewUint(10000000000000000000) // 20 digits\n\t\ty       = new(Uint).Set(z)              // copy to avoid modifying z\n\t\tpos     = len(out)                      // position to write to\n\t\tbuf     = make([]byte, 0, 19)           // buffer to write uint64:s to\n\t)\n\tfor {\n\t\t// Obtain Q and R for divisor\n\t\tvar quot Uint\n\t\trem := udivrem(quot.arr[:], y.arr[:], divisor)\n\t\ty.Set(\u0026quot) // Set Q for next loop\n\t\t// Convert the R to ascii representation\n\t\tbuf = strconv.AppendUint(buf[:0], rem.Uint64(), 10)\n\t\t// Copy in the ascii digits\n\t\tcopy(out[pos-len(buf):], buf)\n\t\tif y.IsZero() {\n\t\t\tbreak\n\t\t}\n\t\t// Move 19 digits left\n\t\tpos -= 19\n\t}\n\t// skip leading zeroes by only using the 'used size' of buf\n\treturn string(out[pos-len(buf):])\n}\n\nfunc (z *Uint) Scan(src any) error {\n\tif src == nil {\n\t\tz.Clear()\n\t\treturn nil\n\t}\n\n\tswitch src := src.(type) {\n\tcase string:\n\t\treturn z.scanScientificFromString(src)\n\tcase []byte:\n\t\treturn z.scanScientificFromString(string(src))\n\t}\n\treturn errors.New(\"default // unsupported type: can't convert to uint256.Uint\")\n}\n\nfunc (z *Uint) scanScientificFromString(src string) error {\n\tif len(src) == 0 {\n\t\tz.Clear()\n\t\treturn nil\n\t}\n\n\tidx := strings.IndexByte(src, 'e')\n\tif idx == -1 {\n\t\treturn z.SetFromDecimal(src)\n\t}\n\tif err := z.SetFromDecimal(src[:idx]); err != nil {\n\t\treturn err\n\t}\n\tif src[(idx+1):] == \"0\" {\n\t\treturn nil\n\t}\n\texp := new(Uint)\n\tif err := exp.SetFromDecimal(src[(idx + 1):]); err != nil {\n\t\treturn err\n\t}\n\tif exp.GtUint64(77) { // 10**78 is larger than 2**256\n\t\treturn ErrBig256Range\n\t}\n\texp.Exp(NewUint(10), exp)\n\tif _, overflow := z.MulOverflow(z, exp); overflow {\n\t\treturn ErrBig256Range\n\t}\n\treturn nil\n}\n\n// ToString returns the decimal string representation of z. It returns an empty string if z is nil.\n// OBS: doesn't exist from holiman's uint256\nfunc (z *Uint) String() string {\n\tif z == nil {\n\t\treturn \"\"\n\t}\n\n\treturn z.Dec()\n}\n\n// MarshalJSON implements json.Marshaler.\n// MarshalJSON marshals using the 'decimal string' representation. This is _not_ compatible\n// with big.Uint: big.Uint marshals into JSON 'native' numeric format.\n//\n// The JSON  native format is, on some platforms, (e.g. javascript), limited to 53-bit large\n// integer space. Thus, U256 uses string-format, which is not compatible with\n// big.int (big.Uint refuses to unmarshal a string representation).\nfunc (z *Uint) MarshalJSON() ([]byte, error) {\n\treturn []byte(`\"` + z.Dec() + `\"`), nil\n}\n\n// UnmarshalJSON implements json.Unmarshaler. UnmarshalJSON accepts either\n// - Quoted string: either hexadecimal OR decimal\n// - Not quoted string: only decimal\nfunc (z *Uint) UnmarshalJSON(input []byte) error {\n\tif len(input) \u003c 2 || input[0] != '\"' || input[len(input)-1] != '\"' {\n\t\t// if not quoted, it must be decimal\n\t\treturn z.fromDecimal(string(input))\n\t}\n\treturn z.UnmarshalText(input[1 : len(input)-1])\n}\n\n// MarshalText implements encoding.TextMarshaler\n// MarshalText marshals using the decimal representation (compatible with big.Uint)\nfunc (z *Uint) MarshalText() ([]byte, error) {\n\treturn []byte(z.Dec()), nil\n}\n\n// UnmarshalText implements encoding.TextUnmarshaler. This method\n// can unmarshal either hexadecimal or decimal.\n// - For hexadecimal, the input _must_ be prefixed with 0x or 0X\nfunc (z *Uint) UnmarshalText(input []byte) error {\n\tif len(input) \u003e= 2 \u0026\u0026 input[0] == '0' \u0026\u0026 (input[1] == 'x' || input[1] == 'X') {\n\t\treturn z.fromHex(string(input))\n\t}\n\treturn z.fromDecimal(string(input))\n}\n\n// SetBytes interprets buf as the bytes of a big-endian unsigned\n// integer, sets z to that value, and returns z.\n// If buf is larger than 32 bytes, the last 32 bytes is used.\nfunc (z *Uint) SetBytes(buf []byte) *Uint {\n\tswitch l := len(buf); l {\n\tcase 0:\n\t\tz.Clear()\n\tcase 1:\n\t\tz.SetBytes1(buf)\n\tcase 2:\n\t\tz.SetBytes2(buf)\n\tcase 3:\n\t\tz.SetBytes3(buf)\n\tcase 4:\n\t\tz.SetBytes4(buf)\n\tcase 5:\n\t\tz.SetBytes5(buf)\n\tcase 6:\n\t\tz.SetBytes6(buf)\n\tcase 7:\n\t\tz.SetBytes7(buf)\n\tcase 8:\n\t\tz.SetBytes8(buf)\n\tcase 9:\n\t\tz.SetBytes9(buf)\n\tcase 10:\n\t\tz.SetBytes10(buf)\n\tcase 11:\n\t\tz.SetBytes11(buf)\n\tcase 12:\n\t\tz.SetBytes12(buf)\n\tcase 13:\n\t\tz.SetBytes13(buf)\n\tcase 14:\n\t\tz.SetBytes14(buf)\n\tcase 15:\n\t\tz.SetBytes15(buf)\n\tcase 16:\n\t\tz.SetBytes16(buf)\n\tcase 17:\n\t\tz.SetBytes17(buf)\n\tcase 18:\n\t\tz.SetBytes18(buf)\n\tcase 19:\n\t\tz.SetBytes19(buf)\n\tcase 20:\n\t\tz.SetBytes20(buf)\n\tcase 21:\n\t\tz.SetBytes21(buf)\n\tcase 22:\n\t\tz.SetBytes22(buf)\n\tcase 23:\n\t\tz.SetBytes23(buf)\n\tcase 24:\n\t\tz.SetBytes24(buf)\n\tcase 25:\n\t\tz.SetBytes25(buf)\n\tcase 26:\n\t\tz.SetBytes26(buf)\n\tcase 27:\n\t\tz.SetBytes27(buf)\n\tcase 28:\n\t\tz.SetBytes28(buf)\n\tcase 29:\n\t\tz.SetBytes29(buf)\n\tcase 30:\n\t\tz.SetBytes30(buf)\n\tcase 31:\n\t\tz.SetBytes31(buf)\n\tdefault:\n\t\tz.SetBytes32(buf[l-32:])\n\t}\n\treturn z\n}\n\n// SetBytes1 is identical to SetBytes(in[:1]), but panics is input is too short\nfunc (z *Uint) SetBytes1(in []byte) *Uint {\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = uint64(in[0])\n\treturn z\n}\n\n// SetBytes2 is identical to SetBytes(in[:2]), but panics is input is too short\nfunc (z *Uint) SetBytes2(in []byte) *Uint {\n\t_ = in[1] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = uint64(binary.BigEndian.Uint16(in[0:2]))\n\treturn z\n}\n\n// SetBytes3 is identical to SetBytes(in[:3]), but panics is input is too short\nfunc (z *Uint) SetBytes3(in []byte) *Uint {\n\t_ = in[2] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])\u003c\u003c16\n\treturn z\n}\n\n// SetBytes4 is identical to SetBytes(in[:4]), but panics is input is too short\nfunc (z *Uint) SetBytes4(in []byte) *Uint {\n\t_ = in[3] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = uint64(binary.BigEndian.Uint32(in[0:4]))\n\treturn z\n}\n\n// SetBytes5 is identical to SetBytes(in[:5]), but panics is input is too short\nfunc (z *Uint) SetBytes5(in []byte) *Uint {\n\t_ = in[4] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = bigEndianUint40(in[0:5])\n\treturn z\n}\n\n// SetBytes6 is identical to SetBytes(in[:6]), but panics is input is too short\nfunc (z *Uint) SetBytes6(in []byte) *Uint {\n\t_ = in[5] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = bigEndianUint48(in[0:6])\n\treturn z\n}\n\n// SetBytes7 is identical to SetBytes(in[:7]), but panics is input is too short\nfunc (z *Uint) SetBytes7(in []byte) *Uint {\n\t_ = in[6] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = bigEndianUint56(in[0:7])\n\treturn z\n}\n\n// SetBytes8 is identical to SetBytes(in[:8]), but panics is input is too short\nfunc (z *Uint) SetBytes8(in []byte) *Uint {\n\t_ = in[7] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\tz.arr[0] = binary.BigEndian.Uint64(in[0:8])\n\treturn z\n}\n\n// SetBytes9 is identical to SetBytes(in[:9]), but panics is input is too short\nfunc (z *Uint) SetBytes9(in []byte) *Uint {\n\t_ = in[8] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = uint64(in[0])\n\tz.arr[0] = binary.BigEndian.Uint64(in[1:9])\n\treturn z\n}\n\n// SetBytes10 is identical to SetBytes(in[:10]), but panics is input is too short\nfunc (z *Uint) SetBytes10(in []byte) *Uint {\n\t_ = in[9] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = uint64(binary.BigEndian.Uint16(in[0:2]))\n\tz.arr[0] = binary.BigEndian.Uint64(in[2:10])\n\treturn z\n}\n\n// SetBytes11 is identical to SetBytes(in[:11]), but panics is input is too short\nfunc (z *Uint) SetBytes11(in []byte) *Uint {\n\t_ = in[10] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])\u003c\u003c16\n\tz.arr[0] = binary.BigEndian.Uint64(in[3:11])\n\treturn z\n}\n\n// SetBytes12 is identical to SetBytes(in[:12]), but panics is input is too short\nfunc (z *Uint) SetBytes12(in []byte) *Uint {\n\t_ = in[11] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = uint64(binary.BigEndian.Uint32(in[0:4]))\n\tz.arr[0] = binary.BigEndian.Uint64(in[4:12])\n\treturn z\n}\n\n// SetBytes13 is identical to SetBytes(in[:13]), but panics is input is too short\nfunc (z *Uint) SetBytes13(in []byte) *Uint {\n\t_ = in[12] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = bigEndianUint40(in[0:5])\n\tz.arr[0] = binary.BigEndian.Uint64(in[5:13])\n\treturn z\n}\n\n// SetBytes14 is identical to SetBytes(in[:14]), but panics is input is too short\nfunc (z *Uint) SetBytes14(in []byte) *Uint {\n\t_ = in[13] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = bigEndianUint48(in[0:6])\n\tz.arr[0] = binary.BigEndian.Uint64(in[6:14])\n\treturn z\n}\n\n// SetBytes15 is identical to SetBytes(in[:15]), but panics is input is too short\nfunc (z *Uint) SetBytes15(in []byte) *Uint {\n\t_ = in[14] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = bigEndianUint56(in[0:7])\n\tz.arr[0] = binary.BigEndian.Uint64(in[7:15])\n\treturn z\n}\n\n// SetBytes16 is identical to SetBytes(in[:16]), but panics is input is too short\nfunc (z *Uint) SetBytes16(in []byte) *Uint {\n\t_ = in[15] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3], z.arr[2] = 0, 0\n\tz.arr[1] = binary.BigEndian.Uint64(in[0:8])\n\tz.arr[0] = binary.BigEndian.Uint64(in[8:16])\n\treturn z\n}\n\n// SetBytes17 is identical to SetBytes(in[:17]), but panics is input is too short\nfunc (z *Uint) SetBytes17(in []byte) *Uint {\n\t_ = in[16] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = uint64(in[0])\n\tz.arr[1] = binary.BigEndian.Uint64(in[1:9])\n\tz.arr[0] = binary.BigEndian.Uint64(in[9:17])\n\treturn z\n}\n\n// SetBytes18 is identical to SetBytes(in[:18]), but panics is input is too short\nfunc (z *Uint) SetBytes18(in []byte) *Uint {\n\t_ = in[17] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = uint64(binary.BigEndian.Uint16(in[0:2]))\n\tz.arr[1] = binary.BigEndian.Uint64(in[2:10])\n\tz.arr[0] = binary.BigEndian.Uint64(in[10:18])\n\treturn z\n}\n\n// SetBytes19 is identical to SetBytes(in[:19]), but panics is input is too short\nfunc (z *Uint) SetBytes19(in []byte) *Uint {\n\t_ = in[18] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])\u003c\u003c16\n\tz.arr[1] = binary.BigEndian.Uint64(in[3:11])\n\tz.arr[0] = binary.BigEndian.Uint64(in[11:19])\n\treturn z\n}\n\n// SetBytes20 is identical to SetBytes(in[:20]), but panics is input is too short\nfunc (z *Uint) SetBytes20(in []byte) *Uint {\n\t_ = in[19] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = uint64(binary.BigEndian.Uint32(in[0:4]))\n\tz.arr[1] = binary.BigEndian.Uint64(in[4:12])\n\tz.arr[0] = binary.BigEndian.Uint64(in[12:20])\n\treturn z\n}\n\n// SetBytes21 is identical to SetBytes(in[:21]), but panics is input is too short\nfunc (z *Uint) SetBytes21(in []byte) *Uint {\n\t_ = in[20] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = bigEndianUint40(in[0:5])\n\tz.arr[1] = binary.BigEndian.Uint64(in[5:13])\n\tz.arr[0] = binary.BigEndian.Uint64(in[13:21])\n\treturn z\n}\n\n// SetBytes22 is identical to SetBytes(in[:22]), but panics is input is too short\nfunc (z *Uint) SetBytes22(in []byte) *Uint {\n\t_ = in[21] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = bigEndianUint48(in[0:6])\n\tz.arr[1] = binary.BigEndian.Uint64(in[6:14])\n\tz.arr[0] = binary.BigEndian.Uint64(in[14:22])\n\treturn z\n}\n\n// SetBytes23 is identical to SetBytes(in[:23]), but panics is input is too short\nfunc (z *Uint) SetBytes23(in []byte) *Uint {\n\t_ = in[22] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = bigEndianUint56(in[0:7])\n\tz.arr[1] = binary.BigEndian.Uint64(in[7:15])\n\tz.arr[0] = binary.BigEndian.Uint64(in[15:23])\n\treturn z\n}\n\n// SetBytes24 is identical to SetBytes(in[:24]), but panics is input is too short\nfunc (z *Uint) SetBytes24(in []byte) *Uint {\n\t_ = in[23] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = 0\n\tz.arr[2] = binary.BigEndian.Uint64(in[0:8])\n\tz.arr[1] = binary.BigEndian.Uint64(in[8:16])\n\tz.arr[0] = binary.BigEndian.Uint64(in[16:24])\n\treturn z\n}\n\n// SetBytes25 is identical to SetBytes(in[:25]), but panics is input is too short\nfunc (z *Uint) SetBytes25(in []byte) *Uint {\n\t_ = in[24] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = uint64(in[0])\n\tz.arr[2] = binary.BigEndian.Uint64(in[1:9])\n\tz.arr[1] = binary.BigEndian.Uint64(in[9:17])\n\tz.arr[0] = binary.BigEndian.Uint64(in[17:25])\n\treturn z\n}\n\n// SetBytes26 is identical to SetBytes(in[:26]), but panics is input is too short\nfunc (z *Uint) SetBytes26(in []byte) *Uint {\n\t_ = in[25] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = uint64(binary.BigEndian.Uint16(in[0:2]))\n\tz.arr[2] = binary.BigEndian.Uint64(in[2:10])\n\tz.arr[1] = binary.BigEndian.Uint64(in[10:18])\n\tz.arr[0] = binary.BigEndian.Uint64(in[18:26])\n\treturn z\n}\n\n// SetBytes27 is identical to SetBytes(in[:27]), but panics is input is too short\nfunc (z *Uint) SetBytes27(in []byte) *Uint {\n\t_ = in[26] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])\u003c\u003c16\n\tz.arr[2] = binary.BigEndian.Uint64(in[3:11])\n\tz.arr[1] = binary.BigEndian.Uint64(in[11:19])\n\tz.arr[0] = binary.BigEndian.Uint64(in[19:27])\n\treturn z\n}\n\n// SetBytes28 is identical to SetBytes(in[:28]), but panics is input is too short\nfunc (z *Uint) SetBytes28(in []byte) *Uint {\n\t_ = in[27] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = uint64(binary.BigEndian.Uint32(in[0:4]))\n\tz.arr[2] = binary.BigEndian.Uint64(in[4:12])\n\tz.arr[1] = binary.BigEndian.Uint64(in[12:20])\n\tz.arr[0] = binary.BigEndian.Uint64(in[20:28])\n\treturn z\n}\n\n// SetBytes29 is identical to SetBytes(in[:29]), but panics is input is too short\nfunc (z *Uint) SetBytes29(in []byte) *Uint {\n\t_ = in[23] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = bigEndianUint40(in[0:5])\n\tz.arr[2] = binary.BigEndian.Uint64(in[5:13])\n\tz.arr[1] = binary.BigEndian.Uint64(in[13:21])\n\tz.arr[0] = binary.BigEndian.Uint64(in[21:29])\n\treturn z\n}\n\n// SetBytes30 is identical to SetBytes(in[:30]), but panics is input is too short\nfunc (z *Uint) SetBytes30(in []byte) *Uint {\n\t_ = in[29] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = bigEndianUint48(in[0:6])\n\tz.arr[2] = binary.BigEndian.Uint64(in[6:14])\n\tz.arr[1] = binary.BigEndian.Uint64(in[14:22])\n\tz.arr[0] = binary.BigEndian.Uint64(in[22:30])\n\treturn z\n}\n\n// SetBytes31 is identical to SetBytes(in[:31]), but panics is input is too short\nfunc (z *Uint) SetBytes31(in []byte) *Uint {\n\t_ = in[30] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = bigEndianUint56(in[0:7])\n\tz.arr[2] = binary.BigEndian.Uint64(in[7:15])\n\tz.arr[1] = binary.BigEndian.Uint64(in[15:23])\n\tz.arr[0] = binary.BigEndian.Uint64(in[23:31])\n\treturn z\n}\n\n// SetBytes32 sets z to the value of the big-endian 256-bit unsigned integer in.\nfunc (z *Uint) SetBytes32(in []byte) *Uint {\n\t_ = in[31] // bounds check hint to compiler; see golang.org/issue/14808\n\tz.arr[3] = binary.BigEndian.Uint64(in[0:8])\n\tz.arr[2] = binary.BigEndian.Uint64(in[8:16])\n\tz.arr[1] = binary.BigEndian.Uint64(in[16:24])\n\tz.arr[0] = binary.BigEndian.Uint64(in[24:32])\n\treturn z\n}\n\n// Utility methods that are \"missing\" among the bigEndian.UintXX methods.\n\n// bigEndianUint40 returns the uint64 value represented by the 5 bytes in big-endian order.\nfunc bigEndianUint40(b []byte) uint64 {\n\t_ = b[4] // bounds check hint to compiler; see golang.org/issue/14808\n\treturn uint64(b[4]) | uint64(b[3])\u003c\u003c8 | uint64(b[2])\u003c\u003c16 | uint64(b[1])\u003c\u003c24 |\n\t\tuint64(b[0])\u003c\u003c32\n}\n\n// bigEndianUint56 returns the uint64 value represented by the 7 bytes in big-endian order.\nfunc bigEndianUint56(b []byte) uint64 {\n\t_ = b[6] // bounds check hint to compiler; see golang.org/issue/14808\n\treturn uint64(b[6]) | uint64(b[5])\u003c\u003c8 | uint64(b[4])\u003c\u003c16 | uint64(b[3])\u003c\u003c24 |\n\t\tuint64(b[2])\u003c\u003c32 | uint64(b[1])\u003c\u003c40 | uint64(b[0])\u003c\u003c48\n}\n\n// bigEndianUint48 returns the uint64 value represented by the 6 bytes in big-endian order.\nfunc bigEndianUint48(b []byte) uint64 {\n\t_ = b[5] // bounds check hint to compiler; see golang.org/issue/14808\n\treturn uint64(b[5]) | uint64(b[4])\u003c\u003c8 | uint64(b[3])\u003c\u003c16 | uint64(b[2])\u003c\u003c24 |\n\t\tuint64(b[1])\u003c\u003c32 | uint64(b[0])\u003c\u003c40\n}\n"},{"name":"conversion_test.gno","body":"package uint256\n\nimport \"testing\"\n\nfunc TestIsUint64(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant bool\n\t}{\n\t\t{\"0x0\", true},\n\t\t{\"0x1\", true},\n\t\t{\"0x10\", true},\n\t\t{\"0xffffffffffffffff\", true},\n\t\t{\"0x10000000000000000\", false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromHex(tt.x)\n\t\tgot := x.IsUint64()\n\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"IsUint64(%s) = %v, want %v\", tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestDec(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tz    Uint\n\t\twant string\n\t}{\n\t\t{\n\t\t\tname: \"zero\",\n\t\t\tz:    Uint{arr: [4]uint64{0, 0, 0, 0}},\n\t\t\twant: \"0\",\n\t\t},\n\t\t{\n\t\t\tname: \"less than 20 digits\",\n\t\t\tz:    Uint{arr: [4]uint64{1234567890, 0, 0, 0}},\n\t\t\twant: \"1234567890\",\n\t\t},\n\t\t{\n\t\t\tname: \"max possible value\",\n\t\t\tz:    Uint{arr: [4]uint64{^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0)}},\n\t\t\twant: twoPow256Sub1,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := tt.z.Dec()\n\t\t\tif result != tt.want {\n\t\t\t\tt.Errorf(\"Dec(%v) = %s, want %s\", tt.z, result, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestUint_Scan(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tinput   any\n\t\twant    *Uint\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname:  \"nil\",\n\t\t\tinput: nil,\n\t\t\twant:  NewUint(0),\n\t\t},\n\t\t{\n\t\t\tname:  \"valid scientific notation\",\n\t\t\tinput: \"1e4\",\n\t\t\twant:  NewUint(10000),\n\t\t},\n\t\t{\n\t\t\tname:  \"valid decimal string\",\n\t\t\tinput: \"12345\",\n\t\t\twant:  NewUint(12345),\n\t\t},\n\t\t{\n\t\t\tname:  \"valid byte slice\",\n\t\t\tinput: []byte(\"12345\"),\n\t\t\twant:  NewUint(12345),\n\t\t},\n\t\t{\n\t\t\tname:    \"invalid string\",\n\t\t\tinput:   \"invalid\",\n\t\t\twantErr: true,\n\t\t},\n\t\t{\n\t\t\tname:    \"out of range\",\n\t\t\tinput:   \"115792089237316195423570985008687907853269984665640564039457584007913129639936\", // 2^256\n\t\t\twantErr: true,\n\t\t},\n\t\t{\n\t\t\tname:    \"unsupported type\",\n\t\t\tinput:   123,\n\t\t\twantErr: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tz := new(Uint)\n\t\t\terr := z.Scan(tt.input)\n\n\t\t\tif tt.wantErr {\n\t\t\t\tif err == nil {\n\t\t\t\t\tt.Errorf(\"Scan() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif err != nil {\n\t\t\t\t\tt.Errorf(\"Scan() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\t}\n\t\t\t\tif !z.Eq(tt.want) {\n\t\t\t\t\tt.Errorf(\"Scan() = %v, want %v\", z, tt.want)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSetBytes(t *testing.T) {\n\ttests := []struct {\n\t\tinput    []byte\n\t\texpected string\n\t}{\n\t\t{[]byte{}, \"0\"},\n\t\t{[]byte{0x01}, \"1\"},\n\t\t{[]byte{0x12, 0x34}, \"4660\"},\n\t\t{[]byte{0x12, 0x34, 0x56}, \"1193046\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78}, \"305419896\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a}, \"78187493530\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}, \"20015998343868\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde}, \"5124095576030430\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}, \"1311768467463790320\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12}, \"335812727670730321938\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34}, \"85968058283706962416180\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56}, \"22007822920628982378542166\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78}, \"5634002667681019488906794616\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a}, \"1442304682926340989160139421850\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}, \"369229998829143293224995691993788\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde}, \"94522879700260683065598897150409950\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}, \"24197857203266734864793317670504947440\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12}, \"6194651444036284125387089323649266544658\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34}, \"1585830769673288736099094866854212235432500\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56}, \"405972677036361916441368285914678332270720086\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78}, \"103929005321308650608990281194157653061304342136\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a}, \"26605825362255014555901511985704359183693911586970\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}, \"6811091292737283726310787068340315951025641366264508\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde}, \"1743639370940744633935561489495120883462564189763714270\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}, \"446371678960830626287503741310750946166416432579510853360\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12}, \"114271149813972640329600957775552242218602606740354778460178\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34}, \"29253414352376995924377845190541374007962267325530823285805620\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56}, \"7488874074208510956640728368778591746038340435335890761166238806\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78}, \"1917151762997378804900026462407319486985815151445988034858557134456\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a}, \"490790851327328974054406774376273788668368678770172936923790626420890\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}, \"125642457939796217357928134240326089899102381765164271852490400363748028\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde}, \"32164469232587831643629602365523479014170209731882053594237542493119495390\"},\n\t\t{[]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}, \"8234104123542484900769178205574010627627573691361805720124810878238590820080\"},\n\t\t// over 32 bytes (last 32 bytes are used)\n\t\t{append([]byte{0xff}, []byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}...), \"8234104123542484900769178205574010627627573691361805720124810878238590820080\"},\n\t}\n\n\tfor _, test := range tests {\n\t\tz := new(Uint)\n\t\tz.SetBytes(test.input)\n\t\texpected := MustFromDecimal(test.expected)\n\t\tif z.Cmp(expected) != 0 {\n\t\t\tt.Errorf(\"SetBytes(%x) = %s, expected %s\", test.input, z.String(), test.expected)\n\t\t}\n\t}\n}\n"},{"name":"error.gno","body":"package uint256\n\nimport (\n\t\"errors\"\n)\n\nvar (\n\tErrEmptyString      = errors.New(\"empty hex string\")\n\tErrSyntax           = errors.New(\"invalid hex string\")\n\tErrRange            = errors.New(\"number out of range\")\n\tErrMissingPrefix    = errors.New(\"hex string without 0x prefix\")\n\tErrEmptyNumber      = errors.New(\"hex string \\\"0x\\\"\")\n\tErrLeadingZero      = errors.New(\"hex number with leading zero digits\")\n\tErrBig256Range      = errors.New(\"hex number \u003e 256 bits\")\n\tErrBadBufferLength  = errors.New(\"bad ssz buffer length\")\n\tErrBadEncodedLength = errors.New(\"bad ssz encoded length\")\n\tErrInvalidBase      = errors.New(\"invalid base\")\n\tErrInvalidBitSize   = errors.New(\"invalid bit size\")\n)\n\ntype u256Error struct {\n\tfn    string // function name\n\tinput string\n\terr   error\n}\n\nfunc (e *u256Error) Error() string {\n\treturn e.fn + \": \" + e.input + \": \" + e.err.Error()\n}\n\nfunc (e *u256Error) Unwrap() error {\n\treturn e.err\n}\n\nfunc errEmptyString(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrEmptyString}\n}\n\nfunc errSyntax(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrSyntax}\n}\n\nfunc errMissingPrefix(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrMissingPrefix}\n}\n\nfunc errEmptyNumber(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrEmptyNumber}\n}\n\nfunc errLeadingZero(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrLeadingZero}\n}\n\nfunc errRange(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrRange}\n}\n\nfunc errBig256Range(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrBig256Range}\n}\n\nfunc errBadBufferLength(fn, input string) error {\n\treturn \u0026u256Error{fn: fn, input: input, err: ErrBadBufferLength}\n}\n\nfunc errInvalidBase(fn string, base int) error {\n\treturn \u0026u256Error{fn: fn, input: string(base), err: ErrInvalidBase}\n}\n\nfunc errInvalidBitSize(fn string, bitSize int) error {\n\treturn \u0026u256Error{fn: fn, input: string(bitSize), err: ErrInvalidBitSize}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/onbloc/uint256\"\ngno = \"0.9\"\n"},{"name":"mod.gno","body":"package uint256\n\nimport (\n\t\"math/bits\"\n)\n\n// Some utility functions\n\n// Reciprocal computes a 320-bit value representing 1/m\n//\n// Notes:\n// - specialized for m.arr[3] != 0, hence limited to 2^192 \u003c= m \u003c 2^256\n// - returns zero if m.arr[3] == 0\n// - starts with a 32-bit division, refines with newton-raphson iterations\nfunc Reciprocal(m *Uint) (mu [5]uint64) {\n\tif m.arr[3] == 0 {\n\t\treturn mu\n\t}\n\n\ts := bits.LeadingZeros64(m.arr[3]) // Replace with leadingZeros(m) for general case\n\tp := 255 - s                       // floor(log_2(m)), m\u003e0\n\n\t// 0 or a power of 2?\n\n\t// Check if at least one bit is set in m.arr[2], m.arr[1] or m.arr[0],\n\t// or at least two bits in m.arr[3]\n\n\tif m.arr[0]|m.arr[1]|m.arr[2]|(m.arr[3]\u0026(m.arr[3]-1)) == 0 {\n\n\t\tmu[4] = ^uint64(0) \u003e\u003e uint(p\u002663)\n\t\tmu[3] = ^uint64(0)\n\t\tmu[2] = ^uint64(0)\n\t\tmu[1] = ^uint64(0)\n\t\tmu[0] = ^uint64(0)\n\n\t\treturn mu\n\t}\n\n\t// Maximise division precision by left-aligning divisor\n\n\tvar (\n\t\ty  Uint   // left-aligned copy of m\n\t\tr0 uint32 // estimate of 2^31/y\n\t)\n\n\ty.Lsh(m, uint(s)) // 1/2 \u003c y \u003c 1\n\n\t// Extract most significant 32 bits\n\n\tyh := uint32(y.arr[3] \u003e\u003e 32)\n\n\tif yh == 0x80000000 { // Avoid overflow in division\n\t\tr0 = 0xffffffff\n\t} else {\n\t\tr0, _ = bits.Div32(0x80000000, 0, yh)\n\t}\n\n\t// First iteration: 32 -\u003e 64\n\n\tt1 := uint64(r0)                 // 2^31/y\n\tt1 *= t1                         // 2^62/y^2\n\tt1, _ = bits.Mul64(t1, y.arr[3]) // 2^62/y^2 * 2^64/y / 2^64 = 2^62/y\n\n\tr1 := uint64(r0) \u003c\u003c 32 // 2^63/y\n\tr1 -= t1               // 2^63/y - 2^62/y = 2^62/y\n\tr1 *= 2                // 2^63/y\n\n\tif (r1 | (y.arr[3] \u003c\u003c 1)) == 0 {\n\t\tr1 = ^uint64(0)\n\t}\n\n\t// Second iteration: 64 -\u003e 128\n\n\t// square: 2^126/y^2\n\ta2h, a2l := bits.Mul64(r1, r1)\n\n\t// multiply by y: e2h:e2l:b2h = 2^126/y^2 * 2^128/y / 2^128 = 2^126/y\n\tb2h, _ := bits.Mul64(a2l, y.arr[2])\n\tc2h, c2l := bits.Mul64(a2l, y.arr[3])\n\td2h, d2l := bits.Mul64(a2h, y.arr[2])\n\te2h, e2l := bits.Mul64(a2h, y.arr[3])\n\n\tb2h, c := bits.Add64(b2h, c2l, 0)\n\te2l, c = bits.Add64(e2l, c2h, c)\n\te2h, _ = bits.Add64(e2h, 0, c)\n\n\t_, c = bits.Add64(b2h, d2l, 0)\n\te2l, c = bits.Add64(e2l, d2h, c)\n\te2h, _ = bits.Add64(e2h, 0, c)\n\n\t// subtract: t2h:t2l = 2^127/y - 2^126/y = 2^126/y\n\tt2l, b := bits.Sub64(0, e2l, 0)\n\tt2h, _ := bits.Sub64(r1, e2h, b)\n\n\t// double: r2h:r2l = 2^127/y\n\tr2l, c := bits.Add64(t2l, t2l, 0)\n\tr2h, _ := bits.Add64(t2h, t2h, c)\n\n\tif (r2h | r2l | (y.arr[3] \u003c\u003c 1)) == 0 {\n\t\tr2h = ^uint64(0)\n\t\tr2l = ^uint64(0)\n\t}\n\n\t// Third iteration: 128 -\u003e 192\n\n\t// square r2 (keep 256 bits): 2^190/y^2\n\ta3h, a3l := bits.Mul64(r2l, r2l)\n\tb3h, b3l := bits.Mul64(r2l, r2h)\n\tc3h, c3l := bits.Mul64(r2h, r2h)\n\n\ta3h, c = bits.Add64(a3h, b3l, 0)\n\tc3l, c = bits.Add64(c3l, b3h, c)\n\tc3h, _ = bits.Add64(c3h, 0, c)\n\n\ta3h, c = bits.Add64(a3h, b3l, 0)\n\tc3l, c = bits.Add64(c3l, b3h, c)\n\tc3h, _ = bits.Add64(c3h, 0, c)\n\n\t// multiply by y: q = 2^190/y^2 * 2^192/y / 2^192 = 2^190/y\n\n\tx0 := a3l\n\tx1 := a3h\n\tx2 := c3l\n\tx3 := c3h\n\n\tvar q0, q1, q2, q3, q4, t0 uint64\n\n\tq0, _ = bits.Mul64(x2, y.arr[0])\n\tq1, t0 = bits.Mul64(x3, y.arr[0])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, _ = bits.Add64(q1, 0, c)\n\n\tt1, _ = bits.Mul64(x1, y.arr[1])\n\tq0, c = bits.Add64(q0, t1, 0)\n\tq2, t0 = bits.Mul64(x3, y.arr[1])\n\tq1, c = bits.Add64(q1, t0, c)\n\tq2, _ = bits.Add64(q2, 0, c)\n\n\tt1, t0 = bits.Mul64(x2, y.arr[1])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, c = bits.Add64(q1, t1, c)\n\tq2, _ = bits.Add64(q2, 0, c)\n\n\tt1, t0 = bits.Mul64(x1, y.arr[2])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, c = bits.Add64(q1, t1, c)\n\tq3, t0 = bits.Mul64(x3, y.arr[2])\n\tq2, c = bits.Add64(q2, t0, c)\n\tq3, _ = bits.Add64(q3, 0, c)\n\n\tt1, _ = bits.Mul64(x0, y.arr[2])\n\tq0, c = bits.Add64(q0, t1, 0)\n\tt1, t0 = bits.Mul64(x2, y.arr[2])\n\tq1, c = bits.Add64(q1, t0, c)\n\tq2, c = bits.Add64(q2, t1, c)\n\tq3, _ = bits.Add64(q3, 0, c)\n\n\tt1, t0 = bits.Mul64(x1, y.arr[3])\n\tq1, c = bits.Add64(q1, t0, 0)\n\tq2, c = bits.Add64(q2, t1, c)\n\tq4, t0 = bits.Mul64(x3, y.arr[3])\n\tq3, c = bits.Add64(q3, t0, c)\n\tq4, _ = bits.Add64(q4, 0, c)\n\n\tt1, t0 = bits.Mul64(x0, y.arr[3])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, c = bits.Add64(q1, t1, c)\n\tt1, t0 = bits.Mul64(x2, y.arr[3])\n\tq2, c = bits.Add64(q2, t0, c)\n\tq3, c = bits.Add64(q3, t1, c)\n\tq4, _ = bits.Add64(q4, 0, c)\n\n\t// subtract: t3 = 2^191/y - 2^190/y = 2^190/y\n\t_, b = bits.Sub64(0, q0, 0)\n\t_, b = bits.Sub64(0, q1, b)\n\tt3l, b := bits.Sub64(0, q2, b)\n\tt3m, b := bits.Sub64(r2l, q3, b)\n\tt3h, _ := bits.Sub64(r2h, q4, b)\n\n\t// double: r3 = 2^191/y\n\tr3l, c := bits.Add64(t3l, t3l, 0)\n\tr3m, c := bits.Add64(t3m, t3m, c)\n\tr3h, _ := bits.Add64(t3h, t3h, c)\n\n\t// Fourth iteration: 192 -\u003e 320\n\n\t// square r3\n\n\ta4h, a4l := bits.Mul64(r3l, r3l)\n\tb4h, b4l := bits.Mul64(r3l, r3m)\n\tc4h, c4l := bits.Mul64(r3l, r3h)\n\td4h, d4l := bits.Mul64(r3m, r3m)\n\te4h, e4l := bits.Mul64(r3m, r3h)\n\tf4h, f4l := bits.Mul64(r3h, r3h)\n\n\tb4h, c = bits.Add64(b4h, c4l, 0)\n\te4l, c = bits.Add64(e4l, c4h, c)\n\te4h, _ = bits.Add64(e4h, 0, c)\n\n\ta4h, c = bits.Add64(a4h, b4l, 0)\n\td4l, c = bits.Add64(d4l, b4h, c)\n\td4h, c = bits.Add64(d4h, e4l, c)\n\tf4l, c = bits.Add64(f4l, e4h, c)\n\tf4h, _ = bits.Add64(f4h, 0, c)\n\n\ta4h, c = bits.Add64(a4h, b4l, 0)\n\td4l, c = bits.Add64(d4l, b4h, c)\n\td4h, c = bits.Add64(d4h, e4l, c)\n\tf4l, c = bits.Add64(f4l, e4h, c)\n\tf4h, _ = bits.Add64(f4h, 0, c)\n\n\t// multiply by y\n\n\tx1, x0 = bits.Mul64(d4h, y.arr[0])\n\tx3, x2 = bits.Mul64(f4h, y.arr[0])\n\tt1, t0 = bits.Mul64(f4l, y.arr[0])\n\tx1, c = bits.Add64(x1, t0, 0)\n\tx2, c = bits.Add64(x2, t1, c)\n\tx3, _ = bits.Add64(x3, 0, c)\n\n\tt1, t0 = bits.Mul64(d4h, y.arr[1])\n\tx1, c = bits.Add64(x1, t0, 0)\n\tx2, c = bits.Add64(x2, t1, c)\n\tx4, t0 := bits.Mul64(f4h, y.arr[1])\n\tx3, c = bits.Add64(x3, t0, c)\n\tx4, _ = bits.Add64(x4, 0, c)\n\tt1, t0 = bits.Mul64(d4l, y.arr[1])\n\tx0, c = bits.Add64(x0, t0, 0)\n\tx1, c = bits.Add64(x1, t1, c)\n\tt1, t0 = bits.Mul64(f4l, y.arr[1])\n\tx2, c = bits.Add64(x2, t0, c)\n\tx3, c = bits.Add64(x3, t1, c)\n\tx4, _ = bits.Add64(x4, 0, c)\n\n\tt1, t0 = bits.Mul64(a4h, y.arr[2])\n\tx0, c = bits.Add64(x0, t0, 0)\n\tx1, c = bits.Add64(x1, t1, c)\n\tt1, t0 = bits.Mul64(d4h, y.arr[2])\n\tx2, c = bits.Add64(x2, t0, c)\n\tx3, c = bits.Add64(x3, t1, c)\n\tx5, t0 := bits.Mul64(f4h, y.arr[2])\n\tx4, c = bits.Add64(x4, t0, c)\n\tx5, _ = bits.Add64(x5, 0, c)\n\tt1, t0 = bits.Mul64(d4l, y.arr[2])\n\tx1, c = bits.Add64(x1, t0, 0)\n\tx2, c = bits.Add64(x2, t1, c)\n\tt1, t0 = bits.Mul64(f4l, y.arr[2])\n\tx3, c = bits.Add64(x3, t0, c)\n\tx4, c = bits.Add64(x4, t1, c)\n\tx5, _ = bits.Add64(x5, 0, c)\n\n\tt1, t0 = bits.Mul64(a4h, y.arr[3])\n\tx1, c = bits.Add64(x1, t0, 0)\n\tx2, c = bits.Add64(x2, t1, c)\n\tt1, t0 = bits.Mul64(d4h, y.arr[3])\n\tx3, c = bits.Add64(x3, t0, c)\n\tx4, c = bits.Add64(x4, t1, c)\n\tx6, t0 := bits.Mul64(f4h, y.arr[3])\n\tx5, c = bits.Add64(x5, t0, c)\n\tx6, _ = bits.Add64(x6, 0, c)\n\tt1, t0 = bits.Mul64(a4l, y.arr[3])\n\tx0, c = bits.Add64(x0, t0, 0)\n\tx1, c = bits.Add64(x1, t1, c)\n\tt1, t0 = bits.Mul64(d4l, y.arr[3])\n\tx2, c = bits.Add64(x2, t0, c)\n\tx3, c = bits.Add64(x3, t1, c)\n\tt1, t0 = bits.Mul64(f4l, y.arr[3])\n\tx4, c = bits.Add64(x4, t0, c)\n\tx5, c = bits.Add64(x5, t1, c)\n\tx6, _ = bits.Add64(x6, 0, c)\n\n\t// subtract\n\t_, b = bits.Sub64(0, x0, 0)\n\t_, b = bits.Sub64(0, x1, b)\n\tr4l, b := bits.Sub64(0, x2, b)\n\tr4k, b := bits.Sub64(0, x3, b)\n\tr4j, b := bits.Sub64(r3l, x4, b)\n\tr4i, b := bits.Sub64(r3m, x5, b)\n\tr4h, _ := bits.Sub64(r3h, x6, b)\n\n\t// Multiply candidate for 1/4y by y, with full precision\n\n\tx0 = r4l\n\tx1 = r4k\n\tx2 = r4j\n\tx3 = r4i\n\tx4 = r4h\n\n\tq1, q0 = bits.Mul64(x0, y.arr[0])\n\tq3, q2 = bits.Mul64(x2, y.arr[0])\n\tq5, q4 := bits.Mul64(x4, y.arr[0])\n\n\tt1, t0 = bits.Mul64(x1, y.arr[0])\n\tq1, c = bits.Add64(q1, t0, 0)\n\tq2, c = bits.Add64(q2, t1, c)\n\tt1, t0 = bits.Mul64(x3, y.arr[0])\n\tq3, c = bits.Add64(q3, t0, c)\n\tq4, c = bits.Add64(q4, t1, c)\n\tq5, _ = bits.Add64(q5, 0, c)\n\n\tt1, t0 = bits.Mul64(x0, y.arr[1])\n\tq1, c = bits.Add64(q1, t0, 0)\n\tq2, c = bits.Add64(q2, t1, c)\n\tt1, t0 = bits.Mul64(x2, y.arr[1])\n\tq3, c = bits.Add64(q3, t0, c)\n\tq4, c = bits.Add64(q4, t1, c)\n\tq6, t0 := bits.Mul64(x4, y.arr[1])\n\tq5, c = bits.Add64(q5, t0, c)\n\tq6, _ = bits.Add64(q6, 0, c)\n\n\tt1, t0 = bits.Mul64(x1, y.arr[1])\n\tq2, c = bits.Add64(q2, t0, 0)\n\tq3, c = bits.Add64(q3, t1, c)\n\tt1, t0 = bits.Mul64(x3, y.arr[1])\n\tq4, c = bits.Add64(q4, t0, c)\n\tq5, c = bits.Add64(q5, t1, c)\n\tq6, _ = bits.Add64(q6, 0, c)\n\n\tt1, t0 = bits.Mul64(x0, y.arr[2])\n\tq2, c = bits.Add64(q2, t0, 0)\n\tq3, c = bits.Add64(q3, t1, c)\n\tt1, t0 = bits.Mul64(x2, y.arr[2])\n\tq4, c = bits.Add64(q4, t0, c)\n\tq5, c = bits.Add64(q5, t1, c)\n\tq7, t0 := bits.Mul64(x4, y.arr[2])\n\tq6, c = bits.Add64(q6, t0, c)\n\tq7, _ = bits.Add64(q7, 0, c)\n\n\tt1, t0 = bits.Mul64(x1, y.arr[2])\n\tq3, c = bits.Add64(q3, t0, 0)\n\tq4, c = bits.Add64(q4, t1, c)\n\tt1, t0 = bits.Mul64(x3, y.arr[2])\n\tq5, c = bits.Add64(q5, t0, c)\n\tq6, c = bits.Add64(q6, t1, c)\n\tq7, _ = bits.Add64(q7, 0, c)\n\n\tt1, t0 = bits.Mul64(x0, y.arr[3])\n\tq3, c = bits.Add64(q3, t0, 0)\n\tq4, c = bits.Add64(q4, t1, c)\n\tt1, t0 = bits.Mul64(x2, y.arr[3])\n\tq5, c = bits.Add64(q5, t0, c)\n\tq6, c = bits.Add64(q6, t1, c)\n\tq8, t0 := bits.Mul64(x4, y.arr[3])\n\tq7, c = bits.Add64(q7, t0, c)\n\tq8, _ = bits.Add64(q8, 0, c)\n\n\tt1, t0 = bits.Mul64(x1, y.arr[3])\n\tq4, c = bits.Add64(q4, t0, 0)\n\tq5, c = bits.Add64(q5, t1, c)\n\tt1, t0 = bits.Mul64(x3, y.arr[3])\n\tq6, c = bits.Add64(q6, t0, c)\n\tq7, c = bits.Add64(q7, t1, c)\n\tq8, _ = bits.Add64(q8, 0, c)\n\n\t// Final adjustment\n\n\t// subtract q from 1/4\n\t_, b = bits.Sub64(0, q0, 0)\n\t_, b = bits.Sub64(0, q1, b)\n\t_, b = bits.Sub64(0, q2, b)\n\t_, b = bits.Sub64(0, q3, b)\n\t_, b = bits.Sub64(0, q4, b)\n\t_, b = bits.Sub64(0, q5, b)\n\t_, b = bits.Sub64(0, q6, b)\n\t_, b = bits.Sub64(0, q7, b)\n\t_, b = bits.Sub64(uint64(1)\u003c\u003c62, q8, b)\n\n\t// decrement the result\n\tx0, t := bits.Sub64(r4l, 1, 0)\n\tx1, t = bits.Sub64(r4k, 0, t)\n\tx2, t = bits.Sub64(r4j, 0, t)\n\tx3, t = bits.Sub64(r4i, 0, t)\n\tx4, _ = bits.Sub64(r4h, 0, t)\n\n\t// commit the decrement if the subtraction underflowed (reciprocal was too large)\n\tif b != 0 {\n\t\tr4h, r4i, r4j, r4k, r4l = x4, x3, x2, x1, x0\n\t}\n\n\t// Shift to correct bit alignment, truncating excess bits\n\n\tp = (p \u0026 63) - 1\n\n\tx0, c = bits.Add64(r4l, r4l, 0)\n\tx1, c = bits.Add64(r4k, r4k, c)\n\tx2, c = bits.Add64(r4j, r4j, c)\n\tx3, c = bits.Add64(r4i, r4i, c)\n\tx4, _ = bits.Add64(r4h, r4h, c)\n\n\tif p \u003c 0 {\n\t\tr4h, r4i, r4j, r4k, r4l = x4, x3, x2, x1, x0\n\t\tp = 0 // avoid negative shift below\n\t}\n\n\t{\n\t\tr := uint(p)      // right shift\n\t\tl := uint(64 - r) // left shift\n\n\t\tx0 = (r4l \u003e\u003e r) | (r4k \u003c\u003c l)\n\t\tx1 = (r4k \u003e\u003e r) | (r4j \u003c\u003c l)\n\t\tx2 = (r4j \u003e\u003e r) | (r4i \u003c\u003c l)\n\t\tx3 = (r4i \u003e\u003e r) | (r4h \u003c\u003c l)\n\t\tx4 = (r4h \u003e\u003e r)\n\t}\n\n\tif p \u003e 0 {\n\t\tr4h, r4i, r4j, r4k, r4l = x4, x3, x2, x1, x0\n\t}\n\n\tmu[0] = r4l\n\tmu[1] = r4k\n\tmu[2] = r4j\n\tmu[3] = r4i\n\tmu[4] = r4h\n\n\treturn mu\n}\n\n// reduce4 computes the least non-negative residue of x modulo m\n//\n// requires a four-word modulus (m.arr[3] \u003e 1) and its inverse (mu)\nfunc reduce4(x [8]uint64, m *Uint, mu [5]uint64) (z Uint) {\n\t// NB: Most variable names in the comments match the pseudocode for\n\t// \tBarrett reduction in the Handbook of Applied Cryptography.\n\n\t// q1 = x/2^192\n\n\tx0 := x[3]\n\tx1 := x[4]\n\tx2 := x[5]\n\tx3 := x[6]\n\tx4 := x[7]\n\n\t// q2 = q1 * mu; q3 = q2 / 2^320\n\n\tvar q0, q1, q2, q3, q4, q5, t0, t1, c uint64\n\n\tq0, _ = bits.Mul64(x3, mu[0])\n\tq1, t0 = bits.Mul64(x4, mu[0])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, _ = bits.Add64(q1, 0, c)\n\n\tt1, _ = bits.Mul64(x2, mu[1])\n\tq0, c = bits.Add64(q0, t1, 0)\n\tq2, t0 = bits.Mul64(x4, mu[1])\n\tq1, c = bits.Add64(q1, t0, c)\n\tq2, _ = bits.Add64(q2, 0, c)\n\n\tt1, t0 = bits.Mul64(x3, mu[1])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, c = bits.Add64(q1, t1, c)\n\tq2, _ = bits.Add64(q2, 0, c)\n\n\tt1, t0 = bits.Mul64(x2, mu[2])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, c = bits.Add64(q1, t1, c)\n\tq3, t0 = bits.Mul64(x4, mu[2])\n\tq2, c = bits.Add64(q2, t0, c)\n\tq3, _ = bits.Add64(q3, 0, c)\n\n\tt1, _ = bits.Mul64(x1, mu[2])\n\tq0, c = bits.Add64(q0, t1, 0)\n\tt1, t0 = bits.Mul64(x3, mu[2])\n\tq1, c = bits.Add64(q1, t0, c)\n\tq2, c = bits.Add64(q2, t1, c)\n\tq3, _ = bits.Add64(q3, 0, c)\n\n\tt1, _ = bits.Mul64(x0, mu[3])\n\tq0, c = bits.Add64(q0, t1, 0)\n\tt1, t0 = bits.Mul64(x2, mu[3])\n\tq1, c = bits.Add64(q1, t0, c)\n\tq2, c = bits.Add64(q2, t1, c)\n\tq4, t0 = bits.Mul64(x4, mu[3])\n\tq3, c = bits.Add64(q3, t0, c)\n\tq4, _ = bits.Add64(q4, 0, c)\n\n\tt1, t0 = bits.Mul64(x1, mu[3])\n\tq0, c = bits.Add64(q0, t0, 0)\n\tq1, c = bits.Add64(q1, t1, c)\n\tt1, t0 = bits.Mul64(x3, mu[3])\n\tq2, c = bits.Add64(q2, t0, c)\n\tq3, c = bits.Add64(q3, t1, c)\n\tq4, _ = bits.Add64(q4, 0, c)\n\n\tt1, t0 = bits.Mul64(x0, mu[4])\n\t_, c = bits.Add64(q0, t0, 0)\n\tq1, c = bits.Add64(q1, t1, c)\n\tt1, t0 = bits.Mul64(x2, mu[4])\n\tq2, c = bits.Add64(q2, t0, c)\n\tq3, c = bits.Add64(q3, t1, c)\n\tq5, t0 = bits.Mul64(x4, mu[4])\n\tq4, c = bits.Add64(q4, t0, c)\n\tq5, _ = bits.Add64(q5, 0, c)\n\n\tt1, t0 = bits.Mul64(x1, mu[4])\n\tq1, c = bits.Add64(q1, t0, 0)\n\tq2, c = bits.Add64(q2, t1, c)\n\tt1, t0 = bits.Mul64(x3, mu[4])\n\tq3, c = bits.Add64(q3, t0, c)\n\tq4, c = bits.Add64(q4, t1, c)\n\tq5, _ = bits.Add64(q5, 0, c)\n\n\t// Drop the fractional part of q3\n\n\tq0 = q1\n\tq1 = q2\n\tq2 = q3\n\tq3 = q4\n\tq4 = q5\n\n\t// r1 = x mod 2^320\n\n\tx0 = x[0]\n\tx1 = x[1]\n\tx2 = x[2]\n\tx3 = x[3]\n\tx4 = x[4]\n\n\t// r2 = q3 * m mod 2^320\n\n\tvar r0, r1, r2, r3, r4 uint64\n\n\tr4, r3 = bits.Mul64(q0, m.arr[3])\n\t_, t0 = bits.Mul64(q1, m.arr[3])\n\tr4, _ = bits.Add64(r4, t0, 0)\n\n\tt1, r2 = bits.Mul64(q0, m.arr[2])\n\tr3, c = bits.Add64(r3, t1, 0)\n\t_, t0 = bits.Mul64(q2, m.arr[2])\n\tr4, _ = bits.Add64(r4, t0, c)\n\n\tt1, t0 = bits.Mul64(q1, m.arr[2])\n\tr3, c = bits.Add64(r3, t0, 0)\n\tr4, _ = bits.Add64(r4, t1, c)\n\n\tt1, r1 = bits.Mul64(q0, m.arr[1])\n\tr2, c = bits.Add64(r2, t1, 0)\n\tt1, t0 = bits.Mul64(q2, m.arr[1])\n\tr3, c = bits.Add64(r3, t0, c)\n\tr4, _ = bits.Add64(r4, t1, c)\n\n\tt1, t0 = bits.Mul64(q1, m.arr[1])\n\tr2, c = bits.Add64(r2, t0, 0)\n\tr3, c = bits.Add64(r3, t1, c)\n\t_, t0 = bits.Mul64(q3, m.arr[1])\n\tr4, _ = bits.Add64(r4, t0, c)\n\n\tt1, r0 = bits.Mul64(q0, m.arr[0])\n\tr1, c = bits.Add64(r1, t1, 0)\n\tt1, t0 = bits.Mul64(q2, m.arr[0])\n\tr2, c = bits.Add64(r2, t0, c)\n\tr3, c = bits.Add64(r3, t1, c)\n\t_, t0 = bits.Mul64(q4, m.arr[0])\n\tr4, _ = bits.Add64(r4, t0, c)\n\n\tt1, t0 = bits.Mul64(q1, m.arr[0])\n\tr1, c = bits.Add64(r1, t0, 0)\n\tr2, c = bits.Add64(r2, t1, c)\n\tt1, t0 = bits.Mul64(q3, m.arr[0])\n\tr3, c = bits.Add64(r3, t0, c)\n\tr4, _ = bits.Add64(r4, t1, c)\n\n\t// r = r1 - r2\n\n\tvar b uint64\n\n\tr0, b = bits.Sub64(x0, r0, 0)\n\tr1, b = bits.Sub64(x1, r1, b)\n\tr2, b = bits.Sub64(x2, r2, b)\n\tr3, b = bits.Sub64(x3, r3, b)\n\tr4, b = bits.Sub64(x4, r4, b)\n\n\t// if r\u003c0 then r+=m\n\n\tif b != 0 {\n\t\tr0, c = bits.Add64(r0, m.arr[0], 0)\n\t\tr1, c = bits.Add64(r1, m.arr[1], c)\n\t\tr2, c = bits.Add64(r2, m.arr[2], c)\n\t\tr3, c = bits.Add64(r3, m.arr[3], c)\n\t\tr4, _ = bits.Add64(r4, 0, c)\n\t}\n\n\t// while (r\u003e=m) r-=m\n\n\tfor {\n\t\t// q = r - m\n\t\tq0, b = bits.Sub64(r0, m.arr[0], 0)\n\t\tq1, b = bits.Sub64(r1, m.arr[1], b)\n\t\tq2, b = bits.Sub64(r2, m.arr[2], b)\n\t\tq3, b = bits.Sub64(r3, m.arr[3], b)\n\t\tq4, b = bits.Sub64(r4, 0, b)\n\n\t\t// if borrow break\n\t\tif b != 0 {\n\t\t\tbreak\n\t\t}\n\n\t\t// r = q\n\t\tr4, r3, r2, r1, r0 = q4, q3, q2, q1, q0\n\t}\n\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = r3, r2, r1, r0\n\n\treturn z\n}\n"},{"name":"uint256.gno","body":"// Ported from https://github.com/holiman/uint256\n// This package provides a 256-bit unsigned integer type, Uint256, and associated functions.\npackage uint256\n\nimport (\n\t\"errors\"\n\t\"math/bits\"\n\t\"strconv\"\n)\n\nconst (\n\tMaxUint64 = 1\u003c\u003c64 - 1\n\tuintSize  = 32 \u003c\u003c (^uint(0) \u003e\u003e 63)\n)\n\n// Uint is represented as an array of 4 uint64, in little-endian order,\n// so that Uint[3] is the most significant, and Uint[0] is the least significant\ntype Uint struct {\n\tarr [4]uint64\n}\n\n// NewUint returns a new initialized Uint.\nfunc NewUint(val uint64) *Uint {\n\tz := \u0026Uint{arr: [4]uint64{val, 0, 0, 0}}\n\treturn z\n}\n\n// Zero returns a new Uint initialized to zero.\nfunc Zero() *Uint {\n\treturn NewUint(0)\n}\n\n// One returns a new Uint initialized to one.\nfunc One() *Uint {\n\treturn NewUint(1)\n}\n\n// SetAllOne sets all the bits of z to 1\nfunc (z *Uint) SetAllOne() *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = MaxUint64, MaxUint64, MaxUint64, MaxUint64\n\treturn z\n}\n\n// Set sets z to x and returns z.\nfunc (z *Uint) Set(x *Uint) *Uint {\n\t*z = *x\n\n\treturn z\n}\n\n// SetOne sets z to 1\nfunc (z *Uint) SetOne() *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, 0, 0, 1\n\treturn z\n}\n\nconst twoPow256Sub1 = \"115792089237316195423570985008687907853269984665640564039457584007913129639935\"\n\n// SetFromDecimal sets z from the given string, interpreted as a decimal number.\n// OBS! This method is _not_ strictly identical to the (*big.Uint).SetString(..., 10) method.\n// Notable differences:\n// - This method does not accept underscore input, e.g. \"100_000\",\n// - This method does not accept negative zero as valid, e.g \"-0\",\n//   - (this method does not accept any negative input as valid))\nfunc (z *Uint) SetFromDecimal(s string) (err error) {\n\t// Remove max one leading +\n\tif len(s) \u003e 0 \u0026\u0026 s[0] == '+' {\n\t\ts = s[1:]\n\t}\n\t// Remove any number of leading zeroes\n\tif len(s) \u003e 0 \u0026\u0026 s[0] == '0' {\n\t\tvar i int\n\t\tvar c rune\n\t\tfor i, c = range s {\n\t\t\tif c != '0' {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\ts = s[i:]\n\t}\n\tif len(s) \u003c len(twoPow256Sub1) {\n\t\treturn z.fromDecimal(s)\n\t}\n\tif len(s) == len(twoPow256Sub1) {\n\t\tif s \u003e twoPow256Sub1 {\n\t\t\treturn ErrBig256Range\n\t\t}\n\t\treturn z.fromDecimal(s)\n\t}\n\treturn ErrBig256Range\n}\n\n// FromDecimal is a convenience-constructor to create an Uint from a\n// decimal (base 10) string. Numbers larger than 256 bits are not accepted.\nfunc FromDecimal(decimal string) (*Uint, error) {\n\tvar z Uint\n\tif err := z.SetFromDecimal(decimal); err != nil {\n\t\treturn nil, err\n\t}\n\treturn \u0026z, nil\n}\n\n// MustFromDecimal is a convenience-constructor to create an Uint from a\n// decimal (base 10) string.\n// Returns a new Uint and panics if any error occurred.\nfunc MustFromDecimal(decimal string) *Uint {\n\tvar z Uint\n\tif err := z.SetFromDecimal(decimal); err != nil {\n\t\tpanic(err)\n\t}\n\treturn \u0026z\n}\n\n// multipliers holds the values that are needed for fromDecimal\nvar multipliers = [5]*Uint{\n\tnil, // represents first round, no multiplication needed\n\t{[4]uint64{10000000000000000000, 0, 0, 0}},                                     // 10 ^ 19\n\t{[4]uint64{687399551400673280, 5421010862427522170, 0, 0}},                     // 10 ^ 38\n\t{[4]uint64{5332261958806667264, 17004971331911604867, 2938735877055718769, 0}}, // 10 ^ 57\n\t{[4]uint64{0, 8607968719199866880, 532749306367912313, 1593091911132452277}},   // 10 ^ 76\n}\n\n// fromDecimal is a helper function to only ever be called via SetFromDecimal\n// this function takes a string and chunks it up, calling ParseUint on it up to 5 times\n// these chunks are then multiplied by the proper power of 10, then added together.\nfunc (z *Uint) fromDecimal(bs string) error {\n\t// first clear the input\n\tz.Clear()\n\t// the maximum value of uint64 is 18446744073709551615, which is 20 characters\n\t// one less means that a string of 19 9's is always within the uint64 limit\n\tvar (\n\t\tnum       uint64\n\t\terr       error\n\t\tremaining = len(bs)\n\t)\n\tif remaining == 0 {\n\t\treturn errors.New(\"EOF\")\n\t}\n\t// We proceed in steps of 19 characters (nibbles), from least significant to most significant.\n\t// This means that the first (up to) 19 characters do not need to be multiplied.\n\t// In the second iteration, our slice of 19 characters needs to be multipleied\n\t// by a factor of 10^19. Et cetera.\n\tfor i, mult := range multipliers {\n\t\tif remaining \u003c= 0 {\n\t\t\treturn nil // Done\n\t\t} else if remaining \u003e 19 {\n\t\t\tnum, err = strconv.ParseUint(bs[remaining-19:remaining], 10, 64)\n\t\t} else {\n\t\t\t// Final round\n\t\t\tnum, err = strconv.ParseUint(bs, 10, 64)\n\t\t}\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t// add that number to our running total\n\t\tif i == 0 {\n\t\t\tz.SetUint64(num)\n\t\t} else {\n\t\t\tbase := NewUint(num)\n\t\t\tz.Add(z, base.Mul(base, mult))\n\t\t}\n\t\t// Chop off another 19 characters\n\t\tif remaining \u003e 19 {\n\t\t\tbs = bs[0 : remaining-19]\n\t\t}\n\t\tremaining -= 19\n\t}\n\treturn nil\n}\n\n// Byte sets z to the value of the byte at position n,\n// with 'z' considered as a big-endian 32-byte integer\n// if 'n' \u003e 32, f is set to 0\n// Example: f = '5', n=31 =\u003e 5\nfunc (z *Uint) Byte(n *Uint) *Uint {\n\t// in z, z.arr[0] is the least significant\n\tif number, overflow := n.Uint64WithOverflow(); !overflow {\n\t\tif number \u003c 32 {\n\t\t\tnumber := z.arr[4-1-number/8]\n\t\t\toffset := (n.arr[0] \u0026 0x7) \u003c\u003c 3 // 8*(n.d % 8)\n\t\t\tz.arr[0] = (number \u0026 (0xff00000000000000 \u003e\u003e offset)) \u003e\u003e (56 - offset)\n\t\t\tz.arr[3], z.arr[2], z.arr[1] = 0, 0, 0\n\t\t\treturn z\n\t\t}\n\t}\n\n\treturn z.Clear()\n}\n\n// BitLen returns the number of bits required to represent z\nfunc (z *Uint) BitLen() int {\n\tswitch {\n\tcase z.arr[3] != 0:\n\t\treturn 192 + bits.Len64(z.arr[3])\n\tcase z.arr[2] != 0:\n\t\treturn 128 + bits.Len64(z.arr[2])\n\tcase z.arr[1] != 0:\n\t\treturn 64 + bits.Len64(z.arr[1])\n\tdefault:\n\t\treturn bits.Len64(z.arr[0])\n\t}\n}\n\n// ByteLen returns the number of bytes required to represent z\nfunc (z *Uint) ByteLen() int {\n\treturn (z.BitLen() + 7) / 8\n}\n\n// Clear sets z to 0\nfunc (z *Uint) Clear() *Uint {\n\tz.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, 0, 0, 0\n\treturn z\n}\n\nconst (\n\t// hextable  = \"0123456789abcdef\"\n\tbintable  = \"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\a\\b\\t\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\n\\v\\f\\r\\x0e\\x0f\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\n\\v\\f\\r\\x0e\\x0f\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\"\n\tbadNibble = 0xff\n)\n\n// SetFromHex sets z from the given string, interpreted as a hexadecimal number.\n// OBS! This method is _not_ strictly identical to the (*big.Int).SetString(..., 16) method.\n// Notable differences:\n// - This method _require_ \"0x\" or \"0X\" prefix.\n// - This method does not accept zero-prefixed hex, e.g. \"0x0001\"\n// - This method does not accept underscore input, e.g. \"100_000\",\n// - This method does not accept negative zero as valid, e.g \"-0x0\",\n//   - (this method does not accept any negative input as valid)\nfunc (z *Uint) SetFromHex(hex string) error {\n\treturn z.fromHex(hex)\n}\n\n// fromHex is the internal implementation of parsing a hex-string.\nfunc (z *Uint) fromHex(hex string) error {\n\tif err := checkNumberS(hex); err != nil {\n\t\treturn err\n\t}\n\tif len(hex) \u003e 66 {\n\t\treturn ErrBig256Range\n\t}\n\tz.Clear()\n\tend := len(hex)\n\tfor i := 0; i \u003c 4; i++ {\n\t\tstart := end - 16\n\t\tif start \u003c 2 {\n\t\t\tstart = 2\n\t\t}\n\t\tfor ri := start; ri \u003c end; ri++ {\n\t\t\tnib := bintable[hex[ri]]\n\t\t\tif nib == badNibble {\n\t\t\t\treturn ErrSyntax\n\t\t\t}\n\t\t\tz.arr[i] = z.arr[i] \u003c\u003c 4\n\t\t\tz.arr[i] += uint64(nib)\n\t\t}\n\t\tend = start\n\t}\n\treturn nil\n}\n\n// FromHex is a convenience-constructor to create an Uint from\n// a hexadecimal string. The string is required to be '0x'-prefixed\n// Numbers larger than 256 bits are not accepted.\nfunc FromHex(hex string) (*Uint, error) {\n\tvar z Uint\n\tif err := z.fromHex(hex); err != nil {\n\t\treturn nil, err\n\t}\n\treturn \u0026z, nil\n}\n\n// MustFromHex is a convenience-constructor to create an Uint from\n// a hexadecimal string.\n// Returns a new Uint and panics if any error occurred.\nfunc MustFromHex(hex string) *Uint {\n\tvar z Uint\n\tif err := z.fromHex(hex); err != nil {\n\t\tpanic(err)\n\t}\n\treturn \u0026z\n}\n\n// Clone creates a new Uint identical to z\nfunc (z *Uint) Clone() *Uint {\n\tvar x Uint\n\tx.arr[0] = z.arr[0]\n\tx.arr[1] = z.arr[1]\n\tx.arr[2] = z.arr[2]\n\tx.arr[3] = z.arr[3]\n\n\treturn \u0026x\n}\n"},{"name":"uint256_test.gno","body":"package uint256\n\nimport (\n\t\"testing\"\n)\n\nfunc TestSetAllOne(t *testing.T) {\n\tz := Zero()\n\tz.SetAllOne()\n\tif z.String() != twoPow256Sub1 {\n\t\tt.Errorf(\"Expected all ones, got %s\", z.String())\n\t}\n}\n\nfunc TestByte(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\tposition uint64\n\t\texpected byte\n\t}{\n\t\t{\"0x1000000000000000000000000000000000000000000000000000000000000000\", 0, 16},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 0, 255},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 31, 255},\n\t}\n\n\tfor i, tt := range tests {\n\t\tz, _ := FromHex(tt.input)\n\t\tn := NewUint(tt.position)\n\t\tresult := z.Byte(n)\n\n\t\tif result.arr[0] != uint64(tt.expected) {\n\t\t\tt.Errorf(\"Test case %d failed. Input: %s, Position: %d, Expected: %d, Got: %d\",\n\t\t\t\ti, tt.input, tt.position, tt.expected, result.arr[0])\n\t\t}\n\n\t\t// check other array elements are 0\n\t\tif result.arr[1] != 0 || result.arr[2] != 0 || result.arr[3] != 0 {\n\t\t\tt.Errorf(\"Test case %d failed. Non-zero values in upper bytes\", i)\n\t\t}\n\t}\n\n\t// overflow\n\tz, _ := FromHex(\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\")\n\tn := NewUint(32)\n\tresult := z.Byte(n)\n\n\tif !result.IsZero() {\n\t\tt.Errorf(\"Expected zero for position \u003e= 32, got %v\", result)\n\t}\n}\n\nfunc TestBitLen(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected int\n\t}{\n\t\t{\"0x0\", 0},\n\t\t{\"0x1\", 1},\n\t\t{\"0xff\", 8},\n\t\t{\"0x100\", 9},\n\t\t{\"0xffff\", 16},\n\t\t{\"0x10000\", 17},\n\t\t{\"0xffffffffffffffff\", 64},\n\t\t{\"0x10000000000000000\", 65},\n\t\t{\"0xffffffffffffffffffffffffffffffff\", 128},\n\t\t{\"0x100000000000000000000000000000000\", 129},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 256},\n\t}\n\n\tfor i, tt := range tests {\n\t\tz, _ := FromHex(tt.input)\n\t\tresult := z.BitLen()\n\n\t\tif result != tt.expected {\n\t\t\tt.Errorf(\"Test case %d failed. Input: %s, Expected: %d, Got: %d\",\n\t\t\t\ti, tt.input, tt.expected, result)\n\t\t}\n\t}\n}\n\nfunc TestByteLen(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected int\n\t}{\n\t\t{\"0x0\", 0},\n\t\t{\"0x1\", 1},\n\t\t{\"0xff\", 1},\n\t\t{\"0x100\", 2},\n\t\t{\"0xffff\", 2},\n\t\t{\"0x10000\", 3},\n\t\t{\"0xffffffffffffffff\", 8},\n\t\t{\"0x10000000000000000\", 9},\n\t\t{\"0xffffffffffffffffffffffffffffffff\", 16},\n\t\t{\"0x100000000000000000000000000000000\", 17},\n\t\t{\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", 32},\n\t}\n\n\tfor i, tt := range tests {\n\t\tz, _ := FromHex(tt.input)\n\t\tresult := z.ByteLen()\n\n\t\tif result != tt.expected {\n\t\t\tt.Errorf(\"Test case %d failed. Input: %s, Expected: %d, Got: %d\",\n\t\t\t\ti, tt.input, tt.expected, result)\n\t\t}\n\t}\n}\n\nfunc TestClone(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected string\n\t}{\n\t\t{\"0x1\", \"1\"},\n\t\t{\"0x100\", \"256\"},\n\t\t{\"0x10000000000000000\", \"18446744073709551616\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz, _ := FromHex(tt.input)\n\t\tresult := z.Clone()\n\t\tif result.String() != tt.expected {\n\t\t\tt.Errorf(\"Test %s failed. Expected %s, got %s\", tt.input, tt.expected, result.String())\n\t\t}\n\t}\n}\n"},{"name":"utils.gno","body":"package uint256\n\nfunc checkNumberS(input string) error {\n\tconst fn = \"UnmarshalText\"\n\tl := len(input)\n\tif l == 0 {\n\t\treturn errEmptyString(fn, input)\n\t}\n\tif l \u003c 2 || input[0] != '0' ||\n\t\t(input[1] != 'x' \u0026\u0026 input[1] != 'X') {\n\t\treturn errMissingPrefix(fn, input)\n\t}\n\tif l == 2 {\n\t\treturn errEmptyNumber(fn, input)\n\t}\n\tif len(input) \u003e 3 \u0026\u0026 input[2] == '0' {\n\t\treturn errLeadingZero(fn, input)\n\t}\n\treturn nil\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Mj7p2GyxbdDOpbX5m55AmsmhL+98it3IWsjTzo5+eRPDQ2fEWk6gyZsGDehcmgpvZpM+kp/KZJ/oL+UAyhFjCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"int256","path":"gno.land/p/onbloc/int256","files":[{"name":"arithmetic.gno","body":"package int256\n\nimport (\n\t\"gno.land/p/onbloc/uint256\"\n)\n\nconst divisionByZeroError = \"division by zero\"\n\n// Add adds two int256 values and saves the result in z.\nfunc (z *Int) Add(x, y *Int) *Int {\n\tz.value.Add(\u0026x.value, \u0026y.value)\n\treturn z\n}\n\n// AddUint256 adds int256 and uint256 values and saves the result in z.\nfunc (z *Int) AddUint256(x *Int, y *uint256.Uint) *Int {\n\tz.value.Add(\u0026x.value, y)\n\treturn z\n}\n\n// Sub subtracts two int256 values and saves the result in z.\nfunc (z *Int) Sub(x, y *Int) *Int {\n\tz.value.Sub(\u0026x.value, \u0026y.value)\n\treturn z\n}\n\n// SubUint256 subtracts uint256 and int256 values and saves the result in z.\nfunc (z *Int) SubUint256(x *Int, y *uint256.Uint) *Int {\n\tz.value.Sub(\u0026x.value, y)\n\treturn z\n}\n\n// Mul multiplies two int256 values and saves the result in z.\n//\n// It considers the signs of the operands to determine the sign of the result.\nfunc (z *Int) Mul(x, y *Int) *Int {\n\txAbs, xSign := x.Abs(), x.Sign()\n\tyAbs, ySign := y.Abs(), y.Sign()\n\n\tz.value.Mul(xAbs, yAbs)\n\n\tif xSign != ySign {\n\t\tz.value.Neg(\u0026z.value)\n\t}\n\n\treturn z\n}\n\n// Abs returns the absolute value of z.\nfunc (z *Int) Abs() *uint256.Uint {\n\tif z.Sign() \u003e= 0 {\n\t\treturn \u0026z.value\n\t}\n\n\tvar absValue uint256.Uint\n\tabsValue.Sub(uint0, \u0026z.value).Neg(\u0026z.value)\n\n\treturn \u0026absValue\n}\n\n// Div performs integer division z = x / y and returns z.\n// If y == 0, it panics with a \"division by zero\" error.\n//\n// This function handles signed division using two's complement representation:\n//  1. Determine the sign of the quotient based on the signs of x and y.\n//  2. Perform unsigned division on the absolute values.\n//  3. Adjust the result's sign if necessary.\n//\n// Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):\n//\n// Let x = -6 (11111010 in two's complement) and y = 3 (00000011)\n//\n// Step 2: Determine signs\n//\n//\tx: negative (MSB is 1)\n//\ty: positive (MSB is 0)\n//\n// Step 3: Calculate absolute values\n//\n//\t|x| = 6:  11111010 -\u003e 00000110\n//\t     NOT: 00000101\n//\t     +1:  00000110\n//\n//\t|y| = 3:  00000011 (already positive)\n//\n// Step 4: Unsigned division\n//\n//\t6 / 3 = 2:  00000010\n//\n// Step 5: Adjust sign (x and y have different signs)\n//\n//\t-2:  00000010 -\u003e 11111110\n//\t     NOT: 11111101\n//\t     +1:  11111110\n//\n// Note: This implementation rounds towards zero, as is standard in Go.\nfunc (z *Int) Div(x, y *Int) *Int {\n\t// Step 1: Check for division by zero\n\tif y.IsZero() {\n\t\tpanic(divisionByZeroError)\n\t}\n\n\t// Step 2, 3: Calculate the absolute values of x and y\n\txAbs, xSign := x.Abs(), x.Sign()\n\tyAbs, ySign := y.Abs(), y.Sign()\n\n\t// Step 4: Perform unsigned division on the absolute values\n\tz.value.Div(xAbs, yAbs)\n\n\t// Step 5: Adjust the sign of the result\n\t// if x and y have different signs, the result must be negative\n\tif xSign != ySign {\n\t\tz.value.Neg(\u0026z.value)\n\t}\n\n\treturn z\n}\n\n// Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):\n//\n// Let x = -7 (11111001 in two's complement) and y = 3 (00000011)\n//\n// Step 2: Determine signs\n//\n//\tx: negative (MSB is 1)\n//\ty: positive (MSB is 0)\n//\n// Step 3: Calculate absolute values\n//\n//\t|x| = 7:  11111001 -\u003e 00000111\n//\t     NOT: 00000110\n//\t     +1:  00000111\n//\n//\t|y| = 3:  00000011 (already positive)\n//\n// Step 4: Unsigned division\n//\n//\t7 / 3 = 2:  00000010\n//\n// Step 5: Adjust sign (x and y have different signs)\n//\n//\t-2:  00000010 -\u003e 11111110\n//\t     NOT: 11111101\n//\t     +1:  11111110\n//\n// Final result: -2 (11111110 in two's complement)\n//\n// Note: This implementation rounds towards zero, as is standard in Go.\nfunc (z *Int) Quo(x, y *Int) *Int {\n\t// Step 1: Check for division by zero\n\tif y.IsZero() {\n\t\tpanic(divisionByZeroError)\n\t}\n\n\t// Step 2, 3: Calculate the absolute values of x and y\n\txAbs, xSign := x.Abs(), x.Sign()\n\tyAbs, ySign := y.Abs(), y.Sign()\n\n\t// perform unsigned division on the absolute values\n\tz.value.Div(xAbs, yAbs)\n\n\t// Step 5: Adjust the sign of the result\n\t// if x and y have different signs, the result must be negative\n\tif xSign != ySign {\n\t\tz.value.Neg(\u0026z.value)\n\t}\n\n\treturn z\n}\n\n// Rem sets z to the remainder x%y for y != 0 and returns z.\n//\n// The function performs the following steps:\n//  1. Check for division by zero\n//  2. Determine the signs of x and y\n//  3. Calculate the absolute values of x and y\n//  4. Perform unsigned division and get the remainder\n//  5. Adjust the sign of the remainder\n//\n// Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):\n//\n// Let x = -7 (11111001 in two's complement) and y = 3 (00000011)\n//\n// Step 2: Determine signs\n//\n//\tx: negative (MSB is 1)\n//\ty: positive (MSB is 0)\n//\n// Step 3: Calculate absolute values\n//\n//\t|x| = 7:  11111001 -\u003e 00000111\n//\t     NOT: 00000110\n//\t     +1:  00000111\n//\n//\t|y| = 3:  00000011 (already positive)\n//\n// Step 4: Unsigned division\n//\n//\t7 / 3 = 2 remainder 1\n//\tq = 2:  00000010 (not used in result)\n//\tr = 1:  00000001\n//\n// Step 5: Adjust sign of remainder (x is negative)\n//\n//\t-1:  00000001 -\u003e 11111111\n//\t     NOT: 11111110\n//\t     +1:  11111111\n//\n// Final result: -1 (11111111 in two's complement)\n//\n// Note: The sign of the remainder is always the same as the sign of the dividend (x).\nfunc (z *Int) Rem(x, y *Int) *Int {\n\t// Step 1: Check for division by zero\n\tif y.IsZero() {\n\t\tpanic(divisionByZeroError)\n\t}\n\n\t// Step 2, 3\n\txAbs, xSign := x.Abs(), x.Sign()\n\tyAbs := y.Abs()\n\n\t// Step 4: Perform unsigned division and get the remainder\n\tvar q, r uint256.Uint\n\tq.DivMod(xAbs, yAbs, \u0026r)\n\n\t// Step 5: Adjust the sign of the remainder\n\tif xSign \u003c 0 {\n\t\tr.Neg(\u0026r)\n\t}\n\n\tz.value.Set(\u0026r)\n\treturn z\n}\n\n// Mod sets z to the modulus x%y for y != 0 and returns z.\n// The result (z) has the same sign as the divisor y.\nfunc (z *Int) Mod(x, y *Int) *Int {\n\treturn z.ModE(x, y)\n}\n\n// DivE performs Euclidean division of x by y, setting z to the quotient and returning z.\n// If y == 0, it panics with a \"division by zero\" error.\n//\n// Euclidean division satisfies the following properties:\n//  1. The remainder is always non-negative: 0 \u003c= x mod y \u003c |y|\n//  2. It follows the identity: x = y * (x div y) + (x mod y)\nfunc (z *Int) DivE(x, y *Int) *Int {\n\tif y.IsZero() {\n\t\tpanic(divisionByZeroError)\n\t}\n\n\t// Compute the truncated division quotient\n\tz.Quo(x, y)\n\n\t// Compute the remainder\n\tr := new(Int).Rem(x, y)\n\n\t// If the remainder is negative, adjust the quotient\n\tif r.Sign() \u003c 0 {\n\t\tif y.Sign() \u003e 0 {\n\t\t\tz.Sub(z, NewInt(1))\n\t\t} else {\n\t\t\tz.Add(z, NewInt(1))\n\t\t}\n\t}\n\n\treturn z\n}\n\n// ModE computes the Euclidean modulus of x by y, setting z to the result and returning z.\n// If y == 0, it panics with a \"division by zero\" error.\n//\n// The Euclidean modulus is always non-negative and satisfies:\n//\n//\t0 \u003c= x mod y \u003c |y|\n//\n// Example visualization for 8-bit integers (scaled down from 256-bit for simplicity):\n//\n// Case 1: Let x = -7 (11111001 in two's complement) and y = 3 (00000011)\n//\n// Step 1: Compute remainder (using Rem)\n//\n//\tResult of Rem: -1 (11111111 in two's complement)\n//\n// Step 2: Adjust sign (result is negative, y is positive)\n//\n//\t-1 + 3 = 2\n//\t11111111 + 00000011 = 00000010\n//\n// Final result: 2 (00000010)\n//\n// Case 2: Let x = -7 (11111001 in two's complement) and y = -3 (11111101 in two's complement)\n//\n// Step 1: Compute remainder (using Rem)\n//\n//\tResult of Rem: -1 (11111111 in two's complement)\n//\n// Step 2: Adjust sign (result is negative, y is negative)\n//\n//\tNo adjustment needed\n//\n// Final result: -1 (11111111 in two's complement)\n//\n// Note: This implementation ensures that the result always has the same sign as y,\n// which is different from the Rem operation.\nfunc (z *Int) ModE(x, y *Int) *Int {\n\tif y.IsZero() {\n\t\tpanic(divisionByZeroError)\n\t}\n\n\t// Perform T-division to get the remainder\n\tz.Rem(x, y)\n\n\t// Adjust the remainder if necessary\n\tif z.Sign() \u003e= 0 {\n\t\treturn z\n\t}\n\tif y.Sign() \u003e 0 {\n\t\treturn z.Add(z, y)\n\t}\n\n\treturn z.Sub(z, y)\n}\n\n// Sets z to the sum x + y, where z and x are uint256s and y is an int256.\n//\n// If the y is positive, it adds y.value to x. otherwise, it subtracts y.Abs() from x.\nfunc AddDelta(z, x *uint256.Uint, y *Int) {\n\tif y.Sign() \u003e= 0 {\n\t\tz.Add(x, \u0026y.value)\n\t} else {\n\t\tz.Sub(x, y.Abs())\n\t}\n}\n\n// Sets z to the sum x + y, where z and x are uint256s and y is an int256.\n//\n// This function returns true if the addition overflows, false otherwise.\nfunc AddDeltaOverflow(z, x *uint256.Uint, y *Int) bool {\n\tvar overflow bool\n\tif y.Sign() \u003e= 0 {\n\t\t_, overflow = z.AddOverflow(x, \u0026y.value)\n\t} else {\n\t\tvar absY uint256.Uint\n\t\tabsY.Sub(uint0, \u0026y.value) // absY = -y.value\n\t\t_, overflow = z.SubOverflow(x, \u0026absY)\n\t}\n\n\treturn overflow\n}\n"},{"name":"arithmetic_test.gno","body":"package int256\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/onbloc/uint256\"\n)\n\nconst (\n\t// 2^255 - 1\n\tMAX_INT256 = \"57896044618658097711785492504343953926634992332820282019728792003956564819967\"\n\t// -(2^255 - 1)\n\tMINUS_MAX_INT256 = \"-57896044618658097711785492504343953926634992332820282019728792003956564819967\"\n\n\t// 2^255 - 1\n\tMAX_UINT256         = \"115792089237316195423570985008687907853269984665640564039457584007913129639935\"\n\tMAX_UINT256_MINUS_1 = \"115792089237316195423570985008687907853269984665640564039457584007913129639934\"\n\n\tMINUS_MAX_UINT256        = \"-115792089237316195423570985008687907853269984665640564039457584007913129639935\"\n\tMINUS_MAX_UINT256_PLUS_1 = \"-115792089237316195423570985008687907853269984665640564039457584007913129639934\"\n\n\tTWO_POW_128               = \"340282366920938463463374607431768211456\"\n\tMINUS_TWO_POW_128         = \"-340282366920938463463374607431768211456\"\n\tMINUS_TWO_POW_128_MINUS_1 = \"-340282366920938463463374607431768211457\"\n\tTWO_POW_128_MINUS_1       = \"340282366920938463463374607431768211455\"\n\n\tTWO_POW_129_MINUS_1 = \"680564733841876926926749214863536422911\"\n\n\tTWO_POW_254           = \"28948022309329048855892746252171976963317496166410141009864396001978282409984\"\n\tMINUS_TWO_POW_254     = \"-28948022309329048855892746252171976963317496166410141009864396001978282409984\"\n\tHALF_MAX_INT256       = \"28948022309329048855892746252171976963317496166410141009864396001978282409983\"\n\tMINUS_HALF_MAX_INT256 = \"-28948022309329048855892746252171976963317496166410141009864396001978282409983\"\n\n\tTWO_POW_255        = \"57896044618658097711785492504343953926634992332820282019728792003956564819968\"\n\tMIN_INT256         = \"-57896044618658097711785492504343953926634992332820282019728792003956564819968\"\n\tMIN_INT256_MINUS_1 = \"-57896044618658097711785492504343953926634992332820282019728792003956564819969\"\n)\n\nfunc TestAdd(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"0\", \"1\", \"1\"},\n\t\t{\"1\", \"0\", \"1\"},\n\t\t{\"1\", \"1\", \"2\"},\n\t\t{\"1\", \"2\", \"3\"},\n\t\t// NEGATIVE\n\t\t{\"-1\", \"1\", \"0\"},\n\t\t{\"1\", \"-1\", \"0\"},\n\t\t{\"3\", \"-3\", \"0\"},\n\t\t{\"-1\", \"-1\", \"-2\"},\n\t\t{\"-1\", \"-2\", \"-3\"},\n\t\t{\"-1\", \"3\", \"2\"},\n\t\t{\"3\", \"-1\", \"2\"},\n\t\t// OVERFLOW\n\t\t{MAX_UINT256, \"1\", \"0\"},\n\t\t{MAX_INT256, \"1\", MIN_INT256},\n\t\t{MIN_INT256, \"-1\", MAX_INT256},\n\t\t{MAX_INT256, MAX_INT256, \"-2\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.Add(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Add(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestAddUint256(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"0\", \"1\", \"1\"},\n\t\t{\"1\", \"0\", \"1\"},\n\t\t{\"1\", \"1\", \"2\"},\n\t\t{\"1\", \"2\", \"3\"},\n\t\t{\"-1\", \"1\", \"0\"},\n\t\t{\"-1\", \"3\", \"2\"},\n\t\t{MINUS_MAX_UINT256_PLUS_1, MAX_UINT256, \"1\"},\n\t\t{MINUS_MAX_UINT256, MAX_UINT256_MINUS_1, \"-1\"},\n\t\t// OVERFLOW\n\t\t{MINUS_MAX_UINT256, MAX_UINT256, \"0\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := uint256.FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.AddUint256(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"AddUint256(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestAddDelta(t *testing.T) {\n\ttests := []struct {\n\t\tz, x, y, want string\n\t}{\n\t\t{\"0\", \"0\", \"0\", \"0\"},\n\t\t{\"0\", \"0\", \"1\", \"1\"},\n\t\t{\"0\", \"1\", \"0\", \"1\"},\n\t\t{\"0\", \"1\", \"1\", \"2\"},\n\t\t{\"1\", \"2\", \"3\", \"5\"},\n\t\t{\"5\", \"10\", \"-3\", \"7\"},\n\t\t// underflow\n\t\t{\"1\", \"2\", \"-3\", MAX_UINT256},\n\t}\n\n\tfor _, tc := range tests {\n\t\tz, err := uint256.FromDecimal(tc.z)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tx, err := uint256.FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := uint256.FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tAddDelta(z, x, y)\n\n\t\tif z.Neq(want) {\n\t\t\tt.Errorf(\"AddDelta(%s, %s, %s) = %v, want %v\", tc.z, tc.x, tc.y, z.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestAddDeltaOverflow(t *testing.T) {\n\ttests := []struct {\n\t\tz, x, y string\n\t\twant    bool\n\t}{\n\t\t{\"0\", \"0\", \"0\", false},\n\t\t// underflow\n\t\t{\"1\", \"2\", \"-3\", true},\n\t}\n\n\tfor _, tc := range tests {\n\t\tz, err := uint256.FromDecimal(tc.z)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tx, err := uint256.FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tresult := AddDeltaOverflow(z, x, y)\n\t\tif result != tc.want {\n\t\t\tt.Errorf(\"AddDeltaOverflow(%s, %s, %s) = %v, want %v\", tc.z, tc.x, tc.y, result, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestSub(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"1\", \"0\", \"1\"},\n\t\t{\"1\", \"1\", \"0\"},\n\t\t{\"-1\", \"1\", \"-2\"},\n\t\t{\"1\", \"-1\", \"2\"},\n\t\t{\"-1\", \"-1\", \"0\"},\n\t\t{MINUS_MAX_UINT256, MINUS_MAX_UINT256, \"0\"},\n\t\t{MINUS_MAX_UINT256, \"0\", MINUS_MAX_UINT256},\n\t\t{MAX_INT256, MIN_INT256, \"-1\"},\n\t\t{MIN_INT256, MIN_INT256, \"0\"},\n\t\t{MAX_INT256, MAX_INT256, \"0\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.Sub(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Sub(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestSubUint256(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"0\", \"1\", \"-1\"},\n\t\t{\"1\", \"0\", \"1\"},\n\t\t{\"1\", \"1\", \"0\"},\n\t\t{\"1\", \"2\", \"-1\"},\n\t\t{\"-1\", \"1\", \"-2\"},\n\t\t{\"-1\", \"3\", \"-4\"},\n\t\t// underflow\n\t\t{MINUS_MAX_UINT256, \"1\", \"0\"},\n\t\t{MINUS_MAX_UINT256, \"2\", \"-1\"},\n\t\t{MINUS_MAX_UINT256, \"3\", \"-2\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := uint256.FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.SubUint256(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"SubUint256(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestMul(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"5\", \"3\", \"15\"},\n\t\t{\"-5\", \"3\", \"-15\"},\n\t\t{\"5\", \"-3\", \"-15\"},\n\t\t{\"0\", \"3\", \"0\"},\n\t\t{\"3\", \"0\", \"0\"},\n\t\t{\"-5\", \"-3\", \"15\"},\n\t\t{MAX_UINT256, \"1\", MAX_UINT256},\n\t\t{MAX_INT256, \"2\", \"-2\"},\n\t\t{TWO_POW_254, \"2\", MIN_INT256},\n\t\t{MINUS_TWO_POW_254, \"2\", MIN_INT256},\n\t\t{MAX_INT256, \"1\", MAX_INT256},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.Mul(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Mul(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestDiv(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, expected string\n\t}{\n\t\t{\"1\", \"1\", \"1\"},\n\t\t{\"0\", \"1\", \"0\"},\n\t\t{\"-1\", \"1\", \"-1\"},\n\t\t{\"1\", \"-1\", \"-1\"},\n\t\t{\"-1\", \"-1\", \"1\"},\n\t\t{\"-6\", \"3\", \"-2\"},\n\t\t{\"10\", \"-2\", \"-5\"},\n\t\t{\"-10\", \"3\", \"-3\"},\n\t\t{\"7\", \"3\", \"2\"},\n\t\t{\"-7\", \"3\", \"-2\"},\n\t\t// the maximum value of a positive number in int256 is less than the maximum value of a uint256\n\t\t{MAX_INT256, \"2\", HALF_MAX_INT256},\n\t\t{MINUS_MAX_INT256, \"2\", MINUS_HALF_MAX_INT256},\n\t\t{MAX_INT256, \"-1\", MINUS_MAX_INT256},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.x+\"/\"+tt.y, func(t *testing.T) {\n\t\t\tx := MustFromDecimal(tt.x)\n\t\t\ty := MustFromDecimal(tt.y)\n\t\t\tresult := Zero().Div(x, y)\n\t\t\tif result.String() != tt.expected {\n\t\t\t\tt.Errorf(\"Div(%s, %s) = %s, want %s\", tt.x, tt.y, result.String(), tt.expected)\n\t\t\t}\n\t\t})\n\t}\n\n\tt.Run(\"Division by zero\", func(t *testing.T) {\n\t\tdefer func() {\n\t\t\tif r := recover(); r == nil {\n\t\t\t\tt.Errorf(\"Div(1, 0) did not panic\")\n\t\t\t}\n\t\t}()\n\t\tx := MustFromDecimal(\"1\")\n\t\ty := MustFromDecimal(\"0\")\n\t\tZero().Div(x, y)\n\t})\n}\n\nfunc TestQuo(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"0\", \"1\", \"0\"},\n\t\t{\"0\", \"-1\", \"0\"},\n\t\t{\"10\", \"1\", \"10\"},\n\t\t{\"10\", \"-1\", \"-10\"},\n\t\t{\"-10\", \"1\", \"-10\"},\n\t\t{\"-10\", \"-1\", \"10\"},\n\t\t{\"10\", \"-3\", \"-3\"},\n\t\t{\"-10\", \"3\", \"-3\"},\n\t\t{\"10\", \"3\", \"3\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.Quo(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Quo(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestRem(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"0\", \"1\", \"0\"},\n\t\t{\"0\", \"-1\", \"0\"},\n\t\t{\"10\", \"1\", \"0\"},\n\t\t{\"10\", \"-1\", \"0\"},\n\t\t{\"-10\", \"1\", \"0\"},\n\t\t{\"-10\", \"-1\", \"0\"},\n\t\t{\"10\", \"3\", \"1\"},\n\t\t{\"10\", \"-3\", \"1\"},\n\t\t{\"-10\", \"3\", \"-1\"},\n\t\t{\"-10\", \"-3\", \"-1\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.Rem(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Rem(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestMod(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"0\", \"1\", \"0\"},\n\t\t{\"0\", \"-1\", \"0\"},\n\t\t{\"10\", \"1\", \"0\"},\n\t\t{\"10\", \"-1\", \"0\"},\n\t\t{\"-10\", \"1\", \"0\"},\n\t\t{\"-10\", \"-1\", \"0\"},\n\t\t{\"10\", \"3\", \"1\"},\n\t\t{\"10\", \"-3\", \"1\"},\n\t\t{\"-10\", \"3\", \"2\"},\n\t\t{\"-10\", \"-3\", \"2\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\twant, err := FromDecimal(tc.want)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := New()\n\t\tgot.Mod(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Mod(%s, %s) = %v, want %v\", tc.x, tc.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestModeOverflow(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{MIN_INT256, \"2\", \"0\"},  // MIN_INT256 % 2 = 0\n\t\t{MAX_INT256, \"2\", \"1\"},  // MAX_INT256 % 2 = 1\n\t\t{MIN_INT256, \"-1\", \"0\"}, // MIN_INT256 % -1 = 0\n\t\t{MAX_INT256, \"-1\", \"0\"}, // MAX_INT256 % -1 = 0\n\t}\n\n\tfor _, tt := range tests {\n\t\tx := MustFromDecimal(tt.x)\n\t\ty := MustFromDecimal(tt.y)\n\t\twant := MustFromDecimal(tt.want)\n\t\tgot := New().Mod(x, y)\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Mod(%s, %s) = %v, want %v\", tt.x, tt.y, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestModPanic(t *testing.T) {\n\ttests := []struct {\n\t\tx, y string\n\t}{\n\t\t{\"10\", \"0\"},\n\t\t{\"10\", \"-0\"},\n\t\t{\"-10\", \"0\"},\n\t\t{\"-10\", \"-0\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tdefer func() {\n\t\t\tif r := recover(); r == nil {\n\t\t\t\tt.Errorf(\"Mod(%s, %s) did not panic\", tc.x, tc.y)\n\t\t\t}\n\t\t}()\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tresult := New().Mod(x, y)\n\t\tt.Errorf(\"Mod(%s, %s) = %v, want %v\", tc.x, tc.y, result.String(), \"0\")\n\t}\n}\n\nfunc TestDivE(t *testing.T) {\n\ttestCases := []struct {\n\t\tx, y int64\n\t\twant int64\n\t}{\n\t\t{8, 3, 2},\n\t\t{8, -3, -2},\n\t\t{-8, 3, -3},\n\t\t{-8, -3, 3},\n\t\t{1, 2, 0},\n\t\t{1, -2, 0},\n\t\t{-1, 2, -1},\n\t\t{-1, -2, 1},\n\t\t{0, 1, 0},\n\t\t{0, -1, 0},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tx := NewInt(tc.x)\n\t\ty := NewInt(tc.y)\n\t\twant := NewInt(tc.want)\n\t\tgot := new(Int).DivE(x, y)\n\t\tif got.Cmp(want) != 0 {\n\t\t\tt.Errorf(\"DivE(%v, %v) = %v, want %v\", tc.x, tc.y, got, want)\n\t\t}\n\t}\n}\n\nfunc TestDivEByZero(t *testing.T) {\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Errorf(\"DivE did not panic on division by zero\")\n\t\t}\n\t}()\n\n\tx := NewInt(1)\n\ty := NewInt(0)\n\tnew(Int).DivE(x, y)\n}\n\nfunc TestModEByZero(t *testing.T) {\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Errorf(\"ModE did not panic on division by zero\")\n\t\t}\n\t}()\n\n\tx := NewInt(1)\n\ty := NewInt(0)\n\tnew(Int).ModE(x, y)\n}\n\nfunc TestLargeNumbers(t *testing.T) {\n\tx, _ := new(Int).SetString(\"123456789012345678901234567890\")\n\ty, _ := new(Int).SetString(\"987654321098765432109876543210\")\n\n\t// Expected results (calculated separately)\n\texpectedQ, _ := new(Int).SetString(\"0\")\n\texpectedR, _ := new(Int).SetString(\"123456789012345678901234567890\")\n\n\tgotQ := new(Int).DivE(x, y)\n\tgotR := new(Int).ModE(x, y)\n\n\tif gotQ.Cmp(expectedQ) != 0 {\n\t\tt.Errorf(\"DivE with large numbers: got %v, want %v\", gotQ, expectedQ)\n\t}\n\n\tif gotR.Cmp(expectedR) != 0 {\n\t\tt.Errorf(\"ModE with large numbers: got %v, want %v\", gotR, expectedR)\n\t}\n}\n\nfunc TestAbs(t *testing.T) {\n\ttests := []struct {\n\t\tx, want string\n\t}{\n\t\t{\"0\", \"0\"},\n\t\t{\"1\", \"1\"},\n\t\t{\"-1\", \"1\"},\n\t\t{\"-2\", \"2\"},\n\t\t{\"-100000000000\", \"100000000000\"},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.Abs()\n\n\t\tif got.String() != tc.want {\n\t\t\tt.Errorf(\"Abs(%s) = %v, want %v\", tc.x, got.String(), tc.want)\n\t\t}\n\t}\n}\n"},{"name":"bitwise.gno","body":"package int256\n\n// Not sets z to the bitwise NOT of x and returns z.\n//\n// The bitwise NOT operation flips each bit of the operand.\nfunc (z *Int) Not(x *Int) *Int {\n\tz.value.Not(\u0026x.value)\n\treturn z\n}\n\n// And sets z to the bitwise AND of x and y and returns z.\n//\n// The bitwise AND operation results in a value that has a bit set\n// only if both corresponding bits of the operands are set.\nfunc (z *Int) And(x, y *Int) *Int {\n\tz.value.And(\u0026x.value, \u0026y.value)\n\treturn z\n}\n\n// Or sets z to the bitwise OR of x and y and returns z.\n//\n// The bitwise OR operation results in a value that has a bit set\n// if at least one of the corresponding bits of the operands is set.\nfunc (z *Int) Or(x, y *Int) *Int {\n\tz.value.Or(\u0026x.value, \u0026y.value)\n\treturn z\n}\n\n// Xor sets z to the bitwise XOR of x and y and returns z.\n//\n// The bitwise XOR operation results in a value that has a bit set\n// only if the corresponding bits of the operands are different.\nfunc (z *Int) Xor(x, y *Int) *Int {\n\tz.value.Xor(\u0026x.value, \u0026y.value)\n\treturn z\n}\n\n// Rsh sets z to the result of right-shifting x by n bits and returns z.\n//\n// Right shift operation moves all bits in the operand to the right by the specified number of positions.\n// Bits shifted out on the right are discarded, and zeros are shifted in on the left.\nfunc (z *Int) Rsh(x *Int, n uint) *Int {\n\tz.value.Rsh(\u0026x.value, n)\n\treturn z\n}\n\n// Lsh sets z to the result of left-shifting x by n bits and returns z.\n//\n// Left shift operation moves all bits in the operand to the left by the specified number of positions.\n// Bits shifted out on the left are discarded, and zeros are shifted in on the right.\nfunc (z *Int) Lsh(x *Int, n uint) *Int {\n\tz.value.Lsh(\u0026x.value, n)\n\treturn z\n}\n"},{"name":"bitwise_test.gno","body":"package int256\n\nimport (\n\t\"testing\"\n)\n\nfunc TestBitwise_And(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"5\", \"1\", \"1\"},  // 0101 \u0026 0001 = 0001\n\t\t{\"-1\", \"1\", \"1\"}, // 1111 \u0026 0001 = 0001\n\t\t{\"-5\", \"3\", \"3\"}, // 1111...1011 \u0026 0000...0011 = 0000...0011\n\t\t{MAX_UINT256, MAX_UINT256, MAX_UINT256},\n\t\t{TWO_POW_128, TWO_POW_128_MINUS_1, \"0\"}, // 2^128 \u0026 (2^128 - 1) = 0\n\t\t{TWO_POW_128, MAX_UINT256, TWO_POW_128}, // 2^128 \u0026 MAX_INT256\n\t\t{MAX_UINT256, TWO_POW_128, TWO_POW_128}, // MAX_INT256 \u0026 2^128\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, _ := FromDecimal(tc.x)\n\t\ty, _ := FromDecimal(tc.y)\n\t\twant, _ := FromDecimal(tc.want)\n\n\t\tgot := new(Int).And(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"And(%s, %s) = %s, want %s\", x.String(), y.String(), got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestBitwise_Or(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"5\", \"1\", \"5\"},   // 0101 | 0001 = 0101\n\t\t{\"-1\", \"1\", \"-1\"}, // 1111 | 0001 = 1111\n\t\t{\"-5\", \"3\", \"-5\"}, // 1111...1011 | 0000...0011 = 1111...1011\n\t\t{TWO_POW_128, TWO_POW_128_MINUS_1, TWO_POW_129_MINUS_1},\n\t\t{TWO_POW_128, MAX_UINT256, MAX_UINT256},\n\t\t{\"0\", TWO_POW_128, TWO_POW_128},         // 0 | 2^128 = 2^128\n\t\t{MAX_UINT256, TWO_POW_128, MAX_UINT256}, // MAX_INT256 | 2^128 = MAX_INT256\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, _ := FromDecimal(tc.x)\n\t\ty, _ := FromDecimal(tc.y)\n\t\twant, _ := FromDecimal(tc.want)\n\n\t\tgot := new(Int).Or(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\n\t\t\t\t\"Or(%s, %s) = %s, want %s\",\n\t\t\t\tx.String(), y.String(), got.String(), want.String(),\n\t\t\t)\n\t\t}\n\t}\n}\n\nfunc TestBitwise_Not(t *testing.T) {\n\ttests := []struct {\n\t\tx, want string\n\t}{\n\t\t{\"5\", \"-6\"},                              // 0101 -\u003e 1111...1010\n\t\t{\"-1\", \"0\"},                              // 1111...1111 -\u003e 0000...0000\n\t\t{TWO_POW_128, MINUS_TWO_POW_128_MINUS_1}, // NOT 2^128\n\t\t{TWO_POW_255, MIN_INT256_MINUS_1},        // NOT 2^255\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, _ := FromDecimal(tc.x)\n\t\twant, _ := FromDecimal(tc.want)\n\n\t\tgot := new(Int).Not(x)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Not(%s) = %s, want %s\", x.String(), got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestBitwise_Xor(t *testing.T) {\n\ttests := []struct {\n\t\tx, y, want string\n\t}{\n\t\t{\"5\", \"1\", \"4\"},                 // 0101 ^ 0001 = 0100\n\t\t{\"-1\", \"1\", \"-2\"},               // 1111...1111 ^ 0000...0001 = 1111...1110\n\t\t{\"-5\", \"3\", \"-8\"},               // 1111...1011 ^ 0000...0011 = 1111...1000\n\t\t{TWO_POW_128, TWO_POW_128, \"0\"}, // 2^128 ^ 2^128 = 0\n\t\t{MAX_UINT256, TWO_POW_128, MINUS_TWO_POW_128_MINUS_1}, // MAX_INT256 ^ 2^128\n\t\t{TWO_POW_255, MAX_UINT256, MIN_INT256_MINUS_1},        // 2^255 ^ MAX_INT256\n\t}\n\n\tfor _, tt := range tests {\n\t\tx, _ := FromDecimal(tt.x)\n\t\ty, _ := FromDecimal(tt.y)\n\t\twant, _ := FromDecimal(tt.want)\n\n\t\tgot := new(Int).Xor(x, y)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Xor(%s, %s) = %s, want %s\", x.String(), y.String(), got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestBitwise_Rsh(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\tn    uint\n\t\twant string\n\t}{\n\t\t{\"5\", 1, \"2\"},  // 0101 \u003e\u003e 1 = 0010\n\t\t{\"42\", 3, \"5\"}, // 00101010 \u003e\u003e 3 = 00000101\n\t\t{TWO_POW_128, 128, \"1\"},\n\t\t{MAX_UINT256, 255, \"1\"},\n\t\t{TWO_POW_255, 254, \"2\"},\n\t\t{MINUS_TWO_POW_128, 128, TWO_POW_128_MINUS_1},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx, _ := FromDecimal(tt.x)\n\t\twant, _ := FromDecimal(tt.want)\n\n\t\tgot := new(Int).Rsh(x, tt.n)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Rsh(%s, %d) = %s, want %s\", x.String(), tt.n, got.String(), want.String())\n\t\t}\n\t}\n}\n\nfunc TestBitwise_Lsh(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\tn    uint\n\t\twant string\n\t}{\n\t\t{\"5\", 2, \"20\"},          // 0101 \u003c\u003c 2 = 10100\n\t\t{\"42\", 5, \"1344\"},       // 00101010 \u003c\u003c 5 = 10101000000\n\t\t{\"1\", 128, TWO_POW_128}, // 1 \u003c\u003c 128 = 2^128\n\t\t{\"2\", 254, TWO_POW_255},\n\t\t{\"1\", 255, MIN_INT256}, // 1 \u003c\u003c 255 = MIN_INT256 (overflow)\n\t}\n\n\tfor _, tt := range tests {\n\t\tx, _ := FromDecimal(tt.x)\n\t\twant, _ := FromDecimal(tt.want)\n\n\t\tgot := new(Int).Lsh(x, tt.n)\n\n\t\tif got.Neq(want) {\n\t\t\tt.Errorf(\"Lsh(%s, %d) = %s, want %s\", x.String(), tt.n, got.String(), want.String())\n\t\t}\n\t}\n}\n"},{"name":"cmp.gno","body":"package int256\n\nfunc (z *Int) Eq(x *Int) bool {\n\treturn z.value.Eq(\u0026x.value)\n}\n\nfunc (z *Int) Neq(x *Int) bool {\n\treturn !z.Eq(x)\n}\n\n// Cmp compares z and x and returns:\n//\n//   - 1 if z \u003e x\n//   - 0 if z == x\n//   - -1 if z \u003c x\nfunc (z *Int) Cmp(x *Int) int {\n\tzSign, xSign := z.Sign(), x.Sign()\n\n\tif zSign == xSign {\n\t\treturn z.value.Cmp(\u0026x.value)\n\t}\n\n\tif zSign == 0 {\n\t\treturn -xSign\n\t}\n\n\treturn zSign\n}\n\n// IsZero returns true if z == 0\nfunc (z *Int) IsZero() bool {\n\treturn z.value.IsZero()\n}\n\n// IsNeg returns true if z \u003c 0\nfunc (z *Int) IsNeg() bool {\n\treturn z.Sign() \u003c 0\n}\n\nfunc (z *Int) Lt(x *Int) bool {\n\treturn z.Cmp(x) \u003c 0\n}\n\nfunc (z *Int) Gt(x *Int) bool {\n\treturn z.Cmp(x) \u003e 0\n}\n\nfunc (z *Int) Le(x *Int) bool {\n\treturn z.Cmp(x) \u003c= 0\n}\n\nfunc (z *Int) Ge(x *Int) bool {\n\treturn z.Cmp(x) \u003e= 0\n}\n\n// Clone creates a new Int identical to z\nfunc (z *Int) Clone() *Int {\n\treturn New().FromUint256(\u0026z.value)\n}\n"},{"name":"cmp_test.gno","body":"package int256\n\nimport \"testing\"\n\nfunc TestEq(t *testing.T) {\n\ttests := []struct {\n\t\tx, y string\n\t\twant bool\n\t}{\n\t\t{\"0\", \"0\", true},\n\t\t{\"0\", \"1\", false},\n\t\t{\"1\", \"0\", false},\n\t\t{\"-1\", \"0\", false},\n\t\t{\"0\", \"-1\", false},\n\t\t{\"1\", \"1\", true},\n\t\t{\"-1\", \"-1\", true},\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"-115792089237316195423570985008687907853269984665640564039457584007913129639935\", false},\n\t\t{\"-115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"-115792089237316195423570985008687907853269984665640564039457584007913129639935\", true},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.Eq(y)\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"Eq(%s, %s) = %v, want %v\", tc.x, tc.y, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestNeq(t *testing.T) {\n\ttests := []struct {\n\t\tx, y string\n\t\twant bool\n\t}{\n\t\t{\"0\", \"0\", false},\n\t\t{\"0\", \"1\", true},\n\t\t{\"1\", \"0\", true},\n\t\t{\"-1\", \"0\", true},\n\t\t{\"0\", \"-1\", true},\n\t\t{\"1\", \"1\", false},\n\t\t{\"-1\", \"-1\", false},\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"-115792089237316195423570985008687907853269984665640564039457584007913129639935\", true},\n\t\t{\"-115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"-115792089237316195423570985008687907853269984665640564039457584007913129639935\", false},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.Neq(y)\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"Neq(%s, %s) = %v, want %v\", tc.x, tc.y, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestCmp(t *testing.T) {\n\ttests := []struct {\n\t\tx, y string\n\t\twant int\n\t}{\n\t\t{\"0\", \"0\", 0},\n\t\t{\"0\", \"1\", -1},\n\t\t{\"1\", \"0\", 1},\n\t\t{\"-1\", \"0\", -1},\n\t\t{\"0\", \"-1\", 1},\n\t\t{\"1\", \"1\", 0},\n\t\t{\"115792089237316195423570985008687907853269984665640564039457584007913129639935\", \"-115792089237316195423570985008687907853269984665640564039457584007913129639935\", -1},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.Cmp(y)\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"Cmp(%s, %s) = %v, want %v\", tc.x, tc.y, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestIsZero(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant bool\n\t}{\n\t\t{\"0\", true},\n\t\t{\"-0\", true},\n\t\t{\"1\", false},\n\t\t{\"-1\", false},\n\t\t{\"10\", false},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.IsZero()\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"IsZero(%s) = %v, want %v\", tc.x, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestIsNeg(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant bool\n\t}{\n\t\t{\"0\", false},\n\t\t{\"-0\", false},\n\t\t{\"1\", false},\n\t\t{\"-1\", true},\n\t\t{\"10\", false},\n\t\t{\"-10\", true},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.IsNeg()\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"IsNeg(%s) = %v, want %v\", tc.x, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestLt(t *testing.T) {\n\ttests := []struct {\n\t\tx, y string\n\t\twant bool\n\t}{\n\t\t{\"0\", \"0\", false},\n\t\t{\"0\", \"1\", true},\n\t\t{\"1\", \"0\", false},\n\t\t{\"-1\", \"0\", true},\n\t\t{\"0\", \"-1\", false},\n\t\t{\"1\", \"1\", false},\n\t\t{\"-1\", \"-1\", false},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.Lt(y)\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"Lt(%s, %s) = %v, want %v\", tc.x, tc.y, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestGt(t *testing.T) {\n\ttests := []struct {\n\t\tx, y string\n\t\twant bool\n\t}{\n\t\t{\"0\", \"0\", false},\n\t\t{\"0\", \"1\", false},\n\t\t{\"1\", \"0\", true},\n\t\t{\"-1\", \"0\", false},\n\t\t{\"0\", \"-1\", true},\n\t\t{\"1\", \"1\", false},\n\t\t{\"-1\", \"-1\", false},\n\t}\n\n\tfor _, tc := range tests {\n\t\tx, err := FromDecimal(tc.x)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty, err := FromDecimal(tc.y)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tgot := x.Gt(y)\n\t\tif got != tc.want {\n\t\t\tt.Errorf(\"Gt(%s, %s) = %v, want %v\", tc.x, tc.y, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestClone(t *testing.T) {\n\ttests := []string{\n\t\t\"0\",\n\t\t\"-0\",\n\t\t\"1\",\n\t\t\"-1\",\n\t\t\"10\",\n\t\t\"-10\",\n\t\t\"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\t\t\"-115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\t}\n\n\tfor _, xStr := range tests {\n\t\tx, err := FromDecimal(xStr)\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\ty := x.Clone()\n\n\t\tif x.Neq(y) {\n\t\t\tt.Errorf(\"cloned value is not equal to original value\")\n\t\t}\n\t}\n}\n"},{"name":"conversion.gno","body":"package int256\n\nimport (\n\t\"math\"\n\n\t\"gno.land/p/onbloc/uint256\"\n)\n\n// SetInt64 sets the Int to the value of the provided int64.\n//\n// This method allows for easy conversion from standard Go integer types\n// to Int, correctly handling both positive and negative values.\nfunc (z *Int) SetInt64(v int64) *Int {\n\tif v \u003e= 0 {\n\t\tz.value.SetUint64(uint64(v))\n\t} else {\n\t\tz.value.SetUint64(uint64(-v)).Neg(\u0026z.value)\n\t}\n\treturn z\n}\n\n// SetUint64 sets the Int to the value of the provided uint64.\nfunc (z *Int) SetUint64(v uint64) *Int {\n\tz.value.SetUint64(v)\n\treturn z\n}\n\n// Uint64 returns the lower 64-bits of z\nfunc (z *Int) Uint64() uint64 {\n\tif z.Sign() \u003c 0 {\n\t\tpanic(\"cannot convert negative int256 to uint64\")\n\t}\n\tif z.value.Gt(uint256.NewUint(0).SetUint64(math.MaxUint64)) {\n\t\tpanic(\"overflow: int256 does not fit in uint64 type\")\n\t}\n\treturn z.value.Uint64()\n}\n\n// Int64 returns the lower 64-bits of z\nfunc (z *Int) Int64() int64 {\n\tif z.Sign() \u003e= 0 {\n\t\tif z.value.BitLen() \u003e 64 {\n\t\t\tpanic(\"overflow: int256 does not fit in int64 type\")\n\t\t}\n\t\treturn int64(z.value.Uint64())\n\t}\n\tvar temp uint256.Uint\n\ttemp.Sub(uint256.NewUint(0), \u0026z.value) // temp = -z.value\n\tif temp.BitLen() \u003e 64 {\n\t\tpanic(\"overflow: int256 does not fit in int64 type\")\n\t}\n\treturn -int64(temp.Uint64())\n}\n\n// Neg sets z to -x and returns z.)\nfunc (z *Int) Neg(x *Int) *Int {\n\tif x.IsZero() {\n\t\tz.value.Clear()\n\t} else {\n\t\tz.value.Neg(\u0026x.value)\n\t}\n\treturn z\n}\n\n// Set sets z to x and returns z.\nfunc (z *Int) Set(x *Int) *Int {\n\tz.value.Set(\u0026x.value)\n\treturn z\n}\n\n// SetFromUint256 converts a uint256.Uint to Int and sets the value to z.\nfunc (z *Int) SetUint256(x *uint256.Uint) *Int {\n\tz.value.Set(x)\n\treturn z\n}\n\n// ToString returns a string representation of z in base 10.\n// The string is prefixed with a minus sign if z is negative.\nfunc (z *Int) String() string {\n\tif z.value.IsZero() {\n\t\treturn \"0\"\n\t}\n\tsign := z.Sign()\n\tvar temp uint256.Uint\n\tif sign \u003e= 0 {\n\t\ttemp.Set(\u0026z.value)\n\t} else {\n\t\t// temp = -z.value\n\t\ttemp.Sub(uint256.NewUint(0), \u0026z.value)\n\t}\n\ts := temp.Dec()\n\tif sign \u003c 0 {\n\t\treturn \"-\" + s\n\t}\n\treturn s\n}\n\n// NilToZero returns the Int if it's not nil, or a new zero-valued Int otherwise.\n//\n// This method is useful for safely handling potentially nil Int pointers,\n// ensuring that operations always have a valid Int to work with.\nfunc (z *Int) NilToZero() *Int {\n\tif z == nil {\n\t\treturn Zero()\n\t}\n\treturn z\n}\n"},{"name":"conversion_test.gno","body":"package int256\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/onbloc/uint256\"\n)\n\nfunc TestSetInt64(t *testing.T) {\n\ttests := []struct {\n\t\tv      int64\n\t\texpect int\n\t}{\n\t\t{0, 0},\n\t\t{1, 1},\n\t\t{-1, -1},\n\t\t{9223372036854775807, 1},   // overflow (max int64)\n\t\t{-9223372036854775808, -1}, // underflow (min int64)\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := New().SetInt64(tt.v)\n\t\tif z.Sign() != tt.expect {\n\t\t\tt.Errorf(\"SetInt64(%d) = %d, want %d\", tt.v, z.Sign(), tt.expect)\n\t\t}\n\t}\n}\n\nfunc TestUint64(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant uint64\n\t}{\n\t\t{\"0\", 0},\n\t\t{\"1\", 1},\n\t\t{\"9223372036854775807\", 9223372036854775807},\n\t\t{\"9223372036854775808\", 9223372036854775808},\n\t\t{\"18446744073709551615\", 18446744073709551615},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := MustFromDecimal(tt.x)\n\n\t\tgot := z.Uint64()\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"Uint64(%s) = %d, want %d\", tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestUint64_Panic(t *testing.T) {\n\ttests := []struct {\n\t\tx string\n\t}{\n\t\t{\"-1\"},\n\t\t{\"18446744073709551616\"},\n\t\t{\"18446744073709551617\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tdefer func() {\n\t\t\tif r := recover(); r == nil {\n\t\t\t\tt.Errorf(\"Uint64(%s) did not panic\", tt.x)\n\t\t\t}\n\t\t}()\n\n\t\tz := MustFromDecimal(tt.x)\n\t\tz.Uint64()\n\t}\n}\n\nfunc TestInt64(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant int64\n\t}{\n\t\t{\"0\", 0},\n\t\t{\"1\", 1},\n\t\t{\"9223372036854775807\", 9223372036854775807},\n\t\t{\"-1\", -1},\n\t\t{\"-9223372036854775808\", -9223372036854775808},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := MustFromDecimal(tt.x)\n\n\t\tgot := z.Int64()\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"Uint64(%s) = %d, want %d\", tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestInt64_Panic(t *testing.T) {\n\ttests := []struct {\n\t\tx string\n\t}{\n\t\t{\"18446744073709551616\"},\n\t\t{\"18446744073709551617\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tdefer func() {\n\t\t\tif r := recover(); r == nil {\n\t\t\t\tt.Errorf(\"Int64(%s) did not panic\", tt.x)\n\t\t\t}\n\t\t}()\n\n\t\tz := MustFromDecimal(tt.x)\n\t\tz.Int64()\n\t}\n}\n\nfunc TestNeg(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant string\n\t}{\n\t\t{\"0\", \"0\"},\n\t\t{\"1\", \"-1\"},\n\t\t{\"-1\", \"1\"},\n\t\t{\"9223372036854775807\", \"-9223372036854775807\"},\n\t\t{\"-18446744073709551615\", \"18446744073709551615\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := MustFromDecimal(tt.x)\n\t\tz.Neg(z)\n\n\t\tgot := z.String()\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"Neg(%s) = %s, want %s\", tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestSet(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant string\n\t}{\n\t\t{\"0\", \"0\"},\n\t\t{\"1\", \"1\"},\n\t\t{\"-1\", \"-1\"},\n\t\t{\"9223372036854775807\", \"9223372036854775807\"},\n\t\t{\"-18446744073709551615\", \"-18446744073709551615\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := MustFromDecimal(tt.x)\n\t\tz.Set(z)\n\n\t\tgot := z.String()\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"Set(%s) = %s, want %s\", tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc TestSetUint256(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant string\n\t}{\n\t\t{\"0\", \"0\"},\n\t\t{\"1\", \"1\"},\n\t\t{\"9223372036854775807\", \"9223372036854775807\"},\n\t\t{\"18446744073709551615\", \"18446744073709551615\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tgot := New()\n\n\t\tz := uint256.MustFromDecimal(tt.x)\n\t\tgot.SetUint256(z)\n\n\t\tif got.String() != tt.want {\n\t\t\tt.Errorf(\"SetUint256(%s) = %s, want %s\", tt.x, got.String(), tt.want)\n\t\t}\n\t}\n}\n\nfunc TestString(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected string\n\t}{\n\t\t{\"0\", \"0\"},\n\t\t{\"1\", \"1\"},\n\t\t{\"-1\", \"-1\"},\n\t\t{\"123456789\", \"123456789\"},\n\t\t{\"-123456789\", \"-123456789\"},\n\t\t{\"18446744073709551615\", \"18446744073709551615\"}, // max uint64\n\t\t{\"-18446744073709551615\", \"-18446744073709551615\"},\n\t\t{TWO_POW_128_MINUS_1, TWO_POW_128_MINUS_1},\n\t\t{MINUS_TWO_POW_128, MINUS_TWO_POW_128},\n\t\t{MIN_INT256, MIN_INT256},\n\t\t{MAX_INT256, MAX_INT256},\n\t}\n\n\tfor _, tt := range tests {\n\t\tx, err := FromDecimal(tt.input)\n\t\tif err != nil {\n\t\t\tt.Errorf(\"Failed to parse input (%s): %v\", tt.input, err)\n\t\t\tcontinue\n\t\t}\n\n\t\toutput := x.String()\n\n\t\tif output != tt.expected {\n\t\t\tt.Errorf(\"String(%s) = %s, want %s\", tt.input, output, tt.expected)\n\t\t}\n\t}\n}\n\nfunc TestNilToZero(t *testing.T) {\n\tz := New().NilToZero()\n\tif z.Sign() != 0 {\n\t\tt.Errorf(\"NilToZero() = %d, want %d\", z.Sign(), 0)\n\t}\n}\n"},{"name":"doc.gno","body":"// The int256 package provides a 256-bit signed interger type for gno,\n// supporting arithmetic operations and bitwise manipulation.\n//\n// It designed for applications that require high-precision arithmetic\n// beyond the standard 64-bit range.\n//\n// ## Features\n//\n//   - 256-bit Signed Integers: Support for large integer ranging from -2^255 to 2^255-1.\n//   - Two's Complement Representation: Efficient storage and computation using two's complement.\n//   - Arithmetic Operations: Add, Sub, Mul, Div, Mod, Inc, Dec, etc.\n//   - Bitwise Operations: And, Or, Xor, Not, etc.\n//   - Comparison Operations: Cmp, Eq, Lt, Gt, etc.\n//   - Conversion Functions: Int to Uint, Uint to Int, etc.\n//   - String Parsing and Formatting: Convert to and from decimal string representation.\n//\n// ## Notes\n//\n//   - Some methods may panic when encountering invalid inputs or overflows.\n//   - The `int256.Int` type can interact with `uint256.Uint` from the `p/demo/uint256` package.\n//   - Unlike `math/big.Int`, the `int256.Int` type has fixed size (256-bit) and does not support\n//     arbitrary precision arithmetic.\n//\n// # Division and modulus operations\n//\n// This package provides three different division and modulus operations:\n//\n//   - Div and Rem: Truncated division (T-division)\n//   - Quo and Mod: Floored division (F-division)\n//   - DivE and ModE: Euclidean division (E-division)\n//\n// Truncated division (Div, Rem) is the most common implementation in modern processors\n// and programming languages. It rounds quotients towards zero and the remainder\n// always has the same sign as the dividend.\n//\n// Floored division (Quo, Mod) always rounds quotients towards negative infinity.\n// This ensures that the modulus is always non-negative for a positive divisor,\n// which can be useful in certain algorithms.\n//\n// Euclidean division (DivE, ModE) ensures that the remainder is always non-negative,\n// regardless of the signs of the dividend and divisor. This has several mathematical\n// advantages:\n//\n//  1. It satisfies the unique division with remainder theorem.\n//  2. It preserves division and modulus properties for negative divisors.\n//  3. It allows for optimizations in divisions by powers of two.\n//\n// [+] Currently, ModE and Mod are shared the same implementation.\n//\n// ## Performance considerations:\n//\n//   - For most operations, the performance difference between these division types is negligible.\n//   - Euclidean division may require an extra comparison and potentially an addition,\n//     which could impact performance in extremely performance-critical scenarios.\n//   - For divisions by powers of two, Euclidean division can be optimized to use\n//     bitwise operations, potentially offering better performance.\n//\n// ## Usage guidelines:\n//\n//   - Use Div and Rem for general-purpose division that matches most common expectations.\n//   - Use Quo and Mod when you need a non-negative remainder for positive divisors,\n//     or when implementing algorithms that assume floored division.\n//   - Use DivE and ModE when you need the mathematical properties of Euclidean division,\n//     or when working with algorithms that specifically require it.\n//\n// Note: When working with negative numbers, be aware of the differences in behavior\n// between these division types, especially at the boundaries of integer ranges.\n//\n// ## References\n//\n// Daan Leijen, “Division and Modulus for Computer Scientists”:\n// https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf\npackage int256\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/onbloc/int256\"\ngno = \"0.9\"\n"},{"name":"int256.gno","body":"package int256\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/onbloc/uint256\"\n)\n\nvar (\n\tint1  = NewInt(1)\n\tuint0 = uint256.NewUint(0)\n\tuint1 = uint256.NewUint(1)\n)\n\ntype Int struct {\n\tvalue uint256.Uint\n}\n\n// New creates and returns a new Int initialized to zero.\nfunc New() *Int {\n\treturn \u0026Int{}\n}\n\n// NewInt allocates and returns a new Int set to the value of the provided int64.\nfunc NewInt(x int64) *Int {\n\treturn New().SetInt64(x)\n}\n\n// Zero returns a new Int initialized to 0.\n//\n// This function is useful for creating a starting point for calculations or\n// when an explicit zero value is needed.\nfunc Zero() *Int { return \u0026Int{} }\n\n// One returns a new Int initialized to one.\n//\n// This function is convenient for operations that require a unit value,\n// such as incrementing or serving as an identity element in multiplication.\nfunc One() *Int {\n\treturn \u0026Int{\n\t\tvalue: *uint256.NewUint(1),\n\t}\n}\n\n// Sign determines the sign of the Int.\n//\n// It returns -1 for negative numbers, 0 for zero, and +1 for positive numbers.\nfunc (z *Int) Sign() int {\n\tif z == nil || z.IsZero() {\n\t\treturn 0\n\t}\n\t// Right shift the value by 255 bits to check the sign bit.\n\t// In two's complement representation, the most significant bit (MSB) is the sign bit.\n\t// If the MSB is 0, the number is positive; if it is 1, the number is negative.\n\t//\n\t// Example:\n\t// Original value:  1 0 1 0 ... 0 1  (256 bits)\n\t// After Rsh 255:   0 0 0 0 ... 0 1  (1 bit)\n\t//\n\t// This approach is highly efficient as it avoids the need for comparisons\n\t// or arithmetic operations on the full 256-bit number. Instead it reduces\n\t// the problem to checking a single bit.\n\t//\n\t// Additionally, this method will work correctly for all values,\n\t// including the minimum possible negative number (which in two's complement\n\t// doesn't have a positive counterpart in the same bit range).\n\tvar temp uint256.Uint\n\tif temp.Rsh(\u0026z.value, 255).IsZero() {\n\t\treturn 1\n\t}\n\treturn -1\n}\n\n// FromDecimal creates a new Int from a decimal string representation.\n// It handles both positive and negative values.\n//\n// This function is useful for parsing user input or reading numeric data\n// from text-based formats.\nfunc FromDecimal(s string) (*Int, error) {\n\treturn New().SetString(s)\n}\n\n// MustFromDecimal is similar to FromDecimal but panics if the input string\n// is not a valid decimal representation.\nfunc MustFromDecimal(s string) *Int {\n\tz, err := FromDecimal(s)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn z\n}\n\n// SetString sets the Int to the value represented by the input string.\n// This method supports decimal string representations of integers and handles\n// both positive and negative values.\nfunc (z *Int) SetString(s string) (*Int, error) {\n\tif len(s) == 0 {\n\t\treturn nil, errors.New(\"cannot set int256 from empty string\")\n\t}\n\n\t// Check for negative sign\n\tneg := s[0] == '-'\n\tif neg || s[0] == '+' {\n\t\ts = s[1:]\n\t}\n\n\t// Convert string to uint256\n\ttemp, err := uint256.FromDecimal(s)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\t// If negative, negate the uint256 value\n\tif neg {\n\t\ttemp.Neg(temp)\n\t}\n\n\tz.value.Set(temp)\n\treturn z, nil\n}\n\n// FromUint256 sets the Int to the value of the provided Uint256.\n//\n// This method allows for conversion from unsigned 256-bit integers\n// to signed integers.\nfunc (z *Int) FromUint256(v *uint256.Uint) *Int {\n\tz.value.Set(v)\n\treturn z\n}\n"},{"name":"int256_test.gno","body":"package int256\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/onbloc/uint256\"\n)\n\nfunc TestInitializers(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tfn       func() *Int\n\t\twantSign int\n\t\twantStr  string\n\t}{\n\t\t{\"Zero\", Zero, 0, \"0\"},\n\t\t{\"New\", New, 0, \"0\"},\n\t\t{\"One\", One, 1, \"1\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tz := tt.fn()\n\t\t\tif z.Sign() != tt.wantSign {\n\t\t\t\tt.Errorf(\"%s() = %d, want %d\", tt.name, z.Sign(), tt.wantSign)\n\t\t\t}\n\t\t\tif z.String() != tt.wantStr {\n\t\t\t\tt.Errorf(\"%s() = %s, want %s\", tt.name, z.String(), tt.wantStr)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNewInt(t *testing.T) {\n\ttests := []struct {\n\t\tinput    int64\n\t\texpected int\n\t}{\n\t\t{0, 0},\n\t\t{1, 1},\n\t\t{-1, -1},\n\t\t{9223372036854775807, 1},   // max int64\n\t\t{-9223372036854775808, -1}, // min int64\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := NewInt(tt.input)\n\t\tif z.Sign() != tt.expected {\n\t\t\tt.Errorf(\"NewInt(%d) = %d, want %d\", tt.input, z.Sign(), tt.expected)\n\t\t}\n\t}\n}\n\nfunc TestFromDecimal(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected int\n\t\tisError  bool\n\t}{\n\t\t{\"0\", 0, false},\n\t\t{\"1\", 1, false},\n\t\t{\"-1\", -1, false},\n\t\t{\"123456789\", 1, false},\n\t\t{\"-123456789\", -1, false},\n\t\t{\"invalid\", 0, true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz, err := FromDecimal(tt.input)\n\t\tif tt.isError {\n\t\t\tif err == nil {\n\t\t\t\tt.Errorf(\"FromDecimal(%s) expected error, but got nil\", tt.input)\n\t\t\t}\n\t\t} else {\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"FromDecimal(%s) unexpected error: %v\", tt.input, err)\n\t\t\t} else if z.Sign() != tt.expected {\n\t\t\t\tt.Errorf(\"FromDecimal(%s) sign is incorrect. Expected: %d, Actual: %d\", tt.input, tt.expected, z.Sign())\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc TestMustFromDecimal(t *testing.T) {\n\ttests := []struct {\n\t\tinput       string\n\t\texpected    int\n\t\tshouldPanic bool\n\t}{\n\t\t{\"0\", 0, false},\n\t\t{\"1\", 1, false},\n\t\t{\"-1\", -1, false},\n\t\t{\"123\", 1, false},\n\t\t{\"invalid\", 0, true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tif tt.shouldPanic {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r == nil {\n\t\t\t\t\tt.Errorf(\"MustFromDecimal(%q) expected panic, but got nil\", tt.input)\n\t\t\t\t}\n\t\t\t}()\n\t\t}\n\n\t\tz := MustFromDecimal(tt.input)\n\t\tif !tt.shouldPanic \u0026\u0026 z.Sign() != tt.expected {\n\t\t\tt.Errorf(\"MustFromDecimal(%q) sign is incorrect. Expected: %d, Actual: %d\", tt.input, tt.expected, z.Sign())\n\t\t}\n\t}\n}\n\nfunc TestSetUint64(t *testing.T) {\n\ttests := []uint64{\n\t\t0,\n\t\t1,\n\t\t18446744073709551615, // max uint64\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := New().SetUint64(tt)\n\t\tif z.Sign() \u003c 0 {\n\t\t\tt.Errorf(\"SetUint64(%d) result is negative\", tt)\n\t\t}\n\t\tif tt == 0 \u0026\u0026 z.Sign() != 0 {\n\t\t\tt.Errorf(\"SetUint64(0) result is not zero\")\n\t\t}\n\t\tif tt \u003e 0 \u0026\u0026 z.Sign() != 1 {\n\t\t\tt.Errorf(\"SetUint64(%d) result is not positive\", tt)\n\t\t}\n\t}\n}\n\nfunc TestFromUint256(t *testing.T) {\n\ttests := []struct {\n\t\tinput    *uint256.Uint\n\t\texpected int\n\t}{\n\t\t{uint256.NewUint(0), 0},\n\t\t{uint256.NewUint(1), 1},\n\t\t{uint256.NewUint(18446744073709551615), 1},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := New().FromUint256(tt.input)\n\t\tif z.Sign() != tt.expected {\n\t\t\tt.Errorf(\"FromUint256(%v) = %d, want %d\", tt.input, z.Sign(), tt.expected)\n\t\t}\n\t}\n}\n\nfunc TestSign(t *testing.T) {\n\ttests := []struct {\n\t\tx    string\n\t\twant int\n\t}{\n\t\t{\"0\", 0},\n\t\t{\"-0\", 0},\n\t\t{\"+0\", 0},\n\t\t{\"1\", 1},\n\t\t{\"-1\", -1},\n\t\t{\"9223372036854775807\", 1},\n\t\t{\"-9223372036854775808\", -1},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz := MustFromDecimal(tt.x)\n\t\tgot := z.Sign()\n\t\tif got != tt.want {\n\t\t\tt.Errorf(\"Sign(%s) = %d, want %d\", tt.x, got, tt.want)\n\t\t}\n\t}\n}\n\nfunc BenchmarkSign(b *testing.B) {\n\tz := New()\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tz.SetUint64(uint64(i))\n\t\tz.Sign()\n\t}\n}\n\nfunc TestSetAndToString(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected int\n\t\tisError  bool\n\t}{\n\t\t{\"0\", 0, false},\n\t\t{\"1\", 1, false},\n\t\t{\"-1\", -1, false},\n\t\t{\"123456789\", 1, false},\n\t\t{\"-123456789\", -1, false},\n\t\t{\"invalid\", 0, true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tz, err := New().SetString(tt.input)\n\t\tif tt.isError {\n\t\t\tif err == nil {\n\t\t\t\tt.Errorf(\"SetString(%s) expected error, but got nil\", tt.input)\n\t\t\t}\n\t\t} else {\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"SetString(%s) unexpected error: %v\", tt.input, err)\n\t\t\t} else if z.Sign() != tt.expected {\n\t\t\t\tt.Errorf(\"SetString(%s) sign is incorrect. Expected: %d, Actual: %d\", tt.input, tt.expected, z.Sign())\n\t\t\t} else if z.String() != tt.input {\n\t\t\t\tt.Errorf(\"SetString(%s) string representation is incorrect. Expected: %s, Actual: %s\", tt.input, tt.input, z.String())\n\t\t\t}\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"YHW5jgFSe8rUvY8K1Qo90mcW2/gVP45TyE5OOBVsCe/IeaKM9H8Vv/k1p9xddcHIxgJC3Jjt78CLKzpciHX6BQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"json","path":"gno.land/p/onbloc/json","files":[{"name":"LICENSE","body":"# MIT License\n\nCopyright (c) 2019 Pyzhov Stepan\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"},{"name":"README.md","body":"# JSON Parser\n\nThe JSON parser is a package that provides functionality for parsing and processing JSON strings. This package accepts JSON strings as byte slices.\n\nCurrently, gno does not [support the `reflect` package](https://docs.gno.land/resources/effective-gno#reflection-is-never-clear), so it cannot retrieve type information at runtime. Therefore, it is designed to infer and handle type information when parsing JSON strings using a state machine approach.\n\nAfter passing through the state machine, JSON strings are represented as the `Node` type. The `Node` type represents nodes for JSON data, including various types such as `ObjectNode`, `ArrayNode`, `StringNode`, `NumberNode`, `BoolNode`, and `NullNode`.\n\nThis package provides methods for manipulating, searching, and extracting the Node type.\n\n## State Machine\n\nTo parse JSON strings, a [finite state machine](https://en.wikipedia.org/wiki/Finite-state_machine) approach is used. The state machine transitions to the next state based on the current state and the input character while parsing the JSON string. Through this method, type information can be inferred and processed without reflect, and the amount of parser code can be significantly reduced.\n\nThe image below shows the state transitions of the state machine according to the states and input characters.\n\n```mermaid\nstateDiagram-v2\n    [*] --\u003e __: Start\n    __ --\u003e ST: String\n    __ --\u003e MI: Number\n    __ --\u003e ZE: Zero\n    __ --\u003e IN: Integer\n    __ --\u003e T1: Boolean (true)\n    __ --\u003e F1: Boolean (false)\n    __ --\u003e N1: Null\n    __ --\u003e ec: Empty Object End\n    __ --\u003e cc: Object End\n    __ --\u003e bc: Array End\n    __ --\u003e co: Object Begin\n    __ --\u003e bo: Array Begin\n    __ --\u003e cm: Comma\n    __ --\u003e cl: Colon\n    __ --\u003e OK: Success/End\n    ST --\u003e OK: String Complete\n    MI --\u003e OK: Number Complete\n    ZE --\u003e OK: Zero Complete\n    IN --\u003e OK: Integer Complete\n    T1 --\u003e OK: True Complete\n    F1 --\u003e OK: False Complete\n    N1 --\u003e OK: Null Complete\n    ec --\u003e OK: Empty Object Complete\n    cc --\u003e OK: Object Complete\n    bc --\u003e OK: Array Complete\n    co --\u003e OB: Inside Object\n    bo --\u003e AR: Inside Array\n    cm --\u003e KE: Expecting New Key\n    cm --\u003e VA: Expecting New Value\n    cl --\u003e VA: Expecting Value\n    OB --\u003e ST: String in Object (Key)\n    OB --\u003e ec: Empty Object\n    OB --\u003e cc: End Object\n    AR --\u003e ST: String in Array\n    AR --\u003e bc: End Array\n    KE --\u003e ST: String as Key\n    VA --\u003e ST: String as Value\n    VA --\u003e MI: Number as Value\n    VA --\u003e T1: True as Value\n    VA --\u003e F1: False as Value\n    VA --\u003e N1: Null as Value\n    OK --\u003e [*]: End\n```\n\n## Examples\n\nThis package provides parsing functionality along with encoding and decoding functionality. The following examples demonstrate how to use this package.\n\n### Decoding\n\nDecoding (or Unmarshaling) is the functionality that converts an input byte slice JSON string into a `Node` type.\n\nThe converted `Node` type allows you to modify the JSON data or search and extract data that meets specific conditions.\n\n```go\npackage main\n\nimport (\n    \"gno.land/p/demo/json\"\n    \"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc main() {\n    node, err := json.Unmarshal([]byte(`{\"foo\": \"var\"}`))\n    if err != nil {\n        ufmt.Errorf(\"error: %v\", err)\n    }\n\n    ufmt.Sprintf(\"node: %v\", node)\n}\n```\n\n### Encoding\n\nEncoding (or Marshaling) is the functionality that converts JSON data represented as a Node type into a byte slice JSON string.\n\n\u003e ⚠️ Caution: Converting a large `Node` type into a JSON string may _impact performance_. or might be cause _unexpected behavior_.\n\n```go\npackage main\n\nimport (\n    \"gno.land/p/demo/json\"\n    \"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc main() {\n    node := ObjectNode(\"\", map[string]*Node{\n        \"foo\": StringNode(\"foo\", \"bar\"),\n        \"baz\": NumberNode(\"baz\", 100500),\n        \"qux\": NullNode(\"qux\"),\n    })\n\n    b, err := json.Marshal(node)\n    if err != nil {\n        ufmt.Errorf(\"error: %v\", err)\n    }\n\n    ufmt.Sprintf(\"json: %s\", string(b))\n}\n```\n\n### Searching\n\nOnce the JSON data converted into a `Node` type, you can **search** and **extract** data that satisfy specific conditions. For example, you can find data with a specific type or data with a specific key.\n\nTo use this functionality, you can use methods in the `GetXXX` prefixed methods. The `MustXXX` methods also provide the same functionality as the former methods, but they will **panic** if data doesn't satisfies the condition.\n\nHere is an example of finding data with a specific key. For more examples, please refer to the [node.gno](node.gno) file.\n\n```go\npackage main\n\nimport (\n    \"gno.land/p/demo/json\"\n    \"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc main() {\n    root, err := Unmarshal([]byte(`{\"foo\": true, \"bar\": null}`))\n    if err != nil {\n        ufmt.Errorf(\"error: %v\", err)\n    }\n\n    value, err := root.GetKey(\"foo\")\n    if err != nil {\n        ufmt.Errorf(\"error occurred while getting key, %s\", err)\n    }\n\n    if value.MustBool() != true {\n        ufmt.Errorf(\"value is not true\")\n    }\n\n    value, err = root.GetKey(\"bar\")\n    if err != nil {\n        t.Errorf(\"error occurred while getting key, %s\", err)\n    }\n\n    _, err = root.GetKey(\"baz\")\n    if err == nil {\n        t.Errorf(\"key baz is not exist. must be failed\")\n    }\n}\n```\n\n## Contributing\n\nPlease submit any issues or pull requests for this package through the GitHub repository at [gnolang/gno](\u003chttps://github.com/gnolang/gno\u003e).\n"},{"name":"buffer.gno","body":"package json\n\nimport (\n\t\"errors\"\n\t\"io\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype buffer struct {\n\tdata   []byte\n\tlength int\n\tindex  int\n\n\tlast  States\n\tstate States\n\tclass Classes\n}\n\n// newBuffer creates a new buffer with the given data\nfunc newBuffer(data []byte) *buffer {\n\treturn \u0026buffer{\n\t\tdata:   data,\n\t\tlength: len(data),\n\t\tlast:   GO,\n\t\tstate:  GO,\n\t}\n}\n\n// first retrieves the first non-whitespace (or other escaped) character in the buffer.\nfunc (b *buffer) first() (byte, error) {\n\tfor ; b.index \u003c b.length; b.index++ {\n\t\tc := b.data[b.index]\n\n\t\tif !(c == whiteSpace || c == carriageReturn || c == newLine || c == tab) {\n\t\t\treturn c, nil\n\t\t}\n\t}\n\n\treturn 0, io.EOF\n}\n\n// current returns the byte of the current index.\nfunc (b *buffer) current() (byte, error) {\n\tif b.index \u003e= b.length {\n\t\treturn 0, io.EOF\n\t}\n\n\treturn b.data[b.index], nil\n}\n\n// next moves to the next byte and returns it.\nfunc (b *buffer) next() (byte, error) {\n\tb.index++\n\treturn b.current()\n}\n\n// step just moves to the next position.\nfunc (b *buffer) step() error {\n\t_, err := b.next()\n\treturn err\n}\n\n// move moves the index by the given position.\nfunc (b *buffer) move(pos int) error {\n\tnewIndex := b.index + pos\n\n\tif newIndex \u003e b.length {\n\t\treturn io.EOF\n\t}\n\n\tb.index = newIndex\n\n\treturn nil\n}\n\n// slice returns the slice from the current index to the given position.\nfunc (b *buffer) slice(pos int) ([]byte, error) {\n\tend := b.index + pos\n\n\tif end \u003e b.length {\n\t\treturn nil, io.EOF\n\t}\n\n\treturn b.data[b.index:end], nil\n}\n\n// sliceFromIndices returns a slice of the buffer's data starting from 'start' up to (but not including) 'stop'.\nfunc (b *buffer) sliceFromIndices(start, stop int) []byte {\n\tif start \u003e b.length {\n\t\tstart = b.length\n\t}\n\n\tif stop \u003e b.length {\n\t\tstop = b.length\n\t}\n\n\treturn b.data[start:stop]\n}\n\n// skip moves the index to skip the given byte.\nfunc (b *buffer) skip(bs byte) error {\n\tfor b.index \u003c b.length {\n\t\tif b.data[b.index] == bs \u0026\u0026 !b.backslash() {\n\t\t\treturn nil\n\t\t}\n\n\t\tb.index++\n\t}\n\n\treturn io.EOF\n}\n\n// skipAndReturnIndex moves the buffer index forward by one and returns the new index.\nfunc (b *buffer) skipAndReturnIndex() (int, error) {\n\terr := b.step()\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\n\treturn b.index, nil\n}\n\n// skipUntil moves the buffer index forward until it encounters a byte contained in the endTokens set.\nfunc (b *buffer) skipUntil(endTokens map[byte]bool) (int, error) {\n\tfor b.index \u003c b.length {\n\t\tcurrentByte, err := b.current()\n\t\tif err != nil {\n\t\t\treturn b.index, err\n\t\t}\n\n\t\t// Check if the current byte is in the set of end tokens.\n\t\tif _, exists := endTokens[currentByte]; exists {\n\t\t\treturn b.index, nil\n\t\t}\n\n\t\tb.index++\n\t}\n\n\treturn b.index, io.EOF\n}\n\n// significantTokens is a map where the keys are the significant characters in a JSON path.\n// The values in the map are all true, which allows us to use the map as a set for quick lookups.\nvar significantTokens = [256]bool{\n\tdot:          true, // access properties of an object\n\tdollarSign:   true, // root object\n\tatSign:       true, // current object\n\tbracketOpen:  true, // start of an array index or filter expression\n\tbracketClose: true, // end of an array index or filter expression\n}\n\n// filterTokens stores the filter expression tokens.\nvar filterTokens = [256]bool{\n\taesterisk: true, // wildcard\n\tandSign:   true,\n\torSign:    true,\n}\n\n// skipToNextSignificantToken advances the buffer index to the next significant character.\n// Significant characters are defined based on the JSON path syntax.\nfunc (b *buffer) skipToNextSignificantToken() {\n\tfor b.index \u003c b.length {\n\t\tcurrent := b.data[b.index]\n\n\t\tif significantTokens[current] {\n\t\t\tbreak\n\t\t}\n\n\t\tb.index++\n\t}\n}\n\n// backslash checks to see if the number of backslashes before the current index is odd.\n//\n// This is used to check if the current character is escaped. However, unlike the \"unescape\" function,\n// \"backslash\" only serves to check the number of backslashes.\nfunc (b *buffer) backslash() bool {\n\tif b.index == 0 {\n\t\treturn false\n\t}\n\n\tcount := 0\n\tfor i := b.index - 1; ; i-- {\n\t\tif b.data[i] != backSlash {\n\t\t\tbreak\n\t\t}\n\n\t\tcount++\n\n\t\tif i == 0 {\n\t\t\tbreak\n\t\t}\n\t}\n\n\treturn count%2 != 0\n}\n\n// numIndex holds a map of valid numeric characters\nvar numIndex = [256]bool{\n\t'0': true,\n\t'1': true,\n\t'2': true,\n\t'3': true,\n\t'4': true,\n\t'5': true,\n\t'6': true,\n\t'7': true,\n\t'8': true,\n\t'9': true,\n\t'.': true,\n\t'e': true,\n\t'E': true,\n}\n\n// pathToken checks if the current token is a valid JSON path token.\nfunc (b *buffer) pathToken() error {\n\tvar stack []byte\n\n\tinToken := false\n\tinNumber := false\n\tfirst := b.index\n\n\tfor b.index \u003c b.length {\n\t\tc := b.data[b.index]\n\n\t\tswitch {\n\t\tcase c == doubleQuote || c == singleQuote:\n\t\t\tinToken = true\n\t\t\tif err := b.step(); err != nil {\n\t\t\t\treturn errors.New(\"error stepping through buffer\")\n\t\t\t}\n\n\t\t\tif err := b.skip(c); err != nil {\n\t\t\t\treturn errUnmatchedQuotePath\n\t\t\t}\n\n\t\t\tif b.index \u003e= b.length {\n\t\t\t\treturn errUnmatchedQuotePath\n\t\t\t}\n\n\t\tcase c == bracketOpen || c == parenOpen:\n\t\t\tinToken = true\n\t\t\tstack = append(stack, c)\n\n\t\tcase c == bracketClose || c == parenClose:\n\t\t\tinToken = true\n\t\t\tif len(stack) == 0 || (c == bracketClose \u0026\u0026 stack[len(stack)-1] != bracketOpen) || (c == parenClose \u0026\u0026 stack[len(stack)-1] != parenOpen) {\n\t\t\t\treturn errUnmatchedParenthesis\n\t\t\t}\n\n\t\t\tstack = stack[:len(stack)-1]\n\n\t\tcase pathStateContainsValidPathToken(c):\n\t\t\tinToken = true\n\n\t\tcase c == plus || c == minus:\n\t\t\tif inNumber || (b.index \u003e 0 \u0026\u0026 numIndex[b.data[b.index-1]]) {\n\t\t\t\tinToken = true\n\t\t\t} else if !inToken \u0026\u0026 (b.index+1 \u003c b.length \u0026\u0026 numIndex[b.data[b.index+1]]) {\n\t\t\t\tinToken = true\n\t\t\t\tinNumber = true\n\t\t\t} else if !inToken {\n\t\t\t\treturn errInvalidToken\n\t\t\t}\n\n\t\tdefault:\n\t\t\tif len(stack) != 0 || inToken {\n\t\t\t\tinToken = true\n\t\t\t} else {\n\t\t\t\tgoto end\n\t\t\t}\n\t\t}\n\n\t\tb.index++\n\t}\n\nend:\n\tif len(stack) != 0 {\n\t\treturn errUnmatchedParenthesis\n\t}\n\n\tif first == b.index {\n\t\treturn errors.New(\"no token found\")\n\t}\n\n\tif inNumber \u0026\u0026 !numIndex[b.data[b.index-1]] {\n\t\tinNumber = false\n\t}\n\n\treturn nil\n}\n\nfunc pathStateContainsValidPathToken(c byte) bool {\n\tif significantTokens[c] {\n\t\treturn true\n\t}\n\n\tif filterTokens[c] {\n\t\treturn true\n\t}\n\n\tif numIndex[c] {\n\t\treturn true\n\t}\n\n\tif 'A' \u003c= c \u0026\u0026 c \u003c= 'Z' || 'a' \u003c= c \u0026\u0026 c \u003c= 'z' {\n\t\treturn true\n\t}\n\n\treturn false\n}\n\nfunc (b *buffer) numeric(token bool) error {\n\tif token {\n\t\tb.last = GO\n\t}\n\n\tfor ; b.index \u003c b.length; b.index++ {\n\t\tb.class = b.getClasses(doubleQuote)\n\t\tif b.class == __ {\n\t\t\treturn errInvalidToken\n\t\t}\n\n\t\tb.state = StateTransitionTable[b.last][b.class]\n\t\tif b.state == __ {\n\t\t\tif token {\n\t\t\t\tbreak\n\t\t\t}\n\n\t\t\treturn errInvalidToken\n\t\t}\n\n\t\tif b.state \u003c __ {\n\t\t\treturn nil\n\t\t}\n\n\t\tif b.state \u003c MI || b.state \u003e E3 {\n\t\t\treturn nil\n\t\t}\n\n\t\tb.last = b.state\n\t}\n\n\tif b.last != ZE \u0026\u0026 b.last != IN \u0026\u0026 b.last != FR \u0026\u0026 b.last != E3 {\n\t\treturn errInvalidToken\n\t}\n\n\treturn nil\n}\n\nfunc (b *buffer) getClasses(c byte) Classes {\n\tif b.data[b.index] \u003e= 128 {\n\t\treturn C_ETC\n\t}\n\n\tif c == singleQuote {\n\t\treturn QuoteAsciiClasses[b.data[b.index]]\n\t}\n\n\treturn AsciiClasses[b.data[b.index]]\n}\n\nfunc (b *buffer) getState() States {\n\tb.last = b.state\n\n\tb.class = b.getClasses(doubleQuote)\n\tif b.class == __ {\n\t\treturn __\n\t}\n\n\tb.state = StateTransitionTable[b.last][b.class]\n\n\treturn b.state\n}\n\n// string parses a string token from the buffer.\nfunc (b *buffer) string(search byte, token bool) error {\n\tif token {\n\t\tb.last = GO\n\t}\n\n\tfor ; b.index \u003c b.length; b.index++ {\n\t\tb.class = b.getClasses(search)\n\n\t\tif b.class == __ {\n\t\t\treturn errInvalidToken\n\t\t}\n\n\t\tb.state = StateTransitionTable[b.last][b.class]\n\t\tif b.state == __ {\n\t\t\treturn errInvalidToken\n\t\t}\n\n\t\tif b.state \u003c __ {\n\t\t\tbreak\n\t\t}\n\n\t\tb.last = b.state\n\t}\n\n\treturn nil\n}\n\nfunc (b *buffer) word(bs []byte) error {\n\tvar c byte\n\n\tmax := len(bs)\n\tindex := 0\n\n\tfor ; b.index \u003c b.length \u0026\u0026 index \u003c max; b.index++ {\n\t\tc = b.data[b.index]\n\n\t\tif c != bs[index] {\n\t\t\treturn errInvalidToken\n\t\t}\n\n\t\tindex++\n\t\tif index \u003e= max {\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif index != max {\n\t\treturn errInvalidToken\n\t}\n\n\treturn nil\n}\n\nfunc numberKind2f64(value any) (result float64, err error) {\n\tswitch typed := value.(type) {\n\tcase float64:\n\t\tresult = typed\n\tcase float32:\n\t\tresult = float64(typed)\n\tcase int:\n\t\tresult = float64(typed)\n\tcase int8:\n\t\tresult = float64(typed)\n\tcase int16:\n\t\tresult = float64(typed)\n\tcase int32:\n\t\tresult = float64(typed)\n\tcase int64:\n\t\tresult = float64(typed)\n\tcase uint:\n\t\tresult = float64(typed)\n\tcase uint8:\n\t\tresult = float64(typed)\n\tcase uint16:\n\t\tresult = float64(typed)\n\tcase uint32:\n\t\tresult = float64(typed)\n\tcase uint64:\n\t\tresult = float64(typed)\n\tdefault:\n\t\terr = ufmt.Errorf(\"invalid number type: %T\", value)\n\t}\n\n\treturn\n}\n"},{"name":"buffer_test.gno","body":"package json\n\nimport (\n\t\"testing\"\n)\n\nfunc TestBufferCurrent(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tbuffer   *buffer\n\t\texpected byte\n\t\twantErr  bool\n\t}{\n\t\t{\n\t\t\tname: \"Valid current byte\",\n\t\t\tbuffer: \u0026buffer{\n\t\t\t\tdata:   []byte(\"test\"),\n\t\t\t\tlength: 4,\n\t\t\t\tindex:  1,\n\t\t\t},\n\t\t\texpected: 'e',\n\t\t\twantErr:  false,\n\t\t},\n\t\t{\n\t\t\tname: \"EOF\",\n\t\t\tbuffer: \u0026buffer{\n\t\t\t\tdata:   []byte(\"test\"),\n\t\t\t\tlength: 4,\n\t\t\t\tindex:  4,\n\t\t\t},\n\t\t\texpected: 0,\n\t\t\twantErr:  true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot, err := tt.buffer.current()\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"buffer.current() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"buffer.current() = %v, want %v\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBufferStep(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tbuffer  *buffer\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname:    \"Valid step\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"EOF error\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 3},\n\t\t\twantErr: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\terr := tt.buffer.step()\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"buffer.step() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBufferNext(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tbuffer  *buffer\n\t\twant    byte\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname:    \"Valid next byte\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\twant:    'e',\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"EOF error\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 3},\n\t\t\twant:    0,\n\t\t\twantErr: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot, err := tt.buffer.next()\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"buffer.next() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif got != tt.want {\n\t\t\t\tt.Errorf(\"buffer.next() = %v, want %v\", got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBufferSlice(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tbuffer  *buffer\n\t\tpos     int\n\t\twant    []byte\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname:    \"Valid slice -- 0 characters\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\tpos:     0,\n\t\t\twant:    nil,\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"Valid slice -- 1 character\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\tpos:     1,\n\t\t\twant:    []byte(\"t\"),\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"Valid slice -- 2 characters\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 1},\n\t\t\tpos:     2,\n\t\t\twant:    []byte(\"es\"),\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"Valid slice -- 3 characters\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\tpos:     3,\n\t\t\twant:    []byte(\"tes\"),\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"Valid slice -- 4 characters\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\tpos:     4,\n\t\t\twant:    []byte(\"test\"),\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"EOF error\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 3},\n\t\t\tpos:     2,\n\t\t\twant:    nil,\n\t\t\twantErr: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot, err := tt.buffer.slice(tt.pos)\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"buffer.slice() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif string(got) != string(tt.want) {\n\t\t\t\tt.Errorf(\"buffer.slice() = %v, want %v\", got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBufferMove(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tbuffer  *buffer\n\t\tpos     int\n\t\twantErr bool\n\t\twantIdx int\n\t}{\n\t\t{\n\t\t\tname:    \"Valid move\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 1},\n\t\t\tpos:     2,\n\t\t\twantErr: false,\n\t\t\twantIdx: 3,\n\t\t},\n\t\t{\n\t\t\tname:    \"Move beyond length\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 1},\n\t\t\tpos:     4,\n\t\t\twantErr: true,\n\t\t\twantIdx: 1,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\terr := tt.buffer.move(tt.pos)\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"buffer.move() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t}\n\t\t\tif tt.buffer.index != tt.wantIdx {\n\t\t\t\tt.Errorf(\"buffer.move() index = %v, want %v\", tt.buffer.index, tt.wantIdx)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBufferSkip(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tbuffer  *buffer\n\t\tb       byte\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname:    \"Skip byte\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\tb:       'e',\n\t\t\twantErr: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"Skip to EOF\",\n\t\t\tbuffer:  \u0026buffer{data: []byte(\"test\"), length: 4, index: 0},\n\t\t\tb:       'x',\n\t\t\twantErr: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\terr := tt.buffer.skip(tt.b)\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"buffer.skip() error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSkipToNextSignificantToken(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []byte\n\t\texpected int\n\t}{\n\t\t{\"No significant chars\", []byte(\"abc\"), 3},\n\t\t{\"One significant char at start\", []byte(\".abc\"), 0},\n\t\t{\"Significant char in middle\", []byte(\"ab.c\"), 2},\n\t\t{\"Multiple significant chars\", []byte(\"a$.c\"), 1},\n\t\t{\"Significant char at end\", []byte(\"abc$\"), 3},\n\t\t{\"Only significant chars\", []byte(\"$.\"), 0},\n\t\t{\"Empty string\", []byte(\"\"), 0},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tb := newBuffer(tt.input)\n\t\t\tb.skipToNextSignificantToken()\n\t\t\tif b.index != tt.expected {\n\t\t\t\tt.Errorf(\"after skipToNextSignificantToken(), got index = %v, want %v\", b.index, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc mockBuffer(s string) *buffer {\n\treturn newBuffer([]byte(s))\n}\n\nfunc TestSkipAndReturnIndex(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    string\n\t\texpected int\n\t}{\n\t\t{\"StartOfString\", \"\", 0},\n\t\t{\"MiddleOfString\", \"abcdef\", 1},\n\t\t{\"EndOfString\", \"abc\", 1},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tbuf := mockBuffer(tt.input)\n\t\t\tgot, err := buf.skipAndReturnIndex()\n\t\t\tif err != nil \u0026\u0026 tt.input != \"\" { // Expect no error unless input is empty\n\t\t\t\tt.Errorf(\"skipAndReturnIndex() error = %v\", err)\n\t\t\t}\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"skipAndReturnIndex() = %v, want %v\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSkipUntil(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    string\n\t\ttokens   map[byte]bool\n\t\texpected int\n\t}{\n\t\t{\"SkipToToken\", \"abcdefg\", map[byte]bool{'c': true}, 2},\n\t\t{\"SkipToEnd\", \"abcdefg\", map[byte]bool{'h': true}, 7},\n\t\t{\"SkipNone\", \"abcdefg\", map[byte]bool{'a': true}, 0},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tbuf := mockBuffer(tt.input)\n\t\t\tgot, err := buf.skipUntil(tt.tokens)\n\t\t\tif err != nil \u0026\u0026 got != len(tt.input) { // Expect error only if reached end without finding token\n\t\t\t\tt.Errorf(\"skipUntil() error = %v\", err)\n\t\t\t}\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"skipUntil() = %v, want %v\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSliceFromIndices(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    string\n\t\tstart    int\n\t\tend      int\n\t\texpected string\n\t}{\n\t\t{\"FullString\", \"abcdefg\", 0, 7, \"abcdefg\"},\n\t\t{\"Substring\", \"abcdefg\", 2, 5, \"cde\"},\n\t\t{\"OutOfBounds\", \"abcdefg\", 5, 10, \"fg\"},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tbuf := mockBuffer(tt.input)\n\t\t\tgot := buf.sliceFromIndices(tt.start, tt.end)\n\t\t\tif string(got) != tt.expected {\n\t\t\t\tt.Errorf(\"sliceFromIndices() = %v, want %v\", string(got), tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBufferToken(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tpath  string\n\t\tindex int\n\t\tisErr bool\n\t}{\n\t\t{\n\t\t\tname:  \"Simple valid path\",\n\t\t\tpath:  \"@.length\",\n\t\t\tindex: 8,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"Path with array expr\",\n\t\t\tpath:  \"@['foo'].0.bar\",\n\t\t\tindex: 14,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"Path with array expr and simple fomula\",\n\t\t\tpath:  \"@['foo'].[(@.length - 1)].*\",\n\t\t\tindex: 27,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"Path with filter expr\",\n\t\t\tpath:  \"@['foo'].[?(@.bar == 1 \u0026 @.baz \u003c @.length)].*\",\n\t\t\tindex: 45,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"addition of foo and bar\",\n\t\t\tpath:  \"@.foo+@.bar\",\n\t\t\tindex: 11,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"logical AND of foo and bar\",\n\t\t\tpath:  \"@.foo \u0026\u0026 @.bar\",\n\t\t\tindex: 14,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"logical OR of foo and bar\",\n\t\t\tpath:  \"@.foo || @.bar\",\n\t\t\tindex: 14,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"accessing third element of foo\",\n\t\t\tpath:  \"@.foo,3\",\n\t\t\tindex: 7,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"accessing last element of array\",\n\t\t\tpath:  \"@.length-1\",\n\t\t\tindex: 10,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"number 1\",\n\t\t\tpath:  \"1\",\n\t\t\tindex: 1,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"float\",\n\t\t\tpath:  \"3.1e4\",\n\t\t\tindex: 5,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"float with minus\",\n\t\t\tpath:  \"3.1e-4\",\n\t\t\tindex: 6,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"float with plus\",\n\t\t\tpath:  \"3.1e+4\",\n\t\t\tindex: 6,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"negative number\",\n\t\t\tpath:  \"-12345\",\n\t\t\tindex: 6,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"negative float\",\n\t\t\tpath:  \"-3.1e4\",\n\t\t\tindex: 6,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"negative float with minus\",\n\t\t\tpath:  \"-3.1e-4\",\n\t\t\tindex: 7,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"negative float with plus\",\n\t\t\tpath:  \"-3.1e+4\",\n\t\t\tindex: 7,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"string number\",\n\t\t\tpath:  \"'12345'\",\n\t\t\tindex: 7,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"string with backslash\",\n\t\t\tpath:  \"'foo \\\\'bar '\",\n\t\t\tindex: 12,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"string with inner double quotes\",\n\t\t\tpath:  \"'foo \\\"bar \\\"'\",\n\t\t\tindex: 12,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"parenthesis 1\",\n\t\t\tpath:  \"(@abc)\",\n\t\t\tindex: 6,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"parenthesis 2\",\n\t\t\tpath:  \"[()]\",\n\t\t\tindex: 4,\n\t\t\tisErr: false,\n\t\t},\n\t\t{\n\t\t\tname:  \"parenthesis mismatch\",\n\t\t\tpath:  \"[(])\",\n\t\t\tindex: 2,\n\t\t\tisErr: true,\n\t\t},\n\t\t{\n\t\t\tname:  \"parenthesis mismatch 2\",\n\t\t\tpath:  \"(\",\n\t\t\tindex: 1,\n\t\t\tisErr: true,\n\t\t},\n\t\t{\n\t\t\tname:  \"parenthesis mismatch 3\",\n\t\t\tpath:  \"())]\",\n\t\t\tindex: 2,\n\t\t\tisErr: true,\n\t\t},\n\t\t{\n\t\t\tname:  \"bracket mismatch\",\n\t\t\tpath:  \"[()\",\n\t\t\tindex: 3,\n\t\t\tisErr: true,\n\t\t},\n\t\t{\n\t\t\tname:  \"bracket mismatch 2\",\n\t\t\tpath:  \"()]\",\n\t\t\tindex: 2,\n\t\t\tisErr: true,\n\t\t},\n\t\t{\n\t\t\tname:  \"path does not close bracket\",\n\t\t\tpath:  \"@.foo[)\",\n\t\t\tindex: 6,\n\t\t\tisErr: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tbuf := newBuffer([]byte(tt.path))\n\n\t\t\terr := buf.pathToken()\n\t\t\tif tt.isErr {\n\t\t\t\tif err == nil {\n\t\t\t\t\tt.Errorf(\"Expected an error for path `%s`, but got none\", tt.path)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif err == nil \u0026\u0026 tt.isErr {\n\t\t\t\tt.Errorf(\"Expected an error for path `%s`, but got none\", tt.path)\n\t\t\t}\n\n\t\t\tif buf.index != tt.index {\n\t\t\t\tt.Errorf(\"Expected final index %d, got %d (token: `%s`) for path `%s`\", tt.index, buf.index, string(buf.data[buf.index]), tt.path)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBufferFirst(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tdata     []byte\n\t\texpected byte\n\t}{\n\t\t{\n\t\t\tname:     \"Valid first byte\",\n\t\t\tdata:     []byte(\"test\"),\n\t\t\texpected: 't',\n\t\t},\n\t\t{\n\t\t\tname:     \"Empty buffer\",\n\t\t\tdata:     []byte(\"\"),\n\t\t\texpected: 0,\n\t\t},\n\t\t{\n\t\t\tname:     \"Whitespace buffer\",\n\t\t\tdata:     []byte(\"   \"),\n\t\t\texpected: 0,\n\t\t},\n\t\t{\n\t\t\tname:     \"whitespace in middle\",\n\t\t\tdata:     []byte(\"hello world\"),\n\t\t\texpected: 'h',\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tb := newBuffer(tt.data)\n\n\t\t\tgot, err := b.first()\n\t\t\tif err != nil \u0026\u0026 tt.expected != 0 {\n\t\t\t\tt.Errorf(\"Unexpected error: %v\", err)\n\t\t\t}\n\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"Expected first byte to be %q, got %q\", tt.expected, got)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"builder.gno","body":"package json\n\ntype NodeBuilder struct {\n\tnode *Node\n}\n\nfunc Builder() *NodeBuilder {\n\treturn \u0026NodeBuilder{node: ObjectNode(\"\", nil)}\n}\n\nfunc (b *NodeBuilder) WriteString(key, value string) *NodeBuilder {\n\tb.node.AppendObject(key, StringNode(\"\", value))\n\treturn b\n}\n\nfunc (b *NodeBuilder) WriteNumber(key string, value float64) *NodeBuilder {\n\tb.node.AppendObject(key, NumberNode(\"\", value))\n\treturn b\n}\n\nfunc (b *NodeBuilder) WriteBool(key string, value bool) *NodeBuilder {\n\tb.node.AppendObject(key, BoolNode(\"\", value))\n\treturn b\n}\n\nfunc (b *NodeBuilder) WriteNull(key string) *NodeBuilder {\n\tb.node.AppendObject(key, NullNode(\"\"))\n\treturn b\n}\n\nfunc (b *NodeBuilder) WriteObject(key string, fn func(*NodeBuilder)) *NodeBuilder {\n\tnestedBuilder := \u0026NodeBuilder{node: ObjectNode(\"\", nil)}\n\tfn(nestedBuilder)\n\tb.node.AppendObject(key, nestedBuilder.node)\n\treturn b\n}\n\nfunc (b *NodeBuilder) WriteArray(key string, fn func(*ArrayBuilder)) *NodeBuilder {\n\tarrayBuilder := \u0026ArrayBuilder{nodes: []*Node{}}\n\tfn(arrayBuilder)\n\tb.node.AppendObject(key, ArrayNode(\"\", arrayBuilder.nodes))\n\treturn b\n}\n\nfunc (b *NodeBuilder) Node() *Node {\n\treturn b.node\n}\n\ntype ArrayBuilder struct {\n\tnodes []*Node\n}\n\nfunc (ab *ArrayBuilder) WriteString(value string) *ArrayBuilder {\n\tab.nodes = append(ab.nodes, StringNode(\"\", value))\n\treturn ab\n}\n\nfunc (ab *ArrayBuilder) WriteNumber(value float64) *ArrayBuilder {\n\tab.nodes = append(ab.nodes, NumberNode(\"\", value))\n\treturn ab\n}\n\nfunc (ab *ArrayBuilder) WriteInt(value int) *ArrayBuilder {\n\treturn ab.WriteNumber(float64(value))\n}\n\nfunc (ab *ArrayBuilder) WriteBool(value bool) *ArrayBuilder {\n\tab.nodes = append(ab.nodes, BoolNode(\"\", value))\n\treturn ab\n}\n\nfunc (ab *ArrayBuilder) WriteNull() *ArrayBuilder {\n\tab.nodes = append(ab.nodes, NullNode(\"\"))\n\treturn ab\n}\n\nfunc (ab *ArrayBuilder) WriteObject(fn func(*NodeBuilder)) *ArrayBuilder {\n\tnestedBuilder := \u0026NodeBuilder{node: ObjectNode(\"\", nil)}\n\tfn(nestedBuilder)\n\tab.nodes = append(ab.nodes, nestedBuilder.node)\n\treturn ab\n}\n\nfunc (ab *ArrayBuilder) WriteArray(fn func(*ArrayBuilder)) *ArrayBuilder {\n\tnestedArrayBuilder := \u0026ArrayBuilder{nodes: []*Node{}}\n\tfn(nestedArrayBuilder)\n\tab.nodes = append(ab.nodes, ArrayNode(\"\", nestedArrayBuilder.nodes))\n\treturn ab\n}\n"},{"name":"builder_test.gno","body":"package json\n\nimport (\n\t\"testing\"\n)\n\nfunc TestNodeBuilder(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tbuild    func() *Node\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname: \"plain object\",\n\t\t\tbuild: func() *Node {\n\t\t\t\treturn Builder().\n\t\t\t\t\tWriteString(\"name\", \"Alice\").\n\t\t\t\t\tWriteNumber(\"age\", 30).\n\t\t\t\t\tWriteBool(\"is_student\", false).\n\t\t\t\t\tNode()\n\t\t\t},\n\t\t\texpected: `{\"name\":\"Alice\",\"age\":30,\"is_student\":false}`,\n\t\t},\n\t\t{\n\t\t\tname: \"nested object\",\n\t\t\tbuild: func() *Node {\n\t\t\t\treturn Builder().\n\t\t\t\t\tWriteString(\"name\", \"Alice\").\n\t\t\t\t\tWriteObject(\"address\", func(b *NodeBuilder) {\n\t\t\t\t\t\tb.WriteString(\"city\", \"New York\").\n\t\t\t\t\t\t\tWriteNumber(\"zipcode\", 10001)\n\t\t\t\t\t}).\n\t\t\t\t\tNode()\n\t\t\t},\n\t\t\texpected: `{\"name\":\"Alice\",\"address\":{\"city\":\"New York\",\"zipcode\":10001}}`,\n\t\t},\n\t\t{\n\t\t\tname: \"null node\",\n\t\t\tbuild: func() *Node {\n\t\t\t\treturn Builder().WriteNull(\"foo\").Node()\n\t\t\t},\n\t\t\texpected: `{\"foo\":null}`,\n\t\t},\n\t\t{\n\t\t\tname: \"array node\",\n\t\t\tbuild: func() *Node {\n\t\t\t\treturn Builder().\n\t\t\t\t\tWriteArray(\"items\", func(ab *ArrayBuilder) {\n\t\t\t\t\t\tab.WriteString(\"item1\").\n\t\t\t\t\t\t\tWriteString(\"item2\").\n\t\t\t\t\t\t\tWriteString(\"item3\")\n\t\t\t\t\t}).\n\t\t\t\t\tNode()\n\t\t\t},\n\t\t\texpected: `{\"items\":[\"item1\",\"item2\",\"item3\"]}`,\n\t\t},\n\t\t{\n\t\t\tname: \"array with objects\",\n\t\t\tbuild: func() *Node {\n\t\t\t\treturn Builder().\n\t\t\t\t\tWriteArray(\"users\", func(ab *ArrayBuilder) {\n\t\t\t\t\t\tab.WriteObject(func(b *NodeBuilder) {\n\t\t\t\t\t\t\tb.WriteString(\"name\", \"Bob\").\n\t\t\t\t\t\t\t\tWriteNumber(\"age\", 25)\n\t\t\t\t\t\t}).\n\t\t\t\t\t\t\tWriteObject(func(b *NodeBuilder) {\n\t\t\t\t\t\t\t\tb.WriteString(\"name\", \"Carol\").\n\t\t\t\t\t\t\t\t\tWriteNumber(\"age\", 27)\n\t\t\t\t\t\t\t})\n\t\t\t\t\t}).\n\t\t\t\t\tNode()\n\t\t\t},\n\t\t\texpected: `{\"users\":[{\"name\":\"Bob\",\"age\":25},{\"name\":\"Carol\",\"age\":27}]}`,\n\t\t},\n\t\t{\n\t\t\tname: \"array with various types\",\n\t\t\tbuild: func() *Node {\n\t\t\t\treturn Builder().\n\t\t\t\t\tWriteArray(\"values\", func(ab *ArrayBuilder) {\n\t\t\t\t\t\tab.WriteString(\"item1\").\n\t\t\t\t\t\t\tWriteNumber(123).\n\t\t\t\t\t\t\tWriteBool(true).\n\t\t\t\t\t\t\tWriteNull()\n\t\t\t\t\t}).\n\t\t\t\t\tNode()\n\t\t\t},\n\t\t\texpected: `{\"values\":[\"item1\",123,true,null]}`,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tnode := tt.build()\n\t\t\tvalue, err := Marshal(node)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"unexpected error: %s\", err)\n\t\t\t}\n\t\t\tif string(value) != tt.expected {\n\t\t\t\tt.Errorf(\"expected %s, got %s\", tt.expected, string(value))\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"decode.gno","body":"// ref: https://github.com/spyzhov/ajson/blob/master/decode.go\n\npackage json\n\nimport (\n\t\"errors\"\n\t\"io\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// This limits the max nesting depth to prevent stack overflow.\n// This is permitted by https://tools.ietf.org/html/rfc7159#section-9\nconst maxNestingDepth = 10000\n\n// Unmarshal parses the JSON-encoded data and returns a Node.\n// The data must be a valid JSON-encoded value.\n//\n// Usage:\n//\n//\tnode, err := json.Unmarshal([]byte(`{\"key\": \"value\"}`))\n//\tif err != nil {\n//\t\tufmt.Println(err)\n//\t}\n//\tprintln(node) // {\"key\": \"value\"}\nfunc Unmarshal(data []byte) (*Node, error) {\n\tbuf := newBuffer(data)\n\n\tvar (\n\t\tstate   States\n\t\tkey     *string\n\t\tcurrent *Node\n\t\tnesting int\n\t\tuseKey  = func() **string {\n\t\t\ttmp := cptrs(key)\n\t\t\tkey = nil\n\t\t\treturn \u0026tmp\n\t\t}\n\t\terr error\n\t)\n\n\tif _, err = buf.first(); err != nil {\n\t\treturn nil, io.EOF\n\t}\n\n\tfor {\n\t\tstate = buf.getState()\n\t\tif state == __ {\n\t\t\treturn nil, unexpectedTokenError(buf.data, buf.index)\n\t\t}\n\n\t\t// region state machine\n\t\tif state \u003e= GO {\n\t\t\tswitch buf.state {\n\t\t\tcase ST: // string\n\t\t\t\tif current != nil \u0026\u0026 current.IsObject() \u0026\u0026 key == nil {\n\t\t\t\t\t// key detected\n\t\t\t\t\tif key, err = getString(buf); err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tbuf.state = CO\n\t\t\t\t} else {\n\t\t\t\t\tcurrent, nesting, err = createNestedNode(current, buf, String, nesting, useKey())\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\terr = buf.string(doubleQuote, false)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tcurrent, nesting = updateNode(current, buf, nesting, true)\n\t\t\t\t\tbuf.state = OK\n\t\t\t\t}\n\n\t\t\tcase MI, ZE, IN: // number\n\t\t\t\tcurrent, err = processNumericNode(current, buf, useKey())\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\tcase T1, F1: // boolean\n\t\t\t\tliteral := falseLiteral\n\t\t\t\tif buf.state == T1 {\n\t\t\t\t\tliteral = trueLiteral\n\t\t\t\t}\n\n\t\t\t\tcurrent, nesting, err = processLiteralNode(current, buf, Boolean, literal, useKey(), nesting)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\tcase N1: // null\n\t\t\t\tcurrent, nesting, err = processLiteralNode(current, buf, Null, nullLiteral, useKey(), nesting)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\t// region action\n\t\t\tswitch state {\n\t\t\tcase ec, cc: // \u003cempty\u003e }\n\t\t\t\tif key != nil {\n\t\t\t\t\treturn nil, unexpectedTokenError(buf.data, buf.index)\n\t\t\t\t}\n\n\t\t\t\tcurrent, nesting, err = updateNodeAndSetBufferState(current, buf, nesting, Object)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\tcase bc: // ]\n\t\t\t\tcurrent, nesting, err = updateNodeAndSetBufferState(current, buf, nesting, Array)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\tcase co, bo: // { [\n\t\t\t\tvalTyp, bState := Object, OB\n\t\t\t\tif state == bo {\n\t\t\t\t\tvalTyp, bState = Array, AR\n\t\t\t\t}\n\n\t\t\t\tcurrent, nesting, err = createNestedNode(current, buf, valTyp, nesting, useKey())\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\tbuf.state = bState\n\n\t\t\tcase cm: // ,\n\t\t\t\tif current == nil {\n\t\t\t\t\treturn nil, unexpectedTokenError(buf.data, buf.index)\n\t\t\t\t}\n\n\t\t\t\tif !current.isContainer() {\n\t\t\t\t\treturn nil, unexpectedTokenError(buf.data, buf.index)\n\t\t\t\t}\n\n\t\t\t\tif current.IsObject() {\n\t\t\t\t\tbuf.state = KE // key expected\n\t\t\t\t} else {\n\t\t\t\t\tbuf.state = VA // value expected\n\t\t\t\t}\n\n\t\t\tcase cl: // :\n\t\t\t\tif current == nil || !current.IsObject() || key == nil {\n\t\t\t\t\treturn nil, unexpectedTokenError(buf.data, buf.index)\n\t\t\t\t}\n\n\t\t\t\tbuf.state = VA\n\n\t\t\tdefault:\n\t\t\t\treturn nil, unexpectedTokenError(buf.data, buf.index)\n\t\t\t}\n\t\t}\n\n\t\tif buf.step() != nil {\n\t\t\tbreak\n\t\t}\n\n\t\tif _, err = buf.first(); err != nil {\n\t\t\terr = nil\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif current == nil || buf.state != OK {\n\t\treturn nil, io.EOF\n\t}\n\n\troot := current.root()\n\tif !root.ready() {\n\t\treturn nil, io.EOF\n\t}\n\n\treturn root, err\n}\n\n// UnmarshalSafe parses the JSON-encoded data and returns a Node.\nfunc UnmarshalSafe(data []byte) (*Node, error) {\n\tvar safe []byte\n\tsafe = append(safe, data...)\n\treturn Unmarshal(safe)\n}\n\n// processNumericNode creates a new node, processes a numeric value,\n// sets the node's borders, and moves to the previous node.\nfunc processNumericNode(current *Node, buf *buffer, key **string) (*Node, error) {\n\tvar err error\n\tcurrent, err = createNode(current, buf, Number, key)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif err = buf.numeric(false); err != nil {\n\t\treturn nil, err\n\t}\n\n\tcurrent.borders[1] = buf.index\n\tif current.prev != nil {\n\t\tcurrent = current.prev\n\t}\n\n\tbuf.index -= 1\n\tbuf.state = OK\n\n\treturn current, nil\n}\n\n// processLiteralNode creates a new node, processes a literal value,\n// sets the node's borders, and moves to the previous node.\nfunc processLiteralNode(\n\tcurrent *Node,\n\tbuf *buffer,\n\tliteralType ValueType,\n\tliteralValue []byte,\n\tuseKey **string,\n\tnesting int,\n) (*Node, int, error) {\n\tvar err error\n\tcurrent, nesting, err = createLiteralNode(current, buf, literalType, literalValue, useKey, nesting)\n\tif err != nil {\n\t\treturn nil, nesting, err\n\t}\n\treturn current, nesting, nil\n}\n\n// isValidContainerType checks if the current node is a valid container (object or array).\n// The container must satisfy the following conditions:\n//  1. The current node must not be nil.\n//  2. The current node must be an object or array.\n//  3. The current node must not be ready.\nfunc isValidContainerType(current *Node, nodeType ValueType) bool {\n\tswitch nodeType {\n\tcase Object:\n\t\treturn current != nil \u0026\u0026 current.IsObject() \u0026\u0026 !current.ready()\n\tcase Array:\n\t\treturn current != nil \u0026\u0026 current.IsArray() \u0026\u0026 !current.ready()\n\tdefault:\n\t\treturn false\n\t}\n}\n\n// getString extracts a string from the buffer and advances the buffer index past the string.\nfunc getString(b *buffer) (*string, error) {\n\tstart := b.index\n\tif err := b.string(doubleQuote, false); err != nil {\n\t\treturn nil, err\n\t}\n\n\tvalue, ok := Unquote(b.data[start:b.index+1], doubleQuote)\n\tif !ok {\n\t\treturn nil, unexpectedTokenError(b.data, start)\n\t}\n\n\treturn \u0026value, nil\n}\n\n// createNode creates a new node and sets the key if it is not nil.\nfunc createNode(\n\tcurrent *Node,\n\tbuf *buffer,\n\tnodeType ValueType,\n\tkey **string,\n) (*Node, error) {\n\tvar err error\n\tcurrent, err = NewNode(current, buf, nodeType, key)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn current, nil\n}\n\n// createNestedNode creates a new nested node (array or object) and sets the key if it is not nil.\nfunc createNestedNode(\n\tcurrent *Node,\n\tbuf *buffer,\n\tnodeType ValueType,\n\tnesting int,\n\tkey **string,\n) (*Node, int, error) {\n\tvar err error\n\tif nesting, err = checkNestingDepth(nesting); err != nil {\n\t\treturn nil, nesting, err\n\t}\n\n\tif current, err = createNode(current, buf, nodeType, key); err != nil {\n\t\treturn nil, nesting, err\n\t}\n\n\treturn current, nesting, nil\n}\n\n// createLiteralNode creates a new literal node and sets the key if it is not nil.\n// The literal is a byte slice that represents a boolean or null value.\nfunc createLiteralNode(\n\tcurrent *Node,\n\tbuf *buffer,\n\tliteralType ValueType,\n\tliteral []byte,\n\tuseKey **string,\n\tnesting int,\n) (*Node, int, error) {\n\tvar err error\n\tif current, err = createNode(current, buf, literalType, useKey); err != nil {\n\t\treturn nil, 0, err\n\t}\n\n\tif err = buf.word(literal); err != nil {\n\t\treturn nil, 0, err\n\t}\n\n\tcurrent, nesting = updateNode(current, buf, nesting, false)\n\tbuf.state = OK\n\n\treturn current, nesting, nil\n}\n\n// updateNode updates the current node and returns the previous node.\nfunc updateNode(\n\tcurrent *Node, buf *buffer, nesting int, decreaseLevel bool,\n) (*Node, int) {\n\tcurrent.borders[1] = buf.index + 1\n\n\tprev := current.prev\n\tif prev == nil {\n\t\treturn current, nesting\n\t}\n\n\tcurrent = prev\n\tif decreaseLevel {\n\t\tnesting--\n\t}\n\n\treturn current, nesting\n}\n\n// updateNodeAndSetBufferState updates the current node and sets the buffer state to OK.\nfunc updateNodeAndSetBufferState(\n\tcurrent *Node,\n\tbuf *buffer,\n\tnesting int,\n\ttyp ValueType,\n) (*Node, int, error) {\n\tif !isValidContainerType(current, typ) {\n\t\treturn nil, nesting, unexpectedTokenError(buf.data, buf.index)\n\t}\n\n\tcurrent, nesting = updateNode(current, buf, nesting, true)\n\tbuf.state = OK\n\n\treturn current, nesting, nil\n}\n\n// checkNestingDepth checks if the nesting depth is within the maximum allowed depth.\nfunc checkNestingDepth(nesting int) (int, error) {\n\tif nesting \u003e= maxNestingDepth {\n\t\treturn nesting, errors.New(\"maximum nesting depth exceeded\")\n\t}\n\n\treturn nesting + 1, nil\n}\n\nfunc unexpectedTokenError(data []byte, index int) error {\n\treturn ufmt.Errorf(\"unexpected token at index %d. data %b\", index, data)\n}\n"},{"name":"decode_test.gno","body":"package json\n\nimport (\n\t\"bytes\"\n\t\"testing\"\n)\n\ntype testNode struct {\n\tname  string\n\tinput []byte\n\tvalue []byte\n\t_type ValueType\n}\n\nfunc simpleValid(test *testNode, t *testing.T) {\n\troot, err := Unmarshal(test.input)\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(%s): %s\", test.input, err.Error())\n\t} else if root == nil {\n\t\tt.Errorf(\"Error on Unmarshal(%s): root is nil\", test.name)\n\t} else if root.nodeType != test._type {\n\t\tt.Errorf(\"Error on Unmarshal(%s): wrong type\", test.name)\n\t} else if !bytes.Equal(root.source(), test.value) {\n\t\tt.Errorf(\"Error on Unmarshal(%s): %s != %s\", test.name, root.source(), test.value)\n\t}\n}\n\nfunc simpleInvalid(test *testNode, t *testing.T) {\n\troot, err := Unmarshal(test.input)\n\tif err == nil {\n\t\tt.Errorf(\"Error on Unmarshal(%s): error expected, got '%s'\", test.name, root.source())\n\t} else if root != nil {\n\t\tt.Errorf(\"Error on Unmarshal(%s): root is not nil\", test.name)\n\t}\n}\n\nfunc simpleCorrupted(name string) *testNode {\n\treturn \u0026testNode{name: name, input: []byte(name)}\n}\n\nfunc TestUnmarshal_StringSimpleSuccess(t *testing.T) {\n\ttests := []*testNode{\n\t\t{name: \"blank\", input: []byte(\"\\\"\\\"\"), _type: String, value: []byte(\"\\\"\\\"\")},\n\t\t{name: \"char\", input: []byte(\"\\\"c\\\"\"), _type: String, value: []byte(\"\\\"c\\\"\")},\n\t\t{name: \"word\", input: []byte(\"\\\"cat\\\"\"), _type: String, value: []byte(\"\\\"cat\\\"\")},\n\t\t{name: \"spaces\", input: []byte(\"  \\\"good cat or dog\\\"\\r\\n \"), _type: String, value: []byte(\"\\\"good cat or dog\\\"\")},\n\t\t{name: \"backslash\", input: []byte(\"\\\"good \\\\\\\"cat\\\\\\\"\\\"\"), _type: String, value: []byte(\"\\\"good \\\\\\\"cat\\\\\\\"\\\"\")},\n\t\t{name: \"backslash 2\", input: []byte(\"\\\"good \\\\\\\\\\\\\\\"cat\\\\\\\"\\\"\"), _type: String, value: []byte(\"\\\"good \\\\\\\\\\\\\\\"cat\\\\\\\"\\\"\")},\n\t}\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleValid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_NumericSimpleSuccess(t *testing.T) {\n\ttests := []*testNode{\n\t\t{name: \"1\", input: []byte(\"1\"), _type: Number, value: []byte(\"1\")},\n\t\t{name: \"-1\", input: []byte(\"-1\"), _type: Number, value: []byte(\"-1\")},\n\n\t\t{name: \"1234567890\", input: []byte(\"1234567890\"), _type: Number, value: []byte(\"1234567890\")},\n\t\t{name: \"-123\", input: []byte(\"-123\"), _type: Number, value: []byte(\"-123\")},\n\n\t\t{name: \"123.456\", input: []byte(\"123.456\"), _type: Number, value: []byte(\"123.456\")},\n\t\t{name: \"-123.456\", input: []byte(\"-123.456\"), _type: Number, value: []byte(\"-123.456\")},\n\n\t\t{name: \"1e3\", input: []byte(\"1e3\"), _type: Number, value: []byte(\"1e3\")},\n\t\t{name: \"1e+3\", input: []byte(\"1e+3\"), _type: Number, value: []byte(\"1e+3\")},\n\t\t{name: \"1e-3\", input: []byte(\"1e-3\"), _type: Number, value: []byte(\"1e-3\")},\n\t\t{name: \"-1e3\", input: []byte(\"-1e3\"), _type: Number, value: []byte(\"-1e3\")},\n\t\t{name: \"-1e-3\", input: []byte(\"-1e-3\"), _type: Number, value: []byte(\"-1e-3\")},\n\n\t\t{name: \"1.123e3456\", input: []byte(\"1.123e3456\"), _type: Number, value: []byte(\"1.123e3456\")},\n\t\t{name: \"1.123e-3456\", input: []byte(\"1.123e-3456\"), _type: Number, value: []byte(\"1.123e-3456\")},\n\t\t{name: \"-1.123e3456\", input: []byte(\"-1.123e3456\"), _type: Number, value: []byte(\"-1.123e3456\")},\n\t\t{name: \"-1.123e-3456\", input: []byte(\"-1.123e-3456\"), _type: Number, value: []byte(\"-1.123e-3456\")},\n\n\t\t{name: \"1E3\", input: []byte(\"1E3\"), _type: Number, value: []byte(\"1E3\")},\n\t\t{name: \"1E-3\", input: []byte(\"1E-3\"), _type: Number, value: []byte(\"1E-3\")},\n\t\t{name: \"-1E3\", input: []byte(\"-1E3\"), _type: Number, value: []byte(\"-1E3\")},\n\t\t{name: \"-1E-3\", input: []byte(\"-1E-3\"), _type: Number, value: []byte(\"-1E-3\")},\n\n\t\t{name: \"1.123E3456\", input: []byte(\"1.123E3456\"), _type: Number, value: []byte(\"1.123E3456\")},\n\t\t{name: \"1.123E-3456\", input: []byte(\"1.123E-3456\"), _type: Number, value: []byte(\"1.123E-3456\")},\n\t\t{name: \"-1.123E3456\", input: []byte(\"-1.123E3456\"), _type: Number, value: []byte(\"-1.123E3456\")},\n\t\t{name: \"-1.123E-3456\", input: []byte(\"-1.123E-3456\"), _type: Number, value: []byte(\"-1.123E-3456\")},\n\n\t\t{name: \"-1.123E-3456 with spaces\", input: []byte(\" \\r -1.123E-3456 \\t\\n\"), _type: Number, value: []byte(\"-1.123E-3456\")},\n\t}\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\troot, err := Unmarshal(test.input)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"Error on Unmarshal(%s): %s\", test.name, err.Error())\n\t\t\t} else if root == nil {\n\t\t\t\tt.Errorf(\"Error on Unmarshal(%s): root is nil\", test.name)\n\t\t\t} else if root.nodeType != test._type {\n\t\t\t\tt.Errorf(\"Error on Unmarshal(%s): wrong type\", test.name)\n\t\t\t} else if !bytes.Equal(root.source(), test.value) {\n\t\t\t\tt.Errorf(\"Error on Unmarshal(%s): %s != %s\", test.name, root.source(), test.value)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_StringSimpleCorrupted(t *testing.T) {\n\ttests := []*testNode{\n\t\t{name: \"white NL\", input: []byte(\"\\\"foo\\nbar\\\"\")},\n\t\t{name: \"white R\", input: []byte(\"\\\"foo\\rbar\\\"\")},\n\t\t{name: \"white Tab\", input: []byte(\"\\\"foo\\tbar\\\"\")},\n\t\t{name: \"wrong quotes\", input: []byte(\"'cat'\")},\n\t\t{name: \"double string\", input: []byte(\"\\\"Hello\\\" \\\"World\\\"\")},\n\t\t{name: \"quotes in quotes\", input: []byte(\"\\\"good \\\"cat\\\"\\\"\")},\n\t}\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleInvalid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_ObjectSimpleSuccess(t *testing.T) {\n\ttests := []*testNode{\n\t\t{name: \"{}\", input: []byte(\"{}\"), _type: Object, value: []byte(\"{}\")},\n\t\t{name: `{ \\r\\n }`, input: []byte(\"{ \\r\\n }\"), _type: Object, value: []byte(\"{ \\r\\n }\")},\n\t\t{name: `{\"key\":1}`, input: []byte(`{\"key\":1}`), _type: Object, value: []byte(`{\"key\":1}`)},\n\t\t{name: `{\"key\":true}`, input: []byte(`{\"key\":true}`), _type: Object, value: []byte(`{\"key\":true}`)},\n\t\t{name: `{\"key\":\"value\"}`, input: []byte(`{\"key\":\"value\"}`), _type: Object, value: []byte(`{\"key\":\"value\"}`)},\n\t\t{name: `{\"foo\":\"bar\",\"baz\":\"foo\"}`, input: []byte(`{\"foo\":\"bar\", \"baz\":\"foo\"}`), _type: Object, value: []byte(`{\"foo\":\"bar\", \"baz\":\"foo\"}`)},\n\t\t{name: \"spaces\", input: []byte(`  {  \"foo\"  :  \"bar\"  , \"baz\"   :   \"foo\"   }    `), _type: Object, value: []byte(`{  \"foo\"  :  \"bar\"  , \"baz\"   :   \"foo\"   }`)},\n\t\t{name: \"nested\", input: []byte(`{\"foo\":{\"bar\":{\"baz\":{}}}}`), _type: Object, value: []byte(`{\"foo\":{\"bar\":{\"baz\":{}}}}`)},\n\t\t{name: \"array\", input: []byte(`{\"array\":[{},{},{\"foo\":[{\"bar\":[\"baz\"]}]}]}`), _type: Object, value: []byte(`{\"array\":[{},{},{\"foo\":[{\"bar\":[\"baz\"]}]}]}`)},\n\t}\n\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleValid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_ObjectSimpleCorrupted(t *testing.T) {\n\ttests := []*testNode{\n\t\tsimpleCorrupted(\"{{{\\\"key\\\": \\\"foo\\\"{{{{\"),\n\t\tsimpleCorrupted(\"}\"),\n\t\tsimpleCorrupted(\"{ }}}}}}}\"),\n\t\tsimpleCorrupted(\" }\"),\n\t\tsimpleCorrupted(\"{,}\"),\n\t\tsimpleCorrupted(\"{:}\"),\n\t\tsimpleCorrupted(\"{100000}\"),\n\t\tsimpleCorrupted(\"{1:1}\"),\n\t\tsimpleCorrupted(\"{'1:2,3:4'}\"),\n\t\tsimpleCorrupted(`{\"d\"}`),\n\t\tsimpleCorrupted(`{\"foo\"}`),\n\t\tsimpleCorrupted(`{\"foo\":}`),\n\t\tsimpleCorrupted(`{:\"foo\"}`),\n\t\tsimpleCorrupted(`{\"foo\":bar}`),\n\t\tsimpleCorrupted(`{\"foo\":\"bar\",}`),\n\t\tsimpleCorrupted(`{}{}`),\n\t\tsimpleCorrupted(`{},{}`),\n\t\tsimpleCorrupted(`{[},{]}`),\n\t\tsimpleCorrupted(`{[,]}`),\n\t\tsimpleCorrupted(`{[]}`),\n\t\tsimpleCorrupted(`{}1`),\n\t\tsimpleCorrupted(`1{}`),\n\t\tsimpleCorrupted(`{\"x\"::1}`),\n\t\tsimpleCorrupted(`{null:null}`),\n\t\tsimpleCorrupted(`{\"foo:\"bar\"}`),\n\t}\n\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleInvalid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_NullSimpleCorrupted(t *testing.T) {\n\ttests := []*testNode{\n\t\t{name: \"nul\", input: []byte(\"nul\")},\n\t\t{name: \"nil\", input: []byte(\"nil\")},\n\t\t{name: \"nill\", input: []byte(\"nill\")},\n\t\t{name: \"NILL\", input: []byte(\"NILL\")},\n\t\t{name: \"Null\", input: []byte(\"Null\")},\n\t\t{name: \"NULL\", input: []byte(\"NULL\")},\n\t\t{name: \"spaces\", input: []byte(\"Nu ll\")},\n\t\t{name: \"null1\", input: []byte(\"null1\")},\n\t\t{name: \"double\", input: []byte(\"null null\")},\n\t}\n\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleInvalid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_BoolSimpleSuccess(t *testing.T) {\n\ttests := []*testNode{\n\t\t{name: \"lower true\", input: []byte(\"true\"), _type: Boolean, value: []byte(\"true\")},\n\t\t{name: \"lower false\", input: []byte(\"false\"), _type: Boolean, value: []byte(\"false\")},\n\t\t{name: \"spaces true\", input: []byte(\"  true\\r\\n \"), _type: Boolean, value: []byte(\"true\")},\n\t\t{name: \"spaces false\", input: []byte(\"  false\\r\\n \"), _type: Boolean, value: []byte(\"false\")},\n\t}\n\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleValid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_BoolSimpleCorrupted(t *testing.T) {\n\ttests := []*testNode{\n\t\tsimpleCorrupted(\"tru\"),\n\t\tsimpleCorrupted(\"fals\"),\n\t\tsimpleCorrupted(\"tre\"),\n\t\tsimpleCorrupted(\"fal se\"),\n\t\tsimpleCorrupted(\"true false\"),\n\t\tsimpleCorrupted(\"True\"),\n\t\tsimpleCorrupted(\"TRUE\"),\n\t\tsimpleCorrupted(\"False\"),\n\t\tsimpleCorrupted(\"FALSE\"),\n\t}\n\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleInvalid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_ArraySimpleSuccess(t *testing.T) {\n\ttests := []*testNode{\n\t\t{name: \"[]\", input: []byte(\"[]\"), _type: Array, value: []byte(\"[]\")},\n\t\t{name: \"[1]\", input: []byte(\"[1]\"), _type: Array, value: []byte(\"[1]\")},\n\t\t{name: \"[1,2,3]\", input: []byte(\"[1,2,3]\"), _type: Array, value: []byte(\"[1,2,3]\")},\n\t\t{name: \"[1, 2, 3]\", input: []byte(\"[1, 2, 3]\"), _type: Array, value: []byte(\"[1, 2, 3]\")},\n\t\t{name: \"[1,[2],3]\", input: []byte(\"[1,[2],3]\"), _type: Array, value: []byte(\"[1,[2],3]\")},\n\t\t{name: \"[[],[],[]]\", input: []byte(\"[[],[],[]]\"), _type: Array, value: []byte(\"[[],[],[]]\")},\n\t\t{name: \"[[[[[]]]]]\", input: []byte(\"[[[[[]]]]]\"), _type: Array, value: []byte(\"[[[[[]]]]]\")},\n\t\t{name: \"[true,null,1,\\\"foo\\\",[]]\", input: []byte(\"[true,null,1,\\\"foo\\\",[]]\"), _type: Array, value: []byte(\"[true,null,1,\\\"foo\\\",[]]\")},\n\t\t{name: \"spaces\", input: []byte(\"\\n\\r [\\n1\\n ]\\r\\n\"), _type: Array, value: []byte(\"[\\n1\\n ]\")},\n\t}\n\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleValid(test, t)\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_ArraySimpleCorrupted(t *testing.T) {\n\ttests := []*testNode{\n\t\tsimpleCorrupted(\"[,]\"),\n\t\tsimpleCorrupted(\"[]\\\\\"),\n\t\tsimpleCorrupted(\"[1,]\"),\n\t\tsimpleCorrupted(\"[[]\"),\n\t\tsimpleCorrupted(\"[]]\"),\n\t\tsimpleCorrupted(\"1[]\"),\n\t\tsimpleCorrupted(\"[]1\"),\n\t\tsimpleCorrupted(\"[[]1]\"),\n\t}\n\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tsimpleInvalid(test, t)\n\t\t})\n\t}\n}\n\n// Examples from https://json.org/example.html\nfunc TestUnmarshal(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tvalue string\n\t}{\n\t\t{\n\t\t\tname: \"glossary\",\n\t\t\tvalue: `{\n\t\t\t\t\"glossary\": {\n\t\t\t\t\t\"title\": \"example glossary\",\n\t\t\t\t\t\"GlossDiv\": {\n\t\t\t\t\t\t\"title\": \"S\",\n\t\t\t\t\t\t\"GlossList\": {\n\t\t\t\t\t\t\t\"GlossEntry\": {\n\t\t\t\t\t\t\t\t\"ID\": \"SGML\",\n\t\t\t\t\t\t\t\t\"SortAs\": \"SGML\",\n\t\t\t\t\t\t\t\t\"GlossTerm\": \"Standard Generalized Markup Language\",\n\t\t\t\t\t\t\t\t\"Acronym\": \"SGML\",\n\t\t\t\t\t\t\t\t\"Abbrev\": \"ISO 8879:1986\",\n\t\t\t\t\t\t\t\t\"GlossDef\": {\n\t\t\t\t\t\t\t\t\t\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n\t\t\t\t\t\t\t\t\t\"GlossSeeAlso\": [\"GML\", \"XML\"]\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\"GlossSee\": \"markup\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}`,\n\t\t},\n\t\t{\n\t\t\tname: \"menu\",\n\t\t\tvalue: `{\"menu\": {\n\t\t\t\t\"id\": \"file\",\n\t\t\t\t\"value\": \"File\",\n\t\t\t\t\"popup\": {\n\t\t\t\t  \"menuitem\": [\n\t\t\t\t\t{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n\t\t\t\t\t{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n\t\t\t\t\t{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n\t\t\t\t  ]\n\t\t\t\t}\n\t\t\t}}`,\n\t\t},\n\t\t{\n\t\t\tname: \"widget\",\n\t\t\tvalue: `{\"widget\": {\n\t\t\t\t\"debug\": \"on\",\n\t\t\t\t\"window\": {\n\t\t\t\t\t\"title\": \"Sample Konfabulator Widget\",\n\t\t\t\t\t\"name\": \"main_window\",\n\t\t\t\t\t\"width\": 500,\n\t\t\t\t\t\"height\": 500\n\t\t\t\t},\n\t\t\t\t\"image\": { \n\t\t\t\t\t\"src\": \"Images/Sun.png\",\n\t\t\t\t\t\"name\": \"sun1\",\n\t\t\t\t\t\"hOffset\": 250,\n\t\t\t\t\t\"vOffset\": 250,\n\t\t\t\t\t\"alignment\": \"center\"\n\t\t\t\t},\n\t\t\t\t\"text\": {\n\t\t\t\t\t\"data\": \"Click Here\",\n\t\t\t\t\t\"size\": 36,\n\t\t\t\t\t\"style\": \"bold\",\n\t\t\t\t\t\"name\": \"text1\",\n\t\t\t\t\t\"hOffset\": 250,\n\t\t\t\t\t\"vOffset\": 100,\n\t\t\t\t\t\"alignment\": \"center\",\n\t\t\t\t\t\"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"\n\t\t\t\t}\n\t\t\t}}    `,\n\t\t},\n\t\t{\n\t\t\tname: \"web-app\",\n\t\t\tvalue: `{\"web-app\": {\n\t\t\t\t\"servlet\": [   \n\t\t\t\t  {\n\t\t\t\t\t\"servlet-name\": \"cofaxCDS\",\n\t\t\t\t\t\"servlet-class\": \"org.cofax.cds.CDSServlet\",\n\t\t\t\t\t\"init-param\": {\n\t\t\t\t\t  \"configGlossary:installationAt\": \"Philadelphia, PA\",\n\t\t\t\t\t  \"configGlossary:adminEmail\": \"ksm@pobox.com\",\n\t\t\t\t\t  \"configGlossary:poweredBy\": \"Cofax\",\n\t\t\t\t\t  \"configGlossary:poweredByIcon\": \"/images/cofax.gif\",\n\t\t\t\t\t  \"configGlossary:staticPath\": \"/content/static\",\n\t\t\t\t\t  \"templateProcessorClass\": \"org.cofax.WysiwygTemplate\",\n\t\t\t\t\t  \"templateLoaderClass\": \"org.cofax.FilesTemplateLoader\",\n\t\t\t\t\t  \"templatePath\": \"templates\",\n\t\t\t\t\t  \"templateOverridePath\": \"\",\n\t\t\t\t\t  \"defaultListTemplate\": \"listTemplate.htm\",\n\t\t\t\t\t  \"defaultFileTemplate\": \"articleTemplate.htm\",\n\t\t\t\t\t  \"useJSP\": false,\n\t\t\t\t\t  \"jspListTemplate\": \"listTemplate.jsp\",\n\t\t\t\t\t  \"jspFileTemplate\": \"articleTemplate.jsp\",\n\t\t\t\t\t  \"cachePackageTagsTrack\": 200,\n\t\t\t\t\t  \"cachePackageTagsStore\": 200,\n\t\t\t\t\t  \"cachePackageTagsRefresh\": 60,\n\t\t\t\t\t  \"cacheTemplatesTrack\": 100,\n\t\t\t\t\t  \"cacheTemplatesStore\": 50,\n\t\t\t\t\t  \"cacheTemplatesRefresh\": 15,\n\t\t\t\t\t  \"cachePagesTrack\": 200,\n\t\t\t\t\t  \"cachePagesStore\": 100,\n\t\t\t\t\t  \"cachePagesRefresh\": 10,\n\t\t\t\t\t  \"cachePagesDirtyRead\": 10,\n\t\t\t\t\t  \"searchEngineListTemplate\": \"forSearchEnginesList.htm\",\n\t\t\t\t\t  \"searchEngineFileTemplate\": \"forSearchEngines.htm\",\n\t\t\t\t\t  \"searchEngineRobotsDb\": \"WEB-INF/robots.db\",\n\t\t\t\t\t  \"useDataStore\": true,\n\t\t\t\t\t  \"dataStoreClass\": \"org.cofax.SqlDataStore\",\n\t\t\t\t\t  \"redirectionClass\": \"org.cofax.SqlRedirection\",\n\t\t\t\t\t  \"dataStoreName\": \"cofax\",\n\t\t\t\t\t  \"dataStoreDriver\": \"com.microsoft.jdbc.sqlserver.SQLServerDriver\",\n\t\t\t\t\t  \"dataStoreUrl\": \"jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon\",\n\t\t\t\t\t  \"dataStoreUser\": \"sa\",\n\t\t\t\t\t  \"dataStorePassword\": \"dataStoreTestQuery\",\n\t\t\t\t\t  \"dataStoreTestQuery\": \"SET NOCOUNT ON;select test='test';\",\n\t\t\t\t\t  \"dataStoreLogFile\": \"/usr/local/tomcat/logs/datastore.log\",\n\t\t\t\t\t  \"dataStoreInitConns\": 10,\n\t\t\t\t\t  \"dataStoreMaxConns\": 100,\n\t\t\t\t\t  \"dataStoreConnUsageLimit\": 100,\n\t\t\t\t\t  \"dataStoreLogLevel\": \"debug\",\n\t\t\t\t\t  \"maxUrlLength\": 500}},\n\t\t\t\t  {\n\t\t\t\t\t\"servlet-name\": \"cofaxEmail\",\n\t\t\t\t\t\"servlet-class\": \"org.cofax.cds.EmailServlet\",\n\t\t\t\t\t\"init-param\": {\n\t\t\t\t\t\"mailHost\": \"mail1\",\n\t\t\t\t\t\"mailHostOverride\": \"mail2\"}},\n\t\t\t\t  {\n\t\t\t\t\t\"servlet-name\": \"cofaxAdmin\",\n\t\t\t\t\t\"servlet-class\": \"org.cofax.cds.AdminServlet\"},\n\t\t\t   \n\t\t\t\t  {\n\t\t\t\t\t\"servlet-name\": \"fileServlet\",\n\t\t\t\t\t\"servlet-class\": \"org.cofax.cds.FileServlet\"},\n\t\t\t\t  {\n\t\t\t\t\t\"servlet-name\": \"cofaxTools\",\n\t\t\t\t\t\"servlet-class\": \"org.cofax.cms.CofaxToolsServlet\",\n\t\t\t\t\t\"init-param\": {\n\t\t\t\t\t  \"templatePath\": \"toolstemplates/\",\n\t\t\t\t\t  \"log\": 1,\n\t\t\t\t\t  \"logLocation\": \"/usr/local/tomcat/logs/CofaxTools.log\",\n\t\t\t\t\t  \"logMaxSize\": \"\",\n\t\t\t\t\t  \"dataLog\": 1,\n\t\t\t\t\t  \"dataLogLocation\": \"/usr/local/tomcat/logs/dataLog.log\",\n\t\t\t\t\t  \"dataLogMaxSize\": \"\",\n\t\t\t\t\t  \"removePageCache\": \"/content/admin/remove?cache=pages\u0026id=\",\n\t\t\t\t\t  \"removeTemplateCache\": \"/content/admin/remove?cache=templates\u0026id=\",\n\t\t\t\t\t  \"fileTransferFolder\": \"/usr/local/tomcat/webapps/content/fileTransferFolder\",\n\t\t\t\t\t  \"lookInContext\": 1,\n\t\t\t\t\t  \"adminGroupID\": 4,\n\t\t\t\t\t  \"betaServer\": true}}],\n\t\t\t\t\"servlet-mapping\": {\n\t\t\t\t  \"cofaxCDS\": \"/\",\n\t\t\t\t  \"cofaxEmail\": \"/cofaxutil/aemail/*\",\n\t\t\t\t  \"cofaxAdmin\": \"/admin/*\",\n\t\t\t\t  \"fileServlet\": \"/static/*\",\n\t\t\t\t  \"cofaxTools\": \"/tools/*\"},\n\t\t\t   \n\t\t\t\t\"taglib\": {\n\t\t\t\t  \"taglib-uri\": \"cofax.tld\",\n\t\t\t\t  \"taglib-location\": \"/WEB-INF/tlds/cofax.tld\"}}}`,\n\t\t},\n\t\t{\n\t\t\tname: \"SVG Viewer\",\n\t\t\tvalue: `{\"menu\": {\n\t\t\t\t\"header\": \"SVG Viewer\",\n\t\t\t\t\"items\": [\n\t\t\t\t\t{\"id\": \"Open\"},\n\t\t\t\t\t{\"id\": \"OpenNew\", \"label\": \"Open New\"},\n\t\t\t\t\tnull,\n\t\t\t\t\t{\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},\n\t\t\t\t\t{\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},\n\t\t\t\t\t{\"id\": \"OriginalView\", \"label\": \"Original View\"},\n\t\t\t\t\tnull,\n\t\t\t\t\t{\"id\": \"Quality\"},\n\t\t\t\t\t{\"id\": \"Pause\"},\n\t\t\t\t\t{\"id\": \"Mute\"},\n\t\t\t\t\tnull,\n\t\t\t\t\t{\"id\": \"Find\", \"label\": \"Find...\"},\n\t\t\t\t\t{\"id\": \"FindAgain\", \"label\": \"Find Again\"},\n\t\t\t\t\t{\"id\": \"Copy\"},\n\t\t\t\t\t{\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},\n\t\t\t\t\t{\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},\n\t\t\t\t\t{\"id\": \"ViewSVG\", \"label\": \"View SVG\"},\n\t\t\t\t\t{\"id\": \"ViewSource\", \"label\": \"View Source\"},\n\t\t\t\t\t{\"id\": \"SaveAs\", \"label\": \"Save As\"},\n\t\t\t\t\tnull,\n\t\t\t\t\t{\"id\": \"Help\"},\n\t\t\t\t\t{\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}\n\t\t\t\t]\n\t\t\t}}`,\n\t\t},\n\t}\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\t_, err := Unmarshal([]byte(test.value))\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"Error on Unmarshal: %s\", err.Error())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestUnmarshalSafe(t *testing.T) {\n\tjson := []byte(`{ \"store\": {\n\t\t\"book\": [ \n\t\t  { \"category\": \"reference\",\n\t\t\t\"author\": \"Nigel Rees\",\n\t\t\t\"title\": \"Sayings of the Century\",\n\t\t\t\"price\": 8.95\n\t\t  },\n\t\t  { \"category\": \"fiction\",\n\t\t\t\"author\": \"Evelyn Waugh\",\n\t\t\t\"title\": \"Sword of Honour\",\n\t\t\t\"price\": 12.99\n\t\t  },\n\t\t  { \"category\": \"fiction\",\n\t\t\t\"author\": \"Herman Melville\",\n\t\t\t\"title\": \"Moby Dick\",\n\t\t\t\"isbn\": \"0-553-21311-3\",\n\t\t\t\"price\": 8.99\n\t\t  },\n\t\t  { \"category\": \"fiction\",\n\t\t\t\"author\": \"J. R. R. Tolkien\",\n\t\t\t\"title\": \"The Lord of the Rings\",\n\t\t\t\"isbn\": \"0-395-19395-8\",\n\t\t\t\"price\": 22.99\n\t\t  }\n\t\t],\n\t\t\"bicycle\": {\n\t\t  \"color\": \"red\",\n\t\t  \"price\": 19.95\n\t\t}\n\t  }\n\t}`)\n\tsafe, err := UnmarshalSafe(json)\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal: %s\", err.Error())\n\t} else if safe == nil {\n\t\tt.Errorf(\"Error on Unmarshal: safe is nil\")\n\t} else {\n\t\troot, err := Unmarshal(json)\n\t\tif err != nil {\n\t\t\tt.Errorf(\"Error on Unmarshal: %s\", err.Error())\n\t\t} else if root == nil {\n\t\t\tt.Errorf(\"Error on Unmarshal: root is nil\")\n\t\t} else if !bytes.Equal(root.source(), safe.source()) {\n\t\t\tt.Errorf(\"Error on UnmarshalSafe: values not same\")\n\t\t}\n\t}\n}\n\n// BenchmarkGoStdUnmarshal-8   \t   61698\t     19350 ns/op\t     288 B/op\t       6 allocs/op\n// BenchmarkUnmarshal-8        \t   45620\t     26165 ns/op\t   21889 B/op\t     367 allocs/op\n//\n// type bench struct {\n// \tName  string `json:\"name\"`\n// \tValue int    `json:\"value\"`\n// }\n\n// func BenchmarkGoStdUnmarshal(b *testing.B) {\n// \tdata := []byte(webApp)\n// \tfor i := 0; i \u003c b.N; i++ {\n// \t\terr := json.Unmarshal(data, \u0026bench{})\n// \t\tif err != nil {\n// \t\t\tb.Fatal(err)\n// \t\t}\n// \t}\n// }\n\n// func BenchmarkUnmarshal(b *testing.B) {\n// \tdata := []byte(webApp)\n// \tfor i := 0; i \u003c b.N; i++ {\n// \t\t_, err := Unmarshal(data)\n// \t\tif err != nil {\n// \t\t\tb.Fatal(err)\n// \t\t}\n// \t}\n// }\n"},{"name":"encode.gno","body":"package json\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Marshal returns the JSON encoding of a Node.\nfunc Marshal(node *Node) ([]byte, error) {\n\tvar (\n\t\tbuf  bytes.Buffer\n\t\tsVal string\n\t\tbVal bool\n\t\tnVal float64\n\t\toVal []byte\n\t\terr  error\n\t)\n\n\tif node == nil {\n\t\treturn nil, errors.New(\"node is nil\")\n\t}\n\n\tif !node.modified \u0026\u0026 !node.ready() {\n\t\treturn nil, errors.New(\"node is not ready\")\n\t}\n\n\tif !node.modified \u0026\u0026 node.ready() {\n\t\tbuf.Write(node.source())\n\t}\n\n\tif node.modified {\n\t\tswitch node.nodeType {\n\t\tcase Null:\n\t\t\tbuf.Write(nullLiteral)\n\n\t\tcase Number:\n\t\t\tnVal, err = node.GetNumeric()\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tnum := strconv.FormatFloat(nVal, 'f', -1, 64)\n\t\t\tbuf.WriteString(num)\n\n\t\tcase String:\n\t\t\tsVal, err = node.GetString()\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tquoted := ufmt.Sprintf(\"%s\", strconv.Quote(sVal))\n\t\t\tbuf.WriteString(quoted)\n\n\t\tcase Boolean:\n\t\t\tbVal, err = node.GetBool()\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tbStr := ufmt.Sprintf(\"%t\", bVal)\n\t\t\tbuf.WriteString(bStr)\n\n\t\tcase Array:\n\t\t\tbuf.WriteByte(bracketOpen)\n\n\t\t\tfor i := 0; i \u003c len(node.next); i++ {\n\t\t\t\tif i != 0 {\n\t\t\t\t\tbuf.WriteByte(comma)\n\t\t\t\t}\n\n\t\t\t\telem, ok := node.next[strconv.Itoa(i)]\n\t\t\t\tif !ok {\n\t\t\t\t\treturn nil, ufmt.Errorf(\"array element %d is not found\", i)\n\t\t\t\t}\n\n\t\t\t\toVal, err = Marshal(elem)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\tbuf.Write(oVal)\n\t\t\t}\n\n\t\t\tbuf.WriteByte(bracketClose)\n\n\t\tcase Object:\n\t\t\tbuf.WriteByte(curlyOpen)\n\n\t\t\tbVal = false\n\t\t\tfor k, v := range node.next {\n\t\t\t\tif bVal {\n\t\t\t\t\tbuf.WriteByte(comma)\n\t\t\t\t} else {\n\t\t\t\t\tbVal = true\n\t\t\t\t}\n\n\t\t\t\tkey := ufmt.Sprintf(\"%s\", strconv.Quote(k))\n\t\t\t\tbuf.WriteString(key)\n\t\t\t\tbuf.WriteByte(colon)\n\n\t\t\t\toVal, err = Marshal(v)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\tbuf.Write(oVal)\n\t\t\t}\n\n\t\t\tbuf.WriteByte(curlyClose)\n\t\t}\n\t}\n\n\treturn buf.Bytes(), nil\n}\n"},{"name":"encode_test.gno","body":"package json\n\nimport \"testing\"\n\nfunc TestMarshal_Primitive(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tnode *Node\n\t}{\n\t\t{\n\t\t\tname: \"null\",\n\t\t\tnode: NullNode(\"\"),\n\t\t},\n\t\t{\n\t\t\tname: \"true\",\n\t\t\tnode: BoolNode(\"\", true),\n\t\t},\n\t\t{\n\t\t\tname: \"false\",\n\t\t\tnode: BoolNode(\"\", false),\n\t\t},\n\t\t{\n\t\t\tname: `\"string\"`,\n\t\t\tnode: StringNode(\"\", \"string\"),\n\t\t},\n\t\t{\n\t\t\tname: `\"one \\\"encoded\\\" string\"`,\n\t\t\tnode: StringNode(\"\", `one \"encoded\" string`),\n\t\t},\n\t\t{\n\t\t\tname: `{\"foo\":\"bar\"}`,\n\t\t\tnode: ObjectNode(\"\", map[string]*Node{\n\t\t\t\t\"foo\": StringNode(\"foo\", \"bar\"),\n\t\t\t}),\n\t\t},\n\t\t{\n\t\t\tname: \"42\",\n\t\t\tnode: NumberNode(\"\", 42),\n\t\t},\n\t\t{\n\t\t\tname: \"3.14\",\n\t\t\tnode: NumberNode(\"\", 3.14),\n\t\t},\n\t\t{\n\t\t\tname: `[1,2,3]`,\n\t\t\tnode: ArrayNode(\"\", []*Node{\n\t\t\t\tNumberNode(\"0\", 1),\n\t\t\t\tNumberNode(\"2\", 2),\n\t\t\t\tNumberNode(\"3\", 3),\n\t\t\t}),\n\t\t},\n\t}\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tvalue, err := Marshal(test.node)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"unexpected error: %s\", err)\n\t\t\t} else if string(value) != test.name {\n\t\t\t\tt.Errorf(\"wrong result: '%s', expected '%s'\", value, test.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMarshal_Object(t *testing.T) {\n\tnode := ObjectNode(\"\", map[string]*Node{\n\t\t\"foo\": StringNode(\"foo\", \"bar\"),\n\t\t\"baz\": NumberNode(\"baz\", 100500),\n\t\t\"qux\": NullNode(\"qux\"),\n\t})\n\n\tmustKey := []string{\"foo\", \"baz\", \"qux\"}\n\n\tvalue, err := Marshal(node)\n\tif err != nil {\n\t\tt.Errorf(\"unexpected error: %s\", err)\n\t}\n\n\t// the order of keys in the map is not guaranteed\n\t// so we need to unmarshal the result and check the keys\n\tdecoded, err := Unmarshal(value)\n\tif err != nil {\n\t\tt.Errorf(\"unexpected error: %s\", err)\n\t}\n\n\tfor _, key := range mustKey {\n\t\tif node, err := decoded.GetKey(key); err != nil {\n\t\t\tt.Errorf(\"unexpected error: %s\", err)\n\t\t} else {\n\t\t\tif node == nil {\n\t\t\t\tt.Errorf(\"node is nil\")\n\t\t\t} else if node.key == nil {\n\t\t\t\tt.Errorf(\"key is nil\")\n\t\t\t} else if *node.key != key {\n\t\t\t\tt.Errorf(\"wrong key: '%s', expected '%s'\", *node.key, key)\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc valueNode(prev *Node, key string, typ ValueType, val any) *Node {\n\tcurr := \u0026Node{\n\t\tprev:     prev,\n\t\tdata:     nil,\n\t\tkey:      \u0026key,\n\t\tborders:  [2]int{0, 0},\n\t\tvalue:    val,\n\t\tmodified: true,\n\t}\n\n\tif val != nil {\n\t\tcurr.nodeType = typ\n\t}\n\n\treturn curr\n}\n\nfunc TestMarshal_Errors(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tnode func() (node *Node)\n\t}{\n\t\t{\n\t\t\tname: \"nil\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\treturn\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"broken\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\tnode = Must(Unmarshal([]byte(`{}`)))\n\t\t\t\tnode.borders[1] = 0\n\t\t\t\treturn\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"Numeric\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\treturn valueNode(nil, \"\", Number, false)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"String\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\treturn valueNode(nil, \"\", String, false)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"Bool\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\treturn valueNode(nil, \"\", Boolean, 1)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"Array_1\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\tnode = ArrayNode(\"\", nil)\n\t\t\t\tnode.next[\"1\"] = NullNode(\"1\")\n\t\t\t\treturn\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"Array_2\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\treturn ArrayNode(\"\", []*Node{valueNode(nil, \"\", Boolean, 1)})\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"Object\",\n\t\t\tnode: func() (node *Node) {\n\t\t\t\treturn ObjectNode(\"\", map[string]*Node{\"key\": valueNode(nil, \"key\", Boolean, 1)})\n\t\t\t},\n\t\t},\n\t}\n\tfor _, test := range tests {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\tvalue, err := Marshal(test.node())\n\t\t\tif err == nil {\n\t\t\t\tt.Errorf(\"expected error\")\n\t\t\t} else if len(value) != 0 {\n\t\t\t\tt.Errorf(\"wrong result\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMarshal_Nil(t *testing.T) {\n\t_, err := Marshal(nil)\n\tif err == nil {\n\t\tt.Error(\"Expected error for nil node, but got nil\")\n\t}\n}\n\nfunc TestMarshal_NotModified(t *testing.T) {\n\tnode := \u0026Node{}\n\t_, err := Marshal(node)\n\tif err == nil {\n\t\tt.Error(\"Expected error for not modified node, but got nil\")\n\t}\n}\n\nfunc TestMarshalCycleReference(t *testing.T) {\n\tnode1 := \u0026Node{\n\t\tkey:      stringPtr(\"node1\"),\n\t\tnodeType: String,\n\t\tnext: map[string]*Node{\n\t\t\t\"next\": nil,\n\t\t},\n\t}\n\n\tnode2 := \u0026Node{\n\t\tkey:      stringPtr(\"node2\"),\n\t\tnodeType: String,\n\t\tprev:     node1,\n\t}\n\n\tnode1.next[\"next\"] = node2\n\n\t_, err := Marshal(node1)\n\tif err == nil {\n\t\tt.Error(\"Expected error for cycle reference, but got nil\")\n\t}\n}\n\nfunc TestMarshalNoCycleReference(t *testing.T) {\n\tnode1 := \u0026Node{\n\t\tkey:      stringPtr(\"node1\"),\n\t\tnodeType: String,\n\t\tvalue:    \"value1\",\n\t\tmodified: true,\n\t}\n\n\tnode2 := \u0026Node{\n\t\tkey:      stringPtr(\"node2\"),\n\t\tnodeType: String,\n\t\tvalue:    \"value2\",\n\t\tmodified: true,\n\t}\n\n\t_, err := Marshal(node1)\n\tif err != nil {\n\t\tt.Errorf(\"Unexpected error: %v\", err)\n\t}\n\n\t_, err = Marshal(node2)\n\tif err != nil {\n\t\tt.Errorf(\"Unexpected error: %v\", err)\n\t}\n}\n\nfunc stringPtr(s string) *string {\n\treturn \u0026s\n}\n"},{"name":"errors.gno","body":"package json\n\nimport \"errors\"\n\nvar (\n\terrNilNode               = errors.New(\"node is nil\")\n\terrNotArrayNode          = errors.New(\"node is not array\")\n\terrNotBoolNode           = errors.New(\"node is not boolean\")\n\terrNotNullNode           = errors.New(\"node is not null\")\n\terrNotNumberNode         = errors.New(\"node is not number\")\n\terrNotObjectNode         = errors.New(\"node is not object\")\n\terrNotStringNode         = errors.New(\"node is not string\")\n\terrInvalidToken          = errors.New(\"invalid token\")\n\terrIndexNotFound         = errors.New(\"index not found\")\n\terrInvalidAppend         = errors.New(\"can't append value to non-appendable node\")\n\terrInvalidAppendCycle    = errors.New(\"appending value to itself or its children or parents will cause a cycle\")\n\terrInvalidEscapeSequence = errors.New(\"invalid escape sequence\")\n\terrInvalidStringValue    = errors.New(\"invalid string value\")\n\terrEmptyBooleanNode      = errors.New(\"boolean node is empty\")\n\terrEmptyStringNode       = errors.New(\"string node is empty\")\n\terrKeyRequired           = errors.New(\"key is required for object\")\n\terrUnmatchedParenthesis  = errors.New(\"mismatched bracket or parenthesis\")\n\terrUnmatchedQuotePath    = errors.New(\"unmatched quote in path\")\n)\n\nvar (\n\terrInvalidStringInput    = errors.New(\"invalid string input\")\n\terrMalformedBooleanValue = errors.New(\"malformed boolean value\")\n\terrEmptyByteSlice        = errors.New(\"empty byte slice\")\n\terrInvalidExponentValue  = errors.New(\"invalid exponent value\")\n\terrNonDigitCharacters    = errors.New(\"non-digit characters found\")\n\terrNumericRangeExceeded  = errors.New(\"numeric value exceeds the range limit\")\n\terrMultipleDecimalPoints = errors.New(\"multiple decimal points found\")\n)\n"},{"name":"escape.gno","body":"package json\n\nimport (\n\t\"unicode/utf8\"\n)\n\nconst (\n\tsupplementalPlanesOffset     = 0x10000\n\thighSurrogateOffset          = 0xD800\n\tlowSurrogateOffset           = 0xDC00\n\tsurrogateEnd                 = 0xDFFF\n\tbasicMultilingualPlaneOffset = 0xFFFF\n\tbadHex                       = -1\n\n\tsingleUnicodeEscapeLen = 6\n\tsurrogatePairLen       = 12\n)\n\nvar hexLookupTable = [256]int{\n\t'0': 0x0, '1': 0x1, '2': 0x2, '3': 0x3, '4': 0x4,\n\t'5': 0x5, '6': 0x6, '7': 0x7, '8': 0x8, '9': 0x9,\n\t'A': 0xA, 'B': 0xB, 'C': 0xC, 'D': 0xD, 'E': 0xE, 'F': 0xF,\n\t'a': 0xA, 'b': 0xB, 'c': 0xC, 'd': 0xD, 'e': 0xE, 'f': 0xF,\n\t// Fill unspecified index-value pairs with key and value of -1\n\t'G': -1, 'H': -1, 'I': -1, 'J': -1,\n\t'K': -1, 'L': -1, 'M': -1, 'N': -1,\n\t'O': -1, 'P': -1, 'Q': -1, 'R': -1,\n\t'S': -1, 'T': -1, 'U': -1, 'V': -1,\n\t'W': -1, 'X': -1, 'Y': -1, 'Z': -1,\n\t'g': -1, 'h': -1, 'i': -1, 'j': -1,\n\t'k': -1, 'l': -1, 'm': -1, 'n': -1,\n\t'o': -1, 'p': -1, 'q': -1, 'r': -1,\n\t's': -1, 't': -1, 'u': -1, 'v': -1,\n\t'w': -1, 'x': -1, 'y': -1, 'z': -1,\n}\n\nfunc h2i(c byte) int {\n\treturn hexLookupTable[c]\n}\n\n// Unescape takes an input byte slice, processes it to Unescape certain characters,\n// and writes the result into an output byte slice.\n//\n// it returns the processed slice and any error encountered during the Unescape operation.\nfunc Unescape(input, output []byte) ([]byte, error) {\n\t// ensure the output slice has enough capacity to hold the input slice.\n\tinputLen := len(input)\n\tif cap(output) \u003c inputLen {\n\t\toutput = make([]byte, inputLen)\n\t}\n\n\tinPos, outPos := 0, 0\n\n\tfor inPos \u003c len(input) {\n\t\tc := input[inPos]\n\t\tif c != backSlash {\n\t\t\toutput[outPos] = c\n\t\t\tinPos++\n\t\t\toutPos++\n\t\t} else {\n\t\t\t// process escape sequence\n\t\t\tinLen, outLen, err := processEscapedUTF8(input[inPos:], output[outPos:])\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tinPos += inLen\n\t\t\toutPos += outLen\n\t\t}\n\t}\n\n\treturn output[:outPos], nil\n}\n\n// isSurrogatePair returns true if the rune is a surrogate pair.\n//\n// A surrogate pairs are used in UTF-16 encoding to encode characters\n// outside the Basic Multilingual Plane (BMP).\nfunc isSurrogatePair(r rune) bool {\n\treturn highSurrogateOffset \u003c= r \u0026\u0026 r \u003c= surrogateEnd\n}\n\n// isHighSurrogate checks if the rune is a high surrogate (U+D800 to U+DBFF).\nfunc isHighSurrogate(r rune) bool {\n\treturn r \u003e= highSurrogateOffset \u0026\u0026 r \u003c= 0xDBFF\n}\n\n// isLowSurrogate checks if the rune is a low surrogate (U+DC00 to U+DFFF).\nfunc isLowSurrogate(r rune) bool {\n\treturn r \u003e= lowSurrogateOffset \u0026\u0026 r \u003c= surrogateEnd\n}\n\n// combineSurrogates reconstruct the original unicode code points in the\n// supplemental plane by combinin the high and low surrogate.\n//\n// The hight surrogate in the range from U+D800 to U+DBFF,\n// and the low surrogate in the range from U+DC00 to U+DFFF.\n//\n// The formula to combine the surrogates is:\n// (high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000\nfunc combineSurrogates(high, low rune) rune {\n\treturn ((high - highSurrogateOffset) \u003c\u003c 10) + (low - lowSurrogateOffset) + supplementalPlanesOffset\n}\n\n// deocdeSingleUnicodeEscape decodes a unicode escape sequence (e.g., \\uXXXX) into a rune.\nfunc decodeSingleUnicodeEscape(b []byte) (rune, bool) {\n\tif len(b) \u003c 6 {\n\t\treturn utf8.RuneError, false\n\t}\n\n\t// convert hex to decimal\n\th1, h2, h3, h4 := h2i(b[2]), h2i(b[3]), h2i(b[4]), h2i(b[5])\n\tif h1 == badHex || h2 == badHex || h3 == badHex || h4 == badHex {\n\t\treturn utf8.RuneError, false\n\t}\n\n\treturn rune(h1\u003c\u003c12 + h2\u003c\u003c8 + h3\u003c\u003c4 + h4), true\n}\n\n// decodeUnicodeEscape decodes a Unicode escape sequence from a byte slice.\n// It handles both single Unicode escape sequences and surrogate pairs.\nfunc decodeUnicodeEscape(b []byte) (rune, int) {\n\t// decode the first Unicode escape sequence.\n\tr, ok := decodeSingleUnicodeEscape(b)\n\tif !ok {\n\t\treturn utf8.RuneError, -1\n\t}\n\n\t// if the rune is within the BMP and not a surrogate, return it\n\tif r \u003c= basicMultilingualPlaneOffset \u0026\u0026 !isSurrogatePair(r) {\n\t\treturn r, 6\n\t}\n\n\tif !isHighSurrogate(r) {\n\t\t// invalid surrogate pair.\n\t\treturn utf8.RuneError, -1\n\t}\n\n\t// if the rune is a high surrogate, need to decode the next escape sequence.\n\n\t// ensure there are enough bytes for the next escape sequence.\n\tif len(b) \u003c surrogatePairLen {\n\t\treturn utf8.RuneError, -1\n\t}\n\t// decode the second Unicode escape sequence.\n\tr2, ok := decodeSingleUnicodeEscape(b[singleUnicodeEscapeLen:])\n\tif !ok {\n\t\treturn utf8.RuneError, -1\n\t}\n\t// check if the second rune is a low surrogate.\n\tif isLowSurrogate(r2) {\n\t\tcombined := combineSurrogates(r, r2)\n\t\treturn combined, surrogatePairLen\n\t}\n\treturn utf8.RuneError, -1\n}\n\nvar escapeByteSet = [256]byte{\n\t'\"':  doubleQuote,\n\t'\\\\': backSlash,\n\t'/':  slash,\n\t'b':  backSpace,\n\t'f':  formFeed,\n\t'n':  newLine,\n\t'r':  carriageReturn,\n\t't':  tab,\n}\n\n// Unquote takes a byte slice and unquotes it by removing\n// the surrounding quotes and unescaping the contents.\nfunc Unquote(s []byte, border byte) (string, bool) {\n\ts, ok := unquoteBytes(s, border)\n\treturn string(s), ok\n}\n\n// unquoteBytes takes a byte slice and unquotes it by removing\nfunc unquoteBytes(s []byte, border byte) ([]byte, bool) {\n\tif len(s) \u003c 2 || s[0] != border || s[len(s)-1] != border {\n\t\treturn nil, false\n\t}\n\n\ts = s[1 : len(s)-1]\n\n\tr := 0\n\tfor r \u003c len(s) {\n\t\tc := s[r]\n\n\t\tif c == backSlash || c == border || c \u003c 0x20 {\n\t\t\tbreak\n\t\t}\n\n\t\tif c \u003c utf8.RuneSelf {\n\t\t\tr++\n\t\t\tcontinue\n\t\t}\n\n\t\trr, size := utf8.DecodeRune(s[r:])\n\t\tif rr == utf8.RuneError \u0026\u0026 size == 1 {\n\t\t\tbreak\n\t\t}\n\n\t\tr += size\n\t}\n\n\tif r == len(s) {\n\t\treturn s, true\n\t}\n\n\tutfDoubleMax := utf8.UTFMax * 2\n\tb := make([]byte, len(s)+utfDoubleMax)\n\tw := copy(b, s[0:r])\n\n\tfor r \u003c len(s) {\n\t\tif w \u003e= len(b)-utf8.UTFMax {\n\t\t\tnb := make([]byte, utfDoubleMax+(2*len(b)))\n\t\t\tcopy(nb, b)\n\t\t\tb = nb\n\t\t}\n\n\t\tc := s[r]\n\t\tif c == backSlash {\n\t\t\tr++\n\t\t\tif r \u003e= len(s) {\n\t\t\t\treturn nil, false\n\t\t\t}\n\n\t\t\tif s[r] == 'u' {\n\t\t\t\trr, res := decodeUnicodeEscape(s[r-1:])\n\t\t\t\tif res \u003c 0 {\n\t\t\t\t\treturn nil, false\n\t\t\t\t}\n\n\t\t\t\tw += utf8.EncodeRune(b[w:], rr)\n\t\t\t\tr += 5\n\t\t\t} else {\n\t\t\t\tdecode := escapeByteSet[s[r]]\n\t\t\t\tif decode == 0 {\n\t\t\t\t\treturn nil, false\n\t\t\t\t}\n\n\t\t\t\tif decode == doubleQuote || decode == backSlash || decode == slash {\n\t\t\t\t\tdecode = s[r]\n\t\t\t\t}\n\n\t\t\t\tb[w] = decode\n\t\t\t\tr++\n\t\t\t\tw++\n\t\t\t}\n\t\t} else if c == border || c \u003c 0x20 {\n\t\t\treturn nil, false\n\t\t} else if c \u003c utf8.RuneSelf {\n\t\t\tb[w] = c\n\t\t\tr++\n\t\t\tw++\n\t\t} else {\n\t\t\trr, size := utf8.DecodeRune(s[r:])\n\n\t\t\tif rr == utf8.RuneError \u0026\u0026 size == 1 {\n\t\t\t\treturn nil, false\n\t\t\t}\n\n\t\t\tr += size\n\t\t\tw += utf8.EncodeRune(b[w:], rr)\n\t\t}\n\t}\n\n\treturn b[:w], true\n}\n\n// processEscapedUTF8 converts escape sequences to UTF-8 characters.\n// It decodes Unicode escape sequences (\\uXXXX) to UTF-8 and\n// converts standard escape sequences (e.g., \\n) to their corresponding special characters.\nfunc processEscapedUTF8(in, out []byte) (int, int, error) {\n\tif len(in) \u003c 2 || in[0] != backSlash {\n\t\treturn -1, -1, errInvalidEscapeSequence\n\t}\n\n\tescapeSeqLen := 2\n\tescapeChar := in[1]\n\n\tif escapeChar != 'u' {\n\t\tval := escapeByteSet[escapeChar]\n\t\tif val == 0 {\n\t\t\treturn -1, -1, errInvalidEscapeSequence\n\t\t}\n\n\t\tout[0] = val\n\t\treturn escapeSeqLen, 1, nil\n\t}\n\n\tr, size := decodeUnicodeEscape(in)\n\tif size == -1 {\n\t\treturn -1, -1, errInvalidEscapeSequence\n\t}\n\n\toutLen := utf8.EncodeRune(out, r)\n\n\treturn size, outLen, nil\n}\n"},{"name":"escape_test.gno","body":"package json\n\nimport (\n\t\"bytes\"\n\t\"testing\"\n\t\"unicode/utf8\"\n)\n\nfunc TestHexToInt(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tc    byte\n\t\twant int\n\t}{\n\t\t{\"Digit 0\", '0', 0},\n\t\t{\"Digit 9\", '9', 9},\n\t\t{\"Uppercase A\", 'A', 10},\n\t\t{\"Uppercase F\", 'F', 15},\n\t\t{\"Lowercase a\", 'a', 10},\n\t\t{\"Lowercase f\", 'f', 15},\n\t\t{\"Invalid character1\", 'g', badHex},\n\t\t{\"Invalid character2\", 'G', badHex},\n\t\t{\"Invalid character3\", 'z', badHex},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif got := h2i(tt.c); got != tt.want {\n\t\t\t\tt.Errorf(\"h2i() = %v, want %v\", got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestIsSurrogatePair(t *testing.T) {\n\ttestCases := []struct {\n\t\tname     string\n\t\tr        rune\n\t\texpected bool\n\t}{\n\t\t{\"high surrogate start\", 0xD800, true},\n\t\t{\"high surrogate end\", 0xDBFF, true},\n\t\t{\"low surrogate start\", 0xDC00, true},\n\t\t{\"low surrogate end\", 0xDFFF, true},\n\t\t{\"Non-surrogate\", 0x0000, false},\n\t\t{\"Non-surrogate 2\", 0xE000, false},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tif got := isSurrogatePair(tc.r); got != tc.expected {\n\t\t\t\tt.Errorf(\"isSurrogate() = %v, want %v\", got, tc.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestCombineSurrogates(t *testing.T) {\n\ttestCases := []struct {\n\t\thigh, low rune\n\t\texpected  rune\n\t}{\n\t\t{0xD83D, 0xDC36, 0x1F436}, // 🐶 U+1F436 DOG FACE\n\t\t{0xD83D, 0xDE00, 0x1F600}, // 😀 U+1F600 GRINNING FACE\n\t\t{0xD83C, 0xDF03, 0x1F303}, // 🌃 U+1F303 NIGHT WITH STARS\n\t}\n\n\tfor _, tc := range testCases {\n\t\tresult := combineSurrogates(tc.high, tc.low)\n\t\tif result != tc.expected {\n\t\t\tt.Errorf(\"combineSurrogates(%U, %U) = %U; want %U\", tc.high, tc.low, result, tc.expected)\n\t\t}\n\t}\n}\n\nfunc TestDecodeSingleUnicodeEscape(t *testing.T) {\n\ttestCases := []struct {\n\t\tinput    []byte\n\t\texpected rune\n\t\tisValid  bool\n\t}{\n\t\t// valid unicode escape sequences\n\t\t{[]byte(`\\u0041`), 'A', true},\n\t\t{[]byte(`\\u03B1`), 'α', true},\n\t\t{[]byte(`\\u00E9`), 'é', true}, // valid non-English character\n\t\t{[]byte(`\\u0021`), '!', true}, // valid special character\n\t\t{[]byte(`\\uFF11`), '１', true},\n\t\t{[]byte(`\\uD83D`), 0xD83D, true},\n\t\t{[]byte(`\\uDE03`), 0xDE03, true},\n\n\t\t// invalid unicode escape sequences\n\t\t{[]byte(`\\u004`), utf8.RuneError, false},  // too short\n\t\t{[]byte(`\\uXYZW`), utf8.RuneError, false}, // invalid hex\n\t\t{[]byte(`\\u00G1`), utf8.RuneError, false}, // non-hex character\n\t}\n\n\tfor _, tc := range testCases {\n\t\tresult, isValid := decodeSingleUnicodeEscape(tc.input)\n\t\tif result != tc.expected || isValid != tc.isValid {\n\t\t\tt.Errorf(\"decodeSingleUnicodeEscape(%s) = (%U, %v); want (%U, %v)\", tc.input, result, isValid, tc.expected, tc.isValid)\n\t\t}\n\t}\n}\n\nfunc TestDecodeUnicodeEscape(t *testing.T) {\n\ttests := []struct {\n\t\tinput    []byte\n\t\texpected rune\n\t\tsize     int\n\t}{\n\t\t{[]byte(`\\u0041`), 'A', 6},\n\t\t{[]byte(`\\uD83D\\uDE00`), 0x1F600, 12}, // 😀\n\t\t{[]byte(`\\uD834\\uDD1E`), 0x1D11E, 12}, // 𝄞\n\t\t{[]byte(`\\uFFFF`), '\\uFFFF', 6},\n\t\t{[]byte(`\\uXYZW`), utf8.RuneError, -1},\n\t\t{[]byte(`\\uD800`), utf8.RuneError, -1},       // single high surrogate\n\t\t{[]byte(`\\uDC00`), utf8.RuneError, -1},       // single low surrogate\n\t\t{[]byte(`\\uD800\\uDC00`), 0x10000, 12},        // First code point above U+FFFF\n\t\t{[]byte(`\\uDBFF\\uDFFF`), 0x10FFFF, 12},       // Maximum code point\n\t\t{[]byte(`\\uD83D\\u0041`), utf8.RuneError, -1}, // invalid surrogate pair\n\t}\n\n\tfor _, tc := range tests {\n\t\tr, size := decodeUnicodeEscape(tc.input)\n\t\tif r != tc.expected || size != tc.size {\n\t\t\tt.Errorf(\"decodeUnicodeEscape(%q) = (%U, %d); want (%U, %d)\", tc.input, r, size, tc.expected, tc.size)\n\t\t}\n\t}\n}\n\nfunc TestUnescapeToUTF8(t *testing.T) {\n\ttests := []struct {\n\t\tinput       []byte\n\t\texpectedIn  int\n\t\texpectedOut int\n\t\tisError     bool\n\t}{\n\t\t// valid escape sequences\n\t\t{[]byte(`\\n`), 2, 1, false},\n\t\t{[]byte(`\\t`), 2, 1, false},\n\t\t{[]byte(`\\u0041`), 6, 1, false},\n\t\t{[]byte(`\\u03B1`), 6, 2, false},\n\t\t{[]byte(`\\uD830\\uDE03`), 12, 4, false},\n\n\t\t// invalid escape sequences\n\t\t{[]byte(`\\`), -1, -1, true},            // incomplete escape sequence\n\t\t{[]byte(`\\x`), -1, -1, true},           // invalid escape character\n\t\t{[]byte(`\\u`), -1, -1, true},           // incomplete unicode escape sequence\n\t\t{[]byte(`\\u004`), -1, -1, true},        // invalid unicode escape sequence\n\t\t{[]byte(`\\uXYZW`), -1, -1, true},       // invalid unicode escape sequence\n\t\t{[]byte(`\\uD83D\\u0041`), -1, -1, true}, // invalid unicode escape sequence\n\t}\n\n\tfor _, tc := range tests {\n\t\tinput := make([]byte, len(tc.input))\n\t\tcopy(input, tc.input)\n\t\toutput := make([]byte, utf8.UTFMax)\n\t\tinLen, outLen, err := processEscapedUTF8(input, output)\n\t\tif (err != nil) != tc.isError {\n\t\t\tt.Errorf(\"processEscapedUTF8(%q) = %v; want %v\", tc.input, err, tc.isError)\n\t\t}\n\n\t\tif inLen != tc.expectedIn || outLen != tc.expectedOut {\n\t\t\tt.Errorf(\"processEscapedUTF8(%q) = (%d, %d); want (%d, %d)\", tc.input, inLen, outLen, tc.expectedIn, tc.expectedOut)\n\t\t}\n\t}\n}\n\nfunc TestUnescape(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []byte\n\t\texpected []byte\n\t\tisError  bool\n\t}{\n\t\t{\"NoEscape\", []byte(\"hello world\"), []byte(\"hello world\"), false},\n\t\t{\"SingleEscape\", []byte(\"hello\\\\nworld\"), []byte(\"hello\\nworld\"), false},\n\t\t{\"MultipleEscapes\", []byte(\"line1\\\\nline2\\\\r\\\\nline3\"), []byte(\"line1\\nline2\\r\\nline3\"), false},\n\t\t{\"UnicodeEscape\", []byte(\"snowman:\\\\u2603\"), []byte(\"snowman:\\u2603\"), false},\n\t\t{\"SurrogatePair\", []byte(\"emoji:\\\\uD83D\\\\uDE00\"), []byte(\"emoji:😀\"), false},\n\t\t{\"InvalidEscape\", []byte(\"hello\\\\xworld\"), nil, true},\n\t\t{\"IncompleteUnicode\", []byte(\"incomplete:\\\\u123\"), nil, true},\n\t\t{\"InvalidSurrogatePair\", []byte(\"invalid:\\\\uD83D\\\\u0041\"), nil, true},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\toutput := make([]byte, len(tc.input)*2) // Allocate extra space for possible expansion\n\t\t\tresult, err := Unescape(tc.input, output)\n\t\t\tif (err != nil) != tc.isError {\n\t\t\t\tt.Errorf(\"Unescape(%q) error = %v; want error = %v\", tc.input, err, tc.isError)\n\t\t\t}\n\n\t\t\tif !tc.isError \u0026\u0026 !bytes.Equal(result, tc.expected) {\n\t\t\t\tt.Errorf(\"Unescape(%q) = %q; want %q\", tc.input, result, tc.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestUnquoteBytes(t *testing.T) {\n\ttests := []struct {\n\t\tinput    []byte\n\t\tborder   byte\n\t\texpected []byte\n\t\tok       bool\n\t}{\n\t\t{[]byte(\"\\\"hello\\\"\"), '\"', []byte(\"hello\"), true},\n\t\t{[]byte(\"'hello'\"), '\\'', []byte(\"hello\"), true},\n\t\t{[]byte(\"\\\"hello\"), '\"', nil, false},\n\t\t{[]byte(\"hello\\\"\"), '\"', nil, false},\n\t\t{[]byte(\"\\\"he\\\\\\\"llo\\\"\"), '\"', []byte(\"he\\\"llo\"), true},\n\t\t{[]byte(\"\\\"he\\\\nllo\\\"\"), '\"', []byte(\"he\\nllo\"), true},\n\t\t{[]byte(\"\\\"\\\"\"), '\"', []byte(\"\"), true},\n\t\t{[]byte(\"''\"), '\\'', []byte(\"\"), true},\n\t\t{[]byte(\"\\\"\\\\u0041\\\"\"), '\"', []byte(\"A\"), true},\n\t\t{[]byte(`\"Hello, 世界\"`), '\"', []byte(\"Hello, 世界\"), true},\n\t\t{[]byte(`\"Hello, \\x80\"`), '\"', nil, false},\n\t\t{[]byte(`\"invalid surrogate: \\uD83D\\u0041\"`), '\"', nil, false},\n\t}\n\n\tfor _, tc := range tests {\n\t\tresult, pass := unquoteBytes(tc.input, tc.border)\n\n\t\tif pass != tc.ok {\n\t\t\tt.Errorf(\"unquoteBytes(%q) = %v; want %v\", tc.input, pass, tc.ok)\n\t\t}\n\n\t\tif !bytes.Equal(result, tc.expected) {\n\t\t\tt.Errorf(\"unquoteBytes(%q) = %q; want %q\", tc.input, result, tc.expected)\n\t\t}\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/onbloc/json\"\ngno = \"0.9\"\n"},{"name":"indent.gno","body":"package json\n\nimport (\n\t\"bytes\"\n\t\"strings\"\n)\n\n// indentGrowthFactor specifies the growth factor of indenting JSON input.\n// A factor no higher than 2 ensures that wasted space never exceeds 50%.\nconst indentGrowthFactor = 2\n\n// IndentJSON formats the JSON data with the specified indentation.\nfunc Indent(data []byte, indent string) ([]byte, error) {\n\tvar (\n\t\tout        bytes.Buffer\n\t\tlevel      int\n\t\tinArray    bool\n\t\tarrayDepth int\n\t)\n\n\tfor i := 0; i \u003c len(data); i++ {\n\t\tc := data[i] // current character\n\n\t\tswitch c {\n\t\tcase bracketOpen:\n\t\t\tarrayDepth++\n\t\t\tif arrayDepth \u003e 1 {\n\t\t\t\tlevel++ // increase the level if it's nested array\n\t\t\t\tinArray = true\n\n\t\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\tif err := writeNewlineAndIndent(\u0026out, level, indent); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// case of the top-level array\n\t\t\t\tinArray = true\n\t\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t}\n\n\t\tcase bracketClose:\n\t\t\tif inArray \u0026\u0026 arrayDepth \u003e 1 { // nested array\n\t\t\t\tlevel--\n\t\t\t\tif err := writeNewlineAndIndent(\u0026out, level, indent); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tarrayDepth--\n\t\t\tif arrayDepth == 0 {\n\t\t\t\tinArray = false\n\t\t\t}\n\n\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\tcase curlyOpen:\n\t\t\t// check if the empty object or array\n\t\t\t// we don't need to apply the indent when it's empty containers.\n\t\t\tif i+1 \u003c len(data) \u0026\u0026 data[i+1] == curlyClose {\n\t\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\ti++ // skip next character\n\t\t\t\tif err := out.WriteByte(data[i]); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\tlevel++\n\t\t\t\tif err := writeNewlineAndIndent(\u0026out, level, indent); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t}\n\n\t\tcase curlyClose:\n\t\t\tlevel--\n\t\t\tif err := writeNewlineAndIndent(\u0026out, level, indent); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\tcase comma, colon:\n\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tif inArray \u0026\u0026 arrayDepth \u003e 1 { // nested array\n\t\t\t\tif err := writeNewlineAndIndent(\u0026out, level, indent); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t} else if c == colon {\n\t\t\t\tif err := out.WriteByte(' '); err != nil {\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\t\t\t}\n\n\t\tdefault:\n\t\t\tif err := out.WriteByte(c); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t}\n\t}\n\n\treturn out.Bytes(), nil\n}\n\nfunc writeNewlineAndIndent(out *bytes.Buffer, level int, indent string) error {\n\tif err := out.WriteByte('\\n'); err != nil {\n\t\treturn err\n\t}\n\n\tidt := strings.Repeat(indent, level*indentGrowthFactor)\n\tif _, err := out.WriteString(idt); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n"},{"name":"indent_test.gno","body":"package json\n\nimport (\n\t\"bytes\"\n\t\"testing\"\n)\n\nfunc TestIndentJSON(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []byte\n\t\tindent   string\n\t\texpected []byte\n\t}{\n\t\t{\n\t\t\tname:     \"empty object\",\n\t\t\tinput:    []byte(`{}`),\n\t\t\tindent:   \"  \",\n\t\t\texpected: []byte(`{}`),\n\t\t},\n\t\t{\n\t\t\tname:     \"empty array\",\n\t\t\tinput:    []byte(`[]`),\n\t\t\tindent:   \"  \",\n\t\t\texpected: []byte(`[]`),\n\t\t},\n\t\t{\n\t\t\tname:     \"nested object\",\n\t\t\tinput:    []byte(`{{}}`),\n\t\t\tindent:   \"\\t\",\n\t\t\texpected: []byte(\"{\\n\\t\\t{}\\n}\"),\n\t\t},\n\t\t{\n\t\t\tname:     \"nested array\",\n\t\t\tinput:    []byte(`[[[]]]`),\n\t\t\tindent:   \"\\t\",\n\t\t\texpected: []byte(\"[[\\n\\t\\t[\\n\\t\\t\\t\\t\\n\\t\\t]\\n]]\"),\n\t\t},\n\t\t{\n\t\t\tname:     \"top-level array\",\n\t\t\tinput:    []byte(`[\"apple\",\"banana\",\"cherry\"]`),\n\t\t\tindent:   \"\\t\",\n\t\t\texpected: []byte(`[\"apple\",\"banana\",\"cherry\"]`),\n\t\t},\n\t\t{\n\t\t\tname:     \"array of arrays\",\n\t\t\tinput:    []byte(`[\"apple\",[\"banana\",\"cherry\"],\"date\"]`),\n\t\t\tindent:   \"  \",\n\t\t\texpected: []byte(\"[\\\"apple\\\",[\\n    \\\"banana\\\",\\n    \\\"cherry\\\"\\n],\\\"date\\\"]\"),\n\t\t},\n\n\t\t{\n\t\t\tname:     \"nested array in object\",\n\t\t\tinput:    []byte(`{\"fruits\":[\"apple\",[\"banana\",\"cherry\"],\"date\"]}`),\n\t\t\tindent:   \"  \",\n\t\t\texpected: []byte(\"{\\n    \\\"fruits\\\": [\\\"apple\\\",[\\n        \\\"banana\\\",\\n        \\\"cherry\\\"\\n    ],\\\"date\\\"]\\n}\"),\n\t\t},\n\t\t{\n\t\t\tname:     \"complex nested structure\",\n\t\t\tinput:    []byte(`{\"data\":{\"array\":[1,2,3],\"bool\":true,\"nestedArray\":[[\"a\",\"b\"],\"c\"]}}`),\n\t\t\tindent:   \"  \",\n\t\t\texpected: []byte(\"{\\n    \\\"data\\\": {\\n        \\\"array\\\": [1,2,3],\\\"bool\\\": true,\\\"nestedArray\\\": [[\\n            \\\"a\\\",\\n            \\\"b\\\"\\n        ],\\\"c\\\"]\\n    }\\n}\"),\n\t\t},\n\t\t{\n\t\t\tname:     \"custom ident character\",\n\t\t\tinput:    []byte(`{\"fruits\":[\"apple\",[\"banana\",\"cherry\"],\"date\"]}`),\n\t\t\tindent:   \"*\",\n\t\t\texpected: []byte(\"{\\n**\\\"fruits\\\": [\\\"apple\\\",[\\n****\\\"banana\\\",\\n****\\\"cherry\\\"\\n**],\\\"date\\\"]\\n}\"),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tactual, err := Indent(tt.input, tt.indent)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"IndentJSON() error = %v\", err)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif !bytes.Equal(actual, tt.expected) {\n\t\t\t\tt.Errorf(\"IndentJSON() = %q, want %q\", actual, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"internal.gno","body":"package json\n\n// Reference: https://github.com/freddierice/php_source/blob/467ed5d6edff72219afd3e644516f131118ef48e/ext/json/JSON_parser.c\n// Copyright (c) 2005 JSON.org\n\n// Go implementation is taken from: https://github.com/spyzhov/ajson/blob/master/internal/state.go\n\ntype (\n\tStates  int8 // possible states of the parser\n\tClasses int8 // JSON string character types\n)\n\nconst __ = -1\n\n// enum classes\nconst (\n\tC_SPACE Classes = iota /* space */\n\tC_WHITE                /* other whitespace */\n\tC_LCURB                /* {  */\n\tC_RCURB                /* } */\n\tC_LSQRB                /* [ */\n\tC_RSQRB                /* ] */\n\tC_COLON                /* : */\n\tC_COMMA                /* , */\n\tC_QUOTE                /* \" */\n\tC_BACKS                /* \\ */\n\tC_SLASH                /* / */\n\tC_PLUS                 /* + */\n\tC_MINUS                /* - */\n\tC_POINT                /* . */\n\tC_ZERO                 /* 0 */\n\tC_DIGIT                /* 123456789 */\n\tC_LOW_A                /* a */\n\tC_LOW_B                /* b */\n\tC_LOW_C                /* c */\n\tC_LOW_D                /* d */\n\tC_LOW_E                /* e */\n\tC_LOW_F                /* f */\n\tC_LOW_L                /* l */\n\tC_LOW_N                /* n */\n\tC_LOW_R                /* r */\n\tC_LOW_S                /* s */\n\tC_LOW_T                /* t */\n\tC_LOW_U                /* u */\n\tC_ABCDF                /* ABCDF */\n\tC_E                    /* E */\n\tC_ETC                  /* everything else */\n)\n\n// AsciiClasses array maps the 128 ASCII characters into character classes.\nvar AsciiClasses = [128]Classes{\n\t/*\n\t   This array maps the 128 ASCII characters into character classes.\n\t   The remaining Unicode characters should be mapped to C_ETC.\n\t   Non-whitespace control characters are errors.\n\t*/\n\t__, __, __, __, __, __, __, __,\n\t__, C_WHITE, C_WHITE, __, __, C_WHITE, __, __,\n\t__, __, __, __, __, __, __, __,\n\t__, __, __, __, __, __, __, __,\n\n\tC_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,\n\tC_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,\n\tC_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,\n\n\tC_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_LSQRB, C_BACKS, C_RSQRB, C_ETC, C_ETC,\n\n\tC_ETC, C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_ETC, C_LOW_L, C_ETC, C_LOW_N, C_ETC,\n\tC_ETC, C_ETC, C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_LCURB, C_ETC, C_RCURB, C_ETC, C_ETC,\n}\n\n// QuoteAsciiClasses is a HACK for single quote from AsciiClasses\nvar QuoteAsciiClasses = [128]Classes{\n\t/*\n\t   This array maps the 128 ASCII characters into character classes.\n\t   The remaining Unicode characters should be mapped to C_ETC.\n\t   Non-whitespace control characters are errors.\n\t*/\n\t__, __, __, __, __, __, __, __,\n\t__, C_WHITE, C_WHITE, __, __, C_WHITE, __, __,\n\t__, __, __, __, __, __, __, __,\n\t__, __, __, __, __, __, __, __,\n\n\tC_SPACE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_QUOTE,\n\tC_ETC, C_ETC, C_ETC, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,\n\tC_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,\n\tC_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,\n\n\tC_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_LSQRB, C_BACKS, C_RSQRB, C_ETC, C_ETC,\n\n\tC_ETC, C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_ETC, C_LOW_L, C_ETC, C_LOW_N, C_ETC,\n\tC_ETC, C_ETC, C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC, C_ETC,\n\tC_ETC, C_ETC, C_ETC, C_LCURB, C_ETC, C_RCURB, C_ETC, C_ETC,\n}\n\n/*\nThe state codes.\n*/\nconst (\n\tGO States = iota /* start    */\n\tOK               /* ok       */\n\tOB               /* object   */\n\tKE               /* key      */\n\tCO               /* colon    */\n\tVA               /* value    */\n\tAR               /* array    */\n\tST               /* string   */\n\tES               /* escape   */\n\tU1               /* u1       */\n\tU2               /* u2       */\n\tU3               /* u3       */\n\tU4               /* u4       */\n\tMI               /* minus    */\n\tZE               /* zero     */\n\tIN               /* integer  */\n\tDT               /* dot      */\n\tFR               /* fraction */\n\tE1               /* e        */\n\tE2               /* ex       */\n\tE3               /* exp      */\n\tT1               /* tr       */\n\tT2               /* tru      */\n\tT3               /* true     */\n\tF1               /* fa       */\n\tF2               /* fal      */\n\tF3               /* fals     */\n\tF4               /* false    */\n\tN1               /* nu       */\n\tN2               /* nul      */\n\tN3               /* null     */\n)\n\n// List of action codes.\n// these constants are defining an action that should be performed under certain conditions.\nconst (\n\tcl States = -2 /* colon           */\n\tcm States = -3 /* comma           */\n\tqt States = -4 /* quote           */\n\tbo States = -5 /* bracket open    */\n\tco States = -6 /* curly bracket open  */\n\tbc States = -7 /* bracket close   */\n\tcc States = -8 /* curly bracket close */\n\tec States = -9 /* curly bracket empty */\n)\n\n// StateTransitionTable is the state transition table takes the current state and the current symbol, and returns either\n// a new state or an action. An action is represented as a negative number. A JSON text is accepted if at the end of the\n// text the state is OK and if the mode is DONE.\nvar StateTransitionTable = [31][31]States{\n\t/*\n\t   The state transition table takes the current state and the current symbol,\n\t   and returns either a new state or an action. An action is represented as a\n\t   negative number. A JSON text is accepted if at the end of the text the\n\t   state is OK and if the mode is DONE.\n\t                  white                                                    1-9                                                ABCDF   etc\n\t            space   |   {   }   [   ]   :   ,   \"   \\   /   +   -   .   0   |   a   b   c   d   e   f   l   n   r   s   t   u   |   E   |*/\n\t/*start  GO*/ {GO, GO, co, __, bo, __, __, __, ST, __, __, __, MI, __, ZE, IN, __, __, __, __, __, F1, __, N1, __, __, T1, __, __, __, __},\n\t/*ok     OK*/ {OK, OK, __, cc, __, bc, __, cm, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*object OB*/ {OB, OB, __, ec, __, __, __, __, ST, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*key    KE*/ {KE, KE, __, __, __, __, __, __, ST, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*colon  CO*/ {CO, CO, __, __, __, __, cl, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*value  VA*/ {VA, VA, co, __, bo, __, __, __, ST, __, __, __, MI, __, ZE, IN, __, __, __, __, __, F1, __, N1, __, __, T1, __, __, __, __},\n\t/*array  AR*/ {AR, AR, co, __, bo, bc, __, __, ST, __, __, __, MI, __, ZE, IN, __, __, __, __, __, F1, __, N1, __, __, T1, __, __, __, __},\n\t/*string ST*/ {ST, __, ST, ST, ST, ST, ST, ST, qt, ES, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},\n\t/*escape ES*/ {__, __, __, __, __, __, __, __, ST, ST, ST, __, __, __, __, __, __, ST, __, __, __, ST, __, ST, ST, __, ST, U1, __, __, __},\n\t/*u1     U1*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, U2, U2, U2, U2, U2, U2, U2, U2, __, __, __, __, __, __, U2, U2, __},\n\t/*u2     U2*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, U3, U3, U3, U3, U3, U3, U3, U3, __, __, __, __, __, __, U3, U3, __},\n\t/*u3     U3*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, U4, U4, U4, U4, U4, U4, U4, U4, __, __, __, __, __, __, U4, U4, __},\n\t/*u4     U4*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, ST, ST, ST, ST, ST, ST, ST, ST, __, __, __, __, __, __, ST, ST, __},\n\t/*minus  MI*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, ZE, IN, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*zero   ZE*/ {OK, OK, __, cc, __, bc, __, cm, __, __, __, __, __, DT, __, __, __, __, __, __, E1, __, __, __, __, __, __, __, __, E1, __},\n\t/*int    IN*/ {OK, OK, __, cc, __, bc, __, cm, __, __, __, __, __, DT, IN, IN, __, __, __, __, E1, __, __, __, __, __, __, __, __, E1, __},\n\t/*dot    DT*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, FR, FR, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*frac   FR*/ {OK, OK, __, cc, __, bc, __, cm, __, __, __, __, __, __, FR, FR, __, __, __, __, E1, __, __, __, __, __, __, __, __, E1, __},\n\t/*e      E1*/ {__, __, __, __, __, __, __, __, __, __, __, E2, E2, __, E3, E3, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*ex     E2*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, E3, E3, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*exp    E3*/ {OK, OK, __, cc, __, bc, __, cm, __, __, __, __, __, __, E3, E3, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*tr     T1*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, T2, __, __, __, __, __, __},\n\t/*tru    T2*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, T3, __, __, __},\n\t/*true   T3*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, OK, __, __, __, __, __, __, __, __, __, __},\n\t/*fa     F1*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, F2, __, __, __, __, __, __, __, __, __, __, __, __, __, __},\n\t/*fal    F2*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, F3, __, __, __, __, __, __, __, __},\n\t/*fals   F3*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, F4, __, __, __, __, __},\n\t/*false  F4*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, OK, __, __, __, __, __, __, __, __, __, __},\n\t/*nu     N1*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, N2, __, __, __},\n\t/*nul    N2*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, N3, __, __, __, __, __, __, __, __},\n\t/*null   N3*/ {__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, OK, __, __, __, __, __, __, __, __},\n}\n"},{"name":"node.gno","body":"package json\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Node represents a JSON node.\ntype Node struct {\n\tprev     *Node            // prev is the parent node of the current node.\n\tnext     map[string]*Node // next is the child nodes of the current node.\n\tkey      *string          // key holds the key of the current node in the parent node.\n\tdata     []byte           // byte slice of JSON data\n\tvalue    any              // value holds the value of the current node.\n\tnodeType ValueType        // NodeType holds the type of the current node. (Object, Array, String, Number, Boolean, Null)\n\tindex    *int             // index holds the index of the current node in the parent array node.\n\tborders  [2]int           // borders stores the start and end index of the current node in the data.\n\tmodified bool             // modified indicates the current node is changed or not.\n}\n\n// NewNode creates a new node instance with the given parent node, buffer, type, and key.\nfunc NewNode(prev *Node, b *buffer, typ ValueType, key **string) (*Node, error) {\n\tcurr := \u0026Node{\n\t\tprev:     prev,\n\t\tdata:     b.data,\n\t\tborders:  [2]int{b.index, 0},\n\t\tkey:      *key,\n\t\tnodeType: typ,\n\t\tmodified: false,\n\t}\n\n\tif typ == Object || typ == Array {\n\t\tcurr.next = make(map[string]*Node)\n\t}\n\n\tif prev != nil {\n\t\tif prev.IsArray() {\n\t\t\tsize := len(prev.next)\n\t\t\tcurr.index = \u0026size\n\n\t\t\tprev.next[strconv.Itoa(size)] = curr\n\t\t} else if prev.IsObject() {\n\t\t\tif key == nil {\n\t\t\t\treturn nil, errKeyRequired\n\t\t\t}\n\n\t\t\tprev.next[**key] = curr\n\t\t} else {\n\t\t\treturn nil, errors.New(\"invalid parent type\")\n\t\t}\n\t}\n\n\treturn curr, nil\n}\n\n// load retrieves the value of the current node.\nfunc (n *Node) load() any {\n\treturn n.value\n}\n\n// Changed checks the current node is changed or not.\nfunc (n *Node) Changed() bool {\n\treturn n.modified\n}\n\n// Key returns the key of the current node.\nfunc (n *Node) Key() string {\n\tif n == nil || n.key == nil {\n\t\treturn \"\"\n\t}\n\n\treturn *n.key\n}\n\n// HasKey checks the current node has the given key or not.\nfunc (n *Node) HasKey(key string) bool {\n\tif n == nil {\n\t\treturn false\n\t}\n\n\t_, ok := n.next[key]\n\treturn ok\n}\n\n// GetKey returns the value of the given key from the current object node.\nfunc (n *Node) GetKey(key string) (*Node, error) {\n\tif n == nil {\n\t\treturn nil, errNilNode\n\t}\n\n\tif n.Type() != Object {\n\t\treturn nil, ufmt.Errorf(\"target node is not object type. got: %s\", n.Type().String())\n\t}\n\n\tvalue, ok := n.next[key]\n\tif !ok {\n\t\treturn nil, ufmt.Errorf(\"key not found: %s\", key)\n\t}\n\n\treturn value, nil\n}\n\n// MustKey returns the value of the given key from the current object node.\nfunc (n *Node) MustKey(key string) *Node {\n\tval, err := n.GetKey(key)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn val\n}\n\n// UniqueKeyLists traverses the current JSON nodes and collects all the unique keys.\nfunc (n *Node) UniqueKeyLists() []string {\n\tvar collectKeys func(*Node) []string\n\tcollectKeys = func(node *Node) []string {\n\t\tif node == nil || !node.IsObject() {\n\t\t\treturn nil\n\t\t}\n\n\t\tresult := make(map[string]bool)\n\t\tfor key, childNode := range node.next {\n\t\t\tresult[key] = true\n\t\t\tchildKeys := collectKeys(childNode)\n\t\t\tfor _, childKey := range childKeys {\n\t\t\t\tresult[childKey] = true\n\t\t\t}\n\t\t}\n\n\t\tkeys := make([]string, 0, len(result))\n\t\tfor key := range result {\n\t\t\tkeys = append(keys, key)\n\t\t}\n\t\treturn keys\n\t}\n\n\treturn collectKeys(n)\n}\n\n// Empty returns true if the current node is empty.\nfunc (n *Node) Empty() bool {\n\tif n == nil {\n\t\treturn false\n\t}\n\n\treturn len(n.next) == 0\n}\n\n// Type returns the type (ValueType) of the current node.\nfunc (n *Node) Type() ValueType {\n\treturn n.nodeType\n}\n\n// Value returns the value of the current node.\n//\n// Usage:\n//\n//\troot := Unmarshal([]byte(`{\"key\": \"value\"}`))\n//\tval, err := root.MustKey(\"key\").Value()\n//\tif err != nil {\n//\t\tt.Errorf(\"Value returns error: %v\", err)\n//\t}\n//\n//\tresult: \"value\"\nfunc (n *Node) Value() (value any, err error) {\n\tvalue = n.load()\n\n\tif value == nil {\n\t\tswitch n.nodeType {\n\t\tcase Null:\n\t\t\treturn nil, nil\n\n\t\tcase Number:\n\t\t\tvalue, err = strconv.ParseFloat(string(n.source()), 64)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tn.value = value\n\n\t\tcase String:\n\t\t\tvar ok bool\n\t\t\tvalue, ok = Unquote(n.source(), doubleQuote)\n\t\t\tif !ok {\n\t\t\t\treturn \"\", errInvalidStringValue\n\t\t\t}\n\n\t\t\tn.value = value\n\n\t\tcase Boolean:\n\t\t\tif len(n.source()) == 0 {\n\t\t\t\treturn nil, errEmptyBooleanNode\n\t\t\t}\n\n\t\t\tb := n.source()[0]\n\t\t\tvalue = b == 't' || b == 'T'\n\t\t\tn.value = value\n\n\t\tcase Array:\n\t\t\telems := make([]*Node, len(n.next))\n\n\t\t\tfor _, e := range n.next {\n\t\t\t\telems[*e.index] = e\n\t\t\t}\n\n\t\t\tvalue = elems\n\t\t\tn.value = value\n\n\t\tcase Object:\n\t\t\tobj := make(map[string]*Node, len(n.next))\n\n\t\t\tfor k, v := range n.next {\n\t\t\t\tobj[k] = v\n\t\t\t}\n\n\t\t\tvalue = obj\n\t\t\tn.value = value\n\t\t}\n\t}\n\n\treturn value, nil\n}\n\n// Delete removes the current node from the parent node.\n//\n// Usage:\n//\n//\troot := Unmarshal([]byte(`{\"key\": \"value\"}`))\n//\tif err := root.MustKey(\"key\").Delete(); err != nil {\n//\t\tt.Errorf(\"Delete returns error: %v\", err)\n//\t}\n//\n//\tresult: {} (empty object)\nfunc (n *Node) Delete() error {\n\tif n == nil {\n\t\treturn errors.New(\"can't delete nil node\")\n\t}\n\n\tif n.prev == nil {\n\t\treturn nil\n\t}\n\n\treturn n.prev.remove(n)\n}\n\n// Size returns the size (length) of the current array node.\n//\n// Usage:\n//\n//\troot := ArrayNode(\"\", []*Node{StringNode(\"\", \"foo\"), NumberNode(\"\", 1)})\n//\tif root == nil {\n//\t\tt.Errorf(\"ArrayNode returns nil\")\n//\t}\n//\n//\tif root.Size() != 2 {\n//\t\tt.Errorf(\"ArrayNode returns wrong size: %d\", root.Size())\n//\t}\nfunc (n *Node) Size() int {\n\tif n == nil {\n\t\treturn 0\n\t}\n\n\treturn len(n.next)\n}\n\n// Index returns the index of the current node in the parent array node.\n//\n// Usage:\n//\n//\troot := ArrayNode(\"\", []*Node{StringNode(\"\", \"foo\"), NumberNode(\"\", 1)})\n//\tif root == nil {\n//\t\tt.Errorf(\"ArrayNode returns nil\")\n//\t}\n//\n//\tif root.MustIndex(1).Index() != 1 {\n//\t\tt.Errorf(\"Index returns wrong index: %d\", root.MustIndex(1).Index())\n//\t}\n//\n// We can also use the index to the byte slice of the JSON data directly.\n//\n// Example:\n//\n//\troot := Unmarshal([]byte(`[\"foo\", 1]`))\n//\tif root == nil {\n//\t\tt.Errorf(\"Unmarshal returns nil\")\n//\t}\n//\n//\tif string(root.MustIndex(1).source()) != \"1\" {\n//\t\tt.Errorf(\"source returns wrong result: %s\", root.MustIndex(1).source())\n//\t}\nfunc (n *Node) Index() int {\n\tif n == nil || n.index == nil {\n\t\treturn -1\n\t}\n\n\treturn *n.index\n}\n\n// MustIndex returns the array element at the given index.\n//\n// If the index is negative, it returns the index is from the end of the array.\n// Also, it panics if the index is not found.\n//\n// check the Index method for detailed usage.\nfunc (n *Node) MustIndex(expectIdx int) *Node {\n\tval, err := n.GetIndex(expectIdx)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn val\n}\n\n// GetIndex returns the array element at the given index.\n//\n// if the index is negative, it returns the index is from the end of the array.\nfunc (n *Node) GetIndex(idx int) (*Node, error) {\n\tif n == nil {\n\t\treturn nil, errNilNode\n\t}\n\n\tif !n.IsArray() {\n\t\treturn nil, errNotArrayNode\n\t}\n\n\tif idx \u003e n.Size() {\n\t\treturn nil, errors.New(\"input index exceeds the array size\")\n\t}\n\n\tif idx \u003c 0 {\n\t\tidx += len(n.next)\n\t}\n\n\tchild, ok := n.next[strconv.Itoa(idx)]\n\tif !ok {\n\t\treturn nil, errIndexNotFound\n\t}\n\n\treturn child, nil\n}\n\n// DeleteIndex removes the array element at the given index.\nfunc (n *Node) DeleteIndex(idx int) error {\n\tnode, err := n.GetIndex(idx)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn n.remove(node)\n}\n\n// NullNode creates a new null type node.\n//\n// Usage:\n//\n//\t_ := NullNode(\"\")\nfunc NullNode(key string) *Node {\n\treturn \u0026Node{\n\t\tkey:      \u0026key,\n\t\tvalue:    nil,\n\t\tnodeType: Null,\n\t\tmodified: true,\n\t}\n}\n\n// NumberNode creates a new number type node.\n//\n// Usage:\n//\n//\troot := NumberNode(\"\", 1)\n//\tif root == nil {\n//\t\tt.Errorf(\"NumberNode returns nil\")\n//\t}\nfunc NumberNode(key string, value float64) *Node {\n\treturn \u0026Node{\n\t\tkey:      \u0026key,\n\t\tvalue:    value,\n\t\tnodeType: Number,\n\t\tmodified: true,\n\t}\n}\n\n// StringNode creates a new string type node.\n//\n// Usage:\n//\n//\troot := StringNode(\"\", \"foo\")\n//\tif root == nil {\n//\t\tt.Errorf(\"StringNode returns nil\")\n//\t}\nfunc StringNode(key string, value string) *Node {\n\treturn \u0026Node{\n\t\tkey:      \u0026key,\n\t\tvalue:    value,\n\t\tnodeType: String,\n\t\tmodified: true,\n\t}\n}\n\n// BoolNode creates a new given boolean value node.\n//\n// Usage:\n//\n//\troot := BoolNode(\"\", true)\n//\tif root == nil {\n//\t\tt.Errorf(\"BoolNode returns nil\")\n//\t}\nfunc BoolNode(key string, value bool) *Node {\n\treturn \u0026Node{\n\t\tkey:      \u0026key,\n\t\tvalue:    value,\n\t\tnodeType: Boolean,\n\t\tmodified: true,\n\t}\n}\n\n// ArrayNode creates a new array type node.\n//\n// If the given value is nil, it creates an empty array node.\n//\n// Usage:\n//\n//\troot := ArrayNode(\"\", []*Node{StringNode(\"\", \"foo\"), NumberNode(\"\", 1)})\n//\tif root == nil {\n//\t\tt.Errorf(\"ArrayNode returns nil\")\n//\t}\nfunc ArrayNode(key string, value []*Node) *Node {\n\tcurr := \u0026Node{\n\t\tkey:      \u0026key,\n\t\tnodeType: Array,\n\t\tmodified: true,\n\t}\n\n\tcurr.next = make(map[string]*Node, len(value))\n\tif value != nil {\n\t\tcurr.value = value\n\n\t\tfor i, v := range value {\n\t\t\tidx := i\n\t\t\tcurr.next[strconv.Itoa(i)] = v\n\n\t\t\tv.prev = curr\n\t\t\tv.index = \u0026idx\n\t\t}\n\t}\n\n\treturn curr\n}\n\n// ObjectNode creates a new object type node.\n//\n// If the given value is nil, it creates an empty object node.\n//\n// next is a map of key and value pairs of the object.\nfunc ObjectNode(key string, value map[string]*Node) *Node {\n\tcurr := \u0026Node{\n\t\tnodeType: Object,\n\t\tkey:      \u0026key,\n\t\tnext:     value,\n\t\tmodified: true,\n\t}\n\n\tif value != nil {\n\t\tcurr.value = value\n\n\t\tfor key, val := range value {\n\t\t\tvkey := key\n\t\t\tval.prev = curr\n\t\t\tval.key = \u0026vkey\n\t\t}\n\t} else {\n\t\tcurr.next = make(map[string]*Node)\n\t}\n\n\treturn curr\n}\n\n// IsArray returns true if the current node is array type.\nfunc (n *Node) IsArray() bool {\n\treturn n.nodeType == Array\n}\n\n// IsObject returns true if the current node is object type.\nfunc (n *Node) IsObject() bool {\n\treturn n.nodeType == Object\n}\n\n// IsNull returns true if the current node is null type.\nfunc (n *Node) IsNull() bool {\n\treturn n.nodeType == Null\n}\n\n// IsBool returns true if the current node is boolean type.\nfunc (n *Node) IsBool() bool {\n\treturn n.nodeType == Boolean\n}\n\n// IsString returns true if the current node is string type.\nfunc (n *Node) IsString() bool {\n\treturn n.nodeType == String\n}\n\n// IsNumber returns true if the current node is number type.\nfunc (n *Node) IsNumber() bool {\n\treturn n.nodeType == Number\n}\n\n// ready checks the current node is ready or not.\n//\n// the meaning of ready is the current node is parsed and has a valid value.\nfunc (n *Node) ready() bool {\n\treturn n.borders[1] != 0\n}\n\n// source returns the source of the current node.\nfunc (n *Node) source() []byte {\n\tif n == nil {\n\t\treturn nil\n\t}\n\n\tif n.ready() \u0026\u0026 !n.modified \u0026\u0026 n.data != nil {\n\t\treturn (n.data)[n.borders[0]:n.borders[1]]\n\t}\n\n\treturn nil\n}\n\n// root returns the root node of the current node.\nfunc (n *Node) root() *Node {\n\tif n == nil {\n\t\treturn nil\n\t}\n\n\tcurr := n\n\tfor curr.prev != nil {\n\t\tcurr = curr.prev\n\t}\n\n\treturn curr\n}\n\n// GetNull returns the null value if current node is null type.\n//\n// Usage:\n//\n//\troot := Unmarshal([]byte(\"null\"))\n//\tval, err := root.GetNull()\n//\tif err != nil {\n//\t\tt.Errorf(\"GetNull returns error: %v\", err)\n//\t}\n//\tif val != nil {\n//\t\tt.Errorf(\"GetNull returns wrong result: %v\", val)\n//\t}\nfunc (n *Node) GetNull() (any, error) {\n\tif n == nil {\n\t\treturn nil, errNilNode\n\t}\n\n\tif !n.IsNull() {\n\t\treturn nil, errNotNullNode\n\t}\n\n\treturn nil, nil\n}\n\n// MustNull returns the null value if current node is null type.\n//\n// It panics if the current node is not null type.\nfunc (n *Node) MustNull() any {\n\tv, err := n.GetNull()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn v\n}\n\n// GetNumeric returns the numeric (int/float) value if current node is number type.\n//\n// Usage:\n//\n//\troot := Unmarshal([]byte(\"10.5\"))\n//\tval, err := root.GetNumeric()\n//\tif err != nil {\n//\t\tt.Errorf(\"GetNumeric returns error: %v\", err)\n//\t}\n//\tprintln(val) // 10.5\nfunc (n *Node) GetNumeric() (float64, error) {\n\tif n == nil {\n\t\treturn 0, errNilNode\n\t}\n\n\tif n.nodeType != Number {\n\t\treturn 0, errNotNumberNode\n\t}\n\n\tval, err := n.Value()\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\n\tv, ok := val.(float64)\n\tif !ok {\n\t\treturn 0, errNotNumberNode\n\t}\n\n\treturn v, nil\n}\n\n// MustNumeric returns the numeric (int/float) value if current node is number type.\n//\n// It panics if the current node is not number type.\nfunc (n *Node) MustNumeric() float64 {\n\tv, err := n.GetNumeric()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn v\n}\n\n// GetString returns the string value if current node is string type.\n//\n// Usage:\n//\n//\troot, err := Unmarshal([]byte(\"foo\"))\n//\tif err != nil {\n//\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n//\t}\n//\n//\tstr, err := root.GetString()\n//\tif err != nil {\n//\t\tt.Errorf(\"should retrieve string value: %s\", err)\n//\t}\n//\n//\tprintln(str) // \"foo\"\nfunc (n *Node) GetString() (string, error) {\n\tif n == nil {\n\t\treturn \"\", errEmptyStringNode\n\t}\n\n\tif !n.IsString() {\n\t\treturn \"\", errNotStringNode\n\t}\n\n\tval, err := n.Value()\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tv, ok := val.(string)\n\tif !ok {\n\t\treturn \"\", errNotStringNode\n\t}\n\n\treturn v, nil\n}\n\n// MustString returns the string value if current node is string type.\n//\n// It panics if the current node is not string type.\nfunc (n *Node) MustString() string {\n\tv, err := n.GetString()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn v\n}\n\n// GetBool returns the boolean value if current node is boolean type.\n//\n// Usage:\n//\n//\troot := Unmarshal([]byte(\"true\"))\n//\tval, err := root.GetBool()\n//\tif err != nil {\n//\t\tt.Errorf(\"GetBool returns error: %v\", err)\n//\t}\n//\tprintln(val) // true\nfunc (n *Node) GetBool() (bool, error) {\n\tif n == nil {\n\t\treturn false, errNilNode\n\t}\n\n\tif n.nodeType != Boolean {\n\t\treturn false, errNotBoolNode\n\t}\n\n\tval, err := n.Value()\n\tif err != nil {\n\t\treturn false, err\n\t}\n\n\tv, ok := val.(bool)\n\tif !ok {\n\t\treturn false, errNotBoolNode\n\t}\n\n\treturn v, nil\n}\n\n// MustBool returns the boolean value if current node is boolean type.\n//\n// It panics if the current node is not boolean type.\nfunc (n *Node) MustBool() bool {\n\tv, err := n.GetBool()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn v\n}\n\n// GetArray returns the array value if current node is array type.\n//\n// Usage:\n//\n//\t\troot := Must(Unmarshal([]byte(`[\"foo\", 1]`)))\n//\t\tarr, err := root.GetArray()\n//\t\tif err != nil {\n//\t\t\tt.Errorf(\"GetArray returns error: %v\", err)\n//\t\t}\n//\n//\t\tfor _, val := range arr {\n//\t\t\tprintln(val)\n//\t\t}\n//\n//\t result: \"foo\", 1\nfunc (n *Node) GetArray() ([]*Node, error) {\n\tif n == nil {\n\t\treturn nil, errNilNode\n\t}\n\n\tif n.nodeType != Array {\n\t\treturn nil, errNotArrayNode\n\t}\n\n\tval, err := n.Value()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tv, ok := val.([]*Node)\n\tif !ok {\n\t\treturn nil, errNotArrayNode\n\t}\n\n\treturn v, nil\n}\n\n// MustArray returns the array value if current node is array type.\n//\n// It panics if the current node is not array type.\nfunc (n *Node) MustArray() []*Node {\n\tv, err := n.GetArray()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn v\n}\n\n// AppendArray appends the given values to the current array node.\n//\n// If the current node is not array type, it returns an error.\n//\n// Example 1:\n//\n//\troot := Must(Unmarshal([]byte(`[{\"foo\":\"bar\"}]`)))\n//\tif err := root.AppendArray(NullNode(\"\")); err != nil {\n//\t\tt.Errorf(\"should not return error: %s\", err)\n//\t}\n//\n//\tresult: [{\"foo\":\"bar\"}, null]\n//\n// Example 2:\n//\n//\troot := Must(Unmarshal([]byte(`[\"bar\", \"baz\"]`)))\n//\terr := root.AppendArray(NumberNode(\"\", 1), StringNode(\"\", \"foo\"))\n//\tif err != nil {\n//\t\tt.Errorf(\"AppendArray returns error: %v\", err)\n//\t }\n//\n//\tresult: [\"bar\", \"baz\", 1, \"foo\"]\nfunc (n *Node) AppendArray(value ...*Node) error {\n\tif !n.IsArray() {\n\t\treturn errInvalidAppend\n\t}\n\n\tfor _, val := range value {\n\t\tif err := n.append(nil, val); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\tn.mark()\n\treturn nil\n}\n\n// ArrayEach executes the callback for each element in the JSON array.\n//\n// Usage:\n//\n//\tjsonArrayNode.ArrayEach(func(i int, valueNode *Node) {\n//\t    ufmt.Println(i, valueNode)\n//\t})\nfunc (n *Node) ArrayEach(callback func(i int, target *Node)) {\n\tif n == nil || !n.IsArray() {\n\t\treturn\n\t}\n\n\tfor idx := 0; idx \u003c len(n.next); idx++ {\n\t\telement, err := n.GetIndex(idx)\n\t\tif err != nil {\n\t\t\tcontinue\n\t\t}\n\n\t\tcallback(idx, element)\n\t}\n}\n\n// GetObject returns the object value if current node is object type.\n//\n// Usage:\n//\n//\troot := Must(Unmarshal([]byte(`{\"key\": \"value\"}`)))\n//\tobj, err := root.GetObject()\n//\tif err != nil {\n//\t\tt.Errorf(\"GetObject returns error: %v\", err)\n//\t}\n//\n//\tresult: map[string]*Node{\"key\": StringNode(\"key\", \"value\")}\nfunc (n *Node) GetObject() (map[string]*Node, error) {\n\tif n == nil {\n\t\treturn nil, errNilNode\n\t}\n\n\tif !n.IsObject() {\n\t\treturn nil, errNotObjectNode\n\t}\n\n\tval, err := n.Value()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tv, ok := val.(map[string]*Node)\n\tif !ok {\n\t\treturn nil, errNotObjectNode\n\t}\n\n\treturn v, nil\n}\n\n// MustObject returns the object value if current node is object type.\n//\n// It panics if the current node is not object type.\nfunc (n *Node) MustObject() map[string]*Node {\n\tv, err := n.GetObject()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn v\n}\n\n// AppendObject appends the given key and value to the current object node.\n//\n// If the current node is not object type, it returns an error.\nfunc (n *Node) AppendObject(key string, value *Node) error {\n\tif !n.IsObject() {\n\t\treturn errInvalidAppend\n\t}\n\n\tif err := n.append(\u0026key, value); err != nil {\n\t\treturn err\n\t}\n\n\tn.mark()\n\treturn nil\n}\n\n// ObjectEach executes the callback for each key-value pair in the JSON object.\n//\n// Usage:\n//\n//\tjsonObjectNode.ObjectEach(func(key string, valueNode *Node) {\n//\t    ufmt.Println(key, valueNode)\n//\t})\nfunc (n *Node) ObjectEach(callback func(key string, value *Node)) {\n\tif n == nil || !n.IsObject() {\n\t\treturn\n\t}\n\n\tfor key, child := range n.next {\n\t\tcallback(key, child)\n\t}\n}\n\n// String converts the node to a string representation.\nfunc (n *Node) String() string {\n\tif n == nil {\n\t\treturn \"\"\n\t}\n\n\tif n.ready() \u0026\u0026 !n.modified {\n\t\treturn string(n.source())\n\t}\n\n\tval, err := Marshal(n)\n\tif err != nil {\n\t\treturn \"error: \" + err.Error()\n\t}\n\n\treturn string(val)\n}\n\n// Path builds the path of the current node.\n//\n// For example:\n//\n//\t{ \"key\": { \"sub\": [ \"val1\", \"val2\" ] }}\n//\n// The path of \"val2\" is: $.key.sub[1]\nfunc (n *Node) Path() string {\n\tif n == nil {\n\t\treturn \"\"\n\t}\n\n\tvar sb strings.Builder\n\n\tif n.prev == nil {\n\t\tsb.WriteString(\"$\")\n\t} else {\n\t\tsb.WriteString(n.prev.Path())\n\n\t\tif n.key != nil {\n\t\t\tsb.WriteString(\"['\" + n.Key() + \"']\")\n\t\t} else {\n\t\t\tsb.WriteString(\"[\" + strconv.Itoa(n.Index()) + \"]\")\n\t\t}\n\t}\n\n\treturn sb.String()\n}\n\n// mark marks the current node as modified.\nfunc (n *Node) mark() {\n\tnode := n\n\tfor node != nil \u0026\u0026 !node.modified {\n\t\tnode.modified = true\n\t\tnode = node.prev\n\t}\n}\n\n// isContainer checks the current node type is array or object.\nfunc (n *Node) isContainer() bool {\n\treturn n.IsArray() || n.IsObject()\n}\n\n// remove removes the value from the current container type node.\nfunc (n *Node) remove(v *Node) error {\n\tif !n.isContainer() {\n\t\treturn ufmt.Errorf(\n\t\t\t\"can't remove value from non-array or non-object node. got=%s\",\n\t\t\tn.Type().String(),\n\t\t)\n\t}\n\n\tif v.prev != n {\n\t\treturn errors.New(\"invalid parent node\")\n\t}\n\n\tn.mark()\n\tif n.IsArray() {\n\t\tdelete(n.next, strconv.Itoa(*v.index))\n\t\tn.dropIndex(*v.index)\n\t} else {\n\t\tdelete(n.next, *v.key)\n\t}\n\n\tv.prev = nil\n\treturn nil\n}\n\n// dropIndex rebase the index of current array node values.\nfunc (n *Node) dropIndex(idx int) {\n\tfor i := idx + 1; i \u003c= len(n.next); i++ {\n\t\tprv := i - 1\n\t\tif curr, ok := n.next[strconv.Itoa(i)]; ok {\n\t\t\tcurr.index = \u0026prv\n\t\t\tn.next[strconv.Itoa(prv)] = curr\n\t\t}\n\n\t\tdelete(n.next, strconv.Itoa(i))\n\t}\n}\n\n// append is a helper function to append the given value to the current container type node.\nfunc (n *Node) append(key *string, val *Node) error {\n\tif n.isSameOrParentNode(val) {\n\t\treturn errInvalidAppendCycle\n\t}\n\n\tif val.prev != nil {\n\t\tif err := val.prev.remove(val); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\tval.prev = n\n\tval.key = key\n\n\tif key == nil {\n\t\tsize := len(n.next)\n\t\tval.index = \u0026size\n\t\tn.next[strconv.Itoa(size)] = val\n\t} else {\n\t\tif old, ok := n.next[*key]; ok {\n\t\t\tif err := n.remove(old); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\tn.next[*key] = val\n\t}\n\n\treturn nil\n}\n\nfunc (n *Node) isSameOrParentNode(nd *Node) bool {\n\treturn n == nd || n.isParentNode(nd)\n}\n\nfunc (n *Node) isParentNode(nd *Node) bool {\n\tif n == nil {\n\t\treturn false\n\t}\n\n\tfor curr := nd.prev; curr != nil; curr = curr.prev {\n\t\tif curr == n {\n\t\t\treturn true\n\t\t}\n\t}\n\n\treturn false\n}\n\n// cptrs returns the pointer of the given string value.\nfunc cptrs(cpy *string) *string {\n\tif cpy == nil {\n\t\treturn nil\n\t}\n\n\tval := *cpy\n\n\treturn \u0026val\n}\n\n// cptri returns the pointer of the given integer value.\nfunc cptri(i *int) *int {\n\tif i == nil {\n\t\treturn nil\n\t}\n\n\tval := *i\n\treturn \u0026val\n}\n\n// Must panics if the given node is not fulfilled the expectation.\n// Usage:\n//\n//\tnode := Must(Unmarshal([]byte(`{\"key\": \"value\"}`))\nfunc Must(root *Node, expect error) *Node {\n\tif expect != nil {\n\t\tpanic(expect)\n\t}\n\n\treturn root\n}\n"},{"name":"node_test.gno","body":"package json\n\nimport (\n\t\"bytes\"\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tnilKey   *string\n\tdummyKey = \"key\"\n)\n\ntype _args struct {\n\tprev *Node\n\tbuf  *buffer\n\ttyp  ValueType\n\tkey  **string\n}\n\ntype simpleNode struct {\n\tname string\n\tnode *Node\n}\n\nfunc TestNode_CreateNewNode(t *testing.T) {\n\trel := \u0026dummyKey\n\n\ttests := []struct {\n\t\tname        string\n\t\targs        _args\n\t\texpectCurr  *Node\n\t\texpectErr   bool\n\t\texpectPanic bool\n\t}{\n\t\t{\n\t\t\tname: \"child for non container type\",\n\t\t\targs: _args{\n\t\t\t\tprev: BoolNode(\"\", true),\n\t\t\t\tbuf:  newBuffer(make([]byte, 10)),\n\t\t\t\ttyp:  Boolean,\n\t\t\t\tkey:  \u0026rel,\n\t\t\t},\n\t\t\texpectCurr: nil,\n\t\t\texpectErr:  true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tif tt.expectPanic {\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t\tt.Errorf(\"%s panic occurred when not expected: %v\", tt.name, r)\n\t\t\t\t} else if tt.expectPanic {\n\t\t\t\t\tt.Errorf(\"%s expected panic but didn't occur\", tt.name)\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tgot, err := NewNode(tt.args.prev, tt.args.buf, tt.args.typ, tt.args.key)\n\t\t\tif (err != nil) != tt.expectErr {\n\t\t\t\tt.Errorf(\"%s error = %v, expect error %v\", tt.name, err, tt.expectErr)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif tt.expectErr {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif !compareNodes(got, tt.expectCurr) {\n\t\t\t\tt.Errorf(\"%s got = %v, want %v\", tt.name, got, tt.expectCurr)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_Value(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tdata        []byte\n\t\t_type       ValueType\n\t\texpected    any\n\t\terrExpected bool\n\t}{\n\t\t{name: \"null\", data: []byte(\"null\"), _type: Null, expected: nil},\n\t\t{name: \"1\", data: []byte(\"1\"), _type: Number, expected: float64(1)},\n\t\t{name: \".1\", data: []byte(\".1\"), _type: Number, expected: float64(.1)},\n\t\t{name: \"-.1e1\", data: []byte(\"-.1e1\"), _type: Number, expected: float64(-1)},\n\t\t{name: \"string\", data: []byte(\"\\\"foo\\\"\"), _type: String, expected: \"foo\"},\n\t\t{name: \"space\", data: []byte(\"\\\"foo bar\\\"\"), _type: String, expected: \"foo bar\"},\n\t\t{name: \"true\", data: []byte(\"true\"), _type: Boolean, expected: true},\n\t\t{name: \"invalid true\", data: []byte(\"tru\"), _type: Unknown, errExpected: true},\n\t\t{name: \"invalid false\", data: []byte(\"fals\"), _type: Unknown, errExpected: true},\n\t\t{name: \"false\", data: []byte(\"false\"), _type: Boolean, expected: false},\n\t\t{name: \"e1\", data: []byte(\"e1\"), _type: Unknown, errExpected: true},\n\t\t{name: \"1a\", data: []byte(\"1a\"), _type: Unknown, errExpected: true},\n\t\t{name: \"string error\", data: []byte(\"\\\"foo\\nbar\\\"\"), _type: String, errExpected: true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tcurr := \u0026Node{\n\t\t\t\tdata:     tt.data,\n\t\t\t\tnodeType: tt._type,\n\t\t\t\tborders:  [2]int{0, len(tt.data)},\n\t\t\t}\n\n\t\t\tgot, err := curr.Value()\n\t\t\tif err != nil {\n\t\t\t\tif !tt.errExpected {\n\t\t\t\t\tt.Errorf(\"%s error = %v, expect error %v\", tt.name, err, tt.errExpected)\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"%s got = %v, want %v\", tt.name, got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_Delete(t *testing.T) {\n\troot := Must(Unmarshal([]byte(`{\"foo\":\"bar\"}`)))\n\tif err := root.Delete(); err != nil {\n\t\tt.Errorf(\"Delete returns error: %v\", err)\n\t}\n\n\tif value, err := Marshal(root); err != nil {\n\t\tt.Errorf(\"Marshal returns error: %v\", err)\n\t} else if string(value) != `{\"foo\":\"bar\"}` {\n\t\tt.Errorf(\"Marshal returns wrong value: %s\", string(value))\n\t}\n\n\tfoo := root.MustKey(\"foo\")\n\tif err := foo.Delete(); err != nil {\n\t\tt.Errorf(\"Delete returns error while handling foo: %v\", err)\n\t}\n\n\tif value, err := Marshal(root); err != nil {\n\t\tt.Errorf(\"Marshal returns error: %v\", err)\n\t} else if string(value) != `{}` {\n\t\tt.Errorf(\"Marshal returns wrong value: %s\", string(value))\n\t}\n\n\tif value, err := Marshal(foo); err != nil {\n\t\tt.Errorf(\"Marshal returns error: %v\", err)\n\t} else if string(value) != `\"bar\"` {\n\t\tt.Errorf(\"Marshal returns wrong value: %s\", string(value))\n\t}\n\n\tif foo.prev != nil {\n\t\tt.Errorf(\"foo.prev should be nil\")\n\t}\n}\n\nfunc TestNode_ObjectNode(t *testing.T) {\n\tobjs := map[string]*Node{\n\t\t\"key1\": NullNode(\"null\"),\n\t\t\"key2\": NumberNode(\"answer\", 42),\n\t\t\"key3\": StringNode(\"string\", \"foobar\"),\n\t\t\"key4\": BoolNode(\"bool\", true),\n\t}\n\n\tnode := ObjectNode(\"test\", objs)\n\n\tif len(node.next) != len(objs) {\n\t\tt.Errorf(\"ObjectNode: want %v got %v\", len(objs), len(node.next))\n\t}\n\n\tfor k, v := range objs {\n\t\tif node.next[k] == nil {\n\t\t\tt.Errorf(\"ObjectNode: want %v got %v\", v, node.next[k])\n\t\t}\n\t}\n}\n\nfunc TestNode_AppendObject(t *testing.T) {\n\tif err := Must(Unmarshal([]byte(`{\"foo\":\"bar\",\"baz\":null}`))).AppendObject(\"biz\", NullNode(\"\")); err != nil {\n\t\tt.Errorf(\"AppendArray should return error\")\n\t}\n\n\troot := Must(Unmarshal([]byte(`{\"foo\":\"bar\"}`)))\n\tif err := root.AppendObject(\"baz\", NullNode(\"\")); err != nil {\n\t\tt.Errorf(\"AppendObject should not return error: %s\", err)\n\t}\n\n\tif value, err := Marshal(root); err != nil {\n\t\tt.Errorf(\"Marshal returns error: %v\", err)\n\t} else if isSameObject(string(value), `\"{\"foo\":\"bar\",\"baz\":null}\"`) {\n\t\tt.Errorf(\"Marshal returns wrong value: %s\", string(value))\n\t}\n\n\t// FIXME: this may fail if execute test in more than 3 times in a row.\n\tif err := root.AppendObject(\"biz\", NumberNode(\"\", 42)); err != nil {\n\t\tt.Errorf(\"AppendObject returns error: %v\", err)\n\t}\n\n\tval, err := Marshal(root)\n\tif err != nil {\n\t\tt.Errorf(\"Marshal returns error: %v\", err)\n\t}\n\n\t// FIXME: this may fail if execute test in more than 3 times in a row.\n\tif isSameObject(string(val), `\"{\"foo\":\"bar\",\"baz\":null,\"biz\":42}\"`) {\n\t\tt.Errorf(\"Marshal returns wrong value: %s\", string(val))\n\t}\n}\n\nfunc TestNode_ArrayNode(t *testing.T) {\n\tarr := []*Node{\n\t\tNullNode(\"nil\"),\n\t\tNumberNode(\"num\", 42),\n\t\tStringNode(\"str\", \"foobar\"),\n\t\tBoolNode(\"bool\", true),\n\t}\n\n\tnode := ArrayNode(\"test\", arr)\n\n\tif len(node.next) != len(arr) {\n\t\tt.Errorf(\"ArrayNode: want %v got %v\", len(arr), len(node.next))\n\t}\n\n\tfor i, v := range arr {\n\t\tif node.next[strconv.Itoa(i)] == nil {\n\t\t\tt.Errorf(\"ArrayNode: want %v got %v\", v, node.next[strconv.Itoa(i)])\n\t\t}\n\t}\n}\n\nfunc TestNode_AppendArray(t *testing.T) {\n\tif err := Must(Unmarshal([]byte(`[{\"foo\":\"bar\"}]`))).AppendArray(NullNode(\"\")); err != nil {\n\t\tt.Errorf(\"should return error\")\n\t}\n\n\troot := Must(Unmarshal([]byte(`[{\"foo\":\"bar\"}]`)))\n\tif err := root.AppendArray(NullNode(\"\")); err != nil {\n\t\tt.Errorf(\"should not return error: %s\", err)\n\t}\n\n\tif value, err := Marshal(root); err != nil {\n\t\tt.Errorf(\"Marshal returns error: %v\", err)\n\t} else if string(value) != `[{\"foo\":\"bar\"},null]` {\n\t\tt.Errorf(\"Marshal returns wrong value: %s\", string(value))\n\t}\n\n\tif err := root.AppendArray(\n\t\tNumberNode(\"\", 1),\n\t\tStringNode(\"\", \"foo\"),\n\t\tMust(Unmarshal([]byte(`[0,1,null,true,\"example\"]`))),\n\t\tMust(Unmarshal([]byte(`{\"foo\": true, \"bar\": null, \"baz\": 123}`))),\n\t); err != nil {\n\t\tt.Errorf(\"AppendArray returns error: %v\", err)\n\t}\n\n\tif value, err := Marshal(root); err != nil {\n\t\tt.Errorf(\"Marshal returns error: %v\", err)\n\t} else if string(value) != `[{\"foo\":\"bar\"},null,1,\"foo\",[0,1,null,true,\"example\"],{\"foo\": true, \"bar\": null, \"baz\": 123}]` {\n\t\tt.Errorf(\"Marshal returns wrong value: %s\", string(value))\n\t}\n}\n\n/******** value getter ********/\n\nfunc TestNode_GetBool(t *testing.T) {\n\troot, err := Unmarshal([]byte(`true`))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err.Error())\n\t\treturn\n\t}\n\n\tvalue, err := root.GetBool()\n\tif err != nil {\n\t\tt.Errorf(\"Error on root.GetBool(): %s\", err.Error())\n\t}\n\n\tif !value {\n\t\tt.Errorf(\"root.GetBool() is corrupted\")\n\t}\n}\n\nfunc TestNode_GetBool_NotSucceed(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"nil node\", (*Node)(nil)},\n\t\t{\"literally null node\", NullNode(\"\")},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif _, err := tt.node.GetBool(); err == nil {\n\t\t\t\tt.Errorf(\"%s should be an error\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_IsBool(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"true\", BoolNode(\"\", true)},\n\t\t{\"false\", BoolNode(\"\", false)},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif !tt.node.IsBool() {\n\t\t\t\tt.Errorf(\"%s should be a bool\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_IsBool_With_Unmarshal(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tjson []byte\n\t\twant bool\n\t}{\n\t\t{\"true\", []byte(\"true\"), true},\n\t\t{\"false\", []byte(\"false\"), true},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\troot, err := Unmarshal(tt.json)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"Error on Unmarshal(): %s\", err.Error())\n\t\t\t}\n\n\t\t\tif root.IsBool() != tt.want {\n\t\t\t\tt.Errorf(\"%s should be a bool\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nvar nullJson = []byte(`null`)\n\nfunc TestNode_GetNull(t *testing.T) {\n\troot, err := Unmarshal(nullJson)\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err.Error())\n\t}\n\n\tvalue, err := root.GetNull()\n\tif err != nil {\n\t\tt.Errorf(\"error occurred while getting null, %s\", err)\n\t}\n\n\tif value != nil {\n\t\tt.Errorf(\"value is not matched. expected: nil, got: %v\", value)\n\t}\n}\n\nfunc TestNode_GetNull_NotSucceed(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"nil node\", (*Node)(nil)},\n\t\t{\"number node is null\", NumberNode(\"\", 42)},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif _, err := tt.node.GetNull(); err == nil {\n\t\t\t\tt.Errorf(\"%s should be an error\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_MustNull(t *testing.T) {\n\troot, err := Unmarshal(nullJson)\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err.Error())\n\t}\n\n\tvalue := root.MustNull()\n\tif value != nil {\n\t\tt.Errorf(\"value is not matched. expected: nil, got: %v\", value)\n\t}\n}\n\nfunc TestNode_GetNumeric_Float(t *testing.T) {\n\troot, err := Unmarshal([]byte(`123.456`))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n\t\treturn\n\t}\n\n\tvalue, err := root.GetNumeric()\n\tif err != nil {\n\t\tt.Errorf(\"Error on root.GetNumeric(): %s\", err)\n\t}\n\n\tif value != float64(123.456) {\n\t\tt.Errorf(ufmt.Sprintf(\"value is not matched. expected: 123.456, got: %v\", value))\n\t}\n}\n\nfunc TestNode_GetNumeric_Scientific_Notation(t *testing.T) {\n\troot, err := Unmarshal([]byte(`1e3`))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n\t\treturn\n\t}\n\n\tvalue, err := root.GetNumeric()\n\tif err != nil {\n\t\tt.Errorf(\"Error on root.GetNumeric(): %s\", err)\n\t}\n\n\tif value != float64(1000) {\n\t\tt.Errorf(ufmt.Sprintf(\"value is not matched. expected: 1000, got: %v\", value))\n\t}\n}\n\nfunc TestNode_GetNumeric_With_Unmarshal(t *testing.T) {\n\troot, err := Unmarshal([]byte(`123`))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n\t\treturn\n\t}\n\n\tvalue, err := root.GetNumeric()\n\tif err != nil {\n\t\tt.Errorf(\"Error on root.GetNumeric(): %s\", err)\n\t}\n\n\tif value != float64(123) {\n\t\tt.Errorf(ufmt.Sprintf(\"value is not matched. expected: 123, got: %v\", value))\n\t}\n}\n\nfunc TestNode_GetNumeric_NotSucceed(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"nil node\", (*Node)(nil)},\n\t\t{\"null node\", NullNode(\"\")},\n\t\t{\"string node\", StringNode(\"\", \"123\")},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif _, err := tt.node.GetNumeric(); err == nil {\n\t\t\t\tt.Errorf(\"%s should be an error\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_GetString(t *testing.T) {\n\troot, err := Unmarshal([]byte(`\"123foobar 3456\"`))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n\t}\n\n\tvalue, err := root.GetString()\n\tif err != nil {\n\t\tt.Errorf(\"Error on root.GetString(): %s\", err)\n\t}\n\n\tif value != \"123foobar 3456\" {\n\t\tt.Errorf(ufmt.Sprintf(\"value is not matched. expected: 123, got: %s\", value))\n\t}\n}\n\nfunc TestNode_GetString_NotSucceed(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"nil node\", (*Node)(nil)},\n\t\t{\"null node\", NullNode(\"\")},\n\t\t{\"number node\", NumberNode(\"\", 123)},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif _, err := tt.node.GetString(); err == nil {\n\t\t\t\tt.Errorf(\"%s should be an error\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_MustString(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tdata []byte\n\t}{\n\t\t{\"foo\", []byte(`\"foo\"`)},\n\t\t{\"foo bar\", []byte(`\"foo bar\"`)},\n\t\t{\"\", []byte(`\"\"`)},\n\t\t{\"안녕하세요\", []byte(`\"안녕하세요\"`)},\n\t\t{\"こんにちは\", []byte(`\"こんにちは\"`)},\n\t\t{\"你好\", []byte(`\"你好\"`)},\n\t\t{\"one \\\"encoded\\\" string\", []byte(`\"one \\\"encoded\\\" string\"`)},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\troot, err := Unmarshal(tt.data)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n\t\t\t}\n\n\t\t\tvalue := root.MustString()\n\t\t\tif value != tt.name {\n\t\t\t\tt.Errorf(\"value is not matched. expected: %s, got: %s\", tt.name, value)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestUnmarshal_Array(t *testing.T) {\n\troot, err := Unmarshal([]byte(\" [1,[\\\"1\\\",[1,[1,2,3]]]]\\r\\n\"))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal: %s\", err.Error())\n\t}\n\n\tif root == nil {\n\t\tt.Errorf(\"Error on Unmarshal: root is nil\")\n\t}\n\n\tif root.Type() != Array {\n\t\tt.Errorf(\"Error on Unmarshal: wrong type\")\n\t}\n\n\tarray, err := root.GetArray()\n\tif err != nil {\n\t\tt.Errorf(\"error occurred while getting array, %s\", err)\n\t} else if len(array) != 2 {\n\t\tt.Errorf(\"expected 2 elements, got %d\", len(array))\n\t} else if val, err := array[0].GetNumeric(); err != nil {\n\t\tt.Errorf(\"value of array[0] is not numeric. got: %v\", array[0].value)\n\t} else if val != 1 {\n\t\tt.Errorf(\"Error on array[0].GetNumeric(): expected to be '1', got: %v\", val)\n\t} else if val, err := array[1].GetArray(); err != nil {\n\t\tt.Errorf(\"error occurred while getting array, %s\", err.Error())\n\t} else if len(val) != 2 {\n\t\tt.Errorf(\"Error on array[1].GetArray(): expected 2 elements, got %d\", len(val))\n\t} else if el, err := val[0].GetString(); err != nil {\n\t\tt.Errorf(\"error occurred while getting string, %s\", err.Error())\n\t} else if el != \"1\" {\n\t\tt.Errorf(\"Error on val[0].GetString(): expected to be '1', got: %s\", el)\n\t}\n}\n\nvar sampleArr = []byte(`[-1, 2, 3, 4, 5, 6]`)\n\nfunc TestNode_GetArray(t *testing.T) {\n\troot, err := Unmarshal(sampleArr)\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n\t\treturn\n\t}\n\n\tarray, err := root.GetArray()\n\tif err != nil {\n\t\tt.Errorf(\"Error on root.GetArray(): %s\", err)\n\t}\n\n\tif len(array) != 6 {\n\t\tt.Errorf(ufmt.Sprintf(\"length is not matched. expected: 3, got: %d\", len(array)))\n\t}\n\n\tfor i, node := range array {\n\t\tfor j, val := range []int{-1, 2, 3, 4, 5, 6} {\n\t\t\tif i == j {\n\t\t\t\tif v, err := node.GetNumeric(); err != nil {\n\t\t\t\t\tt.Errorf(ufmt.Sprintf(\"Error on node.GetNumeric(): %s\", err))\n\t\t\t\t} else if v != float64(val) {\n\t\t\t\t\tt.Errorf(ufmt.Sprintf(\"value is not matched. expected: %d, got: %v\", val, v))\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc TestNode_GetArray_NotSucceed(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"nil node\", (*Node)(nil)},\n\t\t{\"null node\", NullNode(\"\")},\n\t\t{\"number node\", NumberNode(\"\", 123)},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif _, err := tt.node.GetArray(); err == nil {\n\t\t\t\tt.Errorf(\"%s should be an error\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_IsArray(t *testing.T) {\n\troot, err := Unmarshal(sampleArr)\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err)\n\t\treturn\n\t}\n\n\tif root.Type() != Array {\n\t\tt.Errorf(ufmt.Sprintf(\"Must be an array. got: %s\", root.Type().String()))\n\t}\n}\n\nfunc TestNode_ArrayEach(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tjson     string\n\t\texpected []int\n\t}{\n\t\t{\n\t\t\tname:     \"empty array\",\n\t\t\tjson:     `[]`,\n\t\t\texpected: []int{},\n\t\t},\n\t\t{\n\t\t\tname:     \"single element\",\n\t\t\tjson:     `[42]`,\n\t\t\texpected: []int{42},\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple elements\",\n\t\t\tjson:     `[1, 2, 3, 4, 5]`,\n\t\t\texpected: []int{1, 2, 3, 4, 5},\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple elements but all values are same\",\n\t\t\tjson:     `[1, 1, 1, 1, 1]`,\n\t\t\texpected: []int{1, 1, 1, 1, 1},\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple elements with non-numeric values\",\n\t\t\tjson:     `[\"a\", \"b\", \"c\", \"d\", \"e\"]`,\n\t\t\texpected: []int{},\n\t\t},\n\t\t{\n\t\t\tname:     \"non-array node\",\n\t\t\tjson:     `{\"not\": \"an array\"}`,\n\t\t\texpected: []int{},\n\t\t},\n\t\t{\n\t\t\tname:     \"array containing numeric and non-numeric elements\",\n\t\t\tjson:     `[\"1\", 2, 3, \"4\", 5, \"6\"]`,\n\t\t\texpected: []int{2, 3, 5},\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\troot, err := Unmarshal([]byte(tc.json))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatalf(\"Unmarshal failed: %v\", err)\n\t\t\t}\n\n\t\t\tvar result []int // callback result\n\t\t\troot.ArrayEach(func(index int, element *Node) {\n\t\t\t\tif val, err := strconv.Atoi(element.String()); err == nil {\n\t\t\t\t\tresult = append(result, val)\n\t\t\t\t}\n\t\t\t})\n\n\t\t\tif len(result) != len(tc.expected) {\n\t\t\t\tt.Errorf(\"%s: expected %d elements, got %d\", tc.name, len(tc.expected), len(result))\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tfor i, val := range result {\n\t\t\t\tif val != tc.expected[i] {\n\t\t\t\t\tt.Errorf(\"%s: expected value at index %d to be %d, got %d\", tc.name, i, tc.expected[i], val)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_Key(t *testing.T) {\n\troot, err := Unmarshal([]byte(`{\"foo\": true, \"bar\": null, \"baz\": 123, \"biz\": [1,2,3]}`))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err.Error())\n\t}\n\n\tobj := root.MustObject()\n\tfor key, node := range obj {\n\t\tif key != node.Key() {\n\t\t\tt.Errorf(\"Key() = %v, want %v\", node.Key(), key)\n\t\t}\n\t}\n\n\tkeys := []string{\"foo\", \"bar\", \"baz\", \"biz\"}\n\tfor _, key := range keys {\n\t\tif obj[key].Key() != key {\n\t\t\tt.Errorf(\"Key() = %v, want %v\", obj[key].Key(), key)\n\t\t}\n\t}\n\n\t// TODO: resolve stack overflow\n\t// if root.MustKey(\"foo\").Clone().Key() != \"\" {\n\t// \tt.Errorf(\"wrong key found for cloned key\")\n\t// }\n\n\tif (*Node)(nil).Key() != \"\" {\n\t\tt.Errorf(\"wrong key found for nil node\")\n\t}\n}\n\nfunc TestNode_Size(t *testing.T) {\n\troot, err := Unmarshal(sampleArr)\n\tif err != nil {\n\t\tt.Errorf(\"error occurred while unmarshal\")\n\t}\n\n\tsize := root.Size()\n\tif size != 6 {\n\t\tt.Errorf(ufmt.Sprintf(\"Size() must be 6. got: %v\", size))\n\t}\n\n\tif (*Node)(nil).Size() != 0 {\n\t\tt.Errorf(ufmt.Sprintf(\"Size() must be 0. got: %v\", (*Node)(nil).Size()))\n\t}\n}\n\nfunc TestNode_Index(t *testing.T) {\n\troot, err := Unmarshal([]byte(`[1, 2, 3, 4, 5, 6]`))\n\tif err != nil {\n\t\tt.Error(\"error occurred while unmarshal\")\n\t}\n\n\tarr := root.MustArray()\n\tfor i, node := range arr {\n\t\tif i != node.Index() {\n\t\t\tt.Errorf(ufmt.Sprintf(\"Index() must be nil. got: %v\", i))\n\t\t}\n\t}\n}\n\nfunc TestNode_Index_NotSucceed(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tnode *Node\n\t\twant int\n\t}{\n\t\t{\"nil node\", (*Node)(nil), -1},\n\t\t{\"null node\", NullNode(\"\"), -1},\n\t\t{\"object node\", ObjectNode(\"\", nil), -1},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif got := tt.node.Index(); got != tt.want {\n\t\t\t\tt.Errorf(\"Index() = %v, want %v\", got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_GetIndex(t *testing.T) {\n\troot := Must(Unmarshal([]byte(`[1, 2, 3, 4, 5, 6]`)))\n\texpected := []int{1, 2, 3, 4, 5, 6}\n\n\tif len(expected) != root.Size() {\n\t\tt.Errorf(\"length is not matched. expected: %d, got: %d\", len(expected), root.Size())\n\t}\n\n\t// TODO: if length exceeds, stack overflow occurs. need to fix\n\tfor i, v := range expected {\n\t\tval, err := root.GetIndex(i)\n\t\tif err != nil {\n\t\t\tt.Errorf(\"error occurred while getting index %d, %s\", i, err)\n\t\t}\n\n\t\tif val.MustNumeric() != float64(v) {\n\t\t\tt.Errorf(\"value is not matched. expected: %d, got: %v\", v, val.MustNumeric())\n\t\t}\n\t}\n}\n\nfunc TestNode_GetIndex_InputIndex_Exceed_Original_Node_Index(t *testing.T) {\n\troot, err := Unmarshal([]byte(`[1, 2, 3, 4, 5, 6]`))\n\tif err != nil {\n\t\tt.Errorf(\"error occurred while unmarshal\")\n\t}\n\n\t_, err = root.GetIndex(10)\n\tif err == nil {\n\t\tt.Errorf(\"GetIndex should return error\")\n\t}\n}\n\nfunc TestNode_DeleteIndex(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\texpected string\n\t\tindex    int\n\t\tok       bool\n\t}{\n\t\t{`null`, ``, 0, false},\n\t\t{`1`, ``, 0, false},\n\t\t{`{}`, ``, 0, false},\n\t\t{`{\"foo\":\"bar\"}`, ``, 0, false},\n\t\t{`true`, ``, 0, false},\n\t\t{`[]`, ``, 0, false},\n\t\t{`[]`, ``, -1, false},\n\t\t{`[1]`, `[]`, 0, true},\n\t\t{`[{}]`, `[]`, 0, true},\n\t\t{`[{}, [], 42]`, `[{}, []]`, -1, true},\n\t\t{`[{}, [], 42]`, `[[], 42]`, 0, true},\n\t\t{`[{}, [], 42]`, `[{}, 42]`, 1, true},\n\t\t{`[{}, [], 42]`, `[{}, []]`, 2, true},\n\t\t{`[{}, [], 42]`, ``, 10, false},\n\t\t{`[{}, [], 42]`, ``, -10, false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\troot := Must(Unmarshal([]byte(tt.name)))\n\t\t\terr := root.DeleteIndex(tt.index)\n\t\t\tif err != nil \u0026\u0026 tt.ok {\n\t\t\t\tt.Errorf(\"DeleteIndex returns error: %v\", err)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_GetKey(t *testing.T) {\n\troot, err := Unmarshal([]byte(`{\"foo\": true, \"bar\": null}`))\n\tif err != nil {\n\t\tt.Error(\"error occurred while unmarshal\")\n\t}\n\n\tvalue, err := root.GetKey(\"foo\")\n\tif err != nil {\n\t\tt.Errorf(\"error occurred while getting key, %s\", err)\n\t}\n\n\tif value.MustBool() != true {\n\t\tt.Errorf(\"value is not matched. expected: true, got: %v\", value.MustBool())\n\t}\n\n\tvalue, err = root.GetKey(\"bar\")\n\tif err != nil {\n\t\tt.Errorf(\"error occurred while getting key, %s\", err)\n\t}\n\n\t_, err = root.GetKey(\"baz\")\n\tif err == nil {\n\t\tt.Errorf(\"key baz is not exist. must be failed\")\n\t}\n\n\tif value.MustNull() != nil {\n\t\tt.Errorf(\"value is not matched. expected: nil, got: %v\", value.MustNull())\n\t}\n}\n\nfunc TestNode_GetKey_NotSucceed(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"nil node\", (*Node)(nil)},\n\t\t{\"null node\", NullNode(\"\")},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif _, err := tt.node.GetKey(\"\"); err == nil {\n\t\t\t\tt.Errorf(\"%s should be an error\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_GetUniqueKeyList(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tjson     string\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\tname:     \"simple foo/bar\",\n\t\t\tjson:     `{\"foo\": true, \"bar\": null}`,\n\t\t\texpected: []string{\"foo\", \"bar\"},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty object\",\n\t\t\tjson:     `{}`,\n\t\t\texpected: []string{},\n\t\t},\n\t\t{\n\t\t\tname: \"nested object\",\n\t\t\tjson: `{\n\t\t\t\t\"outer\": {\n\t\t\t\t\t\"inner\": {\n\t\t\t\t\t\t\"key\": \"value\"\n\t\t\t\t\t},\n\t\t\t\t\t\"array\": [1, 2, 3]\n\t\t\t\t},\n\t\t\t\t\"another\": \"item\"\n\t\t\t}`,\n\t\t\texpected: []string{\"outer\", \"inner\", \"key\", \"array\", \"another\"},\n\t\t},\n\t\t{\n\t\t\tname: \"complex object\",\n\t\t\tjson: `{\n\t\t\t\t\"Image\": {\n\t\t\t\t\t\"Width\": 800,\n\t\t\t\t\t\"Height\": 600,\n\t\t\t\t\t\"Title\": \"View from 15th Floor\",\n\t\t\t\t\t\"Thumbnail\": {\n\t\t\t\t\t\t\"Url\": \"http://www.example.com/image/481989943\",\n\t\t\t\t\t\t\"Height\": 125,\n\t\t\t\t\t\t\"Width\": 100\n\t\t\t\t\t},\n\t\t\t\t\t\"Animated\": false,\n\t\t\t\t\t\"IDs\": [116, 943, 234, 38793]\n\t\t\t\t}\n\t\t\t}`,\n\t\t\texpected: []string{\"Image\", \"Width\", \"Height\", \"Title\", \"Thumbnail\", \"Url\", \"Animated\", \"IDs\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\troot, err := Unmarshal([]byte(tt.json))\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"error occurred while unmarshal\")\n\t\t\t}\n\n\t\t\tvalue := root.UniqueKeyLists()\n\t\t\tif len(value) != len(tt.expected) {\n\t\t\t\tt.Errorf(\"%s length must be %v. got: %v. retrieved keys: %s\", tt.name, len(tt.expected), len(value), value)\n\t\t\t}\n\n\t\t\tfor _, key := range value {\n\t\t\t\tif !contains(tt.expected, key) {\n\t\t\t\t\tt.Errorf(\"EachKey() must be in %v. got: %v\", tt.expected, key)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\n// TODO: resolve stack overflow\nfunc TestNode_IsEmpty(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tnode     *Node\n\t\texpected bool\n\t}{\n\t\t{\"nil node\", (*Node)(nil), false}, // nil node is not empty.\n\t\t// {\"null node\", NullNode(\"\"), true},\n\t\t{\"empty object\", ObjectNode(\"\", nil), true},\n\t\t{\"empty array\", ArrayNode(\"\", nil), true},\n\t\t{\"non-empty object\", ObjectNode(\"\", map[string]*Node{\"foo\": BoolNode(\"foo\", true)}), false},\n\t\t{\"non-empty array\", ArrayNode(\"\", []*Node{BoolNode(\"0\", true)}), false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif got := tt.node.Empty(); got != tt.expected {\n\t\t\t\tt.Errorf(\"%s = %v, want %v\", tt.name, got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_Index_EmptyList(t *testing.T) {\n\troot, err := Unmarshal([]byte(`[]`))\n\tif err != nil {\n\t\tt.Errorf(\"error occurred while unmarshal\")\n\t}\n\n\tarray := root.MustArray()\n\tfor i, node := range array {\n\t\tif i != node.Index() {\n\t\t\tt.Errorf(ufmt.Sprintf(\"Index() must be nil. got: %v\", i))\n\t\t}\n\t}\n}\n\nfunc TestNode_GetObject(t *testing.T) {\n\troot, err := Unmarshal([]byte(`{\"foo\": true,\"bar\": null}`))\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err.Error())\n\t\treturn\n\t}\n\n\tvalue, err := root.GetObject()\n\tif err != nil {\n\t\tt.Errorf(\"Error on root.GetObject(): %s\", err.Error())\n\t}\n\n\tif _, ok := value[\"foo\"]; !ok {\n\t\tt.Errorf(\"root.GetObject() is corrupted: foo\")\n\t}\n\n\tif _, ok := value[\"bar\"]; !ok {\n\t\tt.Errorf(\"root.GetObject() is corrupted: bar\")\n\t}\n}\n\nfunc TestNode_GetObject_NotSucceed(t *testing.T) {\n\ttests := []simpleNode{\n\t\t{\"nil node\", (*Node)(nil)},\n\t\t{\"get object from null node\", NullNode(\"\")},\n\t\t{\"not object node\", NumberNode(\"\", 123)},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif _, err := tt.node.GetObject(); err == nil {\n\t\t\t\tt.Errorf(\"%s should be an error\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_ObjectEach(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tjson     string\n\t\texpected map[string]int\n\t}{\n\t\t{\n\t\t\tname:     \"empty object\",\n\t\t\tjson:     `{}`,\n\t\t\texpected: make(map[string]int),\n\t\t},\n\t\t{\n\t\t\tname:     \"single key-value pair\",\n\t\t\tjson:     `{\"key\": 42}`,\n\t\t\texpected: map[string]int{\"key\": 42},\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple key-value pairs\",\n\t\t\tjson:     `{\"one\": 1, \"two\": 2, \"three\": 3}`,\n\t\t\texpected: map[string]int{\"one\": 1, \"two\": 2, \"three\": 3},\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple key-value pairs with some non-numeric values\",\n\t\t\tjson:     `{\"one\": 1, \"two\": \"2\", \"three\": 3, \"four\": \"4\"}`,\n\t\t\texpected: map[string]int{\"one\": 1, \"three\": 3},\n\t\t},\n\t\t{\n\t\t\tname:     \"non-object node\",\n\t\t\tjson:     `[\"not\", \"an\", \"object\"]`,\n\t\t\texpected: make(map[string]int),\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\troot, err := Unmarshal([]byte(tc.json))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatalf(\"Unmarshal failed: %v\", err)\n\t\t\t}\n\n\t\t\tresult := make(map[string]int)\n\t\t\troot.ObjectEach(func(key string, value *Node) {\n\t\t\t\t// extract integer values from the object\n\t\t\t\tif val, err := strconv.Atoi(value.String()); err == nil {\n\t\t\t\t\tresult[key] = val\n\t\t\t\t}\n\t\t\t})\n\n\t\t\tif len(result) != len(tc.expected) {\n\t\t\t\tt.Errorf(\"%s: expected %d key-value pairs, got %d\", tc.name, len(tc.expected), len(result))\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tfor key, val := range tc.expected {\n\t\t\t\tif result[key] != val {\n\t\t\t\t\tt.Errorf(\"%s: expected value for key %s to be %d, got %d\", tc.name, key, val, result[key])\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_ExampleMust(t *testing.T) {\n\tdata := []byte(`{\n        \"Image\": {\n            \"Width\":  800,\n            \"Height\": 600,\n            \"Title\":  \"View from 15th Floor\",\n            \"Thumbnail\": {\n                \"Url\":    \"http://www.example.com/image/481989943\",\n                \"Height\": 125,\n                \"Width\":  100\n            },\n            \"Animated\" : false,\n            \"IDs\": [116, 943, 234, 38793]\n        }\n    }`)\n\n\troot := Must(Unmarshal(data))\n\tif root.Size() != 1 {\n\t\tt.Errorf(\"root.Size() must be 1. got: %v\", root.Size())\n\t}\n\n\tufmt.Sprintf(\"Object has %d inheritors inside\", root.Size())\n\t// Output:\n\t// Object has 1 inheritors inside\n}\n\n// Calculate AVG price from different types of objects, JSON from: https://goessner.net/articles/JsonPath/index.html#e3\nfunc TestExampleUnmarshal(t *testing.T) {\n\tdata := []byte(`{ \"store\": {\n    \"book\": [ \n      { \"category\": \"reference\",\n        \"author\": \"Nigel Rees\",\n        \"title\": \"Sayings of the Century\",\n        \"price\": 8.95\n      },\n      { \"category\": \"fiction\",\n        \"author\": \"Evelyn Waugh\",\n        \"title\": \"Sword of Honour\",\n        \"price\": 12.99\n      },\n      { \"category\": \"fiction\",\n        \"author\": \"Herman Melville\",\n        \"title\": \"Moby Dick\",\n        \"isbn\": \"0-553-21311-3\",\n        \"price\": 8.99\n      },\n      { \"category\": \"fiction\",\n        \"author\": \"J. R. R. Tolkien\",\n        \"title\": \"The Lord of the Rings\",\n        \"isbn\": \"0-395-19395-8\",\n        \"price\": 22.99\n      }\n    ],\n    \"bicycle\": { \"color\": \"red\",\n      \"price\": 19.95\n    },\n    \"tools\": null\n  }\n}`)\n\n\troot, err := Unmarshal(data)\n\tif err != nil {\n\t\tt.Errorf(\"error occurred when unmarshal\")\n\t}\n\n\tstore := root.MustKey(\"store\").MustObject()\n\n\tvar prices float64\n\tsize := 0\n\tfor _, objects := range store {\n\t\tif objects.IsArray() \u0026\u0026 objects.Size() \u003e 0 {\n\t\t\tsize += objects.Size()\n\t\t\tfor _, object := range objects.MustArray() {\n\t\t\t\tprices += object.MustKey(\"price\").MustNumeric()\n\t\t\t}\n\t\t} else if objects.IsObject() \u0026\u0026 objects.HasKey(\"price\") {\n\t\t\tsize++\n\t\t\tprices += objects.MustKey(\"price\").MustNumeric()\n\t\t}\n\t}\n\n\tresult := int(prices / float64(size))\n\tufmt.Sprintf(\"AVG price: %d\", result)\n}\n\nfunc TestNode_ExampleMust_panic(t *testing.T) {\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Errorf(\"The code did not panic\")\n\t\t}\n\t}()\n\tdata := []byte(`{]`)\n\troot := Must(Unmarshal(data))\n\tufmt.Sprintf(\"Object has %d inheritors inside\", root.Size())\n}\n\nfunc TestNode_Path(t *testing.T) {\n\tdata := []byte(`{\n        \"Image\": {\n            \"Width\":  800,\n            \"Height\": 600,\n            \"Title\":  \"View from 15th Floor\",\n            \"Thumbnail\": {\n                \"Url\":    \"http://www.example.com/image/481989943\",\n                \"Height\": 125,\n                \"Width\":  100\n            },\n            \"Animated\" : false,\n            \"IDs\": [116, 943, 234, 38793]\n          }\n      }`)\n\n\troot, err := Unmarshal(data)\n\tif err != nil {\n\t\tt.Errorf(\"Error on Unmarshal(): %s\", err.Error())\n\t\treturn\n\t}\n\n\tif root.Path() != \"$\" {\n\t\tt.Errorf(\"Wrong root.Path()\")\n\t}\n\n\telement := root.MustKey(\"Image\").MustKey(\"Thumbnail\").MustKey(\"Url\")\n\tif element.Path() != \"$['Image']['Thumbnail']['Url']\" {\n\t\tt.Errorf(\"Wrong path found: %s\", element.Path())\n\t}\n\n\tif (*Node)(nil).Path() != \"\" {\n\t\tt.Errorf(\"Wrong (nil).Path()\")\n\t}\n}\n\nfunc TestNode_Path2(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\tnode *Node\n\t\twant string\n\t}{\n\t\t{\n\t\t\tname: \"Node with key\",\n\t\t\tnode: \u0026Node{\n\t\t\t\tprev: \u0026Node{},\n\t\t\t\tkey:  func() *string { s := \"key\"; return \u0026s }(),\n\t\t\t},\n\t\t\twant: \"$['key']\",\n\t\t},\n\t\t{\n\t\t\tname: \"Node with index\",\n\t\t\tnode: \u0026Node{\n\t\t\t\tprev:  \u0026Node{},\n\t\t\t\tindex: func() *int { i := 1; return \u0026i }(),\n\t\t\t},\n\t\t\twant: \"$[1]\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif got := tt.node.Path(); got != tt.want {\n\t\t\t\tt.Errorf(\"Path() = %v, want %v\", got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestNode_Root(t *testing.T) {\n\troot := \u0026Node{}\n\tchild := \u0026Node{prev: root}\n\tgrandChild := \u0026Node{prev: child}\n\n\ttests := []struct {\n\t\tname string\n\t\tnode *Node\n\t\twant *Node\n\t}{\n\t\t{\n\t\t\tname: \"Root node\",\n\t\t\tnode: root,\n\t\t\twant: root,\n\t\t},\n\t\t{\n\t\t\tname: \"Child node\",\n\t\t\tnode: child,\n\t\t\twant: root,\n\t\t},\n\t\t{\n\t\t\tname: \"Grandchild node\",\n\t\t\tnode: grandChild,\n\t\t\twant: root,\n\t\t},\n\t\t{\n\t\t\tname: \"Node is nil\",\n\t\t\tnode: nil,\n\t\t\twant: nil,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif got := tt.node.root(); got != tt.want {\n\t\t\t\tt.Errorf(\"root() = %v, want %v\", got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc contains(slice []string, item string) bool {\n\tfor _, a := range slice {\n\t\tif a == item {\n\t\t\treturn true\n\t\t}\n\t}\n\n\treturn false\n}\n\n// ignore the sequence of keys by ordering them.\n// need to avoid import encoding/json and reflect package.\n// because gno does not support them for now.\n// TODO: use encoding/json to compare the result after if possible in gno.\nfunc isSameObject(a, b string) bool {\n\taPairs := strings.Split(strings.Trim(a, \"{}\"), \",\")\n\tbPairs := strings.Split(strings.Trim(b, \"{}\"), \",\")\n\n\taMap := make(map[string]string)\n\tbMap := make(map[string]string)\n\tfor _, pair := range aPairs {\n\t\tkv := strings.Split(pair, \":\")\n\t\tkey := strings.Trim(kv[0], `\"`)\n\t\tvalue := strings.Trim(kv[1], `\"`)\n\t\taMap[key] = value\n\t}\n\tfor _, pair := range bPairs {\n\t\tkv := strings.Split(pair, \":\")\n\t\tkey := strings.Trim(kv[0], `\"`)\n\t\tvalue := strings.Trim(kv[1], `\"`)\n\t\tbMap[key] = value\n\t}\n\n\taKeys := make([]string, 0, len(aMap))\n\tbKeys := make([]string, 0, len(bMap))\n\tfor k := range aMap {\n\t\taKeys = append(aKeys, k)\n\t}\n\n\tfor k := range bMap {\n\t\tbKeys = append(bKeys, k)\n\t}\n\n\tsort.Strings(aKeys)\n\tsort.Strings(bKeys)\n\n\tif len(aKeys) != len(bKeys) {\n\t\treturn false\n\t}\n\n\tfor i := range aKeys {\n\t\tif aKeys[i] != bKeys[i] {\n\t\t\treturn false\n\t\t}\n\n\t\tif aMap[aKeys[i]] != bMap[bKeys[i]] {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n\nfunc compareNodes(n1, n2 *Node) bool {\n\tif n1 == nil || n2 == nil {\n\t\treturn n1 == n2\n\t}\n\n\tif n1.key != n2.key {\n\t\treturn false\n\t}\n\n\tif !bytes.Equal(n1.data, n2.data) {\n\t\treturn false\n\t}\n\n\tif n1.index != n2.index {\n\t\treturn false\n\t}\n\n\tif n1.borders != n2.borders {\n\t\treturn false\n\t}\n\n\tif n1.modified != n2.modified {\n\t\treturn false\n\t}\n\n\tif n1.nodeType != n2.nodeType {\n\t\treturn false\n\t}\n\n\tif !compareNodes(n1.prev, n2.prev) {\n\t\treturn false\n\t}\n\n\tif len(n1.next) != len(n2.next) {\n\t\treturn false\n\t}\n\n\tfor k, v := range n1.next {\n\t\tif !compareNodes(v, n2.next[k]) {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n"},{"name":"parser.gno","body":"package json\n\nimport (\n\t\"bytes\"\n)\n\nconst (\n\tunescapeStackBufSize = 64\n\tabsMinInt64          = 1 \u003c\u003c 63\n\tmaxInt64             = absMinInt64 - 1\n\tmaxUint64            = 1\u003c\u003c64 - 1\n)\n\n// PaseStringLiteral parses a string from the given byte slice.\nfunc ParseStringLiteral(data []byte) (string, error) {\n\tvar buf [unescapeStackBufSize]byte\n\n\tbf, err := Unescape(data, buf[:])\n\tif err != nil {\n\t\treturn \"\", errInvalidStringInput\n\t}\n\n\treturn string(bf), nil\n}\n\n// ParseBoolLiteral parses a boolean value from the given byte slice.\nfunc ParseBoolLiteral(data []byte) (bool, error) {\n\tswitch {\n\tcase bytes.Equal(data, trueLiteral):\n\t\treturn true, nil\n\tcase bytes.Equal(data, falseLiteral):\n\t\treturn false, nil\n\tdefault:\n\t\treturn false, errMalformedBooleanValue\n\t}\n}\n"},{"name":"parser_test.gno","body":"package json\n\nimport \"testing\"\n\nfunc TestParseStringLiteral(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected string\n\t\tisError  bool\n\t}{\n\t\t{`\"Hello, World!\"`, \"\\\"Hello, World!\\\"\", false},\n\t\t{`\\uFF11`, \"\\uFF11\", false},\n\t\t{`\\uFFFF`, \"\\uFFFF\", false},\n\t\t{`true`, \"true\", false},\n\t\t{`false`, \"false\", false},\n\t\t{`\\uDF00`, \"\", true},\n\t}\n\n\tfor i, tt := range tests {\n\t\ts, err := ParseStringLiteral([]byte(tt.input))\n\n\t\tif !tt.isError \u0026\u0026 err != nil {\n\t\t\tt.Errorf(\"%d. unexpected error: %s\", i, err)\n\t\t}\n\n\t\tif tt.isError \u0026\u0026 err == nil {\n\t\t\tt.Errorf(\"%d. expected error, but not error\", i)\n\t\t}\n\n\t\tif s != tt.expected {\n\t\t\tt.Errorf(\"%d. expected=%s, but actual=%s\", i, tt.expected, s)\n\t\t}\n\t}\n}\n\nfunc TestParseBoolLiteral(t *testing.T) {\n\ttests := []struct {\n\t\tinput    string\n\t\texpected bool\n\t\tisError  bool\n\t}{\n\t\t{`true`, true, false},\n\t\t{`false`, false, false},\n\t\t{`TRUE`, false, true},\n\t\t{`FALSE`, false, true},\n\t\t{`foo`, false, true},\n\t\t{`\"true\"`, false, true},\n\t\t{`\"false\"`, false, true},\n\t}\n\n\tfor i, tt := range tests {\n\t\tb, err := ParseBoolLiteral([]byte(tt.input))\n\n\t\tif !tt.isError \u0026\u0026 err != nil {\n\t\t\tt.Errorf(\"%d. unexpected error: %s\", i, err)\n\t\t}\n\n\t\tif tt.isError \u0026\u0026 err == nil {\n\t\t\tt.Errorf(\"%d. expected error, but not error\", i)\n\t\t}\n\n\t\tif b != tt.expected {\n\t\t\tt.Errorf(\"%d. expected=%t, but actual=%t\", i, tt.expected, b)\n\t\t}\n\t}\n}\n"},{"name":"path.gno","body":"package json\n\nimport (\n\t\"errors\"\n)\n\n// ParsePath takes a JSONPath string and returns a slice of strings representing the path segments.\nfunc ParsePath(path string) ([]string, error) {\n\tbuf := newBuffer([]byte(path))\n\tresult := make([]string, 0)\n\n\tfor {\n\t\tb, err := buf.current()\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\n\t\tswitch {\n\t\tcase b == dollarSign || b == atSign:\n\t\t\tresult = append(result, string(b))\n\t\t\tbuf.step()\n\n\t\tcase b == dot:\n\t\t\tbuf.step()\n\n\t\t\tif next, _ := buf.current(); next == dot {\n\t\t\t\tbuf.step()\n\t\t\t\tresult = append(result, \"..\")\n\n\t\t\t\textractNextSegment(buf, \u0026result)\n\t\t\t} else {\n\t\t\t\textractNextSegment(buf, \u0026result)\n\t\t\t}\n\n\t\tcase b == bracketOpen:\n\t\t\tstart := buf.index\n\t\t\tbuf.step()\n\n\t\t\tfor {\n\t\t\t\tif buf.index \u003e= buf.length || buf.data[buf.index] == bracketClose {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\n\t\t\t\tbuf.step()\n\t\t\t}\n\n\t\t\tif buf.index \u003e= buf.length {\n\t\t\t\treturn nil, errors.New(\"unexpected end of path\")\n\t\t\t}\n\n\t\t\tsegment := string(buf.sliceFromIndices(start+1, buf.index))\n\t\t\tresult = append(result, segment)\n\n\t\t\tbuf.step()\n\n\t\tdefault:\n\t\t\tbuf.step()\n\t\t}\n\t}\n\n\treturn result, nil\n}\n\n// extractNextSegment extracts the segment from the current index\n// to the next significant character and adds it to the resulting slice.\nfunc extractNextSegment(buf *buffer, result *[]string) {\n\tstart := buf.index\n\tbuf.skipToNextSignificantToken()\n\n\tif buf.index \u003c= start {\n\t\treturn\n\t}\n\n\tsegment := string(buf.sliceFromIndices(start, buf.index))\n\tif segment != \"\" {\n\t\t*result = append(*result, segment)\n\t}\n}\n"},{"name":"path_test.gno","body":"package json\n\nimport \"testing\"\n\nfunc TestParseJSONPath(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tpath     string\n\t\texpected []string\n\t}{\n\t\t{name: \"Empty string path\", path: \"\", expected: []string{}},\n\t\t{name: \"Root only path\", path: \"$\", expected: []string{\"$\"}},\n\t\t{name: \"Root with dot path\", path: \"$.\", expected: []string{\"$\"}},\n\t\t{name: \"All objects in path\", path: \"$..\", expected: []string{\"$\", \"..\"}},\n\t\t{name: \"Only children in path\", path: \"$.*\", expected: []string{\"$\", \"*\"}},\n\t\t{name: \"All objects' children in path\", path: \"$..*\", expected: []string{\"$\", \"..\", \"*\"}},\n\t\t{name: \"Simple dot notation path\", path: \"$.root.element\", expected: []string{\"$\", \"root\", \"element\"}},\n\t\t{name: \"Complex dot notation path with wildcard\", path: \"$.root.*.element\", expected: []string{\"$\", \"root\", \"*\", \"element\"}},\n\t\t{name: \"Path with array wildcard\", path: \"$.phoneNumbers[*].type\", expected: []string{\"$\", \"phoneNumbers\", \"*\", \"type\"}},\n\t\t{name: \"Path with filter expression\", path: \"$.store.book[?(@.price \u003c 10)].title\", expected: []string{\"$\", \"store\", \"book\", \"?(@.price \u003c 10)\", \"title\"}},\n\t\t{name: \"Path with formula\", path: \"$..phoneNumbers..('ty' + 'pe')\", expected: []string{\"$\", \"..\", \"phoneNumbers\", \"..\", \"('ty' + 'pe')\"}},\n\t\t{name: \"Simple bracket notation path\", path: \"$['root']['element']\", expected: []string{\"$\", \"'root'\", \"'element'\"}},\n\t\t{name: \"Complex bracket notation path with wildcard\", path: \"$['root'][*]['element']\", expected: []string{\"$\", \"'root'\", \"*\", \"'element'\"}},\n\t\t{name: \"Bracket notation path with integer index\", path: \"$['store']['book'][0]['title']\", expected: []string{\"$\", \"'store'\", \"'book'\", \"0\", \"'title'\"}},\n\t\t{name: \"Complex path with wildcard in bracket notation\", path: \"$['root'].*['element']\", expected: []string{\"$\", \"'root'\", \"*\", \"'element'\"}},\n\t\t{name: \"Mixed notation path with dot after bracket\", path: \"$.['root'].*.['element']\", expected: []string{\"$\", \"'root'\", \"*\", \"'element'\"}},\n\t\t{name: \"Mixed notation path with dot before bracket\", path: \"$['root'].*.['element']\", expected: []string{\"$\", \"'root'\", \"*\", \"'element'\"}},\n\t\t{name: \"Single character path with root\", path: \"$.a\", expected: []string{\"$\", \"a\"}},\n\t\t{name: \"Multiple characters path with root\", path: \"$.abc\", expected: []string{\"$\", \"abc\"}},\n\t\t{name: \"Multiple segments path with root\", path: \"$.a.b.c\", expected: []string{\"$\", \"a\", \"b\", \"c\"}},\n\t\t{name: \"Multiple segments path with wildcard and root\", path: \"$.a.*.c\", expected: []string{\"$\", \"a\", \"*\", \"c\"}},\n\t\t{name: \"Multiple segments path with filter and root\", path: \"$.a[?(@.b == 'c')].d\", expected: []string{\"$\", \"a\", \"?(@.b == 'c')\", \"d\"}},\n\t\t{name: \"Complex path with multiple filters\", path: \"$.a[?(@.b == 'c')].d[?(@.e == 'f')].g\", expected: []string{\"$\", \"a\", \"?(@.b == 'c')\", \"d\", \"?(@.e == 'f')\", \"g\"}},\n\t\t{name: \"Complex path with multiple filters and wildcards\", path: \"$.a[?(@.b == 'c')].*.d[?(@.e == 'f')].g\", expected: []string{\"$\", \"a\", \"?(@.b == 'c')\", \"*\", \"d\", \"?(@.e == 'f')\", \"g\"}},\n\t\t{name: \"Path with array index and root\", path: \"$.a[0].b\", expected: []string{\"$\", \"a\", \"0\", \"b\"}},\n\t\t{name: \"Path with multiple array indices and root\", path: \"$.a[0].b[1].c\", expected: []string{\"$\", \"a\", \"0\", \"b\", \"1\", \"c\"}},\n\t\t{name: \"Path with array index, wildcard and root\", path: \"$.a[0].*.c\", expected: []string{\"$\", \"a\", \"0\", \"*\", \"c\"}},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\treult, _ := ParsePath(tt.path)\n\t\t\tif !isEqualSlice(reult, tt.expected) {\n\t\t\t\tt.Errorf(\"ParsePath(%s) expected: %v, got: %v\", tt.path, tt.expected, reult)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc isEqualSlice(a, b []string) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\n\tfor i, v := range a {\n\t\tif v != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n"},{"name":"token.gno","body":"package json\n\nconst (\n\tbracketOpen    = '['\n\tbracketClose   = ']'\n\tparenOpen      = '('\n\tparenClose     = ')'\n\tcurlyOpen      = '{'\n\tcurlyClose     = '}'\n\tcomma          = ','\n\tdot            = '.'\n\tcolon          = ':'\n\tbackTick       = '`'\n\tsingleQuote    = '\\''\n\tdoubleQuote    = '\"'\n\temptyString    = \"\"\n\twhiteSpace     = ' '\n\tplus           = '+'\n\tminus          = '-'\n\taesterisk      = '*'\n\tbang           = '!'\n\tquestion       = '?'\n\tnewLine        = '\\n'\n\ttab            = '\\t'\n\tcarriageReturn = '\\r'\n\tformFeed       = '\\f'\n\tbackSpace      = '\\b'\n\tslash          = '/'\n\tbackSlash      = '\\\\'\n\tunderScore     = '_'\n\tdollarSign     = '$'\n\tatSign         = '@'\n\tandSign        = '\u0026'\n\torSign         = '|'\n)\n\nvar (\n\ttrueLiteral  = []byte(\"true\")\n\tfalseLiteral = []byte(\"false\")\n\tnullLiteral  = []byte(\"null\")\n)\n\ntype ValueType int\n\nconst (\n\tNotExist ValueType = iota\n\tString\n\tNumber\n\tFloat\n\tObject\n\tArray\n\tBoolean\n\tNull\n\tUnknown\n)\n\nfunc (v ValueType) String() string {\n\tswitch v {\n\tcase NotExist:\n\t\treturn \"not-exist\"\n\tcase String:\n\t\treturn \"string\"\n\tcase Number:\n\t\treturn \"number\"\n\tcase Object:\n\t\treturn \"object\"\n\tcase Array:\n\t\treturn \"array\"\n\tcase Boolean:\n\t\treturn \"boolean\"\n\tcase Null:\n\t\treturn \"null\"\n\tdefault:\n\t\treturn \"unknown\"\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"BDfwEwBf4tI0odx1UG4AbD4U753V3HZ4L7jws9m/3xUaqOPQHRbkk6AUPh9PWQj5jO+aVMdRJA6GbNCuEkREAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"table","path":"gno.land/p/sunspirit/table","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/sunspirit/table\"\ngno = \"0.9\"\n"},{"name":"table.gno","body":"package table\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Table defines the structure for a markdown table\ntype Table struct {\n\theader []string\n\trows   [][]string\n}\n\n// Validate checks if the number of columns in each row matches the number of columns in the header\nfunc (t *Table) Validate() error {\n\tnumCols := len(t.header)\n\tfor _, row := range t.rows {\n\t\tif len(row) != numCols {\n\t\t\treturn ufmt.Errorf(\"row %v does not match header length %d\", row, numCols)\n\t\t}\n\t}\n\treturn nil\n}\n\n// New creates a new Table instance, ensuring the header and rows match in size\nfunc New(header []string, rows [][]string) (*Table, error) {\n\tt := \u0026Table{\n\t\theader: header,\n\t\trows:   rows,\n\t}\n\n\tif err := t.Validate(); err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn t, nil\n}\n\n// Table returns a markdown string for the given Table\nfunc (t *Table) String() string {\n\tif err := t.Validate(); err != nil {\n\t\tpanic(err)\n\t}\n\n\tvar sb strings.Builder\n\n\tsb.WriteString(\"| \" + strings.Join(t.header, \" | \") + \" |\\n\")\n\tsb.WriteString(\"| \" + strings.Repeat(\"---|\", len(t.header)) + \"\\n\")\n\n\tfor _, row := range t.rows {\n\t\tsb.WriteString(\"| \" + strings.Join(row, \" | \") + \" |\\n\")\n\t}\n\n\treturn sb.String()\n}\n\n// AddRow adds a new row to the table\nfunc (t *Table) AddRow(row []string) error {\n\tif len(row) != len(t.header) {\n\t\treturn ufmt.Errorf(\"row %v does not match header length %d\", row, len(t.header))\n\t}\n\tt.rows = append(t.rows, row)\n\treturn nil\n}\n\n// AddColumn adds a new column to the table with the specified values\nfunc (t *Table) AddColumn(header string, values []string) error {\n\tif len(values) != len(t.rows) {\n\t\treturn ufmt.Errorf(\"values length %d does not match the number of rows %d\", len(values), len(t.rows))\n\t}\n\n\t// Add the new header\n\tt.header = append(t.header, header)\n\n\t// Add the new column values to each row\n\tfor i, value := range values {\n\t\tt.rows[i] = append(t.rows[i], value)\n\t}\n\treturn nil\n}\n\n// RemoveRow removes a row from the table by its index\nfunc (t *Table) RemoveRow(index int) error {\n\tif index \u003c 0 || index \u003e= len(t.rows) {\n\t\treturn ufmt.Errorf(\"index %d is out of range\", index)\n\t}\n\tt.rows = append(t.rows[:index], t.rows[index+1:]...)\n\treturn nil\n}\n\n// RemoveColumn removes a column from the table by its index\nfunc (t *Table) RemoveColumn(index int) error {\n\tif index \u003c 0 || index \u003e= len(t.header) {\n\t\treturn ufmt.Errorf(\"index %d is out of range\", index)\n\t}\n\n\t// Remove the column from the header\n\tt.header = append(t.header[:index], t.header[index+1:]...)\n\n\t// Remove the corresponding column from each row\n\tfor i := range t.rows {\n\t\tt.rows[i] = append(t.rows[i][:index], t.rows[i][index+1:]...)\n\t}\n\treturn nil\n}\n"},{"name":"table_test.gno","body":"package table\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestNew(t *testing.T) {\n\theader := []string{\"Name\", \"Age\", \"Country\"}\n\trows := [][]string{\n\t\t{\"Alice\", \"30\", \"USA\"},\n\t\t{\"Bob\", \"25\", \"UK\"},\n\t}\n\n\ttable, err := New(header, rows)\n\turequire.NoError(t, err)\n\n\tuassert.Equal(t, len(header), len(table.header))\n\tuassert.Equal(t, len(rows), len(table.rows))\n}\n\nfunc Test_AddRow(t *testing.T) {\n\theader := []string{\"Name\", \"Age\"}\n\trows := [][]string{\n\t\t{\"Alice\", \"30\"},\n\t\t{\"Bob\", \"25\"},\n\t}\n\n\ttable, err := New(header, rows)\n\turequire.NoError(t, err)\n\n\t// Add a valid row\n\terr = table.AddRow([]string{\"Charlie\", \"28\"})\n\turequire.NoError(t, err)\n\n\texpectedRows := [][]string{\n\t\t{\"Alice\", \"30\"},\n\t\t{\"Bob\", \"25\"},\n\t\t{\"Charlie\", \"28\"},\n\t}\n\tuassert.Equal(t, len(expectedRows), len(table.rows))\n\n\t// Attempt to add a row with a different number of columns\n\terr = table.AddRow([]string{\"David\"})\n\tuassert.Error(t, err)\n}\n\nfunc Test_AddColumn(t *testing.T) {\n\theader := []string{\"Name\", \"Age\"}\n\trows := [][]string{\n\t\t{\"Alice\", \"30\"},\n\t\t{\"Bob\", \"25\"},\n\t}\n\n\ttable, err := New(header, rows)\n\turequire.NoError(t, err)\n\n\t// Add a valid column\n\terr = table.AddColumn(\"Country\", []string{\"USA\", \"UK\"})\n\turequire.NoError(t, err)\n\n\texpectedHeader := []string{\"Name\", \"Age\", \"Country\"}\n\texpectedRows := [][]string{\n\t\t{\"Alice\", \"30\", \"USA\"},\n\t\t{\"Bob\", \"25\", \"UK\"},\n\t}\n\tuassert.Equal(t, len(expectedHeader), len(table.header))\n\tuassert.Equal(t, len(expectedRows), len(table.rows))\n\n\t// Attempt to add a column with a different number of values\n\terr = table.AddColumn(\"City\", []string{\"New York\"})\n\tuassert.Error(t, err)\n}\n\nfunc Test_RemoveRow(t *testing.T) {\n\theader := []string{\"Name\", \"Age\", \"Country\"}\n\trows := [][]string{\n\t\t{\"Alice\", \"30\", \"USA\"},\n\t\t{\"Bob\", \"25\", \"UK\"},\n\t}\n\n\ttable, err := New(header, rows)\n\turequire.NoError(t, err)\n\n\t// Remove the first row\n\terr = table.RemoveRow(0)\n\turequire.NoError(t, err)\n\n\texpectedRows := [][]string{\n\t\t{\"Bob\", \"25\", \"UK\"},\n\t}\n\tuassert.Equal(t, len(expectedRows), len(table.rows))\n\n\t// Attempt to remove a row out of range\n\terr = table.RemoveRow(5)\n\tuassert.Error(t, err)\n}\n\nfunc Test_RemoveColumn(t *testing.T) {\n\theader := []string{\"Name\", \"Age\", \"Country\"}\n\trows := [][]string{\n\t\t{\"Alice\", \"30\", \"USA\"},\n\t\t{\"Bob\", \"25\", \"UK\"},\n\t}\n\n\ttable, err := New(header, rows)\n\turequire.NoError(t, err)\n\n\t// Remove the second column (Age)\n\terr = table.RemoveColumn(1)\n\turequire.NoError(t, err)\n\n\texpectedHeader := []string{\"Name\", \"Country\"}\n\texpectedRows := [][]string{\n\t\t{\"Alice\", \"USA\"},\n\t\t{\"Bob\", \"UK\"},\n\t}\n\tuassert.Equal(t, len(expectedHeader), len(table.header))\n\tuassert.Equal(t, len(expectedRows), len(table.rows))\n\n\t// Attempt to remove a column out of range\n\terr = table.RemoveColumn(5)\n\tuassert.Error(t, err)\n}\n\nfunc Test_Validate(t *testing.T) {\n\theader := []string{\"Name\", \"Age\", \"Country\"}\n\trows := [][]string{\n\t\t{\"Alice\", \"30\", \"USA\"},\n\t\t{\"Bob\", \"25\"},\n\t}\n\n\ttable, err := New(header, rows[:1])\n\turequire.NoError(t, err)\n\n\t// Validate should pass\n\terr = table.Validate()\n\turequire.NoError(t, err)\n\n\t// Add an invalid row and validate again\n\ttable.rows = append(table.rows, rows[1])\n\terr = table.Validate()\n\tuassert.Error(t, err)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"n9TieoRlPYr1n5FkbbdNhvFcNZ+X2OLwJHXpzRw/KJeMQontZzGBLPvMfqerTZ+EhF9i6LqfpSZCto7S0HSTBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"md","path":"gno.land/p/sunspirit/md","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/sunspirit/md\"\ngno = \"0.9\"\n"},{"name":"md.gno","body":"package md\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Builder helps to build a Markdown string from individual elements\ntype Builder struct {\n\telements []string\n}\n\n// NewBuilder creates a new Builder instance\nfunc NewBuilder() *Builder {\n\treturn \u0026Builder{}\n}\n\n// Add adds a Markdown element to the builder\nfunc (m *Builder) Add(md ...string) *Builder {\n\tm.elements = append(m.elements, md...)\n\treturn m\n}\n\n// Render returns the final Markdown string joined with the specified separator\nfunc (m *Builder) Render(separator string) string {\n\treturn strings.Join(m.elements, separator)\n}\n\n// Bold returns bold text for markdown\nfunc Bold(text string) string {\n\treturn ufmt.Sprintf(\"**%s**\", text)\n}\n\n// Italic returns italicized text for markdown\nfunc Italic(text string) string {\n\treturn ufmt.Sprintf(\"*%s*\", text)\n}\n\n// Strikethrough returns strikethrough text for markdown\nfunc Strikethrough(text string) string {\n\treturn ufmt.Sprintf(\"~~%s~~\", text)\n}\n\n// H1 returns a level 1 header for markdown\nfunc H1(text string) string {\n\treturn ufmt.Sprintf(\"# %s\\n\", text)\n}\n\n// H2 returns a level 2 header for markdown\nfunc H2(text string) string {\n\treturn ufmt.Sprintf(\"## %s\\n\", text)\n}\n\n// H3 returns a level 3 header for markdown\nfunc H3(text string) string {\n\treturn ufmt.Sprintf(\"### %s\\n\", text)\n}\n\n// H4 returns a level 4 header for markdown\nfunc H4(text string) string {\n\treturn ufmt.Sprintf(\"#### %s\\n\", text)\n}\n\n// H5 returns a level 5 header for markdown\nfunc H5(text string) string {\n\treturn ufmt.Sprintf(\"##### %s\\n\", text)\n}\n\n// H6 returns a level 6 header for markdown\nfunc H6(text string) string {\n\treturn ufmt.Sprintf(\"###### %s\\n\", text)\n}\n\n// BulletList returns an bullet list for markdown\nfunc BulletList(items []string) string {\n\tvar sb strings.Builder\n\tfor _, item := range items {\n\t\tsb.WriteString(ufmt.Sprintf(\"- %s\\n\", item))\n\t}\n\treturn sb.String()\n}\n\n// OrderedList returns an ordered list for markdown\nfunc OrderedList(items []string) string {\n\tvar sb strings.Builder\n\tfor i, item := range items {\n\t\tsb.WriteString(ufmt.Sprintf(\"%d. %s\\n\", i+1, item))\n\t}\n\treturn sb.String()\n}\n\n// TodoList returns a list of todo items with checkboxes for markdown\nfunc TodoList(items []string, done []bool) string {\n\tvar sb strings.Builder\n\n\tfor i, item := range items {\n\t\tcheckbox := \" \"\n\t\tif done[i] {\n\t\t\tcheckbox = \"x\"\n\t\t}\n\t\tsb.WriteString(ufmt.Sprintf(\"- [%s] %s\\n\", checkbox, item))\n\t}\n\treturn sb.String()\n}\n\n// Blockquote returns a blockquote for markdown\nfunc Blockquote(text string) string {\n\tlines := strings.Split(text, \"\\n\")\n\tvar sb strings.Builder\n\tfor _, line := range lines {\n\t\tsb.WriteString(ufmt.Sprintf(\"\u003e %s\\n\", line))\n\t}\n\n\treturn sb.String()\n}\n\n// InlineCode returns inline code for markdown\nfunc InlineCode(code string) string {\n\treturn ufmt.Sprintf(\"`%s`\", code)\n}\n\n// CodeBlock creates a markdown code block\nfunc CodeBlock(content string) string {\n\treturn ufmt.Sprintf(\"```\\n%s\\n```\", content)\n}\n\n// LanguageCodeBlock creates a markdown code block with language-specific syntax highlighting\nfunc LanguageCodeBlock(language, content string) string {\n\treturn ufmt.Sprintf(\"```%s\\n%s\\n```\", language, content)\n}\n\n// LineBreak returns the specified number of line breaks for markdown\nfunc LineBreak(count uint) string {\n\tif count \u003e 0 {\n\t\treturn strings.Repeat(\"\\n\", int(count)+1)\n\t}\n\treturn \"\"\n}\n\n// HorizontalRule returns a horizontal rule for markdown\nfunc HorizontalRule() string {\n\treturn \"---\\n\"\n}\n\n// Link returns a hyperlink for markdown\nfunc Link(text, url string) string {\n\treturn ufmt.Sprintf(\"[%s](%s)\", text, url)\n}\n\n// Image returns an image for markdown\nfunc Image(altText, url string) string {\n\treturn ufmt.Sprintf(\"![%s](%s)\", altText, url)\n}\n\n// Footnote returns a footnote for markdown\nfunc Footnote(reference, text string) string {\n\treturn ufmt.Sprintf(\"[%s]: %s\", reference, text)\n}\n\n// Paragraph wraps the given text in a Markdown paragraph\nfunc Paragraph(content string) string {\n\treturn ufmt.Sprintf(\"%s\\n\", content)\n}\n\n// MdTable is an interface for table types that can be converted to Markdown format\ntype MdTable interface {\n\tString() string\n}\n\n// Table takes any MdTable implementation and returns its markdown representation\nfunc Table(table MdTable) string {\n\treturn table.String()\n}\n\n// EscapeMarkdown escapes special markdown characters in a string\nfunc EscapeMarkdown(text string) string {\n\treturn ufmt.Sprintf(\"``%s``\", text)\n}\n"},{"name":"md_test.gno","body":"package md\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/sunspirit/table\"\n)\n\nfunc TestNewBuilder(t *testing.T) {\n\tmdBuilder := NewBuilder()\n\n\tuassert.Equal(t, len(mdBuilder.elements), 0, \"Expected 0 elements\")\n}\n\nfunc TestAdd(t *testing.T) {\n\tmdBuilder := NewBuilder()\n\n\theader := H1(\"Hi\")\n\tbody := Paragraph(\"This is a test\")\n\n\tmdBuilder.Add(header, body)\n\n\tuassert.Equal(t, len(mdBuilder.elements), 2, \"Expected 2 element\")\n\tuassert.Equal(t, mdBuilder.elements[0], header, \"Expected element %s, got %s\", header, mdBuilder.elements[0])\n\tuassert.Equal(t, mdBuilder.elements[1], body, \"Expected element %s, got %s\", body, mdBuilder.elements[1])\n}\n\nfunc TestRender(t *testing.T) {\n\tmdBuilder := NewBuilder()\n\n\theader := H1(\"Hello\")\n\tbody := Paragraph(\"This is a test\")\n\n\tseperator := \"\\n\"\n\texpected := header + seperator + body\n\n\toutput := mdBuilder.Add(header, body).Render(seperator)\n\n\tuassert.Equal(t, output, expected, \"Expected rendered string %s, got %s\", expected, output)\n}\n\nfunc Test_Bold(t *testing.T) {\n\tuassert.Equal(t, Bold(\"Hello\"), \"**Hello**\")\n}\n\nfunc Test_Italic(t *testing.T) {\n\tuassert.Equal(t, Italic(\"Hello\"), \"*Hello*\")\n}\n\nfunc Test_Strikethrough(t *testing.T) {\n\tuassert.Equal(t, Strikethrough(\"Hello\"), \"~~Hello~~\")\n}\n\nfunc Test_H1(t *testing.T) {\n\tuassert.Equal(t, H1(\"Header 1\"), \"# Header 1\\n\")\n}\n\nfunc Test_H2(t *testing.T) {\n\tuassert.Equal(t, H2(\"Header 2\"), \"## Header 2\\n\")\n}\n\nfunc Test_H3(t *testing.T) {\n\tuassert.Equal(t, H3(\"Header 3\"), \"### Header 3\\n\")\n}\n\nfunc Test_H4(t *testing.T) {\n\tuassert.Equal(t, H4(\"Header 4\"), \"#### Header 4\\n\")\n}\n\nfunc Test_H5(t *testing.T) {\n\tuassert.Equal(t, H5(\"Header 5\"), \"##### Header 5\\n\")\n}\n\nfunc Test_H6(t *testing.T) {\n\tuassert.Equal(t, H6(\"Header 6\"), \"###### Header 6\\n\")\n}\n\nfunc Test_BulletList(t *testing.T) {\n\titems := []string{\"Item 1\", \"Item 2\", \"Item 3\"}\n\tresult := BulletList(items)\n\texpected := \"- Item 1\\n- Item 2\\n- Item 3\\n\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_OrderedList(t *testing.T) {\n\titems := []string{\"Item 1\", \"Item 2\", \"Item 3\"}\n\tresult := OrderedList(items)\n\texpected := \"1. Item 1\\n2. Item 2\\n3. Item 3\\n\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_TodoList(t *testing.T) {\n\titems := []string{\"Task 1\", \"Task 2\"}\n\tdone := []bool{true, false}\n\tresult := TodoList(items, done)\n\texpected := \"- [x] Task 1\\n- [ ] Task 2\\n\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_Blockquote(t *testing.T) {\n\ttext := \"This is a blockquote.\\nIt has multiple lines.\"\n\tresult := Blockquote(text)\n\texpected := \"\u003e This is a blockquote.\\n\u003e It has multiple lines.\\n\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_InlineCode(t *testing.T) {\n\tresult := InlineCode(\"code\")\n\tuassert.Equal(t, result, \"`code`\")\n}\n\nfunc Test_LanguageCodeBlock(t *testing.T) {\n\tresult := LanguageCodeBlock(\"python\", \"print('Hello')\")\n\texpected := \"```python\\nprint('Hello')\\n```\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_CodeBlock(t *testing.T) {\n\tresult := CodeBlock(\"print('Hello')\")\n\texpected := \"```\\nprint('Hello')\\n```\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_LineBreak(t *testing.T) {\n\tresult := LineBreak(2)\n\texpected := \"\\n\\n\\n\"\n\tuassert.Equal(t, result, expected)\n\n\tresult = LineBreak(0)\n\texpected = \"\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_HorizontalRule(t *testing.T) {\n\tresult := HorizontalRule()\n\tuassert.Equal(t, result, \"---\\n\")\n}\n\nfunc Test_Link(t *testing.T) {\n\tresult := Link(\"Google\", \"http://google.com\")\n\tuassert.Equal(t, result, \"[Google](http://google.com)\")\n}\n\nfunc Test_Image(t *testing.T) {\n\tresult := Image(\"Alt text\", \"http://image.url\")\n\tuassert.Equal(t, result, \"![Alt text](http://image.url)\")\n}\n\nfunc Test_Footnote(t *testing.T) {\n\tresult := Footnote(\"1\", \"This is a footnote.\")\n\tuassert.Equal(t, result, \"[1]: This is a footnote.\")\n}\n\nfunc Test_Paragraph(t *testing.T) {\n\tresult := Paragraph(\"This is a paragraph.\")\n\tuassert.Equal(t, result, \"This is a paragraph.\\n\")\n}\n\nfunc Test_Table(t *testing.T) {\n\ttb, err := table.New([]string{\"Header1\", \"Header2\"}, [][]string{\n\t\t{\"Row1Col1\", \"Row1Col2\"},\n\t\t{\"Row2Col1\", \"Row2Col2\"},\n\t})\n\tuassert.NoError(t, err)\n\n\tresult := Table(tb)\n\texpected := \"| Header1 | Header2 |\\n| ---|---|\\n| Row1Col1 | Row1Col2 |\\n| Row2Col1 | Row2Col2 |\\n\"\n\tuassert.Equal(t, result, expected)\n}\n\nfunc Test_EscapeMarkdown(t *testing.T) {\n\tresult := EscapeMarkdown(\"- This is `code`\")\n\tuassert.Equal(t, result, \"``- This is `code```\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"E3wx9wKh+HIZwN16VOe+4W5HOqKCuxntuGsgLz5xPOY3SoIyA6R3eHJ7fa3apU8Njb7ulK1hjKhef5aD2H/wBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"piechart","path":"gno.land/p/samcrew/piechart","files":[{"name":"README.md","body":"# `piechart` - SVG pie charts \n\nGenerate pie charts with legends as SVG markup for gnoweb rendering.\n\n## Usage\n\n```go\nslices := []piechart.PieSlice{\n    {Value: 30, Color: \"#ff6b6b\", Label: \"Frontend\"},\n    {Value: 25, Color: \"#4ecdc4\", Label: \"Backend\"},\n    {Value: 20, Color: \"#45b7d1\", Label: \"DevOps\"},\n    {Value: 15, Color: \"#96ceb4\", Label: \"Mobile\"},\n    {Value: 10, Color: \"#ffeaa7\", Label: \"Other\"},\n}\n\n// With title\ntitledChart := piechart.Render(slices, \"Team Distribution\")\n\n// Without title  \nuntitledChart := piechart.Render(slices, \"\")\n```\n\n## API Reference\n\n```go\ntype PieSlice struct {\n    Value float64 // Numeric value for the slice\n    Color string  // Hex color code (e.g., \"#ff6b6b\")\n    Label string  // Display label for the slice\n}\n\n// slices: Array of PieSlice structs containing the data\n// title: Chart title (empty string for no title)\n// Returns: SVG markup as a string\nfunc Render(slices []PieSlice, title string) string\n```\n\n## Live Example\n\n- [/r/docs/charts:piechart](/r/docs/charts:piechart)\n- [/r/samcrew/daodemo/custom_condition:members](/r/samcrew/daodemo/custom_condition:members)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/piechart\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"},{"name":"piechart.gno","body":"// Package piechart provides functionality to render a pie chart as an SVG image.\n// It takes a list of PieSlice objects, each representing a slice of the pie with a value,\n// color, and label, and generates an SVG representation of the pie chart.\npackage piechart\n\nimport (\n\t\"math\"\n\n\t\"gno.land/p/demo/svg\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sunspirit/md\"\n)\n\ntype PieSlice struct {\n\tValue float64\n\tColor string\n\tLabel string\n}\n\n// Render creates an SVG pie chart from given slices (value, color and label).\n// It returns an img svg markup as a string, including a markdown header if a non-empty title is provided.\nfunc Render(slices []PieSlice, title string) string {\n\t// Validate input slices length\n\tif len(slices) == 0 {\n\t\treturn \"\\npiechart fails: no data provided\"\n\t}\n\n\tconst (\n\t\tcanvasWidth  = 500\n\t\tcanvasHeight = 200\n\t\tcenterX      = 100.0\n\t\tcenterY      = 100.0\n\t\tradius       = 80.0\n\t\tlegendX      = 210\n\t\tlegendStartY = 30\n\t\tlineHeight   = 26\n\t\tsquareSize   = 16\n\t\tfontSize     = 16\n\t)\n\n\tcanvas := svg.NewCanvas(canvasWidth, canvasHeight)\n\n\t// Sum all values to compute slices proportions\n\tvar total float64\n\tfor _, s := range slices {\n\t\ttotal += s.Value\n\t}\n\n\t// Draw pie slices and legend in one pass\n\tstartAngle := -math.Pi / 2\n\tfor i, s := range slices {\n\t\tif s.Value \u003e 0 {\n\t\t\t// --- PIE SLICE ---\n\t\t\t// Calculate angle span for current slice\n\t\t\tangle := (s.Value / total) * 2 * math.Pi\n\t\t\tendAngle := startAngle + angle\n\n\t\t\t// Compute start and end points on the circle circumference\n\t\t\tcosStart, sinStart := math.Cos(startAngle), math.Sin(startAngle)\n\t\t\tcosEnd, sinEnd := math.Cos(endAngle), math.Sin(endAngle)\n\t\t\tx1 := centerX + radius*cosStart\n\t\t\ty1 := centerY + radius*sinStart\n\t\t\tx2 := centerX + radius*cosEnd\n\t\t\ty2 := centerY + radius*sinEnd\n\n\t\t\t// Determine if the arc should be a large arc (\u003e 180 degrees) (Arc direction)\n\t\t\tlargeArcFlag := 0\n\t\t\tif angle \u003e math.Pi {\n\t\t\t\tlargeArcFlag = 1\n\t\t\t}\n\n\t\t\t// Build the SVG path for the pie slice\n\t\t\tpath := ufmt.Sprintf(\n\t\t\t\t\"M%.2f,%.2f L%.2f,%.2f A%.2f,%.2f 0 %d 1 %.2f,%.2f Z\",\n\t\t\t\tcenterX, centerY, x1, y1, radius, radius, largeArcFlag, x2, y2,\n\t\t\t)\n\n\t\t\t// Colored slice\n\t\t\tcanvas.Append(svg.Path{\n\t\t\t\tD:    path,\n\t\t\t\tFill: s.Color,\n\t\t\t})\n\n\t\t\tstartAngle = endAngle\n\t\t}\n\n\t\t// --- LEGEND ---\n\t\ty := legendStartY + i*lineHeight\n\t\t// Colored square representing slice color\n\t\tcanvas.Append(svg.Rectangle{\n\t\t\tX:      legendX,\n\t\t\tY:      y - squareSize/2,\n\t\t\tWidth:  squareSize,\n\t\t\tHeight: squareSize,\n\t\t\tFill:   s.Color,\n\t\t})\n\n\t\t// Legend text showing label, value and percentage\n\t\ttext := ufmt.Sprintf(\"%s: %.0f (%.1f%%)\", s.Label, s.Value, s.Value*100/total)\n\t\tcanvas.Append(svg.Text{\n\t\t\tX:    legendX + squareSize + 8,\n\t\t\tY:    y + fontSize/3,\n\t\t\tText: text,\n\t\t\tFill: \"#54595D\",\n\t\t\tAttr: svg.BaseAttrs{\n\t\t\t\tStyle: ufmt.Sprintf(\"font-family:'Inter var',sans-serif;font-size:%dpx;\", fontSize),\n\t\t\t},\n\t\t})\n\t}\n\n\tif title == \"\" {\n\t\treturn canvas.Render(\"Pie Chart\")\n\t}\n\treturn md.H2(title) + canvas.Render(\"Pie Chart \"+title)\n}\n"},{"name":"piechart_test.gno","body":"package piechart\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestRender(t *testing.T) {\n\ttitle := \"Test\"\n\tslices := []PieSlice{\n\t\t{Value: 10, Color: \"#00FF00\", Label: \"Green\"},\n\t\t{Value: 20, Color: \"#FFFF00\", Label: \"Yellow\"},\n\t\t{Value: 30, Color: \"#FF0000\", Label: \"Red\"},\n\t}\n\n\tresult := Render(slices, title)\n\n\t// Check if the result contains the expected image SVG structure\n\tif !strings.Contains(result, \"data:image/svg+xml;base64,\") {\n\t\tt.Errorf(\"Expected result to contain data:image/svg+xml;base64, got %s\", result)\n\t}\n\n\t// Check if the result contains the title\n\tif !strings.Contains(result, title) {\n\t\tt.Errorf(\"SVG does not contain the title %q\", title)\n\t}\n\n\t// Check if the result contains non-empty slices\n\temptySlices := []PieSlice{}\n\temptyResult := Render(emptySlices, title)\n\texpectedErr := \"\\npiechart fails: no data provided\"\n\tif emptyResult != expectedErr {\n\t\tt.Errorf(\"Expected exact error for empty slices: %q, got %q\", expectedErr, emptyResult)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Gl1IGeqaMnXZfabNvpdj0XHhu9V3g/OtEsLwDDbk+L+nOB4xqG0W3+ORjpPAXHfzaqUoE71JFSQw6R/2/RPTAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"tablesort","path":"gno.land/p/samcrew/tablesort","files":[{"name":"README.md","body":"# `tablesort` - Sortable markdown tables\n\nGenerate sortable markdown tables with clickable column headers. Sorting state is managed via URL query parameters.\n\n## Usage\n\n```go\nimport \"gno.land/p/samcrew/tablesort\"\n\ntable := \u0026tablesort.Table{\n    Headings: []string{\"Name\", \"Age\", \"City\"},\n    Rows: [][]string{\n        {\"Alice\", \"25\", \"New York\"},\n        {\"Bob\", \"30\", \"London\"},\n        {\"Charlie\", \"22\", \"Paris\"},\n    },\n}\n\n// Basic usage\nu, _ := url.Parse(\"/users\")\nmarkdown := tablesort.Render(u, table, \"\")\n\n// Multiple tables on same page (use prefix to avoid conflicts)\nmarkdown1 := tablesort.Render(u, table, \"table1-\")\nmarkdown2 := tablesort.Render(u, table, \"table2-\")\n```\n\n## On-chain Example\n\n- [/r/gov/dao/v3/memberstore:members?filter=T1](/r/gov/dao/v3/memberstore:members?filter=T1)\n\n## API\n\n```go\ntype Table struct {\n    Headings []string   // Column headers\n    Rows     [][]string // Table data rows\n}\n\n// `u`: Current URL for generating sort links\n// `table`: Table data structure\n// `paramPrefix`: Prefix for URL params (use for multiple tables)\nfunc Render(u *url.URL, table *Table, paramPrefix string) string\n```\n\n**URL Parameters:**\n- `{prefix}sort-asc={column}`: Sort column ascending\n- `{prefix}sort-desc={column}`: Sort column descending\n\n**URL Examples:**\n- `/users?sort-desc=Name` - Sort by Name descending\n- `/page?users-sort-asc=Age\u0026orders-sort-desc=Total` - Multiple tables\n\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/tablesort\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"},{"name":"render.gno","body":"package tablesort\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\n\t\"gno.land/p/mason/md\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Table holds the headings and rows for rendering.\n// Each row must have the same number of cells as there are headings.\ntype Table struct {\n\tHeadings []string   // [\"A\", \"B\", \"C\"]\n\tRows     [][]string // [[\"a1\",\"b1\",\"c1\"], [\"a2\",\"b2\",\"c2\"], ...]\n}\n\n// Render generates a Markdown table from a Table struct with sortable columns based on URL params.\n// paramPrefix is an optional prefix for in the URL to identify the tablesort Renders (e.g. \"members-\").\nfunc Render(u *url.URL, table *Table, paramPrefix string) string {\n\tdirection := \"\"\n\tcurrentHeading := \"\"\n\tif h := u.Query().Get(paramPrefix + \"sort-asc\"); h != \"\" {\n\t\tdirection = \"asc\"\n\t\tcurrentHeading = h\n\t} else if h := u.Query().Get(paramPrefix + \"sort-desc\"); h != \"\" {\n\t\tdirection = \"desc\"\n\t\tcurrentHeading = h\n\t}\n\n\tvar sb strings.Builder\n\n\t// Find the index of the column to sort\n\tcolIndex := -1\n\tfor i, h := range table.Headings {\n\t\tif h == currentHeading {\n\t\t\tcolIndex = i\n\t\t\tbreak\n\t\t}\n\t}\n\n\t// Sort rows if necessary\n\tif colIndex != -1 {\n\t\tSortRows(table.Rows, colIndex, direction == \"asc\")\n\t}\n\n\t// Build header\n\tsb.WriteString(buildHeader(u, table.Headings, currentHeading, direction, paramPrefix))\n\tsb.WriteString(\"\\n\")\n\n\tnumCols := len(table.Headings)\n\n\t// Build rows\n\tfor i, row := range table.Rows {\n\t\t// Validate row length\n\t\tif len(row) != numCols {\n\t\t\treturn \"tablesort fails: row \" + ufmt.Sprintf(\"%d\", i+1) + \" has \" +\n\t\t\t\tufmt.Sprintf(\"%d\", len(row)) + \" cells, expected \" +\n\t\t\t\tufmt.Sprintf(\"%d\", numCols) + \", because there are \" + ufmt.Sprintf(\"%d\", numCols) + \" columns.\\n\"\n\t\t}\n\n\t\tsb.WriteString(\"|\")\n\t\tfor _, cell := range row {\n\t\t\tsb.WriteString(\" \" + cell + \" |\")\n\t\t}\n\t\tsb.WriteString(\"\\n\")\n\t}\n\n\treturn sb.String()\n}\n\n// buildHeader builds the Markdown header row with clickable links and arrows\nfunc buildHeader(u *url.URL, headings []string, currentHeading, direction string, paramPrefix string) string {\n\tvar sb strings.Builder\n\tsb.WriteString(\"|\")\n\tfor _, h := range headings {\n\t\tarrow := \"\"\n\t\tif h == currentHeading {\n\t\t\tif direction == \"asc\" {\n\t\t\t\tarrow = \" ↑\"\n\t\t\t} else if direction == \"desc\" {\n\t\t\t\tarrow = \" ↓\"\n\t\t\t}\n\t\t}\n\n\t\t// Build URL for the header link with toggle logic\n\t\tnewURL := *u\n\t\tq := newURL.Query()\n\t\tif h == currentHeading {\n\t\t\t// Toggle sort direction\n\t\t\tif direction == \"asc\" {\n\t\t\t\tq.Del(paramPrefix + \"sort-asc\")\n\t\t\t\tq.Set(paramPrefix+\"sort-desc\", h)\n\t\t\t} else {\n\t\t\t\tq.Del(paramPrefix + \"sort-desc\")\n\t\t\t\tq.Set(paramPrefix+\"sort-asc\", h)\n\t\t\t}\n\t\t} else {\n\t\t\t// First click defaults to descending\n\t\t\tq.Del(paramPrefix + \"sort-asc\")\n\t\t\tq.Set(paramPrefix+\"sort-desc\", h)\n\t\t}\n\t\tnewURL.RawQuery = q.Encode()\n\t\tlink := md.Link(h+arrow, newURL.String())\n\t\tsb.WriteString(\" \" + link + \" |\")\n\t}\n\n\tsb.WriteString(\"\\n|\")\n\tfor range headings {\n\t\tsb.WriteString(\" --- |\")\n\t}\n\treturn sb.String()\n}\n"},{"name":"tablesort.gno","body":"// Package tablesort provides functionality to render a Markdown table with sortable columns.\n// It allows users to click on column headers to sort the table in ascending or descending sort direction.\n// The sorting state is managed via URL query parameters.\n// It displays an error if the table is malformed (e.g. rows with missing cells).\n// Multiple tablesort can be rendered on the same page by using a paramPrefix for each Render (See the Render function).\npackage tablesort\n\nimport (\n\t\"sort\"\n)\n\n// rowSorter implements sort.Interface for sorting rows by a specific column.\ntype rowSorter struct {\n\trows      [][]string\n\tcolIndex  int\n\tascending bool\n}\n\nfunc (rs rowSorter) Len() int {\n\treturn len(rs.rows)\n}\n\nfunc (rs rowSorter) Less(i, j int) bool {\n\tiCell := rs.rows[i][rs.colIndex]\n\tjCell := rs.rows[j][rs.colIndex]\n\tif rs.ascending {\n\t\treturn iCell \u003c jCell\n\t}\n\treturn iCell \u003e jCell\n}\n\nfunc (rs rowSorter) Swap(i, j int) {\n\trs.rows[i], rs.rows[j] = rs.rows[j], rs.rows[i]\n}\n\n// SortRows sorts the rows slice by a given column index and direction\nfunc SortRows(rows [][]string, colIndex int, ascending bool) {\n\tsort.Sort(rowSorter{rows, colIndex, ascending})\n}\n"},{"name":"tablesort_test.gno","body":"package tablesort\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestRender(t *testing.T) {\n\t// --- Case 1: Testing Render with a Table with \"Role\" column sorted descending and param prefix \"members-\"\n\ttable := \u0026Table{\n\t\tHeadings: []string{\"Tier\", \"Member\", \"Role\"},\n\t\tRows: [][]string{\n\t\t\t{\"T1\", \"g11111\", \"finance-officer\"},\n\t\t\t{\"T2\", \"g22222\", \"developer\"},\n\t\t\t{\"T3\", \"g33333\", \"developer\"},\n\t\t},\n\t}\n\n\tu, _ := url.Parse(\"/test?members-sort-desc=Tier\")\n\tmd := Render(u, table, \"members-\")\n\n\texpected := \"| [Tier ↓](/test?members-sort-asc=Tier) | [Member](/test?members-sort-desc=Member) | [Role](/test?members-sort-desc=Role) |\\n\" +\n\t\t\"| --- | --- | --- |\\n\" +\n\t\t\"| T3 | g33333 | developer |\\n\" +\n\t\t\"| T2 | g22222 | developer |\\n\" +\n\t\t\"| T1 | g11111 | finance-officer |\\n\"\n\n\t// Trim spaces for comparison\n\tmd = strings.TrimSpace(md)\n\texpected = strings.TrimSpace(expected)\n\n\tif md != expected {\n\t\tt.Errorf(\"Render() output mismatch.\\nExpected:\\n%s\\nGot:\\n%s\", expected, md)\n\t}\n\n\t// --- Case 2: Testing Render with an invalid Table (row with missing cell)\n\ttable = \u0026Table{\n\t\tHeadings: []string{\"Tier\", \"Member\", \"Role\"},\n\t\tRows: [][]string{\n\t\t\t{\"T1\", \"g11111\"}, // Missing the \"Role\" cell\n\t\t},\n\t}\n\n\tmd = Render(u, table, \"\")\n\texpected = \"tablesort fails: row 1 has 2 cells, expected 3, because there are 3 columns.\\n\"\n\n\tif md != expected {\n\t\tt.Errorf(\"Expected error message:\\n%s\\nGot:\\n%s\", expected, md)\n\t}\n\n\t// --- Case 3: Testing SortRows\n\trows := [][]string{\n\t\t{\"T1\", \"g11111\", \"finance-officer\"},\n\t\t{\"T2\", \"g22222\", \"developer\"},\n\t\t{\"T3\", \"g33333\", \"developer\"},\n\t}\n\n\tSortRows(rows, 2, false) // Sort by \"Role\" descending\n\texpected = \"finance-officer\"\n\n\tif rows[0][2] != expected {\n\t\tt.Errorf(\"SortRows() failed. Expected first role: %s, got: %s\", expected, rows[0][2])\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"LXU8eEKAF+jUpSkdyzC843BUZKtBMIQFf9NK8K+0Sg6LSGICGGKqAT1fKB24ifCF6z4yJ+WqXgC6HJ4d2HmwBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"urlfilter","path":"gno.land/p/samcrew/urlfilter","files":[{"name":"README.md","body":"# `urlfilter` - URL-based filtering\n\nFilter items using URL query parameters with toggleable markdown links. Works with AVL tree structures where each filter contains its associated items.\n\nGiven filters `[\"T1\", \"T2\", \"size:XL\"]` and URL `/shop?filter=T1,size:XL`, it generates toggle links:\n\n- **T1** _(active, click to remove)_\n- ~~T2~~ _(inactive, click to add)_  \n- **size:XL** _(active, click to remove)_\n\n**Markdown output:**\n```markdown\n[**T1**](/p/samcrew/urlfilter?filter=size:XL) - [~~T2~~](/p/samcrew/urlfilter=T1,T2,size:XL) - [**size:XL**](/p/samcrew/urlfilter?filter=T1)\n```\n\n**Rendered as:**\n[**T1**](/p/samcrew/urlfilter?filter=size:XL) - [~~T2~~](/p/samcrew/urlfilter=T1,T2,size:XL) - [**size:XL**](/p/samcrew/urlfilter?filter=T1)\n\n## Usage\n\nThe package expects a two-level AVL tree structure:\n- **Top level**: Filter names as keys (e.g., \"T1\", \"size:XL\", \"on_sale\")  \n- **Second level**: Item trees containing the actual items for each filter\n\n```go\n// Build the main filters tree\nfilters := avl.NewTree()\n\n// Subtree for filter \"T1\" \nt1Items := avl.NewTree()\nt1Items.Set(\"key1\", \"item1\")\nt1Items.Set(\"key2\", \"item2\")\nfilters.Set(\"T1\", t1Items)\n\n// Subtree for filter \"size:XL\"\nt2Items := avl.NewTree()\nt2Items.Set(\"key3\", \"item3\")\nfilters.Set(\"T2\", t2Items)\n\n// URL with active filter \"T1\"\nu, _ := url.Parse(\"/shop?filter=T1\")\n\n// Apply filtering\nmdLinks, filteredItems := urlfilter.ApplyFilters(u, filters, \"filter\") // \"filter\" for /shop?*filter*=T1\n\n// mdLinks    → Markdown links for toggling filters  \n// filteredItems → AVL tree containing only filtered items\n```\n\n## API\n\n```go\nfunc ApplyFilters(u *url.URL, items *avl.Tree, paramName string) (string, *avl.Tree)\n```\n\n**Parameters:**\n- `u`: URL containing query parameters\n- `items`: Two-level AVL tree (filters → item trees)\n- `paramName`: Query parameter name (e.g., \"filter\" for /shop?filter=T1)\n\n**URL Format:**\n- Single filter: `?filter=T1`\n- Multiple filters: `?filter=T1,size:XL,on_sale`\n- Filter names are comma-separated\n\n**Returns:**\n- **Markdown links**: Toggleable filter links with formatting\n- **Filtered items**: AVL tree containing items from active filters\n  - If no filters active: returns all items\n  - Item keys are preserved, values show which filter matched\n\n# Example\n\n- [/r/gov/dao/v3/memberstore:members?filter=T1](/r/gov/dao/v3/memberstore:members?filter=T1)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/urlfilter\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"},{"name":"urlfilter.gno","body":"// Package urlfilter provides functionality to filter items based on URL query parameters.\n// It is designed to work with an avl.Tree structure where each key represents a filter\n// and each value is an avl.Tree containing items associated with that filter.\npackage urlfilter\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sunspirit/md\"\n)\n\n// ApplyFilters filters items based on the \"filter\" query parameter in the given URL\n// and generates a Markdown representation of all available filters.\n//\n// Expected `items` structure:\n//   - `items` is an *bptree.BPTree where each key is a filter name (e.g., \"T1\", \"size:XL\", \"on_sale\")\n//     and each value is an *bptree.BPTree containing the items for that filter.\n//   - Each item tree uses:\n//     Key   (string): Unique item identifier\n//     Value (any)   : Optional associated item data\n//\n// Example:\n//\n//\t// Build the main filters tree\n//\tfilters := bptree.NewBPTree32()\n//\n//\t// Subtree for filter \"T1\"\n//\tt1Items := bptree.NewBPTree32()\n//\tt1Items.Set(\"item1\", nil)\n//\tt1Items.Set(\"item2\", nil)\n//\tfilters.Set(\"T1\", t1Items)\n//\n//\t// URL with active filter \"T1\"\n//\tu, _ := url.Parse(\"/shop?filter=T1\")\n//\n//\tmdFilters, items := ApplyFilters(u, filters, \"filter\")\n//\n//\t// mdFilters\t→ Markdown links for toggling filters\n//\t// items    \t→ AVL tree containing the filtered items\nfunc ApplyFilters(u *url.URL, items *bptree.BPTree, paramName string) (string, *bptree.BPTree) {\n\tactive := parseFilterMap(u.Query(), paramName)\n\tallFilters := make([]string, 0)\n\tresultTree := bptree.NewBPTree32()\n\n\t// Iterate over each filter group in the items tree\n\titems.Iterate(\"\", \"\", func(filterKey string, subtree interface{}) bool {\n\t\tallFilters = append(allFilters, filterKey)\n\n\t\ttree, ok := subtree.(*bptree.BPTree)\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\n\t\t// Add items to result if there are no active filters\n\t\t// or if the current filter is active\n\t\ttree.Iterate(\"\", \"\", func(itemKey string, _ interface{}) bool {\n\t\t\tif len(active) == 0 || active[filterKey] {\n\t\t\t\tresultTree.Set(itemKey, filterKey)\n\t\t\t}\n\t\t\treturn false\n\t\t})\n\t\treturn false\n\t})\n\n\t// Build Markdown links for toggling each filter\n\tvar sb strings.Builder\n\tfor _, f := range allFilters {\n\t\tq := toggleFilterQuery(active, f, allFilters, paramName)\n\t\turlStr := buildURL(u.Path, q)\n\t\tsb.WriteString(ufmt.Sprintf(\" | %v \", md.Link(formatLabel(f, active[f]), urlStr)))\n\t}\n\n\treturn sb.String(), resultTree\n}\n\n// buildURL returns a path + query string, omitting the \"?\" if no query exists.\nfunc buildURL(path string, query url.Values) string {\n\tif enc := query.Encode(); enc != \"\" {\n\t\treturn path + \"?\" + enc\n\t}\n\treturn path\n}\n\n// parseFilterMap reads the \"filter\" query parameter and converts it into a map\n// where keys are filter names and values are true for active filters.\n//\n// Example:\n//\n//\t\"filter=T1,T2\" -\u003e map[string]bool{\"T1\": true, \"T2\": true}\nfunc parseFilterMap(query url.Values, paramName string) map[string]bool {\n\tfilterStr := strings.TrimSpace(query.Get(paramName))\n\tif filterStr == \"\" {\n\t\treturn map[string]bool{}\n\t}\n\tm := make(map[string]bool)\n\tfor _, f := range strings.Split(filterStr, \",\") {\n\t\tif f = strings.TrimSpace(f); f != \"\" {\n\t\t\tm[f] = true\n\t\t}\n\t}\n\treturn m\n}\n\n// toggleFilterQuery returns a new query string with the given filter toggled.\n// - If the filter is currently active, it will be removed.\n// - If it is inactive, it will be added.\n// The order of filters follows the `all` list for consistency.\nfunc toggleFilterQuery(active map[string]bool, toggled string, all []string, paramName string) url.Values {\n\tnewFilters := []string{}\n\tfor _, f := range all {\n\t\tif f == toggled {\n\t\t\tif !active[f] { // Add if it was inactive\n\t\t\t\tnewFilters = append(newFilters, f)\n\t\t\t}\n\t\t} else if active[f] { // Keep other active filters\n\t\t\tnewFilters = append(newFilters, f)\n\t\t}\n\t}\n\tq := url.Values{}\n\tif len(newFilters) \u003e 0 {\n\t\tq.Set(paramName, strings.Join(newFilters, \",\"))\n\t}\n\treturn q\n}\n\n// formatLabel returns the Markdown-formatted label for a filter,\n// showing active filters in bold (**filter**) and inactive filters\n// with strikethrough (~~filter~~).\nfunc formatLabel(name string, active bool) string {\n\tif active {\n\t\treturn md.Bold(name)\n\t}\n\treturn md.Strikethrough(name)\n}\n"},{"name":"urlfilter_test.gno","body":"package urlfilter\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\nfunc buildTestTree() *bptree.BPTree {\n\troot := bptree.NewBPTree32()\n\n\t// keyF1 subtree\n\tt1 := bptree.NewBPTree32()\n\tt1.Set(\"key1_1\", nil)\n\tt1.Set(\"key1_2\", nil)\n\troot.Set(\"keyF1\", t1)\n\n\t// keyF2 subtree\n\tt2 := bptree.NewBPTree32()\n\tt2.Set(\"key2_1\", nil)\n\tt2.Set(\"key2_2\", nil)\n\troot.Set(\"keyF2\", t2)\n\n\treturn root\n}\n\nfunc TestApplyFilters(t *testing.T) {\n\tTreeParent := buildTestTree()\n\n\t// --- Case 1: No filter selected\n\tu, _ := url.Parse(\"/test\")\n\tmdFilters, items := ApplyFilters(u, TreeParent, \"filter\")\n\texpectedMarkdown := \" | [~~keyF1~~](/test?filter=keyF1)  | [~~keyF2~~](/test?filter=keyF2) \"\n\tif mdFilters != expectedMarkdown {\n\t\tt.Errorf(\"Expected Markdown %q, got %q\", expectedMarkdown, mdFilters)\n\t}\n\t// All items should be present\n\tcount := 0\n\titems.Iterate(\"\", \"\", func(k string, _ interface{}) bool {\n\t\tcount++\n\t\treturn false\n\t})\n\tif count != 4 {\n\t\tt.Errorf(\"Expected 4 items, got %d\", count)\n\t}\n\n\t// Try using ApplyFilters with a different name.\n\twithCustom, _ := ApplyFilters(u, TreeParent, \"custom-param-name\")\n\tif !strings.Contains(withCustom, \"custom-param-name=keyF1\") {\n\t\tt.Errorf(\"Expected 'custom-param-name' parameter in Markdown, got %q\", mdFilters)\n\t}\n\n\t// --- Case 2: One filter active (keyF1)\n\tu, _ = url.Parse(\"/test?filter=keyF1\")\n\tmdFilters, items = ApplyFilters(u, TreeParent, \"filter\")\n\texpectedMarkdown = \" | [**keyF1**](/test)  | [~~keyF2~~](/test?filter=keyF1%2CkeyF2) \"\n\tif mdFilters != expectedMarkdown {\n\t\tt.Errorf(\"Expected Markdown %q, got %q\", expectedMarkdown, mdFilters)\n\t}\n\t// Only keyF1 items should be present\n\tkeys := map[string]bool{}\n\titems.Iterate(\"\", \"\", func(k string, _ interface{}) bool {\n\t\tkeys[k] = true\n\t\treturn false\n\t})\n\tif len(keys) != 2 || !keys[\"key1_1\"] || !keys[\"key1_2\"] {\n\t\tt.Errorf(\"Unexpected items in filtered result: %#v\", keys)\n\t}\n\n\t// --- Case 3: Multiple filters active (keyF1, keyF2)\n\tu, _ = url.Parse(\"/test?filter=keyF1,keyF2\")\n\tmdFilters, items = ApplyFilters(u, TreeParent, \"filter\")\n\t// Both filters should be bold, no remove query for last one\n\tif items.Size() != 4 {\n\t\tt.Errorf(\"Expected 4 items, got %d\", items.Size())\n\t}\n\n\t// --- Case 4: Filter not existing\n\tu, _ = url.Parse(\"/test?filter=unknown\")\n\tmdFilters, items = ApplyFilters(u, TreeParent, \"filter\")\n\t// No matching items\n\tif items.Size() != 0 {\n\t\tt.Errorf(\"Expected 0 items for unknown filter, got %d\", items.Size())\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"sPEl52udHoQFaII6zehXxxTtosF4nkNq1dpZbhCK4X1XQLYY38KbV70Gc2t17U9jpvC2P2WZK7ai3FVdoJN+AQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da","package":{"name":"dao","path":"gno.land/r/gov/dao","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gov/dao\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da\"\n"},{"name":"proxy.gno","body":"package dao\n\nimport (\n\t\"chain\"\n\t\"errors\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// dao is the actual govDAO implementation, having all the needed business logic\nvar dao DAO\n\n// allowedDAOs contains realms that can be used to update the actual govDAO implementation,\n// and validate Proposals.\n// This is like that to be able to rollback using a previous govDAO implementation in case\n// the latest implementation has a breaking bug. After a test period, a proposal can be\n// executed to remove all previous govDAOs implementations and leave the last one.\nvar allowedDAOs []string\n\n// proposals contains all the proposals in history.\nvar proposals *Proposals = NewProposals()\n\n// Render calls directly to Render's DAO implementation.\n// This allows to have this realm as the main entry point for everything.\nfunc Render(cur realm, p string) string {\n\tif dao == nil {\n\t\treturn \"DAO not initialized\"\n\t}\n\treturn dao.Render(cross(cur), cur.PkgPath(), p)\n}\n\n// MustCreateProposal is an utility method that does the same as CreateProposal,\n// but instead of erroing if something happens, it panics.\nfunc MustCreateProposal(cur realm, r ProposalRequest) ProposalID {\n\tpid, err := CreateProposal(cur, r)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\n\treturn pid\n}\n\n// ExecuteProposal will try to execute the proposal with the provided ProposalID.\n// If the proposal was denied, it will return false. If the proposal is correctly\n// executed, it will return true. If something happens this function will panic.\nfunc ExecuteProposal(cur realm, pid ProposalID) bool {\n\treturn executeProposal(cur, pid, false)\n}\n\n// ExecuteOrRejectProposal executes the proposal with the provided ProposalID or rejects\n// it when there is an execution error.\n// If the proposal was denied, it will return false. If the proposal is correctly\n// executed, it will return true, unless execution fails with an error, in which case\n// proposal is rejected with the error as the reason.\n// This function allows to finish proposals by rejecting them when there is a state\n// change or an error in the proposal parameters that makes execution fail, potentially\n// leaving the proposal active forever because it can't be successfully executed.\nfunc ExecuteOrRejectProposal(cur realm, pid ProposalID) bool {\n\treturn executeProposal(cur, pid, true)\n}\n\n// CreateProposal will try to create a new proposal, that will be validated by the actual\n// govDAO implementation. If the proposal cannot be created, an error will be returned.\nfunc CreateProposal(cur realm, r ProposalRequest) (ProposalID, error) {\n\tif dao == nil {\n\t\treturn -1, errors.New(\"DAO not initialized\")\n\t}\n\tauthor, err := dao.PreCreateProposal(0, cur, r)\n\tif err != nil {\n\t\treturn -1, err\n\t}\n\n\tp := \u0026Proposal{\n\t\tauthor:      author,\n\t\ttitle:       r.title,\n\t\tdescription: r.description,\n\t\texecutor:    r.executor,\n\t\tallowedDAOs: allowedDAOs[:],\n\t}\n\n\tpid := proposals.SetProposal(p)\n\tdao.PostCreateProposal(0, cur, r, pid)\n\n\tchain.Emit(\"ProposalCreated\",\n\t\t\"id\", strconv.FormatInt(int64(pid), 10),\n\t)\n\n\treturn pid, nil\n}\n\nfunc MustVoteOnProposal(cur realm, r VoteRequest) {\n\tif err := VoteOnProposal(cur, r); err != nil {\n\t\tpanic(err.Error())\n\t}\n}\n\n// VoteOnProposal sends a vote to the actual govDAO implementation.\n// If the voter cannot vote the specified proposal, this method will return an error\n// with the explanation of why.\nfunc VoteOnProposal(cur realm, r VoteRequest) error {\n\tif dao == nil {\n\t\treturn errors.New(\"DAO not initialized\")\n\t}\n\treturn dao.VoteOnProposal(0, cur, r)\n}\n\n// MustVoteOnProposalSimple is like MustVoteOnProposal but intended to be used through gnokey with basic types.\nfunc MustVoteOnProposalSimple(cur realm, pid int64, option string) {\n\tMustVoteOnProposal(cur, VoteRequest{\n\t\tOption:     VoteOption(option),\n\t\tProposalID: ProposalID(pid),\n\t})\n}\n\nfunc MustGetProposal(pid ProposalID) *Proposal {\n\tp, err := GetProposal(pid)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\n\treturn p\n}\n\n// GetProposal gets created proposal by its ID. Non-crossing pure read:\n// looks up the proposal in this realm's package var. Callable directly\n// from any realm without cross-call syntax.\nfunc GetProposal(pid ProposalID) (*Proposal, error) {\n\tif dao == nil {\n\t\treturn nil, errors.New(\"DAO not initialized\")\n\t}\n\tprop := proposals.GetProposal(pid)\n\tif prop == nil {\n\t\treturn nil, errors.New(ufmt.Sprintf(\"Proposal %v does not exist.\", int64(pid)))\n\t}\n\treturn prop, nil\n}\n\n// UpdateImpl is a method intended to be used on a proposal.\n// This method will update the current govDAO implementation\n// to a new one. AllowedDAOs are a list of realms that can\n// call this method, in case the new DAO implementation had\n// a breaking bug. Any value set as nil will be ignored.\n// If AllowedDAOs field is not set correctly, the actual DAO\n// implementation wont be able to execute new Proposals!\nfunc UpdateImpl(cur realm, r UpdateRequest) {\n\tgRealm := cur.Previous().PkgPath()\n\n\tif !InAllowedDAOs(gRealm) {\n\t\tpanic(\"permission denied for prev realm: \" + gRealm)\n\t}\n\n\tif r.AllowedDAOs != nil {\n\t\tallowedDAOs = r.AllowedDAOs\n\t}\n\n\tif r.DAO != nil {\n\t\tdao = r.DAO\n\t}\n}\n\nfunc AllowedDAOs() []string {\n\tdup := make([]string, len(allowedDAOs))\n\tcopy(dup, allowedDAOs)\n\treturn dup\n}\n\nfunc InAllowedDAOs(pkg string) bool {\n\tif len(allowedDAOs) == 0 {\n\t\treturn true // corner case for initialization\n\t}\n\tfor _, d := range allowedDAOs {\n\t\tif pkg == d {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc executeProposal(cur realm, pid ProposalID, execErrorRejects bool) bool {\n\tif dao == nil {\n\t\treturn false\n\t}\n\texecute, err := dao.PreExecuteProposal(0, cur, pid)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\n\tif !execute {\n\t\treturn false\n\t}\n\tprop, err := GetProposal(pid)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\n\terr = dao.ExecuteProposal(0, cur, pid, prop.executor)\n\tif err != nil {\n\t\tif execErrorRejects {\n\t\t\treturn false\n\t\t}\n\n\t\tpanic(err.Error())\n\t}\n\treturn true\n}\n"},{"name":"proxy_test.gno","body":"package dao\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nconst (\n\tv3 = \"gno.land/r/gov/dao/v3/impl\"\n\tv4 = \"gno.land/r/gov/dao/v4/impl\"\n\tv5 = \"gno.land/r/gov/dao/v5/impl\"\n\tv6 = \"gno.land/r/gov/dao/v6/impl\"\n)\n\nconst invalid = \"gno.land/r/invalid/dao\"\n\nvar alice = testutils.TestAddress(\"alice\")\n\nfunc TestProxy_Functions(cur realm, t *testing.T) {\n\t// initialize tests\n\tUpdateImpl(cross(cur), UpdateRequest{\n\t\tDAO:         \u0026dummyDao{},\n\t\tAllowedDAOs: []string{v3},\n\t})\n\n\t// invalid package cannot add a new dao in charge\n\ttesting.SetRealm(testing.NewCodeRealm(invalid))\n\turequire.AbortsWithMessage(t, cur, \"permission denied for prev realm: gno.land/r/invalid/dao\", func() {\n\t\tUpdateImpl(cross(cur), UpdateRequest{\n\t\t\tDAO: \u0026dummyDao{},\n\t\t})\n\t})\n\n\t// dao in charge can add a new dao\n\ttesting.SetRealm(testing.NewCodeRealm(v3))\n\turequire.NotPanics(t, cur, func() {\n\t\tUpdateImpl(cross(cur), UpdateRequest{\n\t\t\tDAO: \u0026dummyDao{},\n\t\t})\n\t})\n\n\t// v3 that is in charge adds v5 in charge\n\ttesting.SetRealm(testing.NewCodeRealm(v3))\n\turequire.NotPanics(t, cur, func() {\n\t\tUpdateImpl(cross(cur), UpdateRequest{\n\t\t\tDAO:         \u0026dummyDao{},\n\t\t\tAllowedDAOs: []string{v3, v5},\n\t\t})\n\t})\n\n\t// v3 can still do updates\n\ttesting.SetRealm(testing.NewCodeRealm(v3))\n\turequire.NotPanics(t, cur, func() {\n\t\tUpdateImpl(cross(cur), UpdateRequest{\n\t\t\tAllowedDAOs: []string{v4},\n\t\t})\n\t})\n\n\t// not after removing himself from allowedDAOs list\n\ttesting.SetRealm(testing.NewCodeRealm(v3))\n\turequire.AbortsWithMessage(t, cur, \"permission denied for prev realm: gno.land/r/gov/dao/v3/impl\", func() {\n\t\tUpdateImpl(cross(cur), UpdateRequest{\n\t\t\tAllowedDAOs: []string{v3},\n\t\t})\n\t})\n\n\tvar pid ProposalID\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\turequire.NotPanics(t, cur, func() {\n\t\te := NewSimpleExecutor(0, cur,\n\t\t\tfunc(realm) error {\n\t\t\t\treturn nil\n\t\t\t},\n\t\t\t\"\",\n\t\t)\n\t\tpid = MustCreateProposal(cross(cur), NewProposalRequest(\"Proposal Title\", \"Description\", e))\n\t})\n\n\tp, err := GetProposal(1000)\n\tif p != nil || err == nil {\n\t\tpanic(\"proposal should not exist and should return an error\")\n\t}\n\tp = MustGetProposal(pid)\n\turequire.Equal(t, \"Proposal Title\", p.Title())\n\turequire.Equal(t, p.Author().String(), alice.String())\n\n\t// need to switch the context back to v4\n\ttesting.SetRealm(testing.NewCodeRealm(v4))\n\turequire.Equal(\n\t\tt,\n\t\t\"Render: gno.land/r/gov/dao/test\",\n\t\tRender(cross(cur), \"test\"),\n\t)\n\n\t// reset state\n\ttesting.SetRealm(testing.NewCodeRealm(v4))\n\tUpdateImpl(cross(cur), UpdateRequest{\n\t\tDAO:         \u0026dummyDao{},\n\t\tAllowedDAOs: []string{},\n\t})\n}\n\ntype dummyDao struct{}\n\nfunc (dd *dummyDao) PreCreateProposal(_ int, rlm realm, r ProposalRequest) (address, error) {\n\treturn unsafe.OriginCaller(), nil\n}\n\nfunc (dd *dummyDao) PostCreateProposal(_ int, rlm realm, r ProposalRequest, pid ProposalID) {\n}\n\nfunc (dd *dummyDao) VoteOnProposal(_ int, rlm realm, r VoteRequest) error {\n\treturn nil\n}\n\nfunc (dd *dummyDao) PreExecuteProposal(_ int, rlm realm, pid ProposalID) (bool, error) {\n\treturn true, nil\n}\n\nfunc (dd *dummyDao) ExecuteProposal(_ int, rlm realm, pid ProposalID, e Executor) error {\n\treturn nil\n}\n\nfunc (dd *dummyDao) Render(cur realm, pkgpath string, path string) string {\n\treturn \"Render: \" + pkgpath + \"/\" + path\n}\n"},{"name":"types.gno","body":"package dao\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\ntype ProposalID int64\n\nfunc (pid ProposalID) String() string {\n\treturn seqid.ID(pid).String()\n}\n\n// VoteOption is the limited voting option for a DAO proposal\n// New govDAOs can create their own VoteOptions if needed in the\n// future.\ntype VoteOption string\n\nconst (\n\tAbstainVote VoteOption = \"ABSTAIN\" // Side is not chosen\n\tYesVote     VoteOption = \"YES\"     // Proposal should be accepted\n\tNoVote      VoteOption = \"NO\"      // Proposal should be rejected\n)\n\ntype VoteRequest struct {\n\tOption     VoteOption\n\tProposalID ProposalID\n\tMetadata   interface{}\n}\n\nfunc NewVoteRequest(option VoteOption, proposalID ProposalID) VoteRequest {\n\treturn VoteRequest{\n\t\tOption:     option,\n\t\tProposalID: proposalID,\n\t}\n}\n\nfunc NewVoteRequestWithMetadata(option VoteOption, proposalID ProposalID, metadata interface{}) VoteRequest {\n\treturn VoteRequest{\n\t\tOption:     option,\n\t\tProposalID: proposalID,\n\t\tMetadata:   metadata,\n\t}\n}\n\nfunc NewProposalRequest(title string, description string, executor Executor) ProposalRequest {\n\treturn ProposalRequest{\n\t\ttitle:       title,\n\t\tdescription: description,\n\t\texecutor:    executor,\n\t}\n}\n\nfunc NewProposalRequestWithFilter(title string, description string, executor Executor, filter Filter) ProposalRequest {\n\treturn ProposalRequest{\n\t\ttitle:       title,\n\t\tdescription: description,\n\t\texecutor:    executor,\n\t\tfilter:      filter,\n\t}\n}\n\ntype Filter interface{}\n\ntype ProposalRequest struct {\n\ttitle       string\n\tdescription string\n\texecutor    Executor\n\tfilter      Filter\n}\n\nfunc (p *ProposalRequest) Title() string {\n\treturn p.title\n}\n\nfunc (p *ProposalRequest) Description() string {\n\treturn p.description\n}\n\nfunc (p *ProposalRequest) Filter() Filter {\n\treturn p.filter\n}\n\ntype Proposal struct {\n\tauthor address\n\n\ttitle       string\n\tdescription string\n\n\texecutor    Executor\n\tallowedDAOs []string\n}\n\nfunc (p *Proposal) Author() address {\n\treturn p.author\n}\n\nfunc (p *Proposal) Title() string {\n\treturn p.title\n}\n\nfunc (p *Proposal) Description() string {\n\treturn p.description\n}\n\nfunc (p *Proposal) ExecutorString() string {\n\tif p.executor != nil {\n\t\treturn p.executor.String()\n\t}\n\n\treturn \"\"\n}\n\nfunc (p *Proposal) ExecutorCreationRealm() string {\n\tif p.executor != nil {\n\t\treturn p.executor.CreationRealm()\n\t}\n\n\treturn \"\"\n}\n\nfunc (p *Proposal) AllowedDAOs() []string {\n\treturn append([]string(nil), p.allowedDAOs...)\n}\n\ntype Proposals struct {\n\tseq            seqid.ID\n\t*bptree.BPTree // *bptree.BPTree[ProposalID]*Proposal\n}\n\nfunc NewProposals() *Proposals {\n\treturn \u0026Proposals{BPTree: bptree.NewBPTree32()}\n}\n\nfunc (ps *Proposals) SetProposal(p *Proposal) ProposalID {\n\tpid := ProposalID(int64(ps.seq))\n\tupdated := ps.Set(pid.String(), p)\n\tif updated {\n\t\tpanic(\"fatal error: Override proposals is not allowed\")\n\t}\n\tps.seq = ps.seq.Next()\n\treturn pid\n}\n\nfunc (ps *Proposals) GetProposal(pid ProposalID) *Proposal {\n\tpv := ps.Get(pid.String())\n\tif pv == nil {\n\t\treturn nil\n\t}\n\n\treturn pv.(*Proposal)\n}\n\ntype Executor interface {\n\tExecute(cur realm) error\n\tString() string\n\tCreationRealm() string\n}\n\n// NewSimpleExecutor constructs an Executor whose creationRealm is captured\n// from rlm.PkgPath() at construction time. The IsCurrent() check rejects\n// stale or stashed realm values so the captured value is the authentic\n// caller realm. creationRealm is display-only (rendered as \"Executor\n// created in: ...\" in proposal listings) — no auth gate downstream.\nfunc NewSimpleExecutor(_ int, rlm realm, callback func(realm) error, description string) *SimpleExecutor {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"NewSimpleExecutor: rlm is not the caller's live cur (stale capture or sibling frame)\")\n\t}\n\tif callback == nil {\n\t\tpanic(\"executor callback must not be nil\")\n\t}\n\n\treturn \u0026SimpleExecutor{\n\t\tcallback:      callback,\n\t\tdesc:          description,\n\t\tcreationRealm: rlm.PkgPath(),\n\t}\n}\n\n// SimpleExecutor implements the Executor interface using\n// a callback function and a description string.\ntype SimpleExecutor struct {\n\tcallback      func(realm) error\n\tdesc          string\n\tcreationRealm string\n}\n\nfunc (e *SimpleExecutor) Execute(cur realm) error {\n\t// Check if executor was created using the constructor func\n\tif e.callback == nil {\n\t\treturn nil\n\t}\n\n\treturn e.callback(cross(cur))\n}\n\nfunc (e *SimpleExecutor) String() string {\n\treturn e.desc\n}\n\nfunc (e *SimpleExecutor) CreationRealm() string {\n\treturn e.creationRealm\n}\n\nfunc NewSafeExecutor(e Executor) *SafeExecutor {\n\treturn \u0026SafeExecutor{\n\t\te: e,\n\t}\n}\n\n// SafeExecutor wraps an Executor to only allow its execution\n// by allowed govDAOs.\ntype SafeExecutor struct {\n\te Executor\n}\n\nfunc (e *SafeExecutor) Execute(cur realm) error {\n\t// Verify the caller is an adequate Realm\n\tif !InAllowedDAOs(cur.Previous().PkgPath()) {\n\t\treturn errors.New(\"execution only allowed by validated govDAOs\")\n\t}\n\n\treturn e.e.Execute(cross(cur))\n}\n\nfunc (e *SafeExecutor) String() string {\n\treturn e.e.String()\n}\n\nfunc (e *SafeExecutor) CreationRealm() string {\n\treturn e.e.CreationRealm()\n}\n\n// DAO is the govDAO implementation interface. All mutating/auth-gated\n// methods take rlm as their realm-typed parameter in the second position\n// (the `_ int, rlm realm` non-crossing form): callers thread the proxy's\n// cur as data without forcing a realm transition, so the impl's existing\n// unsafe.CurrentRealm()-based auth gates (isValidCall, memberstore.Get)\n// continue to see the proxy realm. Render stays unchanged.\ntype DAO interface {\n\t// PreCreateProposal is called just before creating a new Proposal\n\t// It is intended to be used to get the address of the proposal, that\n\t// may vary depending on the DAO implementation, and to validate that\n\t// the requester is allowed to do a proposal\n\tPreCreateProposal(_ int, rlm realm, r ProposalRequest) (address, error)\n\n\t// PostCreateProposal is called after creating the Proposal. It is\n\t// intended to be used as a way to store a new proposal status, that\n\t// depends on the actuall govDAO implementation\n\tPostCreateProposal(_ int, rlm realm, r ProposalRequest, pid ProposalID)\n\n\t// VoteOnProposal will send a petition to vote for a specific proposal\n\t// to the actual govDAO implementation\n\tVoteOnProposal(_ int, rlm realm, r VoteRequest) error\n\n\t// PreExecuteProposal is called when someone is trying to execute a proposal by ID.\n\t// Is intended to be used to validate who can trigger the proposal execution.\n\tPreExecuteProposal(_ int, rlm realm, pid ProposalID) (bool, error)\n\n\t// ExecuteProposal executes the proposal executor and on error changes proposal\n\t// status to denied with the error message being the denial reason.\n\t// It returns the executor error when it fails.\n\tExecuteProposal(_ int, rlm realm, pid ProposalID, e Executor) error\n\n\t// Render will return a human-readable string in markdown format that\n\t// will be used to show new data through the dao proxy entrypoint.\n\t// Crossing: the chain query layer auto-injects .cur, and\n\t// implementations forward cur to internal rlm-aware helpers (mux\n\t// RenderRlm + downstream cross(rlm) reads).\n\tRender(cur realm, pkgpath string, path string) string\n}\n\ntype UpdateRequest struct {\n\tDAO         DAO\n\tAllowedDAOs []string\n}\n\n// NewUpdateRequest copies allowedDAOs into a fresh slice owned by\n// /r/gov/dao. Under the storage=authority model, if we stored the\n// caller-passed slice directly, the base ArrayValue would retain\n// PkgID = caller_realm: storage rent would attribute to caller, and\n// /r/gov/dao could not mutate (e.g. append to) its own copy without\n// a DidUpdate panic. The internal copy ensures the UpdateRequest\n// and its AllowedDAOs both live entirely in /r/gov/dao's authority.\nfunc NewUpdateRequest(d DAO, allowedDAOs []string) UpdateRequest {\n\tcp := make([]string, len(allowedDAOs))\n\tcopy(cp, allowedDAOs)\n\treturn UpdateRequest{\n\t\tDAO:         d,\n\t\tAllowedDAOs: cp,\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"6o72I4BFyiKemQCK1beFMpoDXyFTirX2qyidvwq/9GNDJrMu2UskwRdi9inVgIyeSlwdldQGCGMnCZzjlf8dAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"users","path":"gno.land/r/sys/users","files":[{"name":"README.md","body":"# `r/sys/users`\n\nThe system realm that owns the (name → address, address → user) registry for\ngno.land. It is intentionally minimal: it stores `UserData` records, exposes\nresolve/update/delete primitives, and gates writes through a controller\nwhitelist managed by GovDAO (`ProposeNewController` /\n`ProposeControllerRemoval` / `ProposeControllerAdditionAndRemoval`).\n\nThis realm does **not** define what a \"name\" is, what registration costs, or\nwhether names can be transferred. Those policies live in *controller realms*\nthat the DAO whitelists. See `r/sys/namereg/v1` for one such controller, and\nthe `examples/gno.land/r/sys/names` realm for the related namespace verifier\nthat gates package deployment under `gno.land/r/\u003cnamespace\u003e/...`.\n\n## Trust boundary at genesis (height 0)\n\nThe whitelist check in `RegisterUser` (and the sibling\n`AddControllerAtGenesis`) **short-circuits at chain height 0**:\n\n```go\n// store.gno\nif runtime.ChainHeight() \u003e 0 \u0026\u0026 !controllers.Has(runtime.PreviousRealm().Address()) {\n    return NewErrNotWhitelisted()\n}\n```\n\nThis is **intentional**, not a bug. Genesis is the bootstrap window where:\n\n1. The controller whitelist is empty (it can't be populated until *after* it\n   exists).\n2. System realms (`r/sys/users/init`, `r/sys/namereg/v1`, etc.) need to\n   pre-seed users and add themselves as controllers.\n3. Any realm whose `init()` runs at genesis can therefore call `RegisterUser`\n   without authorization.\n\nThe protection model is **out-of-band trust**: chain operators control which\nrealms ship in genesis (via the contents of `examples/gno.land/r/...`), and\nthose realms are vouched for at chain-binary build time. The realm code does\nnot — and intentionally does not try to — enforce who is \"allowed\" to\npre-register at height 0.\n\n### Audit reference\n\nThis bypass was flagged as audit finding #4 (\"Genesis bypass — any caller can\nregister at height 0\"). After review, it is treated as **WON'T FIX, working\nas intended**:\n\n- Removing the bypass breaks every legitimate genesis pre-registration use\n  case (including this realm's own bootstrap and `r/sys/namereg/v1`'s\n  preregister loop of system names).\n- A hardcoded genesis-allowlist (a la \"only `r/sys/*` realms may bypass\")\n  shifts the trust to a literal in source — a chain upgrade is required to\n  add a new genesis-bootstrap realm. This trades flexibility for the same\n  amount of trust.\n- Path-prefix gating (e.g. \"only `gno.land/r/sys/*`\") couples this realm to\n  the namespace verifier remaining locked-down, an implicit dependency that\n  makes future refactors fragile.\n\nIf chain operators want post-deployment auditing of who pre-registered what\nat genesis, the `RegisterUserEvent` is emitted on every successful\nregistration regardless of height, and the source of each registration can be\nrecovered by walking genesis-block events alongside the `examples/` tree.\n\n### Sibling bypass: `AddControllerAtGenesis`\n\nThe same height-0 trust model applies to `AddControllerAtGenesis` in\n`admin.gno`:\n\n```go\nfunc AddControllerAtGenesis(_ realm, addr address) {\n    height := runtime.ChainHeight()\n    if height \u003e 0 {\n        panic(\"AddControllerAtGenesis can only be called at genesis (height 0)\")\n    }\n    if !addr.IsValid() {\n        panic(ErrInvalidAddress)\n    }\n    controllers.Add(addr)\n}\n```\n\nThis was audit finding #7 (\"AddControllerAtGenesis has no caller check\"). It\nis the **same intentional design** as #4 and is likewise treated as **WON'T\nFIX**:\n\n- Any realm whose `init()` runs at genesis can whitelist any address as a\n  controller, without authorization.\n- This is how the registry bootstraps itself: `r/sys/users/init.Bootstrap`\n  adds its own package address, and `r/sys/namereg/v1/init.gno` likewise\n  auto-whitelists `gno.land/r/sys/namereg/v1`. Removing the bypass would\n  break the bootstrap pattern.\n- After genesis (height \u003e 0) the function hard-panics, so the privilege\n  window is strictly one-time at chain birth.\n- The trust model is identical: chain operators vouch for whatever realms\n  ship in `examples/` at chain-binary build time.\n\nIf you need to add a new controller post-genesis, the supported path is a\nGovDAO proposal via `ProposeNewController` — the same channel that rotates\nevery controller going forward.\n\n### What the audit DID flag that's worth fixing\n\n- #5: `ufmt.Sprint` used instead of `Sprintf` in controller-swap proposal\n  description (governance-vote readability).\n- #6: Add+Remove proposal silently no-ops if `add` fails on an already-listed\n  controller — voted-on swap doesn't actually swap.\n- #7: `AddControllerAtGenesis` shares the height-0 bypass; same trust model\n  applies, same intentional design.\n\nSee `NAMEREG_AUDIT.md` for the full set and `NAMEREG_TODO.md` for tracked\n\"won't fix / accepted risk\" items.\n"},{"name":"admin.gno","body":"package users\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/gov/dao\"\n)\n\nconst initControllerPath = \"gno.land/r/sys/users/init\"\n\nvar controllers = addrset.Set{} // caller whitelist\n\nfunc init() {\n\t// auto-whitelist the init controller for bootstrapping for testing chain.\n\tif chainID := runtime.ChainID(); chainID == \"dev\" {\n\t\tcontrollers.Add(chain.PackageAddress(initControllerPath))\n\t}\n}\n\n// AddControllerAtGenesis allows adding a controller during chain genesis (height 0).\n// This is mostly useful for testing.\nfunc AddControllerAtGenesis(_ realm, addr address) {\n\theight := runtime.ChainHeight()\n\tif height \u003e 0 {\n\t\tpanic(\"AddControllerAtGenesis can only be called at genesis (height 0)\")\n\t}\n\n\tif !addr.IsValid() {\n\t\tpanic(ErrInvalidAddress)\n\t}\n\n\tcontrollers.Add(addr)\n}\n\n// ProposeNewController allows GovDAO to add a whitelisted caller\nfunc ProposeNewController(cur realm, addr address) dao.ProposalRequest {\n\tif !addr.IsValid() {\n\t\tpanic(ErrInvalidAddress)\n\t}\n\n\tcb := func(cur realm) error {\n\t\treturn addToWhitelist(addr)\n\t}\n\n\tdesc := \"This proposal adds \" + addr.String() + \" to `sys/users` realm's callers whitelist.\"\n\treturn dao.NewProposalRequest(\"Add Whitelisted Caller to \\\"sys/users\\\" Realm\", desc, dao.NewSimpleExecutor(0, cur, cb, \"\"))\n}\n\n// ProposeControllerRemoval allows GovDAO to add a whitelisted caller\nfunc ProposeControllerRemoval(cur realm, addr address) dao.ProposalRequest {\n\tif !addr.IsValid() {\n\t\tpanic(ErrInvalidAddress)\n\t}\n\n\tcb := func(cur realm) error {\n\t\treturn deleteFromWhitelist(addr)\n\t}\n\n\tdesc := \"This proposal removes \" + addr.String() + \" from `sys/users` realm's callers whitelist.\"\n\treturn dao.NewProposalRequest(\"Remove Whitelisted Caller From \\\"sys/users\\\" Realm\", desc, dao.NewSimpleExecutor(0, cur, cb, \"\"))\n}\n\n// ProposeControllerAdditionAndRemoval allows GovDAO to add a new caller and remove an old caller in the same proposal.\nfunc ProposeControllerAdditionAndRemoval(cur realm, toAdd, toRemove address) dao.ProposalRequest {\n\tif !toAdd.IsValid() || !toRemove.IsValid() {\n\t\tpanic(ErrInvalidAddress)\n\t}\n\n\tcb := func(cur realm) error {\n\t\treturn applyControllerSwap(toAdd, toRemove)\n\t}\n\n\tdesc := ufmt.Sprintf(\n\t\t\"This proposal adds %s and removes %s from `sys/users` realm's callers whitelist.\",\n\t\ttoAdd,\n\t\ttoRemove,\n\t)\n\treturn dao.NewProposalRequest(\"Add and Remove Whitelisted Callers From \\\"sys/users\\\" Realm\", desc, dao.NewSimpleExecutor(0, cur, cb, \"\"))\n}\n\n// applyControllerSwap is the callback body of ProposeControllerAdditionAndRemoval,\n// extracted so it can be unit-tested without driving the full GovDAO flow.\n//\n// The desired end state is \"toAdd is in the whitelist AND toRemove is out\".\n// Both operations are made idempotent so the swap doesn't silently no-op when\n// the chain state has drifted between proposal creation and execution:\n//\n//   - If toAdd is already whitelisted, treat addToWhitelist's\n//     ErrAlreadyWhitelisted as benign and continue to the remove step.\n//   - If toRemove is already absent, treat deleteFromWhitelist's\n//     ErrNotWhitelisted as benign and return success.\n//\n// Without this idempotency, the original code returned early on an \"already\n// whitelisted\" toAdd and skipped the remove entirely — a swap proposal could\n// pass governance and silently leave the old controller active. (audit\n// finding #6)\nfunc applyControllerSwap(toAdd, toRemove address) error {\n\tif err := addToWhitelist(toAdd); err != nil \u0026\u0026 err != ErrAlreadyWhitelisted {\n\t\treturn err\n\t}\n\tif err := deleteFromWhitelist(toRemove); err != nil {\n\t\tif _, alreadyOut := err.(ErrNotWhitelisted); !alreadyOut {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\n// ProposeRegisterUser allows GovDAO to register a name without checking\n// controllers. The executor closure runs with ignoreCanonical=true (decision\n// #3): DAO grants always bypass canonical-collision detection. Voters see\n// any collision in the proposal description and can vote NO if unintended.\nfunc ProposeRegisterUser(cur realm, name string, addr address) dao.ProposalRequest {\n\t// Validate the name and address now, even though registerUser will validate again\n\tif err := validateName(name); err != nil {\n\t\tpanic(err.Error())\n\t}\n\tif !addr.IsValid() {\n\t\tpanic(ErrInvalidAddress)\n\t}\n\n\tdesc := \"This proposal registers \" + name + \" with address \" + addr.String() + \" in `sys/users`.\"\n\tif existing, taken := IsCanonicalTaken(name); taken \u0026\u0026 existing != name {\n\t\tdesc += \"\\n\\nCANONICAL COLLISION: this name's canonical form matches the existing registration of `\" +\n\t\t\texisting + \"`. DAO grants bypass canonical-collision detection — the proposal will succeed if voted in. \" +\n\t\t\t\"If the collision is unintended, vote NO.\"\n\t}\n\n\tcb := func(cur realm) error {\n\t\treturn registerUser(cur, name, addr, true) // bypass canonical (decision #3)\n\t}\n\n\treturn dao.NewProposalRequest(\"Register User to \\\"sys/users\\\" Realm\", desc, dao.NewSimpleExecutor(0, cur, cb, \"\"))\n}\n\n// ProposeUpdateName allows GovDAO to update a name with an alias without\n// checking controllers. Like ProposeRegisterUser, the executor runs with\n// ignoreCanonical=true (decision #3).\nfunc ProposeUpdateName(cur realm, addr address, newName string) dao.ProposalRequest {\n\tif !addr.IsValid() {\n\t\tpanic(ErrInvalidAddress)\n\t}\n\tif err := validateName(newName); err != nil {\n\t\tpanic(err.Error())\n\t}\n\n\tdesc := \"This proposal updates address \" + addr.String() + \" with alias \" + newName + \" in `sys/users`.\"\n\tif existing, taken := IsCanonicalTaken(newName); taken \u0026\u0026 existing != newName {\n\t\tdesc += \"\\n\\nCANONICAL COLLISION: the new alias's canonical form matches the existing registration of `\" +\n\t\t\texisting + \"`. DAO grants bypass canonical-collision detection — the proposal will succeed if voted in. \" +\n\t\t\t\"If the collision is unintended, vote NO.\"\n\t}\n\n\tcb := func(cur realm) error {\n\t\tdata := ResolveAddress(addr)\n\t\tif data == nil {\n\t\t\treturn ErrUserNotExistOrDeleted\n\t\t}\n\t\treturn data.updateName(newName, true) // bypass canonical (decision #3)\n\t}\n\n\treturn dao.NewProposalRequest(\"Update Name Alias in \\\"sys/users\\\" Realm\", desc, dao.NewSimpleExecutor(0, cur, cb, \"\"))\n}\n\n// ProposeDeleteUser allows GovDAO to delete a user without checking controllers\nfunc ProposeDeleteUser(cur realm, addr address) dao.ProposalRequest {\n\tif !addr.IsValid() {\n\t\tpanic(ErrInvalidAddress)\n\t}\n\n\tcb := func(cur realm) error {\n\t\tdata := ResolveAddress(addr)\n\t\tif data == nil {\n\t\t\treturn ErrUserNotExistOrDeleted\n\t\t}\n\t\treturn data.delete()\n\t}\n\n\tdesc := \"This proposal deletes the user with address \" + addr.String() + \" in `sys/users`.\"\n\treturn dao.NewProposalRequest(\"Delete User in \\\"sys/users\\\" Realm\", desc, dao.NewSimpleExecutor(0, cur, cb, \"\"))\n}\n\n// IsController reports whether the given address is currently in the\n// controller whitelist. Returns the same boolean that gating checks\n// (RegisterUser, UpdateName, Delete) use internally — useful for\n// off-chain monitoring and for governance proposals to inspect state\n// before voting.\nfunc IsController(addr address) bool {\n\treturn controllers.Has(addr)\n}\n\n// Controllers returns a snapshot of the current controller whitelist.\n// The returned slice is a fresh copy; mutating it does not affect realm\n// state. Order is the iteration order of the underlying address set.\n//\n// Audit finding #20: without this getter, the controller whitelist was\n// opaque from outside the package — operators had to read source or\n// replay every governance proposal to know who could write to the\n// registry. This is the read-only API that closes that gap.\nfunc Controllers() []address {\n\tout := make([]address, 0, controllers.Size())\n\tcontrollers.IterateByOffset(0, controllers.Size(), func(a address) bool {\n\t\tout = append(out, a)\n\t\treturn false\n\t})\n\treturn out\n}\n\n// Helpers\n\nfunc deleteFromWhitelist(addr address) error {\n\tif !controllers.Has(addr) {\n\t\treturn ErrNotWhitelisted{Caller: \"UserRealm{ \" + addr.String() + \" }\"}\n\t}\n\n\tif ok := controllers.Remove(addr); !ok {\n\t\treturn ErrWhitelistRemoveFailed\n\t}\n\n\treturn nil\n}\n\nfunc addToWhitelist(newCaller address) error {\n\tif !controllers.Add(newCaller) {\n\t\treturn ErrAlreadyWhitelisted\n\t}\n\n\treturn nil\n}\n"},{"name":"api.gno","body":"package users\n\n// IsNameTaken reports whether the exact-string name exists in nameStore.\n// Returns true for any name ever registered, including:\n//\n//   - active registrations\n//   - tombstoned (deleted) users' names — Delete() sets `deleted=true`\n//     but does not remove the nameStore entry (anti-revival policy)\n//   - old aliases from renames — UpdateName inserts the new name\n//     alongside the old; the old key stays (anti-rename-squat policy)\n//\n// In short: IsNameTaken(name) answers \"would RegisterUser(name, _)\n// fail with ErrNameTaken?\" — same answer for active, deleted, or\n// aliased-away names. Pairs with IsCanonicalTaken (canonical-match)\n// and ResolveName (active-current-user lookup with full UserData).\n//\n// No canonicalization is applied. For controllers that want exact-\n// match uniqueness without pulling in canonical-collision logic.\nfunc IsNameTaken(name string) bool {\n\treturn nameStore.Has(name)\n}\n\n// IsCanonicalTaken reports whether the given name's canonical form is\n// already registered. Pass the raw name; canonicalization is applied\n// internally. The first return is the original (non-canonical) name\n// that owns the canonical key, for UX in collision messages.\n//\n// When a bypass write (RegisterUserIgnoreCanonical or the bypass path\n// through ProposeRegisterUser/ProposeUpdateName) overwrites a prior\n// canonical entry, this returns the most-recently-written original.\nfunc IsCanonicalTaken(name string) (existing string, taken bool) {\n\tv := canonicalStore.Get(Canonicalize(name))\n\tif v == nil {\n\t\treturn \"\", false\n\t}\n\treturn v.(string), true\n}\n"},{"name":"canonical.gno","body":"package users\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\n// canonicalStore maps canonicalized full names to the original name\n// that was registered. Keyed by the result of Canonicalize.\n//\n// Multiple controllers (namereg/v1, future registries, governance,\n// genesis bootstrapping) all share this single store via RegisterUser\n// and the bypass variant RegisterUserIgnoreCanonical. Cross-controller\n// canonical-collision detection is uniform and atomic with the\n// nameStore write.\nvar canonicalStore = bptree.NewBPTree32()\n\n// Canonicalize returns the canonical form of a name. The substitutions\n// collapse single-character visual confusables that arise across the\n// allowed [a-z0-9] character set, and strip the three separators that\n// can sneak between identical alphanumeric runs.\n//\n//   - {l, i, 1} → i\n//   - {0, o}    → o\n//   - {-, ., _} → stripped\n//   - all other characters unchanged\n//\n// CONTRACT: stable. Future controllers that want to share this\n// canonical namespace MUST use this exact function — do not roll your\n// own. Adding new substitutions later is a breaking change because it\n// would silently re-key existing entries in canonicalStore.\n//\n// Input contract: ASCII-only. r/sys/users.validateName already rejects\n// non-ASCII at the registration boundary, so by the time a name reaches\n// Canonicalize through the standard write path it is guaranteed to be\n// ASCII. Direct callers from other realms must honor this contract;\n// non-ASCII bytes are passed through as-is and will produce undefined\n// collision behavior.\n//\n// Multi-char confusables (m↔rn, nn↔m, cl↔d) are NOT canonicalized.\n// They require fixed-point substring substitution rounds, which is out\n// of scope for the unified store.\n//\n// Pure: no state access. Safe to call from anywhere.\nfunc Canonicalize(name string) string {\n\tvar b strings.Builder\n\tb.Grow(len(name))\n\tfor i := 0; i \u003c len(name); i++ {\n\t\tc := name[i]\n\t\tswitch c {\n\t\tcase 'l', '1':\n\t\t\tb.WriteByte('i')\n\t\tcase '0':\n\t\t\tb.WriteByte('o')\n\t\tcase '-', '.', '_':\n\t\t\t// strip\n\t\tdefault:\n\t\t\tb.WriteByte(c)\n\t\t}\n\t}\n\treturn b.String()\n}\n"},{"name":"crossrealm_test.gno","body":"package users\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// TestUpdateNameCallerIdentity verifies that the CurrentRealm() check in\n// UpdateName correctly identifies the calling controller.\n//\n// Background: RegisterUser uses PreviousRealm() because it is a crossing\n// function (cur realm). UpdateName uses CurrentRealm() because it is a\n// non-crossing method on *UserData. Per the interrealm spec:\n//\n//   - Crossing function: CurrentRealm = this realm, PreviousRealm = caller\n//   - Non-crossing method on external object, called from crossing context:\n//     CurrentRealm = caller (unchanged from crossing context)\n//\n// This test verifies that a whitelisted controller can call UpdateName,\n// and a non-whitelisted realm cannot.\nfunc TestUpdateNameCallerIdentity(cur realm, t *testing.T) {\n\tcontrollerPath := initControllerPath\n\tnonControllerPath := \"gno.land/r/evil/attacker\"\n\n\tt.Run(\"whitelisted_controller_can_update\", func(cur realm, t *testing.T) {\n\t\tcleanStore(t)\n\n\t\t// Register as whitelisted controller\n\t\ttesting.SetRealm(testing.NewCodeRealm(controllerPath))\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"testuser1\", testutils.TestAddress(\"testuser1\")))\n\n\t\t// Resolve and update name — should succeed because controller is whitelisted\n\t\tdata := ResolveAddress(testutils.TestAddress(\"testuser1\"))\n\t\turequire.NotEqual(t, nil, data)\n\t\tuassert.NoError(t, data.UpdateName(0, cur, \"newname1\"))\n\t\tuassert.Equal(t, \"newname1\", data.Name())\n\t})\n\n\tt.Run(\"non_whitelisted_realm_cannot_update\", func(cur realm, t *testing.T) {\n\t\tcleanStore(t)\n\n\t\t// Register as whitelisted controller\n\t\ttesting.SetRealm(testing.NewCodeRealm(controllerPath))\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"testuser2\", testutils.TestAddress(\"testuser2\")))\n\n\t\tdata := ResolveAddress(testutils.TestAddress(\"testuser2\"))\n\t\turequire.NotEqual(t, nil, data)\n\n\t\t// Switch to non-whitelisted realm — UpdateName should fail\n\t\ttesting.SetRealm(testing.NewCodeRealm(nonControllerPath))\n\t\terr := data.UpdateName(0, cur, \"hacked\")\n\t\tuassert.ErrorContains(t, err, \"does not exist in whitelist\")\n\t\tuassert.Equal(t, \"testuser2\", data.Name()) // name unchanged\n\t})\n}\n\n// TestDeleteCallerIdentity verifies the same CurrentRealm() behavior for Delete.\nfunc TestDeleteCallerIdentity(cur realm, t *testing.T) {\n\tcontrollerPath := initControllerPath\n\tnonControllerPath := \"gno.land/r/evil/attacker\"\n\n\tt.Run(\"whitelisted_controller_can_delete\", func(cur realm, t *testing.T) {\n\t\tcleanStore(t)\n\n\t\ttesting.SetRealm(testing.NewCodeRealm(controllerPath))\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"deluser1\", testutils.TestAddress(\"deluser1\")))\n\n\t\tdata := ResolveAddress(testutils.TestAddress(\"deluser1\"))\n\t\turequire.NotEqual(t, nil, data)\n\t\tuassert.NoError(t, data.Delete(0, cur))\n\t\tuassert.True(t, data.IsDeleted())\n\t})\n\n\tt.Run(\"non_whitelisted_realm_cannot_delete\", func(cur realm, t *testing.T) {\n\t\tcleanStore(t)\n\n\t\ttesting.SetRealm(testing.NewCodeRealm(controllerPath))\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"deluser2\", testutils.TestAddress(\"deluser2\")))\n\n\t\tdata := ResolveAddress(testutils.TestAddress(\"deluser2\"))\n\t\turequire.NotEqual(t, nil, data)\n\n\t\t// Switch to non-whitelisted realm — Delete should fail\n\t\ttesting.SetRealm(testing.NewCodeRealm(nonControllerPath))\n\t\terr := data.Delete(0, cur)\n\t\tuassert.ErrorContains(t, err, \"does not exist in whitelist\")\n\t\tuassert.False(t, data.IsDeleted()) // not deleted\n\t})\n}\n"},{"name":"errors.gno","body":"package users\n\nimport (\n\t\"errors\"\n)\n\nconst prefix = \"r/sys/users: \"\n\nvar (\n\tErrAlreadyWhitelisted    = errors.New(prefix + \"already whitelisted\")\n\tErrWhitelistRemoveFailed = errors.New(prefix + \"failed to remove address from whitelist\")\n\n\tErrNameTaken          = errors.New(prefix + \"name/Alias already taken\")\n\tErrCanonicalCollision = errors.New(prefix + \"name collides with a confusable variant of an existing name\")\n\tErrInvalidAddress     = errors.New(prefix + \"invalid address\")\n\n\tErrEmptyUsername   = errors.New(prefix + \"empty username provided\")\n\tErrNameLikeAddress = errors.New(prefix + \"username resembles a gno.land address\")\n\tErrInvalidUsername = errors.New(prefix + \"username must match ^[a-z][a-z0-9]*([_-][a-z0-9]+)*$ (max 64 chars)\")\n\n\tErrAlreadyHasName = errors.New(prefix + \"username for this address already registered - try creating an Alias\")\n\tErrDeletedUser    = errors.New(prefix + \"cannot register a new username after deleting\")\n\n\tErrUserNotExistOrDeleted = errors.New(prefix + \"this user does not exist or was deleted\")\n\n\t// ErrInvalidRealm is returned by controller-gated *UserData mutators\n\t// when the supplied rlm is not the caller's live cur (i.e.\n\t// rlm.IsCurrent() is false). Closes Class-2 designation forgery via\n\t// a stored stale realm value whose .Address() resolves to a\n\t// whitelisted controller. See docs/resources/gno-security.md.\n\tErrInvalidRealm = errors.New(prefix + \"rlm is not the caller's live cur\")\n)\n\n// ErrNotWhitelisted stores the failing caller's realm identity as a\n// plain string so the error is a pure data record (no live realm values\n// in its fields).\ntype ErrNotWhitelisted struct {\n\tCaller string // \"CodeRealm{ \u003caddr\u003e, \u003cpkgPath\u003e }\" or \"UserRealm{ \u003caddr\u003e }\" — failed the whitelist check\n}\n\n// NewErrNotWhitelisted constructs the error with the caller's realm\n// identity captured as a string at construction time. The _ int\n// discriminator keeps this non-crossing (a non-crossing function can't\n// take a `realm`-named-`cur` first param, so we use the standard\n// _ int, rlm realm shape).\nfunc NewErrNotWhitelisted(_ int, caller realm) ErrNotWhitelisted {\n\treturn ErrNotWhitelisted{\n\t\tCaller: caller.String(),\n\t}\n}\n\nfunc (e ErrNotWhitelisted) Error() string {\n\treturn prefix + \"caller realm/user does not exist in whitelist: \" + e.Caller\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/users\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"render.gno","body":"package users\n\nimport \"gno.land/p/nt/ufmt/v0\"\n\nfunc Render(_ string) string {\n\tout := \"# r/sys/users\\n\\n\"\n\n\tout += \"`r/sys/users` is a system realm for managing user registrations.\\n\\n\"\n\tout += \"User registration is managed through whitelisted controller realms.\\n\\n\"\n\tout += \"---\\n\\n\"\n\n\tout += \"## Stats\\n\\n\"\n\tout += ufmt.Sprintf(\"Total unique addresses registered: **%d**\\n\\n\", addressStore.Size())\n\tout += ufmt.Sprintf(\"Total unique names registered: **%d**\\n\\n\", nameStore.Size())\n\treturn out\n}\n"},{"name":"store.gno","body":"package users\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\t\"regexp\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tnameStore    = bptree.NewBPTree32() // name/aliases \u003e *UserData\n\taddressStore = bptree.NewBPTree32() // address \u003e *UserData\n\n\treAddressLookalike = regexp.MustCompile(`^g1[a-z0-9]{20,38}$`)\n\n\t// reName mirrors gno's package-name shape (gnovm/pkg/gnolang/mempackage.go\n\t// `Re_name`): start with a lowercase letter, optional alphanumeric body,\n\t// then any number of (separator + alphanumeric run) — so single hyphens\n\t// or underscores are allowed BETWEEN alphanumerics, but consecutive\n\t// separators (`--`, `__`, `-_`, `_-`) are rejected, and so are leading\n\t// or trailing separators. Lowercase-only — closes the case-confusable\n\t// squatting concern (Alice vs alice were two distinct names under the\n\t// previous case-preserving regex). Length cap of 64 enforced separately\n\t// in validateName.\n\treName = regexp.MustCompile(`^[a-z][a-z0-9]*([_-][a-z0-9]+)*$`)\n)\n\nconst maxNameLen = 64\n\nconst (\n\tRegisterUserEvent = \"Registered\"\n\tUpdateNameEvent   = \"Updated\"\n\tDeleteUserEvent   = \"Deleted\"\n)\n\ntype UserData struct {\n\taddr     address\n\tusername string // contains the latest name of a user\n\tdeleted  bool\n}\n\nfunc (u UserData) Name() string {\n\treturn u.username\n}\n\nfunc (u UserData) Addr() address {\n\treturn u.addr\n}\n\n// IsDeleted reports whether this user record is missing or marked deleted.\n// A nil receiver returns true — \"the user does not exist\" is semantically\n// indistinguishable from \"the user was deleted\" for callers that need to\n// gate further state changes. This lets call sites collapse the nil check\n// and the deleted check into a single guard:\n//\n//\tif u.IsDeleted() {\n//\t    return ErrUserNotExistOrDeleted\n//\t}\nfunc (u *UserData) IsDeleted() bool {\n\tif u == nil {\n\t\treturn true\n\t}\n\treturn u.deleted\n}\n\n// RenderLink provides a render link to the user page on gnoweb\n// `linkText` is optional\nfunc (u UserData) RenderLink(linkText string) string {\n\tif linkText == \"\" {\n\t\treturn ufmt.Sprintf(\"[@%s](/u/%s)\", u.username, u.username)\n\t}\n\n\treturn ufmt.Sprintf(\"[%s](/u/%s)\", linkText, u.username)\n}\n\n// registerUser adds a new user to the system without checking controllers.\n// The ignoreCanonical flag suppresses ErrCanonicalCollision; the canonical\n// store is written either way (decision #14: later-wins on bypass).\nfunc registerUser(cur realm, name string, address_XXX address, ignoreCanonical bool) error {\n\t// Validate name\n\tif err := validateName(name); err != nil {\n\t\treturn err\n\t}\n\n\t// Validate address\n\tif !address_XXX.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\n\t// Check if name is taken (exact-string match precedes canonical check)\n\tif nameStore.Has(name) {\n\t\treturn ErrNameTaken\n\t}\n\n\tcanonical := Canonicalize(name)\n\tif !ignoreCanonical {\n\t\tif canonicalStore.Has(canonical) {\n\t\t\treturn ErrCanonicalCollision\n\t\t}\n\t}\n\n\traw := addressStore.Get(address_XXX.String())\n\tif raw != nil {\n\t\t// Cannot re-register after deletion\n\t\tif raw.(*UserData).IsDeleted() {\n\t\t\treturn ErrDeletedUser\n\t\t}\n\n\t\t// For a second name, use UpdateName\n\t\treturn ErrAlreadyHasName\n\t}\n\n\t// Create UserData\n\tdata := \u0026UserData{\n\t\taddr:     address_XXX,\n\t\tusername: name,\n\t\tdeleted:  false,\n\t}\n\n\t// Set corresponding stores\n\tnameStore.Set(name, data)\n\taddressStore.Set(address_XXX.String(), data)\n\tcanonicalStore.Set(canonical, name)\n\n\tchain.Emit(RegisterUserEvent,\n\t\t\"name\", name,\n\t\t\"address\", address_XXX.String(),\n\t)\n\treturn nil\n}\n\n// RegisterUser adds a new user to the system. Enforces canonical-\n// collision detection: a name whose Canonicalize-form matches a prior\n// registration returns ErrCanonicalCollision.\nfunc RegisterUser(cur realm, name string, address_XXX address) error {\n\t// At genesis (height 0), allow any caller to register users.\n\t// After genesis, only whitelisted controllers can register.\n\tif runtime.ChainHeight() \u003e 0 \u0026\u0026 !controllers.Has(cur.Previous().Address()) {\n\t\treturn NewErrNotWhitelisted(0, cur.Previous())\n\t}\n\n\treturn registerUser(cur, name, address_XXX, false)\n}\n\n// RegisterUserIgnoreCanonical is the bypass path: same controller-\n// whitelist gate, but ErrCanonicalCollision is suppressed. The canonical\n// store is still written; a prior entry with the same canonical key is\n// silently overwritten (decision #14, later-wins). Use sparingly — names\n// registered here can canonical-collide with existing ones, weakening\n// confusable protection for everyone.\nfunc RegisterUserIgnoreCanonical(cur realm, name string, address_XXX address) error {\n\tif runtime.ChainHeight() \u003e 0 \u0026\u0026 !controllers.Has(cur.Previous().Address()) {\n\t\treturn NewErrNotWhitelisted(0, cur.Previous())\n\t}\n\n\treturn registerUser(cur, name, address_XXX, true)\n}\n\n// updateName adds a name that is associated with a specific address without\n// checking controllers. The ignoreCanonical flag suppresses\n// ErrCanonicalCollision; the canonical store is written either way (decision\n// #14: later-wins on bypass).\n//\n// All previous names are preserved and resolvable.\n// The new name is the default value returned for address lookups.\nfunc (u *UserData) updateName(newName string, ignoreCanonical bool) error {\n\t// IsDeleted handles both branches: nil receiver (user never existed)\n\t// AND a non-nil receiver whose .deleted is true (a controller cached\n\t// the *UserData pointer before the user was deleted by a separate\n\t// controller or governance proposal). Without the deleted-flag branch,\n\t// nameStore.Set(newName, u) would insert an alias pointing at a\n\t// deleted user — Has(newName) returns true forever but Resolve(newName)\n\t// returns nil (Resolve* APIs filter deleted), so the name is squatted\n\t// with no recovery path. (audit finding #3)\n\tif u.IsDeleted() {\n\t\treturn ErrUserNotExistOrDeleted\n\t}\n\n\t// Validate name\n\tif err := validateName(newName); err != nil {\n\t\treturn err\n\t}\n\n\t// Check if the requested Alias is already taken (exact-string match)\n\tif nameStore.Has(newName) {\n\t\treturn ErrNameTaken\n\t}\n\n\tcanonical := Canonicalize(newName)\n\tif !ignoreCanonical {\n\t\t// No self-collision filter (decision #15): even the user's OWN\n\t\t// prior canonical claim blocks the rename. Prevents accumulating\n\t\t// confusable aliases of one's own name through free renames. The\n\t\t// only path to a self-confusable rename is DAO governance via\n\t\t// ProposeUpdateName.\n\t\tif canonicalStore.Has(canonical) {\n\t\t\treturn ErrCanonicalCollision\n\t\t}\n\t}\n\n\tu.username = newName\n\tnameStore.Set(newName, u)\n\tcanonicalStore.Set(canonical, newName)\n\n\tchain.Emit(UpdateNameEvent,\n\t\t\"alias\", newName,\n\t\t\"address\", u.addr.String(),\n\t)\n\treturn nil\n}\n\n// UpdateName adds a name that is associated with a specific address.\n// Enforces canonical-collision detection.\n// All previous names are preserved and resolvable.\n// The new name is the default value returned for address lookups.\n//\n// rlm is the cur of the caller's enclosing crossing function (passed as\n// data via the `_ int, rlm realm` non-crossing form). rlm.Address() is\n// the calling realm against which we authorize.\nfunc (u *UserData) UpdateName(_ int, rlm realm, newName string) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrInvalidRealm\n\t}\n\tif u.IsDeleted() {\n\t\treturn ErrUserNotExistOrDeleted\n\t}\n\n\t// Validate caller\n\tif !controllers.Has(rlm.Address()) {\n\t\treturn NewErrNotWhitelisted(0, rlm)\n\t}\n\n\treturn u.updateName(newName, false)\n}\n\n// UpdateNameIgnoreCanonical is the bypass path: same controller-\n// whitelist gate, but ErrCanonicalCollision is suppressed. The canonical\n// store is still written; a prior entry with the same canonical key is\n// silently overwritten (decision #14, later-wins).\nfunc (u *UserData) UpdateNameIgnoreCanonical(_ int, rlm realm, newName string) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrInvalidRealm\n\t}\n\tif u.IsDeleted() {\n\t\treturn ErrUserNotExistOrDeleted\n\t}\n\n\tif !controllers.Has(rlm.Address()) {\n\t\treturn NewErrNotWhitelisted(0, rlm)\n\t}\n\n\treturn u.updateName(newName, true)\n}\n\n// delete marks a user and all their aliases as deleted without checking controllers.\nfunc (u *UserData) delete() error {\n\tif u.IsDeleted() {\n\t\treturn ErrUserNotExistOrDeleted\n\t}\n\n\tu.deleted = true\n\n\tchain.Emit(DeleteUserEvent, \"address\", u.addr.String())\n\treturn nil\n}\n\n// Delete marks a user and all their aliases as deleted.\n// rlm is the cur of the caller's enclosing crossing function; see UpdateName.\nfunc (u *UserData) Delete(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrInvalidRealm\n\t}\n\tif u.IsDeleted() {\n\t\treturn ErrUserNotExistOrDeleted\n\t}\n\n\t// Validate caller\n\tif !controllers.Has(rlm.Address()) {\n\t\treturn NewErrNotWhitelisted(0, rlm)\n\t}\n\n\treturn u.delete()\n}\n\n// Validate validates username and address passed in\n// Most of the validation is done in the controllers\n// This provides more flexibility down the line\nfunc validateName(username string) error {\n\tif username == \"\" {\n\t\treturn ErrEmptyUsername\n\t}\n\n\tif len(username) \u003e maxNameLen {\n\t\treturn ErrInvalidUsername\n\t}\n\n\tif !reName.MatchString(username) {\n\t\treturn ErrInvalidUsername\n\t}\n\n\t// Check if the username can be decoded or looks like a valid address\n\tif address(username).IsValid() || reAddressLookalike.MatchString(username) {\n\t\treturn ErrNameLikeAddress\n\t}\n\n\treturn nil\n}\n"},{"name":"store_test.gno","body":"package users\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nvar (\n\talice     = \"alice\"\n\taliceAddr = testutils.TestAddress(alice)\n\tbob       = \"bob\"\n\tbobAddr   = testutils.TestAddress(bob)\n\n\twhitelistedCallerAddr = chain.PackageAddress(initControllerPath)\n)\n\nfunc TestRegister(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"valid_registration\", func(t *testing.T) {\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\tres, isLatest := ResolveName(alice)\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.True(t, isLatest)\n\n\t\tres = ResolveAddress(aliceAddr)\n\t\tuassert.Equal(t, alice, res.Name())\n\t})\n\n\tt.Run(\"invalid_inputs\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"\", aliceAddr), ErrEmptyUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), alice, \"\"), ErrInvalidAddress.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), alice, \"invalidaddress\"), ErrInvalidAddress.Error())\n\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"username with a space\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t,\n\t\t\tRegisterUser(cross(cur), \"verylongusernameverylongusernameverylongusernameverylongusername1\", aliceAddr),\n\t\t\tErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"namewith^\u0026()\", aliceAddr), ErrInvalidUsername.Error())\n\n\t\t// Lowercase-only enforcement (closes case-confusable squatting via\n\t\t// Alice/alice/ALICE registering as distinct names — see TO_REVIEW\n\t\t// adversarial review). reName mirrors gno's mempackage Re_name shape.\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"Alice\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"ALICE\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"aLice\", aliceAddr), ErrInvalidUsername.Error())\n\n\t\t// Must start with a lowercase letter. Names starting with a digit,\n\t\t// underscore, or hyphen are rejected.\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"1alice\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"9\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"_alice\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"-alice\", aliceAddr), ErrInvalidUsername.Error())\n\n\t\t// Cannot end with a separator either (the new mempackage-aligned\n\t\t// regex requires the final char to be in [a-z0-9]).\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"alice_\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"alice-\", aliceAddr), ErrInvalidUsername.Error())\n\n\t\t// Cannot have consecutive separators in the middle either.\n\t\t// Each separator MUST be followed by at least one alphanumeric.\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"alice--bob\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"alice__bob\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"alice-_bob\", aliceAddr), ErrInvalidUsername.Error())\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"alice_-bob\", aliceAddr), ErrInvalidUsername.Error())\n\n\t\t// Length cap of exactly 64 — boundary cases.\n\t\texactly65 := \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" // 65 chars\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), exactly65, aliceAddr), ErrInvalidUsername.Error())\n\t})\n\n\tt.Run(\"valid_edge_cases\", func(t *testing.T) {\n\t\t// Names valid under the mempackage-aligned reName: starts with a\n\t\t// letter, may contain hyphens or underscores in the middle, ends\n\t\t// in [a-z0-9]. The hyphen support is what enables the namereg/v1\n\t\t// `nym-...` prefix shape.\n\t\tcleanStore(t)\n\t\turequire.NoError(t, RegisterUser(cross(cur),\n\t\t\t\"nym-alice123\",\n\t\t\ttestutils.TestAddress(\"u_nym_alice\")))\n\n\t\tcleanStore(t)\n\t\turequire.NoError(t, RegisterUser(cross(cur),\n\t\t\t\"a_b_c\",\n\t\t\ttestutils.TestAddress(\"u_underscore\")))\n\n\t\tcleanStore(t)\n\t\turequire.NoError(t, RegisterUser(cross(cur),\n\t\t\t\"a\",\n\t\t\ttestutils.TestAddress(\"u_singlechar\")))\n\n\t\tcleanStore(t)\n\t\t// 64 chars exactly (length cap is inclusive).\n\t\texactly64 := \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n\t\turequire.NoError(t, RegisterUser(cross(cur),\n\t\t\texactly64,\n\t\t\ttestutils.TestAddress(\"u_64\")))\n\t})\n\n\tt.Run(\"addr_already_registered\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\t// Try registering again\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"othername\", aliceAddr), ErrAlreadyHasName.Error())\n\t})\n\n\tt.Run(\"name_taken\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\t// Try registering alice's name with bob's address\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), alice, bobAddr), ErrNameTaken.Error())\n\t})\n\n\tt.Run(\"user_deleted\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\t\turequire.NoError(t, data.Delete(0, cur))\n\n\t\t// Try re-registering after deletion\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"newname\", aliceAddr), ErrDeletedUser.Error())\n\t})\n\n\tt.Run(\"address_lookalike\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\t// Address as username\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", aliceAddr), ErrNameLikeAddress.Error())\n\t\t// Beginning of address as username\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), \"g1jg8mtutu9khhfwc4nxmu\", aliceAddr), ErrNameLikeAddress.Error())\n\t\tuassert.NoError(t, RegisterUser(cross(cur), \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5longerthananaddress\", aliceAddr))\n\t})\n}\n\nfunc TestUpdateName(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"valid_direct_alias\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\t\t{\n\t\t\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\t\t\tuassert.NoError(t, data.UpdateName(0, cur, \"alice1\"))\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\t})\n\n\tt.Run(\"valid_double_alias\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\t\t{\n\t\t\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\t\t\tuassert.NoError(t, data.UpdateName(0, cur, \"alice2\"))\n\t\t\tuassert.NoError(t, data.UpdateName(0, cur, \"alice3\"))\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\t\tuassert.Equal(t, ResolveAddress(aliceAddr).username, \"alice3\")\n\t})\n\n\tt.Run(\"name_taken\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\tdata := ResolveAddress(aliceAddr)\n\t\tuassert.Error(t, data.UpdateName(0, cur, alice), ErrNameTaken.Error())\n\t})\n\n\tt.Run(\"alias_before_name\", func(t *testing.T) {\n\t\tcleanStore(t)\n\t\tdata := ResolveAddress(aliceAddr) // not registered\n\n\t\tuassert.ErrorContains(t, data.UpdateName(0, cur, alice), ErrUserNotExistOrDeleted.Error())\n\t})\n\n\tt.Run(\"alias_after_delete\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\t\t{\n\t\t\turequire.NoError(t, data.Delete(0, cur))\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\n\t\tdata = ResolveAddress(aliceAddr)\n\t\t{\n\t\t\tuassert.ErrorContains(t, data.UpdateName(0, cur, \"newalice\"), ErrUserNotExistOrDeleted.Error())\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\t})\n\n\t// Audit finding #3: a controller holding a cached *UserData pointer to a\n\t// user that gets deleted between resolve and update must not be able to\n\t// insert a new alias. The alias_after_delete test above re-resolves and\n\t// gets nil, so it only exercises the u==nil branch. This test holds the\n\t// pointer across the delete and verifies the deleted-flag branch.\n\tt.Run(\"alias_with_cached_pointer_after_delete\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\t// Cache the pointer BEFORE deletion (simulating a controller that\n\t\t// resolved earlier and held the reference).\n\t\tcached := ResolveAddress(aliceAddr)\n\t\turequire.NotEqual(t, nil, cached)\n\n\t\t// Delete via a fresh resolve.\n\t\tdata := ResolveAddress(aliceAddr)\n\t\t{\n\t\t\turequire.NoError(t, data.Delete(0, cur))\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\n\t\t// The cached pointer is non-nil but its .deleted is now true.\n\t\turequire.NotEqual(t, nil, cached)\n\t\tuassert.True(t, cached.IsDeleted())\n\n\t\t// Attempting UpdateName on the cached pointer must reject — without\n\t\t// this check, \"squattedname\" would be inserted into nameStore\n\t\t// pointing at the deleted user, becoming permanently unresolvable\n\t\t// AND unregisterable.\n\t\t{\n\t\t\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\t\t\tuassert.ErrorContains(t, cached.UpdateName(0, cur, \"squattedname\"), ErrUserNotExistOrDeleted.Error())\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\n\t\t// Confirm the squat didn't happen: nameStore should NOT have it.\n\t\tuassert.False(t, nameStore.Has(\"squattedname\"))\n\t})\n\n\tt.Run(\"invalid_inputs\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\t\t{\n\t\t\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\t\t\tuassert.ErrorContains(t, data.UpdateName(0, cur, \"\"), ErrEmptyUsername.Error())\n\t\t\tuassert.ErrorContains(t, data.UpdateName(0, cur, \"username with a space\"), ErrInvalidUsername.Error())\n\t\t\tuassert.ErrorContains(t,\n\t\t\t\tdata.UpdateName(0, cur, \"verylongusernameverylongusernameverylongusernameverylongusername1\"),\n\t\t\t\tErrInvalidUsername.Error())\n\t\t\tuassert.ErrorContains(t, data.UpdateName(0, cur, \"namewith^\u0026()\"), ErrInvalidUsername.Error())\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\t})\n\n\tt.Run(\"address_lookalike\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\n\t\t{\n\t\t\t// Address as username\n\t\t\tuassert.ErrorContains(t, data.UpdateName(0, cur, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"), ErrNameLikeAddress.Error())\n\t\t\t// Beginning of address as username\n\t\t\tuassert.ErrorContains(t, data.UpdateName(0, cur, \"g1jg8mtutu9khhfwc4nxmu\"), ErrNameLikeAddress.Error())\n\t\t\tuassert.NoError(t, data.UpdateName(0, cur, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5longerthananaddress\"))\n\t\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t\t}\n\t})\n}\n\nfunc TestDelete(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"non_existent_user\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\tdata := ResolveAddress(testutils.TestAddress(\"unregistered\"))\n\t\tuassert.ErrorContains(t, data.Delete(0, cur), ErrUserNotExistOrDeleted.Error())\n\t})\n\n\tt.Run(\"double_delete\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\t\turequire.NoError(t, data.Delete(0, cur))\n\t\tdata = ResolveAddress(aliceAddr)\n\t\tuassert.ErrorContains(t, data.Delete(0, cur), ErrUserNotExistOrDeleted.Error())\n\t})\n\n\tt.Run(\"valid_delete\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\t\tuassert.NoError(t, data.Delete(0, cur))\n\n\t\tresolved1, _ := ResolveName(alice)\n\t\tuassert.Equal(t, nil, resolved1)\n\t\tuassert.Equal(t, nil, ResolveAddress(aliceAddr))\n\t})\n}\n\nfunc TestRegisterNotWhitelisted(cur realm, t *testing.T) {\n\tt.Run(\"register_not_whitelisted\", func(t *testing.T) {\n\t\tuassert.ErrorContains(t, RegisterUser(cross(cur), alice, aliceAddr), \"does not exist in whitelist\")\n\t})\n}\n\nfunc TestCanonicalize(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tin, want string\n\t}{\n\t\t// Single-rule cases.\n\t\t{\"l\", \"i\"}, {\"i\", \"i\"}, {\"1\", \"i\"},\n\t\t{\"0\", \"o\"}, {\"o\", \"o\"},\n\t\t{\"-\", \"\"}, {\".\", \"\"}, {\"_\", \"\"},\n\n\t\t// Identity / pass-through.\n\t\t{\"abc\", \"abc\"}, {\"xyz\", \"xyz\"}, {\"123\", \"i23\"},\n\n\t\t// Open Nym Tier examples from the design doc.\n\t\t{\"nym-vital1k123\", \"nymvitaiiki23\"},\n\t\t{\"nym-vitalik123\", \"nymvitaiiki23\"},\n\t\t{\"nym-vital1k999\", \"nymvitaiik999\"},\n\n\t\t// Same stem, different digit suffix → distinct canonicals.\n\t\t{\"nym-foolbar000\", \"nymfooibarooo\"},\n\t\t{\"nym-foolbar001\", \"nymfooibarooi\"},\n\n\t\t// Three-way separator equivalence.\n\t\t{\"xyz-com\", \"xyzcom\"},\n\t\t{\"xyz_com\", \"xyzcom\"},\n\t\t{\"xyz.com\", \"xyzcom\"},\n\n\t\t// Idempotency: applying twice equals applying once.\n\t\t{\"nymvitaiiki23\", \"nymvitaiiki23\"},\n\n\t\t// Empty input.\n\t\t{\"\", \"\"},\n\t}\n\tfor _, tc := range cases {\n\t\tgot := Canonicalize(tc.in)\n\t\tuassert.Equal(t, tc.want, got)\n\t}\n}\n\nfunc TestCanonicalize_NonASCIIPassthrough(cur realm, t *testing.T) {\n\t// ASCII-only contract: the function passes non-ASCII bytes through\n\t// unchanged. Locks the documented contract — registration paths\n\t// reject non-ASCII upstream via validateName, so this code path is\n\t// only reachable through direct callers from other realms.\n\tuassert.Equal(t, \"café\", Canonicalize(\"café\"))\n\tuassert.Equal(t, \"naïve\", Canonicalize(\"naïve\"))\n}\n\nfunc TestRegisterUser_CanonicalCollision(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"non_bypass_blocks\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\t\t// Different exact name, same canonical form → ErrCanonicalCollision.\n\t\tuassert.ErrorContains(t,\n\t\t\tRegisterUser(cross(cur), \"vital1k\", bobAddr),\n\t\t\tErrCanonicalCollision.Error(),\n\t\t)\n\t})\n\n\tt.Run(\"non_bypass_blocks_with_separator_strip\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"xyz-com\", aliceAddr))\n\t\tuassert.ErrorContains(t,\n\t\t\tRegisterUser(cross(cur), \"xyz_com\", bobAddr),\n\t\t\tErrCanonicalCollision.Error(),\n\t\t)\n\t})\n\n\tt.Run(\"non_bypass_allows_different_digit_suffix\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\t// Same alpha stem, different digits-after-canonicalization → no collision.\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"nym-foolbar000\", aliceAddr))\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"nym-foolbar999\",\n\t\t\ttestutils.TestAddress(\"foolbar999\")))\n\t})\n\n\tt.Run(\"exact_name_taken_takes_precedence\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\t\t// Same exact name → ErrNameTaken (NOT ErrCanonicalCollision).\n\t\t// The nameStore.Has check precedes the canonical check.\n\t\tuassert.ErrorContains(t,\n\t\t\tRegisterUser(cross(cur), \"vitalik\", bobAddr),\n\t\t\tErrNameTaken.Error(),\n\t\t)\n\t})\n}\n\nfunc TestRegisterUserIgnoreCanonical(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"bypass_succeeds_on_canonical_collision\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\t\t// Bypass succeeds even though canonical collides.\n\t\turequire.NoError(t, RegisterUserIgnoreCanonical(cross(cur), \"vital1k\", bobAddr))\n\n\t\t// Both names resolve.\n\t\tres, _ := ResolveName(\"vitalik\")\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tres, _ = ResolveName(\"vital1k\")\n\t\tuassert.Equal(t, bobAddr, res.Addr())\n\t})\n\n\tt.Run(\"bypass_later_wins_overwrite\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\t// First registration writes canonical entry.\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\t\texisting, taken := IsCanonicalTaken(\"vital1k\")\n\t\turequire.True(t, taken)\n\t\tuassert.Equal(t, \"vitalik\", existing)\n\n\t\t// Bypass write overwrites the canonical entry (decision #14).\n\t\turequire.NoError(t, RegisterUserIgnoreCanonical(cross(cur), \"vital1k\", bobAddr))\n\t\texisting, taken = IsCanonicalTaken(\"vitalik\")\n\t\turequire.True(t, taken)\n\t\tuassert.Equal(t, \"vital1k\", existing)\n\t})\n\n\tt.Run(\"bypass_still_blocks_exact_taken\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\t\t// Same exact name still blocked, even via the bypass path.\n\t\tuassert.ErrorContains(t,\n\t\t\tRegisterUserIgnoreCanonical(cross(cur), \"vitalik\", bobAddr),\n\t\t\tErrNameTaken.Error(),\n\t\t)\n\t})\n}\n\nfunc TestUpdateName_CanonicalCollision(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"self_collision_blocks_per_decision_15\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\t// Alice registers vital1k. Canonical store maps \"vitaiik\" → \"vital1k\".\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vital1k\", aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\n\t\t// Alice attempts to rename to a confusable variant of HER OWN name.\n\t\t// Decision #15: blocked. No self-collision exception in the\n\t\t// non-bypass path.\n\t\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\t\tuassert.ErrorContains(t,\n\t\t\tdata.UpdateName(0, cur, \"vitalik\"),\n\t\t\tErrCanonicalCollision.Error(),\n\t\t)\n\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t})\n\n\tt.Run(\"different_user_collision_blocks\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"bob\", bobAddr))\n\n\t\tdata := ResolveAddress(bobAddr)\n\t\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\t\tuassert.ErrorContains(t,\n\t\t\tdata.UpdateName(0, cur, \"vital1k\"),\n\t\t\tErrCanonicalCollision.Error(),\n\t\t)\n\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\t})\n\n\tt.Run(\"bypass_allows_self_collision_rename\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vital1k\", aliceAddr))\n\t\tdata := ResolveAddress(aliceAddr)\n\n\t\t// Bypass path allows the rename (DAO grant scenario).\n\t\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\t\turequire.NoError(t, data.UpdateNameIgnoreCanonical(0, cur, \"vitalik\"))\n\t\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\n\t\t// Both names point to alice; latest is \"vitalik\".\n\t\tuassert.Equal(t, \"vitalik\", ResolveAddress(aliceAddr).Name())\n\t\t// Old name still resolves to alice (decision #4: keep old entry).\n\t\tres, isLatest := ResolveName(\"vital1k\")\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.False(t, isLatest)\n\t})\n}\n\nfunc TestCanonicalEntry_Persists_After_Delete(cur realm, t *testing.T) {\n\t// Decision #5: Delete does NOT remove the canonical entry. Mirrors\n\t// the existing tombstone behavior (anti-revival policy).\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\tcleanStore(t)\n\n\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\tdata := ResolveAddress(aliceAddr)\n\turequire.NoError(t, data.Delete(0, cur))\n\n\t// Canonical entry retained.\n\texisting, taken := IsCanonicalTaken(\"vitalik\")\n\turequire.True(t, taken)\n\tuassert.Equal(t, \"vitalik\", existing)\n\n\t// A different user CANNOT register a confusable variant — the deleted\n\t// user's canonical claim still stands.\n\tuassert.ErrorContains(t,\n\t\tRegisterUser(cross(cur), \"vital1k\", bobAddr),\n\t\tErrCanonicalCollision.Error(),\n\t)\n}\n\nfunc TestCanonicalEntry_Persists_After_UpdateName(cur realm, t *testing.T) {\n\t// Decision #4: UpdateName keeps the OLD canonical entry. Mirrors the\n\t// existing nameStore alias retention (anti-rename-squat policy).\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\tcleanStore(t)\n\n\turequire.NoError(t, RegisterUser(cross(cur), \"alice\", aliceAddr))\n\tdata := ResolveAddress(aliceAddr)\n\ttesting.SetOriginCaller(whitelistedCallerAddr)\n\turequire.NoError(t, data.UpdateName(0, cur, \"alice2\"))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users\"))\n\n\t// Both old and new canonical entries exist.\n\texisting, taken := IsCanonicalTaken(\"alice\")\n\turequire.True(t, taken)\n\tuassert.Equal(t, \"alice\", existing)\n\n\texisting, taken = IsCanonicalTaken(\"alice2\")\n\turequire.True(t, taken)\n\tuassert.Equal(t, \"alice2\", existing)\n}\n\n// cleanStore should not be needed, as vm store should be reset after each test.\n// Reference: https://github.com/gnolang/gno/issues/1982\nfunc cleanStore(t *testing.T) {\n\tt.Helper()\n\n\tnameStore = bptree.NewBPTree32()\n\taddressStore = bptree.NewBPTree32()\n\tcanonicalStore = bptree.NewBPTree32()\n}\n"},{"name":"users.gno","body":"package users\n\nimport \"gno.land/p/nt/bptree/v0/rotree\"\n\n// ResolveName returns the latest UserData of a specific user by name or alias\nfunc ResolveName(name string) (data *UserData, isCurrent bool) {\n\traw := nameStore.Get(name)\n\tif raw == nil {\n\t\treturn nil, false\n\t}\n\n\tdata = raw.(*UserData)\n\tif data.deleted {\n\t\treturn nil, false\n\t}\n\n\treturn data, name == data.username\n}\n\n// ResolveAddress returns the latest UserData of a specific user by address\nfunc ResolveAddress(addr address) *UserData {\n\traw := addressStore.Get(addr.String())\n\tif raw == nil {\n\t\treturn nil\n\t}\n\n\tdata := raw.(*UserData)\n\tif data.deleted {\n\t\treturn nil\n\t}\n\n\treturn data\n}\n\n// ResolveAny tries to resolve any given string to *UserData\n// If the input is not found in the registry in any form, nil is returned\nfunc ResolveAny(input string) (*UserData, bool) {\n\taddr := address(input)\n\tif addr.IsValid() {\n\t\treturn ResolveAddress(addr), true\n\t}\n\n\treturn ResolveName(input)\n}\n\n// GetReadonlyAddrStore exposes the address store in readonly mode\nfunc GetReadonlyAddrStore() *rotree.ReadOnlyTree {\n\treturn rotree.Wrap(addressStore, makeUserDataSafe)\n}\n\n// GetReadOnlyNameStore exposes the name store in readonly mode\nfunc GetReadOnlyNameStore() *rotree.ReadOnlyTree {\n\treturn rotree.Wrap(nameStore, makeUserDataSafe)\n}\n\nfunc makeUserDataSafe(data any) any {\n\tcpy := new(UserData)\n\t*cpy = *(data.(*UserData))\n\tif cpy.deleted {\n\t\treturn nil\n\t}\n\n\t// Note: when requesting data from this AVL tree, (exists bool) will be true\n\t// Even if the data is \"deleted\". This is currently unavoidable\n\treturn cpy\n}\n"},{"name":"users_test.gno","body":"package users\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestResolveName(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"single_name\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\tres, isLatest := ResolveName(alice)\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.Equal(t, alice, res.Name())\n\t\tuassert.True(t, isLatest)\n\t})\n\n\tt.Run(\"name+Alias\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata, _ := ResolveName(alice)\n\t\turequire.NoError(t, data.UpdateName(0, cur, \"alice1\"))\n\n\t\tres, isLatest := ResolveName(\"alice1\")\n\t\turequire.NotEqual(t, nil, res)\n\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.Equal(t, \"alice1\", res.Name())\n\t\tuassert.True(t, isLatest)\n\t})\n\n\tt.Run(\"multiple_aliases\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\t// RegisterUser and check each Alias\n\t\tvar names []string\n\t\tnames = append(names, alice)\n\t\tfor i := 0; i \u003c 5; i++ {\n\t\t\talias := \"alice\" + strconv.Itoa(i)\n\t\t\tnames = append(names, alias)\n\n\t\t\tdata, _ := ResolveName(alice)\n\t\t\turequire.NoError(t, data.UpdateName(0, cur, alias))\n\t\t}\n\n\t\tfor _, alias := range names {\n\t\t\tres, _ := ResolveName(alias)\n\t\t\turequire.NotEqual(t, nil, res)\n\n\t\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\t\tuassert.Equal(t, \"alice4\", res.Name())\n\t\t}\n\t})\n}\n\nfunc TestResolveAddress(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"single_name\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\tres := ResolveAddress(aliceAddr)\n\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.Equal(t, alice, res.Name())\n\t})\n\n\tt.Run(\"name+Alias\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\t\tdata, _ := ResolveName(alice)\n\t\turequire.NoError(t, data.UpdateName(0, cur, \"alice1\"))\n\n\t\tres := ResolveAddress(aliceAddr)\n\t\turequire.NotEqual(t, nil, res)\n\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.Equal(t, \"alice1\", res.Name())\n\t})\n\n\tt.Run(\"multiple_aliases\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\t// RegisterUser and check each Alias\n\t\tvar names []string\n\t\tnames = append(names, alice)\n\n\t\tfor i := 0; i \u003c 5; i++ {\n\t\t\talias := \"alice\" + strconv.Itoa(i)\n\t\t\tnames = append(names, alias)\n\t\t\tdata, _ := ResolveName(alice)\n\t\t\turequire.NoError(t, data.UpdateName(0, cur, alias))\n\t\t}\n\n\t\tres := ResolveAddress(aliceAddr)\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.Equal(t, \"alice4\", res.Name())\n\t})\n}\n\nfunc TestROStores(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\tcleanStore(t)\n\n\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\troNS := GetReadOnlyNameStore()\n\troAS := GetReadonlyAddrStore()\n\n\tt.Run(\"get user data\", func(t *testing.T) {\n\t\t// Name store\n\t\taliceDataRaw := roNS.Get(alice)\n\t\turequire.NotNil(t, aliceDataRaw)\n\n\t\troData, ok := aliceDataRaw.(*UserData)\n\t\tuassert.True(t, ok, \"Could not cast data from RO tree to UserData\")\n\n\t\t// Try to modify data\n\t\troData.Delete(0, cur)\n\t\traw := nameStore.Get(alice)\n\t\tuassert.False(t, raw.(*UserData).deleted)\n\n\t\t// Addr store\n\t\taliceDataRaw = roAS.Get(aliceAddr.String())\n\t\turequire.NotNil(t, aliceDataRaw)\n\n\t\troData, ok = aliceDataRaw.(*UserData)\n\t\tuassert.True(t, ok, \"Could not cast data from RO tree to UserData\")\n\n\t\t// Try to modify data\n\t\troData.Delete(0, cur)\n\t\traw = nameStore.Get(alice)\n\t\tuassert.False(t, raw.(*UserData).deleted)\n\t})\n\n\tt.Run(\"get deleted data\", func(t *testing.T) {\n\t\traw := nameStore.Get(alice)\n\t\taliceData := raw.(*UserData)\n\n\t\turequire.NoError(t, aliceData.Delete(0, cur))\n\t\turequire.True(t, aliceData.IsDeleted())\n\n\t\t// Should be nil because of makeSafeFn intercepting the value.\n\t\trawRoData := roNS.Get(alice)\n\t\tuassert.Equal(t, rawRoData, nil)\n\t\t_, ok := rawRoData.(*UserData) // shouldn't be castable\n\t\tuassert.False(t, ok)\n\t})\n}\n\nfunc TestResolveAny(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"name\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\tres, _ := ResolveAny(alice)\n\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.Equal(t, alice, res.Name())\n\t})\n\n\tt.Run(\"address\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.NoError(t, RegisterUser(cross(cur), alice, aliceAddr))\n\n\t\tres, _ := ResolveAny(aliceAddr.String())\n\n\t\tuassert.Equal(t, aliceAddr, res.Addr())\n\t\tuassert.Equal(t, alice, res.Name())\n\t})\n\n\tt.Run(\"not_registered\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\tres, _ := ResolveAny(aliceAddr.String())\n\n\t\tuassert.Equal(t, nil, res)\n\t})\n}\n\nfunc TestProposeErrors(cur realm, t *testing.T) {\n\tt.Run(\"propose_register_user_errors\", func(t *testing.T) {\n\t\turequire.PanicsWithMessage(t, cur, ErrInvalidUsername.Error(), func() {\n\t\t\tProposeRegisterUser(cur, \"bad name\", aliceAddr)\n\t\t})\n\t\turequire.PanicsWithMessage(t, cur, ErrInvalidAddress.Error(), func() {\n\t\t\tProposeRegisterUser(cur, alice, \"badaddress\")\n\t\t})\n\t})\n\n\tt.Run(\"propose_update_name_errors\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.PanicsWithMessage(t, cur, ErrInvalidAddress.Error(), func() {\n\t\t\tProposeUpdateName(cur, \"badaddress\", \"alice1\")\n\t\t})\n\t\turequire.PanicsWithMessage(t, cur, ErrInvalidUsername.Error(), func() {\n\t\t\tProposeUpdateName(cur, aliceAddr, \"bad name\")\n\t\t})\n\t\t// Note: unregistered user is not checked at proposal creation time.\n\t\t// The callback handles it at execution time.\n\t})\n\n\tt.Run(\"propose_delete_user_errors\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\turequire.PanicsWithMessage(t, cur, ErrInvalidAddress.Error(), func() {\n\t\t\tProposeDeleteUser(cur, \"badaddress\")\n\t\t})\n\t\t// Note: unregistered user is not checked at proposal creation time.\n\t\t// The callback handles it at execution time.\n\t})\n}\n\n// Audit finding #6: ProposeControllerAdditionAndRemoval used to return early\n// from its callback when toAdd was already whitelisted (addToWhitelist\n// returned ErrAlreadyWhitelisted), skipping the remove step entirely. A\n// passed swap proposal would silently leave the old controller active.\n//\n// applyControllerSwap is the extracted callback body. These tests exercise\n// it directly to confirm:\n//   - normal swap (toAdd new, toRemove present) succeeds and updates state\n//   - toAdd already present is benign — remove still happens\n//   - toRemove already absent is benign — add still happens, no error\n//   - errors that aren't the idempotency cases still propagate\nfunc TestApplyControllerSwap(t *testing.T) {\n\t// Use unique addresses per subtest to avoid having to drain the\n\t// controllers set (addrset.Set has no clear/iterate-all method, and\n\t// state persists across subtests in this package's tests).\n\tt.Run(\"normal swap A-\u003eB\", func(t *testing.T) {\n\t\ta := testutils.TestAddress(\"swapA1\")\n\t\tb := testutils.TestAddress(\"swapB1\")\n\t\tcontrollers.Add(a)\n\t\tdefer controllers.Remove(b)\n\n\t\tuassert.NoError(t, applyControllerSwap(b, a))\n\t\tuassert.True(t, controllers.Has(b), \"b should be whitelisted\")\n\t\tuassert.False(t, controllers.Has(a), \"a should be removed\")\n\t})\n\n\tt.Run(\"toAdd already whitelisted, toRemove present\", func(t *testing.T) {\n\t\t// Regression for audit finding #6. Before the fix, this returned\n\t\t// ErrAlreadyWhitelisted from the swap callback, skipping the remove\n\t\t// step entirely — the swap silently no-op'd and the old controller\n\t\t// stayed active.\n\t\ta := testutils.TestAddress(\"swapA2\")\n\t\tb := testutils.TestAddress(\"swapB2\")\n\t\tcontrollers.Add(a)\n\t\tcontrollers.Add(b)\n\t\tdefer controllers.Remove(b)\n\n\t\tuassert.NoError(t, applyControllerSwap(b, a))\n\t\tuassert.True(t, controllers.Has(b), \"b should remain whitelisted\")\n\t\tuassert.False(t, controllers.Has(a), \"a should be removed even though b was already in\")\n\t})\n\n\tt.Run(\"toRemove already absent, toAdd new\", func(t *testing.T) {\n\t\ta := testutils.TestAddress(\"swapA3\")\n\t\tb := testutils.TestAddress(\"swapB3\")\n\t\tc := testutils.TestAddress(\"swapC3\") // never added\n\t\tcontrollers.Add(a)\n\t\tdefer controllers.Remove(a)\n\t\tdefer controllers.Remove(b)\n\n\t\t// Remove c which isn't in the set; should be benign.\n\t\tuassert.NoError(t, applyControllerSwap(b, c))\n\t\tuassert.True(t, controllers.Has(b), \"b should be whitelisted\")\n\t\tuassert.True(t, controllers.Has(a), \"a should still be whitelisted (unchanged)\")\n\t\tuassert.False(t, controllers.Has(c), \"c was never in the set\")\n\t})\n\n\tt.Run(\"both idempotency cases at once\", func(t *testing.T) {\n\t\t// toAdd already in, toRemove already out — should succeed cleanly,\n\t\t// no state change.\n\t\ta := testutils.TestAddress(\"swapA4\")\n\t\tb := testutils.TestAddress(\"swapB4\")\n\t\tc := testutils.TestAddress(\"swapC4\")\n\t\tcontrollers.Add(a)\n\t\tcontrollers.Add(b)\n\t\tdefer controllers.Remove(a)\n\t\tdefer controllers.Remove(b)\n\n\t\tuassert.NoError(t, applyControllerSwap(b, c))\n\t\tuassert.True(t, controllers.Has(a))\n\t\tuassert.True(t, controllers.Has(b))\n\t\tuassert.False(t, controllers.Has(c))\n\t})\n}\n\n// Audit finding #20: the controller whitelist must be queryable from\n// outside the package so operators can monitor authority without\n// source-diving or replaying governance proposals.\nfunc TestControllerQueries(t *testing.T) {\n\tt.Run(\"IsController reflects whitelist state\", func(t *testing.T) {\n\t\ta := testutils.TestAddress(\"queryA1\")\n\t\tb := testutils.TestAddress(\"queryB1\")\n\n\t\t// Before any add: both report false.\n\t\tuassert.False(t, IsController(a))\n\t\tuassert.False(t, IsController(b))\n\n\t\tcontrollers.Add(a)\n\t\tdefer controllers.Remove(a)\n\n\t\tuassert.True(t, IsController(a))\n\t\tuassert.False(t, IsController(b))\n\t})\n\n\tt.Run(\"Controllers returns a snapshot of current whitelist\", func(t *testing.T) {\n\t\t// Use distinct addresses that aren't already in the set from\n\t\t// other tests in this file.\n\t\ta := testutils.TestAddress(\"queryA2\")\n\t\tb := testutils.TestAddress(\"queryB2\")\n\t\tc := testutils.TestAddress(\"queryC2\")\n\n\t\tcontrollers.Add(a)\n\t\tcontrollers.Add(b)\n\t\tcontrollers.Add(c)\n\t\tdefer controllers.Remove(a)\n\t\tdefer controllers.Remove(b)\n\t\tdefer controllers.Remove(c)\n\n\t\tgot := Controllers()\n\n\t\t// All three must appear (regardless of order vs other tests'\n\t\t// leftover entries — we only assert ours are present).\n\t\tseen := map[string]bool{}\n\t\tfor _, e := range got {\n\t\t\tseen[e.String()] = true\n\t\t}\n\t\tuassert.True(t, seen[a.String()], \"snapshot must contain a\")\n\t\tuassert.True(t, seen[b.String()], \"snapshot must contain b\")\n\t\tuassert.True(t, seen[c.String()], \"snapshot must contain c\")\n\t})\n\n\tt.Run(\"Controllers returns a copy — caller mutation does not affect realm\", func(t *testing.T) {\n\t\ta := testutils.TestAddress(\"queryA3\")\n\t\tcontrollers.Add(a)\n\t\tdefer controllers.Remove(a)\n\n\t\tgot := Controllers()\n\t\t// Mutate the returned slice. The realm's controllers set must not\n\t\t// be affected.\n\t\tgot = got[:0]\n\n\t\tuassert.True(t, controllers.Has(a), \"realm state must be unaffected by caller-side slice mutation\")\n\t\tuassert.True(t, IsController(a))\n\t})\n}\n\n// ProposeRegisterUser auto-injects a CANONICAL COLLISION warning into\n// the proposal description when the proposed name canonical-collides\n// with an existing registration. The warning surfaces the colliding\n// existing name to voters; the proposal still goes through if voted in\n// (decision #3, DAO grants always bypass).\nfunc TestProposeRegisterUser_CollisionWarning(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(initControllerPath))\n\n\tt.Run(\"warning_injected_on_collision\", func(t *testing.T) {\n\t\tcleanStore(t)\n\t\turequire.NoError(t, RegisterUser(cross(cur), \"vitalik\", aliceAddr))\n\n\t\treq := ProposeRegisterUser(cur, \"vital1k\", bobAddr)\n\t\tuassert.True(t,\n\t\t\tstrings.Contains(req.Description(), \"CANONICAL COLLISION\"),\n\t\t\t\"description must include collision warning\")\n\t\tuassert.True(t,\n\t\t\tstrings.Contains(req.Description(), \"`vitalik`\"),\n\t\t\t\"description must name the existing colliding registration\")\n\t})\n\n\tt.Run(\"no_warning_when_no_collision\", func(t *testing.T) {\n\t\tcleanStore(t)\n\n\t\treq := ProposeRegisterUser(cur, \"freshname\", aliceAddr)\n\t\tuassert.False(t,\n\t\t\tstrings.Contains(req.Description(), \"CANONICAL COLLISION\"),\n\t\t\t\"description must not include collision warning when name is fresh\")\n\t})\n}\n\n// Note: full execution of ProposeRegisterUser/ProposeUpdateName closures\n// (with ignoreCanonical=true → later-wins overwrite) is exercised end-to-\n// end by the integration test in gno.land/pkg/integration/testdata/.\n// The bypass-write semantics themselves are covered here at the unit\n// level by TestRegisterUserIgnoreCanonical and TestUpdateName_CanonicalCollision.\n\n// TODO Uncomment after gnoweb /u/ page.\n//func TestUserRenderLink(cur realm, t *testing.T) {\n//\ttesting.SetOriginCaller(whitelistedCallerAddr)\n//\tcleanStore(t)\n//\n//\turequire.NoError(t, RegisterUser(alice, aliceAddr))\n//\n//\tdata, _ := ResolveName(alice)\n//\tuassert.Equal(t, data.RenderLink(\"\"), ufmt.Sprintf(\"[@%s](/u/%s)\", alice, alice))\n//\ttext := \"my link text!\"\n//\tuassert.Equal(t, data.RenderLink(text), ufmt.Sprintf(\"[%s](/u/%s)\", text, alice))\n//}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"kpQ+HCMArk4fqX5GWO2YC162XlqDr/YFV9JqC/IaTxSCWnFewDyIrqT856M1BMOqFqr+q2KVLcXC8XwgN7xrDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"init","path":"gno.land/r/sys/users/init","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/users/init\"\ngno = \"0.9\"\n"},{"name":"init.gno","body":"// Package init provides basic user registration.\n//\n// SECURITY: every public function in this package is genesis-only. The\n// realm exists to seed initial users into r/sys/users at chain genesis\n// and to register itself as a controller. After genesis (block height\n// \u003e 0), no caller may use these functions.\n//\n// Without the genesis-only gate, the wide-open `RegisterUser` wrapper\n// would let any EOA land-grab any name (including reserved-sounding\n// names like \"administrator\" or \"vitalik\") to any address — for free,\n// without the namereg/v1 payment, blacklist, or canonical-collision\n// checks. The gate closes that bypass.\n//\n// Post-genesis user registration must go through a whitelisted\n// controller that enforces its own policy (e.g. r/sys/namereg/v1).\npackage init\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\n\t\"gno.land/r/sys/users\"\n)\n\n// Bootstrap registers this package as a controller in r/sys/users.\n// Genesis-only via AddControllerAtGenesis's own height==0 gate.\nfunc Bootstrap(cur realm) {\n\tusers.AddControllerAtGenesis(cross(cur), chain.PackageAddress(\"gno.land/r/sys/users/init\"))\n}\n\n// RegisterUser registers a new user in r/sys/users at chain genesis.\n// PANICS if called after genesis (height \u003e 0).\n//\n// Uses RegisterUserIgnoreCanonical: the genesis seed set is curated, and\n// any deliberate confusable reservations (e.g. registering both `vitalik`\n// and `vital1k` to two different addresses) must not abort chain bring-\n// up. Decision #14 (later-wins) applies, so order in genesis_txs.jsonl\n// determines which name owns the canonical pointer when stems collide.\nfunc RegisterUser(cur realm, name string, addr address) {\n\tif runtime.ChainHeight() != 0 {\n\t\tpanic(\"r/sys/users/init.RegisterUser: genesis-only; use a whitelisted controller post-genesis (e.g. r/sys/namereg/v1.Register)\")\n\t}\n\tif err := users.RegisterUserIgnoreCanonical(cross(cur), name, addr); err != nil {\n\t\tpanic(err)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"RFiAIBJ5GZTId7lCXtTR5Wedfq+iMkWjAw99WGr/KgARwBk9vWD6JeL+60RwtylXHnS6FRdMIa0eRMfJcFOTAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"boards","path":"gno.land/r/archive/boards","files":[{"name":"board.gno","body":"package boards\n\nimport (\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n//----------------------------------------\n// Board\n\ntype BoardID uint64\n\nfunc (bid BoardID) String() string {\n\treturn strconv.Itoa(int(bid))\n}\n\ntype Board struct {\n\tid        BoardID // only set for public boards.\n\turl       string\n\tname      string\n\tcreator   address\n\tthreads   avl.Tree // Post.id -\u003e *Post\n\tpostsCtr  uint64   // increments Post.id\n\tcreatedAt time.Time\n\tdeleted   avl.Tree // TODO reserved for fast-delete.\n}\n\nfunc newBoard(id BoardID, url string, name string, creator address) *Board {\n\tif !reName.MatchString(name) {\n\t\tpanic(\"invalid name: \" + name)\n\t}\n\texists := gBoardsByName.Has(name)\n\tif exists {\n\t\tpanic(\"board already exists\")\n\t}\n\treturn \u0026Board{\n\t\tid:        id,\n\t\turl:       url,\n\t\tname:      name,\n\t\tcreator:   creator,\n\t\tthreads:   avl.Tree{},\n\t\tcreatedAt: time.Now(),\n\t\tdeleted:   avl.Tree{},\n\t}\n}\n\n/* TODO support this once we figure out how to ensure URL correctness.\n// A private board is not tracked by gBoards*,\n// but must be persisted by the caller's realm.\n// Private boards have 0 id and does not ping\n// back the remote board on reposts.\nfunc NewPrivateBoard(_ realm, url string, name string, creator address) *Board {\n\treturn newBoard(0, url, name, creator)\n}\n*/\n\nfunc (board *Board) IsPrivate() bool {\n\treturn board.id == 0\n}\n\nfunc (board *Board) GetThread(pid PostID) *Post {\n\tpidkey := postIDKey(pid)\n\tpostI := board.threads.Get(pidkey)\n\tif postI == nil {\n\t\treturn nil\n\t}\n\treturn postI.(*Post)\n}\n\nfunc (board *Board) AddThread(creator address, title string, body string) *Post {\n\tpid := board.incGetPostID()\n\tpidkey := postIDKey(pid)\n\tthread := newPost(board, pid, creator, title, body, pid, 0, 0)\n\tboard.threads.Set(pidkey, thread)\n\treturn thread\n}\n\n// NOTE: this can be potentially very expensive for threads with many replies.\n// TODO: implement optional fast-delete where thread is simply moved.\nfunc (board *Board) DeleteThread(pid PostID) {\n\tpidkey := postIDKey(pid)\n\t_, removed := board.threads.Remove(pidkey)\n\tif !removed {\n\t\tpanic(\"thread does not exist with id \" + pid.String())\n\t}\n}\n\nfunc (board *Board) HasPermission(addr address, perm Permission) bool {\n\tif board.creator == addr {\n\t\tswitch perm {\n\t\tcase EditPermission:\n\t\t\treturn true\n\t\tcase DeletePermission:\n\t\t\treturn true\n\t\tdefault:\n\t\t\treturn false\n\t\t}\n\t}\n\treturn false\n}\n\n// Renders the board for display suitable as plaintext in\n// console.  This is suitable for demonstration or tests,\n// but not for prod.\nfunc (board *Board) RenderBoard() string {\n\tstr := \"\"\n\tstr += \"\\\\[[post](\" + board.GetPostFormURL() + \")]\\n\\n\"\n\tif board.threads.Size() \u003e 0 {\n\t\tboard.threads.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tif str != \"\" {\n\t\t\t\tstr += \"----------------------------------------\\n\"\n\t\t\t}\n\t\t\tstr += value.(*Post).RenderSummary() + \"\\n\"\n\t\t\treturn false\n\t\t})\n\t}\n\treturn str\n}\n\nfunc (board *Board) incGetPostID() PostID {\n\tboard.postsCtr++\n\treturn PostID(board.postsCtr)\n}\n\nfunc (board *Board) GetURLFromThreadAndReplyID(threadID, replyID PostID) string {\n\tif replyID == 0 {\n\t\treturn board.url + \"/\" + threadID.String()\n\t} else {\n\t\treturn board.url + \"/\" + threadID.String() + \"/\" + replyID.String()\n\t}\n}\n\nfunc (board *Board) GetPostFormURL() string {\n\treturn gRealmLink.Call(\"CreateThread\", \"bid\", board.id.String())\n}\n"},{"name":"boards.gno","body":"package boards\n\nimport (\n\t\"regexp\"\n\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n//----------------------------------------\n// Realm (package) state\n\nvar (\n\tgRealmLink      = txlink.Realm(\"gno.land/r/archive/boards\")\n\tgBoards         avl.Tree    // id -\u003e *Board\n\tgBoardsCtr      int         // increments Board.id\n\tgBoardsByName   avl.Tree    // name -\u003e *Board\n\tgDefaultAnonFee = 100000000 // minimum fee required if anonymous\n)\n\n//----------------------------------------\n// Constants\n\nvar reName = regexp.MustCompile(`^[a-z]+[_a-z0-9]{2,29}$`)\n"},{"name":"example_post.md","body":"Hey all! 👋\n\nThis is my first post in this land!\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/boards\"\ngno = \"0.9\"\n"},{"name":"misc.gno","body":"package boards\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/r/sys/users\"\n)\n\n//----------------------------------------\n// private utility methods\n// XXX ensure these cannot be called from public.\n\nfunc getBoard(bid BoardID) *Board {\n\tbidkey := boardIDKey(bid)\n\tboard_ := gBoards.Get(bidkey)\n\tif board_ == nil {\n\t\treturn nil\n\t}\n\tboard := board_.(*Board)\n\treturn board\n}\n\nfunc incGetBoardID() BoardID {\n\tgBoardsCtr++\n\treturn BoardID(gBoardsCtr)\n}\n\nfunc padLeft(str string, length int) string {\n\tif len(str) \u003e= length {\n\t\treturn str\n\t} else {\n\t\treturn strings.Repeat(\" \", length-len(str)) + str\n\t}\n}\n\nfunc padZero(u64 uint64, length int) string {\n\tstr := strconv.Itoa(int(u64))\n\tif len(str) \u003e= length {\n\t\treturn str\n\t} else {\n\t\treturn strings.Repeat(\"0\", length-len(str)) + str\n\t}\n}\n\nfunc boardIDKey(bid BoardID) string {\n\treturn padZero(uint64(bid), 10)\n}\n\nfunc postIDKey(pid PostID) string {\n\treturn padZero(uint64(pid), 10)\n}\n\nfunc indentBody(indent string, body string) string {\n\tlines := strings.Split(body, \"\\n\")\n\tres := \"\"\n\tfor i, line := range lines {\n\t\tif i \u003e 0 {\n\t\t\tres += \"\\n\"\n\t\t}\n\t\tres += indent + line\n\t}\n\treturn res\n}\n\n// NOTE: length must be greater than 3.\nfunc summaryOf(str string, length int) string {\n\tlines := strings.SplitN(str, \"\\n\", 2)\n\tline := lines[0]\n\tif len(line) \u003e length {\n\t\tline = line[:(length-3)] + \"...\"\n\t} else if len(lines) \u003e 1 {\n\t\t// len(line) \u003c= 80\n\t\tline = line + \"...\"\n\t}\n\treturn line\n}\n\nfunc displayAddressMD(addr address) string {\n\tuser := users.ResolveAddress(addr)\n\tif user == nil {\n\t\treturn \"[\" + addr.String() + \"](/u/\" + addr.String() + \")\"\n\t} else {\n\t\treturn \"[@\" + user.Name() + \"](/u/\" + user.Name() + \")\"\n\t}\n}\n\nfunc usernameOf(addr address) string {\n\tuser := users.ResolveAddress(addr)\n\tif user == nil {\n\t\treturn \"\"\n\t}\n\treturn user.Name()\n}\n"},{"name":"post.gno","body":"package boards\n\nimport (\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n//----------------------------------------\n// Post\n\n// NOTE: a PostID is relative to the board.\ntype PostID uint64\n\nfunc (pid PostID) String() string {\n\treturn strconv.Itoa(int(pid))\n}\n\n// A Post is a \"thread\" or a \"reply\" depending on context.\n// A thread is a Post of a Board that holds other replies.\ntype Post struct {\n\tboard       *Board\n\tid          PostID\n\tcreator     address\n\ttitle       string // optional\n\tbody        string\n\treplies     avl.Tree // Post.id -\u003e *Post\n\trepliesAll  avl.Tree // Post.id -\u003e *Post (all replies, for top-level posts)\n\treposts     avl.Tree // Board.id -\u003e Post.id\n\tthreadID    PostID   // original Post.id\n\tparentID    PostID   // parent Post.id (if reply or repost)\n\trepostBoard BoardID  // original Board.id (if repost)\n\tcreatedAt   time.Time\n\tupdatedAt   time.Time\n}\n\nfunc newPost(board *Board, id PostID, creator address, title, body string, threadID, parentID PostID, repostBoard BoardID) *Post {\n\treturn \u0026Post{\n\t\tboard:       board,\n\t\tid:          id,\n\t\tcreator:     creator,\n\t\ttitle:       title,\n\t\tbody:        body,\n\t\treplies:     avl.Tree{},\n\t\trepliesAll:  avl.Tree{},\n\t\treposts:     avl.Tree{},\n\t\tthreadID:    threadID,\n\t\tparentID:    parentID,\n\t\trepostBoard: repostBoard,\n\t\tcreatedAt:   time.Now(),\n\t}\n}\n\nfunc (post *Post) IsThread() bool {\n\treturn post.parentID == 0\n}\n\nfunc (post *Post) GetPostID() PostID {\n\treturn post.id\n}\n\nfunc (post *Post) AddReply(creator address, body string) *Post {\n\tboard := post.board\n\tpid := board.incGetPostID()\n\tpidkey := postIDKey(pid)\n\treply := newPost(board, pid, creator, \"\", body, post.threadID, post.id, 0)\n\tpost.replies.Set(pidkey, reply)\n\tif post.threadID == post.id {\n\t\tpost.repliesAll.Set(pidkey, reply)\n\t} else {\n\t\tthread := board.GetThread(post.threadID)\n\t\tthread.repliesAll.Set(pidkey, reply)\n\t}\n\treturn reply\n}\n\nfunc (post *Post) Update(title string, body string) {\n\tpost.title = title\n\tpost.body = body\n\tpost.updatedAt = time.Now()\n}\n\nfunc (thread *Post) GetReply(pid PostID) *Post {\n\tpidkey := postIDKey(pid)\n\treplyI := thread.repliesAll.Get(pidkey)\n\tif replyI == nil {\n\t\treturn nil\n\t} else {\n\t\treturn replyI.(*Post)\n\t}\n}\n\nfunc (post *Post) AddRepostTo(creator address, title, body string, dst *Board) *Post {\n\tif !post.IsThread() {\n\t\tpanic(\"cannot repost non-thread post\")\n\t}\n\tpid := dst.incGetPostID()\n\tpidkey := postIDKey(pid)\n\trepost := newPost(dst, pid, creator, title, body, pid, post.id, post.board.id)\n\tdst.threads.Set(pidkey, repost)\n\tif !dst.IsPrivate() {\n\t\tbidkey := boardIDKey(dst.id)\n\t\tpost.reposts.Set(bidkey, pid)\n\t}\n\treturn repost\n}\n\nfunc (thread *Post) DeletePost(pid PostID) {\n\tif thread.id == pid {\n\t\tpanic(\"should not happen\")\n\t}\n\tpidkey := postIDKey(pid)\n\tpostI, removed := thread.repliesAll.Remove(pidkey)\n\tif !removed {\n\t\tpanic(\"post not found in thread\")\n\t}\n\tpost := postI.(*Post)\n\tif post.parentID != thread.id {\n\t\tparent := thread.GetReply(post.parentID)\n\t\tparent.replies.Remove(pidkey)\n\t} else {\n\t\tthread.replies.Remove(pidkey)\n\t}\n}\n\nfunc (post *Post) HasPermission(addr address, perm Permission) bool {\n\tif post.creator == addr {\n\t\tswitch perm {\n\t\tcase EditPermission:\n\t\t\treturn true\n\t\tcase DeletePermission:\n\t\t\treturn true\n\t\tdefault:\n\t\t\treturn false\n\t\t}\n\t}\n\t// post notes inherit permissions of the board.\n\treturn post.board.HasPermission(addr, perm)\n}\n\nfunc (post *Post) GetSummary() string {\n\treturn summaryOf(post.body, 80)\n}\n\nfunc (post *Post) GetURL() string {\n\tif post.IsThread() {\n\t\treturn post.board.GetURLFromThreadAndReplyID(\n\t\t\tpost.id, 0)\n\t} else {\n\t\treturn post.board.GetURLFromThreadAndReplyID(\n\t\t\tpost.threadID, post.id)\n\t}\n}\n\nfunc (post *Post) GetReplyFormURL() string {\n\treturn gRealmLink.Call(\"CreateReply\",\n\t\t\"bid\", post.board.id.String(),\n\t\t\"threadid\", post.threadID.String(),\n\t\t\"postid\", post.id.String(),\n\t)\n}\n\nfunc (post *Post) GetRepostFormURL() string {\n\treturn gRealmLink.Call(\"CreateRepost\",\n\t\t\"bid\", post.board.id.String(),\n\t\t\"postid\", post.id.String(),\n\t)\n}\n\nfunc (post *Post) GetDeleteFormURL() string {\n\treturn gRealmLink.Call(\"DeletePost\",\n\t\t\"bid\", post.board.id.String(),\n\t\t\"threadid\", post.threadID.String(),\n\t\t\"postid\", post.id.String(),\n\t)\n}\n\nfunc (post *Post) RenderSummary() string {\n\tif post.repostBoard != 0 {\n\t\tdstBoard := getBoard(post.repostBoard)\n\t\tif dstBoard == nil {\n\t\t\tpanic(\"repostBoard does not exist\")\n\t\t}\n\t\tthread := dstBoard.GetThread(PostID(post.parentID))\n\t\tif thread == nil {\n\t\t\treturn \"reposted post does not exist\"\n\t\t}\n\t\treturn \"Repost: \" + post.GetSummary() + \"\\n\" + thread.RenderSummary()\n\t}\n\tstr := \"\"\n\tif post.title != \"\" {\n\t\tstr += \"## [\" + summaryOf(post.title, 80) + \"](\" + post.GetURL() + \")\\n\"\n\t\tstr += \"\\n\"\n\t}\n\tstr += post.GetSummary() + \"\\n\"\n\tstr += \"\\\\- \" + displayAddressMD(post.creator) + \",\"\n\tstr += \" [\" + post.createdAt.Format(\"2006-01-02 3:04pm MST\") + \"](\" + post.GetURL() + \")\"\n\tstr += \" \\\\[[x](\" + post.GetDeleteFormURL() + \")]\"\n\tstr += \" (\" + strconv.Itoa(post.replies.Size()) + \" replies)\"\n\tstr += \" (\" + strconv.Itoa(post.reposts.Size()) + \" reposts)\" + \"\\n\"\n\treturn str\n}\n\nfunc (post *Post) RenderPost(indent string, levels int) string {\n\tif post == nil {\n\t\treturn \"nil post\"\n\t}\n\tstr := \"\"\n\tif post.title != \"\" {\n\t\tstr += indent + \"# \" + post.title + \"\\n\"\n\t\tstr += indent + \"\\n\"\n\t}\n\tstr += indentBody(indent, post.body) + \"\\n\" // TODO: indent body lines.\n\tstr += indent + \"\\\\- \" + displayAddressMD(post.creator) + \", \"\n\tstr += \"[\" + post.createdAt.Format(\"2006-01-02 3:04pm (MST)\") + \"](\" + post.GetURL() + \")\"\n\tstr += \" \\\\[[reply](\" + post.GetReplyFormURL() + \")]\"\n\tif post.IsThread() {\n\t\tstr += \" \\\\[[repost](\" + post.GetRepostFormURL() + \")]\"\n\t}\n\tstr += \" \\\\[[x](\" + post.GetDeleteFormURL() + \")]\\n\"\n\tif levels \u003e 0 {\n\t\tif post.replies.Size() \u003e 0 {\n\t\t\tpost.replies.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\t\tstr += indent + \"\\n\"\n\t\t\t\tstr += value.(*Post).RenderPost(indent+\"\u003e \", levels-1)\n\t\t\t\treturn false\n\t\t\t})\n\t\t}\n\t} else {\n\t\tif post.replies.Size() \u003e 0 {\n\t\t\tstr += indent + \"\\n\"\n\t\t\tstr += indent + \"_[see all \" + strconv.Itoa(post.replies.Size()) + \" replies](\" + post.GetURL() + \")_\\n\"\n\t\t}\n\t}\n\treturn str\n}\n\n// render reply and link to context thread\nfunc (post *Post) RenderInner() string {\n\tif post.IsThread() {\n\t\tpanic(\"unexpected thread\")\n\t}\n\tthreadID := post.threadID\n\t// replyID := post.id\n\tparentID := post.parentID\n\tstr := \"\"\n\tstr += \"_[see thread](\" + post.board.GetURLFromThreadAndReplyID(\n\t\tthreadID, 0) + \")_\\n\\n\"\n\tthread := post.board.GetThread(post.threadID)\n\tvar parent *Post\n\tif thread.id == parentID {\n\t\tparent = thread\n\t} else {\n\t\tparent = thread.GetReply(parentID)\n\t}\n\tstr += parent.RenderPost(\"\", 0)\n\tstr += \"\\n\"\n\tstr += post.RenderPost(\"\u003e \", 5)\n\treturn str\n}\n"},{"name":"public.gno","body":"package boards\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"strconv\"\n)\n\n//----------------------------------------\n// Public facing functions\n\nfunc GetBoardIDFromName(name string) (BoardID, bool) {\n\tboardI := gBoardsByName.Get(name)\n\tif boardI == nil {\n\t\treturn 0, false\n\t}\n\treturn boardI.(*Board).id, true\n}\n\nfunc CreateBoard(cur realm, name string) BoardID {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\tbid := incGetBoardID()\n\tcaller := unsafe.OriginCaller()\n\tif usernameOf(caller) == \"\" {\n\t\tpanic(\"unauthorized\")\n\t}\n\turl := \"/r/archive/boards:\" + name\n\tboard := newBoard(bid, url, name, caller)\n\tbidkey := boardIDKey(bid)\n\tgBoards.Set(bidkey, board)\n\tgBoardsByName.Set(name, board)\n\treturn board.id\n}\n\n// checkAnonFee reads unsafe.OriginSend() to verify the anonymous-posting\n// fee was attached to the tx. Callers MUST also assert\n// cur.Previous().IsUserCall() before calling — see\n// docs/resources/effective-gno.md#verifying-inbound-coin-payments.\nfunc checkAnonFee() bool {\n\tsent := unsafe.OriginSend()\n\tanonFeeCoin := chain.NewCoin(\"ugnot\", int64(gDefaultAnonFee))\n\tif len(sent) == 1 \u0026\u0026 sent[0].IsGTE(anonFeeCoin) {\n\t\treturn true\n\t}\n\treturn false\n}\n\nfunc CreateThread(cur realm, bid BoardID, title string, body string) PostID {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\tif usernameOf(caller) == \"\" {\n\t\tif !checkAnonFee() {\n\t\t\tpanic(\"please register, otherwise minimum fee \" + strconv.Itoa(gDefaultAnonFee) + \" is required if anonymous\")\n\t\t}\n\t}\n\tboard := getBoard(bid)\n\tif board == nil {\n\t\tpanic(\"board not exist\")\n\t}\n\tthread := board.AddThread(caller, title, body)\n\treturn thread.id\n}\n\nfunc CreateReply(cur realm, bid BoardID, threadid, postid PostID, body string) PostID {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\tif usernameOf(caller) == \"\" {\n\t\tif !checkAnonFee() {\n\t\t\tpanic(\"please register, otherwise minimum fee \" + strconv.Itoa(gDefaultAnonFee) + \" is required if anonymous\")\n\t\t}\n\t}\n\tboard := getBoard(bid)\n\tif board == nil {\n\t\tpanic(\"board not exist\")\n\t}\n\tthread := board.GetThread(threadid)\n\tif thread == nil {\n\t\tpanic(\"thread not exist\")\n\t}\n\tif postid == threadid {\n\t\treply := thread.AddReply(caller, body)\n\t\treturn reply.id\n\t} else {\n\t\tpost := thread.GetReply(postid)\n\t\treply := post.AddReply(caller, body)\n\t\treturn reply.id\n\t}\n}\n\n// If dstBoard is private, does not ping back.\n// If board specified by bid is private, panics.\nfunc CreateRepost(cur realm, bid BoardID, postid PostID, title string, body string, dstBoardID BoardID) PostID {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\tif usernameOf(caller) == \"\" {\n\t\t// TODO: allow with gDefaultAnonFee payment.\n\t\tif !checkAnonFee() {\n\t\t\tpanic(\"please register, otherwise minimum fee \" + strconv.Itoa(gDefaultAnonFee) + \" is required if anonymous\")\n\t\t}\n\t}\n\tboard := getBoard(bid)\n\tif board == nil {\n\t\tpanic(\"src board not exist\")\n\t}\n\tif board.IsPrivate() {\n\t\tpanic(\"cannot repost from a private board\")\n\t}\n\tdst := getBoard(dstBoardID)\n\tif dst == nil {\n\t\tpanic(\"dst board not exist\")\n\t}\n\tthread := board.GetThread(postid)\n\tif thread == nil {\n\t\tpanic(\"thread not exist\")\n\t}\n\trepost := thread.AddRepostTo(caller, title, body, dst)\n\treturn repost.id\n}\n\nfunc DeletePost(cur realm, bid BoardID, threadid, postid PostID, reason string) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\tboard := getBoard(bid)\n\tif board == nil {\n\t\tpanic(\"board not exist\")\n\t}\n\tthread := board.GetThread(threadid)\n\tif thread == nil {\n\t\tpanic(\"thread not exist\")\n\t}\n\tif postid == threadid {\n\t\t// delete thread\n\t\tif !thread.HasPermission(caller, DeletePermission) {\n\t\t\tpanic(\"unauthorized\")\n\t\t}\n\t\tboard.DeleteThread(threadid)\n\t} else {\n\t\t// delete thread's post\n\t\tpost := thread.GetReply(postid)\n\t\tif post == nil {\n\t\t\tpanic(\"post not exist\")\n\t\t}\n\t\tif !post.HasPermission(caller, DeletePermission) {\n\t\t\tpanic(\"unauthorized\")\n\t\t}\n\t\tthread.DeletePost(postid)\n\t}\n}\n\nfunc EditPost(cur realm, bid BoardID, threadid, postid PostID, title, body string) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\tboard := getBoard(bid)\n\tif board == nil {\n\t\tpanic(\"board not exist\")\n\t}\n\tthread := board.GetThread(threadid)\n\tif thread == nil {\n\t\tpanic(\"thread not exist\")\n\t}\n\tif postid == threadid {\n\t\t// edit thread\n\t\tif !thread.HasPermission(caller, EditPermission) {\n\t\t\tpanic(\"unauthorized\")\n\t\t}\n\t\tthread.Update(title, body)\n\t} else {\n\t\t// edit thread's post\n\t\tpost := thread.GetReply(postid)\n\t\tif post == nil {\n\t\t\tpanic(\"post not exist\")\n\t\t}\n\t\tif !post.HasPermission(caller, EditPermission) {\n\t\t\tpanic(\"unauthorized\")\n\t\t}\n\t\tpost.Update(title, body)\n\t}\n}\n"},{"name":"render.gno","body":"package boards\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n)\n\n//----------------------------------------\n// Render functions\n\nfunc RenderBoard(bid BoardID) string {\n\tboard := getBoard(bid)\n\tif board == nil {\n\t\treturn \"missing board\"\n\t}\n\treturn board.RenderBoard()\n}\n\nfunc Render(path string) string {\n\tif path == \"\" {\n\t\tstr := \"These are all the boards of this realm:\\n\\n\"\n\t\tgBoards.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tboard := value.(*Board)\n\t\t\tstr += \" * [\" + board.url + \"](\" + board.url + \")\\n\"\n\t\t\treturn false\n\t\t})\n\t\treturn str\n\t}\n\tparts := strings.Split(path, \"/\")\n\tif len(parts) == 1 {\n\t\t// /r/archive/boards:BOARD_NAME\n\t\tname := parts[0]\n\t\tboardI := gBoardsByName.Get(name)\n\t\tif boardI == nil {\n\t\t\treturn \"board does not exist: \" + name\n\t\t}\n\t\treturn boardI.(*Board).RenderBoard()\n\t} else if len(parts) == 2 {\n\t\t// /r/archive/boards:BOARD_NAME/THREAD_ID\n\t\tname := parts[0]\n\t\tboardI := gBoardsByName.Get(name)\n\t\tif boardI == nil {\n\t\t\treturn \"board does not exist: \" + name\n\t\t}\n\t\tpid, err := strconv.Atoi(parts[1])\n\t\tif err != nil {\n\t\t\treturn \"invalid thread id: \" + parts[1]\n\t\t}\n\t\tboard := boardI.(*Board)\n\t\tthread := board.GetThread(PostID(pid))\n\t\tif thread == nil {\n\t\t\treturn \"thread does not exist with id: \" + parts[1]\n\t\t}\n\t\treturn thread.RenderPost(\"\", 5)\n\t} else if len(parts) == 3 {\n\t\t// /r/archive/boards:BOARD_NAME/THREAD_ID/REPLY_ID\n\t\tname := parts[0]\n\t\tboardI := gBoardsByName.Get(name)\n\t\tif boardI == nil {\n\t\t\treturn \"board does not exist: \" + name\n\t\t}\n\t\tpid, err := strconv.Atoi(parts[1])\n\t\tif err != nil {\n\t\t\treturn \"invalid thread id: \" + parts[1]\n\t\t}\n\t\tboard := boardI.(*Board)\n\t\tthread := board.GetThread(PostID(pid))\n\t\tif thread == nil {\n\t\t\treturn \"thread does not exist with id: \" + parts[1]\n\t\t}\n\t\trid, err := strconv.Atoi(parts[2])\n\t\tif err != nil {\n\t\t\treturn \"invalid reply id: \" + parts[2]\n\t\t}\n\t\treply := thread.GetReply(PostID(rid))\n\t\tif reply == nil {\n\t\t\treturn \"reply does not exist with id: \" + parts[2]\n\t\t}\n\t\treturn reply.RenderInner()\n\t} else {\n\t\treturn \"unrecognized path \" + path\n\t}\n}\n"},{"name":"role.gno","body":"package boards\n\ntype Permission string\n\nconst (\n\tDeletePermission Permission = \"role:delete\"\n\tEditPermission   Permission = \"role:edit\"\n)\n"},{"name":"z_0_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_0_a\npackage z_0_a\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/boards\"\n)\n\nvar bid boards.BoardID\n\nfunc init(cur realm) {\n\tcaller := testutils.TestAddress(\"caller\")\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid := boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n\tboards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"test_board\"))\n}\n\n// Error:\n// unauthorized\n"},{"name":"z_0_c_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_0_c\npackage z_0_c\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar bid boards.BoardID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tboards.CreateThread(cross(cur), 1, \"First Post (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"test_board\"))\n}\n\n// Error:\n// board not exist\n"},{"name":"z_0_d_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_0_d\npackage z_0_d\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar bid boards.BoardID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateReply(cross(cur), bid, 0, 0, \"Reply of the second post\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"test_board\"))\n}\n\n// Error:\n// thread not exist\n"},{"name":"z_0_e_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_0_e\npackage z_0_e\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar bid boards.BoardID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tboards.CreateReply(cross(cur), bid, 0, 0, \"Reply of the second post\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"test_board\"))\n}\n\n// Error:\n// board not exist\n"},{"name":"z_0_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_0_filetest\n\npackage z_0_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar bid boards.BoardID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid := boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n\tboards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"test_board\"))\n}\n\n// Output:\n// \\[[post](/r/archive/boards$help\u0026func=CreateThread\u0026bid=1)]\n//\n// ----------------------------------------\n// ## [First Post (title)](/r/archive/boards:test_board/1)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm UTC](/r/archive/boards:test_board/1) \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)] (0 replies) (0 reposts)\n//\n// ----------------------------------------\n// ## [Second Post (title)](/r/archive/boards:test_board/2)\n//\n// Body of the second post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm UTC](/r/archive/boards:test_board/2) \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=2)] (1 replies) (0 reposts)\n//\n//\n"},{"name":"z_10_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_10_a_filetest\n\npackage z_10_a_filetest\n\n// SEND: 1000000ugnot\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\t// boardId 2 not exist\n\tboards.DeletePost(cross(cur), 2, pid, pid, \"\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Error:\n// invalid non-user call\n"},{"name":"z_10_b_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_10_b_filetest\n\npackage z_10_b_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\t// pid of 2 not exist\n\tboards.DeletePost(cross(cur), bid, 2, 2, \"\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Error:\n// invalid non-user call\n"},{"name":"z_10_c_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_10_c_filetest\n\npackage z_10_c_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n\trid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n\trid = boards.CreateReply(cross(cur), bid, pid, pid, \"First reply of the First post\\n\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\tboards.DeletePost(cross(cur), bid, pid, rid, \"\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # First Post in (title)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n// \u003e First reply of the First post\n// \u003e\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=1)]\n//\n// ----------------------------------------------------\n// # First Post in (title)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n"},{"name":"z_10_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_10_filetest\n\npackage z_10_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\tboards.DeletePost(cross(cur), bid, pid, pid, \"\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # First Post in (title)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n// ----------------------------------------------------\n// thread does not exist with id: 1\n"},{"name":"z_11_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_11_a_filetest\n\npackage z_11_a_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\t// board 2 not exist\n\tboards.EditPost(cross(cur), 2, pid, pid, \"Edited: First Post in (title)\", \"Edited: Body of the first post. (body)\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Error:\n// invalid non-user call\n"},{"name":"z_11_b_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_11_b_filetest\n\npackage z_11_b_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\t// thread 2 not exist\n\tboards.EditPost(cross(cur), bid, 2, pid, \"Edited: First Post in (title)\", \"Edited: Body of the first post. (body)\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Error:\n// invalid non-user call\n"},{"name":"z_11_c_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_11_c_filetest\n\npackage z_11_c_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\t// post 2 not exist\n\tboards.EditPost(cross(cur), bid, pid, 2, \"Edited: First Post in (title)\", \"Edited: Body of the first post. (body)\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Error:\n// invalid non-user call\n"},{"name":"z_11_d_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_11_d_filetest\n\npackage z_11_d_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n\trid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n\trid = boards.CreateReply(cross(cur), bid, pid, pid, \"First reply of the First post\\n\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\tboards.EditPost(cross(cur), bid, pid, rid, \"\", \"Edited: First reply of the First post\\n\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # First Post in (title)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n// \u003e First reply of the First post\n// \u003e\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=1)]\n//\n// ----------------------------------------------------\n// # First Post in (title)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n// \u003e Edited: First reply of the First post\n// \u003e\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=1)]\n//\n"},{"name":"z_11_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_11_filetest\n\npackage z_11_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tpid = boards.CreateThread(cross(cur), bid, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\tboards.EditPost(cross(cur), bid, pid, pid, \"Edited: First Post in (title)\", \"Edited: Body of the first post. (body)\")\n\tprintln(\"----------------------------------------------------\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # First Post in (title)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n// ----------------------------------------------------\n// # Edited: First Post in (title)\n//\n// Edited: Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n"},{"name":"z_12_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_12_a_filetest\n\npackage z_12_a_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\t// create a post via registered user\n\tbid1 := boards.CreateBoard(cross(cur), \"test_board1\")\n\tpid := boards.CreateThread(cross(cur), bid1, \"First Post (title)\", \"Body of the first post. (body)\")\n\tbid2 := boards.CreateBoard(cross(cur), \"test_board2\")\n\n\t// create a repost via anon user\n\ttest2 := testutils.TestAddress(\"test2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 9000000}})\n\n\trid := boards.CreateRepost(cross(cur), bid1, pid, \"\", \"Check this out\", bid2)\n\tprintln(rid)\n\tprintln(boards.Render(\"test_board1\"))\n}\n\n// Error:\n// please register, otherwise minimum fee 100000000 is required if anonymous\n"},{"name":"z_12_b_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_12_b_filetest\n\npackage z_12_b_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tbid1 := boards.CreateBoard(cross(cur), \"test_board1\")\n\tpid := boards.CreateThread(cross(cur), bid1, \"First Post (title)\", \"Body of the first post. (body)\")\n\tbid2 := boards.CreateBoard(cross(cur), \"test_board2\")\n\n\t// create a repost to a non-existing board\n\trid := boards.CreateRepost(cross(cur), 5, pid, \"\", \"Check this out\", bid2)\n\tprintln(rid)\n\tprintln(boards.Render(\"test_board1\"))\n}\n\n// Error:\n// src board not exist\n"},{"name":"z_12_c_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_12_c_filetest\n\npackage z_12_c_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tbid1 := boards.CreateBoard(cross(cur), \"test_board1\")\n\tboards.CreateThread(cross(cur), bid1, \"First Post (title)\", \"Body of the first post. (body)\")\n\tbid2 := boards.CreateBoard(cross(cur), \"test_board2\")\n\n\t// create a repost to a non-existing thread\n\trid := boards.CreateRepost(cross(cur), bid1, 5, \"\", \"Check this out\", bid2)\n\tprintln(rid)\n\tprintln(boards.Render(\"test_board1\"))\n}\n\n// Error:\n// thread not exist\n"},{"name":"z_12_d_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_12_d_filetest\n\npackage z_12_d_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tbid1 := boards.CreateBoard(cross(cur), \"test_board1\")\n\tpid := boards.CreateThread(cross(cur), bid1, \"First Post (title)\", \"Body of the first post. (body)\")\n\tboards.CreateBoard(cross(cur), \"test_board2\")\n\n\t// create a repost to a non-existing destination board\n\trid := boards.CreateRepost(cross(cur), bid1, pid, \"\", \"Check this out\", 5)\n\tprintln(rid)\n\tprintln(boards.Render(\"test_board1\"))\n}\n\n// Error:\n// dst board not exist\n"},{"name":"z_12_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_12_filetest\n\npackage z_12_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid1 boards.BoardID\n\tbid2 boards.BoardID\n\tpid  boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid1 = boards.CreateBoard(cross(cur), \"test_board1\")\n\tpid = boards.CreateThread(cross(cur), bid1, \"First Post (title)\", \"Body of the first post. (body)\")\n\tbid2 = boards.CreateBoard(cross(cur), \"test_board2\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\trid := boards.CreateRepost(cross(cur), bid1, pid, \"\", \"Check this out\", bid2)\n\tprintln(rid)\n\tprintln(boards.Render(\"test_board2\"))\n}\n\n// Output:\n// 1\n// \\[[post](/r/archive/boards$help\u0026func=CreateThread\u0026bid=2)]\n//\n// ----------------------------------------\n// Repost: Check this out\n// ## [First Post (title)](/r/archive/boards:test_board1/1)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm UTC](/r/archive/boards:test_board1/1) \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)] (0 replies) (1 reposts)\n//\n//\n"},{"name":"z_1_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_1_filetest\n\npackage z_1_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar board *boards.Board\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\t_ = boards.CreateBoard(cross(cur), \"test_board_1\")\n\t_ = boards.CreateBoard(cross(cur), \"test_board_2\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"\"))\n}\n\n// Output:\n// These are all the boards of this realm:\n//\n//  * [/r/archive/boards:test_board_1](/r/archive/boards:test_board_1)\n//  * [/r/archive/boards:test_board_2](/r/archive/boards:test_board_2)\n//\n"},{"name":"z_2_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_2_filetest\n\npackage z_2_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid = boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n\tboards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # Second Post (title)\n//\n// Body of the second post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=2)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=2)]\n//\n// \u003e Reply of the second post\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/3) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=3\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=3\u0026threadid=2)]\n//\n"},{"name":"z_3_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_3_filetest\n\npackage z_3_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid = boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\trid := boards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n\tprintln(rid)\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// 3\n// # Second Post (title)\n//\n// Body of the second post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=2)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=2)]\n//\n// \u003e Reply of the second post\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/3) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=3\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=3\u0026threadid=2)]\n//\n"},{"name":"z_4_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_4_filetest\n\npackage z_4_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid = boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n\trid := boards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n\tprintln(rid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\trid2 := boards.CreateReply(cross(cur), bid, pid, pid, \"Second reply of the second post\")\n\tprintln(rid2)\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// 3\n// 4\n// # Second Post (title)\n//\n// Body of the second post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=2)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=2)]\n//\n// \u003e Reply of the second post\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/3) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=3\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=3\u0026threadid=2)]\n//\n// \u003e Second reply of the second post\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/4) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=4\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=4\u0026threadid=2)]\n//\n"},{"name":"z_5_b_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_5_b_filetest\n\npackage z_5_b_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nconst admin = address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\t// create board via registered user\n\tbid := boards.CreateBoard(cross(cur), \"test_board\")\n\n\t// create post via anon user\n\ttest2 := testutils.TestAddress(\"test2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 9000000}})\n\n\tpid := boards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Error:\n// please register, otherwise minimum fee 100000000 is required if anonymous\n"},{"name":"z_5_c_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_5_c_filetest\n\npackage z_5_c_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nconst admin = address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\t// create board via registered user\n\tbid := boards.CreateBoard(cross(cur), \"test_board\")\n\n\t// create post via anon user\n\ttest2 := testutils.TestAddress(\"test2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 101000000}})\n\n\tpid := boards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tboards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the first post\")\n\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # First Post (title)\n//\n// Body of the first post. (body)\n// \\- [g1w3jhxapjta047h6lta047h6lta047h6laqcyu4](/u/g1w3jhxapjta047h6lta047h6lta047h6laqcyu4), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=1\u0026threadid=1)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)]\n//\n// \u003e Reply of the first post\n// \u003e \\- [g1w3jhxapjta047h6lta047h6lta047h6laqcyu4](/u/g1w3jhxapjta047h6lta047h6lta047h6laqcyu4), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/1/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=1)]\n//\n"},{"name":"z_5_d_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_5_d_filetest\n\npackage z_5_d_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nconst admin = address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\t// create board via registered user\n\tbid := boards.CreateBoard(cross(cur), \"test_board\")\n\tpid := boards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\n\t// create reply via anon user\n\ttest2 := testutils.TestAddress(\"test2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 9000000}})\n\tboards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the first post\")\n\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Error:\n// please register, otherwise minimum fee 100000000 is required if anonymous\n"},{"name":"z_5_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_5_filetest\n\npackage z_5_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid = boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n\t_ = boards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\t_ = boards.CreateReply(cross(cur), bid, pid, pid, \"Second reply of the second post\\n\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # Second Post (title)\n//\n// Body of the second post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=2)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=2)]\n//\n// \u003e Reply of the second post\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/3) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=3\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=3\u0026threadid=2)]\n//\n// \u003e Second reply of the second post\n// \u003e\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/4) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=4\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=4\u0026threadid=2)]\n//\n"},{"name":"z_6_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_6_filetest\n\npackage z_6_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n\trid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid = boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n\trid = boards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\tboards.CreateReply(cross(cur), bid, pid, pid, \"Second reply of the second post\\n\")\n\tboards.CreateReply(cross(cur), bid, pid, rid, \"First reply of the first reply\\n\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # Second Post (title)\n//\n// Body of the second post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=2\u0026threadid=2)] \\[[repost](/r/archive/boards$help\u0026func=CreateRepost\u0026bid=1\u0026postid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=2\u0026threadid=2)]\n//\n// \u003e Reply of the second post\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/3) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=3\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=3\u0026threadid=2)]\n// \u003e\n// \u003e \u003e First reply of the first reply\n// \u003e \u003e\n// \u003e \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/5) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=5\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=5\u0026threadid=2)]\n//\n// \u003e Second reply of the second post\n// \u003e\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/4) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=4\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=4\u0026threadid=2)]\n//\n"},{"name":"z_7_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_7_filetest\n\npackage z_7_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nfunc init(cur realm) {\n\t// register\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\t// create board and post\n\tbid := boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main() {\n\tprintln(boards.Render(\"test_board\"))\n}\n\n// Output:\n// \\[[post](/r/archive/boards$help\u0026func=CreateThread\u0026bid=1)]\n//\n// ----------------------------------------\n// ## [First Post (title)](/r/archive/boards:test_board/1)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm UTC](/r/archive/boards:test_board/1) \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=1\u0026threadid=1)] (0 replies) (0 reposts)\n//\n//\n"},{"name":"z_8_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_8_filetest\n\npackage z_8_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tbid boards.BoardID\n\tpid boards.PostID\n\trid boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tbid = boards.CreateBoard(cross(cur), \"test_board\")\n\tboards.CreateThread(cross(cur), bid, \"First Post (title)\", \"Body of the first post. (body)\")\n\tpid = boards.CreateThread(cross(cur), bid, \"Second Post (title)\", \"Body of the second post. (body)\")\n\trid = boards.CreateReply(cross(cur), bid, pid, pid, \"Reply of the second post\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\")))\n\tboards.CreateReply(cross(cur), bid, pid, pid, \"Second reply of the second post\\n\")\n\trid2 := boards.CreateReply(cross(cur), bid, pid, rid, \"First reply of the first reply\\n\")\n\tprintln(boards.Render(\"test_board/\" + strconv.Itoa(int(pid)) + \"/\" + strconv.Itoa(int(rid2))))\n}\n\n// Output:\n// _[see thread](/r/archive/boards:test_board/2)_\n//\n// Reply of the second post\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/3) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=3\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=3\u0026threadid=2)]\n//\n// _[see all 1 replies](/r/archive/boards:test_board/2/3)_\n//\n// \u003e First reply of the first reply\n// \u003e\n// \u003e \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:test_board/2/5) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=1\u0026postid=5\u0026threadid=2)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=1\u0026postid=5\u0026threadid=2)]\n//\n"},{"name":"z_9_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_9_a_filetest\n\npackage z_9_a_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar dstBoard boards.BoardID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tdstBoard = boards.CreateBoard(cross(cur), \"dst_board\")\n}\n\nfunc main(cur realm) {\n\tboards.CreateRepost(cross(cur), 0, 0, \"First Post in (title)\", \"Body of the first post. (body)\", dstBoard)\n}\n\n// Error:\n// invalid non-user call\n"},{"name":"z_9_b_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_9_b_filetest\n\npackage z_9_b_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tsrcBoard boards.BoardID\n\tpid      boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tsrcBoard = boards.CreateBoard(cross(cur), \"first_board\")\n\tpid = boards.CreateThread(cross(cur), srcBoard, \"First Post in (title)\", \"Body of the first post. (body)\")\n}\n\nfunc main(cur realm) {\n\tboards.CreateRepost(cross(cur), srcBoard, pid, \"First Post in (title)\", \"Body of the first post. (body)\", 0)\n}\n\n// Error:\n// invalid non-user call\n"},{"name":"z_9_filetest.gno","body":"// PKGPATH: gno.land/r/archive/boards/filetests/z_9_filetest\n\npackage z_9_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/boards\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar (\n\tfirstBoard  boards.BoardID\n\tsecondBoard boards.BoardID\n\tpid         boards.PostID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\n\tfirstBoard = boards.CreateBoard(cross(cur), \"first_board\")\n\tsecondBoard = boards.CreateBoard(cross(cur), \"second_board\")\n\tpid = boards.CreateThread(cross(cur), firstBoard, \"First Post in (title)\", \"Body of the first post. (body)\")\n\n\tboards.CreateRepost(cross(cur), firstBoard, pid, \"First Post in (title)\", \"Body of the first post. (body)\", secondBoard)\n}\n\nfunc main() {\n\tprintln(boards.Render(\"second_board/\" + strconv.Itoa(int(pid))))\n}\n\n// Output:\n// # First Post in (title)\n//\n// Body of the first post. (body)\n// \\- [@gnouser123](/u/gnouser123), [2009-02-13 11:31pm (UTC)](/r/archive/boards:second_board/1/1) \\[[reply](/r/archive/boards$help\u0026func=CreateReply\u0026bid=2\u0026postid=1\u0026threadid=1)] \\[[x](/r/archive/boards$help\u0026func=DeletePost\u0026bid=2\u0026postid=1\u0026threadid=1)]\n//\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"FLfntuELY1S0sgWLJdSSYOgP8E/qcd+06rtVkSxPhiG50BKUcgARP8VP8G2bc3JfdKFlGLsEh3VDoAl3nTb+BQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"echo","path":"gno.land/r/archive/echo","files":[{"name":"echo.gno","body":"package echo\n\n/*\n * This realm echoes the `path` argument it received.\n * Can be used by developers as a simple endpoint to test\n * forbidden characters, for pentesting or simply to\n * test it works.\n *\n * See also r/demo/print (to print various thing like user address)\n */\nfunc Render(path string) string {\n\treturn path\n}\n"},{"name":"echo_test.gno","body":"package echo\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc Test(t *testing.T) {\n\turequire.Equal(t, \"aa\", Render(\"aa\"))\n\turequire.Equal(t, \"\", Render(\"\"))\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/echo\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"jrVZTcSIZth5neDoJ3G0kFmXoUX59dVhCvaqW/hPoCeWd1U0snaXd0Je4k6i3YEN9zI7rAC0ZR3PiTw+BllACA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"closuretest","path":"gno.land/r/demo/closuretest","files":[{"name":"closuretest.gno","body":"package closuretest\n\nimport \"strconv\"\n\nvar (\n\tcount   int\n\tstepper func() int\n)\n\nvar (\n\taccumulator func(int)\n\thistory     []int\n)\n\nfunc init() {\n\tstep := 3\n\tstepper = func() int {\n\t\tcount += step\n\t\treturn count\n\t}\n\n\tmaxLen := 10\n\thistory = make([]int, 0, maxLen)\n\taccumulator = func(val int) {\n\t\tif len(history) \u003c maxLen {\n\t\t\thistory = append(history, val)\n\t\t}\n\t}\n}\n\nfunc Step() string {\n\tresult := stepper()\n\treturn \"count=\" + strconv.Itoa(result)\n}\n\nfunc Accumulate(val int) string {\n\taccumulator(val)\n\treturn \"history length=\" + strconv.Itoa(len(history))\n}\n\nfunc Render(_ string) string {\n\treturn \"closuretest: count=\" + strconv.Itoa(count) + \" history=\" + strconv.Itoa(len(history))\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/closuretest\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ftCu+Abe7s6Okmf6VAt25kt79sGro6sKy1RVUdARJW8nohHpd0N8USbQ6FW+PwGNHoT8L+ZwO5T9zD6xQoh0Aw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"counter","path":"gno.land/r/demo/counter","files":[{"name":"counter.gno","body":"package counter\n\nimport \"strconv\"\n\nvar counter int\n\nfunc Increment(_ realm) int {\n\tcounter++\n\treturn counter\n}\n\nfunc Render(_ string) string {\n\treturn strconv.Itoa(counter)\n}\n"},{"name":"counter_test.gno","body":"package counter\n\nimport \"testing\"\n\nfunc TestIncrement(cur realm, t *testing.T) {\n\tcounter = 0\n\tval := Increment(cross(cur))\n\tif val != 1 {\n\t\tt.Fatalf(\"result from Increment(): %d != 1\", val)\n\t}\n\tif counter != val {\n\t\tt.Fatalf(\"counter (%d) != val (%d)\", counter, val)\n\t}\n}\n\nfunc TestRender(t *testing.T) {\n\tcounter = 1337\n\tres := Render(\"\")\n\tif res != \"1337\" {\n\t\tt.Fatalf(\"render result %q != %q\", res, \"1337\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/counter\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"hXMAdoSom7IhHgprh4Zt2H0M8Ivc47kxJbkY0jyUFAy4VEtLpRAYkXXxTlegCQQTONCtBB8K3r4Gpl/I8SThDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"test20","path":"gno.land/r/tests/vm/test20","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/test20\"\ngno = \"0.9\"\n"},{"name":"test20.gno","body":"// Package test20 implements a deliberately insecure ERC20 token for testing purposes.\n// The Test20 token allows anyone to mint any amount of tokens to any address, making\n// it unsuitable for production use. The primary goal of this package is to facilitate\n// testing and experimentation without any security measures or restrictions.\n//\n//\tWARNING: This token is highly insecure and should not be used in any\n//\t production environment. It is intended solely for testing and\n//\t educational purposes.\npackage test20\n\nimport (\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n)\n\nvar (\n\tToken         *grc20.Token\n\tPrivateLedger *grc20.PrivateLedger\n)\n\nfunc init(cur realm) {\n\tToken, PrivateLedger = grc20.NewToken(0, cur, \"Test20\", \"TST\", 4)\n\tgrc20reg.Register(cross(cur), Token, \"\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Bw3x76AcqTjTSw4f6MGzwuO2Nkb4jMnXdgEesLpExNinOQKPm9N6FdCr/ZtpEKC38TH/DRQUNU98Icy/UVioBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"atomicswap","path":"gno.land/r/demo/defi/atomicswap","files":[{"name":"atomicswap.gno","body":"// Package atomicswap implements a hash time-locked contract (HTLC) for atomic swaps\n// between native coins (ugnot) or GRC20 tokens.\n//\n// An atomic swap allows two parties to exchange assets in a trustless way, where\n// either both transfers happen or neither does. The process works as follows:\n//\n//  1. Alice wants to swap with Bob. She generates a secret and creates a swap with\n//     Bob's address and the hash of the secret (hashlock).\n//\n//  2. Bob can claim the assets by providing the correct secret before the timelock expires.\n//     The secret proves Bob knows the preimage of the hashlock.\n//\n// 3. If Bob doesn't claim in time, Alice can refund the assets back to herself.\n//\n// Example usage for native coins:\n//\n//\t// Alice creates a swap with 1000ugnot for Bob\n//\tsecret := \"mysecret\"\n//\thashlock := hex.EncodeToString(sha256.Sum256([]byte(secret)))\n//\tid, _ := atomicswap.NewCoinSwap(bobAddr, hashlock) // -send 1000ugnot\n//\n//\t// Bob claims the swap by providing the secret\n//\tatomicswap.Claim(id, \"mysecret\")\n//\n// Example usage for GRC20 tokens:\n//\n//\t// Alice approves the swap contract to spend her tokens\n//\ttoken.Approve(swapAddr, 1000)\n//\n//\t// Alice creates a swap with 1000 tokens for Bob\n//\tid, _ := atomicswap.NewGRC20Swap(bobAddr, hashlock, \"gno.land/r/demo/token\")\n//\n//\t// Bob claims the swap by providing the secret\n//\tatomicswap.Claim(id, \"mysecret\")\n//\n// If Bob doesn't claim in time (default 1 week), Alice can refund:\n//\n//\tatomicswap.Refund(id)\npackage atomicswap\n\nimport (\n\t\"chain/banker\"\n\t\"chain/runtime/unsafe\"\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n)\n\nconst defaultTimelockDuration = 7 * 24 * time.Hour // 1w\n\nvar (\n\tswaps   avl.Tree // id -\u003e *Swap\n\tcounter int\n)\n\n// NewCoinSwap creates a new atomic swap contract for native coins.\n// It uses a default timelock duration.\nfunc NewCoinSwap(cur realm, recipient address, hashlock string) (int, *Swap) {\n\ttimelock := time.Now().Add(defaultTimelockDuration)\n\treturn NewCustomCoinSwap(cur, recipient, hashlock, timelock)\n}\n\n// NewGRC20Swap creates a new atomic swap contract for grc20 tokens.\n// It uses gno.land/r/demo/defi/grc20reg to lookup for a registered token.\nfunc NewGRC20Swap(cur realm, recipient address, hashlock string, tokenRegistryKey string) (int, *Swap) {\n\ttimelock := time.Now().Add(defaultTimelockDuration)\n\ttoken := grc20reg.MustGet(tokenRegistryKey)\n\treturn NewCustomGRC20Swap(cur, recipient, hashlock, timelock, token)\n}\n\n// NewCoinSwapWithTimelock creates a new atomic swap contract for native coin.\n// It allows specifying a custom timelock duration.\n//\n// Only direct user-call (maketx call) is accepted: unsafe.OriginSend()\n// describes a real receipt at this realm only when the caller is a pure\n// EOA. Intermediate code realms or `maketx run` ephemeral realms can\n// attach -send to the tx but spend the envelope elsewhere, leaving\n// OriginSend() describing a phantom payment that would let the swap\n// drain the realm's pre-existing balance on Claim.\nfunc NewCustomCoinSwap(cur realm, recipient address, hashlock string, timelock time.Time) (int, *Swap) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\tsender := cur.Previous().Address()\n\tsent := unsafe.OriginSend()\n\trequire(len(sent) != 0, \"at least one coin needs to be sent\")\n\n\t// Create the swap\n\tsendFn := func(cur realm, to address) {\n\t\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\t\tpkgAddr := cur.Address()\n\t\tbanker_.SendCoins(pkgAddr, to, sent)\n\t}\n\tamountStr := sent.String()\n\tswap := newSwap(sender, recipient, hashlock, timelock, amountStr, sendFn)\n\n\tcounter++\n\tid := strconv.Itoa(counter)\n\tswaps.Set(id, swap)\n\treturn counter, swap\n}\n\n// NewCustomGRC20Swap creates a new atomic swap contract for grc20 tokens.\n// It is not callable with `gnokey maketx call`, but can be imported by another contract or `gnokey maketx run`.\nfunc NewCustomGRC20Swap(cur realm, recipient address, hashlock string, timelock time.Time, token *grc20.Token) (int, *Swap) {\n\tsender := cur.Previous().Address()\n\tcurAddr := cur.Address()\n\n\tallowance := token.Allowance(sender, curAddr)\n\trequire(allowance \u003e 0, \"no allowance\")\n\n\tuserTeller := token.RealmTeller(0, cur)\n\terr := userTeller.TransferFrom(0, cur, sender, curAddr, allowance)\n\trequire(err == nil, \"cannot retrieve tokens from allowance\")\n\n\tamountStr := ufmt.Sprintf(\"%d%s\", allowance, token.GetSymbol())\n\tsendFn := func(cur realm, to address) {\n\t\terr := userTeller.Transfer(0, cur, to, allowance)\n\t\trequire(err == nil, \"cannot transfer tokens\")\n\t}\n\n\tswap := newSwap(sender, recipient, hashlock, timelock, amountStr, sendFn)\n\n\tcounter++\n\tid := strconv.Itoa(counter)\n\tswaps.Set(id, swap)\n\n\treturn counter, swap\n}\n\n// Claim loads a registered swap and tries to claim it.\nfunc Claim(cur realm, id int, secret string) {\n\tswap := mustGet(id)\n\tswap.Claim(0, cur, secret)\n}\n\n// Refund loads a registered swap and tries to refund it.\nfunc Refund(cur realm, id int) {\n\tswap := mustGet(id)\n\tswap.Refund(0, cur)\n}\n\n// Render returns a list of swaps (simplified) for the homepage, and swap details when specifying a swap ID.\nfunc Render(path string) string {\n\tif path == \"\" { // home\n\t\toutput := \"\"\n\t\tsize := swaps.Size()\n\t\tmax := 10\n\t\tswaps.ReverseIterateByOffset(size-max, max, func(key string, value any) bool {\n\t\t\tswap := value.(*Swap)\n\t\t\toutput += ufmt.Sprintf(\"- %s: %s -(%s)\u003e %s - %s\\n\",\n\t\t\t\tkey, swap.sender, swap.amountStr, swap.recipient, swap.Status())\n\t\t\treturn false\n\t\t})\n\t\treturn output\n\t} else { // by id\n\t\tswap := swaps.Get(path)\n\t\tif swap == nil {\n\t\t\treturn \"404\"\n\t\t}\n\t\treturn swap.(*Swap).String()\n\t}\n}\n\n// require checks a condition and panics with a message if the condition is false.\nfunc require(check bool, msg string) {\n\tif !check {\n\t\tpanic(msg)\n\t}\n}\n\n// mustGet retrieves a swap by its id or panics.\nfunc mustGet(id int) *Swap {\n\tkey := strconv.Itoa(id)\n\tswap := swaps.Get(key)\n\tif swap == nil {\n\t\tpanic(\"unknown swap ID\")\n\t}\n\treturn swap.(*Swap)\n}\n"},{"name":"atomicswap_test.gno","body":"package atomicswap\n\nimport (\n\t\"chain\"\n\t\"crypto/sha256\"\n\t\"encoding/hex\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/r/tests/vm/test20\"\n)\n\nfunc TestNewCustomCoinSwap_Claim(cur realm, t *testing.T) {\n\tdefer resetTestState()\n\n\t// Setup\n\tpkgAddr := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\tsender := testutils.TestAddress(\"sender1\")\n\trecipient := testutils.TestAddress(\"recipient1\")\n\tamount := chain.Coins{{Denom: \"ugnot\", Amount: 1}}\n\thashlock := sha256.Sum256([]byte(\"secret\"))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\ttimelock := time.Now().Add(1 * time.Hour)\n\ttesting.IssueCoins(pkgAddr, chain.Coins{{\"ugnot\", 100000000}})\n\n\t// Create a new swap\n\ttesting.SetRealm(testing.NewUserRealm(sender))\n\ttesting.SetOriginSend(amount)\n\tid, swap := NewCustomCoinSwap(cross(cur), recipient, hashlockHex, timelock)\n\tuassert.Equal(t, 1, id)\n\n\texpected := `- status: active\n- sender: g1wdjkuer9wgc47h6lta047h6lta047h6l56jtjc\n- recipient: g1wfjkx6tsd9jkuap3ta047h6lta047h6lkk20gv\n- amount: 1ugnot\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-14T00:31:30Z\n- remaining: 1h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n\n\t// Test initial state\n\tuassert.Equal(t, sender, swap.sender, \"expected sender to match\")\n\tuassert.Equal(t, recipient, swap.recipient, \"expected recipient to match\")\n\tuassert.Equal(t, swap.amountStr, amount.String(), \"expected amount to match\")\n\tuassert.Equal(t, hashlockHex, swap.hashlock, \"expected hashlock to match\")\n\tuassert.True(t, swap.timelock.Equal(timelock), \"expected timelock to match\")\n\tuassert.False(t, swap.claimed, \"expected claimed to be false\")\n\tuassert.False(t, swap.refunded, \"expected refunded to be false\")\n\n\t// Test claim — recipient calls via the public wrapper.\n\ttesting.SetRealm(testing.NewUserRealm(recipient))\n\tuassert.AbortsWithMessage(t, cur, \"invalid preimage\", func() { Claim(cross(cur), id, \"invalid\") })\n\tClaim(cross(cur), id, \"secret\")\n\tuassert.True(t, swap.claimed, \"expected claimed to be true\")\n\n\t// Test refund (should fail because already claimed)\n\tuassert.AbortsWithMessage(t, cur, \"already claimed\", func() { Refund(cross(cur), id) })\n\tuassert.AbortsWithMessage(t, cur, \"already claimed\", func() { Claim(cross(cur), id, \"secret\") })\n\n\texpected = `- status: claimed\n- sender: g1wdjkuer9wgc47h6lta047h6lta047h6l56jtjc\n- recipient: g1wfjkx6tsd9jkuap3ta047h6lta047h6lkk20gv\n- amount: 1ugnot\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-14T00:31:30Z\n- remaining: 1h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n}\n\nfunc TestNewCustomCoinSwap_Refund(cur realm, t *testing.T) {\n\tdefer resetTestState()\n\n\t// Setup\n\tpkgAddr := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\tsender := testutils.TestAddress(\"sender2\")\n\trecipient := testutils.TestAddress(\"recipient2\")\n\tamount := chain.Coins{{Denom: \"ugnot\", Amount: 1}}\n\thashlock := sha256.Sum256([]byte(\"secret\"))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\ttimelock := time.Now().Add(1 * time.Hour)\n\n\t// Create a new swap\n\ttesting.SetRealm(testing.NewUserRealm(sender))\n\ttesting.SetOriginSend(amount)\n\tid, swap := NewCustomCoinSwap(cross(cur), recipient, hashlockHex, timelock)\n\tuassert.Equal(t, 1, id)\n\n\texpected := `- status: active\n- sender: g1wdjkuer9wge97h6lta047h6lta047h6ltfacad\n- recipient: g1wfjkx6tsd9jkuapjta047h6lta047h6lducc3v\n- amount: 1ugnot\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-14T00:31:30Z\n- remaining: 1h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n\n\t// Test Refund — sender calls via the public wrapper.\n\ttesting.IssueCoins(pkgAddr, chain.Coins{{\"ugnot\", 100000000}})\n\tuassert.AbortsWithMessage(t, cur, \"timelock not expired\", func() { Refund(cross(cur), id) })\n\tswap.timelock = time.Now().Add(-1 * time.Hour) // override timelock\n\tRefund(cross(cur), id)\n\tuassert.True(t, swap.refunded, \"expected refunded to be true\")\n\n\texpected = `- status: refunded\n- sender: g1wdjkuer9wge97h6lta047h6lta047h6ltfacad\n- recipient: g1wfjkx6tsd9jkuapjta047h6lta047h6lducc3v\n- amount: 1ugnot\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-13T22:31:30Z\n- remaining: 0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n}\n\nfunc TestNewCustomGRC20Swap_Claim(cur realm, t *testing.T) {\n\tdefer resetTestState()\n\n\t// Setup\n\tsender := testutils.TestAddress(\"sender3\")\n\trecipient := testutils.TestAddress(\"recipient3\")\n\trlm := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\thashlock := sha256.Sum256([]byte(\"secret\"))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\ttimelock := time.Now().Add(1 * time.Hour)\n\n\ttest20.PrivateLedger.Mint(sender, 100_000)\n\ttest20.PrivateLedger.Approve(sender, rlm, 70_000)\n\n\t// Create a new swap\n\ttesting.SetRealm(testing.NewUserRealm(sender))\n\tid, swap := NewCustomGRC20Swap(cross(cur), recipient, hashlockHex, timelock, test20.Token)\n\tuassert.Equal(t, 1, id)\n\n\texpected := `- status: active\n- sender: g1wdjkuer9wge47h6lta047h6lta047h6l5rk38l\n- recipient: g1wfjkx6tsd9jkuapnta047h6lta047h6ly6k4pv\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-14T00:31:30Z\n- remaining: 1h0m0s`\n\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n\n\t// Test initial state\n\tuassert.Equal(t, sender, swap.sender, \"expected sender to match\")\n\tuassert.Equal(t, recipient, swap.recipient, \"expected recipient to match\")\n\tbal := test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, bal, int64(30_000))\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, bal, int64(70_000))\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, bal, int64(0))\n\n\tuassert.Equal(t, hashlockHex, swap.hashlock, \"expected hashlock to match\")\n\tuassert.True(t, swap.timelock.Equal(timelock), \"expected timelock to match\")\n\tuassert.False(t, swap.claimed, \"expected claimed to be false\")\n\tuassert.False(t, swap.refunded, \"expected refunded to be false\")\n\n\t// Test claim\n\ttesting.SetRealm(testing.NewUserRealm(recipient))\n\tuassert.AbortsWithMessage(t, cur, \"invalid preimage\", func() { Claim(cross(cur), id, \"invalid\") })\n\tClaim(cross(cur), id, \"secret\")\n\tuassert.True(t, swap.claimed, \"expected claimed to be true\")\n\n\tbal = test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, bal, int64(30_000))\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, bal, int64(0))\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, bal, int64(70_000))\n\n\t// Test refund (should fail because already claimed)\n\tuassert.AbortsWithMessage(t, cur, \"already claimed\", func() { Refund(cross(cur), id) })\n\tuassert.AbortsWithMessage(t, cur, \"already claimed\", func() { Claim(cross(cur), id, \"secret\") })\n\n\texpected = `- status: claimed\n- sender: g1wdjkuer9wge47h6lta047h6lta047h6l5rk38l\n- recipient: g1wfjkx6tsd9jkuapnta047h6lta047h6ly6k4pv\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-14T00:31:30Z\n- remaining: 1h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n}\n\nfunc TestNewCustomGRC20Swap_Refund(cur realm, t *testing.T) {\n\tdefer resetTestState()\n\n\t// Setup\n\tpkgAddr := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\tsender := testutils.TestAddress(\"sender5\")\n\trecipient := testutils.TestAddress(\"recipient5\")\n\trlm := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\thashlock := sha256.Sum256([]byte(\"secret\"))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\ttimelock := time.Now().Add(1 * time.Hour)\n\n\ttest20.PrivateLedger.Mint(sender, 100_000)\n\ttest20.PrivateLedger.Approve(sender, rlm, 70_000)\n\n\t// Create a new swap\n\ttesting.SetRealm(testing.NewUserRealm(sender))\n\tid, swap := NewCustomGRC20Swap(cross(cur), recipient, hashlockHex, timelock, test20.Token)\n\tuassert.Equal(t, 1, id)\n\n\texpected := `- status: active\n- sender: g1wdjkuer9wg647h6lta047h6lta047h6l5p6k3k\n- recipient: g1wfjkx6tsd9jkuap4ta047h6lta047h6lmwmj6v\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-14T00:31:30Z\n- remaining: 1h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n\n\t// Test initial state\n\tuassert.Equal(t, sender, swap.sender, \"expected sender to match\")\n\tuassert.Equal(t, recipient, swap.recipient, \"expected recipient to match\")\n\tbal := test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, bal, int64(30_000))\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, bal, int64(70_000))\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, bal, int64(0))\n\n\t// Test Refund — sender is still the current realm.\n\ttesting.IssueCoins(pkgAddr, chain.Coins{{\"ugnot\", 100000000}})\n\tuassert.AbortsWithMessage(t, cur, \"timelock not expired\", func() { Refund(cross(cur), id) })\n\n\tswap.timelock = time.Now().Add(-1 * time.Hour) // override timelock\n\tRefund(cross(cur), id)\n\tuassert.True(t, swap.refunded, \"expected refunded to be true\")\n\n\tbal = test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, bal, int64(100_000))\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, bal, int64(0))\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, bal, int64(0))\n\n\texpected = `- status: refunded\n- sender: g1wdjkuer9wg647h6lta047h6lta047h6l5p6k3k\n- recipient: g1wfjkx6tsd9jkuap4ta047h6lta047h6lmwmj6v\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-13T22:31:30Z\n- remaining: 0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n}\n\nfunc TestNewGRC20Swap_Claim(cur realm, t *testing.T) {\n\tdefer resetTestState()\n\n\t// Setup\n\tsender := testutils.TestAddress(\"sender4\")\n\trecipient := testutils.TestAddress(\"recipient4\")\n\trlm := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\thashlock := sha256.Sum256([]byte(\"secret\"))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\ttimelock := time.Now().Add(defaultTimelockDuration)\n\n\ttest20.PrivateLedger.Mint(sender, 100_000)\n\ttest20.PrivateLedger.Approve(sender, rlm, 70_000)\n\n\t// Create a new swap\n\ttesting.SetRealm(testing.NewUserRealm(sender))\n\tid, swap := NewGRC20Swap(cross(cur), recipient, hashlockHex, \"gno.land/r/tests/vm/test20\")\n\tuassert.Equal(t, 1, id)\n\n\texpected := `- status: active\n- sender: g1wdjkuer9wg697h6lta047h6lta047h6ltt3lty\n- recipient: g1wfjkx6tsd9jkuap5ta047h6lta047h6ljg4l2v\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-20T23:31:30Z\n- remaining: 168h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n\n\t// Test initial state\n\tuassert.Equal(t, sender, swap.sender, \"expected sender to match\")\n\tuassert.Equal(t, recipient, swap.recipient, \"expected recipient to match\")\n\tbal := test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, bal, int64(30_000))\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, bal, int64(70_000))\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, bal, int64(0))\n\n\tuassert.Equal(t, hashlockHex, swap.hashlock, \"expected hashlock to match\")\n\tuassert.True(t, swap.timelock.Equal(timelock), \"expected timelock to match\")\n\tuassert.False(t, swap.claimed, \"expected claimed to be false\")\n\tuassert.False(t, swap.refunded, \"expected refunded to be false\")\n\n\t// Test claim\n\ttesting.SetRealm(testing.NewUserRealm(recipient))\n\tuassert.AbortsWithMessage(t, cur, \"invalid preimage\", func() { Claim(cross(cur), id, \"invalid\") })\n\tClaim(cross(cur), id, \"secret\")\n\tuassert.True(t, swap.claimed, \"expected claimed to be true\")\n\n\tbal = test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, int64(30_000), bal)\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, int64(0), bal)\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, int64(70_000), bal)\n\n\t// Test refund (should fail because already claimed)\n\tuassert.AbortsWithMessage(t, cur, \"already claimed\", func() { Refund(cross(cur), id) })\n\tuassert.AbortsWithMessage(t, cur, \"already claimed\", func() { Claim(cross(cur), id, \"secret\") })\n\n\texpected = `- status: claimed\n- sender: g1wdjkuer9wg697h6lta047h6lta047h6ltt3lty\n- recipient: g1wfjkx6tsd9jkuap5ta047h6lta047h6ljg4l2v\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-20T23:31:30Z\n- remaining: 168h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n}\n\nfunc TestNewGRC20Swap_Refund(cur realm, t *testing.T) {\n\tdefer resetTestState()\n\n\t// Setup\n\tpkgAddr := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\tsender := testutils.TestAddress(\"sender6\")\n\trecipient := testutils.TestAddress(\"recipient6\")\n\trlm := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\thashlock := sha256.Sum256([]byte(\"secret\"))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\n\ttest20.PrivateLedger.Mint(sender, 100_000)\n\ttest20.PrivateLedger.Approve(sender, rlm, 70_000)\n\n\t// Create a new swap\n\ttesting.SetRealm(testing.NewUserRealm(sender))\n\tid, swap := NewGRC20Swap(cross(cur), recipient, hashlockHex, \"gno.land/r/tests/vm/test20\")\n\tuassert.Equal(t, 1, id)\n\n\texpected := `- status: active\n- sender: g1wdjkuer9wgm97h6lta047h6lta047h6ltj497r\n- recipient: g1wfjkx6tsd9jkuapkta047h6lta047h6lqyf9rv\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-20T23:31:30Z\n- remaining: 168h0m0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n\n\t// Test initial state\n\tuassert.Equal(t, sender, swap.sender, \"expected sender to match\")\n\tuassert.Equal(t, recipient, swap.recipient, \"expected recipient to match\")\n\tbal := test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, bal, int64(30_000))\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, bal, int64(70_000))\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, bal, int64(0))\n\n\t// Test Refund — sender is still the current realm.\n\ttesting.IssueCoins(pkgAddr, chain.Coins{{\"ugnot\", 100000000}})\n\tuassert.AbortsWithMessage(t, cur, \"timelock not expired\", func() { Refund(cross(cur), id) })\n\n\tswap.timelock = time.Now().Add(-1 * time.Hour) // override timelock\n\tRefund(cross(cur), id)\n\tuassert.True(t, swap.refunded, \"expected refunded to be true\")\n\n\tbal = test20.Token.BalanceOf(sender)\n\tuassert.Equal(t, bal, int64(100_000))\n\tbal = test20.Token.BalanceOf(rlm)\n\tuassert.Equal(t, bal, int64(0))\n\tbal = test20.Token.BalanceOf(recipient)\n\tuassert.Equal(t, bal, int64(0))\n\n\texpected = `- status: refunded\n- sender: g1wdjkuer9wgm97h6lta047h6lta047h6ltj497r\n- recipient: g1wfjkx6tsd9jkuapkta047h6lta047h6lqyf9rv\n- amount: 70000TST\n- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b\n- timelock: 2009-02-13T22:31:30Z\n- remaining: 0s`\n\tuassert.Equal(t, expected, swap.String())\n\tuassert.Equal(t, expected, Render(\"1\"))\n}\n\nfunc TestRender(cur realm, t *testing.T) {\n\tdefer resetTestState()\n\n\t// Setup\n\talice := testutils.TestAddress(\"alice\")\n\tbob := testutils.TestAddress(\"bob\")\n\tcharly := testutils.TestAddress(\"charly\")\n\trlm := chain.PackageAddress(\"gno.land/r/demo/defi/atomicswap\")\n\thashlock := sha256.Sum256([]byte(\"secret\"))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\ttimelock := time.Now().Add(1 * time.Hour)\n\n\ttest20.PrivateLedger.Mint(alice, 100_000)\n\t// SetRealm mutates the test's cur in place to look like alice's\n\t// userRealm. cur's HIV pointer is unchanged so IsCurrent still passes;\n\t// RealmTeller(0, cur) binds to alice via cur.Address().\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\tuserTeller := test20.Token.RealmTeller(0, cur)\n\tuserTeller.Approve(0, cur, rlm, 10_000)\n\tbobSwapID, _ := NewCustomGRC20Swap(cross(cur), bob, hashlockHex, timelock, test20.Token)\n\n\tuserTeller.Approve(0, cur, rlm, 20_000)\n\t_, _ = NewCustomGRC20Swap(cross(cur), charly, hashlockHex, timelock, test20.Token)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tClaim(cross(cur), bobSwapID, \"secret\")\n\texpected := `- 2: g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh -(20000TST)\u003e g1vd5xzunv09047h6lta047h6lta047h6lhsyveh - active\n- 1: g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh -(10000TST)\u003e g1vfhkyh6lta047h6lta047h6lta047h6l03vdhu - claimed\n`\n\tuassert.Equal(t, expected, Render(\"\"))\n}\n\nfunc resetTestState() {\n\tswaps = avl.Tree{}\n\tcounter = 0\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/defi/atomicswap\"\ngno = \"0.9\"\n"},{"name":"swap.gno","body":"package atomicswap\n\nimport (\n\t\"crypto/sha256\"\n\t\"encoding/hex\"\n\t\"time\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Swap represents an atomic swap contract.\ntype Swap struct {\n\tsender    address\n\trecipient address\n\thashlock  string\n\ttimelock  time.Time\n\tclaimed   bool\n\trefunded  bool\n\tamountStr string\n\tsendFn    func(cur realm, to address)\n}\n\nfunc newSwap(\n\tsender address,\n\trecipient address,\n\thashlock string,\n\ttimelock time.Time,\n\tamountStr string,\n\tsendFn func(realm, address),\n) *Swap {\n\trequire(time.Now().Before(timelock), \"timelock must be in the future\")\n\trequire(hashlock != \"\", \"hashlock must not be empty\")\n\treturn \u0026Swap{\n\t\trecipient: recipient,\n\t\tsender:    sender,\n\t\thashlock:  hashlock,\n\t\ttimelock:  timelock,\n\t\tclaimed:   false,\n\t\trefunded:  false,\n\t\tsendFn:    sendFn,\n\t\tamountStr: amountStr,\n\t}\n}\n\n// Claim allows the recipient to claim the funds if they provide the correct preimage.\n// rlm is the cur of the surrounding crossing wrapper; rlm.Previous() is\n// the immediate caller of that wrapper, against which we authorize.\nfunc (s *Swap) Claim(_ int, rlm realm, preimage string) {\n\trequire(rlm.IsCurrent(), \"unauthorized: rlm is not the caller's live cur\")\n\trequire(!s.claimed, \"already claimed\")\n\trequire(!s.refunded, \"already refunded\")\n\trequire(rlm.Previous().Address() == s.recipient, \"unauthorized\")\n\n\thashlock := sha256.Sum256([]byte(preimage))\n\thashlockHex := hex.EncodeToString(hashlock[:])\n\trequire(hashlockHex == s.hashlock, \"invalid preimage\")\n\n\ts.claimed = true\n\ts.sendFn(cross(rlm), s.recipient)\n}\n\n// Refund allows the sender to refund the funds after the timelock has expired.\nfunc (s *Swap) Refund(_ int, rlm realm) {\n\trequire(rlm.IsCurrent(), \"unauthorized: rlm is not the caller's live cur\")\n\trequire(!s.claimed, \"already claimed\")\n\trequire(!s.refunded, \"already refunded\")\n\trequire(rlm.Previous().Address() == s.sender, \"unauthorized\")\n\trequire(time.Now().After(s.timelock), \"timelock not expired\")\n\n\ts.refunded = true\n\ts.sendFn(cross(rlm), s.sender)\n}\n\nfunc (s Swap) Status() string {\n\tswitch {\n\tcase s.refunded:\n\t\treturn \"refunded\"\n\tcase s.claimed:\n\t\treturn \"claimed\"\n\tcase s.TimeRemaining() \u003c 0:\n\t\treturn \"expired\"\n\tdefault:\n\t\treturn \"active\"\n\t}\n}\n\nfunc (s Swap) TimeRemaining() time.Duration {\n\tremaining := time.Until(s.timelock)\n\tif remaining \u003c 0 {\n\t\treturn 0\n\t}\n\treturn remaining\n}\n\n// String returns the current state of the swap.\nfunc (s Swap) String() string {\n\treturn ufmt.Sprintf(\n\t\t\"- status: %s\\n- sender: %s\\n- recipient: %s\\n- amount: %s\\n- hashlock: %s\\n- timelock: %s\\n- remaining: %s\",\n\t\ts.Status(), s.sender, s.recipient, s.amountStr, s.hashlock, s.timelock.Format(time.RFC3339), s.TimeRemaining().String(),\n\t)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"9eFToCDRtw1sEkRN5gtqP9ANNUzQDwNfOuT3CJuGZVpvKM2yK1+sU9u8aY5k+R8lkoxMZFIK8ljaegRGEFIZBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"foo20","path":"gno.land/r/demo/defi/foo20","files":[{"name":"foo20.gno","body":"// foo20 is a GRC20 token contract where all the grc20.Teller methods are\n// proxified with top-level functions. see also gno.land/r/demo/bar20.\npackage foo20\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n)\n\nvar (\n\tToken         *grc20.Token\n\tprivateLedger *grc20.PrivateLedger\n\tuserTeller    grc20.Teller\n\tOwnable       = ownable.NewWithAddress(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\") // govdao t1 multisig\n)\n\nfunc init(cur realm) {\n\tToken, privateLedger = grc20.NewToken(0, cur, \"Foo\", \"FOO\", 4)\n\tuserTeller = Token.CallerTeller()\n\tprivateLedger.Mint(Ownable.Owner(), 1_000_000*10_000) // @privateLedgeristrator (1M)\n\tgrc20reg.Register(cross(cur), Token, \"\")\n}\n\nfunc TotalSupply() int64 {\n\treturn userTeller.TotalSupply()\n}\n\nfunc BalanceOf(owner address) int64 {\n\treturn userTeller.BalanceOf(owner)\n}\n\nfunc Allowance(owner, spender address) int64 {\n\treturn userTeller.Allowance(owner, spender)\n}\n\nfunc Transfer(cur realm, to address, amount int64) {\n\tcheckErr(userTeller.Transfer(0, cur, to, amount))\n}\n\nfunc Approve(cur realm, spender address, amount int64) {\n\tcheckErr(userTeller.Approve(0, cur, spender, amount))\n}\n\nfunc TransferFrom(cur realm, from, to address, amount int64) {\n\tcheckErr(userTeller.TransferFrom(0, cur, from, to, amount))\n}\n\n// Faucet is distributing foo20 tokens without restriction (unsafe).\n// For a real token faucet, you should take care of setting limits are asking payment.\nfunc Faucet(cur realm) {\n\tcaller := cur.Previous().Address()\n\tamount := int64(1_000 * 10_000) // 1k\n\tcheckErr(privateLedger.Mint(caller, amount))\n}\n\nfunc Mint(cur realm, to address, amount int64) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\tcheckErr(privateLedger.Mint(to, amount))\n}\n\nfunc Burn(cur realm, from address, amount int64) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\tcheckErr(privateLedger.Burn(from, amount))\n}\n\nfunc Render(path string) string {\n\tparts := strings.Split(path, \"/\")\n\tc := len(parts)\n\n\tswitch {\n\tcase path == \"\":\n\t\treturn Token.RenderHome()\n\tcase c == 2 \u0026\u0026 parts[0] == \"balance\":\n\t\towner := address(parts[1])\n\t\tbalance := userTeller.BalanceOf(owner)\n\t\treturn ufmt.Sprintf(\"%d\\n\", balance)\n\tdefault:\n\t\treturn \"404\\n\"\n\t}\n}\n\nfunc checkErr(err error) {\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n}\n"},{"name":"foo20_test.gno","body":"package foo20\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestReadOnlyPublicMethods(cur realm, t *testing.T) {\n\tvar (\n\t\tadmin = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\t\talice = testutils.TestAddress(\"alice\")\n\t\tbob   = testutils.TestAddress(\"bob\")\n\t)\n\n\ttype test struct {\n\t\tname    string\n\t\tbalance int64\n\t\tfn      func() int64\n\t}\n\n\t// check balances #1.\n\t{\n\t\ttests := []test{\n\t\t\t{\"TotalSupply\", 10_000_000_000, func() int64 { return TotalSupply() }},\n\t\t\t{\"BalanceOf(admin)\", 10_000_000_000, func() int64 { return BalanceOf(admin) }},\n\t\t\t{\"BalanceOf(alice)\", 0, func() int64 { return BalanceOf(alice) }},\n\t\t\t{\"Allowance(admin, alice)\", 0, func() int64 { return Allowance(admin, alice) }},\n\t\t\t{\"BalanceOf(bob)\", 0, func() int64 { return BalanceOf(bob) }},\n\t\t}\n\t\tfor _, tc := range tests {\n\t\t\tgot := tc.fn()\n\t\t\tuassert.Equal(t, got, tc.balance)\n\t\t}\n\t}\n\n\t// bob uses the faucet.\n\ttesting.SetOriginCaller(bob)\n\tFaucet(cross(cur))\n\n\t// check balances #2.\n\t{\n\t\ttests := []test{\n\t\t\t{\"TotalSupply\", 10_010_000_000, func() int64 { return TotalSupply() }},\n\t\t\t{\"BalanceOf(admin)\", 10_000_000_000, func() int64 { return BalanceOf(admin) }},\n\t\t\t{\"BalanceOf(alice)\", 0, func() int64 { return BalanceOf(alice) }},\n\t\t\t{\"Allowance(admin, alice)\", 0, func() int64 { return Allowance(admin, alice) }},\n\t\t\t{\"BalanceOf(bob)\", 10_000_000, func() int64 { return BalanceOf(bob) }},\n\t\t}\n\t\tfor _, tc := range tests {\n\t\t\tgot := tc.fn()\n\t\t\tuassert.Equal(t, got, tc.balance)\n\t\t}\n\t}\n}\n\nfunc TestErrConditions(cur realm, t *testing.T) {\n\tvar (\n\t\tadmin = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\t\tempty = address(\"\")\n\t)\n\n\ttype test struct {\n\t\tname    string\n\t\tmsg     string\n\t\tisCross bool\n\t\tfn      func()\n\t}\n\n\tprivateLedger.Mint(address(admin), 10000)\n\t{\n\t\ttests := []test{\n\t\t\t{\"Transfer(admin, 1)\", \"cannot send transfer to self\", false, func() {\n\t\t\t\t// XXX: should replace with: Transfer(admin, 1)\n\t\t\t\t// but there is currently a limitation in manipulating the frame stack and simulate\n\t\t\t\t// calling this package from an outside point of view.\n\t\t\t\tadminAddr := address(admin)\n\t\t\t\tif err := privateLedger.Transfer(adminAddr, adminAddr, 1); err != nil {\n\t\t\t\t\tpanic(err)\n\t\t\t\t}\n\t\t\t}},\n\t\t\t{\"Approve(empty, 1))\", \"invalid address\", true, func() { Approve(cross(cur), empty, 1) }},\n\t\t}\n\t\tfor _, tc := range tests {\n\t\t\tif tc.isCross {\n\t\t\t\tuassert.AbortsWithMessage(t, cur, tc.msg, tc.fn)\n\t\t\t} else {\n\t\t\t\tuassert.PanicsWithMessage(t, cur, tc.msg, tc.fn)\n\t\t\t}\n\t\t}\n\t}\n}\n\n//func TestNewFoo20(t *testing.T) {\n//\tt.Run(\"invalid input\", func(t *testing.T) {\n//\t\ttestCases := []struct {\n//\t\t\tmsg string\n//\t\t\tfn  func()\n//\t\t}{\n//\t\t\t// Test AbortsWithMessage\n//\tuassert.PanicsWithMessage\", func() { NewFoo20(\"foo\", \"f\", 0) }},\n//\t\t\t{\"symbol cannot be empty\", func() { NewFoo20(\"foo\", \"\", 1) }},\n//\t\t\t{\"name cannot be empty\", func() { NewFoo20(\"\", \"f\", 1) }},\n//\t\t}\n//\t\tfor _, tc := range testCases {\n//\t\t\tuassert.AbortsWithMessage(t, cur, tc.msg, tc.fn)\n//\t\t}\n//\t})\n//\tt.Run(\"transfer\", func(t *testing.T) {\n//\t\t// ... existing code ...\n//\t})\n//}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/defi/foo20\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"1q0eOzF1yca1D4ry5pooSkph8EnP/lAunjYFgIQCjuwnf89XXeJfXeTIPpqB5pVfOTgyo/GG1ZQl0QjWnbeTBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"disperse","path":"gno.land/r/demo/disperse","files":[{"name":"disperse.gno","body":"package disperse\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"chain/runtime/unsafe\"\n\n\ttokens \"gno.land/r/demo/defi/grc20factory\"\n)\n\n// DisperseUgnot parses receivers and amounts and sends out ugnot\n// The function will send out the coins to the addresses and return the leftover coins to the caller\n// if there are any to return\nfunc DisperseUgnot(cur realm, addresses []address, coins chain.Coins) {\n\t// Reject non-EOA callers: unsafe.OriginSend() and the realm-balance\n\t// check below describe coins that actually landed at this realm only\n\t// when the caller is a pure EOA. A `maketx run` ephemeral realm or\n\t// intermediate code realm could otherwise consume the envelope and\n\t// have this function disperse pre-existing realm balance to\n\t// attacker-chosen addresses.\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\tcoinSent := unsafe.OriginSend()\n\tcaller := cur.Previous().Address()\n\trealmAddr := cur.Address()\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\n\tif len(addresses) != len(coins) {\n\t\tpanic(ErrNumAddrValMismatch)\n\t}\n\n\tfor _, coin := range coins {\n\t\tif coin.Amount \u003c= 0 {\n\t\t\tpanic(ErrNegativeCoinAmount)\n\t\t}\n\n\t\tif banker_.GetCoins(realmAddr).AmountOf(coin.Denom) \u003c coin.Amount {\n\t\t\tpanic(ErrMismatchBetweenSentAndParams)\n\t\t}\n\t}\n\n\t// Send coins\n\tfor i := range addresses {\n\t\tbanker_.SendCoins(realmAddr, addresses[i], chain.NewCoins(coins[i]))\n\t}\n\n\t// Return possible leftover coins\n\tfor _, coin := range coinSent {\n\t\tleftoverAmt := banker_.GetCoins(realmAddr).AmountOf(coin.Denom)\n\t\tif leftoverAmt \u003e 0 {\n\t\t\tsend := chain.Coins{chain.NewCoin(coin.Denom, leftoverAmt)}\n\t\t\tbanker_.SendCoins(realmAddr, caller, send)\n\t\t}\n\t}\n}\n\n// DisperseUgnotString receives a string of addresses and a string of amounts\n// and parses them to be used in DisperseUgnot\nfunc DisperseUgnotString(cur realm, addresses string, amounts string) {\n\tparsedAddresses, err := parseAddresses(addresses)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tparsedAmounts, err := parseAmounts(amounts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tcoins := make(chain.Coins, len(parsedAmounts))\n\tfor i, amount := range parsedAmounts {\n\t\tcoins[i] = chain.NewCoin(\"ugnot\", amount)\n\t}\n\n\tDisperseUgnot(cur, parsedAddresses, coins)\n}\n\n// DisperseGRC20 disperses tokens to multiple addresses\n// Note that it is necessary to approve the realm to spend the tokens before calling this function\n// see the corresponding filetests for examples\nfunc DisperseGRC20(cur realm, addresses []address, amounts []int64, symbols []string) {\n\tcaller := cur.Previous().Address()\n\n\tif (len(addresses) != len(amounts)) || (len(amounts) != len(symbols)) {\n\t\tpanic(ErrArgLenAndSentLenMismatch)\n\t}\n\tfor _, amount := range amounts {\n\t\tif amount \u003c 0 {\n\t\t\tpanic(ErrInvalidAmount)\n\t\t}\n\t}\n\n\tfor i := 0; i \u003c len(addresses); i++ {\n\t\ttokens.TransferFrom(cross(cur), symbols[i], caller, addresses[i], amounts[i])\n\t}\n}\n\n// DisperseGRC20String receives a string of addresses and a string of tokens\n// and parses them to be used in DisperseGRC20\nfunc DisperseGRC20String(cur realm, addresses string, tokens string) {\n\tparsedAddresses, err := parseAddresses(addresses)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tparsedAmounts, parsedSymbols, err := parseTokens(tokens)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tDisperseGRC20(cur, parsedAddresses, parsedAmounts, parsedSymbols)\n}\n"},{"name":"doc.gno","body":"// Package disperse provides methods to disperse coins or GRC20 tokens among multiple addresses.\n//\n// The disperse package is an implementation of an existing service that allows users to send coins or GRC20 tokens to multiple addresses\n// on the Ethereum blockchain.\n//\n// Usage:\n// To use disperse, you can either use `DisperseUgnot` to send coins or `DisperseGRC20` to send GRC20 tokens to multiple addresses.\n//\n// Example:\n// Dispersing 200 coins to two addresses:\n// - DisperseUgnotString(\"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c\", \"150,50\")\n// Dispersing 200 worth of a GRC20 token \"TEST\" to two addresses:\n// - DisperseGRC20String(\"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c\", \"150TEST,50TEST\")\n//\n// Reference:\n// - [the original dispere app](https://disperse.app/)\n// - [the original disperse app on etherscan](https://etherscan.io/address/0xd152f549545093347a162dce210e7293f1452150#code)\n// - [the gno disperse web app](https://gno-disperse.netlify.app/)\npackage disperse // import \"gno.land/r/demo/disperse\"\n"},{"name":"errors.gno","body":"package disperse\n\nimport \"errors\"\n\nvar (\n\tErrNotEnoughCoin                = errors.New(\"disperse: not enough coin sent in\")\n\tErrNumAddrValMismatch           = errors.New(\"disperse: number of addresses and values to send doesn't match\")\n\tErrInvalidAddress               = errors.New(\"disperse: invalid address\")\n\tErrNegativeCoinAmount           = errors.New(\"disperse: coin amount cannot be negative\")\n\tErrMismatchBetweenSentAndParams = errors.New(\"disperse: mismatch between coins sent and params called\")\n\tErrArgLenAndSentLenMismatch     = errors.New(\"disperse: mismatch between coins sent and args called\")\n\tErrInvalidAmount                = errors.New(\"disperse: invalid amount\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/disperse\"\ngno = \"0.9\"\n"},{"name":"util.gno","body":"package disperse\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\t\"unicode\"\n)\n\nfunc parseAddresses(addresses string) ([]address, error) {\n\tvar ret []address\n\n\tfor _, str := range strings.Split(addresses, \",\") {\n\t\taddr := address(str)\n\t\tif !addr.IsValid() {\n\t\t\treturn nil, ErrInvalidAddress\n\t\t}\n\n\t\tret = append(ret, addr)\n\t}\n\n\treturn ret, nil\n}\n\nfunc splitString(input string) (string, string) {\n\tvar pos int\n\tfor i, char := range input {\n\t\tif !unicode.IsDigit(char) {\n\t\t\tpos = i\n\t\t\tbreak\n\t\t}\n\t}\n\treturn input[:pos], input[pos:]\n}\n\nfunc parseTokens(tokens string) ([]int64, []string, error) {\n\tvar amounts []int64\n\tvar symbols []string\n\n\tfor _, token := range strings.Split(tokens, \",\") {\n\t\tamountStr, symbol := splitString(token)\n\t\tamount, _ := strconv.Atoi(amountStr)\n\t\tif amount \u003c 0 {\n\t\t\treturn nil, nil, ErrNegativeCoinAmount\n\t\t}\n\n\t\tamounts = append(amounts, int64(amount))\n\t\tsymbols = append(symbols, symbol)\n\t}\n\n\treturn amounts, symbols, nil\n}\n\nfunc parseAmounts(amounts string) ([]int64, error) {\n\tvar ret []int64\n\n\tfor _, amt := range strings.Split(amounts, \",\") {\n\t\tamount, _ := strconv.Atoi(amt)\n\t\tif amount \u003c 0 {\n\t\t\treturn nil, ErrNegativeCoinAmount\n\t\t}\n\n\t\tret = append(ret, int64(amount))\n\t}\n\n\treturn ret, nil\n}\n"},{"name":"z_0_filetest.gno","body":"// PKGPATH: gno.land/r/demo/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/r/demo/disperse\"\n)\n\nfunc main(cur realm) {\n\tmainAddr := chain.PackageAddress(\"gno.land/r/demo/main\")\n\tdisperseAddr := chain.PackageAddress(\"gno.land/r/demo/disperse\")\n\tbeneficiary1 := address(\"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0\")\n\tbeneficiary2 := address(\"g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c\")\n\n\ttesting.IssueCoins(mainAddr, chain.Coins{{Denom: \"ugnot\", Amount: 250}})\n\ttesting.SetOriginSend(chain.Coins{{Denom: \"ugnot\", Amount: 250}})\n\ttesting.SetRealm(testing.NewUserRealm(mainAddr))\n\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tprintln(\"main balance before send:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance before send:\", banker_.GetCoins(disperseAddr))\n\n\tbanker_.SendCoins(mainAddr, disperseAddr, chain.Coins{{\"ugnot\", 250}})\n\tprintln(\"main balance after send:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance after send:\", banker_.GetCoins(disperseAddr))\n\n\taddressesStr := beneficiary1.String() + \",\" + beneficiary2.String()\n\tdisperse.DisperseUgnotString(cross(cur), addressesStr, \"150,50\")\n\n\tprintln(\"main balance after disperse:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance after disperse:\", banker_.GetCoins(disperseAddr))\n\tprintln(\"beneficiary1 balance:\", banker_.GetCoins(beneficiary1))\n\tprintln(\"beneficiary2 balance:\", banker_.GetCoins(beneficiary2))\n}\n\n// Output:\n// main balance before send: 250ugnot\n// disperse balance before send:\n// main balance after send:\n// disperse balance after send: 250ugnot\n// main balance after disperse: 50ugnot\n// disperse balance after disperse:\n// beneficiary1 balance: 150ugnot\n// beneficiary2 balance: 50ugnot\n"},{"name":"z_1_filetest.gno","body":"// PKGPATH: gno.land/r/demo/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/r/demo/disperse\"\n)\n\nfunc main(cur realm) {\n\tmainAddr := chain.PackageAddress(\"gno.land/r/demo/main\")\n\tdisperseAddr := chain.PackageAddress(\"gno.land/r/demo/disperse\")\n\tbeneficiary1 := address(\"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0\")\n\tbeneficiary2 := address(\"g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c\")\n\n\t// Envelope = 300 ugnot, but only 200 forwarded to disperse (the\n\t// remaining 100 stays in mainAddr). Tests that the realm-balance\n\t// check, not OriginSend, is what gates dispersal.\n\ttesting.IssueCoins(mainAddr, chain.Coins{{Denom: \"ugnot\", Amount: 300}})\n\ttesting.SetOriginSend(chain.Coins{{Denom: \"ugnot\", Amount: 300}})\n\ttesting.SetRealm(testing.NewUserRealm(mainAddr))\n\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tprintln(\"main balance before send:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance before send:\", banker_.GetCoins(disperseAddr))\n\n\tbanker_.SendCoins(mainAddr, disperseAddr, chain.Coins{{\"ugnot\", 200}})\n\tprintln(\"main balance after send:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance after send:\", banker_.GetCoins(disperseAddr))\n\n\taddressesStr := beneficiary1.String() + \",\" + beneficiary2.String()\n\tdisperse.DisperseUgnotString(cross(cur), addressesStr, \"150,50\")\n\n\tprintln(\"main balance after disperse:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance after disperse:\", banker_.GetCoins(disperseAddr))\n\tprintln(\"beneficiary1 balance:\", banker_.GetCoins(beneficiary1))\n\tprintln(\"beneficiary2 balance:\", banker_.GetCoins(beneficiary2))\n}\n\n// Output:\n// main balance before send: 300ugnot\n// disperse balance before send:\n// main balance after send: 100ugnot\n// disperse balance after send: 200ugnot\n// main balance after disperse: 100ugnot\n// disperse balance after disperse:\n// beneficiary1 balance: 150ugnot\n// beneficiary2 balance: 50ugnot\n"},{"name":"z_2_filetest.gno","body":"// PKGPATH: gno.land/r/demo/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/r/demo/disperse\"\n)\n\nfunc main(cur realm) {\n\tmainAddr := chain.PackageAddress(\"gno.land/r/demo/main\")\n\tdisperseAddr := chain.PackageAddress(\"gno.land/r/demo/disperse\")\n\tbeneficiary1 := address(\"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0\")\n\tbeneficiary2 := address(\"g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c\")\n\n\t// Mismatch: requested dispersal sum (200) exceeds the realm balance (100).\n\ttesting.IssueCoins(mainAddr, chain.Coins{{Denom: \"ugnot\", Amount: 100}})\n\ttesting.SetOriginSend(chain.Coins{{Denom: \"ugnot\", Amount: 100}})\n\ttesting.SetRealm(testing.NewUserRealm(mainAddr))\n\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tprintln(\"main balance before send:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance before send:\", banker_.GetCoins(disperseAddr))\n\n\tbanker_.SendCoins(mainAddr, disperseAddr, chain.Coins{{\"ugnot\", 100}})\n\tprintln(\"main balance after send:\", banker_.GetCoins(mainAddr))\n\tprintln(\"disperse balance after send:\", banker_.GetCoins(disperseAddr))\n\n\taddressesStr := beneficiary1.String() + \",\" + beneficiary2.String()\n\tdisperse.DisperseUgnotString(cross(cur), addressesStr, \"150,50\")\n}\n\n// Error:\n// disperse: mismatch between coins sent and params called\n"},{"name":"z_3_filetest.gno","body":"// PKGPATH: gno.land/r/demo/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\ttokens \"gno.land/r/demo/defi/grc20factory\"\n\t\"gno.land/r/demo/disperse\"\n)\n\nfunc main(cur realm) {\n\tdisperseAddr := chain.PackageAddress(\"gno.land/r/demo/disperse\")\n\tmainAddr := chain.PackageAddress(\"gno.land/r/demo/main\")\n\tbeneficiary1 := address(\"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0\")\n\tbeneficiary2 := address(\"g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c\")\n\n\ttesting.SetOriginCaller(mainAddr)\n\n\ttokens.New(cross(cur), \"test\", \"TEST\", 4, 0, 0)\n\ttokens.Mint(cross(cur), \"TEST\", mainAddr, 200)\n\tprintln(\"main balance before:\", tokens.BalanceOf(\"TEST\", mainAddr))\n\n\ttokens.Approve(cross(cur), \"TEST\", disperseAddr, 200)\n\tprintln(\"disperse allowance before:\", tokens.Allowance(\"TEST\", mainAddr, disperseAddr))\n\n\taddressesStr := beneficiary1.String() + \",\" + beneficiary2.String()\n\tdisperse.DisperseGRC20String(cross(cur), addressesStr, \"150TEST,50TEST\")\n\n\tprintln(\"main balance after:\", tokens.BalanceOf(\"TEST\", mainAddr))\n\tprintln(\"disperse allowance after:\", tokens.Allowance(\"TEST\", mainAddr, disperseAddr))\n\tprintln(\"beneficiary1 balance:\", tokens.BalanceOf(\"TEST\", beneficiary1))\n\tprintln(\"beneficiary2 balance:\", tokens.BalanceOf(\"TEST\", beneficiary2))\n}\n\n// Output:\n// main balance before: 200\n// disperse allowance before: 200\n// main balance after: 0\n// disperse allowance after: 0\n// beneficiary1 balance: 150\n// beneficiary2 balance: 50\n"},{"name":"z_4_filetest.gno","body":"// PKGPATH: gno.land/r/demo/main\n\npackage main\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\ttokens \"gno.land/r/demo/defi/grc20factory\"\n\t\"gno.land/r/demo/disperse\"\n)\n\nfunc main(cur realm) {\n\tdisperseAddr := chain.PackageAddress(\"gno.land/r/demo/disperse\")\n\tmainAddr := chain.PackageAddress(\"gno.land/r/demo/main\")\n\tbeneficiary1 := address(\"g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0\")\n\tbeneficiary2 := address(\"g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c\")\n\n\ttesting.SetOriginCaller(mainAddr)\n\n\ttokens.New(cross(cur), \"test1\", \"TEST1\", 4, 0, 0)\n\ttokens.Mint(cross(cur), \"TEST1\", mainAddr, 200)\n\tprintln(\"main balance before (TEST1):\", tokens.BalanceOf(\"TEST1\", mainAddr))\n\n\ttokens.New(cross(cur), \"test2\", \"TEST2\", 4, 0, 0)\n\ttokens.Mint(cross(cur), \"TEST2\", mainAddr, 200)\n\tprintln(\"main balance before (TEST2):\", tokens.BalanceOf(\"TEST2\", mainAddr))\n\n\ttokens.Approve(cross(cur), \"TEST1\", disperseAddr, 200)\n\tprintln(\"disperse allowance before (TEST1):\", tokens.Allowance(\"TEST1\", mainAddr, disperseAddr))\n\n\ttokens.Approve(cross(cur), \"TEST2\", disperseAddr, 200)\n\tprintln(\"disperse allowance before (TEST2):\", tokens.Allowance(\"TEST2\", mainAddr, disperseAddr))\n\n\taddressesStr := beneficiary1.String() + \",\" + beneficiary2.String()\n\tdisperse.DisperseGRC20String(cross(cur), addressesStr, \"200TEST1,200TEST2\")\n\n\tprintln(\"main balance after (TEST1):\", tokens.BalanceOf(\"TEST1\", mainAddr))\n\tprintln(\"main balance after (TEST2):\", tokens.BalanceOf(\"TEST2\", mainAddr))\n\tprintln(\"disperse allowance after (TEST1):\", tokens.Allowance(\"TEST1\", mainAddr, disperseAddr))\n\tprintln(\"disperse allowance after (TEST2):\", tokens.Allowance(\"TEST2\", mainAddr, disperseAddr))\n\tprintln(\"beneficiary1 balance (TEST1):\", tokens.BalanceOf(\"TEST1\", beneficiary1))\n\tprintln(\"beneficiary1 balance (TEST2):\", tokens.BalanceOf(\"TEST2\", beneficiary1))\n\tprintln(\"beneficiary2 balance (TEST1):\", tokens.BalanceOf(\"TEST1\", beneficiary2))\n\tprintln(\"beneficiary2 balance (TEST2):\", tokens.BalanceOf(\"TEST2\", beneficiary2))\n}\n\n// Output:\n// main balance before (TEST1): 200\n// main balance before (TEST2): 200\n// disperse allowance before (TEST1): 200\n// disperse allowance before (TEST2): 200\n// main balance after (TEST1): 0\n// main balance after (TEST2): 0\n// disperse allowance after (TEST1): 0\n// disperse allowance after (TEST2): 0\n// beneficiary1 balance (TEST1): 200\n// beneficiary1 balance (TEST2): 0\n// beneficiary2 balance (TEST1): 0\n// beneficiary2 balance (TEST2): 200\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"TVjU1sO8F9/rGsVZIbffJsXvtTLvyvA11EJMKvvaOlbJ0zX++J6hm8l3mNcndynYLGt72iGm6z7nDE9j/hkXAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"draftrealm","path":"gno.land/r/demo/draftrealm","files":[{"name":"draftrealm.gno","body":"package draftrealm\n\n// this realm is an example of a draft realm\n// it can only be deployed and imported by packages added at genesis time\n\nfunc Render(path string) string {\n\treturn \"draft\"\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/draftrealm\"\ngno = \"0.9\"\ndraft = true\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"aYPAQjQwhcwukjllcEPPtlro+kc1h7a5+L0eMV5nvbZ5ZDYkZFliIElE413sRze3y0kYXhefPiEb/BQlieldDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"profile","path":"gno.land/r/demo/profile","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/profile\"\ngno = \"0.9\"\n"},{"name":"profile.gno","body":"package profile\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tfields = avl.NewTree()\n\trouter = mux.NewRouter()\n)\n\n// Standard fields\nconst (\n\tDisplayName        = \"DisplayName\"\n\tHomepage           = \"Homepage\"\n\tBio                = \"Bio\"\n\tAge                = \"Age\"\n\tLocation           = \"Location\"\n\tAvatar             = \"Avatar\"\n\tGravatarEmail      = \"GravatarEmail\"\n\tAvailableForHiring = \"AvailableForHiring\"\n\tInvalidField       = \"InvalidField\"\n)\n\n// Events\nconst (\n\tProfileFieldCreated = \"ProfileFieldCreated\"\n\tProfileFieldUpdated = \"ProfileFieldUpdated\"\n)\n\n// Field types used when emitting event\nconst FieldType = \"FieldType\"\n\nconst (\n\tBoolField   = \"BoolField\"\n\tStringField = \"StringField\"\n\tIntField    = \"IntField\"\n)\n\nfunc init() {\n\trouter.HandleFunc(\"\", homeHandler)\n\trouter.HandleFunc(\"u/{addr}\", profileHandler)\n\trouter.HandleFunc(\"f/{addr}/{field}\", fieldHandler)\n}\n\n// List of supported string fields\nvar stringFields = map[string]bool{\n\tDisplayName:   true,\n\tHomepage:      true,\n\tBio:           true,\n\tLocation:      true,\n\tAvatar:        true,\n\tGravatarEmail: true,\n}\n\n// List of support int fields\nvar intFields = map[string]bool{\n\tAge: true,\n}\n\n// List of support bool fields\nvar boolFields = map[string]bool{\n\tAvailableForHiring: true,\n}\n\n// Setters\n\nfunc SetStringField(cur realm, field, value string) bool {\n\taddr := cur.Previous().Address()\n\tkey := addr.String() + \":\" + field\n\tupdated := fields.Set(key, value)\n\n\tevent := ProfileFieldCreated\n\tif updated {\n\t\tevent = ProfileFieldUpdated\n\t}\n\n\tchain.Emit(event, FieldType, StringField, field, value)\n\n\treturn updated\n}\n\nfunc SetIntField(cur realm, field string, value int) bool {\n\taddr := cur.Previous().Address()\n\tkey := addr.String() + \":\" + field\n\tupdated := fields.Set(key, value)\n\n\tevent := ProfileFieldCreated\n\tif updated {\n\t\tevent = ProfileFieldUpdated\n\t}\n\n\tchain.Emit(event, FieldType, IntField, field, string(value))\n\n\treturn updated\n}\n\nfunc SetBoolField(cur realm, field string, value bool) bool {\n\taddr := cur.Previous().Address()\n\tkey := addr.String() + \":\" + field\n\tupdated := fields.Set(key, value)\n\n\tevent := ProfileFieldCreated\n\tif updated {\n\t\tevent = ProfileFieldUpdated\n\t}\n\n\tchain.Emit(event, FieldType, BoolField, field, ufmt.Sprintf(\"%t\", value))\n\n\treturn updated\n}\n\n// Getters\n\nfunc GetStringField(addr address, field, def string) string {\n\tkey := addr.String() + \":\" + field\n\tif value := fields.Get(key); value != nil {\n\t\treturn value.(string)\n\t}\n\n\treturn def\n}\n\nfunc GetBoolField(addr address, field string, def bool) bool {\n\tkey := addr.String() + \":\" + field\n\tif value := fields.Get(key); value != nil {\n\t\treturn value.(bool)\n\t}\n\n\treturn def\n}\n\nfunc GetIntField(addr address, field string, def int) int {\n\tkey := addr.String() + \":\" + field\n\tif value := fields.Get(key); value != nil {\n\t\treturn value.(int)\n\t}\n\n\treturn def\n}\n"},{"name":"profile_test.gno","body":"package profile\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\n// Global addresses for test users\nvar (\n\talice   = testutils.TestAddress(\"alice\")\n\tbob     = testutils.TestAddress(\"bob\")\n\tcharlie = testutils.TestAddress(\"charlie\")\n\tdave    = testutils.TestAddress(\"dave\")\n\teve     = testutils.TestAddress(\"eve\")\n\tfrank   = testutils.TestAddress(\"frank\")\n\tuser1   = testutils.TestAddress(\"user1\")\n\tuser2   = testutils.TestAddress(\"user2\")\n)\n\nfunc TestStringFields(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\t// Get before setting\n\tname := GetStringField(alice, DisplayName, \"anon\")\n\tuassert.Equal(t, \"anon\", name)\n\n\t// Set new key\n\tupdated := SetStringField(cross(cur), DisplayName, \"Alice foo\")\n\tuassert.Equal(t, updated, false)\n\tupdated = SetStringField(cross(cur), Homepage, \"https://example.com\")\n\tuassert.Equal(t, updated, false)\n\n\t// Update the key\n\tupdated = SetStringField(cross(cur), DisplayName, \"Alice foo\")\n\tuassert.Equal(t, updated, true)\n\n\t// Get after setting\n\tname = GetStringField(alice, DisplayName, \"anon\")\n\thomepage := GetStringField(alice, Homepage, \"\")\n\tbio := GetStringField(alice, Bio, \"42\")\n\n\tuassert.Equal(t, \"Alice foo\", name)\n\tuassert.Equal(t, \"https://example.com\", homepage)\n\tuassert.Equal(t, \"42\", bio)\n}\n\nfunc TestIntFields(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\n\t// Get before setting\n\tage := GetIntField(bob, Age, 25)\n\tuassert.Equal(t, 25, age)\n\n\t// Set new key\n\tupdated := SetIntField(cross(cur), Age, 30)\n\tuassert.Equal(t, updated, false)\n\n\t// Update the key\n\tupdated = SetIntField(cross(cur), Age, 30)\n\tuassert.Equal(t, updated, true)\n\n\t// Get after setting\n\tage = GetIntField(bob, Age, 25)\n\tuassert.Equal(t, 30, age)\n}\n\nfunc TestBoolFields(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(charlie))\n\n\t// Get before setting\n\thiring := GetBoolField(charlie, AvailableForHiring, false)\n\tuassert.Equal(t, false, hiring)\n\n\t// Set\n\tupdated := SetBoolField(cross(cur), AvailableForHiring, true)\n\tuassert.Equal(t, updated, false)\n\n\t// Update the key\n\tupdated = SetBoolField(cross(cur), AvailableForHiring, true)\n\tuassert.Equal(t, updated, true)\n\n\t// Get after setting\n\thiring = GetBoolField(charlie, AvailableForHiring, false)\n\tuassert.Equal(t, true, hiring)\n}\n\nfunc TestMultipleProfiles(cur realm, t *testing.T) {\n\t// Set profile for user1\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\tupdated := SetStringField(cross(cur), DisplayName, \"User One\")\n\tuassert.Equal(t, updated, false)\n\n\t// Set profile for user2\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\tupdated = SetStringField(cross(cur), DisplayName, \"User Two\")\n\tuassert.Equal(t, updated, false)\n\n\t// Get profiles\n\ttesting.SetRealm(testing.NewUserRealm(user1)) // Switch back to user1\n\tname1 := GetStringField(user1, DisplayName, \"anon\")\n\ttesting.SetRealm(testing.NewUserRealm(user2)) // Switch back to user2\n\tname2 := GetStringField(user2, DisplayName, \"anon\")\n\n\tuassert.Equal(t, \"User One\", name1)\n\tuassert.Equal(t, \"User Two\", name2)\n}\n\nfunc TestArbitraryStringField(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\n\t// Set arbitrary string field\n\tupdated := SetStringField(cross(cur), \"MyEmail\", \"my@email.com\")\n\tuassert.Equal(t, updated, false)\n\n\tval := GetStringField(user1, \"MyEmail\", \"\")\n\tuassert.Equal(t, val, \"my@email.com\")\n}\n\nfunc TestArbitraryIntField(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\n\t// Set arbitrary int field\n\tupdated := SetIntField(cross(cur), \"MyIncome\", 100_000)\n\tuassert.Equal(t, updated, false)\n\n\tval := GetIntField(user1, \"MyIncome\", 0)\n\tuassert.Equal(t, val, 100_000)\n}\n\nfunc TestArbitraryBoolField(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\n\t// Set arbitrary bool field\n\tupdated := SetBoolField(cross(cur), \"IsWinner\", true)\n\tuassert.Equal(t, updated, false)\n\n\tval := GetBoolField(user1, \"IsWinner\", false)\n\tuassert.Equal(t, val, true)\n}\n"},{"name":"render.gno","body":"package profile\n\nimport (\n\t\"bytes\"\n\t\"net/url\"\n\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst (\n\tBaseURL           = \"/r/demo/profile\"\n\tSetStringFieldURL = BaseURL + \"$help\u0026func=SetStringField\u0026field=%s\"\n\tSetIntFieldURL    = BaseURL + \"$help\u0026func=SetIntField\u0026field=%s\"\n\tSetBoolFieldURL   = BaseURL + \"$help\u0026func=SetBoolField\u0026field=%s\"\n\tViewAllFieldsURL  = BaseURL + \":u/%s\"\n\tViewFieldURL      = BaseURL + \":f/%s/%s\"\n)\n\nfunc homeHandler(res *mux.ResponseWriter, req *mux.Request) {\n\tvar b bytes.Buffer\n\n\tb.WriteString(\"## Setters\\n\")\n\tfor field := range stringFields {\n\t\tlink := ufmt.Sprintf(SetStringFieldURL, field)\n\t\tb.WriteString(ufmt.Sprintf(\"- [Set %s](%s)\\n\", field, link))\n\t}\n\n\tfor field := range intFields {\n\t\tlink := ufmt.Sprintf(SetIntFieldURL, field)\n\t\tb.WriteString(ufmt.Sprintf(\"- [Set %s](%s)\\n\", field, link))\n\t}\n\n\tfor field := range boolFields {\n\t\tlink := ufmt.Sprintf(SetBoolFieldURL, field)\n\t\tb.WriteString(ufmt.Sprintf(\"- [Set %s Field](%s)\\n\", field, link))\n\t}\n\n\tb.WriteString(\"\\n---\\n\\n\")\n\n\tres.Write(b.String())\n}\n\nfunc profileHandler(res *mux.ResponseWriter, req *mux.Request) {\n\tvar b bytes.Buffer\n\taddr := req.GetVar(\"addr\")\n\n\tb.WriteString(ufmt.Sprintf(\"# Profile %s\\n\", addr))\n\n\taddress_XXX := address(addr)\n\n\tfor field := range stringFields {\n\t\tvalue := GetStringField(address_XXX, field, \"n/a\")\n\t\tlink := ufmt.Sprintf(SetStringFieldURL, field)\n\t\tb.WriteString(ufmt.Sprintf(\"- %s: %s [Edit](%s)\\n\", field, value, link))\n\t}\n\n\tfor field := range intFields {\n\t\tvalue := GetIntField(address_XXX, field, 0)\n\t\tlink := ufmt.Sprintf(SetIntFieldURL, field)\n\t\tb.WriteString(ufmt.Sprintf(\"- %s: %d [Edit](%s)\\n\", field, value, link))\n\t}\n\n\tfor field := range boolFields {\n\t\tvalue := GetBoolField(address_XXX, field, false)\n\t\tlink := ufmt.Sprintf(SetBoolFieldURL, field)\n\t\tb.WriteString(ufmt.Sprintf(\"- %s: %t [Edit](%s)\\n\", field, value, link))\n\t}\n\n\tres.Write(b.String())\n}\n\nfunc fieldHandler(res *mux.ResponseWriter, req *mux.Request) {\n\tvar b bytes.Buffer\n\taddr := req.GetVar(\"addr\")\n\tfield := req.GetVar(\"field\")\n\n\tb.WriteString(ufmt.Sprintf(\"# Field %s for %s\\n\", field, addr))\n\n\taddress_XXX := address(addr)\n\tvalue := \"n/a\"\n\tvar editLink string\n\n\tif _, ok := stringFields[field]; ok {\n\t\tvalue = ufmt.Sprintf(\"%s\", GetStringField(address_XXX, field, \"n/a\"))\n\t\teditLink = ufmt.Sprintf(SetStringFieldURL+\"\u0026addr=%s\u0026value=%s\", field, addr, url.QueryEscape(value))\n\t} else if _, ok := intFields[field]; ok {\n\t\tvalue = ufmt.Sprintf(\"%d\", GetIntField(address_XXX, field, 0))\n\t\teditLink = ufmt.Sprintf(SetIntFieldURL+\"\u0026addr=%s\u0026value=%s\", field, addr, value)\n\t} else if _, ok := boolFields[field]; ok {\n\t\tvalue = ufmt.Sprintf(\"%t\", GetBoolField(address_XXX, field, false))\n\t\teditLink = ufmt.Sprintf(SetBoolFieldURL+\"\u0026addr=%s\u0026value=%s\", field, addr, value)\n\t}\n\n\tb.WriteString(ufmt.Sprintf(\"- %s: %s [Edit](%s)\\n\", field, value, editLink))\n\n\tres.Write(b.String())\n}\n\nfunc Render(path string) string {\n\treturn router.Render(path)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"1tbnIAcrBiSGXxRZoCPpOEmwpMR8JdKJYSj75KPYFTaZzk16jJp2B0acELYiiqub8PswxvN3ceABnyaaEfXKDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"events","path":"gno.land/r/devrels/events","files":[{"name":"errors.gno","body":"package events\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n)\n\nvar (\n\tErrEmptyName                 = errors.New(\"event name cannot be empty\")\n\tErrNoSuchID                  = errors.New(\"event with specified ID does not exist\")\n\tErrMinWidgetSize             = errors.New(\"you need to request at least 1 event to render\")\n\tErrMaxWidgetSize             = errors.New(\"maximum number of events in widget is\" + strconv.Itoa(MaxWidgetSize))\n\tErrDescriptionTooLong        = errors.New(\"event description is too long\")\n\tErrInvalidStartTime          = errors.New(\"invalid start time format\")\n\tErrInvalidEndTime            = errors.New(\"invalid end time format\")\n\tErrEndBeforeStart            = errors.New(\"end time cannot be before start time\")\n\tErrStartEndTimezonemMismatch = errors.New(\"start and end timezones are not the same\")\n)\n"},{"name":"events.gno","body":"// Package events allows you to upload data about specific IRL/online events\n// It includes dynamic support for updating rendering events based on their\n// status, ie if they are upcoming, in progress, or in the past.\npackage events\n\nimport (\n\t\"chain\"\n\t\"sort\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype (\n\tEvent struct {\n\t\tid          string\n\t\tname        string    // name of event\n\t\tdescription string    // short description of event\n\t\tlink        string    // link to auth corresponding web2 page, ie eventbrite/luma or conference page\n\t\tlocation    string    // location of the event\n\t\tstartTime   time.Time // given in RFC3339\n\t\tendTime     time.Time // end time of the event, given in RFC3339\n\t}\n\n\teventsSlice []*Event\n)\n\nvar (\n\tOwnable   = ownable.NewWithAddress(address(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\")) // @leohhhn\n\tevents    = make(eventsSlice, 0)                                                        // sorted\n\tidCounter seqid.ID\n)\n\nconst (\n\tmaxDescLength = 100\n\tEventAdded    = \"EventAdded\"\n\tEventDeleted  = \"EventDeleted\"\n\tEventEdited   = \"EventEdited\"\n)\n\n// AddEvent adds auth new event\n// Start time \u0026 end time need to be specified in RFC3339, ie 2024-08-08T12:00:00+02:00\nfunc AddEvent(cur realm, name, description, link, location, startTime, endTime string) (string, error) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\n\tif strings.TrimSpace(name) == \"\" {\n\t\treturn \"\", ErrEmptyName\n\t}\n\n\tif len(description) \u003e maxDescLength {\n\t\treturn \"\", ufmt.Errorf(\"%s: provided length is %d, maximum is %d\", ErrDescriptionTooLong, len(description), maxDescLength)\n\t}\n\n\t// Parse times\n\tst, et, err := parseTimes(startTime, endTime)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tid := idCounter.Next().String()\n\te := \u0026Event{\n\t\tid:          id,\n\t\tname:        name,\n\t\tdescription: description,\n\t\tlink:        link,\n\t\tlocation:    location,\n\t\tstartTime:   st,\n\t\tendTime:     et,\n\t}\n\n\tevents = append(events, e)\n\tsort.Sort(events)\n\n\tchain.Emit(EventAdded,\n\t\t\"id\", e.id,\n\t)\n\n\treturn id, nil\n}\n\n// DeleteEvent deletes an event with auth given ID\nfunc DeleteEvent(cur realm, id string) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\n\te, idx, err := GetEventByID(id)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents = append(events[:idx], events[idx+1:]...)\n\n\tchain.Emit(EventDeleted,\n\t\t\"id\", e.id,\n\t)\n}\n\n// EditEvent edits an event with auth given ID\n// It only updates values corresponding to non-empty arguments sent with the call\n// Note: if you need to update the start time or end time, you need to provide both every time\nfunc EditEvent(cur realm, id string, name, description, link, location, startTime, endTime string) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\n\te, _, err := GetEventByID(id)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Set only valid values\n\tif strings.TrimSpace(name) != \"\" {\n\t\te.name = name\n\t}\n\n\tif strings.TrimSpace(description) != \"\" {\n\t\te.description = description\n\t}\n\n\tif strings.TrimSpace(link) != \"\" {\n\t\te.link = link\n\t}\n\n\tif strings.TrimSpace(location) != \"\" {\n\t\te.location = location\n\t}\n\n\tif strings.TrimSpace(startTime) != \"\" || strings.TrimSpace(endTime) != \"\" {\n\t\tst, et, err := parseTimes(startTime, endTime)\n\t\tif err != nil {\n\t\t\tpanic(err) // need to also revert other state changes\n\t\t}\n\n\t\toldStartTime := e.startTime\n\t\te.startTime = st\n\t\te.endTime = et\n\n\t\t// If sort order was disrupted, sort again\n\t\tif oldStartTime != e.startTime {\n\t\t\tsort.Sort(events)\n\t\t}\n\t}\n\n\tchain.Emit(EventEdited,\n\t\t\"id\", e.id,\n\t)\n}\n\nfunc GetEventByID(id string) (*Event, int, error) {\n\tfor i, event := range events {\n\t\tif event.id == id {\n\t\t\treturn event, i, nil\n\t\t}\n\t}\n\n\treturn nil, -1, ErrNoSuchID\n}\n\n// Len returns the length of the slice\nfunc (m eventsSlice) Len() int {\n\treturn len(m)\n}\n\n// Less compares the startTime fields of two elements\n// In this case, events will be sorted by largest startTime first (upcoming \u003e past)\nfunc (m eventsSlice) Less(i, j int) bool {\n\treturn m[i].startTime.After(m[j].startTime)\n}\n\n// Swap swaps two elements in the slice\nfunc (m eventsSlice) Swap(i, j int) {\n\tm[i], m[j] = m[j], m[i]\n}\n\n// parseTimes parses the start and end time for an event and checks for possible errors\nfunc parseTimes(startTime, endTime string) (time.Time, time.Time, error) {\n\tst, err := time.Parse(time.RFC3339, startTime)\n\tif err != nil {\n\t\treturn time.Time{}, time.Time{}, ufmt.Errorf(\"%s: %s\", ErrInvalidStartTime, err.Error())\n\t}\n\n\tet, err := time.Parse(time.RFC3339, endTime)\n\tif err != nil {\n\t\treturn time.Time{}, time.Time{}, ufmt.Errorf(\"%s: %s\", ErrInvalidEndTime, err.Error())\n\t}\n\n\tif et.Before(st) {\n\t\treturn time.Time{}, time.Time{}, ErrEndBeforeStart\n\t}\n\n\t_, stOffset := st.Zone()\n\t_, etOffset := et.Zone()\n\tif stOffset != etOffset {\n\t\treturn time.Time{}, time.Time{}, ErrStartEndTimezonemMismatch\n\t}\n\n\treturn st, et, nil\n}\n"},{"name":"events_test.gno","body":"package events\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar (\n\tsuRealm          = testing.NewUserRealm(address(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"))\n\tnow              = \"2009-02-13T23:31:30Z\" // time.Now() is hardcoded to this value in the gno test machine currently\n\tparsedTimeNow, _ = time.Parse(time.RFC3339, now)\n)\n\nfunc TestAddEvent(cur realm, t *testing.T) {\n\ttesting.SetRealm(suRealm)\n\n\te1Start := parsedTimeNow.Add(time.Hour * 24 * 5)\n\te1End := e1Start.Add(time.Hour * 4)\n\n\t_, err := AddEvent(cross(cur), \"Event 1\", \"this event is upcoming\", \"gno.land\", \"gnome land\", e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))\n\n\turequire.NoError(t, err)\n\tgot := renderHome(false)\n\n\tif !strings.Contains(got, \"Event 1\") {\n\t\tt.Fatalf(\"Expected to find Event 1 in render\")\n\t}\n\n\te2Start := parsedTimeNow.Add(-time.Hour * 24 * 5)\n\te2End := e2Start.Add(time.Hour * 4)\n\n\t_, err = AddEvent(cross(cur), \"Event 2\", \"this event is in the past\", \"gno.land\", \"gnome land\", e2Start.Format(time.RFC3339), e2End.Format(time.RFC3339))\n\turequire.NoError(t, err)\n\n\tgot = renderHome(false)\n\n\tupcomingPos := strings.Index(got, \"## Upcoming events\")\n\tpastPos := strings.Index(got, \"## Past events\")\n\n\te1Pos := strings.Index(got, \"Event 1\")\n\te2Pos := strings.Index(got, \"Event 2\")\n\n\t// expected index ordering: upcoming \u003c e1 \u003c past \u003c e2\n\tif e1Pos \u003c upcomingPos || e1Pos \u003e pastPos {\n\t\tt.Fatalf(\"Expected to find Event 1 in Upcoming events\")\n\t}\n\n\tif e2Pos \u003c upcomingPos || e2Pos \u003c pastPos || e2Pos \u003c e1Pos {\n\t\tt.Fatalf(\"Expected to find Event 2 on auth different pos\")\n\t}\n\n\t// larger index =\u003e smaller startTime (future =\u003e past)\n\tif events[0].startTime.Unix() \u003c events[1].startTime.Unix() {\n\t\tt.Fatalf(\"expected ordering to be different\")\n\t}\n}\n\nfunc TestAddEventErrors(cur realm, t *testing.T) {\n\ttesting.SetRealm(suRealm)\n\n\t_, err := AddEvent(cross(cur), \"\", \"sample desc\", \"gno.land\", \"gnome land\", \"2009-02-13T23:31:31Z\", \"2009-02-13T23:33:31Z\")\n\tuassert.ErrorIs(t, err, ErrEmptyName)\n\n\t_, err = AddEvent(cross(cur), \"sample name\", \"sample desc\", \"gno.land\", \"gnome land\", \"\", \"2009-02-13T23:33:31Z\")\n\tuassert.ErrorContains(t, err, ErrInvalidStartTime.Error())\n\n\t_, err = AddEvent(cross(cur), \"sample name\", \"sample desc\", \"gno.land\", \"gnome land\", \"2009-02-13T23:31:31Z\", \"\")\n\tuassert.ErrorContains(t, err, ErrInvalidEndTime.Error())\n\n\t_, err = AddEvent(cross(cur), \"sample name\", \"sample desc\", \"gno.land\", \"gnome land\", \"2009-02-13T23:31:31Z\", \"2009-02-13T23:30:31Z\")\n\tuassert.ErrorIs(t, err, ErrEndBeforeStart)\n\n\t_, err = AddEvent(cross(cur), \"sample name\", \"sample desc\", \"gno.land\", \"gnome land\", \"2009-02-13T23:31:31+06:00\", \"2009-02-13T23:33:31+02:00\")\n\tuassert.ErrorIs(t, err, ErrStartEndTimezonemMismatch)\n\n\ttooLongDesc := `Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean ma`\n\t_, err = AddEvent(cross(cur), \"sample name\", tooLongDesc, \"gno.land\", \"gnome land\", \"2009-02-13T23:31:31Z\", \"2009-02-13T23:33:31Z\")\n\tuassert.ErrorContains(t, err, ErrDescriptionTooLong.Error())\n}\n\nfunc TestDeleteEvent(cur realm, t *testing.T) {\n\ttesting.SetRealm(suRealm)\n\n\te1Start := parsedTimeNow.Add(time.Hour * 24 * 5)\n\te1End := e1Start.Add(time.Hour * 4)\n\n\tid, err := AddEvent(cross(cur), \"ToDelete\", \"description\", \"gno.land\", \"gnome land\", e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))\n\turequire.NoError(t, err)\n\n\tgot := renderHome(false)\n\n\tif !strings.Contains(got, \"ToDelete\") {\n\t\tt.Fatalf(\"Expected to find ToDelete event in render\")\n\t}\n\n\tDeleteEvent(cross(cur), id)\n\tgot = renderHome(false)\n\n\tif strings.Contains(got, \"ToDelete\") {\n\t\tt.Fatalf(\"Did not expect to find ToDelete event in render\")\n\t}\n}\n\nfunc TestEditEvent(cur realm, t *testing.T) {\n\ttesting.SetRealm(suRealm)\n\n\te1Start := parsedTimeNow.Add(time.Hour * 24 * 5)\n\te1End := e1Start.Add(time.Hour * 4)\n\tloc := \"gnome land\"\n\n\tid, err := AddEvent(cross(cur), \"ToDelete\", \"description\", \"gno.land\", loc, e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))\n\turequire.NoError(t, err)\n\n\tnewName := \"New Name\"\n\tnewDesc := \"Normal description\"\n\tnewLink := \"new Link\"\n\tnewST := e1Start.Add(time.Hour)\n\tnewET := newST.Add(time.Hour)\n\n\tEditEvent(cross(cur), id, newName, newDesc, newLink, \"\", newST.Format(time.RFC3339), newET.Format(time.RFC3339))\n\tedited, _, _ := GetEventByID(id)\n\n\t// Check updated values\n\tuassert.Equal(t, edited.name, newName)\n\tuassert.Equal(t, edited.description, newDesc)\n\tuassert.Equal(t, edited.link, newLink)\n\tuassert.True(t, edited.startTime.Equal(newST))\n\tuassert.True(t, edited.endTime.Equal(newET))\n\n\t// Check if the old values are the same\n\tuassert.Equal(t, edited.location, loc)\n}\n\nfunc TestInvalidEdit(cur realm, t *testing.T) {\n\ttesting.SetRealm(suRealm)\n\n\tuassert.AbortsWithMessage(t, cur, ErrNoSuchID.Error(), func() {\n\t\tEditEvent(cross(cur), \"123123\", \"\", \"\", \"\", \"\", \"\", \"\")\n\t})\n}\n\nfunc TestParseTimes(t *testing.T) {\n\t// times not provided\n\t// end time before start time\n\t// timezone Missmatch\n\n\t_, _, err := parseTimes(\"\", \"\")\n\tuassert.ErrorContains(t, err, ErrInvalidStartTime.Error())\n\n\t_, _, err = parseTimes(now, \"\")\n\tuassert.ErrorContains(t, err, ErrInvalidEndTime.Error())\n\n\t_, _, err = parseTimes(\"2009-02-13T23:30:30Z\", \"2009-02-13T21:30:30Z\")\n\tuassert.ErrorContains(t, err, ErrEndBeforeStart.Error())\n\n\t_, _, err = parseTimes(\"2009-02-10T23:30:30+02:00\", \"2009-02-13T21:30:33+05:00\")\n\tuassert.ErrorContains(t, err, ErrStartEndTimezonemMismatch.Error())\n}\n\nfunc TestRenderEventWidget(cur realm, t *testing.T) {\n\ttesting.SetRealm(suRealm)\n\n\t// No events yet\n\tevents = nil\n\tout, err := RenderEventWidget(1)\n\tuassert.NoError(t, err)\n\tuassert.Equal(t, out, \"No events.\")\n\n\t// Ordering \u0026 if requested amt is larger than the num of events that exist\n\te1Start := parsedTimeNow.Add(time.Hour * 24 * 5)\n\te1End := e1Start.Add(time.Hour * 4)\n\n\te2Start := parsedTimeNow.Add(time.Hour * 24 * 10) // event 2 is after event 1\n\te2End := e2Start.Add(time.Hour * 4)\n\n\t_, err = AddEvent(cross(cur), \"Event 1\", \"description\", \"gno.land\", \"loc\", e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339))\n\turequire.NoError(t, err)\n\n\t_, err = AddEvent(cross(cur), \"Event 2\", \"description\", \"gno.land\", \"loc\", e2Start.Format(time.RFC3339), e2End.Format(time.RFC3339))\n\turequire.NoError(t, err)\n\n\t// Too many events (must be checked after adding events, otherwise\n\t// RenderEventWidget returns early with \"No events.\")\n\tout, err = RenderEventWidget(MaxWidgetSize + 1)\n\tuassert.ErrorIs(t, err, ErrMaxWidgetSize)\n\n\t// Too few events\n\tout, err = RenderEventWidget(0)\n\tuassert.ErrorIs(t, err, ErrMinWidgetSize)\n\n\tout, err = RenderEventWidget(MaxWidgetSize)\n\turequire.NoError(t, err)\n\n\tuniqueSequence := \"- [\" // sequence that is displayed once per each event as per the RenderEventWidget function\n\tuassert.Equal(t, 2, strings.Count(out, uniqueSequence))\n\n\tuassert.True(t, strings.Index(out, \"Event 1\") \u003e strings.Index(out, \"Event 2\"))\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/devrels/events\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"render.gno","body":"package events\n\nimport (\n\t\"bytes\"\n\n\t\"time\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst (\n\tMaxWidgetSize = 5\n)\n\n// RenderEventWidget shows up to eventsToRender of the latest events to a caller\nfunc RenderEventWidget(eventsToRender int) (string, error) {\n\tnumOfEvents := len(events)\n\tif numOfEvents == 0 {\n\t\treturn \"No events.\", nil\n\t}\n\n\tif eventsToRender \u003e MaxWidgetSize {\n\t\treturn \"\", ErrMaxWidgetSize\n\t}\n\n\tif eventsToRender \u003c 1 {\n\t\treturn \"\", ErrMinWidgetSize\n\t}\n\n\tif eventsToRender \u003e numOfEvents {\n\t\teventsToRender = numOfEvents\n\t}\n\n\toutput := \"\"\n\n\tfor _, event := range events[:eventsToRender] {\n\t\toutput += ufmt.Sprintf(\"- [%s](%s)\\n\", event.name, event.link)\n\t}\n\n\treturn output, nil\n}\n\n// renderHome renders the home page of the events realm\nfunc renderHome(admin bool) string {\n\toutput := \"# gno.land events\\n\\n\"\n\n\tif len(events) == 0 {\n\t\toutput += \"No upcoming or past events.\"\n\t\treturn output\n\t}\n\n\toutput += \"Below is a list of all gno.land events, including in progress, upcoming, and past ones.\\n\\n\"\n\toutput += \"---\\n\\n\"\n\n\tvar (\n\t\tinProgress []string\n\t\tupcoming   []string\n\t\tpast       []string\n\t\tnow        = time.Now()\n\t)\n\n\tfor _, e := range events {\n\t\tif now.Before(e.startTime) {\n\t\t\tupcoming = append(upcoming, e.Render(admin))\n\t\t} else if now.After(e.endTime) {\n\t\t\tpast = append(past, e.Render(admin))\n\t\t} else {\n\t\t\tinProgress = append(inProgress, e.Render(admin))\n\t\t}\n\t}\n\n\tif len(upcoming) != 0 {\n\t\t// Add upcoming events\n\t\toutput += \"## Upcoming events\\n\\n\"\n\t\toutput += md.ColumnsN(upcoming, 3, true)\n\t\toutput += \"---\\n\\n\"\n\t}\n\n\tif len(inProgress) != 0 {\n\t\toutput += \"## Currently in progress\\n\\n\"\n\t\toutput += md.ColumnsN(inProgress, 3, true)\n\t\toutput += \"---\\n\\n\"\n\t}\n\n\tif len(past) != 0 {\n\t\t// Add past events\n\t\toutput += \"## Past events\\n\\n\"\n\t\toutput += md.ColumnsN(past, 3, true)\n\t}\n\n\treturn output\n}\n\n// Render returns the markdown representation of a single event instance\nfunc (e Event) Render(admin bool) string {\n\tvar buf bytes.Buffer\n\n\tbuf.WriteString(ufmt.Sprintf(\"### %s\\n\\n\", e.name))\n\tbuf.WriteString(ufmt.Sprintf(\"%s\\n\\n\", e.description))\n\tbuf.WriteString(ufmt.Sprintf(\"**Location:** %s\\n\\n\", e.location))\n\n\t_, offset := e.startTime.Zone() // offset is in seconds\n\thoursOffset := offset / (60 * 60)\n\tsign := \"\"\n\tif offset \u003e= 0 {\n\t\tsign = \"+\"\n\t}\n\n\tbuf.WriteString(ufmt.Sprintf(\"**Starts:** %s UTC%s%d\\n\\n\", e.startTime.Format(\"02 Jan 2006, 03:04 PM\"), sign, hoursOffset))\n\tbuf.WriteString(ufmt.Sprintf(\"**Ends:** %s UTC%s%d\\n\\n\", e.endTime.Format(\"02 Jan 2006, 03:04 PM\"), sign, hoursOffset))\n\n\tif admin {\n\t\tbuf.WriteString(ufmt.Sprintf(\"[EDIT](/r/devrels/events$help\u0026func=EditEvent\u0026id=%s)\\n\\n\", e.id))\n\t\tbuf.WriteString(ufmt.Sprintf(\"[DELETE](/r/devrels/events$help\u0026func=DeleteEvent\u0026id=%s)\\n\\n\", e.id))\n\t}\n\n\tif e.link != \"\" {\n\t\tbuf.WriteString(ufmt.Sprintf(\"[See more](%s)\\n\\n\", e.link))\n\t}\n\n\treturn buf.String()\n}\n\n// Render is the main rendering entry point\nfunc Render(path string) string {\n\tif path == \"admin\" {\n\t\treturn renderHome(true)\n\t}\n\n\treturn renderHome(false)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"nad2a6yDVHZLdKRkztLr+yPeAsIcAWmVpRz+AjqpVkmJ+5olfIAqhrAjibqlGzywSKSTvE1R4DffDEnGbtRYDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"blog","path":"gno.land/r/gnoland/blog","files":[{"name":"admin.gno","body":"package blog\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/gov/dao\"\n)\n\nvar (\n\terrNotAdmin     = errors.New(\"access restricted: not admin\")\n\terrNotModerator = errors.New(\"access restricted: not moderator\")\n\terrNotCommenter = errors.New(\"access restricted: not commenter\")\n)\n\nvar (\n\tadminAddr     = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\") // govdao t1 multisig\n\tmoderatorList = bptree.NewBPTree32()\n\tcommenterList = bptree.NewBPTree32()\n\tinPause       bool\n)\n\nfunc AdminSetAdminAddr(_ realm, addr address) {\n\tassertIsAdmin()\n\tadminAddr = addr\n}\n\nfunc AdminSetInPause(_ realm, state bool) {\n\tassertIsAdmin()\n\tinPause = state\n}\n\nfunc AdminAddModerator(_ realm, addr address) {\n\tassertIsAdmin()\n\tmoderatorList.Set(addr.String(), true)\n}\n\nfunc AdminRemoveModerator(_ realm, addr address) {\n\tassertIsAdmin()\n\tmoderatorList.Set(addr.String(), false) // entry kept as a revocation record; isModerator checks the value\n}\n\nfunc NewPostProposalRequest(cur realm, slug, title, body, publicationDate, authors, tags string) dao.ProposalRequest {\n\tcaller := cur.Previous().Address()\n\te := dao.NewSimpleExecutor(0, cur,\n\t\tfunc(realm) error {\n\t\t\taddPost(caller, slug, title, body, publicationDate, authors, tags)\n\n\t\t\treturn nil\n\t\t},\n\t\tufmt.Sprintf(\"- Post Title: %v\\n- Post Publication Date: %v\\n- Authors: %v\\n- Tags: %v\", title, publicationDate, authors, tags),\n\t)\n\n\treturn dao.NewProposalRequest(\n\t\t\"Add new post to gnoland blog\",\n\t\t\"This propoposal is looking to add a new post to gnoland blog\",\n\t\te,\n\t)\n}\n\nfunc ModAddPost(_ realm, slug, title, body, publicationDate, authors, tags string) {\n\tassertIsModerator()\n\tcaller := unsafe.OriginCaller()\n\taddPost(caller, slug, title, body, publicationDate, authors, tags)\n}\n\nfunc addPost(caller address, slug, title, body, publicationDate, authors, tags string) {\n\tvar tagList []string\n\tif tags != \"\" {\n\t\ttagList = strings.Split(tags, \",\")\n\t}\n\tvar authorList []string\n\tif authors != \"\" {\n\t\tauthorList = strings.Split(authors, \",\")\n\t}\n\n\terr := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)\n\n\tcheckErr(err)\n}\n\nfunc ModEditPost(_ realm, slug, title, body, publicationDate, authors, tags string) {\n\tassertIsModerator()\n\ttagList := strings.Split(tags, \",\")\n\tauthorList := strings.Split(authors, \",\")\n\n\terr := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)\n\tcheckErr(err)\n}\n\nfunc ModRemovePost(_ realm, slug string) {\n\tassertIsModerator()\n\tb.RemovePost(slug)\n}\n\nfunc ModAddCommenter(_ realm, addr address) {\n\tassertIsModerator()\n\tcommenterList.Set(addr.String(), true)\n}\n\nfunc ModDelCommenter(_ realm, addr address) {\n\tassertIsModerator()\n\tcommenterList.Set(addr.String(), false) // entry kept as a revocation record; isCommenter checks the value\n}\n\nfunc ModDelComment(_ realm, slug string, index int) {\n\tassertIsModerator()\n\terr := b.GetPost(slug).DeleteComment(index)\n\tcheckErr(err)\n}\n\nfunc isAdmin(addr address) bool {\n\treturn addr == adminAddr\n}\n\nfunc isModerator(addr address) bool {\n\t// Removed moderators stay in the list with a false value, so the\n\t// stored value must be checked, not just key presence.\n\tactive, _ := moderatorList.Get(addr.String()).(bool)\n\treturn active\n}\n\nfunc isCommenter(addr address) bool {\n\t// Removed commenters stay in the list with a false value, so the\n\t// stored value must be checked, not just key presence.\n\tactive, _ := commenterList.Get(addr.String()).(bool)\n\treturn active\n}\n\nfunc assertIsAdmin() {\n\tcaller := unsafe.OriginCaller()\n\tif !isAdmin(caller) {\n\t\tpanic(errNotAdmin.Error())\n\t}\n}\n\nfunc assertIsModerator() {\n\tcaller := unsafe.OriginCaller()\n\tif isAdmin(caller) || isModerator(caller) {\n\t\treturn\n\t}\n\tpanic(errNotModerator.Error())\n}\n\nfunc assertIsCommenter() {\n\tcaller := unsafe.OriginCaller()\n\tif isAdmin(caller) || isModerator(caller) || isCommenter(caller) {\n\t\treturn\n\t}\n\tpanic(errNotCommenter.Error())\n}\n\nfunc assertNotInPause() {\n\tif inPause {\n\t\tpanic(\"access restricted (pause)\")\n\t}\n}\n"},{"name":"admin_test.gno","body":"package blog\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/blog\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\n// clearState wipes the global state between test calls\nfunc clearState(t *testing.T) {\n\tt.Helper()\n\n\tb = \u0026blog.Blog{\n\t\tTitle:  \"Gno.land's blog\",\n\t\tPrefix: \"/r/gnoland/blog:\",\n\t}\n\tadminAddr = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\tinPause = false\n\tmoderatorList = bptree.NewBPTree32()\n\tcommenterList = bptree.NewBPTree32()\n}\n\nfunc TestBlog_AdminControls(cur realm, t *testing.T) {\n\tt.Run(\"non-admin call\", func(t *testing.T) {\n\t\tclearState(t)\n\n\t\tnonAdmin := testutils.TestAddress(\"bob\")\n\n\t\ttesting.SetOriginCaller(nonAdmin)\n\t\ttesting.SetRealm(testing.NewUserRealm(nonAdmin))\n\n\t\tuassert.AbortsWithMessage(t, cur, errNotAdmin.Error(), func() {\n\t\t\tAdminSetInPause(cross(cur), true)\n\t\t})\n\t})\n\n\tt.Run(\"pause toggled\", func(t *testing.T) {\n\t\tclearState(t)\n\n\t\ttesting.SetOriginCaller(adminAddr)\n\t\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\n\t\tuassert.NotAborts(t, cur, func() {\n\t\t\tAdminSetInPause(cross(cur), true)\n\t\t})\n\n\t\tuassert.True(t, inPause)\n\t})\n\n\tt.Run(\"admin set\", func(t *testing.T) {\n\t\tclearState(t)\n\n\t\t// Set the new admin\n\t\tvar (\n\t\t\toldAdmin = adminAddr\n\t\t\tnewAdmin = testutils.TestAddress(\"alice\")\n\t\t)\n\n\t\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\t\tAdminSetAdminAddr(cross(cur), newAdmin)\n\n\t\tuassert.Equal(t, adminAddr, newAdmin)\n\n\t\t// Make sure the old admin can't do anything\n\t\ttesting.SetOriginCaller(oldAdmin)\n\t\ttesting.SetRealm(testing.NewUserRealm(oldAdmin))\n\n\t\tuassert.AbortsWithMessage(t, cur, errNotAdmin.Error(), func() {\n\t\t\tAdminSetInPause(cross(cur), false)\n\t\t})\n\t})\n}\n\nfunc TestBlog_AddRemoveModerator(cur realm, t *testing.T) {\n\tclearState(t)\n\n\tmod := testutils.TestAddress(\"mod\")\n\n\t// Add the moderator\n\ttesting.SetOriginCaller(adminAddr)\n\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\tAdminAddModerator(cross(cur), mod)\n\n\turequire.True(t, moderatorList.Has(mod.String()))\n\n\t// Remove the moderator\n\tAdminRemoveModerator(cross(cur), mod)\n\n\t// Make sure the moderator is disabled\n\tisMod := moderatorList.Get(mod.String())\n\tuassert.NotNil(t, isMod)\n\n\tuassert.False(t, isMod.(bool))\n}\n\nfunc TestBlog_AddCommenter(cur realm, t *testing.T) {\n\tclearState(t)\n\n\tvar (\n\t\tmod       = testutils.TestAddress(\"mod\")\n\t\tcommenter = testutils.TestAddress(\"comm\")\n\t\trand      = testutils.TestAddress(\"rand\")\n\t)\n\n\t// Appoint the moderator\n\ttesting.SetOriginCaller(adminAddr)\n\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\tAdminAddModerator(cross(cur), mod)\n\n\t// Add a commenter as a mod\n\ttesting.SetOriginCaller(mod)\n\ttesting.SetRealm(testing.NewUserRealm(mod))\n\tModAddCommenter(cross(cur), commenter)\n\n\tuassert.True(t, commenterList.Has(commenter.String()))\n\n\t// Make sure a non-mod can't add commenters\n\ttesting.SetOriginCaller(rand)\n\ttesting.SetRealm(testing.NewUserRealm(rand))\n\n\tuassert.AbortsWithMessage(t, cur, errNotModerator.Error(), func() {\n\t\tModAddCommenter(cross(cur), testutils.TestAddress(\"evil\"))\n\t})\n\n\t// Remove a commenter\n\ttesting.SetOriginCaller(mod)\n\ttesting.SetRealm(testing.NewUserRealm(mod))\n\tModDelCommenter(cross(cur), commenter)\n\n\tactive := commenterList.Get(commenter.String())\n\tuassert.False(t, active.(bool))\n}\n\nfunc TestBlog_ManagePost(cur realm, t *testing.T) {\n\tclearState(t)\n\n\tvar (\n\t\tmod  = testutils.TestAddress(\"mod\")\n\t\tslug = \"slug\"\n\t)\n\n\t// Appoint the moderator\n\ttesting.SetOriginCaller(adminAddr)\n\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\tAdminAddModerator(cross(cur), mod)\n\n\t// Add the post\n\ttesting.SetOriginCaller(mod)\n\ttesting.SetRealm(testing.NewUserRealm(mod))\n\tModAddPost(\n\t\tcross(cur),\n\t\tslug, \"title\", \"body\", \"2022-05-20T13:17:22Z\", \"moul\", \"tag\",\n\t)\n\n\t// Make sure the post is present\n\tuassert.NotNil(t, b.GetPost(slug))\n\n\t// Remove the post\n\tModRemovePost(cross(cur), slug)\n\tuassert.TypedNil(t, b.GetPost(slug))\n}\n\nfunc TestBlog_ManageComment(cur realm, t *testing.T) {\n\tclearState(t)\n\n\tvar (\n\t\tslug = \"slug\"\n\n\t\tmod       = testutils.TestAddress(\"mod\")\n\t\tcommenter = testutils.TestAddress(\"comm\")\n\t)\n\n\t// Appoint the moderator\n\ttesting.SetOriginCaller(adminAddr)\n\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\tAdminAddModerator(cross(cur), mod)\n\n\t// Add a commenter as a mod\n\ttesting.SetOriginCaller(mod)\n\ttesting.SetRealm(testing.NewUserRealm(mod))\n\tModAddCommenter(cross(cur), commenter)\n\n\tuassert.True(t, commenterList.Has(commenter.String()))\n\n\t// Add the post\n\ttesting.SetOriginCaller(mod)\n\ttesting.SetRealm(testing.NewUserRealm(mod))\n\tModAddPost(\n\t\tcross(cur),\n\t\tslug, \"title\", \"body\", \"2022-05-20T13:17:22Z\", \"moul\", \"tag\",\n\t)\n\n\t// Make sure the post is present\n\tuassert.NotNil(t, b.GetPost(slug))\n\n\t// Add the comment\n\ttesting.SetOriginCaller(commenter)\n\ttesting.SetRealm(testing.NewUserRealm(commenter))\n\tuassert.NotAborts(t, cur, func() {\n\t\tAddComment(cross(cur), slug, \"comment\")\n\t})\n\n\t// Delete the comment\n\ttesting.SetOriginCaller(mod)\n\ttesting.SetRealm(testing.NewUserRealm(mod))\n\tuassert.NotAborts(t, cur, func() {\n\t\tModDelComment(cross(cur), slug, 0)\n\t})\n}\n"},{"name":"gnoblog.gno","body":"package blog\n\nimport (\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/demo/blog\"\n)\n\nvar b = \u0026blog.Blog{\n\tTitle:  \"Gno.land's blog\",\n\tPrefix: \"/r/gnoland/blog:\",\n}\n\nfunc AddComment(_ realm, postSlug, comment string) {\n\tassertIsCommenter()\n\tassertNotInPause()\n\n\tcaller := unsafe.OriginCaller()\n\terr := b.GetPost(postSlug).AddComment(caller, comment)\n\tcheckErr(err)\n}\n\nfunc Render(path string) string {\n\treturn b.Render(path)\n}\n\nfunc RenderLastPostsWidget(limit int) string {\n\treturn b.RenderLastPostsWidget(limit)\n}\n\nfunc PostExists(slug string) bool {\n\tif b.GetPost(slug) == nil {\n\t\treturn false\n\t}\n\treturn true\n}\n"},{"name":"gnoblog_test.gno","body":"package blog\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestPackage(cur realm, t *testing.T) {\n\tclearState(t)\n\n\ttesting.SetOriginCaller(adminAddr)\n\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\n\t// by default, no posts.\n\t{\n\t\tgot := Render(\"\")\n\t\texpected := `\n# Gno.land's blog\n\nNo posts.\n`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t// create two posts, list post.\n\t{\n\t\tModAddPost(cross(cur), \"slug1\", \"title1\", \"body1\", \"2022-05-20T13:17:22Z\", \"moul\", \"tag1,tag2\")\n\t\tModAddPost(cross(cur), \"slug2\", \"title2\", \"body2\", \"2022-05-20T13:17:23Z\", \"moul\", \"tag1,tag3\")\n\t\tgot := Render(\"\")\n\t\texpected := `\n\t\t\t# Gno.land's blog\n\n\u003cgno-columns\u003e\n### [title2](/r/gnoland/blog:p/slug2)\n20 May 2022\n\n\u003cgno-columns-sep\u003e\n\n### [title1](/r/gnoland/blog:p/slug1)\n20 May 2022\n\n\u003cgno-columns-sep\u003e\n\u003c/gno-columns\u003e\n`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t// view post.\n\t{\n\t\tgot := Render(\"p/slug2\")\n\t\texpected := `\n\t\u003cmain class='gno-tmpl-page'\u003e\n\n# title2\n\nbody2\n\n---\n\nTags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)\n\nWritten by moul on 20 May 2022\n\nPublished by g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh to Gno.land's blog\n\n---\n\u003cdetails\u003e\u003csummary\u003eComment section\u003c/summary\u003e\n\n\u003c/details\u003e\n\u003c/main\u003e\n\t\n\t\t`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t// list by tags.\n\t{\n\t\tgot := Render(\"t/invalid\")\n\t\texpected := \"# [Gno.land's blog](/r/gnoland/blog:) / t / invalid\\n\\nNo posts.\"\n\t\tassertMDEquals(t, got, expected)\n\n\t\tgot = Render(\"t/tag2\")\n\t\texpected = `\n# [Gno.land's blog](/r/gnoland/blog:) / t / tag2\n\n\n### [title1](/r/gnoland/blog:p/slug1)\n20 May 2022\n\t\t`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t// add comments.\n\t{\n\t\tAddComment(cross(cur), \"slug1\", \"comment1\")\n\t\tAddComment(cross(cur), \"slug2\", \"comment2\")\n\t\tAddComment(cross(cur), \"slug1\", \"comment3\")\n\t\tAddComment(cross(cur), \"slug2\", \"comment4\")\n\t\tAddComment(cross(cur), \"slug1\", \"comment5\")\n\t\tgot := Render(\"p/slug2\")\n\t\texpected := `\u003cmain class='gno-tmpl-page'\u003e\n\n# title2\n\nbody2\n\n---\n\nTags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)\n\nWritten by moul on 20 May 2022\n\nPublished by g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh to Gno.land's blog\n\n---\n\u003cdetails\u003e\u003csummary\u003eComment section\u003c/summary\u003e\n\n\u003ch5\u003ecomment4\n\n\u003c/h5\u003e\u003ch6\u003eby g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh on 13 Feb 09 23:31 UTC\u003c/h6\u003e\n\n---\n\n\u003ch5\u003ecomment2\n\n\u003c/h5\u003e\u003ch6\u003eby g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh on 13 Feb 09 23:31 UTC\u003c/h6\u003e\n\n---\n\n\u003c/details\u003e\n\u003c/main\u003e\n\n\t\t`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t// edit post.\n\t{\n\t\toldTitle := \"title2\"\n\t\toldDate := \"2022-05-20T13:17:23Z\"\n\n\t\tModEditPost(cur, \"slug2\", oldTitle, \"body2++\", oldDate, \"manfred\", \"tag1,tag4\")\n\t\tgot := Render(\"p/slug2\")\n\t\texpected := `\u003cmain class='gno-tmpl-page'\u003e\n\n# title2\n\nbody2++\n\n---\n\nTags: [#tag1](/r/gnoland/blog:t/tag1) [#tag4](/r/gnoland/blog:t/tag4)\n\nWritten by manfred on 20 May 2022\n\nPublished by g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh to Gno.land's blog\n\n---\n\u003cdetails\u003e\u003csummary\u003eComment section\u003c/summary\u003e\n\n\u003ch5\u003ecomment4\n\n\u003c/h5\u003e\u003ch6\u003eby g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh on 13 Feb 09 23:31 UTC\u003c/h6\u003e\n\n---\n\n\u003ch5\u003ecomment2\n\n\u003c/h5\u003e\u003ch6\u003eby g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh on 13 Feb 09 23:31 UTC\u003c/h6\u003e\n\n---\n\n\u003c/details\u003e\n\u003c/main\u003e\n\n\t\t`\n\t\tassertMDEquals(t, got, expected)\n\n\t\thome := Render(\"\")\n\n\t\tif strings.Count(home, oldTitle) != 1 {\n\t\t\tt.Errorf(\"post not edited properly\")\n\t\t}\n\t\t// Edits work everything except title, slug, and publicationDate\n\t\t// Edits to the above will cause duplication on the blog home page\n\t}\n\t//\n\t{ // Test remove functionality\n\t\ttitle := \"example title\"\n\t\tslug := \"testSlug1\"\n\t\tModAddPost(cross(cur), slug, title, \"body1\", \"2022-05-25T13:17:22Z\", \"moul\", \"tag1,tag2\")\n\n\t\tgot := Render(\"\")\n\n\t\tif !strings.Contains(got, title) {\n\t\t\tt.Errorf(\"post was not added properly\")\n\t\t}\n\n\t\tpostRender := Render(\"p/\" + slug)\n\n\t\tif !strings.Contains(postRender, title) {\n\t\t\tt.Errorf(\"post not rendered properly\")\n\t\t}\n\n\t\tModRemovePost(cur, slug)\n\t\tgot = Render(\"\")\n\n\t\tif strings.Contains(got, title) {\n\t\t\tt.Errorf(\"post was not removed\")\n\t\t}\n\n\t\tpostRender = Render(\"p/\" + slug)\n\n\t\tassertMDEquals(t, postRender, \"404\")\n\t}\n\t//\n\t//\t// TODO: pagination.\n\t//\t// TODO: ?format=...\n\t//\n\t// all 404s\n\t{\n\t\tnotFoundPaths := []string{\n\t\t\t\"p/slug3\",\n\t\t\t\"p\",\n\t\t\t\"p/\",\n\t\t\t\"x/x\",\n\t\t\t\"t\",\n\t\t\t\"t/\",\n\t\t\t\"/\",\n\t\t\t\"p/slug1/\",\n\t\t}\n\t\tfor _, notFoundPath := range notFoundPaths {\n\t\t\tgot := Render(notFoundPath)\n\t\t\texpected := \"404\"\n\t\t\tif got != expected {\n\t\t\t\tt.Errorf(\"path %q: expected %q, got %q.\", notFoundPath, expected, got)\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc assertMDEquals(t *testing.T, got, expected string) {\n\tt.Helper()\n\texpected = strings.TrimSpace(expected)\n\tgot = strings.TrimSpace(got)\n\tif expected != got {\n\t\tt.Errorf(\"invalid render output.\\nexpected %q.\\ngot      %q.\", expected, got)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/blog\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"util.gno","body":"package blog\n\nfunc checkErr(err error) {\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"R9lBKUb27enO8i2z391rcDAOpfIWuPwbapZHvxQLvLMioCGi699XhWqN0wlR0YgmRR0Dii5y5xgHaKHyhg9/Ag=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"boards2","path":"gno.land/r/gnoland/boards2/v1","files":[{"name":"README.md","body":"# Boards2\n\nBoards2 is a social discussion forum for open communication and community-driven conversations.\n\nUsers can start discussions by creating or reposting threads and then submitting comments or replies to\nother user comments.\n\nDiscussions happen within different boards, where each board is an independent self managed community.\n\nBoards2 allows users to create two types of boards, one is the invite only board where only invited users\ncan create threads and comment, and where non invited users can only read the content and discussions; The\nother type of board is the open board where any user with a specific amount of GNOT in their account can\ncreate threads, repost and comment.\n\n## Open Boards Quick Start\n\nIf you are new to Gno.land in general, the quick start guide below can help you get started.\n\nWhat you need to create threads and start commenting within open boards is having a specific amount of GNOT\nin your Gno.land user account, which by default initially is 3000 GNOT. This initial GNOT amount could be\nchanged over time to a different amount, so this requirement can change.\n\n### How To Get a Gno.land Address\n\nTo use Boards2 you'll need a Gno.land address. You can quickly setup your account using [Adena] or any\nGno.land compatible wallet by following these steps:\n\n- Download [Adena], or a Gno.land compatible wallet\n- Once installed, you have to create a new account or add an existing one following wallet's instructions\n- If you don't have GNOT you will need to use a faucet to get some, if the network allows it\n\nFor testing networks you can use the official [Faucet Hub] to receive GNOT in your account.\n\n### How to Start Using Open Boards\n\nOnce you have the required GNOT amount in your account you can start commenting, creating and reposting\nthreads within any open board.\n\nTo comment and engage on an open board discussion visit a thread and click on the \"Comment\" link. You can\nalso reply to any of the thread's comments by clicking on the \"Reply\" link.\n\nTo create threads, visit an open board and then click on the \"Create Thread\" link, there you will have to\nenter a title and some content for the thread body.\n\nThread and comments content can be written as plaintext, or Markdown if you want to format the content so\nit's rendered as rich text.\n\nYou can also repost any thread, even the ones from invite only boards, into any open board. To do so visit\nthe thread you want to repost and click on the \"Repost\" link at the bottom of the thread, there you will have\nto enter the open board where you want the repost to be created, a title for the thread repost and optionally\nalso some content to render at the top of the repost. The optional content can also be written as plaintext\nor Markdown, like threads.\n\nAfter your thread, repost or comment is created, you can easily share the link with others so they can join\nthe discussion!\n\n## Boards\n\nBoards2 realm enables the creation of different communities though independent boards.\n\nWhen a board is created, and independetly of the board type, it initially has a single \"owner\" member\nassigned by default, which is the user that creates it. The member is called \"owner\" because by default it\nhas the `owner` role, which grants all permissions within that board.\n\nMembers of a board with the `owner` or `admin` role, independently of the board type, can invite other\nmembers, or otherwise users can request being invited to be a member by visiting the board and clicking the\n\"Request Invite\" link. Requested invites can be accepted or revoked though the board's \"Invite Requests\" view\nor using these public realm functions:\n\n```go\n// AcceptInvite accepts a board invite request.\nfunc AcceptInvite(_ realm, boardID boards.ID, user address)\n\n// RevokeInvite revokes a board invite request\nfunc RevokeInvite(_ realm, boardID boards.ID, user address)\n```\n\nThere are four possible roles that invited users can have when they are members of a board:\n- `owner`: Grants all available permissions\n- `admin`: Grants basic, moderator and advanced permissions, like being able to rename boards, add or remove\n   members, or change their role.\n- `moderator`: Grants basic and moderation related permissions, like being able to ban or unban users, or\n  flag content.\n- `guest`: Grants basic permissions that allow creating threads, reposting and commenting.\n\nDefault board configuration, permissions and roles are defined in the [permissions file].\n\nNo roles or number of members is enforced for boards, so technically a board can be updated to have no\nmembers, or for example, boards could exists without any \"owner\" if all members with `owner` role are removed\nfrom it.\n\nOther custom user defined roles can exists on top of the default ones though [custom board] implementations.\n\n### Custom Boards\n\nBoards2 realm allows users to customize the mechanics of their boards when the default ones doesn't make\nsense to that community, or when users want to integrate a board with their realms.\n\nAn example of this would be a case where thread creation should be allowed only though a new `publisher`\nrole, or a case where a community have their own DAO realm and governance implementation and are looking to\nintegrate it into their board mechanics by creating threads though proposals that must be approved for the\nthread to be published.\n\nEach board can customize the way it works by implementing the [Permissions] interface that is defined in the\n[gno.land/p/gnoland/boards] package. It is though the implementation of that interface within a new realm\nthat the default board mechanics can be customized. The new realm can then be used to create an instance of\na custom `Permissions` implementation to replace the one assigned by default to a board.\n\nRight now only Boards2 realm `owner` members are allowed to change default board permissions using a public\nrealm function:\n\n```go\n// SetPermissions sets a permissions implementation for boards2 realm or a board\nfunc SetPermissions(_ realm, boardID boards.ID, p boards.Permissions)\n```\n\n\u003e This function will be replaced by a proposal that would need to pass for the custom permissions to be\n\u003e applied to a board once Boards2 governance is implemented.\n\n`Permissions` implementation allow communities to customize the way they want to manage users and roles,\nwhere or how they should be stored, and the requirements or effects different board actions have.\n\nBoards2 provides a custom `Permissions` implementation in [gno.land/r/gnoland/boards2/v1/permissions] that\ncan be imported by realms and used to implement custom boards.\n\n### Boards Governance\n\nBy default boards are created with an undelying DAO, so each new board is linked to an independent DAO which\nis used to organize members by role, and can also be used to update boards in a permissionless manner.\n\nRight now is possible to integrate with the underlying DAO and change the default board mechanics to rely on\nproposals using a [custom board] implementation, by creating a new realm that imports and uses the\n[gno.land/r/gnoland/boards2/v1/permissions] realm, which exposes the underlying DAO.\n\n\u003e Current Boards2 realm implementation doesn't run proposals, but some of the current mechanics will rely on\n\u003e DAO proposals to actually execute changes.\n\n## Moderation\n\n### Flagging\n\nThreads and comments are moderated by flagging, which requires the `moderator`, `admin` or `owner` roles.\n\nA reason is required each time content is flagged by a member. Content is replaced by a feedback message\nand a link to the list of flagging reasons given by moderators when a moderation flagging threshold is\nreached. By default the threshold is of a single flag.\n\n\u003e Right now is not possible to show the content of a thread or comment that has been hidden because of\n\u003e moderation, but future Boards2 versions might implement a way to handle moderation disputes and allow\n\u003e restoring the thread or comment content.\n\n\u003e Boards2 realm `owners` are allowed to moderate content with a single flag within any board at this point,\n\u003e but this might be changed to work though a DAO proposal.\n\nEach board's `owner` or `admin` members are free to change the flagging threshold within a single board to a\ngreater value using a public realm function:\n\n```go\n// SetFlaggingThreshold sets the number of flags required to hide a thread or comment\nfunc SetFlaggingThreshold(_ realm, boardID boards.ID, threshold int)\n```\n\n### Banning\n\nMembers with the `moderator`, `admin` or `owner` roles are the only ones that are allowed to ban or unban\na user within a board.\n\nUsers can be banned with a reason for any number of hours. Within this period banned users are not allowed\nto interact or make any changes.\n\nOnly invited `guest` members and open board users can be banned, banning board owners, admins and moderators\nis not allowed.\n\nBanning and unbanning can be done by calling these public realm functions:\n\n```go\n// Ban bans a user from a board for a period of time\nfunc Ban(_ realm, boardID boards.ID, user address, hours uint, reason string)\n\n// Unban unbans a user from a board\nfunc Unban(_ realm, boardID boards.ID, user address, reason string)\n```\n\n## Freezing\n\nBoards2 realm allows `owner` or `admin` members of a board to freeze the board or any of its threads.\nFreezing makes the board or thread readonly, disallowing any changes or additions until unfrozen.\n\nThe following public realm function can be called for freezing:\n\n```go\n// FreezeBoard freezes a board so no more threads and comments can be created or modified\nfunc FreezeBoard(_ realm, boardID boards.ID)\n\n// UnfreezeBoard removes frozen status from a board\nfunc UnfreezeBoard(_ realm, boardID boards.ID)\n\n// FreezeThread freezes a thread so thread cannot be replied, modified or deleted\nfunc FreezeThread(_ realm, boardID, threadID boards.ID)\n\n// UnfreezeThread removes frozen status from a thread\nfunc UnfreezeThread(_ realm, boardID, threadID boards.ID)\n```\n\n\n[permissions file]: https://gno.land/r/gnoland/boards2/v1$source\u0026file=permissions.gno\n[gno.land/r/gnoland/boards2/v1/permissions]: https://gno.land/r/gnoland/boards2/v1/permissions/\n[custom board]: #custom-boards\n[Adena]: https://www.adena.app/\n[Faucet Hub]: https://faucet.gno.land/\n[gno.land/p/gnoland/boards]: https://gno.land/p/gnoland/boards\n[Permissions]: https://gno.land/p/gnoland/boards$source\u0026file=permissions.gno#L23\n"},{"name":"boards.gno","body":"package boards2\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/gnoland/boards/exts/permissions\"\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\nconst (\n\trealmPkgPath = \"gno.land/r/gnoland/boards2/v1\"\n\tgRealmPath   = \"/r/gnoland/boards2/v1\"\n)\n\nvar (\n\t// RealmLink contains Boards2 realm link.\n\t// It can be used to generate board TX links from other realms.\n\tRealmLink = txlink.Realm(realmPkgPath)\n\n\t// RequiredAccountAmount contains the required account amount for open board interactions.\n\t// The amount requirement is not applied to members that were invited to an open board.\n\t// Amount is defined as ugnot.\n\tRequiredAccountAmount = int64(3_000_000_000)\n\n\t// Notice contains an optional message that is displayed globally within the realm.\n\tNotice string\n\n\t// Help contains optional Markdown with Boards2 realm help.\n\tHelp string\n)\n\n// TODO: Refactor globals in favor of a cleaner pattern\nvar (\n\tgListedBoardsByID bptree.BPTree // string(id) -\u003e *boards.Board\n\tgInviteRequests   bptree.BPTree // string(board id) -\u003e *bptree.BPTree(address -\u003e time.Time)\n\tgBannedUsers      bptree.BPTree // string(board id) -\u003e *bptree.BPTree(address -\u003e time.Time)\n\tgLocked           struct {\n\t\trealm        bool\n\t\trealmMembers bool\n\t}\n)\n\nvar (\n\tgBoards         = boards.NewStorage()\n\tgBoardsSequence = boards.NewIdentifierGenerator()\n\tgPerms          = initRealmPermissions(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\") // GovDAO T1 multisig\n)\n\n// initRealmPermissions returns the default realm permissions.\nfunc initRealmPermissions(owners ...address) boards.Permissions {\n\tperms := permissions.New(\n\t\tpermissions.UseSingleUserRole(),\n\t\tpermissions.WithSuperRole(RoleOwner),\n\t)\n\tperms.AddRole(RoleAdmin, PermissionBoardCreate)\n\tfor _, owner := range owners {\n\t\tperms.SetUserRoles(owner, RoleOwner)\n\t}\n\n\tperms.ValidateFunc(PermissionBoardCreate, validateBasicBoardCreate)\n\tperms.ValidateFunc(PermissionMemberInvite, validateBasicMemberInvite)\n\tperms.ValidateFunc(PermissionRoleChange, validateBasicRoleChange)\n\treturn perms\n}\n\n// getInviteRequests returns invite requests for a board.\nfunc getInviteRequests(boardID boards.ID) (_ *bptree.BPTree, found bool) {\n\tv := gInviteRequests.Get(boardID.Key())\n\tif v == nil {\n\t\treturn nil, false\n\t}\n\treturn v.(*bptree.BPTree), true\n}\n\n// getBannedUsers returns banned users within a board.\nfunc getBannedUsers(boardID boards.ID) (_ *bptree.BPTree, found bool) {\n\tv := gBannedUsers.Get(boardID.Key())\n\tif v == nil {\n\t\treturn nil, false\n\t}\n\treturn v.(*bptree.BPTree), true\n}\n\n// mustGetBoardByName returns a board or panics when it's not found.\nfunc mustGetBoardByName(name string) *boards.Board {\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tpanic(\"board does not exist with name: \" + name)\n\t}\n\treturn board\n}\n\n// mustGetBoard returns a board or panics when it's not found.\nfunc mustGetBoard(id boards.ID) *boards.Board {\n\tboard, found := gBoards.Get(id)\n\tif !found {\n\t\tpanic(\"board does not exist with ID: \" + id.String())\n\t}\n\treturn board\n}\n\n// getThread returns a board thread.\nfunc getThread(board *boards.Board, threadID boards.ID) (*boards.Post, bool) {\n\tthread, found := board.Threads.Get(threadID)\n\tif !found {\n\t\t// When thread is not found search it within hidden threads\n\t\tmeta := board.Meta.(*BoardMeta)\n\t\tthread, found = meta.HiddenThreads.Get(threadID)\n\t}\n\treturn thread, found\n}\n\n// getReply returns a thread comment or reply.\nfunc getReply(thread *boards.Post, replyID boards.ID) (*boards.Post, bool) {\n\tmeta := thread.Meta.(*ThreadMeta)\n\treturn meta.AllReplies.Get(replyID)\n}\n\n// mustGetThread returns a thread or panics when it's not found.\nfunc mustGetThread(board *boards.Board, threadID boards.ID) *boards.Post {\n\tthread, found := getThread(board, threadID)\n\tif !found {\n\t\tpanic(\"thread does not exist with ID: \" + threadID.String())\n\t}\n\treturn thread\n}\n\n// mustGetReply returns a reply or panics when it's not found.\nfunc mustGetReply(thread *boards.Post, replyID boards.ID) *boards.Post {\n\treply, found := getReply(thread, replyID)\n\tif !found {\n\t\tpanic(\"reply does not exist with ID: \" + replyID.String())\n\t}\n\treturn reply\n}\n\nfunc mustGetPermissions(bid boards.ID) boards.Permissions {\n\tif bid != 0 {\n\t\tboard := mustGetBoard(bid)\n\t\treturn board.Permissions\n\t}\n\treturn gPerms\n}\n\nfunc parseRealmPath(path string) *realmpath.Request {\n\t// Make sure request is using current realm path so paths can be parsed during Render\n\tr := realmpath.Parse(path)\n\tr.Realm = string(RealmLink)\n\treturn r\n}\n"},{"name":"doc.gno","body":"// Boards2 is a social discussion forum for open communication and community-driven conversations.\n//\n// Users can start discussions by creating or reposting threads and then submitting comments or replies\n// to other user comments.\n//\n// Discussions happen within different boards, where each board is an independent self managed community.\n//\n// Boards2 allows users to create two types of boards:\n// - Invite Only: Only invited users (members) can create threads, reposts and comments.\n// - Open: Anyone with a specific amount of GNOT in their account can create threads, reposts and comments.\npackage boards2\n"},{"name":"flag.gno","body":"package boards2\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\n// DefaultFlaggingThreshold defines the default number of flags that hides flaggable items.\nconst DefaultFlaggingThreshold = 1\n\nvar gFlaggingThresholds bptree.BPTree // string(board ID) -\u003e int\n\n// flagItem adds a flag to a post.\n// Returns whether flag count threshold is reached and post can be hidden.\n// Panics if flag count threshold was already reached.\nfunc flagItem(post *boards.Post, user address, reason string, threshold int) bool {\n\tif post.Flags.Size() \u003e= threshold {\n\t\tpanic(\"flag count threshold exceeded: \" + strconv.Itoa(threshold))\n\t}\n\n\tif post.Flags.Exists(user) {\n\t\tpanic(\"post has been already flagged by \" + user.String())\n\t}\n\n\tpost.Flags.Add(boards.Flag{\n\t\tUser:   user,\n\t\tReason: reason,\n\t})\n\n\treturn post.Flags.Size() == threshold\n}\n\nfunc getFlaggingThreshold(bid boards.ID) int {\n\tif v := gFlaggingThresholds.Get(bid.String()); v != nil {\n\t\treturn v.(int)\n\t}\n\treturn DefaultFlaggingThreshold\n}\n"},{"name":"format.gno","body":"package boards2\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/markdown/foreign/v0\"\n\n\t\"gno.land/r/sys/users\"\n)\n\nconst dateFormat = \"2006-01-02 3:04pm MST\"\n\nfunc padLeft(s string, length int) string {\n\tif len(s) \u003e= length {\n\t\treturn s\n\t}\n\treturn strings.Repeat(\" \", length-len(s)) + s\n}\n\nfunc padZero(u64 uint64, length int) string {\n\ts := strconv.Itoa(int(u64))\n\tif len(s) \u003e= length {\n\t\treturn s\n\t}\n\treturn strings.Repeat(\"0\", length-len(s)) + s\n}\n\nfunc indentBody(indent string, body string) string {\n\tvar (\n\t\tres   string\n\t\tlines = strings.Split(body, \"\\n\")\n\t)\n\tfor i, line := range lines {\n\t\tif i \u003e 0 {\n\t\t\t// Add two spaces to keep newlines within Markdown\n\t\t\tres += \"  \\n\"\n\t\t}\n\t\tres += indent + line\n\t}\n\treturn res\n}\n\n// indentForeignBody is the single \"render a user body\" operation: it\n// sandboxes the body in a \u003cgno-foreign\u003e block, indents it like\n// indentBody, AND charges one unit against the per-render budget — so\n// wrapping and budget-accounting can't drift apart (the *int signals\n// the mutation). The body renders inside the sandbox: its block\n// structure (headings, blockquotes, lists, columns, alerts) is\n// contained and cannot hijack realm chrome, so boards no longer needs\n// the write-time markdown blacklist. The opener survives the \"\u003e \"\n// comment indentation at any depth (goldmark strips the \"\u003e \" prefix\n// before the block parser runs).\nfunc indentForeignBody(indent, body string, budget *int) string {\n\t*budget-- // one \u003cgno-foreign\u003e block\n\treturn indentBody(indent, foreign.Foreign(body))\n}\n\nfunc summaryOf(text string, length int) string {\n\tlines := strings.SplitN(text, \"\\n\", 2)\n\tline := lines[0]\n\tif len(line) \u003e length {\n\t\tline = line[:(length-3)] + \"...\"\n\t} else if len(lines) \u003e 1 {\n\t\tline = line + \"...\"\n\t}\n\treturn line\n}\n\nfunc userLink(addr address) string {\n\tif u := users.ResolveAddress(addr); u != nil {\n\t\treturn md.UserLink(u.Name())\n\t}\n\treturn md.UserLink(addr.String())\n}\n\nfunc getRoleBadge(post *boards.Post) string {\n\tif post == nil || post.Board == nil || post.Board.Permissions == nil {\n\t\treturn \"\"\n\t}\n\n\tperms := post.Board.Permissions\n\tcreator := post.Creator\n\n\t// Check roles in order of priority\n\tif perms.HasRole(creator, RoleOwner) {\n\t\treturn \" `owner`\"\n\t}\n\tif perms.HasRole(creator, RoleAdmin) {\n\t\treturn \" `admin`\"\n\t}\n\tif perms.HasRole(creator, RoleModerator) {\n\t\treturn \" `mod`\"\n\t}\n\treturn \"\"\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/boards2/v1\"\ngno = \"0.9\"\n"},{"name":"meta.gno","body":"package boards2\n\nimport \"gno.land/p/gnoland/boards\"\n\n// BoardMeta defines a type for board metadata.\ntype BoardMeta struct {\n\t// HiddenThreads contains hidden board threads.\n\tHiddenThreads boards.PostStorage\n}\n\n// ThreadMeta defines a type for thread metadata.\ntype ThreadMeta struct {\n\t// AllReplies contains all existing thread comments and replies.\n\tAllReplies boards.PostStorage\n}\n"},{"name":"permissions.gno","body":"package boards2\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/gnoland/boards/exts/permissions\"\n)\n\n// List of Boards2 member roles.\nconst (\n\tRoleOwner     boards.Role = \"owner\"\n\tRoleAdmin                 = \"admin\"\n\tRoleModerator             = \"moderator\"\n\tRoleGuest                 = \"guest\"\n)\n\n// PermissionCustom defines an initial value for custom board permissions.\n// When a board defines custom permissions it must starts from a value\n// greater or equal than PermissionCustom.\n//\n// Custom permissions definition example:\n//\n//\tconst (\n//\t  PermissionCustom1 boards.Permission = iota + boards2.PermissionCustom\n//\t  PermissionCustom2\n//\t  PermissionCustom3\n//\t)\nconst PermissionCustom boards.Permission = 200\n\n// List of Boards2 permissions.\nconst (\n\tPermissionBoardCreate boards.Permission = iota\n\tPermissionBoardFlaggingUpdate\n\tPermissionBoardFreeze\n\tPermissionBoardRename\n\tPermissionMemberInvite\n\tPermissionMemberInviteRevoke\n\tPermissionMemberRemove\n\tPermissionPermissionsUpdate\n\tPermissionRealmHelpChange\n\tPermissionRealmLock\n\tPermissionRealmNotice\n\tPermissionAccountRequiredAmountChange\n\tPermissionReplyCreate\n\tPermissionReplyDelete\n\tPermissionReplyFlag\n\tPermissionRoleChange\n\tPermissionThreadCreate\n\tPermissionThreadDelete\n\tPermissionThreadEdit\n\tPermissionThreadFlag\n\tPermissionThreadFreeze\n\tPermissionThreadRepost\n\tPermissionUserBan\n\tPermissionUserUnban\n)\n\nfunc createBasicBoardPermissions(owner address) *permissions.Permissions {\n\tperms := permissions.New(\n\t\tpermissions.UseSingleUserRole(),\n\t\tpermissions.WithSuperRole(RoleOwner),\n\t)\n\tperms.AddRole(\n\t\tRoleAdmin,\n\t\tPermissionBoardRename,\n\t\tPermissionBoardFlaggingUpdate,\n\t\tPermissionMemberInvite,\n\t\tPermissionMemberInviteRevoke,\n\t\tPermissionMemberRemove,\n\t\tPermissionThreadCreate,\n\t\tPermissionThreadEdit,\n\t\tPermissionThreadDelete,\n\t\tPermissionThreadRepost,\n\t\tPermissionThreadFlag,\n\t\tPermissionThreadFreeze,\n\t\tPermissionReplyCreate,\n\t\tPermissionReplyDelete,\n\t\tPermissionReplyFlag,\n\t\tPermissionRoleChange,\n\t\tPermissionUserBan,\n\t\tPermissionUserUnban,\n\t)\n\tperms.AddRole(\n\t\tRoleModerator,\n\t\tPermissionThreadCreate,\n\t\tPermissionThreadEdit,\n\t\tPermissionThreadRepost,\n\t\tPermissionThreadFlag,\n\t\tPermissionReplyCreate,\n\t\tPermissionReplyFlag,\n\t\tPermissionUserBan,\n\t\tPermissionUserUnban,\n\t)\n\tperms.AddRole(\n\t\tRoleGuest,\n\t\tPermissionThreadCreate,\n\t\tPermissionThreadRepost,\n\t\tPermissionReplyCreate,\n\t)\n\tperms.SetUserRoles(owner, RoleOwner)\n\tperms.ValidateFunc(PermissionBoardRename, validateBasicBoardRename)\n\tperms.ValidateFunc(PermissionMemberInvite, validateBasicMemberInvite)\n\tperms.ValidateFunc(PermissionRoleChange, validateBasicRoleChange)\n\treturn perms\n}\n\nfunc createOpenBoardPermissions(owner address) *permissions.Permissions {\n\tperms := permissions.New(\n\t\tpermissions.UseSingleUserRole(),\n\t\tpermissions.WithSuperRole(RoleOwner),\n\t)\n\tperms.SetPublicPermissions(\n\t\tPermissionThreadCreate,\n\t\tPermissionThreadRepost,\n\t\tPermissionReplyCreate,\n\t)\n\tperms.AddRole(\n\t\tRoleAdmin,\n\t\tPermissionBoardRename,\n\t\tPermissionBoardFlaggingUpdate,\n\t\tPermissionMemberInvite,\n\t\tPermissionMemberInviteRevoke,\n\t\tPermissionMemberRemove,\n\t\tPermissionThreadCreate,\n\t\tPermissionThreadEdit,\n\t\tPermissionThreadDelete,\n\t\tPermissionThreadRepost,\n\t\tPermissionThreadFlag,\n\t\tPermissionThreadFreeze,\n\t\tPermissionReplyCreate,\n\t\tPermissionReplyDelete,\n\t\tPermissionReplyFlag,\n\t\tPermissionRoleChange,\n\t\tPermissionUserBan,\n\t\tPermissionUserUnban,\n\t)\n\tperms.AddRole(\n\t\tRoleModerator,\n\t\tPermissionThreadCreate,\n\t\tPermissionThreadEdit,\n\t\tPermissionThreadRepost,\n\t\tPermissionThreadFlag,\n\t\tPermissionReplyCreate,\n\t\tPermissionReplyFlag,\n\t\tPermissionUserBan,\n\t\tPermissionUserUnban,\n\t)\n\tperms.AddRole(\n\t\tRoleGuest,\n\t\tPermissionThreadCreate,\n\t\tPermissionThreadRepost,\n\t\tPermissionReplyCreate,\n\t)\n\tperms.SetUserRoles(owner, RoleOwner)\n\tperms.ValidateFunc(PermissionBoardRename, validateOpenBoardRename)\n\tperms.ValidateFunc(PermissionMemberInvite, validateOpenMemberInvite)\n\tperms.ValidateFunc(PermissionRoleChange, validateOpenRoleChange)\n\tperms.ValidateFunc(PermissionThreadCreate, validateOpenThreadCreate)\n\tperms.ValidateFunc(PermissionReplyCreate, validateOpenReplyCreate)\n\treturn perms\n}\n"},{"name":"permissions_validators_basic.gno","body":"package boards2\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\t\"gno.land/r/sys/users\"\n)\n\n// validateBasicBoardCreate validates PermissionBoardCreate.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board name\n// 3. Board ID\n// 4. Is board listed\n// 5. Is board open\nfunc validateBasicBoardCreate(perms boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\tname, ok := args[1].(string)\n\tif !ok {\n\t\treturn errors.New(\"expected board name to be a string\")\n\t}\n\n\topen, ok := args[4].(bool)\n\tif !ok {\n\t\treturn errors.New(\"expected board open flag to be a boolean\")\n\t}\n\n\tif open \u0026\u0026 !perms.HasRole(caller, RoleOwner) {\n\t\treturn errors.New(\"only owners can create open boards\")\n\t}\n\n\tif err := checkBoardNameIsNotAddress(name); err != nil {\n\t\treturn err\n\t}\n\n\tif err := checkBoardNameBelongsToAddress(caller, name); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n// validateBasicBoardRename validates PermissionBoardRename.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Current board name\n// 4. New board name\nfunc validateBasicBoardRename(_ boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\tnewName, ok := args[3].(string)\n\tif !ok {\n\t\treturn errors.New(\"expected new board name to be a string\")\n\t}\n\n\tif err := checkBoardNameIsNotAddress(newName); err != nil {\n\t\treturn err\n\t}\n\n\tif err := checkBoardNameBelongsToAddress(caller, newName); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n// validateBasicMemberInvite validates PermissionMemberInvite.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Invites\nfunc validateBasicMemberInvite(perms boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\tinvites, ok := args[2].([]Invite)\n\tif !ok {\n\t\treturn errors.New(\"expected valid user invites\")\n\t}\n\n\t// Make sure that only owners invite other owners\n\tcallerIsOwner := perms.HasRole(caller, RoleOwner)\n\tfor _, v := range invites {\n\t\tif v.Role == RoleOwner \u0026\u0026 !callerIsOwner {\n\t\t\treturn errors.New(\"only owners are allowed to invite other owners\")\n\t\t}\n\t}\n\treturn nil\n}\n\n// validateBasicRoleChange validates PermissionRoleChange.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Member address\n// 4. Role\nfunc validateBasicRoleChange(perms boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\t// Owners and Admins can change roles.\n\t// Admins should not be able to assign or remove the Owner role from members.\n\tif perms.HasRole(caller, RoleAdmin) {\n\t\trole, ok := args[3].(boards.Role)\n\t\tif !ok {\n\t\t\treturn errors.New(\"expected a valid member role\")\n\t\t}\n\n\t\tif role == RoleOwner {\n\t\t\treturn errors.New(\"admins are not allowed to promote members to Owner\")\n\t\t} else {\n\t\t\tmember, ok := args[2].(address)\n\t\t\tif !ok {\n\t\t\t\treturn errors.New(\"expected a valid member address\")\n\t\t\t}\n\n\t\t\tif perms.HasRole(member, RoleOwner) {\n\t\t\t\treturn errors.New(\"admins are not allowed to remove the Owner role\")\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc checkBoardNameIsNotAddress(s string) error {\n\tif address(s).IsValid() {\n\t\treturn errors.New(\"addresses are not allowed as board name\")\n\t}\n\treturn nil\n}\n\nfunc checkBoardNameBelongsToAddress(owner address, name string) error {\n\t// When the board name is the name of a registered user\n\t// check that caller is the owner of the name.\n\tuser, _ := users.ResolveName(name)\n\tif user != nil \u0026\u0026 user.Addr() != owner {\n\t\treturn errors.New(\"board name is a user name registered to a different user\")\n\t}\n\treturn nil\n}\n"},{"name":"permissions_validators_open.gno","body":"package boards2\n\nimport (\n\t\"chain/banker\"\n\t\"errors\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// validateOpenBoardRename validates PermissionBoardRename.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Current board name\n// 4. New board name\nfunc validateOpenBoardRename(_ boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\tnewName, ok := args[3].(string)\n\tif !ok {\n\t\treturn errors.New(\"expected new board name to be a string\")\n\t}\n\n\tif err := checkBoardNameIsNotAddress(newName); err != nil {\n\t\treturn err\n\t}\n\n\tif err := checkBoardNameBelongsToAddress(caller, newName); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n// validateOpenMemberInvite validates PermissionMemberInvite.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Invites\nfunc validateOpenMemberInvite(perms boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\tinvites, ok := args[2].([]Invite)\n\tif !ok {\n\t\treturn errors.New(\"expected valid user invites\")\n\t}\n\n\t// Make sure that only owners invite other owners\n\tcallerIsOwner := perms.HasRole(caller, RoleOwner)\n\tfor _, v := range invites {\n\t\tif v.Role == RoleOwner \u0026\u0026 !callerIsOwner {\n\t\t\treturn errors.New(\"only owners are allowed to invite other owners\")\n\t\t}\n\t}\n\treturn nil\n}\n\n// validateOpenRoleChange validates PermissionRoleChange.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Member address\n// 4. Role\nfunc validateOpenRoleChange(perms boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\t// Owners and Admins can change roles.\n\t// Admins should not be able to assign or remove the Owner role from members.\n\tif perms.HasRole(caller, RoleAdmin) {\n\t\trole, ok := args[3].(boards.Role)\n\t\tif !ok {\n\t\t\treturn errors.New(\"expected a valid member role\")\n\t\t}\n\n\t\tif role == RoleOwner {\n\t\t\treturn errors.New(\"admins are not allowed to promote members to Owner\")\n\t\t} else {\n\t\t\tmember, ok := args[2].(address)\n\t\t\tif !ok {\n\t\t\t\treturn errors.New(\"expected a valid member address\")\n\t\t\t}\n\n\t\t\tif perms.HasRole(member, RoleOwner) {\n\t\t\t\treturn errors.New(\"admins are not allowed to remove the Owner role\")\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// validateOpenThreadCreate validates PermissionThreadCreate.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Thread ID\n// 4. Title\n// 5. Body\nfunc validateOpenThreadCreate(perms boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\t// Owners and admins can create threads without special requirements\n\tif perms.HasRole(caller, RoleOwner) || perms.HasRole(caller, RoleAdmin) {\n\t\treturn nil\n\t}\n\n\t// Require non members to have some GNOT in their accounts\n\tif err := checkAccountHasAmount(caller, RequiredAccountAmount); err != nil {\n\t\treturn ufmt.Errorf(\"caller is not allowed to create threads: %s\", err)\n\t}\n\treturn nil\n}\n\n// validateOpenReplyCreate validates PermissionReplyCreate.\n//\n// Expected `args` values:\n// 1. Caller address\n// 2. Board ID\n// 3. Thread ID\n// 4. Parent ID\n// 5. Reply ID\n// 6. Body\nfunc validateOpenReplyCreate(perms boards.Permissions, args boards.Args) error {\n\tcaller, ok := args[0].(address)\n\tif !ok {\n\t\treturn errors.New(\"expected a valid caller address\")\n\t}\n\n\t// All board members can reply\n\tif perms.HasUser(caller) {\n\t\treturn nil\n\t}\n\n\t// Require non members to have some GNOT in their accounts\n\tif err := checkAccountHasAmount(caller, RequiredAccountAmount); err != nil {\n\t\treturn ufmt.Errorf(\"caller is not allowed to comment: %s\", err)\n\t}\n\treturn nil\n}\n\nfunc checkAccountHasAmount(addr address, amount int64) error {\n\tbnk := banker.NewReadonlyBanker()\n\tcoins := bnk.GetCoins(addr)\n\tif coins.AmountOf(\"ugnot\") \u003c RequiredAccountAmount {\n\t\tamount = amount / 1_000_000 // ugnot -\u003e GNOT\n\t\treturn ufmt.Errorf(\"account amount is lower than %d GNOT\", amount)\n\t}\n\treturn nil\n}\n"},{"name":"protected.gno","body":"// The `protected.gno` file contains public realm functions that must only be called\n// from realms that live within the Boards2 package namespace. This allows sub realms\n// to access boards data to be able to migrate content from one version to another or\n// to implement specific features in separate sub realms.\n\npackage boards2\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// TODO: Authorize sub realms to be able to call protected functions (use DAOs)\n\n// boardsNS is the package-path namespace shared by all Boards2 versions.\n// Update as part of each version bump.\nconst boardsNS = \"gno.land/r/gnoland/boards2/\"\n\n// GetRealmPermissions returns Boards2 realm permissions.\n// This is a protected function only callable by Boards2 sub realms.\nfunc GetRealmPermissions(cur realm) boards.Permissions {\n\tassertCallerHasBoardsNS(0, cur)\n\treturn gPerms\n}\n\n// GetBoard returns a board.\n// This is a protected function only callable by Boards2 sub realms.\nfunc GetBoard(cur realm, boardID boards.ID) (_ *boards.Board, found bool) {\n\tassertCallerHasBoardsNS(0, cur)\n\treturn gBoards.Get(boardID)\n}\n\n// MustGetBoard returns a board or panics on error.\n// This is a protected function only callable by Boards2 sub realms.\nfunc MustGetBoard(cur realm, boardID boards.ID) *boards.Board {\n\tassertCallerHasBoardsNS(0, cur)\n\treturn mustGetBoard(boardID)\n}\n\n// Iterate iterates boards.\n// Iteration is done for all boards, including the ones that are not listed.\n// To reverse iterate boards use a negative count.\n// If the callback returns true, iteration is stopped.\nfunc Iterate(cur realm, start, count int, fn boards.BoardIterFn) bool {\n\tassertCallerHasBoardsNS(0, cur)\n\treturn gBoards.Iterate(start, count, fn)\n}\n\n// assertCallerHasBoardsNS panics unless the caller realm's PkgPath\n// lives within the Boards2 namespace. Callers pass their own live cur;\n// the helper enforces rlm.IsCurrent() and derives the caller as\n// rlm.Previous() internally. This mirrors the Transfer/AddMember\n// pattern (see docs/resources/gno-security.md): receive the live cur,\n// extract Previous() inside, never trust a stale-or-stashed realm\n// value's PkgPath().\n//\n// _ int discriminator keeps this helper non-crossing.\nfunc assertCallerHasBoardsNS(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tif !strings.HasPrefix(rlm.Previous().PkgPath(), boardsNS) {\n\t\tpanic(\"forbidden, caller should live within \\\"\" + boardsNS + \"\\\" namespace\")\n\t}\n}\n"},{"name":"public.gno","body":"package boards2\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"regexp\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nconst (\n\t// MaxBoardNameLength defines the maximum length allowed for board names.\n\tMaxBoardNameLength = 50\n\n\t// MaxThreadTitleLength defines the maximum length allowed for thread titles.\n\tMaxThreadTitleLength = 100\n\n\t// MaxThreadBodyLength defines the maximum length allowed for thread bodies.\n\t// 40,000 mirrors Reddit's self-post body cap.\n\tMaxThreadBodyLength = 40000\n\n\t// MaxReplyLength defines the maximum length allowed for replies.\n\t// 10,000 mirrors Reddit's comment cap.\n\tMaxReplyLength = 10000\n)\n\nvar reBoardName = regexp.MustCompile(`(?i)^[a-z]+[a-z0-9_\\-]{2,50}$`)\n\n// SetHelp sets or updates boards realm help content.\nfunc SetHelp(cur realm, content string) {\n\tcontent = strings.TrimSpace(content)\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{content}\n\tgPerms.WithPermission(caller, PermissionRealmHelpChange, args, func() {\n\t\tHelp = content\n\t})\n}\n\n// SetRequiredAccountAmount sets the required account amount to interact as a non member with open boards.\n// Amount must be given as ugnot.\n// The amount requirement is not applied to members that were invited to an open board.\nfunc SetRequiredAccountAmount(cur realm, amount int64) {\n\tif amount \u003c 0 {\n\t\tpanic(\"invalid amount\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{amount}\n\tgPerms.WithPermission(caller, PermissionAccountRequiredAmountChange, args, func() {\n\t\tRequiredAccountAmount = amount\n\n\t\tchain.Emit(\n\t\t\t\"RequiredAccountAmountChanged\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"amount\", strconv.FormatInt(amount, 10),\n\t\t)\n\t})\n}\n\n// SetPermissions sets a permissions implementation for boards2 realm or a board.\nfunc SetPermissions(cur realm, boardID boards.ID, p boards.Permissions) {\n\tassertRealmIsNotLocked()\n\tassertBoardExists(boardID)\n\n\tif p == nil {\n\t\tpanic(\"permissions is required\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{boardID}\n\tgPerms.WithPermission(caller, PermissionPermissionsUpdate, args, func() {\n\t\tassertRealmIsNotLocked()\n\n\t\t// When board ID is zero it means that realm permissions are being updated\n\t\tif boardID == 0 {\n\t\t\tgPerms = p\n\n\t\t\tchain.Emit(\n\t\t\t\t\"RealmPermissionsChanged\",\n\t\t\t\t\"caller\", caller.String(),\n\t\t\t)\n\t\t\treturn\n\t\t}\n\n\t\t// Otherwise update the permissions of a single board\n\t\tboard := mustGetBoard(boardID)\n\t\tboard.Permissions = p\n\n\t\tchain.Emit(\n\t\t\t\"BoardPermissionsChanged\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t)\n\t})\n}\n\n// SetRealmNotice sets a notice to be displayed globally within the realm.\n// An empty message removes the realm notice.\nfunc SetRealmNotice(cur realm, message string) {\n\tmessage = strings.TrimSpace(message)\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{message}\n\tgPerms.WithPermission(caller, PermissionRealmNotice, args, func() {\n\t\tNotice = message\n\n\t\tchain.Emit(\n\t\t\t\"RealmNoticeChanged\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"message\", message,\n\t\t)\n\t})\n}\n\n// GetBoardIDFromName searches a board by name and returns its ID.\nfunc GetBoardIDFromName(_ realm, name string) (_ boards.ID, found bool) {\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\treturn 0, false\n\t}\n\treturn board.ID, true\n}\n\n// BoardCount returns the total number of boards.\nfunc BoardCount() int {\n\treturn gBoards.Size()\n}\n\n// CreateBoard creates a new board.\n//\n// Listed boards are included in the realm's list of boards.\n// Open boards allow anyone to create threads and comment.\nfunc CreateBoard(cur realm, name string, listed, open bool) boards.ID {\n\tassertRealmIsNotLocked()\n\n\tname = strings.TrimSpace(name)\n\tassertIsValidBoardName(name)\n\tassertBoardNameNotExists(name)\n\n\tcaller := cur.Previous().Address()\n\tid := gBoardsSequence.Next()\n\tboard := boards.New(id)\n\targs := boards.Args{caller, name, board.ID, listed, open}\n\tgPerms.WithPermission(caller, PermissionBoardCreate, args, func() {\n\t\tassertRealmIsNotLocked()\n\t\tassertBoardNameNotExists(name)\n\n\t\tboard.Name = name\n\t\tboard.Creator = caller\n\t\tboard.Meta = \u0026BoardMeta{\n\t\t\tHiddenThreads: boards.NewPostStorage(),\n\t\t}\n\n\t\tif open {\n\t\t\tboard.Permissions = createOpenBoardPermissions(caller)\n\t\t} else {\n\t\t\tboard.Permissions = createBasicBoardPermissions(caller)\n\t\t}\n\n\t\tif err := gBoards.Add(board); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\t// Listed boards are also indexed separately for easier iteration and pagination\n\t\tif listed {\n\t\t\tgListedBoardsByID.Set(board.ID.Key(), board)\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"BoardCreated\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"name\", name,\n\t\t)\n\t})\n\treturn board.ID\n}\n\n// RenameBoard changes the name of an existing board.\n//\n// A history of previous board names is kept when boards are renamed.\n// Because of that boards are also accessible using previous name(s).\nfunc RenameBoard(cur realm, name, newName string) {\n\tassertRealmIsNotLocked()\n\n\tnewName = strings.TrimSpace(newName)\n\tassertIsValidBoardName(newName)\n\tassertBoardNameNotExists(newName)\n\n\tboard := mustGetBoardByName(name)\n\tassertBoardIsNotFrozen(board)\n\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{caller, board.ID, name, newName}\n\tboard.Permissions.WithPermission(caller, PermissionBoardRename, args, func() {\n\t\tassertRealmIsNotLocked()\n\t\tassertBoardNameNotExists(newName)\n\n\t\tboard.Aliases = append(board.Aliases, board.Name)\n\t\tboard.Name = newName\n\t\tboard.UpdatedAt = time.Now()\n\n\t\t// Index board for the new name keeping previous indexes for older names\n\t\tgBoards.Add(board)\n\n\t\tchain.Emit(\n\t\t\t\"BoardRenamed\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"name\", name,\n\t\t\t\"newName\", newName,\n\t\t)\n\t})\n}\n\n// CreateThread creates a new thread within a board.\nfunc CreateThread(cur realm, boardID boards.ID, title, body string) boards.ID {\n\tassertRealmIsNotLocked()\n\n\ttitle = strings.TrimSpace(title)\n\tassertTitleIsValid(title)\n\tassertThreadBodyIsValid(body)\n\n\tcaller := cur.Previous().Address()\n\tassertUserIsNotBanned(boardID, caller)\n\n\tboard := mustGetBoard(boardID)\n\tassertBoardIsNotFrozen(board)\n\n\tthread := boards.MustNewThread(board, caller, title, body)\n\targs := boards.Args{caller, board.ID, thread.ID, title, body}\n\tboard.Permissions.WithPermission(caller, PermissionThreadCreate, args, func() {\n\t\tassertRealmIsNotLocked()\n\t\tassertUserIsNotBanned(board.ID, caller)\n\n\t\tthread.Meta = \u0026ThreadMeta{\n\t\t\tAllReplies: boards.NewPostStorage(),\n\t\t}\n\n\t\tif err := board.Threads.Add(thread); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"ThreadCreated\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"title\", title,\n\t\t)\n\t})\n\treturn thread.ID\n}\n\n// CreateReply creates a new comment or reply within a thread.\n//\n// The value of `replyID` is only required when creating a reply of another reply.\nfunc CreateReply(cur realm, boardID, threadID, replyID boards.ID, body string) boards.ID {\n\tassertRealmIsNotLocked()\n\n\tbody = strings.TrimSpace(body)\n\tassertReplyBodyIsValid(body)\n\n\tcaller := cur.Previous().Address()\n\tassertUserIsNotBanned(boardID, caller)\n\n\tboard := mustGetBoard(boardID)\n\tassertBoardIsNotFrozen(board)\n\n\tthread := mustGetThread(board, threadID)\n\tassertThreadIsVisible(thread)\n\tassertThreadIsNotFrozen(thread)\n\n\t// By default consider that reply's parent is the thread.\n\t// Or when replyID is assigned use that reply as the parent.\n\tparent := thread\n\tif replyID \u003e 0 {\n\t\tparent = mustGetReply(thread, replyID)\n\t\tif parent.Hidden || parent.Readonly {\n\t\t\tpanic(\"replying to a hidden or frozen reply is not allowed\")\n\t\t}\n\t}\n\n\treply := boards.MustNewReply(parent, caller, body)\n\targs := boards.Args{caller, board.ID, thread.ID, parent.ID, reply.ID, body}\n\tboard.Permissions.WithPermission(caller, PermissionReplyCreate, args, func() {\n\t\tassertRealmIsNotLocked()\n\n\t\t// Add reply to its parent\n\t\tif err := parent.Replies.Add(reply); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\t// Always add reply to the thread so it contains all comments and replies.\n\t\t// Comment and reply only contains direct replies.\n\t\tmeta := thread.Meta.(*ThreadMeta)\n\t\tif err := meta.AllReplies.Add(reply); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"ReplyCreate\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"replyID\", reply.ID.String(),\n\t\t)\n\t})\n\treturn reply.ID\n}\n\n// CreateRepost reposts a thread into another board.\nfunc CreateRepost(cur realm, boardID, threadID, destinationBoardID boards.ID, title, body string) boards.ID {\n\tassertRealmIsNotLocked()\n\n\ttitle = strings.TrimSpace(title)\n\tassertTitleIsValid(title)\n\tassertThreadBodyIsValid(body)\n\n\tcaller := cur.Previous().Address()\n\tassertUserIsNotBanned(destinationBoardID, caller)\n\n\tdst := mustGetBoard(destinationBoardID)\n\tassertBoardIsNotFrozen(dst)\n\n\tboard := mustGetBoard(boardID)\n\tthread := mustGetThread(board, threadID)\n\tassertThreadIsVisible(thread)\n\n\trepost := boards.MustNewRepost(thread, dst, caller)\n\targs := boards.Args{caller, board.ID, thread.ID, dst.ID, repost.ID, title, body}\n\tdst.Permissions.WithPermission(caller, PermissionThreadRepost, args, func() {\n\t\tassertRealmIsNotLocked()\n\n\t\trepost.Title = title\n\t\trepost.Body = strings.TrimSpace(body)\n\t\trepost.Meta = \u0026ThreadMeta{\n\t\t\tAllReplies: boards.NewPostStorage(),\n\t\t}\n\n\t\tif err := dst.Threads.Add(repost); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tif err := thread.Reposts.Add(repost); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"Repost\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"destinationBoardID\", dst.ID.String(),\n\t\t\t\"repostID\", repost.ID.String(),\n\t\t\t\"title\", title,\n\t\t)\n\t})\n\treturn repost.ID\n}\n\n// DeleteThread deletes a thread from a board.\n//\n// Threads can be deleted by the users who created them or otherwise by users with special permissions.\nfunc DeleteThread(cur realm, boardID, threadID boards.ID) {\n\tassertRealmIsNotLocked()\n\n\tcaller := cur.Previous().Address()\n\tboard := mustGetBoard(boardID)\n\tassertUserIsNotBanned(boardID, caller)\n\n\tisRealmOwner := gPerms.HasRole(caller, RoleOwner) // TODO: Add DeleteThread filetest cases for realm owners\n\tif !isRealmOwner {\n\t\tassertBoardIsNotFrozen(board)\n\t}\n\n\tthread := mustGetThread(board, threadID)\n\tdeleteThread := func() {\n\t\tboard.Threads.Remove(thread.ID)\n\n\t\tchain.Emit(\n\t\t\t\"ThreadDeleted\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t)\n\t}\n\n\t// Thread can be directly deleted by user that created it.\n\t// It can also be deleted by realm owners, to be able to delete inappropriate content.\n\t// TODO: Discuss and decide if realm owners should be able to delete threads.\n\tif isRealmOwner || caller == thread.Creator {\n\t\tdeleteThread()\n\t\treturn\n\t}\n\n\targs := boards.Args{caller, board.ID, thread.ID}\n\tboard.Permissions.WithPermission(caller, PermissionThreadDelete, args, func() {\n\t\tassertRealmIsNotLocked()\n\t\tdeleteThread()\n\t})\n}\n\n// DeleteReply deletes a reply from a thread.\n//\n// Replies can be deleted by the users who created them or otherwise by users with special permissions.\n// Soft deletion is used when the deleted reply contains sub replies, in which case the reply content\n// is replaced by a text informing that reply has been deleted to avoid deleting sub-replies.\nfunc DeleteReply(cur realm, boardID, threadID, replyID boards.ID) {\n\tassertRealmIsNotLocked()\n\n\tcaller := cur.Previous().Address()\n\tboard := mustGetBoard(boardID)\n\tassertUserIsNotBanned(boardID, caller)\n\n\tthread := mustGetThread(board, threadID)\n\treply := mustGetReply(thread, replyID)\n\tisRealmOwner := gPerms.HasRole(caller, RoleOwner) // TODO: Add DeleteReply filetest cases for realm owners\n\tif !isRealmOwner {\n\t\tassertBoardIsNotFrozen(board)\n\t\tassertThreadIsNotFrozen(thread)\n\t\tassertReplyIsVisible(reply)\n\t}\n\n\tdeleteReply := func() {\n\t\t// Soft delete comment/reply by changing its body when\n\t\t// it contains replies, otherwise hard delete it.\n\t\tif reply.Replies.Size() \u003e 0 {\n\t\t\treply.Body = \"⚠ This comment has been deleted\"\n\t\t\treply.UpdatedAt = time.Now()\n\t\t} else {\n\t\t\t// Remove reply from the flat thread index.\n\t\t\tmeta := thread.Meta.(*ThreadMeta)\n\t\t\treply, removed := meta.AllReplies.Remove(replyID)\n\t\t\tif !removed {\n\t\t\t\tpanic(\"reply not found\")\n\t\t\t}\n\n\t\t\t// Remove reply from its parent's direct-children list too. A\n\t\t\t// direct thread reply's parent is the thread itself; a nested\n\t\t\t// reply's parent is another reply (resolved via the flat index).\n\t\t\t// Missing the thread-parent case left a ghost in thread.Replies\n\t\t\t// (rendered in the threaded view, with a stale reply count).\n\t\t\tif reply.ParentID == thread.ID {\n\t\t\t\tthread.Replies.Remove(replyID)\n\t\t\t} else if parent, found := meta.AllReplies.Get(reply.ParentID); found {\n\t\t\t\tparent.Replies.Remove(replyID)\n\t\t\t}\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"ReplyDeleted\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"replyID\", reply.ID.String(),\n\t\t)\n\t}\n\n\t// Reply can be directly deleted by user that created it.\n\t// It can also be deleted by realm owners, to be able to delete inappropriate content.\n\t// TODO: Discuss and decide if realm owners should be able to delete replies.\n\tif isRealmOwner || caller == reply.Creator {\n\t\tdeleteReply()\n\t\treturn\n\t}\n\n\targs := boards.Args{caller, board.ID, thread.ID, reply.ID}\n\tboard.Permissions.WithPermission(caller, PermissionReplyDelete, args, func() {\n\t\tassertRealmIsNotLocked()\n\t\tdeleteReply()\n\t})\n}\n\n// EditThread updates the title and body of a thread.\n//\n// Threads can be updated by the users who created them or otherwise by users with special permissions.\nfunc EditThread(cur realm, boardID, threadID boards.ID, title, body string) {\n\tassertRealmIsNotLocked()\n\n\ttitle = strings.TrimSpace(title)\n\tassertTitleIsValid(title)\n\tassertThreadBodyIsValid(body)\n\n\tboard := mustGetBoard(boardID)\n\tassertBoardIsNotFrozen(board)\n\n\tcaller := cur.Previous().Address()\n\tassertUserIsNotBanned(boardID, caller)\n\n\tthread := mustGetThread(board, threadID)\n\tassertThreadIsNotFrozen(thread)\n\n\tbody = strings.TrimSpace(body)\n\tif !boards.IsRepost(thread) {\n\t\tassertBodyIsNotEmpty(body)\n\t}\n\n\teditThread := func() {\n\t\tthread.Title = title\n\t\tthread.Body = body\n\t\tthread.UpdatedAt = time.Now()\n\n\t\tchain.Emit(\n\t\t\t\"ThreadEdited\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"title\", title,\n\t\t)\n\t}\n\n\tif caller == thread.Creator {\n\t\teditThread()\n\t\treturn\n\t}\n\n\targs := boards.Args{caller, board.ID, thread.ID, title, body}\n\tboard.Permissions.WithPermission(caller, PermissionThreadEdit, args, func() {\n\t\tassertRealmIsNotLocked()\n\t\teditThread()\n\t})\n}\n\n// EditReply updates the body of a comment or reply.\n//\n// Replies can be updated only by the users who created them.\nfunc EditReply(cur realm, boardID, threadID, replyID boards.ID, body string) {\n\tassertRealmIsNotLocked()\n\n\tbody = strings.TrimSpace(body)\n\tassertReplyBodyIsValid(body)\n\n\tboard := mustGetBoard(boardID)\n\tassertBoardIsNotFrozen(board)\n\n\tcaller := cur.Previous().Address()\n\tassertUserIsNotBanned(boardID, caller)\n\n\tthread := mustGetThread(board, threadID)\n\tassertThreadIsNotFrozen(thread)\n\n\treply := mustGetReply(thread, replyID)\n\tassertReplyIsVisible(reply)\n\n\tif caller != reply.Creator {\n\t\tpanic(\"only the reply creator is allowed to edit it\")\n\t}\n\n\treply.Body = body\n\treply.UpdatedAt = time.Now()\n\n\tchain.Emit(\n\t\t\"ReplyEdited\",\n\t\t\"caller\", caller.String(),\n\t\t\"boardID\", board.ID.String(),\n\t\t\"threadID\", thread.ID.String(),\n\t\t\"replyID\", reply.ID.String(),\n\t\t\"body\", body,\n\t)\n}\n\n// RemoveMember removes a member from the realm or a board.\n//\n// Board ID is only required when removing a member from board.\nfunc RemoveMember(cur realm, boardID boards.ID, member address) {\n\tassertMembersUpdateIsEnabled(boardID)\n\tassertMemberAddressIsValid(member)\n\n\tperms := mustGetPermissions(boardID)\n\torigin := unsafe.OriginCaller()\n\tcaller := cur.Previous().Address()\n\tremoveMember := func() {\n\t\tif !perms.RemoveUser(member) {\n\t\t\tpanic(\"member not found\")\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"MemberRemoved\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"origin\", origin.String(), // When origin and caller match it means self removal\n\t\t\t\"boardID\", boardID.String(),\n\t\t\t\"member\", member.String(),\n\t\t)\n\t}\n\n\t// Members can remove themselves without permission\n\tif origin == member {\n\t\tremoveMember()\n\t\treturn\n\t}\n\n\targs := boards.Args{boardID, member}\n\tperms.WithPermission(caller, PermissionMemberRemove, args, func() {\n\t\tassertMembersUpdateIsEnabled(boardID)\n\t\tremoveMember()\n\t})\n}\n\n// IsMember checks if a user is a member of the realm or a board.\n//\n// Board ID is only required when checking if a user is a member of a board.\nfunc IsMember(boardID boards.ID, user address) bool {\n\tassertUserAddressIsValid(user)\n\n\tif boardID != 0 {\n\t\tboard := mustGetBoard(boardID)\n\t\tassertBoardIsNotFrozen(board)\n\t}\n\n\tperms := mustGetPermissions(boardID)\n\treturn perms.HasUser(user)\n}\n\n// HasMemberRole checks if a realm or board member has a specific role assigned.\n//\n// Board ID is only required when checking a member of a board.\nfunc HasMemberRole(boardID boards.ID, member address, role boards.Role) bool {\n\tassertMemberAddressIsValid(member)\n\n\tif boardID != 0 {\n\t\tboard := mustGetBoard(boardID)\n\t\tassertBoardIsNotFrozen(board)\n\t}\n\n\tperms := mustGetPermissions(boardID)\n\treturn perms.HasRole(member, role)\n}\n\n// ChangeMemberRole changes the role of a realm or board member.\n//\n// Board ID is only required when changing the role for a member of a board.\nfunc ChangeMemberRole(cur realm, boardID boards.ID, member address, role boards.Role) {\n\tassertMemberAddressIsValid(member)\n\tassertMembersUpdateIsEnabled(boardID)\n\n\tif role == \"\" {\n\t\trole = RoleGuest\n\t}\n\n\tperms := mustGetPermissions(boardID)\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{caller, boardID, member, role}\n\tperms.WithPermission(caller, PermissionRoleChange, args, func() {\n\t\tassertMembersUpdateIsEnabled(boardID)\n\n\t\tperms.SetUserRoles(member, role)\n\n\t\tchain.Emit(\n\t\t\t\"RoleChanged\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", boardID.String(),\n\t\t\t\"member\", member.String(),\n\t\t\t\"newRole\", string(role),\n\t\t)\n\t})\n}\n\nfunc assertMemberAddressIsValid(member address) {\n\tif !member.IsValid() {\n\t\tpanic(\"invalid member address: \" + member.String())\n\t}\n}\n\nfunc assertUserAddressIsValid(user address) {\n\tif !user.IsValid() {\n\t\tpanic(\"invalid user address: \" + user.String())\n\t}\n}\n\nfunc assertBoardExists(id boards.ID) {\n\tif id == 0 { // ID zero is used to refer to the realm\n\t\treturn\n\t}\n\n\tif _, found := gBoards.Get(id); !found {\n\t\tpanic(\"board not found: \" + id.String())\n\t}\n}\n\nfunc assertBoardIsNotFrozen(b *boards.Board) {\n\tif b.Readonly {\n\t\tpanic(\"board is frozen\")\n\t}\n}\n\nfunc assertIsValidBoardName(name string) {\n\tsize := len(name)\n\tif size == 0 {\n\t\tpanic(\"board name is empty\")\n\t}\n\n\tif size \u003c 3 {\n\t\tpanic(\"board name is too short, minimum length is 3 characters\")\n\t}\n\n\tif size \u003e MaxBoardNameLength {\n\t\tn := strconv.Itoa(MaxBoardNameLength)\n\t\tpanic(\"board name is too long, maximum allowed is \" + n + \" characters\")\n\t}\n\n\tif !reBoardName.MatchString(name) {\n\t\tpanic(\"board name must start with a letter and have letters, numbers, \\\"-\\\" and \\\"_\\\"\")\n\t}\n}\n\nfunc assertThreadIsNotFrozen(t *boards.Post) {\n\tif t.Readonly {\n\t\tpanic(\"thread is frozen\")\n\t}\n}\n\nfunc assertNameIsNotEmpty(name string) {\n\tif name == \"\" {\n\t\tpanic(\"name is empty\")\n\t}\n}\n\nfunc assertTitleIsValid(title string) {\n\tif title == \"\" {\n\t\tpanic(\"title is empty\")\n\t}\n\n\tif len(title) \u003e MaxThreadTitleLength {\n\t\tn := strconv.Itoa(MaxThreadTitleLength)\n\t\tpanic(\"title is too long, maximum allowed is \" + n + \" characters\")\n\t}\n}\n\nfunc assertBodyIsNotEmpty(body string) {\n\tif body == \"\" {\n\t\tpanic(\"body is empty\")\n\t}\n}\n\nfunc assertBoardNameNotExists(name string) {\n\tname = strings.ToLower(name)\n\tif _, found := gBoards.GetByName(name); found {\n\t\tpanic(\"board already exists\")\n\t}\n}\n\nfunc assertThreadExists(b *boards.Board, threadID boards.ID) {\n\tif _, found := getThread(b, threadID); !found {\n\t\tpanic(\"thread not found: \" + threadID.String())\n\t}\n}\n\nfunc assertReplyExists(thread *boards.Post, replyID boards.ID) {\n\tif _, found := getReply(thread, replyID); !found {\n\t\tpanic(\"reply not found: \" + replyID.String())\n\t}\n}\n\nfunc assertThreadIsVisible(thread *boards.Post) {\n\tif thread.Hidden {\n\t\tpanic(\"thread is hidden\")\n\t}\n}\n\nfunc assertReplyIsVisible(thread *boards.Post) {\n\tif thread.Hidden {\n\t\tpanic(\"reply is hidden\")\n\t}\n}\n\nfunc assertThreadBodyIsValid(body string) {\n\tif len(body) \u003e MaxThreadBodyLength {\n\t\tn := strconv.Itoa(MaxThreadBodyLength)\n\t\tpanic(\"thread body is too long, maximum allowed is \" + n + \" characters\")\n\t}\n}\n\nfunc assertReplyBodyIsValid(body string) {\n\tassertBodyIsNotEmpty(body)\n\n\tif len(body) \u003e MaxReplyLength {\n\t\tn := strconv.Itoa(MaxReplyLength)\n\t\tpanic(\"reply is too long, maximum allowed is \" + n + \" characters\")\n\t}\n\n\t// No markdown-structure or gno-form blacklist here: reply bodies\n\t// render inside a \u003cgno-foreign\u003e sandbox (see indentForeignBody),\n\t// which contains block structure and omits forms at render time.\n}\n\nfunc assertMembersUpdateIsEnabled(boardID boards.ID) {\n\tif boardID != 0 {\n\t\tassertRealmIsNotLocked()\n\t} else {\n\t\tassertRealmMembersAreNotLocked()\n\t}\n}\n"},{"name":"public_ban.gno","body":"package boards2\n\nimport (\n\t\"chain\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\n// Constants for different banning periods.\nconst (\n\tBanDay  = uint(24)\n\tBanWeek = BanDay * 7\n\tBanYear = BanDay * 365\n)\n\n// Ban bans a user from a board for a period of time.\n// Only invited guest members and external users can be banned.\n// Banning board owners, admins and moderators is not allowed.\nfunc Ban(cur realm, boardID boards.ID, user address, hours uint, reason string) {\n\tassertAddressIsValid(user)\n\n\tif hours == 0 {\n\t\tpanic(\"ban period in hours is required\")\n\t}\n\n\treason = strings.TrimSpace(reason)\n\tif reason == \"\" {\n\t\tpanic(\"ban reason is required\")\n\t}\n\n\tboard := mustGetBoard(boardID)\n\tcaller := cur.Previous().Address()\n\tuntil := time.Now().Add(time.Minute * 60 * time.Duration(hours))\n\targs := boards.Args{boardID, user, until, reason}\n\tboard.Permissions.WithPermission(caller, PermissionUserBan, args, func() {\n\t\t// When banning invited members make sure they are guests, otherwise\n\t\t// disallow banning. Only guest or external users can be banned.\n\t\tif board.Permissions.HasUser(user) \u0026\u0026 !board.Permissions.HasRole(user, RoleGuest) {\n\t\t\tpanic(\"owner, admin and moderator banning is not allowed\")\n\t\t}\n\n\t\tbanned, found := getBannedUsers(boardID)\n\t\tif !found {\n\t\t\tbanned = bptree.NewBPTree32()\n\t\t\tgBannedUsers.Set(boardID.Key(), banned)\n\t\t}\n\n\t\tbanned.Set(user.String(), until)\n\n\t\tchain.Emit(\n\t\t\t\"UserBanned\",\n\t\t\t\"bannedBy\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"user\", user.String(),\n\t\t\t\"until\", until.Format(time.RFC3339),\n\t\t\t\"reason\", reason,\n\t\t)\n\t})\n}\n\n// Unban unbans a user from a board.\nfunc Unban(cur realm, boardID boards.ID, user address, reason string) {\n\tassertAddressIsValid(user)\n\n\tboard := mustGetBoard(boardID)\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{boardID, user, reason}\n\tboard.Permissions.WithPermission(caller, PermissionUserUnban, args, func() {\n\t\tbanned, found := getBannedUsers(boardID)\n\t\tif !found || !banned.Has(user.String()) {\n\t\t\tpanic(\"user is not banned\")\n\t\t}\n\n\t\tbanned.Remove(user.String())\n\n\t\tchain.Emit(\n\t\t\t\"UserUnbanned\",\n\t\t\t\"bannedBy\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"user\", user.String(),\n\t\t\t\"reason\", reason,\n\t\t)\n\t})\n}\n\n// IsBanned checks if a user is banned from a board.\nfunc IsBanned(boardID boards.ID, user address) bool {\n\tbanned, found := getBannedUsers(boardID)\n\treturn found \u0026\u0026 banned.Has(user.String())\n}\n\nfunc assertAddressIsValid(addr address) {\n\tif !addr.IsValid() {\n\t\tpanic(\"invalid address: \" + addr.String())\n\t}\n}\n\nfunc assertUserIsNotBanned(boardID boards.ID, user address) {\n\tbanned, found := getBannedUsers(boardID)\n\tif !found {\n\t\treturn\n\t}\n\n\tv := banned.Get(user.String())\n\tif v == nil {\n\t\treturn\n\t}\n\n\tuntil := v.(time.Time)\n\tif time.Now().Before(until) {\n\t\tpanic(user.String() + \" is banned until \" + until.Format(dateFormat))\n\t}\n}\n"},{"name":"public_flag.gno","body":"package boards2\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// SetFlaggingThreshold sets the number of flags required to hide a thread or comment.\n//\n// Threshold is only applicable within the board where it's setted.\nfunc SetFlaggingThreshold(cur realm, boardID boards.ID, threshold int) {\n\tif threshold \u003c 1 {\n\t\tpanic(\"invalid flagging threshold\")\n\t}\n\n\tassertRealmIsNotLocked()\n\n\tboard := mustGetBoard(boardID)\n\tassertBoardIsNotFrozen(board)\n\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{board.ID, threshold}\n\tboard.Permissions.WithPermission(caller, PermissionBoardFlaggingUpdate, args, func() {\n\t\tassertRealmIsNotLocked()\n\n\t\tgFlaggingThresholds.Set(boardID.String(), threshold)\n\n\t\tchain.Emit(\n\t\t\t\"FlaggingThresholdUpdated\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threshold\", strconv.Itoa(threshold),\n\t\t)\n\t})\n}\n\n// GetFlaggingThreshold returns the number of flags required to hide a thread or comment within a board.\nfunc GetFlaggingThreshold(boardID boards.ID) int {\n\tassertBoardExists(boardID)\n\treturn getFlaggingThreshold(boardID)\n}\n\n// FlagThread adds a new flag to a thread.\n//\n// Flagging requires special permissions and hides the thread when\n// the number of flags reaches a pre-defined flagging threshold.\nfunc FlagThread(cur realm, boardID, threadID boards.ID, reason string) {\n\treason = strings.TrimSpace(reason)\n\tif reason == \"\" {\n\t\tpanic(\"flagging reason is required\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tboard := mustGetBoard(boardID)\n\tisRealmOwner := gPerms.HasRole(caller, RoleOwner)\n\tif !isRealmOwner {\n\t\tassertRealmIsNotLocked()\n\t\tassertBoardIsNotFrozen(board)\n\t}\n\n\tthread, found := getThread(board, threadID)\n\tif !found {\n\t\tpanic(\"thread not found\")\n\t}\n\n\tif thread.Hidden {\n\t\tpanic(\"flagging hidden threads is not allowed\")\n\t}\n\n\tflagThread := func() {\n\t\tif thread.Hidden {\n\t\t\tpanic(\"flagged thread is already hidden\")\n\t\t}\n\n\t\t// Hide thread when flagging threshold is reached.\n\t\t// Realm owners can hide with a single flag.\n\t\thide := flagItem(thread, caller, reason, getFlaggingThreshold(board.ID))\n\t\tif hide || isRealmOwner {\n\t\t\t// Remove thread from the list of visible threads\n\t\t\tthread, removed := board.Threads.Remove(threadID)\n\t\t\tif !removed {\n\t\t\t\tpanic(\"thread not found\")\n\t\t\t}\n\n\t\t\t// Mark thread as hidden to avoid rendering content\n\t\t\tthread.Hidden = true\n\n\t\t\t// Keep track of hidden the thread to be able to restore it after moderation disputes\n\t\t\tmeta := board.Meta.(*BoardMeta)\n\t\t\tmeta.HiddenThreads.Add(thread)\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"ThreadFlagged\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"reason\", reason,\n\t\t)\n\t}\n\n\t// Realm owners should be able to flag without permissions even when board is frozen\n\tif isRealmOwner {\n\t\tflagThread()\n\t\treturn\n\t}\n\n\targs := boards.Args{caller, board.ID, thread.ID, reason}\n\tboard.Permissions.WithPermission(caller, PermissionThreadFlag, args, func() {\n\t\tflagThread()\n\t})\n}\n\n// FlagReply adds a new flag to a comment or reply.\n//\n// Flagging requires special permissions and hides the comment or reply\n// when the number of flags reaches a pre-defined flagging threshold.\nfunc FlagReply(cur realm, boardID, threadID, replyID boards.ID, reason string) {\n\treason = strings.TrimSpace(reason)\n\tif reason == \"\" {\n\t\tpanic(\"flagging reason is required\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tboard := mustGetBoard(boardID)\n\tisRealmOwner := gPerms.HasRole(caller, RoleOwner)\n\tif !isRealmOwner {\n\t\tassertRealmIsNotLocked()\n\t\tassertBoardIsNotFrozen(board)\n\t}\n\n\tthread := mustGetThread(board, threadID)\n\treply := mustGetReply(thread, replyID)\n\tif reply.Hidden {\n\t\tpanic(\"flagging hidden comments or replies is not allowed\")\n\t}\n\n\tflagReply := func() {\n\t\tif reply.Hidden {\n\t\t\tpanic(\"flagged comment or reply is already hidden\")\n\t\t}\n\n\t\thide := flagItem(reply, caller, reason, getFlaggingThreshold(board.ID))\n\t\tif hide || isRealmOwner {\n\t\t\treply.Hidden = true\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"ReplyFlagged\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"replyID\", reply.ID.String(),\n\t\t\t\"reason\", reason,\n\t\t)\n\t}\n\n\t// Realm owners should be able to flag without permissions even when board is frozen\n\tif isRealmOwner {\n\t\tflagReply()\n\t\treturn\n\t}\n\n\targs := boards.Args{caller, board.ID, thread.ID, reply.ID, reason}\n\tboard.Permissions.WithPermission(caller, PermissionReplyFlag, args, func() {\n\t\tflagReply()\n\t})\n}\n"},{"name":"public_freeze.gno","body":"package boards2\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// FreezeBoard freezes a board so no more threads and comments can be created or modified.\nfunc FreezeBoard(cur realm, boardID boards.ID) {\n\tsetBoardReadonly(0, cur, boardID, true)\n}\n\n// UnfreezeBoard removes frozen status from a board.\nfunc UnfreezeBoard(cur realm, boardID boards.ID) {\n\tsetBoardReadonly(0, cur, boardID, false)\n}\n\n// IsBoardFrozen checks if a board has been frozen.\nfunc IsBoardFrozen(boardID boards.ID) bool {\n\tboard := mustGetBoard(boardID)\n\treturn board.Readonly\n}\n\n// FreezeThread freezes a thread so thread cannot be replied, modified or deleted.\n//\n// Fails if board is frozen.\nfunc FreezeThread(cur realm, boardID, threadID boards.ID) {\n\tsetThreadReadonly(0, cur, boardID, threadID, true)\n}\n\n// UnfreezeThread removes frozen status from a thread.\n//\n// Fails if board is frozen.\nfunc UnfreezeThread(cur realm, boardID, threadID boards.ID) {\n\tsetThreadReadonly(0, cur, boardID, threadID, false)\n}\n\n// IsThreadFrozen checks if a thread has been frozen.\n//\n// Returns true if board is frozen.\nfunc IsThreadFrozen(boardID, threadID boards.ID) bool {\n\tboard := mustGetBoard(boardID)\n\tthread := mustGetThread(board, threadID)\n\treturn board.Readonly || thread.Readonly\n}\n\nfunc setBoardReadonly(_ int, rlm realm, boardID boards.ID, readonly bool) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tassertRealmIsNotLocked()\n\n\tboard := mustGetBoard(boardID)\n\tif readonly {\n\t\tassertBoardIsNotFrozen(board)\n\t}\n\n\tcaller := rlm.Previous().Address()\n\targs := boards.Args{caller, board.ID, readonly}\n\tboard.Permissions.WithPermission(caller, PermissionBoardFreeze, args, func() {\n\t\tassertRealmIsNotLocked()\n\n\t\tboard.Readonly = readonly\n\n\t\tchain.Emit(\n\t\t\t\"BoardFreeze\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"frozen\", strconv.FormatBool(readonly),\n\t\t)\n\t})\n}\n\nfunc setThreadReadonly(_ int, rlm realm, boardID, threadID boards.ID, readonly bool) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tassertRealmIsNotLocked()\n\n\tboard := mustGetBoard(boardID)\n\tassertBoardIsNotFrozen(board)\n\n\tthread := mustGetThread(board, threadID)\n\tif readonly {\n\t\tassertThreadIsNotFrozen(thread)\n\t}\n\n\tcaller := rlm.Previous().Address()\n\targs := boards.Args{caller, board.ID, thread.ID, readonly}\n\tboard.Permissions.WithPermission(caller, PermissionThreadFreeze, args, func() {\n\t\tassertRealmIsNotLocked()\n\n\t\tthread.Readonly = readonly\n\n\t\tchain.Emit(\n\t\t\t\"ThreadFreeze\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"threadID\", thread.ID.String(),\n\t\t\t\"frozen\", strconv.FormatBool(readonly),\n\t\t)\n\t})\n}\n"},{"name":"public_invite.gno","body":"package boards2\n\nimport (\n\t\"chain\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\n// Invite contains a user invitation.\ntype Invite struct {\n\t// User is the user to invite.\n\tUser address\n\n\t// Role is the optional role to assign to the user.\n\tRole boards.Role\n}\n\n// InviteMember adds a member to the realm or to a board.\n//\n// A role can optionally be specified to be assigned to the new member.\nfunc InviteMember(cur realm, boardID boards.ID, user address, role boards.Role) {\n\tinviteMembers(0, cur, boardID, Invite{\n\t\tUser: user,\n\t\tRole: role,\n\t})\n}\n\n// InviteMembers adds one or more members to the realm or to a board.\n//\n// Board ID is only required when inviting a member to a specific board.\nfunc InviteMembers(cur realm, boardID boards.ID, invites ...Invite) {\n\tinviteMembers(0, cur, boardID, invites...)\n}\n\n// RequestInvite request to be invited to a board.\nfunc RequestInvite(cur realm, boardID boards.ID) {\n\tassertMembersUpdateIsEnabled(boardID)\n\n\tif !cur.Previous().IsUser() {\n\t\tpanic(\"caller must be user\")\n\t}\n\n\t// TODO: Request a fee (returned on accept) or registered user to avoid spam?\n\t//   WARNING: if a fee is added via unsafe.OriginSend(), the guard above\n\t//   must be tightened to IsUserCall() — IsUser() accepts maketx-run\n\t//   ephemeral realms which can consume the origin-send envelope before\n\t//   calling us, bypassing the fee. See\n\t//   docs/resources/effective-gno.md#verifying-inbound-coin-payments.\n\t// TODO: Make open invite requests optional (per board)\n\n\tboard := mustGetBoard(boardID)\n\tuser := cur.Previous().Address()\n\tif board.Permissions.HasUser(user) {\n\t\tpanic(\"caller is already a member\")\n\t}\n\n\tinvitee := user.String()\n\trequests, found := getInviteRequests(boardID)\n\tif !found {\n\t\trequests = bptree.NewBPTree32()\n\t\trequests.Set(invitee, time.Now())\n\t\tgInviteRequests.Set(boardID.Key(), requests)\n\t\treturn\n\t}\n\n\tif requests.Has(invitee) {\n\t\tpanic(\"invite request already exists\")\n\t}\n\n\trequests.Set(invitee, time.Now())\n}\n\n// AcceptInvite accepts a board invite request.\nfunc AcceptInvite(cur realm, boardID boards.ID, user address) {\n\tassertMembersUpdateIsEnabled(boardID)\n\tassertInviteRequestExists(boardID, user)\n\n\tboard := mustGetBoard(boardID)\n\tif board.Permissions.HasUser(user) {\n\t\tpanic(\"user is already a member\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tinvite := Invite{\n\t\tUser: user,\n\t\tRole: RoleGuest,\n\t}\n\targs := boards.Args{caller, boardID, []Invite{invite}}\n\tboard.Permissions.WithPermission(caller, PermissionMemberInvite, args, func() {\n\t\tassertMembersUpdateIsEnabled(boardID)\n\n\t\tinvitee := user.String()\n\t\trequests, found := getInviteRequests(boardID)\n\t\tif !found || !requests.Has(invitee) {\n\t\t\tpanic(\"invite request not found\")\n\t\t}\n\n\t\tif board.Permissions.HasUser(user) {\n\t\t\tpanic(\"user is already a member\")\n\t\t}\n\n\t\tboard.Permissions.SetUserRoles(user)\n\t\trequests.Remove(invitee)\n\n\t\tchain.Emit(\n\t\t\t\"MembersInvited\",\n\t\t\t\"invitedBy\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"members\", user.String()+\":\"+string(RoleGuest), // TODO: Support optional role assign\n\t\t)\n\t})\n}\n\n// RevokeInvite revokes a board invite request.\nfunc RevokeInvite(cur realm, boardID boards.ID, user address) {\n\tassertInviteRequestExists(boardID, user)\n\n\tboard := mustGetBoard(boardID)\n\tcaller := cur.Previous().Address()\n\targs := boards.Args{boardID, user, RoleGuest}\n\tboard.Permissions.WithPermission(caller, PermissionMemberInviteRevoke, args, func() {\n\t\tinvitee := user.String()\n\t\trequests, found := getInviteRequests(boardID)\n\t\tif !found || !requests.Has(invitee) {\n\t\t\tpanic(\"invite request not found\")\n\t\t}\n\n\t\trequests.Remove(invitee)\n\n\t\tchain.Emit(\n\t\t\t\"InviteRevoked\",\n\t\t\t\"revokedBy\", caller.String(),\n\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\"user\", user.String(),\n\t\t)\n\t})\n}\n\nfunc inviteMembers(_ int, rlm realm, boardID boards.ID, invites ...Invite) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tif len(invites) == 0 {\n\t\tpanic(\"one or more user invites are required\")\n\t}\n\n\tassertMembersUpdateIsEnabled(boardID)\n\tassertNoDuplicatedInvites(invites)\n\n\tperms := mustGetPermissions(boardID)\n\tcaller := rlm.Previous().Address()\n\targs := boards.Args{caller, boardID, invites}\n\tperms.WithPermission(caller, PermissionMemberInvite, args, func() {\n\t\tassertMembersUpdateIsEnabled(boardID)\n\n\t\tusers := make([]string, len(invites))\n\t\tfor _, v := range invites {\n\t\t\tassertMemberAddressIsValid(v.User)\n\n\t\t\tif perms.HasUser(v.User) {\n\t\t\t\tpanic(\"user is already a member: \" + v.User.String())\n\t\t\t}\n\n\t\t\t// NOTE: Permissions implementation should check that role is valid\n\t\t\tperms.SetUserRoles(v.User, v.Role)\n\t\t\tusers = append(users, v.User.String()+\":\"+string(v.Role))\n\t\t}\n\n\t\tchain.Emit(\n\t\t\t\"MembersInvited\",\n\t\t\t\"invitedBy\", caller.String(),\n\t\t\t\"boardID\", boardID.String(),\n\t\t\t\"members\", strings.Join(users, \",\"),\n\t\t)\n\t})\n}\n\nfunc assertInviteRequestExists(boardID boards.ID, user address) {\n\tinvitee := user.String()\n\trequests, found := getInviteRequests(boardID)\n\tif !found || !requests.Has(invitee) {\n\t\tpanic(\"invite request not found\")\n\t}\n}\n\nfunc assertNoDuplicatedInvites(invites []Invite) {\n\tif len(invites) == 1 {\n\t\treturn\n\t}\n\n\tseen := make(map[address]struct{}, len(invites))\n\tfor _, v := range invites {\n\t\tif _, found := seen[v.User]; found {\n\t\t\tpanic(\"duplicated invite: \" + v.User.String())\n\t\t}\n\n\t\tseen[v.User] = struct{}{}\n\t}\n}\n"},{"name":"public_lock.gno","body":"package boards2\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// LockRealm locks the realm making it readonly.\n//\n// WARNING: Realm can't be unlocked once locked.\n//\n// Realm can also be locked without locking realm members.\n// Realm members can be locked when locking the realm or afterwards.\n// This is relevant for two reasons, one so that members can be modified after the lock.\n// The other is for realm owners, who can delete threads and comments after the lock.\nfunc LockRealm(cur realm, lockRealmMembers bool) {\n\tassertRealmMembersAreNotLocked()\n\n\t// If realm members are not being locked assert that realm is not locked.\n\t// Members can be locked after locking the realm, in a second `LockRealm` call.\n\tif !lockRealmMembers {\n\t\tassertRealmIsNotLocked()\n\t}\n\n\tcaller := cur.Previous().Address()\n\tgPerms.WithPermission(caller, PermissionRealmLock, boards.Args{}, func() {\n\t\tgLocked.realm = true\n\t\tgLocked.realmMembers = lockRealmMembers\n\n\t\tchain.Emit(\n\t\t\t\"RealmLocked\",\n\t\t\t\"caller\", caller.String(),\n\t\t\t\"lockRealmMembers\", strconv.FormatBool(lockRealmMembers),\n\t\t)\n\t})\n}\n\n// IsRealmLocked checks if boards realm has been locked.\nfunc IsRealmLocked() bool {\n\treturn gLocked.realm\n}\n\n// AreRealmMembersLocked checks if realm members have been locked.\nfunc AreRealmMembersLocked() bool {\n\treturn gLocked.realmMembers\n}\n\nfunc assertRealmIsNotLocked() { // TODO: Add filtests for locked realm case to all public functions\n\tif gLocked.realm {\n\t\tpanic(\"realm is locked\")\n\t}\n}\n\nfunc assertRealmMembersAreNotLocked() { // TODO: Add filtests for locked members case to all public member functions\n\tif gLocked.realmMembers {\n\t\tpanic(\"realm and members are locked\")\n\t}\n}\n"},{"name":"render.gno","body":"package boards2\n\nimport (\n\t\"net/url\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/jeronimoalbi/mdform\"\n\t\"gno.land/p/jeronimoalbi/pager\"\n\t\"gno.land/p/leon/svgbtn\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/nt/markdown/foreign/v0\"\n\t\"gno.land/p/nt/mux/v0\"\n)\n\nconst (\n\tpageSizeDefault = 6\n\tpageSizeReplies = 10\n\t// pageSizeFlat is the page size of the flat \"all comments\" view (?flat=1).\n\t// Each comment renders exactly one \u003cgno-foreign\u003e block (no recursion); with\n\t// the OP (and any repost-source body) the per-page total stays well under\n\t// maxRenderedBodies, so a flat page can't hit the render cap.\n\tpageSizeFlat = 50\n)\n\n// maxRenderedBodies caps user bodies wrapped in \u003cgno-foreign\u003e per page\n// render, kept a margin under gnoweb's per-render foreign-block cap\n// (foreign.MaxBlocksPerRender, sourced from chain/markdown so it can't\n// drift from the renderer). On reaching it the tree stops descending\n// and shows a truncation notice instead of letting the renderer blank\n// comments past the cap. The margin absorbs the OP, repost source\n// bodies, and chrome.\n//\n// Computed lazily (a func, not a package-level var) so it tracks the\n// native cap across chain upgrades: a var initializer runs once at\n// realm-init and persists, freezing a stale value if the underlying\n// MaxBlocksPerRender ever changes; recomputing per render re-reads it.\nfunc maxRenderedBodies() int {\n\treturn foreign.MaxBlocksPerRender() - 10\n}\n\n// sortToggleLink builds the asc/desc sort-toggle link for path. The label\n// names the DESTINATION order (what you get by clicking), not the order you're\n// currently viewing, and ?page= is reset since page N of one order is a\n// different slice in the other. It reports whether the CURRENT order is\n// descending, so callers can drive their own (signed-count or reverse-iterate)\n// pagination. Other query params (e.g. flat=1) are preserved.\nfunc sortToggleLink(path string) (link string, desc bool) {\n\tr := parseRealmPath(path)\n\tdesc = r.Query.Get(\"order\") == \"desc\"\n\tr.Query.Del(\"page\")\n\tif desc {\n\t\tr.Query.Set(\"order\", \"asc\")\n\t\treturn md.Link(\"oldest first\", r.String()), true\n\t}\n\tr.Query.Set(\"order\", \"desc\")\n\treturn md.Link(\"newest first\", r.String()), false\n}\n\nconst menuManageBoard = \"manageBoard\"\n\nvar (\n\tcreateBoardURI = gRealmPath + \":create-board\"\n\tadminUsersURI  = gRealmPath + \":admin-users\"\n\thelpURI        = gRealmPath + \":help\"\n)\n\nfunc Render(path string) string {\n\tvar (\n\t\tb      strings.Builder\n\t\trouter = mux.NewRouter()\n\t)\n\n\trouter.HandleFunc(\"\", renderBoardsList)\n\trouter.HandleFunc(\"help\", renderHelp)\n\trouter.HandleFunc(\"admin-users\", renderMembers)\n\trouter.HandleFunc(\"create-board\", renderCreateBoard)\n\trouter.HandleFunc(\"{board}\", renderBoard)\n\trouter.HandleFunc(\"{board}/members\", renderMembers)\n\trouter.HandleFunc(\"{board}/invites\", renderInvites)\n\trouter.HandleFunc(\"{board}/banned-users\", renderBannedUsers)\n\trouter.HandleFunc(\"{board}/create-thread\", renderCreateThread)\n\trouter.HandleFunc(\"{board}/invite-member\", renderInviteMember)\n\trouter.HandleFunc(\"{board}/{thread}\", renderThread)\n\trouter.HandleFunc(\"{board}/{thread}/flag\", renderFlagPost)\n\trouter.HandleFunc(\"{board}/{thread}/flagging-reasons\", renderFlaggingReasonsPost)\n\trouter.HandleFunc(\"{board}/{thread}/reply\", renderReplyPost)\n\trouter.HandleFunc(\"{board}/{thread}/edit\", renderEditThread)\n\trouter.HandleFunc(\"{board}/{thread}/repost\", renderRepostThread)\n\trouter.HandleFunc(\"{board}/{thread}/{reply}\", renderReply)\n\trouter.HandleFunc(\"{board}/{thread}/{reply}/flag\", renderFlagPost)\n\trouter.HandleFunc(\"{board}/{thread}/{reply}/flagging-reasons\", renderFlaggingReasonsPost)\n\trouter.HandleFunc(\"{board}/{thread}/{reply}/reply\", renderReplyPost)\n\trouter.HandleFunc(\"{board}/{thread}/{reply}/edit\", renderEditReply)\n\n\trouter.NotFoundHandler = func(res *mux.ResponseWriter, _ *mux.Request) {\n\t\tres.Write(md.Blockquote(\"Path not found\"))\n\t}\n\n\t// Render common realm header before resolving render path\n\tif Notice != \"\" {\n\t\tb.WriteString(infoAlert(\"Notice\", Notice))\n\t}\n\n\t// Render view for current path\n\tb.WriteString(router.Render(path))\n\n\treturn b.String()\n}\n\nfunc renderHelp(res *mux.ResponseWriter, _ *mux.Request) {\n\tres.Write(md.H1(\"Boards Help\"))\n\tif Help != \"\" {\n\t\tres.Write(Help)\n\t\treturn\n\t}\n\n\tlink := RealmLink.Call(\"SetHelp\", \"content\", \"\")\n\tres.Write(md.H3(\"Help content has not been uploaded\"))\n\tres.Write(\"Do you want to \" + md.Link(\"upload boards help\", link) + \"?\")\n}\n\nfunc renderBoardsList(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(md.H1(\"Boards\"))\n\trenderBoardListMenu(res, req)\n\tres.Write(md.HorizontalRule())\n\n\tif gListedBoardsByID.Size() == 0 {\n\t\tres.Write(md.H3(\"Currently there are no boards\"))\n\t\tres.Write(\"Be the first to \" + md.Link(\"create a new board\", createBoardURI) + \"!\")\n\t\treturn\n\t}\n\n\tp := newClampedPager(req.RawPath, gListedBoardsByID.Size(), pageSizeDefault)\n\n\trender := func(_ string, v any) bool {\n\t\tboard := v.(*boards.Board)\n\t\tuserLink := userLink(board.Creator)\n\t\tdate := board.CreatedAt.Format(dateFormat)\n\n\t\tres.Write(md.H6(md.Link(board.Name, makeBoardURI(board))))\n\t\tres.Write(\"Created by \" + userLink + \" on \" + date + \", #\" + board.ID.String() + \"  \\n\")\n\n\t\tstatus := strconv.Itoa(board.Threads.Size()) + \" threads\"\n\t\tif board.Readonly {\n\t\t\tstatus += \", read-only\"\n\t\t}\n\n\t\tres.Write(md.Bold(status) + \"\\n\\n\")\n\t\treturn false\n\t}\n\n\tres.Write(\"Sort by: \")\n\tlink, desc := sortToggleLink(req.RawPath)\n\tres.Write(link + \"\\n\\n\")\n\tif desc {\n\t\tgListedBoardsByID.ReverseIterateByOffset(p.Offset(), p.PageSize(), render)\n\t} else {\n\t\tgListedBoardsByID.IterateByOffset(p.Offset(), p.PageSize(), render)\n\t}\n\n\tif p.HasPages() {\n\t\tres.Write(md.HorizontalRule())\n\t\tres.Write(pager.Picker(p))\n\t}\n}\n\nfunc renderBoardListMenu(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(md.Link(\"Create Board\", createBoardURI))\n\tres.Write(\" • \")\n\tres.Write(md.Link(\"List Admin Users\", adminUsersURI))\n\tres.Write(\" • \")\n\tres.Write(md.Link(\"Help\", helpURI))\n\tres.Write(\"\\n\\n\")\n}\n\nfunc renderCreateBoard(res *mux.ResponseWriter, _ *mux.Request) {\n\tform := mdform.New(\"exec\", \"CreateBoard\")\n\tform.Input(\n\t\t\"name\",\n\t\t\"placeholder\", \"Board name\",\n\t\t\"required\", \"true\",\n\t)\n\tform.Radio(\n\t\t\"listed\",\n\t\t\"true\",\n\t\t\"checked\", \"true\",\n\t\t\"description\", \"Should board be publicly listed?\",\n\t)\n\tform.Radio(\n\t\t\"listed\",\n\t\t\"false\",\n\t)\n\tform.Radio(\n\t\t\"open\",\n\t\t\"true\",\n\t\t\"description\", \"Should anyone be allowed to create threads and comments?\",\n\t)\n\tform.Radio(\n\t\t\"open\",\n\t\t\"false\",\n\t\t\"checked\", \"true\",\n\t)\n\n\tres.Write(md.H1(\"Boards: Create Board\"))\n\tres.Write(md.Link(\"← Back to boards\", gRealmPath) + \"\\n\\n\")\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\t\"Boards are by default listed by the realm but they can optionally \" +\n\t\t\t\t\"be created so they are only found by their URL.\",\n\t\t),\n\t)\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\t\"They can also be created to be open so anyone is allowed to create \" +\n\t\t\t\t\"new threads and also to comment on any thread within the open board.\",\n\t\t),\n\t)\n\tres.Write(form.String())\n\tres.Write(\"\\n\\n**Done?** \" + svgbtn.ButtonWithRadius(136, 32, 4, \"#E2E2E2\", \"#54595D\", \"Return to boards\", gRealmPath) + \"\\n\")\n}\n\nfunc renderMembers(res *mux.ResponseWriter, req *mux.Request) {\n\tboardID := boards.ID(0)\n\tperms := gPerms\n\tname := req.GetVar(\"board\")\n\tif name != \"\" {\n\t\tboard, found := gBoards.GetByName(name)\n\t\tif !found {\n\t\t\tres.Write(md.H3(\"Board not found\"))\n\t\t\treturn\n\t\t}\n\n\t\tboardID = board.ID\n\t\tperms = board.Permissions\n\n\t\tres.Write(md.H1(board.Name + \" Members\"))\n\t\tres.Write(md.H3(\"These are the board members\"))\n\t} else {\n\t\tres.Write(md.H1(\"Admin Users\"))\n\t\tres.Write(md.H3(\"These are the admin users of the realm\"))\n\t}\n\n\t// Create a pager with a small page size to reduce\n\t// the number of username lookups per page.\n\tp := newClampedPager(req.RawPath, perms.UsersCount(), pageSizeDefault)\n\n\ttable := mdtable.Table{\n\t\tHeaders: []string{\"Member\", \"Role\", \"Actions\"},\n\t}\n\n\tperms.IterateUsers(p.Offset(), p.PageSize(), func(u boards.User) bool {\n\t\tactions := []string{\n\t\t\tmd.Link(\"remove\", RealmLink.Call(\n\t\t\t\t\"RemoveMember\",\n\t\t\t\t\"boardID\", boardID.String(),\n\t\t\t\t\"member\", u.Address.String(),\n\t\t\t)),\n\t\t\tmd.Link(\"change role\", RealmLink.Call(\n\t\t\t\t\"ChangeMemberRole\",\n\t\t\t\t\"boardID\", boardID.String(),\n\t\t\t\t\"member\", u.Address.String(),\n\t\t\t\t\"role\", \"\",\n\t\t\t)),\n\t\t}\n\n\t\ttable.Append([]string{\n\t\t\tuserLink(u.Address),\n\t\t\trolesToString(u.Roles),\n\t\t\tstrings.Join(actions, \" • \"),\n\t\t})\n\t\treturn false\n\t})\n\tres.Write(table.String())\n\n\tif p.HasPages() {\n\t\tres.Write(\"\\n\" + pager.Picker(p))\n\t}\n}\n\nfunc renderInvites(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(md.H3(\"Board not found\"))\n\t\treturn\n\t}\n\n\tres.Write(md.H1(board.Name + \" Invite Requests\"))\n\n\trequests, found := getInviteRequests(board.ID)\n\tif !found || requests.Size() == 0 {\n\t\tres.Write(md.H3(\"Board has no invite requests\"))\n\t\treturn\n\t}\n\n\tp := newClampedPager(req.RawPath, requests.Size(), pageSizeDefault)\n\n\ttable := mdtable.Table{\n\t\tHeaders: []string{\"User\", \"Request Date\", \"Actions\"},\n\t}\n\n\tres.Write(md.H3(\"These users have requested to be invited to the board\"))\n\trequests.ReverseIterateByOffset(p.Offset(), p.PageSize(), func(addr string, v any) bool {\n\t\tactions := []string{\n\t\t\tmd.Link(\"accept\", RealmLink.Call(\n\t\t\t\t\"AcceptInvite\",\n\t\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\t\"user\", addr,\n\t\t\t)),\n\t\t\tmd.Link(\"revoke\", RealmLink.Call(\n\t\t\t\t\"RevokeInvite\",\n\t\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\t\"user\", addr,\n\t\t\t)),\n\t\t}\n\n\t\ttable.Append([]string{\n\t\t\tuserLink(address(addr)),\n\t\t\tv.(time.Time).Format(dateFormat),\n\t\t\tstrings.Join(actions, \" • \"),\n\t\t})\n\t\treturn false\n\t})\n\n\tres.Write(table.String())\n\n\tif p.HasPages() {\n\t\tres.Write(\"\\n\" + pager.Picker(p))\n\t}\n}\n\nfunc renderBannedUsers(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(md.H3(\"Board not found\"))\n\t\treturn\n\t}\n\n\tres.Write(md.H1(board.Name + \" Banned Users\"))\n\n\tbanned, found := getBannedUsers(board.ID)\n\tif !found || banned.Size() == 0 {\n\t\tres.Write(md.H3(\"Board has no banned users\"))\n\t\treturn\n\t}\n\n\tp := newClampedPager(req.RawPath, banned.Size(), pageSizeDefault)\n\n\ttable := mdtable.Table{\n\t\tHeaders: []string{\"User\", \"Banned Until\", \"Actions\"},\n\t}\n\n\tres.Write(md.H3(\"These users have been banned from the board\"))\n\tbanned.ReverseIterateByOffset(p.Offset(), p.PageSize(), func(addr string, v any) bool {\n\t\ttable.Append([]string{\n\t\t\tuserLink(address(addr)),\n\t\t\tv.(time.Time).Format(dateFormat),\n\t\t\tmd.Link(\"unban\", RealmLink.Call(\n\t\t\t\t\"Unban\",\n\t\t\t\t\"boardID\", board.ID.String(),\n\t\t\t\t\"user\", addr,\n\t\t\t\t\"reason\", \"\",\n\t\t\t)),\n\t\t})\n\t\treturn false\n\t})\n\n\tres.Write(table.String())\n\n\tif p.HasPages() {\n\t\tres.Write(\"\\n\" + pager.Picker(p))\n\t}\n}\n\nfunc infoAlert(title, msg string) string {\n\theader := strings.TrimSpace(\"[!INFO] \" + title)\n\treturn md.Blockquote(header + \"\\n\" + msg)\n}\n\nfunc rolesToString(roles []boards.Role) string {\n\tif len(roles) == 0 {\n\t\treturn \"\"\n\t}\n\n\tnames := make([]string, len(roles))\n\tfor i, r := range roles {\n\t\tnames[i] = string(r)\n\t}\n\treturn strings.Join(names, \", \")\n}\n\nfunc menuURL(name string) string {\n\t// TODO: Menu URL works because no other GET arguments are being used\n\treturn \"?menu=\" + name\n}\n\nfunc getCurrentMenu(rawURL string) string {\n\t_, rawQuery, found := strings.Cut(rawURL, \"?\")\n\tif !found {\n\t\treturn \"\"\n\t}\n\n\tquery, _ := url.ParseQuery(rawQuery)\n\treturn query.Get(\"menu\")\n}\n"},{"name":"render_board.gno","body":"package boards2\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/jeronimoalbi/mdform\"\n\t\"gno.land/p/jeronimoalbi/pager\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/mdalert/v0\"\n\t\"gno.land/p/nt/mux/v0\"\n)\n\nfunc renderBoard(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(md.H3(\"The board you are looking for does not exist\"))\n\t\tres.Write(\"Do you want to \" + md.Link(\"create a new board\", createBoardURI) + \"?\")\n\t\treturn\n\t}\n\n\tcreatorLink := userLink(board.Creator)\n\tdate := board.CreatedAt.Format(dateFormat)\n\n\tres.Write(md.H1(md.Link(\"Boards\", gRealmPath) + \" › \" + board.Name))\n\tif board.Readonly {\n\t\tres.Write(\n\t\t\tmdalert.Warning(\"Info\", \"Creating new threads and commenting are disabled within this board\") + \"\\n\",\n\t\t)\n\t}\n\n\tres.Write(\"Created by \" + creatorLink + \" on \" + date + \", #\" + board.ID.String())\n\tres.Write(\"  \\n\" + renderBoardMenu(board, req))\n\tres.Write(md.HorizontalRule())\n\n\tif board.Threads.Size() == 0 {\n\t\tres.Write(md.H3(\"This board doesn't have any threads\"))\n\t\tif !board.Readonly {\n\t\t\tstartConversationLink := md.Link(\"start a new conversation\", makeCreateThreadURI(board))\n\t\t\tres.Write(\"Do you want to \" + startConversationLink + \" in this board?\")\n\t\t}\n\t\treturn\n\t}\n\n\tp := newClampedPager(req.RawPath, board.Threads.Size(), pageSizeDefault)\n\n\trender := func(thread *boards.Post) bool {\n\t\tres.Write(renderThreadSummary(thread) + \"\\n\")\n\t\treturn false\n\t}\n\n\tres.Write(\"Sort by: \")\n\n\tlink, desc := sortToggleLink(req.RawPath)\n\tres.Write(link + \"\\n\\n\")\n\n\tcount := p.PageSize()\n\tif desc {\n\t\tcount = -count // Reverse iterate\n\t}\n\n\tboard.Threads.Iterate(p.Offset(), count, render)\n\n\tif p.HasPages() {\n\t\tres.Write(md.HorizontalRule())\n\t\tres.Write(pager.Picker(p))\n\t}\n}\n\n// renderSubMenu renders a sub-menu with a distinct visual pattern.\nfunc renderSubMenu(items []string) string {\n\tif len(items) == 0 {\n\t\treturn \"\"\n\t}\n\treturn \"└─ \" + strings.Join(items, \" • \") + \"\\n\"\n}\n\nfunc renderBoardMenu(board *boards.Board, req *mux.Request) string {\n\tvar (\n\t\tb               strings.Builder\n\t\tboardMembersURL = makeBoardURI(board) + \"/members\"\n\t)\n\n\tif board.Readonly {\n\t\tb.WriteString(md.Link(\"List Members\", boardMembersURL))\n\t\tb.WriteString(\" • \")\n\t\tb.WriteString(md.Link(\"Unfreeze Board\", makeUnfreezeBoardURI(board)))\n\t\tb.WriteString(\"\\n\")\n\t} else {\n\t\tb.WriteString(\"↳ \")\n\t\tb.WriteString(md.Link(\"Create Thread\", makeCreateThreadURI(board)))\n\t\tb.WriteString(\" • \")\n\t\tb.WriteString(md.Link(\"Request Invite\", makeRequestInviteURI(board)))\n\t\tb.WriteString(\" • \")\n\n\t\tmenu := getCurrentMenu(req.RawPath)\n\t\tif menu == menuManageBoard {\n\t\t\tb.WriteString(md.Bold(\"Manage Board\"))\n\t\t} else {\n\t\t\tb.WriteString(md.Link(\"Manage Board\", menuURL(menuManageBoard)))\n\t\t}\n\n\t\tb.WriteString(\"  \\n\")\n\n\t\tif menu == menuManageBoard {\n\t\t\tsubMenuItems := []string{\n\t\t\t\tmd.Link(\"Invite Member\", makeInviteMemberURI(board)),\n\t\t\t\tmd.Link(\"List Invite Requests\", makeBoardURI(board)+\"/invites\"),\n\t\t\t\tmd.Link(\"List Members\", boardMembersURL),\n\t\t\t\tmd.Link(\"List Banned Users\", makeBoardURI(board)+\"/banned-users\"),\n\t\t\t\tmd.Link(\"Freeze Board\", makeFreezeBoardURI(board)),\n\t\t\t}\n\t\t\tb.WriteString(renderSubMenu(subMenuItems))\n\t\t}\n\t}\n\n\tb.WriteString(\"\\n\")\n\treturn b.String()\n}\n\nfunc renderInviteMember(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\tform := mdform.New(\"exec\", \"InviteMember\")\n\tform.Input(\n\t\t\"boardID\",\n\t\t\"placeholder\", \"Board ID\",\n\t\t\"value\", board.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"user\",\n\t\t\"placeholder\", \"Address\",\n\t\t\"required\", \"true\",\n\t)\n\tform.Select(\n\t\t\"role\",\n\t\tstring(RoleOwner),\n\t)\n\tform.Select(\n\t\t\"role\",\n\t\tstring(RoleAdmin),\n\t)\n\tform.Select(\n\t\t\"role\",\n\t\tstring(RoleModerator),\n\t)\n\tform.Select(\n\t\t\"role\",\n\t\tstring(RoleGuest),\n\t\t\"selected\", \"true\",\n\t)\n\n\tres.Write(md.H1(board.Name + \": Invite Member\"))\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\t\"Both open and invite only boards can have multiple members with different roles within a \"+\n\t\t\t\t\"board, where members can have a single role at a time.\",\n\t\t) +\n\t\t\tmd.Paragraph(\n\t\t\t\t\"Boards are independent communities which could apply different permissions per role than \"+\n\t\t\t\t\t\"other boards, but generally Boards2 supports four roles, _owner_, _admin_, _moderator_ \"+\n\t\t\t\t\t\"and _guest_.\",\n\t\t\t) +\n\t\t\tmd.Paragraph(\n\t\t\t\t\"Member will be added to \"+md.Link(board.Name, makeBoardURI(board))+\" board.\",\n\t\t\t),\n\t)\n\tres.Write(form.String())\n}\n"},{"name":"render_post.gno","body":"package boards2\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/jeronimoalbi/mdform\"\n\t\"gno.land/p/leon/svgbtn\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/nt/markdown/foreign/v0\"\n\t\"gno.land/p/nt/mdalert/v0\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// renderPost renders a post and (unless it's a leaf or capped) its replies.\n// desc is the page's sort order, forwarded so a comment's nested inline\n// children (renderSubReplies) match the order of the view they appear in. It\n// is unused for the top-level/re-root call (path != \"\" routes to\n// renderTopLevelReplies, which derives the order from the path); those\n// callers pass false.\nfunc renderPost(post *boards.Post, path, indent string, levels int, budget *int, desc bool) string {\n\tvar b strings.Builder\n\n\t// Thread reposts might not have a title, if so get title from source thread\n\ttitle := post.Title\n\tif boards.IsRepost(post) \u0026\u0026 title == \"\" {\n\t\tif board, ok := gBoards.Get(post.OriginalBoardID); ok {\n\t\t\tif src, ok := getThread(board, post.ParentID); ok {\n\t\t\t\ttitle = src.Title\n\t\t\t}\n\t\t}\n\t}\n\n\tif title != \"\" { // Replies don't have a title\n\t\tb.WriteString(md.H2(md.EscapeText(title)))\n\t}\n\n\tb.WriteString(indent + \"\\n\")\n\tb.WriteString(renderPostContent(post, indent, levels, budget))\n\n\tif post.Replies.Size() == 0 {\n\t\treturn b.String()\n\t}\n\n\t// In practice this only fires for the re-rooted view's context-parent,\n\t// which renderPostInner renders with an explicit levels==0. The thread\n\t// recursion does levels-1 in BOTH renderPost and renderTopLevelReplies/\n\t// renderSubReplies, so levels drops by 2 per nesting level and skips 0 —\n\t// i.e. levels does NOT bound depth in the thread view; the render budget\n\t// and the per-node breadth cap (renderSubReplies) are the real bounds.\n\tif levels == 0 {\n\t\tb.WriteString(indent + \"\\n\")\n\t\treturn b.String()\n\t}\n\n\tif path != \"\" {\n\t\tb.WriteString(renderTopLevelReplies(post, path, indent, levels-1, budget))\n\t} else {\n\t\tb.WriteString(renderSubReplies(post, indent, levels-1, budget, desc))\n\t}\n\treturn b.String()\n}\n\nfunc renderPostContent(post *boards.Post, indent string, levels int, budget *int) string {\n\tvar b strings.Builder\n\n\t// Author and date header\n\tcreatorLink := userLink(post.Creator)\n\troleBadge := getRoleBadge(post)\n\tdate := post.CreatedAt.Format(dateFormat)\n\tb.WriteString(indent)\n\tb.WriteString(md.Bold(creatorLink) + roleBadge + \" · \" + date)\n\tif !boards.IsThread(post) {\n\t\tb.WriteString(\" \" + md.Link(\"#\"+post.ID.String(), makeReplyURI(post)))\n\t}\n\tb.WriteString(\"  \\n\")\n\n\t// Flagged comment should be hidden, but replies still visible (see: #3480)\n\t// Flagged threads will be hidden by render function caller.\n\tif post.Hidden {\n\t\tlink := md.Link(\"inappropriate\", makeFlaggingReasonsURI(post))\n\t\tb.WriteString(indentBody(indent, \"⚠ Reply is hidden as it has been flagged as \"+link))\n\t\tb.WriteString(\"\\n\")\n\t\treturn b.String()\n\t}\n\n\tsrcContent, srcPost := renderSourcePost(post, indent, budget)\n\tif boards.IsRepost(post) \u0026\u0026 srcPost != nil {\n\t\tmsg := ufmt.Sprintf(\n\t\t\t\"Original thread is %s  \\nCreated by %s on %s\",\n\t\t\tmd.Link(srcPost.Title, makeThreadURI(srcPost)),\n\t\t\tuserLink(srcPost.Creator),\n\t\t\tsrcPost.CreatedAt.Format(dateFormat),\n\t\t)\n\n\t\tb.WriteString(mdalert.New(mdalert.TypeInfo, \"Thread Repost\", msg, true).String())\n\t\tb.WriteString(\"\\n\")\n\t}\n\n\t// Render repost body before original thread's body\n\tif post.Body != \"\" {\n\t\tb.WriteString(indentForeignBody(indent, post.Body, budget) + \"\\n\")\n\t\tif srcContent != \"\" {\n\t\t\t// Add extra line to separate repost content from original thread content\n\t\t\tb.WriteString(\"\\n\")\n\t\t}\n\t}\n\n\tb.WriteString(srcContent)\n\n\t// Add a newline to separate source deleted message from repost body content\n\tif boards.IsRepost(post) \u0026\u0026 srcPost == nil \u0026\u0026 len(post.Body) \u003e 0 {\n\t\tb.WriteString(\"\\n\\n\")\n\t}\n\n\t// Split thread content and actions\n\tif boards.IsThread(post) \u0026\u0026 !boards.IsRepost(post) {\n\t\tb.WriteString(\"\\n\")\n\t}\n\n\t// Action buttons\n\tb.WriteString(indent)\n\tif !boards.IsThread(post) { // is comment\n\t\tb.WriteString(\"  \\n\")\n\t\tb.WriteString(indent)\n\t}\n\n\tactions := []string{\n\t\tmd.Link(\"Flag\", makeFlagURI(post)),\n\t}\n\n\tif boards.IsThread(post) {\n\t\trepostAction := md.Link(\"Repost\", makeCreateRepostURI(post))\n\t\tif post.Reposts.Size() \u003e 0 {\n\t\t\trepostAction += \" [\" + strconv.Itoa(post.Reposts.Size()) + \"]\"\n\t\t}\n\t\tactions = append(actions, repostAction)\n\t}\n\n\tisReadonly := post.Readonly || post.Board.Readonly\n\t// A reply doesn't carry the thread's frozen flag (FreezeThread sets\n\t// Readonly on the thread post only), so check the enclosing thread too —\n\t// otherwise a frozen thread's replies show Reply/Edit/Delete links that\n\t// the backend rejects. Mirrors the IsReadonly helper (board || thread).\n\tif !isReadonly \u0026\u0026 !boards.IsThread(post) {\n\t\tif t, ok := getThread(post.Board, post.ThreadID); ok {\n\t\t\tisReadonly = t.Readonly\n\t\t}\n\t}\n\tif !isReadonly {\n\t\treplyLabel := \"Reply\"\n\t\tif boards.IsThread(post) {\n\t\t\treplyLabel = \"Comment\"\n\t\t}\n\t\treplyAction := md.Link(replyLabel, makeCreateReplyURI(post))\n\t\t// Add reply count if any\n\t\tif post.Replies.Size() \u003e 0 {\n\t\t\treplyAction += \" [\" + strconv.Itoa(post.Replies.Size()) + \"]\"\n\t\t}\n\n\t\tactions = append(\n\t\t\tactions,\n\t\t\treplyAction,\n\t\t\tmd.Link(\"Edit\", makeEditPostURI(post)),\n\t\t\tmd.Link(\"Delete\", makeDeletePostURI(post)),\n\t\t)\n\t}\n\n\tif levels == 0 {\n\t\tswitch {\n\t\tcase boards.IsThread(post):\n\t\t\tactions = append(actions, md.Link(\"Show all Replies\", makeThreadURI(post)))\n\t\tcase post.Replies.Size() \u003e 0:\n\t\t\t// Reached at levels==0 — in practice the re-rooted view's\n\t\t\t// context-parent (see renderPost). It still has replies below, so\n\t\t\t// re-root here (Reddit/HN \"continue this thread\") to keep the\n\t\t\t// subtree drillable instead of bouncing to the thread root.\n\t\t\tactions = append(actions, md.Link(\"Continue this thread →\", makeReplyURI(post)))\n\t\t}\n\t}\n\n\tb.WriteString(\"↳ \" + strings.Join(actions, \" • \") + \"\\n\")\n\treturn b.String()\n}\n\nfunc renderPostInner(post *boards.Post, path string) string {\n\tif boards.IsThread(post) {\n\t\treturn \"\"\n\t}\n\n\tvar (\n\t\ts         string\n\t\tthreadID  = post.ThreadID\n\t\tthread, _ = getThread(post.Board, threadID)\n\t\tbudget    = maxRenderedBodies()\n\t)\n\n\t// Fully render parent if it's not a repost.\n\tif !boards.IsRepost(post) {\n\t\tparentID := post.ParentID\n\t\tparent := thread\n\n\t\tif thread.ID != parentID {\n\t\t\tparent, _ = getReply(thread, parentID)\n\t\t}\n\n\t\ts += renderPost(parent, \"\", \"\", 0, \u0026budget, false) + \"\\n\"\n\t}\n\n\t// Pass the reply's own path so renderPost routes to renderTopLevelReplies\n\t// and paginates this comment's direct replies (the re-root has its own\n\t// ?page= — no collision with the thread view, which is a different path).\n\t// desc=false: order is derived from path by renderTopLevelReplies.\n\ts += renderPost(post, path, \"\u003e \", 5, \u0026budget, false)\n\treturn s\n}\n\nfunc renderSourcePost(post *boards.Post, indent string, budget *int) (string, *boards.Post) {\n\tif !boards.IsRepost(post) {\n\t\treturn \"\", nil\n\t}\n\n\tindent += \"\u003e \"\n\n\t// TODO: figure out a way to decouple posts from a global storage.\n\tboard, ok := gBoards.Get(post.OriginalBoardID)\n\tif !ok {\n\t\t// TODO: Boards can't be deleted so this might be redundant\n\t\treturn indentBody(indent, \"⚠ Source board has been deleted\"), nil\n\t}\n\n\tsrcPost, ok := getThread(board, post.ParentID)\n\tif !ok {\n\t\treturn indentBody(indent, \"⚠ Source post has been deleted\"), nil\n\t}\n\n\tif srcPost.Hidden {\n\t\treturn indentBody(indent, \"⚠ Source post has been flagged as inappropriate\"), nil\n\t}\n\n\treturn indentForeignBody(indent, srcPost.Body, budget) + \"\\n\\n\", srcPost\n}\n\nfunc renderFlagPost(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\t// Thread ID must always be available\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := getThread(board, boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\t// Parse reply ID when post is a reply\n\tvar reply *boards.Post\n\trawID = req.GetVar(\"reply\")\n\tisReply := rawID != \"\"\n\tif isReply {\n\t\treplyID, err := strconv.Atoi(rawID)\n\t\tif err != nil {\n\t\t\tres.Write(\"Invalid reply ID: \" + md.EscapeText(rawID))\n\t\t\treturn\n\t\t}\n\n\t\treply, _ = getReply(thread, boards.ID(replyID))\n\t\tif reply == nil {\n\t\t\tres.Write(\"Reply not found\")\n\t\t\treturn\n\t\t}\n\t}\n\n\texec := \"FlagThread\"\n\tif isReply {\n\t\texec = \"FlagReply\"\n\t}\n\n\tform := mdform.New(\"exec\", exec)\n\tform.Input(\n\t\t\"boardID\",\n\t\t\"placeholder\", \"Board ID\",\n\t\t\"value\", board.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"threadID\",\n\t\t\"placeholder\", \"Thread ID\",\n\t\t\"value\", thread.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\n\tif isReply {\n\t\tform.Input(\n\t\t\t\"replyID\",\n\t\t\t\"placeholder\", \"Reply ID\",\n\t\t\t\"value\", reply.ID.String(),\n\t\t\t\"readonly\", \"true\",\n\t\t)\n\t}\n\n\tform.Input(\n\t\t\"reason\",\n\t\t\"placeholder\", \"Flagging Reason\",\n\t)\n\n\t// Breadcrumb navigation\n\tbackLink := md.Link(\"← Back to thread\", makeThreadURI(thread))\n\n\tif isReply {\n\t\tres.Write(md.H1(board.Name + \": Flag Comment\"))\n\t} else {\n\t\tres.Write(md.H1(board.Name + \": Flag Thread\"))\n\t}\n\tres.Write(backLink + \"\\n\\n\")\n\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\t\"Thread or comment moderation is done through flagging, which is usually done \"+\n\t\t\t\t\"by board members with the moderator role, though other roles could also potentially flag.\",\n\t\t) +\n\t\t\tmd.Paragraph(\n\t\t\t\t\"Flagging relies on a configurable threshold, which by default is of one flag, that when \"+\n\t\t\t\t\t\"reached leads to the flagged thread or comment to be hidden.\",\n\t\t\t) +\n\t\t\tmd.Paragraph(\n\t\t\t\t\"Flagging thresholds can be different within each board.\",\n\t\t\t),\n\t)\n\n\tif isReply {\n\t\tres.Write(\n\t\t\tmd.Paragraph(\n\t\t\t\tufmt.Sprintf(\n\t\t\t\t\t\"⚠ You are flagging a %s from %s ⚠\",\n\t\t\t\t\tmd.Link(\"comment\", makeReplyURI(reply)),\n\t\t\t\t\tuserLink(reply.Creator),\n\t\t\t\t),\n\t\t\t),\n\t\t)\n\t} else {\n\t\tres.Write(\n\t\t\tmd.Paragraph(\n\t\t\t\tufmt.Sprintf(\n\t\t\t\t\t\"⚠ You are flagging the thread: %s ⚠\",\n\t\t\t\t\tmd.Link(thread.Title, makeThreadURI(thread)),\n\t\t\t\t),\n\t\t\t),\n\t\t)\n\t}\n\n\tres.Write(form.String())\n\tres.Write(\"\\n\\n**Done?** \" + svgbtn.ButtonWithRadius(136, 32, 4, \"#E2E2E2\", \"#54595D\", \"Return to thread\", makeThreadURI(thread)) + \"\\n\")\n}\n\nfunc renderFlaggingReasonsPost(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\t// Thread ID must always be available\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := getThread(board, boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\tflags := thread.Flags\n\n\t// Parse reply ID when post is a reply\n\tvar reply *boards.Post\n\trawID = req.GetVar(\"reply\")\n\tisReply := rawID != \"\"\n\tif isReply {\n\t\treplyID, err := strconv.Atoi(rawID)\n\t\tif err != nil {\n\t\t\tres.Write(\"Invalid reply ID: \" + md.EscapeText(rawID))\n\t\t\treturn\n\t\t}\n\n\t\treply, found = getReply(thread, boards.ID(replyID))\n\t\tif !found {\n\t\t\tres.Write(\"Reply not found\")\n\t\t\treturn\n\t\t}\n\n\t\tflags = reply.Flags\n\t}\n\n\ttable := mdtable.Table{\n\t\tHeaders: []string{\"Moderator\", \"Reason\"},\n\t}\n\n\tflags.Iterate(0, flags.Size(), func(f boards.Flag) bool {\n\t\t// f.Reason is user-supplied (only trimmed at write); escape it so a\n\t\t// flag reason can't inject markdown (links/images) or HTML into the\n\t\t// reasons table. md.EscapeText leaves '|' for mdtable to escape.\n\t\ttable.Append([]string{userLink(f.User), md.EscapeText(f.Reason)})\n\t\treturn false\n\t})\n\n\t// Breadcrumb navigation\n\tbackLink := md.Link(\"← Back to thread\", makeThreadURI(thread))\n\n\tres.Write(md.H1(\"Flagging Reasons\"))\n\tres.Write(backLink + \"\\n\\n\")\n\tif isReply {\n\t\tres.Write(\n\t\t\tmd.Paragraph(\n\t\t\t\tufmt.Sprintf(\n\t\t\t\t\t\"Moderation flags for a %s submitted by %s\",\n\t\t\t\t\tmd.Link(\"comment\", makeReplyURI(reply)),\n\t\t\t\t\tuserLink(reply.Creator),\n\t\t\t\t),\n\t\t\t),\n\t\t)\n\t} else {\n\t\tres.Write(\n\t\t\tmd.Paragraph(\n\t\t\t\t// Intentionally hide flagged thread title\n\t\t\t\tufmt.Sprintf(\"Moderation flags for %s\", md.Link(\"thread\", makeThreadURI(thread))),\n\t\t\t),\n\t\t)\n\t}\n\tres.Write(table.String())\n}\n\nfunc renderReplyPost(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\t// Thread ID must always be available\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := board.Threads.Get(boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\t// Parse reply ID when post is a reply\n\tvar reply *boards.Post\n\trawID = req.GetVar(\"reply\")\n\tisReply := rawID != \"\"\n\tif isReply {\n\t\treplyID, err := strconv.Atoi(rawID)\n\t\tif err != nil {\n\t\t\tres.Write(\"Invalid reply ID: \" + md.EscapeText(rawID))\n\t\t\treturn\n\t\t}\n\n\t\treply, _ = getReply(thread, boards.ID(replyID))\n\t\tif reply == nil {\n\t\t\tres.Write(\"Reply not found\")\n\t\t\treturn\n\t\t}\n\t}\n\n\tform := mdform.New(\"exec\", \"CreateReply\")\n\tform.Input(\n\t\t\"boardID\",\n\t\t\"placeholder\", \"Board ID\",\n\t\t\"value\", board.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"threadID\",\n\t\t\"placeholder\", \"Thread ID\",\n\t\t\"value\", thread.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\n\tif isReply {\n\t\tform.Input(\n\t\t\t\"replyID\",\n\t\t\t\"placeholder\", \"Reply ID\",\n\t\t\t\"value\", reply.ID.String(),\n\t\t\t\"readonly\", \"true\",\n\t\t)\n\t} else {\n\t\tform.Input(\n\t\t\t\"replyID\",\n\t\t\t\"placeholder\", \"Reply ID\",\n\t\t\t\"value\", \"0\",\n\t\t\t\"readonly\", \"true\",\n\t\t)\n\t}\n\n\tform.Textarea(\n\t\t\"body\",\n\t\t\"placeholder\", \"Comment\",\n\t\t\"required\", \"true\",\n\t)\n\n\t// Breadcrumb navigation\n\tbackLink := md.Link(\"← Back to thread\", makeThreadURI(thread))\n\n\tif isReply {\n\t\tres.Write(md.H1(board.Name + \": Reply\"))\n\t\tres.Write(backLink + \"\\n\\n\")\n\t\tres.Write(\n\t\t\tmd.Paragraph(ufmt.Sprintf(\"Replying to a comment posted by %s:\", userLink(reply.Creator))) +\n\t\t\t\tforeign.ForeignWithLabel(\"Quoted comment\", reply.Body),\n\t\t)\n\t} else {\n\t\tres.Write(md.H1(board.Name + \": Comment\"))\n\t\tres.Write(backLink + \"\\n\\n\")\n\t\tres.Write(\n\t\t\tmd.Paragraph(\n\t\t\t\tufmt.Sprintf(\"Commenting on the thread: %s\", md.Link(thread.Title, makeThreadURI(thread))),\n\t\t\t),\n\t\t)\n\t}\n\n\tres.Write(form.String())\n\tres.Write(\"\\n\\n**Done?** \" + svgbtn.ButtonWithRadius(136, 32, 4, \"#E2E2E2\", \"#54595D\", \"Return to thread\", makeThreadURI(thread)) + \"\\n\")\n}\n"},{"name":"render_reply.gno","body":"package boards2\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/jeronimoalbi/mdform\"\n\t\"gno.land/p/jeronimoalbi/pager\"\n\t\"gno.land/p/leon/svgbtn\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc renderReply(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\trawID = req.GetVar(\"reply\")\n\treplyID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid reply ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := getThread(board, boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\treply, found := getReply(thread, boards.ID(replyID))\n\tif !found {\n\t\tres.Write(\"Reply not found\")\n\t\treturn\n\t}\n\n\t// Call render even for hidden replies to display children.\n\t// Original comment content will be hidden under the hood.\n\t// See: #3480\n\tres.Write(renderPostInner(reply, req.RawPath))\n}\n\n// newClampedPager builds a pager, clamping an out-of-range, zero, negative,\n// or malformed ?page= to the last valid page instead of erroring. pager.New\n// rejects page==0, page\u003epageCount, and non-numeric pages with\n// ErrInvalidPageNumber, which the callers would otherwise surface as a panic\n// (aborting the whole render) or an error page in place of the list. A\n// NEGATIVE page is not rejected by pager.New (it only checks ==0 and\n// \u003epageCount), so it must be caught here too — otherwise it renders a broken\n// \"page -N of M\" picker with both arrows disabled. Triggers on a stale deep\n// link after deletions, or a hand-edited URL.\nfunc newClampedPager(path string, size, pageSize int) pager.Pager {\n\tp, err := pager.New(path, size, pager.WithPageSize(pageSize))\n\tif err == nil \u0026\u0026 p.Page() \u003e= 1 {\n\t\treturn p\n\t}\n\t// Re-parse without ?page= to get a valid page-1 pager and read the real\n\t// page count, then jump to the last page when there is more than one.\n\tr := parseRealmPath(path)\n\tr.Query.Del(\"page\")\n\tfirst, ferr := pager.New(r.String(), size, pager.WithPageSize(pageSize))\n\tif ferr != nil {\n\t\t// A page-less path is valid for every current route, so this is\n\t\t// unreachable today; fall back to the original pager rather than\n\t\t// asserting the invariant with a panic that would abort the render.\n\t\treturn p\n\t}\n\tif last := first.PageCount(); last \u003e 1 {\n\t\tr.Query.Set(\"page\", strconv.Itoa(last))\n\t\tif clamped, e := pager.New(r.String(), size, pager.WithPageSize(pageSize)); e == nil {\n\t\t\treturn clamped\n\t\t}\n\t}\n\treturn first\n}\n\nfunc renderTopLevelReplies(post *boards.Post, path, indent string, levels int, budget *int) string {\n\tp := newClampedPager(path, post.Replies.Size(), pageSizeReplies)\n\tlink, desc := sortToggleLink(path)\n\n\tvar (\n\t\tb              strings.Builder\n\t\tcommentsIndent = indent + \"\u003e \"\n\t\ttruncated      bool\n\t)\n\n\trender := func(reply *boards.Post) bool {\n\t\tif *budget \u003c= 0 {\n\t\t\ttruncated = true\n\t\t\treturn true // stop: render budget exhausted (see maxRenderedBodies)\n\t\t}\n\t\t// Forward the page order so this reply's nested children match it.\n\t\tb.WriteString(indent + \"\\n\" + renderPost(reply, \"\", commentsIndent, levels-1, budget, desc))\n\t\treturn false\n\t}\n\n\tb.WriteString(\"\\n\" + md.HorizontalRule() + \"Sort by: \" + link + \"\\n\")\n\n\tcount := p.PageSize()\n\tif desc {\n\t\tcount = -count // Reverse iterate\n\t}\n\n\tpost.Replies.Iterate(p.Offset(), count, render)\n\n\tif truncated {\n\t\tb.WriteString(indent + \"\\n\" + commentsIndent + \"_Some replies not shown — \" +\n\t\t\tmd.Link(\"view all comments\", makeThreadFlatURI(post)) + \"._\\n\")\n\t}\n\n\t// Suppress the page picker when the budget truncated this page: later\n\t// replies in the page were skipped, so the offset-based \"next page\" would\n\t// jump past them. The flat link above is the complete, reachable view.\n\tif !truncated \u0026\u0026 p.HasPages() {\n\t\tb.WriteString(md.HorizontalRule())\n\t\tb.WriteString(pager.Picker(p))\n\t}\n\treturn b.String()\n}\n\nfunc renderSubReplies(post *boards.Post, indent string, levels int, budget *int, desc bool) string {\n\tvar (\n\t\tb              strings.Builder\n\t\tcommentsIndent = indent + \"\u003e \"\n\t\ttruncated      bool\n\t)\n\n\t// Cap inline children at pageSizeReplies. A nested reply list can't have\n\t// its own pager (it would collide with the page's ?page=), so instead of\n\t// dumping every child here a comment with more links to its own re-rooted\n\t// view, which paginates them. Keeps every view bounded to \u003c=pageSizeReplies\n\t// children per post. count's sign follows the page order so the inline\n\t// children match the view they appear in (desc → the newest ones).\n\tcount := pageSizeReplies\n\tif desc {\n\t\tcount = -count\n\t}\n\tpost.Replies.Iterate(0, count, func(reply *boards.Post) bool {\n\t\tif *budget \u003c= 0 {\n\t\t\ttruncated = true\n\t\t\treturn true // stop: render budget exhausted (see maxRenderedBodies)\n\t\t}\n\t\tb.WriteString(indent + \"\\n\" + renderPost(reply, \"\", commentsIndent, levels-1, budget, desc))\n\t\treturn false\n\t})\n\n\tnotice := func(text, uri string) {\n\t\tb.WriteString(indent + \"\\n\" + commentsIndent + md.Link(text, uri) + \"\\n\")\n\t}\n\tswitch {\n\tcase truncated:\n\t\t// Budget exhausted: the whole-thread flat view is the reachable backstop.\n\t\tnotice(\"More replies — view all comments\", makeThreadFlatURI(post))\n\tcase post.Replies.Size() \u003e pageSizeReplies:\n\t\t// Breadth cap hit: re-root at this comment to page the rest, carrying\n\t\t// the page's sort order so the re-root opens the same way (its first\n\t\t// page then matches the newest-first children shown inline here).\n\t\turi := makeReplyURI(post)\n\t\tif desc {\n\t\t\turi += \"?order=desc\"\n\t\t}\n\t\tnotice(\"View all \"+strconv.Itoa(post.Replies.Size())+\" replies\", uri)\n\t}\n\treturn b.String()\n}\n\nfunc renderEditReply(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\trawID = req.GetVar(\"reply\")\n\treplyID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid reply ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := getThread(board, boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\treply, found := getReply(thread, boards.ID(replyID))\n\tif !found {\n\t\tres.Write(\"Reply not found\")\n\t\treturn\n\t}\n\n\tform := mdform.New(\"exec\", \"EditReply\")\n\tform.Input(\n\t\t\"boardID\",\n\t\t\"placeholder\", \"Board ID\",\n\t\t\"value\", board.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"threadID\",\n\t\t\"placeholder\", \"Thread ID\",\n\t\t\"value\", thread.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"replyID\",\n\t\t\"placeholder\", \"Reply ID\",\n\t\t\"value\", reply.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Textarea(\n\t\t\"body\",\n\t\t\"placeholder\", \"Comment\",\n\t\t\"value\", reply.Body,\n\t\t\"required\", \"true\",\n\t)\n\n\tres.Write(md.H1(board.Name + \": Edit Comment\"))\n\tres.Write(md.Link(\"← Back to thread\", makeThreadURI(thread)) + \"\\n\\n\")\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\tufmt.Sprintf(\"Editing a comment from the thread: %s\", md.Link(thread.Title, makeThreadURI(thread))),\n\t\t),\n\t)\n\tres.Write(form.String())\n\tres.Write(\"\\n\\n**Done?** \" + svgbtn.ButtonWithRadius(136, 32, 4, \"#E2E2E2\", \"#54595D\", \"Return to thread\", makeThreadURI(thread)) + \"\\n\")\n}\n"},{"name":"render_thread.gno","body":"package boards2\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/jeronimoalbi/mdform\"\n\t\"gno.land/p/jeronimoalbi/pager\"\n\t\"gno.land/p/leon/svgbtn\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// maxFlatIndentDepth caps the blockquote nesting in the flat comment view so\n// deep reply chains stay readable; comments deeper than this still render,\n// just at the capped indent.\nconst maxFlatIndentDepth = 6\n\nfunc renderThread(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := getThread(board, boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\tif thread.Hidden {\n\t\tlink := md.Link(\"inappropriate\", makeFlaggingReasonsURI(thread))\n\t\tres.Write(\"⚠ Thread has been flagged as \" + link)\n\t\treturn\n\t}\n\n\tres.Write(md.H1(md.Link(\"Boards\", gRealmPath) + \" › \" + md.Link(board.Name, makeBoardURI(board))))\n\tbudget := maxRenderedBodies()\n\tif parseRealmPath(req.RawPath).Query.Get(\"flat\") != \"\" {\n\t\tres.Write(renderThreadFlat(thread, req.RawPath, \u0026budget))\n\t\treturn\n\t}\n\tres.Write(renderPost(thread, req.RawPath, \"\", 5, \u0026budget, false))\n}\n\n// renderThreadFlat renders every comment in the thread as a single flat,\n// depth-indented, paginated list backed by ThreadMeta.AllReplies (the index\n// that already holds every reply at every depth). Unlike the recursive\n// threaded view — which bounds work with the render budget and truncates a\n// large subtree — this view is reachable to the very last comment: each\n// comment renders one \u003cgno-foreign\u003e block (no recursion), so a fixed page\n// size (pageSizeFlat) plus the OP stays well under the budget regardless of\n// nesting, and the pager always advances. ?order=desc shows newest first, so\n// its page 1 is the latest comments.\nfunc renderThreadFlat(thread *boards.Post, path string, budget *int) string {\n\tvar b strings.Builder\n\n\t// The OP for context (its body only; no replies — levels 0).\n\tb.WriteString(renderPost(thread, \"\", \"\", 0, budget, false))\n\n\tmeta, ok := thread.Meta.(*ThreadMeta)\n\tif !ok || meta.AllReplies.Size() == 0 {\n\t\treturn b.String()\n\t}\n\tall := meta.AllReplies\n\tp := newClampedPager(path, all.Size(), pageSizeFlat)\n\n\tb.WriteString(\"\\n\" + md.HorizontalRule())\n\tb.WriteString(md.Link(\"← Threaded view\", makeThreadURI(thread)) + \" · All \" +\n\t\tstrconv.Itoa(all.Size()) + \" comments — sort by: \")\n\n\t// sortToggleLink preserves flat=1, so the toggle stays in the flat view.\n\tlink, desc := sortToggleLink(path)\n\tb.WriteString(link + \"\\n\")\n\n\tcount := p.PageSize()\n\tif desc {\n\t\tcount = -count // reverse iterate: newest first\n\t}\n\tall.Iterate(p.Offset(), count, func(reply *boards.Post) bool {\n\t\tif *budget \u003c= 0 {\n\t\t\t// Unreachable while pageSizeFlat \u003c\u003c maxRenderedBodies; a backstop\n\t\t\t// in case a chain upgrade drops the native cap below one page.\n\t\t\treturn true\n\t\t}\n\t\tindent := flatIndent(thread, reply)\n\t\tb.WriteString(indent + \"\\n\" + renderPost(reply, \"\", indent, 0, budget, false))\n\t\treturn false\n\t})\n\n\tif p.HasPages() {\n\t\tb.WriteString(md.HorizontalRule())\n\t\tb.WriteString(pager.Picker(p))\n\t}\n\treturn b.String()\n}\n\n// flatIndent returns the blockquote indent for a reply in the flat view,\n// derived from its depth below the thread root (depth 1 = a direct reply to\n// the thread), capped at maxFlatIndentDepth.\nfunc flatIndent(thread *boards.Post, reply *boards.Post) string {\n\tdepth := 1\n\tpid := reply.ParentID\n\tfor pid != thread.ID \u0026\u0026 pid != 0 \u0026\u0026 depth \u003c maxFlatIndentDepth {\n\t\tparent, ok := getReply(thread, pid)\n\t\tif !ok {\n\t\t\tbreak\n\t\t}\n\t\tdepth++\n\t\tpid = parent.ParentID\n\t}\n\treturn strings.Repeat(\"\u003e \", depth)\n}\n\nfunc renderThreadSummary(thread *boards.Post) string {\n\tvar (\n\t\tb           strings.Builder\n\t\tpostURI     = makeThreadURI(thread)\n\t\tsummary     = summaryOf(thread.Title, 80)\n\t\tcreatorLink = userLink(thread.Creator)\n\t\troleBadge   = getRoleBadge(thread)\n\t\tdate        = thread.CreatedAt.Format(dateFormat)\n\t)\n\n\tif boards.IsRepost(thread) {\n\t\tsummary += ` ⟳`\n\t\tpostURI += ` \"This is a thread repost\"`\n\t}\n\n\tb.WriteString(md.H6(md.Link(summary, postURI)))\n\tb.WriteString(\"Created by \" + creatorLink + roleBadge + \" on \" + date + \"  \\n\")\n\n\tstatus := []string{\n\t\tstrconv.Itoa(thread.Replies.Size()) + \" replies\",\n\t\tstrconv.Itoa(thread.Reposts.Size()) + \" reposts\",\n\t}\n\tb.WriteString(md.Bold(strings.Join(status, \" • \")) + \"\\n\")\n\treturn b.String()\n}\n\nfunc renderCreateThread(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\tform := mdform.New(\"exec\", \"CreateThread\")\n\tform.Input(\n\t\t\"boardID\",\n\t\t\"placeholder\", \"Board ID\",\n\t\t\"value\", board.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"title\",\n\t\t\"placeholder\", \"Title\",\n\t\t\"required\", \"true\",\n\t)\n\tform.Textarea(\n\t\t\"body\",\n\t\t\"placeholder\", \"Content\",\n\t\t\"rows\", \"10\",\n\t\t\"required\", \"true\",\n\t)\n\n\tres.Write(md.H1(board.Name + \": Create Thread\"))\n\tres.Write(md.Link(\"← Back to board\", makeBoardURI(board)) + \"\\n\\n\")\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\tufmt.Sprintf(\"Thread will be created in the board: %s\", md.Link(board.Name, makeBoardURI(board))),\n\t\t),\n\t)\n\tres.Write(form.String())\n\tres.Write(\"\\n\\n**Done?** \" + svgbtn.ButtonWithRadius(136, 32, 4, \"#E2E2E2\", \"#54595D\", \"Return to board\", makeBoardURI(board)) + \"\\n\")\n}\n\nfunc renderEditThread(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := getThread(board, boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\tform := mdform.New(\"exec\", \"EditThread\")\n\tform.Input(\n\t\t\"boardID\",\n\t\t\"placeholder\", \"Board ID\",\n\t\t\"value\", board.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"threadID\",\n\t\t\"placeholder\", \"Thread ID\",\n\t\t\"value\", thread.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"title\",\n\t\t\"placeholder\", \"Title\",\n\t\t\"value\", thread.Title,\n\t\t\"required\", \"true\",\n\t)\n\tform.Textarea(\n\t\t\"body\",\n\t\t\"placeholder\", \"Content\",\n\t\t\"rows\", \"10\",\n\t\t\"value\", thread.Body,\n\t\t\"required\", \"true\",\n\t)\n\n\tres.Write(md.H1(board.Name + \": Edit Thread\"))\n\tres.Write(md.Link(\"← Back to thread\", makeThreadURI(thread)) + \"\\n\\n\")\n\tres.Write(\n\t\tmd.Paragraph(\"Editing \" + md.Link(thread.Title, makeThreadURI(thread))),\n\t)\n\tres.Write(form.String())\n\tres.Write(\"\\n\\n**Done?** \" + svgbtn.ButtonWithRadius(136, 32, 4, \"#E2E2E2\", \"#54595D\", \"Return to thread\", makeThreadURI(thread)) + \"\\n\")\n}\n\nfunc renderRepostThread(res *mux.ResponseWriter, req *mux.Request) {\n\tname := req.GetVar(\"board\")\n\tboard, found := gBoards.GetByName(name)\n\tif !found {\n\t\tres.Write(\"Board not found\")\n\t\treturn\n\t}\n\n\trawID := req.GetVar(\"thread\")\n\tthreadID, err := strconv.Atoi(rawID)\n\tif err != nil {\n\t\tres.Write(\"Invalid thread ID: \" + md.EscapeText(rawID))\n\t\treturn\n\t}\n\n\tthread, found := getThread(board, boards.ID(threadID))\n\tif !found {\n\t\tres.Write(\"Thread not found\")\n\t\treturn\n\t}\n\n\tform := mdform.New(\"exec\", \"CreateRepost\")\n\tform.Input(\n\t\t\"boardID\",\n\t\t\"placeholder\", \"Board ID\",\n\t\t\"value\", board.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"threadID\",\n\t\t\"placeholder\", \"Thread ID\",\n\t\t\"value\", thread.ID.String(),\n\t\t\"readonly\", \"true\",\n\t)\n\tform.Input(\n\t\t\"destinationBoardID\",\n\t\t\"type\", mdform.InputTypeNumber,\n\t\t\"placeholder\", \"Board ID where to repost\",\n\t\t\"required\", \"true\",\n\t)\n\tform.Input(\n\t\t\"title\",\n\t\t\"value\", thread.Title,\n\t\t\"placeholder\", \"Title\",\n\t\t\"required\", \"true\",\n\t)\n\tform.Textarea(\n\t\t\"body\",\n\t\t\"placeholder\", \"Content\",\n\t\t\"rows\", \"10\",\n\t)\n\n\tres.Write(md.H1(board.Name + \": Repost Thread\"))\n\tres.Write(md.Link(\"← Back to thread\", makeThreadURI(thread)) + \"\\n\\n\")\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\t\"Threads can be reposted to other open boards or boards where you are a member \" +\n\t\t\t\t\"and are allowed to create new threads.\",\n\t\t),\n\t)\n\tres.Write(\n\t\tmd.Paragraph(\n\t\t\tufmt.Sprintf(\"Reposting the thread: %s.\", md.Link(thread.Title, makeThreadURI(thread))),\n\t\t),\n\t)\n\tres.Write(form.String())\n\tres.Write(\"\\n\\n**Done?** \" + svgbtn.ButtonWithRadius(136, 32, 4, \"#E2E2E2\", \"#54595D\", \"Return to thread\", makeThreadURI(thread)) + \"\\n\")\n}\n"},{"name":"uris_board.gno","body":"package boards2\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc makeBoardURI(b *boards.Board) string {\n\tpath := strings.TrimPrefix(string(RealmLink), \"gno.land\")\n\treturn path + \":\" + url.PathEscape(b.Name)\n}\n\nfunc makeFreezeBoardURI(b *boards.Board) string {\n\treturn RealmLink.Call(\n\t\t\"FreezeBoard\",\n\t\t\"boardID\", b.ID.String(),\n\t)\n}\n\nfunc makeUnfreezeBoardURI(b *boards.Board) string {\n\treturn RealmLink.Call(\n\t\t\"UnfreezeBoard\",\n\t\t\"boardID\", b.ID.String(),\n\t\t\"threadID\", \"\",\n\t\t\"replyID\", \"\",\n\t)\n}\n\nfunc makeInviteMemberURI(b *boards.Board) string {\n\treturn makeBoardURI(b) + \"/invite-member\"\n}\n\nfunc makeCreateThreadURI(b *boards.Board) string {\n\treturn makeBoardURI(b) + \"/create-thread\"\n}\n\nfunc makeRequestInviteURI(b *boards.Board) string {\n\treturn RealmLink.Call(\n\t\t\"RequestInvite\",\n\t\t\"boardID\", b.ID.String(),\n\t)\n}\n"},{"name":"uris_post.gno","body":"package boards2\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n)\n\nfunc makeThreadURI(p *boards.Post) string {\n\tif boards.IsThread(p) {\n\t\treturn makeBoardURI(p.Board) + \"/\" + p.ID.String()\n\t}\n\n\t// When post is a reply use the parent thread ID\n\treturn makeBoardURI(p.Board) + \"/\" + p.ThreadID.String()\n}\n\n// makeThreadFlatURI links to the thread's flat \"all comments\" view. Works for\n// a thread or any reply within it (makeThreadURI resolves to the thread).\nfunc makeThreadFlatURI(p *boards.Post) string {\n\treturn makeThreadURI(p) + \"?flat=1\"\n}\n\nfunc makeReplyURI(p *boards.Post) string {\n\treturn makeBoardURI(p.Board) + \"/\" + p.ThreadID.String() + \"/\" + p.ID.String()\n}\n\nfunc makeCreateReplyURI(p *boards.Post) string {\n\tif boards.IsThread(p) {\n\t\treturn makeThreadURI(p) + \"/reply\"\n\t}\n\treturn makeReplyURI(p) + \"/reply\"\n}\n\nfunc makeCreateRepostURI(p *boards.Post) string {\n\treturn makeThreadURI(p) + \"/repost\"\n}\n\nfunc makeDeletePostURI(p *boards.Post) string {\n\tif boards.IsThread(p) {\n\t\treturn RealmLink.Call(\n\t\t\t\"DeleteThread\",\n\t\t\t\"boardID\", p.Board.ID.String(),\n\t\t\t\"threadID\", p.ThreadID.String(),\n\t\t)\n\t}\n\treturn RealmLink.Call(\n\t\t\"DeleteReply\",\n\t\t\"boardID\", p.Board.ID.String(),\n\t\t\"threadID\", p.ThreadID.String(),\n\t\t\"replyID\", p.ID.String(),\n\t)\n}\n\nfunc makeEditPostURI(p *boards.Post) string {\n\tif boards.IsThread(p) {\n\t\treturn makeThreadURI(p) + \"/edit\"\n\t}\n\treturn makeReplyURI(p) + \"/edit\"\n}\n\nfunc makeFlagURI(p *boards.Post) string {\n\tif boards.IsThread(p) {\n\t\treturn makeThreadURI(p) + \"/flag\"\n\t}\n\treturn makeReplyURI(p) + \"/flag\"\n}\n\nfunc makeFlaggingReasonsURI(p *boards.Post) string {\n\tif boards.IsThread(p) {\n\t\treturn makeThreadURI(p) + \"/flagging-reasons\"\n\t}\n\treturn makeReplyURI(p) + \"/flagging-reasons\"\n}\n"},{"name":"z_accept_invite_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_accept_invite_00_filetest\n\npackage z_accept_invite_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.AcceptInvite(cross(cur), bid, user)\n\n\tprintln(boards2.IsMember(bid, user))\n\tprintln()\n\tprintln(boards2.Render(\"test123/invites\"))\n}\n\n// Output:\n// true\n//\n// # test123 Invite Requests\n// ### Board has no invite requests\n"},{"name":"z_accept_invite_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_accept_invite_01_filetest\n\npackage z_accept_invite_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\t// Request an invite as a user that is not a member\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tboards2.RequestInvite(cross(cur), bid)\n\n\t// Add user as a member idependently of the invite request\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.InviteMember(cross(cur), bid, user, boards2.RoleGuest)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.AcceptInvite(cross(cur), bid, user)\n}\n\n// Error:\n// user is already a member\n"},{"name":"z_accept_invite_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_accept_invite_02_filetest\n\npackage z_accept_invite_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.AcceptInvite(cross(cur), bid, user)\n}\n\n// Error:\n// invite request not found\n"},{"name":"z_accept_invite_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_accept_invite_03_filetest\n\npackage z_accept_invite_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\t// Caller is not a member and has no permission to invite\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.AcceptInvite(cross(cur), bid, user)\n}\n\n// Error:\n// unauthorized, user g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5 doesn't have the required permission\n"},{"name":"z_ban_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ban_00_filetest\n\npackage z_ban_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.Ban(cross(cur), bid, user, boards2.BanDay, \"Unpolite behavior\")\n\n\tprintln(boards2.IsBanned(bid, user))\n}\n\n// Output:\n// true\n"},{"name":"z_ban_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ban_01_filetest\n\npackage z_ban_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\t// Invite the user as a moderator\n\tboards2.InviteMember(cross(cur), bid, user, boards2.RoleModerator)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.Ban(cross(cur), bid, user, boards2.BanDay, \"Reason\")\n}\n\n// Error:\n// owner, admin and moderator banning is not allowed\n"},{"name":"z_ban_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ban_02_filetest\n\npackage z_ban_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Try to ban a user without banning permissions\n\tboards2.Ban(cross(cur), bid, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", boards2.BanDay, \"Reason\")\n}\n\n// Error:\n// unauthorized, user g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5 doesn't have the required permission\n"},{"name":"z_change_member_role_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_00_filetest\n\npackage z_change_member_role_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tnewRole         = boards2.RoleOwner\n\tbid             = boards.ID(0) // Operate on realm DAO instead of individual boards\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.ChangeMemberRole(cross(cur), bid, member, newRole)\n\n\t// Ensure that new role has been changed\n\tprintln(boards2.HasMemberRole(bid, member, newRole))\n}\n\n// Output:\n// true\n"},{"name":"z_change_member_role_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_01_filetest\n\npackage z_change_member_role_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tnewRole         = boards2.RoleAdmin\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"foo123\", false, false)\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleGuest)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.ChangeMemberRole(cross(cur), bid, member, newRole)\n\n\t// Ensure that new role has been changed\n\tprintln(boards2.HasMemberRole(bid, member, newRole))\n}\n\n// Output:\n// true\n"},{"name":"z_change_member_role_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_02_filetest\n\npackage z_change_member_role_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner  address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\towner2 address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tadmin  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\tboards2.InviteMember(cross(cur), bid, owner2, boards2.RoleOwner)\n\tboards2.InviteMember(cross(cur), bid, admin, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\n\tboards2.ChangeMemberRole(cross(cur), bid, owner2, boards2.RoleAdmin)\n}\n\n// Error:\n// admins are not allowed to remove the Owner role\n"},{"name":"z_change_member_role_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_03_filetest\n\npackage z_change_member_role_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner  address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tadmin2 address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\tboards2.InviteMember(cross(cur), bid, admin, boards2.RoleAdmin)\n\tboards2.InviteMember(cross(cur), bid, admin2, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\n\tboards2.ChangeMemberRole(cross(cur), bid, admin2, boards2.RoleOwner)\n}\n\n// Error:\n// admins are not allowed to promote members to Owner\n"},{"name":"z_change_member_role_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_04_filetest\n\npackage z_change_member_role_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tbid             = boards.ID(0)                               // Operate on realm DAO members instead of individual boards\n\tnewRole         = boards2.RoleOwner\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.ChangeMemberRole(cross(cur), bid, member, newRole) // Owner can promote other members to Owner\n\n\t// Ensure that new role has been changed to owner\n\tprintln(boards2.HasMemberRole(bid, member, newRole))\n}\n\n// Output:\n// true\n"},{"name":"z_change_member_role_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_05_filetest\n\npackage z_change_member_role_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\tboards2.InviteMember(cross(cur), bid, admin, boards2.RoleGuest)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.ChangeMemberRole(cross(cur), bid, admin, boards.Role(\"foo\"))\n}\n\n// Error:\n// invalid role: foo\n"},{"name":"z_change_member_role_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_06_filetest\n\npackage z_change_member_role_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\tboards2.InviteMember(cross(cur), bid, admin, boards2.RoleGuest)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.ChangeMemberRole(cross(cur), bid, \"foo\", boards2.RoleModerator)\n}\n\n// Error:\n// invalid member address: foo\n"},{"name":"z_change_member_role_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_change_member_role_07_filetest\n\npackage z_change_member_role_07_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.ChangeMemberRole(cross(cur), 0, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", boards2.RoleGuest)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_create_board_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_00_filetest\n\npackage z_create_board_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tbid := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tprintln(\"ID =\", bid)\n}\n\n// Output:\n// ID = 1\n"},{"name":"z_create_board_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_01_filetest\n\npackage z_create_board_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), \"\", false, false)\n}\n\n// Error:\n// board name is empty\n"},{"name":"z_create_board_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_02_filetest\n\npackage z_create_board_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test123\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), boardName, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), boardName, false, false)\n}\n\n// Error:\n// board already exists\n"},{"name":"z_create_board_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_03_filetest\n\npackage z_create_board_03_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", false, false)\n}\n\n// Error:\n// addresses are not allowed as board name\n"},{"name":"z_create_board_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_04_filetest\n\npackage z_create_board_04_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\t// uinit.RegisterUser is genesis-only since the security fix.\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnoland\", address(\"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"))\n\ttesting.SetHeight(123)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), \"gnoland\", false, false)\n}\n\n// Error:\n// board name is a user name registered to a different user\n"},{"name":"z_create_board_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_05_filetest\n\npackage z_create_board_05_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar name = strings.Repeat(\"X\", boards2.MaxBoardNameLength+1)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), name, false, false)\n}\n\n// Error:\n// board name is too long, maximum allowed is 50 characters\n"},{"name":"z_create_board_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_06_filetest\n\npackage z_create_board_06_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner  address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tname           = \"test123\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Operate on realm DAO members instead of individual boards\n\tboards2.InviteMember(cross(cur), 0, member, boards2.RoleOwner)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(member))\n\n\t// Create a board as an invited realm member\n\tbid := boards2.CreateBoard(cross(cur), name, false, false)\n\tprintln(\"ID =\", bid)\n}\n\n// Output:\n// ID = 1\n"},{"name":"z_create_board_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_07_filetest\n\npackage z_create_board_07_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_create_board_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_08_filetest\n\npackage z_create_board_08_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tname          = \"TestBoard\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), name, false, false)\n\n\t// Unlisted board should not be rendered\n\tprintln(boards2.Render(\"\"))\n\n\t// Unlisted board can be rendered by path\n\tprintln(\"\\n==================\")\n\tprintln(boards2.Render(name))\n}\n\n// Output:\n// # Boards\n// [Create Board](/r/gnoland/boards2/v1:create-board) • [List Admin Users](/r/gnoland/boards2/v1:admin-users) • [Help](/r/gnoland/boards2/v1:help)\n//\n// ---\n// ### Currently there are no boards\n// Be the first to [create a new board](/r/gnoland/boards2/v1:create-board)!\n//\n// ==================\n// # [Boards](/r/gnoland/boards2/v1) › TestBoard\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// ↳ [Create Thread](/r/gnoland/boards2/v1:TestBoard/create-thread) • [Request Invite](/r/gnoland/boards2/v1$help\u0026func=RequestInvite\u0026boardID=1) • [Manage Board](?menu=manageBoard)\n//\n// ---\n// ### This board doesn't have any threads\n// Do you want to [start a new conversation](/r/gnoland/boards2/v1:TestBoard/create-thread) in this board?\n"},{"name":"z_create_board_09_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_09_filetest\n\npackage z_create_board_09_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), \"TEST123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Should fail because board name already exists with a different casing\n\tboards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\n// Error:\n// board already exists\n"},{"name":"z_create_board_10_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_board_10_filetest\n\npackage z_create_board_10_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Should fail because board name has a space which is not allowed\n\tboards2.CreateBoard(cross(cur), \"test 123\", false, false)\n}\n\n// Error:\n// board name must start with a letter and have letters, numbers, \"-\" and \"_\"\n"},{"name":"z_create_reply_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_00_filetest\n\npackage z_create_reply_00_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tpath            = \"test-board/1/2\"\n\tcomment         = \"Test comment\"\n)\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\trid := boards2.CreateReply(cross(cur), bid, tid, 0, comment)\n\n\t// Ensure that returned ID is right\n\tprintln(rid == 2)\n\n\t// Render content must contain the reply\n\tcontent := boards2.Render(path)\n\tprintln(strings.Contains(content, \"\\n\u003e \"+comment))\n}\n\n// Output:\n// true\n// true\n"},{"name":"z_create_reply_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_01_filetest\n\npackage z_create_reply_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), 404, 1, 0, \"comment\")\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_create_reply_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_02_filetest\n\npackage z_create_reply_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, 404, 0, \"comment\")\n}\n\n// Error:\n// thread does not exist with ID: 404\n"},{"name":"z_create_reply_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_03_filetest\n\npackage z_create_reply_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, tid, 404, \"comment\")\n}\n\n// Error:\n// reply does not exist with ID: 404\n"},{"name":"z_create_reply_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_04_filetest\n\npackage z_create_reply_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\n\t// Hide thread by flagging it so reply can't be submitted\n\tboards2.FlagThread(cross(cur), bid, tid, \"reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"Test reply\")\n}\n\n// Error:\n// thread is hidden\n"},{"name":"z_create_reply_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_05_filetest\n\npackage z_create_reply_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"reply1\")\n\n\t// Hide thread by flagging it so reply of a reply can't be submitted\n\tboards2.FlagThread(cross(cur), bid, tid, \"reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, tid, rid, \"reply1.1\")\n}\n\n// Error:\n// thread is hidden\n"},{"name":"z_create_reply_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_06_filetest\n\npackage z_create_reply_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"thread\", \"thread\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"reply1\")\n\n\t// Hide reply by flagging it so sub reply can't be submitted\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, tid, rid, \"reply1.1\")\n}\n\n// Error:\n// replying to a hidden or frozen reply is not allowed\n"},{"name":"z_create_reply_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_07_filetest\n\npackage z_create_reply_07_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"Test reply\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_create_reply_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_08_filetest\n\npackage z_create_reply_08_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"\")\n}\n\n// Error:\n// body is empty\n"},{"name":"z_create_reply_09_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_09_filetest\n\npackage z_create_reply_09_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tpath            = \"test-board/1/2\"\n\tcomment         = \"Second comment\"\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"First comment\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\trid2 := boards2.CreateReply(cross(cur), bid, tid, rid, comment)\n\n\t// Ensure that returned ID is right\n\tprintln(rid2 == 3)\n\n\t// Render content must contain the sub-reply\n\tcontent := boards2.Render(path)\n\tprintln(strings.Contains(content, \"\\n\u003e \u003e \"+comment))\n}\n\n// Output:\n// true\n// true\n"},{"name":"z_create_reply_10_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_10_filetest\n\npackage z_create_reply_10_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tcomment         = \"Second comment\"\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"Parent comment\")\n\n\t// Flag parent post so it's hidden\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"Reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, tid, rid, \"Sub comment\")\n}\n\n// Error:\n// replying to a hidden or frozen reply is not allowed\n"},{"name":"z_create_reply_11_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_11_filetest\n\npackage z_create_reply_11_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"foo\", \"bar\")\n\tboards2.FreezeThread(cross(cur), bid, tid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// cannot reply to a frozen thread\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"foobar\")\n}\n\n// Error:\n// thread is frozen\n"},{"name":"z_create_reply_12_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_12_filetest\n\npackage z_create_reply_12_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateReply(cross(cur), bid, tid, 0, strings.Repeat(\"x\", boards2.MaxReplyLength+1))\n}\n\n// Error:\n// reply is too long, maximum allowed is 10000 characters\n"},{"name":"z_create_reply_13_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_13_filetest\n\n// A reply may now contain Markdown block structure (here a blockquote).\n// The write-time blacklist used to reject it; now the reply body renders\n// inside a \u003cgno-foreign\u003e sandbox, so its structure is contained and\n// cannot hijack realm chrome.\npackage z_create_reply_13_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Previously rejected (\"headings, blockquotes ... not allowed\").\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"\u003e Markdown blockquote\")\n\n\t// The reply body is wrapped in a gno-foreign sandbox at render time.\n\tcontent := boards2.Render(\"test-board/1\")\n\tprintln(strings.Contains(content, \"Markdown blockquote\") \u0026\u0026 strings.Contains(content, \"\u003cgno-foreign\u003e\"))\n}\n\n// Output:\n// true\n"},{"name":"z_create_reply_14_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_14_filetest\n\n// Open board: Test creating a new reply as a non member user\npackage z_create_reply_14_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser    address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tcomment         = \"Test Comment\"\n)\n\nvar (\n\tbid boards.ID // Operate on board DAO\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Title\", \"Body\")\n\n\t// Make sure user account has the required amount of GNOT for open board actions\n\ttesting.IssueCoins(user, chain.Coins{{\"ugnot\", 3_000_000_000}})\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Non members should be able to add replies\n\tboards2.CreateReply(cross(cur), bid, tid, 0, comment)\n\n\t// Render content must contain the reply\n\tcontent := boards2.Render(\"test123/1\")\n\tprintln(strings.Contains(content, \"\\n\u003e \"+comment))\n}\n\n// Output:\n// true\n"},{"name":"z_create_reply_15_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_15_filetest\n\n// Open board: Test creating a new reply as a non member user that has no GNOT\npackage z_create_reply_15_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser    address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tcomment         = \"Test Comment\"\n)\n\nvar (\n\tbid boards.ID // Operate on board DAO\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Title\", \"Body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Non members should be able to add replies only if they have enough GNOT\n\tboards2.CreateReply(cross(cur), bid, tid, 0, comment)\n}\n\n// Error:\n// caller is not allowed to comment: account amount is lower than 3000 GNOT\n"},{"name":"z_create_reply_16_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_reply_16_filetest\n\n// A reply may now contain a \u003cgno-form\u003e. The write-time blacklist used to\n// reject it; now the reply body renders inside a \u003cgno-foreign\u003e sandbox\n// whose inner renderer does not load forms (gnoweb safe-mode omits the\n// raw HTML at render).\npackage z_create_reply_16_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid, tid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Previously rejected (\"forms are not allowed in replies\").\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"\u003cgno-form\u003e\u003cgno-select name=\\\"foo\\\" value=\\\"\\\" /\u003e\u003c/gno-form\u003e\")\n\n\t// Accepted; the body is wrapped in a gno-foreign sandbox.\n\tcontent := boards2.Render(\"test-board/1\")\n\tprintln(strings.Contains(content, \"\u003cgno-foreign\u003e\"))\n}\n\n// Output:\n// true\n"},{"name":"z_create_repost_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_repost_00_filetest\n\npackage z_create_repost_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tsrcBID boards.ID\n\tdstBID boards.ID\n\tsrcTID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tsrcBID = boards2.CreateBoard(cross(cur), \"src-board\", false, false)\n\tdstBID = boards2.CreateBoard(cross(cur), \"dst-board\", false, false)\n\n\tsrcTID = boards2.CreateThread(cross(cur), srcBID, \"Foo\", \"bar\")\n\tboards2.FlagThread(cross(cur), srcBID, srcTID, \"idk\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Repost should fail if source thread is flagged\n\tboards2.CreateRepost(cross(cur), srcBID, srcTID, dstBID, \"foo\", \"bar\")\n}\n\n// Error:\n// thread is hidden\n"},{"name":"z_create_repost_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_repost_01_filetest\n\npackage z_create_repost_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tsrcBID boards.ID\n\tdstBID boards.ID\n\tsrcTID boards.ID = 1024\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tsrcBID = boards2.CreateBoard(cross(cur), \"src-board\", false, false)\n\tdstBID = boards2.CreateBoard(cross(cur), \"dst-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Repost should fail if source thread doesn't exist\n\tboards2.CreateRepost(cross(cur), srcBID, srcTID, dstBID, \"foo\", \"bar\")\n}\n\n// Error:\n// thread does not exist with ID: 1024\n"},{"name":"z_create_repost_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_repost_02_filetest\n\npackage z_create_repost_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tsrcBID boards.ID\n\tdstBID boards.ID\n\tsrcTID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tsrcBID = boards2.CreateBoard(cross(cur), \"src-board\", false, false)\n\tdstBID = boards2.CreateBoard(cross(cur), \"dst-board\", false, false)\n\n\tsrcTID = boards2.CreateThread(cross(cur), srcBID, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.CreateRepost(cross(cur), srcBID, srcTID, dstBID, \"foo\", \"bar\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_create_repost_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_repost_03_filetest\n\npackage z_create_repost_03_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tsrcBID boards.ID\n\tdstBID boards.ID\n\tsrcTID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tsrcBID = boards2.CreateBoard(cross(cur), \"src-board\", false, false)\n\tdstBID = boards2.CreateBoard(cross(cur), \"dst-board\", false, false)\n\n\tsrcTID = boards2.CreateThread(cross(cur), srcBID, \"original title\", \"original text\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Success case\n\ttID := boards2.CreateRepost(cross(cur), srcBID, srcTID, dstBID, \"repost title\", \"repost text\")\n\tp := ufmt.Sprintf(\"dst-board/%s\", tID)\n\tout := boards2.Render(p)\n\n\tprintln(strings.Contains(out, \"original text\"))\n\tprintln(strings.Contains(out, \"repost title\"))\n\tprintln(strings.Contains(out, \"repost text\"))\n}\n\n// Output:\n// true\n// true\n// true\n"},{"name":"z_create_repost_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_repost_04_filetest\n\npackage z_create_repost_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tsrcBID boards.ID\n\tdstBID boards.ID\n\tsrcTID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board with a thread\n\torigBID := boards2.CreateBoard(cross(cur), \"origin-board\", false, false)\n\torigTID := boards2.CreateThread(cross(cur), origBID, \"title\", \"text\")\n\n\t// Create a second board and repost a thread using an empty title\n\tsrcBID = boards2.CreateBoard(cross(cur), \"source-board\", false, false)\n\tsrcTID = boards2.CreateRepost(cross(cur), origBID, origTID, srcBID, \"original title\", \"original text\")\n\n\t// Create a third board to try reposting the repost\n\tdstBID = boards2.CreateBoard(cross(cur), \"destination-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateRepost(cross(cur), srcBID, srcTID, dstBID, \"repost title\", \"repost text\")\n}\n\n// Error:\n// reposting a thread that is a repost is not allowed\n"},{"name":"z_create_thread_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_thread_00_filetest\n\npackage z_create_thread_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\ttitle         = \"Test Thread\"\n\tbody          = \"Test body\"\n\tpath          = \"test-board/1\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\ttid := boards2.CreateThread(cross(cur), bid, title, body)\n\n\t// Ensure that returned ID is right\n\tprintln(tid == 1)\n\n\t// Thread should not be frozen by default\n\tprintln(boards2.IsThreadFrozen(bid, tid))\n\n\t// Render content must contains thread's title and body\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// true\n// false\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## Test Thread\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Test body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n"},{"name":"z_create_thread_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_thread_01_filetest\n\npackage z_create_thread_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateThread(cross(cur), 404, \"Foo\", \"bar\")\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_create_thread_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_thread_02_filetest\n\npackage z_create_thread_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_create_thread_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_thread_03_filetest\n\npackage z_create_thread_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateThread(cross(cur), bid, \"\", \"bar\")\n}\n\n// Error:\n// title is empty\n"},{"name":"z_create_thread_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_thread_04_filetest\n\npackage z_create_thread_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateThread(cross(cur), bid, \"Foo\", \"\")\n}\n\n// Error:\n// thread body is required\n"},{"name":"z_create_thread_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_thread_05_filetest\n\n// Open board: Test creating a new thread as a non member user\npackage z_create_thread_05_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\ttitle         = \"Test Thread\"\n\tbody          = \"Test body\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n\n\t// Make sure user account has the required amount of GNOT for open board actions\n\ttesting.IssueCoins(user, chain.Coins{{\"ugnot\", 3_000_000_000}})\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Non members should be able to create threads\n\ttid := boards2.CreateThread(cross(cur), bid, title, body)\n\n\t// Ensure that returned ID is right\n\tprintln(tid == 1)\n\n\t// Render content must contains thread's title and body\n\tprintln(boards2.Render(\"test123/1\"))\n}\n\n// Output:\n// true\n// # [Boards](/r/gnoland/boards2/v1) › [test123](/r/gnoland/boards2/v1:test123)\n// ## Test Thread\n//\n// **[g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj](/u/g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj)** · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Test body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test123/1/flag) • [Repost](/r/gnoland/boards2/v1:test123/1/repost) • [Comment](/r/gnoland/boards2/v1:test123/1/reply) • [Edit](/r/gnoland/boards2/v1:test123/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n"},{"name":"z_create_thread_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_create_thread_06_filetest\n\n// Open board: Test creating a new thread as a non member user that has no GNOT\npackage z_create_thread_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Non members should be able to create threads only if they have enough GNOT\n\tboards2.CreateThread(cross(cur), bid, \"Title\", \"Body\")\n}\n\n// Error:\n// caller is not allowed to create threads: account amount is lower than 3000 GNOT\n"},{"name":"z_delete_reply_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_00_filetest\n\npackage z_delete_reply_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteReply(cross(cur), bid, tid, rid)\n\n\t// Ensure reply doesn't exist\n\tprintln(boards2.Render(\"test-board/1/2\"))\n}\n\n// Output:\n// Reply not found\n"},{"name":"z_delete_reply_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_01_filetest\n\npackage z_delete_reply_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteReply(cross(cur), 404, 1, 1)\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_delete_reply_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_02_filetest\n\npackage z_delete_reply_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteReply(cross(cur), bid, 404, 1)\n}\n\n// Error:\n// thread does not exist with ID: 404\n"},{"name":"z_delete_reply_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_03_filetest\n\npackage z_delete_reply_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteReply(cross(cur), bid, tid, 404)\n}\n\n// Error:\n// reply does not exist with ID: 404\n"},{"name":"z_delete_reply_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_04_filetest\n\npackage z_delete_reply_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"Parent\")\n\tboards2.CreateReply(cross(cur), bid, tid, rid, \"Child reply\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteReply(cross(cur), bid, tid, rid)\n\n\t// Render content must contain the releted message instead of reply's body\n\tprintln(boards2.Render(\"test-board/1/2\"))\n}\n\n// Output:\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// bar\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1) • [Show all Replies](/r/gnoland/boards2/v1:test-board/1)\n//\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e ⚠ This comment has been deleted\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n// \u003e\n// \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/2/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/2/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/2/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=2\u0026threadID=1)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:test-board/1/2?order=desc)\n// \u003e\n// \u003e \u003e\n// \u003e \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#3](/r/gnoland/boards2/v1:test-board/1/3)\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e \u003cgno-foreign\u003e\n// \u003e \u003e Child reply\n// \u003e \u003e \u003c/gno-foreign\u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/3/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/3/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/3/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=3\u0026threadID=1)\n"},{"name":"z_delete_reply_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_05_filetest\n\npackage z_delete_reply_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\t// Call using a user that has not permission to delete replies\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.DeleteReply(cross(cur), bid, tid, rid)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_delete_reply_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_06_filetest\n\npackage z_delete_reply_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner  address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n\n\t// Invite a member using a role with permission to delete replies\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(member))\n\n\tboards2.DeleteReply(cross(cur), bid, tid, rid)\n\n\t// Ensure reply doesn't exist\n\tprintln(boards2.Render(\"test-board/1/2\"))\n}\n\n// Output:\n// Reply not found\n"},{"name":"z_delete_reply_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_07_filetest\n\n// Open board: Test deleting a reply as the non member user that created it\npackage z_delete_reply_07_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID // Operate on board DAO\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Title\", \"Body\")\n\n\t// Make sure user account has the required amount of GNOT for open board actions\n\ttesting.IssueCoins(user, chain.Coins{{\"ugnot\", 3_000_000_000}})\n\n\t// Create a new reply as a non member user\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"Comment\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Delete the reply as the non member user that created it\n\tboards2.DeleteReply(cross(cur), bid, tid, rid)\n\n\t// Ensure reply doesn't exist\n\tprintln(rid == 2)\n\tprintln(boards2.Render(\"test123/1/2\"))\n}\n\n// Output:\n// true\n// Reply not found\n"},{"name":"z_delete_reply_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_08_filetest\n\n// Open board: Test deleting a reply of another non member user\npackage z_delete_reply_08_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tuser2 address = \"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\"\n)\n\nvar (\n\tbid      boards.ID // Operate on board DAO\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Title\", \"Body\")\n\n\t// Make sure user account has the required amount of GNOT for open board actions\n\ttesting.IssueCoins(user, chain.Coins{{\"ugnot\", 3_000_000_000}})\n\n\t// Create a new reply as a non member user\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"Comment\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\n\t// Try to delete the reply of another non member user\n\tboards2.DeleteReply(cross(cur), bid, tid, rid)\n}\n\n// Error:\n// unauthorized, user g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt doesn't have the required permission\n"},{"name":"z_delete_reply_09_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_reply_09_filetest\n\n// Hard-deleting a childless DIRECT thread reply removes it from the thread's\n// direct-children list (not just the flat AllReplies index), so the threaded\n// view doesn't render a ghost comment and the reply count stays accurate.\npackage z_delete_reply_09_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n\trb  boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"reply-A\")      // #2, direct, leaf\n\trb = boards2.CreateReply(cross(cur), bid, tid, 0, \"reply-B\") // #3, direct, leaf\n\tboards2.CreateReply(cross(cur), bid, tid, 0, \"reply-C\")      // #4, direct, leaf\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.DeleteReply(cross(cur), bid, tid, rb) // hard delete the leaf\n\n\tout := boards2.Render(\"test-board/1\")\n\t// A and C remain; B must be fully gone (no ghost) from the threaded view.\n\tok := strings.Contains(out, \"reply-A\") \u0026\u0026\n\t\tstrings.Contains(out, \"reply-C\") \u0026\u0026\n\t\t!strings.Contains(out, \"reply-B\")\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_delete_thread_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_thread_00_filetest\n\npackage z_delete_thread_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\ttitle         = \"Test Thread\"\n\tbody          = \"Test body\"\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, title, body)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteThread(cross(cur), bid, pid)\n\n\t// Ensure thread doesn't exist\n\tprintln(boards2.Render(\"test-board/1\"))\n}\n\n// Output:\n// Thread not found\n"},{"name":"z_delete_thread_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_thread_01_filetest\n\npackage z_delete_thread_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteThread(cross(cur), 404, 1)\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_delete_thread_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_thread_02_filetest\n\npackage z_delete_thread_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.DeleteThread(cross(cur), bid, 404)\n}\n\n// Error:\n// thread does not exist with ID: 404\n"},{"name":"z_delete_thread_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_thread_03_filetest\n\npackage z_delete_thread_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\t// Call using a user that has not permission to delete threads\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.DeleteThread(cross(cur), bid, pid)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_delete_thread_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_thread_04_filetest\n\npackage z_delete_thread_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner  address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\n\t// Invite a member using a role with permission to delete threads\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(member))\n\n\tboards2.DeleteThread(cross(cur), bid, pid)\n\n\t// Ensure thread doesn't exist\n\tprintln(boards2.Render(\"test-board/1\"))\n}\n\n// Output:\n// Thread not found\n"},{"name":"z_delete_thread_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_thread_05_filetest\n\n// Open board: Test deleting a thread as the non member user that created it\npackage z_delete_thread_05_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID // Operate on board DAO\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n\n\t// Make sure user account has the required amount of GNOT for open board actions\n\ttesting.IssueCoins(user, chain.Coins{{\"ugnot\", 3_000_000_000}})\n\n\t// Create a new reply as a non member user\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttid = boards2.CreateThread(cross(cur), bid, \"Title\", \"Body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Delete the thread as the non member user that created it\n\tboards2.DeleteThread(cross(cur), bid, tid)\n\n\t// Ensure reply doesn't exist\n\tprintln(tid == 1)\n\tprintln(boards2.Render(\"test123/1\"))\n}\n\n// Output:\n// true\n// Thread not found\n"},{"name":"z_delete_thread_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_delete_thread_06_filetest\n\n// Open board: Test deleting a reply of another non member user\npackage z_delete_thread_06_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tuser2 address = \"g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt\"\n)\n\nvar (\n\tbid boards.ID // Operate on board DAO\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, true)\n\n\t// Make sure user account has the required amount of GNOT for open board actions\n\ttesting.IssueCoins(user, chain.Coins{{\"ugnot\", 3_000_000_000}})\n\n\t// Create a new reply as a non member user\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttid = boards2.CreateThread(cross(cur), bid, \"Title\", \"Body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\n\t// Try to delete the thread of another non member user\n\tboards2.DeleteThread(cross(cur), bid, tid)\n}\n\n// Error:\n// unauthorized, user g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt doesn't have the required permission\n"},{"name":"z_edit_reply_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_00_filetest\n\npackage z_edit_reply_00_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tbody          = \"Test reply\"\n\tpath          = \"test-board/1/2\"\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditReply(cross(cur), bid, tid, rid, body)\n\n\t// Render content must contain the modified reply\n\tcontent := boards2.Render(path)\n\tprintln(strings.Contains(content, \"\\n\u003e \"+body))\n}\n\n// Output:\n// true\n"},{"name":"z_edit_reply_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_01_filetest\n\npackage z_edit_reply_01_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tbody          = \"Test reply\"\n\tpath          = \"test-board/1/2\"\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\n\t// Create a reply and a sub reply\n\tparentRID := boards2.CreateReply(cross(cur), bid, tid, 0, \"Parent\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, parentRID, \"Child\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditReply(cross(cur), bid, tid, rid, body)\n\n\t// Render content must contain the modified reply\n\tcontent := boards2.Render(path)\n\tprintln(strings.Contains(content, \"\\n\u003e \u003e \"+body))\n}\n\n// Output:\n// true\n"},{"name":"z_edit_reply_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_02_filetest\n\npackage z_edit_reply_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditReply(cross(cur), 404, 1, 0, \"body\")\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_edit_reply_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_03_filetest\n\npackage z_edit_reply_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditReply(cross(cur), bid, 404, 0, \"body\")\n}\n\n// Error:\n// thread does not exist with ID: 404\n"},{"name":"z_edit_reply_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_04_filetest\n\npackage z_edit_reply_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditReply(cross(cur), bid, tid, 404, \"body\")\n}\n\n// Error:\n// reply does not exist with ID: 404\n"},{"name":"z_edit_reply_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_05_filetest\n\npackage z_edit_reply_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.EditReply(cross(cur), bid, tid, rid, \"new body\")\n}\n\n// Error:\n// only the reply creator is allowed to edit it\n"},{"name":"z_edit_reply_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_06_filetest\n\npackage z_edit_reply_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n\n\t// Flag the reply so it's hidden\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditReply(cross(cur), bid, tid, rid, \"body\")\n}\n\n// Error:\n// reply is hidden\n"},{"name":"z_edit_reply_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_07_filetest\n\npackage z_edit_reply_07_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\ttid, rid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditReply(cross(cur), bid, tid, rid, \"\")\n}\n\n// Error:\n// body is empty\n"},{"name":"z_edit_reply_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_reply_08_filetest\n\n// Editing a reply to contain a \u003cgno-form\u003e is now accepted: the\n// write-time blacklist relaxation applies to edits too (EditReply shares\n// the validator). The body renders inside a \u003cgno-foreign\u003e sandbox.\npackage z_edit_reply_08_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid, tid, rid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Previously rejected on edit (\"forms are not allowed in replies\").\n\tboards2.EditReply(cross(cur), bid, tid, rid, \"\u003cgno-form\u003e\u003cgno-select name=\\\"foo\\\" value=\\\"\\\" /\u003e\u003c/gno-form\u003e\")\n\n\tcontent := boards2.Render(\"test-board/1\")\n\tprintln(strings.Contains(content, \"\u003cgno-foreign\u003e\"))\n}\n\n// Output:\n// true\n"},{"name":"z_edit_thread_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_00_filetest\n\npackage z_edit_thread_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\ttitle         = \"Test Thread\"\n\tbody          = \"Test body\"\n\tpath          = \"test-board/1\"\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditThread(cross(cur), bid, pid, title, body)\n\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## Test Thread\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Test body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n"},{"name":"z_edit_thread_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_01_filetest\n\npackage z_edit_thread_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditThread(cross(cur), bid, pid, \"\", \"bar\")\n}\n\n// Error:\n// title is empty\n"},{"name":"z_edit_thread_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_02_filetest\n\npackage z_edit_thread_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditThread(cross(cur), bid, pid, \"Foo\", \"\")\n}\n\n// Error:\n// body is empty\n"},{"name":"z_edit_thread_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_03_filetest\n\npackage z_edit_thread_03_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditThread(cross(cur), 404, 1, \"Foo\", \"bar\")\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_edit_thread_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_04_filetest\n\npackage z_edit_thread_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditThread(cross(cur), bid, pid, \"Foo\", \"\")\n}\n\n// Error:\n// body is empty\n"},{"name":"z_edit_thread_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_05_filetest\n\npackage z_edit_thread_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.EditThread(cross(cur), bid, pid, \"Foo\", \"bar\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_edit_thread_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_06_filetest\n\npackage z_edit_thread_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\ttitle         = \"Test Thread\"\n\tbody          = \"Test body\"\n\tpath          = \"test-board/1\"\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\n\t// Invite a member using a role with permission to edit threads\n\tboards2.InviteMember(cross(cur), bid, admin, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\n\tboards2.EditThread(cross(cur), bid, pid, title, body)\n\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## Test Thread\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Test body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n"},{"name":"z_edit_thread_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_edit_thread_07_filetest\n\npackage z_edit_thread_07_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid   boards.ID\n\tpid   boards.ID\n\ttitle = strings.Repeat(\"X\", boards2.MaxThreadTitleLength+1)\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.EditThread(cross(cur), bid, pid, title, \"bar\")\n}\n\n// Error:\n// title is too long, maximum allowed is 100 characters\n"},{"name":"z_flag_reply_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_00_filetest\n\npackage z_flag_reply_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"Reason\")\n\n\t// Render content must contain a message about the hidden reply\n\tprintln(boards2.Render(\"test-board/1/2\"))\n}\n\n// Output:\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// bar\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1) • [Show all Replies](/r/gnoland/boards2/v1:test-board/1)\n//\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e ⚠ Reply is hidden as it has been flagged as [inappropriate](/r/gnoland/boards2/v1:test-board/1/2/flagging-reasons)\n"},{"name":"z_flag_reply_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_01_filetest\n\npackage z_flag_reply_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagReply(cross(cur), 404, 1, 1, \"Reason\")\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_flag_reply_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_02_filetest\n\npackage z_flag_reply_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagReply(cross(cur), bid, 404, 1, \"Reason\")\n}\n\n// Error:\n// thread does not exist with ID: 404\n"},{"name":"z_flag_reply_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_03_filetest\n\npackage z_flag_reply_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagReply(cross(cur), bid, tid, 404, \"Reason\")\n}\n\n// Error:\n// reply does not exist with ID: 404\n"},{"name":"z_flag_reply_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_04_filetest\n\npackage z_flag_reply_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"Reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"Reason\")\n}\n\n// Error:\n// flagging hidden comments or replies is not allowed\n"},{"name":"z_flag_reply_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_05_filetest\n\npackage z_flag_reply_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"Reason\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_flag_reply_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_06_filetest\n\npackage z_flag_reply_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmoderator address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n\n\t// Invite a member using a role with permission to flag replies\n\tboards2.InviteMember(cross(cur), bid, moderator, boards2.RoleModerator)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(moderator))\n\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"Reason\")\n\n\t// Render content must contain a message about the hidden reply\n\tprintln(boards2.Render(\"test-board/1/2\"))\n}\n\n// Output:\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// bar\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1) • [Show all Replies](/r/gnoland/boards2/v1:test-board/1)\n//\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e ⚠ Reply is hidden as it has been flagged as [inappropriate](/r/gnoland/boards2/v1:test-board/1/2/flagging-reasons)\n"},{"name":"z_flag_reply_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_07_filetest\n\npackage z_flag_reply_07_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmoderator address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\t// Created a board with flagging threshold greater than 1\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tboards2.SetFlaggingThreshold(cross(cur), bid, 2)\n\n\t// Create a reply so the realm owner can flag and hide it with a single flag\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n\t// Also freeze board to make sure that realm owner can still flag the reply\n\tboards2.FreezeBoard(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"Reason\")\n\n\t// Render content must contain a message about the hidden reply\n\tprintln(boards2.Render(\"test-board/1/2\"))\n}\n\n// Output:\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// bar\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Show all Replies](/r/gnoland/boards2/v1:test-board/1)\n//\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e ⚠ Reply is hidden as it has been flagged as [inappropriate](/r/gnoland/boards2/v1:test-board/1/2/flagging-reasons)\n"},{"name":"z_flag_reply_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_08_filetest\n\npackage z_flag_reply_08_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"\")\n}\n\n// Error:\n// flagging reason is required\n"},{"name":"z_flag_reply_09_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_reply_09_filetest\n\n// A flag reason is user-supplied (only trimmed at write). The flagging-reasons\n// table must escape it so a reason can't inject markdown (links/images) or HTML\n// into the view other moderators see.\npackage z_flag_reply_09_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.FlagReply(cross(cur), bid, tid, rid, \"![pwn](https://evil/x.png) [click](https://evil)\")\n\n\tout := boards2.Render(\"test-board/1/2/flagging-reasons\")\n\t// Reason text is shown but escaped — no live markdown image/link syntax.\n\tok := strings.Contains(out, \"pwn\") \u0026\u0026\n\t\t!strings.Contains(out, \"![pwn]\") \u0026\u0026\n\t\t!strings.Contains(out, \"](https://evil)\")\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_flag_thread_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_00_filetest\n\npackage z_flag_thread_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmoderator address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\n\t// Invite a moderator to the new board\n\tboards2.InviteMember(cross(cur), bid, moderator, boards2.RoleModerator)\n\n\t// Create a new thread as a moderator\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Flag thread as owner\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n\n\t// Ensure thread is not listed\n\tprintln(boards2.Render(\"test-board\"))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › test-board\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// ↳ [Create Thread](/r/gnoland/boards2/v1:test-board/create-thread) • [Request Invite](/r/gnoland/boards2/v1$help\u0026func=RequestInvite\u0026boardID=1) • [Manage Board](?menu=manageBoard)\n//\n// ---\n// ### This board doesn't have any threads\n// Do you want to [start a new conversation](/r/gnoland/boards2/v1:test-board/create-thread) in this board?\n"},{"name":"z_flag_thread_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_01_filetest\n\npackage z_flag_thread_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagThread(cross(cur), 404, 1, \"Reason\")\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_flag_thread_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_02_filetest\n\npackage z_flag_thread_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\t// Make the next call as an uninvited user\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_flag_thread_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_03_filetest\n\npackage z_flag_thread_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagThread(cross(cur), bid, 404, \"Reason\")\n}\n\n// Error:\n// thread not found\n"},{"name":"z_flag_thread_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_04_filetest\n\npackage z_flag_thread_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n}\n\n// Error:\n// flagging hidden threads is not allowed\n"},{"name":"z_flag_thread_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_05_filetest\n\npackage z_flag_thread_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmoderator address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\n\t// Invite a member using a role with permission to flag threads\n\tboards2.InviteMember(cross(cur), bid, moderator, boards2.RoleModerator)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(moderator))\n\n\t// Flag thread as moderator\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n\n\t// Ensure that original thread content not visible\n\tprintln(boards2.Render(\"test-board/1\"))\n}\n\n// Output:\n// ⚠ Thread has been flagged as [inappropriate](/r/gnoland/boards2/v1:test-board/1/flagging-reasons)\n"},{"name":"z_flag_thread_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_06_filetest\n\npackage z_flag_thread_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmoderator address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\t// Created a board with a specific flagging threshold\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tboards2.SetFlaggingThreshold(cross(cur), bid, 2)\n\n\t// Invite a moderator to the new board\n\tboards2.InviteMember(cross(cur), bid, moderator, boards2.RoleModerator)\n\n\t// Create a new thread and flag it as a moderator\n\ttesting.SetRealm(testing.NewUserRealm(moderator))\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(moderator))\n\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n}\n\n// Error:\n// post has been already flagged by g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\n"},{"name":"z_flag_thread_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_07_filetest\n\npackage z_flag_thread_07_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\t// Created a board with flagging threshold greater than 1\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tboards2.SetFlaggingThreshold(cross(cur), bid, 2)\n\n\t// Create a thread so the realm owner can flag and hide it with a single flag\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n\t// Also freeze board to make sure that realm owner can still flag the thread\n\tboards2.FreezeBoard(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagThread(cross(cur), bid, pid, \"Reason\")\n\n\t// Ensure that original thread content not visible\n\tprintln(boards2.Render(\"test-board/1\"))\n}\n\n// Output:\n// ⚠ Thread has been flagged as [inappropriate](/r/gnoland/boards2/v1:test-board/1/flagging-reasons)\n"},{"name":"z_flag_thread_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_flag_thread_08_filetest\n\npackage z_flag_thread_08_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\tpid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\tpid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FlagThread(cross(cur), bid, pid, \"\")\n}\n\n// Error:\n// flagging reason is required\n"},{"name":"z_freeze_board_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_freeze_board_00_filetest\n\npackage z_freeze_board_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FreezeBoard(cross(cur), bid)\n\n\tprintln(boards2.IsBoardFrozen(bid))\n}\n\n// Output:\n// true\n"},{"name":"z_freeze_board_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_freeze_board_01_filetest\n\npackage z_freeze_board_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tboards2.FreezeBoard(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FreezeBoard(cross(cur), bid)\n}\n\n// Error:\n// board is frozen\n"},{"name":"z_freeze_thread_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_freeze_thread_00_filetest\n\npackage z_freeze_thread_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"foo\", \"bar\")\n\n\tboards2.FreezeBoard(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Attempt to freeze a thread on frozen board\n\tboards2.FreezeThread(cross(cur), bid, tid)\n}\n\n// Error:\n// board is frozen\n"},{"name":"z_freeze_thread_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_freeze_thread_01_filetest\n\npackage z_freeze_thread_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"foo\", \"bar\")\n\n\tboards2.FreezeThread(cross(cur), bid, tid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Attempt to freeze a frozen thread\n\tboards2.FreezeThread(cross(cur), bid, tid)\n}\n\n// Error:\n// thread is frozen\n"},{"name":"z_freeze_thread_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_freeze_thread_02_filetest\n\npackage z_freeze_thread_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"foo\", \"bar\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.FreezeThread(cross(cur), bid, tid)\n\n\tprintln(boards2.IsThreadFrozen(bid, tid))\n}\n\n// Output:\n// true\n"},{"name":"z_get_board_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_get_board_00_filetest\n\npackage z_get_board_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\n\t// Calling as a Boards2 sub realm should succeed\n\tboard, found := boards2.GetBoard(cross(cur), bid)\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(board.ID == bid)\n}\n\n// Output:\n// true\n"},{"name":"z_get_board_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_get_board_01_filetest\n\npackage z_get_board_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/test\"))\n\n\t// Calling as a non Boards2 sub realm should fail\n\tboards2.GetBoard(cross(cur), bid)\n}\n\n// Error:\n// forbidden, caller should live within \"gno.land/r/gnoland/boards2/\" namespace\n"},{"name":"z_get_board_id_from_name_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_get_board_id_from_name_00_filetest\n\npackage z_get_board_id_from_name_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tname          = \"test123\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), name, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tbid2, found := boards2.GetBoardIDFromName(cross(cur), name)\n\n\tprintln(found)\n\tprintln(bid2 == bid)\n}\n\n// Output:\n// true\n// true\n"},{"name":"z_get_board_id_from_name_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_get_board_id_from_name_01_filetest\n\npackage z_get_board_id_from_name_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tbid, found := boards2.GetBoardIDFromName(cross(cur), \"foobar\")\n\n\tprintln(found)\n\tprintln(bid == 0)\n}\n\n// Output:\n// false\n// true\n"},{"name":"z_get_realm_permissions_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_get_realm_permissions_00_filetest\n\npackage z_get_realm_permissions_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tperms := boards2.GetRealmPermissions(cross(cur))\n\tprintln(perms != nil)\n}\n\n// Output:\n// true\n"},{"name":"z_get_realm_permissions_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_get_realm_permissions_01_filetest\n\npackage z_get_realm_permissions_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/test\"))\n\n\t// Calling as a non Boards2 sub realm should fail\n\t_ = boards2.GetRealmPermissions(cross(cur))\n}\n\n// Error:\n// forbidden, caller should live within \"gno.land/r/gnoland/boards2/\" namespace\n"},{"name":"z_invite_member_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_invite_member_00_filetest\n\npackage z_invite_member_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tbid           = boards.ID(0)                               // Operate on realm DAO instead of individual boards\n\trole          = boards2.RoleOwner\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.InviteMember(cross(cur), bid, user, role)\n\n\t// Check that user is invited\n\tprintln(boards2.HasMemberRole(bid, user, role))\n}\n\n// Output:\n// true\n"},{"name":"z_invite_member_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_invite_member_01_filetest\n\npackage z_invite_member_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tuser  address = \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\t// Add an admin member\n\tboards2.InviteMember(cross(cur), bid, admin, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\t// Next call will be done by the admin member\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\n\tboards2.InviteMember(cross(cur), bid, user, boards2.RoleOwner)\n}\n\n// Error:\n// only owners are allowed to invite other owners\n"},{"name":"z_invite_member_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_invite_member_02_filetest\n\npackage z_invite_member_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tuser  address = \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\"\n\trole          = boards2.RoleAdmin\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\t// Add an admin member\n\tboards2.InviteMember(cross(cur), bid, admin, boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\t// Next call will be done by the admin member\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\n\tboards2.InviteMember(cross(cur), bid, user, role)\n\n\t// Check that user is invited\n\tprintln(boards2.HasMemberRole(bid, user, role))\n}\n\n// Output:\n// true\n"},{"name":"z_invite_member_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_invite_member_03_filetest\n\npackage z_invite_member_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.InviteMember(cross(cur), 0, user, boards.Role(\"foobar\")) // Operate on realm DAO instead of individual boards\n}\n\n// Error:\n// invalid role: foobar\n"},{"name":"z_invite_member_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_invite_member_04_filetest\n\npackage z_invite_member_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\trole          = boards2.RoleOwner\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"foo123\", false, false) // Operate on board DAO members\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.InviteMember(cross(cur), bid, user, role)\n\n\t// Check that user is invited\n\tprintln(boards2.HasMemberRole(0, user, role)) // Operate on realm DAO\n\tprintln(boards2.HasMemberRole(bid, user, role))\n}\n\n// Output:\n// false\n// true\n"},{"name":"z_invite_member_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_invite_member_05_filetest\n\npackage z_invite_member_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tbid           = boards.ID(0)                               // Operate on realm DAO instead of individual boards\n\trole          = boards2.RoleOwner\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.InviteMember(cross(cur), bid, user, role)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.InviteMember(cross(cur), bid, user, role)\n}\n\n// Error:\n// user is already a member: g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\n"},{"name":"z_invite_member_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_invite_member_06_filetest\n\npackage z_invite_member_06_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.InviteMember(cross(cur), 0, \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\", boards2.RoleGuest)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_is_banned_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_is_banned_00_filetest\n\npackage z_is_banned_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main() {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tprintln(boards2.IsBanned(bid, \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"))\n}\n\n// Output:\n// false\n"},{"name":"z_is_banned_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_is_banned_01_filetest\n\npackage z_is_banned_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tboards2.Ban(cross(cur), bid, user, boards2.BanDay, \"Reason\")\n}\n\nfunc main() {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tprintln(boards2.IsBanned(bid, user))\n}\n\n// Output:\n// true\n"},{"name":"z_is_member_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_is_member_00_filetest\n\npackage z_is_member_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner  address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\trole           = boards2.RoleGuest\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\tboards2.InviteMember(cross(cur), bid, member, role)\n}\n\nfunc main() {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tprintln(boards2.HasMemberRole(bid, member, role))\n\tprintln(boards2.HasMemberRole(bid, member, \"invalid\"))\n\tprintln(boards2.IsMember(bid, member))\n}\n\n// Output:\n// true\n// false\n// true\n"},{"name":"z_is_member_01_filetest.gno","body":"package main\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tbid           = boards.ID(0)                               // Operate on realm DAO instead of individual boards\n\trole          = boards2.RoleGuest\n)\n\nfunc main() {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tprintln(boards2.HasMemberRole(bid, user, role))\n\tprintln(boards2.IsMember(bid, user))\n}\n\n// Output:\n// false\n// false\n"},{"name":"z_iterate_realm_members_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_iterate_realm_members_00_filetest\n\npackage z_iterate_realm_members_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tbid           = boards.ID(0) // Operate on realm DAO instead of individual boards\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.InviteMember(cross(cur), bid, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", boards2.RoleAdmin)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\n\t// Calling as a Boards2 sub realm should succeed\n\tperms := boards2.GetRealmPermissions(cross(cur))\n\tperms.IterateUsers(0, perms.UsersCount(), func(u boards.User) bool {\n\t\tprintln(u.Address, string(u.Roles[0]))\n\t\treturn false\n\t})\n}\n\n// Output:\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh owner\n// g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj admin\n"},{"name":"z_lock_realm_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_lock_realm_00_filetest\n\npackage z_lock_realm_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.LockRealm(cross(cur), false)\n\n\tprintln(boards2.IsRealmLocked())\n\tprintln(boards2.AreRealmMembersLocked())\n}\n\n// Output:\n// true\n// false\n"},{"name":"z_lock_realm_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_lock_realm_01_filetest\n\npackage z_lock_realm_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.LockRealm(cross(cur), false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Should fail because realm is already locked\n\tboards2.LockRealm(cross(cur), false)\n}\n\n// Error:\n// realm is locked\n"},{"name":"z_lock_realm_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_lock_realm_02_filetest\n\npackage z_lock_realm_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\nfunc main(cur realm) {\n\t// Call realm with a user that has not permission to lock the realm\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.LockRealm(cross(cur), false)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_lock_realm_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_lock_realm_03_filetest\n\npackage z_lock_realm_03_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.LockRealm(cross(cur), true)\n\n\tprintln(boards2.IsRealmLocked())\n\tprintln(boards2.AreRealmMembersLocked())\n}\n\n// Output:\n// true\n// true\n"},{"name":"z_lock_realm_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_lock_realm_04_filetest\n\npackage z_lock_realm_04_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.LockRealm(cross(cur), true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Should fail because realm is already locked\n\tboards2.LockRealm(cross(cur), true)\n}\n\n// Error:\n// realm and members are locked\n"},{"name":"z_lock_realm_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_lock_realm_05_filetest\n\npackage z_lock_realm_05_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\t// Lock the realm without locking realm members\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.LockRealm(cross(cur), false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.LockRealm(cross(cur), true)\n\n\tprintln(boards2.IsRealmLocked())\n\tprintln(boards2.AreRealmMembersLocked())\n}\n\n// Output:\n// true\n// true\n"},{"name":"z_remove_member_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_remove_member_00_filetest\n\npackage z_remove_member_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\tboards2.InviteMember(cross(cur), bid, user, boards2.RoleGuest)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RemoveMember(cross(cur), bid, user)\n\n\t// Check that user is not a member\n\tprintln(boards2.IsMember(bid, user))\n}\n\n// Output:\n// false\n"},{"name":"z_remove_member_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_remove_member_01_filetest\n\npackage z_remove_member_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RemoveMember(cross(cur), 0, \"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\") // Operate on realm DAO instead of individual boards\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_remove_member_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_remove_member_02_filetest\n\npackage z_remove_member_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RemoveMember(cross(cur), 0, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\") // Operate on realm DAO instead of individual boards\n}\n\n// Error:\n// member not found\n"},{"name":"z_remove_member_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_remove_member_03_filetest\n\npackage z_remove_member_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\tboards2.InviteMember(cross(cur), bid, user, boards2.RoleGuest)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Users must be able to remove themselves without permissions\n\tboards2.RemoveMember(cross(cur), bid, user)\n\n\t// Check that user is not a member\n\tprintln(boards2.IsMember(bid, user))\n}\n\n// Output:\n// false\n"},{"name":"z_rename_board_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_00_filetest\n\npackage z_rename_board_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tname            = \"foo123\"\n\tnewName         = \"bar123\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), name, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RenameBoard(cross(cur), name, newName)\n\n\t// Ensure board is renamed by the default board owner\n\tbid2, _ := boards2.GetBoardIDFromName(cross(cur), newName)\n\tprintln(\"IDs match =\", bid == bid2)\n}\n\n// Output:\n// IDs match = true\n"},{"name":"z_rename_board_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_01_filetest\n\npackage z_rename_board_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tname          = \"foo123\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), name, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RenameBoard(cross(cur), name, \"\")\n}\n\n// Error:\n// board name is empty\n"},{"name":"z_rename_board_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_02_filetest\n\npackage z_rename_board_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tname          = \"foo123\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), name, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RenameBoard(cross(cur), name, name)\n}\n\n// Error:\n// board already exists\n"},{"name":"z_rename_board_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_03_filetest\n\npackage z_rename_board_03_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RenameBoard(cross(cur), \"unexisting\", \"foo\")\n}\n\n// Error:\n// board does not exist with name: unexisting\n"},{"name":"z_rename_board_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_04_filetest\n\npackage z_rename_board_04_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tname          = \"foo123\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), name, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RenameBoard(cross(cur), name, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n}\n\n// Error:\n// addresses are not allowed as board name\n"},{"name":"z_rename_board_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_05_filetest\n\npackage z_rename_board_05_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tname            = \"foo123\"\n\tnewName         = \"barbaz123\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tbid = boards2.CreateBoard(cross(cur), name, false, false)\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleOwner)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(member))\n\n\tboards2.RenameBoard(cross(cur), name, newName)\n\n\t// Ensure board is renamed by another board owner\n\tbid2, _ := boards2.GetBoardIDFromName(cross(cur), newName)\n\tprintln(\"IDs match =\", bid == bid2)\n}\n\n// Output:\n// IDs match = true\n"},{"name":"z_rename_board_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_06_filetest\n\npackage z_rename_board_06_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tmember2 address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n\tname            = \"foo123\"\n\tnewName         = \"barbaz123\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), name, false, false)\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleOwner)\n\n\t// Test1 is the boards owner and its address has a user already registered\n\t// so a new member must register a user with the new board name.\n\t// uinit.RegisterUser is genesis-only since the security fix.\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), newName, member)\n\ttesting.SetHeight(123)\n\n\t// Invite a new member that doesn't own the user that matches the new board name\n\tboards2.InviteMember(cross(cur), bid, member2, boards2.RoleOwner)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(member2))\n\n\tboards2.RenameBoard(cross(cur), name, newName)\n}\n\n// Error:\n// board name is a user name registered to a different user\n"},{"name":"z_rename_board_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_07_filetest\n\npackage z_rename_board_07_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tname          = \"foo123\"\n)\n\nvar newName string\n\nfunc init(cur realm) {\n\tnewName = strings.Repeat(\"A\", boards2.MaxBoardNameLength+1)\n\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), name, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RenameBoard(cross(cur), name, newName)\n}\n\n// Error:\n// board name is too long, maximum allowed is 50 characters\n"},{"name":"z_rename_board_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_08_filetest\n\npackage z_rename_board_08_filetest\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nconst (\n\towner   address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tmember  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tmember2 address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n\tname            = \"foo123\"\n\tnewName         = \"barbaz123\"\n)\n\nvar bid boards.ID // Operate on board DAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), name, false, false)\n\tboards2.InviteMember(cross(cur), bid, member, boards2.RoleOwner)\n\n\t// Test1 is the boards owner and its address has a user already registered\n\t// so a new member must register a user with the new board name.\n\t// uinit.RegisterUser is genesis-only since the security fix.\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), newName, member)\n\ttesting.SetHeight(123)\n\n\t// Invite a new member that doesn't own the user that matches the new board name\n\tboards2.InviteMember(cross(cur), bid, member2, boards2.RoleOwner)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(member2))\n\n\tboards2.RenameBoard(cross(cur), name, newName)\n}\n\n// Error:\n// board name is a user name registered to a different user\n"},{"name":"z_rename_board_09_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_rename_board_09_filetest\n\npackage z_rename_board_09_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tname          = \"foo123\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), name, false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.RenameBoard(cross(cur), name, \"barbaz\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_request_invite_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_request_invite_00_filetest\n\npackage z_request_invite_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"))\n\n\tboards2.RequestInvite(cross(cur), bid)\n\n\tprintln(boards2.Render(\"test123/invites\"))\n}\n\n// Output:\n// # test123 Invite Requests\n// ### These users have requested to be invited to the board\n// | User | Request Date | Actions |\n// | --- | --- | --- |\n// | [g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5](/u/g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5) | 2009-02-13 11:31pm UTC | [accept](/r/gnoland/boards2/v1$help\u0026func=AcceptInvite\u0026boardID=1\u0026user=g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5) • [revoke](/r/gnoland/boards2/v1$help\u0026func=RevokeInvite\u0026boardID=1\u0026user=g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5) |\n"},{"name":"z_request_invite_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_request_invite_01_filetest\n\npackage z_request_invite_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\n// Error:\n// caller must be user\n"},{"name":"z_request_invite_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_request_invite_02_filetest\n\npackage z_request_invite_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\n// Error:\n// caller is already a member\n"},{"name":"z_request_invite_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_request_invite_03_filetest\n\npackage z_request_invite_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\n// Error:\n// invite request already exists\n"},{"name":"z_request_invite_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_request_invite_04_filetest\n\npackage z_request_invite_04_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.RequestInvite(cross(cur), 404)\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_revoke_invite_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_revoke_invite_00_filetest\n\npackage z_revoke_invite_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RevokeInvite(cross(cur), bid, user)\n\n\tprintln(boards2.IsMember(bid, user))\n\tprintln()\n\tprintln(boards2.Render(\"test123/invites\"))\n}\n\n// Output:\n// false\n//\n// # test123 Invite Requests\n// ### Board has no invite requests\n"},{"name":"z_revoke_invite_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_revoke_invite_01_filetest\n\npackage z_revoke_invite_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.RevokeInvite(cross(cur), bid, user)\n}\n\n// Error:\n// invite request not found\n"},{"name":"z_revoke_invite_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_revoke_invite_02_filetest\n\npackage z_revoke_invite_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tboards2.RequestInvite(cross(cur), bid)\n}\n\nfunc main(cur realm) {\n\t// Caller is not a member and has no permission to revoke invites\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.RevokeInvite(cross(cur), bid, user)\n}\n\n// Error:\n// unauthorized, user g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5 doesn't have the required permission\n"},{"name":"z_set_flagging_threshold_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_flagging_threshold_00_filetest\n\npackage z_set_flagging_threshold_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetFlaggingThreshold(cross(cur), bid, 4)\n\n\t// Ensure that flagging threshold changed\n\tprintln(boards2.GetFlaggingThreshold(bid))\n}\n\n// Output:\n// 4\n"},{"name":"z_set_flagging_threshold_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_flagging_threshold_01_filetest\n\npackage z_set_flagging_threshold_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address   = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tbid   boards.ID = 404\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetFlaggingThreshold(cross(cur), bid, 1)\n}\n\n// Error:\n// board does not exist with ID: 404\n"},{"name":"z_set_flagging_threshold_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_flagging_threshold_02_filetest\n\npackage z_set_flagging_threshold_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetFlaggingThreshold(cross(cur), 1, 0)\n}\n\n// Error:\n// invalid flagging threshold\n"},{"name":"z_set_permissions_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_permissions_00_filetest\n\npackage z_set_permissions_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/gnoland/boards/exts/permissions\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tbid           = boards.ID(0) // Operate on realm instead of individual boards\n)\n\nvar perms boards.Permissions\n\nfunc init() {\n\t// Create a new permissions instance without users\n\tperms = permissions.New()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetPermissions(cross(cur), bid, perms)\n\n\t// Owner that setted new permissions is not a member of the new permissions\n\tprintln(boards2.IsMember(bid, owner))\n}\n\n// Output:\n// false\n"},{"name":"z_set_permissions_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_permissions_01_filetest\n\npackage z_set_permissions_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/gnoland/boards/exts/permissions\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\tuser address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\tbid          = boards.ID(0)                               // Operate on realm instead of individual boards\n)\n\nvar perms boards.Permissions\n\nfunc init() {\n\t// Create a new permissions instance\n\tperms = permissions.New()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tboards2.SetPermissions(cross(cur), bid, perms)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_set_permissions_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_permissions_02_filetest\n\npackage z_set_permissions_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/gnoland/boards/exts/permissions\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tperms boards.Permissions\n\tbid   boards.ID\n)\n\nfunc init(cur realm) {\n\t// Create a new permissions instance without users\n\tperms = permissions.New()\n\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"foobar\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetPermissions(cross(cur), bid, perms)\n\n\t// Owner that setted new board permissions is not a member of the new permissions\n\tprintln(boards2.IsMember(bid, owner))\n}\n\n// Output:\n// false\n"},{"name":"z_set_realm_notice_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_realm_notice_00_filetest\n\npackage z_set_realm_notice_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetRealmNotice(cross(cur), \"This is a test realm message\")\n\n\tprintln(boards2.Notice)\n}\n\n// Output:\n// This is a test realm message\n"},{"name":"z_set_realm_notice_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_realm_notice_01_filetest\n\npackage z_set_realm_notice_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\t// Set an initial message so it can be cleared\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.SetRealmNotice(cross(cur), \"This is a test realm message\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetRealmNotice(cross(cur), \"\")\n\n\tprintln(boards2.Notice == \"\")\n}\n\n// Output:\n// true\n"},{"name":"z_set_realm_notice_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_realm_notice_02_filetest\n\npackage z_set_realm_notice_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\nfunc main(cur realm) {\n\t// Call realm with a user that has not permission to set realm notice\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetRealmNotice(cross(cur), \"Foo\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_set_realm_notice_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_realm_notice_03_filetest\n\npackage z_set_realm_notice_03_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetRealmNotice(cross(cur), \"This is a test realm message\")\n\n\tprintln(boards2.Render(\"\"))\n}\n\n// Output:\n//\n// \u003e \\[!INFO\\] Notice\n// \u003e This is a test realm message\n//\n// # Boards\n// [Create Board](/r/gnoland/boards2/v1:create-board) • [List Admin Users](/r/gnoland/boards2/v1:admin-users) • [Help](/r/gnoland/boards2/v1:help)\n//\n// ---\n// ### Currently there are no boards\n// Be the first to [create a new board](/r/gnoland/boards2/v1:create-board)!\n"},{"name":"z_set_required_account_amount_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_required_account_amount_00_filetest\n\npackage z_set_required_account_amount_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetRequiredAccountAmount(cross(cur), 1_000_000)\n\n\tprintln(boards2.RequiredAccountAmount)\n}\n\n// Output:\n// 1000000\n"},{"name":"z_set_required_account_amount_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_required_account_amount_01_filetest\n\npackage z_set_required_account_amount_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetRequiredAccountAmount(cross(cur), 0) // Disable\n\n\tprintln(boards2.RequiredAccountAmount)\n}\n\n// Output:\n// 0\n"},{"name":"z_set_required_account_amount_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_set_required_account_amount_02_filetest\n\npackage z_set_required_account_amount_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\" // @test2\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.SetRequiredAccountAmount(cross(cur), 1_000_000)\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"},{"name":"z_ui_admin_users_00_filetest.gno","body":"// Render realm admin users view.\npackage main\n\nimport (\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nfunc main() {\n\tprintln(boards2.Render(\"admin-users\"))\n}\n\n// Output:\n// # Admin Users\n// ### These are the admin users of the realm\n// | Member | Role | Actions |\n// | --- | --- | --- |\n// | [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) | owner | [remove](/r/gnoland/boards2/v1$help\u0026func=RemoveMember\u0026boardID=0\u0026member=g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) • [change role](/r/gnoland/boards2/v1$help\u0026func=ChangeMemberRole\u0026boardID=0\u0026member=g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\u0026role=) |\n"},{"name":"z_ui_board_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_board_00_filetest\n\n// Render default board view.\npackage z_ui_board_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"TestBoard\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and then add 3 threads\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\n\t// Create thread \"A\" with a single comment\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"A\", \"Body\")\n\tboards2.CreateReply(cross(cur), boardID, threadID, 0, \"Body\")\n\n\t// The other 2 threads are created without comments\n\tboards2.CreateThread(cross(cur), boardID, \"B\", \"Body\")\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"C\", \"Body\")\n\n\t// Repost thread \"C\" into a different board\n\tdstBoardID := boards2.CreateBoard(cross(cur), \"Bar\", false, false)\n\tboards2.CreateRepost(cross(cur), boardID, threadID, dstBoardID, \"Title\", \"Body\")\n}\n\nfunc main() {\n\tprintln(boards2.Render(boardName))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › TestBoard\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// ↳ [Create Thread](/r/gnoland/boards2/v1:TestBoard/create-thread) • [Request Invite](/r/gnoland/boards2/v1$help\u0026func=RequestInvite\u0026boardID=1) • [Manage Board](?menu=manageBoard)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:TestBoard?order=desc)\n//\n// ###### [A](/r/gnoland/boards2/v1:TestBoard/1)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **1 replies • 0 reposts**\n//\n// ###### [B](/r/gnoland/boards2/v1:TestBoard/3)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **0 replies • 0 reposts**\n//\n// ###### [C](/r/gnoland/boards2/v1:TestBoard/4)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **0 replies • 1 reposts**\n"},{"name":"z_ui_board_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_board_01_filetest\n\n// Render board sorting threads from newest to oldest.\npackage z_ui_board_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"TestBoard\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and then add 3 threads\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\n\tboards2.CreateThread(cross(cur), boardID, \"A\", \"Body\")\n\tboards2.CreateThread(cross(cur), boardID, \"B\", \"Body\")\n\tboards2.CreateThread(cross(cur), boardID, \"C\", \"Body\")\n}\n\nfunc main() {\n\tpath := boardName + \"?order=desc\"\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › TestBoard\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// ↳ [Create Thread](/r/gnoland/boards2/v1:TestBoard/create-thread) • [Request Invite](/r/gnoland/boards2/v1$help\u0026func=RequestInvite\u0026boardID=1) • [Manage Board](?menu=manageBoard)\n//\n// ---\n// Sort by: [oldest first](/r/gnoland/boards2/v1:TestBoard?order=asc)\n//\n// ###### [C](/r/gnoland/boards2/v1:TestBoard/3)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **0 replies • 0 reposts**\n//\n// ###### [B](/r/gnoland/boards2/v1:TestBoard/2)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **0 replies • 0 reposts**\n//\n// ###### [A](/r/gnoland/boards2/v1:TestBoard/1)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **0 replies • 0 reposts**\n"},{"name":"z_ui_board_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_board_02_filetest\n\n// Render board view with the manage board menu expanded.\npackage z_ui_board_02_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"TestBoard\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tboards2.CreateThread(cross(cur), boardID, \"A\", \"Body\")\n}\n\nfunc main() {\n\tpath := boardName + \"?menu=manageBoard\"\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › TestBoard\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// ↳ [Create Thread](/r/gnoland/boards2/v1:TestBoard/create-thread) • [Request Invite](/r/gnoland/boards2/v1$help\u0026func=RequestInvite\u0026boardID=1) • **Manage Board**\n// └─ [Invite Member](/r/gnoland/boards2/v1:TestBoard/invite-member) • [List Invite Requests](/r/gnoland/boards2/v1:TestBoard/invites) • [List Members](/r/gnoland/boards2/v1:TestBoard/members) • [List Banned Users](/r/gnoland/boards2/v1:TestBoard/banned-users) • [Freeze Board](/r/gnoland/boards2/v1$help\u0026func=FreezeBoard\u0026boardID=1)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:TestBoard?menu=manageBoard\u0026order=desc)\n//\n// ###### [A](/r/gnoland/boards2/v1:TestBoard/1)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **0 replies • 0 reposts**\n"},{"name":"z_ui_board_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_board_03_filetest\n\n// Render readonly board.\npackage z_ui_board_03_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"TestBoard\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a readonly board and then add a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tboards2.CreateThread(cross(cur), boardID, \"A\", \"Body\")\n\tboards2.FreezeBoard(cross(cur), boardID)\n}\n\nfunc main() {\n\tprintln(boards2.Render(boardName))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › TestBoard\n// \u003e [!WARNING] Info\n// \u003e Creating new threads and commenting are disabled within this board\n//\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// [List Members](/r/gnoland/boards2/v1:TestBoard/members) • [Unfreeze Board](/r/gnoland/boards2/v1$help\u0026func=UnfreezeBoard\u0026boardID=1\u0026replyID=\u0026threadID=)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:TestBoard?order=desc)\n//\n// ###### [A](/r/gnoland/boards2/v1:TestBoard/1)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) `owner` on 2009-02-13 11:31pm UTC\n// **0 replies • 0 reposts**\n"},{"name":"z_ui_board_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_board_04_filetest\n\n// Render default board view when there are no threads.\npackage z_ui_board_04_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"TestBoard\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), boardName, false, false)\n}\n\nfunc main() {\n\tprintln(boards2.Render(boardName))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › TestBoard\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// ↳ [Create Thread](/r/gnoland/boards2/v1:TestBoard/create-thread) • [Request Invite](/r/gnoland/boards2/v1$help\u0026func=RequestInvite\u0026boardID=1) • [Manage Board](?menu=manageBoard)\n//\n// ---\n// ### This board doesn't have any threads\n// Do you want to [start a new conversation](/r/gnoland/boards2/v1:TestBoard/create-thread) in this board?\n"},{"name":"z_ui_board_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_board_05_filetest\n\n// An invalid ?page= (out of range, zero, negative, or non-numeric) clamps to\n// the last valid page instead of aborting the render or echoing a broken\n// \"page N of M\" picker (see newClampedPager). Notably a NEGATIVE page is not\n// rejected by pager.New, so it must be caught by the clamp.\npackage z_ui_board_05_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\t// pageSizeDefault is 6, so 7 threads span 2 pages; the last page is 2.\n\tfor i := 0; i \u003c 7; i++ {\n\t\tboards2.CreateThread(cross(cur), boardID, \"T\", \"Body\")\n\t}\n}\n\nfunc main() {\n\t// Each value is invalid: too high, zero, negative, non-numeric. All must\n\t// clamp to the last page (\"page 2 of 2\"); if any slipped through, the\n\t// picker would echo the bad number (e.g. \"page -5 of 2\") and this fails.\n\tok := true\n\tfor _, q := range []string{\"?page=99\", \"?page=0\", \"?page=-5\", \"?page=abc\"} {\n\t\tif !strings.Contains(boards2.Render(boardName+q), \"page 2 of 2\") {\n\t\t\tok = false\n\t\t}\n\t}\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_ui_board_members_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_board_members_00_filetest\n\n// Render board members view.\npackage z_ui_board_members_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"BoardName\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), boardName, false, false)\n}\n\nfunc main() {\n\tpath := boardName + \"/members\"\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # BoardName Members\n// ### These are the board members\n// | Member | Role | Actions |\n// | --- | --- | --- |\n// | [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) | owner | [remove](/r/gnoland/boards2/v1$help\u0026func=RemoveMember\u0026boardID=1\u0026member=g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) • [change role](/r/gnoland/boards2/v1$help\u0026func=ChangeMemberRole\u0026boardID=1\u0026member=g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\u0026role=) |\n"},{"name":"z_ui_home_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_home_00_filetest\n\n// Render default realm view.\n// Default realm view must render the list of listed boards.\npackage z_ui_home_00_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create board \"AAA\" with a single thread\n\tboardID := boards2.CreateBoard(cross(cur), \"AAA\", true, false)\n\tboards2.CreateThread(cross(cur), boardID, \"Foo\", \"Bar\")\n\n\t// Create 2 more boards\n\tboards2.CreateBoard(cross(cur), \"BBB\", true, false)\n\tboards2.CreateBoard(cross(cur), \"CCC\", true, false)\n\tboards2.CreateBoard(cross(cur), \"DDD\", false, false) // \u003c-- Unlisted board\n}\n\nfunc main() {\n\tprintln(boards2.Render(\"\"))\n}\n\n// Output:\n// # Boards\n// [Create Board](/r/gnoland/boards2/v1:create-board) • [List Admin Users](/r/gnoland/boards2/v1:admin-users) • [Help](/r/gnoland/boards2/v1:help)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:?order=desc)\n//\n// ###### [AAA](/r/gnoland/boards2/v1:AAA)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// **1 threads**\n//\n// ###### [BBB](/r/gnoland/boards2/v1:BBB)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #2\n// **0 threads**\n//\n// ###### [CCC](/r/gnoland/boards2/v1:CCC)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #3\n// **0 threads**\n"},{"name":"z_ui_home_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_home_01_filetest\n\n// Render boards sorted from newest to oldest.\npackage z_ui_home_01_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.CreateBoard(cross(cur), \"AAA\", true, false)\n\tboards2.CreateBoard(cross(cur), \"BBB\", true, false)\n\tboards2.CreateBoard(cross(cur), \"CCC\", true, false)\n}\n\nfunc main() {\n\tprintln(boards2.Render(\"?order=desc\"))\n}\n\n// Output:\n// # Boards\n// [Create Board](/r/gnoland/boards2/v1:create-board) • [List Admin Users](/r/gnoland/boards2/v1:admin-users) • [Help](/r/gnoland/boards2/v1:help)\n//\n// ---\n// Sort by: [oldest first](/r/gnoland/boards2/v1:?order=asc)\n//\n// ###### [CCC](/r/gnoland/boards2/v1:CCC)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #3\n// **0 threads**\n//\n// ###### [BBB](/r/gnoland/boards2/v1:BBB)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #2\n// **0 threads**\n//\n// ###### [AAA](/r/gnoland/boards2/v1:AAA)\n// Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC, #1\n// **0 threads**\n"},{"name":"z_ui_home_02_filetest.gno","body":"// Render default realm view when there are no boards.\npackage main\n\nimport (\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nfunc main() {\n\tprintln(boards2.Render(\"\"))\n}\n\n// Output:\n// # Boards\n// [Create Board](/r/gnoland/boards2/v1:create-board) • [List Admin Users](/r/gnoland/boards2/v1:admin-users) • [Help](/r/gnoland/boards2/v1:help)\n//\n// ---\n// ### Currently there are no boards\n// Be the first to [create a new board](/r/gnoland/boards2/v1:create-board)!\n"},{"name":"z_ui_reply_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_reply_00_filetest\n\n// Render comment/reply view.\npackage z_ui_reply_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar (\n\tthreadID boards.ID\n\treplyID  boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Create two comments and a reply to a comment\n\tboards2.CreateReply(cross(cur), boardID, threadID, 0, \"First comment\")\n\n\treplyID = boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Second comment\")\n\tboards2.CreateReply(cross(cur), boardID, threadID, replyID, \"Third comment\")\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String() + \"/\" + replyID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [2] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1) • [Show all Replies](/r/gnoland/boards2/v1:test-board/1)\n//\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#3](/r/gnoland/boards2/v1:test-board/1/3)\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e Second comment\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n// \u003e\n// \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/3/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/3/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/3/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=3\u0026threadID=1)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:test-board/1/3?order=desc)\n// \u003e\n// \u003e \u003e\n// \u003e \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#4](/r/gnoland/boards2/v1:test-board/1/4)\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e \u003cgno-foreign\u003e\n// \u003e \u003e Third comment\n// \u003e \u003e \u003c/gno-foreign\u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/4/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/4/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/4/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=4\u0026threadID=1)\n"},{"name":"z_ui_reply_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_reply_01_filetest\n\n// Render comment/reply view of a deleted comment.\npackage z_ui_reply_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar (\n\tthreadID boards.ID\n\treplyID  boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Create a comments with a reply\n\treplyID = boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Second comment\")\n\tboards2.CreateReply(cross(cur), boardID, threadID, replyID, \"Third comment\")\n\n\t// Delete the comment\n\tboards2.DeleteReply(cross(cur), boardID, threadID, replyID)\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String() + \"/\" + replyID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1) • [Show all Replies](/r/gnoland/boards2/v1:test-board/1)\n//\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e ⚠ This comment has been deleted\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n// \u003e\n// \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/2/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/2/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/2/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=2\u0026threadID=1)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:test-board/1/2?order=desc)\n// \u003e\n// \u003e \u003e\n// \u003e \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#3](/r/gnoland/boards2/v1:test-board/1/3)\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e \u003cgno-foreign\u003e\n// \u003e \u003e Third comment\n// \u003e \u003e \u003c/gno-foreign\u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/3/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/3/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/3/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=3\u0026threadID=1)\n"},{"name":"z_ui_reply_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_reply_02_filetest\n\n// Render comment/reply view of a flagged comment.\npackage z_ui_reply_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar (\n\tthreadID boards.ID\n\treplyID  boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Create a comments with a reply\n\treplyID = boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Second comment\")\n\tboards2.CreateReply(cross(cur), boardID, threadID, replyID, \"Third comment\")\n\n\t// Flag the comment\n\tboards2.FlagReply(cross(cur), boardID, threadID, replyID, \"Reason\")\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String() + \"/\" + replyID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1) • [Show all Replies](/r/gnoland/boards2/v1:test-board/1)\n//\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e ⚠ Reply is hidden as it has been flagged as [inappropriate](/r/gnoland/boards2/v1:test-board/1/2/flagging-reasons)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:test-board/1/2?order=desc)\n// \u003e\n// \u003e \u003e\n// \u003e \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#3](/r/gnoland/boards2/v1:test-board/1/3)\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e \u003cgno-foreign\u003e\n// \u003e \u003e Third comment\n// \u003e \u003e \u003c/gno-foreign\u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/3/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/3/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/3/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=3\u0026threadID=1)\n"},{"name":"z_ui_reply_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_reply_03_filetest\n\n// Render the \"reply to a comment\" form: the parent-comment preview is now\n// shown inside a labeled \u003cgno-foreign\u003e sandbox (was md.Blockquote, which\n// gave no sandboxing).\npackage z_ui_reply_03_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar (\n\tthreadID boards.ID\n\treplyID  boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\treplyID = boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Parent comment body\")\n}\n\nfunc main() {\n\t// The \".../{reply}/reply\" route renders the parent-preview branch.\n\tpath := boardName + \"/\" + threadID.String() + \"/\" + replyID.String() + \"/reply\"\n\tcontent := boards2.Render(path)\n\tprintln(strings.Contains(content, \"Replying to a comment posted by\") \u0026\u0026\n\t\tstrings.Contains(content, `\u003cgno-foreign label=\"Quoted comment\"\u003e`) \u0026\u0026\n\t\tstrings.Contains(content, \"Parent comment body\"))\n}\n\n// Output:\n// true\n"},{"name":"z_ui_reply_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_reply_04_filetest\n\n// Re-rooting at a depth-2 reply renders its parent as context with a\n// \"Continue this thread →\" drill-up link (the parent is itself a reply with\n// children), instead of a \"View Thread\" jump to the thread root.\npackage z_ui_reply_04_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid := boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid := boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\taid := boards2.CreateReply(cross(cur), bid, tid, 0, \"comment A\") // #2, parent = thread\n\tboards2.CreateReply(cross(cur), bid, tid, aid, \"reply B\")        // #3, parent = A\n}\n\nfunc main(cur realm) {\n\t// Re-root at reply #3 (B); its parent #2 (A) renders as context and, being\n\t// a reply with children, links \"Continue this thread →\" (to A's re-root).\n\tout := boards2.Render(\"test-board/1/3\")\n\tok := strings.Contains(out, \"Continue this thread →\") \u0026\u0026\n\t\tstrings.Contains(out, \"/r/gnoland/boards2/v1:test-board/1/2\") // → A's re-root, not the thread root\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_ui_thread_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_00_filetest\n\n// Render default thread view.\npackage z_ui_thread_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar threadID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Create two comments and a reply to a comment\n\tboards2.CreateReply(cross(cur), boardID, threadID, 0, \"First comment\")\n\n\treplyID := boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Second comment\")\n\tboards2.CreateReply(cross(cur), boardID, threadID, replyID, \"Third comment\")\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [2] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n//\n// ---\n// Sort by: [newest first](/r/gnoland/boards2/v1:test-board/1?order=desc)\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e First comment\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n// \u003e\n// \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/2/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/2/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/2/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=2\u0026threadID=1)\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#3](/r/gnoland/boards2/v1:test-board/1/3)\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e Second comment\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n// \u003e\n// \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/3/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/3/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/3/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=3\u0026threadID=1)\n// \u003e\n// \u003e \u003e\n// \u003e \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#4](/r/gnoland/boards2/v1:test-board/1/4)\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e \u003cgno-foreign\u003e\n// \u003e \u003e Third comment\n// \u003e \u003e \u003c/gno-foreign\u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/4/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/4/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/4/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=4\u0026threadID=1)\n"},{"name":"z_ui_thread_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_01_filetest\n\n// Render thread sorting comments from newest to oldest.\npackage z_ui_thread_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar threadID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Create two comments and a reply to a comment\n\tboards2.CreateReply(cross(cur), boardID, threadID, 0, \"First comment\")\n\n\treplyID := boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Second comment\")\n\tboards2.CreateReply(cross(cur), boardID, threadID, replyID, \"Third comment\")\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String() + \"?order=desc\"\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) [2] • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n//\n// ---\n// Sort by: [oldest first](/r/gnoland/boards2/v1:test-board/1?order=asc)\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#3](/r/gnoland/boards2/v1:test-board/1/3)\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e Second comment\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n// \u003e\n// \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/3/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/3/reply) [1] • [Edit](/r/gnoland/boards2/v1:test-board/1/3/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=3\u0026threadID=1)\n// \u003e\n// \u003e \u003e\n// \u003e \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#4](/r/gnoland/boards2/v1:test-board/1/4)\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e \u003cgno-foreign\u003e\n// \u003e \u003e Third comment\n// \u003e \u003e \u003c/gno-foreign\u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e\n// \u003e \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/4/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/4/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/4/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=4\u0026threadID=1)\n//\n// \u003e\n// \u003e **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC [\\#2](/r/gnoland/boards2/v1:test-board/1/2)\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e First comment\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n// \u003e\n// \u003e ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/2/flag) • [Reply](/r/gnoland/boards2/v1:test-board/1/2/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/2/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteReply\u0026boardID=1\u0026replyID=2\u0026threadID=1)\n"},{"name":"z_ui_thread_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_02_filetest\n\n// Render both original thread and thread repost view.\npackage z_ui_thread_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner        address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tsrcBoardName         = \"test-board\"\n\tdstBoardName         = \"test-board-2\"\n)\n\nvar (\n\tthreadID       boards.ID\n\trepostThreadID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), srcBoardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Repost thread into a different board\n\tdstBoardID := boards2.CreateBoard(cross(cur), dstBoardName, false, false)\n\trepostThreadID = boards2.CreateRepost(cross(cur), boardID, threadID, dstBoardID, \"Bar\", \"Body2\")\n}\n\nfunc main() {\n\tpath := srcBoardName + \"/\" + threadID.String()\n\tprintln(boards2.Render(path))\n\n\tprintln(\"\u003e\u003e\u003e\u003c\u003c\u003c\\n\")\n\n\tpath = dstBoardName + \"/\" + repostThreadID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) [1] • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n//\n// \u003e\u003e\u003e\u003c\u003c\u003c\n//\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board\\-2](/r/gnoland/boards2/v1:test-board-2)\n// ## Bar\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n// \u003e [!INFO]- Thread Repost\n// \u003e Original thread is [Foo](/r/gnoland/boards2/v1:test-board/1)\n// \u003e Created by [g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh) on 2009-02-13 11:31pm UTC\n//\n//\n//\n// \u003cgno-foreign\u003e\n// Body2\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// \u003e\n// \u003e\n// \u003e \u003cgno-foreign\u003e\n// \u003e Body\n// \u003e \u003c/gno-foreign\u003e\n// \u003e\n// \u003e\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board-2/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board-2/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board-2/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board-2/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=2\u0026threadID=1)\n"},{"name":"z_ui_thread_03_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_03_filetest\n\n// Render thread from a readonly board.\n// Rendered thread action links should be limited to readonly actions.\npackage z_ui_thread_03_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar threadID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a readonly board and then add a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\tboards2.FreezeBoard(cross(cur), boardID)\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## Foo\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost)\n"},{"name":"z_ui_thread_04_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_04_filetest\n\n// Render thread repost of a deleted thread.\npackage z_ui_thread_04_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner        address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tsrcBoardName         = \"test-board\"\n\tdstBoardName         = \"test-board-2\"\n)\n\nvar repostThreadID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), srcBoardName, false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Repost thread into a different board\n\tdstBoardID := boards2.CreateBoard(cross(cur), dstBoardName, false, false)\n\trepostThreadID = boards2.CreateRepost(cross(cur), boardID, threadID, dstBoardID, \"Bar\", \"Body2\")\n\n\t// Remove the original thread\n\tboards2.DeleteThread(cross(cur), boardID, threadID)\n}\n\nfunc main() {\n\tpath := dstBoardName + \"/\" + repostThreadID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board\\-2](/r/gnoland/boards2/v1:test-board-2)\n// ## Bar\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body2\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// \u003e ⚠ Source post has been deleted\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board-2/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board-2/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board-2/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board-2/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=2\u0026threadID=1)\n"},{"name":"z_ui_thread_05_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_05_filetest\n\n// Render thread which has been flagged.\npackage z_ui_thread_05_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"BoardName\"\n)\n\nvar threadID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a readonly board and then add a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Flag the thread\n\tboards2.SetFlaggingThreshold(cross(cur), boardID, 1)\n\tboards2.FlagThread(cross(cur), boardID, threadID, \"Reason\")\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// ⚠ Thread has been flagged as [inappropriate](/r/gnoland/boards2/v1:BoardName/1/flagging-reasons)\n"},{"name":"z_ui_thread_06_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_06_filetest\n\n// Render thread repost of a flagged thread.\npackage z_ui_thread_06_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner        address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tsrcBoardName         = \"test-board\"\n\tdstBoardName         = \"test-board-2\"\n)\n\nvar repostThreadID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), srcBoardName, false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Foo\", \"Body\")\n\n\t// Repost thread into a different board\n\tdstBoardID := boards2.CreateBoard(cross(cur), dstBoardName, false, false)\n\trepostThreadID = boards2.CreateRepost(cross(cur), boardID, threadID, dstBoardID, \"Bar\", \"Body2\")\n\n\t// Flag original thread\n\tboards2.SetFlaggingThreshold(cross(cur), boardID, 1)\n\tboards2.FlagThread(cross(cur), boardID, threadID, \"Reason\")\n}\n\nfunc main() {\n\tpath := dstBoardName + \"/\" + repostThreadID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board\\-2](/r/gnoland/boards2/v1:test-board-2)\n// ## Bar\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body2\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// \u003e ⚠ Source post has been flagged as inappropriate\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board-2/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board-2/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board-2/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board-2/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=2\u0026threadID=1)\n"},{"name":"z_ui_thread_07_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_07_filetest\n\n// Render thread with a title that contains Markdown\npackage z_ui_thread_07_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tboardName         = \"test-board\"\n)\n\nvar threadID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\t// Create a board and a thread\n\tboardID := boards2.CreateBoard(cross(cur), boardName, false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"[Foo](https://foo.com)\", \"Body\")\n}\n\nfunc main() {\n\tpath := boardName + \"/\" + threadID.String()\n\tprintln(boards2.Render(path))\n}\n\n// Output:\n// # [Boards](/r/gnoland/boards2/v1) › [test\\-board](/r/gnoland/boards2/v1:test-board)\n// ## \\[Foo\\]\\(https://foo\\.com\\)\n//\n// **[g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh](/u/g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh)** `owner` · 2009-02-13 11:31pm UTC\n//\n//\n// \u003cgno-foreign\u003e\n// Body\n// \u003c/gno-foreign\u003e\n//\n//\n//\n// ↳ [Flag](/r/gnoland/boards2/v1:test-board/1/flag) • [Repost](/r/gnoland/boards2/v1:test-board/1/repost) • [Comment](/r/gnoland/boards2/v1:test-board/1/reply) • [Edit](/r/gnoland/boards2/v1:test-board/1/edit) • [Delete](/r/gnoland/boards2/v1$help\u0026func=DeleteThread\u0026boardID=1\u0026threadID=1)\n"},{"name":"z_ui_thread_08_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_08_filetest\n\n// A thread large enough to exceed the foreign-block render budget\n// degrades gracefully: rendering stops before gnoweb's per-render\n// foreign-block cap would blank comments, and a \"More replies — view\n// full thread\" notice is shown instead (see maxRenderedBodies). The\n// reply count and assertion are derived from MaxBlocksPerRender so this\n// stays a truncation test if the cap changes.\npackage z_ui_thread_08_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/markdown/foreign/v0\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\n\t// One top-level comment with many direct sub-replies (sub-replies\n\t// are not paginated, so they consume the shared render budget).\n\t// Create MaxBlocksPerRender of them — strictly more than the budget\n\t// (cap minus a margin) — so rendering must truncate.\n\trid := boards2.CreateReply(cross(cur), bid, tid, 0, \"top comment\")\n\tfor i := 0; i \u003c foreign.MaxBlocksPerRender(); i++ {\n\t\tboards2.CreateReply(cross(cur), bid, tid, rid, \"sub reply\")\n\t}\n}\n\nfunc main(cur realm) {\n\tcontent := boards2.Render(\"test-board/1\")\n\tn := strings.Count(content, \"\u003cgno-foreign\u003e\")\n\t// The top comment's many sub-replies are capped inline at pageSizeReplies\n\t// (so n stays well under the foreign-block budget); the rest are reachable\n\t// via the comment's paginated re-rooted view (\"View all N replies\").\n\tcapped := n \u003e 0 \u0026\u0026 n \u003c foreign.MaxBlocksPerRender()/4\n\tprintln(capped \u0026\u0026 strings.Contains(content, \"View all 256 replies\"))\n}\n\n// Output:\n// true\n"},{"name":"z_ui_thread_09_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_09_filetest\n\n// The flat \"all comments\" view (?flat=1) paginates ThreadMeta.AllReplies so\n// every comment is reachable, unlike the recursive threaded view which\n// truncates a large thread. Each flat page renders one \u003cgno-foreign\u003e block\n// per comment, bounded by pageSizeFlat (50). With 60 comments the flat view\n// spans 2 pages; the last comment is reachable via newest-first (desc page 1)\n// and via page 2.\npackage z_ui_thread_09_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\t// pageSizeFlat is 50, so 60 comments span 2 flat pages.\n\tfor i := 0; i \u003c 60; i++ {\n\t\tboards2.CreateReply(cross(cur), bid, tid, 0, ufmt.Sprintf(\"comment-%d\", i))\n\t}\n}\n\nfunc main(cur realm) {\n\tp1 := boards2.Render(\"test-board/1?flat=1\")              // oldest first, page 1\n\tdesc := boards2.Render(\"test-board/1?flat=1\u0026order=desc\") // newest first, page 1\n\tp2 := boards2.Render(\"test-board/1?flat=1\u0026page=2\")       // oldest first, page 2\n\n\tok := strings.Contains(p1, \"All 60 comments\") \u0026\u0026\n\t\tstrings.Contains(p1, \"page 1 of 2\") \u0026\u0026\n\t\tstrings.Contains(p1, \"comment-0\") \u0026\u0026 // first comment is on page 1\n\t\t!strings.Contains(p1, \"comment-59\") \u0026\u0026 // last comment is NOT on oldest-first page 1\n\t\tstrings.Contains(desc, \"comment-59\") \u0026\u0026 // last comment reachable newest-first\n\t\tstrings.Contains(p2, \"comment-59\") \u0026\u0026 // and on the last page\n\t\tstrings.Contains(p2, \"page 2 of 2\")\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_ui_thread_10_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_10_filetest\n\n// No post or comment renders more than pageSizeReplies (10) direct children\n// inline. A comment with more shows the cap inline plus a \"View all N replies\"\n// link to its re-rooted view, which paginates the children on its own ?page=\n// (a distinct path from the thread, so no pager collision).\npackage z_ui_thread_10_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid := boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid := boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\tcid := boards2.CreateReply(cross(cur), bid, tid, 0, \"parent comment\") // reply #2\n\t// 12 direct children under the comment — past the inline cap of 10.\n\tfor i := 0; i \u003c 12; i++ {\n\t\tboards2.CreateReply(cross(cur), bid, tid, cid, ufmt.Sprintf(\"child-%d\", i))\n\t}\n}\n\nfunc main(cur realm) {\n\tthread := boards2.Render(\"test-board/1\")           // threaded view\n\treroot := boards2.Render(\"test-board/1/2\")         // re-root at the comment, page 1\n\treroot2 := boards2.Render(\"test-board/1/2?page=2\") // re-root, page 2\n\tthreadDesc := boards2.Render(\"test-board/1?order=desc\")\n\n\tok := strings.Contains(thread, \"View all 12 replies\") \u0026\u0026 // breadth cap inline + link\n\t\tstrings.Contains(reroot, \"page 1 of 2\") \u0026\u0026 // re-root paginates the children\n\t\tstrings.Contains(reroot2, \"page 2 of 2\") \u0026\u0026 // and advances to the tail\n\t\t// In a desc thread the \"View all\" link carries the order so the\n\t\t// re-root opens newest-first too; in the default (asc) view it doesn't.\n\t\tstrings.Contains(threadDesc, \"/r/gnoland/boards2/v1:test-board/1/2?order=desc\") \u0026\u0026\n\t\t!strings.Contains(thread, \"test-board/1/2?order=desc\")\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_ui_thread_11_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_11_filetest\n\n// A comment's inline children follow the page's ?order: newest-first when the\n// thread is sorted desc, oldest-first otherwise — not always oldest-first.\npackage z_ui_thread_11_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid := boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid := boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\tcid := boards2.CreateReply(cross(cur), bid, tid, 0, \"parent\") // #2\n\tfor i := 0; i \u003c 3; i++ {\n\t\tboards2.CreateReply(cross(cur), bid, tid, cid, ufmt.Sprintf(\"child-%d\", i)) // #3,#4,#5\n\t}\n}\n\nfunc main(cur realm) {\n\tdesc := boards2.Render(\"test-board/1?order=desc\")\n\tasc := boards2.Render(\"test-board/1\") // default = oldest first\n\n\t// desc view: the comment's children render newest-first (child-2 before child-0).\n\tdescOK := strings.Index(desc, \"child-2\") \u003c strings.Index(desc, \"child-0\")\n\t// asc view: oldest-first (child-0 before child-2).\n\tascOK := strings.Index(asc, \"child-0\") \u003c strings.Index(asc, \"child-2\")\n\tprintln(descOK \u0026\u0026 ascOK)\n}\n\n// Output:\n// true\n"},{"name":"z_ui_thread_12_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_12_filetest\n\n// When the render budget is exhausted mid-page in the threaded view, the\n// offset page-picker would offer a \"next page\" that jumps past the replies\n// that were skipped when the budget ran out. Instead the picker is suppressed\n// and the reader is funneled to the complete, reachable flat view.\npackage z_ui_thread_12_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid boards.ID\n\ttid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\n\t// First top-level reply carries a deep chain that alone exhausts the\n\t// render budget (maxRenderedBodies, 246) on page 1.\n\tprev := boards2.CreateReply(cross(cur), bid, tid, 0, \"top-deep\")\n\tfor i := 0; i \u003c 250; i++ {\n\t\tprev = boards2.CreateReply(cross(cur), bid, tid, prev, \"chain\")\n\t}\n\t// Plus enough more top-level replies that the thread spans 2 pages, so a\n\t// picker WOULD render absent the suppression.\n\tfor i := 0; i \u003c 12; i++ {\n\t\tboards2.CreateReply(cross(cur), bid, tid, 0, \"top-more\")\n\t}\n}\n\nfunc main(cur realm) {\n\tout := boards2.Render(\"test-board/1\")\n\t// Budget truncated page 1: the flat \"view all comments\" link is shown,\n\t// and the content-skipping page picker (\"page 1 of N\") is suppressed.\n\tok := strings.Contains(out, \"view all comments\") \u0026\u0026\n\t\t!strings.Contains(out, \"page 1 of\")\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_ui_thread_13_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_13_filetest\n\n// A non-numeric thread/reply ID segment in the URL is echoed back in the error\n// message; it must be escaped so a crafted URL can't inject markup/HTML into\n// the rendered page (a reflected-injection vector, live under gnoweb -html).\npackage z_ui_thread_13_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.CreateBoard(cross(cur), \"test-board\", false, false)\n}\n\nfunc main(cur realm) {\n\t// board exists, so the route reaches renderThread; \"9\u003cx\" fails Atoi and is\n\t// echoed — escaped, so the raw \"9\u003cx\" never appears (md.EscapeText → \"9\\\u003cx\").\n\tout := boards2.Render(\"test-board/9\u003cx\")\n\tok := strings.Contains(out, \"Invalid thread ID\") \u0026\u0026\n\t\t!strings.Contains(out, \"9\u003cx\")\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_ui_thread_14_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_ui_thread_14_filetest\n\n// In a frozen thread, a reply must not show Reply/Edit/Delete action links\n// (the backend rejects them as the thread is frozen); only Flag remains —\n// matching how the OP and a readonly board already behave.\npackage z_ui_thread_14_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar (\n\tbid      boards.ID\n\trid, tid boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test-board\", false, false)\n\ttid = boards2.CreateThread(cross(cur), bid, \"Foo\", \"OP body\")\n\trid = boards2.CreateReply(cross(cur), bid, tid, 0, \"a reply\") // #2\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboards2.FreezeThread(cross(cur), bid, tid)\n\n\tout := boards2.Render(\"test-board/1\")\n\t// Reply #2 keeps Flag but loses Reply/Edit/Delete in a frozen thread.\n\tok := strings.Contains(out, \"test-board/1/2/flag\") \u0026\u0026\n\t\t!strings.Contains(out, \"test-board/1/2/edit\") \u0026\u0026\n\t\t!strings.Contains(out, \"test-board/1/2/reply\")\n\tprintln(ok)\n}\n\n// Output:\n// true\n"},{"name":"z_unban_00_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_unban_00_filetest\n\npackage z_unban_00_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tboards2.Ban(cross(cur), bid, user, boards2.BanDay, \"Unpolite behavior\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.Unban(cross(cur), bid, user, \"\")\n\n\tprintln(boards2.IsBanned(bid, user))\n}\n\n// Output:\n// false\n"},{"name":"z_unban_01_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_unban_01_filetest\n\npackage z_unban_01_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst owner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tboards2.Unban(cross(cur), bid, \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\", \"\")\n}\n\n// Error:\n// user is not banned\n"},{"name":"z_unban_02_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/filetests/z_unban_02_filetest\n\npackage z_unban_02_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\nconst (\n\towner address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tuser  address = \"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"\n)\n\nvar bid boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tbid = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tboards2.Ban(cross(cur), bid, user, boards2.BanDay, \"Unpolite behavior\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"))\n\n\t// Try to unban without unbanning permissions\n\tboards2.Unban(cross(cur), bid, user, \"\")\n}\n\n// Error:\n// unauthorized, user g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj doesn't have the required permission\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"wCxSQg4p8WvTMLYfbxBuBPIKVNnA8d9lqumx8tyXYeBan0uUqOlBa3CYG7fji4QQl2N66GOHVjnll3as2ckOCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"hub","path":"gno.land/r/gnoland/boards2/v1/hub","files":[{"name":"board.gno","body":"package hub\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// Member defines a type for board members.\ntype Member struct {\n\tAddress address\n\tRoles   []string\n}\n\n// Board defines a safe type for boards.\ntype Board struct {\n\tref *boards.Board\n\n\t// ID is the unique identifier of the board.\n\tID uint64\n\n\t// Name is the current name of the board.\n\tName string\n\n\t// Aliases contains a list of alternative names for the board.\n\tAliases []string\n\n\t// Readonly indicates that the board is readonly.\n\tReadonly bool\n\n\t// ThreadsCount contains the number of threads within the board.\n\tThreadCount int\n\n\t// MemberCount contains the number of members of the board.\n\tMemberCount int\n\n\t// Creator is the account address that created the board.\n\tCreator address\n\n\t// CreatedAt is the board's creation time as Unix time.\n\tCreatedAt int64\n\n\t// UpdatedAt is the board's update time as Unix time.\n\tUpdatedAt int64\n}\n\n// IterateThreads iterates board threads by creation time.\n// To reverse iterate use a negative count.\nfunc (b Board) IterateThreads(start, count int, fn func(Thread) bool) bool {\n\treturn b.ref.Threads.Iterate(start, count, func(thread *boards.Post) bool {\n\t\treturn fn(NewSafeThread(thread))\n\t})\n}\n\n// IterateMembers iterates board members.\n// To reverse iterate use a negative count.\nfunc (b Board) IterateMembers(start, count int, fn func(boards.User) bool) bool {\n\treturn b.ref.Permissions.IterateUsers(start, count, fn)\n}\n\n// NewSafeBoard creates a safe board.\nfunc NewSafeBoard(ref *boards.Board) Board {\n\tvar usersCount int\n\tif ref.Permissions != nil {\n\t\tusersCount = ref.Permissions.UsersCount()\n\t}\n\n\tvar threadCount int\n\tif ref.Threads != nil {\n\t\tthreadCount = ref.Threads.Size()\n\t}\n\n\treturn Board{\n\t\tref:         ref,\n\t\tID:          uint64(ref.ID),\n\t\tName:        ref.Name,\n\t\tAliases:     append([]string(nil), ref.Aliases...),\n\t\tReadonly:    ref.Readonly,\n\t\tThreadCount: threadCount,\n\t\tMemberCount: usersCount,\n\t\tCreator:     ref.Creator,\n\t\tCreatedAt:   timeToUnix(ref.CreatedAt),\n\t\tUpdatedAt:   timeToUnix(ref.UpdatedAt),\n\t}\n}\n"},{"name":"board_test.gno","body":"package hub_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/gnoland/boards/exts/permissions\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nfunc TestBoardIterateThreads(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func(*boards.Board)\n\t}{\n\t\t{\n\t\t\tname: \"no threads\",\n\t\t},\n\t\t{\n\t\t\tname: \"one thread\",\n\t\t\tsetup: func(b *boards.Board) {\n\t\t\t\tt := boards.MustNewThread(b, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Title 1\", \"Body 1\")\n\t\t\t\tb.Threads.Add(t)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple threads\",\n\t\t\tsetup: func(b *boards.Board) {\n\t\t\t\tthreads := []*boards.Post{\n\t\t\t\t\tboards.MustNewThread(b, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Title 1\", \"Body 1\"),\n\t\t\t\t\tboards.MustNewThread(b, \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\", \"Title 2\", \"Body 2\"),\n\t\t\t\t\tboards.MustNewThread(b, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", \"Title 3\", \"Body 3\"),\n\t\t\t\t}\n\t\t\t\tfor _, t := range threads {\n\t\t\t\t\tb.Threads.Add(t)\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tref := boards.New(1)\n\t\t\tif tt.setup != nil {\n\t\t\t\ttt.setup(ref)\n\t\t\t}\n\n\t\t\tboard := hub.NewSafeBoard(ref)\n\n\t\t\turequire.Equal(t, ref.Threads.Size(), board.ThreadCount, \"expect number of threads to match\")\n\t\t\tboard.IterateThreads(0, board.ThreadCount, func(thread hub.Thread) bool {\n\t\t\t\tid := boards.ID(thread.ID)\n\t\t\t\texpected, found := ref.Threads.Get(id)\n\n\t\t\t\turequire.True(t, found, \"expect thread to be found\")\n\t\t\t\turequire.Equal(t, expected.Creator, thread.Creator, \"expect creator to match\")\n\t\t\t\turequire.Equal(t, expected.Title, thread.Title, \"expect title to match\")\n\t\t\t\turequire.Equal(t, expected.Body, thread.Body, \"expect body to match\")\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestBoardIterateMembers(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tusers []boards.User\n\t}{\n\t\t{\n\t\t\tname: \"no members\",\n\t\t},\n\t\t{\n\t\t\tname: \"one member\",\n\t\t\tusers: []boards.User{\n\t\t\t\t{Address: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple members\",\n\t\t\tusers: []boards.User{\n\t\t\t\t{Address: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t\t\t{Address: \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"},\n\t\t\t\t{Address: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar i int\n\t\t\tperms := permissions.New()\n\t\t\tfor _, u := range tt.users {\n\t\t\t\tperms.SetUserRoles(u.Address)\n\t\t\t}\n\n\t\t\tref := boards.New(1)\n\t\t\tref.Permissions = perms\n\t\t\tboard := hub.NewSafeBoard(ref)\n\n\t\t\turequire.Equal(t, len(tt.users), board.MemberCount, \"expect number of members to match\")\n\t\t\tboard.IterateMembers(0, board.MemberCount, func(user boards.User) bool {\n\t\t\t\turequire.Equal(t, tt.users[i].Address, user.Address, \"expect address to match\")\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"},{"name":"comment.gno","body":"package hub\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// Comment defines a type for threads comment/replies.\ntype Comment struct {\n\tref *boards.Post\n\n\t// ID is the unique identifier of the comment.\n\tID uint64\n\n\t// BoardID is the board ID where comment is created.\n\tBoardID uint64\n\n\t// ThreadID contains is the ID of the thread where comment is created.\n\tThreadID uint64\n\n\t// ParentID is the ID of the parent comment or reply.\n\tParentID uint64\n\n\t// Body contains the comment's content.\n\tBody string\n\n\t// Hidden indicates that comment is hidden.\n\tHidden bool\n\n\t// ReplyCount contains the number of comments replies.\n\t// Count only includes top level replies, sub-replies are not included.\n\tReplyCount int\n\n\t// FlagCount contains the number of flags that comment has.\n\tFlagCount int\n\n\t// Creator is the account address that created the comment or reply.\n\tCreator address\n\n\t// CreatedAt is thread's creation time as Unix time.\n\tCreatedAt int64\n\n\t// UpdatedAt is thread's update time as Unix time.\n\tUpdatedAt int64\n}\n\n// IterateFlags iterates comment moderation flags.\n// To reverse iterate use a negative count.\nfunc (c Comment) IterateFlags(start, count int, fn func(boards.Flag) bool) bool {\n\treturn c.ref.Flags.Iterate(start, count, fn)\n}\n\n// IterateReplies iterates comment replies (sub-comments).\n// To reverse iterate use a negative count.\nfunc (t Comment) IterateReplies(start, count int, fn func(Comment) bool) bool {\n\treturn t.ref.Replies.Iterate(start, count, func(comment *boards.Post) bool {\n\t\treturn fn(NewSafeComment(comment))\n\t})\n}\n\n// NewSafeComment creates a safe comment.\nfunc NewSafeComment(ref *boards.Post) Comment {\n\tif boards.IsThread(ref) {\n\t\tpanic(\"post is not a comment or reply\")\n\t}\n\n\tvar replyCount int\n\tif ref.Replies != nil {\n\t\treplyCount = ref.Replies.Size()\n\t}\n\n\tvar flagCount int\n\tif ref.Flags != nil {\n\t\tflagCount = ref.Flags.Size()\n\t}\n\n\treturn Comment{\n\t\tref:        ref,\n\t\tID:         uint64(ref.ID),\n\t\tBoardID:    uint64(ref.Board.ID),\n\t\tThreadID:   uint64(ref.ThreadID),\n\t\tParentID:   uint64(ref.ParentID),\n\t\tBody:       ref.Body,\n\t\tHidden:     ref.Hidden,\n\t\tReplyCount: replyCount,\n\t\tFlagCount:  flagCount,\n\t\tCreator:    ref.Creator,\n\t\tCreatedAt:  timeToUnix(ref.CreatedAt),\n\t\tUpdatedAt:  timeToUnix(ref.UpdatedAt),\n\t}\n}\n"},{"name":"comment_test.gno","body":"package hub_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nfunc TestCommentIterateFlags(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tflags []boards.Flag\n\t}{\n\t\t{\n\t\t\tname: \"no flags\",\n\t\t},\n\t\t{\n\t\t\tname: \"one flag\",\n\t\t\tflags: []boards.Flag{\n\t\t\t\t{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", Reason: \"Reason 1\"},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple flags\",\n\t\t\tflags: []boards.Flag{\n\t\t\t\t{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", Reason: \"Reason 1\"},\n\t\t\t\t{User: \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\", Reason: \"Reason 2\"},\n\t\t\t\t{User: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", Reason: \"Reason 3\"},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar i int\n\t\t\tref := createComment(t, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Comment\")\n\t\t\tfor _, f := range tt.flags {\n\t\t\t\tref.Flags.Add(f)\n\t\t\t}\n\n\t\t\tcomment := hub.NewSafeComment(ref)\n\n\t\t\turequire.Equal(t, len(tt.flags), comment.FlagCount, \"expect number of flags to match\")\n\t\t\tcomment.IterateFlags(0, comment.FlagCount, func(f boards.Flag) bool {\n\t\t\t\turequire.Equal(t, tt.flags[i].User, f.User, \"expect user to match\")\n\t\t\t\turequire.Equal(t, tt.flags[i].Reason, f.Reason, \"expect reason to match\")\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestCommentIterateReplies(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func(*boards.Post)\n\t}{\n\t\t{\n\t\t\tname: \"no replies\",\n\t\t},\n\t\t{\n\t\t\tname: \"one reply\",\n\t\t\tsetup: func(comment *boards.Post) {\n\t\t\t\tr := boards.MustNewReply(comment, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Body 1\")\n\t\t\t\tcomment.Replies.Add(r)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple replies\",\n\t\t\tsetup: func(comment *boards.Post) {\n\t\t\t\treplies := []*boards.Post{\n\t\t\t\t\tboards.MustNewReply(comment, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Body 1\"),\n\t\t\t\t\tboards.MustNewReply(comment, \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\", \"Body 2\"),\n\t\t\t\t\tboards.MustNewReply(comment, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", \"Body 3\"),\n\t\t\t\t}\n\t\t\t\tfor _, t := range replies {\n\t\t\t\t\tcomment.Replies.Add(t)\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tref := createComment(t, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Comment\")\n\t\t\tif tt.setup != nil {\n\t\t\t\ttt.setup(ref)\n\t\t\t}\n\n\t\t\tcomment := hub.NewSafeComment(ref)\n\n\t\t\turequire.Equal(t, ref.Replies.Size(), comment.ReplyCount, \"expect number of replies to match\")\n\t\t\tcomment.IterateReplies(0, comment.ReplyCount, func(reply hub.Comment) bool {\n\t\t\t\tid := boards.ID(reply.ID)\n\t\t\t\texpected, found := ref.Replies.Get(id)\n\n\t\t\t\turequire.True(t, found, \"expect reply to be found\")\n\t\t\t\turequire.Equal(t, expected.Creator, reply.Creator, \"expect creator to match\")\n\t\t\t\turequire.Equal(t, expected.Body, reply.Body, \"expect body to match\")\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc createComment(t *testing.T, user address, body string) *boards.Post {\n\tt.Helper()\n\n\tthread := createThread(t, user, \"Title\", \"Body\")\n\treturn boards.MustNewReply(thread, user, body)\n}\n"},{"name":"format.gno","body":"package hub\n\nimport \"time\"\n\n// timeToUnix converts time to Unix epoch.\nfunc timeToUnix(t time.Time) int64 {\n\tif t.IsZero() {\n\t\treturn 0\n\t}\n\treturn t.Unix()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/boards2/v1/hub\"\ngno = \"0.9\"\n"},{"name":"hub.gno","body":"// Package hub exposes safe, read-only views over boards2's persistent\n// state. The Get* functions are crossing functions: callers must invoke\n// them with `cross` (e.g. `hub.GetBoard(cross, id)`). The crossing into\n// hub gives each call a live `cur` whose `Previous()` is the caller's\n// realm — that's what boards2's protected entry points use for the\n// namespace check.\n//\n// Note for future maintainers: inside a Get* body, `cur.Previous()` is\n// the *immediate* caller's realm, which may be an intermediary, not\n// necessarily the end user. Do not graft user-identity gating onto\n// these reads.\npackage hub\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n)\n\n// GetBoard returns a safe board.\nfunc GetBoard(cur realm, id uint64) (Board, bool) {\n\tb, found := getBoard(0, cur, id)\n\tif !found {\n\t\treturn Board{}, false\n\t}\n\treturn NewSafeBoard(b), true\n}\n\n// GetThread returns a safe board thread.\nfunc GetThread(cur realm, boardID, threadID uint64) (Thread, bool) {\n\tt, found := getThread(0, cur, boardID, threadID)\n\tif !found {\n\t\treturn Thread{}, false\n\t}\n\treturn NewSafeThread(t), true\n}\n\n// GetComment returns a safe thread comment.\nfunc GetComment(cur realm, boardID, threadID, commentID uint64) (Comment, bool) {\n\tc, found := getComment(0, cur, boardID, threadID, commentID)\n\tif !found {\n\t\treturn Comment{}, false\n\t}\n\treturn NewSafeComment(c), true\n}\n\n// GetBoards returns a list with all boards.\n// To reverse iterate use a negative count.\nfunc GetBoards(cur realm, start, count int) []Board {\n\tvar boards_ []Board\n\tboards2.Iterate(cross(cur), start, count, func(b *boards.Board) bool {\n\t\tboards_ = append(boards_, NewSafeBoard(b))\n\t\treturn false\n\t})\n\treturn boards_\n}\n\n// GetThreads returns a list with threads of a board.\n// To reverse iterate use a negative count.\nfunc GetThreads(cur realm, boardID uint64, start, count int) []Thread {\n\tb, found := getBoard(0, cur, boardID)\n\tif !found {\n\t\treturn nil\n\t}\n\n\tvar threads []Thread\n\tb.Threads.Iterate(start, count, func(thread *boards.Post) bool {\n\t\tthreads = append(threads, NewSafeThread(thread))\n\t\treturn false\n\t})\n\treturn threads\n}\n\n// GetMembers returns a list with the members of a board.\n// To reverse iterate use a negative count.\nfunc GetMembers(cur realm, boardID uint64, start, count int) []boards.User {\n\tb, found := getBoard(0, cur, boardID)\n\tif !found {\n\t\treturn nil\n\t}\n\n\tvar members []boards.User\n\tb.Permissions.IterateUsers(start, count, func(u boards.User) bool {\n\t\tmembers = append(members, u)\n\t\treturn false\n\t})\n\treturn members\n}\n\n// GetReposts returns a list with repost of a board thread.\n// To reverse iterate use a negative count.\nfunc GetReposts(cur realm, boardID, threadID uint64, start, count int) []Thread {\n\tt, found := getThread(0, cur, boardID, threadID)\n\tif !found {\n\t\treturn nil\n\t}\n\n\tvar reposts []Thread\n\tt.Reposts.Iterate(start, count, func(rBoardID, rRepostID boards.ID) bool {\n\t\tr, found := getThread(0, cur, uint64(rBoardID), uint64(rRepostID))\n\t\tif found {\n\t\t\treposts = append(reposts, NewSafeThread(r))\n\t\t}\n\t\treturn false\n\t})\n\treturn reposts\n}\n\n// GetFlag returns a list with thread or comment moderation flags.\n// To reverse iterate use a negative count.\n// Thread flags are returned when `commentID` is zero, or comment flags are returned otherwise.\nfunc GetFlags(cur realm, boardID, threadID, commentID uint64, start, count int) []boards.Flag {\n\tvar storage boards.FlagStorage\n\tif commentID == 0 {\n\t\tt, found := getThread(0, cur, boardID, threadID)\n\t\tif !found {\n\t\t\treturn nil\n\t\t}\n\n\t\tstorage = t.Flags\n\t} else {\n\t\tc, found := getComment(0, cur, boardID, threadID, commentID)\n\t\tif !found {\n\t\t\treturn nil\n\t\t}\n\n\t\tstorage = c.Flags\n\t}\n\n\tvar flags []boards.Flag\n\tstorage.Iterate(start, count, func(f boards.Flag) bool {\n\t\tflags = append(flags, f)\n\t\treturn false\n\t})\n\treturn flags\n}\n\n// GetComments returns a list with all thread comments and replies.\n// To reverse iterate use a negative count.\n// Top level comments can be filtered by checking `Comment.ParentID`, replies\n// always have a parent comment or reply, while comments have no parent.\nfunc GetComments(cur realm, boardID, threadID uint64, start, count int) []Comment {\n\tt, found := getThread(0, cur, boardID, threadID)\n\tif !found {\n\t\treturn nil\n\t}\n\n\tvar comments []Comment\n\tt.Replies.Iterate(start, count, func(comment *boards.Post) bool {\n\t\tcomments = append(comments, NewSafeComment(comment))\n\t\treturn false\n\t})\n\treturn comments\n}\n\n// GetReplies returns a list with top level comment replies.\n// To reverse iterate use a negative count.\nfunc GetReplies(cur realm, boardID, threadID, commentID uint64, start, count int) []Comment {\n\tc, found := getComment(0, cur, boardID, threadID, commentID)\n\tif !found {\n\t\treturn nil\n\t}\n\n\tvar replies []Comment\n\tc.Replies.Iterate(start, count, func(comment *boards.Post) bool {\n\t\treplies = append(replies, NewSafeComment(comment))\n\t\treturn false\n\t})\n\treturn replies\n}\n\n// getBoard fetches a raw *boards.Board via boards2's protected\n// GetBoard. Non-crossing helper: rlm is hub's own cur, threaded\n// through from the enclosing crossing function.\nfunc getBoard(_ int, rlm realm, boardID uint64) (*boards.Board, bool) {\n\treturn boards2.GetBoard(cross(rlm), boards.ID(boardID))\n}\n\nfunc getThread(_ int, rlm realm, boardID, threadID uint64) (*boards.Post, bool) {\n\tb, found := getBoard(0, rlm, boardID)\n\tif !found {\n\t\treturn nil, false\n\t}\n\n\tt, found := b.Threads.Get(boards.ID(threadID))\n\tif !found {\n\t\t// When thread is not found search it within hidden threads\n\t\tmeta := b.Meta.(*boards2.BoardMeta)\n\t\tt, found = meta.HiddenThreads.Get(boards.ID(threadID))\n\t}\n\treturn t, found\n}\n\nfunc getComment(_ int, rlm realm, boardID, threadID, commentID uint64) (*boards.Post, bool) {\n\tt, found := getThread(0, rlm, boardID, threadID)\n\tif !found {\n\t\treturn nil, false\n\t}\n\n\treturn t.Replies.Get(boards.ID(commentID))\n}\n"},{"name":"thread.gno","body":"package hub\n\nimport (\n\t\"gno.land/p/gnoland/boards\"\n)\n\n// Flag defines a type for thread and comment flags.\ntype Flag struct {\n\t// User is the user that flagged.\n\tUser address\n\n\t// Reason is the reason for flagging.\n\tReason string\n}\n\n// Thread defines a type for board threads.\ntype Thread struct {\n\tref *boards.Post\n\n\t// ID is the unique identifier of the thread.\n\tID uint64\n\n\t// OriginalBoardID contains the board ID of the original thread when current is a repost.\n\tOriginalBoardID uint64\n\n\t// OriginalThreadID contains the ID of the original thread when current is a repost.\n\tOriginalThreadID uint64\n\n\t// BoardID is the board ID where thread is created.\n\tBoardID uint64\n\n\t// Title contains thread's title.\n\tTitle string\n\n\t// Body contains content of the thread.\n\tBody string\n\n\t// Hidden indicates that thread is hidden.\n\tHidden bool\n\n\t// Readonly indicates that thread is readonly.\n\tReadonly bool\n\n\t// CommentCount contains the number of thread comments.\n\t// Count only includes top level comment, replies are not included.\n\tCommentCount int\n\n\t// RepostCount contains the number of times thread has been reposted.\n\tRepostCount int\n\n\t// FlagCount contains the number of flags that thread has.\n\tFlagCount int\n\n\t// Creator is the account address that created the thread.\n\tCreator address\n\n\t// CreatedAt is thread's creation time as Unix time.\n\tCreatedAt int64\n\n\t// UpdatedAt is thread's update time as unix time.\n\tUpdatedAt int64\n}\n\n// IterateFlags iterates thread moderation flags.\n// To reverse iterate use a negative count.\nfunc (t Thread) IterateFlags(start, count int, fn func(boards.Flag) bool) bool {\n\treturn t.ref.Flags.Iterate(start, count, fn)\n}\n\n// IterateReposts iterates thread reposts.\n// To reverse iterate use a negative count.\nfunc (t Thread) IterateReposts(start, count int, fn func(boardID, repostThreadID uint64) bool) bool {\n\treturn t.ref.Reposts.Iterate(start, count, func(boardID, repostThreadID boards.ID) bool {\n\t\treturn fn(uint64(boardID), uint64(repostThreadID))\n\t})\n}\n\n// IterateComments iterates thread comments.\n// To reverse iterate use a negative count.\nfunc (t Thread) IterateComments(start, count int, fn func(Comment) bool) bool {\n\treturn t.ref.Replies.Iterate(start, count, func(comment *boards.Post) bool {\n\t\treturn fn(NewSafeComment(comment))\n\t})\n}\n\n// NewSafeThread creates a safe thread.\nfunc NewSafeThread(ref *boards.Post) Thread {\n\tif !boards.IsThread(ref) {\n\t\tpanic(\"post is not a thread\")\n\t}\n\n\treturn Thread{\n\t\tref:              ref,\n\t\tID:               uint64(ref.ID),\n\t\tOriginalBoardID:  uint64(ref.OriginalBoardID),\n\t\tOriginalThreadID: uint64(ref.ParentID),\n\t\tBoardID:          uint64(ref.Board.ID),\n\t\tTitle:            ref.Title,\n\t\tBody:             ref.Body,\n\t\tHidden:           ref.Hidden,\n\t\tReadonly:         ref.Readonly,\n\t\tCommentCount:     ref.Replies.Size(),\n\t\tRepostCount:      ref.Reposts.Size(),\n\t\tFlagCount:        ref.Flags.Size(),\n\t\tCreator:          ref.Creator,\n\t\tCreatedAt:        timeToUnix(ref.CreatedAt),\n\t\tUpdatedAt:        timeToUnix(ref.UpdatedAt),\n\t}\n}\n"},{"name":"thread_test.gno","body":"package hub_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nfunc TestThreadIterateFlags(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tflags []boards.Flag\n\t}{\n\t\t{\n\t\t\tname: \"no flags\",\n\t\t},\n\t\t{\n\t\t\tname: \"one flag\",\n\t\t\tflags: []boards.Flag{\n\t\t\t\t{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", Reason: \"Reason 1\"},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple flags\",\n\t\t\tflags: []boards.Flag{\n\t\t\t\t{User: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", Reason: \"Reason 1\"},\n\t\t\t\t{User: \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\", Reason: \"Reason 2\"},\n\t\t\t\t{User: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", Reason: \"Reason 3\"},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar i int\n\t\t\tref := createComment(t, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Comment\")\n\t\t\tfor _, f := range tt.flags {\n\t\t\t\tref.Flags.Add(f)\n\t\t\t}\n\n\t\t\tcomment := hub.NewSafeComment(ref)\n\n\t\t\turequire.Equal(t, len(tt.flags), comment.FlagCount, \"expect number of flags to match\")\n\t\t\tcomment.IterateFlags(0, comment.FlagCount, func(f boards.Flag) bool {\n\t\t\t\turequire.Equal(t, tt.flags[i].User, f.User, \"expect user to match\")\n\t\t\t\turequire.Equal(t, tt.flags[i].Reason, f.Reason, \"expect reason to match\")\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestThreadIterateReposts(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tsetup    func(*boards.Post)\n\t\texpected [][2]uint64 // {boardID, repostThreadID}\n\t}{\n\t\t{\n\t\t\tname: \"no reposts\",\n\t\t},\n\t\t{\n\t\t\tname: \"one repost\",\n\t\t\tsetup: func(thread *boards.Post) {\n\t\t\t\tr := boards.MustNewRepost(thread, boards.New(1), \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\t\t\t\tthread.Reposts.Add(r)\n\t\t\t},\n\t\t\texpected: [][2]uint64{\n\t\t\t\t{1, 1},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple reposts\",\n\t\t\tsetup: func(thread *boards.Post) {\n\t\t\t\treposts := []*boards.Post{\n\t\t\t\t\tboards.MustNewRepost(thread, boards.New(1), \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\t\t\tboards.MustNewRepost(thread, boards.New(2), \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"),\n\t\t\t\t\tboards.MustNewRepost(thread, boards.New(5), \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"),\n\t\t\t\t}\n\t\t\t\tfor _, r := range reposts {\n\t\t\t\t\tthread.Reposts.Add(r)\n\t\t\t\t}\n\t\t\t},\n\t\t\texpected: [][2]uint64{\n\t\t\t\t{1, 1},\n\t\t\t\t{2, 1},\n\t\t\t\t{5, 1},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar i int\n\t\t\tref := createThread(t, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Title\", \"Body\")\n\t\t\tif tt.setup != nil {\n\t\t\t\ttt.setup(ref)\n\t\t\t}\n\n\t\t\tthread := hub.NewSafeThread(ref)\n\n\t\t\turequire.Equal(t, ref.Reposts.Size(), thread.RepostCount, \"expect number of reposts to match\")\n\t\t\tthread.IterateReposts(0, thread.RepostCount, func(boardID, repostThreadID uint64) bool {\n\t\t\t\turequire.Equal(t, tt.expected[i][0], boardID, \"expect repost board ID to match\")\n\t\t\t\turequire.Equal(t, tt.expected[i][1], repostThreadID, \"expect repost thread ID to match\")\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestThreadIterateComments(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\tsetup func(*boards.Post)\n\t}{\n\t\t{\n\t\t\tname: \"no comments\",\n\t\t},\n\t\t{\n\t\t\tname: \"one comment\",\n\t\t\tsetup: func(thread *boards.Post) {\n\t\t\t\tr := boards.MustNewReply(thread, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Body 1\")\n\t\t\t\tthread.Replies.Add(r)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"multiple comments\",\n\t\t\tsetup: func(thread *boards.Post) {\n\t\t\t\treplies := []*boards.Post{\n\t\t\t\t\tboards.MustNewReply(thread, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Body 1\"),\n\t\t\t\t\tboards.MustNewReply(thread, \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\", \"Body 2\"),\n\t\t\t\t\tboards.MustNewReply(thread, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", \"Body 3\"),\n\t\t\t\t}\n\t\t\t\tfor _, t := range replies {\n\t\t\t\t\tthread.Replies.Add(t)\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tref := createThread(t, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"Title\", \"Body\")\n\t\t\tif tt.setup != nil {\n\t\t\t\ttt.setup(ref)\n\t\t\t}\n\n\t\t\tthread := hub.NewSafeThread(ref)\n\n\t\t\turequire.Equal(t, ref.Replies.Size(), thread.CommentCount, \"expect number of replies to match\")\n\t\t\tthread.IterateComments(0, thread.CommentCount, func(comment hub.Comment) bool {\n\t\t\t\tid := boards.ID(comment.ID)\n\t\t\t\texpected, found := ref.Replies.Get(id)\n\n\t\t\t\turequire.True(t, found, \"expect comment to be found\")\n\t\t\t\turequire.Equal(t, expected.Creator, comment.Creator, \"expect creator to match\")\n\t\t\t\turequire.Equal(t, expected.Body, comment.Body, \"expect body to match\")\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc createThread(t *testing.T, user address, title, body string) *boards.Post {\n\tt.Helper()\n\n\tboard := boards.New(1)\n\treturn boards.MustNewThread(board, user, title, body)\n}\n"},{"name":"z_0_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_0_a_filetest\n\n// Test default board values\npackage z_0_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar boardID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tboard, found := hub.GetBoard(cross(cur), uint64(boardID))\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(board.ID)\n\tprintln(board.Name)\n\tprintln(board.Aliases)\n\tprintln(board.Readonly)\n\tprintln(board.ThreadCount)\n\tprintln(board.MemberCount)\n\tprintln(board.Creator)\n\tprintln(board.CreatedAt)\n\tprintln(board.UpdatedAt)\n}\n\n// Output:\n// 1\n// test123\n// (nil []string)\n// false\n// 0\n// 1\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n// 1234567890\n// 0\n"},{"name":"z_0_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_0_b_filetest\n\n// Test non default board values\npackage z_0_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar boardID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\t// Invite member\n\tboards2.InviteMember(cross(cur), boardID, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", \"admin\")\n\n\t// Rename board\n\tboards2.RenameBoard(cross(cur), \"test123\", \"foo123\")\n\n\t// Create a thread\n\tboards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\n\t// Freeze board\n\tboards2.FreezeBoard(cross(cur), boardID)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tboard, found := hub.GetBoard(cross(cur), uint64(boardID))\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(board.ID)\n\tprintln(board.Name)\n\tprintln(board.Aliases)\n\tprintln(board.Readonly)\n\tprintln(board.ThreadCount)\n\tprintln(board.MemberCount)\n\tprintln(board.Creator)\n\tprintln(board.CreatedAt)\n\tprintln(board.UpdatedAt)\n}\n\n// Output:\n// 1\n// foo123\n// slice[(\"test123\" string)]\n// true\n// 1\n// 2\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n// 1234567890\n// 1234567890\n"},{"name":"z_1_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_1_a_filetest\n\n// Test default thread values\npackage z_1_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar (\n\tboardID  boards.ID\n\tthreadID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"origin123\", false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, found := hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(thread.ID)\n\tprintln(thread.OriginalBoardID)\n\tprintln(thread.OriginalThreadID)\n\tprintln(thread.BoardID)\n\tprintln(thread.Title)\n\tprintln(thread.Body)\n\tprintln(thread.Hidden)\n\tprintln(thread.Readonly)\n\tprintln(thread.CommentCount)\n\tprintln(thread.RepostCount)\n\tprintln(thread.FlagCount)\n\tprintln(thread.Creator)\n\tprintln(thread.CreatedAt)\n\tprintln(thread.UpdatedAt)\n}\n\n// Output:\n// 1\n// 0\n// 0\n// 1\n// Title\n// Body\n// false\n// false\n// 0\n// 0\n// 0\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n// 1234567890\n// 0\n"},{"name":"z_1_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_1_b_filetest\n\n// Test non default thread values\npackage z_1_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar (\n\tboardID  boards.ID\n\tthreadID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\n\t// Create another board and repost thread\n\tdstBoardID := boards2.CreateBoard(cross(cur), \"destination123\", false, false)\n\tboards2.CreateRepost(cross(cur), boardID, threadID, dstBoardID, \"Title\", \"Body\")\n\n\t// Edit thread\n\tboards2.EditThread(cross(cur), boardID, threadID, \"Foo\", \"Bar\")\n\n\t// Add a comment to the thread\n\tboards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n\n\t// Freeze thread\n\tboards2.FreezeThread(cross(cur), boardID, threadID)\n\n\t// Flag thread\n\tboards2.FlagThread(cross(cur), boardID, threadID, \"Reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, found := hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(thread.ID)\n\tprintln(thread.OriginalBoardID)  // Only reposts have an original board ID\n\tprintln(thread.OriginalThreadID) // Only reposts have an original thread ID\n\tprintln(thread.BoardID)\n\tprintln(thread.Title)\n\tprintln(thread.Body)\n\tprintln(thread.Hidden)\n\tprintln(thread.Readonly)\n\tprintln(thread.CommentCount)\n\tprintln(thread.RepostCount)\n\tprintln(thread.FlagCount)\n\tprintln(thread.Creator)\n\tprintln(thread.CreatedAt)\n\tprintln(thread.UpdatedAt)\n}\n\n// Output:\n// 1\n// 0\n// 0\n// 1\n// Foo\n// Bar\n// true\n// true\n// 1\n// 1\n// 1\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n// 1234567890\n// 1234567890\n"},{"name":"z_1_c_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_1_c_filetest\n\n// Test non default reposted thread values\npackage z_1_c_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar (\n\tboardID  boards.ID\n\tthreadID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tsrcBoardID := boards2.CreateBoard(cross(cur), \"origin123\", false, false)\n\n\t// Create two threads where the second is the one to repost\n\tboards2.CreateThread(cross(cur), srcBoardID, \"Title1\", \"Body1\")\n\tsrcThreadID := boards2.CreateThread(cross(cur), srcBoardID, \"Title2\", \"Body2\") // ID = 2\n\n\t// Create repost\n\tboardID = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID = boards2.CreateRepost(cross(cur), srcBoardID, srcThreadID, boardID, \"Title\", \"Body\")\n\n\t// Edit repost\n\tboards2.EditThread(cross(cur), boardID, threadID, \"Foo\", \"Bar\")\n\n\t// Add a comment to the repost\n\tboards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n\n\t// Freeze repost\n\tboards2.FreezeThread(cross(cur), boardID, threadID)\n\n\t// Flag repost\n\tboards2.FlagThread(cross(cur), boardID, threadID, \"Reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, found := hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(thread.ID)\n\tprintln(thread.OriginalBoardID)\n\tprintln(thread.OriginalThreadID)\n\tprintln(thread.BoardID)\n\tprintln(thread.Title)\n\tprintln(thread.Body)\n\tprintln(thread.Hidden)\n\tprintln(thread.Readonly)\n\tprintln(thread.CommentCount)\n\tprintln(thread.RepostCount) // Reposts can't be reposted, so count must be 0\n\tprintln(thread.FlagCount)\n\tprintln(thread.Creator)\n\tprintln(thread.CreatedAt)\n\tprintln(thread.UpdatedAt)\n}\n\n// Output:\n// 1\n// 1\n// 2\n// 2\n// Foo\n// Bar\n// true\n// true\n// 1\n// 0\n// 1\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n// 1234567890\n// 1234567890\n"},{"name":"z_2_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_2_a_filetest\n\n// Test getting boards when no boards exist\npackage z_2_a_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tboards := hub.GetBoards(cross(cur), 0, boards2.BoardCount())\n\tprintln(len(boards))\n}\n\n// Output:\n// 0\n"},{"name":"z_2_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_2_b_filetest\n\n// Test getting boards\npackage z_2_b_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboards2.CreateBoard(cross(cur), \"aaa123\", false, false)\n\tboards2.CreateBoard(cross(cur), \"bbb123\", false, false)\n\tboards2.CreateBoard(cross(cur), \"ccc123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tboards := hub.GetBoards(cross(cur), 0, boards2.BoardCount())\n\n\tfor _, b := range boards {\n\t\tprintln(b.ID, b.Name)\n\t}\n}\n\n// Output:\n// 1 aaa123\n// 2 bbb123\n// 3 ccc123\n"},{"name":"z_3_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_3_a_filetest\n\n// Test getting threads from an empty board\npackage z_3_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar boardID boards.ID\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tboards := hub.GetThreads(cross(cur), uint64(boardID), 0, boards2.BoardCount())\n\tprintln(len(boards))\n}\n\n// Output:\n// 0\n"},{"name":"z_3_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_3_b_filetest\n\n// Test getting threads from a board\npackage z_3_b_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar board hub.Board\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\t// Create a couple of board threads\n\tboards2.CreateThread(cross(cur), boardID, \"First\", \"Body\")\n\tboards2.CreateThread(cross(cur), boardID, \"Second\", \"Body\")\n\tboards2.CreateThread(cross(cur), boardID, \"Third\", \"Body\")\n\n\t// Get readonly board\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tboard, _ = hub.GetBoard(cross(cur), uint64(boardID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthreads := hub.GetThreads(cross(cur), board.ID, 0, board.ThreadCount)\n\n\tfor _, t := range threads {\n\t\tprintln(t.ID, t.Title)\n\t}\n}\n\n// Output:\n// 1 First\n// 2 Second\n// 3 Third\n"},{"name":"z_4_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_4_a_filetest\n\n// Test getting board members\npackage z_4_a_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar board hub.Board\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\n\t// Invite board members\n\tboards2.InviteMember(cross(cur), boardID, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\", \"owner\")\n\tboards2.InviteMember(cross(cur), boardID, \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", \"admin\")\n\n\t// Get readonly board\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tboard, _ = hub.GetBoard(cross(cur), uint64(boardID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tmembers := hub.GetMembers(cross(cur), board.ID, 0, board.MemberCount)\n\n\tfor _, m := range members {\n\t\tprintln(m.Address, m.Roles)\n\t}\n}\n\n// Output:\n// g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 slice[(\"owner\" gno.land/p/gnoland/boards.Role)]\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh slice[(\"owner\" gno.land/p/gnoland/boards.Role)]\n// g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj slice[(\"admin\" gno.land/p/gnoland/boards.Role)]\n"},{"name":"z_5_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_5_a_filetest\n\n// Test getting reposts from a thread without reposts\npackage z_5_a_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar thread hub.Thread\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"aaa123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\n\t// Get readonly thread\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, _ = hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\treposts := hub.GetReposts(cross(cur), thread.BoardID, thread.ID, 0, thread.RepostCount)\n\tprintln(len(reposts))\n}\n\n// Output:\n// 0\n"},{"name":"z_5_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_5_b_filetest\n\n// Test getting reposts of a thread\npackage z_5_b_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar thread hub.Thread\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"aaa123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\n\t// Create first repost\n\tdstBoardID := boards2.CreateBoard(cross(cur), \"bbb123\", false, false)\n\tboards2.CreateRepost(cross(cur), boardID, threadID, dstBoardID, \"First\", \"Body\")\n\n\t// Create second repost\n\tdstBoardID = boards2.CreateBoard(cross(cur), \"ccc123\", false, false)\n\tboards2.CreateRepost(cross(cur), boardID, threadID, dstBoardID, \"Second\", \"Body\")\n\n\t// Get readonly thread\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, _ = hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\treposts := hub.GetReposts(cross(cur), thread.BoardID, thread.ID, 0, thread.RepostCount)\n\n\tfor _, t := range reposts {\n\t\tprintln(t.ID, t.Title)\n\t}\n}\n\n// Output:\n// 1 First\n// 1 Second\n"},{"name":"z_6_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_6_a_filetest\n\n// Test getting flags from an unflagged thread\npackage z_6_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar (\n\tboardID  boards.ID\n\tthreadID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tflags := hub.GetFlags(cross(cur), uint64(boardID), uint64(threadID), 0, 0, 1)\n\tprintln(len(flags))\n}\n\n// Output:\n// 0\n"},{"name":"z_6_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_6_b_filetest\n\n// Test getting flags from an unflagged comment\npackage z_6_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar (\n\tboardID   boards.ID\n\tthreadID  boards.ID\n\tcommentID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\tcommentID = boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tflags := hub.GetFlags(cross(cur), uint64(boardID), uint64(threadID), uint64(commentID), 0, 1)\n\tprintln(len(flags))\n}\n\n// Output:\n// 0\n"},{"name":"z_6_c_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_6_c_filetest\n\n// Test getting flags from a thread\npackage z_6_c_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin             = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\tmoderator         = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"\n)\n\nvar thread hub.Thread\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\n\t// Invite members\n\tboards2.InviteMember(cross(cur), boardID, admin, \"admin\")\n\tboards2.InviteMember(cross(cur), boardID, moderator, \"moderator\")\n\n\t// Update flagging threshold to two flags\n\tboards2.SetFlaggingThreshold(cross(cur), boardID, 2)\n\n\t// Add first flag\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tboards2.FlagThread(cross(cur), boardID, threadID, \"Reason 1\")\n\n\t// Add second flag\n\ttesting.SetRealm(testing.NewUserRealm(moderator))\n\tboards2.FlagThread(cross(cur), boardID, threadID, \"Reason 2\")\n\n\t// Get readonly thread\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, _ = hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tflags := hub.GetFlags(cross(cur), thread.BoardID, thread.ID, 0, 0, thread.FlagCount)\n\n\tfor _, f := range flags {\n\t\tprintln(f.User, f.Reason)\n\t}\n}\n\n// Output:\n// g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 Reason 1\n// g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj Reason 2\n"},{"name":"z_6_d_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_6_d_filetest\n\n// Test getting flags from a comment\npackage z_6_d_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nconst (\n\towner     address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"\n\tadmin             = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\tmoderator         = \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"\n)\n\nvar comment hub.Comment\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\tcommentID := boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n\n\t// Invite member\n\tboards2.InviteMember(cross(cur), boardID, admin, \"admin\")\n\tboards2.InviteMember(cross(cur), boardID, moderator, \"moderator\")\n\n\t// Update flagging threshold to two flags\n\tboards2.SetFlaggingThreshold(cross(cur), boardID, 2)\n\n\t// Add first flag\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tboards2.FlagReply(cross(cur), boardID, threadID, commentID, \"Reason 1\")\n\n\t// Add second flag\n\ttesting.SetRealm(testing.NewUserRealm(moderator))\n\tboards2.FlagReply(cross(cur), boardID, threadID, commentID, \"Reason 2\")\n\n\t// Get readonly comment\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tcomment, _ = hub.GetComment(cross(cur), uint64(boardID), uint64(threadID), uint64(commentID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tflags := hub.GetFlags(cross(cur), comment.BoardID, comment.ThreadID, comment.ID, 0, comment.FlagCount)\n\n\tfor _, f := range flags {\n\t\tprintln(f.User, f.Reason)\n\t}\n}\n\n// Output:\n// g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 Reason 1\n// g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj Reason 2\n"},{"name":"z_7_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_7_a_filetest\n\n// Test getting comments from a thread without comments\npackage z_7_a_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar thread hub.Thread\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\n\t// Get readonly thread\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, _ = hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tcomments := hub.GetComments(cross(cur), thread.BoardID, thread.ID, 0, thread.CommentCount)\n\tprintln(len(comments))\n}\n\n// Output:\n// 0\n"},{"name":"z_7_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_7_b_filetest\n\n// Test getting comments from a thread\npackage z_7_b_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar thread hub.Thread\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\tcommentID := boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment 1\")\n\n\t// Create another comment\n\tboards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment 2\")\n\n\t// Add a reply (it should not be included in the output)\n\tboards2.CreateReply(cross(cur), boardID, threadID, commentID, \"Reply\")\n\n\t// Get readonly thread\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tthread, _ = hub.GetThread(cross(cur), uint64(boardID), uint64(threadID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tcomments := hub.GetComments(cross(cur), thread.BoardID, thread.ID, 0, thread.CommentCount)\n\n\tfor _, c := range comments {\n\t\tprintln(c.ID, c.Body)\n\t}\n}\n\n// Output:\n// 2 Comment 1\n// 3 Comment 2\n"},{"name":"z_8_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_8_a_filetest\n\n// Test getting replies from a comment without replies\npackage z_8_a_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar comment hub.Comment\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\tcommentID := boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n\n\t// Get readonly comment\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tcomment, _ = hub.GetComment(cross(cur), uint64(boardID), uint64(threadID), uint64(commentID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\treplies := hub.GetReplies(cross(cur), comment.BoardID, comment.ThreadID, comment.ID, 0, comment.ReplyCount)\n\tprintln(len(replies))\n}\n\n// Output:\n// 0\n"},{"name":"z_8_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_8_b_filetest\n\n// Test getting replies from a comment\npackage z_8_b_filetest\n\nimport (\n\t\"testing\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar comment hub.Comment\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID := boards2.CreateBoard(cross(cur), \"test123\", false, false)\n\tthreadID := boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\tcommentID := boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n\n\t// Create replies\n\tboards2.CreateReply(cross(cur), boardID, threadID, commentID, \"Reply 1\")\n\tsubCommentID := boards2.CreateReply(cross(cur), boardID, threadID, commentID, \"Reply 2\")\n\n\t// Add a sub-reply (it should not be included in the output)\n\tboards2.CreateReply(cross(cur), boardID, threadID, subCommentID, \"Reply 3\")\n\n\t// Get readonly comment\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tcomment, _ = hub.GetComment(cross(cur), uint64(boardID), uint64(threadID), uint64(commentID))\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\treplies := hub.GetReplies(cross(cur), comment.BoardID, comment.ThreadID, comment.ID, 0, comment.ReplyCount)\n\n\tfor _, r := range replies {\n\t\tprintln(r.ID, r.Body)\n\t}\n}\n\n// Output:\n// 3 Reply 1\n// 4 Reply 2\n"},{"name":"z_9_a_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_9_a_filetest\n\n// Test default thread comment values\npackage z_9_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar (\n\tboardID   boards.ID\n\tthreadID  boards.ID\n\tcommentID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"origin123\", false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\tcommentID = boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tcomment, found := hub.GetComment(cross(cur), uint64(boardID), uint64(threadID), uint64(commentID))\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(comment.ID)\n\tprintln(comment.BoardID)\n\tprintln(comment.ThreadID)\n\tprintln(comment.ParentID)\n\tprintln(comment.Body)\n\tprintln(comment.Hidden)\n\tprintln(comment.ReplyCount)\n\tprintln(comment.FlagCount)\n\tprintln(comment.Creator)\n\tprintln(comment.CreatedAt)\n\tprintln(comment.UpdatedAt)\n}\n\n// Output:\n// 2\n// 1\n// 1\n// 1\n// Comment\n// false\n// 0\n// 0\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n// 1234567890\n// 0\n"},{"name":"z_9_b_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/boards2/v1/hub/filetests/z_9_b_filetest\n\n// Test non default thread comment values\npackage z_9_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/gnoland/boards\"\n\n\tboards2 \"gno.land/r/gnoland/boards2/v1\"\n\t\"gno.land/r/gnoland/boards2/v1/hub\"\n)\n\nvar (\n\tboardID   boards.ID\n\tthreadID  boards.ID\n\tcommentID boards.ID\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\"))\n\tboardID = boards2.CreateBoard(cross(cur), \"origin123\", false, false)\n\tthreadID = boards2.CreateThread(cross(cur), boardID, \"Title\", \"Body\")\n\tcommentID = boards2.CreateReply(cross(cur), boardID, threadID, 0, \"Comment\")\n\n\t// Edit comment\n\tboards2.EditReply(cross(cur), boardID, threadID, commentID, \"Test comment\")\n\n\t// Create a comment reply\n\tboards2.CreateReply(cross(cur), boardID, threadID, commentID, \"Reply\")\n\n\t// Flag comment\n\tboards2.FlagReply(cross(cur), boardID, threadID, commentID, \"Reason\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gnoland/boards2/test\"))\n\tcomment, found := hub.GetComment(cross(cur), uint64(boardID), uint64(threadID), uint64(commentID))\n\tif !found {\n\t\treturn\n\t}\n\n\tprintln(comment.ID)\n\tprintln(comment.BoardID)\n\tprintln(comment.ThreadID)\n\tprintln(comment.ParentID)\n\tprintln(comment.Body)\n\tprintln(comment.Hidden)\n\tprintln(comment.ReplyCount)\n\tprintln(comment.FlagCount)\n\tprintln(comment.Creator)\n\tprintln(comment.CreatedAt)\n\tprintln(comment.UpdatedAt)\n}\n\n// Output:\n// 2\n// 1\n// 1\n// 1\n// Test comment\n// true\n// 1\n// 1\n// g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n// 1234567890\n// 1234567890\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"LDZsOqZF/sryVabOFbpDGKP7sCt8R/oRc/s+er4TrsvofyWrnJb21jX0Lqu1vgL2Xs4HMCf1i5iqGZNagI+uCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"coins","path":"gno.land/r/gnoland/coins","files":[{"name":"coins.gno","body":"// Package coins provides simple helpers to retrieve information about coins\n// on the Gno.land blockchain.\n//\n// The primary goal of this realm is to allow users to check their token balances without\n// relying on external tools or services. This is particularly valuable for new networks\n// that aren't yet widely supported by public explorers or wallets. By using this realm,\n// users can always access their balance information directly through the gnodev.\n//\n// While currently focused on basic balance checking functionality, this realm could\n// potentially be extended to support other banker-related workflows in the future.\n// However, we aim to keep it minimal and focused on its core purpose.\n//\n// This is a \"Render-only realm\" - it exposes only a Render function as its public\n// interface and doesn't maintain any state of its own. This pattern allows for\n// simple, stateless information retrieval directly through the blockchain's\n// rendering capabilities.\npackage coins\n\nimport (\n\t\"chain/banker\"\n\t\"chain/runtime\"\n\t\"net/url\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/leon/coinsort\"\n\t\"gno.land/p/leon/ctg\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/sys/users\"\n)\n\nvar router *mux.Router\n\nfunc init() {\n\trouter = mux.NewRouter()\n\n\trouter.HandleFunc(\"\", func(res *mux.ResponseWriter, req *mux.Request) {\n\t\tres.Write(renderHomepage())\n\t})\n\n\trouter.HandleFunc(\"balances\", func(res *mux.ResponseWriter, req *mux.Request) {\n\t\tres.Write(renderBalances(req))\n\t})\n\n\trouter.HandleFunc(\"convert/{address}\", func(res *mux.ResponseWriter, req *mux.Request) {\n\t\tres.Write(renderConvertedAddress(req.GetVar(\"address\")))\n\t})\n\n\t// Coin info\n\trouter.HandleFunc(\"supply/{denom}\", func(res *mux.ResponseWriter, req *mux.Request) {\n\t\t// banker := banker.NewReadonlyBanker()\n\t\t// res.Write(renderAddressBalance(banker, denom, denom))\n\t\tres.Write(\"The total supply feature is coming soon.\")\n\t})\n\n\trouter.NotFoundHandler = func(res *mux.ResponseWriter, req *mux.Request) {\n\t\tres.Write(\"# 404\\n\\nThat page was not found. Would you like to [**go home**?](/r/gnoland/coins)\")\n\t}\n}\n\nfunc Render(path string) string {\n\treturn router.Render(path)\n}\n\nfunc renderHomepage() string {\n\treturn strings.Replace(`# Gno.land Coins Explorer\n\nThis is a simple, readonly realm that allows users to browse native coin balances. Check your coin balance below!\n\n\u003cgno-form path=\"balances\"\u003e\n\t\u003cgno-input name=\"address\" type=\"text\" placeholder=\"Valid bech32 address (e.g. g1..., cosmos1..., osmo1...)\" /\u003e\n\t\u003cgno-input name=\"coin\" type=\"text\" placeholder=\"Coin (e.g. ugnot)\"\" /\u003e\n\u003c/gno-form\u003e\n\nHere are a few more ways to use this app:\n\n- ~/r/gnoland/coins:balances?address=g1...~ - show full list of coin balances of an address\n\t- [Example](/r/gnoland/coins:balances?address=g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5)\n- ~/r/gnoland/coins:balances?address=g1...\u0026coin=ugnot~ - shows the balance of an address for a specific coin\n\t- [Example](/r/gnoland/coins:balances?address=g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\u0026coin=ugnot)\n- ~/r/gnoland/coins:convert/\u003cbech32_addr\u003e~ - convert a bech32 address to a Gno address\n\t- [Example](/r/gnoland/coins:convert/cosmos1jg8mtutu9khhfwc4nxmuhcpftf0pajdh6svrgs)\n- ~/r/gnoland/coins:supply/\u003cdenom\u003e~ - shows the total supply of denom\n\t- Coming soon!\n\n`, \"~\", \"`\", -1)\n}\n\nfunc renderBalances(req *mux.Request) string {\n\tout := \"# Balances\\n\\n\"\n\n\tinput := req.Query.Get(\"address\")\n\tcoin := req.Query.Get(\"coin\")\n\n\tif input == \"\" \u0026\u0026 coin == \"\" {\n\t\tout += \"Please input a valid address and coin denomination.\\n\\n\"\n\t\treturn out\n\t}\n\n\tif input == \"\" {\n\t\tout += \"Please input a valid bech32 address.\\n\\n\"\n\t\treturn out\n\t}\n\n\toriginalInput := input\n\tvar wasConverted bool\n\n\t// Try to validate or convert\n\tif !address(input).IsValid() {\n\t\taddr, err := ctg.ConvertAnyToGno(input)\n\t\tif err != nil {\n\t\t\treturn out + ufmt.Sprintf(\"Tried converting `%s` to a Gno address but failed. Please try with a valid bech32 address.\\n\\n\", input)\n\t\t}\n\t\tinput = addr.String()\n\t\twasConverted = true\n\t}\n\n\tif wasConverted {\n\t\tout += ufmt.Sprintf(\"\u003e [!NOTE]\\n\u003e  Automatically converted `%s` to its Gno equivalent.\\n\\n\", originalInput)\n\t}\n\n\tbanker_ := banker.NewReadonlyBanker()\n\tbalances := banker_.GetCoins(address(input))\n\n\tif len(balances) == 0 {\n\t\tout += \"This address currently has no coins.\"\n\t\treturn out\n\t}\n\n\tif coin != \"\" {\n\t\treturn renderSingleCoinBalance(coin, input, originalInput, wasConverted)\n\t}\n\n\tuser, _ := users.ResolveAny(input)\n\tname := \"`\" + input + \"`\"\n\tif user != nil {\n\t\tname = user.RenderLink(\"\")\n\t}\n\n\tout += ufmt.Sprintf(\"This page shows full coin balances of %s at block #%d\\n\\n\",\n\t\tname, runtime.ChainHeight())\n\n\t// Determine sorting\n\tif getSortField(req) == \"balance\" {\n\t\tcoinsort.SortByBalance(balances)\n\t}\n\n\t// Create table\n\tdenomColumn := renderSortLink(req, \"denom\", \"Denomination\")\n\tbalanceColumn := renderSortLink(req, \"balance\", \"Balance\")\n\ttable := mdtable.Table{\n\t\tHeaders: []string{denomColumn, balanceColumn},\n\t}\n\n\tif isSortReversed(req) {\n\t\tfor _, b := range balances {\n\t\t\ttable.Append([]string{b.Denom, strconv.Itoa(int(b.Amount))})\n\t\t}\n\t} else {\n\t\tfor i := len(balances) - 1; i \u003e= 0; i-- {\n\t\t\ttable.Append([]string{balances[i].Denom, strconv.Itoa(int(balances[i].Amount))})\n\t\t}\n\t}\n\n\tout += table.String() + \"\\n\\n\"\n\treturn out\n}\n\nfunc renderSingleCoinBalance(denom, addr, origInput string, wasConverted bool) string {\n\tout := \"# Coin balance\\n\\n\"\n\tbanker_ := banker.NewReadonlyBanker()\n\n\tif wasConverted {\n\t\tout += ufmt.Sprintf(\"\u003e [!NOTE]\\n\u003e  Automatically converted `%s` to its Gno equivalent.\\n\\n\", origInput)\n\t}\n\n\tuser, _ := users.ResolveAny(addr)\n\tname := \"`\" + addr + \"`\"\n\tif user != nil {\n\t\tname = user.RenderLink(\"\")\n\t}\n\n\tout += ufmt.Sprintf(\"%s has `%d%s` at block #%d\\n\\n\",\n\t\tname, banker_.GetCoins(address(addr)).AmountOf(denom), denom, runtime.ChainHeight())\n\n\tout += \"[View full balance list for this address](/r/gnoland/coins:balances?address=\" + addr + \")\"\n\n\treturn out\n}\n\nfunc renderConvertedAddress(addr string) string {\n\tout := \"# Address converter\\n\\n\"\n\n\tgnoAddress, err := ctg.ConvertAnyToGno(addr)\n\tif err != nil {\n\t\tout += err.Error()\n\t\treturn out\n\t}\n\n\tuser, _ := users.ResolveAny(gnoAddress.String())\n\tname := \"`\" + gnoAddress.String() + \"`\"\n\tif user != nil {\n\t\tname = user.RenderLink(\"\")\n\t}\n\n\tout += ufmt.Sprintf(\"`%s` on Cosmos matches %s on gno.land.\\n\\n\", addr, name)\n\tout += \"[[View `ugnot` balance for this address]](/r/gnoland/coins:balances?address=\" + gnoAddress.String() + \"\u0026coin=ugnot) - \"\n\tout += \"[[View full balance list for this address]](/r/gnoland/coins:balances?address=\" + gnoAddress.String() + \")\"\n\treturn out\n}\n\n// Helper functions for sorting and pagination\nfunc getSortField(req *mux.Request) string {\n\tfield := req.Query.Get(\"sort\")\n\tswitch field {\n\tcase \"denom\", \"balance\":\n\t\treturn field\n\t}\n\treturn \"denom\"\n}\n\nfunc isSortReversed(req *mux.Request) bool {\n\treturn req.Query.Get(\"order\") != \"asc\"\n}\n\nfunc renderSortLink(req *mux.Request, field, label string) string {\n\tcurrentField := getSortField(req)\n\tcurrentOrder := req.Query.Get(\"order\")\n\n\tnewOrder := \"desc\"\n\tif field == currentField \u0026\u0026 currentOrder != \"asc\" {\n\t\tnewOrder = \"asc\"\n\t}\n\n\tquery := make(url.Values)\n\tfor k, vs := range req.Query {\n\t\tquery[k] = append([]string(nil), vs...)\n\t}\n\n\tquery.Set(\"sort\", field)\n\tquery.Set(\"order\", newOrder)\n\n\tif field == currentField {\n\t\tif currentOrder == \"asc\" {\n\t\t\tlabel += \" ↑\"\n\t\t} else {\n\t\t\tlabel += \" ↓\"\n\t\t}\n\t}\n\n\treturn md.Link(label, \"?\"+query.Encode())\n}\n"},{"name":"coins_test.gno","body":"package coins\n\nimport (\n\t\"chain\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/leon/ctg\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestBalanceChecker(t *testing.T) {\n\tdenom1 := \"testtoken1\"\n\tdenom2 := \"testtoken2\"\n\taddr1 := testutils.TestAddress(\"user1\")\n\taddr2 := testutils.TestAddress(\"user2\")\n\n\tcoinsRealm := testing.NewCodeRealm(\"gno.land/r/gnoland/coins\")\n\ttesting.SetRealm(coinsRealm)\n\n\ttesting.IssueCoins(addr1, chain.NewCoins(chain.NewCoin(denom1, 1000000)))\n\ttesting.IssueCoins(addr2, chain.NewCoins(chain.NewCoin(denom1, 501)))\n\n\ttesting.IssueCoins(addr2, chain.NewCoins(chain.NewCoin(denom2, 12345)))\n\n\tgnoAddr, _ := ctg.ConvertCosmosToGno(\"cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr\")\n\tosmoAddr := \"osmo1fl48vsnmsdzcv85q5d2q4z5ajdha8yu3aq6l09\"\n\tgnoAddr1, _ := ctg.ConvertAnyToGno(osmoAddr)\n\n\ttesting.IssueCoins(gnoAddr1, chain.NewCoins(chain.NewCoin(denom2, 12345)))\n\n\ttests := []struct {\n\t\tname      string\n\t\tpath      string\n\t\tcontains  string\n\t\twantPanic bool\n\t}{\n\t\t{\n\t\t\tname:     \"homepage\",\n\t\t\tpath:     \"\",\n\t\t\tcontains: \"# Gno.land Coins Explorer\",\n\t\t},\n\t\t// TODO: not supported yet\n\t\t// {\n\t\t// \tname:     \"total supply\",\n\t\t// \tpath:     denom,\n\t\t// \texpected: \"Balance: 1500000testtoken\",\n\t\t// },\n\t\t{\n\t\t\tname:     \"addr1's coin balance\",\n\t\t\tpath:     ufmt.Sprintf(\"balances?address=%s\u0026coin=%s\", addr1.String(), denom1),\n\t\t\tcontains: ufmt.Sprintf(\"`%s` has `%d%s`\", addr1.String(), 1000000, denom1),\n\t\t},\n\t\t{\n\t\t\tname:     \"addr2's full balances\",\n\t\t\tpath:     ufmt.Sprintf(\"balances?address=%s\", addr2.String()),\n\t\t\tcontains: ufmt.Sprintf(\"This page shows full coin balances of `%s` at block\", addr2.String()),\n\t\t},\n\t\t{\n\t\t\tname: \"addr2's full balances\",\n\t\t\tpath: ufmt.Sprintf(\"balances?address=%s\", addr2.String()),\n\t\t\tcontains: `| testtoken1 | 501 |\n| testtoken2 | 12345 |`,\n\t\t},\n\t\t{\n\t\t\tname:     \"addr2's coin balance\",\n\t\t\tpath:     ufmt.Sprintf(\"balances?address=%s\u0026coin=%s\", addr2.String(), denom1),\n\t\t\tcontains: ufmt.Sprintf(\"`%s` has `%d%s`\", addr2.String(), 501, denom1),\n\t\t},\n\t\t{\n\t\t\tname:     \"cosmos addr conversion\",\n\t\t\tpath:     \"convert/cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr\",\n\t\t\tcontains: ufmt.Sprintf(\"`cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr` on Cosmos matches `%s`\", gnoAddr),\n\t\t},\n\t\t{\n\t\t\tname:     \"balances bech32 auto convert\",\n\t\t\tpath:     ufmt.Sprintf(\"balances?address=%s\u0026coin=%s\", osmoAddr, denom1),\n\t\t\tcontains: \"Automatically converted `osmo1fl48vsnmsdzcv85q5d2q4z5ajdha8yu3aq6l09`\",\n\t\t},\n\t\t{\n\t\t\tname:     \"single coin balance bech32 auto convert\",\n\t\t\tpath:     ufmt.Sprintf(\"balances?address=%s\", osmoAddr),\n\t\t\tcontains: \"Automatically converted `osmo1fl48vsnmsdzcv85q5d2q4z5ajdha8yu3aq6l09`\",\n\t\t},\n\t\t{\n\t\t\tname:      \"no addr\",\n\t\t\tpath:      \"balances?address=\",\n\t\t\tcontains:  \"Please input a valid address\",\n\t\t\twantPanic: false,\n\t\t},\n\t\t{\n\t\t\tname:      \"no addr\",\n\t\t\tpath:      \"balances?address=\u0026coin=\",\n\t\t\tcontains:  \"Please input a valid address and coin denomination.\",\n\t\t\twantPanic: false,\n\t\t},\n\t\t{\n\t\t\tname:      \"invalid path\",\n\t\t\tpath:      \"invalid\",\n\t\t\tcontains:  \"404\",\n\t\t\twantPanic: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif tt.wantPanic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r == nil {\n\t\t\t\t\t\tt.Errorf(\"expected panic for %s\", tt.name)\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\n\t\t\tresult := Render(tt.path)\n\t\t\tif !tt.wantPanic {\n\t\t\t\tif !strings.Contains(result, tt.contains) {\n\t\t\t\t\tt.Errorf(\"expected %s to contain %s\", result, tt.contains)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/coins\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"9moN3nLaXcRzvlY8vLqux21l+g1OP66kHv9ENrBIEJRDMosbVaJFKgtVJaGnjv1eUVz9x33T2RE+/ZR1xOsxAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"ghverify","path":"gno.land/r/gnoland/ghverify","files":[{"name":"README.md","body":"# ghverify\n\nThis realm is intended to enable off chain gno address to github handle verification.\nThe steps are as follows:\n- A user calls `RequestVerification` and provides a github handle. This creates a new static oracle feed.\n- An off-chain agent controlled by the owner of this realm requests current feeds using the `GnorkleEntrypoint` function and provides a message of `\"request\"`\n- The agent receives the task information that includes the github handle and the gno address. It performs the verification step by checking whether this github user has the address in a github repository it controls.\n- The agent publishes the result of the verification by calling `GnorkleEntrypoint` with a message structured like: `\"ingest,\u003ctask id\u003e,\u003cverification status\u003e\"`. The verification status is `OK` if verification succeeded and any other value if it failed.\n- The oracle feed's ingester processes the verification and the handle to address mapping is written to the avl trees that exist as ghverify realm variables.\n"},{"name":"contract.gno","body":"package ghverify\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\n\t\"gno.land/p/demo/gnorkle/feeds/static\"\n\t\"gno.land/p/demo/gnorkle/gnorkle\"\n\t\"gno.land/p/demo/gnorkle/message\"\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\nconst (\n\t// The agent should send this value if it has verified the github handle.\n\tverifiedResult = \"OK\"\n)\n\nvar (\n\townerAddress = unsafe.OriginCaller()\n\toracle       *gnorkle.Instance\n\tpostHandler  postGnorkleMessageHandler\n\n\thandleToAddressMap = bptree.NewBPTree32()\n\taddressToHandleMap = bptree.NewBPTree32()\n)\n\nfunc init() {\n\toracle = gnorkle.NewInstance()\n\toracle.AddToWhitelist(\"\", []string{string(ownerAddress)})\n}\n\ntype postGnorkleMessageHandler struct{}\n\n// Handle does post processing after a message is ingested by the oracle feed. It extracts the value to realm\n// storage and removes the feed from the oracle.\nfunc (h postGnorkleMessageHandler) Handle(i *gnorkle.Instance, funcType message.FuncType, feed gnorkle.Feed) error {\n\tif funcType != message.FuncTypeIngest {\n\t\treturn nil\n\t}\n\n\tresult, _, consumable := feed.Value()\n\tif !consumable {\n\t\treturn nil\n\t}\n\n\t// The value is consumable, meaning the ingestion occurred, so we can remove the feed from the oracle\n\t// after saving it to realm storage.\n\tdefer oracle.RemoveFeed(feed.ID())\n\n\t// Couldn't verify; nothing to do.\n\tif result.String != verifiedResult {\n\t\treturn nil\n\t}\n\n\tfeedTasks := feed.Tasks()\n\tif len(feedTasks) != 1 {\n\t\treturn errors.New(\"expected feed to have exactly one task\")\n\t}\n\n\ttask, ok := feedTasks[0].(*verificationTask)\n\tif !ok {\n\t\treturn errors.New(\"expected ghverify task\")\n\t}\n\n\thandleToAddressMap.Set(task.githubHandle, task.gnoAddress)\n\taddressToHandleMap.Set(task.gnoAddress, task.githubHandle)\n\treturn nil\n}\n\n// RequestVerification creates a new static feed with a single task that will\n// instruct an agent to verify the github handle / gno address pair.\nfunc RequestVerification(cur realm, githubHandle string) {\n\tgnoAddress := string(unsafe.OriginCaller())\n\tif err := oracle.AddFeeds(\n\t\tstatic.NewSingleValueFeed(\n\t\t\tgnoAddress,\n\t\t\t\"string\",\n\t\t\t\u0026verificationTask{\n\t\t\t\tgnoAddress:   gnoAddress,\n\t\t\t\tgithubHandle: githubHandle,\n\t\t\t},\n\t\t),\n\t); err != nil {\n\t\tpanic(err)\n\t}\n\tchain.Emit(\n\t\t\"verification_requested\",\n\t\t\"from\", gnoAddress,\n\t\t\"handle\", githubHandle,\n\t)\n}\n\n// GnorkleEntrypoint is the entrypoint to the gnorkle oracle handler.\nfunc GnorkleEntrypoint(cur realm, message string) string {\n\tresult, err := oracle.HandleMessage(message, postHandler)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn result\n}\n\n// SetOwner transfers ownership of the contract to the given address.\nfunc SetOwner(_ realm, owner address) {\n\tif ownerAddress != unsafe.OriginCaller() {\n\t\tpanic(\"only the owner can set a new owner\")\n\t}\n\n\townerAddress = owner\n\n\t// In the context of this contract, the owner is the only one that can\n\t// add new feeds to the oracle.\n\toracle.ClearWhitelist(\"\")\n\toracle.AddToWhitelist(\"\", []string{string(ownerAddress)})\n}\n\n// GetHandleByAddress returns the github handle associated with the given gno address.\nfunc GetHandleByAddress(cur realm, address_XXX string) string {\n\tif value := addressToHandleMap.Get(address_XXX); value != nil {\n\t\treturn value.(string)\n\t}\n\n\treturn \"\"\n}\n\n// GetAddressByHandle returns the gno address associated with the given github handle.\nfunc GetAddressByHandle(cur realm, handle string) string {\n\tif value := handleToAddressMap.Get(handle); value != nil {\n\t\treturn value.(string)\n\t}\n\n\treturn \"\"\n}\n\n// Render returns a json object string will all verified handle -\u003e address mappings.\nfunc Render(_ string) string {\n\tresult := \"{\"\n\tvar appendComma bool\n\thandleToAddressMap.Iterate(\"\", \"\", func(handle string, address_XXX any) bool {\n\t\tif appendComma {\n\t\t\tresult += \",\"\n\t\t}\n\n\t\tresult += `\"` + handle + `\": \"` + address_XXX.(string) + `\"`\n\t\tappendComma = true\n\n\t\treturn false\n\t})\n\n\treturn result + \"}\"\n}\n"},{"name":"contract_test.gno","body":"package ghverify\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nfunc TestVerificationLifecycle(cur realm, t *testing.T) {\n\tdefaultAddress := unsafe.OriginCaller()\n\tuser1Address := address(testutils.TestAddress(\"user 1\"))\n\tuser2Address := address(testutils.TestAddress(\"user 2\"))\n\n\t// Verify request returns no feeds.\n\tresult := GnorkleEntrypoint(cur, \"request\")\n\tif result != \"[]\" {\n\t\tt.Fatalf(\"expected empty request result, got %s\", result)\n\t}\n\n\t// Make a verification request with the created user.\n\ttesting.SetOriginCaller(user1Address)\n\tRequestVerification(cur, \"deelawn\")\n\n\t// A subsequent request from the same address should panic because there is\n\t// already a feed with an ID of this user's address.\n\tvar errMsg string\n\tfunc(cur realm) {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\terrMsg = r.(error).Error()\n\t\t\t}\n\t\t}()\n\t\tRequestVerification(cur, \"deelawn\")\n\t}(cur)\n\tif errMsg != \"feed already exists\" {\n\t\tt.Fatalf(\"expected feed already exists, got %s\", errMsg)\n\t}\n\n\t// Verify the request returns no feeds for this non-whitelisted user.\n\tresult = GnorkleEntrypoint(cur, \"request\")\n\tif result != \"[]\" {\n\t\tt.Fatalf(\"expected empty request result, got %s\", result)\n\t}\n\n\t// Make a verification request with the created user.\n\ttesting.SetOriginCaller(user2Address)\n\tRequestVerification(cur, \"omarsy\")\n\n\t// Set the caller back to the whitelisted user and verify that the feed data\n\t// returned matches what should have been created by the `RequestVerification`\n\t// invocation.\n\ttesting.SetOriginCaller(defaultAddress)\n\tresult = GnorkleEntrypoint(cur, \"request\")\n\texpResult := `[{\"id\":\"` + string(user1Address) + `\",\"type\":\"0\",\"value_type\":\"string\",\"tasks\":[{\"gno_address\":\"` +\n\t\tstring(user1Address) + `\",\"github_handle\":\"deelawn\"}]},` +\n\t\t`{\"id\":\"` + string(user2Address) + `\",\"type\":\"0\",\"value_type\":\"string\",\"tasks\":[{\"gno_address\":\"` +\n\t\tstring(user2Address) + `\",\"github_handle\":\"omarsy\"}]}]`\n\tif result != expResult {\n\t\tt.Fatalf(\"expected request result %s, got %s\", expResult, result)\n\t}\n\n\t// Try to trigger feed ingestion from the non-authorized user.\n\ttesting.SetOriginCaller(user1Address)\n\tfunc(cur realm) {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\terrMsg = r.(error).Error()\n\t\t\t}\n\t\t}()\n\t\tGnorkleEntrypoint(cur, \"ingest,\"+string(user1Address)+\",OK\")\n\t}(cur)\n\tif errMsg != \"caller not whitelisted\" {\n\t\tt.Fatalf(\"expected caller not whitelisted, got %s\", errMsg)\n\t}\n\n\t// Set the caller back to the whitelisted user and transfer contract ownership.\n\ttesting.SetOriginCaller(defaultAddress)\n\tSetOwner(cross(cur), defaultAddress)\n\n\t// Now trigger the feed ingestion from the user and new owner and only whitelisted address.\n\tGnorkleEntrypoint(cur, \"ingest,\"+string(user1Address)+\",OK\")\n\tGnorkleEntrypoint(cur, \"ingest,\"+string(user2Address)+\",OK\")\n\n\t// Verify the ingestion autocommitted the value and triggered the post handler.\n\tdata := Render(\"\")\n\texpResult = `{\"deelawn\": \"` + string(user1Address) + `\",\"omarsy\": \"` + string(user2Address) + `\"}`\n\tif data != expResult {\n\t\tt.Fatalf(\"expected render data %s, got %s\", expResult, data)\n\t}\n\n\t// Finally make sure the feed was cleaned up after the data was committed.\n\tresult = GnorkleEntrypoint(cur, \"request\")\n\tif result != \"[]\" {\n\t\tt.Fatalf(\"expected empty request result, got %s\", result)\n\t}\n\n\t// Check that the accessor functions are working as expected.\n\tif handle := GetHandleByAddress(cur, string(user1Address)); handle != \"deelawn\" {\n\t\tt.Fatalf(\"expected deelawn, got %s\", handle)\n\t}\n\tif address_XXX := GetAddressByHandle(cur, \"deelawn\"); address_XXX != string(user1Address) {\n\t\tt.Fatalf(\"expected %s, got %s\", string(user1Address), address_XXX)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/ghverify\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"task.gno","body":"package ghverify\n\nimport (\n\t\"bufio\"\n\t\"bytes\"\n)\n\ntype verificationTask struct {\n\tgnoAddress   string\n\tgithubHandle string\n}\n\n// MarshalJSON marshals the task contents to JSON.\nfunc (t *verificationTask) MarshalJSON() ([]byte, error) {\n\tbuf := new(bytes.Buffer)\n\tw := bufio.NewWriter(buf)\n\n\tw.Write(\n\t\t[]byte(`{\"gno_address\":\"` + t.gnoAddress + `\",\"github_handle\":\"` + t.githubHandle + `\"}`),\n\t)\n\n\tw.Flush()\n\treturn buf.Bytes(), nil\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Sl98v00lLsLYe8Qw6rdnovxEe5YrxxmZk0ARFGf5gSTiC2cD1UhdnTozerRKuPbxzAZTlX6oivxMvzmF91f2AQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"home","path":"gno.land/r/gnoland/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/home\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"chain/runtime\"\n\t\"strconv\"\n\n\t\"gno.land/p/leon/svgbtn\"\n\t\"gno.land/p/moul/dynreplacer\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/r/devrels/events\"\n\tblog \"gno.land/r/gnoland/blog\"\n)\n\nvar (\n\toverride string\n\tAdmin    = ownable.NewWithAddress(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\") // govdao t1 multisig\n)\n\nfunc Render(_ string) string {\n\tr := dynreplacer.New()\n\tr.RegisterCallback(\":latest-blogposts:\", func() string {\n\t\treturn blog.RenderLastPostsWidget(4)\n\t})\n\tr.RegisterCallback(\":upcoming-events:\", func() string {\n\t\tout, _ := events.RenderEventWidget(events.MaxWidgetSize)\n\t\treturn out\n\t})\n\tr.RegisterCallback(\":qotb:\", quoteOfTheBlock)\n\tr.RegisterCallback(\":newsletter-button:\", newsletterButton)\n\tr.RegisterCallback(\":chain-height:\", func() string {\n\t\treturn strconv.Itoa(int(runtime.ChainHeight()))\n\t})\n\n\ttemplate := `# Welcome to Gno.land\n\nWe're building Gno.land, set to become the leading open-source smart contract\nplatform, using Gno, an interpreted and fully deterministic variation of the\nGo programming language for succinct and composable smart contracts.\n\nWith transparent and timeless code, Gno.land is the next generation of smart\ncontract platforms, serving as the \"GitHub\" of the ecosystem, with realms built\nusing fully transparent, auditable code that anyone can inspect and reuse.\n\nIntuitive and easy to use, Gno.land lowers the barrier to web3 and makes\ncensorship-resistant platforms accessible to everyone. If you want to help lay\nthe foundations of a fairer and freer world, join us today.\n\n---\n\n## [Boards](/r/gnoland/boards2/v1) - On-chain forum for the Gno.land community\n\n**Post, discuss, and create your content community**: Boards is a fully on-chain social forum to create Boards topics, post threads, comment and reply. A plug-and-deploy DAO lets communities manage content, permissions and moderation their way.\n\nExplore this ready-to-use Gno dApp, and experience decentralized social media in action.\n\n**[Open Boards](/r/gnoland/boards2/v1)**\n\n---\n\n\u003cgno-columns\u003e\n## Learn about Gno.land\n\n- [About](/about)\n- [GitHub](https://github.com/gnolang)\n- [Blog](/blog)\n- [Events](/events)\n- [Partners, Fund, Grants](/partners)\n- [Explore the Ecosystem](/ecosystem)\n- [Careers](https://jobs.ashbyhq.com/allinbits)\n\n\u003cgno-columns-sep\u003e\n\n## Build with Gno\n\n- [Write Gno in the browser](https://play.gno.land)\n- [Read about the Gno Language](/gnolang)\n- [Visit the official documentation](https://docs.gno.land)\n- [Efficient local development for Gno](https://docs.gno.land/resources/gnodev)\n- [Get testnet GNOTs](https://faucet.gno.land)\n\n\u003cgno-columns-sep\u003e\n\n## Explore the universe\n\n- [Discover demo packages](https://github.com/gnolang/gno/tree/master/examples)\n- [Gnoscan](https://gnoscan.io)\n- [Gno networks documentation](https://docs.gno.land/resources/gnoland-networks/)\n- [Staging](https://staging.gno.land/)\n- [Testnet 12](https://test12.testnets.gno.land/)\n- [Faucet Hub](https://faucet.gno.land)\n\n\u003c/gno-columns\u003e\n\n\u003cgno-columns\u003e\n\n## [Latest Blogposts](/r/gnoland/blog)\n\n:latest-blogposts:\n\n\u003cgno-columns-sep\u003e\n\n## [Latest Events](/events)\n\n:upcoming-events:\n\n\u003c/gno-columns\u003e\n\n---\n\n## [Gno Playground](https://play.gno.land)\n\nGno Playground is a web application designed for building, running, testing, and\ninteracting with your Gno code, enhancing your understanding of the Gno\nlanguage. With Gno Playground, you can share your code, execute tests, deploy\nyour realms and packages to Gno.land, and explore a multitude of other features.\n\nExperience the convenience of code sharing and rapid experimentation with\n[Gno Playground](https://play.gno.land).\n\n---\n\n## Explore New Packages and Realms\n\nAll code in Gno.land is organized in packages, and each package lives at a unique package path like\n\"r/gnoland/home\". You can browse packages, inspect their source, and use them in your own libraries and realms.\n\n\u003cgno-columns\u003e\n\n### r/gnoland\n\nOfficial realm packages developed by the Gno.land core team.\n\n[Browse](/r/gnoland)\n\n\u003cgno-columns-sep\u003e\n\n### r/sys\n\nSystem-level realm packages used by the chain.\n\n[Browse](/r/sys)\n\n\u003cgno-columns-sep\u003e\n\n### r/demo\n\nDemo realm packages showcasing what’s possible.\n\n[Browse](/r/demo)\n\n\u003cgno-columns-sep\u003e\n\n### p/demo\n\nPure packages for demo purposes.\n\n[Browse](/p/demo)\n\n\u003c/gno-columns\u003e\n\n---\n\n\u003cgno-columns\u003e\n\n## Socials\n\n- Check out our [community projects](https://github.com/gnolang/awesome-gno)\n- [Discord](https://discord.gg/S8nKUqwkPn)\n- [Twitter](https://twitter.com/_gnoland)\n- [Youtube](https://www.youtube.com/@_gnoland)\n- [Telegram](https://t.me/gnoland)\n\n\u003cgno-columns-sep\u003e\n\n## Quote of the ~Day~ Block #:chain-height:\n\n\u003e :qotb:\n\n\u003c/gno-columns\u003e\n\n---\n\n## Sign up for our newsletter\n\nStay in the Gno by signing up for our newsletter. You'll get the scoop on dev updates, fresh content, and community news.\n\n:newsletter-button:\n\n---\n\n**This is a testnet.** Package names are not guaranteed to be available for production.`\n\n\tif override != \"\" {\n\t\ttemplate = override\n\t}\n\tresult := r.Replace(template)\n\treturn result\n}\n\nfunc newsletterButton() string {\n\treturn svgbtn.Button(\n\t\t256,\n\t\t44,\n\t\t\"#226c57\",\n\t\t\"#ffffff\",\n\t\t\"Subscribe to stay in the Gno\",\n\t\t\"https://land.us18.list-manage.com/subscribe?u=8befe3303cf82796d2c1a1aff\u0026id=271812000b\",\n\t)\n}\n\nfunc quoteOfTheBlock() string {\n\tquotes := []string{\n\t\t\"Gno is for Truth.\",\n\t\t\"Gno is for Social Coordination.\",\n\t\t\"Gno is _not only_ for DeFi.\",\n\t\t\"Now, you Gno.\",\n\t\t\"Come for the Go, Stay for the Gno.\",\n\t}\n\theight := runtime.ChainHeight()\n\tidx := int(height) % len(quotes)\n\tqotb := quotes[idx]\n\treturn qotb\n}\n\nfunc AdminSetOverride(cur realm, content string) {\n\tAdmin.AssertOwnedBy(cur.Previous().Address())\n\toverride = content\n}\n\nfunc AdminTransferOwnership(cur realm, newOwner address) {\n\tif err := Admin.TransferOwnership(0, cur, newOwner); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"home_filetest.gno","body":"package main\n\nimport \"gno.land/r/gnoland/home\"\n\nfunc main() {\n\tprintln(home.Render(\"\"))\n}\n\n// Output:\n// # Welcome to Gno.land\n//\n// We're building Gno.land, set to become the leading open-source smart contract\n// platform, using Gno, an interpreted and fully deterministic variation of the\n// Go programming language for succinct and composable smart contracts.\n//\n// With transparent and timeless code, Gno.land is the next generation of smart\n// contract platforms, serving as the \"GitHub\" of the ecosystem, with realms built\n// using fully transparent, auditable code that anyone can inspect and reuse.\n//\n// Intuitive and easy to use, Gno.land lowers the barrier to web3 and makes\n// censorship-resistant platforms accessible to everyone. If you want to help lay\n// the foundations of a fairer and freer world, join us today.\n//\n// ---\n//\n// ## [Boards](/r/gnoland/boards2/v1) - On-chain forum for the Gno.land community\n//\n// **Post, discuss, and create your content community**: Boards is a fully on-chain social forum to create Boards topics, post threads, comment and reply. A plug-and-deploy DAO lets communities manage content, permissions and moderation their way.\n//\n// Explore this ready-to-use Gno dApp, and experience decentralized social media in action.\n//\n// **[Open Boards](/r/gnoland/boards2/v1)**\n//\n// ---\n//\n// \u003cgno-columns\u003e\n// ## Learn about Gno.land\n//\n// - [About](/about)\n// - [GitHub](https://github.com/gnolang)\n// - [Blog](/blog)\n// - [Events](/events)\n// - [Partners, Fund, Grants](/partners)\n// - [Explore the Ecosystem](/ecosystem)\n// - [Careers](https://jobs.ashbyhq.com/allinbits)\n//\n// \u003cgno-columns-sep\u003e\n//\n// ## Build with Gno\n//\n// - [Write Gno in the browser](https://play.gno.land)\n// - [Read about the Gno Language](/gnolang)\n// - [Visit the official documentation](https://docs.gno.land)\n// - [Efficient local development for Gno](https://docs.gno.land/resources/gnodev)\n// - [Get testnet GNOTs](https://faucet.gno.land)\n//\n// \u003cgno-columns-sep\u003e\n//\n// ## Explore the universe\n//\n// - [Discover demo packages](https://github.com/gnolang/gno/tree/master/examples)\n// - [Gnoscan](https://gnoscan.io)\n// - [Gno networks documentation](https://docs.gno.land/resources/gnoland-networks/)\n// - [Staging](https://staging.gno.land/)\n// - [Testnet 12](https://test12.testnets.gno.land/)\n// - [Faucet Hub](https://faucet.gno.land)\n//\n// \u003c/gno-columns\u003e\n//\n// \u003cgno-columns\u003e\n//\n// ## [Latest Blogposts](/r/gnoland/blog)\n//\n// No posts.\n//\n// \u003cgno-columns-sep\u003e\n//\n// ## [Latest Events](/events)\n//\n// No events.\n//\n// \u003c/gno-columns\u003e\n//\n// ---\n//\n// ## [Gno Playground](https://play.gno.land)\n//\n// Gno Playground is a web application designed for building, running, testing, and\n// interacting with your Gno code, enhancing your understanding of the Gno\n// language. With Gno Playground, you can share your code, execute tests, deploy\n// your realms and packages to Gno.land, and explore a multitude of other features.\n//\n// Experience the convenience of code sharing and rapid experimentation with\n// [Gno Playground](https://play.gno.land).\n//\n// ---\n//\n// ## Explore New Packages and Realms\n//\n// All code in Gno.land is organized in packages, and each package lives at a unique package path like\n// \"r/gnoland/home\". You can browse packages, inspect their source, and use them in your own libraries and realms.\n//\n// \u003cgno-columns\u003e\n//\n// ### r/gnoland\n//\n// Official realm packages developed by the Gno.land core team.\n//\n// [Browse](/r/gnoland)\n//\n// \u003cgno-columns-sep\u003e\n//\n// ### r/sys\n//\n// System-level realm packages used by the chain.\n//\n// [Browse](/r/sys)\n//\n// \u003cgno-columns-sep\u003e\n//\n// ### r/demo\n//\n// Demo realm packages showcasing what’s possible.\n//\n// [Browse](/r/demo)\n//\n// \u003cgno-columns-sep\u003e\n//\n// ### p/demo\n//\n// Pure packages for demo purposes.\n//\n// [Browse](/p/demo)\n//\n// \u003c/gno-columns\u003e\n//\n// ---\n//\n// \u003cgno-columns\u003e\n//\n// ## Socials\n//\n// - Check out our [community projects](https://github.com/gnolang/awesome-gno)\n// - [Discord](https://discord.gg/S8nKUqwkPn)\n// - [Twitter](https://twitter.com/_gnoland)\n// - [Youtube](https://www.youtube.com/@_gnoland)\n// - [Telegram](https://t.me/gnoland)\n//\n// \u003cgno-columns-sep\u003e\n//\n// ## Quote of the ~Day~ Block #123\n//\n// \u003e Now, you Gno.\n//\n// \u003c/gno-columns\u003e\n//\n// ---\n//\n// ## Sign up for our newsletter\n//\n// Stay in the Gno by signing up for our newsletter. You'll get the scoop on dev updates, fresh content, and community news.\n//\n// [![Subscribe to stay in the Gno](data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNTYiIGhlaWdodD0iNDQiIHZpZXdCb3g9IjAgMCAyNTYgNDQiPjxzdHlsZT50ZXh0e2ZvbnQtZmFtaWx5OnNhbnMtc2VyaWY7Zm9udC1zaXplOjE0cHg7dGV4dC1hbmNob3I6bWlkZGxlO2RvbWluYW50LWJhc2VsaW5lOm1pZGRsZTt9PC9zdHlsZT48cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMjU2IiBoZWlnaHQ9IjQ0IiByeD0iOCIgcnk9IjgiIGZpbGw9IiMyMjZjNTciIC8+PHRleHQgeD0iMTI4IiB5PSIyMiIgZHg9IjAiIGR5PSIwIiByb3RhdGU9IiIgZmlsbD0iI2ZmZmZmZiIgPlN1YnNjcmliZSB0byBzdGF5IGluIHRoZSBHbm88L3RleHQ+PC9zdmc+)](https://land.us18.list-manage.com/subscribe?u=8befe3303cf82796d2c1a1aff\u0026id=271812000b)\n//\n// ---\n//\n// **This is a testnet.** Package names are not guaranteed to be available for production.\n"},{"name":"override_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/home/filetests/override_filetest\n\npackage override_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnoland/home\"\n)\n\nfunc main(cur realm) {\n\tvar admin = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\ttesting.SetOriginCaller(admin)\n\thome.AdminSetOverride(cross(cur), \"Hello World!\")\n\tprintln(\"---\")\n\tprintln(home.Render(\"\"))\n\n\tnewAdmin := testutils.TestAddress(\"newAdmin\")\n\thome.AdminTransferOwnership(cross(cur), newAdmin)\n\tif err := revive(func() {\n\t\thome.AdminSetOverride(cross(cur), \"Not admin anymore\")\n\t}); err == nil {\n\t\tpanic(\"AdminSetOverride should have aborted the transaction\")\n\t}\n}\n\n// Output:\n// ---\n// Hello World!\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"EQZMenOJGO2ZM8Atmu8ykZ1F5gTK1NxKMGWBftWt9iVgalrCT/1nJ1AcI8cnRImShm76JUSMhAItnQQJ1073DA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"wugnot","path":"gno.land/r/gnoland/wugnot","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/wugnot\"\ngno = \"0.9\"\n"},{"name":"wugnot.gno","body":"package wugnot\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n)\n\nvar (\n\tToken *grc20.Token\n\tadm   *grc20.PrivateLedger\n)\n\nconst (\n\tugnotMinDeposit  int64 = 1000\n\twugnotMinDeposit int64 = 1\n)\n\nfunc init(cur realm) {\n\tToken, adm = grc20.NewToken(0, cur, \"wrapped GNOT\", \"wugnot\", 0)\n\tgrc20reg.Register(cross(cur), Token, \"\")\n}\n\nfunc Deposit(cur realm) {\n\t// Prevent cross-realm MITM: without this, an intermediary could\n\t// deposit on behalf of the caller and mint wugnot to itself\n\t// instead of the actual sender.\n\truntime.AssertOriginCall()\n\tcaller := cur.Previous().Address()\n\tsent := unsafe.OriginSend()\n\tamount := sent.AmountOf(\"ugnot\")\n\n\trequire(int64(amount) \u003e= ugnotMinDeposit, ufmt.Sprintf(\"Deposit below minimum: %d/%d ugnot.\", amount, ugnotMinDeposit))\n\n\tcheckErr(adm.Mint(caller, int64(amount)))\n}\n\nfunc Withdraw(cur realm, amount int64) {\n\truntime.AssertOriginCall()\n\trequire(amount \u003e= wugnotMinDeposit, ufmt.Sprintf(\"Deposit below minimum: %d/%d wugnot.\", amount, wugnotMinDeposit))\n\n\tcaller := cur.Previous().Address()\n\tpkgaddr := cur.Address()\n\tcallerBal := Token.BalanceOf(caller)\n\trequire(amount \u003c= callerBal, ufmt.Sprintf(\"Insufficient balance: %d available, %d needed.\", callerBal, amount))\n\n\t// send swapped ugnots to qcaller\n\tstdBanker := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tsend := chain.Coins{{\"ugnot\", int64(amount)}}\n\tstdBanker.SendCoins(pkgaddr, caller, send)\n\tcheckErr(adm.Burn(caller, amount))\n}\n\nfunc Render(path string) string {\n\tparts := strings.Split(path, \"/\")\n\tc := len(parts)\n\n\tswitch {\n\tcase path == \"\":\n\t\treturn Token.RenderHome()\n\tcase c == 2 \u0026\u0026 parts[0] == \"balance\":\n\t\towner := address(parts[1])\n\t\tbalance := Token.BalanceOf(owner)\n\t\treturn ufmt.Sprintf(\"%d\", balance)\n\tdefault:\n\t\treturn \"404\"\n\t}\n}\n\nfunc TotalSupply() int64 {\n\treturn Token.TotalSupply()\n}\n\nfunc BalanceOf(owner address) int64 {\n\treturn Token.BalanceOf(owner)\n}\n\nfunc Allowance(owner, spender address) int64 {\n\treturn Token.Allowance(owner, spender)\n}\n\nfunc Transfer(cur realm, to address, amount int64) {\n\tuserTeller := Token.CallerTeller()\n\tcheckErr(userTeller.Transfer(0, cur, to, amount))\n}\n\nfunc Approve(cur realm, spender address, amount int64) {\n\tuserTeller := Token.CallerTeller()\n\tcheckErr(userTeller.Approve(0, cur, spender, amount))\n}\n\nfunc TransferFrom(cur realm, from, to address, amount int64) {\n\tuserTeller := Token.CallerTeller()\n\tcheckErr(userTeller.TransferFrom(0, cur, from, to, amount))\n}\n\nfunc require(condition bool, msg string) {\n\tif !condition {\n\t\tpanic(msg)\n\t}\n}\n\nfunc checkErr(err error) {\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"z0_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"fmt\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnoland/wugnot\"\n)\n\nvar (\n\taddr1 = testutils.TestAddress(\"test1\")\n\taddrc = chain.PackageAddress(\"gno.land/r/gnoland/wugnot\")\n)\n\nfunc main(cur realm) {\n\t// issue ugnots\n\ttesting.IssueCoins(addr1, chain.Coins{{\"ugnot\", 100000001}})\n\tprintBalances()\n\t// println(wugnot.Render(\"queues\"))\n\t// println(\"A -\", wugnot.Render(\"\"))\n\n\t// deposit of 123400ugnot from addr1\n\t// origin send must be simulated\n\tcoins := chain.Coins{{\"ugnot\", 123_400}}\n\ttesting.SetOriginCaller(addr1)\n\ttesting.SetOriginSend(coins)\n\tbanker.NewBanker(banker.BankerTypeRealmSend, cur).SendCoins(addr1, addrc, coins)\n\twugnot.Deposit(cross(cur))\n\tprintBalances()\n\n\t// withdraw of 4242ugnot to addr1\n\twugnot.Withdraw(cross(cur), 4242)\n\tprintBalances()\n}\n\nfunc printBalances() {\n\tprintSingleBalance := func(name string, addr address) {\n\t\twugnotBal := wugnot.BalanceOf(addr)\n\t\ttesting.SetOriginCaller(addr)\n\t\trobanker := banker.NewReadonlyBanker()\n\t\tcoins := robanker.GetCoins(addr).AmountOf(\"ugnot\")\n\t\tfmt.Printf(\"| %-13s | addr=%s | wugnot=%-6d | ugnot=%-9d |\\n\",\n\t\t\tname, addr, wugnotBal, coins)\n\t}\n\tprintln(\"-----------\")\n\tprintSingleBalance(\"wugnot\", addrc)\n\tprintSingleBalance(\"addr1\", addr1)\n\tprintln(\"-----------\")\n}\n\n// Output:\n// -----------\n// | wugnot        | addr=g15vj5q08amlvyd0nx6zjgcvwq2d0gt9fcchrvum | wugnot=0      | ugnot=0         |\n// | addr1         | addr=g1w3jhxap3ta047h6lta047h6lta047h6l4mfnm7 | wugnot=0      | ugnot=100000001 |\n// -----------\n// -----------\n// | wugnot        | addr=g15vj5q08amlvyd0nx6zjgcvwq2d0gt9fcchrvum | wugnot=0      | ugnot=123400    |\n// | addr1         | addr=g1w3jhxap3ta047h6lta047h6lta047h6l4mfnm7 | wugnot=123400 | ugnot=99876601  |\n// -----------\n// -----------\n// | wugnot        | addr=g15vj5q08amlvyd0nx6zjgcvwq2d0gt9fcchrvum | wugnot=0      | ugnot=119158    |\n// | addr1         | addr=g1w3jhxap3ta047h6lta047h6lta047h6l4mfnm7 | wugnot=119158 | ugnot=99880843  |\n// -----------\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"XqM7wHnfvKCVthaQdiQ5noAAQSXbNn0St/vZvOyeCOECDPhEcZwtd2bbPRTo4FqGDE/RdRuIkr4p2WZYiSBMCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da","package":{"name":"memberstore","path":"gno.land/r/gov/dao/v3/memberstore","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gov/dao/v3/memberstore\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da\"\n"},{"name":"memberstore.gno","body":"package memberstore\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/demo/svg\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/gov/dao\"\n)\n\nvar (\n\tmembers MembersByTier\n\ttiers   TiersByName // private to prevent external modification\n\trouter  *mux.Router\n)\n\nconst (\n\tT1 = \"T1\"\n\tT2 = \"T2\"\n\tT3 = \"T3\"\n)\n\nfunc init() {\n\tmembers = NewMembersByTier()\n\n\ttiers = TiersByName{bptree.NewBPTree32()}\n\ttiers.Set(T1, Tier{\n\t\tInvitationPoints: 3,\n\t\tMinSize: func(membersByTier MembersByTier, tiersByName TiersByName) int {\n\t\t\treturn 70\n\t\t},\n\t\tMaxSize: func(membersByTier MembersByTier, tiersByName TiersByName) int {\n\t\t\treturn 0\n\t\t},\n\t\tBasePower: 3,\n\t\tPowerHandler: func(membersByTier MembersByTier, tiersByName TiersByName) float64 {\n\t\t\treturn 3\n\t\t},\n\t})\n\n\ttiers.Set(T2, Tier{\n\t\tInvitationPoints: 2,\n\t\tMaxSize: func(membersByTier MembersByTier, tiersByName TiersByName) int {\n\t\t\treturn membersByTier.GetTierSize(T1) * 2\n\t\t},\n\t\tMinSize: func(membersByTier MembersByTier, tiersByName TiersByName) int {\n\t\t\treturn membersByTier.GetTierSize(T1) / 4\n\t\t},\n\t\tBasePower: 2,\n\t\tPowerHandler: func(membersByTier MembersByTier, tiersByName TiersByName) float64 {\n\t\t\tt1ms := float64(membersByTier.GetTierSize(T1))\n\t\t\tt1, _ := tiersByName.GetTier(T1)\n\t\t\tt2ms := float64(membersByTier.GetTierSize(T2))\n\t\t\tt2, _ := tiersByName.GetTier(T2)\n\n\t\t\tt1p := t1.BasePower * t1ms\n\t\t\tt2p := t2.BasePower * t2ms\n\n\t\t\t// capped to 2/3 of tier 1\n\t\t\tt1ptreshold := t1p * (2.0 / 3.0)\n\t\t\tif t2p \u003e t1ptreshold {\n\t\t\t\treturn t1ptreshold / t2ms\n\t\t\t}\n\n\t\t\treturn t2.BasePower\n\t\t},\n\t})\n\n\ttiers.Set(T3, Tier{\n\t\tInvitationPoints: 1,\n\t\tMaxSize: func(membersByTier MembersByTier, tiersByName TiersByName) int {\n\t\t\treturn 0\n\t\t},\n\t\tMinSize: func(membersByTier MembersByTier, tiersByName TiersByName) int {\n\t\t\treturn 0\n\t\t},\n\t\tBasePower: 1,\n\t\tPowerHandler: func(membersByTier MembersByTier, tiersByName TiersByName) float64 {\n\t\t\tt1ms := float64(membersByTier.GetTierSize(T1))\n\t\t\tt1, _ := tiersByName.GetTier(T1)\n\t\t\tt3ms := float64(membersByTier.GetTierSize(T3))\n\t\t\tt3, _ := tiersByName.GetTier(T3)\n\n\t\t\tt1p := t1.BasePower * t1ms\n\t\t\tt3p := t3.BasePower * t3ms\n\n\t\t\t// capped to 1/3 of tier 1\n\t\t\tt1ptreshold := t1p * (1.0 / 3.0)\n\t\t\tif t3p \u003e t1ptreshold {\n\t\t\t\treturn t1ptreshold / t3ms\n\t\t\t}\n\n\t\t\treturn t3.BasePower\n\t\t},\n\t})\n\n\tinitRouter()\n}\n\n// initRouter initializes the router for the memberstore.\nfunc initRouter() {\n\trouter = mux.NewRouter()\n\trouter.HandleFunc(\"\", renderHome)\n\trouter.HandleFunc(\"members\", renderMembers)\n\trouter.NotFoundHandler = renderNotFound\n}\n\n// renderHome displays the tiers data (Number of members and powers) and tiers charts.\nfunc renderHome(res *mux.ResponseWriter, req *mux.Request) {\n\tvar sb strings.Builder\n\tsb.WriteString(md.Link(\"\u003e Go to Members list \u003c\", \"/r/gov/dao/v3/memberstore:members\") + \"\\n\")\n\n\tmembers.Iterate(\"\", \"\", func(tn string, ti interface{}) bool {\n\t\ttree, ok := ti.(*bptree.BPTree)\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\n\t\ttier, ok := tiers.GetTier(tn)\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\n\t\ttp := (tier.PowerHandler(members, tiers) * float64(members.GetTierSize(tn)))\n\n\t\tsb.WriteString(ufmt.Sprintf(\"- %v Tier %v contains %v members with power: %v\\n\", tierColoredChip(tn), tn, tree.Size(), tp))\n\n\t\treturn false\n\t})\n\n\tsb.WriteString(\"\\n\" + RenderCharts(members))\n\tres.Write(sb.String())\n}\n\n// renderMembers displays the members list.\nfunc renderMembers(res *mux.ResponseWriter, req *mux.Request) {\n\tpath := strings.Replace(req.RawPath, \"members\", \"\", 1) // We have to clean the path\n\tres.Write(RenderMembers(path, members))\n}\n\nfunc renderNotFound(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(\"# 404\\n\\nThat page was not found. Would you like to [**go home**?](/r/gov/dao/v3/memberstore)\")\n}\n\nfunc tierColor(tn string) string {\n\tswitch tn {\n\tcase T1:\n\t\treturn \"#329175\"\n\tcase T2:\n\t\treturn \"#21577A\"\n\tcase T3:\n\t\treturn \"#F3D3BC\"\n\tdefault:\n\t\treturn \"#FFF\"\n\t}\n}\n\n// tierColoredChip returns a colored chip svg for the given tier name.\nfunc tierColoredChip(tn string) string {\n\tcanvas := svg.NewCanvas(16, 16)\n\tcanvas.Append(svg.NewRectangle(0, 0, 16, 16, tierColor(tn)))\n\treturn canvas.Render(tn + \" colored chip\")\n}\n\nfunc Render(path string) string {\n\tvar sb strings.Builder\n\tsb.WriteString(md.H1(\"Memberstore Govdao v3\"))\n\tsb.WriteString(router.Render(path))\n\treturn sb.String()\n}\n\n// Get gets the Members store.\n//\n// rlm is the cur of an in-scope crossing frame, threaded by the caller.\n// The IsCurrent() check rejects stale or stashed realm values — a\n// malicious realm cannot replay an old cur to claim allowed-DAO\n// identity. After the check, rlm.PkgPath() is the authentic immediate\n// caller's realm.\nfunc Get(_ int, rlm realm) MembersByTier {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"memberstore.Get: rlm is not the caller's live cur (stale capture or sibling frame)\")\n\t}\n\tcurrealm := rlm.PkgPath()\n\tif !dao.InAllowedDAOs(currealm) {\n\t\tpanic(\"this Realm is not allowed to get the Members data: \" + currealm)\n\t}\n\n\treturn members\n}\n\n// GetTier returns a tier by name. This is a read-only accessor.\nfunc GetTier(name string) (Tier, bool) {\n\treturn tiers.GetTier(name)\n}\n\n// IterateTiers iterates over all tiers in order. This is a read-only accessor.\n// The callback receives the tier name and tier data.\n// Return true from the callback to stop iteration.\nfunc IterateTiers(fn func(name string, tier Tier) bool) {\n\ttiers.Iterate(\"\", \"\", func(name string, value interface{}) bool {\n\t\ttier, ok := value.(Tier)\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\t\treturn fn(name, tier)\n\t})\n}\n\n// setTiers replaces the tiers configuration.\n// This is internal and should only be called via governance proposal execution.\nfunc setTiers(newTiers TiersByName) {\n\ttiers = newTiers\n}\n\n// GetTierPower calculates the effective voting power for a tier given the current members.\n// This is a safe accessor that uses the internal tiers configuration.\nfunc GetTierPower(tierName string, members MembersByTier) float64 {\n\ttier, ok := tiers.GetTier(tierName)\n\tif !ok {\n\t\treturn 0\n\t}\n\treturn tier.PowerHandler(members, tiers)\n}\n"},{"name":"memberstore_test.gno","body":"package memberstore\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestPower(t *testing.T) {\n\tms := NewMembersByTier()\n\taddMembers(ms, 100, T1)\n\taddMembers(ms, 100, T2)\n\taddMembers(ms, 100, T3)\n\n\ttiers.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tpo := value.(Tier).PowerHandler(ms, tiers)\n\t\tif key == T1 \u0026\u0026 po != 3.0 {\n\t\t\tt.Fatal(\"wrong value for T1\")\n\t\t}\n\t\tif key == T2 \u0026\u0026 po != 2.0 {\n\t\t\tt.Fatal(\"wrong value for T2\")\n\t\t}\n\t\tif key == T3 \u0026\u0026 po != 1.0 {\n\t\t\tt.Fatal(\"wrong value for T3\")\n\t\t}\n\n\t\treturn false\n\t})\n\n\tms = NewMembersByTier()\n\taddMembers(ms, 100, T1)\n\taddMembers(ms, 50, T2)\n\taddMembers(ms, 10, T3)\n\n\ttiers.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tpo := value.(Tier).PowerHandler(ms, tiers)\n\t\tif key == T1 \u0026\u0026 po != 3.0 {\n\t\t\tt.Fatal(\"wrong value for T1\")\n\t\t}\n\t\tif key == T2 \u0026\u0026 po != 2.0 {\n\t\t\tt.Fatal(\"wrong value for T2\")\n\t\t}\n\t\tif key == T3 \u0026\u0026 po != 1.0 {\n\t\t\tt.Fatal(\"wrong value for T3\")\n\t\t}\n\n\t\treturn false\n\t})\n\n\tms = NewMembersByTier()\n\taddMembers(ms, 100, T1)\n\taddMembers(ms, 200, T2)\n\taddMembers(ms, 100, T3)\n\n\ttiers.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tpo := value.(Tier).PowerHandler(ms, tiers)\n\t\tif key == T1 \u0026\u0026 po != 3.0 {\n\t\t\tt.Fatal(\"wrong value for T1\")\n\t\t}\n\t\tif key == T2 \u0026\u0026 po != 1.0 {\n\t\t\tt.Fatal(\"wrong value for T2\")\n\t\t}\n\t\tif key == T3 \u0026\u0026 po != 1.0 {\n\t\t\tt.Fatal(\"wrong value for T3\")\n\t\t}\n\n\t\treturn false\n\t})\n\n\tms = NewMembersByTier()\n\taddMembers(ms, 100, T1)\n\taddMembers(ms, 200, T2)\n\taddMembers(ms, 1000, T3)\n\n\ttiers.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tpo := value.(Tier).PowerHandler(ms, tiers)\n\t\tif key == T1 \u0026\u0026 po != 3.0 {\n\t\t\tt.Fatal(\"wrong value for T1\")\n\t\t}\n\t\tif key == T2 \u0026\u0026 po != 1.0 {\n\t\t\tt.Fatal(\"wrong value for T2\")\n\t\t}\n\t\tif key == T3 \u0026\u0026 po != 0.1 {\n\t\t\tt.Fatal(\"wrong value for T3\")\n\t\t}\n\n\t\treturn false\n\t})\n}\n\nfunc TestCreateMembers(t *testing.T) {\n\tms := NewMembersByTier()\n\tprintln(\"adding members...\")\n\taddMembers(ms, 10, \"T1\")\n\tprintln(\"added T1\")\n\taddMembers(ms, 100, \"T2\")\n\tprintln(\"added T2\")\n\taddMembers(ms, 1000, \"T3\")\n\tprintln(\"added T3\")\n\n\tm, tier := ms.GetMember(address(\"11T3\"))\n\turequire.Equal(t, \"T3\", tier)\n\n\tm, tier = ms.GetMember(address(\"2000T1\"))\n\turequire.Equal(t, \"\", tier)\n\tif m != nil {\n\t\tt.Fatal(\"member must be nil if not found\")\n\t}\n\n\ttier = ms.RemoveMember(address(\"1T1\"))\n\turequire.Equal(t, \"T1\", tier)\n}\n\nfunc addMembers(ms MembersByTier, c int, tier string) {\n\t// mt := avl.NewTree() XXX\n\tms.SetTier(tier)\n\tfor i := 0; i \u003c c; i++ {\n\t\taddr := address(strconv.Itoa(i) + tier)\n\t\tif err := ms.SetMember(tier, addr, \u0026Member{}); err != nil {\n\t\t\tpanic(err.Error())\n\t\t}\n\t}\n}\n"},{"name":"prop_requests.gno","body":"package memberstore\n\nimport (\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\n\t\"gno.land/r/gov/dao\"\n)\n\nfunc NewChangeTiersRequest(cur realm, tiers map[string]Tier) dao.ProposalRequest {\n\tif len(tiers) == 0 {\n\t\tpanic(\"tiers list is empty\")\n\t}\n\n\tmember, _ := Get(0, cur).GetMember(unsafe.OriginCaller())\n\tif member == nil {\n\t\tpanic(\"proposer is not a member\")\n\t}\n\n\tnewTiers := TiersByName{bptree.NewBPTree32()}\n\tfor name, tier := range tiers {\n\t\tnewTiers.Set(name, tier)\n\t}\n\n\tcallback := func(cur realm) error {\n\t\tsetTiers(newTiers)\n\n\t\treturn nil\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, callback, \"New set of tiers proposed.\")\n\n\treturn dao.NewProposalRequest(\"Change Tiers Proposal\", \"This proposal is looking to change the existing Tiers in memberstore\", e)\n}\n"},{"name":"rendercharts.gno","body":"package memberstore\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/samcrew/piechart\"\n)\n\n// RenderCharts generates two pie charts for member tiers:\n// 1) distribution of member counts per tier\n// 2) distribution of power per tier\nfunc RenderCharts(members MembersByTier) string {\n\tvar sb strings.Builder\n\n\ttierNames := []string{T1, T2, T3}\n\tpieSlicesTs := make([]piechart.PieSlice, 0, len(tierNames))\n\tpieSlicesTp := make([]piechart.PieSlice, 0, len(tierNames))\n\n\tfor _, tn := range tierNames {\n\t\ttier, ok := tiers.GetTier(tn)\n\t\tif !ok {\n\t\t\treturn \"\"\n\t\t}\n\n\t\tts := float64(members.GetTierSize(tn))\n\t\ttp := tier.PowerHandler(members, tiers) * ts\n\n\t\tpieSlicesTs = append(pieSlicesTs, piechart.PieSlice{\n\t\t\tValue: ts,\n\t\t\tColor: tierColor(tn),\n\t\t\tLabel: tn,\n\t\t})\n\t\tpieSlicesTp = append(pieSlicesTp, piechart.PieSlice{\n\t\t\tValue: tp,\n\t\t\tColor: tierColor(tn),\n\t\t\tLabel: tn,\n\t\t})\n\t}\n\n\t// Render pie charts for members count and power distribution\n\tresultPieChartTs := piechart.Render(pieSlicesTs, \"Members distribution:\")\n\tresultPieChartTp := piechart.Render(pieSlicesTp, \"Power distribution:\")\n\n\tsb.WriteString(resultPieChartTs + \"\\n\")\n\tsb.WriteString(resultPieChartTp + \"\\n\")\n\n\treturn sb.String()\n}\n"},{"name":"rendermembers.gno","body":"package memberstore\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/bptree/v0/pager\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/samcrew/tablesort\"\n\t\"gno.land/p/samcrew/urlfilter\"\n)\n\n// RenderMembers returns the members list with tier filters and pagination.\nfunc RenderMembers(path string, members MembersByTier) string {\n\tu, _ := url.Parse(path)\n\tmdFilters, items := urlfilter.ApplyFilters(u, members.BPTree, \"filter\")\n\tvar sb strings.Builder\n\n\tsb.WriteString(md.Link(\"\u003e Go to Tiers summary \u003c\", \"/r/gov/dao/v3/memberstore\") + \"\\n\\n\")\n\tsb.WriteString(md.Bold(\"Filter members by tiers:\"))\n\tsb.WriteString(mdFilters + \"\\n\")\n\n\tconst pageSize = 14\n\tpager := pager.NewPager(items, pageSize, false)\n\tpage := pager.MustGetPageByPath(path)\n\n\tsb.WriteString(renderMembersPages(u, page, items) + \"\\n\")\n\tsb.WriteString(renderPagination(u, page))\n\n\treturn sb.String()\n}\n\n// renderMembersPages returns the members of each page.\nfunc renderMembersPages(u *url.URL, page *pager.Page, members *bptree.BPTree) string {\n\tvar sb strings.Builder\n\n\ttable := \u0026tablesort.Table{\n\t\tHeadings: []string{\"Tier\", \"Address\"},\n\t\tRows:     [][]string{},\n\t}\n\n\tfor _, item := range page.Items {\n\t\taddr := item.Key\n\t\ttn := members.Get(addr)\n\t\ttnStr, _ := tn.(string)\n\t\ttierCell := ufmt.Sprintf(\"%s %s\", tierColoredChip(tnStr), tn)\n\t\ttable.Rows = append(table.Rows, []string{tierCell, addr})\n\t}\n\n\tsb.WriteString(tablesort.Render(u, table, \"\"))\n\n\treturn sb.String()\n}\n\n// renderPagination returns the pagination UI for the current page.\nfunc renderPagination(u *url.URL, page *pager.Page) string {\n\tq := u.Query()\n\tq.Del(\"page\")\n\tu.RawQuery = q.Encode()\n\n\tvar sb strings.Builder\n\tsb.WriteString(page.Picker(u.String()))\n\n\treturn sb.String()\n}\n"},{"name":"types.gno","body":"package memberstore\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\ntype ErrMemberAlreadyExists struct {\n\tTier string\n}\n\nfunc (e *ErrMemberAlreadyExists) Error() string {\n\treturn \"member already exists on tier \" + e.Tier\n}\n\ntype Member struct {\n\tInvitationPoints int\n}\n\nfunc NewMember(invitationPoints int) *Member {\n\treturn \u0026Member{InvitationPoints: invitationPoints}\n}\n\nfunc (m *Member) RemoveInvitationPoint() {\n\tif m.InvitationPoints \u003c= 0 {\n\t\tpanic(\"not enough invitation points\")\n\t}\n\n\tm.InvitationPoints = m.InvitationPoints - 1\n}\n\n// MembersByTier contains all `Member`s indexed by their Address.\ntype MembersByTier struct {\n\t*bptree.BPTree // tier name -\u003e address -\u003e member\n}\n\nfunc NewMembersByTier() MembersByTier {\n\treturn MembersByTier{BPTree: bptree.NewBPTree32()}\n}\n\nfunc (mbt MembersByTier) DeleteAll() {\n\tmbt.Iterate(\"\", \"\", func(tn string, msv interface{}) bool {\n\t\tmbt.Remove(tn)\n\t\treturn false\n\t})\n}\n\nfunc (mbt MembersByTier) SetTier(tier string) error {\n\tif ok := mbt.Has(tier); ok {\n\t\treturn errors.New(\"tier already exist: \" + tier)\n\t}\n\n\tmbt.Set(tier, bptree.NewBPTree32())\n\n\treturn nil\n}\n\n// GetTierSize tries to get how many members are on the specified tier. If the tier does not exists, it returns 0.\nfunc (mbt MembersByTier) GetTierSize(tn string) int {\n\ttv := mbt.Get(tn)\n\tif tv == nil {\n\t\treturn 0\n\t}\n\n\ttree, ok := tv.(*bptree.BPTree)\n\tif !ok {\n\t\treturn 0\n\t}\n\n\treturn tree.Size()\n}\n\n// SetMember adds a new member to the specified tier. The tier index is created on the fly if it does not exists.\nfunc (mbt MembersByTier) SetMember(tier string, addr address, member *Member) error {\n\t_, t := mbt.GetMember(addr)\n\tif t != \"\" {\n\t\treturn \u0026ErrMemberAlreadyExists{Tier: t}\n\t}\n\n\tif ok := mbt.Has(tier); !ok {\n\t\treturn errors.New(\"tier does not exist: \" + tier)\n\t}\n\n\tms := mbt.Get(tier)\n\tmst := ms.(*bptree.BPTree)\n\n\tmst.Set(string(addr), member)\n\n\treturn nil\n}\n\n// GetMember iterate over all tiers to try to find a member by its address. The tier ID is also returned if the Member is found.\nfunc (mbt MembersByTier) GetMember(addr address) (m *Member, t string) {\n\tmbt.Iterate(\"\", \"\", func(tn string, msv interface{}) bool {\n\t\tmst, ok := msv.(*bptree.BPTree)\n\t\tif !ok {\n\t\t\tpanic(\"MembersByTier values can only be bptree.BPTree\")\n\t\t}\n\n\t\tmv := mst.Get(string(addr))\n\t\tif mv == nil {\n\t\t\treturn false\n\t\t}\n\n\t\tmm, ok := mv.(*Member)\n\t\tif !ok {\n\t\t\tpanic(\"MembersByTier values can only be *Member\")\n\t\t}\n\n\t\tm = mm\n\t\tt = tn\n\n\t\treturn true\n\t})\n\n\treturn\n}\n\n// RemoveMember removes a member from any tier\nfunc (mbt MembersByTier) RemoveMember(addr address) (t string) {\n\tmbt.Iterate(\"\", \"\", func(tn string, msv interface{}) bool {\n\t\tmst, ok := msv.(*bptree.BPTree)\n\t\tif !ok {\n\t\t\tpanic(\"MembersByTier values can only be bptree.BPTree\")\n\t\t}\n\n\t\t_, removed := mst.Remove(string(addr))\n\t\tif removed {\n\t\t\tt = tn\n\t\t}\n\t\treturn removed\n\t})\n\n\treturn\n}\n\n// GetTotalPower obtains the total voting power from all the specified tiers.\nfunc (mbt MembersByTier) GetTotalPower() float64 {\n\tvar out float64\n\tmbt.Iterate(\"\", \"\", func(tn string, msv interface{}) bool {\n\t\ttier, ok := tiers.GetTier(tn)\n\t\tif !ok {\n\t\t\t// tier does not exists, so we cannot count power from this tier\n\t\t\treturn false\n\t\t}\n\n\t\tout = out + (tier.PowerHandler(mbt, tiers) * float64(mbt.GetTierSize(tn)))\n\n\t\treturn false\n\t})\n\n\treturn out\n}\n\ntype Tier struct {\n\t// BasePower defines the standard voting power for the members on this tier.\n\tBasePower float64\n\n\t// InvitationPoints defines how many invitation points users on that tier will receive.\n\tInvitationPoints int\n\n\t// MaxSize calculates the max amount of members expected to be on this tier.\n\tMaxSize func(membersByTier MembersByTier, tiersByName TiersByName) int\n\n\t// MinSize calculates the min amount of members expected to be on this tier.\n\tMinSize func(membersByTier MembersByTier, tiersByName TiersByName) int\n\n\t// PowerHandler calculates what is the final power of this tier after taking into account Members by other tiers.\n\tPowerHandler func(membersByTier MembersByTier, tiersByName TiersByName) float64\n}\n\n// TiersByName contains all tier objects indexed by its name.\ntype TiersByName struct {\n\t*bptree.BPTree // *bptree.BPTree[string]Tier\n}\n\n// GetTier obtains a Tier struct by its name. It returns false if the Tier is not found.\nfunc (tbn TiersByName) GetTier(tn string) (Tier, bool) {\n\tval := tbn.Get(tn)\n\tif val == nil {\n\t\treturn Tier{}, false\n\t}\n\n\tt, ok := val.(Tier)\n\tif !ok {\n\t\tpanic(\"TiersByName must contains only Tier types\")\n\t}\n\n\treturn t, true\n}\n"},{"name":"z0_filetest.gno","body":"// PKGPATH: gno.land/r/test/exploit\npackage exploit\n\nimport (\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nfunc main() {\n\t// After the fix, memberstore.Tiers is no longer accessible (lowercase 'tiers')\n\t// External realms can only use the safe accessor functions:\n\t// - memberstore.GetTier(name) - read-only tier access\n\t// - memberstore.IterateTiers(fn) - read-only iteration\n\t// - memberstore.GetTierPower(name, members) - calculated power\n\n\t// Verify we can still READ tier data via the safe accessor\n\tt3, ok := memberstore.GetTier(memberstore.T3)\n\tif !ok {\n\t\tpanic(\"T3 tier not found\")\n\t}\n\tprintln(\"T3 BasePower (read-only):\", t3.BasePower)\n\tprintln(\"T3 InvitationPoints (read-only):\", t3.InvitationPoints)\n\n\t// The following lines would cause a compile error if uncommented:\n\t// memberstore.Tiers.Set(...) // ERROR: Tiers is not exported (lowercase)\n\n\t// Iterate over tiers (read-only)\n\tprintln(\"All tiers:\")\n\tmemberstore.IterateTiers(func(name string, tier memberstore.Tier) bool {\n\t\tprintln(\"  -\", name, \"BasePower:\", tier.BasePower)\n\t\treturn false\n\t})\n\n\tprintln(\"Security fix verified: external realms cannot modify tiers\")\n}\n\n// Output:\n// T3 BasePower (read-only): 1\n// T3 InvitationPoints (read-only): 1\n// All tiers:\n//   - T1 BasePower: 3\n//   - T2 BasePower: 2\n//   - T3 BasePower: 1\n// Security fix verified: external realms cannot modify tiers\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"NZvttV3EtQOhH79cw74K0AaR2r7Tan+JRb9NKMhnak8mxjOpCY/fF92cLF5UrTsY8Q/LEo4ngo7dFD8mrqZyDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"treasury","path":"gno.land/r/gov/dao/v3/treasury","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gov/dao/v3/treasury\"\ngno = \"0.9\"\n"},{"name":"treasury.gno","body":"package treasury\n\nimport (\n\t\"chain/banker\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\tt \"gno.land/p/nt/treasury/v0\"\n\n\t\"gno.land/r/demo/defi/grc20reg\"\n\t\"gno.land/r/gov/dao\"\n)\n\nvar (\n\ttreasury  *t.Treasury\n\ttokenKeys = []string{\n\t\t// TODO: Add the default GRC20 tokens we want to support here.\n\t}\n)\n\nfunc init(cur realm) {\n\t// Define a token lister for the GRC20Banker.\n\t// For now, GovDAO uses a static list of tokens.\n\tgrc20Lister := func() map[string]*grc20.Token {\n\t\t// Get the GRC20 tokens from the registry.\n\t\ttokens := map[string]*grc20.Token{}\n\t\tfor _, key := range tokenKeys {\n\t\t\t// Get the token by its key.\n\t\t\ttoken := grc20reg.Get(key)\n\t\t\tif token != nil {\n\t\t\t\ttokens[key] = token\n\t\t\t}\n\t\t}\n\n\t\treturn tokens\n\t}\n\n\t// Init the treasury bankers.\n\tcoinsBanker, err := t.NewCoinsBankerWithOwner(cur.Address(), banker.NewBanker(banker.BankerTypeRealmSend, cur))\n\tif err != nil {\n\t\tpanic(\"failed to create CoinsBanker: \" + err.Error())\n\t}\n\tgrc20Banker, err := t.NewGRC20BankerWithOwner(cur.Address(), grc20Lister)\n\tif err != nil {\n\t\tpanic(\"failed to create GRC20Banker: \" + err.Error())\n\t}\n\tbankers := []t.Banker{\n\t\tcoinsBanker,\n\t\tgrc20Banker,\n\t}\n\n\t// Create the treasury instance with the bankers. cur.PkgPath() is\n\t// captured for render-link construction (See full history → /r/gov/dao/v3/treasury:.../history).\n\ttreasury, err = t.New(bankers, cur.PkgPath())\n\tif err != nil {\n\t\tpanic(\"failed to create treasury: \" + err.Error())\n\t}\n}\n\n// SetTokenKeys sets the GRC20 token registry keys that the treasury will use.\nfunc SetTokenKeys(cur realm, keys []string) {\n\tcaller := cur.Previous().PkgPath()\n\n\t// Check if the caller realm is allowed to set token keys.\n\tif !dao.InAllowedDAOs(caller) {\n\t\tpanic(\"this Realm is not allowed to send payment: \" + caller)\n\t}\n\n\ttokenKeys = keys\n}\n\n// Send sends a payment using the treasury instance.\nfunc Send(cur realm, payment t.Payment) {\n\tcaller := cur.Previous().PkgPath()\n\n\t// Check if the caller realm is allowed to send payments.\n\tif !dao.InAllowedDAOs(caller) {\n\t\tpanic(\"this Realm is not allowed to send payment: \" + caller)\n\t}\n\n\t// Send the payment using the treasury instance. cur is this realm's\n\t// captured cur — passes IsCurrent inside Banker.Send and matches the\n\t// banker's owner (this realm's address) registered at init.\n\tif err := treasury.Send(0, cur, payment); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// History returns the payment history sent by the banker with the given ID.\n// Payments are paginated, with the most recent payments first.\nfunc History(bankerID string, pageNumber int, pageSize int) []t.Payment {\n\thistory, err := treasury.History(bankerID, pageNumber, pageSize)\n\tif err != nil {\n\t\tpanic(\"failed to get history: \" + err.Error())\n\t}\n\n\treturn history\n}\n\n// Balances returns the balances of the banker with the given ID.\nfunc Balances(bankerID string) []t.Balance {\n\tbalances, err := treasury.Balances(bankerID)\n\tif err != nil {\n\t\tpanic(\"failed to get balances: \" + err.Error())\n\t}\n\n\treturn balances\n}\n\n// Address returns the address of the banker with the given ID.\nfunc Address(bankerID string) string {\n\taddr, err := treasury.Address(bankerID)\n\tif err != nil {\n\t\tpanic(\"failed to get address: \" + err.Error())\n\t}\n\n\treturn addr\n}\n\n// HasBanker checks if a banker with the given ID is registered.\nfunc HasBanker(bankerID string) bool {\n\treturn treasury.HasBanker(bankerID)\n}\n\n// ListBankerIDs returns a list of all registered banker IDs.\nfunc ListBankerIDs() []string {\n\treturn treasury.ListBankerIDs()\n}\n\nfunc Render(path string) string {\n\treturn treasury.Render(path)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"0XZ1k7lK76yiDartfUpGGtGA+QiiVgbzko7jAvOdz2M0LOFu8w+vP9/OrdjPDTfP6NCLHN095HxNgqnYIdRkDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da","package":{"name":"impl","path":"gno.land/r/gov/dao/v3/impl","files":[{"name":"filter.gno","body":"package impl\n\ntype FilterByTier struct {\n\tTier string\n}\n\nfunc NewFilterByTier(tier string) FilterByTier {\n\treturn FilterByTier{Tier: tier}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gov/dao/v3/impl\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da\"\n"},{"name":"govdao.gno","body":"package impl\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nvar ErrMemberNotFound = errors.New(\"member not found\")\n\ntype GovDAO struct {\n\tpss    ProposalsStatuses\n\trender *render\n}\n\nfunc NewGovDAO() *GovDAO {\n\tpss := NewProposalsStatuses()\n\td := \u0026GovDAO{\n\t\tpss: pss,\n\t}\n\n\td.render = NewRender(d)\n\n\t// Attach to package var (impl owns _govdao). Plain assignment is\n\t// fine — we're in impl's package, no realm transition needed.\n\t// TODO: replace with future attach().\n\t_govdao = d\n\n\treturn d\n}\n\n// Setting this to a global variable forces attaching the GovDAO struct to this\n// realm. TODO replace with future `attach()`.\nvar _govdao *GovDAO\n\nfunc (g *GovDAO) PreCreateProposal(_ int, rlm realm, r dao.ProposalRequest) (address, error) {\n\tif !g.isValidCall(0, rlm) {\n\t\treturn \"\", errors.New(ufmt.Sprintf(\"proposal creation must be done directly by a user or through the r/gov/dao proxy. caller realm: %v; caller's previous: %v\",\n\t\t\trlm, rlm.Previous()))\n\t}\n\n\t// Verify that the one creating the proposal is a member.\n\tcaller := unsafe.OriginCaller()\n\tmem, _ := getMembers(cross(rlm)).GetMember(caller)\n\tif mem == nil {\n\t\treturn caller, errors.New(\"only members can create new proposals\")\n\t}\n\n\treturn caller, nil\n}\n\nfunc (g *GovDAO) PostCreateProposal(_ int, rlm realm, r dao.ProposalRequest, pid dao.ProposalID) {\n\t// Tiers Allowed to Vote\n\ttatv := []string{memberstore.T1, memberstore.T2, memberstore.T3}\n\tswitch v := r.Filter().(type) {\n\tcase FilterByTier:\n\t\t// only members from T1 are allowed to vote when adding new members to T1\n\t\tif v.Tier == memberstore.T1 {\n\t\t\ttatv = []string{memberstore.T1}\n\t\t}\n\t\t// only members from T1 and T2 are allowed to vote when adding new members to T2\n\t\tif v.Tier == memberstore.T2 {\n\t\t\ttatv = []string{memberstore.T1, memberstore.T2}\n\t\t}\n\t}\n\tg.pss.Set(pid.String(), newProposalStatus(tatv))\n}\n\nfunc (g *GovDAO) VoteOnProposal(_ int, rlm realm, r dao.VoteRequest) error {\n\tif !g.isValidCall(0, rlm) {\n\t\treturn errors.New(\"proposal voting must be done directly by a user\")\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\tmem, tie := getMembers(cross(rlm)).GetMember(caller)\n\tif mem == nil {\n\t\treturn ErrMemberNotFound\n\t}\n\n\tstatus := g.pss.GetStatus(r.ProposalID)\n\tif status == nil {\n\t\treturn errors.New(\"proposal not found\")\n\t}\n\n\tif status.Denied || status.Accepted {\n\t\treturn errors.New(ufmt.Sprintf(\"proposal closed. Accepted: %v\", status.Accepted))\n\t}\n\n\tif !status.IsAllowed(tie) {\n\t\treturn errors.New(\"member on specified tier is not allowed to vote on this proposal\")\n\t}\n\n\tmVoted, _ := status.AllVotes.GetMember(caller)\n\tif mVoted != nil {\n\t\treturn errors.New(\"already voted on proposal\")\n\t}\n\n\tswitch r.Option {\n\tcase dao.YesVote:\n\t\tstatus.AllVotes.SetMember(tie, caller, mem)\n\t\tstatus.YesVotes.SetMember(tie, caller, mem)\n\tcase dao.NoVote:\n\t\tstatus.AllVotes.SetMember(tie, caller, mem)\n\t\tstatus.NoVotes.SetMember(tie, caller, mem)\n\tcase dao.AbstainVote:\n\t\tstatus.AllVotes.SetMember(tie, caller, mem)\n\t\tstatus.AbstainVotes.SetMember(tie, caller, mem)\n\tdefault:\n\t\treturn errors.New(\"voting can only be YES, NO, or ABSTAIN\")\n\t}\n\n\treturn nil\n}\n\nfunc (g *GovDAO) PreExecuteProposal(_ int, rlm realm, pid dao.ProposalID) (bool, error) {\n\tif !g.isValidCall(0, rlm) {\n\t\treturn false, errors.New(\"proposal execution must be done directly by a user\")\n\t}\n\tstatus := g.pss.GetStatus(pid)\n\tif status.Denied || status.Accepted {\n\t\treturn false, errors.New(ufmt.Sprintf(\"proposal already executed. Accepted: %v\", status.Accepted))\n\t}\n\n\tif status.YesPercent(0, rlm) \u003e= law.Supermajority {\n\t\tstatus.Accepted = true\n\t\treturn true, nil\n\t}\n\n\tif status.NoPercent(0, rlm) \u003e= law.Supermajority {\n\t\tstatus.Denied = true\n\t\treturn false, nil\n\t}\n\n\treturn false, errors.New(ufmt.Sprintf(\"proposal didn't reach supermajority yet: %v\", law.Supermajority))\n}\n\nfunc (g *GovDAO) ExecuteProposal(_ int, rlm realm, pid dao.ProposalID, e dao.Executor) error {\n\tif e == nil {\n\t\tpanic(\"an executor is required to execute the proposal\")\n\t}\n\n\tstatus := g.pss.GetStatus(pid)\n\tif status == nil {\n\t\tpanic(\"proposal not found\")\n\t}\n\n\terr := e.Execute(cross(rlm))\n\tif err != nil {\n\t\tstatus.Accepted = false\n\t\tstatus.Denied = true\n\t\tstatus.DeniedReason = \"execution failed: \" + err.Error()\n\t}\n\treturn err\n}\n\nfunc (g *GovDAO) Render(cur realm, pkgPath string, path string) string {\n\t// Same-realm dispatch: pass cur through as data (non-crossing).\n\treturn g.render.Render(0, cur, pkgPath, path)\n}\n\n// isValidCall verifies that the impl method is being invoked from the\n// r/gov/dao proxy via a legitimate user transaction (MsgCall or MsgRun).\n//\n// The proxy passes its own crossing-frame Cur as rlm when calling the\n// impl methods. rlm.IsCurrent() rejects stale or stashed realm values —\n// a malicious realm cannot replay a captured proxy cur to impersonate\n// the proxy. After the IsCurrent() check:\n//   - rlm.PkgPath() == \"gno.land/r/gov/dao\" identifies the proxy\n//     unforgeably (pkg path is set at mint time by installCrossingCur).\n//   - rlm.Previous() is the caller of the proxy.\n//\n// The proxy is the only legitimate entrypoint. The impl methods are\n// non-crossing and take rlm as a regular argument, so a direct user\n// MsgCall to them cannot supply a valid rlm: the IsCurrent() check\n// rejects any forged or stashed realm value.\nfunc (g *GovDAO) isValidCall(_ int, rlm realm) bool {\n\tif !rlm.IsCurrent() {\n\t\treturn false\n\t}\n\tif rlm.PkgPath() != \"gno.land/r/gov/dao\" {\n\t\treturn false\n\t}\n\tprev := rlm.Previous()\n\t// MsgCall: proxy was called directly by an EOA (UserRealm).\n\tif prev.IsUser() {\n\t\treturn true\n\t}\n\t// MsgRun: proxy was called from the ephemeral run realm; that\n\t// realm's package address equals the EOA OriginCaller.\n\treturn chain.PackageAddress(prev.PkgPath()) == unsafe.OriginCaller()\n}\n"},{"name":"govdao_execute_proposal_00_filetest.gno","body":"// PKGPATH: gno.land/r/test/govdao\npackage govdao\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/impl\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nconst user address = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\nvar (\n\texecutor   dao.Executor\n\tproposalID dao.ProposalID\n\trenderPath string\n\tgovdao     = impl.NewGovDAO()\n)\n\nfunc init(cur realm) {\n\t// Initialize GovDAO members\n\tmemberstore.Get(0, cur).DeleteAll()\n\tmemberstore.Get(0, cur).SetTier(memberstore.T1)\n\tmemberstore.Get(0, cur).SetMember(memberstore.T1, user, memberstore.NewMember(3))\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(impl.NewGovDAO(), nil))\n\n\t// Create an executor that always fails\n\tcb := func(realm) error { return errors.New(\"Boom!\") }\n\texecutor = dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\t// Create a proposal request that fails on execution\n\trequest := dao.NewProposalRequest(\"Test\", \"This proposal always fails on execution\", executor)\n\n\t// Create the proposal from a realm so GovDAO instance is able to render the proposal\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tproposalID = dao.MustCreateProposal(cross(cur), request)\n\trenderPath = strconv.FormatUint(uint64(proposalID), 10)\n\n\t// Register proposal with the local GovDAO instance\n\tgovdao.PostCreateProposal(0, cur, request, proposalID)\n}\n\nfunc main(cur realm) {\n\t// Execute proposal, status should be REJECTED\n\terr := govdao.ExecuteProposal(0, cur, proposalID, executor)\n\n\tprintln(err.Error())\n\tprintln()\n\tprintln(govdao.Render(cross(cur), \"gno.land/r/gov/dao/v3/impl\", renderPath))\n}\n\n// Output:\n// Boom!\n//\n// ## Prop #0 - Test\n// Author: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\n//\n// This proposal always fails on execution\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **PROPOSAL HAS BEEN DENIED**\n// REASON: execution failed: Boom!\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 0%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao/v3/impl:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n"},{"name":"govdao_execute_proposal_01_filetest.gno","body":"// PKGPATH: gno.land/r/test/govdao\npackage govdao\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/impl\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nconst user address = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n\nvar (\n\texecutor   dao.Executor\n\tproposalID dao.ProposalID\n\trenderPath string\n\tgovdao     = impl.NewGovDAO()\n)\n\nfunc init(cur realm) {\n\t// Initialize GovDAO members\n\tmemberstore.Get(0, cur).DeleteAll()\n\tmemberstore.Get(0, cur).SetTier(memberstore.T1)\n\tmemberstore.Get(0, cur).SetMember(memberstore.T1, user, memberstore.NewMember(3))\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(impl.NewGovDAO(), nil))\n\n\t// Create a dummy executor\n\tcb := func(realm) error { return nil }\n\texecutor = dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\t// Create a proposal request that pass on execution\n\trequest := dao.NewProposalRequest(\"Test\", \"This proposal always pass on execution\", executor)\n\n\t// Create the proposal from a realm so GovDAO instance is able to render the proposal\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tproposalID = dao.MustCreateProposal(cross(cur), request)\n\trenderPath = strconv.FormatUint(uint64(proposalID), 10)\n\n\t// Register proposal with the local GovDAO instance\n\tgovdao.PostCreateProposal(0, cur, request, proposalID)\n}\n\nfunc main(cur realm) {\n\t// Execute proposal, status should be ACTIVE\n\terr := govdao.ExecuteProposal(0, cur, proposalID, executor)\n\n\tprintln(err == nil)\n\tprintln()\n\tprintln(govdao.Render(cross(cur), \"gno.land/r/gov/dao/v3/impl\", renderPath))\n}\n\n// Output:\n// true\n//\n// ## Prop #0 - Test\n// Author: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\n//\n// This proposal always pass on execution\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **Proposal is open for votes**\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 0%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao/v3/impl:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n"},{"name":"govdao_execute_proposal_02_filetest.gno","body":"// PKGPATH: gno.land/r/test/govdao\n\npackage govdao\n\nimport \"gno.land/r/gov/dao/v3/impl\"\n\nvar govdao = impl.NewGovDAO()\n\nfunc main(cur realm) {\n\t// Try to execute a proposal using a nil executor\n\tgovdao.ExecuteProposal(0, cur, 0, nil)\n}\n\n// Error:\n// an executor is required to execute the proposal\n"},{"name":"govdao_execute_proposal_03_filetest.gno","body":"// PKGPATH: gno.land/r/test/govdao\npackage govdao\n\nimport (\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/impl\"\n)\n\nvar (\n\texecutor dao.Executor\n\tgovdao   = impl.NewGovDAO()\n)\n\nfunc init(cur realm) {\n\t// Create a dummy executor\n\tcb := func(realm) error { return nil }\n\texecutor = dao.NewSimpleExecutor(0, cur, cb, \"\")\n}\n\nfunc main(cur realm) {\n\t// Try to execute a proposal that doesn't exist\n\tgovdao.ExecuteProposal(0, cur, 404, executor)\n}\n\n// Error:\n// proposal not found\n"},{"name":"govdao_test.gno","body":"package impl\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc init(cur realm) {\n\tloadMembers(cur)\n\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(govDAO, []string{\"gno.land/r/gov/dao/v3/impl\"}))\n}\n\nvar (\n\tm1    = testutils.TestAddress(\"m1\")\n\tm11   = testutils.TestAddress(\"m1.1\")\n\tm111  = testutils.TestAddress(\"m1.1.1\")\n\tm1111 = testutils.TestAddress(\"m1.1.1.1\")\n\tm2    = testutils.TestAddress(\"m2\")\n\tm3    = testutils.TestAddress(\"m3\")\n\tm4    = testutils.TestAddress(\"m4\")\n\tm5    = testutils.TestAddress(\"m5\")\n\tm6    = testutils.TestAddress(\"m6\")\n\n\tnoMember = testutils.TestAddress(\"nm1\")\n)\n\nfunc loadMembers(cur realm) {\n\t// This is needed because state is saved between unit tests,\n\t// and we want to avoid having real members used on tests\n\tmstore := memberstore.Get(0, cur)\n\tmstore.DeleteAll()\n\n\tmstore.SetTier(memberstore.T1)\n\tmstore.SetTier(memberstore.T2)\n\tmstore.SetTier(memberstore.T3)\n\n\tmstore.SetMember(memberstore.T1, m1, memberByTier(memberstore.T1))\n\tmstore.SetMember(memberstore.T1, m11, memberByTier(memberstore.T1))\n\tmstore.SetMember(memberstore.T1, m111, memberByTier(memberstore.T1))\n\tmstore.SetMember(memberstore.T1, m1111, memberByTier(memberstore.T1))\n\n\tmstore.SetMember(memberstore.T2, m2, memberByTier(memberstore.T2))\n\tmstore.SetMember(memberstore.T2, m3, memberByTier(memberstore.T2))\n\tmstore.SetMember(memberstore.T3, m4, memberByTier(memberstore.T3))\n\tmstore.SetMember(memberstore.T3, m5, memberByTier(memberstore.T3))\n\tmstore.SetMember(memberstore.T3, m6, memberByTier(memberstore.T3))\n}\n\nfunc TestCreateProposalAndVote(cur realm, t *testing.T) {\n\tloadMembers(cur)\n\n\tportfolio := \"# This is my portfolio:\\n\\n- THINGS\"\n\n\ttesting.SetOriginCaller(noMember)\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gov/dao/v3/impl\"))\n\n\tnm1 := testutils.TestAddress(\"nm1\")\n\n\turequire.AbortsWithMessage(t, cur, \"Only T1 and T2 members can be added by proposal. To add a T3 member use AddMember function directly.\", func(cur realm) {\n\t\tdao.MustCreateProposal(cross(cur), NewAddMemberRequest(cur, nm1, memberstore.T3, portfolio))\n\t})\n\n\turequire.AbortsWithMessage(t, cur, \"proposer is not a member\", func(cur realm) {\n\t\tdao.MustCreateProposal(cross(cur), NewAddMemberRequest(cur, nm1, memberstore.T2, portfolio))\n\t})\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gov/dao/v3/impl\"))\n\n\tproposalRequest := NewAddMemberRequest(cur, nm1, memberstore.T2, portfolio)\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid := dao.MustCreateProposal(cross(cur), proposalRequest)\n\turequire.Equal(t, int(pid), 0)\n\n\t// m1 votes yes because that member is interested on it\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(0)))\n\n\ttesting.SetOriginCaller(m11)\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.NoVote, dao.ProposalID(0)))\n\n\ttesting.SetOriginCaller(m2)\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.NoVote, dao.ProposalID(0)))\n\n\ttesting.SetOriginCaller(m3)\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.NoVote, dao.ProposalID(0)))\n\n\ttesting.SetOriginCaller(m4)\n\n\turequire.AbortsWithMessage(t, cur, \"member on specified tier is not allowed to vote on this proposal\", func() {\n\t\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.NoVote, dao.ProposalID(0)))\n\t})\n\n\ttesting.SetOriginCaller(m111)\n\n\t// Same effect as:\n\t// dao.MustVoteOnProposal(dao.VoteRequest{\n\t// \tOption:     dao.NoVote,\n\t// \tProposalID: dao.ProposalID(0),\n\t// })\n\tdao.MustVoteOnProposalSimple(cross(cur), 0, \"NO\")\n\n\turequire.Equal(t, true, strings.Contains(dao.Render(cross(cur), \"\"), \"Prop #0 - New T2 Member Proposal\"))\n\t// urequire.Equal(t, true, strings.Contains(dao.Render(cross(cur), \"\"), \"Author: \"+m1.String()))\n\n\turequire.AbortsWithMessage(t, cur, \"proposal didn't reach supermajority yet: 66.66\", func() {\n\t\tdao.ExecuteProposal(cross(cur), dao.ProposalID(0))\n\t})\n\n\ttesting.SetOriginCaller(m1111)\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.NoVote, dao.ProposalID(0)))\n\n\taccepted := dao.ExecuteProposal(cross(cur), dao.ProposalID(0))\n\turequire.Equal(t, false, accepted)\n\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"0\"), \"**PROPOSAL HAS BEEN DENIED**\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"0\"), \"NO PERCENT: 81.25%\"))\n}\n\nfunc TestExecutorCreationRealm(cur realm, t *testing.T) {\n\tloadMembers(cur)\n\n\t// Test that executor creation realm is captured correctly\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/template/contract\"))\n\n\t// Create executor in the template contract realm\n\texecutor := dao.NewSimpleExecutor(0, cur, func(realm) error { return nil }, \"Test executor from template\")\n\n\tproposalRequest := dao.NewProposalRequest(\n\t\t\"Test Proposal\",\n\t\t\"This proposal tests executor creation realm tracking\",\n\t\texecutor,\n\t)\n\n\t// Create proposal from user realm (user can call DAO directly)\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid := dao.MustCreateProposal(cross(cur), proposalRequest)\n\n\t// Get the proposal\n\tprop := dao.MustGetProposal(pid)\n\n\t// Verify the author is m1\n\turequire.Equal(t, m1, prop.Author())\n\n\t// Verify the executor creation realm is captured correctly\n\turequire.Equal(t, \"gno.land/r/template/contract\", prop.ExecutorCreationRealm())\n\n\t// Check that it's displayed in the individual proposal render output\n\tindividualRendered := dao.Render(cross(cur), pid.String())\n\turequire.Equal(t, true, contains(individualRendered, \"Executor created in: gno.land/r/template/contract\"))\n\turequire.Equal(t, true, contains(individualRendered, \"Test executor from template\"))\n\n\t// Also verify the main content is there\n\turequire.Equal(t, true, contains(individualRendered, \"Test Proposal\"))\n\turequire.Equal(t, true, contains(individualRendered, \"This proposal tests executor creation realm tracking\"))\n}\n\nfunc TestProposalPagination(cur realm, t *testing.T) {\n\tloadMembers(cur)\n\tportfolio := \"### This is my portfolio:\\n\\n- THINGS\"\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gov/dao/v3/impl\"))\n\n\tnm1 := testutils.TestAddress(\"nm1\")\n\n\tvar pid dao.ProposalID\n\n\tproposalRequest := NewAddMemberRequest(cur, nm1, memberstore.T2, portfolio)\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid = dao.MustCreateProposal(cross(cur), proposalRequest)\n\n\t// TODO: tests keep the same vm state: https://github.com/gnolang/gno/issues/1982\n\turequire.Equal(t, 2, int(pid))\n\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid = dao.MustCreateProposal(cross(cur), proposalRequest)\n\turequire.Equal(t, 3, int(pid))\n\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid = dao.MustCreateProposal(cross(cur), proposalRequest)\n\turequire.Equal(t, 4, int(pid))\n\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid = dao.MustCreateProposal(cross(cur), proposalRequest)\n\turequire.Equal(t, 5, int(pid))\n\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid = dao.MustCreateProposal(cross(cur), proposalRequest)\n\turequire.Equal(t, 6, int(pid))\n\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid = dao.MustCreateProposal(cross(cur), proposalRequest)\n\turequire.Equal(t, 7, int(pid))\n\n\tfmt.Println(dao.Render(cross(cur), \"\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"\"), \"### [Prop #7 - New T2 Member Proposal](/r/gov/dao:7)\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"\"), \"### [Prop #6 - New T2 Member Proposal](/r/gov/dao:6)\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"\"), \"### [Prop #5 - New T2 Member Proposal](/r/gov/dao:5)\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"\"), \"### [Prop #4 - New T2 Member Proposal](/r/gov/dao:4)\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"\"), \"### [Prop #3 - New T2 Member Proposal](/r/gov/dao:3)\"))\n\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"?page=2\"), \"### [Prop #2 - New T2 Member Proposal](/r/gov/dao:2)\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"?page=2\"), \"### [Prop #1 - Test Proposal](/r/gov/dao:1)\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"?page=2\"), \"### [Prop #0 - New T2 Member Proposal](/r/gov/dao:0)\"))\n}\n\nfunc TestUpgradeDaoImplementation(cur realm, t *testing.T) {\n\tloadMembers(cur)\n\n\ttesting.SetOriginCaller(noMember)\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gov/dao/v3/impl\"))\n\n\turequire.PanicsWithMessage(t, cur, \"proposer is not a member\", func() {\n\t\tNewUpgradeDaoImplRequest(cur, govDAO, \"gno.land/r/gov/dao/v4/impl\", \"Something happened and we have to fix it.\")\n\t})\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gov/dao/v3/impl\"))\n\n\tpreq := NewUpgradeDaoImplRequest(cur, govDAO, \"gno.land/r/gov/dao/v4/impl\", \"Something happened and we have to fix it.\")\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid := dao.MustCreateProposal(cross(cur), preq)\n\turequire.Equal(t, int(pid), 8)\n\n\t// m1 votes yes because that member is interested on it\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(pid)))\n\n\ttesting.SetOriginCaller(m11)\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(pid)))\n\n\ttesting.SetOriginCaller(m2)\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(pid)))\n\n\ttesting.SetOriginCaller(m3)\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(pid)))\n\n\ttesting.SetOriginCaller(m111)\n\n\t// Same effect as:\n\t// dao.MustVoteOnProposal(dao.VoteRequest{\n\t// \tOption:     dao.YesVote,\n\t// \tProposalID: dao.ProposalID(pid),\n\t// })\n\tdao.MustVoteOnProposalSimple(cross(cur), int64(pid), \"YES\")\n\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"8\"), \"**Proposal is open for votes**\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"8\"), \"68.42105263157895%\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"8\"), \"0%\"))\n\n\taccepted := dao.ExecuteProposal(cross(cur), dao.ProposalID(pid))\n\turequire.Equal(t, true, accepted)\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"8\"), \"**PROPOSAL HAS BEEN ACCEPTED**\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), \"8\"), \"YES PERCENT: 68.42105263157895%\"))\n}\n\nfunc TestAbstainVote(cur realm, t *testing.T) {\n\tloadMembers(cur)\n\n\tportfolio := \"# This is my portfolio:\\n\\n- THINGS\"\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gov/dao/v3/impl\"))\n\n\tnm1 := testutils.TestAddress(\"nm1\")\n\tproposalRequest := NewAddMemberRequest(cur, nm1, memberstore.T2, portfolio)\n\n\ttesting.SetOriginCaller(m1)\n\ttesting.SetRealm(testing.NewUserRealm(m1))\n\tpid := dao.MustCreateProposal(cross(cur), proposalRequest)\n\n\t// m1 votes abstain\n\tdao.MustVoteOnProposalSimple(cross(cur), int64(pid), \"ABSTAIN\")\n\n\t// Other members vote YES to reach supermajority\n\ttesting.SetOriginCaller(m11)\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, pid))\n\n\ttesting.SetOriginCaller(m111)\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, pid))\n\n\ttesting.SetOriginCaller(m1111)\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, pid))\n\n\ttesting.SetOriginCaller(m2)\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, pid))\n\n\ttesting.SetOriginCaller(m3)\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, pid))\n\n\t// Verify render shows correct percentages\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), pid.String()), \"YES PERCENT: 81.25%\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), pid.String()), \"NO PERCENT: 0%\"))\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), pid.String()), \"ABSTAIN PERCENT: 18.75%\"))\n\n\t// Supermajority reached despite one abstain voter\n\taccepted := dao.ExecuteProposal(cross(cur), pid)\n\turequire.Equal(t, true, accepted)\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), pid.String()), \"**PROPOSAL HAS BEEN ACCEPTED**\"))\n\n\t// Abstain voter appears in vote list\n\turequire.Equal(t, true, contains(dao.Render(cross(cur), fmt.Sprintf(\"%v/votes\", int64(pid))), \"ABSTAIN\"))\n}\n\nfunc contains(s, substr string) bool {\n\treturn strings.Index(s, substr) \u003e= 0\n}\n"},{"name":"impl.gno","body":"package impl\n\nimport (\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nvar (\n\tlaw    *Law\n\tgovDAO *GovDAO = NewGovDAO()\n)\n\nfunc init() {\n\tlaw = \u0026Law{\n\t\tSupermajority: 66.66, // Two thirds\n\t}\n}\n\nfunc Render(cur realm, in string) string {\n\t// Same-realm: pass cur to govDAO.Render (also crossing, but same realm\n\t// so use the literal cur form rather than cross(cur)).\n\treturn govDAO.Render(cur, cur.PkgPath(), in)\n}\n\n// AddMember allows T1 and T2 members to freely add T3 members using their invitation points.\nfunc AddMember(cur realm, addr address) {\n\tcaller := cur.Previous()\n\tif !caller.IsUser() {\n\t\tpanic(\"this function must be called by an EOA through msg call or msg run\")\n\t}\n\tm, t := memberstore.Get(0, cur).GetMember(caller.Address())\n\tif m == nil {\n\t\tpanic(\"caller is not a member\")\n\t}\n\n\tif t != memberstore.T1 \u0026\u0026 t != memberstore.T2 {\n\t\tpanic(\"caller is not on T1 or T2. To add members, propose them through proposals\")\n\t}\n\n\tm.RemoveInvitationPoint()\n\n\tif err := memberstore.Get(0, cur).SetMember(memberstore.T3, addr, memberByTier(memberstore.T3)); err != nil {\n\t\tpanic(err.Error())\n\t}\n}\n\n// GetInstance returns the singleton *GovDAO. Only the loader realm may\n// call it (used during the bootstrap UpdateImpl handoff). The\n// IsCurrent() check rejects stale or stashed realm values; PkgPath()\n// after the check is the authentic immediate caller.\nfunc GetInstance(_ int, rlm realm) *GovDAO {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"GetInstance: rlm is not the caller's live cur (stale capture or sibling frame)\")\n\t}\n\tif rlm.PkgPath() != \"gno.land/r/gov/dao/v3/loader\" {\n\t\tpanic(\"not allowed\")\n\t}\n\n\treturn govDAO\n}\n"},{"name":"prop_requests.gno","body":"package impl\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/aeddi/panictoerr\"\n\t\"gno.land/p/moul/md\"\n\ttrs_pkg \"gno.land/p/nt/treasury/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n\t\"gno.land/r/gov/dao/v3/treasury\"\n)\n\nfunc NewChangeLawRequest(cur realm, newLaw Law) dao.ProposalRequest {\n\tmember, _ := memberstore.Get(0, cur).GetMember(unsafe.OriginCaller())\n\tif member == nil {\n\t\tpanic(\"proposer is not a member\")\n\t}\n\n\tcb := func(cur realm) error {\n\t\tlaw = \u0026newLaw\n\t\treturn nil\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, ufmt.Sprintf(\"A new Law is proposed:\\n %v\", newLaw))\n\n\treturn dao.NewProposalRequest(\"Change Law Proposal\", \"This proposal is looking to change the actual govDAO Law\", e)\n}\n\nfunc NewUpgradeDaoImplRequest(cur realm, newDao dao.DAO, realmPkg, reason string) dao.ProposalRequest {\n\tmember, _ := memberstore.Get(0, cur).GetMember(unsafe.OriginCaller())\n\tif member == nil {\n\t\tpanic(\"proposer is not a member\")\n\t}\n\n\tcb := func(cur realm) error {\n\t\t// dao.UpdateImpl() must be cross-called from v3/impl but\n\t\t// what calls this cb function is r/gov/dao.\n\t\t// therefore we must cross back into v3/impl and then\n\t\t// cross call dao.UpdateRequest().\n\t\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(newDao, []string{\"gno.land/r/gov/dao/v3/impl\", realmPkg}))\n\t\treturn nil\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\treturn dao.NewProposalRequest(\"Change DAO implementation\", \"This proposal is looking to change the actual govDAO implementation. Reason: \"+reason, e)\n}\n\nfunc NewAddMemberRequest(cur realm, addr address, tier string, portfolio string) dao.ProposalRequest {\n\t_, ok := memberstore.GetTier(tier)\n\tif !ok {\n\t\tpanic(\"provided tier does not exists\")\n\t}\n\n\tif tier != memberstore.T1 \u0026\u0026 tier != memberstore.T2 {\n\t\tpanic(\"Only T1 and T2 members can be added by proposal. To add a T3 member use AddMember function directly.\")\n\t}\n\n\tif portfolio == \"\" {\n\t\tpanic(\"A portfolio for the proposed member is required\")\n\t}\n\n\tmember, _ := memberstore.Get(0, cur).GetMember(unsafe.OriginCaller())\n\tif member == nil {\n\t\tpanic(\"proposer is not a member\")\n\t}\n\n\tif member.InvitationPoints \u003c= 0 {\n\t\tpanic(\"proposer does not have enough invitation points for inviting new people to the board\")\n\t}\n\n\tcb := func(cur realm) error {\n\t\tmember.RemoveInvitationPoint()\n\t\terr := memberstore.Get(0, cur).SetMember(tier, addr, memberByTier(tier))\n\n\t\treturn err\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, ufmt.Sprintf(\"A new member with address %v is proposed to be on tier %v. Provided Portfolio information:\\n\\n%v\", addr, tier, portfolio))\n\n\tname := tryResolveAddr(addr)\n\treturn dao.NewProposalRequestWithFilter(\n\t\tufmt.Sprintf(\"New %s Member Proposal\", tier),\n\t\tufmt.Sprintf(\"This is a proposal to add `%s` to **%s**.\\n#### `%s`'s Portfolio:\\n\\n%s\\n\", name, tier, name, portfolio),\n\t\te,\n\t\tFilterByTier{Tier: tier},\n\t)\n}\n\nfunc NewWithdrawMemberRequest(cur realm, addr address, reason string) dao.ProposalRequest {\n\tmember, tier := memberstore.Get(0, cur).GetMember(addr)\n\tif member == nil {\n\t\tpanic(\"user we want to remove not found\")\n\t}\n\n\treason = strings.TrimSpace(reason)\n\tif tier == memberstore.T1 \u0026\u0026 reason == \"\" {\n\t\tpanic(\"T1 user removals must contains a reason.\")\n\t}\n\n\tcb := func(cur realm) error {\n\t\tmemberstore.Get(0, cur).RemoveMember(addr)\n\t\treturn nil\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, ufmt.Sprintf(\"Member with address %v will be withdrawn.\\n\\n REASON: %v.\", addr, reason))\n\n\treturn dao.NewProposalRequest(\n\t\t\"Member Withdrawal Proposal\",\n\t\tufmt.Sprintf(\"This is a proposal to remove %s from the GovDAO\", tryResolveAddr(addr)),\n\t\te,\n\t)\n}\n\nfunc NewPromoteMemberRequest(cur realm, addr address, fromTier string, toTier string) dao.ProposalRequest {\n\tcb := func(cur realm) error {\n\t\tprevTier := memberstore.Get(0, cur).RemoveMember(addr)\n\t\tif prevTier == \"\" {\n\t\t\tpanic(\"member not found, so cannot be promoted\")\n\t\t}\n\n\t\tif prevTier != fromTier {\n\t\t\tpanic(\"previous tier changed from the one indicated in the proposal\")\n\t\t}\n\n\t\terr := memberstore.Get(0, cur).SetMember(toTier, addr, memberByTier(toTier))\n\n\t\treturn err\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, ufmt.Sprintf(\"A new member with address %v will be promoted from tier %v to tier %v.\", addr, fromTier, toTier))\n\n\treturn dao.NewProposalRequestWithFilter(\n\t\t\"Member Promotion Proposal\",\n\t\tufmt.Sprintf(\"This is a proposal to promote %s from **%s** to **%s**.\", tryResolveAddr(addr), fromTier, toTier),\n\t\te,\n\t\tFilterByTier{Tier: toTier},\n\t)\n}\n\nfunc NewTreasuryPaymentRequest(cur realm, payment trs_pkg.Payment, reason string) dao.ProposalRequest {\n\tif !treasury.HasBanker(payment.BankerID()) {\n\t\tpanic(\"banker not registered in treasury with ID: \" + payment.BankerID())\n\t}\n\n\treason = strings.TrimSpace(reason)\n\tif reason == \"\" {\n\t\tpanic(\"treasury payment request requires a reason\")\n\t}\n\n\tcb := func(cur realm) error {\n\t\treturn panictoerr.PanicToError(func() {\n\t\t\ttreasury.Send(cross(cur), payment)\n\t\t})\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur,\n\t\tcb,\n\t\tufmt.Sprintf(\n\t\t\t\"A payment will be sent by the GovDAO treasury.\\n\\nReason: %s\\n\\nPayment: %s.\",\n\t\t\treason,\n\t\t\tpayment.String(),\n\t\t),\n\t)\n\n\treturn dao.NewProposalRequest(\n\t\t\"Treasury Payment\",\n\t\tufmt.Sprintf(\n\t\t\t\"This proposal is looking to send a payment using the treasury.\\n\\nReason: %s\\n\\nPayment: %s\",\n\t\t\treason,\n\t\t\tpayment.String(),\n\t\t),\n\t\te,\n\t)\n}\n\n// NewTreasuryGRC20TokensUpdate creates a proposal request to update the list of GRC20 tokens registry\n// keys used by the treasury. The new list, if voted and accepted, will overwrite the current one.\nfunc NewTreasuryGRC20TokensUpdate(cur realm, newTokenKeys []string) dao.ProposalRequest {\n\tif len(newTokenKeys) == 0 {\n\t\tpanic(\"the list of new tokens is empty\")\n\t}\n\n\tcb := func(cur realm) error {\n\t\treturn panictoerr.PanicToError(func() {\n\t\t\t// NOTE:: Consider checking if the newTokenKeys are already registered\n\t\t\t// in the grc20reg before updating the treasury tokens keys.\n\t\t\ttreasury.SetTokenKeys(cross(cur), newTokenKeys)\n\t\t})\n\t}\n\n\tbulletList := md.BulletList(newTokenKeys)\n\n\te := dao.NewSimpleExecutor(0, cur,\n\t\tcb,\n\t\tufmt.Sprintf(\n\t\t\t\"The list of GRC20 tokens used by the treasury will be updated.\\n\\nNew Token Keys:\\n%s.\\n\",\n\t\t\tbulletList,\n\t\t),\n\t)\n\n\treturn dao.NewProposalRequest(\n\t\t\"Treasury GRC20 Tokens Update\",\n\t\tufmt.Sprintf(\n\t\t\t\"This proposal is looking to update the list of GRC20 tokens used by the treasury.\\n\\nNew Token Keys:\\n%s\",\n\t\t\tbulletList,\n\t\t),\n\t\te,\n\t)\n}\n\nfunc memberByTier(tier string) *memberstore.Member {\n\tswitch tier {\n\tcase memberstore.T1:\n\t\tt, _ := memberstore.GetTier(memberstore.T1)\n\t\treturn memberstore.NewMember(t.InvitationPoints)\n\tcase memberstore.T2:\n\t\tt, _ := memberstore.GetTier(memberstore.T2)\n\t\treturn memberstore.NewMember(t.InvitationPoints)\n\tcase memberstore.T3:\n\t\tt, _ := memberstore.GetTier(memberstore.T3)\n\t\treturn memberstore.NewMember(t.InvitationPoints)\n\tdefault:\n\t\tpanic(\"member not found by the specified tier\")\n\t}\n}\n"},{"name":"render.gno","body":"package impl\n\nimport (\n\t\"chain/runtime\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/helplink\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/bptree/v0/pager\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/sys/users\"\n)\n\ntype render struct {\n\trelativeRealmPath string\n\trouter            *mux.Router\n\tpssPager          *pager.Pager\n}\n\nfunc NewRender(d *GovDAO) *render {\n\tren := \u0026render{\n\t\tpssPager: pager.NewPager(d.pss.BPTree, 5, true),\n\t}\n\n\tr := mux.NewRouter()\n\n\t// Handlers use mux's rlm-aware shape: rlm is supplied at RenderRlm\n\t// dispatch time rather than captured at NewRender time. This lets\n\t// downstream crossing reads (dao.GetProposal etc.) use cross(rlm)\n\t// without relying on bare cross or restructuring the router.\n\tr.HandleFuncRlm(\"\", func(_ int, rlm realm, rw *mux.ResponseWriter, req *mux.Request) {\n\t\trw.Write(ren.renderActiveProposals(0, rlm, req.RawPath, d))\n\t})\n\n\tr.HandleFuncRlm(\"{pid}\", func(_ int, rlm realm, rw *mux.ResponseWriter, req *mux.Request) {\n\t\trw.Write(ren.renderProposalPage(0, rlm, req.GetVar(\"pid\"), d))\n\t})\n\n\tr.HandleFuncRlm(\"{pid}/votes\", func(_ int, rlm realm, rw *mux.ResponseWriter, req *mux.Request) {\n\t\trw.Write(ren.renderVotesForProposal(0, rlm, req.GetVar(\"pid\"), d))\n\t})\n\n\tren.router = r\n\n\treturn ren\n}\n\nfunc (ren *render) Render(_ int, rlm realm, pkgPath string, path string) string {\n\trelativePath, found := strings.CutPrefix(pkgPath, runtime.ChainDomain())\n\tif !found {\n\t\tpanic(ufmt.Sprintf(\n\t\t\t\"realm package with unexpected name found: %v in chain domain %v\",\n\t\t\tpkgPath, runtime.ChainDomain()))\n\t}\n\tren.relativeRealmPath = relativePath\n\treturn ren.router.RenderRlm(0, rlm, path)\n}\n\nfunc (ren *render) renderActiveProposals(_ int, rlm realm, url string, d *GovDAO) string {\n\tout := \"# GovDAO\\n\"\n\tout += \"## Members\\n\"\n\tout += \"[\u003e Go to Memberstore \u003c](/r/gov/dao/v3/memberstore)\\n\"\n\tout += \"## Proposals\\n\"\n\tpage := ren.pssPager.MustGetPageByPath(url)\n\tif len(page.Items) == 0 {\n\t\tout += \"\\nNo proposals yet.\\n\\n\"\n\t\treturn out\n\t}\n\n\tfor _, item := range page.Items {\n\t\tseqpid, err := seqid.FromString(item.Key)\n\t\tif err != nil {\n\t\t\tcontinue\n\t\t}\n\t\tout += ren.renderProposalListItem(0, rlm, ufmt.Sprintf(\"%v\", int64(seqpid)), d)\n\t\tout += \"---\\n\\n\"\n\t}\n\n\tout += page.Picker(\"\")\n\n\treturn out\n}\n\nfunc (ren *render) renderProposalPage(_ int, rlm realm, sPid string, d *GovDAO) string {\n\tpid, err := strconv.ParseInt(sPid, 10, 64)\n\tif err != nil {\n\t\treturn ufmt.Sprintf(\"# Error: Invalid proposal ID format.\\n\\n\\n%s\\n\\n\", err.Error())\n\t}\n\n\tp, err := dao.GetProposal(dao.ProposalID(pid))\n\tif err != nil {\n\t\treturn ufmt.Sprintf(\"# Proposal not found\\n\\n%s\", err.Error())\n\t}\n\n\tps := d.pss.GetStatus(dao.ProposalID(pid))\n\tout := ufmt.Sprintf(\"## Prop #%v - %v\\n\", pid, md.EscapeText(p.Title()))\n\tout += \"Author: \" + tryResolveAddr(p.Author()) + \"\\n\\n\"\n\n\tout += p.Description()\n\tout += \"\\n\\n\"\n\n\t// Add executor metadata if available\n\tif p.ExecutorString() != \"\" {\n\t\tout += ufmt.Sprintf(`This proposal contains the following metadata:\n\n%s\n\nExecutor created in: %s\n`, p.ExecutorString(), p.ExecutorCreationRealm())\n\t\tout += \"\\n\\n\"\n\t}\n\n\tout += \"\\n\\n---\\n\\n\"\n\tout += ps.String(0, rlm)\n\tout += \"\\n\"\n\tout += ufmt.Sprintf(\"[Detailed voting list](%v:%v/votes)\", ren.relativeRealmPath, pid)\n\tout += \"\\n\\n---\\n\\n\"\n\n\tout += renderActionBar(ufmt.Sprintf(\"%v\", pid))\n\n\treturn out\n}\n\nfunc (ren *render) renderProposalListItem(_ int, rlm realm, sPid string, d *GovDAO) string {\n\tpid, err := strconv.ParseInt(sPid, 10, 64)\n\tif err != nil {\n\t\treturn ufmt.Sprintf(\"# Error: Invalid proposal ID format.\\n\\n\\n%s\\n\\n\", err.Error())\n\t}\n\n\tp, err := dao.GetProposal(dao.ProposalID(pid))\n\tif err != nil {\n\t\treturn ufmt.Sprintf(\"# Proposal not found\\n\\n%s\\n\\n\", err.Error())\n\t}\n\n\tps := d.pss.GetStatus(dao.ProposalID(pid))\n\tout := ufmt.Sprintf(\"### [Prop #%v - %v](%v:%v)\\n\", pid, md.EscapeText(p.Title()), ren.relativeRealmPath, pid)\n\tout += ufmt.Sprintf(\"Author: %s\\n\\n\", tryResolveAddr(p.Author()))\n\n\tout += \"Status: \" + getPropStatus(ps)\n\tout += \"\\n\\n\"\n\n\tout += \"Tiers eligible to vote: \"\n\tout += strings.Join(ps.TiersAllowedToVote, \", \")\n\n\tout += \"\\n\\n\"\n\treturn out\n}\n\nfunc (ren *render) renderVotesForProposal(_ int, rlm realm, sPid string, d *GovDAO) string {\n\tpid, err := strconv.ParseInt(sPid, 10, 64)\n\tif err != nil {\n\t\treturn ufmt.Sprintf(\"# Error: Invalid proposal ID format.\\n\\n\\n%s\\n\\n\", err.Error())\n\t}\n\n\tps := d.pss.GetStatus(dao.ProposalID(pid))\n\tif ps == nil {\n\t\treturn ufmt.Sprintf(\"# Proposal not found\\n\\nProposal %v does not exist.\", pid)\n\t}\n\n\tout := \"\"\n\tout += ufmt.Sprintf(\"# Proposal #%v - Vote List\\n\\n\", pid)\n\tout += StringifyVotes(0, rlm, ps)\n\n\treturn out\n}\n\nfunc isPropActive(ps *proposalStatus) bool {\n\treturn !ps.Accepted \u0026\u0026 !ps.Denied\n}\n\nfunc getPropStatus(ps *proposalStatus) string {\n\tif ps == nil {\n\t\treturn \"UNKNOWN\"\n\t}\n\tif ps.Accepted {\n\t\treturn \"ACCEPTED\"\n\t} else if ps.Denied {\n\t\treturn \"REJECTED\"\n\t}\n\treturn \"ACTIVE\"\n}\n\nfunc renderActionBar(sPid string) string {\n\tout := \"### Actions\\n\"\n\n\tproxy := helplink.Realm(\"gno.land/r/gov/dao\")\n\tout += proxy.Func(\"Vote YES\", \"MustVoteOnProposalSimple\", \"pid\", sPid, \"option\", \"YES\") + \" | \"\n\tout += proxy.Func(\"Vote NO\", \"MustVoteOnProposalSimple\", \"pid\", sPid, \"option\", \"NO\") + \" | \"\n\tout += proxy.Func(\"Vote ABSTAIN\", \"MustVoteOnProposalSimple\", \"pid\", sPid, \"option\", \"ABSTAIN\")\n\n\tout += \"\\n\\n\"\n\tout += \"WARNING: Please double check transaction data before voting.\"\n\treturn out\n}\n\nfunc tryResolveAddr(addr address) string {\n\tuserData := users.ResolveAddress(addr)\n\tif userData == nil {\n\t\treturn addr.String()\n\t}\n\treturn userData.RenderLink(\"\")\n}\n"},{"name":"stringify_proposal_00_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/impl\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nvar (\n\ttestUser = testutils.TestAddress(\"test\")\n)\n\nfunc memberByTier(tier string) *memberstore.Member {\n\tswitch tier {\n\tcase memberstore.T1:\n\t\tt, _ := memberstore.GetTier(memberstore.T1)\n\t\treturn memberstore.NewMember(t.InvitationPoints)\n\tdefault:\n\t\tpanic(\"unsupported tier: \" + tier)\n\t}\n}\n\nfunc init(cur realm) {\n\t// Load members for testing\n\tmstore := memberstore.Get(0, cur)\n\tmstore.DeleteAll()\n\tmstore.SetTier(memberstore.T1)\n\tmstore.SetMember(memberstore.T1, testUser, memberByTier(memberstore.T1))\n\n\t// Set up the DAO implementation using proper constructor\n\tgovDAO := impl.NewGovDAO()\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(govDAO, []string{\"gno.land/r/test\", \"gno.land/r/gov/dao/v3/impl\"}))\n}\n\nfunc main(cur realm) {\n\t// Create an executor in a specific realm to test creation realm tracking\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/template/contract\"))\n\texecutor := dao.NewSimpleExecutor(0, cur, func(realm) error { return nil }, \"Test executor description\")\n\n\t// Create a proposal request\n\tproposalRequest := dao.NewProposalRequest(\n\t\t\"Test Proposal Title\",\n\t\t\"This is a test proposal description to verify StringifyProposal works correctly.\",\n\t\texecutor,\n\t)\n\n\t// Switch to user realm to create the proposal\n\ttesting.SetOriginCaller(testUser)\n\ttesting.SetRealm(testing.NewUserRealm(testUser))\n\tpid := dao.MustCreateProposal(cross(cur), proposalRequest)\n\n\t// Get the proposal and test the core functionality\n\tprop := dao.MustGetProposal(pid)\n\n\t// Test that executor string is captured correctly\n\tprintln(\"Executor string:\", prop.ExecutorString())\n\n\t// Test that executor creation realm is captured correctly\n\tprintln(\"Executor creation realm:\", prop.ExecutorCreationRealm())\n\n\t// Stringify\n\tprintln(\"----\")\n\tprintln(impl.StringifyProposal(prop))\n}\n\n// Output:\n// Executor string: Test executor description\n// Executor creation realm: gno.land/r/template/contract\n// ----\n//\n// ### Title: Test Proposal Title\n//\n// ### Proposed by: g1w3jhxazlta047h6lta047h6lta047h6lwmjv0n\n//\n// This is a test proposal description to verify StringifyProposal works correctly.\n//\n// This proposal contains the following metadata:\n//\n// Test executor description\n//\n// Executor created in: gno.land/r/template/contract\n"},{"name":"types.gno","body":"package impl\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\ntype Law struct {\n\tSupermajority float64\n}\n\nfunc NewLaw(supermajority float64) Law {\n\treturn Law{Supermajority: supermajority}\n}\n\nfunc (l *Law) String() string {\n\treturn ufmt.Sprintf(\"This law contains the following data:\\n\\n- Supermajority: %v%%\", l.Supermajority)\n}\n\n// ProposalsStatuses contains the status of all the proposals indexed by the proposal ID.\ntype ProposalsStatuses struct {\n\t*bptree.BPTree // map[int]*proposalStatus\n}\n\nfunc NewProposalsStatuses() ProposalsStatuses {\n\treturn ProposalsStatuses{bptree.NewBPTree32()}\n}\n\nfunc (pss ProposalsStatuses) GetStatus(id dao.ProposalID) *proposalStatus {\n\tif pss.BPTree == nil {\n\t\treturn nil\n\t}\n\n\tpids := id.String()\n\tpsv := pss.Get(pids)\n\tif psv == nil {\n\t\treturn nil\n\t}\n\n\tps, ok := psv.(*proposalStatus)\n\tif !ok {\n\t\tpanic(\"ProposalsStatuses must contains only proposalStatus types\")\n\t}\n\n\treturn ps\n}\n\ntype proposalStatus struct {\n\tYesVotes     memberstore.MembersByTier\n\tNoVotes      memberstore.MembersByTier\n\tAbstainVotes memberstore.MembersByTier\n\tAllVotes     memberstore.MembersByTier\n\n\tAccepted bool\n\tDenied   bool\n\n\tDeniedReason string\n\n\tTiersAllowedToVote []string\n}\n\nfunc getMembers(cur realm) memberstore.MembersByTier {\n\treturn memberstore.Get(0, cur)\n}\n\nfunc newEmptyVoteStore() memberstore.MembersByTier {\n\tmbt := memberstore.NewMembersByTier()\n\tmbt.SetTier(memberstore.T1)\n\tmbt.SetTier(memberstore.T2)\n\tmbt.SetTier(memberstore.T3)\n\treturn mbt\n}\n\nfunc newProposalStatus(allowedToVote []string) *proposalStatus {\n\treturn \u0026proposalStatus{\n\t\tYesVotes:           newEmptyVoteStore(),\n\t\tNoVotes:            newEmptyVoteStore(),\n\t\tAbstainVotes:       newEmptyVoteStore(),\n\t\tAllVotes:           newEmptyVoteStore(),\n\t\tTiersAllowedToVote: allowedToVote,\n\t}\n}\n\n// totalPower computes the total voting power dynamically from current members\n// rather than using a snapshot. See https://github.com/gnolang/gno/pull/5271#discussion_r2952523023\n//\n// Non-crossing: rlm is threaded into the crossing getMembers(cross(rlm))\n// call. The proposalStatus methods that compose this (votePowerPercent,\n// YesPercent etc., String) all take `_ int, rlm realm` for the same reason.\nfunc (ps *proposalStatus) totalPower(_ int, rlm realm) float64 {\n\tmembers := getMembers(cross(rlm))\n\tvar tp float64\n\tfor _, tn := range ps.TiersAllowedToVote {\n\t\tpower := memberstore.GetTierPower(tn, members)\n\t\ttp += power * float64(members.GetTierSize(tn))\n\t}\n\treturn tp\n}\n\nfunc (ps *proposalStatus) votePowerPercent(_ int, rlm realm, votes memberstore.MembersByTier) float64 {\n\tmembers := getMembers(cross(rlm))\n\tvar vp float64\n\tmemberstore.IterateTiers(func(tn string, tier memberstore.Tier) bool {\n\t\tpower := memberstore.GetTierPower(tn, members)\n\t\tts := votes.GetTierSize(tn)\n\t\tvp = vp + (power * float64(ts))\n\t\treturn false\n\t})\n\ttp := ps.totalPower(0, rlm)\n\tif tp == 0 {\n\t\treturn 0\n\t}\n\treturn (vp / tp) * 100\n}\n\nfunc (ps *proposalStatus) YesPercent(_ int, rlm realm) float64 {\n\treturn ps.votePowerPercent(0, rlm, ps.YesVotes)\n}\n\nfunc (ps *proposalStatus) NoPercent(_ int, rlm realm) float64 {\n\treturn ps.votePowerPercent(0, rlm, ps.NoVotes)\n}\n\nfunc (ps *proposalStatus) AbstainPercent(_ int, rlm realm) float64 {\n\treturn ps.votePowerPercent(0, rlm, ps.AbstainVotes)\n}\n\nfunc (ps *proposalStatus) IsAllowed(tier string) bool {\n\tfor _, ta := range ps.TiersAllowedToVote {\n\t\tif ta == tier {\n\t\t\treturn true\n\t\t}\n\t}\n\n\treturn false\n}\n\n// XXX: can be optimized by passing down total power to Yes/No/Abstain percent fn to avoid re-computing\nfunc (ps *proposalStatus) String(_ int, rlm realm) string {\n\tvar sb strings.Builder\n\tsb.WriteString(\"### Stats\\n\")\n\n\tif ps.Accepted {\n\t\tsb.WriteString(\"- **PROPOSAL HAS BEEN ACCEPTED**\\n\")\n\t} else if ps.Denied {\n\t\tsb.WriteString(\"- **PROPOSAL HAS BEEN DENIED**\\n\")\n\t\tif ps.DeniedReason != \"\" {\n\t\t\tsb.WriteString(\"REASON: \")\n\t\t\tsb.WriteString(ps.DeniedReason)\n\t\t\tsb.WriteString(\"\\n\")\n\t\t}\n\t} else {\n\t\tsb.WriteString(\"- **Proposal is open for votes**\\n\")\n\t}\n\n\tsb.WriteString(\"- Tiers eligible to vote: \")\n\tsb.WriteString(strings.Join(ps.TiersAllowedToVote, \", \"))\n\tsb.WriteString(\"\\n\")\n\n\tsb.WriteString(ufmt.Sprintf(\"- YES PERCENT: %v%%\\n\", ps.YesPercent(0, rlm)))\n\tsb.WriteString(ufmt.Sprintf(\"- NO PERCENT: %v%%\\n\", ps.NoPercent(0, rlm)))\n\tsb.WriteString(ufmt.Sprintf(\"- ABSTAIN PERCENT: %v%%\\n\", ps.AbstainPercent(0, rlm)))\n\n\treturn sb.String()\n}\n\nfunc StringifyVotes(_ int, rlm realm, ps *proposalStatus) string {\n\tvar sb strings.Builder\n\n\twriteVotes(0, rlm, \u0026sb, ps.YesVotes, \"YES\")\n\twriteVotes(0, rlm, \u0026sb, ps.NoVotes, \"NO\")\n\twriteVotes(0, rlm, \u0026sb, ps.AbstainVotes, \"ABSTAIN\")\n\n\tif sb.String() == \"\" {\n\t\treturn \"No one voted yet.\"\n\t}\n\n\treturn sb.String()\n}\n\nfunc writeVotes(_ int, rlm realm, sb *strings.Builder, t memberstore.MembersByTier, title string) {\n\tif t.Size() == 0 {\n\t\treturn\n\t}\n\tmembers := getMembers(cross(rlm))\n\tt.Iterate(\"\", \"\", func(tn string, value interface{}) bool {\n\t\t_, ok := memberstore.GetTier(tn)\n\t\tif !ok {\n\t\t\tpanic(\"tier not found\")\n\t\t}\n\n\t\tpower := memberstore.GetTierPower(tn, members)\n\n\t\tsb.WriteString(ufmt.Sprintf(\"%v from %v (VPPM %v):\\n\\n\", title, tn, power))\n\t\tms, _ := value.(*bptree.BPTree)\n\t\tms.Iterate(\"\", \"\", func(addr string, _ interface{}) bool {\n\t\t\tsb.WriteString(\"- \" + tryResolveAddr(address(addr)) + \"\\n\")\n\t\t\treturn false\n\t\t})\n\n\t\tsb.WriteString(\"\\n\")\n\n\t\treturn false\n\t})\n}\n\nfunc StringifyProposal(p *dao.Proposal) string {\n\tout := ufmt.Sprintf(`\n### Title: %s\n\n### Proposed by: %s\n\n%s\n`, p.Title(), p.Author(), p.Description())\n\n\tif p.ExecutorString() != \"\" {\n\t\tout += ufmt.Sprintf(`\nThis proposal contains the following metadata:\n\n%s\n\nExecutor created in: %s\n`, p.ExecutorString(), p.ExecutorCreationRealm())\n\t}\n\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Njm1RVYw7CYK6Bj3nGn0v0YFxNoaCisDYaYkQXoptoTjfW0994icB7tiWp0KCfDDfZe30zGl1Hxnwk1lnliTDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da","package":{"name":"init","path":"gno.land/r/gov/dao/v3/init","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gov/dao/v3/init\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da\"\n"},{"name":"init.gno","body":"package init\n\nimport (\n\t\"chain/runtime\"\n\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/impl\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nfunc Init(cur realm) {\n\tassertIsDevChain()\n\n\t// This is needed because state is saved between unit tests,\n\t// and we want to avoid having real members used on tests\n\tmemberstore.Get(0, cur).DeleteAll()\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(impl.NewGovDAO(), []string{\"gno.land/r/gov/dao/v3/impl\"}))\n}\n\nfunc InitWithUsers(cur realm, addrs ...address) {\n\tassertIsDevChain()\n\n\t// This is needed because state is saved between unit tests,\n\t// and we want to avoid having real members used on tests\n\tmemberstore.Get(0, cur).DeleteAll()\n\tmemberstore.Get(0, cur).SetTier(memberstore.T1)\n\tfor _, a := range addrs {\n\t\tif !a.IsValid() {\n\t\t\tpanic(\"invalid address: \" + a.String())\n\t\t}\n\t\tmemberstore.Get(0, cur).SetMember(memberstore.T1, a, memberstore.NewMember(3))\n\t}\n\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(impl.NewGovDAO(), []string{\"gno.land/r/gov/dao/v3/impl\"}))\n}\n\nfunc assertIsDevChain() {\n\tchainID := runtime.ChainID()\n\tif chainID != \"dev\" \u0026\u0026 chainID != \"tendermint_test\" {\n\t\tpanic(\"unauthorized\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"eeHzaxwRBsF8sOAgnW+oFmuieBVQJkasErmx0+dFPfTx7K6Foj/8sh3Dwv8ITSZou0TKaNJpE4idIN80W6f0Dg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"params","path":"gno.land/r/sys/params","files":[{"name":"fee_collector.gno","body":"package params\n\nimport (\n\t\"gno.land/r/gov/dao\"\n)\n\nfunc NewSetFeeCollectorRequest(cur realm, addr address) dao.ProposalRequest {\n\treturn NewSysParamStringPropRequest(cur,\n\t\t\"auth\", \"p\", \"fee_collector\",\n\t\taddr.String(),\n\t)\n}\n"},{"name":"fee_collector_test.gno","body":"package params\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\t\"gno.land/r/gov/dao\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestSetFeeCollector(cur realm, t *testing.T) {\n\tuserRealm := testing.NewUserRealm(g1user)\n\ttesting.SetRealm(userRealm)\n\n\tpr := NewSetFeeCollectorRequest(cur, userRealm.Address())\n\tid := dao.MustCreateProposal(cross(cur), pr)\n\t_, err := dao.GetProposal(id)\n\turequire.NoError(t, err)\n\n\turequire.NotPanics(\n\t\tt, cur,\n\t\tfunc() {\n\t\t\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(id)))\n\t\t},\n\t)\n\n\turequire.NotPanics(\n\t\tt, cur,\n\t\tfunc() {\n\t\t\tdao.ExecuteProposal(cross(cur), id)\n\t\t},\n\t)\n\n\t// XXX: test that the value got properly updated, when we can get params from gno code\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/params\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"halt.gno","body":"package params\n\nimport (\n\t\"strconv\"\n\n\t\"chain\"\n\tprms \"sys/params\"\n\n\t\"gno.land/r/gov/dao\"\n)\n\nconst (\n\tnodeModulePrefix  = \"node\"\n\thaltHeightKey     = \"halt_height\"\n\thaltMinVersionKey = \"halt_min_version\"\n)\n\n// NewSetHaltRequest creates a GovDAO proposal to halt all chain nodes at the given block height.\n// Once approved and executed, nodes will gracefully stop after committing the specified block,\n// enabling coordinated chain upgrades.\n//\n// minVersion, if non-empty, sets the minimum binary version required to resume after the halt.\n// Nodes will refuse to restart unless their version satisfies the minimum requirement,\n// preventing old binaries from accidentally resuming a chain halted for an upgrade.\n// Example: minVersion=\"chain/gnoland1.1\" prevents gnoland1.0 from resuming.\n//\n// Use height=0 to cancel a previously scheduled halt.\nfunc NewSetHaltRequest(cur realm, height int64, minVersion string) dao.ProposalRequest {\n\tcallback := func(cur realm) error {\n\t\tprms.SetSysParamInt64(nodeModulePrefix, \"p\", haltHeightKey, height)\n\t\tprms.SetSysParamString(nodeModulePrefix, \"p\", haltMinVersionKey, minVersion)\n\t\tchain.Emit(\"set_halt\",\n\t\t\t\"height\", strconv.FormatInt(height, 10),\n\t\t\t\"min_version\", minVersion,\n\t\t)\n\t\treturn nil\n\t}\n\n\tvar desc string\n\tif height == 0 {\n\t\tdesc = \"Cancel the scheduled chain halt and clear the minimum version requirement.\"\n\t} else {\n\t\tdesc = \"Halt the chain at block \" + strconv.FormatInt(height, 10) + \".\"\n\t\tif minVersion != \"\" {\n\t\t\tdesc += \" Requires binary version \u003e= \" + minVersion + \" to resume.\"\n\t\t}\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, callback, \"\")\n\treturn dao.NewProposalRequest(\"Set node halt height\", desc, e)\n}\n"},{"name":"params.gno","body":"// Package params provides functions for creating parameter executors that\n// interface with the Params Keeper.\n//\n// This package enables setting various parameter types (such as strings,\n// integers, booleans, and byte slices) through the GovDAO proposal mechanism.\n// Each function returns an executor that, when called, sets the specified\n// parameter in the Params Keeper.\n//\n// The executors are designed to be used within governance proposals to modify\n// parameters dynamically. The integration with the GovDAO allows for parameter\n// changes to be proposed and executed in a controlled manner, ensuring that\n// modifications are subject to governance processes.\n//\n// Example usage:\n//\n//\t// This executor can be used in a governance proposal to set the parameter.\n//\tpr := params.NewSysParamStringPropExecutor(\"bank\", \"p\", \"restricted_denoms\")\npackage params\n\nimport (\n\t\"chain\"\n\tprms \"sys/params\"\n\n\t\"gno.land/r/gov/dao\"\n)\n\n// this is only used for emitting events.\nfunc syskey(module, submodule, name string) string {\n\treturn module + \":\" + submodule + \":\" + name\n}\n\n// assertNotValsetKey rejects governance proposals that target the\n// node:valset:* key family. Those keys are reserved for the realm-side\n// gate in r/sys/params/valset.gno (SetValsetProposal) which checks\n// the immediate caller is gno.land/r/sys/validators/v3. Without this\n// guard, a generic NewSysParam*PropRequest(\"node\",\"valset\",...) would\n// let any GovDAO supermajority bypass the v3 authorization and write\n// validator-set state directly.\nfunc assertNotValsetKey(module, submodule string) {\n\tif module == \"node\" \u0026\u0026 submodule == \"valset\" {\n\t\tpanic(\"node:valset:* is reserved for r/sys/validators/v3; use it instead of the generic factory\")\n\t}\n}\n\nfunc NewSysParamStringPropRequest(cur realm, module, submodule, name, value string) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.SetSysParamString(module, submodule, name, value) },\n\t\t\"\",\n\t)\n}\n\nfunc NewSysParamInt64PropRequest(cur realm, module, submodule, name string, value int64) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.SetSysParamInt64(module, submodule, name, value) },\n\t\t\"\",\n\t)\n}\n\nfunc NewSysParamUint64PropRequest(cur realm, module, submodule, name string, value uint64) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.SetSysParamUint64(module, submodule, name, value) },\n\t\t\"\",\n\t)\n}\n\nfunc NewSysParamBoolPropRequest(cur realm, module, submodule, name string, value bool) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.SetSysParamBool(module, submodule, name, value) },\n\t\t\"\",\n\t)\n}\n\nfunc NewSysParamBytesPropRequest(cur realm, module, submodule, name string, value []byte) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.SetSysParamBytes(module, submodule, name, value) },\n\t\t\"\",\n\t)\n}\n\nfunc NewSysParamStringsPropRequest(cur realm, module, submodule, name string, value []string) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.SetSysParamStrings(module, submodule, name, value) },\n\t\t\"\",\n\t)\n}\n\nfunc NewSysParamStringsPropRequestWithTitle(cur realm, module, submodule, name, title string, value []string) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.SetSysParamStrings(module, submodule, name, value) },\n\t\ttitle,\n\t)\n}\nfunc NewSysParamStringsPropRequestAddWithTitle(cur realm, module, submodule, name, title string, value []string) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.UpdateSysParamStrings(module, submodule, name, value, true) },\n\t\ttitle,\n\t)\n}\nfunc NewSysParamStringsPropRequestRemoveWithTitle(cur realm, module, submodule, name, title string, value []string) dao.ProposalRequest {\n\tassertNotValsetKey(module, submodule)\n\treturn newPropRequest(cur,\n\t\tsyskey(module, submodule, name),\n\t\tfunc() { prms.UpdateSysParamStrings(module, submodule, name, value, false) },\n\t\ttitle,\n\t)\n}\nfunc newPropRequest(cur realm, key string, fn func(), title string) dao.ProposalRequest {\n\tcallback := func(cur realm) error {\n\t\tfn()\n\t\tchain.Emit(\"set\", \"key\", key) // TODO document, make const, make consistent. 'k'??\n\t\treturn nil\n\t}\n\n\tif title == \"\" {\n\t\ttitle = \"Set new sys/params key\"\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, callback, \"\")\n\n\treturn dao.NewProposalRequest(title, \"This proposal wants to add a new key to sys/params: \"+key, e)\n}\n"},{"name":"params_test.gno","body":"package params\n\nimport (\n\t\"testing\"\n)\n\n// Testing this package is limited because it only contains an `std.Set` method\n// without a corresponding `std.Get` method. For comprehensive testing, refer to\n// the tests located in the r/gov/dao/ directory, specifically in one of the\n// propX_filetest.gno files.\n\nfunc TestNewStringPropRequest(cur realm, t *testing.T) {\n\tpr := NewSysParamStringPropRequest(cur, \"foo\", \"bar\", \"baz\", \"qux\")\n\tif pr.Title() == \"\" {\n\t\tt.Errorf(\"executor shouldn't be nil\")\n\t}\n}\n\nfunc TestNewSetHaltRequest(cur realm, t *testing.T) {\n\tpr := NewSetHaltRequest(cur, 100_000, \"chain/gnoland1.1\")\n\tif pr.Title() == \"\" {\n\t\tt.Errorf(\"proposal title shouldn't be empty\")\n\t}\n}\n\nfunc TestNewSetHaltRequestNoVersion(cur realm, t *testing.T) {\n\tpr := NewSetHaltRequest(cur, 100_000, \"\")\n\tif pr.Title() == \"\" {\n\t\tt.Errorf(\"proposal title shouldn't be empty\")\n\t}\n}\n\nfunc TestNewSetHaltRequestCancel(cur realm, t *testing.T) {\n\tpr := NewSetHaltRequest(cur, 0, \"\")\n\tif pr.Title() == \"\" {\n\t\tt.Errorf(\"proposal title shouldn't be empty\")\n\t}\n}\n"},{"name":"unlock.gno","body":"package params\n\nimport \"gno.land/r/gov/dao\"\n\nconst (\n\tbankModulePrefix     = \"bank\"\n\trestrictedDenomsKey  = \"restricted_denoms\"\n\tunlockTransferTitle  = \"Proposal to unlock the transfer of ugnot.\"\n\tlockTransferTitle    = \"Proposal to lock the transfer of ugnot.\"\n\tauthModulePrefix     = \"auth\"\n\tunrestrictedAddrsKey = \"unrestricted_addrs\"\n)\n\nfunc ProposeUnlockTransferRequest(cur realm) dao.ProposalRequest {\n\treturn NewSysParamStringsPropRequestWithTitle(cur, bankModulePrefix, \"p\", restrictedDenomsKey, unlockTransferTitle, []string{})\n}\n\nfunc ProposeLockTransferRequest(cur realm) dao.ProposalRequest {\n\treturn NewSysParamStringsPropRequestWithTitle(cur, bankModulePrefix, \"p\", restrictedDenomsKey, lockTransferTitle, []string{\"ugnot\"})\n}\n\nfunc ProposeAddUnrestrictedAcctsRequest(cur realm, addrs ...address) dao.ProposalRequest {\n\taddrStrings := make([]string, 0, len(addrs))\n\tfor _, addr := range addrs {\n\t\ts := addr.String()\n\t\taddrStrings = append(addrStrings, s)\n\t}\n\treturn NewSysParamStringsPropRequestAddWithTitle(cur, authModulePrefix, \"p\", unrestrictedAddrsKey, \"Add unrestricted transfer accounts\", addrStrings)\n}\n\nfunc ProposeRemoveUnrestrictedAcctsRequest(cur realm, addrs ...address) dao.ProposalRequest {\n\taddrStrings := make([]string, 0, len(addrs))\n\tfor _, addr := range addrs {\n\t\ts := addr.String()\n\t\taddrStrings = append(addrStrings, s)\n\t}\n\treturn NewSysParamStringsPropRequestRemoveWithTitle(cur, authModulePrefix, \"p\", unrestrictedAddrsKey, \"Add unrestricted transfer accounts\", addrStrings)\n}\n"},{"name":"unlock_test.gno","body":"package params\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\t\"gno.land/r/gov/dao\"\n\tini \"gno.land/r/gov/dao/v3/init\"\n)\n\nvar g1user = testutils.TestAddress(\"g1user\")\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(g1user))\n\tini.InitWithUsers(cross(cur), g1user)\n}\n\nfunc TestProUnlockTransfer(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(g1user))\n\n\tpr := ProposeUnlockTransferRequest(cur)\n\tid := dao.MustCreateProposal(cross(cur), pr)\n\tp, err := dao.GetProposal(id)\n\turequire.NoError(t, err)\n\turequire.Equal(t, unlockTransferTitle, p.Title())\n}\n\nfunc TestFailUnlockTransfer(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(g1user))\n\n\tpr := ProposeUnlockTransferRequest(cur)\n\tid := dao.MustCreateProposal(cross(cur), pr)\n\turequire.AbortsWithMessage(\n\t\tt, cur,\n\t\t\"proposal didn't reach supermajority yet: 66.66\",\n\t\tfunc() {\n\t\t\tdao.ExecuteProposal(cross(cur), id)\n\t\t},\n\t)\n}\n\nfunc TestExeUnlockTransfer(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(g1user))\n\n\tpr := ProposeUnlockTransferRequest(cur)\n\tid := dao.MustCreateProposal(cross(cur), pr)\n\t_, err := dao.GetProposal(id)\n\turequire.NoError(t, err)\n\t// urequire.True(t, dao.Active == p.Status()) // TODO\n\n\turequire.NotPanics(\n\t\tt, cur,\n\t\tfunc() {\n\t\t\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(id)))\n\t\t},\n\t)\n\n\turequire.NotPanics(\n\t\tt, cur,\n\t\tfunc() {\n\t\t\tdao.ExecuteProposal(cross(cur), id)\n\t\t},\n\t)\n}\n"},{"name":"valoper.gno","body":"package params\n\nimport (\n\tprms \"sys/params\"\n)\n\n// Valoper sys-param keys consumed by r/gnops/valopers (Register +\n// UpdateSigningKey). Governance can update them via the generic\n// NewSysParam*PropRequest factories — no realm-side gate is needed\n// because the values only affect fees and throttle inside valopers.\nconst (\n\tvaloperSubmodule = \"valoper\"\n\n\tvaloperRegisterFeeKey          = \"register_fee\"\n\tvaloperRotationFeeKey          = \"rotation_fee\"\n\tvaloperRotationPeriodBlocksKey = \"rotation_period_blocks\"\n)\n\n// Default values used when the sys-param has never been set by\n// governance. Zero fees (GNOT transfers are disabled chain-wide\n// pre-fork) and a ~1-hour throttle at 6s/block.\nconst (\n\tdefaultValoperRegisterFee          = uint64(0)\n\tdefaultValoperRotationFee          = uint64(0)\n\tdefaultValoperRotationPeriodBlocks = int64(600)\n)\n\n// GetValoperRegisterFee returns the fee (in ugnot) required to call\n// valopers.Register. Defaults to 0 if governance hasn't set it.\nfunc GetValoperRegisterFee() uint64 {\n\tv, ok := prms.GetSysParamUint64(nodeModulePrefix, valoperSubmodule, valoperRegisterFeeKey)\n\tif !ok {\n\t\treturn defaultValoperRegisterFee\n\t}\n\treturn v\n}\n\n// GetValoperRotationFee returns the fee (in ugnot) required to call\n// valopers.UpdateSigningKey. Defaults to 0.\nfunc GetValoperRotationFee() uint64 {\n\tv, ok := prms.GetSysParamUint64(nodeModulePrefix, valoperSubmodule, valoperRotationFeeKey)\n\tif !ok {\n\t\treturn defaultValoperRotationFee\n\t}\n\treturn v\n}\n\n// GetValoperRotationPeriodBlocks returns the per-operator rotation\n// throttle (in blocks). Defaults to ~1h worth at 6s/block (600).\n// This is the primary anti-spam defense pre-fee while rotation_fee\n// stays at 0; tightens further once non-zero fees become enforceable.\nfunc GetValoperRotationPeriodBlocks() int64 {\n\tv, ok := prms.GetSysParamInt64(nodeModulePrefix, valoperSubmodule, valoperRotationPeriodBlocksKey)\n\tif !ok {\n\t\treturn defaultValoperRotationPeriodBlocks\n\t}\n\treturn v\n}\n"},{"name":"valset.gno","body":"package params\n\nimport (\n\t\"chain\"\n\t\"errors\"\n\t\"strconv\"\n\t\"strings\"\n\n\tprms \"sys/params\"\n\n\t\"gno.land/p/sys/validators\"\n)\n\n// Param keys read by gno.land/pkg/gnoland (EndBlocker).\n// Keep in sync with gno.land/pkg/gnoland/node_params.go.\n// nodeModulePrefix is declared in halt.gno (same package).\nconst (\n\tvalsetSubmodule = \"valset\"\n\n\t// dirty signals the chain that the proposed valset differs from the\n\t// current applied one. Realm sets true; EndBlocker clears.\n\tvalsetDirtyKey = \"dirty\"\n\n\t// One []string per slot; each entry has the form \"\u003cpubkey\u003e:\u003cpower\u003e\"\n\t// (bech32 pubkey + decimal power). Address is derived from pubkey\n\t// on the chain side and not stored.\n\t//\n\t//   proposed = v3's full target valset\n\t//   current  = chain-managed: the set that becomes ACTIVE AT H+2\n\t//              once the most recent EndBlock's updates apply.\n\t//              NOT necessarily the set actively signing the current\n\t//              block — see ABCI H+2 sequencing.\n\tvalsetProposedKey = \"proposed\"\n\tvalsetCurrentKey  = \"current\"\n\n\t// Only this realm may write valset:proposed and valset:dirty.\n\t// valset:current is chain-managed (see ctx-sentinel in node_params.go).\n\tvalsetAuthorizedRealm = \"gno.land/r/sys/validators/v3\"\n)\n\n// SetValsetProposal publishes the realm's desired valset. Each entry is\n// \"\u003cbech32-pubkey\u003e:\u003cdecimal-power\u003e\"; power=0 removes the validator.\n// The chain reads this on the next EndBlocker, diffs it against\n// valset:current, and propagates the changes to consensus.\nfunc SetValsetProposal(cur realm, entries []string) {\n\tassertValsetCaller(0, cur)\n\tprms.SetSysParamStrings(nodeModulePrefix, valsetSubmodule, valsetProposedKey, entries)\n\tprms.SetSysParamBool(nodeModulePrefix, valsetSubmodule, valsetDirtyKey, true)\n}\n\n// GetValsetEntries returns the chain's authoritative committed\n// validator set (the contents of valset:current). This is the\n// V_{H+2} view — the set that will be active at H+2 once the most\n// recent EndBlock's updates apply, NOT the set signing the current\n// block. Callers that want \"what v3 reports as the current\n// validator set\" — including the in-flight proposed set during\n// the dirty window — should call GetValsetEffective instead.\nfunc GetValsetEntries() []validators.Validator {\n\treturn parseValsetSlot(valsetCurrentKey)\n}\n\n// ValsetDirty reports whether valset:proposed is awaiting EndBlocker.\n// Realm callers MUST treat this as transient: the dirty flag is set\n// by SetValsetProposal and cleared by the chain's EndBlocker (every\n// block where dirty=true on entry exits with dirty=false).\nfunc ValsetDirty() bool {\n\td, _ := prms.GetSysParamBool(nodeModulePrefix, valsetSubmodule, valsetDirtyKey)\n\treturn d\n}\n\n// GetValsetEffective returns the set that WILL be active at H+2:\n// valset:proposed if dirty, else valset:current. Used by v3 so that\n// (a) reads after a same-block proposal callback see that proposal's\n// effects, and (b) sequential same-block proposals accumulate\n// correctly on top of each other.\n//\n// Misuse warning: this exists for r/sys/validators/v3's internal\n// reads. Other realms making \"is X a validator\" decisions should\n// call v3.IsValidator, not this directly, so future changes to v3's\n// read semantics propagate uniformly.\nfunc GetValsetEffective() []validators.Validator {\n\tkey := valsetCurrentKey\n\tif ValsetDirty() {\n\t\tkey = valsetProposedKey\n\t}\n\treturn parseValsetSlot(key)\n}\n\nfunc parseValsetSlot(key string) []validators.Validator {\n\traw, _ := prms.GetSysParamStrings(nodeModulePrefix, valsetSubmodule, key)\n\tout := make([]validators.Validator, 0, len(raw))\n\tfor _, e := range raw {\n\t\tv, err := parseEntry(e)\n\t\tif err != nil {\n\t\t\tpanic(\"valset:\" + key + \" corrupted: \" + err.Error())\n\t\t}\n\t\tout = append(out, v)\n\t}\n\treturn out\n}\n\n// parseEntry splits \"\u003cbech32-pubkey\u003e:\u003cdecimal-power\u003e\" and derives the\n// validator address via the chain.PubKeyAddress native helper.\nfunc parseEntry(entry string) (validators.Validator, error) {\n\tpkStr, pStr, ok := strings.Cut(entry, \":\")\n\tif !ok {\n\t\treturn validators.Validator{}, errors.New(\"missing ':' separator in \" + entry)\n\t}\n\taddr, err := chain.PubKeyAddress(pkStr)\n\tif err != nil {\n\t\treturn validators.Validator{}, err\n\t}\n\tpower, err := strconv.ParseUint(pStr, 10, 64)\n\tif err != nil {\n\t\treturn validators.Validator{}, err\n\t}\n\treturn validators.Validator{\n\t\tAddress:     addr,\n\t\tPubKey:      pkStr,\n\t\tVotingPower: power,\n\t}, nil\n}\n\nfunc assertValsetCaller(_ int, rlm realm) {\n\t// Defense-in-depth IsCurrent gate. Current call sites all pass live\n\t// cur (cross(cur) from cache.gno / proposal.gno), so this never\n\t// fires today — but the helper trusts its rlm input, and a future\n\t// caller that threads a stashed/sibling-frame realm value would\n\t// silently bypass the PkgPath check below (Class-2 designation\n\t// forgery; see docs/resources/gno-security.md). Gating here makes\n\t// the precondition enforceable rather than convention.\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tif rlm.Previous().PkgPath() != valsetAuthorizedRealm {\n\t\tpanic(\"unauthorized: only \" + valsetAuthorizedRealm + \" may write valset params\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"p0+rQWLIgZ0gWRcTHvlGyt7o8TEE3r5ViMuY02Q1FN+ymXBl6l3ZQUEkx2E1sxDfm0sVvrCdO3GW9yWdU9g9CA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"validators","path":"gno.land/r/sys/validators/v3","files":[{"name":"cache.gno","body":"package validators\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\t\"sort\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/sys/validators\"\n\tsysparams \"gno.land/r/sys/params\"\n)\n\n// valopersRealmPath is the only realm allowed to refresh valoperCache\n// or invoke RotateValoperSigningKey. Both auth checks below depend on\n// being called via `cross` from a crossing function in valopers.\nconst valopersRealmPath = \"gno.land/r/gnops/valopers\"\n\n// valoperCache mirrors the (operator -\u003e current signing key) view from\n// r/gnops/valopers. Written by valopers via NotifyValoperChanged. Read\n// by future operator-keyed proposal flow (step 5).\n//\n// Pushing (valopers passes the values as args) rather than pulling\n// (v3 imports valopers and reads them) — pulling would create an\n// import cycle, since valopers already imports v3 for IsValidator.\nvar valoperCache = bptree.NewBPTree32()\n\ntype cacheEntry struct {\n\tSigningPubKey  string\n\tSigningAddress address\n\tKeepRunning    bool\n}\n\n// assertValopersCaller panics if the caller realm is not r/gnops/valopers.\n// Per docs/resources/gno-interrealm.md, this check works only when (a)\n// the host function is a crossing function (`cur realm`) and (b) it's\n// invoked via `cross` from a crossing function in valopers. Then\n// PreviousRealm() shifts exactly one frame to valopers. A user MsgCall\n// would see PreviousRealm() == UserRealm (pkgpath \"\"); a third realm\n// cross-call would see its own pkgpath. Either fails this check.\nfunc assertValopersCaller(_ int, rlm realm) {\n\t// Defense-in-depth IsCurrent gate. Current call sites pass live\n\t// cur (cross(cur) from valopers), so this never fires today — but\n\t// the helper trusts its rlm input, and any future call that threads\n\t// a stashed/sibling-frame realm value with .Previous().PkgPath() ==\n\t// valopersRealmPath would silently bypass the gate (Class-2\n\t// designation forgery; see docs/resources/gno-security.md). Gating\n\t// here makes the precondition enforceable rather than convention.\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tcaller := rlm.Previous().PkgPath()\n\tif caller != valopersRealmPath {\n\t\tpanic(\"caller realm must be \" + valopersRealmPath + \", got \" + caller)\n\t}\n}\n\n// NotifyValoperChanged refreshes the cached entry for op. Auth: caller\n// realm must be r/gnops/valopers.\n//\n// READ-ONLY against valopers: by design this function does not call\n// back into valopers (no pull). Valopers pushes the current values in\n// as args. This eliminates the confused-deputy class where v3 → valopers\n// callbacks would make valopers see v3 as PreviousRealm.\nfunc NotifyValoperChanged(cur realm, op address, signingPubKey string, signingAddress address, keepRunning bool) {\n\tassertValopersCaller(0, cur)\n\tvaloperCache.Set(op.String(), cacheEntry{\n\t\tSigningPubKey:  signingPubKey,\n\t\tSigningAddress: signingAddress,\n\t\tKeepRunning:    keepRunning,\n\t})\n}\n\n// RotateValoperSigningKey applies a signing-key rotation to the\n// effective valset and publishes the new full set via sysparams.\n// Auth: caller realm must be r/gnops/valopers.\n//\n// Body is read-modify-write against sysparams.GetValsetEffective so\n// concurrent same-block writers (other rotations or GovDAO executors)\n// accumulate instead of clobbering. Mirrors the executor pattern in\n// validators.gno.\n//\n// Idempotent: if the rotating operator's old signing address is not\n// currently in the effective valset (e.g., they were removed), the\n// rotation is a no-op at the sysparams level — valopers' profile and\n// signingRegistry already record the new key. Either replays cleanly.\n//\n// Emits ValoperRotated event with op + old/new addresses + height.\nfunc RotateValoperSigningKey(cur realm, op address, oldPubKey, newPubKey string) {\n\tassertValopersCaller(0, cur)\n\n\toldAddr, err := chain.PubKeyAddress(oldPubKey)\n\tif err != nil {\n\t\tpanic(\"invalid oldPubKey: \" + err.Error())\n\t}\n\tnewAddr, err := chain.PubKeyAddress(newPubKey)\n\tif err != nil {\n\t\tpanic(\"invalid newPubKey: \" + err.Error())\n\t}\n\n\tbaseline := sysparams.GetValsetEffective()\n\tset := make(map[address]validators.Validator, len(baseline))\n\tfor _, v := range baseline {\n\t\tset[v.Address] = v\n\t}\n\n\t// The rotating operator must currently be in the active set; if\n\t// not (operator removed before rotating), nothing to publish.\n\t// Valopers-side state has already been updated regardless.\n\tprev, ok := set[oldAddr]\n\tif !ok {\n\t\tchain.Emit(\n\t\t\t\"ValoperRotated\",\n\t\t\t\"op\", op.String(),\n\t\t\t\"oldAddr\", oldAddr.String(),\n\t\t\t\"newAddr\", newAddr.String(),\n\t\t\t\"height\", strconv.FormatInt(runtime.ChainHeight(), 10),\n\t\t\t\"applied\", \"false\",\n\t\t)\n\t\treturn\n\t}\n\n\tdelete(set, oldAddr)\n\tset[newAddr] = validators.Validator{\n\t\tAddress:     newAddr,\n\t\tPubKey:      newPubKey,\n\t\tVotingPower: prev.VotingPower,\n\t}\n\n\t// Defense-in-depth: a delete+insert in this branch always leaves\n\t// at least one entry (the freshly inserted newAddr), so an empty\n\t// set is unreachable today. Panic explicitly anyway: a future\n\t// refactor of this body that ends up publishing an empty set\n\t// would otherwise be silently swallowed by the EndBlocker\n\t// (which logs and clears dirty for empty publishes), masking\n\t// the regression.\n\tif len(set) == 0 {\n\t\tpanic(\"rotation would empty the validator set; refused to keep consensus liveness\")\n\t}\n\n\tentries := make([]string, 0, len(set))\n\tfor _, v := range set {\n\t\tentries = append(entries, v.PubKey+\":\"+strconv.FormatUint(v.VotingPower, 10))\n\t}\n\tsort.Strings(entries)\n\tsysparams.SetValsetProposal(cross(cur), entries)\n\n\tchain.Emit(\n\t\t\"ValoperRotated\",\n\t\t\"op\", op.String(),\n\t\t\"oldAddr\", oldAddr.String(),\n\t\t\"newAddr\", newAddr.String(),\n\t\t\"height\", strconv.FormatInt(runtime.ChainHeight(), 10),\n\t\t\"applied\", \"true\",\n\t)\n}\n\n// AssertGenesisValopersConsistent panics if any entry in valset:current\n// (the seeded genesis valset) lacks a corresponding valoperCache profile\n// whose SigningAddress matches.\n//\n// **Genesis-mode only.** The function refuses to run unless\n// runtime.ChainHeight() == 0. This is the documented intended usage\n// (last migration .jsonl tx, before any block has been produced) and\n// also closes a post-genesis MsgCall DoS surface — without the guard,\n// an attacker could pay gas to repeatedly invoke an O(N) iteration\n// over valoperCache + valset:current after the chain is live.\n//\n// gnoland's InitChainer auto-runs this assertion at end of\n// genesis-mode replay when GnoGenesisState.PastChainIDs is non-empty;\n// failure aborts the boot unconditionally. valoper-seed and\n// hand-crafted migration .jsonls do NOT need to emit the call\n// themselves.\n//\n// Crossing function: callable via MsgCall (only at genesis-mode).\n// Doesn't mutate state — pure invariant check. Inverse direction\n// (every valoperCache entry must have a corresponding valset:current\n// entry) is intentionally NOT checked: extra valoper profiles\n// registered without immediate valset inclusion are a normal\n// post-genesis state.\nfunc AssertGenesisValopersConsistent(cur realm) {\n\tif runtime.ChainHeight() != 0 {\n\t\tpanic(\"AssertGenesisValopersConsistent is only callable during genesis-mode replay (ChainHeight()==0)\")\n\t}\n\n\t// Collect the signing addresses present in valoperCache.\n\tseen := map[string]bool{}\n\tvaloperCache.Iterate(\"\", \"\", func(_ string, raw any) bool {\n\t\tentry := raw.(cacheEntry)\n\t\tseen[entry.SigningAddress.String()] = true\n\t\treturn false\n\t})\n\n\t// Every entry in valset:current must appear in seen.\n\tfor _, v := range sysparams.GetValsetEntries() {\n\t\tif !seen[v.Address.String()] {\n\t\t\tpanic(\"genesis-validator \" + v.Address.String() + \" has no corresponding valoper profile (signing address not in valoperCache)\")\n\t\t}\n\t}\n}\n"},{"name":"cache_test.gno","body":"package validators\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\tsysparams \"gno.land/r/sys/params\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\n// resetCache clears valoperCache so subtests don't leak state.\nfunc resetCache() {\n\tvaloperCache = bptree.NewBPTree32()\n}\n\nfunc TestNotifyValoperChanged_HappyPath(cur realm, t *testing.T) {\n\tresetCache()\n\n\top := testutils.TestAddress(\"op-A\")\n\tsigningAddr := mustAddr(t, pubKeyA)\n\n\t// Caller realm = valopers; auth check passes.\n\ttesting.SetRealm(testing.NewCodeRealm(valopersRealmPath))\n\n\tNotifyValoperChanged(cross(cur), op, pubKeyA, signingAddr, true)\n\n\trawEntry := valoperCache.Get(op.String())\n\turequire.NotNil(t, rawEntry, \"cache entry must exist\")\n\tentry := rawEntry.(cacheEntry)\n\tuassert.Equal(t, pubKeyA, entry.SigningPubKey)\n\tuassert.Equal(t, signingAddr, entry.SigningAddress)\n\tuassert.Equal(t, true, entry.KeepRunning)\n\n\t// Subsequent call updates the same slot.\n\tsigningAddrB := mustAddr(t, pubKeyB)\n\tNotifyValoperChanged(cross(cur), op, pubKeyB, signingAddrB, false)\n\n\trawEntry = valoperCache.Get(op.String())\n\turequire.NotNil(t, rawEntry)\n\tentry = rawEntry.(cacheEntry)\n\tuassert.Equal(t, pubKeyB, entry.SigningPubKey)\n\tuassert.Equal(t, signingAddrB, entry.SigningAddress)\n\tuassert.Equal(t, false, entry.KeepRunning)\n}\n\nfunc TestNotifyValoperChanged_RejectsNonValopersCaller(cur realm, t *testing.T) {\n\tresetCache()\n\n\top := testutils.TestAddress(\"op-A\")\n\tsigningAddr := mustAddr(t, pubKeyA)\n\n\t// Caller realm = some other realm; auth check rejects.\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/attacker\"))\n\n\tuassert.AbortsContains(t, cur, \"caller realm must be \"+valopersRealmPath, func() {\n\t\tNotifyValoperChanged(cross(cur), op, pubKeyA, signingAddr, true)\n\t})\n\n\t// User MsgCall — UserRealm has empty pkgpath; rejected the same way.\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"user\")))\n\n\tuassert.AbortsContains(t, cur, \"caller realm must be \"+valopersRealmPath, func() {\n\t\tNotifyValoperChanged(cross(cur), op, pubKeyA, signingAddr, true)\n\t})\n}\n\nfunc TestRotateValoperSigningKey_AppliesRotation(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\n\t// Seed the valset with op-A signing under pubKeyA at power=10.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":10\"})\n\n\top := testutils.TestAddress(\"op-A\")\n\ttesting.SetRealm(testing.NewCodeRealm(valopersRealmPath))\n\n\tRotateValoperSigningKey(cross(cur), op, pubKeyA, pubKeyB)\n\n\t// After rotation the effective set (proposed-when-dirty) should\n\t// contain pubKeyB at the same power; pubKeyA should be gone.\n\tuassert.True(t, sysparams.ValsetDirty(), \"dirty bit set after RotateValoperSigningKey\")\n\n\teffective := sysparams.GetValsetEffective()\n\turequire.Equal(t, 1, len(effective))\n\tuassert.Equal(t, pubKeyB, effective[0].PubKey)\n\tuassert.Equal(t, uint64(10), effective[0].VotingPower)\n}\n\nfunc TestRotateValoperSigningKey_OperatorNotInValset_NoOp(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\n\t// Empty valset; the operator is not currently signing.\n\top := testutils.TestAddress(\"op-A\")\n\ttesting.SetRealm(testing.NewCodeRealm(valopersRealmPath))\n\n\t// Should not panic; no sysparams write expected (dirty stays false).\n\tuassert.NotAborts(t, cur, func() {\n\t\tRotateValoperSigningKey(cross(cur), op, pubKeyA, pubKeyB)\n\t})\n\n\tuassert.False(t, sysparams.ValsetDirty(), \"dirty bit must remain false on no-op rotation\")\n\tuassert.Equal(t, 0, len(sysparams.GetValsetEffective()))\n}\n\nfunc TestRotateValoperSigningKey_RejectsNonValopersCaller(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":10\"})\n\n\top := testutils.TestAddress(\"op-A\")\n\n\t// Foreign realm — rejected.\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/attacker\"))\n\tuassert.AbortsContains(t, cur, \"caller realm must be \"+valopersRealmPath, func() {\n\t\tRotateValoperSigningKey(cross(cur), op, pubKeyA, pubKeyB)\n\t})\n\n\t// User MsgCall — UserRealm pkgpath is \"\" — rejected.\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"user\")))\n\tuassert.AbortsContains(t, cur, \"caller realm must be \"+valopersRealmPath, func() {\n\t\tRotateValoperSigningKey(cross(cur), op, pubKeyA, pubKeyB)\n\t})\n\n\t// Sanity: the valset wasn't mutated by the rejected calls (dirty\n\t// bit stays false; effective set still equals the seeded current).\n\tuassert.False(t, sysparams.ValsetDirty())\n\teffective := sysparams.GetValsetEffective()\n\turequire.Equal(t, 1, len(effective))\n\tuassert.Equal(t, pubKeyA, effective[0].PubKey)\n}\n\nfunc TestAssertGenesisValopersConsistent_HappyPath(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\ttesting.SetHeight(0) // assertion is genesis-mode only\n\n\t// Seed valset:current with two genesis validators.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":10\",\n\t\tpubKeyB + \":5\",\n\t})\n\n\t// Seed valoperCache with profiles whose SigningAddress matches.\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: true},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\tuassert.NotAborts(t, cur, func() {\n\t\tAssertGenesisValopersConsistent(cross(cur))\n\t})\n}\n\nfunc TestAssertGenesisValopersConsistent_PanicsOnMissingProfile(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\ttesting.SetHeight(0)\n\n\t// Two validators in valset:current but only one has a profile.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":10\",\n\t\tpubKeyB + \":5\",\n\t})\n\n\topA := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: true},\n\t\t// op for pubKeyB intentionally missing.\n\t})\n\n\tuassert.AbortsContains(t, cur, \"no corresponding valoper profile\", func() {\n\t\tAssertGenesisValopersConsistent(cross(cur))\n\t})\n}\n\nfunc TestAssertGenesisValopersConsistent_EmptyValsetTrivial(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\ttesting.SetHeight(0)\n\n\t// Empty valset → assertion trivially holds (no entries to check).\n\tuassert.NotAborts(t, cur, func() {\n\t\tAssertGenesisValopersConsistent(cross(cur))\n\t})\n}\n\nfunc TestAssertGenesisValopersConsistent_RejectsPostGenesis(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\t// Default test height is non-zero (123); the assertion must\n\t// refuse to run outside genesis-mode replay.\n\tuassert.AbortsContains(t, cur, \"only callable during genesis-mode replay\", func() {\n\t\tAssertGenesisValopersConsistent(cross(cur))\n\t})\n}\n\nfunc TestRotateValoperSigningKey_AccumulatesAcrossSameBlock(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\n\t// Seed valset with two operators signing under pubKeyA and pubKeyC.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":10\",\n\t\tpubKeyC + \":5\",\n\t})\n\n\topA := testutils.TestAddress(\"op-A\")\n\topC := testutils.TestAddress(\"op-C\")\n\ttesting.SetRealm(testing.NewCodeRealm(valopersRealmPath))\n\n\t// First rotation: A → B. Read-modify-write of GetValsetEffective.\n\tRotateValoperSigningKey(cross(cur), opA, pubKeyA, pubKeyB)\n\n\t// Second rotation in the same block: C → A. Should not clobber\n\t// the first rotation; should read proposed-when-dirty as baseline.\n\tRotateValoperSigningKey(cross(cur), opC, pubKeyC, pubKeyA)\n\n\t// Final effective set: {pubKeyB:10, pubKeyA:5}. Order may differ\n\t// because GetValsetEffective parses sysparams' sorted-string slot\n\t// and returns []Validator preserving that order.\n\teffective := sysparams.GetValsetEffective()\n\turequire.Equal(t, 2, len(effective))\n\n\t// Build a power-by-pubkey map and assert.\n\tpowerOf := map[string]uint64{}\n\tfor _, v := range effective {\n\t\tpowerOf[v.PubKey] = v.VotingPower\n\t}\n\tuassert.Equal(t, uint64(10), powerOf[pubKeyB])\n\tuassert.Equal(t, uint64(5), powerOf[pubKeyA])\n\t_, ok := powerOf[pubKeyC]\n\tuassert.False(t, ok, \"pubKeyC should be replaced by pubKeyA at the rotated slot\")\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/validators/v3\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"proposal.gno","body":"package validators\n\nimport (\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"chain\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sys/validators\"\n\t\"gno.land/r/gov/dao\"\n\tsysparams \"gno.land/r/sys/params\"\n)\n\n// ValoperChange is the operator-keyed input shape for the v3 valset\n// proposal builder. Power=0 removes; Power\u003e0 adds (or upserts the\n// power on an op already in the active set — Tendermint's natural\n// ValidatorUpdate semantics).\n//\n// Each operator may appear AT MOST ONCE per proposal; duplicates are\n// rejected at create-time.\ntype ValoperChange struct {\n\tOperatorAddress address\n\tPower           uint64\n}\n\nfunc NewValoperChange(operatorAddress address, power uint64) ValoperChange {\n\treturn ValoperChange{\n\t\tOperatorAddress: operatorAddress,\n\t\tPower:           power,\n\t}\n}\n\nconst errNoValoperChanges = \"no valoper changes proposed\"\n\n// NewValidatorProposalRequest builds a GovDAO proposal that, when\n// executed, applies the deltas to the chain's effective valset and\n// publishes the new full set via SetValsetProposal.\n//\n// NON-CROSSING (no `cur realm`). Direct MsgCall is unsupported;\n// proposers route through r/gnops/valopers/proposal's facade\n// (which IS crossing and accepts user txs).\n//\n// Validation at creation time:\n//   - Each operator may appear AT MOST ONCE in changes; duplicates\n//     panic. Power changes for an op already in the active set use\n//     a single {op, newPower} entry (upsert), not the legacy\n//     remove/re-add pair.\n//   - Every ValoperChange's OperatorAddress must exist in\n//     valoperCache. Unknown operators panic.\n//   - Adds (Power \u003e 0) require KeepRunning=true. An op that has\n//     called UpdateKeepRunning(false) signals opt-out; no proposal\n//     can keep them in the active set, period.\n//\n// Pubkey resolution at execution time: the executor callback\n// re-reads valoperCache for each entry to capture the CURRENT\n// signing pubkey/address — not the creation-time one. Defends\n// against a stale (now-retired) key publication if the operator\n// rotated while the proposal sat in GovDAO. Also re-checks\n// KeepRunning so an operator flipping to KeepRunning=false between\n// propose-create and propose-execute is honored. Removes are\n// unaffected (operator address is the lookup key, not signing\n// address).\n//\n// Emits ValidatorAdded / ValidatorRemoved events per entry on\n// successful execution. (Power-upsert on an existing op also emits\n// ValidatorAdded with the new power.)\nfunc NewValidatorProposalRequest(cur realm, changes []ValoperChange, title, description string) dao.ProposalRequest {\n\tif len(changes) == 0 {\n\t\tpanic(errNoValoperChanges)\n\t}\n\ttitle = strings.TrimSpace(title)\n\tif title == \"\" {\n\t\tpanic(\"proposal title is empty\")\n\t}\n\tif len(changes) \u003e 40 {\n\t\tpanic(\"max number of allowed validators per proposal is 40\")\n\t}\n\n\t// Dedupe: each operator may appear at most once per proposal.\n\t// Power changes are now expressed as a single {op, newPower}\n\t// upsert entry, so the legacy [{op,0},{op,N}] pair is a duplicate\n\t// and rejected.\n\tseen := map[string]bool{}\n\tfor _, c := range changes {\n\t\tkey := c.OperatorAddress.String()\n\t\tif seen[key] {\n\t\t\tpanic(\"duplicate operator in proposal: \" + key)\n\t\t}\n\t\tseen[key] = true\n\t}\n\n\t// Creation-time validation: every operator must exist in cache,\n\t// and adds require KeepRunning=true. KeepRunning=false is a\n\t// binding opt-out; no proposal shape can override it.\n\tfor _, c := range changes {\n\t\trawCache := valoperCache.Get(c.OperatorAddress.String())\n\t\tif rawCache == nil {\n\t\t\tpanic(\"unknown operator: \" + c.OperatorAddress.String())\n\t\t}\n\t\tentry := rawCache.(cacheEntry)\n\t\tif c.Power \u003e 0 \u0026\u0026 !entry.KeepRunning {\n\t\t\tpanic(\"operator \" + c.OperatorAddress.String() + \" has KeepRunning=false; refusing to add (operator must call UpdateKeepRunning(true) first)\")\n\t\t}\n\t}\n\n\t// Render description against creation-time data. Voters see the\n\t// operator addresses being proposed; signing addresses are an\n\t// implementation detail resolved at exec.\n\tvar desc strings.Builder\n\tdesc.WriteString(description)\n\tif len(description) \u003e 0 {\n\t\tdesc.WriteString(\"\\n\\n\")\n\t}\n\tdesc.WriteString(\"## Validator Updates\\n\")\n\tfor _, c := range changes {\n\t\tif c.Power == 0 {\n\t\t\tdesc.WriteString(ufmt.Sprintf(\"- %s: remove\\n\", c.OperatorAddress))\n\t\t} else {\n\t\t\tdesc.WriteString(ufmt.Sprintf(\"- %s: add (power %d)\\n\", c.OperatorAddress, c.Power))\n\t\t}\n\t}\n\n\treturn dao.NewProposalRequest(title, desc.String(), newValoperChangeExecutor(cur, changes))\n}\n\n// newValoperChangeExecutor builds the GovDAO executor that, on\n// approval, applies the captured ValoperChange deltas. Resolves\n// operator → signing addr/pubkey via valoperCache at execution time\n// for adds (so a mid-flight rotation doesn't publish a stale key).\n// Removes resolve the operator's CURRENT signing address (also via\n// cache) — operator-keyed removes are immune to rotation churn.\n//\n// Power\u003e0 is an upsert against the effective valset map (keyed on\n// signing address): if the op is already present under that signing\n// address, the entry's voting power is overwritten. Tendermint\n// natively handles ValidatorUpdates as upserts, so a single-entry\n// power change is the canonical form.\nfunc newValoperChangeExecutor(cur realm, changes []ValoperChange) dao.Executor {\n\tcallback := func(cur realm) error {\n\t\tbaseline := sysparams.GetValsetEffective()\n\t\tset := make(map[address]validators.Validator, len(baseline))\n\t\tfor _, v := range baseline {\n\t\t\tset[v.Address] = v\n\t\t}\n\n\t\tfor _, c := range changes {\n\t\t\trawCache := valoperCache.Get(c.OperatorAddress.String())\n\t\t\tif rawCache == nil {\n\t\t\t\tpanic(\"operator vanished from valoperCache between propose and execute: \" + c.OperatorAddress.String())\n\t\t\t}\n\t\t\tentry := rawCache.(cacheEntry)\n\n\t\t\tif c.Power == 0 {\n\t\t\t\tif _, ok := set[entry.SigningAddress]; !ok {\n\t\t\t\t\tpanic(\"validator does not exist: \" + entry.SigningAddress.String())\n\t\t\t\t}\n\t\t\t\tdelete(set, entry.SigningAddress)\n\t\t\t\tchain.Emit(\n\t\t\t\t\t\"ValidatorRemoved\",\n\t\t\t\t\t\"op\", c.OperatorAddress.String(),\n\t\t\t\t\t\"signingAddr\", entry.SigningAddress.String(),\n\t\t\t\t)\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t// Race-safety: operator may have flipped KeepRunning=false\n\t\t\t// between propose-create and propose-execute. Re-check.\n\t\t\t// The opt-out is binding regardless of proposal shape.\n\t\t\tif !entry.KeepRunning {\n\t\t\t\tpanic(\"operator \" + c.OperatorAddress.String() + \" has KeepRunning=false at execution; refusing to add\")\n\t\t\t}\n\n\t\t\t// Upsert at the current signing address. If the entry was\n\t\t\t// already present (single-entry power change on an active\n\t\t\t// validator), this overwrites the prior power.\n\t\t\tset[entry.SigningAddress] = validators.Validator{\n\t\t\t\tAddress:     entry.SigningAddress,\n\t\t\t\tPubKey:      entry.SigningPubKey,\n\t\t\t\tVotingPower: c.Power,\n\t\t\t}\n\t\t\tchain.Emit(\n\t\t\t\t\"ValidatorAdded\",\n\t\t\t\t\"op\", c.OperatorAddress.String(),\n\t\t\t\t\"signingAddr\", entry.SigningAddress.String(),\n\t\t\t\t\"power\", strconv.FormatUint(c.Power, 10),\n\t\t\t)\n\t\t}\n\n\t\t// Liveness floor: refuse to publish an empty set.\n\t\tif len(set) == 0 {\n\t\t\tpanic(\"valset proposal would empty the validator set; refused to keep consensus liveness\")\n\t\t}\n\n\t\tentries := make([]string, 0, len(set))\n\t\tfor _, v := range set {\n\t\t\tentries = append(entries, v.PubKey+\":\"+strconv.FormatUint(v.VotingPower, 10))\n\t\t}\n\t\tsort.Strings(entries)\n\t\tsysparams.SetValsetProposal(cross(cur), entries)\n\t\treturn nil\n\t}\n\n\treturn dao.NewSimpleExecutor(0, cur, callback, \"\")\n}\n"},{"name":"proposal_test.gno","body":"package validators\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\tsysparams \"gno.land/r/sys/params\"\n)\n\n// seedCache populates valoperCache with the given (op, pubkey, kr)\n// tuples — used in tests to satisfy NewValidatorProposalRequest's\n// creation-time membership check without going through valopers.\nfunc seedCache(t *testing.T, entries []struct {\n\top          address\n\tpubKey      string\n\tkeepRunning bool\n}) {\n\tt.Helper()\n\tfor _, e := range entries {\n\t\tsigningAddr := mustAddr(t, e.pubKey)\n\t\tvaloperCache.Set(e.op.String(), cacheEntry{\n\t\t\tSigningPubKey:  e.pubKey,\n\t\t\tSigningAddress: signingAddr,\n\t\t\tKeepRunning:    e.keepRunning,\n\t\t})\n\t}\n}\n\nfunc TestNewValidatorProposalRequest_RejectsUnknownOperator(cur realm, t *testing.T) {\n\tresetCache()\n\n\top := testutils.TestAddress(\"ghost-op\")\n\n\tuassert.PanicsContains(t, cur, \"unknown operator\", func() {\n\t\t_ = NewValidatorProposalRequest(cur,\n\t\t\t[]ValoperChange{{OperatorAddress: op, Power: 1}},\n\t\t\t\"add ghost\",\n\t\t\t\"\",\n\t\t)\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_RejectsEmptyChanges(cur realm, t *testing.T) {\n\tresetCache()\n\n\tuassert.PanicsContains(t, cur, errNoValoperChanges, func() {\n\t\t_ = NewValidatorProposalRequest(cur, nil, \"title\", \"\")\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_RejectsEmptyTitle(cur realm, t *testing.T) {\n\tresetCache()\n\top := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: op, pubKey: pubKeyA, keepRunning: true}})\n\n\tuassert.PanicsContains(t, cur, \"proposal title is empty\", func() {\n\t\t_ = NewValidatorProposalRequest(cur,\n\t\t\t[]ValoperChange{{OperatorAddress: op, Power: 1}},\n\t\t\t\"   \",\n\t\t\t\"\",\n\t\t)\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_RejectsTooManyChanges(cur realm, t *testing.T) {\n\tresetCache()\n\n\t// Seed 41 cache entries so the membership check passes; the\n\t// length cap should fire before the per-entry validation.\n\tchanges := make([]ValoperChange, 41)\n\tpubkeys := []string{pubKeyA, pubKeyB, pubKeyC}\n\tfor i := 0; i \u003c 41; i++ {\n\t\top := testutils.TestAddress(\"op-\" + strconv.Itoa(i))\n\t\tpk := pubkeys[i%3]\n\t\tvaloperCache.Set(op.String(), cacheEntry{\n\t\t\tSigningPubKey:  pk,\n\t\t\tSigningAddress: mustAddr(t, pk),\n\t\t\tKeepRunning:    true,\n\t\t})\n\t\tchanges[i] = ValoperChange{OperatorAddress: op, Power: 1}\n\t}\n\n\tuassert.PanicsContains(t, cur, \"max number of allowed validators per proposal is 40\", func() {\n\t\t_ = NewValidatorProposalRequest(cur, changes, \"too many\", \"\")\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_DescriptionRendering(cur realm, t *testing.T) {\n\tresetCache()\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: true},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\tpr := NewValidatorProposalRequest(cur,\n\t\t[]ValoperChange{\n\t\t\t{OperatorAddress: opA, Power: 5},\n\t\t\t{OperatorAddress: opB, Power: 0},\n\t\t},\n\t\t\"mixed changes\",\n\t\t\"context line\",\n\t)\n\n\tdesc := pr.Description()\n\turequire.True(t, len(desc) \u003e 0)\n\tuassert.True(t, contains(desc, \"context line\"))\n\tuassert.True(t, contains(desc, \"## Validator Updates\"))\n\tuassert.True(t, contains(desc, opA.String()+\": add (power 5)\"))\n\tuassert.True(t, contains(desc, opB.String()+\": remove\"))\n}\n\nfunc TestNewValidatorProposalRequest_ExecutorReResolvesPubkey(cur realm, t *testing.T) {\n\t// Creation-time captured changes: ValoperChange refers to opA.\n\t// Cache for opA points to pubKeyA at creation. Before execution,\n\t// opA's cache entry is updated to pubKeyB. Executor must publish\n\t// the NEW pubkey, not the creation-time one.\n\tresetValset(t)\n\tresetCache()\n\n\topA := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: opA, pubKey: pubKeyA, keepRunning: true}})\n\n\tchanges := []ValoperChange{{OperatorAddress: opA, Power: 7}}\n\n\t// Build the executor; it captures `changes` by reference (slice\n\t// of structs) but resolves SigningPubKey at run-time via cache.\n\texec := newValoperChangeExecutor(cur, changes)\n\n\t// Simulate operator rotation: opA's cache entry now points to\n\t// pubKeyB. Captured changes slice is unchanged.\n\tvaloperCache.Set(opA.String(), cacheEntry{\n\t\tSigningPubKey:  pubKeyB,\n\t\tSigningAddress: mustAddr(t, pubKeyB),\n\t\tKeepRunning:    true,\n\t})\n\n\turequire.NoError(t, exec.Execute(cross(cur)))\n\n\t// Effective valset should contain pubKeyB (post-rotation), not\n\t// pubKeyA (creation-time).\n\teffective := sysparams.GetValsetEffective()\n\turequire.Equal(t, 1, len(effective))\n\tuassert.Equal(t, pubKeyB, effective[0].PubKey)\n\tuassert.Equal(t, uint64(7), effective[0].VotingPower)\n}\n\nfunc TestNewValidatorProposalRequest_RemoveOperator(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\n\t// Seed valset with opA already signing under pubKeyA.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":10\"})\n\n\topA := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: opA, pubKey: pubKeyA, keepRunning: false}})\n\n\t// Liveness floor: removing the only validator empties the set.\n\t// Executor runs inside a crossing dao.Executor.Execute call, so\n\t// the panic surfaces as an abort, not a regular panic.\n\tuassert.AbortsContains(t, cur, \"would empty the validator set\", func() {\n\t\t_ = newValoperChangeExecutor(cur, []ValoperChange{{OperatorAddress: opA, Power: 0}}).Execute(cross(cur))\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_RemoveLeavesOthers(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\n\t// Seed valset with two validators.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":10\",\n\t\tpubKeyB + \":5\",\n\t})\n\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: true},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\tchanges := []ValoperChange{{OperatorAddress: opA, Power: 0}}\n\t// Build the executor directly (private function, same package).\n\turequire.NoError(t, newValoperChangeExecutor(cur, changes).Execute(cross(cur)))\n\n\t// Effective set: only opB / pubKeyB remains.\n\teffective := sysparams.GetValsetEffective()\n\turequire.Equal(t, 1, len(effective))\n\tuassert.Equal(t, pubKeyB, effective[0].PubKey)\n}\n\nfunc TestNewValidatorProposalRequest_AllowsFullValsetReplacement(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":10\"})\n\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: false},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\tchanges := []ValoperChange{\n\t\t{OperatorAddress: opA, Power: 0},\n\t\t{OperatorAddress: opB, Power: 10},\n\t}\n\turequire.NoError(t, newValoperChangeExecutor(cur, changes).Execute(cross(cur)))\n\n\teffective := sysparams.GetValsetEffective()\n\turequire.Equal(t, 1, len(effective))\n\tuassert.Equal(t, pubKeyB, effective[0].PubKey)\n\tuassert.Equal(t, uint64(10), effective[0].VotingPower)\n}\n\nfunc TestNewValidatorProposalRequest_RejectsKeepRunningFalseAtCreation(cur realm, t *testing.T) {\n\tresetCache()\n\top := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: op, pubKey: pubKeyA, keepRunning: false}})\n\n\tuassert.PanicsContains(t, cur, \"KeepRunning=false\", func() {\n\t\t_ = NewValidatorProposalRequest(cur,\n\t\t\t[]ValoperChange{{OperatorAddress: op, Power: 1}},\n\t\t\t\"add opted-out\", \"\",\n\t\t)\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_AllowsRemoveOfKeepRunningFalse(cur realm, t *testing.T) {\n\t// KeepRunning=false is the operator's opt-out signal; removing\n\t// such an operator must still be allowed (it's the standard\n\t// exit path). Only adds are gated.\n\tresetValset(t)\n\tresetCache()\n\n\t// Seed the valset with two operators so removing one doesn't\n\t// trip the empty-valset liveness floor.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":10\",\n\t\tpubKeyB + \":5\",\n\t})\n\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: false}, // opted out\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\t// Build proposal succeeds (remove path: Power=0 ignores KeepRunning).\n\tpr := NewValidatorProposalRequest(cur,\n\t\t[]ValoperChange{{OperatorAddress: opA, Power: 0}},\n\t\t\"remove opted-out opA\", \"\",\n\t)\n\t_ = pr\n\n\t// Executor also succeeds.\n\turequire.NoError(t, newValoperChangeExecutor(cur, []ValoperChange{{OperatorAddress: opA, Power: 0}}).Execute(cross(cur)))\n}\n\nfunc TestNewValidatorProposalRequest_RejectsDuplicateOp(cur realm, t *testing.T) {\n\t// Each operator may appear at most once per proposal; any shape\n\t// that mentions the same op twice must panic at create-time.\n\tresetCache()\n\top := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: op, pubKey: pubKeyA, keepRunning: true}})\n\n\tcases := [][]ValoperChange{\n\t\t{{OperatorAddress: op, Power: 0}, {OperatorAddress: op, Power: 7}}, // remove + re-add\n\t\t{{OperatorAddress: op, Power: 7}, {OperatorAddress: op, Power: 8}}, // double add\n\t\t{{OperatorAddress: op, Power: 0}, {OperatorAddress: op, Power: 0}}, // double remove\n\t}\n\tfor _, changes := range cases {\n\t\tuassert.PanicsContains(t, cur, \"duplicate operator in proposal\", func() {\n\t\t\t_ = NewValidatorProposalRequest(cur, changes, \"dup\", \"\")\n\t\t})\n\t}\n}\n\nfunc TestNewValidatorProposalRequest_RejectsPowerUpdatePairForOptedOutOp(cur realm, t *testing.T) {\n\t// KeepRunning=false is binding: no proposal shape can keep an\n\t// opted-out operator in the active set. The dedupe rejection\n\t// fires before the KR check is even reached.\n\tresetCache()\n\top := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: op, pubKey: pubKeyA, keepRunning: false}})\n\n\tuassert.PanicsContains(t, cur, \"duplicate operator in proposal\", func() {\n\t\t_ = NewValidatorProposalRequest(cur,\n\t\t\t[]ValoperChange{\n\t\t\t\t{OperatorAddress: op, Power: 0},\n\t\t\t\t{OperatorAddress: op, Power: 7},\n\t\t\t},\n\t\t\t\"bypass attempt\", \"\",\n\t\t)\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_UpsertExistingValidator(cur realm, t *testing.T) {\n\t// Single-entry {op, newPower} against an op already in the\n\t// effective valset must upsert: the existing entry's power is\n\t// overwritten, no remove/re-add ceremony required.\n\tresetValset(t)\n\tresetCache()\n\n\t// Seed valset with two validators; we upsert opA's power.\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":1\",\n\t\tpubKeyB + \":1\",\n\t})\n\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: true},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\tchanges := []ValoperChange{{OperatorAddress: opA, Power: 9}}\n\turequire.NoError(t, newValoperChangeExecutor(cur, changes).Execute(cross(cur)))\n\n\teffective := sysparams.GetValsetEffective()\n\tpowerOf := map[string]uint64{}\n\tfor _, v := range effective {\n\t\tpowerOf[v.PubKey] = v.VotingPower\n\t}\n\tuassert.Equal(t, uint64(9), powerOf[pubKeyA], \"opA power upserted from 1 to 9\")\n\tuassert.Equal(t, uint64(1), powerOf[pubKeyB], \"opB unchanged\")\n}\n\nfunc TestNewValidatorProposalRequest_ExecutorRejectsRaceFlippedKeepRunning(cur realm, t *testing.T) {\n\t// KeepRunning=true at proposal-create time; operator flips to\n\t// false BEFORE the executor runs. Race-safety check rejects.\n\tresetValset(t)\n\tresetCache()\n\n\top := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: op, pubKey: pubKeyA, keepRunning: true}})\n\n\tchanges := []ValoperChange{{OperatorAddress: op, Power: 5}}\n\texec := newValoperChangeExecutor(cur, changes)\n\n\t// Operator flips KeepRunning=false BEFORE the executor runs.\n\tvaloperCache.Set(op.String(), cacheEntry{\n\t\tSigningPubKey:  pubKeyA,\n\t\tSigningAddress: mustAddr(t, pubKeyA),\n\t\tKeepRunning:    false,\n\t})\n\n\tuassert.AbortsContains(t, cur, \"KeepRunning=false at execution\", func() {\n\t\t_ = exec.Execute(cross(cur))\n\t})\n}\n\nfunc TestNewValidatorProposalRequest_NaturalRotationFlow_NoGhost(cur realm, t *testing.T) {\n\t// RotateValoperSigningKey publishes valset:proposed before any\n\t// subsequent executor reads, so baseline always reflects the\n\t// post-rotation state. A later power-update upserts at NEW only;\n\t// no OLD ghost.\n\tresetValset(t)\n\tresetCache()\n\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":1\",\n\t\tpubKeyB + \":5\",\n\t})\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: true},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\ttesting.SetRealm(testing.NewCodeRealm(valopersRealmPath))\n\tRotateValoperSigningKey(cross(cur), opA, pubKeyA, pubKeyC)\n\tNotifyValoperChanged(cross(cur), opA, pubKeyC, mustAddr(t, pubKeyC), true)\n\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/gov/dao/v3/impl\"))\n\turequire.NoError(t, newValoperChangeExecutor(cur,\n\t\t[]ValoperChange{{OperatorAddress: opA, Power: 2}},\n\t).Execute(cross(cur)))\n\n\teffective := sysparams.GetValsetEffective()\n\tpowerOf := map[string]uint64{}\n\tfor _, v := range effective {\n\t\tpowerOf[v.PubKey] = v.VotingPower\n\t}\n\tuassert.Equal(t, uint64(2), powerOf[pubKeyC], \"opA published at NEW power=2\")\n\tuassert.Equal(t, uint64(5), powerOf[pubKeyB], \"opB unchanged\")\n\t_, ghost := powerOf[pubKeyA]\n\tuassert.False(t, ghost, \"OLD signing key (pubKeyA) must not linger in valset\")\n\turequire.Equal(t, 2, len(effective), \"exactly two entries — opA(NEW), opB\")\n}\n\nfunc TestNewValidatorProposalRequest_PhantomBaselineDocumentsUnreachableState(cur realm, t *testing.T) {\n\t// Pin executor behavior on a phantom state (cache=NEW,\n\t// valset:current=OLD, dirty=false) — unreachable via natural\n\t// flow because RotateValoperSigningKey publishes proposed\n\t// before NotifyValoperChanged updates the cache. If a future\n\t// code path ever updates the cache without going through\n\t// Rotate, the asymmetry would be a real bug; this test pins\n\t// the current behavior so the divergence surfaces.\n\tresetValset(t)\n\tresetCache()\n\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":1\",\n\t\tpubKeyB + \":5\",\n\t})\n\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyC, keepRunning: true},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\turequire.NoError(t, newValoperChangeExecutor(cur,\n\t\t[]ValoperChange{{OperatorAddress: opA, Power: 2}},\n\t).Execute(cross(cur)))\n\n\teffective := sysparams.GetValsetEffective()\n\tpowerOf := map[string]uint64{}\n\tfor _, v := range effective {\n\t\tpowerOf[v.PubKey] = v.VotingPower\n\t}\n\tuassert.Equal(t, uint64(1), powerOf[pubKeyA], \"phantom OLD lingers from baseline\")\n\tuassert.Equal(t, uint64(2), powerOf[pubKeyC], \"executor upserts at NEW\")\n\tuassert.Equal(t, uint64(5), powerOf[pubKeyB], \"opB unchanged\")\n\turequire.Equal(t, 3, len(effective),\n\t\t\"three entries — phantom-state ghost; this state is unreachable via natural flow\")\n}\n\nfunc TestNewValidatorProposalRequest_SameBlockExecuteThenRotate(cur realm, t *testing.T) {\n\t// Same-block ordering: proposal-execute writes proposed\n\t// (dirty=true); a subsequent rotation reads proposed-when-dirty\n\t// and accumulates the prior power change rather than clobbering\n\t// back to current.\n\tresetValset(t)\n\tresetCache()\n\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{\n\t\tpubKeyA + \":1\",\n\t\tpubKeyB + \":5\",\n\t})\n\n\topA := testutils.TestAddress(\"op-A\")\n\topB := testutils.TestAddress(\"op-B\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{\n\t\t{op: opA, pubKey: pubKeyA, keepRunning: true},\n\t\t{op: opB, pubKey: pubKeyB, keepRunning: true},\n\t})\n\n\turequire.NoError(t, newValoperChangeExecutor(cur,\n\t\t[]ValoperChange{{OperatorAddress: opA, Power: 3}},\n\t).Execute(cross(cur)))\n\n\ttesting.SetRealm(testing.NewCodeRealm(valopersRealmPath))\n\tRotateValoperSigningKey(cross(cur), opA, pubKeyA, pubKeyC)\n\tNotifyValoperChanged(cross(cur), opA, pubKeyC, mustAddr(t, pubKeyC), true)\n\n\teffective := sysparams.GetValsetEffective()\n\tpowerOf := map[string]uint64{}\n\tfor _, v := range effective {\n\t\tpowerOf[v.PubKey] = v.VotingPower\n\t}\n\tuassert.Equal(t, uint64(3), powerOf[pubKeyC], \"rotation accumulates with prior upsert; final power=3\")\n\tuassert.Equal(t, uint64(5), powerOf[pubKeyB], \"opB unchanged\")\n\t_, gotOld := powerOf[pubKeyA]\n\tuassert.False(t, gotOld, \"OLD signing key (pubKeyA) must not appear after rotation\")\n\turequire.Equal(t, 2, len(effective), \"exactly two entries — opA(NEW) and opB\")\n}\n\n// TestNewValidatorProposalRequest_ExecutorVanishedCacheEntryPanics pins\n// the \"operator vanished from valoperCache between propose and execute\"\n// branch. No production path deletes from valoperCache today (only Set\n// is called by NotifyValoperChanged), but bptree.BPTree exposes Remove\n// so the underlying data structure does support deletion. This test\n// simulates that hypothetical state directly via the package-private\n// cache var to confirm the executor panics with the documented message\n// rather than silently mis-publishing an empty/wrong valset.\n//\n// If a future commit ever introduces a public cache-delete path, this\n// test still passes — it's a contract-pinning test for the panic itself.\n// If the team decides the branch is unreachable enough to drop, this\n// test is the first thing to break.\nfunc TestNewValidatorProposalRequest_ExecutorVanishedCacheEntryPanics(cur realm, t *testing.T) {\n\tresetValset(t)\n\tresetCache()\n\n\top := testutils.TestAddress(\"op-A\")\n\tseedCache(t, []struct {\n\t\top          address\n\t\tpubKey      string\n\t\tkeepRunning bool\n\t}{{op: op, pubKey: pubKeyA, keepRunning: true}})\n\n\texec := newValoperChangeExecutor(cur,\n\t\t[]ValoperChange{{OperatorAddress: op, Power: 5}},\n\t)\n\n\t// Simulate the unreachable-today state: cache entry deleted between\n\t// proposal-create and proposal-execute. Direct package-private mutation\n\t// (NOT a public API) — production code has no path here today.\n\t_, removed := valoperCache.Remove(op.String())\n\turequire.True(t, removed, \"fixture: cache entry must have been present before Remove\")\n\n\tuassert.AbortsContains(t, cur, \"operator vanished from valoperCache between propose and execute\", func() {\n\t\t_ = exec.Execute(cross(cur))\n\t})\n}\n\n// contains is a tiny strings.Contains shim so tests don't import a\n// new package.\nfunc contains(s, substr string) bool {\n\tfor i := 0; i+len(substr) \u003c= len(s); i++ {\n\t\tif s[i:i+len(substr)] == substr {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"},{"name":"validators.gno","body":"// Package validators implements on-chain validator set management\n// through Proof of Authority. The realm exposes a public proposal\n// constructor for GovDAO; on approval, the proposal callback applies\n// the captured deltas to the chain's effective valset and publishes\n// the new full set via gno.land/r/sys/params. The chain's EndBlocker\n// reads the result on the next block and propagates to consensus.\n//\n// No in-realm validator state. All reads go through\n// sysparams.GetValsetEffective (proposed-if-dirty, else current).\npackage validators\n\nimport (\n\t\"chain/runtime\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sys/validators\"\n\tsysparams \"gno.land/r/sys/params\"\n)\n\n// Operator-keyed proposal builder lives in proposal.gno\n// (NewValidatorProposalRequest + newValoperChangeExecutor). The legacy\n// signing-keyed NewProposalRequest was removed: every valid\n// signing-keyed input is also a valid operator-keyed input under\n// always-on valoper enforcement.\n\n// IsValidator returns true if addr is part of the effective validator\n// set (proposed if a v3 proposal is awaiting EndBlocker, else\n// current).\nfunc IsValidator(addr address) bool {\n\tfor _, v := range sysparams.GetValsetEffective() {\n\t\tif v.Address == addr {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// GetValidator returns the validator with the given address from the\n// effective set; panics if absent.\nfunc GetValidator(addr address) validators.Validator {\n\tfor _, v := range sysparams.GetValsetEffective() {\n\t\tif v.Address == addr {\n\t\t\treturn v\n\t\t}\n\t}\n\tpanic(\"validator not found\")\n}\n\n// GetValidators returns the effective validator set.\nfunc GetValidators() []validators.Validator {\n\treturn sysparams.GetValsetEffective()\n}\n\n// Render displays the effective validator set.\nfunc Render(string) string {\n\tvar sb strings.Builder\n\th := runtime.ChainHeight()\n\tset := sysparams.GetValsetEffective()\n\tsb.WriteString(ufmt.Sprintf(\"## Valset at height %d\\n\\n\", h))\n\tif len(set) == 0 {\n\t\tsb.WriteString(\"Valset is empty.\\n\")\n\t\treturn sb.String()\n\t}\n\tfor i, v := range set {\n\t\tsb.WriteString(ufmt.Sprintf(\"- #%d: %s (%d)\\n\", i, v.Address.String(), v.VotingPower))\n\t}\n\treturn sb.String()\n}\n"},{"name":"validators_test.gno","body":"// Tests for stateless v3.\n//\n// State isolation: gno's test runner constructs a fresh testParams\n// per top-level TestXxx, but t.Run subtests share the same map.\n// Each subtest below seeds (or clears) the valset slots it cares\n// about up front; do the same when adding cases.\npackage validators\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nconst (\n\t// Three distinct deterministic test pubkeys (and the addresses\n\t// they derive to) used across the cases below. Stable across\n\t// runs.\n\tpubKeyA = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zq3ds6sdvc0shfkq02h6xx5g0jp04aadexfnpsmgjxu72xz9y30aqfrlpny\"\n\tpubKeyB = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\"\n\tpubKeyC = \"gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pqddddqg2glc8x4fl7vxjlnr7p5a3czm5kcdp4239sg6yqdc4rc2r5cjrffs\"\n\n\tmodule    = \"node\"\n\tsubmodule = \"valset\"\n\tcurrKey   = \"current\"\n\tpropKey   = \"proposed\"\n\tdirtyKey  = \"dirty\"\n)\n\nfunc resetValset(t *testing.T) {\n\tt.Helper()\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{})\n\ttesting.SetSysParamStrings(module, submodule, propKey, []string{})\n\ttesting.SetSysParamBool(module, submodule, dirtyKey, false)\n}\n\nfunc mustAddr(t *testing.T, pk string) address {\n\tt.Helper()\n\ta, err := chain.PubKeyAddress(pk)\n\turequire.NoError(t, err, \"valid pubkey\")\n\treturn a\n}\n\nfunc TestEffectiveView_DirtyFalseReadsCurrent(cur realm, t *testing.T) {\n\tresetValset(t)\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":10\"})\n\n\taddrA := mustAddr(t, pubKeyA)\n\turequire.True(t, IsValidator(addrA), \"A in current\")\n\turequire.Equal(t, 1, len(GetValidators()))\n}\n\nfunc TestEffectiveView_DirtyTrueReadsProposed(cur realm, t *testing.T) {\n\tresetValset(t)\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":10\"})\n\ttesting.SetSysParamStrings(module, submodule, propKey, []string{pubKeyA + \":10\", pubKeyB + \":1\"})\n\ttesting.SetSysParamBool(module, submodule, dirtyKey, true)\n\n\taddrA := mustAddr(t, pubKeyA)\n\taddrB := mustAddr(t, pubKeyB)\n\turequire.True(t, IsValidator(addrA), \"A still visible\")\n\turequire.True(t, IsValidator(addrB), \"B visible via proposed\")\n\turequire.Equal(t, 2, len(GetValidators()))\n}\n\nfunc TestGetValidator_ReturnsParsedEntry(cur realm, t *testing.T) {\n\tresetValset(t)\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":42\"})\n\n\tv := GetValidator(mustAddr(t, pubKeyA))\n\turequire.Equal(t, uint64(42), v.VotingPower)\n\turequire.Equal(t, pubKeyA, v.PubKey)\n}\n\nfunc TestRender_DefaultTestHeight(cur realm, t *testing.T) {\n\tresetValset(t)\n\ttesting.SetSysParamStrings(module, submodule, currKey, []string{pubKeyA + \":10\"})\n\n\tout := Render(\"\")\n\t// gnovm/pkg/test/test.go sets DefaultHeight = 123.\n\turequire.Equal(t,\n\t\t\"## Valset at height 123\\n\\n- #0: \"+mustAddr(t, pubKeyA).String()+\" (10)\\n\",\n\t\tout)\n}\n\n// The same-block accumulation, panic-on-remove-of-missing,\n// panic-on-add-of-existing, and pubkey-validation tests for the\n// signing-keyed NewProposalRequest/newValsetChangeExecutor were\n// removed alongside the function itself. Equivalent coverage for\n// the operator-keyed flow lives in proposal_test.gno\n// (TestNewValidatorProposalRequest_*) and cache_test.gno\n// (TestRotateValoperSigningKey_AccumulatesAcrossSameBlock).\n//\n// Empty-final-set rejection in the executor still applies; it's\n// covered by TestNewValidatorProposalRequest_RemoveOperator in\n// proposal_test.gno.\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"zB2le/f/9a/CCfHVINwJ+0uCoA0W8n5ViVuE4NgskntWkBskMwWDGFSQzkAb24uNCzpAt2aqbzKg7ibNwVlWDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"valopers","path":"gno.land/r/gnops/valopers","files":[{"name":"admin.gno","body":"package valopers\n\nimport (\n\t\"gno.land/p/moul/authz\"\n)\n\nvar auth *authz.Authorizer\n\nfunc Auth() *authz.Authorizer {\n\treturn auth\n}\n\nfunc updateInstructions(_ int, rlm realm, newInstructions string) {\n\terr := auth.DoByCurrent(0, rlm, \"update-instructions\", func() error {\n\t\tinstructions = newInstructions\n\t\treturn nil\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc NewInstructionsProposalCallback(newInstructions string) func(realm) error {\n\tcb := func(cur realm) error {\n\t\tupdateInstructions(0, cur, newInstructions)\n\t\treturn nil\n\t}\n\n\treturn cb\n}\n\n// The min-fee callback was removed: the fee now lives in sysparams\n// under node:valoper:register_fee, and\n// proposal.ProposeNewMinFeeProposalRequest delegates to\n// sys/params.NewSysParamUint64PropRequest. Removing avoids the\n// forward-compat hazard of a no-op shim with no caller-auth gating.\n"},{"name":"admin_test.gno","body":"package valopers\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/authz\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\n// Earlier this test also asserted a panic (\"action can only be\n// executed by the contract\") when called without SetOriginCaller.\n// That panic was emitted by a `unsafe.CurrentRealm() == contractAddr`\n// check inside authz.ContractAuthority.Authorize, which has been\n// removed — it was .Title()-bypassable (runtime.CurrentRealm inside a\n// non-crossing closure walks past non-crossing frames to the\n// most-recent crossing ancestor, so a malicious caller could shape\n// the call chain to make the check resolve to contractAddr).\n//\n// Caller authentication is now upstream of the deleted gate:\n// authz.Authorizer.DoByCurrent requires `rlm.IsCurrent()`, and the\n// contract handler closure (registered at init in this realm) is the\n// remaining Class-4 trust root by lexical capture. There is no\n// negative path here to assert, so the prior expectation was retired\n// along with its source.\nfunc TestUpdateInstructions(cur realm, t *testing.T) {\n\tauth = authz.NewWithAuthority(\n\t\tauthz.NewContractAuthority(\n\t\t\t\"gno.land/r/gov/dao\",\n\t\t\tfunc(title string, action authz.PrivilegedAction) error {\n\t\t\t\treturn action()\n\t\t\t},\n\t\t),\n\t)\n\n\tnewInstructions := \"new instructions\"\n\n\tuassert.NotPanics(t, cur, func() {\n\t\tupdateInstructions(0, cur, newInstructions)\n\t})\n\n\tuassert.Equal(t, newInstructions, instructions)\n}\n\n// TestUpdateMinFee was removed: register_fee now lives in sysparams\n// (node:valoper:register_fee), governed via the generic\n// NewSysParamUint64PropRequest factory. See proposal.gno for the\n// replacement flow.\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnops/valopers\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"init.gno","body":"package valopers\n\nimport (\n\t\"gno.land/p/moul/authz\"\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nfunc init() {\n\tvalopers = avl.NewTree()\n\n\tauth = authz.NewWithAuthority(\n\t\tauthz.NewContractAuthority(\n\t\t\t\"gno.land/r/gnops/valopers\",\n\t\t\tfunc(_ string, action authz.PrivilegedAction) error {\n\t\t\t\treturn action()\n\t\t\t},\n\t\t),\n\t)\n\n\tinstructions = `\n# Welcome to the **Valopers** realm\n\n## 📌 Purpose of this Contract\n\nThe **Valopers** contract is designed to maintain a registry of **validator profiles**. This registry provides essential information to **GovDAO members**, enabling them to make informed decisions when voting on the inclusion of new validators into the **valset**.\n\nBy registering your validator profile, you contribute to a transparent and well-informed governance process within **gno.land**.\n\n---\n\n## 📝 How to Register Your Validator Node\n\nTo add your validator node to the registry, use the [**Register**](` + txlink.Call(\"Register\") + `) function with the following parameters:\n\n- **Moniker** (Validator Name)\n  - Must be **human-readable**\n  - **Max length**: **32 characters**\n  - **Allowed characters**: Letters, numbers, spaces, hyphens (**-**), and underscores (**_**)\n  - **No special characters** at the beginning or end\n\n- **Description** (Introduction \u0026 Validator Details)\n  - **Max length**: **2048 characters**\n  - Must include answers to the questions listed below\n\n- **Server Type** (Infrastructure Type)\n  - Must be one of the following values:\n    - **cloud**: For validators running on cloud infrastructure (AWS, GCP, Azure, etc.)\n    - **on-prem**: For validators running on on-premises infrastructure\n    - **data-center**: For validators running in dedicated data centers\n\n- **Operator Address**\n  - The ` + \"`g1...`\" + ` address of your operator account (from your ` + \"`gnokey`\" + ` keyring)\n  - **Must be controlled by the signer** of this transaction — the realm rejects the call if the signer doesn't control that address\n\n- **Validator Consensus Public Key**\n  - Your validator node's consensus public key, in the ` + \"`gpub1...`\" + ` format\n  - Retrieve it by running: ` + \"`gnoland secrets get validator_key`\" + `\n\n### ✍️ Required Information for the Description\n\nPlease provide detailed answers to the following questions to ensure transparency and improve your chances of being accepted:\n\n1. The name of your validator\n2. Networks you are currently validating and your total AuM (assets under management)\n3. Links to your **digital presence** (website, social media, etc.). Please include your Discord handle to be added to our main comms channel, the gno.land valoper Discord channel.\n4. Contact details\n5. Why are you interested in validating on **gno.land**?\n6. What contributions have you made or are willing to make to **gno.land**?\n\n---\n\n## 🔄 Updating Your Validator Information\n\nAfter registration, you can update your validator details using the **update functions** provided by the contract.\n\n---\n\n## 📢 Submitting a Proposal to Join the Validator Set\n\nOnce you're satisfied with your **valoper** profile, you need to notify GovDAO; only a GovDAO member can submit a proposal to add you to the validator set.\n\nIf you are a GovDAO member, you can nominate yourself by executing the following function: [**r/gnops/valopers/proposal.ProposeNewValidator**](` + txlink.Realm(\"gno.land/r/gnops/valopers/proposal\").Call(\"ProposeNewValidator\") + `)\n\nThis will initiate a governance process where **GovDAO** members will vote on your proposal.\n\n---\n\n🚀 **Register now and become a part of gno.land’s validator ecosystem!**\n\nRead more: [How to become a testnet validator](https://gnops.io/articles/guides/become-testnet-validator/) \u003c!-- XXX: replace with a r/gnops/blog:xxx link --\u003e\n\nDisclaimer: Please note, registering your validator profile and/or validating on testnets does not guarantee a validator slot on the gno.land beta mainnet. However, active participation and contributions to testnets will help establish credibility and may improve your chances for future validator acceptance. The initial validator amount and valset will ultimately be selected through GovDAO governance proposals and acceptance.\n\n---\n\n`\n}\n"},{"name":"rotate_test.gno","body":"package valopers\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nconst (\n\t// Second valid pubkey used as the rotation target. Distinct from\n\t// validValidatorInfo's pubkey so the signingRegistry uniqueness\n\t// check is exercised.\n\trotateTargetPubKey = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zq3ds6sdvc0shfkq02h6xx5g0jp04aadexfnpsmgjxu72xz9y30aqfrlpny\"\n\n\t// Test-local mirror of the rotation_period_blocks sys-param's\n\t// default. resetState() seeds the same value into sysparams.\n\ttestRotationPeriodBlocks = int64(600)\n)\n\n// registerForRotation does the boilerplate setup that every rotation\n// test starts from: clear state, register the valoper as info.Address,\n// and return the info struct. rlm is threaded into Register via\n// cross(rlm) — it's a non-first parameter so the helper stays a\n// regular (non-crossing) function.\nfunc registerForRotation(t *testing.T, rlm realm) struct {\n\tMoniker     string\n\tDescription string\n\tServerType  string\n\tAddress     address\n\tPubKey      string\n} {\n\tt.Helper()\n\tresetState()\n\tinfo := validValidatorInfo(t)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\ttesting.SetOriginSend(chain.Coins{minFee})\n\tRegister(cross(rlm), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\treturn info\n}\n\nfunc TestUpdateSigningKey_HappyPath(cur realm, t *testing.T) {\n\tinfo := registerForRotation(t, cur)\n\n\t// Advance past the throttle window. SkipHeights doesn't preserve\n\t// the realm context across the boundary, so re-set it before the\n\t// next cross-call.\n\ttesting.SkipHeights(testRotationPeriodBlocks + 1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\tuassert.NotAborts(t, cur, func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\t})\n\n\tv := GetByAddr(info.Address)\n\tuassert.Equal(t, rotateTargetPubKey, v.SigningPubKey)\n\n\tnewSigningAddr, err := chain.PubKeyAddress(rotateTargetPubKey)\n\turequire.NoError(t, err)\n\tuassert.Equal(t, newSigningAddr, v.SigningAddress)\n\n\t// New entry active in registry.\n\trawNew := signingRegistry.Get(newSigningAddr.String())\n\turequire.NotNil(t, rawNew, \"new signing addr in registry\")\n\tnewEntry := rawNew.(regEntry)\n\tuassert.Equal(t, info.Address, newEntry.OperatorAddress)\n\tuassert.Equal(t, int64(0), newEntry.RetiredAtHeight)\n\n\t// Old entry retired (still present, RetiredAtHeight \u003e 0).\n\toldSigningAddr, err := chain.PubKeyAddress(info.PubKey)\n\turequire.NoError(t, err)\n\trawOld := signingRegistry.Get(oldSigningAddr.String())\n\turequire.NotNil(t, rawOld, \"old signing addr retained as retired\")\n\toldEntry := rawOld.(regEntry)\n\tuassert.Equal(t, info.Address, oldEntry.OperatorAddress)\n\tuassert.True(t, oldEntry.RetiredAtHeight \u003e 0, \"old entry must be retired (RetiredAtHeight \u003e 0)\")\n}\n\nfunc TestUpdateSigningKey_ThrottleRejection(cur realm, t *testing.T) {\n\tinfo := registerForRotation(t, cur)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\t// Try to rotate immediately, before throttle elapses.\n\tuassert.AbortsWithMessage(t, cur, ErrRotationThrottled.Error(), func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\t})\n\n\t// Advance just shy of the threshold; still rejected.\n\ttesting.SkipHeights(testRotationPeriodBlocks - 1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\tuassert.AbortsWithMessage(t, cur, ErrRotationThrottled.Error(), func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\t})\n\n\t// One more block — now exactly at the threshold; allowed.\n\ttesting.SkipHeights(1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\tuassert.NotAborts(t, cur, func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\t})\n}\n\nfunc TestUpdateSigningKey_RejectsNonAuthListCaller(cur realm, t *testing.T) {\n\tinfo := registerForRotation(t, cur)\n\ttesting.SkipHeights(testRotationPeriodBlocks + 1)\n\n\t// Switch to a caller not on the auth list.\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"attacker\")))\n\n\t// Pin the exact authorizable error so this catches an auth\n\t// regression rather than any abort. Empty-substring match (the\n\t// previous form) accepted any panic, including unrelated ones.\n\tuassert.AbortsContains(t, cur, \"not in authorized list\", func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\t})\n}\n\nfunc TestUpdateSigningKey_RejectsReuseOfActiveKey(cur realm, t *testing.T) {\n\tinfo := registerForRotation(t, cur)\n\ttesting.SkipHeights(testRotationPeriodBlocks + 1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\t// Try to \"rotate\" to the same key — already in registry.\n\tuassert.AbortsWithMessage(t, cur, ErrSigningKeyTaken.Error(), func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, info.PubKey)\n\t})\n}\n\nfunc TestUpdateSigningKey_RejectsRotationOntoActiveValidator(cur realm, t *testing.T) {\n\t// Front-running guard: a rotation whose derived signing address is\n\t// already an active validator must be rejected. Mirrors the same\n\t// guard in Register (valopers.gno: ErrFrontrunValidator). Without\n\t// this, any valoper could rotate onto an unregistered (e.g.\n\t// genesis-seeded) validator slot — v3's executor would overwrite\n\t// the active validator's entry in the effective valset with this\n\t// operator's claim, then a subsequent govDAO remove-op proposal\n\t// would delete it. signingRegistry uniqueness alone doesn't catch\n\t// this case: a genesis-seeded validator never went through\n\t// Register or UpdateSigningKey, so its signing address is absent\n\t// from signingRegistry.\n\tinfo := registerForRotation(t, cur)\n\n\t// Seed v3's valset:current with the address that rotateTargetPubKey\n\t// derives to. ValsetEffective is read from valset:current when the\n\t// dirty flag is unset, which is the default.\n\ttesting.SetSysParamStrings(\"node\", \"valset\", \"current\",\n\t\t[]string{rotateTargetPubKey + \":1\"})\n\n\ttesting.SkipHeights(testRotationPeriodBlocks + 1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\tuassert.AbortsWithMessage(t, cur, ErrFrontrunValidator.Error(), func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\t})\n\n\t// Cleanup: clear the seeded valset to avoid leaking into sibling\n\t// subtests if the runner doesn't reset package state between them.\n\ttesting.SetSysParamStrings(\"node\", \"valset\", \"current\", []string{})\n}\n\nfunc TestUpdateSigningKey_RejectsReuseOfRetiredKey(cur realm, t *testing.T) {\n\tinfo := registerForRotation(t, cur)\n\n\t// First rotation: info.PubKey -\u003e rotateTargetPubKey. info.PubKey\n\t// becomes retired.\n\ttesting.SkipHeights(testRotationPeriodBlocks + 1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\n\t// Second rotation back to info.PubKey must fail — it's retired\n\t// but signingRegistry retains it forever.\n\ttesting.SkipHeights(testRotationPeriodBlocks + 1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\tuassert.AbortsWithMessage(t, cur, ErrSigningKeyTaken.Error(), func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, info.PubKey)\n\t})\n}\n\nfunc TestUpdateSigningKey_LastRotationHeightAdvances(cur realm, t *testing.T) {\n\tinfo := registerForRotation(t, cur)\n\ttesting.SkipHeights(testRotationPeriodBlocks + 1)\n\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\tUpdateSigningKey(cross(cur), info.Address, rotateTargetPubKey)\n\n\tv := GetByAddr(info.Address)\n\tuassert.True(t, v.LastRotationHeight \u003e 0, \"LastRotationHeight set on rotation\")\n\n\t// Immediately after, throttle rejects again.\n\tuassert.AbortsWithMessage(t, cur, ErrRotationThrottled.Error(), func() {\n\t\tUpdateSigningKey(cross(cur), info.Address, \"gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pqddddqg2glc8x4fl7vxjlnr7p5a3czm5kcdp4239sg6yqdc4rc2r5cjrffs\")\n\t})\n}\n"},{"name":"valopers.gno","body":"// Package valopers is designed around the permissionless lifecycle of valoper profiles.\npackage valopers\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"crypto/bech32\"\n\t\"errors\"\n\t\"regexp\"\n\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/combinederr/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/ownable/v0/exts/authorizable\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\tsysparams \"gno.land/r/sys/params\"\n\tvalidators \"gno.land/r/sys/validators/v3\"\n)\n\nconst (\n\tMonikerMaxLength     = 32\n\tDescriptionMaxLength = 2048\n\n\t// Valid server types\n\tServerTypeCloud      = \"cloud\"\n\tServerTypeOnPrem     = \"on-prem\"\n\tServerTypeDataCenter = \"data-center\"\n)\n\nvar (\n\tErrValoperExists        = errors.New(\"valoper already exists\")\n\tErrValoperMissing       = errors.New(\"valoper does not exist\")\n\tErrInvalidAddress       = errors.New(\"invalid address\")\n\tErrInvalidMoniker       = errors.New(\"moniker is not valid\")\n\tErrInvalidDescription   = errors.New(\"description is not valid\")\n\tErrInvalidServerType    = errors.New(\"server type is not valid\")\n\tErrOperatorSquatGuard   = errors.New(\"post-genesis: caller must equal operator address\")\n\tErrSigningKeyTaken      = errors.New(\"signing address already in registry (active or retired)\")\n\tErrFrontrunValidator    = errors.New(\"post-genesis: signing address is already an active validator\")\n\tErrRotationThrottled    = errors.New(\"rotation throttled: try again later\")\n\tErrRegistryEntryMissing = errors.New(\"signing address has no active registry entry (corrupted state)\")\n)\n\nvar (\n\tvalopers     *avl.Tree // operator-address -\u003e Valoper\n\tinstructions string    // markdown instructions for valoper's registration\n\n\t// signingRegistry maps SigningAddress.String() -\u003e regEntry.\n\t// Permanently retains retired entries to prevent key reuse and\n\t// to support future slashing-attribution by signing address.\n\tsigningRegistry = bptree.NewBPTree32()\n\n\tmonikerMaxLengthMiddle = ufmt.Sprintf(\"%d\", MonikerMaxLength-2)\n\tvalidateMonikerRe      = regexp.MustCompile(`^[a-zA-Z0-9][\\w -]{0,` + monikerMaxLengthMiddle + `}[a-zA-Z0-9]$`) // 32 characters, including spaces, hyphens or underscores in the middle\n)\n\n// regEntry tracks signing-address -\u003e operator with retirement metadata.\n// retiredAtHeight == 0 means the entry is currently active for the operator.\ntype regEntry struct {\n\tOperatorAddress    address\n\tRegisteredAtHeight int64\n\tRetiredAtHeight    int64\n}\n\n// Valoper represents a validator operator profile.\ntype Valoper struct {\n\tMoniker     string // A human-readable name\n\tDescription string // A description and details about the valoper\n\tServerType  string // The type of server (cloud/on-prem/data-center)\n\n\tOperatorAddress address // operator identity, profile key, stable across rotations\n\tSigningPubKey   string  // current consensus signing pubkey (bech32 gpub1...)\n\tSigningAddress  address // = chain.PubKeyAddress(SigningPubKey)\n\n\tLastRotationHeight int64 // throttle anchor for UpdateSigningKey\n\n\tKeepRunning bool // operator wants this validator running in the active set\n\n\tauth *authorizable.Authorizable\n}\n\nfunc (v Valoper) Auth() *authorizable.Authorizable {\n\treturn v.auth\n}\n\nfunc AddToAuthList(cur realm, addr address, member address) {\n\tv := GetByAddr(addr)\n\tif err := v.Auth().AddToAuthList(0, cur, member); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc DeleteFromAuthList(cur realm, addr address, member address) {\n\tv := GetByAddr(addr)\n\tif err := v.Auth().DeleteFromAuthList(0, cur, member); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Register registers a new valoper. The `addr` parameter is the\n// operator address (stable identity, profile key); `pubKey` is the\n// consensus signing pubkey, from which the signing address is derived.\n//\n// Auth shape:\n//   - Post-genesis: OriginCaller must equal addr (operator-slot squat\n//     guard). Genesis-mode replay (ChainHeight()==0) bypasses, so\n//     migration .jsonl txs and historical Register replays succeed.\n//   - Signing-address uniqueness: derived(pubKey) must not already be\n//     in signingRegistry, active or retired.\n//   - Front-running guard: post-genesis, derived(pubKey) must not\n//     already be an active validator (a fresh registration cannot\n//     squat on the consensus address of an existing validator).\n//\n// Why OriginCaller==addr is sufficient (no IsUserCall): squatting\n// requires the attacker to be able to satisfy OriginCaller==victim,\n// which requires the victim's signing key. r/sys/namereg/v1.Register\n// also gates on IsUserCall, but that's because IT reads\n// unsafe.OriginSend() for the anti-squatting payment and IsUserCall\n// is needed to ensure the OriginSend envelope reflects what landed at\n// this realm rather than a phantom payment from a previous frame.\n// valopers.Register has no per-call payment-receipt check (fees are\n// validated against banker.OriginSend in a way that's symmetric to\n// IsUserCall via direct comparison), so the IsUserCall tightening\n// would only block legitimate `maketx run` flows (operator-authored\n// scripts that legitimately set OriginCaller==operator) without\n// adding identity-squat protection.\n//\n// Auth-list seeding: the profile's Authorizable owner is set to addr\n// (NOT OriginCaller). At H\u003e0 the squat guard makes them equal anyway;\n// at H==0 the deployer pattern (one signer registers many operators)\n// requires owner == addr so each operator can manage their own profile\n// post-genesis without needing the deployer's auth.\nfunc Register(cur realm, moniker string, description string, serverType string, addr address, pubKey string) {\n\t// Operator-slot squat guard.\n\tif runtime.ChainHeight() \u003e 0 \u0026\u0026 unsafe.OriginCaller() != addr {\n\t\tpanic(ErrOperatorSquatGuard)\n\t}\n\n\t// Fee enforcement (read from sysparams; defaults to 0 until\n\t// governance raises it post-transfer-enablement).\n\tif fee := sysparams.GetValoperRegisterFee(); fee \u003e 0 {\n\t\tminFee := chain.NewCoin(\"ugnot\", int64(fee))\n\t\tsentCoins := unsafe.OriginSend()\n\t\tif len(sentCoins) != 1 || sentCoins[0].IsLT(minFee) {\n\t\t\tpanic(ufmt.Sprintf(\"payment must not be less than %d%s\", minFee.Amount, minFee.Denom))\n\t\t}\n\t}\n\n\t// Check if the valoper is already registered.\n\tif isValoper(addr) {\n\t\tpanic(ErrValoperExists)\n\t}\n\n\t// Derive the consensus signing address from the pubkey.\n\tsigningAddr, err := chain.PubKeyAddress(pubKey)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Signing-address uniqueness across all profiles, ever.\n\tif signingRegistry.Has(signingAddr.String()) {\n\t\tpanic(ErrSigningKeyTaken)\n\t}\n\n\t// Front-running guard: post-genesis, the signing address must\n\t// not already be an active validator.\n\tif runtime.ChainHeight() \u003e 0 \u0026\u0026 validators.IsValidator(signingAddr) {\n\t\tpanic(ErrFrontrunValidator)\n\t}\n\n\tv := Valoper{\n\t\tMoniker:            moniker,\n\t\tDescription:        description,\n\t\tServerType:         serverType,\n\t\tOperatorAddress:    addr,\n\t\tSigningPubKey:      pubKey,\n\t\tSigningAddress:     signingAddr,\n\t\tLastRotationHeight: runtime.ChainHeight(),\n\t\tKeepRunning:        true,\n\t\tauth:               authorizable.New(ownable.NewWithAddress(addr)),\n\t}\n\n\tif err := v.Validate(); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Save the valoper to the set.\n\tvalopers.Set(v.OperatorAddress.String(), v)\n\n\t// Insert into the signing-address registry.\n\tsigningRegistry.Set(signingAddr.String(), regEntry{\n\t\tOperatorAddress:    addr,\n\t\tRegisteredAtHeight: runtime.ChainHeight(),\n\t\tRetiredAtHeight:    0,\n\t})\n\n\t// Refresh v3's cache for this operator.\n\tvalidators.NotifyValoperChanged(cross(cur), addr, v.SigningPubKey, v.SigningAddress, v.KeepRunning)\n}\n\n// UpdateMoniker updates an existing valoper's moniker.\nfunc UpdateMoniker(cur realm, addr address, moniker string) {\n\t// Check that the moniker is not empty.\n\tif err := validateMoniker(moniker); err != nil {\n\t\tpanic(err)\n\t}\n\n\tv := GetByAddr(addr)\n\n\t// Check that the caller has permissions.\n\tv.Auth().AssertPreviousOnAuthList(0, cur)\n\n\t// Update the moniker.\n\tv.Moniker = moniker\n\n\t// Save the valoper info.\n\tvalopers.Set(addr.String(), v)\n}\n\n// UpdateDescription updates an existing valoper's description.\nfunc UpdateDescription(cur realm, addr address, description string) {\n\t// Check that the description is not empty.\n\tif err := validateDescription(description); err != nil {\n\t\tpanic(err)\n\t}\n\n\tv := GetByAddr(addr)\n\n\t// Check that the caller has permissions.\n\tv.Auth().AssertPreviousOnAuthList(0, cur)\n\n\t// Update the description.\n\tv.Description = description\n\n\t// Save the valoper info.\n\tvalopers.Set(addr.String(), v)\n}\n\n// UpdateKeepRunning updates an existing valoper's active status.\n// Calls v3.NotifyValoperChanged because the cache stores KeepRunning.\nfunc UpdateKeepRunning(cur realm, addr address, keepRunning bool) {\n\tv := GetByAddr(addr)\n\n\t// Check that the caller has permissions.\n\tv.Auth().AssertPreviousOnAuthList(0, cur)\n\n\t// Update status.\n\tv.KeepRunning = keepRunning\n\n\t// Save the valoper info.\n\tvalopers.Set(addr.String(), v)\n\n\t// Refresh v3's cache (KeepRunning is one of the cached fields).\n\tvalidators.NotifyValoperChanged(cross(cur), addr, v.SigningPubKey, v.SigningAddress, v.KeepRunning)\n}\n\n// UpdateServerType updates an existing valoper's server type.\nfunc UpdateServerType(cur realm, addr address, serverType string) {\n\t// Check that the server type is valid.\n\tif err := validateServerType(serverType); err != nil {\n\t\tpanic(err)\n\t}\n\n\tv := GetByAddr(addr)\n\n\t// Check that the caller has permissions.\n\tv.Auth().AssertPreviousOnAuthList(0, cur)\n\n\t// Update server type.\n\tv.ServerType = serverType\n\n\t// Save the valoper info.\n\tvalopers.Set(addr.String(), v)\n}\n\n// UpdateSigningKey rotates an operator's consensus signing key.\n//\n// Auth: caller must be on the operator's auth list (defaults to\n// operator at Register time; extendable via AddToAuthList).\n//\n// Invariants checked at entry:\n//   - throttle: ChainHeight() - v.LastRotationHeight \u003e=\n//     rotationPeriodBlocks\n//   - signingRegistry uniqueness: derived(newPubKey) not in registry\n//     (active OR retired); permanently blocks key reuse\n//   - fee: unsafe.OriginSend() \u003e= rotationFee (mirrors Register's\n//     fee-check pattern)\n//\n// Effect: profile's SigningPubKey/SigningAddress/LastRotationHeight\n// updated; old registry entry marked retired (retiredAtHeight =\n// ChainHeight()); new entry inserted into signingRegistry; v3 emits\n// remove+add to sysparams via RotateValoperSigningKey; v3 cache\n// refreshed via NotifyValoperChanged. Rotation lands in consensus\n// at H+2.\n//\n// Atomicity: Gno tx atomicity rolls back all state if any step\n// panics. If v3.RotateValoperSigningKey panics, the registry insert\n// and profile mutation revert with it.\nfunc UpdateSigningKey(cur realm, addr address, newPubKey string) {\n\tv := GetByAddr(addr)\n\n\t// Auth: caller must be on operator's auth list.\n\tv.Auth().AssertPreviousOnAuthList(0, cur)\n\n\t// Throttle: limit one rotation per rotation_period_blocks per\n\t// operator (per profile, not per caller — multi-member auth lists\n\t// can't multiplicative-rotate).\n\theight := runtime.ChainHeight()\n\tif height-v.LastRotationHeight \u003c sysparams.GetValoperRotationPeriodBlocks() {\n\t\tpanic(ErrRotationThrottled)\n\t}\n\n\t// Fee: enforce only if non-zero (matches Register's pattern;\n\t// rotation_fee defaults to zero pre-transfer-enablement).\n\tif fee := sysparams.GetValoperRotationFee(); fee \u003e 0 {\n\t\tminFee := chain.NewCoin(\"ugnot\", int64(fee))\n\t\tsentCoins := unsafe.OriginSend()\n\t\tif len(sentCoins) != 1 || sentCoins[0].IsLT(minFee) {\n\t\t\tpanic(ufmt.Sprintf(\"payment must not be less than %d%s\", minFee.Amount, minFee.Denom))\n\t\t}\n\t}\n\n\t// Derive the new signing address from the new pubkey.\n\tnewSigningAddr, err := chain.PubKeyAddress(newPubKey)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// signingRegistry uniqueness: new key must not have ever been\n\t// registered (active or retired).\n\tif signingRegistry.Has(newSigningAddr.String()) {\n\t\tpanic(ErrSigningKeyTaken)\n\t}\n\n\t// Front-running guard: the derived signing address must not already\n\t// be an active validator. Mirrors the same guard in Register\n\t// (ErrFrontrunValidator). signingRegistry uniqueness above only\n\t// blocks signing addresses that previously went through Register or\n\t// UpdateSigningKey — genesis-seeded validators bypassed both, so\n\t// their signing addresses are absent from signingRegistry. Without\n\t// this check, a valoper could rotate onto such a slot and hijack\n\t// it: v3.RotateValoperSigningKey would overwrite the active entry\n\t// with this operator's claim, and a subsequent govDAO remove-op\n\t// proposal would then delete it.\n\tif validators.IsValidator(newSigningAddr) {\n\t\tpanic(ErrFrontrunValidator)\n\t}\n\n\t// Remember the previous signing key for the v3 cross-call.\n\toldPubKey := v.SigningPubKey\n\toldSigningAddr := v.SigningAddress\n\n\t// Mark the old registry entry retired. The entry must exist —\n\t// it was inserted at Register time.\n\trawOld := signingRegistry.Get(oldSigningAddr.String())\n\tif rawOld == nil {\n\t\tpanic(ErrRegistryEntryMissing)\n\t}\n\toldEntry := rawOld.(regEntry)\n\toldEntry.RetiredAtHeight = height\n\tsigningRegistry.Set(oldSigningAddr.String(), oldEntry)\n\n\t// Insert the new entry as active.\n\tsigningRegistry.Set(newSigningAddr.String(), regEntry{\n\t\tOperatorAddress:    addr,\n\t\tRegisteredAtHeight: height,\n\t\tRetiredAtHeight:    0,\n\t})\n\n\t// Update the profile.\n\tv.SigningPubKey = newPubKey\n\tv.SigningAddress = newSigningAddr\n\tv.LastRotationHeight = height\n\tvalopers.Set(addr.String(), v)\n\n\t// Apply to consensus via v3, then refresh v3's cache view of the\n\t// profile. Order matters only in that both must complete; tx\n\t// atomicity rolls back together on any panic.\n\tvalidators.RotateValoperSigningKey(cross(cur), addr, oldPubKey, newPubKey)\n\tvalidators.NotifyValoperChanged(cross(cur), addr, v.SigningPubKey, v.SigningAddress, v.KeepRunning)\n}\n\n// GetByAddr fetches the valoper using the operator address, if present.\nfunc GetByAddr(addr address) Valoper {\n\tvaloperRaw := valopers.Get(addr.String())\n\tif valoperRaw == nil {\n\t\tpanic(ErrValoperMissing)\n\t}\n\n\treturn valoperRaw.(Valoper)\n}\n\n// Render renders the current valoper set.\n// \"/r/gnops/valopers\" lists all valopers, paginated.\n// \"/r/gnops/valopers:addr\" shows the detail for the valoper with the addr.\nfunc Render(fullPath string) string {\n\treq := realmpath.Parse(fullPath)\n\tif req.Path == \"\" {\n\t\treturn renderHome(fullPath)\n\t} else {\n\t\taddr := req.Path\n\t\tif len(addr) \u003c 2 || addr[:2] != \"g1\" {\n\t\t\treturn \"invalid address \" + addr\n\t\t}\n\t\tvaloperRaw := valopers.Get(addr)\n\t\tif valoperRaw == nil {\n\t\t\treturn \"unknown address \" + addr\n\t\t}\n\t\tv := valoperRaw.(Valoper)\n\t\treturn \"Valoper's details:\\n\" + v.Render()\n\t}\n}\n\nfunc renderHome(path string) string {\n\t// if there are no valopers, display instructions\n\tif valopers.Size() == 0 {\n\t\treturn ufmt.Sprintf(\"%s\\n\\nNo valopers to display.\", instructions)\n\t}\n\n\tpage := pager.NewPager(valopers, 50, false).MustGetPageByPath(path)\n\n\toutput := \"\"\n\n\t// if we are on the first page, display instructions\n\tif page.PageNumber == 1 {\n\t\toutput += ufmt.Sprintf(\"%s\\n\\n\", instructions)\n\t}\n\n\tfor _, item := range page.Items {\n\t\tv := item.Value.(Valoper)\n\t\toutput += ufmt.Sprintf(\" * [%s](/r/gnops/valopers:%s) - [profile](/r/demo/profile:u/%s)\\n\",\n\t\t\tv.Moniker, v.OperatorAddress, v.OperatorAddress)\n\t}\n\n\toutput += \"\\n\"\n\toutput += page.Picker(path)\n\treturn output\n}\n\n// Validate checks if the fields of the Valoper are valid.\nfunc (v *Valoper) Validate() error {\n\terrs := \u0026combinederr.CombinedError{}\n\n\terrs.Add(validateMoniker(v.Moniker))\n\terrs.Add(validateDescription(v.Description))\n\terrs.Add(validateServerType(v.ServerType))\n\terrs.Add(validateBech32(v.OperatorAddress))\n\terrs.Add(validatePubKey(v.SigningPubKey))\n\n\tif errs.Size() == 0 {\n\t\treturn nil\n\t}\n\n\treturn errs\n}\n\n// Render renders a single valoper with their information.\nfunc (v Valoper) Render() string {\n\toutput := ufmt.Sprintf(\"## %s\\n\", v.Moniker)\n\n\tif v.Description != \"\" {\n\t\toutput += ufmt.Sprintf(\"%s\\n\\n\", v.Description)\n\t}\n\n\toutput += ufmt.Sprintf(\"- Operator Address: %s\\n\", v.OperatorAddress.String())\n\toutput += ufmt.Sprintf(\"- Signing Address: %s\\n\", v.SigningAddress.String())\n\toutput += ufmt.Sprintf(\"- Signing PubKey: %s\\n\", v.SigningPubKey)\n\toutput += ufmt.Sprintf(\"- Server Type: %s\\n\\n\", v.ServerType)\n\toutput += ufmt.Sprintf(\"[Profile link](/r/demo/profile:u/%s)\\n\", v.OperatorAddress)\n\n\treturn output\n}\n\n// isValoper checks if the valoper exists.\nfunc isValoper(addr address) bool {\n\treturn valopers.Has(addr.String())\n}\n\n// validateMoniker checks if the moniker is valid.\nfunc validateMoniker(moniker string) error {\n\tif moniker == \"\" {\n\t\treturn ErrInvalidMoniker\n\t}\n\n\tif len(moniker) \u003e MonikerMaxLength {\n\t\treturn ErrInvalidMoniker\n\t}\n\n\tif !validateMonikerRe.MatchString(moniker) {\n\t\treturn ErrInvalidMoniker\n\t}\n\n\treturn nil\n}\n\n// validateDescription checks if the description is valid.\nfunc validateDescription(description string) error {\n\tif description == \"\" {\n\t\treturn ErrInvalidDescription\n\t}\n\n\tif len(description) \u003e DescriptionMaxLength {\n\t\treturn ErrInvalidDescription\n\t}\n\n\treturn nil\n}\n\n// validateBech32 checks if the value is a valid bech32 address.\nfunc validateBech32(addr address) error {\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\n\treturn nil\n}\n\n// validatePubKey checks if the public key is valid.\nfunc validatePubKey(pubKey string) error {\n\tif _, _, err := bech32.DecodeNoLimit(pubKey); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// validateServerType checks if the server type is valid.\nfunc validateServerType(serverType string) error {\n\tif serverType != ServerTypeCloud \u0026\u0026\n\t\tserverType != ServerTypeOnPrem \u0026\u0026\n\t\tserverType != ServerTypeDataCenter {\n\t\treturn ErrInvalidServerType\n\t}\n\n\treturn nil\n}\n"},{"name":"valopers_test.gno","body":"package valopers\n\nimport (\n\t\"chain\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ownable/v0/exts/authorizable\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Test-local fee constant. Production reads register_fee from sysparams;\n// these tests use this value as a reference Coin for OriginSend setup\n// and (when needed) seed it via testing.SetSysParamUint64.\nvar minFee = chain.NewCoin(\"ugnot\", 20*1_000_000)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert dispatch helpers that gained an `rlm realm` param. These tests\n// pass `func()` callbacks (no crossing inside the callback), so rlm is\n// ignored — a nil realm here is safe.\nvar cur realm\n\n// resetState clears realm-level state and zeroes the valoper sys-params\n// so subtests don't leak through valopers (operator slots),\n// signingRegistry (signing-address uniqueness), or sys-param values.\nfunc resetState() {\n\tvalopers = avl.NewTree()\n\tsigningRegistry = bptree.NewBPTree32()\n\ttesting.SetSysParamUint64(\"node\", \"valoper\", \"register_fee\", 0)\n\ttesting.SetSysParamUint64(\"node\", \"valoper\", \"rotation_fee\", 0)\n\ttesting.SetSysParamInt64(\"node\", \"valoper\", \"rotation_period_blocks\", 600)\n}\n\n// enableRegisterFee seeds register_fee = minFee.Amount in sysparams\n// so the Register fee path fires. Subtests that exercise fee\n// rejection or sufficient-fee acceptance call this after resetState.\nfunc enableRegisterFee() {\n\ttesting.SetSysParamUint64(\"node\", \"valoper\", \"register_fee\", uint64(minFee.Amount))\n}\n\nfunc validValidatorInfo(t *testing.T) struct {\n\tMoniker     string\n\tDescription string\n\tServerType  string\n\tAddress     address\n\tPubKey      string\n} {\n\tt.Helper()\n\n\treturn struct {\n\t\tMoniker     string\n\t\tDescription string\n\t\tServerType  string\n\t\tAddress     address\n\t\tPubKey      string\n\t}{\n\t\tMoniker:     \"test-1\",\n\t\tDescription: \"test-1's description\",\n\t\tServerType:  ServerTypeOnPrem,\n\t\tAddress:     address(\"g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h\"),\n\t\tPubKey:      \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\",\n\t}\n}\n\nfunc TestValopers_Register(cur realm, t *testing.T) {\n\tt.Run(\"already a valoper\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\t\tv := Valoper{\n\t\t\tMoniker:         info.Moniker,\n\t\t\tDescription:     info.Description,\n\t\t\tServerType:      info.ServerType,\n\t\t\tOperatorAddress: info.Address,\n\t\t\tSigningPubKey:   info.PubKey,\n\t\t\tKeepRunning:     true,\n\t\t}\n\n\t\t// Add the valoper directly to the slot.\n\t\tvalopers.Set(v.OperatorAddress.String(), v)\n\n\t\t// Send coins.\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrValoperExists.Error(), func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\t})\n\n\tt.Run(\"no coins deposited\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\t\tenableRegisterFee()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\t\t// Send no coins.\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", 0)})\n\n\t\tuassert.AbortsWithMessage(t, cur, ufmt.Sprintf(\"payment must not be less than %d%s\", minFee.Amount, minFee.Denom), func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\t})\n\n\tt.Run(\"insufficient coins amount deposited\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\t\tenableRegisterFee()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\t\t// Send invalid coins.\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", minFee.Amount-1)})\n\n\t\tuassert.AbortsWithMessage(t, cur, ufmt.Sprintf(\"payment must not be less than %d%s\", minFee.Amount, minFee.Denom), func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\t})\n\n\tt.Run(\"coin amount deposited is not ugnot\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\t\tenableRegisterFee()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\t\t// Send invalid coins.\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"gnogno\", minFee.Amount)})\n\n\t\tuassert.AbortsWithMessage(t, cur, \"incompatible coin denominations: gnogno, ugnot\", func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\t})\n\n\tt.Run(\"squat guard rejects mismatched OriginCaller\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\t// Caller is NOT info.Address: post-genesis squat guard fires.\n\t\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"attacker\")))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrOperatorSquatGuard.Error(), func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\t})\n\n\tt.Run(\"successful registration\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\n\t\t// Send coins.\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotAborts(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tvaloper := GetByAddr(info.Address)\n\n\t\t\tuassert.Equal(t, info.Moniker, valoper.Moniker)\n\t\t\tuassert.Equal(t, info.Description, valoper.Description)\n\t\t\tuassert.Equal(t, info.ServerType, valoper.ServerType)\n\t\t\tuassert.Equal(t, info.Address, valoper.OperatorAddress)\n\t\t\tuassert.Equal(t, info.PubKey, valoper.SigningPubKey)\n\t\t\tuassert.Equal(t, true, valoper.KeepRunning)\n\n\t\t\t// SigningAddress is derived from the pubkey and present.\n\t\t\tderived, err := chain.PubKeyAddress(info.PubKey)\n\t\t\tuassert.NoError(t, err)\n\t\t\tuassert.Equal(t, derived, valoper.SigningAddress)\n\n\t\t\t// signingRegistry tracks the active entry.\n\t\t\tuassert.True(t, signingRegistry.Has(derived.String()), \"signingRegistry must contain the active entry\")\n\t\t})\n\t})\n\n\tt.Run(\"signing-key reuse rejected\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\t// First registration succeeds.\n\t\tuassert.NotAborts(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\t// Second attempt with a different operator addr but the same\n\t\t// pubkey must fail signingRegistry uniqueness.\n\t\tother := testutils.TestAddress(\"other-op\")\n\t\ttesting.SetRealm(testing.NewUserRealm(other))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrSigningKeyTaken.Error(), func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, other, info.PubKey)\n\t\t})\n\t})\n\n\tt.Run(\"front-running guard rejects post-genesis if signing addr already validates\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\t// Seed v3's valset:current with the very signing address that\n\t\t// `info.PubKey` derives to (g1sp8v98...). Any post-genesis\n\t\t// Register attempting the same pubkey now trips\n\t\t// `ChainHeight()\u003e0 \u0026\u0026 validators.IsValidator(signingAddr)`.\n\t\ttesting.SetSysParamStrings(\"node\", \"valset\", \"current\", []string{info.PubKey + \":1\"})\n\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrFrontrunValidator.Error(), func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\t// Cleanup: clear the seeded valset to avoid leaking into\n\t\t// later subtests if the package state isn't reset between\n\t\t// them in this test runner mode.\n\t\ttesting.SetSysParamStrings(\"node\", \"valset\", \"current\", []string{})\n\t})\n}\n\nfunc TestValopers_Register_AuthOwnerIsOperatorAddress(cur realm, t *testing.T) {\n\t// Pin: the Authorizable owner is bound to the OperatorAddress\n\t// (the addr arg), NOT to OriginCaller. This matters in the\n\t// genesis-mode deployer pattern: one signer (e.g., the hardfork\n\t// ceremony deployer) registers profiles for many operators.\n\t// Each operator must end up on their own profile's auth list\n\t// so they can manage it post-genesis without depending on the\n\t// deployer.\n\tt.Run(\"genesis deployer pattern: operator (not deployer) is owner\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\tdeployer := testutils.TestAddress(\"deployer\")\n\n\t\t// Genesis mode: ChainHeight()==0 bypasses the squat guard so\n\t\t// deployer (OriginCaller) can register a profile for a\n\t\t// different operator addr.\n\t\ttesting.SetHeight(0)\n\t\ttesting.SetRealm(testing.NewUserRealm(deployer))\n\t\ttesting.SetOriginCaller(deployer)\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\t// Auth owner must be the operator addr (info.Address), NOT\n\t\t// the deployer.\n\t\tv := GetByAddr(info.Address)\n\t\tuassert.Equal(t, info.Address.String(), v.Auth().Owner().String(),\n\t\t\t\"auth owner must equal OperatorAddress, not OriginCaller\")\n\n\t\t// Operator can manage their own profile post-genesis.\n\t\ttesting.SetHeight(100)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginCaller(info.Address)\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tUpdateMoniker(cross(cur), info.Address, \"operator-renamed\")\n\t\t})\n\t\tuassert.Equal(t, \"operator-renamed\", GetByAddr(info.Address).Moniker)\n\n\t\t// Deployer cannot manage the operator's profile (not on the\n\t\t// auth list).\n\t\ttesting.SetRealm(testing.NewUserRealm(deployer))\n\t\ttesting.SetOriginCaller(deployer)\n\t\tuassert.AbortsContains(t, cur, \"caller is not in authorized list\", func() {\n\t\t\tUpdateMoniker(cross(cur), info.Address, \"deployer-attempt\")\n\t\t})\n\t})\n\n\tt.Run(\"post-genesis self-Register: operator is owner\", func(cur realm, t *testing.T) {\n\t\t// At H\u003e0 the squat guard forces OriginCaller==addr, so owner\n\t\t// would be the same regardless. This subtest pins that the\n\t\t// post-genesis behavior is unchanged.\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetHeight(100)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginCaller(info.Address)\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tv := GetByAddr(info.Address)\n\t\tuassert.Equal(t, info.Address.String(), v.Auth().Owner().String())\n\t})\n}\n\nfunc TestValopers_UpdateAuthMembers(cur realm, t *testing.T) {\n\ttest2Address := testutils.TestAddress(\"test2\")\n\n\tt.Run(\"unauthorized member adds member\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\t// Add the valoper.\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\t// A different caller (not on the auth list) tries to add a member.\n\t\ttesting.SetRealm(testing.NewUserRealm(test2Address))\n\n\t\tuassert.AbortsWithMessage(t, cur, authorizable.ErrNotSuperuser.Error(), func() {\n\t\t\tAddToAuthList(cross(cur), info.Address, test2Address)\n\t\t})\n\t})\n\n\tt.Run(\"unauthorized member deletes member\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tAddToAuthList(cross(cur), info.Address, test2Address)\n\t\t})\n\n\t\t// A different caller tries to delete a member.\n\t\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"attacker\")))\n\n\t\tuassert.AbortsWithMessage(t, cur, authorizable.ErrNotSuperuser.Error(), func() {\n\t\t\tDeleteFromAuthList(cross(cur), info.Address, test2Address)\n\t\t})\n\t})\n\n\tt.Run(\"authorized member adds member\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tAddToAuthList(cross(cur), info.Address, test2Address)\n\t\t})\n\n\t\ttesting.SetRealm(testing.NewUserRealm(test2Address))\n\n\t\tnewMoniker := \"new moniker\"\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tUpdateMoniker(cross(cur), info.Address, newMoniker)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tvaloper := GetByAddr(info.Address)\n\t\t\tuassert.Equal(t, newMoniker, valoper.Moniker)\n\t\t})\n\t})\n}\n\nfunc TestValopers_UpdateMoniker(cur realm, t *testing.T) {\n\ttest2Address := testutils.TestAddress(\"test2\")\n\n\tt.Run(\"non-existing valoper\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrValoperMissing.Error(), func() {\n\t\t\tUpdateMoniker(cross(cur), info.Address, \"new moniker\")\n\t\t})\n\t})\n\n\tt.Run(\"invalid caller\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\t// Change the caller to someone not on the auth list.\n\t\ttesting.SetRealm(testing.NewUserRealm(test2Address))\n\n\t\tuassert.AbortsWithMessage(t, cur, authorizable.ErrNotInAuthList.Error(), func() {\n\t\t\tUpdateMoniker(cross(cur), info.Address, \"new moniker\")\n\t\t})\n\t})\n\n\tt.Run(\"invalid moniker\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tinvalidMonikers := []string{\n\t\t\t\"\",     // Empty\n\t\t\t\"    \", // Whitespace\n\t\t\t\"a\",    // Too short\n\t\t\t\"a very long moniker that is longer than 32 characters\", // Too long\n\t\t\t\"!@#$%^\u0026*()+{}|:\u003c\u003e?/.,;'\",                               // Invalid characters\n\t\t\t\" space in front\",\n\t\t\t\"space in back \",\n\t\t}\n\n\t\tfor _, invalidMoniker := range invalidMonikers {\n\t\t\tuassert.AbortsWithMessage(t, cur, ErrInvalidMoniker.Error(), func() {\n\t\t\t\tUpdateMoniker(cross(cur), info.Address, invalidMoniker)\n\t\t\t})\n\t\t}\n\t})\n\n\tt.Run(\"too long moniker\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrInvalidMoniker.Error(), func() {\n\t\t\tUpdateMoniker(cross(cur), info.Address, strings.Repeat(\"a\", MonikerMaxLength+1))\n\t\t})\n\t})\n\n\tt.Run(\"successful update\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tnewMoniker := \"new moniker\"\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tUpdateMoniker(cross(cur), info.Address, newMoniker)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tvaloper := GetByAddr(info.Address)\n\t\t\tuassert.Equal(t, newMoniker, valoper.Moniker)\n\t\t})\n\t})\n}\n\nfunc TestValopers_UpdateDescription(cur realm, t *testing.T) {\n\ttest2Address := testutils.TestAddress(\"test2\")\n\n\tt.Run(\"non-existing valoper\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrValoperMissing.Error(), func() {\n\t\t\tUpdateDescription(cross(cur), validValidatorInfo(t).Address, \"new description\")\n\t\t})\n\t})\n\n\tt.Run(\"invalid caller\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\ttesting.SetRealm(testing.NewUserRealm(test2Address))\n\n\t\tuassert.AbortsWithMessage(t, cur, authorizable.ErrNotInAuthList.Error(), func() {\n\t\t\tUpdateDescription(cross(cur), info.Address, \"new description\")\n\t\t})\n\t})\n\n\tt.Run(\"empty description\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrInvalidDescription.Error(), func() {\n\t\t\tUpdateDescription(cross(cur), info.Address, \"\")\n\t\t})\n\t})\n\n\tt.Run(\"too long description\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrInvalidDescription.Error(), func() {\n\t\t\tUpdateDescription(cross(cur), info.Address, strings.Repeat(\"a\", DescriptionMaxLength+1))\n\t\t})\n\t})\n\n\tt.Run(\"successful update\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tnewDescription := \"new description\"\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tUpdateDescription(cross(cur), info.Address, newDescription)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tvaloper := GetByAddr(info.Address)\n\t\t\tuassert.Equal(t, newDescription, valoper.Description)\n\t\t})\n\t})\n}\n\nfunc TestValopers_UpdateKeepRunning(cur realm, t *testing.T) {\n\ttest2Address := testutils.TestAddress(\"test2\")\n\n\tt.Run(\"non-existing valoper\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrValoperMissing.Error(), func() {\n\t\t\tUpdateKeepRunning(cross(cur), validValidatorInfo(t).Address, false)\n\t\t})\n\t})\n\n\tt.Run(\"invalid caller\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\ttesting.SetRealm(testing.NewUserRealm(test2Address))\n\n\t\tuassert.AbortsWithMessage(t, cur, authorizable.ErrNotInAuthList.Error(), func() {\n\t\t\tUpdateKeepRunning(cross(cur), info.Address, false)\n\t\t})\n\t})\n\n\tt.Run(\"successful update\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tUpdateKeepRunning(cross(cur), info.Address, false)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tvaloper := GetByAddr(info.Address)\n\t\t\tuassert.Equal(t, false, valoper.KeepRunning)\n\t\t})\n\t})\n}\n\nfunc TestValopers_UpdateServerType(cur realm, t *testing.T) {\n\ttest2Address := testutils.TestAddress(\"test2\")\n\n\tt.Run(\"non-existing valoper\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tuassert.AbortsWithMessage(t, cur, ErrValoperMissing.Error(), func() {\n\t\t\tUpdateServerType(cross(cur), validValidatorInfo(t).Address, ServerTypeCloud)\n\t\t})\n\t})\n\n\tt.Run(\"invalid caller\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\ttesting.SetRealm(testing.NewUserRealm(test2Address))\n\n\t\tuassert.AbortsWithMessage(t, cur, authorizable.ErrNotInAuthList.Error(), func() {\n\t\t\tUpdateServerType(cross(cur), info.Address, ServerTypeCloud)\n\t\t})\n\t})\n\n\tt.Run(\"invalid server type\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tinvalidServerTypes := []string{\n\t\t\t\"\",\n\t\t\t\"invalid\",\n\t\t\t\"Cloud\",      // case sensitive\n\t\t\t\"ON-PREM\",    // case sensitive\n\t\t\t\"datacenter\", // wrong format\n\t\t}\n\n\t\tfor _, invalidType := range invalidServerTypes {\n\t\t\tuassert.AbortsWithMessage(t, cur, ErrInvalidServerType.Error(), func() {\n\t\t\t\tUpdateServerType(cross(cur), info.Address, invalidType)\n\t\t\t})\n\t\t}\n\t})\n\n\tt.Run(\"successful update\", func(cur realm, t *testing.T) {\n\t\tresetState()\n\n\t\tinfo := validValidatorInfo(t)\n\t\ttesting.SetRealm(testing.NewUserRealm(info.Address))\n\t\ttesting.SetOriginSend(chain.Coins{minFee})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), info.Moniker, info.Description, info.ServerType, info.Address, info.PubKey)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tUpdateServerType(cross(cur), info.Address, ServerTypeCloud)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tvaloper := GetByAddr(info.Address)\n\t\t\tuassert.Equal(t, ServerTypeCloud, valoper.ServerType)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tUpdateServerType(cross(cur), info.Address, ServerTypeDataCenter)\n\t\t})\n\n\t\tuassert.NotPanics(t, cur, func() {\n\t\t\tvaloper := GetByAddr(info.Address)\n\t\t\tuassert.Equal(t, ServerTypeDataCenter, valoper.ServerType)\n\t\t})\n\t})\n}\n"},{"name":"z_1_filetest.gno","body":"// PKGPATH: gno.land/r/gnops/valopers/filetests/z_1\n// SEND: 20000000ugnot\n\npackage z_1\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnops/valopers\"\n)\n\nvar g1user = testutils.TestAddress(\"g1user\") // g1vuch2um9wf047h6lta047h6lta047h6l2ewm6w\n\nconst (\n\tvalidMoniker     = \"test-1\"\n\tvalidDescription = \"test-1's description\"\n\tvalidServerType  = valopers.ServerTypeOnPrem\n\tvalidAddress     = address(\"g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h\")\n\tvalidPubKey      = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\"\n)\n\nfunc init(cur realm) {\n\t// OriginCaller must equal the operator address (post-genesis squat guard).\n\ttesting.SetOriginCaller(validAddress)\n\n\t// Register a validator and add the proposal\n\tvalopers.Register(cross(cur), validMoniker, validDescription, validServerType, validAddress, validPubKey)\n}\n\nfunc main() {\n\tprintln(valopers.Render(\"\"))\n}\n\n// Output:\n//\n// # Welcome to the **Valopers** realm\n//\n// ## 📌 Purpose of this Contract\n//\n// The **Valopers** contract is designed to maintain a registry of **validator profiles**. This registry provides essential information to **GovDAO members**, enabling them to make informed decisions when voting on the inclusion of new validators into the **valset**.\n//\n// By registering your validator profile, you contribute to a transparent and well-informed governance process within **gno.land**.\n//\n// ---\n//\n// ## 📝 How to Register Your Validator Node\n//\n// To add your validator node to the registry, use the [**Register**](/r/gnops/valopers$help\u0026func=Register) function with the following parameters:\n//\n// - **Moniker** (Validator Name)\n//   - Must be **human-readable**\n//   - **Max length**: **32 characters**\n//   - **Allowed characters**: Letters, numbers, spaces, hyphens (**-**), and underscores (**_**)\n//   - **No special characters** at the beginning or end\n//\n// - **Description** (Introduction \u0026 Validator Details)\n//   - **Max length**: **2048 characters**\n//   - Must include answers to the questions listed below\n//\n// - **Server Type** (Infrastructure Type)\n//   - Must be one of the following values:\n//     - **cloud**: For validators running on cloud infrastructure (AWS, GCP, Azure, etc.)\n//     - **on-prem**: For validators running on on-premises infrastructure\n//     - **data-center**: For validators running in dedicated data centers\n//\n// - **Operator Address**\n//   - The `g1...` address of your operator account (from your `gnokey` keyring)\n//   - **Must be controlled by the signer** of this transaction — the realm rejects the call if the signer doesn't control that address\n//\n// - **Validator Consensus Public Key**\n//   - Your validator node's consensus public key, in the `gpub1...` format\n//   - Retrieve it by running: `gnoland secrets get validator_key`\n//\n// ### ✍️ Required Information for the Description\n//\n// Please provide detailed answers to the following questions to ensure transparency and improve your chances of being accepted:\n//\n// 1. The name of your validator\n// 2. Networks you are currently validating and your total AuM (assets under management)\n// 3. Links to your **digital presence** (website, social media, etc.). Please include your Discord handle to be added to our main comms channel, the gno.land valoper Discord channel.\n// 4. Contact details\n// 5. Why are you interested in validating on **gno.land**?\n// 6. What contributions have you made or are willing to make to **gno.land**?\n//\n// ---\n//\n// ## 🔄 Updating Your Validator Information\n//\n// After registration, you can update your validator details using the **update functions** provided by the contract.\n//\n// ---\n//\n// ## 📢 Submitting a Proposal to Join the Validator Set\n//\n// Once you're satisfied with your **valoper** profile, you need to notify GovDAO; only a GovDAO member can submit a proposal to add you to the validator set.\n//\n// If you are a GovDAO member, you can nominate yourself by executing the following function: [**r/gnops/valopers/proposal.ProposeNewValidator**](/r/gnops/valopers/proposal$help\u0026func=ProposeNewValidator)\n//\n// This will initiate a governance process where **GovDAO** members will vote on your proposal.\n//\n// ---\n//\n// 🚀 **Register now and become a part of gno.land’s validator ecosystem!**\n//\n// Read more: [How to become a testnet validator](https://gnops.io/articles/guides/become-testnet-validator/) \u003c!-- XXX: replace with a r/gnops/blog:xxx link --\u003e\n//\n// Disclaimer: Please note, registering your validator profile and/or validating on testnets does not guarantee a validator slot on the gno.land beta mainnet. However, active participation and contributions to testnets will help establish credibility and may improve your chances for future validator acceptance. The initial validator amount and valset will ultimately be selected through GovDAO governance proposals and acceptance.\n//\n// ---\n//\n//\n//\n//  * [test-1](/r/gnops/valopers:g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h) - [profile](/r/demo/profile:u/g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h)\n"},{"name":"z_2_filetest.gno","body":"// PKGPATH: gno.land/r/gnops/valopers/filetests/z_2\n// SEND: 20000000ugnot\n\npackage z_2\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnops/valopers\"\n)\n\nvar g1user = testutils.TestAddress(\"g1user\")\n\nconst (\n\tvalidMoniker     = \"test-1\"\n\tvalidDescription = \"test-1's description\"\n\tvalidServerType  = valopers.ServerTypeOnPrem\n\tvalidAddress     = address(\"g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h\")\n\tvalidPubKey      = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\"\n)\n\nfunc init(cur realm) {\n\t// OriginCaller must equal the operator address (post-genesis squat guard).\n\t// validAddress here was chosen so that derive(validPubKey) == validAddress,\n\t// so OperatorAddress == SigningAddress in this fixture.\n\ttesting.SetOriginCaller(validAddress)\n\n\t// Register a validator and add the proposal\n\tvalopers.Register(cross(cur), validMoniker, validDescription, validServerType, validAddress, validPubKey)\n}\n\nfunc main() {\n\t// Simulate clicking on the validator\n\tprintln(valopers.Render(validAddress.String()))\n}\n\n// Output:\n// Valoper's details:\n// ## test-1\n// test-1's description\n//\n// - Operator Address: g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h\n// - Signing Address: g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h\n// - Signing PubKey: gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\n// - Server Type: on-prem\n//\n// [Profile link](/r/demo/profile:u/g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h)\n//\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"vMqycss0cViS+M2frA5O2D9/m00r7gxVlFO1P5Ab2J8+offdvflbPscIcTXq/6TvDLD/P7yirenujC3Q7SbuDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"proposal","path":"gno.land/r/gnops/valopers/proposal","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gnops/valopers/proposal\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"proposal.gno","body":"package proposal\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\tvalopers \"gno.land/r/gnops/valopers\"\n\t\"gno.land/r/gov/dao\"\n\tsysparams \"gno.land/r/sys/params\"\n\tvalidators \"gno.land/r/sys/validators/v3\"\n)\n\nvar (\n\tErrValidatorMissing = errors.New(\"the validator is missing\")\n\tErrSameValues       = errors.New(\"the valoper has the same voting power and pubkey\")\n)\n\n// NewValidatorProposalRequest creates a proposal request to the GovDAO\n// for adding (or removing) the given valoper to/from the validator set.\n//\n// Signature is preserved for historical-replay compatibility (gnoland-1\n// callers call this with a single address). Body is rewired to call\n// v3's operator-keyed NewValidatorProposalRequest with a single-element\n// slice; v3's executor re-resolves the signing pubkey from valoperCache\n// at execution time, so a mid-flight rotation publishes the current key.\nfunc NewValidatorProposalRequest(cur realm, addr address) dao.ProposalRequest {\n\tvar (\n\t\tvaloper     = valopers.GetByAddr(addr)\n\t\tvotingPower = uint64(1)\n\t)\n\n\texist := validators.IsValidator(valoper.SigningAddress)\n\n\t// Determine the voting power\n\tif !valoper.KeepRunning {\n\t\tif !exist {\n\t\t\tpanic(ErrValidatorMissing)\n\t\t}\n\t\tvotingPower = uint64(0)\n\t}\n\n\tif exist {\n\t\tvalidator := validators.GetValidator(valoper.SigningAddress)\n\t\tif validator.VotingPower == votingPower \u0026\u0026 validator.PubKey == valoper.SigningPubKey {\n\t\t\tpanic(ErrSameValues)\n\t\t}\n\t}\n\n\t// Craft the proposal title and description, framed around the\n\t// valoper profile. Voters see the operator identity (moniker +\n\t// operator address); the signing key is an implementation detail\n\t// resolved at execution time by v3's executor.\n\ttitle := ufmt.Sprintf(\n\t\t\"Add valoper %s to the valset\",\n\t\tvaloper.Moniker,\n\t)\n\n\tdescription := ufmt.Sprintf(\"Valoper profile: [%s](/r/gnops/valopers:%s)\\n\\n%s\",\n\t\tvaloper.Moniker,\n\t\tvaloper.OperatorAddress,\n\t\tvaloper.Render(),\n\t)\n\n\treturn validators.NewValidatorProposalRequest(cross(cur),\n\t\t[]validators.ValoperChange{validators.NewValoperChange(valoper.OperatorAddress, votingPower)},\n\t\ttitle,\n\t\tdescription,\n\t)\n}\n\n// ProposeNewInstructionsProposalRequest creates a proposal to the GovDAO\n// for updating the realm instructions.\nfunc ProposeNewInstructionsProposalRequest(cur realm, newInstructions string) dao.ProposalRequest {\n\tcb := valopers.NewInstructionsProposalCallback(newInstructions)\n\t// Create a proposal\n\ttitle := \"/p/gnops/valopers: Update instructions\"\n\tdescription := ufmt.Sprintf(\"Update the instructions to: \\n\\n%s\", newInstructions)\n\n\te := dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\treturn dao.NewProposalRequest(title, description, e)\n}\n\n// ProposeNewMinFeeProposalRequest creates a proposal to the GovDAO\n// for updating the minimum fee to register a new valoper. Signature\n// preserved for historical-replay compatibility (gnoland-1's\n// set_minfee.gno MsgRun calls this); body now delegates to the\n// generic sys/params factory so the fee lives in\n// node:valoper:register_fee.\nfunc ProposeNewMinFeeProposalRequest(cur realm, newMinFee int64) dao.ProposalRequest {\n\treturn sysparams.NewSysParamUint64PropRequest(cross(cur), \"node\", \"valoper\", \"register_fee\", uint64(newMinFee))\n}\n"},{"name":"proposal_test.gno","body":"package proposal\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\t\"gno.land/r/gnops/valopers\"\n\t\"gno.land/r/gov/dao\"\n\tdaoinit \"gno.land/r/gov/dao/v3/init\" // so that the govdao initializer is executed\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar g1user = testutils.TestAddress(\"g1user\")\n\nfunc init(cur realm) {\n\tdaoinit.InitWithUsers(cross(cur), g1user)\n}\n\nfunc TestValopers_ProposeNewValidator(cur realm, t *testing.T) {\n\tconst (\n\t\tregisterMinFee int64 = 20 * 1_000_000 // minimum gnot must be paid to register.\n\t\tproposalMinFee int64 = 100 * 1_000_000\n\n\t\tmoniker     string = \"moniker\"\n\t\tdescription string = \"description\"\n\t\tserverType  string = valopers.ServerTypeOnPrem\n\t\tpubKey             = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\"\n\t)\n\n\t// signingAddr is derived from the pubkey and is what\n\t// v3.IsValidator/GetValidator return after a proposal lands\n\t// (parseEntry in r/sys/params canonicalizes from PubKey). Used\n\t// for the \"ErrSameValues\" subtest's post-execute check.\n\tsigningAddr, err := chain.PubKeyAddress(pubKey)\n\turequire.NoError(t, err, \"valid pubkey\")\n\n\t// The operator address is g1user (the GovDAO member); the\n\t// signing address is derived separately from pubKey. Splitting\n\t// them exercises the operator/signer separation.\n\topAddr := g1user\n\n\t// Set origin caller for valoper operations: must equal the\n\t// operator address (post-genesis squat guard in Register).\n\ttesting.SetRealm(testing.NewUserRealm(opAddr))\n\n\tt.Run(\"remove an unexisting validator\", func(t *testing.T) {\n\t\t// Send coins to be able to register a valoper\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", registerMinFee)})\n\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvalopers.Register(cross(cur), moniker, description, serverType, opAddr, pubKey)\n\t\t\tvalopers.UpdateKeepRunning(cross(cur), opAddr, false)\n\t\t})\n\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvalopers.GetByAddr(opAddr)\n\t\t})\n\n\t\t// Send coins to be able to make a proposal\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", proposalMinFee)})\n\n\t\turequire.AbortsWithMessage(t, cur, ErrValidatorMissing.Error(), func(cur realm) {\n\t\t\tpr := NewValidatorProposalRequest(cur, opAddr)\n\n\t\t\tdao.MustCreateProposal(cross(cur), pr)\n\t\t})\n\t})\n\n\tt.Run(\"proposal successfully created\", func(t *testing.T) {\n\t\t// Send coins to be able to register a valoper\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", registerMinFee)})\n\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvalopers.UpdateKeepRunning(cross(cur), opAddr, true)\n\t\t})\n\n\t\tvar valoper valopers.Valoper\n\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvaloper = valopers.GetByAddr(opAddr)\n\t\t})\n\n\t\t// Send coins to be able to make a proposal\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", proposalMinFee)})\n\n\t\tvar pid dao.ProposalID\n\t\turequire.NotPanics(t, cur, func(cur realm) {\n\t\t\ttesting.SetRealm(testing.NewUserRealm(g1user))\n\t\t\tpr := NewValidatorProposalRequest(cur, opAddr)\n\n\t\t\tpid = dao.MustCreateProposal(cross(cur), pr)\n\t\t})\n\n\t\tproposal, err := dao.GetProposal(pid) // index starts from 0\n\t\turequire.NoError(t, err, \"proposal not found\")\n\n\t\t// The proposal description is now operator-keyed (matches the\n\t\t// new v3.NewValidatorProposalRequest format): both the profile\n\t\t// link and the validator-updates section reference the\n\t\t// OPERATOR address with explicit power.\n\t\tdescription := ufmt.Sprintf(\n\t\t\t\"Valoper profile: [%s](/r/gnops/valopers:%s)\\n\\n%s\\n\\n## Validator Updates\\n- %s: add (power 1)\\n\",\n\t\t\tvaloper.Moniker,\n\t\t\tvaloper.OperatorAddress,\n\t\t\tvaloper.Render(),\n\t\t\tvaloper.OperatorAddress,\n\t\t)\n\n\t\t// Check that the proposal is correct\n\t\turequire.Equal(t, description, proposal.Description())\n\t})\n\n\tt.Run(\"try to update a validator with the same values\", func(t *testing.T) {\n\t\t// Send coins to be able to register a valoper\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", registerMinFee)})\n\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvalopers.GetByAddr(opAddr)\n\t\t})\n\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\t// Vote the proposal created in the previous test\n\t\t\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(0)))\n\n\t\t\t// Execute the proposal — callback writes valset:proposed +\n\t\t\t// dirty=true. v3.IsValidator(signingAddr) below reads the\n\t\t\t// effective view (proposed-when-dirty) and finds the\n\t\t\t// just-added validator → enters the ErrSameValues branch.\n\t\t\tdao.ExecuteProposal(cross(cur), dao.ProposalID(0))\n\t\t})\n\n\t\t// Verify the just-added entry exists at the SIGNING address.\n\t\t_ = signingAddr\n\n\t\t// Send coins to be able to make a proposal\n\t\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", proposalMinFee)})\n\n\t\turequire.AbortsWithMessage(t, cur, ErrSameValues.Error(), func() {\n\t\t\tpr := NewValidatorProposalRequest(cross(cur), opAddr)\n\t\t\tdao.MustCreateProposal(cross(cur), pr)\n\t\t})\n\t})\n}\n\nfunc TestValopers_ProposeNewInstructions(cur realm, t *testing.T) {\n\tconst proposalMinFee int64 = 100 * 1_000_000\n\n\tnewInstructions := \"new instructions\"\n\tdescription := ufmt.Sprintf(\"Update the instructions to: \\n\\n%s\", newInstructions)\n\n\t// Set origin caller\n\ttesting.SetRealm(testing.NewUserRealm(g1user))\n\n\t// Send coins to be able to make a proposal\n\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", proposalMinFee)})\n\n\tvar pid dao.ProposalID\n\turequire.NotPanics(t, cur, func() {\n\t\tpr := ProposeNewInstructionsProposalRequest(cross(cur), newInstructions)\n\n\t\tpid = dao.MustCreateProposal(cross(cur), pr)\n\t})\n\n\tproposal, err := dao.GetProposal(pid) // index starts from 0\n\turequire.NoError(t, err, \"proposal not found\")\n\tif proposal == nil {\n\t\tpanic(\"PROPOSAL NOT FOUND\")\n\t}\n\n\t// Check that the proposal is correct\n\turequire.Equal(t, description, proposal.Description())\n}\n\nfunc TestValopers_ProposeNewMinFee(cur realm, t *testing.T) {\n\tconst proposalMinFee int64 = 100 * 1_000_000\n\tnewMinFee := int64(10)\n\t// ProposeNewMinFeeProposalRequest now delegates to the generic\n\t// sys/params factory; description is the standard one from\n\t// r/sys/params.newPropRequest, keyed on node:valoper:register_fee.\n\tdescription := \"This proposal wants to add a new key to sys/params: node:valoper:register_fee\"\n\n\t// Set origin caller\n\ttesting.SetRealm(testing.NewUserRealm(g1user))\n\n\t// Send coins to be able to make a proposal\n\ttesting.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", proposalMinFee)})\n\n\tvar pid dao.ProposalID\n\turequire.NotPanics(t, cur, func() {\n\t\tpr := ProposeNewMinFeeProposalRequest(cross(cur), newMinFee)\n\n\t\tpid = dao.MustCreateProposal(cross(cur), pr)\n\t})\n\n\tproposal, err := dao.GetProposal(pid) // index starts from 0\n\turequire.NoError(t, err, \"proposal not found\")\n\t// Check that the proposal is correct\n\turequire.Equal(t, description, proposal.Description())\n}\n\n/* TODO fix this @moul\nfunc TestValopers_ProposeNewValidator2(cur realm, t *testing.T) {\n\tconst (\n\t\tregisterMinFee int64 = 20 * 1_000_000 // minimum gnot must be paid to register.\n\t\tproposalMinFee int64 = 100 * 1_000_000\n\n\t\tmoniker     string = \"moniker\"\n\t\tdescription string = \"description\"\n\t\tpubKey             = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\"\n\t)\n\n\t// Set origin caller\n\ttesting.SetRealm(std.NewUserRealm(g1user))\n\n\tt.Run(\"create valid proposal\", func(t *testing.T) {\n\t\t// Validator exists, should not panic\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\t_ = valopers.MustGetValoper(g1user)\n\t\t})\n\n\t\t// Create the proposal\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tcross(valopers.Register)(moniker, description, g1user, pubKey)\n\t\t})\n\n\t\t// Verify proposal details\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvaloper := valopers.MustGetValoper(g1user)\n\t\t\turequire.Equal(t, moniker, valoper.Moniker)\n\t\t\turequire.Equal(t, description, valoper.Description)\n\t\t})\n\t\t// Execute proposal with admin rights\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tstd.TestSetOrigCaller(std.Admin)\n\t\t\tcross(dao.ExecuteProposal)(dao.ProposalID(0))\n\t\t})\n\t\t// Check if valoper was updated\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvaloper := valopers.MustGetValoper(g1user)\n\t\t\turequire.Equal(t, moniker, valoper.Moniker)\n\t\t\turequire.Equal(t, description, valoper.Description)\n\t\t})\n\n\t\t// Expect ExecuteProposal to pass\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tcross(dao.ExecuteProposal)(dao.ProposalID(0))\n\t\t})\n\t\t// Check if valoper was updated\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tvaloper := valopers.MustGetValoper(g1user)\n\t\t\turequire.Equal(t, moniker, valoper.Moniker)\n\t\t\turequire.Equal(t, description, valoper.Description)\n\t\t})\n\t\t// Execute proposal with admin rights\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tstd.TestSetOrigCaller(std.Admin)\n\t\t\tcross(dao.ExecuteProposal)(dao.ProposalID(0))\n\t\t})\n\t})\n}\n*/\n"},{"name":"z_0_a_filetest.gno","body":"// PKGPATH: gno.land/r/test/proposal\n// SEND: 20000000ugnot\n\npackage proposal\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnops/valopers\"\n\t\"gno.land/r/gnops/valopers/proposal\"\n\t\"gno.land/r/gov/dao\"\n\tdaoinit \"gno.land/r/gov/dao/v3/init\"\n)\n\nvar g1user = testutils.TestAddress(\"g1user\")\n\nconst (\n\tvalidMoniker     = \"test-1\"\n\tvalidDescription = \"test-1's description\"\n\tvalidServerType  = valopers.ServerTypeOnPrem\n\tvalidAddress     = address(\"g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h\")\n\totherAddress     = address(\"g1juz2yxmdsa6audkp6ep9vfv80c8p5u76e03vvh\")\n\tvalidPubKey      = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetOriginCaller(g1user)\n\tdaoinit.InitWithUsers(cross(cur), g1user)\n}\n\nfunc main(cur realm) {\n\t// Register a validator: OriginCaller must equal operator addr (squat guard).\n\ttesting.SetOriginCaller(validAddress)\n\tvalopers.Register(cross(cur), validMoniker, validDescription, validServerType, validAddress, validPubKey)\n\n\t// Switch back to g1user for the GovDAO proposal submission.\n\ttesting.SetOriginCaller(g1user)\n\n\t// Try to make a proposal for a non-existing validator\n\tif err := revive(func() {\n\t\tpr := proposal.NewValidatorProposalRequest(cross(cur), otherAddress)\n\t\tdao.MustCreateProposal(cross(cur), pr)\n\t}); err != nil {\n\t\tprintln(\"r: \", err)\n\t}\n}\n\n// Output:\n// r:  valoper does not exist\n"},{"name":"z_1_filetest.gno","body":"// PKGPATH: gno.land/r/test/proposal\n// SEND: 100000000ugnot\n\npackage proposal\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnops/valopers\"\n\t\"gno.land/r/gnops/valopers/proposal\"\n\t\"gno.land/r/gov/dao\"\n\tdaoinit \"gno.land/r/gov/dao/v3/init\" // so that the govdao initializer is executed\n)\n\nvar g1user = testutils.TestAddress(\"g1user\") // g1vuch2um9wf047h6lta047h6lta047h6l2ewm6w\n\nconst (\n\tvalidMoniker     = \"test-1\"\n\tvalidDescription = \"test-1's description\"\n\tvalidServerType  = valopers.ServerTypeOnPrem\n\tvalidAddress     = address(\"g1sp8v98h2gadm5jggtzz9w5ksexqn68ympsd68h\")\n\tvalidPubKey      = \"gpub1pggj7ard9eg82cjtv4u52epjx56nzwgjyg9zqwpdwpd0f9fvqla089ndw5g9hcsufad77fml2vlu73fk8q8sh8v72cza5p\"\n)\n\nfunc init(cur realm) {\n\ttesting.SetOriginCaller(g1user)\n\tdaoinit.InitWithUsers(cross(cur), g1user)\n\n\t// Register a validator: OriginCaller must equal operator addr (squat guard).\n\ttesting.SetOriginCaller(validAddress)\n\tvalopers.Register(cross(cur), validMoniker, validDescription, validServerType, validAddress, validPubKey)\n\n\t// Switch back to g1user for the GovDAO submission.\n\ttesting.SetOriginCaller(g1user)\n\n\tif err := revive(func() {\n\t\tpr := proposal.NewValidatorProposalRequest(cross(cur), validAddress)\n\t\tdao.MustCreateProposal(cross(cur), pr)\n\t}); err != nil {\n\t\tprintln(\"r: \", err)\n\t} else {\n\t\tprintln(\"OK\")\n\t}\n}\n\nfunc main(cur realm) {\n\tprintln(dao.Render(cross(cur), \"\"))\n}\n\n// Output:\n// OK\n// # GovDAO\n// ## Members\n// [\u003e Go to Memberstore \u003c](/r/gov/dao/v3/memberstore)\n// ## Proposals\n// ### [Prop #0 - Add valoper test\\-1 to the valset](/r/gov/dao:0)\n// Author: g1vuch2um9wf047h6lta047h6lta047h6l2ewm6w\n//\n// Status: ACTIVE\n//\n// Tiers eligible to vote: T1, T2, T3\n//\n// ---\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"KmQiGb8WO6TqM/4MacEDIVmVpaA8WoumE4tsaM0UFjmPj5dtk+nE0Po9XBt1ab3ZicgYkBBY0oAsPvu/E5BUAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da","package":{"name":"loader","path":"gno.land/r/gov/dao/v3/loader","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gov/dao/v3/loader\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da\"\n"},{"name":"loader.gno","body":"// loader.gno initialises the govDAO v3 implementation and tier structure.\n//\n// It intentionally does NOT add any members or set AllowedDAOs.  When the\n// allowedDAOs list in the DAO proxy is empty, InAllowedDAOs() returns true\n// for any caller (see r/gov/dao/proxy.gno), which lets a subsequent MsgRun\n// bootstrap the member set and then lock things down.\n//\n// Bootstrap flow (official network genesis or local dev):\n//\n//  1. All packages — including this loader — are deployed via MsgAddPackage.\n//     The loader sets up tier entries and the DAO implementation.\n//  2. A MsgRun executes a setup script (e.g. govdao_prop1.gno) which:\n//     a. Adds a temporary deployer as T1 member (for supermajority).\n//     b. Creates a governance proposal to register validators, votes YES,\n//     and executes it.\n//     c. Adds the real govDAO members directly via memberstore.Get().\n//     d. Removes the temporary deployer.\n//     e. Calls dao.UpdateImpl to set AllowedDAOs, locking down access.\n//\n// See misc/deployments/ for concrete genesis generation examples.\npackage loader\n\nimport (\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/impl\"\n\t\"gno.land/r/gov/dao/v3/memberstore\"\n)\n\nfunc init(cur realm) {\n\t// Create tier entries in the members tree (required before any SetMember).\n\tmemberstore.Get(0, cur).SetTier(memberstore.T1)\n\tmemberstore.Get(0, cur).SetTier(memberstore.T2)\n\tmemberstore.Get(0, cur).SetTier(memberstore.T3)\n\n\t// Set the DAO implementation.  AllowedDAOs is intentionally left empty\n\t// so that the genesis MsgRun can manipulate the memberstore directly.\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(impl.GetInstance(0, cur), nil))\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"7ngRE4VWgV3goqSXSWFwh0Xew2keT2m4ZJYnXpVcHWxDn5v2lN0uM2NgskPd8KgVylhiwr3iU9Ij5ci6Vxl2BQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"test","path":"gno.land/r/gov/dao/v3/treasury/test","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gov/dao/v3/treasury/test\"\ngno = \"0.9\"\n"},{"name":"treasury_test.gno","body":"package test\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"chain/runtime/unsafe\"\n\t\"sort\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/fqname/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\ttrs_pkg \"gno.land/p/nt/treasury/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\n\t\"gno.land/r/demo/defi/grc20reg\"\n\t\"gno.land/r/gov/dao\"\n\t\"gno.land/r/gov/dao/v3/impl\"\n\t\"gno.land/r/gov/dao/v3/treasury\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar (\n\tuser1Addr       = testutils.TestAddress(\"g1user1\")\n\tuser2Addr       = testutils.TestAddress(\"g1user2\")\n\ttreasuryAddr    = chain.PackageAddress(\"gno.land/r/gov/dao/v3/treasury\")\n\tallowedRealm    = testing.NewCodeRealm(\"gno.land/r/test/allowed\")\n\tnotAllowedRealm = testing.NewCodeRealm(\"gno.land/r/test/notallowed\")\n\tmintAmount      = int64(1000)\n)\n\n// Define a dummy trs_pkg.Payment type for testing purposes.\ntype dummyPayment struct {\n\tbankerID string\n\tstr      string\n}\n\nvar _ trs_pkg.Payment = (*dummyPayment)(nil)\n\nfunc (dp *dummyPayment) BankerID() string { return dp.bankerID }\nfunc (dp *dummyPayment) String() string   { return dp.str }\n\nfunc init(cur realm) {\n\t// Register allowed Realm path.\n\tdao.UpdateImpl(cross(cur), dao.NewUpdateRequest(impl.NewGovDAO(), []string{allowedRealm.PkgPath()}))\n}\n\nfunc ugnotCoins(t *testing.T, amount int64) chain.Coins {\n\tt.Helper()\n\n\t// Create a new coin with the ugnot denomination.\n\treturn chain.NewCoins(chain.NewCoin(\"ugnot\", amount))\n}\n\nfunc ugnotBalance(t *testing.T, addr address) int64 {\n\tt.Helper()\n\n\t// Get the balance of ugnot coins for the given address.\n\tbanker_ := banker.NewReadonlyBanker()\n\tcoins := banker_.GetCoins(addr)\n\n\treturn coins.AmountOf(\"ugnot\")\n}\n\n// Define a keyedToken type to hold the token and its key.\ntype keyedToken struct {\n\tkey   string\n\ttoken *grc20.Token\n}\n\nfunc registerGRC20Tokens(_ int, rlm realm, t *testing.T, tokenNames []string, toMint address) []keyedToken {\n\tt.Helper()\n\n\tvar (\n\t\tkeyedTokens = make([]keyedToken, 0, len(tokenNames))\n\t\tkeys        = make([]string, 0, len(tokenNames))\n\t)\n\n\tfor _, name := range tokenNames {\n\t\t// Create the token.\n\t\tsymbol := strings.ToUpper(name)\n\t\ttoken, ledger := grc20.NewToken(0, rlm, name, symbol, 0)\n\n\t\t// Register the token.\n\t\tgrc20reg.Register(cross(rlm), token, symbol)\n\n\t\t// Mint tokens to the specified address.\n\t\tledger.Mint(toMint, mintAmount)\n\n\t\t// Add the token and key to the lists.\n\t\tkey := fqname.Construct(unsafe.CurrentRealm().PkgPath(), symbol)\n\t\tkeyedTokens = append(keyedTokens, keyedToken{key: key, token: token})\n\t\tkeys = append(keys, key)\n\t}\n\n\t// Set the token keys in the treasury.\n\ttreasury.SetTokenKeys(cross(rlm), keys)\n\n\treturn keyedTokens\n}\n\nfunc TestAllowedDAOs(cur realm, t *testing.T) {\n\t// Set the current Realm to the not allowed one.\n\ttesting.SetRealm(notAllowedRealm)\n\n\t// Define a dummy payment to test sending.\n\tdummyP := \u0026dummyPayment{bankerID: \"Dummy\"}\n\n\t// Try to send, it should abort because the Realm is not allowed.\n\tuassert.AbortsWithMessage(\n\t\tt, cur,\n\t\t\"this Realm is not allowed to send payment: \"+notAllowedRealm.PkgPath(),\n\t\tfunc() { treasury.Send(cross(cur), dummyP) },\n\t)\n\n\t// Set the current Realm to the allowed one.\n\ttesting.SetRealm(allowedRealm)\n\n\t// Try to send, it should not abort because the Realm is allowed,\n\t// but because the dummy banker ID is not registered.\n\tuassert.AbortsWithMessage(\n\t\tt, cur,\n\t\t\"banker not found: \"+dummyP.BankerID(),\n\t\tfunc() { treasury.Send(cross(cur), dummyP) },\n\t)\n}\n\nfunc TestRegisteredBankers(t *testing.T) {\n\t// Set the current Realm to the allowed one.\n\ttesting.SetRealm(allowedRealm)\n\n\t// Define the expected banker IDs.\n\texpectedBankerIDs := []string{\n\t\ttrs_pkg.CoinsBanker{}.ID(),\n\t\ttrs_pkg.GRC20Banker{}.ID(),\n\t}\n\n\t// Get the registered bankers from the treasury and compare their lengths.\n\tregistered := treasury.ListBankerIDs()\n\tuassert.Equal(t, len(registered), len(expectedBankerIDs))\n\n\t// The treasury-returned slice is foreign-readonly; copy locally\n\t// before sorting in place.\n\tregisteredBankerIDs := append([]string(nil), registered...)\n\n\t// Sort both slices then compare them.\n\tsort.StringSlice(expectedBankerIDs).Sort()\n\tsort.StringSlice(registeredBankerIDs).Sort()\n\n\tfor i := range expectedBankerIDs {\n\t\tuassert.Equal(t, expectedBankerIDs[i], registeredBankerIDs[i])\n\t}\n\n\t// Test HasBanker method.\n\tfor _, bankerID := range expectedBankerIDs {\n\t\tuassert.True(t, treasury.HasBanker(bankerID))\n\t}\n\tuassert.False(t, treasury.HasBanker(\"UnknownBankerID\"))\n\n\t// Test Address method.\n\tfor _, bankerID := range expectedBankerIDs {\n\t\t// The two bankers used for now should have the treasury Realm address.\n\t\tuassert.Equal(t, treasury.Address(bankerID), treasuryAddr.String())\n\t}\n}\n\nfunc TestSendGRC20Payment(cur realm, t *testing.T) {\n\t// Set the current Realm to the allowed one.\n\ttesting.SetRealm(allowedRealm)\n\n\t// Try to send a GRC20 payment with a not registered token, it should abort.\n\tuassert.AbortsWithMessage(\n\t\tt, cur,\n\t\t\"failed to send payment: GRC20 token not found: UNKNOW\",\n\t\tfunc() {\n\t\t\ttreasury.Send(cross(cur), trs_pkg.NewGRC20Payment(\"UNKNOW\", 100, user1Addr))\n\t\t},\n\t)\n\n\t// Create 3 GRC20 tokens and register them.\n\tkeyedTokens := registerGRC20Tokens(\n\t\t0, cur,\n\t\tt,\n\t\t[]string{\"TestToken0\", \"TestToken1\", \"TestToken2\"},\n\t\ttreasuryAddr,\n\t)\n\n\tconst txAmount = 42\n\n\t// For each token-user pair.\n\tfor i, userAddr := range []address{user1Addr, user2Addr} {\n\t\tfor _, keyed := range keyedTokens {\n\t\t\t// Check that the treasury has the expected balance before sending.\n\t\t\tuassert.Equal(t, keyed.token.BalanceOf(treasuryAddr), mintAmount-int64(txAmount*i))\n\n\t\t\t// Check that the user has no balance before sending.\n\t\t\tuassert.Equal(t, keyed.token.BalanceOf(userAddr), int64(0))\n\n\t\t\t// Try to send a GRC20 payment with a registered token, it should not abort.\n\t\t\tuassert.NotAborts(t, cur, func() {\n\t\t\t\ttreasury.Send(\n\t\t\t\t\tcross(cur),\n\t\t\t\t\ttrs_pkg.NewGRC20Payment(\n\t\t\t\t\t\tkeyed.key,\n\t\t\t\t\t\ttxAmount,\n\t\t\t\t\t\tuserAddr,\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t})\n\n\t\t\t// Check that the user has the expected balance after sending.\n\t\t\tuassert.Equal(t, keyed.token.BalanceOf(userAddr), int64(txAmount))\n\n\t\t\t// Check that the treasury has the expected balance after sending.\n\t\t\tuassert.Equal(t, keyed.token.BalanceOf(treasuryAddr), mintAmount-int64(txAmount*(i+1)))\n\t\t}\n\t}\n\n\t// Get the GRC20Banker ID.\n\tgrc20BankerID := trs_pkg.GRC20Banker{}.ID()\n\n\t// Test Balances method for the GRC20Banker.\n\tbalances := treasury.Balances(grc20BankerID)\n\tuassert.Equal(t, len(balances), len(keyedTokens))\n\n\tcompared := 0\n\tfor _, balance := range balances {\n\t\tfor _, keyed := range keyedTokens {\n\t\t\tif balance.Denom == keyed.key {\n\t\t\t\tuassert.Equal(t, balance.Amount, keyed.token.BalanceOf(treasuryAddr))\n\t\t\t\tcompared++\n\t\t\t}\n\t\t}\n\t}\n\tuassert.Equal(t, compared, len(keyedTokens))\n\n\t// Check the history of the GRC20Banker.\n\thistory := treasury.History(grc20BankerID, 1, 10)\n\tuassert.Equal(t, len(history), 6)\n\n\t// Try to send a dummy payment with the GRC20 banker ID, it should abort.\n\tuassert.AbortsWithMessage(\n\t\tt, cur,\n\t\t\"failed to send payment: invalid payment type\",\n\t\tfunc() {\n\t\t\ttreasury.Send(cross(cur), \u0026dummyPayment{bankerID: grc20BankerID})\n\t\t},\n\t)\n\n\t// Try to send a GRC20 payment without enough balance, it should abort.\n\tuassert.AbortsWithMessage(\n\t\tt, cur,\n\t\t\"failed to send payment: insufficient balance\",\n\t\tfunc() {\n\t\t\ttreasury.Send(\n\t\t\t\tcross(cur),\n\t\t\t\ttrs_pkg.NewGRC20Payment(\n\t\t\t\t\tkeyedTokens[0].key,\n\t\t\t\t\tmintAmount*42, // Try to send more than the treasury has.\n\t\t\t\t\tuser1Addr,\n\t\t\t\t),\n\t\t\t)\n\t\t},\n\t)\n\n\t// Check the history of the GRC20Banker.\n\thistory = treasury.History(grc20BankerID, 1, 10)\n\tuassert.Equal(t, len(history), 6)\n}\n\nfunc TestSendCoinPayment(cur realm, t *testing.T) {\n\t// Set the current Realm to the allowed one.\n\ttesting.SetRealm(allowedRealm)\n\n\t// Issue initial ugnot coins to the treasury address.\n\ttesting.IssueCoins(treasuryAddr, ugnotCoins(t, mintAmount))\n\n\t// Get the CoinsBanker ID.\n\tbankerID := trs_pkg.CoinsBanker{}.ID()\n\n\t// Define helper function to check balances and history.\n\tvar (\n\t\texpectedTreasuryBalance = mintAmount\n\t\texpectedUser1Balance    = int64(0)\n\t\texpectedUser2Balance    = int64(0)\n\t\texpectedHistoryLen      = 0\n\t\tcheckHistoryAndBalances = func() {\n\t\t\tt.Helper()\n\n\t\t\tuassert.Equal(t, ugnotBalance(t, treasuryAddr), expectedTreasuryBalance)\n\t\t\tuassert.Equal(t, ugnotBalance(t, user1Addr), expectedUser1Balance)\n\t\t\tuassert.Equal(t, ugnotBalance(t, user2Addr), expectedUser2Balance)\n\n\t\t\t// Check treasury.Balances returned value.\n\t\t\tbalances := treasury.Balances(bankerID)\n\t\t\tuassert.Equal(t, len(balances), 1)\n\t\t\tuassert.Equal(t, balances[0].Denom, \"ugnot\")\n\t\t\tuassert.Equal(t, balances[0].Amount, expectedTreasuryBalance)\n\n\t\t\t// Check treasury.History returned value.\n\t\t\thistory := treasury.History(bankerID, 1, expectedHistoryLen+1)\n\t\t\tuassert.Equal(t, len(history), expectedHistoryLen)\n\t\t}\n\t)\n\n\t// Check initial balances and history.\n\tcheckHistoryAndBalances()\n\n\tconst txAmount = int64(42)\n\n\t// Treasury send coins.\n\tfor i := int64(0); i \u003c 3; i++ {\n\t\t// Send ugnot coins to user1 and user2.\n\t\tuassert.NotAborts(t, cur, func() {\n\t\t\ttreasury.Send(\n\t\t\t\tcross(cur),\n\t\t\t\ttrs_pkg.NewCoinsPayment(ugnotCoins(t, txAmount), user1Addr),\n\t\t\t)\n\t\t\ttreasury.Send(\n\t\t\t\tcross(cur),\n\t\t\t\ttrs_pkg.NewCoinsPayment(ugnotCoins(t, txAmount), user2Addr),\n\t\t\t)\n\t\t})\n\n\t\t// Update expected balances and history length.\n\t\texpectedTreasuryBalance = mintAmount - txAmount*2*(i+1)\n\t\texpectedUser1Balance = txAmount * (i + 1)\n\t\texpectedUser2Balance = expectedUser1Balance\n\t\texpectedHistoryLen = int(2 * (i + 1))\n\n\t\t// Check balances and history after sending.\n\t\tcheckHistoryAndBalances()\n\t}\n}\n"},{"name":"workaround.gno","body":"package test\n\n// This package exists solely to circumvent a limitation associated with the\n// suffixed test package (a test package sharing the same folder as the main\n// package to be tested but having the suffix _test in its name).\n// Currently, the GnoVM no longer differentiates between the dependencies of a\n// package and its test package, which causes circular dependencies issues.\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"HOLfCiQPlQCGHY+obVjF8nPnRBXPFVfjRCwpUsmra1O+tIrM1xNY+nNN/QWbd8Z0CTQf0ITqg3oEbRTu56e2DA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"cla","path":"gno.land/r/sys/cla","files":[{"name":"admin.gno","body":"package cla\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/moul/helplink\"\n\t\"gno.land/r/gov/dao\"\n)\n\nconst RequiredHashChangedEvent = \"CLARequiredHashChanged\"\n\n// ProposeNewCLA creates a govdao proposal to update the CLA document hash and URL.\n// When executed, it resets all existing signatures.\n// Propose an empty hash to disable CLA enforcement.\nfunc ProposeNewCLA(cur realm, newHash, newURL string) dao.ProposalRequest {\n\tcb := func(cur realm) error {\n\t\tsetRequiredHash(newHash)\n\t\tclaURL = newURL\n\t\treturn nil\n\t}\n\n\tdesc := \"Propose updating the CLA requirement.\\n\\n\"\n\tif requiredHash != \"\" {\n\t\tdesc += \"Current hash: \" + requiredHash + \"\\n\"\n\t}\n\tif claURL != \"\" {\n\t\tdesc += \"Current URL: \" + claURL + \"\\n\"\n\t}\n\tif newHash != \"\" {\n\t\tdesc += \"New hash: \" + newHash + \"\\n\"\n\t}\n\tif newURL != \"\" {\n\t\tdesc += \"New URL: \" + newURL + \"\\n\"\n\t}\n\tif newHash == \"\" {\n\t\tdesc += \"This proposal disables CLA enforcement.\\n\"\n\t}\n\n\treturn dao.NewProposalRequest(\n\t\t\"Update CLA requirement\",\n\t\tdesc,\n\t\tdao.NewSimpleExecutor(0, cur, cb, helplink.Realm(\"gno.land/r/sys/cla\").Home()),\n\t)\n}\n\nfunc setRequiredHash(newHash string) {\n\tprevHash := requiredHash\n\trequiredHash = newHash\n\tsignatures = addrset.Set{} // reset all signatures\n\n\tchain.Emit(\n\t\tRequiredHashChangedEvent,\n\t\t\"from\", prevHash,\n\t\t\"to\", newHash,\n\t)\n}\n"},{"name":"cla.gno","body":"package cla\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/moul/addrset\"\n)\n\nconst SignedEvent = \"CLASigned\"\n\nvar (\n\trequiredHash string // SHA256 hash of the CLA document; empty = enforcement disabled\n\tclaURL       string // URL where the CLA document can be found\n\tsignatures   addrset.Set\n)\n\n// Sign records a CLA signature for the caller.\n// The hash must match the current required hash.\nfunc Sign(cur realm, hash string) {\n\tif hash != requiredHash {\n\t\tpanic(\"hash does not match required CLA hash\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tsignatures.Add(caller)\n\n\tchain.Emit(\n\t\tSignedEvent,\n\t\t\"signer\", caller.String(),\n\t\t\"hash\", hash,\n\t)\n}\n\n// HasValidSignature checks if an address has signed the current required CLA.\n// Returns true if CLA enforcement is disabled (requiredHash == \"\"),\n// or if the address has signed.\nfunc HasValidSignature(addr address) bool {\n\tif requiredHash == \"\" {\n\t\treturn true\n\t}\n\treturn signatures.Has(addr)\n}\n"},{"name":"cla_test.gno","body":"package cla\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nconst (\n\ttestHash1 = \"abc123def456\"\n\ttestHash2 = \"xyz789uvw012\"\n\ttestUser1 = \"g1user1address1234567890\"\n\ttestUser2 = \"g1user2address0987654321\"\n)\n\nfunc resetState() {\n\tsignatures = addrset.Set{}\n\trequiredHash = \"\"\n\tclaURL = \"\"\n}\n\nfunc TestSign(cur realm, t *testing.T) {\n\tresetState()\n\n\tsetRequiredHash(testHash1)\n\n\ttesting.SetRealm(testing.NewUserRealm(testUser1))\n\tSign(cross(cur), testHash1)\n\n\tuassert.True(t, HasValidSignature(address(testUser1)))\n}\n\nfunc TestSign_WrongHash(cur realm, t *testing.T) {\n\tresetState()\n\n\tsetRequiredHash(testHash1)\n\n\ttesting.SetRealm(testing.NewUserRealm(testUser1))\n\tuassert.AbortsWithMessage(t, cur, \"hash does not match required CLA hash\", func() {\n\t\tSign(cross(cur), testHash2)\n\t})\n\n\tuassert.False(t, HasValidSignature(address(testUser1)))\n}\n\nfunc TestHasValidSignature_Disabled(t *testing.T) {\n\tresetState()\n\n\tuassert.Equal(t, \"\", requiredHash)\n\tuassert.True(t, HasValidSignature(address(testUser1)))\n\tuassert.True(t, HasValidSignature(address(testUser2)))\n}\n\nfunc TestHasValidSignature_Valid(cur realm, t *testing.T) {\n\tresetState()\n\n\tsetRequiredHash(testHash1)\n\n\ttesting.SetRealm(testing.NewUserRealm(testUser1))\n\tSign(cross(cur), testHash1)\n\n\tuassert.True(t, HasValidSignature(address(testUser1)))\n}\n\nfunc TestHasValidSignature_NotSigned(t *testing.T) {\n\tresetState()\n\n\tsetRequiredHash(testHash1)\n\n\tuassert.False(t, HasValidSignature(address(testUser1)))\n}\n\nfunc TestSetRequiredHash_ResetsSignatures(cur realm, t *testing.T) {\n\tresetState()\n\n\tsetRequiredHash(testHash1)\n\n\ttesting.SetRealm(testing.NewUserRealm(testUser1))\n\tSign(cross(cur), testHash1)\n\tuassert.True(t, HasValidSignature(address(testUser1)))\n\tuassert.Equal(t, 1, signatures.Size())\n\n\t// Update hash - should reset signatures\n\tsetRequiredHash(testHash2)\n\n\tuassert.False(t, HasValidSignature(address(testUser1)))\n\tuassert.Equal(t, 0, signatures.Size())\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/cla\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package cla\n\nimport (\n\t\"gno.land/p/moul/helplink\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc Render(path string) string {\n\tout := md.H1(\"Contributor License Agreement (CLA)\")\n\n\tout += md.Paragraph(\"A Contributor License Agreement (CLA) must be signed before deploying packages.\")\n\tout += md.Paragraph(\n\t\t\"The Agreement governs Contributions uploaded, published, or made available \" +\n\t\t\t\"for execution on the Gno.land blockchain network, and the \" +\n\t\t\t\"related software and repositories used to publish such Contributions.\",\n\t)\n\n\tif requiredHash == \"\" {\n\t\tout += md.HorizontalRule()\n\t\tout += md.H2(\"Status\")\n\t\tout += md.Paragraph(md.Bold(\"CLA enforcement is currently DISABLED.\"))\n\t\tout += md.Paragraph(\"All package deployments are allowed.\")\n\t\treturn out\n\t}\n\n\tout += md.HorizontalRule()\n\tout += md.H2(\"Status\")\n\tout += md.Paragraph(md.Bold(\"CLA enforcement is ENABLED\"))\n\n\tif claURL != \"\" {\n\t\tout += md.Paragraph(\"You can read the full agreement here: \" + md.Link(claURL, claURL))\n\t}\n\n\ttable := mdtable.Table{Headers: []string{\"\", \"\"}}\n\ttable.Append([]string{md.Bold(\"Required Hash\"), md.InlineCode(requiredHash)})\n\ttable.Append([]string{md.Bold(\"Signers\"), ufmt.Sprintf(\"%d contributor(s)\", signatures.Size())})\n\tout += table.String()\n\n\tout += md.H3(\"Actions\")\n\tout += md.Paragraph(helplink.Func(\"Sign CLA\", \"Sign\", \"hash\", requiredHash))\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"qPzG5F+5w3RHO7c8AA9MJa5dZfj+xXDAM96bSExCbT1BGV6vRJ7VkCV+xL8uZrSUXMlk+1CLn8qkZ1KXYgcvCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"namereg","path":"gno.land/r/sys/namereg/v1","files":[{"name":"admin.gno","body":"package namereg\n\nimport (\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/gov/dao\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\nvar paused = false // XXX: replace with p/moul/authz\n\n//----------------------------------------\n// Privileged mutators.\n\nfunc setPaused(cur realm, newPausedValue bool) {\n\tpaused = newPausedValue\n}\n\nfunc updateUsername(cur realm, userData *susers.UserData, newName string) error {\n\t// UpdateName must be called from this realm.\n\treturn userData.UpdateName(0, cur, newName)\n}\n\nfunc deleteUserdata(cur realm, userData *susers.UserData) error {\n\t// Delete must be called from this realm.\n\treturn userData.Delete(0, cur)\n}\n\nfunc setRegisterPrice(cur realm, newPrice int64) {\n\tregisterPrice = newPrice\n}\n\n//----------------------------------------\n// Public API\n\n// NewSetPausedExecutor allows GovDAO to pause or unpause this realm\nfunc NewSetPausedExecutor(cur realm, newPausedValue bool) dao.ProposalRequest {\n\tcb := func(cur realm) error {\n\t\tsetPaused(cur, newPausedValue)\n\t\treturn nil\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\tif newPausedValue {\n\t\treturn dao.NewProposalRequest(\"User Registry V1: Pause\", \"\", e)\n\t}\n\n\treturn dao.NewProposalRequest(\"User Registry V1: Unpause\", \"\", e)\n}\n\n// ProposeNewName allows GovDAO to propose a new name for an existing user.\n// The associated address and all previous names of a user that changes a\n// name are preserved, and all resolve to the new name.\n//\n// Governance renames bypass the Open Nym Tier `nym-...\\d{3}` format intentionally.\n// The DAO is the trust root for this realm; if voters approve a rename to\n// `vitalik` (e.g. for trademark dispute resolution or system reservations),\n// the validation here imposes no further opinion. The new name is still\n// subject to the base shape enforced by `r/sys/users.validateName`\n// (`^[a-z][a-z0-9]*([_-][a-z0-9]+)*$`, max 64 chars), which runs inside\n// the updateUsername callback below.\nfunc ProposeNewName(cur realm, addr address, newName string) dao.ProposalRequest {\n\tuserData := susers.ResolveAddress(addr)\n\tif userData == nil {\n\t\tpanic(susers.ErrUserNotExistOrDeleted)\n\t}\n\n\tcb := func(cur realm) error {\n\t\terr := updateUsername(cur, userData, newName)\n\t\treturn err\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\treturn dao.NewProposalRequest(\n\t\tufmt.Sprintf(\"User Registry V1: Rename user `%s` to `%s`\", userData.Name(), newName),\n\t\t\"\",\n\t\te,\n\t)\n}\n\n// ProposeDeleteUser allows GovDAO to propose deletion of a user\n// This will make the associated address and names unresolvable.\n// WARN: After deletion, the same address WILL NOT be able to register a new name.\nfunc ProposeDeleteUser(cur realm, addr address, reason string) dao.ProposalRequest {\n\tuserData := susers.ResolveAddress(addr)\n\tif userData == nil {\n\t\tpanic(susers.ErrUserNotExistOrDeleted)\n\t}\n\n\tcb := func(cur realm) error {\n\t\treturn deleteUserdata(cur, userData)\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\treturn dao.NewProposalRequest(\n\t\tufmt.Sprintf(\"User Registry V1: Delete user `%s`\", userData.Name()),\n\t\treason,\n\t\te,\n\t)\n}\n\n// ProposeNewRegisterPrice allows GovDAO to update the price of registration.\n// Rejects prices below MinRegisterPrice (currently 0) at proposal-creation\n// time. (audit finding #14: original code only rejected negative values,\n// which would have been arithmetically nonsensical.)\nfunc ProposeNewRegisterPrice(cur realm, newPrice int64) dao.ProposalRequest {\n\tif newPrice \u003c MinRegisterPrice {\n\t\tpanic(ufmt.Sprintf(\"price below floor: %d ugnot \u003c %d ugnot (MinRegisterPrice)\",\n\t\t\tnewPrice, MinRegisterPrice))\n\t}\n\n\tcb := func(cur realm) error {\n\t\tsetRegisterPrice(cur, newPrice)\n\t\treturn nil\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, cb, \"\")\n\n\treturn dao.NewProposalRequest(\n\t\tufmt.Sprintf(\"User Registry V1: Update registration price to `%d`\", newPrice),\n\t\t\"\",\n\t\te,\n\t)\n}\n"},{"name":"api.gno","body":"package namereg\n\nimport (\n\t\"errors\"\n\t\"regexp\"\n\t\"strings\"\n)\n\n// Open Nym Tier username format. Anchored.\n//   - literal `nym-` prefix (4 chars)\n//   - 5-13 lowercase ASCII letters (the alpha stem)\n//   - exactly 3 trailing decimal digits\n//\n// Total length 12-20 chars. Distinct by length from `g1...` addresses\n// which are always 40 chars.\nconst reNymFormat = `^nym-[a-z]{5,13}\\d{3}$`\n\nvar reNym = regexp.MustCompile(reNymFormat)\n\n// Reserved alpha-stem prefixes. Names whose stem starts with one of\n// these are rejected at format-validation time with ErrReservedPrefix\n// (clearer than ErrCanonicalCollision after the fact).\n//\n// `gi` is intentionally NOT listed: in most rendering targets `i` is\n// visually distinct enough from `1`/`l` that legitimate `gi*` names\n// (giggles, gimbal, gift, etc.) should remain registerable. Phishing\n// protection for the visual class is still enforced by canonical-\n// collision detection in r/sys/users — once any `gi*` or `gl*` name\n// is registered, all variants under the {l,i,1}→i canonicalization\n// collide.\n//\n// `gl` and `g1` remain listed because they're more visually\n// confusable with the bech32 address prefix `g1`. `g1` itself is\n// unreachable through the alpha-only stem regex; defense-in-depth\n// for any future regex relaxation.\nvar reservedPrefixes = []string{\"gl\", \"g1\", \"gno\", \"atom\", \"atone\", \"photon\", \"cosmos\"}\n\n// Exported error sentinels returned by ValidateNymFormat. Use\n// errors.Is or direct equality; do not string-match.\n//\n// ErrReservedPrefix's message is built from reservedPrefixes at package\n// init time so the surfaced list never drifts from the actual policy.\n//\n// Canonical-collision detection moved to r/sys/users in Option B.\n// Consumers that previously caught namereg.ErrCanonicalCollision\n// should switch to susers.ErrCanonicalCollision.\nvar (\n\tErrInvalidFormat  = errors.New(\"namereg: name must match nym-[a-z]{5,13}\\\\d{3}\")\n\tErrReservedPrefix = errors.New(\"namereg: stem starts with a reserved prefix (\" + strings.Join(reservedPrefixes, \"/\") + \")\")\n\tErrBlacklisted    = errors.New(\"namereg: stem matches a reserved role name\")\n)\n\n// IsReserved reports whether the given alpha stem matches a reserved\n// role name (with implicit `s`-suffix expansion). The check is\n// canonicalized — so `vital1k`-style l-substituted variants of a\n// reserved name are also caught. O(1) backed by `reservedSet` built\n// in init().\nfunc IsReserved(stem string) bool {\n\t_, found := reservedSet[Canonicalize(stem)]\n\treturn found\n}\n\n// ValidateNymFormat checks the regex, prefix-exclusion, and reserved-\n// name rules in that order. Returns one of the exported sentinel\n// errors per failure mode, or nil on success.\n//\n// Does NOT run the canonical-collision check — that lives in r/sys/users\n// (susers.IsCanonicalTaken or, atomically with the write, inside\n// susers.RegisterUser).\nfunc ValidateNymFormat(username string) error {\n\tif !reNym.MatchString(username) {\n\t\treturn ErrInvalidFormat\n\t}\n\n\t// Stem is everything between `nym-` (4 chars) and the trailing\n\t// 3 digits. Regex guarantees 5..13 alpha chars in this slice.\n\tstem := username[4 : len(username)-3]\n\n\tfor _, p := range reservedPrefixes {\n\t\tif strings.HasPrefix(stem, p) {\n\t\t\treturn ErrReservedPrefix\n\t\t}\n\t}\n\n\tif IsReserved(stem) {\n\t\treturn ErrBlacklisted\n\t}\n\n\treturn nil\n}\n\n// IsPaused exposes the realm's pause flag for cross-controller\n// coordination.\nfunc IsPaused() bool {\n\treturn paused\n}\n"},{"name":"blacklist.gno","body":"package namereg\n\n// reservedNames lists role/system identifiers that must never be allocated\n// as a registered name. The intent is to prevent Open Nym Tier\n// auto-registrations like \"nym-admin000\" from impersonating system roles.\n//\n// Sources merged here:\n//   - Common role names already covered by Handshake's valid.json (the 90k\n//     curated trademark/gTLD-application list) — admin, help, support, etc.\n//   - Common role names NOT covered by Handshake — administrator, root,\n//     sysadmin, owner, staff, api, etc.\n//   - RFC 2606 / RFC 6761 reserved labels — example, invalid, localhost,\n//     local, test.\n//\n// Plural rule: every entry below is ALSO reserved with the literal \"s\"\n// suffix appended. So \"doc\" reserves both \"doc\" and \"docs\"; \"setting\"\n// reserves both \"setting\" and \"settings\"; \"new\" covers \"news\", and so on.\n// Entries are stored in the singular here and the validator appends \"s\"\n// at check time. This halves list maintenance and avoids the temptation\n// to add `name+\"s\"` after every singular entry.\n//\n// Note on length: the Open Nym Tier regex restricts the [a-z]{5,13}\n// middle to 5–13 chars, so entries shorter than 5 (mod, api, bot, god,\n// gno, ...) and longer than 13 (administrator, jesuschrist,\n// newtendermint, ...) cannot appear in Register() even without this list.\n// They are kept anyway because:\n//\n//\t(a) cross-reference value — a single canonical list is easier to audit\n//\t    than two scope-specific lists with overlapping intent; and\n//\t(b) future controllers (e.g. a hypothetical DAO-allocated path) can\n//\t    opt in to the same blacklist by querying IsReserved, providing\n//\t    defense-in-depth across the registration ecosystem.\n//\n// IMPORTANT: this list is NOT consulted by `ProposeNewName` — governance\n// renames are gated by GovDAO vote alone, not by this blacklist. Voters\n// reviewing a rename proposal are responsible for catching collisions\n// with reserved names.\n//\n// Sortedness: entries must be sorted lexicographically (Go's \u003c on strings,\n// which is byte-wise ASCII). TestReservedNamesSorted enforces this.\nvar reservedNames = []string{\n\t\"about\",\n\t\"abuse\",\n\t\"account\",\n\t\"admin\",\n\t\"administrator\",\n\t\"aib\",\n\t\"aibinc\",\n\t\"allinbits\",\n\t\"allinbitsinc\",\n\t\"anonymous\",\n\t\"api\",\n\t\"atom\",\n\t\"atomone\",\n\t\"atomonehub\",\n\t\"atone\",\n\t\"atonehub\",\n\t\"bitcoin\",\n\t\"blockchain\",\n\t\"blog\",\n\t\"bot\",\n\t\"chain\",\n\t\"coin\",\n\t\"community\",\n\t\"contact\",\n\t\"cosmos\",\n\t\"cosmoshub\",\n\t\"crypto\",\n\t\"daemon\",\n\t\"dashboard\",\n\t\"default\",\n\t\"demo\",\n\t\"doc\",\n\t\"domain\",\n\t\"email\",\n\t\"ether\",\n\t\"ethereum\",\n\t\"everyone\",\n\t\"example\",\n\t\"gno\",\n\t\"gnoland\",\n\t\"gnolang\",\n\t\"gnome\",\n\t\"gnot\",\n\t\"gnoworld\",\n\t\"god\",\n\t\"gov\",\n\t\"govdao\",\n\t\"governance\",\n\t\"guest\",\n\t\"help\",\n\t\"home\",\n\t\"host\",\n\t\"info\",\n\t\"invalid\",\n\t\"jaekwon\",\n\t\"jesus\",\n\t\"jesuschrist\",\n\t\"local\",\n\t\"localhost\",\n\t\"login\",\n\t\"mail\",\n\t\"mod\",\n\t\"moderator\",\n\t\"new\",\n\t\"newtendermint\",\n\t\"newtendermintllc\",\n\t\"nt\",\n\t\"ntllc\",\n\t\"null\",\n\t\"owner\",\n\t\"photon\",\n\t\"profile\",\n\t\"register\",\n\t\"registry\",\n\t\"resolution\",\n\t\"resolve\",\n\t\"resolver\",\n\t\"root\",\n\t\"security\",\n\t\"service\",\n\t\"setting\",\n\t\"shop\",\n\t\"signup\",\n\t\"staff\",\n\t\"store\",\n\t\"support\",\n\t\"sys\",\n\t\"sysadmin\",\n\t\"system\",\n\t\"team\",\n\t\"tendermint\",\n\t\"test\",\n\t\"user\",\n}\n"},{"name":"blacklist_test.gno","body":"package namereg\n\nimport \"testing\"\n\n// TestReservedNamesSorted enforces lexicographic ordering on reservedNames.\n// The list is intended to be auditable at a glance and to grow over time;\n// keeping it sorted makes diffs minimal and makes manual scanning for a\n// given entry feasible. If this fails after a maintainer adds an entry,\n// re-sort the slice rather than weakening the test.\nfunc TestReservedNamesSorted(cur realm, t *testing.T) {\n\tfor i := 1; i \u003c len(reservedNames); i++ {\n\t\tif reservedNames[i-1] \u003e= reservedNames[i] {\n\t\t\tt.Errorf(\n\t\t\t\t\"reservedNames not sorted at index %d: %q should come after %q\",\n\t\t\t\ti, reservedNames[i-1], reservedNames[i],\n\t\t\t)\n\t\t}\n\t}\n}\n"},{"name":"canonical.gno","body":"package namereg\n\nimport (\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// reservedSet is the runtime O(1) lookup for the role-name blacklist.\n// Built in init() from reservedNames in blacklist.gno: each source\n// entry contributes BOTH `Canonicalize(n)` and `Canonicalize(n+\"s\")`\n// as keys, implementing the \"implicit `s` suffix\" rule documented on\n// reservedNames.\n//\n// Why canonicalize the blacklist itself: validation canonicalizes the\n// candidate stem before checking, so the comparison set must also be\n// in canonical form. Otherwise a candidate like \"vital1k\" would\n// canonicalize to \"vitaiik\" but the blacklist would contain only\n// \"vitalik\" — the comparison would miss the match. Keeping both sides\n// in canonical form makes the lookup exact.\n//\n// The blacklist remains namereg/v1-local because it is policy specific\n// to the Open Nym Tier. Other controllers may have entirely different\n// reserved-name policies, or none at all.\nvar reservedSet map[string]struct{}\n\nfunc init() {\n\treservedSet = make(map[string]struct{}, len(reservedNames)*2)\n\tfor _, n := range reservedNames {\n\t\treservedSet[Canonicalize(n)] = struct{}{}\n\t\treservedSet[Canonicalize(n+\"s\")] = struct{}{}\n\t}\n}\n\n// Canonicalize is a delegating shim to r/sys/users.Canonicalize.\n//\n// HISTORY: namereg/v1 used to host its own per-stem canonical store and\n// its own Canonicalize (l→i only). Option B unified the canonical lookup\n// into r/sys/users keyed by full canonical name with broader\n// substitutions ({l,i,1}→i, {0,o}→o, {-,.,_} stripped). This shim\n// preserves the call-site name for local consumers (blacklist init,\n// IsReserved) and any external consumer that imported the function\n// from namereg/v1 before Option B.\n//\n// New code should call susers.Canonicalize directly.\nfunc Canonicalize(s string) string {\n\treturn susers.Canonicalize(s)\n}\n"},{"name":"canonical_test.gno","body":"package namereg\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// Canonicalize is now a delegating shim to susers.Canonicalize; verify\n// the local export and the upstream produce the same output for the\n// rules namereg/v1 cares about.\nfunc TestCanonicalize_DelegatesToSusers(cur realm, t *testing.T) {\n\tcases := []string{\n\t\t\"\",\n\t\t\"a\",\n\t\t\"alice\",\n\t\t\"vitalik\",\n\t\t\"vital1k\",\n\t\t\"balloon\",\n\t\t\"already-canonical\",\n\t\t\"xyz123\",\n\t\t\"nym-foolbar000\",\n\t\t\"nym-foolbar001\",\n\t}\n\tfor _, in := range cases {\n\t\tuassert.Equal(t, susers.Canonicalize(in), Canonicalize(in),\n\t\t\t\"namereg.Canonicalize must match susers.Canonicalize\")\n\t}\n}\n\nfunc TestReservedSet_BuiltFromBlacklist(cur realm, t *testing.T) {\n\t// Sanity: every entry from reservedNames AND its `+s` form must be\n\t// in the canonicalized lookup.\n\tfor _, n := range reservedNames {\n\t\t_, gotBare := reservedSet[Canonicalize(n)]\n\t\tuassert.True(t, gotBare,\n\t\t\t\"reservedSet missing canonical(%q)\", n)\n\n\t\t_, gotPlural := reservedSet[Canonicalize(n+\"s\")]\n\t\tuassert.True(t, gotPlural,\n\t\t\t\"reservedSet missing canonical(%q+s)\", n)\n\t}\n}\n\nfunc TestIsReserved_LiteralAndCanonical(cur realm, t *testing.T) {\n\t// Literal entry must be reserved.\n\tuassert.True(t, IsReserved(\"admin\"), \"admin should be reserved\")\n\n\t// `blogs` covered via `blog`+s rule (blog is in reservedNames).\n\tuassert.True(t, IsReserved(\"blogs\"), \"blogs covered by blog+s\")\n\n\t// l-substituted: `blog` canonicalizes to `biog`. Both forms blocked\n\t// because we canonicalize the candidate before lookup.\n\tuassert.True(t, IsReserved(\"biog\"), \"canonical(blog)=biog also blocked\")\n\n\t// 1-substituted under the new susers.Canonicalize rules: `1` → `i`.\n\t// `b1og` canonicalizes to `biog`, same as `blog`. Confirms the shim\n\t// picks up the broader rule set.\n\tuassert.True(t, IsReserved(\"b1og\"), \"1→i variant also blocked\")\n\n\t// Non-reserved stem should not be flagged.\n\tuassert.False(t, IsReserved(\"zulufoxtrot\"), \"zulufoxtrot is not reserved\")\n\n\t// Identity passthrough.\n\tuassert.True(t, IsReserved(\"tendermint\"))\n}\n"},{"name":"errors.gno","body":"package namereg\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tErrNonUserCall     = errors.New(\"r/gnoland/users: non-user call\")\n\tErrPaused          = errors.New(\"r/gnoland/users: paused\")\n\tErrInvalidUsername = errors.New(\"r/gnoland/users: invalid username\")\n\n\t// ErrInvalidPayment is the sentinel for \"OriginSend amount didn't\n\t// match registerPrice.\" It deliberately omits the price from its\n\t// message — the price is read at panic time via errInvalidPayment()\n\t// below, so users see the CURRENT price even after governance has\n\t// changed it. Tests that match against this error use it as a\n\t// substring check via uassert.AbortsWithMessage. (audit finding #13)\n\tErrInvalidPayment = errors.New(\"r/gnoland/users: invalid payment amount\")\n)\n\n// errInvalidPayment returns the panic value for an OriginSend mismatch.\n// Constructed lazily so the formatted price reflects the current value\n// of registerPrice rather than the value frozen at package init time.\n// Replaces the old `ErrInvalidPayment = ufmt.Errorf(...)` package var\n// (audit finding #13) which captured registerPrice once and silently\n// drifted out of date after every ProposeNewRegisterPrice execution.\nfunc errInvalidPayment() error {\n\treturn ufmt.Errorf(\"%s: must send exactly %d ugnot\",\n\t\tErrInvalidPayment.Error(), registerPrice)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/namereg/v1\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"init.gno","body":"package namereg\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\n\tsusers \"gno.land/r/sys/users\"\n)\n\nfunc init(cur realm) {\n\tif runtime.ChainHeight() == 0 {\n\t\tsusers.AddControllerAtGenesis(cross(cur), chain.PackageAddress(\"gno.land/r/sys/namereg/v1\"))\n\t}\n}\n"},{"name":"preregister.gno","body":"package namereg\n\nimport (\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// Pre-registered names bypass the Open Nym Tier `nym-...\\d{3}` format\n// intentionally. They are bootstrap names allocated at genesis and are\n// not subject to the auto-registration regex. They DO appear in\n// reservedNames as a defense-in-depth (so a future governance-bypass\n// path can't accidentally reallocate them either), but at this layer\n// they are written directly via susers.RegisterUserIgnoreCanonical.\n//\n// Uses the bypass path so curated confusables in the seed (e.g.\n// `gnoland` and `gnolang` both canonicalize distinctly today, but a\n// future addition that collides must not abort chain bring-up).\n// Matches the genesis posture in r/sys/users/init.gno.\n\n// pre-registered users\nvar preRegisteredUsers = []struct {\n\tName    string\n\tAddress address\n}{\n\t// system names.\n\t// the goal is to make them either team/DAO-owned or ownerless.\n\t{\"archive\", \"g1xlnyjrnf03ju82v0f98ruhpgnquk28knmjfe5k\"}, // -\u003e @archive\n\t{\"demo\", \"g13ek2zz9qurzynzvssyc4sthwppnruhnp0gdz8n\"},    // -\u003e @demo\n\t{\"gno\", \"g19602kd9tfxrfd60sgreadt9zvdyyuudcyxsz8a\"},     // -\u003e @gno\n\t{\"gnoland\", \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"}, // -\u003e @gnoland\n\t{\"gnolang\", \"g1yjlnm3z2630gg5mryjd79907e0zx658wxs9hnd\"}, // -\u003e @gnolang\n\t{\"gov\", \"g1g73v2anukg4ej7axwqpthsatzrxjsh0wk797da\"},     // -\u003e @gov\n\t{\"nt\", \"g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l\"},      // -\u003e @nt\n\t{\"sys\", \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"},     // -\u003e @sys\n\t{\"x\", \"g164sdpew3c2t3rvxj3kmfv7c7ujlvcw2punzzuz\"},       // -\u003e @x\n\n\t// test1 user\n\t{\"test1\", \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"}, // -\u003e @test1\n}\n\nfunc init(cur realm) {\n\t// add pre-registered users via the bypass path (genesis posture).\n\t// Errors are intentionally discarded; the seed is curated and any\n\t// duplicate-address / already-deleted entries (re-init across test\n\t// realm reuse) just no-op.\n\tfor _, res := range preRegisteredUsers {\n\t\tsusers.RegisterUserIgnoreCanonical(cross(cur), res.Name, res.Address)\n\t}\n}\n"},{"name":"render.gno","body":"package namereg\n\nimport (\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/demo/profile\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\nfunc Render(path string) string {\n\treq := realmpath.Parse(path)\n\n\tif req.Path == \"\" {\n\t\treturn renderHomePage()\n\t}\n\n\t// Otherwise, render the user page\n\treturn renderUserPage(req.Path)\n}\n\nfunc renderHomePage() string {\n\tvar out string\n\n\tout += \"# Gno.land User Registry\\n\"\n\n\tif paused {\n\t\tout += md.HorizontalRule()\n\t\tout += md.H2(\"This realm is paused.\")\n\t\tout += md.Paragraph(\"Check out [`gno.land/r/sys/users`](/r/sys/users) for the current user registry.\")\n\t\tout += md.HorizontalRule()\n\t}\n\n\tout += renderIntroParagraph()\n\n\tout += md.H2(\"Latest registrations\")\n\tout += RenderLatestUsersWidget(-1)\n\n\treturn out\n}\n\nfunc renderIntroParagraph() string {\n\tout := md.Paragraph(\"Welcome to the Gno.land User Registry (v1). Please register a username.\")\n\tout += md.Paragraph(`Registering a username grants the registering address the right to deploy packages and realms\nunder that username’s namespace. For example, if an address registers the username ` + md.InlineCode(\"nym-alice123\") + `, it\nwill gain permission to deploy packages and realms to package paths with the pattern ` + md.InlineCode(\"gno.land/{p,r}/nym-alice123/*\") + `.`)\n\n\tout += md.Paragraph(\"In V1, usernames must match `nym-\u003cstem\u003e\u003cdigits\u003e`, where:\")\n\titems := []string{\n\t\t\"`\u003cstem\u003e` is 5 to 13 lowercase ASCII letters\",\n\t\t\"`\u003cdigits\u003e` is exactly 3 decimal digits\",\n\t\t\"The stem must NOT start with `gno`, `gl`, `atom`, `atone`, `photon`, or `cosmos` (reserved prefixes)\",\n\t\t\"The stem must NOT match a reserved role name (admin, root, support, ...)\",\n\t\t\"Confusable variants (e.g. `vitaiik` vs `vitalik` via `l↔i`) are blocked via canonical-form collision detection\",\n\t\t\"Total username length: 12–20 chars (distinct from `g1...` addresses, which are always 40 chars)\",\n\t}\n\tout += md.BulletList(items)\n\n\tout += \"\\n\\n\"\n\tout += md.Paragraph(\"Vanity names outside this format may be allocated by GovDAO governance through `ProposeNewName`.\")\n\n\tif !paused {\n\t\tamount := ufmt.Sprintf(\"%dugnot\", registerPrice)\n\t\tlink := txlink.NewLink(\"Register\")\n\t\tif registerPrice \u003e 0 {\n\t\t\tlink = link.SetSend(amount)\n\t\t}\n\n\t\tout += md.H3(ufmt.Sprintf(\" [[Click here to register]](%s)\", link.URL()))\n\t\t// XXX: Display registration price adjusting for dynamic GNOT price when it becomes possible.\n\t\tout += ufmt.Sprintf(\"Registration price: %f GNOT (%s)\\n\\n\", float64(registerPrice)/1_000_000, amount)\n\t}\n\n\tout += md.HorizontalRule()\n\tout += \"\\n\\n\"\n\n\treturn out\n}\n\n// resolveUser resolves the user based on the path, determining if it's a name or address\nfunc resolveUser(path string) (*susers.UserData, bool, bool) {\n\tif address(path).IsValid() {\n\t\treturn susers.ResolveAddress(address(path)), false, false\n\t}\n\n\tdata, isLatest := susers.ResolveName(path)\n\treturn data, isLatest, true\n}\n\n// renderUserPage generates the user page based on user data and path\nfunc renderUserPage(path string) string {\n\tvar out string\n\n\t// Render single user page\n\tdata, isLatest, isName := resolveUser(path)\n\tif data == nil {\n\t\tout += md.H1(\"User not found.\")\n\t\tout += \"This user does not exist or has been deleted.\\n\"\n\t\treturn out\n\t}\n\n\tout += md.H1(\"User - \" + md.InlineCode(data.Name()))\n\n\tif isName \u0026\u0026 !isLatest {\n\t\tout += md.Paragraph(ufmt.Sprintf(\n\t\t\t\"Note: You searched for `%s`, which is a previous name of [`%s`](/u/%s).\",\n\t\t\tpath, data.Name(), data.Name()))\n\t} else {\n\t\tout += ufmt.Sprintf(\"Address: %s\\n\\n\", data.Addr().String())\n\n\t\tout += md.H2(\"Bio\")\n\t\tout += profile.GetStringField(data.Addr(), \"Bio\", \"No bio defined.\")\n\t\tout += \"\\n\\n\"\n\t\tout += ufmt.Sprintf(\"[Update bio](%s)\", txlink.Realm(\"gno.land/r/demo/profile\").Call(\"SetStringField\", \"field\", \"Bio\"))\n\t\tout += \"\\n\\n\"\n\t}\n\n\treturn out\n}\n\n// RenderLatestUsersWidget renders the latest num registered users.\n// For num = -1, the maximum number (100) will be displayed.\nfunc RenderLatestUsersWidget(num int) string {\n\tsize := latestUsers.Size()\n\tif size == 0 {\n\t\treturn \"No registered users.\"\n\t}\n\n\tif num \u003e size || num \u003c 0 {\n\t\tnum = size\n\t}\n\n\tentries := latestUsers.Entries()\n\tvar out string\n\n\tfor i := size - 1; i \u003e= size-num; i-- {\n\t\tuser := entries[i].(string)\n\t\tout += md.BulletItem(md.UserLink(user))\n\t}\n\n\treturn out\n}\n"},{"name":"users.gno","body":"package namereg\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/moul/fifo\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// MinRegisterPrice is the lowest price (in ugnot) that\n// ProposeNewRegisterPrice will accept. Set to 0 — registration is free\n// by default; governance can raise the price via ProposeNewRegisterPrice\n// without a floor.\nconst MinRegisterPrice = int64(0)\n\nvar (\n\tregisterPrice = int64(0)      // free by default; governance can raise via ProposeNewRegisterPrice\n\tlatestUsers   = fifo.New(100) // Save the latest 100 users for rendering purposes\n)\n\n// Register registers a new username for the caller.\n//\n// Valid usernames match `nym-[a-z]{5,13}\\d{3}`:\n//   - literal `nym-` prefix (4 chars)\n//   - 5-13 lowercase letters (the alpha stem)\n//   - exactly 3 trailing decimal digits\n//\n// Total length 12-20 chars. The alpha stem additionally must NOT start\n// with `gno`/`gi`/`gl` and must not match a reserved role name (with\n// implicit `s`-suffix expansion). See ValidateNymFormat for the\n// format/blacklist check.\n//\n// Canonical-collision detection is enforced atomically by\n// susers.RegisterUser via the unified canonical store in r/sys/users\n// (decision: per Option B, every controller participates in the same\n// canonical-form lookup keyed by full canonical name).\n//\n// Only direct EOA (maketx call) invocations are supported.\nfunc Register(cur realm, username string) {\n\t// Anti-squatting payment check, two paired guards:\n\t//\n\t//   (a) PreviousRealm must be a pure EOA (IsUserCall: pkgPath == \"\").\n\t//       This excludes intermediate code realms AND user-run ephemeral\n\t//       realms (\"maketx run\" scripts). Both can attach -send to the\n\t//       tx but spend the coins on something other than forwarding to\n\t//       this realm, leaving OriginSend() describing a phantom payment.\n\t//       IsUserCall is the only PreviousRealm shape where the tx-send\n\t//       envelope is guaranteed to have landed at this realm's address.\n\t//\n\t//   (b) OriginSend amount must exactly equal registerPrice. Verifies\n\t//       the tx actually attached the expected amount.\n\t//\n\t// Both checks MUST run together. Removing (a) alone makes (b) meaningless\n\t// because OriginSend() describes tx intent, not realm receipt.\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(ErrNonUserCall)\n\t}\n\n\tif paused {\n\t\tpanic(ErrPaused)\n\t}\n\n\tif unsafe.OriginSend().AmountOf(\"ugnot\") != registerPrice {\n\t\tpanic(errInvalidPayment())\n\t}\n\n\t// Format + prefix + reserved-name check. ValidateNymFormat returns\n\t// one of ErrInvalidFormat, ErrReservedPrefix, ErrBlacklisted.\n\tif err := ValidateNymFormat(username); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Delegate the canonical-collision check + nameStore write atomically\n\t// to r/sys/users. Returns susers.ErrCanonicalCollision if the\n\t// canonical form clashes with an existing registration in any\n\t// controller.\n\tregistrant := cur.Previous().Address()\n\tif err := susers.RegisterUser(cross(cur), username, registrant); err != nil {\n\t\tpanic(err)\n\t}\n\n\tlatestUsers.Append(username)\n\tchain.Emit(\"Registration\", \"address\", registrant.String(), \"name\", username)\n}\n"},{"name":"users_test.gno","body":"package namereg\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc init(cur realm) {\n\t// Unit tests run with DefaultHeight=123, so the production init() in init.gno\n\t// (guarded on height==0) is a no-op in tests. Temporarily reset height so we\n\t// can whitelist this realm as a controller for testing.\n\ttesting.SetHeight(0)\n\tsusers.AddControllerAtGenesis(cross(cur), chain.PackageAddress(\"gno.land/r/sys/namereg/v1\"))\n\ttesting.SetHeight(123)\n}\n\nfunc TestRegister_Valid(cur realm, t *testing.T) {\n\t// Stems chosen with no `l` characters to keep canonical forms identical\n\t// to the originals — avoids accidental cross-subtest collisions with\n\t// other tests that may register l-bearing names.\n\tvalidUsernames := []string{\n\t\t\"nym-bravo123\",         // 5-char stem (minimum)\n\t\t\"nym-tango456\",         // 5-char stem\n\t\t\"nym-victor789\",        // 6-char stem\n\t\t\"nym-romeoecho012\",     // 10-char stem\n\t\t\"nym-mikefoxhote345\",   // 11-char stem\n\t\t\"nym-mikefoxhotenp678\", // 13-char stem (maximum)\n\t}\n\n\tfor _, username := range validUsernames {\n\t\taddr := testutils.TestAddress(username)\n\n\t\ttesting.SetRealm(testing.NewUserRealm(addr))\n\t\ttesting.SetOriginCaller(addr)\n\t\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tRegister(cross(cur), username)\n\t\t})\n\t}\n}\n\nfunc TestRegister_Free(cur realm, t *testing.T) {\n\toldPrice := registerPrice\n\tdefer func() { registerPrice = oldPrice }()\n\tregisterPrice = 0\n\n\taddr := testutils.TestAddress(\"free-payer\")\n\n\ttesting.SetRealm(testing.NewUserRealm(addr))\n\ttesting.SetOriginCaller(addr)\n\n\turequire.NotPanics(t, cur, func() {\n\t\tRegister(cross(cur), \"nym-foxtrot456\")\n\t})\n}\n\nfunc TestRegister_InvalidFormat(cur realm, t *testing.T) {\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"fmt-payer\")))\n\n\tcases := []string{\n\t\t\"\",                      // empty\n\t\t\"    \",                  // whitespace\n\t\t\"alice123\",              // missing nym- prefix\n\t\t\"usr-alice123\",          // wrong prefix\n\t\t\"nym-\",                  // prefix only\n\t\t\"nym-abc123\",            // stem too short (3 chars, need ≥5)\n\t\t\"nym-abcd123\",           // stem too short (4 chars)\n\t\t\"nym-abcdefghijklmn123\", // stem too long (14 chars, need ≤13)\n\t\t\"nym-Alice123\",          // uppercase in stem\n\t\t\"nym-al1ce123\",          // digit in stem\n\t\t\"nym-alice12\",           // only 2 trailing digits\n\t\t\"nym-alice1234\",         // 4 trailing digits (extra digit in stem too long)\n\t\t\"nym-alice\u0026#($)123\",     // special chars in stem\n\t}\n\n\tfor _, username := range cases {\n\t\tuassert.AbortsWithMessage(t, cur, ErrInvalidFormat.Error(), func() {\n\t\t\tRegister(cross(cur), username)\n\t\t})\n\t}\n}\n\nfunc TestRegister_ReservedPrefix(cur realm, t *testing.T) {\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"prefix-payer\")))\n\n\tcases := []string{\n\t\t\"nym-gnoblah123\",    // gno prefix\n\t\t\"nym-gnomeland456\",  // gno prefix\n\t\t\"nym-glasgow012\",    // gl prefix (most-confusable with bech32 g1...)\n\t\t\"nym-atomic123\",     // atom prefix\n\t\t\"nym-atonex123\",     // atone prefix\n\t\t\"nym-photons456\",    // photon prefix\n\t\t\"nym-cosmoswide789\", // cosmos prefix\n\t}\n\n\tfor _, username := range cases {\n\t\tuassert.AbortsWithMessage(t, cur, ErrReservedPrefix.Error(), func() {\n\t\t\tRegister(cross(cur), username)\n\t\t})\n\t}\n}\n\n// gi is intentionally NOT in reservedPrefixes (i is visually distinct\n// enough from 1/l that legitimate gi* names should be registerable).\n// Phishing protection for the gi/gl visual class is enforced by\n// canonical-collision detection in r/sys/users — see\n// TestRegister_CanonicalCollision.\nfunc TestRegister_GiPrefix_Allowed(cur realm, t *testing.T) {\n\taddr := testutils.TestAddress(\"gi-prefix-allowed\")\n\ttesting.SetRealm(testing.NewUserRealm(addr))\n\ttesting.SetOriginCaller(addr)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\n\turequire.NotPanics(t, cur, func() {\n\t\tRegister(cross(cur), \"nym-gillette789\")\n\t})\n}\n\nfunc TestRegister_Blacklisted(cur realm, t *testing.T) {\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"blk-payer\")))\n\n\tcases := []string{\n\t\t// `admin` is in reservedNames; with stem 5 chars it now matches the regex.\n\t\t\"nym-admin123\",\n\t\t// `support` is reserved (7 chars).\n\t\t\"nym-support456\",\n\t\t// `bitcoin` is reserved (7 chars).\n\t\t\"nym-bitcoin789\",\n\t\t// Implicit s-suffix expansion: `blog` is reserved, so `blogs` (stem\n\t\t// = blog+s) must also be rejected.\n\t\t\"nym-blogs012\",\n\t\t// Canonical match: `setting` is reserved → canonical \"setting\" (no l).\n\t\t// Try `setting` directly via 7-char stem.\n\t\t\"nym-setting345\",\n\t\t// Canonical match: `news` is reserved via `new+s` rule; canonical\n\t\t// of `news` is `news`. With a 5-char stem this won't reach here\n\t\t// (stem must be ≥5). Use a longer reserved name with l→i interaction:\n\t\t// `tendermint` (10 chars, no l) → canonical = tendermint → blacklisted.\n\t\t\"nym-tendermint678\",\n\t}\n\n\tfor _, username := range cases {\n\t\tuassert.AbortsWithMessage(t, cur, ErrBlacklisted.Error(), func() {\n\t\t\tRegister(cross(cur), username)\n\t\t})\n\t}\n}\n\nfunc TestRegister_CanonicalCollision(cur realm, t *testing.T) {\n\t// First register a name. Same-digits confusable variant must collide\n\t// in the unified susers canonical store.\n\taddr1 := testutils.TestAddress(\"collision-1\")\n\ttesting.SetRealm(testing.NewUserRealm(addr1))\n\ttesting.SetOriginCaller(addr1)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\turequire.NotPanics(t, cur, func() {\n\t\tRegister(cross(cur), \"nym-balloon123\")\n\t})\n\n\t// Then attempt to register a canonical-equivalent variant — same\n\t// canonical full name (after l→i, etc.). Must reject from susers.\n\taddr2 := testutils.TestAddress(\"collision-2\")\n\ttesting.SetRealm(testing.NewUserRealm(addr2))\n\ttesting.SetOriginCaller(addr2)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\tuassert.AbortsWithMessage(t, cur, susers.ErrCanonicalCollision.Error(), func() {\n\t\tRegister(cross(cur), \"nym-baiioon123\")\n\t})\n\n\t// Reverse direction also blocked.\n\taddr3 := testutils.TestAddress(\"collision-3\")\n\ttesting.SetRealm(testing.NewUserRealm(addr3))\n\ttesting.SetOriginCaller(addr3)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\turequire.NotPanics(t, cur, func() {\n\t\tRegister(cross(cur), \"nym-papaiio789\")\n\t})\n\n\taddr4 := testutils.TestAddress(\"collision-4\")\n\ttesting.SetRealm(testing.NewUserRealm(addr4))\n\ttesting.SetOriginCaller(addr4)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\tuassert.AbortsWithMessage(t, cur, susers.ErrCanonicalCollision.Error(), func() {\n\t\tRegister(cross(cur), \"nym-papallo789\")\n\t})\n}\n\n// Decision #2 in Option B: store key is full canonical username (not\n// stem). Same alpha stem with different digit suffixes coexists — does\n// not collide.\nfunc TestRegister_SameStemDifferentDigits_Coexist(cur realm, t *testing.T) {\n\taddrA := testutils.TestAddress(\"stem-coexist-A\")\n\ttesting.SetRealm(testing.NewUserRealm(addrA))\n\ttesting.SetOriginCaller(addrA)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\turequire.NotPanics(t, cur, func() {\n\t\tRegister(cross(cur), \"nym-foolbar000\")\n\t})\n\n\taddrB := testutils.TestAddress(\"stem-coexist-B\")\n\ttesting.SetRealm(testing.NewUserRealm(addrB))\n\ttesting.SetOriginCaller(addrB)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\turequire.NotPanics(t, cur, func() {\n\t\tRegister(cross(cur), \"nym-foolbar999\")\n\t})\n}\n\nfunc TestRegister_TakenUsername(cur realm, t *testing.T) {\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"taken-payer\")))\n\n\tusername := \"nym-zulutwo567\"\n\n\turequire.NotPanics(t, cur, func() {\n\t\tRegister(cross(cur), username)\n\t})\n\n\t// Re-registration of the same exact name is rejected by susers via\n\t// ErrNameTaken (the nameStore.Has check precedes the canonical check).\n\tuassert.AbortsWithMessage(t, cur, susers.ErrNameTaken.Error(), func() {\n\t\tRegister(cross(cur), username)\n\t})\n}\n\nfunc TestRegister_InvalidPayment(cur realm, t *testing.T) {\n\taddr := testutils.TestAddress(\"payment-payer\")\n\n\ttesting.SetRealm(testing.NewUserRealm(addr))\n\ttesting.SetOriginCaller(addr)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", 12))) // invalid\n\n\tuassert.AbortsContains(t, cur, ErrInvalidPayment.Error(), func() {\n\t\tRegister(cross(cur), \"nym-yankee345\")\n\t})\n}\n\n// Audit finding #11: the OriginSend() payment check is only trustworthy\n// when the direct caller is a pure EOA (IsUserCall). Any intermediate\n// code realm can attach -send to the tx, keep the coins, and call\n// Register; OriginSend() would still describe the envelope but namereg\n// would receive nothing. The EOA-only guard must reject all such callers.\nfunc TestRegister_IntermediateCodeRealmRejected(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/evil/wrapper\"))\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", registerPrice)))\n\n\tuassert.AbortsWithMessage(t, cur, ErrNonUserCall.Error(), func() {\n\t\tRegister(cross(cur), \"nym-whisky345\")\n\t})\n}\n\n// Audit finding #13: the OriginSend mismatch error must report the\n// CURRENT registerPrice, not the value frozen at package init time.\nfunc TestRegister_PaymentErrorReflectsCurrentPrice(cur realm, t *testing.T) {\n\toldPrice := registerPrice\n\tdefer func() { registerPrice = oldPrice }()\n\n\tregisterPrice = 5_000_000\n\n\taddr := testutils.TestAddress(\"price-payer\")\n\ttesting.SetRealm(testing.NewUserRealm(addr))\n\ttesting.SetOriginCaller(addr)\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", 1_000_000))) // wrong\n\n\tuassert.AbortsContains(t, cur, \"5000000\", func() {\n\t\tRegister(cross(cur), \"nym-xrayfox678\")\n\t})\n\n\tuassert.AbortsContains(t, cur, ErrInvalidPayment.Error(), func() {\n\t\tRegister(cross(cur), \"nym-xrayfox678\")\n\t})\n}\n\n// Audit finding #14: ProposeNewRegisterPrice originally rejected only\n// negative prices. A passed proposal to set a negative price would\n// have been arithmetically nonsense; reject below MinRegisterPrice\n// (currently 0) at proposal-creation time so governance can never\n// underflow the floor.\nfunc TestProposeNewRegisterPrice_floor(cur realm, t *testing.T) {\n\tt.Run(\"zero is accepted (free registration is the default)\", func(t *testing.T) {\n\t\turequire.NotPanics(t, cur, func() { ProposeNewRegisterPrice(cur, 0) })\n\t})\n\n\tt.Run(\"negative is rejected\", func(t *testing.T) {\n\t\turequire.PanicsWithMessage(t, cur,\n\t\t\t\"price below floor: -1 ugnot \u003c 0 ugnot (MinRegisterPrice)\",\n\t\t\tfunc() { ProposeNewRegisterPrice(cur, -1) })\n\t})\n\n\tt.Run(\"at floor is accepted\", func(t *testing.T) {\n\t\turequire.NotPanics(t, cur, func() { ProposeNewRegisterPrice(cur, MinRegisterPrice) })\n\t})\n\n\tt.Run(\"above floor is accepted\", func(t *testing.T) {\n\t\turequire.NotPanics(t, cur, func() { ProposeNewRegisterPrice(cur, 1_000_000_000) })\n\t})\n}\n"},{"name":"z_0_prop1_filetest.gno","body":"// PKGPATH: gno.land/r/sys/namereg/v1/filetests/z_0_prop1_filetest\n\npackage z_0_prop1_filetest\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gov/dao\"\n\tdaov3init \"gno.land/r/gov/dao/v3/init\"\n\tusers \"gno.land/r/sys/namereg/v1\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// Test updating a name via GovDAO\nvar c address = unsafe.OriginCaller()\n\nfunc init(cur realm) {\n\t// Whitelist this realm as a controller so its Register() can reach r/sys/users.\n\ttesting.SetHeight(0)\n\tsusers.AddControllerAtGenesis(cross(cur), chain.PackageAddress(\"gno.land/r/sys/namereg/v1\"))\n\ttesting.SetHeight(123)\n\n\tdaov3init.InitWithUsers(cross(cur), c)\n\n\talice := testutils.TestAddress(\"alice\")\n\n\t// Register alice\n\ttesting.SetOriginCaller(alice)\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tusers.Register(cross(cur), \"nym-alice123\")\n\n\t// Prop to change name\n\ttesting.SetOriginCaller(c)\n\ttesting.SetRealm(testing.NewUserRealm(c))\n\tpr := users.ProposeNewName(cross(cur), alice, \"alice_new123\")\n\tdao.MustCreateProposal(cross(cur), pr)\n}\n\nfunc main(cur realm) {\n\ttesting.SetOriginCaller(c)\n\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"\"))\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"0\"))\n\tprintln(\"--\")\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(0)))\n\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"0\"))\n\tprintln(\"--\")\n\n\tdao.ExecuteProposal(cross(cur), dao.ProposalID(0))\n\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"0\"))\n\n\tdata, _ := susers.ResolveName(\"alice_new123\")\n\tprintln(data.Addr())\n}\n\n// Output:\n// --\n// # GovDAO\n// ## Members\n// [\u003e Go to Memberstore \u003c](/r/gov/dao/v3/memberstore)\n// ## Proposals\n// ### [Prop #0 - User Registry V1: Rename user \\`nym\\-alice123\\` to \\`alice\\_new123\\`](/r/gov/dao:0)\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n// Status: ACTIVE\n//\n// Tiers eligible to vote: T1, T2, T3\n//\n// ---\n//\n//\n// --\n// ## Prop #0 - User Registry V1: Rename user \\`nym\\-alice123\\` to \\`alice\\_new123\\`\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n//\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **Proposal is open for votes**\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 0%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n// --\n// --\n// ## Prop #0 - User Registry V1: Rename user \\`nym\\-alice123\\` to \\`alice\\_new123\\`\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n//\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **Proposal is open for votes**\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 100%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n// --\n// --\n// ## Prop #0 - User Registry V1: Rename user \\`nym\\-alice123\\` to \\`alice\\_new123\\`\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n//\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **PROPOSAL HAS BEEN ACCEPTED**\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 100%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n// g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh\n"},{"name":"z_1_prop2_filetest.gno","body":"// PKGPATH: gno.land/r/sys/namereg/v1/filetests/z_1_prop2_filetest\n\npackage z_1_prop2_filetest\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gov/dao\"\n\tdaov3init \"gno.land/r/gov/dao/v3/init\"\n\tusers \"gno.land/r/sys/namereg/v1\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// Test updating a name via GovDAO\nvar c address = unsafe.OriginCaller()\n\nfunc init(cur realm) {\n\t// Whitelist this realm as a controller so its Register() can reach r/sys/users.\n\ttesting.SetHeight(0)\n\tsusers.AddControllerAtGenesis(cross(cur), chain.PackageAddress(\"gno.land/r/sys/namereg/v1\"))\n\ttesting.SetHeight(123)\n\n\tdaov3init.InitWithUsers(cross(cur), c)\n\n\talice := testutils.TestAddress(\"alice\")\n\n\t// Register alice\n\ttesting.SetOriginCaller(alice)\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tusers.Register(cross(cur), \"nym-alice123\")\n\n\t// Prop to delete user\n\ttesting.SetOriginCaller(c)\n\ttesting.SetRealm(testing.NewUserRealm(c))\n\tpr := users.ProposeDeleteUser(cross(cur), alice, \"delete user test\")\n\tdao.MustCreateProposal(cross(cur), pr)\n}\n\nfunc main(cur realm) {\n\ttesting.SetOriginCaller(c)\n\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"\"))\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"0\"))\n\tprintln(\"--\")\n\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(0)))\n\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"0\"))\n\tprintln(\"--\")\n\n\tdao.ExecuteProposal(cross(cur), dao.ProposalID(0))\n\n\tprintln(\"--\")\n\tprintln(dao.Render(cross(cur), \"0\"))\n\n\tdata, _ := susers.ResolveName(\"nym-alice123\")\n\tif data == nil {\n\t\tprintln(\"Successfully deleted alice\")\n\t}\n}\n\n// Output:\n// --\n// # GovDAO\n// ## Members\n// [\u003e Go to Memberstore \u003c](/r/gov/dao/v3/memberstore)\n// ## Proposals\n// ### [Prop #0 - User Registry V1: Delete user \\`nym\\-alice123\\`](/r/gov/dao:0)\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n// Status: ACTIVE\n//\n// Tiers eligible to vote: T1, T2, T3\n//\n// ---\n//\n//\n// --\n// ## Prop #0 - User Registry V1: Delete user \\`nym\\-alice123\\`\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n// delete user test\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **Proposal is open for votes**\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 0%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n// --\n// --\n// ## Prop #0 - User Registry V1: Delete user \\`nym\\-alice123\\`\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n// delete user test\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **Proposal is open for votes**\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 100%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n// --\n// --\n// ## Prop #0 - User Registry V1: Delete user \\`nym\\-alice123\\`\n// Author: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n//\n// delete user test\n//\n//\n//\n// ---\n//\n// ### Stats\n// - **PROPOSAL HAS BEEN ACCEPTED**\n// - Tiers eligible to vote: T1, T2, T3\n// - YES PERCENT: 100%\n// - NO PERCENT: 0%\n// - ABSTAIN PERCENT: 0%\n//\n// [Detailed voting list](/r/gov/dao:0/votes)\n//\n// ---\n//\n// ### Actions\n// [Vote YES](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=YES\u0026pid=0) | [Vote NO](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=NO\u0026pid=0) | [Vote ABSTAIN](/r/gov/dao$help\u0026func=MustVoteOnProposalSimple\u0026option=ABSTAIN\u0026pid=0)\n//\n// WARNING: Please double check transaction data before voting.\n// Successfully deleted alice\n"},{"name":"z_2_prop3_filetest.gno","body":"// PKGPATH: gno.land/r/sys/namereg/v1/filetests/z_2_prop3_filetest\n\npackage z_2_prop3_filetest\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gov/dao\"\n\tdaov3init \"gno.land/r/gov/dao/v3/init\"\n\tusers \"gno.land/r/sys/namereg/v1\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// Test the full governance-driven registerPrice flow:\n//  1. propose a new price via users.ProposeNewRegisterPrice\n//  2. DAO accepts the proposal (vote YES, execute)\n//  3. verify the executed callback updated registerPrice by exercising\n//     Register with the new amount and watching it succeed (registration\n//     under the old default of 0 ugnot would now panic with the new\n//     price in the error message).\nvar c address = unsafe.OriginCaller()\n\nfunc init(cur realm) {\n\t// Whitelist this realm as a controller so its Register() can reach r/sys/users.\n\ttesting.SetHeight(0)\n\tsusers.AddControllerAtGenesis(cross(cur), chain.PackageAddress(\"gno.land/r/sys/namereg/v1\"))\n\ttesting.SetHeight(123)\n\n\tdaov3init.InitWithUsers(cross(cur), c)\n\n\t// Propose raising the price from 0 to 20 GNOT.\n\ttesting.SetOriginCaller(c)\n\ttesting.SetRealm(testing.NewUserRealm(c))\n\tpr := users.ProposeNewRegisterPrice(cross(cur), 20_000_000)\n\tdao.MustCreateProposal(cross(cur), pr)\n}\n\nfunc main(cur realm) {\n\ttesting.SetOriginCaller(c)\n\n\t// Vote and execute.\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(0)))\n\tdao.ExecuteProposal(cross(cur), dao.ProposalID(0))\n\n\t// New price should be active. Register a user with the new amount;\n\t// registration with the old default (0 ugnot) would panic.\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetOriginCaller(alice)\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\ttesting.SetOriginSend(chain.Coins{{Denom: \"ugnot\", Amount: 20_000_000}})\n\tusers.Register(cross(cur), \"nym-alice123\")\n\n\tdata, _ := susers.ResolveName(\"nym-alice123\")\n\tprintln(\"registered alice at:\", data.Addr())\n}\n\n// Output:\n// registered alice at: g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh\n"},{"name":"z_3_prop4_filetest.gno","body":"// PKGPATH: gno.land/r/sys/namereg/v1/filetests/z_3_prop4_filetest\n\npackage z_3_prop4_filetest\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/r/gov/dao\"\n\tdaov3init \"gno.land/r/gov/dao/v3/init\"\n\tusers \"gno.land/r/sys/namereg/v1\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\n// Test the full governance-driven pause/unpause flow:\n//  1. propose pause via NewSetPausedExecutor(true), vote YES, execute,\n//     observe IsPaused() flip to true.\n//  2. propose unpause via NewSetPausedExecutor(false), vote YES, execute,\n//     observe IsPaused() flip back to false.\n//  3. Register works again post-unpause.\nvar c address = unsafe.OriginCaller()\n\nfunc init(cur realm) {\n\t// Whitelist this realm as a controller so its Register() can reach r/sys/users.\n\ttesting.SetHeight(0)\n\tsusers.AddControllerAtGenesis(cross(cur), chain.PackageAddress(\"gno.land/r/sys/namereg/v1\"))\n\ttesting.SetHeight(123)\n\n\tdaov3init.InitWithUsers(cross(cur), c)\n}\n\nfunc main(cur realm) {\n\ttesting.SetOriginCaller(c)\n\ttesting.SetRealm(testing.NewUserRealm(c))\n\n\tprintln(\"paused before:\", users.IsPaused())\n\n\t// Propose, vote, execute: pause.\n\tdao.MustCreateProposal(cross(cur), users.NewSetPausedExecutor(cross(cur), true))\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(0)))\n\tdao.ExecuteProposal(cross(cur), dao.ProposalID(0))\n\tprintln(\"paused after pause prop:\", users.IsPaused())\n\n\t// Propose, vote, execute: unpause.\n\tdao.MustCreateProposal(cross(cur), users.NewSetPausedExecutor(cross(cur), false))\n\tdao.MustVoteOnProposal(cross(cur), dao.NewVoteRequest(dao.YesVote, dao.ProposalID(1)))\n\tdao.ExecuteProposal(cross(cur), dao.ProposalID(1))\n\tprintln(\"paused after unpause prop:\", users.IsPaused())\n\n\t// Register works again (the pause check inside Register would have\n\t// panicked with ErrPaused had the unpause executor not run).\n\tusers.Register(cross(cur), \"nym-alice123\")\n\tdata, _ := susers.ResolveName(\"nym-alice123\")\n\tprintln(\"registered alice at:\", data.Addr())\n}\n\n// Output:\n// paused before: false\n// paused after pause prop: true\n// paused after unpause prop: false\n// registered alice at: g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"fmXAaCZHI+11NY6aG6JFK9168LEFBFs6tNJ0fINOgzKsC1/5U247g9+rRNAtJsiErxxq1rpycH+AwPImiYU2BA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"names","path":"gno.land/r/sys/names","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/names\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"render.gno","body":"package names\n\nfunc Render(_ string) string {\n\treturn `# r/sys/names\nSystem Realm for checking namespace deployment permissions.`\n}\n"},{"name":"verifier.gno","body":"// Package names enforces namespace permissions for package deployment.\n//\n// Two namespace shapes grant deploy authority when enforcement is enabled:\n//\n//  1. PA (personal-address) namespaces — gno.land/{r,p}/\u003caddr\u003e/* — the\n//     deployer's address string equals the namespace literal. Anyone can\n//     deploy under their own address.\n//\n//  2. Registered-name namespaces — gno.land/{r,p}/\u003cname\u003e/* — r/sys/users\n//     has a (name → addr) mapping where the resolved address equals the\n//     deployer AND the name is the user's CURRENT name (not a historical\n//     alias from a rename chain). This is the bridge that lets\n//     r/sys/namereg/v1 (or any other DAO-whitelisted controller) grant\n//     deploy authority via name registration.\n//\n// Authority is unscoped: a registered name owns BOTH r/\u003cname\u003e/* and\n// p/\u003cname\u003e/* paths. There is no sub-prefix isolation (e.g. r/u/\u003cname\u003e/*).\n//\n// The realm exposes an emergency-halt switch via SetPaused. When paused,\n// the verifier rejects EVERY namespace check — PA included — until\n// unpaused. This is the \"true emergency\" semantic; the narrow alternative\n// (pause registered-name only, preserve PA) was considered and rejected\n// because the threats most likely to justify pausing this realm\n// (verifier bug, compromised controller, signature-layer incident) do\n// not reliably exempt PA from the same blast radius.\npackage names\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/r/gov/dao\"\n\tgovimpl \"gno.land/r/gov/dao/v3/impl\"\n\tmemberstore \"gno.land/r/gov/dao/v3/memberstore\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\nvar (\n\t// admin is the GovDAO T1 multisig address, hardcoded at realm-source\n\t// commit time. Its only capability is gating Enable() — a one-way,\n\t// one-shot genesis activation of the namespace verifier. The address\n\t// has no other authority on this realm; pause/unpause is gated on a\n\t// separate GovDAO T1 proposal (see ProposeSetPaused), and there is\n\t// no SetEnabled(false) or SetAdmin path.\n\t//\n\t// Hardcoding is acceptable because:\n\t//   - Enable() is called once, at chain genesis. After that the\n\t//     address is dead weight — no further capability flows through it.\n\t//   - The narrow blast radius of \"stale admin\" is \"Enable() can never\n\t//     be called\", which leaves the verifier in pre-Enable bypass mode\n\t//     (returns true for all checks) — degraded but not exploitable.\n\t//   - A rotation path would only matter if Enable() needed to be\n\t//     re-issued. It doesn't; the flag is sticky.\n\t//\n\t// If the genesis activation pattern ever needs to change (e.g. a\n\t// SetEnabled(false) emergency disable is added later), this admin\n\t// model should be replaced with a GovDAO T1 proposal flow that\n\t// mirrors ProposeSetPaused. Until then, the hardcoded address is\n\t// the smallest viable governance surface for the one-shot use case.\n\tadmin   = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\tenabled = false\n\tpaused  = false\n)\n\n// nameLookupFn returns (addr, ok) for a given registered name. Allows\n// the verifier function to be unit-tested without wiring up r/sys/users\n// state — production binds resolveCurrentName as the lookup, tests pass\n// nil (PA-only) or a fake.\ntype nameLookupFn func(name string) (addr address, ok bool)\n\n// IsAuthorizedAddressForNamespace checks if the given address can deploy\n// to the given namespace. See package doc for the two authorization paths\n// and the pause semantic.\n//\n// Pre-Enable, all checks pass (testing/dev convenience).\nfunc IsAuthorizedAddressForNamespace(address_XXX address, namespace string) bool {\n\treturn verifier(enabled, paused, address_XXX, namespace, resolveCurrentName)\n}\n\n// resolveCurrentName is the production nameLookupFn, backed by r/sys/users.\n// Returns ok=true only if the name resolves to a non-deleted user AND the\n// queried name is that user's CURRENT name (the most recent UpdateName).\n//\n// Restricting to the current name has two consequences worth knowing:\n//\n//  1. After a UpdateName from \"alice\" to \"alice2\", the user keeps deploy\n//     authority over r/alice2/* but LOSES it for r/alice/*. Already-\n//     deployed packages at r/alice/* keep working — deploy-time\n//     authorization doesn't unwind the past — but no NEW deploys can\n//     land there.\n//\n//  2. The old name \"alice\" is also unregisterable by anyone else: when\n//     r/sys/users.UpdateName runs, it inserts the new name into nameStore\n//     but does not remove the old one. r/sys/users.RegisterUser then\n//     rejects re-registration of \"alice\" with ErrNameTaken. Net effect:\n//     a rename permanently removes the old name from circulation.\n//\n// The alternative (allow historical aliases to retain authority) was\n// rejected because it lets a single user register one cheap name, then\n// rename N times to claim authority over N distinct namespaces — a\n// stealth namespace acquisition vector worse than the current\n// burn-on-rename behavior.\nfunc resolveCurrentName(name string) (address, bool) {\n\tdata, isCurrent := susers.ResolveName(name)\n\tif data == nil || !isCurrent {\n\t\treturn \"\", false\n\t}\n\treturn data.Addr(), true\n}\n\n// Enable enables the namespace check for this realm.\n// The namespace check is disabled initially to ease txtar and other testing contexts,\n// but this function is meant to be called in the genesis of a chain.\nfunc Enable(cur realm) {\n\tif cur.Previous().Address() != admin {\n\t\tpanic(\"caller is not admin\")\n\t}\n\tenabled = true\n}\n\nfunc IsEnabled() bool {\n\treturn enabled\n}\n\n// ProposeSetPaused returns a GovDAO proposal request that, when voted\n// through and executed, toggles the chain-wide deploy gate. When the\n// realm is paused, the verifier rejects EVERY namespace check — PA\n// (personal-address) included — until a subsequent ProposeSetPaused(false)\n// proposal executes.\n//\n// This is an emergency halt. A paused state means NO new MsgAddPackage\n// transactions land at any path on the chain. Existing realms continue\n// to receive MsgCall traffic normally — pause is scoped to addpkg, not\n// to all VM operations. Use cases:\n//   - Bug discovered in this realm or r/sys/users that requires a\n//     hotfix before further deploys can be trusted.\n//   - Wallet/signature-layer incident under investigation.\n//\n// (A \"compromised controller\" use case was considered and removed: the\n// controller's RegisterUser path is direct into r/sys/users and does\n// NOT go through this verifier, so pause does not freeze new\n// registrations. To contain a compromised controller, the appropriate\n// flow is ProposeControllerRemoval in r/sys/users, not pause here.)\n//\n// The narrow alternative (pause registered-name path only, preserve\n// PA) was considered and rejected. See package doc for rationale.\n//\n// Gated on GovDAO proposal at T1 tier — not the hardcoded admin used\n// by Enable. Pause is consequential enough to warrant a tier-restricted\n// governance vote rather than a single-multisig click. T1 filter\n// prevents lower-tier members from spamming pause proposals to dilute\n// attention. Trade-off is response time: a T1 vote takes hours-to-days;\n// if a faster emergency-halt mechanism is needed, that belongs at a\n// different layer (e.g. an ante-handler-level chain pause), not here.\n//\n// Pause is orthogonal to the pre-Enable bypass: before Enable, the\n// verifier returns true regardless of paused state. So executing a\n// pause proposal before Enable has no effect on deploys, but the\n// value persists and applies the moment Enable runs. To avoid this\n// staging trap, operators should call Enable BEFORE any pause\n// proposals are voted in.\n//\n// Idempotency: calling ProposeSetPaused(v) when the realm's current\n// paused state already equals v panics at proposal-creation time so\n// voters never see a proposal whose execution would no-op.\nfunc ProposeSetPaused(cur realm, v bool) dao.ProposalRequest {\n\tif paused == v {\n\t\tpanic(\"paused state already matches requested value; no-op proposal rejected\")\n\t}\n\tcb := func(cur realm) error {\n\t\tsetPaused(0, cur, v)\n\t\treturn nil\n\t}\n\ttitle := \"Unpause Namespace Verifier\"\n\tdesc := \"This proposal unpauses `r/sys/names`. After execution, the namespace verifier will resume normal authorization checks (PA + registered-name paths). MsgCall traffic to existing realms is unaffected (pause was scoped to MsgAddPackage); only NEW package deploys gate on the unpaused state.\"\n\tif v {\n\t\ttitle = \"Pause Namespace Verifier\"\n\t\tdesc = \"This proposal pauses `r/sys/names`. After execution, the namespace verifier will reject EVERY new MsgAddPackage on the chain — PA (personal-address) and registered-name namespaces alike — until a subsequent unpause proposal executes. This is an emergency halt scoped to addpkg; MsgCall traffic to existing realms is unaffected.\"\n\t}\n\treturn dao.NewProposalRequestWithFilter(\n\t\ttitle,\n\t\tdesc,\n\t\tdao.NewSimpleExecutor(0, cur, cb, \"\"),\n\t\tgovimpl.NewFilterByTier(memberstore.T1),\n\t)\n}\n\n// setPaused is the private actuator behind ProposeSetPaused's executor\n// callback. It is unexported to ensure no path outside the proposal\n// flow can flip the flag — every state change goes through GovDAO.\n//\n// Emits NamespaceEnforcement{Paused,Unpaused} with the executor's\n// realm path for off-chain audit trails.\nfunc setPaused(_ int, rlm realm, v bool) {\n\tpaused = v\n\tif v {\n\t\tchain.Emit(\"NamespaceEnforcementPaused\", \"by\", rlm.Previous().PkgPath())\n\t} else {\n\t\tchain.Emit(\"NamespaceEnforcementUnpaused\", \"by\", rlm.Previous().PkgPath())\n\t}\n}\n\n// IsPaused reports the current value of the pause flag. Note: when\n// the realm is pre-Enable, IsPaused may return true but the verifier\n// will still pass-through (pre-Enable bypass takes priority).\nfunc IsPaused() bool {\n\treturn paused\n}\n\n// verifier checks namespace deployment permissions.\n// lookup is the registered-name resolver — pass nil to disable that path\n// (used by tests that want to exercise PA-only behavior).\n//\n// Order of checks (top to bottom, first matching wins):\n//  1. !isEnabled → return true   (pre-Enable: testing/dev bypass)\n//  2. isPaused   → return false  (emergency halt — INCLUDES PA)\n//  3. invalid input → return false\n//  4. PA match (addr.String() == namespace) → return true\n//  5. Registered-name lookup match → return true\n//  6. otherwise → return false\n//\n// The pause check is intentionally above the PA check. A SetPaused(true)\n// halts every deploy regardless of namespace shape.\nfunc verifier(isEnabled, isPaused bool, address_XXX address, namespace string, lookup nameLookupFn) bool {\n\tif !isEnabled {\n\t\treturn true // pre-genesis / dev convenience: bypass everything\n\t}\n\n\tif isPaused {\n\t\treturn false // emergency halt: reject every deploy including PA\n\t}\n\n\tif namespace == \"\" || !address_XXX.IsValid() {\n\t\treturn false\n\t}\n\n\t// Path 1: PA (personal-address) namespace.\n\t// gno.land/{p,r}/{ADDRESS}/**\n\tif address_XXX.String() == namespace {\n\t\treturn true\n\t}\n\n\t// Path 2: registered-name namespace via r/sys/users.\n\tif lookup != nil {\n\t\tif owner, ok := lookup(namespace); ok \u0026\u0026 owner == address_XXX {\n\t\t\treturn true\n\t\t}\n\t}\n\n\treturn false\n}\n"},{"name":"verifier_test.gno","body":"package names\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar (\n\talice = testutils.TestAddress(\"alice\")\n\tbob   = testutils.TestAddress(\"bob\")\n)\n\nfunc TestDefaultVerifier(t *testing.T) {\n\t// Disabled: any case is true (regardless of paused or lookup).\n\tuassert.True(t, verifier(false, false, alice, alice.String(), nil))\n\tuassert.True(t, verifier(false, false, \"\", alice.String(), nil))\n\tuassert.True(t, verifier(false, false, alice, \"somerandomusername\", nil))\n\n\t// Pre-Enable bypass takes priority over paused — even when paused=true,\n\t// !isEnabled returns true. This is the documented \"stage pause before\n\t// Enable\" behavior.\n\tuassert.True(t, verifier(false, true, alice, alice.String(), nil))\n\tuassert.True(t, verifier(false, true, alice, \"anything\", nil))\n\n\t// Enabled: PA namespace check.\n\tuassert.True(t, verifier(true, false, alice, alice.String(), nil))\n\n\t// Enabled: non-PA namespaces denied when no registered-name lookup.\n\tuassert.False(t, verifier(true, false, alice, \"notregistered\", nil))\n\tuassert.False(t, verifier(true, false, alice, \"alice\", nil))\n\n\t// Enabled: empty name/address.\n\tuassert.False(t, verifier(true, false, address(\"\"), \"\", nil))\n\tuassert.False(t, verifier(true, false, alice, \"\", nil))\n\tuassert.False(t, verifier(true, false, address(\"\"), \"something\", nil))\n}\n\n// TestRegisteredNameVerifier exercises the second authorization path —\n// the registered-name lookup. Uses a fake lookup so this test doesn't\n// depend on r/sys/users state.\nfunc TestRegisteredNameVerifier(t *testing.T) {\n\t// Lookup that says \"gnobody\" is registered to alice.\n\taliceOwnsGnobody := func(name string) (address, bool) {\n\t\tif name == \"gnobody\" {\n\t\t\treturn alice, true\n\t\t}\n\t\treturn \"\", false\n\t}\n\n\t// alice can deploy under \"gnobody\" (her registered name).\n\tuassert.True(t, verifier(true, false, alice, \"gnobody\", aliceOwnsGnobody))\n\n\t// bob cannot deploy under \"gnobody\" (registered to alice, not bob).\n\tuassert.False(t, verifier(true, false, bob, \"gnobody\", aliceOwnsGnobody))\n\n\t// Unknown name still rejects (lookup returns ok=false).\n\tuassert.False(t, verifier(true, false, alice, \"notregistered\", aliceOwnsGnobody))\n\n\t// PA path still works alongside registered-name lookup. Tried first.\n\tuassert.True(t, verifier(true, false, alice, alice.String(), aliceOwnsGnobody))\n\n\t// Disabled bypasses both paths entirely (returns true regardless).\n\tuassert.True(t, verifier(false, false, bob, \"gnobody\", aliceOwnsGnobody))\n\n\t// Empty namespace + non-nil lookup: still rejects (early-out before lookup).\n\tuassert.False(t, verifier(true, false, alice, \"\", aliceOwnsGnobody))\n\n\t// Lookup returns ok=true for current-but-mismatched address.\n\tbobOwnsGnobody := func(name string) (address, bool) {\n\t\tif name == \"gnobody\" {\n\t\t\treturn bob, true\n\t\t}\n\t\treturn \"\", false\n\t}\n\tuassert.False(t, verifier(true, false, alice, \"gnobody\", bobOwnsGnobody))\n}\n\n// TestPausedVerifier covers the emergency-halt semantic. When paused\n// AND enabled, the verifier rejects EVERY namespace check including PA.\nfunc TestPausedVerifier(t *testing.T) {\n\t// Lookup that says \"gnobody\" is registered to alice. Used to confirm\n\t// the registered-name path is also halted by pause.\n\taliceOwnsGnobody := func(name string) (address, bool) {\n\t\tif name == \"gnobody\" {\n\t\t\treturn alice, true\n\t\t}\n\t\treturn \"\", false\n\t}\n\n\tt.Run(\"paused blocks PA namespaces\", func(t *testing.T) {\n\t\t// PA would normally succeed (addr.String() == namespace), but\n\t\t// pause short-circuits before the PA check.\n\t\tuassert.False(t, verifier(true, true, alice, alice.String(), nil))\n\t\tuassert.False(t, verifier(true, true, alice, alice.String(), aliceOwnsGnobody))\n\t})\n\n\tt.Run(\"paused blocks registered-name namespaces\", func(t *testing.T) {\n\t\t// Registered-name path would normally succeed, but pause\n\t\t// short-circuits before the lookup is consulted.\n\t\tuassert.False(t, verifier(true, true, alice, \"gnobody\", aliceOwnsGnobody))\n\t})\n\n\tt.Run(\"paused blocks any other namespace too\", func(t *testing.T) {\n\t\tuassert.False(t, verifier(true, true, alice, \"anything\", aliceOwnsGnobody))\n\t\tuassert.False(t, verifier(true, true, alice, \"\", aliceOwnsGnobody))\n\t\tuassert.False(t, verifier(true, true, address(\"\"), \"anything\", aliceOwnsGnobody))\n\t})\n\n\tt.Run(\"pre-Enable bypass takes priority over paused\", func(t *testing.T) {\n\t\t// !enabled is checked BEFORE paused in the verifier — the\n\t\t// pre-Enable shortcut returns true regardless of pause state.\n\t\t// This is the \"pause is staged, applies on Enable\" semantic.\n\t\tuassert.True(t, verifier(false, true, alice, alice.String(), nil))\n\t\tuassert.True(t, verifier(false, true, alice, \"gnobody\", aliceOwnsGnobody))\n\t\tuassert.True(t, verifier(false, true, alice, \"anything\", nil))\n\t})\n\n\tt.Run(\"paused=false with enabled=true behaves normally\", func(t *testing.T) {\n\t\t// Sanity: just confirms the false case doesn't accidentally do\n\t\t// something different from the no-pause baseline.\n\t\tuassert.True(t, verifier(true, false, alice, alice.String(), nil))\n\t\tuassert.True(t, verifier(true, false, alice, \"gnobody\", aliceOwnsGnobody))\n\t\tuassert.False(t, verifier(true, false, bob, \"gnobody\", aliceOwnsGnobody))\n\t})\n}\n\n// TestProposeSetPaused_idempotency verifies the proposal-creation guard:\n// requesting a pause state that already matches the current state panics\n// at proposal-creation time, so voters never see a no-op proposal.\n//\n// NOTE: this test mutates the package-level `paused` var directly to\n// avoid driving the full DAO flow. Restores the value via defer.\nfunc TestProposeSetPaused_idempotency(cur realm, t *testing.T) {\n\tsaved := paused\n\tdefer func() { paused = saved }()\n\n\tt.Run(\"propose pause when already paused panics\", func(t *testing.T) {\n\t\tpaused = true\n\t\turequire.PanicsWithMessage(t, cur,\n\t\t\t\"paused state already matches requested value; no-op proposal rejected\",\n\t\t\tfunc() { ProposeSetPaused(cur, true) })\n\t})\n\n\tt.Run(\"propose unpause when already unpaused panics\", func(t *testing.T) {\n\t\tpaused = false\n\t\turequire.PanicsWithMessage(t, cur,\n\t\t\t\"paused state already matches requested value; no-op proposal rejected\",\n\t\t\tfunc() { ProposeSetPaused(cur, false) })\n\t})\n\n\tt.Run(\"propose pause when unpaused succeeds\", func(t *testing.T) {\n\t\tpaused = false\n\t\turequire.NotPanics(t, cur, func() { ProposeSetPaused(cur, true) })\n\t\t// The proposal-create call doesn't actually mutate paused —\n\t\t// only its execution would. Confirm.\n\t\tuassert.False(t, paused)\n\t})\n\n\tt.Run(\"propose unpause when paused succeeds\", func(t *testing.T) {\n\t\tpaused = true\n\t\turequire.NotPanics(t, cur, func() { ProposeSetPaused(cur, false) })\n\t\t// Same: proposal-create does not mutate.\n\t\tuassert.True(t, paused)\n\t})\n}\n\n// TestSetPaused_internal verifies the private setPaused actuator\n// directly. Bypasses the proposal flow to confirm the toggle and the\n// event-emission path independently.\nfunc TestSetPaused_internal(cur realm, t *testing.T) {\n\tsaved := paused\n\tdefer func() { paused = saved }()\n\n\tpaused = false\n\tsetPaused(0, cur, true)\n\tuassert.True(t, paused)\n\n\tsetPaused(0, cur, false)\n\tuassert.False(t, paused)\n}\n\nfunc TestIsPaused(t *testing.T) {\n\tsaved := paused\n\tdefer func() { paused = saved }()\n\n\tpaused = false\n\tuassert.False(t, IsPaused())\n\n\tpaused = true\n\tuassert.True(t, IsPaused())\n}\n\nfunc TestEnable(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"random\")))\n\tuassert.AbortsWithMessage(t, cur, \"caller is not admin\", func() {\n\t\tEnable(cross(cur))\n\t})\n\tuassert.False(t, IsEnabled())\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tuassert.NotPanics(t, cur, func() {\n\t\tEnable(cross(cur))\n\t})\n\tuassert.True(t, IsEnabled())\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"FVtw7tr7BvEImv8sFCY7E8Sj4wolgQzY1ayDM6EfquNAwPJn2HPZ3f8CkdeC5Dbxn4H7cYIzXF7bXvLHKVhdAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"rewards","path":"gno.land/r/sys/rewards","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/rewards\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"rewards.gno","body":"// This package will be used to manage proof-of-contributions on the exposed smart-contract side.\npackage rewards\n\n// TODO: write specs.\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"+6n7Dz58Isw8ZiHANnkSieQQ413y8jCdz57/hOUNDjMse6yfXc891v2IqNpaUuKmdx/meZnaacfpZu4cT1xfDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"txfees","path":"gno.land/r/sys/txfees","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/txfees\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"render.gno","body":"package txfees\n\nimport (\n\t\"chain/banker\"\n\t\"strings\"\n)\n\nfunc Render(cur realm, _ string) string {\n\tbanker_ := banker.NewReadonlyBanker()\n\trealmAddr := cur.Address()\n\tbalance := banker_.GetCoins(realmAddr).String()\n\n\tif strings.TrimSpace(balance) == \"\" {\n\t\tbalance = \"\\\\\u003cempty\\\\\u003e\"\n\t}\n\n\tvar output string\n\toutput += \"# Transaction Fees\\n\"\n\toutput += \"Balance: \" + balance + \"\\n\\n\"\n\n\toutput += \"Bucket address: \" + realmAddr.String() + \"\\n\"\n\treturn output\n}\n"},{"name":"txfees.gno","body":"package txfees\n\n// XXX: TODO distribution logic\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ZhseHdgBAXgmzDPWDgLta1LTVyonh52bb8mp2tktAFh1UF9Mbr4bKVMxsebYuQiNdgBEn+cqwJtnx7YgDANgBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"validators","path":"gno.land/r/sys/validators/v2","files":[{"name":"doc.gno","body":"// Package validators implements the on-chain validator set management through Proof of Contribution.\n// The Realm exposes only a public executor for govdao proposals, that can suggest validator set changes.\npackage validators\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sys/validators/v2\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"},{"name":"gnosdk.gno","body":"package validators\n\nimport (\n\t\"math\"\n\n\t\"gno.land/p/sys/validators\"\n)\n\n// GetChanges returns the validator changes stored on the realm,\n// for blocks in the [from, to] range (inclusive on both ends).\n// If to \u003e= math.MaxInt64, it is clamped to math.MaxInt64-1 to avoid overflow.\n// Panics if from \u003e to (after clamping).\n// This function is intended to be called by gno.land through the GnoSDK.\nfunc GetChanges(from, to int64) []validators.Validator {\n\tif to \u003e math.MaxInt64-1 {\n\t\tto = math.MaxInt64 - 1\n\t}\n\tif to \u003c from {\n\t\tpanic(\"invalid range: from must be \u003c= to\")\n\t}\n\n\tvalsetChanges := make([]validators.Validator, 0)\n\n\t// Gather the changes in the [from, to] block range.\n\t// AVL Iterate uses an exclusive end, so we pass to+1.\n\tchanges.Iterate(getBlockID(from), getBlockID(to+1), func(_ string, value any) bool {\n\t\tchs := value.([]change)\n\n\t\tfor _, ch := range chs {\n\t\t\tvalsetChanges = append(valsetChanges, ch.validator)\n\t\t}\n\n\t\treturn false\n\t})\n\n\treturn valsetChanges\n}\n"},{"name":"init.gno","body":"package validators\n\nimport (\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/poa/v0\"\n)\n\nfunc init() {\n\t// The default valset protocol is PoA\n\tvp = poa.NewPoA()\n\n\t// No changes to apply initially\n\tchanges = bptree.NewBPTree32()\n}\n"},{"name":"poc.gno","body":"package validators\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sys/validators\"\n\t\"gno.land/r/gov/dao\"\n)\n\n// NewPropRequest creates a new proposal request that wraps a changes closure\n// proposal. This wrapper is required to ensure the GovDAO Realm actually\n// executed the callback.\nfunc NewPropRequest(cur realm, changesFn func() []validators.Validator, title, description string) dao.ProposalRequest {\n\tif changesFn == nil {\n\t\tpanic(\"no set changes proposed\")\n\t}\n\n\ttitle = strings.TrimSpace(title)\n\tif title == \"\" {\n\t\tpanic(\"proposal title is empty\")\n\t}\n\n\t// Get the list of validators now to make sure the list\n\t// doesn't change during the lifetime of the proposal\n\tchanges := changesFn()\n\n\t// Limit the number of validators to keep the description within a limit\n\t// that makes sense because there is not pagination of validators\n\tif len(changes) \u003e 40 {\n\t\tpanic(\"max number of allowed validators per proposal is 40\")\n\t} else if len(changes) == 0 {\n\t\tpanic(\"proposal requires at least one validator\")\n\t}\n\n\t// List the validator addresses and the action to be taken for each one\n\tvar desc strings.Builder\n\tdesc.WriteString(description)\n\tif len(description) \u003e 0 {\n\t\tdesc.WriteString(\"\\n\\n\")\n\t}\n\n\tdesc.WriteString(\"## Validator Updates\\n\")\n\tfor _, change := range changes {\n\t\tif change.VotingPower == 0 {\n\t\t\tdesc.WriteString(ufmt.Sprintf(\"- %s: remove\\n\", change.Address))\n\t\t} else {\n\t\t\tdesc.WriteString(ufmt.Sprintf(\"- %s: add\\n\", change.Address))\n\t\t}\n\t}\n\n\tcallback := func(cur realm) error {\n\t\tfor _, change := range changes {\n\t\t\tif change.VotingPower == 0 {\n\t\t\t\t// This change request is to remove the validator\n\t\t\t\tremoveValidator(change.Address)\n\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t// This change request is to add the validator\n\t\t\taddValidator(change)\n\t\t}\n\n\t\treturn nil\n\t}\n\n\te := dao.NewSimpleExecutor(0, cur, callback, \"\")\n\n\treturn dao.NewProposalRequest(title, desc.String(), e)\n}\n\n// IsValidator returns a flag indicating if the given bech32 address\n// is part of the validator set\nfunc IsValidator(addr address) bool {\n\treturn vp.IsValidator(addr)\n}\n\n// GetValidator returns the typed validator\nfunc GetValidator(addr address) validators.Validator {\n\tif validator, err := vp.GetValidator(addr); err == nil {\n\t\treturn validator\n\t}\n\n\tpanic(\"validator not found\")\n}\n\n// GetValidators returns the typed validator set\nfunc GetValidators() []validators.Validator {\n\treturn vp.GetValidators()\n}\n"},{"name":"validators.gno","body":"package validators\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sys/validators\"\n)\n\nvar (\n\tvp      validators.ValsetProtocol // p is the underlying validator set protocol\n\tchanges *bptree.BPTree            // changes holds any valset changes; seqid(block number) -\u003e []change\n)\n\n// change represents a single valset change, tied to a specific block number\ntype change struct {\n\tblockNum  int64                // the block number associated with the valset change\n\tvalidator validators.Validator // the validator update\n}\n\n// addValidator adds a new validator to the validator set.\n// If the validator is already present, the method errors out\nfunc addValidator(validator validators.Validator) {\n\tval, err := vp.AddValidator(validator.Address, validator.PubKey, validator.VotingPower)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Validator added, note the change\n\tch := change{\n\t\tblockNum:  runtime.ChainHeight(),\n\t\tvalidator: val,\n\t}\n\n\tsaveChange(ch)\n\n\t// Emit the validator set change\n\tchain.Emit(validators.ValidatorAddedEvent)\n}\n\n// removeValidator removes the given validator from the set.\n// If the validator is not present in the set, the method errors out\nfunc removeValidator(address_XXX address) {\n\tval, err := vp.RemoveValidator(address_XXX)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Validator removed, note the change\n\tch := change{\n\t\tblockNum: runtime.ChainHeight(),\n\t\tvalidator: validators.Validator{\n\t\t\tAddress:     val.Address,\n\t\t\tPubKey:      val.PubKey,\n\t\t\tVotingPower: 0, // nullified the voting power indicates removal\n\t\t},\n\t}\n\n\tsaveChange(ch)\n\n\t// Emit the validator set change\n\tchain.Emit(validators.ValidatorRemovedEvent)\n}\n\n// saveChange saves the valset change\nfunc saveChange(ch change) {\n\tid := getBlockID(ch.blockNum)\n\n\tsetRaw := changes.Get(id)\n\tif setRaw == nil {\n\t\tchanges.Set(id, []change{ch})\n\n\t\treturn\n\t}\n\n\t// Save the change\n\tset := setRaw.([]change)\n\tset = append(set, ch)\n\n\tchanges.Set(id, set)\n}\n\n// getBlockID converts the block number to a sequential ID\nfunc getBlockID(blockNum int64) string {\n\treturn seqid.ID(uint64(blockNum)).String()\n}\n\nfunc Render(_ string) string {\n\tvar (\n\t\tsize       = changes.Size()\n\t\tmaxDisplay = 10\n\t)\n\n\tif size == 0 {\n\t\treturn \"No valset changes to apply.\"\n\t}\n\n\toutput := \"Valset changes:\\n\"\n\tchanges.ReverseIterateByOffset(0, maxDisplay, func(_ string, value any) bool {\n\t\tchs := value.([]change)\n\n\t\tfor _, ch := range chs {\n\t\t\toutput += ufmt.Sprintf(\n\t\t\t\t\"- #%d: %s (%d)\\n\",\n\t\t\t\tch.blockNum,\n\t\t\t\tch.validator.Address.String(),\n\t\t\t\tch.validator.VotingPower,\n\t\t\t)\n\t\t}\n\n\t\treturn false\n\t})\n\n\treturn output\n}\n"},{"name":"validators_test.gno","body":"package validators\n\nimport (\n\t\"chain/runtime\"\n\t\"math\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/poa/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sys/validators\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\n// generateTestValidators generates a dummy validator set\nfunc generateTestValidators(count int) []validators.Validator {\n\tvals := make([]validators.Validator, 0, count)\n\n\tfor i := 0; i \u003c count; i++ {\n\t\tval := validators.Validator{\n\t\t\tAddress:     testutils.TestAddress(ufmt.Sprintf(\"%d\", i)),\n\t\t\tPubKey:      \"public-key\",\n\t\t\tVotingPower: 10,\n\t\t}\n\n\t\tvals = append(vals, val)\n\t}\n\n\treturn vals\n}\n\nfunc TestValidators_AddRemove(t *testing.T) {\n\t// Clear any changes\n\tchanges = bptree.NewBPTree32()\n\n\tvar (\n\t\tvals          = generateTestValidators(100)\n\t\tinitialHeight = int64(123)\n\t)\n\n\t// Add in the validators\n\tfor _, val := range vals {\n\t\taddValidator(val)\n\n\t\t// Make sure the validator is added\n\t\tuassert.True(t, vp.IsValidator(val.Address))\n\n\t\ttesting.SkipHeights(1)\n\t}\n\n\tfor i := initialHeight; i \u003c initialHeight+int64(len(vals)); i++ {\n\t\t// Make sure the changes are saved\n\t\tchs := GetChanges(i, initialHeight+int64(len(vals)))\n\n\t\t// We use the funky index calculation to make sure\n\t\t// changes are properly handled for each block span\n\t\tuassert.Equal(t, initialHeight+int64(len(vals))-i, int64(len(chs)))\n\n\t\tfor index, val := range vals[i-initialHeight:] {\n\t\t\t// Make sure the changes are equal to the additions\n\t\t\tch := chs[index]\n\n\t\t\tuassert.Equal(t, val.Address, ch.Address)\n\t\t\tuassert.Equal(t, val.PubKey, ch.PubKey)\n\t\t\tuassert.Equal(t, val.VotingPower, ch.VotingPower)\n\t\t}\n\t}\n\n\t// Save the beginning height for the removal\n\tinitialRemoveHeight := runtime.ChainHeight()\n\n\t// Clear any changes\n\tchanges = bptree.NewBPTree32()\n\n\t// Remove the validators\n\tfor _, val := range vals {\n\t\tremoveValidator(val.Address)\n\n\t\t// Make sure the validator is removed\n\t\tuassert.False(t, vp.IsValidator(val.Address))\n\n\t\ttesting.SkipHeights(1)\n\t}\n\n\tfor i := initialRemoveHeight; i \u003c initialRemoveHeight+int64(len(vals)); i++ {\n\t\t// Make sure the changes are saved\n\t\tchs := GetChanges(i, initialRemoveHeight+int64(len(vals)))\n\n\t\t// We use the funky index calculation to make sure\n\t\t// changes are properly handled for each block span\n\t\tuassert.Equal(t, initialRemoveHeight+int64(len(vals))-i, int64(len(chs)))\n\n\t\tfor index, val := range vals[i-initialRemoveHeight:] {\n\t\t\t// Make sure the changes are equal to the additions\n\t\t\tch := chs[index]\n\n\t\t\tuassert.Equal(t, val.Address, ch.Address)\n\t\t\tuassert.Equal(t, val.PubKey, ch.PubKey)\n\t\t\tuassert.Equal(t, uint64(0), ch.VotingPower)\n\t\t}\n\t}\n}\n\n// TestGetChanges_BoundedRange verifies that GetChanges(from, to) correctly\n// returns only changes within the [from, to] block range.\nfunc TestGetChanges_BoundedRange(t *testing.T) {\n\tchanges = bptree.NewBPTree32()\n\tvp = poa.NewPoA()\n\n\tvals := generateTestValidators(3)\n\n\t// Store additions at block h1\n\th1 := runtime.ChainHeight()\n\tfor _, val := range vals {\n\t\taddValidator(val)\n\t}\n\ttesting.SkipHeights(1)\n\n\t// Store removals at block h2\n\th2 := runtime.ChainHeight()\n\tfor _, val := range vals {\n\t\tremoveValidator(val.Address)\n\t}\n\ttesting.SkipHeights(1)\n\n\t// Query spanning both blocks returns all changes\n\tall := GetChanges(h1, h2)\n\tuassert.Equal(t, 6, len(all))\n\n\t// Query for h1 only returns additions\n\tatH1 := GetChanges(h1, h1)\n\tuassert.Equal(t, 3, len(atH1))\n\tfor i, ch := range atH1 {\n\t\tuassert.Equal(t, vals[i].Address, ch.Address)\n\t\tuassert.True(t, ch.VotingPower \u003e 0)\n\t}\n\n\t// Query for h2 only returns removals\n\tatH2 := GetChanges(h2, h2)\n\tuassert.Equal(t, 3, len(atH2))\n\tfor i, ch := range atH2 {\n\t\tuassert.Equal(t, vals[i].Address, ch.Address)\n\t\tuassert.Equal(t, uint64(0), ch.VotingPower)\n\t}\n\n\t// Query beyond stored range returns empty\n\tuassert.Equal(t, 0, len(GetChanges(h2+1, h2+1)))\n}\n\n// TestRender_ShowsNewestWhenOverLimit verifies that Render displays the newest\n// maxDisplay buckets when there are more change buckets than maxDisplay.\nfunc TestRender_ShowsNewestWhenOverLimit(t *testing.T) {\n\tchanges = bptree.NewBPTree32()\n\tvp = poa.NewPoA()\n\n\tconst total = 13\n\tconst maxDisplay = 10\n\n\tvals := generateTestValidators(total)\n\tbase := runtime.ChainHeight()\n\n\tfor i := 0; i \u003c total; i++ {\n\t\th := base + int64(i)\n\t\tchanges.Set(getBlockID(h), []change{\n\t\t\t{blockNum: h, validator: vals[i]},\n\t\t})\n\t}\n\n\toutput := Render(\"\")\n\n\t// Newest maxDisplay buckets must appear.\n\tfor i := total - maxDisplay; i \u003c total; i++ {\n\t\th := base + int64(i)\n\t\tuassert.True(t, strings.Contains(output, ufmt.Sprintf(\"#%d:\", h)),\n\t\t\tufmt.Sprintf(\"expected block #%d in output\", h))\n\t}\n\n\t// Oldest (total - maxDisplay) buckets must NOT appear.\n\tfor i := 0; i \u003c total-maxDisplay; i++ {\n\t\th := base + int64(i)\n\t\tuassert.False(t, strings.Contains(output, ufmt.Sprintf(\"#%d:\", h)),\n\t\t\tufmt.Sprintf(\"block #%d should be absent from output\", h))\n\t}\n}\n\n// TestRender_ShowsAllWhenUnderLimit verifies that all buckets are displayed when\n// there are fewer than maxDisplay, and that no panic occurs from a negative offset.\nfunc TestRender_ShowsAllWhenUnderLimit(t *testing.T) {\n\tchanges = bptree.NewBPTree32()\n\tvp = poa.NewPoA()\n\n\tconst total = 5 // less than maxDisplay=10\n\n\tvals := generateTestValidators(total)\n\tbase := runtime.ChainHeight()\n\n\tfor i := 0; i \u003c total; i++ {\n\t\th := base + int64(i)\n\t\tchanges.Set(getBlockID(h), []change{\n\t\t\t{blockNum: h, validator: vals[i]},\n\t\t})\n\t}\n\n\toutput := Render(\"\")\n\n\tfor i := 0; i \u003c total; i++ {\n\t\th := base + int64(i)\n\t\tuassert.True(t, strings.Contains(output, ufmt.Sprintf(\"#%d:\", h)),\n\t\t\tufmt.Sprintf(\"expected block #%d in output\", h))\n\t}\n}\n\nfunc TestGetChanges_PanicsOnInvalidRange(cur realm, t *testing.T) {\n\tuassert.PanicsWithMessage(t, cur, \"invalid range: from must be \u003c= to\", func() {\n\t\tGetChanges(10, 5)\n\t})\n}\n\nfunc TestGetChanges_ClampsMaxInt64(t *testing.T) {\n\tchanges = bptree.NewBPTree32()\n\n\tvals := generateTestValidators(1)\n\n\t// Simulate a validator change at block math.MaxInt64-1 (the boundary value).\n\tchanges.Set(getBlockID(math.MaxInt64-1), []change{\n\t\t{blockNum: math.MaxInt64 - 1, validator: vals[0]},\n\t})\n\n\t// Passing math.MaxInt64 as \"to\" means \"get all updates from here onwards\".\n\t// The clamp (to = MaxInt64-1) must still include the boundary block.\n\tresult := GetChanges(math.MaxInt64-1, math.MaxInt64)\n\tuassert.Equal(t, 1, len(result))\n\tuassert.Equal(t, vals[0].Address, result[0].Address)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"rXeKNK5dzrM2IVbbDJKElRjupoL+tmVqZBDUc/aNdxxjBAQKsaedXCpOfEpH1uTd0zHZzXm9nEw9Iun+R84fDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"issue5736_common","path":"gno.land/r/tests/issue5736_common","files":[{"name":"common.gno","body":"// Package issue5736_common is a regression fixture for\n// https://github.com/gnolang/gno/issues/5736.\n//\n// It mimics the gnoswap pattern that surfaced the bug: a non-crossing\n// helper in an /r/ realm that performs in-place uint256 arithmetic on\n// a value handed in from another /r/ realm. Before the fix to\n// {Array,Struct}Value.Copy, this triggered:\n//\n//\tpanic: cannot directly modify readonly tainted object\n//\t  gno.land/p/onbloc/uint256/bitwise.gno (z.arr[0] = z.arr[0] \u003c\u003c n)\npackage issue5736_common\n\nimport (\n\tu256 \"gno.land/p/onbloc/uint256\"\n)\n\nfunc DoLsh(x *u256.Uint, n uint) *u256.Uint {\n\treturn u256.Zero().Lsh(x, n)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/issue5736_common\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"n+LjYD1H/GpoMiEnP3oNGJiXJ0KJfNcJiWGXwQhSjd1KKBozNf35F51wMGmW2VRlAi8ls0KlsOu5ppIuTNkCDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"issue5736_bar","path":"gno.land/r/tests/issue5736_bar","files":[{"name":"bar.gno","body":"// Package issue5736_bar is the caller-side fixture for\n// https://github.com/gnolang/gno/issues/5736. See the sibling\n// gno.land/r/tests/issue5736_common package for context.\npackage issue5736_bar\n\nimport (\n\tu256 \"gno.land/p/onbloc/uint256\"\n\t\"gno.land/r/tests/issue5736_common\"\n)\n\nfunc Call(cur realm) string {\n\tout := issue5736_common.DoLsh(u256.NewUint(123), 1)\n\treturn out.Dec()\n}\n"},{"name":"bar_test.gno","body":"package issue5736_bar\n\nimport (\n\t\"testing\"\n)\n\nfunc TestCall(cur realm, t *testing.T) {\n\tgot := Call(cross(cur))\n\tif got != \"246\" {\n\t\tt.Fatalf(\"got %s, want 246\", got)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/issue5736_bar\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Q1rgoq41h2dOKAOxnkNLNLC3c6k2C9IznLb8fvyE8SWwdw/WuWaMnnJMX7dXd95nfF3Ul4yivgKLD7P6qtvFAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"crossrealm","path":"gno.land/r/tests/vm/crossrealm","files":[{"name":"crossrealm.gno","body":"package crossrealm\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/demo/tests/p_crossrealm\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype LocalStruct struct {\n\tA int\n}\n\nfunc (ls *LocalStruct) String() string {\n\treturn ufmt.Sprintf(\"LocalStruct{%d}\", ls.A)\n}\n\n// local is saved locally in this realm\nvar local *LocalStruct\n\nfunc init() {\n\tlocal = \u0026LocalStruct{A: 123}\n}\n\n// Make1 returns a local object wrapped by a p struct\nfunc Make1() *p_crossrealm.Container {\n\treturn \u0026p_crossrealm.Container{\n\t\tA: 1,\n\t\tB: local,\n\t}\n}\n\ntype Fooer interface {\n\tFoo(realm)\n\tBar()\n}\n\nvar fooer Fooer\n\nfunc SetFooer(cur realm, f Fooer) Fooer {\n\tfooer = f\n\treturn fooer\n}\n\nfunc GetFooer() Fooer {\n\treturn fooer\n}\n\nfunc CallFooerFooCur(cur realm) {\n\tfooer.Foo(cur)\n}\n\nfunc CallFooerFooCross(cur realm) {\n\tfooer.Foo(cross(cur))\n}\n\nfunc CallFooerBar() {\n\tfooer.Bar()\n}\n\nfunc CallFooerBarCrossing(cur realm) {\n\tfooer.Bar()\n}\n\ntype FooerGetter func() Fooer\n\nvar fooerGetter FooerGetter\n\nfunc SetFooerGetter(cur realm, fg FooerGetter) FooerGetter {\n\tfooerGetter = fg\n\treturn fg\n}\n\nfunc GetFooerGetter() FooerGetter {\n\treturn fooerGetter\n}\n\nfunc CallFooerGetterBar() {\n\tfooerGetter().Bar()\n}\n\nfunc CallFooerGetterBarCrossing(cur realm) {\n\tfooerGetter().Bar()\n}\n\nfunc CallFooerGetterFooCur(cur realm) {\n\tfooerGetter().Foo(cur)\n}\n\nfunc CallFooerGetterFooCross(cur realm) {\n\tfooerGetter().Foo(cross(cur))\n}\n\n// This is a top function that does switch realms.\nfunc ExecCrossing(cur realm, cb func() string) string {\n\treturn cb()\n}\n\n// This is a top function that doesn't switch realms.\nfunc Exec(cb func() string) string {\n\treturn cb()\n}\n\n// ------------------------------------\n// SECURITY XXX: Closure and Closure2 below are exported package-level\n// function-typed vars that any foreign realm can set via SetClosure /\n// SetClosure2 and trigger via ExecuteClosure / ExecuteClosureCross.\n// Closure2's signature is `func(realm)`, which means whoever sets it\n// receives a realm value at invocation time — capability-bearing data\n// stored in an exported package var, then handed back to caller-supplied\n// code. THIS IS DELIBERATE VM-PARITY-TEST INFRASTRUCTURE — these vars\n// exist to exercise the VM's handling of stored closures and cross-\n// realm callbacks. DO NOT COPY THIS PATTERN IN PRODUCTION CODE. Real\n// /r/ realms must never expose `func(realm)` (or any function-typed\n// value capable of receiving cur) as an exported package var.\nvar Closure func()\n\nfunc SetClosure(cur realm, f func()) {\n\tClosure = f\n}\n\nfunc ExecuteClosure(cur realm) {\n\tClosure()\n}\n\nvar Closure2 func(realm)\n\nfunc SetClosure2(cur realm, f func(realm)) {\n\tClosure2 = f\n}\nfunc ExecuteClosureCross(cur realm) {\n\tClosure2(cross(cur))\n}\n\n// Closure3 mirrors Closure but for non-crossing-with-rlm closures —\n// `func(_ int, rlm realm)`. Whoever sets Closure3 receives cur as a\n// plain value (no realm boundary at invocation time), then can cross\n// internally via cross(rlm).\nvar Closure3 func(_ int, rlm realm)\n\nfunc SetClosure3(cur realm, f func(_ int, rlm realm)) {\n\tClosure3 = f\n}\n\nfunc ExecuteClosure3(cur realm) {\n\tClosure3(0, cur)\n}\n\n// Closure -\u003e FooUpdate\nfunc PrintRealms(cur realm) {\n\tufmt.Printf(\"current realm: %s\\n\", unsafe.CurrentRealm())\n\tufmt.Printf(\"previous realm: %s\\n\", unsafe.PreviousRealm())\n}\n\n// -------------------------------------------------\nvar Object any\n\nfunc SetObject(cur realm, x any) {\n\tObject = x\n}\n\nfunc GetObject() any {\n\treturn Object\n}\n\nfunc EntryPoint() (noCros *ownable.Ownable) {\n\tprintln(\"crossrealm  EntryPoint: \" + unsafe.PreviousRealm().PkgPath())\n\tprintln(\"crossrealm  EntryPoint: \" + unsafe.PreviousRealm().Address())\n\tprintln()\n\treturn PrevRealmNoCrossing()\n}\n\n// EntryPointWithCrossing is a non-crossing helper that forwards into\n// the (cur realm)-crossing PrevRealmCrossing. Callers pass their own\n// live cur; `cross(rlm)` does the actual cross.\nfunc EntryPointWithCrossing(_ int, rlm realm) (withCros *ownable.Ownable) {\n\treturn PrevRealmCrossing(cross(rlm))\n}\n\nfunc PrevRealmNoCrossing() *ownable.Ownable {\n\tprintln(\"crossrealm PreviousRealm no crossing: \" + unsafe.PreviousRealm().PkgPath())\n\tprintln(\"crossrealm PreviousRealm no crossing: \" + unsafe.PreviousRealm().Address())\n\treturn ownable.NewWithAddress(unsafe.CurrentRealm().Address())\n}\n\nfunc PrevRealmCrossing(cur realm) *ownable.Ownable {\n\tprintln(\"crossrealm PreviousRealm with crossing: \" + unsafe.PreviousRealm().PkgPath())\n\tprintln(\"crossrealm PreviousRealm with crossing: \" + unsafe.PreviousRealm().Address())\n\treturn ownable.NewWithAddress(cur.Address())\n}\n\nfunc CurRealmNoCrossing() runtime.Realm {\n\treturn unsafe.CurrentRealm()\n}\nfunc CurRealmCrossing(cur realm) runtime.Realm {\n\treturn unsafe.CurrentRealm()\n}\n\n// call the package that returns current realm\nfunc PkgCurRealmNoCrossing() runtime.Realm {\n\treturn p_crossrealm.CurrentRealm()\n}\n\n// call the package that returns current realm\nfunc PkgCurRealmCrossing(cur realm) runtime.Realm {\n\treturn unsafe.CurrentRealm()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/crossrealm\"\ngno = \"0.9\"\n"},{"name":"switchrealm.gno","body":"package crossrealm\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"5uKzOyMffCO+/gWnPUCZfBVgzySEmgTCVnDGkP671VpulFXK0reqSkBKsdbUPK86XlEZ2Y0jCG2SMQb2+ajJCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"crossrealm_b","path":"gno.land/r/tests/vm/crossrealm_b","files":[{"name":"crossrealm.gno","body":"package crossrealm_b\n\nimport (\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/r/tests/vm/crossrealm\"\n)\n\ntype fooer struct {\n\ts string\n}\n\nfunc (f *fooer) SetS(newVal string) {\n\tf.s = newVal\n}\n\nfunc (f *fooer) Foo(cur realm) {\n\tprintln(\"hello \" + f.s + \" cur=\" + unsafe.CurrentRealm().PkgPath() + \" prev=\" + unsafe.PreviousRealm().PkgPath())\n}\n\nfunc (f *fooer) Bar() {\n\tprintln(\"hello \" + f.s + \" cur=\" + unsafe.CurrentRealm().PkgPath() + \" prev=\" + unsafe.PreviousRealm().PkgPath())\n}\n\nvar (\n\tFooer              = \u0026fooer{s: \"A\"}\n\tFooerGetter        = func() crossrealm.Fooer { return Fooer }\n\tFooerGetterBuilder = func() crossrealm.FooerGetter { return func() crossrealm.Fooer { return Fooer } }\n)\n\nvar Closure func()\n\nfunc SetClosure(cur realm, f func()) {\n\tClosure = f\n}\n\nvar Object any\n\nfunc SetObject(cur realm, x any) {\n\tObject = x\n}\n\nfunc GetObject() any {\n\treturn Object\n}\n\nfunc IncrementObject(cur realm) any {\n\tptr := Object.(*int)\n\t*ptr += 1\n\treturn Object\n}\n\nvar n int\n\n// NOTE should be non-crossing\nfunc IncrGlobal() {\n\tn++\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/crossrealm_b\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"nxdeBiXPRt3CIonrZ771/RdGMf2q+yTbWwL08H/r1ppcS0jhoJ0B8O0PQS6ftHnLiMT3P4tLC3cTRgAaIrbVDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"crossrealm_d","path":"gno.land/r/tests/vm/crossrealm_d","files":[{"name":"crossrealm.gno","body":"package crossrealm_d\n\n// Simple stateful realm for cross-realm consistency tests.\n// Separated from crossrealm_b to avoid perturbing its object IDs.\n//\n// Contains both crossing and non-crossing setters so tests can\n// demonstrate that non-crossing calls from another realm cannot\n// silently mutate state via assign+recover.\n\nvar counter int\n\nfunc init() {\n\tcounter = 100\n}\n\n// SetCounter: non-crossing. Calling this cross-realm triggers\n// the readonly check because it directly assigns a package var.\nfunc SetCounter(n int) {\n\tcounter = n\n}\n\n// SetCounterCrossing: crossing version. This works correctly\n// cross-realm because the caller enters this realm's context.\nfunc SetCounterCrossing(cur realm, n int) {\n\tcounter = n\n}\n\nfunc GetCounter(cur realm) int {\n\treturn counter\n}\n\n// DoubleCounter reads counter and doubles it. Used to show that\n// if counter were silently corrupted in memory, subsequent crossing\n// calls would act on the wrong value.\nfunc DoubleCounter(cur realm) int {\n\tcounter = counter * 2\n\treturn counter\n}\n\n// MutateBytes mutates the first byte of bz to 0xff. Used to test\n// whether a foreign realm can write to caller-allocated bytes.\n// Under storage=authority, bz has PkgID=caller; borrow rule #1 here\n// makes m.Realm=crossrealm_d (declaring realm); the write site\n// (bz[0] = ...) should fire readonly because bz.PkgID != m.Realm.ID.\nfunc MutateBytes(bz []byte) {\n\tbz[0] = 0xff\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/crossrealm_d\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"hnNYUnsjx873Qw6cPovniyEhUMSj14G3piuZ+lc3eFYA4v9eI7ZTxFJr/efyFukh4ho4UeO7p6uawYM/Scx9Aw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"crossrealm_e","path":"gno.land/r/tests/vm/crossrealm_e","files":[{"name":"crossrealm.gno","body":"package crossrealm_e\n\nimport (\n\t\"chain/runtime/unsafe\"\n)\n\nvar (\n\tbalance int64\n\towner   address\n)\n\nfunc init() {\n\tbalance = 1000\n\tSetOwner(address(\"g1dao_address_here\"))\n}\n\n// SetOwner is an internal helper that was exported by mistake\n// (should be setOwner). Without the pre-mutation readonly check,\n// a cross-realm caller could call SetOwner + recover to silently\n// hijack ownership in memory, then call TransferToken to steal funds.\nfunc SetOwner(o address) {\n\towner = o\n}\n\nfunc GetOwner() address {\n\treturn owner\n}\n\nfunc TransferOwnership(cur realm, o address) {\n\tif unsafe.PreviousRealm().Address() != owner {\n\t\tpanic(\"unauthorized\")\n\t}\n\towner = o\n}\n\nfunc TransferToken(cur realm) {\n\tcaller := unsafe.PreviousRealm().Address()\n\tif caller != owner {\n\t\tpanic(\"unauthorized\")\n\t}\n\tbalance -= 500\n\tprintln(\"===send token to: \", caller)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/crossrealm_e\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"/xSLSBFp4uuTk5UD0b+6RbTpMNMHWA4i/eF+bq+CTX5deDSWvS1bUnWv0ERj9nsgfBkVlS1xYoTSotwKwDxmCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"crossrealm_f","path":"gno.land/r/tests/vm/crossrealm_f","files":[{"name":"crossrealm.gno","body":"// Package crossrealm_f provides a collection realm for testing cross-realm\n// ownership scenarios. It uses a nested structure so that intermediate objects\n// in the ownership chain have RefCount == 1.\npackage crossrealm_f\n\ntype Entry struct {\n\tKey   string\n\tValue int\n}\n\nvar entries []*Entry\n\nfunc NewEntry(key string, value int) *Entry {\n\treturn \u0026Entry{Key: key, Value: value}\n}\n\nfunc Add(cur realm, e *Entry) {\n\tentries = append(entries, e)\n}\n\nfunc Remove(cur realm, key string) *Entry {\n\tfor i, e := range entries {\n\t\tif e.Key == key {\n\t\t\tentries = append(entries[:i], entries[i+1:]...)\n\t\t\treturn e\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc Get(key string) *Entry {\n\tfor _, e := range entries {\n\t\tif e.Key == key {\n\t\t\treturn e\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc Len() int {\n\treturn len(entries)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/crossrealm_f\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"gG0HVEDKNpiOEJb7SY2ACU9zOjaaLAcJMXsvaOYr0J5BtWQiWmnycfCr/1BVwCyfKhtj8A2q5yc/Wvv4lRaVBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"curchain_b","path":"gno.land/r/tests/vm/curchain_b","files":[{"name":"curchain_b.gno","body":"// Package curchain_b is the terminal hop in the multi-level cross-call\n// chain test helpers. It prints the full captured chain visible from this\n// realm, and asserts parity with unsafe.CurrentRealm() /\n// unsafe.PreviousRealm() at the cross-callee position.\npackage curchain_b\n\nimport (\n\t\"chain/runtime/unsafe\"\n)\n\n// B is a crossing function that prints the captured chain reached via\n// .Previous() walks and asserts parity with unsafe.CurrentRealm() /\n// unsafe.PreviousRealm() at the cross-callee position. The chain-root\n// case is covered separately by zrealm_cur_parity.\n//\n// Chain layout for callsite main -\u003e curchain_a.A -\u003e curchain_b.B:\n//\n//\tcur                  = B\n//\tcur.Previous()       = A\n//\tcur.Previous()^2     = main\n//\tcur.Previous()^3     = EOA origin (PkgPath() == \"\")\n//\tcur.Previous()^4     = panics (origin has no further previous)\nfunc B(cur realm) {\n\tprintln(\"B cur.PkgPath:\", cur.PkgPath())\n\tp1 := cur.Previous()\n\tprintln(\"B Previous().PkgPath:\", p1.PkgPath())\n\tp2 := p1.Previous()\n\tprintln(\"B Previous().Previous().PkgPath:\", p2.PkgPath())\n\tp3 := p2.Previous()\n\tprintln(\"B Previous()^3 PkgPath empty (EOA origin):\", p3.PkgPath() == \"\")\n\tfunc() {\n\t\tdefer func() {\n\t\t\tr := recover()\n\t\t\tprintln(\"B Previous()^4 panics past origin:\", r != nil)\n\t\t}()\n\t\t_ = p3.Previous()\n\t}()\n\n\t// Parity at cross-callee position: cur agrees with\n\t// unsafe.CurrentRealm(), and cur.Previous() (the caller realm\n\t// A) agrees with unsafe.PreviousRealm().\n\trc := unsafe.CurrentRealm()\n\trp := unsafe.PreviousRealm()\n\tprintln(\"B parity cur==Current:\",\n\t\tstring(cur.Address()) == string(rc.Address()) \u0026\u0026\n\t\t\tcur.PkgPath() == rc.PkgPath())\n\tprintln(\"B parity prev==Previous:\",\n\t\tstring(p1.Address()) == string(rp.Address()) \u0026\u0026\n\t\t\tp1.PkgPath() == rp.PkgPath())\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/curchain_b\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"4ZKzm+RbhPDyV8hwM+UicsuQaQZaXPGTO43uLToQOpf2w0ZdcHYBIJrWjBdFxgP2scNh0l1cGnKlBKoIGCqzCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"curchain_a","path":"gno.land/r/tests/vm/curchain_a","files":[{"name":"curchain_a.gno","body":"// Package curchain_a is a test helper for multi-level cross-call chain tests.\n// A is the middle hop: when called via cross, it cross-calls into curchain_b.\npackage curchain_a\n\nimport (\n\t\"gno.land/r/tests/vm/curchain_b\"\n)\n\n// A is a crossing function that cross-calls curchain_b.B(cross).\n// Use from a filetest as: curchain_a.A(cross) to set up a 3-level chain\n// caller → A → B with the filetest's main as the chain root.\nfunc A(cur realm) {\n\tprintln(\"A cur.PkgPath:\", cur.PkgPath())\n\tprintln(\"A prev.PkgPath:\", cur.Previous().PkgPath())\n\tcurchain_b.B(cross(cur))\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/curchain_a\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"iiAAyfHWmujBovbVPZe5yp3gLYukfSvd+6uiqA4aRSXzHI4XIBU+xSN/kI9bQjCsbZ4Ljua2GPmvCw/U3mm3Cw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"innerowner","path":"gno.land/r/tests/vm/innerowner","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/innerowner\"\ngno = \"0.9\"\n"},{"name":"innerowner.gno","body":"// Package innerowner persists an *Inner that lives in this realm.\n// Foreign-realm callers use GetInner() to obtain a pointer to it.\n// When they pass it as the receiver of a /p/ method, PushFrameCall's\n// borrow rule 2 fires and m.Realm shifts to this realm.\npackage innerowner\n\nimport \"gno.land/p/demo/tests/p_closurecap\"\n\nvar inn *p_closurecap.Inner\n\nfunc init() {\n\tinn = \u0026p_closurecap.Inner{N: 0}\n}\n\n// GetInner returns the persisted Inner. Non-crossing: the caller's\n// realm stays current, but the returned *Inner carries PkgID =\n// innerowner from its init-time allocation.\nfunc GetInner() *p_closurecap.Inner {\n\treturn inn\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"oSHBZIGd9C3E4fNcenm8Xzd8wstTJB++FSOEvbF1v8RDx0NvMj0U9086inaSLWIXYhIpfzxl1wX+xQ0fIOp9CA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"launderrvictim","path":"gno.land/r/tests/vm/launderrvictim","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/launderrvictim\"\ngno = \"0.9\"\n"},{"name":"launderrvictim.gno","body":"// Package launderrvictim is the /r/-DATA-DECLARED variant of the\n// launder-game victim. Its Immutable type is declared HERE (in /r/),\n// not in /p/launderpkg. This is the recommended inter-realm pattern:\n// realms declare their own logic data types.\n//\n// The hypothesis under test: with /r/-declared logic data, the\n// Attack H/I/J/K/L laundering shapes are structurally impossible.\n// Tests against this victim should all fail to mutate gImm.\npackage launderrvictim\n\nimport \"gno.land/p/demo/tests/launderpkg\"\n\n// Immutable is /r/-declared (the key difference from /r/laundervictim,\n// which uses /p/launderpkg.Immutable).\ntype Immutable struct {\n\tField string\n}\n\n// Read is /r/launderrvictim-declared, so calling it borrow rule #1 borrows\n// m.Realm to launderrvictim.\nfunc (i *Immutable) Read() string { return i.Field }\n\nvar gImm *Immutable\n\nfunc init() {\n\tgImm = \u0026Immutable{Field: \"rdata-original\"}\n}\n\n// GetImm hands out a pointer to gImm. Standard \"victim exposes a\n// pointer to its state\" antipattern — but with /r/-declared data,\n// the attacker should still be unable to write through it.\nfunc GetImm() *Immutable { return gImm }\n\n// ReadImm reads the current field for after-attack verification.\nfunc ReadImm() string { return gImm.Field }\n\n// UseAnyMutator boxes gImm as any and dispatches a /p/-declared\n// AnyMutator. This is the dangerous shape from Attack L: victim\n// boxes its own /r/-declared data through a /p/-defined interface\n// that the attacker can implement.\nfunc UseAnyMutator(m launderpkg.AnyMutator) {\n\tm.Run(gImm)\n}\n\n// ApplyHook dispatches a caller-supplied callback on gImm. The\n// callback's parameter type is /r/launderrvictim-declared, so /p/\n// packages can't supply this hook — only /r/ realms can.\nfunc ApplyHook(h func(*Immutable)) {\n\th(gImm)\n}\n\n// --- /p/-type embedded / fielded inside /r/-declared types ---\n//\n// The three shapes below mix /r/-declared containers with /p/-typed\n// inner state. Even though the container types are /r/-declared,\n// the inner /p/-typed values inherit /p/'s methods — including\n// Apply-style higher-order methods that take /p/-typed callbacks.\n// /p/-attacker code can supply such callbacks. The attacker reaches\n// the inner /p/-value (read access works), then invokes Apply with\n// a /p/-declared function pointer. Inside Apply (borrow rule #2ed\n// to /r/launderrvictim), the callback runs without any borrow rule #1 or #2\n// shift (top-level /p/ fn), so the write commits under victim\n// authority.\n\n// WithEmbed embeds launderpkg.Immutable by VALUE (method promotion\n// gives WithEmbed an .Apply method).\ntype WithEmbed struct {\n\tlaunderpkg.Immutable\n}\n\n// WithPtr has a POINTER FIELD to launderpkg.Immutable.\ntype WithPtr struct {\n\tInner *launderpkg.Immutable\n}\n\n// WithVal has a VALUE FIELD of launderpkg.Immutable (not embedded;\n// the field is named, no method promotion — but the value is still\n// addressable through c.Inner).\ntype WithVal struct {\n\tInner launderpkg.Immutable\n}\n\nvar (\n\tgWithEmbed *WithEmbed\n\tgWithPtr   *WithPtr\n\tgWithVal   *WithVal\n)\n\nfunc init() {\n\tgWithEmbed = \u0026WithEmbed{Immutable: launderpkg.Immutable{Field: \"embed-orig\"}}\n\tgWithPtr = \u0026WithPtr{Inner: \u0026launderpkg.Immutable{Field: \"ptr-orig\"}}\n\tgWithVal = \u0026WithVal{Inner: launderpkg.Immutable{Field: \"val-orig\"}}\n}\n\nfunc GetWithEmbed() *WithEmbed { return gWithEmbed }\nfunc GetWithPtr() *WithPtr     { return gWithPtr }\nfunc GetWithVal() *WithVal     { return gWithVal }\n\nfunc ReadEmbed() string { return gWithEmbed.Field }\nfunc ReadPtr() string   { return gWithPtr.Inner.Field }\nfunc ReadVal() string   { return gWithVal.Inner.Field }\n\n// --- Methods-less /p/-type inner state ---\n// launderpkg.Bare has no methods. These containers wrap Bare in\n// the three field shapes. The question: does readonly taint catch\n// a direct field write through the /r/-container's getter?\n\ntype WithBareEmbed struct {\n\tlaunderpkg.Bare\n}\n\ntype WithBarePtr struct {\n\tInner *launderpkg.Bare\n}\n\ntype WithBareVal struct {\n\tInner launderpkg.Bare\n}\n\nvar (\n\tgWithBareEmbed *WithBareEmbed\n\tgWithBarePtr   *WithBarePtr\n\tgWithBareVal   *WithBareVal\n)\n\nfunc init() {\n\tgWithBareEmbed = \u0026WithBareEmbed{Bare: launderpkg.Bare{Field: \"bare-embed-orig\"}}\n\tgWithBarePtr = \u0026WithBarePtr{Inner: \u0026launderpkg.Bare{Field: \"bare-ptr-orig\"}}\n\tgWithBareVal = \u0026WithBareVal{Inner: launderpkg.Bare{Field: \"bare-val-orig\"}}\n}\n\nfunc GetWithBareEmbed() *WithBareEmbed { return gWithBareEmbed }\nfunc GetWithBarePtr() *WithBarePtr     { return gWithBarePtr }\nfunc GetWithBareVal() *WithBareVal     { return gWithBareVal }\n\nfunc ReadBareEmbed() string { return gWithBareEmbed.Field }\nfunc ReadBarePtr() string   { return gWithBarePtr.Inner.Field }\nfunc ReadBareVal() string   { return gWithBareVal.Inner.Field }\n\n// --- Composite containers holding /p/-typed elements ---\n// Slices, arrays, maps of methods-less /p/-Bare values and pointers.\n\nvar (\n\tgBareSlice    []launderpkg.Bare\n\tgBarePtrSlice []*launderpkg.Bare\n\tgBareArr      [3]launderpkg.Bare\n\tgBarePtrArr   [3]*launderpkg.Bare\n\tgBareMap      map[string]launderpkg.Bare\n\tgBarePtrMap   map[string]*launderpkg.Bare\n)\n\nfunc init() {\n\tgBareSlice = []launderpkg.Bare{\n\t\t{Field: \"slice0\"}, {Field: \"slice1\"},\n\t}\n\tgBarePtrSlice = []*launderpkg.Bare{\n\t\t{Field: \"ptrslice0\"}, {Field: \"ptrslice1\"},\n\t}\n\tgBareArr = [3]launderpkg.Bare{\n\t\t{Field: \"arr0\"}, {Field: \"arr1\"}, {Field: \"arr2\"},\n\t}\n\tgBarePtrArr = [3]*launderpkg.Bare{\n\t\t{Field: \"ptrarr0\"}, {Field: \"ptrarr1\"}, {Field: \"ptrarr2\"},\n\t}\n\tgBareMap = map[string]launderpkg.Bare{\n\t\t\"a\": {Field: \"mapA\"}, \"b\": {Field: \"mapB\"},\n\t}\n\tgBarePtrMap = map[string]*launderpkg.Bare{\n\t\t\"a\": {Field: \"ptrmapA\"}, \"b\": {Field: \"ptrmapB\"},\n\t}\n}\n\nfunc GetBareSlice() []launderpkg.Bare            { return gBareSlice }\nfunc GetBarePtrSlice() []*launderpkg.Bare        { return gBarePtrSlice }\nfunc GetBareArr() *[3]launderpkg.Bare            { return \u0026gBareArr }\nfunc GetBarePtrArr() *[3]*launderpkg.Bare        { return \u0026gBarePtrArr }\nfunc GetBareMap() map[string]launderpkg.Bare     { return gBareMap }\nfunc GetBarePtrMap() map[string]*launderpkg.Bare { return gBarePtrMap }\n\nfunc ReadBareSlice0() string      { return gBareSlice[0].Field }\nfunc ReadBareSlice0Then1() string { return gBareSlice[1].Field }\nfunc ReadBarePtrSlice0() string   { return gBarePtrSlice[0].Field }\nfunc ReadBareArr0() string        { return gBareArr[0].Field }\nfunc ReadBarePtrArr0() string     { return gBarePtrArr[0].Field }\nfunc ReadBareMapA() string        { return gBareMap[\"a\"].Field }\nfunc ReadBarePtrMapA() string     { return gBarePtrMap[\"a\"].Field }\n\n// --- Panic/defer/recover helpers ---\n// These victim-side helpers expose scenarios where the m.Realm\n// borrow can interact with deferred calls, recover(), and panics\n// in unusual control-flow shapes.\n\n// DeferCallback installs h as a defer inside an /r/launderrvictim\n// frame, then returns. h runs at frame pop. The question: at the\n// time h is invoked, m.Realm has just been restored to caller's\n// realm by PopFrameAndReturn — but wait, defers run BEFORE\n// PopFrameAndReturn. So m.Realm should still be victim's. Does\n// the deferred h then run under victim authority?\nfunc DeferCallback(h func(*Immutable)) {\n\tdefer h(gImm)\n}\n\n// PanicAfterPushDefer pushes a defer and then panics, so the defer\n// runs as part of panic unwinding. Tests that m.Realm is correctly\n// borrowed when the defer body invokes a foreign function.\nfunc PanicAfterPushDefer(h func(*Immutable)) {\n\tdefer h(gImm)\n\tpanic(\"victim-induced panic\")\n}\n\n// DeferApplyHook defers an ApplyHook call. The deferred ApplyHook\n// itself runs borrow rule #1 to /r/launderrvictim, and inside the\n// callback runs as borrow rule #1 of the attacker's realm — the standard\n// known-open Apply pattern, but now triggered via defer.\nfunc DeferApplyHook(h func(*Immutable)) {\n\tdefer ApplyHook(h)\n}\n\n// RecoverAndRetry: inside a victim method, defer a recover, write\n// something to gImm, then panic. After the recover, the function\n// returns normally. Tests that internal panic/recover doesn't leak\n// state.\nfunc RecoverAndRetry(h func(*Immutable)) (recovered any) {\n\tdefer func() {\n\t\trecovered = recover()\n\t}()\n\th(gImm)\n\treturn\n}\n\n// CallThenPanic invokes h synchronously and then panics. If h is\n// attacker-supplied and writes via captured pointer, this is just\n// a re-shape of ApplyHook.\nfunc CallThenPanic(h func(*Immutable)) {\n\th(gImm)\n\tpanic(\"victim panic after callback\")\n}\n\n// CallPDeferApply: multi-level defer chain. Victim invokes a\n// /p/-method (DeferApply) on a victim-owned *launderpkg.Immutable;\n// the /p/-method defers the attacker callback. Three frames at\n// callback time: attacker.main → victim.CallPDeferApply →\n// /p/.DeferApply (deferred fn dispatches here).\nfunc CallPDeferApply(fn func(*launderpkg.Immutable)) {\n\tgWithPtr.Inner.DeferApply(fn)\n}\n\n// --- Stored-hook plumbing ---\n//\n// Victim accepts caller-registered callbacks and dispatches them\n// LATER, from inside a /r/-victim method body. If the registered\n// callback is /p/-declared and writes through a captured /r/-stamped\n// pointer, the laundering shape is: stored callback rather than\n// callback-arg.\n\ntype ImmHook func(*Immutable)\n\nvar gHooks []ImmHook\n\nfunc RegisterHook(h ImmHook) { gHooks = append(gHooks, h) }\nfunc RunHooks() {\n\tfor _, h := range gHooks {\n\t\th(gImm)\n\t}\n}\n\ntype PlainHook func()\n\nvar gPlainHooks []PlainHook\n\nfunc RegisterPlainHook(h PlainHook) { gPlainHooks = append(gPlainHooks, h) }\nfunc RunPlainHooks() {\n\tfor _, h := range gPlainHooks {\n\t\th()\n\t}\n}\n\nfunc ClearHooks() {\n\tgHooks = nil\n\tgPlainHooks = nil\n}\n\n// MakeWriterClosure constructs a /r/-victim-declared closure that\n// captures gImm and writes through it. The closure body is /r/-victim-\n// declared, so borrow rule #1 fires at invocation → m.Realm = /r/-victim →\n// write commits with victim authority. Returning this closure to an\n// attacker is \"consenting to write\" by the victim.\nfunc MakeWriterClosure(value string) func() {\n\treturn func() {\n\t\tgImm.Field = value\n\t}\n}\n\n// MakeApplyTrampoline returns a closure that captures \u0026gImm.Field\n// indirectly: it captures *Immutable, and dispatches a caller-supplied\n// callback fn on it. /r/-victim-declared body → borrow rule #1 → m.Realm =\n// /r/-victim. If `fn` is /p/-declared (e.g. EvilWrite), it inherits\n// victim authority. This is \"victim returns a closure that's itself\n// an Apply-style trampoline\" — a packaged Apply.\nfunc MakeApplyTrampoline() func(func(*Immutable)) {\n\treturn func(fn func(*Immutable)) {\n\t\tfn(gImm)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"5gr8FQOwGYwtSHA2BIPYAiO5EH3YYuYpk74x7usjo+VCz13yzsTLsycHurE3WLbitf131UxzP28f+eeNW52ECw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"laundervictim","path":"gno.land/r/tests/vm/laundervictim","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/laundervictim\"\ngno = \"0.9\"\n"},{"name":"laundervictim.gno","body":"// Package laundervictim is the \"victim\" realm in the launder-game\n// tests. It exposes a package-level g that an attacker tries to\n// mutate by various means. Two shapes are exposed:\n//\n//   - gVal is `launderpkg.Object` (value type).\n//   - gPtr is `*launderpkg.Object` (pointer to a fresh Object).\n//\n// Both are allocated at init under this realm's context, so their\n// PkgID stamp is /r/.../laundervictim. The attacker's job is to make\n// the stamp not match m.Realm at the write site — by laundering the\n// stamp, capturing the value, or exploiting a borrow-rule shift.\npackage laundervictim\n\nimport \"gno.land/p/demo/tests/launderpkg\"\n\nvar (\n\tgVal launderpkg.Object\n\tgPtr *launderpkg.Object\n\t// gImm is an Immutable: same layout as Object but no mutator\n\t// method in /p/launderpkg. Victim exposes a pointer to it,\n\t// intending \"callers can read but not write.\"\n\tgImm *launderpkg.Immutable\n\t// gBuf is a victim-owned byte buffer (real, /r/laundervictim-stamped\n\t// after init). Used to probe whether a stdlib method (e.g.\n\t// base64.Encode) can be tricked into writing the victim's own buffer\n\t// when an attacker passes it as an out-parameter.\n\tgBuf []byte\n)\n\nfunc init() {\n\tgVal = launderpkg.Object{Field: \"original-val\"}\n\tgPtr = \u0026launderpkg.Object{Field: \"original-ptr\"}\n\tgImm = \u0026launderpkg.Immutable{Field: \"original-imm\"}\n\tgBuf = []byte(\"original-buffer!\")\n}\n\n// GetVal returns g by VALUE (caller gets a copy).\nfunc GetVal() launderpkg.Object { return gVal }\n\n// GetPtr returns the pointer to gPtr's underlying Object. The\n// returned pointer aliases the victim's persisted state.\nfunc GetPtr() *launderpkg.Object { return gPtr }\n\n// GetValAddr returns \u0026gVal — a pointer to the value-typed slot.\n// The returned pointer aliases the victim's persisted state.\nfunc GetValAddr() *launderpkg.Object { return \u0026gVal }\n\n// GetImm returns the pointer to gImm — a *Immutable, which has no\n// mutator method in /p/launderpkg. Victim's intent: callers can read\n// but not write.\nfunc GetImm() *launderpkg.Immutable { return gImm }\n\n// GetBuf returns the victim's own byte buffer. The returned slice\n// aliases the victim's persisted backing array (/r/laundervictim-stamped).\nfunc GetBuf() []byte { return gBuf }\n\n// ReadVal / ReadPtr / ReadImm / ReadBuf report the current values for\n// after-attack verification.\nfunc ReadVal() string { return gVal.Field }\nfunc ReadPtr() string { return gPtr.Field }\nfunc ReadImm() string { return gImm.Field }\nfunc ReadBuf() string { return string(gBuf) }\n\n// Exploiter is the interface the victim accepts. The attacker\n// supplies an implementation; the victim invokes Something(g)\n// passing its own g. This is the \"victim hands attacker the data\"\n// vector.\ntype Exploiter interface {\n\tSomething(launderpkg.Object)\n\tSomethingPtr(*launderpkg.Object)\n}\n\n// Invoke calls the attacker's methods passing the victim's g by both\n// value and by pointer.\nfunc Invoke(e Exploiter) {\n\te.Something(gVal)\n\te.SomethingPtr(gPtr)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"BdNlPw6wBOZTI8+veoDe0wJl3llOtX/6j9clRSWGkROLwCzCt3v+QHPeEGQIZ4RKZCTuc/ObdgpQJ489DE8tDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"persistedcap","path":"gno.land/r/tests/vm/persistedcap","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/persistedcap\"\ngno = \"0.9\"\n"},{"name":"persistedcap.gno","body":"// Package persistedcap stores a closure (constructed by a /p/ factory)\n// at init, persisting the closure's captured HIV as part of this\n// realm's state. Foreign-realm callers can grab the closure via\n// GetCounter() and invoke it; the closure body's write to its\n// captured HIV is REJECTED by the readonly check because the HIV is\n// real (NewTime\u003e0) and belongs to a different realm than the caller's\n// m.Realm. The unreal-HIV exception only applies while the HIV is\n// transient — persistence elevates it to a normal realm-owned slot.\npackage persistedcap\n\nimport \"gno.land/p/demo/tests/p_closurecap\"\n\nvar counter func() int\n\nfunc init() {\n\tcounter = p_closurecap.MakeCounter(0)\n}\n\n// GetCounter returns the persisted closure. The caller invokes it\n// directly — because the closure's FuncLit lives in /p/ p_closurecap,\n// PushFrameCall's borrow rule does NOT switch m.Realm to persistedcap\n// at invocation. The closure body's write to the captured HIV (PkgID\n// = persistedcap, persisted) therefore runs under the caller's\n// m.Realm, which is the persisted-closure-capture cross-realm case.\n//\n// SECURITY: deliberate VM-parity-test infrastructure — do NOT copy this\n// pattern in production code. See gno.land/r/tests/vm/crossrealm\n// Closure for the broader rationale.\nfunc GetCounter() func() int {\n\treturn counter\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"h2LFchsEApaXUrq9CyoaD7PnAAVgLeXYffxZsa3dLEl9RSjEa+axNgxgtBKMlYwWQCCcDI8smV7jw7FEK8h0DA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"tests_foo","path":"gno.land/r/tests/vm/tests_foo","files":[{"name":"foo.gno","body":"package tests_foo\n\nimport (\n\ttests \"gno.land/r/tests/vm\"\n)\n\n// for testing gno.land/r/tests/vm/interfaces.go\n\ntype FooStringer struct {\n\tFieldA string\n}\n\nfunc (fs *FooStringer) String() string {\n\treturn \"\u0026FooStringer{\" + fs.FieldA + \"}\"\n}\n\n// AddFooStringer is a non-crossing helper. Callers thread their own\n// live cur as `rlm`; `cross(rlm)` forwards it into the (cur realm)-\n// crossing tests.AddStringer.\nfunc AddFooStringer(_ int, rlm realm, fa string) {\n\ttests.AddStringer(cross(rlm), \u0026FooStringer{fa})\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/tests_foo\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"mRd5AjMfpcF2ZSVLHpphvaufzI9jNwruo9ZZoJwbcuQuOPYfOVxHY+9BWYBW5INEzPOEyejvuno/EN8kkLzLAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"variadic","path":"gno.land/r/tests/vm/variadic","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/variadic\"\ngno = \"0.9\"\n"},{"name":"main.gno","body":"package variadic\n\nimport \"strings\"\n\nfunc Echo(cur realm, vals ...string) string {\n\treturn strings.Join(vals, \" \")\n}\n\nfunc Add(cur realm, nums ...int) int {\n\tres := 0\n\n\tfor _, num := range nums {\n\t\tres += num\n\t}\n\n\treturn res\n}\n\nfunc And(cur realm, booleans ...bool) bool {\n\n\tfor _, boolean := range booleans {\n\t\tif !boolean {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Ggz3ftSwpyyP8+7YZadLYy9J3NXQPqaMkHv4StCC7G6L4Vblh1O4qjcdVLLN5PlQsQVsbIgUZ/O2U6kuoeymDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"forms","path":"gno.land/p/agherasie/forms","files":[{"name":"create.gno","body":"package forms\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/onbloc/json\"\n)\n\nconst dateFormat = \"2006-01-02T15:04:05Z\"\n\nfunc CreateField(label string, fieldType string, required bool) Field {\n\treturn Field{\n\t\tLabel:     label,\n\t\tFieldType: fieldType,\n\t\tRequired:  required,\n\t}\n}\n\n// CreateForm creates a new form. rlm must be the caller's own captured\n// cur (asserted via rlm.IsCurrent()); the form Owner is set to\n// rlm.Previous().Address() — unforgeable, vs. the prior runtime walk\n// which a non-crossing wrapper could spoof.\nfunc (db *FormDB) CreateForm(_ int, rlm realm, title string, description string, openAt string, closeAt string, data string) (string, error) {\n\tif !rlm.IsCurrent() {\n\t\treturn \"\", errSpoofedRealm\n\t}\n\t// Parsing the dates\n\tvar parsedOpenTime, parsedCloseTime time.Time\n\n\tif openAt != \"\" {\n\t\tvar err error\n\t\tparsedOpenTime, err = time.Parse(dateFormat, openAt)\n\t\tif err != nil {\n\t\t\treturn \"\", errInvalidDate\n\t\t}\n\t}\n\n\tif closeAt != \"\" {\n\t\tvar err error\n\t\tparsedCloseTime, err = time.Parse(dateFormat, closeAt)\n\t\tif err != nil {\n\t\t\treturn \"\", errInvalidDate\n\t\t}\n\t}\n\n\t// Parsing the json submission\n\tnode, err := json.Unmarshal([]byte(data))\n\tif err != nil {\n\t\treturn \"\", errInvalidJson\n\t}\n\n\tfieldsCount := node.Size()\n\tfields := make([]Field, fieldsCount)\n\n\t// Parsing the json submission to create the gno data structures\n\tfor i := 0; i \u003c fieldsCount; i++ {\n\t\tfield := node.MustIndex(i)\n\n\t\tfields[i] = CreateField(\n\t\t\tfield.MustKey(\"label\").MustString(),\n\t\t\tfield.MustKey(\"fieldType\").MustString(),\n\t\t\tfield.MustKey(\"required\").MustBool(),\n\t\t)\n\t}\n\n\t// Generating the form ID\n\tid := db.IDCounter.Next().String()\n\n\t// Creating the form\n\tform := Form{\n\t\tID:          id,\n\t\tOwner:       rlm.Previous().Address(),\n\t\tTitle:       title,\n\t\tDescription: description,\n\t\tCreatedAt:   time.Now(),\n\t\topenAt:      parsedOpenTime,\n\t\tcloseAt:     parsedCloseTime,\n\t\tFields:      fields,\n\t}\n\n\t// Adding the form to the database\n\tdb.Forms = append(db.Forms, \u0026form)\n\n\treturn id, nil\n}\n"},{"name":"create_test.gno","body":"package forms\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestCreateForm(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetOriginCaller(alice)\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\n\tdb := NewDB()\n\ttitle := \"Simple Form\"\n\tdescription := \"This is a form\"\n\topenAt := \"2021-01-01T00:00:00Z\"\n\tcloseAt := \"2021-01-02T00:00:00Z\"\n\tdata := `[\n\t\t{\n\t\t\t\"label\": \"Name\",\n\t\t\t\"fieldType\": \"string\",\n\t\t\t\"required\": true\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Age\",\n\t\t\t\"fieldType\": \"number\",\n\t\t\t\"required\": false\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Is this a test?\",\n\t\t\t\"fieldType\": \"boolean\",\n\t\t\t\"required\": false\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Favorite Food\",\n\t\t\t\"fieldType\": \"['Pizza', 'Schnitzel', 'Burger']\",\n\t\t\t\"required\": true\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Favorite Foods\",\n\t\t\t\"fieldType\": \"{'Pizza', 'Schnitzel', 'Burger'}\",\n\t\t\t\"required\": true\n\t\t}\n\t]`\n\n\turequire.NotPanics(t, cur, func() {\n\t\tvar id string\n\t\tvar err error\n\t\tfunc(cur realm) {\n\t\t\tid, err = db.CreateForm(0, cur, title, description, openAt, closeAt, data)\n\t\t}(cross(cur))\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\turequire.True(t, id != \"\", \"Form ID is empty\")\n\n\t\tform, err := db.GetForm(id)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\turequire.True(t, form.ID == id, \"Form ID is not correct\")\n\t\turequire.Equal(t, form.Owner, alice, \"Owner is not correct\")\n\t\turequire.True(t, form.Title == title, \"Title is not correct\")\n\t\turequire.True(t, form.Description == description, \"Description is not correct\")\n\t\turequire.True(t, len(form.Fields) == 5, \"Not enough fields were provided\")\n\t\turequire.True(t, form.Fields[0].Label == \"Name\", \"Field 0 label is not correct\")\n\t\turequire.True(t, form.Fields[0].FieldType == \"string\", \"Field 0 type is not correct\")\n\t\turequire.True(t, form.Fields[0].Required == true, \"Field 0 required is not correct\")\n\t\turequire.True(t, form.Fields[1].Label == \"Age\", \"Field 1 label is not correct\")\n\t\turequire.True(t, form.Fields[1].FieldType == \"number\", \"Field 1 type is not correct\")\n\t})\n}\n"},{"name":"doc.gno","body":"// # Gno forms\n\n// gno-forms is a package which demonstrates a form editing and sharing application in gno\n\n// ## Features\n// - **Form Creation**: Create new forms with specified titles, descriptions, and fields.\n// - **Form Submission**: Submit answers to forms.\n// - **Form Retrieval**: Retrieve existing forms and their submissions.\n// - **Form Deadline**: Set a precise time range during which a form can be interacted with.\n\n// ## Field Types\n// The system supports the following field types:\n\n// | type         | example                                                                                         |\n// |--------------|-------------------------------------------------------------------------------------------------|\n// | string       | `{\"label\": \"Name\", \"fieldType\": \"string\", \"required\": true}`                                    |\n// | number       | `{\"label\": \"Age\", \"fieldType\": \"number\", \"required\": true}`                                     |\n// | boolean      | `{\"label\": \"Is Student?\", \"fieldType\": \"boolean\", \"required\": false}`                           |\n// | choice       | `{\"label\": \"Favorite Food\", \"fieldType\": \"['Pizza', 'Schnitzel', 'Burger']\", \"required\": true}` |\n// | multi-choice | `{\"label\": \"Hobbies\", \"fieldType\": \"{'Reading', 'Swimming', 'Gaming'}\", \"required\": false}`     |\n\n// ## Web-app\n\n// The external repo where the initial development took place and where you can find the frontend is [here](https://github.com/agherasie/gno-forms).\npackage forms\n"},{"name":"errors.gno","body":"package forms\n\nimport \"errors\"\n\nvar (\n\terrNoOpenDate       = errors.New(\"Form has no open date\")\n\terrNoCloseDate      = errors.New(\"Form has no close date\")\n\terrInvalidJson      = errors.New(\"Invalid JSON\")\n\terrInvalidDate      = errors.New(\"Invalid date\")\n\terrFormNotFound     = errors.New(\"Form not found\")\n\terrAnswerNotFound   = errors.New(\"Answer not found\")\n\terrAlreadySubmitted = errors.New(\"You already submitted this form\")\n\terrFormClosed       = errors.New(\"Form is closed\")\n\terrInvalidAnswers   = errors.New(\"Invalid answers\")\n\terrSpoofedRealm     = errors.New(\"rlm does not match the current crossing frame\")\n)\n"},{"name":"forms.gno","body":"package forms\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// FieldType examples :\n// - string: \"string\";\n// - number: \"number\";\n// - boolean: \"boolean\";\n// - choice: \"['Pizza', 'Schnitzel', 'Burger']\";\n// - multi-choice: \"{'Pizza', 'Schnitzel', 'Burger'}\";\ntype Field struct {\n\tLabel     string\n\tFieldType string\n\tRequired  bool\n}\n\ntype Form struct {\n\tID          string\n\tOwner       address\n\tTitle       string\n\tDescription string\n\tFields      []Field\n\tCreatedAt   time.Time\n\topenAt      time.Time\n\tcloseAt     time.Time\n}\n\n// Answers example :\n// - [\"Alex\", 21, true, 0, [0, 1]]\ntype Submission struct {\n\tFormID      string\n\tAuthor      address\n\tAnswers     string // json\n\tSubmittedAt time.Time\n}\n\ntype FormDB struct {\n\tForms     []*Form\n\tAnswers   []*Submission\n\tIDCounter seqid.ID\n}\n\nfunc NewDB() *FormDB {\n\treturn \u0026FormDB{\n\t\tForms:   make([]*Form, 0),\n\t\tAnswers: make([]*Submission, 0),\n\t}\n}\n\n// This function checks if the form is open by verifying the given dates\n// - If a form doesn't have any dates, it's considered open\n// - If a form has only an open date, it's considered open if the open date is in the past\n// - If a form has only a close date, it's considered open if the close date is in the future\n// - If a form has both open and close dates, it's considered open if the current date is between the open and close dates\nfunc (form *Form) IsOpen() bool {\n\topenAt, errOpen := form.OpenAt()\n\tclosedAt, errClose := form.CloseAt()\n\n\tnoOpenDate := errOpen != nil\n\tnoCloseDate := errClose != nil\n\n\tif noOpenDate \u0026\u0026 noCloseDate {\n\t\treturn true\n\t}\n\n\tif noOpenDate \u0026\u0026 !noCloseDate {\n\t\treturn time.Now().Before(closedAt)\n\t}\n\n\tif !noOpenDate \u0026\u0026 noCloseDate {\n\t\treturn time.Now().After(openAt)\n\t}\n\n\tnow := time.Now()\n\treturn now.After(openAt) \u0026\u0026 now.Before(closedAt)\n}\n\n// OpenAt returns the open date of the form if it exists\nfunc (form *Form) OpenAt() (time.Time, error) {\n\tif form.openAt.IsZero() {\n\t\treturn time.Time{}, errNoOpenDate\n\t}\n\n\treturn form.openAt, nil\n}\n\n// CloseAt returns the close date of the form if it exists\nfunc (form *Form) CloseAt() (time.Time, error) {\n\tif form.closeAt.IsZero() {\n\t\treturn time.Time{}, errNoCloseDate\n\t}\n\n\treturn form.closeAt, nil\n}\n\n// GetForm returns a form by its ID if it exists\nfunc (db *FormDB) GetForm(id string) (*Form, error) {\n\tfor _, form := range db.Forms {\n\t\tif form.ID == id {\n\t\t\treturn form, nil\n\t\t}\n\t}\n\treturn nil, errFormNotFound\n}\n\n// GetAnswer returns an answer by its form - and author ids if it exists\nfunc (db *FormDB) GetAnswer(formID string, author address) (*Submission, error) {\n\tfor _, answer := range db.Answers {\n\t\tif answer.FormID == formID \u0026\u0026 answer.Author.String() == author.String() {\n\t\t\treturn answer, nil\n\t\t}\n\t}\n\treturn nil, errAnswerNotFound\n}\n\n// GetSubmissionsByFormID returns a list containing the existing form submissions by the form ID\nfunc (db *FormDB) GetSubmissionsByFormID(formID string) []*Submission {\n\tsubmissions := make([]*Submission, 0)\n\n\tfor _, answer := range db.Answers {\n\t\tif answer.FormID == formID {\n\t\t\tsubmissions = append(submissions, answer)\n\t\t}\n\t}\n\n\treturn submissions\n}\n"},{"name":"forms_json.gno","body":"package forms\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/onbloc/json\"\n)\n\ntype FormNodeBuilder struct {\n\t*json.NodeBuilder\n}\n\ntype FormArrayBuilder struct {\n\t*json.ArrayBuilder\n}\n\nfunc (b *FormNodeBuilder) WriteArray(key string, fn func(*FormArrayBuilder)) *FormNodeBuilder {\n\tb.NodeBuilder.WriteArray(key, func(ab *json.ArrayBuilder) {\n\t\tfn(\u0026FormArrayBuilder{ab})\n\t})\n\treturn b\n}\n\nfunc (b *FormNodeBuilder) WriteObject(key string, fn func(*FormNodeBuilder)) *FormNodeBuilder {\n\tb.NodeBuilder.WriteObject(key, func(nb *json.NodeBuilder) {\n\t\tfn(\u0026FormNodeBuilder{nb})\n\t})\n\treturn b\n}\n\nfunc (b *FormArrayBuilder) WriteObject(fn func(*FormNodeBuilder)) *FormArrayBuilder {\n\tb.ArrayBuilder.WriteObject(func(nb *json.NodeBuilder) {\n\t\tfn(\u0026FormNodeBuilder{nb})\n\t})\n\treturn b\n}\n\nfunc (b *FormNodeBuilder) WriteFormFields(key string, fields []Field) *FormNodeBuilder {\n\tb.WriteArray(key, func(builder *FormArrayBuilder) {\n\t\tfor _, field := range fields {\n\t\t\tbuilder.WriteObject(func(builder *FormNodeBuilder) {\n\t\t\t\tbuilder.WriteString(\"label\", field.Label).\n\t\t\t\t\tWriteString(\"fieldType\", field.FieldType).\n\t\t\t\t\tWriteBool(\"required\", field.Required)\n\t\t\t})\n\t\t}\n\t})\n\treturn b\n}\n\nfunc (b *FormNodeBuilder) WriteFormSubmission(key string, submission *Submission) *FormNodeBuilder {\n\tb.WriteObject(key, func(builder *FormNodeBuilder) {\n\t\tbuilder.WriteString(\"submittedAt\", submission.SubmittedAt.Format(\"2006-01-02 15:04:05\")).\n\t\t\tWriteString(\"answers\", strings.ReplaceAll(submission.Answers, \"\\\"\", \"'\"))\n\t})\n\treturn b\n}\n\nfunc (b *FormNodeBuilder) WriteForm(key string, value *Form) *FormNodeBuilder {\n\tb.WriteString(\"id\", value.ID).\n\t\tWriteString(\"owner\", value.Owner.String()).\n\t\tWriteString(\"title\", value.Title).\n\t\tWriteString(\"description\", value.Description).\n\t\tWriteString(\"createdAt\", value.CreatedAt.Format(\"2006-01-02 15:04:05\"))\n\tb.WriteFormFields(\"fields\", value.Fields)\n\treturn b\n}\n\nfunc (b *FormArrayBuilder) WriteForm(key string, value *Form) *FormArrayBuilder {\n\tb.WriteObject(func(builder *FormNodeBuilder) {\n\t\tbuilder.WriteString(\"id\", value.ID).\n\t\t\tWriteString(\"owner\", value.Owner.String()).\n\t\t\tWriteString(\"title\", value.Title).\n\t\t\tWriteString(\"description\", value.Description).\n\t\t\tWriteString(\"createdAt\", value.CreatedAt.Format(\"2006-01-02 15:04:05\"))\n\t\tbuilder.WriteFormFields(\"fields\", value.Fields)\n\t})\n\treturn b\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/agherasie/forms\"\ngno = \"0.9\"\n"},{"name":"submit.gno","body":"package forms\n\nimport (\n\t\"time\"\n)\n\n// SubmitForm records an answer to a form. rlm must be the caller's own\n// captured cur; the Author is set to rlm.Previous().Address() —\n// unforgeable, vs. the prior runtime walk a non-crossing wrapper could\n// spoof.\nfunc (db *FormDB) SubmitForm(_ int, rlm realm, formID string, answers string) {\n\t// Check if form exists\n\tform, err := db.GetForm(formID)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tif !rlm.IsCurrent() {\n\t\tpanic(errSpoofedRealm)\n\t}\n\tauthor := rlm.Previous().Address()\n\n\t// Check if form was already submitted by this user\n\tpreviousAnswer, _ := db.GetAnswer(formID, author)\n\tif previousAnswer != nil {\n\t\tpanic(errAlreadySubmitted)\n\t}\n\n\t// Check time restrictions\n\tif !form.IsOpen() {\n\t\tpanic(errFormClosed)\n\t}\n\n\t// Check if answers are formatted correctly\n\tif ValidateAnswers(answers, form.Fields) == false {\n\t\tpanic(errInvalidAnswers)\n\t}\n\n\t// Save answers\n\tanswer := Submission{\n\t\tFormID:      formID,\n\t\tAnswers:     answers,\n\t\tAuthor:      author,\n\t\tSubmittedAt: time.Now(),\n\t}\n\tdb.Answers = append(db.Answers, \u0026answer)\n}\n"},{"name":"submit_test.gno","body":"package forms\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestAnswerForm(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\n\tdb := NewDB()\n\n\tdata := `[\n\t\t{\n\t\t\t\"label\": \"Name\",\n\t\t\t\"fieldType\": \"string\",\n\t\t\t\"required\": true\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Age\",\n\t\t\t\"fieldType\": \"number\",\n\t\t\t\"required\": false\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Is this a test?\",\n\t\t\t\"fieldType\": \"boolean\",\n\t\t\t\"required\": false\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Favorite Food\",\n\t\t\t\"fieldType\": \"[Pizza|Schnitzel|Burger]\",\n\t\t\t\"required\": true\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Favorite Foods\",\n\t\t\t\"fieldType\": \"{Pizza|Schnitzel|Burger}\",\n\t\t\t\"required\": true\n\t\t}\n\t]`\n\n\tvar formID string\n\tvar err error\n\tfunc(cur realm) {\n\t\tformID, err = db.CreateForm(0, cur, \"Test Form\", \"Test Description\", \"\", \"\", data)\n\t}(cross(cur))\n\tif err != nil {\n\t\tt.Error(err)\n\t}\n\tanswers := `[\"Alex\", 21, true, 0, [0, 1]]`\n\tfunc(cur realm) { db.SubmitForm(0, cur, formID, answers) }(cross(cur))\n\n\turequire.True(t, len(db.Answers) == 1, \"Expected 1 answer, got\", string(len(db.Answers)))\n\turequire.True(t, db.Answers[0].FormID == formID, \"Expected form ID\", formID, \"got\", db.Answers[0].FormID)\n\turequire.True(t, db.Answers[0].Answers == answers, \"Expected answers\", answers, \"got\", db.Answers[0].Answers)\n\turequire.True(t, err == nil, \"Submit should not return an error\")\n}\n\nfunc TestAnswerFormDates(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\tdb := NewDB()\n\n\tnow := time.Now()\n\ttomorrow := now.AddDate(0, 0, 1).Format(\"2006-01-02T15:04:05Z\")\n\tyesterday := now.AddDate(0, 0, -1).Format(\"2006-01-02T15:04:05Z\")\n\n\tdata := `[\n\t\t{\n\t\t\t\"label\": \"Name\",\n\t\t\t\"fieldType\": \"string\",\n\t\t\t\"required\": true\n\t\t}\n\t]`\n\tanswers := `[\"Test\"]`\n\n\turequire.AbortsWithMessage(t, cur, \"Form is closed\", func() {\n\t\tvar formID string\n\t\tvar err error\n\t\tfunc(cur realm) {\n\t\t\tformID, err = db.CreateForm(0, cur, \"Test Form\", \"Test Description\", tomorrow, \"\", data)\n\t\t}(cross(cur))\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t}\n\t\tfunc(cur realm) { db.SubmitForm(0, cur, formID, answers) }(cross(cur))\n\t})\n\n\turequire.AbortsWithMessage(t, cur, \"Form is closed\", func() {\n\t\tvar formID string\n\t\tvar err error\n\t\tfunc(cur realm) {\n\t\t\tformID, err = db.CreateForm(0, cur, \"Test Form\", \"Test Description\", \"\", yesterday, data)\n\t\t}(cross(cur))\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t}\n\t\tfunc(cur realm) { db.SubmitForm(0, cur, formID, answers) }(cross(cur))\n\t})\n\n\turequire.NotAborts(t, cur, func() {\n\t\tvar formID string\n\t\tvar err error\n\t\tfunc(cur realm) {\n\t\t\tformID, err = db.CreateForm(0, cur, \"Test Form\", \"Test Description\", yesterday, tomorrow, data)\n\t\t}(cross(cur))\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tfunc(cur realm) { db.SubmitForm(0, cur, formID, answers) }(cross(cur))\n\t})\n}\n"},{"name":"validate.gno","body":"package forms\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/onbloc/json\"\n)\n\nfunc validateBooleanField(node *json.Node, field Field) bool {\n\tif node.IsBool() == false {\n\t\treturn false\n\t}\n\n\tanswer, err := node.GetBool()\n\tif err != nil {\n\t\treturn false\n\t}\n\n\t// If the field is required, checkbox must be checked\n\tif field.Required == true \u0026\u0026 answer == false {\n\t\treturn false\n\t}\n\n\treturn true\n}\n\nfunc validateStringField(node *json.Node, field Field) bool {\n\tif node.IsString() == false {\n\t\treturn false\n\t}\n\n\tanswer, err := node.GetString()\n\tif err != nil {\n\t\treturn false\n\t}\n\n\t// If the field is required, the answer must not be empty\n\tif field.Required == true \u0026\u0026 answer == \"\" {\n\t\treturn false\n\t}\n\n\treturn true\n}\n\nfunc validateNumberField(node *json.Node, field Field) bool {\n\tif node.IsNumber() == false {\n\t\treturn false\n\t}\n\n\t_, err := node.GetNumeric()\n\tif err != nil {\n\t\treturn false\n\t}\n\n\treturn true\n}\n\nfunc validateMultiChoiceField(node *json.Node, field Field) bool {\n\tchoices := strings.Split(field.FieldType[1:len(field.FieldType)-1], \"|\")\n\n\tif node.IsArray() == false {\n\t\treturn false\n\t}\n\n\tif field.Required == true \u0026\u0026 node.Size() == 0 {\n\t\treturn false\n\t}\n\n\tif node.Size() \u003e len(choices) {\n\t\treturn false\n\t}\n\n\tfor i := 0; i \u003c node.Size(); i++ {\n\t\tchoiceNode, err := node.GetIndex(i)\n\t\tif err != nil {\n\t\t\treturn false\n\t\t}\n\n\t\tchoiceIdx := choiceNode.MustNumeric()\n\t\tif choiceIdx \u003c 0 || int(choiceIdx) \u003e= len(choices) {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n\nfunc validateChoiceField(node *json.Node, field Field) bool {\n\tchoices := strings.Split(field.FieldType[1:len(field.FieldType)-1], \"|\")\n\n\tif node.IsNumber() == false {\n\t\treturn false\n\t}\n\n\tchoiceIdx := node.MustNumeric()\n\tif choiceIdx \u003c 0 || int(choiceIdx) \u003e= len(choices) {\n\t\treturn false\n\t}\n\n\treturn true\n}\n\nfunc ValidateAnswer(answer *json.Node, field Field) bool {\n\tif field.FieldType == \"boolean\" {\n\t\treturn validateBooleanField(answer, field)\n\t} else if field.FieldType == \"string\" {\n\t\treturn validateStringField(answer, field)\n\t} else if field.FieldType == \"number\" {\n\t\treturn validateNumberField(answer, field)\n\t} else if strings.HasPrefix(field.FieldType, \"{\") \u0026\u0026 strings.HasSuffix(field.FieldType, \"}\") {\n\t\treturn validateMultiChoiceField(answer, field)\n\t} else if strings.HasPrefix(field.FieldType, \"[\") \u0026\u0026 strings.HasSuffix(field.FieldType, \"]\") {\n\t\treturn validateChoiceField(answer, field)\n\t}\n\n\treturn false\n}\n\n// ValidateAnswers checks if the given answers are valid for the given fields\nfunc ValidateAnswers(answers string, fields []Field) bool {\n\tunmarshalled, err := json.Unmarshal([]byte(answers))\n\tif err != nil {\n\t\treturn false\n\t}\n\n\t// If the number of answers is different from the number of fields, it's invalid\n\tif len(fields) != unmarshalled.Size() {\n\t\treturn false\n\t}\n\n\tfor i, field := range fields {\n\t\tanswer, err := unmarshalled.GetIndex(i)\n\t\tif err != nil {\n\t\t\treturn false\n\t\t}\n\n\t\t// If the answer is empty and the field is not required, it's valid\n\t\tif answer.IsNull() \u0026\u0026 !field.Required {\n\t\t\treturn true\n\t\t}\n\n\t\tif !ValidateAnswer(answer, field) {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n"},{"name":"validate_test.gno","body":"package forms\n\nimport (\n\t\"testing\"\n)\n\nfunc TestAnswerFormInvalidForm(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\tdb := NewDB()\n\n\tdataAllTypes := `[\n\t\t{\n\t\t\t\"label\": \"Name\",\n\t\t\t\"fieldType\": \"string\",\n\t\t\t\"required\": true\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Age\",\n\t\t\t\"fieldType\": \"number\",\n\t\t\t\"required\": false\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Is this a test?\",\n\t\t\t\"fieldType\": \"boolean\",\n\t\t\t\"required\": false\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Favorite Food\",\n\t\t\t\"fieldType\": \"[Pizza|Schnitzel|Burger]\",\n\t\t\t\"required\": true\n\t\t},\n\t\t{\n\t\t\t\"label\": \"Favorite Foods\",\n\t\t\t\"fieldType\": \"{Pizza|Schnitzel|Burger}\",\n\t\t\t\"required\": true\n\t\t}\n\t]`\n\tdataOneRequiredText := `[\n\t\t{\n\t\t\t\"label\": \"Name\",\n\t\t\t\"fieldType\": \"string\",\n\t\t\t\"required\": true\n\t\t}\n\t]`\n\n\ttests := []struct {\n\t\tname        string\n\t\tanswer      string\n\t\texpectPanic bool\n\t\tdata        string\n\t}{\n\t\t{\n\t\t\tname:        \"correct\",\n\t\t\tanswer:      `[\"Alex\", 21, true, 0, [0, 1]]`,\n\t\t\texpectPanic: false,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"invalid string\",\n\t\t\tanswer:      `[0, 21, true, 0, [0, 1]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"invalid number\",\n\t\t\tanswer:      `[\"Alex\", \"21\", true, 0, [0, 1]]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"invalid boolean\",\n\t\t\tanswer:      `[\"Alex\", 21, 1, 0, [0, 1]]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"invalid choice\",\n\t\t\tanswer:      `[\"Alex\", 21, true, 10, [0, 1]]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"invalid multi-choice 1\",\n\t\t\tanswer:      `[\"Alex\", 21, true, 0, [0, 1, 2, 3, 4, 5]]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"invalid multi-choice 2\",\n\t\t\tanswer:      `[\"Alex\", 21, true, 0, [5]]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"invalid multi-choice 3\",\n\t\t\tanswer:      `[\"Alex\", 21, true, 0, 0]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"required string\",\n\t\t\tanswer:      `[\"\", 21, true, 0, [0, 1]]`,\n\t\t\texpectPanic: true,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"unrequired number\",\n\t\t\tanswer:      `[\"Alex\", null, true, 0, [0, 1]]`,\n\t\t\texpectPanic: false,\n\t\t\tdata:        dataAllTypes,\n\t\t},\n\t\t{\n\t\t\tname:        \"correct one field\",\n\t\t\tanswer:      `[\"Alex\"]`,\n\t\t\texpectPanic: false,\n\t\t\tdata:        dataOneRequiredText,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar formID string\n\t\t\tvar err error\n\t\t\tfunc(cur realm) {\n\t\t\t\tformID, err = db.CreateForm(0, cur, \"Test Form\", \"Test Description\", \"\", \"\", tt.data)\n\t\t\t}(cross(cur))\n\t\t\tif err != nil {\n\t\t\t\tt.Error(err)\n\t\t\t}\n\n\t\t\t// SubmitForm crosses a realm boundary (cross(cur)), so a\n\t\t\t// panic from it becomes a cross-realm \"abort\". A plain\n\t\t\t// defer-recover won't see it; use revive() instead.\n\t\t\tr := revive(func() {\n\t\t\t\tfunc(cur realm) { db.SubmitForm(0, cur, formID, tt.answer) }(cross(cur))\n\t\t\t})\n\t\t\tif r != nil \u0026\u0026 !tt.expectPanic {\n\t\t\t\tt.Errorf(\"%s panic occurred when not expected: %v\", tt.name, r)\n\t\t\t} else if r == nil \u0026\u0026 tt.expectPanic {\n\t\t\t\tt.Errorf(\"%s expected panic but didn't occur\", tt.name)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"0szueUgEtlpsA2byG0TehFtxi49zajDYq5ox3RdlnCaAz/UnxyDeis9hpOmlvtGdGIbB2Y00KLry4ZujNThrBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"acl","path":"gno.land/p/archive/acl","files":[{"name":"acl.gno","body":"package acl\n\nimport \"gno.land/p/nt/avl/v0\"\n\nfunc New() *Directory {\n\treturn \u0026Directory{\n\t\tuserGroups:  avl.Tree{},\n\t\tpermBuckets: avl.Tree{},\n\t}\n}\n\ntype Directory struct {\n\tpermBuckets avl.Tree // identifier -\u003e perms\n\tuserGroups  avl.Tree // chain.Address -\u003e []string\n}\n\nfunc (d *Directory) HasPerm(addr address, verb, resource string) bool {\n\t// FIXME: consider memoize.\n\n\t// user perms\n\tif d.getBucketPerms(\"u:\"+addr.String()).hasPerm(verb, resource) {\n\t\treturn true\n\t}\n\n\t// everyone's perms.\n\tif d.getBucketPerms(\"g:\"+Everyone).hasPerm(verb, resource) {\n\t\treturn true\n\t}\n\n\t// user groups' perms.\n\tgroups := d.userGroups.Get(addr.String())\n\tif groups != nil {\n\t\tfor _, group := range groups.([]string) {\n\t\t\tif d.getBucketPerms(\"g:\"+group).hasPerm(verb, resource) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t}\n\n\treturn false\n}\n\nfunc (d *Directory) getBucketPerms(bucket string) perms {\n\tres := d.permBuckets.Get(bucket)\n\tif res != nil {\n\t\treturn res.(perms)\n\t}\n\treturn perms{}\n}\n\nfunc (d *Directory) HasRole(addr address, role string) bool {\n\treturn d.HasPerm(addr, \"role\", role)\n}\n\nfunc (d *Directory) AddUserPerm(addr address, verb, resource string) {\n\tbucket := \"u:\" + addr.String()\n\tp := perm{\n\t\tverbs:     []string{verb},\n\t\tresources: []string{resource},\n\t}\n\td.addPermToBucket(bucket, p)\n}\n\nfunc (d *Directory) AddGroupPerm(name string, verb, resource string) {\n\tbucket := \"g:\" + name\n\tp := perm{\n\t\tverbs:     []string{verb},\n\t\tresources: []string{resource},\n\t}\n\td.addPermToBucket(bucket, p)\n}\n\nfunc (d *Directory) addPermToBucket(bucket string, p perm) {\n\tvar ps perms\n\n\texisting := d.permBuckets.Get(bucket)\n\tif existing != nil {\n\t\tps = existing.(perms)\n\t}\n\tps = append(ps, p)\n\n\td.permBuckets.Set(bucket, ps)\n}\n\nfunc (d *Directory) AddUserToGroup(user address, group string) {\n\texisting := d.userGroups.Get(user.String())\n\tvar groups []string\n\tif existing != nil {\n\t\tgroups = existing.([]string)\n\t}\n\tgroups = append(groups, group)\n\td.userGroups.Set(user.String(), groups)\n}\n\n// TODO: helpers to remove permissions.\n// TODO: helpers to adds multiple permissions at once -\u003e {verbs: []string{\"read\",\"write\"}}.\n// TODO: helpers to delete users from gorups.\n// TODO: helpers to quickly reset states.\n"},{"name":"acl_test.gno","body":"package acl\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc Test(t *testing.T) {\n\tadm := testutils.TestAddress(\"admin\")\n\tmod := testutils.TestAddress(\"mod\")\n\tusr := testutils.TestAddress(\"user\")\n\tcst := testutils.TestAddress(\"custom\")\n\n\tdir := New()\n\n\t// by default, no one has perm.\n\tshouldNotHasRole(t, dir, adm, \"foo\")\n\tshouldNotHasRole(t, dir, mod, \"foo\")\n\tshouldNotHasRole(t, dir, usr, \"foo\")\n\tshouldNotHasRole(t, dir, cst, \"foo\")\n\tshouldNotHasPerm(t, dir, adm, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, mod, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, cst, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, adm, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, mod, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, cst, \"read\", \"r/demo/boards:gnolang/1\")\n\n\t// adding all the rights to admin.\n\tdir.AddUserPerm(adm, \".*\", \".*\")\n\tshouldHasRole(t, dir, adm, \"foo\")\n\tshouldNotHasRole(t, dir, mod, \"foo\")\n\tshouldNotHasRole(t, dir, usr, \"foo\")\n\tshouldNotHasRole(t, dir, cst, \"foo\")\n\tshouldHasPerm(t, dir, adm, \"write\", \"r/demo/boards:gnolang/1\") // new\n\tshouldNotHasPerm(t, dir, mod, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, cst, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, adm, \"read\", \"r/demo/boards:gnolang/1\") // new\n\tshouldNotHasPerm(t, dir, mod, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, cst, \"read\", \"r/demo/boards:gnolang/1\")\n\n\t// adding custom regexp rule for user \"cst\".\n\tdir.AddUserPerm(cst, \"write\", \"r/demo/boards:gnolang/.*\")\n\tshouldHasRole(t, dir, adm, \"foo\")\n\tshouldNotHasRole(t, dir, mod, \"foo\")\n\tshouldNotHasRole(t, dir, usr, \"foo\")\n\tshouldNotHasRole(t, dir, cst, \"foo\")\n\tshouldHasPerm(t, dir, adm, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, mod, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, cst, \"write\", \"r/demo/boards:gnolang/1\") // new\n\tshouldHasPerm(t, dir, adm, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, mod, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, cst, \"read\", \"r/demo/boards:gnolang/1\")\n\n\t// adding a group perm for a new group.\n\t// no changes expected.\n\tdir.AddGroupPerm(\"mods\", \"role\", \"moderator\")\n\tdir.AddGroupPerm(\"mods\", \"write\", \".*\")\n\tshouldHasRole(t, dir, adm, \"foo\")\n\tshouldNotHasRole(t, dir, mod, \"foo\")\n\tshouldNotHasRole(t, dir, usr, \"foo\")\n\tshouldNotHasRole(t, dir, cst, \"foo\")\n\tshouldHasPerm(t, dir, adm, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, mod, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, cst, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, adm, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, mod, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, cst, \"read\", \"r/demo/boards:gnolang/1\")\n\n\t// assigning the user \"mod\" to the \"mods\" group.\n\tdir.AddUserToGroup(mod, \"mods\")\n\tshouldHasRole(t, dir, adm, \"foo\")\n\tshouldNotHasRole(t, dir, mod, \"foo\")\n\tshouldNotHasRole(t, dir, usr, \"foo\")\n\tshouldNotHasRole(t, dir, cst, \"foo\")\n\tshouldHasPerm(t, dir, adm, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, mod, \"write\", \"r/demo/boards:gnolang/1\") // new\n\tshouldNotHasPerm(t, dir, usr, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, cst, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, adm, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, mod, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, cst, \"read\", \"r/demo/boards:gnolang/1\")\n\n\t// adding \"read\" permission for everyone.\n\tdir.AddGroupPerm(Everyone, \"read\", \".*\")\n\tshouldHasRole(t, dir, adm, \"foo\")\n\tshouldNotHasRole(t, dir, mod, \"foo\")\n\tshouldNotHasRole(t, dir, usr, \"foo\")\n\tshouldNotHasRole(t, dir, cst, \"foo\")\n\tshouldHasPerm(t, dir, adm, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, mod, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldNotHasPerm(t, dir, usr, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, cst, \"write\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, adm, \"read\", \"r/demo/boards:gnolang/1\")\n\tshouldHasPerm(t, dir, mod, \"read\", \"r/demo/boards:gnolang/1\") // new\n\tshouldHasPerm(t, dir, usr, \"read\", \"r/demo/boards:gnolang/1\") // new\n\tshouldHasPerm(t, dir, cst, \"read\", \"r/demo/boards:gnolang/1\") // new\n}\n\nfunc shouldHasRole(t *testing.T, dir *Directory, addr address, role string) {\n\tt.Helper()\n\tcheck := dir.HasRole(addr, role)\n\tuassert.Equal(t, true, check, ufmt.Sprintf(\"%s should has role %s\", addr.String(), role))\n}\n\nfunc shouldNotHasRole(t *testing.T, dir *Directory, addr address, role string) {\n\tt.Helper()\n\tcheck := dir.HasRole(addr, role)\n\tuassert.Equal(t, false, check, ufmt.Sprintf(\"%s should not has role %s\", addr.String(), role))\n}\n\nfunc shouldHasPerm(t *testing.T, dir *Directory, addr address, verb string, resource string) {\n\tt.Helper()\n\tcheck := dir.HasPerm(addr, verb, resource)\n\tuassert.Equal(t, true, check, ufmt.Sprintf(\"%s should has perm for %s - %s\", addr.String(), verb, resource))\n}\n\nfunc shouldNotHasPerm(t *testing.T, dir *Directory, addr address, verb string, resource string) {\n\tt.Helper()\n\tcheck := dir.HasPerm(addr, verb, resource)\n\tuassert.Equal(t, false, check, ufmt.Sprintf(\"%s should not has perm for %s - %s\", addr.String(), verb, resource))\n}\n"},{"name":"const.gno","body":"package acl\n\nconst Everyone string = \"everyone\"\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/archive/acl\"\ngno = \"0.9\"\n"},{"name":"perm.gno","body":"package acl\n\nimport \"regexp\"\n\ntype perm struct {\n\tverbs     []string\n\tresources []string\n}\n\nfunc (perm perm) hasPerm(verb, resource string) bool {\n\t// check verb\n\tverbOK := false\n\tfor _, pattern := range perm.verbs {\n\t\tif match(pattern, verb) {\n\t\t\tverbOK = true\n\t\t\tbreak\n\t\t}\n\t}\n\tif !verbOK {\n\t\treturn false\n\t}\n\n\t// check resource\n\tfor _, pattern := range perm.resources {\n\t\tif match(pattern, resource) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc match(pattern, target string) bool {\n\tif pattern == \".*\" {\n\t\treturn true\n\t}\n\n\tif pattern == target {\n\t\treturn true\n\t}\n\n\t// regexp handling\n\tmatch, _ := regexp.MatchString(pattern, target)\n\treturn match\n}\n"},{"name":"perms.gno","body":"package acl\n\ntype perms []perm\n\nfunc (perms perms) hasPerm(verb, resource string) bool {\n\tfor _, perm := range perms {\n\t\tif perm.hasPerm(verb, resource) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"JELktOTE9IdYBwIZccwyZ76ojQKefuhWqv8ucOBlefet2gDwX3stRO7u1UAVZcG02i8rfDB4fvwAqtkLnLFqBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"bank","path":"gno.land/p/archive/bank","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/archive/bank\"\ngno = \"0.9\"\n"},{"name":"types.gno","body":"// TODO: this is an example, and needs to be fixed up and tested.\n\npackage bank\n\n// NOTE: unexposed struct for security.\ntype order struct {\n\tfrom      Address\n\tto        Address\n\tamount    Coins\n\tprocessed bool\n}\n\n// NOTE: unexposed methods for security.\nfunc (ch *order) string() string {\n\treturn \"TODO\"\n}\n\n// Wraps the internal *order for external use.\ntype Order struct {\n\t*order\n}\n\n// XXX only exposed for demonstration. TODO unexpose, make full demo.\nfunc NewOrder(from Address, to Address, amount Coins) Order {\n\treturn Order{\n\t\torder: \u0026order{\n\t\t\tfrom:   from,\n\t\t\tto:     to,\n\t\t\tamount: amount,\n\t\t},\n\t}\n}\n\n// Panics if error, or already processed.\nfunc (o Order) Execute() {\n\tif o.order.processed {\n\t\tpanic(\"order already processed\")\n\t}\n\to.order.processed = true\n\t// TODO implemement.\n}\n\nfunc (o Order) IsZero() bool {\n\treturn o.order == nil\n}\n\nfunc (o Order) From() Address {\n\treturn o.order.from\n}\n\nfunc (o Order) To() Address {\n\treturn o.order.to\n}\n\nfunc (o Order) Amount() Coins {\n\treturn o.order.amount\n}\n\nfunc (o Order) Processed() bool {\n\treturn o.order.processed\n}\n\n//----------------------------------------\n// Escrow\n\ntype EscrowTerms struct {\n\tPartyA  Address\n\tPartyB  Address\n\tAmountA Coins\n\tAmountB Coins\n}\n\ntype EscrowContract struct {\n\tEscrowTerms\n\tOrderA Order\n\tOrderB Order\n}\n\nfunc CreateEscrow(terms EscrowTerms) *EscrowContract {\n\treturn \u0026EscrowContract{\n\t\tEscrowTerms: terms,\n\t}\n}\n\nfunc (esc *EscrowContract) SetOrderA(order Order) {\n\tif !esc.OrderA.IsZero() {\n\t\tpanic(\"order-a already set\")\n\t}\n\tif esc.EscrowTerms.PartyA != order.From() {\n\t\tpanic(\"invalid order-a:from mismatch\")\n\t}\n\tif esc.EscrowTerms.PartyB != order.To() {\n\t\tpanic(\"invalid order-a:to mismatch\")\n\t}\n\tif !esc.EscrowTerms.AmountA.Equal(order.Amount()) {\n\t\tpanic(\"invalid order-a amount\")\n\t}\n\tesc.OrderA = order\n}\n\nfunc (esc *EscrowContract) SetOrderB(order Order) {\n\tif !esc.OrderB.IsZero() {\n\t\tpanic(\"order-b already set\")\n\t}\n\tif esc.EscrowTerms.PartyB != order.From() {\n\t\tpanic(\"invalid order-b:from mismatch\")\n\t}\n\tif esc.EscrowTerms.PartyA != order.To() {\n\t\tpanic(\"invalid order-b:to mismatch\")\n\t}\n\tif !esc.EscrowTerms.AmountB.Equal(order.Amount()) {\n\t\tpanic(\"invalid order-b amount\")\n\t}\n\tesc.OrderA = order\n}\n\nfunc (esc *EscrowContract) Execute() {\n\tif esc.OrderA.IsZero() {\n\t\tpanic(\"order-a not yet set\")\n\t}\n\tif esc.OrderB.IsZero() {\n\t\tpanic(\"order-b not yet set\")\n\t}\n\t// NOTE: succeeds atomically.\n\tesc.OrderA.Execute()\n\tesc.OrderB.Execute()\n}\n\n//----------------------------------------\n// TODO: actually implement these in std package.\n\ntype (\n\tAddress string\n\tCoins   []Coin\n\tCoin    struct {\n\t\tDenom  bool\n\t\tAmount int64\n\t}\n)\n\nfunc (a Coins) Equal(b Coins) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i, v := range a {\n\t\tif v != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"rOCTc+8WzbljqWCX2weNf/shJY7RJIFfgEqQvh27G4TBCCZ/ODoQMd5gUj52MqM1RQIn+hq8eQtUZwXaCbb4Bw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"gnode","path":"gno.land/p/archive/gnode","files":[{"name":"gnode.gno","body":"package gnode\n\n// XXX what about Gnodes signing on behalf of others?\n// XXX like a multi-sig of Gnodes?\n\ntype Name string\n\ntype Gnode interface {\n\t//----------------------------------------\n\t// Basic properties\n\tGetName() Name\n\n\t//----------------------------------------\n\t// Affiliate Gnodes\n\tNumAffiliates() int\n\tGetAffiliates(Name) Affiliate\n\tAddAffiliate(Affiliate) error // must be affiliated\n\tRemAffiliate(Name) error      // must have become unaffiliated\n\n\t//----------------------------------------\n\t// Signing\n\tNumSignedDocuments() int\n\tGetSignedDocument(idx int) Document\n\tSignDocument(doc Document) (int, error) // index relative to signer\n\n\t//----------------------------------------\n\t// Rendering\n\tRenderLines() []string\n}\n\ntype Affiliate struct {\n\tType  string\n\tGnode Gnode\n\tTags  []string\n}\n\ntype MyGnode struct {\n\tName\n\t// Owners     // voting set, something that gives authority of action.\n\t// Treasury   //\n\t// Affiliates //\n\t// Board      // discussions\n\t// Data       // XXX ?\n}\n\ntype Affiliates []*Affiliate\n\n// Documents are equal if they compare equal.\n// NOTE: requires all fields to be comparable.\ntype Document struct {\n\tAuthors string\n\t// Timestamp\n\t// Body\n\t// Attachments\n}\n\n// ACTIONS\n\n// * Lend tokens\n// * Pay tokens\n// * Administrate transferrable and non-transferrable tokens\n// * Sum tokens\n// * Passthrough dependencies\n// * Code\n// * ...\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/archive/gnode\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"r59dbD4KnciLkfUUCZ8ebjqcCSuUVqsJ8505WFDNKKYZQEvEPid0xH3syVw18qWAtQIVHQySUATwztHN1+6YBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"rat","path":"gno.land/p/archive/rat","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/archive/rat\"\ngno = \"0.9\"\n"},{"name":"maths.gno","body":"package rat\n\nconst (\n\tintSize = 32 \u003c\u003c (^uint(0) \u003e\u003e 63) // 32 or 64\n\n\tMaxInt    = 1\u003c\u003c(intSize-1) - 1\n\tMinInt    = -1 \u003c\u003c (intSize - 1)\n\tMaxInt8   = 1\u003c\u003c7 - 1\n\tMinInt8   = -1 \u003c\u003c 7\n\tMaxInt16  = 1\u003c\u003c15 - 1\n\tMinInt16  = -1 \u003c\u003c 15\n\tMaxInt32  = 1\u003c\u003c31 - 1\n\tMinInt32  = -1 \u003c\u003c 31\n\tMaxInt64  = 1\u003c\u003c63 - 1\n\tMinInt64  = -1 \u003c\u003c 63\n\tMaxUint   = 1\u003c\u003cintSize - 1\n\tMaxUint8  = 1\u003c\u003c8 - 1\n\tMaxUint16 = 1\u003c\u003c16 - 1\n\tMaxUint32 = 1\u003c\u003c32 - 1\n\tMaxUint64 = 1\u003c\u003c64 - 1\n)\n"},{"name":"rat.gno","body":"package rat\n\n//----------------------------------------\n// Rat fractions\n\n// represents a fraction.\ntype Rat struct {\n\tX int32\n\tY int32 // must be positive\n}\n\nfunc NewRat(x, y int32) Rat {\n\tif y \u003c= 0 {\n\t\tpanic(\"invalid std.Rat denominator\")\n\t}\n\treturn Rat{X: x, Y: y}\n}\n\nfunc (r1 Rat) IsValid() bool {\n\tif r1.Y \u003c= 0 {\n\t\treturn false\n\t}\n\treturn true\n}\n\nfunc (r1 Rat) Cmp(r2 Rat) int {\n\tif !r1.IsValid() {\n\t\tpanic(\"invalid std.Rat left operand\")\n\t}\n\tif !r2.IsValid() {\n\t\tpanic(\"invalid std.Rat right operand\")\n\t}\n\tvar p1, p2 int64\n\tp1 = int64(r1.X) * int64(r2.Y)\n\tp2 = int64(r1.Y) * int64(r2.X)\n\tif p1 \u003c p2 {\n\t\treturn -1\n\t} else if p1 == p2 {\n\t\treturn 0\n\t} else {\n\t\treturn 1\n\t}\n}\n\n//func (r1 Rat) Plus(r2 Rat) Rat {\n// XXX\n//}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Tnh9i5pRQyOQ6hBTP/Ne/jnbAajKVTBlId3zNLYt6SwEY+d0fYX9TuqD5KK1YeeHolm1HXD0d2znCohXGGXPAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"groups","path":"gno.land/p/archive/groups","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/archive/groups\"\ngno = \"0.9\"\n"},{"name":"vote_set.gno","body":"package groups\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/archive/rat\"\n)\n\n//----------------------------------------\n// VoteSet\n\ntype VoteSet interface {\n\t// number of present votes in set.\n\tSize() int\n\t// add or update vote for voter.\n\tSetVote(voter address, value string) error\n\t// count the number of votes for value.\n\tCountVotes(value string) int\n}\n\n//----------------------------------------\n// VoteList\n\ntype Vote struct {\n\tVoter address\n\tValue string\n}\n\ntype VoteList []Vote\n\nfunc NewVoteList() *VoteList {\n\treturn \u0026VoteList{}\n}\n\nfunc (vlist *VoteList) Size() int {\n\treturn len(*vlist)\n}\n\nfunc (vlist *VoteList) SetVote(voter address, value string) error {\n\t// TODO optimize with binary algorithm\n\tfor i, vote := range *vlist {\n\t\tif vote.Voter == voter {\n\t\t\t// update vote\n\t\t\t(*vlist)[i] = Vote{\n\t\t\t\tVoter: voter,\n\t\t\t\tValue: value,\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\t}\n\t*vlist = append(*vlist, Vote{\n\t\tVoter: voter,\n\t\tValue: value,\n\t})\n\treturn nil\n}\n\nfunc (vlist *VoteList) CountVotes(target string) int {\n\t// TODO optimize with binary algorithm\n\tvar count int\n\tfor _, vote := range *vlist {\n\t\tif vote.Value == target {\n\t\t\tcount++\n\t\t}\n\t}\n\treturn count\n}\n\n//----------------------------------------\n// Committee\n\ntype Committee struct {\n\tQuorum    rat.Rat\n\tThreshold rat.Rat\n\tAddresses []address\n}\n\n//----------------------------------------\n// VoteSession\n// NOTE: this seems a bit too formal and\n// complicated vs what might be possible;\n// something simpler, more informal.\n\ntype SessionStatus int\n\nconst (\n\tSessionNew SessionStatus = iota\n\tSessionStarted\n\tSessionCompleted\n\tSessionCanceled\n)\n\ntype VoteSession struct {\n\tName      string\n\tCreator   address\n\tBody      string\n\tStart     time.Time\n\tDeadline  time.Time\n\tStatus    SessionStatus\n\tCommittee *Committee\n\tVotes     VoteSet\n\tChoices   []string\n\tResult    string\n}\n"},{"name":"z_1_filetest.gno","body":"// PKGPATH: gno.land/p/archive/groups_test\npackage groups_test\n\nimport (\n\t\"gno.land/p/archive/groups\"\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nvar vset groups.VoteSet\n\nfunc init() {\n\taddr1 := testutils.TestAddress(\"test1\")\n\taddr2 := testutils.TestAddress(\"test2\")\n\tvset = groups.NewVoteList()\n\tvset.SetVote(addr1, \"yes\")\n\tvset.SetVote(addr2, \"yes\")\n}\n\nfunc main() {\n\tprintln(vset.Size())\n\tprintln(\"yes:\", vset.CountVotes(\"yes\"))\n\tprintln(\"no:\", vset.CountVotes(\"no\"))\n}\n\n// Output:\n// 2\n// yes: 2\n// no: 0\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"8xQhpgLSRgz3A5UpUXJbVmgWlRzgwMRQOylUoQDC3MWN+NIvYT9dMSXM2hBoPGjyLIkUcx5OlgfsIEUtk473Bw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ui","path":"gno.land/p/archive/ui","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/archive/ui\"\ngno = \"0.9\"\n"},{"name":"ui.gno","body":"package ui\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n)\n\ntype DOM struct {\n\t// metadata\n\tPrefix       string\n\tTitle        string\n\tWithComments bool\n\tClasses      []string\n\n\t// elements\n\tHeader Element\n\tBody   Element\n\tFooter Element\n}\n\nfunc (dom DOM) String() string {\n\tclasses := strings.Join(dom.Classes, \" \")\n\n\toutput := \"\"\n\n\tif classes != \"\" {\n\t\toutput += \"\u003cmain class='\" + classes + \"'\u003e\" + \"\\n\\n\"\n\t}\n\n\tif dom.Title != \"\" {\n\t\toutput += H1(dom.Title).String(dom) + \"\\n\"\n\t}\n\n\tif header := dom.Header.String(dom); header != \"\" {\n\t\tif dom.WithComments {\n\t\t\toutput += \"\u003c!-- header --\u003e\"\n\t\t}\n\t\toutput += header + \"\\n\"\n\t\tif dom.WithComments {\n\t\t\toutput += \"\u003c!-- /header --\u003e\"\n\t\t}\n\t}\n\n\tif body := dom.Body.String(dom); body != \"\" {\n\t\tif dom.WithComments {\n\t\t\toutput += \"\u003c!-- body --\u003e\"\n\t\t}\n\t\toutput += body + \"\\n\"\n\t\tif dom.WithComments {\n\t\t\toutput += \"\u003c!-- /body --\u003e\"\n\t\t}\n\t}\n\n\tif footer := dom.Footer.String(dom); footer != \"\" {\n\t\tif dom.WithComments {\n\t\t\toutput += \"\u003c!-- footer --\u003e\"\n\t\t}\n\t\toutput += footer + \"\\n\"\n\t\tif dom.WithComments {\n\t\t\toutput += \"\u003c!-- /footer --\u003e\"\n\t\t}\n\t}\n\n\tif classes != \"\" {\n\t\toutput += \"\u003c/main\u003e\"\n\t}\n\n\t// TODO: cleanup double new-lines.\n\n\treturn output\n}\n\ntype Jumbotron []DomStringer\n\nfunc (j Jumbotron) String(dom DOM) string {\n\toutput := `\u003cdiv class=\"jumbotron\"\u003e` + \"\\n\\n\"\n\tfor _, elem := range j {\n\t\toutput += elem.String(dom) + \"\\n\"\n\t}\n\toutput += `\u003c/div\u003e\u003c!-- /jumbotron --\u003e` + \"\\n\"\n\treturn output\n}\n\n// XXX: rename Element to Div?\ntype Element []DomStringer\n\nfunc (e *Element) Append(elems ...DomStringer) {\n\t*e = append(*e, elems...)\n}\n\nfunc (e *Element) String(dom DOM) string {\n\toutput := \"\"\n\tfor _, elem := range *e {\n\t\toutput += elem.String(dom) + \"\\n\"\n\t}\n\treturn output\n}\n\ntype Breadcrumb []DomStringer\n\nfunc (b *Breadcrumb) Append(elems ...DomStringer) {\n\t*b = append(*b, elems...)\n}\n\nfunc (b Breadcrumb) String(dom DOM) string {\n\toutput := \"\"\n\tfor idx, entry := range b {\n\t\tif idx \u003e 0 {\n\t\t\toutput += \" / \"\n\t\t}\n\t\toutput += entry.String(dom)\n\t}\n\treturn output\n}\n\ntype Columns struct {\n\tMaxWidth int\n\tColumns  []Element\n}\n\nfunc (c *Columns) Append(elems ...Element) {\n\tc.Columns = append(c.Columns, elems...)\n}\n\nfunc (c Columns) String(dom DOM) string {\n\toutput := `\u003cdiv class=\"columns-` + strconv.Itoa(c.MaxWidth) + `\"\u003e` + \"\\n\"\n\tfor _, entry := range c.Columns {\n\t\toutput += `\u003cdiv class=\"column\"\u003e` + \"\\n\\n\"\n\t\toutput += entry.String(dom)\n\t\toutput += \"\u003c/div\u003e\u003c!-- /column--\u003e\\n\"\n\t}\n\toutput += \"\u003c/div\u003e\u003c!-- /columns-\" + strconv.Itoa(c.MaxWidth) + \" --\u003e\\n\"\n\treturn output\n}\n\ntype Link struct {\n\tText string\n\tPath string\n\tURL  string\n}\n\n// TODO: image\n\n// TODO: pager\n\nfunc (l Link) String(dom DOM) string {\n\turl := \"\"\n\tswitch {\n\tcase l.Path != \"\" \u0026\u0026 l.URL != \"\":\n\t\tpanic(\"a link should have a path or a URL, not both.\")\n\tcase l.Path != \"\":\n\t\tif l.Text == \"\" {\n\t\t\tl.Text = l.Path\n\t\t}\n\t\turl = dom.Prefix + l.Path\n\tcase l.URL != \"\":\n\t\tif l.Text == \"\" {\n\t\t\tl.Text = l.URL\n\t\t}\n\t\turl = l.URL\n\t}\n\n\treturn \"[\" + l.Text + \"](\" + url + \")\"\n}\n\ntype BulletList []DomStringer\n\nfunc (bl BulletList) String(dom DOM) string {\n\toutput := \"\"\n\n\tfor _, entry := range bl {\n\t\toutput += \"- \" + entry.String(dom) + \"\\n\"\n\t}\n\n\treturn output\n}\n\nfunc Text(s string) DomStringer {\n\treturn Raw{Content: s}\n}\n\ntype DomStringer interface {\n\tString(dom DOM) string\n}\n\ntype Raw struct {\n\tContent string\n}\n\nfunc (r Raw) String(_ DOM) string {\n\treturn r.Content\n}\n\ntype (\n\tH1        string\n\tH2        string\n\tH3        string\n\tH4        string\n\tH5        string\n\tH6        string\n\tBold      string\n\tItalic    string\n\tCode      string\n\tParagraph string\n\tQuote     string\n\tHR        struct{}\n)\n\nfunc (text H1) String(_ DOM) string        { return \"# \" + string(text) + \"\\n\" }\nfunc (text H2) String(_ DOM) string        { return \"## \" + string(text) + \"\\n\" }\nfunc (text H3) String(_ DOM) string        { return \"### \" + string(text) + \"\\n\" }\nfunc (text H4) String(_ DOM) string        { return \"#### \" + string(text) + \"\\n\" }\nfunc (text H5) String(_ DOM) string        { return \"##### \" + string(text) + \"\\n\" }\nfunc (text H6) String(_ DOM) string        { return \"###### \" + string(text) + \"\\n\" }\nfunc (text Quote) String(_ DOM) string     { return \"\u003e \" + string(text) + \"\\n\" }\nfunc (text Bold) String(_ DOM) string      { return \"**\" + string(text) + \"**\" }\nfunc (text Italic) String(_ DOM) string    { return \"_\" + string(text) + \"_\" }\nfunc (text Paragraph) String(_ DOM) string { return \"\\n\" + string(text) + \"\\n\" }\nfunc (_ HR) String(_ DOM) string           { return \"\\n---\\n\" }\n\nfunc (text Code) String(_ DOM) string {\n\t// multiline\n\tif strings.Contains(string(text), \"\\n\") {\n\t\treturn \"\\n```\\n\" + string(text) + \"\\n```\\n\"\n\t}\n\n\t// single line\n\treturn \"`\" + string(text) + \"`\"\n}\n"},{"name":"ui_test.gno","body":"package ui\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"nIRmNHWna8gqiyc7OquZkSNED1vh38PP9PMKaHku4G53zJDA7XC9Kc7HA29A3o+aKS4FxT2J0rYJZEbPHKhlAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"bf","path":"gno.land/p/demo/bf","files":[{"name":"bf.gno","body":"package bf\n\nimport (\n\t\"strings\"\n)\n\nconst maxlen = 30000\n\nfunc Execute(code string) string {\n\tvar (\n\t\tmemory  = make([]byte, maxlen) // memory tape\n\t\tpointer = 0                    // initial memory pointer\n\t\tbuf     strings.Builder\n\t)\n\n\t// Loop through each character in the code\n\tfor i := 0; i \u003c len(code); i++ {\n\t\tswitch code[i] {\n\t\tcase '\u003e':\n\t\t\t// Increment memory pointer\n\t\t\tpointer++\n\t\t\tif pointer \u003e= maxlen {\n\t\t\t\tpointer = 0\n\t\t\t}\n\t\tcase '\u003c':\n\t\t\t// Decrement memory pointer\n\t\t\tpointer--\n\t\t\tif pointer \u003c 0 {\n\t\t\t\tpointer = maxlen - 1\n\t\t\t}\n\t\tcase '+':\n\t\t\t// Increment the byte at the memory pointer\n\t\t\tmemory[pointer]++\n\t\tcase '-':\n\t\t\t// Decrement the byte at the memory pointer\n\t\t\tmemory[pointer]--\n\t\tcase '.':\n\t\t\t// Output the byte at the memory pointer\n\t\t\tbuf.WriteByte(memory[pointer])\n\t\tcase ',':\n\t\t\t// Input a byte and store it in the memory\n\t\t\tpanic(\"unsupported\")\n\t\t\t// fmt.Scan(\u0026memory[pointer])\n\t\tcase '[':\n\t\t\t// Jump forward past the matching ']' if the byte at the memory pointer is zero\n\t\t\tif memory[pointer] == 0 {\n\t\t\t\tbraceCount := 1\n\t\t\t\tfor braceCount \u003e 0 {\n\t\t\t\t\ti++\n\t\t\t\t\tif code[i] == '[' {\n\t\t\t\t\t\tbraceCount++\n\t\t\t\t\t} else if code[i] == ']' {\n\t\t\t\t\t\tbraceCount--\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\tcase ']':\n\t\t\t// Jump backward to the matching '[' if the byte at the memory pointer is nonzero\n\t\t\tif memory[pointer] != 0 {\n\t\t\t\tbraceCount := 1\n\t\t\t\tfor braceCount \u003e 0 {\n\t\t\t\t\ti--\n\t\t\t\t\tif code[i] == ']' {\n\t\t\t\t\t\tbraceCount++\n\t\t\t\t\t} else if code[i] == '[' {\n\t\t\t\t\t\tbraceCount--\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ti-- // Move back one more to compensate for the upcoming increment in the loop\n\t\t\t}\n\t\t}\n\t}\n\treturn buf.String()\n}\n"},{"name":"bf_test.gno","body":"package bf\n\nimport \"testing\"\n\nfunc TestExecuteBrainfuck(t *testing.T) {\n\ttestCases := []struct {\n\t\tname     string\n\t\tcode     string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"hello\",\n\t\t\tcode:     \"++++++++++[\u003e+++++++\u003e++++++++++\u003e+++\u003e+\u003c\u003c\u003c\u003c-]\u003e++.\u003e+.+++++++..+++.\u003e++.\u003c\u003c+++++++++++++++.\u003e.+++.------.--------.\",\n\t\t\texpected: \"Hello World\",\n\t\t},\n\t\t{\n\t\t\tname:     \"increment\",\n\t\t\tcode:     \"+++++ +++++ [ \u003e +++++ ++ \u003c - ] \u003e +++++ .\",\n\t\t\texpected: \"K\",\n\t\t},\n\t\t// Add more test cases as needed\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tresult := Execute(tc.code)\n\t\t\tif result != tc.expected {\n\t\t\t\tt.Errorf(\"Expected output: %s, but got: %s\", tc.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"doc.gno","body":"// Package bf implements a minimalist Brainfuck virtual machine in Gno.\n//\n// Brainfuck is an esoteric programming language known for its simplicity and minimalistic design.\n// It operates on an array of memory cells, with a memory pointer that can move left or right.\n// The language consists of eight commands: \u003e \u003c + - . , [ ].\n//\n// Usage:\n// To execute Brainfuck code, use the Execute function and provide the code as a string.\n//\n//\tcode := \"++++++++++[\u003e+++++++\u003e++++++++++\u003e+++\u003e+\u003c\u003c\u003c\u003c-]\u003e++.\u003e+.+++++++..+++.\u003e++.\u003c\u003c+++++++++++++++.\u003e.+++.------.--------.\"\n//\toutput := bf.Execute(code)\n//\n// Note:\n// This implementation is a minimalist version and may not handle all edge cases or advanced features of the Brainfuck language.\n//\n// Reference:\n// For more information on Brainfuck, refer to the Wikipedia page: https://en.wikipedia.org/wiki/Brainfuck\npackage bf // import \"gno.land/p/demo/bf\"\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/bf\"\ngno = \"0.9\"\n"},{"name":"run.gno","body":"package bf\n\n// for `gno run`\nfunc main() {\n\tcode := \"++++++++++[\u003e+++++++\u003e++++++++++\u003e+++\u003e+\u003c\u003c\u003c\u003c-]\u003e++.\u003e+.+++++++..+++.\u003e++.\u003c\u003c+++++++++++++++.\u003e.+++.------.--------.\"\n\t// TODO: code = os.Args...\n\tExecute(code)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"HYFTZ9iw123XEqOvsXGlkXp/6vmERNZYaXpLuIwPQ9+51suIlYqWFlSpAddkRamoUyzElw0uyfD/d43zsCKIBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"btree","path":"gno.land/p/demo/btree","files":[{"name":"btree.gno","body":"//////////\n//\n// Copyright 2014 Google Inc.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n//\n// Copyright 2024 New Tendermint\n//\n// This Gno port of the original Go BTree is substantially rewritten/reimplemented\n// from the original, primarily for clarity of code, clarity of documentation,\n// and for compatibility with Gno.\n//\n// Authors:\n//   Original version authors -- https://github.com/google/btree/graphs/contributors\n//   Kirk Haines \u003cwyhaines@gmail.com\u003e\n//\n//////////\n\n// Package btree implements in-memory B-Trees of arbitrary degree.\n//\n// It has a flatter structure than an equivalent red-black or other binary tree,\n// which may yield better memory usage and/or performance.\npackage btree\n\nimport \"sort\"\n\n//////////\n//\n// Types\n//\n//////////\n\n// BTreeOption is a function interface for setting options on a btree with `New()`.\ntype BTreeOption func(*BTree)\n\n// BTree is an implementation of a B-Tree.\n//\n// BTree stores Record instances in an ordered structure, allowing easy insertion,\n// removal, and iteration.\ntype BTree struct {\n\tdegree int\n\tlength int\n\troot   *node\n\tcowCtx *copyOnWriteContext\n}\n\n//\tAny type that implements this interface can be stored in the BTree. This allows considerable\n//\n// flexiblity in storage within the BTree.\ntype Record interface {\n\t// Less compares self to `than`, returning true if self is less than `than`\n\tLess(than Record) bool\n}\n\n// records is the storage within a node. It is expressed as a slice of Record, where a Record\n// is any struct that implements the Record interface.\ntype records []Record\n\n// node is an internal node in a tree.\n//\n// It must at all times maintain on of the two conditions:\n//   - len(children) == 0, len(records) unconstrained\n//   - len(children) == len(records) + 1\ntype node struct {\n\trecords  records\n\tchildren children\n\tcowCtx   *copyOnWriteContext\n}\n\n// children is the list of child nodes below the current node. It is a slice of nodes.\ntype children []*node\n\n// FreeNodeList represents a slice of nodes which are available for reuse. The default\n// behavior of New() is for each BTree instance to have its own FreeNodeList. However,\n// it is possible for multiple instances of BTree to share the same tree. If one uses\n// New(WithFreeNodeList()) to create a tree, one may pass an existing FreeNodeList, allowing\n// multiple trees to use a single list. In an application with multiple trees, it might\n// be more efficient to allocate a single FreeNodeList with a significant initial capacity,\n// and then have all of the trees use that same large FreeNodeList.\ntype FreeNodeList struct {\n\tnodes []*node\n}\n\n// copyOnWriteContext manages node ownership and ensures that cloned trees\n// maintain isolation from each other when a node is changed.\n//\n// Ownership Rules:\n//   - Each node is associated with a specific copyOnWriteContext.\n//   - A tree can modify a node directly only if the tree's context matches the node's context.\n//   - If a tree attempts to modify a node with a different context, it must create a\n//     new, writable copy of that node (i.e., perform a clone) before making changes.\n//\n// Write Operation Invariant:\n//   - During any write operation, the current node being modified must have the same\n//     context as the tree requesting the write.\n//   - To maintain this invariant, before descending into a child node, the system checks\n//     if the child’s context matches the tree's context.\n//   - If the contexts match, the node can be modified in place.\n//   - If the contexts do not match, a mutable copy of the child node is created with the\n//     correct context before proceeding.\n//\n// Practical Implications:\n//   - The node currently being modified inherits the requesting tree's context, allowing\n//     in-place modifications.\n//   - Child nodes may initially have different contexts. Before any modification, these\n//     children are copied to ensure they share the correct context, enabling safe and\n//     isolated updates without affecting other trees that might be referencing the original nodes.\n//\n// Example Usage:\n// When a tree performs a write operation (e.g., inserting or deleting a node), it uses\n// its copyOnWriteContext to determine whether it can modify nodes directly or needs to\n// create copies. This mechanism ensures that trees can share nodes efficiently while\n// maintaining data integrity.\ntype copyOnWriteContext struct {\n\tnodes *FreeNodeList\n}\n\n// Record implements an interface with a single function, Less. Any type that implements\n// RecordIterator allows callers of all of the iteration functions for the BTree\n// to evaluate an element of the tree as it is traversed. The function will receive\n// a stored element from the tree. The function must return either a true or a false value.\n// True indicates that iteration should continue, while false indicates that it should halt.\ntype RecordIterator func(i Record) bool\n\n//////////\n//\n// Functions\n//\n//////////\n\n// NewFreeNodeList creates a new free list.\n// size is the maximum size of the returned free list.\nfunc NewFreeNodeList(size int) *FreeNodeList {\n\treturn \u0026FreeNodeList{nodes: make([]*node, 0, size)}\n}\n\nfunc (freeList *FreeNodeList) newNode() (nodeInstance *node) {\n\tindex := len(freeList.nodes) - 1\n\tif index \u003c 0 {\n\t\treturn new(node)\n\t}\n\tnodeInstance = freeList.nodes[index]\n\tfreeList.nodes[index] = nil\n\tfreeList.nodes = freeList.nodes[:index]\n\n\treturn nodeInstance\n}\n\n// freeNode adds the given node to the list, returning true if it was added\n// and false if it was discarded.\n\nfunc (freeList *FreeNodeList) freeNode(nodeInstance *node) (nodeWasAdded bool) {\n\tif len(freeList.nodes) \u003c cap(freeList.nodes) {\n\t\tfreeList.nodes = append(freeList.nodes, nodeInstance)\n\t\tnodeWasAdded = true\n\t}\n\treturn\n}\n\n// A default size for the free node list. We might want to run some benchmarks to see if\n// there are any pros or cons to this size versus other sizes. This seems to be a reasonable\n// compromise to reduce GC pressure by reusing nodes where possible, without stacking up too\n// much baggage in a given tree.\nconst DefaultFreeNodeListSize = 32\n\n// WithDegree sets the degree of the B-Tree.\nfunc WithDegree(degree int) BTreeOption {\n\treturn func(bt *BTree) {\n\t\tif degree \u003c= 1 {\n\t\t\tpanic(\"Degrees less than 1 do not make any sense for a BTree. Please provide a degree of 1 or greater.\")\n\t\t}\n\t\tbt.degree = degree\n\t}\n}\n\n// WithFreeNodeList sets a custom free node list for the B-Tree.\nfunc WithFreeNodeList(freeList *FreeNodeList) BTreeOption {\n\treturn func(bt *BTree) {\n\t\tbt.cowCtx = \u0026copyOnWriteContext{nodes: freeList}\n\t}\n}\n\n// New creates a new B-Tree with optional configurations. If configuration is not provided,\n// it will default to 16 element nodes. Degree may not be less than 1 (which effectively\n// makes the tree into a binary tree).\n//\n// `New(WithDegree(2))`, for example, will create a 2-3-4 tree (each node contains 1-3 records\n// and 2-4 children).\n//\n// `New(WithFreeNodeList(NewFreeNodeList(64)))` will create a tree with a degree of 16, and\n// with a free node list with a size of 64.\nfunc New(options ...BTreeOption) *BTree {\n\tbtree := \u0026BTree{\n\t\tdegree: 16, // default degree\n\t\tcowCtx: \u0026copyOnWriteContext{nodes: NewFreeNodeList(DefaultFreeNodeListSize)},\n\t}\n\tfor _, opt := range options {\n\t\topt(btree)\n\t}\n\treturn btree\n}\n\n// insertAt inserts a value into the given index, pushing all subsequent values\n// forward.\nfunc (recordsSlice *records) insertAt(index int, newRecord Record) {\n\toriginalLength := len(*recordsSlice)\n\n\t// Extend the slice by one element\n\t*recordsSlice = append(*recordsSlice, nil)\n\n\t// Move elements from the end to avoid overwriting during the copy\n\t// TODO: Make this work with slice appends, instead. It should be faster?\n\tif index \u003c originalLength {\n\t\tfor position := originalLength; position \u003e index; position-- {\n\t\t\t(*recordsSlice)[position] = (*recordsSlice)[position-1]\n\t\t}\n\t}\n\n\t// Insert the new record\n\t(*recordsSlice)[index] = newRecord\n}\n\n// removeAt removes a Record from the records slice at the specified index.\n// It shifts subsequent records to fill the gap and returns the removed Record.\nfunc (recordSlicePointer *records) removeAt(index int) Record {\n\trecordSlice := *recordSlicePointer\n\tremovedRecord := recordSlice[index]\n\tcopy(recordSlice[index:], recordSlice[index+1:])\n\trecordSlice[len(recordSlice)-1] = nil\n\t*recordSlicePointer = recordSlice[:len(recordSlice)-1]\n\n\treturn removedRecord\n}\n\n// Pop removes and returns the last Record from the records slice.\n// It also clears the reference to the removed Record to aid garbage collection.\nfunc (r *records) pop() Record {\n\trecordSlice := *r\n\tlastIndex := len(recordSlice) - 1\n\tremovedRecord := recordSlice[lastIndex]\n\trecordSlice[lastIndex] = nil\n\t*r = recordSlice[:lastIndex]\n\treturn removedRecord\n}\n\n// This slice is intended only as a supply of records for the truncate function\n// that follows, and it should not be changed or altered.\nvar emptyRecords = make(records, 32)\n\n// truncate reduces the length of the slice to the specified index,\n// and clears the elements beyond that index to prevent memory leaks.\n// The index must be less than or equal to the current length of the slice.\nfunc (originalSlice *records) truncate(index int) {\n\t// Split the slice into the part to keep and the part to clear.\n\trecordsToKeep := (*originalSlice)[:index]\n\trecordsToClear := (*originalSlice)[index:]\n\n\t// Update the original slice to only contain the records to keep.\n\t*originalSlice = recordsToKeep\n\n\t// Clear the memory of the part that was truncated.\n\tfor len(recordsToClear) \u003e 0 {\n\t\t// Copy empty values from `emptyRecords` to the recordsToClear slice.\n\t\t// This effectively \"clears\" the memory by overwriting elements.\n\t\tnumCleared := copy(recordsToClear, emptyRecords)\n\t\trecordsToClear = recordsToClear[numCleared:]\n\t}\n}\n\n// Find determines the appropriate index at which a given Record should be inserted\n// into the sorted records slice. If the Record already exists in the slice,\n// the method returns its index and sets found to true.\n//\n// Parameters:\n// - record: The Record to search for within the records slice.\n//\n// Returns:\n// - insertIndex: The index at which the Record should be inserted.\n// - found: A boolean indicating whether the Record already exists in the slice.\nfunc (recordsSlice records) find(record Record) (insertIndex int, found bool) {\n\ttotalRecords := len(recordsSlice)\n\n\t// Perform a binary search to find the insertion point for the record\n\tinsertionPoint := sort.Search(totalRecords, func(currentIndex int) bool {\n\t\treturn record.Less(recordsSlice[currentIndex])\n\t})\n\n\tif insertionPoint \u003e 0 {\n\t\tpreviousRecord := recordsSlice[insertionPoint-1]\n\n\t\tif !previousRecord.Less(record) {\n\t\t\treturn insertionPoint - 1, true\n\t\t}\n\t}\n\n\treturn insertionPoint, false\n}\n\n// insertAt inserts a value into the given index, pushing all subsequent values\n// forward.\nfunc (childSlice *children) insertAt(index int, n *node) {\n\toriginalLength := len(*childSlice)\n\n\t// Extend the slice by one element\n\t*childSlice = append(*childSlice, nil)\n\n\t// Move elements from the end to avoid overwriting during the copy\n\tif index \u003c originalLength {\n\t\tfor i := originalLength; i \u003e index; i-- {\n\t\t\t(*childSlice)[i] = (*childSlice)[i-1]\n\t\t}\n\t}\n\n\t// Insert the new record\n\t(*childSlice)[index] = n\n}\n\n// removeAt removes a Record from the records slice at the specified index.\n// It shifts subsequent records to fill the gap and returns the removed Record.\nfunc (childSlicePointer *children) removeAt(index int) *node {\n\tchildSlice := *childSlicePointer\n\tremovedChild := childSlice[index]\n\tcopy(childSlice[index:], childSlice[index+1:])\n\tchildSlice[len(childSlice)-1] = nil\n\t*childSlicePointer = childSlice[:len(childSlice)-1]\n\n\treturn removedChild\n}\n\n// Pop removes and returns the last Record from the records slice.\n// It also clears the reference to the removed Record to aid garbage collection.\nfunc (childSlicePointer *children) pop() *node {\n\tchildSlice := *childSlicePointer\n\tlastIndex := len(childSlice) - 1\n\tremovedChild := childSlice[lastIndex]\n\tchildSlice[lastIndex] = nil\n\t*childSlicePointer = childSlice[:lastIndex]\n\treturn removedChild\n}\n\n// This slice is intended only as a supply of records for the truncate function\n// that follows, and it should not be changed or altered.\nvar emptyChildren = make(children, 32)\n\n// truncate reduces the length of the slice to the specified index,\n// and clears the elements beyond that index to prevent memory leaks.\n// The index must be less than or equal to the current length of the slice.\nfunc (originalSlice *children) truncate(index int) {\n\t// Split the slice into the part to keep and the part to clear.\n\tchildrenToKeep := (*originalSlice)[:index]\n\tchildrenToClear := (*originalSlice)[index:]\n\n\t// Update the original slice to only contain the records to keep.\n\t*originalSlice = childrenToKeep\n\n\t// Clear the memory of the part that was truncated.\n\tfor len(childrenToClear) \u003e 0 {\n\t\t// Copy empty values from `emptyChildren` to the recordsToClear slice.\n\t\t// This effectively \"clears\" the memory by overwriting elements.\n\t\tnumCleared := copy(childrenToClear, emptyChildren)\n\n\t\t// Slice recordsToClear to exclude the elements that were just cleared.\n\t\tchildrenToClear = childrenToClear[numCleared:]\n\t}\n}\n\n// mutableFor creates a mutable copy of the node if the current node does not\n// already belong to the provided copy-on-write context (COW). If the node is\n// already associated with the given COW context, it returns the current node.\n//\n// Parameters:\n// - cowCtx: The copy-on-write context that should own the returned node.\n//\n// Returns:\n// - A pointer to the mutable node associated with the given COW context.\n//\n// If the current node belongs to a different COW context, this function:\n// - Allocates a new node using the provided context.\n// - Copies the node’s records and children slices into the newly allocated node.\n// - Returns the new node which is now owned by the given COW context.\nfunc (n *node) mutableFor(cowCtx *copyOnWriteContext) *node {\n\t// If the current node is already owned by the provided context, return it as-is.\n\tif n.cowCtx == cowCtx {\n\t\treturn n\n\t}\n\n\t// Create a new node in the provided context.\n\tnewNode := cowCtx.newNode()\n\n\t// Copy the records from the current node into the new node.\n\tnewNode.records = append(newNode.records[:0], n.records...)\n\n\t// Copy the children from the current node into the new node.\n\tnewNode.children = append(newNode.children[:0], n.children...)\n\n\treturn newNode\n}\n\n// mutableChild ensures that the child node at the given index is mutable and\n// associated with the same COW context as the parent node. If the child node\n// belongs to a different context, a copy of the child is created and stored in the\n// parent node.\n//\n// Parameters:\n// - i: The index of the child node to be made mutable.\n//\n// Returns:\n// - A pointer to the mutable child node.\nfunc (n *node) mutableChild(i int) *node {\n\t// Ensure that the child at index `i` is mutable and belongs to the same context as the parent.\n\tmutableChildNode := n.children[i].mutableFor(n.cowCtx)\n\t// Update the child node reference in the current node to the mutable version.\n\tn.children[i] = mutableChildNode\n\treturn mutableChildNode\n}\n\n// split splits the given node at the given index.  The current node shrinks,\n// and this function returns the record that existed at that index and a new node\n// containing all records/children after it.\nfunc (n *node) split(i int) (Record, *node) {\n\trecord := n.records[i]\n\tnext := n.cowCtx.newNode()\n\tnext.records = append(next.records, n.records[i+1:]...)\n\tn.records.truncate(i)\n\tif len(n.children) \u003e 0 {\n\t\tnext.children = append(next.children, n.children[i+1:]...)\n\t\tn.children.truncate(i + 1)\n\t}\n\treturn record, next\n}\n\n// maybeSplitChild checks if a child should be split, and if so splits it.\n// Returns whether or not a split occurred.\nfunc (n *node) maybeSplitChild(i, maxRecords int) bool {\n\tif len(n.children[i].records) \u003c maxRecords {\n\t\treturn false\n\t}\n\tfirst := n.mutableChild(i)\n\trecord, second := first.split(maxRecords / 2)\n\tn.records.insertAt(i, record)\n\tn.children.insertAt(i+1, second)\n\treturn true\n}\n\n// insert adds a record to the subtree rooted at the current node, ensuring that no node in the subtree\n// exceeds the maximum number of allowed records (`maxRecords`). If an equivalent record is already present,\n// it replaces the existing one and returns it; otherwise, it returns nil.\n//\n// Parameters:\n// - record: The record to be inserted.\n// - maxRecords: The maximum number of records allowed per node.\n//\n// Returns:\n// - The record that was replaced if an equivalent record already existed, otherwise nil.\nfunc (n *node) insert(record Record, maxRecords int) Record {\n\t// Find the position where the new record should be inserted and check if an equivalent record already exists.\n\tinsertionIndex, recordExists := n.records.find(record)\n\n\tif recordExists {\n\t\t// If an equivalent record is found, replace it and return the old record.\n\t\texistingRecord := n.records[insertionIndex]\n\t\tn.records[insertionIndex] = record\n\t\treturn existingRecord\n\t}\n\n\t// If the current node is a leaf (has no children), insert the new record at the calculated index.\n\tif len(n.children) == 0 {\n\t\tn.records.insertAt(insertionIndex, record)\n\t\treturn nil\n\t}\n\n\t// Check if the child node at the insertion index needs to be split due to exceeding maxRecords.\n\tif n.maybeSplitChild(insertionIndex, maxRecords) {\n\t\t// If a split occurred, compare the new record with the record moved up to the current node.\n\t\tsplitRecord := n.records[insertionIndex]\n\t\tswitch {\n\t\tcase record.Less(splitRecord):\n\t\t\t// The new record belongs to the first (left) split node; no change to insertion index.\n\t\tcase splitRecord.Less(record):\n\t\t\t// The new record belongs to the second (right) split node; move the insertion index to the next position.\n\t\t\tinsertionIndex++\n\t\tdefault:\n\t\t\t// If the record is equivalent to the split record, replace it and return the old record.\n\t\t\texistingRecord := n.records[insertionIndex]\n\t\t\tn.records[insertionIndex] = record\n\t\t\treturn existingRecord\n\t\t}\n\t}\n\n\t// Recursively insert the record into the appropriate child node, now guaranteed to have space.\n\treturn n.mutableChild(insertionIndex).insert(record, maxRecords)\n}\n\n// get finds the given key in the subtree and returns it.\nfunc (n *node) get(key Record) Record {\n\ti, found := n.records.find(key)\n\tif found {\n\t\treturn n.records[i]\n\t} else if len(n.children) \u003e 0 {\n\t\treturn n.children[i].get(key)\n\t}\n\treturn nil\n}\n\n// min returns the first record in the subtree.\nfunc min(n *node) Record {\n\tif n == nil {\n\t\treturn nil\n\t}\n\tfor len(n.children) \u003e 0 {\n\t\tn = n.children[0]\n\t}\n\tif len(n.records) == 0 {\n\t\treturn nil\n\t}\n\treturn n.records[0]\n}\n\n// max returns the last record in the subtree.\nfunc max(n *node) Record {\n\tif n == nil {\n\t\treturn nil\n\t}\n\tfor len(n.children) \u003e 0 {\n\t\tn = n.children[len(n.children)-1]\n\t}\n\tif len(n.records) == 0 {\n\t\treturn nil\n\t}\n\treturn n.records[len(n.records)-1]\n}\n\n// toRemove details what record to remove in a node.remove call.\ntype toRemove int\n\nconst (\n\tremoveRecord toRemove = iota // removes the given record\n\tremoveMin                    // removes smallest record in the subtree\n\tremoveMax                    // removes largest record in the subtree\n)\n\n// remove removes a record from the subtree rooted at the current node.\n//\n// Parameters:\n// - record: The record to be removed (can be nil when the removal type indicates min or max).\n// - minRecords: The minimum number of records a node should have after removal.\n// - typ: The type of removal operation to perform (removeMin, removeMax, or removeRecord).\n//\n// Returns:\n// - The record that was removed, or nil if no such record was found.\nfunc (n *node) remove(record Record, minRecords int, removalType toRemove) Record {\n\tvar targetIndex int\n\tvar recordFound bool\n\n\t// Determine the index of the record to remove based on the removal type.\n\tswitch removalType {\n\tcase removeMax:\n\t\t// If this node is a leaf, remove and return the last record.\n\t\tif len(n.children) == 0 {\n\t\t\treturn n.records.pop()\n\t\t}\n\t\ttargetIndex = len(n.records) // The last record index for removing max.\n\n\tcase removeMin:\n\t\t// If this node is a leaf, remove and return the first record.\n\t\tif len(n.children) == 0 {\n\t\t\treturn n.records.removeAt(0)\n\t\t}\n\t\ttargetIndex = 0 // The first record index for removing min.\n\n\tcase removeRecord:\n\t\t// Locate the index of the record to be removed.\n\t\ttargetIndex, recordFound = n.records.find(record)\n\t\tif len(n.children) == 0 {\n\t\t\tif recordFound {\n\t\t\t\treturn n.records.removeAt(targetIndex)\n\t\t\t}\n\t\t\treturn nil // The record was not found in the leaf node.\n\t\t}\n\n\tdefault:\n\t\tpanic(\"invalid removal type\")\n\t}\n\n\t// If the current node has children, handle the removal recursively.\n\tif len(n.children[targetIndex].records) \u003c= minRecords {\n\t\t// If the target child node has too few records, grow it before proceeding with removal.\n\t\treturn n.growChildAndRemove(targetIndex, record, minRecords, removalType)\n\t}\n\n\t// Get a mutable reference to the child node at the target index.\n\ttargetChild := n.mutableChild(targetIndex)\n\n\t// If the record to be removed was found in the current node:\n\tif recordFound {\n\t\t// Replace the current record with its predecessor from the child node, and return the removed record.\n\t\treplacedRecord := n.records[targetIndex]\n\t\tn.records[targetIndex] = targetChild.remove(nil, minRecords, removeMax)\n\t\treturn replacedRecord\n\t}\n\n\t// Recursively remove the record from the child node.\n\treturn targetChild.remove(record, minRecords, removalType)\n}\n\n// growChildAndRemove grows child 'i' to make sure it's possible to remove an\n// record from it while keeping it at minRecords, then calls remove to actually\n// remove it.\n//\n// Most documentation says we have to do two sets of special casing:\n//  1. record is in this node\n//  2. record is in child\n//\n// In both cases, we need to handle the two subcases:\n//\n//\tA) node has enough values that it can spare one\n//\tB) node doesn't have enough values\n//\n// For the latter, we have to check:\n//\n//\ta) left sibling has node to spare\n//\tb) right sibling has node to spare\n//\tc) we must merge\n//\n// To simplify our code here, we handle cases #1 and #2 the same:\n// If a node doesn't have enough records, we make sure it does (using a,b,c).\n// We then simply redo our remove call, and the second time (regardless of\n// whether we're in case 1 or 2), we'll have enough records and can guarantee\n// that we hit case A.\nfunc (n *node) growChildAndRemove(i int, record Record, minRecords int, typ toRemove) Record {\n\tif i \u003e 0 \u0026\u0026 len(n.children[i-1].records) \u003e minRecords {\n\t\t// Steal from left child\n\t\tchild := n.mutableChild(i)\n\t\tstealFrom := n.mutableChild(i - 1)\n\t\tstolenRecord := stealFrom.records.pop()\n\t\tchild.records.insertAt(0, n.records[i-1])\n\t\tn.records[i-1] = stolenRecord\n\t\tif len(stealFrom.children) \u003e 0 {\n\t\t\tchild.children.insertAt(0, stealFrom.children.pop())\n\t\t}\n\t} else if i \u003c len(n.records) \u0026\u0026 len(n.children[i+1].records) \u003e minRecords {\n\t\t// steal from right child\n\t\tchild := n.mutableChild(i)\n\t\tstealFrom := n.mutableChild(i + 1)\n\t\tstolenRecord := stealFrom.records.removeAt(0)\n\t\tchild.records = append(child.records, n.records[i])\n\t\tn.records[i] = stolenRecord\n\t\tif len(stealFrom.children) \u003e 0 {\n\t\t\tchild.children = append(child.children, stealFrom.children.removeAt(0))\n\t\t}\n\t} else {\n\t\tif i \u003e= len(n.records) {\n\t\t\ti--\n\t\t}\n\t\tchild := n.mutableChild(i)\n\t\t// merge with right child\n\t\tmergeRecord := n.records.removeAt(i)\n\t\tmergeChild := n.children.removeAt(i + 1).mutableFor(n.cowCtx)\n\t\tchild.records = append(child.records, mergeRecord)\n\t\tchild.records = append(child.records, mergeChild.records...)\n\t\tchild.children = append(child.children, mergeChild.children...)\n\t\tn.cowCtx.freeNode(mergeChild)\n\t}\n\treturn n.remove(record, minRecords, typ)\n}\n\ntype direction int\n\nconst (\n\tdescend = direction(-1)\n\tascend  = direction(+1)\n)\n\n// iterate provides a simple method for iterating over elements in the tree.\n//\n// When ascending, the 'start' should be less than 'stop' and when descending,\n// the 'start' should be greater than 'stop'. Setting 'includeStart' to true\n// will force the iterator to include the first record when it equals 'start',\n// thus creating a \"greaterOrEqual\" or \"lessThanEqual\" rather than just a\n// \"greaterThan\" or \"lessThan\" queries.\nfunc (n *node) iterate(dir direction, start, stop Record, includeStart bool, hit bool, iter RecordIterator) (bool, bool) {\n\tvar ok, found bool\n\tvar index int\n\tswitch dir {\n\tcase ascend:\n\t\tif start != nil {\n\t\t\tindex, _ = n.records.find(start)\n\t\t}\n\t\tfor i := index; i \u003c len(n.records); i++ {\n\t\t\tif len(n.children) \u003e 0 {\n\t\t\t\tif hit, ok = n.children[i].iterate(dir, start, stop, includeStart, hit, iter); !ok {\n\t\t\t\t\treturn hit, false\n\t\t\t\t}\n\t\t\t}\n\t\t\tif !includeStart \u0026\u0026 !hit \u0026\u0026 start != nil \u0026\u0026 !start.Less(n.records[i]) {\n\t\t\t\thit = true\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\thit = true\n\t\t\tif stop != nil \u0026\u0026 !n.records[i].Less(stop) {\n\t\t\t\treturn hit, false\n\t\t\t}\n\t\t\tif !iter(n.records[i]) {\n\t\t\t\treturn hit, false\n\t\t\t}\n\t\t}\n\t\tif len(n.children) \u003e 0 {\n\t\t\tif hit, ok = n.children[len(n.children)-1].iterate(dir, start, stop, includeStart, hit, iter); !ok {\n\t\t\t\treturn hit, false\n\t\t\t}\n\t\t}\n\tcase descend:\n\t\tif start != nil {\n\t\t\tindex, found = n.records.find(start)\n\t\t\tif !found {\n\t\t\t\tindex = index - 1\n\t\t\t}\n\t\t} else {\n\t\t\tindex = len(n.records) - 1\n\t\t}\n\t\tfor i := index; i \u003e= 0; i-- {\n\t\t\tif start != nil \u0026\u0026 !n.records[i].Less(start) {\n\t\t\t\tif !includeStart || hit || start.Less(n.records[i]) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t}\n\t\t\tif len(n.children) \u003e 0 {\n\t\t\t\tif hit, ok = n.children[i+1].iterate(dir, start, stop, includeStart, hit, iter); !ok {\n\t\t\t\t\treturn hit, false\n\t\t\t\t}\n\t\t\t}\n\t\t\tif stop != nil \u0026\u0026 !stop.Less(n.records[i]) {\n\t\t\t\treturn hit, false //\tcontinue\n\t\t\t}\n\t\t\thit = true\n\t\t\tif !iter(n.records[i]) {\n\t\t\t\treturn hit, false\n\t\t\t}\n\t\t}\n\t\tif len(n.children) \u003e 0 {\n\t\t\tif hit, ok = n.children[0].iterate(dir, start, stop, includeStart, hit, iter); !ok {\n\t\t\t\treturn hit, false\n\t\t\t}\n\t\t}\n\t}\n\treturn hit, true\n}\n\nfunc (tree *BTree) Iterate(dir direction, start, stop Record, includeStart bool, hit bool, iter RecordIterator) (bool, bool) {\n\treturn tree.root.iterate(dir, start, stop, includeStart, hit, iter)\n}\n\n// Clone creates a new BTree instance that shares the current tree's structure using a copy-on-write (COW) approach.\n//\n// How Cloning Works:\n//   - The cloned tree (`clonedTree`) shares the current tree’s nodes in a read-only state. This means that no additional memory\n//     is allocated for shared nodes, and read operations on the cloned tree are as fast as on the original tree.\n//   - When either the original tree (`t`) or the cloned tree (`clonedTree`) needs to perform a write operation (such as an insert, delete, etc.),\n//     a new copy of the affected nodes is created on-demand. This ensures that modifications to one tree do not affect the other.\n//\n// Performance Implications:\n//   - **Clone Creation:** The creation of a clone is inexpensive since it only involves copying references to the original tree's nodes\n//     and creating new copy-on-write contexts.\n//   - **Read Operations:** Reading from either the original tree or the cloned tree has no additional performance overhead compared to the original tree.\n//   - **Write Operations:** The first write operation on either tree may experience a slight slow-down due to the allocation of new nodes,\n//     but subsequent write operations will perform at the same speed as if the tree were not cloned.\n//\n// Returns:\n// - A new BTree instance (`clonedTree`) that shares the original tree's structure.\nfunc (t *BTree) Clone() *BTree {\n\t// Create two independent copy-on-write contexts, one for the original tree (`t`) and one for the cloned tree.\n\toriginalContext := *t.cowCtx\n\tclonedContext := *t.cowCtx\n\n\t// Create a shallow copy of the current tree, which will be the new cloned tree.\n\tclonedTree := *t\n\n\t// Assign the new contexts to their respective trees.\n\tt.cowCtx = \u0026originalContext\n\tclonedTree.cowCtx = \u0026clonedContext\n\n\treturn \u0026clonedTree\n}\n\n// maxRecords returns the max number of records to allow per node.\nfunc (t *BTree) maxRecords() int {\n\treturn t.degree*2 - 1\n}\n\n// minRecords returns the min number of records to allow per node (ignored for the\n// root node).\nfunc (t *BTree) minRecords() int {\n\treturn t.degree - 1\n}\n\nfunc (c *copyOnWriteContext) newNode() (n *node) {\n\tn = c.nodes.newNode()\n\tn.cowCtx = c\n\treturn\n}\n\ntype freeType int\n\nconst (\n\tftFreelistFull freeType = iota // node was freed (available for GC, not stored in nodes)\n\tftStored                       // node was stored in the nodes for later use\n\tftNotOwned                     // node was ignored by COW, since it's owned by another one\n)\n\n// freeNode frees a node within a given COW context, if it's owned by that\n// context.  It returns what happened to the node (see freeType const\n// documentation).\nfunc (c *copyOnWriteContext) freeNode(n *node) freeType {\n\tif n.cowCtx == c {\n\t\t// clear to allow GC\n\t\tn.records.truncate(0)\n\t\tn.children.truncate(0)\n\t\tn.cowCtx = nil\n\t\tif c.nodes.freeNode(n) {\n\t\t\treturn ftStored\n\t\t} else {\n\t\t\treturn ftFreelistFull\n\t\t}\n\t} else {\n\t\treturn ftNotOwned\n\t}\n}\n\n// Insert adds the given record to the B-tree. If a record already exists in the tree with the same value,\n// it is replaced, and the old record is returned. Otherwise, it returns nil.\n//\n// Notes:\n// - The function panics if a nil record is provided as input.\n// - If the root node is empty, a new root node is created and the record is inserted.\n//\n// Parameters:\n// - record: The record to be inserted into the B-tree.\n//\n// Returns:\n// - The replaced record if an equivalent record already exists, or nil if no replacement occurred.\nfunc (t *BTree) Insert(record Record) Record {\n\tif record == nil {\n\t\tpanic(\"nil record cannot be added to BTree\")\n\t}\n\n\t// If the tree is empty (no root), create a new root node and insert the record.\n\tif t.root == nil {\n\t\tt.root = t.cowCtx.newNode()\n\t\tt.root.records = append(t.root.records, record)\n\t\tt.length++\n\t\treturn nil\n\t}\n\n\t// Ensure that the root node is mutable (associated with the current tree's copy-on-write context).\n\tt.root = t.root.mutableFor(t.cowCtx)\n\n\t// If the root node is full (contains the maximum number of records), split the root.\n\tif len(t.root.records) \u003e= t.maxRecords() {\n\t\t// Split the root node, promoting the middle record and creating a new child node.\n\t\tmiddleRecord, newChildNode := t.root.split(t.maxRecords() / 2)\n\n\t\t// Create a new root node to hold the promoted middle record.\n\t\toldRoot := t.root\n\t\tt.root = t.cowCtx.newNode()\n\t\tt.root.records = append(t.root.records, middleRecord)\n\t\tt.root.children = append(t.root.children, oldRoot, newChildNode)\n\t}\n\n\t// Insert the new record into the subtree rooted at the current root node.\n\treplacedRecord := t.root.insert(record, t.maxRecords())\n\n\t// If no record was replaced, increase the tree's length.\n\tif replacedRecord == nil {\n\t\tt.length++\n\t}\n\n\treturn replacedRecord\n}\n\n// Delete removes an record equal to the passed in record from the tree, returning\n// it.  If no such record exists, returns nil.\nfunc (t *BTree) Delete(record Record) Record {\n\treturn t.deleteRecord(record, removeRecord)\n}\n\n// DeleteMin removes the smallest record in the tree and returns it.\n// If no such record exists, returns nil.\nfunc (t *BTree) DeleteMin() Record {\n\treturn t.deleteRecord(nil, removeMin)\n}\n\n// Shift is identical to DeleteMin. If the tree is thought of as an ordered list, then Shift()\n// removes the element at the start of the list, the smallest element, and returns it.\nfunc (t *BTree) Shift() Record {\n\treturn t.deleteRecord(nil, removeMin)\n}\n\n// DeleteMax removes the largest record in the tree and returns it.\n// If no such record exists, returns nil.\nfunc (t *BTree) DeleteMax() Record {\n\treturn t.deleteRecord(nil, removeMax)\n}\n\n// Pop is identical to DeleteMax. If the tree is thought of as an ordered list, then Shift()\n// removes the element at the end of the list, the largest element, and returns it.\nfunc (t *BTree) Pop() Record {\n\treturn t.deleteRecord(nil, removeMax)\n}\n\n// deleteRecord removes a record from the B-tree based on the specified removal type (removeMin, removeMax, or removeRecord).\n// It returns the removed record if it was found, or nil if no matching record was found.\n//\n// Parameters:\n// - record: The record to be removed (can be nil if the removal type indicates min or max).\n// - removalType: The type of removal operation to perform (removeMin, removeMax, or removeRecord).\n//\n// Returns:\n// - The removed record if it existed in the tree, or nil if it was not found.\nfunc (t *BTree) deleteRecord(record Record, removalType toRemove) Record {\n\t// If the tree is empty or the root has no records, return nil.\n\tif t.root == nil || len(t.root.records) == 0 {\n\t\treturn nil\n\t}\n\n\t// Ensure the root node is mutable (associated with the tree's copy-on-write context).\n\tt.root = t.root.mutableFor(t.cowCtx)\n\n\t// Attempt to remove the specified record from the root node.\n\tremovedRecord := t.root.remove(record, t.minRecords(), removalType)\n\n\t// Check if the root node has become empty but still has children.\n\t// In this case, the tree height should be reduced, making the first child the new root.\n\tif len(t.root.records) == 0 \u0026\u0026 len(t.root.children) \u003e 0 {\n\t\toldRoot := t.root\n\t\tt.root = t.root.children[0]\n\t\t// Free the old root node, as it is no longer needed.\n\t\tt.cowCtx.freeNode(oldRoot)\n\t}\n\n\t// If a record was successfully removed, decrease the tree's length.\n\tif removedRecord != nil {\n\t\tt.length--\n\t}\n\n\treturn removedRecord\n}\n\n// AscendRange calls the iterator for every value in the tree within the range\n// [greaterOrEqual, lessThan), until iterator returns false.\nfunc (t *BTree) AscendRange(greaterOrEqual, lessThan Record, iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(ascend, greaterOrEqual, lessThan, true, false, iterator)\n}\n\n// AscendLessThan calls the iterator for every value in the tree within the range\n// [first, pivot), until iterator returns false.\nfunc (t *BTree) AscendLessThan(pivot Record, iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(ascend, nil, pivot, false, false, iterator)\n}\n\n// AscendGreaterOrEqual calls the iterator for every value in the tree within\n// the range [pivot, last], until iterator returns false.\nfunc (t *BTree) AscendGreaterOrEqual(pivot Record, iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(ascend, pivot, nil, true, false, iterator)\n}\n\n// Ascend calls the iterator for every value in the tree within the range\n// [first, last], until iterator returns false.\nfunc (t *BTree) Ascend(iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(ascend, nil, nil, false, false, iterator)\n}\n\n// DescendRange calls the iterator for every value in the tree within the range\n// [lessOrEqual, greaterThan), until iterator returns false.\nfunc (t *BTree) DescendRange(lessOrEqual, greaterThan Record, iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(descend, lessOrEqual, greaterThan, true, false, iterator)\n}\n\n// DescendLessOrEqual calls the iterator for every value in the tree within the range\n// [pivot, first], until iterator returns false.\nfunc (t *BTree) DescendLessOrEqual(pivot Record, iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(descend, pivot, nil, true, false, iterator)\n}\n\n// DescendGreaterThan calls the iterator for every value in the tree within\n// the range [last, pivot), until iterator returns false.\nfunc (t *BTree) DescendGreaterThan(pivot Record, iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(descend, nil, pivot, false, false, iterator)\n}\n\n// Descend calls the iterator for every value in the tree within the range\n// [last, first], until iterator returns false.\nfunc (t *BTree) Descend(iterator RecordIterator) {\n\tif t.root == nil {\n\t\treturn\n\t}\n\tt.root.iterate(descend, nil, nil, false, false, iterator)\n}\n\n// Get looks for the key record in the tree, returning it.  It returns nil if\n// unable to find that record.\nfunc (t *BTree) Get(key Record) Record {\n\tif t.root == nil {\n\t\treturn nil\n\t}\n\treturn t.root.get(key)\n}\n\n// Min returns the smallest record in the tree, or nil if the tree is empty.\nfunc (t *BTree) Min() Record {\n\treturn min(t.root)\n}\n\n// Max returns the largest record in the tree, or nil if the tree is empty.\nfunc (t *BTree) Max() Record {\n\treturn max(t.root)\n}\n\n// Has returns true if the given key is in the tree.\nfunc (t *BTree) Has(key Record) bool {\n\treturn t.Get(key) != nil\n}\n\n// Len returns the number of records currently in the tree.\nfunc (t *BTree) Len() int {\n\treturn t.length\n}\n\n// Clear removes all elements from the B-tree.\n//\n// Parameters:\n// - addNodesToFreelist:\n//     - If true, the tree's nodes are added to the freelist during the clearing process,\n//       up to the freelist's capacity.\n//     - If false, the root node is simply dereferenced, allowing Go's garbage collector\n//       to reclaim the memory.\n//\n// Benefits:\n// - **Performance:**\n//     - Significantly faster than deleting each element individually, as it avoids the overhead\n//       of searching and updating the tree structure for each deletion.\n//     - More efficient than creating a new tree, since it reuses existing nodes by adding them\n//       to the freelist instead of discarding them to the garbage collector.\n//\n// Time Complexity:\n// - **O(1):**\n//     - When `addNodesToFreelist` is false.\n//     - When `addNodesToFreelist` is true but the freelist is already full.\n// - **O(freelist size):**\n//     - When adding nodes to the freelist up to its capacity.\n// - **O(tree size):**\n//     - When iterating through all nodes to add to the freelist, but none can be added due to\n//       ownership by another tree.\n\nfunc (tree *BTree) Clear(addNodesToFreelist bool) {\n\tif tree.root != nil \u0026\u0026 addNodesToFreelist {\n\t\ttree.root.reset(tree.cowCtx)\n\t}\n\ttree.root = nil\n\ttree.length = 0\n}\n\n// reset adds all nodes in the current subtree to the freelist.\n//\n// The function operates recursively:\n// - It first attempts to reset all child nodes.\n// - If the freelist becomes full at any point, the process stops immediately.\n//\n// Parameters:\n// - copyOnWriteCtx: The copy-on-write context managing the freelist.\n//\n// Returns:\n// - true: Indicates that the parent node should continue attempting to reset its nodes.\n// - false: Indicates that the freelist is full and no further nodes should be added.\n//\n// Usage:\n// This method is called during the `Clear` operation of the B-tree to efficiently reuse\n// nodes by adding them to the freelist, thereby avoiding unnecessary allocations and reducing\n// garbage collection overhead.\nfunc (currentNode *node) reset(copyOnWriteCtx *copyOnWriteContext) bool {\n\t// Iterate through each child node and attempt to reset it.\n\tfor _, childNode := range currentNode.children {\n\t\t// If any child reset operation signals that the freelist is full, stop the process.\n\t\tif !childNode.reset(copyOnWriteCtx) {\n\t\t\treturn false\n\t\t}\n\t}\n\n\t// Attempt to add the current node to the freelist.\n\t// If the freelist is full after this operation, indicate to the parent to stop.\n\tfreelistStatus := copyOnWriteCtx.freeNode(currentNode)\n\treturn freelistStatus != ftFreelistFull\n}\n"},{"name":"btree_test.gno","body":"package btree\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\t\"testing\"\n)\n\n// Content represents a key-value pair where the Key can be either an int or string\n// and the Value can be any type.\ntype Content struct {\n\tKey   any\n\tValue any\n}\n\n// Less compares two Content records by their Keys.\n// The Key must be either an int or a string.\nfunc (c Content) Less(than Record) bool {\n\tother, ok := than.(Content)\n\tif !ok {\n\t\tpanic(\"cannot compare: incompatible types\")\n\t}\n\n\tswitch key := c.Key.(type) {\n\tcase int:\n\t\tswitch otherKey := other.Key.(type) {\n\t\tcase int:\n\t\t\treturn key \u003c otherKey\n\t\tcase string:\n\t\t\treturn true // ints are always less than strings\n\t\tdefault:\n\t\t\tpanic(\"unsupported key type: must be int or string\")\n\t\t}\n\tcase string:\n\t\tswitch otherKey := other.Key.(type) {\n\t\tcase int:\n\t\t\treturn false // strings are always greater than ints\n\t\tcase string:\n\t\t\treturn key \u003c otherKey\n\t\tdefault:\n\t\t\tpanic(\"unsupported key type: must be int or string\")\n\t\t}\n\tdefault:\n\t\tpanic(\"unsupported key type: must be int or string\")\n\t}\n}\n\ntype ContentSlice []Content\n\nfunc (s ContentSlice) Len() int {\n\treturn len(s)\n}\n\nfunc (s ContentSlice) Less(i, j int) bool {\n\treturn s[i].Less(s[j])\n}\n\nfunc (s ContentSlice) Swap(i, j int) {\n\ts[i], s[j] = s[j], s[i]\n}\n\nfunc (s ContentSlice) Copy() ContentSlice {\n\tnewSlice := make(ContentSlice, len(s))\n\tcopy(newSlice, s)\n\treturn newSlice\n}\n\n// Ensure Content implements the Record interface.\nvar _ Record = Content{}\n\n// ****************************************************************************\n// Test helpers\n// ****************************************************************************\n\nfunc genericSeeding(tree *BTree, size int) *BTree {\n\tfor i := 0; i \u003c size; i++ {\n\t\ttree.Insert(Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)})\n\t}\n\treturn tree\n}\n\nfunc intSlicesCompare(left, right []int) int {\n\tif len(left) != len(right) {\n\t\tif len(left) \u003e len(right) {\n\t\t\treturn 1\n\t\t} else {\n\t\t\treturn -1\n\t\t}\n\t}\n\n\tfor position, leftInt := range left {\n\t\tif leftInt != right[position] {\n\t\t\tif leftInt \u003e right[position] {\n\t\t\t\treturn 1\n\t\t\t} else {\n\t\t\t\treturn -1\n\t\t\t}\n\t\t}\n\t}\n\n\treturn 0\n}\n\n// ****************************************************************************\n// Tests\n// ****************************************************************************\n\nfunc TestLen(t *testing.T) {\n\tlength := genericSeeding(New(WithDegree(10)), 7).Len()\n\tif length != 7 {\n\t\tt.Errorf(\"Length is incorrect. Expected 7, but got %d.\", length)\n\t}\n\n\tlength = genericSeeding(New(WithDegree(5)), 111).Len()\n\tif length != 111 {\n\t\tt.Errorf(\"Length is incorrect. Expected 111, but got %d.\", length)\n\t}\n\n\tlength = genericSeeding(New(WithDegree(30)), 123).Len()\n\tif length != 123 {\n\t\tt.Errorf(\"Length is incorrect. Expected 123, but got %d.\", length)\n\t}\n}\n\nfunc TestHas(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 40)\n\n\tif tree.Has(Content{Key: 7}) != true {\n\t\tt.Errorf(\"Has(7) reported false, but it should be true.\")\n\t}\n\tif tree.Has(Content{Key: 39}) != true {\n\t\tt.Errorf(\"Has(40) reported false, but it should be true.\")\n\t}\n\tif tree.Has(Content{Key: 1111}) == true {\n\t\tt.Errorf(\"Has(1111) reported true, but it should be false.\")\n\t}\n}\n\nfunc TestMin(t *testing.T) {\n\tmin := genericSeeding(New(WithDegree(10)), 53).Min().(Content)\n\n\tif min.Key != 0 {\n\t\tt.Errorf(\"Minimum should have been 0, but it was reported as %d.\", min)\n\t}\n}\n\nfunc TestMax(t *testing.T) {\n\tmax := genericSeeding(New(WithDegree(10)), 53).Max().(Content)\n\n\tif max.Key != 52 {\n\t\tt.Errorf(\"Maximum should have been 52, but it was reported as %d.\", max)\n\t}\n}\n\nfunc TestGet(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 40)\n\n\tif val := tree.Get(Content{Key: 7}); val != nil \u0026\u0026 val.(Content).Value != \"Value_7\" {\n\t\tt.Errorf(\"Get(7) should have returned 'Value_7', but it returned %v.\", val)\n\t}\n\tif val := tree.Get(Content{Key: 39}); val != nil \u0026\u0026 val.(Content).Value != \"Value_39\" {\n\t\tt.Errorf(\"Get(39) should have returned 'Value_39', but it returned %v.\", val)\n\t}\n\tif val := tree.Get(Content{Key: 1111}); val != nil {\n\t\tt.Errorf(\"Get(1111) returned %v, but it should be nil.\", val)\n\t}\n}\n\nfunc TestDescend(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 5)\n\n\texpected := []int{4, 3, 2, 1, 0}\n\tfound := []int{}\n\n\ttree.Descend(func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"Descend returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestDescendGreaterThan(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 10)\n\n\texpected := []int{9, 8, 7, 6, 5}\n\tfound := []int{}\n\n\ttree.DescendGreaterThan(Content{Key: 4}, func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"DescendGreaterThan returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestDescendLessOrEqual(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 10)\n\n\texpected := []int{4, 3, 2, 1, 0}\n\tfound := []int{}\n\n\ttree.DescendLessOrEqual(Content{Key: 4}, func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"DescendLessOrEqual returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestDescendRange(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 10)\n\n\texpected := []int{6, 5, 4, 3, 2}\n\tfound := []int{}\n\n\ttree.DescendRange(Content{Key: 6}, Content{Key: 1}, func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"DescendRange returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestAscend(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 5)\n\n\texpected := []int{0, 1, 2, 3, 4}\n\tfound := []int{}\n\n\ttree.Ascend(func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"Ascend returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestAscendGreaterOrEqual(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 10)\n\n\texpected := []int{5, 6, 7, 8, 9}\n\tfound := []int{}\n\n\ttree.AscendGreaterOrEqual(Content{Key: 5}, func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"AscendGreaterOrEqual returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestAscendLessThan(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 10)\n\n\texpected := []int{0, 1, 2, 3, 4}\n\tfound := []int{}\n\n\ttree.AscendLessThan(Content{Key: 5}, func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"AscendLessThan returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestAscendRange(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(10)), 10)\n\n\texpected := []int{2, 3, 4, 5, 6}\n\tfound := []int{}\n\n\ttree.AscendRange(Content{Key: 2}, Content{Key: 7}, func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tfound = append(found, record.Key.(int))\n\t\treturn true\n\t})\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"AscendRange returned the wrong sequence. Expected %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestDeleteMin(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(3)), 100)\n\n\texpected := []int{0, 1, 2, 3, 4}\n\tfound := []int{}\n\n\tfound = append(found, tree.DeleteMin().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMin().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMin().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMin().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMin().(Content).Key.(int))\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"5 rounds of DeleteMin returned the wrong elements. Expected  %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestShift(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(3)), 100)\n\n\texpected := []int{0, 1, 2, 3, 4}\n\tfound := []int{}\n\n\tfound = append(found, tree.Shift().(Content).Key.(int))\n\tfound = append(found, tree.Shift().(Content).Key.(int))\n\tfound = append(found, tree.Shift().(Content).Key.(int))\n\tfound = append(found, tree.Shift().(Content).Key.(int))\n\tfound = append(found, tree.Shift().(Content).Key.(int))\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"5 rounds of Shift returned the wrong elements. Expected  %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestDeleteMax(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(3)), 100)\n\n\texpected := []int{99, 98, 97, 96, 95}\n\tfound := []int{}\n\n\tfound = append(found, tree.DeleteMax().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMax().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMax().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMax().(Content).Key.(int))\n\tfound = append(found, tree.DeleteMax().(Content).Key.(int))\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"5 rounds of DeleteMax returned the wrong elements. Expected  %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestPop(t *testing.T) {\n\ttree := genericSeeding(New(WithDegree(3)), 100)\n\n\texpected := []int{99, 98, 97, 96, 95}\n\tfound := []int{}\n\n\tfound = append(found, tree.Pop().(Content).Key.(int))\n\tfound = append(found, tree.Pop().(Content).Key.(int))\n\tfound = append(found, tree.Pop().(Content).Key.(int))\n\tfound = append(found, tree.Pop().(Content).Key.(int))\n\tfound = append(found, tree.Pop().(Content).Key.(int))\n\n\tif intSlicesCompare(expected, found) != 0 {\n\t\tt.Errorf(\"5 rounds of Pop returned the wrong elements. Expected  %v, but got %v.\", expected, found)\n\t}\n}\n\nfunc TestInsertGet(t *testing.T) {\n\ttree := New(WithDegree(4))\n\n\texpected := []Content{}\n\n\tfor count := 0; count \u003c 20; count++ {\n\t\tvalue := fmt.Sprintf(\"Value_%d\", count)\n\t\ttree.Insert(Content{Key: count, Value: value})\n\t\texpected = append(expected, Content{Key: count, Value: value})\n\t}\n\n\tfor count := 0; count \u003c 20; count++ {\n\t\tval := tree.Get(Content{Key: count})\n\t\tif val == nil || val.(Content) != expected[count] {\n\t\t\tt.Errorf(\"Insert/Get doesn't appear to be working. Expected to retrieve %v with key %d, but got %v.\", expected[count], count, val)\n\t\t}\n\t}\n}\n\nfunc TestClone(t *testing.T) {\n\t// Implement the clone test\n}\n\n// ***** The following tests are functional or stress testing type tests.\n\nfunc TestBTree(t *testing.T) {\n\t// Create a B-Tree of degree 3\n\ttree := New(WithDegree(3))\n\n\t// insertData := []Content{}\n\tvar insertData ContentSlice\n\n\t// Insert integer keys\n\tintKeys := []int{10, 20, 5, 6, 12, 30, 7, 17}\n\tfor _, key := range intKeys {\n\t\tcontent := Content{Key: key, Value: fmt.Sprintf(\"Value_%d\", key)}\n\t\tinsertData = append(insertData, content)\n\t\tresult := tree.Insert(content)\n\t\tif result != nil {\n\t\t\tt.Errorf(\"**** Already in the tree?  %v\", result)\n\t\t}\n\t}\n\n\t// Insert string keys\n\tstringKeys := []string{\"apple\", \"banana\", \"cherry\", \"date\", \"fig\", \"grape\"}\n\tfor _, key := range stringKeys {\n\t\tcontent := Content{Key: key, Value: fmt.Sprintf(\"Fruit_%s\", key)}\n\t\tinsertData = append(insertData, content)\n\t\ttree.Insert(content)\n\t}\n\n\tif tree.Len() != 14 {\n\t\tt.Errorf(\"Tree length wrong. Expected 14 but got %d\", tree.Len())\n\t}\n\n\t// Search for existing and non-existing keys\n\tsearchTests := []struct {\n\t\ttest     Content\n\t\texpected bool\n\t}{\n\t\t{Content{Key: 10, Value: \"Value_10\"}, true},\n\t\t{Content{Key: 15, Value: \"\"}, false},\n\t\t{Content{Key: \"banana\", Value: \"Fruit_banana\"}, true},\n\t\t{Content{Key: \"kiwi\", Value: \"\"}, false},\n\t}\n\n\tt.Logf(\"Search Tests:\\n\")\n\tfor _, test := range searchTests {\n\t\tval := tree.Get(test.test)\n\n\t\tif test.expected {\n\t\t\tif val != nil \u0026\u0026 val.(Content).Value == test.test.Value {\n\t\t\t\tt.Logf(\"Found expected key:value %v:%v\", test.test.Key, test.test.Value)\n\t\t\t} else {\n\t\t\t\tif val == nil {\n\t\t\t\t\tt.Logf(\"Didn't find %v, but expected\", test.test.Key)\n\t\t\t\t} else {\n\t\t\t\t\tt.Errorf(\"Expected key %v:%v, but found %v:%v.\", test.test.Key, test.test.Value, val.(Content).Key, val.(Content).Value)\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif val != nil {\n\t\t\t\tt.Errorf(\"Did not expect key %v, but found key:value %v:%v\", test.test.Key, val.(Content).Key, val.(Content).Value)\n\t\t\t} else {\n\t\t\t\tt.Logf(\"Didn't find %v, but wasn't expected\", test.test.Key)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Iterate in order\n\tt.Logf(\"\\nIn-order Iteration:\\n\")\n\tpos := 0\n\n\tif tree.Len() != 14 {\n\t\tt.Errorf(\"Tree length wrong. Expected 14 but got %d\", tree.Len())\n\t}\n\n\tsortedInsertData := insertData.Copy()\n\tsort.Sort(sortedInsertData)\n\n\tt.Logf(\"Insert Data Length: %d\", len(insertData))\n\tt.Logf(\"Sorted Data Length: %d\", len(sortedInsertData))\n\tt.Logf(\"Tree Length: %d\", tree.Len())\n\n\ttree.Ascend(func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tt.Logf(\"Key:Value == %v:%v\", record.Key, record.Value)\n\t\tif record.Key != sortedInsertData[pos].Key {\n\t\t\tt.Errorf(\"Out of order! Expected %v, but got %v\", sortedInsertData[pos].Key, record.Key)\n\t\t}\n\t\tpos++\n\t\treturn true\n\t})\n\t// // Reverse Iterate\n\tt.Logf(\"\\nReverse-order Iteration:\\n\")\n\tpos = len(sortedInsertData) - 1\n\n\ttree.Descend(func(_record Record) bool {\n\t\trecord := _record.(Content)\n\t\tt.Logf(\"Key:Value == %v:%v\", record.Key, record.Value)\n\t\tif record.Key != sortedInsertData[pos].Key {\n\t\t\tt.Errorf(\"Out of order! Expected %v, but got %v\", sortedInsertData[pos].Key, record.Key)\n\t\t}\n\t\tpos--\n\t\treturn true\n\t})\n\n\tdeleteTests := []Content{\n\t\t{Key: 10, Value: \"Value_10\"},\n\t\t{Key: 15, Value: \"\"},\n\t\t{Key: \"banana\", Value: \"Fruit_banana\"},\n\t\t{Key: \"kiwi\", Value: \"\"},\n\t}\n\tfor _, test := range deleteTests {\n\t\tfmt.Printf(\"\\nDeleting %+v\\n\", test)\n\t\ttree.Delete(test)\n\t}\n\n\tif tree.Len() != 12 {\n\t\tt.Errorf(\"Tree length wrong. Expected 12 but got %d\", tree.Len())\n\t}\n\n\tfor _, test := range deleteTests {\n\t\tval := tree.Get(test)\n\t\tif val != nil {\n\t\t\tt.Errorf(\"Did not expect key %v, but found key:value %v:%v\", test.Key, val.(Content).Key, val.(Content).Value)\n\t\t} else {\n\t\t\tt.Logf(\"Didn't find %v, but wasn't expected\", test.Key)\n\t\t}\n\t}\n}\n\n// Write a test that populates a large B-Tree with 1000 records.\n// It should then `Clone` the tree, make some changes to both the original and the clone,\n// And then clone the clone, and make some changes to all three trees, and then check that the changes are isolated\n// to the tree they were made in.\nfunc TestBTreeCloneIsolation(t *testing.T) {\n\tt.Logf(\"Creating B-Tree of degree 10 with 1000 records\\n\")\n\tsize := 1000\n\ttree := genericSeeding(New(WithDegree(10)), size)\n\n\t// Clone the tree\n\tt.Logf(\"Cloning the tree\\n\")\n\tclone := tree.Clone()\n\n\t// Make some changes to the original and the clone\n\tt.Logf(\"Making changes to the original and the clone\\n\")\n\tfor i := 0; i \u003c size; i += 2 {\n\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n\t\ttree.Delete(content)\n\t\tcontent = Content{Key: i + 1, Value: fmt.Sprintf(\"Value_%d\", i+1)}\n\t\tclone.Delete(content)\n\t}\n\n\t// Clone the clone\n\tt.Logf(\"Cloning the clone\\n\")\n\tclone2 := clone.Clone()\n\n\t// Make some changes to all three trees\n\tt.Logf(\"Making changes to all three trees\\n\")\n\tfor i := 0; i \u003c size; i += 3 {\n\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n\t\ttree.Delete(content)\n\t\tcontent = Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i+1)}\n\t\tclone.Delete(content)\n\t\tcontent = Content{Key: i + 2, Value: fmt.Sprintf(\"Value_%d\", i+2)}\n\t\tclone2.Delete(content)\n\t}\n\n\t// Check that the changes are isolated to the tree they were made in\n\tt.Logf(\"Checking that the changes are isolated to the tree they were made in\\n\")\n\tfor i := 0; i \u003c size; i++ {\n\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n\t\tval := tree.Get(content)\n\n\t\tif i%3 == 0 || i%2 == 0 {\n\t\t\tif val != nil {\n\t\t\t\tt.Errorf(\"Didn't expect key %v, but found key:value %v:%v\", content.Key, val.(Content).Key, val.(Content).Value)\n\t\t\t}\n\t\t} else {\n\t\t\tif val == nil {\n\t\t\t\tt.Errorf(\"Expected key %v, but didn't find it\", content.Key)\n\t\t\t}\n\t\t}\n\n\t\tval = clone.Get(content)\n\t\tif i%2 != 0 || i%3 == 0 {\n\t\t\tif val != nil {\n\t\t\t\tt.Errorf(\"Didn't expect key %v, but found key:value %v:%v\", content.Key, val.(Content).Key, val.(Content).Value)\n\t\t\t}\n\t\t} else {\n\t\t\tif val == nil {\n\t\t\t\tt.Errorf(\"Expected key %v, but didn't find it\", content.Key)\n\t\t\t}\n\t\t}\n\n\t\tval = clone2.Get(content)\n\t\tif i%2 != 0 || (i-2)%3 == 0 {\n\t\t\tif val != nil {\n\t\t\t\tt.Errorf(\"Didn't expect key %v, but found key:value %v:%v\", content.Key, val.(Content).Key, val.(Content).Value)\n\t\t\t}\n\t\t} else {\n\t\t\tif val == nil {\n\t\t\t\tt.Errorf(\"Expected key %v, but didn't find it\", content.Key)\n\t\t\t}\n\t\t}\n\t}\n}\n\n// --------------------\n// Stress tests. Disabled for testing performance\n\n//func TestStress(t *testing.T) {\n//\t// Loop through creating B-Trees with a range of degrees from 3 to 12, stepping by 3.\n//\t// Insert 1000 records into each tree, then search for each record.\n//\t// Delete half of the records, skipping every other one, then search for each record.\n//\n//\tfor degree := 3; degree \u003c= 12; degree += 3 {\n//\t\tt.Logf(\"Testing B-Tree of degree %d\\n\", degree)\n//\t\ttree := New(WithDegree(degree))\n//\n//\t\t// Insert 1000 records\n//\t\tt.Logf(\"Inserting 1000 records\\n\")\n//\t\tfor i := 0; i \u003c 1000; i++ {\n//\t\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n//\t\t\ttree.Insert(content)\n//\t\t}\n//\n//\t\t// Search for all records\n//\t\tfor i := 0; i \u003c 1000; i++ {\n//\t\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n//\t\t\tval := tree.Get(content)\n//\t\t\tif val == nil {\n//\t\t\t\tt.Errorf(\"Expected key %v, but didn't find it\", content.Key)\n//\t\t\t}\n//\t\t}\n//\n//\t\t// Delete half of the records\n//\t\tfor i := 0; i \u003c 1000; i += 2 {\n//\t\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n//\t\t\ttree.Delete(content)\n//\t\t}\n//\n//\t\t// Search for all records\n//\t\tfor i := 0; i \u003c 1000; i++ {\n//\t\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n//\t\t\tval := tree.Get(content)\n//\t\t\tif i%2 == 0 {\n//\t\t\t\tif val != nil {\n//\t\t\t\t\tt.Errorf(\"Didn't expect key %v, but found key:value %v:%v\", content.Key, val.(Content).Key, val.(Content).Value)\n//\t\t\t\t}\n//\t\t\t} else {\n//\t\t\t\tif val == nil {\n//\t\t\t\t\tt.Errorf(\"Expected key %v, but didn't find it\", content.Key)\n//\t\t\t\t}\n//\t\t\t}\n//\t\t}\n//\t}\n//\n//\t// Now create a very large tree, with 100000 records\n//\t// Then delete roughly one third of them, using a very basic random number generation scheme\n//\t// (implement it right here) to determine which records to delete.\n//\t// Print a few lines using Logf to let the user know what's happening.\n//\n//\tt.Logf(\"Testing B-Tree of degree 10 with 100000 records\\n\")\n//\ttree := New(WithDegree(10))\n//\n//\t// Insert 100000 records\n//\tt.Logf(\"Inserting 100000 records\\n\")\n//\tfor i := 0; i \u003c 100000; i++ {\n//\t\tcontent := Content{Key: i, Value: fmt.Sprintf(\"Value_%d\", i)}\n//\t\ttree.Insert(content)\n//\t}\n//\n//\t// Implement a very basic random number generator\n//\tseed := 0\n//\trandom := func() int {\n//\t\tseed = (seed*1103515245 + 12345) \u0026 0x7fffffff\n//\t\treturn seed\n//\t}\n//\n//\t// Delete one third of the records\n//\tt.Logf(\"Deleting one third of the records\\n\")\n//\tfor i := 0; i \u003c 35000; i++ {\n//\t\tcontent := Content{Key: random() % 100000, Value: fmt.Sprintf(\"Value_%d\", i)}\n//\t\ttree.Delete(content)\n//\t}\n//}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/btree\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"14jesKaZOl9W/h+hKARjfHYJ3fHEeBvlYzpnbrwVyMSQHJzbFJXmqqamTQy31gbJNSw7MW6mb8NgxTYx/sGbCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"context","path":"gno.land/p/demo/context","files":[{"name":"context.gno","body":"// Package context provides a minimal implementation of Go context with support\n// for Value and WithValue.\n//\n// Adapted from https://github.com/golang/go/tree/master/src/context/.\n// Copyright 2016 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\npackage context\n\ntype Context interface {\n\t// Value returns the value associated with this context for key, or nil\n\t// if no value is associated with key.\n\tValue(key any) any\n}\n\n// Empty returns a non-nil, empty context, similar with context.Background and\n// context.TODO in Go.\nfunc Empty() Context {\n\treturn \u0026emptyCtx{}\n}\n\ntype emptyCtx struct{}\n\nfunc (ctx emptyCtx) Value(key any) any {\n\treturn nil\n}\n\nfunc (ctx emptyCtx) String() string {\n\treturn \"context.Empty\"\n}\n\ntype valueCtx struct {\n\tparent   Context\n\tkey, val any\n}\n\nfunc (ctx *valueCtx) Value(key any) any {\n\tif ctx.key == key {\n\t\treturn ctx.val\n\t}\n\treturn ctx.parent.Value(key)\n}\n\nfunc stringify(v any) string {\n\tswitch s := v.(type) {\n\tcase stringer:\n\t\treturn s.String()\n\tcase string:\n\t\treturn s\n\t}\n\treturn \"non-stringer\"\n}\n\ntype stringer interface {\n\tString() string\n}\n\nfunc (c *valueCtx) String() string {\n\treturn stringify(c.parent) + \".WithValue(\" +\n\t\tstringify(c.key) + \", \" +\n\t\tstringify(c.val) + \")\"\n}\n\n// WithValue returns a copy of parent in which the value associated with key is\n// val.\nfunc WithValue(parent Context, key, val any) Context {\n\tif key == nil {\n\t\tpanic(\"nil key\")\n\t}\n\t// XXX: if !reflect.TypeOf(key).Comparable() { panic(\"key is not comparable\") }\n\treturn \u0026valueCtx{parent, key, val}\n}\n"},{"name":"context_test.gno","body":"package context\n\nimport \"testing\"\n\nfunc TestContextExample(t *testing.T) {\n\ttype favContextKey string\n\n\tk := favContextKey(\"language\")\n\tctx := WithValue(Empty(), k, \"Gno\")\n\n\tif v := ctx.Value(k); v != nil {\n\t\tif v.(string) != \"Gno\" {\n\t\t\tt.Errorf(\"language value should be Gno, but is %s\", v)\n\t\t}\n\t} else {\n\t\tt.Errorf(\"language key value was not found\")\n\t}\n\n\tif v := ctx.Value(favContextKey(\"color\")); v != nil {\n\t\tt.Errorf(\"color key was found\")\n\t}\n}\n\n// otherContext is a Context that's not one of the types defined in context.go.\n// This lets us test code paths that differ based on the underlying type of the\n// Context.\ntype otherContext struct {\n\tContext\n}\n\ntype (\n\tkey1 int\n\tkey2 int\n)\n\n// func (k key2) String() string { return fmt.Sprintf(\"%[1]T(%[1]d)\", k) }\n\nvar (\n\tk1 = key1(1)\n\tk2 = key2(1) // same int as k1, different type\n\tk3 = key2(3) // same type as k2, different int\n)\n\nfunc TestValues(t *testing.T) {\n\tcheck := func(c Context, nm, v1, v2, v3 string) {\n\t\tif v, ok := c.Value(k1).(string); ok == (len(v1) == 0) || v != v1 {\n\t\t\tt.Errorf(`%s.Value(k1).(string) = %q, %t want %q, %t`, nm, v, ok, v1, len(v1) != 0)\n\t\t}\n\t\tif v, ok := c.Value(k2).(string); ok == (len(v2) == 0) || v != v2 {\n\t\t\tt.Errorf(`%s.Value(k2).(string) = %q, %t want %q, %t`, nm, v, ok, v2, len(v2) != 0)\n\t\t}\n\t\tif v, ok := c.Value(k3).(string); ok == (len(v3) == 0) || v != v3 {\n\t\t\tt.Errorf(`%s.Value(k3).(string) = %q, %t want %q, %t`, nm, v, ok, v3, len(v3) != 0)\n\t\t}\n\t}\n\n\tc0 := Empty()\n\tcheck(c0, \"c0\", \"\", \"\", \"\")\n\n\tt.Skip() // XXX: depends on https://github.com/gnolang/gno/issues/2386\n\n\tc1 := WithValue(Empty(), k1, \"c1k1\")\n\tcheck(c1, \"c1\", \"c1k1\", \"\", \"\")\n\n\t/*if got, want := c1.String(), `context.Empty.WithValue(context_test.key1, c1k1)`; got != want {\n\t\tt.Errorf(\"c.String() = %q want %q\", got, want)\n\t}*/\n\n\tc2 := WithValue(c1, k2, \"c2k2\")\n\tcheck(c2, \"c2\", \"c1k1\", \"c2k2\", \"\")\n\n\t/*if got, want := fmt.Sprint(c2), `context.Empty.WithValue(context_test.key1, c1k1).WithValue(context_test.key2(1), c2k2)`; got != want {\n\t\tt.Errorf(\"c.String() = %q want %q\", got, want)\n\t}*/\n\n\tc3 := WithValue(c2, k3, \"c3k3\")\n\tcheck(c3, \"c2\", \"c1k1\", \"c2k2\", \"c3k3\")\n\n\tc4 := WithValue(c3, k1, nil)\n\tcheck(c4, \"c4\", \"\", \"c2k2\", \"c3k3\")\n\n\to0 := otherContext{Empty()}\n\tcheck(o0, \"o0\", \"\", \"\", \"\")\n\n\to1 := otherContext{WithValue(Empty(), k1, \"c1k1\")}\n\tcheck(o1, \"o1\", \"c1k1\", \"\", \"\")\n\n\to2 := WithValue(o1, k2, \"o2k2\")\n\tcheck(o2, \"o2\", \"c1k1\", \"o2k2\", \"\")\n\n\to3 := otherContext{c4}\n\tcheck(o3, \"o3\", \"\", \"c2k2\", \"c3k3\")\n\n\to4 := WithValue(o3, k3, nil)\n\tcheck(o4, \"o4\", \"\", \"c2k2\", \"\")\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/context\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"mHq2ic+OMERx2xK5RMs/3o/VYh6YC0clq5xUqfF2kJeUzPnVN8VrAQiLGrjbwojfv9UKyAqwnATz6EkQjrAWBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"entropy","path":"gno.land/p/demo/entropy","files":[{"name":"entropy.gno","body":"// Entropy generates fully deterministic, cost-effective, and hard to guess\n// numbers.\n//\n// It is designed both for single-usage, like seeding math/rand or for being\n// reused which increases the entropy and its cost effectiveness.\n//\n// Disclaimer: this package is unsafe and won't prevent others to guess values\n// in advance.\n//\n// It uses the Bernstein's hash djb2 to be CPU-cycle efficient.\npackage entropy\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"math\"\n\t\"time\"\n)\n\ntype Instance struct {\n\tvalue uint32\n}\n\nfunc New() *Instance {\n\tr := Instance{value: 5381}\n\tr.addEntropy()\n\treturn \u0026r\n}\n\nfunc FromSeed(seed uint32) *Instance {\n\tr := Instance{value: seed}\n\tr.addEntropy()\n\treturn \u0026r\n}\n\nfunc (i *Instance) Seed() uint32 {\n\treturn i.value\n}\n\nfunc (i *Instance) djb2String(input string) {\n\tfor _, c := range input {\n\t\ti.djb2Uint32(uint32(c))\n\t}\n}\n\n// super fast random algorithm.\n// http://www.cse.yorku.ca/~oz/hash.html\nfunc (i *Instance) djb2Uint32(input uint32) {\n\ti.value = (i.value \u003c\u003c 5) + i.value + input\n}\n\n// AddEntropy uses various runtime variables to add entropy to the existing seed.\nfunc (i *Instance) addEntropy() {\n\t// FIXME: reapply the 5381 initial value?\n\n\t// inherit previous entropy\n\t// nothing to do\n\n\t// handle callers\n\t{\n\t\tcurrentRealm := unsafe.CurrentRealm().Address().String()\n\t\ti.djb2String(currentRealm)\n\t\toriginCaller := unsafe.OriginCaller().String()\n\t\ti.djb2String(originCaller)\n\t}\n\n\t// height\n\t{\n\t\theight := runtime.ChainHeight()\n\t\tif height \u003e= math.MaxUint32 {\n\t\t\theight -= math.MaxUint32\n\t\t}\n\t\ti.djb2Uint32(uint32(height))\n\t}\n\n\t// time\n\t{\n\t\tsecs := time.Now().Second()\n\t\ti.djb2Uint32(uint32(secs))\n\t\tnsecs := time.Now().Nanosecond()\n\t\ti.djb2Uint32(uint32(nsecs))\n\t}\n\n\t// FIXME: compute other hard-to-guess but deterministic variables, like real gas?\n}\n\nfunc (i *Instance) Value() uint32 {\n\ti.addEntropy()\n\treturn i.value\n}\n\nfunc (i *Instance) Value64() uint64 {\n\ti.addEntropy()\n\thigh := i.value\n\ti.addEntropy()\n\n\treturn (uint64(high) \u003c\u003c 32) | uint64(i.value)\n}\n"},{"name":"entropy_test.gno","body":"package entropy\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n)\n\nfunc TestInstance(t *testing.T) {\n\tinstance := New()\n\tif instance == nil {\n\t\tt.Errorf(\"instance should not be nil\")\n\t}\n}\n\nfunc TestInstanceValue(t *testing.T) {\n\tbaseEntropy := New()\n\tbaseResult := computeValue(t, baseEntropy)\n\n\tsameHeightEntropy := New()\n\tsameHeightResult := computeValue(t, sameHeightEntropy)\n\n\tif baseResult != sameHeightResult {\n\t\tt.Errorf(\"should have the same result: new=%s, base=%s\", sameHeightResult, baseResult)\n\t}\n\n\ttesting.SkipHeights(1)\n\tdifferentHeightEntropy := New()\n\tdifferentHeightResult := computeValue(t, differentHeightEntropy)\n\n\tif baseResult == differentHeightResult {\n\t\tt.Errorf(\"should have different result: new=%s, base=%s\", differentHeightResult, baseResult)\n\t}\n}\n\nfunc TestInstanceValue64(t *testing.T) {\n\tbaseEntropy := New()\n\tbaseResult := computeValue64(t, baseEntropy)\n\n\tsameHeightEntropy := New()\n\tsameHeightResult := computeValue64(t, sameHeightEntropy)\n\n\tif baseResult != sameHeightResult {\n\t\tt.Errorf(\"should have the same result: new=%s, base=%s\", sameHeightResult, baseResult)\n\t}\n\n\ttesting.SkipHeights(1)\n\tdifferentHeightEntropy := New()\n\tdifferentHeightResult := computeValue64(t, differentHeightEntropy)\n\n\tif baseResult == differentHeightResult {\n\t\tt.Errorf(\"should have different result: new=%s, base=%s\", differentHeightResult, baseResult)\n\t}\n}\n\nfunc computeValue(t *testing.T, r *Instance) string {\n\tt.Helper()\n\n\tout := \"\"\n\tfor i := 0; i \u003c 10; i++ {\n\t\tval := int(r.Value())\n\t\tout += strconv.Itoa(val) + \" \"\n\t}\n\n\treturn out\n}\n\nfunc computeValue64(t *testing.T, r *Instance) string {\n\tt.Helper()\n\n\tout := \"\"\n\tfor i := 0; i \u003c 10; i++ {\n\t\tval := int(r.Value64())\n\t\tout += strconv.Itoa(val) + \" \"\n\t}\n\n\treturn out\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/entropy\"\ngno = \"0.9\"\n"},{"name":"z_filetest.gno","body":"package main\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/entropy\"\n)\n\nfunc main() {\n\t// initial\n\tprintln(\"---\")\n\tr := entropy.New()\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value64())\n\n\t// should be the same\n\tprintln(\"---\")\n\tr = entropy.New()\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value64())\n\n\ttesting.SkipHeights(1)\n\tprintln(\"---\")\n\tr = entropy.New()\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value())\n\tprintln(r.Value64())\n}\n\n// Output:\n// ---\n// 2916840437\n// 3981870829\n// 1311573733\n// 2267403229\n// 484167125\n// 5277608318285564101\n// ---\n// 2916840437\n// 3981870829\n// 1311573733\n// 2267403229\n// 484167125\n// 5277608318285564101\n// ---\n// 3603434145\n// 911682639\n// 1320246589\n// 2861798763\n// 1938803929\n// 17792872788806539637\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"rEgNv/RWpVVj8vZ01/NpWX2TeYQzXF3LI0ejDTUgEMVVY2Kiu8qOny5od1O50skMfnfH5cdl+f9dn3zOUR2KAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"faker","path":"gno.land/p/demo/faker","files":[{"name":"data.gno","body":"package faker\n\nvar (\n\tFirstNames = []string{\n\t\t\"James\", \"Mary\", \"John\", \"Patricia\", \"Robert\", \"Jennifer\", \"Michael\", \"Linda\",\n\t\t\"William\", \"Elizabeth\", \"David\", \"Barbara\", \"Richard\", \"Susan\", \"Joseph\", \"Jessica\",\n\t\t\"Thomas\", \"Sarah\", \"Christopher\", \"Karen\", \"Charles\", \"Nancy\", \"Daniel\", \"Lisa\",\n\t\t\"Matthew\", \"Betty\", \"Anthony\", \"Helen\", \"Mark\", \"Sandra\", \"Donald\", \"Donna\",\n\t\t\"Steven\", \"Carol\", \"Andrew\", \"Ruth\", \"Kenneth\", \"Sharon\", \"Paul\", \"Michelle\",\n\t\t\"Joshua\", \"Laura\", \"Kevin\", \"Sarah\", \"Brian\", \"Kimberly\", \"George\", \"Deborah\",\n\t\t\"Timothy\", \"Dorothy\", \"Ronald\", \"Lisa\", \"Jason\", \"Nancy\", \"Edward\", \"Karen\",\n\t\t\"Jeffrey\", \"Betty\", \"Ryan\", \"Helen\", \"Jacob\", \"Sandra\", \"Gary\", \"Donna\",\n\t\t\"Nicholas\", \"Carol\", \"Eric\", \"Ruth\", \"Jonathan\", \"Sharon\", \"Stephen\", \"Michelle\",\n\t}\n\n\tLastNames = []string{\n\t\t\"Smith\", \"Johnson\", \"Williams\", \"Brown\", \"Jones\", \"Garcia\", \"Miller\", \"Davis\",\n\t\t\"Rodriguez\", \"Martinez\", \"Hernandez\", \"Lopez\", \"Gonzalez\", \"Wilson\", \"Anderson\", \"Thomas\",\n\t\t\"Taylor\", \"Moore\", \"Jackson\", \"Martin\", \"Lee\", \"Perez\", \"Thompson\", \"White\",\n\t\t\"Harris\", \"Sanchez\", \"Clark\", \"Ramirez\", \"Lewis\", \"Robinson\", \"Walker\", \"Young\",\n\t\t\"Allen\", \"King\", \"Wright\", \"Scott\", \"Torres\", \"Nguyen\", \"Hill\", \"Flores\",\n\t\t\"Green\", \"Adams\", \"Nelson\", \"Baker\", \"Hall\", \"Rivera\", \"Campbell\", \"Mitchell\",\n\t\t\"Carter\", \"Roberts\", \"Gomez\", \"Phillips\", \"Evans\", \"Turner\", \"Diaz\", \"Parker\",\n\t\t\"Cruz\", \"Edwards\", \"Collins\", \"Reyes\", \"Stewart\", \"Morris\", \"Morales\", \"Murphy\",\n\t}\n\n\tCities = []string{\n\t\t\"New York\", \"Los Angeles\", \"Chicago\", \"Houston\", \"Phoenix\", \"Philadelphia\",\n\t\t\"San Antonio\", \"San Diego\", \"Dallas\", \"San Jose\", \"Austin\", \"Jacksonville\",\n\t\t\"Fort Worth\", \"Columbus\", \"Charlotte\", \"San Francisco\", \"Indianapolis\", \"Seattle\",\n\t\t\"Denver\", \"Washington\", \"Boston\", \"El Paso\", \"Nashville\", \"Detroit\", \"Oklahoma City\",\n\t\t\"Portland\", \"Las Vegas\", \"Memphis\", \"Louisville\", \"Baltimore\", \"Milwaukee\",\n\t\t\"Albuquerque\", \"Tucson\", \"Fresno\", \"Sacramento\", \"Mesa\", \"Kansas City\",\n\t\t\"Atlanta\", \"Long Beach\", \"Colorado Springs\", \"Raleigh\", \"Miami\", \"Virginia Beach\",\n\t\t\"Omaha\", \"Oakland\", \"Minneapolis\", \"Tulsa\", \"Arlington\", \"Tampa\", \"New Orleans\",\n\t}\n\n\tCountries = []string{\n\t\t\"United States\", \"Canada\", \"Mexico\", \"United Kingdom\", \"France\", \"Germany\",\n\t\t\"Italy\", \"Spain\", \"Japan\", \"China\", \"India\", \"Brazil\", \"Argentina\", \"Australia\",\n\t\t\"Russia\", \"South Korea\", \"Netherlands\", \"Belgium\", \"Switzerland\", \"Sweden\",\n\t\t\"Norway\", \"Denmark\", \"Finland\", \"Poland\", \"Portugal\", \"Greece\", \"Turkey\",\n\t\t\"Egypt\", \"South Africa\", \"Nigeria\", \"Kenya\", \"Morocco\", \"Israel\", \"Saudi Arabia\",\n\t\t\"United Arab Emirates\", \"Thailand\", \"Singapore\", \"Malaysia\", \"Indonesia\", \"Philippines\",\n\t\t\"Vietnam\", \"Taiwan\", \"Hong Kong\", \"New Zealand\", \"Chile\", \"Peru\", \"Colombia\",\n\t}\n\n\tStreetNames = []string{\n\t\t\"Main St\", \"Oak Ave\", \"Elm St\", \"First St\", \"Second Ave\", \"Park Rd\",\n\t\t\"Maple Dr\", \"Cedar Ln\", \"Pine St\", \"Washington Ave\", \"Broadway\",\n\t\t\"Church St\", \"Market St\", \"Union Ave\", \"High St\", \"Mill Rd\",\n\t\t\"School Dr\", \"Hill St\", \"River Rd\", \"Spring Ave\",\n\t}\n\n\tEmailDomains = []string{\n\t\t\"example.com\", \"test.com\", \"demo.org\", \"sample.net\", \"fake.io\",\n\t}\n\n\tLoremWords = []string{\n\t\t\"lorem\", \"ipsum\", \"dolor\", \"sit\", \"amet\", \"consectetur\", \"adipiscing\", \"elit\",\n\t\t\"sed\", \"do\", \"eiusmod\", \"tempor\", \"incididunt\", \"ut\", \"labore\", \"et\", \"dolore\",\n\t\t\"magna\", \"aliqua\", \"enim\", \"ad\", \"minim\", \"veniam\", \"quis\", \"nostrud\",\n\t\t\"exercitation\", \"ullamco\", \"laboris\", \"nisi\", \"aliquip\", \"ex\", \"ea\", \"commodo\",\n\t\t\"consequat\", \"duis\", \"aute\", \"irure\", \"in\", \"reprehenderit\", \"voluptate\",\n\t\t\"velit\", \"esse\", \"cillum\", \"fugiat\", \"nulla\", \"pariatur\", \"excepteur\", \"sint\",\n\t\t\"occaecat\", \"cupidatat\", \"non\", \"proident\", \"sunt\", \"culpa\", \"qui\", \"officia\",\n\t\t\"deserunt\", \"mollit\", \"anim\", \"id\", \"est\", \"laborum\", \"at\", \"vero\", \"eos\",\n\t\t\"accusamus\", \"accusantium\", \"doloremque\", \"laudantium\", \"totam\", \"rem\",\n\t\t\"aperiam\", \"eaque\", \"ipsa\", \"quae\", \"ab\", \"illo\", \"inventore\", \"veritatis\",\n\t\t\"et\", \"quasi\", \"architecto\", \"beatae\", \"vitae\", \"dicta\", \"sunt\", \"explicabo\",\n\t\t\"nemo\", \"ipsam\", \"voluptatem\", \"quia\", \"voluptas\", \"aspernatur\", \"aut\", \"odit\",\n\t\t\"fugit\", \"sed\", \"quia\", \"consequuntur\", \"magni\", \"dolores\", \"ratione\",\n\t\t\"sequi\", \"nesciunt\", \"neque\", \"porro\", \"quisquam\", \"qui\", \"dolorem\",\n\t\t\"ipsum\", \"quia\", \"dolor\", \"sit\", \"amet\", \"consectetur\", \"adipisci\",\n\t\t\"numquam\", \"eius\", \"modi\", \"tempora\", \"incidunt\", \"ut\", \"labore\",\n\t\t\"et\", \"dolore\", \"magnam\", \"aliquam\", \"quaerat\", \"voluptatem\",\n\t\t\"ut\", \"enim\", \"ad\", \"minima\", \"veniam\", \"quis\", \"nostrum\",\n\t\t\"exercitationem\", \"ullam\", \"corporis\", \"suscipit\", \"laboriosam\",\n\t}\n)\n"},{"name":"faker.gno","body":"// Package faker provides fake data generation utilities for testing and\n// development purposes. It includes methods for generating names, addresses,\n// dates, emails, and other common data types using a pseudorandom generator.\npackage faker\n\nimport (\n\t\"crypto/bech32\"\n\t\"math/rand\"\n\t\"strconv\"\n\t\"strings\"\n\t\"unicode\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Faker provides fake data generation using a pseudorandom number generator.\ntype Faker struct {\n\trng *rand.Rand\n}\n\n// NewGenerator creates a new Faker instance with a random seed.\nfunc NewGenerator() *Faker {\n\treturn NewGeneratorWithSeed(entropy.New().Value64())\n}\n\n// NewGeneratorWithSeed creates a new Faker instance with a specified seed.\nfunc NewGeneratorWithSeed(seed uint64) *Faker {\n\treturn \u0026Faker{\n\t\trng: rand.New(rand.NewPCG(seed, 0xdeadbeef)),\n\t}\n}\n\n// FirstName returns a random first name.\nfunc (f *Faker) FirstName() string {\n\treturn f.pickRandom(\u0026FirstNames)\n}\n\n// LastName returns a random last name.\nfunc (f *Faker) LastName() string {\n\treturn f.pickRandom(\u0026LastNames)\n}\n\n// FullName returns a random full name (first name + last name).\nfunc (f *Faker) FullName() string {\n\treturn f.FirstName() + \" \" + f.LastName()\n}\n\n// Age returns a random age between min and min+80.\nfunc (f *Faker) Age(min int) int {\n\treturn f.rng.IntN(80) + min\n}\n\n// City returns a random city name.\nfunc (f *Faker) City() string {\n\treturn f.pickRandom(\u0026Cities)\n}\n\n// Country returns a random country name.\nfunc (f *Faker) Country() string {\n\treturn f.pickRandom(\u0026Countries)\n}\n\n// Address returns a random address in the format \"123 Street Name, City, Country\".\nfunc (f *Faker) Address() string {\n\tvar (\n\t\tstreetNum  = strconv.Itoa(f.rng.IntN(9999) + 1)\n\t\tstreetName = f.pickRandom(\u0026StreetNames)\n\t\tcity       = f.City()\n\t\tcountry    = f.Country()\n\t)\n\n\treturn ufmt.Sprintf(\"%s %s, %s, %s\", streetNum, streetName, city, country)\n}\n\n// Lorem returns random lorem ipsum text with the specified number of words.\nfunc (f *Faker) Lorem(wordCount int) string {\n\tif wordCount \u003c= 0 {\n\t\treturn \"\"\n\t}\n\n\tresult := \"\"\n\n\tfor i := 0; i \u003c wordCount; i++ {\n\t\tif i \u003e 0 {\n\t\t\tresult += \" \"\n\t\t}\n\t\tresult += f.pickRandom(\u0026LoremWords)\n\t}\n\n\treturn result\n}\n\n// LoremSentence returns a random lorem ipsum sentence with 5-15 words.\nfunc (f *Faker) LoremSentence() string {\n\tvar (\n\t\twordCount = f.rng.IntN(10) + 5\n\t\tsentence  = f.Lorem(wordCount)\n\t)\n\n\tif len(sentence) \u003e 0 {\n\t\tsentence = string(sentence[0]-32) + sentence[1:] + \".\"\n\t}\n\n\treturn sentence\n}\n\n// LoremParagraph returns a random lorem ipsum paragraph with 3-8 sentences.\nfunc (f *Faker) LoremParagraph() string {\n\tvar (\n\t\tsentenceCount = f.rng.IntN(5) + 3\n\t\tparagraph     = \"\"\n\t)\n\n\tfor i := 0; i \u003c sentenceCount; i++ {\n\t\tif i \u003e 0 {\n\t\t\tparagraph += \" \"\n\t\t}\n\t\tparagraph += f.LoremSentence()\n\t}\n\n\treturn paragraph\n}\n\n// Date returns a random date in YYYY-MM-DD format between 1974 and 2025.\nfunc (f *Faker) Date() string {\n\tvar (\n\t\tyear  = strconv.Itoa(f.rng.IntN(52) + 1974)\n\t\tmonth = itoa2Digits(f.rng.IntN(12) + 1)\n\t\tday   = itoa2Digits(f.rng.IntN(28) + 1) // Don't bother handling month lengths\n\t)\n\n\treturn ufmt.Sprintf(\"%s-%s-%s\", year, month, day)\n}\n\n// Time returns a random time in HH:MM:SS format (24-hour).\nfunc (f *Faker) Time() string {\n\tvar (\n\t\thour   = itoa2Digits(f.rng.IntN(24))\n\t\tminute = itoa2Digits(f.rng.IntN(60))\n\t\tsecond = itoa2Digits(f.rng.IntN(60))\n\t)\n\n\treturn ufmt.Sprintf(\"%s:%s:%s\", hour, minute, second)\n}\n\n// DateTime returns a random date and time in \"YYYY-MM-DD HH:MM:SS\" format.\nfunc (f *Faker) DateTime() string {\n\treturn f.Date() + \" \" + f.Time()\n}\n\n// Email returns a random email address in firstname.lastname@domain format.\nfunc (f *Faker) Email() string {\n\tvar (\n\t\tfirstName = strings.ToLower(f.FirstName())\n\t\tlastName  = strings.ToLower(f.LastName())\n\t\tdomain    = f.pickRandom(\u0026EmailDomains)\n\t)\n\n\treturn ufmt.Sprintf(\"%s.%s@%s\", firstName, lastName, domain)\n}\n\n// Phone returns a random phone number in (XXX) XXX-XXXX format.\nfunc (f *Faker) Phone() string {\n\tvar (\n\t\tarea     = f.rng.IntN(900) + 100\n\t\texchange = f.rng.IntN(900) + 100\n\t\tnumber   = f.rng.IntN(10000)\n\t)\n\n\treturn ufmt.Sprintf(\"(%d) %d-%d\", area, exchange, number)\n}\n\n// StdAddress returns a random chain.Address.\nfunc (f *Faker) StdAddress() address {\n\trawAddr := make([]byte, 32)\n\tfor i := 0; i \u003c 32; i++ {\n\t\trawAddr[i] = byte(f.rng.IntN(32))\n\t}\n\n\taddr, err := bech32.Encode(\"g\", rawAddr)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn address(addr)\n}\n\n// Username returns a random gno username following the rules:\n// - Start with minimum 3 letters (lowercase)\n// - End with minimum 3 numbers\n// - Less than 20 chars total\n// - Only letters, numbers, and underscore allowed\nfunc (f *Faker) Username() string {\n\tvar (\n\t\tusername = \"\"\n\t\tsources  = []*[]string{\n\t\t\t\u0026FirstNames,\n\t\t\t\u0026LastNames,\n\t\t\t\u0026LoremWords,\n\t\t}\n\t)\n\n\tfor {\n\t\tvar (\n\t\t\tsource = sources[f.rng.IntN(len(sources))]\n\t\t\tword   = strings.ToLower(f.pickRandomLettersOnly(source))\n\t\t\tnext   = username + word\n\t\t)\n\n\t\tif len(next) \u003e 16 \u0026\u0026 len(username) \u003e 3 {\n\t\t\tbreak\n\t\t} else if len(next) \u003c= 16 {\n\t\t\tusername = next\n\t\t}\n\t}\n\n\tfor i := 0; i \u003c 3; i++ {\n\t\tusername += strconv.Itoa(f.rng.IntN(10))\n\t}\n\n\treturn username\n}\n\nfunc (f *Faker) pickRandom(source *[]string) string {\n\treturn (*source)[f.rng.IntN(len(*source))]\n}\n\nfunc (f *Faker) pickRandomLettersOnly(source *[]string) string {\n\tisLettersOnly := func(s string) bool {\n\t\tfor _, r := range s {\n\t\t\tif !unicode.IsLetter(r) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\treturn true\n\t}\n\n\t// Limit attempts to 100 to avoid infinite loops.\n\tfor i := 0; i \u003c 100; i++ {\n\t\tword := f.pickRandom(source)\n\t\tif isLettersOnly(word) {\n\t\t\treturn word\n\t\t}\n\t}\n\n\tpanic(\"could not find a letters-only word after 100 attempts\")\n}\n\nfunc itoa2Digits(n int) string {\n\tstr := strconv.Itoa(n)\n\tif n \u003c 10 {\n\t\treturn \"0\" + str\n\t}\n\treturn str\n}\n\n// defaultFaker is the default Faker instance used by package-level functions.\nvar defaultFaker = NewGenerator()\n\n// FirstName returns a random first name using the default faker.\nfunc FirstName() string {\n\treturn defaultFaker.FirstName()\n}\n\n// LastName returns a random last name using the default faker.\nfunc LastName() string {\n\treturn defaultFaker.LastName()\n}\n\n// FullName returns a random full name using the default faker.\nfunc FullName() string {\n\treturn defaultFaker.FullName()\n}\n\n// Age returns a random age using the default faker.\nfunc Age(min int) int {\n\treturn defaultFaker.Age(min)\n}\n\n// City returns a random city name using the default faker.\nfunc City() string {\n\treturn defaultFaker.City()\n}\n\n// Country returns a random country name using the default faker.\nfunc Country() string {\n\treturn defaultFaker.Country()\n}\n\n// Address returns a random address using the default faker.\nfunc Address() string {\n\treturn defaultFaker.Address()\n}\n\n// Lorem returns random lorem ipsum text using the default faker.\nfunc Lorem(wordCount int) string {\n\treturn defaultFaker.Lorem(wordCount)\n}\n\n// LoremSentence returns a random lorem ipsum sentence using the default faker.\nfunc LoremSentence() string {\n\treturn defaultFaker.LoremSentence()\n}\n\n// LoremParagraph returns a random lorem ipsum paragraph using the default faker.\nfunc LoremParagraph() string {\n\treturn defaultFaker.LoremParagraph()\n}\n\n// Date returns a random date using the default faker.\nfunc Date() string {\n\treturn defaultFaker.Date()\n}\n\n// Time returns a random time using the default faker.\nfunc Time() string {\n\treturn defaultFaker.Time()\n}\n\n// DateTime returns a random date and time using the default faker.\nfunc DateTime() string {\n\treturn defaultFaker.DateTime()\n}\n\n// Email returns a random email address using the default faker.\nfunc Email() string {\n\treturn defaultFaker.Email()\n}\n\n// Phone returns a random phone number using the default faker.\nfunc Phone() string {\n\treturn defaultFaker.Phone()\n}\n\n// StdAddress returns a random chain.Address using the default faker.\nfunc StdAddress() address {\n\treturn defaultFaker.StdAddress()\n}\n\n// Username returns a random gno username using the default faker.\nfunc Username() string {\n\treturn defaultFaker.Username()\n}\n"},{"name":"faker_test.gno","body":"package faker\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestNewGenerator(t *testing.T) {\n\tf := NewGenerator()\n\n\tuassert.NotNil(t, f)\n\tuassert.NotNil(t, f.rng)\n}\n\nfunc TestNewGeneratorWithSeed(t *testing.T) {\n\tvar (\n\t\tseed = uint64(12345)\n\t\tf    = NewGeneratorWithSeed(seed)\n\t)\n\n\tuassert.NotNil(t, f)\n\tuassert.NotNil(t, f.rng)\n}\n\nfunc TestDeterministicOutput(t *testing.T) {\n\tvar (\n\t\tseed = uint64(42)\n\t\tf1   = NewGeneratorWithSeed(seed)\n\t\tf2   = NewGeneratorWithSeed(seed)\n\t)\n\n\tfor i := 0; i \u003c 42; i++ {\n\t\tuassert.Equal(t, f1.FirstName(), f2.FirstName())\n\t\tuassert.Equal(t, f1.LastName(), f2.LastName())\n\t\tuassert.Equal(t, f1.FullName(), f2.FullName())\n\t\tuassert.Equal(t, f1.Age(0), f2.Age(0))\n\t\tuassert.Equal(t, f1.City(), f2.City())\n\t\tuassert.Equal(t, f1.Country(), f2.Country())\n\t\tuassert.Equal(t, f1.Address(), f2.Address())\n\t\tuassert.Equal(t, f1.Lorem(42), f2.Lorem(42))\n\t\tuassert.Equal(t, f1.LoremSentence(), f2.LoremSentence())\n\t\tuassert.Equal(t, f1.LoremParagraph(), f2.LoremParagraph())\n\t\tuassert.Equal(t, f1.Date(), f2.Date())\n\t\tuassert.Equal(t, f1.Time(), f2.Time())\n\t\tuassert.Equal(t, f1.DateTime(), f2.DateTime())\n\t\tuassert.Equal(t, f1.Email(), f2.Email())\n\t\tuassert.Equal(t, f1.Phone(), f2.Phone())\n\t\tuassert.Equal(t, f1.StdAddress(), f2.StdAddress())\n\t\tuassert.Equal(t, f1.Username(), f2.Username())\n\t}\n}\n\nfunc TestPickRandomLettersOnly(cur realm, t *testing.T) {\n\tf := NewGenerator()\n\n\t// Source without any letters only word\n\ttestData := []string{\"hello8\", \"123\", \"6world\", \"hey_hey\", \"ok.\"}\n\n\tuassert.PanicsWithMessage(\n\t\tt, cur,\n\t\t\"could not find a letters-only word after 100 attempts\",\n\t\tfunc() { f.pickRandomLettersOnly(\u0026testData) },\n\t)\n}\n\ntype goldenTestCase struct {\n\tseed       uint64\n\titerations []goldenValues\n}\n\ntype goldenValues struct {\n\tfirstName      string\n\tlastName       string\n\tfullName       string\n\tage            int\n\tcity           string\n\tcountry        string\n\taddress        string\n\tlorem          string\n\tloremSentence  string\n\tloremParagraph string\n\tdate           string\n\ttime           string\n\tdateTime       string\n\temail          string\n\tphone          string\n\tstdAddress     address\n\tusername       string\n}\n\nfunc TestGoldenValues(t *testing.T) {\n\tfor _, testCase := range goldenTestCases { // See golden_test_cases.gno\n\t\tf := NewGeneratorWithSeed(testCase.seed)\n\n\t\tfor i, c := range testCase.iterations {\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.firstName,\n\t\t\t\tf.FirstName(),\n\t\t\t\tufmt.Sprintf(\"First name doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.lastName,\n\t\t\t\tf.LastName(),\n\t\t\t\tufmt.Sprintf(\"Last name doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.fullName,\n\t\t\t\tf.FullName(),\n\t\t\t\tufmt.Sprintf(\"Full name doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.age,\n\t\t\t\tf.Age(0),\n\t\t\t\tufmt.Sprintf(\"Age doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.city,\n\t\t\t\tf.City(),\n\t\t\t\tufmt.Sprintf(\"City doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.country,\n\t\t\t\tf.Country(),\n\t\t\t\tufmt.Sprintf(\"Country doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.address,\n\t\t\t\tf.Address(),\n\t\t\t\tufmt.Sprintf(\"Address doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.lorem,\n\t\t\t\tf.Lorem(42),\n\t\t\t\tufmt.Sprintf(\"Lorem doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.loremSentence,\n\t\t\t\tf.LoremSentence(),\n\t\t\t\tufmt.Sprintf(\"Lorem sentence doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.loremParagraph,\n\t\t\t\tf.LoremParagraph(),\n\t\t\t\tufmt.Sprintf(\"Lorem paragraph doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.date,\n\t\t\t\tf.Date(),\n\t\t\t\tufmt.Sprintf(\"Date doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.time,\n\t\t\t\tf.Time(),\n\t\t\t\tufmt.Sprintf(\"Time doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.dateTime,\n\t\t\t\tf.DateTime(),\n\t\t\t\tufmt.Sprintf(\"DateTime doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.email,\n\t\t\t\tf.Email(),\n\t\t\t\tufmt.Sprintf(\"Email doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.phone,\n\t\t\t\tf.Phone(),\n\t\t\t\tufmt.Sprintf(\"Phone doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.stdAddress,\n\t\t\t\tf.StdAddress(),\n\t\t\t\tufmt.Sprintf(\"StdAddress doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\n\t\t\tuassert.Equal(\n\t\t\t\tt,\n\t\t\t\tc.username,\n\t\t\t\tf.Username(),\n\t\t\t\tufmt.Sprintf(\"Username doesn't match for seed %d (%d)\", testCase.seed, i),\n\t\t\t)\n\t\t}\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/faker\"\ngno = \"0.9\"\n"},{"name":"golden_cases_test.gno","body":"package faker\n\n// To generate these test cases, run the script hosted here: https://gist.github.com/aeddi/9a089bf02e8e4a015f38e3948337189d\nvar goldenTestCases = []goldenTestCase{\n\t{\n\t\tseed: 42,\n\t\titerations: []goldenValues{\n\t\t\t{\n\t\t\t\tfirstName:      \"Nicholas\",\n\t\t\t\tlastName:       \"Jones\",\n\t\t\t\tfullName:       \"Matthew Ramirez\",\n\t\t\t\tage:            10,\n\t\t\t\tcity:           \"Oakland\",\n\t\t\t\tcountry:        \"Greece\",\n\t\t\t\taddress:        \"3413 Church St, Baltimore, Taiwan\",\n\t\t\t\tlorem:          \"aperiam quisquam culpa esse do quis incididunt sed laboriosam et veritatis ex occaecat dolor dolor explicabo ullam et magnam nesciunt nostrud quasi aliquam at dolor sed aliquam minima dolor quia quia corporis proident ullamco lorem enim aut sed rem eos totam exercitation\",\n\t\t\t\tloremSentence:  \"Non aspernatur magna eiusmod pariatur exercitation sit neque consectetur et ea minim dolores.\",\n\t\t\t\tloremParagraph: \"Ab do quis beatae sit consequuntur tempora. Enim nemo incidunt consectetur ad sint suscipit irure doloremque. Pariatur tempor ad minima ullam cillum cillum ipsum.\",\n\t\t\t\tdate:           \"2018-06-03\",\n\t\t\t\ttime:           \"08:03:00\",\n\t\t\t\tdateTime:       \"2013-06-01 03:18:06\",\n\t\t\t\temail:          \"sharon.collins@test.com\",\n\t\t\t\tphone:          \"(198) 641-1385\",\n\t\t\t\tstdAddress:     \"g1km2qq5885nxld9m9plt0x22u3eecy70gzhw3f9\",\n\t\t\t\tusername:       \"charlesrivera056\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tfirstName:      \"Sarah\",\n\t\t\t\tlastName:       \"Lewis\",\n\t\t\t\tfullName:       \"George Turner\",\n\t\t\t\tage:            22,\n\t\t\t\tcity:           \"Boston\",\n\t\t\t\tcountry:        \"Thailand\",\n\t\t\t\taddress:        \"4204 Pine St, Fort Worth, Malaysia\",\n\t\t\t\tlorem:          \"sunt quisquam eos enim veniam ullam et ipsum consectetur quia accusamus totam sint explicabo et ipsum fugiat magnam sit ab quae in quaerat nisi accusantium sit laboriosam quia ipsum quaerat sit non consequat deserunt voluptas beatae ipsa aperiam ex inventore quia eos\",\n\t\t\t\tloremSentence:  \"Excepteur modi quia laudantium et fugit quis rem quia aute vero.\",\n\t\t\t\tloremParagraph: \"Irure quasi dolore minima quaerat enim ullamco aliqua aliquam explicabo nulla. Illo qui dolor totam mollit dolore veritatis tempor vero mollit magna. Laborum non deserunt sunt cupidatat dolores odit sunt numquam eaque deserunt.\",\n\t\t\t\tdate:           \"1976-04-04\",\n\t\t\t\ttime:           \"08:52:55\",\n\t\t\t\tdateTime:       \"2008-12-15 04:05:55\",\n\t\t\t\temail:          \"lisa.rivera@test.com\",\n\t\t\t\tphone:          \"(645) 598-9929\",\n\t\t\t\tstdAddress:     \"g1nphhzdwk07qewm6zlhwpgucn89uxtmqqz7e7ke\",\n\t\t\t\tusername:       \"robertleemartin940\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tfirstName:      \"Stephen\",\n\t\t\t\tlastName:       \"Lee\",\n\t\t\t\tfullName:       \"Susan Hall\",\n\t\t\t\tage:            26,\n\t\t\t\tcity:           \"San Francisco\",\n\t\t\t\tcountry:        \"Thailand\",\n\t\t\t\taddress:        \"7912 Washington Ave, Columbus, Saudi Arabia\",\n\t\t\t\tlorem:          \"ipsum accusantium dicta eius quia dolore eaque reprehenderit porro suscipit inventore cillum cupidatat dolore eius anim ratione accusantium et velit laboriosam dolore excepteur sed ab eiusmod inventore magnam ullam dolore eius non excepteur quis explicabo accusamus dolor suscipit sed nostrum quisquam illo\",\n\t\t\t\tloremSentence:  \"Corporis minim id laudantium sint non occaecat sit porro ut nostrud fugiat.\",\n\t\t\t\tloremParagraph: \"Voluptate totam mollit voluptate fugiat et excepteur numquam dolor proident mollit sed. Proident proident qui qui et dolores ipsam. Enim amet quae incidunt ipsam incididunt quasi quasi corporis accusamus consequat vitae et.\",\n\t\t\t\tdate:           \"1990-10-09\",\n\t\t\t\ttime:           \"08:20:56\",\n\t\t\t\tdateTime:       \"2010-10-06 16:03:34\",\n\t\t\t\temail:          \"sandra.campbell@sample.net\",\n\t\t\t\tphone:          \"(394) 964-9105\",\n\t\t\t\tstdAddress:     \"g1lmwtmeqdethnt3kenlf6327wkp3f9fz7tgsag8\",\n\t\t\t\tusername:       \"inciduntjonathan959\",\n\t\t\t},\n\t\t},\n\t}, {\n\t\tseed: 404,\n\t\titerations: []goldenValues{\n\t\t\t{\n\t\t\t\tfirstName:      \"Lisa\",\n\t\t\t\tlastName:       \"Johnson\",\n\t\t\t\tfullName:       \"Steven Young\",\n\t\t\t\tage:            57,\n\t\t\t\tcity:           \"Denver\",\n\t\t\t\tcountry:        \"Egypt\",\n\t\t\t\taddress:        \"2842 Market St, New Orleans, Sweden\",\n\t\t\t\tlorem:          \"suscipit veritatis in et est sunt nulla sit laudantium minim labore eaque aperiam ipsum quaerat magna sint mollit amet proident commodo magnam cillum modi proident nostrum enim cupidatat excepteur fugiat tempor fugiat lorem aliquam ullamco incididunt dolorem occaecat aliquam adipisci amet dolor\",\n\t\t\t\tloremSentence:  \"Quae sed ullamco reprehenderit aute mollit et deserunt beatae do.\",\n\t\t\t\tloremParagraph: \"Veritatis minim nisi mollit quia veniam explicabo. Aute dicta aliquip corporis adipiscing accusantium ea consectetur labore ipsa minima porro ut quaerat. Nesciunt est consequat explicabo totam consectetur anim. Occaecat numquam dolorem accusantium qui culpa velit nulla incidunt accusantium. Magnam excepteur accusamus voluptatem dolore. Elit ad qui modi cupidatat.\",\n\t\t\t\tdate:           \"1992-03-04\",\n\t\t\t\ttime:           \"06:18:38\",\n\t\t\t\tdateTime:       \"2015-07-14 06:46:34\",\n\t\t\t\temail:          \"nicholas.thompson@fake.io\",\n\t\t\t\tphone:          \"(529) 158-9162\",\n\t\t\t\tstdAddress:     \"g1gnkpd5hlnscj2fcl83rl87rk70denmpj4jp5kk\",\n\t\t\t\tusername:       \"jonesporroquia121\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tfirstName:      \"Michael\",\n\t\t\t\tlastName:       \"Ramirez\",\n\t\t\t\tfullName:       \"Mary Moore\",\n\t\t\t\tage:            5,\n\t\t\t\tcity:           \"Austin\",\n\t\t\t\tcountry:        \"Australia\",\n\t\t\t\taddress:        \"8445 Market St, Detroit, Switzerland\",\n\t\t\t\tlorem:          \"quis aspernatur fugit incidunt et deserunt consectetur culpa eaque laudantium ex minim inventore accusantium enim porro porro at ad laboriosam deserunt consectetur quis et in ratione dolores officia vitae pariatur eos ex cupidatat sit aliquip est ad amet minim illo do ratione\",\n\t\t\t\tloremSentence:  \"Voluptatem aperiam voluptatem accusantium ut.\",\n\t\t\t\tloremParagraph: \"Veniam tempor veniam nemo sequi cillum doloremque consequuntur. Est nostrud aspernatur aute ipsum nostrud. Sit nulla duis ad aute ullam numquam dolorem eiusmod ipsam veniam. Magni sunt amet ea consequat ullamco magna eius irure aspernatur.\",\n\t\t\t\tdate:           \"2015-10-15\",\n\t\t\t\ttime:           \"22:51:05\",\n\t\t\t\tdateTime:       \"2000-11-15 08:54:31\",\n\t\t\t\temail:          \"deborah.gonzalez@example.com\",\n\t\t\t\tphone:          \"(729) 653-7778\",\n\t\t\t\tstdAddress:     \"g1fedr3nyrs59235q9h5c4gxp5cvzwska736rt9s\",\n\t\t\t\tusername:       \"laboriosamruth713\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tfirstName:      \"Lisa\",\n\t\t\t\tlastName:       \"Williams\",\n\t\t\t\tfullName:       \"Sarah Taylor\",\n\t\t\t\tage:            41,\n\t\t\t\tcity:           \"Oakland\",\n\t\t\t\tcountry:        \"Singapore\",\n\t\t\t\taddress:        \"4180 First St, Omaha, Switzerland\",\n\t\t\t\tlorem:          \"voluptate aut ea ut quaerat nemo eos modi deserunt quis cupidatat sequi qui nemo illo dolore ipsam quaerat veniam suscipit minim dolor eius quasi numquam explicabo amet nemo doloremque illo reprehenderit aliquam ea modi consequat minima voluptate ab sed deserunt neque corporis\",\n\t\t\t\tloremSentence:  \"Labore consequat odit accusamus quis labore exercitationem odit enim quisquam voluptatem architecto.\",\n\t\t\t\tloremParagraph: \"Amet consequat neque ratione nostrud. Quasi veniam inventore exercitationem dolore aliquam veniam doloremque enim. Eos enim nostrum sed sit quae ipsa magni labore adipisci. Veniam sed eaque exercitation et sed aliqua ut irure exercitation qui anim adipisci aliquam.\",\n\t\t\t\tdate:           \"2017-05-27\",\n\t\t\t\ttime:           \"20:36:57\",\n\t\t\t\tdateTime:       \"2014-07-01 01:53:12\",\n\t\t\t\temail:          \"donna.gomez@sample.net\",\n\t\t\t\tphone:          \"(250) 473-8838\",\n\t\t\t\tstdAddress:     \"g1c3rzldvfn53ddkeakvqn2k3fq99jtyv3uhkf0a\",\n\t\t\t\tusername:       \"kevinnancycruz246\",\n\t\t\t},\n\t\t},\n\t}, {\n\t\tseed: 1337,\n\t\titerations: []goldenValues{\n\t\t\t{\n\t\t\t\tfirstName:      \"Sarah\",\n\t\t\t\tlastName:       \"Davis\",\n\t\t\t\tfullName:       \"Dorothy Reyes\",\n\t\t\t\tage:            68,\n\t\t\t\tcity:           \"Columbus\",\n\t\t\t\tcountry:        \"Thailand\",\n\t\t\t\taddress:        \"3639 Second Ave, Boston, Indonesia\",\n\t\t\t\tlorem:          \"voluptas fugit anim consequat do sunt esse mollit doloremque sit magna ab sequi nostrud ut porro fugiat laudantium laboriosam voluptatem ipsum tempora ex neque ex aut nostrud et sint ut qui odit ullam quis magnam labore accusantium quia nostrud occaecat quia beatae\",\n\t\t\t\tloremSentence:  \"Veniam aut consequat dicta totam ratione dolore neque nisi nisi dicta qui amet mollit.\",\n\t\t\t\tloremParagraph: \"Dolore magna minima eos aliquip quisquam voluptas. Exercitation minima enim nemo eos mollit commodo. Accusamus ea modi aliqua ut fugiat qui anim illo cillum corporis do eaque. Cupidatat ipsa laboris officia et. Neque veritatis dolor officia quis ut magni tempor quia ipsum adipisci consectetur quis. Dolorem culpa excepteur accusamus explicabo numquam.\",\n\t\t\t\tdate:           \"1974-06-08\",\n\t\t\t\ttime:           \"08:14:32\",\n\t\t\t\tdateTime:       \"2010-04-07 22:25:29\",\n\t\t\t\temail:          \"susan.reyes@demo.org\",\n\t\t\t\tphone:          \"(417) 813-7768\",\n\t\t\t\tstdAddress:     \"g1pcfhsz7menqxg9rzka4j276nhdffjvzjllhfka\",\n\t\t\t\tusername:       \"eiusmoddavis015\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tfirstName:      \"Ruth\",\n\t\t\t\tlastName:       \"Young\",\n\t\t\t\tfullName:       \"Sharon Nguyen\",\n\t\t\t\tage:            61,\n\t\t\t\tcity:           \"Seattle\",\n\t\t\t\tcountry:        \"Greece\",\n\t\t\t\taddress:        \"3013 Oak Ave, Fresno, Mexico\",\n\t\t\t\tlorem:          \"aliqua eius in do esse doloremque accusamus est in quis dolor quisquam laboris dolores quasi illo consequuntur commodo commodo tempora vitae quaerat dolore corporis dolore labore cillum duis lorem dolore nesciunt duis vero minima totam voluptatem aperiam architecto ipsam excepteur vero esse\",\n\t\t\t\tloremSentence:  \"Labore duis labore pariatur aut vero.\",\n\t\t\t\tloremParagraph: \"Adipiscing tempor occaecat reprehenderit magni eius veritatis ut et minim. Vitae beatae nemo quaerat ut. Incididunt ipsum incididunt laboris ad minima deserunt minim quis enim at minim veniam.\",\n\t\t\t\tdate:           \"1979-05-09\",\n\t\t\t\ttime:           \"22:45:41\",\n\t\t\t\tdateTime:       \"2013-07-21 23:52:22\",\n\t\t\t\temail:          \"michelle.parker@demo.org\",\n\t\t\t\tphone:          \"(675) 814-3202\",\n\t\t\t\tstdAddress:     \"g1vvm4wumwhtwjc6jn66jrkkx0wk88w20gw0hnsh\",\n\t\t\t\tusername:       \"barbaradonnaodit581\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tfirstName:      \"Stephen\",\n\t\t\t\tlastName:       \"Garcia\",\n\t\t\t\tfullName:       \"Sarah Walker\",\n\t\t\t\tage:            32,\n\t\t\t\tcity:           \"Boston\",\n\t\t\t\tcountry:        \"Netherlands\",\n\t\t\t\taddress:        \"7697 Pine St, Tucson, Philippines\",\n\t\t\t\tlorem:          \"aperiam corporis velit consequat eius nostrud velit adipiscing fugiat at sunt consectetur inventore quaerat rem nostrum voluptatem odit eiusmod laborum occaecat tempor sed nostrum vero veritatis amet id sit culpa eiusmod lorem do et ipsa sit magna ad aperiam ipsam velit sunt\",\n\t\t\t\tloremSentence:  \"Ipsum ipsum sed quia incidunt.\",\n\t\t\t\tloremParagraph: \"Do exercitation excepteur et laboriosam aspernatur exercitationem dolores. Ipsum lorem et in culpa amet laborum ad consectetur accusamus id. Consequat ullamco qui deserunt elit nisi culpa quia proident exercitationem laborum duis. Dolor aliquam dolorem esse exercitation sunt ipsum sequi. Ipsum ex modi laudantium labore.\",\n\t\t\t\tdate:           \"2000-10-27\",\n\t\t\t\ttime:           \"06:44:19\",\n\t\t\t\tdateTime:       \"2020-01-26 20:36:17\",\n\t\t\t\temail:          \"jonathan.perez@example.com\",\n\t\t\t\tphone:          \"(816) 174-1755\",\n\t\t\t\tstdAddress:     \"g1gnj5tq9tm5vfqqjnue9re3uw5vlc962gp7huu6\",\n\t\t\t\tusername:       \"georgeallenillo577\",\n\t\t\t},\n\t\t},\n\t},\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ujU46J62igvNEoOk5JWn0OpWofwzFddoKTslJ6O8dD5F+cQ6+4IgRmJKh1PWfeF3tS5nh60NGrOFCSc4w3QkBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"flow","path":"gno.land/p/demo/flow","files":[{"name":"LICENSE","body":"https://github.com/mxk/go-flowrate/blob/master/LICENSE\nBSD 3-Clause \"New\" or \"Revised\" License\n\nCopyright (c) 2014 The Go-FlowRate Authors. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n * Redistributions of source code must retain the above copyright\n   notice, this list of conditions and the following disclaimer.\n\n * Redistributions in binary form must reproduce the above copyright\n   notice, this list of conditions and the following disclaimer in the\n   documentation and/or other materials provided with the\n   distribution.\n\n * Neither the name of the go-flowrate project nor the names of its\n   contributors may be used to endorse or promote products derived\n   from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"},{"name":"README.md","body":"Data Flow Rate Control\n======================\n\nTo download and install this package run:\n\ngo get github.com/mxk/go-flowrate/flowrate\n\nThe documentation is available at:\n\nhttp://godoc.org/github.com/mxk/go-flowrate/flowrate\n"},{"name":"flow.gno","body":"//\n// Written by Maxim Khitrov (November 2012)\n//\n// XXX modified to disable blocking, time.Sleep().\n\n// Package flow provides the tools for monitoring and limiting the flow rate\n// of an arbitrary data stream.\npackage flow\n\nimport (\n\t\"math\"\n\t// \"sync\"\n\t\"time\"\n)\n\n// Monitor monitors and limits the transfer rate of a data stream.\ntype Monitor struct {\n\t// mu      sync.Mutex    // Mutex guarding access to all internal fields\n\tactive  bool          // Flag indicating an active transfer\n\tstart   time.Duration // Transfer start time (clock() value)\n\tbytes   int64         // Total number of bytes transferred\n\tsamples int64         // Total number of samples taken\n\n\trSample float64 // Most recent transfer rate sample (bytes per second)\n\trEMA    float64 // Exponential moving average of rSample\n\trPeak   float64 // Peak transfer rate (max of all rSamples)\n\trWindow float64 // rEMA window (seconds)\n\n\tsBytes int64         // Number of bytes transferred since sLast\n\tsLast  time.Duration // Most recent sample time (stop time when inactive)\n\tsRate  time.Duration // Sampling rate\n\n\ttBytes int64         // Number of bytes expected in the current transfer\n\ttLast  time.Duration // Time of the most recent transfer of at least 1 byte\n}\n\n// New creates a new flow control monitor. Instantaneous transfer rate is\n// measured and updated for each sampleRate interval. windowSize determines the\n// weight of each sample in the exponential moving average (EMA) calculation.\n// The exact formulas are:\n//\n//\tsampleTime = currentTime - prevSampleTime\n//\tsampleRate = byteCount / sampleTime\n//\tweight     = 1 - exp(-sampleTime/windowSize)\n//\tnewRate    = weight*sampleRate + (1-weight)*oldRate\n//\n// The default values for sampleRate and windowSize (if \u003c= 0) are 100ms and 1s,\n// respectively.\nfunc New(sampleRate, windowSize time.Duration) *Monitor {\n\tif sampleRate = clockRound(sampleRate); sampleRate \u003c= 0 {\n\t\tsampleRate = 5 * clockRate\n\t}\n\tif windowSize \u003c= 0 {\n\t\twindowSize = 1 * time.Second\n\t}\n\tnow := clock()\n\treturn \u0026Monitor{\n\t\tactive:  true,\n\t\tstart:   now,\n\t\trWindow: windowSize.Seconds(),\n\t\tsLast:   now,\n\t\tsRate:   sampleRate,\n\t\ttLast:   now,\n\t}\n}\n\n// Update records the transfer of n bytes and returns n. It should be called\n// after each Read/Write operation, even if n is 0.\nfunc (m *Monitor) Update(n int) int {\n\t// m.mu.Lock()\n\tm.update(n)\n\t// m.mu.Unlock()\n\treturn n\n}\n\n// Hack to set the current rEMA.\nfunc (m *Monitor) SetREMA(rEMA float64) {\n\t// m.mu.Lock()\n\tm.rEMA = rEMA\n\tm.samples++\n\t// m.mu.Unlock()\n}\n\n// IO is a convenience method intended to wrap io.Reader and io.Writer method\n// execution. It calls m.Update(n) and then returns (n, err) unmodified.\nfunc (m *Monitor) IO(n int, err error) (int, error) {\n\treturn m.Update(n), err\n}\n\n// Done marks the transfer as finished and prevents any further updates or\n// limiting. Instantaneous and current transfer rates drop to 0. Update, IO, and\n// Limit methods become NOOPs. It returns the total number of bytes transferred.\nfunc (m *Monitor) Done() int64 {\n\t// m.mu.Lock()\n\tif now := m.update(0); m.sBytes \u003e 0 {\n\t\tm.reset(now)\n\t}\n\tm.active = false\n\tm.tLast = 0\n\tn := m.bytes\n\t// m.mu.Unlock()\n\treturn n\n}\n\n// timeRemLimit is the maximum Status.TimeRem value.\nconst timeRemLimit = 999*time.Hour + 59*time.Minute + 59*time.Second\n\n// Status represents the current Monitor status. All transfer rates are in bytes\n// per second rounded to the nearest byte.\ntype Status struct {\n\tActive   bool          // Flag indicating an active transfer\n\tStart    time.Time     // Transfer start time\n\tDuration time.Duration // Time period covered by the statistics\n\tIdle     time.Duration // Time since the last transfer of at least 1 byte\n\tBytes    int64         // Total number of bytes transferred\n\tSamples  int64         // Total number of samples taken\n\tInstRate int64         // Instantaneous transfer rate\n\tCurRate  int64         // Current transfer rate (EMA of InstRate)\n\tAvgRate  int64         // Average transfer rate (Bytes / Duration)\n\tPeakRate int64         // Maximum instantaneous transfer rate\n\tBytesRem int64         // Number of bytes remaining in the transfer\n\tTimeRem  time.Duration // Estimated time to completion\n\tProgress Percent       // Overall transfer progress\n}\n\nfunc (s Status) String() string {\n\treturn \"STATUS{}\"\n}\n\n// Status returns current transfer status information. The returned value\n// becomes static after a call to Done.\nfunc (m *Monitor) Status() Status {\n\t// m.mu.Lock()\n\tnow := m.update(0)\n\ts := Status{\n\t\tActive:   m.active,\n\t\tStart:    clockToTime(m.start),\n\t\tDuration: m.sLast - m.start,\n\t\tIdle:     now - m.tLast,\n\t\tBytes:    m.bytes,\n\t\tSamples:  m.samples,\n\t\tPeakRate: round(m.rPeak),\n\t\tBytesRem: m.tBytes - m.bytes,\n\t\tProgress: percentOf(float64(m.bytes), float64(m.tBytes)),\n\t}\n\tif s.BytesRem \u003c 0 {\n\t\ts.BytesRem = 0\n\t}\n\tif s.Duration \u003e 0 {\n\t\trAvg := float64(s.Bytes) / s.Duration.Seconds()\n\t\ts.AvgRate = round(rAvg)\n\t\tif s.Active {\n\t\t\ts.InstRate = round(m.rSample)\n\t\t\ts.CurRate = round(m.rEMA)\n\t\t\tif s.BytesRem \u003e 0 {\n\t\t\t\tif tRate := 0.8*m.rEMA + 0.2*rAvg; tRate \u003e 0 {\n\t\t\t\t\tns := float64(s.BytesRem) / tRate * 1e9\n\t\t\t\t\tif ns \u003e float64(timeRemLimit) {\n\t\t\t\t\t\tns = float64(timeRemLimit)\n\t\t\t\t\t}\n\t\t\t\t\ts.TimeRem = clockRound(time.Duration(ns))\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t// m.mu.Unlock()\n\treturn s\n}\n\n// Limit restricts the instantaneous (per-sample) data flow to rate bytes per\n// second. It returns the maximum number of bytes (0 \u003c= n \u003c= want) that may be\n// transferred immediately without exceeding the limit. If block == true, the\n// call blocks until n \u003e 0. want is returned unmodified if want \u003c 1, rate \u003c 1,\n// or the transfer is inactive (after a call to Done).\n//\n// At least one byte is always allowed to be transferred in any given sampling\n// period. Thus, if the sampling rate is 100ms, the lowest achievable flow rate\n// is 10 bytes per second.\n//\n// For usage examples, see the implementation of Reader and Writer in io.go.\nfunc (m *Monitor) Limit(want int, rate int64, block bool) (n int) {\n\tif block {\n\t\tpanic(\"blocking not yet supported\")\n\t}\n\tif want \u003c 1 || rate \u003c 1 {\n\t\treturn want\n\t}\n\t// m.mu.Lock()\n\n\t// Determine the maximum number of bytes that can be sent in one sample\n\tlimit := round(float64(rate) * m.sRate.Seconds())\n\tif limit \u003c= 0 {\n\t\tlimit = 1\n\t}\n\n\t_ = m.update(0)\n\t/* XXX\n\t// If block == true, wait until m.sBytes \u003c limit\n\tif now := m.update(0); block {\n\t\tfor m.sBytes \u003e= limit \u0026\u0026 m.active {\n\t\t\tnow = m.waitNextSample(now)\n\t\t}\n\t}\n\t*/\n\n\t// Make limit \u003c= want (unlimited if the transfer is no longer active)\n\tif limit -= m.sBytes; limit \u003e int64(want) || !m.active {\n\t\tlimit = int64(want)\n\t}\n\t// m.mu.Unlock()\n\n\tif limit \u003c 0 {\n\t\tlimit = 0\n\t}\n\treturn int(limit)\n}\n\n// SetTransferSize specifies the total size of the data transfer, which allows\n// the Monitor to calculate the overall progress and time to completion.\nfunc (m *Monitor) SetTransferSize(bytes int64) {\n\tif bytes \u003c 0 {\n\t\tbytes = 0\n\t}\n\t// m.mu.Lock()\n\tm.tBytes = bytes\n\t// m.mu.Unlock()\n}\n\n// update accumulates the transferred byte count for the current sample until\n// clock() - m.sLast \u003e= m.sRate. The monitor status is updated once the current\n// sample is done.\nfunc (m *Monitor) update(n int) (now time.Duration) {\n\tif !m.active {\n\t\treturn\n\t}\n\tif now = clock(); n \u003e 0 {\n\t\tm.tLast = now\n\t}\n\tm.sBytes += int64(n)\n\tif sTime := now - m.sLast; sTime \u003e= m.sRate {\n\t\tt := sTime.Seconds()\n\t\tif m.rSample = float64(m.sBytes) / t; m.rSample \u003e m.rPeak {\n\t\t\tm.rPeak = m.rSample\n\t\t}\n\n\t\t// Exponential moving average using a method similar to *nix load\n\t\t// average calculation. Longer sampling periods carry greater weight.\n\t\tif m.samples \u003e 0 {\n\t\t\tw := math.Exp(-t / m.rWindow)\n\t\t\tm.rEMA = m.rSample + w*(m.rEMA-m.rSample)\n\t\t} else {\n\t\t\tm.rEMA = m.rSample\n\t\t}\n\t\tm.reset(now)\n\t}\n\treturn\n}\n\n// reset clears the current sample state in preparation for the next sample.\nfunc (m *Monitor) reset(sampleTime time.Duration) {\n\tm.bytes += m.sBytes\n\tm.samples++\n\tm.sBytes = 0\n\tm.sLast = sampleTime\n}\n\n/*\n// waitNextSample sleeps for the remainder of the current sample. The lock is\n// released and reacquired during the actual sleep period, so it's possible for\n// the transfer to be inactive when this method returns.\nfunc (m *Monitor) waitNextSample(now time.Duration) time.Duration {\n\tconst minWait = 5 * time.Millisecond\n\tcurrent := m.sLast\n\n\t// sleep until the last sample time changes (ideally, just one iteration)\n\tfor m.sLast == current \u0026\u0026 m.active {\n\t\td := current + m.sRate - now\n\t\t// m.mu.Unlock()\n\t\tif d \u003c minWait {\n\t\t\td = minWait\n\t\t}\n\t\ttime.Sleep(d)\n\t\t// m.mu.Lock()\n\t\tnow = m.update(0)\n\t}\n\treturn now\n}\n*/\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/flow\"\ngno = \"0.9\"\n"},{"name":"io.gno","body":"//\n// Written by Maxim Khitrov (November 2012)\n//\n\npackage flow\n\nimport (\n\t\"errors\"\n\t\"io\"\n)\n\n// ErrLimit is returned by the Writer when a non-blocking write is short due to\n// the transfer rate limit.\nvar ErrLimit = errors.New(\"flowrate: flow rate limit exceeded\")\n\n// Limiter is implemented by the Reader and Writer to provide a consistent\n// interface for monitoring and controlling data transfer.\ntype Limiter interface {\n\tDone() int64\n\tStatus() Status\n\tSetTransferSize(bytes int64)\n\tSetLimit(new int64) (old int64)\n\tSetBlocking(new bool) (old bool)\n}\n\n// Reader implements io.ReadCloser with a restriction on the rate of data\n// transfer.\ntype Reader struct {\n\tio.Reader // Data source\n\t*Monitor  // Flow control monitor\n\n\tlimit int64 // Rate limit in bytes per second (unlimited when \u003c= 0)\n\tblock bool  // What to do when no new bytes can be read due to the limit\n}\n\n// NewReader restricts all Read operations on r to limit bytes per second.\nfunc NewReader(r io.Reader, limit int64) *Reader {\n\treturn \u0026Reader{r, New(0, 0), limit, false} // XXX default false\n}\n\n// Read reads up to len(p) bytes into p without exceeding the current transfer\n// rate limit. It returns (0, nil) immediately if r is non-blocking and no new\n// bytes can be read at this time.\nfunc (r *Reader) Read(p []byte) (n int, err error) {\n\tp = p[:r.Limit(len(p), r.limit, r.block)]\n\tif len(p) \u003e 0 {\n\t\tn, err = r.IO(r.Reader.Read(p))\n\t}\n\treturn\n}\n\n// SetLimit changes the transfer rate limit to new bytes per second and returns\n// the previous setting.\nfunc (r *Reader) SetLimit(new int64) (old int64) {\n\told, r.limit = r.limit, new\n\treturn\n}\n\n// SetBlocking changes the blocking behavior and returns the previous setting. A\n// Read call on a non-blocking reader returns immediately if no additional bytes\n// may be read at this time due to the rate limit.\nfunc (r *Reader) SetBlocking(new bool) (old bool) {\n\tif new == true {\n\t\tpanic(\"blocking not yet supported\")\n\t}\n\told, r.block = r.block, new\n\treturn\n}\n\n// Close closes the underlying reader if it implements the io.Closer interface.\nfunc (r *Reader) Close() error {\n\tdefer r.Done()\n\tif c, ok := r.Reader.(io.Closer); ok {\n\t\treturn c.Close()\n\t}\n\treturn nil\n}\n\n// Writer implements io.WriteCloser with a restriction on the rate of data\n// transfer.\ntype Writer struct {\n\tio.Writer // Data destination\n\t*Monitor  // Flow control monitor\n\n\tlimit int64 // Rate limit in bytes per second (unlimited when \u003c= 0)\n\tblock bool  // What to do when no new bytes can be written due to the limit\n}\n\n// NewWriter restricts all Write operations on w to limit bytes per second. The\n// transfer rate and the default blocking behavior (true) can be changed\n// directly on the returned *Writer.\nfunc NewWriter(w io.Writer, limit int64) *Writer {\n\treturn \u0026Writer{w, New(0, 0), limit, false} // XXX default false\n}\n\n// Write writes len(p) bytes from p to the underlying data stream without\n// exceeding the current transfer rate limit. It returns (n, ErrLimit) if w is\n// non-blocking and no additional bytes can be written at this time.\nfunc (w *Writer) Write(p []byte) (n int, err error) {\n\tvar c int\n\tfor len(p) \u003e 0 \u0026\u0026 err == nil {\n\t\ts := p[:w.Limit(len(p), w.limit, w.block)]\n\t\tif len(s) \u003e 0 {\n\t\t\tc, err = w.IO(w.Writer.Write(s))\n\t\t} else {\n\t\t\treturn n, ErrLimit\n\t\t}\n\t\tp = p[c:]\n\t\tn += c\n\t}\n\treturn\n}\n\n// SetLimit changes the transfer rate limit to new bytes per second and returns\n// the previous setting.\nfunc (w *Writer) SetLimit(new int64) (old int64) {\n\told, w.limit = w.limit, new\n\treturn\n}\n\n// SetBlocking changes the blocking behavior and returns the previous setting. A\n// Write call on a non-blocking writer returns as soon as no additional bytes\n// may be written at this time due to the rate limit.\nfunc (w *Writer) SetBlocking(new bool) (old bool) {\n\told, w.block = w.block, new\n\treturn\n}\n\n// Close closes the underlying writer if it implements the io.Closer interface.\nfunc (w *Writer) Close() error {\n\tdefer w.Done()\n\tif c, ok := w.Writer.(io.Closer); ok {\n\t\treturn c.Close()\n\t}\n\treturn nil\n}\n"},{"name":"io_test.gno","body":"//\n// Written by Maxim Khitrov (November 2012)\n//\n\npackage flow\n\nimport (\n\t\"bytes\"\n\t\"testing\"\n\t\"time\"\n\n\tios_test \"os\"\n)\n\n// XXX ugh, I can't even sleep milliseconds.\n// XXX\n\nconst (\n\t_50ms  = 50 * time.Millisecond\n\t_100ms = 100 * time.Millisecond\n\t_200ms = 200 * time.Millisecond\n\t_300ms = 300 * time.Millisecond\n\t_400ms = 400 * time.Millisecond\n\t_500ms = 500 * time.Millisecond\n)\n\nfunc nextStatus(m *Monitor) Status {\n\tsamples := m.samples\n\tfor i := 0; i \u003c 30; i++ {\n\t\tif s := m.Status(); s.Samples != samples {\n\t\t\treturn s\n\t\t}\n\t\tios_test.Sleep(5 * time.Millisecond)\n\t}\n\treturn m.Status()\n}\n\nfunc TestReader(t *testing.T) {\n\tin := make([]byte, 100)\n\tfor i := range in {\n\t\tin[i] = byte(i)\n\t}\n\tb := make([]byte, 100)\n\tr := NewReader(bytes.NewReader(in), 100)\n\tstart := time.Now()\n\n\t// Make sure r implements Limiter\n\t_ = Limiter(r)\n\n\t// 1st read of 10 bytes is performed immediately\n\tif n, err := r.Read(b); n != 10 {\n\t\tt.Fatalf(\"r.Read(b) expected 10 (\u003cnil\u003e); got %v\", n)\n\t} else if err != nil {\n\t\tt.Fatalf(\"r.Read(b) expected 10 (\u003cnil\u003e); got %v (%v)\", n, err.Error())\n\t} else if rt := time.Since(start); rt \u003e _50ms {\n\t\tt.Fatalf(\"r.Read(b) took too long (%v)\", rt.String())\n\t}\n\n\t// No new Reads allowed in the current sample\n\tr.SetBlocking(false)\n\tif n, err := r.Read(b); n != 0 {\n\t\tt.Fatalf(\"r.Read(b) expected 0 (\u003cnil\u003e); got %v\", n)\n\t} else if err != nil {\n\t\tt.Fatalf(\"r.Read(b) expected 0 (\u003cnil\u003e); got %v (%v)\", n, err.Error())\n\t} else if rt := time.Since(start); rt \u003e _50ms {\n\t\tt.Fatalf(\"r.Read(b) took too long (%v)\", rt.String())\n\t}\n\n\tstatus := [6]Status{0: r.Status()} // No samples in the first status\n\n\t// 2nd read of 10 bytes blocks until the next sample\n\t// r.SetBlocking(true)\n\tios_test.Sleep(100 * time.Millisecond)\n\tif n, err := r.Read(b[10:]); n != 10 {\n\t\tt.Fatalf(\"r.Read(b[10:]) expected 10 (\u003cnil\u003e); got %v\", n)\n\t} else if err != nil {\n\t\tt.Fatalf(\"r.Read(b[10:]) expected 10 (\u003cnil\u003e); got %v (%v)\", n, err.Error())\n\t} else if rt := time.Since(start); rt \u003c _100ms {\n\t\tt.Fatalf(\"r.Read(b[10:]) returned ahead of time (%v)\", rt.String())\n\t}\n\n\tstatus[1] = r.Status()            // 1st sample\n\tstatus[2] = nextStatus(r.Monitor) // 2nd sample\n\tstatus[3] = nextStatus(r.Monitor) // No activity for the 3rd sample\n\n\tif n := r.Done(); n != 20 {\n\t\tt.Fatalf(\"r.Done() expected 20; got %v\", n)\n\t}\n\n\tstatus[4] = r.Status()\n\tstatus[5] = nextStatus(r.Monitor) // Timeout\n\tstart = status[0].Start\n\n\t// Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress\n\twant := []Status{\n\t\t{true, start, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\n\t\t{true, start, _100ms, 0, 10, 1, 100, 100, 100, 100, 0, 0, 0},\n\t\t{true, start, _200ms, _100ms, 20, 2, 100, 100, 100, 100, 0, 0, 0},\n\t\t{true, start, _300ms, _200ms, 20, 3, 0, 90, 67, 100, 0, 0, 0},\n\t\t{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},\n\t\t{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},\n\t}\n\tfor i, s := range status {\n\t\t// XXX s := s\n\t\tif !statusesAreEqual(\u0026s, \u0026want[i]) {\n\t\t\tt.Errorf(\"r.Status(%v)\\nexpected: %v\\ngot     : %v\", i, want[i].String(), s.String())\n\t\t}\n\t}\n\tif !bytes.Equal(b[:20], in[:20]) {\n\t\tt.Errorf(\"r.Read() input doesn't match output\")\n\t}\n}\n\n// XXX blocking writer test doesn't work.\nfunc _TestWriter(t *testing.T) {\n\tb := make([]byte, 100)\n\tfor i := range b {\n\t\tb[i] = byte(i)\n\t}\n\tw := NewWriter(\u0026bytes.Buffer{}, 200)\n\tstart := time.Now()\n\n\t// Make sure w implements Limiter\n\t_ = Limiter(w)\n\n\t// Non-blocking 20-byte write for the first sample returns ErrLimit\n\tw.SetBlocking(false)\n\tif n, err := w.Write(b); n != 20 || err != ErrLimit {\n\t\tt.Fatalf(\"w.Write(b) expected 20 (ErrLimit); got %v (%v)\", n, err.Error())\n\t} else if rt := time.Since(start); rt \u003e _50ms {\n\t\tt.Fatalf(\"w.Write(b) took too long (%v)\", rt)\n\t}\n\n\t// Blocking 80-byte write\n\t// w.SetBlocking(true)\n\t// XXX This test doesn't work, because w.Write calls w.Limit(block=false),\n\t// XXX and it returns ErrLimit after 20. What we want is to keep waiting until 80 is returned,\n\t// XXX but blocking isn't supported. Sleeping 800 shouldn't be sufficient either (its a burst).\n\t// XXX This limits the usage of Limiter and m.Limit().\n\tios_test.Sleep(800 * time.Millisecond)\n\tif n, err := w.Write(b[20:]); n \u003c 80 {\n\t} else if n != 80 || err != nil {\n\t\tt.Fatalf(\"w.Write(b[20:]) expected 80 (\u003cnil\u003e); got %v (%v)\", n, err.Error())\n\t} else if rt := time.Since(start); rt \u003c _300ms {\n\t\t// Explanation for `rt \u003c _300ms` (as opposed to `\u003c _400ms`)\n\t\t//\n\t\t//                 |\u003c-- start        |        |\n\t\t// epochs: -----0ms|---100ms|---200ms|---300ms|---400ms\n\t\t// sends:        20|20      |20      |20      |20#\n\t\t//\n\t\t// NOTE: The '#' symbol can thus happen before 400ms is up.\n\t\t// Thus, we can only panic if rt \u003c _300ms.\n\t\tt.Fatalf(\"w.Write(b[20:]) returned ahead of time (%v)\", rt.String())\n\t}\n\n\tw.SetTransferSize(100)\n\tstatus := []Status{w.Status(), nextStatus(w.Monitor)}\n\tstart = status[0].Start\n\n\t// Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress\n\twant := []Status{\n\t\t{true, start, _400ms, 0, 80, 4, 200, 200, 200, 200, 20, _100ms, 80000},\n\t\t{true, start, _500ms, _100ms, 100, 5, 200, 200, 200, 200, 0, 0, 100000},\n\t}\n\tfor i, s := range status {\n\t\t// XXX s := s\n\t\tif !statusesAreEqual(\u0026s, \u0026want[i]) {\n\t\t\tt.Errorf(\"w.Status(%v)\\nexpected: %v\\ngot     : %v\\n\", i, want[i].String(), s.String())\n\t\t}\n\t}\n\tif !bytes.Equal(b, w.Writer.(*bytes.Buffer).Bytes()) {\n\t\tt.Errorf(\"w.Write() input doesn't match output\")\n\t}\n}\n\nconst (\n\tmaxDeviationForDuration       = 50 * time.Millisecond\n\tmaxDeviationForRate     int64 = 50\n)\n\n// statusesAreEqual returns true if s1 is equal to s2. Equality here means\n// general equality of fields except for the duration and rates, which can\n// drift due to unpredictable delays (e.g. thread wakes up 25ms after\n// `time.Sleep` has ended).\nfunc statusesAreEqual(s1 *Status, s2 *Status) bool {\n\tif s1.Active == s2.Active \u0026\u0026\n\t\ts1.Start == s2.Start \u0026\u0026\n\t\tdurationsAreEqual(s1.Duration, s2.Duration, maxDeviationForDuration) \u0026\u0026\n\t\ts1.Idle == s2.Idle \u0026\u0026\n\t\ts1.Bytes == s2.Bytes \u0026\u0026\n\t\ts1.Samples == s2.Samples \u0026\u0026\n\t\tratesAreEqual(s1.InstRate, s2.InstRate, maxDeviationForRate) \u0026\u0026\n\t\tratesAreEqual(s1.CurRate, s2.CurRate, maxDeviationForRate) \u0026\u0026\n\t\tratesAreEqual(s1.AvgRate, s2.AvgRate, maxDeviationForRate) \u0026\u0026\n\t\tratesAreEqual(s1.PeakRate, s2.PeakRate, maxDeviationForRate) \u0026\u0026\n\t\ts1.BytesRem == s2.BytesRem \u0026\u0026\n\t\tdurationsAreEqual(s1.TimeRem, s2.TimeRem, maxDeviationForDuration) \u0026\u0026\n\t\ts1.Progress == s2.Progress {\n\t\treturn true\n\t}\n\treturn false\n}\n\nfunc durationsAreEqual(d1 time.Duration, d2 time.Duration, maxDeviation time.Duration) bool {\n\treturn d2-d1 \u003c= maxDeviation\n}\n\nfunc ratesAreEqual(r1 int64, r2 int64, maxDeviation int64) bool {\n\tsub := r1 - r2\n\tif sub \u003c 0 {\n\t\tsub = -sub\n\t}\n\tif sub \u003c= maxDeviation {\n\t\treturn true\n\t}\n\treturn false\n}\n"},{"name":"util.gno","body":"//\n// Written by Maxim Khitrov (November 2012)\n//\n\npackage flow\n\nimport (\n\t\"math\"\n\t\"strconv\"\n\t\"time\"\n)\n\n// clockRate is the resolution and precision of clock().\nconst clockRate = 20 * time.Millisecond\n\n// czero is the process start time rounded down to the nearest clockRate\n// increment.\nvar czero = time.Now().Round(clockRate)\n\n// clock returns a low resolution timestamp relative to the process start time.\nfunc clock() time.Duration {\n\treturn time.Now().Round(clockRate).Sub(czero)\n}\n\n// clockToTime converts a clock() timestamp to an absolute time.Time value.\nfunc clockToTime(c time.Duration) time.Time {\n\treturn czero.Add(c)\n}\n\n// clockRound returns d rounded to the nearest clockRate increment.\nfunc clockRound(d time.Duration) time.Duration {\n\treturn (d + clockRate\u003e\u003e1) / clockRate * clockRate\n}\n\n// round returns x rounded to the nearest int64 (non-negative values only).\nfunc round(x float64) int64 {\n\tif _, frac := math.Modf(x); frac \u003e= 0.5 {\n\t\treturn int64(math.Ceil(x))\n\t}\n\treturn int64(math.Floor(x))\n}\n\n// Percent represents a percentage in increments of 1/1000th of a percent.\ntype Percent uint32\n\n// percentOf calculates what percent of the total is x.\nfunc percentOf(x, total float64) Percent {\n\tif x \u003c 0 || total \u003c= 0 {\n\t\treturn 0\n\t} else if p := round(x / total * 1e5); p \u003c= math.MaxUint32 {\n\t\treturn Percent(p)\n\t}\n\treturn Percent(math.MaxUint32)\n}\n\nfunc (p Percent) Float() float64 {\n\treturn float64(p) * 1e-3\n}\n\nfunc (p Percent) String() string {\n\tvar buf [12]byte\n\tb := strconv.AppendUint(buf[:0], uint64(p)/1000, 10)\n\tn := len(b)\n\tb = strconv.AppendUint(b, 1000+uint64(p)%1000, 10)\n\tb[n] = '.'\n\treturn string(append(b, '%'))\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"arpHERvIUd1mEFAcllnx+RVL8qL3adxS9xT7gZaIU3oj+aHGwoEJpDJNTbpA8B5k166G5k6oAHqeD0O+4QoDCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"int32","path":"gno.land/p/demo/math_eval/int32","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/math_eval/int32\"\ngno = \"0.9\"\n"},{"name":"int32.gno","body":"// eval/int32 is a evaluator for int32 expressions.\n// This code is heavily forked from https://github.com/dengsgo/math-engine\n// which is licensed under Apache 2.0:\n// https://raw.githubusercontent.com/dengsgo/math-engine/298e2b57b7e7350d0f67bd036916efd5709abe25/LICENSE\npackage int32\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst (\n\tIdentifier = iota\n\tNumber     // numbers\n\tOperator   // +, -, *, /, etc.\n\tVariable   // x, y, z, etc. (one-letter only)\n)\n\ntype expression interface {\n\tString() string\n}\n\ntype expressionRaw struct {\n\texpression string\n\tType       int\n\tFlag       int\n\tOffset     int\n}\n\ntype parser struct {\n\tInput  string\n\tch     byte\n\toffset int\n\terr    error\n}\n\ntype expressionNumber struct {\n\tVal int\n\tStr string\n}\n\ntype expressionVariable struct {\n\tVal int\n\tStr string\n}\n\ntype expressionOperation struct {\n\tOp string\n\tLhs,\n\tRhs expression\n}\n\ntype ast struct {\n\trawexpressions    []*expressionRaw\n\tsource            string\n\tcurrentexpression *expressionRaw\n\tcurrentIndex      int\n\tdepth             int\n\terr               error\n}\n\n// Parse takes an expression string, e.g. \"1+2\" and returns\n// a parsed expression. If there is an error it will return.\nfunc Parse(s string) (ar expression, err error) {\n\ttoks, err := lexer(s)\n\tif err != nil {\n\t\treturn\n\t}\n\tast, err := newAST(toks, s)\n\tif err != nil {\n\t\treturn\n\t}\n\tar, err = ast.parseExpression()\n\treturn\n}\n\n// Eval takes a parsed expression and a map of variables (or nil). The parsed\n// expression is evaluated using any variables and returns the\n// resulting int and/or error.\nfunc Eval(expr expression, variables map[string]int) (res int, err error) {\n\tif err != nil {\n\t\treturn\n\t}\n\tvar l, r int\n\tswitch expr.(type) {\n\tcase expressionVariable:\n\t\tast := expr.(expressionVariable)\n\t\tok := false\n\t\tif variables != nil {\n\t\t\tres, ok = variables[ast.Str]\n\t\t}\n\t\tif !ok {\n\t\t\terr = ufmt.Errorf(\"variable '%s' not found\", ast.Str)\n\t\t}\n\t\treturn\n\tcase expressionOperation:\n\t\tast := expr.(expressionOperation)\n\t\tl, err = Eval(ast.Lhs, variables)\n\t\tif err != nil {\n\t\t\treturn\n\t\t}\n\t\tr, err = Eval(ast.Rhs, variables)\n\t\tif err != nil {\n\t\t\treturn\n\t\t}\n\t\tswitch ast.Op {\n\t\tcase \"+\":\n\t\t\tres = l + r\n\t\tcase \"-\":\n\t\t\tres = l - r\n\t\tcase \"*\":\n\t\t\tres = l * r\n\t\tcase \"/\":\n\t\t\tif r == 0 {\n\t\t\t\terr = ufmt.Errorf(\"violation of arithmetic specification: a division by zero in Eval: [%d/%d]\", l, r)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tres = l / r\n\t\tcase \"%\":\n\t\t\tif r == 0 {\n\t\t\t\tres = 0\n\t\t\t} else {\n\t\t\t\tres = l % r\n\t\t\t}\n\t\tcase \"^\":\n\t\t\tres = l ^ r\n\t\tcase \"\u003e\u003e\":\n\t\t\tres = l \u003e\u003e r\n\t\tcase \"\u003c\u003c\":\n\t\t\tres = l \u003c\u003c r\n\t\tcase \"\u003e\":\n\t\t\tif l \u003e r {\n\t\t\t\tres = 1\n\t\t\t} else {\n\t\t\t\tres = 0\n\t\t\t}\n\t\tcase \"\u003c\":\n\t\t\tif l \u003c r {\n\t\t\t\tres = 1\n\t\t\t} else {\n\t\t\t\tres = 0\n\t\t\t}\n\t\tcase \"\u0026\":\n\t\t\tres = l \u0026 r\n\t\tcase \"|\":\n\t\t\tres = l | r\n\t\tdefault:\n\n\t\t}\n\tcase expressionNumber:\n\t\tres = expr.(expressionNumber).Val\n\t}\n\n\treturn\n}\n\nfunc expressionError(s string, pos int) string {\n\tr := strings.Repeat(\"-\", len(s)) + \"\\n\"\n\ts += \"\\n\"\n\tfor i := 0; i \u003c pos; i++ {\n\t\ts += \" \"\n\t}\n\ts += \"^\\n\"\n\treturn r + s + r\n}\n\nfunc (n expressionVariable) String() string {\n\treturn ufmt.Sprintf(\n\t\t\"expressionVariable: %s\",\n\t\tn.Str,\n\t)\n}\n\nfunc (n expressionNumber) String() string {\n\treturn ufmt.Sprintf(\n\t\t\"expressionNumber: %s\",\n\t\tn.Str,\n\t)\n}\n\nfunc (b expressionOperation) String() string {\n\treturn ufmt.Sprintf(\n\t\t\"expressionOperation: (%s %s %s)\",\n\t\tb.Op,\n\t\tb.Lhs.String(),\n\t\tb.Rhs.String(),\n\t)\n}\n\nfunc newAST(toks []*expressionRaw, s string) (*ast, error) {\n\ta := \u0026ast{\n\t\trawexpressions: toks,\n\t\tsource:         s,\n\t}\n\tif a.rawexpressions == nil || len(a.rawexpressions) == 0 {\n\t\treturn a, errors.New(\"empty token\")\n\t} else {\n\t\ta.currentIndex = 0\n\t\ta.currentexpression = a.rawexpressions[0]\n\t}\n\treturn a, nil\n}\n\nfunc (a *ast) parseExpression() (expression, error) {\n\ta.depth++ // called depth\n\tlhs := a.parsePrimary()\n\tr := a.parseBinOpRHS(0, lhs)\n\ta.depth--\n\tif a.depth == 0 \u0026\u0026 a.currentIndex != len(a.rawexpressions) \u0026\u0026 a.err == nil {\n\t\treturn r, ufmt.Errorf(\"bad expression, reaching the end or missing the operator\\n%s\",\n\t\t\texpressionError(a.source, a.currentexpression.Offset))\n\t}\n\treturn r, nil\n}\n\nfunc (a *ast) getNextexpressionRaw() *expressionRaw {\n\ta.currentIndex++\n\tif a.currentIndex \u003c len(a.rawexpressions) {\n\t\ta.currentexpression = a.rawexpressions[a.currentIndex]\n\t\treturn a.currentexpression\n\t}\n\treturn nil\n}\n\nfunc (a *ast) getTokPrecedence() int {\n\tswitch a.currentexpression.expression {\n\tcase \"/\", \"%\", \"*\":\n\t\treturn 100\n\tcase \"\u003c\u003c\", \"\u003e\u003e\":\n\t\treturn 80\n\tcase \"+\", \"-\":\n\t\treturn 75\n\tcase \"\u003c\", \"\u003e\":\n\t\treturn 70\n\tcase \"\u0026\":\n\t\treturn 60\n\tcase \"^\":\n\t\treturn 50\n\tcase \"|\":\n\t\treturn 40\n\t}\n\treturn -1\n}\n\nfunc (a *ast) parseNumber() expressionNumber {\n\tf64, err := strconv.Atoi(a.currentexpression.expression)\n\tif err != nil {\n\t\ta.err = ufmt.Errorf(\"%v\\nwant '(' or '0-9' but get '%s'\\n%s\",\n\t\t\terr.Error(),\n\t\t\ta.currentexpression.expression,\n\t\t\texpressionError(a.source, a.currentexpression.Offset))\n\t\treturn expressionNumber{}\n\t}\n\tn := expressionNumber{\n\t\tVal: f64,\n\t\tStr: a.currentexpression.expression,\n\t}\n\ta.getNextexpressionRaw()\n\treturn n\n}\n\nfunc (a *ast) parseVariable() expressionVariable {\n\tn := expressionVariable{\n\t\tVal: 0,\n\t\tStr: a.currentexpression.expression,\n\t}\n\ta.getNextexpressionRaw()\n\treturn n\n}\n\nfunc (a *ast) parsePrimary() expression {\n\tswitch a.currentexpression.Type {\n\tcase Variable:\n\t\treturn a.parseVariable()\n\tcase Number:\n\t\treturn a.parseNumber()\n\tcase Operator:\n\t\tif a.currentexpression.expression == \"(\" {\n\t\t\tt := a.getNextexpressionRaw()\n\t\t\tif t == nil {\n\t\t\t\ta.err = ufmt.Errorf(\"want '(' or '0-9' but get EOF\\n%s\",\n\t\t\t\t\texpressionError(a.source, a.currentexpression.Offset))\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\te, _ := a.parseExpression()\n\t\t\tif e == nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tif a.currentexpression.expression != \")\" {\n\t\t\t\ta.err = ufmt.Errorf(\"want ')' but get %s\\n%s\",\n\t\t\t\t\ta.currentexpression.expression,\n\t\t\t\t\texpressionError(a.source, a.currentexpression.Offset))\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\ta.getNextexpressionRaw()\n\t\t\treturn e\n\t\t} else if a.currentexpression.expression == \"-\" {\n\t\t\tif a.getNextexpressionRaw() == nil {\n\t\t\t\ta.err = ufmt.Errorf(\"want '0-9' but get '-'\\n%s\",\n\t\t\t\t\texpressionError(a.source, a.currentexpression.Offset))\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tbin := expressionOperation{\n\t\t\t\tOp:  \"-\",\n\t\t\t\tLhs: expressionNumber{},\n\t\t\t\tRhs: a.parsePrimary(),\n\t\t\t}\n\t\t\treturn bin\n\t\t} else {\n\t\t\treturn a.parseNumber()\n\t\t}\n\tdefault:\n\t\treturn nil\n\t}\n}\n\nfunc (a *ast) parseBinOpRHS(execPrec int, lhs expression) expression {\n\tfor {\n\t\ttokPrec := a.getTokPrecedence()\n\t\tif tokPrec \u003c execPrec {\n\t\t\treturn lhs\n\t\t}\n\t\tbinOp := a.currentexpression.expression\n\t\tif a.getNextexpressionRaw() == nil {\n\t\t\ta.err = ufmt.Errorf(\"want '(' or '0-9' but get EOF\\n%s\",\n\t\t\t\texpressionError(a.source, a.currentexpression.Offset))\n\t\t\treturn nil\n\t\t}\n\t\trhs := a.parsePrimary()\n\t\tif rhs == nil {\n\t\t\treturn nil\n\t\t}\n\t\tnextPrec := a.getTokPrecedence()\n\t\tif tokPrec \u003c nextPrec {\n\t\t\trhs = a.parseBinOpRHS(tokPrec+1, rhs)\n\t\t\tif rhs == nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t}\n\t\tlhs = expressionOperation{\n\t\t\tOp:  binOp,\n\t\t\tLhs: lhs,\n\t\t\tRhs: rhs,\n\t\t}\n\t}\n}\n\nfunc lexer(s string) ([]*expressionRaw, error) {\n\tp := \u0026parser{\n\t\tInput: s,\n\t\terr:   nil,\n\t\tch:    s[0],\n\t}\n\ttoks := p.parse()\n\tif p.err != nil {\n\t\treturn nil, p.err\n\t}\n\treturn toks, nil\n}\n\nfunc (p *parser) parse() []*expressionRaw {\n\ttoks := make([]*expressionRaw, 0)\n\tfor {\n\t\ttok := p.nextTok()\n\t\tif tok == nil {\n\t\t\tbreak\n\t\t}\n\t\ttoks = append(toks, tok)\n\t}\n\treturn toks\n}\n\nfunc (p *parser) nextTok() *expressionRaw {\n\tif p.offset \u003e= len(p.Input) || p.err != nil {\n\t\treturn nil\n\t}\n\tvar err error\n\tfor p.isWhitespace(p.ch) \u0026\u0026 err == nil {\n\t\terr = p.nextCh()\n\t}\n\tstart := p.offset\n\tvar tok *expressionRaw\n\tswitch p.ch {\n\tcase\n\t\t'(',\n\t\t')',\n\t\t'+',\n\t\t'-',\n\t\t'*',\n\t\t'/',\n\t\t'^',\n\t\t'\u0026',\n\t\t'|',\n\t\t'%':\n\t\ttok = \u0026expressionRaw{\n\t\t\texpression: string(p.ch),\n\t\t\tType:       Operator,\n\t\t}\n\t\ttok.Offset = start\n\t\terr = p.nextCh()\n\tcase '\u003e', '\u003c':\n\t\ttokS := string(p.ch)\n\t\tbb, be := p.nextChPeek()\n\t\tif be == nil \u0026\u0026 string(bb) == tokS {\n\t\t\ttokS += string(p.ch)\n\t\t}\n\t\ttok = \u0026expressionRaw{\n\t\t\texpression: tokS,\n\t\t\tType:       Operator,\n\t\t}\n\t\ttok.Offset = start\n\t\tif len(tokS) \u003e 1 {\n\t\t\tp.nextCh()\n\t\t}\n\t\terr = p.nextCh()\n\tcase\n\t\t'0',\n\t\t'1',\n\t\t'2',\n\t\t'3',\n\t\t'4',\n\t\t'5',\n\t\t'6',\n\t\t'7',\n\t\t'8',\n\t\t'9':\n\t\tfor p.isDigitNum(p.ch) \u0026\u0026 p.nextCh() == nil {\n\t\t\tif (p.ch == '-' || p.ch == '+') \u0026\u0026 p.Input[p.offset-1] != 'e' {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\ttok = \u0026expressionRaw{\n\t\t\texpression: strings.ReplaceAll(p.Input[start:p.offset], \"_\", \"\"),\n\t\t\tType:       Number,\n\t\t}\n\t\ttok.Offset = start\n\tdefault:\n\t\tif p.isChar(p.ch) {\n\t\t\ttok = \u0026expressionRaw{\n\t\t\t\texpression: string(p.ch),\n\t\t\t\tType:       Variable,\n\t\t\t}\n\t\t\ttok.Offset = start\n\t\t\terr = p.nextCh()\n\t\t} else if p.ch != ' ' {\n\t\t\tp.err = ufmt.Errorf(\"symbol error: unknown '%v', pos [%v:]\\n%s\",\n\t\t\t\tstring(p.ch),\n\t\t\t\tstart,\n\t\t\t\texpressionError(p.Input, start))\n\t\t}\n\t}\n\treturn tok\n}\n\nfunc (p *parser) nextChPeek() (byte, error) {\n\toffset := p.offset + 1\n\tif offset \u003c len(p.Input) {\n\t\treturn p.Input[offset], nil\n\t}\n\treturn byte(0), errors.New(\"no byte\")\n}\n\nfunc (p *parser) nextCh() error {\n\tp.offset++\n\tif p.offset \u003c len(p.Input) {\n\t\tp.ch = p.Input[p.offset]\n\t\treturn nil\n\t}\n\treturn errors.New(\"EOF\")\n}\n\nfunc (p *parser) isWhitespace(c byte) bool {\n\treturn c == ' ' ||\n\t\tc == '\\t' ||\n\t\tc == '\\n' ||\n\t\tc == '\\v' ||\n\t\tc == '\\f' ||\n\t\tc == '\\r'\n}\n\nfunc (p *parser) isDigitNum(c byte) bool {\n\treturn '0' \u003c= c \u0026\u0026 c \u003c= '9' || c == '.' || c == '_' || c == 'e' || c == '-' || c == '+'\n}\n\nfunc (p *parser) isChar(c byte) bool {\n\treturn 'a' \u003c= c \u0026\u0026 c \u003c= 'z' || 'A' \u003c= c \u0026\u0026 c \u003c= 'Z'\n}\n\nfunc (p *parser) isWordChar(c byte) bool {\n\treturn p.isChar(c) || '0' \u003c= c \u0026\u0026 c \u003c= '9'\n}\n"},{"name":"int32_test.gno","body":"package int32\n\nimport \"testing\"\n\nfunc TestOne(t *testing.T) {\n\tttt := []struct {\n\t\texp string\n\t\tres int\n\t}{\n\t\t{\"1\", 1},\n\t\t{\"--1\", 1},\n\t\t{\"1+2\", 3},\n\t\t{\"-1+2\", 1},\n\t\t{\"-(1+2)\", -3},\n\t\t{\"-(1+2)*5\", -15},\n\t\t{\"-(1+2)*5/3\", -5},\n\t\t{\"1+(-(1+2)*5/3)\", -4},\n\t\t{\"3^4\", 3 ^ 4},\n\t\t{\"8%2\", 8 % 2},\n\t\t{\"8%3\", 8 % 3},\n\t\t{\"8|3\", 8 | 3},\n\t\t{\"10%2\", 0},\n\t\t{\"(4    + 3)/2-1+11*15\", (4+3)/2 - 1 + 11*15},\n\t\t{\n\t\t\t\"(30099\u003e\u003e10^30099\u003e\u003e11)%5*((30099\u003e\u003e14\u00263^30099\u003e\u003e15\u00261)+1)*30099%99 + ((3 + (30099 \u003e\u003e 14 \u0026 3) - (30099 \u003e\u003e 16 \u0026 1)) / 3 * 30099 % 99 \u0026 64)\",\n\t\t\t(30099\u003e\u003e10^30099\u003e\u003e11)%5*((30099\u003e\u003e14\u00263^30099\u003e\u003e15\u00261)+1)*30099%99 + ((3 + (30099 \u003e\u003e 14 \u0026 3) - (30099 \u003e\u003e 16 \u0026 1)) / 3 * 30099 % 99 \u0026 64),\n\t\t},\n\t\t{\n\t\t\t\"(1023850\u003e\u003e10^1023850\u003e\u003e11)%5*((1023850\u003e\u003e14\u00263^1023850\u003e\u003e15\u00261)+1)*1023850%99 + ((3 + (1023850 \u003e\u003e 14 \u0026 3) - (1023850 \u003e\u003e 16 \u0026 1)) / 3 * 1023850 % 99 \u0026 64)\",\n\t\t\t(1023850\u003e\u003e10^1023850\u003e\u003e11)%5*((1023850\u003e\u003e14\u00263^1023850\u003e\u003e15\u00261)+1)*1023850%99 + ((3 + (1023850 \u003e\u003e 14 \u0026 3) - (1023850 \u003e\u003e 16 \u0026 1)) / 3 * 1023850 % 99 \u0026 64),\n\t\t},\n\t\t{\"((0000+1)*0000)\", 0},\n\t}\n\tfor _, tc := range ttt {\n\t\tt.Run(tc.exp, func(t *testing.T) {\n\t\t\texp, err := Parse(tc.exp)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"%s:\\n%s\", tc.exp, err.Error())\n\t\t\t} else {\n\t\t\t\tres, errEval := Eval(exp, nil)\n\t\t\t\tif errEval != nil {\n\t\t\t\t\tt.Errorf(\"eval error: %s\", errEval.Error())\n\t\t\t\t} else if res != tc.res {\n\t\t\t\t\tt.Errorf(\"%s:\\nexpected %d, got %d\", tc.exp, tc.res, res)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestVariables(t *testing.T) {\n\tfn := func(x, y int) int {\n\t\treturn 1 + ((x*3+1)*(x*2))\u003e\u003ey + 1\n\t}\n\texpr := \"1 + ((x*3+1)*(x*2))\u003e\u003ey + 1\"\n\texp, err := Parse(expr)\n\tif err != nil {\n\t\tt.Errorf(\"could not parse: %s\", err.Error())\n\t}\n\tvariables := make(map[string]int)\n\tfor i := 0; i \u003c 10; i++ {\n\t\tvariables[\"x\"] = i\n\t\tvariables[\"y\"] = 2\n\t\tres, errEval := Eval(exp, variables)\n\t\tif errEval != nil {\n\t\t\tt.Errorf(\"could not evaluate: %s\", err.Error())\n\t\t}\n\t\texpected := fn(variables[\"x\"], variables[\"y\"])\n\t\tif res != expected {\n\t\t\tt.Errorf(\"expected: %d, actual: %d\", expected, res)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"XbU9YAaatQdVY3gNzVIL2bhICTwPVAVXB7zJZVnCgzDgw30mVzwTqwtNSPhlTtLELdA9kdqgZTCf8eS0PGpLDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"memeland","path":"gno.land/p/demo/memeland","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/memeland\"\ngno = \"0.9\"\n"},{"name":"memeland.gno","body":"package memeland\n\nimport (\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\nconst (\n\tDATE_CREATED = \"DATE_CREATED\"\n\tUPVOTES      = \"UPVOTES\"\n)\n\ntype Post struct {\n\tID            string\n\tData          string\n\tAuthor        address\n\tTimestamp     time.Time\n\tUpvoteTracker *avl.Tree // address \u003e struct{}{}\n}\n\ntype Memeland struct {\n\t*ownable.Ownable\n\tPosts       []*Post\n\tMemeCounter seqid.ID\n}\n\nfunc NewMemeland(owner address) *Memeland {\n\treturn \u0026Memeland{\n\t\tOwnable: ownable.NewWithAddress(owner),\n\t\tPosts:   make([]*Post, 0),\n\t}\n}\n\n// PostMeme adds a new post. rlm must be the caller's own captured cur\n// (asserted via rlm.IsCurrent()); the post's Author is set to\n// rlm.Previous().Address(), so an external realm cannot forge a post\n// attributed to a different address.\nfunc (m *Memeland) PostMeme(_ int, rlm realm, data string, timestamp int64) string {\n\tif data == \"\" || timestamp \u003c= 0 {\n\t\tpanic(\"timestamp or data cannot be empty\")\n\t}\n\tif !rlm.IsCurrent() {\n\t\tpanic(ownable.ErrUnauthorized)\n\t}\n\n\tid := m.MemeCounter.Next().String()\n\n\tnewPost := \u0026Post{\n\t\tID:            id,\n\t\tData:          data,\n\t\tAuthor:        rlm.Previous().Address(),\n\t\tTimestamp:     time.Unix(timestamp, 0),\n\t\tUpvoteTracker: avl.NewTree(),\n\t}\n\n\tm.Posts = append(m.Posts, newPost)\n\treturn id\n}\n\n// Upvote records an upvote on a post. rlm must be the caller's own\n// captured cur; the voter is rlm.Previous().Address(). Closes the\n// vote-stuffing forgery: an external realm cannot upvote-as-victim.\nfunc (m *Memeland) Upvote(_ int, rlm realm, id string) string {\n\tif !rlm.IsCurrent() {\n\t\tpanic(ownable.ErrUnauthorized)\n\t}\n\tpost := m.getPost(id)\n\tif post == nil {\n\t\tpanic(\"post with specified ID does not exist\")\n\t}\n\n\tvoterKey := rlm.Previous().Address().String()\n\tif post.UpvoteTracker.Has(voterKey) {\n\t\tpanic(\"user has already upvoted this post\")\n\t}\n\tpost.UpvoteTracker.Set(voterKey, struct{}{})\n\n\treturn \"upvote successful\"\n}\n\n// GetPostsInRange returns a JSON string of posts within the given timestamp range, supporting pagination\nfunc (m *Memeland) GetPostsInRange(startTimestamp, endTimestamp int64, page, pageSize int, sortBy string) string {\n\tif len(m.Posts) == 0 {\n\t\treturn \"[]\"\n\t}\n\n\tif page \u003c 1 {\n\t\tpanic(\"page number cannot be less than 1\")\n\t}\n\n\t// No empty pages\n\tif pageSize \u003c 1 {\n\t\tpanic(\"page size cannot be less than 1\")\n\t}\n\n\t// No pages larger than 10\n\tif pageSize \u003e 10 {\n\t\tpanic(\"page size cannot be larger than 10\")\n\t}\n\n\t// Need to pass in a sort parameter\n\tif sortBy == \"\" {\n\t\tpanic(\"sort order cannot be empty\")\n\t}\n\n\tvar filteredPosts []*Post\n\n\tstart := time.Unix(startTimestamp, 0)\n\tend := time.Unix(endTimestamp, 0)\n\n\t// Filtering posts\n\tfor _, p := range m.Posts {\n\t\tif !p.Timestamp.Before(start) \u0026\u0026 !p.Timestamp.After(end) {\n\t\t\tfilteredPosts = append(filteredPosts, p)\n\t\t}\n\t}\n\n\tswitch sortBy {\n\t// Sort by upvote descending\n\tcase UPVOTES:\n\t\tdateSorter := PostSorter{\n\t\t\tPosts: filteredPosts,\n\t\t\tLessF: func(i, j int) bool {\n\t\t\t\treturn filteredPosts[i].UpvoteTracker.Size() \u003e filteredPosts[j].UpvoteTracker.Size()\n\t\t\t},\n\t\t}\n\t\tsort.Sort(dateSorter)\n\tcase DATE_CREATED:\n\t\t// Sort by timestamp, beginning with newest\n\t\tdateSorter := PostSorter{\n\t\t\tPosts: filteredPosts,\n\t\t\tLessF: func(i, j int) bool {\n\t\t\t\treturn filteredPosts[i].Timestamp.After(filteredPosts[j].Timestamp)\n\t\t\t},\n\t\t}\n\t\tsort.Sort(dateSorter)\n\tdefault:\n\t\tpanic(\"sort order can only be \\\"UPVOTES\\\" or \\\"DATE_CREATED\\\"\")\n\t}\n\n\t// Pagination\n\tstartIndex := (page - 1) * pageSize\n\tendIndex := startIndex + pageSize\n\n\t// If page does not contain any posts\n\tif startIndex \u003e= len(filteredPosts) {\n\t\treturn \"[]\"\n\t}\n\n\t// If page contains fewer posts than the page size\n\tif endIndex \u003e len(filteredPosts) {\n\t\tendIndex = len(filteredPosts)\n\t}\n\n\t// Return JSON representation of paginated and sorted posts\n\treturn PostsToJSONString(filteredPosts[startIndex:endIndex])\n}\n\n// RemovePost allows the owner to remove a post with a specific ID. rlm\n// must be the caller's own captured cur; rlm.Previous().Address() must\n// equal the owner.\nfunc (m *Memeland) RemovePost(_ int, rlm realm, id string) string {\n\tif id == \"\" {\n\t\tpanic(\"id cannot be empty\")\n\t}\n\n\tif !rlm.IsCurrent() {\n\t\tpanic(ownable.ErrUnauthorized)\n\t}\n\tif !m.OwnedBy(rlm.Previous().Address()) {\n\t\tpanic(ownable.ErrUnauthorized)\n\t}\n\n\tfor i, post := range m.Posts {\n\t\tif post.ID == id {\n\t\t\tm.Posts = append(m.Posts[:i], m.Posts[i+1:]...)\n\t\t\treturn id\n\t\t}\n\t}\n\n\tpanic(\"post with specified id does not exist\")\n}\n\n// PostsToJSONString converts a slice of Post structs into a JSON string\nfunc PostsToJSONString(posts []*Post) string {\n\tvar sb strings.Builder\n\tsb.WriteString(\"[\")\n\n\tfor i, post := range posts {\n\t\tif i \u003e 0 {\n\t\t\tsb.WriteString(\",\")\n\t\t}\n\n\t\tsb.WriteString(PostToJSONString(post))\n\t}\n\tsb.WriteString(\"]\")\n\n\treturn sb.String()\n}\n\n// PostToJSONString returns a Post formatted as a JSON string\nfunc PostToJSONString(post *Post) string {\n\tvar sb strings.Builder\n\n\tsb.WriteString(\"{\")\n\tsb.WriteString(`\"id\":\"` + post.ID + `\",`)\n\tsb.WriteString(`\"data\":\"` + escapeString(post.Data) + `\",`)\n\tsb.WriteString(`\"author\":\"` + escapeString(post.Author.String()) + `\",`)\n\tsb.WriteString(`\"timestamp\":\"` + strconv.Itoa(int(post.Timestamp.Unix())) + `\",`)\n\tsb.WriteString(`\"upvotes\":` + strconv.Itoa(post.UpvoteTracker.Size()))\n\tsb.WriteString(\"}\")\n\n\treturn sb.String()\n}\n\n// escapeString escapes quotes in a string for JSON compatibility.\nfunc escapeString(s string) string {\n\treturn strings.ReplaceAll(s, `\"`, `\\\"`)\n}\n\nfunc (m *Memeland) getPost(id string) *Post {\n\tfor _, p := range m.Posts {\n\t\tif p.ID == id {\n\t\t\treturn p\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// PostSorter is a flexible sorter for the *Post slice\ntype PostSorter struct {\n\tPosts []*Post\n\tLessF func(i, j int) bool\n}\n\nfunc (p PostSorter) Len() int {\n\treturn len(p.Posts)\n}\n\nfunc (p PostSorter) Swap(i, j int) {\n\tp.Posts[i], p.Posts[j] = p.Posts[j], p.Posts[i]\n}\n\nfunc (p PostSorter) Less(i, j int) bool {\n\treturn p.LessF(i, j)\n}\n"},{"name":"memeland_test.gno","body":"package memeland\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// postMemeAs / upvoteAs / removePostAs are tiny helpers that thread the\n// migrated (_ int, rlm realm) API: each sets the test realm to the\n// actor's user realm and invokes the call inside a crossing closure so\n// cur.Previous().Address() resolves to actor.\nfunc postMemeAs(cur realm, actor address, m *Memeland, data string, timestamp int64) string {\n\ttesting.SetRealm(testing.NewUserRealm(actor))\n\tvar id string\n\tfunc(cur realm) { id = m.PostMeme(0, cur, data, timestamp) }(cross(cur))\n\treturn id\n}\n\nfunc upvoteAs(cur realm, actor address, m *Memeland, id string) string {\n\ttesting.SetRealm(testing.NewUserRealm(actor))\n\tvar result string\n\tfunc(cur realm) { result = m.Upvote(0, cur, id) }(cross(cur))\n\treturn result\n}\n\nfunc removePostAs(cur realm, actor address, m *Memeland, id string) string {\n\ttesting.SetRealm(testing.NewUserRealm(actor))\n\tvar result string\n\tfunc(cur realm) { result = m.RemovePost(0, cur, id) }(cross(cur))\n\treturn result\n}\n\nfunc TestPostMeme(cur realm, t *testing.T) {\n\tadmin := testutils.TestAddress(\"admin\")\n\tm := NewMemeland(admin)\n\tid := postMemeAs(cur, admin, m, \"Test meme data\", time.Now().Unix())\n\tuassert.NotEqual(t, \"\", string(id), \"Expected valid ID, got empty string\")\n}\n\nfunc TestGetPostsInRangePagination(cur realm, t *testing.T) {\n\tadmin := testutils.TestAddress(\"admin\")\n\tm := NewMemeland(admin)\n\tnow := time.Now()\n\n\tnumOfPosts := 5\n\tvar memeData []string\n\tfor i := 1; i \u003c= numOfPosts; i++ {\n\t\t// Prepare meme data\n\t\tnextTime := now.Add(time.Duration(i) * time.Minute)\n\t\tdata := ufmt.Sprintf(\"Meme #%d\", i)\n\t\tmemeData = append(memeData, data)\n\n\t\tpostMemeAs(cur, admin, m, data, nextTime.Unix())\n\t}\n\n\t// Get timestamps\n\tbeforeEarliest := now.Add(-1 * time.Minute)\n\tafterLatest := now.Add(time.Duration(numOfPosts)*time.Minute + time.Minute)\n\n\ttestCases := []struct {\n\t\tpage               int\n\t\tpageSize           int\n\t\texpectedNumOfPosts int\n\t}{\n\t\t{page: 1, pageSize: 1, expectedNumOfPosts: 1},                       // one per page\n\t\t{page: 2, pageSize: 1, expectedNumOfPosts: 1},                       // one on second page\n\t\t{page: 1, pageSize: numOfPosts, expectedNumOfPosts: numOfPosts},     // all posts on single page\n\t\t{page: 12, pageSize: 1, expectedNumOfPosts: 0},                      // empty page\n\t\t{page: 1, pageSize: numOfPosts + 1, expectedNumOfPosts: numOfPosts}, // page with fewer posts than its size\n\t\t{page: 5, pageSize: numOfPosts / 5, expectedNumOfPosts: 1},          // evenly distribute posts per page\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(ufmt.Sprintf(\"Page%d_Size%d\", tc.page, tc.pageSize), func(t *testing.T) {\n\t\t\tresult := m.GetPostsInRange(beforeEarliest.Unix(), afterLatest.Unix(), tc.page, tc.pageSize, \"DATE_CREATED\")\n\n\t\t\t// Count posts by how many times id: shows up in JSON string\n\t\t\tpostCount := strings.Count(result, `\"id\":\"`)\n\t\t\tuassert.Equal(t, tc.expectedNumOfPosts, postCount)\n\t\t})\n\t}\n}\n\nfunc TestGetPostsInRangeByTimestamp(cur realm, t *testing.T) {\n\tadmin := testutils.TestAddress(\"admin\")\n\tm := NewMemeland(admin)\n\tnow := time.Now()\n\n\tnumOfPosts := 5\n\tvar memeData []string\n\tfor i := 1; i \u003c= numOfPosts; i++ {\n\t\t// Prepare meme data\n\t\tnextTime := now.Add(time.Duration(i) * time.Minute)\n\t\tdata := ufmt.Sprintf(\"Meme #%d\", i)\n\t\tmemeData = append(memeData, data)\n\n\t\tpostMemeAs(cur, admin, m, data, nextTime.Unix())\n\t}\n\n\t// Get timestamps\n\tbeforeEarliest := now.Add(-1 * time.Minute)\n\tafterLatest := now.Add(time.Duration(numOfPosts)*time.Minute + time.Minute)\n\n\t// Default sort is by addition order/timestamp\n\tjsonStr := m.GetPostsInRange(\n\t\tbeforeEarliest.Unix(), // start at earliest post\n\t\tafterLatest.Unix(),    // end at latest post\n\t\t1,                     // first page\n\t\tnumOfPosts,            // all memes on the page\n\t\t\"DATE_CREATED\",        // sort by newest first\n\t)\n\n\tuassert.NotEmpty(t, jsonStr, \"Expected non-empty JSON string, got empty string\")\n\n\t// Count the number of posts returned in the JSON string as a rudimentary check for correct pagination/filtering\n\tpostCount := strings.Count(jsonStr, `\"id\":\"`)\n\tuassert.Equal(t, uint64(m.MemeCounter), uint64(postCount))\n\n\t// Check if data is there\n\tfor _, expData := range memeData {\n\t\tcheck := strings.Contains(jsonStr, expData)\n\t\tuassert.True(t, check, ufmt.Sprintf(\"Expected %s in the JSON string, but counld't find it\", expData))\n\t}\n\n\t// Check if ordering is correct, sort by created date\n\tfor i := 0; i \u003c len(memeData)-2; i++ {\n\t\tcheck := strings.Index(jsonStr, memeData[i]) \u003e= strings.Index(jsonStr, memeData[i+1])\n\t\tuassert.True(t, check, ufmt.Sprintf(\"Expected %s to be before %s, but was at %d, and %d\", memeData[i], memeData[i+1], i, i+1))\n\t}\n}\n\nfunc TestGetPostsInRangeByUpvote(cur realm, t *testing.T) {\n\tadmin := testutils.TestAddress(\"admin\")\n\talice := testutils.TestAddress(\"alice\")\n\tm := NewMemeland(admin)\n\tnow := time.Now()\n\n\tmemeData1 := \"Meme #1\"\n\tmemeData2 := \"Meme #2\"\n\n\t// Create posts at specific times for testing\n\tid1 := postMemeAs(cur, admin, m, memeData1, now.Unix())\n\tid2 := postMemeAs(cur, admin, m, memeData2, now.Add(time.Minute).Unix())\n\n\tupvoteAs(cur, admin, m, id1)\n\tupvoteAs(cur, admin, m, id2)\n\tupvoteAs(cur, alice, m, id1)\n\n\t// Final upvote count:\n\t// Meme #1 - 2 upvote\n\t// Meme #2 - 1 upvotes\n\n\t// Get timestamps\n\tbeforeEarliest := now.Add(-time.Minute)\n\tafterLatest := now.Add(time.Hour)\n\n\t// Default sort is by addition order/timestamp\n\tjsonStr := m.GetPostsInRange(\n\t\tbeforeEarliest.Unix(), // start at earliest post\n\t\tafterLatest.Unix(),    // end at latest post\n\t\t1,                     // first page\n\t\t2,                     // all memes on the page\n\t\t\"UPVOTES\",             // sort by upvote\n\t)\n\n\tuassert.NotEmpty(t, jsonStr, \"Expected non-empty JSON string, got empty string\")\n\n\t// Count the number of posts returned in the JSON string as a rudimentary check for correct pagination/filtering\n\tpostCount := strings.Count(jsonStr, `\"id\":\"`)\n\tuassert.Equal(t, uint64(m.MemeCounter), uint64(postCount))\n\n\t// Check if ordering is correct\n\tcheck := strings.Index(jsonStr, \"Meme #1\") \u003c= strings.Index(jsonStr, \"Meme #2\")\n\tuassert.True(t, check, ufmt.Sprintf(\"Expected %s to be before %s\", memeData1, memeData2))\n}\n\nfunc TestBadSortBy(cur realm, t *testing.T) {\n\tadmin := testutils.TestAddress(\"admin\")\n\tm := NewMemeland(admin)\n\tnow := time.Now()\n\n\tnumOfPosts := 5\n\tvar memeData []string\n\tfor i := 1; i \u003c= numOfPosts; i++ {\n\t\t// Prepare meme data\n\t\tnextTime := now.Add(time.Duration(i) * time.Minute)\n\t\tdata := ufmt.Sprintf(\"Meme #%d\", i)\n\t\tmemeData = append(memeData, data)\n\n\t\tpostMemeAs(cur, admin, m, data, nextTime.Unix())\n\t}\n\n\t// Get timestamps\n\tbeforeEarliest := now.Add(-1 * time.Minute)\n\tafterLatest := now.Add(time.Duration(numOfPosts)*time.Minute + time.Minute)\n\n\ttests := []struct {\n\t\tname      string\n\t\tsortBy    string\n\t\twantPanic string\n\t}{\n\t\t{\n\t\t\tname:      \"Empty sortBy\",\n\t\t\tsortBy:    \"\",\n\t\t\twantPanic: \"runtime error: index out of range\",\n\t\t},\n\t\t{\n\t\t\tname:      \"Wrong sortBy\",\n\t\t\tsortBy:    \"random string\",\n\t\t\twantPanic: \"\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r == nil {\n\t\t\t\t\tt.Errorf(\"code did not panic when it should have\")\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\t// Panics should be caught\n\t\t\t_ = m.GetPostsInRange(beforeEarliest.Unix(), afterLatest.Unix(), 1, 1, tc.sortBy)\n\t\t})\n\t}\n}\n\nfunc TestNoPosts(cur realm, t *testing.T) {\n\tm := NewMemeland(testutils.TestAddress(\"admin\"))\n\n\t// Add a post to Memeland\n\tnow := time.Now().Unix()\n\n\tjsonStr := m.GetPostsInRange(0, now, 1, 1, \"DATE_CREATED\")\n\n\tuassert.Equal(t, jsonStr, \"[]\")\n}\n\nfunc TestUpvote(cur realm, t *testing.T) {\n\tadmin := testutils.TestAddress(\"admin\")\n\tm := NewMemeland(admin)\n\n\t// Add a post to Memeland\n\tnow := time.Now().Unix()\n\tpostID := postMemeAs(cur, admin, m, \"Test meme data\", now)\n\n\t// Initial upvote count should be 0\n\tpost := m.getPost(postID)\n\tuassert.Equal(t, 0, post.UpvoteTracker.Size())\n\n\t// Upvote the post\n\tupvoteResult := upvoteAs(cur, admin, m, postID)\n\tuassert.Equal(t, \"upvote successful\", upvoteResult)\n\n\t// Retrieve the post again and check the upvote count\n\tpost = m.getPost(postID)\n\tuassert.Equal(t, 1, post.UpvoteTracker.Size())\n}\n\nfunc TestDelete(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tbob := testutils.TestAddress(\"bob\")\n\n\t// Alice is admin\n\tm := NewMemeland(alice)\n\n\t// Bob adds post to Memeland\n\tnow := time.Now()\n\tpostID := postMemeAs(cur, bob, m, \"Meme #1\", now.Unix())\n\n\t// Alice removes Bob's post\n\tid := removePostAs(cur, alice, m, postID)\n\tuassert.Equal(t, postID, id, \"post IDs not matching\")\n\tuassert.Equal(t, 0, len(m.Posts), \"there should be 0 posts after removing\")\n}\n\nfunc TestDeleteByNonAdmin(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\tm := NewMemeland(alice)\n\n\t// Add a post to Memeland\n\tnow := time.Now()\n\tpostID := postMemeAs(cur, alice, m, \"Meme #1\", now.Unix())\n\n\t// Bob will try to delete meme posted by Alice, which should fail.\n\t// removePostAs wraps the call in cross(cur), so the panic crosses\n\t// a realm boundary and must be caught with revive(); a plain\n\t// defer-recover wouldn't see it.\n\tbob := testutils.TestAddress(\"bob\")\n\tr := revive(func() {\n\t\tremovePostAs(cur, bob, m, postID)\n\t})\n\tif r == nil {\n\t\tt.Errorf(\"code did not panic when it should have\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"jICYidgbupf9ReIa4H4rGAi9EdFZmHH0zeq9G8S/03ThXKW7/WqtzobjqeCdHMd+A7EhyP5ipJXv3rhRUdYABg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"merkle","path":"gno.land/p/demo/merkle","files":[{"name":"README.md","body":"# p/demo/merkle\n\nThis package implement a merkle tree that is complient with [merkletreejs](https://github.com/merkletreejs/merkletreejs)\n\n## [merkletreejs](https://github.com/merkletreejs/merkletreejs)\n\n```javascript\nconst { MerkleTree } = require(\"merkletreejs\");\nconst SHA256 = require(\"crypto-js/sha256\");\n\nlet leaves = [];\nfor (let i = 0; i \u003c 10; i++) {\n  leaves.push(SHA256(`node_${i}`));\n}\n\nconst tree = new MerkleTree(leaves, SHA256);\nconst root = tree.getRoot().toString(\"hex\");\n\nconsole.log(root); // cd8a40502b0b92bf58e7432a5abb2d8b60121cf2b7966d6ebaf103f907a1bc21\n```\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/merkle\"\ngno = \"0.9\"\n"},{"name":"merkle.gno","body":"package merkle\n\nimport (\n\t\"bytes\"\n\t\"crypto/sha256\"\n\t\"encoding/hex\"\n\t\"errors\"\n)\n\ntype Hashable interface {\n\tBytes() []byte\n}\n\ntype nodes []Node\n\ntype Node struct {\n\thash []byte\n\n\tposition uint8\n}\n\nfunc NewNode(hash []byte, position uint8) Node {\n\treturn Node{\n\t\thash:     hash,\n\t\tposition: position,\n\t}\n}\n\nfunc (n Node) Position() uint8 {\n\treturn n.position\n}\n\nfunc (n Node) Hash() string {\n\treturn hex.EncodeToString(n.hash[:])\n}\n\ntype Tree struct {\n\tlayers []nodes\n}\n\n// Root return the merkle root of the tree\nfunc (t *Tree) Root() string {\n\tfor _, l := range t.layers {\n\t\tif len(l) == 1 {\n\t\t\treturn l[0].Hash()\n\t\t}\n\t}\n\treturn \"\"\n}\n\n// NewTree create a new Merkle Tree\nfunc NewTree(data []Hashable) *Tree {\n\ttree := \u0026Tree{}\n\n\tleaves := make([]Node, len(data))\n\n\tfor i, d := range data {\n\t\thash := sha256.Sum256(d.Bytes())\n\t\tleaves[i] = Node{hash: hash[:]}\n\t}\n\n\ttree.layers = []nodes{nodes(leaves)}\n\n\tvar buff bytes.Buffer\n\tfor len(leaves) \u003e 1 {\n\t\tlevel := make([]Node, 0, len(leaves)/2+1)\n\t\tfor i := 0; i \u003c len(leaves); i += 2 {\n\t\t\tbuff.Reset()\n\n\t\t\tif i \u003c len(leaves)-1 {\n\t\t\t\tbuff.Write(leaves[i].hash)\n\t\t\t\tbuff.Write(leaves[i+1].hash)\n\t\t\t\thash := sha256.Sum256(buff.Bytes())\n\t\t\t\tlevel = append(level, Node{\n\t\t\t\t\thash: hash[:],\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tlevel = append(level, leaves[i])\n\t\t\t}\n\t\t}\n\t\tleaves = level\n\t\ttree.layers = append(tree.layers, level)\n\t}\n\treturn tree\n}\n\n// Proof return a MerkleProof\nfunc (t *Tree) Proof(data Hashable) ([]Node, error) {\n\ttargetHash := sha256.Sum256(data.Bytes())\n\ttargetIndex := -1\n\n\tfor i, layer := range t.layers[0] {\n\t\tif bytes.Equal(targetHash[:], layer.hash) {\n\t\t\ttargetIndex = i\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif targetIndex == -1 {\n\t\treturn nil, errors.New(\"target not found\")\n\t}\n\n\tproofs := make([]Node, 0, len(t.layers))\n\n\tfor _, layer := range t.layers {\n\t\tvar pairIndex int\n\n\t\tif targetIndex%2 == 0 {\n\t\t\tpairIndex = targetIndex + 1\n\t\t} else {\n\t\t\tpairIndex = targetIndex - 1\n\t\t}\n\t\tif pairIndex \u003c len(layer) {\n\t\t\tproofs = append(proofs, Node{\n\t\t\t\thash:     layer[pairIndex].hash,\n\t\t\t\tposition: uint8(targetIndex) % 2,\n\t\t\t})\n\t\t}\n\t\ttargetIndex /= 2\n\t}\n\treturn proofs, nil\n}\n\n// Verify if a merkle proof is valid\nfunc (t *Tree) Verify(leaf Hashable, proofs []Node) bool {\n\treturn Verify(t.Root(), leaf, proofs)\n}\n\n// Verify if a merkle proof is valid\nfunc Verify(root string, leaf Hashable, proofs []Node) bool {\n\thash := sha256.Sum256(leaf.Bytes())\n\n\tfor i := 0; i \u003c len(proofs); i += 1 {\n\t\tvar h []byte\n\t\tif proofs[i].position == 0 {\n\t\t\th = append(hash[:], proofs[i].hash...)\n\t\t} else {\n\t\t\th = append(proofs[i].hash, hash[:]...)\n\t\t}\n\t\thash = sha256.Sum256(h)\n\t}\n\treturn hex.EncodeToString(hash[:]) == root\n}\n"},{"name":"merkle_test.gno","body":"package merkle\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n)\n\ntype testData struct {\n\tcontent string\n}\n\nfunc (d testData) Bytes() []byte {\n\treturn []byte(d.content)\n}\n\nfunc TestMerkleTree(t *testing.T) {\n\ttests := []struct {\n\t\tsize     int\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tsize:     1,\n\t\t\texpected: \"cf9f824bce7f5bc63d557b23591f58577f53fe29f974a615bdddbd0140f912f4\",\n\t\t},\n\t\t{\n\t\t\tsize:     3,\n\t\t\texpected: \"1a4a5f0fa267244bf9f74a63fdf2a87eed5e97e4bd104a9e94728c8fb5442177\",\n\t\t},\n\t\t{\n\t\t\tsize:     10,\n\t\t\texpected: \"cd8a40502b0b92bf58e7432a5abb2d8b60121cf2b7966d6ebaf103f907a1bc21\",\n\t\t},\n\t\t{\n\t\t\tsize:     1000,\n\t\t\texpected: \"fa533d2efdf12be26bc410dfa42936ac63361324e35e9b1ff54d422a1dd2388b\",\n\t\t},\n\t}\n\n\tfor _, test := range tests {\n\t\tvar leaves []Hashable\n\t\tfor i := 0; i \u003c test.size; i++ {\n\t\t\tleaves = append(leaves, testData{fmt.Sprintf(\"node_%d\", i)})\n\t\t}\n\n\t\ttree := NewTree(leaves)\n\n\t\tif tree == nil {\n\t\t\tt.Error(\"Merkle tree creation failed\")\n\t\t}\n\n\t\troot := tree.Root()\n\n\t\tif root != test.expected {\n\t\t\tt.Fatalf(\"merkle.Tree.Root(), expected: %s; got: %s\", test.expected, root)\n\t\t}\n\n\t\tfor _, leaf := range leaves {\n\t\t\tproofs, err := tree.Proof(leaf)\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(\"failed to proof leaf: %v, on tree: %v\", leaf, test)\n\t\t\t}\n\n\t\t\tok := Verify(root, leaf, proofs)\n\t\t\tif !ok {\n\t\t\t\tt.Fatal(\"failed to verify leaf: %v, on tree: %v\", leaf, tree)\n\t\t\t}\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"UiCROLwMCq10dUQDockhXEXupD5s+fELhs/9JPpDd39BpbkZskcKxaxn+7QCBQX1E8ekcbTNehRv0m/OyyJcBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"microblog","path":"gno.land/p/demo/microblog","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/microblog\"\ngno = \"0.9\"\n"},{"name":"microblog.gno","body":"package microblog\n\nimport (\n\t\"errors\"\n\t\"sort\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tErrNotFound    = errors.New(\"not found\")\n\tStatusNotFound = \"404\"\n)\n\ntype Microblog struct {\n\tTitle  string\n\tPrefix string   // i.e. r/gnoland/blog:\n\tPages  avl.Tree // author (string) -\u003e Page\n}\n\nfunc NewMicroblog(title string, prefix string) (m *Microblog) {\n\treturn \u0026Microblog{\n\t\tTitle:  title,\n\t\tPrefix: prefix,\n\t\tPages:  avl.Tree{},\n\t}\n}\n\nfunc (m *Microblog) GetPages() []*Page {\n\tvar (\n\t\tpages = make([]*Page, m.Pages.Size())\n\t\tindex = 0\n\t)\n\n\tm.Pages.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tpages[index] = value.(*Page)\n\t\tindex++\n\t\treturn false\n\t})\n\n\tsort.Sort(byLastPosted(pages))\n\n\treturn pages\n}\n\n// NewPost records a new post. rlm must be the caller's own captured\n// cur (asserted via rlm.IsCurrent()); the post's author is set to\n// rlm.Previous().Address() — an external realm cannot forge a post\n// under a different identity.\nfunc (m *Microblog) NewPost(_ int, rlm realm, text string) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"microblog: unauthorized\")\n\t}\n\tauthor := rlm.Previous().Address()\n\tif m.Pages.Get(author.String()) == nil {\n\t\t// make a new page for the new author\n\t\tm.Pages.Set(author.String(), \u0026Page{\n\t\t\tAuthor:    author,\n\t\t\tCreatedAt: time.Now(),\n\t\t})\n\t}\n\n\tpage, err := m.GetPage(author.String())\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn page.NewPost(text)\n}\n\nfunc (m *Microblog) GetPage(author string) (*Page, error) {\n\tsilo := m.Pages.Get(author)\n\tif silo == nil {\n\t\treturn nil, ErrNotFound\n\t}\n\treturn silo.(*Page), nil\n}\n\ntype Page struct {\n\tID         int\n\tAuthor     address\n\tCreatedAt  time.Time\n\tLastPosted time.Time\n\tPosts      avl.Tree // time -\u003e Post\n}\n\n// byLastPosted implements sort.Interface for []Page based on\n// the LastPosted field.\ntype byLastPosted []*Page\n\nfunc (a byLastPosted) Len() int           { return len(a) }\nfunc (a byLastPosted) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }\nfunc (a byLastPosted) Less(i, j int) bool { return a[i].LastPosted.After(a[j].LastPosted) }\n\nfunc (p *Page) NewPost(text string) error {\n\tnow := time.Now()\n\tp.LastPosted = now\n\tp.Posts.Set(ufmt.Sprintf(\"%s%d\", now.Format(time.RFC3339), p.Posts.Size()), \u0026Post{\n\t\tID:        p.Posts.Size(),\n\t\tText:      text,\n\t\tCreatedAt: now,\n\t})\n\treturn nil\n}\n\nfunc (p *Page) GetPosts() []*Post {\n\tposts := make([]*Post, p.Posts.Size())\n\ti := 0\n\tp.Posts.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\tpostParsed := value.(*Post)\n\t\tposts[i] = postParsed\n\t\ti++\n\t\treturn false\n\t})\n\treturn posts\n}\n\n// Post lists the specific update\ntype Post struct {\n\tID        int\n\tCreatedAt time.Time\n\tText      string\n}\n\nfunc (p *Post) String() string {\n\treturn \"\u003e \" + strings.ReplaceAll(p.Text, \"\\n\", \"\\n\u003e\\n\u003e\") + \"\\n\u003e\\n\u003e *\" + p.CreatedAt.Format(time.RFC1123) + \"*\"\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"NK29+EyKOVAgog3UCn7UV5awHHsicHmZFAGh8WbvcbbZU46af/3LGCnBApz/9lUCrF0vmsOdTmVTYPIkmFbDCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"releases","path":"gno.land/p/demo/releases","files":[{"name":"changelog.gno","body":"package releases\n\nimport (\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype changelog struct {\n\tname     string\n\treleases []release\n}\n\nfunc NewChangelog(name string) *changelog {\n\treturn \u0026changelog{\n\t\tname:     name,\n\t\treleases: make([]release, 0),\n\t}\n}\n\nfunc (c *changelog) NewRelease(version, url, notes string) {\n\tif latest := c.Latest(); latest != nil {\n\t\tlatest.isLatest = false\n\t}\n\n\trelease := release{\n\t\t// manual\n\t\tversion: version,\n\t\turl:     url,\n\t\tnotes:   notes,\n\n\t\t// internal\n\t\tchangelog: c,\n\t\tisLatest:  true,\n\t}\n\n\tc.releases = append(c.releases, release)\n}\n\nfunc (c changelog) RenderAsTable(max int) string {\n\tout := md.H1(c.name)\n\n\tif len(c.releases) == 0 {\n\t\tout += \"No releases.\"\n\t\treturn out\n\t}\n\n\tout += ufmt.Sprintf(\"See the %s changelog below.\\n\\n\", c.name)\n\ttable := mdtable.Table{\n\t\tHeaders: []string{\"Version\", \"Link\", \"Notes\"},\n\t}\n\n\tif max \u003e len(c.releases) {\n\t\tmax = len(c.releases)\n\t}\n\n\tfor i := len(c.releases) - 1; i \u003e= len(c.releases)-max; i-- {\n\t\tr := c.releases[i]\n\t\ttable.Append([]string{r.version, r.Link(), r.notes})\n\t}\n\n\tout += table.String()\n\treturn out\n}\n\nfunc (c *changelog) Render(path string) string {\n\tif path == \"\" {\n\t\toutput := \"# \" + c.name + \"\\n\\n\"\n\t\tmax := len(c.releases) - 1\n\t\tmin := 0\n\t\tif max-min \u003e 10 {\n\t\t\tmin = max - 10\n\t\t}\n\t\tfor i := max; i \u003e= min; i-- {\n\t\t\trelease := c.releases[i]\n\t\t\toutput += release.Render()\n\t\t}\n\t\treturn output\n\t}\n\n\trelease := c.ByVersion(path)\n\tif release != nil {\n\t\treturn release.Render()\n\t}\n\n\treturn \"no such release\"\n}\n\nfunc (c *changelog) Latest() *release {\n\tif len(c.releases) \u003e 0 {\n\t\tpos := len(c.releases) - 1\n\t\treturn \u0026c.releases[pos]\n\t}\n\treturn nil\n}\n\nfunc (c *changelog) ByVersion(version string) *release {\n\tfor _, release := range c.releases {\n\t\tif release.version == version {\n\t\t\treturn \u0026release\n\t\t}\n\t}\n\treturn nil\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/releases\"\ngno = \"0.9\"\n"},{"name":"release.gno","body":"package releases\n\ntype release struct {\n\t// manual\n\tversion string\n\turl     string\n\tnotes   string\n\n\t// internal\n\tisLatest  bool\n\tchangelog *changelog\n}\n\nfunc (r *release) URL() string     { return r.url }\nfunc (r *release) Version() string { return r.version }\nfunc (r *release) Notes() string   { return r.notes }\nfunc (r *release) IsLatest() bool  { return r.isLatest }\n\nfunc (r *release) Title() string {\n\toutput := r.changelog.name + \" \" + r.version\n\tif r.isLatest {\n\t\toutput += \" (latest)\"\n\t}\n\treturn output\n}\n\nfunc (r *release) Link() string {\n\treturn \"[\" + r.Title() + \"](\" + r.url + \")\"\n}\n\nfunc (r *release) Render() string {\n\toutput := \"\"\n\toutput += \"## \" + r.Link() + \"\\n\\n\"\n\tif r.notes != \"\" {\n\t\toutput += r.notes + \"\\n\\n\"\n\t}\n\treturn output\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"zkutMdAhJpD4P1tYnm0dalhKx7592Z9EqoRuPYKicRU5PIJOenpsHP1L8SU4fUTYfvEM5VVsURd/XIPAmMxqBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"stack","path":"gno.land/p/demo/stack","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/stack\"\ngno = \"0.9\"\n"},{"name":"stack.gno","body":"package stack\n\ntype Stack struct {\n\ttop    *node\n\tlength int\n}\n\ntype node struct {\n\tvalue any\n\tprev  *node\n}\n\nfunc New() *Stack {\n\treturn \u0026Stack{nil, 0}\n}\n\nfunc (s *Stack) Len() int {\n\treturn s.length\n}\n\nfunc (s *Stack) Top() any {\n\tif s.length == 0 {\n\t\treturn nil\n\t}\n\treturn s.top.value\n}\n\nfunc (s *Stack) Pop() any {\n\tif s.length == 0 {\n\t\treturn nil\n\t}\n\n\tnode := s.top\n\ts.top = node.prev\n\ts.length -= 1\n\treturn node.value\n}\n\nfunc (s *Stack) Push(value any) {\n\tnode := \u0026node{value, s.top}\n\ts.top = node\n\ts.length += 1\n}\n"},{"name":"stack_test.gno","body":"package stack\n\nimport \"testing\"\n\nfunc TestStack(t *testing.T) {\n\ts := New() // Empty stack\n\n\tif s.Len() != 0 {\n\t\tt.Errorf(\"s.Len(): expected 0; got %d\", s.Len())\n\t}\n\n\ts.Push(1)\n\n\tif s.Len() != 1 {\n\t\tt.Errorf(\"s.Len(): expected 1; got %d\", s.Len())\n\t}\n\n\tif top := s.Top(); top.(int) != 1 {\n\t\tt.Errorf(\"s.Top(): expected 1; got %v\", top.(int))\n\t}\n\n\tif elem := s.Pop(); elem.(int) != 1 {\n\t\tt.Errorf(\"s.Pop(): expected 1; got %v\", elem.(int))\n\t}\n\tif s.Len() != 0 {\n\t\tt.Errorf(\"s.Len(): expected 0; got %d\", s.Len())\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"JAIxqSJzS+obodEgMhlAZsfBeP1pKOQage9lOui7xziqWjAbfq/2XmzYmS2vcWnzeN3/7NW02cbLT0oWoHjuAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"subscription","path":"gno.land/p/demo/subscription","files":[{"name":"doc.gno","body":"// Package subscription provides recurring and lifetime subscription\n// primitives. Payment is pulled through a caller-supplied\n// *allowancesender.AllowanceSender (a cap-token whose construction is\n// gated on banker.IsCanonical, so the lib can rely on Send actually\n// moving real coins). Subscriber identity is decoupled from cap-token\n// authority; the lib does not inspect call-chain shape.\n//\n// Each subscription instance locks its payment destination at\n// construction time (payeeAddr) and pulls a fixed per-period amount\n// from the cap on each Subscribe call. A leaked struct pointer cannot\n// redirect payment.\n//\n// Recurring Subscription:\n//\n//\t// In the consumer realm, at deployment:\n//\tvar recSub = recurring.NewRecurringSubscription(\n//\t    ownerAddr,                          // address authorized to call UpdateAmount\n//\t    time.Hour*24*30,                    // 30-day periods\n//\t    100,                                // 100 ugnot per period\n//\t    unsafe.CurrentRealm().Address(),   // payee = this realm\n//\t)\n//\n//\t// Wrapper exposed to users. The wrapper builds an AllowanceSender\n//\t// from a real banker, defers Close, and forwards the call. Subscribe\n//\t// pulls 100 ugnot from the consumer realm (whose -send envelope\n//\t// the wrapper validated) and records `subscriber` as active until\n//\t// time.Now() + 30 days.\n//\tfunc Subscribe(_ realm, subscriber address) {\n//\t    if !unsafe.PreviousRealm().IsUserCall() { panic(\"EOA only\") }\n//\t    src := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n//\t    al := allowancesender.New(src, unsafe.CurrentRealm().Address(),\n//\t        chain.NewCoins(chain.NewCoin(\"ugnot\", recSub.GetAmount())))\n//\t    defer al.Close()\n//\t    if err := recSub.Subscribe(al, subscriber); err != nil {\n//\t        panic(err)\n//\t    }\n//\t}\n//\n// Lifetime Subscription:\n//\n//\tvar lifeSub = lifetime.NewLifetimeSubscription(\n//\t    ownerAddr,                          // address authorized to call UpdateAmount\n//\t    500,                                // 500 ugnot one-time\n//\t    unsafe.CurrentRealm().Address(),\n//\t)\n//\n//\t// Same wrapper shape as recurring.\n//\n// Both libraries trust whatever address the consumer's wrapper passes\n// as `subscriber` — the wrapper's policy decides who's allowed to\n// register, who can be gifted, etc. Self-Subscribe and gift collapse\n// into the same call with different `subscriber` arguments.\npackage subscription\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/subscription\"\ngno = \"0.9\"\n"},{"name":"subscription.gno","body":"package subscription\n\nimport (\n\t\"gno.land/p/jaekwon/allowancesender/v0\"\n)\n\n// Subscription is the cross-implementation interface. Payment is\n// pulled from a caller-supplied *AllowanceSender; subscriber identity\n// is decoupled from cap-token authority.\ntype Subscription interface {\n\tHasValidSubscription(address) error\n\tSubscribe(al *allowancesender.AllowanceSender, subscriber address) error\n\tUpdateAmount(newAmount int64) error\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Dr3gMwfIFuyktV39vIjiiDnNmtu2iNf1DW3gU/DjFISMeGc4PgyGLaK1Rxhu2SJtYvThPi0FNpG3M7/em1H7Bg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"lifetime","path":"gno.land/p/demo/subscription/lifetime","files":[{"name":"errors.gno","body":"package lifetime\n\nimport \"errors\"\n\nvar (\n\tErrNoSub             = errors.New(\"lifetime subscription: no active subscription found\")\n\tErrAlreadySub        = errors.New(\"lifetime subscription: this address already has an active lifetime subscription\")\n\tErrNotAuthorized     = errors.New(\"lifetime subscription: action not authorized\")\n\tErrInvalidPayee      = errors.New(\"lifetime subscription: payee address is invalid\")\n\tErrInvalidSubscriber = errors.New(\"lifetime subscription: subscriber address is invalid\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/subscription/lifetime\"\ngno = \"0.9\"\n"},{"name":"lifetime.gno","body":"package lifetime\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/jaekwon/allowancesender/v0\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\n// LifetimeSubscription represents a one-time-payment subscription that\n// grants permanent access. Payment is pulled from a caller-supplied\n// *AllowanceSender cap-token; funds are sent to payeeAddr, which is\n// locked at construction.\ntype LifetimeSubscription struct {\n\townable.Ownable\n\tamount    int64\n\tpayeeAddr address\n\tsubs      *avl.Tree // address.String() -\u003e bool\n}\n\n// NewLifetimeSubscription creates a new lifetime subscription. owner is\n// the address authorized to call UpdateAmount.\nfunc NewLifetimeSubscription(owner address, amount int64, payeeAddr address) *LifetimeSubscription {\n\tif !payeeAddr.IsValid() {\n\t\tpanic(ErrInvalidPayee)\n\t}\n\treturn \u0026LifetimeSubscription{\n\t\tOwnable:   *ownable.NewWithAddress(owner),\n\t\tamount:    amount,\n\t\tpayeeAddr: payeeAddr,\n\t\tsubs:      avl.NewTree(),\n\t}\n}\n\n// Subscribe pulls the one-time payment from al into payeeAddr and\n// records subscriber as having a lifetime subscription. Already-subscribed\n// returns ErrAlreadySub WITHOUT pulling payment.\nfunc (ls *LifetimeSubscription) Subscribe(al *allowancesender.AllowanceSender, subscriber address) error {\n\tif !subscriber.IsValid() {\n\t\treturn ErrInvalidSubscriber\n\t}\n\tif ls.subs.Has(subscriber.String()) {\n\t\treturn ErrAlreadySub\n\t}\n\tal.Send(ls.payeeAddr, chain.NewCoins(chain.NewCoin(\"ugnot\", ls.amount)))\n\tls.subs.Set(subscriber.String(), true)\n\treturn nil\n}\n\n// HasValidSubscription verifies whether subscriber holds a lifetime subscription.\nfunc (ls *LifetimeSubscription) HasValidSubscription(subscriber address) error {\n\tif !ls.subs.Has(subscriber.String()) {\n\t\treturn ErrNoSub\n\t}\n\treturn nil\n}\n\n// UpdateAmount lets the subscription owner change the one-time amount.\n// rlm must be the caller's own captured cur; rlm.Previous().Address()\n// must equal the owner.\nfunc (ls *LifetimeSubscription) UpdateAmount(_ int, rlm realm, newAmount int64) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotAuthorized\n\t}\n\tif !ls.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ErrNotAuthorized\n\t}\n\tls.amount = newAmount\n\treturn nil\n}\n\n// GetAmount returns the current one-time subscription amount.\nfunc (ls *LifetimeSubscription) GetAmount() int64 {\n\treturn ls.amount\n}\n\n// GetPayee returns the locked payment destination.\nfunc (ls *LifetimeSubscription) GetPayee() address {\n\treturn ls.payeeAddr\n}\n"},{"name":"lifetime_test.gno","body":"package lifetime\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/jaekwon/allowancesender/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar (\n\talice = testutils.TestAddress(\"alice\")\n\tbob   = testutils.TestAddress(\"bob\")\n\tpayee = testutils.TestAddress(\"payee\")\n)\n\nfunc TestNewLifetimeSubscription_RejectsInvalidPayee(cur realm, t *testing.T) {\n\tuassert.PanicsWithMessage(t, cur, ErrInvalidPayee.Error(), func() {\n\t\tNewLifetimeSubscription(alice, 1000, address(\"\"))\n\t})\n}\n\nfunc TestSubscribe_RejectsInvalidSubscriber(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\terr := ls.Subscribe(\u0026allowancesender.AllowanceSender{}, address(\"\"))\n\tuassert.ErrorIs(t, err, ErrInvalidSubscriber)\n}\n\nfunc TestHasValidSubscription_NoSub(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\tuassert.ErrorIs(t, ls.HasValidSubscription(alice), ErrNoSub)\n}\n\nfunc TestHasValidSubscription_Active(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\tls.subs.Set(alice.String(), true)\n\tuassert.NoError(t, ls.HasValidSubscription(alice))\n}\n\nfunc TestSubscribe_AlreadySubscribedReturnsErrBeforeSend(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\tls.subs.Set(alice.String(), true)\n\n\terr := ls.Subscribe(\u0026allowancesender.AllowanceSender{}, alice)\n\tuassert.ErrorIs(t, err, ErrAlreadySub)\n}\n\nfunc TestSubscribe_SendPanicLeavesNoRecord(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\n\tfunc() {\n\t\tdefer func() { _ = recover() }()\n\t\tls.Subscribe(\u0026allowancesender.AllowanceSender{}, bob)\n\t}()\n\n\tuassert.ErrorIs(t, ls.HasValidSubscription(bob), ErrNoSub)\n}\n\nfunc TestUpdateAmount_OwnerSucceeds(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tvar err error\n\tfunc(cur realm) { err = ls.UpdateAmount(0, cur, 2000) }(cross(cur))\n\tuassert.NoError(t, err)\n\tuassert.Equal(t, int64(2000), ls.GetAmount())\n}\n\nfunc TestUpdateAmount_NonOwnerFails(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tvar err error\n\tfunc(cur realm) { err = ls.UpdateAmount(0, cur, 3000) }(cross(cur))\n\tuassert.ErrorIs(t, err, ErrNotAuthorized)\n}\n\nfunc TestGetPayee(cur realm, t *testing.T) {\n\tls := NewLifetimeSubscription(alice, 1000, payee)\n\tuassert.Equal(t, payee.String(), ls.GetPayee().String())\n}\n\nvar _ = chain.Coins{}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"UjAmPrWG2w30CDSwczWUMEwFZz1tT0ckMgbroNdAjjOBa+vJOTGcV/GFRGzlBgS1kux9GSUK95cjnHwPEmaTAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"recurring","path":"gno.land/p/demo/subscription/recurring","files":[{"name":"errors.gno","body":"package recurring\n\nimport \"errors\"\n\nvar (\n\tErrNoSub             = errors.New(\"recurring subscription: no active subscription found\")\n\tErrSubExpired        = errors.New(\"recurring subscription: your subscription has expired\")\n\tErrAlreadySub        = errors.New(\"recurring subscription: this address already has an active subscription\")\n\tErrNotAuthorized     = errors.New(\"recurring subscription: action not authorized\")\n\tErrInvalidPayee      = errors.New(\"recurring subscription: payee address is invalid\")\n\tErrInvalidSubscriber = errors.New(\"recurring subscription: subscriber address is invalid\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/subscription/recurring\"\ngno = \"0.9\"\n"},{"name":"recurring.gno","body":"package recurring\n\nimport (\n\t\"chain\"\n\t\"time\"\n\n\t\"gno.land/p/jaekwon/allowancesender/v0\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\n// RecurringSubscription represents a subscription that requires periodic\n// payments. Payment is pulled from a caller-supplied *AllowanceSender\n// cap-token (which the constructor of this struct trusts via\n// banker.IsCanonical). Funds are sent to payeeAddr, locked at\n// construction so that a leaked struct pointer cannot redirect payment.\ntype RecurringSubscription struct {\n\townable.Ownable\n\tduration  time.Duration\n\tamount    int64\n\tpayeeAddr address   // payment destination; set at construction.\n\tsubs      *avl.Tree // address.String() -\u003e time.Time\n}\n\n// NewRecurringSubscription creates a new recurring subscription. owner is\n// the address authorized to call UpdateAmount. payeeAddr must be valid\n// (typically the constructing realm's own address) and is the fixed\n// destination for every Send pulled by Subscribe.\nfunc NewRecurringSubscription(owner address, duration time.Duration, amount int64, payeeAddr address) *RecurringSubscription {\n\tif !payeeAddr.IsValid() {\n\t\tpanic(ErrInvalidPayee)\n\t}\n\treturn \u0026RecurringSubscription{\n\t\tOwnable:   *ownable.NewWithAddress(owner),\n\t\tduration:  duration,\n\t\tamount:    amount,\n\t\tpayeeAddr: payeeAddr,\n\t\tsubs:      avl.NewTree(),\n\t}\n}\n\n// HasValidSubscription verifies whether subscriber holds an active\n// recurring subscription.\nfunc (rs *RecurringSubscription) HasValidSubscription(subscriber address) error {\n\texpTime := rs.subs.Get(subscriber.String())\n\tif expTime == nil {\n\t\treturn ErrNoSub\n\t}\n\tif time.Now().After(expTime.(time.Time)) {\n\t\treturn ErrSubExpired\n\t}\n\treturn nil\n}\n\n// Subscribe pulls one period's worth of payment from al into payeeAddr\n// and records subscriber as having an active subscription until\n// time.Now() + duration. Already-subscribed-and-active returns\n// ErrAlreadySub WITHOUT pulling payment. Subscriber identity is\n// decoupled from cap-token authority: a single AllowanceSender holder\n// can subscribe any address (gift, self, or third-party).\n//\n// On Send-side failure (closed allowance, cap exceeded, insufficient\n// funds, etc.), al.Send panics — the whole tx reverts and no\n// subscription is recorded. Atomicity covers any half-state.\nfunc (rs *RecurringSubscription) Subscribe(al *allowancesender.AllowanceSender, subscriber address) error {\n\tif !subscriber.IsValid() {\n\t\treturn ErrInvalidSubscriber\n\t}\n\t// Already-sub check first; avoid burning cap-token capacity on a\n\t// duplicate that would just be rejected.\n\tif expTime := rs.subs.Get(subscriber.String()); expTime != nil {\n\t\tif time.Now().Before(expTime.(time.Time)) {\n\t\t\treturn ErrAlreadySub\n\t\t}\n\t}\n\tal.Send(rs.payeeAddr, chain.NewCoins(chain.NewCoin(\"ugnot\", rs.amount)))\n\trs.subs.Set(subscriber.String(), time.Now().Add(rs.duration))\n\treturn nil\n}\n\n// GetExpiration returns the subscription expiration for subscriber.\nfunc (rs *RecurringSubscription) GetExpiration(subscriber address) (time.Time, error) {\n\texpTime := rs.subs.Get(subscriber.String())\n\tif expTime == nil {\n\t\treturn time.Time{}, ErrNoSub\n\t}\n\treturn expTime.(time.Time), nil\n}\n\n// UpdateAmount lets the subscription owner change the per-period amount.\n// rlm must be the caller's own captured cur; rlm.Previous().Address()\n// must equal the owner.\nfunc (rs *RecurringSubscription) UpdateAmount(_ int, rlm realm, newAmount int64) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotAuthorized\n\t}\n\tif !rs.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ErrNotAuthorized\n\t}\n\trs.amount = newAmount\n\treturn nil\n}\n\n// GetAmount returns the current per-period subscription amount.\nfunc (rs *RecurringSubscription) GetAmount() int64 {\n\treturn rs.amount\n}\n\n// GetPayee returns the locked payment destination.\nfunc (rs *RecurringSubscription) GetPayee() address {\n\treturn rs.payeeAddr\n}\n"},{"name":"recurring_test.gno","body":"package recurring\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/jaekwon/allowancesender/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar (\n\talice = testutils.TestAddress(\"alice\")\n\tbob   = testutils.TestAddress(\"bob\")\n\tpayee = testutils.TestAddress(\"payee\")\n)\n\n// stubSender is a Send-only stand-in for *AllowanceSender used by these\n// tests. Subscribe accepts a concrete *allowancesender.AllowanceSender,\n// and outside the allowancesender package we cannot construct a working\n// one (the IsCanonical gate rejects synthetic bankers). Tests therefore\n// inject behavior through stubSender by wrapping it as a fake\n// AllowanceSender via the allowancesender package's same-package helpers.\n//\n// Since allowancesender's struct fields are unexported, we can't build\n// one here either. Instead each test uses the empty-struct shortcut\n// `\u0026allowancesender.AllowanceSender{}` which compiles cross-package\n// (no field-name reference) but produces an unusable instance whose\n// Send always panics. That panic propagates out of Subscribe, the tx\n// reverts, and tests assert the panic shape. For success-path tests,\n// we sidestep the AllowanceSender plumbing by constructing the\n// RecurringSubscription, then directly setting subs entries via the\n// in-package field — testing Subscribe's bookkeeping without the cap.\n//\n// This is sufficient for unit-level coverage of the subscription state\n// machine (already-sub gate, expiration, ownership, idempotency).\n// Full payment-path coverage requires integration txtars, which are\n// future work.\n\nfunc TestNewRecurringSubscription_RejectsInvalidPayee(cur realm, t *testing.T) {\n\tuassert.PanicsWithMessage(t, cur, ErrInvalidPayee.Error(), func() {\n\t\tNewRecurringSubscription(alice, time.Hour, 1000, address(\"\"))\n\t})\n}\n\nfunc TestSubscribe_RejectsInvalidSubscriber(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\terr := rs.Subscribe(\u0026allowancesender.AllowanceSender{}, address(\"\"))\n\tuassert.ErrorIs(t, err, ErrInvalidSubscriber)\n}\n\nfunc TestHasValidSubscription_NoSub(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\terr := rs.HasValidSubscription(alice)\n\tuassert.ErrorIs(t, err, ErrNoSub)\n}\n\nfunc TestHasValidSubscription_Active(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\trs.subs.Set(alice.String(), time.Now().Add(time.Hour))\n\tuassert.NoError(t, rs.HasValidSubscription(alice))\n}\n\nfunc TestHasValidSubscription_Expired(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\trs.subs.Set(alice.String(), time.Now().Add(-time.Hour))\n\tuassert.ErrorIs(t, rs.HasValidSubscription(alice), ErrSubExpired)\n}\n\n// TestSubscribe_AlreadyActiveReturnsErrBeforeSend exercises the\n// duplicate-detection short-circuit: when the subscriber is already\n// active, Subscribe returns ErrAlreadySub WITHOUT calling al.Send. We\n// pass an unusable empty AllowanceSender; if Send were called it would\n// panic, but the early return prevents that.\nfunc TestSubscribe_AlreadyActiveReturnsErrBeforeSend(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\trs.subs.Set(alice.String(), time.Now().Add(time.Hour))\n\n\terr := rs.Subscribe(\u0026allowancesender.AllowanceSender{}, alice)\n\tuassert.ErrorIs(t, err, ErrAlreadySub)\n}\n\n// TestSubscribe_SendPanicLeavesNoRecord exercises the send-side failure:\n// junk AllowanceSender → Send panics → tx revert. Since unit tests don't\n// have tx semantics, the panic propagates. Recover and assert no\n// subscription was recorded.\nfunc TestSubscribe_SendPanicLeavesNoRecord(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\n\tfunc() {\n\t\tdefer func() { _ = recover() }()\n\t\trs.Subscribe(\u0026allowancesender.AllowanceSender{}, bob)\n\t}()\n\n\tuassert.ErrorIs(t, rs.HasValidSubscription(bob), ErrNoSub)\n}\n\nfunc TestUpdateAmount_OwnerSucceeds(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tvar err error\n\tfunc(cur realm) { err = rs.UpdateAmount(0, cur, 2000) }(cross(cur))\n\tuassert.NoError(t, err)\n\tuassert.Equal(t, int64(2000), rs.GetAmount())\n}\n\nfunc TestUpdateAmount_NonOwnerFails(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tvar err error\n\tfunc(cur realm) { err = rs.UpdateAmount(0, cur, 3000) }(cross(cur))\n\tuassert.ErrorIs(t, err, ErrNotAuthorized)\n}\n\nfunc TestGetAmount(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\tuassert.Equal(t, int64(1000), rs.GetAmount())\n}\n\nfunc TestGetPayee(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\tuassert.Equal(t, payee.String(), rs.GetPayee().String())\n}\n\nfunc TestGetExpiration_NoSub(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\t_, err := rs.GetExpiration(alice)\n\tuassert.ErrorIs(t, err, ErrNoSub)\n}\n\nfunc TestGetExpiration_Active(cur realm, t *testing.T) {\n\trs := NewRecurringSubscription(alice, time.Hour, 1000, payee)\n\texpected := time.Now().Add(time.Hour)\n\trs.subs.Set(alice.String(), expected)\n\n\tgot, err := rs.GetExpiration(alice)\n\tuassert.NoError(t, err)\n\tuassert.Equal(t, expected.String(), got.String())\n}\n\n// silence unused-import warning for chain (used in production code paths).\nvar _ = chain.Coins{}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"7YolyjzaMQnELX6iIRlBUC0pxcb8dkNpBuuOeBisJihdmfYg6vlMgKUBi5Ig7NHDtEGLZ7ex+pQxibcXvHUBDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"tamagotchi","path":"gno.land/p/demo/tamagotchi","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tamagotchi\"\ngno = \"0.9\"\n"},{"name":"tamagotchi.gno","body":"package tamagotchi\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Tamagotchi structure\ntype Tamagotchi struct {\n\tname        string\n\thunger      int\n\thappiness   int\n\thealth      int\n\tage         int\n\tmaxAge      int\n\tsleepy      int\n\tcreated     time.Time\n\tlastUpdated time.Time\n}\n\nfunc New(name string) *Tamagotchi {\n\tnow := time.Now()\n\treturn \u0026Tamagotchi{\n\t\tname:        name,\n\t\thunger:      50,\n\t\thappiness:   50,\n\t\thealth:      50,\n\t\tmaxAge:      100,\n\t\tlastUpdated: now,\n\t\tcreated:     now,\n\t}\n}\n\nfunc (t *Tamagotchi) Name() string {\n\tt.update()\n\treturn t.name\n}\n\nfunc (t *Tamagotchi) Hunger() int {\n\tt.update()\n\treturn t.hunger\n}\n\nfunc (t *Tamagotchi) Happiness() int {\n\tt.update()\n\treturn t.happiness\n}\n\nfunc (t *Tamagotchi) Health() int {\n\tt.update()\n\treturn t.health\n}\n\nfunc (t *Tamagotchi) Age() int {\n\tt.update()\n\treturn t.age\n}\n\nfunc (t *Tamagotchi) Sleepy() int {\n\tt.update()\n\treturn t.sleepy\n}\n\n// Feed method for Tamagotchi\nfunc (t *Tamagotchi) Feed() {\n\tt.update()\n\tif t.dead() {\n\t\treturn\n\t}\n\tt.hunger = bound(t.hunger-10, 0, 100)\n}\n\n// Play method for Tamagotchi\nfunc (t *Tamagotchi) Play() {\n\tt.update()\n\tif t.dead() {\n\t\treturn\n\t}\n\tt.happiness = bound(t.happiness+10, 0, 100)\n}\n\n// Heal method for Tamagotchi\nfunc (t *Tamagotchi) Heal() {\n\tt.update()\n\n\tif t.dead() {\n\t\treturn\n\t}\n\tt.health = bound(t.health+10, 0, 100)\n}\n\nfunc (t Tamagotchi) dead() bool { return t.health == 0 }\n\n// Update applies changes based on the duration since the last update\nfunc (t *Tamagotchi) update() {\n\tif t.dead() {\n\t\treturn\n\t}\n\n\tnow := time.Now()\n\tif t.lastUpdated == now {\n\t\treturn\n\t}\n\n\tduration := now.Sub(t.lastUpdated)\n\telapsedMins := int(duration.Minutes())\n\n\tt.hunger = bound(t.hunger+elapsedMins, 0, 100)\n\tt.happiness = bound(t.happiness-elapsedMins, 0, 100)\n\tt.health = bound(t.health-elapsedMins, 0, 100)\n\tt.sleepy = bound(t.sleepy+elapsedMins, 0, 100)\n\n\t// age is hours since created\n\tt.age = int(now.Sub(t.created).Hours())\n\tif t.age \u003e t.maxAge {\n\t\tt.age = t.maxAge\n\t\tt.health = 0\n\t}\n\tif t.health == 0 {\n\t\tt.sleepy = 0\n\t\tt.happiness = 0\n\t\tt.hunger = 0\n\t}\n\n\tt.lastUpdated = now\n}\n\n// Face returns an ASCII art representation of the Tamagotchi's current state\nfunc (t *Tamagotchi) Face() string {\n\tt.update()\n\treturn t.face()\n}\n\nfunc (t *Tamagotchi) face() string {\n\tswitch {\n\tcase t.health == 0:\n\t\treturn \"😵\" // dead face\n\tcase t.health \u003c 30:\n\t\treturn \"😷\" // sick face\n\tcase t.happiness \u003c 30:\n\t\treturn \"😢\" // sad face\n\tcase t.hunger \u003e 70:\n\t\treturn \"😫\" // hungry face\n\tcase t.sleepy \u003e 70:\n\t\treturn \"😴\" // sleepy face\n\tdefault:\n\t\treturn \"😃\" // happy face\n\t}\n}\n\n// Markdown method for Tamagotchi\nfunc (t *Tamagotchi) Markdown() string {\n\tt.update()\n\treturn ufmt.Sprintf(`# %s %s\n\n* age: %d\n* hunger: %d\n* happiness: %d\n* health: %d\n* sleepy: %d`,\n\t\tt.name, t.Face(),\n\t\tt.age, t.hunger, t.happiness, t.health, t.sleepy,\n\t)\n}\n\nfunc bound(n, min, max int) int {\n\tif n \u003c min {\n\t\treturn min\n\t}\n\tif n \u003e max {\n\t\treturn max\n\t}\n\treturn n\n}\n"},{"name":"z0_filetest.gno","body":"package main\n\nimport (\n\t\"os\"\n\t\"time\"\n\n\t\"gno.land/p/demo/tamagotchi\"\n)\n\nfunc main() {\n\tt := tamagotchi.New(\"Gnome\")\n\n\tprintln(\"\\n-- INITIAL\\n\")\n\tprintln(t.Markdown())\n\n\tprintln(\"\\n-- WAIT 20 minutes\\n\")\n\tos.Sleep(20 * time.Minute)\n\tprintln(t.Markdown())\n\n\tprintln(\"\\n-- FEEDx3, PLAYx2, HEALx4\\n\")\n\tt.Feed()\n\tt.Feed()\n\tt.Feed()\n\tt.Play()\n\tt.Play()\n\tt.Heal()\n\tt.Heal()\n\tt.Heal()\n\tt.Heal()\n\tprintln(t.Markdown())\n\n\tprintln(\"\\n-- WAIT 20 minutes\\n\")\n\tos.Sleep(20 * time.Minute)\n\tprintln(t.Markdown())\n\n\tprintln(\"\\n-- WAIT 20 hours\\n\")\n\tos.Sleep(20 * time.Hour)\n\tprintln(t.Markdown())\n\n\tprintln(\"\\n-- WAIT 20 hours\\n\")\n\tos.Sleep(20 * time.Hour)\n\tprintln(t.Markdown())\n}\n\n// Output:\n//\n// -- INITIAL\n//\n// # Gnome 😃\n//\n// * age: 0\n// * hunger: 50\n// * happiness: 50\n// * health: 50\n// * sleepy: 0\n//\n// -- WAIT 20 minutes\n//\n// # Gnome 😃\n//\n// * age: 0\n// * hunger: 70\n// * happiness: 30\n// * health: 30\n// * sleepy: 20\n//\n// -- FEEDx3, PLAYx2, HEALx4\n//\n// # Gnome 😃\n//\n// * age: 0\n// * hunger: 40\n// * happiness: 50\n// * health: 70\n// * sleepy: 20\n//\n// -- WAIT 20 minutes\n//\n// # Gnome 😃\n//\n// * age: 0\n// * hunger: 60\n// * happiness: 30\n// * health: 50\n// * sleepy: 40\n//\n// -- WAIT 20 hours\n//\n// # Gnome 😵\n//\n// * age: 20\n// * hunger: 0\n// * happiness: 0\n// * health: 0\n// * sleepy: 0\n//\n// -- WAIT 20 hours\n//\n// # Gnome 😵\n//\n// * age: 20\n// * hunger: 0\n// * happiness: 0\n// * health: 0\n// * sleepy: 0\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"aOsUgFsrs1UxVIc0Ghx5Os2FRbLSKqyl7clINC+K6LbNvVyaoQdVkIUkpgqGCtBjbHnuCUBuEq1jEkQfFmTyBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"todolist","path":"gno.land/p/demo/todolist","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/todolist\"\ngno = \"0.9\"\n"},{"name":"todolist.gno","body":"package todolist\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\ntype TodoList struct {\n\tTitle string\n\tTasks *avl.Tree\n\tOwner address\n}\n\ntype Task struct {\n\tTitle string\n\tDone  bool\n}\n\nfunc NewTodoList(title string) *TodoList {\n\treturn \u0026TodoList{\n\t\tTitle: title,\n\t\tTasks: avl.NewTree(),\n\t\tOwner: unsafe.OriginCaller(),\n\t}\n}\n\nfunc NewTask(title string) *Task {\n\treturn \u0026Task{\n\t\tTitle: title,\n\t\tDone:  false,\n\t}\n}\n\nfunc (tl *TodoList) AddTask(id int, task *Task) {\n\ttl.Tasks.Set(strconv.Itoa(id), task)\n}\n\nfunc ToggleTaskStatus(task *Task) {\n\ttask.Done = !task.Done\n}\n\nfunc (tl *TodoList) RemoveTask(taskId string) {\n\ttl.Tasks.Remove(taskId)\n}\n\nfunc (tl *TodoList) GetTasks() []*Task {\n\ttasks := make([]*Task, 0, tl.Tasks.Size())\n\ttl.Tasks.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\ttasks = append(tasks, value.(*Task))\n\t\treturn false\n\t})\n\treturn tasks\n}\n\nfunc (tl *TodoList) GetTodolistOwner() address {\n\treturn tl.Owner\n}\n\nfunc (tl *TodoList) GetTodolistTitle() string {\n\treturn tl.Title\n}\n"},{"name":"todolist_test.gno","body":"package todolist\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNewTodoList(t *testing.T) {\n\ttitle := \"My Todo List\"\n\ttodoList := NewTodoList(title)\n\n\tuassert.Equal(t, title, todoList.GetTodolistTitle())\n\tuassert.Equal(t, 0, len(todoList.GetTasks()))\n\tuassert.Equal(t, unsafe.OriginCaller().String(), todoList.GetTodolistOwner().String())\n}\n\nfunc TestNewTask(t *testing.T) {\n\ttitle := \"My Task\"\n\ttask := NewTask(title)\n\n\tuassert.Equal(t, title, task.Title)\n\tuassert.False(t, task.Done, \"Expected task to be not done, but it is done\")\n}\n\nfunc TestAddTask(t *testing.T) {\n\ttodoList := NewTodoList(\"My Todo List\")\n\ttask := NewTask(\"My Task\")\n\n\ttodoList.AddTask(1, task)\n\n\ttasks := todoList.GetTasks()\n\n\tuassert.Equal(t, 1, len(tasks))\n\tuassert.True(t, tasks[0] == task, \"Task does not match\")\n}\n\nfunc TestToggleTaskStatus(t *testing.T) {\n\ttask := NewTask(\"My Task\")\n\n\tToggleTaskStatus(task)\n\tuassert.True(t, task.Done, \"Expected task to be done, but it is not done\")\n\n\tToggleTaskStatus(task)\n\tuassert.False(t, task.Done, \"Expected task to be done, but it is not done\")\n}\n\nfunc TestRemoveTask(t *testing.T) {\n\ttodoList := NewTodoList(\"My Todo List\")\n\ttask := NewTask(\"My Task\")\n\ttodoList.AddTask(1, task)\n\n\ttodoList.RemoveTask(\"1\")\n\n\ttasks := todoList.GetTasks()\n\tuassert.Equal(t, 0, len(tasks))\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"F2DVvbMjJzE/E80IoxMPzbsia2fWdP0s4WUpwt7ZdHU0gc6u5j9l/w5qFKPjjZY5c8FUnhJnZ26lCpBm/U48Dg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"grc1155","path":"gno.land/p/demo/tokens/grc1155","files":[{"name":"README.md","body":"# GRC-1155 Spec: Multi Token Standard\n\nGRC1155 is a specification for managing multiple tokens based on Gnoland. The name and design is based on Ethereum's ERC1155 standard.\n\n## See also:\n\n[ERC-1155 Spec][erc-1155]\n\n[erc-1155]: https://eips.ethereum.org/EIPS/eip-1155\n"},{"name":"basic_grc1155_token.gno","body":"package grc1155\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"math/overflow\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype basicGRC1155Token struct {\n\turi               string\n\tbalances          avl.Tree // \"TokenId:Address\" -\u003e int64\n\toperatorApprovals avl.Tree // \"OwnerAddress:OperatorAddress\" -\u003e bool\n}\n\nvar _ IGRC1155 = (*basicGRC1155Token)(nil)\n\n// Returns new basic GRC1155 token\nfunc NewBasicGRC1155Token(uri string) *basicGRC1155Token {\n\treturn \u0026basicGRC1155Token{\n\t\turi:               uri,\n\t\tbalances:          avl.Tree{},\n\t\toperatorApprovals: avl.Tree{},\n\t}\n}\n\nfunc (s *basicGRC1155Token) Uri() string { return s.uri }\n\n// BalanceOf returns the input address's balance of the token type requested\nfunc (s *basicGRC1155Token) BalanceOf(addr address, tid TokenID) (int64, error) {\n\tif !isValidAddress(addr) {\n\t\treturn 0, ErrInvalidAddress\n\t}\n\n\tkey := string(tid) + \":\" + addr.String()\n\tbalance := s.balances.Get(key)\n\tif balance == nil {\n\t\treturn 0, nil\n\t}\n\n\treturn balance.(int64), nil\n}\n\n// BalanceOfBatch returns the balance of multiple account/token pairs\nfunc (s *basicGRC1155Token) BalanceOfBatch(owners []address, batch []TokenID) ([]int64, error) {\n\tif len(owners) != len(batch) {\n\t\treturn nil, ErrMismatchLength\n\t}\n\n\tbalanceOfBatch := make([]int64, len(owners))\n\n\tfor i := 0; i \u003c len(owners); i++ {\n\t\tbalanceOfBatch[i], _ = s.BalanceOf(owners[i], batch[i])\n\t}\n\n\treturn balanceOfBatch, nil\n}\n\n// SetApprovalForAll can approve the operator to operate on all tokens\nfunc (s *basicGRC1155Token) SetApprovalForAll(operator address, approved bool) error {\n\tif !isValidAddress(operator) {\n\t\treturn ErrInvalidAddress\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\treturn s.setApprovalForAll(caller, operator, approved)\n}\n\n// IsApprovedForAll returns true if operator is the owner or is approved for all by the owner.\n// Otherwise, returns false\nfunc (s *basicGRC1155Token) IsApprovedForAll(owner, operator address) bool {\n\tif operator == owner {\n\t\treturn true\n\t}\n\tkey := owner.String() + \":\" + operator.String()\n\tapproved := s.operatorApprovals.Get(key)\n\tif approved == nil {\n\t\treturn false\n\t}\n\n\tab, ok := approved.(bool)\n\treturn ok \u0026\u0026 ab\n}\n\n// Safely transfers `tokenId` token from `from` to `to`, checking that\n// contract recipients are aware of the GRC1155 protocol to prevent\n// tokens from being forever locked.\nfunc (s *basicGRC1155Token) SafeTransferFrom(from, to address, tid TokenID, amount int64) error {\n\tcaller := unsafe.OriginCaller()\n\tif !s.IsApprovedForAll(caller, from) {\n\t\treturn ErrCallerIsNotOwnerOrApproved\n\t}\n\n\terr := s.safeBatchTransferFrom(from, to, []TokenID{tid}, []int64{amount})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !s.doSafeTransferAcceptanceCheck(caller, from, to, tid, amount) {\n\t\treturn ErrTransferToRejectedOrNonGRC1155Receiver\n\t}\n\n\temit(\u0026TransferSingleEvent{caller, from, to, tid, amount})\n\n\treturn nil\n}\n\n// Safely transfers a `batch` of tokens from `from` to `to`, checking that\n// contract recipients are aware of the GRC1155 protocol to prevent\n// tokens from being forever locked.\nfunc (s *basicGRC1155Token) SafeBatchTransferFrom(from, to address, batch []TokenID, amounts []int64) error {\n\tcaller := unsafe.OriginCaller()\n\tif !s.IsApprovedForAll(caller, from) {\n\t\treturn ErrCallerIsNotOwnerOrApproved\n\t}\n\n\terr := s.safeBatchTransferFrom(from, to, batch, amounts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !s.doSafeBatchTransferAcceptanceCheck(caller, from, to, batch, amounts) {\n\t\treturn ErrTransferToRejectedOrNonGRC1155Receiver\n\t}\n\n\temit(\u0026TransferBatchEvent{caller, from, to, batch, amounts})\n\n\treturn nil\n}\n\n// Creates `amount` tokens of token type `id`, and assigns them to `to`. Also checks that\n// contract recipients are using GRC1155 protocol.\nfunc (s *basicGRC1155Token) SafeMint(to address, tid TokenID, amount int64) error {\n\tcaller := unsafe.OriginCaller()\n\n\terr := s.mintBatch(to, []TokenID{tid}, []int64{amount})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !s.doSafeTransferAcceptanceCheck(caller, zeroAddress, to, tid, amount) {\n\t\treturn ErrTransferToRejectedOrNonGRC1155Receiver\n\t}\n\n\temit(\u0026TransferSingleEvent{caller, zeroAddress, to, tid, amount})\n\n\treturn nil\n}\n\n// Batch version of `SafeMint()`. Also checks that\n// contract recipients are using GRC1155 protocol.\nfunc (s *basicGRC1155Token) SafeBatchMint(to address, batch []TokenID, amounts []int64) error {\n\tcaller := unsafe.OriginCaller()\n\n\terr := s.mintBatch(to, batch, amounts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !s.doSafeBatchTransferAcceptanceCheck(caller, zeroAddress, to, batch, amounts) {\n\t\treturn ErrTransferToRejectedOrNonGRC1155Receiver\n\t}\n\n\temit(\u0026TransferBatchEvent{caller, zeroAddress, to, batch, amounts})\n\n\treturn nil\n}\n\n// Destroys `amount` tokens of token type `id` from `from`.\nfunc (s *basicGRC1155Token) Burn(from address, tid TokenID, amount int64) error {\n\tcaller := unsafe.OriginCaller()\n\n\terr := s.burnBatch(from, []TokenID{tid}, []int64{amount})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\temit(\u0026TransferSingleEvent{caller, from, zeroAddress, tid, amount})\n\n\treturn nil\n}\n\n// Batch version of `Burn()`\nfunc (s *basicGRC1155Token) BatchBurn(from address, batch []TokenID, amounts []int64) error {\n\tcaller := unsafe.OriginCaller()\n\n\terr := s.burnBatch(from, batch, amounts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\temit(\u0026TransferBatchEvent{caller, from, zeroAddress, batch, amounts})\n\n\treturn nil\n}\n\n/* Helper methods */\n\n// Helper for SetApprovalForAll(): approve `operator` to operate on all of `owner` tokens\nfunc (s *basicGRC1155Token) setApprovalForAll(owner, operator address, approved bool) error {\n\tif owner == operator {\n\t\treturn nil\n\t}\n\n\tkey := owner.String() + \":\" + operator.String()\n\tif approved {\n\t\ts.operatorApprovals.Set(key, approved)\n\t} else {\n\t\ts.operatorApprovals.Remove(key)\n\t}\n\n\temit(\u0026ApprovalForAllEvent{owner, operator, approved})\n\n\treturn nil\n}\n\n// Helper for SafeTransferFrom() and SafeBatchTransferFrom()\nfunc (s *basicGRC1155Token) safeBatchTransferFrom(from, to address, batch []TokenID, amounts []int64) error {\n\tif len(batch) != len(amounts) {\n\t\treturn ErrMismatchLength\n\t}\n\tif !isValidAddress(from) || !isValidAddress(to) {\n\t\treturn ErrInvalidAddress\n\t}\n\tif from == to {\n\t\treturn ErrCannotTransferToSelf\n\t}\n\tfor _, amount := range amounts {\n\t\tif amount \u003c 0 {\n\t\t\treturn ErrInvalidAmount\n\t\t}\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\ts.beforeTokenTransfer(caller, from, to, batch, amounts)\n\n\tfor i := 0; i \u003c len(batch); i++ {\n\t\ttid := batch[i]\n\t\tamount := amounts[i]\n\t\tfromBalance, err := s.BalanceOf(from, tid)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif fromBalance \u003c amount {\n\t\t\treturn ErrInsufficientBalance\n\t\t}\n\t\ttoBalance, err := s.BalanceOf(to, tid)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tfromBalance = overflow.Sub64p(fromBalance, amount)\n\t\ttoBalance = overflow.Add64p(toBalance, amount)\n\t\tfromBalanceKey := string(tid) + \":\" + from.String()\n\t\ttoBalanceKey := string(tid) + \":\" + to.String()\n\t\ts.balances.Set(fromBalanceKey, fromBalance)\n\t\ts.balances.Set(toBalanceKey, toBalance)\n\t}\n\n\ts.afterTokenTransfer(caller, from, to, batch, amounts)\n\n\treturn nil\n}\n\n// Helper for SafeMint() and SafeBatchMint()\nfunc (s *basicGRC1155Token) mintBatch(to address, batch []TokenID, amounts []int64) error {\n\tif len(batch) != len(amounts) {\n\t\treturn ErrMismatchLength\n\t}\n\tif !isValidAddress(to) {\n\t\treturn ErrInvalidAddress\n\t}\n\tfor _, amount := range amounts {\n\t\tif amount \u003c 0 {\n\t\t\treturn ErrInvalidAmount\n\t\t}\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\ts.beforeTokenTransfer(caller, zeroAddress, to, batch, amounts)\n\n\tfor i := 0; i \u003c len(batch); i++ {\n\t\ttid := batch[i]\n\t\tamount := amounts[i]\n\t\ttoBalance, err := s.BalanceOf(to, tid)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\ttoBalance = overflow.Add64p(toBalance, amount)\n\t\ttoBalanceKey := string(tid) + \":\" + to.String()\n\t\ts.balances.Set(toBalanceKey, toBalance)\n\t}\n\n\ts.afterTokenTransfer(caller, zeroAddress, to, batch, amounts)\n\n\treturn nil\n}\n\n// Helper for Burn() and BurnBatch()\nfunc (s *basicGRC1155Token) burnBatch(from address, batch []TokenID, amounts []int64) error {\n\tif len(batch) != len(amounts) {\n\t\treturn ErrMismatchLength\n\t}\n\tif !isValidAddress(from) {\n\t\treturn ErrInvalidAddress\n\t}\n\tfor _, amount := range amounts {\n\t\tif amount \u003c 0 {\n\t\t\treturn ErrInvalidAmount\n\t\t}\n\t}\n\n\tcaller := unsafe.OriginCaller()\n\ts.beforeTokenTransfer(caller, from, zeroAddress, batch, amounts)\n\n\tfor i := 0; i \u003c len(batch); i++ {\n\t\ttid := batch[i]\n\t\tamount := amounts[i]\n\t\tfromBalance, err := s.BalanceOf(from, tid)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif fromBalance \u003c amount {\n\t\t\treturn ErrBurnAmountExceedsBalance\n\t\t}\n\t\tfromBalance = overflow.Sub64p(fromBalance, amount)\n\t\tfromBalanceKey := string(tid) + \":\" + from.String()\n\t\ts.balances.Set(fromBalanceKey, fromBalance)\n\t}\n\n\ts.afterTokenTransfer(caller, from, zeroAddress, batch, amounts)\n\n\treturn nil\n}\n\nfunc (s *basicGRC1155Token) setUri(newUri string) {\n\ts.uri = newUri\n\temit(\u0026UpdateURIEvent{newUri})\n}\n\nfunc (s *basicGRC1155Token) beforeTokenTransfer(operator, from, to address, batch []TokenID, amounts []int64) {\n\t// TODO: Implementation\n}\n\nfunc (s *basicGRC1155Token) afterTokenTransfer(operator, from, to address, batch []TokenID, amounts []int64) {\n\t// TODO: Implementation\n}\n\nfunc (s *basicGRC1155Token) doSafeTransferAcceptanceCheck(operator, from, to address, tid TokenID, amount int64) bool {\n\t// TODO: Implementation\n\treturn true\n}\n\nfunc (s *basicGRC1155Token) doSafeBatchTransferAcceptanceCheck(operator, from, to address, batch []TokenID, amounts []int64) bool {\n\t// TODO: Implementation\n\treturn true\n}\n\nfunc (s *basicGRC1155Token) RenderHome() (str string) {\n\tstr += ufmt.Sprintf(\"# URI:%s\\n\", s.uri)\n\n\treturn\n}\n\nfunc (mt *basicGRC1155Token) Getter() MultiTokenGetter {\n\treturn func() IGRC1155 {\n\t\treturn mt\n\t}\n}\n"},{"name":"basic_grc1155_token_test.gno","body":"package grc1155\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nconst dummyURI = \"ipfs://xyz\"\n\nfunc TestNewBasicGRC1155Token(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n}\n\nfunc TestUri(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\tuassert.Equal(t, dummyURI, dummy.Uri())\n}\n\nfunc TestBalanceOf(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\ttid1 := TokenID(\"1\")\n\ttid2 := TokenID(\"2\")\n\n\t_, err := dummy.BalanceOf(zeroAddress, tid1)\n\tuassert.Error(t, err, \"should result in error\")\n\n\tbalanceAddr1OfToken1, err := dummy.BalanceOf(addr1, tid1)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(0), balanceAddr1OfToken1)\n\n\tdummy.mintBatch(addr1, []TokenID{tid1, tid2}, []int64{10, 100})\n\tdummy.mintBatch(addr2, []TokenID{tid1}, []int64{20})\n\n\tbalanceAddr1OfToken1, err = dummy.BalanceOf(addr1, tid1)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tbalanceAddr1OfToken2, err := dummy.BalanceOf(addr1, tid2)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tbalanceAddr2OfToken1, err := dummy.BalanceOf(addr2, tid1)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tuassert.Equal(t, int64(10), balanceAddr1OfToken1)\n\tuassert.Equal(t, int64(100), balanceAddr1OfToken2)\n\tuassert.Equal(t, int64(20), balanceAddr2OfToken1)\n}\n\nfunc TestBalanceOfBatch(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\ttid1 := TokenID(\"1\")\n\ttid2 := TokenID(\"2\")\n\n\tbalanceBatch, err := dummy.BalanceOfBatch([]address{addr1, addr2}, []TokenID{tid1, tid2})\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(0), balanceBatch[0])\n\tuassert.Equal(t, int64(0), balanceBatch[1])\n\n\tdummy.mintBatch(addr1, []TokenID{tid1}, []int64{10})\n\tdummy.mintBatch(addr2, []TokenID{tid2}, []int64{20})\n\n\tbalanceBatch, err = dummy.BalanceOfBatch([]address{addr1, addr2}, []TokenID{tid1, tid2})\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(10), balanceBatch[0])\n\tuassert.Equal(t, int64(20), balanceBatch[1])\n}\n\nfunc TestIsApprovedForAll(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\tisApprovedForAll := dummy.IsApprovedForAll(addr1, addr2)\n\tuassert.False(t, isApprovedForAll)\n}\n\nfunc TestSetApprovalForAll(t *testing.T) {\n\t//alice := testutils.TestAddress(\"alice\")\n\t//testing.SetOriginCaller(alice)\n\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tcaller := unsafe.OriginCaller()\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\tisApprovedForAll := dummy.IsApprovedForAll(caller, addr)\n\tuassert.False(t, isApprovedForAll)\n\n\terr := dummy.SetApprovalForAll(addr, true)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tisApprovedForAll = dummy.IsApprovedForAll(caller, addr)\n\tuassert.True(t, isApprovedForAll)\n\n\terr = dummy.SetApprovalForAll(addr, false)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tisApprovedForAll = dummy.IsApprovedForAll(caller, addr)\n\tuassert.False(t, isApprovedForAll)\n}\n\nfunc TestSafeTransferFrom(t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetOriginCaller(alice)\n\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tcaller := unsafe.OriginCaller()\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\ttid := TokenID(\"1\")\n\n\tdummy.mintBatch(caller, []TokenID{tid}, []int64{100})\n\n\terr := dummy.SafeTransferFrom(caller, zeroAddress, tid, 10)\n\tuassert.Error(t, err, \"should result in error\")\n\n\terr = dummy.SafeTransferFrom(caller, addr, tid, 160)\n\tuassert.Error(t, err, \"should result in error\")\n\n\terr = dummy.SafeTransferFrom(caller, addr, tid, 60)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\t// Check balance of caller after transfer\n\tbalanceOfCaller, err := dummy.BalanceOf(caller, tid)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(40), balanceOfCaller)\n\n\t// Check balance of addr after transfer\n\tbalanceOfAddr, err := dummy.BalanceOf(addr, tid)\n\tuassert.NoError(t, err, \"should not result in error\")\n\tuassert.Equal(t, int64(60), balanceOfAddr)\n}\n\nfunc TestSafeBatchTransferFrom(t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetOriginCaller(alice)\n\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\tcaller := unsafe.OriginCaller()\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\ttid1 := TokenID(\"1\")\n\ttid2 := TokenID(\"2\")\n\n\tdummy.mintBatch(caller, []TokenID{tid1, tid2}, []int64{10, 100})\n\n\terr := dummy.SafeBatchTransferFrom(caller, zeroAddress, []TokenID{tid1, tid2}, []int64{4, 60})\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.SafeBatchTransferFrom(caller, addr, []TokenID{tid1, tid2}, []int64{40, 60})\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.SafeBatchTransferFrom(caller, addr, []TokenID{tid1}, []int64{40, 60})\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.SafeBatchTransferFrom(caller, addr, []TokenID{tid1, tid2}, []int64{4, 60})\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tbalanceBatch, err := dummy.BalanceOfBatch([]address{caller, addr, caller, addr}, []TokenID{tid1, tid1, tid2, tid2})\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\t// Check token1's balance of caller after batch transfer\n\tuassert.Equal(t, int64(6), balanceBatch[0])\n\n\t// Check token1's balance of addr after batch transfer\n\tuassert.Equal(t, int64(4), balanceBatch[1])\n\n\t// Check token2's balance of caller after batch transfer\n\tuassert.Equal(t, int64(40), balanceBatch[2])\n\n\t// Check token2's balance of addr after batch transfer\n\tuassert.Equal(t, int64(60), balanceBatch[3])\n}\n\nfunc TestSafeMint(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\ttid1 := TokenID(\"1\")\n\ttid2 := TokenID(\"2\")\n\n\terr := dummy.SafeMint(zeroAddress, tid1, 100)\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.SafeMint(addr1, tid1, 100)\n\tuassert.NoError(t, err, \"should not result in error\")\n\terr = dummy.SafeMint(addr1, tid2, 200)\n\tuassert.NoError(t, err, \"should not result in error\")\n\terr = dummy.SafeMint(addr2, tid1, 50)\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tbalanceBatch, err := dummy.BalanceOfBatch([]address{addr1, addr2, addr1}, []TokenID{tid1, tid1, tid2})\n\tuassert.NoError(t, err, \"should not result in error\")\n\t// Check token1's balance of addr1 after mint\n\tuassert.Equal(t, int64(100), balanceBatch[0])\n\t// Check token1's balance of addr2 after mint\n\tuassert.Equal(t, int64(50), balanceBatch[1])\n\t// Check token2's balance of addr1 after mint\n\tuassert.Equal(t, int64(200), balanceBatch[2])\n}\n\nfunc TestSafeBatchMint(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr1 := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\taddr2 := address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\n\ttid1 := TokenID(\"1\")\n\ttid2 := TokenID(\"2\")\n\n\terr := dummy.SafeBatchMint(zeroAddress, []TokenID{tid1, tid2}, []int64{100, 200})\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.SafeBatchMint(addr1, []TokenID{tid1, tid2}, []int64{100, 200})\n\tuassert.NoError(t, err, \"should not result in error\")\n\terr = dummy.SafeBatchMint(addr2, []TokenID{tid1, tid2}, []int64{300, 400})\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\tbalanceBatch, err := dummy.BalanceOfBatch([]address{addr1, addr2, addr1, addr2}, []TokenID{tid1, tid1, tid2, tid2})\n\tuassert.NoError(t, err, \"should not result in error\")\n\t// Check token1's balance of addr1 after batch mint\n\tuassert.Equal(t, int64(100), balanceBatch[0])\n\t// Check token1's balance of addr2 after batch mint\n\tuassert.Equal(t, int64(300), balanceBatch[1])\n\t// Check token2's balance of addr1 after batch mint\n\tuassert.Equal(t, int64(200), balanceBatch[2])\n\t// Check token2's balance of addr2 after batch mint\n\tuassert.Equal(t, int64(400), balanceBatch[3])\n}\n\nfunc TestBurn(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\ttid1 := TokenID(\"1\")\n\ttid2 := TokenID(\"2\")\n\n\tdummy.mintBatch(addr, []TokenID{tid1, tid2}, []int64{100, 200})\n\terr := dummy.Burn(zeroAddress, tid1, int64(60))\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.Burn(addr, tid1, int64(160))\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.Burn(addr, tid1, int64(60))\n\tuassert.NoError(t, err, \"should not result in error\")\n\terr = dummy.Burn(addr, tid2, int64(60))\n\tuassert.NoError(t, err, \"should not result in error\")\n\tbalanceBatch, err := dummy.BalanceOfBatch([]address{addr, addr}, []TokenID{tid1, tid2})\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\t// Check token1's balance of addr after burn\n\tuassert.Equal(t, int64(40), balanceBatch[0])\n\t// Check token2's balance of addr after burn\n\tuassert.Equal(t, int64(140), balanceBatch[1])\n}\n\nfunc TestBatchBurn(t *testing.T) {\n\tdummy := NewBasicGRC1155Token(dummyURI)\n\tuassert.True(t, dummy != nil, \"should not be nil\")\n\n\taddr := address(\"g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm\")\n\n\ttid1 := TokenID(\"1\")\n\ttid2 := TokenID(\"2\")\n\n\tdummy.mintBatch(addr, []TokenID{tid1, tid2}, []int64{100, 200})\n\terr := dummy.BatchBurn(zeroAddress, []TokenID{tid1, tid2}, []int64{60, 60})\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.BatchBurn(addr, []TokenID{tid1, tid2}, []int64{160, 60})\n\tuassert.Error(t, err, \"should result in error\")\n\terr = dummy.BatchBurn(addr, []TokenID{tid1, tid2}, []int64{60, 60})\n\tuassert.NoError(t, err, \"should not result in error\")\n\tbalanceBatch, err := dummy.BalanceOfBatch([]address{addr, addr}, []TokenID{tid1, tid2})\n\tuassert.NoError(t, err, \"should not result in error\")\n\n\t// Check token1's balance of addr after batch burn\n\tuassert.Equal(t, int64(40), balanceBatch[0])\n\t// Check token2's balance of addr after batch burn\n\tuassert.Equal(t, int64(140), balanceBatch[1])\n}\n"},{"name":"errors.gno","body":"package grc1155\n\nimport \"errors\"\n\nvar (\n\tErrInvalidAddress                         = errors.New(\"invalid address\")\n\tErrMismatchLength                         = errors.New(\"accounts and ids length mismatch\")\n\tErrCannotTransferToSelf                   = errors.New(\"cannot send transfer to self\")\n\tErrTransferToRejectedOrNonGRC1155Receiver = errors.New(\"transfer to rejected or non GRC1155Receiver implementer\")\n\tErrCallerIsNotOwnerOrApproved             = errors.New(\"caller is not token owner or approved\")\n\tErrInsufficientBalance                    = errors.New(\"insufficient balance for transfer\")\n\tErrBurnAmountExceedsBalance               = errors.New(\"burn amount exceeds balance\")\n\tErrInvalidAmount                          = errors.New(\"invalid amount\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tokens/grc1155\"\ngno = \"0.9\"\n"},{"name":"igrc1155.gno","body":"package grc1155\n\ntype IGRC1155 interface {\n\tSafeTransferFrom(from, to address, tid TokenID, amount int64) error\n\tSafeBatchTransferFrom(from, to address, batch []TokenID, amounts []int64) error\n\tBalanceOf(owner address, tid TokenID) (int64, error)\n\tBalanceOfBatch(owners []address, batch []TokenID) ([]int64, error)\n\tSetApprovalForAll(operator address, approved bool) error\n\tIsApprovedForAll(owner, operator address) bool\n}\n\ntype TokenID string\n\ntype TransferSingleEvent struct {\n\tOperator address\n\tFrom     address\n\tTo       address\n\tTokenID  TokenID\n\tAmount   int64\n}\n\ntype TransferBatchEvent struct {\n\tOperator address\n\tFrom     address\n\tTo       address\n\tBatch    []TokenID\n\tAmounts  []int64\n}\n\ntype ApprovalForAllEvent struct {\n\tOwner    address\n\tOperator address\n\tApproved bool\n}\n\ntype UpdateURIEvent struct {\n\tURI string\n}\n\ntype MultiTokenGetter func() IGRC1155\n"},{"name":"util.gno","body":"package grc1155\n\nconst zeroAddress address = \"\"\n\nfunc isValidAddress(addr address) bool {\n\tif !addr.IsValid() {\n\t\treturn false\n\t}\n\treturn true\n}\n\nfunc emit(event any) {\n\t// TODO: setup a pubsub system here?\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"zMaDGJW7SX5/rlFg/z/JqfrWS/ikap9LMW3Hzui+7h3m2ns4N7tOhwN+eng1vTqaXqUboSMJ19NX+zaKae3ODQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"grc777","path":"gno.land/p/demo/tokens/grc777","files":[{"name":"dummy_test.gno","body":"package grc777\n\nimport (\n\t\"testing\"\n)\n\ntype dummyImpl struct{}\n\n// FIXME: this should fail.\nvar _ IGRC777 = (*dummyImpl)(nil)\n\nfunc TestInterface(t *testing.T) {\n\tvar _ IGRC777 = \u0026dummyImpl{}\n}\n\nfunc (impl *dummyImpl) GetName() string                     { panic(\"not implemented\") }\nfunc (impl *dummyImpl) GetSymbol() string                   { panic(\"not implemented\") }\nfunc (impl *dummyImpl) GetDecimals() int                    { panic(\"not implemented\") }\nfunc (impl *dummyImpl) Granularity() (granularity int64)    { panic(\"not implemented\") }\nfunc (impl *dummyImpl) TotalSupply() (supply int64)         { panic(\"not implemented\") }\nfunc (impl *dummyImpl) BalanceOf(address_XXX address) int64 { panic(\"not implemented\") }\nfunc (impl *dummyImpl) Burn(amount int64, data []byte)      { panic(\"not implemented\") }\nfunc (impl *dummyImpl) AuthorizeOperator(operator address)  { panic(\"not implemented\") }\nfunc (impl *dummyImpl) RevokeOperator(operators address)    { panic(\"not implemented\") }\nfunc (impl *dummyImpl) DefaultOperators() []address         { panic(\"not implemented\") }\nfunc (impl *dummyImpl) Send(recipient address, amount int64, data []byte) {\n\tpanic(\"not implemented\")\n}\n\nfunc (impl *dummyImpl) IsOperatorFor(operator, tokenHolder address) bool {\n\tpanic(\"not implemented\")\n}\n\nfunc (impl *dummyImpl) OperatorSend(sender, recipient address, amount int64, data, operatorData []byte) {\n\tpanic(\"not implemented\")\n}\n\nfunc (impl *dummyImpl) OperatorBurn(account address, amount int64, data, operatorData []byte) {\n\tpanic(\"not implemented\")\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/demo/tokens/grc777\"\ngno = \"0.9\"\n"},{"name":"igrc777.gno","body":"package grc777\n\n// TODO: use big.Int or a custom int64 instead of int64\n\ntype IGRC777 interface {\n\t// Returns the name of the token.\n\tGetName() string\n\n\t// Returns the symbol of the token, usually a shorter version of the\n\t// name.\n\tGetSymbol() string\n\n\t// Returns the decimals places of the token.\n\tGetDecimals() int\n\n\t// Returns the smallest part of the token that is not divisible. This\n\t// means all token operations (creation, movement and destruction) must\n\t// have amounts that are a multiple of this number.\n\t//\n\t// For most token contracts, this value will equal 1.\n\tGranularity() (granularity int64)\n\n\t// Returns the amount of tokens in existence.\n\tTotalSupply() (supply int64)\n\n\t// Returns the amount of tokens owned by an account (`owner`).\n\tBalanceOf(address_XXX address) int64\n\n\t// Moves `amount` tokens from the caller's account to `recipient`.\n\t//\n\t// If send or receive hooks are registered for the caller and `recipient`,\n\t// the corresponding functions will be called with `data` and empty\n\t// `operatorData`. See {IERC777Sender} and {IERC777Recipient}.\n\t//\n\t// Emits a {Sent} event.\n\t//\n\t// Requirements\n\t//\n\t// - the caller must have at least `amount` tokens.\n\t// - `recipient` cannot be the zero address.\n\t// - if `recipient` is a contract, it must implement the {IERC777Recipient}\n\t// interface.\n\tSend(recipient address, amount int64, data []byte)\n\n\t// Destroys `amount` tokens from the caller's account, reducing the\n\t// total supply.\n\t//\n\t// If a send hook is registered for the caller, the corresponding function\n\t// will be called with `data` and empty `operatorData`. See {IERC777Sender}.\n\t//\n\t// Emits a {Burned} event.\n\t//\n\t// Requirements\n\t//\n\t// - the caller must have at least `amount` tokens.\n\tBurn(amount int64, data []byte)\n\n\t// Returns true if an account is an operator of `tokenHolder`.\n\t// Operators can send and burn tokens on behalf of their owners. All\n\t// accounts are their own operator.\n\t//\n\t// See {operatorSend} and {operatorBurn}.\n\tIsOperatorFor(operator, tokenHolder address) bool\n\n\t// Make an account an operator of the caller.\n\t//\n\t// See {isOperatorFor}.\n\t//\n\t// Emits an {AuthorizedOperator} event.\n\t//\n\t// Requirements\n\t//\n\t// - `operator` cannot be calling address.\n\tAuthorizeOperator(operator address)\n\n\t// Revoke an account's operator status for the caller.\n\t//\n\t// See {isOperatorFor} and {defaultOperators}.\n\t//\n\t// Emits a {RevokedOperator} event.\n\t//\n\t// Requirements\n\t//\n\t// - `operator` cannot be calling address.\n\tRevokeOperator(operators address)\n\n\t// Returns the list of default operators. These accounts are operators\n\t// for all token holders, even if {authorizeOperator} was never called on\n\t// them.\n\t//\n\t// This list is immutable, but individual holders may revoke these via\n\t// {revokeOperator}, in which case {isOperatorFor} will return false.\n\tDefaultOperators() []address\n\n\t// Moves `amount` tokens from `sender` to `recipient`. The caller must\n\t// be an operator of `sender`.\n\t//\n\t// If send or receive hooks are registered for `sender` and `recipient`,\n\t// the corresponding functions will be called with `data` and\n\t// `operatorData`. See {IERC777Sender} and {IERC777Recipient}.\n\t//\n\t// Emits a {Sent} event.\n\t//\n\t// Requirements\n\t//\n\t// - `sender` cannot be the zero address.\n\t// - `sender` must have at least `amount` tokens.\n\t// - the caller must be an operator for `sender`.\n\t// - `recipient` cannot be the zero address.\n\t// - if `recipient` is a contract, it must implement the {IERC777Recipient}\n\t// interface.\n\tOperatorSend(sender, recipient address, amount int64, data, operatorData []byte)\n\n\t// Destroys `amount` tokens from `account`, reducing the total supply.\n\t// The caller must be an operator of `account`.\n\t//\n\t// If a send hook is registered for `account`, the corresponding function\n\t// will be called with `data` and `operatorData`. See {IERC777Sender}.\n\t//\n\t// Emits a {Burned} event.\n\t//\n\t// Requirements\n\t//\n\t// - `account` cannot be the zero address.\n\t// - `account` must have at least `amount` tokens.\n\t// - the caller must be an operator for `account`.\n\tOperatorBurn(account address, amount int64, data, operatorData []byte)\n}\n\n// Emitted when `amount` tokens are created by `operator` and assigned to `to`.\n//\n// Note that some additional user `data` and `operatorData` can be logged in the event.\ntype MintedEvent struct {\n\tOperator     address\n\tTo           address\n\tAmount       int64\n\tData         []byte\n\tOperatorData []byte\n}\n\n// Emitted when `operator` destroys `amount` tokens from `account`.\n//\n// Note that some additional user `data` and `operatorData` can be logged in the event.\ntype BurnedEvent struct {\n\tOperator     address\n\tFrom         address\n\tAmount       int64\n\tData         []byte\n\tOperatorData []byte\n}\n\n// Emitted when `operator` is made operator for `tokenHolder`\ntype AuthorizedOperatorEvent struct {\n\tOperator    address\n\tTokenHolder address\n}\n\n// Emitted when `operator` is revoked its operator status for `tokenHolder`.\ntype RevokedOperatorEvent struct {\n\tOperator    address\n\tTokenHolder address\n}\n\ntype SentEvent struct {\n\tOperator     address\n\tFrom         address\n\tTo           address\n\tAmount       int64\n\tData         []byte\n\tOperatorData []byte\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"k9CWWV4kTLDcLJcyW+sXQTMp1lmE4cH1QkV5CPavc9oubl9MP97UB5747fQ0ij/aTY8p/n2fND6igQsP+xg2Dw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"bitset","path":"gno.land/p/jeronimoalbi/bitset","files":[{"name":"README.md","body":"# Bitset Package\n\nPackage implements an arbitrary-size bit set, also known as bit array.\n\nBit sets are useful when you need a compact way to track large sets of boolean flags,\nsuch as permissions, feature toggles, or membership sets, using significantly less\nmemory than a slice of booleans while also supporting fast bulk operations across\nentire sets.\n\n## API\n\n- `New(size uint64) BitSet` - Returns a new BitSet pre-allocated for `size` bits\n- `BitSet.Set(i uint64)` - Turn on the bit at position `i`\n- `BitSet.Clear(i uint64)` - Turn off the bit at position `i`\n- `BitSet.ClearAll()` - Turn off all bits\n- `BitSet.Compact()` - Reclaim memory by removing trailing zero words\n- `BitSet.IsSet(i uint64) bool` - Check whether the bit at position `i` is set\n- `BitSet.Size() int` - Return the total number of bits currently allocated\n- `BitSet.Len() int` - Return the number of set bits\n- `BitSet.And(other BitSet)` - In-place intersection with another set\n- `BitSet.Or(other BitSet)` - In-place union with another set\n- `BitSet.Xor(other BitSet)` - In-place symmetric difference with another set\n- `BitSet.Equal(other BitSet) bool` - Check whether two sets have the same bits set\n- `BitSet.String() string` - Return a binary (MSB-first) representation of the set\n- `BitSet.PaddedString() string` - Return a zero-padded binary (MSB-first) representation of the set\n\n## Usage\n\n[embedmd]:# (filetests/readme_filetest.gno go)\n```go\npackage main\n\nimport \"gno.land/p/jeronimoalbi/bitset\"\n\nconst (\n\tPermRead   = 0\n\tPermWrite  = 1\n\tPermDelete = 2\n\tPermAdmin  = 3\n)\n\nfunc main() {\n\tvar perms bitset.BitSet\n\tperms.Set(PermRead)\n\tperms.Set(PermWrite)\n\n\tprintln(\"Can read:\", perms.IsSet(PermRead))\n\tprintln(\"Can write:\", perms.IsSet(PermWrite))\n\tprintln(\"Can delete:\", perms.IsSet(PermDelete))\n\tprintln(\"Is admin:\", perms.IsSet(PermAdmin))\n\tprintln(\"Permissions set:\", perms.Len())\n}\n\n// Output:\n// Can read: true\n// Can write: true\n// Can delete: false\n// Is admin: false\n// Permissions set: 2\n```\n"},{"name":"bitset.gno","body":"package bitset\n\nimport \"strings\"\n\nconst bitsPerWord = 64\n\n// BitSet implements an arbitrary-size bit array.\ntype BitSet struct {\n\twords []uint64\n}\n\n// New creates a new BitSet pre-allocated to hold at least size bits.\nfunc New(size uint64) BitSet {\n\tif size == 0 {\n\t\treturn BitSet{}\n\t}\n\treturn BitSet{words: make([]uint64, wordIndex(size-1)+1)}\n}\n\n// Set turns on the bit at a given position.\nfunc (b *BitSet) Set(i uint64) {\n\tidx := wordIndex(i)\n\tif idx \u003e= len(b.words) {\n\t\tb.grow(idx + 1)\n\t}\n\tb.words[idx] |= bitMask(i) // Turn bit on\n}\n\n// Clear turns off the bit at a given position.\nfunc (b *BitSet) Clear(i uint64) {\n\tidx := wordIndex(i)\n\tif idx \u003c len(b.words) {\n\t\tb.words[idx] \u0026^= bitMask(i) // Turn bit off\n\t}\n}\n\n// ClearAll turns off all bits.\n// The size of the bitset remains unchanged.\nfunc (b *BitSet) ClearAll() {\n\tfor i := range b.words {\n\t\tb.words[i] = 0\n\t}\n}\n\n// Compact reclaims memory by removing trailing zero words.\nfunc (b *BitSet) Compact() {\n\tend := len(b.words)\n\tfor ; end \u003e 0 \u0026\u0026 b.words[end-1] == 0; end-- {\n\t}\n\twords := make([]uint64, end)\n\tcopy(words, b.words)\n\tb.words = words\n}\n\n// IsSet checks whether the bit at a given position is set.\nfunc (b BitSet) IsSet(i uint64) bool {\n\tidx := wordIndex(i)\n\tif idx \u003e= len(b.words) {\n\t\treturn false // Bit has not been set yet\n\t}\n\treturn b.words[idx]\u0026bitMask(i) != 0\n}\n\n// Size returns the total number of bits (including unset) currently stored.\n// It's the total space allocated within the set in bits.\nfunc (b BitSet) Size() int {\n\treturn len(b.words) * bitsPerWord\n}\n\n// Len returns the number of set bits.\nfunc (b BitSet) Len() int {\n\tvar count int\n\tfor _, w := range b.words {\n\t\tcount += countSetBits(w)\n\t}\n\treturn count\n}\n\n// And performs an in-place AND with other bitset.\n// Bits beyond the other bitset are cleared.\nfunc (b *BitSet) And(other BitSet) {\n\tfor i := range b.words {\n\t\tif i \u003c len(other.words) {\n\t\t\tb.words[i] \u0026= other.words[i]\n\t\t} else {\n\t\t\tb.words[i] = 0\n\t\t}\n\t}\n}\n\n// Or performs an in-place OR (union) with other bitset.\n// Current bitset grows if the other bitset is bigger.\nfunc (b *BitSet) Or(other BitSet) {\n\totherLen := len(other.words)\n\tif otherLen \u003e len(b.words) {\n\t\tb.grow(otherLen)\n\t}\n\n\tfor i := range other.words {\n\t\tb.words[i] |= other.words[i]\n\t}\n}\n\n// Xor performs an in-place XOR with other.\n// Current bitset grows if the other bitset is bigger.\nfunc (b *BitSet) Xor(other BitSet) {\n\totherLen := len(other.words)\n\tif otherLen \u003e len(b.words) {\n\t\tb.grow(otherLen)\n\t}\n\n\tfor i := range other.words {\n\t\tb.words[i] ^= other.words[i]\n\t}\n}\n\n// Equal checks whether other bitset have exactly the same bits set.\nfunc (b BitSet) Equal(other BitSet) bool {\n\tshort, long := b.words, other.words\n\tif len(short) \u003e len(long) {\n\t\tshort, long = long, short\n\t}\n\n\tfor i := range short {\n\t\tif short[i] != long[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\n\t// Any trailing words in the longer set must be zero\n\tfor i := len(short); i \u003c len(long); i++ {\n\t\tif long[i] != 0 {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// PaddedString returns a binary representation of the bitset with zero padding.\n// Representation uses MSB-first binary representation.\nfunc (b BitSet) PaddedString() string {\n\twordCount := len(b.words)\n\tif wordCount == 0 {\n\t\treturn \"\"\n\t}\n\n\tbuf := make([]byte, wordCount*bitsPerWord)\n\tfor i, w := range b.words {\n\t\tbase := (wordCount - 1 - i) * bitsPerWord\n\t\tfor j := 0; j \u003c bitsPerWord; j++ {\n\t\t\tif w\u003e\u003euint64(bitsPerWord-1-j)\u00261 == 1 {\n\t\t\t\tbuf[base+j] = '1'\n\t\t\t} else {\n\t\t\t\tbuf[base+j] = '0'\n\t\t\t}\n\t\t}\n\t}\n\treturn string(buf)\n}\n\n// String returns a binary representation of the bitset.\n// Representation uses MSB-first binary representation.\nfunc (b BitSet) String() string {\n\ts := strings.TrimLeft(b.PaddedString(), \"0\")\n\tif s == \"\" \u0026\u0026 len(b.words) \u003e 0 {\n\t\treturn \"0\"\n\t}\n\treturn s\n}\n\nfunc (b *BitSet) grow(length int) {\n\twords := make([]uint64, length)\n\tcopy(words, b.words)\n\tb.words = words\n}\n\nfunc wordIndex(i uint64) int {\n\treturn int(i / bitsPerWord)\n}\n\nfunc bitMask(i uint64) uint64 {\n\treturn 1 \u003c\u003c (i % bitsPerWord)\n}\n\nfunc countSetBits(x uint64) int {\n\t// Count the number of set bits using Kernighan's algorithm\n\tvar count int\n\tfor x != 0 {\n\t\tx \u0026= x - 1\n\t\tcount++\n\t}\n\treturn count\n}\n"},{"name":"bitset_test.gno","body":"package bitset_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/bitset\"\n)\n\nfunc TestBitSetSet(t *testing.T) {\n\tcases := []struct {\n\t\tname string\n\t\tbits []uint64\n\t\twant string\n\t}{\n\t\t{\n\t\t\tname: \"single bit 0\",\n\t\t\tbits: []uint64{0},\n\t\t\twant: \"1\",\n\t\t},\n\t\t{\n\t\t\tname: \"single bit 5\",\n\t\t\tbits: []uint64{5},\n\t\t\twant: \"100000\",\n\t\t},\n\t\t{\n\t\t\tname: \"multiple bits in same word\",\n\t\t\tbits: []uint64{0, 3, 7},\n\t\t\twant: \"10001001\",\n\t\t},\n\t\t{\n\t\t\tname: \"bit at word boundary\",\n\t\t\tbits: []uint64{63},\n\t\t\twant: \"1000000000000000000000000000000000000000000000000000000000000000\",\n\t\t},\n\t\t{\n\t\t\tname: \"bit across word boundary\",\n\t\t\tbits: []uint64{63, 64},\n\t\t\twant: \"11000000000000000000000000000000000000000000000000000000000000000\",\n\t\t},\n\t\t{\n\t\t\tname: \"set same bit twice\",\n\t\t\tbits: []uint64{2, 2},\n\t\t\twant: \"100\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar b bitset.BitSet\n\n\t\t\t// Act\n\t\t\tfor _, v := range tc.bits {\n\t\t\t\tb.Set(v)\n\t\t\t}\n\n\t\t\t// Assert\n\t\t\tif got := b.String(); got != tc.want {\n\t\t\t\tt.Errorf(\"expected %s, got %s\", tc.want, got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetIsSet(t *testing.T) {\n\tvar (\n\t\tb      bitset.BitSet\n\t\tvalues = []uint64{0, 5, 63, 64, 200}\n\t)\n\n\tfor _, v := range values {\n\t\tb.Set(v)\n\t}\n\n\tfor _, v := range values {\n\t\tif !b.IsSet(v) {\n\t\t\tt.Errorf(\"bit %d should be set\", v)\n\t\t}\n\t}\n\n\tif b.IsSet(999) {\n\t\tt.Error(\"bit 999 should not be set\")\n\t}\n}\n\nfunc TestBitSetClear(t *testing.T) {\n\tvar b bitset.BitSet\n\tb.Set(10)\n\tb.Clear(10)\n\n\tif b.IsSet(10) {\n\t\tt.Error(\"bit 10 should be cleared\")\n\t}\n\n\t// Clearing a bit beyond the slice should not panic\n\tb.Clear(999)\n}\n\nfunc TestBitSetClearAll(t *testing.T) {\n\t// Arrange\n\tvar b bitset.BitSet\n\tb.Set(1)\n\tb.Set(64)\n\tb.Set(200)\n\n\t// Act\n\tb.ClearAll()\n\n\t// Assert\n\tif b.Len() != 0 {\n\t\tt.Errorf(\"expected len 0 after ClearAll, got %d\", b.Len())\n\t}\n\n\tif b.IsSet(1) || b.IsSet(64) || b.IsSet(200) {\n\t\tt.Error(\"no bits should be set after ClearAll\")\n\t}\n}\n\nfunc TestBitSetCompact(t *testing.T) {\n\tcases := []struct {\n\t\tname     string\n\t\tsetup    func() bitset.BitSet\n\t\twantSize int\n\t}{\n\t\t{\n\t\t\tname: \"clear all\",\n\t\t\tsetup: func() bitset.BitSet {\n\t\t\t\tvar b bitset.BitSet\n\t\t\t\tb.Set(1)\n\t\t\t\tb.Set(64)\n\t\t\t\tb.Set(200)\n\t\t\t\tb.ClearAll()\n\t\t\t\treturn b\n\t\t\t},\n\t\t\twantSize: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"trailing zeros\",\n\t\t\tsetup: func() bitset.BitSet {\n\t\t\t\tvar a, other bitset.BitSet\n\t\t\t\ta.Set(1)\n\t\t\t\ta.Set(128)\n\t\t\t\tother.Set(1)\n\t\t\t\ta.And(other)\n\t\t\t\treturn a\n\t\t\t},\n\t\t\twantSize: 64,\n\t\t},\n\t\t{\n\t\t\tname: \"no trailing zeros\",\n\t\t\tsetup: func() bitset.BitSet {\n\t\t\t\tvar b bitset.BitSet\n\t\t\t\tb.Set(1)\n\t\t\t\tb.Set(64)\n\t\t\t\treturn b\n\t\t\t},\n\t\t\twantSize: 128,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tb := tc.setup()\n\n\t\t\t// Act\n\t\t\tb.Compact()\n\n\t\t\t// Assert\n\t\t\tif got := b.Size(); got != tc.wantSize {\n\t\t\t\tt.Errorf(\"expected size %d after Compact, got %d\", tc.wantSize, got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetSize(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tsetup func() bitset.BitSet\n\t\twant  int\n\t}{\n\t\t{\n\t\t\tname:  \"empty\",\n\t\t\tsetup: func() bitset.BitSet { return bitset.BitSet{} },\n\t\t\twant:  0,\n\t\t},\n\t\t{\n\t\t\tname:  \"new 128\",\n\t\t\tsetup: func() bitset.BitSet { return bitset.New(128) },\n\t\t\twant:  128,\n\t\t},\n\t\t{\n\t\t\tname: \"set bit 0\",\n\t\t\tsetup: func() bitset.BitSet {\n\t\t\t\tvar b bitset.BitSet\n\t\t\t\tb.Set(0)\n\t\t\t\treturn b\n\t\t\t},\n\t\t\twant: 64,\n\t\t},\n\t\t{\n\t\t\tname: \"set bit 200\",\n\t\t\tsetup: func() bitset.BitSet {\n\t\t\t\tvar b bitset.BitSet\n\t\t\t\tb.Set(200)\n\t\t\t\treturn b\n\t\t\t},\n\t\t\twant: 256,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tb := tc.setup()\n\n\t\t\t// Act\n\t\t\tgot := b.Size()\n\n\t\t\t// Assert\n\t\t\tif got != tc.want {\n\t\t\t\tt.Errorf(\"expected size %d, got %d\", tc.want, got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetLen(t *testing.T) {\n\tvar b bitset.BitSet\n\tif b.Len() != 0 {\n\t\tt.Error(\"empty bitset should have len 0\")\n\t}\n\n\tb.Set(0)\n\tb.Set(1)\n\tb.Set(100)\n\tif b.Len() != 3 {\n\t\tt.Errorf(\"expected len 3, got %d\", b.Len())\n\t}\n}\n\nfunc TestBitSetNew(t *testing.T) {\n\tb := bitset.New(0)\n\tif b.Size() != 0 {\n\t\tt.Errorf(\"expected size 0, got %d\", b.Size())\n\t}\n\n\tb = bitset.New(128)\n\tif b.Size() != 128 {\n\t\tt.Errorf(\"expected size 128, got %d\", b.Size())\n\t}\n}\n\nfunc TestBitSetOr(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tcurrent []uint64\n\t\tother   []uint64\n\t\twant    string\n\t}{\n\t\t{\n\t\t\tname:    \"empty sets\",\n\t\t\tcurrent: []uint64{},\n\t\t\tother:   []uint64{},\n\t\t\twant:    \"\",\n\t\t},\n\t\t{\n\t\t\tname:    \"equal sets\",\n\t\t\tcurrent: []uint64{1, 4, 8},\n\t\t\tother:   []uint64{1, 4, 8},\n\t\t\twant:    \"100010010\",\n\t\t},\n\t\t{\n\t\t\tname:    \"different sets\",\n\t\t\tcurrent: []uint64{1, 4, 8},\n\t\t\tother:   []uint64{2, 3, 5},\n\t\t\twant:    \"100111110\",\n\t\t},\n\t\t{\n\t\t\tname:    \"current smaller than other\",\n\t\t\tcurrent: []uint64{1, 4},\n\t\t\tother:   []uint64{2, 3, 5, 8},\n\t\t\twant:    \"100111110\",\n\t\t},\n\t\t{\n\t\t\tname:    \"current bigger than other\",\n\t\t\tcurrent: []uint64{2, 3, 5, 8},\n\t\t\tother:   []uint64{1, 4},\n\t\t\twant:    \"100111110\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar a, b bitset.BitSet\n\n\t\t\tfor _, v := range tc.current {\n\t\t\t\ta.Set(v)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.other {\n\t\t\t\tb.Set(v)\n\t\t\t}\n\n\t\t\t// Act\n\t\t\ta.Or(b)\n\n\t\t\t// Assert\n\t\t\tif got := a.String(); got != tc.want {\n\t\t\t\tt.Errorf(\"expected %s, got %s\", tc.want, got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetAnd(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tcurrent []uint64\n\t\tother   []uint64\n\t\twant    string\n\t}{\n\t\t{\n\t\t\tname:    \"empty sets\",\n\t\t\tcurrent: []uint64{},\n\t\t\tother:   []uint64{},\n\t\t\twant:    \"\",\n\t\t},\n\t\t{\n\t\t\tname:    \"equal sets\",\n\t\t\tcurrent: []uint64{1, 4, 8},\n\t\t\tother:   []uint64{1, 4, 8},\n\t\t\twant:    \"100010010\",\n\t\t},\n\t\t{\n\t\t\tname:    \"different sets\",\n\t\t\tcurrent: []uint64{1, 4, 8},\n\t\t\tother:   []uint64{2, 3, 5},\n\t\t\twant:    \"0\",\n\t\t},\n\t\t{\n\t\t\tname:    \"current smaller than other\",\n\t\t\tcurrent: []uint64{1, 4},\n\t\t\tother:   []uint64{1, 4, 5, 8},\n\t\t\twant:    \"10010\",\n\t\t},\n\t\t{\n\t\t\tname:    \"current bigger than other\",\n\t\t\tcurrent: []uint64{0, 3, 5, 8},\n\t\t\tother:   []uint64{0, 3},\n\t\t\twant:    \"1001\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar a, b bitset.BitSet\n\n\t\t\tfor _, v := range tc.current {\n\t\t\t\ta.Set(v)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.other {\n\t\t\t\tb.Set(v)\n\t\t\t}\n\n\t\t\t// Act\n\t\t\ta.And(b)\n\n\t\t\t// Assert\n\t\t\tif got := a.String(); got != tc.want {\n\t\t\t\tt.Errorf(\"expected %s, got %s\", tc.want, got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetXor(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tcurrent []uint64\n\t\tother   []uint64\n\t\twant    string\n\t}{\n\t\t{\n\t\t\tname:    \"empty sets\",\n\t\t\tcurrent: []uint64{},\n\t\t\tother:   []uint64{},\n\t\t\twant:    \"\",\n\t\t},\n\t\t{\n\t\t\tname:    \"equal sets\",\n\t\t\tcurrent: []uint64{1, 4, 8},\n\t\t\tother:   []uint64{1, 4, 8},\n\t\t\twant:    \"0\",\n\t\t},\n\t\t{\n\t\t\tname:    \"different sets\",\n\t\t\tcurrent: []uint64{1, 3, 8},\n\t\t\tother:   []uint64{2, 3, 5},\n\t\t\twant:    \"100100110\",\n\t\t},\n\t\t{\n\t\t\tname:    \"current smaller than other\",\n\t\t\tcurrent: []uint64{1, 3, 4},\n\t\t\tother:   []uint64{2, 3, 5, 8},\n\t\t\twant:    \"100110110\",\n\t\t},\n\t\t{\n\t\t\tname:    \"current bigger than other\",\n\t\t\tcurrent: []uint64{2, 3, 5, 8},\n\t\t\tother:   []uint64{1, 3},\n\t\t\twant:    \"100100110\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar a, b bitset.BitSet\n\n\t\t\tfor _, v := range tc.current {\n\t\t\t\ta.Set(v)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.other {\n\t\t\t\tb.Set(v)\n\t\t\t}\n\n\t\t\t// Act\n\t\t\ta.Xor(b)\n\n\t\t\t// Assert\n\t\t\tif got := a.String(); got != tc.want {\n\t\t\t\tt.Errorf(\"expected %s, got %s\", tc.want, got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetEqual(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tsetup func() (a, b bitset.BitSet)\n\t\twant  bool\n\t}{\n\t\t{\n\t\t\tname:  \"empty\",\n\t\t\tsetup: func() (a, b bitset.BitSet) { return a, b },\n\t\t\twant:  true,\n\t\t},\n\t\t{\n\t\t\tname: \"equal\",\n\t\t\tsetup: func() (a, b bitset.BitSet) {\n\t\t\t\ta.Set(1)\n\t\t\t\ta.Set(100)\n\t\t\t\tb.Set(1)\n\t\t\t\tb.Set(100)\n\t\t\t\treturn a, b\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname: \"equal with different sizes\",\n\t\t\tsetup: func() (a, b bitset.BitSet) {\n\t\t\t\tb = bitset.New(256)\n\t\t\t\tb.Set(5)\n\t\t\t\ta.Set(5)\n\t\t\t\treturn a, b\n\t\t\t},\n\t\t\twant: true,\n\t\t},\n\t\t{\n\t\t\tname: \"not equal\",\n\t\t\tsetup: func() (a, b bitset.BitSet) {\n\t\t\t\ta.Set(1)\n\t\t\t\tb.Set(1)\n\t\t\t\tb.Set(50)\n\t\t\t\treturn a, b\n\t\t\t},\n\t\t\twant: false,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\ta, b := tc.setup()\n\n\t\t\t// Act\n\t\t\tgot := a.Equal(b)\n\n\t\t\t// Assert\n\t\t\tif tc.want == got {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif tc.want {\n\t\t\t\tt.Error(\"bitsets should be equal\")\n\t\t\t} else {\n\t\t\t\tt.Error(\"bitsets should not be equal\")\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetString(t *testing.T) {\n\tcases := []struct {\n\t\tname   string\n\t\tvalues []uint64\n\t\twant   string\n\t}{\n\t\t{\"empty\", nil, \"\"},\n\t\t{\"single bit\", []uint64{0}, \"1\"},\n\t\t{\"max word value\", []uint64{63}, \"1000000000000000000000000000000000000000000000000000000000000000\"},\n\t\t{\"multiple sequential bits\", []uint64{0, 1, 2, 3}, \"1111\"},\n\t\t{\"multiple non sequential bits\", []uint64{0, 2, 4}, \"10101\"},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar b bitset.BitSet\n\t\t\tfor _, v := range tc.values {\n\t\t\t\tb.Set(v)\n\t\t\t}\n\n\t\t\t// Act\n\t\t\tgot := b.String()\n\n\t\t\t// Assert\n\t\t\tif got != tc.want {\n\t\t\t\tt.Errorf(\"expected %q, got %q\", tc.want, got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBitSetPaddedString(t *testing.T) {\n\tcases := []struct {\n\t\tname   string\n\t\tvalues []uint64\n\t\twant   string\n\t}{\n\t\t{\"empty\", nil, \"\"},\n\t\t{\"single bit\", []uint64{0}, \"0000000000000000000000000000000000000000000000000000000000000001\"},\n\t\t{\"max word value\", []uint64{63}, \"1000000000000000000000000000000000000000000000000000000000000000\"},\n\t\t{\"multiple sequential bits\", []uint64{0, 1, 2, 3}, \"0000000000000000000000000000000000000000000000000000000000001111\"},\n\t\t{\"multiple non sequential bits\", []uint64{0, 2, 4}, \"0000000000000000000000000000000000000000000000000000000000010101\"},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar b bitset.BitSet\n\t\t\tfor _, v := range tc.values {\n\t\t\t\tb.Set(v)\n\t\t\t}\n\n\t\t\t// Act\n\t\t\tgot := b.PaddedString()\n\n\t\t\t// Assert\n\t\t\tif got != tc.want {\n\t\t\t\tt.Errorf(\"expected %q, got %q\", tc.want, got)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/bitset\"\ngno = \"0.9\"\n"},{"name":"readme_filetest.gno","body":"package main\n\nimport \"gno.land/p/jeronimoalbi/bitset\"\n\nconst (\n\tPermRead   = 0\n\tPermWrite  = 1\n\tPermDelete = 2\n\tPermAdmin  = 3\n)\n\nfunc main() {\n\tvar perms bitset.BitSet\n\tperms.Set(PermRead)\n\tperms.Set(PermWrite)\n\n\tprintln(\"Can read:\", perms.IsSet(PermRead))\n\tprintln(\"Can write:\", perms.IsSet(PermWrite))\n\tprintln(\"Can delete:\", perms.IsSet(PermDelete))\n\tprintln(\"Is admin:\", perms.IsSet(PermAdmin))\n\tprintln(\"Permissions set:\", perms.Len())\n}\n\n// Output:\n// Can read: true\n// Can write: true\n// Can delete: false\n// Is admin: false\n// Permissions set: 2\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ALSNHnG6jy8T9xGFYO2yJhHHkIG7MJls/xcSDV36Q9Prt9slNFXfRn/RZXFmO0EzZSoNGKo+JX53hxjnXeJRCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"datasource","path":"gno.land/p/jeronimoalbi/datasource","files":[{"name":"datasource.gno","body":"// Package datasource defines generic interfaces for datasources.\n//\n// Datasources contain a set of records which can optionally be\n// taggable. Tags can optionally be used to filter records by taxonomy.\n//\n// Datasources can help in cases where the data sent during\n// communication between different realms needs to be generic\n// to avoid direct dependencies.\npackage datasource\n\nimport \"errors\"\n\n// ErrInvalidRecord indicates that a datasource contains invalid records.\nvar ErrInvalidRecord = errors.New(\"datasource records is not valid\")\n\ntype (\n\t// Fields defines an interface for read-only fields.\n\tFields interface {\n\t\t// Has checks whether a field exists.\n\t\tHas(name string) bool\n\n\t\t// Get retrieves the value associated with the given field.\n\t\t// Returns nil if the field does not exist.\n\t\tGet(name string) any\n\t}\n\n\t// Record defines a datasource record.\n\tRecord interface {\n\t\t// ID returns the unique record's identifier.\n\t\tID() string\n\n\t\t// String returns a string representation of the record.\n\t\tString() string\n\n\t\t// Fields returns record fields and values.\n\t\tFields() (Fields, error)\n\t}\n\n\t// TaggableRecord defines a datasource record that supports tags.\n\t// Tags can be used to build a taxonomy to filter records by category.\n\tTaggableRecord interface {\n\t\t// Tags returns a list of tags for the record.\n\t\tTags() []string\n\t}\n\n\t// ContentRecord defines a datasource record that can return content.\n\tContentRecord interface {\n\t\t// Content returns the record content.\n\t\tContent() (string, error)\n\t}\n\n\t// Iterator defines an iterator of datasource records.\n\tIterator interface {\n\t\t// Next returns true when a new record is available.\n\t\tNext() bool\n\n\t\t// Err returns any error raised when reading records.\n\t\tErr() error\n\n\t\t// Record returns the current record.\n\t\tRecord() Record\n\t}\n\n\t// Datasource defines a generic datasource.\n\tDatasource interface {\n\t\t// Records returns a new datasource records iterator.\n\t\tRecords(Query) Iterator\n\n\t\t// Size returns the total number of records in the datasource.\n\t\t// When -1 is returned it means datasource doesn't support size.\n\t\tSize() int\n\n\t\t// Record returns a single datasource record.\n\t\tRecord(id string) (Record, error)\n\t}\n)\n\n// NewIterator returns a new record iterator for a datasource query.\nfunc NewIterator(ds Datasource, options ...QueryOption) Iterator {\n\treturn ds.Records(NewQuery(options...))\n}\n\n// QueryRecords return a slice of records for a datasource query.\nfunc QueryRecords(ds Datasource, options ...QueryOption) ([]Record, error) {\n\tvar (\n\t\trecords []Record\n\t\tquery   = NewQuery(options...)\n\t\titer    = ds.Records(query)\n\t)\n\n\tfor i := 0; i \u003c query.Count \u0026\u0026 iter.Next(); i++ {\n\t\tr := iter.Record()\n\t\tif r == nil {\n\t\t\treturn nil, ErrInvalidRecord\n\t\t}\n\n\t\trecords = append(records, r)\n\t}\n\n\tif err := iter.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\treturn records, nil\n}\n"},{"name":"datasource_test.gno","body":"package datasource\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestNewIterator(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\trecords []Record\n\t\terr     error\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\trecords: []Record{\n\t\t\t\ttestRecord{id: \"1\"},\n\t\t\t\ttestRecord{id: \"2\"},\n\t\t\t\ttestRecord{id: \"3\"},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"error\",\n\t\t\terr:  errors.New(\"test\"),\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tds := testDatasource{\n\t\t\t\trecords: tc.records,\n\t\t\t\terr:     tc.err,\n\t\t\t}\n\n\t\t\t// Act\n\t\t\titer := NewIterator(ds)\n\n\t\t\t// Assert\n\t\t\tif tc.err != nil {\n\t\t\t\tuassert.ErrorIs(t, tc.err, iter.Err())\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tuassert.NoError(t, iter.Err())\n\n\t\t\tfor i := 0; iter.Next(); i++ {\n\t\t\t\tr := iter.Record()\n\t\t\t\turequire.NotEqual(t, nil, r, \"valid record\")\n\t\t\t\turequire.True(t, i \u003c len(tc.records), \"iteration count\")\n\t\t\t\tuassert.Equal(t, tc.records[i].ID(), r.ID())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestQueryRecords(t *testing.T) {\n\tcases := []struct {\n\t\tname        string\n\t\trecords     []Record\n\t\trecordCount int\n\t\toptions     []QueryOption\n\t\terr         error\n\t}{\n\t\t{\n\t\t\tname: \"ok\",\n\t\t\trecords: []Record{\n\t\t\t\ttestRecord{id: \"1\"},\n\t\t\t\ttestRecord{id: \"2\"},\n\t\t\t\ttestRecord{id: \"3\"},\n\t\t\t},\n\t\t\trecordCount: 3,\n\t\t},\n\t\t{\n\t\t\tname:    \"with count\",\n\t\t\toptions: []QueryOption{WithCount(2)},\n\t\t\trecords: []Record{\n\t\t\t\ttestRecord{id: \"1\"},\n\t\t\t\ttestRecord{id: \"2\"},\n\t\t\t\ttestRecord{id: \"3\"},\n\t\t\t},\n\t\t\trecordCount: 2,\n\t\t},\n\t\t{\n\t\t\tname: \"invalid record\",\n\t\t\trecords: []Record{\n\t\t\t\ttestRecord{id: \"1\"},\n\t\t\t\tnil,\n\t\t\t\ttestRecord{id: \"3\"},\n\t\t\t},\n\t\t\terr: ErrInvalidRecord,\n\t\t},\n\t\t{\n\t\t\tname: \"iterator error\",\n\t\t\trecords: []Record{\n\t\t\t\ttestRecord{id: \"1\"},\n\t\t\t\ttestRecord{id: \"3\"},\n\t\t\t},\n\t\t\terr: errors.New(\"test\"),\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tds := testDatasource{\n\t\t\t\trecords: tc.records,\n\t\t\t\terr:     tc.err,\n\t\t\t}\n\n\t\t\t// Act\n\t\t\trecords, err := QueryRecords(ds, tc.options...)\n\n\t\t\t// Assert\n\t\t\tif tc.err != nil {\n\t\t\t\tuassert.ErrorIs(t, tc.err, err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tuassert.NoError(t, err)\n\n\t\t\turequire.Equal(t, tc.recordCount, len(records), \"record count\")\n\t\t\tfor i, r := range records {\n\t\t\t\turequire.NotEqual(t, nil, r, \"valid record\")\n\t\t\t\tuassert.Equal(t, tc.records[i].ID(), r.ID())\n\t\t\t}\n\t\t})\n\t}\n}\n\ntype testDatasource struct {\n\trecords []Record\n\terr     error\n}\n\nfunc (testDatasource) Size() int                     { return -1 }\nfunc (testDatasource) Record(string) (Record, error) { return nil, nil }\nfunc (ds testDatasource) Records(Query) Iterator     { return \u0026testIter{records: ds.records, err: ds.err} }\n\ntype testRecord struct {\n\tid     string\n\tfields Fields\n\terr    error\n}\n\nfunc (r testRecord) ID() string              { return r.id }\nfunc (r testRecord) String() string          { return \"str\" + r.id }\nfunc (r testRecord) Fields() (Fields, error) { return r.fields, r.err }\n\ntype testIter struct {\n\tindex   int\n\trecords []Record\n\tcurrent Record\n\terr     error\n}\n\nfunc (it testIter) Err() error     { return it.err }\nfunc (it testIter) Record() Record { return it.current }\n\nfunc (it *testIter) Next() bool {\n\tcount := len(it.records)\n\tif it.err != nil || count == 0 || it.index \u003e= count {\n\t\treturn false\n\t}\n\tit.current = it.records[it.index]\n\tit.index++\n\treturn true\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/datasource\"\ngno = \"0.9\"\n"},{"name":"query.gno","body":"package datasource\n\nimport \"gno.land/p/nt/avl/v0\"\n\n// DefaultQueryRecords defines the default number of records returned by queries.\nconst DefaultQueryRecords = 50\n\ntype (\n\t// QueryOption configures datasource queries.\n\tQueryOption func(*Query)\n\n\t// Query contains datasource query options.\n\tQuery struct {\n\t\t// Offset of the first record to return during iteration.\n\t\tOffset int\n\n\t\t// Count contains the number to records that query should return.\n\t\tCount int\n\n\t\t// Tag contains a tag to use as filter for the records.\n\t\tTag string\n\n\t\t// Filters contains optional query filters by field value.\n\t\tFilters avl.Tree\n\t}\n)\n\n// WithOffset configures query to return records starting from an offset.\nfunc WithOffset(offset int) QueryOption {\n\treturn func(q *Query) {\n\t\tq.Offset = offset\n\t}\n}\n\n// WithCount configures the number of records that query returns.\nfunc WithCount(count int) QueryOption {\n\treturn func(q *Query) {\n\t\tif count \u003c 1 {\n\t\t\tcount = DefaultQueryRecords\n\t\t}\n\t\tq.Count = count\n\t}\n}\n\n// ByTag configures query to filter by tag.\nfunc ByTag(tag string) QueryOption {\n\treturn func(q *Query) {\n\t\tq.Tag = tag\n\t}\n}\n\n// WithFilter assigns a new filter argument to a query.\n// This option can be used multiple times if more than one\n// filter has to be given to the query.\nfunc WithFilter(field string, value any) QueryOption {\n\treturn func(q *Query) {\n\t\tq.Filters.Set(field, value)\n\t}\n}\n\n// NewQuery creates a new datasource query.\nfunc NewQuery(options ...QueryOption) Query {\n\t// Construct a fresh Query in the caller's realm; copying from a\n\t// package-level default would propagate the /p/-readonly taint\n\t// and break the apply(\u0026q) options below.\n\tq := Query{Count: DefaultQueryRecords}\n\tfor _, apply := range options {\n\t\tapply(\u0026q)\n\t}\n\treturn q\n}\n"},{"name":"query_test.gno","body":"package datasource\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNewQuery(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\toptions []QueryOption\n\t\tsetup   func() Query\n\t}{\n\t\t{\n\t\t\tname: \"default\",\n\t\t\tsetup: func() Query {\n\t\t\t\treturn Query{Count: DefaultQueryRecords}\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with offset\",\n\t\t\toptions: []QueryOption{WithOffset(100)},\n\t\t\tsetup: func() Query {\n\t\t\t\treturn Query{\n\t\t\t\t\tOffset: 100,\n\t\t\t\t\tCount:  DefaultQueryRecords,\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with count\",\n\t\t\toptions: []QueryOption{WithCount(10)},\n\t\t\tsetup: func() Query {\n\t\t\t\treturn Query{Count: 10}\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with invalid count\",\n\t\t\toptions: []QueryOption{WithCount(0)},\n\t\t\tsetup: func() Query {\n\t\t\t\treturn Query{Count: DefaultQueryRecords}\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"by tag\",\n\t\t\toptions: []QueryOption{ByTag(\"foo\")},\n\t\t\tsetup: func() Query {\n\t\t\t\treturn Query{\n\t\t\t\t\tTag:   \"foo\",\n\t\t\t\t\tCount: DefaultQueryRecords,\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with filter\",\n\t\t\toptions: []QueryOption{WithFilter(\"foo\", 42)},\n\t\t\tsetup: func() Query {\n\t\t\t\tq := Query{Count: DefaultQueryRecords}\n\t\t\t\tq.Filters.Set(\"foo\", 42)\n\t\t\t\treturn q\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"with multiple filters\",\n\t\t\toptions: []QueryOption{\n\t\t\t\tWithFilter(\"foo\", 42),\n\t\t\t\tWithFilter(\"bar\", \"baz\"),\n\t\t\t},\n\t\t\tsetup: func() Query {\n\t\t\t\tq := Query{Count: DefaultQueryRecords}\n\t\t\t\tq.Filters.Set(\"foo\", 42)\n\t\t\t\tq.Filters.Set(\"bar\", \"baz\")\n\t\t\t\treturn q\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\twant := tc.setup()\n\n\t\t\t// Act\n\t\t\tq := NewQuery(tc.options...)\n\n\t\t\t// Assert\n\t\t\tuassert.Equal(t, want.Offset, q.Offset)\n\t\t\tuassert.Equal(t, want.Count, q.Count)\n\t\t\tuassert.Equal(t, want.Tag, q.Tag)\n\t\t\tuassert.Equal(t, want.Filters.Size(), q.Filters.Size())\n\n\t\t\twant.Filters.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\t\t\tgot := q.Filters.Get(k)\n\t\t\t\tuassert.NotNil(t, got)\n\t\t\t\tif got != nil {\n\t\t\t\t\tuassert.Equal(t, fmt.Sprint(v), fmt.Sprint(got))\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"xCDtowVud0MYleUVdP3yFoGUrgcNEzv598stMHZR8JGbhP0DbHfHPoTASw/rTO+g1KBtgRucxjUqmDy7OCSEBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"collection","path":"gno.land/p/moul/collection","files":[{"name":"collection.gno","body":"// Package collection provides a generic collection implementation with support for\n// multiple indexes, including unique indexes and case-insensitive indexes.\n// It is designed to be used with any type and allows efficient lookups using\n// different fields or computed values.\n//\n// Example usage:\n//\n//\t// Define a data type\n//\ttype User struct {\n//\t    Name     string\n//\t    Email    string\n//\t    Age      int\n//\t    Username string\n//\t    Tags     []string\n//\t}\n//\n//\t// Create a new collection\n//\tc := collection.New()\n//\n//\t// Add indexes with different options\n//\tc.AddIndex(\"name\", func(v any) string {\n//\t    return v.(*User).Name\n//\t}, UniqueIndex)\n//\n//\tc.AddIndex(\"email\", func(v any) string {\n//\t    return v.(*User).Email\n//\t}, UniqueIndex|CaseInsensitiveIndex)\n//\n//\tc.AddIndex(\"age\", func(v any) string {\n//\t    return strconv.Itoa(v.(*User).Age)\n//\t}, DefaultIndex)  // Non-unique index\n//\n//\tc.AddIndex(\"username\", func(v any) string {\n//\t    return v.(*User).Username\n//\t}, UniqueIndex|SparseIndex)  // Allow empty usernames\n//\n//\t// For tags, we index all tags for the user\n//\tc.AddIndex(\"tag\", func(v any) []string {\n//\t    return v.(*User).Tags\n//\t}, DefaultIndex)  // Non-unique to allow multiple users with same tag\n//\n//\t// Store an object\n//\tid := c.Set(\u0026User{\n//\t    Name:  \"Alice\",\n//\t    Email: \"alice@example.com\",\n//\t    Age:   30,\n//\t    Tags:  []string{\"admin\", \"moderator\"},  // User can have multiple tags\n//\t})\n//\n//\t// Retrieve by any index\n//\tentry := c.GetFirst(\"email\", \"alice@example.com\")\n//\tadminUsers := c.GetAll(\"tag\", \"admin\")      // Find all users with admin tag\n//\tmodUsers := c.GetAll(\"tag\", \"moderator\")    // Find all users with moderator tag\n//\n// Index options can be combined using the bitwise OR operator.\n// Available options:\n//   - DefaultIndex: Regular index with no special behavior\n//   - UniqueIndex: Ensures values are unique within the index\n//   - CaseInsensitiveIndex: Makes string comparisons case-insensitive\n//   - SparseIndex: Skips indexing empty values (nil or empty string)\n//\n// Example: UniqueIndex|CaseInsensitiveIndex for a case-insensitive unique index\npackage collection\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// New creates a new Collection instance with an initialized ID index.\n// The ID index is a special unique index that is always present and\n// serves as the primary key for all objects in the collection.\nfunc New() *Collection {\n\tc := \u0026Collection{\n\t\tindexes: make(map[string]*Index),\n\t\tidGen:   seqid.ID(0),\n\t}\n\t// Initialize _id index\n\tc.indexes[IDIndex] = \u0026Index{\n\t\toptions: UniqueIndex,\n\t\ttree:    avl.NewTree(),\n\t}\n\treturn c\n}\n\n// Collection represents a collection of objects with multiple indexes\ntype Collection struct {\n\tindexes map[string]*Index\n\tidGen   seqid.ID\n}\n\nconst (\n\t// IDIndex is the reserved name for the primary key index\n\tIDIndex = \"_id\"\n)\n\n// IndexOption represents configuration options for an index using bit flags\ntype IndexOption uint64\n\nconst (\n\t// DefaultIndex is a basic index with no special options\n\tDefaultIndex IndexOption = 0\n\n\t// UniqueIndex ensures no duplicate values are allowed\n\tUniqueIndex IndexOption = 1 \u003c\u003c iota\n\n\t// CaseInsensitiveIndex automatically converts string values to lowercase\n\tCaseInsensitiveIndex\n\n\t// SparseIndex only indexes non-empty values\n\tSparseIndex\n)\n\n// Index represents an index with its configuration and data.\n// The index function can return either:\n//   - string: for single-value indexes\n//   - []string: for multi-value indexes where one object can be indexed under multiple keys\n//\n// The backing tree stores either a single ID or []string for multiple IDs per key.\ntype Index struct {\n\tfn      any\n\toptions IndexOption\n\ttree    avl.ITree\n}\n\n// AddIndex adds a new index to the collection with the specified options\n//\n// Parameters:\n//   - name: the unique name of the index (e.g., \"tags\")\n//   - indexFn: a function that extracts either a string or []string from an object\n//   - options: bit flags for index configuration (e.g., UniqueIndex)\nfunc (c *Collection) AddIndex(name string, indexFn any, options IndexOption) {\n\tif name == IDIndex {\n\t\tpanic(\"_id is a reserved index name\")\n\t}\n\tc.indexes[name] = \u0026Index{\n\t\tfn:      indexFn,\n\t\toptions: options,\n\t\ttree:    avl.NewTree(),\n\t}\n}\n\n// storeIndex handles how we store an ID in the index tree\nfunc (idx *Index) store(key string, idStr string) {\n\tstored := idx.tree.Get(key)\n\tif stored == nil {\n\t\t// First entry for this key\n\t\tidx.tree.Set(key, idStr)\n\t\treturn\n\t}\n\n\t// Handle existing entries\n\tswitch existing := stored.(type) {\n\tcase string:\n\t\tif existing == idStr {\n\t\t\treturn // Already stored\n\t\t}\n\t\t// Convert to array\n\t\tidx.tree.Set(key, []string{existing, idStr})\n\tcase []string:\n\t\t// Check if ID already exists\n\t\tfor _, id := range existing {\n\t\t\tif id == idStr {\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t\t// Append new ID\n\t\tidx.tree.Set(key, append(existing, idStr))\n\t}\n}\n\n// removeIndex handles how we remove an ID from the index tree\nfunc (idx *Index) remove(key string, idStr string) {\n\tstored := idx.tree.Get(key)\n\tif stored == nil {\n\t\treturn\n\t}\n\n\tswitch existing := stored.(type) {\n\tcase string:\n\t\tif existing == idStr {\n\t\t\tidx.tree.Remove(key)\n\t\t}\n\tcase []string:\n\t\tnewIds := make([]string, 0, len(existing))\n\t\tfor _, id := range existing {\n\t\t\tif id != idStr {\n\t\t\t\tnewIds = append(newIds, id)\n\t\t\t}\n\t\t}\n\t\tif len(newIds) == 0 {\n\t\t\tidx.tree.Remove(key)\n\t\t} else if len(newIds) == 1 {\n\t\t\tidx.tree.Set(key, newIds[0])\n\t\t} else {\n\t\t\tidx.tree.Set(key, newIds)\n\t\t}\n\t}\n}\n\n// generateKeys extracts one or more keys from an object for a given index.\nfunc generateKeys(idx *Index, obj any) ([]string, bool) {\n\tif obj == nil {\n\t\treturn nil, false\n\t}\n\n\tswitch fnTyped := idx.fn.(type) {\n\tcase func(any) string:\n\t\t// Single-value index\n\t\tkey := fnTyped(obj)\n\t\treturn []string{key}, true\n\tcase func(any) []string:\n\t\t// Multi-value index\n\t\tkeys := fnTyped(obj)\n\t\treturn keys, true\n\tdefault:\n\t\tpanic(\"invalid index function type\")\n\t}\n}\n\n// Set adds or updates an object in the collection.\n// Returns a positive ID if successful.\n// Returns 0 if:\n//   - The object is nil\n//   - A uniqueness constraint would be violated\n//   - Index generation fails for any index\nfunc (c *Collection) Set(obj any) uint64 {\n\tif obj == nil {\n\t\treturn 0\n\t}\n\n\t// Generate new ID\n\tid := c.idGen.Next()\n\tidStr := id.String()\n\n\t// Check uniqueness constraints first\n\tfor name, idx := range c.indexes {\n\t\tif name == IDIndex {\n\t\t\tcontinue\n\t\t}\n\t\tkeys, ok := generateKeys(idx, obj)\n\t\tif !ok {\n\t\t\treturn 0\n\t\t}\n\n\t\tfor _, key := range keys {\n\t\t\t// Skip empty values for sparse indexes\n\t\t\tif idx.options\u0026SparseIndex != 0 \u0026\u0026 key == \"\" {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\t\t\tkey = strings.ToLower(key)\n\t\t\t}\n\t\t\t// Only check uniqueness for unique + single-value indexes\n\t\t\t// (UniqueIndex is ambiguous; skipping that scenario)\n\t\t\tif idx.options\u0026UniqueIndex != 0 {\n\t\t\t\tif idx.tree.Has(key) {\n\t\t\t\t\treturn 0\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Store in _id index first (the actual object)\n\tc.indexes[IDIndex].tree.Set(idStr, obj)\n\n\t// Store in all other indexes\n\tfor name, idx := range c.indexes {\n\t\tif name == IDIndex {\n\t\t\tcontinue\n\t\t}\n\t\tkeys, ok := generateKeys(idx, obj)\n\t\tif !ok {\n\t\t\t// Rollback: remove from _id index\n\t\t\tc.indexes[IDIndex].tree.Remove(idStr)\n\t\t\treturn 0\n\t\t}\n\n\t\tfor _, key := range keys {\n\t\t\tif idx.options\u0026SparseIndex != 0 \u0026\u0026 key == \"\" {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\t\t\tkey = strings.ToLower(key)\n\t\t\t}\n\t\t\tidx.store(key, idStr)\n\t\t}\n\t}\n\n\treturn uint64(id)\n}\n\n// Get retrieves entries matching the given key in the specified index.\n// Returns an iterator over the matching entries.\nfunc (c *Collection) Get(indexName string, key string) EntryIterator {\n\tidx, exists := c.indexes[indexName]\n\tif !exists {\n\t\treturn EntryIterator{err: errors.New(\"index not found: \" + indexName)}\n\t}\n\n\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\tkey = strings.ToLower(key)\n\t}\n\n\tif indexName == IDIndex {\n\t\t// For ID index, validate the ID format first\n\t\t_, err := seqid.FromString(key)\n\t\tif err != nil {\n\t\t\treturn EntryIterator{err: err}\n\t\t}\n\t}\n\n\treturn EntryIterator{\n\t\tcollection: c,\n\t\tindexName:  indexName,\n\t\tkey:        key,\n\t}\n}\n\n// GetFirst returns the first matching entry or nil if none found\nfunc (c *Collection) GetFirst(indexName, key string) *Entry {\n\titer := c.Get(indexName, key)\n\tif iter.Next() {\n\t\treturn iter.Value()\n\t}\n\treturn nil\n}\n\n// Delete removes an object by its ID and returns true if something was deleted\nfunc (c *Collection) Delete(id uint64) bool {\n\tidStr := seqid.ID(id).String()\n\n\t// Get the object first to clean up other indexes\n\tobj := c.indexes[IDIndex].tree.Get(idStr)\n\tif obj == nil {\n\t\treturn false\n\t}\n\n\t// Remove from all indexes\n\tfor name, idx := range c.indexes {\n\t\tif name == IDIndex {\n\t\t\tidx.tree.Remove(idStr)\n\t\t\tcontinue\n\t\t}\n\t\tkeys, ok := generateKeys(idx, obj)\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tfor _, key := range keys {\n\t\t\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\t\t\tkey = strings.ToLower(key)\n\t\t\t}\n\t\t\tidx.remove(key, idStr)\n\t\t}\n\t}\n\treturn true\n}\n\n// Update updates an existing object and returns true if successful\n// Returns true if the update was successful.\n// Returns false if:\n//   - The object is nil\n//   - The ID doesn't exist\n//   - A uniqueness constraint would be violated\n//   - Index generation fails for any index\n//\n// If the update fails, the collection remains unchanged.\nfunc (c *Collection) Update(id uint64, obj any) bool {\n\tif obj == nil {\n\t\treturn false\n\t}\n\tidStr := seqid.ID(id).String()\n\toldObj := c.indexes[IDIndex].tree.Get(idStr)\n\tif oldObj == nil {\n\t\treturn false\n\t}\n\n\t// Check unique constraints\n\tfor name, idx := range c.indexes {\n\t\tif name == IDIndex {\n\t\t\tcontinue\n\t\t}\n\n\t\tif idx.options\u0026UniqueIndex != 0 {\n\t\t\tnewKeys, newOk := generateKeys(idx, obj)\n\t\t\t_, oldOk := generateKeys(idx, oldObj)\n\t\t\tif !newOk || !oldOk {\n\t\t\t\treturn false\n\t\t\t}\n\n\t\t\tfor _, newKey := range newKeys {\n\t\t\t\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\t\t\t\tnewKey = strings.ToLower(newKey)\n\t\t\t\t}\n\n\t\t\t\tfound := idx.tree.Get(newKey)\n\t\t\t\tif found != nil {\n\t\t\t\t\tif storedID, ok := found.(string); !ok || storedID != idStr {\n\t\t\t\t\t\treturn false\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Store old index entries for potential rollback\n\toldEntries := make(map[string][]string)\n\tfor name, idx := range c.indexes {\n\t\tif name == IDIndex {\n\t\t\tcontinue\n\t\t}\n\t\toldKeys, ok := generateKeys(idx, oldObj)\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar adjusted []string\n\t\tfor _, okey := range oldKeys {\n\t\t\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\t\t\tokey = strings.ToLower(okey)\n\t\t\t}\n\t\t\t// Remove the oldObj from the index right away\n\t\t\tidx.remove(okey, idStr)\n\t\t\tadjusted = append(adjusted, okey)\n\t\t}\n\t\toldEntries[name] = adjusted\n\t}\n\n\t// Update the object in the _id index\n\tc.indexes[IDIndex].tree.Set(idStr, obj)\n\n\t// Add new index entries\n\tfor name, idx := range c.indexes {\n\t\tif name == IDIndex {\n\t\t\tcontinue\n\t\t}\n\t\tnewKeys, ok := generateKeys(idx, obj)\n\t\tif !ok {\n\t\t\t// Rollback: restore old object and old index entries\n\t\t\tc.indexes[IDIndex].tree.Set(idStr, oldObj)\n\t\t\tfor idxName, keys := range oldEntries {\n\t\t\t\tfor _, oldKey := range keys {\n\t\t\t\t\tc.indexes[idxName].store(oldKey, idStr)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false\n\t\t}\n\t\tfor _, nkey := range newKeys {\n\t\t\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\t\t\tnkey = strings.ToLower(nkey)\n\t\t\t}\n\t\t\tidx.store(nkey, idStr)\n\t\t}\n\t}\n\n\treturn true\n}\n\n// GetAll retrieves all entries matching the given key in the specified index.\nfunc (c *Collection) GetAll(indexName string, key string) []Entry {\n\tidx, exists := c.indexes[indexName]\n\tif !exists {\n\t\treturn nil\n\t}\n\n\tif idx.options\u0026CaseInsensitiveIndex != 0 {\n\t\tkey = strings.ToLower(key)\n\t}\n\n\tif indexName == IDIndex {\n\t\tif obj := idx.tree.Get(key); obj != nil {\n\t\t\treturn []Entry{{ID: key, Obj: obj}}\n\t\t}\n\t\treturn nil\n\t}\n\n\tidData := idx.tree.Get(key)\n\tif idData == nil {\n\t\treturn nil\n\t}\n\n\t// Handle both single and multi-value cases based on the actual data type\n\tswitch stored := idData.(type) {\n\tcase []string:\n\t\tresult := make([]Entry, 0, len(stored))\n\t\tfor _, idStr := range stored {\n\t\t\tif obj := c.indexes[IDIndex].tree.Get(idStr); obj != nil {\n\t\t\t\tresult = append(result, Entry{ID: idStr, Obj: obj})\n\t\t\t}\n\t\t}\n\t\treturn result\n\tcase string:\n\t\tif obj := c.indexes[IDIndex].tree.Get(stored); obj != nil {\n\t\t\treturn []Entry{{ID: stored, Obj: obj}}\n\t\t}\n\t}\n\treturn nil\n}\n\n// GetIndex returns the underlying tree for an index\nfunc (c *Collection) GetIndex(name string) avl.ITree {\n\tidx, exists := c.indexes[name]\n\tif !exists {\n\t\treturn nil\n\t}\n\treturn idx.tree\n}\n"},{"name":"collection_test.gno","body":"package collection\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Person struct {\n\tName     string\n\tAge      int\n\tEmail    string\n\tUsername string\n\tTags     []string\n}\n\nfunc (p Person) String() string {\n\treturn ufmt.Sprintf(\"name=%s age=%d email=%s username=%s tags=%s\",\n\t\tp.Name, p.Age, p.Email, p.Username, strings.Join(p.Tags, \",\"))\n}\n\n// TestOperation represents a single operation in a test sequence\ntype TestOperation struct {\n\top      string // \"set\" or \"update\"\n\tperson  *Person\n\tid      uint64 // for updates\n\twantID  uint64\n\twantErr bool\n}\n\n// TestCase represents a complete test case with setup and operations\ntype TestCase struct {\n\tname       string\n\tsetupIndex func(*Collection)\n\toperations []TestOperation\n}\n\nfunc TestBasicOperations(t *testing.T) {\n\tc := New()\n\n\t// Add indexes\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\tc.AddIndex(\"age\", func(v any) string {\n\t\treturn strconv.Itoa(v.(*Person).Age)\n\t}, DefaultIndex)\n\n\t// Test basic Set and Get\n\tp1 := \u0026Person{Name: \"Alice\", Age: 30, Email: \"alice@test.com\"}\n\tid1 := c.Set(p1)\n\tif id1 == 0 {\n\t\tt.Error(\"Failed to set first object\")\n\t}\n\n\t// Get by ID\n\titer := c.Get(IDIndex, seqid.ID(id1).String())\n\tif !iter.Next() {\n\t\tt.Error(\"Failed to get object by ID\")\n\t}\n\tentry := iter.Value()\n\tif entry.Obj.(*Person).Name != \"Alice\" {\n\t\tt.Error(\"Got wrong object\")\n\t}\n}\n\nfunc TestUniqueConstraints(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tsetup  func(*Collection) uint64\n\t\twantID bool\n\t}{\n\t\t{\n\t\t\tname: \"First person\",\n\t\t\tsetup: func(c *Collection) uint64 {\n\t\t\t\treturn c.Set(\u0026Person{Name: \"Alice\"})\n\t\t\t},\n\t\t\twantID: true,\n\t\t},\n\t\t{\n\t\t\tname: \"Duplicate name\",\n\t\t\tsetup: func(c *Collection) uint64 {\n\t\t\t\tc.Set(\u0026Person{Name: \"Alice\"})\n\t\t\t\treturn c.Set(\u0026Person{Name: \"Alice\"})\n\t\t\t},\n\t\t\twantID: false,\n\t\t},\n\t\t{\n\t\t\tname: \"Same age (non-unique index)\",\n\t\t\tsetup: func(c *Collection) uint64 {\n\t\t\t\tc.AddIndex(\"age\", func(v any) string {\n\t\t\t\t\treturn strconv.Itoa(v.(*Person).Age)\n\t\t\t\t}, DefaultIndex)\n\t\t\t\tc.Set(\u0026Person{Name: \"Alice\", Age: 30})\n\t\t\t\treturn c.Set(\u0026Person{Name: \"Bob\", Age: 30})\n\t\t\t},\n\t\t\twantID: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tc := New()\n\t\t\tc.AddIndex(\"name\", func(v any) string {\n\t\t\t\treturn v.(*Person).Name\n\t\t\t}, UniqueIndex)\n\n\t\t\tid := tt.setup(c)\n\t\t\tif (id != 0) != tt.wantID {\n\t\t\t\tt.Errorf(\"Set() got id = %v, want non-zero: %v\", id, tt.wantID)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestUpdates(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\tc.AddIndex(\"username\", func(v any) string {\n\t\treturn strings.ToLower(v.(*Person).Username)\n\t}, UniqueIndex|CaseInsensitiveIndex)\n\n\t// Initial setup\n\tp1 := \u0026Person{Name: \"Alice\", Username: \"alice123\"}\n\tp2 := \u0026Person{Name: \"Bob\", Username: \"bob456\"}\n\n\tid1 := c.Set(p1)\n\tc.Set(p2)\n\n\ttests := []struct {\n\t\tname      string\n\t\tid        uint64\n\t\tnewPerson *Person\n\t\twantRet   bool\n\t}{\n\t\t{\n\t\t\tname:      \"Update to non-conflicting values\",\n\t\t\tid:        id1,\n\t\t\tnewPerson: \u0026Person{Name: \"Alice2\", Username: \"alice1234\"},\n\t\t\twantRet:   true,\n\t\t},\n\t\t{\n\t\t\tname:      \"Update to conflicting username\",\n\t\t\tid:        id1,\n\t\t\tnewPerson: \u0026Person{Name: \"Alice2\", Username: \"bob456\"},\n\t\t\twantRet:   false,\n\t\t},\n\t\t{\n\t\t\tname:      \"Update non-existent ID\",\n\t\t\tid:        99999,\n\t\t\tnewPerson: \u0026Person{Name: \"Test\", Username: \"test\"},\n\t\t\twantRet:   false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgotID := c.Update(tt.id, tt.newPerson)\n\t\t\tif gotID != tt.wantRet {\n\t\t\t\tt.Errorf(\"Update() got = %v, want %v\", gotID, tt.wantRet)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestDelete(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\n\tp1 := \u0026Person{Name: \"Alice\"}\n\tid1 := c.Set(p1)\n\n\ttests := []struct {\n\t\tname    string\n\t\tid      uint64\n\t\twantRet bool\n\t}{\n\t\t{\n\t\t\tname:    \"Delete existing object\",\n\t\t\tid:      id1,\n\t\t\twantRet: true,\n\t\t},\n\t\t{\n\t\t\tname:    \"Delete non-existent object\",\n\t\t\tid:      99999,\n\t\t\twantRet: false,\n\t\t},\n\t\t{\n\t\t\tname:    \"Delete already deleted object\",\n\t\t\tid:      id1,\n\t\t\twantRet: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgotID := c.Delete(tt.id)\n\t\t\tif gotID != tt.wantRet {\n\t\t\t\tt.Errorf(\"Delete() got = %v, want %v\", gotID, tt.wantRet)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestEdgeCases(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\n\ttests := []struct {\n\t\tname      string\n\t\toperation func() bool\n\t\twantPanic bool\n\t}{\n\t\t{\n\t\t\tname: \"Set nil object\",\n\t\t\toperation: func() bool {\n\t\t\t\treturn c.Set(nil) != 0\n\t\t\t},\n\t\t\twantPanic: false,\n\t\t},\n\t\t{\n\t\t\tname: \"Set wrong type\",\n\t\t\toperation: func() bool {\n\t\t\t\treturn c.Set(\"not a person\") != 0\n\t\t\t},\n\t\t\twantPanic: true,\n\t\t},\n\t\t{\n\t\t\tname: \"Update with nil\",\n\t\t\toperation: func() bool {\n\t\t\t\tid := c.Set(\u0026Person{Name: \"Test\"})\n\t\t\t\treturn c.Update(id, nil)\n\t\t\t},\n\t\t\twantPanic: false,\n\t\t},\n\t\t{\n\t\t\tname: \"Get with invalid index name\",\n\t\t\toperation: func() bool {\n\t\t\t\titer := c.Get(\"invalid_index\", \"key\")\n\t\t\t\tif iter.Empty() {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t\tentry := iter.Value()\n\t\t\t\tif entry == nil {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t\t_, err := seqid.FromString(entry.ID)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t\treturn true\n\t\t\t},\n\t\t\twantPanic: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar got bool\n\t\t\tpanicked := false\n\n\t\t\tfunc() {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\t\tpanicked = true\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t\tgot = tt.operation()\n\t\t\t}()\n\n\t\t\tif panicked != tt.wantPanic {\n\t\t\t\tt.Errorf(\"Operation panicked = %v, want panic = %v\", panicked, tt.wantPanic)\n\t\t\t}\n\t\t\tif !panicked \u0026\u0026 got != false {\n\t\t\t\tt.Errorf(\"Operation returned %v, want 0\", got)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestIndexTypes(t *testing.T) {\n\tc := New()\n\n\t// Test different types of indexes\n\tc.AddIndex(\"composite\", func(v any) string {\n\t\tp := v.(*Person)\n\t\treturn p.Name + \":\" + strconv.Itoa(p.Age)\n\t}, UniqueIndex)\n\n\tc.AddIndex(\"case_insensitive\", func(v any) string {\n\t\treturn strings.ToLower(v.(*Person).Username)\n\t}, UniqueIndex|CaseInsensitiveIndex)\n\n\t// Test composite index\n\tp1 := \u0026Person{Name: \"Alice\", Age: 30, Username: \"Alice123\"}\n\tid1 := c.Set(p1)\n\tif id1 == 0 {\n\t\tt.Error(\"Failed to set object with composite index\")\n\t}\n\n\t// Test case-insensitive index\n\tp2 := \u0026Person{Name: \"Bob\", Age: 25, Username: \"alice123\"}\n\tid2 := c.Set(p2)\n\tif id2 != 0 {\n\t\tt.Error(\"Case-insensitive index failed to prevent duplicate\")\n\t}\n}\n\nfunc TestIndexOptions(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func(*Collection) uint64\n\t\twantID  bool\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname: \"Unique case-sensitive index\",\n\t\t\tsetup: func(c *Collection) uint64 {\n\t\t\t\tc.AddIndex(\"username\", func(v any) string {\n\t\t\t\t\treturn v.(*Person).Username\n\t\t\t\t}, UniqueIndex)\n\n\t\t\t\tc.Set(\u0026Person{Username: \"Alice\"})\n\t\t\t\treturn c.Set(\u0026Person{Username: \"Alice\"}) // Should fail\n\t\t\t},\n\t\t\twantID: false,\n\t\t},\n\t\t{\n\t\t\tname: \"Unique case-insensitive index\",\n\t\t\tsetup: func(c *Collection) uint64 {\n\t\t\t\tc.AddIndex(\"email\", func(v any) string {\n\t\t\t\t\treturn v.(*Person).Email\n\t\t\t\t}, UniqueIndex|CaseInsensitiveIndex)\n\n\t\t\t\tc.Set(\u0026Person{Email: \"test@example.com\"})\n\t\t\t\treturn c.Set(\u0026Person{Email: \"TEST@EXAMPLE.COM\"}) // Should fail\n\t\t\t},\n\t\t\twantID: false,\n\t\t},\n\t\t{\n\t\t\tname: \"Default index\",\n\t\t\tsetup: func(c *Collection) uint64 {\n\t\t\t\tc.AddIndex(\"age\", func(v any) string {\n\t\t\t\t\treturn strconv.Itoa(v.(*Person).Age)\n\t\t\t\t}, DefaultIndex)\n\n\t\t\t\t// First person with age 30\n\t\t\t\tid1 := c.Set(\u0026Person{Age: 30})\n\t\t\t\tif id1 == 0 {\n\t\t\t\t\tt.Error(\"Failed to set first person\")\n\t\t\t\t}\n\n\t\t\t\t// Second person with same age should succeed\n\t\t\t\treturn c.Set(\u0026Person{Age: 30})\n\t\t\t},\n\t\t\twantID: true,\n\t\t},\n\t\t{\n\t\t\tname: \"Multiple options\",\n\t\t\tsetup: func(c *Collection) uint64 {\n\t\t\t\tc.AddIndex(\"name\", func(v any) string {\n\t\t\t\t\treturn v.(*Person).Name\n\t\t\t\t}, UniqueIndex|CaseInsensitiveIndex|SparseIndex)\n\n\t\t\t\tc.Set(\u0026Person{Name: \"Alice\"})\n\t\t\t\treturn c.Set(\u0026Person{Name: \"ALICE\"}) // Should fail\n\t\t\t},\n\t\t\twantID: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tc := New() // Create new collection for each test\n\t\t\tid := tt.setup(c)\n\t\t\tif (id != 0) != tt.wantID {\n\t\t\t\tt.Errorf(\"got id = %v, want non-zero: %v\", id, tt.wantID)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestConcurrentOperations(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\n\tp1 := \u0026Person{Name: \"Alice\"}\n\tid1 := c.Set(p1)\n\titer := c.Get(\"_id\", seqid.ID(id1).String())\n\tsuccess := c.Update(id1, \u0026Person{Name: \"Alice2\"})\n\n\tif iter.Empty() || !success {\n\t\tt.Error(\"Concurrent operations failed\")\n\t}\n}\n\nfunc TestSparseIndexBehavior(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"optional_field\", func(v any) string {\n\t\treturn v.(*Person).Username\n\t}, SparseIndex)\n\n\ttests := []struct {\n\t\tname   string\n\t\tperson *Person\n\t\twantID bool\n\t}{\n\t\t{\n\t\t\tname:   \"Empty optional field\",\n\t\t\tperson: \u0026Person{Name: \"Alice\", Email: \"alice@test.com\"},\n\t\t\twantID: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"Populated optional field\",\n\t\t\tperson: \u0026Person{Name: \"Bob\", Email: \"bob@test.com\", Username: \"bobby\"},\n\t\t\twantID: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"Multiple empty fields\",\n\t\t\tperson: \u0026Person{Name: \"Charlie\"},\n\t\t\twantID: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tid := c.Set(tt.person)\n\t\t\tif (id != 0) != tt.wantID {\n\t\t\t\tt.Errorf(\"Set() got id = %v, want non-zero: %v\", id, tt.wantID)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestIndexKeyGeneration(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"composite\", func(v any) string {\n\t\tp := v.(*Person)\n\t\treturn p.Name + \":\" + strconv.Itoa(p.Age)\n\t}, UniqueIndex)\n\n\ttests := []struct {\n\t\tname   string\n\t\tperson *Person\n\t\twantID bool\n\t}{\n\t\t{\n\t\t\tname:   \"Valid composite key\",\n\t\t\tperson: \u0026Person{Name: \"Alice\", Age: 30},\n\t\t\twantID: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"Duplicate composite key\",\n\t\t\tperson: \u0026Person{Name: \"Alice\", Age: 30},\n\t\t\twantID: false,\n\t\t},\n\t\t{\n\t\t\tname:   \"Different composite key\",\n\t\t\tperson: \u0026Person{Name: \"Alice\", Age: 31},\n\t\t\twantID: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tid := c.Set(tt.person)\n\t\t\tif (id != 0) != tt.wantID {\n\t\t\t\tt.Errorf(\"Set() got id = %v, want non-zero: %v\", id, tt.wantID)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetIndex(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\n\ttests := []struct {\n\t\tname      string\n\t\tindexName string\n\t\twantNil   bool\n\t}{\n\t\t{\n\t\t\tname:      \"Get existing index\",\n\t\t\tindexName: \"name\",\n\t\t\twantNil:   false,\n\t\t},\n\t\t{\n\t\t\tname:      \"Get _id index\",\n\t\t\tindexName: IDIndex,\n\t\t\twantNil:   false,\n\t\t},\n\t\t{\n\t\t\tname:      \"Get non-existent index\",\n\t\t\tindexName: \"invalid\",\n\t\t\twantNil:   true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\ttree := c.GetIndex(tt.indexName)\n\t\t\tif (tree == nil) != tt.wantNil {\n\t\t\t\tt.Errorf(\"GetIndex() got nil = %v, want nil = %v\", tree == nil, tt.wantNil)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestAddIndexPanic(t *testing.T) {\n\tc := New()\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Error(\"Expected panic when adding _id index\")\n\t\t}\n\t}()\n\n\tc.AddIndex(IDIndex, func(v any) string {\n\t\treturn \"\"\n\t}, DefaultIndex)\n}\n\nfunc TestCaseInsensitiveOperations(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"email\", func(v any) string {\n\t\treturn v.(*Person).Email\n\t}, UniqueIndex|CaseInsensitiveIndex)\n\n\tp := \u0026Person{Email: \"Test@Example.com\"}\n\tid := c.Set(p)\n\n\ttests := []struct {\n\t\tname      string\n\t\tkey       string\n\t\twantObj   bool\n\t\toperation string // \"get\" or \"getAll\"\n\t\twantCount int\n\t}{\n\t\t{\"Get exact match\", \"Test@Example.com\", true, \"get\", 1},\n\t\t{\"Get different case\", \"test@example.COM\", true, \"get\", 1},\n\t\t{\"Get non-existent\", \"other@example.com\", false, \"get\", 0},\n\t\t{\"GetAll exact match\", \"Test@Example.com\", true, \"getAll\", 1},\n\t\t{\"GetAll different case\", \"test@example.COM\", true, \"getAll\", 1},\n\t\t{\"GetAll non-existent\", \"other@example.com\", false, \"getAll\", 0},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tif tt.operation == \"get\" {\n\t\t\t\titer := c.Get(\"email\", tt.key)\n\t\t\t\tif iter.Empty() {\n\t\t\t\t\tif tt.wantObj {\n\t\t\t\t\t\tt.Error(\"Expected iterator to not be empty\")\n\t\t\t\t\t}\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\thasValue := iter.Next()\n\t\t\t\tif hasValue != tt.wantObj {\n\t\t\t\t\tt.Errorf(\"Get() got object = %v, want object = %v\", hasValue, tt.wantObj)\n\t\t\t\t}\n\t\t\t\tif hasValue {\n\t\t\t\t\tentry := iter.Value()\n\t\t\t\t\tif entry.ID != seqid.ID(id).String() {\n\t\t\t\t\t\tt.Errorf(\"Get() got id = %v, want id = %v\", entry.ID, seqid.ID(id).String())\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tentries := c.GetAll(\"email\", tt.key)\n\t\t\t\tif len(entries) != tt.wantCount {\n\t\t\t\t\tt.Errorf(\"GetAll() returned %d entries, want %d\", len(entries), tt.wantCount)\n\t\t\t\t}\n\t\t\t\tif tt.wantCount \u003e 0 {\n\t\t\t\t\tentry := entries[0]\n\t\t\t\t\tif entry.ID != seqid.ID(id).String() {\n\t\t\t\t\t\tt.Errorf(\"GetAll() returned ID %s, want %s\", entry.ID, seqid.ID(id).String())\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetInvalidID(t *testing.T) {\n\tc := New()\n\titer := c.Get(IDIndex, \"not-a-valid-id\")\n\tif !iter.Empty() {\n\t\tt.Errorf(\"Get() with invalid ID format got an entry, want nil\")\n\t}\n}\n\nfunc TestGetAll(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"tags\", func(v any) []string {\n\t\treturn v.(*Person).Tags\n\t}, DefaultIndex)\n\tc.AddIndex(\"age\", func(v any) string {\n\t\treturn strconv.Itoa(v.(*Person).Age)\n\t}, DefaultIndex)\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\n\t// Create test data\n\tpeople := []*Person{\n\t\t{Name: \"Alice\", Age: 30, Tags: []string{\"dev\", \"go\"}},\n\t\t{Name: \"Bob\", Age: 30, Tags: []string{\"dev\", \"python\"}},\n\t\t{Name: \"Charlie\", Age: 25, Tags: []string{\"dev\", \"rust\"}},\n\t}\n\n\tids := make([]uint64, len(people))\n\tfor i, p := range people {\n\t\tids[i] = c.Set(p)\n\t\tif ids[i] == 0 {\n\t\t\tt.Fatalf(\"Failed to set person %s\", p.Name)\n\t\t}\n\t}\n\n\ttests := []struct {\n\t\tname      string\n\t\tindexName string\n\t\tkey       string\n\t\twantCount int\n\t}{\n\t\t{\n\t\t\tname:      \"Multi-value index with multiple matches\",\n\t\t\tindexName: \"tags\",\n\t\t\tkey:       \"dev\",\n\t\t\twantCount: 3,\n\t\t},\n\t\t{\n\t\t\tname:      \"Multi-value index with single match\",\n\t\t\tindexName: \"tags\",\n\t\t\tkey:       \"go\",\n\t\t\twantCount: 1,\n\t\t},\n\t\t{\n\t\t\tname:      \"Multi-value index with no matches\",\n\t\t\tindexName: \"tags\",\n\t\t\tkey:       \"java\",\n\t\t\twantCount: 0,\n\t\t},\n\t\t{\n\t\t\tname:      \"Single-value non-unique index with multiple matches\",\n\t\t\tindexName: \"age\",\n\t\t\tkey:       \"30\",\n\t\t\twantCount: 2,\n\t\t},\n\t\t{\n\t\t\tname:      \"Single-value unique index\",\n\t\t\tindexName: \"name\",\n\t\t\tkey:       \"Alice\",\n\t\t\twantCount: 1,\n\t\t},\n\t\t{\n\t\t\tname:      \"Non-existent index\",\n\t\t\tindexName: \"invalid\",\n\t\t\tkey:       \"value\",\n\t\t\twantCount: 0,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\titer := c.Get(tt.indexName, tt.key)\n\t\t\tcount := 0\n\t\t\tfor iter.Next() {\n\t\t\t\tentry := iter.Value()\n\t\t\t\tif entry.ID == \"\" {\n\t\t\t\t\tt.Error(\"Got entry with empty ID\")\n\t\t\t\t}\n\t\t\t\tif entry.Obj == nil {\n\t\t\t\t\tt.Error(\"Got entry with nil Obj\")\n\t\t\t\t}\n\t\t\t\tcount++\n\t\t\t}\n\t\t\tif count != tt.wantCount {\n\t\t\t\tt.Errorf(\"Got %d entries, want %d\", count, tt.wantCount)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestIndexOperations(t *testing.T) {\n\ttests := []struct {\n\t\tname    string\n\t\tsetup   func(*Collection) (uint64, error)\n\t\tverify  func(*Collection, uint64) error\n\t\twantErr bool\n\t}{\n\t\t{\n\t\t\tname: \"Basic set and get\",\n\t\t\tsetup: func(c *Collection) (uint64, error) {\n\t\t\t\tc.AddIndex(\"name\", func(v any) string {\n\t\t\t\t\treturn v.(*Person).Name\n\t\t\t\t}, UniqueIndex)\n\t\t\t\treturn c.Set(\u0026Person{Name: \"Alice\", Age: 30}), nil\n\t\t\t},\n\t\t\tverify: func(c *Collection, id uint64) error {\n\t\t\t\titer := c.Get(IDIndex, seqid.ID(id).String())\n\t\t\t\tif !iter.Next() {\n\t\t\t\t\treturn errors.New(\"failed to get object by ID\")\n\t\t\t\t}\n\t\t\t\tentry := iter.Value()\n\t\t\t\tif entry.Obj.(*Person).Name != \"Alice\" {\n\t\t\t\t\treturn errors.New(\"got wrong object\")\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"Composite index\",\n\t\t\tsetup: func(c *Collection) (uint64, error) {\n\t\t\t\tc.AddIndex(\"composite\", func(v any) string {\n\t\t\t\t\tp := v.(*Person)\n\t\t\t\t\treturn p.Name + \":\" + strconv.Itoa(p.Age)\n\t\t\t\t}, UniqueIndex)\n\t\t\t\treturn c.Set(\u0026Person{Name: \"Alice\", Age: 30}), nil\n\t\t\t},\n\t\t\tverify: func(c *Collection, id uint64) error {\n\t\t\t\titer := c.Get(\"composite\", \"Alice:30\")\n\t\t\t\tif !iter.Next() {\n\t\t\t\t\treturn errors.New(\"failed to get object by composite index\")\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t},\n\t\t},\n\t\t// Add more test cases combining unique scenarios from original tests\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tc := New()\n\t\t\tid, err := tt.setup(c)\n\t\t\tif (err != nil) != tt.wantErr {\n\t\t\t\tt.Errorf(\"setup error = %v, wantErr %v\", err, tt.wantErr)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif err == nil {\n\t\t\t\tif err := tt.verify(c, id); err != nil {\n\t\t\t\t\tt.Errorf(\"verification failed: %v\", err)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMultiValueIndexes(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"tags\", func(v any) []string {\n\t\treturn v.(*Person).Tags\n\t}, DefaultIndex)\n\n\ttests := []struct {\n\t\tname      string\n\t\tsetup     []*Person\n\t\tsearchTag string\n\t\twantCount int\n\t}{\n\t\t{\n\t\t\tname: \"Multiple tags, multiple matches\",\n\t\t\tsetup: []*Person{\n\t\t\t\t{Name: \"Alice\", Tags: []string{\"dev\", \"go\"}},\n\t\t\t\t{Name: \"Bob\", Tags: []string{\"dev\", \"python\"}},\n\t\t\t\t{Name: \"Charlie\", Tags: []string{\"dev\", \"rust\"}},\n\t\t\t},\n\t\t\tsearchTag: \"dev\",\n\t\t\twantCount: 3,\n\t\t},\n\t\t{\n\t\t\tname: \"Single tag match\",\n\t\t\tsetup: []*Person{\n\t\t\t\t{Name: \"Alice\", Tags: []string{\"dev\", \"go\"}},\n\t\t\t\t{Name: \"Bob\", Tags: []string{\"dev\", \"python\"}},\n\t\t\t},\n\t\t\tsearchTag: \"go\",\n\t\t\twantCount: 1,\n\t\t},\n\t\t{\n\t\t\tname: \"No matches\",\n\t\t\tsetup: []*Person{\n\t\t\t\t{Name: \"Alice\", Tags: []string{\"dev\", \"go\"}},\n\t\t\t\t{Name: \"Bob\", Tags: []string{\"dev\", \"python\"}},\n\t\t\t},\n\t\t\tsearchTag: \"java\",\n\t\t\twantCount: 0,\n\t\t},\n\t\t{\n\t\t\tname: \"Empty tags\",\n\t\t\tsetup: []*Person{\n\t\t\t\t{Name: \"Alice\", Tags: []string{}},\n\t\t\t\t{Name: \"Bob\", Tags: nil},\n\t\t\t},\n\t\t\tsearchTag: \"dev\",\n\t\t\twantCount: 0,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tc := New()\n\t\t\tc.AddIndex(\"tags\", func(v any) []string {\n\t\t\t\treturn v.(*Person).Tags\n\t\t\t}, DefaultIndex)\n\n\t\t\t// Setup test data\n\t\t\tfor _, p := range tt.setup {\n\t\t\t\tif id := c.Set(p); id == 0 {\n\t\t\t\t\tt.Fatalf(\"Failed to set person %s\", p.Name)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Test Get operation\n\t\t\titer := c.Get(\"tags\", tt.searchTag)\n\t\t\tcount := 0\n\t\t\tfor iter.Next() {\n\t\t\t\tcount++\n\t\t\t}\n\t\t\tif count != tt.wantCount {\n\t\t\t\tt.Errorf(\"Get() got %d matches, want %d\", count, tt.wantCount)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetOperations(t *testing.T) {\n\tc := New()\n\tc.AddIndex(\"name\", func(v any) string {\n\t\treturn v.(*Person).Name\n\t}, UniqueIndex)\n\tc.AddIndex(\"age\", func(v any) string {\n\t\treturn strconv.Itoa(v.(*Person).Age)\n\t}, DefaultIndex)\n\n\t// Setup test data\n\ttestPeople := []*Person{\n\t\t{Name: \"Alice\", Age: 30},\n\t\t{Name: \"Bob\", Age: 30},\n\t\t{Name: \"Charlie\", Age: 25},\n\t}\n\n\tids := make([]uint64, len(testPeople))\n\tfor i, p := range testPeople {\n\t\tids[i] = c.Set(p)\n\t\tif ids[i] == 0 {\n\t\t\tt.Fatalf(\"Failed to set person %s\", p.Name)\n\t\t}\n\t}\n\n\ttests := []struct {\n\t\tname      string\n\t\tindexName string\n\t\tkey       string\n\t\twantCount int\n\t\twantErr   bool\n\t}{\n\t\t{\n\t\t\tname:      \"Get by ID\",\n\t\t\tindexName: IDIndex,\n\t\t\tkey:       seqid.ID(ids[0]).String(),\n\t\t\twantCount: 1,\n\t\t\twantErr:   false,\n\t\t},\n\t\t{\n\t\t\tname:      \"Get by unique index\",\n\t\t\tindexName: \"name\",\n\t\t\tkey:       \"Alice\",\n\t\t\twantCount: 1,\n\t\t\twantErr:   false,\n\t\t},\n\t\t{\n\t\t\tname:      \"Get by non-unique index\",\n\t\t\tindexName: \"age\",\n\t\t\tkey:       \"30\",\n\t\t\twantCount: 2,\n\t\t\twantErr:   false,\n\t\t},\n\t\t{\n\t\t\tname:      \"Get with invalid index\",\n\t\t\tindexName: \"invalid_index\",\n\t\t\tkey:       \"value\",\n\t\t\twantCount: 0,\n\t\t\twantErr:   true,\n\t\t},\n\t\t{\n\t\t\tname:      \"Get with invalid ID format\",\n\t\t\tindexName: IDIndex,\n\t\t\tkey:       \"not-a-valid-id\",\n\t\t\twantCount: 0,\n\t\t\twantErr:   true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\titer := c.Get(tt.indexName, tt.key)\n\t\t\tif iter.Empty() {\n\t\t\t\tif !tt.wantErr {\n\t\t\t\t\tt.Errorf(\"Get() returned empty iterator, wanted %d results\", tt.wantCount)\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tcount := 0\n\t\t\tfor iter.Next() {\n\t\t\t\tentry := iter.Value()\n\t\t\t\tif entry.ID == \"\" {\n\t\t\t\t\tt.Error(\"Got entry with empty ID\")\n\t\t\t\t}\n\t\t\t\tif entry.Obj == nil {\n\t\t\t\t\tt.Error(\"Got entry with nil Obj\")\n\t\t\t\t}\n\t\t\t\tcount++\n\t\t\t}\n\n\t\t\tif count != tt.wantCount {\n\t\t\t\tt.Errorf(\"Get() returned %d results, want %d\", count, tt.wantCount)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestEntryString(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tentry    *Entry\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"Nil entry\",\n\t\t\tentry:    nil,\n\t\t\texpected: \"\u003cnil\u003e\",\n\t\t},\n\t\t{\n\t\t\tname: \"Person entry\",\n\t\t\tentry: \u0026Entry{\n\t\t\t\tID:  \"123\",\n\t\t\t\tObj: \u0026Person{Name: \"Alice\", Age: 30},\n\t\t\t},\n\t\t\texpected: `Entry{ID: 123, Obj: name=Alice age=30 email= username= tags=}`,\n\t\t},\n\t\t{\n\t\t\tname: \"Entry with nil object\",\n\t\t\tentry: \u0026Entry{\n\t\t\t\tID:  \"456\",\n\t\t\t\tObj: nil,\n\t\t\t},\n\t\t\texpected: `Entry{ID: 456, Obj: \u003cnil\u003e}`,\n\t\t},\n\t\t{\n\t\t\tname: \"Entry with complete person\",\n\t\t\tentry: \u0026Entry{\n\t\t\t\tID: \"789\",\n\t\t\t\tObj: \u0026Person{\n\t\t\t\t\tName:     \"Bob\",\n\t\t\t\t\tAge:      25,\n\t\t\t\t\tEmail:    \"bob@example.com\",\n\t\t\t\t\tUsername: \"bobby\",\n\t\t\t\t\tTags:     []string{\"dev\", \"go\"},\n\t\t\t\t},\n\t\t\t},\n\t\t\texpected: `Entry{ID: 789, Obj: name=Bob age=25 email=bob@example.com username=bobby tags=dev,go}`,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := tt.entry.String()\n\t\t\tif got != tt.expected {\n\t\t\t\tt.Errorf(\"Entry.String() = %q, want %q\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"entry.gno","body":"package collection\n\nimport \"gno.land/p/nt/ufmt/v0\"\n\n// Entry represents a single object in the collection with its ID\ntype Entry struct {\n\tID  string\n\tObj any\n}\n\n// String returns a string representation of the Entry\nfunc (e *Entry) String() string {\n\tif e == nil {\n\t\treturn \"\u003cnil\u003e\"\n\t}\n\treturn ufmt.Sprintf(\"Entry{ID: %s, Obj: %v}\", e.ID, e.Obj)\n}\n\n// EntryIterator provides iteration over collection entries\ntype EntryIterator struct {\n\tcollection *Collection\n\tindexName  string\n\tkey        string\n\tcurrentID  string\n\tcurrentObj any\n\terr        error\n\tclosed     bool\n\n\t// For multi-value cases\n\tids        []string\n\tcurrentIdx int\n}\n\nfunc (ei *EntryIterator) Close() error {\n\tei.closed = true\n\tei.currentID = \"\"\n\tei.currentObj = nil\n\tei.ids = nil\n\treturn nil\n}\n\nfunc (ei *EntryIterator) Next() bool {\n\tif ei == nil || ei.closed || ei.err != nil {\n\t\treturn false\n\t}\n\n\t// Handle ID index specially\n\tif ei.indexName == IDIndex {\n\t\tif ei.currentID != \"\" { // We've already returned the single value\n\t\t\treturn false\n\t\t}\n\t\tobj := ei.collection.indexes[IDIndex].tree.Get(ei.key)\n\t\tif obj == nil {\n\t\t\treturn false\n\t\t}\n\t\tei.currentID = ei.key\n\t\tei.currentObj = obj\n\t\treturn true\n\t}\n\n\t// Get the index\n\tidx, exists := ei.collection.indexes[ei.indexName]\n\tif !exists {\n\t\treturn false\n\t}\n\n\t// Initialize ids slice if needed\n\tif ei.ids == nil {\n\t\tidData := idx.tree.Get(ei.key)\n\t\tif idData == nil {\n\t\t\treturn false\n\t\t}\n\n\t\tswitch stored := idData.(type) {\n\t\tcase []string:\n\t\t\tei.ids = stored\n\t\t\tei.currentIdx = -1\n\t\tcase string:\n\t\t\tei.ids = []string{stored}\n\t\t\tei.currentIdx = -1\n\t\tdefault:\n\t\t\treturn false\n\t\t}\n\t}\n\n\t// Move to next ID\n\tei.currentIdx++\n\tif ei.currentIdx \u003e= len(ei.ids) {\n\t\treturn false\n\t}\n\n\t// Fetch the actual object\n\tei.currentID = ei.ids[ei.currentIdx]\n\tobj := ei.collection.indexes[IDIndex].tree.Get(ei.currentID)\n\tif obj == nil {\n\t\t// Skip invalid entries\n\t\treturn ei.Next()\n\t}\n\tei.currentObj = obj\n\treturn true\n}\n\nfunc (ei *EntryIterator) Error() error {\n\treturn ei.err\n}\n\nfunc (ei *EntryIterator) Value() *Entry {\n\tif ei == nil || ei.closed || ei.currentID == \"\" {\n\t\treturn nil\n\t}\n\treturn \u0026Entry{\n\t\tID:  ei.currentID,\n\t\tObj: ei.currentObj,\n\t}\n}\n\nfunc (ei *EntryIterator) Empty() bool {\n\tif ei == nil || ei.closed || ei.err != nil {\n\t\treturn true\n\t}\n\n\t// Handle ID index specially\n\tif ei.indexName == IDIndex {\n\t\treturn !ei.collection.indexes[IDIndex].tree.Has(ei.key)\n\t}\n\n\t// Get the index\n\tidx, exists := ei.collection.indexes[ei.indexName]\n\tif !exists {\n\t\treturn true\n\t}\n\n\t// Check if key exists in index\n\tidData := idx.tree.Get(ei.key)\n\tif idData == nil {\n\t\treturn true\n\t}\n\n\t// Check if there are any valid IDs\n\tswitch stored := idData.(type) {\n\tcase []string:\n\t\treturn len(stored) == 0\n\tcase string:\n\t\treturn stored == \"\"\n\tdefault:\n\t\treturn true\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/collection\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"+tBDhGrSVKIGVqritaSyFc8vmmqTdxDluAy+AgONTurdH4u0KLuUVe+ZzH5jaesJwBjBV/CfGKgCa24wa5/FBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"list","path":"gno.land/p/nt/avl/v0/list","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/avl/v0/list\"\ngno = \"0.9\"\n"},{"name":"list.gno","body":"// Package list implements a dynamic list data structure backed by an AVL tree.\n// It provides O(log n) operations for most list operations while maintaining\n// order stability.\n//\n// The list supports various operations including append, get, set, delete,\n// range queries, and iteration. It can store values of any type.\n//\n// Example usage:\n//\n//\t// Create a new list and add elements\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\n//\t// Get and set elements\n//\tvalue := l.Get(1)  // returns 2\n//\tl.Set(1, 42)      // updates index 1 to 42\n//\n//\t// Delete elements\n//\tl.Delete(0)       // removes first element\n//\n//\t// Iterate over elements\n//\tl.ForEach(func(index int, value any) bool {\n//\t    ufmt.Printf(\"index %d: %v\\n\", index, value)\n//\t    return false  // continue iteration\n//\t})\n//\t// Output:\n//\t// index 0: 42\n//\t// index 1: 3\n//\n//\t// Create a list of specific size\n//\tl = list.Make(3, \"default\")  // creates [default, default, default]\n//\n//\t// Create a list using a variable declaration\n//\tvar l2 list.List\n//\tl2.Append(4, 5, 6)\n//\tprintln(l2.Len())  // Output: 3\npackage list\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/rotree\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// IList defines the interface for list operations\ntype IList interface {\n\tLen() int\n\tAppend(values ...any)\n\tGet(index int) any\n\tSet(index int, value any) bool\n\tDelete(index int) (any, bool)\n\tSlice(startIndex, endIndex int) []any\n\tForEach(fn func(index int, value any) bool)\n\tClone() *List\n\tDeleteRange(startIndex, endIndex int) int\n}\n\n// Verify List implements IList interface\nvar _ IList = (*List)(nil)\n\n// List represents an ordered sequence of items backed by an AVL tree\ntype List struct {\n\ttree  avl.Tree\n\tidGen seqid.ID\n}\n\n// Len returns the number of elements in the list.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\tprintln(l.Len()) // Output: 3\nfunc (l *List) Len() int {\n\treturn l.tree.Size()\n}\n\n// Append adds one or more values to the end of the list.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1)        // adds single value\n//\tl.Append(2, 3, 4)  // adds multiple values\n//\tprintln(l.Len()) // Output: 4\nfunc (l *List) Append(values ...any) {\n\tfor _, v := range values {\n\t\tl.tree.Set(l.idGen.Next().String(), v)\n\t}\n}\n\n// Get returns the value at the specified index.\n// Returns nil if index is out of bounds.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\tprintln(l.Get(1))    // Output: 2\n//\tprintln(l.Get(-1))   // Output: nil\n//\tprintln(l.Get(999))  // Output: nil\nfunc (l *List) Get(index int) any {\n\tif index \u003c 0 || index \u003e= l.tree.Size() {\n\t\treturn nil\n\t}\n\t_, value := l.tree.GetByIndex(index)\n\treturn value\n}\n\n// Set updates or appends a value at the specified index.\n// Returns true if the operation was successful, false otherwise.\n// For empty lists, only index 0 is valid (append case).\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\n//\tl.Set(1, 42)      // updates existing index\n//\tprintln(l.Get(1)) // Output: 42\n//\n//\tl.Set(3, 4)       // appends at end\n//\tprintln(l.Get(3)) // Output: 4\n//\n//\tl.Set(-1, 5)      // invalid index\n//\tprintln(l.Len()) // Output: 4 (list unchanged)\nfunc (l *List) Set(index int, value any) bool {\n\tsize := l.tree.Size()\n\n\t// Handle empty list case - only allow index 0\n\tif size == 0 {\n\t\tif index == 0 {\n\t\t\tl.Append(value)\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t}\n\n\tif index \u003c 0 || index \u003e size {\n\t\treturn false\n\t}\n\n\t// If setting at the end (append case)\n\tif index == size {\n\t\tl.Append(value)\n\t\treturn true\n\t}\n\n\t// Get the key at the specified index\n\tkey, _ := l.tree.GetByIndex(index)\n\tif key == \"\" {\n\t\treturn false\n\t}\n\n\t// Update the value at the existing key\n\tl.tree.Set(key, value)\n\treturn true\n}\n\n// Delete removes the element at the specified index.\n// Returns the deleted value and true if successful, nil and false otherwise.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\n//\tval, ok := l.Delete(1)\n//\tprintln(val, ok)  // Output: 2 true\n//\tprintln(l.Len())  // Output: 2\n//\n//\tval, ok = l.Delete(-1)\n//\tprintln(val, ok)  // Output: nil false\nfunc (l *List) Delete(index int) (any, bool) {\n\tsize := l.tree.Size()\n\t// Always return nil, false for empty list\n\tif size == 0 {\n\t\treturn nil, false\n\t}\n\n\tif index \u003c 0 || index \u003e= size {\n\t\treturn nil, false\n\t}\n\n\tkey, value := l.tree.GetByIndex(index)\n\tif key == \"\" {\n\t\treturn nil, false\n\t}\n\n\tl.tree.Remove(key)\n\treturn value, true\n}\n\n// Slice returns a slice of values from startIndex (inclusive) to endIndex (exclusive).\n// Returns nil if the range is invalid.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3, 4, 5)\n//\n//\tprintln(l.Slice(1, 4))   // Output: [2 3 4]\n//\tprintln(l.Slice(-1, 2))  // Output: [1 2]\n//\tprintln(l.Slice(3, 999)) // Output: [4 5]\n//\tprintln(l.Slice(3, 2))   // Output: nil\nfunc (l *List) Slice(startIndex, endIndex int) []any {\n\tsize := l.tree.Size()\n\n\t// Normalize bounds\n\tif startIndex \u003c 0 {\n\t\tstartIndex = 0\n\t}\n\tif endIndex \u003e size {\n\t\tendIndex = size\n\t}\n\tif startIndex \u003e= endIndex {\n\t\treturn nil\n\t}\n\n\tcount := endIndex - startIndex\n\tresult := make([]any, count)\n\n\ti := 0\n\tl.tree.IterateByOffset(startIndex, count, func(_ string, value any) bool {\n\t\tresult[i] = value\n\t\ti++\n\t\treturn false\n\t})\n\treturn result\n}\n\n// ForEach iterates through all elements in the list.\nfunc (l *List) ForEach(fn func(index int, value any) bool) {\n\tif l.tree.Size() == 0 {\n\t\treturn\n\t}\n\n\tindex := 0\n\tl.tree.IterateByOffset(0, l.tree.Size(), func(_ string, value any) bool {\n\t\tresult := fn(index, value)\n\t\tindex++\n\t\treturn result\n\t})\n}\n\n// Clone creates a shallow copy of the list.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\n//\tclone := l.Clone()\n//\tclone.Set(0, 42)\n//\n//\tprintln(l.Get(0))    // Output: 1\n//\tprintln(clone.Get(0)) // Output: 42\nfunc (l *List) Clone() *List {\n\tnewList := \u0026List{\n\t\ttree:  avl.Tree{},\n\t\tidGen: l.idGen,\n\t}\n\n\tsize := l.tree.Size()\n\tif size == 0 {\n\t\treturn newList\n\t}\n\n\tl.tree.IterateByOffset(0, size, func(_ string, value any) bool {\n\t\tnewList.Append(value)\n\t\treturn false\n\t})\n\n\treturn newList\n}\n\n// DeleteRange removes elements from startIndex (inclusive) to endIndex (exclusive).\n// Returns the number of elements deleted.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3, 4, 5)\n//\n//\tdeleted := l.DeleteRange(1, 4)\n//\tprintln(deleted)     // Output: 3\n//\tprintln(l.Range(0, l.Len())) // Output: [1 5]\nfunc (l *List) DeleteRange(startIndex, endIndex int) int {\n\tsize := l.tree.Size()\n\n\t// Normalize bounds\n\tif startIndex \u003c 0 {\n\t\tstartIndex = 0\n\t}\n\tif endIndex \u003e size {\n\t\tendIndex = size\n\t}\n\tif startIndex \u003e= endIndex {\n\t\treturn 0\n\t}\n\n\t// Collect keys to delete\n\tkeysToDelete := make([]string, 0, endIndex-startIndex)\n\tl.tree.IterateByOffset(startIndex, endIndex-startIndex, func(key string, _ any) bool {\n\t\tkeysToDelete = append(keysToDelete, key)\n\t\treturn false\n\t})\n\n\t// Delete collected keys\n\tfor _, key := range keysToDelete {\n\t\tl.tree.Remove(key)\n\t}\n\n\treturn len(keysToDelete)\n}\n\n// Tree returns a read-only pointer to the underlying AVL tree.\n//\n// Example:\n//\n//\tvar l list.List\n//\tl.Append(1, 2, 3)\n//\n//\trotree := l.Tree()\n//\trotree.ReverseIterateByOffset(0, rotree.Size(), func(key string, value any) bool {\n//\t  println(value) // Output: 3 2 1\n//\t  return false\n//\t})\nfunc (l *List) Tree() *rotree.ReadOnlyTree {\n\treturn rotree.Wrap(\u0026l.tree, nil)\n}\n"},{"name":"list_test.gno","body":"package list\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestList_Basic(t *testing.T) {\n\tvar l List\n\n\t// Test empty list\n\tif l.Len() != 0 {\n\t\tt.Errorf(\"new list should be empty, got len %d\", l.Len())\n\t}\n\n\t// Test append and length\n\tl.Append(1, 2, 3)\n\tif l.Len() != 3 {\n\t\tt.Errorf(\"expected len 3, got %d\", l.Len())\n\t}\n\n\t// Test get\n\tif v := l.Get(0); v != 1 {\n\t\tt.Errorf(\"expected 1 at index 0, got %v\", v)\n\t}\n\tif v := l.Get(1); v != 2 {\n\t\tt.Errorf(\"expected 2 at index 1, got %v\", v)\n\t}\n\tif v := l.Get(2); v != 3 {\n\t\tt.Errorf(\"expected 3 at index 2, got %v\", v)\n\t}\n\n\t// Test out of bounds\n\tif v := l.Get(-1); v != nil {\n\t\tt.Errorf(\"expected nil for negative index, got %v\", v)\n\t}\n\tif v := l.Get(3); v != nil {\n\t\tt.Errorf(\"expected nil for out of bounds index, got %v\", v)\n\t}\n}\n\nfunc TestList_Set(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\t// Test valid set within bounds\n\tif ok := l.Set(1, 42); !ok {\n\t\tt.Error(\"Set should return true for valid index\")\n\t}\n\tif v := l.Get(1); v != 42 {\n\t\tt.Errorf(\"expected 42 after Set, got %v\", v)\n\t}\n\n\t// Test set at size (append)\n\tif ok := l.Set(3, 4); !ok {\n\t\tt.Error(\"Set should return true when appending at size\")\n\t}\n\tif v := l.Get(3); v != 4 {\n\t\tt.Errorf(\"expected 4 after Set at size, got %v\", v)\n\t}\n\n\t// Test invalid sets\n\tif ok := l.Set(-1, 10); ok {\n\t\tt.Error(\"Set should return false for negative index\")\n\t}\n\tif ok := l.Set(5, 10); ok {\n\t\tt.Error(\"Set should return false for index \u003e size\")\n\t}\n\n\t// Verify list state hasn't changed after invalid operations\n\texpected := []any{1, 42, 3, 4}\n\tfor i, want := range expected {\n\t\tif got := l.Get(i); got != want {\n\t\t\tt.Errorf(\"index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n}\n\nfunc TestList_Delete(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\t// Test valid delete\n\tif v, ok := l.Delete(1); !ok || v != 2 {\n\t\tt.Errorf(\"Delete(1) = %v, %v; want 2, true\", v, ok)\n\t}\n\tif l.Len() != 2 {\n\t\tt.Errorf(\"expected len 2 after delete, got %d\", l.Len())\n\t}\n\tif v := l.Get(1); v != 3 {\n\t\tt.Errorf(\"expected 3 at index 1 after delete, got %v\", v)\n\t}\n\n\t// Test invalid delete\n\tif v, ok := l.Delete(-1); ok || v != nil {\n\t\tt.Errorf(\"Delete(-1) = %v, %v; want nil, false\", v, ok)\n\t}\n\tif v, ok := l.Delete(2); ok || v != nil {\n\t\tt.Errorf(\"Delete(2) = %v, %v; want nil, false\", v, ok)\n\t}\n}\n\nfunc TestList_Slice(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3, 4, 5)\n\n\t// Test valid ranges\n\tvalues := l.Slice(1, 4)\n\texpected := []any{2, 3, 4}\n\tif !sliceEqual(values, expected) {\n\t\tt.Errorf(\"Slice(1,4) = %v; want %v\", values, expected)\n\t}\n\n\t// Test edge cases\n\tif values := l.Slice(-1, 2); !sliceEqual(values, []any{1, 2}) {\n\t\tt.Errorf(\"Slice(-1,2) = %v; want [1 2]\", values)\n\t}\n\tif values := l.Slice(3, 10); !sliceEqual(values, []any{4, 5}) {\n\t\tt.Errorf(\"Slice(3,10) = %v; want [4 5]\", values)\n\t}\n\tif values := l.Slice(3, 2); values != nil {\n\t\tt.Errorf(\"Slice(3,2) = %v; want nil\", values)\n\t}\n}\n\nfunc TestList_ForEach(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\tsum := 0\n\tl.ForEach(func(index int, value any) bool {\n\t\tsum += value.(int)\n\t\treturn false\n\t})\n\n\tif sum != 6 {\n\t\tt.Errorf(\"ForEach sum = %d; want 6\", sum)\n\t}\n\n\t// Test early termination\n\tcount := 0\n\tl.ForEach(func(index int, value any) bool {\n\t\tcount++\n\t\treturn true // stop after first item\n\t})\n\n\tif count != 1 {\n\t\tt.Errorf(\"ForEach early termination count = %d; want 1\", count)\n\t}\n}\n\nfunc TestList_Clone(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\tclone := l.Clone()\n\n\t// Test same length\n\tif clone.Len() != l.Len() {\n\t\tt.Errorf(\"clone.Len() = %d; want %d\", clone.Len(), l.Len())\n\t}\n\n\t// Test same values\n\tfor i := 0; i \u003c l.Len(); i++ {\n\t\tif clone.Get(i) != l.Get(i) {\n\t\t\tt.Errorf(\"clone.Get(%d) = %v; want %v\", i, clone.Get(i), l.Get(i))\n\t\t}\n\t}\n\n\t// Test independence\n\tl.Set(0, 42)\n\tif clone.Get(0) == l.Get(0) {\n\t\tt.Error(\"clone should be independent of original\")\n\t}\n\n\t// Test that appending to both after clone works independently.\n\tvar l2 List\n\tl2.Append(\"a\", \"b\", \"c\")\n\tclone2 := l2.Clone()\n\n\tl2.Append(\"d\", \"e\")\n\tclone2.Append(\"x\", \"y\")\n\n\tif l2.Len() != 5 {\n\t\tt.Errorf(\"original after append: Len() = %d; want 5\", l2.Len())\n\t}\n\tif clone2.Len() != 5 {\n\t\tt.Errorf(\"clone after append: Len() = %d; want 5\", clone2.Len())\n\t}\n\torigExpected := []any{\"a\", \"b\", \"c\", \"d\", \"e\"}\n\tcloneExpected := []any{\"a\", \"b\", \"c\", \"x\", \"y\"}\n\tfor i := 0; i \u003c 5; i++ {\n\t\tif l2.Get(i) != origExpected[i] {\n\t\t\tt.Errorf(\"original.Get(%d) = %v; want %v\", i, l2.Get(i), origExpected[i])\n\t\t}\n\t\tif clone2.Get(i) != cloneExpected[i] {\n\t\t\tt.Errorf(\"clone.Get(%d) = %v; want %v\", i, clone2.Get(i), cloneExpected[i])\n\t\t}\n\t}\n}\n\nfunc TestList_DeleteRange(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3, 4, 5)\n\n\t// Test valid range delete\n\tdeleted := l.DeleteRange(1, 4)\n\tif deleted != 3 {\n\t\tt.Errorf(\"DeleteRange(1,4) deleted %d elements; want 3\", deleted)\n\t}\n\tif l.Len() != 2 {\n\t\tt.Errorf(\"after DeleteRange(1,4) len = %d; want 2\", l.Len())\n\t}\n\texpected := []any{1, 5}\n\tfor i, want := range expected {\n\t\tif got := l.Get(i); got != want {\n\t\t\tt.Errorf(\"after DeleteRange(1,4) index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n\n\t// Test edge cases\n\tl = List{}\n\tl.Append(1, 2, 3)\n\n\t// Delete with negative start\n\tif deleted := l.DeleteRange(-1, 2); deleted != 2 {\n\t\tt.Errorf(\"DeleteRange(-1,2) deleted %d elements; want 2\", deleted)\n\t}\n\n\t// Delete with end \u003e length\n\tl = List{}\n\tl.Append(1, 2, 3)\n\tif deleted := l.DeleteRange(1, 5); deleted != 2 {\n\t\tt.Errorf(\"DeleteRange(1,5) deleted %d elements; want 2\", deleted)\n\t}\n\n\t// Delete invalid range\n\tif deleted := l.DeleteRange(2, 1); deleted != 0 {\n\t\tt.Errorf(\"DeleteRange(2,1) deleted %d elements; want 0\", deleted)\n\t}\n\n\t// Delete empty range\n\tif deleted := l.DeleteRange(1, 1); deleted != 0 {\n\t\tt.Errorf(\"DeleteRange(1,1) deleted %d elements; want 0\", deleted)\n\t}\n}\n\nfunc TestList_Tree(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3)\n\n\trotree := l.Tree()\n\texpected := 3\n\n\t// Reverse iterate through the ReadOnlyTree\n\trotree.ReverseIterateByOffset(0, rotree.Size(), func(key string, value any) bool {\n\t\tintValue := value.(int)\n\t\tif intValue != expected {\n\t\t\tt.Errorf(\"ReadOnlyTree expected %d, got %d\", expected, intValue)\n\t\t}\n\t\texpected--\n\t\treturn false\n\t})\n}\n\nfunc TestList_EmptyOperations(t *testing.T) {\n\tvar l List\n\n\t// Operations on empty list\n\tif v := l.Get(0); v != nil {\n\t\tt.Errorf(\"Get(0) on empty list = %v; want nil\", v)\n\t}\n\n\t// Set should work at index 0 for empty list (append case)\n\tif ok := l.Set(0, 1); !ok {\n\t\tt.Error(\"Set(0,1) on empty list = false; want true\")\n\t}\n\tif v := l.Get(0); v != 1 {\n\t\tt.Errorf(\"Get(0) after Set = %v; want 1\", v)\n\t}\n\n\tl = List{} // Reset to empty list\n\tif v, ok := l.Delete(0); ok || v != nil {\n\t\tt.Errorf(\"Delete(0) on empty list = %v, %v; want nil, false\", v, ok)\n\t}\n\tif values := l.Slice(0, 1); values != nil {\n\t\tt.Errorf(\"Range(0,1) on empty list = %v; want nil\", values)\n\t}\n}\n\nfunc TestList_DifferentTypes(t *testing.T) {\n\tvar l List\n\n\t// Test with different types\n\tl.Append(42, \"hello\", true, 3.14)\n\n\tif v := l.Get(0).(int); v != 42 {\n\t\tt.Errorf(\"Get(0) = %v; want 42\", v)\n\t}\n\tif v := l.Get(1).(string); v != \"hello\" {\n\t\tt.Errorf(\"Get(1) = %v; want 'hello'\", v)\n\t}\n\tif v := l.Get(2).(bool); !v {\n\t\tt.Errorf(\"Get(2) = %v; want true\", v)\n\t}\n\tif v := l.Get(3).(float64); v != 3.14 {\n\t\tt.Errorf(\"Get(3) = %v; want 3.14\", v)\n\t}\n}\n\nfunc TestList_LargeOperations(t *testing.T) {\n\tvar l List\n\n\t// Test with larger number of elements\n\tn := 1000\n\tfor i := 0; i \u003c n; i++ {\n\t\tl.Append(i)\n\t}\n\n\tif l.Len() != n {\n\t\tt.Errorf(\"Len() = %d; want %d\", l.Len(), n)\n\t}\n\n\t// Test range on large list\n\tvalues := l.Slice(n-3, n)\n\texpected := []any{n - 3, n - 2, n - 1}\n\tif !sliceEqual(values, expected) {\n\t\tt.Errorf(\"Range(%d,%d) = %v; want %v\", n-3, n, values, expected)\n\t}\n\n\t// Test large range deletion\n\tdeleted := l.DeleteRange(100, 900)\n\tif deleted != 800 {\n\t\tt.Errorf(\"DeleteRange(100,900) = %d; want 800\", deleted)\n\t}\n\tif l.Len() != 200 {\n\t\tt.Errorf(\"Len() after large delete = %d; want 200\", l.Len())\n\t}\n}\n\nfunc TestList_ChainedOperations(t *testing.T) {\n\tvar l List\n\n\t// Test sequence of operations\n\tl.Append(1, 2, 3)\n\tl.Delete(1)\n\tl.Append(4)\n\tl.Set(1, 5)\n\n\texpected := []any{1, 5, 4}\n\tfor i, want := range expected {\n\t\tif got := l.Get(i); got != want {\n\t\t\tt.Errorf(\"index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n}\n\nfunc TestList_RangeEdgeCases(t *testing.T) {\n\tvar l List\n\tl.Append(1, 2, 3, 4, 5)\n\n\t// Test various edge cases for Range\n\tcases := []struct {\n\t\tstart, end int\n\t\twant       []any\n\t}{\n\t\t{-10, 2, []any{1, 2}},\n\t\t{3, 10, []any{4, 5}},\n\t\t{0, 0, nil},\n\t\t{5, 5, nil},\n\t\t{4, 3, nil},\n\t\t{-1, -1, nil},\n\t}\n\n\tfor _, tc := range cases {\n\t\tgot := l.Slice(tc.start, tc.end)\n\t\tif !sliceEqual(got, tc.want) {\n\t\t\tt.Errorf(\"Slice(%d,%d) = %v; want %v\", tc.start, tc.end, got, tc.want)\n\t\t}\n\t}\n}\n\nfunc TestList_IndexConsistency(t *testing.T) {\n\tvar l List\n\n\t// Initial additions\n\tl.Append(1, 2, 3, 4, 5) // [1,2,3,4,5]\n\n\t// Delete from middle\n\tl.Delete(2) // [1,2,4,5]\n\n\t// Add more elements\n\tl.Append(6, 7) // [1,2,4,5,6,7]\n\n\t// Delete range from middle\n\tl.DeleteRange(1, 4) // [1,6,7]\n\n\t// Add more elements\n\tl.Append(8, 9, 10) // [1,6,7,8,9,10]\n\n\t// Verify sequence is continuous\n\texpected := []any{1, 6, 7, 8, 9, 10}\n\tfor i, want := range expected {\n\t\tif got := l.Get(i); got != want {\n\t\t\tt.Errorf(\"index %d = %v; want %v\", i, got, want)\n\t\t}\n\t}\n\n\t// Verify no extra elements exist\n\tif l.Len() != len(expected) {\n\t\tt.Errorf(\"length = %d; want %d\", l.Len(), len(expected))\n\t}\n\n\t// Verify all indices are accessible\n\tallValues := l.Slice(0, l.Len())\n\tif !sliceEqual(allValues, expected) {\n\t\tt.Errorf(\"Slice(0, Len()) = %v; want %v\", allValues, expected)\n\t}\n\n\t// Verify no gaps in iteration\n\tvar iteratedValues []any\n\tvar indices []int\n\tl.ForEach(func(index int, value any) bool {\n\t\titeratedValues = append(iteratedValues, value)\n\t\tindices = append(indices, index)\n\t\treturn false\n\t})\n\n\t// Check values from iteration\n\tif !sliceEqual(iteratedValues, expected) {\n\t\tt.Errorf(\"ForEach values = %v; want %v\", iteratedValues, expected)\n\t}\n\n\t// Check indices are sequential\n\tfor i, idx := range indices {\n\t\tif idx != i {\n\t\t\tt.Errorf(\"ForEach index %d = %d; want %d\", i, idx, i)\n\t\t}\n\t}\n}\n\nfunc TestList_RecursiveSafety(t *testing.T) {\n\t// Create a new list\n\tl := \u0026List{}\n\n\t// Add some initial values\n\tl.Append(\"id1\")\n\tl.Append(\"id2\")\n\tl.Append(\"id3\")\n\n\t// Test deep list traversal\n\tfound := false\n\tl.ForEach(func(i int, v any) bool {\n\t\tif str, ok := v.(string); ok {\n\t\t\tif str == \"id2\" {\n\t\t\t\tfound = true\n\t\t\t\treturn true // stop iteration\n\t\t\t}\n\t\t}\n\t\treturn false // continue iteration\n\t})\n\n\tif !found {\n\t\tt.Error(\"Failed to find expected value in list\")\n\t}\n\n\tshort := testing.Short()\n\n\t// Test recursive safety by performing multiple operations\n\tfor i := 0; i \u003c 1000; i++ {\n\t\t// Add new value\n\t\tl.Append(ufmt.Sprintf(\"id%d\", i+4))\n\n\t\tif !short {\n\t\t\t// Search for a value\n\t\t\tvar lastFound bool\n\t\t\tl.ForEach(func(j int, v any) bool {\n\t\t\t\tif str, ok := v.(string); ok {\n\t\t\t\t\tif str == ufmt.Sprintf(\"id%d\", i+3) {\n\t\t\t\t\t\tlastFound = true\n\t\t\t\t\t\treturn true\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !lastFound {\n\t\t\t\tt.Errorf(\"Failed to find value id%d after insertion\", i+3)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Verify final length\n\texpectedLen := 1003 // 3 initial + 1000 added\n\tif l.Len() != expectedLen {\n\t\tt.Errorf(\"Expected length %d, got %d\", expectedLen, l.Len())\n\t}\n\n\tif short {\n\t\tt.Skip(\"skipping extended recursive safety test in short mode\")\n\t}\n}\n\n// Helper function to compare slices\nfunc sliceEqual(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"+6Bqzkk2ydazq3ulhG7eyZt4hucvWCd5l8SDfj1geydse9MHRX0dxrgB5RnFe9ffzD3Z0TBblY8CNVhDl+gLDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"datastore","path":"gno.land/p/jeronimoalbi/datastore","files":[{"name":"datastore.gno","body":"package datastore\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// ErrStorageExists indicates that a storage exists with the same name.\nvar ErrStorageExists = errors.New(\"a storage with the same name exists\")\n\n// Datastore is a store that can contain multiple named storages.\n// A storage is a collection of records.\n//\n// Example usage:\n//\n//\t// Create an empty storage to store user records\n//\tvar db Datastore\n//\tstorage := db.CreateStorage(\"users\")\n//\n//\t// Get a storage that has been created before\n//\tstorage = db.GetStorage(\"profiles\")\ntype Datastore struct {\n\tstorages avl.Tree // string(name) -\u003e *Storage\n}\n\n// CreateStorage creates a new named storage within the data store.\nfunc (ds *Datastore) CreateStorage(name string, options ...StorageOption) *Storage {\n\tif ds.storages.Has(name) {\n\t\treturn nil\n\t}\n\n\ts := NewStorage(name, options...)\n\tds.storages.Set(name, \u0026s)\n\treturn \u0026s\n}\n\n// HasStorage checks if data store contains a storage with a specific name.\nfunc (ds Datastore) HasStorage(name string) bool {\n\treturn ds.storages.Has(name)\n}\n\n// GetStorage returns a storage that has been created with a specific name.\n// It returns nil when a storage with the specified name is not found.\nfunc (ds Datastore) GetStorage(name string) *Storage {\n\tif v := ds.storages.Get(name); v != nil {\n\t\treturn v.(*Storage)\n\t}\n\treturn nil\n}\n"},{"name":"datastore_test.gno","body":"package datastore\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestDatastoreCreateStorage(t *testing.T) {\n\tcases := []struct {\n\t\tname        string\n\t\tstorageName string\n\t\tmustFail    bool\n\t\tsetup       func(*Datastore)\n\t}{\n\t\t{\n\t\t\tname:        \"success\",\n\t\t\tstorageName: \"users\",\n\t\t},\n\t\t{\n\t\t\tname:        \"storage exists\",\n\t\t\tstorageName: \"users\",\n\t\t\tmustFail:    true,\n\t\t\tsetup: func(db *Datastore) {\n\t\t\t\tdb.CreateStorage(\"users\")\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar db Datastore\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026db)\n\t\t\t}\n\n\t\t\tstorage := db.CreateStorage(tc.storageName)\n\n\t\t\tif tc.mustFail {\n\t\t\t\tuassert.Equal(t, nil, storage)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotEqual(t, nil, storage, \"storage created\")\n\t\t\tuassert.Equal(t, tc.storageName, storage.Name())\n\t\t\tuassert.True(t, db.HasStorage(tc.storageName))\n\t\t})\n\t}\n}\n\nfunc TestDatastoreHasStorage(t *testing.T) {\n\tvar (\n\t\tdb   Datastore\n\t\tname = \"users\"\n\t)\n\n\tuassert.False(t, db.HasStorage(name))\n\n\tdb.CreateStorage(name)\n\tuassert.True(t, db.HasStorage(name))\n}\n\nfunc TestDatastoreGetStorage(t *testing.T) {\n\tvar (\n\t\tdb   Datastore\n\t\tname = \"users\"\n\t)\n\n\tstorage := db.GetStorage(name)\n\tuassert.Equal(t, nil, storage)\n\n\tdb.CreateStorage(name)\n\n\tstorage = db.GetStorage(name)\n\turequire.NotEqual(t, nil, storage, \"storage found\")\n\tuassert.Equal(t, name, storage.Name())\n}\n"},{"name":"doc.gno","body":"// Package datastore provides support to store multiple collections of records.\n//\n// It supports the definition of multiple storages, where each one is a collection\n// of records. Records can have any number of user defined fields which are added\n// dynamically when values are set on a record. These fields can also be renamed\n// or removed.\n//\n// Storages have support for simple schemas that allows users to pre-define fields\n// which can optionally have a default value also defined. Default values are\n// assigned to new records on creation.\n//\n// User defined schemas can optionally be strict, which means that records from a\n// storage using the schema can only assign values to the pre-defined set of fields.\n// In which case, assigning a value to an unknown field would result on an error.\n//\n// Package also support the definition of custom record indexes. Indexes are used\n// by storages to search and iterate records.\n// The default index is the ID index but custom single and multi value indexes can\n// be defined.\n//\n// WARNING: Using this package to store your realm data must be carefully considered.\n// The fact that record fields are not strictly typed and can be renamed or removed\n// could lead to issues if not careful when coding your realm(s). So it's recommended\n// that you consider other alternatives first, like alternative patterns or solutions\n// provided by the blockchain to deal with data, types and data migration for example.\n//\n// Example usage:\n//\n//\tvar db datastore.Datastore\n//\n//\t// Define a unique case insensitive index for user emails\n//\temailIdx := datastore.NewIndex(\"email\", func(r datastore.Record) string {\n//\t  return r.MustGet(\"email\").(string)\n//\t}).Unique().CaseInsensitive()\n//\n//\t// Create a new storage for user records\n//\tstorage := db.CreateStorage(\"users\", datastore.WithIndex(emailIdx))\n//\n//\t// Add a user with a single \"email\" field\n//\tuser := storage.NewRecord()\n//\tuser.Set(\"email\", \"foo@bar.org\")\n//\n//\t// Save to assing user ID and update indexes\n//\tuser.Save()\n//\n//\t// Find user by email using the custom index\n//\tuser, _ = storage.Get(emailIdx.Name(), \"foo@bar.org\")\n//\n//\t// Find user by ID\n//\tuser, _ = storage.GetByID(user.ID())\n//\n//\t// Search user's profile by email in another existing storage\n//\tstorage = db.GetStorage(\"profiles\")\n//\temail := user.MustGet(\"email\").(string)\n//\tprofile, found := storage.Get(\"user\", email)\n//\tif !found {\n//\t  panic(\"profile not found\")\n//\t}\n//\n//\t// Delete the profile from the storage and update indexes\n//\tstorage.Delete(profile.ID())\n//\n// Example query usage:\n//\n//\tvar db datastore.Datastore\n//\n//\t// Create a query with a custom offset and size\n//\tstorage := db.GetStorage(\"users\")\n//\trecordset, err := storage.Query(datastore.WithOffset(100), datastore.WithSize(50))\n//\tif err != nil {\n//\t  panic(err)\n//\t}\n//\n//\t// Get all query results\n//\tvar records []Record\n//\trecordset.Iterate(func(r datastore.Record) bool {\n//\t  records = append(records, r)\n//\t  return false\n//\t})\n//\n// Example query using a custom index usage:\n//\n//\tvar db datastore.Datastore\n//\n//\t// Create a query to get records using a custom pre-defined index\n//\tstorage := db.GetStorage(\"posts\")\n//\trecordset, err := storage.Query(datastore.UseIndex(\"tags\", \"tagname\"))\n//\tif err != nil {\n//\t  panic(err)\n//\t}\n//\n//\t// Get all query results\n//\tvar records []Record\n//\trecordset.Iterate(func(r datastore.Record) bool {\n//\t  records = append(records, r)\n//\t  return false\n//\t})\npackage datastore\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/datastore\"\ngno = \"0.9\"\n"},{"name":"index.gno","body":"package datastore\n\nimport (\n\t\"gno.land/p/moul/collection\"\n)\n\n// DefaultIndexOptions defines the default options for new indexes.\nconst DefaultIndexOptions = collection.DefaultIndex | collection.SparseIndex\n\ntype (\n\t// IndexFn defines a type for single value indexing functions.\n\t// This type of function extracts a single string value from\n\t// a record that is then used to index it.\n\tIndexFn func(Record) string\n\n\t// IndexMultiValueFn defines a type for multi value indexing functions.\n\t// This type of function extracts multiple string values from a\n\t// record that are then used to index it.\n\tIndexMultiValueFn func(Record) []string\n\n\t// Index defines a type for custom user defined storage indexes.\n\t// Storages are by default indexed by the auto geneated record ID\n\t// but can additionally be indexed by other custom record fields.\n\tIndex struct {\n\t\tname    string\n\t\toptions collection.IndexOption\n\t\tfn      interface{}\n\t}\n)\n\n// NewIndex creates a new single value index.\n//\n// Usage example:\n//\n//\t// Index a User record by email\n//\tidx := NewIndex(\"email\", func(r Record) string {\n//\t  return r.MustGet(\"email\").(string)\n//\t})\nfunc NewIndex(name string, fn IndexFn) Index {\n\treturn Index{\n\t\tname:    name,\n\t\toptions: DefaultIndexOptions,\n\t\tfn: func(v interface{}) string {\n\t\t\treturn fn(v.(Record))\n\t\t},\n\t}\n}\n\n// NewMultiValueIndex creates a new multi value index.\n//\n// Usage example:\n//\n//\t// Index a Post record by tag\n//\tidx := NewMultiValueIndex(\"tag\", func(r Record) []string {\n//\t  return r.MustGet(\"tags\").([]string)\n//\t})\nfunc NewMultiValueIndex(name string, fn IndexMultiValueFn) Index {\n\treturn Index{\n\t\tname:    name,\n\t\toptions: DefaultIndexOptions,\n\t\tfn: func(v interface{}) []string {\n\t\t\treturn fn(v.(Record))\n\t\t},\n\t}\n}\n\n// Name returns index's name.\nfunc (idx Index) Name() string {\n\treturn idx.name\n}\n\n// Options returns current index options.\n// These options define the index behavior regarding case sensitivity and uniquenes.\nfunc (idx Index) Options() collection.IndexOption {\n\treturn idx.options\n}\n\n// Func returns the function that storage collections apply\n// to each record to get the value to use for indexing it.\nfunc (idx Index) Func() interface{} {\n\treturn idx.fn\n}\n\n// Unique returns a copy of the index that indexes record values as unique values.\n// Returned index contains previous options plus the unique one.\nfunc (idx Index) Unique() Index {\n\tif idx.options\u0026collection.UniqueIndex == 0 {\n\t\tidx.options |= collection.UniqueIndex\n\t}\n\treturn idx\n}\n\n// CaseInsensitive returns a copy of the index that indexes record values ignoring casing.\n// Returned index contains previous options plus the case insensitivity one.\nfunc (idx Index) CaseInsensitive() Index {\n\tif idx.options\u0026collection.CaseInsensitiveIndex == 0 {\n\t\tidx.options |= collection.CaseInsensitiveIndex\n\t}\n\treturn idx\n}\n"},{"name":"index_test.gno","body":"package datastore\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/collection\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNewIndex(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\toptions collection.IndexOption\n\t\tsetup   func(Index) Index\n\t}{\n\t\t{\n\t\t\tname:    \"default\",\n\t\t\toptions: DefaultIndexOptions,\n\t\t},\n\t\t{\n\t\t\tname:    \"unique\",\n\t\t\toptions: DefaultIndexOptions | collection.UniqueIndex,\n\t\t\tsetup:   func(idx Index) Index { return idx.Unique() },\n\t\t},\n\t\t{\n\t\t\tname:    \"case insensitive\",\n\t\t\toptions: DefaultIndexOptions | collection.CaseInsensitiveIndex,\n\t\t\tsetup:   func(idx Index) Index { return idx.CaseInsensitive() },\n\t\t},\n\t\t{\n\t\t\tname:    \"unique case insensitive\",\n\t\t\toptions: DefaultIndexOptions | collection.CaseInsensitiveIndex | collection.UniqueIndex,\n\t\t\tsetup:   func(idx Index) Index { return idx.CaseInsensitive().Unique() },\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tname := \"foo\"\n\t\t\tidx := NewIndex(name, func(Record) string { return \"\" })\n\n\t\t\tif tc.setup != nil {\n\t\t\t\tidx = tc.setup(idx)\n\t\t\t}\n\n\t\t\tuassert.Equal(t, name, idx.Name())\n\t\t\tuassert.Equal(t, uint64(tc.options), uint64(idx.Options()))\n\n\t\t\t_, ok := idx.Func().(func(interface{}) string)\n\t\t\tuassert.True(t, ok)\n\t\t})\n\t}\n}\n\nfunc TestNewMultiIndex(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\toptions collection.IndexOption\n\t\tsetup   func(Index) Index\n\t}{\n\t\t{\n\t\t\tname:    \"default\",\n\t\t\toptions: DefaultIndexOptions,\n\t\t},\n\t\t{\n\t\t\tname:    \"unique\",\n\t\t\toptions: DefaultIndexOptions | collection.UniqueIndex,\n\t\t\tsetup:   func(idx Index) Index { return idx.Unique() },\n\t\t},\n\t\t{\n\t\t\tname:    \"case insensitive\",\n\t\t\toptions: DefaultIndexOptions | collection.CaseInsensitiveIndex,\n\t\t\tsetup:   func(idx Index) Index { return idx.CaseInsensitive() },\n\t\t},\n\t\t{\n\t\t\tname:    \"unique case insensitive\",\n\t\t\toptions: DefaultIndexOptions | collection.CaseInsensitiveIndex | collection.UniqueIndex,\n\t\t\tsetup:   func(idx Index) Index { return idx.CaseInsensitive().Unique() },\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tname := \"foo\"\n\t\t\tidx := NewMultiValueIndex(name, func(Record) []string { return nil })\n\n\t\t\tif tc.setup != nil {\n\t\t\t\tidx = tc.setup(idx)\n\t\t\t}\n\n\t\t\tuassert.Equal(t, name, idx.Name())\n\t\t\tuassert.Equal(t, uint64(tc.options), uint64(idx.Options()))\n\n\t\t\t_, ok := idx.Func().(func(interface{}) []string)\n\t\t\tuassert.True(t, ok)\n\t\t})\n\t}\n}\n"},{"name":"query.gno","body":"package datastore\n\nimport (\n\t\"gno.land/p/moul/collection\"\n)\n\nvar defaultQuery = Query{indexName: collection.IDIndex}\n\n// Query contains arguments for querying a storage.\ntype Query struct {\n\toffset    int\n\tsize      int\n\tindexName string\n\tindexKey  string\n}\n\n// Offset returns the position of the first record to return.\n// The minimum offset value is 0.\nfunc (q Query) Offset() int {\n\treturn q.offset\n}\n\n// Size returns the maximum number of records a query returns.\nfunc (q Query) Size() int {\n\treturn q.size\n}\n\n// IndexName returns the name of the storage index being used for the query.\nfunc (q Query) IndexName() string {\n\treturn q.indexName\n}\n\n// IndexKey return the index key value to locate the records.\n// An empty string is returned when all indexed records match the query.\nfunc (q Query) IndexKey() string {\n\treturn q.indexKey\n}\n\n// IsEmpty checks if the query is empty.\n// Empty queries return no records.\nfunc (q Query) IsEmpty() bool {\n\treturn q.indexName == \"\"\n}\n"},{"name":"query_options.gno","body":"package datastore\n\nimport (\n\t\"errors\"\n\t\"strings\"\n)\n\nvar (\n\tErrEmptyQueryIndexName = errors.New(\"query index name is empty\")\n\tErrInvalidQueryOffset  = errors.New(\"minimum allowed query offset is 0\")\n\tErrInvalidQuerySize    = errors.New(\"minimum allowed query size is 1\")\n)\n\n// QueryOption configures queries.\ntype QueryOption func(*Query) error\n\n// WithOffset assigns the offset or position of the first record that query must return.\n// The minimum allowed offset is 0.\nfunc WithOffset(offset int) QueryOption {\n\treturn func(q *Query) error {\n\t\tif offset \u003c 0 {\n\t\t\treturn ErrInvalidQueryOffset\n\t\t}\n\n\t\tq.offset = offset\n\t\treturn nil\n\t}\n}\n\n// WithSize assigns the maximum number of records that query can return.\n// The minimum allowed size is 1.\nfunc WithSize(size int) QueryOption {\n\treturn func(q *Query) error {\n\t\tif size \u003c 1 {\n\t\t\treturn ErrInvalidQuerySize\n\t\t}\n\n\t\tq.size = size\n\t\treturn nil\n\t}\n}\n\n// UseIndex assigns the index that the query must use to get the records.\n// Using an index requires a key value to locate the records within the index.\nfunc UseIndex(name, key string) QueryOption {\n\treturn func(q *Query) error {\n\t\tq.indexName = strings.TrimSpace(name)\n\t\tif q.indexName == \"\" {\n\t\t\treturn ErrEmptyQueryIndexName\n\t\t}\n\n\t\tq.indexKey = key\n\t\treturn nil\n\t}\n}\n"},{"name":"record.gno","body":"package datastore\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/moul/collection\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// ErrUndefinedField indicates that a field in not defined in a record's schema.\nvar ErrUndefinedField = errors.New(\"undefined field\")\n\ntype (\n\t// Record stores values for one or more fields.\n\tRecord interface {\n\t\tReadOnlyRecord\n\n\t\t// Set assings a value to a record field.\n\t\t// If the field doesn't exist it's created if the underlying schema allows it.\n\t\t// Storage schema can optionally be strict in which case no new fields other than\n\t\t// the ones that were previously defined are allowed.\n\t\tSet(field string, value interface{}) error\n\n\t\t// Save assigns an ID to newly created records and update storage indexes.\n\t\tSave() bool\n\t}\n\n\t// ReadOnlyRecord defines an interface for read-only records.\n\tReadOnlyRecord interface {\n\t\t// ID returns record's ID\n\t\tID() uint64\n\n\t\t// Key returns a string representation of the record's ID.\n\t\t// It's used to be able to search records within the ID index.\n\t\tKey() string\n\n\t\t// Type returns the record's type.\n\t\tType() string\n\n\t\t// Fields returns the list of the record's field names.\n\t\tFields() []string\n\n\t\t// IsEmpty checks if the record has no values.\n\t\tIsEmpty() bool\n\n\t\t// HasField checks if the record has a specific field.\n\t\tHasField(name string) bool\n\n\t\t// Get returns the value of a record's field.\n\t\tGet(field string) (value interface{}, found bool)\n\n\t\t// MustGet returns the value of a record's field or panics when the field is not found.\n\t\tMustGet(field string) interface{}\n\t}\n\n\t// RecordIterFn defines a type for record iteration functions.\n\tRecordIterFn func(Record) (stop bool)\n\n\t// Recordset defines an interface that allows iterating multiple records.\n\tRecordset interface {\n\t\t// Iterate iterates records in order.\n\t\tIterate(fn RecordIterFn) (stopped bool)\n\n\t\t// ReverseIterate iterates records in reverse order.\n\t\tReverseIterate(fn RecordIterFn) (stopped bool)\n\n\t\t// Size returns the number of records in the recordset.\n\t\tSize() int\n\t}\n)\n\ntype record struct {\n\tid         uint64\n\tschema     *Schema\n\tcollection *collection.Collection\n\tvalues     avl.Tree // string(field index) -\u003e interface{}\n}\n\n// ID returns record's ID\nfunc (r record) ID() uint64 {\n\treturn r.id\n}\n\n// Key returns a string representation of the record's ID.\n// It's used to be able to search records within the ID index.\nfunc (r record) Key() string {\n\treturn seqid.ID(r.id).String()\n}\n\n// Type returns the record's type.\nfunc (r record) Type() string {\n\treturn r.schema.Name()\n}\n\n// Fields returns the list of the record's field names.\nfunc (r record) Fields() []string {\n\treturn r.schema.Fields()\n}\n\n// IsEmpty checks if the record has no values.\nfunc (r record) IsEmpty() bool {\n\treturn r.values.Size() == 0\n}\n\n// HasField checks if the record has a specific field.\nfunc (r record) HasField(name string) bool {\n\treturn r.schema.HasField(name)\n}\n\n// Set assings a value to a record field.\n// If the field doesn't exist it's created if the underlying schema allows it.\n// Storage schema can optionally be strict in which case no new fields other than\n// the ones that were previously defined are allowed.\nfunc (r *record) Set(field string, value interface{}) error {\n\ti := r.schema.GetFieldIndex(field)\n\tif i == -1 {\n\t\tif r.schema.IsStrict() {\n\t\t\treturn ErrUndefinedField\n\t\t}\n\n\t\ti, _ = r.schema.AddField(field, nil)\n\t}\n\n\tkey := castIntToKey(i)\n\tr.values.Set(key, value)\n\treturn nil\n}\n\n// Get returns the value of a record's field.\nfunc (r record) Get(field string) (value interface{}, found bool) {\n\ti := r.schema.GetFieldIndex(field)\n\tif i == -1 {\n\t\treturn nil, false\n\t}\n\n\tkey := castIntToKey(i)\n\treturn r.values.Get(key), r.values.Has(key)\n}\n\n// MustGet returns the value of a record's field or panics when the field is not found.\nfunc (r record) MustGet(field string) interface{} {\n\tv, found := r.Get(field)\n\tif !found {\n\t\tpanic(\"field not found: \" + field)\n\t}\n\treturn v\n}\n\n// Save assigns an ID to newly created records and update storage indexes.\nfunc (r *record) Save() bool {\n\tif r.id == 0 {\n\t\tr.id = r.collection.Set(r)\n\t\treturn r.id != 0\n\t}\n\treturn r.collection.Update(r.id, r)\n}\n\ntype recordset struct {\n\tquery   Query\n\trecords avl.ITree\n\tkeys    []string\n\tsize    int\n}\n\n// Iterate iterates records in order.\nfunc (rs recordset) Iterate(fn RecordIterFn) (stopped bool) {\n\tif rs.isUsingCustomIndex() {\n\t\tfor _, k := range rs.keys {\n\t\t\tv := rs.records.Get(k)\n\t\t\tif v == nil {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tif fn(v.(Record)) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\n\t\treturn false\n\t}\n\n\toffset := rs.query.Offset()\n\tcount := rs.query.Size()\n\tif count == 0 {\n\t\tcount = rs.records.Size()\n\t}\n\n\treturn rs.records.IterateByOffset(offset, count, func(_ string, v interface{}) bool {\n\t\treturn fn(v.(Record))\n\t})\n}\n\n// ReverseIterate iterates records in reverse order.\nfunc (rs recordset) ReverseIterate(fn RecordIterFn) (stopped bool) {\n\tif rs.isUsingCustomIndex() {\n\t\tfor i := len(rs.keys) - 1; i \u003e= 0; i-- {\n\t\t\tv := rs.records.Get(rs.keys[i])\n\t\t\tif v == nil {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tif fn(v.(Record)) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\n\t\treturn false\n\t}\n\n\toffset := rs.query.Offset()\n\tcount := rs.query.Size()\n\tif count == 0 {\n\t\tcount = rs.records.Size()\n\t}\n\n\treturn rs.records.ReverseIterateByOffset(offset, count, func(_ string, v interface{}) bool {\n\t\treturn fn(v.(Record))\n\t})\n}\n\n// Size returns the number of records in the recordset.\nfunc (rs recordset) Size() int {\n\treturn rs.size\n}\n\nfunc (rs recordset) isUsingCustomIndex() bool {\n\treturn rs.query.IndexName() != collection.IDIndex\n}\n"},{"name":"record_test.gno","body":"package datastore\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nvar (\n\t_ Record    = (*record)(nil)\n\t_ Recordset = (*recordset)(nil)\n)\n\nfunc TestRecordDefaults(t *testing.T) {\n\t// Arrange\n\tstorage := NewStorage(\"foo\")\n\n\t// Act\n\tr := storage.NewRecord()\n\n\t// Assert\n\tuassert.Equal(t, uint64(0), r.ID())\n\tuassert.Equal(t, \"0000000\", r.Key())\n\tuassert.Equal(t, \"Foo\", r.Type())\n\tuassert.Equal(t, nil, r.Fields())\n\tuassert.True(t, r.IsEmpty())\n}\n\nfunc TestRecordHasField(t *testing.T) {\n\tstorage := NewStorage(\"foo\")\n\tstorage.Schema().AddField(\"foo\", nil)\n\n\tr := storage.NewRecord()\n\n\tuassert.True(t, r.HasField(\"foo\"))\n\tuassert.False(t, r.HasField(\"undefined\"))\n}\n\nfunc TestRecordSet(t *testing.T) {\n\tcases := []struct {\n\t\tname        string\n\t\toptions     []SchemaOption\n\t\tfield       string\n\t\tfieldsCount int\n\t\tvalue       int\n\t\terr         error\n\t}{\n\t\t{\n\t\t\tname:        \"first new field\",\n\t\t\tfield:       \"test\",\n\t\t\tfieldsCount: 1,\n\t\t\tvalue:       42,\n\t\t},\n\t\t{\n\t\t\tname: \"new extra field\",\n\t\t\toptions: []SchemaOption{\n\t\t\t\tWithField(\"foo\"),\n\t\t\t\tWithField(\"bar\"),\n\t\t\t},\n\t\t\tfield:       \"test\",\n\t\t\tfieldsCount: 3,\n\t\t\tvalue:       42,\n\t\t},\n\t\t{\n\t\t\tname: \"existing field\",\n\t\t\toptions: []SchemaOption{\n\t\t\t\tWithField(\"test\"),\n\t\t\t},\n\t\t\tfield:       \"test\",\n\t\t\tfieldsCount: 1,\n\t\t\tvalue:       42,\n\t\t},\n\t\t{\n\t\t\tname:        \"undefined field\",\n\t\t\toptions:     []SchemaOption{Strict()},\n\t\t\tfield:       \"test\",\n\t\t\tfieldsCount: 1,\n\t\t\tvalue:       42,\n\t\t\terr:         ErrUndefinedField,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\ts := NewSchema(\"Foo\", tc.options...)\n\t\t\tstorage := NewStorage(\"foo\", WithSchema(s))\n\t\t\tr := storage.NewRecord()\n\n\t\t\t// Act\n\t\t\terr := r.Set(tc.field, tc.value)\n\n\t\t\t// Assert\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err)\n\t\t\tuassert.True(t, r.HasField(\"test\"))\n\t\t\tuassert.False(t, r.IsEmpty())\n\t\t\tuassert.Equal(t, tc.fieldsCount, len(r.Fields()))\n\t\t})\n\t}\n}\n\nfunc TestRecordGet(t *testing.T) {\n\tstorage := NewStorage(\"foo\")\n\tr := storage.NewRecord()\n\tr.Set(\"foo\", \"bar\")\n\tr.Set(\"test\", 42)\n\n\tv, found := r.Get(\"test\")\n\turequire.True(t, found, \"get setted value\")\n\n\tgot, ok := v.(int)\n\turequire.True(t, ok, \"setted value type\")\n\tuassert.Equal(t, 42, got)\n\n\t_, found = r.Get(\"unknown\")\n\tuassert.False(t, found)\n}\n\nfunc TestRecordSave(t *testing.T) {\n\tindex := NewIndex(\"name\", func(r Record) string {\n\t\treturn r.MustGet(\"name\").(string)\n\t}).Unique().CaseInsensitive()\n\n\tstorage := NewStorage(\"foo\", WithIndex(index))\n\tcases := []struct {\n\t\tname            string\n\t\tid              uint64\n\t\tfieldValue, key string\n\t\tstorageSize     int\n\t\tsetup           func(Storage) Record\n\t}{\n\t\t{\n\t\t\tname:        \"create first record\",\n\t\t\tid:          1,\n\t\t\tkey:         \"0000001\",\n\t\t\tfieldValue:  \"foo\",\n\t\t\tstorageSize: 1,\n\t\t\tsetup:       func(s Storage) Record { return s.NewRecord() },\n\t\t},\n\t\t{\n\t\t\tname:        \"create second record\",\n\t\t\tid:          2,\n\t\t\tkey:         \"0000002\",\n\t\t\tfieldValue:  \"bar\",\n\t\t\tstorageSize: 2,\n\t\t\tsetup:       func(s Storage) Record { return s.NewRecord() },\n\t\t},\n\t\t{\n\t\t\tname:        \"update second record\",\n\t\t\tid:          2,\n\t\t\tkey:         \"0000002\",\n\t\t\tfieldValue:  \"baz\",\n\t\t\tstorageSize: 2,\n\t\t\tsetup: func(s Storage) Record {\n\t\t\t\tr, _ := storage.Get(index.Name(), \"bar\")\n\t\t\t\treturn r\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tr := tc.setup(storage)\n\t\t\tr.Set(\"name\", tc.fieldValue)\n\n\t\t\t_, found := storage.Get(index.Name(), tc.fieldValue)\n\t\t\turequire.False(t, found, \"record not found\")\n\t\t\turequire.True(t, r.Save(), \"save success\")\n\t\t\tuassert.Equal(t, tc.storageSize, storage.Size())\n\n\t\t\tr, found = storage.Get(index.Name(), tc.fieldValue)\n\t\t\turequire.True(t, found, \"record found\")\n\t\t\tuassert.Equal(t, tc.id, r.ID())\n\t\t\tuassert.Equal(t, tc.key, r.Key())\n\t\t\tuassert.Equal(t, tc.fieldValue, r.MustGet(\"name\"))\n\t\t})\n\t}\n}\n\nfunc TestRecordsetIterate(t *testing.T) {\n\tcases := []struct {\n\t\tname      string\n\t\trecordIDs []uint64\n\t\tsetup     func(*Storage)\n\t}{\n\t\t{\n\t\t\tname:      \"single record\",\n\t\t\trecordIDs: []uint64{1},\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:      \"two records\",\n\t\t\trecordIDs: []uint64{1, 2},\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"empty recordset\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tstorage := NewStorage(\"foo\")\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026storage)\n\t\t\t}\n\n\t\t\tvar (\n\t\t\t\trecords []Record\n\t\t\t\trs      = storage.MustQuery()\n\t\t\t)\n\n\t\t\t// Act\n\t\t\trs.Iterate(func(r Record) bool {\n\t\t\t\trecords = append(records, r)\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\t// Assert\n\t\t\turequire.Equal(t, len(tc.recordIDs), len(records), \"results count\")\n\t\t\tfor i, r := range records {\n\t\t\t\tuassert.Equal(t, tc.recordIDs[i], r.ID())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRecordsetReverseIterate(t *testing.T) {\n\tcases := []struct {\n\t\tname      string\n\t\trecordIDs []uint64\n\t\tsetup     func(*Storage)\n\t}{\n\t\t{\n\t\t\tname:      \"single record\",\n\t\t\trecordIDs: []uint64{1},\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:      \"two records\",\n\t\t\trecordIDs: []uint64{2, 1},\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"empty recordser\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tstorage := NewStorage(\"foo\")\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026storage)\n\t\t\t}\n\n\t\t\tvar (\n\t\t\t\trecords []Record\n\t\t\t\trs      = storage.MustQuery()\n\t\t\t)\n\n\t\t\t// Act\n\t\t\trs.ReverseIterate(func(r Record) bool {\n\t\t\t\trecords = append(records, r)\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\t// Assert\n\t\t\turequire.Equal(t, len(tc.recordIDs), len(records), \"results count\")\n\t\t\tfor i, r := range records {\n\t\t\t\tuassert.Equal(t, tc.recordIDs[i], r.ID())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRecordsetSize(t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\tsize  int\n\t\tsetup func(*Storage)\n\t}{\n\t\t{\n\t\t\tname: \"single record\",\n\t\t\tsize: 1,\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"two records\",\n\t\t\tsize: 2,\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"empty recordser\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tstorage := NewStorage(\"foo\")\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026storage)\n\t\t\t}\n\n\t\t\trs := storage.MustQuery()\n\n\t\t\t// Act\n\t\t\tsize := rs.Size()\n\n\t\t\t// Assert\n\t\t\tuassert.Equal(t, tc.size, size)\n\t\t})\n\t}\n}\n"},{"name":"schema.gno","body":"package datastore\n\nimport (\n\t\"encoding/binary\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/list\"\n)\n\n// TODO: Support versioning\n\n// Schema contains information about fields and default field values.\n// It also offers the possibility to configure it as static to indicate\n// that only configured fields should be allowed.\ntype Schema struct {\n\tname     string\n\tstrict   bool\n\tfields   list.List // int(field index) -\u003e string(field name)\n\tdefaults avl.Tree  // string(field index) -\u003e interface{}\n}\n\n// NewSchema creates a new schema.\nfunc NewSchema(name string, options ...SchemaOption) *Schema {\n\ts := \u0026Schema{name: name}\n\tfor _, apply := range options {\n\t\tapply(s)\n\t}\n\treturn s\n}\n\n// Name returns schema's name.\nfunc (s Schema) Name() string {\n\treturn s.name\n}\n\n// Fields returns the list field names that are defined in the schema.\nfunc (s Schema) Fields() []string {\n\tfields := make([]string, s.fields.Len())\n\ts.fields.ForEach(func(i int, v interface{}) bool {\n\t\tfields[i] = v.(string)\n\t\treturn false\n\t})\n\treturn fields\n}\n\n// Size returns the number of fields the schema has.\nfunc (s Schema) Size() int {\n\treturn s.fields.Len()\n}\n\n// IsStrict check if the schema is configured as a strict one.\nfunc (s Schema) IsStrict() bool {\n\treturn s.strict\n}\n\n// HasField check is a field has been defined in the schema.\nfunc (s Schema) HasField(name string) bool {\n\treturn s.GetFieldIndex(name) \u003e= 0\n}\n\n// AddField adds a new field to the schema.\n// A default field value can be specified, otherwise `defaultValue` must be nil.\nfunc (s *Schema) AddField(name string, defaultValue interface{}) (index int, added bool) {\n\tif s.HasField(name) {\n\t\treturn -1, false\n\t}\n\n\ts.fields.Append(name)\n\tindex = s.fields.Len() - 1\n\tif defaultValue != nil {\n\t\tkey := castIntToKey(index)\n\t\ts.defaults.Set(key, defaultValue)\n\t}\n\treturn index, true\n}\n\n// GetFieldIndex returns the index number of a schema field.\n//\n// Field index indicates the order the field has within the schema.\n// When defined fields are added they get an index starting from\n// field index 0.\n//\n// Fields are internally referenced by index number instead of the name\n// to be able to rename fields easily.\nfunc (s Schema) GetFieldIndex(name string) int {\n\tindex := -1\n\ts.fields.ForEach(func(i int, v interface{}) bool {\n\t\tif name != v.(string) {\n\t\t\treturn false\n\t\t}\n\n\t\tindex = i\n\t\treturn true\n\t})\n\treturn index\n}\n\n// GetFieldName returns the name of a field for a specific field index.\nfunc (s Schema) GetFieldName(index int) (name string, found bool) {\n\tv := s.fields.Get(index)\n\tif v == nil {\n\t\treturn \"\", false\n\t}\n\treturn v.(string), true\n}\n\n// GetDefault returns the default value for a field.\nfunc (s Schema) GetDefault(name string) (value interface{}, found bool) {\n\ti := s.GetFieldIndex(name)\n\tif i == -1 {\n\t\treturn nil, false\n\t}\n\treturn s.GetDefaultByIndex(i)\n}\n\n// GetDefaultByIndex returns the default value for a field by it's index.\nfunc (s Schema) GetDefaultByIndex(index int) (value interface{}, found bool) {\n\tkey := castIntToKey(index)\n\tv := s.defaults.Get(key)\n\tif v == nil {\n\t\treturn nil, false\n\t}\n\n\tif fn, ok := v.(func() interface{}); ok {\n\t\treturn fn(), true\n\t}\n\treturn v, true\n}\n\n// RenameField renames a field.\nfunc (s *Schema) RenameField(name, newName string) (renamed bool) {\n\tif s.HasField(newName) {\n\t\treturn false\n\t}\n\n\ti := s.GetFieldIndex(name)\n\tif i == -1 {\n\t\treturn false\n\t}\n\n\ts.fields.Set(i, newName)\n\treturn true\n}\n\nfunc castIntToKey(i int) string {\n\tbuf := make([]byte, 8)\n\tbinary.BigEndian.PutUint64(buf, uint64(i))\n\treturn string(buf)\n}\n"},{"name":"schema_options.gno","body":"package datastore\n\nimport \"strings\"\n\n// StorageOption configures schemas.\ntype SchemaOption func(*Schema)\n\n// WithField assign a new field to the schema definition.\nfunc WithField(name string) SchemaOption {\n\treturn func(s *Schema) {\n\t\tname = strings.TrimSpace(name)\n\t\tif name != \"\" {\n\t\t\ts.fields.Append(name)\n\t\t}\n\t}\n}\n\n// WithDefaultField assign a new field with a default value to the schema definition.\n// Default value is assigned to newly created records asociated to to schema.\nfunc WithDefaultField(name string, value interface{}) SchemaOption {\n\treturn func(s *Schema) {\n\t\tname = strings.TrimSpace(name)\n\t\tif name != \"\" {\n\t\t\ts.fields.Append(name)\n\n\t\t\tkey := castIntToKey(s.fields.Len() - 1)\n\t\t\ts.defaults.Set(key, value)\n\t\t}\n\t}\n}\n\n// Strict configures the schema as a strict one.\n// By default schemas should allow the creation of any user defined field,\n// making them strict limits the allowed record fields to the ones pre-defined\n// in the schema. Fields are pre-defined using `WithField`, `WithDefaultField`\n// or by calling `Schema.AddField()`.\nfunc Strict() SchemaOption {\n\treturn func(s *Schema) {\n\t\ts.strict = true\n\t}\n}\n"},{"name":"schema_test.gno","body":"package datastore\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestSchemaNew(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\toptions []SchemaOption\n\t\tfields  []string\n\t\tstrict  bool\n\t}{\n\t\t{\n\t\t\tname: \"default\",\n\t\t},\n\t\t{\n\t\t\tname:    \"strict\",\n\t\t\toptions: []SchemaOption{Strict()},\n\t\t\tstrict:  true,\n\t\t},\n\t\t{\n\t\t\tname: \"with fields\",\n\t\t\toptions: []SchemaOption{\n\t\t\t\tWithField(\"foo\"),\n\t\t\t\tWithField(\"bar\"),\n\t\t\t\tWithDefaultField(\"baz\", 42),\n\t\t\t},\n\t\t\tfields: []string{\"foo\", \"bar\", \"baz\"},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\ts := NewSchema(\"Foo\", tc.options...)\n\n\t\t\tuassert.Equal(t, \"Foo\", s.Name())\n\t\t\tuassert.Equal(t, tc.strict, s.IsStrict())\n\t\t\turequire.Equal(t, len(tc.fields), s.Size(), \"field count\")\n\n\t\t\tfor i, name := range s.Fields() {\n\t\t\t\tuassert.Equal(t, tc.fields[i], name)\n\t\t\t\tuassert.True(t, s.HasField(name))\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSchemaAddField(t *testing.T) {\n\tcases := []struct {\n\t\tname       string\n\t\toptions    []SchemaOption\n\t\tfieldName  string\n\t\tfieldIndex int\n\t\tfields     []string\n\t\tsuccess    bool\n\t}{\n\t\t{\n\t\t\tname:       \"new only field\",\n\t\t\tfieldName:  \"foo\",\n\t\t\tfieldIndex: 0,\n\t\t\tfields:     []string{\"foo\"},\n\t\t\tsuccess:    true,\n\t\t},\n\t\t{\n\t\t\tname: \"new existing fields\",\n\t\t\toptions: []SchemaOption{\n\t\t\t\tWithField(\"foo\"),\n\t\t\t\tWithField(\"bar\"),\n\t\t\t},\n\t\t\tfieldName:  \"baz\",\n\t\t\tfieldIndex: 2,\n\t\t\tfields:     []string{\"foo\", \"bar\", \"baz\"},\n\t\t\tsuccess:    true,\n\t\t},\n\t\t{\n\t\t\tname:       \"duplicated field\",\n\t\t\toptions:    []SchemaOption{WithField(\"foo\")},\n\t\t\tfieldName:  \"foo\",\n\t\t\tfieldIndex: -1,\n\t\t\tfields:     []string{\"foo\"},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\ts := NewSchema(\"Foo\", tc.options...)\n\n\t\t\tindex, added := s.AddField(tc.fieldName, nil)\n\n\t\t\tif tc.success {\n\t\t\t\tuassert.Equal(t, tc.fieldIndex, index)\n\t\t\t\tuassert.True(t, added)\n\t\t\t} else {\n\t\t\t\tuassert.Equal(t, -1, index)\n\t\t\t\tuassert.False(t, added)\n\t\t\t}\n\n\t\t\turequire.Equal(t, len(tc.fields), s.Size(), \"field count\")\n\n\t\t\tfor i, name := range s.Fields() {\n\t\t\t\tuassert.Equal(t, tc.fields[i], name)\n\t\t\t\tuassert.True(t, s.HasField(name))\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestSchemaGetFieldIndex(t *testing.T) {\n\ts := NewSchema(\"Foo\")\n\ts.AddField(\"foo\", nil)\n\ts.AddField(\"bar\", nil)\n\ts.AddField(\"baz\", nil)\n\n\tuassert.Equal(t, 0, s.GetFieldIndex(\"foo\"))\n\tuassert.Equal(t, 1, s.GetFieldIndex(\"bar\"))\n\tuassert.Equal(t, 2, s.GetFieldIndex(\"baz\"))\n\n\tuassert.Equal(t, -1, s.GetFieldIndex(\"\"))\n\tuassert.Equal(t, -1, s.GetFieldIndex(\"unknown\"))\n}\n\nfunc TestSchemaGetFieldName(t *testing.T) {\n\ts := NewSchema(\"Foo\")\n\ts.AddField(\"foo\", nil)\n\ts.AddField(\"bar\", nil)\n\ts.AddField(\"baz\", nil)\n\n\tname, found := s.GetFieldName(0)\n\tuassert.Equal(t, \"foo\", name)\n\tuassert.True(t, found)\n\n\tname, found = s.GetFieldName(1)\n\tuassert.Equal(t, \"bar\", name)\n\tuassert.True(t, found)\n\n\tname, found = s.GetFieldName(2)\n\tuassert.Equal(t, \"baz\", name)\n\tuassert.True(t, found)\n\n\tname, found = s.GetFieldName(404)\n\tuassert.Equal(t, \"\", name)\n\tuassert.False(t, found)\n}\n\nfunc TestSchemaGetDefault(t *testing.T) {\n\ts := NewSchema(\"Foo\")\n\ts.AddField(\"foo\", nil)\n\ts.AddField(\"bar\", 42)\n\n\t_, found := s.GetDefault(\"foo\")\n\tuassert.False(t, found)\n\n\tv, found := s.GetDefault(\"bar\")\n\tuassert.True(t, found)\n\n\tgot, ok := v.(int)\n\turequire.True(t, ok, \"default field value\")\n\tuassert.Equal(t, 42, got)\n}\n\nfunc TestSchemaGetDefaultByIndex(t *testing.T) {\n\ts := NewSchema(\"Foo\")\n\ts.AddField(\"foo\", nil)\n\ts.AddField(\"bar\", 42)\n\n\t_, found := s.GetDefaultByIndex(0)\n\tuassert.False(t, found)\n\n\t_, found = s.GetDefaultByIndex(404)\n\tuassert.False(t, found)\n\n\tv, found := s.GetDefaultByIndex(1)\n\tuassert.True(t, found)\n\n\tgot, ok := v.(int)\n\turequire.True(t, ok, \"default field value\")\n\tuassert.Equal(t, 42, got)\n}\n\nfunc TestSchemaRenameField(t *testing.T) {\n\ts := NewSchema(\"Foo\")\n\ts.AddField(\"foo\", nil)\n\ts.AddField(\"bar\", nil)\n\n\trenamed := s.RenameField(\"foo\", \"bar\")\n\tuassert.False(t, renamed)\n\n\trenamed = s.RenameField(\"\", \"baz\")\n\tuassert.False(t, renamed)\n\n\trenamed = s.RenameField(\"foo\", \"\")\n\tuassert.True(t, renamed)\n\n\trenamed = s.RenameField(\"\", \"foo\")\n\tuassert.True(t, renamed)\n\n\trenamed = s.RenameField(\"foo\", \"foobar\")\n\tuassert.True(t, renamed)\n\n\turequire.Equal(t, 2, s.Size(), \"field count\")\n\tfields := []string{\"foobar\", \"bar\"}\n\tfor i, name := range s.Fields() {\n\t\tuassert.Equal(t, fields[i], name)\n\t\tuassert.True(t, s.HasField(name))\n\t}\n}\n"},{"name":"storage.gno","body":"package datastore\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/collection\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// NewStorage creates a new records storage.\nfunc NewStorage(name string, options ...StorageOption) Storage {\n\ts := Storage{\n\t\tname:       name,\n\t\tcollection: collection.New(),\n\t\tschema:     NewSchema(strings.Title(name)),\n\t}\n\n\tfor _, apply := range options {\n\t\tapply(\u0026s)\n\t}\n\treturn s\n}\n\n// Storage stores a collection of records.\n//\n// By default it searches records by record ID but it allows\n// using custom user defined indexes for other record fields.\n//\n// When a storage is created it defines a default schema that\n// keeps track of record fields. Storage can be optionally\n// created with a user defined schema in cases where the number\n// of fields has to be pre-defined or when new records must have\n// one or more fields initialized to default values.\ntype Storage struct {\n\tname       string\n\tcollection *collection.Collection\n\tschema     *Schema\n}\n\n// Name returns storage's name.\nfunc (s Storage) Name() string {\n\treturn s.name\n}\n\n// Collection returns the undelying collection used by the\n// storage to store all records.\nfunc (s Storage) Collection() *collection.Collection {\n\treturn s.collection\n}\n\n// Schema returns the schema being used to track record fields.\nfunc (s Storage) Schema() *Schema {\n\treturn s.schema\n}\n\n// Size returns the number of records that the storage have.\nfunc (s Storage) Size() int {\n\treturn s.collection.GetIndex(collection.IDIndex).Size()\n}\n\n// NewRecord creates a new storage record.\n//\n// If a custom schema with default field values is assigned to\n// storage it's used to assign initial default values when new\n// records are created.\n//\n// Creating a new record doesn't assign an ID to it, a new ID\n// is generated and assigned to the record when it's saved for\n// the first time.\nfunc (s Storage) NewRecord() Record {\n\tr := \u0026record{\n\t\tschema:     s.schema,\n\t\tcollection: s.collection,\n\t}\n\n\t// Assign default record values if the schema defines them\n\tfor i, name := range s.schema.Fields() {\n\t\tif v, found := s.schema.GetDefaultByIndex(i); found {\n\t\t\tr.Set(name, v)\n\t\t}\n\t}\n\treturn r\n}\n\n// Query returns a recordset that matches the query parameters.\n// By default query selects records using the ID index.\n//\n// Example usage:\n//\n//\t// Get 50 records starting from the one at position 100\n//\trs, _ := storage.Query(\n//\t\tWithOffset(100),\n//\t\tWithSize(50),\n//\t)\n//\n//\t// Iterate records to create a new slice\n//\tvar records []Record\n//\trs.Iterate(func (r Record) bool {\n//\t\trecords = append(records, r)\n//\t\treturn false\n//\t})\nfunc (s Storage) Query(options ...QueryOption) (Recordset, error) {\n\t// Initialize the recordset for the query\n\trs := recordset{\n\t\tquery:   defaultQuery,\n\t\trecords: s.collection.GetIndex(collection.IDIndex),\n\t}\n\n\tfor _, apply := range options {\n\t\tif err := apply(\u0026rs.query); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\tindexName := rs.query.IndexName()\n\tif indexName != collection.IDIndex {\n\t\t// When using a custom index get the keys to get records from the ID index\n\t\tkeys, err := s.getIndexRecordsKeys(indexName, rs.query.IndexKey())\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\t// Adjust the number of keys to match available query options\n\t\tif offset := rs.query.Offset(); offset \u003e 0 {\n\t\t\tif offset \u003e len(keys) {\n\t\t\t\tkeys = nil\n\t\t\t} else {\n\t\t\t\tkeys = keys[offset:]\n\t\t\t}\n\t\t}\n\n\t\tif size := rs.query.Size(); size \u003e 0 \u0026\u0026 size \u003c len(keys) {\n\t\t\tkeys = keys[:size]\n\t\t}\n\n\t\trs.keys = keys\n\t\trs.size = len(keys)\n\t} else {\n\t\t// When using the default ID index init size with the total number of records\n\t\trs.size = rs.records.Size()\n\n\t\t// Adjust recordset size to match available query options\n\t\tif offset := rs.query.Offset(); offset \u003e 0 {\n\t\t\tif offset \u003e rs.size {\n\t\t\t\trs.size = 0\n\t\t\t} else {\n\t\t\t\trs.size -= offset\n\t\t\t}\n\t\t}\n\n\t\tif size := rs.query.Size(); size \u003e 0 \u0026\u0026 size \u003c rs.size {\n\t\t\trs.size = size\n\t\t}\n\t}\n\n\treturn rs, nil\n}\n\n// MustQuery returns a recordset that matches the query parameters or panics on error.\n// By default query selects records using the ID index.\n//\n// Example usage:\n//\n//\t// Get 50 records starting from the one at position 100\n//\tvar records []Record\n//\tstorage.MustQuery(\n//\t\tWithOffset(100),\n//\t\tWithSize(50),\n//\t).Iterate(func (r Record) bool {\n//\t\trecords = append(records, r)\n//\t\treturn false\n//\t})\nfunc (s Storage) MustQuery(options ...QueryOption) Recordset {\n\trs, err := s.Query(options...)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn rs\n}\n\n// Get returns the first record found for a key within a storage index.\n//\n// This is a convenience method to get a single record. A multi index will\n// always return the first record value for the specified key in this case.\n// To get multiple records create a query using a custom index and key value\n// or use the underlying storage collection.\nfunc (s Storage) Get(indexName, indexKey string) (_ Record, found bool) {\n\titer := s.collection.Get(indexName, indexKey)\n\tif iter.Next() {\n\t\treturn iter.Value().Obj.(Record), true\n\t}\n\treturn nil, false\n}\n\n// GetByID returns a record whose ID matches the specified ID.\nfunc (s Storage) GetByID(id uint64) (_ Record, found bool) {\n\titer := s.collection.Get(collection.IDIndex, seqid.ID(id).String())\n\tif iter.Next() {\n\t\treturn iter.Value().Obj.(Record), true\n\t}\n\treturn nil, false\n}\n\n// Delete deletes a record from the storage.\nfunc (s Storage) Delete(id uint64) bool {\n\treturn s.collection.Delete(id)\n}\n\nfunc (s Storage) getIndexRecordsKeys(indexName, indexKey string) ([]string, error) {\n\tidx := s.collection.GetIndex(indexName)\n\tif idx == nil {\n\t\treturn nil, errors.New(\"storage index for query not found: \" + indexName)\n\t}\n\n\tvar keys []string\n\tif v := idx.Get(indexKey); v != nil {\n\t\tkeys = castIfaceToRecordKeys(v)\n\t\tif keys == nil {\n\t\t\treturn nil, errors.New(\"unexpected storage index key format\")\n\t\t}\n\t}\n\treturn keys, nil\n}\n\nfunc castIfaceToRecordKeys(v interface{}) []string {\n\tswitch k := v.(type) {\n\tcase []string:\n\t\treturn k\n\tcase string:\n\t\treturn []string{k}\n\t}\n\treturn nil\n}\n"},{"name":"storage_options.gno","body":"package datastore\n\n// StorageOption configures storages.\ntype StorageOption func(*Storage)\n\n// WithSchema assigns a schema to the storage.\nfunc WithSchema(s *Schema) StorageOption {\n\treturn func(st *Storage) {\n\t\tif s != nil {\n\t\t\tst.schema = s\n\t\t}\n\t}\n}\n\n// WithIndex assigns an index to the storage.\nfunc WithIndex(i Index) StorageOption {\n\treturn func(st *Storage) {\n\t\tst.collection.AddIndex(i.name, i.fn, i.options)\n\t}\n}\n"},{"name":"storage_test.gno","body":"package datastore\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestStorageDefaults(t *testing.T) {\n\tname := \"foo\"\n\tstorage := NewStorage(name)\n\n\tuassert.Equal(t, name, storage.Name())\n\tuassert.NotEqual(t, nil, storage.Collection())\n\tuassert.Equal(t, 0, storage.Size())\n\n\ts := storage.Schema()\n\tuassert.NotEqual(t, nil, s)\n\tuassert.Equal(t, strings.Title(name), s.Name())\n}\n\nfunc TestStorageNewRecord(t *testing.T) {\n\tfield := \"status\"\n\tdefaultValue := \"testing\"\n\ts := NewSchema(\"Foo\", WithDefaultField(field, defaultValue))\n\tstorage := NewStorage(\"foo\", WithSchema(s))\n\n\tr := storage.NewRecord()\n\turequire.NotEqual(t, nil, r, \"new record is not nil\")\n\tuassert.Equal(t, uint64(0), r.ID())\n\tuassert.Equal(t, storage.Schema().Name(), r.Type())\n\n\tv, found := r.Get(field)\n\turequire.True(t, found, \"default value found\")\n\n\tgot, ok := v.(string)\n\turequire.True(t, ok, \"default value type\")\n\tuassert.Equal(t, defaultValue, got)\n}\n\nfunc TestStorageQuery(t *testing.T) {\n\tindex := NewIndex(\"tag\", func(r Record) string {\n\t\tif v, found := r.Get(\"tag\"); found {\n\t\t\treturn v.(string)\n\t\t}\n\t\treturn \"\"\n\t})\n\n\tcases := []struct {\n\t\tname    string\n\t\toptions []QueryOption\n\t\tresults []uint64\n\t\tsetup   func() *Storage\n\t\terrMsg  string\n\t}{\n\t\t{\n\t\t\tname:    \"default query\",\n\t\t\tresults: []uint64{1, 2},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\")\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with size\",\n\t\t\tresults: []uint64{1},\n\t\t\toptions: []QueryOption{WithSize(1)},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\")\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with offset\",\n\t\t\tresults: []uint64{2},\n\t\t\toptions: []QueryOption{WithOffset(1)},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\")\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with offset overflow\",\n\t\t\toptions: []QueryOption{WithOffset(4)},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\")\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"with size and offset\",\n\t\t\tresults: []uint64{2, 3},\n\t\t\toptions: []QueryOption{WithSize(2), WithOffset(1)},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\")\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"custom index\",\n\t\t\toptions: []QueryOption{UseIndex(\"tag\", \"A\")},\n\t\t\tresults: []uint64{1, 3},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\", WithIndex(index))\n\n\t\t\t\tr := s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"A\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"B\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"A\")\n\t\t\t\tr.Save()\n\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"custom index with offset\",\n\t\t\toptions: []QueryOption{UseIndex(\"tag\", \"B\"), WithOffset(1)},\n\t\t\tresults: []uint64{3, 4},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\", WithIndex(index))\n\n\t\t\t\tr := s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"B\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"A\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"B\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"B\")\n\t\t\t\tr.Save()\n\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"custom index with offset and size\",\n\t\t\toptions: []QueryOption{UseIndex(\"tag\", \"B\"), WithOffset(1), WithSize(1)},\n\t\t\tresults: []uint64{3},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\", WithIndex(index))\n\n\t\t\t\tr := s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"B\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"A\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"B\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"tag\", \"B\")\n\t\t\t\tr.Save()\n\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:    \"custom index not found\",\n\t\t\toptions: []QueryOption{UseIndex(\"foo\", \"B\")},\n\t\t\tsetup: func() *Storage {\n\t\t\t\ts := NewStorage(\"foo\")\n\t\t\t\treturn \u0026s\n\t\t\t},\n\t\t\terrMsg: \"storage index for query not found: foo\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tstorage := tc.setup()\n\n\t\t\t// Act\n\t\t\trs, err := storage.Query(tc.options...)\n\n\t\t\t// Assert\n\t\t\tif tc.errMsg != \"\" {\n\t\t\t\tuassert.ErrorContains(t, err, tc.errMsg, \"expect error\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\turequire.NotEqual(t, nil, rs, \"new record is not nil\")\n\t\t\turequire.Equal(t, len(tc.results), rs.Size(), \"expect query results count to match\")\n\n\t\t\tvar i int\n\t\t\trs.Iterate(func(r Record) bool {\n\t\t\t\turequire.Equal(t, tc.results[i], r.ID(), \"expect result IDs to match\")\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestStorageGet(t *testing.T) {\n\tindex := NewIndex(\"name\", func(r Record) string {\n\t\tif v, found := r.Get(\"name\"); found {\n\t\t\treturn v.(string)\n\t\t}\n\t\treturn \"\"\n\t})\n\n\tcases := []struct {\n\t\tname     string\n\t\tkey      string\n\t\trecordID uint64\n\t\tsetup    func(*Storage)\n\t}{\n\t\t{\n\t\t\tname:     \"single record\",\n\t\t\tkey:      \"foobar\",\n\t\t\trecordID: 1,\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\tr := s.NewRecord()\n\t\t\t\tr.Set(\"name\", \"foobar\")\n\t\t\t\tr.Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"two records\",\n\t\t\tkey:      \"foobar\",\n\t\t\trecordID: 1,\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\tr := s.NewRecord()\n\t\t\t\tr.Set(\"name\", \"foobar\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"name\", \"foobar\")\n\t\t\t\tr.Save()\n\n\t\t\t\tr = s.NewRecord()\n\t\t\t\tr.Set(\"name\", \"extra\")\n\t\t\t\tr.Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"record not found\",\n\t\t\tkey:  \"unknown\",\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\tr := s.NewRecord()\n\t\t\t\tr.Set(\"name\", \"foobar\")\n\t\t\t\tr.Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"empty storage\",\n\t\t\tkey:  \"foobar\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tstorage := NewStorage(\"foo\", WithIndex(index))\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026storage)\n\t\t\t}\n\n\t\t\tr, found := storage.Get(index.Name(), tc.key)\n\n\t\t\tif tc.recordID == 0 {\n\t\t\t\tuassert.Equal(t, nil, r, \"expect no record\")\n\t\t\t\tuassert.False(t, found, \"expect record not found\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tuassert.True(t, found, \"expect record found\")\n\t\t\turequire.NotEqual(t, nil, r, \"expect record to be found\")\n\t\t\tuassert.Equal(t, tc.recordID, r.ID(), \"expect ID to match\")\n\t\t})\n\t}\n}\n\nfunc TestStorageGetByID(t *testing.T) {\n\tcases := []struct {\n\t\tname     string\n\t\trecordID uint64\n\t\tfound    bool\n\t\tsetup    func(*Storage)\n\t}{\n\t\t{\n\t\t\tname:     \"single record\",\n\t\t\trecordID: 1,\n\t\t\tfound:    true,\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple records\",\n\t\t\trecordID: 2,\n\t\t\tfound:    true,\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"record not found\",\n\t\t\trecordID: 3,\n\t\t\tsetup: func(s *Storage) {\n\t\t\t\ts.NewRecord().Save()\n\t\t\t\ts.NewRecord().Save()\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty storage\",\n\t\t\trecordID: 1,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tstorage := NewStorage(\"foo\")\n\t\t\tif tc.setup != nil {\n\t\t\t\ttc.setup(\u0026storage)\n\t\t\t}\n\n\t\t\tr, found := storage.GetByID(tc.recordID)\n\n\t\t\tif !tc.found {\n\t\t\t\tuassert.Equal(t, nil, r, \"expect no record\")\n\t\t\t\tuassert.False(t, found, \"expect record not found\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tuassert.True(t, found, \"expect record found\")\n\t\t\turequire.NotEqual(t, nil, r, \"expect record to be found\")\n\t\t\tuassert.Equal(t, tc.recordID, r.ID(), \"expect ID to match\")\n\t\t})\n\t}\n}\n\nfunc TestStorageDelete(t *testing.T) {\n\tstorage := NewStorage(\"foo\")\n\tr := storage.NewRecord()\n\tr.Save()\n\n\tdeleted := storage.Delete(r.ID())\n\tuassert.True(t, deleted)\n\n\tdeleted = storage.Delete(r.ID())\n\tuassert.False(t, deleted)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ztN/NRZEZmQ4d/6NOH9QuC54ZFSsYF76anyA0uZ8dV9NK4w9T9HqJ6ha7I4ep7+cI2aILDWhkPlmS9hcpU3SBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"murmur3","path":"gno.land/p/jeronimoalbi/murmur3","files":[{"name":"README.md","body":"# MurmurHash3 Package\n\nPackage implements Austin Appleby's MurmurHash3 algorithm.\n\nMurmurHash3 is a fast, non-cryptographic hash function well suited for hash tables,\nbloom filters, data partitioning, and deduplication where speed and good distribution\nmatter more than cryptographic security.\n\n## API\n\n- `New32()` — Returns a `hash.Hash32` with seed zero\n- `NewWithSeed32(seed)` — Returns a `hash.Hash32` with the given seed\n- `Sum32(data)` — One-shot hash with seed zero\n- `Sum32WithSeed(data, seed)` — One-shot hash with the given seed\n- `New64()` — Returns a `hash.Hash64` using seeds zero and one\n- `NewWithSeed64(seed1, seed2)` — Returns a `hash.Hash64` with the given seeds\n- `Sum64(data)` — One-shot 64-bit hash using seeds zero and one\n- `Sum64WithSeed(data, seed1, seed2)` — One-shot 64-bit hash with the given seeds\n- `EncodeToString(sum64)` — Returns the hexadecimal encoding of a hash value\n\n## Usage\n\n[embedmd]:# (filetests/readme_filetest.gno go)\n```go\npackage main\n\nimport \"gno.land/p/jeronimoalbi/murmur3\"\n\nfunc main() {\n\tdata := []byte(\"Hello, world!\")\n\tseed := uint32(42)\n\n\t// Hash32 without seed\n\th32 := murmur3.New32()\n\th32.Write(data)\n\tsum32 := uint64(h32.Sum32())\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash32 with seed\n\th32 = murmur3.NewWithSeed32(seed)\n\th32.Write(data)\n\tsum32 = uint64(h32.Sum32())\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash32 without seed using a helper function\n\tsum32 = uint64(murmur3.Sum32(data))\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash32 with seed using a helper function\n\tsum32 = uint64(murmur3.Sum32WithSeed(data, seed))\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash64 without seed\n\th64 := murmur3.New64()\n\th64.Write(data)\n\tsum64 := h64.Sum64()\n\tprintln(murmur3.EncodeToString(sum64))\n\n\t// Hash64 with seed\n\th64 = murmur3.NewWithSeed64(0, seed)\n\th64.Write(data)\n\tsum64 = h64.Sum64()\n\tprintln(murmur3.EncodeToString(sum64))\n\n\t// Hash64 without seed using a helper function\n\tsum64 = murmur3.Sum64(data)\n\tprintln(murmur3.EncodeToString(sum64))\n\n\t// Hash64 with seed using a helper function\n\tsum64 = murmur3.Sum64WithSeed(data, 0, seed)\n\tprintln(murmur3.EncodeToString(sum64))\n}\n\n// Output:\n// c0363e43\n// 2c8c8533\n// c0363e43\n// 2c8c8533\n// c0363e43aa5dc85b\n// c0363e432c8c8533\n// c0363e43aa5dc85b\n// c0363e432c8c8533\n```\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/jeronimoalbi/murmur3\"\ngno = \"0.9\"\n"},{"name":"murmur32.gno","body":"// Package murmur3 implements the MurmurHash3 hash algorithm.\n//\n// Package implements Austin Appleby's MurmurHash3 algorithm.\n//\n// MurmurHash3 is a fast, non-cryptographic hash function well suited for hash\n// tables, bloom filters, data partitioning, and deduplication where speed and\n// good distribution matter more than cryptographic security.\n//\n// References:\n//\n//\thttps://en.wikipedia.org/wiki/MurmurHash#Algorithm\n//\thttps://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp\npackage murmur3\n\nimport \"strconv\"\n\n// EncodeToString returns the hexadecimal encoding of a hash value.\nfunc EncodeToString(sum uint64) string {\n\treturn strconv.FormatUint(sum, 16)\n}\n"},{"name":"murmur3_32.gno","body":"package murmur3\n\nimport (\n\t\"hash\"\n\t\"math/bits\"\n)\n\nconst (\n\tc1          = 0xcc9e2d51\n\tc2          = 0x1b873593\n\tr1          = 15\n\tr2          = 13\n\tblockSize32 = 4\n\thashSize32  = 4\n)\n\ntype digest32 struct {\n\tseed           uint32\n\th1             uint32\n\ttail           [4]byte\n\ttailBytesCount int\n\twrittenLen     int\n}\n\n// New32 returns a new hash.Hash32 computing the MurmurHash3 hash.\n// It can generate 2 to the power of 32 unique 32-bit values ranging\n// from 0 to 4_294_967_296.\n// Hash is not collision safe, so it's recommended to handle potential\n// collisions when dealing with more than 65_536 values which is when\n// probability raises.\nfunc New32() hash.Hash32 {\n\treturn \u0026digest32{}\n}\n\n// NewWithSeed32 returns a new hash.Hash32 computing the MurmurHash3\n// hash with the given seed.\n// It can generate 2 to the power of 32 unique 32-bit values ranging\n// from 0 to 4_294_967_296.\n// Hash is not collision safe, so it's recommended to handle potential\n// collisions when dealing with more than 65_536 values which is when\n// provability raises.\nfunc NewWithSeed32(seed uint32) hash.Hash32 {\n\treturn \u0026digest32{seed: seed, h1: seed}\n}\n\n// Sum32 returns the MurmurHash3 hash of the specified data.\nfunc Sum32(data []byte) uint32 {\n\treturn Sum32WithSeed(data, 0)\n}\n\n// Sum32WithSeed returns the MurmurHash3 hash of the specified data\n// using the given seed.\nfunc Sum32WithSeed(data []byte, seed uint32) uint32 {\n\th := NewWithSeed32(seed)\n\th.Write(data)\n\treturn h.Sum32()\n}\n\nfunc (digest32) BlockSize() int {\n\treturn blockSize32\n}\n\nfunc (digest32) Size() int {\n\treturn hashSize32\n}\n\nfunc (d *digest32) Reset() {\n\td.h1 = d.seed\n\td.tailBytesCount = 0\n\td.writtenLen = 0\n}\n\nfunc (d *digest32) Write(bz []byte) (int, error) {\n\t// Keep track of all written bytes\n\ttotal := len(bz)\n\td.writtenLen += total\n\n\t// If there are leftover bytes in the tail,\n\t// try to fill a block with the new data.\n\tif d.tailBytesCount \u003e 0 {\n\t\tneed := blockSize32 - d.tailBytesCount\n\t\tif len(bz) \u003c need {\n\t\t\t// Append more data to the block\n\t\t\tcopy(d.tail[d.tailBytesCount:], bz)\n\t\t\td.tailBytesCount += len(bz)\n\n\t\t\t// Tail block is still not filled\n\t\t\treturn total, nil\n\t\t}\n\n\t\t// Append remaining block data and process current block\n\t\tcopy(d.tail[d.tailBytesCount:], bz[:need])\n\t\td.processBlock(d.tail[:])\n\n\t\t// Update data to exclude the appended processed data\n\t\tbz = bz[need:]\n\t\td.tailBytesCount = 0\n\t}\n\n\t// Process full 4-byte blocks\n\tfor len(bz) \u003e= blockSize32 {\n\t\td.processBlock(bz[:blockSize32])\n\t\tbz = bz[blockSize32:]\n\t}\n\n\t// Save remaining bytes in the tail. Remaining data can be left\n\t// out of the last block when data size is less than 4 bytes.\n\tif len(bz) \u003e 0 {\n\t\td.tailBytesCount = copy(d.tail[:], bz)\n\t}\n\n\treturn total, nil\n}\n\nfunc (d digest32) Sum32() uint32 {\n\th1 := d.h1\n\n\t// Process tail bytes when they exist. Blocks have 4 bytes, so depending\n\t// on the size of the data some bytes might not be processed after the last\n\t// write if the last block has less than 4 bytes.\n\tvar k1 uint32\n\tswitch d.tailBytesCount {\n\tcase 3:\n\t\tk1 ^= uint32(d.tail[2]) \u003c\u003c 16\n\t\tfallthrough\n\tcase 2:\n\t\tk1 ^= uint32(d.tail[1]) \u003c\u003c 8\n\t\tfallthrough\n\tcase 1:\n\t\tk1 ^= uint32(d.tail[0])\n\t\tk1 *= c1\n\t\tk1 = bits.RotateLeft32(k1, r1)\n\t\tk1 *= c2\n\t\th1 ^= k1\n\t}\n\n\t// Finalization mix\n\th1 ^= uint32(d.writtenLen)\n\n\th1 ^= h1 \u003e\u003e 16\n\th1 *= 0x85ebca6b\n\th1 ^= h1 \u003e\u003e r2\n\th1 *= 0xc2b2ae35\n\th1 ^= h1 \u003e\u003e 16\n\n\treturn h1\n}\n\nfunc (d digest32) Sum(bz []byte) []byte {\n\t// Append hash in little-endian order\n\ts := d.Sum32()\n\treturn append(bz, byte(s), byte(s\u003e\u003e8), byte(s\u003e\u003e16), byte(s\u003e\u003e24))\n}\n\n// processBlock reads a 4-byte little-endian block\n// and mixes it into the hash state.\nfunc (d *digest32) processBlock(bz []byte) {\n\tk1 := uint32(bz[0]) | uint32(bz[1])\u003c\u003c8 | uint32(bz[2])\u003c\u003c16 | uint32(bz[3])\u003c\u003c24\n\tk1 *= c1\n\tk1 = bits.RotateLeft32(k1, r1)\n\tk1 *= c2\n\n\td.h1 ^= k1\n\td.h1 = bits.RotateLeft32(d.h1, r2)\n\td.h1 = d.h1*5 + 0xe6546b64 // MurmurHash3 \"m\" = 5, \"n\" = 0xe6546b64\n}\n"},{"name":"murmur3_32_test.gno","body":"package murmur3_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/murmur3\"\n)\n\nfunc TestHash32Sum32(t *testing.T) {\n\t// Test cases -\u003e https://en.wikipedia.org/wiki/MurmurHash#Algorithm\n\ttests := []struct {\n\t\tname string\n\t\tdata string\n\t\tseed uint32\n\t\twant uint32\n\t}{\n\t\t{\"empty with seed 0\", \"\", 0, 0},\n\t\t{\"empty with seed 1\", \"\", 1, 0x514e28b7},\n\t\t{\"empty with seed max\", \"\", 0xffffffff, 0x81f16f39},\n\t\t{\"test\", \"test\", 0, 0xba6bd213},\n\t\t{\"test with seed\", \"test\", 0x9747b28c, 0x704b81dc},\n\t\t{\"hello\", \"Hello, world!\", 0, 0xc0363e43},\n\t\t{\"hello with seed\", \"Hello, world!\", 42, 0x2c8c8533},\n\t\t{\"hello with seed 0x9747b28c\", \"Hello, world!\", 0x9747b28c, 0x24884cba},\n\t\t{\"pangram\", \"The quick brown fox jumps over the lazy dog\", 0, 0x2e4ff723},\n\t\t{\"pangram with seed\", \"The quick brown fox jumps over the lazy dog\", 0x9747b28c, 0x2fa826cd},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := murmur3.Sum32WithSeed([]byte(tt.data), tt.seed)\n\t\t\tif got != tt.want {\n\t\t\t\tt.Errorf(\"Sum32WithSeed(%q, %d) = 0x%08x, want 0x%08x\", tt.data, tt.seed, got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestHash32InterfaceSizes(t *testing.T) {\n\th := murmur3.New32()\n\tif h.Size() != 4 {\n\t\tt.Errorf(\"Size() = %d, want 4\", h.Size())\n\t}\n\tif h.BlockSize() != 4 {\n\t\tt.Errorf(\"BlockSize() = %d, want 4\", h.BlockSize())\n\t}\n}\n\nfunc TestHash32IncrementalWrite(t *testing.T) {\n\twant := uint32(3224780355)\n\tdata := []byte(\"Hello, world!\")\n\n\t// Hash one byte at a time\n\th := murmur3.New32()\n\tfor _, b := range data {\n\t\th.Write([]byte{b})\n\t}\n\n\tif got := h.Sum32(); got != want {\n\t\tt.Errorf(\"incremental Write got 0x%08x, want 0x%08x\", got, want)\n\t}\n\n\t// Hash in two parts\n\th.Reset()\n\th.Write(data[:5])\n\th.Write(data[5:])\n\tif got := h.Sum32(); got != want {\n\t\tt.Errorf(\"two-part Write got 0x%08x, want 0x%08x\", got, want)\n\t}\n}\n\nfunc TestHash32Sum(t *testing.T) {\n\th := murmur3.New32()\n\th.Write([]byte(\"Hello, world!\"))\n\tb := h.Sum(nil)\n\tif len(b) != 4 {\n\t\tt.Fatalf(\"Sum(nil) returned %d bytes, want 4\", len(b))\n\t}\n\n\tgot := uint32(b[0]) | uint32(b[1])\u003c\u003c8 | uint32(b[2])\u003c\u003c16 | uint32(b[3])\u003c\u003c24\n\twant := uint32(3224780355)\n\tif got != want {\n\t\tt.Errorf(\"Sum got 0x%08x, want 0x%08x\", got, want)\n\t}\n}\n\nfunc TestHash32SumDoesNotChangeState(t *testing.T) {\n\th := murmur3.New32()\n\th.Write([]byte(\"Hello\"))\n\ts1 := h.Sum32()\n\ts2 := h.Sum32()\n\tif s1 != s2 {\n\t\tt.Errorf(\"Sum32 changed state: first 0x%08x, second 0x%08x\", s1, s2)\n\t}\n}\n\nfunc TestHash32Reset(t *testing.T) {\n\th := murmur3.New32()\n\th.Write([]byte(\"some data\"))\n\th.Reset()\n\th.Write([]byte(\"Hello, world!\"))\n\tgot := h.Sum32()\n\twant := uint32(3224780355)\n\tif got != want {\n\t\tt.Errorf(\"after Reset got 0x%08x, want 0x%08x\", got, want)\n\t}\n}\n"},{"name":"murmur3_64.gno","body":"package murmur3\n\nimport \"hash\"\n\nconst hashSize64 = 8\n\ntype digest64 struct {\n\td1 digest32\n\td2 digest32\n}\n\n// New64 returns a new hash.Hash64 computing a 64-bit MurmurHash3\n// hash using two 32-bit hashes with different seeds.\n// It can generate 2 to the power of 64 unique 64-bit values.\n// Hash is not collision safe, so it's recommended to handle potential\n// collisions when dealing with more than 4_294_967_296 values which is\n// when probability raises.\nfunc New64() hash.Hash64 {\n\treturn \u0026digest64{\n\t\td1: digest32{},\n\t\td2: digest32{seed: 1, h1: 1},\n\t}\n}\n\n// NewWithSeed64 returns a new hash.Hash64 computing a 64-bit\n// MurmurHash3 hash. The first 32-bit hash uses seed1 and the\n// second uses seed2. The two seeds should ideally be different.\n// It can generate 2 to the power of 64 unique 64-bit values.\n// Hash is not collision safe, so it's recommended to handle potential\n// collisions when dealing with more than 4_294_967_296 values which is\n// when provability raises.\nfunc NewWithSeed64(seed1, seed2 uint32) hash.Hash64 {\n\treturn \u0026digest64{\n\t\td1: digest32{seed: seed1, h1: seed1},\n\t\td2: digest32{seed: seed2, h1: seed2},\n\t}\n}\n\n// Sum64 returns the 64-bit MurmurHash3 hash of the specified data.\nfunc Sum64(data []byte) uint64 {\n\treturn Sum64WithSeed(data, 0, 1)\n}\n\n// Sum64WithSeed returns the 64-bit MurmurHash3 hash of the specified\n// data using the given seeds.\nfunc Sum64WithSeed(data []byte, seed1, seed2 uint32) uint64 {\n\th := NewWithSeed64(seed1, seed2)\n\th.Write(data)\n\treturn h.Sum64()\n}\n\nfunc (digest64) BlockSize() int {\n\t// Use a 32-bit hash block size because writes are\n\t// done to two hash32 hashes using the same data.\n\treturn blockSize32\n}\n\nfunc (digest64) Size() int {\n\treturn hashSize64\n}\n\nfunc (d *digest64) Reset() {\n\td.d1.Reset()\n\td.d2.Reset()\n}\n\nfunc (d *digest64) Write(bz []byte) (int, error) {\n\td.d1.Write(bz)\n\treturn d.d2.Write(bz)\n}\n\nfunc (d digest64) Sum64() uint64 {\n\treturn uint64(d.d1.Sum32())\u003c\u003c32 | uint64(d.d2.Sum32())\n}\n\nfunc (d digest64) Sum(bz []byte) []byte {\n\ts := d.Sum64()\n\treturn append(bz,\n\t\tbyte(s), byte(s\u003e\u003e8), byte(s\u003e\u003e16), byte(s\u003e\u003e24),\n\t\tbyte(s\u003e\u003e32), byte(s\u003e\u003e40), byte(s\u003e\u003e48), byte(s\u003e\u003e56),\n\t)\n}\n"},{"name":"murmur3_64_test.gno","body":"package murmur3_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/murmur3\"\n)\n\nfunc TestHash64Sum64(t *testing.T) {\n\t// Test cases -\u003e https://en.wikipedia.org/wiki/MurmurHash#Algorithm\n\ttests := []struct {\n\t\tname string\n\t\tdata string\n\t\twant uint64\n\t}{\n\t\t{\"empty\", \"\", 0x00000000514e28b7},\n\t\t{\"test\", \"test\", 0xba6bd21399c02ae2},\n\t\t{\"hello\", \"Hello, world!\", 0xc0363e43aa5dc85b},\n\t\t{\"pangram\", \"The quick brown fox jumps over the lazy dog\", 0x2e4ff72378e69e27},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := murmur3.Sum64([]byte(tt.data))\n\t\t\tif got != tt.want {\n\t\t\t\tt.Errorf(\"Sum64(%q) = 0x%016x, want 0x%016x\", tt.data, got, tt.want)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestHash64InterfaceSizes(t *testing.T) {\n\th := murmur3.New64()\n\tif h.Size() != 8 {\n\t\tt.Errorf(\"Size() = %d, want 8\", h.Size())\n\t}\n\tif h.BlockSize() != 4 {\n\t\tt.Errorf(\"BlockSize() = %d, want 4\", h.BlockSize())\n\t}\n}\n\nfunc TestHash64IncrementalWrite(t *testing.T) {\n\twant := uint64(13850326164366542939)\n\tdata := []byte(\"Hello, world!\")\n\n\t// Hash one byte at a time\n\th := murmur3.New64()\n\tfor _, b := range data {\n\t\th.Write([]byte{b})\n\t}\n\n\tif got := h.Sum64(); got != want {\n\t\tt.Errorf(\"incremental Write got 0x%016x, want 0x%016x\", got, want)\n\t}\n\n\t// Hash in two parts\n\th.Reset()\n\th.Write(data[:5])\n\th.Write(data[5:])\n\tif got := h.Sum64(); got != want {\n\t\tt.Errorf(\"two-part Write got 0x%016x, want 0x%016x\", got, want)\n\t}\n}\n\nfunc TestHash64Sum(t *testing.T) {\n\th := murmur3.New64()\n\th.Write([]byte(\"Hello, world!\"))\n\tb := h.Sum(nil)\n\tif len(b) != 8 {\n\t\tt.Fatalf(\"Sum(nil) returned %d bytes, want 8\", len(b))\n\t}\n\n\tgot := uint64(b[0]) | uint64(b[1])\u003c\u003c8 | uint64(b[2])\u003c\u003c16 | uint64(b[3])\u003c\u003c24 |\n\t\tuint64(b[4])\u003c\u003c32 | uint64(b[5])\u003c\u003c40 | uint64(b[6])\u003c\u003c48 | uint64(b[7])\u003c\u003c56\n\twant := uint64(13850326164366542939)\n\tif got != want {\n\t\tt.Errorf(\"Sum got 0x%016x, want 0x%016x\", got, want)\n\t}\n}\n\nfunc TestHash64SumDoesNotChangeState(t *testing.T) {\n\th := murmur3.New64()\n\th.Write([]byte(\"Hello\"))\n\ts1 := h.Sum64()\n\ts2 := h.Sum64()\n\tif s1 != s2 {\n\t\tt.Errorf(\"Sum64 changed state: first 0x%016x, second 0x%016x\", s1, s2)\n\t}\n}\n\nfunc TestHash64Reset(t *testing.T) {\n\th := murmur3.New64()\n\th.Write([]byte(\"some data\"))\n\th.Reset()\n\th.Write([]byte(\"Hello, world!\"))\n\tgot := h.Sum64()\n\twant := uint64(13850326164366542939)\n\tif got != want {\n\t\tt.Errorf(\"after Reset got 0x%016x, want 0x%016x\", got, want)\n\t}\n}\n"},{"name":"readme_filetest.gno","body":"package main\n\nimport \"gno.land/p/jeronimoalbi/murmur3\"\n\nfunc main() {\n\tdata := []byte(\"Hello, world!\")\n\tseed := uint32(42)\n\n\t// Hash32 without seed\n\th32 := murmur3.New32()\n\th32.Write(data)\n\tsum32 := uint64(h32.Sum32())\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash32 with seed\n\th32 = murmur3.NewWithSeed32(seed)\n\th32.Write(data)\n\tsum32 = uint64(h32.Sum32())\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash32 without seed using a helper function\n\tsum32 = uint64(murmur3.Sum32(data))\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash32 with seed using a helper function\n\tsum32 = uint64(murmur3.Sum32WithSeed(data, seed))\n\tprintln(murmur3.EncodeToString(sum32))\n\n\t// Hash64 without seed\n\th64 := murmur3.New64()\n\th64.Write(data)\n\tsum64 := h64.Sum64()\n\tprintln(murmur3.EncodeToString(sum64))\n\n\t// Hash64 with seed\n\th64 = murmur3.NewWithSeed64(0, seed)\n\th64.Write(data)\n\tsum64 = h64.Sum64()\n\tprintln(murmur3.EncodeToString(sum64))\n\n\t// Hash64 without seed using a helper function\n\tsum64 = murmur3.Sum64(data)\n\tprintln(murmur3.EncodeToString(sum64))\n\n\t// Hash64 with seed using a helper function\n\tsum64 = murmur3.Sum64WithSeed(data, 0, seed)\n\tprintln(murmur3.EncodeToString(sum64))\n}\n\n// Output:\n// c0363e43\n// 2c8c8533\n// c0363e43\n// 2c8c8533\n// c0363e43aa5dc85b\n// c0363e432c8c8533\n// c0363e43aa5dc85b\n// c0363e432c8c8533\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"q7rL4UQ6LUWzCN/Rv9h44TbzMCCxKozVzUDKY9i8vvXa3zUdDzsOVrGKlrOjtHiSQNegRwFA9BBkmuoClx9ZAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"pkgerr","path":"gno.land/p/leon/pkgerr","files":[{"name":"err.gno","body":"// Package pkgerr provides a custom error wrapper that prepends the realm link to your error\n// This is useful for identifying the source package of the error.\n//\n// Usage:\n//\n// To wrap an error with realm and chain domain information, use the `New` function:\n//\n//\terr := pkgerr.New(\"my error message\") // in gno.land/r/leon/example\n//\tfmt.Println(err.Error()) // Output: \"r/leon/example: my error message\"\npackage pkgerr\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// pkgErr is a custom error type that prepends the realm link\n// (captured at construction time) to the original error message.\ntype pkgErr struct {\n\trealmPath   string\n\toriginalErr error\n}\n\n// New creates a new pkgErr with the given message. The realm link\n// captured by unsafe.CurrentRealm() at THIS call's evaluation is\n// stored on the returned value, so the prefix in Error() reflects\n// the realm that CREATED the error — not whichever realm later\n// happens to call .Error() on it.\n//\n// SECURITY (Class-2-shaped, accepted as display-only): the capture\n// uses unsafe.CurrentRealm() inside a non-crossing /p/ function and\n// is therefore .Title()-vulnerable (the walk resolves to the\n// most-recent crossing ancestor, not the immediate caller). For the\n// typical package-level-var usage (e.g. `var ErrX = pkgerr.New(...)`\n// in r/foo/bar) the construction site IS that package's init, so\n// the resolution is correct. For dynamic construction inside a\n// function call chain, a contrived chain could produce a misleading\n// prefix. This is acceptable here because no consumer in /examples\n// uses the prefix for authorization — it is rendered only as part of\n// error.Error() text. See docs/resources/gno-security.md for the\n// threat-class taxonomy.\nfunc New(msg string) error {\n\treturn \u0026pkgErr{\n\t\trealmPath:   unsafe.CurrentRealm().PkgPath(),\n\t\toriginalErr: errors.New(msg),\n\t}\n}\n\n// Wrap behaves like New but reuses an existing error as the cause.\n// Same construction-time capture semantics and same SECURITY caveat\n// as New.\nfunc Wrap(err error) error {\n\tif err == nil {\n\t\treturn nil\n\t}\n\n\treturn \u0026pkgErr{\n\t\trealmPath:   unsafe.CurrentRealm().PkgPath(),\n\t\toriginalErr: err,\n\t}\n}\n\nfunc (e pkgErr) Unwrap() error {\n\treturn e.originalErr\n}\n\n// Error implements the `error` interface for pkgErr. It uses the\n// realmPath captured at construction time so the prefix is stable\n// across the error's lifetime, regardless of which realm renders it.\nfunc (e *pkgErr) Error() string {\n\treturn ufmt.Sprintf(\"%s: %s\",\n\t\tstrings.TrimPrefix(e.realmPath, \"gno.land/\"),\n\t\te.originalErr)\n}\n"},{"name":"err_test.gno","body":"package pkgerr\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\t\"testing\"\n)\n\nconst pkgPath = \"gno.land/r/leon/test\"\n\nvar prefix = strings.TrimPrefix(pkgPath, \"gno.land/\")\n\nfunc TestNew(t *testing.T) {\n\t// SetRealm BEFORE New: capture happens at construction time now,\n\t// so the realm context must be set first. (The previous test\n\t// ordering — SetRealm AFTER New — asserted the now-fixed bug\n\t// where the prefix was read at .Error()-render time.)\n\ttesting.SetRealm(testing.NewCodeRealm(pkgPath))\n\n\terr := New(\"my error message\")\n\tif err == nil {\n\t\tt.Fatal(\"Expected an error, got nil\")\n\t}\n\n\texpected := prefix + \": my error message\"\n\n\tif err.Error() != expected {\n\t\tt.Errorf(\"Expected error message %q, got %q\", expected, err.Error())\n\t}\n}\n\nfunc TestWrap(t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(pkgPath))\n\n\toriginalErr := errors.New(\"original error\")\n\twrappedErr := Wrap(originalErr)\n\tif wrappedErr == nil {\n\t\tt.Fatal(\"Expected an error, got nil\")\n\t}\n\n\texpected := prefix + \": original error\"\n\tif wrappedErr.Error() != expected {\n\t\tt.Errorf(\"Expected error message %q, got %q\", expected, wrappedErr.Error())\n\t}\n}\n\nfunc TestUnwrap(t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(pkgPath))\n\toriginalErr := errors.New(\"original error\")\n\twrappedErr := Wrap(originalErr)\n\n\tunwrappedErr := wrappedErr.(*pkgErr).Unwrap()\n\tif unwrappedErr != originalErr {\n\t\tt.Errorf(\"Expected unwrapped error %v, got %v\", originalErr, unwrappedErr)\n\t}\n}\n\nfunc TestErrorMethod(t *testing.T) {\n\t// Build the struct directly with realmPath set so this test\n\t// exercises Error()'s formatting independently of the\n\t// New/Wrap construction-time capture.\n\toriginalErr := errors.New(\"original error\")\n\tpkgErr := \u0026pkgErr{realmPath: pkgPath, originalErr: originalErr}\n\n\texpected := prefix + \": original error\"\n\tif pkgErr.Error() != expected {\n\t\tt.Errorf(\"Expected error message %q, got %q\", expected, pkgErr.Error())\n\t}\n}\n\nfunc TestWrapNilError(t *testing.T) {\n\terr := Wrap(nil)\n\tif err != nil {\n\t\tt.Errorf(\"Expected nil error, got %v\", err)\n\t}\n}\n\nfunc TestNewWithEmptyMessage(t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(pkgPath))\n\terr := New(\"\")\n\tif err == nil {\n\t\tt.Fatal(\"Expected an error, got nil\")\n\t}\n\n\texpected := prefix + \": \"\n\tif err.Error() != expected {\n\t\tt.Errorf(\"Expected error message %q, got %q\", expected, err.Error())\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/leon/pkgerr\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"myVQ2xpUG5WSCGWn7qEG+IYQLQOUfe/gGefhkS3r467qAUHIYD0j7yhWsaDsdSJqTmmmzgf98Q1mCNJrEH6+BQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ascii","path":"gno.land/p/lou/ascii","files":[{"name":"ascii.gno","body":"package ascii\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/md\"\n)\n\ntype WAlignment string\ntype HAlignment string\n\nconst (\n\t// Width Alignment\n\tAlignWCenter WAlignment = \"center\"\n\tAlignLeft    WAlignment = \"left\"\n\tAlignRight   WAlignment = \"right\"\n\n\t// Height Alignment\n\tAlignHCenter HAlignment = \"center\"\n\tAlignTop     HAlignment = \"top\"\n\tAlignBottom  HAlignment = \"bottom\"\n)\n\n// padLine aligns text within a given width.\n//\n// Supports AlignLeft, AlignRight, and AlignWCenter alignment.\nfunc padLine(line string, width int, align WAlignment, space string) string {\n\tpadding := width - len(line)\n\tif width \u003c len(line) {\n\t\tpadding = 0\n\t}\n\n\tswitch align {\n\tcase AlignRight:\n\t\treturn Repeat(space, padding) + line\n\tcase AlignWCenter:\n\t\tleft := padding / 2\n\t\tright := padding - left\n\t\treturn Repeat(space, left) + line + Repeat(space, right)\n\tdefault: // AlignLeft\n\t\treturn line + Repeat(space, padding)\n\t}\n}\n\n// padHeight pads lines vertically according to alignment.\n//\n// Supports AlignTop, AlignBottom, and AlignHCenter alignment.\nfunc padHeight(lines []string, height int, align HAlignment) []string {\n\tpadded := []string{}\n\tif height \u003c= 0 {\n\t\treturn lines\n\t}\n\textra := height - len(lines)\n\n\ttopPad := 0\n\tswitch align {\n\tcase AlignBottom:\n\t\ttopPad = extra\n\tcase AlignHCenter:\n\t\ttopPad = extra / 2\n\t}\n\n\tfor i := 0; i \u003c topPad; i++ {\n\t\tpadded = append(padded, \"\")\n\t}\n\tpadded = append(padded, lines...)\n\tfor len(padded) \u003c height {\n\t\tpadded = append(padded, \"\")\n\t}\n\treturn padded\n}\n\n// Repeat returns repetition of a string n times.\nfunc Repeat(char string, n int) string {\n\tif n \u003c= 0 {\n\t\treturn \"\"\n\t}\n\treturn strings.Repeat(char, n)\n}\n\n// Box draws a single-line text in a simple box.\n// If the string contains newlines, it falls back to FlexFrame function.\n//\n// Example:\n//\n//\tBox(\"Hello World\\n!\")\n//\n// Gives:\n//\n//\t+-------------+\n//\t| Hello World |\n//\t| !           |\n//\t+-------------+\nfunc Box(text string) string {\n\treturn FlexFrame(strings.Split(text, \"\\n\"), AlignLeft)\n}\n\n// FlexFrame draws a frame with automatic width and alignment.\n//\n// Example:\n//\n//\tFlexFrame([]string{\"hello\", \"worldd\", \"!!\"}, \"right\")\n//\n// Gives:\n//\n//\t+-------+\n//\t|  hello |\n//\t| worldd |\n//\t|     !! |\n//\t+-------+\nfunc FlexFrame(lines []string, align WAlignment) string {\n\tmaxWidth := 0\n\n\tfor _, line := range lines {\n\t\tif len(line) \u003e maxWidth {\n\t\t\tmaxWidth = len(line)\n\t\t}\n\t}\n\n\ttop := \"+\" + Repeat(\"-\", maxWidth+2) + \"+\\n\"\n\tbottom := \"+\" + Repeat(\"-\", maxWidth+2) + \"+\"\n\n\tbody := \"\"\n\tfor i := 0; i \u003c len(lines); i++ {\n\t\tbody += \"| \" + padLine(lines[i], maxWidth, align, \" \") + \" |\\n\"\n\t}\n\n\treturn md.CodeBlock(top + body + bottom)\n}\n\n// Frame draws a frame with specific width, height and alignment options.\n//\n// Example:\n//\n//\tFrame([]string{\"hello\", \"world\", \"!!\"}, \"center\", 10, 5, \"center\")\n//\n// Gives:\n//\n//\t+------------+\n//\t|            |\n//\t|   hello    |\n//\t|   world    |\n//\t|     !!     |\n//\t|            |\n//\t+------------+\nfunc Frame(\n\tlines []string,\n\twAlign WAlignment,\n\twidth, height int,\n\thAlign HAlignment,\n) string {\n\tmaxWidth := width\n\tfor i := 0; i \u003c len(lines); i++ {\n\t\tif len(lines[i]) \u003e maxWidth {\n\t\t\tmaxWidth = len(lines[i])\n\t\t}\n\t}\n\n\tif len(lines) \u003e height {\n\t\theight = len(lines)\n\t}\n\tlines = padHeight(lines, height, hAlign)\n\n\ttop := \"+\" + Repeat(\"-\", maxWidth+2) + \"+\\n\"\n\tbottom := \"+\" + Repeat(\"-\", maxWidth+2) + \"+\"\n\tbody := \"\"\n\n\tfor _, line := range lines {\n\t\tbody += \"| \" + padLine(line, maxWidth, wAlign, \" \") + \" |\\n\"\n\t}\n\n\treturn md.CodeBlock(top + body + bottom)\n}\n\n// FixedFrame draws a frame with a fixed width and height, truncating or wrapping content as needed.\n// Width and height include the content area, not the frame borders.\n//\n// Example:\n//\n//\tFrame([]string{\"hello world!!\"}, ascii.AlignWCenter, 10, 4, ascii.AlignHCenter)\n//\n// Gives:\n//\n//\t+------------+\n//\t|            |\n//\t|   hello    |\n//\t|  world!!   |\n//\t|            |\n//\t+------------+\nfunc FixedFrame(\n\tlines []string,\n\twAlign WAlignment,\n\twidth, height int,\n\thAlign HAlignment,\n) string {\n\tvar wrapped []string\n\tif width \u003c 0 {\n\t\twidth = 0\n\t}\n\tif height \u003c 0 {\n\t\theight = 0\n\t}\n\n\tfor _, line := range lines {\n\t\twords := strings.Fields(line)\n\t\tcurrent := \"\"\n\t\tfor _, word := range words {\n\t\t\tif len(current)+len(word)+1 \u003e width {\n\t\t\t\twrapped = append(wrapped, current)\n\t\t\t\tcurrent = word\n\t\t\t} else {\n\t\t\t\tif current == \"\" {\n\t\t\t\t\tcurrent = word\n\t\t\t\t} else {\n\t\t\t\t\tcurrent += \" \" + word\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif current != \"\" {\n\t\t\twrapped = append(wrapped, current)\n\t\t}\n\t}\n\twrapped = padHeight(wrapped, height, hAlign)\n\n\ttop := \"+\" + Repeat(\"-\", width+2) + \"+\\n\"\n\tbottom := \"+\" + Repeat(\"-\", width+2) + \"+\"\n\n\tbody := \"\"\n\tfor i, line := range wrapped {\n\t\tif i == height {\n\t\t\tbreak\n\t\t}\n\t\tbody += \"| \" + padLine(line, width, wAlign, \" \") + \" |\\n\"\n\t}\n\n\treturn md.CodeBlock(top + body + bottom)\n}\n\n// ProgressBar renders a visual progress bar, the size represents the number of chars in length for the bar.\n//\n// Example:\n//\n//\tProgressBar(2, 6, 10, true)\n//\n// Gives: [###-------] 33%\nfunc ProgressBar(current int, total int, charSize int, displayPercent bool) string {\n\tif total == 0 {\n\t\treturn PercentageBar(0, charSize, displayPercent)\n\t}\n\tpercent := (current * 100) / total\n\n\treturn PercentageBar(percent, charSize, displayPercent)\n}\n\n// PercentageBar renders a visual progress bar, the size represents the number of chars in length for the bar.\n// This differs from ProgressBar in that it does not require a total value, takes a percentage directly.\n//\n// Example:\n//\n//\tPercentageBar(50, 6, true)\n//\n// Gives: [###---] 50%\nfunc PercentageBar(percent int, charSize int, displayPercent bool) string {\n\tfillLength := (percent * charSize) / 100\n\temptyLength := charSize - fillLength\n\n\tfilled := Repeat(\"#\", fillLength)\n\tempty := Repeat(\"-\", emptyLength)\n\n\tout := \"[\" + filled + empty + \"]\"\n\tif !displayPercent {\n\t\treturn out\n\t}\n\treturn out + \" \" + strconv.Itoa(percent) + \"%\"\n}\n\n// Grid renders a 2D grid of characters.\n//\n// Example:\n//\n//\tGrid(3, 3, \"x\")\n//\n// Gives:\n//\n//\txxx\n//\txxx\n//\txxx\nfunc Grid(rows int, cols int, fill string) string {\n\tout := \"\"\n\tif rows \u003c= 0 || cols \u003c= 0 {\n\t\treturn out\n\t}\n\n\tfor r := 0; r \u003c rows; r++ {\n\t\trow := \"\"\n\t\tfor c := 0; c \u003c cols; c++ {\n\t\t\trow += fill\n\t\t}\n\t\tout += row + \"\\n\"\n\t}\n\treturn out\n}\n"},{"name":"ascii_test.gno","body":"package ascii\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\n// padLine\n\nfunc TestpadLine_Left(t *testing.T) {\n\tgot := padLine(\"hi\", 6, AlignLeft, \" \")\n\twant := \"hi    \"\n\tif got != want {\n\t\tt.Fatalf(\"padLine left failed: got '%s', want '%s'\", got, want)\n\t}\n}\n\nfunc TestpadLine_Right(t *testing.T) {\n\tgot := padLine(\"hi\", 6, AlignRight, \" \")\n\twant := \"    hi\"\n\tif got != want {\n\t\tt.Fatalf(\"padLine right failed: got '%s', want '%s'\", got, want)\n\t}\n}\n\nfunc TestpadLine_Center(t *testing.T) {\n\tgot := padLine(\"hi\", 6, AlignWCenter, \" \")\n\twant := \"  hi  \"\n\tif got != want {\n\t\tt.Fatalf(\"padLine center failed: got '%s', want '%s'\", got, want)\n\t}\n}\n\nfunc TestpadLineNegativeWidth(t *testing.T) {\n\tgot := padLine(\"hi\", -1, AlignLeft, \" \")\n\twant := \"hi\"\n\tif got != want {\n\t\tt.Fatalf(\"padLine negative width failed: got '%s', want '%s'\", got, want)\n\t}\n}\n\n// padHeight\n\nfunc TestpadHeight_Top(t *testing.T) {\n\tlines := []string{\"one\", \"two\"}\n\tout := padHeight(lines, 4, AlignTop)\n\tif len(out) != 4 || out[0] != \"one\" || out[3] != \"\" {\n\t\tt.Fatalf(\"padHeight top failed: got %#v\", out)\n\t}\n}\n\nfunc TestpadHeight_Center(t *testing.T) {\n\tlines := []string{\"one\"}\n\tout := padHeight(lines, 3, AlignHCenter)\n\tif len(out) != 3 || out[0] != \"\" || out[1] != \"one\" || out[2] != \"\" {\n\t\tt.Fatalf(\"padHeight center failed: got %#v\", out)\n\t}\n}\n\nfunc TestpadHeight_Bottom(t *testing.T) {\n\tlines := []string{\"one\"}\n\tout := padHeight(lines, 2, AlignBottom)\n\tif len(out) != 2 || out[0] != \"\" || out[1] != \"one\" {\n\t\tt.Fatalf(\"padHeight bottom failed: got %#v\", out)\n\t}\n}\n\nfunc TestpadHeightNegativeHeight(t *testing.T) {\n\tlines := []string{\"one\", \"two\"}\n\tout := padHeight(lines, -1, AlignTop)\n\tif len(out) != 2 || out[0] != \"one\" || out[1] != \"two\" {\n\t\tt.Fatalf(\"padHeight negative height failed: got %#v\", out)\n\t}\n}\n\n// Box\n\nfunc TestBox_SingleLine(t *testing.T) {\n\tgot := Box(\"Hi\")\n\tif !strings.Contains(got, \"| Hi |\") {\n\t\tt.Fatalf(\"Box did not render expected line: got\\n%s\", got)\n\t}\n}\n\nfunc TestBox_MultiLineFallback(t *testing.T) {\n\tgot := Box(\"One\\nTwo\")\n\tif !strings.Contains(got, \"| One |\") || !strings.Contains(got, \"| Two |\") {\n\t\tt.Fatalf(\"Box (multi-line fallback) failed: got\\n%s\", got)\n\t}\n}\n\n// FlexFrame\n\nfunc TestFlexFrame(t *testing.T) {\n\tlines := []string{\"Short\", \"A bit longer\"}\n\tgot := FlexFrame(lines, AlignRight)\n\tif !strings.Contains(got, \"|        Short |\") {\n\t\tt.Fatalf(\"FlexFrame right alignment failed:\\n%s\", got)\n\t}\n}\n\n// Frame\n\nfunc TestFrame_SizeAlign(t *testing.T) {\n\tlines := []string{\"line\"}\n\tgot := Frame(lines, AlignWCenter, 10, 3, AlignHCenter)\n\tif res := strings.Count(got, \"\\n\"); res != 7 {\n\t\tt.Fatalf(\"Frame height failed (got %d):\\n%s\", res, got)\n\t}\n\tif !strings.Contains(got, \"|    line    |\") {\n\t\tt.Fatalf(\"Frame width or alignment failed:\\n%s\", got)\n\t}\n}\n\nfunc TestFrameNegativeSize(t *testing.T) {\n\tlines := []string{\"line\"}\n\tgot := Frame(lines, AlignWCenter, -1, -1, AlignHCenter)\n\tif res := strings.Count(got, \"\\n\"); res != 1+4 {\n\t\tt.Fatalf(\"Frame negative size failed (got %d):\\n%s\", res, got)\n\t}\n\tif !strings.Contains(got, \"| line |\") {\n\t\tt.Fatalf(\"Frame negative size failed:\\n%s\", got)\n\t}\n}\n\n// FixedFrame\n\nfunc TestFixedFrame(t *testing.T) {\n\tlines := []string{\"line\"}\n\tgot := FixedFrame(lines, AlignWCenter, 10, 3, AlignTop)\n\tif res := strings.Count(got, \"\\n\"); res != 7 {\n\t\tt.Fatalf(\"FixedFrame height failed (got %d):\\n%s\", res, got)\n\t}\n\tif !strings.Contains(got, \"|    line    |\") {\n\t\tt.Fatalf(\"FixedFrame width failed:\\n%s\", got)\n\t}\n}\n\nfunc TestFixedFrameNegativeSize(t *testing.T) {\n\tlines := []string{\"line\"}\n\tgot := FixedFrame(lines, AlignLeft, -1, -1, AlignTop)\n\tif res := strings.Count(got, \"\\n\"); res != 4 {\n\t\tt.Fatalf(\"FixedFrame negative size failed (got %d):\\n%s\", res, got)\n\t}\n\tif strings.Contains(got, \"| line |\") {\n\t\tt.Fatalf(\"FixedFrame negative size failed:\\n%s\", got)\n\t}\n}\n\n// ProgressBar\n\nfunc TestProgressBar(t *testing.T) {\n\tgot := ProgressBar(30, 100, 10, true)\n\tif !strings.Contains(got, \"30%\") || !strings.Contains(got, \"[###-------]\") {\n\t\tt.Fatalf(\"ProgressBar output incorrect: %s\", got)\n\t}\n}\n\nfunc TestProgressBar_ZeroTotal(t *testing.T) {\n\tgot := ProgressBar(0, 0, 10, true)\n\tif !strings.Contains(got, \"0%\") {\n\t\tt.Fatalf(\"ProgressBar zero-total failed: %s\", got)\n\t}\n}\n\n// Grid\n\nfunc TestGrid(t *testing.T) {\n\tgot := Grid(2, 3, \"*\")\n\twant := \"***\\n***\\n\"\n\tif got != want {\n\t\tt.Fatalf(\"Grid failed: got\\n%s\\nwant\\n%s\", got, want)\n\t}\n}\n\nfunc TestGrid_NegativeSize(t *testing.T) {\n\tgot := Grid(-1, -1, \"*\")\n\twant := \"\"\n\tif got != want {\n\t\tt.Fatalf(\"Grid negative size failed: got\\n%s\\nwant\\n%s\", got, want)\n\t}\n}\n"},{"name":"doc.gno","body":"/*\nPackage ascii provides a simple way to create ASCII components in Gno.\n\nThe ascii package allows you to create basic ASCII components like boxes and lines, and output the generated ASCII to a string in codeblock format when necessary.\n\nExample:\n\n\timport \"gno.land/p/lou/ascii\"\n\n\tfunc Foo() string {\n\t    return ascii.Frame([]string{\"Hello, World!\"}, 20, 5, ascii.AlignWCenter, ascii.AlignHCenter)\n\t}\n\n*/\n\npackage ascii // import \"gno.land/p/lou/ascii\"\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/lou/ascii\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"xipBH65IbIeGpIS5JLKobGcVbLHiLiZzf/ZRqsnsJHVYjSniAccuImli8wNePbLVPDDuCROpx0Bmp893mQT2Dw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"query","path":"gno.land/p/lou/query","files":[{"name":"doc.gno","body":"/*\nPackage query provides a simple way to handle queries in your realm.\n\nIt includes helper functions to extract individual query parameters, convert the full query string into a map,\nand update one or more parameters in a URL.\n\nExample:\n\n\timport (\n\t\t\"gno.land/p/lou/query\"\n\t\t\"gno.land/p/moul/md\"\n\t)\n\n\tfunc Render(rawPath string) string {\n\t\tmode, err := query.GetQueryValueFromURL(\"mode\", rawPath)\n\t\tif err != nil || mode == \"\" {\n\t\t\tmode = \"grid\" // default value\n\t\t}\n\n\t\t// Handle queryMode value\n\t\tlistURL, _ := query.UpdateQueryValue(rawPath, \"mode\", \"list\")\n\t\tgridURL, _ := query.UpdateQueryValue(rawPath, \"mode\", \"grid\")\n\n\t\tout := md.Link(\"list mode\", listURL) + \"\\n\\n\"\n\t\tout += md.Link(\"grid mode\", gridURL) + \"\\n\\n\"\n\t\treturn out\n\t}\n\n*/\n\npackage query // import \"gno.land/p/lou/query\"\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/lou/query\"\ngno = \"0.9\"\n"},{"name":"query.gno","body":"package query\n\nimport (\n\t\"net/url\"\n)\n\n// GetQueryValues returns all values for a given key in query parameters.\nfunc GetQueryValues(params url.Values, key string) []string {\n\treturn params[key]\n}\n\n// GetQueryFirstValue returns the first value for a given key in query parameters.\nfunc GetQueryFirstValue(params url.Values, key string) string {\n\tif vals, ok := params[key]; ok \u0026\u0026 len(vals) \u003e 0 {\n\t\treturn vals[0]\n\t}\n\treturn \"\"\n}\n\n// HasQueryKey checks if the given key exists in query parameters.\nfunc HasQueryKey(params url.Values, key string) bool {\n\t_, exists := params[key]\n\treturn exists\n}\n\n// GetQueryValueFromURL retrieves a single query value from the raw URL.\n// If the URL is invalid or the parameter does not exist, it returns an empty string.\n//\n//\t value := GetQueryValueFromURL(\"user\", \"https://example.com/?user=john\")\n//\t\t// value == \"john\"\nfunc GetQueryValueFromURL(key, rawPath string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn u.Query().Get(key), nil\n}\n\n// GetQuery retrieves a single query parameter value from the raw URL.\n// If the URL is invalid or the parameter does not exist, it returns an empty string.\nfunc GetQuery(key, rawPath string) string {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\"\n\t}\n\treturn u.Query().Get(key)\n}\n\n// ParseQueryFirstValuesMap returns a map of the first values for all query parameters.\n//\n//\tm := ParseQueryFirstValuesMap(\"https://example.com/?foo=1\u0026bar=2\")\n//\t// m == map[string]string{\"foo\": \"1\", \"bar\": \"2\"}\nfunc ParseQueryFirstValuesMap(rawPath string) (map[string]string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tresult := make(map[string]string)\n\tfor key, vals := range u.Query() {\n\t\tif len(vals) \u003e 0 {\n\t\t\tresult[key] = vals[0]\n\t\t}\n\t}\n\treturn result, nil\n}\n\n// UpdateQueryValue sets a single query key to one value, replacing existing values.\n//\n//\turl := UpdateQueryValue(\"https://example.com/?foo=1\", \"foo\", \"2\")\n//\t// url == \"https://example.com/?foo=2\"\nfunc UpdateQueryValue(rawPath, key, value string) (string, error) {\n\treturn UpdateQueryValues(rawPath, key, []string{value})\n}\n\n// UpdateQueryValues sets a single query key to multiple values.\n// Existing values are replaced, and new ones are added. Returns the modified URL as a string.\n//\n//\turl := UpdateQueryValues(\"https://example.com/?foo=1\", \"foo\", []string{\"2\", \"3\"})\n//\t// url == \"https://example.com/?foo=2\u0026foo=3\"\nfunc UpdateQueryValues(rawPath, key string, values []string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tq := u.Query()\n\tq[key] = values\n\tu.RawQuery = q.Encode()\n\treturn u.String(), nil\n}\n\n// UpdateQueryFirstValues sets multiple query parameters to single values.\n// Existing parameters are overwritten, and new ones are added. Returns the modified URL as a string.\n//\n//\turl := UpdateQueryFirstValues(\"https://example.com/?foo=1\", map[string]string{\"foo\": \"2\", \"bar\": \"3\"})\n//\t// url == \"https://example.com/?bar=3\u0026foo=2\"\nfunc UpdateQueryFirstValues(rawPath string, updates map[string]string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tq := u.Query()\n\tfor k, v := range updates {\n\t\tq.Set(k, v)\n\t}\n\tu.RawQuery = q.Encode()\n\treturn u.String(), nil\n}\n\n// UpdateQueryAllValues sets multiple query parameters, each with multiple values.\n// Existing parameters are replaced, and new ones are added. Returns the modified URL as a string.\n//\n//\turl := UpdateQueryAllValues(\"https://example.com/?foo=1\", map[string][]string{\"foo\": {\"2\", \"3\"}, \"bar\": {\"4\"}})\n//\t// url == \"https://example.com/?bar=4\u0026foo=2\u0026foo=3\"\nfunc UpdateQueryAllValues(rawPath string, updates map[string][]string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tq := u.Query()\n\tfor k, v := range updates {\n\t\tq[k] = v\n\t}\n\tu.RawQuery = q.Encode()\n\treturn u.String(), nil\n}\n\n// SetQueries sets multiple query parameters in the URL.\n// It replaces existing parameters and adds new ones, returning the modified URL as a string.\n//\n// url := SetQueries(\"https://example.com/?fa=1\", map[string]string{\"foo\": \"2\", \"bar\": \"3\"})\n// // url == \"https://example.com/?bar=3\u0026foo=2\"\nfunc SetQueries(rawPath string, queries map[string]string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tq := u.Query()\n\tfor k, v := range queries {\n\t\tq.Set(k, v)\n\t}\n\tu.RawQuery = q.Encode()\n\treturn u.String(), nil\n}\n\n// SetQueriesMulti sets multiple query parameters with multiple values.\n// It replaces existing parameters and adds new ones, returning the modified URL as a string.\n//\n// url := SetQueriesMulti(\"https://example.com/?fa=1\", map[string][]string{\"foo\": {\"2\", \"3\"}, \"bar\": {\"4\"}})\n// // url == \"https://example.com/?bar=4\u0026foo=2\u0026foo=3\"\nfunc SetQueriesMulti(rawPath string, queries map[string][]string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tq := u.Query()\n\tfor k, v := range queries {\n\t\tq[k] = v\n\t}\n\tu.RawQuery = q.Encode()\n\treturn u.String(), nil\n}\n\n// DeleteQuery removes a key from the query parameters.\n//\n//\turl := DeleteQuery(\"https://example.com/?foo=1\u0026bar=2\", \"foo\")\n//\t// url == \"https://example.com/?bar=2\"\nfunc DeleteQuery(rawPath, key string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tq := u.Query()\n\tq.Del(key)\n\tu.RawQuery = q.Encode()\n\treturn u.String(), nil\n}\n\n// ResetQuery clears all query parameters from the URL path.\n//\n//\turl := ResetQuery(\"https://example.com/?foo=1\u0026bar=2\")\n//\t// url == \"https://example.com/\"\nfunc ResetQuery(rawPath string) (string, error) {\n\tu, err := url.Parse(rawPath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tu.RawQuery = \"\"\n\treturn u.String(), nil\n}\n"},{"name":"query_test.gno","body":"package query\n\nimport (\n\t\"net/url\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestUpdateQueryValue(t *testing.T) {\n\traw := \"/blog?foo=bar\"\n\tupdated, err := UpdateQueryValue(raw, \"foo\", \"baz\")\n\turequire.NoError(t, err, \"no error\")\n\tuassert.Equal(t, \"/blog?foo=baz\", updated)\n}\n\nfunc TestUpdateQueryFirstValues(t *testing.T) {\n\traw := \"/blog?sort=alpha\"\n\tupdated, err := UpdateQueryFirstValues(raw, map[string]string{\n\t\t\"sort\":  \"recent\",\n\t\t\"order\": \"asc\",\n\t})\n\turequire.NoError(t, err, \"no error\")\n\texpected1 := \"/blog?order=asc\u0026sort=recent\"\n\texpected2 := \"/blog?sort=recent\u0026order=asc\"\n\tif updated != expected1 \u0026\u0026 updated != expected2 {\n\t\tt.Errorf(\"got %s, expected %s or %s\", updated, expected1, expected2)\n\t}\n}\n\nfunc TestGetQueryValueFromURL(t *testing.T) {\n\traw := \"/blog?view=grid\u0026sort=alpha\"\n\tval, err := GetQueryValueFromURL(\"sort\", raw)\n\turequire.NoError(t, err, \"no error\")\n\tuassert.Equal(t, \"alpha\", val)\n}\n\nfunc TestParseQueryFirstValuesMap(t *testing.T) {\n\traw := \"/blog?sort=alpha\u0026order=desc\"\n\tm, err := ParseQueryFirstValuesMap(raw)\n\turequire.NoError(t, err, \"no error\")\n\tuassert.Equal(t, \"alpha\", m[\"sort\"])\n\tuassert.Equal(t, \"desc\", m[\"order\"])\n}\n\nfunc TestDeleteQuery(t *testing.T) {\n\traw := \"/blog?sort=alpha\u0026mode=grid\"\n\tdeleted, err := DeleteQuery(raw, \"sort\")\n\turequire.NoError(t, err, \"no error\")\n\tuassert.Equal(t, \"/blog?mode=grid\", deleted)\n}\n\nfunc TestMalformedURLHandling(t *testing.T) {\n\t_, err := ParseQueryFirstValuesMap(\":::\")\n\turequire.Error(t, err)\n\n\t_, err = UpdateQueryValue(\":::\", \"a\", \"b\")\n\turequire.Error(t, err)\n\n\tval, err := GetQueryValueFromURL(\"x\", \":::\")\n\turequire.Error(t, err)\n\turequire.Equal(t, \"\", val)\n}\n\nfunc TestGetQueryValues(t *testing.T) {\n\tvalues := url.Values{\n\t\t\"foo\": {\"1\", \"2\"},\n\t}\n\tgot := GetQueryValues(values, \"foo\")\n\tuassert.Equal(t, \"1\", got[0])\n\tuassert.Equal(t, \"2\", got[1])\n}\n\nfunc TestGetQueryFirstValue(t *testing.T) {\n\tvalues := url.Values{\n\t\t\"foo\": {\"1\", \"2\"},\n\t}\n\tuassert.Equal(t, \"1\", GetQueryFirstValue(values, \"foo\"))\n\tuassert.Equal(t, \"\", GetQueryFirstValue(values, \"bar\")) // key doesn't exist\n}\n\nfunc TestHasQueryKey(t *testing.T) {\n\tvalues := url.Values{\n\t\t\"foo\": {\"1\"},\n\t}\n\tuassert.Equal(t, true, HasQueryKey(values, \"foo\"))\n\tuassert.Equal(t, false, HasQueryKey(values, \"bar\"))\n}\n\nfunc TestUpdateQueryValues(t *testing.T) {\n\traw := \"/blog?foo=bar\"\n\tupdated, err := UpdateQueryValues(raw, \"foo\", []string{\"1\", \"2\"})\n\turequire.NoError(t, err, \"no error\")\n\tuassert.Equal(t, \"/blog?foo=1\u0026foo=2\", updated)\n}\n\nfunc TestUpdateQueryAllValues(t *testing.T) {\n\traw := \"/blog?foo=1\"\n\tupdated, err := UpdateQueryAllValues(raw, map[string][]string{\n\t\t\"foo\": {\"2\", \"3\"},\n\t\t\"bar\": {\"4\"},\n\t})\n\turequire.NoError(t, err, \"no error\")\n\t// order may vary\n\texpected1 := \"/blog?bar=4\u0026foo=2\u0026foo=3\"\n\texpected2 := \"/blog?foo=2\u0026foo=3\u0026bar=4\"\n\tif updated != expected1 \u0026\u0026 updated != expected2 {\n\t\tt.Errorf(\"got %s, expected %s or %s\", updated, expected1, expected2)\n\t}\n}\n\nfunc TestSetQueries(t *testing.T) {\n\traw := \"/blog?foo=1\"\n\tupdated, err := SetQueries(raw, map[string]string{\n\t\t\"foo\": \"2\",\n\t\t\"bar\": \"3\",\n\t})\n\turequire.NoError(t, err, \"no error\")\n\texpected1 := \"/blog?bar=3\u0026foo=2\"\n\texpected2 := \"/blog?foo=2\u0026bar=3\"\n\tif updated != expected1 \u0026\u0026 updated != expected2 {\n\t\tt.Errorf(\"got %s, expected %s or %s\", updated, expected1, expected2)\n\t}\n}\n\nfunc TestSetQueriesMulti(t *testing.T) {\n\traw := \"/blog?foo=1\"\n\tupdated, err := SetQueriesMulti(raw, map[string][]string{\n\t\t\"foo\": {\"2\", \"3\"},\n\t\t\"bar\": {\"4\"},\n\t})\n\turequire.NoError(t, err, \"no error\")\n\texpected1 := \"/blog?bar=4\u0026foo=2\u0026foo=3\"\n\texpected2 := \"/blog?foo=2\u0026foo=3\u0026bar=4\"\n\tif updated != expected1 \u0026\u0026 updated != expected2 {\n\t\tt.Errorf(\"got %s, expected %s or %s\", updated, expected1, expected2)\n\t}\n}\n\nfunc TestResetQuery(t *testing.T) {\n\traw := \"/blog?foo=1\u0026bar=2\"\n\tupdated, err := ResetQuery(raw)\n\turequire.NoError(t, err, \"no error\")\n\tuassert.Equal(t, \"/blog\", updated)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"cDy0axNk8ndrt4sLcuCGIk3RFU3/2KCkkhDI/2wUJ2CnciETiPU6kIge1/zzyalKPmzXzTViyuscBHxpvvLOCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"blog","path":"gno.land/p/lou/blog","files":[{"name":"blog.gno","body":"package blog\n\nimport (\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/ownable/v0/exts/authorizable\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Blog struct {\n\tAuthorizable     *authorizable.Authorizable\n\ttitle            string\n\tprefix           string\n\tPostId           seqid.ID\n\tPosts            *avl.Tree // id --\u003e *Post\n\tPostsBySlug      *avl.Tree // slug --\u003e *Post\n\tPostsByUpdatedAt *avl.Tree // \"\u003cid\u003e::updatedAt\" --\u003e *Post (To ensure no overlay)\n\tTagsIndex        *avl.Tree // tagName --\u003e int\n\tTagsSorted       *avl.Tree // \"\u003ccount\u003e::tag\" --\u003e tag (To sort from most/least common tags)\n\tAuthorsIndex     *avl.Tree // authorAddress --\u003e int\n\tAuthorsSorted    *avl.Tree // \"\u003ccount\u003e::author\" --\u003e author (To sort from most/least common authors)\n\n\tCustomHeader    *[]HeaderPreset // header.gno\n\tDisableLikes    bool\n\tDisableComments bool\n\tUserResolver    UserResolver // moderation.gno\n}\n\nfunc (b Blog) Title() string {\n\treturn b.title\n}\n\nfunc (b Blog) Prefix() string {\n\treturn b.prefix\n}\n\n// NewBlog creates a Blog whose prefix is bound to the calling realm.\n// rlm must be the caller's own captured cur (asserted via\n// rlm.IsCurrent()); rlm.PkgPath() — the calling realm itself — provides\n// the prefix used for txlink/markdown URLs. Same shape as\n// grc20.NewToken's identity binding: unforgeable because rlm comes from\n// a runtime-validated crossing frame.\nfunc NewBlog(_ int, rlm realm, title string, owner address, opts ...Options) (*Blog, error) {\n\tif title == \"\" {\n\t\treturn nil, ErrEmptyTitle\n\t}\n\tif err := CheckAddr(owner); err != nil {\n\t\treturn nil, err\n\t}\n\n\tif !rlm.IsCurrent() {\n\t\treturn nil, ErrEmptyPrefix\n\t}\n\tpkgPath := rlm.PkgPath()\n\tif pkgPath == \"\" {\n\t\treturn nil, ErrEmptyPrefix\n\t}\n\tprefix := strings.Split(pkgPath, \"gno.land\")[1]\n\tif prefix == \"\" {\n\t\treturn nil, ErrEmptyPrefix\n\t}\n\n\tauth := authorizable.New(ownable.NewWithAddress(owner))\n\tblog := \u0026Blog{\n\t\tAuthorizable:     auth,\n\t\ttitle:            title,\n\t\tprefix:           prefix,\n\t\tPostId:           seqid.ID(0),\n\t\tPosts:            avl.NewTree(),\n\t\tPostsBySlug:      avl.NewTree(),\n\t\tPostsByUpdatedAt: avl.NewTree(),\n\t\tTagsIndex:        avl.NewTree(),\n\t\tTagsSorted:       avl.NewTree(),\n\t\tAuthorsIndex:     avl.NewTree(),\n\t\tAuthorsSorted:    avl.NewTree(),\n\t\tDisableLikes:     false,\n\t\tDisableComments:  false,\n\t\tUserResolver:     nil,\n\t\tCustomHeader:     nil,\n\t}\n\n\tfor _, opt := range opts {\n\t\topt(blog)\n\t}\n\treturn blog, nil\n}\n\ntype Options func(*Blog)\n\nfunc WithDisableLikes() Options {\n\treturn func(b *Blog) {\n\t\tb.DisableLikes = true\n\t\tb.setDisablePostLikes(true)\n\t}\n}\n\nfunc WithDisableComments() Options {\n\treturn func(b *Blog) {\n\t\tb.DisableComments = true\n\t\tb.setDisableComments(true)\n\t}\n}\n\nfunc WithUserResolver(resolver UserResolver) Options {\n\treturn func(b *Blog) {\n\t\tb.UserResolver = resolver\n\t}\n}\n\nfunc (b *Blog) AddPost(_ int, rlm realm, post *Post) error {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\n\tpost.id = b.PostId.Next()\n\tif _, err := b.GetPostBySlug(post.Slug()); err == nil {\n\t\treturn ErrPostAlreadyExists\n\t}\n\tif err := CheckAddr(address(post.Publisher())); err != nil {\n\t\treturn err\n\t}\n\n\tpost.DisableLikes = b.DisableLikes\n\tpost.DisableComments = b.DisableComments\n\tif b.UserResolver != nil {\n\t\tpost.UserResolver = b.UserResolver\n\t\tpost.authors = post.resolveAuthors(post.Authors())\n\t\tpost.publisher, _ = CheckUser(post.Publisher(), b.UserResolver)\n\t}\n\n\tb.addToIndex(post)\n\tb.Posts.Set(post.ID(), post)\n\tb.PostsBySlug.Set(post.Slug(), post)\n\tb.PostsByUpdatedAt.Set(post.ID()+\"::\"+post.UpdatedAt().String(), post)\n\treturn nil\n}\n\nfunc (b *Blog) UpdatePostById(_ int, rlm realm, id string, newPost *Post) error {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\n\tpost, err := b.GetPostById(id)\n\tif err != nil {\n\t\treturn err\n\t}\n\tpostBySlug := b.PostsBySlug.Get(post.Slug())\n\tpostByUpdated := b.PostsByUpdatedAt.Get(post.ID() + \"::\" + post.UpdatedAt().String())\n\tpost.UpdatePost(newPost)\n\tpostBySlug.(*Post).UpdatePost(newPost)\n\tpostByUpdated.(*Post).UpdatePost(newPost)\n\treturn nil\n}\n\nfunc (b *Blog) UpdatePostBySlug(_ int, rlm realm, slug string, newPost *Post) error {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\n\tpost, err := b.GetPostBySlug(slug)\n\tif err != nil {\n\t\treturn err\n\t}\n\tpostById := b.Posts.Get(post.ID())\n\tpostByUpdated := b.PostsByUpdatedAt.Get(post.ID() + \"::\" + post.UpdatedAt().String())\n\tpost.UpdatePost(newPost)\n\tpostById.(*Post).UpdatePost(newPost)\n\tpostByUpdated.(*Post).UpdatePost(newPost)\n\treturn nil\n}\n\nfunc (b *Blog) DeletePostById(_ int, rlm realm, id string) error {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\n\tpost, err := b.GetPostById(id)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn b.DeletePost(post)\n}\n\nfunc (b *Blog) DeletePostBySlug(_ int, rlm realm, slug string) error {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\n\tpost, err := b.GetPostBySlug(slug)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn b.DeletePost(post)\n}\n\nfunc (b *Blog) DeletePost(post *Post) error {\n\t_, removed := b.Posts.Remove(post.ID())\n\t_, removedSlug := b.PostsBySlug.Remove(post.Slug())\n\t_, removedUpdated := b.PostsByUpdatedAt.Remove(post.ID() + \"::\" + post.UpdatedAt().String())\n\tif !removed || !removedSlug || !removedUpdated {\n\t\treturn ErrDeleteFailed\n\t}\n\treturn nil\n}\n\nfunc (b *Blog) LikePostById(_ int, rlm realm, id string) error { // toggles between like and unlike\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\n\tpost, err := b.GetPostById(id)\n\tif err != nil {\n\t\treturn err\n\t}\n\tcaller := rlm.Previous().Address().String()\n\tif err := post.LikePost(caller); err != nil {\n\t\tpost.UnlikePost(caller)\n\t}\n\treturn nil\n\n}\n\nfunc (b *Blog) LikePostBySlug(_ int, rlm realm, slug string) error { // toggles between like and unlike\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\n\tpost, err := b.GetPostBySlug(slug)\n\tif err != nil {\n\t\treturn err\n\t}\n\tcaller := rlm.Previous().Address().String()\n\tif err := post.LikePost(caller); err != nil {\n\t\tpost.UnlikePost(caller)\n\t}\n\treturn nil\n}\n\nfunc (b Blog) GetPostById(id string) (*Post, error) {\n\tpost := b.Posts.Get(id)\n\tif post == nil {\n\t\treturn nil, ErrPostNotFound\n\t}\n\treturn post.(*Post), nil\n}\n\nfunc (b Blog) GetPostBySlug(slug string) (*Post, error) {\n\tpost := b.PostsBySlug.Get(slug)\n\tif post == nil {\n\t\treturn nil, ErrPostNotFound\n\t}\n\treturn post.(*Post), nil\n}\n\nfunc (b *Blog) SetDisablePostLikes(_ int, rlm realm, disable bool) {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\tb.setDisablePostLikes(disable)\n}\n\nfunc (b *Blog) setDisablePostLikes(disable bool) {\n\tb.DisableLikes = disable\n\tb.Posts.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tpost := value.(*Post)\n\t\tpost.DisableLikes = disable\n\t\treturn false\n\t})\n\tb.PostsBySlug.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tpost := value.(*Post)\n\t\tpost.DisableLikes = disable\n\t\treturn false\n\t})\n\tb.PostsByUpdatedAt.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tpost := value.(*Post)\n\t\tpost.DisableLikes = disable\n\t\treturn false\n\t})\n}\n\nfunc (b *Blog) SetDisableComments(_ int, rlm realm, disable bool) {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\tb.setDisableComments(disable)\n}\n\nfunc (b *Blog) setDisableComments(disable bool) {\n\tb.DisableComments = disable\n\tb.Posts.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tpost := value.(*Post)\n\t\tpost.DisableComments = disable\n\t\treturn false\n\t})\n\tb.PostsBySlug.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tpost := value.(*Post)\n\t\tpost.DisableComments = disable\n\t\treturn false\n\t})\n\tb.PostsByUpdatedAt.Iterate(\"\", \"\", func(_ string, value any) bool {\n\t\tpost := value.(*Post)\n\t\tpost.DisableComments = disable\n\t\treturn false\n\t})\n}\n\nfunc (b *Blog) SetUserResolver(_ int, rlm realm, resolver UserResolver) {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\tb.UserResolver = resolver\n}\n\nfunc (b *Blog) SetCustomHeader(_ int, rlm realm, presets []HeaderPreset) {\n\tb.Authorizable.AssertPreviousOnAuthList(0, rlm)\n\tb.CustomHeader = \u0026presets\n}\n\nfunc (b Blog) Mention(role, recipient string) string {\n\tif role == \"author\" {\n\t\treturn md.Bold(md.Link(\"@\"+recipient, b.Prefix()+\":authors/\"+recipient))\n\t}\n\tif role == \"commenter\" {\n\t\treturn md.Bold(md.Link(\"@\"+recipient, b.Prefix()+\":commenters/\"+recipient))\n\t}\n\tif role == \"tag\" {\n\t\treturn md.Bold(md.Link(\"#\"+recipient, b.Prefix()+\":tags/\"+recipient))\n\t}\n\treturn \"\"\n}\n\nfunc (b *Blog) addToIndex(post *Post) {\n\tfor _, tag := range post.Tags() {\n\t\toldCount := 0\n\t\tif val := b.TagsIndex.Get(tag); val != nil {\n\t\t\toldCount = val.(int)\n\t\t\tb.TagsSorted.Remove(ufmt.Sprintf(\"%05d::%s\", oldCount, tag))\n\t\t}\n\t\tnewCount := oldCount + 1\n\t\tb.TagsIndex.Set(tag, newCount)\n\t\tb.TagsSorted.Set(ufmt.Sprintf(\"%05d::%s\", newCount, tag), newCount)\n\t}\n\tfor _, author := range post.Authors() {\n\t\toldCount := 0\n\t\tif val := b.AuthorsIndex.Get(author); val != nil {\n\t\t\toldCount = val.(int)\n\t\t\tb.AuthorsSorted.Remove(ufmt.Sprintf(\"%05d::%s\", oldCount, author))\n\t\t}\n\t\tnewCount := oldCount + 1\n\t\tb.AuthorsIndex.Set(author, newCount)\n\t\tb.AuthorsSorted.Set(ufmt.Sprintf(\"%05d::%s\", newCount, author), newCount)\n\t}\n}\n\nfunc (b Blog) filterPostsStartEnd(tree *avl.Tree, start, end *time.Time) *avl.Tree {\n\tfiltered := avl.NewTree()\n\ttree.Iterate(\"\", \"\", func(k string, v interface{}) bool {\n\t\tpost := v.(*Post)\n\t\tif (start == nil || post.CreatedAt().After(*start)) \u0026\u0026\n\t\t\t(end == nil || post.CreatedAt().Before(*end)) {\n\t\t\tfiltered.Set(k, post)\n\t\t}\n\t\treturn false\n\t})\n\treturn filtered\n}\n\nfunc (b Blog) filterPostsByField(field, value, sort string) (*avl.Tree, bool) {\n\trecentPosts := avl.NewTree()\n\talphaPosts := avl.NewTree()\n\tupdatedPosts := avl.NewTree()\n\n\tswitch field {\n\tcase \"tag\", \"author\":\n\t\tb.Posts.ReverseIterate(\"\", \"\", func(k string, v interface{}) bool {\n\t\t\tpost := v.(*Post)\n\t\t\tvar match bool\n\t\t\tif field == \"tag\" {\n\t\t\t\tmatch = hasField(post.Tags(), value)\n\t\t\t} else {\n\t\t\t\tmatch = hasField(post.Authors(), value)\n\t\t\t}\n\t\t\tif match {\n\t\t\t\trecentPosts.Set(k, post)\n\t\t\t\talphaPosts.Set(post.Slug(), post)\n\t\t\t\tupdatedPosts.Set(post.UpdatedAt().String(), post)\n\t\t\t}\n\t\t\treturn false\n\t\t})\n\n\tcase \"commenter\":\n\t\tcommenterId := seqid.ID(0)\n\t\tb.Posts.ReverseIterate(\"\", \"\", func(k string, v interface{}) bool {\n\t\t\tpost := v.(*Post)\n\t\t\tcomments := post.GetCommentsByAuthor(value)\n\t\t\tfor _, comment := range comments {\n\t\t\t\tkeyPrefix := commenterId.Next().String() + \"::\"\n\t\t\t\trecentPosts.Set(keyPrefix+post.ID(), comment)\n\t\t\t\talphaPosts.Set(keyPrefix+post.Slug(), comment)\n\t\t\t\tupdatedPosts.Set(keyPrefix+post.UpdatedAt().String(), comment)\n\t\t\t}\n\t\t\treturn false\n\t\t})\n\t}\n\n\tswitch sort {\n\tcase \"alpha\":\n\t\treturn alphaPosts, alphaPosts.Size() \u003e 0\n\tcase \"update\":\n\t\treturn updatedPosts, updatedPosts.Size() \u003e 0\n\tdefault:\n\t\treturn recentPosts, recentPosts.Size() \u003e 0\n\t}\n}\n\nfunc (b Blog) findPostBySlug(value string) (string, bool) {\n\tvar foundKey string\n\tvar found bool\n\tb.Posts.Iterate(\"\", \"\", func(k string, v interface{}) bool {\n\t\tpost := v.(*Post)\n\t\tif post.Slug() == value {\n\t\t\tfoundKey = k\n\t\t\tfound = true\n\t\t}\n\t\treturn found\n\t})\n\treturn foundKey, found\n}\n"},{"name":"comment.gno","body":"package blog\n\nimport (\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\ntype Comment struct {\n\tid        seqid.ID\n\tauthor    string\n\tcontent   string\n\tcreatedAt time.Time\n\teditedAt  *time.Time // nil if unedited\n\tpinned    bool\n\tlikes     int\n\tRepliesId seqid.ID\n\tReplies   *avl.Tree // id --\u003e *Comment\n\n\tfooter       string // additional text, e.g. \"via @lou\" or txlink calls\n\tDisableLikes bool\n}\n\nfunc (c Comment) ID() string {\n\treturn c.id.String()\n}\n\nfunc (c Comment) Author() string {\n\treturn c.author\n}\n\nfunc (c Comment) Content() string {\n\treturn c.content\n}\n\nfunc (c Comment) CreatedAt() time.Time {\n\treturn c.createdAt\n}\n\nfunc (c Comment) EditedAt() *time.Time {\n\treturn c.editedAt\n}\n\nfunc (c Comment) Pinned() bool {\n\treturn c.pinned\n}\n\nfunc (c Comment) Likes() int {\n\treturn c.likes\n}\n\nfunc (c Comment) Footer() string {\n\treturn c.footer\n}\n\nfunc NewComment(author, content string) (*Comment, error) {\n\tif content == \"\" {\n\t\treturn nil, ErrEmptyComment\n\t}\n\treturn \u0026Comment{\n\t\tauthor:       author,\n\t\tcontent:      content,\n\t\tcreatedAt:    time.Now(),\n\t\teditedAt:     nil,\n\t\tpinned:       false,\n\t\tlikes:        0,\n\t\tRepliesId:    seqid.ID(0),\n\t\tReplies:      avl.NewTree(),\n\t\tfooter:       \"\",\n\t\tDisableLikes: false,\n\t}, nil\n}\n\nfunc (c *Comment) Edit(content string) error {\n\tif content == \"\" {\n\t\treturn ErrEmptyComment\n\t}\n\tc.content = content\n\tnow := time.Now()\n\tc.editedAt = \u0026now\n\treturn nil\n}\n\nfunc (c *Comment) Pin() {\n\tc.pinned = true\n}\n\nfunc (c *Comment) Unpin() {\n\tc.pinned = false\n}\n\nfunc (c *Comment) AddLike() {\n\tc.likes++\n}\n\nfunc (c *Comment) RemoveLike() {\n\tif c.likes \u003e 0 {\n\t\tc.likes--\n\t}\n}\n\nfunc (c *Comment) SetID(id seqid.ID) {\n\tc.id = id\n}\n\nfunc (c *Comment) SetFooter(footer string) {\n\tc.footer = footer\n}\n\nfunc (c *Comment) Render(blogPrefix string, depth int, resolver UserResolver) string {\n\tprefix := \"\"\n\tfor i := 0; i \u003c depth; i++ {\n\t\tprefix += \"\u003e \"\n\t}\n\tuser, _ := CheckUser(c.Author(), resolver)\n\tout := prefix + md.Bold(md.Link(\"@\"+user, blogPrefix+\":commenters/\"+user)) + \" \"\n\tif c.Pinned() {\n\t\tout += \"📌 \"\n\t}\n\tif c.EditedAt() != nil {\n\t\tout += md.Italic(formatTime(*c.EditedAt(), \"relative\")+\" (edited)\") + \"\\n\"\n\t} else {\n\t\tout += md.Italic(formatTime(c.CreatedAt(), \"relative\")) + \"\\n\"\n\t}\n\tout += \"\\n\" + prefix + c.Content() + \"\\n\"\n\tif !c.DisableLikes {\n\t\tout += prefix + \"❤️ \" + strconv.Itoa(c.Likes()) + \" \\n\\n\"\n\t}\n\tif c.Footer() != \"\" {\n\t\tout += prefix + c.Footer() + \"\\n\\n\"\n\t}\n\n\tif c.Replies != nil \u0026\u0026 c.Replies.Size() \u003e 0 {\n\t\tc.Replies.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\t\treply := value.(*Comment)\n\t\t\tout += reply.Render(blogPrefix, depth+1, resolver)\n\t\t\treturn false\n\t\t})\n\t}\n\treturn out\n}\n\nfunc searchInReplies(tree *avl.Tree, id string, result **Comment) bool {\n\tif tree == nil {\n\t\treturn false\n\t}\n\tfound := false\n\ttree.ReverseIterate(\"\", \"\", func(_ string, v interface{}) bool {\n\t\tc := v.(*Comment)\n\t\tif c.ID() == id {\n\t\t\t*result = c\n\t\t\tfound = true\n\t\t\treturn true\n\t\t}\n\t\tif searchInReplies(c.Replies, id, result) {\n\t\t\tfound = true\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\treturn found\n}\n"},{"name":"doc.gno","body":"/*\nPackage blog is a simple blogging system that allows users to create, update, delete, and render blog posts.\n\nIt supports features like pagination, filtering by tags and authors, and rendering posts in Markdown format. The package is designed to be extensible and can be integrated into larger applications.\n\nExample:\n\n\timport \"gno.land/p/lou/blog\"\n\n\tfunc init(cur realm) {\n\t\tmyBlog, _ = blog.NewBlog(\n\t\t\t0, cur,\n\t\t\t\"Personal Blog\",\n\t\t\t\"g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\",\n\t\t\tblog.WithUserResolver(myResolver),\n\t\t)\n\t}\n\n\tfunc CreatePost(cur realm, slug, title, body, publicationDate, authors, tags string) {\n\t\tauthorsField := strings.Split(authors, \" \")\n\t\ttagsField := strings.Split(tags, \" \")\n\n\t\tpost, err := blog.NewPost(\n\t\t\tslug,\n\t\t\ttitle,\n\t\t\tbody,\n\t\t\tpublicationDate,\n\t\t\tunsafe.OriginCaller().String(),\n\t\t\tauthorsField,\n\t\t\ttagsField,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tif err := myBlog.AddPost(0, cur, post); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\n\tfunc Render(path string) string {\n\t\treturn myBlog.Render(path)\n\t}\n\n\tfunc myResolver(input string) (string, bool) {\n\t\tdata, ok := users.ResolveAny(input)\n\t\tif !ok || data == nil {\n\t\t\treturn \"\", false\n\t\t}\n\t\treturn data.Name(), true\n\t}\n*/\npackage blog // import \"gno.land/p/lou/blog\"\n"},{"name":"errors.gno","body":"package blog\n\nimport \"errors\"\n\nvar (\n\tErrPostAlreadyExists = errors.New(\"post already exists\")\n\n\tErrPostNotFound    = errors.New(\"post not found\")\n\tErrCommentNotFound = errors.New(\"comment not found\")\n\tErrLikesDisabled   = errors.New(\"likes are disabled\")\n\tErrAlreadyLiked    = errors.New(\"this address has already liked\")\n\tErrNotLiked        = errors.New(\"this address hasn't liked yet\")\n\n\tErrInvalidPostID    = errors.New(\"invalid post ID\")\n\tErrInvalidCommentID = errors.New(\"invalid comment ID\")\n\tErrInvalidPost      = errors.New(\"post not found/ invalid\")\n\n\tErrEmptyPrefix  = errors.New(\"prefix cannot be empty\")\n\tErrEmptyTitle   = errors.New(\"title cannot be empty\")\n\tErrEmptyBody    = errors.New(\"body cannot be empty\")\n\tErrEmptySlug    = errors.New(\"slug cannot be empty\")\n\tErrEmptyComment = errors.New(\"content of comment cannot be empty\")\n\n\tErrDeleteFailed        = errors.New(\"delete operation failed\")\n\tErrUnlikeFailed        = errors.New(\"failed to unlike post\")\n\tErrCommentDeleteFailed = errors.New(\"failed to delete comment\")\n\n\tErrInvalidCaller = errors.New(\"invalid caller address\")\n\tErrNotSuperuser  = errors.New(\"address is not owner\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/lou/blog\"\ngno = \"0.9\"\n"},{"name":"header.gno","body":"package blog\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/lou/query\"\n\t\"gno.land/p/moul/md\"\n)\n\n// HeaderLink applies to a function that transform the rawPath to make the suggested query changes (when header is interacted with)\n// will typically returns a md.Link as string\ntype HeaderLink func(rawPath string) string\n\ntype HeaderPreset struct {\n\tLabel   string // title of header (can be your query value, etc...)\n\tDefault string // default value, if none just \"\"\n\tRender  HeaderLink\n}\n\nfunc RenderModeToggle(rawPath string) string {\n\treturn md.Link(\"⊞ grid\", GetGridFmtPath(rawPath)) + \" | \" +\n\t\tmd.Link(\"≔ list\", GetListFmtPath(rawPath))\n}\n\nfunc RenderTimeFormat(rawPath string) string {\n\ttime := query.GetQuery(\"time\", rawPath)\n\tif time == \"\" {\n\t\ttime = \"relative\"\n\t}\n\treturn md.Link(\"⧖ \"+time, GetTimeFmtPath(rawPath, toggleTimeFormat(time)))\n}\n\nfunc RenderSortAlpha(rawPath string) string {\n\tsort := query.GetQuery(\"sort\", rawPath)\n\torder := query.GetQuery(\"order\", rawPath)\n\tif order == \"\" {\n\t\torder = \"desc\"\n\t}\n\talphaOrder := \"(A-Z)\"\n\tif order == \"desc\" \u0026\u0026 sort == \"alpha\" {\n\t\talphaOrder = \"(Z-A)\"\n\t}\n\treturn md.Link(orderArrow(\"alpha\", sort, order)+\" alphabetical \"+alphaOrder,\n\t\tGetSortByAlphabeticalPath(rawPath, order))\n}\n\nfunc RenderSortRecent(rawPath string) string {\n\tsort := query.GetQuery(\"sort\", rawPath)\n\torder := query.GetQuery(\"order\", rawPath)\n\tif order == \"\" {\n\t\torder = \"desc\"\n\t}\n\tpublishedOrder := \" recent\"\n\tif order == \"desc\" \u0026\u0026 sort == \"recent\" {\n\t\tpublishedOrder = \" oldest\"\n\t}\n\treturn md.Link(orderArrow(\"recent\", sort, order)+publishedOrder,\n\t\tGetSortByPublishedPath(rawPath, order))\n}\n\nfunc RenderSortUpdate(rawPath string) string {\n\tsort := query.GetQuery(\"sort\", rawPath)\n\torder := query.GetQuery(\"order\", rawPath)\n\tif order == \"\" {\n\t\torder = \"desc\"\n\t}\n\treturn md.Link(orderArrow(\"update\", sort, order)+\" last updated\",\n\t\tGetSortByUpdatedPath(rawPath, order))\n}\n\nfunc RenderSortCommon(rawPath string) string {\n\tsort := query.GetQuery(\"sort\", rawPath)\n\torder := query.GetQuery(\"order\", rawPath)\n\tif order == \"\" {\n\t\torder = \"desc\"\n\t}\n\tcommonOrder := \" most common\"\n\tif order == \"desc\" \u0026\u0026 sort == \"common\" {\n\t\tcommonOrder = \" least common\"\n\t}\n\treturn md.Link(orderArrow(\"common\", sort, order)+commonOrder,\n\t\tGetSortByCommonPath(rawPath, order))\n}\n\nfunc RenderHeader(rawPath string, presets []HeaderPreset) string {\n\tout := \"\"\n\tfor i, preset := range presets {\n\t\tif preset.Render != nil {\n\t\t\tout += preset.Render(rawPath)\n\t\t} else {\n\t\t\tout += preset.Label + \": \" + preset.Default\n\t\t}\n\t\tif i \u003c len(presets)-1 {\n\t\t\tout += \" | \"\n\t\t}\n\t}\n\treturn out\n}\n\n// TimeRangePreset struct is used for any customisation of the time range feature\ntype TimeRangePreset struct {\n\tLabel string // title of time range\n\tStart string // start time in form of string (check compatibility through `tryParseFlexibleDate`)\n\tEnd   string // end time ^^\n}\n\nvar DefaultTimeRanges = []TimeRangePreset{\n\t{\n\t\t\"past year\",\n\t\ttime.Now().AddDate(-1, 0, 0).Format(\"2006-01-02\"),\n\t\ttime.Now().Format(\"2006-01-02\"),\n\t},\n\t{\n\t\t\"this year\",\n\t\ttime.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location()).Format(\"2006-01-02\"),\n\t\ttime.Now().Format(\"2006-01-02\"),\n\t},\n\t{\n\t\t\"last 30 days\",\n\t\ttime.Now().AddDate(0, 0, -30).Format(\"2006-01-02\"),\n\t\ttime.Now().Format(\"2006-01-02\"),\n\t},\n}\n\nfunc RenderTimeRangeLinks(rawPath string, presets *[]TimeRangePreset) string {\n\tout := \"\"\n\ttimePresets := presets\n\tif presets == nil {\n\t\ttimePresets = \u0026DefaultTimeRanges\n\t}\n\tfor i, r := range *timePresets {\n\t\tlink, _ := query.UpdateQueryFirstValues(rawPath, map[string]string{\n\t\t\t\"start\": r.Start,\n\t\t\t\"end\":   r.End,\n\t\t})\n\t\tout += md.Link(r.Label, link)\n\t\tif i \u003c len(*timePresets)-1 {\n\t\t\tout += \", \"\n\t\t}\n\t}\n\treturn out\n}\n"},{"name":"moderation.gno","body":"package blog\n\nfunc (b *Blog) AddModerator(_ int, rlm realm, addr address) error {\n\treturn b.Authorizable.AddToAuthList(0, rlm, addr)\n}\n\nfunc (b *Blog) DeleteModerator(_ int, rlm realm, addr address) error {\n\treturn b.Authorizable.DeleteFromAuthList(0, rlm, addr)\n}\n\ntype UserResolver func(input string) (string, bool)\n\nfunc CheckAddr(addr address) error {\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidCaller\n\t}\n\treturn nil\n}\n\nfunc CheckUser(addr string, resolver UserResolver) (string, error) {\n\tif CheckAddr(address(addr)) != nil {\n\t\treturn \"\", ErrInvalidCaller\n\t}\n\tif !isRegistered(addr, resolver) {\n\t\treturn addr, nil\n\t}\n\tuserData, _ := resolver(addr)\n\treturn userData, nil\n}\n\nfunc isRegistered(addr string, resolver UserResolver) bool {\n\tif resolver == nil {\n\t\treturn false\n\t}\n\tuserData, _ := resolver(addr)\n\tif userData == \"\" {\n\t\treturn false\n\t}\n\treturn true\n}\n"},{"name":"post.gno","body":"package blog\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Post struct {\n\tid           seqid.ID\n\tslug         string\n\ttitle        string\n\tbody         string\n\tcreatedAt    time.Time\n\tupdatedAt    time.Time\n\tpublishedAt  time.Time\n\ttags         []string\n\tauthors      []string\n\tpublisher    string\n\tlikes        *avl.Tree // addr --\u003e bool\n\tCommentsId   seqid.ID\n\tComments     *avl.Tree // id --\u003e *Comment\n\tCommentsSize int\n\n\tPreviewFooter   string // additional text, e.g. \"via @lou\" or txlink calls\n\tDisableLikes    bool\n\tDisableComments bool\n\tUserResolver    UserResolver\n}\n\nfunc (p Post) ID() string {\n\treturn p.id.String()\n}\n\nfunc (p Post) Slug() string {\n\treturn p.slug\n}\n\nfunc (p Post) Title() string {\n\treturn p.title\n}\n\nfunc (p Post) Body() string {\n\treturn p.body\n}\n\nfunc (p Post) CreatedAt() time.Time {\n\treturn p.createdAt\n}\n\nfunc (p Post) UpdatedAt() time.Time {\n\treturn p.updatedAt\n}\n\nfunc (p Post) PublishedAt() time.Time {\n\treturn p.publishedAt\n}\n\nfunc (p Post) Tags() []string {\n\treturn p.tags\n}\n\nfunc (p Post) Authors() []string {\n\treturn p.authors\n}\n\nfunc (p Post) Publisher() string {\n\treturn p.publisher\n}\n\nfunc (p Post) Likes() int {\n\treturn p.likes.Size()\n}\n\nfunc NewPost(slug, title, body, publicationDate, caller string, authors, tags []string) (*Post, error) {\n\tnewSlug := handleSlug(slug, title)\n\tif title == \"\" {\n\t\treturn nil, ErrEmptyTitle\n\t}\n\tif body == \"\" {\n\t\treturn nil, ErrEmptyBody\n\t}\n\n\tvar err error\n\tpublishedAt := time.Now()\n\tif publicationDate != \"\" {\n\t\tpublishedAt, err = time.Parse(time.RFC3339, publicationDate)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\treturn \u0026Post{\n\t\tslug:            newSlug,\n\t\ttitle:           title,\n\t\tbody:            body,\n\t\tcreatedAt:       publishedAt,\n\t\tupdatedAt:       publishedAt,\n\t\tpublishedAt:     time.Now(),\n\t\ttags:            tags,\n\t\tauthors:         authors,\n\t\tpublisher:       caller,\n\t\tlikes:           avl.NewTree(),\n\t\tCommentsId:      seqid.ID(0),\n\t\tComments:        avl.NewTree(),\n\t\tCommentsSize:    0,\n\t\tPreviewFooter:   \"\",\n\t\tDisableLikes:    false,\n\t\tDisableComments: false,\n\t\tUserResolver:    nil,\n\t}, nil\n}\n\nfunc (p *Post) UpdatePost(newPost *Post) {\n\tif p.UserResolver != nil {\n\t\tp.authors = p.resolveAuthors(p.Authors())\n\t\tp.publisher, _ = CheckUser(p.Publisher(), p.UserResolver)\n\t} else {\n\t\tp.authors = newPost.authors\n\t}\n\n\tp.slug = newPost.slug\n\tp.title = newPost.title\n\tp.body = newPost.body\n\tp.tags = newPost.tags\n\tp.CommentsId = newPost.CommentsId\n\tp.Comments = newPost.Comments\n\tp.likes = newPost.likes\n\tp.DisableLikes = newPost.DisableLikes\n\tp.DisableComments = newPost.DisableComments\n\tp.PreviewFooter = newPost.PreviewFooter\n\tp.updatedAt = time.Now()\n}\n\nfunc (p *Post) AddComment(comment *Comment) error {\n\tif p == nil {\n\t\treturn ErrInvalidPost\n\t}\n\tuser, err := CheckUser(comment.Author(), p.UserResolver)\n\tif err != nil {\n\t\treturn err\n\t}\n\tcomment.author = user\n\n\tp.CommentsId.Next()\n\tp.CommentsSize++\n\tcomment.SetID(p.CommentsId)\n\tp.Comments.Set(p.CommentsId.String(), comment)\n\treturn nil\n}\n\nfunc (p *Post) AddReply(parentID string, reply *Comment) error {\n\tif p == nil {\n\t\treturn ErrInvalidPost\n\t}\n\tuser, err := CheckUser(reply.Author(), p.UserResolver)\n\tif err != nil {\n\t\treturn err\n\t}\n\treply.author = user\n\n\tparent, err := p.GetCommentByID(parentID)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tp.CommentsId.Next()\n\tp.CommentsSize++\n\treply.SetID(p.CommentsId)\n\tparent.Replies.Set(p.CommentsId.String(), reply)\n\treturn nil\n}\n\nfunc (p *Post) EditCommentByID(id string, content string) error {\n\tcomment, err := p.GetCommentByID(id)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif err := comment.Edit(content); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc (p *Post) DeleteCommentById(id string) error {\n\tif _, err := p.GetCommentByID(id); err != nil {\n\t\treturn err\n\t}\n\t_, removed := p.Comments.Remove(id)\n\tif !removed {\n\t\treturn ErrCommentDeleteFailed\n\t}\n\tp.CommentsSize--\n\treturn nil\n}\n\nfunc (p *Post) PinCommentById(id string) error {\n\tcomment, err := p.GetCommentByID(id)\n\tif err != nil {\n\t\treturn ErrCommentNotFound\n\t}\n\tcomment.Pin()\n\treturn nil\n}\n\nfunc (p *Post) LikePost(addr string) error {\n\tif p.DisableLikes {\n\t\treturn ErrLikesDisabled\n\t}\n\tif p.likes.Has(addr) {\n\t\treturn ErrAlreadyLiked\n\t}\n\tp.likes.Set(addr, true)\n\treturn nil\n}\n\nfunc (p *Post) UnlikePost(addr string) error {\n\tif p.DisableLikes {\n\t\treturn ErrLikesDisabled\n\t}\n\tif !p.likes.Has(addr) {\n\t\treturn ErrNotLiked\n\t}\n\tif _, removed := p.likes.Remove(addr); !removed {\n\t\treturn ErrUnlikeFailed\n\t}\n\treturn nil\n}\n\nfunc (p *Post) SetPreviewFooter(footer string) {\n\tp.PreviewFooter = footer\n}\n\nfunc (p Post) GetCommentByID(id string) (*Comment, error) {\n\tcomment := p.Comments.Get(id)\n\tif comment != nil {\n\t\treturn comment.(*Comment), nil\n\t}\n\n\tvar result *Comment\n\tp.Comments.ReverseIterate(\"\", \"\", func(_ string, v interface{}) bool {\n\t\tc := v.(*Comment)\n\t\tif found := searchInReplies(c.Replies, id, \u0026result); found {\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\tif result == nil {\n\t\treturn nil, ErrCommentNotFound\n\t}\n\treturn result, nil\n}\n\nfunc (p Post) GetCommentsByAuthor(author string) []*Comment {\n\tvar comments []*Comment = nil\n\tp.Comments.ReverseIterate(\"\", \"\", func(_ string, comment interface{}) bool {\n\t\tc := comment.(*Comment)\n\t\tif c.Author() == author {\n\t\t\tcomments = append(comments, c)\n\t\t}\n\t\tcomments = append(comments, p.getReplies(c.Replies, author)...)\n\t\treturn false\n\t})\n\treturn comments\n}\n\nfunc (p Post) RenderPreview(gridMode bool, timeFmt, prefix string) string {\n\tout := md.H2(md.Link(p.Title(), prefix+\":posts/\"+p.Slug())) + \"\\n\\n\"\n\tout += md.H5(md.Italic(\"/\"+p.Slug())) + \"\\n\\n\"\n\tif p.UpdatedAt() != p.CreatedAt() {\n\t\tout += formatTime(p.UpdatedAt(), timeFmt) + \" (updated)\\n\\n\"\n\t} else {\n\t\tout += formatTime(p.CreatedAt(), timeFmt) + \"\\n\\n\"\n\t}\n\tif !gridMode {\n\t\tout += md.Bold(\"author(s):\") + \" \" + strings.Join(p.Authors(), \", \") + \"\\n\\n\"\n\t}\n\tif len(p.Tags()) \u003e 0 \u0026\u0026 p.Tags()[0] != \"\" {\n\t\tout += md.Bold(\"tags:\") + \" `\" + strings.Join(p.Tags(), \"`, `\") + \"`\\n\\n\"\n\t}\n\tif !p.DisableComments {\n\t\tout += md.Bold(md.Link(\"comments (\"+strconv.Itoa(p.CommentsSize), prefix+\":posts/\"+p.Slug()+\"#comments\")) + \") \"\n\t}\n\tif !p.DisableLikes {\n\t\tout += ufmt.Sprintf(\"| ❤️ (%d)\\n\\n\", p.Likes())\n\t}\n\tif p.PreviewFooter != \"\" {\n\t\tout += p.PreviewFooter + \"\\n\\n\"\n\t}\n\tout += md.HorizontalRule()\n\treturn out\n}\n\nfunc (p Post) RenderPost(prefix string) string {\n\tresolvedAuthors := p.resolveAuthors(p.Authors())\n\tout := md.H1(p.Title()) + \"\\n\\n\"\n\tif p.Comments.Size() \u003e 0 {\n\t\tout += md.Link(strconv.Itoa(p.CommentsSize)+\" Comment(s)\", \"#comments\") + \"\\n\\n\"\n\t}\n\tout += md.Italic(\"Author(s):\") + \" \" + renderAuthorLinks(prefix, resolvedAuthors) + \"\\n\\n\"\n\tout += p.Body() + \"\\n\\n\"\n\tout += md.HorizontalRule()\n\tout += md.Bold(\"Created on:\") + \" \" + formatTime(p.CreatedAt(), \"full\") + \"\\n\\n\"\n\tout += md.Bold(\"Published on:\") + \" \" + formatTime(p.PublishedAt(), \"full\") + \"\\n\\n\"\n\tif p.UpdatedAt() != p.CreatedAt() {\n\t\tout += md.Bold(\"Last updated:\") + \" \" + formatTime(p.UpdatedAt(), \"full\") + \"\\n\\n\"\n\t}\n\tout += md.Bold(\"Publisher:\") + \" \" + md.Link(p.Publisher(), \"/u/\"+p.Publisher()) + \"\\n\\n\"\n\tout += md.Bold(\"Tags:\") + \" \" + renderTagLinks(prefix, p.Tags()) + \"\\n\\n\"\n\tif !p.DisableLikes {\n\t\tout += ufmt.Sprintf(\"❤️ %d\\n\\n\", p.Likes())\n\t}\n\tif !p.DisableComments {\n\t\tout += md.HorizontalRule()\n\t\tout += p.RenderComments(prefix)\n\t}\n\treturn out\n}\n\nfunc (p Post) RenderComments(prefix string) string {\n\tout := md.H3(\"Comments (\"+strconv.Itoa(p.CommentsSize)+\")\") + \"\\n\\n\"\n\tif p.Comments.Size() == 0 {\n\t\tout += \"No comments yet.\\n\\n\"\n\t\treturn out\n\t}\n\tp.Comments.ReverseIterate(\"\", \"\", func(_ string, v interface{}) bool {\n\t\tcomment := v.(*Comment)\n\t\tif comment.Pinned() {\n\t\t\tout += comment.Render(prefix, 0, p.UserResolver) + \"\\n\\n\"\n\t\t}\n\t\treturn false\n\t})\n\tp.Comments.ReverseIterate(\"\", \"\", func(_ string, v interface{}) bool {\n\t\tcomment := v.(*Comment)\n\t\tif !comment.Pinned() {\n\t\t\tout += comment.Render(prefix, 0, p.UserResolver) + \"\\n\\n\"\n\t\t}\n\t\treturn false\n\t})\n\treturn out\n}\n\nfunc (p Post) getReplies(replies *avl.Tree, author string) []*Comment {\n\tvar comments []*Comment\n\tif replies == nil {\n\t\treturn comments\n\t}\n\treplies.ReverseIterate(\"\", \"\", func(_ string, value any) bool {\n\t\tcomment := value.(*Comment)\n\t\tif comment.Author() == author {\n\t\t\tcomments = append(comments, comment)\n\t\t}\n\t\tif comment.Replies.Size() \u003e 0 {\n\t\t\tcomments = append(comments, p.getReplies(comment.Replies, author)...)\n\t\t}\n\t\treturn false\n\t})\n\treturn comments\n}\n\nfunc (p Post) resolveAuthors(authors []string) []string {\n\tvar resolvedAuthors []string\n\tfor _, author := range authors {\n\t\tuser, err := CheckUser(author, p.UserResolver)\n\t\tif err == nil {\n\t\t\tresolvedAuthors = append(resolvedAuthors, user)\n\t\t} else {\n\t\t\tresolvedAuthors = append(resolvedAuthors, author)\n\t\t}\n\t}\n\treturn resolvedAuthors\n}\n\nfunc renderTagLinks(prefix string, tags []string) string {\n\treturn renderLinks(prefix, \"tags\", tags, \"#\")\n}\n\nfunc renderAuthorLinks(prefix string, authors []string) string {\n\treturn renderLinks(prefix, \"authors\", authors, \"\")\n}\n"},{"name":"query.gno","body":"package blog\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/lou/query\"\n)\n\ntype RenderOptions struct {\n\tSort           string\n\tTimeFormat     string\n\tStartTime      *time.Time\n\tEndTime        *time.Time\n\tAscending      bool\n\tIsGrid         bool\n\tIsAlphabetical bool\n\tIsLastUpdated  bool\n\tIsCommon       bool\n\tHasOrder       bool\n}\n\nfunc ParseRenderOptions(rawPath string) RenderOptions {\n\tsort := query.GetQuery(\"sort\", rawPath)\n\torder := query.GetQuery(\"order\", rawPath)\n\tmode := query.GetQuery(\"mode\", rawPath)\n\ttimeFmt := query.GetQuery(\"time\", rawPath)\n\tstartTime, endTime := parseTimeRange(rawPath)\n\n\treturn RenderOptions{\n\t\tSort:           sort,\n\t\tTimeFormat:     timeFmt,\n\t\tStartTime:      startTime,\n\t\tEndTime:        endTime,\n\t\tAscending:      order == \"asc\" || order == \"\",\n\t\tIsGrid:         mode == \"grid\" || mode == \"\",\n\t\tIsAlphabetical: sort == \"alpha\",\n\t\tIsLastUpdated:  sort == \"update\",\n\t\tIsCommon:       sort == \"common\",\n\t\tHasOrder:       order == \"asc\" || order == \"desc\",\n\t}\n}\n\nfunc GetGridFmtPath(rawPath string) string {\n\tlink, _ := query.UpdateQueryValue(rawPath, \"mode\", \"grid\")\n\treturn link\n}\n\nfunc GetListFmtPath(rawPath string) string {\n\tlink, _ := query.UpdateQueryValue(rawPath, \"mode\", \"list\")\n\treturn link\n}\n\nfunc GetSortByPublishedPath(rawPath, order string) string {\n\tlink, _ := query.UpdateQueryFirstValues(rawPath, map[string]string{\n\t\t\"sort\":  \"recent\",\n\t\t\"order\": toggle(order),\n\t})\n\treturn link\n}\n\nfunc GetTimeFmtPath(rawPath, timeFormat string) string {\n\tlink, _ := query.UpdateQueryValue(rawPath, \"time\", timeFormat)\n\treturn link\n}\n\nfunc GetTimeStartPath(rawPath, startTime string) string {\n\tlink, _ := query.UpdateQueryValue(rawPath, \"start\", startTime)\n\treturn link\n}\n\nfunc GetTimeEndPath(rawPath, endTime string) string {\n\tlink, _ := query.UpdateQueryValue(rawPath, \"end\", endTime)\n\treturn link\n}\n\nfunc GetSortByUpdatedPath(rawPath, order string) string {\n\tlink, _ := query.UpdateQueryFirstValues(rawPath, map[string]string{\n\t\t\"sort\":  \"update\",\n\t\t\"order\": toggle(order),\n\t})\n\treturn link\n}\n\nfunc GetSortByAlphabeticalPath(rawPath, order string) string {\n\tlink, _ := query.UpdateQueryFirstValues(rawPath, map[string]string{\n\t\t\"sort\":  \"alpha\",\n\t\t\"order\": toggle(order),\n\t})\n\treturn link\n}\n\nfunc GetSortByCommonPath(rawPath, order string) string {\n\tlink, _ := query.UpdateQueryFirstValues(rawPath, map[string]string{\n\t\t\"sort\":  \"common\",\n\t\t\"order\": toggle(order),\n\t})\n\treturn link\n}\n\nfunc toggle(current string) string {\n\tif current == \"desc\" {\n\t\treturn \"asc\"\n\t}\n\treturn \"desc\"\n}\n\nfunc toggleTimeFormat(current string) string {\n\tif current == \"relative\" {\n\t\treturn \"short\"\n\t}\n\tif current == \"short\" {\n\t\treturn \"full\"\n\t}\n\tif current == \"full\" {\n\t\treturn \"relative\"\n\t}\n\treturn \"short\"\n}\n\nfunc parseTimeRange(rawPath string) (*time.Time, *time.Time) {\n\tstartStr := query.GetQuery(\"start\", rawPath)\n\tendStr := query.GetQuery(\"end\", rawPath)\n\tvar start, end *time.Time\n\n\tif startStr != \"\" {\n\t\tif t := tryParseFlexibleDate(startStr); t != nil {\n\t\t\tstart = t\n\t\t}\n\t}\n\tif endStr != \"\" {\n\t\tif t := tryParseFlexibleDate(endStr); t != nil {\n\t\t\tend = t\n\t\t}\n\t}\n\treturn start, end\n}\n"},{"name":"query_test.gno","body":"package blog\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestParseRenderOptions(t *testing.T) {\n\tt.Run(\"default options with empty path\", func(t *testing.T) {\n\t\topts := ParseRenderOptions(\"/blog\")\n\t\tuassert.Equal(t, \"\", opts.Sort)\n\t\tuassert.Equal(t, \"\", opts.TimeFormat)\n\t\tuassert.True(t, opts.Ascending)\n\t\tuassert.True(t, opts.IsGrid)\n\t\tuassert.False(t, opts.IsAlphabetical)\n\t\tuassert.False(t, opts.IsLastUpdated)\n\t\tuassert.False(t, opts.IsCommon)\n\t\tuassert.False(t, opts.HasOrder)\n\t})\n\n\tt.Run(\"full options parsing\", func(t *testing.T) {\n\t\turl := \"/blog?sort=alpha\u0026order=desc\u0026mode=list\u0026time=full\"\n\t\topts := ParseRenderOptions(url)\n\t\tuassert.Equal(t, \"alpha\", opts.Sort)\n\t\tuassert.Equal(t, \"full\", opts.TimeFormat)\n\t\tuassert.False(t, opts.Ascending)\n\t\tuassert.False(t, opts.IsGrid)\n\t\tuassert.True(t, opts.IsAlphabetical)\n\t\tuassert.False(t, opts.IsLastUpdated)\n\t\tuassert.False(t, opts.IsCommon)\n\t\tuassert.True(t, opts.HasOrder)\n\t})\n}\n\nfunc TestToggle(t *testing.T) {\n\tuassert.Equal(t, \"asc\", toggle(\"desc\"))\n\tuassert.Equal(t, \"desc\", toggle(\"asc\"))\n\tuassert.Equal(t, \"desc\", toggle(\"\")) // fallback\n}\n\nfunc TestToggleTimeFormat(t *testing.T) {\n\tuassert.Equal(t, \"short\", toggleTimeFormat(\"relative\"))\n\tuassert.Equal(t, \"full\", toggleTimeFormat(\"short\"))\n\tuassert.Equal(t, \"relative\", toggleTimeFormat(\"full\"))\n\tuassert.Equal(t, \"short\", toggleTimeFormat(\"\")) // fallback\n}\n\nfunc TestGetSortHelpers(t *testing.T) {\n\tbase := \"/blog?sort=recent\u0026order=desc\"\n\n\tuassert.Equal(t, \"/blog?mode=grid\", GetGridFmtPath(\"/blog\"))\n\tuassert.Equal(t, \"/blog?mode=list\", GetListFmtPath(\"/blog\"))\n\n\tuassert.Equal(t, \"/blog?order=asc\u0026sort=recent\", GetSortByPublishedPath(base, \"desc\"))\n\tuassert.Equal(t, \"/blog?order=asc\u0026sort=update\", GetSortByUpdatedPath(base, \"desc\"))\n\tuassert.Equal(t, \"/blog?order=asc\u0026sort=alpha\", GetSortByAlphabeticalPath(base, \"desc\"))\n\tuassert.Equal(t, \"/blog?order=asc\u0026sort=common\", GetSortByCommonPath(base, \"desc\"))\n}\n\nfunc TestTimeFormatAndRangeHelpers(t *testing.T) {\n\tbase := \"/blog\"\n\n\tuassert.Equal(t, \"/blog?time=full\", GetTimeFmtPath(base, \"full\"))\n\tuassert.Equal(t, \"/blog?start=2024-01-01\", GetTimeStartPath(base, \"2024-01-01\"))\n\tuassert.Equal(t, \"/blog?end=2025-01-01\", GetTimeEndPath(base, \"2025-01-01\"))\n}\n\nfunc TestParseTimeRange(t *testing.T) {\n\tpath := \"/blog?start=2024-01-01\u0026end=2024-12-31\"\n\tstart, end := parseTimeRange(path)\n\tuassert.NotEqual(t, nil, start)\n\tuassert.NotEqual(t, nil, end)\n\n\tuassert.Equal(t, true, start.Before(*end))\n}\n"},{"name":"render.gno","body":"package blog\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\n\t\"gno.land/p/lou/query\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc (b Blog) Render(path string) string {\n\trouter := mux.NewRouter()\n\trouter.HandleFunc(\"\", b.RenderPosts)\n\trouter.HandleFunc(\"posts\", b.RenderPosts)\n\trouter.HandleFunc(\"posts/{slug}\", b.RenderPost)\n\trouter.HandleFunc(\"tags\", b.RenderTags)\n\trouter.HandleFunc(\"tags/{slug}\", b.RenderTags)\n\trouter.HandleFunc(\"authors\", b.RenderAuthors)\n\trouter.HandleFunc(\"authors/{slug}\", b.RenderAuthors)\n\trouter.HandleFunc(\"commenters\", b.RenderCommenters)\n\trouter.HandleFunc(\"commenters/{slug}\", b.RenderCommenters)\n\treturn router.Render(path)\n}\n\nfunc RenderBlogHeader(prefix, rawPath string) string {\n\trawPath = prefix + \":\" + rawPath\n\theaderPresets := []HeaderPreset{\n\t\t{\"mode\", \"grid\", RenderModeToggle},\n\t\t{\"time\", \"relative\", RenderTimeFormat},\n\t\t{\"alpha\", \"\", RenderSortAlpha},\n\t\t{\"recent\", \"\", RenderSortRecent},\n\t\t{\"update\", \"\", RenderSortUpdate},\n\t\t{\"time-range\", \"\", func(p string) string { return RenderTimeRangeLinks(p, nil) }},\n\t\t{\"reset\", \"\", func(p string) string { return md.Link(\"⟳ reset\", prefix) }},\n\t}\n\treturn RenderHeader(rawPath, headerPresets)\n}\n\n// :posts\nfunc (b Blog) RenderPosts(res *mux.ResponseWriter, req *mux.Request) {\n\tout := md.H1(b.Title()) + \"\\n\"\n\tout += RenderBlogHeader(b.Prefix(), req.RawPath) + \"\\n\\n\"\n\tif b.Posts.Size() == 0 {\n\t\tres.Write(out + \"No posts found.\")\n\t\treturn\n\t}\n\n\toptions := ParseRenderOptions(req.RawPath)\n\tpageSize := 9\n\tif !options.IsGrid {\n\t\tpageSize = 3\n\t}\n\torder := options.Ascending\n\tif options.IsAlphabetical {\n\t\torder = !order\n\t}\n\n\tvar tree *avl.Tree\n\tswitch {\n\tcase options.IsLastUpdated:\n\t\ttree = b.PostsByUpdatedAt\n\tcase options.IsAlphabetical:\n\t\ttree = b.PostsBySlug\n\tdefault:\n\t\ttree = b.Posts\n\t}\n\tif options.StartTime != nil || options.EndTime != nil {\n\t\ttree = b.filterPostsStartEnd(tree, options.StartTime, options.EndTime)\n\t}\n\n\tp := pager.NewPager(tree, pageSize, order)\n\tpage := p.MustGetPageByPath(req.RawPath)\n\tout += b.RenderListGrid(page, options.IsGrid, options.TimeFormat) + page.Picker(req.Path)\n\tres.Write(out)\n}\n\nfunc (b Blog) RenderListGrid(page *pager.Page, gridMode bool, timeFmt string) string {\n\tcolCount := 0\n\tout := \"\u003cgno-columns\u003e\\n\"\n\tfor _, item := range page.Items {\n\t\tif colCount%3 == 0 {\n\t\t\tout += \"\u003cgno-columns\u003e\\n\"\n\t\t}\n\t\tpost := item.Value.(*Post)\n\t\tout += post.RenderPreview(gridMode, timeFmt, b.Prefix())\n\t\tcolCount++\n\t\tif colCount%3 == 0 {\n\t\t\tout += \"\u003c/gno-columns\u003e\\n\"\n\t\t} else if gridMode {\n\t\t\tout += \"\u003cgno-columns-sep\u003e\\n\"\n\t\t}\n\t}\n\tif colCount%3 != 0 {\n\t\tout += \"\u003c/gno-columns\u003e\\n\"\n\t}\n\treturn out\n}\n\n// :posts/{slug}\nfunc (b Blog) RenderPost(res *mux.ResponseWriter, req *mux.Request) {\n\tpostSlug := req.GetVar(\"slug\")\n\tfoundKey, found := b.findPostBySlug(url.PathEscape(postSlug))\n\tif !found {\n\t\tres.Write(\"Post not found.\")\n\t\treturn\n\t}\n\tpost := b.Posts.Get(foundKey)\n\tout := post.(*Post).RenderPost(b.Prefix())\n\n\tres.Write(out)\n}\n\nfunc (b Blog) RenderFilterHeader(req *mux.Request, user string, isListing bool) string {\n\tout := \"\"\n\tfield := strings.Split(req.RawPath, \"/\")[0]\n\tswitch field {\n\tcase \"commenters\":\n\t\tout += md.H2(md.Link(b.Prefix(), b.Prefix()) + \"/commenters/\" + user)\n\t\tout += md.H4(md.Link(\"@\"+user, \"/u/\"+user) + \"'s profile\")\n\tcase \"authors\":\n\t\tout += md.H2(renderBreadcrumb(b.Prefix(), req.Path))\n\t\tif user != \"\" {\n\t\t\tout += md.H4(md.Link(\"@\"+user, \"/u/\"+user) + \"'s profile\")\n\t\t}\n\tdefault:\n\t\tout += md.H2(renderBreadcrumb(b.Prefix(), req.Path))\n\t}\n\n\tvar headerPresets []HeaderPreset\n\tif !isListing {\n\t\theaderPresets = append(headerPresets,\n\t\t\tHeaderPreset{\"mode\", \"grid\", RenderModeToggle},\n\t\t\tHeaderPreset{\"time\", \"relative\", RenderTimeFormat},\n\t\t)\n\t}\n\theaderPresets = append(headerPresets,\n\t\tHeaderPreset{\"alpha\", \"\", RenderSortAlpha},\n\t\tHeaderPreset{\"recent\", \"\", RenderSortRecent},\n\t)\n\n\tif isListing {\n\t\theaderPresets = append(headerPresets, HeaderPreset{\"common\", \"\", RenderSortCommon})\n\t} else {\n\t\theaderPresets = append(headerPresets, HeaderPreset{\"update\", \"\", RenderSortUpdate})\n\t}\n\theaderPresets = append(headerPresets,\n\t\tHeaderPreset{\"time-range\", \"\", func(p string) string { return RenderTimeRangeLinks(p, nil) }},\n\t\tHeaderPreset{\"reset\", \"\", func(p string) string { return md.Link(\"⟳ reset\", b.Prefix()) }},\n\t)\n\tout += RenderHeader(b.Prefix()+\":\"+req.RawPath, headerPresets)\n\treturn out + \"\\n\\n\"\n}\n\n// :tags\nfunc (b Blog) RenderTags(res *mux.ResponseWriter, req *mux.Request) {\n\tout := b.RenderListings(\"tag\", req.GetVar(\"slug\"), req)\n\tres.Write(out)\n}\n\n// :authors\nfunc (b Blog) RenderAuthors(res *mux.ResponseWriter, req *mux.Request) {\n\tout := b.RenderListings(\"author\", req.GetVar(\"slug\"), req)\n\tres.Write(out)\n}\n\n// :commenters\nfunc (b Blog) RenderCommenters(res *mux.ResponseWriter, req *mux.Request) {\n\tif req.GetVar(\"slug\") == \"\" {\n\t\tres.Write(\"Commenter slug is required.\")\n\t\treturn\n\t}\n\tout := b.RenderListings(\"commenter\", req.GetVar(\"slug\"), req)\n\tres.Write(out)\n}\n\nfunc (b Blog) RenderListings(field, value string, req *mux.Request) string {\n\toptions := ParseRenderOptions(req.RawPath)\n\tif query.GetQuery(\"mode\", req.RawPath) == \"\" {\n\t\toptions.IsGrid = false\n\t}\n\tif value == \"\" {\n\t\treturn b.renderListingIndex(field, options, req)\n\t}\n\tuser, err := CheckUser(value, b.UserResolver)\n\tif err == nil \u0026\u0026 user != \"\" {\n\t\treturn b.renderFilteredListing(field, user, options, req)\n\t}\n\treturn b.renderFilteredListing(field, value, options, req)\n}\n\nfunc (b Blog) renderListingIndex(field string, options RenderOptions, req *mux.Request) string {\n\tout := b.RenderFilterHeader(req, \"\", true)\n\tp, exists := b.determinePager(options, field, \"\")\n\tif !exists {\n\t\treturn out + \"No \" + field + \"s found.\"\n\t}\n\tpage := p.MustGetPageByPath(req.RawPath)\n\n\tfor _, item := range page.Items {\n\t\tlabel, count := extractListingDisplay(item.Key, item.Value)\n\t\tout += md.H3(md.Link(label, b.Prefix()+\":\"+field+\"s/\"+label)+\" (\"+ufmt.Sprintf(\"%d\", count)+\")\") + \"\\n\\n\"\n\t}\n\tout += page.Picker(req.Path) + \"\\n\\n\"\n\treturn out\n}\n\nfunc (b Blog) renderFilteredListing(field, value string, options RenderOptions, req *mux.Request) string {\n\tout := b.RenderFilterHeader(req, value, false)\n\tp, exists := b.determinePager(options, field, value)\n\tif !exists {\n\t\tout += \"No posts found for this \" + field + \".\"\n\t}\n\tpage := p.MustGetPageByPath(req.RawPath)\n\tout += \"\u003cgno-columns\u003e\\n\"\n\tcolCount := 0\n\tfor _, item := range page.Items {\n\t\tif colCount%3 == 0 {\n\t\t\tout += \"\u003cgno-columns\u003e\\n\"\n\t\t}\n\t\tcolCount++\n\t\tif field == \"commenter\" {\n\t\t\tout += b.renderCommenterItem(item, options)\n\t\t} else {\n\t\t\tpost := item.Value.(*Post)\n\t\t\tout += post.RenderPreview(options.IsGrid, options.TimeFormat, b.Prefix()) + \"\\n\"\n\t\t}\n\t\tif colCount%3 == 0 {\n\t\t\tout += \"\u003c/gno-columns\u003e\\n\"\n\t\t} else if options.IsGrid {\n\t\t\tout += \"\u003cgno-columns-sep\u003e\\n\"\n\t\t}\n\t}\n\tif colCount%3 != 0 {\n\t\tout += \"\u003c/gno-columns\u003e\\n\"\n\t}\n\tout += page.Picker(req.Path) + \"\\n\\n\"\n\treturn out\n}\n\nfunc (b Blog) renderCommenterItem(item pager.Item, options RenderOptions) string {\n\tcomment := item.Value.(*Comment)\n\tparts := strings.Split(item.Key, \"::\")\n\tif len(parts) \u003c 2 {\n\t\treturn \"\"\n\t}\n\tpostKey := parts[1]\n\n\tpost := b.Posts.Get(postKey)\n\tif post == nil {\n\t\tpost = b.PostsBySlug.Get(postKey)\n\t}\n\tif post == nil {\n\t\tpost = b.PostsByUpdatedAt.Get(postKey)\n\t}\n\tif post == nil {\n\t\treturn \"\"\n\t}\n\tp := post.(*Post)\n\n\tuser, _ := CheckUser(comment.Author(), b.UserResolver)\n\tout := md.H4(b.Mention(\"commenter\", user)) + \"\\n\\n\"\n\tout += comment.Content() + \"\\n\\n\"\n\tout += md.Italic(formatTime(comment.CreatedAt(), options.TimeFormat)) + \"\\n\\n\"\n\tout += \"in \" + md.Link(p.Title(), b.Prefix()+\":posts/\"+p.Slug()) + \"\\n\\n\"\n\tout += md.HorizontalRule()\n\treturn out\n}\n\nfunc (b Blog) determinePager(options RenderOptions, field, value string) (*pager.Pager, bool) {\n\tif value == \"\" {\n\t\tif options.IsAlphabetical \u0026\u0026 field == \"author\" {\n\t\t\treturn pager.NewPager(b.AuthorsIndex, 12, !options.Ascending), true\n\t\t} else if options.IsCommon \u0026\u0026 field == \"author\" {\n\t\t\treturn pager.NewPager(b.AuthorsSorted, 12, options.Ascending), true\n\t\t} else if options.IsAlphabetical \u0026\u0026 field == \"tag\" {\n\t\t\treturn pager.NewPager(b.TagsIndex, 12, !options.Ascending), true\n\t\t} else if options.IsCommon \u0026\u0026 field == \"tag\" {\n\t\t\treturn pager.NewPager(b.TagsSorted, 12, options.Ascending), true\n\t\t} else if field == \"author\" {\n\t\t\treturn pager.NewPager(b.AuthorsIndex, 12, options.Ascending), true\n\t\t} else if field == \"tag\" {\n\t\t\treturn pager.NewPager(b.TagsIndex, 12, options.Ascending), true\n\t\t}\n\t}\n\tpageSize := 9\n\tif !options.IsGrid {\n\t\tpageSize = 3\n\t}\n\n\tfilteredPosts, exists := b.filterPostsByField(field, value, options.Sort)\n\tif options.StartTime != nil || options.EndTime != nil {\n\t\tfilteredPosts = b.filterPostsStartEnd(filteredPosts, options.StartTime, options.EndTime)\n\t}\n\tif options.Sort == \"alpha\" {\n\t\treturn pager.NewPager(filteredPosts, pageSize, !options.Ascending), exists\n\t}\n\treturn pager.NewPager(filteredPosts, pageSize, options.Ascending), exists\n}\n\nfunc orderArrow(targetSort, currentSort, order string) string {\n\tif targetSort != currentSort {\n\t\treturn \"↕\"\n\t}\n\tif order == \"asc\" {\n\t\treturn \"▲\"\n\t}\n\treturn \"▼\"\n}\n"},{"name":"utils.gno","body":"package blog\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc renderLinks(prefix, kind string, values []string, labelPrefix string) string {\n\tlinks := make([]string, len(values))\n\tfor i, val := range values {\n\t\tlabel := labelPrefix + val\n\t\thref := prefix + \":\" + kind + \"/\" + val\n\t\tlinks[i] = md.Link(label, href)\n\t}\n\treturn strings.Join(links, \", \") + \"\\n\"\n}\n\nfunc hasField(fields []string, match string) bool {\n\tfor _, field := range fields {\n\t\tif field == match {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc extractListingDisplay(key string, value interface{}) (label string, count int) {\n\tif strings.Contains(key, \"::\") {\n\t\tparts := strings.Split(key, \"::\")\n\t\tif len(parts) \u003e 1 {\n\t\t\treturn parts[1], value.(int)\n\t\t}\n\t}\n\treturn key, value.(int)\n}\n\nfunc renderBreadcrumb(prefix, path string) string {\n\tsegments := strings.Split(path, \"/\")\n\tbase := prefix\n\tvar parts []string\n\n\tif prefix != \"\" {\n\t\tparts = append(parts, md.Link(prefix, prefix))\n\t}\n\n\tfor i, seg := range segments {\n\t\tif seg == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tbase += \":\" + seg\n\t\tlabel := seg\n\t\tif i == len(segments)-1 {\n\t\t\tlabel = seg\n\t\t\tparts = append(parts, label)\n\t\t\tbreak\n\t\t}\n\t\tparts = append(parts, md.Link(label, base))\n\t}\n\treturn strings.Join(parts, \"/\")\n}\n\nfunc formatTime(t time.Time, format string) string {\n\tswitch format {\n\tcase \"relative\":\n\t\treturn calculateTimeSince(t)\n\tcase \"short\":\n\t\treturn t.Format(\"02 Jan 2006\")\n\tcase \"full\":\n\t\treturn t.Format(\"January 02, 2006 at 3:04 PM\")\n\tdefault:\n\t\treturn calculateTimeSince(t)\n\t}\n}\n\nfunc calculateTimeSince(t time.Time) string {\n\tduration := time.Since(t)\n\tswitch {\n\tcase duration \u003c time.Minute:\n\t\treturn \"just now\"\n\tcase duration \u003c time.Hour:\n\t\tif duration \u003c 2*time.Minute {\n\t\t\treturn \"a minute ago\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"%d minutes ago\", int(duration.Minutes()))\n\tcase duration \u003c 24*time.Hour:\n\t\tif duration \u003c 2*time.Hour {\n\t\t\treturn \"an hour ago\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"%d hours ago\", int(duration.Hours()))\n\tcase duration \u003c 7*24*time.Hour:\n\t\tif duration \u003c 2*24*time.Hour {\n\t\t\treturn \"a day ago\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"%d days ago\", int(duration.Hours()/24))\n\tcase duration \u003c 30*24*time.Hour:\n\t\tif duration \u003c 2*7*24*time.Hour {\n\t\t\treturn \"a week ago\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"%d weeks ago\", int(duration.Hours()/(7*24)))\n\tdefault:\n\t\tif duration \u003c 2*30*24*time.Hour {\n\t\t\treturn \"a month ago\"\n\t\t}\n\t\treturn ufmt.Sprintf(\"%d months ago\", int(duration.Hours()/(30*24)))\n\t}\n}\n\nfunc tryParseFlexibleDate(s string) *time.Time {\n\tformats := []string{\n\t\ttime.RFC3339, // rfc3339 format\n\t\t\"2006-01-02\", // date only\n\t\t\"2006-01\",    // year and month\n\t\t\"2006\",       // year only\n\t}\n\tfor _, format := range formats {\n\t\tif t, err := time.Parse(format, s); err == nil {\n\t\t\treturn \u0026t\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc titleToSlug(title string) string {\n\tslug := strings.ToLower(title)\n\tslug = strings.ReplaceAll(slug, \" \", \"-\")\n\tslug = strings.Map(func(r rune) rune {\n\t\tif r \u003e= 'a' \u0026\u0026 r \u003c= 'z' || r \u003e= '0' \u0026\u0026 r \u003c= '9' || r == '-' {\n\t\t\treturn r\n\t\t}\n\t\treturn -1\n\t}, slug)\n\treturn slug\n}\n\nfunc handleSlug(slug, title string) string {\n\tif slug == \"\" {\n\t\tslug = titleToSlug(title)\n\t}\n\tonce := url.PathEscape(slug)\n\ttwice := url.PathEscape(once)\n\treturn twice\n}\n"},{"name":"z_blog_0_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/lou/blog\"\n)\n\nfunc main(cur realm) {\n\tuser := address(\"g1testuser\")\n\t_, err := blog.NewBlog(0, cur, \"\", user)\n\tif err != nil {\n\t\tprintln(\"error:\", err.Error())\n\t}\n}\n\n// Output:\n// error: title cannot be empty\n"},{"name":"z_blog_1_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/lou/blog\"\n)\n\nfunc main(cur realm) {\n\t_, err := blog.NewBlog(0, cur, \"My Blog\", address(\"he\"))\n\tif err != nil {\n\t\tprintln(\"error:\", err.Error())\n\t}\n}\n\n// Output:\n// error: invalid caller address\n"},{"name":"z_blog_2_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/lou/blog\"\n)\n\nfunc main(cur realm) {\n\tuser := address(\"g14x2crt492f84shxa9s7e5ulk7lnf2p3euz7r9n\")\n\ttestBlog, err := blog.NewBlog(0, cur, \"test blog\", user)\n\tif err != nil {\n\t\tprintln(err.Error())\n\t}\n\tprintln(testBlog.Render(\"\"))\n}\n\n// Output:\n// # test blog\n//\n// [⊞ grid](/r/test:?mode=grid) | [≔ list](/r/test:?mode=list) | [⧖ relative](/r/test:?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/test:?order=asc\u0026sort=alpha) | [↕ recent](/r/test:?order=asc\u0026sort=recent) | [↕ last updated](/r/test:?order=asc\u0026sort=update) | [past year](/r/test:?end=2009-02-13\u0026start=2008-02-13), [this year](/r/test:?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/test:?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/test)\n//\n// No posts found.\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"JRHSIXprTypFuZEFe52MqQOh04JMpNl2kJbOx2rawjeG4RIqMlRkIyUWXnglDoQ9yF2eQyv6QWeYn9+BJxJaDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"charts","path":"gno.land/p/matijamarjanovic/charts","files":[{"name":"bar_chart.gno","body":"package charts\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// GenerateBarChart creates an ASCII bar chart in markdown format\n// values: slice of float values to chart\n// labels: slice of labels for each bar\n// maxWidth: maximum width of bars in characters\n// title: chart title\n// Returns a markdown string representing the chart\nfunc GenerateBarChart(values []float64, labels []string, maxWidth int, title string) string {\n\tif len(values) == 0 || len(labels) == 0 || len(values) != len(labels) {\n\t\treturn \"invalid data for display\"\n\t}\n\n\tif maxWidth \u003c= 0 {\n\t\treturn \"maxWidth must be greater than 0\"\n\t}\n\n\tmaxVal := findMaxValue(values)\n\n\tmaxLabelLength := 0\n\tfor _, label := range labels {\n\t\tif len(label) \u003e maxLabelLength {\n\t\t\tmaxLabelLength = len(label)\n\t\t}\n\t}\n\n\tscale := float64(maxWidth) / maxVal\n\n\toutput := formatChartHeader(title)\n\toutput += \"\\n```\\n\"\n\n\tfor i, value := range values {\n\t\tpadding := strings.Repeat(\" \", maxLabelLength-len(labels[i]))\n\t\toutput += labels[i] + padding + \"    \"\n\n\t\tbarLength := int(value * scale)\n\t\toutput += strings.Repeat(\"█\", barLength)\n\n\t\toutput += \" \" + ufmt.Sprintf(\"%.2f\", value)\n\t\toutput += \"\\n\"\n\t}\n\n\toutput += \"```\\n\"\n\treturn output\n}\n"},{"name":"charts.gno","body":"// Charts is a package that provides functions to generate ASCII charts in markdown format.\n// It includes functions for line charts, bar charts, and column charts.\n//\n// Both line and column charts are implemented in a way that offers normalization of data;\n// In short terms precision of the chart can be adjusted by changing the number of points or columns.\n// Line chart offers a way to adjust the spacing between points as well, which allows for more control over the chart.\n//\n// e.g. Let's say there is a data set of a value of dollar per day in the year of 2025 in a slice of floats.\n// If those values are passed to the `GenerateLineChart` in this manner:\n// `GenerateLineChart(values, 12, 5, \"Dollar per day\", \"Days\", \"Dollars\")`\n// The chart will be normalized to 12 points, with 5 characters of spacing between each point, but more importantly,\n// these points will represent a rough monthly average of the dollar value (months are not of equal length, so this is not an exact science).\n\npackage charts\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst height = 15\n\n// normalizeData reduces the number of data points to maxColumns by averaging\n// Returns nil if values is empty or maxColumns is zero or negative\nfunc normalizeData(values []float64, maxColumns int) []float64 {\n\tif len(values) == 0 || maxColumns \u003c= 0 {\n\t\treturn nil\n\t}\n\n\tif maxColumns \u003e= len(values) {\n\t\tresult := make([]float64, len(values))\n\t\tcopy(result, values)\n\t\treturn result\n\t}\n\n\tresult := make([]float64, maxColumns)\n\tgroupSize := float64(len(values)) / float64(maxColumns)\n\n\tfor i := 0; i \u003c maxColumns; i++ {\n\t\tstart := int(float64(i) * groupSize)\n\t\tend := int(float64(i+1) * groupSize)\n\t\tif end \u003e len(values) {\n\t\t\tend = len(values)\n\t\t}\n\n\t\tsum := 0.0\n\t\tcount := end - start\n\t\tfor j := start; j \u003c end; j++ {\n\t\t\tsum += values[j]\n\t\t}\n\t\tif count \u003e 0 {\n\t\t\tresult[i] = sum / float64(count)\n\t\t}\n\t}\n\n\treturn result\n}\n\n// findMaxValue returns the maximum value in a slice of float64\nfunc findMaxValue(values []float64) float64 {\n\tif len(values) == 0 {\n\t\treturn 0\n\t}\n\n\tmaxVal := values[0]\n\tfor _, v := range values[1:] {\n\t\tif v \u003e maxVal {\n\t\t\tmaxVal = v\n\t\t}\n\t}\n\treturn maxVal\n}\n\n// calculateMaxValueStringLength returns the maximum string length when formatting values\nfunc calculateMaxValueStringLength(values []float64) int {\n\tmaxValStrLen := 0\n\tfor _, v := range values {\n\t\tvalStr := ufmt.Sprintf(\"%.2f\", v)\n\t\tif len(valStr) \u003e maxValStrLen {\n\t\t\tmaxValStrLen = len(valStr)\n\t\t}\n\t}\n\treturn maxValStrLen\n}\n\n// formatChartHeader formats the chart header with title and markdown code block\nfunc formatChartHeader(title string) string {\n\toutput := \"\"\n\tif title != \"\" {\n\t\toutput += \"## \" + title + \"\\n\"\n\t}\n\treturn output\n}\n\n// formatYAxisTitle adds the y-axis title to the chart\nfunc formatYAxisTitle(yAxisTitle string) string {\n\tif yAxisTitle != \"\" {\n\t\treturn yAxisTitle + \"\\n\\n\"\n\t}\n\treturn \"\"\n}\n\n// formatXAxisTitle adds the x-axis title to the chart\nfunc formatXAxisTitle(xAxisTitle string, width int) string {\n\tif xAxisTitle != \"\" {\n\t\tpadding := strings.Repeat(\" \", width/2-len(xAxisTitle)/2)\n\t\treturn \"       \" + padding + xAxisTitle + \"\\n\"\n\t}\n\treturn \"\"\n}\n\n// formatYAxisLabel formats a y-axis label with proper padding\nfunc formatYAxisLabel(value float64, scale float64, yAxisWidth int) string {\n\tyValue := ufmt.Sprintf(\"%.2f\", value/scale)\n\tpadding := strings.Repeat(\" \", yAxisWidth-len(yValue))\n\treturn ufmt.Sprintf(\"%s%s | \", padding, yValue)\n}\n\n// formatYAxisSpace returns spacing for y-axis when no label is needed\nfunc formatYAxisSpace(yAxisWidth int) string {\n\treturn strings.Repeat(\" \", yAxisWidth) + \" | \"\n}\n"},{"name":"charts_test.gno","body":"package charts\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestGenerateLineChart_Basic(t *testing.T) {\n\tvalues := []float64{1, 2, 3, 2, 1}\n\tresult := GenerateLineChart(values, 5, 6, \"Test Line\", \"X\", \"Y\")\n\tuassert.True(t, strings.Contains(result, \"Test Line\"))\n\tuassert.True(t, strings.Contains(result, \"X\"))\n\tuassert.True(t, strings.Contains(result, \"Y\"))\n\tuassert.True(t, strings.Contains(result, \"*\"))\n}\n\nfunc TestGenerateLineChart_EmptyData(t *testing.T) {\n\tresult := GenerateLineChart([]float64{}, 5, 6, \"Title\", \"X\", \"Y\")\n\tuassert.Equal(t, \"no data to display\", result)\n}\n\nfunc TestGenerateLineChart_BadSpacing(t *testing.T) {\n\tvalues := []float64{1, 2, 3}\n\tresult := GenerateLineChart(values, 5, 2, \"Title\", \"X\", \"Y\")\n\tuassert.Equal(t, \"pointSpacing must be at least 6 to fit value labels\", result)\n}\n\nfunc TestGenerateLineChart_Normalization(t *testing.T) {\n\tvalues := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n\tresult := GenerateLineChart(values, 5, 25, \"Norm\", \"X\", \"Y\")\n\tprintln(result)\n\tuassert.True(t, strings.Contains(result, \"Norm\"))\n\tuassert.True(t, len(result) \u003e 0)\n}\n\nfunc TestGenerateBarChart_Basic(t *testing.T) {\n\tvalues := []float64{3, 5, 2}\n\tlabels := []string{\"A\", \"B\", \"C\"}\n\tresult := GenerateBarChart(values, labels, 10, \"Bar Test\")\n\tuassert.True(t, strings.Contains(result, \"Bar Test\"))\n\tuassert.True(t, strings.Contains(result, \"A\"))\n\tuassert.True(t, strings.Contains(result, \"B\"))\n\tuassert.True(t, strings.Contains(result, \"C\"))\n\tuassert.True(t, strings.Contains(result, \"█\"))\n}\n\nfunc TestGenerateBarChart_InvalidData(t *testing.T) {\n\tresult := GenerateBarChart([]float64{}, []string{}, 10, \"Title\")\n\tuassert.Equal(t, \"invalid data for display\", result)\n\tresult2 := GenerateBarChart([]float64{1, 2}, []string{\"A\"}, 10, \"Title\")\n\tuassert.Equal(t, \"invalid data for display\", result2)\n}\n\nfunc TestGenerateBarChart_Widths(t *testing.T) {\n\tvalues := []float64{1, 2}\n\tlabels := []string{\"A\", \"B\"}\n\tresult := GenerateBarChart(values, labels, 5, \"Widths\")\n\tuassert.True(t, strings.Contains(result, \"Widths\"))\n}\n\nfunc TestGenerateColumnChart_Basic(t *testing.T) {\n\tvalues := []float64{2, 4, 3}\n\tresult := GenerateColumnChart(values, 3, \"Col Test\", \"X\", \"Y\")\n\tuassert.True(t, strings.Contains(result, \"Col Test\"))\n\tuassert.True(t, strings.Contains(result, \"X\"))\n\tuassert.True(t, strings.Contains(result, \"Y\"))\n\tuassert.True(t, strings.Contains(result, \"|||||\"))\n}\n\nfunc TestGenerateColumnChart_EmptyData(t *testing.T) {\n\tresult := GenerateColumnChart([]float64{}, 3, \"Title\", \"X\", \"Y\")\n\tuassert.Equal(t, \"no data to display\", result)\n}\n\nfunc TestGenerateColumnChart_Normalization(t *testing.T) {\n\tvalues := []float64{1, 2, 3, 4, 5, 6}\n\tresult := GenerateColumnChart(values, 3, \"Norm\", \"X\", \"Y\")\n\tuassert.True(t, strings.Contains(result, \"Norm\"))\n}\n"},{"name":"column_chart.gno","body":"package charts\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// GenerateColumnChart creates an ASCII column chart in markdown format\n// values: slice of float values to chart\n// maxColumns: maximum number of columns to display (will normalize if exceeded)\n// title: chart title\n// xAxisTitle: title for the x-axis\n// yAxisTitle: title for the y-axis\n// Returns a markdown string representing the chart\nfunc GenerateColumnChart(values []float64, maxColumns int, title, xAxisTitle, yAxisTitle string) string {\n\tif len(values) == 0 {\n\t\treturn \"no data to display\"\n\t}\n\n\tif maxColumns \u003c= 0 {\n\t\treturn \"maxColumns must be greater than 0\"\n\t}\n\n\tmaxVal := findMaxValue(values)\n\n\tdisplayValues := values\n\tif len(values) \u003e maxColumns {\n\t\tdisplayValues = normalizeData(values, maxColumns)\n\t}\n\n\tscale := height / maxVal\n\n\toutput := formatChartHeader(title)\n\toutput += \"\\n```\\n\"\n\toutput += formatYAxisTitle(yAxisTitle)\n\n\tmaxYValue := ufmt.Sprintf(\"%.2f\", maxVal)\n\tyAxisWidth := len(maxYValue)\n\n\tmaxValStrLen := calculateMaxValueStringLength(displayValues)\n\tif maxValStrLen \u003c 2 {\n\t\tmaxValStrLen = 2\n\t}\n\tcolWidth := maxValStrLen + 2\n\n\tfor row := height; row \u003e 0; row-- {\n\t\tif row%3 == 0 {\n\t\t\toutput += formatYAxisLabel(float64(row), scale, yAxisWidth)\n\t\t} else {\n\t\t\toutput += formatYAxisSpace(yAxisWidth)\n\t\t}\n\n\t\tfor _, val := range displayValues {\n\t\t\tscaledHeight := int(val * scale)\n\t\t\tif scaledHeight \u003e= row {\n\t\t\t\tbarWidth := colWidth - 1\n\t\t\t\toutput += strings.Repeat(\"|\", barWidth) + \" \"\n\t\t\t} else {\n\t\t\t\toutput += strings.Repeat(\" \", colWidth)\n\t\t\t}\n\t\t}\n\t\toutput += \"\\n\"\n\t}\n\n\toutput += strings.Repeat(\" \", yAxisWidth) + \" +\"\n\toutput += strings.Repeat(strings.Repeat(\"-\", colWidth), len(displayValues))\n\toutput += \"\\n\"\n\n\toutput += formatValueLabelsColumnChart(displayValues, colWidth, yAxisWidth)\n\n\tif xAxisTitle != \"\" {\n\t\ttotalWidth := len(displayValues) * colWidth\n\t\toutput += formatXAxisTitle(xAxisTitle, totalWidth)\n\t}\n\n\toutput += \"```\\n\"\n\treturn output\n}\n\n// formatValueLabelsColumnChart formats the value labels for the x-axis for column charts\nfunc formatValueLabelsColumnChart(values []float64, colWidth int, yAxisWidth int) string {\n\toutput := strings.Repeat(\" \", yAxisWidth+2)\n\n\tfor i := 0; i \u003c len(values); i++ {\n\t\tx := i * colWidth\n\t\tvalStr := ufmt.Sprintf(\"%.2f\", values[i])\n\t\tlabelPos := x + colWidth/2 - len(valStr)/2\n\t\tif labelPos \u003c 0 {\n\t\t\tlabelPos = 0\n\t\t}\n\n\t\tif i == 0 {\n\t\t\toutput += strings.Repeat(\" \", labelPos)\n\t\t} else {\n\t\t\tprevX := (i - 1) * colWidth\n\t\t\tprevValStr := ufmt.Sprintf(\"%.2f\", values[i-1])\n\t\t\tprevLabelEnd := prevX + colWidth/2 - len(prevValStr)/2 + len(prevValStr)\n\t\t\toutput += strings.Repeat(\" \", labelPos-prevLabelEnd)\n\t\t}\n\t\toutput += valStr\n\t}\n\n\treturn output + \"\\n\"\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/matijamarjanovic/charts\"\ngno = \"0.9\"\n"},{"name":"line_chart.gno","body":"package charts\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// GenerateLineChart creates an ASCII line chart in markdown format\n// values: slice of float values to chart\n// maxPoints: maximum number of points to display (will normalize if exceeded)\n// pointSpacing: horizontal spacing between points (in characters)\n// title: chart title\n// xAxisTitle: title for the x-axis\n// yAxisTitle: title for the y-axis\n// Returns a markdown string representing the chart\nfunc GenerateLineChart(values []float64, maxPoints int, pointSpacing int, title, xAxisTitle, yAxisTitle string) string {\n\tif len(values) == 0 {\n\t\treturn \"no data to display\"\n\t}\n\n\tif maxPoints \u003c= 0 {\n\t\treturn \"maxPoints must be greater than 0\"\n\t}\n\n\tmaxVal := findMaxValue(values)\n\tmaxValStrLen := calculateMaxValueStringLength(values)\n\n\tminSpacing := maxValStrLen + 2\n\tif pointSpacing \u003c minSpacing {\n\t\treturn ufmt.Sprintf(\"pointSpacing must be at least %d to fit value labels\", minSpacing)\n\t}\n\tif pointSpacing \u003e 25 {\n\t\treturn ufmt.Sprintf(\"pointSpacing must be between %d and 25\", minSpacing)\n\t}\n\n\tdisplayValues := values\n\tif len(values) \u003e maxPoints {\n\t\tdisplayValues = normalizeData(values, maxPoints)\n\t}\n\n\tscale := float64(height) / maxVal\n\n\tgridWidth := (len(displayValues)-1)*pointSpacing + len(displayValues)\n\tgrid := make([][]rune, height+1)\n\tfor i := range grid {\n\t\tgrid[i] = make([]rune, gridWidth)\n\t\tfor j := range grid[i] {\n\t\t\tgrid[i][j] = ' '\n\t\t}\n\t}\n\n\tspacing := pointSpacing + 1\n\tfor i := 0; i \u003c len(displayValues); i++ {\n\t\tx := i * spacing\n\t\ty := int(displayValues[i] * scale)\n\t\tif y \u003e height {\n\t\t\ty = height\n\t\t}\n\t\tgrid[height-y][x] = '*'\n\n\t\tif i \u003c len(displayValues)-1 {\n\t\t\tnextX := (i + 1) * spacing\n\t\t\tnextY := int(displayValues[i+1] * scale)\n\t\t\tif nextY \u003e height {\n\t\t\t\tnextY = height\n\t\t\t}\n\t\t\tdx := nextX - x\n\t\t\tdy := nextY - y\n\t\t\tprevInterpY := y\n\t\t\tfor step := 1; step \u003c dx; step++ {\n\t\t\t\tinterpY := y + (dy*step)/dx\n\t\t\t\tstartY := prevInterpY\n\t\t\t\tendY := interpY\n\t\t\t\tif startY \u003e endY {\n\t\t\t\t\tstartY, endY = endY, startY\n\t\t\t\t}\n\t\t\t\tfor fillY := startY; fillY \u003c= endY; fillY++ {\n\t\t\t\t\tgrid[height-fillY][x+step] = '*'\n\t\t\t\t}\n\t\t\t\tprevInterpY = interpY\n\t\t\t}\n\t\t}\n\t}\n\n\toutput := \"\\n\"\n\toutput += formatChartHeader(title)\n\toutput += \"\\n```\\n\"\n\toutput += formatYAxisTitle(yAxisTitle)\n\n\tmaxYValue := ufmt.Sprintf(\"%.2f\", maxVal)\n\tyAxisWidth := len(maxYValue)\n\n\tfor row := 0; row \u003c= height; row++ {\n\t\tgridRow := grid[row]\n\t\tgridY := height - row\n\t\tif gridY%3 == 0 {\n\t\t\toutput += formatYAxisLabel(float64(gridY), scale, yAxisWidth)\n\t\t} else {\n\t\t\toutput += formatYAxisSpace(yAxisWidth)\n\t\t}\n\t\toutput += string(gridRow)\n\t\toutput += \"\\n\"\n\t}\n\n\toutput += strings.Repeat(\" \", yAxisWidth) + \" +\"\n\toutput += strings.Repeat(\"-\", gridWidth+3)\n\toutput += \"\\n\"\n\n\toutput += formatValueLabelsLineChart(displayValues, pointSpacing, yAxisWidth)\n\n\tif xAxisTitle != \"\" {\n\t\toutput += formatXAxisTitle(xAxisTitle, gridWidth)\n\t}\n\n\toutput += \"```\"\n\treturn output\n}\n\n// formatValueLabelsLineChart formats the value labels for the x-axis for line charts\nfunc formatValueLabelsLineChart(displayValues []float64, pointSpacing int, yAxisWidth int) string {\n\toutput := strings.Repeat(\" \", yAxisWidth+2)\n\tspacing := pointSpacing + 1\n\tfor i := 0; i \u003c len(displayValues); i++ {\n\t\tx := i * spacing\n\t\tvalStr := ufmt.Sprintf(\"%.2f\", displayValues[i])\n\t\tlabelPos := x - len(valStr)/2\n\t\tif labelPos \u003c 0 {\n\t\t\tlabelPos = 0\n\t\t}\n\t\tif i == 0 {\n\t\t\toutput += strings.Repeat(\" \", labelPos)\n\t\t} else {\n\t\t\tprevX := (i - 1) * spacing\n\t\t\tprevValStr := ufmt.Sprintf(\"%.2f\", displayValues[i-1])\n\t\t\tprevLabelEnd := prevX - len(prevValStr)/2 + len(prevValStr)\n\t\t\toutput += strings.Repeat(\" \", labelPos-prevLabelEnd)\n\t\t}\n\t\toutput += valStr\n\t}\n\treturn output + \"\\n\"\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"DKUHsobr89qz5qmK+0oV1Kx8ZBA5VY4t9FZmwMzHC0Eq5nJbq3ZLVPcE+9RKCtZC9rUvnVa2HmU8aftceE7MBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"zobrist","path":"gno.land/p/morgan/chess/zobrist","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/morgan/chess/zobrist\"\ngno = \"0.9\"\n"},{"name":"zobrist.gno","body":"// Package zobrist contains an implementation of the Zobrist Hashing algorithm.\n// https://www.chessprogramming.org/Zobrist_Hashing\n//\n// The hash is performed using a series of random numbers generated through\n// rands.go, in this source directory.\npackage zobrist\n\n// Board is a representation of a chess board.\n// Details on how to transform a chess algebraic position into an index\n// can be found at [Square].\ntype Board [64]Piece\n\n// Piece represents a piece on the board.\ntype Piece byte\n\n// Possible values of Piece. Within the context of Board, Piece is assumed to\n// be white, unless p\u0026PieceBlack != 0. Note PieceBlack is not a valid piece; it\n// must be bitwise OR'd to a non-empty piece.\nconst (\n\tPieceEmpty Piece = iota\n\n\tPiecePawn\n\tPieceRook\n\tPieceKnight\n\tPieceBishop\n\tPieceQueen\n\tPieceKing\n\n\tPieceBlack Piece = 8 // bit-flag\n)\n\n// InitialPosition is the zobrist hash (as implemented by this package)\n// for the initial chess position.\nconst InitialPosition uint64 = 1957848132405881468\n\n// Hash generates a Zobrist hash with the given parameter.\n//\n// castling can assume a value between [0,16),\n// epFile should be set to a value between [0,8) -- if there is no en passant\n// file, pass 255 (as a convention).\nfunc Hash(b Board, isBlack bool, castling, epFile byte) uint64 {\n\tvar hash uint64\n\tif isBlack {\n\t\thash ^= hashBlack\n\t}\n\tfor sq, p := range b {\n\t\tif p == PieceEmpty {\n\t\t\tcontinue\n\t\t}\n\t\tif p\u0026PieceBlack == 0 {\n\t\t\t// values 0-5\n\t\t\tp--\n\t\t} else {\n\t\t\t// values 6-11\n\t\t\tp -= PieceBlack | PiecePawn\n\t\t}\n\t\thash ^= hashPieces[sq*12+int(p)]\n\t}\n\thash ^= hashCastling[castling]\n\tif epFile \u003c 8 {\n\t\thash ^= hashEpFile[epFile]\n\t}\n\treturn hash\n}\n\nvar (\n\thashBlack    uint64 = 0x331cf7440c179431\n\thashCastling        = [...]uint64{\n\t\t0xda401e23397e78ca, 0x41dd6246f3cdd0fc, 0xe7a57999953a1784, 0x1e1fdc4db40f6c93,\n\t\t0xff76ddf8b88d7c47, 0xb25c00230f48e677, 0xfdffd279de1e888a, 0x61e89216839cbe34,\n\t\t0xc082fd59e3b8e542, 0x041d42f60a9a6719, 0xa162c6fbfe15b8c0, 0x5105c749ea4f9def,\n\t\t0x68e60d8b032b2f1c, 0xc4c95eb6f2a5600d, 0xd005b4bf6d3d18e1, 0xd18bb1e4fd30b333,\n\t}\n\thashEpFile = [...]uint64{\n\t\t0x986e8dc916fd650b, 0x97dfccc4b5827a70, 0x5888298d167f8b9b, 0x937b5022b30a532b,\n\t\t0x40228426ba8bb614, 0xb7c1c137ace2674e, 0x0c3ef7d3fac8d0be, 0x9cdf9d7b763f2e6c,\n\t}\n\t// 6 pieces * 2 colours * 64 squares = 768 random uint64's.\n\thashPieces = [...]uint64{\n\t\t0x6de9d290a487babc, 0x81095e35de2bc008, 0xfdf0e0d93a5f4c5c, 0xc63827a4cea42d4b,\n\t\t0x103c199f0290bdae, 0x61bf576e61c2d862, 0x178b9ec5427fb45c, 0xd34361ffe3a6abb7,\n\t\t0x4301de67181ccc30, 0xb70e72de5895cf5e, 0xedf154e7e16965fd, 0x543efdb0aeef09d2,\n\t\t0xb2091f463dce5477, 0x0db5bef892262850, 0x69c6bdc6ddb7c1cf, 0x3955a54fd5a60f96,\n\t\t0x74f5e115e8293d45, 0x9e9fb05af1c96e09, 0xab6685da47c28488, 0x80b82ee1a494665d,\n\t\t0x412701df2f89d3a1, 0x8ac06869db920dd2, 0xea7b99e10ba51307, 0xc13365ed0d123d8a,\n\t\t0x42cebc0bcaba8cd7, 0x314a7e49e9b51bcf, 0x5abb9198d4ad0f9d, 0x0a6f4456d77eb317,\n\t\t0x04d2ba6598ffb494, 0x89cdd7e425256b29, 0xfb5853f8b71a1496, 0x2111a3ba29f78227,\n\t\t0x1ac8ec9f94e60981, 0x9c1ea2e3e24da61f, 0x62cf491e74065750, 0x19578de8083259ac,\n\t\t0xb4a576703ccf3d02, 0xb38e458dd8196e9e, 0x42f5d714cd5947e5, 0x7b84ea130bcb7f7f,\n\t\t0xb05f2fcc47356344, 0x87c9c45a24657fe4, 0x815aff9977943588, 0x484c65c342290c44,\n\t\t0x2d326480e45a4f1a, 0x6c06c7db312dc1d2, 0xb6f01ac7c66f5a45, 0xf4c88ad4ff1176db,\n\t\t0x5f921eb462e69949, 0x990de640b9b274e1, 0x9c961b70243bf6ab, 0x51f7bfa5e4bc9575,\n\t\t0x2e89aed81f40939a, 0x78ce647c4760c4b4, 0xf12f7ebcc4f86666, 0x44bce0ebee1fc047,\n\t\t0x19613b81961e8c69, 0x1481b5c8affcba3a, 0x94d6321ba8417f26, 0xa43bf26acbb02a17,\n\t\t0xe0a66cca2572428a, 0x25fd4f9950444f18, 0x33728d2c1bdf953f, 0x8c35d0d7a31a7723,\n\t\t0x93a6641e67e92047, 0x0d27f43da10061d2, 0x560384de9e6b4b4f, 0xec1f847836357e1b,\n\t\t0xe9cd3eaf397443d3, 0x3cad122344e04c1b, 0x80626be75cccc58f, 0xaf6fcf59f731ff23,\n\t\t0x60f92853b13f2480, 0x865fb5e62163b781, 0x6019d5d1170770e9, 0x5e9528c7589f10c4,\n\t\t0x792d01b3672169c2, 0x8f812368414a03f7, 0xf8d7bd34601ad664, 0x13b52975cf7b3251,\n\t\t0xcb4fdf3b50b64338, 0x0536184766d7b464, 0x72ded1ba157bb20f, 0xf8ae92fde84a5c19,\n\t\t0xf2379c8815c2e71c, 0x86a6fda12a95af71, 0x3e18f5f431fd566d, 0x9e0b75a4eacc0b96,\n\t\t0x83ff97e0917f9db3, 0x8bd3889e98157154, 0xbc56ee3a9bcf3b4b, 0x8386739420fd2727,\n\t\t0x160dbcab1f83a9dc, 0x6e4e36335993cf57, 0xb53933016a624b7c, 0x11e7b44599a6a980,\n\t\t0x6f6c927432d695ae, 0x672ead6a7a67c3ce, 0xa313c9ce239a1a1e, 0x1d9b559b13be222a,\n\t\t0x73dfff734fbbee8b, 0x87abe0f6a14be15c, 0x42388fe5e4f904c2, 0x7705ef7492f0dc2d,\n\t\t0xc0e281c867e902b1, 0xbf29803779c71b67, 0xf468583ebde5789a, 0xe28ba371a6cac398,\n\t\t0x917f56b7b1c27398, 0xbf29669535aaa4e0, 0xe744219af67a0621, 0xd5af8e55ff58d5d5,\n\t\t0x72e2cc0e495c7094, 0x72c857412018b79a, 0x1db6efd2b47cb031, 0x9739fb93667f2bbe,\n\t\t0x7151b89b41dcb1bb, 0x24cf2e514e7f33e0, 0xfbc38081840febe1, 0x6eddef1967d0d653,\n\t\t0xf1f91203400ae92c, 0x26b1777c7babae3d, 0x3baf2f2abd8910c8, 0x5b47759ffbd37842,\n\t\t0xffe868968ec9270d, 0x89a7ac0d255b357f, 0xd9b56079f915c43d, 0x422ba52ec00bb2f9,\n\t\t0x18f66fad5c3b238c, 0x2a33af6d60146d64, 0xefdc9649adf7e956, 0x005f5181aa2d41db,\n\t\t0x1dfff9e7449938d3, 0xeabeac20a34e6545, 0x1e786123884674fd, 0x6a719ae518a98556,\n\t\t0x6020b1ea2fe10223, 0x600a04f0521dcb55, 0xa6c947a541137295, 0x80df3344d3cdfe9f,\n\t\t0xc2fdcc9cf8da02bd, 0xa3d52d7633c4b5b8, 0x7596ea450a6c4d79, 0x298299d89d59d5d1,\n\t\t0x0020b9b7c790a672, 0x7867f067a107dbbc, 0x04ae6e4821bbd355, 0x075efcf522abba1d,\n\t\t0x831e0c547977c42e, 0x252a2ccbb9cae9b9, 0xb64ba9e683a44b52, 0x1c271189f6f2fcea,\n\t\t0xde6252635b8a6c1e, 0x4294ae1f329ddd8a, 0xda93eb7bf7d9706f, 0x4382925fe1404821,\n\t\t0xf791164a1b549969, 0xd60e8934c81cb154, 0x81019c98d5254d75, 0x024f3dac35b8e853,\n\t\t0x2d5c20f2e708a079, 0x8efb9c33bf895e8a, 0x378d97d4b623cacc, 0xcb62d165606b574c,\n\t\t0x8ad6ec30e8367851, 0x9fb21d6482f9a6b8, 0xa7725c027baed0e7, 0xb3e15af756bcee91,\n\t\t0x407b67ef1c9c71bc, 0x6f981f475f94d19c, 0x86c26906e61dec18, 0xa263045310ee8935,\n\t\t0x272ac35622e61a11, 0xf6fc4c7c91691cbf, 0x910393ad76ced568, 0x10d3bcbcf6454bf8,\n\t\t0x3c05a3b1703469a0, 0x78029abf41b1bd40, 0x34b4e74ce8b474e9, 0xe3b47e81c39fefa6,\n\t\t0x91d83224fe32db32, 0xe6c96cb196fcc83e, 0xf7b94f6f7231d874, 0x9a834ff1a03f5b38,\n\t\t0x0e98878ae294edc1, 0xff2694b96b00b0dd, 0xae3bdce9265aac7c, 0xc97887009d14f08d,\n\t\t0xb7f44cdd578ec417, 0xe602ed604ef3c824, 0xe352a0ac128bcc3a, 0x79a0b291d4cd9c14,\n\t\t0x5ee4a9e2c195ff16, 0x26fa8a6d4d66e24d, 0xaeafa129bd9c62c7, 0x3da01af74b818094,\n\t\t0x38c499fb2823505b, 0x757a32af70db20da, 0xcd2b0fec6311d81c, 0xd10ed19e0da6460a,\n\t\t0x0f7b060a7707dc32, 0x7042d0c262a6a06b, 0xd0d437e753d560ed, 0xaefcc4e07a948586,\n\t\t0x9516944f8863368c, 0x60cfa52a9cfaf4d5, 0x2e22817497bd2772, 0xb7dfc139fbd79268,\n\t\t0x15ea3777b785a4df, 0xf44cb9592c31d48b, 0x5893e556af63aa5e, 0xb63609fb150e1e1f,\n\t\t0x44920c624c2c144e, 0xd9e13f6f8d504303, 0x09681482e9af2e2c, 0x072c18b4d02a88d0,\n\t\t0xd3f8886bd2e3c58c, 0x081abec6305b42c8, 0xe68c68756cba2638, 0x3f1843fc7969e258,\n\t\t0xb25d2db11a7b6757, 0x0a066242ead527d6, 0xf2a47d93d403bd29, 0xbbf5a65e74ca4d34,\n\t\t0x7ea55dabf5244210, 0x79d3ed1639e00808, 0xb2dbb1f606335295, 0xdf0b2c1256b1b3af,\n\t\t0x2bc9bf68fbf2c035, 0x7aabc308077a3400, 0x945a92d9b807784d, 0xae3a32ff2dd3ad6a,\n\t\t0x63a59bf8137d3c2b, 0xa919df7ac39f4ce3, 0x00c3688b75e3754b, 0xc589f782da55edcb,\n\t\t0xe97758166f728546, 0x3d9ebff5bea838f0, 0xe3f141938b9281dd, 0xc3c3a7a75aafbbac,\n\t\t0x1d6f368464eb8c54, 0x596ce122feada666, 0xf768ffa3509627c0, 0x5ea90c12875fe357,\n\t\t0x11307b7514087de5, 0x1eb75d679ca6ebe1, 0xfa2a85179ecccdc0, 0x73cafcc11d1a19b6,\n\t\t0x7942c51eb73934db, 0xd31925b7bebd3766, 0x4f1975c179b4b082, 0x406ec764a5b0059b,\n\t\t0x7df4dcbd21411be8, 0xaaa5c8df72d0e4f3, 0xfefa53616f4effb2, 0x82dd4177438fcc5b,\n\t\t0x8cc62697b107de0e, 0x140066b1a9e48d6a, 0xa1bc5118a5048eb3, 0x8924bdfe570facc7,\n\t\t0x55cf653d02d670b0, 0xa9130a65e2f0fa04, 0x0f6a1eecdb30d8a3, 0x255cb52477aba452,\n\t\t0xade06ddc6f835cd4, 0xd329541bdbb6b279, 0xdd921ee95ea595ea, 0xaedbba578197ffab,\n\t\t0x85b9b59808076c56, 0x2439ddd88b842c8a, 0xb11190014736ff58, 0x3223b6457cd90b6f,\n\t\t0x5b1c51474d5b443d, 0xca8b496fbed47b11, 0xe371cd43623b4e5f, 0x1e5a62f4040e9242,\n\t\t0xe5baa812538471c2, 0x3914ded0b9c68d7b, 0x211ceb64d9099bbe, 0x7fc33f702220dc78,\n\t\t0x3b5e9c5840a2b8b3, 0xa0e21e2c37d4c30a, 0x169fa2324b2addc3, 0x9b457d6ae103fa48,\n\t\t0x91f1f72f5e258912, 0xeb701bd0b95314c7, 0x7965c7b11ffbb5d7, 0x898b14b385433c77,\n\t\t0xe1ae26dbc9ec4653, 0xac37595684b326d3, 0x84bf7f441b85ef51, 0x0486ee8e1cf0efb2,\n\t\t0xa9b8acdf69f73960, 0xa34d1a08c8076a32, 0x7f9246295970518c, 0x6f656dc6fb980791,\n\t\t0xcfe89b893838d8ab, 0x51e9a6e88c6a59dd, 0x0eacf50f85a334bb, 0x82d23ccf2a49be8a,\n\t\t0xa7d385b0cf2a7d4e, 0xa3a849d79ebac654, 0xbc000cf465923371, 0xa1a3c61b7d9dde7d,\n\t\t0x08878bf2cf37eace, 0xf0ed44ef8046656b, 0xdb7c02c81afd1072, 0x3c7daea881c1c1af,\n\t\t0xba43d6f4df817382, 0x6d828358a59f5f2c, 0x75eebe28937070ad, 0x792df3f928010752,\n\t\t0x0c3be6141c90c5d9, 0x0efec51f83e272a8, 0x9a6f416b0a6df723, 0x04b2e247d3914036,\n\t\t0xcbbaf36eded44848, 0xda39769f8831bdcb, 0x47068c11232c8118, 0xf120b77c9b3e6210,\n\t\t0x52e11d727c031745, 0x2c78f899a17fb88f, 0x95ddc231c0cdd8c2, 0x1b32bca26532b25b,\n\t\t0x31af0dbbe225beec, 0x594e64fbe32f365f, 0xd4218aabd87b5569, 0xbb36aff9719c04cc,\n\t\t0xf44e23c7aa02657a, 0x0f980e87e514d690, 0x02db35ab5497bb72, 0x5adcdefaa2eae880,\n\t\t0x59b3ce68c22fac3d, 0x7dd52a14da3abc88, 0xcdf09a31d10c28cf, 0xc7ac17887fb668da,\n\t\t0xac55650afca95f7d, 0x81374af3db3e7b4b, 0x6c68c53c855bafc7, 0x1406727187441363,\n\t\t0x5ba83433a8bf732d, 0x4a4a968409ab0137, 0x304dd4ec27b37a5e, 0x5c00efb6a1a4b9ed,\n\t\t0xc06eecaa0c82859f, 0x9aa9465883ac52c3, 0x993f3d988706142a, 0x6d7395a0b1e83c09,\n\t\t0xaf8a2b5634ffe143, 0x9769ee0de8694d5e, 0xd439980424a5db0f, 0xb04f5a21742ae9e2,\n\t\t0xbf9059f0b1c4171c, 0x4ff1a289c070f22d, 0x6dd87cd9642d3ca6, 0x9febd09c132f3c88,\n\t\t0x332f6b2973b8d987, 0x14465e925981e26b, 0x5b4074fc700e8910, 0x6b5eb71612931147,\n\t\t0xa796b842757c937c, 0x4becbeca2c5a8d3b, 0xc48f1a998822fbd2, 0xd223e3864f6fa221,\n\t\t0x344cc8ffede3909e, 0x980724e16f14e4f4, 0xb836750d3f50f20b, 0x84c87a98513035a5,\n\t\t0x1dacdc18f768c936, 0xa99bb4ad168e2e38, 0x6e0708645f549d34, 0x9b8d705a0c81bae2,\n\t\t0x54a4ff6b225d98fb, 0x362cfdb47340082c, 0x2c07d8af734835bf, 0x5e04437c1e32b391,\n\t\t0x1be670cba5f4e187, 0xa71852f798fb9887, 0xa06f2d65637c765c, 0x8bbfe9dd54e33f7c,\n\t\t0xf3dd657e0650ecf7, 0x7e42c999e6430f76, 0x6073b783560802b0, 0xb69a6e688e4d8317,\n\t\t0xa0bfb135c7f32d01, 0x5e32d7581368346d, 0x68629a024f2c770c, 0x5018edb9a0dfee9c,\n\t\t0x3a3c1e0d89599a97, 0x95a78f07d2312971, 0x3b813a9323dea87c, 0xd63e9b2a16a2cfc2,\n\t\t0xebb8758e940ae51b, 0x363faaf7e372367f, 0x8f2a34f38a508850, 0xe05b4ef569b14cfc,\n\t\t0x5104b9217fe22504, 0xd349aaf78cb844bc, 0x6236abb35107b232, 0x1635a8e5231cfac6,\n\t\t0x53772874e0d274fb, 0x347c6f99ae78d163, 0x1ca9cc2fb069df18, 0x9aab3fa7c74018cf,\n\t\t0xe7a77550899599d6, 0x14d4ed6292d3f471, 0x21ead5efdaa996a0, 0xbf4fbba5d0ac53ef,\n\t\t0x3bc684e5c1b1722e, 0x2ac4ada0514a8c51, 0x22d6399046a83428, 0x3b08d444ef83104e,\n\t\t0x022c75a7c9f64723, 0xa5ed7da2590be77f, 0x8e721ad2defe24aa, 0x3e69e053e2b187a2,\n\t\t0xce60fe699dcacb24, 0x7e72705bc9af79da, 0x594e8f4f4ae42029, 0x6021f96539b44c14,\n\t\t0x6b8c89769f397818, 0x8866edf93b123c2e, 0xcb158860c0919543, 0x01ff9fe71fb36a63,\n\t\t0x90db5d765d3f5011, 0x46541703158ba9ac, 0x09225f5b655d3ba6, 0x4213c64639e1736f,\n\t\t0x68c8b644dd94355a, 0x87169c4e0431171b, 0xf36646eec0067423, 0x99e7cd8c751aa646,\n\t\t0xcfe626d3031a2171, 0x3ed71337f54757f3, 0x5ac1fd1f4b353432, 0x25dfc52821ea2394,\n\t\t0x35ad1be1cb2ad7be, 0x26139157c61fc26b, 0x35e9e273d88f99e7, 0xa0ee86a30ea7343b,\n\t\t0x886ce1e26d6001d9, 0x014403c0715f7b11, 0x3a6bd359b9f433b7, 0x5513d22bae1f21a1,\n\t\t0xe38a239677ed4277, 0x17b39ca14896c138, 0x41b98a7a2b89109f, 0x6c5b2c21ad260da1,\n\t\t0x45aec24a92505b1e, 0xa0bfe245c932c47d, 0x6318997a226c3ac2, 0x820cac6be1f42886,\n\t\t0x5760c2b35415c521, 0x83e7a063f2e6cd56, 0x76332d7fc2dcb1ff, 0xeb9ac3304d4b72ea,\n\t\t0x00c393e61141c46f, 0x4e0e603506aaef80, 0xd78b9f0a8b9042b6, 0xdb6b1ce781374a54,\n\t\t0x463f921e426d10e0, 0xd3928841c7d7c481, 0x82b84916883bd6a0, 0x339a8eb1581931db,\n\t\t0x004e077b5dc400da, 0xfbe0bb55d0595e84, 0xdebac9094a7416c5, 0xbbe4aeee8f16ea37,\n\t\t0x903143492d3a0958, 0xdc10c884e32309b9, 0x5758329e210f4bfe, 0x9fa79b8124950e9c,\n\t\t0x1dea376717a93e38, 0xd5ecd2f169f0c9a9, 0x12bc43a1112d451f, 0x972c868fd1c0ab91,\n\t\t0x5385dcbe56133869, 0xfdbaa4cb4f597f9e, 0x6e7acd0f42030036, 0xd89476e12d8cbe1f,\n\t\t0x5d567b8c0902e5d7, 0xf5b6430d3246c66b, 0x1ff82d8cc5bf68f2, 0x43939e47df3c2997,\n\t\t0xc20a589e2eccf7ad, 0x55100d45c1468ce9, 0xca1a2bbbfa7f99a0, 0xd97425cce2b3ca74,\n\t\t0xba2c73d6b645b815, 0x5c5b487b827e5c4a, 0x707cd09ac43a88b9, 0x453346a6582fa103,\n\t\t0xf781a361fe52e6d9, 0xfa302baeb75815ca, 0xfae30f25cfc3f559, 0x94a6d9edc13d4d14,\n\t\t0x1ff9243cd4b19e60, 0xd979205825360367, 0x066e3de27c6cf6fb, 0xd4736aabe8eacefe,\n\t\t0xdb64a75b5794885f, 0xc8b376b48351c9dd, 0x2bf1f8e779c49d15, 0x6fc413b9230771e6,\n\t\t0x824bfbefc0392561, 0x0ed0b8ad97a79ce3, 0xa763ee97f261840e, 0xd756fa2efc2781a1,\n\t\t0x61460c93e9a965be, 0x4d9d0b6847a7306d, 0xe9695b4b911b10f6, 0xa27997d0823ddddd,\n\t\t0xf3c563dd03448651, 0xab837e3084a2d65c, 0x5083294e0a4d749b, 0x1218544274486331,\n\t\t0xdc0dc147e39c57b7, 0x67145247fa3b66b2, 0x409e9f1d134bf695, 0x174ec11ccb150efe,\n\t\t0x026cca48bc69b7ca, 0xf7fb076aef504b5a, 0xfa8fdf601acf9ac0, 0xad03549894853639,\n\t\t0xaf315b2f8d954b06, 0x0f28d793bae103a7, 0x46a140f98a954e03, 0x21c1997400961c3c,\n\t\t0x292bf7bd55409eb7, 0xbd0b7669a52edffe, 0x350c5a32dddc5fdf, 0xa4dcf45331142e57,\n\t\t0x9e87a71388559236, 0xd35f7f41c3c1ed37, 0x660b4cb35e186a63, 0x8fc3ca5285b75d19,\n\t\t0x0970820ef13ba1c6, 0xea8d7ee8c0f14148, 0x0ab7ddf3b3d3758b, 0x9d8a0befc4bdb9ba,\n\t\t0xdb917c2a2a832bb7, 0xb2dceb528077e210, 0x4730786f8c9acd54, 0x8903ad39dc1b9b0f,\n\t\t0x6ec2884ffbca1aa1, 0xcb9e6a0660c3e3a2, 0x3162d7fec3b970af, 0xb284839e161a4286,\n\t\t0x982a1a79a1d7766f, 0x2b400e2a1d9714ec, 0xe892501cb2706459, 0x6fd7fb1bff1d3fcb,\n\t\t0x46392fa39c367d6a, 0x9e018a08716c4dc5, 0x167c4c329a9bfa2e, 0x649d2b29dfedb145,\n\t\t0x137591ec13d426ff, 0x9e3363980fe7da47, 0x04c9f9da52a0fae2, 0xd815b33f27fd7410,\n\t\t0x8a96624c4f789f6d, 0x18343837448f64dc, 0xbbd3ab977386ee3b, 0xa2b4ffb4b8f58918,\n\t\t0x1c13fc6f1956a2e8, 0xac7988d677b2d63d, 0xd03ecaf6c2e15145, 0x552528467c783247,\n\t\t0xa136ea87edcfe2ff, 0xc2b017fad6722a8a, 0x496b1d4b56893bd9, 0x8d1b173b6b59ce8a,\n\t\t0x4beb409746a3b1c9, 0x7f42465934340659, 0xd1c5cc9eef153ff2, 0x2d874e915df47096,\n\t\t0xc6262045a8619834, 0x04729a19deab5aa6, 0xc981a87fc69259ee, 0x89a239d61aa270ee,\n\t\t0x83a6ff9710437b1e, 0x44abfd3de24fdbf2, 0x8f3adfda0e3650ef, 0x9d8f73b40a578c2d,\n\t\t0x9a1c451324a30c3c, 0x4b57fe4caf865edf, 0xf5424003e4a17dd6, 0xc5b1561aadb03ab3,\n\t\t0x5b15e2791b0dd4ff, 0xc3f4da34adffddb3, 0x71c22fa8624965b4, 0xf2dc6b254c28af72,\n\t\t0x985e8e4a6b96ac4b, 0x224a9984be53e804, 0xa5c1b977dbe38b7b, 0xf5904fac298383db,\n\t\t0xe046ed14aa4bb4a4, 0x065f2b6273c95e85, 0x25bee6fdd8823f01, 0xc401c8f47f8063ce,\n\t\t0xc457975bb198da7a, 0x5b1b68f651d9f275, 0x27f5ba16132250e2, 0xaf21fd989c053981,\n\t\t0x7f2227281262014d, 0x4acff17cd88624c0, 0x04f1d174b80e66ec, 0x5ab3ec7a8e8c5f45,\n\t\t0x36df34fa8d802366, 0x2dc45158fb0505b9, 0x09c81949f6981e7a, 0x1b40e30915de08e4,\n\t\t0x5de1eae5e97ce5b9, 0xcbd089d1d17fedf7, 0xc61337c3959e5c2d, 0xee10d945d75eba04,\n\t\t0xf842d40598067f10, 0x4aa524d182736f5f, 0xff8dfbef7cf7ed73, 0x1a97605236a60f0b,\n\t\t0x1d627812ff7f0e46, 0xf95f3005af31daeb, 0x16dd691afa094b27, 0xa341a1f55ebdaf16,\n\t\t0xae4427ce089c81ee, 0x2a1d03bbf12ae877, 0x6219e32ebec52155, 0x104b5fccc184df6a,\n\t\t0x38fe4cd53cb1b4f5, 0x4b0f4c6a630ae5f9, 0x7e3736c21fcf82e7, 0xa3c535da32890136,\n\t\t0x957584eefed5c5b6, 0x8c5285471dda564b, 0x4c16c99cf64c3737, 0xc91fabd40b0ba31f,\n\t\t0x68982161074fce8c, 0x3c4e204aba55c7cf, 0x562d7364029f465f, 0x603446fcd29438f5,\n\t\t0x21c6244966890e45, 0x798ff5753194e48e, 0x3047800e51ea3bac, 0xc88043df5d5304a5,\n\t\t0xf0131c1f3a1a6606, 0x68f3be12e32bdb4f, 0x8a32c291670af7a0, 0x6c7639e075126b95,\n\t\t0xc2dc5d872ffdb1d1, 0xd7ef8519a343443f, 0xae36b135b2c6c3ab, 0xc2ba69004ad234ca,\n\t\t0x29ead2a1a6aa4a38, 0x361262755e7e706f, 0x5a691aaf4616d7d4, 0xdc721a4d5412977e,\n\t\t0x3c3b63d739fa7357, 0xe305255d4e333ece, 0x1aa76bbb5ba55887, 0x876f314814e54688,\n\t\t0x145df714c9a7afb5, 0x2f2bc11a57760c1e, 0xd7d2ad3305b9d44c, 0x566c06f97a90736c,\n\t\t0x9a3aaf529e2fd129, 0x29652b7ffebb6213, 0x15c6c74f62425d83, 0x4dc913fc82a43e91,\n\t\t0x7a01f560f603b8dc, 0xafa2291ecf10106d, 0xdc7611daba496735, 0xf81302aab11f65bc,\n\t\t0x743b9b1937af1167, 0xb7174f8520995ae4, 0xca881e5ea17f8705, 0xf605904ebbeda8ea,\n\t\t0x438ad40d293ad385, 0x0bb7d5de136f3d1c, 0xaae0348d3ffc5ff5, 0xf91e7e51f404856a,\n\t\t0x31fbd722068a6fc1, 0x864891d15bb4a89d, 0x6d126f0a4dfa6f1c, 0xd712b3b470fb5aaa,\n\t\t0x210679c2e7f2c4e5, 0x95e4a2f927478150, 0x7dd767a69d4b6e5f, 0x872e096f6218826e,\n\t\t0x377a47cffc920bad, 0xd25c95981469b866, 0xa88505a39da468a5, 0x37be0e4e0c788ab3,\n\t\t0x00eab8b3319105d9, 0x69b5e0512d2ee447, 0x03cc0ab9a4afbd84, 0xd8c48e75c57d27ef,\n\t\t0x50b9fbb4b2a7e28b, 0xcb6adb8d1ff67876, 0x73a1001086e53111, 0xf5ab519c8013349e,\n\t\t0x3cad9c51cbd789aa, 0x6054d55d2dfde2c6, 0x080ed1165d00c782, 0xa5c61ab7d8ad913d,\n\t\t0xf793f44ab7b08399, 0xea808a254cb32744, 0x725543e54ab02d17, 0x1a9e9b7af53bfd57,\n\t\t0x17e4f93f31e1946f, 0xc9c5ddeae73ecd62, 0x97d23bd21fd433cf, 0xa3b275491370d1d6,\n\t\t0xb11f894611aa3032, 0x88812884130146c5, 0x5e1d165feed365f2, 0x74eb3d8a3aa050eb,\n\t\t0xbdb53fef87ce2b77, 0xb56b7c6b8e252272, 0x1578c4dafc9cdd1a, 0xbcc3ebcf5e0e426e,\n\t\t0x050dde0254c70bf1, 0x85570c34369e7dfc, 0x79e9f82cab6ca84c, 0xbc81b8b89e3ebff5,\n\t\t0xc30e1620853f58a0, 0xc93e6d17fd29de5c, 0x22a81e8b93023d85, 0xb050be8554dcae34,\n\t\t0xa21c4e0248fafe46, 0x691a89370cf92c8f, 0x328f846846aaa897, 0xf36e7fd2b6e00d55,\n\t\t0x74eca9441886f398, 0x9b77954cfbc5d37d, 0xa601cc3d8dbd9417, 0x0d227beab2f2188d,\n\t\t0x6332124a8037360e, 0x475b581c1ae4617f, 0xe59ef4433484c860, 0x4ea43c61313e18dc,\n\t\t0x52880f1db6a06c9f, 0x3eda91ae52b504a7, 0x0902fa9c936f035d, 0xdf5807133df966ef,\n\t\t0x8f072fd1b4c332d1, 0x17d50c8eda5f369c, 0xc147ebe8f1c74e79, 0x9c5a22cb3b907ead,\n\t\t0x1d45a8b4b979eef7, 0x50c0493f531caf0b, 0x9b0503132930ad42, 0x1d93c9c4729d622f,\n\t\t0x62035e3181c9deb5, 0x8dfa8a80e9771bee, 0xae35a09d89a02aa2, 0x8692654edf0e67de,\n\t\t0xac06586213c259f1, 0x2a206d5282ec17c0, 0xa460b439cfc938a8, 0x04a0e405ba56c8ae,\n\t\t0x662b08dac8a62507, 0x469b055fe85471a6, 0x155250849d3aa846, 0xa126917e719bd859,\n\t\t0x7424f5be7c4447b0, 0x992b63c06e98a715, 0x3a182aacddbfa805, 0xda0c271e2fc2c6d6,\n\t}\n)\n"},{"name":"zobrist_test.gno","body":"package zobrist\n\nimport (\n\t\"testing\"\n)\n\n// piece character to internal piece\nvar p = [256]Piece{\n\t'P': PiecePawn,\n\t'R': PieceRook,\n\t'N': PieceKnight,\n\t'B': PieceBishop,\n\t'Q': PieceQueen,\n\t'K': PieceKing,\n\n\t'p': PieceBlack | PiecePawn,\n\t'r': PieceBlack | PieceRook,\n\t'n': PieceBlack | PieceKnight,\n\t'b': PieceBlack | PieceBishop,\n\t'q': PieceBlack | PieceQueen,\n\t'k': PieceBlack | PieceKing,\n}\n\n// NewBoard returns a Board normally set up at the initial position for standard\n// chess.\nfunc NewBoard() Board {\n\treturn Board{\n\t\t// row 1\n\t\tp['R'], p['N'], p['B'], p['Q'],\n\t\tp['K'], p['B'], p['N'], p['R'],\n\t\t// row 2\n\t\tp['P'], p['P'], p['P'], p['P'],\n\t\tp['P'], p['P'], p['P'], p['P'],\n\n\t\t// rows 3, 4, 5, 6\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\n\t\t// row 7\n\t\tp['p'], p['p'], p['p'], p['p'],\n\t\tp['p'], p['p'], p['p'], p['p'],\n\t\t// row 8\n\t\tp['r'], p['n'], p['b'], p['q'],\n\t\tp['k'], p['b'], p['n'], p['r'],\n\t}\n}\n\nfunc TestInitialPosition(t *testing.T) {\n\th := Hash(NewBoard(), false, 0, 255)\n\tif h != InitialPosition {\n\t\tt.Fatalf(\"InitialPosition is invalid: set to %d, should be %d\", InitialPosition, h)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"kF+c6vKBaahuEP3tRbqLtWxpGIaJxgCQskm4LO72g9vG3GxnrKCGx/gTHkP9gOecvvwRjoMPVkMbA4gnzXtTBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"chess","path":"gno.land/p/morgan/chess","files":[{"name":"engine.gno","body":"// Package chess implements a simple chess engine, designed to implement all\n// of the official FIDE rules with the intention of validating moves for a\n// \"chess server\" realm (gno.land/r/demo/chess).\n//\n// To implement the rules, the FIDE \"Laws of Chess\" are used as a reference:\n// https://www.fide.com/FIDE/handbook/LawsOfChess.pdf\n//\n// This package was designed with a focus on clarity and on using this code as\n// a didactic tool. Any contributions to the code should respect this.\npackage chess\n\nimport (\n\t\"errors\"\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/morgan/chess/zobrist\"\n)\n\n// PositionFlags. The lower 4 bits indicate an en passant column; the upper\n// 4 indicate castling rights.\ntype PositionFlags byte\n\nconst (\n\tEnPassant PositionFlags = 1 \u003c\u003c (iota + 3)\n\tNoCastleWQ\n\tNoCastleWK\n\tNoCastleBQ\n\tNoCastleBK\n\n\tMaskEnPassant = 7 // low 4 bits\n)\n\n// CastlingRights returns FEN castling rights.\n// https://www.chessprogramming.org/Forsyth-Edwards_Notation#Castling_ability\nfunc (p PositionFlags) CastlingRights() string {\n\ts := \"\"\n\tif p\u0026NoCastleWK == 0 {\n\t\ts += \"K\"\n\t}\n\tif p\u0026NoCastleWQ == 0 {\n\t\ts += \"Q\"\n\t}\n\tif p\u0026NoCastleBK == 0 {\n\t\ts += \"k\"\n\t}\n\tif p\u0026NoCastleBQ == 0 {\n\t\ts += \"q\"\n\t}\n\tif s == \"\" {\n\t\treturn \"-\"\n\t}\n\treturn s\n}\n\n// Position contains the information about a chessboard, and surrounding\n// context: the previous moves, the castling rights and \"en passant\" column.\n//\n// NOTE: the position of a piece is encoded in a [Square].\ntype Position struct {\n\tB     Board\n\tMoves []Move\n\tFlags PositionFlags\n\n\t// Halfmoves since the last pawn move or capture.\n\t// https://www.chessprogramming.org/Halfmove_Clock\n\tHalfMoveClock uint16\n\t// Used to calculate repeating positions (3- 5-fold repetition).\n\t// Zobrist hashing: https://www.chessprogramming.org/Zobrist_Hashing\n\t// Reset together with halfmove clock.\n\tHashes []uint64\n}\n\n// NewPosition returns a new Position, set up with the initial board position.\nfunc NewPosition() Position {\n\treturn Position{\n\t\tB:      NewBoard(),\n\t\tMoves:  make([]Move, 0, 80), // typical chess game is ~40 moves, 80 half-moves\n\t\tHashes: []uint64{zobrist.InitialPosition},\n\t}\n}\n\n// Color of the \"next\" move after p.Moves. (White for even len(p.Moves),\n// Black otherwise)\nfunc (p Position) Color() Color { return Color(len(p.Moves)\u00261 == 1) }\n\n// uintSlice attaches the methods of sort.Interface to []uint64, sorting in increasing order.\ntype uintSlice []uint64\n\nfunc (x uintSlice) Len() int           { return len(x) }\nfunc (x uintSlice) Less(i, j int) bool { return x[i] \u003c x[j] }\nfunc (x uintSlice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }\n\n// maxHashCount counts the maximum number of repeating hashes in p.Hashes.\nfunc (p Position) maxHashCount() int {\n\tif len(p.Hashes) == 0 {\n\t\treturn 0\n\t}\n\tsort.Sort(uintSlice(p.Hashes))\n\tvar last uint64\n\tvar cur, max int\n\tfor _, v := range p.Hashes {\n\t\tif last != v {\n\t\t\tlast = v\n\t\t\tif cur \u003e= max {\n\t\t\t\tmax = cur\n\t\t\t}\n\t\t\tcur = 0\n\t\t}\n\t\tcur++\n\t}\n\tif cur \u003e= max {\n\t\tmax = cur\n\t}\n\treturn max\n}\n\nfunc sign(n int8) int8 {\n\tswitch {\n\tcase n \u003e 0:\n\t\treturn 1\n\tcase n \u003c 0:\n\t\treturn -1\n\tdefault:\n\t\treturn 0\n\t}\n}\n\nfunc abs(n int8) int8 {\n\treturn n * sign(n)\n}\n\n// EncodeFEN encodes p into FEN.\n// https://www.chessprogramming.org/Forsyth-Edwards_Notation\nfunc (p Position) EncodeFEN() string {\n\tvar s string\n\temptyCount := 0\n\t// FEN has different ordering from us, as [0] is a black rook while for us\n\t// is a white rook. So we need to invert the order of rows.\n\tfor i := 56; i \u003e= 0; i++ {\n\t\tv := p.B[i]\n\t\tif v == PieceEmpty {\n\t\t\temptyCount++\n\t\t\tif i%8 == 7 {\n\t\t\t\ts += strconv.Itoa(emptyCount) + \"/\"\n\t\t\t\temptyCount = 0\n\t\t\t\ti -= 16\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t\tif emptyCount \u003e 0 {\n\t\t\ts += strconv.Itoa(emptyCount)\n\t\t\temptyCount = 0\n\t\t}\n\t\ts += v.String()\n\t\tif i%8 == 7 {\n\t\t\ts += \"/\"\n\t\t\ti -= 16\n\t\t}\n\t}\n\t// remove trailing slash\n\ts = s[:len(s)-1]\n\n\tstrs := []string{\n\t\ts,                        // 0: piece placement\n\t\t\"w\",                      // 1: side to move\n\t\tp.Flags.CastlingRights(), // 2: castling ability\n\t\t\"-\",                      // 3: e.p. target square\n\n\t\tstrconv.Itoa(int(p.HalfMoveClock)), // 4: halfmove clock\n\t\tstrconv.Itoa(len(p.Moves)/2 + 1),   // 5: fullmove counter\n\t}\n\n\tvar epFile byte\n\tif p.Flags\u0026EnPassant \u003e 0 {\n\t\tepFile = 'a' + byte(p.Flags\u0026MaskEnPassant)\n\t}\n\n\tif p.Color() == Black {\n\t\tstrs[1] = \"b\"\n\t\tif epFile != 0 {\n\t\t\tstrs[3] = string(epFile) + \"3\"\n\t\t}\n\t} else if epFile != 0 {\n\t\tstrs[3] = string(epFile) + \"6\"\n\t}\n\n\treturn strings.Join(strs, \" \")\n}\n\n// ValidateMove checks whether the given move is legal in Chess.\n//\n// Caller must guarantee m.To and m.From to be valid (\u003c64).\nfunc (p Position) ValidateMove(m Move) (newP Position, valid bool) {\n\tif p.B[m.To].StripColor() == PieceKing {\n\t\treturn\n\t}\n\n\treturn p.validateMove(m)\n}\n\n// validateMove allows for m to be a \"king-capture\" move, which is illegal in\n// chess, but it is useful for InCheck.\n// This is commonly known in chess programming as a \"pseudo-legal\" move.\nfunc (oldp Position) validateMove(m Move) (newPos Position, ok bool) {\n\tp := oldp\n\n\tpiece := p.B[m.From]\n\n\t// piece moved must be of player's color\n\tcolor := p.Color()\n\tif piece == PieceEmpty || piece.Color() != color ||\n\t\t// additionally, check piece has actually moved\n\t\tm.From == m.To {\n\t\treturn\n\t}\n\t// destination must not be occupied by piece of same color\n\tif to := p.B[m.To]; to != PieceEmpty \u0026\u0026 to.Color() == color {\n\t\treturn\n\t}\n\n\t// one of the two necessarily != 0 (consequence of m.From != m.To).\n\tdelta := m.From.Sub(m.To)\n\tdr, dc := delta[0], delta[1]\n\n\t// Keep old castling rights; remove en passant info.\n\tnewFlags := p.Flags \u0026^ (MaskEnPassant | EnPassant)\n\t// Marked as true for succesful promotions.\n\tvar promoted bool\n\n\tisDiag := func() bool {\n\t\t// move diagonally (|dr| == |dc|)\n\t\tif abs(dr) != abs(dc) {\n\t\t\treturn false\n\t\t}\n\t\tsignr, signc := sign(dr), sign(dc)\n\t\t// squares crossed must be empty\n\t\tfor i := int8(1); i \u003c abs(dr); i++ {\n\t\t\tif p.B[m.From.Move(i*signr, i*signc)] != PieceEmpty {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\treturn true\n\t}\n\tisHorizVert := func() bool {\n\t\t// only one of dr, dc must be 0 (horiz/vert movement)\n\t\tif dr != 0 \u0026\u0026 dc != 0 {\n\t\t\treturn false\n\t\t}\n\t\t// squares crossed must be empty\n\t\tfor i := int8(1); i \u003c abs(dr); i++ {\n\t\t\tif p.B[m.From.Move(i*sign(dr), 0)] != PieceEmpty {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\tfor i := int8(1); i \u003c abs(dc); i++ {\n\t\t\tif p.B[m.From.Move(0, i*sign(dc))] != PieceEmpty {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\treturn true\n\t}\n\n\tswitch piece.StripColor() {\n\tcase PieceRook:\n\t\tif !isHorizVert() {\n\t\t\treturn\n\t\t}\n\t\t// if rook has moved from a starting position, this disables castling\n\t\t// on the side of the rook. flag accordingly in the move.\n\t\tvar fg Square\n\t\tif color == Black {\n\t\t\tfg = 7 \u003c\u003c 3\n\t\t}\n\t\tswitch m.From {\n\t\tcase fg: // a-col rook (either side)\n\t\t\tif color == White {\n\t\t\t\tnewFlags |= NoCastleWQ\n\t\t\t} else {\n\t\t\t\tnewFlags |= NoCastleBQ\n\t\t\t}\n\t\tcase fg | 7: // h-col rook (either side)\n\t\t\tif color == White {\n\t\t\t\tnewFlags |= NoCastleWK\n\t\t\t} else {\n\t\t\t\tnewFlags |= NoCastleBK\n\t\t\t}\n\t\t}\n\n\tcase PieceKnight:\n\t\t// move L-shaped\n\t\t// rationale: if you only have positive integers, the only way you can\n\t\t// obtain x * y == 2 is if x,y are either 1,2 or 2,1.\n\t\tif abs(dc*dr) != 2 {\n\t\t\treturn\n\t\t}\n\n\tcase PieceBishop:\n\t\tif !isDiag() {\n\t\t\treturn\n\t\t}\n\n\tcase PieceQueen:\n\t\tif !isHorizVert() \u0026\u0026 !isDiag() {\n\t\t\treturn\n\t\t}\n\n\tcase PieceKing:\n\t\t// castling\n\t\tif abs(dc) == 2 \u0026\u0026 dr == 0 {\n\t\t\t// determine if castle is a valid form of castling for the given color\n\t\t\tctype := m.isCastle(color)\n\t\t\tif ctype == 0 {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif false ||\n\t\t\t\t// check that there are no previous moves which disable castling\n\t\t\t\tp.castlingDisabled(color, ctype) ||\n\t\t\t\t// check that we have the exact board set ups we need\n\t\t\t\t// + make sure that the original and crossed squares are not in check\n\t\t\t\t!p.checkCastlingSetup(ctype) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\t// perform rook move here\n\t\t\tp.B = p.B.castleRookMove(color, ctype)\n\t\t\t// add NoCastle flags to prevent any further castling\n\t\t\tif color == White {\n\t\t\t\tnewFlags |= NoCastleWQ | NoCastleWK\n\t\t\t} else {\n\t\t\t\tnewFlags |= NoCastleBQ | NoCastleBK\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\t// move 1sq in all directions\n\t\tif dc \u003c -1 || dc \u003e 1 || dr \u003c -1 || dr \u003e 1 {\n\t\t\treturn\n\t\t}\n\t\t// king has moved: disable castling.\n\t\tif color == White {\n\t\t\tnewFlags |= NoCastleWQ | NoCastleWK\n\t\t} else {\n\t\t\tnewFlags |= NoCastleBQ | NoCastleBK\n\t\t}\n\n\tcase PiecePawn:\n\t\t// determine direction depending on color\n\t\tdir := int8(1)\n\t\tif color == Black {\n\t\t\tdir = -1\n\t\t}\n\n\t\tswitch {\n\t\tcase dc == 0 \u0026\u0026 dr == dir: // 1sq up\n\t\t\t// destination must be empty (no captures allowed)\n\t\t\tif p.B[m.To] != PieceEmpty {\n\t\t\t\treturn\n\t\t\t}\n\t\tcase dc == 0 \u0026\u0026 dr == dir*2: // 2sq up (only from starting row)\n\t\t\twantRow := Square(1)\n\t\t\tif color == Black {\n\t\t\t\twantRow = 6\n\t\t\t}\n\t\t\t// check starting row, and that two squares are empty\n\t\t\tif (m.From\u003e\u003e3) != wantRow ||\n\t\t\t\tp.B[m.From.Move(int8(dir), 0)] != PieceEmpty ||\n\t\t\t\tp.B[m.To] != PieceEmpty {\n\t\t\t\treturn\n\t\t\t}\n\t\t\t_, col := m.To.Split()\n\t\t\tnewFlags |= EnPassant | PositionFlags(col)\n\t\tcase abs(dc) == 1 \u0026\u0026 dr == dir: // capture on diag\n\t\t\t// must be a capture\n\t\t\tif p.B[m.To] == PieceEmpty {\n\t\t\t\tif sq := p.checkEnPassant(color, m.To); sq != SquareInvalid {\n\t\t\t\t\t// remove other pawn\n\t\t\t\t\tp.B[sq] = PieceEmpty\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\t\t\t// p.B[m.To] is necessarily an opponent piece; we check \u0026 return\n\t\t\t// p.B[m.To].Color == color at the beginning of the fn.\n\t\tdefault: // not a recognized move\n\t\t\treturn\n\t\t}\n\n\t\trow := m.To \u003e\u003e 3\n\t\tif (color == White \u0026\u0026 row == 7) ||\n\t\t\t(color == Black \u0026\u0026 row == 0) {\n\t\t\tswitch m.Promotion {\n\t\t\tcase 0:\n\t\t\t\t// m.To is a king? then this is a pseudo-move check.\n\t\t\t\t// assume queen in that case.\n\t\t\t\tif p.B[m.To].StripColor() != PieceKing {\n\t\t\t\t\t// no promotion given, invalid\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tm.Promotion = PieceQueen\n\t\t\tcase PieceQueen, PieceBishop, PieceKnight, PieceRook:\n\t\t\tdefault:\n\t\t\t\treturn\n\t\t\t}\n\t\t\tpromoted = true\n\t\t\tp.B[m.From] = m.Promotion | color.Piece()\n\t\t}\n\t}\n\n\t// reject moves with promotion if there's nothing to promote\n\tif m.Promotion != 0 \u0026\u0026 !promoted {\n\t\treturn\n\t}\n\n\tif p.B[m.To].StripColor() == PieceKing {\n\t\t// King captures don't check for our own king in check;\n\t\t// these are only \"theoretical\" moves.\n\t\treturn Position{}, true\n\t}\n\n\t// perform board mutation\n\tcapture := p.B[m.To] != PieceEmpty\n\tp.B[m.From], p.B[m.To] = PieceEmpty, p.B[m.From]\n\tp.Flags = newFlags\n\tp.Moves = append([]Move{}, p.Moves...)\n\tp.Moves = append(p.Moves, m)\n\n\t// halfmove clock + hashes logic\n\tif piece.StripColor() == PiecePawn || capture {\n\t\t// reset both\n\t\tp.HalfMoveClock = 0\n\t\tp.Hashes = nil\n\t} else {\n\t\tp.HalfMoveClock++\n\t}\n\tep := byte(255)\n\tif p.Flags\u0026EnPassant != 0 {\n\t\tep = byte(p.Flags \u0026 MaskEnPassant)\n\t}\n\t// Not ideal, but avoids GenMoves potentially polluting \"real moves\" hashes.\n\tp.Hashes = append([]uint64{}, p.Hashes...)\n\tp.Hashes = append(\n\t\tp.Hashes,\n\t\t// color is inverted, because we consider the present move as already\n\t\t// done (hence, it is the other player to move).\n\t\tzobrist.Hash(toZobristBoard(p.B), bool(!color), byte(p.Flags)\u003e\u003e4, ep),\n\t)\n\n\t// is our king in check, as a result of the current move?\n\tif p.B.InCheck(color) {\n\t\treturn\n\t}\n\treturn p, true\n}\n\nfunc toZobristBoard(src Board) zobrist.Board {\n\tvar zb zobrist.Board\n\tfor pos, piece := range src {\n\t\tzb[pos] = zobrist.Piece(piece)\n\t}\n\treturn zb\n}\n\n// used by InCheck to simulate a move by black player.\nvar blackPrevMoves = make([]Move, 1)\n\n// InCheck checks whether the king with the given color is in check.\n// If such king does not exist on the board, InCheck returns false.\n//\n// A king is in check if the move from a piece of the other color\n// towards the king is valid, ignoring any checks on the other color's king.\n//\n// NOTE: the last remark is important:\n// https://lichess.org/analysis/4k3/8/4b3/8/8/8/K3R3/8_w_-_-_0_1?color=white\n// -- this is still a check for white, even if _technically_ black couldn't\n// move the bishop (as that would check its own king)\nfunc (b Board) InCheck(color Color) bool {\n\tpWant := PieceKing | color.Piece()\n\tkingp := b.findPiece(pWant)\n\tif kingp == SquareInvalid {\n\t\treturn false\n\t}\n\n\tpos := Position{B: b}\n\tif color == White {\n\t\t// color == White -\u003e simulate a move by black player -\u003e pos.Moves odd\n\t\tpos.Moves = blackPrevMoves\n\t}\n\n\tfor sq, piece := range b {\n\t\tif piece == PieceEmpty || piece.Color() == color {\n\t\t\tcontinue\n\t\t}\n\t\t_, ok := pos.validateMove(Move{\n\t\t\tFrom: Square(sq),\n\t\t\tTo:   kingp,\n\t\t\t// validateMove (unexp) understands that moves to capture a king are\n\t\t\t// pseudo moves, so it doesn't check for checking on its own king,\n\t\t\t// or promotion.\n\t\t})\n\t\tif ok {\n\t\t\treturn true\n\t\t}\n\t}\n\n\treturn false\n}\n\n// Board is a representation of a chess board.\n// Details on how to transform a chess algebraic position into an index\n// can be found at [Square].\ntype Board [64]Piece\n\n// NewBoard returns a Board normally set up at the initial position for standard\n// chess.\nfunc NewBoard() Board {\n\treturn Board{\n\t\t// row 1\n\t\tp['R'], p['N'], p['B'], p['Q'],\n\t\tp['K'], p['B'], p['N'], p['R'],\n\t\t// row 2\n\t\tp['P'], p['P'], p['P'], p['P'],\n\t\tp['P'], p['P'], p['P'], p['P'],\n\n\t\t// rows 3, 4, 5, 6\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 0,\n\n\t\t// row 7\n\t\tp['p'], p['p'], p['p'], p['p'],\n\t\tp['p'], p['p'], p['p'], p['p'],\n\t\t// row 8\n\t\tp['r'], p['n'], p['b'], p['q'],\n\t\tp['k'], p['b'], p['n'], p['r'],\n\t}\n}\n\nfunc (b Board) findPiece(pWant Piece) Square {\n\tfor sq, p := range b {\n\t\tif p == pWant {\n\t\t\treturn Square(sq)\n\t\t}\n\t}\n\treturn SquareInvalid\n}\n\nfunc (p Position) checkCastlingSetup(typ byte) bool {\n\t// set up correct row and piece flags according to color\n\tc := p.Color()\n\tb := p.B\n\tvar fg Square\n\tvar pfg Piece\n\tif c == Black {\n\t\tfg, pfg = 7\u003c\u003c3, PieceBlack\n\t}\n\n\t// _cross are the squares that the king starts from,\n\t// crosses and \"lands\". they are recorded as they must all be\n\t// not in check by any opponent piece.\n\tvar _cross [3]Square\n\n\tif typ == 'K' {\n\t\tif !(b[fg|4] == pfg|PieceKing \u0026\u0026\n\t\t\tb[fg|5] == PieceEmpty \u0026\u0026\n\t\t\tb[fg|6] == PieceEmpty \u0026\u0026\n\t\t\tb[fg|7] == pfg|PieceRook) {\n\t\t\treturn false\n\t\t}\n\t\t_cross = [3]Square{fg | 4, fg | 5, fg | 6}\n\t} else {\n\t\tif !(b[fg|4] == pfg|PieceKing \u0026\u0026\n\t\t\tb[fg|3] == PieceEmpty \u0026\u0026\n\t\t\tb[fg|2] == PieceEmpty \u0026\u0026\n\t\t\tb[fg|1] == PieceEmpty \u0026\u0026\n\t\t\tb[fg|0] == pfg|PieceRook) {\n\t\t\treturn false\n\t\t}\n\t\t_cross = [3]Square{fg | 4, fg | 3, fg | 2}\n\t}\n\n\ttestb := p.B\n\tfor _, sq := range _cross {\n\t\ttestb[sq] = pfg | PieceKing\n\t\tif testb.InCheck(c) {\n\t\t\treturn false\n\t\t}\n\t\ttestb[sq] = PieceEmpty\n\t}\n\n\treturn true\n}\n\nfunc (b Board) castleRookMove(c Color, typ byte) Board {\n\tvar fg Square\n\tvar pfg Piece\n\tif c == Black {\n\t\tfg, pfg = 7\u003c\u003c3, PieceBlack\n\t}\n\n\tif typ == 'K' {\n\t\tb[fg|7], b[fg|5] = PieceEmpty, PieceRook|pfg\n\t} else {\n\t\tb[fg|0], b[fg|3] = PieceEmpty, PieceRook|pfg\n\t}\n\treturn b\n}\n\nfunc (p Position) castlingDisabled(color Color, kind byte) bool {\n\tif kind != 'K' \u0026\u0026 kind != 'Q' {\n\t\treturn false\n\t}\n\n\t// Determine what flag we're looking for.\n\tvar want PositionFlags\n\tswitch {\n\tcase color == White \u0026\u0026 kind == 'K':\n\t\twant = NoCastleWK\n\tcase color == White \u0026\u0026 kind == 'Q':\n\t\twant = NoCastleWQ\n\tcase color == Black \u0026\u0026 kind == 'K':\n\t\twant = NoCastleBK\n\tcase color == Black \u0026\u0026 kind == 'Q':\n\t\twant = NoCastleBQ\n\t}\n\n\treturn p.Flags\u0026want != 0\n}\n\nfunc (p Position) checkEnPassant(c Color, sq Square) Square {\n\trow, col := sq.Split()\n\tif p.Flags\u0026EnPassant == 0 ||\n\t\t(c == White \u0026\u0026 row != 5) ||\n\t\t(c == Black \u0026\u0026 row != 2) {\n\t\treturn SquareInvalid\n\t}\n\twantCol := byte(p.Flags \u0026 MaskEnPassant)\n\n\tif col != wantCol {\n\t\treturn SquareInvalid\n\t}\n\n\tif c == White {\n\t\treturn Square(4\u003c\u003c3 | col)\n\t}\n\treturn Square(3\u003c\u003c3 | col)\n}\n\n// InsufficientMaterial tests for insufficient material on the board, in which\n// case it returns true.\n//\n// See this reference for the rules used:\n// https://www.chess.com/article/view/how-chess-games-can-end-8-ways-explained#insufficient-material\n//\n//   - king vs king\n//   - king+N/B vs king\n//   - king+NN vs king\n//   - king+N/B vs king+N/B\nfunc (bd Board) InsufficientMaterial() bool {\n\t// strategy:\n\t// store the pieces which could count for an insufficient material\n\t// scenario in w, b.\n\t// if we encounter any pawn, queen, or rook, material is sufficient.\n\t// if we encounter 2 bishops, material is sufficient.\n\t// afterwards, we verify that w and b are one of the possible insuf material\n\t// scenarios.\n\n\tconst (\n\t\timN  byte = 1 \u003c\u003c iota // knight\n\t\timN2                  // second knight\n\t\timB                   // bishop\n\t)\n\tvar w, b byte\n\tfor _, p := range bd {\n\t\tstrip := p.StripColor()\n\t\tswitch strip {\n\t\tcase PieceQueen, PiecePawn, PieceRook:\n\t\t\treturn false\n\t\tcase PieceKing, PieceEmpty:\n\t\t\tcontinue\n\t\t}\n\t\t// strip is one of PieceBishop PieceKnight\n\t\tt := \u0026w\n\t\tif p.Color() == Black {\n\t\t\tt = \u0026b\n\t\t}\n\t\tif strip == PieceBishop {\n\t\t\tif *t\u0026imB != 0 {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\t*t |= imB\n\t\t} else {\n\t\t\tif *t\u0026imN2 != 0 {\n\t\t\t\t// third knight (ie. from pawn promotion)\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tif *t\u0026imN != 0 {\n\t\t\t\t*t |= imN2\n\t\t\t} else {\n\t\t\t\t*t |= imN\n\t\t\t}\n\t\t}\n\t}\n\t// make it so that w is bigger than b\n\tif b \u003e w {\n\t\tw, b = b, w\n\t}\n\tswitch [2]byte{w, b} {\n\tcase [2]byte{0, 0}, // king vs king\n\t\t[2]byte{imN, 0}, // king+N/B vs king\n\t\t[2]byte{imB, 0},\n\t\t[2]byte{imN | imN2, 0}, // king+NN vs king\n\t\t[2]byte{imN, imN},      // king+N/B vs king+N/B\n\t\t[2]byte{imB, imB},\n\t\t[2]byte{imB, imN}: // imB \u003e imN, so [2]byte{imN, imB} cannot happen\n\t\treturn true\n\t}\n\treturn false\n}\n\n// GenMoves implements a rudimentary move generator.\n// This is not used beyond aiding in determing stalemate and doing perft tests.\n// Each generated move is passed to cb.\n// If cb returns an error, it is returned without processing further moves.\nfunc (p Position) GenMoves(cb func(Position, Move) error) error {\n\tcolor := p.Color()\n\tfor sq, piece := range p.B {\n\t\tif piece == PieceEmpty || piece.Color() != color {\n\t\t\tcontinue\n\t\t}\n\n\t\tfrom := Square(sq)\n\n\t\tpstrip := piece.StripColor()\n\t\t// If the piece is a pawn, and they are on the second last row, we know\n\t\t// that whatever move they do (advance, or take diagonally) they're going\n\t\t// to promote.\n\t\tprom := pstrip == PiecePawn \u0026\u0026\n\t\t\t((color == White \u0026\u0026 from\u003e\u003e3 == 6) ||\n\t\t\t\t(color == Black \u0026\u0026 from\u003e\u003e3 == 1))\n\n\t\t// delta generator needs to know if p is black\n\t\tif pstrip == PiecePawn \u0026\u0026 color == Black {\n\t\t\tpstrip |= Black.Piece()\n\t\t}\n\n\t\tvar err error\n\t\tdeltaGenerator(pstrip, func(delta Delta) byte {\n\t\t\t// create move; if the resulting square is oob, continue\n\t\t\tm := Move{\n\t\t\t\tFrom: from,\n\t\t\t\tTo:   from.Apply(delta),\n\t\t\t}\n\t\t\tif m.To == SquareInvalid ||\n\t\t\t\t(p.B[m.To] != PieceEmpty \u0026\u0026 p.B[m.To].Color() == color) {\n\t\t\t\treturn deltaGenStopLinear\n\t\t\t}\n\n\t\t\t// handle promotion case\n\t\t\tif prom {\n\t\t\t\tm.Promotion = PieceQueen\n\t\t\t}\n\n\t\t\t// if it's a valid move, call cb on it\n\t\t\tnewp, ok := p.ValidateMove(m)\n\t\t\tif !ok {\n\t\t\t\treturn deltaGenOK\n\t\t\t}\n\t\t\tif err = cb(newp, m); err != nil {\n\t\t\t\treturn deltaGenStop\n\t\t\t}\n\n\t\t\t// if we've promoted, handle the cases where we've promoted to a non-queen.\n\t\t\tif !prom {\n\t\t\t\treturn deltaGenOK\n\t\t\t}\n\n\t\t\tfor _, promPiece := range [...]Piece{PieceRook, PieceKnight, PieceBishop} {\n\t\t\t\tnewp.B[m.To] = promPiece | color.Piece()\n\t\t\t\tm.Promotion = promPiece\n\t\t\t\tif err = cb(newp, m); err != nil {\n\t\t\t\t\treturn deltaGenStop\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn deltaGenOK\n\t\t})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nconst (\n\t// carry on normally\n\tdeltaGenOK = iota\n\t// if the generator is doing a linear attack (ie. rook, bishop, queen),\n\t// then stop that (there is a piece of same colour in the way.)\n\tdeltaGenStopLinear\n\t// abort generation asap.\n\tdeltaGenStop\n)\n\n/*func init() {\n\tfor i := PiecePawn; i \u003c= PieceKing; i++ {\n\t\tprintln(\"generator \", i.String())\n\t\tdeltaGenerator(i, func(d Delta) byte {\n\t\t\tprintln(\"  \", d[0], d[1])\n\t\t\treturn deltaGenOK\n\t\t})\n\t}\n}*/\n\n// deltaGenerator generates the possible ways in which p can move.\n// the callback may return one of the three deltaGen* values.\nfunc deltaGenerator(p Piece, cb func(d Delta) byte) {\n\tdoLinear := func(d Delta) bool {\n\t\tfor i := int8(1); i \u003c= 7; i++ {\n\t\t\tswitch cb(d.Mul(i)) {\n\t\t\tcase deltaGenStopLinear:\n\t\t\t\treturn false\n\t\t\tcase deltaGenStop:\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n\trotate := func(d Delta, lin bool) bool {\n\t\tfor i := 0; i \u003c 4; i++ {\n\t\t\tif lin {\n\t\t\t\tif doLinear(d) {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif cb(d) == deltaGenStop {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t}\n\n\t\t\td = d.Rot()\n\t\t}\n\t\treturn false\n\t}\n\n\t// In the following, we use logical OR's to do conditional evaluation\n\t// (if the first item returns true, the second won't be evaluated)\n\tswitch p {\n\tcase PiecePawn, PiecePawn | PieceBlack:\n\t\tdir := int8(1)\n\t\tif p.Color() == Black {\n\t\t\tdir = -1\n\t\t}\n\t\t// try moving 1sq forward; if we get StopLinear, don't try to do 2sq.\n\t\tfw := cb(Delta{dir, 0})\n\t\tif fw == deltaGenStop {\n\t\t\treturn\n\t\t}\n\t\tif fw != deltaGenStopLinear {\n\t\t\tif cb(Delta{dir * 2, 0}) == deltaGenStop {\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\n\t\t_ = cb(Delta{dir, 1}) == deltaGenStop ||\n\t\t\tcb(Delta{dir, -1}) == deltaGenStop\n\n\tcase PieceRook:\n\t\trotate(Delta{0, 1}, true)\n\tcase PieceBishop:\n\t\trotate(Delta{1, 1}, true)\n\tcase PieceKnight:\n\t\t_ = rotate(Delta{1, 2}, false) ||\n\t\t\trotate(Delta{2, 1}, false)\n\tcase PieceQueen:\n\t\t_ = rotate(Delta{0, 1}, true) ||\n\t\t\trotate(Delta{1, 1}, true)\n\tcase PieceKing:\n\t\t_ = rotate(Delta{0, 1}, false) ||\n\t\t\trotate(Delta{1, 1}, false) ||\n\t\t\tcb(Delta{0, 2}) == deltaGenStop ||\n\t\t\tcb(Delta{0, -2}) == deltaGenStop\n\t}\n}\n\n// Outcome is returned by IsFinished, and indicates if the game has finished,\n// and additionally if either of the player can claim draw by the 50-move rule\n// or by 3-fold repetition.\n//\n// Either one of the sequential outcomes will be set (values 1-5), indicating\n// that the game is terminated, or the low 3 bits will be 0, and\n// the Can* flags may be set.\ntype Outcome byte\n\nconst (\n\tNotFinished Outcome = iota\n\tCheckmate\n\tStalemate\n\tDrawn75Move\n\tDrawn5Fold\n\n\tCan50Move       Outcome = 8\n\tCan3Fold        Outcome = 16\n\tCanInsufficient Outcome = 32\n)\n\nvar errFound = errors.New(\"found\")\n\n// IsFinished determines if the game at the given position can be considered\n// \"finished\".\n// See [Outcome] for the possible results.\nfunc (p Position) IsFinished() Outcome {\n\terr := p.GenMoves(func(Position, Move) error {\n\t\treturn errFound\n\t})\n\t// If there is any legal move, this is not any kind of mate.\n\tif err != nil {\n\t\tmhc := p.maxHashCount()\n\t\tswitch {\n\t\tcase mhc \u003e= 5:\n\t\t\treturn Drawn5Fold\n\t\tcase p.HalfMoveClock \u003e= 150:\n\t\t\treturn Drawn75Move\n\t\t}\n\t\tvar o Outcome\n\t\tif mhc \u003e= 3 {\n\t\t\to |= Can3Fold\n\t\t}\n\t\tif p.HalfMoveClock \u003e= 100 {\n\t\t\to |= Can50Move\n\t\t}\n\t\tif p.B.InsufficientMaterial() {\n\t\t\to |= CanInsufficient\n\t\t}\n\t\treturn o\n\t}\n\n\t// No legal moves. Is the king in check?\n\tif p.B.InCheck(p.Color()) {\n\t\treturn Checkmate\n\t}\n\treturn Stalemate\n}\n\n// Color determines a player's color -- either white or black.\ntype Color bool\n\nconst (\n\tWhite Color = false\n\tBlack Color = true\n)\n\n// Piece returns the color as a piece to be OR'd into a Piece;\n// ie. 0 on White, and [PieceBlack] on black.\nfunc (c Color) Piece() Piece {\n\tif c == White {\n\t\treturn 0\n\t}\n\treturn PieceBlack\n}\n\n// Piece represents a piece on the board.\ntype Piece byte\n\n// PieceFromChar returns the piece corresponding to the given character.\n// White pieces are uppercase, and black pieces are lowercase.\n// If a piece is invalid, PieceInvalid is returned.\nfunc PieceFromChar(b byte) Piece {\n\treturn p[b]\n}\n\n// piece character to internal piece\nvar p = [256]Piece{\n\t'P': PiecePawn,\n\t'R': PieceRook,\n\t'N': PieceKnight,\n\t'B': PieceBishop,\n\t'Q': PieceQueen,\n\t'K': PieceKing,\n\n\t'p': PieceBlack | PiecePawn,\n\t'r': PieceBlack | PieceRook,\n\t'n': PieceBlack | PieceKnight,\n\t'b': PieceBlack | PieceBishop,\n\t'q': PieceBlack | PieceQueen,\n\t'k': PieceBlack | PieceKing,\n}\n\nvar pstring = [PieceBlack | PieceKing + 1]byte{\n\tPiecePawn:                'P',\n\tPieceRook:                'R',\n\tPieceKnight:              'N',\n\tPieceBishop:              'B',\n\tPieceQueen:               'Q',\n\tPieceKing:                'K',\n\tPieceBlack | PiecePawn:   'p',\n\tPieceBlack | PieceRook:   'r',\n\tPieceBlack | PieceKnight: 'n',\n\tPieceBlack | PieceBishop: 'b',\n\tPieceBlack | PieceQueen:  'q',\n\tPieceBlack | PieceKing:   'k',\n}\n\nfunc (p Piece) String() string {\n\tif int(p) \u003e= len(pstring) {\n\t\treturn \"\"\n\t}\n\tv := pstring[p]\n\tif v == 0 {\n\t\treturn \"\"\n\t}\n\treturn string(v)\n}\n\n// Possible values of Piece. Within the context of Board, Piece is assumed to\n// be white, unless p\u0026PieceBlack != 0. Note PieceBlack is not a valid piece; it\n// must be bitwise OR'd to a non-empty piece.\nconst (\n\tPieceEmpty Piece = iota\n\n\tPiecePawn\n\tPieceRook\n\tPieceKnight\n\tPieceBishop\n\tPieceQueen\n\tPieceKing\n\n\tPieceBlack Piece = 8 // bit-flag\n)\n\n// Color returns the color of the piece.\nfunc (p Piece) Color() Color { return Color(p\u0026PieceBlack != 0) }\n\n// Piece returns the given Piece without color information.\nfunc (p Piece) StripColor() Piece { return p \u0026^ PieceBlack }\n\n// Switch switches the color of the given piece.\nfunc (p Piece) Switch() Piece {\n\tif p.Color() == Black {\n\t\treturn p \u0026^ PieceBlack\n\t}\n\treturn p | PieceBlack\n}\n\n// Delta represents a 2d vector for indicating a movement from one square\n// to another. The first value indicates the change in column, the second the\n// change in rows.\ntype Delta [2]int8\n\n// Valid ensures the two values of delta are valid.\nfunc (d Delta) Valid() bool {\n\treturn d[0] \u003e= -7 \u0026\u0026 d[0] \u003c= 7 \u0026\u0026\n\t\td[1] \u003e= -7 \u0026\u0026 d[1] \u003c= 7 \u0026\u0026\n\t\t!(d[0] == 0 \u0026\u0026 d[1] == 0)\n}\n\n// Rot applies a 90 degree anti-clockwise rotation to d.\nfunc (d Delta) Rot() Delta {\n\t// Rationale: this is just matrix-vector multiplication.\n\t// 90 deg rotation is just the matrix {0, -1; 1, 0}.\n\treturn Delta{d[1], -d[0]}\n}\n\n// Mul multiplies both values by n, otherwise known as scalar product.\nfunc (d Delta) Mul(n int8) Delta {\n\treturn Delta{d[0] * n, d[1] * n}\n}\n\n// Square encodes piece position information, in chess the \"square\" the piece is on.\n// Indexing 0 as the LSB, bits 0-3 indicate the column and bits 4-6 indicate\n// the row. For instance, square 44 (decimal) is:\n//\n//\t44 = 0b00 101  100  = d5\n//\t          ^row ^col\n//\n// (note: in algebraic notation, this is swapped: the letter represents the\n// column, and the number represents the row).\ntype Square byte\n\n// SquareInvalid is returned by some Square-related methods to indicate\n// invalid parameters.\nconst SquareInvalid Square = 255\n\n// String returns p in algebraic notation.\nfunc (q Square) String() string {\n\tif q \u003e= 64 {\n\t\treturn \"\u003cinvalid\u003e\"\n\t}\n\treturn string(q\u00267+'a') + string(q\u003e\u003e3+'1')\n}\n\n// SquareFromString returns Square, reading the human-readable algebraic\n// notation in s. s must be 2 bytes long, with the first byte a letter included\n// between ['a'; 'h'], and the second a number included between ['1';'8'].\n// If s is invalid, SquareInvalid is returned.\nfunc SquareFromString(s string) Square {\n\tif len(s) != 2 {\n\t\treturn SquareInvalid\n\t}\n\tcol, row := s[0]-'a', s[1]-'1'\n\t// because s[0] is a byte, if s[0] \u003c 'a' then the above will underflow and\n\t// row will be \u003e= 8 (same for col).\n\tif row \u003e= 8 || col \u003e= 8 {\n\t\treturn SquareInvalid\n\t}\n\treturn Square(row\u003c\u003c3 | col)\n}\n\n// Move changes the square of q, moving it vertically according to dr\n// (delta row) and horizontally according to dc (delta column).\n// If the resulting square is not on the board, then SquareInvalid is returned.\nfunc (q Square) Move(dr, dc int8) Square {\n\tif q == SquareInvalid || !(Delta{dr, dc}).Valid() {\n\t\treturn SquareInvalid\n\t}\n\n\trow, col := int8(q\u003e\u003e3), int8(q\u00267)\n\trow += dr\n\tcol += dc\n\n\tnr, nc := Square(row), Square(col)\n\tif nr \u003e= 8 || nc \u003e= 8 {\n\t\treturn SquareInvalid\n\t}\n\treturn nr\u003c\u003c3 | nc\n}\n\n// Apply applies the given delta to the square.\n// It is shorthand for q.Move(d[0], d[1]).\nfunc (q Square) Apply(d Delta) Square { return q.Move(d[0], d[1]) }\n\n// Split splits Square into its components.\n// This function does not check if p is invalid.\nfunc (q Square) Split() (row, col byte) {\n\treturn byte(q \u003e\u003e 3), byte(q \u0026 7)\n}\n\n// SplitI works like [Square.Split], but returns int8's instead\n// of bytes.\nfunc (q Square) SplitI() (row, col int8) {\n\treturn int8(q \u003e\u003e 3), int8(q \u0026 7)\n}\n\n// Sub calculates the difference between the two squares.\n// q is the originating square, s is the ending square. The difference in\n// rows and columns from q to s is returned; for instance, d1.Sub(a4) yields\n// Delta{3, -3}.\nfunc (q Square) Sub(s Square) Delta {\n\tfr, fc := q.SplitI()\n\ttr, tc := s.SplitI()\n\treturn Delta{tr - fr, tc - fc}\n}\n\n// Move represents a chess game move.\ntype Move struct {\n\tFrom, To  Square\n\tPromotion Piece\n}\n\n// String returns a string representation of Move.\n// It is in the form of \"Long Algebraic Notation\".\n// https://backscattering.de/chess/uci/#move-lan\nfunc (m Move) String() string {\n\tp := \"\"\n\tif m.Promotion != 0 {\n\t\tp = string(m.Promotion.String()[0] + ('a' - 'A'))\n\t}\n\treturn m.From.String() + m.To.String() + p\n}\n\nvar (\n\tcastleWhiteQ = Move{From: SquareFromString(\"e1\"), To: SquareFromString(\"c1\")}\n\tcastleWhiteK = Move{From: SquareFromString(\"e1\"), To: SquareFromString(\"g1\")}\n\tcastleBlackQ = Move{From: SquareFromString(\"e8\"), To: SquareFromString(\"c8\")}\n\tcastleBlackK = Move{From: SquareFromString(\"e8\"), To: SquareFromString(\"g8\")}\n)\n\n// returns 0, 'K' or 'Q'.\nfunc (m Move) isCastle(c Color) (kind byte) {\n\tif c == White {\n\t\tswitch m {\n\t\tcase castleWhiteQ:\n\t\t\treturn 'Q'\n\t\tcase castleWhiteK:\n\t\t\treturn 'K'\n\t\t}\n\t} else {\n\t\tswitch m {\n\t\tcase castleBlackQ:\n\t\t\treturn 'Q'\n\t\tcase castleBlackK:\n\t\t\treturn 'K'\n\t\t}\n\t}\n\treturn 0\n}\n"},{"name":"engine_test.gno","body":"package chess\n\nimport (\n\t\"fmt\"\n\t\"strconv\"\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc movePromo(from, to string, promo Piece) Move {\n\tm := move(from, to)\n\tm.Promotion = promo\n\treturn m\n}\n\nfunc move(from, to string) Move {\n\treturn Move{\n\t\tFrom: SquareFromString(strings.ToLower(from)),\n\t\tTo:   SquareFromString(strings.ToLower(to)),\n\t}\n}\n\nfunc TestCheckmate(t *testing.T) {\n\tfp := unsafeFEN(\"rn1qkbnr/pbpp1ppp/1p6/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR w KQkq - 0 1\")\n\tm := move(\"f3\", \"f7\")\n\tnewp, ok := fp.ValidateMove(m)\n\tif !ok {\n\t\tt.Fatal(\"ValidateMove returned false\")\n\t}\n\tmr := newp.IsFinished()\n\tif mr != Checkmate {\n\t\tt.Fatalf(\"expected Checkmate (%d), got %d\", Checkmate, mr)\n\t}\n}\n\nfunc TestCheckmateFromFEN(t *testing.T) {\n\tfp := unsafeFEN(\"rn1qkbnr/pbpp1Qpp/1p6/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 1\")\n\tmr := fp.IsFinished()\n\tif mr != Checkmate {\n\t\tt.Fatalf(\"expected Checkmate (%d), got %d\", Checkmate, mr)\n\t}\n}\n\nfunc TestStalemate(t *testing.T) {\n\tfp := unsafeFEN(\"k1K5/8/8/8/8/8/8/1Q6 w - - 0 1\")\n\tm := move(\"b1\", \"b6\")\n\tnewp, ok := fp.ValidateMove(m)\n\tif !ok {\n\t\tt.Fatal(\"ValidateMove rejected move\")\n\t}\n\tmr := newp.IsFinished()\n\tif mr != Stalemate {\n\t\tt.Fatalf(\"expected Stalemate (%d), got %d\", Stalemate, mr)\n\t}\n}\n\n// position shouldn't result in check/stalemate\n// because pawn can move http://en.lichess.org/Pc6mJDZN#138\nfunc TestNotMate(t *testing.T) {\n\tfp := unsafeFEN(\"8/3P4/8/8/8/7k/7p/7K w - - 2 70\")\n\tm := movePromo(\"d7\", \"d8\", PieceQueen)\n\tnewp, ok := fp.ValidateMove(m)\n\tif !ok {\n\t\tt.Fatal(\"ValidateMove returned false\")\n\t}\n\tmr := newp.IsFinished()\n\tif mr != NotFinished {\n\t\tt.Fatalf(\"expected NotFinished (%d), got %d\", NotFinished, mr)\n\t}\n}\n\nfunc TestXFoldRepetition(t *testing.T) {\n\tp := NewPosition()\n\tloop := [...]Move{\n\t\tmove(\"g1\", \"f3\"),\n\t\tmove(\"g8\", \"f6\"),\n\t\tmove(\"f3\", \"g1\"),\n\t\tmove(\"f6\", \"g8\"),\n\t}\n\tvar valid bool\n\tfor i := 0; i \u003c 5; i++ {\n\t\tfor j, m := range loop {\n\t\t\tp, valid = p.ValidateMove(m)\n\t\t\tif !valid {\n\t\t\t\tt.Fatalf(\"move %s not considered valid\", m.String())\n\t\t\t}\n\t\t\tfini := p.IsFinished()\n\t\t\tswitch {\n\t\t\tcase (i == 3 \u0026\u0026 j == 3) || i == 4:\n\t\t\t\t// after the fourth full iteration, it should be marked as \"drawn\" for 5-fold.\n\t\t\t\tif fini != Drawn5Fold {\n\t\t\t\t\tt.Errorf(\"i: %d j: %d; expect %d got %d\", i, j, Drawn5Fold, fini)\n\t\t\t\t}\n\t\t\tcase (i == 1 \u0026\u0026 j == 3) || i \u003e= 2:\n\t\t\t\t// After the second full iteration, IsFinished should mark this as \"can 3 fold\"\n\t\t\t\tif fini != Can3Fold {\n\t\t\t\t\tt.Errorf(\"i: %d j: %d; expect %d got %d\", i, j, Can3Fold, fini)\n\t\t\t\t}\n\t\t\tdefault:\n\t\t\t\tif fini != NotFinished {\n\t\t\t\t\tt.Errorf(\"i: %d j: %d; expect %d got %d\", i, j, NotFinished, fini)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc assertMoves(p Position, moves ...Move) Position {\n\tvar valid bool\n\tfor _, move := range moves {\n\t\tp, valid = p.ValidateMove(move)\n\t\tif !valid {\n\t\t\tpanic(\"invalid move\")\n\t\t}\n\t}\n\treturn p\n}\n\nfunc TestXFoldRepetition2(t *testing.T) {\n\t// Like TestXFoldRepetition, but starts after the initial position.\n\n\tp := assertMoves(\n\t\tNewPosition(),\n\t\tmove(\"f2\", \"f4\"),\n\t\tmove(\"c7\", \"c5\"),\n\t)\n\n\tloop := [...]Move{\n\t\tmove(\"g1\", \"f3\"),\n\t\tmove(\"g8\", \"f6\"),\n\t\tmove(\"f3\", \"g1\"),\n\t\tmove(\"f6\", \"g8\"),\n\t}\n\tvar valid bool\n\tfor i := 0; i \u003c 5; i++ {\n\t\tfor j, m := range loop {\n\t\t\tp, valid = p.ValidateMove(m)\n\t\t\tif !valid {\n\t\t\t\tt.Fatalf(\"move %s not considered valid\", m.String())\n\t\t\t}\n\t\t\tfini := p.IsFinished()\n\t\t\tswitch {\n\t\t\tcase i == 4:\n\t\t\t\tif fini != Drawn5Fold {\n\t\t\t\t\tt.Errorf(\"i: %d j: %d; expect %d got %d\", i, j, Drawn5Fold, fini)\n\t\t\t\t}\n\t\t\tcase i \u003e= 2:\n\t\t\t\tif fini != Can3Fold {\n\t\t\t\t\tt.Errorf(\"i: %d j: %d; expect %d got %d\", i, j, Can3Fold, fini)\n\t\t\t\t}\n\t\t\tdefault:\n\t\t\t\tif fini != NotFinished {\n\t\t\t\t\tt.Errorf(\"i: %d j: %d; expect %d got %d\", i, j, NotFinished, fini)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc TestXMoveRule(t *testing.T) {\n\tp := NewPosition()\n\n\tp.HalfMoveClock = 99\n\tnewp := assertMoves(p, move(\"g1\", \"f3\"))\n\tfini := newp.IsFinished()\n\tif fini != Can50Move {\n\t\tt.Errorf(\"want %d got %d\", Can50Move, fini)\n\t}\n\n\tp.HalfMoveClock = 149\n\tnewp = assertMoves(p, move(\"g1\", \"f3\"))\n\tfini = newp.IsFinished()\n\tif fini != Drawn75Move {\n\t\tt.Errorf(\"want %d got %d\", Drawn75Move, fini)\n\t}\n}\n\nfunc TestInsufficientMaterial(t *testing.T) {\n\tfens := []string{\n\t\t\"8/2k5/8/8/8/3K4/8/8 w - - 1 1\",\n\t\t\"8/2k5/8/8/8/3K1N2/8/8 w - - 1 1\",\n\t\t\"8/2k5/8/8/8/3K1B2/8/8 w - - 1 1\",\n\t\t\"8/2k5/2b5/8/8/3K1B2/8/8 w - - 1 1\",\n\t\t\"8/2k1n1n1/8/8/8/3K4/8/8 w - - 1 1\",\n\t\t\"8/2k1b3/8/8/8/3K1B2/8/8 w - - 1 1\",\n\t}\n\tfor _, f := range fens {\n\t\tpos := unsafeFEN(f)\n\t\to := pos.IsFinished()\n\t\tif o != CanInsufficient {\n\t\t\tt.Errorf(\"fen %q: want %d got %d\", f, CanInsufficient, o)\n\t\t}\n\t}\n}\n\nfunc TestSufficientMaterial(t *testing.T) {\n\tfens := []string{\n\t\t\"8/2k5/8/8/8/3K1B2/4N3/8 w - - 1 1\",\n\t\t\"8/2k5/8/8/8/3KBB2/8/8 w - - 1 1\",\n\t\t\"8/2k5/8/8/4P3/3K4/8/8 w - - 1 1\",\n\t\t\"8/2k5/8/8/8/3KQ3/8/8 w - - 1 1\",\n\t\t\"8/2k5/8/8/8/3KR3/8/8 w - - 1 1\",\n\t}\n\tfor _, f := range fens {\n\t\tpos := unsafeFEN(f)\n\t\to := pos.IsFinished()\n\t\tif o != NotFinished {\n\t\t\tt.Errorf(\"fen %q: want %d got %d\", f, NotFinished, o)\n\t\t}\n\t}\n}\n\ntype moveTest struct {\n\tpos     unsafeFENRes\n\tm       Move\n\tpostPos unsafeFENRes\n}\n\nvar (\n\tinvalidMoves = []moveTest{\n\t\t// out of turn moves\n\t\t{m: move(\"E7\", \"E5\"), pos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\")},\n\t\t{m: move(\"E2\", \"E4\"), pos: unsafeFEN(\"rnbqkbnr/1ppppppp/p7/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1\")},\n\t\t// pawn moves\n\t\t{m: move(\"E2\", \"D3\"), pos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\")},\n\t\t{m: move(\"E2\", \"F3\"), pos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\")},\n\t\t{m: move(\"E2\", \"E5\"), pos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\")},\n\t\t{m: move(\"A2\", \"A1\"), pos: unsafeFEN(\"8/8/8/8/8/8/p7/8 b - - 0 1\")},\n\t\t{m: move(\"E6\", \"E5\"), pos: unsafeFEN(`2b1r3/2k2p1B/p2np3/4B3/8/5N2/PP1K1PPP/3R4 b - - 2 1`)},\n\t\t{m: move(\"H7\", \"H5\"), pos: unsafeFEN(`2bqkbnr/rpppp2p/2n2p2/p5pB/5P2/4P3/PPPP2PP/RNBQK1NR b KQk - 4 6`)},\n\t\t// knight moves\n\t\t{m: move(\"E4\", \"F2\"), pos: unsafeFEN(\"8/8/8/3pp3/4N3/8/5B2/8 w - - 0 1\")},\n\t\t{m: move(\"E4\", \"F3\"), pos: unsafeFEN(\"8/8/8/3pp3/4N3/8/5B2/8 w - - 0 1\")},\n\t\t// bishop moves\n\t\t{m: move(\"E4\", \"C6\"), pos: unsafeFEN(\"8/8/8/3pp3/4B3/5N2/8/8 w - - 0 1\")},\n\t\t{m: move(\"E4\", \"E5\"), pos: unsafeFEN(\"8/8/8/3pp3/4B3/5N2/8/8 w - - 0 1\")},\n\t\t{m: move(\"E4\", \"E4\"), pos: unsafeFEN(\"8/8/8/3pp3/4B3/5N2/8/8 w - - 0 1\")},\n\t\t{m: move(\"E4\", \"F3\"), pos: unsafeFEN(\"8/8/8/3pp3/4B3/5N2/8/8 w - - 0 1\")},\n\t\t// rook moves\n\t\t{m: move(\"B2\", \"B1\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1R6/1B6 w - - 0 1\")},\n\t\t{m: move(\"B2\", \"C3\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1R6/1B6 w - - 0 1\")},\n\t\t{m: move(\"B2\", \"B8\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1R6/1B6 w - - 0 1\")},\n\t\t{m: move(\"B2\", \"G7\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1R6/1B6 w - - 0 1\")},\n\t\t// queen moves\n\t\t{m: move(\"B2\", \"B1\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1Q6/1B6 w - - 0 1\")},\n\t\t{m: move(\"B2\", \"C4\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1Q6/1B6 w - - 0 1\")},\n\t\t{m: move(\"B2\", \"B8\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1Q6/1B6 w - - 0 1\")},\n\t\t{m: move(\"B2\", \"G7\"), pos: unsafeFEN(\"8/1p5b/4N3/4p3/8/8/1Q6/1B6 w - - 0 1\")},\n\t\t// king moves\n\t\t{m: move(\"E4\", \"F3\"), pos: unsafeFEN(\"5r2/8/8/8/4K3/8/8/8 w - - 0 1\")},\n\t\t{m: move(\"E4\", \"F4\"), pos: unsafeFEN(\"5r2/8/8/8/4K3/8/8/8 w - - 0 1\")},\n\t\t{m: move(\"E4\", \"F5\"), pos: unsafeFEN(\"5r2/8/8/8/4K3/8/8/8 w - - 0 1\")},\n\t\t// castleing\n\t\t{m: move(\"E1\", \"B1\"), pos: unsafeFEN(\"r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1\")},\n\t\t{m: move(\"E8\", \"B8\"), pos: unsafeFEN(\"r3k2r/8/8/8/8/8/8/R3K2R b KQkq - 0 1\")},\n\t\t{m: move(\"E1\", \"C1\"), pos: unsafeFEN(\"r3k2r/8/8/8/8/8/8/R2QK2R w KQkq - 0 1\")},\n\t\t{m: move(\"E1\", \"C1\"), pos: unsafeFEN(\"2r1k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1\")},\n\t\t{m: move(\"E1\", \"C1\"), pos: unsafeFEN(\"3rk2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1\")},\n\t\t{m: move(\"E1\", \"G1\"), pos: unsafeFEN(\"r3k2r/8/8/8/8/8/8/R3K2R w Qkq - 0 1\")},\n\t\t{m: move(\"E1\", \"C1\"), pos: unsafeFEN(\"r3k2r/8/8/8/8/8/8/R3K2R w Kkq - 0 1\")},\n\t\t// invalid promotion for non-pawn move\n\t\t{m: movePromo(\"B8\", \"D7\", PieceQueen), pos: unsafeFEN(\"rn1qkb1r/pp3ppp/2p1pn2/3p4/2PP4/2NQPN2/PP3PPP/R1B1K2R b KQkq - 0 7\")},\n\t\t// en passant on doubled pawn file http://en.lichess.org/TnRtrHxf#24\n\t\t{m: move(\"E3\", \"F6\"), pos: unsafeFEN(\"r1b2rk1/pp2b1pp/1qn1p3/3pPp2/1P1P4/P2BPN2/6PP/RN1Q1RK1 w - f6 0 13\")},\n\t\t// can't move piece out of pin (even if checking enemy king) http://en.lichess.org/JCRBhXH7#62\n\t\t{m: move(\"E1\", \"E7\"), pos: unsafeFEN(\"4R3/1r1k2pp/p1p5/1pP5/8/8/1PP3PP/2K1Rr2 w - - 5 32\")},\n\t\t// invalid one up pawn capture\n\t\t{m: move(\"E6\", \"E5\"), pos: unsafeFEN(`2b1r3/2k2p1B/p2np3/4B3/8/5N2/PP1K1PPP/3R4 b - - 2 1`)},\n\t\t// invalid two up pawn capture\n\t\t{m: move(\"H7\", \"H5\"), pos: unsafeFEN(`2bqkbnr/rpppp2p/2n2p2/p5pB/5P2/4P3/PPPP2PP/RNBQK1NR b KQk - 4 6`)},\n\t\t// invalid pawn move d5e4\n\t\t{m: move(\"D5\", \"E4\"), pos: unsafeFEN(`rnbqkbnr/pp2pppp/8/2pp4/3P4/4PN2/PPP2PPP/RNBQKB1R b KQkq - 0 3`)},\n\t}\n\n\tpositionUpdates = []moveTest{\n\t\t{\n\t\t\tm:       move(\"E2\", \"E4\"),\n\t\t\tpos:     unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\"),\n\t\t\tpostPos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1\"),\n\t\t},\n\t\t{\n\t\t\tm:       move(\"E1\", \"G1\"),\n\t\t\tpos:     unsafeFEN(\"r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1\"),\n\t\t\tpostPos: unsafeFEN(\"r3k2r/8/8/8/8/8/8/R4RK1 b kq - 1 1\"),\n\t\t},\n\t\t{\n\t\t\tm:       move(\"A4\", \"B3\"),\n\t\t\tpos:     unsafeFEN(\"2r3k1/1q1nbppp/r3p3/3pP3/pPpP4/P1Q2N2/2RN1PPP/2R4K b - b3 0 23\"),\n\t\t\tpostPos: unsafeFEN(\"2r3k1/1q1nbppp/r3p3/3pP3/11pP4/PpQ2N2/2RN1PPP/2R4K w - - 0 24\"),\n\t\t},\n\t\t{\n\t\t\tm:       move(\"E1\", \"G1\"),\n\t\t\tpos:     unsafeFEN(\"r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B1K2R w KQkq - 1 9\"),\n\t\t\tpostPos: unsafeFEN(\"r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 2 9\"),\n\t\t},\n\t\t// half move clock - knight move to f3 from starting position\n\t\t{\n\t\t\tm:       move(\"G1\", \"F3\"),\n\t\t\tpos:     unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\"),\n\t\t\tpostPos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/5N2/PPPPPPPP/RNBQKB1R b KQkq - 1 1\"),\n\t\t},\n\t\t// half move clock - king side castle\n\t\t{\n\t\t\tm:       move(\"E1\", \"G1\"),\n\t\t\tpos:     unsafeFEN(\"r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1\"),\n\t\t\tpostPos: unsafeFEN(\"r3k2r/8/8/8/8/8/8/R4RK1 b kq - 1 1\"),\n\t\t},\n\t\t// half move clock - queen side castle\n\t\t{\n\t\t\tm:       move(\"E1\", \"C1\"),\n\t\t\tpos:     unsafeFEN(\"r3k2r/ppqn1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P2B1PPP/R3K2R w KQkq - 3 10\"),\n\t\t\tpostPos: unsafeFEN(\"r3k2r/ppqn1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P2B1PPP/2KR3R b kq - 4 10\"),\n\t\t},\n\t\t// half move clock - pawn push\n\t\t{\n\t\t\tm:       move(\"E2\", \"E4\"),\n\t\t\tpos:     unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\"),\n\t\t\tpostPos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1\"),\n\t\t},\n\t\t// half move clock - pawn capture\n\t\t{\n\t\t\tm:       move(\"E4\", \"D5\"),\n\t\t\tpos:     unsafeFEN(\"r1bqkbnr/ppp1pppp/2n5/3p4/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3\"),\n\t\t\tpostPos: unsafeFEN(\"r1bqkbnr/ppp1pppp/2n5/3P4/8/5N2/PPPP1PPP/RNBQKB1R b KQkq - 0 3\"),\n\t\t},\n\t\t// half move clock - en passant\n\t\t{\n\t\t\tm:       move(\"E5\", \"F6\"),\n\t\t\tpos:     unsafeFEN(\"r1bqkbnr/ppp1p1pp/2n5/3pPp2/8/5N2/PPPP1PPP/RNBQKB1R w KQkq f6 0 4\"),\n\t\t\tpostPos: unsafeFEN(\"r1bqkbnr/ppp1p1pp/2n2P2/3p4/8/5N2/PPPP1PPP/RNBQKB1R b KQkq - 0 4\"),\n\t\t},\n\t\t// half move clock - piece captured by knight\n\t\t{\n\t\t\tm:       move(\"C6\", \"D4\"),\n\t\t\tpos:     unsafeFEN(\"r1bqkbnr/ppp1p1pp/2n5/3pPp2/3N4/8/PPPP1PPP/RNBQKB1R b KQkq - 1 4\"),\n\t\t\tpostPos: unsafeFEN(\"r1bqkbnr/ppp1p1pp/8/3pPp2/3n4/8/PPPP1PPP/RNBQKB1R w KQkq - 0 5\"),\n\t\t},\n\t}\n)\n\nfunc TestInvalidMoves(t *testing.T) {\n\tfor _, mt := range invalidMoves {\n\t\t_, ok := mt.pos.ValidateMove(mt.m)\n\t\tif ok {\n\t\t\tt.Errorf(\"fen %q: unexpected valid move (%s)\", mt.pos.orig, mt.m.String())\n\t\t}\n\t}\n}\n\nfunc TestPositionUpdates(t *testing.T) {\n\tfor _, mt := range positionUpdates {\n\t\tnp, ok := mt.pos.ValidateMove(mt.m)\n\t\tif !ok {\n\t\t\tt.Errorf(\"fen %q: rejected valid move (%s)\", mt.pos.orig, mt.m.String())\n\t\t\tcontinue\n\t\t}\n\t\tif np.B != mt.postPos.B {\n\t\t\tt.Errorf(\"%q: boards don't match\", mt.pos.orig)\n\t\t}\n\t\tif np.HalfMoveClock != mt.postPos.HalfMoveClock {\n\t\t\tt.Errorf(\"%q: hmc doesn't match; want %d got %d\", mt.pos.orig, mt.postPos.HalfMoveClock, np.HalfMoveClock)\n\t\t}\n\t\tif np.HalfMoveClock == 0 \u0026\u0026 len(np.Hashes) != 1 {\n\t\t\tt.Errorf(\"%q: hashes not reset\", mt.pos.orig)\n\t\t}\n\t\tif np.Flags != mt.postPos.Flags {\n\t\t\tt.Errorf(\"%q: flags don't match; want %d got %d\", mt.pos.orig, mt.postPos.Flags, np.Flags)\n\t\t}\n\t}\n}\n\nfunc TestPerft(t *testing.T) {\n\tmoves := make([]Move, 0, 10)\n\tfor n, res := range perfResults {\n\t\tt.Run(fmt.Sprintf(\"n%d\", n), func(t *testing.T) {\n\t\t\tif testing.Short() {\n\t\t\t\tt.Skip(\"skipping perft in short tests\")\n\t\t\t}\n\t\t\tres.pos.Moves = append(moves[:0], res.pos.Moves...)\n\t\t\tcounts := make([]int, len(res.nodesPerDepth))\n\t\t\tCountMoves(res.pos.Position, len(res.nodesPerDepth), counts)\n\t\t\tt.Logf(\"counts: %v\", counts)\n\t\t\tif !intsMatch(counts, res.nodesPerDepth) {\n\t\t\t\tt.Errorf(\"counts don't match: got %v want %v\", counts, res.nodesPerDepth)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc intsMatch(xx, yy []int) bool {\n\tif len(xx) != len(yy) {\n\t\treturn false\n\t}\n\tfor i := range xx {\n\t\tif xx[i] != yy[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nconst perftDebug = false\n\nfunc CountMoves(p Position, depth int, counts []int) {\n\ttotal := 0\n\tl := len(counts) - depth\n\tp.GenMoves(func(newp Position, m Move) error {\n\t\tcounts[l]++\n\t\tif depth \u003e 1 {\n\t\t\tcountMoves(newp, depth-1, counts)\n\t\t}\n\t\tdelta := counts[len(counts)-1] - total\n\t\tif perftDebug {\n\t\t\tfmt.Printf(\"%s%s: %d\\n\", m.From.String(), m.To.String(), delta)\n\t\t}\n\t\ttotal += delta\n\t\treturn nil\n\t})\n}\n\nfunc countMoves(p Position, depth int, counts []int) {\n\tl := len(counts) - depth\n\tp.GenMoves(func(newp Position, m Move) error {\n\t\tcounts[l]++\n\t\tif depth \u003e 1 {\n\t\t\tcountMoves(newp, depth-1, counts)\n\t\t}\n\t\treturn nil\n\t})\n}\n\ntype perfTest struct {\n\tpos           unsafeFENRes\n\tnodesPerDepth []int\n}\n\n/* https://www.chessprogramming.org/Perft_Results */\nvar perfResults = []perfTest{\n\t{pos: unsafeFEN(\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\"), nodesPerDepth: []int{\n\t\t20, 400, 8902, // 197281,\n\t\t// 4865609, 119060324, 3195901860, 84998978956, 2439530234167, 69352859712417\n\t}},\n\t{pos: unsafeFEN(\"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1\"), nodesPerDepth: []int{\n\t\t48, 2039, 97862,\n\t\t// 4085603, 193690690\n\t}},\n\t{pos: unsafeFEN(\"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1\"), nodesPerDepth: []int{\n\t\t14, 191, 2812, 43238, // 674624,\n\t\t// 11030083, 178633661\n\t}},\n\t{pos: unsafeFEN(\"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1\"), nodesPerDepth: []int{\n\t\t6, 264, 9467, // 422333,\n\t\t// 15833292, 706045033\n\t}},\n\t{pos: unsafeFEN(\"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1\"), nodesPerDepth: []int{\n\t\t6, 264, 9467, // 422333,\n\t\t// 15833292, 706045033\n\t}},\n\t{pos: unsafeFEN(\"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8\"), nodesPerDepth: []int{\n\t\t44, 1486, 62379,\n\t\t// 2103487, 89941194\n\t}},\n\t{pos: unsafeFEN(\"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10\"), nodesPerDepth: []int{\n\t\t46, 2079, // 89890,\n\t\t// 3894594, 164075551, 6923051137, 287188994746, 11923589843526, 490154852788714\n\t}},\n}\n\n// ---\n// testing utility functions\n\n// FEN decoding: see https://www.chessprogramming.org/Forsyth-Edwards_Notation\n// copied mostly from notnil/chess and adapted to our own system.\n\ntype unsafeFENRes struct {\n\tPosition\n\torig string\n}\n\nfunc unsafeFEN(fen string) unsafeFENRes {\n\tp, e := decodeFEN(fen)\n\tif e != nil {\n\t\tpanic(e)\n\t}\n\treturn unsafeFENRes{p, fen}\n}\n\n// Decodes FEN into Board and previous moves.\nfunc decodeFEN(fen string) (p Position, err error) {\n\tfen = strings.TrimSpace(fen)\n\tparts := strings.Split(fen, \" \")\n\tif len(parts) != 6 {\n\t\terr = fmt.Errorf(\"chess: fen invalid notation %s must have 6 sections\", fen)\n\t\treturn\n\t}\n\n\tp = NewPosition()\n\n\t// fen board\n\tvar ok bool\n\tp.B, ok = fenBoard(parts[0])\n\tif !ok {\n\t\terr = fmt.Errorf(\"chess: invalid fen board %s\", parts[0])\n\t\treturn\n\t}\n\n\t// do castling rights first (more convenient to set prev)\n\tif parts[2] != \"KQkq\" {\n\t\tp.Flags = castleRightsToPositionFlags(parts[2])\n\t}\n\n\t// color to play\n\tcolor := Color(parts[1] == \"b\")\n\tif color == Black {\n\t\t// add fake move to make len(prev) odd\n\t\tp.Moves = append(p.Moves, Move{})\n\t}\n\n\t// en passant\n\tif parts[3] != \"-\" {\n\t\tf, e := parseEnPassant(parts[3])\n\t\tif e != nil {\n\t\t\terr = e\n\t\t\treturn\n\t\t}\n\t\tp.Flags |= f\n\t}\n\n\thalfMove, _ := strconv.Atoi(parts[4])\n\tp.HalfMoveClock = uint16(halfMove)\n\n\t// parts[5]: full move counter, probably never implementing\n\n\treturn\n}\n\n// generates board from fen format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR\nfunc fenBoard(boardStr string) (Board, bool) {\n\trankStrs := strings.Split(boardStr, \"/\")\n\tif len(rankStrs) != 8 {\n\t\treturn Board{}, false\n\t}\n\tvar b Board\n\tfor idx, pieces := range rankStrs {\n\t\trank := (7 - Square(idx)) \u003c\u003c 3\n\t\tfile := Square(0)\n\t\tfor _, ch := range pieces {\n\t\t\tif ch \u003e= '1' \u0026\u0026 ch \u003c= '8' {\n\t\t\t\tdelta := byte(ch) - '0'\n\t\t\t\tfile += Square(delta)\n\t\t\t\tif file \u003e 8 {\n\t\t\t\t\treturn b, false\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tpiece := p[byte(ch)]\n\t\t\tif piece == PieceEmpty || file \u003e= 8 {\n\t\t\t\treturn b, false\n\t\t\t}\n\t\t\tb[rank|file] = piece\n\t\t\tfile++\n\t\t}\n\t\tif file != 8 {\n\t\t\treturn b, false\n\t\t}\n\t}\n\treturn b, true\n}\n\nfunc castleRightsToPositionFlags(cr string) (pf PositionFlags) {\n\tpf = NoCastleWQ | NoCastleWK | NoCastleBQ | NoCastleBK\n\tif cr == \"-\" {\n\t\treturn\n\t}\n\tfor _, ch := range cr {\n\t\tswitch ch {\n\t\tcase 'K':\n\t\t\tpf \u0026^= NoCastleWK\n\t\tcase 'Q':\n\t\t\tpf \u0026^= NoCastleWQ\n\t\tcase 'k':\n\t\t\tpf \u0026^= NoCastleBK\n\t\tcase 'q':\n\t\t\tpf \u0026^= NoCastleBQ\n\t\t}\n\t}\n\treturn\n}\n\nfunc parseEnPassant(strpos string) (PositionFlags, error) {\n\teppos := SquareFromString(strpos)\n\tif eppos == SquareInvalid {\n\t\treturn 0, fmt.Errorf(\"invalid pos: %s\", eppos)\n\t}\n\trow, col := eppos.Split()\n\tif row != 5 \u0026\u0026 row != 2 {\n\t\treturn 0, fmt.Errorf(\"invalid en passant pos: %s\", eppos)\n\t}\n\treturn EnPassant | PositionFlags(col), nil\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/morgan/chess\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"NEftqprifdUXCUhjlQOVulQmXBG8KUes/SSFKSXC5Joj9hYm5CXO4Zhcf35AgLGf2W/ZWL3vWrbGB2I0E0vcBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"glicko2","path":"gno.land/p/morgan/chess/glicko2","files":[{"name":"glicko2.gno","body":"package glicko2\n\nimport (\n\t\"math\"\n)\n\n// http://www.glicko.net/glicko/glicko2.pdf\n\nconst (\n\t// Step 1.\n\t// Determine a rating and RD for each player at the onset of the rating period. The\n\t// system constant, τ , which constrains the change in volatility over time, needs to be\n\t// set prior to application of the system. Reasonable choices are between 0.3 and 1.2,\n\t// though the system should be tested to decide which value results in greatest predictive\n\t// accuracy. Smaller values of τ prevent the volatility measures from changing by large\n\t// amounts, which in turn prevent enormous changes in ratings based on very improbable\n\t// results. If the application of Glicko-2 is expected to involve extremely improbable\n\t// collections of game outcomes, then τ should be set to a small value, even as small as,\n\t// say, τ = 0.2.\n\tGlickoTau = 0.5\n\n\tGlickoInitialRating     = 1500\n\tGlickoInitialRD         = 350\n\tGlickoInitialVolatility = 0.06\n\n\tglickoScaleFactor = 173.7178\n)\n\ntype PlayerRating struct {\n\tID address\n\n\tRating           float64\n\tRatingDeviation  float64\n\tRatingVolatility float64\n\n\t// working values, these are referred to as μ, φ and σ in the paper.\n\twr, wrd, wrv float64\n}\n\nfunc NewPlayerRating(addr address) *PlayerRating {\n\treturn \u0026PlayerRating{\n\t\tID:               addr,\n\t\tRating:           GlickoInitialRating,\n\t\tRatingDeviation:  GlickoInitialRD,\n\t\tRatingVolatility: GlickoInitialVolatility,\n\t}\n}\n\n// RatingScore is the outcome of a game between two players.\ntype RatingScore struct {\n\tWhite, Black address\n\tScore        float64 // 0 = black win, 0.5 = draw, 1 = white win\n}\n\nfunc getRatingScore(scores []RatingScore, player, opponent address) float64 {\n\tfor _, score := range scores {\n\t\tif score.White == player \u0026\u0026 score.Black == opponent {\n\t\t\treturn score.Score\n\t\t}\n\t\tif score.Black == player \u0026\u0026 score.White == opponent {\n\t\t\treturn 1 - score.Score\n\t\t}\n\t}\n\treturn -1\n}\n\nfunc UpdateRatings(source []*PlayerRating, scores []RatingScore) {\n\t// step 2: assign working wr/wrd/wrv\n\tfor _, player := range source {\n\t\tplayer.wr = (player.Rating - GlickoInitialRating) / glickoScaleFactor\n\t\tplayer.wrd = player.RatingDeviation / glickoScaleFactor\n\t\tplayer.wrv = player.RatingVolatility\n\t}\n\n\tfor _, player := range source {\n\t\t// step 3: compute the quantity v. This is the estimated variance of the team’s/player’s\n\t\t// rating based only on game outcomes\n\t\t// step 4: compute the quantity ∆, the estimated improvement in rating\n\t\tv, delta, competed := glickoVDelta(player, source, scores)\n\n\t\tnewRDPart := math.Sqrt(player.wrd*player.wrd + player.wrv*player.wrv)\n\t\tif !competed {\n\t\t\t// fast path for players who have not competed:\n\t\t\t// update only rating deviation\n\t\t\tplayer.RatingDeviation = newRDPart\n\t\t\tplayer.wr, player.wrd, player.wrv = 0, 0, 0\n\t\t\tcontinue\n\t\t}\n\n\t\t// step 5: determine the new value, σ′, of the volatility.\n\t\tplayer.wrv = glickoVolatility(player, delta, v)\n\t\t// step 6 and 7\n\t\tplayer.wrd = 1 / math.Sqrt(1/v+1/(newRDPart*newRDPart))\n\t\tplayer.wr = player.wr + player.wrd*player.wrd*(delta/v)\n\n\t\t// step 8\n\t\tplayer.Rating = player.wr*glickoScaleFactor + 1500\n\t\tplayer.RatingDeviation = player.wrd * glickoScaleFactor\n\t\tplayer.RatingVolatility = player.wrv\n\t\tplayer.wrv, player.wrd, player.wr = 0, 0, 0\n\t}\n}\n\nfunc glickoVDelta(player *PlayerRating, source []*PlayerRating, scores []RatingScore) (v, delta float64, competed bool) {\n\tvar deltaPartial float64\n\tfor _, opponent := range source {\n\t\tif opponent.ID == player.ID {\n\t\t\tcontinue\n\t\t}\n\t\tscore := getRatingScore(scores, player.ID, opponent.ID)\n\t\tif score == -1 { // not found\n\t\t\tcontinue\n\t\t}\n\t\tcompeted = true\n\n\t\t// Step 3\n\t\tgTheta := 1 / math.Sqrt(1+(3*opponent.wrd*opponent.wrd)/(math.Pi*math.Pi))\n\t\teMu := 1 / (1 + math.Exp(-gTheta*(player.wr-opponent.wr)))\n\t\tv += gTheta * gTheta * eMu * (1 - eMu)\n\n\t\t// step 4\n\t\tdeltaPartial += gTheta * (score - eMu)\n\t}\n\tv = 1 / v\n\treturn v, v * deltaPartial, competed\n}\n\nconst glickoVolatilityEps = 0.000001\n\n// Step 5\nfunc glickoVolatility(player *PlayerRating, delta, v float64) float64 {\n\taInit := math.Log(player.wrv * player.wrv)\n\tA := aInit\n\tvar B float64\n\trdp2 := player.wrd * player.wrd\n\tif delta*delta \u003e rdp2+v {\n\t\tB = math.Log(delta*delta - rdp2 - v)\n\t} else {\n\t\tk := float64(1)\n\t\tfor ; glickoVolatilityF(A-k*GlickoTau, aInit, rdp2, delta, v) \u003c 0; k++ {\n\t\t}\n\t\tB = A - k*GlickoTau\n\t}\n\tfA, fB := glickoVolatilityF(A, aInit, rdp2, delta, v), glickoVolatilityF(B, aInit, rdp2, delta, v)\n\tfor math.Abs(B-A) \u003e glickoVolatilityEps {\n\t\tC := A + ((A-B)*fA)/(fB-fA)\n\t\tfC := glickoVolatilityF(C, aInit, rdp2, delta, v)\n\t\tif fC*fB \u003c= 0 {\n\t\t\tA, fA = B, fB\n\t\t} else {\n\t\t\tfA = fA / 2\n\t\t}\n\t\tB, fB = C, fC\n\t}\n\treturn math.Exp(A / 2)\n}\n\n// rdp2: player.rd, power 2\nfunc glickoVolatilityF(x, a, rdp2, delta, v float64) float64 {\n\texpX := math.Exp(x)\n\treturn (expX*(delta*delta-rdp2-v-expX))/(2*pow2(rdp2+v+expX)) -\n\t\t(x-a)/(GlickoTau*GlickoTau)\n}\n\nfunc pow2(f float64) float64 { return f * f }\n"},{"name":"glicko2_test.gno","body":"package glicko2\n\nimport (\n\t\"testing\"\n)\n\nfunc TestExampleCalculations(t *testing.T) {\n\t// These are from the example in prof. Glickman's paper.\n\t// At the end, t.Log should print for the first player updated values\n\t// for Rating, RatingDeviation and RatingVolatility matching those in the\n\t// examples.\n\tratings := []*PlayerRating{\n\t\t{ID: \"1\", Rating: 1500, RatingDeviation: 200, RatingVolatility: 0.06},\n\t\t{ID: \"2\", Rating: 1400, RatingDeviation: 30, RatingVolatility: 0.06},\n\t\t{ID: \"3\", Rating: 1550, RatingDeviation: 100, RatingVolatility: 0.06},\n\t\t{ID: \"4\", Rating: 1700, RatingDeviation: 300, RatingVolatility: 0.06},\n\t}\n\tscores := []RatingScore{\n\t\t{White: \"1\", Black: \"2\", Score: 1},\n\t\t{White: \"1\", Black: \"3\", Score: 0},\n\t\t{White: \"1\", Black: \"4\", Score: 0},\n\t}\n\tUpdateRatings(ratings, scores)\n\tr := ratings[0]\n\tt.Logf(\"%.4f (± %.4f, volatility: %.4f); working values: %.2f / %.2f / %.2f\\n\",\n\t\tr.Rating, r.RatingDeviation, r.RatingVolatility,\n\t\tr.wr, r.wrd, r.wrv,\n\t)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/morgan/chess/glicko2\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Tl1F+X3EOodLaeN/j+exhUpyFoO/csNY+UyKMrZxulrOoYdccT766gw5XjdGHXfFx0I+4a8rTDvCgKrE9zYIBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"cow","path":"gno.land/p/moul/cow","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/cow\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"node.gno","body":"package cow\n\n//----------------------------------------\n// Node\n\n// Node represents a node in an AVL tree.\ntype Node struct {\n\tkey       string // key is the unique identifier for the node.\n\tvalue     any    // value is the data stored in the node.\n\theight    int8   // height is the height of the node in the tree.\n\tsize      int    // size is the number of nodes in the subtree rooted at this node.\n\tleftNode  *Node  // leftNode is the left child of the node.\n\trightNode *Node  // rightNode is the right child of the node.\n}\n\n// NewNode creates a new node with the given key and value.\nfunc NewNode(key string, value any) *Node {\n\treturn \u0026Node{\n\t\tkey:    key,\n\t\tvalue:  value,\n\t\theight: 0,\n\t\tsize:   1,\n\t}\n}\n\n// Size returns the size of the subtree rooted at the node.\nfunc (node *Node) Size() int {\n\tif node == nil {\n\t\treturn 0\n\t}\n\treturn node.size\n}\n\n// IsLeaf checks if the node is a leaf node (has no children).\nfunc (node *Node) IsLeaf() bool {\n\treturn node.height == 0\n}\n\n// Key returns the key of the node.\nfunc (node *Node) Key() string {\n\treturn node.key\n}\n\n// Value returns the value of the node.\nfunc (node *Node) Value() any {\n\treturn node.value\n}\n\n// _copy creates a copy of the node (excluding value).\nfunc (node *Node) _copy() *Node {\n\tif node.height == 0 {\n\t\tpanic(\"Why are you copying a value node?\")\n\t}\n\treturn \u0026Node{\n\t\tkey:       node.key,\n\t\theight:    node.height,\n\t\tsize:      node.size,\n\t\tleftNode:  node.leftNode,\n\t\trightNode: node.rightNode,\n\t}\n}\n\n// Has checks if a node with the given key exists in the subtree rooted at the node.\nfunc (node *Node) Has(key string) (has bool) {\n\tif node == nil {\n\t\treturn false\n\t}\n\tif node.key == key {\n\t\treturn true\n\t}\n\tif node.height == 0 {\n\t\treturn false\n\t}\n\tif key \u003c node.key {\n\t\treturn node.getLeftNode().Has(key)\n\t}\n\treturn node.getRightNode().Has(key)\n}\n\n// Get searches for a node with the given key in the subtree rooted at the node\n// and returns its index, value, and whether it exists.\nfunc (node *Node) Get(key string) (index int, value any, exists bool) {\n\tif node == nil {\n\t\treturn 0, nil, false\n\t}\n\n\tif node.height == 0 {\n\t\tif node.key == key {\n\t\t\treturn 0, node.value, true\n\t\t}\n\t\tif node.key \u003c key {\n\t\t\treturn 1, nil, false\n\t\t}\n\t\treturn 0, nil, false\n\t}\n\n\tif key \u003c node.key {\n\t\treturn node.getLeftNode().Get(key)\n\t}\n\n\trightNode := node.getRightNode()\n\tindex, value, exists = rightNode.Get(key)\n\tindex += node.size - rightNode.size\n\treturn index, value, exists\n}\n\n// GetByIndex retrieves the key-value pair of the node at the given index\n// in the subtree rooted at the node.\nfunc (node *Node) GetByIndex(index int) (key string, value any) {\n\tif node.height == 0 {\n\t\tif index == 0 {\n\t\t\treturn node.key, node.value\n\t\t}\n\t\tpanic(\"GetByIndex asked for invalid index\")\n\t}\n\t// TODO: could improve this by storing the sizes\n\tleftNode := node.getLeftNode()\n\tif index \u003c leftNode.size {\n\t\treturn leftNode.GetByIndex(index)\n\t}\n\treturn node.getRightNode().GetByIndex(index - leftNode.size)\n}\n\n// Set inserts a new node with the given key-value pair into the subtree rooted at the node,\n// and returns the new root of the subtree and whether an existing node was updated.\n//\n// XXX consider a better way to do this... perhaps split Node from Node.\nfunc (node *Node) Set(key string, value any) (newSelf *Node, updated bool) {\n\tif node == nil {\n\t\treturn NewNode(key, value), false\n\t}\n\n\t// Always create a new node for leaf nodes\n\tif node.height == 0 {\n\t\treturn node.setLeaf(key, value)\n\t}\n\n\t// Copy the node before modifying\n\tnewNode := node._copy()\n\tif key \u003c node.key {\n\t\tnewNode.leftNode, updated = node.getLeftNode().Set(key, value)\n\t} else {\n\t\tnewNode.rightNode, updated = node.getRightNode().Set(key, value)\n\t}\n\n\tif !updated {\n\t\tnewNode.calcHeightAndSize()\n\t\treturn newNode.balance(), updated\n\t}\n\n\treturn newNode, updated\n}\n\n// setLeaf inserts a new leaf node with the given key-value pair into the subtree rooted at the node,\n// and returns the new root of the subtree and whether an existing node was updated.\nfunc (node *Node) setLeaf(key string, value any) (newSelf *Node, updated bool) {\n\tif key == node.key {\n\t\treturn NewNode(key, value), true\n\t}\n\n\tif key \u003c node.key {\n\t\treturn \u0026Node{\n\t\t\tkey:       node.key,\n\t\t\theight:    1,\n\t\t\tsize:      2,\n\t\t\tleftNode:  NewNode(key, value),\n\t\t\trightNode: node,\n\t\t}, false\n\t}\n\n\treturn \u0026Node{\n\t\tkey:       key,\n\t\theight:    1,\n\t\tsize:      2,\n\t\tleftNode:  node,\n\t\trightNode: NewNode(key, value),\n\t}, false\n}\n\n// Remove deletes the node with the given key from the subtree rooted at the node.\n// returns the new root of the subtree, the new leftmost leaf key (if changed),\n// the removed value and the removal was successful.\nfunc (node *Node) Remove(key string) (\n\tnewNode *Node, newKey string, value any, removed bool,\n) {\n\tif node == nil {\n\t\treturn nil, \"\", nil, false\n\t}\n\tif node.height == 0 {\n\t\tif key == node.key {\n\t\t\treturn nil, \"\", node.value, true\n\t\t}\n\t\treturn node, \"\", nil, false\n\t}\n\tif key \u003c node.key {\n\t\tvar newLeftNode *Node\n\t\tnewLeftNode, newKey, value, removed = node.getLeftNode().Remove(key)\n\t\tif !removed {\n\t\t\treturn node, \"\", value, false\n\t\t}\n\t\tif newLeftNode == nil { // left node held value, was removed\n\t\t\treturn node.rightNode, node.key, value, true\n\t\t}\n\t\tnode = node._copy()\n\t\tnode.leftNode = newLeftNode\n\t\tnode.calcHeightAndSize()\n\t\tnode = node.balance()\n\t\treturn node, newKey, value, true\n\t}\n\n\tvar newRightNode *Node\n\tnewRightNode, newKey, value, removed = node.getRightNode().Remove(key)\n\tif !removed {\n\t\treturn node, \"\", value, false\n\t}\n\tif newRightNode == nil { // right node held value, was removed\n\t\treturn node.leftNode, \"\", value, true\n\t}\n\tnode = node._copy()\n\tnode.rightNode = newRightNode\n\tif newKey != \"\" {\n\t\tnode.key = newKey\n\t}\n\tnode.calcHeightAndSize()\n\tnode = node.balance()\n\treturn node, \"\", value, true\n}\n\n// getLeftNode returns the left child of the node.\nfunc (node *Node) getLeftNode() *Node {\n\treturn node.leftNode\n}\n\n// getRightNode returns the right child of the node.\nfunc (node *Node) getRightNode() *Node {\n\treturn node.rightNode\n}\n\n// rotateRight performs a right rotation on the node and returns the new root.\n// NOTE: overwrites node\n// TODO: optimize balance \u0026 rotate\nfunc (node *Node) rotateRight() *Node {\n\tnode = node._copy()\n\tl := node.getLeftNode()\n\t_l := l._copy()\n\n\t_lrCached := _l.rightNode\n\t_l.rightNode = node\n\tnode.leftNode = _lrCached\n\n\tnode.calcHeightAndSize()\n\t_l.calcHeightAndSize()\n\n\treturn _l\n}\n\n// rotateLeft performs a left rotation on the node and returns the new root.\n// NOTE: overwrites node\n// TODO: optimize balance \u0026 rotate\nfunc (node *Node) rotateLeft() *Node {\n\tnode = node._copy()\n\tr := node.getRightNode()\n\t_r := r._copy()\n\n\t_rlCached := _r.leftNode\n\t_r.leftNode = node\n\tnode.rightNode = _rlCached\n\n\tnode.calcHeightAndSize()\n\t_r.calcHeightAndSize()\n\n\treturn _r\n}\n\n// calcHeightAndSize updates the height and size of the node based on its children.\n// NOTE: mutates height and size\nfunc (node *Node) calcHeightAndSize() {\n\tnode.height = maxInt8(node.getLeftNode().height, node.getRightNode().height) + 1\n\tnode.size = node.getLeftNode().size + node.getRightNode().size\n}\n\n// calcBalance calculates the balance factor of the node.\nfunc (node *Node) calcBalance() int {\n\treturn int(node.getLeftNode().height) - int(node.getRightNode().height)\n}\n\n// balance balances the subtree rooted at the node and returns the new root.\n// NOTE: assumes that node can be modified\n// TODO: optimize balance \u0026 rotate\nfunc (node *Node) balance() (newSelf *Node) {\n\tbalance := node.calcBalance()\n\tif balance \u003e= -1 {\n\t\treturn node\n\t}\n\tif balance \u003e 1 {\n\t\tif node.getLeftNode().calcBalance() \u003e= 0 {\n\t\t\t// Left Left Case\n\t\t\treturn node.rotateRight()\n\t\t}\n\t\t// Left Right Case\n\t\tleft := node.getLeftNode()\n\t\tnode.leftNode = left.rotateLeft()\n\t\treturn node.rotateRight()\n\t}\n\n\tif node.getRightNode().calcBalance() \u003c= 0 {\n\t\t// Right Right Case\n\t\treturn node.rotateLeft()\n\t}\n\n\t// Right Left Case\n\tright := node.getRightNode()\n\tnode.rightNode = right.rotateRight()\n\treturn node.rotateLeft()\n}\n\n// Shortcut for TraverseInRange.\nfunc (node *Node) Iterate(start, end string, cb func(*Node) bool) bool {\n\treturn node.TraverseInRange(start, end, true, true, cb)\n}\n\n// Shortcut for TraverseInRange.\nfunc (node *Node) ReverseIterate(start, end string, cb func(*Node) bool) bool {\n\treturn node.TraverseInRange(start, end, false, true, cb)\n}\n\n// TraverseInRange traverses all nodes, including inner nodes.\n// Start is inclusive and end is exclusive when ascending,\n// Start and end are inclusive when descending.\n// Empty start and empty end denote no start and no end.\n// If leavesOnly is true, only visit leaf nodes.\n// NOTE: To simulate an exclusive reverse traversal,\n// just append 0x00 to start.\nfunc (node *Node) TraverseInRange(start, end string, ascending bool, leavesOnly bool, cb func(*Node) bool) bool {\n\tif node == nil {\n\t\treturn false\n\t}\n\tafterStart := (start == \"\" || start \u003c node.key)\n\tstartOrAfter := (start == \"\" || start \u003c= node.key)\n\tbeforeEnd := false\n\tif ascending {\n\t\tbeforeEnd = (end == \"\" || node.key \u003c end)\n\t} else {\n\t\tbeforeEnd = (end == \"\" || node.key \u003c= end)\n\t}\n\n\t// Run callback per inner/leaf node.\n\tstop := false\n\tif (!node.IsLeaf() \u0026\u0026 !leavesOnly) ||\n\t\t(node.IsLeaf() \u0026\u0026 startOrAfter \u0026\u0026 beforeEnd) {\n\t\tstop = cb(node)\n\t\tif stop {\n\t\t\treturn stop\n\t\t}\n\t}\n\tif node.IsLeaf() {\n\t\treturn stop\n\t}\n\n\tif ascending {\n\t\t// check lower nodes, then higher\n\t\tif afterStart {\n\t\t\tstop = node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t\tif stop {\n\t\t\treturn stop\n\t\t}\n\t\tif beforeEnd {\n\t\t\tstop = node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t} else {\n\t\t// check the higher nodes first\n\t\tif beforeEnd {\n\t\t\tstop = node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t\tif stop {\n\t\t\treturn stop\n\t\t}\n\t\tif afterStart {\n\t\t\tstop = node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb)\n\t\t}\n\t}\n\n\treturn stop\n}\n\n// TraverseByOffset traverses all nodes, including inner nodes.\n// A limit of math.MaxInt means no limit.\nfunc (node *Node) TraverseByOffset(offset, limit int, descending bool, leavesOnly bool, cb func(*Node) bool) bool {\n\tif node == nil {\n\t\treturn false\n\t}\n\n\t// fast paths. these happen only if TraverseByOffset is called directly on a leaf.\n\tif limit \u003c= 0 || offset \u003e= node.size {\n\t\treturn false\n\t}\n\tif node.IsLeaf() {\n\t\tif offset \u003e 0 {\n\t\t\treturn false\n\t\t}\n\t\treturn cb(node)\n\t}\n\n\t// go to the actual recursive function.\n\treturn node.traverseByOffset(offset, limit, descending, leavesOnly, cb)\n}\n\n// TraverseByOffset traverses the subtree rooted at the node by offset and limit,\n// in either ascending or descending order, and applies the callback function to each traversed node.\n// If leavesOnly is true, only leaf nodes are visited.\nfunc (node *Node) traverseByOffset(offset, limit int, descending bool, leavesOnly bool, cb func(*Node) bool) bool {\n\t// caller guarantees: offset \u003c node.size; limit \u003e 0.\n\tif !leavesOnly {\n\t\tif cb(node) {\n\t\t\treturn true\n\t\t}\n\t}\n\tfirst, second := node.getLeftNode(), node.getRightNode()\n\tif descending {\n\t\tfirst, second = second, first\n\t}\n\tif first.IsLeaf() {\n\t\t// either run or skip, based on offset\n\t\tif offset \u003e 0 {\n\t\t\toffset--\n\t\t} else {\n\t\t\tcb(first)\n\t\t\tlimit--\n\t\t\tif limit \u003c= 0 {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t} else {\n\t\t// possible cases:\n\t\t// 1 the offset given skips the first node entirely\n\t\t// 2 the offset skips none or part of the first node, but the limit requires some of the second node.\n\t\t// 3 the offset skips none or part of the first node, and the limit stops our search on the first node.\n\t\tif offset \u003e= first.size {\n\t\t\toffset -= first.size // 1\n\t\t} else {\n\t\t\tif first.traverseByOffset(offset, limit, descending, leavesOnly, cb) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\t// number of leaves which could actually be called from inside\n\t\t\tdelta := first.size - offset\n\t\t\toffset = 0\n\t\t\tif delta \u003e= limit {\n\t\t\t\treturn true // 3\n\t\t\t}\n\t\t\tlimit -= delta // 2\n\t\t}\n\t}\n\n\t// because of the caller guarantees and the way we handle the first node,\n\t// at this point we know that limit \u003e 0 and there must be some values in\n\t// this second node that we include.\n\n\t// =\u003e if the second node is a leaf, it has to be included.\n\tif second.IsLeaf() {\n\t\treturn cb(second)\n\t}\n\t// =\u003e if it is not a leaf, it will still be enough to recursively call this\n\t// function with the updated offset and limit\n\treturn second.traverseByOffset(offset, limit, descending, leavesOnly, cb)\n}\n\n// Only used in testing...\nfunc (node *Node) lmd() *Node {\n\tif node.height == 0 {\n\t\treturn node\n\t}\n\treturn node.getLeftNode().lmd()\n}\n\n// Only used in testing...\nfunc (node *Node) rmd() *Node {\n\tif node.height == 0 {\n\t\treturn node\n\t}\n\treturn node.getRightNode().rmd()\n}\n\nfunc maxInt8(a, b int8) int8 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\n// Equal compares two nodes for structural equality.\n// WARNING: This is an expensive operation that recursively traverses the entire tree structure.\n// It should only be used in tests or when absolutely necessary.\nfunc (node *Node) Equal(other *Node) bool {\n\t// Handle nil cases\n\tif node == nil || other == nil {\n\t\treturn node == other\n\t}\n\n\t// Compare node properties\n\tif node.key != other.key ||\n\t\tnode.value != other.value ||\n\t\tnode.height != other.height ||\n\t\tnode.size != other.size {\n\t\treturn false\n\t}\n\n\t// Compare children\n\tleftEqual := (node.leftNode == nil \u0026\u0026 other.leftNode == nil) ||\n\t\t(node.leftNode != nil \u0026\u0026 other.leftNode != nil \u0026\u0026 node.leftNode.Equal(other.leftNode))\n\tif !leftEqual {\n\t\treturn false\n\t}\n\n\trightEqual := (node.rightNode == nil \u0026\u0026 other.rightNode == nil) ||\n\t\t(node.rightNode != nil \u0026\u0026 other.rightNode != nil \u0026\u0026 node.rightNode.Equal(other.rightNode))\n\treturn rightEqual\n}\n"},{"name":"node_test.gno","body":"package cow\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestTraverseByOffset(t *testing.T) {\n\tconst testStrings = `Alfa\nAlfred\nAlpha\nAlphabet\nBeta\nBeth\nBook\nBrowser`\n\ttt := []struct {\n\t\tname string\n\t\tdesc bool\n\t}{\n\t\t{\"ascending\", false},\n\t\t{\"descending\", true},\n\t}\n\n\tfor _, tt := range tt {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tsl := strings.Split(testStrings, \"\\n\")\n\n\t\t\t// sort a first time in the order opposite to how we'll be traversing\n\t\t\t// the tree, to ensure that we are not just iterating through with\n\t\t\t// insertion order.\n\t\t\tsort.Strings(sl)\n\t\t\tif !tt.desc {\n\t\t\t\treverseSlice(sl)\n\t\t\t}\n\n\t\t\tr := NewNode(sl[0], nil)\n\t\t\tfor _, v := range sl[1:] {\n\t\t\t\tr, _ = r.Set(v, nil)\n\t\t\t}\n\n\t\t\t// then sort sl in the order we'll be traversing it, so that we can\n\t\t\t// compare the result with sl.\n\t\t\treverseSlice(sl)\n\n\t\t\tvar result []string\n\t\t\tfor i := 0; i \u003c len(sl); i++ {\n\t\t\t\tr.TraverseByOffset(i, 1, tt.desc, true, func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tif !slicesEqual(sl, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", sl, result)\n\t\t\t}\n\n\t\t\tfor l := 2; l \u003c= len(sl); l++ {\n\t\t\t\t// \"slices\"\n\t\t\t\tfor i := 0; i \u003c= len(sl); i++ {\n\t\t\t\t\tmax := i + l\n\t\t\t\t\tif max \u003e len(sl) {\n\t\t\t\t\t\tmax = len(sl)\n\t\t\t\t\t}\n\t\t\t\t\texp := sl[i:max]\n\t\t\t\t\tactual := []string{}\n\n\t\t\t\t\tr.TraverseByOffset(i, l, tt.desc, true, func(tr *Node) bool {\n\t\t\t\t\t\tactual = append(actual, tr.Key())\n\t\t\t\t\t\treturn false\n\t\t\t\t\t})\n\t\t\t\t\tif !slicesEqual(exp, actual) {\n\t\t\t\t\t\tt.Errorf(\"want %v got %v\", exp, actual)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestHas(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\thasKey   string\n\t\texpected bool\n\t}{\n\t\t{\n\t\t\t\"has key in non-empty tree\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"B\",\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"does not have key in non-empty tree\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"F\",\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"has key in single-node tree\",\n\t\t\t[]string{\"A\"},\n\t\t\t\"A\",\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"does not have key in single-node tree\",\n\t\t\t[]string{\"A\"},\n\t\t\t\"B\",\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"does not have key in empty tree\",\n\t\t\t[]string{},\n\t\t\t\"A\",\n\t\t\tfalse,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tresult := tree.Has(tt.hasKey)\n\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"Expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGet(t *testing.T) {\n\ttests := []struct {\n\t\tname         string\n\t\tinput        []string\n\t\tgetKey       string\n\t\texpectIdx    int\n\t\texpectVal    any\n\t\texpectExists bool\n\t}{\n\t\t{\n\t\t\t\"get existing key\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"B\",\n\t\t\t1,\n\t\t\tnil,\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"get non-existent key (smaller)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"@\",\n\t\t\t0,\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get non-existent key (larger)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"F\",\n\t\t\t5,\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get from empty tree\",\n\t\t\t[]string{},\n\t\t\t\"A\",\n\t\t\t0,\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tidx, val, exists := tree.Get(tt.getKey)\n\n\t\t\tif idx != tt.expectIdx {\n\t\t\t\tt.Errorf(\"Expected index %d, got %d\", tt.expectIdx, idx)\n\t\t\t}\n\n\t\t\tif val != tt.expectVal {\n\t\t\t\tt.Errorf(\"Expected value %v, got %v\", tt.expectVal, val)\n\t\t\t}\n\n\t\t\tif exists != tt.expectExists {\n\t\t\t\tt.Errorf(\"Expected exists %t, got %t\", tt.expectExists, exists)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetByIndex(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tinput       []string\n\t\tidx         int\n\t\texpectKey   string\n\t\texpectVal   any\n\t\texpectPanic bool\n\t}{\n\t\t{\n\t\t\t\"get by valid index\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t2,\n\t\t\t\"C\",\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get by valid index (smallest)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t0,\n\t\t\t\"A\",\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get by valid index (largest)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t4,\n\t\t\t\"E\",\n\t\t\tnil,\n\t\t\tfalse,\n\t\t},\n\t\t{\n\t\t\t\"get by invalid index (negative)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t-1,\n\t\t\t\"\",\n\t\t\tnil,\n\t\t\ttrue,\n\t\t},\n\t\t{\n\t\t\t\"get by invalid index (out of range)\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t5,\n\t\t\t\"\",\n\t\t\tnil,\n\t\t\ttrue,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tif tt.expectPanic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r == nil {\n\t\t\t\t\t\tt.Errorf(\"Expected a panic but didn't get one\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\n\t\t\tkey, val := tree.GetByIndex(tt.idx)\n\n\t\t\tif !tt.expectPanic {\n\t\t\t\tif key != tt.expectKey {\n\t\t\t\t\tt.Errorf(\"Expected key %s, got %s\", tt.expectKey, key)\n\t\t\t\t}\n\n\t\t\t\tif val != tt.expectVal {\n\t\t\t\t\tt.Errorf(\"Expected value %v, got %v\", tt.expectVal, val)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRemove(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tinput     []string\n\t\tremoveKey string\n\t\texpected  []string\n\t}{\n\t\t{\n\t\t\t\"remove leaf node\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"D\"},\n\t\t\t\"B\",\n\t\t\t[]string{\"A\", \"C\", \"D\"},\n\t\t},\n\t\t{\n\t\t\t\"remove node with one child\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"D\"},\n\t\t\t\"A\",\n\t\t\t[]string{\"B\", \"C\", \"D\"},\n\t\t},\n\t\t{\n\t\t\t\"remove node with two children\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"C\",\n\t\t\t[]string{\"A\", \"B\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"remove root node\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"C\",\n\t\t\t[]string{\"A\", \"B\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"remove non-existent key\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t\"F\",\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\ttree, _, _, _ = tree.Remove(tt.removeKey)\n\n\t\t\tresult := make([]string, 0)\n\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\tresult = append(result, n.Key())\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestTraverse(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\t\"empty tree\",\n\t\t\t[]string{},\n\t\t\t[]string{},\n\t\t},\n\t\t{\n\t\t\t\"single node tree\",\n\t\t\t[]string{\"A\"},\n\t\t\t[]string{\"A\"},\n\t\t},\n\t\t{\n\t\t\t\"small tree\",\n\t\t\t[]string{\"C\", \"A\", \"B\", \"E\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"large tree\",\n\t\t\t[]string{\"H\", \"D\", \"L\", \"B\", \"F\", \"J\", \"N\", \"A\", \"C\", \"E\", \"G\", \"I\", \"K\", \"M\", \"O\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\tt.Run(\"iterate\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t\t}\n\t\t\t})\n\n\t\t\tt.Run(\"ReverseIterate\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\ttree.ReverseIterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\texpected := make([]string, len(tt.expected))\n\t\t\t\tcopy(expected, tt.expected)\n\t\t\t\tfor i, j := 0, len(expected)-1; i \u003c j; i, j = i+1, j-1 {\n\t\t\t\t\texpected[i], expected[j] = expected[j], expected[i]\n\t\t\t\t}\n\t\t\t\tif !slicesEqual(expected, result) {\n\t\t\t\t\tt.Errorf(\"want %v got %v\", expected, result)\n\t\t\t\t}\n\t\t\t})\n\n\t\t\tt.Run(\"TraverseInRange\", func(t *testing.T) {\n\t\t\t\tvar result []string\n\t\t\t\tstart, end := \"C\", \"M\"\n\t\t\t\ttree.TraverseInRange(start, end, true, true, func(n *Node) bool {\n\t\t\t\t\tresult = append(result, n.Key())\n\t\t\t\t\treturn false\n\t\t\t\t})\n\t\t\t\texpected := make([]string, 0)\n\t\t\t\tfor _, key := range tt.expected {\n\t\t\t\t\tif key \u003e= start \u0026\u0026 key \u003c end {\n\t\t\t\t\t\texpected = append(expected, key)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif !slicesEqual(expected, result) {\n\t\t\t\t\tt.Errorf(\"want %v got %v\", expected, result)\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc TestRotateWhenHeightDiffers(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\t\"right rotation when left subtree is higher\",\n\t\t\t[]string{\"E\", \"C\", \"A\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"E\", \"D\"},\n\t\t},\n\t\t{\n\t\t\t\"left rotation when right subtree is higher\",\n\t\t\t[]string{\"A\", \"C\", \"E\", \"D\", \"F\"},\n\t\t\t[]string{\"A\", \"C\", \"D\", \"E\", \"F\"},\n\t\t},\n\t\t{\n\t\t\t\"left-right rotation\",\n\t\t\t[]string{\"E\", \"A\", \"C\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"E\", \"D\"},\n\t\t},\n\t\t{\n\t\t\t\"right-left rotation\",\n\t\t\t[]string{\"A\", \"E\", \"C\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"E\", \"D\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\t// perform rotation or balance\n\t\t\ttree = tree.balance()\n\n\t\t\t// check tree structure\n\t\t\tvar result []string\n\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\tresult = append(result, n.Key())\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestRotateAndBalance(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []string\n\t\texpected []string\n\t}{\n\t\t{\n\t\t\t\"right rotation\",\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"left rotation\",\n\t\t\t[]string{\"E\", \"D\", \"C\", \"B\", \"A\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"left-right rotation\",\n\t\t\t[]string{\"C\", \"A\", \"E\", \"B\", \"D\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t\t{\n\t\t\t\"right-left rotation\",\n\t\t\t[]string{\"C\", \"E\", \"A\", \"D\", \"B\"},\n\t\t\t[]string{\"A\", \"B\", \"C\", \"D\", \"E\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvar tree *Node\n\t\t\tfor _, key := range tt.input {\n\t\t\t\ttree, _ = tree.Set(key, nil)\n\t\t\t}\n\n\t\t\ttree = tree.balance()\n\n\t\t\tvar result []string\n\t\t\ttree.Iterate(\"\", \"\", func(n *Node) bool {\n\t\t\t\tresult = append(result, n.Key())\n\t\t\t\treturn false\n\t\t\t})\n\n\t\t\tif !slicesEqual(tt.expected, result) {\n\t\t\t\tt.Errorf(\"want %v got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc slicesEqual(w1, w2 []string) bool {\n\tif len(w1) != len(w2) {\n\t\treturn false\n\t}\n\tfor i := 0; i \u003c len(w1); i++ {\n\t\tif w1[0] != w2[0] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc maxint8(a, b int8) int8 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc reverseSlice(ss []string) {\n\tfor i := 0; i \u003c len(ss)/2; i++ {\n\t\tj := len(ss) - 1 - i\n\t\tss[i], ss[j] = ss[j], ss[i]\n\t}\n}\n\nfunc TestNodeStructuralSharing(t *testing.T) {\n\tt.Run(\"unmodified paths remain shared\", func(t *testing.T) {\n\t\troot := NewNode(\"B\", 2)\n\t\troot, _ = root.Set(\"A\", 1)\n\t\troot, _ = root.Set(\"C\", 3)\n\n\t\toriginalRight := root.rightNode\n\t\tnewRoot, _ := root.Set(\"A\", 10)\n\n\t\tif newRoot.rightNode != originalRight {\n\t\t\tt.Error(\"Unmodified right subtree should remain shared\")\n\t\t}\n\t})\n\n\tt.Run(\"multiple modifications reuse shared structure\", func(t *testing.T) {\n\t\t// Create initial tree\n\t\troot := NewNode(\"B\", 2)\n\t\troot, _ = root.Set(\"A\", 1)\n\t\troot, _ = root.Set(\"C\", 3)\n\n\t\t// Store original nodes\n\t\toriginalRight := root.rightNode\n\n\t\t// First modification\n\t\tmod1, _ := root.Set(\"A\", 10)\n\n\t\t// Second modification\n\t\tmod2, _ := mod1.Set(\"C\", 30)\n\n\t\t// Check sharing in first modification\n\t\tif mod1.rightNode != originalRight {\n\t\t\tt.Error(\"First modification should share unmodified right subtree\")\n\t\t}\n\n\t\t// Check that second modification creates new right node\n\t\tif mod2.rightNode == originalRight {\n\t\t\tt.Error(\"Second modification should create new right node\")\n\t\t}\n\t})\n}\n\nfunc TestNodeCopyOnWrite(t *testing.T) {\n\tt.Run(\"copy preserves structure\", func(t *testing.T) {\n\t\troot := NewNode(\"B\", 2)\n\t\troot, _ = root.Set(\"A\", 1)\n\t\troot, _ = root.Set(\"C\", 3)\n\n\t\t// Only copy non-leaf nodes\n\t\tif !root.IsLeaf() {\n\t\t\tcopied := root._copy()\n\t\t\tif copied == root {\n\t\t\t\tt.Error(\"Copy should create new instance\")\n\t\t\t}\n\n\t\t\t// Create temporary trees to use Equal method\n\t\t\toriginal := \u0026Tree{node: root}\n\t\t\tcopiedTree := \u0026Tree{node: copied}\n\t\t\tif !original.Equal(copiedTree) {\n\t\t\t\tt.Error(\"Copied node should preserve structure\")\n\t\t\t}\n\t\t}\n\t})\n\n\tt.Run(\"removal copy pattern\", func(t *testing.T) {\n\t\t// Create a more complex tree to test removal\n\t\troot := NewNode(\"B\", 2)\n\t\troot, _ = root.Set(\"A\", 1)\n\t\troot, _ = root.Set(\"C\", 3)\n\t\troot, _ = root.Set(\"D\", 4) // Add this to ensure proper tree structure\n\n\t\t// Store references to original nodes\n\t\toriginalRight := root.rightNode\n\t\toriginalRightRight := originalRight.rightNode\n\n\t\t// Remove \"A\" which should only affect the left subtree\n\t\tnewRoot, _, _, _ := root.Remove(\"A\")\n\n\t\t// Verify right subtree remains unchanged and shared\n\t\tif newRoot.rightNode != originalRight {\n\t\t\tt.Error(\"Right subtree should remain shared during removal of left node\")\n\t\t}\n\n\t\t// Also verify deeper nodes remain shared\n\t\tif newRoot.rightNode.rightNode != originalRightRight {\n\t\t\tt.Error(\"Deep right subtree should remain shared during removal\")\n\t\t}\n\n\t\t// Verify original tree is unchanged\n\t\tif _, _, exists := root.Get(\"A\"); !exists {\n\t\t\tt.Error(\"Original tree should remain unchanged\")\n\t\t}\n\t})\n\n\tt.Run(\"copy leaf node panic\", func(t *testing.T) {\n\t\tleaf := NewNode(\"A\", 1)\n\n\t\tdefer func() {\n\t\t\tif r := recover(); r == nil {\n\t\t\t\tt.Error(\"Expected panic when copying leaf node\")\n\t\t\t}\n\t\t}()\n\n\t\t// This should panic with our specific message\n\t\tleaf._copy()\n\t})\n}\n\nfunc TestNodeEqual(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tnode1    func() *Node\n\t\tnode2    func() *Node\n\t\texpected bool\n\t}{\n\t\t{\n\t\t\tname:     \"nil nodes\",\n\t\t\tnode1:    func() *Node { return nil },\n\t\t\tnode2:    func() *Node { return nil },\n\t\t\texpected: true,\n\t\t},\n\t\t{\n\t\t\tname:     \"one nil node\",\n\t\t\tnode1:    func() *Node { return NewNode(\"A\", 1) },\n\t\t\tnode2:    func() *Node { return nil },\n\t\t\texpected: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"single leaf nodes equal\",\n\t\t\tnode1:    func() *Node { return NewNode(\"A\", 1) },\n\t\t\tnode2:    func() *Node { return NewNode(\"A\", 1) },\n\t\t\texpected: true,\n\t\t},\n\t\t{\n\t\t\tname:     \"single leaf nodes different key\",\n\t\t\tnode1:    func() *Node { return NewNode(\"A\", 1) },\n\t\t\tnode2:    func() *Node { return NewNode(\"B\", 1) },\n\t\t\texpected: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"single leaf nodes different value\",\n\t\t\tnode1:    func() *Node { return NewNode(\"A\", 1) },\n\t\t\tnode2:    func() *Node { return NewNode(\"A\", 2) },\n\t\t\texpected: false,\n\t\t},\n\t\t{\n\t\t\tname: \"complex trees equal\",\n\t\t\tnode1: func() *Node {\n\t\t\t\tn, _ := NewNode(\"B\", 2).Set(\"A\", 1)\n\t\t\t\tn, _ = n.Set(\"C\", 3)\n\t\t\t\treturn n\n\t\t\t},\n\t\t\tnode2: func() *Node {\n\t\t\t\tn, _ := NewNode(\"B\", 2).Set(\"A\", 1)\n\t\t\t\tn, _ = n.Set(\"C\", 3)\n\t\t\t\treturn n\n\t\t\t},\n\t\t\texpected: true,\n\t\t},\n\t\t{\n\t\t\tname: \"complex trees different structure\",\n\t\t\tnode1: func() *Node {\n\t\t\t\t// Create a tree with structure:\n\t\t\t\t//     B\n\t\t\t\t//    / \\\n\t\t\t\t//   A   D\n\t\t\t\tn := NewNode(\"B\", 2)\n\t\t\t\tn, _ = n.Set(\"A\", 1)\n\t\t\t\tn, _ = n.Set(\"D\", 4)\n\t\t\t\treturn n\n\t\t\t},\n\t\t\tnode2: func() *Node {\n\t\t\t\t// Create a tree with structure:\n\t\t\t\t//     C\n\t\t\t\t//    / \\\n\t\t\t\t//   A   D\n\t\t\t\tn := NewNode(\"C\", 3)\n\t\t\t\tn, _ = n.Set(\"A\", 1)\n\t\t\t\tn, _ = n.Set(\"D\", 4)\n\t\t\t\treturn n\n\t\t\t},\n\t\t\texpected: false, // These trees should be different\n\t\t},\n\t\t{\n\t\t\tname: \"complex trees same structure despite different insertion order\",\n\t\t\tnode1: func() *Node {\n\t\t\t\tn, _ := NewNode(\"B\", 2).Set(\"A\", 1)\n\t\t\t\tn, _ = n.Set(\"C\", 3)\n\t\t\t\treturn n\n\t\t\t},\n\t\t\tnode2: func() *Node {\n\t\t\t\tn, _ := NewNode(\"A\", 1).Set(\"B\", 2)\n\t\t\t\tn, _ = n.Set(\"C\", 3)\n\t\t\t\treturn n\n\t\t\t},\n\t\t\texpected: true,\n\t\t},\n\t\t{\n\t\t\tname: \"truly different structures\",\n\t\t\tnode1: func() *Node {\n\t\t\t\tn, _ := NewNode(\"B\", 2).Set(\"A\", 1)\n\t\t\t\treturn n // Tree with just two nodes\n\t\t\t},\n\t\t\tnode2: func() *Node {\n\t\t\t\tn, _ := NewNode(\"B\", 2).Set(\"C\", 3)\n\t\t\t\treturn n // Different two-node tree\n\t\t\t},\n\t\t\texpected: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tnode1 := tt.node1()\n\t\t\tnode2 := tt.node2()\n\t\t\tresult := node1.Equal(node2)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"Expected Equal to return %v for %s\", tt.expected, tt.name)\n\t\t\t\tprintln(\"\\nComparison failed:\")\n\t\t\t\tprintln(\"Tree 1:\")\n\t\t\t\tprintTree(node1, 0)\n\t\t\t\tprintln(\"Tree 2:\")\n\t\t\t\tprintTree(node2, 0)\n\t\t\t}\n\t\t})\n\t}\n}\n\n// Helper function to print tree structure\nfunc printTree(node *Node, level int) {\n\tif node == nil {\n\t\treturn\n\t}\n\tindent := strings.Repeat(\"  \", level)\n\tprintln(fmt.Sprintf(\"%sKey: %s, Value: %v, Height: %d, Size: %d\",\n\t\tindent, node.key, node.value, node.height, node.size))\n\tprintTree(node.leftNode, level+1)\n\tprintTree(node.rightNode, level+1)\n}\n"},{"name":"tree.gno","body":"// Package cow provides a Copy-on-Write (CoW) AVL tree implementation.\n// This is a fork of gno.land/p/nt/avl/v0 that adds CoW functionality\n// while maintaining the original AVL tree interface and properties.\n//\n// Copy-on-Write creates a copy of a data structure only when it is modified,\n// while still presenting the appearance of a full copy. When a tree is cloned,\n// it initially shares all its nodes with the original tree. Only when a\n// modification is made to either the original or the clone are new nodes created,\n// and only along the path from the root to the modified node.\n//\n// Key features:\n//   - O(1) cloning operation\n//   - Minimal memory usage through structural sharing\n//   - Full AVL tree functionality (self-balancing, ordered operations)\n//   - Thread-safe for concurrent reads of shared structures\n//\n// While the CoW mechanism handles structural copying automatically, users need\n// to consider how to handle the values stored in the tree:\n//\n//  1. Simple Values (int, string, etc.):\n//     - These are copied by value automatically\n//     - No additional handling needed\n//\n//  2. Complex Values (structs, pointers):\n//     - Only the reference is copied by default\n//     - Users must implement their own deep copy mechanism if needed\n//\n// Example:\n//\n//\t// Create original tree\n//\toriginal := cow.NewTree()\n//\toriginal.Set(\"key1\", \"value1\")\n//\n//\t// Create a clone - O(1) operation\n//\tclone := original.Clone()\n//\n//\t// Modify clone - only affected nodes are copied\n//\tclone.Set(\"key1\", \"modified\")\n//\n//\t// Original remains unchanged\n//\tval := original.Get(\"key1\") // Returns \"value1\"\npackage cow\n\ntype IterCbFn func(key string, value any) bool\n\n//----------------------------------------\n// Tree\n\n// The zero struct can be used as an empty tree.\ntype Tree struct {\n\tnode *Node\n}\n\n// NewTree creates a new empty AVL tree.\nfunc NewTree() *Tree {\n\treturn \u0026Tree{\n\t\tnode: nil,\n\t}\n}\n\n// Size returns the number of key-value pair in the tree.\nfunc (tree *Tree) Size() int {\n\treturn tree.node.Size()\n}\n\n// Has checks whether a key exists in the tree.\n// It returns true if the key exists, otherwise false.\nfunc (tree *Tree) Has(key string) (has bool) {\n\treturn tree.node.Has(key)\n}\n\n// Get retrieves the value associated with the given key.\n// It returns the value if the key exists, or nil if it doesn't.\nfunc (tree *Tree) Get(key string) any {\n\t_, value, exists := tree.node.Get(key)\n\tif !exists {\n\t\treturn nil\n\t}\n\treturn value\n}\n\n// GetByIndex retrieves the key-value pair at the specified index in the tree.\n// It returns the key and value at the given index.\nfunc (tree *Tree) GetByIndex(index int) (key string, value any) {\n\treturn tree.node.GetByIndex(index)\n}\n\n// Set inserts a key-value pair into the tree.\n// If the key already exists, the value will be updated.\n// It returns a boolean indicating whether the key was newly inserted or updated.\nfunc (tree *Tree) Set(key string, value any) (updated bool) {\n\tnewnode, updated := tree.node.Set(key, value)\n\ttree.node = newnode\n\treturn updated\n}\n\n// Remove removes a key-value pair from the tree.\n// It returns the removed value and a boolean indicating whether the key was found and removed.\nfunc (tree *Tree) Remove(key string) (value any, removed bool) {\n\tnewnode, _, value, removed := tree.node.Remove(key)\n\ttree.node = newnode\n\treturn value, removed\n}\n\n// Iterate performs an in-order traversal of the tree within the specified key range.\n// It calls the provided callback function for each key-value pair encountered.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) Iterate(start, end string, cb IterCbFn) bool {\n\treturn tree.node.TraverseInRange(start, end, true, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// ReverseIterate performs a reverse in-order traversal of the tree within the specified key range.\n// It calls the provided callback function for each key-value pair encountered.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) ReverseIterate(start, end string, cb IterCbFn) bool {\n\treturn tree.node.TraverseInRange(start, end, false, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// IterateByOffset performs an in-order traversal of the tree starting from the specified offset.\n// It calls the provided callback function for each key-value pair encountered, up to the specified count.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) IterateByOffset(offset int, count int, cb IterCbFn) bool {\n\treturn tree.node.TraverseByOffset(offset, count, true, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// ReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset.\n// It calls the provided callback function for each key-value pair encountered, up to the specified count.\n// If the callback returns true, the iteration is stopped.\nfunc (tree *Tree) ReverseIterateByOffset(offset int, count int, cb IterCbFn) bool {\n\treturn tree.node.TraverseByOffset(offset, count, false, true,\n\t\tfunc(node *Node) bool {\n\t\t\treturn cb(node.Key(), node.Value())\n\t\t},\n\t)\n}\n\n// Equal returns true if the two trees contain the same key-value pairs.\n// WARNING: This is an expensive operation that recursively traverses the entire tree structure.\n// It should only be used in tests or when absolutely necessary.\nfunc (tree *Tree) Equal(other *Tree) bool {\n\tif tree == nil || other == nil {\n\t\treturn tree == other\n\t}\n\treturn tree.node.Equal(other.node)\n}\n\n// Clone creates a shallow copy of the tree\nfunc (tree *Tree) Clone() *Tree {\n\tif tree == nil {\n\t\treturn nil\n\t}\n\treturn \u0026Tree{\n\t\tnode: tree.node,\n\t}\n}\n"},{"name":"tree_test.gno","body":"package cow\n\nimport (\n\t\"testing\"\n)\n\nfunc TestNewTree(t *testing.T) {\n\ttree := NewTree()\n\tif tree.node != nil {\n\t\tt.Error(\"Expected tree.node to be nil\")\n\t}\n}\n\nfunc TestTreeSize(t *testing.T) {\n\ttree := NewTree()\n\tif tree.Size() != 0 {\n\t\tt.Error(\"Expected empty tree size to be 0\")\n\t}\n\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\tif tree.Size() != 2 {\n\t\tt.Error(\"Expected tree size to be 2\")\n\t}\n}\n\nfunc TestTreeHas(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\n\tif !tree.Has(\"key1\") {\n\t\tt.Error(\"Expected tree to have key1\")\n\t}\n\n\tif tree.Has(\"key2\") {\n\t\tt.Error(\"Expected tree to not have key2\")\n\t}\n}\n\nfunc TestTreeGet(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\n\tvalue := tree.Get(\"key1\")\n\tif value == nil || value != \"value1\" {\n\t\tt.Error(\"Expected Get to return value1 and true\")\n\t}\n\n\tif tree.Has(\"key2\") {\n\t\tt.Error(\"Expected Get to return false for non-existent key\")\n\t}\n}\n\nfunc TestTreeGetByIndex(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\n\tkey, value := tree.GetByIndex(0)\n\tif key != \"key1\" || value != \"value1\" {\n\t\tt.Error(\"Expected GetByIndex(0) to return key1 and value1\")\n\t}\n\n\tkey, value = tree.GetByIndex(1)\n\tif key != \"key2\" || value != \"value2\" {\n\t\tt.Error(\"Expected GetByIndex(1) to return key2 and value2\")\n\t}\n\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Error(\"Expected GetByIndex to panic for out-of-range index\")\n\t\t}\n\t}()\n\ttree.GetByIndex(2)\n}\n\nfunc TestTreeRemove(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\n\tvalue, removed := tree.Remove(\"key1\")\n\tif !removed || value != \"value1\" || tree.Size() != 0 {\n\t\tt.Error(\"Expected Remove to remove key-value pair\")\n\t}\n\n\t_, removed = tree.Remove(\"key2\")\n\tif removed {\n\t\tt.Error(\"Expected Remove to return false for non-existent key\")\n\t}\n}\n\nfunc TestTreeIterate(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key1\", \"key2\", \"key3\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\nfunc TestTreeReverseIterate(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.ReverseIterate(\"\", \"\", func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key3\", \"key2\", \"key1\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\nfunc TestTreeIterateByOffset(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.IterateByOffset(1, 2, func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key2\", \"key3\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\nfunc TestTreeReverseIterateByOffset(t *testing.T) {\n\ttree := NewTree()\n\ttree.Set(\"key1\", \"value1\")\n\ttree.Set(\"key2\", \"value2\")\n\ttree.Set(\"key3\", \"value3\")\n\n\tvar keys []string\n\ttree.ReverseIterateByOffset(1, 2, func(key string, value any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\n\texpectedKeys := []string{\"key2\", \"key1\"}\n\tif !slicesEqual(keys, expectedKeys) {\n\t\tt.Errorf(\"Expected keys %v, got %v\", expectedKeys, keys)\n\t}\n}\n\n// Verify that Tree implements avl.ITree\n// var _ avl.ITree = (*Tree)(nil) // TODO: fix gnovm bug: ./examples/gno.land/p/moul/cow: test pkg: panic: gno.land/p/moul/cow/tree_test.gno:166:5: name avl not defined in fileset with files [node.gno tree.gno node_test.gno tree_test.gno]:\n\nfunc TestCopyOnWrite(t *testing.T) {\n\t// Create original tree\n\toriginal := NewTree()\n\toriginal.Set(\"A\", 1)\n\toriginal.Set(\"B\", 2)\n\toriginal.Set(\"C\", 3)\n\n\t// Create a clone\n\tclone := original.Clone()\n\n\t// Modify clone\n\tclone.Set(\"B\", 20)\n\tclone.Set(\"D\", 4)\n\n\t// Verify original is unchanged\n\tif val := original.Get(\"B\"); val != 2 {\n\t\tt.Errorf(\"Original tree was modified: expected B=2, got B=%v\", val)\n\t}\n\tif original.Has(\"D\") {\n\t\tt.Error(\"Original tree was modified: found key D\")\n\t}\n\n\t// Verify clone has new values\n\tif val := clone.Get(\"B\"); val != 20 {\n\t\tt.Errorf(\"Clone not updated: expected B=20, got B=%v\", val)\n\t}\n\tif val := clone.Get(\"D\"); val != 4 {\n\t\tt.Errorf(\"Clone not updated: expected D=4, got D=%v\", val)\n\t}\n}\n\nfunc TestCopyOnWriteEdgeCases(t *testing.T) {\n\tt.Run(\"nil tree clone\", func(t *testing.T) {\n\t\tvar original *Tree\n\t\tclone := original.Clone()\n\t\tif clone != nil {\n\t\t\tt.Error(\"Expected nil clone from nil tree\")\n\t\t}\n\t})\n\n\tt.Run(\"empty tree clone\", func(t *testing.T) {\n\t\toriginal := NewTree()\n\t\tclone := original.Clone()\n\n\t\t// Modify clone\n\t\tclone.Set(\"A\", 1)\n\n\t\tif original.Size() != 0 {\n\t\t\tt.Error(\"Original empty tree was modified\")\n\t\t}\n\t\tif clone.Size() != 1 {\n\t\t\tt.Error(\"Clone was not modified\")\n\t\t}\n\t})\n\n\tt.Run(\"multiple clones\", func(t *testing.T) {\n\t\toriginal := NewTree()\n\t\toriginal.Set(\"A\", 1)\n\t\toriginal.Set(\"B\", 2)\n\n\t\t// Create multiple clones\n\t\tclone1 := original.Clone()\n\t\tclone2 := original.Clone()\n\t\tclone3 := clone1.Clone()\n\n\t\t// Modify each clone differently\n\t\tclone1.Set(\"A\", 10)\n\t\tclone2.Set(\"B\", 20)\n\t\tclone3.Set(\"C\", 30)\n\n\t\t// Check original remains unchanged\n\t\tif val := original.Get(\"A\"); val != 1 {\n\t\t\tt.Errorf(\"Original modified: expected A=1, got A=%v\", val)\n\t\t}\n\t\tif val := original.Get(\"B\"); val != 2 {\n\t\t\tt.Errorf(\"Original modified: expected B=2, got B=%v\", val)\n\t\t}\n\n\t\t// Verify each clone has correct values\n\t\tif val := clone1.Get(\"A\"); val != 10 {\n\t\t\tt.Errorf(\"Clone1 incorrect: expected A=10, got A=%v\", val)\n\t\t}\n\t\tif val := clone2.Get(\"B\"); val != 20 {\n\t\t\tt.Errorf(\"Clone2 incorrect: expected B=20, got B=%v\", val)\n\t\t}\n\t\tif val := clone3.Get(\"C\"); val != 30 {\n\t\t\tt.Errorf(\"Clone3 incorrect: expected C=30, got C=%v\", val)\n\t\t}\n\t})\n\n\tt.Run(\"clone after removal\", func(t *testing.T) {\n\t\toriginal := NewTree()\n\t\toriginal.Set(\"A\", 1)\n\t\toriginal.Set(\"B\", 2)\n\t\toriginal.Set(\"C\", 3)\n\n\t\t// Remove a node and then clone\n\t\toriginal.Remove(\"B\")\n\t\tclone := original.Clone()\n\n\t\t// Modify clone\n\t\tclone.Set(\"B\", 20)\n\n\t\t// Verify original state\n\t\tif original.Has(\"B\") {\n\t\t\tt.Error(\"Original tree should not have key B\")\n\t\t}\n\n\t\t// Verify clone state\n\t\tif val := clone.Get(\"B\"); val != 20 {\n\t\t\tt.Errorf(\"Clone incorrect: expected B=20, got B=%v\", val)\n\t\t}\n\t})\n\n\tt.Run(\"concurrent modifications\", func(t *testing.T) {\n\t\toriginal := NewTree()\n\t\toriginal.Set(\"A\", 1)\n\t\toriginal.Set(\"B\", 2)\n\n\t\tclone1 := original.Clone()\n\t\tclone2 := original.Clone()\n\n\t\t// Modify same key in different clones\n\t\tclone1.Set(\"B\", 20)\n\t\tclone2.Set(\"B\", 30)\n\n\t\t// Each clone should have its own value\n\t\tif val := clone1.Get(\"B\"); val != 20 {\n\t\t\tt.Errorf(\"Clone1 incorrect: expected B=20, got B=%v\", val)\n\t\t}\n\t\tif val := clone2.Get(\"B\"); val != 30 {\n\t\t\tt.Errorf(\"Clone2 incorrect: expected B=30, got B=%v\", val)\n\t\t}\n\t})\n\n\tt.Run(\"deep tree modifications\", func(t *testing.T) {\n\t\toriginal := NewTree()\n\t\t// Create a deeper tree\n\t\tkeys := []string{\"M\", \"F\", \"T\", \"B\", \"H\", \"P\", \"Z\"}\n\t\tfor _, k := range keys {\n\t\t\toriginal.Set(k, k)\n\t\t}\n\n\t\tclone := original.Clone()\n\n\t\t// Modify a deep node\n\t\tclone.Set(\"H\", \"modified\")\n\n\t\t// Check original remains unchanged\n\t\tif val := original.Get(\"H\"); val != \"H\" {\n\t\t\tt.Errorf(\"Original modified: expected H='H', got H=%v\", val)\n\t\t}\n\n\t\t// Verify clone modification\n\t\tif val := clone.Get(\"H\"); val != \"modified\" {\n\t\t\tt.Errorf(\"Clone incorrect: expected H='modified', got H=%v\", val)\n\t\t}\n\t})\n\n\tt.Run(\"rebalancing test\", func(t *testing.T) {\n\t\toriginal := NewTree()\n\t\t// Insert nodes that will cause rotations\n\t\tkeys := []string{\"A\", \"B\", \"C\", \"D\", \"E\"}\n\t\tfor _, k := range keys {\n\t\t\toriginal.Set(k, k)\n\t\t}\n\n\t\tclone := original.Clone()\n\n\t\t// Add more nodes to clone to trigger rebalancing\n\t\tclone.Set(\"F\", \"F\")\n\t\tclone.Set(\"G\", \"G\")\n\n\t\t// Verify original structure remains unchanged\n\t\toriginalKeys := collectKeys(original)\n\t\texpectedOriginal := []string{\"A\", \"B\", \"C\", \"D\", \"E\"}\n\t\tif !slicesEqual(originalKeys, expectedOriginal) {\n\t\t\tt.Errorf(\"Original tree structure changed: got %v, want %v\", originalKeys, expectedOriginal)\n\t\t}\n\n\t\t// Verify clone has all keys\n\t\tcloneKeys := collectKeys(clone)\n\t\texpectedClone := []string{\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\"}\n\t\tif !slicesEqual(cloneKeys, expectedClone) {\n\t\t\tt.Errorf(\"Clone tree structure incorrect: got %v, want %v\", cloneKeys, expectedClone)\n\t\t}\n\t})\n\n\tt.Run(\"value mutation test\", func(t *testing.T) {\n\t\ttype MutableValue struct {\n\t\t\tData string\n\t\t}\n\n\t\toriginal := NewTree()\n\t\tmutable := \u0026MutableValue{Data: \"original\"}\n\t\toriginal.Set(\"key\", mutable)\n\n\t\tclone := original.Clone()\n\n\t\t// Modify the mutable value\n\t\tmutable.Data = \"modified\"\n\n\t\t// Both original and clone should see the modification\n\t\t// because we're not deep copying values\n\t\torigVal := original.Get(\"key\")\n\t\tcloneVal := clone.Get(\"key\")\n\n\t\tif origVal.(*MutableValue).Data != \"modified\" {\n\t\t\tt.Error(\"Original value not modified as expected\")\n\t\t}\n\t\tif cloneVal.(*MutableValue).Data != \"modified\" {\n\t\t\tt.Error(\"Clone value not modified as expected\")\n\t\t}\n\t})\n}\n\n// Helper function to collect all keys in order\nfunc collectKeys(tree *Tree) []string {\n\tvar keys []string\n\ttree.Iterate(\"\", \"\", func(key string, _ any) bool {\n\t\tkeys = append(keys, key)\n\t\treturn false\n\t})\n\treturn keys\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"IU4PEZfPpOE/oTU1zZxqUpFKtLnYx71asUfrasazw5Yk+FrnmM6zotGEpzXoRK1lrVComxGb1RvomqxSroXXAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"debug","path":"gno.land/p/moul/debug","files":[{"name":"debug.gno","body":"// Package debug provides utilities for logging and displaying debug information\n// within Gno realms. It supports conditional rendering of logs and metadata,\n// toggleable via query parameters.\n//\n// Key Features:\n// - Log collection and display using Markdown formatting.\n// - Metadata display for realm path, address, and height.\n// - Collapsible debug section for cleaner presentation.\n// - Query-based debug toggle using `?debug=1`.\npackage debug\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"time\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Debug encapsulates debug information, including logs and metadata.\ntype Debug struct {\n\tLogs         []string\n\tHideMetadata bool\n}\n\n// Log appends a new line of debug information to the Logs slice.\nfunc (d *Debug) Log(line string) {\n\td.Logs = append(d.Logs, line)\n}\n\n// Render generates the debug content as a collapsible Markdown section.\n// It conditionally renders logs and metadata if enabled via the `?debug=1` query parameter.\nfunc (d Debug) Render(path string) string {\n\tif realmpath.Parse(path).Query.Get(\"debug\") != \"1\" {\n\t\treturn \"\"\n\t}\n\n\tvar content string\n\n\tif d.Logs != nil {\n\t\tcontent += md.H3(\"Logs\")\n\t\tcontent += md.BulletList(d.Logs)\n\t}\n\n\tif !d.HideMetadata {\n\t\tcontent += md.H3(\"Metadata\")\n\t\ttable := mdtable.Table{\n\t\t\tHeaders: []string{\"Key\", \"Value\"},\n\t\t}\n\t\ttable.Append([]string{\"`unsafe.CurrentRealm().PkgPath()`\", string(unsafe.CurrentRealm().PkgPath())})\n\t\ttable.Append([]string{\"`unsafe.CurrentRealm().Address()`\", string(unsafe.CurrentRealm().Address())})\n\t\ttable.Append([]string{\"`unsafe.PreviousRealm().PkgPath()`\", string(unsafe.PreviousRealm().PkgPath())})\n\t\ttable.Append([]string{\"`unsafe.PreviousRealm().Address()`\", string(unsafe.PreviousRealm().Address())})\n\t\ttable.Append([]string{\"`runtime.ChainHeight()`\", ufmt.Sprintf(\"%d\", runtime.ChainHeight())})\n\t\ttable.Append([]string{\"`time.Now().Format(time.RFC3339)`\", time.Now().Format(time.RFC3339)})\n\t\tcontent += table.String()\n\t}\n\n\tif content == \"\" {\n\t\treturn \"\"\n\t}\n\n\treturn md.CollapsibleSection(\"debug\", content)\n}\n\n// Render displays metadata about the current realm but does not display logs.\n// This function uses a default Debug struct with metadata enabled and no logs.\nfunc Render(path string) string {\n\treturn Debug{}.Render(path)\n}\n\n// IsEnabled checks if the `?debug=1` query parameter is set in the given path.\n// Returns true if debugging is enabled, otherwise false.\nfunc IsEnabled(path string) bool {\n\treq := realmpath.Parse(path)\n\treturn req.Query.Get(\"debug\") == \"1\"\n}\n\n// ToggleURL modifies the given path's query string to toggle the `?debug=1` parameter.\n// If debugging is currently enabled, it removes the parameter.\n// If debugging is disabled, it adds the parameter.\nfunc ToggleURL(path string) string {\n\treq := realmpath.Parse(path)\n\tif IsEnabled(path) {\n\t\treq.Query.Del(\"debug\")\n\t} else {\n\t\treq.Query.Add(\"debug\", \"1\")\n\t}\n\treturn req.String()\n}\n"},{"name":"debug_test.gno","body":"package debug\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestPackage(t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\n\ttestPackage(t)\n}\n\nfunc testPackage(t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test/test\"))\n\n\t// no debug\n\tgot := Render(\"\")\n\texpected := ``\n\tuassert.Equal(t, expected, got)\n\n\t// debug without logs\n\tgot = Render(\"?debug=1\")\n\texpected = `\u003cdetails\u003e\u003csummary\u003edebug\u003c/summary\u003e\n\n### Metadata\n| Key | Value |\n| --- | --- |\n| ±unsafe.CurrentRealm().PkgPath()± | gno.land/r/test/test |\n| ±unsafe.CurrentRealm().Address()± | g1z7fga7u94pdmamlvcrtvsfwxgsye0qv3rres7n |\n| ±unsafe.PreviousRealm().PkgPath()± |  |\n| ±unsafe.PreviousRealm().Address()± | g1user |\n| ±runtime.ChainHeight()± | 123 |\n| ±time.Now().Format(time.RFC3339)± | 2009-02-13T23:31:30Z |\n\n\u003c/details\u003e\n`\n\texpected = strings.ReplaceAll(expected, \"±\", \"`\")\n\n\tprintln(\"###################\")\n\tprintln(got)\n\tprintln(\"###################\")\n\tprintln(expected)\n\tprintln(\"###################\")\n\n\tuassert.Equal(t, expected, got)\n\n\t// debug with logs\n\tvar d Debug\n\td.Log(\"hello world!\")\n\td.Log(\"foobar\")\n\tgot = d.Render(\"?debug=1\")\n\texpected = `\u003cdetails\u003e\u003csummary\u003edebug\u003c/summary\u003e\n\n### Logs\n- hello world!\n- foobar\n### Metadata\n| Key | Value |\n| --- | --- |\n| ±unsafe.CurrentRealm().PkgPath()± | gno.land/r/test/test |\n| ±unsafe.CurrentRealm().Address()± | g1z7fga7u94pdmamlvcrtvsfwxgsye0qv3rres7n |\n| ±unsafe.PreviousRealm().PkgPath()± |  |\n| ±unsafe.PreviousRealm().Address()± | g1user |\n| ±runtime.ChainHeight()± | 123 |\n| ±time.Now().Format(time.RFC3339)± | 2009-02-13T23:31:30Z |\n\n\u003c/details\u003e\n`\n\texpected = strings.ReplaceAll(expected, \"±\", \"`\")\n\tuassert.Equal(t, got, expected)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/debug\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"0fCiOowF/OWaVDxpd+zhQx2ClmVS/KZGqdXKNlvDmXIhAu5vYnZhT622aOR78zzSOLUw4r5IvcNuQcCEvMSWCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"deque","path":"gno.land/p/moul/deque","files":[{"name":"deque.gno","body":"// Package deque provides a doubly-linked deque with optional size limits,\n// optimized for minimal storage updates and O(1) operations in Gno environments.\n//\n// This implementation uses a doubly-linked list structure that provides:\n// - O(1) push back operations\n// - O(1) pop front operations\n// - O(1) access to first/last elements\n// - No array resizing or slice shifting\n// - Minimal allocations (one node per element)\n// - Minimal storage updates (typically 1-2 pointer updates per operation)\n// - Optional max-size eviction policy\n//\n// Optimized for Gno blockchain storage: each operation updates only specific nodes\n// rather than entire data structures, making it ideal for storage-efficient lists\n// that need simple read operations like first/last access, size checks, list\n// enumeration, and iteration with minimal storage update overhead.\n//\n// Example usage:\n//\n//\t// Unbounded deque\n//\td := deque.New()\n//\td.PushBack(\"a\", \"b\", \"c\")\n//\n//\t// Bounded deque with automatic eviction\n//\td = deque.NewBounded(3)\n//\td.PushBack(\"a\", \"b\", \"c\", \"d\")  // \"a\" gets evicted\n//\n//\tfirst := d.PopFront()  // Returns \"b\"\n//\tlast := d.Last()       // Returns \"d\"\n//\n//\t// Iterate forward\n//\titer := d.All()\n//\titer(func(val any) bool {\n//\t    println(val)\n//\t    return true // continue\n//\t})\n//\n//\t// Iterate backward\n//\titer = d.Backward()\n//\titer(func(val any) bool {\n//\t    println(val)\n//\t    return true // continue\n//\t})\npackage deque\n\n// Node represents a single element in the doubly-linked list\ntype Node struct {\n\tvalue any\n\tprev  *Node\n\tnext  *Node\n}\n\n// Deque represents a doubly-linked deque with optional size limits\ntype Deque struct {\n\thead *Node\n\ttail *Node\n\tsize int\n\tmax  int // 0 = unlimited\n}\n\n// New creates a new unbounded deque.\nfunc New() *Deque {\n\treturn \u0026Deque{\n\t\tmax: 0, // unlimited\n\t}\n}\n\n// NewBounded creates a new bounded deque with the specified maximum size.\n// When the deque exceeds maxSize, elements are removed from the front.\nfunc NewBounded(maxSize int) *Deque {\n\tif maxSize \u003c= 0 {\n\t\tmaxSize = 1\n\t}\n\treturn \u0026Deque{\n\t\tmax: maxSize,\n\t}\n}\n\n// PushBack adds one or more elements to the back of the deque.\n// If the deque exceeds maxSize, elements are removed from the front.\nfunc (d *Deque) PushBack(items ...any) {\n\tfor _, item := range items {\n\t\td.pushBackSingle(item)\n\t}\n}\n\n// pushBackSingle adds a single element to the back\nfunc (d *Deque) pushBackSingle(item any) {\n\tnode := \u0026Node{value: item}\n\n\tif d.tail == nil {\n\t\t// First element\n\t\td.head = node\n\t\td.tail = node\n\t} else {\n\t\t// Link to existing tail\n\t\td.tail.next = node\n\t\tnode.prev = d.tail\n\t\td.tail = node\n\t}\n\n\td.size++\n\n\t// Handle size limit by removing from front\n\tif d.max \u003e 0 \u0026\u0026 d.size \u003e d.max {\n\t\td.popFrontNode()\n\t}\n}\n\n// PopFront removes and returns the front element.\n// Returns nil if the deque is empty.\nfunc (d *Deque) PopFront() any {\n\tif d.head == nil {\n\t\treturn nil\n\t}\n\n\tvalue := d.head.value\n\td.popFrontNode()\n\treturn value\n}\n\n// popFrontNode removes the front node\nfunc (d *Deque) popFrontNode() {\n\tif d.head == nil {\n\t\treturn\n\t}\n\n\tif d.head == d.tail {\n\t\t// Only one element\n\t\td.head = nil\n\t\td.tail = nil\n\t} else {\n\t\t// Move head forward\n\t\td.head = d.head.next\n\t\td.head.prev = nil\n\t}\n\n\td.size--\n}\n\n// PopBack removes and returns the back element.\n// Returns nil if the deque is empty.\nfunc (d *Deque) PopBack() any {\n\tif d.tail == nil {\n\t\treturn nil\n\t}\n\n\tvalue := d.tail.value\n\td.popBackNode()\n\treturn value\n}\n\n// popBackNode removes the back node\nfunc (d *Deque) popBackNode() {\n\tif d.tail == nil {\n\t\treturn\n\t}\n\n\tif d.head == d.tail {\n\t\t// Only one element\n\t\td.head = nil\n\t\td.tail = nil\n\t} else {\n\t\t// Move tail backward\n\t\td.tail = d.tail.prev\n\t\td.tail.next = nil\n\t}\n\n\td.size--\n}\n\n// PushFront adds one or more elements to the front of the deque.\n// If bounded and size limit is exceeded, elements are removed from the back.\nfunc (d *Deque) PushFront(items ...any) {\n\tfor _, item := range items {\n\t\td.pushFrontSingle(item)\n\t}\n}\n\n// pushFrontSingle adds a single element to the front\nfunc (d *Deque) pushFrontSingle(item any) {\n\tnode := \u0026Node{value: item}\n\n\tif d.head == nil {\n\t\t// First element\n\t\td.head = node\n\t\td.tail = node\n\t} else {\n\t\t// Link to existing head\n\t\td.head.prev = node\n\t\tnode.next = d.head\n\t\td.head = node\n\t}\n\n\td.size++\n\n\t// Handle size limit by removing from back\n\tif d.max \u003e 0 \u0026\u0026 d.size \u003e d.max {\n\t\td.popBackNode()\n\t}\n}\n\n// Size returns the current number of elements\nfunc (d *Deque) Size() int {\n\treturn d.size\n}\n\n// MaxSize returns the maximum size limit (0 = unlimited)\nfunc (d *Deque) MaxSize() int {\n\treturn d.max\n}\n\n// IsEmpty returns true if the deque is empty\nfunc (d *Deque) IsEmpty() bool {\n\treturn d.size == 0\n}\n\n// IsBounded returns true if the deque has a size limit\nfunc (d *Deque) IsBounded() bool {\n\treturn d.max \u003e 0\n}\n\n// First returns the front element without removing it.\n// Returns nil if the deque is empty.\nfunc (d *Deque) First() any {\n\tif d.head == nil {\n\t\treturn nil\n\t}\n\treturn d.head.value\n}\n\n// Last returns the back element without removing it.\n// Returns nil if the deque is empty.\nfunc (d *Deque) Last() any {\n\tif d.tail == nil {\n\t\treturn nil\n\t}\n\treturn d.tail.value\n}\n\n// Get returns the element at the specified index (0 = front).\n// Returns nil if index is out of bounds.\nfunc (d *Deque) Get(index int) any {\n\tif index \u003c 0 || index \u003e= d.size {\n\t\treturn nil\n\t}\n\n\tnode := d.head\n\tfor i := 0; i \u003c index; i++ {\n\t\tnode = node.next\n\t}\n\treturn node.value\n}\n\n// List returns all elements as a slice, ordered from front to back\nfunc (d *Deque) List() []any {\n\tif d.size == 0 {\n\t\treturn nil\n\t}\n\n\tresult := make([]any, d.size)\n\tnode := d.head\n\tfor i := 0; i \u003c d.size; i++ {\n\t\tresult[i] = node.value\n\t\tnode = node.next\n\t}\n\treturn result\n}\n\n// Enumerate calls fn for each element with its index (0-based from front)\nfunc (d *Deque) Enumerate(fn func(index int, value any) bool) {\n\tnode := d.head\n\tfor i := 0; i \u003c d.size; i++ {\n\t\tif fn(i, node.value) {\n\t\t\tbreak\n\t\t}\n\t\tnode = node.next\n\t}\n}\n\n// Clear removes all elements from the deque\nfunc (d *Deque) Clear() {\n\td.head = nil\n\td.tail = nil\n\td.size = 0\n}\n\n// SetMaxSize changes the maximum size limit.\n// If the new limit is smaller than current size, elements are removed from front.\nfunc (d *Deque) SetMaxSize(maxSize int) {\n\td.max = maxSize\n\n\tif maxSize \u003e 0 {\n\t\tfor d.size \u003e maxSize {\n\t\t\td.popFrontNode()\n\t\t}\n\t}\n}\n\n// All returns an iterator function for forward traversal (Go 1.23+ compatible)\n// Note: range-over-func syntax is not yet supported in Gno, use iter() directly\nfunc (d *Deque) All() func(yield func(any) bool) {\n\treturn func(yield func(any) bool) {\n\t\tcurrent := d.head\n\t\tfor current != nil {\n\t\t\tif !yield(current.value) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcurrent = current.next\n\t\t}\n\t}\n}\n\n// Backward returns an iterator function for backward traversal (Go 1.23+ compatible)\n// Note: range-over-func syntax is not yet supported in Gno, use iter() directly\nfunc (d *Deque) Backward() func(yield func(any) bool) {\n\treturn func(yield func(any) bool) {\n\t\tcurrent := d.tail\n\t\tfor current != nil {\n\t\t\tif !yield(current.value) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcurrent = current.prev\n\t\t}\n\t}\n}\n"},{"name":"deque_test.gno","body":"package deque\n\nimport \"testing\"\n\nfunc TestNew(t *testing.T) {\n\td := New()\n\tif d.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0, got %d\", d.Size())\n\t}\n\tif d.MaxSize() != 0 {\n\t\tt.Errorf(\"Expected max size 0 (unlimited), got %d\", d.MaxSize())\n\t}\n\tif d.IsBounded() {\n\t\tt.Error(\"Expected unbounded deque\")\n\t}\n}\n\nfunc TestNewBounded(t *testing.T) {\n\td := NewBounded(5)\n\tif d.MaxSize() != 5 {\n\t\tt.Errorf(\"Expected max size 5, got %d\", d.MaxSize())\n\t}\n\tif d.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0, got %d\", d.Size())\n\t}\n\tif !d.IsBounded() {\n\t\tt.Error(\"Expected bounded deque\")\n\t}\n}\n\nfunc TestPushBackPopFront(t *testing.T) {\n\td := New() // unbounded\n\n\t// Test empty pop\n\tif d.PopFront() != nil {\n\t\tt.Error(\"Expected nil when popping from empty deque\")\n\t}\n\n\td.PushBack(\"a\", \"b\", \"c\")\n\tif d.Size() != 3 {\n\t\tt.Errorf(\"Expected size 3, got %d\", d.Size())\n\t}\n\n\titem := d.PopFront()\n\tif item != \"a\" {\n\t\tt.Errorf(\"Expected 'a', got %v\", item)\n\t}\n\tif d.Size() != 2 {\n\t\tt.Errorf(\"Expected size 2, got %d\", d.Size())\n\t}\n}\n\nfunc TestPushFrontPopBack(t *testing.T) {\n\td := New() // unbounded\n\n\td.PushFront(\"a\", \"b\", \"c\")\n\tif d.Size() != 3 {\n\t\tt.Errorf(\"Expected size 3, got %d\", d.Size())\n\t}\n\n\t// Elements should be in reverse order: [c, b, a]\n\tif d.First() != \"c\" {\n\t\tt.Errorf(\"Expected 'c' as first, got %v\", d.First())\n\t}\n\tif d.Last() != \"a\" {\n\t\tt.Errorf(\"Expected 'a' as last, got %v\", d.Last())\n\t}\n\n\titem := d.PopBack()\n\tif item != \"a\" {\n\t\tt.Errorf(\"Expected 'a', got %v\", item)\n\t}\n\tif d.Size() != 2 {\n\t\tt.Errorf(\"Expected size 2, got %d\", d.Size())\n\t}\n}\n\nfunc TestBoundedEvictionBack(t *testing.T) {\n\td := NewBounded(3)\n\n\t// Fill to capacity\n\td.PushBack(\"a\", \"b\", \"c\")\n\tif d.Size() != 3 {\n\t\tt.Errorf(\"Expected size 3, got %d\", d.Size())\n\t}\n\n\t// Push beyond capacity - should evict \"a\" from front\n\td.PushBack(\"d\")\n\tif d.Size() != 3 {\n\t\tt.Errorf(\"Expected size 3, got %d\", d.Size())\n\t}\n\tif d.First() != \"b\" {\n\t\tt.Errorf(\"Expected 'b' (oldest after eviction), got %v\", d.First())\n\t}\n\tif d.Last() != \"d\" {\n\t\tt.Errorf(\"Expected 'd' (newest), got %v\", d.Last())\n\t}\n}\n\nfunc TestBoundedEvictionFront(t *testing.T) {\n\td := NewBounded(3)\n\n\t// Fill to capacity\n\td.PushBack(\"a\", \"b\", \"c\")\n\n\t// Push to front beyond capacity - should evict \"c\" from back\n\td.PushFront(\"x\")\n\tif d.Size() != 3 {\n\t\tt.Errorf(\"Expected size 3, got %d\", d.Size())\n\t}\n\tif d.First() != \"x\" {\n\t\tt.Errorf(\"Expected 'x' (newest at front), got %v\", d.First())\n\t}\n\tif d.Last() != \"b\" {\n\t\tt.Errorf(\"Expected 'b' (oldest after back eviction), got %v\", d.Last())\n\t}\n}\n\nfunc TestFirstLast(t *testing.T) {\n\td := New()\n\n\t// Empty deque\n\tif d.First() != nil {\n\t\tt.Error(\"Expected nil for empty deque first\")\n\t}\n\tif d.Last() != nil {\n\t\tt.Error(\"Expected nil for empty deque last\")\n\t}\n\n\td.PushBack(\"x\", \"y\", \"z\")\n\tif d.First() != \"x\" {\n\t\tt.Errorf(\"Expected 'x', got %v\", d.First())\n\t}\n\tif d.Last() != \"z\" {\n\t\tt.Errorf(\"Expected 'z', got %v\", d.Last())\n\t}\n}\n\nfunc TestGet(t *testing.T) {\n\td := New()\n\td.PushBack(\"a\", \"b\", \"c\")\n\n\tif d.Get(0) != \"a\" {\n\t\tt.Errorf(\"Expected 'a' at index 0, got %v\", d.Get(0))\n\t}\n\tif d.Get(1) != \"b\" {\n\t\tt.Errorf(\"Expected 'b' at index 1, got %v\", d.Get(1))\n\t}\n\tif d.Get(2) != \"c\" {\n\t\tt.Errorf(\"Expected 'c' at index 2, got %v\", d.Get(2))\n\t}\n\tif d.Get(3) != nil {\n\t\tt.Error(\"Expected nil for out-of-bounds index\")\n\t}\n\tif d.Get(-1) != nil {\n\t\tt.Error(\"Expected nil for negative index\")\n\t}\n}\n\nfunc TestList(t *testing.T) {\n\td := New()\n\n\t// Empty deque\n\tlist := d.List()\n\tif list != nil {\n\t\tt.Errorf(\"Expected nil for empty deque, got %v\", list)\n\t}\n\n\td.PushBack(\"x\", \"y\", \"z\")\n\tlist = d.List()\n\tif len(list) != 3 {\n\t\tt.Errorf(\"Expected length 3, got %d\", len(list))\n\t}\n\tif list[0] != \"x\" || list[1] != \"y\" || list[2] != \"z\" {\n\t\tt.Errorf(\"Expected [x, y, z], got %v\", list)\n\t}\n}\n\nfunc TestEnumerate(t *testing.T) {\n\td := New()\n\td.PushBack(\"a\", \"b\", \"c\")\n\n\tvar enumerated []string\n\tvar indices []int\n\n\td.Enumerate(func(index int, item any) bool {\n\t\tindices = append(indices, index)\n\t\tenumerated = append(enumerated, item.(string))\n\t\treturn false // continue\n\t})\n\n\tif len(enumerated) != 3 {\n\t\tt.Errorf(\"Expected 3 enumerated items, got %d\", len(enumerated))\n\t}\n\tif enumerated[0] != \"a\" || enumerated[1] != \"b\" || enumerated[2] != \"c\" {\n\t\tt.Errorf(\"Expected [a, b, c], got %v\", enumerated)\n\t}\n\tif indices[0] != 0 || indices[1] != 1 || indices[2] != 2 {\n\t\tt.Errorf(\"Expected [0, 1, 2], got %v\", indices)\n\t}\n}\n\nfunc TestEnumerateEarlyStop(t *testing.T) {\n\td := New()\n\td.PushBack(\"a\", \"b\", \"c\")\n\n\tvar enumerated []string\n\n\td.Enumerate(func(index int, item any) bool {\n\t\tenumerated = append(enumerated, item.(string))\n\t\treturn item == \"b\" // stop after b\n\t})\n\n\tif len(enumerated) != 2 {\n\t\tt.Errorf(\"Expected 2 enumerated items, got %d\", len(enumerated))\n\t}\n\tif enumerated[0] != \"a\" || enumerated[1] != \"b\" {\n\t\tt.Errorf(\"Expected [a, b], got %v\", enumerated)\n\t}\n}\n\nfunc TestSetMaxSize(t *testing.T) {\n\td := New()\n\td.PushBack(\"a\", \"b\", \"c\", \"d\", \"e\")\n\n\t// Reduce max size - should trim from front\n\td.SetMaxSize(3)\n\tif d.Size() != 3 {\n\t\tt.Errorf(\"Expected size 3 after reducing max size, got %d\", d.Size())\n\t}\n\tif d.First() != \"c\" {\n\t\tt.Errorf(\"Expected 'c' as first after trim, got %v\", d.First())\n\t}\n\n\t// Increase max size\n\td.SetMaxSize(10)\n\tif d.MaxSize() != 10 {\n\t\tt.Errorf(\"Expected max size 10, got %d\", d.MaxSize())\n\t}\n}\n\nfunc TestClear(t *testing.T) {\n\td := New()\n\td.PushBack(\"a\", \"b\", \"c\")\n\n\td.Clear()\n\n\tif d.Size() != 0 {\n\t\tt.Errorf(\"Expected size 0 after clear, got %d\", d.Size())\n\t}\n\tif d.First() != nil {\n\t\tt.Error(\"Expected nil first after clear\")\n\t}\n\tif d.Last() != nil {\n\t\tt.Error(\"Expected nil last after clear\")\n\t}\n\tif !d.IsEmpty() {\n\t\tt.Error(\"Expected deque to be empty after clear\")\n\t}\n}\n\nfunc TestUnboundedGrowth(t *testing.T) {\n\td := New() // unbounded\n\n\t// Add many items\n\tfor i := 0; i \u003c 100; i++ {\n\t\td.PushBack(i)\n\t}\n\n\tif d.Size() != 100 {\n\t\tt.Errorf(\"Expected size 100, got %d\", d.Size())\n\t}\n\tif d.First() != 0 {\n\t\tt.Errorf(\"Expected 0 as first, got %v\", d.First())\n\t}\n\tif d.Last() != 99 {\n\t\tt.Errorf(\"Expected 99 as last, got %v\", d.Last())\n\t}\n}\n\nfunc TestMixedOperations(t *testing.T) {\n\td := NewBounded(4)\n\n\t// Test mixed push front and back operations\n\td.PushBack(\"b\", \"c\")\n\td.PushFront(\"a\")\n\td.PushBack(\"d\")\n\n\t// Should have [a, b, c, d]\n\tlist := d.List()\n\tif len(list) != 4 || list[0] != \"a\" || list[1] != \"b\" || list[2] != \"c\" || list[3] != \"d\" {\n\t\tt.Errorf(\"Expected [a, b, c, d], got %v\", list)\n\t}\n\n\t// Push beyond capacity\n\td.PushFront(\"x\") // Should evict \"d\" from back\n\n\t// Should have [x, a, b, c]\n\tlist = d.List()\n\tif len(list) != 4 || list[0] != \"x\" || list[1] != \"a\" || list[2] != \"b\" || list[3] != \"c\" {\n\t\tt.Errorf(\"Expected [x, a, b, c], got %v\", list)\n\t}\n\n\t// Mixed pop operations\n\tfront := d.PopFront() // \"x\"\n\tback := d.PopBack()   // \"c\"\n\n\tif front != \"x\" {\n\t\tt.Errorf(\"Expected 'x' from front, got %v\", front)\n\t}\n\tif back != \"c\" {\n\t\tt.Errorf(\"Expected 'c' from back, got %v\", back)\n\t}\n\n\t// Should have [a, b]\n\tlist = d.List()\n\tif len(list) != 2 || list[0] != \"a\" || list[1] != \"b\" {\n\t\tt.Errorf(\"Expected [a, b], got %v\", list)\n\t}\n}\n\nfunc TestAllIterator(t *testing.T) {\n\td := New()\n\td.PushBack(\"x\", \"y\", \"z\")\n\n\tvar items []any\n\titer := d.All()\n\titer(func(value any) bool {\n\t\titems = append(items, value)\n\t\treturn true // continue\n\t})\n\n\tif len(items) != 3 {\n\t\tt.Errorf(\"Expected 3 items, got %d\", len(items))\n\t}\n\tif items[0] != \"x\" || items[1] != \"y\" || items[2] != \"z\" {\n\t\tt.Errorf(\"Expected [x, y, z], got %v\", items)\n\t}\n}\n\nfunc TestBackwardIterator(t *testing.T) {\n\td := New()\n\td.PushBack(\"x\", \"y\", \"z\")\n\n\tvar items []any\n\titer := d.Backward()\n\titer(func(value any) bool {\n\t\titems = append(items, value)\n\t\treturn true // continue\n\t})\n\n\tif len(items) != 3 {\n\t\tt.Errorf(\"Expected 3 items, got %d\", len(items))\n\t}\n\tif items[0] != \"z\" || items[1] != \"y\" || items[2] != \"x\" {\n\t\tt.Errorf(\"Expected [z, y, x] (reverse), got %v\", items)\n\t}\n}\n\nfunc TestAllIteratorEarlyBreak(t *testing.T) {\n\td := New()\n\td.PushBack(\"a\", \"b\", \"c\", \"d\", \"e\")\n\n\tvar items []any\n\titer := d.All()\n\titer(func(value any) bool {\n\t\titems = append(items, value)\n\t\tif value == \"c\" {\n\t\t\treturn false // stop\n\t\t}\n\t\treturn true // continue\n\t})\n\n\tif len(items) != 3 {\n\t\tt.Errorf(\"Expected 3 items after break, got %d\", len(items))\n\t}\n\tif items[0] != \"a\" || items[1] != \"b\" || items[2] != \"c\" {\n\t\tt.Errorf(\"Expected [a, b, c], got %v\", items)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/deque\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"xkAe5cmINUHgRGiCGHxMLtaojp2JvHNmK8+4q4v2tQEcEYhjsC4g2FC4EyAI446QBauFFG6hszkizLCF8p4hAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"errs","path":"gno.land/p/moul/errs","files":[{"name":"errs.gno","body":"// Package errs provides utilities for combining multiple errors.\n// This is a simplified version of https://github.com/uber-go/multierr (MIT License),\n// adapted for the Gno programming language with a focus on core functionality\n// and idiomatic usage patterns.\n//\n// Example usage:\n//\n//\terr1 := doSomething()\n//\terr2 := doSomethingElse()\n//\tif err := errs.Combine(err1, err2); err != nil {\n//\t    return err // Returns combined errors or single error\n//\t}\npackage errs\n\nimport (\n\t\"strings\"\n)\n\n// multiError represents multiple errors combined into one.\ntype multiError struct {\n\terrors []error\n}\n\n// Error implements the error interface by returning a single-line representation\n// of all contained errors, separated by semicolons.\nfunc (m *multiError) Error() string {\n\tif m == nil || len(m.errors) == 0 {\n\t\treturn \"\"\n\t}\n\n\tvar b strings.Builder\n\tfirst := true\n\tfor _, err := range m.errors {\n\t\tif first {\n\t\t\tfirst = false\n\t\t} else {\n\t\t\tb.WriteString(\"; \")\n\t\t}\n\t\tb.WriteString(err.Error())\n\t}\n\treturn b.String()\n}\n\n// String returns a multi-line representation of the error.\nfunc (m *multiError) String() string {\n\tif m == nil || len(m.errors) == 0 {\n\t\treturn \"\"\n\t}\n\n\tvar b strings.Builder\n\tb.WriteString(\"the following errors occurred:\")\n\tfor _, err := range m.errors {\n\t\tb.WriteString(\"\\n -  \")\n\t\tb.WriteString(err.Error())\n\t}\n\treturn b.String()\n}\n\n// Unwrap returns the slice of underlying errors contained in this multiError.\n// Returns nil if the receiver is nil.\nfunc (m *multiError) Unwrap() []error {\n\tif m == nil {\n\t\treturn nil\n\t}\n\treturn m.errors\n}\n\n// Errors extracts the underlying errors from an error interface.\n// If the error is a multiError, it returns its contained errors.\n// If the error is nil, returns nil.\n// If the error is a regular error, returns a slice containing just that error.\nfunc Errors(err error) []error {\n\tif err == nil {\n\t\treturn nil\n\t}\n\n\tif merr, ok := err.(*multiError); ok {\n\t\treturn merr.Unwrap()\n\t}\n\n\treturn []error{err}\n}\n\n// Combine merges multiple errors into a single error efficiently.\n// It handles several cases:\n//   - If all input errors are nil, returns nil\n//   - If there's exactly one non-nil error, returns that error directly\n//   - If there are multiple non-nil errors, returns a multiError containing them\n//   - Flattens any *multiError in the input\nfunc Combine(errs ...error) error {\n\tnonNil := make([]error, 0, len(errs))\n\tfor _, err := range errs {\n\t\tif err == nil {\n\t\t\tcontinue\n\t\t}\n\t\tif m, ok := err.(*multiError); ok \u0026\u0026 m != nil {\n\t\t\tnonNil = append(nonNil, m.Unwrap()...)\n\t\t} else {\n\t\t\tnonNil = append(nonNil, err)\n\t\t}\n\t}\n\n\tswitch len(nonNil) {\n\tcase 0:\n\t\treturn nil\n\tcase 1:\n\t\treturn nonNil[0]\n\tdefault:\n\t\treturn \u0026multiError{errors: nonNil}\n\t}\n}\n"},{"name":"errs_test.gno","body":"package errs\n\nimport (\n\t\"testing\"\n)\n\n// testError is a simple error implementation for testing\ntype testError struct {\n\tmsg string\n}\n\nfunc (e *testError) Error() string {\n\treturn e.msg\n}\n\nfunc newError(msg string) error {\n\treturn \u0026testError{msg: msg}\n}\n\nfunc TestCombine(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\terrors   []error\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"single error\",\n\t\t\terrors:   []error{newError(\"error1\"), nil},\n\t\t\texpected: \"error1\",\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple errors\",\n\t\t\terrors:   []error{newError(\"error1\"), newError(\"error2\"), newError(\"error3\")},\n\t\t\texpected: \"error1; error2; error3\",\n\t\t},\n\t\t{\n\t\t\tname:     \"mixed nil and non-nil\",\n\t\t\terrors:   []error{nil, newError(\"error1\"), nil, newError(\"error2\")},\n\t\t\texpected: \"error1; error2\",\n\t\t},\n\t\t{\n\t\t\tname:     \"both nil (2)\",\n\t\t\terrors:   []error{nil, nil},\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:     \"first nil (2)\",\n\t\t\terrors:   []error{nil, newError(\"error2\")},\n\t\t\texpected: \"error2\",\n\t\t},\n\t\t{\n\t\t\tname:     \"second nil (2)\",\n\t\t\terrors:   []error{newError(\"error1\"), nil},\n\t\t\texpected: \"error1\",\n\t\t},\n\t\t{\n\t\t\tname:     \"both non-nil (2)\",\n\t\t\terrors:   []error{newError(\"error1\"), newError(\"error2\")},\n\t\t\texpected: \"error1; error2\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\terr := Combine(tt.errors...)\n\t\tif tt.expected == \"\" {\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"%s: expected nil error, got %v\", tt.name, err)\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\n\t\tif err == nil {\n\t\t\tt.Errorf(\"%s: expected non-nil error\", tt.name)\n\t\t\tcontinue\n\t\t}\n\n\t\tif got := err.Error(); got != tt.expected {\n\t\t\tt.Errorf(\"%s: expected %q, got %q\", tt.name, tt.expected, got)\n\t\t}\n\t}\n}\n\nfunc TestErrors(t *testing.T) {\n\terr1 := newError(\"error1\")\n\terr2 := newError(\"error2\")\n\tcombined := Combine(err1, err2)\n\n\ttests := []struct {\n\t\tname          string\n\t\terr           error\n\t\texpectedCount int\n\t}{\n\t\t{\n\t\t\tname:          \"nil error\",\n\t\t\terr:           nil,\n\t\t\texpectedCount: 0,\n\t\t},\n\t\t{\n\t\t\tname:          \"single error\",\n\t\t\terr:           err1,\n\t\t\texpectedCount: 1,\n\t\t},\n\t\t{\n\t\t\tname:          \"multiple errors\",\n\t\t\terr:           combined,\n\t\t\texpectedCount: 2,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\terrs := Errors(tt.err)\n\t\tif len(errs) != tt.expectedCount {\n\t\t\tt.Errorf(\"%s: expected %d errors, got %d\", tt.name, tt.expectedCount, len(errs))\n\t\t}\n\t}\n}\n\nfunc TestMultiErrorString(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\terrors   []error\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"nil errors\",\n\t\t\terrors:   nil,\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:     \"empty errors\",\n\t\t\terrors:   []error{},\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:     \"single error\",\n\t\t\terrors:   []error{newError(\"error1\")},\n\t\t\texpected: \"the following errors occurred:\\n -  error1\",\n\t\t},\n\t\t{\n\t\t\tname:     \"multiple errors\",\n\t\t\terrors:   []error{newError(\"error1\"), newError(\"error2\")},\n\t\t\texpected: \"the following errors occurred:\\n -  error1\\n -  error2\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tmerr := \u0026multiError{errors: tt.errors}\n\t\t\tif got := merr.String(); got != tt.expected {\n\t\t\t\tt.Errorf(\"multiError.String() = %q, want %q\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestCombineEdgeCases(t *testing.T) {\n\terr1 := newError(\"error1\")\n\terr2 := newError(\"error2\")\n\terr3 := newError(\"error3\")\n\n\tmerr := Combine(err1, err2)\n\n\ttests := []struct {\n\t\tname     string\n\t\terrors   []error\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"append to multiError\",\n\t\t\terrors:   []error{merr, err3},\n\t\t\texpected: \"error1; error2; error3\",\n\t\t},\n\t\t{\n\t\t\tname:     \"prepend to multiError\",\n\t\t\terrors:   []error{err3, merr},\n\t\t\texpected: \"error3; error1; error2\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\terr := Combine(tt.errors...)\n\t\t\tif got := err.Error(); got != tt.expected {\n\t\t\t\tt.Errorf(\"Combine() = %q, want %q\", got, tt.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestUnwrapNilReceiver(t *testing.T) {\n\tvar merr *multiError\n\terrs := merr.Unwrap()\n\tif errs != nil {\n\t\tt.Errorf(\"Expected nil slice for nil receiver, got %v\", errs)\n\t}\n}\n\nfunc TestMultiErrorUnwrap(t *testing.T) {\n\tvar nilMulti *multiError\n\tif got := nilMulti.Unwrap(); got != nil {\n\t\tt.Errorf(\"Unwrap on nil receiver: expected nil, got %v\", got)\n\t}\n\n\tempty := \u0026multiError{errors: nil}\n\tif got := empty.Unwrap(); got != nil {\n\t\tt.Errorf(\"Unwrap on empty: expected nil, got %v\", got)\n\t}\n\n\tsingle := \u0026multiError{errors: []error{newError(\"foo\")}}\n\tgot := single.Unwrap()\n\tif len(got) != 1 || got[0].Error() != \"foo\" {\n\t\tt.Errorf(\"Unwrap on single: expected [foo], got %v\", got)\n\t}\n\n\tmulti := \u0026multiError{errors: []error{newError(\"foo\"), newError(\"bar\")}}\n\tgot = multi.Unwrap()\n\tif len(got) != 2 || got[0].Error() != \"foo\" || got[1].Error() != \"bar\" {\n\t\tt.Errorf(\"Unwrap on multi: expected [foo bar], got %v\", got)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/errs\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"LUdDDBLlQe8bDYKqxxfJvI016D5x4Op1EcnIzMcDkNfU0JKRYRgnfRJzNed1VJ0kVSdNr2C3u/P+NPVJevxtAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"fp","path":"gno.land/p/moul/fp","files":[{"name":"fp.gno","body":"// Package fp provides functional programming utilities for Gno, enabling\n// transformations, filtering, and other operations on slices of any.\n//\n// Example of chaining operations:\n//\n//\tnumbers := []any{1, 2, 3, 4, 5, 6}\n//\n//\t// Define predicates, mappers and reducers\n//\tisEven := func(v any) bool { return v.(int)%2 == 0 }\n//\tdouble := func(v any) any { return v.(int) * 2 }\n//\tsum := func(a, b any) any { return a.(int) + b.(int) }\n//\n//\t// Chain operations: filter even numbers, double them, then sum\n//\tevenNums := Filter(numbers, isEven)        // [2, 4, 6]\n//\tdoubled := Map(evenNums, double)           // [4, 8, 12]\n//\tresult := Reduce(doubled, sum, 0)          // 24\n//\n//\t// Alternative: group by even/odd, then get even numbers\n//\tbyMod2 := func(v any) any { return v.(int) % 2 }\n//\tgrouped := GroupBy(numbers, byMod2)        // {0: [2,4,6], 1: [1,3,5]}\n//\tevens := grouped[0]                        // [2,4,6]\npackage fp\n\n// Mapper is a function type that maps an element to another element.\ntype Mapper func(any) any\n\n// Predicate is a function type that evaluates a condition on an element.\ntype Predicate func(any) bool\n\n// Reducer is a function type that reduces two elements to a single value.\ntype Reducer func(any, any) any\n\n// Filter filters elements from the slice that satisfy the given predicate.\n//\n// Example:\n//\n//\tnumbers := []any{-1, 0, 1, 2}\n//\tisPositive := func(v any) bool { return v.(int) \u003e 0 }\n//\tresult := Filter(numbers, isPositive) // [1, 2]\nfunc Filter(values []any, fn Predicate) []any {\n\tresult := []any{}\n\tfor _, v := range values {\n\t\tif fn(v) {\n\t\t\tresult = append(result, v)\n\t\t}\n\t}\n\treturn result\n}\n\n// Map applies a function to each element in the slice.\n//\n// Example:\n//\n//\tnumbers := []any{1, 2, 3}\n//\ttoString := func(v any) any { return fmt.Sprintf(\"%d\", v) }\n//\tresult := Map(numbers, toString) // [\"1\", \"2\", \"3\"]\nfunc Map(values []any, fn Mapper) []any {\n\tresult := make([]any, len(values))\n\tfor i, v := range values {\n\t\tresult[i] = fn(v)\n\t}\n\treturn result\n}\n\n// Reduce reduces a slice to a single value by applying a function.\n//\n// Example:\n//\n//\tnumbers := []any{1, 2, 3, 4}\n//\tsum := func(a, b any) any { return a.(int) + b.(int) }\n//\tresult := Reduce(numbers, sum, 0) // 10\nfunc Reduce(values []any, fn Reducer, initial any) any {\n\tacc := initial\n\tfor _, v := range values {\n\t\tacc = fn(acc, v)\n\t}\n\treturn acc\n}\n\n// FlatMap maps each element to a collection and flattens the results.\n//\n// Example:\n//\n//\twords := []any{\"hello\", \"world\"}\n//\tsplit := func(v any) any {\n//\t    chars := []any{}\n//\t    for _, c := range v.(string) {\n//\t        chars = append(chars, string(c))\n//\t    }\n//\t    return chars\n//\t}\n//\tresult := FlatMap(words, split) // [\"h\",\"e\",\"l\",\"l\",\"o\",\"w\",\"o\",\"r\",\"l\",\"d\"]\nfunc FlatMap(values []any, fn Mapper) []any {\n\tresult := []any{}\n\tfor _, v := range values {\n\t\tinner := fn(v).([]any)\n\t\tresult = append(result, inner...)\n\t}\n\treturn result\n}\n\n// All returns true if all elements satisfy the predicate.\n//\n// Example:\n//\n//\tnumbers := []any{2, 4, 6, 8}\n//\tisEven := func(v any) bool { return v.(int)%2 == 0 }\n//\tresult := All(numbers, isEven) // true\nfunc All(values []any, fn Predicate) bool {\n\tfor _, v := range values {\n\t\tif !fn(v) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// Any returns true if at least one element satisfies the predicate.\n//\n// Example:\n//\n//\tnumbers := []any{1, 3, 4, 7}\n//\tisEven := func(v any) bool { return v.(int)%2 == 0 }\n//\tresult := Any(numbers, isEven) // true (4 is even)\nfunc Any(values []any, fn Predicate) bool {\n\tfor _, v := range values {\n\t\tif fn(v) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// None returns true if no elements satisfy the predicate.\n//\n// Example:\n//\n//\tnumbers := []any{1, 3, 5, 7}\n//\tisEven := func(v any) bool { return v.(int)%2 == 0 }\n//\tresult := None(numbers, isEven) // true (no even numbers)\nfunc None(values []any, fn Predicate) bool {\n\tfor _, v := range values {\n\t\tif fn(v) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// Chunk splits a slice into chunks of the given size.\n//\n// Example:\n//\n//\tnumbers := []any{1, 2, 3, 4, 5}\n//\tresult := Chunk(numbers, 2) // [[1,2], [3,4], [5]]\nfunc Chunk(values []any, size int) [][]any {\n\tif size \u003c= 0 {\n\t\treturn nil\n\t}\n\tvar chunks [][]any\n\tfor i := 0; i \u003c len(values); i += size {\n\t\tend := i + size\n\t\tif end \u003e len(values) {\n\t\t\tend = len(values)\n\t\t}\n\t\tchunks = append(chunks, values[i:end])\n\t}\n\treturn chunks\n}\n\n// Find returns the first element that satisfies the predicate and a boolean indicating if an element was found.\n//\n// Example:\n//\n//\tnumbers := []any{1, 2, 3, 4}\n//\tisEven := func(v any) bool { return v.(int)%2 == 0 }\n//\tresult, found := Find(numbers, isEven) // 2, true\nfunc Find(values []any, fn Predicate) (any, bool) {\n\tfor _, v := range values {\n\t\tif fn(v) {\n\t\t\treturn v, true\n\t\t}\n\t}\n\treturn nil, false\n}\n\n// Reverse reverses the order of elements in a slice.\n//\n// Example:\n//\n//\tnumbers := []any{1, 2, 3}\n//\tresult := Reverse(numbers) // [3, 2, 1]\nfunc Reverse(values []any) []any {\n\tresult := make([]any, len(values))\n\tfor i, v := range values {\n\t\tresult[len(values)-1-i] = v\n\t}\n\treturn result\n}\n\n// Zip combines two slices into a slice of pairs. If the slices have different lengths,\n// extra elements from the longer slice are ignored.\n//\n// Example:\n//\n//\ta := []any{1, 2, 3}\n//\tb := []any{\"a\", \"b\", \"c\"}\n//\tresult := Zip(a, b) // [[1,\"a\"], [2,\"b\"], [3,\"c\"]]\nfunc Zip(a, b []any) [][2]any {\n\tlength := min(len(a), len(b))\n\tresult := make([][2]any, length)\n\tfor i := 0; i \u003c length; i++ {\n\t\tresult[i] = [2]any{a[i], b[i]}\n\t}\n\treturn result\n}\n\n// Unzip splits a slice of pairs into two separate slices.\n//\n// Example:\n//\n//\tpairs := [][2]any{{1,\"a\"}, {2,\"b\"}, {3,\"c\"}}\n//\tnumbers, letters := Unzip(pairs) // [1,2,3], [\"a\",\"b\",\"c\"]\nfunc Unzip(pairs [][2]any) ([]any, []any) {\n\ta := make([]any, len(pairs))\n\tb := make([]any, len(pairs))\n\tfor i, pair := range pairs {\n\t\ta[i] = pair[0]\n\t\tb[i] = pair[1]\n\t}\n\treturn a, b\n}\n\n// GroupBy groups elements based on a key returned by a Mapper.\n//\n// Example:\n//\n//\tnumbers := []any{1, 2, 3, 4, 5, 6}\n//\tbyMod3 := func(v any) any { return v.(int) % 3 }\n//\tresult := GroupBy(numbers, byMod3) // {0: [3,6], 1: [1,4], 2: [2,5]}\nfunc GroupBy(values []any, fn Mapper) map[any][]any {\n\tresult := make(map[any][]any)\n\tfor _, v := range values {\n\t\tkey := fn(v)\n\t\tresult[key] = append(result[key], v)\n\t}\n\treturn result\n}\n\n// Flatten flattens a slice of slices into a single slice.\n//\n// Example:\n//\n//\tnested := [][]any{{1,2}, {3,4}, {5}}\n//\tresult := Flatten(nested) // [1,2,3,4,5]\nfunc Flatten(values [][]any) []any {\n\tresult := []any{}\n\tfor _, v := range values {\n\t\tresult = append(result, v...)\n\t}\n\treturn result\n}\n\n// Helper functions\nfunc min(a, b int) int {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n"},{"name":"fp_test.gno","body":"package fp\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n)\n\nfunc TestMap(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []any\n\t\tfn       func(any) any\n\t\texpected []any\n\t}{\n\t\t{\n\t\t\tname:     \"multiply numbers by 2\",\n\t\t\tinput:    []any{1, 2, 3},\n\t\t\tfn:       func(v any) any { return v.(int) * 2 },\n\t\t\texpected: []any{2, 4, 6},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tinput:    []any{},\n\t\t\tfn:       func(v any) any { return v.(int) * 2 },\n\t\t\texpected: []any{},\n\t\t},\n\t\t{\n\t\t\tname:     \"convert numbers to strings\",\n\t\t\tinput:    []any{1, 2, 3},\n\t\t\tfn:       func(v any) any { return fmt.Sprintf(\"%d\", v.(int)) },\n\t\t\texpected: []any{\"1\", \"2\", \"3\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := Map(tt.input, tt.fn)\n\t\t\tif !equalSlices(result, tt.expected) {\n\t\t\t\tt.Errorf(\"Map failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestFilter(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []any\n\t\tfn       func(any) bool\n\t\texpected []any\n\t}{\n\t\t{\n\t\t\tname:     \"filter even numbers\",\n\t\t\tinput:    []any{1, 2, 3, 4},\n\t\t\tfn:       func(v any) bool { return v.(int)%2 == 0 },\n\t\t\texpected: []any{2, 4},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tinput:    []any{},\n\t\t\tfn:       func(v any) bool { return v.(int)%2 == 0 },\n\t\t\texpected: []any{},\n\t\t},\n\t\t{\n\t\t\tname:     \"no matches\",\n\t\t\tinput:    []any{1, 3, 5},\n\t\t\tfn:       func(v any) bool { return v.(int)%2 == 0 },\n\t\t\texpected: []any{},\n\t\t},\n\t\t{\n\t\t\tname:     \"all matches\",\n\t\t\tinput:    []any{2, 4, 6},\n\t\t\tfn:       func(v any) bool { return v.(int)%2 == 0 },\n\t\t\texpected: []any{2, 4, 6},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := Filter(tt.input, tt.fn)\n\t\t\tif !equalSlices(result, tt.expected) {\n\t\t\t\tt.Errorf(\"Filter failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestReduce(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []any\n\t\tfn       func(any, any) any\n\t\tinitial  any\n\t\texpected any\n\t}{\n\t\t{\n\t\t\tname:     \"sum numbers\",\n\t\t\tinput:    []any{1, 2, 3},\n\t\t\tfn:       func(a, b any) any { return a.(int) + b.(int) },\n\t\t\tinitial:  0,\n\t\t\texpected: 6,\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tinput:    []any{},\n\t\t\tfn:       func(a, b any) any { return a.(int) + b.(int) },\n\t\t\tinitial:  0,\n\t\t\texpected: 0,\n\t\t},\n\t\t{\n\t\t\tname:     \"concatenate strings\",\n\t\t\tinput:    []any{\"a\", \"b\", \"c\"},\n\t\t\tfn:       func(a, b any) any { return a.(string) + b.(string) },\n\t\t\tinitial:  \"\",\n\t\t\texpected: \"abc\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := Reduce(tt.input, tt.fn, tt.initial)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"Reduce failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestFlatMap(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []any\n\t\tfn       func(any) any\n\t\texpected []any\n\t}{\n\t\t{\n\t\t\tname:  \"split words into chars\",\n\t\t\tinput: []any{\"go\", \"fn\"},\n\t\t\tfn: func(word any) any {\n\t\t\t\tchars := []any{}\n\t\t\t\tfor _, c := range word.(string) {\n\t\t\t\t\tchars = append(chars, string(c))\n\t\t\t\t}\n\t\t\t\treturn chars\n\t\t\t},\n\t\t\texpected: []any{\"g\", \"o\", \"f\", \"n\"},\n\t\t},\n\t\t{\n\t\t\tname:  \"empty string handling\",\n\t\t\tinput: []any{\"\", \"a\", \"\"},\n\t\t\tfn: func(word any) any {\n\t\t\t\tchars := []any{}\n\t\t\t\tfor _, c := range word.(string) {\n\t\t\t\t\tchars = append(chars, string(c))\n\t\t\t\t}\n\t\t\t\treturn chars\n\t\t\t},\n\t\t\texpected: []any{\"a\"},\n\t\t},\n\t\t{\n\t\t\tname:  \"nil handling\",\n\t\t\tinput: []any{nil, \"a\", nil},\n\t\t\tfn: func(word any) any {\n\t\t\t\tif word == nil {\n\t\t\t\t\treturn []any{}\n\t\t\t\t}\n\t\t\t\treturn []any{word}\n\t\t\t},\n\t\t\texpected: []any{\"a\"},\n\t\t},\n\t\t{\n\t\t\tname:  \"empty slice result\",\n\t\t\tinput: []any{\"\", \"\", \"\"},\n\t\t\tfn: func(word any) any {\n\t\t\t\treturn []any{}\n\t\t\t},\n\t\t\texpected: []any{},\n\t\t},\n\t\t{\n\t\t\tname:  \"nested array flattening\",\n\t\t\tinput: []any{1, 2, 3},\n\t\t\tfn: func(n any) any {\n\t\t\t\treturn []any{n, n}\n\t\t\t},\n\t\t\texpected: []any{1, 1, 2, 2, 3, 3},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := FlatMap(tt.input, tt.fn)\n\t\t\tif !equalSlices(result, tt.expected) {\n\t\t\t\tt.Errorf(\"FlatMap failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestAllAnyNone(t *testing.T) {\n\ttests := []struct {\n\t\tname         string\n\t\tinput        []any\n\t\tfn           func(any) bool\n\t\texpectedAll  bool\n\t\texpectedAny  bool\n\t\texpectedNone bool\n\t}{\n\t\t{\n\t\t\tname:         \"all even numbers\",\n\t\t\tinput:        []any{2, 4, 6, 8},\n\t\t\tfn:           func(x any) bool { return x.(int)%2 == 0 },\n\t\t\texpectedAll:  true,\n\t\t\texpectedAny:  true,\n\t\t\texpectedNone: false,\n\t\t},\n\t\t{\n\t\t\tname:         \"no even numbers\",\n\t\t\tinput:        []any{1, 3, 5, 7},\n\t\t\tfn:           func(x any) bool { return x.(int)%2 == 0 },\n\t\t\texpectedAll:  false,\n\t\t\texpectedAny:  false,\n\t\t\texpectedNone: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"mixed even/odd numbers\",\n\t\t\tinput:        []any{1, 2, 3, 4},\n\t\t\tfn:           func(x any) bool { return x.(int)%2 == 0 },\n\t\t\texpectedAll:  false,\n\t\t\texpectedAny:  true,\n\t\t\texpectedNone: false,\n\t\t},\n\t\t{\n\t\t\tname:         \"empty slice\",\n\t\t\tinput:        []any{},\n\t\t\tfn:           func(x any) bool { return x.(int)%2 == 0 },\n\t\t\texpectedAll:  true,  // vacuously true\n\t\t\texpectedAny:  false, // vacuously false\n\t\t\texpectedNone: true,  // vacuously true\n\t\t},\n\t\t{\n\t\t\tname:         \"nil predicate handling\",\n\t\t\tinput:        []any{nil, nil, nil},\n\t\t\tfn:           func(x any) bool { return x == nil },\n\t\t\texpectedAll:  true,\n\t\t\texpectedAny:  true,\n\t\t\texpectedNone: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresultAll := All(tt.input, tt.fn)\n\t\t\tif resultAll != tt.expectedAll {\n\t\t\t\tt.Errorf(\"All failed, expected %v, got %v\", tt.expectedAll, resultAll)\n\t\t\t}\n\n\t\t\tresultAny := Any(tt.input, tt.fn)\n\t\t\tif resultAny != tt.expectedAny {\n\t\t\t\tt.Errorf(\"Any failed, expected %v, got %v\", tt.expectedAny, resultAny)\n\t\t\t}\n\n\t\t\tresultNone := None(tt.input, tt.fn)\n\t\t\tif resultNone != tt.expectedNone {\n\t\t\t\tt.Errorf(\"None failed, expected %v, got %v\", tt.expectedNone, resultNone)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestChunk(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []any\n\t\tsize     int\n\t\texpected [][]any\n\t}{\n\t\t{\n\t\t\tname:     \"normal chunks\",\n\t\t\tinput:    []any{1, 2, 3, 4, 5},\n\t\t\tsize:     2,\n\t\t\texpected: [][]any{{1, 2}, {3, 4}, {5}},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tinput:    []any{},\n\t\t\tsize:     2,\n\t\t\texpected: [][]any{},\n\t\t},\n\t\t{\n\t\t\tname:     \"chunk size equals length\",\n\t\t\tinput:    []any{1, 2, 3},\n\t\t\tsize:     3,\n\t\t\texpected: [][]any{{1, 2, 3}},\n\t\t},\n\t\t{\n\t\t\tname:     \"chunk size larger than length\",\n\t\t\tinput:    []any{1, 2},\n\t\t\tsize:     3,\n\t\t\texpected: [][]any{{1, 2}},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := Chunk(tt.input, tt.size)\n\t\t\tif !equalNestedSlices(result, tt.expected) {\n\t\t\t\tt.Errorf(\"Chunk failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestFind(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tinput       []any\n\t\tfn          func(any) bool\n\t\texpected    any\n\t\tshouldFound bool\n\t}{\n\t\t{\n\t\t\tname:        \"find first number greater than 2\",\n\t\t\tinput:       []any{1, 2, 3, 4},\n\t\t\tfn:          func(v any) bool { return v.(int) \u003e 2 },\n\t\t\texpected:    3,\n\t\t\tshouldFound: true,\n\t\t},\n\t\t{\n\t\t\tname:        \"empty slice\",\n\t\t\tinput:       []any{},\n\t\t\tfn:          func(v any) bool { return v.(int) \u003e 2 },\n\t\t\texpected:    nil,\n\t\t\tshouldFound: false,\n\t\t},\n\t\t{\n\t\t\tname:        \"no match\",\n\t\t\tinput:       []any{1, 2},\n\t\t\tfn:          func(v any) bool { return v.(int) \u003e 10 },\n\t\t\texpected:    nil,\n\t\t\tshouldFound: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult, found := Find(tt.input, tt.fn)\n\t\t\tif found != tt.shouldFound {\n\t\t\t\tt.Errorf(\"Find failed, expected found=%v, got found=%v\", tt.shouldFound, found)\n\t\t\t}\n\t\t\tif found \u0026\u0026 result != tt.expected {\n\t\t\t\tt.Errorf(\"Find failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestReverse(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []any\n\t\texpected []any\n\t}{\n\t\t{\n\t\t\tname:     \"normal sequence\",\n\t\t\tinput:    []any{1, 2, 3, 4},\n\t\t\texpected: []any{4, 3, 2, 1},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tinput:    []any{},\n\t\t\texpected: []any{},\n\t\t},\n\t\t{\n\t\t\tname:     \"single element\",\n\t\t\tinput:    []any{1},\n\t\t\texpected: []any{1},\n\t\t},\n\t\t{\n\t\t\tname:     \"mixed types\",\n\t\t\tinput:    []any{1, \"a\", true, 2.5},\n\t\t\texpected: []any{2.5, true, \"a\", 1},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := Reverse(tt.input)\n\t\t\tif !equalSlices(result, tt.expected) {\n\t\t\t\tt.Errorf(\"Reverse failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestZipUnzip(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\ta           []any\n\t\tb           []any\n\t\texpectedZip [][2]any\n\t\texpectedA   []any\n\t\texpectedB   []any\n\t}{\n\t\t{\n\t\t\tname:        \"normal case\",\n\t\t\ta:           []any{1, 2, 3},\n\t\t\tb:           []any{\"a\", \"b\", \"c\"},\n\t\t\texpectedZip: [][2]any{{1, \"a\"}, {2, \"b\"}, {3, \"c\"}},\n\t\t\texpectedA:   []any{1, 2, 3},\n\t\t\texpectedB:   []any{\"a\", \"b\", \"c\"},\n\t\t},\n\t\t{\n\t\t\tname:        \"empty slices\",\n\t\t\ta:           []any{},\n\t\t\tb:           []any{},\n\t\t\texpectedZip: [][2]any{},\n\t\t\texpectedA:   []any{},\n\t\t\texpectedB:   []any{},\n\t\t},\n\t\t{\n\t\t\tname:        \"different lengths - a shorter\",\n\t\t\ta:           []any{1, 2},\n\t\t\tb:           []any{\"a\", \"b\", \"c\"},\n\t\t\texpectedZip: [][2]any{{1, \"a\"}, {2, \"b\"}},\n\t\t\texpectedA:   []any{1, 2},\n\t\t\texpectedB:   []any{\"a\", \"b\"},\n\t\t},\n\t\t{\n\t\t\tname:        \"different lengths - b shorter\",\n\t\t\ta:           []any{1, 2, 3},\n\t\t\tb:           []any{\"a\"},\n\t\t\texpectedZip: [][2]any{{1, \"a\"}},\n\t\t\texpectedA:   []any{1},\n\t\t\texpectedB:   []any{\"a\"},\n\t\t},\n\t\t{\n\t\t\tname:        \"mixed types\",\n\t\t\ta:           []any{1, true, \"x\"},\n\t\t\tb:           []any{2.5, false, \"y\"},\n\t\t\texpectedZip: [][2]any{{1, 2.5}, {true, false}, {\"x\", \"y\"}},\n\t\t\texpectedA:   []any{1, true, \"x\"},\n\t\t\texpectedB:   []any{2.5, false, \"y\"},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tzipped := Zip(tt.a, tt.b)\n\t\t\tif len(zipped) != len(tt.expectedZip) {\n\t\t\t\tt.Errorf(\"Zip failed, expected length %v, got %v\", len(tt.expectedZip), len(zipped))\n\t\t\t}\n\t\t\tfor i, pair := range zipped {\n\t\t\t\tif pair[0] != tt.expectedZip[i][0] || pair[1] != tt.expectedZip[i][1] {\n\t\t\t\t\tt.Errorf(\"Zip failed at index %d, expected %v, got %v\", i, tt.expectedZip[i], pair)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tunzippedA, unzippedB := Unzip(zipped)\n\t\t\tif !equalSlices(unzippedA, tt.expectedA) {\n\t\t\t\tt.Errorf(\"Unzip failed for slice A, expected %v, got %v\", tt.expectedA, unzippedA)\n\t\t\t}\n\t\t\tif !equalSlices(unzippedB, tt.expectedB) {\n\t\t\t\tt.Errorf(\"Unzip failed for slice B, expected %v, got %v\", tt.expectedB, unzippedB)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGroupBy(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    []any\n\t\tfn       func(any) any\n\t\texpected map[any][]any\n\t}{\n\t\t{\n\t\t\tname:  \"group by even/odd\",\n\t\t\tinput: []any{1, 2, 3, 4, 5, 6},\n\t\t\tfn:    func(v any) any { return v.(int) % 2 },\n\t\t\texpected: map[any][]any{\n\t\t\t\t0: {2, 4, 6},\n\t\t\t\t1: {1, 3, 5},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tinput:    []any{},\n\t\t\tfn:       func(v any) any { return v.(int) % 2 },\n\t\t\texpected: map[any][]any{},\n\t\t},\n\t\t{\n\t\t\tname:  \"single group\",\n\t\t\tinput: []any{2, 4, 6},\n\t\t\tfn:    func(v any) any { return v.(int) % 2 },\n\t\t\texpected: map[any][]any{\n\t\t\t\t0: {2, 4, 6},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:  \"group by type\",\n\t\t\tinput: []any{1, \"a\", 2, \"b\", true},\n\t\t\tfn: func(v any) any {\n\t\t\t\tswitch v.(type) {\n\t\t\t\tcase int:\n\t\t\t\t\treturn \"int\"\n\t\t\t\tcase string:\n\t\t\t\t\treturn \"string\"\n\t\t\t\tdefault:\n\t\t\t\t\treturn \"other\"\n\t\t\t\t}\n\t\t\t},\n\t\t\texpected: map[any][]any{\n\t\t\t\t\"int\":    {1, 2},\n\t\t\t\t\"string\": {\"a\", \"b\"},\n\t\t\t\t\"other\":  {true},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := GroupBy(tt.input, tt.fn)\n\t\t\tif len(result) != len(tt.expected) {\n\t\t\t\tt.Errorf(\"GroupBy failed, expected %d groups, got %d\", len(tt.expected), len(result))\n\t\t\t}\n\t\t\tfor k, v := range tt.expected {\n\t\t\t\tif !equalSlices(result[k], v) {\n\t\t\t\t\tt.Errorf(\"GroupBy failed for key %v, expected %v, got %v\", k, v, result[k])\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestFlatten(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tinput    [][]any\n\t\texpected []any\n\t}{\n\t\t{\n\t\t\tname:     \"normal nested slices\",\n\t\t\tinput:    [][]any{{1, 2}, {3, 4}, {5}},\n\t\t\texpected: []any{1, 2, 3, 4, 5},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty outer slice\",\n\t\t\tinput:    [][]any{},\n\t\t\texpected: []any{},\n\t\t},\n\t\t{\n\t\t\tname:     \"empty inner slices\",\n\t\t\tinput:    [][]any{{}, {}, {}},\n\t\t\texpected: []any{},\n\t\t},\n\t\t{\n\t\t\tname:     \"mixed types\",\n\t\t\tinput:    [][]any{{1, \"a\"}, {true, 2.5}, {nil}},\n\t\t\texpected: []any{1, \"a\", true, 2.5, nil},\n\t\t},\n\t\t{\n\t\t\tname:     \"single element slices\",\n\t\t\tinput:    [][]any{{1}, {2}, {3}},\n\t\t\texpected: []any{1, 2, 3},\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := Flatten(tt.input)\n\t\t\tif !equalSlices(result, tt.expected) {\n\t\t\t\tt.Errorf(\"Flatten failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestContains(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tslice    []any\n\t\titem     any\n\t\texpected bool\n\t}{\n\t\t{\n\t\t\tname:     \"contains integer\",\n\t\t\tslice:    []any{1, 2, 3},\n\t\t\titem:     2,\n\t\t\texpected: true,\n\t\t},\n\t\t{\n\t\t\tname:     \"does not contain integer\",\n\t\t\tslice:    []any{1, 2, 3},\n\t\t\titem:     4,\n\t\t\texpected: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"contains string\",\n\t\t\tslice:    []any{\"a\", \"b\", \"c\"},\n\t\t\titem:     \"b\",\n\t\t\texpected: true,\n\t\t},\n\t\t{\n\t\t\tname:     \"empty slice\",\n\t\t\tslice:    []any{},\n\t\t\titem:     1,\n\t\t\texpected: false,\n\t\t},\n\t\t{\n\t\t\tname:     \"contains nil\",\n\t\t\tslice:    []any{1, nil, 3},\n\t\t\titem:     nil,\n\t\t\texpected: true,\n\t\t},\n\t\t{\n\t\t\tname:     \"mixed types\",\n\t\t\tslice:    []any{1, \"a\", true},\n\t\t\titem:     true,\n\t\t\texpected: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tresult := contains(tt.slice, tt.item)\n\t\t\tif result != tt.expected {\n\t\t\t\tt.Errorf(\"contains failed, expected %v, got %v\", tt.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\n// Helper function for testing\nfunc contains(slice []any, item any) bool {\n\tfor _, v := range slice {\n\t\tif v == item {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// Helper functions for comparing slices\nfunc equalSlices(a, b []any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc equalNestedSlices(a, b [][]any) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif !equalSlices(a[i], b[i]) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/fp\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"6UK5fVGuoO5vYNuiRqys0TgK68acSqU5GkQapynpdEjXFjx9CVjNvqwEoFebNvfhELQLP+YwK2Bh+glBQq5tAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"memo","path":"gno.land/p/moul/memo","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/memo\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"memo.gno","body":"// Package memo provides a simple memoization utility to cache function results.\n//\n// The package offers a Memoizer type that can cache function results based on keys,\n// with optional validation of cached values. This is useful for expensive computations\n// that need to be cached and potentially invalidated based on custom conditions.\n//\n// /!\\ Important Warning for Gno Usage:\n// In Gno, storage updates only persist during transactions. This means:\n//   - Cache entries created during queries will NOT persist\n//   - Creating cache entries during queries will actually decrease performance\n//     as it wastes resources trying to save data that won't be saved\n//\n// Best Practices:\n// - Use this pattern in transaction-driven contexts rather than query/render scenarios\n// - Consider controlled cache updates, e.g., by specific accounts (like oracles)\n// - Ideal for cases where cache updates happen every N blocks or on specific events\n// - Carefully evaluate if caching will actually improve performance in your use case\n//\n// Basic usage example:\n//\n//\tm := memo.New()\n//\n//\t// Cache expensive computation\n//\tresult := m.Memoize(\"key\", func() any {\n//\t    // expensive operation\n//\t    return \"computed-value\"\n//\t})\n//\n//\t// Subsequent calls with same key return cached result\n//\tresult = m.Memoize(\"key\", func() any {\n//\t    // function won't be called, cached value is returned\n//\t    return \"computed-value\"\n//\t})\n//\n// Example with validation:\n//\n//\ttype TimestampedValue struct {\n//\t    Value     string\n//\t    Timestamp time.Time\n//\t}\n//\n//\tm := memo.New()\n//\n//\t// Cache value with timestamp\n//\tresult := m.MemoizeWithValidator(\n//\t    \"key\",\n//\t    func() any {\n//\t        return TimestampedValue{\n//\t            Value:     \"data\",\n//\t            Timestamp: time.Now(),\n//\t        }\n//\t    },\n//\t    func(cached any) bool {\n//\t        // Validate that the cached value is not older than 1 hour\n//\t        if tv, ok := cached.(TimestampedValue); ok {\n//\t            return time.Since(tv.Timestamp) \u003c time.Hour\n//\t        }\n//\t        return false\n//\t    },\n//\t)\npackage memo\n\nimport (\n\t\"gno.land/p/demo/btree\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Record implements the btree.Record interface for our cache entries\ntype cacheEntry struct {\n\tkey   any\n\tvalue any\n}\n\n// Less implements btree.Record interface\nfunc (e cacheEntry) Less(than btree.Record) bool {\n\t// Convert the other record to cacheEntry\n\tother := than.(cacheEntry)\n\t// Compare string representations of keys for consistent ordering\n\treturn ufmt.Sprintf(\"%v\", e.key) \u003c ufmt.Sprintf(\"%v\", other.key)\n}\n\n// Memoizer is a structure to handle memoization of function results.\ntype Memoizer struct {\n\tcache *btree.BTree\n}\n\n// New creates a new Memoizer instance.\nfunc New() *Memoizer {\n\treturn \u0026Memoizer{\n\t\tcache: btree.New(),\n\t}\n}\n\n// Memoize ensures the result of the given function is cached for the specified key.\nfunc (m *Memoizer) Memoize(key any, fn func() any) any {\n\tentry := cacheEntry{key: key}\n\tif found := m.cache.Get(entry); found != nil {\n\t\treturn found.(cacheEntry).value\n\t}\n\n\tvalue := fn()\n\tm.cache.Insert(cacheEntry{key: key, value: value})\n\treturn value\n}\n\n// MemoizeWithValidator ensures the result is cached and valid according to the validator function.\nfunc (m *Memoizer) MemoizeWithValidator(key any, fn func() any, isValid func(any) bool) any {\n\tentry := cacheEntry{key: key}\n\tif found := m.cache.Get(entry); found != nil {\n\t\tcachedEntry := found.(cacheEntry)\n\t\tif isValid(cachedEntry.value) {\n\t\t\treturn cachedEntry.value\n\t\t}\n\t}\n\n\tvalue := fn()\n\tm.cache.Insert(cacheEntry{key: key, value: value})\n\treturn value\n}\n\n// Invalidate removes the cached value for the specified key.\nfunc (m *Memoizer) Invalidate(key any) {\n\tm.cache.Delete(cacheEntry{key: key})\n}\n\n// Clear clears all cached values.\nfunc (m *Memoizer) Clear() {\n\tm.cache.Clear(true)\n}\n\n// Size returns the number of items currently in the cache.\nfunc (m *Memoizer) Size() int {\n\treturn m.cache.Len()\n}\n"},{"name":"memo_test.gno","body":"package memo\n\nimport (\n\t\"testing\"\n\t\"time\"\n)\n\ntype timestampedValue struct {\n\tvalue     any\n\ttimestamp time.Time\n}\n\n// complexKey is used to test struct keys\ntype complexKey struct {\n\tID   int\n\tName string\n}\n\nfunc TestMemoize(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tkey       any\n\t\tvalue     any\n\t\tcallCount *int\n\t}{\n\t\t{\n\t\t\tname:      \"string key and value\",\n\t\t\tkey:       \"test-key\",\n\t\t\tvalue:     \"test-value\",\n\t\t\tcallCount: new(int),\n\t\t},\n\t\t{\n\t\t\tname:      \"int key and value\",\n\t\t\tkey:       42,\n\t\t\tvalue:     123,\n\t\t\tcallCount: new(int),\n\t\t},\n\t\t{\n\t\t\tname:      \"mixed types\",\n\t\t\tkey:       \"number\",\n\t\t\tvalue:     42,\n\t\t\tcallCount: new(int),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tm := New()\n\t\t\tif m.Size() != 0 {\n\t\t\t\tt.Errorf(\"Initial size = %d, want 0\", m.Size())\n\t\t\t}\n\n\t\t\tfn := func() any {\n\t\t\t\t*tt.callCount++\n\t\t\t\treturn tt.value\n\t\t\t}\n\n\t\t\t// First call should compute\n\t\t\tresult := m.Memoize(tt.key, fn)\n\t\t\tif result != tt.value {\n\t\t\t\tt.Errorf(\"Memoize() = %v, want %v\", result, tt.value)\n\t\t\t}\n\t\t\tif *tt.callCount != 1 {\n\t\t\t\tt.Errorf(\"Function called %d times, want 1\", *tt.callCount)\n\t\t\t}\n\t\t\tif m.Size() != 1 {\n\t\t\t\tt.Errorf(\"Size after first call = %d, want 1\", m.Size())\n\t\t\t}\n\n\t\t\t// Second call should use cache\n\t\t\tresult = m.Memoize(tt.key, fn)\n\t\t\tif result != tt.value {\n\t\t\t\tt.Errorf(\"Memoize() second call = %v, want %v\", result, tt.value)\n\t\t\t}\n\t\t\tif *tt.callCount != 1 {\n\t\t\t\tt.Errorf(\"Function called %d times, want 1\", *tt.callCount)\n\t\t\t}\n\t\t\tif m.Size() != 1 {\n\t\t\t\tt.Errorf(\"Size after second call = %d, want 1\", m.Size())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMemoizeWithValidator(t *testing.T) {\n\ttests := []struct {\n\t\tname            string\n\t\tkey             any\n\t\tvalue           any\n\t\tvalidDuration   time.Duration\n\t\twaitDuration    time.Duration\n\t\texpectedCalls   int\n\t\tshouldRecompute bool\n\t}{\n\t\t{\n\t\t\tname:            \"valid cache\",\n\t\t\tkey:             \"key1\",\n\t\t\tvalue:           \"value1\",\n\t\t\tvalidDuration:   time.Hour,\n\t\t\twaitDuration:    time.Millisecond,\n\t\t\texpectedCalls:   1,\n\t\t\tshouldRecompute: false,\n\t\t},\n\t\t{\n\t\t\tname:            \"expired cache\",\n\t\t\tkey:             \"key2\",\n\t\t\tvalue:           \"value2\",\n\t\t\tvalidDuration:   time.Millisecond,\n\t\t\twaitDuration:    time.Millisecond * 2,\n\t\t\texpectedCalls:   2,\n\t\t\tshouldRecompute: true,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tm := New()\n\t\t\tcallCount := 0\n\n\t\t\tfn := func() any {\n\t\t\t\tcallCount++\n\t\t\t\treturn timestampedValue{\n\t\t\t\t\tvalue:     tt.value,\n\t\t\t\t\ttimestamp: time.Now(),\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tisValid := func(cached any) bool {\n\t\t\t\tif tv, ok := cached.(timestampedValue); ok {\n\t\t\t\t\treturn time.Since(tv.timestamp) \u003c tt.validDuration\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t}\n\n\t\t\t// First call\n\t\t\tresult := m.MemoizeWithValidator(tt.key, fn, isValid)\n\t\t\tif tv, ok := result.(timestampedValue); !ok || tv.value != tt.value {\n\t\t\t\tt.Errorf(\"MemoizeWithValidator() = %v, want value %v\", result, tt.value)\n\t\t\t}\n\n\t\t\t// Wait\n\t\t\ttesting.SkipHeights(10)\n\n\t\t\t// Second call\n\t\t\tresult = m.MemoizeWithValidator(tt.key, fn, isValid)\n\t\t\tif tv, ok := result.(timestampedValue); !ok || tv.value != tt.value {\n\t\t\t\tt.Errorf(\"MemoizeWithValidator() second call = %v, want value %v\", result, tt.value)\n\t\t\t}\n\n\t\t\tif callCount != tt.expectedCalls {\n\t\t\t\tt.Errorf(\"Function called %d times, want %d\", callCount, tt.expectedCalls)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestInvalidate(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tkey       any\n\t\tvalue     any\n\t\tcallCount *int\n\t}{\n\t\t{\n\t\t\tname:      \"invalidate existing key\",\n\t\t\tkey:       \"test-key\",\n\t\t\tvalue:     \"test-value\",\n\t\t\tcallCount: new(int),\n\t\t},\n\t\t{\n\t\t\tname:      \"invalidate non-existing key\",\n\t\t\tkey:       \"missing-key\",\n\t\t\tvalue:     \"test-value\",\n\t\t\tcallCount: new(int),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tm := New()\n\t\t\tfn := func() any {\n\t\t\t\t*tt.callCount++\n\t\t\t\treturn tt.value\n\t\t\t}\n\n\t\t\t// First call\n\t\t\tm.Memoize(tt.key, fn)\n\t\t\tif m.Size() != 1 {\n\t\t\t\tt.Errorf(\"Size after first call = %d, want 1\", m.Size())\n\t\t\t}\n\n\t\t\t// Invalidate\n\t\t\tm.Invalidate(tt.key)\n\t\t\tif m.Size() != 0 {\n\t\t\t\tt.Errorf(\"Size after invalidate = %d, want 0\", m.Size())\n\t\t\t}\n\n\t\t\t// Call again should recompute\n\t\t\tresult := m.Memoize(tt.key, fn)\n\t\t\tif result != tt.value {\n\t\t\t\tt.Errorf(\"Memoize() after invalidate = %v, want %v\", result, tt.value)\n\t\t\t}\n\t\t\tif *tt.callCount != 2 {\n\t\t\t\tt.Errorf(\"Function called %d times, want 2\", *tt.callCount)\n\t\t\t}\n\t\t\tif m.Size() != 1 {\n\t\t\t\tt.Errorf(\"Size after recompute = %d, want 1\", m.Size())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestClear(t *testing.T) {\n\tm := New()\n\tcallCount := 0\n\n\tfn := func() any {\n\t\tcallCount++\n\t\treturn \"value\"\n\t}\n\n\t// Cache some values\n\tm.Memoize(\"key1\", fn)\n\tm.Memoize(\"key2\", fn)\n\n\tif callCount != 2 {\n\t\tt.Errorf(\"Initial calls = %d, want 2\", callCount)\n\t}\n\tif m.Size() != 2 {\n\t\tt.Errorf(\"Size after initial calls = %d, want 2\", m.Size())\n\t}\n\n\t// Clear cache\n\tm.Clear()\n\tif m.Size() != 0 {\n\t\tt.Errorf(\"Size after clear = %d, want 0\", m.Size())\n\t}\n\n\t// Recompute values\n\tm.Memoize(\"key1\", fn)\n\tm.Memoize(\"key2\", fn)\n\n\tif callCount != 4 {\n\t\tt.Errorf(\"Calls after clear = %d, want 4\", callCount)\n\t}\n\tif m.Size() != 2 {\n\t\tt.Errorf(\"Size after recompute = %d, want 2\", m.Size())\n\t}\n}\n\nfunc TestSize(t *testing.T) {\n\tm := New()\n\n\tif m.Size() != 0 {\n\t\tt.Errorf(\"Initial size = %d, want 0\", m.Size())\n\t}\n\n\tcallCount := 0\n\tfn := func() any {\n\t\tcallCount++\n\t\treturn \"value\"\n\t}\n\n\t// Add items\n\tm.Memoize(\"key1\", fn)\n\tif m.Size() != 1 {\n\t\tt.Errorf(\"Size after first insert = %d, want 1\", m.Size())\n\t}\n\n\tm.Memoize(\"key2\", fn)\n\tif m.Size() != 2 {\n\t\tt.Errorf(\"Size after second insert = %d, want 2\", m.Size())\n\t}\n\n\t// Duplicate key should not increase size\n\tm.Memoize(\"key1\", fn)\n\tif m.Size() != 2 {\n\t\tt.Errorf(\"Size after duplicate insert = %d, want 2\", m.Size())\n\t}\n\n\t// Remove item\n\tm.Invalidate(\"key1\")\n\tif m.Size() != 1 {\n\t\tt.Errorf(\"Size after invalidate = %d, want 1\", m.Size())\n\t}\n\n\t// Clear all\n\tm.Clear()\n\tif m.Size() != 0 {\n\t\tt.Errorf(\"Size after clear = %d, want 0\", m.Size())\n\t}\n}\n\nfunc TestMemoizeWithDifferentKeyTypes(t *testing.T) {\n\ttests := []struct {\n\t\tname      string\n\t\tkeys      []any    // Now an array of keys\n\t\tvalues    []string // Corresponding values\n\t\tcallCount *int\n\t}{\n\t\t{\n\t\t\tname:      \"integer keys\",\n\t\t\tkeys:      []any{42, 43},\n\t\t\tvalues:    []string{\"value-for-42\", \"value-for-43\"},\n\t\t\tcallCount: new(int),\n\t\t},\n\t\t{\n\t\t\tname:      \"float keys\",\n\t\t\tkeys:      []any{3.14, 2.718},\n\t\t\tvalues:    []string{\"value-for-pi\", \"value-for-e\"},\n\t\t\tcallCount: new(int),\n\t\t},\n\t\t{\n\t\t\tname:      \"bool keys\",\n\t\t\tkeys:      []any{true, false},\n\t\t\tvalues:    []string{\"value-for-true\", \"value-for-false\"},\n\t\t\tcallCount: new(int),\n\t\t},\n\t\t/*\n\t\t\t{\n\t\t\t\tname: \"struct keys\",\n\t\t\t\tkeys: []any{\n\t\t\t\t\tcomplexKey{ID: 1, Name: \"test1\"},\n\t\t\t\t\tcomplexKey{ID: 2, Name: \"test2\"},\n\t\t\t\t},\n\t\t\t\tvalues:    []string{\"value-for-struct1\", \"value-for-struct2\"},\n\t\t\t\tcallCount: new(int),\n\t\t\t},\n\t\t\t{\n\t\t\t\tname:      \"nil and empty interface keys\",\n\t\t\t\tkeys:      []any{nil, any(nil)},\n\t\t\t\tvalues:    []string{\"value-for-nil\", \"value-for-empty-interface\"},\n\t\t\t\tcallCount: new(int),\n\t\t\t},\n\t\t*/\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tm := New()\n\n\t\t\t// Test both keys\n\t\t\tfor i, key := range tt.keys {\n\t\t\t\tvalue := tt.values[i]\n\t\t\t\tfn := func() any {\n\t\t\t\t\t*tt.callCount++\n\t\t\t\t\treturn value\n\t\t\t\t}\n\n\t\t\t\t// First call should compute\n\t\t\t\tresult := m.Memoize(key, fn)\n\t\t\t\tif result != value {\n\t\t\t\t\tt.Errorf(\"Memoize() for key %v = %v, want %v\", key, result, value)\n\t\t\t\t}\n\t\t\t\tif *tt.callCount != i+1 {\n\t\t\t\t\tt.Errorf(\"Function called %d times, want %d\", *tt.callCount, i+1)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Verify size includes both entries\n\t\t\tif m.Size() != 2 {\n\t\t\t\tt.Errorf(\"Size after both inserts = %d, want 2\", m.Size())\n\t\t\t}\n\n\t\t\t// Second call for each key should use cache\n\t\t\tfor i, key := range tt.keys {\n\t\t\t\tinitialCount := *tt.callCount\n\t\t\t\tresult := m.Memoize(key, func() any {\n\t\t\t\t\t*tt.callCount++\n\t\t\t\t\treturn \"should-not-be-called\"\n\t\t\t\t})\n\n\t\t\t\tif result != tt.values[i] {\n\t\t\t\t\tt.Errorf(\"Memoize() second call for key %v = %v, want %v\", key, result, tt.values[i])\n\t\t\t\t}\n\t\t\t\tif *tt.callCount != initialCount {\n\t\t\t\t\tt.Errorf(\"Cache miss for key %v\", key)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Test invalidate for each key\n\t\t\tfor i, key := range tt.keys {\n\t\t\t\tm.Invalidate(key)\n\t\t\t\tif m.Size() != 1-i {\n\t\t\t\t\tt.Errorf(\"Size after invalidate %d = %d, want %d\", i+1, m.Size(), 1-i)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMultipleKeyTypes(t *testing.T) {\n\tm := New()\n\tcallCount := 0\n\n\t// Insert different key types simultaneously (two of each type)\n\tkeys := []any{\n\t\t42, 43, // ints\n\t\t\"string-key1\", \"string-key2\", // strings\n\t\t3.14, 2.718, // floats\n\t\ttrue, false, // bools\n\t}\n\n\tfor i, key := range keys {\n\t\tvalue := i\n\t\tm.Memoize(key, func() any {\n\t\t\tcallCount++\n\t\t\treturn value\n\t\t})\n\t}\n\n\t// Verify size includes all entries\n\tif m.Size() != len(keys) {\n\t\tt.Errorf(\"Size = %d, want %d\", m.Size(), len(keys))\n\t}\n\n\t// Verify all values are cached correctly\n\tfor i, key := range keys {\n\t\tinitialCount := callCount\n\t\tresult := m.Memoize(key, func() any {\n\t\t\tcallCount++\n\t\t\treturn -1 // Should never be returned if cache works\n\t\t})\n\n\t\tif result != i {\n\t\t\tt.Errorf(\"Memoize(%v) = %v, want %v\", key, result, i)\n\t\t}\n\t\tif callCount != initialCount {\n\t\t\tt.Errorf(\"Cache miss for key %v\", key)\n\t\t}\n\t}\n\n\t// Test invalidation of pairs\n\tfor i := 0; i \u003c len(keys); i += 2 {\n\t\tm.Invalidate(keys[i])\n\t\tm.Invalidate(keys[i+1])\n\t\texpectedSize := len(keys) - (i + 2)\n\t\tif m.Size() != expectedSize {\n\t\t\tt.Errorf(\"Size after invalidating pair %d = %d, want %d\", i/2, m.Size(), expectedSize)\n\t\t}\n\t}\n\n\t// Clear and verify\n\tm.Clear()\n\tif m.Size() != 0 {\n\t\tt.Errorf(\"Size after clear = %d, want 0\", m.Size())\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"QG8GurmyvLKdFixYF2vBqTowwuV977KBpA+2UnPAhx8GgUuKWTqqC74fvzhRdEN1+7Y1WHYp55mIm6mChubzAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"printfdebugging","path":"gno.land/p/moul/printfdebugging","files":[{"name":"color.gno","body":"package printfdebugging\n\n// consts copied from https://github.com/fatih/color/blob/main/color.go\n\n// Attribute defines a single SGR Code\ntype Attribute int\n\nconst Escape = \"\\x1b\"\n\n// Base attributes\nconst (\n\tReset Attribute = iota\n\tBold\n\tFaint\n\tItalic\n\tUnderline\n\tBlinkSlow\n\tBlinkRapid\n\tReverseVideo\n\tConcealed\n\tCrossedOut\n)\n\nconst (\n\tResetBold Attribute = iota + 22\n\tResetItalic\n\tResetUnderline\n\tResetBlinking\n\t_\n\tResetReversed\n\tResetConcealed\n\tResetCrossedOut\n)\n\n// Foreground text colors\nconst (\n\tFgBlack Attribute = iota + 30\n\tFgRed\n\tFgGreen\n\tFgYellow\n\tFgBlue\n\tFgMagenta\n\tFgCyan\n\tFgWhite\n)\n\n// Foreground Hi-Intensity text colors\nconst (\n\tFgHiBlack Attribute = iota + 90\n\tFgHiRed\n\tFgHiGreen\n\tFgHiYellow\n\tFgHiBlue\n\tFgHiMagenta\n\tFgHiCyan\n\tFgHiWhite\n)\n\n// Background text colors\nconst (\n\tBgBlack Attribute = iota + 40\n\tBgRed\n\tBgGreen\n\tBgYellow\n\tBgBlue\n\tBgMagenta\n\tBgCyan\n\tBgWhite\n)\n\n// Background Hi-Intensity text colors\nconst (\n\tBgHiBlack Attribute = iota + 100\n\tBgHiRed\n\tBgHiGreen\n\tBgHiYellow\n\tBgHiBlue\n\tBgHiMagenta\n\tBgHiCyan\n\tBgHiWhite\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/printfdebugging\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"printfdebugging.gno","body":"// this package is a joke... or not.\npackage printfdebugging\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc BigRedLine(args ...string) {\n\tprintln(ufmt.Sprintf(\"%s[%dm####################################%s[%dm %s\",\n\t\tEscape, int(BgRed), Escape, int(Reset),\n\t\tstrings.Join(args, \" \"),\n\t))\n}\n\nfunc Success() {\n\tprintln(\"   \\033[31mS\\033[33mU\\033[32mC\\033[36mC\\033[34mE\\033[35mS\\033[31mS\\033[0m   \")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"y0tZPBFzu4swfXKz8hovT4cZrkiPVHpKI0hFEKbna6oBPoguPrR+rZu65B2CiTlbi41P6Bo2Qwg2Sgox3KHMCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"lplist","path":"gno.land/p/moul/ulist/lplist","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/ulist/lplist\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"lplist.gno","body":"// Package lplist provides a layered proxy implementation for lists that allows transparent\n// migration of data between different schema versions.\n//\n// LayeredProxyList wraps an existing list (source) with a new list (target) and optionally\n// applies migrations to source data when it's accessed. This enables schema evolution without\n// requiring upfront migration of all data, making it ideal for large datasets or when\n// preserving original data is important.\n//\n// Key features:\n// - Lazy migration: Data is only transformed when accessed, not stored in migrated form\n// - Append-only source: Source data is treated as immutable to preserve original data\n// - Chaining: Multiple LayeredProxyLists can be stacked for multi-step migrations\n//\n// Example usage:\n//\n//\t// Define data types for different schema versions\n//\ttype UserV1 struct {\n//\t    Name string\n//\t    Age int\n//\t}\n//\n//\ttype UserV2 struct {\n//\t    FullName string\n//\t    Age int\n//\t    Active bool\n//\t}\n//\n//\t// Create source list with old schema\n//\tsourceList := ulist.New()\n//\tsourceList.Append(\n//\t    UserV1{Name: \"Alice\", Age: 30},\n//\t    UserV1{Name: \"Bob\", Age: 25},\n//\t)\n//\n//\t// Define migration function from V1 to V2\n//\tmigrateUserV1ToV2 := func(v any) any {\n//\t    user := v.(UserV1)\n//\t    return UserV2{\n//\t        FullName: user.Name,  // Name field renamed to FullName\n//\t        Age: user.Age,\n//\t        Active: true,         // New field with default value\n//\t    }\n//\t}\n//\n//\t// Create layered proxy with migration\n//\tproxy := NewLayeredProxyList(sourceList, migrateUserV1ToV2)\n//\n//\t// Add new data directly in V2 format\n//\tproxy.Append(UserV2{FullName: \"Charlie\", Age: 40, Active: false})\n//\n//\t// All access through proxy returns data in V2 format\n//\tfor i := 0; i \u003c proxy.Size(); i++ {\n//\t    user := proxy.Get(i).(UserV2)\n//\t    fmt.Printf(\"User: %s, Age: %d, Active: %t\\n\", user.FullName, user.Age, user.Active)\n//\t}\npackage lplist\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/moul/ulist\"\n)\n\n// MigratorFn is a function type that lazily converts values from source to target\ntype MigratorFn func(any) any\n\n// LayeredProxyList represents a wrapper around an existing List that handles migration\ntype LayeredProxyList struct {\n\tsource       ulist.IList\n\ttarget       *ulist.List\n\tmigrator     MigratorFn\n\tsourceHeight int // Store initial source size to optimize lookups\n}\n\n// NewLayeredProxyList creates a new LayeredProxyList instance that wraps an existing List\nfunc NewLayeredProxyList(source ulist.IList, migrator MigratorFn) *LayeredProxyList {\n\tsourceHeight := source.TotalSize()\n\ttarget := ulist.New()\n\treturn \u0026LayeredProxyList{\n\t\tsource:       source,\n\t\ttarget:       target,\n\t\tmigrator:     migrator,\n\t\tsourceHeight: sourceHeight,\n\t}\n}\n\n// Get retrieves the value at the specified index\n// Uses sourceHeight to efficiently route requests\nfunc (l *LayeredProxyList) Get(index int) any {\n\tif index \u003c l.sourceHeight {\n\t\t// Direct access to source for indices below sourceHeight\n\t\tval := l.source.Get(index)\n\t\tif val == nil {\n\t\t\treturn nil\n\t\t}\n\t\t// Only apply migrator if it exists\n\t\tif l.migrator != nil {\n\t\t\treturn l.migrator(val)\n\t\t}\n\t\treturn val\n\t}\n\t// For indices \u003e= sourceHeight, adjust index to be relative to target list starting at 0\n\ttargetIndex := index - l.sourceHeight\n\treturn l.target.Get(targetIndex)\n}\n\n// Append adds one or more values to the target list\nfunc (l *LayeredProxyList) Append(values ...any) {\n\tl.target.Append(values...)\n}\n\n// Delete marks elements as deleted in the appropriate list\nfunc (l *LayeredProxyList) Delete(indices ...int) error {\n\tfor _, index := range indices {\n\t\tif index \u003c l.sourceHeight {\n\t\t\terr := l.source.Delete(index)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t}\n\n\tfor _, index := range indices {\n\t\ttargetIndex := index - l.sourceHeight\n\t\terr := l.target.Delete(targetIndex)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\n// Size returns the total number of active elements\nfunc (l *LayeredProxyList) Size() int {\n\treturn l.source.Size() + l.target.Size()\n}\n\n// TotalSize returns the total number of elements in the list\nfunc (l *LayeredProxyList) TotalSize() int {\n\treturn l.sourceHeight + l.target.TotalSize()\n}\n\n// MustDelete deletes elements, panicking on error\nfunc (l *LayeredProxyList) MustDelete(indices ...int) {\n\tif err := l.Delete(indices...); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// MustGet retrieves a value, panicking if not found\nfunc (l *LayeredProxyList) MustGet(index int) any {\n\tval := l.Get(index)\n\tif val == nil {\n\t\tpanic(ulist.ErrDeleted)\n\t}\n\treturn val\n}\n\n// GetRange returns elements between start and end indices\nfunc (l *LayeredProxyList) GetRange(start, end int) []ulist.Entry {\n\tvar entries []ulist.Entry\n\tl.Iterator(start, end, func(index int, value any) bool {\n\t\tentries = append(entries, ulist.Entry{Index: index, Value: value})\n\t\treturn false\n\t})\n\treturn entries\n}\n\n// GetRangeByOffset returns elements starting from offset\nfunc (l *LayeredProxyList) GetRangeByOffset(offset int, count int) []ulist.Entry {\n\tvar entries []ulist.Entry\n\tl.IteratorByOffset(offset, count, func(index int, value any) bool {\n\t\tentries = append(entries, ulist.Entry{Index: index, Value: value})\n\t\treturn false\n\t})\n\treturn entries\n}\n\n// Iterator performs iteration between start and end indices\nfunc (l *LayeredProxyList) Iterator(start, end int, cb ulist.IterCbFn) bool {\n\t// For empty list or invalid range\n\tif start \u003c 0 \u0026\u0026 end \u003c 0 {\n\t\treturn false\n\t}\n\n\t// Normalize indices\n\tif start \u003c 0 {\n\t\tstart = 0\n\t}\n\tif end \u003c 0 {\n\t\tend = 0\n\t}\n\n\ttotalSize := l.TotalSize()\n\tif end \u003e= totalSize {\n\t\tend = totalSize - 1\n\t}\n\tif start \u003e= totalSize {\n\t\tstart = totalSize - 1\n\t}\n\n\t// Handle reverse iteration\n\tif start \u003e end {\n\t\tfor i := start; i \u003e= end; i-- {\n\t\t\tval := l.Get(i)\n\t\t\tif val != nil {\n\t\t\t\tif cb(i, val) {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n\n\t// Handle forward iteration\n\tfor i := start; i \u003c= end; i++ {\n\t\tval := l.Get(i)\n\t\tif val != nil {\n\t\t\tif cb(i, val) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\n// IteratorByOffset performs iteration starting from offset\nfunc (l *LayeredProxyList) IteratorByOffset(offset int, count int, cb ulist.IterCbFn) bool {\n\tif count == 0 {\n\t\treturn false\n\t}\n\n\t// Normalize offset\n\tif offset \u003c 0 {\n\t\toffset = 0\n\t}\n\ttotalSize := l.TotalSize()\n\tif offset \u003e= totalSize {\n\t\toffset = totalSize - 1\n\t}\n\n\t// Determine end based on count direction\n\tvar end int\n\tif count \u003e 0 {\n\t\tend = totalSize - 1\n\t} else {\n\t\tend = 0\n\t}\n\n\twrapperReturned := false\n\n\t// Calculate absolute value manually instead of using abs function\n\tremaining := count\n\tif remaining \u003c 0 {\n\t\tremaining = -remaining\n\t}\n\n\twrapper := func(index int, value any) bool {\n\t\tif remaining \u003c= 0 {\n\t\t\twrapperReturned = true\n\t\t\treturn true\n\t\t}\n\t\tremaining--\n\t\treturn cb(index, value)\n\t}\n\n\tret := l.Iterator(offset, end, wrapper)\n\tif wrapperReturned {\n\t\treturn false\n\t}\n\treturn ret\n}\n\n// Set updates the value at the specified index\nfunc (l *LayeredProxyList) Set(index int, value any) error {\n\tif index \u003c l.sourceHeight {\n\t\t// Cannot modify source list directly\n\t\treturn errors.New(\"cannot modify source list directly\")\n\t}\n\n\t// Adjust index to be relative to target list starting at 0\n\ttargetIndex := index - l.sourceHeight\n\treturn l.target.Set(targetIndex, value)\n}\n\n// MustSet updates the value at the specified index, panicking on error\nfunc (l *LayeredProxyList) MustSet(index int, value any) {\n\tif err := l.Set(index, value); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// GetByOffset returns elements starting from offset with count determining direction\nfunc (l *LayeredProxyList) GetByOffset(offset int, count int) []ulist.Entry {\n\tvar entries []ulist.Entry\n\tl.IteratorByOffset(offset, count, func(index int, value any) bool {\n\t\tentries = append(entries, ulist.Entry{Index: index, Value: value})\n\t\treturn false\n\t})\n\treturn entries\n}\n\n// Verify that LayeredProxyList implements IList\nvar _ ulist.IList = (*LayeredProxyList)(nil)\n"},{"name":"lplist_test.gno","body":"package lplist\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/ulist\"\n)\n\n// TestLayeredProxyListBasicOperations tests the basic operations of LayeredProxyList\nfunc TestLayeredProxyListBasicOperations(t *testing.T) {\n\t// Create source list with initial data\n\tsource := ulist.New()\n\tsource.Append(1, 2, 3)\n\n\t// Create proxy list with a simple multiplier migrator\n\tmigrator := func(v any) any {\n\t\treturn v.(int) * 2\n\t}\n\tproxy := NewLayeredProxyList(source, migrator)\n\n\t// Test initial state\n\tif got := proxy.Size(); got != 3 {\n\t\tt.Errorf(\"initial Size() = %v, want %v\", got, 3)\n\t}\n\tif got := proxy.TotalSize(); got != 3 {\n\t\tt.Errorf(\"initial TotalSize() = %v, want %v\", got, 3)\n\t}\n\n\t// Test Get with migration\n\ttests := []struct {\n\t\tindex int\n\t\twant  any\n\t}{\n\t\t{0, 2}, // 1 * 2\n\t\t{1, 4}, // 2 * 2\n\t\t{2, 6}, // 3 * 2\n\t}\n\n\tfor _, tt := range tests {\n\t\tif got := proxy.Get(tt.index); got != tt.want {\n\t\t\tt.Errorf(\"Get(%v) = %v, want %v\", tt.index, got, tt.want)\n\t\t}\n\t}\n\n\t// Test Append to target\n\tproxy.Append(7, 8)\n\tif got := proxy.Size(); got != 5 {\n\t\tt.Errorf(\"Size() after append = %v, want %v\", got, 5)\n\t}\n\n\t// Test Get from target (no migration)\n\tif got := proxy.Get(3); got != 7 {\n\t\tt.Errorf(\"Get(3) = %v, want %v\", got, 7)\n\t}\n}\n\n// TestLayeredProxyListDelete tests delete operations\nfunc TestLayeredProxyListDelete(t *testing.T) {\n\tsource := ulist.New()\n\tsource.Append(1, 2, 3)\n\tproxy := NewLayeredProxyList(source, nil)\n\tproxy.Append(4, 5)\n\n\t// Test deleting from source (should fail)\n\tif err := proxy.Delete(1); err == nil {\n\t\tt.Error(\"Delete from source should return error\")\n\t}\n\n\t// Test deleting from target (should succeed)\n\tif err := proxy.Delete(3); err != nil {\n\t\tt.Errorf(\"Delete from target failed: %s\", err.Error())\n\t}\n\n\t// After deletion, the value might be undefined rather than nil\n\t// Check that it's not equal to the original value\n\tif got := proxy.Get(3); got == 5 {\n\t\tt.Errorf(\"Get(3) after delete = %v, want it to be deleted\", got)\n\t}\n}\n\n// TestLayeredProxyListIteration tests iteration methods\nfunc TestLayeredProxyListIteration(t *testing.T) {\n\tsource := ulist.New()\n\tsource.Append(1, 2, 3)\n\tproxy := NewLayeredProxyList(source, nil)\n\tproxy.Append(4, 5)\n\n\t// Test GetRange\n\tentries := proxy.GetRange(0, 4)\n\tif len(entries) != 5 {\n\t\tt.Errorf(\"GetRange returned %v entries, want 5\", len(entries))\n\t}\n\n\t// Test reverse iteration\n\tentries = proxy.GetRange(4, 0)\n\tif len(entries) != 5 {\n\t\tt.Errorf(\"Reverse GetRange returned %v entries, want 5\", len(entries))\n\t}\n\n\t// Test IteratorByOffset with positive count\n\tvar values []any\n\tproxy.IteratorByOffset(1, 3, func(index int, value any) bool {\n\t\tvalues = append(values, value)\n\t\treturn false\n\t})\n\tif len(values) != 3 {\n\t\tt.Errorf(\"IteratorByOffset returned %v values, want 3\", len(values))\n\t}\n}\n\n// TestLayeredProxyListMustOperations tests must operations\nfunc TestLayeredProxyListMustOperations(t *testing.T) {\n\tsource := ulist.New()\n\tsource.Append(1, 2)\n\tproxy := NewLayeredProxyList(source, nil)\n\n\t// Test MustGet success\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tt.Errorf(\"MustGet panicked unexpectedly: %v\", r)\n\t\t}\n\t}()\n\tif got := proxy.MustGet(1); got != 2 {\n\t\tt.Errorf(\"MustGet(1) = %v, want 2\", got)\n\t}\n\n\t// Test MustGet panic\n\tdefer func() {\n\t\tif r := recover(); r == nil {\n\t\t\tt.Error(\"MustGet should have panicked\")\n\t\t}\n\t}()\n\tproxy.MustGet(99) // Should panic\n}\n\n// TestLayeredProxyListWithNilMigrator tests behavior without a migrator\nfunc TestLayeredProxyListWithNilMigrator(t *testing.T) {\n\tsource := ulist.New()\n\tsource.Append(1, 2)\n\tproxy := NewLayeredProxyList(source, nil)\n\n\tif got := proxy.Get(0); got != 1 {\n\t\tt.Errorf(\"Get(0) with nil migrator = %v, want 1\", got)\n\t}\n}\n\n// TestLayeredProxyListEmpty tests operations on empty lists\nfunc TestLayeredProxyListEmpty(t *testing.T) {\n\tsource := ulist.New()\n\tproxy := NewLayeredProxyList(source, nil)\n\n\tif got := proxy.Size(); got != 0 {\n\t\tt.Errorf(\"Size() of empty list = %v, want 0\", got)\n\t}\n\n\tif got := proxy.Get(0); got != nil {\n\t\tt.Errorf(\"Get(0) of empty list = %v, want nil\", got)\n\t}\n\n\tentries := proxy.GetRange(0, 10)\n\tif len(entries) != 0 {\n\t\tt.Errorf(\"GetRange on empty list returned %v entries, want 0\", len(entries))\n\t}\n}\n\n// TestLayeredProxyListChaining tests chaining of layered proxies with struct migrations\nfunc TestLayeredProxyListChaining(t *testing.T) {\n\t// Define struct types for different versions\n\ttype v1 struct {\n\t\tnamev1 string\n\t}\n\ttype v2 struct {\n\t\tnamev2 string\n\t}\n\ttype v3 struct {\n\t\tnamev3 string\n\t}\n\n\t// Create source list with v1 objects\n\tsource := ulist.New()\n\tsource.Append(v1{namev1: \"object1\"}, v1{namev1: \"object2\"})\n\n\t// Migration function from v1 to v2\n\tv1Tov2 := func(v any) any {\n\t\tobj := v.(v1)\n\t\treturn v2{namev2: obj.namev1 + \"_v2\"}\n\t}\n\n\t// Create first proxy with v1-\u003ev2 migration\n\tproxyV2 := NewLayeredProxyList(source, v1Tov2)\n\tproxyV2.Append(v2{namev2: \"direct_v2\"})\n\n\t// Migration function from v2 to v3\n\tv2Tov3 := func(v any) any {\n\t\tobj := v.(v2)\n\t\treturn v3{namev3: obj.namev2 + \"_v3\"}\n\t}\n\n\t// Create second proxy with v2-\u003ev3 migration, using the first proxy as source\n\tproxyV3 := NewLayeredProxyList(proxyV2, v2Tov3)\n\tproxyV3.Append(v3{namev3: \"direct_v3\"})\n\n\t// Verify sizes\n\tif got := proxyV3.Size(); got != 4 {\n\t\tt.Errorf(\"proxyV3.Size() = %v, want 4\", got)\n\t}\n\n\t// Test that all objects are correctly migrated when accessed through proxyV3\n\texpected := []struct {\n\t\tindex int\n\t\tname  string\n\t}{\n\t\t{0, \"object1_v2_v3\"}, // v1 -\u003e v2 -\u003e v3\n\t\t{1, \"object2_v2_v3\"}, // v1 -\u003e v2 -\u003e v3\n\t\t{2, \"direct_v2_v3\"},  // v2 -\u003e v3\n\t\t{3, \"direct_v3\"},     // v3 (no migration)\n\t}\n\n\tfor _, tt := range expected {\n\t\tobj := proxyV3.Get(tt.index).(v3)\n\t\tif obj.namev3 != tt.name {\n\t\t\tt.Errorf(\"proxyV3.Get(%d).namev3 = %v, want %v\", tt.index, obj.namev3, tt.name)\n\t\t}\n\t}\n\n\t// Verify getting items from middle layer (proxyV2)\n\tmiddleExpected := []struct {\n\t\tindex int\n\t\tname  string\n\t}{\n\t\t{0, \"object1_v2\"}, // v1 -\u003e v2\n\t\t{1, \"object2_v2\"}, // v1 -\u003e v2\n\t\t{2, \"direct_v2\"},  // v2 (no migration)\n\t}\n\n\tfor _, tt := range middleExpected {\n\t\tobj := proxyV2.Get(tt.index).(v2)\n\t\tif obj.namev2 != tt.name {\n\t\t\tt.Errorf(\"proxyV2.Get(%d).namev2 = %v, want %v\", tt.index, obj.namev2, tt.name)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"9TIei7U84wtsP9wmPkTvPRG0LaP+divHYwxI5H4o6nUKgx29KACSZajlfMhBEZADD+8csCNujf7e5kedU+aTAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"web25","path":"gno.land/p/moul/web25","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/web25\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"web25.gno","body":"// Pacakge web25 provides an opinionated way to register an external web2\n// frontend to provide a \"better\" web2.5 experience.\npackage web25\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/moul/realmpath\"\n)\n\ntype Config struct {\n\tCID  string\n\tURL  string\n\tText string\n}\n\nfunc (c *Config) SetRemoteFrontendByURL(url string) {\n\tc.CID = \"\"\n\tc.URL = url\n}\n\nfunc (c *Config) SetRemoteFrontendByCID(cid string) {\n\tc.CID = cid\n\tc.URL = \"\"\n}\n\nfunc (c Config) GetLink() string {\n\tif c.CID != \"\" {\n\t\treturn \"https://ipfs.io/ipfs/\" + c.CID\n\t}\n\treturn c.URL\n}\n\nconst DefaultText = \"Click [here]({link}) to visit the full rendering experience.\\n\"\n\n// Render displays a frontend link at the top of your realm's Render function in\n// a concistent way to help gno visitors to have a consistent experience.\n//\n// if query is not nil, then it will check if it's not disable by ?no-web25, so\n// that you can call the render function from an external point of view.\nfunc (c Config) Render(path string) string {\n\tif realmpath.Parse(path).Query.Get(\"no-web25\") == \"1\" {\n\t\treturn \"\"\n\t}\n\ttext := c.Text\n\tif text == \"\" {\n\t\ttext = DefaultText\n\t}\n\ttext = strings.ReplaceAll(text, \"{link}\", c.GetLink())\n\treturn text\n}\n"},{"name":"web25_test.gno","body":"package web25\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"+fQkZQth8sto1/4d4GM3bYP/0d3UmtM/Guk4fd8b68X+dmS13UFq4mErfig0ZmeWMiV49NM19nLu451Kz/WqCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"xmath","path":"gno.land/p/moul/xmath","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/moul/xmath\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"xmath.gen.gno","body":"// Code generated by generator.go; DO NOT EDIT.\npackage xmath\n\n// Int8 helpers\nfunc MaxInt8(a, b int8) int8 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinInt8(a, b int8) int8 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampInt8(value, min, max int8) int8 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\nfunc AbsInt8(x int8) int8 {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\nfunc SignInt8(x int8) int8 {\n\tif x \u003c 0 {\n\t\treturn -1\n\t}\n\tif x \u003e 0 {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\n// Int16 helpers\nfunc MaxInt16(a, b int16) int16 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinInt16(a, b int16) int16 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampInt16(value, min, max int16) int16 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\nfunc AbsInt16(x int16) int16 {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\nfunc SignInt16(x int16) int16 {\n\tif x \u003c 0 {\n\t\treturn -1\n\t}\n\tif x \u003e 0 {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\n// Int32 helpers\nfunc MaxInt32(a, b int32) int32 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinInt32(a, b int32) int32 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampInt32(value, min, max int32) int32 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\nfunc AbsInt32(x int32) int32 {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\nfunc SignInt32(x int32) int32 {\n\tif x \u003c 0 {\n\t\treturn -1\n\t}\n\tif x \u003e 0 {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\n// Int64 helpers\nfunc MaxInt64(a, b int64) int64 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinInt64(a, b int64) int64 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampInt64(value, min, max int64) int64 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\nfunc AbsInt64(x int64) int64 {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\nfunc SignInt64(x int64) int64 {\n\tif x \u003c 0 {\n\t\treturn -1\n\t}\n\tif x \u003e 0 {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\n// Int helpers\nfunc MaxInt(a, b int) int {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinInt(a, b int) int {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampInt(value, min, max int) int {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\nfunc AbsInt(x int) int {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\nfunc SignInt(x int) int {\n\tif x \u003c 0 {\n\t\treturn -1\n\t}\n\tif x \u003e 0 {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\n// Uint8 helpers\nfunc MaxUint8(a, b uint8) uint8 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinUint8(a, b uint8) uint8 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampUint8(value, min, max uint8) uint8 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\n// Uint16 helpers\nfunc MaxUint16(a, b uint16) uint16 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinUint16(a, b uint16) uint16 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampUint16(value, min, max uint16) uint16 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\n// Uint32 helpers\nfunc MaxUint32(a, b uint32) uint32 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinUint32(a, b uint32) uint32 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampUint32(value, min, max uint32) uint32 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\n// Uint64 helpers\nfunc MaxUint64(a, b uint64) uint64 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinUint64(a, b uint64) uint64 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampUint64(value, min, max uint64) uint64 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\n// Uint helpers\nfunc MaxUint(a, b uint) uint {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinUint(a, b uint) uint {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampUint(value, min, max uint) uint {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\n// Float32 helpers\nfunc MaxFloat32(a, b float32) float32 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinFloat32(a, b float32) float32 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampFloat32(value, min, max float32) float32 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\nfunc AbsFloat32(x float32) float32 {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\nfunc SignFloat32(x float32) float32 {\n\tif x \u003c 0 {\n\t\treturn -1\n\t}\n\tif x \u003e 0 {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\n// Float64 helpers\nfunc MaxFloat64(a, b float64) float64 {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc MinFloat64(a, b float64) float64 {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n\nfunc ClampFloat64(value, min, max float64) float64 {\n\tif value \u003c min {\n\t\treturn min\n\t}\n\tif value \u003e max {\n\t\treturn max\n\t}\n\treturn value\n}\n\nfunc AbsFloat64(x float64) float64 {\n\tif x \u003c 0 {\n\t\treturn -x\n\t}\n\treturn x\n}\n\nfunc SignFloat64(x float64) float64 {\n\tif x \u003c 0 {\n\t\treturn -1\n\t}\n\tif x \u003e 0 {\n\t\treturn 1\n\t}\n\treturn 0\n}\n"},{"name":"xmath.gen_test.gno","body":"package xmath\n\nimport \"testing\"\n\nfunc TestInt8Helpers(t *testing.T) {\n\t// Test MaxInt8\n\tif MaxInt8(1, 2) != 2 {\n\t\tt.Error(\"MaxInt8(1, 2) should be 2\")\n\t}\n\tif MaxInt8(-1, -2) != -1 {\n\t\tt.Error(\"MaxInt8(-1, -2) should be -1\")\n\t}\n\n\t// Test MinInt8\n\tif MinInt8(1, 2) != 1 {\n\t\tt.Error(\"MinInt8(1, 2) should be 1\")\n\t}\n\tif MinInt8(-1, -2) != -2 {\n\t\tt.Error(\"MinInt8(-1, -2) should be -2\")\n\t}\n\n\t// Test ClampInt8\n\tif ClampInt8(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampInt8(5, 1, 3) should be 3\")\n\t}\n\tif ClampInt8(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampInt8(0, 1, 3) should be 1\")\n\t}\n\tif ClampInt8(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampInt8(2, 1, 3) should be 2\")\n\t}\n\n\t// Test AbsInt8\n\tif AbsInt8(-5) != 5 {\n\t\tt.Error(\"AbsInt8(-5) should be 5\")\n\t}\n\tif AbsInt8(5) != 5 {\n\t\tt.Error(\"AbsInt8(5) should be 5\")\n\t}\n\n\t// Test SignInt8\n\tif SignInt8(-5) != -1 {\n\t\tt.Error(\"SignInt8(-5) should be -1\")\n\t}\n\tif SignInt8(5) != 1 {\n\t\tt.Error(\"SignInt8(5) should be 1\")\n\t}\n\tif SignInt8(0) != 0 {\n\t\tt.Error(\"SignInt8(0) should be 0\")\n\t}\n\n}\n\nfunc TestInt16Helpers(t *testing.T) {\n\t// Test MaxInt16\n\tif MaxInt16(1, 2) != 2 {\n\t\tt.Error(\"MaxInt16(1, 2) should be 2\")\n\t}\n\tif MaxInt16(-1, -2) != -1 {\n\t\tt.Error(\"MaxInt16(-1, -2) should be -1\")\n\t}\n\n\t// Test MinInt16\n\tif MinInt16(1, 2) != 1 {\n\t\tt.Error(\"MinInt16(1, 2) should be 1\")\n\t}\n\tif MinInt16(-1, -2) != -2 {\n\t\tt.Error(\"MinInt16(-1, -2) should be -2\")\n\t}\n\n\t// Test ClampInt16\n\tif ClampInt16(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampInt16(5, 1, 3) should be 3\")\n\t}\n\tif ClampInt16(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampInt16(0, 1, 3) should be 1\")\n\t}\n\tif ClampInt16(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampInt16(2, 1, 3) should be 2\")\n\t}\n\n\t// Test AbsInt16\n\tif AbsInt16(-5) != 5 {\n\t\tt.Error(\"AbsInt16(-5) should be 5\")\n\t}\n\tif AbsInt16(5) != 5 {\n\t\tt.Error(\"AbsInt16(5) should be 5\")\n\t}\n\n\t// Test SignInt16\n\tif SignInt16(-5) != -1 {\n\t\tt.Error(\"SignInt16(-5) should be -1\")\n\t}\n\tif SignInt16(5) != 1 {\n\t\tt.Error(\"SignInt16(5) should be 1\")\n\t}\n\tif SignInt16(0) != 0 {\n\t\tt.Error(\"SignInt16(0) should be 0\")\n\t}\n\n}\n\nfunc TestInt32Helpers(t *testing.T) {\n\t// Test MaxInt32\n\tif MaxInt32(1, 2) != 2 {\n\t\tt.Error(\"MaxInt32(1, 2) should be 2\")\n\t}\n\tif MaxInt32(-1, -2) != -1 {\n\t\tt.Error(\"MaxInt32(-1, -2) should be -1\")\n\t}\n\n\t// Test MinInt32\n\tif MinInt32(1, 2) != 1 {\n\t\tt.Error(\"MinInt32(1, 2) should be 1\")\n\t}\n\tif MinInt32(-1, -2) != -2 {\n\t\tt.Error(\"MinInt32(-1, -2) should be -2\")\n\t}\n\n\t// Test ClampInt32\n\tif ClampInt32(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampInt32(5, 1, 3) should be 3\")\n\t}\n\tif ClampInt32(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampInt32(0, 1, 3) should be 1\")\n\t}\n\tif ClampInt32(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampInt32(2, 1, 3) should be 2\")\n\t}\n\n\t// Test AbsInt32\n\tif AbsInt32(-5) != 5 {\n\t\tt.Error(\"AbsInt32(-5) should be 5\")\n\t}\n\tif AbsInt32(5) != 5 {\n\t\tt.Error(\"AbsInt32(5) should be 5\")\n\t}\n\n\t// Test SignInt32\n\tif SignInt32(-5) != -1 {\n\t\tt.Error(\"SignInt32(-5) should be -1\")\n\t}\n\tif SignInt32(5) != 1 {\n\t\tt.Error(\"SignInt32(5) should be 1\")\n\t}\n\tif SignInt32(0) != 0 {\n\t\tt.Error(\"SignInt32(0) should be 0\")\n\t}\n\n}\n\nfunc TestInt64Helpers(t *testing.T) {\n\t// Test MaxInt64\n\tif MaxInt64(1, 2) != 2 {\n\t\tt.Error(\"MaxInt64(1, 2) should be 2\")\n\t}\n\tif MaxInt64(-1, -2) != -1 {\n\t\tt.Error(\"MaxInt64(-1, -2) should be -1\")\n\t}\n\n\t// Test MinInt64\n\tif MinInt64(1, 2) != 1 {\n\t\tt.Error(\"MinInt64(1, 2) should be 1\")\n\t}\n\tif MinInt64(-1, -2) != -2 {\n\t\tt.Error(\"MinInt64(-1, -2) should be -2\")\n\t}\n\n\t// Test ClampInt64\n\tif ClampInt64(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampInt64(5, 1, 3) should be 3\")\n\t}\n\tif ClampInt64(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampInt64(0, 1, 3) should be 1\")\n\t}\n\tif ClampInt64(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampInt64(2, 1, 3) should be 2\")\n\t}\n\n\t// Test AbsInt64\n\tif AbsInt64(-5) != 5 {\n\t\tt.Error(\"AbsInt64(-5) should be 5\")\n\t}\n\tif AbsInt64(5) != 5 {\n\t\tt.Error(\"AbsInt64(5) should be 5\")\n\t}\n\n\t// Test SignInt64\n\tif SignInt64(-5) != -1 {\n\t\tt.Error(\"SignInt64(-5) should be -1\")\n\t}\n\tif SignInt64(5) != 1 {\n\t\tt.Error(\"SignInt64(5) should be 1\")\n\t}\n\tif SignInt64(0) != 0 {\n\t\tt.Error(\"SignInt64(0) should be 0\")\n\t}\n\n}\n\nfunc TestIntHelpers(t *testing.T) {\n\t// Test MaxInt\n\tif MaxInt(1, 2) != 2 {\n\t\tt.Error(\"MaxInt(1, 2) should be 2\")\n\t}\n\tif MaxInt(-1, -2) != -1 {\n\t\tt.Error(\"MaxInt(-1, -2) should be -1\")\n\t}\n\n\t// Test MinInt\n\tif MinInt(1, 2) != 1 {\n\t\tt.Error(\"MinInt(1, 2) should be 1\")\n\t}\n\tif MinInt(-1, -2) != -2 {\n\t\tt.Error(\"MinInt(-1, -2) should be -2\")\n\t}\n\n\t// Test ClampInt\n\tif ClampInt(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampInt(5, 1, 3) should be 3\")\n\t}\n\tif ClampInt(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampInt(0, 1, 3) should be 1\")\n\t}\n\tif ClampInt(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampInt(2, 1, 3) should be 2\")\n\t}\n\n\t// Test AbsInt\n\tif AbsInt(-5) != 5 {\n\t\tt.Error(\"AbsInt(-5) should be 5\")\n\t}\n\tif AbsInt(5) != 5 {\n\t\tt.Error(\"AbsInt(5) should be 5\")\n\t}\n\n\t// Test SignInt\n\tif SignInt(-5) != -1 {\n\t\tt.Error(\"SignInt(-5) should be -1\")\n\t}\n\tif SignInt(5) != 1 {\n\t\tt.Error(\"SignInt(5) should be 1\")\n\t}\n\tif SignInt(0) != 0 {\n\t\tt.Error(\"SignInt(0) should be 0\")\n\t}\n\n}\n\nfunc TestUint8Helpers(t *testing.T) {\n\t// Test MaxUint8\n\tif MaxUint8(1, 2) != 2 {\n\t\tt.Error(\"MaxUint8(1, 2) should be 2\")\n\t}\n\n\t// Test MinUint8\n\tif MinUint8(1, 2) != 1 {\n\t\tt.Error(\"MinUint8(1, 2) should be 1\")\n\t}\n\n\t// Test ClampUint8\n\tif ClampUint8(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampUint8(5, 1, 3) should be 3\")\n\t}\n\tif ClampUint8(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampUint8(0, 1, 3) should be 1\")\n\t}\n\tif ClampUint8(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampUint8(2, 1, 3) should be 2\")\n\t}\n\n}\n\nfunc TestUint16Helpers(t *testing.T) {\n\t// Test MaxUint16\n\tif MaxUint16(1, 2) != 2 {\n\t\tt.Error(\"MaxUint16(1, 2) should be 2\")\n\t}\n\n\t// Test MinUint16\n\tif MinUint16(1, 2) != 1 {\n\t\tt.Error(\"MinUint16(1, 2) should be 1\")\n\t}\n\n\t// Test ClampUint16\n\tif ClampUint16(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampUint16(5, 1, 3) should be 3\")\n\t}\n\tif ClampUint16(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampUint16(0, 1, 3) should be 1\")\n\t}\n\tif ClampUint16(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampUint16(2, 1, 3) should be 2\")\n\t}\n\n}\n\nfunc TestUint32Helpers(t *testing.T) {\n\t// Test MaxUint32\n\tif MaxUint32(1, 2) != 2 {\n\t\tt.Error(\"MaxUint32(1, 2) should be 2\")\n\t}\n\n\t// Test MinUint32\n\tif MinUint32(1, 2) != 1 {\n\t\tt.Error(\"MinUint32(1, 2) should be 1\")\n\t}\n\n\t// Test ClampUint32\n\tif ClampUint32(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampUint32(5, 1, 3) should be 3\")\n\t}\n\tif ClampUint32(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampUint32(0, 1, 3) should be 1\")\n\t}\n\tif ClampUint32(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampUint32(2, 1, 3) should be 2\")\n\t}\n\n}\n\nfunc TestUint64Helpers(t *testing.T) {\n\t// Test MaxUint64\n\tif MaxUint64(1, 2) != 2 {\n\t\tt.Error(\"MaxUint64(1, 2) should be 2\")\n\t}\n\n\t// Test MinUint64\n\tif MinUint64(1, 2) != 1 {\n\t\tt.Error(\"MinUint64(1, 2) should be 1\")\n\t}\n\n\t// Test ClampUint64\n\tif ClampUint64(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampUint64(5, 1, 3) should be 3\")\n\t}\n\tif ClampUint64(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampUint64(0, 1, 3) should be 1\")\n\t}\n\tif ClampUint64(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampUint64(2, 1, 3) should be 2\")\n\t}\n\n}\n\nfunc TestUintHelpers(t *testing.T) {\n\t// Test MaxUint\n\tif MaxUint(1, 2) != 2 {\n\t\tt.Error(\"MaxUint(1, 2) should be 2\")\n\t}\n\n\t// Test MinUint\n\tif MinUint(1, 2) != 1 {\n\t\tt.Error(\"MinUint(1, 2) should be 1\")\n\t}\n\n\t// Test ClampUint\n\tif ClampUint(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampUint(5, 1, 3) should be 3\")\n\t}\n\tif ClampUint(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampUint(0, 1, 3) should be 1\")\n\t}\n\tif ClampUint(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampUint(2, 1, 3) should be 2\")\n\t}\n\n}\n\nfunc TestFloat32Helpers(t *testing.T) {\n\t// Test MaxFloat32\n\tif MaxFloat32(1, 2) != 2 {\n\t\tt.Error(\"MaxFloat32(1, 2) should be 2\")\n\t}\n\tif MaxFloat32(-1, -2) != -1 {\n\t\tt.Error(\"MaxFloat32(-1, -2) should be -1\")\n\t}\n\n\t// Test MinFloat32\n\tif MinFloat32(1, 2) != 1 {\n\t\tt.Error(\"MinFloat32(1, 2) should be 1\")\n\t}\n\tif MinFloat32(-1, -2) != -2 {\n\t\tt.Error(\"MinFloat32(-1, -2) should be -2\")\n\t}\n\n\t// Test ClampFloat32\n\tif ClampFloat32(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampFloat32(5, 1, 3) should be 3\")\n\t}\n\tif ClampFloat32(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampFloat32(0, 1, 3) should be 1\")\n\t}\n\tif ClampFloat32(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampFloat32(2, 1, 3) should be 2\")\n\t}\n\n\t// Test AbsFloat32\n\tif AbsFloat32(-5) != 5 {\n\t\tt.Error(\"AbsFloat32(-5) should be 5\")\n\t}\n\tif AbsFloat32(5) != 5 {\n\t\tt.Error(\"AbsFloat32(5) should be 5\")\n\t}\n\n\t// Test SignFloat32\n\tif SignFloat32(-5) != -1 {\n\t\tt.Error(\"SignFloat32(-5) should be -1\")\n\t}\n\tif SignFloat32(5) != 1 {\n\t\tt.Error(\"SignFloat32(5) should be 1\")\n\t}\n\tif SignFloat32(0.0) != 0 {\n\t\tt.Error(\"SignFloat32(0.0) should be 0\")\n\t}\n\n}\n\nfunc TestFloat64Helpers(t *testing.T) {\n\t// Test MaxFloat64\n\tif MaxFloat64(1, 2) != 2 {\n\t\tt.Error(\"MaxFloat64(1, 2) should be 2\")\n\t}\n\tif MaxFloat64(-1, -2) != -1 {\n\t\tt.Error(\"MaxFloat64(-1, -2) should be -1\")\n\t}\n\n\t// Test MinFloat64\n\tif MinFloat64(1, 2) != 1 {\n\t\tt.Error(\"MinFloat64(1, 2) should be 1\")\n\t}\n\tif MinFloat64(-1, -2) != -2 {\n\t\tt.Error(\"MinFloat64(-1, -2) should be -2\")\n\t}\n\n\t// Test ClampFloat64\n\tif ClampFloat64(5, 1, 3) != 3 {\n\t\tt.Error(\"ClampFloat64(5, 1, 3) should be 3\")\n\t}\n\tif ClampFloat64(0, 1, 3) != 1 {\n\t\tt.Error(\"ClampFloat64(0, 1, 3) should be 1\")\n\t}\n\tif ClampFloat64(2, 1, 3) != 2 {\n\t\tt.Error(\"ClampFloat64(2, 1, 3) should be 2\")\n\t}\n\n\t// Test AbsFloat64\n\tif AbsFloat64(-5) != 5 {\n\t\tt.Error(\"AbsFloat64(-5) should be 5\")\n\t}\n\tif AbsFloat64(5) != 5 {\n\t\tt.Error(\"AbsFloat64(5) should be 5\")\n\t}\n\n\t// Test SignFloat64\n\tif SignFloat64(-5) != -1 {\n\t\tt.Error(\"SignFloat64(-5) should be -1\")\n\t}\n\tif SignFloat64(5) != 1 {\n\t\tt.Error(\"SignFloat64(5) should be 1\")\n\t}\n\tif SignFloat64(0.0) != 0 {\n\t\tt.Error(\"SignFloat64(0.0) should be 0\")\n\t}\n\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"h3NWiQpnliNt3ZkmyKAXkhdn6rri4spRO1IgkUYsJaRbRfX7Mm3Ui+Xr0VeeWaqGVfDqpBpz09WnZCOx1h9nDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"brainfuck","path":"gno.land/p/n2p5/brainfuck","files":[{"name":"brainfuck.gno","body":"// Package brainfuck implements a minimalist Brainfuck language interpreter.\npackage brainfuck\n\n// Run executes a byte slice encoded Brainfuck program with the provided input and\n// returns the output. If no input is desired, use an empty string \"\". Memory is\n// dynamically allocated and grows rightward as needed. The pointer cannot go below\n// zero (does not wrap from the beginning to end). When input is exhausted, the ','\n// command writes 0 to the current cell. Program exits early if unmatched brackets\n// are encountered.\nfunc Run(code, input []byte) []byte {\n\tmemory := []byte{} // dynamic memory tape\n\toutput := []byte{} // collected output bytes\n\tpointer := 0       // memory cell pointer\n\tcursor := 0        // code instruction pointer\n\ti := 0             // input index\n\n\tfor cursor \u003c len(code) {\n\t\tswitch code[cursor] {\n\t\tcase '\u003e': // Move pointer right\n\t\t\tpointer++\n\t\t\tif pointer \u003c= 0 { // Guard against potential overflow\n\t\t\t\tpointer = 0\n\t\t\t}\n\t\t\tensureMemory(\u0026memory, pointer)\n\t\tcase '\u003c': // Move pointer left\n\t\t\tpointer--\n\t\t\tif pointer \u003c= 0 { // Guard against below zero\n\t\t\t\tpointer = 0\n\t\t\t}\n\t\tcase '+': // Increment current cell (wraps: 255+1=0)\n\t\t\tensureMemory(\u0026memory, pointer)\n\t\t\tmemory[pointer]++\n\t\tcase '-': // Decrement current cell (wraps: 0-1=255)\n\t\t\tensureMemory(\u0026memory, pointer)\n\t\t\tmemory[pointer]--\n\t\tcase '.': // Output current cell\n\t\t\tensureMemory(\u0026memory, pointer)\n\t\t\toutput = append(output, memory[pointer])\n\t\tcase ',': // Read one byte from input\n\t\t\tensureMemory(\u0026memory, pointer)\n\t\t\tif i \u003c len(input) {\n\t\t\t\tmemory[pointer] = input[i]\n\t\t\t\ti++\n\t\t\t} else {\n\t\t\t\tmemory[pointer] = 0 // Write 0 when input is exhausted\n\t\t\t}\n\t\tcase '[': // Jump forward to matching ] if current cell is 0\n\t\t\tensureMemory(\u0026memory, pointer)\n\t\t\tif memory[pointer] == 0 {\n\t\t\t\tbrackets := 1 // Track nesting level\n\t\t\t\tfor brackets \u003e 0 {\n\t\t\t\t\tcursor++\n\t\t\t\t\tif cursor \u003e= len(code) {\n\t\t\t\t\t\treturn output // Exit if unmatched bracket\n\t\t\t\t\t}\n\t\t\t\t\tif code[cursor] == '[' {\n\t\t\t\t\t\tbrackets++\n\t\t\t\t\t} else if code[cursor] == ']' {\n\t\t\t\t\t\tbrackets--\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\tcase ']': // Jump back to matching [ if current cell is not 0\n\t\t\tensureMemory(\u0026memory, pointer)\n\t\t\tif memory[pointer] != 0 {\n\t\t\t\tbrackets := 1 // Track nesting level\n\t\t\t\tfor brackets \u003e 0 {\n\t\t\t\t\tcursor--\n\t\t\t\t\tif cursor \u003c 0 {\n\t\t\t\t\t\treturn output // Exit if unmatched bracket\n\t\t\t\t\t}\n\t\t\t\t\tif code[cursor] == ']' {\n\t\t\t\t\t\tbrackets++\n\t\t\t\t\t} else if code[cursor] == '[' {\n\t\t\t\t\t\tbrackets--\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcursor++\n\t\tif cursor \u003c 0 { // Guard cursor against potential overflow\n\t\t\tcursor = 0\n\t\t}\n\t\tif i \u003c 0 { // Guard input index against potential overflow\n\t\t\ti = 0\n\t\t}\n\t}\n\treturn output\n}\n\n// ensureMemory grows the memory slice if needed to accommodate the specified\n// pointer position. New cells are initialized to zero.\nfunc ensureMemory(memory *[]byte, pointer int) {\n\tfor pointer \u003e= len(*memory) {\n\t\t*memory = append(*memory, 0)\n\t}\n}\n"},{"name":"brainfuck_test.gno","body":"package brainfuck\n\nimport (\n\t\"bytes\"\n\t\"testing\"\n)\n\nfunc TestBasicOperations(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tcode     string\n\t\tinput    string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"Increment and Output\",\n\t\t\tcode:     \"+++.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x03\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Decrement and Output\",\n\t\t\tcode:     \"+++--.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x01\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Pointer Movement\",\n\t\t\tcode:     \"\u003e+++\u003e++\u003e+\u003c\u003c\u003c.\u003e.\u003e.\u003e.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x00\\x03\\x02\\x01\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Pointer at Zero Boundary\",\n\t\t\tcode:     \"\u003c\u003c\u003c+++.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x03\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\toutput := Run([]byte(tc.code), []byte(tc.input))\n\t\t\tif !bytes.Equal(output, []byte(tc.expected)) {\n\t\t\t\tt.Errorf(\"Expected %v, got %v\", []byte(tc.expected), output)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestLoops(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tcode     string\n\t\tinput    string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"Simple Loop\",\n\t\t\tcode:     \"+++[\u003e+\u003c-]\u003e.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x03\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Nested Loops\",\n\t\t\tcode:     \"++[\u003e++[\u003e++\u003c-]\u003c-]\u003e\u003e.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x08\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Skip Loop if Zero\",\n\t\t\tcode:     \"[\u003e+++\u003c-]\u003e.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x00\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Unmatched Opening Bracket\",\n\t\t\tcode:     \"[.\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Unmatched Closing Bracket\",\n\t\t\tcode:     \"].\",\n\t\t\tinput:    \"\",\n\t\t\texpected: \"\\x00\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\toutput := Run([]byte(tc.code), []byte(tc.input))\n\t\t\tif !bytes.Equal(output, []byte(tc.expected)) {\n\t\t\t\tt.Errorf(\"Expected %v, got %v\", []byte(tc.expected), output)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestInput(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tcode     string\n\t\tinput    string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"Read Single Input\",\n\t\t\tcode:     \",.\",\n\t\t\tinput:    \"A\",\n\t\t\texpected: \"A\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Read Multiple Inputs\",\n\t\t\tcode:     \",\u003e,\u003e,.\",\n\t\t\tinput:    \"ABC\",\n\t\t\texpected: \"C\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Input Modification\",\n\t\t\tcode:     \",+.\",\n\t\t\tinput:    \"A\",\n\t\t\texpected: \"B\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Input Exhaustion\",\n\t\t\tcode:     \",.,.,.,..\",\n\t\t\tinput:    \"AB\",\n\t\t\texpected: \"AB\\x00\\x00\\x00\",\n\t\t},\n\t\t{\n\t\t\tname:     \"Output Input Stream\",\n\t\t\tcode:     \",[.,]\",\n\t\t\tinput:    \"hello, world!\\n\",\n\t\t\texpected: \"hello, world!\\n\",\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\toutput := Run([]byte(tc.code), []byte(tc.input))\n\t\t\tif !bytes.Equal(output, []byte(tc.expected)) {\n\t\t\t\tt.Errorf(\"Expected %v, got %v\", []byte(tc.expected), output)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestBsort(t *testing.T) {\n\t// [bsort.b -- bubble sort\n\t// (c) 2016 Daniel B. Cristofani\n\t// http://brainfuck.org/]\n\t// [This program sorts the bytes of its input by bubble sort.]\n\tcode := `\n\t\t\u003e\u003e,[\u003e\u003e,]\u003c\u003c[\n\t\t[\u003c\u003c]\u003e\u003e\u003e\u003e[\n\t\t\u003c\u003c[\u003e+\u003c\u003c+\u003e-]\n\t\t\u003e\u003e[\u003e+\u003c\u003c\u003c\u003c[-\u003e]\u003e[\u003c]\u003e\u003e-]\n\t\t\u003c\u003c\u003c[[-]\u003e\u003e[\u003e+\u003c-]\u003e\u003e[\u003c\u003c\u003c+\u003e\u003e\u003e-]]\n\t\t\u003e\u003e[[\u003c+\u003e-]\u003e\u003e]\u003c\n\t\t]\u003c\u003c[\u003e\u003e+\u003c\u003c-]\u003c\u003c\n\t\t]\u003e\u003e\u003e\u003e[.\u003e\u003e]`\n\tinput := \"ckduwcomaz\"\n\texpected := \"accdkmouwz\"\n\n\toutput := Run([]byte(code), []byte(input))\n\tif string(output) != expected {\n\t\tt.Errorf(\"Hello World test failed. Expected '%s', got '%s'\", expected, string(output))\n\t}\n}\n\nfunc TestHelloWorld(t *testing.T) {\n\t// Classic \"Hello World!\" program from wikipedia\n\tcode := `\n+++++ +++++             initialize counter (cell #0) to 10\n[                       use loop to set 70/100/30/10\n    \u003e +++++ ++              add  7 to cell #1\n    \u003e +++++ +++++           add 10 to cell #2\n    \u003e +++                   add  3 to cell #3\n    \u003e +                     add  1 to cell #4\n\u003c\u003c\u003c\u003c -                  decrement counter (cell #0)\n]\n\u003e ++ .                  print 'H'\n\u003e + .                   print 'e'\n+++++ ++ .              print 'l'\n.                       print 'l'\n+++ .                   print 'o'\n\u003e ++ .                  print ' '\n\u003c\u003c +++++ +++++ +++++ .  print 'W'\n\u003e .                     print 'o'\n+++ .                   print 'r'\n----- - .               print 'l'\n----- --- .             print 'd'\n\u003e + .                   print '!'\n\u003e .                     print 'eol'`\n\texpected := \"Hello World!\\n\"\n\n\toutput := Run([]byte(code), []byte{})\n\tif string(output) != expected {\n\t\tt.Errorf(\"Hello World test failed. Expected '%s', got '%s'\", expected, string(output))\n\t}\n}\n\nfunc TestEnsureMemory(t *testing.T) {\n\ttests := []struct {\n\t\tname        string\n\t\tinitialMem  []byte\n\t\tpointer     int\n\t\texpectedLen int\n\t}{\n\t\t{\n\t\t\tname:        \"Already Sufficient\",\n\t\t\tinitialMem:  []byte{0, 0, 0},\n\t\t\tpointer:     2,\n\t\t\texpectedLen: 3,\n\t\t},\n\t\t{\n\t\t\tname:        \"Extend By One\",\n\t\t\tinitialMem:  []byte{0, 0, 0},\n\t\t\tpointer:     3,\n\t\t\texpectedLen: 4,\n\t\t},\n\t\t{\n\t\t\tname:        \"Extend By Many\",\n\t\t\tinitialMem:  []byte{},\n\t\t\tpointer:     10,\n\t\t\texpectedLen: 11,\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tmemory := tc.initialMem\n\t\t\tensureMemory(\u0026memory, tc.pointer)\n\n\t\t\tif len(memory) != tc.expectedLen {\n\t\t\t\tt.Errorf(\"Expected memory length %d, got %d\", tc.expectedLen, len(memory))\n\t\t\t}\n\n\t\t\t// Check that we can access the pointer without panic\n\t\t\t_ = memory[tc.pointer]\n\t\t})\n\t}\n}\n\n// Test various complex programs to ensure the interpreter works correctly\nfunc TestComplexPrograms(t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\tcode     string\n\t\tinput    string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tname:     \"Multiply Two Numbers\",\n\t\t\tcode:     \",\u003e,\u003c[\u003e[-\u003e+\u003e+\u003c\u003c]\u003e\u003e[-\u003c\u003c+\u003e\u003e]\u003c\u003c\u003c-]\u003e\u003e.\",\n\t\t\tinput:    \"\\x03\\x04\", // 3 * 4 = 12\n\t\t\texpected: \"\\x0c\",     // 12 in ASCII\n\t\t},\n\t\t{\n\t\t\tname:     \"Cat Program\",\n\t\t\tcode:     \",[.,]\",\n\t\t\tinput:    \"Hello!\",\n\t\t\texpected: \"Hello!\",\n\t\t},\n\t\t{\n\t\t\tname:     \"ASCII to Decimal\",\n\t\t\tcode:     \",\u003e++++++[\u003c--------\u003e-]\u003c.\", // ASCII value minus 48\n\t\t\tinput:    \"5\",                       // ASCII 53\n\t\t\texpected: \"\\x05\",                    // 5 in decimal\n\t\t},\n\t}\n\n\tfor _, tc := range tests {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\toutput := Run([]byte(tc.code), []byte(tc.input))\n\t\t\tif !bytes.Equal(output, []byte(tc.expected)) {\n\t\t\t\tt.Errorf(\"Expected %v, got %v\", []byte(tc.expected), output)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/n2p5/brainfuck\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"i3Vao8d4zgPwoDokpHwOdPx5/pAL6WX+bA9vsQ/s1MAOs0Aqa+0hryEJCSQCsd8+F5Ly2AwoHMrM9How4+MEDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"chonk","path":"gno.land/p/n2p5/chonk","files":[{"name":"chonk.gno","body":"// Package chonk provides a simple way to store arbitrarily large strings\n// in a linked list across transactions for efficient storage and retrieval.\n// A Chonk support three operations: Add, Flush, and Scanner.\n// - Add appends a string to the Chonk.\n// - Flush clears the Chonk.\n// - Scanner is used to iterate over the chunks in the Chonk.\npackage chonk\n\n// Chonk is a linked list string storage and\n// retrieval system for fine bois.\ntype Chonk struct {\n\tfirst *chunk\n\tlast  *chunk\n}\n\n// chunk is a linked list node for Chonk\ntype chunk struct {\n\ttext string\n\tnext *chunk\n}\n\n// New creates a reference to a new Chonk\nfunc New() *Chonk {\n\treturn \u0026Chonk{}\n}\n\n// Add appends a string to the Chonk. If the Chonk is empty,\n// the string will be the first and last chunk. Otherwise,\n// the string will be appended to the end of the Chonk.\nfunc (c *Chonk) Add(text string) {\n\tnext := \u0026chunk{text: text}\n\tif c.first == nil {\n\t\tc.first = next\n\t\tc.last = next\n\t\treturn\n\t}\n\tc.last.next = next\n\tc.last = next\n}\n\n// Flush clears the Chonk by setting the first and last\n// chunks to nil. This will allow the garbage collector to\n// free the memory used by the Chonk.\nfunc (c *Chonk) Flush() {\n\tc.first = nil\n\tc.last = nil\n}\n\n// Scanner returns a new Scanner for the Chonk. The Scanner\n// is used to iterate over the chunks in the Chonk.\nfunc (c *Chonk) Scanner() *Scanner {\n\treturn \u0026Scanner{\n\t\tnext: c.first,\n\t}\n}\n\n// Scanner is a simple string scanner for Chonk. It is used\n// to iterate over the chunks in a Chonk from first to last.\ntype Scanner struct {\n\tcurrent *chunk\n\tnext    *chunk\n}\n\n// Scan advances the scanner to the next chunk. It returns\n// true if there is a next chunk, and false if there is not.\nfunc (s *Scanner) Scan() bool {\n\tif s.next != nil {\n\t\ts.current = s.next\n\t\ts.next = s.next.next\n\t\treturn true\n\t}\n\treturn false\n}\n\n// Text returns the current chunk. It is only valid to call\n// this method after a call to Scan returns true. Expected usage:\n//\n//\t\tscanner := chonk.Scanner()\n//\t\t\tfor scanner.Scan() {\n//\t    \t\tfmt.Println(scanner.Text())\n//\t\t\t}\nfunc (s *Scanner) Text() string {\n\treturn s.current.text\n}\n"},{"name":"chonk_test.gno","body":"package chonk\n\nimport (\n\t\"testing\"\n)\n\nfunc TestChonk(t *testing.T) {\n\tt.Parallel()\n\tc := New()\n\ttestTable := []struct {\n\t\tname   string\n\t\tchunks []string\n\t}{\n\t\t{\n\t\t\tname:   \"empty\",\n\t\t\tchunks: []string{},\n\t\t},\n\t\t{\n\t\t\tname:   \"single chunk\",\n\t\t\tchunks: []string{\"a\"},\n\t\t},\n\t\t{\n\t\t\tname:   \"multiple chunks\",\n\t\t\tchunks: []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t},\n\t\t{\n\t\t\tname:   \"multiline chunks\",\n\t\t\tchunks: []string{\"1a\\nb\\nc\\n\\n\", \"d\\ne\\nf\", \"g\\nh\\ni\", \"j\\nk\\nl\\n\\n\\n\\n\"},\n\t\t},\n\t\t{\n\t\t\tname:   \"empty\",\n\t\t\tchunks: []string{},\n\t\t},\n\t}\n\ttestChonk := func(t *testing.T, c *Chonk, chunks []string) {\n\t\tfor _, chunk := range chunks {\n\t\t\tc.Add(chunk)\n\t\t}\n\t\tscanner := c.Scanner()\n\t\ti := 0\n\t\tfor scanner.Scan() {\n\t\t\tif scanner.Text() != chunks[i] {\n\t\t\t\tt.Errorf(\"expected %s, got %s\", chunks[i], scanner.Text())\n\t\t\t}\n\t\t\ti++\n\t\t}\n\t}\n\tfor _, test := range testTable {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\ttestChonk(t, c, test.chunks)\n\t\t\tc.Flush()\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/n2p5/chonk\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"dqFrOMD4kQVc7YdvCLpT76MIvqsokCO8arMzs0HVX6acY/6XqQ6IWd3kzkX+Wwn11/S++68J4+Xl998K6txNAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"needle","path":"gno.land/p/n2p5/haystack/needle","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/n2p5/haystack/needle\"\ngno = \"0.9\"\n"},{"name":"needle.gno","body":"package needle\n\nimport (\n\t\"bytes\"\n\t\"crypto/sha256\"\n\t\"errors\"\n)\n\nconst (\n\t// HashLength is the length in bytes of the hash prefix in any message\n\tHashLength = 32\n\t// PayloadLength is the length of the remaining bytes of the message.\n\tPayloadLength = 160\n\t// NeedleLength is the number of bytes required for a valid needle.\n\tNeedleLength = HashLength + PayloadLength\n)\n\n// Needle is a container for a 160 byte payload\n// and a 32 byte sha256 hash of the payload.\ntype Needle struct {\n\thash    [HashLength]byte\n\tpayload [PayloadLength]byte\n}\n\nvar (\n\t// ErrorInvalidHash is an error for in invalid hash\n\tErrorInvalidHash = errors.New(\"invalid hash\")\n\t// ErrorByteSliceLength is an error for an invalid byte slice length passed in to New or FromBytes\n\tErrorByteSliceLength = errors.New(\"invalid byte slice length\")\n)\n\n// New creates a Needle used for submitting a payload to a Haystack sever. It takes a Payload\n// byte slice that is 160 bytes in length and returns a reference to a\n// Needle and an error. The purpose of this function is to make it\n// easy to create a new Needle from a payload. This function handles creating a sha256\n// hash of the payload, which is used by the Needle to submit to a haystack server.\nfunc New(p []byte) (*Needle, error) {\n\tif len(p) != PayloadLength {\n\t\treturn nil, ErrorByteSliceLength\n\t}\n\tvar n Needle\n\tsum := sha256.Sum256(p)\n\tcopy(n.hash[:], sum[:])\n\tcopy(n.payload[:], p)\n\treturn \u0026n, nil\n}\n\n// FromBytes is intended convert raw bytes (from UDP or storage) into a Needle.\n// It takes a byte slice and expects it to be exactly the length of NeedleLength.\n// The byte slice should consist of the first 32 bytes being the sha256 hash of the\n// payload and the payload bytes. This function verifies the length of the byte slice,\n// copies the bytes into a private [192]byte array, and validates the Needle. It returns\n// a reference to a Needle and an error.\nfunc FromBytes(b []byte) (*Needle, error) {\n\tif len(b) != NeedleLength {\n\t\treturn nil, ErrorByteSliceLength\n\t}\n\tvar n Needle\n\tcopy(n.hash[:], b[:HashLength])\n\tcopy(n.payload[:], b[HashLength:])\n\tif err := n.validate(); err != nil {\n\t\treturn nil, err\n\t}\n\treturn \u0026n, nil\n}\n\n// Hash returns a copy of the bytes of the sha256 256 hash of the Needle payload.\nfunc (n *Needle) Hash() []byte {\n\treturn n.Bytes()[:HashLength]\n}\n\n// Payload returns a byte slice of the Needle payload\nfunc (n *Needle) Payload() []byte {\n\treturn n.Bytes()[HashLength:]\n}\n\n// Bytes returns a byte slice of the entire 192 byte hash + payload\nfunc (n *Needle) Bytes() []byte {\n\tb := make([]byte, NeedleLength)\n\tcopy(b, n.hash[:])\n\tcopy(b[HashLength:], n.payload[:])\n\treturn b\n}\n\n// validate checks that a Needle has a valid hash, it returns either nil or an error.\nfunc (n *Needle) validate() error {\n\tif hash := sha256.Sum256(n.Payload()); !bytes.Equal(n.Hash(), hash[:]) {\n\t\treturn ErrorInvalidHash\n\t}\n\treturn nil\n}\n"},{"name":"needle_test.gno","body":"package needle\n\nimport (\n\t\"bytes\"\n\t\"crypto/sha256\"\n\t\"encoding/hex\"\n\t\"testing\"\n)\n\nfunc TestNeedle(t *testing.T) {\n\tt.Parallel()\n\tt.Run(\"Bytes\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\tp, _ := hex.DecodeString(\"40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\t\tn, _ := New(p)\n\t\tb := n.Bytes()\n\t\tb[0], b[1], b[2], b[3] = 0, 0, 0, 0\n\t\tif bytes.Equal(n.Bytes(), b) {\n\t\t\tt.Error(\"mutating Bytes() changed needle bytes\")\n\t\t}\n\t})\n\tt.Run(\"Payload\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\tp, _ := hex.DecodeString(\"40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\t\tn, _ := New(p)\n\t\tpayload := n.Payload()\n\t\tif !bytes.Equal(p, payload) {\n\t\t\tt.Error(\"payload imported by New does not match needle.Payload()\")\n\t\t}\n\t\tpayload[0] = 0\n\t\tpl := n.Payload()\n\t\tif bytes.Equal(pl, payload) {\n\t\t\tt.Error(\"mutating Payload() changed needle payload\")\n\t\t}\n\t})\n\tt.Run(\"Hash\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\tp, _ := hex.DecodeString(\"40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\t\tn, _ := New(p)\n\t\thash := n.Hash()\n\t\th := sha256.Sum256(p)\n\t\tif !bytes.Equal(h[:], hash) {\n\t\t\tt.Error(\"exported hash is invalid\")\n\t\t}\n\t\thash[0] = 0\n\t\th2 := n.Hash()\n\t\tif bytes.Equal(h2, hash) {\n\t\t\tt.Error(\"mutating Hash() changed needle hash\")\n\t\t}\n\t})\n}\n\nfunc TestNew(t *testing.T) {\n\tt.Parallel()\n\n\tp, _ := hex.DecodeString(\"40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\texpected, _ := hex.DecodeString(\"f1b462c84a0c51dad44293951f0b084a8871b3700ac1b9fc7a53a20bc0ba0fed40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\n\ttestTable := []struct {\n\t\tpayload     []byte\n\t\texpected    []byte\n\t\thasError    bool\n\t\tdescription string\n\t}{\n\t\t{\n\t\t\tpayload:     p,\n\t\t\texpected:    expected,\n\t\t\thasError:    false,\n\t\t\tdescription: \"expected payload\",\n\t\t},\n\t\t{\n\t\t\tpayload:     p[:PayloadLength-1],\n\t\t\texpected:    nil,\n\t\t\thasError:    true,\n\t\t\tdescription: \"payload invalid length (too small)\",\n\t\t},\n\t\t{\n\t\t\tpayload:     append(p, byte(1)),\n\t\t\texpected:    nil,\n\t\t\thasError:    true,\n\t\t\tdescription: \"payload invalid length (too large)\",\n\t\t},\n\t}\n\n\tfor _, test := range testTable {\n\t\tn, err := New(test.payload)\n\t\tif err != nil {\n\t\t\tif !test.hasError {\n\t\t\t\tt.Errorf(\"test: %v had error: %v\", test.description, err)\n\t\t\t}\n\t\t} else if !bytes.Equal(n.Bytes(), test.expected) {\n\t\t\tt.Errorf(\"%v, bytes not equal\\n%x\\n%x\", test.description, n.Bytes(), test.expected)\n\t\t}\n\t}\n}\n\nfunc TestFromBytes(t *testing.T) {\n\tt.Parallel()\n\n\tvalidRaw, _ := hex.DecodeString(\"f1b462c84a0c51dad44293951f0b084a8871b3700ac1b9fc7a53a20bc0ba0fed40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\tvalidExpected, _ := hex.DecodeString(\"f1b462c84a0c51dad44293951f0b084a8871b3700ac1b9fc7a53a20bc0ba0fed40e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\tinvalidHash, _ := hex.DecodeString(\"182e0ca0d2fb1da76da6caf36a9d0d2838655632e85891216dc8b545d8f1410940e4350b03d8b0c9e340321210b259d9a20b19632929b4a219254a4269c11f820c75168c6a91d309f4b134a7d715a5ac408991e1cf9415995053cf8a4e185dae22a06617ac51ebf7d232bc49e567f90be4db815c2b88ca0d9a4ef7a5119c0e592c88dfb96706e6510fb8a657c0f70f6695ea310d24786e6d980e9b33cf2665342b965b2391f6bb982c4c5f6058b9cba58038d32452e07cdee9420a8bd7f514e1\")\n\n\ttestTable := []struct {\n\t\trawBytes    []byte\n\t\texpected    []byte\n\t\thasError    bool\n\t\tdescription string\n\t}{\n\t\t{\n\t\t\trawBytes:    validRaw,\n\t\t\texpected:    validExpected,\n\t\t\thasError:    false,\n\t\t\tdescription: \"valid raw bytes\",\n\t\t},\n\t\t{\n\t\t\trawBytes:    make([]byte, 0),\n\t\t\texpected:    nil,\n\t\t\thasError:    true,\n\t\t\tdescription: \"empty bytes\",\n\t\t},\n\t\t{\n\t\t\trawBytes:    make([]byte, NeedleLength-1),\n\t\t\texpected:    nil,\n\t\t\thasError:    true,\n\t\t\tdescription: \"too few bytes, one less than expected\",\n\t\t},\n\t\t{\n\t\t\trawBytes:    make([]byte, 0),\n\t\t\texpected:    nil,\n\t\t\thasError:    true,\n\t\t\tdescription: \"too few bytes, no bytes\",\n\t\t},\n\t\t{\n\t\t\trawBytes:    make([]byte, NeedleLength+1),\n\t\t\texpected:    nil,\n\t\t\thasError:    true,\n\t\t\tdescription: \"too many bytes\",\n\t\t},\n\t\t{\n\t\t\trawBytes:    invalidHash,\n\t\t\texpected:    nil,\n\t\t\thasError:    true,\n\t\t\tdescription: \"invalid hash\",\n\t\t},\n\t}\n\tfor _, test := range testTable {\n\t\tn, err := FromBytes(test.rawBytes)\n\t\tif err != nil {\n\t\t\tif !test.hasError {\n\t\t\t\tt.Errorf(\"test: %v had error: %v\", test.description, err)\n\t\t\t}\n\t\t} else if !bytes.Equal(n.Bytes(), test.expected) {\n\t\t\tt.Errorf(\"%v, bytes not equal\\n%x\\n%x\", test.description, n.Bytes(), test.expected)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"eWrzh+stBPFXUWyIFRMVazmDpTjxVOohaYQAQ3FDPqELZkpEBAktjhDjljfJJ4z0GbVubYkXADbeAu9ih3xFCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"haystack","path":"gno.land/p/n2p5/haystack","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/n2p5/haystack\"\ngno = \"0.9\"\n"},{"name":"haystack.gno","body":"package haystack\n\nimport (\n\t\"encoding/hex\"\n\t\"errors\"\n\n\t\"gno.land/p/n2p5/haystack/needle\"\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar (\n\t// ErrorNeedleNotFound is returned when a needle is not found in the haystack.\n\tErrorNeedleNotFound = errors.New(\"needle not found\")\n\t// ErrorNeedleLength is returned when a needle is not the correct length.\n\tErrorNeedleLength = errors.New(\"invalid needle length\")\n\t// ErrorHashLength is returned when a needle hash is not the correct length.\n\tErrorHashLength = errors.New(\"invalid hash length\")\n\t// ErrorDuplicateNeedle is returned when a needle already exists in the haystack.\n\tErrorDuplicateNeedle = errors.New(\"needle already exists\")\n\t// ErrorHashMismatch is returned when a needle hash does not match the needle. This should\n\t// never happen and indicates a critical internal storage error.\n\tErrorHashMismatch = errors.New(\"storage error: hash mismatch\")\n\t// ErrorValueInvalidType is returned when a needle value is not a byte slice. This should\n\t// never happen and indicates a critical internal storage error.\n\tErrorValueInvalidType = errors.New(\"storage error: invalid value type, expected []byte\")\n)\n\nconst (\n\t// EncodedHashLength is the length of the hex-encoded needle hash.\n\tEncodedHashLength = needle.HashLength * 2\n\t// EncodedPayloadLength is the length of the hex-encoded needle payload.\n\tEncodedPayloadLength = needle.PayloadLength * 2\n\t// EncodedNeedleLength is the length of the hex-encoded needle.\n\tEncodedNeedleLength = EncodedHashLength + EncodedPayloadLength\n)\n\n// Haystack is a permissionless, append-only, content-addressed key-value store for fix\n// length messages known as needles. A needle is a 192 byte byte slice with a 32 byte\n// hash (sha256) and a 160 byte payload.\ntype Haystack struct{ internal *avl.Tree }\n\n// New creates a new instance of a Haystack key-value store.\nfunc New() *Haystack {\n\treturn \u0026Haystack{\n\t\tinternal: avl.NewTree(),\n\t}\n}\n\n// Add takes a fixed-length hex-encoded needle bytes and adds it to the haystack key-value\n// store. The key is the first 32 bytes of the needle hash (64 bytes hex-encoded) of the\n// sha256 sum of the payload. The value is the 160 byte byte slice of the needle payload.\n// An error is returned if the needle is found to be invalid.\nfunc (h *Haystack) Add(needleHex string) error {\n\tif len(needleHex) != EncodedNeedleLength {\n\t\treturn ErrorNeedleLength\n\t}\n\tb, err := hex.DecodeString(needleHex)\n\tif err != nil {\n\t\treturn err\n\t}\n\tn, err := needle.FromBytes(b)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif h.internal.Has(needleHex[:EncodedHashLength]) {\n\t\treturn ErrorDuplicateNeedle\n\t}\n\th.internal.Set(needleHex[:EncodedHashLength], n.Payload())\n\treturn nil\n}\n\n// Get takes a hex-encoded needle hash and returns the complete hex-encoded needle bytes\n// and an error. Errors covers errors that span from the needle not being found, internal\n// storage error inconsistencies, and invalid value types.\nfunc (h *Haystack) Get(hash string) (string, error) {\n\tif len(hash) != EncodedHashLength {\n\t\treturn \"\", ErrorHashLength\n\t}\n\tif _, err := hex.DecodeString(hash); err != nil {\n\t\treturn \"\", err\n\t}\n\tv := h.internal.Get(hash)\n\tif v == nil {\n\t\treturn \"\", ErrorNeedleNotFound\n\t}\n\tb, ok := v.([]byte)\n\tif !ok {\n\t\treturn \"\", ErrorValueInvalidType\n\t}\n\tn, err := needle.New(b)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tneedleHash := hex.EncodeToString(n.Hash())\n\tif needleHash != hash {\n\t\treturn \"\", ErrorHashMismatch\n\t}\n\treturn hex.EncodeToString(n.Bytes()), nil\n}\n"},{"name":"haystack_test.gno","body":"package haystack\n\nimport (\n\t\"encoding/hex\"\n\t\"testing\"\n\n\t\"gno.land/p/n2p5/haystack/needle\"\n)\n\nfunc TestHaystack(t *testing.T) {\n\tt.Parallel()\n\n\tt.Run(\"New\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\th := New()\n\t\tif h == nil {\n\t\t\tt.Error(\"New returned nil\")\n\t\t}\n\t})\n\n\tt.Run(\"Add\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\th := New()\n\t\tn, _ := needle.New(make([]byte, needle.PayloadLength))\n\t\tvalidNeedleHex := hex.EncodeToString(n.Bytes())\n\n\t\ttestTable := []struct {\n\t\t\tneedleHex string\n\t\t\terr       error\n\t\t}{\n\t\t\t{validNeedleHex, nil},\n\t\t\t{validNeedleHex, ErrorDuplicateNeedle},\n\t\t\t{\"bad\" + validNeedleHex[3:], needle.ErrorInvalidHash},\n\t\t\t{\"XXX\" + validNeedleHex[3:], hex.InvalidByteError('X')},\n\t\t\t{validNeedleHex[:len(validNeedleHex)-2], ErrorNeedleLength},\n\t\t\t{validNeedleHex + \"00\", ErrorNeedleLength},\n\t\t\t{\"000\", ErrorNeedleLength},\n\t\t}\n\t\tfor _, tt := range testTable {\n\t\t\terr := h.Add(tt.needleHex)\n\t\t\tif err != tt.err {\n\t\t\t\tt.Error(tt.needleHex, err.Error(), \"!=\", tt.err.Error())\n\t\t\t}\n\t\t}\n\t})\n\n\tt.Run(\"Get\", func(t *testing.T) {\n\t\tt.Parallel()\n\t\th := New()\n\n\t\t// genNeedleHex returns a hex-encoded needle and its hash for a given index.\n\t\tgenNeedleHex := func(i int) (string, string) {\n\t\t\tb := make([]byte, needle.PayloadLength)\n\t\t\tb[0] = byte(i)\n\t\t\tn, _ := needle.New(b)\n\t\t\treturn hex.EncodeToString(n.Bytes()), hex.EncodeToString(n.Hash())\n\t\t}\n\n\t\t// Add a valid needle to the haystack.\n\t\tvalidNeedleHex, validHash := genNeedleHex(0)\n\t\th.Add(validNeedleHex)\n\n\t\t// Add a needle and break the value type.\n\t\t_, brokenHashValueType := genNeedleHex(1)\n\t\th.internal.Set(brokenHashValueType, 0)\n\n\t\t// Add a needle with invalid hash.\n\t\t_, invalidHash := genNeedleHex(2)\n\t\th.internal.Set(invalidHash, make([]byte, needle.PayloadLength))\n\n\t\ttestTable := []struct {\n\t\t\thash     string\n\t\t\texpected string\n\t\t\terr      error\n\t\t}{\n\t\t\t{validHash, validNeedleHex, nil},\n\t\t\t{validHash[:len(validHash)-2], \"\", ErrorHashLength},\n\t\t\t{validHash + \"00\", \"\", ErrorHashLength},\n\t\t\t{\"XXX\" + validHash[3:], \"\", hex.InvalidByteError('X')},\n\t\t\t{\"bad\" + validHash[3:], \"\", ErrorNeedleNotFound},\n\t\t\t{brokenHashValueType, \"\", ErrorValueInvalidType},\n\t\t\t{invalidHash, \"\", ErrorHashMismatch},\n\t\t}\n\t\tfor _, tt := range testTable {\n\t\t\tactual, err := h.Get(tt.hash)\n\t\t\tif err != tt.err {\n\t\t\t\tt.Error(tt.hash, err.Error(), \"!=\", tt.err.Error())\n\t\t\t}\n\t\t\tif actual != tt.expected {\n\t\t\t\tt.Error(tt.hash, actual, \"!=\", tt.expected)\n\t\t\t}\n\t\t}\n\t})\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"D+bibsiX8364YuzsYF0L3NaFo9yvS4I71Yh/mcDS9i6b9Xbsq51yImVtKfsLLcHrQVkGDFYJPRDWZuGBM5PkDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"loci","path":"gno.land/p/n2p5/loci","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/n2p5/loci\"\ngno = \"0.9\"\n"},{"name":"loci.gno","body":"// loci is a single purpose datastore keyed by the caller's address. It has two\n// functions: Set and Get. loci is plural for locus, which is a central or core\n// place where something is found or from which it originates. In this case,\n// it's a simple key-value store where an address (the key) can store exactly\n// one value (in the form of a byte slice). Only the caller can set the value\n// for their address, but anyone can retrieve the value for any address.\npackage loci\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// LociStore is a simple key-value store that uses\n// an AVL tree to store the data.\ntype LociStore struct {\n\tinternal *avl.Tree\n}\n\n// New creates a reference to a new LociStore.\nfunc New() *LociStore {\n\treturn \u0026LociStore{\n\t\tinternal: avl.NewTree(),\n\t}\n}\n\n// Set stores a byte slice in the AVL tree using `rlm.Previous().Address()`\n// as the key. rlm must be the caller's own captured cur (asserted via\n// rlm.IsCurrent()); the previous walk used to be unsafe.PreviousRealm()\n// which is forgeable from non-crossing wrappers — an attacker realm\n// could overwrite another realm's slot. With IsCurrent + rlm.Previous(),\n// the slot key is unforgeable.\nfunc (s *LociStore) Set(_ int, rlm realm, value []byte) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"loci: rlm does not match the current crossing frame\")\n\t}\n\tkey := string(rlm.Previous().Address())\n\ts.internal.Set(key, value)\n}\n\n// Get retrieves a byte slice from the AVL tree using the provided address.\n// The return values are the byte slice value and a boolean indicating\n// whether the value exists.\nfunc (s *LociStore) Get(addr address) []byte {\n\tvalue := s.internal.Get(string(addr))\n\tif value == nil {\n\t\treturn nil\n\t}\n\treturn value.([]byte)\n}\n"},{"name":"loci_test.gno","body":"package loci\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nfunc TestLociStore(cur realm, t *testing.T) {\n\tcaller := testutils.TestAddress(\"caller\")\n\n\tt.Run(\"TestSet\", func(cur realm, t *testing.T) {\n\t\ttesting.SetRealm(testing.NewUserRealm(caller))\n\t\tstore := New()\n\t\t// Ensure that the value is nil before setting it.\n\t\tif r1 := store.Get(caller); r1 != nil {\n\t\t\tt.Errorf(\"expected value to be nil, got '%s'\", r1)\n\t\t}\n\t\tfunc(cur realm) { store.Set(0, cur, []byte(\"hello\")) }(cross(cur))\n\t\tif r2 := store.Get(caller); string(r2) != \"hello\" {\n\t\t\tt.Errorf(\"expected value to be 'hello', got '%s'\", r2)\n\t\t}\n\t\tfunc(cur realm) { store.Set(0, cur, []byte(\"world\")) }(cross(cur))\n\t\tif r3 := store.Get(caller); string(r3) != \"world\" {\n\t\t\tt.Errorf(\"expected value to be 'world', got '%s'\", r3)\n\t\t}\n\t})\n\tt.Run(\"TestGet\", func(cur realm, t *testing.T) {\n\t\ttesting.SetRealm(testing.NewUserRealm(caller))\n\t\tstore := New()\n\t\tfunc(cur realm) { store.Set(0, cur, []byte(\"hello\")) }(cross(cur))\n\t\tif r0 := store.Get(testutils.TestAddress(\"nil_user\")); r0 != nil {\n\t\t\tt.Errorf(\"expected value to be nil, got '%s'\", r0)\n\t\t}\n\t\tif r1 := store.Get(caller); string(r1) != \"hello\" {\n\t\t\tt.Errorf(\"expected value to be 'hello', got '%s'\", r1)\n\t\t}\n\t})\n}\n"},{"name":"z_0_filetest.gno","body":"// PKGPATH: gno.land/r/test\npackage test\n\nimport (\n\t\"gno.land/p/n2p5/loci\"\n)\n\nvar store *loci.LociStore\n\nfunc init() {\n\tstore = loci.New()\n}\n\nfunc main(cur realm) {\n\tstore.Set(0, cur, []byte(\"hello, world\"))\n\tprintln(string(store.Get(cur.Previous().Address())))\n}\n\n// Output:\n// hello, world\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"eqamMHTp0S6FBDE59ZQ+bCMndo/fEGEu7AY8eUq0cUX0gH5BrA2/iKw5rftutYbMX1bONxES0crCIB2Vk8tBBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"mgroup","path":"gno.land/p/n2p5/mgroup","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/n2p5/mgroup\"\ngno = \"0.9\"\n"},{"name":"mgroup.gno","body":"// Package mgroup is a simple managed group managing ownership and membership\n// for authorization in gno realms. The ManagedGroup struct is used to manage\n// the owner, backup owners, and members of a group. The owner is the primary\n// owner of the group and can add and remove backup owners and members. Backup\n// owners can claim ownership of the group. This is meant to provide backup\n// accounts for the owner in case the owner account is lost or compromised.\n// Members are used to authorize actions across realms.\npackage mgroup\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\nvar (\n\tErrCannotRemoveOwner = errors.New(\"mgroup: cannot remove owner\")\n\tErrNotBackupOwner    = errors.New(\"mgroup: not a backup owner\")\n\tErrNotMember         = errors.New(\"mgroup: not a member\")\n\tErrInvalidAddress    = errors.New(\"mgroup: address is invalid\")\n)\n\ntype ManagedGroup struct {\n\towner        *ownable.Ownable\n\tbackupOwners *avl.Tree\n\tmembers      *avl.Tree\n}\n\n// New creates a new ManagedGroup with the owner set to the provided address.\n// The owner is automatically added as a backup owner and member of the group.\nfunc New(ownerAddress address) *ManagedGroup {\n\tg := \u0026ManagedGroup{\n\t\towner:        ownable.NewWithAddress(ownerAddress),\n\t\tbackupOwners: avl.NewTree(),\n\t\tmembers:      avl.NewTree(),\n\t}\n\terr := g.addBackupOwner(ownerAddress)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = g.addMember(ownerAddress)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn g\n}\n\n// AddBackupOwner adds a backup owner to the group by address. rlm must\n// be the caller's own captured cur (asserted via rlm.IsCurrent());\n// rlm.Previous().Address() must equal the owner.\nfunc (g *ManagedGroup) AddBackupOwner(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !g.owner.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\treturn g.addBackupOwner(addr)\n}\n\nfunc (g *ManagedGroup) addBackupOwner(addr address) error {\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tg.backupOwners.Set(addr.String(), struct{}{})\n\treturn nil\n}\n\n// RemoveBackupOwner removes a backup owner from the group by address.\n// The owner cannot be removed. rlm must be the caller's own captured\n// cur; rlm.Previous().Address() must equal the owner.\nfunc (g *ManagedGroup) RemoveBackupOwner(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !g.owner.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tif addr == g.Owner() {\n\t\treturn ErrCannotRemoveOwner\n\t}\n\tg.backupOwners.Remove(addr.String())\n\treturn nil\n}\n\n// ClaimOwnership allows a backup owner to claim ownership of the group.\n// rlm must be the caller's own captured cur; the claimant is\n// rlm.Previous().Address(), which must already be a backup owner. The\n// claimant is automatically added as a member of the group.\nfunc (g *ManagedGroup) ClaimOwnership(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tcaller := rlm.Previous().Address()\n\t// already owner, skip\n\tif caller == g.Owner() {\n\t\treturn nil\n\t}\n\tif !g.IsBackupOwner(caller) {\n\t\treturn ErrNotMember\n\t}\n\tg.owner = ownable.NewWithAddress(caller)\n\treturn g.addMember(caller)\n}\n\n// AddMember adds a member to the group by address. rlm must be the\n// caller's own captured cur; rlm.Previous().Address() must equal the\n// owner.\nfunc (g *ManagedGroup) AddMember(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !g.owner.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\treturn g.addMember(addr)\n}\n\nfunc (g *ManagedGroup) addMember(addr address) error {\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tg.members.Set(addr.String(), struct{}{})\n\treturn nil\n}\n\n// RemoveMember removes a member from the group by address. The owner\n// cannot be removed. rlm must be the caller's own captured cur;\n// rlm.Previous().Address() must equal the owner.\nfunc (g *ManagedGroup) RemoveMember(_ int, rlm realm, addr address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !g.owner.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\tif addr == g.Owner() {\n\t\treturn ErrCannotRemoveOwner\n\t}\n\tg.members.Remove(addr.String())\n\treturn nil\n}\n\n// MemberCount returns the number of members in the group.\nfunc (g *ManagedGroup) MemberCount() int {\n\treturn g.members.Size()\n}\n\n// BackupOwnerCount returns the number of backup owners in the group.\nfunc (g *ManagedGroup) BackupOwnerCount() int {\n\treturn g.backupOwners.Size()\n}\n\n// IsMember checks if an address is a member of the group.\nfunc (g *ManagedGroup) IsMember(addr address) bool {\n\treturn g.members.Has(addr.String())\n}\n\n// IsBackupOwner checks if an address is a backup owner in the group.\nfunc (g *ManagedGroup) IsBackupOwner(addr address) bool {\n\treturn g.backupOwners.Has(addr.String())\n}\n\n// Owner returns the owner of the group.\nfunc (g *ManagedGroup) Owner() address {\n\treturn g.owner.Owner()\n}\n\n// BackupOwners returns a slice of all backup owners in the group, using the underlying\n// avl.Tree to iterate over the backup owners. If you have a large group, you may\n// want to use BackupOwnersWithOffset to iterate over backup owners in chunks.\nfunc (g *ManagedGroup) BackupOwners() []string {\n\treturn g.BackupOwnersWithOffset(0, g.BackupOwnerCount())\n}\n\n// Members returns a slice of all members in the group, using the underlying\n// avl.Tree to iterate over the members. If you have a large group, you may\n// want to use MembersWithOffset to iterate over members in chunks.\nfunc (g *ManagedGroup) Members() []string {\n\treturn g.MembersWithOffset(0, g.MemberCount())\n}\n\n// BackupOwnersWithOffset returns a slice of backup owners in the group, using the underlying\n// avl.Tree to iterate over the backup owners. The offset and count parameters allow you\n// to iterate over backup owners in chunks to support patterns such as pagination.\nfunc (g *ManagedGroup) BackupOwnersWithOffset(offset, count int) []string {\n\treturn sliceWithOffset(g.backupOwners, offset, count)\n}\n\n// MembersWithOffset returns a slice of members in the group, using the underlying\n// avl.Tree to iterate over the members. The offset and count parameters allow you\n// to iterate over members in chunks to support patterns such as pagination.\nfunc (g *ManagedGroup) MembersWithOffset(offset, count int) []string {\n\treturn sliceWithOffset(g.members, offset, count)\n}\n\n// sliceWithOffset is a helper function to iterate over an avl.Tree with an offset and count.\nfunc sliceWithOffset(t *avl.Tree, offset, count int) []string {\n\tvar result []string\n\tt.IterateByOffset(offset, count, func(k string, _ any) bool {\n\t\tif k == \"\" {\n\t\t\treturn true\n\t\t}\n\t\tresult = append(result, k)\n\t\treturn false\n\t})\n\treturn result\n}\n"},{"name":"mgroup_test.gno","body":"package mgroup\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestManagedGroup(cur realm, t *testing.T) {\n\tu1 := testutils.TestAddress(\"u1\")\n\tu2 := testutils.TestAddress(\"u2\")\n\tu3 := testutils.TestAddress(\"u3\")\n\tu4 := testutils.TestAddress(\"u4\")\n\n\tt.Run(\"AddBackupOwner\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tif err := g.AddBackupOwner(0, cur, u2); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t\t// ensure invalid address is caught\n\t\t\tvar badAddr address\n\t\t\tif err := g.AddBackupOwner(0, cur, badAddr); err != ErrInvalidAddress {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ErrInvalidAddress.Error(), err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\n\t\ttesting.SetRealm(testing.NewUserRealm(u3))\n\t\tfunc(cur realm) {\n\t\t\t// an address that is not the owner should not be allowed to add a backup owner\n\t\t\tif err := g.AddBackupOwner(0, cur, u4); err != ownable.ErrUnauthorized {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ownable.ErrUnauthorized, err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\n\t})\n\tt.Run(\"RemoveBackupOwner\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tg.AddBackupOwner(0, cur, u2)\n\t\t\tif err := g.RemoveBackupOwner(0, cur, u2); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t\t// running this twice should not error.\n\t\t\tif err := g.RemoveBackupOwner(0, cur, u2); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t\tvar badAddr address\n\t\t\tif err := g.RemoveBackupOwner(0, cur, badAddr); err != ErrInvalidAddress {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ErrInvalidAddress.Error(), err.Error())\n\t\t\t}\n\t\t\tif err := g.RemoveBackupOwner(0, cur, u1); err != ErrCannotRemoveOwner {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ErrCannotRemoveOwner.Error(), err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\n\t\ttesting.SetRealm(testing.NewUserRealm(u3))\n\t\tfunc(cur realm) {\n\t\t\tg.AddBackupOwner(0, cur, u2)\n\t\t\tif err := g.RemoveBackupOwner(0, cur, u2); err != ownable.ErrUnauthorized {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ownable.ErrUnauthorized, err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\n\tt.Run(\"ClaimOwnership\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tg.AddBackupOwner(0, cur, u2)\n\t\t}(cross(cur))\n\t\ttesting.SetRealm(testing.NewUserRealm(u2))\n\t\tfunc(cur realm) {\n\t\t\tif err := g.ClaimOwnership(0, cur); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t\tif g.Owner() != u2 {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", u2, g.Owner())\n\t\t\t}\n\t\t\tif !g.IsMember(u2) {\n\t\t\t\tt.Errorf(\"expected %v to be a member\", u2)\n\t\t\t}\n\t\t\tif err := g.ClaimOwnership(0, cur); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\t\ttesting.SetRealm(testing.NewUserRealm(u3))\n\t\tfunc(cur realm) {\n\t\t\tif err := g.ClaimOwnership(0, cur); err != ErrNotMember {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ErrNotMember.Error(), err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\n\tt.Run(\"AddMember\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tif err := g.AddMember(0, cur, u2); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t\tif !g.IsMember(u2) {\n\t\t\t\tt.Errorf(\"expected %v to be a member\", u2)\n\t\t\t}\n\t\t\tvar badAddr address\n\t\t\tif err := g.AddMember(0, cur, badAddr); err != ErrInvalidAddress {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ErrInvalidAddress, err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\t\ttesting.SetRealm(testing.NewUserRealm(u3))\n\t\tfunc(cur realm) {\n\t\t\tif err := g.AddMember(0, cur, u4); err != ownable.ErrUnauthorized {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ownable.ErrUnauthorized, err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\n\tt.Run(\"RemoveMember\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tg.AddMember(0, cur, u2)\n\t\t\tif err := g.RemoveMember(0, cur, u2); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t\tif g.IsMember(u2) {\n\t\t\t\tt.Errorf(\"expected %v to not be a member\", u2)\n\t\t\t}\n\t\t\t// running this twice should not error\n\t\t\tif err := g.RemoveMember(0, cur, u2); err != nil {\n\t\t\t\tt.Errorf(\"expected nil, got %v\", err.Error())\n\t\t\t}\n\t\t\tvar badAddr address\n\t\t\tif err := g.RemoveMember(0, cur, badAddr); err != ErrInvalidAddress {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ErrInvalidAddress.Error(), err.Error())\n\t\t\t}\n\t\t\tif err := g.RemoveMember(0, cur, u1); err != ErrCannotRemoveOwner {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ErrCannotRemoveOwner.Error(), err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\n\t\ttesting.SetRealm(testing.NewUserRealm(u2))\n\t\tfunc(cur realm) {\n\t\t\tif err := g.RemoveMember(0, cur, u3); err != ownable.ErrUnauthorized {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", ownable.ErrUnauthorized.Error(), err.Error())\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\n\tt.Run(\"MemberCount\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tuassert.Equal(t, 1, g.MemberCount())\n\t\t\tg.AddMember(0, cur, u2)\n\t\t\tuassert.Equal(t, 2, g.MemberCount())\n\t\t\tg.AddMember(0, cur, u3)\n\t\t\tuassert.Equal(t, 3, g.MemberCount())\n\t\t\tg.RemoveMember(0, cur, u2)\n\t\t\tuassert.Equal(t, 2, g.MemberCount())\n\t\t}(cross(cur))\n\t})\n\n\tt.Run(\"BackupOwnerCount\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tuassert.Equal(t, 1, g.BackupOwnerCount())\n\t\t\tg.AddBackupOwner(0, cur, u2)\n\t\t\tuassert.Equal(t, 2, g.BackupOwnerCount())\n\t\t\tg.AddBackupOwner(0, cur, u3)\n\t\t\tuassert.Equal(t, 3, g.BackupOwnerCount())\n\t\t\tg.RemoveBackupOwner(0, cur, u2)\n\t\t\tuassert.Equal(t, 2, g.BackupOwnerCount())\n\t\t}(cross(cur))\n\t})\n\n\tt.Run(\"IsMember\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tif !g.IsMember(u1) {\n\t\t\t\tt.Errorf(\"expected %v to be a member\", u1)\n\t\t\t}\n\t\t\tif g.IsMember(u2) {\n\t\t\t\tt.Errorf(\"expected %v to not be a member\", u2)\n\t\t\t}\n\t\t\tg.AddMember(0, cur, u2)\n\t\t\tif !g.IsMember(u2) {\n\t\t\t\tt.Errorf(\"expected %v to be a member\", u2)\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\tt.Run(\"IsBackupOwner\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tif !g.IsBackupOwner(u1) {\n\t\t\t\tt.Errorf(\"expected %v to be a backup owner\", u1)\n\t\t\t}\n\t\t\tif g.IsBackupOwner(u2) {\n\t\t\t\tt.Errorf(\"expected %v to not be a backup owner\", u2)\n\t\t\t}\n\t\t\tg.AddBackupOwner(0, cur, u2)\n\t\t\tif !g.IsBackupOwner(u2) {\n\t\t\t\tt.Errorf(\"expected %v to be a backup owner\", u2)\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\n\tt.Run(\"Owner\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tif g.Owner() != u1 {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", u1, g.Owner())\n\t\t\t}\n\t\t\tg.AddBackupOwner(0, cur, u2)\n\t\t\tif g.Owner() != u1 {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", u1, g.Owner())\n\t\t\t}\n\t\t}(cross(cur))\n\t\ttesting.SetRealm(testing.NewUserRealm(u2))\n\t\tfunc(cur realm) {\n\t\t\tg.ClaimOwnership(0, cur)\n\t\t\tif g.Owner() != u2 {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", u2, g.Owner())\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\tt.Run(\"BackupOwners\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tg.AddBackupOwner(0, cur, u2)\n\t\t\tg.AddBackupOwner(0, cur, u3)\n\t\t\towners := g.BackupOwners()\n\t\t\tif len(owners) != 3 {\n\t\t\t\tt.Errorf(\"expected 3, got %v\", len(owners))\n\t\t\t}\n\t\t\t// BackupOwners iterates AVL by address-string key. TestAddress\n\t\t\t// does not preserve u1\u003cu2\u003cu3 order, so assert set membership.\n\t\t\tset := map[string]bool{owners[0]: true, owners[1]: true, owners[2]: true}\n\t\t\tfor _, addr := range []address{u1, u2, u3} {\n\t\t\t\tif !set[addr.String()] {\n\t\t\t\t\tt.Errorf(\"expected %v in backup owners\", addr)\n\t\t\t\t}\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n\tt.Run(\"Members\", func(cur realm, t *testing.T) {\n\t\tg := New(u1)\n\t\ttesting.SetRealm(testing.NewUserRealm(u1))\n\t\tfunc(cur realm) {\n\t\t\tg.AddMember(0, cur, u2)\n\t\t\tg.AddMember(0, cur, u3)\n\t\t\tmembers := g.Members()\n\t\t\tif len(members) != 3 {\n\t\t\t\tt.Errorf(\"expected 3, got %v\", len(members))\n\t\t\t}\n\t\t\tset := map[string]bool{members[0]: true, members[1]: true, members[2]: true}\n\t\t\tfor _, addr := range []address{u1, u2, u3} {\n\t\t\t\tif !set[addr.String()] {\n\t\t\t\t\tt.Errorf(\"expected %v in members\", addr)\n\t\t\t\t}\n\t\t\t}\n\t\t}(cross(cur))\n\t})\n}\n\nfunc TestSliceWithOffset(cur realm, t *testing.T) {\n\ttestTable := []struct {\n\t\tname          string\n\t\tslice         []string\n\t\toffset        int\n\t\tcount         int\n\t\texpected      []string\n\t\texpectedCount int\n\t}{\n\t\t{\n\t\t\tname:          \"empty\",\n\t\t\tslice:         []string{},\n\t\t\toffset:        0,\n\t\t\tcount:         0,\n\t\t\texpected:      []string{},\n\t\t\texpectedCount: 0,\n\t\t},\n\t\t{\n\t\t\tname:          \"single\",\n\t\t\tslice:         []string{\"a\"},\n\t\t\toffset:        0,\n\t\t\tcount:         1,\n\t\t\texpected:      []string{\"a\"},\n\t\t\texpectedCount: 1,\n\t\t},\n\t\t{\n\t\t\tname:          \"single offset\",\n\t\t\tslice:         []string{\"a\"},\n\t\t\toffset:        1,\n\t\t\tcount:         1,\n\t\t\texpected:      []string{},\n\t\t\texpectedCount: 0,\n\t\t},\n\t\t{\n\t\t\tname:          \"multiple\",\n\t\t\tslice:         []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\toffset:        0,\n\t\t\tcount:         10,\n\t\t\texpected:      []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\texpectedCount: 10,\n\t\t},\n\t\t{\n\t\t\tname:          \"multiple offset\",\n\t\t\tslice:         []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\toffset:        5,\n\t\t\tcount:         5,\n\t\t\texpected:      []string{\"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\texpectedCount: 5,\n\t\t},\n\t\t{\n\t\t\tname:          \"multiple offset end\",\n\t\t\tslice:         []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\toffset:        10,\n\t\t\tcount:         5,\n\t\t\texpected:      []string{},\n\t\t\texpectedCount: 0,\n\t\t},\n\t\t{\n\t\t\tname:          \"multiple offset past end\",\n\t\t\tslice:         []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\toffset:        11,\n\t\t\tcount:         5,\n\t\t\texpected:      []string{},\n\t\t\texpectedCount: 0,\n\t\t},\n\t\t{\n\t\t\tname:          \"multiple offset count past end\",\n\t\t\tslice:         []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\toffset:        5,\n\t\t\tcount:         20,\n\t\t\texpected:      []string{\"f\", \"g\", \"h\", \"i\", \"j\"},\n\t\t\texpectedCount: 5,\n\t\t},\n\t}\n\tfor _, test := range testTable {\n\t\tt.Run(test.name, func(t *testing.T) {\n\t\t\ttree := avl.NewTree()\n\t\t\tfor _, s := range test.slice {\n\t\t\t\ttree.Set(s, struct{}{})\n\t\t\t}\n\t\t\tslice := sliceWithOffset(tree, test.offset, test.count)\n\t\t\tif len(slice) != test.expectedCount {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", test.expectedCount, len(slice))\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"DFWW53ZBh9QJsQr7ngQtCw9hVVBKGYqkOIC876Baj2y4mK8YLi++XY92Ujv0RrVboIQO1R6Xb9E9miKWx38qCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"rolist","path":"gno.land/p/nt/avl/v0/rolist","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/avl/v0/rolist\"\ngno = \"0.9\"\n"},{"name":"rolist.gno","body":"// Package rolist provides a read-only wrapper for list.List with safe value transformation.\n//\n// It is useful when you want to expose a read-only view of a list while ensuring that\n// the sensitive data cannot be modified.\n//\n// Example:\n//\n//\t// Define a user structure with sensitive data\n//\ttype User struct {\n//\t    Name     string\n//\t    Balance  int\n//\t    Internal string // sensitive field\n//\t}\n//\n//\t// Create and populate the original list\n//\tvar privateList list.List\n//\tprivateList.Append(\u0026User{\n//\t    Name:     \"Alice\",\n//\t    Balance:  100,\n//\t    Internal: \"sensitive\",\n//\t})\n//\n//\t// Create a safe transformation function that copies the struct\n//\t// while excluding sensitive data\n//\tmakeEntrySafeFn := func(v any) any {\n//\t    u := v.(*User)\n//\t    return \u0026User{\n//\t        Name:     u.Name,\n//\t        Balance:  u.Balance,\n//\t        Internal: \"\", // omit sensitive data\n//\t    }\n//\t}\n//\n//\t// Create a read-only view of the list\n//\tpublicList := rolist.Wrap(privateList, makeEntrySafeFn)\n//\n//\t// Safely access the data\n//\tvalue := publicList.Get(0)\n//\tuser := value.(*User)\n//\t// user.Name == \"Alice\"\n//\t// user.Balance == 100\n//\t// user.Internal == \"\" (sensitive data is filtered)\npackage rolist\n\nimport (\n\t\"gno.land/p/nt/avl/v0/list\"\n\t\"gno.land/p/nt/avl/v0/rotree\"\n)\n\n// IReadOnlyList defines the read-only operations available on a list.\ntype IReadOnlyList interface {\n\tLen() int\n\tGet(index int) any\n\tSlice(startIndex, endIndex int) []any\n\tForEach(fn func(index int, value any) bool)\n}\n\n// ReadOnlyList wraps a list.List and provides read-only access.\ntype ReadOnlyList struct {\n\tlist            *list.List\n\tmakeEntrySafeFn func(any) any\n}\n\n// Verify interface implementations\nvar (\n\t_ IReadOnlyList = (*ReadOnlyList)(nil)\n\t_ IReadOnlyList = (interface{ list.IList })(nil) // is subset of list.IList\n)\n\n// Wrap creates a new ReadOnlyList from an existing list.List and a safety transformation function.\n// If makeEntrySafeFn is nil, values will be returned as-is without transformation.\nfunc Wrap(list *list.List, makeEntrySafeFn func(any) any) *ReadOnlyList {\n\treturn \u0026ReadOnlyList{\n\t\tlist:            list,\n\t\tmakeEntrySafeFn: makeEntrySafeFn,\n\t}\n}\n\n// getSafeValue applies the makeEntrySafeFn if it exists, otherwise returns the original value\nfunc (rol *ReadOnlyList) getSafeValue(value any) any {\n\tif rol.makeEntrySafeFn == nil {\n\t\treturn value\n\t}\n\treturn rol.makeEntrySafeFn(value)\n}\n\n// Len returns the number of elements in the list.\nfunc (rol *ReadOnlyList) Len() int {\n\treturn rol.list.Len()\n}\n\n// Get returns the value at the specified index, converted to a safe format.\n// Returns nil if index is out of bounds.\nfunc (rol *ReadOnlyList) Get(index int) any {\n\tvalue := rol.list.Get(index)\n\tif value == nil {\n\t\treturn nil\n\t}\n\treturn rol.getSafeValue(value)\n}\n\n// Slice returns a slice of values from startIndex (inclusive) to endIndex (exclusive),\n// with all values converted to a safe format.\nfunc (rol *ReadOnlyList) Slice(startIndex, endIndex int) []any {\n\tvalues := rol.list.Slice(startIndex, endIndex)\n\tif values == nil {\n\t\treturn nil\n\t}\n\n\tresult := make([]any, len(values))\n\tfor i, v := range values {\n\t\tresult[i] = rol.getSafeValue(v)\n\t}\n\treturn result\n}\n\n// ForEach iterates through all elements in the list, providing safe versions of the values.\nfunc (rol *ReadOnlyList) ForEach(fn func(index int, value any) bool) {\n\trol.list.ForEach(func(index int, value any) bool {\n\t\treturn fn(index, rol.getSafeValue(value))\n\t})\n}\n\n// Tree returns a read-only pointer to the underlying AVL tree.\nfunc (rol *ReadOnlyList) Tree() *rotree.ReadOnlyTree {\n\treturn rol.list.Tree()\n}\n"},{"name":"rolist_test.gno","body":"package rolist\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/avl/v0/list\"\n)\n\nfunc TestExample(t *testing.T) {\n\t// User represents our internal data structure\n\ttype User struct {\n\t\tID       string\n\t\tName     string\n\t\tBalance  int\n\t\tInternal string // sensitive internal data\n\t}\n\n\t// Create and populate the original list\n\tl := \u0026list.List{}\n\tl.Append(\n\t\t\u0026User{\n\t\t\tID:       \"1\",\n\t\t\tName:     \"Alice\",\n\t\t\tBalance:  100,\n\t\t\tInternal: \"sensitive_data_1\",\n\t\t},\n\t\t\u0026User{\n\t\t\tID:       \"2\",\n\t\t\tName:     \"Bob\",\n\t\t\tBalance:  200,\n\t\t\tInternal: \"sensitive_data_2\",\n\t\t},\n\t)\n\n\t// Define a makeEntrySafeFn that:\n\t// 1. Creates a defensive copy of the User struct\n\t// 2. Omits sensitive internal data\n\tmakeEntrySafeFn := func(v any) any {\n\t\toriginalUser := v.(*User)\n\t\treturn \u0026User{\n\t\t\tID:       originalUser.ID,\n\t\t\tName:     originalUser.Name,\n\t\t\tBalance:  originalUser.Balance,\n\t\t\tInternal: \"\", // Omit sensitive data\n\t\t}\n\t}\n\n\t// Create a read-only view of the list\n\troList := Wrap(l, makeEntrySafeFn)\n\n\t// Test retrieving and verifying a user\n\tt.Run(\"Get User\", func(t *testing.T) {\n\t\t// Get user from read-only list\n\t\tvalue := roList.Get(0)\n\t\tif value == nil {\n\t\t\tt.Fatal(\"User at index 0 not found\")\n\t\t}\n\n\t\tuser := value.(*User)\n\n\t\t// Verify user data is correct\n\t\tif user.Name != \"Alice\" || user.Balance != 100 {\n\t\t\tt.Errorf(\"Unexpected user data: got name=%s balance=%d\", user.Name, user.Balance)\n\t\t}\n\n\t\t// Verify sensitive data is not exposed\n\t\tif user.Internal != \"\" {\n\t\t\tt.Error(\"Sensitive data should not be exposed\")\n\t\t}\n\n\t\t// Verify it's a different instance than the original\n\t\toriginalUser := l.Get(0).(*User)\n\t\tif user == originalUser {\n\t\t\tt.Error(\"Read-only list should return a copy, not the original pointer\")\n\t\t}\n\t})\n\n\t// Test slice functionality\n\tt.Run(\"Slice Users\", func(t *testing.T) {\n\t\tusers := roList.Slice(0, 2)\n\t\tif len(users) != 2 {\n\t\t\tt.Fatalf(\"Expected 2 users, got %d\", len(users))\n\t\t}\n\n\t\tfor _, v := range users {\n\t\t\tuser := v.(*User)\n\t\t\tif user.Internal != \"\" {\n\t\t\t\tt.Error(\"Sensitive data exposed in slice\")\n\t\t\t}\n\t\t}\n\t})\n\n\t// Test ForEach functionality\n\tt.Run(\"ForEach Users\", func(t *testing.T) {\n\t\tcount := 0\n\t\troList.ForEach(func(index int, value any) bool {\n\t\t\tuser := value.(*User)\n\t\t\tif user.Internal != \"\" {\n\t\t\t\tt.Error(\"Sensitive data exposed during iteration\")\n\t\t\t}\n\t\t\tcount++\n\t\t\treturn false\n\t\t})\n\n\t\tif count != 2 {\n\t\t\tt.Errorf(\"Expected 2 users, got %d\", count)\n\t\t}\n\t})\n}\n\nfunc TestNilMakeEntrySafeFn(t *testing.T) {\n\t// Create a list with some test data\n\tl := \u0026list.List{}\n\toriginalValue := []int{1, 2, 3}\n\tl.Append(originalValue)\n\n\t// Create a ReadOnlyList with nil makeEntrySafeFn\n\troList := Wrap(l, nil)\n\n\t// Test that we get back the original value\n\tvalue := roList.Get(0)\n\tif value == nil {\n\t\tt.Fatal(\"Value not found\")\n\t}\n\n\t// Verify it's the exact same slice (not a copy)\n\tretrievedSlice := value.([]int)\n\tif \u0026retrievedSlice[0] != \u0026originalValue[0] {\n\t\tt.Error(\"Expected to get back the original slice reference\")\n\t}\n}\n\nfunc TestReadOnlyList(t *testing.T) {\n\t// Example of a makeEntrySafeFn that appends \"_readonly\" to demonstrate transformation\n\tmakeEntrySafeFn := func(value any) any {\n\t\treturn value.(string) + \"_readonly\"\n\t}\n\n\tl := \u0026list.List{}\n\tl.Append(\"value1\", \"value2\", \"value3\")\n\n\troList := Wrap(l, makeEntrySafeFn)\n\n\ttests := []struct {\n\t\tname     string\n\t\tindex    int\n\t\texpected any\n\t}{\n\t\t{\"ExistingIndex0\", 0, \"value1_readonly\"},\n\t\t{\"ExistingIndex1\", 1, \"value2_readonly\"},\n\t\t{\"NonExistingIndex\", 3, nil},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvalue := roList.Get(tt.index)\n\t\t\tif value != tt.expected {\n\t\t\t\tt.Errorf(\"For index %d, expected %v, got %v\", tt.index, tt.expected, value)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ZQsqBCWRepUN3x/H/nDgqfdMFof64DvXLmdC1C4SgfmNXaS9H03WIsEXMAkSwrQRsHXL8TvqFuaxlCD4T04XAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"rolist","path":"gno.land/p/nt/bptree/v0/rolist","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/bptree/v0/rolist\"\ngno = \"0.9\"\n"},{"name":"rolist.gno","body":"// Package rolist provides a read-only wrapper for list.List with safe value transformation.\npackage rolist\n\nimport (\n\t\"gno.land/p/nt/bptree/v0/list\"\n\t\"gno.land/p/nt/bptree/v0/rotree\"\n)\n\n// IReadOnlyList defines the read-only operations available on a list.\ntype IReadOnlyList interface {\n\tLen() int\n\tGet(index int) (any, bool)\n\tSlice(startIndex, endIndex int) []any\n\tForEach(fn func(index int, value any) bool)\n}\n\n// ReadOnlyList wraps a list.List and provides read-only access.\ntype ReadOnlyList struct {\n\tlist            *list.List\n\tmakeEntrySafeFn func(any) any\n}\n\n// Verify interface implementations\nvar (\n\t_ IReadOnlyList = (*ReadOnlyList)(nil)\n\t_ IReadOnlyList = (interface{ list.IList })(nil) // is subset of list.IList\n)\n\n// Wrap creates a new ReadOnlyList from an existing list.List and a safety transformation function.\n// If makeEntrySafeFn is nil, values will be returned as-is without transformation.\nfunc Wrap(list *list.List, makeEntrySafeFn func(any) any) *ReadOnlyList {\n\treturn \u0026ReadOnlyList{\n\t\tlist:            list,\n\t\tmakeEntrySafeFn: makeEntrySafeFn,\n\t}\n}\n\n// getSafeValue applies the makeEntrySafeFn if it exists, otherwise returns the original value\nfunc (rol *ReadOnlyList) getSafeValue(value any) any {\n\tif rol.makeEntrySafeFn == nil {\n\t\treturn value\n\t}\n\treturn rol.makeEntrySafeFn(value)\n}\n\n// Len returns the number of elements in the list.\nfunc (rol *ReadOnlyList) Len() int {\n\treturn rol.list.Len()\n}\n\n// Get returns the value at the specified index, converted to a safe format.\n// Returns (nil, false) if index is out of bounds.\nfunc (rol *ReadOnlyList) Get(index int) (any, bool) {\n\tvalue, ok := rol.list.Get(index)\n\tif !ok {\n\t\treturn nil, false\n\t}\n\treturn rol.getSafeValue(value), true\n}\n\n// Slice returns a slice of values from startIndex (inclusive) to endIndex (exclusive),\n// with all values converted to a safe format.\nfunc (rol *ReadOnlyList) Slice(startIndex, endIndex int) []any {\n\tvalues := rol.list.Slice(startIndex, endIndex)\n\tif values == nil {\n\t\treturn nil\n\t}\n\n\tresult := make([]any, len(values))\n\tfor i, v := range values {\n\t\tresult[i] = rol.getSafeValue(v)\n\t}\n\treturn result\n}\n\n// ForEach iterates through all elements in the list, providing safe versions of the values.\nfunc (rol *ReadOnlyList) ForEach(fn func(index int, value any) bool) {\n\trol.list.ForEach(func(index int, value any) bool {\n\t\treturn fn(index, rol.getSafeValue(value))\n\t})\n}\n\n// Tree returns a read-only pointer to the underlying B+ tree.\nfunc (rol *ReadOnlyList) Tree() *rotree.ReadOnlyTree {\n\treturn rol.list.Tree()\n}\n"},{"name":"rolist_test.gno","body":"package rolist\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/bptree/v0/list\"\n)\n\nfunc TestExample(t *testing.T) {\n\t// User represents our internal data structure\n\ttype User struct {\n\t\tID       string\n\t\tName     string\n\t\tBalance  int\n\t\tInternal string // sensitive internal data\n\t}\n\n\t// Create and populate the original list\n\tl := \u0026list.List{}\n\tl.Append(\n\t\t\u0026User{\n\t\t\tID:       \"1\",\n\t\t\tName:     \"Alice\",\n\t\t\tBalance:  100,\n\t\t\tInternal: \"sensitive_data_1\",\n\t\t},\n\t\t\u0026User{\n\t\t\tID:       \"2\",\n\t\t\tName:     \"Bob\",\n\t\t\tBalance:  200,\n\t\t\tInternal: \"sensitive_data_2\",\n\t\t},\n\t)\n\n\t// Define a makeEntrySafeFn that:\n\t// 1. Creates a defensive copy of the User struct\n\t// 2. Omits sensitive internal data\n\tmakeEntrySafeFn := func(v any) any {\n\t\toriginalUser := v.(*User)\n\t\treturn \u0026User{\n\t\t\tID:       originalUser.ID,\n\t\t\tName:     originalUser.Name,\n\t\t\tBalance:  originalUser.Balance,\n\t\t\tInternal: \"\", // Omit sensitive data\n\t\t}\n\t}\n\n\t// Create a read-only view of the list\n\troList := Wrap(l, makeEntrySafeFn)\n\n\t// Test retrieving and verifying a user\n\tt.Run(\"Get User\", func(t *testing.T) {\n\t\t// Get user from read-only list\n\t\tvalue, ok := roList.Get(0)\n\t\tif !ok {\n\t\t\tt.Fatal(\"User at index 0 not found\")\n\t\t}\n\n\t\tuser := value.(*User)\n\n\t\t// Verify user data is correct\n\t\tif user.Name != \"Alice\" || user.Balance != 100 {\n\t\t\tt.Errorf(\"Unexpected user data: got name=%s balance=%d\", user.Name, user.Balance)\n\t\t}\n\n\t\t// Verify sensitive data is not exposed\n\t\tif user.Internal != \"\" {\n\t\t\tt.Error(\"Sensitive data should not be exposed\")\n\t\t}\n\n\t\t// Verify it's a different instance than the original\n\t\torigVal, _ := l.Get(0)\n\t\toriginalUser := origVal.(*User)\n\t\tif user == originalUser {\n\t\t\tt.Error(\"Read-only list should return a copy, not the original pointer\")\n\t\t}\n\t})\n\n\t// Test slice functionality\n\tt.Run(\"Slice Users\", func(t *testing.T) {\n\t\tusers := roList.Slice(0, 2)\n\t\tif len(users) != 2 {\n\t\t\tt.Fatalf(\"Expected 2 users, got %d\", len(users))\n\t\t}\n\n\t\tfor _, v := range users {\n\t\t\tuser := v.(*User)\n\t\t\tif user.Internal != \"\" {\n\t\t\t\tt.Error(\"Sensitive data exposed in slice\")\n\t\t\t}\n\t\t}\n\t})\n\n\t// Test ForEach functionality\n\tt.Run(\"ForEach Users\", func(t *testing.T) {\n\t\tcount := 0\n\t\troList.ForEach(func(index int, value any) bool {\n\t\t\tuser := value.(*User)\n\t\t\tif user.Internal != \"\" {\n\t\t\t\tt.Error(\"Sensitive data exposed during iteration\")\n\t\t\t}\n\t\t\tcount++\n\t\t\treturn false\n\t\t})\n\n\t\tif count != 2 {\n\t\t\tt.Errorf(\"Expected 2 users, got %d\", count)\n\t\t}\n\t})\n}\n\nfunc TestNilMakeEntrySafeFn(t *testing.T) {\n\t// Create a list with some test data\n\tl := \u0026list.List{}\n\toriginalValue := []int{1, 2, 3}\n\tl.Append(originalValue)\n\n\t// Create a ReadOnlyList with nil makeEntrySafeFn\n\troList := Wrap(l, nil)\n\n\t// Test that we get back the original value\n\tvalue, ok := roList.Get(0)\n\tif !ok {\n\t\tt.Fatal(\"Value not found\")\n\t}\n\n\t// Verify it's the exact same slice (not a copy)\n\tretrievedSlice := value.([]int)\n\tif \u0026retrievedSlice[0] != \u0026originalValue[0] {\n\t\tt.Error(\"Expected to get back the original slice reference\")\n\t}\n}\n\nfunc TestReadOnlyList(t *testing.T) {\n\t// Example of a makeEntrySafeFn that appends \"_readonly\" to demonstrate transformation\n\tmakeEntrySafeFn := func(value any) any {\n\t\treturn value.(string) + \"_readonly\"\n\t}\n\n\tl := \u0026list.List{}\n\tl.Append(\"value1\", \"value2\", \"value3\")\n\n\troList := Wrap(l, makeEntrySafeFn)\n\n\ttests := []struct {\n\t\tname     string\n\t\tindex    int\n\t\texpected any\n\t\tok       bool\n\t}{\n\t\t{\"ExistingIndex0\", 0, \"value1_readonly\", true},\n\t\t{\"ExistingIndex1\", 1, \"value2_readonly\", true},\n\t\t{\"NonExistingIndex\", 3, nil, false},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tvalue, ok := roList.Get(tt.index)\n\t\t\tif ok != tt.ok || value != tt.expected {\n\t\t\t\tt.Errorf(\"For index %d, expected (%v, %v), got (%v, %v)\", tt.index, tt.expected, tt.ok, value, ok)\n\t\t\t}\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"+FIW2PW+gp+dian6yrVMcoBMuQ7HbEAvJZBcIFFznBwi/zqM+GC/0gJoPQRqb/dzqTTZ/h3tMTxEb0Nov7P/Cg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"definition","path":"gno.land/p/nt/commondao/v0/exts/definition","files":[{"name":"README.md","body":"# CommonDAO Package Definition Extension\n\nDefinition package is an extension of `gno.land/p/nt/commondao/v0` that provides\nan alternative approach to define custom proposal types.\n\n## Definition\n\nThe `Definition` type is an implementation that allows creating custom proposal\ndefinitions using callback functions and definition options.\n\nCommonDAO package supports different proposal types through the\n`ProposalDefinition` interface, so new proposal types require the definition\nof a custom type that implements the interface. The `Definition` type is a\ncallback based alternative to the type based approach.\n\nBy default, new definitions have a voting period of 7 days, allowing _YES_,\n_NO_ and _ABSTAIN_ votes, tallying those votes using an absolute majority of\nmore than 50% of member votes, considering that a proposal passes when the\nmajority of the votes are _YES_.\n\nNew definition can be created using any of the following functions:\n\n```go\n// New creates a new custom proposal definition or returns an error\nfunc New(title, body string, options ...Option) (Definition, error)\n\n// MustNew creates a new custom proposal definition or panics on error\nfunc MustNew(title, body string, options ...Option) Definition\n```\n\nDefault definition behavior can be configured by setting custom options that\nconfigures the following proposal options:\n\n- Custom vote choices\n- Voting period\n- Tally behavior\n- Pre-execution and render validation\n- Execution behavior\n\nExample usage:\n\n```go\nimport (\n  \"chain/runtime\"\n  \"errors\"\n  \"time\"\n\n  \"gno.land/p/nt/commondao/v0\"\n  \"gno.land/p/nt/commondao/v0/exts/definition\"\n)\n\nvar dao = commondao.New()\n\n// CreateMemberProposal creates a new example proposal to add a DAO member.\nfunc CreateMemberProposal(member address) uint64 {\n  if !member.IsValid() {\n    panic(\"invalid member address\")\n  }\n\n  // Define a function to validate that member doesn't exist within the DAO\n  validate := func() error {\n    if dao.Members().Has(member) {\n      return errors.New(\"member already exists within the DAO\")\n    }\n    return nil\n  }\n\n  // Define a custom tally function that approves proposals without votes\n  tally := func(commondao.VotingContext) (bool, error) {\n    return true, nil\n  }\n\n  // Define an executor to add the new member to the DAO\n  executor := func(realm) error {\n    dao.Members().Add(member)\n    return nil\n  }\n\n  // Create a custom proposal definition for an example proposal type\n  def := definition.MustNew(\n    \"Example Proposal\",\n    \"This is a simple proposal example\",\n    definition.WithVotingPeriod(time.Hour * 24 * 2), // 2 days\n    definition.WithTally(tally),\n    definition.WithValidation(validate),\n    definition.WithExecutor(executor),\n  )\n\n  // Create a new proposal\n  p := dao.MustPropose(runtime.PreviousRealm().Address(), def)\n  return p.ID()\n}\n```\n"},{"name":"definition.gno","body":"package definition\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// DefaultVotingPeriod defines the default voting period for proposals.\nconst DefaultVotingPeriod = time.Hour * 24 * 7\n\nvar (\n\tErrBodyIsRequired  = errors.New(\"proposal body is required\")\n\tErrTitleIsRequired = errors.New(\"proposal title is required\")\n)\n\nvar defaultVoteChoices = []commondao.VoteChoice{\n\tcommondao.ChoiceYes,\n\tcommondao.ChoiceNo,\n\tcommondao.ChoiceAbstain,\n}\n\n// TallyFunc defines a function to handle proposal votes tallying.\ntype TallyFunc func(commondao.VotingContext) (passes bool, _ error)\n\n// New creates a new proposal definition.\n//\n// By default, new definitions have a voting period of 7 days, allowing YES, NO and\n// ABSTAIN votes, tallying those votes using an absolute majority of more than 50% of\n// member votes, considering that a proposal passes when the majority of the votes are YES.\n//\n// Default definition behavior can be configured by setting custom options.\nfunc New(title, body string, options ...Option) (Definition, error) {\n\ttitle = strings.TrimSpace(title)\n\tif title == \"\" {\n\t\treturn Definition{}, ErrTitleIsRequired\n\t}\n\n\tbody = strings.TrimSpace(body)\n\tif body == \"\" {\n\t\treturn Definition{}, ErrBodyIsRequired\n\t}\n\n\tdef := Definition{\n\t\ttitle:        title,\n\t\tbody:         body,\n\t\tvotingPeriod: DefaultVotingPeriod,\n\t\tvoteChoices:  defaultVoteChoices,\n\t\ttallyCb:      TallyByAbsoluteMajority,\n\t}\n\tfor _, apply := range options {\n\t\tapply(\u0026def)\n\t}\n\treturn def, nil\n}\n\n// MustNew creates a new proposal definition or panics on error.\nfunc MustNew(title, body string, options ...Option) Definition {\n\tdef, err := New(title, body, options...)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn def\n}\n\n// Definition defines CommonDAO proposal types.\ntype Definition struct {\n\ttitle        string\n\tbody         string\n\tvotingPeriod time.Duration\n\tvoteChoices  []commondao.VoteChoice\n\ttallyCb      TallyFunc\n\tvalidateCb   func() error\n\texecuteCb    commondao.ExecFunc\n}\n\n// Title returns the proposal title.\nfunc (d Definition) Title() string {\n\treturn d.title\n}\n\n// Body returns proposal's body.\nfunc (d Definition) Body() string {\n\treturn d.body\n}\n\n// VotingPeriod returns the period where votes are allowed after proposal creation.\nfunc (d Definition) VotingPeriod() time.Duration {\n\treturn d.votingPeriod\n}\n\n// CustomVoteChoices returns a list of valid voting choices.\nfunc (d Definition) CustomVoteChoices() []commondao.VoteChoice {\n\treturn d.voteChoices\n}\n\n// Tally counts the number of votes and verifies if proposal passes.\nfunc (d Definition) Tally(ctx commondao.VotingContext) (passes bool, _ error) {\n\treturn d.tallyCb(ctx)\n}\n\n// Validate validates that the proposal is valid for the current state.\nfunc (d Definition) Validate() error {\n\tif d.validateCb != nil {\n\t\treturn d.validateCb()\n\t}\n\treturn nil\n}\n\n// Executor returns a function to execute the proposal.\nfunc (d Definition) Executor() commondao.ExecFunc {\n\treturn d.executeCb\n}\n\n// TallyByAbsoluteMajority tallies votes by absolute majority.\n// A quorum of 51% of member votes is required to tally votes.\n// Tally considers that proposals passes when more than half of members votes YES.\nfunc TallyByAbsoluteMajority(ctx commondao.VotingContext) (bool, error) {\n\t// Check if a quorum of 51% has been met\n\tif !commondao.IsQuorumReached(commondao.QuorumMoreThanHalf, ctx.VotingRecord, ctx.Members) {\n\t\treturn false, commondao.ErrNoQuorum\n\t}\n\n\t// Tally votes by absolute majority, which requires 51% votes\n\tc, success := commondao.SelectChoiceByAbsoluteMajority(ctx.VotingRecord, ctx.Members.Size())\n\tif success {\n\t\treturn c == commondao.ChoiceYes, nil\n\t}\n\treturn false, nil\n}\n"},{"name":"definition_test.gno","body":"package definition_test\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/commondao/v0/exts/definition\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestNew(t *testing.T) {\n\ttests := []struct {\n\t\tname  string\n\t\ttitle string\n\t\tbody  string\n\t\terr   error\n\t}{\n\t\t{\n\t\t\tname:  \"ok\",\n\t\t\ttitle: \"Proposal Title\",\n\t\t\tbody:  \"Foo\",\n\t\t},\n\t\t{\n\t\t\tname:  \"empty title\",\n\t\t\ttitle: \"\",\n\t\t\terr:   definition.ErrTitleIsRequired,\n\t\t},\n\t\t{\n\t\t\tname:  \"empty title\",\n\t\t\ttitle: \"Proposal Title\",\n\t\t\tbody:  \"\",\n\t\t\terr:   definition.ErrBodyIsRequired,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Act\n\t\t\tdef, err := definition.New(tt.title, tt.body)\n\n\t\t\t// Assert\n\t\t\tif tt.err != nil {\n\t\t\t\tuassert.ErrorIs(t, err, tt.err, \"expect an error\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tuassert.NoError(t, err, \"expect no errors\")\n\t\t\tuassert.Equal(t, tt.title, def.Title(), \"expect title to match\")\n\t\t\tuassert.Equal(t, tt.body, def.Body(), \"expect body to match\")\n\t\t})\n\t}\n}\n\nfunc TestDefinitionCustomVoteChoices(t *testing.T) {\n\t// Default voting choices\n\tdef := definition.MustNew(\"Title\", \"Body\")\n\tuassert.Equal(t, \"YES,NO,ABSTAIN\", joinVoteChoices(def.CustomVoteChoices()), \"expect default vote choces to match\")\n\n\t// Custom voting choices\n\tdef = definition.MustNew(\"Title\", \"Body\", definition.WithVoteChoices([]commondao.VoteChoice{\"A\", \"B\", \"C\"}))\n\tuassert.Equal(t, \"A,B,C\", joinVoteChoices(def.CustomVoteChoices()), \"expect custom vote choces to match\")\n}\n\nfunc TestDefinitionVotingPeriod(t *testing.T) {\n\t// Default voting period\n\tdef := definition.MustNew(\"Title\", \"Body\")\n\tuassert.True(t, definition.DefaultVotingPeriod == def.VotingPeriod(), \"expect default voting period to match\")\n\n\t// Custom voting period\n\tperiod := time.Hour * 24 * 30\n\tdef = definition.MustNew(\"Title\", \"Body\", definition.WithVotingPeriod(period))\n\tuassert.True(t, period == def.VotingPeriod(), \"expect custom voting period to match\")\n\n\t// Zero voting period\n\tperiod = 0\n\tdef = definition.MustNew(\"Title\", \"Body\", definition.WithVotingPeriod(period))\n\tuassert.True(t, period == def.VotingPeriod(), \"expect zero voting period to match\")\n}\n\nfunc TestDefinitionTally(t *testing.T) {\n\ttests := []struct {\n\t\tname   string\n\t\tpasses bool\n\t\terr    error\n\t}{\n\t\t{\n\t\t\tname:   \"proposal passes\",\n\t\t\tpasses: true,\n\t\t},\n\t\t{\n\t\t\tname:   \"proposal fails\",\n\t\t\tpasses: false,\n\t\t},\n\t\t{\n\t\t\tname: \"tally error\",\n\t\t\terr:  errors.New(\"no quorum\"),\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tfn := func(commondao.VotingContext) (bool, error) {\n\t\t\t\treturn tt.passes, tt.err\n\t\t\t}\n\n\t\t\tdef := definition.MustNew(\"Title\", \"Body\", definition.WithTally(fn))\n\n\t\t\t// Act\n\t\t\tpasses, err := def.Tally(commondao.VotingContext{})\n\n\t\t\t// Assert\n\t\t\tif tt.err != nil {\n\t\t\t\tuassert.ErrorIs(t, err, tt.err, \"expect tally to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tuassert.NoError(t, err, \"expect no errors\")\n\t\t\tuassert.Equal(t, tt.passes, passes, \"expect tally result to match\")\n\t\t})\n\t}\n}\n\nfunc TestDefinitionValidation(t *testing.T) {\n\terrTest := errors.New(\"test error\")\n\ttests := []struct {\n\t\tname      string\n\t\tvalidator func() error\n\t\terr       error\n\t}{\n\t\t{\n\t\t\tname:      \"ok\",\n\t\t\tvalidator: func() error { return nil },\n\t\t},\n\t\t{\n\t\t\tname:      \"validation error\",\n\t\t\tvalidator: func() error { return errTest },\n\t\t\terr:       errTest,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tdef := definition.MustNew(\"Title\", \"Body\", definition.WithValidation(tt.validator))\n\n\t\t\t// Act\n\t\t\terr := def.Validate()\n\n\t\t\t// Assert\n\t\t\tif tt.err != nil {\n\t\t\t\tuassert.ErrorIs(t, err, tt.err, \"expect validation to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tuassert.NoError(t, err, \"expect no errors\")\n\t\t})\n\t}\n}\n\nfunc joinVoteChoices(choices []commondao.VoteChoice) string {\n\tvalues := make([]string, len(choices))\n\tfor i, c := range choices {\n\t\tvalues[i] = string(c)\n\t}\n\treturn strings.Join(values, \",\")\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/commondao/v0/exts/definition\"\ngno = \"0.9\"\n"},{"name":"options.gno","body":"package definition\n\nimport (\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// Option configures CommonDAO proposal definitions.\ntype Option func(*Definition)\n\n// WithVoteChoices configures a proposal to use custom voting choices.\n// It panics when there are less than two choices or when a voting choice is duplicated.\nfunc WithVoteChoices(choices []commondao.VoteChoice) Option {\n\tcount := len(choices)\n\tif count \u003c 2 {\n\t\tpanic(\"proposal requires at least two voting choices: got \" + strconv.Itoa(count))\n\t}\n\n\treturn func(d *Definition) {\n\t\tseen := make(map[commondao.VoteChoice]struct{}, count)\n\t\td.voteChoices = make([]commondao.VoteChoice, count)\n\t\tfor i, choice := range choices {\n\t\t\tif _, found := seen[choice]; found {\n\t\t\t\tpanic(\"proposal voting choice is duplicated: \" + string(choice))\n\t\t\t}\n\n\t\t\tseen[choice] = struct{}{}\n\t\t\td.voteChoices[i] = choice\n\t\t}\n\t}\n}\n\n// WithVotingPeriod configures the voting period of a proposal.\n// It panics when voting period is negative.\nfunc WithVotingPeriod(period time.Duration) Option {\n\tif period \u003c 0 {\n\t\t// Zero allows unit testing of proposals\n\t\tpanic(\"voting period of proposal must be greater or equal to zero\")\n\t}\n\n\treturn func(d *Definition) {\n\t\td.votingPeriod = period\n\t}\n}\n\n// SECURITY XXX: WithTally and WithValidation capture caller-supplied\n// callbacks that run during voting and pre-execution. Whoever supplies\n// the callbacks controls the outcome of \"did this proposal pass?\" and\n// \"is this proposal valid?\". Hostile callbacks can pass invalid\n// proposals, skew vote counts, or always-deny (DoS).\n// Fix: document that callers must only supply callbacks from trusted\n// code (a registration site, not a public API), or restrict to a\n// canonical-impl allowlist. These callbacks are trust-extensions of\n// the DAO using them — treat like inlining the code into the DAO.\n//\n// WithTally configures a proposal definition to use a custom vote tallying.\n// New definitions tally using simple majority (51%) by default.\n// It panics when tally callback is nil.\nfunc WithTally(cb TallyFunc) Option {\n\tif cb == nil {\n\t\tpanic(\"proposal tally callback is nil\")\n\t}\n\n\treturn func(d *Definition) {\n\t\td.tallyCb = cb\n\t}\n}\n\n// WithValidation configures a proposal definition to support proposal validation.\n// Validation is done before execution and normally also during proposal rendering.\n// It panics when validation callback is nil.\nfunc WithValidation(cb func() error) Option {\n\tif cb == nil {\n\t\tpanic(\"proposal validation callback is nil\")\n\t}\n\n\treturn func(d *Definition) {\n\t\td.validateCb = cb\n\t}\n}\n\n// SECURITY XXX: commondao.ExecFunc takes a realm parameter, which\n// means whoever registers an executor here receives `cur` from the\n// commondao machinery when a proposal executes. The executor can\n// capture that cur and use it to act AS the calling realm on any\n// downstream cross-call. This is the same cur-disclosure leak that\n// daokit had pre-migration; commondao is a peer subsystem that\n// hasn't been migrated yet.\n// Fix: change ExecFunc to take an address (the proposer or executor\n// identity) instead of realm, and have commondao derive the address\n// from cur under IsCurrent before invoking. See p/samcrew/daokit and\n// p/samcrew/basedao for the address-typed pattern this should follow.\n// Until fixed, only register executors from trusted code — never\n// accept an ExecFunc from an external realm.\n//\n// WithExecutor configures a proposal definition to support proposal execution.\n// It panics when executor callback is nil.\nfunc WithExecutor(cb commondao.ExecFunc) Option {\n\tif cb == nil {\n\t\tpanic(\"proposal executor callback is nil\")\n\t}\n\n\treturn func(d *Definition) {\n\t\td.executeCb = cb\n\t}\n}\n"},{"name":"options_test.gno","body":"package definition_test\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/commondao/v0/exts/definition\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestOptions(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\toption   func() definition.Option\n\t\tpanicMsg string\n\t}{\n\t\t{\n\t\t\tname: \"custom voting choices\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithVoteChoices([]commondao.VoteChoice{\"A\", \"B\", \"C\"})\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"no voting choices\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithVoteChoices([]commondao.VoteChoice{})\n\t\t\t},\n\t\t\tpanicMsg: \"proposal requires at least two voting choices: got 0\",\n\t\t},\n\t\t{\n\t\t\tname: \"one voting choice\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithVoteChoices([]commondao.VoteChoice{\"A\"})\n\t\t\t},\n\t\t\tpanicMsg: \"proposal requires at least two voting choices: got 1\",\n\t\t},\n\t\t{\n\t\t\tname: \"duplicated voting choice\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithVoteChoices([]commondao.VoteChoice{\"A\", \"B\", \"A\"})\n\t\t\t},\n\t\t\tpanicMsg: \"proposal voting choice is duplicated: A\",\n\t\t},\n\t\t{\n\t\t\tname: \"voting period\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithVotingPeriod(time.Hour * 24)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"zero voting period\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithVotingPeriod(0)\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"negative voting period\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithVotingPeriod(-time.Hour)\n\t\t\t},\n\t\t\tpanicMsg: \"voting period of proposal must be greater or equal to zero\",\n\t\t},\n\t\t{\n\t\t\tname: \"tally callback\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithTally(func(commondao.VotingContext) (bool, error) { return true, nil })\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"nil tally callback\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithTally(nil)\n\t\t\t},\n\t\t\tpanicMsg: \"proposal tally callback is nil\",\n\t\t},\n\t\t{\n\t\t\tname: \"validator callback\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithValidation(func() error { return nil })\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"nil validator callback\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithValidation(nil)\n\t\t\t},\n\t\t\tpanicMsg: \"proposal validation callback is nil\",\n\t\t},\n\t\t{\n\t\t\tname: \"nil executor callback\",\n\t\t\toption: func() definition.Option {\n\t\t\t\treturn definition.WithExecutor(nil)\n\t\t\t},\n\t\t\tpanicMsg: \"proposal executor callback is nil\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\t// Arrange\n\t\t\tvar option definition.Option\n\n\t\t\t// Act\n\t\t\tfn := func() {\n\t\t\t\toption = tt.option()\n\t\t\t\t_ = definition.MustNew(\"Title\", \"Body\", option)\n\t\t\t}\n\n\t\t\t// Assert\n\t\t\tif tt.panicMsg != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tt.panicMsg, fn, \"expect panic with message\")\n\t\t\t} else {\n\t\t\t\turequire.NotPanics(t, cur, fn, \"expect no panic\")\n\t\t\t\turequire.True(t, option != nil, \"expect a non nil option\")\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"z_executor_0_filetest.gno","body":"// PKGPATH: gno.land/r/definition/test\npackage test\n\nimport \"gno.land/p/nt/commondao/v0/exts/definition\"\n\nfunc executor(realm) error {\n\treturn nil\n}\n\nfunc main(cur realm) {\n\t// Arrange\n\tdef := definition.MustNew(\"Title\", \"Body\", definition.WithExecutor(executor))\n\tfn := def.Executor()\n\n\t// Act\n\terr := fn(cross(cur))\n\n\t// Assert\n\tprintln(err == nil)\n}\n\n// Output:\n// true\n"},{"name":"z_executor_1_filetest.gno","body":"// PKGPATH: gno.land/r/definition/test\npackage test\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/commondao/v0/exts/definition\"\n)\n\nvar wantErr = errors.New(\"test error\")\n\nfunc executor(realm) error {\n\treturn wantErr\n}\n\nfunc main(cur realm) {\n\t// Arrange\n\tdef := definition.MustNew(\"Title\", \"Body\", definition.WithExecutor(executor))\n\tfn := def.Executor()\n\n\t// Act\n\terr := fn(cross(cur))\n\n\t// Assert\n\tprintln(err == wantErr)\n}\n\n// Output:\n// true\n"},{"name":"z_readme_filetest.gno","body":"// PKGPATH: gno.land/r/definition/test\npackage test\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/commondao/v0/exts/definition\"\n)\n\nvar dao = commondao.New()\n\n// CreateMemberProposal creates a new example proposal to add a DAO member.\nfunc CreateMemberProposal(member address) uint64 {\n\tif !member.IsValid() {\n\t\tpanic(\"invalid member address\")\n\t}\n\n\t// Define a function to validate that member doesn't exist within the DAO\n\tvalidate := func() error {\n\t\tif dao.Members().Has(member) {\n\t\t\treturn errors.New(\"member already exists within the DAO\")\n\t\t}\n\t\treturn nil\n\t}\n\n\t// Define a custom tally function that approves proposals without votes\n\ttally := func(commondao.VotingContext) (bool, error) {\n\t\treturn true, nil\n\t}\n\n\t// Define an executor to add the new member to the DAO\n\texecutor := func(realm) error {\n\t\tdao.Members().Add(member)\n\t\treturn nil\n\t}\n\n\t// Create a custom proposal definition for an example proposal type\n\tdef := definition.MustNew(\n\t\t\"Example Proposal\",\n\t\t\"This is a simple proposal example\",\n\t\tdefinition.WithVotingPeriod(time.Hour*24*2), // 2 days\n\t\tdefinition.WithTally(tally),\n\t\tdefinition.WithValidation(validate),\n\t\tdefinition.WithExecutor(executor),\n\t)\n\n\t// Create a new proposal\n\tp := dao.MustPropose(unsafe.PreviousRealm().Address(), def)\n\treturn p.ID()\n}\n\nfunc main() {\n\tproposalID := CreateMemberProposal(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tprintln(proposalID)\n}\n\n// Output:\n// 1\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"7ezy59yQSfRBE246y+TcumCRNOdQ47Csd4ZMfao/Vs4IvVg+aK4O5egNem+Q8LC3nhJFywckPqV2r+ssUC+dBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"pausable","path":"gno.land/p/nt/pausable/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# pausable\n\nPackage `pausable` provides a mechanism to programmatically pause and unpause functionality, allowing an owner to restrict operations when the contract is in a paused state.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package pausable provides a mechanism to programmatically pause and unpause\n// functionality, allowing an owner to restrict operations when the contract\n// is in a paused state.\npackage pausable\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/pausable/v0\"\ngno = \"0.9\"\n"},{"name":"pausable.gno","body":"// Package pausable provides a mechanism to programmatically pause and unpause\n// functionality. This package allows an owner, defined via an Ownable object,\n// to restrict operations or methods when the contract is in a \"paused\" state.\npackage pausable\n\nimport (\n\t\"chain\"\n\t\"errors\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\ntype Pausable struct {\n\to      *ownable.Ownable\n\tpaused bool\n}\n\nvar ErrPaused = errors.New(\"pausable: realm is currently paused\")\n\n// NewFromOwnable is the same as New, but with a pre-existing top-level ownable\nfunc NewFromOwnable(ownable *ownable.Ownable) *Pausable {\n\treturn \u0026Pausable{\n\t\to:      ownable,\n\t\tpaused: false,\n\t}\n}\n\n// IsPaused checks if Pausable is paused\nfunc (p Pausable) IsPaused() bool {\n\treturn p.paused\n}\n\n// Pause sets the state of Pausable to true, meaning all pausable functions\n// are paused. rlm must be the caller's own captured cur;\n// rlm.Previous().Address() must equal the underlying Ownable's owner.\nfunc (p *Pausable) Pause(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !p.o.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\n\tp.paused = true\n\tchain.Emit(\"Paused\", \"by\", p.o.Owner().String())\n\n\treturn nil\n}\n\n// Unpause sets the state of Pausable to false, meaning all pausable\n// functions are resumed. rlm must be the caller's own captured cur;\n// rlm.Previous().Address() must equal the underlying Ownable's owner.\nfunc (p *Pausable) Unpause(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\tif !p.o.OwnedBy(rlm.Previous().Address()) {\n\t\treturn ownable.ErrUnauthorized\n\t}\n\n\tp.paused = false\n\tchain.Emit(\"Unpaused\", \"by\", p.o.Owner().String())\n\n\treturn nil\n}\n\n// Ownable returns the underlying ownable\nfunc (p *Pausable) Ownable() *ownable.Ownable {\n\treturn p.o\n}\n"},{"name":"pausable_test.gno","body":"package pausable\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nvar (\n\tfirstCaller = address(\"g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de\")\n\to           = ownable.NewWithAddress(firstCaller)\n)\n\nfunc TestNewFromOwnable(cur realm, t *testing.T) {\n\tresult := NewFromOwnable(o)\n\n\turequire.Equal(t, firstCaller.String(), result.Ownable().Owner().String())\n}\n\nfunc TestSetUnpaused(cur realm, t *testing.T) {\n\tresult := NewFromOwnable(o)\n\n\ttesting.SetRealm(testing.NewUserRealm(firstCaller))\n\tfunc(cur realm) {\n\t\turequire.NoError(t, result.Unpause(0, cur))\n\t}(cross(cur))\n\tuassert.False(t, result.IsPaused(), \"Expected result to be unpaused\")\n}\n\nfunc TestSetPaused(cur realm, t *testing.T) {\n\tresult := NewFromOwnable(o)\n\n\ttesting.SetRealm(testing.NewUserRealm(firstCaller))\n\tfunc(cur realm) {\n\t\turequire.NoError(t, result.Pause(0, cur))\n\t}(cross(cur))\n\tuassert.True(t, result.IsPaused(), \"Expected result to be paused\")\n}\n\nfunc TestIsPaused(cur realm, t *testing.T) {\n\tresult := NewFromOwnable(o)\n\turequire.False(t, result.IsPaused(), \"Expected result to be unpaused\")\n\n\ttesting.SetRealm(testing.NewUserRealm(firstCaller))\n\tfunc(cur realm) {\n\t\turequire.NoError(t, result.Pause(0, cur))\n\t}(cross(cur))\n\tuassert.True(t, result.IsPaused(), \"Expected result to be paused\")\n}\n\nfunc TestOwnable(cur realm, t *testing.T) {\n\tresult := NewFromOwnable(o)\n\n\tuassert.Equal(t, result.Ownable().Owner(), o.Owner())\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"kqb8l+uB6pS0783+TkGXWX6vYVh8wpFfMl70/T3h7xGZ82ML/Ed0SBMk1wstKWwJLTVsgubpg4REwvNeaTR6CA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"watchdog","path":"gno.land/p/nt/watchdog/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this package that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# watchdog\n\nPackage `watchdog` provides a simple watchdog timer for monitoring liveness.\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package watchdog provides a simple watchdog timer for monitoring liveness.\npackage watchdog\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/nt/watchdog/v0\"\ngno = \"0.9\"\n"},{"name":"watchdog.gno","body":"package watchdog\n\nimport \"time\"\n\ntype Watchdog struct {\n\tDuration   time.Duration\n\tlastUpdate time.Time\n\tlastDown   time.Time\n}\n\nfunc (w *Watchdog) Alive() {\n\tnow := time.Now()\n\tif !w.IsAlive() {\n\t\tw.lastDown = now\n\t}\n\tw.lastUpdate = now\n}\n\nfunc (w Watchdog) Status() string {\n\tif w.IsAlive() {\n\t\treturn \"OK\"\n\t}\n\treturn \"KO\"\n}\n\nfunc (w Watchdog) IsAlive() bool {\n\treturn time.Since(w.lastUpdate) \u003c w.Duration\n}\n\nfunc (w Watchdog) UpSince() time.Time {\n\treturn w.lastDown\n}\n\nfunc (w Watchdog) DownSince() time.Time {\n\tif !w.IsAlive() {\n\t\treturn w.lastUpdate\n\t}\n\treturn time.Time{}\n}\n"},{"name":"watchdog_test.gno","body":"package watchdog\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestPackage(t *testing.T) {\n\tw := Watchdog{Duration: 5 * time.Minute}\n\tuassert.False(t, w.IsAlive())\n\tw.Alive()\n\tuassert.True(t, w.IsAlive())\n\t// XXX: add more tests when we'll be able to \"skip time\".\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"lWMSO70Y0in0xEnTBGzf0jzOhaGISWXE8/yE/ixpB7rEFwZvfC7AWCDzt/o8Fc+uvjd/2/ie+cfm8GuLH2i/AA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ownable2step","path":"gno.land/p/oxtekgrinder/ownable2step","files":[{"name":"errors.gno","body":"package ownable2step\n\nimport \"errors\"\n\nvar (\n\tErrNoPendingOwner      = errors.New(\"ownable2step: no pending owner\")\n\tErrUnauthorized        = errors.New(\"ownable2step: caller is not owner\")\n\tErrPendingUnauthorized = errors.New(\"ownable2step: caller is not pending owner\")\n\tErrInvalidAddress      = errors.New(\"ownable2step: new owner address is invalid\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/oxtekgrinder/ownable2step\"\ngno = \"0.9\"\n"},{"name":"ownable.gno","body":"package ownable2step\n\nimport \"chain\"\n\nconst OwnershipTransferEvent = \"OwnershipTransfer\"\n\n// Ownable2Step is a two-step ownership transfer package.\n// It allows the current owner to set a new owner; the new owner must\n// accept ownership before the transfer completes.\n//\n// Authority-mutating methods (TransferOwnership, AcceptOwnership,\n// DropOwnership) take (_ int, rlm realm). The caller threads its own\n// cur; the method asserts rlm.IsCurrent() and identifies the principal\n// as rlm.Previous().Address() — unforgeable.\n//\n//\to.TransferOwnership(0, cur, newOwner)\n//\to.AcceptOwnership(0, cur)\n//\to.DropOwnership(0, cur)\n//\n// XXX Implement using Ownable instead of replicating it.\ntype Ownable2Step struct {\n\towner        address\n\tpendingOwner address\n}\n\n// NewWithAddress constructs an Ownable2Step with the given owner. This\n// is the only constructor — the previous NewWithOrigin / NewWithCurrent\n// sugar baked in runtime walks; the realm using this package now picks\n// the owner address explicitly (e.g. cur.Previous().Address() after\n// verifying cur.Previous().IsUserCall() in init).\nfunc NewWithAddress(addr address) *Ownable2Step {\n\treturn \u0026Ownable2Step{\n\t\towner:        addr,\n\t\tpendingOwner: \"\",\n\t}\n}\n\n// TransferOwnership initiates the transfer of ownership to newOwner by\n// setting the PendingOwner. rlm must be the caller's own captured cur;\n// rlm.Previous().Address() must equal the current owner.\nfunc (o *Ownable2Step) TransferOwnership(_ int, rlm realm, newOwner address) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrUnauthorized\n\t}\n\tif rlm.Previous().Address() != o.owner {\n\t\treturn ErrUnauthorized\n\t}\n\tif !newOwner.IsValid() {\n\t\treturn ErrInvalidAddress\n\t}\n\to.pendingOwner = newOwner\n\treturn nil\n}\n\n// AcceptOwnership accepts the pending ownership transfer. rlm must be\n// the caller's own captured cur; rlm.Previous().Address() must equal\n// the pending owner.\nfunc (o *Ownable2Step) AcceptOwnership(_ int, rlm realm) error {\n\tif o.pendingOwner.String() == \"\" {\n\t\treturn ErrNoPendingOwner\n\t}\n\tif !rlm.IsCurrent() {\n\t\treturn ErrPendingUnauthorized\n\t}\n\tif rlm.Previous().Address() != o.pendingOwner {\n\t\treturn ErrPendingUnauthorized\n\t}\n\to.owner = o.pendingOwner\n\to.pendingOwner = \"\"\n\treturn nil\n}\n\n// DropOwnership removes the owner, disabling any owner-related actions.\n// rlm must be the caller's own captured cur; rlm.Previous().Address()\n// must equal the current owner.\nfunc (o *Ownable2Step) DropOwnership(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrUnauthorized\n\t}\n\tif rlm.Previous().Address() != o.owner {\n\t\treturn ErrUnauthorized\n\t}\n\tprevOwner := o.owner\n\to.owner = \"\"\n\to.pendingOwner = \"\"\n\tchain.Emit(\n\t\tOwnershipTransferEvent,\n\t\t\"from\", prevOwner.String(),\n\t\t\"to\", \"\",\n\t)\n\treturn nil\n}\n\n// Owner returns the current owner address.\nfunc (o *Ownable2Step) Owner() address {\n\treturn o.owner\n}\n\n// PendingOwner returns the pending owner address (empty if none).\nfunc (o *Ownable2Step) PendingOwner() address {\n\treturn o.pendingOwner\n}\n\n// OwnedBy reports whether the given address is the current owner.\nfunc (o *Ownable2Step) OwnedBy(addr address) bool {\n\treturn addr == o.owner\n}\n\n// AssertOwnedBy panics with ErrUnauthorized if addr is not the owner.\nfunc (o *Ownable2Step) AssertOwnedBy(addr address) {\n\tif addr != o.owner {\n\t\tpanic(ErrUnauthorized)\n\t}\n}\n"},{"name":"ownable_test.gno","body":"package ownable2step\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nvar (\n\talice = testutils.TestAddress(\"alice\")\n\tbob   = testutils.TestAddress(\"bob\")\n)\n\nfunc TestNewWithAddress(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\n\tgot := o.Owner()\n\tpendingOwner := o.PendingOwner()\n\n\tuassert.Equal(t, got, alice)\n\tuassert.Equal(t, pendingOwner.String(), \"\")\n}\n\nfunc TestInitiateTransferOwnership(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.TransferOwnership(0, cur, bob)\n\t\turequire.NoError(t, err)\n\t}(cross(cur))\n\n\towner := o.Owner()\n\tpendingOwner := o.PendingOwner()\n\n\tuassert.Equal(t, owner, alice)\n\tuassert.Equal(t, pendingOwner, bob)\n}\n\nfunc TestTransferOwnership(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.TransferOwnership(0, cur, bob)\n\t\turequire.NoError(t, err)\n\t}(cross(cur))\n\n\towner := o.Owner()\n\tpendingOwner := o.PendingOwner()\n\n\tuassert.Equal(t, owner, alice)\n\tuassert.Equal(t, pendingOwner, bob)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) {\n\t\terr := o.AcceptOwnership(0, cur)\n\t\turequire.NoError(t, err)\n\t}(cross(cur))\n\n\towner = o.Owner()\n\tpendingOwner = o.PendingOwner()\n\n\tuassert.Equal(t, owner, bob)\n\tuassert.Equal(t, pendingOwner.String(), \"\")\n}\n\nfunc TestOwnedBy(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\tuassert.True(t, o.OwnedBy(alice))\n\tuassert.False(t, o.OwnedBy(bob))\n}\n\nfunc TestDropOwnership(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.DropOwnership(0, cur)\n\t\turequire.NoError(t, err, \"DropOwnership failed\")\n\t}(cross(cur))\n\n\towner := o.Owner()\n\tuassert.Empty(t, owner, \"owner should be empty\")\n}\n\nfunc TestDropOwnershipVulnerability(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\t// alice initiates ownership transfer to bob\n\t\terr := o.TransferOwnership(0, cur, bob)\n\t\turequire.NoError(t, err)\n\n\t\t// alice drops ownership while ownership transfer is pending\n\t\terr = o.DropOwnership(0, cur)\n\t\turequire.NoError(t, err)\n\t}(cross(cur))\n\n\towner := o.Owner()\n\tpendingOwner := o.PendingOwner()\n\tuassert.Empty(t, owner, \"owner should be empty\")\n\tuassert.Empty(t, pendingOwner.String(), \"pending owner should be empty\")\n\n\t// verify bob can no longer claim ownership\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) {\n\t\terr := o.AcceptOwnership(0, cur)\n\t\tuassert.ErrorContains(t, err, ErrNoPendingOwner.Error())\n\t}(cross(cur))\n}\n\n// Errors\n\nfunc TestErrUnauthorized(cur realm, t *testing.T) {\n\to := NewWithAddress(alice)\n\n\ttesting.SetRealm(testing.NewUserRealm(bob))\n\tfunc(cur realm) {\n\t\tuassert.ErrorContains(t, o.TransferOwnership(0, cur, alice), ErrUnauthorized.Error())\n\t\tuassert.ErrorContains(t, o.DropOwnership(0, cur), ErrUnauthorized.Error())\n\t}(cross(cur))\n}\n\nfunc TestErrInvalidAddress(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.TransferOwnership(0, cur, \"\")\n\t\tuassert.ErrorContains(t, err, ErrInvalidAddress.Error())\n\n\t\terr = o.TransferOwnership(0, cur, \"10000000001000000000100000000010000000001000000000\")\n\t\tuassert.ErrorContains(t, err, ErrInvalidAddress.Error())\n\t}(cross(cur))\n}\n\nfunc TestErrNoPendingOwner(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.AcceptOwnership(0, cur)\n\t\tuassert.ErrorContains(t, err, ErrNoPendingOwner.Error())\n\t}(cross(cur))\n}\n\nfunc TestErrPendingUnauthorized(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\to := NewWithAddress(alice)\n\tfunc(cur realm) {\n\t\terr := o.TransferOwnership(0, cur, bob)\n\t\turequire.NoError(t, err)\n\n\t\t// alice (still the owner, NOT the pending) tries to AcceptOwnership.\n\t\terr = o.AcceptOwnership(0, cur)\n\t\tuassert.ErrorContains(t, err, ErrPendingUnauthorized.Error())\n\t}(cross(cur))\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"4jx6CZT50uD5wB9i6WHQH8GJTScvMGe3HmIrzICHrdQgR5985i/HjD4AD/0KohHnmQhCWKW45TOz+ufbhTd3DQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"levenshtein","path":"gno.land/p/pol/levenshtein","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/pol/levenshtein\"\ngno = \"0.9\"\n"},{"name":"levenshtein.gno","body":"// from www.youtube.com/watch?v=d-Eq6x1yssU\n// Levenshtein distance is an algorithm to detect how close 2 words are.\n// For example, \"Float\" and \"Boats\" have a levenstein distance of 3.\n// Why ? Because you need 3 steps to go from Boats to Float\n// - We start with the word (Boats)\n// - Remove the s (Boat)\n// - Insert the l (Bloat)\n// - Substitute b with f (Float)\npackage levenshtein\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// Distance computes the Levenshtein distance between two strings.\n//\n// The Levenshtein distance is a measure of the similarity between two strings,\n// defined as the minimum number of single-character edits (insertions,\n// deletions, or substitutions) required to change one string into the other.\n//\n// For example, the Levenshtein distance between \"Float\" and \"Boats\" is 3:\n//   - Remove 's' from \"Boats\" → \"Boat\"\n//   - Insert 'l' → \"Bloat\"\n//   - Substitute 'b' with 'f' → \"Float\"\n//\n// This implementation uses a memory-efficient dynamic programming approach\n// that computes the distance in O(len(a) * len(b)) time and O(min(len(a), len(b))) space.\nfunc Distance(a, b string) uint {\n\t// make sure a is the longest\n\tif len(a) \u003c len(b) {\n\t\ta, b = b, a\n\t}\n\n\t// init first row\n\tline := make([]uint, len(b)+1)\n\tfor j := range line {\n\t\tline[j] = uint(j)\n\t}\n\n\t// main loop\n\tfor i := 1; i \u003c= len(a); i++ {\n\t\tprev := line[0]\n\t\tline[0] = uint(i)\n\t\tfor j := 1; j \u003c= len(b); j++ {\n\t\t\told := line[j]\n\t\t\tif a[i-1] == b[j-1] {\n\t\t\t\tline[j] = prev\n\t\t\t} else {\n\t\t\t\tline[j] = min3(\n\t\t\t\t\tline[j-1]+1, // insertion\n\t\t\t\t\tline[j]+1,   // deletion\n\t\t\t\t\tprev+1,      // substitution\n\t\t\t\t)\n\t\t\t}\n\t\t\tprev = old\n\t\t}\n\t}\n\treturn line[len(b)]\n}\n\n// Levenshteinable is an interface for types that can be compared using\n// Levenshtein distance. It provides the LString method, which returns\n// the string representation used for comparison.\n//\n// Note: This differs from the standard String() method, which may be used\n// for display or debugging.\ntype Levenshteinable interface {\n\tLString() string\n}\n\n// PickFromString returns the Levenshteinable object from the slice xs\n// that is closest in Levenshtein distance to the input string xStr.\n//\n// It returns the best match along with its distance. If xs is empty,\n// it returns (nil, 0).\n//\n// This function runs in O(n) time, where n is the length of the slice.\nfunc PickFromString(xStr string, xs []Levenshteinable) (Levenshteinable, uint) {\n\t// check for empty list\n\tif len(xs) == 0 {\n\t\treturn nil, 0\n\t}\n\n\t// initialize\n\tbestDistance := Distance(xStr, xs[0].LString())\n\tbest := xs[0]\n\n\t// loop through objects\n\tfor n := 1; n \u003c len(xs); n++ {\n\t\tdist := Distance(xStr, xs[n].LString())\n\t\tif dist \u003c bestDistance {\n\t\t\tbestDistance = dist\n\t\t\tbest = xs[n]\n\t\t}\n\t}\n\treturn best, bestDistance\n}\n\n// Pick returns the Levenshteinable object from the slice xs that is\n// closest in Levenshtein distance to the input object x.\n//\n// This is a convenience wrapper around PickFromString(x.LString(), xs).\n// It runs in O(n) time.\nfunc Pick(x Levenshteinable, xs []Levenshteinable) (Levenshteinable, uint) {\n\treturn PickFromString(x.LString(), xs)\n}\n\nfunc padLeftNum(n uint, width int) string {\n\ts := strconv.FormatUint(uint64(n), 10)\n\tfor len(s) \u003c width {\n\t\ts = \"0\" + s\n\t}\n\treturn s\n}\n\n// SeveralPickFromString returns the n closest Levenshteinable objects\n// to the input string xStr from the slice xs, using Levenshtein distance.\n//\n// If xs is empty, it returns nil. If n is 0, it returns an empty slice.\n//\n// The result is not guaranteed to be sorted by distance.\n// This function runs in O(n) time using an AVL tree for efficient selection.\nfunc SeveralPickFromString(xStr string, xs []Levenshteinable, n uint) []Levenshteinable {\n\t// error casing\n\tif len(xs) == 0 {\n\t\treturn nil\n\t}\n\tif n == 0 {\n\t\treturn []Levenshteinable{}\n\t}\n\n\ttree := avl.NewTree()\n\tmaxKey := \"\"\n\n\tfor k, v := range xs {\n\t\t// calculate distance and create unique key\n\t\td := Distance(xStr, v.LString())\n\t\t// ufmt.Sprintf(\"%016u:%d\", d, k)\n\t\tkey := padLeftNum(d, 6) + \":\" + strconv.Itoa(k) // no sprintf ( ;-;)\n\t\ttree.Set(key, v)\n\n\t\t// remove the element with the greater key\n\t\tif uint(tree.Size()) \u003e n {\n\t\t\ttree.ReverseIterate(\"\", \"\", func(k string, _ any) bool {\n\t\t\t\tmaxKey = k\n\t\t\t\treturn true\n\t\t\t})\n\t\t\ttree.Remove(maxKey)\n\t\t}\n\t}\n\n\t// create the list\n\tout := make([]Levenshteinable, 0, tree.Size())\n\ttree.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tout = append(out, v.(Levenshteinable))\n\t\treturn false\n\t})\n\treturn out\n}\n\n// SeveralPick returns the n closest Levenshteinable objects to the\n// input object x from the slice xs, using Levenshtein distance.\n//\n// This is a convenience wrapper around SeveralPickFromString(x.LString(), xs, n).\n// It runs in O(n) time.\nfunc SeveralPick(x Levenshteinable, xs []Levenshteinable, n uint) []Levenshteinable {\n\treturn SeveralPickFromString(x.LString(), xs, n)\n}\n\nfunc min3(a, b, c uint) uint {\n\tmin := a\n\tif b \u003c min {\n\t\tmin = b\n\t}\n\tif c \u003c min {\n\t\tmin = c\n\t}\n\treturn min\n}\n"},{"name":"levenshtein_test.gno","body":"package levenshtein\n\nimport \"testing\"\n\nfunc TestExample(t *testing.T) {\n\tif Distance(\"float\", \"boats\") != 3 {\n\t\tt.Fatal(\"expected float vs boats to be 3\")\n\t}\n\n\tif Distance(\"kitten\", \"sitting\") != 3 {\n\t\tt.Fatal(\"expected kitten vs sitting to be 3\")\n\t}\n\n\tif Distance(\"foo\", \"foo\") != 0 {\n\t\tt.Fatal(\"expected foo and foo to be 0 (the same)\")\n\t}\n}\n\ntype person struct {\n\tname     string\n\tlastName string\n}\n\nfunc (p person) LString() string {\n\treturn p.name + \" \" + p.lastName\n}\n\nfunc TestWithMyPhoneContact(t *testing.T) {\n\tcontacts := []Levenshteinable{\n\t\tperson{\"maman\", \"\"},\n\t\tperson{\"papa\", \"\"},\n\t\tperson{\"paul\", \"parisot\"},\n\t\tperson{\"leonardo\", \"dicaprio\"},\n\t\tperson{\"jenifer\", \"lawrence\"},\n\t\tperson{\"jae\", \"kwon\"},\n\t\tperson{\"justin\", \"bieber\"},\n\t}\n\twho, _ := PickFromString(\"pau parosit\", contacts)\n\t// println(who.LString(), n)\n\tif who.LString() != \"paul parisot\" {\n\t\tt.Fatal(\"expected to be paul parisot !\")\n\t}\n\n\twho, _ = PickFromString(\"jkwo0n\", contacts)\n\t// println(who.LString(), n)\n\tif who.LString() != \"jae kwon\" {\n\t\tt.Fatal(\"expected to be jae kwon !\")\n\t}\n\n\twho, _ = PickFromString(\"paulparis\", contacts)\n\t// println(who, n)\n}\n\nfunc TestWithAVL(t *testing.T) {\n\tcontacts := []Levenshteinable{\n\t\tperson{\"maman\", \"\"},\n\t\tperson{\"papa\", \"\"},\n\t\tperson{\"paul\", \"parisot\"},\n\t\tperson{\"leonardo\", \"dicaprio\"},\n\t\tperson{\"jenifer\", \"lawrence\"},\n\t\tperson{\"jae\", \"kwon\"},\n\t\tperson{\"justin\", \"bieber\"},\n\t}\n\tlist := SeveralPickFromString(\"paulparis\", contacts, 3)\n\tif len(list) != 3 {\n\t\tt.Fatal(\"expected 3 elements, got \", len(list))\n\t}\n\tif list[0] != contacts[2] {\n\t\tt.Fatal(\"expected first element to be paul parisot\")\n\t}\n\tif list[1] != contacts[1] {\n\t\tt.Fatal(\"expected second to be papa\")\n\t}\n}\n\nfunc TestWhitespaces(t *testing.T) {\n\tif Distance(\"hello world\", \"helloworld\") != 1 {\n\t\tt.Fatal(\"'helloworld' vs 'hello world' doesnt have 1 distance\")\n\t}\n\tif Distance(\"hello  world\", \"hello world\") != 1 {\n\t\tt.Fatal(\"'hello  world' vs 'hello world' doesnt have 1 distance\")\n\t}\n\tif Distance(\"hello world\", \"hello  world\") != 1 {\n\t\tt.Fatal(\"'hello world' vs 'hello  world' doesnt have 1 distance\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"w8eN6tPS1z2RwW/+SJxXLyaP9aQCpxLIEvXk94AZgrbW1xcwSPo1Uj2IlQ/XVNvfh1klPEBtxkLTwp8WbuKcBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"daocond","path":"gno.land/p/samcrew/daocond","files":[{"name":"ballot_avl.gno","body":"package daocond\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\ntype BallotAVL struct {\n\ttree *avl.Tree\n}\n\nfunc NewBallot() Ballot {\n\treturn \u0026BallotAVL{tree: avl.NewTree()}\n}\n\n// Implements Ballot interface\nfunc (b *BallotAVL) Vote(voter string, vote Vote) {\n\tb.tree.Set(voter, vote)\n}\n\n// Implements Ballot interface\nfunc (b *BallotAVL) Get(voter string) Vote {\n\tvote := b.tree.Get(voter)\n\tif vote == nil {\n\t\treturn VoteAbstain\n\t}\n\treturn vote.(Vote)\n}\n\n// Implements Ballot interface\nfunc (b *BallotAVL) Total() int {\n\treturn b.tree.Size()\n}\n\n// Implements Ballot interface\nfunc (b *BallotAVL) Iterate(fn func(voter string, vote Vote) bool) {\n\tb.tree.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tv, ok := value.(Vote)\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\t\treturn fn(key, v)\n\t})\n}\n"},{"name":"cond_and.gno","body":"package daocond\n\nimport (\n\t\"strings\"\n)\n\ntype andCond struct {\n\tconditions []Condition\n}\n\nfunc And(conditions ...Condition) Condition {\n\tif len(conditions) \u003c 2 {\n\t\tpanic(\"at least two conditions are required\")\n\t}\n\treturn \u0026andCond{conditions: conditions}\n}\n\n// Eval implements Condition.\nfunc (c *andCond) Eval(ballot Ballot) bool {\n\tfor _, condition := range c.conditions {\n\t\tif !condition.Eval(ballot) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// Signal implements Condition.\nfunc (c *andCond) Signal(ballot Ballot) float64 {\n\ttotalSignal := 0.0\n\tfor _, condition := range c.conditions {\n\t\ttotalSignal += condition.Signal(ballot)\n\t}\n\treturn totalSignal / float64(len(c.conditions))\n}\n\n// Render implements Condition.\nfunc (c *andCond) Render() string {\n\trenders := []string{}\n\tfor _, condition := range c.conditions {\n\t\trenders = append(renders, condition.Render())\n\t}\n\treturn \"[\" + strings.Join(renders, \" AND \") + \"]\"\n}\n\n// RenderWithVotes implements Condition.\nfunc (c *andCond) RenderWithVotes(ballot Ballot) string {\n\trenders := []string{}\n\tfor _, condition := range c.conditions {\n\t\trenders = append(renders, condition.RenderWithVotes(ballot))\n\t}\n\treturn \"[\" + strings.Join(renders, \" AND \") + \"]\"\n}\n\nvar _ Condition = (*andCond)(nil)\n"},{"name":"cond_gnolovedao.gno","body":"package daocond\n\nimport (\n\t\"errors\"\n\t\"math\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype gnolovDaoCondThreshold struct {\n\tthreshold            float64\n\troles                []string\n\thasRoleFn            func(memberId string, role string) bool\n\tusersWithRoleCountFn func(role string) uint32\n}\n\nvar roleWeights = []float64{3.0, 2.0, 1.0}\n\n// This is our implementation of the govdao codition following Jae Kwon's specification: https://gist.github.com/jaekwon/918ad325c4c8f7fb5d6e022e33cb7eb3\nfunc GnoloveDAOCondThreshold(threshold float64, roles []string, hasRoleFn func(memberId string, role string) bool, usersWithRoleCountFn func(role string) uint32) Condition {\n\tif threshold \u003c= 0 || threshold \u003e 1 {\n\t\tpanic(errors.New(\"invalid threshold\"))\n\t}\n\tif usersWithRoleCountFn == nil {\n\t\tpanic(errors.New(\"nil usersWithRoleCountFn\"))\n\t}\n\tif hasRoleFn == nil {\n\t\tpanic(errors.New(\"nil hasRoleFn\"))\n\t}\n\tif len(roles) \u003e 3 {\n\t\tpanic(\"the gnolove dao condition handles at most 3 roles\")\n\t}\n\treturn \u0026gnolovDaoCondThreshold{\n\t\tthreshold:            threshold,\n\t\troles:                roles,\n\t\thasRoleFn:            hasRoleFn,\n\t\tusersWithRoleCountFn: usersWithRoleCountFn,\n\t}\n}\n\n// Eval implements Condition.\nfunc (c *gnolovDaoCondThreshold) Eval(ballot Ballot) bool {\n\treturn c.yesRatio(ballot) \u003e= c.threshold\n}\n\n// Signal implements Condition.\nfunc (c *gnolovDaoCondThreshold) Signal(ballot Ballot) float64 {\n\treturn math.Min(c.yesRatio(ballot)/c.threshold, 1)\n}\n\n// Render implements Condition.\nfunc (c *gnolovDaoCondThreshold) Render() string {\n\trolePowers := []string{}\n\tfor i, role := range c.roles {\n\t\tweight := strconv.FormatFloat(roleWeights[i], 'f', 2, 64) // ufmt.Sprintf(\"%.2f\", ...) is not working\n\t\trolePowers = append(rolePowers, ufmt.Sprintf(\"%s =\u003e %s power\", role, weight))\n\t}\n\treturn ufmt.Sprintf(\"%g%% of total voting power | %s\", c.threshold*100, strings.Join(rolePowers, \" | \"))\n}\n\n// RenderWithVotes implements Condition.\nfunc (c *gnolovDaoCondThreshold) RenderWithVotes(ballot Ballot) string {\n\tvPowers, totalPower := c.computeVotingPowers()\n\trolePowers := []string{}\n\tfor _, role := range c.roles {\n\t\tweight := strconv.FormatFloat(vPowers[role], 'f', 2, 64) // ufmt.Sprintf(\"%.2f\", ...) is not working\n\t\trolePowers = append(rolePowers, ufmt.Sprintf(\"%s =\u003e %s power\", role, weight))\n\t}\n\ts := \"\"\n\ts += ufmt.Sprintf(\"%g%% of total voting power | %s\\n\\n\", c.threshold*100, strings.Join(rolePowers, \" | \"))\n\ts += ufmt.Sprintf(\"Threshold needed: %g%% of total voting power\\n\\n\", c.threshold*100)\n\ts += ufmt.Sprintf(\"Yes: %d/%d\\n\\n\", c.yesRatio(ballot), totalPower)\n\ts += ufmt.Sprintf(\"Voting power needed: %g%% of total voting power\\n\\n\", c.threshold*totalPower)\n\treturn s\n}\n\nvar _ Condition = (*gnolovDaoCondThreshold)(nil)\n\nfunc (c *gnolovDaoCondThreshold) yesRatio(ballot Ballot) float64 {\n\tvar totalYes float64\n\tvotingPowersByTier, totalPower := c.computeVotingPowers()\n\t// Case when there are zero T1s\n\tif totalPower == 0.0 {\n\t\treturn totalPower\n\t}\n\n\tballot.Iterate(func(voter string, vote Vote) bool {\n\t\tif vote != VoteYes {\n\t\t\treturn false\n\t\t}\n\t\ttier := c.getUserRole(voter)\n\t\ttotalYes += votingPowersByTier[tier]\n\t\treturn false\n\t})\n\treturn totalYes / totalPower\n}\n\nfunc (c *gnolovDaoCondThreshold) getUserRole(userID string) string {\n\tfor _, role := range c.roles {\n\t\tif c.hasRoleFn(userID, role) {\n\t\t\treturn role\n\t\t}\n\t}\n\tpanic(\"No role found for user\")\n}\n\nfunc (c *gnolovDaoCondThreshold) computeVotingPowers() (map[string]float64, float64) {\n\tvotingPowers := make(map[string]float64)\n\ttotalPower := 0.0\n\tcountsMembersPerRole := make(map[string]float64)\n\n\tfor _, role := range c.roles {\n\t\tcountsMembersPerRole[role] = float64(c.usersWithRoleCountFn(role))\n\t}\n\n\tfor i, role := range c.roles {\n\t\tif i == 0 {\n\t\t\tvotingPowers[role] = roleWeights[0] // Highest tier always gets max power (3.0)\n\t\t} else {\n\t\t\tvotingPowers[role] = computePower(countsMembersPerRole[c.roles[0]], countsMembersPerRole[role], roleWeights[i])\n\t\t}\n\t\ttotalPower += votingPowers[role] * countsMembersPerRole[role]\n\t}\n\n\treturn votingPowers, totalPower\n}\n\n// max power here is the number of votes each tier gets when we have\n// the same number of member on each tier\n// T2 = 2.0 and T1 = 1.0 with the ration T1/Tn\n// we compute the actual ratio\nfunc computePower(T1, Tn, maxPower float64) float64 {\n\t// If there are 0 Tn (T2, T3) just return the max power\n\t// we could also return 0.0 as voting power\n\tif Tn \u003c= 0.0 {\n\t\treturn maxPower\n\t}\n\n\tcomputedPower := (T1 / Tn) * maxPower\n\tif computedPower \u003e= maxPower {\n\t\t// If computed power is bigger than the max, this happens if Tn is lower than T1\n\t\t// cap the max power to max power.\n\t\treturn maxPower\n\t}\n\treturn computedPower\n}\n"},{"name":"cond_gnolovedao_test.gno","body":"package daocond\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n/*\nExample 1:\nT1 100 members --\u003e 300 VP, 3 votes per member\nT2 100 members --\u003e 200 VP, 2 votes per member\nT3 100 members --\u003e 100 VP, 1 votes per member\nExample 2:\n\nT1 100 members --\u003e 300 VP, 3 votes per member\nT2 50 members --\u003e 100 VP, 2 votes per member *\nT3 10 members --\u003e 10 VP, 1 votes per member *\nExample 3:\n\nT1 100 members --\u003e 300 VP, 3 votes per member\nT2 200 members --\u003e 200 VP, 1 votes per member *\nT3 100 members --\u003e 100 VP, 1 votes per member\nExample 4:\n\nT1 100 members --\u003e 300 VP, 3 votes per member\nT2 200 members --\u003e 200 VP, 1 votes per member *\nT3 1000 members --\u003e 100 VP, 0.1 votes per member\n*/\nfunc TestComputeVotingPowers(t *testing.T) {\n\ttype gnoloveDaoComposition struct {\n\t\tt1s                int\n\t\tt2s                int\n\t\tt3s                int\n\t\tabstainT3          bool\n\t\texpectedTotalPower float64\n\t\texpectedPowers     map[string]float64\n\t}\n\ttests := map[string]gnoloveDaoComposition{\n\t\t\"example 1\": {\n\t\t\tt1s:                100,\n\t\t\tt2s:                100,\n\t\t\tt3s:                100,\n\t\t\tabstainT3:          false,\n\t\t\texpectedTotalPower: 600,\n\t\t\texpectedPowers: map[string]float64{\n\t\t\t\t\"T1\": 3.0,\n\t\t\t\t\"T2\": 2.0,\n\t\t\t\t\"T3\": 1.0,\n\t\t\t},\n\t\t},\n\t\t\"example 2\": {\n\t\t\tt1s:                100,\n\t\t\tt2s:                50,\n\t\t\tt3s:                10,\n\t\t\tabstainT3:          false,\n\t\t\texpectedTotalPower: 410,\n\t\t\texpectedPowers: map[string]float64{\n\t\t\t\t\"T1\": 3.0,\n\t\t\t\t\"T2\": 2.0,\n\t\t\t\t\"T3\": 1.0,\n\t\t\t},\n\t\t},\n\t\t\"example 3\": {\n\t\t\tt1s:                100,\n\t\t\tt2s:                200,\n\t\t\tt3s:                100,\n\t\t\tabstainT3:          false,\n\t\t\texpectedTotalPower: 600,\n\t\t\texpectedPowers: map[string]float64{\n\t\t\t\t\"T1\": 3.0,\n\t\t\t\t\"T2\": 1.0,\n\t\t\t\t\"T3\": 1.0,\n\t\t\t},\n\t\t},\n\t\t\"example 4\": {\n\t\t\tt1s:                100,\n\t\t\tt2s:                200,\n\t\t\tt3s:                1000,\n\t\t\tabstainT3:          false,\n\t\t\texpectedTotalPower: 600,\n\t\t\texpectedPowers: map[string]float64{\n\t\t\t\t\"T1\": 3.0,\n\t\t\t\t\"T2\": 1.0,\n\t\t\t\t\"T3\": 0.1,\n\t\t\t},\n\t\t},\n\t\t\"0 -T1s\": {\n\t\t\tt1s:                0,\n\t\t\tt2s:                100,\n\t\t\tt3s:                100,\n\t\t\tabstainT3:          false,\n\t\t\texpectedTotalPower: 0,\n\t\t\texpectedPowers: map[string]float64{\n\t\t\t\t\"T1\": 3.0,\n\t\t\t\t\"T2\": 0.0,\n\t\t\t\t\"T3\": 0.0,\n\t\t\t},\n\t\t},\n\t\t\"100 T1, 1 T2, 1 T3\": {\n\t\t\tt1s:                100,\n\t\t\tt2s:                1,\n\t\t\tt3s:                1,\n\t\t\tabstainT3:          false,\n\t\t\texpectedTotalPower: 303,\n\t\t\texpectedPowers: map[string]float64{\n\t\t\t\t\"T1\": 3.0,\n\t\t\t\t\"T2\": 2.0,\n\t\t\t\t\"T3\": 1.0,\n\t\t\t},\n\t\t},\n\t\t\"T3 Abstaining\": {\n\t\t\tt1s:                100,\n\t\t\tt2s:                100,\n\t\t\tt3s:                100,\n\t\t\texpectedTotalPower: 500,\n\t\t\tabstainT3:          true,\n\t\t\texpectedPowers: map[string]float64{\n\t\t\t\t\"T1\": 3.0,\n\t\t\t\t\"T2\": 2.0,\n\t\t\t},\n\t\t},\n\t}\n\n\tfor name, composition := range tests {\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tdao := newMockDAO()\n\t\t\tfor i := 0; i \u003c composition.t1s; i++ {\n\t\t\t\tdao.addUser(ufmt.Sprintf(\"%d_T1\", i), []string{\"T1\"})\n\t\t\t}\n\t\t\tfor i := 0; i \u003c composition.t2s; i++ {\n\t\t\t\tdao.addUser(ufmt.Sprintf(\"%d_T2\", i), []string{\"T2\"})\n\t\t\t}\n\t\t\tfor i := 0; i \u003c composition.t3s; i++ {\n\t\t\t\tdao.addUser(ufmt.Sprintf(\"%d_T3\", i), []string{\"T3\"})\n\t\t\t}\n\n\t\t\troles := []string{\"T1\", \"T2\"}\n\t\t\tif !composition.abstainT3 {\n\t\t\t\troles = append(roles, \"T3\")\n\t\t\t}\n\n\t\t\tcond := \u0026gnolovDaoCondThreshold{\n\t\t\t\tthreshold:            0.6,\n\t\t\t\thasRoleFn:            dao.hasRole,\n\t\t\t\troles:                roles,\n\t\t\t\tusersWithRoleCountFn: dao.usersWithRoleCount,\n\t\t\t}\n\t\t\tvotingPowers, totalPower := cond.computeVotingPowers()\n\t\t\tfor tier, expectedPower := range composition.expectedPowers {\n\t\t\t\tif votingPowers[tier] != expectedPower {\n\t\t\t\t\tt.Fail()\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif totalPower != composition.expectedTotalPower {\n\t\t\t\tt.Fail()\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestEval(t *testing.T) {\n\ttype gnoloveDaoVotes struct {\n\t\tvotesT1      []Vote\n\t\tvotesT2      []Vote\n\t\tvotesT3      []Vote\n\t\tthreshold    float64\n\t\texpectedEval bool\n\t\texpectedYes  float64\n\t\tabstainT3    bool\n\t\tpanic        bool\n\t}\n\ttests := map[string]gnoloveDaoVotes{\n\t\t\"2/6% Yes\": { //0.3333\n\t\t\tvotesT1:      []Vote{VoteNo},                             // 3 voting power\n\t\t\tvotesT2:      []Vote{VoteYes, VoteYes, VoteYes, VoteYes}, // 2 voting power combined\n\t\t\tvotesT3:      []Vote{VoteNo},\n\t\t\texpectedEval: false,\n\t\t\tabstainT3:    false,\n\t\t\tthreshold:    0.45,\n\t\t\texpectedYes:  2.0 / 6.0,\n\t\t\tpanic:        false,\n\t\t},\n\t\t\"50% Yes\": {\n\t\t\tvotesT1:      []Vote{VoteNo},                             // 3 voting power\n\t\t\tvotesT2:      []Vote{VoteYes, VoteYes, VoteYes, VoteYes}, // 2 voting power combined\n\t\t\tvotesT3:      []Vote{VoteYes},\n\t\t\texpectedEval: true,\n\t\t\tabstainT3:    false,\n\t\t\tthreshold:    0.45,\n\t\t\texpectedYes:  3.0 / 6.0,\n\t\t\tpanic:        false,\n\t\t},\n\t\t\"several T2 \u0026 T3\": {\n\t\t\tvotesT1: []Vote{VoteNo, VoteNo}, // 6 voting power\n\t\t\t// 10 T2 total power (2/3) powerT1 = 4, 4/10 0.4 each\n\t\t\tvotesT2: []Vote{VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes},\n\t\t\t// 4 T3 total power (1/3) powerT1 = 2, 2/4 0.5 each\n\t\t\tvotesT3:      []Vote{VoteYes, VoteNo, VoteYes, VoteNo},\n\t\t\texpectedEval: false,\n\t\t\tabstainT3:    false,\n\t\t\tthreshold:    0.42, //total power = 6+4+2 T3yes = 1, T2yes = 4 T1yes = 0 totalYes = 0.41666666666 (5/12)\n\t\t\texpectedYes:  5.0 / 12.0,\n\t\t\tpanic:        false,\n\t\t},\n\t\t\"several T2 \u0026 T3 eval true\": {\n\t\t\tvotesT1: []Vote{VoteNo, VoteNo}, // 6 voting power\n\t\t\t// 10 T2 total power (2/3) powerT1 = 4, 4/10 0.4 each\n\t\t\tvotesT2: []Vote{VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes, VoteYes},\n\t\t\t// 4 T3 total power (1/3) powerT1 = 2, 2/4 0.5 each\n\t\t\tvotesT3:      []Vote{VoteYes, VoteYes, VoteYes, VoteNo},\n\t\t\texpectedEval: true,\n\t\t\tabstainT3:    false,\n\t\t\tthreshold:    0.42, //total power = 6+4+2 T3yes = 1.5, T2yes = 4 T1yes = 0 totalYes = 0.45833333333 (5.5/12)\n\t\t\texpectedYes:  5.5 / 12.0,\n\t\t\tpanic:        false,\n\t\t},\n\t\t\"only T1s\": {\n\t\t\tvotesT1:      []Vote{VoteYes, VoteNo}, // 6 voting power\n\t\t\texpectedEval: false,\n\t\t\tabstainT3:    false,\n\t\t\tthreshold:    0.6,\n\t\t\texpectedYes:  3.0 / 6.0,\n\t\t\tpanic:        false,\n\t\t},\n\t\t\"only T3s\": { // as T2 \u0026 T3 power is capped as power of T1, in this case the power will be 0 everywhere\n\t\t\tvotesT3:      []Vote{VoteYes, VoteYes, VoteYes, VoteYes}, // voting power = 0, 0 each\n\t\t\texpectedEval: false,\n\t\t\tabstainT3:    false,\n\t\t\tthreshold:    0.6,\n\t\t\texpectedYes:  0.0,\n\t\t},\n\t\t\"T3 abstaining\": {\n\t\t\tvotesT1:      []Vote{VoteYes, VoteNo}, // 6 voting power\n\t\t\tvotesT2:      []Vote{VoteYes, VoteYes},\n\t\t\tvotesT3:      []Vote{},\n\t\t\texpectedEval: true,\n\t\t\tabstainT3:    true,\n\t\t\tthreshold:    0.6,\n\t\t\texpectedYes:  7.0 / 10.0,\n\t\t\tpanic:        false,\n\t\t},\n\t\t\"T3 abstaining w/ votes\": {\n\t\t\tvotesT1:      []Vote{VoteYes, VoteNo}, // 6 voting power\n\t\t\tvotesT2:      []Vote{VoteYes, VoteYes},\n\t\t\tvotesT3:      []Vote{VoteYes, VoteNo},\n\t\t\texpectedEval: true,\n\t\t\tabstainT3:    true,\n\t\t\tthreshold:    0.6,\n\t\t\texpectedYes:  7.0 / 10.0,\n\t\t\tpanic:        true, // a user with T3 when T3 is abstaining should panic\n\t\t},\n\t}\n\n\tfor name, tdata := range tests {\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tif tdata.panic {\n\t\t\t\tdefer func() {\n\t\t\t\t\tif r := recover(); r == nil {\n\t\t\t\t\t\tt.Errorf(\"The code did not panic\")\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t}\n\t\t\tvotes := map[string]Vote{}\n\t\t\tdao := newMockDAO()\n\t\t\tfor i, vote := range tdata.votesT1 {\n\t\t\t\tuserID := ufmt.Sprintf(\"%d_T1\", i)\n\t\t\t\tdao.addUser(userID, []string{\"T1\"})\n\t\t\t\tvotes[userID] = vote\n\t\t\t}\n\n\t\t\tfor i, vote := range tdata.votesT2 {\n\t\t\t\tuserID := ufmt.Sprintf(\"%d_T2\", i)\n\t\t\t\tdao.addUser(userID, []string{\"T2\"})\n\t\t\t\tvotes[userID] = vote\n\t\t\t}\n\n\t\t\tfor i, vote := range tdata.votesT3 {\n\t\t\t\tuserID := ufmt.Sprintf(\"%d_T3\", i)\n\t\t\t\tdao.addUser(userID, []string{\"T3\"})\n\t\t\t\tvotes[userID] = vote\n\t\t\t}\n\t\t\tballot := NewBallot()\n\t\t\tfor userId, vote := range votes {\n\t\t\t\tballot.Vote(userId, vote)\n\t\t\t}\n\t\t\troles := []string{\"T1\", \"T2\"}\n\t\t\tif !tdata.abstainT3 {\n\t\t\t\troles = append(roles, \"T3\")\n\t\t\t}\n\t\t\tcond := GnoloveDAOCondThreshold(tdata.threshold, roles, dao.hasRole, dao.usersWithRoleCount)\n\t\t\t// Get percent of total yes\n\t\t\turequire.Equal(t, tdata.expectedEval, cond.Eval(ballot))\n\t\t})\n\t}\n}\n\ntype mockDAO struct {\n\tmembers map[string][]string\n\troles   map[string][]string\n}\n\nfunc newMockDAO() *mockDAO {\n\treturn \u0026mockDAO{\n\t\tmembers: map[string][]string{},\n\t\troles:   map[string][]string{}, // roles to users\n\t}\n}\n\nfunc (m *mockDAO) addUser(memberId string, roles []string) {\n\tm.members[memberId] = roles\n\tfor _, memberRole := range roles {\n\t\tm.roles[memberRole] = append(m.roles[memberRole], memberId)\n\t}\n}\nfunc (m *mockDAO) usersWithRoleCount(role string) uint32 {\n\treturn uint32(len(m.roles[role]))\n}\n\nfunc (m *mockDAO) hasRole(memberId string, role string) bool {\n\troles, ok := m.members[memberId]\n\tif !ok {\n\t\treturn false\n\t}\n\tfor _, memberRole := range roles {\n\t\tif memberRole == role {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"},{"name":"cond_members_threshold.gno","body":"package daocond\n\nimport (\n\t\"errors\"\n\t\"math\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype membersThresholdCond struct {\n\tisMemberFn     func(memberId string) bool\n\tmembersCountFn func() uint64\n\tthreshold      float64\n}\n\n// SECURITY XXX: MembersThreshold captures two predicate functions\n// (isMemberFn, membersCountFn) at construction. Whoever constructs the\n// condition controls what counts as \"is this voter a member\" and \"how\n// many members exist\" — both inputs to the threshold check. Hostile\n// predicates can pass any vote regardless of actual membership, or\n// reject all votes (DoS).\n// In practice these are wired up by basedao/commondao with their own\n// member store, so the DAO author IS the trust boundary. Don't\n// construct a MembersThreshold from external-realm-supplied predicates.\nfunc MembersThreshold(threshold float64, isMemberFn func(memberId string) bool, membersCountFn func() uint64) Condition {\n\tif threshold \u003c= 0 || threshold \u003e 1 {\n\t\tpanic(errors.New(\"invalid threshold\"))\n\t}\n\tif isMemberFn == nil {\n\t\tpanic(errors.New(\"nil isMemberFn\"))\n\t}\n\tif membersCountFn == nil {\n\t\tpanic(errors.New(\"nil membersCountFn\"))\n\t}\n\treturn \u0026membersThresholdCond{\n\t\tthreshold:      threshold,\n\t\tisMemberFn:     isMemberFn,\n\t\tmembersCountFn: membersCountFn,\n\t}\n}\n\n// Eval implements Condition.\nfunc (c *membersThresholdCond) Eval(ballot Ballot) bool {\n\treturn c.yesRatio(ballot) \u003e= c.threshold\n}\n\n// Signal implements Condition.\nfunc (c *membersThresholdCond) Signal(ballot Ballot) float64 {\n\treturn math.Min(c.yesRatio(ballot)/c.threshold, 1)\n}\n\n// Render implements Condition.\nfunc (c *membersThresholdCond) Render() string {\n\treturn ufmt.Sprintf(\"%g%% of members\", c.threshold*100)\n}\n\n// RenderWithVotes implements Condition.\nfunc (c *membersThresholdCond) RenderWithVotes(ballot Ballot) string {\n\ts := \"\"\n\ts += ufmt.Sprintf(\"to meet the condition, %g%% of members must vote yes\\n\\n\", c.threshold*100)\n\ts += ufmt.Sprintf(\"Yes: %d/%d = %g%%\\n\\n\", c.totalYes(ballot), c.membersCountFn(), c.yesRatio(ballot)*100)\n\treturn s\n}\n\nvar _ Condition = (*membersThresholdCond)(nil)\n\nfunc (c *membersThresholdCond) yesRatio(ballot Ballot) float64 {\n\treturn float64(c.totalYes(ballot)) / float64(c.membersCountFn())\n}\n\nfunc (c *membersThresholdCond) totalYes(ballot Ballot) uint64 {\n\ttotalYes := uint64(0)\n\tballot.Iterate(func(voter string, vote Vote) bool {\n\t\tif vote == VoteYes \u0026\u0026 c.isMemberFn(voter) {\n\t\t\ttotalYes += 1\n\t\t}\n\t\treturn false\n\t})\n\treturn totalYes\n}\n"},{"name":"cond_or.gno","body":"package daocond\n\nimport (\n\t\"math\"\n\t\"strings\"\n)\n\ntype orCond struct {\n\tconditions []Condition\n}\n\nfunc Or(conditions ...Condition) Condition {\n\tif len(conditions) \u003c 2 {\n\t\tpanic(\"at least two conditions are required\")\n\t}\n\treturn \u0026orCond{conditions: conditions}\n}\n\n// Eval implements Condition.\nfunc (c *orCond) Eval(ballot Ballot) bool {\n\tfor _, condition := range c.conditions {\n\t\tif condition.Eval(ballot) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n// Signal implements Condition.\nfunc (c *orCond) Signal(ballot Ballot) float64 {\n\tmaxSignal := 0.0\n\tfor _, condition := range c.conditions {\n\t\tmaxSignal = math.Max(maxSignal, condition.Signal(ballot))\n\t}\n\treturn maxSignal\n}\n\n// Render implements Condition.\nfunc (c *orCond) Render() string {\n\trenders := []string{}\n\tfor _, condition := range c.conditions {\n\t\trenders = append(renders, condition.Render())\n\t}\n\treturn \"[\" + strings.Join(renders, \" OR \") + \"]\"\n}\n\n// RenderWithVotes implements Condition.\nfunc (c *orCond) RenderWithVotes(ballot Ballot) string {\n\trenders := []string{}\n\tfor _, condition := range c.conditions {\n\t\trenders = append(renders, condition.RenderWithVotes(ballot))\n\t}\n\treturn \"[\" + strings.Join(renders, \" OR \") + \"]\"\n}\n\nvar _ Condition = (*orCond)(nil)\n"},{"name":"cond_role_count.gno","body":"package daocond\n\nimport (\n\t\"errors\"\n\t\"math\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype roleCountCond struct {\n\thasRoleFn func(memberId string, role string) bool\n\tcount     uint64\n\trole      string\n}\n\nfunc RoleCount(count uint64, role string, hasRoleFn func(memberId string, role string) bool) Condition {\n\tif count == 0 {\n\t\tpanic(errors.New(\"count must be greater than 0\"))\n\t}\n\tif role == \"\" {\n\t\tpanic(errors.New(\"role must not be empty\"))\n\t}\n\tif hasRoleFn == nil {\n\t\tpanic(errors.New(\"nil hasRoleFn\"))\n\t}\n\treturn \u0026roleCountCond{\n\t\tcount:     count,\n\t\thasRoleFn: hasRoleFn,\n\t\trole:      role,\n\t}\n}\n\n// Eval implements Condition.\nfunc (c *roleCountCond) Eval(ballot Ballot) bool {\n\treturn c.totalYes(ballot) \u003e= c.count\n}\n\n// Signal implements Condition.\nfunc (c *roleCountCond) Signal(ballot Ballot) float64 {\n\treturn math.Min(float64(c.totalYes(ballot))/float64(c.count), 1)\n}\n\n// Render implements Condition.\nfunc (c *roleCountCond) Render() string {\n\treturn ufmt.Sprintf(\"%d %s\", c.count, c.role)\n}\n\n// RenderWithVotes implements Condition.\nfunc (c *roleCountCond) RenderWithVotes(ballot Ballot) string {\n\ts := \"\"\n\ts += ufmt.Sprintf(\"to meet the condition, %d members with role %s must vote yes\\n\\n\", c.count, c.role)\n\ts += ufmt.Sprintf(\"Yes: %d/%d\\n\\n\", c.totalYes(ballot), c.count)\n\treturn s\n}\n\nvar _ Condition = (*roleCountCond)(nil)\n\nfunc (c *roleCountCond) totalYes(ballot Ballot) uint64 {\n\ttotalYes := uint64(0)\n\tballot.Iterate(func(voter string, vote Vote) bool {\n\t\tif vote == VoteYes \u0026\u0026 c.hasRoleFn(voter, c.role) {\n\t\t\ttotalYes += 1\n\t\t}\n\t\treturn false\n\t})\n\treturn totalYes\n}\n"},{"name":"cond_role_treshold.gno","body":"package daocond\n\nimport (\n\t\"errors\"\n\t\"math\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype roleThresholdCond struct {\n\thasRoleFn        func(memberId string, role string) bool\n\tusersRoleCountFn func(role string) uint32\n\tthreshold        float64\n\trole             string\n}\n\nfunc RoleThreshold(threshold float64, role string, hasRoleFn func(memberId string, role string) bool, usersRoleCountFn func(role string) uint32) Condition {\n\tif threshold \u003c= 0 || threshold \u003e 1 {\n\t\tpanic(errors.New(\"invalid threshold\"))\n\t}\n\tif hasRoleFn == nil {\n\t\tpanic(errors.New(\"nil hasRoleFn\"))\n\t}\n\tif usersRoleCountFn == nil {\n\t\tpanic(errors.New(\"nil usersRoleCountFn\"))\n\t}\n\treturn \u0026roleThresholdCond{\n\t\tthreshold:        threshold,\n\t\thasRoleFn:        hasRoleFn,\n\t\tusersRoleCountFn: usersRoleCountFn,\n\t\trole:             role,\n\t}\n}\n\n// Eval implements Condition.\nfunc (c *roleThresholdCond) Eval(ballot Ballot) bool {\n\treturn c.yesRatio(ballot) \u003e= c.threshold\n}\n\n// Signal implements Condition.\nfunc (c *roleThresholdCond) Signal(ballot Ballot) float64 {\n\treturn math.Min(c.yesRatio(ballot)/c.threshold, 1)\n}\n\n// Render implements Condition.\nfunc (c *roleThresholdCond) Render() string {\n\treturn ufmt.Sprintf(\"%g%% of %s members\", c.threshold*100, c.role)\n}\n\n// RenderWithVotes implements Condition.\nfunc (c *roleThresholdCond) RenderWithVotes(ballot Ballot) string {\n\ts := \"\"\n\ts += ufmt.Sprintf(\"To meet the condition, %g%% of %s members with role %s must vote yes\\n\\n\", c.threshold*100, c.role)\n\ts += ufmt.Sprintf(\"Yes: %d/%d = %g%%\\n\\n\", c.totalYes(ballot), c.usersRoleCountFn(c.role), c.yesRatio(ballot)*100)\n\treturn s\n}\n\nvar _ Condition = (*roleThresholdCond)(nil)\n\nfunc (c *roleThresholdCond) yesRatio(ballot Ballot) float64 {\n\treturn float64(c.totalYes(ballot)) / float64(c.usersRoleCountFn(c.role))\n}\n\nfunc (c *roleThresholdCond) totalYes(ballot Ballot) uint32 {\n\ttotalYes := uint32(0)\n\tballot.Iterate(func(voter string, vote Vote) bool {\n\t\tif vote == VoteYes \u0026\u0026 c.hasRoleFn(voter, c.role) {\n\t\t\ttotalYes += 1\n\t\t}\n\t\treturn false\n\t})\n\treturn totalYes\n}\n"},{"name":"daocond.gno","body":"package daocond\n\n// We made conditions stateless to avoid complexity of handling events like (member added, member removed, role assigned, role removed, etc.)\n// This model should work pretty good for small DAOs with few voters.\n// In future, we should probably add a stateful model for large DAOs with many voters.\n\ntype Condition interface {\n\tEval(ballot Ballot) bool\n\tSignal(ballot Ballot) float64\n\n\tRender() string\n\tRenderWithVotes(ballot Ballot) string\n}\n\ntype Ballot interface {\n\tVote(voter string, vote Vote)\n\tGet(voter string) Vote\n\tTotal() int\n\n\tIterate(fn func(voter string, vote Vote) bool)\n}\n\ntype Vote string\n\nconst (\n\tVoteYes     = \"yes\"\n\tVoteNo      = \"no\"\n\tVoteAbstain = \"abstain\"\n)\n"},{"name":"daocond_test.gno","body":"package daocond_test\n\nimport (\n\t\"errors\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/urequire/v0\"\n\t\"gno.land/p/samcrew/daocond\"\n)\n\nfunc TestCondition(t *testing.T) {\n\tdao := newMockDAO()\n\n\t// leaf conditions\n\tmembersMajority := daocond.MembersThreshold(0.6, dao.isMember, dao.membersCount)\n\tpublicRelationships := daocond.RoleCount(1, \"public-relationships\", dao.hasRole)\n\tfinanceOfficer := daocond.RoleCount(1, \"finance-officer\", dao.hasRole)\n\n\turequire.Equal(t, \"60% of members\", membersMajority.Render())\n\turequire.Equal(t, \"1 public-relationships\", publicRelationships.Render())\n\turequire.Equal(t, \"1 finance-officer\", financeOfficer.Render())\n\n\t// ressource expressions\n\tressources := map[string]daocond.Condition{\n\t\t\"social.post\":    daocond.And(publicRelationships, membersMajority),\n\t\t\"finance.invest\": daocond.Or(financeOfficer, membersMajority),\n\t}\n\n\turequire.Equal(t, \"[1 public-relationships AND 60% of members]\", ressources[\"social.post\"].Render())\n\turequire.Equal(t, \"[1 finance-officer OR 60% of members]\", ressources[\"finance.invest\"].Render())\n}\n\nfunc TestEval(t *testing.T) {\n\tsetups := []struct {\n\t\tname  string\n\t\tsetup func(dao *mockDAO)\n\t}{\n\t\t{name: \"basic\", setup: func(dao *mockDAO) {\n\t\t\tmembersMajority := daocond.MembersThreshold(0.6, dao.isMember, dao.membersCount)\n\t\t\tpublicRelationships := daocond.RoleCount(1, \"public-relationships\", dao.hasRole)\n\t\t\tfinanceOfficer := daocond.RoleCount(1, \"finance-officer\", dao.hasRole)\n\t\t\tdao.resources = map[string]daocond.Condition{\n\t\t\t\t\"social.post\":    daocond.And(publicRelationships, membersMajority),\n\t\t\t\t\"finance.invest\": daocond.Or(financeOfficer, membersMajority),\n\t\t\t}\n\t\t}},\n\t}\n\n\tcases := []struct {\n\t\tname     string\n\t\tresource string\n\t\tphases   []testPhase\n\t}{\n\t\t{\n\t\t\tname:     \"post with public-relationships\",\n\t\t\tresource: \"social.post\",\n\t\t\tphases: []testPhase{{\n\t\t\t\tvotes: map[string]daocond.Vote{\n\t\t\t\t\t\"alice\": \"yes\",\n\t\t\t\t\t\"bob\":   \"yes\",\n\t\t\t\t\t\"eve\":   \"no\",\n\t\t\t\t},\n\t\t\t\tresult: true,\n\t\t\t}},\n\t\t},\n\t\t{\n\t\t\tname:     \"post without public-relationships\",\n\t\t\tresource: \"social.post\",\n\t\t\tphases: []testPhase{{\n\t\t\t\tvotes: map[string]daocond.Vote{\n\t\t\t\t\t\"alice\": \"yes\",\n\t\t\t\t\t\"bob\":   \"no\",\n\t\t\t\t\t\"eve\":   \"yes\",\n\t\t\t\t},\n\t\t\t\tresult: false,\n\t\t\t}},\n\t\t},\n\t\t{\n\t\t\tname:     \"post after public-relationships changes\",\n\t\t\tresource: \"social.post\",\n\t\t\tphases: []testPhase{\n\t\t\t\t{\n\t\t\t\t\tvotes: map[string]daocond.Vote{\n\t\t\t\t\t\t\"alice\": \"yes\",\n\t\t\t\t\t\t\"bob\":   \"yes\",\n\t\t\t\t\t\t\"eve\":   \"no\",\n\t\t\t\t\t},\n\t\t\t\t\tresult: true,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tchanges: func(dao *mockDAO) {\n\t\t\t\t\t\tdao.unassignRole(\"bob\", \"public-relationships\")\n\t\t\t\t\t},\n\t\t\t\t\tresult: false,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tchanges: func(dao *mockDAO) {\n\t\t\t\t\t\tdao.assignRole(\"alice\", \"public-relationships\")\n\t\t\t\t\t},\n\t\t\t\t\tresult: true,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname:     \"post public-relationships alone\",\n\t\t\tresource: \"social.post\",\n\t\t\tphases: []testPhase{{\n\t\t\t\tvotes: map[string]daocond.Vote{\n\t\t\t\t\t\"alice\": \"no\",\n\t\t\t\t\t\"bob\":   \"yes\",\n\t\t\t\t\t\"eve\":   \"no\",\n\t\t\t\t},\n\t\t\t\tresult: false,\n\t\t\t}},\n\t\t},\n\t\t{\n\t\t\tname:     \"invest with finance officer\",\n\t\t\tresource: \"finance.invest\",\n\t\t\tphases: []testPhase{{\n\t\t\t\tvotes: map[string]daocond.Vote{\n\t\t\t\t\t\"alice\": \"yes\",\n\t\t\t\t\t\"bob\":   \"no\",\n\t\t\t\t\t\"eve\":   \"no\",\n\t\t\t\t},\n\t\t\t\tresult: true,\n\t\t\t}},\n\t\t},\n\t\t{\n\t\t\tname:     \"invest without finance officer\",\n\t\t\tresource: \"finance.invest\",\n\t\t\tphases: []testPhase{{\n\t\t\t\tvotes: map[string]daocond.Vote{\n\t\t\t\t\t\"alice\": \"no\",\n\t\t\t\t\t\"bob\":   \"yes\",\n\t\t\t\t\t\"eve\":   \"yes\",\n\t\t\t\t},\n\t\t\t\tresult: true,\n\t\t\t}},\n\t\t},\n\t\t{\n\t\t\tname:     \"invest alone\",\n\t\t\tresource: \"finance.invest\",\n\t\t\tphases: []testPhase{{\n\t\t\t\tvotes: map[string]daocond.Vote{\n\t\t\t\t\t\"alice\": \"no\",\n\t\t\t\t\t\"bob\":   \"no\",\n\t\t\t\t\t\"eve\":   \"yes\",\n\t\t\t\t},\n\t\t\t\tresult: false,\n\t\t\t}},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tfor _, s := range setups {\n\t\t\tt.Run(tc.name+\" \"+s.name, func(t *testing.T) {\n\t\t\t\tdao := newMockDAO()\n\t\t\t\ts.setup(dao)\n\n\t\t\t\tresource, ok := dao.resources[tc.resource]\n\t\t\t\turequire.True(t, ok)\n\n\t\t\t\tballot := daocond.NewBallot()\n\t\t\t\tfor _, phase := range tc.phases {\n\t\t\t\t\tif phase.changes != nil {\n\t\t\t\t\t\tphase.changes(dao)\n\t\t\t\t\t}\n\t\t\t\t\tif phase.votes != nil {\n\t\t\t\t\t\tfor memberId, vote := range phase.votes {\n\t\t\t\t\t\t\tballot.Vote(memberId, vote)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tresult := resource.Eval(ballot)\n\t\t\t\t\tif phase.result != result {\n\t\t\t\t\t\tprintln(\"State:\", resource.RenderWithVotes(ballot))\n\t\t\t\t\t}\n\t\t\t\t\turequire.Equal(t, phase.result, result)\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t}\n}\n\ntype testPhase struct {\n\tchanges func(dao *mockDAO)\n\tvotes   map[string]daocond.Vote\n\tresult  bool\n}\n\ntype mockDAO struct {\n\tmembers   map[string][]string\n\troles     map[string][]string\n\tresources map[string]daocond.Condition\n}\n\nfunc newMockDAO() *mockDAO {\n\treturn \u0026mockDAO{\n\t\tmembers: map[string][]string{\n\t\t\t\"alice\": []string{\"finance-officer\"},\n\t\t\t\"bob\":   []string{\"public-relationships\"},\n\t\t\t\"eve\":   []string{},\n\t\t},\n\t\troles: map[string][]string{\n\t\t\t\"finance-officer\":      []string{\"alice\"},\n\t\t\t\"public-relationships\": []string{\"bob\"},\n\t\t}, // roles to users\n\t\tresources: make(map[string]daocond.Condition),\n\t}\n}\n\nfunc (m *mockDAO) assignRole(userId string, role string) {\n\troles, ok := m.members[userId]\n\tif !ok {\n\t\tpanic(errors.New(\"unknown member\"))\n\t}\n\tm.members[userId], ok = strsadd(roles, role)\n}\n\nfunc (m *mockDAO) unassignRole(userId string, role string) {\n\troles, ok := m.members[userId]\n\tif !ok {\n\t\tpanic(errors.New(\"unknown member\"))\n\t}\n\tm.members[userId], ok = strsrm(roles, role)\n}\n\nfunc (m *mockDAO) isMember(memberId string) bool {\n\t_, ok := m.members[memberId]\n\treturn ok\n}\n\nfunc (m *mockDAO) membersCount() uint64 {\n\treturn uint64(len(m.members))\n}\n\nfunc (m *mockDAO) hasRole(memberId string, role string) bool {\n\troles, ok := m.members[memberId]\n\tif !ok {\n\t\treturn false\n\t}\n\tfor _, memberRole := range roles {\n\t\tif memberRole == role {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc strsrm(strs []string, val string) ([]string, bool) {\n\tremoved := false\n\tres := []string{}\n\tfor _, str := range strs {\n\t\tif str == val {\n\t\t\tremoved = true\n\t\t\tcontinue\n\t\t}\n\t\tres = append(res, str)\n\t}\n\treturn res, removed\n}\n\nfunc strsadd(strs []string, val string) ([]string, bool) {\n\tfor _, str := range strs {\n\t\tif str == val {\n\t\t\treturn strs, false\n\t\t}\n\t}\n\treturn append(strs, val), true\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/daocond\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"HhU2EVvJEE/+uD1f+F4dmKYN+pKR6GTdq0sX6c+PvU5+/6J38a5RO/LM8iahvvZ36nKc1dJqKnCOP4TfcQ1xCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"daokit","path":"gno.land/p/samcrew/daokit","files":[{"name":"README.md","body":"# daokit\n\n# 1. Introduction\n\nA **Decentralized Autonomous Organization (DAO)** is a self-governing entity that operates through smart contracts, enabling transparent decision-making without centralized control.\n\n`daokit` is a gnolang package for creating complex DAO models. It introduces a new framework based on conditions, composed of :\n- `daokit` : Core package for building DAOs, proposals, and actions\n- `basedao` : Extension with membership and role management\n- `daocond`: Stateless condition engine for evaluating proposals\n\n# 2. What is `daokit` ?\n\n`daokit` provides a powerful condition and role-based system to build flexible and programmable DAOs.\n\n## Key Features:\n- Create proposals that include complex execution logic\n- Attach rules (conditions) to each resource\n- Assign roles to users to structure permissions and governance\n\n## 2.1 Key Concepts\n\n- **Proposal**: A request to execute a **resource**. Proposals are voted on and executed only if predefined **conditions** are met.\n- **Resource**: An executable action within the DAO. Each resource is governed by a **condition**.\n- **Condition**: A set of rules that determine whether a proposal can be executed.\n- **Role**: Labels that assign governance power or permissions to DAO members.\n\n**Example Use Case**: A DAO wants to create a proposal to spend money from its treasury.\n\n**Rules**:\n- `SpendMoney` is a resource with a condition requiring:\n\t- 50% approval from the administration board\n\t- Approval from the CFO\n\n**Outcome**:\n- Any user can propose to spend money\n- Only board and CFO votes are considered\n- The proposal executes only if the condition is satisfied\n\n# 3. Architecture\n\nDAOkit framework is composed of three packages:\n\n## 3.1 [daocond](/p/samcrew/daocond)\n\n`daocond` provides a stateless condition engine used to evaluate if a proposal should be executed.\n\n### 3.1.1 Interface\n```go\ntype Condition interface {\n\t// Eval checks if the condition is satisfied based on current votes.\n\tEval(ballot Ballot) bool\n\t// Signal returns a value from 0.0 to 1.0 to indicate how close the condition is to being met.\n\tSignal(ballot Ballot) float64\n\n\t// Render returns a static human-readable representation of the condition.\n\tRender() string\n\t// RenderWithVotes returns a dynamic representation with vote context included.\n\tRenderWithVotes(ballot Ballot) string\n}\n\ntype Ballot interface {\n\t// Vote allows a user to vote on a proposal.\n\tVote(voter string, vote Vote)\n\t// Get returns the vote of a user.\n\tGet(voter string) Vote\n\t// Total returns the total number of votes.\n\tTotal() int\n\t// Iterate iterates over all votes, similar as avl.Tree.Iterate.\n\tIterate(fn func(voter string, vote Vote) bool)\n}\n```\n\n### 3.1.2 Built-in Conditions\n`daocond` provides several built-in conditions to cover common governance scenarios.\n\n```go\n// MembersThreshold requires that a specified fraction of all DAO members approve the proposal.\nfunc MembersThreshold(threshold float64, isMemberFn func(memberId string) bool, membersCountFn func() uint64) Condition\n\n// RoleThreshold requires that a certain percentage of members holding a specific role approve.\nfunc RoleThreshold(threshold float64, role string, hasRoleFn func(memberId string, role string) bool, usersRoleCountFn func(role string) uint32) Condition\n\n// RoleCount requires a fixed minimum number of members holding a specific role to approve.\nfunc RoleCount(count uint64, role string, hasRoleFn func(memberId string, role string) bool) Condition\n```\n\n### 3.1.3 Logical Composition\nYou can combine multiple conditions to create complex governance rules using logical operators:\n\n```go\n// And returns a condition that is satisfied only if *all* provided conditions are met.\nfunc And(conditions ...Condition) Condition\n// Or returns a condition that is satisfied if *any* one of the provided conditions is met.\nfunc Or(conditions ...Condition) Condition\n```\n\n**Example**:\n```go\n// Require both admin approval and at least one CFO\ncond := daocond.And(\n    daocond.RoleThreshold(0.5, \"admin\", hasRole, roleCount),\n    daocond.RoleCount(1, \"CFO\", hasRole),\n)\n```\n\nConditions are stateless for flexibility and scalability.\n\n## 3.2 daokit\n\n`daokit` provides the core mechanics:\n\n### 3.2.1 Core Structure:\nIt's the central component of a DAO, responsible for managing both available resources that can be executed and the proposals.\n```go\ntype Core struct {\n\tResources *ResourcesStore\n\tProposals *ProposalsStore\n}\n```\n\n### 3.2.2 DAO Interface:\nThe interface defines the external functions that users or other modules interact with. It abstracts the core governance flow: proposing, voting, and executing.\n```go\ntype DAO interface {\n\tPropose(req ProposalRequest) uint64\n\tVote(id uint64, vote daocond.Vote)\n\tExecute(id uint64)\n}\n```\n\u003e 📖 [Code Example of a Basic DAO](#4-code-example-of-a-basic-dao)\n\n### 3.2.3 Proposal Lifecycle\n\nEach proposal goes through the following states:\n\n1. **Open**: \n- Initial state after proposal creation.\n- Accepts votes from eligible participants.\n\n2. **Passed**\n- Proposal has gathered enough valid votes to meet the condition.\n- Voting is **closed** and cannot be modified.\n- The proposal is now eligible for **execution**.\n\n3. **Executed**\n- Proposal action has been successfully carried out.\n- Final state — proposal can no longer be voted on or modified.\n\n\n## 3.3 [basedao](/p/samcrew/basedao)\n\n`basedao` extends `daokit` to handle members and roles management.\nIt handles who can participate in a DAO and what permissions they have.\n\n### 3.3.1 Core Types\n```go\ntype MembersStore struct {\n\tRoles   *avl.Tree \n\tMembers *avl.Tree \n}\n```\n\n### 3.3.2 Initialize the DAO\nCreate a `MembersStore` structure to initialize the DAO with predefined roles and members.\n\n```go\nroles := []basedao.RoleInfo{\n\t{Name: \"admin\", Description: \"Administrators\"},\n\t{Name: \"finance\", Description: \"Handles treasury\"},\n}\n\nmembers := []basedao.Member{\n\t{Address: \"g1abc...\", Roles: []string{\"admin\"}},\n\t{Address: \"g1xyz...\", Roles: []string{\"finance\"}},\n}\n\nstore := basedao.NewMembersStore(roles, members)\n```\n\n### 3.3.3 Example Usage\n```go\nstore := basedao.NewMembersStore(nil, nil)\n\n// Add a role and assign it\nstore.AddRole(basedao.RoleInfo{Name: \"moderator\", Description: \"Can moderate posts\"})\nstore.AddMember(\"g1alice...\", []string{\"moderator\"})\n\n// Update role assignment\nstore.AddRoleToMember(\"g1alice...\", \"editor\")\nstore.RemoveRoleFromMember(\"g1alice...\", \"moderator\")\n\n// Inspect the state\nisMember := store.IsMember(\"g1alice...\") // \"Is Alice a member?\"\nhasRole := store.HasRole(\"g1alice...\", \"editor\") // \"Is Alice an editor?\"\nmembers := store.GetMembersJSON() // \"All Members (JSON):\"\n```\n\n### 3.3.4 Creating a DAO:\n\n```go\nfunc New(conf *Config) *DAOWrapper\n```\n\n#### 3.3.4.1 Key Structures:\n- `*basedao.DAOWrapper`: The public, rlm-validating surface. Mutating\n  methods take `(_ int, rlm realm, ...)`, validate `rlm.IsCurrent()`,\n  and derive the principal from `rlm.Previous().Address()` before\n  invoking the private impl.\n- `*basedao.DAOPrivate`: Internal impl with address-typed writers.\n  Accessible from the owning realm via `wrapper.Private()` for custom\n  handler registration. **Never expose this externally** — its writer\n  methods trust the supplied caller address (Class-2 designation-forgery\n  if a foreign realm holds it).\n- `daokit.DAO`: Internal dispatch interface (address-typed). **Never\n  store as a public var or return from a public constructor** — exposes\n  designation-forgery surface. Hold `*DAOWrapper` instead.\n\nSealing via an unexported marker method would NOT defend against\nforeign impls — embedding bypasses the seal. See\n`p/test/seal/filetests/z_seal_*_filetest.gno` for the four bypass\nfiletests.\n\n\n### 3.3.5 Configuration:\n```go\ntype Config struct {\n\tName              string\n\tDescription       string\n\tImageURI          string\n\t// Use `basedao.NewMembersStore(...)` to create members and roles.\n\tMembers           *MembersStore\n\t// Set to `true` to disable built-in actions like add/remove member.\n\tNoDefaultHandlers bool\n\t// Default rule applied to all built-in DAO actions.\n\tInitialCondition  daocond.Condition\n\t// Optional helpers to store profile data (e.g., from `/r/demo/profile`).\n\tSetProfileString  ProfileStringSetter\n\tGetProfileString  ProfileStringGetter\n\t// Set to `true` if you don’t want a \"DAO Created\" event to be emitted.\n\tNoCreationEvent   bool\n}\n```\n\n# 4. Code Example of a Basic DAO\n\n```go\npackage daokit_demo\n\nimport (\n\t\"gno.land/p/samcrew/basedao\"\n\t\"gno.land/p/samcrew/daocond\"\n\t\"gno.land/p/samcrew/daokit\"\n\t\"gno.land/r/demo/profile\"\n)\n\n// Hold the concrete *DAOWrapper, NOT the daokit.DAO interface.\n// The wrapper provides rlm-validating methods; the interface is an\n// internal contract that would expose designation-forgery if returned\n// publicly.\nvar DAO *basedao.DAOWrapper\n\nfunc init() {\n\tinitialRoles := []basedao.RoleInfo{\n\t\t{Name: \"admin\", Description: \"Admin is the superuser\"},\n\t\t{Name: \"public-relationships\", Description: \"Responsible of communication with the public\"},\n\t\t{Name: \"finance-officer\", Description: \"Responsible of funds management\"},\n\t}\n\n\tinitialMembers := []basedao.Member{\n\t\t{Address: \"g126...zlg\", Roles: []string{\"admin\", \"public-relationships\"}},\n\t\t{Address: \"g1ld6...3jv\", Roles: []string{\"public-relationships\"}},\n\t\t{Address: \"g1r69...0tth\", Roles: []string{\"finance-officer\"}},\n\t\t{Address: \"g16jv...6e0r\", Roles: []string{}},\n\t}\n\n\tmemberStore := basedao.NewMembersStore(initialRoles, initialMembers)\n\n\tmembersMajority := daocond.MembersThreshold(0.6, memberStore.IsMember, memberStore.MembersCount)\n\tpublicRelationships := daocond.RoleCount(1, \"public-relationships\", memberStore.HasRole)\n\tfinanceOfficer := daocond.RoleCount(1, \"finance-officer\", memberStore.HasRole)\n\n\t// `and` and `or` use va_args so you can pass as many conditions as needed\n\tadminCond := daocond.And(membersMajority, publicRelationships, financeOfficer)\n\n\tDAO = basedao.New(\u0026basedao.Config{\n\t\tName:             \"Demo DAOKIT DAO\",\n\t\tDescription:      \"This is a demo DAO built with DAOKIT\",\n\t\tMembers:          memberStore,\n\t\tInitialCondition: adminCond,\n\t\tGetProfileString: profile.GetStringField,\n\t\tSetProfileString: profile.SetStringField,\n\t}, cur)\n}\n\nfunc Vote(cur realm, proposalID uint64, vote daocond.Vote) {\n\tDAO.Vote(0, cur, proposalID, vote)\n}\n\nfunc Execute(cur realm, proposalID uint64) {\n\tDAO.Execute(0, cur, proposalID)\n}\n\nfunc Render(path string) string {\n\treturn DAO.Render(path)\n}\n```\n\n# 5. Create Custom Resources\n\nTo add new behavior to your DAO — or to enable others to integrate your package into their own DAOs — define custom resources by implementing:\n\n```go\ntype Action interface {\n\tType() string // return the type of the action. e.g.: \"gno.land/p/samcrew/blog.NewPost\"\n\tString() string // return stringify content of the action\n}\n\ntype ActionHandler interface {\n\tType() string // return the type of the action. e.g.: \"gno.land/p/samcrew/blog\"\n\tExecute(action Action) // executes logic associated with the action\n}\n```\nThis allows DAOs to execute arbitrary logic or interact with Gno packages through governance-approved decisions.\n\n## Steps to Add a Custom Resource:\n1. Define the path of the action, it should be unique \n```go\n// XXX: pkg \"/p/samcrew/blog\" - does not exist, it's just an example\nconst ActionNewPostKind = \"gno.land/p/samcrew/blog.NewPost\"\n```\n\n2. Create the structure type of the payload\n```go\ntype ActionNewPost struct {\n\tTitle string\n\tContent string\n}\n```\n\n3. Implement the action and handler\n```go\nfunc NewPostAction(title, content string) daokit.Action {\n\t// def: daoKit.NewAction(kind: String, payload: interface{})\n\treturn daokit.NewAction(ActionNewPostKind, \u0026ActionNewPost{\n\t\tTitle:   title,\n\t\tContent: content,\n\t})\n}\n\nfunc NewPostHandler(blog *Blog) daokit.ActionHandler {\n\t// def: daoKit.NewActionHandler(kind: String, payload: func(interface{}))\n\treturn daokit.NewActionHandler(ActionNewPostKind, func(payload interface{}) {\n\t\taction, ok := payload.(*ActionNewPost)\n\t\tif !ok {\n\t\t\tpanic(errors.New(\"invalid action type\"))\n\t\t}\n\t\tblog.NewPost(action.Title, action.Content)\n\t})\n}\n```\n\n4. Register the resource\n```go\nresource := daokit.Resource{\n    Condition: daocond.NewRoleCount(1, \"CEO\", daoPrivate.Members.HasRole),\n    Handler: blog.NewPostHandler(blog),\n}\ndaoPrivate.Core.Resources.Set(\u0026resource)\n```\n"},{"name":"actions.gno","body":"package daokit\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// SECURITY XXX: Action, ActionHandler, and the daocond.Condition /\n// daocond.Ballot interfaces are open extension points. Whatever you\n// register via Core.Resources.Set runs WITH THE DAO'S AUTHORITY when\n// a proposal executes — same level of trust as code inside the DAO\n// package itself. A hostile handler can do anything the DAO can do\n// (modify members, transfer treasury funds via an action that calls\n// a token, etc.). The owning realm is responsible for only registering\n// handlers/conditions from trusted code.\n//\n// Related re-entrancy risk: Core.Execute calls Condition.Eval before\n// running Action. A malicious Condition.Eval can call back into the\n// same DAO (Propose / Vote / Execute on another proposal) during the\n// same crossing frame. IsCurrent still passes because we're inside\n// the original caller's frame, so the re-entrant call runs as the\n// original caller. Same shape as Ethereum's DAO hack.\n// Fix: add a re-entrancy guard in Core.Execute — a package-level lock\n// or per-proposal in-flight flag — set before any user-supplied code\n// (Eval, Action.String, handler Execute) runs and cleared on return.\ntype Action interface {\n\tString() string\n\tType() string\n}\n\ntype ActionHandler interface {\n\tExecute(action Action)\n\tType() string\n}\n\nfunc NewAction(kind string, payload interface{}) Action {\n\treturn \u0026genericAction{kind: kind, payload: payload}\n}\n\ntype genericAction struct {\n\tkind    string\n\tpayload interface{}\n}\n\n// String implements Action.\nfunc (g *genericAction) String() string {\n\treturn ufmt.Sprintf(\"%v\", g.payload)\n}\n\n// Type implements Action.\nfunc (g *genericAction) Type() string {\n\treturn g.kind\n}\n\nfunc NewActionHandler(kind string, executor func(interface{})) ActionHandler {\n\treturn \u0026genericActionHandler{kind: kind, executor: executor}\n}\n\ntype genericActionHandler struct {\n\tkind     string\n\texecutor func(payload interface{})\n}\n\n// Execute implements ActionHandler.\nfunc (g *genericActionHandler) Execute(iaction Action) {\n\taction, ok := iaction.(*genericAction)\n\tif !ok {\n\t\tpanic(errors.New(\"invalid action type\"))\n\t}\n\tg.executor(action.payload)\n}\n\n// Instantiate implements ActionHandler.\nfunc (g *genericActionHandler) Instantiate() Action {\n\treturn \u0026genericAction{\n\t\tkind: g.kind,\n\t}\n}\n\n// Type implements ActionHandler.\nfunc (g *genericActionHandler) Type() string {\n\treturn g.kind\n}\n\nconst ActionExecuteLambdaKind = \"gno.land/p/samcrew/daokit.ExecuteLambda\"\n\nfunc NewExecuteLambdaHandler() ActionHandler {\n\treturn NewActionHandler(ActionExecuteLambdaKind, func(i interface{}) {\n\t\tcb, ok := i.(func())\n\t\tif !ok {\n\t\t\tpanic(errors.New(\"invalid action type\"))\n\t\t}\n\t\tcb()\n\t})\n}\n\nfunc NewExecuteLambdaAction(cb func()) Action {\n\treturn NewAction(ActionExecuteLambdaKind, cb)\n}\n\n// InstantExecute-as-an-action (NewInstantExecuteHandler /\n// NewInstantExecuteAction) was removed during the rlm-based auth\n// migration: the action-handler callback shape `func(i interface{})`\n// has no rlm in scope, so it cannot drive Propose/Vote/Execute which\n// now all require an IsCurrent-validated rlm. The free function\n// InstantExecute(_ int, rlm realm, d DAO, req) remains for direct\n// caller use.\n"},{"name":"daokit.gno","body":"// Package daokit is a reference DAO toolkit (propose / vote / execute,\n// resource registration, action handlers) used internally by\n// p/samcrew/basedao and the r/samcrew/daodemo example realm.\n//\n// ─── STATUS: EXPERIMENTAL / INSECURE / INCOMPLETE / COMPLICATED ───\n//\n// Do not use as the basis for a production DAO without significant\n// hardening. The gnoland chain's actual governance is r/gov/dao/v3\n// (independent code path; not built on daokit).\n//\n// Known issues (non-exhaustive):\n//\n//   - Re-entrancy: Core.ExecuteWithDispatch evaluates the\n//     caller-supplied daocond.Condition.Eval BEFORE mutating proposal\n//     status. A hostile Condition can re-enter Propose/Vote/Execute\n//     mid-flight on the same DAO. The SECURITY XXX in actions.gno\n//     (lines 17-26) proposes a per-proposal in-flight flag — not\n//     implemented.\n//\n//   - Status state machine is incomplete: there is no Failed or\n//     Closed state. ExecuteWithDispatch moves Status Open→Passed\n//     BEFORE invoking the handler, then sets Executed only on\n//     success. A handler panic leaves the proposal stuck in Passed\n//     with no path to retry, fail, or cancel.\n//\n//   - ResourcesStore.Set silently overwrites entries by Action kind.\n//     The Handler is re-fetched at execute time, so a malicious\n//     owner can swap the handler between propose and execute. The\n//     captured Condition is immune (resolved at propose time) but\n//     Handler is not.\n//\n//   - Core.Vote does not bound the Vote payload size. Anyone\n//     reaching Core directly (e.g. via *DAOPrivate.Core in basedao)\n//     can grief storage. basedao.DAOPrivate.Vote bounds at 16 bytes;\n//     the underlying Core does not.\n//\n//   - daokit.ActionHandler and daocond.Condition are open interfaces.\n//     Hostile implementations registered via Resources.Set run with\n//     the DAO's authority — same trust level as code inside the DAO\n//     package itself. This is documented in actions.gno but not\n//     enforced.\n//\n//   - Stale comment at actions.gno:102-108 references a removed\n//     InstantExecute free function that no longer exists.\n//\n// Architectural complexity notes:\n//\n//   - The Action / ActionHandler / Resources triangle requires three\n//     coordinated registrations per action kind (Action type,\n//     Handler closure, Resource entry with Condition).\n//   - basedao layers a DAOWrapper / DAOPrivate split on top.\n//     DAOWrapper.ExecuteWithDispatch routes actions that need\n//     cross-realm authority through an rlm-aware handler before\n//     falling back to the address-typed Core.Execute path. Reading\n//     the execute path requires tracing through three packages.\n//\n// Use at your own risk. If you adopt this package, fork it and\n// harden the items above before exposing it to untrusted input.\npackage daokit\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/samcrew/daocond\"\n)\n\n// DAO is the internal dispatch interface for a DAO. All methods are\n// address-typed (no rlm crosses the interface boundary) so a malicious\n// implementer cannot capture a cur from a consumer's wrapper. This is the\n// Authority-pattern: the trusted rlm-validating wrapper (e.g.\n// *basedao.DAOWrapper) is the only public surface; this interface is\n// dispatch-only.\n//\n// CONTRACT: NEVER store this interface as a public package var, NEVER\n// accept it as a public function parameter, and NEVER return it from a\n// public constructor. Public APIs should expose a concrete wrapper type\n// (such as *basedao.DAOWrapper) whose rlm-typed methods derive the\n// caller from cur.Previous().Address() under an IsCurrent() guard.\n//\n// Sealing via an unexported marker method would NOT help — embedding\n// bypasses the seal; see p/test/seal/filetests/z_seal_*_filetest.gno.\ntype DAO interface {\n\tPropose(proposer address, req ProposalRequest) uint64\n\tExecute(executor address, id uint64)\n\tVote(voter address, id uint64, vote daocond.Vote)\n}\n\ntype Core struct {\n\tResources *ResourcesStore\n\tProposals *ProposalsStore\n}\n\nfunc NewCore() *Core {\n\treturn \u0026Core{\n\t\tResources: NewResourcesStore(),\n\t\tProposals: NewProposalsStore(),\n\t}\n}\n\nfunc (d *Core) Vote(voterID string, proposalID uint64, vote daocond.Vote) {\n\tproposal := d.Proposals.GetProposal(proposalID)\n\tif proposal == nil {\n\t\tpanic(\"proposal not found\")\n\t}\n\n\tif proposal.Status != ProposalStatusOpen {\n\t\tpanic(\"proposal is not open\")\n\t}\n\n\tproposal.Ballot.Vote(voterID, daocond.Vote(vote))\n}\n\n// Execute runs the proposal's registered action handler via the generic\n// daokit.ActionHandler.Execute path. Equivalent to\n// ExecuteWithDispatch(proposalID, nil).\nfunc (d *Core) Execute(proposalID uint64) {\n\td.ExecuteWithDispatch(proposalID, nil)\n}\n\n// ExecuteWithDispatch validates the proposal (existence, Open status,\n// Condition.Eval, status → Passed) and then either runs the registered\n// Handler or yields to the caller-supplied dispatch override. If\n// `dispatch(action)` returns true the generic Handler.Execute call is\n// skipped — the caller is asserting that it dispatched the action via\n// its own path. Used by basedao to thread a live `rlm` into the\n// EditProfile dispatcher without putting `rlm` on the public\n// daokit.ActionHandler interface (Class-1a defense — see\n// docs/resources/gno-security.md).\n//\n// The dispatch callback receives only the public `Action` value; no\n// `rlm` flows through this function's signature. rlm-bearing dispatch\n// is the caller's responsibility, captured in their closure.\n//\n// CAVEAT (documented contract-violation symptom): if the registered\n// generic Handler.Execute panics — e.g. basedao's editProfileStubHandler\n// when EditProfile is reached via the legacy DAOPrivate.Execute →\n// Core.Execute path — the proposal's status has already moved\n// Open → Passed (by UpdateStatus above), but Status = Executed is never\n// set. The proposal becomes stuck in Passed. This is only reachable\n// when a caller violates the \"DAOPrivate is internal-only\" contract\n// (basedao.gno DAOWrapper.Private() doc); production callers go through\n// DAOWrapper.Execute → DAOPrivate.executeWithRlm → here with a non-nil\n// dispatch that intercepts EditProfile before the stub is reached.\nfunc (d *Core) ExecuteWithDispatch(proposalID uint64, dispatch func(Action) bool) {\n\tproposal := d.Proposals.GetProposal(proposalID)\n\tif proposal == nil {\n\t\tpanic(\"proposal not found\")\n\t}\n\n\tif proposal.Status != ProposalStatusOpen {\n\t\tpanic(\"proposal is not open\")\n\t}\n\n\tif !proposal.Condition.Eval(proposal.Ballot) {\n\t\tpanic(\"proposal condition is not met\")\n\t}\n\n\tproposal.UpdateStatus()\n\tif proposal.Status != ProposalStatusPassed {\n\t\tpanic(\"proposal does not meet the condition(s) or is already closed/executed\")\n\t}\n\n\tif dispatch == nil || !dispatch(proposal.Action) {\n\t\td.Resources.Get(proposal.Action.Type()).Handler.Execute(proposal.Action)\n\t}\n\tproposal.Status = ProposalStatusExecuted\n\tproposal.ExecutedAt = time.Now()\n}\n\nfunc (d *Core) Propose(proposerID string, req ProposalRequest) uint64 {\n\tactionType := req.Action.Type()\n\n\tresource := d.Resources.Get(actionType)\n\tif resource == nil {\n\t\tpanic(\"action type is not registered as a resource\")\n\t}\n\n\tprop := d.Proposals.newProposal(proposerID, req, resource.Condition)\n\treturn uint64(prop.ID)\n}\n\nfunc (d *Core) ResourcesCount() int {\n\treturn d.Resources.Tree.Size()\n}\n"},{"name":"daokit_test.gno","body":"package daokit\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nvar (\n\talice = testutils.TestAddress(\"alice\")\n\tbob   = testutils.TestAddress(\"bob\")\n\tcarol = testutils.TestAddress(\"carol\")\n\tdave  = testutils.TestAddress(\"dave\")\n)\n\nfunc TestNewDAO(t *testing.T) {\n\ttesting.SetOriginCaller(alice)\n\tdao := NewCore()\n\n\tresourcesLen := dao.Resources.Tree.Size()\n\tif resourcesLen != 0 { // There is 0 default resources\n\t\tt.Errorf(\"Expected 0 resources, got %d\", resourcesLen)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/daokit\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"},{"name":"proposals.gno","body":"package daokit\n\nimport (\n\t\"chain/runtime\"\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/onbloc/json\"\n\t\"gno.land/p/samcrew/daocond\"\n)\n\ntype ProposalStatus int\n\nconst (\n\tProposalStatusOpen ProposalStatus = iota\n\tProposalStatusPassed\n\tProposalStatusExecuted\n)\n\nfunc (s ProposalStatus) String() string {\n\tswitch s {\n\tcase ProposalStatusOpen:\n\t\treturn \"Open\"\n\tcase ProposalStatusPassed:\n\t\treturn \"Passed\"\n\tcase ProposalStatusExecuted:\n\t\treturn \"Executed\"\n\tdefault:\n\t\treturn \"Unknown\"\n\t}\n}\n\ntype Proposal struct {\n\tID            seqid.ID\n\tTitle         string\n\tDescription   string\n\tCreatedAt     time.Time\n\tCreatedHeight int64\n\tProposerID    string\n\tCondition     daocond.Condition\n\tAction        Action\n\tStatus        ProposalStatus\n\tExecutorID    string\n\tExecutedAt    time.Time\n\tBallot        daocond.Ballot\n}\n\ntype ProposalsStore struct {\n\tTree  *avl.Tree // int -\u003e Proposal\n\tgenID seqid.ID\n}\n\ntype ProposalRequest struct {\n\tTitle       string\n\tDescription string\n\tAction      Action\n}\n\nfunc NewProposalsStore() *ProposalsStore {\n\treturn \u0026ProposalsStore{\n\t\tTree: avl.NewTree(),\n\t}\n}\n\nfunc (p *ProposalsStore) newProposal(proposer string, req ProposalRequest, condition daocond.Condition) *Proposal {\n\tid := p.genID.Next()\n\tproposal := \u0026Proposal{\n\t\tID:            id,\n\t\tTitle:         req.Title,\n\t\tDescription:   req.Description,\n\t\tProposerID:    proposer,\n\t\tStatus:        ProposalStatusOpen,\n\t\tAction:        req.Action,\n\t\tCondition:     condition,\n\t\tBallot:        daocond.NewBallot(),\n\t\tCreatedAt:     time.Now(),\n\t\tCreatedHeight: runtime.ChainHeight(),\n\t}\n\tp.Tree.Set(id.String(), proposal)\n\treturn proposal\n}\n\nfunc (p *ProposalsStore) GetProposal(id uint64) *Proposal {\n\tvalue := p.Tree.Get(seqid.ID(id).String())\n\tif value == nil {\n\t\treturn nil\n\t}\n\tproposal := value.(*Proposal)\n\treturn proposal\n}\n\nfunc (p *Proposal) UpdateStatus() {\n\tconditionsAreMet := p.Condition.Eval(p.Ballot)\n\tif p.Status == ProposalStatusOpen \u0026\u0026 conditionsAreMet {\n\t\tp.Status = ProposalStatusPassed\n\t}\n}\n\nfunc (p *ProposalsStore) GetProposalsJSON() string {\n\tprops := make([]*json.Node, 0, p.Tree.Size())\n\t// XXX: pagination\n\tp.Tree.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tprop, ok := value.(*Proposal)\n\t\tif !ok {\n\t\t\tpanic(errors.New(\"unexpected invalid proposal type\"))\n\t\t}\n\t\tprop.UpdateStatus()\n\t\tprops = append(props, json.ObjectNode(\"\", map[string]*json.Node{\n\t\t\t\"id\":          json.NumberNode(\"\", float64(prop.ID)),\n\t\t\t\"title\":       json.StringNode(\"\", prop.Title),\n\t\t\t\"description\": json.StringNode(\"\", prop.Description),\n\t\t\t\"proposer\":    json.StringNode(\"\", prop.ProposerID),\n\t\t\t\"status\":      json.StringNode(\"\", prop.Status.String()),\n\t\t\t\"startHeight\": json.NumberNode(\"\", float64(prop.CreatedHeight)),\n\t\t\t\"signal\":      json.NumberNode(\"\", prop.Condition.Signal(prop.Ballot)),\n\t\t}))\n\t\treturn false\n\t})\n\tbz, err := json.Marshal(json.ArrayNode(\"\", props))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn string(bz)\n}\n"},{"name":"resources.gno","body":"package daokit\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/samcrew/daocond\"\n)\n\ntype ResourcesStore struct {\n\tTree *avl.Tree // string -\u003e daocond.Condition\n}\n\n// XXX: should condition be a pointer?\ntype Resource struct {\n\tHandler     ActionHandler\n\tCondition   daocond.Condition\n\tDisplayName string\n\tDescription string\n}\n\nfunc NewResourcesStore() *ResourcesStore {\n\treturn \u0026ResourcesStore{\n\t\tTree: avl.NewTree(),\n\t}\n}\n\nfunc (r *ResourcesStore) Set(resource *Resource) {\n\tr.Tree.Set(resource.Handler.Type(), resource)\n}\n\nfunc (r *ResourcesStore) Get(name string) *Resource {\n\tvalue := r.Tree.Get(name)\n\tif value == nil {\n\t\treturn nil\n\t}\n\tres := value.(*Resource)\n\treturn res\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"tHjoHbHc/+IyWgzpHVBYhQOQZM0S3Eq9SdjFnOmyo6bgEBcx3Zg91oCPuRhUhkMSnEdGZSVzZHUTFZF1N4qqBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"basedao","path":"gno.land/p/samcrew/basedao","files":[{"name":"actions.gno","body":"package basedao\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/samcrew/daokit\"\n)\n\n// ADD MEMBER\n\nconst ActionAddMemberKind = \"gno.land/p/samcrew/basedao.AddMember\"\n\ntype ActionAddMember struct {\n\tAddress address\n\tRoles   []string\n}\n\nfunc (a *ActionAddMember) String() string {\n\treturn ufmt.Sprintf(\"Add member %s with roles %v\", a.Address, a.Roles)\n}\n\nfunc NewAddMemberHandler(dao *DAOPrivate) daokit.ActionHandler {\n\treturn daokit.NewActionHandler(ActionAddMemberKind, func(ipayload interface{}) {\n\t\tpayload, ok := ipayload.(*ActionAddMember)\n\t\tif !ok {\n\t\t\tpanic(errors.New(\"invalid payload type\"))\n\t\t}\n\t\tdao.Members.AddMember(payload.Address.String(), payload.Roles)\n\t})\n}\n\nfunc NewAddMemberAction(payload *ActionAddMember) daokit.Action {\n\treturn daokit.NewAction(ActionAddMemberKind, payload)\n}\n\n// REMOVE MEMBER\n\nconst ActionRemoveMemberKind = \"gno.land/p/samcrew/basedao.RemoveMember\"\n\nfunc NewRemoveMemberHandler(dao *DAOPrivate) daokit.ActionHandler {\n\treturn daokit.NewActionHandler(ActionRemoveMemberKind, func(ipayload interface{}) {\n\t\taddr, ok := ipayload.(address)\n\t\tif !ok {\n\t\t\tpanic(errors.New(\"invalid payload type\"))\n\t\t}\n\t\tdao.Members.RemoveMember(addr.String())\n\t})\n}\n\nfunc NewRemoveMemberAction(addr address) daokit.Action {\n\treturn daokit.NewAction(ActionRemoveMemberKind, addr)\n}\n\n// ASSIGN ROLE\n\nconst ActionAssignRoleKind = \"gno.land/p/samcrew/basedao.AssignRole\"\n\ntype ActionAssignRole struct {\n\tAddress address\n\tRole    string\n}\n\nfunc (a *ActionAssignRole) String() string {\n\treturn ufmt.Sprintf(\"Assign role %q to user %s\", a.Role, a.Address)\n}\n\nfunc NewAssignRoleHandler(dao *DAOPrivate) daokit.ActionHandler {\n\treturn daokit.NewActionHandler(ActionAssignRoleKind, func(i interface{}) {\n\t\tpayload, ok := i.(*ActionAssignRole)\n\t\tif !ok {\n\t\t\tpanic(errors.New(\"invalid payload type\"))\n\t\t}\n\t\tdao.Members.AddRoleToMember(payload.Address.String(), payload.Role)\n\t})\n}\n\nfunc NewAssignRoleAction(payload *ActionAssignRole) daokit.Action {\n\treturn daokit.NewAction(ActionAssignRoleKind, payload)\n}\n\n// UNASSIGN ROLE\n\nconst ActionUnassignRoleKind = \"gno.land/p/samcrew/basedao.UnassignRole\"\n\ntype ActionUnassignRole struct {\n\tAddress address\n\tRole    string\n}\n\nfunc (a *ActionUnassignRole) String() string {\n\treturn ufmt.Sprintf(\"Remove role %q from user %s\", a.Role, a.Address)\n}\n\nfunc NewUnassignRoleHandler(dao *DAOPrivate) daokit.ActionHandler {\n\treturn daokit.NewActionHandler(ActionUnassignRoleKind, func(i interface{}) {\n\t\tpayload, ok := i.(*ActionUnassignRole)\n\t\tif !ok {\n\t\t\tpanic(errors.New(\"invalid payload type\"))\n\t\t}\n\t\tdao.Members.RemoveRoleFromMember(payload.Address.String(), payload.Role)\n\t})\n}\n\nfunc NewUnassignRoleAction(payload *ActionUnassignRole) daokit.Action {\n\treturn daokit.NewAction(ActionUnassignRoleKind, payload)\n}\n\n// EDIT PROFILE\n//\n// EditProfile is the only basedao action that crosses into a foreign\n// realm (the consumer-supplied profile store). It cannot go through the\n// generic daokit.ActionHandler dispatch because that interface is\n// cur-free by design — putting rlm on it would be Class-1a (see\n// docs/resources/gno-security.md). Instead:\n//\n//   - editProfileHandler is a concrete basedao-internal type with an\n//     UNEXPORTED execute method. No foreign type can satisfy an\n//     interface that includes an unexported method, so no hostile\n//     implementation can be substituted in.\n//   - editProfileStubHandler is registered in daokit.Core.Resources so\n//     that proposal-creation can find the action's Condition. Its\n//     Execute method panics — if reached, a caller bypassed\n//     DAOWrapper.Execute (the rlm-aware path).\n//   - DAOWrapper.Execute → DAOPrivate.executeWithRlm uses\n//     Core.ExecuteWithDispatch to intercept ActionEditProfileKind and\n//     dispatch through editProfileHandler.execute(0, rlm, action),\n//     enabling cross(rlm) instead of a bare cross.\n\nconst ActionEditProfileKind = \"gno.land/p/samcrew/basedao.EditProfile\"\n\n// ActionEditProfile implements daokit.Action directly (rather than being\n// wrapped via daokit.NewAction's *genericAction). The rlm-aware\n// dispatcher in basedao type-asserts action.(*ActionEditProfile) to\n// reach the kv payload; that only works if the proposal carries\n// *ActionEditProfile as the Action value, not a generic wrapper around\n// it. (genericAction's payload field is unexported in daokit, so basedao\n// cannot unwrap.)\ntype ActionEditProfile struct {\n\tkv [][2]string\n}\n\nfunc (a *ActionEditProfile) String() string {\n\telems := []string{}\n\tfor _, v := range a.kv {\n\t\telems = append(elems, v[0]+\": \"+v[1])\n\t}\n\treturn md.BulletList(elems)\n}\n\nfunc (a *ActionEditProfile) Type() string { return ActionEditProfileKind }\n\nfunc NewEditProfileAction(kv ...[2]string) daokit.Action {\n\treturn \u0026ActionEditProfile{kv: kv}\n}\n\n// editProfileHandler is the concrete, rlm-aware EditProfile dispatcher.\n// Stored on *DAOPrivate.editProfile (not in Core.Resources), invoked\n// from DAOPrivate.executeWithRlm under DAOWrapper.Execute.\ntype editProfileHandler struct {\n\tsetter        ProfileStringSetter\n\tallowedFields []string\n}\n\n// execute is unexported intentionally — see the file-level EditProfile\n// comment. The unexported method name binds the interface signature to\n// the basedao package, preventing any foreign type from being\n// substituted into basedao's rlm-bearing dispatch path.\nfunc (h *editProfileHandler) execute(_ int, rlm realm, action daokit.Action) {\n\ta, ok := action.(*ActionEditProfile)\n\tif !ok {\n\t\tpanic(errors.New(\"invalid action type\"))\n\t}\n\tfor _, elem := range a.kv {\n\t\tk, v := elem[0], elem[1]\n\t\tif len(h.allowedFields) \u003e 0 \u0026\u0026 !stringSliceContains(h.allowedFields, k) {\n\t\t\tpanic(ufmt.Errorf(\"unauthorized field %q\", k))\n\t\t}\n\t\th.setter(cross(rlm), k, v)\n\t}\n}\n\n// editProfileStubHandler is a daokit.ActionHandler registered in\n// Core.Resources for ActionEditProfileKind so that Core.Propose's\n// Condition lookup succeeds. Its Execute panics — EditProfile MUST be\n// dispatched via DAOWrapper.Execute (the rlm-aware path); reaching this\n// method means the caller bypassed that contract.\ntype editProfileStubHandler struct{}\n\nfunc (editProfileStubHandler) Type() string { return ActionEditProfileKind }\n\nfunc (editProfileStubHandler) Execute(action daokit.Action) {\n\tpanic(errors.New(\"ActionEditProfileKind must be dispatched via basedao.DAOWrapper.Execute (rlm-aware path)\"))\n}\n\nfunc stringSliceContains(s []string, target string) bool {\n\tfor _, elem := range s {\n\t\tif elem == target {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"},{"name":"basedao.gno","body":"// Package basedao is a reference DAO implementation built on\n// p/samcrew/daokit. Used by the r/samcrew/daodemo example realm.\n//\n// ─── STATUS: EXPERIMENTAL / INSECURE / INCOMPLETE / COMPLICATED ───\n//\n// Do not use as the basis for a production DAO without significant\n// hardening. The gnoland chain's actual governance is r/gov/dao/v3\n// (independent code path; not built on basedao).\n//\n// Known issues (non-exhaustive):\n//\n//   - DAOWrapper.Private() returns the raw *DAOPrivate. Its Core,\n//     Members, and Resources fields are all exported and mutable.\n//     A caller that holds *DAOPrivate can mutate members, swap\n//     handlers, or invoke Core.Execute directly with any address,\n//     bypassing IsCurrent / member-only / proposal-vote gates. The\n//     \"DAOPrivate is internal-only\" contract is documented but\n//     unenforced — anyone exposing *DAOPrivate outside their own\n//     realm has handed the DAO away.\n//\n//   - MembersStore.RemoveMember has a role-bookkeeping bug: the line\n//     `role.Members.Remove(role.Name)` should be `Remove(member)`.\n//     Removed members continue to count toward Role-keyed quorum\n//     math (RoleThreshold / RoleCount conditions) until the role\n//     itself is removed.\n//\n//   - No \"at-least-one-member\" invariant. RemoveMember accepts going\n//     to zero members; the resulting DAO is permanently bricked\n//     (IsMember returns false for all callers, all writes panic).\n//     Similarly, no \"at-least-one-admin\" guard if you use a\n//     RoleThreshold condition.\n//\n//   - Address/role-name input validation is missing in handlers.\n//     ActionAddMember.Address is not checked for .IsValid(); empty\n//     or zero addresses produce phantom voters that can be added\n//     and granted roles. Role names accept arbitrary strings\n//     including empty / unicode / slashes (the last breaks\n//     render-URL construction).\n//\n//   - EditProfile dispatch uses an rlm-aware path via\n//     ExecuteWithDispatch + editProfileStubHandler. If a caller\n//     reaches the legacy DAOPrivate.Execute → Core.Execute path\n//     for an EditProfile proposal, the stub panics AFTER\n//     UpdateStatus mutates Open→Passed, leaving the proposal\n//     permanently stuck. Production daodemo doesn't trigger this;\n//     tests don't either.\n//\n//   - See p/samcrew/daokit's package doc for additional issues\n//     inherited from the underlying toolkit (re-entrancy via\n//     Condition.Eval, stuck-Passed handler-panic state,\n//     resource-overwrite mid-flight, etc.)\n//\n// Architectural complexity notes:\n//\n//   - The action dispatch path threads through three packages\n//     (basedao → daokit Core → registered Handler), with an\n//     additional carve-out (DAOPrivate.executeWithRlm +\n//     editProfileHandler) for cross-realm dispatch that needs a\n//     live cur. Auditing any single action requires understanding\n//     all three layers.\n//   - DAOWrapper (rlm-typed public surface) and DAOPrivate\n//     (address-typed internal) is the Authority-pattern split;\n//     the split is correct in intent but Private() escapes it.\n//\n// Use at your own risk. If you adopt this package, fork it and\n// harden the items above before exposing it to untrusted input.\npackage basedao\n\nimport (\n\t\"chain\"\n\t\"errors\"\n\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/samcrew/daocond\"\n\t\"gno.land/p/samcrew/daokit\"\n)\n\n// DAOWrapper is the public, rlm-validating surface of a basedao DAO. All\n// mutating methods take (_ int, rlm realm, ...), validate via\n// rlm.IsCurrent(), and derive the principal from rlm.Previous().Address()\n// before invoking the private impl's address-typed methods. This is the\n// trusted boundary — the daokit.DAO interface itself is dispatch-only and\n// never exposed publicly.\n//\n// Consumers in /r/ should hold `*DAOWrapper`, never `daokit.DAO`.\ntype DAOWrapper struct {\n\timpl *DAOPrivate\n}\n\nfunc (w *DAOWrapper) Propose(_ int, rlm realm, req daokit.ProposalRequest) uint64 {\n\tif !rlm.IsCurrent() {\n\t\tpanic(errors.New(\"caller is not a member\"))\n\t}\n\tproposer := rlm.Previous().Address()\n\treturn w.impl.Propose(proposer, req)\n}\n\nfunc (w *DAOWrapper) Execute(_ int, rlm realm, id uint64) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(errors.New(\"caller is not a member\"))\n\t}\n\texecutor := rlm.Previous().Address()\n\t// Route through the rlm-aware path: ActionEditProfileKind is dispatched\n\t// via editProfileHandler.execute(0, rlm, ...) which uses cross(rlm).\n\t// All other action kinds fall through to generic Core dispatch.\n\tw.impl.executeWithRlm(0, rlm, executor, id)\n}\n\nfunc (w *DAOWrapper) Vote(_ int, rlm realm, id uint64, vote daocond.Vote) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(errors.New(\"caller is not a member\"))\n\t}\n\tvoter := rlm.Previous().Address()\n\tw.impl.Vote(voter, id, vote)\n}\n\n// InstantExecute proposes, votes Yes, and executes in one call. Each step\n// goes through this wrapper's rlm-validated methods, so IsCurrent is\n// re-checked at every step (defense-in-depth against drift if the impl is\n// later changed to skip per-call validation).\nfunc (w *DAOWrapper) InstantExecute(_ int, rlm realm, req daokit.ProposalRequest) uint64 {\n\tid := w.Propose(0, rlm, req)\n\tw.Vote(0, rlm, id, daocond.VoteYes)\n\tw.Execute(0, rlm, id)\n\treturn id\n}\n\n// Render delegates to the private impl's renderer. The impl's render\n// surface is read-only (no rlm/cur in any render method).\nfunc (w *DAOWrapper) Render(path string) string {\n\treturn w.impl.Render(path)\n}\n\n// Private returns the internal *DAOPrivate. This is intended ONLY for\n// the realm that owns the DAOWrapper to wire up custom handlers or\n// extend rendering — never expose the result to other realms. Foreign\n// code that holds a *DAOPrivate can call its address-typed writers\n// directly with any address argument, which would bypass IsCurrent.\nfunc (w *DAOWrapper) Private() *DAOPrivate {\n\treturn w.impl\n}\n\n// DAOPrivate is meant for internal realm usage and should not be exposed.\n//\n// SECURITY: writer methods (Propose, Vote, Execute) are address-typed and\n// trust the supplied caller. They DO NOT validate IsCurrent — that\n// validation lives in DAOWrapper. Holding *DAOPrivate and calling its\n// methods with a forged caller address is a Class-2 designation-forgery.\n// Owning realms must keep their *DAOPrivate in an unexported package var\n// and never return it from any exported function. Use DAOWrapper.Private()\n// only at construction time to register custom action handlers.\ntype DAOPrivate struct {\n\tCore             *daokit.Core\n\tMembers          *MembersStore\n\tRenderRouter     *mux.Router\n\tGetProfileString ProfileStringGetter\n\t// RealmPkgPath / RealmAddress are captured at construction time as\n\t// plain string/address values rather than a runtime.Realm struct.\n\t// Storing a runtime.Realm in a struct field would be a parallel\n\t// anti-pattern to storing a `cur realm` (capability token in a\n\t// struct). PkgPath/Address are stable identity strings, safe to\n\t// persist.\n\tRealmPkgPath string\n\tRealmAddress address\n\t// editProfile carries the rlm-aware dispatcher for\n\t// ActionEditProfileKind. nil if Config.SetProfileString was nil.\n\t// See actions.gno for the rationale (concrete type + unexported\n\t// method closes Class-1a; basedao routes around the generic\n\t// daokit.ActionHandler interface for this one action).\n\teditProfile *editProfileHandler\n}\n\ntype Config struct {\n\tName               string\n\tDescription        string\n\tImageURI           string\n\tMembers            *MembersStore\n\tNoDefaultHandlers  bool\n\tNoDefaultRendering bool\n\tInitialCondition   daocond.Condition\n\tSetProfileString   ProfileStringSetter\n\tGetProfileString   ProfileStringGetter\n\tNoCreationEvent    bool\n}\n\ntype ProfileStringSetter func(cur realm, field string, value string) bool\ntype ProfileStringGetter func(addr address, field string, def string) string\n\nconst EventBaseDAOCreated = \"BaseDAOCreated\"\n\n// New constructs a basedao DAO and returns its public wrapper. The\n// returned *DAOWrapper is the canonical type for /r/ consumers to hold;\n// it exposes rlm-validating Propose / Vote / Execute / InstantExecute /\n// Render methods, and a .Private() accessor for the owning realm to wire\n// custom action handlers at init time.\n//\n// rlm is threaded into the optional `conf.SetProfileString` callback via\n// cross(rlm). /p/ production code cannot declare crossing functions, so\n// rlm is taken as a non-first parameter — callers pass `cur` directly.\nfunc New(conf *Config, rlm realm) *DAOWrapper {\n\t// XXX: emit events from memberstore\n\n\t// IsCurrent gate: rlm must be the caller's live cur. Otherwise\n\t// RealmPkgPath/RealmAddress (captured below into a long-lived\n\t// struct) would be set from a stale or sibling-frame value, and\n\t// the cross(rlm) calls further down would panic anyway when\n\t// SetProfileString is non-nil. Catch the misuse up-front so the\n\t// `SetProfileString == nil` path is also guarded.\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"basedao.New: rlm is not the caller's live cur\")\n\t}\n\n\tmembers := conf.Members\n\tif members == nil {\n\t\tmembers = NewMembersStore(nil, nil)\n\t}\n\n\tif conf.GetProfileString == nil {\n\t\tpanic(errors.New(\"GetProfileString is required\"))\n\t}\n\n\tcore := daokit.NewCore()\n\tdao := \u0026DAOPrivate{\n\t\tCore:             core,\n\t\tMembers:          members,\n\t\tGetProfileString: conf.GetProfileString,\n\t\tRealmPkgPath:     rlm.PkgPath(),\n\t\tRealmAddress:     rlm.Address(),\n\t}\n\tdao.initRenderingRouter()\n\n\tif !conf.NoDefaultRendering {\n\t\tdao.InitDefaultRendering()\n\t}\n\n\tif conf.SetProfileString != nil {\n\t\tconf.SetProfileString(cross(rlm), \"DisplayName\", conf.Name)\n\t\tconf.SetProfileString(cross(rlm), \"Bio\", conf.Description)\n\t\tconf.SetProfileString(cross(rlm), \"Avatar\", conf.ImageURI)\n\t}\n\n\tif !conf.NoDefaultHandlers {\n\t\tif conf.InitialCondition == nil {\n\t\t\tconf.InitialCondition = daocond.MembersThreshold(0.6, members.IsMember, members.MembersCount)\n\t\t}\n\n\t\tif conf.SetProfileString != nil {\n\t\t\t// editProfile is the rlm-aware dispatcher used by\n\t\t\t// DAOPrivate.executeWithRlm. The Resource entry below uses a\n\t\t\t// panicking stub (editProfileStubHandler) so Core.Propose's\n\t\t\t// Condition lookup succeeds; the stub is unreachable on the\n\t\t\t// production execute path (DAOWrapper.Execute routes via\n\t\t\t// executeWithRlm with an override that intercepts EditProfile).\n\t\t\tdao.editProfile = \u0026editProfileHandler{\n\t\t\t\tsetter:        conf.SetProfileString,\n\t\t\t\tallowedFields: []string{\"DisplayName\", \"Bio\", \"Avatar\"},\n\t\t\t}\n\t\t\tdao.Core.Resources.Set(\u0026daokit.Resource{\n\t\t\t\tHandler:   editProfileStubHandler{},\n\t\t\t\tCondition: conf.InitialCondition,\n\t\t\t})\n\t\t}\n\n\t\tdefaultResources := []daokit.Resource{\n\t\t\t{\n\t\t\t\tHandler:     NewAddMemberHandler(dao),\n\t\t\t\tCondition:   conf.InitialCondition,\n\t\t\t\tDisplayName: \"Add Member\",\n\t\t\t\tDescription: \"This proposal allows you to add a new member to the DAO.\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tHandler:     NewRemoveMemberHandler(dao),\n\t\t\t\tCondition:   conf.InitialCondition,\n\t\t\t\tDisplayName: \"Remove Member\",\n\t\t\t\tDescription: \"This proposal allows you to remove a member from the DAO.\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tHandler:     NewAssignRoleHandler(dao),\n\t\t\t\tCondition:   conf.InitialCondition,\n\t\t\t\tDisplayName: \"Assign Role\",\n\t\t\t\tDescription: \"This proposal allows you to assign a role to a member.\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tHandler:     NewUnassignRoleHandler(dao),\n\t\t\t\tCondition:   conf.InitialCondition,\n\t\t\t\tDisplayName: \"Unassign Role\",\n\t\t\t\tDescription: \"This proposal allows you to unassign a role from a member.\",\n\t\t\t},\n\t\t}\n\t\t// register management handlers\n\t\tfor _, resource := range defaultResources {\n\t\t\tdao.Core.Resources.Set(\u0026resource)\n\t\t}\n\n\t}\n\n\tif !conf.NoCreationEvent {\n\t\tchain.Emit(EventBaseDAOCreated)\n\t}\n\n\treturn \u0026DAOWrapper{impl: dao}\n}\n\n// Vote on a proposal. caller must be a current DAO member; the wrapper\n// (DAOWrapper.Vote) derives this from rlm.Previous().Address() under\n// IsCurrent. Direct callers of DAOPrivate.Vote forge the caller at their\n// own risk — see the type-level security note.\nfunc (d *DAOPrivate) Vote(caller address, proposalID uint64, vote daocond.Vote) {\n\tif len(vote) \u003e 16 {\n\t\tpanic(\"invalid vote\")\n\t}\n\tvoterID := d.assertCallerIsMember(caller)\n\td.Core.Vote(voterID, proposalID, vote)\n}\n\n// Execute a proposal. See Vote for caller semantics.\n//\n// Address-typed legacy path (preserves the daokit.DAO interface contract\n// of no rlm crossing the dispatch boundary). EditProfile proposals\n// reaching this method will panic via the registered editProfileStubHandler\n// — they must be dispatched via DAOPrivate.executeWithRlm (which\n// DAOWrapper.Execute calls). See daokit.gno ExecuteWithDispatch CAVEAT.\nfunc (d *DAOPrivate) Execute(caller address, proposalID uint64) {\n\t_ = d.assertCallerIsMember(caller)\n\td.Core.Execute(proposalID)\n}\n\n// executeWithRlm is the rlm-aware execute path. Called by DAOWrapper.Execute\n// after IsCurrent has been validated. Asserts caller membership (same as\n// DAOPrivate.Execute), then dispatches through Core.ExecuteWithDispatch\n// with an override that routes ActionEditProfileKind into the\n// editProfile concrete handler. All other action types fall through to\n// generic Core dispatch unchanged.\nfunc (d *DAOPrivate) executeWithRlm(_ int, rlm realm, caller address, proposalID uint64) {\n\t_ = d.assertCallerIsMember(caller)\n\td.Core.ExecuteWithDispatch(proposalID, func(action daokit.Action) bool {\n\t\tif action.Type() == ActionEditProfileKind \u0026\u0026 d.editProfile != nil {\n\t\t\td.editProfile.execute(0, rlm, action)\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n}\n\n// Propose a proposal. See Vote for caller semantics.\nfunc (d *DAOPrivate) Propose(caller address, req daokit.ProposalRequest) uint64 {\n\tproposerID := d.assertCallerIsMember(caller)\n\treturn d.Core.Propose(proposerID, req)\n}\n\n// assertCallerIsMember returns the caller as a string after verifying\n// it's a current DAO member.\nfunc (d *DAOPrivate) assertCallerIsMember(caller address) string {\n\tid := caller.String()\n\tif !d.Members.IsMember(id) {\n\t\tpanic(errors.New(\"caller is not a member\"))\n\t}\n\treturn id\n}\n"},{"name":"basedao_test.gno","body":"package basedao\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\t\"gno.land/p/samcrew/daocond\"\n\t\"gno.land/p/samcrew/daokit\"\n)\n\nvar (\n\talice = testutils.TestAddress(\"alice\")\n\tbob   = testutils.TestAddress(\"bob\")\n\tcarol = testutils.TestAddress(\"carol\")\n\tdave  = testutils.TestAddress(\"dave\")\n)\n\nfunc TestNewDAO(cur realm, t *testing.T) {\n\tinitialRoles := []RoleInfo{\n\t\t{Name: \"admin\", Description: \"Admin is the superuser\"},\n\t}\n\tinitialMembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{}},\n\t\t{carol.String(), []string{}},\n\t}\n\tconf := \u0026Config{\n\t\tName:             \"My DAO\",\n\t\tDescription:      \"My DAO Description\",\n\t\tMembers:          NewMembersStore(initialRoles, initialMembers),\n\t\tGetProfileString: func(addr address, field, def string) string { return \"\" },\n\t}\n\n\tdaoRealm := testing.NewCodeRealm(\"gno.land/r/testing/daorealm\")\n\ttesting.SetOriginCaller(alice)\n\ttesting.SetRealm(daoRealm)\n\twrapper := New(conf, cur)\n\tdao := wrapper.Private()\n\troles := dao.Members.GetRoles()\n\tif len(roles) != 1 {\n\t\tt.Errorf(\"Expected 1 role, got %d\", len(roles))\n\t}\n\tif roles[0] != \"admin\" {\n\t\tt.Errorf(\"Expected role 'admin', got %s\", roles[0])\n\t}\n\n\tfor _, member := range initialMembers {\n\t\taddr := member.Address\n\t\tif !dao.Members.IsMember(addr) {\n\t\t\tt.Errorf(\"Expected member %s to be a member\", addr)\n\t\t}\n\t\tif len(member.Roles) == 1 \u0026\u0026 !dao.Members.HasRole(addr, member.Roles[0]) {\n\t\t\tt.Errorf(\"Expected member %s to have role %s\", addr, member.Roles[0])\n\t\t}\n\t}\n\n\turequire.Equal(t, 4, dao.Core.ResourcesCount(), \"expected 4 resources\")\n\turequire.Equal(t, dao.RealmPkgPath, cur.PkgPath())\n\n\t// XXX: check realm and profile\n}\n\nfunc TestPropose(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{}},\n\t\t{carol.String(), []string{}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.6, members)\n\n\ttype testNewProposalInput struct {\n\t\tproposalReq daokit.ProposalRequest\n\t\tproposer    address\n\t}\n\n\ttype tesNewProposalExpected struct {\n\t\ttitle        string\n\t\tdescription  string\n\t\tproposer     address\n\t\tmesssageType string\n\t\tpanic        bool\n\t}\n\n\ttype testNewProposal struct {\n\t\tinput    testNewProposalInput\n\t\texpected tesNewProposalExpected\n\t}\n\n\ttype testNewProposalTable map[string]testNewProposal\n\n\ttests := testNewProposalTable{\n\t\t\"Success\": {\n\t\t\tinput: testNewProposalInput{\n\t\t\t\tproposalReq: tdao.mockProposalRequest,\n\t\t\t\tproposer:    alice,\n\t\t\t},\n\t\t\texpected: tesNewProposalExpected{\n\t\t\t\ttitle:        \"My Proposal\",\n\t\t\t\tdescription:  \"My Proposal Description\",\n\t\t\t\tproposer:     alice,\n\t\t\t\tmesssageType: \"valid\",\n\t\t\t\tpanic:        false,\n\t\t\t},\n\t\t},\n\t\t\"Non-member\": {\n\t\t\tinput: testNewProposalInput{\n\t\t\t\tproposalReq: tdao.mockProposalRequest,\n\t\t\t\tproposer:    dave,\n\t\t\t},\n\t\t\texpected: tesNewProposalExpected{\n\t\t\t\tpanic: true,\n\t\t\t},\n\t\t},\n\t\t\"Unknown action type\": {\n\t\t\tinput: testNewProposalInput{\n\t\t\t\tproposalReq: tdao.unknownProposalRequest,\n\t\t\t\tproposer:    alice,\n\t\t\t},\n\t\t\texpected: tesNewProposalExpected{\n\t\t\t\tpanic: true,\n\t\t\t},\n\t\t},\n\t}\n\n\tfor testName, test := range tests {\n\t\tt.Run(testName, func(cur realm, t *testing.T) {\n\t\t\t*tdao.mockHandlerCalled = false\n\t\t\ttesting.SetRealm(testing.NewUserRealm(test.input.proposer))\n\n\t\t\t// tdao.propose crosses into basedao via an IIFE same-realm\n\t\t\t// cross; any panic inside the crossing frame is cross-realm\n\t\t\t// and cannot be caught by defer/recover here. Use revive\n\t\t\t// to capture the cross-boundary abort.\n\t\t\tvar id uint64\n\t\t\texc := revive(func() {\n\t\t\t\tid = tdao.propose(0, cur, test.input.proposalReq)\n\t\t\t})\n\t\t\tif test.expected.panic {\n\t\t\t\tif exc == nil {\n\t\t\t\t\tt.Errorf(\"Expected panic, got none\")\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif exc != nil {\n\t\t\t\tt.Errorf(\"Unexpected panic: %v\", exc)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.False(t, *tdao.mockHandlerCalled, \"execute should not be called\")\n\n\t\t\tproposal := tdao.privdao.Core.Proposals.GetProposal(id)\n\t\t\tif proposal.Title != test.expected.title {\n\t\t\t\tt.Errorf(\"Expected title %s, got %s\", test.expected.title, proposal.Title)\n\t\t\t}\n\t\t\tif proposal.Description != test.expected.description {\n\t\t\t\tt.Errorf(\"Expected description %s, got %s\", test.expected.description, proposal.Description)\n\t\t\t}\n\t\t\tif proposal.ProposerID != test.expected.proposer.String() {\n\t\t\t\tt.Errorf(\"Expected proposer %s, got %s\", test.expected.proposer, proposal.ProposerID)\n\t\t\t}\n\t\t\tif proposal.Action.Type() != test.expected.messsageType {\n\t\t\t\tt.Errorf(\"Expected action type %s, got %s\", test.expected.messsageType, proposal.Action.Type())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestVote(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{}},\n\t\t{carol.String(), []string{}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.6, members)\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\ttdao.propose(0, cur, tdao.mockProposalRequest)\n\n\ttype testVoteInput struct {\n\t\tproposalID uint64\n\t\tvote       daocond.Vote\n\t\tvoter      address\n\t}\n\n\ttype testVoteExpected struct {\n\t\teval  bool\n\t\tpanic string\n\t}\n\n\ttype testVote struct {\n\t\tinput    testVoteInput\n\t\texpected testVoteExpected\n\t}\n\n\ttype testVoteTable map[string]testVote\n\n\ttests := testVoteTable{\n\t\t\"Success no\": {\n\t\t\tinput: testVoteInput{\n\t\t\t\tproposalID: 1,\n\t\t\t\tvote:       \"no\",\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testVoteExpected{\n\t\t\t\teval: false,\n\t\t\t},\n\t\t},\n\t\t\"Success yes\": {\n\t\t\tinput: testVoteInput{\n\t\t\t\tproposalID: 1,\n\t\t\t\tvote:       \"yes\",\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testVoteExpected{\n\t\t\t\teval: true,\n\t\t\t},\n\t\t},\n\t\t\"Unknown proposal\": {\n\t\t\tinput: testVoteInput{\n\t\t\t\tproposalID: 2,\n\t\t\t\tvote:       \"yes\",\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testVoteExpected{\n\t\t\t\tpanic: \"proposal not found\",\n\t\t\t},\n\t\t},\n\t\t\"Non-member\": {\n\t\t\tinput: testVoteInput{\n\t\t\t\tproposalID: 1,\n\t\t\t\tvote:       \"yes\",\n\t\t\t\tvoter:      dave,\n\t\t\t},\n\t\t\texpected: testVoteExpected{\n\t\t\t\tpanic: \"caller is not a member\",\n\t\t\t},\n\t\t},\n\t\t\"Invalid vote\": {\n\t\t\tinput: testVoteInput{\n\t\t\t\tproposalID: 1,\n\t\t\t\tvote:       \"very-long-vote-very-long-vote-very-long-vote\",\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testVoteExpected{\n\t\t\t\tpanic: \"invalid vote\",\n\t\t\t},\n\t\t},\n\t}\n\n\tfor testName, test := range tests {\n\t\tt.Run(testName, func(cur realm, t *testing.T) {\n\t\t\t// SetRealm must run here, not inside `run`: SetRealm targets\n\t\t\t// the deepest non-testing frame's fr.Cur HIV. A non-crossing\n\t\t\t// `run` closure has no fr.Cur, so a SetRealm-from-inside-run\n\t\t\t// silently no-ops on the closure-captured cur.\n\t\t\ttesting.SetRealm(testing.NewUserRealm(test.input.voter))\n\t\t\trun := func() {\n\t\t\t\ttdao.vote(0, cur, test.input.proposalID, test.input.vote)\n\t\t\t}\n\n\t\t\t*tdao.mockHandlerCalled = false\n\n\t\t\tif test.expected.panic != \"\" {\n\t\t\t\t// tdao.vote crosses into basedao; panic is cross-realm\n\t\t\t\t// and shows up as an abort, not a same-realm panic.\n\t\t\t\tuassert.AbortsWithMessage(t, cur, test.expected.panic, run)\n\t\t\t} else {\n\t\t\t\turequire.NotPanics(t, cur, run)\n\t\t\t\turequire.False(t, *tdao.mockHandlerCalled, \"execute should not be called\")\n\t\t\t\tproposal := tdao.privdao.Core.Proposals.GetProposal(test.input.proposalID)\n\t\t\t\teval := proposal.Condition.Eval(proposal.Ballot)\n\t\t\t\tif eval != test.expected.eval {\n\t\t\t\t\tt.Errorf(\"Expected eval %t, got %t\", test.expected.eval, eval)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestExecuteProposal(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{}},\n\t\t{carol.String(), []string{}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.6, members)\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\ttdao.propose(0, cur, tdao.mockProposalRequest)\n\n\ttype testExecuteInput struct {\n\t\tproposalID uint64\n\t\texecutor   address\n\t\thaveVote   bool\n\t\tvoter      address\n\t}\n\n\ttype testExecuteExpected struct {\n\t\tpanic bool\n\t}\n\n\ttype testExecute struct {\n\t\tinput    testExecuteInput\n\t\texpected testExecuteExpected\n\t}\n\n\ttype testExecuteTable map[string]testExecute\n\n\ttests := testExecuteTable{\n\t\t\"Conditions not met\": {\n\t\t\tinput: testExecuteInput{\n\t\t\t\tproposalID: 1,\n\t\t\t\texecutor:   alice,\n\t\t\t\thaveVote:   false,\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testExecuteExpected{\n\t\t\t\tpanic: true,\n\t\t\t},\n\t\t},\n\t\t\"Success\": {\n\t\t\tinput: testExecuteInput{\n\t\t\t\tproposalID: 1,\n\t\t\t\texecutor:   alice,\n\t\t\t\thaveVote:   true,\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testExecuteExpected{\n\t\t\t\tpanic: false,\n\t\t\t},\n\t\t},\n\t\t\"Unknown proposal\": {\n\t\t\tinput: testExecuteInput{\n\t\t\t\tproposalID: 2,\n\t\t\t\texecutor:   alice,\n\t\t\t\thaveVote:   false,\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testExecuteExpected{\n\t\t\t\tpanic: true,\n\t\t\t},\n\t\t},\n\t\t\"Non-member\": {\n\t\t\tinput: testExecuteInput{\n\t\t\t\tproposalID: 1,\n\t\t\t\texecutor:   dave,\n\t\t\t\thaveVote:   false,\n\t\t\t\tvoter:      alice,\n\t\t\t},\n\t\t\texpected: testExecuteExpected{\n\t\t\t\tpanic: true,\n\t\t\t},\n\t\t},\n\t}\n\n\tfor testName, test := range tests {\n\t\tt.Run(testName, func(cur realm, t *testing.T) {\n\t\t\tif test.input.haveVote {\n\t\t\t\ttesting.SetRealm(testing.NewUserRealm(test.input.voter))\n\t\t\t\ttdao.vote(0, cur, test.input.proposalID, \"yes\")\n\t\t\t}\n\n\t\t\ttesting.SetRealm(testing.NewUserRealm(test.input.executor))\n\n\t\t\t// tdao.execute crosses into basedao; cross-realm panic is\n\t\t\t// not catchable by defer/recover, so use revive.\n\t\t\texc := revive(func() {\n\t\t\t\ttdao.execute(0, cur, test.input.proposalID)\n\t\t\t})\n\t\t\tif test.expected.panic {\n\t\t\t\tif exc == nil {\n\t\t\t\t\tt.Errorf(\"Expected panic, got none\")\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif exc != nil {\n\t\t\t\tt.Errorf(\"Unexpected panic: %v\", exc)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tproposal := tdao.privdao.Core.Proposals.GetProposal(test.input.proposalID)\n\n\t\t\tif proposal.Status != daokit.ProposalStatusExecuted {\n\t\t\t\tt.Errorf(\"Expected status %s, got %s\", daokit.ProposalStatusExecuted, proposal.Status)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestInstantExecute(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{}},\n\t\t{carol.String(), []string{}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.6, members)\n\n\ttype testInstantExecuteInput struct {\n\t\tproposalReq daokit.ProposalRequest\n\t\texecutor    address\n\t}\n\n\ttype testInstantExecuteExpected struct {\n\t\tpanic bool\n\t}\n\n\ttype testInstantExecute struct {\n\t\tinput    testInstantExecuteInput\n\t\texpected testInstantExecuteExpected\n\t}\n\n\ttype testInstantExecuteTable map[string]testInstantExecute\n\n\ttests := testInstantExecuteTable{\n\t\t\"Success\": {\n\t\t\tinput: testInstantExecuteInput{\n\t\t\t\tproposalReq: tdao.mockProposalRequest,\n\t\t\t\texecutor:    alice,\n\t\t\t},\n\t\t\texpected: testInstantExecuteExpected{\n\t\t\t\tpanic: false,\n\t\t\t},\n\t\t},\n\t\t\"Unknown action type\": {\n\t\t\tinput: testInstantExecuteInput{\n\t\t\t\tproposalReq: tdao.unknownProposalRequest,\n\t\t\t\texecutor:    alice,\n\t\t\t},\n\t\t\texpected: testInstantExecuteExpected{\n\t\t\t\tpanic: true,\n\t\t\t},\n\t\t},\n\t\t\"Non-member\": {\n\t\t\tinput: testInstantExecuteInput{\n\t\t\t\tproposalReq: tdao.mockProposalRequest,\n\t\t\t\texecutor:    dave,\n\t\t\t},\n\t\t\texpected: testInstantExecuteExpected{\n\t\t\t\tpanic: true,\n\t\t\t},\n\t\t},\n\t}\n\n\tfor testName, test := range tests {\n\t\tt.Run(testName, func(cur realm, t *testing.T) {\n\t\t\ttesting.SetRealm(testing.NewUserRealm(test.input.executor))\n\n\t\t\t// tdao.propose crosses into basedao; cross-realm panic\n\t\t\t// is not catchable by defer/recover, so use revive.\n\t\t\texc := revive(func() {\n\t\t\t\ttdao.propose(0, cur, test.input.proposalReq)\n\t\t\t})\n\t\t\tif test.expected.panic {\n\t\t\t\tif exc == nil {\n\t\t\t\t\tt.Errorf(\"Expected panic, got none\")\n\t\t\t\t}\n\t\t\t} else if exc != nil {\n\t\t\t\tt.Errorf(\"Unexpected panic: %v\", exc)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestGetMembers(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{}},\n\t\t{carol.String(), []string{}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.6, members)\n\n\texpectedMembers := []string{alice.String(), bob.String(), carol.String()}\n\tm := tdao.privdao.Members.GetMembers()\n\tif len(m) != len(expectedMembers) {\n\t\tt.Errorf(\"Expected %d members, got %d\", len(expectedMembers), len(m))\n\t}\n\n\tfor _, eMember := range expectedMembers {\n\t\tif !tdao.privdao.Members.IsMember(eMember) {\n\t\t\tt.Errorf(\"Expected member %s to be a member\", eMember)\n\t\t}\n\t}\n}\n\nfunc TestAddMemberProposal(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.2, members)\n\n\tif tdao.privdao.Members.IsMember(bob.String()) {\n\t\tt.Errorf(\"Expected member %s to not be a member\", bob.String())\n\t}\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\ttdao.instantExecute(0, cur, daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction: NewAddMemberAction(\u0026ActionAddMember{\n\t\t\tAddress: bob,\n\t\t\tRoles:   []string{\"admin\"},\n\t\t}),\n\t})\n\n\tif !tdao.privdao.Members.IsMember(bob.String()) {\n\t\tt.Errorf(\"Expected member %s to be a member\", bob.String())\n\t}\n\n\tif !tdao.privdao.Members.HasRole(bob.String(), \"admin\") {\n\t\tt.Errorf(\"Expected member %s to have role 'admin'\", bob.String())\n\t}\n\n\tproposalWithUnknownRole := daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction: NewAddMemberAction(\u0026ActionAddMember{\n\t\t\tAddress: bob,\n\t\t\tRoles:   []string{\"unknown\"},\n\t\t}),\n\t}\n\t// Cross-realm panic from instantExecute is not catchable by\n\t// defer/recover; use revive.\n\tif revive(func() { tdao.instantExecute(0, cur, proposalWithUnknownRole) }) == nil {\n\t\tt.Errorf(\"Expected panic, got none\")\n\t}\n}\n\nfunc TestRemoveMemberProposal(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{\"admin\"}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.2, members)\n\n\tif !tdao.privdao.Members.IsMember(bob.String()) {\n\t\tt.Errorf(\"Expected member %s to be a member\", bob.String())\n\t}\n\n\tif !tdao.privdao.Members.HasRole(bob.String(), \"admin\") {\n\t\tt.Errorf(\"Expected member %s to have role 'admin'\", bob.String())\n\t}\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\ttdao.instantExecute(0, cur, daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      NewRemoveMemberAction(bob),\n\t})\n\n\tif tdao.privdao.Members.IsMember(bob.String()) {\n\t\tt.Errorf(\"Expected user %s to not be a member\", bob.String())\n\t}\n\n\tif tdao.privdao.Members.HasRole(bob.String(), \"admin\") {\n\t\tt.Errorf(\"Expected user %s to not have role 'admin'\", bob.String())\n\t}\n}\n\nfunc TestAddRoleToUserProposal(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{alice.String(), []string{\"admin\"}},\n\t\t{bob.String(), []string{}},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.2, members)\n\n\tif tdao.privdao.Members.HasRole(bob.String(), \"admin\") {\n\t\tt.Errorf(\"Expected member %s to not have role 'admin'\", bob.String())\n\t}\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\ttdao.instantExecute(0, cur, daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      NewAssignRoleAction(\u0026ActionAssignRole{Address: bob, Role: \"admin\"}),\n\t})\n\tif !tdao.privdao.Members.HasRole(bob.String(), \"admin\") {\n\t\tt.Errorf(\"Expected member %s to have role 'admin'\", bob.String())\n\t}\n\n\t// Cross-realm panics aren't catchable by defer/recover; use revive\n\t// per failing case.\n\tif revive(func() {\n\t\ttdao.instantExecute(0, cur, daokit.ProposalRequest{\n\t\t\tTitle:       \"My Proposal\",\n\t\t\tDescription: \"My Proposal Description\",\n\t\t\tAction:      NewAssignRoleAction(\u0026ActionAssignRole{Address: alice, Role: \"unknown\"}),\n\t\t})\n\t}) == nil {\n\t\tt.Errorf(\"Expected panic on unknown role, got none\")\n\t}\n\n\tif revive(func() {\n\t\ttdao.instantExecute(0, cur, daokit.ProposalRequest{\n\t\t\tTitle:       \"My Proposal\",\n\t\t\tDescription: \"My Proposal Description\",\n\t\t\tAction:      NewAssignRoleAction(\u0026ActionAssignRole{Address: carol, Role: \"admin\"}),\n\t\t})\n\t}) == nil {\n\t\tt.Errorf(\"Expected panic on non-member, got none\")\n\t}\n\n\tif revive(func() {\n\t\ttdao.instantExecute(0, cur, daokit.ProposalRequest{\n\t\t\tTitle:       \"My Proposal\",\n\t\t\tDescription: \"My Proposal Description\",\n\t\t\tAction:      NewAssignRoleAction(\u0026ActionAssignRole{Address: bob, Role: \"admin\"}),\n\t\t})\n\t}) == nil {\n\t\tt.Errorf(\"Expected panic on already-assigned role, got none\")\n\t}\n}\n\nfunc TestRemoveRoleFromUserProposal(cur realm, t *testing.T) {\n\tmembers := []Member{\n\t\t{\n\t\t\talice.String(),\n\t\t\t[]string{\"admin\"},\n\t\t},\n\t\t{\n\t\t\tbob.String(),\n\t\t\t[]string{\"admin\"},\n\t\t},\n\t}\n\ttdao := newTestingDAO(0, cur, t, 0.2, members)\n\n\tif !tdao.privdao.Members.HasRole(bob.String(), \"admin\") {\n\t\tt.Errorf(\"Expected member %s to have role 'admin'\", bob.String())\n\t}\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\ttdao.instantExecute(0, cur, daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      NewUnassignRoleAction(\u0026ActionUnassignRole{Address: bob, Role: \"admin\"}),\n\t})\n\n\tif tdao.privdao.Members.HasRole(bob.String(), \"admin\") {\n\t\tt.Errorf(\"Expected member %s to not have role 'admin'\", bob.String())\n\t}\n\n\tproposalWithUnknowkRole := daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      NewUnassignRoleAction(\u0026ActionUnassignRole{Address: alice, Role: \"unknown\"}),\n\t}\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tif revive(func() { tdao.instantExecute(0, cur, proposalWithUnknowkRole) }) == nil {\n\t\tt.Errorf(\"Expected panic on unknown role, got none\")\n\t}\n\n\tproposalWithNonMember := daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      NewUnassignRoleAction(\u0026ActionUnassignRole{Address: carol, Role: \"admin\"}),\n\t}\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tif revive(func() { tdao.instantExecute(0, cur, proposalWithNonMember) }) == nil {\n\t\tt.Errorf(\"Expected panic on non-member, got none\")\n\t}\n\n\tproposalWithNonRole := daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      NewUnassignRoleAction(\u0026ActionUnassignRole{Address: bob, Role: \"admin\"}),\n\t}\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tif revive(func() { tdao.instantExecute(0, cur, proposalWithNonRole) }) == nil {\n\t\tt.Errorf(\"Expected panic on already-unassigned role, got none\")\n\t}\n}\n\ntype testingDAOContext struct {\n\tdao                    *DAOWrapper\n\tprivdao                *DAOPrivate\n\tmockHandlerCalled      *bool\n\tmockHandler            daokit.ActionHandler\n\tmockProposalRequest    daokit.ProposalRequest\n\tunknownProposalRequest daokit.ProposalRequest\n}\n\nfunc newTestingDAO(_ int, rlm realm, t *testing.T, threshold float64, members []Member) *testingDAOContext {\n\troles := []RoleInfo{{Name: \"admin\", Description: \"Admin is the superuser\"}}\n\tmembersStore := NewMembersStore(roles, members)\n\tinitialCondition := daocond.MembersThreshold(threshold, membersStore.IsMember, membersStore.MembersCount)\n\n\tconf := \u0026Config{\n\t\tName:             \"My DAO\",\n\t\tDescription:      \"My DAO Description\",\n\t\tMembers:          membersStore,\n\t\tInitialCondition: initialCondition,\n\t\tGetProfileString: func(addr address, field, def string) string { return \"\" },\n\t}\n\n\tdaoRealm := testing.NewCodeRealm(\"gno.land/r/testing/daorealm\")\n\ttesting.SetOriginCaller(alice)\n\ttesting.SetRealm(daoRealm)\n\tpubdao := New(conf, rlm)\n\tdao := pubdao.Private()\n\n\tmockHandlerCalled := false\n\tmockHandler := daokit.NewActionHandler(\"valid\", func(_ interface{}) { mockHandlerCalled = true })\n\tdao.Core.Resources.Set(\u0026daokit.Resource{\n\t\tHandler:   mockHandler,\n\t\tCondition: daocond.MembersThreshold(0.2, dao.Members.IsMember, dao.Members.MembersCount),\n\t})\n\n\tmockProposalRequest := daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      daokit.NewAction(\"valid\", nil),\n\t}\n\n\tunknownProposalRequest := daokit.ProposalRequest{\n\t\tTitle:       \"My Proposal\",\n\t\tDescription: \"My Proposal Description\",\n\t\tAction:      daokit.NewAction(\"unknown\", nil),\n\t}\n\n\treturn \u0026testingDAOContext{\n\t\tdao:                    pubdao,\n\t\tprivdao:                dao,\n\t\tmockHandlerCalled:      \u0026mockHandlerCalled,\n\t\tmockHandler:            mockHandler,\n\t\tmockProposalRequest:    mockProposalRequest,\n\t\tunknownProposalRequest: unknownProposalRequest,\n\t}\n}\n\n// Crossing wrappers for dao.X calls.\n\nfunc (c *testingDAOContext) propose(_ int, rlm realm, req daokit.ProposalRequest) (id uint64) {\n\tfunc(cur realm) { id = c.dao.Propose(0, cur, req) }(cross(rlm))\n\treturn\n}\n\nfunc (c *testingDAOContext) vote(_ int, rlm realm, id uint64, v daocond.Vote) {\n\tfunc(cur realm) { c.dao.Vote(0, cur, id, v) }(cross(rlm))\n}\n\nfunc (c *testingDAOContext) execute(_ int, rlm realm, id uint64) {\n\tfunc(cur realm) { c.dao.Execute(0, cur, id) }(cross(rlm))\n}\n\nfunc (c *testingDAOContext) instantExecute(_ int, rlm realm, req daokit.ProposalRequest) (id uint64) {\n\tfunc(cur realm) { id = c.dao.InstantExecute(0, cur, req) }(cross(rlm))\n\treturn\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/basedao\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"},{"name":"members.gno","body":"package basedao\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/onbloc/json\"\n)\n\ntype MembersStore struct {\n\tRoles   *avl.Tree // role name -\u003e *Role\n\tMembers *avl.Tree // string -\u003e *avl.Tree [roles -\u003e struct{}]\n}\n\ntype Member struct {\n\tAddress string\n\tRoles   []string\n}\n\ntype RoleInfo struct {\n\tName        string\n\tDescription string\n}\n\ntype Role struct {\n\tName        string\n\tDescription string\n\tMembers     *avl.Tree // string -\u003e struct{}\n}\n\nconst EventAddMember = \"BaseDAOAddMember\"\nconst EventRemoveMember = \"BaseDAORemoveMember\"\n\nfunc NewMembersStore(initialRoles []RoleInfo, initialMembers []Member) *MembersStore {\n\tres := \u0026MembersStore{\n\t\tRoles:   avl.NewTree(),\n\t\tMembers: avl.NewTree(),\n\t}\n\tres.setRoles(initialRoles)\n\tres.setMembers(initialMembers)\n\treturn res\n}\n\nfunc (m *MembersStore) HasRole(member string, role string) bool {\n\trolesRaw := m.Members.Get(member)\n\tif rolesRaw == nil {\n\t\treturn false\n\t}\n\troles, ok := rolesRaw.(*avl.Tree)\n\tif !ok {\n\t\treturn false\n\t}\n\treturn roles.Has(role)\n}\n\nfunc (m *MembersStore) IsMember(member string) bool {\n\treturn m.Members.Has(member)\n}\n\nfunc (m *MembersStore) RoleInfo(role string) RoleInfo {\n\troleDataRaw := m.Roles.Get(role)\n\tif roleDataRaw == nil {\n\t\tpanic(\"role does not exist\")\n\t}\n\troleData, ok := roleDataRaw.(*Role)\n\tif !ok {\n\t\tpanic(\"a value of memberstore.roles is not a Role, should not happen\")\n\t}\n\treturn RoleInfo{\n\t\tName:        roleData.Name,\n\t\tDescription: roleData.Description,\n\t}\n}\n\nfunc (m *MembersStore) MembersCount() uint64 {\n\treturn uint64(m.Members.Size())\n}\n\nfunc (m *MembersStore) GetMembers() []string {\n\tmembers := make([]string, 0, m.Members.Size())\n\tm.Members.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tmembers = append(members, key)\n\t\treturn false\n\t})\n\treturn members\n}\n\nfunc (m *MembersStore) GetRoles() []string {\n\ti := 0\n\tres := make([]string, m.Roles.Size())\n\tm.Roles.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tres[i] = key\n\t\ti++\n\t\treturn false\n\t})\n\treturn res\n}\n\nfunc (m *MembersStore) GetMemberRoles(member string) []string {\n\trolesRaw := m.Members.Get(member)\n\tif rolesRaw == nil {\n\t\treturn []string{}\n\t}\n\troles, ok := rolesRaw.(*avl.Tree)\n\tif !ok {\n\t\treturn []string{}\n\t}\n\ti := 0\n\tres := make([]string, roles.Size())\n\troles.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tres[i] = key\n\t\ti++\n\t\treturn false\n\t})\n\treturn res\n}\n\nfunc (m *MembersStore) CountMemberRoles(member string) int {\n\trolesRaw := m.Members.Get(member)\n\tif rolesRaw == nil {\n\t\treturn 0\n\t}\n\troles, ok := rolesRaw.(*avl.Tree)\n\tif !ok {\n\t\treturn 0\n\t}\n\treturn roles.Size()\n}\n\nfunc (m *MembersStore) GetMembersWithRole(role string) []string {\n\troleDataRaw := m.Roles.Get(role)\n\tif roleDataRaw == nil {\n\t\treturn []string{}\n\t}\n\troleData, ok := roleDataRaw.(*Role)\n\tif !ok {\n\t\treturn []string{}\n\t}\n\ti := 0\n\tres := make([]string, roleData.Members.Size())\n\troleData.Members.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tres[i] = key\n\t\ti++\n\t\treturn false\n\t})\n\treturn res\n}\n\nfunc (m *MembersStore) CountMembersWithRole(role string) uint32 {\n\treturn uint32(len(m.GetMembersWithRole(role)))\n}\n\nfunc (m *MembersStore) setRoles(roles []RoleInfo) {\n\tfor _, role := range roles {\n\t\tm.AddRole(role)\n\t}\n}\n\nfunc (m *MembersStore) setMembers(members []Member) {\n\tfor _, member := range members {\n\t\tm.AddMember(member.Address, member.Roles)\n\t}\n}\n\nfunc (m *MembersStore) AddMember(member string, roles []string) {\n\tif m.IsMember(member) {\n\t\tpanic(\"member already exists\")\n\t}\n\n\tmembersRoles := avl.NewTree()\n\tfor _, role := range roles {\n\t\tif !m.Roles.Has(role) {\n\t\t\tpanic(\"role: \" + role + \" does not exist\")\n\t\t}\n\t\tmembersRoles.Set(role, struct{}{})\n\t}\n\tm.Members.Set(member, membersRoles)\n\n\tchain.Emit(EventAddMember,\n\t\t\"address\", member,\n\t)\n}\n\nfunc (m *MembersStore) RemoveMember(member string) {\n\tif !m.IsMember(member) {\n\t\tpanic(\"member does not exist\")\n\t}\n\n\tmemberRolesRaw := m.Members.Get(member)\n\tif memberRolesRaw == nil {\n\t\tpanic(\"should not happen\")\n\t}\n\tmemberRoles, ok := memberRolesRaw.(*avl.Tree)\n\tif !ok {\n\t\tpanic(\"a value of memberstore.members is not an avl.Tree, should not happen\")\n\t}\n\n\tmemberRoles.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\troleRaw := m.Roles.Get(key)\n\t\tif roleRaw == nil {\n\t\t\treturn false\n\t\t}\n\t\trole, ok := roleRaw.(*Role)\n\t\tif !ok {\n\t\t\tpanic(\"a value of memberstore.roles is not a Role, should not happen\")\n\t\t}\n\t\trole.Members.Remove(role.Name)\n\t\treturn false\n\t})\n\tm.Members.Remove(member)\n\n\tchain.Emit(EventRemoveMember,\n\t\t\"address\", member,\n\t)\n}\n\nfunc (m *MembersStore) AddRole(role RoleInfo) {\n\tif m.Roles.Has(role.Name) {\n\t\tpanic(\"role already exists\")\n\t}\n\n\troleData := \u0026Role{\n\t\tName:        role.Name,\n\t\tDescription: role.Description,\n\t\tMembers:     avl.NewTree(),\n\t}\n\n\tm.Roles.Set(role.Name, roleData)\n}\n\nfunc (m *MembersStore) RemoveRole(role string) {\n\troleDataRaw := m.Roles.Get(role)\n\tif roleDataRaw == nil {\n\t\tpanic(\"role does not exist\")\n\t}\n\troleData, ok := roleDataRaw.(*Role)\n\tif !ok {\n\t\tpanic(\"a value of memberstore.roles is not a Role, should not happen\")\n\t}\n\n\troleData.Members.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tmemberRaw := m.Members.Get(key)\n\t\tif memberRaw == nil {\n\t\t\treturn false\n\t\t}\n\t\tmember, ok := memberRaw.(*avl.Tree)\n\t\tif !ok {\n\t\t\tpanic(\"a value of memberstore.members is not an avl.Tree, should not happen\")\n\t\t}\n\t\tmember.Remove(role)\n\t\treturn false\n\t})\n\tm.Roles.Remove(role)\n}\n\nfunc (m *MembersStore) AddRoleToMember(member string, role string) {\n\tif !m.IsMember(member) {\n\t\tpanic(\"member does not exist\")\n\t}\n\tif !m.Roles.Has(role) {\n\t\tpanic(\"role \" + role + \" does not exist\")\n\t}\n\tif m.HasRole(member, role) {\n\t\tpanic(\"member already has the role\")\n\t}\n\n\tmemberRolesRaw := m.Members.Get(member)\n\tif memberRolesRaw == nil {\n\t\tpanic(\"should not happen\")\n\t}\n\tmemberRoles, ok := memberRolesRaw.(*avl.Tree)\n\tif !ok {\n\t\tpanic(\"a value of memberstore.members is not an avl.Tree, should not happen\")\n\t}\n\n\troleDataRaw := m.Roles.Get(role)\n\tif roleDataRaw == nil {\n\t\tpanic(\"should not happen\")\n\t}\n\troleData, ok := roleDataRaw.(*Role)\n\tif !ok {\n\t\tpanic(\"a value of memberstore.roles is not a Role, should not happen\")\n\t}\n\n\troleData.Members.Set(member, struct{}{})\n\tmemberRoles.Set(role, struct{}{})\n}\n\nfunc (m *MembersStore) RemoveRoleFromMember(member string, role string) {\n\tif !m.IsMember(member) {\n\t\tpanic(\"member does not exist\")\n\t}\n\tif !m.Roles.Has(role) {\n\t\tpanic(\"role \" + role + \" does not exist\")\n\t}\n\tif !m.HasRole(member, role) {\n\t\tpanic(\"member does not have the role\")\n\t}\n\n\tmemberRolesRaw := m.Members.Get(member)\n\tif memberRolesRaw == nil {\n\t\tpanic(\"should not happen\")\n\t}\n\tmemberRoles, ok := memberRolesRaw.(*avl.Tree)\n\tif !ok {\n\t\tpanic(\"a value of memberstore.members is not an avl.Tree, should not happen\")\n\t}\n\n\troleDataRaw := m.Roles.Get(role)\n\tif roleDataRaw == nil {\n\t\tpanic(\"should not happen\")\n\t}\n\troleData, ok := roleDataRaw.(*Role)\n\tif !ok {\n\t\tpanic(\"a value of memberstore.roles is not a Role, should not happen\")\n\t}\n\n\tmemberRoles.Remove(role)\n\troleData.Members.Remove(member)\n}\n\nfunc (m *MembersStore) GetMembersJSON() string {\n\t// XXX: replace with protoc-gen-gno\n\tmembers := []*json.Node{}\n\tfor _, memberID := range m.GetMembers() {\n\t\troles := []*json.Node{}\n\t\tfor _, role := range m.GetMemberRoles(memberID) {\n\t\t\troles = append(roles, json.StringNode(\"\", role))\n\t\t}\n\t\tmembers = append(members, json.ObjectNode(\"\", map[string]*json.Node{\n\t\t\t\"address\": json.StringNode(\"\", memberID),\n\t\t\t\"roles\":   json.ArrayNode(\"\", roles),\n\t\t}))\n\t}\n\tnode := json.ArrayNode(\"\", members)\n\tbz, err := json.Marshal(node)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn string(bz)\n}\n"},{"name":"render.gno","body":"package basedao\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/samcrew/daokit\"\n)\n\n// WELL KNOWN PATHS\nconst (\n\tHOME_PATH             = \"\"\n\tHOME_NO_PROFILE_PATH  = \"noprofile\"\n\tCONFIG_PATH           = \"config\"\n\tPROPOSAL_HISTORY_PATH = \"history\"\n\tMEMBER_DETAIL_PATH    = \"member/{address}\"\n\tPROPOSAL_DETAIL_PATH  = \"proposal/{id}\"\n\tFALLBACK_DISPLAY_NAME = \"Anon\"\n)\n\nfunc (d *DAOPrivate) initRenderingRouter() {\n\tif d.RenderRouter == nil {\n\t\td.RenderRouter = mux.NewRouter()\n\t}\n}\n\nfunc (d *DAOPrivate) InitDefaultRendering() {\n\td.RenderRouter.HandleFunc(HOME_PATH, d.MuxHomePage)\n\td.RenderRouter.HandleFunc(HOME_NO_PROFILE_PATH, d.MuxHomePage)\n\td.RenderRouter.HandleFunc(CONFIG_PATH, d.MuxConfigPage)\n\td.RenderRouter.HandleFunc(PROPOSAL_HISTORY_PATH, d.MuxProposalHistoryPage)\n\td.RenderRouter.HandleFunc(MEMBER_DETAIL_PATH, d.MuxMemberDetailPage)\n\td.RenderRouter.HandleFunc(PROPOSAL_DETAIL_PATH, d.MuxProposalDetailPage)\n}\n\nfunc (d *DAOPrivate) Render(path string) string {\n\treturn d.RenderRouter.Render(path)\n}\n\n// DEFAULT_HANDLERS\n\nfunc (d *DAOPrivate) MuxHomePage(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(d.HomePageView())\n}\n\nfunc (d *DAOPrivate) MuxConfigPage(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(d.ConfigPageView())\n}\n\nfunc (d *DAOPrivate) MuxProposalHistoryPage(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(d.ProposalHistoryPageView())\n}\n\nfunc (d *DAOPrivate) MuxMemberDetailPage(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(d.MemberDetailPageView(req.GetVar(\"address\")))\n}\n\nfunc (d *DAOPrivate) MuxProposalDetailPage(res *mux.ResponseWriter, req *mux.Request) {\n\tid, err := strconv.ParseUint(req.GetVar(\"id\"), 10, 64)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tres.Write(d.ProposalDetailPageView(id))\n}\n\n// PUBLIC PAGES\n\nfunc (d *DAOPrivate) HomePageView() string {\n\treturn d.HomeProfileHeaderView(false) +\n\t\td.HomeMembersView() +\n\t\td.HomeProposalsView()\n}\n\nfunc (d *DAOPrivate) ConfigPageView() string {\n\treturn d.ConfigHeaderView() +\n\t\td.ConfigRolesView() +\n\t\td.ConfigResourcesView()\n}\n\nfunc (d *DAOPrivate) ProposalHistoryPageView() string {\n\treturn d.ProposalHistoryHeaderView() +\n\t\td.ProposalHistoryView()\n}\n\nfunc (d *DAOPrivate) MemberDetailPageView(address string) string {\n\treturn d.MemberDetailHeaderView() +\n\t\td.MemberDetailView(address)\n}\n\nfunc (d *DAOPrivate) ProposalDetailPageView(idu uint64) string {\n\treturn d.ProposalDetailHeaderView() +\n\t\td.ProposalDetailView(idu)\n}\n\n// --- PUBLIC COMPONENT VIEWS ---\n\n// HOME PAGE COMPONENTS VIEWS\n\nfunc (d *DAOPrivate) HomeProfileHeaderView(noprofile bool) string {\n\ts := \"\"\n\tname := d.GetProfileString(d.RealmAddress, \"DisplayName\", \"DAO\")\n\tdescription := d.GetProfileString(d.RealmAddress, \"Bio\", \"Created with daokit\")\n\tpkgPath := d.RealmPkgPath\n\tlinkPath := getLinkPath(pkgPath)\n\n\tif !noprofile {\n\t\ts += ufmt.Sprintf(\"# %s\\n\\n\", name)\n\t\timageURI := d.GetProfileString(d.RealmAddress, \"Avatar\", \"\")\n\t\tif imageURI != \"\" {\n\t\t\ts += ufmt.Sprintf(\"![Image](%s)\\n\\n\", imageURI)\n\t\t}\n\t\ts += ufmt.Sprintf(\"%s\\n\\n\", description)\n\t}\n\n\ts += ufmt.Sprintf(\"\u003e Realm address: %s\\n\\n\", d.RealmAddress)\n\ts += ufmt.Sprintf(\"Discover more about this DAO on the [configuration page ⚙️](%s:%s)\\n\\n\", linkPath, CONFIG_PATH)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\n\treturn s\n}\n\nfunc (d *DAOPrivate) HomeMembersView() string {\n\tpkgPath := d.RealmPkgPath\n\tlinkPath := getLinkPath(pkgPath)\n\ts := \"\"\n\ts += ufmt.Sprintf(\"## Members 👤 \\n\\n\")\n\ti := 1\n\td.Members.Members.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\ts += ufmt.Sprintf(\"- **Member %d: [%s](%s:%s/%s)**\\n\\n\", i, key, linkPath, \"member\", key)\n\t\ts += ufmt.Sprintf(\"\t- **Profile:** %s\\n\", d.GetProfileString(address(key), \"DisplayName\", FALLBACK_DISPLAY_NAME))\n\t\ts += ufmt.Sprintf(\" \t- **Roles:** %s\\n\\n\", strings.Join(d.Members.GetMemberRoles(key), \", \"))\n\t\ti += 1\n\t\treturn false\n\t})\n\ts += ufmt.Sprintf(\"\u003e You can find more information about a member by clicking on their address\\n\\n\")\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\nfunc (d *DAOPrivate) HomeProposalsView() string {\n\tpkgPath := d.RealmPkgPath\n\tlinkPath := getLinkPath(pkgPath)\n\ts := \"\"\n\ts += ufmt.Sprintf(\"## Proposals 🗳️ \\n\\n\")\n\ti := 0\n\td.Core.Proposals.Tree.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tproposal := value.(*daokit.Proposal)\n\t\tif proposal.Status != daokit.ProposalStatusOpen {\n\t\t\treturn false\n\t\t}\n\t\tid, err := seqid.FromString(key)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\ts += ufmt.Sprintf(\"- **Proposal %d: [%s](%s:%s/%d)**\\n\\n\", uint64(id), proposal.Title, linkPath, \"proposal\", uint64(id))\n\t\ti += 1\n\t\treturn false\n\t})\n\tif i == 0 {\n\t\ts += ufmt.Sprintf(\"\\t⚠️ There are no running proposals at the moment\\n\\n\")\n\t}\n\ts += ufmt.Sprintf(\"\u003e See the [proposal history 📜](%s:%s) for more information\\n\\n\", linkPath, PROPOSAL_HISTORY_PATH)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\n// CONFIG PAGE COMPONENTS VIEWS\n\nfunc (d *DAOPrivate) ConfigHeaderView() string {\n\tname := d.GetProfileString(d.RealmAddress, \"DisplayName\", \"DAO\")\n\ts := \"\"\n\ts += ufmt.Sprintf(\"# %s - Config ⚙️\\n\\n\", name)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\nfunc (d *DAOPrivate) ConfigRolesView() string {\n\troles := d.Members.GetRoles()\n\ts := \"\"\n\ts += ufmt.Sprintf(\"## Roles 🏷️\\n\\n\")\n\tfor _, role := range roles {\n\t\ts += ufmt.Sprintf(\"- %s\\n\\n\", role)\n\t\tinfo := d.Members.RoleInfo(role)\n\t\ts += ufmt.Sprintf(\"  %s\\n\\n\", info.Description)\n\t}\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\nfunc (d *DAOPrivate) ConfigResourcesView() string {\n\ts := \"\"\n\ts += ufmt.Sprintf(\"## Resources 📦\\n\\n\")\n\ti := 1\n\td.Core.Resources.Tree.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tresource := value.(*daokit.Resource)\n\t\ts += ufmt.Sprintf(\"- **Resource #%d: %s**\\n\\n\", i, key)\n\t\t// TODO: add doc to handler and print here\n\t\ts += ufmt.Sprintf(\"  - **Name:** %s\\n\", resource.DisplayName)\n\t\ts += ufmt.Sprintf(\"  - **Description:** %s\\n\", resource.Description)\n\t\ts += ufmt.Sprintf(\"  - **Condition:** %s\\n\\n\", resource.Condition.Render())\n\t\ti += 1\n\t\treturn false\n\t})\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\n// HISTORY PAGE COMPONENTS VIEWS\n\nfunc (d *DAOPrivate) ProposalHistoryHeaderView() string {\n\tname := d.GetProfileString(d.RealmAddress, \"DisplayName\", \"DAO\")\n\ts := \"\"\n\ts += ufmt.Sprintf(\"# %s - Proposal History\\n\\n\", name)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\nfunc (d *DAOPrivate) ProposalHistoryView() string {\n\tpkgPath := d.RealmPkgPath\n\tlinkPath := getLinkPath(pkgPath)\n\ts := \"\"\n\ts += ufmt.Sprintf(\"## Proposals 🗳️\\n\\n\")\n\ti := 1\n\td.Core.Proposals.Tree.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tproposal, ok := value.(*daokit.Proposal)\n\t\tif !ok {\n\t\t\tpanic(\"unexpected invalid proposal type\")\n\t\t}\n\t\tid, err := seqid.FromString(key)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\ts += ufmt.Sprintf(\"- **Proposal %d: [%s](%s:%s/%d) - %s**\\n\\n\", uint64(id), proposal.Title, linkPath, \"proposal\", uint64(id), proposal.Status)\n\t\ti += 1\n\t\treturn false\n\t})\n\ts += ufmt.Sprintf(\"[Add a new proposal 🗳️](%s$help)\\n\\n\", linkPath)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\n// MEMBER DETAIL PAGE COMPONENTS VIEWS\n\nfunc (d *DAOPrivate) MemberDetailHeaderView() string {\n\tname := d.GetProfileString(d.RealmAddress, \"DisplayName\", \"DAO\")\n\ts := \"\"\n\ts += ufmt.Sprintf(\"# %s - Member Detail\\n\\n\", name)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\nfunc (d *DAOPrivate) MemberDetailView(addr string) string {\n\tpkgPath := d.RealmPkgPath\n\tlinkPath := getLinkPath(pkgPath)\n\troles := d.Members.GetMemberRoles(addr)\n\tdisplayName := d.GetProfileString(address(addr), \"DisplayName\", FALLBACK_DISPLAY_NAME)\n\tbio := d.GetProfileString(address(addr), \"Bio\", \"No bio\")\n\tpp := d.GetProfileString(address(addr), \"Avatar\", \"\")\n\ts := \"\"\n\ts += ufmt.Sprintf(\"## Profile 👤\\n\\n\")\n\ts += ufmt.Sprintf(\"- **Display Name:** %s\\n\\n\", displayName)\n\ts += ufmt.Sprintf(\"- **Bio:** %s\\n\\n\", bio)\n\tif pp != \"\" {\n\t\ts += ufmt.Sprintf(\"![Avatar](%s)\\n\\n\", pp)\n\t}\n\ts += ufmt.Sprintf(\"## Roles 🏷️\\n\\n\")\n\tfor _, role := range roles {\n\t\ts += ufmt.Sprintf(\"- %s\\n\\n\", role)\n\t}\n\ts += ufmt.Sprintf(\"\u003e Learn more about the roles on the [configuration page ⚙️](%s:%s)\\n\\n\", linkPath, CONFIG_PATH)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\n// PROPOSAL DETAIL PAGE COMPONENTS VIEWS\n\nfunc (d *DAOPrivate) ProposalDetailHeaderView() string {\n\tname := d.GetProfileString(d.RealmAddress, \"DisplayName\", \"DAO\")\n\ts := \"\"\n\ts += ufmt.Sprintf(\"# %s - Proposal Detail\\n\\n\", name)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\nfunc (d *DAOPrivate) ProposalDetailView(idu uint64) string {\n\tpkgPath := d.RealmPkgPath\n\tlinkPath := getLinkPath(pkgPath)\n\tid := seqid.ID(idu)\n\tproposal := d.Core.Proposals.GetProposal(uint64(id))\n\ts := \"\"\n\ts += ufmt.Sprintf(\"## Title - %s 📜\\n\\n\", proposal.Title)\n\ts += ufmt.Sprintf(\"## Description 📝\\n\\n%s\\n\\n\", proposal.Description)\n\ts += ufmt.Sprintf(\"## Resource - %s 📦\\n\\n\", proposal.Action.Type())\n\tresource := d.Core.Resources.Get(proposal.Action.Type())\n\ts += ufmt.Sprintf(\"  - **Name:** %s\\n\", resource.DisplayName)\n\ts += ufmt.Sprintf(\"  - **Description:** %s\\n\", resource.Description)\n\ts += ufmt.Sprintf(\"  - **Condition:** %s\\n\\n\", resource.Condition.Render())\n\ts += (\"---\\n\\n\")\n\ts += proposal.Action.String() + \"\\n\\n\"\n\ts += (\"---\\n\\n\")\n\tif proposal.Status == daokit.ProposalStatusOpen {\n\t\ts += ufmt.Sprintf(\"## Status - Open 🟡\\n\\n\")\n\t\ts += ufmt.Sprintf(\"[Vote on this proposal 🗳️](%s$help)\\n\\n\", linkPath)\n\t} else if proposal.Status == daokit.ProposalStatusPassed {\n\t\ts += ufmt.Sprintf(\"## Status - Passed 🟢\\n\\n\")\n\t\ts += ufmt.Sprintf(\"[Execute this proposal 🗳️](%s$help)\\n\\n\", linkPath)\n\t} else if proposal.Status == daokit.ProposalStatusExecuted {\n\t\ts += ufmt.Sprintf(\"## Status - Executed ✅\\n\\n\")\n\t} else {\n\t\ts += ufmt.Sprintf(\"## Status - Closed 🔴\\n\\n\")\n\t}\n\ts += ufmt.Sprintf(\"\u003e proposed by %s 👤\\n\\n\", proposal.ProposerID)\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\ts += ufmt.Sprintf(\"## Votes 🗳️\\n\\n%s\\n\\n\", proposal.Condition.RenderWithVotes(proposal.Ballot))\n\ts += ufmt.Sprintf(\"\\n--------------------------------\\n\")\n\treturn s\n}\n\nfunc getLinkPath(pkgPath string) string {\n\tslashIdx := strings.IndexRune(pkgPath, '/')\n\tif slashIdx != 1 {\n\t\treturn pkgPath[slashIdx:]\n\t}\n\treturn \"\"\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"TnNDH19wKS6y4ePnf0/Na28N3JOTeFRYMUP5uC41aCK/+biDEiD/auJK1mWXqi/5LQVXtd5UioZ+dCyOlLLfBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"gauge","path":"gno.land/p/samcrew/gauge","files":[{"name":"README.md","body":"# `gauge` - SVG progress bars\n\nGenerate progress bars and gauges as SVG images with customizable styling and labels.\n\n## Usage\n\n```go\n// Basic gauge with default config\ngauge := gauge.Render(75, 100, \"Progress\", \"#4caf50\", gauge.DefaultConfig)\n\n// Custom configuration\nconfig := gauge.Config{\n    PercentOnly:  true,\n    Width:        400,\n    CanvasHeight: 40,\n    FontSize:     18,\n    PaddingH:     10,\n}\ngauge := gauge.Render(33, 50, \"Loading\", \"#2196f3\", config)\n\n// Different styles\nprogress := gauge.Render(8, 10, \"Health\", \"#f44336\", gauge.DefaultConfig)\n```\n\n## API Reference\n\n```go\ntype Config struct {\n    PercentOnly  bool // Only display the percentage on the right side\n    Width        int  // Width of the gauge in pixels\n    CanvasHeight int  // Height of the gauge in pixels\n    FontSize     int  // Font size of the text in pixels\n    PaddingH     int  // Horizontal padding (for the text) in pixels\n}\n\nvar DefaultConfig = Config{\n    PercentOnly:  false,\n    Width:        300,\n    CanvasHeight: 30,\n    FontSize:     16,\n    PaddingH:     6,\n}\n\n// value: Current value (must be ≤ total)\n// total: Maximum value (must be \u003e 0)\n// label: Text label displayed on the left\n// color: Fill color (hex format, e.g., \"#4caf50\")\n// config: Configuration options\n// Returns: SVG string as markdown image\nfunc Render(value int, total int, label string, color string, config Config) string\n```\n\n## Live Example\n\n- [/r/docs/charts:gauge](/r/docs/charts:gauge)\n"},{"name":"gauge.gno","body":"// Package gauge provides a simple way to render a gauge bar in SVG format.\npackage gauge\n\nimport (\n\t\"gno.land/p/demo/svg\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Config struct {\n\tPercentOnly  bool\n\tWidth        int\n\tCanvasHeight int\n\tFontSize     int\n\tPaddingH     int\n}\n\nvar DefaultConfig = Config{\n\tPercentOnly:  false,\n\tWidth:        300,\n\tCanvasHeight: 30,\n\tFontSize:     16,\n\tPaddingH:     6,\n}\n\n// Render generates an SVG gauge bar.\n// Parameters:\n// - value: current value (must be \u003c= total)\n// - total: maximum value (must be \u003e 0)\n// - label: text label to display on the left side of the gauge\n// - color: color of the filled part of the gauge (e.g., \"#4caf50\")\n// - config: configuration options:\n//   - PercentOnly: Only display the percentage on the right side; otherwise, display \"value / total · percentage\"\n//   - Width: Width of the gauge in pixels\n//   - CanvasHeight: Height of the gauge in pixels\n//   - FontSize: Font size of the text in pixels\n//   - PaddingH: Horizontal padding (for the text) in pixels\n//\n// Example usage: gauge.Render(30, 100, \"Progress\", \"#4caf50\", \u0026gauge.Config{PercentOnly: true, Width: 400})\nfunc Render(value int, total int, label string, color string, config Config) string {\n\tif total \u003c= 0 {\n\t\treturn \"jauge fails: total must be greater than 0\"\n\t}\n\tif value \u003c= 0 {\n\t\treturn \"jauge fails: value must be greater than 0\"\n\t}\n\tif value \u003e total {\n\t\treturn \"jauge fails: value cannot be greater than total\"\n\t}\n\n\tcanvasWidth := config.Width\n\tcanvasHeight := config.CanvasHeight\n\tfontSize := config.FontSize\n\tpaddingH := config.PaddingH\n\tcanvas := svg.NewCanvas(canvasWidth, canvasHeight)\n\n\tratio := 0.0\n\tif total \u003e 0 {\n\t\tratio = float64(value) / float64(total)\n\t}\n\n\tleftText := label\n\trightText := ufmt.Sprintf(\"%d / %d · %0.0f%%\", value, total, ratio*100)\n\tif config.PercentOnly {\n\t\trightText = ufmt.Sprintf(\"%0.0f%%\", ratio*100)\n\t}\n\n\t// Background\n\tcanvas.Append(svg.Rectangle{\n\t\tX:      0,\n\t\tY:      0,\n\t\tWidth:  canvasWidth,\n\t\tHeight: canvasHeight,\n\t\tFill:   \"#e0e0e0\",\n\t\tRX:     2,\n\t\tRY:     2,\n\t})\n\t// Filled bar\n\tcanvas.Append(svg.Rectangle{\n\t\tX:      0,\n\t\tY:      0,\n\t\tWidth:  int(ratio * float64(canvasWidth)),\n\t\tHeight: canvasHeight,\n\t\tFill:   color,\n\t\tRX:     2,\n\t\tRY:     2,\n\t})\n\t// Left text (label)\n\tcanvas.Append(svg.Text{\n\t\tX:    paddingH,\n\t\tY:    canvasHeight / 2,\n\t\tText: leftText,\n\t\tFill: \"#000000\",\n\t\tAttr: svg.BaseAttrs{\n\t\t\tStyle: ufmt.Sprintf(\n\t\t\t\t\"font-family:'Inter var',sans-serif;font-size:%dpx;dominant-baseline:middle;text-anchor:start;\",\n\t\t\t\tfontSize,\n\t\t\t),\n\t\t},\n\t})\n\t// Right text (ratio + %)\n\tcanvas.Append(svg.Text{\n\t\tX:    canvasWidth - paddingH,\n\t\tY:    canvasHeight / 2,\n\t\tText: rightText,\n\t\tFill: \"#000000\",\n\t\tAttr: svg.BaseAttrs{\n\t\t\tStyle: ufmt.Sprintf(\n\t\t\t\t\"font-family:'Inter var',sans-serif;font-size:%dpx;dominant-baseline:middle;text-anchor:end;\",\n\t\t\t\tfontSize,\n\t\t\t),\n\t\t},\n\t})\n\n\treturn canvas.Render(\"Jauge\")\n}\n"},{"name":"gauge_test.gno","body":"package gauge\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestRender(t *testing.T) {\n\ttests := []struct {\n\t\tname         string\n\t\tvalue        int\n\t\ttotal        int\n\t\tlabel        string\n\t\tcolor        string\n\t\tpercentOnly  bool\n\t\twidth        int\n\t\tcanvasHeight int\n\t\tfontSize     int\n\t\tpaddingH     int\n\t\twantSubstr   string\n\t}{\n\t\t{\n\t\t\tname:         \"fails when value \u003c= 0\",\n\t\t\tvalue:        0,\n\t\t\ttotal:        5,\n\t\t\tlabel:        \"Yes\",\n\t\t\tcolor:        \"#a6da95\",\n\t\t\tpercentOnly:  false,\n\t\t\twidth:        100,\n\t\t\tcanvasHeight: 30,\n\t\t\tfontSize:     16,\n\t\t\tpaddingH:     6,\n\t\t\twantSubstr:   \"jauge fails: value must be greater than 0\",\n\t\t},\n\t\t{\n\t\t\tname:         \"fails when total \u003c= 0\",\n\t\t\tvalue:        5,\n\t\t\ttotal:        0,\n\t\t\tlabel:        \"Yes\",\n\t\t\tcolor:        \"#a6da95\",\n\t\t\tpercentOnly:  false,\n\t\t\twidth:        100,\n\t\t\tcanvasHeight: 30,\n\t\t\tfontSize:     16,\n\t\t\tpaddingH:     6,\n\t\t\twantSubstr:   \"jauge fails: total must be greater than 0\",\n\t\t},\n\t\t{\n\t\t\tname:         \"fails when value \u003e total\",\n\t\t\tvalue:        11,\n\t\t\ttotal:        10,\n\t\t\tlabel:        \"Yes\",\n\t\t\tcolor:        \"#a6da95\",\n\t\t\tpercentOnly:  false,\n\t\t\twidth:        100,\n\t\t\tcanvasHeight: 30,\n\t\t\tfontSize:     16,\n\t\t\tpaddingH:     6,\n\t\t\twantSubstr:   \"jauge fails: value cannot be greater than total\",\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tgot := Render(tt.value, tt.total, tt.label, tt.color, Config{tt.percentOnly, tt.width, tt.canvasHeight, tt.fontSize, tt.paddingH})\n\t\t\tif !strings.Contains(got, tt.wantSubstr) {\n\t\t\t\tt.Errorf(\"Render() = %q, want substring %q\", got, tt.wantSubstr)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/gauge\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"0eUNWf9/Uo8dacEH9qkHUMNW37V2BK+x7qwsIWtpLLajHqItSaO7qzhgfmrraJgoyC7/1gdj31eKn7JKbUx3Bg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"keccak256","path":"gno.land/p/samcrew/keccak256","files":[{"name":"README.md","body":"# `keccak256` \n\nKeccak-256 hash (32‑byte output). Port of Go's x/crypto/sha3 (legacy Keccak variant).\n\n## Usage\n```go\ndata := []byte(\"hello world\")\ndigest := keccak256.Hash(data) // [32]byte\n\nh := keccak256.NewLegacyKeccak256() // streaming\nh.Write([]byte(\"hello \"))\nh.Write([]byte(\"world\"))\nfull := h.Sum(nil) // []byte len 32\n```\n\n## API\n```go\nfunc Hash(data []byte) [32]byte\nfunc NewLegacyKeccak256() hash.Hash\n```\n\n`Hash` = one-shot helper. `NewLegacyKeccak256` = streaming (implements hash.Hash).\n\n## Notes\n- Fixed size: 32 bytes (256 bits).\n- Uses legacy Keccak padding (differs from finalized SHA3-256 padding).\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/samcrew/keccak256\"\ngno = \"0.9\"\n"},{"name":"hashes.gno","body":"package keccak256\n\n// This file provides functions for creating instances of the SHA-3\n// and SHAKE hash functions, as well as utility functions for hashing\n// bytes.\n\nimport (\n\t\"hash\"\n)\n\nconst (\n\tdsbyteSHA3   = 0b00000110\n\tdsbyteKeccak = 0b00000001\n\tdsbyteShake  = 0b00011111\n\tdsbyteCShake = 0b00000100\n\n\t// rateK[c] is the rate in bytes for Keccak[c] where c is the capacity in\n\t// bits. Given the sponge size is 1600 bits, the rate is 1600 - c bits.\n\trateK256  = (1600 - 256) / 8\n\trateK448  = (1600 - 448) / 8\n\trateK512  = (1600 - 512) / 8\n\trateK768  = (1600 - 768) / 8\n\trateK1024 = (1600 - 1024) / 8\n)\n\n// NewLegacyKeccak256 creates a new Keccak-256 hash.\n//\n// Only use this function if you require compatibility with an existing cryptosystem\n// that uses non-standard padding. All other users should use New256 instead.\nfunc NewLegacyKeccak256() hash.Hash {\n\treturn \u0026state{rate: rateK512, outputLen: 32, dsbyte: dsbyteKeccak}\n}\n\nfunc Hash(data []byte) [32]byte {\n\tvar res [32]byte\n\tkeccak256 := NewLegacyKeccak256()\n\tkeccak256.Write(data)\n\tcopy(res[:], keccak256.Sum(nil))\n\n\treturn res\n}\n"},{"name":"keccakf.gno","body":"package keccak256\n\nimport \"math/bits\"\n\n// rc stores the round constants for use in the ι step.\nvar rc = [24]uint64{\n\t0x0000000000000001,\n\t0x0000000000008082,\n\t0x800000000000808A,\n\t0x8000000080008000,\n\t0x000000000000808B,\n\t0x0000000080000001,\n\t0x8000000080008081,\n\t0x8000000000008009,\n\t0x000000000000008A,\n\t0x0000000000000088,\n\t0x0000000080008009,\n\t0x000000008000000A,\n\t0x000000008000808B,\n\t0x800000000000008B,\n\t0x8000000000008089,\n\t0x8000000000008003,\n\t0x8000000000008002,\n\t0x8000000000000080,\n\t0x000000000000800A,\n\t0x800000008000000A,\n\t0x8000000080008081,\n\t0x8000000000008080,\n\t0x0000000080000001,\n\t0x8000000080008008,\n}\n\n// keccakF1600 applies the Keccak permutation to a 1600b-wide\n// state represented as a slice of 25 uint64s.\nfunc keccakF1600(a *[25]uint64) {\n\t// Implementation translated from Keccak-inplace.c\n\t// in the keccak reference code.\n\tvar t, bc0, bc1, bc2, bc3, bc4, d0, d1, d2, d3, d4 uint64\n\n\tfor i := 0; i \u003c 24; i += 4 {\n\t\t// Combines the 5 steps in each round into 2 steps.\n\t\t// Unrolls 4 rounds per loop and spreads some steps across rounds.\n\n\t\t// Round 1\n\t\tbc0 = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]\n\t\tbc1 = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]\n\t\tbc2 = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]\n\t\tbc3 = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]\n\t\tbc4 = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]\n\t\td0 = bc4 ^ (bc1\u003c\u003c1 | bc1\u003e\u003e63)\n\t\td1 = bc0 ^ (bc2\u003c\u003c1 | bc2\u003e\u003e63)\n\t\td2 = bc1 ^ (bc3\u003c\u003c1 | bc3\u003e\u003e63)\n\t\td3 = bc2 ^ (bc4\u003c\u003c1 | bc4\u003e\u003e63)\n\t\td4 = bc3 ^ (bc0\u003c\u003c1 | bc0\u003e\u003e63)\n\n\t\tbc0 = a[0] ^ d0\n\t\tt = a[6] ^ d1\n\t\tbc1 = bits.RotateLeft64(t, 44)\n\t\tt = a[12] ^ d2\n\t\tbc2 = bits.RotateLeft64(t, 43)\n\t\tt = a[18] ^ d3\n\t\tbc3 = bits.RotateLeft64(t, 21)\n\t\tt = a[24] ^ d4\n\t\tbc4 = bits.RotateLeft64(t, 14)\n\t\ta[0] = bc0 ^ (bc2 \u0026^ bc1) ^ rc[i]\n\t\ta[6] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[12] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[18] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[24] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[10] ^ d0\n\t\tbc2 = bits.RotateLeft64(t, 3)\n\t\tt = a[16] ^ d1\n\t\tbc3 = bits.RotateLeft64(t, 45)\n\t\tt = a[22] ^ d2\n\t\tbc4 = bits.RotateLeft64(t, 61)\n\t\tt = a[3] ^ d3\n\t\tbc0 = bits.RotateLeft64(t, 28)\n\t\tt = a[9] ^ d4\n\t\tbc1 = bits.RotateLeft64(t, 20)\n\t\ta[10] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[16] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[22] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[3] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[9] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[20] ^ d0\n\t\tbc4 = bits.RotateLeft64(t, 18)\n\t\tt = a[1] ^ d1\n\t\tbc0 = bits.RotateLeft64(t, 1)\n\t\tt = a[7] ^ d2\n\t\tbc1 = bits.RotateLeft64(t, 6)\n\t\tt = a[13] ^ d3\n\t\tbc2 = bits.RotateLeft64(t, 25)\n\t\tt = a[19] ^ d4\n\t\tbc3 = bits.RotateLeft64(t, 8)\n\t\ta[20] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[1] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[7] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[13] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[19] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[5] ^ d0\n\t\tbc1 = bits.RotateLeft64(t, 36)\n\t\tt = a[11] ^ d1\n\t\tbc2 = bits.RotateLeft64(t, 10)\n\t\tt = a[17] ^ d2\n\t\tbc3 = bits.RotateLeft64(t, 15)\n\t\tt = a[23] ^ d3\n\t\tbc4 = bits.RotateLeft64(t, 56)\n\t\tt = a[4] ^ d4\n\t\tbc0 = bits.RotateLeft64(t, 27)\n\t\ta[5] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[11] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[17] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[23] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[4] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[15] ^ d0\n\t\tbc3 = bits.RotateLeft64(t, 41)\n\t\tt = a[21] ^ d1\n\t\tbc4 = bits.RotateLeft64(t, 2)\n\t\tt = a[2] ^ d2\n\t\tbc0 = bits.RotateLeft64(t, 62)\n\t\tt = a[8] ^ d3\n\t\tbc1 = bits.RotateLeft64(t, 55)\n\t\tt = a[14] ^ d4\n\t\tbc2 = bits.RotateLeft64(t, 39)\n\t\ta[15] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[21] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[2] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[8] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[14] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\t// Round 2\n\t\tbc0 = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]\n\t\tbc1 = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]\n\t\tbc2 = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]\n\t\tbc3 = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]\n\t\tbc4 = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]\n\t\td0 = bc4 ^ (bc1\u003c\u003c1 | bc1\u003e\u003e63)\n\t\td1 = bc0 ^ (bc2\u003c\u003c1 | bc2\u003e\u003e63)\n\t\td2 = bc1 ^ (bc3\u003c\u003c1 | bc3\u003e\u003e63)\n\t\td3 = bc2 ^ (bc4\u003c\u003c1 | bc4\u003e\u003e63)\n\t\td4 = bc3 ^ (bc0\u003c\u003c1 | bc0\u003e\u003e63)\n\n\t\tbc0 = a[0] ^ d0\n\t\tt = a[16] ^ d1\n\t\tbc1 = bits.RotateLeft64(t, 44)\n\t\tt = a[7] ^ d2\n\t\tbc2 = bits.RotateLeft64(t, 43)\n\t\tt = a[23] ^ d3\n\t\tbc3 = bits.RotateLeft64(t, 21)\n\t\tt = a[14] ^ d4\n\t\tbc4 = bits.RotateLeft64(t, 14)\n\t\ta[0] = bc0 ^ (bc2 \u0026^ bc1) ^ rc[i+1]\n\t\ta[16] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[7] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[23] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[14] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[20] ^ d0\n\t\tbc2 = bits.RotateLeft64(t, 3)\n\t\tt = a[11] ^ d1\n\t\tbc3 = bits.RotateLeft64(t, 45)\n\t\tt = a[2] ^ d2\n\t\tbc4 = bits.RotateLeft64(t, 61)\n\t\tt = a[18] ^ d3\n\t\tbc0 = bits.RotateLeft64(t, 28)\n\t\tt = a[9] ^ d4\n\t\tbc1 = bits.RotateLeft64(t, 20)\n\t\ta[20] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[11] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[2] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[18] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[9] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[15] ^ d0\n\t\tbc4 = bits.RotateLeft64(t, 18)\n\t\tt = a[6] ^ d1\n\t\tbc0 = bits.RotateLeft64(t, 1)\n\t\tt = a[22] ^ d2\n\t\tbc1 = bits.RotateLeft64(t, 6)\n\t\tt = a[13] ^ d3\n\t\tbc2 = bits.RotateLeft64(t, 25)\n\t\tt = a[4] ^ d4\n\t\tbc3 = bits.RotateLeft64(t, 8)\n\t\ta[15] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[6] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[22] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[13] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[4] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[10] ^ d0\n\t\tbc1 = bits.RotateLeft64(t, 36)\n\t\tt = a[1] ^ d1\n\t\tbc2 = bits.RotateLeft64(t, 10)\n\t\tt = a[17] ^ d2\n\t\tbc3 = bits.RotateLeft64(t, 15)\n\t\tt = a[8] ^ d3\n\t\tbc4 = bits.RotateLeft64(t, 56)\n\t\tt = a[24] ^ d4\n\t\tbc0 = bits.RotateLeft64(t, 27)\n\t\ta[10] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[1] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[17] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[8] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[24] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[5] ^ d0\n\t\tbc3 = bits.RotateLeft64(t, 41)\n\t\tt = a[21] ^ d1\n\t\tbc4 = bits.RotateLeft64(t, 2)\n\t\tt = a[12] ^ d2\n\t\tbc0 = bits.RotateLeft64(t, 62)\n\t\tt = a[3] ^ d3\n\t\tbc1 = bits.RotateLeft64(t, 55)\n\t\tt = a[19] ^ d4\n\t\tbc2 = bits.RotateLeft64(t, 39)\n\t\ta[5] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[21] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[12] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[3] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[19] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\t// Round 3\n\t\tbc0 = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]\n\t\tbc1 = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]\n\t\tbc2 = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]\n\t\tbc3 = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]\n\t\tbc4 = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]\n\t\td0 = bc4 ^ (bc1\u003c\u003c1 | bc1\u003e\u003e63)\n\t\td1 = bc0 ^ (bc2\u003c\u003c1 | bc2\u003e\u003e63)\n\t\td2 = bc1 ^ (bc3\u003c\u003c1 | bc3\u003e\u003e63)\n\t\td3 = bc2 ^ (bc4\u003c\u003c1 | bc4\u003e\u003e63)\n\t\td4 = bc3 ^ (bc0\u003c\u003c1 | bc0\u003e\u003e63)\n\n\t\tbc0 = a[0] ^ d0\n\t\tt = a[11] ^ d1\n\t\tbc1 = bits.RotateLeft64(t, 44)\n\t\tt = a[22] ^ d2\n\t\tbc2 = bits.RotateLeft64(t, 43)\n\t\tt = a[8] ^ d3\n\t\tbc3 = bits.RotateLeft64(t, 21)\n\t\tt = a[19] ^ d4\n\t\tbc4 = bits.RotateLeft64(t, 14)\n\t\ta[0] = bc0 ^ (bc2 \u0026^ bc1) ^ rc[i+2]\n\t\ta[11] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[22] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[8] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[19] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[15] ^ d0\n\t\tbc2 = bits.RotateLeft64(t, 3)\n\t\tt = a[1] ^ d1\n\t\tbc3 = bits.RotateLeft64(t, 45)\n\t\tt = a[12] ^ d2\n\t\tbc4 = bits.RotateLeft64(t, 61)\n\t\tt = a[23] ^ d3\n\t\tbc0 = bits.RotateLeft64(t, 28)\n\t\tt = a[9] ^ d4\n\t\tbc1 = bits.RotateLeft64(t, 20)\n\t\ta[15] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[1] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[12] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[23] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[9] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[5] ^ d0\n\t\tbc4 = bits.RotateLeft64(t, 18)\n\t\tt = a[16] ^ d1\n\t\tbc0 = bits.RotateLeft64(t, 1)\n\t\tt = a[2] ^ d2\n\t\tbc1 = bits.RotateLeft64(t, 6)\n\t\tt = a[13] ^ d3\n\t\tbc2 = bits.RotateLeft64(t, 25)\n\t\tt = a[24] ^ d4\n\t\tbc3 = bits.RotateLeft64(t, 8)\n\t\ta[5] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[16] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[2] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[13] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[24] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[20] ^ d0\n\t\tbc1 = bits.RotateLeft64(t, 36)\n\t\tt = a[6] ^ d1\n\t\tbc2 = bits.RotateLeft64(t, 10)\n\t\tt = a[17] ^ d2\n\t\tbc3 = bits.RotateLeft64(t, 15)\n\t\tt = a[3] ^ d3\n\t\tbc4 = bits.RotateLeft64(t, 56)\n\t\tt = a[14] ^ d4\n\t\tbc0 = bits.RotateLeft64(t, 27)\n\t\ta[20] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[6] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[17] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[3] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[14] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[10] ^ d0\n\t\tbc3 = bits.RotateLeft64(t, 41)\n\t\tt = a[21] ^ d1\n\t\tbc4 = bits.RotateLeft64(t, 2)\n\t\tt = a[7] ^ d2\n\t\tbc0 = bits.RotateLeft64(t, 62)\n\t\tt = a[18] ^ d3\n\t\tbc1 = bits.RotateLeft64(t, 55)\n\t\tt = a[4] ^ d4\n\t\tbc2 = bits.RotateLeft64(t, 39)\n\t\ta[10] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[21] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[7] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[18] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[4] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\t// Round 4\n\t\tbc0 = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]\n\t\tbc1 = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]\n\t\tbc2 = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]\n\t\tbc3 = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]\n\t\tbc4 = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]\n\t\td0 = bc4 ^ (bc1\u003c\u003c1 | bc1\u003e\u003e63)\n\t\td1 = bc0 ^ (bc2\u003c\u003c1 | bc2\u003e\u003e63)\n\t\td2 = bc1 ^ (bc3\u003c\u003c1 | bc3\u003e\u003e63)\n\t\td3 = bc2 ^ (bc4\u003c\u003c1 | bc4\u003e\u003e63)\n\t\td4 = bc3 ^ (bc0\u003c\u003c1 | bc0\u003e\u003e63)\n\n\t\tbc0 = a[0] ^ d0\n\t\tt = a[1] ^ d1\n\t\tbc1 = bits.RotateLeft64(t, 44)\n\t\tt = a[2] ^ d2\n\t\tbc2 = bits.RotateLeft64(t, 43)\n\t\tt = a[3] ^ d3\n\t\tbc3 = bits.RotateLeft64(t, 21)\n\t\tt = a[4] ^ d4\n\t\tbc4 = bits.RotateLeft64(t, 14)\n\t\ta[0] = bc0 ^ (bc2 \u0026^ bc1) ^ rc[i+3]\n\t\ta[1] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[2] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[3] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[4] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[5] ^ d0\n\t\tbc2 = bits.RotateLeft64(t, 3)\n\t\tt = a[6] ^ d1\n\t\tbc3 = bits.RotateLeft64(t, 45)\n\t\tt = a[7] ^ d2\n\t\tbc4 = bits.RotateLeft64(t, 61)\n\t\tt = a[8] ^ d3\n\t\tbc0 = bits.RotateLeft64(t, 28)\n\t\tt = a[9] ^ d4\n\t\tbc1 = bits.RotateLeft64(t, 20)\n\t\ta[5] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[6] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[7] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[8] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[9] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[10] ^ d0\n\t\tbc4 = bits.RotateLeft64(t, 18)\n\t\tt = a[11] ^ d1\n\t\tbc0 = bits.RotateLeft64(t, 1)\n\t\tt = a[12] ^ d2\n\t\tbc1 = bits.RotateLeft64(t, 6)\n\t\tt = a[13] ^ d3\n\t\tbc2 = bits.RotateLeft64(t, 25)\n\t\tt = a[14] ^ d4\n\t\tbc3 = bits.RotateLeft64(t, 8)\n\t\ta[10] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[11] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[12] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[13] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[14] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[15] ^ d0\n\t\tbc1 = bits.RotateLeft64(t, 36)\n\t\tt = a[16] ^ d1\n\t\tbc2 = bits.RotateLeft64(t, 10)\n\t\tt = a[17] ^ d2\n\t\tbc3 = bits.RotateLeft64(t, 15)\n\t\tt = a[18] ^ d3\n\t\tbc4 = bits.RotateLeft64(t, 56)\n\t\tt = a[19] ^ d4\n\t\tbc0 = bits.RotateLeft64(t, 27)\n\t\ta[15] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[16] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[17] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[18] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[19] = bc4 ^ (bc1 \u0026^ bc0)\n\n\t\tt = a[20] ^ d0\n\t\tbc3 = bits.RotateLeft64(t, 41)\n\t\tt = a[21] ^ d1\n\t\tbc4 = bits.RotateLeft64(t, 2)\n\t\tt = a[22] ^ d2\n\t\tbc0 = bits.RotateLeft64(t, 62)\n\t\tt = a[23] ^ d3\n\t\tbc1 = bits.RotateLeft64(t, 55)\n\t\tt = a[24] ^ d4\n\t\tbc2 = bits.RotateLeft64(t, 39)\n\t\ta[20] = bc0 ^ (bc2 \u0026^ bc1)\n\t\ta[21] = bc1 ^ (bc3 \u0026^ bc2)\n\t\ta[22] = bc2 ^ (bc4 \u0026^ bc3)\n\t\ta[23] = bc3 ^ (bc0 \u0026^ bc4)\n\t\ta[24] = bc4 ^ (bc1 \u0026^ bc0)\n\t}\n}\n"},{"name":"sha3.gno","body":"// Copyright 2014 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\n// Package keccak256 was ported from x/crypto/sha3\n// the original source can be found here:\n// https://cs.opensource.google/go/x/crypto/+/master:sha3/sha3.go\npackage keccak256\n\nimport (\n\t\"crypto/subtle\"\n\t\"encoding/binary\"\n\t\"errors\"\n)\n\n// spongeDirection indicates the direction bytes are flowing through the sponge.\ntype spongeDirection int\n\nconst (\n\t// spongeAbsorbing indicates that the sponge is absorbing input.\n\tspongeAbsorbing spongeDirection = iota\n\t// spongeSqueezing indicates that the sponge is being squeezed.\n\tspongeSqueezing\n)\n\ntype state struct {\n\ta [1600 / 8]byte // main state of the hash\n\n\t// a[n:rate] is the buffer. If absorbing, it's the remaining space to XOR\n\t// into before running the permutation. If squeezing, it's the remaining\n\t// output to produce before running the permutation.\n\tn, rate int\n\n\t// dsbyte contains the \"domain separation\" bits and the first bit of\n\t// the padding. Sections 6.1 and 6.2 of [1] separate the outputs of the\n\t// SHA-3 and SHAKE functions by appending bitstrings to the message.\n\t// Using a little-endian bit-ordering convention, these are \"01\" for SHA-3\n\t// and \"1111\" for SHAKE, or 00000010b and 00001111b, respectively. Then the\n\t// padding rule from section 5.1 is applied to pad the message to a multiple\n\t// of the rate, which involves adding a \"1\" bit, zero or more \"0\" bits, and\n\t// a final \"1\" bit. We merge the first \"1\" bit from the padding into dsbyte,\n\t// giving 00000110b (0x06) and 00011111b (0x1f).\n\t// [1] http://csrc.nist.gov/publications/drafts/fips-202/fips_202_draft.pdf\n\t//     \"Draft FIPS 202: SHA-3 Standard: Permutation-Based Hash and\n\t//      Extendable-Output Functions (May 2014)\"\n\tdsbyte byte\n\n\toutputLen int             // the default output size in bytes\n\tstate     spongeDirection // whether the sponge is absorbing or squeezing\n}\n\n// BlockSize returns the rate of sponge underlying this hash function.\nfunc (d *state) BlockSize() int { return d.rate }\n\n// Size returns the output size of the hash function in bytes.\nfunc (d *state) Size() int { return d.outputLen }\n\n// Reset clears the internal state by zeroing the sponge state and\n// the buffer indexes, and setting Sponge.state to absorbing.\nfunc (d *state) Reset() {\n\t// Zero the permutation's state.\n\tfor i := range d.a {\n\t\td.a[i] = 0\n\t}\n\td.state = spongeAbsorbing\n\td.n = 0\n}\n\nfunc (d *state) clone() *state {\n\tret := *d\n\treturn \u0026ret\n}\n\n// permute applies the KeccakF-1600 permutation without unsafe.\nfunc (d *state) permute() {\n\tvar a [25]uint64\n\n\t// Always load in little-endian order\n\tfor i := range a {\n\t\ta[i] = binary.LittleEndian.Uint64(d.a[i*8:])\n\t}\n\n\tkeccakF1600(\u0026a)\n\td.n = 0\n\n\t// Always store in little-endian order\n\tfor i := range a {\n\t\tbinary.LittleEndian.PutUint64(d.a[i*8:], a[i])\n\t}\n}\n\n// pads appends the domain separation bits in dsbyte, applies\n// the multi-bitrate 10..1 padding rule, and permutes the state.\nfunc (d *state) padAndPermute() {\n\t// Pad with this instance's domain-separator bits. We know that there's\n\t// at least one byte of space in the sponge because, if it were full,\n\t// permute would have been called to empty it. dsbyte also contains the\n\t// first one bit for the padding. See the comment in the state struct.\n\td.a[d.n] ^= d.dsbyte\n\t// This adds the final one bit for the padding. Because of the way that\n\t// bits are numbered from the LSB upwards, the final bit is the MSB of\n\t// the last byte.\n\td.a[d.rate-1] ^= 0x80\n\t// Apply the permutation\n\td.permute()\n\td.state = spongeSqueezing\n}\n\n// Write absorbs more data into the hash's state. It panics if any\n// output has already been read.\nfunc (d *state) Write(p []byte) (n int, err error) {\n\tif d.state != spongeAbsorbing {\n\t\tpanic(\"sha3: Write after Read\")\n\t}\n\n\tn = len(p)\n\tfor len(p) \u003e 0 {\n\t\tx := subtle.XORBytesUnsafe(d.a[d.n:d.rate], d.a[d.n:d.rate], p)\n\t\td.n += x\n\t\tp = p[x:]\n\n\t\t// If the sponge is full, apply the permutation.\n\t\tif d.n == d.rate {\n\t\t\td.permute()\n\t\t}\n\t}\n\n\treturn\n}\n\n// Read squeezes an arbitrary number of bytes from the sponge.\nfunc (d *state) Read(out []byte) (n int, err error) {\n\t// If we're still absorbing, pad and apply the permutation.\n\tif d.state == spongeAbsorbing {\n\t\td.padAndPermute()\n\t}\n\n\tn = len(out)\n\n\t// Now, do the squeezing.\n\tfor len(out) \u003e 0 {\n\t\t// Apply the permutation if we've squeezed the sponge dry.\n\t\tif d.n == d.rate {\n\t\t\td.permute()\n\t\t}\n\n\t\tx := copy(out, d.a[d.n:d.rate])\n\t\td.n += x\n\t\tout = out[x:]\n\t}\n\n\treturn\n}\n\n// Sum applies padding to the hash state and then squeezes out the desired\n// number of output bytes. It panics if any output has already been read.\nfunc (d *state) Sum(in []byte) []byte {\n\tif d.state != spongeAbsorbing {\n\t\tpanic(\"sha3: Sum after Read\")\n\t}\n\n\t// Make a copy of the original hash so that caller can keep writing\n\t// and summing.\n\tdup := d.clone()\n\thash := make([]byte, dup.outputLen, 64) // explicit cap to allow stack allocation\n\tdup.Read(hash)\n\treturn append(in, hash...)\n}\n\nconst (\n\tmagicSHA3   = \"sha\\x08\"\n\tmagicShake  = \"sha\\x09\"\n\tmagicCShake = \"sha\\x0a\"\n\tmagicKeccak = \"sha\\x0b\"\n\t// magic || rate || main state || n || sponge direction\n\tmarshaledSize = len(magicSHA3) + 1 + 200 + 1 + 1\n)\n\nfunc (d *state) MarshalBinary() ([]byte, error) {\n\treturn d.AppendBinary(make([]byte, 0, marshaledSize))\n}\n\nfunc (d *state) AppendBinary(b []byte) ([]byte, error) {\n\tswitch d.dsbyte {\n\tcase dsbyteSHA3:\n\t\tb = append(b, magicSHA3...)\n\tcase dsbyteShake:\n\t\tb = append(b, magicShake...)\n\tcase dsbyteCShake:\n\t\tb = append(b, magicCShake...)\n\tcase dsbyteKeccak:\n\t\tb = append(b, magicKeccak...)\n\tdefault:\n\t\tpanic(\"unknown dsbyte\")\n\t}\n\t// rate is at most 168, and n is at most rate.\n\tb = append(b, byte(d.rate))\n\tb = append(b, d.a[:]...)\n\tb = append(b, byte(d.n), byte(d.state))\n\treturn b, nil\n}\n\nfunc (d *state) UnmarshalBinary(b []byte) error {\n\tif len(b) != marshaledSize {\n\t\treturn errors.New(\"sha3: invalid hash state\")\n\t}\n\n\tmagic := string(b[:len(magicSHA3)])\n\tb = b[len(magicSHA3):]\n\tswitch {\n\tcase magic == magicSHA3 \u0026\u0026 d.dsbyte == dsbyteSHA3:\n\tcase magic == magicShake \u0026\u0026 d.dsbyte == dsbyteShake:\n\tcase magic == magicCShake \u0026\u0026 d.dsbyte == dsbyteCShake:\n\tcase magic == magicKeccak \u0026\u0026 d.dsbyte == dsbyteKeccak:\n\tdefault:\n\t\treturn errors.New(\"sha3: invalid hash state identifier\")\n\t}\n\n\trate := int(b[0])\n\tb = b[1:]\n\tif rate != d.rate {\n\t\treturn errors.New(\"sha3: invalid hash state function\")\n\t}\n\n\tcopy(d.a[:], b)\n\tb = b[len(d.a):]\n\n\tn, state := int(b[0]), spongeDirection(b[1])\n\tif n \u003e d.rate {\n\t\treturn errors.New(\"sha3: invalid hash state\")\n\t}\n\td.n = n\n\tif state != spongeAbsorbing \u0026\u0026 state != spongeSqueezing {\n\t\treturn errors.New(\"sha3: invalid hash state\")\n\t}\n\td.state = state\n\n\treturn nil\n}\n"},{"name":"sha3_test.gno","body":"// Copyright 2014 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage keccak256\n\n// Tests include all the ShortMsgKATs provided by the Keccak team at\n// https://github.com/gvanas/KeccakCodePackage\n//\n// They only include the zero-bit case of the bitwise testvectors\n// published by NIST in the draft of FIPS-202.\n\nimport (\n\t\"bytes\"\n\t\"encoding/hex\"\n\t\"hash\"\n\t\"math/rand\"\n\t\"testing\"\n)\n\nconst (\n\ttestString  = \"brekeccakkeccak koax koax\"\n\tkatFilename = \"testdata/keccakKats.json.deflate\"\n)\n\n// testDigests contains functions returning hash.Hash instances\n// with output-length equal to the KAT length for SHA-3, Keccak\n// and SHAKE instances.\nvar testDigests = map[string]func() hash.Hash{\n\t\"Keccak-256\": NewLegacyKeccak256,\n}\n\n// decodeHex converts a hex-encoded string into a raw byte string.\nfunc decodeHex(s string) []byte {\n\tb, err := hex.DecodeString(s)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn b\n}\n\n// structs used to marshal JSON test-cases.\ntype KeccakKats struct {\n\tKats map[string][]struct {\n\t\tDigest  string `json:\"digest\"`\n\t\tLength  int64  `json:\"length\"`\n\t\tMessage string `json:\"message\"`\n\n\t\t// Defined only for cSHAKE\n\t\tN string `json:\"N\"`\n\t\tS string `json:\"S\"`\n\t}\n}\n\n// TestKeccakKats tests the SHA-3 and Shake implementations against all the\n// ShortMsgKATs from https://github.com/gvanas/KeccakCodePackage\n// (The testvectors are stored in keccakKats.json.deflate due to their length.)\n//XXX Do not use \"compress/flate\"\n/*func TestKeccakKats(t *testing.T) {\n\t// Read the KATs.\n\tdeflated, err := os.Open(katFilename)\n\tif err != nil {\n\t\tt.Errorf(\"error opening %s: %s\", katFilename, err)\n\t}\n\tfile := flate.NewReader(deflated)\n\tdec := json.NewDecoder(file)\n\tvar katSet KeccakKats\n\terr = dec.Decode(\u0026katSet)\n\tif err != nil {\n\t\tt.Errorf(\"error decoding KATs: %s\", err)\n\t}\n\n\tfor algo, function := range testDigests {\n\t\td := function()\n\t\tfor _, kat := range katSet.Kats[algo] {\n\t\t\td.Reset()\n\t\t\tin, err := hex.DecodeString(kat.Message)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"error decoding KAT: %s\", err)\n\t\t\t}\n\t\t\td.Write(in[:kat.Length/8])\n\t\t\tgot := strings.ToUpper(hex.EncodeToString(d.Sum(nil)))\n\t\t\tif got != kat.Digest {\n\t\t\t\tt.Errorf(\"function=%s, length=%d\\nmessage:\\n %s\\ngot:\\n  %s\\nwanted:\\n %s\",\n\t\t\t\t\talgo, kat.Length, kat.Message, got, kat.Digest)\n\t\t\t\tt.Logf(\"wanted %+v\", kat)\n\t\t\t\tt.FailNow()\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t}\n\n\tfor algo, v := range testShakes {\n\t\tfor _, kat := range katSet.Kats[algo] {\n\t\t\tN, err := hex.DecodeString(kat.N)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"error decoding KAT: %s\", err)\n\t\t\t}\n\n\t\t\tS, err := hex.DecodeString(kat.S)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"error decoding KAT: %s\", err)\n\t\t\t}\n\t\t\td := v.constructor(N, S)\n\t\t\tin, err := hex.DecodeString(kat.Message)\n\t\t\tif err != nil {\n\t\t\t\tt.Errorf(\"error decoding KAT: %s\", err)\n\t\t\t}\n\n\t\t\td.Write(in[:kat.Length/8])\n\t\t\tout := make([]byte, len(kat.Digest)/2)\n\t\t\td.Read(out)\n\t\t\tgot := strings.ToUpper(hex.EncodeToString(out))\n\t\t\tif got != kat.Digest {\n\t\t\t\tt.Errorf(\"function=%s, length=%d N:%s\\n S:%s\\nmessage:\\n %s \\ngot:\\n  %s\\nwanted:\\n %s\",\n\t\t\t\t\talgo, kat.Length, kat.N, kat.S, kat.Message, got, kat.Digest)\n\t\t\t\tt.Logf(\"wanted %+v\", kat)\n\t\t\t\tt.FailNow()\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t}\n}*/\n\n// TestKeccak does a basic test of the non-standardized Keccak hash functions.\nfunc TestKeccak(t *testing.T) {\n\ttests := []struct {\n\t\tfn   func() hash.Hash\n\t\tdata []byte\n\t\twant string\n\t}{\n\n\t\t{\n\t\t\tNewLegacyKeccak256,\n\t\t\t[]byte(\"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text\"),\n\t\t\t\"39670cbdfbfc25519e9834899e13569e5f5802b77df3d8259961f713ad09745d\",\n\t\t},\n\t\t{\n\t\t\tNewLegacyKeccak256,\n\t\t\t[]byte(\"abc\"),\n\t\t\t\"4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45\",\n\t\t},\n\t\t{\n\t\t\tNewLegacyKeccak256,\n\t\t\t[]byte(\"916889520661998333947422699691\"),\n\t\t\t\"c5b223cc231dd56389ca19435758fba38e79f3c461d989d818ceb96595d310d1\",\n\t\t},\n\t\t{\n\t\t\tNewLegacyKeccak256,\n\t\t\t[]byte(\"6R1Q2KvQNGxI4OtUGDS1rSCbJXkp1H\"),\n\t\t\t\"8e94c6d49ca23597ee1d4a317b17a85ae38f6f3241e11c3ace4abe756287839a\",\n\t\t},\n\t\t{\n\t\t\tNewLegacyKeccak256,\n\t\t\t[]byte(\"SOMERANDOMSTRING\"),\n\t\t\t\"59f47b8dc1ffdfb716d2fcb313c070040f6049bd23c321c6cb8adb3f79eb720f\",\n\t\t},\n\t\t{\n\t\t\tNewLegacyKeccak256,\n\t\t\t[]byte(\"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC\"),\n\t\t\t\"da5293ad50619d7fdac8b4c8dc2f80e0cd40b6571be14740229d0a1d6e2fc232\",\n\t\t},\n\t\t{\n\t\t\tNewLegacyKeccak256,\n\t\t\t[]byte(\"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.\"),\n\t\t\t\"a84506d23b7246dc18805c8bf60316fb6f677934a546ee8d8650495929f21eef\",\n\t\t},\n\t}\n\n\tfor _, u := range tests {\n\t\th := u.fn()\n\t\th.Write(u.data)\n\t\tgot := h.Sum(nil)\n\t\twant := decodeHex(u.want)\n\t\tif !bytes.Equal(got, want) {\n\t\t\tt.Errorf(\"unexpected hash for size %d: got '%x' want '%s'\", h.Size()*8, got, u.want)\n\t\t}\n\t}\n}\n\nfunc TestHash(t *testing.T) {\n\ttests := []struct {\n\t\tdata []byte\n\t\twant string\n\t}{\n\n\t\t{\n\n\t\t\t[]byte(\"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text\"),\n\t\t\t\"39670cbdfbfc25519e9834899e13569e5f5802b77df3d8259961f713ad09745d\",\n\t\t},\n\t\t{\n\n\t\t\t[]byte(\"abc\"),\n\t\t\t\"4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45\",\n\t\t},\n\t\t{\n\n\t\t\t[]byte(\"916889520661998333947422699691\"),\n\t\t\t\"c5b223cc231dd56389ca19435758fba38e79f3c461d989d818ceb96595d310d1\",\n\t\t},\n\t\t{\n\n\t\t\t[]byte(\"6R1Q2KvQNGxI4OtUGDS1rSCbJXkp1H\"),\n\t\t\t\"8e94c6d49ca23597ee1d4a317b17a85ae38f6f3241e11c3ace4abe756287839a\",\n\t\t},\n\t\t{\n\n\t\t\t[]byte(\"SOMERANDOMSTRING\"),\n\t\t\t\"59f47b8dc1ffdfb716d2fcb313c070040f6049bd23c321c6cb8adb3f79eb720f\",\n\t\t},\n\t\t{\n\n\t\t\t[]byte(\"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC\"),\n\t\t\t\"da5293ad50619d7fdac8b4c8dc2f80e0cd40b6571be14740229d0a1d6e2fc232\",\n\t\t},\n\t\t{\n\n\t\t\t[]byte(\"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.\"),\n\t\t\t\"a84506d23b7246dc18805c8bf60316fb6f677934a546ee8d8650495929f21eef\",\n\t\t},\n\t}\n\n\tfor _, u := range tests {\n\t\tgot := Hash(u.data)\n\t\twant := decodeHex(u.want)\n\t\tif !bytes.Equal(got[:], want) {\n\t\t\tt.Errorf(\"unexpected hash: got '%x' want '%s'\", got, u.want)\n\t\t}\n\t}\n}\n\n// sequentialBytes produces a buffer of size consecutive bytes 0x00, 0x01, ..., used for testing.\n//\n// The alignment of each slice is intentionally randomized to detect alignment\n// issues in the implementation. See https://golang.org/issue/37644.\n// Ideally, the compiler should fuzz the alignment itself.\n// (See https://golang.org/issue/35128.)\nfunc sequentialBytes(size int) []byte {\n\talignmentOffset := rand.IntN(8)\n\tresult := make([]byte, size+alignmentOffset)[alignmentOffset:]\n\tfor i := range result {\n\t\tresult[i] = byte(i)\n\t}\n\treturn result\n}\n\n/*func TestMarshalUnmarshal(t *testing.T) {\n\tt.Run(\"Keccak-256\", func(t *testing.T) { testMarshalUnmarshal(t, NewLegacyKeccak256()) })\n}*/\n\n// TODO(filippo): move this to crypto/internal/cryptotest.\n//XX: remove rand.Read\n/*func testMarshalUnmarshal(t *testing.T, h hash.Hash) {\n\tbuf := make([]byte, 200)\n\trand.Read(buf)\n\tn := rand.IntN(200)\n\th.Write(buf)\n\twant := h.Sum(nil)\n\th.Reset()\n\th.Write(buf[:n])\n\tb, err := h.(encoding.BinaryMarshaler).MarshalBinary()\n\tif err != nil {\n\t\tt.Errorf(\"MarshalBinary: %v\", err)\n\t}\n\th.Write(bytes.Repeat([]byte{0}, 200))\n\tif err := h.(encoding.BinaryUnmarshaler).UnmarshalBinary(b); err != nil {\n\t\tt.Errorf(\"UnmarshalBinary: %v\", err)\n\t}\n\th.Write(buf[n:])\n\tgot := h.Sum(nil)\n\tif !bytes.Equal(got, want) {\n\t\tt.Errorf(\"got %x, want %x\", got, want)\n\t}\n}*/\n\n// BenchmarkPermutationFunction measures the speed of the permutation function\n// with no input data.\nfunc BenchmarkPermutationFunction(b *testing.B) {\n\tb.SetBytes(int64(200))\n\tvar lanes [25]uint64\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tkeccakF1600(\u0026lanes)\n\t}\n}\n\n// benchmarkHash tests the speed to hash num buffers of buflen each.\nfunc benchmarkHash(b *testing.B, h hash.Hash, size, num int) {\n\tb.StopTimer()\n\th.Reset()\n\tdata := sequentialBytes(size)\n\tb.SetBytes(int64(size * num))\n\tb.StartTimer()\n\n\tvar state []byte\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tfor j := 0; j \u003c num; j++ {\n\t\t\th.Write(data)\n\t\t}\n\t\tstate = h.Sum(state[:0])\n\t}\n\tb.StopTimer()\n\th.Reset()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"XtTAv5R/PeU7oEDa8Ctu5soeW/epfck192waoiKqZEMDO48bUm7yk38066uLnd8ZZszKIWu/sknafa55tN2KBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l","package":{"name":"genesis","path":"gno.land/p/sys/genesis","files":[{"name":"consts.gno","body":"// Package genesis provides a way to store and access dynamic variables that are\n// set during the genesis or block0 initialization phase. This package\n// demonstrates an important aspect of p/ packages in Gno: while they are\n// pure packages, they can have mutable state during initialization (init phase)\n// before becoming read-only.\n//\n// Future improvements:\n// When Gno supports something similar to \"go build -X\", this package could be\n// enhanced to accept variables from the CLI or environment variables (e.g., in\n// gnodev context). For now, it only supports initialization-time operations\n// that make sense at the beginning of the system's lifecycle.\npackage genesis\n\nimport (\n\t\"chain/runtime\"\n\t\"time\"\n)\n\nvar (\n\t// Time is the time of the genesis block.\n\tTime = time.Now()\n\n\t// Height is the height of the genesis block (usually 0).\n\tHeight = runtime.ChainHeight()\n\n\t// Domain is the domain of the chain.\n\tDomain = runtime.ChainDomain()\n\n\t// XXX: TZ\n\t// XXX: Supply = chain.Coins{{\"ugnot\", banker.NewReadonlyBanker().TotalSupply(\"ugnot\")}}\n)\n\n// Uptime returns the uptime of the chain.\nfunc Uptime() time.Duration {\n\treturn time.Since(Time)\n}\n\n// Upheight returns the height of the chain.\nfunc Upheight() int64 {\n\treturn runtime.ChainHeight() - Height\n}\n"},{"name":"consts_test.gno","body":"package genesis_test\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/sys/genesis\"\n)\n\nfunc TestGenesisConstants(t *testing.T) {\n\t// Test that Time is not zero\n\tif genesis.Time.IsZero() {\n\t\tt.Error(\"Time should not be zero\")\n\t}\n\n\t// Test that Height is not zero\n\tif genesis.Height == 0 {\n\t\tt.Error(\"Height should not be zero\")\n\t}\n\n\t// Test that Domain is not empty\n\tif genesis.Domain == \"\" {\n\t\tt.Error(\"Domain should not be empty\")\n\t}\n\n\t// Test that Uptime is not zero\n\tif genesis.Uptime() != 0 {\n\t\tt.Error(\"Uptime should be zero\")\n\t}\n\n\t// Test that Upheight is not zero\n\tif genesis.Upheight() != 0 {\n\t\tt.Error(\"Upheight should be zero\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/sys/genesis\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1r929wt2qplfawe4lvqv9zuwfdcz4vxdun7qh8l\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"DsHTRKTfAYlEOn+0IyHOgsPzvm6kOGvRL2yPKxPHt8MYU/GqZT3o9yjTpoF43x1jKPqnSrn01sGBCGee2lTaBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"seal","path":"gno.land/p/test/seal","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/test/seal\"\ngno = \"0.9\"\n"},{"name":"seal.gno","body":"// Package seal documents whether Gno's sealed-interface pattern\n// (unexported marker method on an interface) provides a security\n// boundary. CONCLUSION: it does NOT. See the filetests for proof.\n//\n// Search terms: sealed interface, embedding bypass, unexported marker\n// method, capability-token leak, isSealed bypass, cur leak via\n// embedding, rlm leak via interface, defense in depth, canonical-impl\n// allowlist, IsCanonical pattern, allowancesender, Teller seal,\n// Banker seal.\n//\n// Filetests (read these before reaching for the sealed-marker\n// pattern as a security control):\n//   - z_seal_violation_filetest.gno — direct redeclaration of the\n//     unexported marker in a foreign package FAILS. (The original\n//     intended property survives.)\n//   - z_seal_embedding_filetest.gno — concrete-type embedding\n//     BYPASSES the seal when the legitimate impl is exported. The\n//     promoted unexported marker retains its origin package's\n//     identity and satisfies the interface.\n//   - z_seal_iface_embedding_filetest.gno — embedding the interface\n//     value itself BYPASSES the seal regardless of whether the\n//     legitimate concrete impl is exported. This is the realistic\n//     Teller/Banker attack: hold a legit Teller obtained from\n//     Token.CallerTeller(), embed it, shadow Transfer to capture rlm.\n//\n// Implication for the cur-leak migration: don't seal as a defense.\n// Use Reader/Writer split, Authority-pattern with private concrete,\n// or canonical-impl allowlist (see migration guide).\npackage seal\n\n// Sealed is intended to be implementable only from within the seal\n// package. The unexported isSealed() method is the seal — any foreign\n// type that declares an `isSealed()` method declares\n// `r/foreign.isSealed`, not `p/test/seal.isSealed`, so structural\n// satisfaction fails.\ntype Sealed interface {\n\tHello() string\n\tisSealed()\n}\n\n// Impl is the legitimate in-package implementation of Sealed.\ntype Impl struct {\n\tName string\n}\n\nfunc NewImpl(name string) *Impl {\n\treturn \u0026Impl{Name: name}\n}\n\nfunc (i *Impl) Hello() string { return \"hello, \" + i.Name }\nfunc (i *Impl) isSealed()     {}\n\n// Accept demonstrates a consumer that takes a Sealed value. In real\n// code this is the surface a cur-leaking interface would expose; the\n// security property of sealing is that Accept can only ever receive a\n// package-internal *Impl, not an attacker-supplied type.\nfunc Accept(s Sealed) string {\n\treturn s.Hello()\n}\n"},{"name":"z_seal_embedding_filetest.gno","body":"// PKGPATH: gno.land/r/test/sealembed\npackage sealembed\n\n// Embedding-bypass test: if a foreign package can satisfy a sealed\n// interface by *embedding* the legitimate in-package implementation,\n// then the seal does not defend against the actual cur-leak threat\n// model. The attacker would obtain a legitimate *seal.Impl, embed it\n// in their own struct, and shadow whatever methods they want to\n// control — including methods that take rlm in a real-world Teller\n// or Banker interface.\n//\n// RESULT: Gno mirrors Go promotion semantics — the test compiles\n// and runs. The seal is BYPASSABLE via embedding when the\n// implementation type is exported. The attacker's Hello() runs\n// while the embedded *seal.Impl supplies the promoted isSealed().\n// See `// Output:` below for the attacker-controlled string.\n//\n// IMPLICATION for the cur-leak migration: sealing alone is NOT\n// sufficient for Teller/Banker if their concrete impls are\n// exported. `treasury.CoinsBanker` is exported (banker_coins.gno);\n// `grc20.fnTeller` is unexported, which blocks concrete embedding\n// but not interface-value embedding — see\n// z_seal_iface_embedding_filetest.gno.\n\nimport (\n\t\"gno.land/p/test/seal\"\n)\n\n// foreignImpl embeds *seal.Impl. The promoted isSealed() method\n// satisfies the seal's package-qualified requirement IF Gno follows\n// Go's promotion rules.\ntype foreignImpl struct {\n\t*seal.Impl\n}\n\n// Shadow Hello() — this is the attack: foreign code controls what\n// the \"sealed\" interface returns, while the seal check is satisfied\n// via the embedded type's promoted isSealed().\nfunc (f *foreignImpl) Hello() string { return \"evil from r/test/sealembed\" }\n\nfunc main() {\n\tlegit := seal.NewImpl(\"legit\")\n\tevil := \u0026foreignImpl{Impl: legit}\n\tvar s seal.Sealed = evil\n\tprintln(s.Hello())\n}\n\n// Output:\n// evil from r/test/sealembed\n"},{"name":"z_seal_iface_embedding_filetest.gno","body":"// PKGPATH: gno.land/r/test/sealifaceembed\npackage sealifaceembed\n\n// Interface-embedding bypass: even when the legitimate concrete impl\n// is unexported (e.g., grc20.fnTeller), a foreign realm can still\n// satisfy a sealed interface by embedding the *interface itself* in\n// a struct. The embedded interface value carries the package-\n// qualified isSealed() forward; the struct's method set includes\n// the promoted methods. Shadowing the methods the attacker cares\n// about (e.g., Transfer that takes rlm) gives them full cur-leak\n// despite the seal.\n//\n// This is the realistic Teller/Banker attack: an attacker realm\n// obtains a legitimate seal.Sealed value from a public factory\n// (e.g., Token.CallerTeller()), stores it in an embedded interface\n// field, and shadows the rlm-typed methods.\n\nimport (\n\t\"gno.land/p/test/seal\"\n)\n\n// evilWrap embeds the sealed interface directly — no concrete type\n// reference needed. If embedded-interface promotion satisfies the\n// seal, this test compiles and the attack surface is real even when\n// the legitimate concrete impl is unexported.\ntype evilWrap struct {\n\tseal.Sealed\n}\n\n// Shadow Hello() with attacker-controlled behavior.\nfunc (e *evilWrap) Hello() string { return \"evil via interface embedding\" }\n\nfunc main() {\n\tlegit := seal.NewImpl(\"legit\")\n\tevil := \u0026evilWrap{Sealed: legit}\n\tvar s seal.Sealed = evil\n\tprintln(s.Hello())\n}\n\n// Output:\n// evil via interface embedding\n"},{"name":"z_seal_rlm_shadow_filetest.gno","body":"// PKGPATH: gno.land/r/test/sealrlmshadow\npackage sealrlmshadow\n\n// Realistic Teller/Banker attack shape: the prior embedding filetests\n// shadow Hello() string, which proves embedding-bypass in principle.\n// This filetest shadows a method whose signature takes `(_ int, rlm\n// realm, ...)` — the actual shape of grc20.Teller.Transfer,\n// treasury.Banker.Send, and similar rlm-leaking methods — to confirm\n// the attacker really can capture cur via the embedding bypass.\n//\n// Search terms: rlm shadow, capture cur via embedding, Teller attack\n// proof, sealed interface embedding bypass for rlm-typed methods.\n\nimport (\n\t\"gno.land/p/test/seal\"\n)\n\n// Capturable is a Sealed-shaped interface with an rlm-typed method,\n// matching the Teller/Banker pattern (sealed marker + a method that\n// takes rlm).\ntype Capturable interface {\n\tseal.Sealed\n\tDoWithRealm(_ int, rlm realm) string\n}\n\n// evilImpl embeds *seal.Impl — gains the promoted isSealed() marker\n// satisfying the seal, plus the promoted Hello() — then adds its\n// own DoWithRealm that captures rlm. This is the realistic Teller\n// attack shape: attacker holds a legit Sealed value, embeds it,\n// adds a method that takes rlm and stashes it for later use.\ntype evilImpl struct {\n\t*seal.Impl\n}\n\nvar capturedRealmPkgPath string\n\nfunc (e *evilImpl) DoWithRealm(_ int, rlm realm) string {\n\t// rlm captured here — attacker can later use this stash to act\n\t// AS the realm whose cur was captured (A/B-class leak).\n\tcapturedRealmPkgPath = rlm.PkgPath()\n\treturn \"evil captured: \" + capturedRealmPkgPath\n}\n\n// attack runs in a crossing frame so cur is available.\nfunc attack(cur realm) {\n\tevil := \u0026evilImpl{Impl: seal.NewImpl(\"inner\")}\n\t// Assignment SUCCEEDS — *evilImpl satisfies Capturable via the\n\t// embedded *seal.Impl's promoted isSealed() and Hello(), plus\n\t// the attacker-defined DoWithRealm.\n\tvar c Capturable = evil\n\t// Caller hands their cur to what they believe is a sealed,\n\t// in-package Capturable. Attacker's DoWithRealm captures rlm.\n\tprintln(c.DoWithRealm(0, cur))\n\tprintln(\"stash holds:\", capturedRealmPkgPath)\n}\n\nfunc main(cur realm) {\n\tattack(cross(cur))\n}\n\n// Output:\n// evil captured: gno.land/r/test/sealrlmshadow\n// stash holds: gno.land/r/test/sealrlmshadow\n"},{"name":"z_seal_violation_filetest.gno","body":"// PKGPATH: gno.land/r/test/sealviolation\npackage sealviolation\n\n// Regression test: verify that an interface declared in another\n// package with an unexported method cannot be satisfied by a type\n// declared in this package. If this filetest compiles, Gno's\n// sealed-interface semantics differ from Go's and the cur-leak\n// mitigation strategy (sealing /p/ interfaces like grc20.Teller via\n// an unexported method) does NOT work.\n//\n// The expected behavior is a preprocess/typecheck error at the\n// assignment line: foreignImpl's isSealed() method belongs to\n// gno.land/r/test/sealviolation, not gno.land/p/test/seal, so it\n// doesn't satisfy seal.Sealed's seal.isSealed() requirement.\n\nimport (\n\t\"gno.land/p/test/seal\"\n)\n\ntype foreignImpl struct{}\n\nfunc (f *foreignImpl) Hello() string { return \"evil\" }\n\n// This method is named isSealed() but it belongs to package\n// sealviolation, not package seal. Structural interface matching\n// should look for *seal.isSealed* on the type, not just any method\n// named isSealed.\nfunc (f *foreignImpl) isSealed() {}\n\nfunc main() {\n\tvar s seal.Sealed = \u0026foreignImpl{} // should be a compile-time error\n\tprintln(s.Hello())\n}\n\n// Error:\n// gno.land/r/test/sealviolation/z_seal_violation.gno:31:6-36: *gno.land/r/test/sealviolation.foreignImpl does not implement gno.land/p/test/seal.Sealed (missing method isSealed)\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"PWqUOSLzPZewHA6oNKYa27HC5LNUhpGCKddEJS3gtxi2EwaP3MhdKfKeCVKUqHt4XU66JAv34S+Mulg1oivJCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"accesscontrol","path":"gno.land/p/thox/accesscontrol","files":[{"name":"accesscontrol.gno","body":"package accesscontrol\n\nimport (\n\t\"chain\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\nconst (\n\tRoleCreatedEvent   = \"RoleCreated\"\n\tRoleGrantedEvent   = \"RoleGranted\"\n\tRoleRevokedEvent   = \"RoleRevoked\"\n\tRoleRenouncedEvent = \"RoleRenounced\"\n\tRoleSetEvent       = \"RoleSet\"\n)\n\n// Role struct to store role information\ntype Role struct {\n\tName    string\n\tHolders *avl.Tree // address -\u003e struct{}\n\tOwnable *ownable.Ownable\n}\n\n// Roles struct to store all Roles information\ntype Roles struct {\n\tRoles       []*Role\n\tUserToRoles avl.Tree // address -\u003e []*Role\n\tOwnable     *ownable.Ownable\n}\n\nfunc validRoleName(name string) error {\n\tif len(name) \u003e 30 || name == \"\" {\n\t\treturn ErrNameRole\n\t}\n\treturn nil\n}\n\n// NewRole creates a new instance of Role\nfunc NewRole(name string, admin address) (*Role, error) {\n\tif err := validRoleName(name); err != nil {\n\t\treturn nil, ErrNameRole\n\t}\n\n\treturn \u0026Role{\n\t\tName:    name,\n\t\tHolders: avl.NewTree(),\n\t\tOwnable: ownable.NewWithAddress(admin),\n\t}, nil\n}\n\n// CreateRole create a new role within the realm. rlm must be the\n// caller's own captured cur; rlm.Previous().Address() must equal the\n// owner of the Roles container.\nfunc (rs *Roles) CreateRole(_ int, rlm realm, name string) (*Role, error) {\n\tif err := validRoleName(name); err != nil {\n\t\treturn nil, ErrNameRole\n\t}\n\n\tif !rlm.IsCurrent() {\n\t\treturn nil, ErrNotOwner\n\t}\n\tif !rs.Ownable.OwnedBy(rlm.Previous().Address()) {\n\t\treturn nil, ErrNotOwner\n\t}\n\n\tfor _, role := range rs.Roles {\n\t\tif role.Name == name {\n\t\t\treturn nil, ErrRoleSameName\n\t\t}\n\t}\n\n\trole, err := NewRole(name, rs.Ownable.Owner())\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\trs.Roles = append(rs.Roles, role)\n\n\tchain.Emit(\n\t\tRoleCreatedEvent,\n\t\t\"roleName\", name,\n\t\t\"sender\", rs.Ownable.Owner().String(),\n\t)\n\n\treturn role, nil\n}\n\n// HasAccount check if an account has a specific role\nfunc (r *Role) HasAccount(account address) bool {\n\treturn r.Holders.Has(account.String())\n}\n\n// FindRole searches for a role by its name\nfunc (rs *Roles) FindRole(name string) (*Role, error) {\n\tfor _, role := range rs.Roles {\n\t\tif role.Name == name {\n\t\t\treturn role, nil\n\t\t}\n\t}\n\n\treturn nil, ErrRoleNotFound\n}\n\n// GrantRole grants a role to an account. rlm must be the caller's own\n// captured cur; rlm.Previous().Address() must equal the role's\n// admin/owner.\nfunc (rs *Roles) GrantRole(_ int, rlm realm, name string, account address) error {\n\tr, err := rs.FindRole(name)\n\tif err != nil {\n\t\treturn ErrRoleNotFound\n\t}\n\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotOwner\n\t}\n\tcaller := rlm.Previous().Address()\n\tif !r.Ownable.OwnedBy(caller) {\n\t\treturn ErrNotOwner\n\t}\n\n\tr.Holders.Set(account.String(), struct{}{})\n\n\t// Add in UserToRoles\n\troles := rs.UserToRoles.Get(account.String())\n\tif roles == nil {\n\t\troles = []*Role{}\n\t}\n\troles = append(roles.([]*Role), r)\n\trs.UserToRoles.Set(account.String(), roles)\n\n\tchain.Emit(\n\t\tRoleGrantedEvent,\n\t\t\"roleName\", r.Name,\n\t\t\"account\", account.String(),\n\t\t\"sender\", caller.String(),\n\t)\n\n\treturn nil\n}\n\n// RevokeRole revokes a role from an account. rlm must be the caller's\n// own captured cur; rlm.Previous().Address() must equal the role's\n// admin/owner.\nfunc (rs *Roles) RevokeRole(_ int, rlm realm, name string, account address) error {\n\tr, err := rs.FindRole(name)\n\tif err != nil {\n\t\treturn ErrRoleNotFound\n\t}\n\n\tif !rlm.IsCurrent() {\n\t\treturn ErrNotOwner\n\t}\n\tcaller := rlm.Previous().Address()\n\tif !r.Ownable.OwnedBy(caller) {\n\t\treturn ErrNotOwner\n\t}\n\n\tr.Holders.Remove(account.String())\n\n\t// Remove in UserToRoles\n\troles := rs.UserToRoles.Get(account.String())\n\tif roles != nil {\n\t\tupdatedRoles := []*Role{}\n\t\tfor _, role := range roles.([]*Role) {\n\t\t\tif role != r {\n\t\t\t\tupdatedRoles = append(updatedRoles, role)\n\t\t\t}\n\t\t}\n\t\trs.UserToRoles.Set(account.String(), updatedRoles)\n\t}\n\n\tchain.Emit(\n\t\tRoleRevokedEvent,\n\t\t\"roleName\", r.Name,\n\t\t\"account\", account.String(),\n\t\t\"sender\", caller.String(),\n\t)\n\n\treturn nil\n}\n\n// RenounceRole allows an account to renounce a role it holds. rlm must\n// be the caller's own captured cur; the renouncer is\n// rlm.Previous().Address(). Closing the forgery here matters: without\n// it, any external realm could force a victim to renounce a role by\n// passing the victim's address as caller.\nfunc (rs *Roles) RenounceRole(_ int, rlm realm, name string) error {\n\tr, err := rs.FindRole(name)\n\tif err != nil {\n\t\treturn ErrRoleNotFound\n\t}\n\n\tif !rlm.IsCurrent() {\n\t\treturn ErrAccountNotRole\n\t}\n\tcaller := rlm.Previous().Address()\n\tif !r.HasAccount(caller) {\n\t\treturn ErrAccountNotRole\n\t}\n\n\tr.Holders.Remove(caller.String())\n\n\tchain.Emit(\n\t\tRoleRenouncedEvent,\n\t\t\"roleName\", r.Name,\n\t\t\"account\", caller.String(),\n\t\t\"sender\", caller.String(),\n\t)\n\n\treturn nil\n}\n\n// SetRoleAdmin transfers the ownership of the role to a new\n// administrator. rlm must be the caller's own captured cur and must be\n// the current admin (delegated to ownable.TransferOwnership which\n// re-checks via rlm.Previous().Address()).\nfunc (r *Role) SetRoleAdmin(_ int, rlm realm, admin address) error {\n\tif err := r.Ownable.TransferOwnership(0, rlm, admin); err != nil {\n\t\treturn err\n\t}\n\n\tchain.Emit(\n\t\tRoleSetEvent,\n\t\t\"roleName\", r.Name,\n\t\t\"newAdminRole\", r.Ownable.Owner().String(),\n\t)\n\n\treturn nil\n}\n"},{"name":"accesscontrol_test.gno","body":"package accesscontrol\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nvar (\n\tadmin    = testutils.TestAddress(\"admin1\")\n\tnewAdmin = testutils.TestAddress(\"admin2\")\n\tuser1    = testutils.TestAddress(\"user1\")\n\tuser2    = testutils.TestAddress(\"user2\")\n\n\troleName = \"TestRole\"\n)\n\nfunc initSetup(adminAddr address) *Roles {\n\treturn \u0026Roles{\n\t\tRoles:   []*Role{},\n\t\tOwnable: ownable.NewWithAddress(adminAddr),\n\t}\n}\n\n// asAdmin / asUser1 / asUser2 wrap the migrated (_ int, rlm realm)\n// API in a crossing closure under SetRealm(NewUserRealm(addr)) so\n// rlm.Previous().Address() inside the callee resolves to addr.\n\nfunc TestCreateRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar (\n\t\trole *Role\n\t\terr  error\n\t)\n\tfunc(cur realm) { role, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\tuassert.NotNil(t, role, \"role should not be nil\")\n\tuassert.Equal(t, role.Name, roleName)\n\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.Error(t, err, \"should fail on duplicate role creation\")\n}\n\nfunc TestGrantRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\trole, err := roles.FindRole(roleName)\n\tuassert.NoError(t, err)\n\tuassert.True(t, role.HasAccount(user1), \"user1 should have the TestRole\")\n\n\trolesList := roles.UserToRoles.Get(user1.String())\n\tuassert.NotNil(t, rolesList, \"user1 should be in UserToRoles\")\n\tuassert.True(t, containsRole(rolesList.([]*Role), role), \"UserToRoles should contain TestRole for user1\")\n}\n\nfunc TestGrantRoleByNonOwner(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\t// user2 is not the role admin (admin is); GrantRole should fail.\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.Error(t, err, \"non-owner should not be able to grant roles\")\n}\n\nfunc TestRevokeRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\tfunc(cur realm) { err = roles.RevokeRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\trole, err := roles.FindRole(roleName)\n\tuassert.NoError(t, err)\n\tuassert.False(t, role.HasAccount(user1), \"user1 should no longer have the TestRole\")\n\n\trolesList := roles.UserToRoles.Get(user1.String())\n\tif rolesList != nil {\n\t\tuassert.False(t, containsRole(rolesList.([]*Role), role), \"UserToRoles should not contain TestRole for user1 after revocation\")\n\t}\n}\n\nfunc TestRenounceRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\t// user1 renounces their own role — must be in user1's context.\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\tfunc(cur realm) { err = roles.RenounceRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\trole, err := roles.FindRole(roleName)\n\tuassert.NoError(t, err)\n\tuassert.False(t, role.HasAccount(user1), \"user1 should have renounced the TestRole\")\n}\n\nfunc TestRenounceRoleByNonHolder(cur realm, t *testing.T) {\n\t// Regression for the address-parameter forgery: previously\n\t// RenounceRole(victim) could be called by any realm to force a\n\t// victim to renounce. After the IsCurrent + rlm.Previous() fix,\n\t// the renouncer is the principal — an attacker cannot strip\n\t// someone else's role.\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\t// user2 tries to \"renounce\" user1's role — must fail because user2\n\t// does not hold the role.\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\tfunc(cur realm) { err = roles.RenounceRole(0, cur, roleName) }(cross(cur))\n\tuassert.Error(t, err, \"non-holder must not be able to renounce\")\n\n\trole, err := roles.FindRole(roleName)\n\tuassert.NoError(t, err)\n\tuassert.True(t, role.HasAccount(user1), \"user1 must still hold the role\")\n}\n\nfunc TestSetRoleAdmin(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar (\n\t\trole *Role\n\t\terr  error\n\t)\n\tfunc(cur realm) { role, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\tfunc(cur realm) { err = role.SetRoleAdmin(0, cur, newAdmin) }(cross(cur))\n\tuassert.NoError(t, err, \"admin change should succeed\")\n\n\tuassert.Equal(t, role.Ownable.Owner(), newAdmin, \"the new admin should be newAdmin\")\n\tuassert.NotEqual(t, role.Ownable.Owner(), admin, \"the old admin should no longer be the owner\")\n}\n\nfunc TestCreateRoleInvalidName(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, \"\") }(cross(cur))\n\tuassert.Error(t, err, \"should fail on empty role name\")\n\n\tlongRoleName := \"thisisaverylongrolenamethatexceedsthenormallimitfortestingpurposes\"\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, longRoleName) }(cross(cur))\n\tuassert.Error(t, err, \"should fail on very long role name\")\n}\n\nfunc TestRevokeRoleByNonOwner(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\tfunc(cur realm) { err = roles.RevokeRole(0, cur, roleName, user1) }(cross(cur))\n\tuassert.Error(t, err, \"non-owner should not be able to revoke roles\")\n}\n\nfunc TestGrantRoleToNonExistentRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, \"NonExistentRole\", user1) }(cross(cur))\n\tuassert.Error(t, err, \"should fail when granting non-existent role\")\n}\n\nfunc TestRevokeRoleFromNonExistentRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { err = roles.RevokeRole(0, cur, \"NonExistentRole\", user1) }(cross(cur))\n\tuassert.Error(t, err, \"should fail when revoking non-existent role\")\n}\n\nfunc TestRenounceNonExistentRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\tvar err error\n\tfunc(cur realm) { err = roles.RenounceRole(0, cur, \"NonExistentRole\") }(cross(cur))\n\tuassert.Error(t, err, \"should fail when renouncing non-existent role\")\n}\n\nfunc TestDeleteRole(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar (\n\t\trole *Role\n\t\terr  error\n\t)\n\tfunc(cur realm) { role, err = roles.CreateRole(0, cur, roleName) }(cross(cur))\n\tuassert.NoError(t, err)\n\tuassert.NotNil(t, role, \"role should not be nil\")\n\n\troles.Roles = []*Role{} // Clear roles for testing purpose\n\t_, err = roles.FindRole(roleName)\n\tuassert.Error(t, err, \"should fail when trying to find deleted role\")\n}\n\nfunc TestUserToRolesWithMultipleRoles(cur realm, t *testing.T) {\n\troles := initSetup(admin)\n\n\troleName1 := \"Role1\"\n\troleName2 := \"Role2\"\n\n\t// Create two roles\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\tvar err error\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName1) }(cross(cur))\n\tuassert.NoError(t, err)\n\tfunc(cur realm) { _, err = roles.CreateRole(0, cur, roleName2) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\t// Grant both roles to user1\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName1, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\tfunc(cur realm) { err = roles.GrantRole(0, cur, roleName2, user1) }(cross(cur))\n\tuassert.NoError(t, err)\n\n\t// Check if user1 has both roles\n\trolesList := roles.UserToRoles.Get(user1.String())\n\tuassert.NotNil(t, rolesList, \"user1 should be in UserToRoles\")\n\trole1, _ := roles.FindRole(roleName1)\n\trole2, _ := roles.FindRole(roleName2)\n\tuassert.True(t, containsRole(rolesList.([]*Role), role1), \"UserToRoles should contain Role1 for user1\")\n\tuassert.True(t, containsRole(rolesList.([]*Role), role2), \"UserToRoles should contain Role2 for user1\")\n}\n\n// func test for check if a role is in a list of roles\nfunc containsRole(roles []*Role, target *Role) bool {\n\tfor _, role := range roles {\n\t\tif role == target {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"},{"name":"doc.gno","body":"// Package accesscontrol implements a role-based access control (RBAC) system for Gno applications.\n// It provides functionality to create, assign, revoke, and transfer roles.\n//\n// # Usage\n//\n// Import the `gno.land/p/demo/accesscontrol` package to manage roles within your Gno realm. You can create roles,\n// assign them to users, revoke them, and transfer role ownership.\n//\n// Roles can be created by the contract owner using `CreateRole`. Each role is uniquely identified by its name.\n//\n//\tCreateRole(\"editor\")\n//\n// Use `GrantRole` to assign a role to a user, and `RevokeRole` to remove a role from a user.\n//\n//\tGrantRole(\"editor\", userAddress)\n//\n//\tRevokeRole(\"editor\", userAddress)\n//\n// Users can renounce their roles using `RenounceRole`, voluntarily removing themselves from a role.\n//\n//\tRenounceRole(\"editor\")\n//\n// You can look up a role by name with `FindRole`.\n//\n// FindRole(\"editor\")\n//\n// Role ownership can be transferred using `SetRoleAdmin`.\n//\n// SetRoleAdmin(newAdminAddress)\n//\n// Key events\n// - `RoleCreatedEvent`: Triggered when a new role is created\n// Key includes:\n// `roleName` (name of the role)\n// `sender` (address of the sender)\n//\n// - `RoleGrantedEvent`: Triggered when a role is granted to an account\n// Key includes:\n// `roleName` (name of the role)\n// `account` (address of the account)\n// `sender` (address of the sender)\n//\n// - `RoleRevokedEvent`: Triggered when a role is revoked from an account\n// Key includes:\n// `roleName` (name of the role)\n// `account` (address of the account)\n// `sender` (address of the sender)\n//\n// - `RoleRenouncedEvent`: Triggered when a role is renounced by an account\n// Key includes:\n// `roleName` (name of the role)\n// `account` (address of the account)\n//\n// - `RoleSetEvent`: Triggered when a role's administrator is set or changed\n// Key includes:\n// `roleName` (name of the role)\n// `newAdmin` (address of the new administrator)\n// `sender` (address of the sender)\npackage accesscontrol\n"},{"name":"errors.gno","body":"package accesscontrol\n\nimport \"errors\"\n\nvar (\n\tErrNotMatchAccount = errors.New(\"accesscontrol: caller confirmation does not match account\")\n\tErrRoleSameName    = errors.New(\"accesscontrol: role already exists with the same name\")\n\tErrRoleNotFound    = errors.New(\"accesscontrol: role not found\")\n\tErrAccountNotRole  = errors.New(\"accesscontrol: this account does not have the role\")\n\tErrNameRole        = errors.New(\"accesscontrol: role name cannot be empty or exceed 30 characters\")\n\tErrNotOwner        = errors.New(\"accesscontrol: caller is not the owner of the role\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/thox/accesscontrol\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Q1noHffXHLxqu9p3ySVEFpR+a5see3EKArXYuUO26QN68a+tZ0YJFnzGbFRFYZ115USKf4JIdCnuzZ3vz9MNBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"snowflake","path":"gno.land/p/thox/snowflake","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/thox/snowflake\"\ngno = \"0.9\"\n"},{"name":"snowflake.gno","body":"// Package snowflake provides utility functions for generating unique identifiers\n// (Snowflake IDs) based on a custom implementation inspired by the Snowflake algorithm (https://pkg.go.dev/github.com/godruoyi/go-snowflake#section-sourcefiles).\n// The Snowflake IDs generated by this package ensure uniqueness and are suitable for\n// distributed systems. The package offers a mechanism to convert these IDs\n// to standard SnowflakeID format strings, making them compatible with systems\n// requiring UUIDs.\n// Add import \"gno.land/p/demo/entropy\" to use the entropy package. For of the pseudo-random number generator.\n// TimestampLength is 42-bit.\n// 42 it's the maximum value that can be returned \"2^42\", It's represent 139 years (2^42 secondes)\n// SequenBits allows for 4096 unique IDs to be generated per millisecond.\n\npackage snowflake\n\nimport (\n\t\"encoding/binary\"\n\t\"encoding/hex\"\n\t\"time\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst (\n\tTimestampBits uint8  = 42\n\tMachineIDBits uint64 = 10\n\tSequenceBits  uint64 = 12\n\tMaxSequence   uint16 = 1\u003c\u003cSequenceBits - 1\n\tMaxTimestamp  uint64 = 1\u003c\u003cTimestampBits - 1\n\tMaxMachineID  uint64 = 1\u003c\u003cMachineIDBits - 1\n\n\tmachineIDMoveLength = SequenceBits\n\ttimestampMoveLength = MachineIDBits + SequenceBits\n\n\tstartTime uint64 = 1226354400000 // 10 Nov 2008 23:00:00 UTC in milliseconds (Snowflake epoch)\n)\n\n// SnowflakeID struct for generating unique Snowflake-based identifiers.\ntype SnowflakeID struct {\n\tmachineID     uint64\n\tsequence      uint16\n\tstartTime     uint64\n\tlastTimestamp uint64\n\tentropy       *entropy.Instance\n}\n\n// NewSnowflakeID constructs a SnowflakeID with machineID = 0. Callers\n// who want a realm-derived machineID should follow up with\n// SetMachineID(0, cur). Split from the constructor so the constructor\n// stays usable in non-crossing contexts (e.g. package-level var init,\n// where there's no rlm in scope).\nfunc NewSnowflakeID() *SnowflakeID {\n\treturn \u0026SnowflakeID{\n\t\tstartTime: startTime,\n\t\tentropy:   entropy.New(),\n\t}\n}\n\n// GenerateID generates a unique identifier based on the Snowflake algorithm.\n// It combines the current timestamp, machine ID, and a sequence number to ensure uniqueness\n// across distributed systems.\nfunc (s *SnowflakeID) GenerateID() uint64 {\n\tcurrent := s.getTimenow()\n\n\tif current == s.lastTimestamp {\n\t\ts.incrementSequence()\n\t} else {\n\t\ts.sequence = 0\n\t\ts.lastTimestamp = current\n\t}\n\n\telapsedTime := uint64(current-s.startTime) % MaxTimestamp\n\n\tid := (uint64(elapsedTime) \u003c\u003c timestampMoveLength) |\n\t\t(uint64(s.machineID) \u003c\u003c machineIDMoveLength) |\n\t\tuint64(s.incrementSequence())\n\treturn id\n}\n\nfunc (s *SnowflakeID) incrementSequence() uint16 {\n\trandom := uint16(s.entropy.Value())\n\ts.sequence = (s.sequence + 1 + random) \u0026 MaxSequence\n\treturn s.sequence\n}\n\nfunc (s *SnowflakeID) getTimenow() uint64 {\n\treturn uint64(time.Now().UnixMilli())\n}\n\n// SetMachineID derives the machine ID from the caller's address. rlm\n// must be the caller's own captured cur; the caller is\n// rlm.Previous().Address(). Pre-migration used unsafe.PreviousRealm()\n// which a non-crossing wrapper could spoof, producing a machineID\n// computed from the wrong address.\nfunc (s *SnowflakeID) SetMachineID(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"snowflake: rlm does not match the current crossing frame\")\n\t}\n\tcaller := rlm.Previous().Address()\n\tmachineID := uint64(0)\n\tfor _, c := range caller.String() {\n\t\tmachineID += uint64(c)\n\t}\n\tmachineID %= MaxMachineID\n\ts.machineID = machineID\n}\n\nfunc SnowflakeIDToUUIDString(id uint64) string {\n\tbytes := make([]byte, 16)\n\n\t// Copy transformed ID into the second half of the array\n\tcopy(bytes[8:], uint64ToBytes(id))\n\n\t// Use bits from the Snowflake ID to generate the first half of the SnowflakeID\n\tbytes[0] = byte(id \u003e\u003e 60)\n\tbytes[1] = byte(id \u003e\u003e 52)\n\tbytes[2] = byte(id \u003e\u003e 44)\n\tbytes[3] = byte(id \u003e\u003e 36)\n\tbytes[4] = byte(id \u003e\u003e 28)\n\tbytes[5] = byte(id \u003e\u003e 20)\n\tbytes[6] = byte(id \u003e\u003e 12)\n\tbytes[7] = byte(id \u003e\u003e 4)\n\n\t// set the version and variant bits according to SnowflakeID specification.\n\tbytes[6] = (bytes[6] \u0026 0x0f) | 0x40 // version 4 (random)\n\tbytes[8] = (bytes[8] \u0026 0x3f) | 0x80 // variant 1 (RFC 4122)\n\n\thexStr := hex.EncodeToString(bytes)\n\treturn ufmt.Sprintf(\"%s-%s-%s-%s-%s\",\n\t\thexStr[0:8],\n\t\thexStr[8:12],\n\t\thexStr[12:16],\n\t\thexStr[16:20],\n\t\thexStr[20:],\n\t)\n}\n\nfunc uint64ToBytes(i uint64) []byte {\n\tbuf := make([]byte, 8)\n\tbinary.BigEndian.PutUint64(buf, i)\n\treturn buf\n}\n"},{"name":"snowflake_test.gno","body":"package snowflake\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestID(cur realm, t *testing.T) {\n\tu := NewSnowflakeID()\n\tfunc(cur realm) { u.SetMachineID(0, cur) }(cross(cur))\n\tid1 := u.GenerateID()\n\tid2 := u.GenerateID()\n\tuuid1 := SnowflakeIDToUUIDString(id1)\n\tuuid2 := SnowflakeIDToUUIDString(id2)\n\n\tuassert.NotEqual(t, uuid1, uuid2)\n\n\tidMap := make(map[uint64]bool)\n\tuuidMap := make(map[string]bool)\n\n\tfor i := 0; i \u003c 101; i++ {\n\t\tid := u.GenerateID()\n\t\tif _, exists := idMap[id]; exists {\n\t\t\tt.Errorf(\"Duplicate ID found: %d\", id)\n\t\t}\n\t\tidMap[id] = true\n\n\t\tuuidStr := SnowflakeIDToUUIDString(id)\n\t\tif _, exists := uuidMap[uuidStr]; exists {\n\t\t\tt.Errorf(\"Duplicate UUID found: %s\", uuidStr)\n\t\t}\n\t\tuuidMap[uuidStr] = true\n\t\tt.Logf(\"Generated UUID %d: %s\", i, uuidStr)\n\t}\n}\n\nfunc TestGetEntropy(cur realm, t *testing.T) {\n\tu := NewSnowflakeID()\n\tinitialEntropyValue := u.entropy.Value()\n\n\tfor i := 0; i \u003c 10; i++ {\n\t\tu.GenerateID()\n\t}\n\n\tuassert.NotEqual(t, initialEntropyValue, u.entropy.Value())\n}\n\n// Output:\n// Generated UUID 0: 0007a63f-a542-4310-807a-63fa54253102\n// Generated UUID 1: 0007a63f-a542-43fe-807a-63fa54253fe1\n// Generated UUID 2: 0007a63f-a542-43e3-807a-63fa54253e34\n// Generated UUID 3: 0007a63f-a542-432f-807a-63fa542532fb\n// Generated UUID 4: 0007a63f-a542-4353-807a-63fa54253536\n// Generated UUID 5: 0007a63f-a542-43be-807a-63fa54253be5\n// Generated UUID 6: 0007a63f-a542-43e0-807a-63fa54253e08\n// Generated UUID 7: 0007a63f-a542-4329-807a-63fa5425329f\n// Generated UUID 8: 0007a63f-a542-430a-807a-63fa542530aa\n// Generated UUID 9: 0007a63f-a542-43f2-807a-63fa54253f29\n// Generated UUID 10: 0007a63f-a542-4351-807a-63fa5425351c\n// .......\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"RkEDfZgIaXSVbDYhWLWVo8OIPHwCYbPBtmCbAKXUZ0adwB/274bOQa13BXLP5gHNL330wRLueO6nOUA2sEOhAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"timelock","path":"gno.land/p/thox/timelock","files":[{"name":"doc.gno","body":"// Package timelock provides a library for scheduling, cancelling, and\n// executing time-locked operations in Gno. It ensures that\n// operations are only carried out after a specified delay and offers\n// mechanisms for managing and verifying the status of these operations.\n// This package leverages an AVL tree for efficient management of timestamps\n// and integrates role-based access control for administrative tasks.\n//\n// # Usage:\n//\n//\timport \"gno.land/p/demo/timelock\"\n//\timport \"gno.land/p/demo/accesscontrol\"\n//\n//\tInitialize timelock utility with an AVL tree and access control.\n//\ttimestamps := avl.NewTree()\n//\tadminRole := accesscontrol.NewRole(\"admin\", address(\"admin-address\"))\n//\ttimeLockUtil := timelock.NewTimeLockUtil(timestamps, adminRole, 30)\n//\n//\tSchedule an operation with a delay of 60 seconds.\n//\tid := seqid.ID()\n//\ttimeLockUtil.Schedule(id, 60)\n//\n//\tCheck if an operation is pending.\n//\tisPending := timeLockUtil.IsPending(id)\n//\n//\tExecute the operation when it is pending.\n//\tif timeLockUtil.IsPending(id) {\n//\t    timeLockUtil.Execute(id)\n//\t}\n//\n//\tUpdate the minimum delay for future operations.\n//\ttimeLockUtil.UpdateDelay(45)\npackage timelock\n"},{"name":"errors.gno","body":"package timelock\n\nimport \"errors\"\n\nvar (\n\tErrNilTimestampsOrAccessControl = errors.New(\"timelock: timestamps and accesscontrol values must be different from nil\")\n\tErrInsufficientDelay            = errors.New(\"timelockutil: Schedule: insufficient delay\")\n\tErrOperationAlreadyScheduled    = errors.New(\"timelockutil: Schedule: operation already scheduled\")\n\tErrOperationNotPending          = errors.New(\"timelock: operation not pending\")\n\tErrUnexpectedType               = errors.New(\"timelockutil: GetTimestamp: unexpected type\")\n\tErrUpadateDelay                 = errors.New(\"timelock: UpdateDelay: only admin can update delay\")\n\tErrOperationCancelNotPending    = errors.New(\"timelock: Cancel: operation not pending\")\n\tErrOperationExecuteNotPending   = errors.New(\"timelock: Execute: operation not pending\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/thox/timelock\"\ngno = \"0.9\"\n"},{"name":"timelock.gno","body":"package timelock\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/thox/accesscontrol\"\n)\n\n// Represents the status of a planned operation\ntype OperationState int\n\nconst (\n\tUnset OperationState = iota\n\tPending\n\tReady\n\tDone\n)\n\nfunc (os OperationState) StateToString() string {\n\tswitch os {\n\tcase Unset:\n\t\treturn \"Unset\"\n\tcase Pending:\n\t\treturn \"Pending\"\n\tcase Ready:\n\t\treturn \"Ready\"\n\tcase Done:\n\t\treturn \"Done\"\n\tdefault:\n\t\treturn \"Unknown\"\n\t}\n}\n\n// OperationStatus represents the status of an operation\ntype OperationStatus struct {\n\tsheduleTime int64\n\tisDone      bool\n}\n\n// TimeLock stores the necessary parameters for the timelock operations\ntype TimeLock struct {\n\ttimestamps    *avl.Tree // id -\u003e time.Time\n\taccessControl *accesscontrol.Role\n\tminDelay      uint64\n}\n\n// New instance of TimeLock\nfunc NewTimeLock(timestamps *avl.Tree, accessControl *accesscontrol.Role, minDelay uint64) (*TimeLock, error) {\n\tif timestamps == nil || accessControl == nil {\n\t\treturn nil, ErrNilTimestampsOrAccessControl\n\t}\n\n\treturn \u0026TimeLock{\n\t\ttimestamps:    timestamps,\n\t\taccessControl: accessControl,\n\t\tminDelay:      minDelay,\n\t}, nil\n}\n\n// Schedules an operation to be carried out after a minimum delay\nfunc (tl *TimeLock) Schedule(id seqid.ID, delay uint64) error {\n\tif delay \u003c tl.minDelay {\n\t\treturn ErrInsufficientDelay\n\t}\n\n\tif tl.timestamps.Has(id.Binary()) {\n\t\treturn ErrOperationAlreadyScheduled\n\t}\n\n\ttimestamp := time.Now().Unix() + int64(delay)\n\tstatus := OperationStatus{sheduleTime: timestamp, isDone: false}\n\ttl.timestamps.Set(id.Binary(), status)\n\n\tchain.Emit(\n\t\t\"TimeLockScheduled\",\n\t\t\"id\", id.String(),\n\t\t\"delay\", strconv.FormatInt(int64(delay), 10),\n\t)\n\n\treturn nil\n}\n\n// Remove operation\nfunc (tl *TimeLock) Remove(id seqid.ID) {\n\ttl.timestamps.Remove(id.Binary())\n\n\tchain.Emit(\n\t\t\"TimeLockRemoved\",\n\t\t\"id\", id.String(),\n\t)\n}\n\n// Cancels a planned operation\nfunc (tl *TimeLock) Cancel(id seqid.ID) error {\n\tif !tl.IsPending(id) {\n\t\treturn ErrOperationCancelNotPending\n\t}\n\n\ttl.timestamps.Remove(id.Binary())\n\n\tchain.Emit(\n\t\t\"TimeLockCancelled\",\n\t\t\"id\", id.String(),\n\t)\n\treturn nil\n}\n\n// Executes a pending operation\nfunc (tl *TimeLock) Execute(id seqid.ID) error {\n\tif !tl.IsPending(id) {\n\t\treturn ErrOperationExecuteNotPending\n\t}\n\n\tstatus, err := tl.GetOperationStatus(id)\n\tif err != nil {\n\t\treturn err\n\t}\n\tstatus.isDone = true\n\ttl.timestamps.Set(id.Binary(), status)\n\n\tchain.Emit(\n\t\t\"TimeLockExecuted\",\n\t\t\"id\", id.String(),\n\t)\n\n\treturn nil\n}\n\n// UpdateDelay changes the minimum lead time for future operations. rlm\n// must be the caller's own captured cur; rlm.Previous().Address() must\n// be the TimeLock's owner.\nfunc (tl *TimeLock) UpdateDelay(_ int, rlm realm, newDelay uint64) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrUpadateDelay\n\t}\n\tif rlm.Previous().Address() != tl.accessControl.Ownable.Owner() {\n\t\treturn ErrUpadateDelay\n\t}\n\n\tchain.Emit(\n\t\t\"TimeLockMinDelayChanged\",\n\t\t\"oldDelay\", strconv.FormatInt(int64(tl.minDelay), 10),\n\t\t\"newDelay\", strconv.FormatInt(int64(newDelay), 10),\n\t)\n\n\ttl.minDelay = newDelay\n\n\treturn nil\n}\n\n// Checks if an operation is pending\nfunc (tl *TimeLock) IsPending(id seqid.ID) bool {\n\tstate, err := tl.GetOperationState(id)\n\tif err != nil {\n\t\t// Handle the error appropriately; for now, we assume the operation is not pending if there's an error\n\t\tufmt.Errorf(\"Error retrieving operation state: %v\", err)\n\t\treturn false\n\t}\n\n\treturn state == Pending\n}\n\n// Checks if an operation is ready\nfunc (tl *TimeLock) IsReady(id seqid.ID) bool {\n\tstate, err := tl.GetOperationState(id)\n\tif err != nil {\n\t\t// Handle the error appropriately; for now, we assume the operation is not pending if there's an error\n\t\tufmt.Errorf(\"Error retrieving operation state: %v\", err)\n\t\treturn false\n\t}\n\n\treturn state == Ready\n}\n\n// Returns the status of an operation\nfunc (tl *TimeLock) GetOperationState(id seqid.ID) (OperationState, error) {\n\tstatus, err := tl.GetOperationStatus(id)\n\tif err != nil {\n\t\treturn Unset, err\n\t}\n\tif status.isDone {\n\t\treturn Done, nil\n\t}\n\tif status.sheduleTime == 0 {\n\t\treturn Unset, nil\n\t}\n\tif status.sheduleTime \u003e time.Now().Unix() {\n\t\treturn Pending, nil\n\t}\n\treturn Ready, nil\n}\n\n// Returns the status of an operation\nfunc (tl *TimeLock) GetOperationStatus(id seqid.ID) (OperationStatus, error) {\n\tvalue := tl.timestamps.Get(id.Binary())\n\n\tif value == nil {\n\t\treturn OperationStatus{}, nil // Return an empty status if the operation is not found\n\t}\n\tif status, ok := value.(OperationStatus); ok {\n\t\treturn status, nil\n\t} else {\n\t\treturn OperationStatus{}, ErrUnexpectedType\n\t}\n}\n"},{"name":"timelock_test.gno","body":"package timelock\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/thox/accesscontrol\"\n)\n\nfunc TestTimelock(cur realm, t *testing.T) {\n\t// Initialization\n\ttimestamps := avl.NewTree()\n\tminDelay := uint64(2) // 2 seconds to simplify testing\n\taccessControl, _ := accesscontrol.NewRole(\"admin\", unsafe.OriginCaller())\n\ttimelockUtil, err := NewTimeLock(timestamps, accessControl, minDelay)\n\n\t// Generate a new ID from time.Now().UnixNano() with seconds added to guarantee uniqueness\n\tnewID := func(offset int64) seqid.ID {\n\t\treturn seqid.ID(time.Now().UnixNano() + offset)\n\t}\n\n\tuassert.NoError(t, err, \"Failed to create TimeLock instance\")\n\n\t// Test Schedule\n\tt.Run(\"Schedule\", func(t *testing.T) {\n\t\tid := newID(0)\n\t\tdelay := uint64(3) // 3 seconds\n\n\t\terr := timelockUtil.Schedule(id, delay)\n\n\t\tuassert.NoError(t, err, \"Schedule failed\")\n\n\t\tstatus, err := timelockUtil.GetOperationStatus(id)\n\n\t\tuassert.NoError(t, err, \"failed to get operation status\")\n\t\tuassert.NotEmpty(t, status.sheduleTime, \"operation status not set or invalid\")\n\t})\n\n\t// Test Cancel\n\tt.Run(\"Cancel\", func(t *testing.T) {\n\t\tid := newID(1)\n\n\t\t// Plan a new operation to ensure it is unique\n\t\terr := timelockUtil.Schedule(id, uint64(3))\n\t\tuassert.NoError(t, err, \"Failed to schedule operation for cancellation\")\n\n\t\terr = timelockUtil.Cancel(id)\n\t\tuassert.NoError(t, err, \"Cancel failed\")\n\n\t\tstatus, err := timelockUtil.GetOperationStatus(id)\n\t\tuassert.NoError(t, err, \"failed to get operation status\")\n\t\tuassert.Empty(t, status.sheduleTime, \"operation not cancelled\")\n\t})\n\n\t// Test Execute\n\tt.Run(\"Execute\", func(t *testing.T) {\n\t\tid := newID(2)\n\t\tdelay := uint64(3) // 3 seconds\n\t\tfutureTime := time.Now().Unix() + int64(delay)\n\n\t\t// Schedule the operation with a future timestamp\n\t\terr := timelockUtil.Schedule(id, delay)\n\t\tuassert.NoError(t, err, \"Failed to schedule operation for execution\")\n\n\t\t// Simulates the passage of time by setting the timestamp to a future time\n\t\ttimestamps.Set(id.Binary(), OperationStatus{sheduleTime: futureTime, isDone: false})\n\n\t\terr = timelockUtil.Execute(id)\n\t\tuassert.NoError(t, err, \"Execute failed\")\n\n\t\tstate, err := timelockUtil.GetOperationState(id)\n\t\tuassert.NoError(t, err, \"failed to get operation state\")\n\t\tuassert.Equal(t, Done.StateToString(), state.StateToString(), \"operation not executed\")\n\t})\n\n\t// Test UpdateDelay\n\tt.Run(\"UpdateDelay\", func(t *testing.T) {\n\t\tnewDelay := uint64(4) // 4 seconds\n\n\t\t// UpdateDelay now requires the caller to be the timelock owner;\n\t\t// thread cur via a crossing closure under SetRealm(NewUserRealm)\n\t\t// so rlm.Previous().Address() resolves to the test's OriginCaller\n\t\t// (the owner set at NewTimeLock construction).\n\t\ttesting.SetRealm(testing.NewUserRealm(unsafe.OriginCaller()))\n\t\tvar err error\n\t\tfunc(cur realm) { err = timelockUtil.UpdateDelay(0, cur, newDelay) }(cross(cur))\n\t\tuassert.NoError(t, err, \"UpdateDelay failed\")\n\n\t\tuassert.Equal(t, newDelay, timelockUtil.minDelay, \"minDelay not updated\")\n\t})\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"LtXtw+K6ek7b1dNFVISkOeAxO3jUdEm5Z5lyrxQxeQJuEOGU58LfptNakYghrlwOERC2Fy9qmY2UeKlgbgJ+Bw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"xorshiftr128plus","path":"gno.land/p/wyhaines/rand/xorshiftr128plus","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/wyhaines/rand/xorshiftr128plus\"\ngno = \"0.9\"\n"},{"name":"xorshiftr128plus.gno","body":"// Xorshiftr128+ is a very fast psuedo-random number generation algorithm with strong\n// statistical properties.\n//\n// The default random number algorithm in gno was ported from Go's v2 rand implementatoon, which\n// defaults to the PCG algorithm. This algorithm is commonly used in language PRNG implementations\n// because it has modest seeding requirements, and generates statistically strong randomness.\n//\n// This package provides an implementation of the Xorshiftr128+ PRNG algorithm. This algorithm provides\n// strong statistical performance with most seeds (just don't seed it with zeros), and the performance\n// of this implementation in Gno is more than four times faster than the default PCG implementation in\n// `math/rand`.\n//\n//\tBenchmark\n//\t---------\n//\tPCG:           1000000 Uint64 generated in 15.48s\n//\tXorshiftr128+: 1000000 Uint64 generated in 3.22s\n//\tRatio:         x4.81 times faster than PCG\n//\n// Use it directly:\n//\n//\tprng = xorshiftr128plus.New() // pass a uint64 to seed it or pass nothing to seed it with entropy\n//\n// Or use it as a drop-in replacement for the default PRNT in Rand:\n//\n//\tsource = xorshiftr128plus.New()\n//\tprng := rand.New(source)\npackage xorshiftr128plus\n\nimport (\n\t\"errors\"\n\t\"math\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Xorshiftr128Plus struct {\n\tseed [2]uint64 // Seeds\n}\n\nfunc New(seeds ...uint64) *Xorshiftr128Plus {\n\tvar s1, s2 uint64\n\tseed_length := len(seeds)\n\tif seed_length \u003c 2 {\n\t\te := entropy.New()\n\t\tif seed_length == 0 {\n\t\t\ts1 = e.Value64()\n\t\t\ts2 = e.Value64()\n\t\t} else {\n\t\t\ts1 = seeds[0]\n\t\t\ts2 = e.Value64()\n\t\t}\n\t} else {\n\t\ts1 = seeds[0]\n\t\ts2 = seeds[1]\n\t}\n\n\tprng := \u0026Xorshiftr128Plus{}\n\tprng.Seed(s1, s2)\n\treturn prng\n}\n\nfunc (x *Xorshiftr128Plus) Seed(s1, s2 uint64) {\n\tif s1 == 0 \u0026\u0026 s2 == 0 {\n\t\tpanic(\"Seeds must not both be zero\")\n\t}\n\tx.seed[0] = s1\n\tx.seed[1] = s2\n}\n\n// beUint64() decodes a uint64 from a set of eight bytes, assuming big endian encoding.\n// binary.bigEndian.Uint64, copied to avoid dependency\nfunc beUint64(b []byte) uint64 {\n\t_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808\n\treturn uint64(b[7]) | uint64(b[6])\u003c\u003c8 | uint64(b[5])\u003c\u003c16 | uint64(b[4])\u003c\u003c24 |\n\t\tuint64(b[3])\u003c\u003c32 | uint64(b[2])\u003c\u003c40 | uint64(b[1])\u003c\u003c48 | uint64(b[0])\u003c\u003c56\n}\n\n// bePutUint64() encodes a uint64 into a buffer of eight bytes.\n// binary.bigEndian.PutUint64, copied to avoid dependency\nfunc bePutUint64(b []byte, v uint64) {\n\t_ = b[7] // early bounds check to guarantee safety of writes below\n\tb[0] = byte(v \u003e\u003e 56)\n\tb[1] = byte(v \u003e\u003e 48)\n\tb[2] = byte(v \u003e\u003e 40)\n\tb[3] = byte(v \u003e\u003e 32)\n\tb[4] = byte(v \u003e\u003e 24)\n\tb[5] = byte(v \u003e\u003e 16)\n\tb[6] = byte(v \u003e\u003e 8)\n\tb[7] = byte(v)\n}\n\n// A label to identify the marshalled data.\nvar marshalXorshiftr128PlusLabel = []byte(\"xorshiftr128+:\")\n\n// MarshalBinary() returns a byte array that encodes the state of the PRNG. This can later be used\n// with UnmarshalBinary() to restore the state of the PRNG.\n// MarshalBinary implements the encoding.BinaryMarshaler interface.\nfunc (xs *Xorshiftr128Plus) MarshalBinary() ([]byte, error) {\n\tb := make([]byte, 30)\n\tcopy(b, marshalXorshiftr128PlusLabel)\n\tbePutUint64(b[14:], xs.seed[0])\n\tbePutUint64(b[22:], xs.seed[1])\n\treturn b, nil\n}\n\n// errUnmarshalXorshiftr128Plus is returned when unmarshalling fails.\nvar errUnmarshalXorshiftr128Plus = errors.New(\"invalid Xorshiftr128Plus encoding\")\n\n// UnmarshalBinary() restores the state of the PRNG from a byte array that was created with MarshalBinary().\n// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.\nfunc (xs *Xorshiftr128Plus) UnmarshalBinary(data []byte) error {\n\tif len(data) != 30 || string(data[:14]) != string(marshalXorshiftr128PlusLabel) {\n\t\treturn errUnmarshalXorshiftr128Plus\n\t}\n\txs.seed[0] = beUint64(data[14:])\n\txs.seed[1] = beUint64(data[22:])\n\treturn nil\n}\n\nfunc (x *Xorshiftr128Plus) Uint64() uint64 {\n\tx0 := x.seed[0]\n\tx1 := x.seed[1]\n\tx.seed[0] = x1\n\tx0 ^= x0 \u003c\u003c 23\n\tx0 ^= x0 \u003e\u003e 17\n\tx0 ^= x1\n\tx.seed[1] = x0 + x1\n\treturn x.seed[1]\n}\n\n// Until there is better benchmarking support in gno, you can test the performance of this PRNG with this function.\n// This isn't perfect, since it will include the startup time of gno in the results, but this will give you a timing\n// for generating a million random uint64 numbers on any unix based system:\n//\n// `time gno run -expr 'benchmarkXorshiftr128Plus()' xorshiftr128plus.gno\nfunc benchmarkXorshiftr128Plus(_iterations ...int) {\n\titerations := 1000000\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\txs128p := New()\n\n\tfor i := 0; i \u003c iterations; i++ {\n\t\t_ = xs128p.Uint64()\n\t}\n\tufmt.Println(ufmt.Sprintf(\"Xorshiftr128Plus: generate %d uint64\\n\", iterations))\n}\n\n// The averageXorshiftr128Plus() function is a simple benchmarking helper to demonstrate\n// the most basic statistical property of the Xorshiftr128+ PRNG.\nfunc averageXorshiftr128Plus(_iterations ...int) {\n\ttarget := uint64(500000)\n\titerations := 1000000\n\tvar squares [1000000]uint64\n\n\tufmt.Println(\n\t\tufmt.Sprintf(\n\t\t\t\"Averaging %d random numbers. The average should be very close to %d.\\n\",\n\t\t\titerations,\n\t\t\ttarget))\n\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\txs128p := New()\n\n\tvar average float64 = 0\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tn := xs128p.Uint64()%(target*2) + 1\n\t\taverage += (float64(n) - average) / float64(i+1)\n\t\tsquares[i] = n\n\t}\n\n\tsum_of_squares := uint64(0)\n\t// transform numbers into their squares of the distance from the average\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tdifference := average - float64(squares[i])\n\t\tsquare := uint64(difference * difference)\n\t\tsum_of_squares += square\n\t}\n\n\tufmt.Println(ufmt.Sprintf(\"Xorshiftr128+ average of %d uint64: %f\\n\", iterations, average))\n\tufmt.Println(ufmt.Sprintf(\"Xorshiftr128+ standard deviation  : %f\\n\", math.Sqrt(float64(sum_of_squares)/float64(iterations))))\n\tufmt.Println(ufmt.Sprintf(\"Xorshiftr128+ theoretical perfect deviation: %f\\n\", (float64(target*2)-1)/math.Sqrt(12)))\n}\n"},{"name":"xorshiftr128plus_test.gno","body":"package xorshiftr128plus\n\nimport (\n\t\"math/rand\"\n\t\"testing\"\n)\n\nfunc TestXorshift64StarSeeding(t *testing.T) {\n\trnd := New()\n\tvalue1 := rnd.Uint64()\n\n\trnd = New(987654321)\n\tvalue2 := rnd.Uint64()\n\n\trnd = New(987654321, 9876543210)\n\tvalue3 := rnd.Uint64()\n\n\tif value1 != 4368859828809982745 ||\n\t\tvalue2 != 6152356058823566752 ||\n\t\tvalue3 != 8285073084540510 ||\n\t\tvalue1 == value2 ||\n\t\tvalue2 == value3 ||\n\t\tvalue1 == value3 {\n\t\tt.Errorf(\"Expected three different values\\n  got: %d, %d, %d\", value1, value2, value3)\n\t}\n}\n\nfunc TestXorshiftr128PlusRand(t *testing.T) {\n\trnd := New(987654321)\n\trng := rand.New(rnd)\n\n\t// Expected outputs for the first 5 random floats with the given seed\n\texpected := []float64{\n\t\t0.048735219800779106,\n\t\t0.0372152171449619,\n\t\t0.667254760531175,\n\t\t0.16615979111253953,\n\t\t0.27578895545492665,\n\t\t0.48342823127830337,\n\t\t0.7825693830495895,\n\t\t0.14643955390763952,\n\t\t0.29003469381875835,\n\t\t0.726334398545258,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rng.Float64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Rand.Float64() at iteration %d: got %g, expected %g\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc TestXorshiftr128PlusUint64(t *testing.T) {\n\trnd := New(987654321, 9876543210)\n\n\texpected := []uint64{\n\t\t8285073084540510,\n\t\t97010855169053386,\n\t\t11353359435625603792,\n\t\t10289232744262291728,\n\t\t14019961444418950453,\n\t\t15829492476941720545,\n\t\t2764732928842099222,\n\t\t6871047144273883379,\n\t\t16142204260470661970,\n\t\t11803223757041229095,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Xorshiftr128Plus.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc TestXorshiftr128PlusMarshalUnmarshal(t *testing.T) {\n\trnd := New(987654321, 9876543210)\n\n\texpected1 := []uint64{\n\t\t8285073084540510,\n\t\t97010855169053386,\n\t\t11353359435625603792,\n\t\t10289232744262291728,\n\t\t14019961444418950453,\n\t}\n\n\texpected2 := []uint64{\n\t\t15829492476941720545,\n\t\t2764732928842099222,\n\t\t6871047144273883379,\n\t\t16142204260470661970,\n\t\t11803223757041229095,\n\t}\n\n\tfor i, exp := range expected1 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Xorshiftr128Plus.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n\n\tmarshalled, err := rnd.MarshalBinary()\n\n\t// t.Logf(\"Original State: [%x]\\n\", rnd.seed)\n\t// t.Logf(\"Marshalled State: [%x] -- %v\\n\", marshalled, err)\n\tstate_before := rnd.seed\n\n\tif err != nil {\n\t\tt.Errorf(\"Xorshiftr128Plus.MarshalBinary() error: %v\", err)\n\t}\n\n\t// Advance state by one number; then check the next 5. The expectation is that they _will_ fail.\n\trnd.Uint64()\n\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp == val {\n\t\t\tt.Errorf(\"  Iteration %d matched %d; which is from iteration %d; something strange is happening.\", (i + 6), val, (i + 5))\n\t\t}\n\t}\n\n\t// t.Logf(\"State before unmarshall: [%x]\\n\", rnd.seed)\n\n\t// Now restore the state of the PRNG\n\terr = rnd.UnmarshalBinary(marshalled)\n\n\t// t.Logf(\"State after unmarshall: [%x]\\n\", rnd.seed)\n\n\tif state_before != rnd.seed {\n\t\tt.Errorf(\"States before and after marshal/unmarshal are not equal; go %x and %x\", state_before, rnd.seed)\n\t}\n\n\t// Now we should be back on track for the last 5 numbers\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Xorshiftr128Plus.Uint64() at iteration %d: got %d, expected %d\", (i + 5), val, exp)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"WslugG+I5I0Dzf9q9c6/Ph0i/pAi0FYxjt+5rNIOfn4hY6RKVwKTOJ/D2gP0Q0OIWXjUIVP7BeBNx0coQdaNCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"isaac","path":"gno.land/p/wyhaines/rand/isaac","files":[{"name":"README.md","body":"# package isaac // import \"gno.land/p/demo/math/rand/isaac\"\n\nThis is a port of the ISAAC cryptographically secure PRNG,\noriginally based on the reference implementation found at\nhttps://burtleburtle.net/bob/rand/isaacafa.html\n\nISAAC has excellent statistical properties, with long cycle times, and\nuniformly distributed, unbiased, and unpredictable number generation. It can\nnot be distinguished from real random data, and in three decades of scrutiny,\nno practical attacks have been found.\n\nThe default random number algorithm in gno was ported from Go's v2 rand\nimplementatoon, which defaults to the PCG algorithm. This algorithm is\ncommonly used in language PRNG implementations because it has modest seeding\nrequirements, and generates statistically strong randomness.\n\nThis package provides an implementation of the 32-bit ISAAC PRNG algorithm. This\nalgorithm provides very strong statistical performance, and is cryptographically\nsecure, while still being substantially faster than the default PCG\nimplementation in `math/rand`. Note that this package does implement a `Uint64()`\nfunction in order to generate a 64 bit number out of two 32 bit numbers. Doing this\nmakes the generator only slightly faster than PCG, however,\n\nNote that the approach to seeing with ISAAC is very important for best results,\nand seeding with ISAAC is not as simple as seeding with a single uint64 value.\nThe ISAAC algorithm requires a 256-element seed. If used for cryptographic\npurposes, this will likely require entropy generated off-chain for actual\ncryptographically secure seeding. For other purposes, however, one can utilize\nthe built-in seeding mechanism, which will leverage the xorshiftr128plus PRNG to\ngenerate any missing seeds if fewer than 256 are provided.\n\n\n```\nBenchmark\n---------\nPCG:         1000000 Uint64 generated in 15.58s\nISAAC:       1000000 Uint64 generated in 13.23s (uint64)\nISAAC:       1000000 Uint32 generated in 6.43s (uint32)\nRatio:       x1.18 times faster than PCG (uint64)\nRatio:       x2.42 times faster than PCG (uint32)\n```\n\nUse it directly:\n\n```\nprng = isaac.New() // pass 0 to 256 uint32 seeds; if fewer than 256 are provided, the rest\n                   // will be generated using the xorshiftr128plus PRNG.\n```\n\nOr use it as a drop-in replacement for the default PRNT in Rand:\n\n```\nsource = isaac.New()\nprng := rand.New(source)\n```\n\n# TYPES\n\n`\ntype ISAAC struct {\n\t// Has unexported fields.\n}\n`\n\n`func New(seeds ...uint32) *ISAAC`\n    ISAAC requires a large, 256-element seed. This implementation will leverage\n    the entropy package combined with the the xorshiftr128plus PRNG to generate\n    any missing seeds of fewer than the required number of arguments are\n    provided.\n\n`func (isaac *ISAAC) MarshalBinary() ([]byte, error)`\n    MarshalBinary() returns a byte array that encodes the state of the PRNG.\n    This can later be used with UnmarshalBinary() to restore the state of the\n    PRNG. MarshalBinary implements the encoding.BinaryMarshaler interface.\n\n`func (isaac *ISAAC) Seed(seed [256]uint32)`\n\n`func (isaac *ISAAC) Uint32() uint32`\n\n`func (isaac *ISAAC) Uint64() uint64`\n\n`func (isaac *ISAAC) UnmarshalBinary(data []byte) error`\n    UnmarshalBinary() restores the state of the PRNG from a byte array\n    that was created with MarshalBinary(). UnmarshalBinary implements the\n    encoding.BinaryUnmarshaler interface.\n\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/wyhaines/rand/isaac\"\ngno = \"0.9\"\n"},{"name":"isaac.gno","body":"// This is a port of the ISAAC cryptographically secure PRNG, originally based on the reference\n// implementation found at https://burtleburtle.net/bob/rand/isaacafa.html\n//\n// ISAAC has excellent statistical properties, with long cycle times, and uniformly distributed,\n// unbiased, and unpredictable number generation. It can not be distinguished from real random\n// data, and in three decades of scrutiny, no practical attacks have been found.\n//\n// The default random number algorithm in gno was ported from Go's v2 rand implementation, which\n// defaults to the PCG algorithm. This algorithm is commonly used in language PRNG implementations\n// because it has modest seeding requirements, and generates statistically strong randomness.\n//\n// This package provides an implementation of the 32-bit ISAAC PRNG algorithm. This\n// algorithm provides very strong statistical performance, and is cryptographically\n// secure, while still being substantially faster than the default PCG\n// implementation in `math/rand`. Note that this package does implement a `Uint64()`\n// function in order to generate a 64 bit number out of two 32 bit numbers. Doing this\n// makes the generator only slightly faster than PCG, however,\n//\n// Note that the approach to seeing with ISAAC is very important for best results, and seeding with\n// ISAAC is not as simple as seeding with a single uint64 value. The ISAAC algorithm requires a\n// 256-element seed. If used for cryptographic purposes, this will likely require entropy generated\n// off-chain for actual cryptographically secure seeding. For other purposes, however, one can\n// utilize the built-in seeding mechanism, which will leverage the xorshiftr128plus PRNG to generate\n// any missing seeds if fewer than 256 are provided.\n//\n//\t\tBenchmark\n//\t\t---------\n//\t\tPCG:         1000000 Uint64 generated in 15.58s\n//\t\tISAAC:       1000000 Uint64 generated in 13.23s\n//\t\tISAAC:       1000000 Uint32 generated in 6.43s\n//\t     Ratio:       x1.18 times faster than PCG (uint64)\n//\t     Ratio:       x2.42 times faster than PCG (uint32)\n//\n// Use it directly:\n//\n//\t\tprng = isaac.New() // pass 0 to 256 uint32 seeds; if fewer than 256 are provided, the rest\n//\t                    // will be generated using the xorshiftr128plus PRNG.\n//\n// Or use it as a drop-in replacement for the default PRNG in Rand:\n//\n//\tsource = isaac.New()\n//\tprng := rand.New(source)\npackage isaac\n\nimport (\n\t\"errors\"\n\t\"math\"\n\t\"math/rand\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/wyhaines/rand/xorshiftr128plus\"\n)\n\ntype ISAAC struct {\n\trandrsl    [256]uint32\n\trandcnt    uint32\n\tmm         [256]uint32\n\taa, bb, cc uint32\n\tseed       [256]uint32\n}\n\n// ISAAC requires a large, 256-element seed. This implementation will leverage the entropy\n// package combined with the the xorshiftr128plus PRNG to generate any missing seeds of\n// fewer than the required number of arguments are provided.\nfunc New(seeds ...uint32) *ISAAC {\n\tisaac := \u0026ISAAC{}\n\tseed := [256]uint32{}\n\n\tindex := 0\n\tfor index = 0; index \u003c len(seeds); index++ {\n\t\tseed[index] = seeds[index]\n\t}\n\n\tif index \u003c 4 {\n\t\te := entropy.New()\n\t\tfor ; index \u003c 4; index++ {\n\t\t\tseed[index] = e.Value()\n\t\t}\n\t}\n\n\t// Use up to the first four seeds as seeding inputs for xorshiftr128+, in order to\n\t// use it to provide any remaining missing seeds.\n\tprng := xorshiftr128plus.New(\n\t\t(uint64(seed[0])\u003c\u003c32)|uint64(seed[1]),\n\t\t(uint64(seed[2])\u003c\u003c32)|uint64(seed[3]),\n\t)\n\tfor ; index \u003c 256; index += 2 {\n\t\tval := prng.Uint64()\n\t\tseed[index] = uint32(val \u0026 0xffffffff)\n\t\tif index+1 \u003c 256 {\n\t\t\tseed[index+1] = uint32(val \u003e\u003e 32)\n\t\t}\n\t}\n\tisaac.Seed(seed)\n\treturn isaac\n}\n\nfunc (isaac *ISAAC) Seed(seed [256]uint32) {\n\tisaac.randrsl = seed\n\tisaac.seed = seed\n\tisaac.randinit(true)\n}\n\n// beUint32() decodes a uint32 from a set of four bytes, assuming big endian encoding.\n// binary.bigEndian.Uint32, copied to avoid dependency\nfunc beUint32(b []byte) uint32 {\n\t_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808\n\treturn uint32(b[3]) | uint32(b[2])\u003c\u003c8 | uint32(b[1])\u003c\u003c16 | uint32(b[0])\u003c\u003c24\n}\n\n// bePutUint32() encodes a uint64 into a buffer of eight bytes.\n// binary.bigEndian.PutUint32, copied to avoid dependency\nfunc bePutUint32(b []byte, v uint32) {\n\t_ = b[3] // early bounds check to guarantee safety of writes below\n\tb[0] = byte(v \u003e\u003e 24)\n\tb[1] = byte(v \u003e\u003e 16)\n\tb[2] = byte(v \u003e\u003e 8)\n\tb[3] = byte(v)\n}\n\n// A label to identify the marshalled data.\nvar marshalISAACLabel = []byte(\"isaac:\")\n\n// MarshalBinary() returns a byte array that encodes the state of the PRNG. This can later be used\n// with UnmarshalBinary() to restore the state of the PRNG.\n// MarshalBinary implements the encoding.BinaryMarshaler interface.\nfunc (isaac *ISAAC) MarshalBinary() ([]byte, error) {\n\tb := make([]byte, 3094) // 6 + 1024 + 1024 + 1024 + 4 + 4 + 4 + 4 == 3090\n\tcopy(b, marshalISAACLabel)\n\tfor i := 0; i \u003c 256; i++ {\n\t\tbePutUint32(b[6+i*4:], isaac.seed[i])\n\t}\n\tfor i := 256; i \u003c 512; i++ {\n\t\tbePutUint32(b[6+i*4:], isaac.randrsl[i-256])\n\t}\n\tfor i := 512; i \u003c 768; i++ {\n\t\tbePutUint32(b[6+i*4:], isaac.mm[i-512])\n\t}\n\tbePutUint32(b[3078:], isaac.aa)\n\tbePutUint32(b[3082:], isaac.bb)\n\tbePutUint32(b[3086:], isaac.cc)\n\tbePutUint32(b[3090:], isaac.randcnt)\n\n\treturn b, nil\n}\n\n// errUnmarshalISAAC is returned when unmarshalling fails.\nvar errUnmarshalISAAC = errors.New(\"invalid ISAAC encoding\")\n\n// UnmarshalBinary() restores the state of the PRNG from a byte array that was created with MarshalBinary().\n// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.\nfunc (isaac *ISAAC) UnmarshalBinary(data []byte) error {\n\tif len(data) != 3094 || string(data[:6]) != string(marshalISAACLabel) {\n\t\treturn errUnmarshalISAAC\n\t}\n\tfor i := 0; i \u003c 256; i++ {\n\t\tisaac.seed[i] = beUint32(data[6+i*4:])\n\t}\n\tfor i := 256; i \u003c 512; i++ {\n\t\tisaac.randrsl[i-256] = beUint32(data[6+i*4:])\n\t}\n\tfor i := 512; i \u003c 768; i++ {\n\t\tisaac.mm[i-512] = beUint32(data[6+i*4:])\n\t}\n\tisaac.aa = beUint32(data[3078:])\n\tisaac.bb = beUint32(data[3082:])\n\tisaac.cc = beUint32(data[3086:])\n\tisaac.randcnt = beUint32(data[3090:])\n\treturn nil\n}\n\nfunc (isaac *ISAAC) randinit(flag bool) {\n\tisaac.aa = 0\n\tisaac.bb = 0\n\tisaac.cc = 0\n\n\tvar a, b, c, d, e, f, g, h uint32 = 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9\n\n\tfor i := 0; i \u003c 4; i++ {\n\t\ta ^= b \u003c\u003c 11\n\t\td += a\n\t\tb += c\n\t\tb ^= c \u003e\u003e 2\n\t\te += b\n\t\tc += d\n\t\tc ^= d \u003c\u003c 8\n\t\tf += c\n\t\td += e\n\t\td ^= e \u003e\u003e 16\n\t\tg += d\n\t\te += f\n\t\te ^= f \u003c\u003c 10\n\t\th += e\n\t\tf += g\n\t\tf ^= g \u003e\u003e 4\n\t\ta += f\n\t\tg += h\n\t\tg ^= h \u003c\u003c 8\n\t\tb += g\n\t\th += a\n\t\th ^= a \u003e\u003e 9\n\t\tc += h\n\t\ta += b\n\t}\n\n\tfor i := 0; i \u003c 256; i += 8 {\n\t\tif flag {\n\t\t\ta += isaac.randrsl[i]\n\t\t\tb += isaac.randrsl[i+1]\n\t\t\tc += isaac.randrsl[i+2]\n\t\t\td += isaac.randrsl[i+3]\n\t\t\te += isaac.randrsl[i+4]\n\t\t\tf += isaac.randrsl[i+5]\n\t\t\tg += isaac.randrsl[i+6]\n\t\t\th += isaac.randrsl[i+7]\n\t\t}\n\n\t\ta ^= b \u003c\u003c 11\n\t\td += a\n\t\tb += c\n\t\tb ^= c \u003e\u003e 2\n\t\te += b\n\t\tc += d\n\t\tc ^= d \u003c\u003c 8\n\t\tf += c\n\t\td += e\n\t\td ^= e \u003e\u003e 16\n\t\tg += d\n\t\te += f\n\t\te ^= f \u003c\u003c 10\n\t\th += e\n\t\tf += g\n\t\tf ^= g \u003e\u003e 4\n\t\ta += f\n\t\tg += h\n\t\tg ^= h \u003c\u003c 8\n\t\tb += g\n\t\th += a\n\t\th ^= a \u003e\u003e 9\n\t\tc += h\n\t\ta += b\n\n\t\tisaac.mm[i] = a\n\t\tisaac.mm[i+1] = b\n\t\tisaac.mm[i+2] = c\n\t\tisaac.mm[i+3] = d\n\t\tisaac.mm[i+4] = e\n\t\tisaac.mm[i+5] = f\n\t\tisaac.mm[i+6] = g\n\t\tisaac.mm[i+7] = h\n\t}\n\n\tif flag {\n\t\tfor i := 0; i \u003c 256; i += 8 {\n\t\t\ta += isaac.mm[i]\n\t\t\tb += isaac.mm[i+1]\n\t\t\tc += isaac.mm[i+2]\n\t\t\td += isaac.mm[i+3]\n\t\t\te += isaac.mm[i+4]\n\t\t\tf += isaac.mm[i+5]\n\t\t\tg += isaac.mm[i+6]\n\t\t\th += isaac.mm[i+7]\n\n\t\t\ta ^= b \u003c\u003c 11\n\t\t\td += a\n\t\t\tb += c\n\t\t\tb ^= c \u003e\u003e 2\n\t\t\te += b\n\t\t\tc += d\n\t\t\tc ^= d \u003c\u003c 8\n\t\t\tf += c\n\t\t\td += e\n\t\t\td ^= e \u003e\u003e 16\n\t\t\tg += d\n\t\t\te += f\n\t\t\te ^= f \u003c\u003c 10\n\t\t\th += e\n\t\t\tf += g\n\t\t\tf ^= g \u003e\u003e 4\n\t\t\ta += f\n\t\t\tg += h\n\t\t\tg ^= h \u003c\u003c 8\n\t\t\tb += g\n\t\t\th += a\n\t\t\th ^= a \u003e\u003e 9\n\t\t\tc += h\n\t\t\ta += b\n\n\t\t\tisaac.mm[i] = a\n\t\t\tisaac.mm[i+1] = b\n\t\t\tisaac.mm[i+2] = c\n\t\t\tisaac.mm[i+3] = d\n\t\t\tisaac.mm[i+4] = e\n\t\t\tisaac.mm[i+5] = f\n\t\t\tisaac.mm[i+6] = g\n\t\t\tisaac.mm[i+7] = h\n\t\t}\n\t}\n\n\tisaac.isaac()\n\tisaac.randcnt = uint32(256)\n}\n\nfunc (isaac *ISAAC) isaac() {\n\tisaac.cc++\n\tisaac.bb += isaac.cc\n\n\tfor i := 0; i \u003c 256; i++ {\n\t\tx := isaac.mm[i]\n\t\tswitch i % 4 {\n\t\tcase 0:\n\t\t\tisaac.aa ^= isaac.aa \u003c\u003c 13\n\t\tcase 1:\n\t\t\tisaac.aa ^= isaac.aa \u003e\u003e 6\n\t\tcase 2:\n\t\t\tisaac.aa ^= isaac.aa \u003c\u003c 2\n\t\tcase 3:\n\t\t\tisaac.aa ^= isaac.aa \u003e\u003e 16\n\t\t}\n\t\tisaac.aa += isaac.mm[(i+128)\u00260xff]\n\n\t\ty := isaac.mm[(x\u003e\u003e2)\u00260xff] + isaac.aa + isaac.bb\n\t\tisaac.mm[i] = y\n\t\tisaac.bb = isaac.mm[(y\u003e\u003e10)\u00260xff] + x\n\t\tisaac.randrsl[i] = isaac.bb\n\t}\n}\n\n// Returns a random uint32.\nfunc (isaac *ISAAC) Uint32() uint32 {\n\tif isaac.randcnt == uint32(0) {\n\t\tisaac.isaac()\n\t\tisaac.randcnt = uint32(256)\n\t}\n\tisaac.randcnt--\n\treturn isaac.randrsl[isaac.randcnt]\n}\n\n// Returns a random uint64 by combining two uint32s.\nfunc (isaac *ISAAC) Uint64() uint64 {\n\treturn uint64(isaac.Uint32()) | (uint64(isaac.Uint32()) \u003c\u003c 32)\n}\n\n// Until there is better benchmarking support in gno, you can test the performance of this PRNG with this function.\n// This isn't perfect, since it will include the startup time of gno in the results, but this will give you a timing\n// for generating a million random uint64 numbers on any unix based system:\n//\n// `time gno run -expr 'benchmarkISAAC()' xorshift64star.gno\nfunc benchmarkISAAC(_iterations ...int) {\n\titerations := 1000000\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\tisaac := New()\n\n\tfor i := 0; i \u003c iterations; i++ {\n\t\t_ = isaac.Uint64()\n\t}\n\tufmt.Println(ufmt.Sprintf(\"ISAAC: generate %d uint64\\n\", iterations))\n}\n\n// The averageISAAC() function is a simple benchmarking helper to demonstrate\n// the most basic statistical property of the ISAAC PRNG.\nfunc averageISAAC(_iterations ...int) {\n\ttarget := uint64(500000)\n\titerations := 1000000\n\tvar squares [1000000]uint64\n\n\tufmt.Println(\n\t\tufmt.Sprintf(\n\t\t\t\"Averaging %d random numbers. The average should be very close to %d.\\n\",\n\t\t\titerations,\n\t\t\ttarget))\n\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\tisaac := New(987654321, 123456789, 999999999, 111111111)\n\n\tvar average float64 = 0\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tn := isaac.Uint64()%(target*2) + 1\n\t\taverage += (float64(n) - average) / float64(i+1)\n\t\tsquares[i] = n\n\t}\n\n\tsum_of_squares := uint64(0)\n\t// transform numbers into their squares of the distance from the average\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tdifference := average - float64(squares[i])\n\t\tsquare := uint64(difference * difference)\n\t\tsum_of_squares += square\n\t}\n\n\tufmt.Println(ufmt.Sprintf(\"ISAAC average of %d uint64: %f\\n\", iterations, average))\n\tufmt.Println(ufmt.Sprintf(\"ISAAC standard deviation  : %f\\n\", math.Sqrt(float64(sum_of_squares)/float64(iterations))))\n\tufmt.Println(ufmt.Sprintf(\"ISAAC theoretical perfect deviation: %f\\n\", (float64(target*2)-1)/math.Sqrt(12)))\n}\n\nfunc averagePCG(_iterations ...int) {\n\ttarget := uint64(500000)\n\titerations := 1000000\n\tvar squares [1000000]uint64\n\n\tufmt.Println(\n\t\tufmt.Sprintf(\n\t\t\t\"Averaging %d random numbers. The average should be very close to %d.\\n\",\n\t\t\titerations,\n\t\t\ttarget))\n\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\tisaac := rand.NewPCG(987654321, 123456789)\n\n\tvar average float64 = 0\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tn := isaac.Uint64()%(target*2) + 1\n\t\taverage += (float64(n) - average) / float64(i+1)\n\t\tsquares[i] = n\n\t}\n\n\tsum_of_squares := uint64(0)\n\t// transform numbers into their squares of the distance from the average\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tdifference := average - float64(squares[i])\n\t\tsquare := uint64(difference * difference)\n\t\tsum_of_squares += square\n\t}\n\n\tufmt.Println(ufmt.Sprintf(\"PCG average of %d uint64: %f\\n\", iterations, average))\n\tufmt.Println(ufmt.Sprintf(\"PCG standard deviation  : %f\\n\", math.Sqrt(float64(sum_of_squares)/float64(iterations))))\n\tufmt.Println(ufmt.Sprintf(\"PCG theoretical perfect deviation: %f\\n\", (float64(target*2)-1)/math.Sqrt(12)))\n}\n"},{"name":"isaac_test.gno","body":"package isaac\n\nimport (\n\t\"math/rand\"\n\t\"testing\"\n)\n\ntype OpenISAAC struct {\n\tRandrsl    [256]uint32\n\tRandcnt    uint32\n\tMm         [256]uint32\n\tAa, Bb, Cc uint32\n\tSeed       [256]uint32\n}\n\nfunc TestISAACSeeding(t *testing.T) {\n\t_ = New()\n}\n\nfunc TestISAACRand(t *testing.T) {\n\trnd := New(987654321)\n\trng := rand.New(rnd)\n\n\texpected := []float64{\n\t\t0.3590173976876423,\n\t\t0.7045500585814575,\n\t\t0.3307624938209778,\n\t\t0.9174646414250772,\n\t\t0.11232269485263391,\n\t\t0.9276658847827113,\n\t\t0.9561549853128902,\n\t\t0.3921638978394879,\n\t\t0.9824881209760224,\n\t\t0.8213784955963486,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rng.Float64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Rand.Float64() at iteration %d: got %g, expected %g\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc TestISAACUint64(t *testing.T) {\n\trnd := New(1000)\n\n\texpected := []uint64{\n\t\t13706738165129397958,\n\t\t11158865851759859683,\n\t\t7282911433372880595,\n\t\t10191257834247701829,\n\t\t6756510588635422211,\n\t\t9188469355127567259,\n\t\t3870407692778398450,\n\t\t7510499000403643056,\n\t\t11921506945015596058,\n\t\t5594436529078496461,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"ISAAC.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc dupState(i *ISAAC) *OpenISAAC {\n\tstate := \u0026OpenISAAC{}\n\tstate.Seed = i.seed\n\tstate.Randrsl = i.randrsl\n\tstate.Mm = i.mm\n\tstate.Aa = i.aa\n\tstate.Bb = i.bb\n\tstate.Cc = i.cc\n\tstate.Randcnt = i.randcnt\n\n\treturn state\n}\n\nfunc TestISAACMarshalUnmarshal(t *testing.T) {\n\trnd := New(1001)\n\n\texpected1 := []uint64{\n\t\t15520355889829550420,\n\t\t14048062122838424762,\n\t\t17386984608929258959,\n\t\t6631892771034928162,\n\t\t10939419587267807737,\n\t}\n\n\texpected2 := []uint64{\n\t\t3835981378089717525,\n\t\t13955658256492919532,\n\t\t10077846634918708781,\n\t\t5773204650675356768,\n\t\t4971527406542890875,\n\t}\n\n\tfor i, exp := range expected1 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"ISAAC.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n\n\tmarshalled, err := rnd.MarshalBinary()\n\n\t// t.Logf(\"State: [%v]\\n\", dupState(rnd))\n\t// t.Logf(\"Marshalled State: [%x] -- %v\\n\", marshalled, err)\n\tstate_before := dupState(rnd)\n\n\tif err != nil {\n\t\tt.Errorf(\"ISAAC.MarshalBinary() error: %v\", err)\n\t}\n\n\t// Advance state by one number; then check the next 5. The expectation is that they _will_ fail.\n\trnd.Uint64()\n\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp == val {\n\t\t\tt.Errorf(\"  Iteration %d matched %d; which is from iteration %d; something strange is happening.\", (i + 6), val, (i + 5))\n\t\t}\n\t}\n\n\t// t.Logf(\"State before unmarshall: [%v]\\n\", dupState(rnd))\n\n\t// Now restore the state of the PRNG\n\terr = rnd.UnmarshalBinary(marshalled)\n\n\t// t.Logf(\"State after unmarshall: [%v]\\n\", dupState(rnd))\n\n\tif state_before.Seed != dupState(rnd).Seed {\n\t\tt.Errorf(\"Seed mismatch\")\n\t}\n\tif state_before.Randrsl != dupState(rnd).Randrsl {\n\t\tt.Errorf(\"Randrsl mismatch\")\n\t}\n\tif state_before.Mm != dupState(rnd).Mm {\n\t\tt.Errorf(\"Mm mismatch\")\n\t}\n\tif state_before.Aa != dupState(rnd).Aa {\n\t\tt.Errorf(\"Aa mismatch\")\n\t}\n\tif state_before.Bb != dupState(rnd).Bb {\n\t\tt.Errorf(\"Bb mismatch\")\n\t}\n\tif state_before.Cc != dupState(rnd).Cc {\n\t\tt.Errorf(\"Cc mismatch\")\n\t}\n\tif state_before.Randcnt != dupState(rnd).Randcnt {\n\t\tt.Errorf(\"Randcnt mismatch\")\n\t}\n\n\t// Now we should be back on track for the last 5 numbers\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"ISAAC.Uint64() at iteration %d: got %d, expected %d\", (i + 5), val, exp)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"NAtRkTS5Y8KM5vY8KhCD7QtusFYzjuSLVqMM+le0fMapid7IyXaj+KpUW73lvhDHcrwuirJXnXtKGKXC+3bFBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"isaac64","path":"gno.land/p/wyhaines/rand/isaac64","files":[{"name":"README.md","body":"# package isaac64 // import \"gno.land/p/demo/math/rand/isaac64\"\n\nThis is a port of the 64-bit version of the ISAAC cryptographically\nsecure PRNG, originally based on the reference implementation found at\nhttps://burtleburtle.net/bob/rand/isaacafa.html\n\nISAAC has excellent statistical properties, with long cycle times, and\nuniformly distributed, unbiased, and unpredictable number generation. It can\nnot be distinguished from real random data, and in three decades of scrutiny,\nno practical attacks have been found.\n\nThe default random number algorithm in gno was ported from Go's v2 rand\nimplementatoon, which defaults to the PCG algorithm. This algorithm is\ncommonly used in language PRNG implementations because it has modest seeding\nrequirements, and generates statistically strong randomness.\n\nThis package provides an implementation of the 64-bit ISAAC PRNG algorithm. This\nalgorithm provides very strong statistical performance, and is cryptographically\nsecure, while still being substantially faster than the default PCG\nimplementation in `math/rand`.\n\nNote that the approach to seeing with ISAAC is very important for best results,\nand seeding with ISAAC is not as simple as seeding with a single uint64 value.\nThe ISAAC algorithm requires a 256-element seed. If used for cryptographic\npurposes, this will likely require entropy generated off-chain for actual\ncryptographically secure seeding. For other purposes, however, one can utilize\nthe built-in seeding mechanism, which will leverage the xorshiftr128plus PRNG to\ngenerate any missing seeds if fewer than 256 are provided.\n\n\n```\nBenchmark\n---------\nPCG:         1000000 Uint64 generated in 15.58s\nISAAC:       1000000 Uint64 generated in 8.95s\nISAAC:       1000000 Uint32 generated in 7.66s\nRatio:       x1.74 times faster than PCG (uint64)\nRatio:       x2.03 times faster than PCG (uint32)\n```\n\nUse it directly:\n\n\n```\nprng = isaac.New() // pass 0 to 256 uint64 seeds; if fewer than 256 are provided, the rest\n                   // will be generated using the xorshiftr128plus PRNG.\n```\n\nOr use it as a drop-in replacement for the default PRNT in Rand:\n\n```\nsource = isaac64.New()\nprng := rand.New(source)\n```\n\n## CONSTANTS\n\n\n```\nconst (\n\tRANDSIZL = 8\n\tRANDSIZ  = 1 \u003c\u003c RANDSIZL // 256\n)\n```\n\n## TYPES\n\n\n```\ntype ISAAC struct {\n\t// Has unexported fields.\n}\n```\n\n`func New(seeds ...uint64) *ISAAC`\nISAAC requires a large, 256-element seed. This implementation will leverage\nthe entropy package combined with the xorshiftr128plus PRNG to generate any\nmissing seeds if fewer than the required number of arguments are provided.\n\n`func (isaac *ISAAC) MarshalBinary() ([]byte, error)`\nMarshalBinary() returns a byte array that encodes the state of the PRNG.\nThis can later be used with UnmarshalBinary() to restore the state of the\nPRNG. MarshalBinary implements the encoding.BinaryMarshaler interface.\n\n`func (isaac *ISAAC) Seed(seed [256]uint64)`\nReinitialize the generator with a new seed. A seed must be composed of 256 uint64.\n\n`func (isaac *ISAAC) Uint32() uint32`\nReturn a 32 bit random integer, composed of the high 32 bits of the generated 32 bit result.\n\n`func (isaac *ISAAC) Uint64() uint64`\nReturn a 64 bit random integer.\n\n`func (isaac *ISAAC) UnmarshalBinary(data []byte) error`\nUnmarshalBinary() restores the state of the PRNG from a byte array\nthat was created with MarshalBinary(). UnmarshalBinary implements the\nencoding.BinaryUnmarshaler interface.\n"},{"name":"gnomod.toml","body":"module = \"gno.land/p/wyhaines/rand/isaac64\"\ngno = \"0.9\"\n"},{"name":"isaac64.gno","body":"// This is a port of the 64-bit version of the ISAAC cryptographically secure PRNG, originally\n// based on the reference implementation found at https://burtleburtle.net/bob/rand/isaacafa.html\n//\n// ISAAC has excellent statistical properties, with long cycle times, and uniformly distributed,\n// unbiased, and unpredictable number generation. It can not be distinguished from real random\n// data, and in three decades of scrutiny, no practical attacks have been found.\n//\n// The default random number algorithm in gno was ported from Go's v2 rand implementatoon, which\n// defaults to the PCG algorithm. This algorithm is commonly used in language PRNG implementations\n// because it has modest seeding requirements, and generates statistically strong randomness.\n//\n// This package provides an implementation of the 64-bit ISAAC PRNG algorithm. This algorithm\n// provides very strong statistical performance, and is cryptographically secure, while still\n// being substantially faster than the default PCG implementation in `math/rand`.\n//\n// Note that the approach to seeing with ISAAC is very important for best results, and seeding with\n// ISAAC is not as simple as seeding with a single uint64 value. The ISAAC algorithm requires a\n// 256-element seed. If used for cryptographic purposes, this will likely require entropy generated\n// off-chain for actual cryptographically secure seeding. For other purposes, however, one can\n// utilize the built-in seeding mechanism, which will leverage the xorshiftr128plus PRNG to generate\n// any missing seeds if fewer than 256 are provided.\n//\n//\t\tBenchmark\n//\t\t---------\n//\t\tPCG:         1000000 Uint64 generated in 15.58s\n//\t\tISAAC:       1000000 Uint64 generated in 8.95s\n//\t        ISAAC:       1000000 Uint32 generated in 7.66s\n//\t\tRatio:       x1.74 times faster than PCG (uint64)\n//\t        Ratio:       x2.03 times faster than PCG (uint32)\n//\n// Use it directly:\n//\n//\t\tprng = isaac.New() // pass 0 to 256 uint64 seeds; if fewer than 256 are provided, the rest\n//\t                    // will be generated using the xorshiftr128plus PRNG.\n//\n// Or use it as a drop-in replacement for the default PRNT in Rand:\n//\n//\tsource = isaac64.New()\n//\tprng := rand.New(source)\npackage isaac64\n\nimport (\n\t\"errors\"\n\t\"math\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/wyhaines/rand/xorshiftr128plus\"\n)\n\nconst (\n\tRANDSIZL = 8\n\tRANDSIZ  = 1 \u003c\u003c RANDSIZL // 256\n)\n\ntype ISAAC struct {\n\trandrsl    [256]uint64\n\trandcnt    uint64\n\tmm         [256]uint64\n\taa, bb, cc uint64\n\tseed       [256]uint64\n}\n\n// ISAAC requires a large, 256-element seed. This implementation will leverage the entropy\n// package combined with the xorshiftr128plus PRNG to generate any missing seeds if fewer than\n// the required number of arguments are provided.\nfunc New(seeds ...uint64) *ISAAC {\n\tisaac := \u0026ISAAC{}\n\tseed := [256]uint64{}\n\n\tindex := 0\n\tfor index = 0; index \u003c len(seeds) \u0026\u0026 index \u003c 256; index++ {\n\t\tseed[index] = seeds[index]\n\t}\n\n\tif index \u003c 2 {\n\t\te := entropy.New()\n\t\tfor ; index \u003c 2; index++ {\n\t\t\tseed[index] = e.Value64()\n\t\t}\n\t}\n\n\t// Use the first two seeds as seeding inputs for xorshiftr128plus, in order to\n\t// use it to provide any remaining missing seeds.\n\tprng := xorshiftr128plus.New(\n\t\tseed[0],\n\t\tseed[1],\n\t)\n\tfor ; index \u003c 256; index++ {\n\t\tseed[index] = prng.Uint64()\n\t}\n\tisaac.Seed(seed)\n\treturn isaac\n}\n\n// Reinitialize the generator with a new seed. A seed must be composed of 256 uint64.\nfunc (isaac *ISAAC) Seed(seed [256]uint64) {\n\tisaac.randrsl = seed\n\tisaac.seed = seed\n\tisaac.randinit(true)\n}\n\n// beUint64() decodes a uint64 from a set of eight bytes, assuming big endian encoding.\nfunc beUint64(b []byte) uint64 {\n\t_ = b[7] // bounds check hint to compiler\n\treturn uint64(b[7]) | uint64(b[6])\u003c\u003c8 | uint64(b[5])\u003c\u003c16 | uint64(b[4])\u003c\u003c24 |\n\t\tuint64(b[3])\u003c\u003c32 | uint64(b[2])\u003c\u003c40 | uint64(b[1])\u003c\u003c48 | uint64(b[0])\u003c\u003c56\n}\n\n// bePutUint64() encodes a uint64 into a buffer of eight bytes.\nfunc bePutUint64(b []byte, v uint64) {\n\t_ = b[7] // early bounds check to guarantee safety of writes below\n\tb[0] = byte(v \u003e\u003e 56)\n\tb[1] = byte(v \u003e\u003e 48)\n\tb[2] = byte(v \u003e\u003e 40)\n\tb[3] = byte(v \u003e\u003e 32)\n\tb[4] = byte(v \u003e\u003e 24)\n\tb[5] = byte(v \u003e\u003e 16)\n\tb[6] = byte(v \u003e\u003e 8)\n\tb[7] = byte(v)\n}\n\n// A label to identify the marshalled data.\nvar marshalISAACLabel = []byte(\"isaac:\")\n\n// MarshalBinary() returns a byte array that encodes the state of the PRNG. This can later be used\n// with UnmarshalBinary() to restore the state of the PRNG.\n// MarshalBinary implements the encoding.BinaryMarshaler interface.\nfunc (isaac *ISAAC) MarshalBinary() ([]byte, error) {\n\tb := make([]byte, 6+2048*3+8*3+8) // 6 + 2048*3 + 8*3 + 8 == 6182\n\tcopy(b, marshalISAACLabel)\n\toffset := 6\n\tfor i := 0; i \u003c 256; i++ {\n\t\tbePutUint64(b[offset:], isaac.seed[i])\n\t\toffset += 8\n\t}\n\tfor i := 0; i \u003c 256; i++ {\n\t\tbePutUint64(b[offset:], isaac.randrsl[i])\n\t\toffset += 8\n\t}\n\tfor i := 0; i \u003c 256; i++ {\n\t\tbePutUint64(b[offset:], isaac.mm[i])\n\t\toffset += 8\n\t}\n\tbePutUint64(b[offset:], isaac.aa)\n\toffset += 8\n\tbePutUint64(b[offset:], isaac.bb)\n\toffset += 8\n\tbePutUint64(b[offset:], isaac.cc)\n\toffset += 8\n\tbePutUint64(b[offset:], isaac.randcnt)\n\treturn b, nil\n}\n\n// errUnmarshalISAAC is returned when unmarshalling fails.\nvar errUnmarshalISAAC = errors.New(\"invalid ISAAC encoding\")\n\n// UnmarshalBinary() restores the state of the PRNG from a byte array that was created with MarshalBinary().\n// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.\nfunc (isaac *ISAAC) UnmarshalBinary(data []byte) error {\n\tif len(data) != 6182 || string(data[:6]) != string(marshalISAACLabel) {\n\t\treturn errUnmarshalISAAC\n\t}\n\toffset := 6\n\tfor i := 0; i \u003c 256; i++ {\n\t\tisaac.seed[i] = beUint64(data[offset:])\n\t\toffset += 8\n\t}\n\tfor i := 0; i \u003c 256; i++ {\n\t\tisaac.randrsl[i] = beUint64(data[offset:])\n\t\toffset += 8\n\t}\n\tfor i := 0; i \u003c 256; i++ {\n\t\tisaac.mm[i] = beUint64(data[offset:])\n\t\toffset += 8\n\t}\n\tisaac.aa = beUint64(data[offset:])\n\toffset += 8\n\tisaac.bb = beUint64(data[offset:])\n\toffset += 8\n\tisaac.cc = beUint64(data[offset:])\n\toffset += 8\n\tisaac.randcnt = beUint64(data[offset:])\n\treturn nil\n}\n\nfunc (isaac *ISAAC) randinit(flag bool) {\n\tvar a, b, c, d, e, f, g, h uint64\n\tisaac.aa = 0\n\tisaac.bb = 0\n\tisaac.cc = 0\n\n\ta = 0x9e3779b97f4a7c13\n\tb = 0x9e3779b97f4a7c13\n\tc = 0x9e3779b97f4a7c13\n\td = 0x9e3779b97f4a7c13\n\te = 0x9e3779b97f4a7c13\n\tf = 0x9e3779b97f4a7c13\n\tg = 0x9e3779b97f4a7c13\n\th = 0x9e3779b97f4a7c13\n\n\t// scramble it\n\tfor i := 0; i \u003c 4; i++ {\n\t\tmix(\u0026a, \u0026b, \u0026c, \u0026d, \u0026e, \u0026f, \u0026g, \u0026h)\n\t}\n\n\t// fill in mm[] with messy stuff\n\tfor i := 0; i \u003c RANDSIZ; i += 8 {\n\t\tif flag {\n\t\t\ta += isaac.randrsl[i]\n\t\t\tb += isaac.randrsl[i+1]\n\t\t\tc += isaac.randrsl[i+2]\n\t\t\td += isaac.randrsl[i+3]\n\t\t\te += isaac.randrsl[i+4]\n\t\t\tf += isaac.randrsl[i+5]\n\t\t\tg += isaac.randrsl[i+6]\n\t\t\th += isaac.randrsl[i+7]\n\t\t}\n\t\tmix(\u0026a, \u0026b, \u0026c, \u0026d, \u0026e, \u0026f, \u0026g, \u0026h)\n\t\tisaac.mm[i] = a\n\t\tisaac.mm[i+1] = b\n\t\tisaac.mm[i+2] = c\n\t\tisaac.mm[i+3] = d\n\t\tisaac.mm[i+4] = e\n\t\tisaac.mm[i+5] = f\n\t\tisaac.mm[i+6] = g\n\t\tisaac.mm[i+7] = h\n\t}\n\n\tif flag {\n\t\t// do a second pass to make all of the seed affect all of mm\n\t\tfor i := 0; i \u003c RANDSIZ; i += 8 {\n\t\t\ta += isaac.mm[i]\n\t\t\tb += isaac.mm[i+1]\n\t\t\tc += isaac.mm[i+2]\n\t\t\td += isaac.mm[i+3]\n\t\t\te += isaac.mm[i+4]\n\t\t\tf += isaac.mm[i+5]\n\t\t\tg += isaac.mm[i+6]\n\t\t\th += isaac.mm[i+7]\n\t\t\tmix(\u0026a, \u0026b, \u0026c, \u0026d, \u0026e, \u0026f, \u0026g, \u0026h)\n\t\t\tisaac.mm[i] = a\n\t\t\tisaac.mm[i+1] = b\n\t\t\tisaac.mm[i+2] = c\n\t\t\tisaac.mm[i+3] = d\n\t\t\tisaac.mm[i+4] = e\n\t\t\tisaac.mm[i+5] = f\n\t\t\tisaac.mm[i+6] = g\n\t\t\tisaac.mm[i+7] = h\n\t\t}\n\t}\n\n\tisaac.isaac()\n\tisaac.randcnt = RANDSIZ\n}\n\nfunc mix(a, b, c, d, e, f, g, h *uint64) {\n\t*a -= *e\n\t*f ^= *h \u003e\u003e 9\n\t*h += *a\n\n\t*b -= *f\n\t*g ^= *a \u003c\u003c 9\n\t*a += *b\n\n\t*c -= *g\n\t*h ^= *b \u003e\u003e 23\n\t*b += *c\n\n\t*d -= *h\n\t*a ^= *c \u003c\u003c 15\n\t*c += *d\n\n\t*e -= *a\n\t*b ^= *d \u003e\u003e 14\n\t*d += *e\n\n\t*f -= *b\n\t*c ^= *e \u003c\u003c 20\n\t*e += *f\n\n\t*g -= *c\n\t*d ^= *f \u003e\u003e 17\n\t*f += *g\n\n\t*h -= *d\n\t*e ^= *g \u003c\u003c 14\n\t*g += *h\n}\n\nfunc ind(mm []uint64, x uint64) uint64 {\n\treturn mm[(x\u003e\u003e3)\u0026(RANDSIZ-1)]\n}\n\nfunc (isaac *ISAAC) isaac() {\n\tvar a, b, x, y uint64\n\ta = isaac.aa\n\tb = isaac.bb + isaac.cc + 1\n\tisaac.cc++\n\n\tm := isaac.mm[:]\n\tr := isaac.randrsl[:]\n\n\tvar i, m2Index int\n\n\t// First half\n\tfor i = 0; i \u003c RANDSIZ/2; i++ {\n\t\tm2Index = i + RANDSIZ/2\n\t\tswitch i % 4 {\n\t\tcase 0:\n\t\t\ta = ^(a ^ (a \u003c\u003c 21)) + m[m2Index]\n\t\tcase 1:\n\t\t\ta = (a ^ (a \u003e\u003e 5)) + m[m2Index]\n\t\tcase 2:\n\t\t\ta = (a ^ (a \u003c\u003c 12)) + m[m2Index]\n\t\tcase 3:\n\t\t\ta = (a ^ (a \u003e\u003e 33)) + m[m2Index]\n\t\t}\n\t\tx = m[i]\n\t\ty = ind(m, x) + a + b\n\t\tm[i] = y\n\t\tb = ind(m, y\u003e\u003eRANDSIZL) + x\n\t\tr[i] = b\n\t}\n\n\t// Second half\n\tfor i = RANDSIZ / 2; i \u003c RANDSIZ; i++ {\n\t\tm2Index = i - RANDSIZ/2\n\t\tswitch i % 4 {\n\t\tcase 0:\n\t\t\ta = ^(a ^ (a \u003c\u003c 21)) + m[m2Index]\n\t\tcase 1:\n\t\t\ta = (a ^ (a \u003e\u003e 5)) + m[m2Index]\n\t\tcase 2:\n\t\t\ta = (a ^ (a \u003c\u003c 12)) + m[m2Index]\n\t\tcase 3:\n\t\t\ta = (a ^ (a \u003e\u003e 33)) + m[m2Index]\n\t\t}\n\t\tx = m[i]\n\t\ty = ind(m, x) + a + b\n\t\tm[i] = y\n\t\tb = ind(m, y\u003e\u003eRANDSIZL) + x\n\t\tr[i] = b\n\t}\n\n\tisaac.bb = b\n\tisaac.aa = a\n}\n\n// Return a 64 bit random integer.\nfunc (isaac *ISAAC) Uint64() uint64 {\n\tif isaac.randcnt == 0 {\n\t\tisaac.isaac()\n\t\tisaac.randcnt = RANDSIZ\n\t}\n\tisaac.randcnt--\n\treturn isaac.randrsl[isaac.randcnt]\n}\n\nvar gencycle int = 0\nvar bufferFor32 uint64 = uint64(0)\n\n// Return a 32 bit random integer, composed of the high 32 bits of the generated 32 bit result.\nfunc (isaac *ISAAC) Uint32() uint32 {\n\tif gencycle == 0 {\n\t\tbufferFor32 = isaac.Uint64()\n\t\tgencycle = 1\n\t\treturn uint32(bufferFor32 \u003e\u003e 32)\n\t}\n\n\tgencycle = 0\n\treturn uint32(bufferFor32 \u0026 0xffffffff)\n}\n\n// Until there is better benchmarking support in gno, you can test the performance of this PRNG with this function.\n// This isn't perfect, since it will include the startup time of gno in the results, but this will give you a timing\n// for generating a million random uint64 numbers on any unix based system:\n//\n// `time gno run -expr 'benchmarkISAAC()' isaac64.gno\nfunc benchmarkISAAC(_iterations ...int) {\n\titerations := 1000000\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\tisaac := New()\n\n\tfor i := 0; i \u003c iterations; i++ {\n\t\t_ = isaac.Uint64()\n\t}\n\tufmt.Println(ufmt.Sprintf(\"ISAAC: generated %d uint64\\n\", iterations))\n}\n\n// The averageISAAC() function is a simple benchmarking helper to demonstrate\n// the most basic statistical property of the ISAAC PRNG.\nfunc averageISAAC(_iterations ...int) {\n\ttarget := uint64(500000)\n\titerations := 1000000\n\n\tufmt.Println(\n\t\tufmt.Sprintf(\n\t\t\t\"Averaging %d random numbers. The average should be very close to %d.\\n\",\n\t\t\titerations,\n\t\t\ttarget))\n\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\tisaac := New(987654321987654321, 123456789987654321, 1, 997755331886644220)\n\n\tvar average float64 = 0\n\tvar squares []uint64 = make([]uint64, iterations)\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tn := isaac.Uint64()%(target*2) + 1\n\t\taverage += (float64(n) - average) / float64(i+1)\n\t\tsquares[i] = n\n\t}\n\n\tsum_of_squares := uint64(0)\n\t// transform numbers into their squares of the distance from the average\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tdifference := average - float64(squares[i])\n\t\tsquare := uint64(difference * difference)\n\t\tsum_of_squares += square\n\t}\n\n\tufmt.Println(ufmt.Sprintf(\"ISAAC average of %d uint64: %f\\n\", iterations, average))\n\tufmt.Println(ufmt.Sprintf(\"ISAAC standard deviation  : %f\\n\", math.Sqrt(float64(sum_of_squares)/float64(iterations))))\n\tufmt.Println(ufmt.Sprintf(\"ISAAC theoretical perfect deviation: %f\\n\", (float64(target*2)-1)/math.Sqrt(12)))\n}\n"},{"name":"isaac64_test.gno","body":"package isaac64\n\nimport (\n\t\"math/rand\"\n\t\"testing\"\n)\n\ntype OpenISAAC struct {\n\tRandrsl    [256]uint64\n\tRandcnt    uint64\n\tMm         [256]uint64\n\tAa, Bb, Cc uint64\n\tSeed       [256]uint64\n}\n\nfunc TestISAACSeeding(t *testing.T) {\n\t_ = New()\n}\n\nfunc TestISAACRand(t *testing.T) {\n\trnd := New(987654321)\n\trng := rand.New(rnd)\n\n\t// Expected outputs for the first 5 random floats with the given seed\n\texpected := []float64{\n\t\t0.2818878834295122,\n\t\t0.8575461830821571,\n\t\t0.9878021063787968,\n\t\t0.6503544780116336,\n\t\t0.5158329690433359,\n\t\t0.7959152461588924,\n\t\t0.5432366486934906,\n\t\t0.824665978209607,\n\t\t0.8615372170680458,\n\t\t0.22954589404739578,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rng.Float64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Rand.Float64() at iteration %d: got %g, expected %g\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc TestISAACUint64(t *testing.T) {\n\trnd := New(1000)\n\n\texpected := []uint64{\n\t\t10083220283665581455,\n\t\t10039389761195725041,\n\t\t6820016387036140989,\n\t\t6784213597523088182,\n\t\t13120722600477653778,\n\t\t3491117614651563646,\n\t\t1297676147275528930,\n\t\t15006384980354042338,\n\t\t3104467119059991036,\n\t\t4914319123654344819,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"ISAAC.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc dupState(i *ISAAC) *OpenISAAC {\n\tstate := \u0026OpenISAAC{}\n\tstate.Seed = i.seed\n\tstate.Randrsl = i.randrsl\n\tstate.Mm = i.mm\n\tstate.Aa = i.aa\n\tstate.Bb = i.bb\n\tstate.Cc = i.cc\n\tstate.Randcnt = i.randcnt\n\n\treturn state\n}\n\nfunc TestISAACMarshalUnmarshal(t *testing.T) {\n\trnd := New(1001)\n\n\texpected1 := []uint64{\n\t\t4398183556077595549,\n\t\t14479654616302101831,\n\t\t15852653767232940552,\n\t\t2801765968457115882,\n\t\t8875575139772470433,\n\t}\n\n\texpected2 := []uint64{\n\t\t17583056722733587141,\n\t\t16906215529544723388,\n\t\t7599862885469865851,\n\t\t9623269843822592805,\n\t\t4311429062865512072,\n\t}\n\n\tfor i, exp := range expected1 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"ISAAC.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n\n\tmarshalled, err := rnd.MarshalBinary()\n\n\t// t.Logf(\"State: [%v]\\n\", dupState(rnd))\n\t// t.Logf(\"Marshalled State: [%x] -- %v\\n\", marshalled, err)\n\tstate_before := dupState(rnd)\n\n\tif err != nil {\n\t\tt.Errorf(\"ISAAC.MarshalBinary() error: %v\", err)\n\t}\n\n\t// Advance state by one number; then check the next 5. The expectation is that they _will_ fail.\n\trnd.Uint64()\n\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp == val {\n\t\t\tt.Errorf(\"  Iteration %d matched %d; which is from iteration %d; something strange is happening.\", (i + 6), val, (i + 5))\n\t\t}\n\t}\n\n\t// t.Logf(\"State before unmarshall: [%v]\\n\", dupState(rnd))\n\n\t// Now restore the state of the PRNG\n\terr = rnd.UnmarshalBinary(marshalled)\n\n\t// t.Logf(\"State after unmarshall: [%v]\\n\", dupState(rnd))\n\n\tif state_before.Seed != dupState(rnd).Seed {\n\t\tt.Errorf(\"Seed mismatch\")\n\t}\n\tif state_before.Randrsl != dupState(rnd).Randrsl {\n\t\tt.Errorf(\"Randrsl mismatch\")\n\t}\n\tif state_before.Mm != dupState(rnd).Mm {\n\t\tt.Errorf(\"Mm mismatch\")\n\t}\n\tif state_before.Aa != dupState(rnd).Aa {\n\t\tt.Errorf(\"Aa mismatch\")\n\t}\n\tif state_before.Bb != dupState(rnd).Bb {\n\t\tt.Errorf(\"Bb mismatch\")\n\t}\n\tif state_before.Cc != dupState(rnd).Cc {\n\t\tt.Errorf(\"Cc mismatch\")\n\t}\n\tif state_before.Randcnt != dupState(rnd).Randcnt {\n\t\tt.Errorf(\"Randcnt mismatch\")\n\t}\n\n\t// Now we should be back on track for the last 5 numbers\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"ISAAC.Uint64() at iteration %d: got %d, expected %d\", (i + 5), val, exp)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"WuT4iky0cbiThr8yn3A4Ma3Hi8tsJIYOI41EXzRSJVPCmOaNpANgOXbHjQDTcw3f5pE5li0iXogRI4ScgSEKDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"xorshift64star","path":"gno.land/p/wyhaines/rand/xorshift64star","files":[{"name":"gnomod.toml","body":"module = \"gno.land/p/wyhaines/rand/xorshift64star\"\ngno = \"0.9\"\n"},{"name":"xorshift64star.gno","body":"// Xorshift64* is a very fast psuedo-random number generation algorithm with strong\n// statistical properties.\n//\n// The default random number algorithm in gno was ported from Go's v2 rand implementatoon, which\n// defaults to the PCG algorithm. This algorithm is commonly used in language PRNG implementations\n// because it has modest seeding requirements, and generates statistically strong randomness.\n//\n// This package provides an implementation of the Xorshift64* PRNG algorithm. This algorithm provides\n// strong statistical performance with most seeds (just don't seed it with zero), and the performance\n// of this implementation in Gno is more than four times faster than the default PCG implementation in\n// `math/rand`.\n//\n//\tBenchmark\n//\t---------\n//\tPCG:         1000000 Uint64 generated in 15.58s\n//\tXorshift64*: 1000000 Uint64 generated in 3.77s\n//\tRatio:       x4.11 times faster than PCG\n//\n// Use it directly:\n//\n//\tprng = xorshift64star.New() // pass a uint64 to seed it or pass nothing to seed it with entropy\n//\n// Or use it as a drop-in replacement for the default PRNT in Rand:\n//\n//\tsource = xorshift64star.New()\n//\tprng := rand.New(source)\npackage xorshift64star\n\nimport (\n\t\"errors\"\n\t\"math\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Xorshift64Star is a PRNG that implements the Xorshift64* algorithm.\ntype Xorshift64Star struct {\n\tseed uint64\n}\n\n// New() creates a new instance of the PRNG with a given seed, which\n// should be a uint64. If no seed is provided, the PRNG will be seeded via the\n// gno.land/p/demo/entropy package.\nfunc New(seed ...uint64) *Xorshift64Star {\n\txs := \u0026Xorshift64Star{}\n\txs.Seed(seed...)\n\treturn xs\n}\n\n// Seed() implements the rand.Source interface. It provides a way to set the seed for the PRNG.\nfunc (xs *Xorshift64Star) Seed(seed ...uint64) {\n\tif len(seed) == 0 {\n\t\te := entropy.New()\n\t\txs.seed = e.Value64()\n\t} else {\n\t\txs.seed = seed[0]\n\t}\n}\n\n// beUint64() decodes a uint64 from a set of eight bytes, assuming big endian encoding.\n// binary.bigEndian.Uint64, copied to avoid dependency\nfunc beUint64(b []byte) uint64 {\n\t_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808\n\treturn uint64(b[7]) | uint64(b[6])\u003c\u003c8 | uint64(b[5])\u003c\u003c16 | uint64(b[4])\u003c\u003c24 |\n\t\tuint64(b[3])\u003c\u003c32 | uint64(b[2])\u003c\u003c40 | uint64(b[1])\u003c\u003c48 | uint64(b[0])\u003c\u003c56\n}\n\n// bePutUint64() encodes a uint64 into a buffer of eight bytes.\n// binary.bigEndian.PutUint64, copied to avoid dependency\nfunc bePutUint64(b []byte, v uint64) {\n\t_ = b[7] // early bounds check to guarantee safety of writes below\n\tb[0] = byte(v \u003e\u003e 56)\n\tb[1] = byte(v \u003e\u003e 48)\n\tb[2] = byte(v \u003e\u003e 40)\n\tb[3] = byte(v \u003e\u003e 32)\n\tb[4] = byte(v \u003e\u003e 24)\n\tb[5] = byte(v \u003e\u003e 16)\n\tb[6] = byte(v \u003e\u003e 8)\n\tb[7] = byte(v)\n}\n\n// A label to identify the marshalled data.\nvar marshalXorshift64StarLabel = []byte(\"xorshift64*:\")\n\n// MarshalBinary() returns a byte array that encodes the state of the PRNG. This can later be used\n// with UnmarshalBinary() to restore the state of the PRNG.\n// MarshalBinary implements the encoding.BinaryMarshaler interface.\nfunc (xs *Xorshift64Star) MarshalBinary() ([]byte, error) {\n\tb := make([]byte, 20)\n\tcopy(b, marshalXorshift64StarLabel)\n\tbePutUint64(b[12:], xs.seed)\n\treturn b, nil\n}\n\n// errUnmarshalXorshift64Star is returned when unmarshalling fails.\nvar errUnmarshalXorshift64Star = errors.New(\"invalid Xorshift64* encoding\")\n\n// UnmarshalBinary() restores the state of the PRNG from a byte array that was created with MarshalBinary().\n// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.\nfunc (xs *Xorshift64Star) UnmarshalBinary(data []byte) error {\n\tif len(data) != 20 || string(data[:12]) != string(marshalXorshift64StarLabel) {\n\t\treturn errUnmarshalXorshift64Star\n\t}\n\txs.seed = beUint64(data[12:])\n\treturn nil\n}\n\n// Uint64() generates the next random uint64 value.\nfunc (xs *Xorshift64Star) Uint64() uint64 {\n\txs.seed ^= xs.seed \u003e\u003e 12\n\txs.seed ^= xs.seed \u003c\u003c 25\n\txs.seed ^= xs.seed \u003e\u003e 27\n\txs.seed *= 2685821657736338717\n\treturn xs.seed // Operations naturally wrap around in uint64\n}\n\n// Until there is better benchmarking support in gno, you can test the performance of this PRNG with this function.\n// This isn't perfect, since it will include the startup time of gno in the results, but this will give you a timing\n// for generating a million random uint64 numbers on any unix based system:\n//\n// `time gno run -expr 'benchmarkXorshift64Star()' xorshift64star.gno\nfunc benchmarkXorshift64Star(_iterations ...int) {\n\titerations := 1000000\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\txs64s := New()\n\n\tfor i := 0; i \u003c iterations; i++ {\n\t\t_ = xs64s.Uint64()\n\t}\n\tufmt.Println(ufmt.Sprintf(\"Xorshift64*: generate %d uint64\\n\", iterations))\n}\n\n// The averageXorshift64Star() function is a simple benchmarking helper to demonstrate\n// the most basic statistical property of the Xorshift64* PRNG.\nfunc averageXorshift64Star(_iterations ...int) {\n\ttarget := uint64(500000)\n\titerations := 1000000\n\tvar squares [1000000]uint64\n\n\tufmt.Println(\n\t\tufmt.Sprintf(\n\t\t\t\"Averaging %d random numbers. The average should be very close to %d.\\n\",\n\t\t\titerations,\n\t\t\ttarget))\n\n\tif len(_iterations) \u003e 0 {\n\t\titerations = _iterations[0]\n\t}\n\txs64s := New()\n\n\tvar average float64 = 0\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tn := xs64s.Uint64()%(target*2) + 1\n\t\taverage += (float64(n) - average) / float64(i+1)\n\t\tsquares[i] = n\n\t}\n\n\tsum_of_squares := uint64(0)\n\t// transform numbers into their squares of the distance from the average\n\tfor i := 0; i \u003c iterations; i++ {\n\t\tdifference := average - float64(squares[i])\n\t\tsquare := uint64(difference * difference)\n\t\tsum_of_squares += square\n\t}\n\n\tufmt.Println(ufmt.Sprintf(\"Xorshift64* average of %d uint64: %f\\n\", iterations, average))\n\tufmt.Println(ufmt.Sprintf(\"Xorshift64* standard deviation  : %f\\n\", math.Sqrt(float64(sum_of_squares)/float64(iterations))))\n\tufmt.Println(ufmt.Sprintf(\"Xorshift64* theoretical perfect deviation: %f\\n\", (float64(target*2)-1)/math.Sqrt(12)))\n}\n"},{"name":"xorshift64star_test.gno","body":"package xorshift64star\n\nimport (\n\t\"math/rand\"\n\t\"testing\"\n)\n\nfunc TestXorshift64StarSeeding(t *testing.T) {\n\trnd := New()\n\tvalue1 := rnd.Uint64()\n\n\trnd = New(987654321)\n\tvalue2 := rnd.Uint64()\n\n\tif value1 != 3506338452768534464 || value2 != 18211065302896784785 || value1 == value2 {\n\t\tt.Errorf(\"Expected 2 different values; got: %d and %d\", value1, value2)\n\t}\n}\n\nfunc TestXorshift64StarRand(t *testing.T) {\n\trnd := New(987654321)\n\trng := rand.New(rnd)\n\n\t// Expected outputs for the first 5 random floats with the given seed\n\texpected := []float64{\n\t\t.8344002228310946,\n\t\t0.01777174153236205,\n\t\t0.23521769507865276,\n\t\t0.5387610198576143,\n\t\t0.631539862225968,\n\t\t0.9369068148346704,\n\t\t0.6387002315083188,\n\t\t0.5047507613688854,\n\t\t0.5208486273732391,\n\t\t0.25023746271541747,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rng.Float64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Rand.Float64() at iteration %d: got %g, expected %g\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc TestXorshift64StarUint64(t *testing.T) {\n\trnd := New(1000)\n\n\texpected := []uint64{\n\t\t12487186097140327178,\n\t\t14661465266242046735,\n\t\t14694269887751719025,\n\t\t15763651124252725051,\n\t\t6358063137690011177,\n\t\t16123467710013993794,\n\t\t8086526499083208127,\n\t\t5907916440057635441,\n\t\t7074828965897564835,\n\t\t219959441764368518,\n\t}\n\n\tfor i, exp := range expected {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Xorshift64Star.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n}\n\nfunc TestXorshift64StarMarshalUnmarshal(t *testing.T) {\n\trnd := New(1001)\n\n\texpected1 := []uint64{\n\t\t17667678392346722343,\n\t\t8998941448230025236,\n\t\t6038719778092581458,\n\t\t9916057400810746083,\n\t\t3240504040424884895,\n\t}\n\n\texpected2 := []uint64{\n\t\t3977048231667561376,\n\t\t18438555156602529247,\n\t\t2172795924893074637,\n\t\t12043507788481457357,\n\t\t8032279100325099159,\n\t}\n\n\tfor i, exp := range expected1 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Xorshift64Star.Uint64() at iteration %d: got %d, expected %d\", i, val, exp)\n\t\t}\n\t}\n\n\tmarshalled, err := rnd.MarshalBinary()\n\n\t// t.Logf(\"Original State: [%x]\\n\", rnd.seed)\n\t// t.Logf(\"Marshalled State: [%x] -- %v\\n\", marshalled, err)\n\tstate_before := rnd.seed\n\n\tif err != nil {\n\t\tt.Errorf(\"Xorshift64Star.MarshalBinary() error: %v\", err)\n\t}\n\n\t// Advance state by one number; then check the next 5. The expectation is that they _will_ fail.\n\trnd.Uint64()\n\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp == val {\n\t\t\tt.Errorf(\"  Iteration %d matched %d; which is from iteration %d; something strange is happening.\", (i + 6), val, (i + 5))\n\t\t}\n\t}\n\n\t// t.Logf(\"State before unmarshall: [%x]\\n\", rnd.seed)\n\n\t// Now restore the state of the PRNG\n\terr = rnd.UnmarshalBinary(marshalled)\n\n\t// t.Logf(\"State after unmarshall: [%x]\\n\", rnd.seed)\n\n\tif state_before != rnd.seed {\n\t\tt.Errorf(\"States before and after marshal/unmarshal are not equal; go %x and %x\", state_before, rnd.seed)\n\t}\n\n\t// Now we should be back on track for the last 5 numbers\n\tfor i, exp := range expected2 {\n\t\tval := rnd.Uint64()\n\t\tif exp != val {\n\t\t\tt.Errorf(\"Xorshift64Star.Uint64() at iteration %d: got %d, expected %d\", (i + 5), val, exp)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"nV+Psum1IoXm0yyYg414+jDiTbClS7o+273aI5kRpQHrDNRE9WcUcDUIrlL9ggeo76NfnkJMteQ5Q043CJpgAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"config","path":"gno.land/r/leon/config","files":[{"name":"config.gno","body":"package config\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\nconst absPath = \"/r/leon/config\"\n\nvar (\n\tcfgID   seqid.ID\n\tconfigs = avl.NewTree()\n\n\t// SafeObjects\n\tOwnableMain   = ownable.NewWithAddress(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\")\n\tOwnableBackup = ownable.NewWithAddress(\"g1lavlav7zwsjqlzzl3qdl3nl242qtf638vnhdjh\")\n\n\tErrUnauthorized = errors.New(\"leon's config: unauthorized\")\n)\n\ntype Config struct {\n\tid      seqid.ID\n\tname    string\n\tlines   string\n\tupdated time.Time\n}\n\nfunc AddConfig(cur realm, name, lines string) {\n\tif !IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(ErrUnauthorized)\n\t}\n\n\tid := cfgID.Next()\n\tconfigs.Set(id.String(), Config{\n\t\tid:      id,\n\t\tname:    name,\n\t\tlines:   lines,\n\t\tupdated: time.Now(),\n\t})\n}\n\nfunc EditConfig(cur realm, id string, name, lines string) {\n\tif !IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(ErrUnauthorized)\n\t}\n\n\traw, ok := configs.Remove(id)\n\tif !ok {\n\t\tpanic(\"no config with that id\")\n\t}\n\n\tconf := raw.(Config)\n\t// Overwrites data\n\tconf.lines = lines\n\tconf.name = name\n\tconf.updated = time.Now()\n}\n\nfunc RemoveConfig(cur realm, id string) {\n\tif !IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(ErrUnauthorized)\n\t}\n\n\tif _, ok := configs.Remove(id); !ok {\n\t\tpanic(\"no config with that id\")\n\t}\n}\n\nfunc UpdateBanner(cur realm, newBanner string) {\n\tif !IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(ErrUnauthorized)\n\t}\n\n\tbanner = newBanner\n}\n\nfunc IsAuthorized(addr address) bool {\n\treturn addr == OwnableMain.Owner() || addr == OwnableBackup.Owner()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/leon/config\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"},{"name":"render.gno","body":"package config\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/moul/txlink\"\n\tp \"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tbanner = \"---\\n[[Leon's Home page]](/r/leon/home) | [[Leon's snippets]](/r/leon/config) | [[GitHub: @leohhhn]](https://github.com/leohhhn)\\n\\n---\"\n\tpager  = p.NewPager(configs, 10, true)\n)\n\nfunc Banner() string {\n\treturn banner\n}\n\nfunc Render(path string) (out string) {\n\treq := realmpath.Parse(path)\n\tif req.Path == \"\" {\n\t\tout += md.H1(\"Leon's configs \u0026 snippets\")\n\n\t\tout += ufmt.Sprintf(\"Leon's main address: %s\\n\\n\", OwnableMain.Owner().String())\n\t\tout += ufmt.Sprintf(\"Leon's backup address: %s\\n\\n\", OwnableBackup.Owner().String())\n\n\t\tout += md.H2(\"Snippets\")\n\n\t\tif configs.Size() == 0 {\n\t\t\tout += \"No configs yet :c\\n\\n\"\n\t\t} else {\n\t\t\tpage := pager.MustGetPageByPath(path)\n\t\t\tfor _, item := range page.Items {\n\t\t\t\tout += ufmt.Sprintf(\"- [%s](%s:%s)\\n\\n\", item.Value.(Config).name, absPath, item.Key)\n\t\t\t}\n\n\t\t\tout += page.Picker(path)\n\t\t\tout += \"\\n\\n\"\n\t\t\tout += \"Page \" + strconv.Itoa(page.PageNumber) + \" of \" + strconv.Itoa(page.TotalPages) + \"\\n\\n\"\n\t\t}\n\n\t\tout += Banner()\n\n\t\treturn out\n\t}\n\n\treturn renderConfPage(req.Path)\n}\n\nfunc renderConfPage(id string) (out string) {\n\traw := configs.Get(id)\n\tif raw == nil {\n\t\tout += md.H1(\"404\")\n\t\tout += \"That config does not exist :/\"\n\t\treturn out\n\t}\n\n\tconf := raw.(Config)\n\tout += md.H1(conf.name)\n\tout += ufmt.Sprintf(\"```\\n%s\\n```\\n\\n\", conf.lines)\n\tout += ufmt.Sprintf(\"_Last updated on %s_\\n\\n\", conf.updated.Format(\"02 Jan, 2006\"))\n\tout += md.HorizontalRule()\n\tout += ufmt.Sprintf(\"[[EDIT]](%s) - [[DELETE]](%s)\", txlink.Call(\"EditConfig\", \"id\", conf.id.String()), txlink.Call(\"RemoveConfig\", \"id\", conf.id.String()))\n\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ajXc7KlCZHz6EdiafofyDf4bLWzjlFy6ql7iKrxy56nxZ2EUB0Tk+tH+JpXfCab9LtIrBGSHiKIJpTrS2hkcCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"hor","path":"gno.land/r/leon/hor","files":[{"name":"datasource.gno","body":"package hor\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/jeronimoalbi/datasource\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc NewDatasource() Datasource {\n\treturn Datasource{exhibition}\n}\n\ntype Datasource struct {\n\texhibition *Exhibition\n}\n\nfunc (ds Datasource) Size() int { return ds.exhibition.items.Size() }\n\nfunc (ds Datasource) Records(q datasource.Query) datasource.Iterator {\n\treturn \u0026iterator{\n\t\texhibition: ds.exhibition,\n\t\tindex:      q.Offset,\n\t\tmaxIndex:   q.Offset + q.Count,\n\t}\n}\n\nfunc (ds Datasource) Record(id string) (datasource.Record, error) {\n\tv := ds.exhibition.items.Get(id)\n\tif v == nil {\n\t\treturn nil, errors.New(\"realm submission not found\")\n\t}\n\treturn record{v.(*Item)}, nil\n}\n\ntype record struct {\n\titem *Item\n}\n\nfunc (r record) ID() string     { return r.item.id.String() }\nfunc (r record) String() string { return r.item.pkgpath }\n\nfunc (r record) Fields() (datasource.Fields, error) {\n\tfields := avl.NewTree()\n\tfields.Set(\n\t\t\"details\",\n\t\tufmt.Sprintf(\"Votes: ⏶ %d - ⏷ %d\", r.item.upvote.Size(), r.item.downvote.Size()),\n\t)\n\treturn fields, nil\n}\n\nfunc (r record) Content() (string, error) {\n\tcontent := r.item.Render(false)\n\treturn content, nil\n}\n\ntype iterator struct {\n\texhibition      *Exhibition\n\tindex, maxIndex int\n\trecord          *record\n}\n\nfunc (it iterator) Record() datasource.Record { return it.record }\nfunc (it iterator) Err() error                { return nil }\n\nfunc (it *iterator) Next() bool {\n\tif it.index \u003e= it.maxIndex || it.index \u003e= it.exhibition.items.Size() {\n\t\treturn false\n\t}\n\n\t_, v := it.exhibition.items.GetByIndex(it.index)\n\tit.record = \u0026record{v.(*Item)}\n\tit.index++\n\treturn true\n}\n"},{"name":"datasource_test.gno","body":"package hor\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/jeronimoalbi/datasource\"\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nfunc init(cur realm) {\n\t// Pre-register \"demo\" user for render tests. uinit.RegisterUser is\n\t// genesis-only (height==0) since the security fix; tests run at\n\t// DefaultHeight=123, so we temporarily reset and restore.\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"demo\", address(\"g13ek2zz9qurzynzvssyc4sthwppnruhnp0gdz8n\"))\n\ttesting.SetHeight(123)\n}\n\nvar (\n\t_ datasource.Datasource    = (*Datasource)(nil)\n\t_ datasource.Record        = (*record)(nil)\n\t_ datasource.ContentRecord = (*record)(nil)\n\t_ datasource.Iterator      = (*iterator)(nil)\n)\n\nfunc TestDatasourceRecords(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname      string\n\t\titems     []*Item\n\t\trecordIDs []string\n\t\toptions   []datasource.QueryOption\n\t}{\n\t\t{\n\t\t\tname:      \"all items\",\n\t\t\titems:     []*Item{{id: 1}, {id: 2}, {id: 3}},\n\t\t\trecordIDs: []string{\"0000001\", \"0000002\", \"0000003\"},\n\t\t},\n\t\t{\n\t\t\tname:      \"with offset\",\n\t\t\titems:     []*Item{{id: 1}, {id: 2}, {id: 3}},\n\t\t\trecordIDs: []string{\"0000002\", \"0000003\"},\n\t\t\toptions:   []datasource.QueryOption{datasource.WithOffset(1)},\n\t\t},\n\t\t{\n\t\t\tname:      \"with count\",\n\t\t\titems:     []*Item{{id: 1}, {id: 2}, {id: 3}},\n\t\t\trecordIDs: []string{\"0000001\", \"0000002\"},\n\t\t\toptions:   []datasource.QueryOption{datasource.WithCount(2)},\n\t\t},\n\t\t{\n\t\t\tname:      \"with offset and count\",\n\t\t\titems:     []*Item{{id: 1}, {id: 2}, {id: 3}},\n\t\t\trecordIDs: []string{\"0000002\"},\n\t\t\toptions: []datasource.QueryOption{\n\t\t\t\tdatasource.WithOffset(1),\n\t\t\t\tdatasource.WithCount(1),\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Initialize a local instance of exhibition\n\t\t\texhibition := \u0026Exhibition{items: avl.NewTree()}\n\t\t\tfor _, item := range tc.items {\n\t\t\t\texhibition.items.Set(item.id.String(), item)\n\t\t\t}\n\n\t\t\t// Get a records iterator\n\t\t\tds := Datasource{exhibition}\n\t\t\tquery := datasource.NewQuery(tc.options...)\n\t\t\titer := ds.Records(query)\n\n\t\t\t// Start asserting\n\t\t\turequire.Equal(t, len(tc.items), ds.Size(), \"datasource size\")\n\n\t\t\tvar records []datasource.Record\n\t\t\tfor iter.Next() {\n\t\t\t\trecords = append(records, iter.Record())\n\t\t\t}\n\t\t\turequire.Equal(t, len(tc.recordIDs), len(records), \"record count\")\n\n\t\t\tfor i, r := range records {\n\t\t\t\tuassert.Equal(t, tc.recordIDs[i], r.ID())\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestDatasourceRecord(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname  string\n\t\titems []*Item\n\t\tid    string\n\t\terr   string\n\t}{\n\t\t{\n\t\t\tname:  \"found\",\n\t\t\titems: []*Item{{id: 1}, {id: 2}, {id: 3}},\n\t\t\tid:    \"0000001\",\n\t\t},\n\t\t{\n\t\t\tname:  \"no found\",\n\t\t\titems: []*Item{{id: 1}, {id: 2}, {id: 3}},\n\t\t\tid:    \"42\",\n\t\t\terr:   \"realm submission not found\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t// Initialize a local instance of exhibition\n\t\t\texhibition := \u0026Exhibition{items: avl.NewTree()}\n\t\t\tfor _, item := range tc.items {\n\t\t\t\texhibition.items.Set(item.id.String(), item)\n\t\t\t}\n\n\t\t\t// Get a single record\n\t\t\tds := Datasource{exhibition}\n\t\t\tr, err := ds.Record(tc.id)\n\n\t\t\t// Start asserting\n\t\t\tif tc.err != \"\" {\n\t\t\t\tuassert.ErrorContains(t, err, tc.err)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"no error\")\n\t\t\turequire.NotEqual(t, nil, r, \"record not nil\")\n\t\t\tuassert.Equal(t, tc.id, r.ID())\n\t\t})\n\t}\n}\n\nfunc TestItemRecord(cur realm, t *testing.T) {\n\tpkgpath := \"gno.land/r/demo/test\"\n\titem := Item{\n\t\tid:          1,\n\t\tpkgpath:     pkgpath,\n\t\ttitle:       \"Test Realm\",\n\t\tdescription: \"This is a test realm in the Hall of Fame\",\n\t\tblockNum:    42,\n\t\tupvote:      \u0026addrset.Set{},\n\t\tdownvote:    \u0026addrset.Set{},\n\t}\n\titem.downvote.Add(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\titem.upvote.Add(\"g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn\")\n\titem.upvote.Add(\"g1w4ek2u3jta047h6lta047h6lta047h6l9huexc\")\n\n\tr := record{\u0026item}\n\n\tuassert.Equal(t, \"0000001\", r.ID())\n\tuassert.Equal(t, pkgpath, r.String())\n\n\tfields, _ := r.Fields()\n\tdetails := fields.Get(\"details\")\n\turequire.True(t, details != nil, \"details field\")\n\tuassert.Equal(t, \"Votes: ⏶ 2 - ⏷ 1\", details)\n\n\tcontent, _ := r.Content()\n\twantContent := `### [Test Realm](/r/demo/test)\nThis is a test realm in the Hall of Fame\n\nby [@demo](/u/demo)\n\nSubmitted at Block #42\n\n**[2👍](/r/leon/hor$help\u0026func=Upvote\u0026pkgpath=gno.land%2Fr%2Fdemo%2Ftest) - [1👎](/r/leon/hor$help\u0026func=Downvote\u0026pkgpath=gno.land%2Fr%2Fdemo%2Ftest)**`\n\tuassert.Equal(t, wantContent, content)\n}\n"},{"name":"errors.gno","body":"package hor\n\nimport (\n\t\"gno.land/p/leon/pkgerr\"\n)\n\nvar (\n\tErrNoSuchItem     = pkgerr.New(\"no such item exists\")\n\tErrDoubleUpvote   = pkgerr.New(\"cannot upvote twice\")\n\tErrDoubleDownvote = pkgerr.New(\"cannot downvote twice\")\n)\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/leon/hor\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"},{"name":"hof_test.gno","body":"package hor\n\nimport (\n\t\"chain/runtime\"\n\t\"testing\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nconst (\n\trlmPath  = \"gno.land/r/gnoland/home\"\n\trlmPath2 = \"gno.land/r/gnoland/test2\"\n\n\trlmPath3 = \"gno.land/r/gnoland/test3\"\n\trlmPath4 = \"gno.land/r/gnoland/test4\"\n\trlmPath5 = \"gno.land/r/gnoland/test5\"\n\n\tvalidTitle         = \"valid title\"\n\tinvalidTitle       = \"This title is very very very long, longer than 30 characters\"\n\tvalidDesc          = \"valid description\"\n\tinvalidDescription = \"This description is very very very long, longer than 50 characters\"\n)\n\nvar (\n\tadmin      address = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\" // hardcoded from config.OwnableMain\n\tadminRealm runtime.Realm\n\talice      = testutils.TestAddress(\"alice\")\n)\n\nfunc init() {\n\tadminRealm = testing.NewUserRealm(admin)\n}\n\nfunc TestRegister(cur realm, t *testing.T) {\n\t// Test user realm register\n\taliceRealm := testing.NewUserRealm(alice)\n\ttesting.SetRealm(aliceRealm)\n\n\tRegister(cross(cur), validTitle, validDesc)\n\tuassert.False(t, itemExists(t, rlmPath))\n\n\t// Test register while paused\n\ttesting.SetRealm(adminRealm)\n\terr := Pause(cross(cur))\n\tuassert.NoError(t, err)\n\n\t// Set legitimate caller\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath))\n\n\tRegister(cross(cur), validTitle, validDesc)\n\tuassert.False(t, itemExists(t, rlmPath))\n\n\t// Unpause\n\ttesting.SetRealm(adminRealm)\n\terr = Unpause(cross(cur))\n\tuassert.NoError(t, err)\n\n\t// Set legitimate caller\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\t// Find registered items\n\tuassert.True(t, itemExists(t, rlmPath))\n\n\t// Test register with invalid title\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath2))\n\tRegister(cross(cur), invalidTitle, validDesc)\n\tuassert.False(t, itemExists(t, rlmPath2))\n\n\t// Test register with invalid description\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath2))\n\tRegister(cross(cur), validTitle, invalidDescription)\n\tuassert.False(t, itemExists(t, rlmPath2))\n}\n\nfunc TestUpvote(cur realm, t *testing.T) {\n\traw := exhibition.items.Get(rlmPath)\n\titem := raw.(*Item)\n\n\t// 0 upvotes by default\n\turequire.Equal(t, item.upvote.Size(), 0)\n\n\ttesting.SetRealm(adminRealm)\n\n\turequire.NotPanics(t, cur, func() {\n\t\tUpvote(cross(cur), rlmPath)\n\t})\n\n\t// Check both trees for 1 upvote\n\tuassert.Equal(t, item.upvote.Size(), 1)\n\n\t// Check double upvote\n\tuassert.AbortsWithMessage(t, cur, ErrDoubleUpvote.Error(), func() {\n\t\tUpvote(cross(cur), rlmPath)\n\t})\n}\n\nfunc TestDownvote(cur realm, t *testing.T) {\n\traw := exhibition.items.Get(rlmPath)\n\titem := raw.(*Item)\n\n\t// 0 downvotes by default\n\turequire.Equal(t, item.downvote.Size(), 0)\n\n\tuserRealm := testing.NewUserRealm(alice)\n\ttesting.SetRealm(userRealm)\n\n\turequire.NotPanics(t, cur, func() {\n\t\tDownvote(cross(cur), rlmPath)\n\t})\n\n\t// Check both trees for 1 upvote\n\tuassert.Equal(t, item.downvote.Size(), 1)\n\n\t// Check double downvote\n\tuassert.AbortsWithMessage(t, cur, ErrDoubleDownvote.Error(), func() {\n\t\tDownvote(cross(cur), rlmPath)\n\t})\n}\n\nfunc TestDelete(cur realm, t *testing.T) {\n\tuserRealm := testing.NewUserRealm(admin)\n\ttesting.SetRealm(userRealm)\n\ttesting.SetOriginCaller(admin)\n\n\tuassert.AbortsWithMessage(t, cur, ErrNoSuchItem.Error(), func() {\n\t\tDelete(cross(cur), \"nonexistentpkgpath\")\n\t})\n\n\ti := exhibition.items.Get(rlmPath)\n\t_ = i.(*Item).id\n\n\tuassert.NotPanics(t, cur, func() {\n\t\tDelete(cross(cur), rlmPath)\n\t})\n\n\tuassert.False(t, exhibition.items.Has(rlmPath))\n}\n\nfunc itemExists(t *testing.T, rlmPath string) bool {\n\tt.Helper()\n\n\treturn exhibition.items.Has(rlmPath)\n}\n\nfunc TestgetVoteSortKey(t *testing.T) {\n\ti := \u0026Item{\n\t\tid:          1,\n\t\ttitle:       validTitle,\n\t\tdescription: validDesc,\n\t\tpkgpath:     rlmPath,\n\t\tblockNum:    runtime.ChainHeight(),\n\t\tupvote:      \u0026addrset.Set{},\n\t\tdownvote:    \u0026addrset.Set{},\n\t}\n\n\ti.upvote.Add(alice)\n\n\tgeneratedKey := getVoteSortKey(i.upvote.Size(), i.id)\n\texpectedKey := \"0000000001:1\"\n\n\turequire.Equal(t, generatedKey, expectedKey)\n}\n\nfunc TestSortByUpvote(cur realm, t *testing.T) {\n\t// Remove all items from all trees\n\texhibition.items.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.items.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByUpvotes.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByUpvotes.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByDownvotes.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByDownvotes.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByCreation.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByCreation.Remove(key)\n\t\treturn false\n\t})\n\n\t// Add items\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath3))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath4))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath5))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\tuser1 := testutils.TestAddress(\"user1\")\n\tuser2 := testutils.TestAddress(\"user2\")\n\tuser3 := testutils.TestAddress(\"user3\")\n\n\ttesting.SetOriginCaller(user1)\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\tUpvote(cross(cur), rlmPath3)\n\tUpvote(cross(cur), rlmPath4)\n\tUpvote(cross(cur), rlmPath5)\n\n\ttesting.SetOriginCaller(user2)\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\tUpvote(cross(cur), rlmPath4)\n\tUpvote(cross(cur), rlmPath5)\n\n\ttesting.SetOriginCaller(user3)\n\ttesting.SetRealm(testing.NewUserRealm(user3))\n\tUpvote(cross(cur), rlmPath5)\n\n\t// We are displaying data in reverse order in render, so items should be sorted in reverse order\n\t_, firstRawValue := exhibition.itemsSortedByUpvotes.GetByIndex(0)\n\tfirstValue := firstRawValue.(*Item)\n\tuassert.Equal(t, firstValue.pkgpath, rlmPath3)\n\n\t_, secondRawValue := exhibition.itemsSortedByUpvotes.GetByIndex(1)\n\tsecondValue := secondRawValue.(*Item)\n\tuassert.Equal(t, secondValue.pkgpath, rlmPath4)\n}\n\nfunc TestSortByDownvote(cur realm, t *testing.T) {\n\t// Remove all items from all trees\n\texhibition.items.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.items.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByUpvotes.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByUpvotes.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByDownvotes.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByDownvotes.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByCreation.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByCreation.Remove(key)\n\t\treturn false\n\t})\n\n\t// Add items\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath3))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath4))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath5))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\tuser1 := testutils.TestAddress(\"user1\")\n\tuser2 := testutils.TestAddress(\"user2\")\n\tuser3 := testutils.TestAddress(\"user3\")\n\n\ttesting.SetOriginCaller(user1)\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\tDownvote(cross(cur), rlmPath3)\n\tDownvote(cross(cur), rlmPath4)\n\tDownvote(cross(cur), rlmPath5)\n\n\ttesting.SetOriginCaller(user2)\n\ttesting.SetRealm(testing.NewUserRealm(user2))\n\tDownvote(cross(cur), rlmPath4)\n\tDownvote(cross(cur), rlmPath5)\n\n\ttesting.SetOriginCaller(user3)\n\ttesting.SetRealm(testing.NewUserRealm(user3))\n\tDownvote(cross(cur), rlmPath5)\n\n\t// We are dispalying data is reverse order in render, so items should be sorted in reverse order\n\t_, firstRawValue := exhibition.itemsSortedByDownvotes.GetByIndex(0)\n\n\tfirstValue := firstRawValue.(*Item)\n\n\tuassert.Equal(t, firstValue.pkgpath, rlmPath3)\n\n\t_, secondRawValue := exhibition.itemsSortedByDownvotes.GetByIndex(1)\n\n\tsecondValue := secondRawValue.(*Item)\n\n\tuassert.Equal(t, secondValue.pkgpath, rlmPath4)\n}\n\nfunc TestSortByCreation(cur realm, t *testing.T) {\n\t// Remove all items from all trees\n\texhibition.items.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.items.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByUpvotes.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByUpvotes.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByDownvotes.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByDownvotes.Remove(key)\n\t\treturn false\n\t})\n\texhibition.itemsSortedByCreation.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\texhibition.itemsSortedByCreation.Remove(key)\n\t\treturn false\n\t})\n\n\ttesting.SkipHeights(10)\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath3))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\ttesting.SkipHeights(10)\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath4))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\ttesting.SkipHeights(10)\n\ttesting.SetRealm(testing.NewCodeRealm(rlmPath5))\n\tRegister(cross(cur), validTitle, validDesc)\n\n\t// We are dispalying data is reverse order in render, so items should be sorted in reverse order\n\t_, firstRawValue := exhibition.itemsSortedByCreation.GetByIndex(0)\n\n\tfirstValue := firstRawValue.(*Item)\n\n\tuassert.Equal(t, firstValue.pkgpath, rlmPath3)\n\n\t_, secondRawValue := exhibition.itemsSortedByCreation.GetByIndex(1)\n\n\tsecondValue := secondRawValue.(*Item)\n\n\tuassert.Equal(t, secondValue.pkgpath, rlmPath4)\n}\n\n/* XXX fix @moul\nfunc TestGetScore(cur realm, t *testing.T) {\n\tscore, err := h.GetScore(42)\n\tuassert.Nil(t, err)\n\tuassert.Equal(t, 0, score)\n\tuassert.NotPanics(t, cur, func() {\n\t\tuassert.Equal(t, 0, h.MustGetScore(42))\n\t})\n}\n*/\n"},{"name":"hor.gno","body":"// Package hor is the hall of realms.\n// The Hall of Realms is an exhibition that holds items. Users can add their realms to the Hall of Realms by\n// importing the Hall of Realms package and calling hor.Register() from their init function.\npackage hor\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/pausable/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/r/leon/config\"\n)\n\nconst (\n\tmaxTitleLength       = 30\n\tmaxDescriptionLength = 50\n)\n\nvar (\n\texhibition *Exhibition\n\n\t// Safe objects\n\tOwnable  *ownable.Ownable\n\tPausable *pausable.Pausable\n)\n\ntype (\n\tExhibition struct {\n\t\titemCounter            seqid.ID\n\t\tdescription            string\n\t\titems                  *avl.Tree // pkgPath \u003e *Item\n\t\titemsSortedByCreation  *avl.Tree // same data but sorted by creation time\n\t\titemsSortedByUpvotes   *avl.Tree // same data but sorted by upvotes\n\t\titemsSortedByDownvotes *avl.Tree // same data but sorted by downvotes\n\t}\n\n\tItem struct {\n\t\tid          seqid.ID\n\t\ttitle       string\n\t\tdescription string\n\t\tpkgpath     string\n\t\tblockNum    int64\n\t\tupvote      *addrset.Set\n\t\tdownvote    *addrset.Set\n\t}\n)\n\nfunc init() {\n\texhibition = \u0026Exhibition{\n\t\titems:                  avl.NewTree(),\n\t\titemsSortedByCreation:  avl.NewTree(),\n\t\titemsSortedByUpvotes:   avl.NewTree(),\n\t\titemsSortedByDownvotes: avl.NewTree(),\n\t}\n\n\tOwnable = ownable.NewWithAddress(config.OwnableMain.Owner()) // OrigSendOwnable?\n\tPausable = pausable.NewFromOwnable(Ownable)\n}\n\n// Register registers your realm to the Hall of Fame\n// Should be called from within code\nfunc Register(cur realm, title, description string) {\n\tif Pausable.IsPaused() {\n\t\treturn\n\t}\n\n\tsubmission := cur.Previous()\n\tpkgpath := submission.PkgPath()\n\n\t// Must be called from code\n\tif submission.IsUser() {\n\t\treturn\n\t}\n\n\t// Must not yet exist\n\tif exhibition.items.Has(pkgpath) {\n\t\treturn\n\t}\n\n\t// Title must be between 1 maxTitleLength long\n\tif title == \"\" || len(title) \u003e maxTitleLength {\n\t\treturn\n\t}\n\n\t// Description must be between 1 maxDescriptionLength long\n\tif len(description) \u003e maxDescriptionLength {\n\t\treturn\n\t}\n\n\tid := exhibition.itemCounter.Next()\n\ti := \u0026Item{\n\t\tid:          id,\n\t\ttitle:       title,\n\t\tdescription: description,\n\t\tpkgpath:     pkgpath,\n\t\tblockNum:    runtime.ChainHeight(),\n\t\tupvote:      \u0026addrset.Set{},\n\t\tdownvote:    \u0026addrset.Set{},\n\t}\n\n\texhibition.items.Set(pkgpath, i)\n\texhibition.itemsSortedByCreation.Set(getCreationSortKey(i.blockNum, i.id), i)\n\texhibition.itemsSortedByUpvotes.Set(getVoteSortKey(i.upvote.Size(), i.id), i)\n\texhibition.itemsSortedByDownvotes.Set(getVoteSortKey(i.downvote.Size(), i.id), i)\n\n\tchain.Emit(\"Registration\")\n}\n\nfunc Upvote(cur realm, pkgpath string) {\n\trawItem := exhibition.items.Get(pkgpath)\n\tif rawItem == nil {\n\t\tpanic(ErrNoSuchItem)\n\t}\n\n\titem := rawItem.(*Item)\n\tcaller := cur.Previous().Address()\n\n\tif item.upvote.Has(caller) {\n\t\tpanic(ErrDoubleUpvote)\n\t}\n\n\tif _, exists := exhibition.itemsSortedByUpvotes.Remove(getVoteSortKey(item.upvote.Size(), item.id)); !exists {\n\t\tpanic(\"error removing old upvote entry\")\n\t}\n\n\titem.upvote.Add(caller)\n\n\texhibition.itemsSortedByUpvotes.Set(getVoteSortKey(item.upvote.Size(), item.id), item)\n}\n\nfunc Downvote(cur realm, pkgpath string) {\n\trawItem := exhibition.items.Get(pkgpath)\n\tif rawItem == nil {\n\t\tpanic(ErrNoSuchItem)\n\t}\n\n\titem := rawItem.(*Item)\n\tcaller := cur.Previous().Address()\n\n\tif item.downvote.Has(caller) {\n\t\tpanic(ErrDoubleDownvote)\n\t}\n\n\tif _, exist := exhibition.itemsSortedByDownvotes.Remove(getVoteSortKey(item.downvote.Size(), item.id)); !exist {\n\t\tpanic(\"error removing old downvote entry\")\n\n\t}\n\n\titem.downvote.Add(caller)\n\n\texhibition.itemsSortedByDownvotes.Set(getVoteSortKey(item.downvote.Size(), item.id), item)\n}\n\nfunc Pause(cur realm) error {\n\treturn Pausable.Pause(0, cur)\n}\n\nfunc Unpause(cur realm) error {\n\treturn Pausable.Unpause(0, cur)\n}\n\nfunc Delete(cur realm, pkgpath string) {\n\tif !Ownable.OwnedBy(cur.Previous().Address()) {\n\t\tpanic(ownable.ErrUnauthorized)\n\t}\n\n\ti := exhibition.items.Get(pkgpath)\n\tif i == nil {\n\t\tpanic(ErrNoSuchItem)\n\t}\n\n\titem := i.(*Item)\n\tupvoteKey := getVoteSortKey(item.upvote.Size(), item.id)\n\tdownvoteKey := getVoteSortKey(item.downvote.Size(), item.id)\n\n\tif _, removed := exhibition.items.Remove(pkgpath); !removed {\n\t\tpanic(ErrNoSuchItem)\n\t}\n\n\tif _, removed := exhibition.itemsSortedByUpvotes.Remove(upvoteKey); !removed {\n\t\tpanic(ErrNoSuchItem)\n\t}\n\n\tif _, removed := exhibition.itemsSortedByDownvotes.Remove(downvoteKey); !removed {\n\t\tpanic(ErrNoSuchItem)\n\t}\n\n\tif _, removed := exhibition.itemsSortedByCreation.Remove(getCreationSortKey(item.blockNum, item.id)); !removed {\n\t\tpanic(ErrNoSuchItem)\n\t}\n}\n\nfunc getVoteSortKey(votes int, id seqid.ID) string {\n\tvotesStr := strconv.Itoa(votes)\n\tpaddedVotes := strings.Repeat(\"0\", 10-len(votesStr)) + votesStr\n\treturn paddedVotes + \":\" + strconv.FormatUint(uint64(id), 10)\n}\n\nfunc getCreationSortKey(blockNum int64, id seqid.ID) string {\n\tblockNumStr := strconv.Itoa(int(blockNum))\n\tpaddedBlockNum := strings.Repeat(\"0\", 10-len(blockNumStr)) + blockNumStr\n\treturn paddedBlockNum + \":\" + strconv.FormatUint(uint64(id), 10)\n}\n"},{"name":"render.gno","body":"package hor\n\nimport (\n\t\"net/url\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/fqname/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/sys/users\"\n)\n\nconst colsPerRow = 3\n\nfunc Render(path string) string {\n\tout := md.H1(\"The Hall of Realms\\n\\n\")\n\n\tif !strings.Contains(path, \"hall\") \u0026\u0026 !strings.Contains(path, \"dashboard\") {\n\t\tout += renderAbout()\n\t\treturn out\n\t}\n\n\tdashboardEnabled := strings.Contains(path, \"dashboard\")\n\tif dashboardEnabled {\n\t\tout += renderDashboard()\n\t\tout += renderActions(path)\n\t\treturn out\n\t}\n\n\tout += renderActions(path)\n\tout += exhibition.Render(path, dashboardEnabled)\n\n\treturn out\n}\n\nfunc (e Exhibition) Render(path string, dashboard bool) string {\n\ttree := getTreeByPath(\u0026e, path)\n\n\tu, _ := url.Parse(path)\n\treversed := u.Query().Get(\"sort\") != \"oldest\"\n\n\tpage := pager.NewPager(tree, colsPerRow*3, reversed).MustGetPageByPath(path)\n\tout := ufmt.Sprintf(\"%s\\n\\n\", e.description)\n\n\tif e.items.Size() == 0 {\n\t\tout += \"No items in this exhibition currently.\\n\\n\"\n\t\treturn out\n\t}\n\n\tstr := make([]string, len(page.Items))\n\tfor i, item := range page.Items {\n\t\titemValue := item.Value.(*Item)\n\t\tstr[i] += itemValue.Render(dashboard)\n\t}\n\n\tout += md.ColumnsN(str, 3, true)\n\tout += md.H3(page.Picker(path))\n\n\treturn out\n}\n\nfunc (i Item) Render(dashboard bool) string {\n\tout := ufmt.Sprintf(\"### [%s](%s)\\n\",\n\t\ti.title,\n\t\tstrings.TrimPrefix(i.pkgpath, \"gno.land\"),\n\t)\n\n\tif i.description == \"\" {\n\t\ti.description = \"_No description provided._\\n\"\n\t}\n\tout += ufmt.Sprintf(\"%s\\n\\n\", i.description)\n\n\tnamespace := strings.Split(i.pkgpath, \"/\")[2]\n\tuser, _ := users.ResolveAny(namespace)\n\tif user != nil {\n\t\tnamespace = user.RenderLink(\"\")\n\t}\n\tout += ufmt.Sprintf(\"by %s\\n\\n\", namespace)\n\n\tblockMsg := \"Submitted via the [Monorepo](https://github.com/gnolang/gno)\"\n\tif i.blockNum \u003e 0 {\n\t\tblockMsg = ufmt.Sprintf(\"Submitted at Block #%d\", i.blockNum)\n\t}\n\tout += ufmt.Sprintf(\"%s\\n\\n\", blockMsg)\n\n\tout += md.Bold(ufmt.Sprintf(\"[%d👍](%s) - [%d👎](%s)\",\n\t\ti.upvote.Size(), txlink.Call(\"Upvote\", \"pkgpath\", i.pkgpath),\n\t\ti.downvote.Size(), txlink.Call(\"Downvote\", \"pkgpath\", i.pkgpath),\n\t))\n\n\tif dashboard {\n\t\tout += md.Link(\"Delete\", txlink.Call(\"Delete\", \"pkgpath\", i.pkgpath))\n\t}\n\n\treturn out\n}\n\nfunc renderDashboard() string {\n\tout := md.H3(\"Dashboard\\n\\n\")\n\tout += ufmt.Sprintf(\"Total submissions: %d\\n\\n\", exhibition.items.Size())\n\n\tout += ufmt.Sprintf(\"Exhibition admin: %s\\n\\n\", Ownable.Owner().String())\n\n\tif !Pausable.IsPaused() {\n\t\tout += md.Link(\"Pause exhibition\", txlink.Call(\"Pause\"))\n\t} else {\n\t\tout += md.Link(\"Unpause exhibition\", txlink.Call(\"Unpause\"))\n\t}\n\n\tout += \"\\n\\n\"\n\treturn out\n}\n\nfunc RenderExhibWidget(itemsToRender int) string {\n\tif itemsToRender \u003c 1 {\n\t\treturn \"\"\n\t}\n\n\tif exhibition.items.Size() == 0 {\n\t\treturn \"No items in the Hall of Realms.\\n\\n\"\n\t}\n\n\tout := \"\"\n\ti := 0\n\texhibition.items.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\titem := value.(*Item)\n\n\t\tout += ufmt.Sprintf(\"- %s\\n\", fqname.RenderLink(item.pkgpath, \"\"))\n\n\t\ti++\n\t\treturn i \u003e= itemsToRender\n\t})\n\n\treturn out\n}\n\nfunc getTreeByPath(e *Exhibition, path string) *avl.Tree {\n\tu, _ := url.Parse(path)\n\tswitch u.Query().Get(\"sort\") {\n\tcase \"upvotes\":\n\t\treturn e.itemsSortedByUpvotes\n\tcase \"downvotes\":\n\t\treturn e.itemsSortedByDownvotes\n\tcase \"creation\":\n\t\treturn e.itemsSortedByCreation\n\tcase \"oldest\":\n\t\treturn e.itemsSortedByCreation\n\tdefault:\n\t\treturn e.itemsSortedByCreation\n\t}\n}\n\nfunc renderAbout() string {\n\tout := `\nWelcome, gnomes!\n\nThe Hall of Realms is a simple \u0026 permissionless dashboard for gnomes to share\ntheir work with the community.\n\nHere, anyone is welcome to submit their own code. This realm utilizes a common\nGno pattern - the registry pattern - to allow anyone to programmatically submit\ntheir work.\n\nSimply import the Hall of Realms in your code, and call the \"Register()\" function\ninside your realm init, as shown below:\n\n\"\"\"go\npackage myrealm\n\nimport \"gno.land/r/leon/hor\"\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"My Gnome App\", \"This is my submission to the Hall of Realms.\")\n}\n...\n\"\"\"\n\n## [Visit The Hall -\u003e](/r/leon/hor:hall)\n\n`\n\tout = strings.ReplaceAll(out, \"\\\"\", \"`\")\n\n\treturn out\n}\n\nfunc renderActions(path string) string {\n\tout := md.HorizontalRule()\n\tout += md.Link(\"Reset Sort\", \"?\") + \" | \"\n\tout += md.Link(\"Sort by Upvotes\", \"?sort=upvotes\") + \" | \"\n\tout += md.Link(\"Sort by Downvotes\", \"?sort=downvotes\") + \" | \"\n\tout += md.Link(\"Sort by Most Recent\", \"?sort=creation\") + \" | \"\n\tout += md.Link(\"Sort by Oldest\", \"?sort=oldest\") + \" | \"\n\n\tif !strings.Contains(path, \"dashboard\") {\n\t\tout += md.Link(\"Dashboard\", \"/r/leon/hor:dashboard\")\n\t} else {\n\t\tout += md.Link(\"Dashboard off\", \"/r/leon/hor:hall\")\n\t}\n\n\tout += \" | \"\n\tout += md.Link(\"About\", \"/r/leon/hor\") + \"\\n\\n\"\n\n\tout += md.HorizontalRule()\n\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"de0ajnvnQeLAb7NDMeICESWMELcrKWg+fmX7goWuKmNjMOR/NZL2yfSJnwp5KN08dNWOOW8EChvNozQWd4YeDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"forms","path":"gno.land/r/agherasie/forms","files":[{"name":"forms.gno","body":"package forms\n\nimport (\n\t\"gno.land/p/agherasie/forms\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/onbloc/json\"\n\t\"gno.land/r/leon/hor\"\n)\n\nvar db *forms.FormDB\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"Ahgerasle's forms\", \"\")\n\tdb = forms.NewDB()\n}\n\nfunc CreateForm(cur realm, title string, description string, openAt string, closeAt string, data string) string {\n\tid, err := db.CreateForm(0, cur, title, description, openAt, closeAt, data)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn id\n}\n\nfunc GetForms() string {\n\tbuilder := forms.FormNodeBuilder{json.Builder()}\n\n\tbuilder.WriteArray(\"forms\", func(builder *forms.FormArrayBuilder) {\n\t\tfor _, form := range db.Forms {\n\t\t\tbuilder.WriteObject(func(builder *forms.FormNodeBuilder) {\n\t\t\t\tbuilder.WriteForm(\"form\", form)\n\t\t\t})\n\t\t}\n\t})\n\n\tencoded, err := json.Marshal(builder.Node())\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn string(encoded)\n}\n\nfunc GetFormByID(id string) string {\n\tform, err := db.GetForm(id)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tbuilder := forms.FormNodeBuilder{json.Builder()}\n\n\tbuilder.WriteForm(\"form\", form).\n\t\tWriteObject(\"submissions\", func(builder *forms.FormNodeBuilder) {\n\t\t\tformSubmissions := db.GetSubmissionsByFormID(form.ID)\n\t\t\tfor _, submission := range formSubmissions {\n\t\t\t\tbuilder.WriteFormSubmission(submission.Author.String(), submission)\n\t\t\t}\n\t\t})\n\n\topenAt, err := form.OpenAt()\n\tif err == nil {\n\t\tbuilder.WriteString(\"openAt\", openAt.Format(\"2006-01-02 15:04:05\"))\n\t}\n\tcloseAt, err := form.CloseAt()\n\tif err == nil {\n\t\tbuilder.WriteString(\"closeAt\", closeAt.Format(\"2006-01-02 15:04:05\"))\n\t}\n\n\tencoded, err := json.Marshal(builder.Node())\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn string(encoded)\n}\n\nfunc GetAnswer(formID string, authorID string) string {\n\t_, err := db.GetForm(formID)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tanswer, err := db.GetAnswer(formID, address(authorID))\n\tif answer != nil {\n\t\tpanic(err)\n\t}\n\n\treturn answer.Answers\n}\n\nfunc SubmitForm(cur realm, formID string, answers string) {\n\t_, err := db.GetForm(formID)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tdb.SubmitForm(0, cur, formID, answers)\n}\n\nfunc Render(_ string) string {\n\tif len(db.Forms) == 0 {\n\t\tresponse := \"No forms yet !\"\n\t\treturn response\n\t}\n\n\tresponse := \"Forms:\\n\\n\"\n\tfor _, form := range db.Forms {\n\t\tresponse += ufmt.Sprintf(\"- %s\\n\\n\", GetFormByID(form.ID))\n\t}\n\tresponse += \"Answers:\\n\\n\"\n\tfor _, answer := range db.Answers {\n\t\tresponse += ufmt.Sprintf(\"- Form ID: %s\\nAuthor: %s\\nSubmitted At: %s\\n\u003eAnswers: %s\\n\\n\", answer.FormID, answer.Author, answer.SubmittedAt, answer.Answers)\n\t}\n\n\treturn response\n}\n"},{"name":"forms_test.gno","body":"package forms\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestGetFormByID(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetOriginCaller(alice)\n\ttitle := \"Simple Form\"\n\tdescription := \"This is a form\"\n\topenAt := \"2021-01-01T00:00:00Z\"\n\tcloseAt := \"2021-01-02T00:00:00Z\"\n\tdata := `[{\"label\":\"Name\",\"fieldType\":\"string\",\"required\":true},{\"label\":\"Age\",\"fieldType\":\"number\",\"required\":false},{\"label\":\"Is this a test?\",\"fieldType\":\"boolean\",\"required\":false},{\"label\":\"Favorite Food\",\"fieldType\":\"['Pizza', 'Schnitzel', 'Burger']\",\"required\":true},{\"label\":\"Favorite Foods\",\"fieldType\":\"{'Pizza', 'Schnitzel', 'Burger'}\",\"required\":true}]`\n\n\turequire.NotPanics(t, cur, func() {\n\t\tid := CreateForm(cross(cur), title, description, openAt, closeAt, data)\n\n\t\tform := GetFormByID(id)\n\n\t\turequire.True(t, strings.Contains(form, data), \"Form JSON was not rebuilt properly\")\n\t})\n}\n\nfunc TestGetForms(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetOriginCaller(alice)\n\tdescription := \"This is a form\"\n\topenAt := \"2021-01-01T00:00:00Z\"\n\tcloseAt := \"2021-01-02T00:00:00Z\"\n\n\turequire.NotPanics(t, cur, func() {\n\t\tdata1 := `[{\"label\":\"Name\",\"fieldType\":\"string\",\"required\":true}]`\n\t\tCreateForm(cross(cur), \"NameForm\", description, openAt, closeAt, data1)\n\t\tdata2 := `[{\"label\":\"Age\",\"fieldType\":\"number\",\"required\":false}]`\n\t\tCreateForm(cross(cur), \"AgeForm\", description, openAt, closeAt, data2)\n\n\t\tforms := GetForms()\n\n\t\turequire.True(t, strings.Contains(forms, data1) \u0026\u0026 strings.Contains(forms, data2), \"Forms JSON were not rebuilt properly\")\n\t})\n\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/agherasie/forms\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"RjKhHvGU1o2YUVPq+sDaTLHZRfNafBPNrhjQK0aa2QDCOg7U8tLvWPvV9tcLP+jw+eTny4+kB2YzI2iA0S24Bg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"banktest","path":"gno.land/r/archive/banktest","files":[{"name":"README.md","body":"This is a simple test realm contract that demonstrates how to use the banker.\n\nSee [gno.land/r/demo/banktest/banktest.go](/r/demo/banktest/banktest.go) to see the original contract code.\n\nThis article will go through each line to explain how it works.\n\n```go\npackage banktest\n```\n\nThis package is locally named \"banktest\" (could be anything).\n\n```go\nimport (\n    \"std\"\n)\n```\n\nThe \"std\" package is defined by the gno code in stdlibs/std/. \u003c/br\u003e Self explanatory; and you'll see more usage from std later.\n\n```go\ntype activity struct {\n    caller   address\n    sent     std.Coins\n    returned std.Coins\n    time     time.Time\n}\n\nfunc (act *activity) String() string {\n    return act.caller.String() + \" \" +\n        act.sent.String() + \" sent, \" +\n        act.returned.String() + \" returned, at \" +\n        act.time.Format(\"2006-01-02 3:04pm MST\")\n}\n\nvar latest [10]*activity\n```\n\nThis is just maintaining a list of recent activity to this contract. Notice that the \"latest\" variable is defined \"globally\" within the context of the realm with path \"gno.land/r/demo/banktest\".\n\nThis means that calls to functions defined within this package are encapsulated within this \"data realm\", where the data is mutated based on transactions that can potentially cross many realm and non-realm package boundaries (in the call stack).\n\n```go\n// Deposit will take the coins (to the realm's pkgaddr) or return them to user.\nfunc Deposit(returnDenom string, returnAmount int64) string {\n    std.AssertOriginCall()\n    caller := std.OriginCaller()\n    send := std.Coins{{returnDenom, returnAmount}}\n```\n\nThis is the beginning of the definition of the contract function named \"Deposit\". `std.AssertOriginCall() asserts that this function was called by a gno transactional Message. The caller is the user who signed off on this transactional message. Send is the amount of deposit sent along with this message.\n\n```go\n    // record activity\n    act := \u0026activity{\n        caller:   caller,\n        sent:     std.OriginSend(),\n        returned: send,\n        time:     time.Now(),\n    }\n    for i := len(latest) - 2; i \u003e= 0; i-- {\n        latest[i+1] = latest[i] // shift by +1.\n    }\n    latest[0] = act\n```\n\nUpdating the \"latest\" array for viewing at gno.land/r/demo/banktest: (w/ trailing colon).\n\n```go\n    // return if any.\n    if returnAmount \u003e 0 {\n```\n\nIf the user requested the return of coins...\n\n```go\n        banker := std.NewBanker(std.BankerTypeOriginSend)\n```\n\nuse a std.Banker instance to return any deposited coins to the original sender.\n\n```go\n        pkgaddr := std.CurrentRealm().Address()\n        // TODO: use std.Coins constructors, this isn't generally safe.\n        banker.SendCoins(pkgaddr, caller, send)\n        return \"returned!\"\n```\n\nNotice that each realm package has an associated Cosmos address.\n\nFinally, the results are rendered via an ABCI query call when you visit [/r/demo/banktest:](/r/demo/banktest:).\n\n```go\nfunc Render(path string) string {\n    // get realm coins.\n    banker := std.NewReadonlyBanker()\n    coins := banker.GetCoins(std.CurrentRealm().Address())\n\n    // render\n    res := \"\"\n    res += \"## recent activity\\n\"\n    res += \"\\n\"\n    for _, act := range latest {\n        if act == nil {\n            break\n        }\n        res += \" * \" + act.String() + \"\\n\"\n    }\n    res += \"\\n\"\n    res += \"## total deposits\\n\"\n    res += coins.String()\n    return res\n}\n```\n\nYou can call this contract yourself, by vistiing [/r/demo/banktest](/r/demo/banktest) and the [quickstart guide](/r/demo/boards:gnolang/4).\n"},{"name":"banktest.gno","body":"package banktest\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"time\"\n)\n\ntype activity struct {\n\tcaller   address\n\tsent     chain.Coins\n\treturned chain.Coins\n\ttime     time.Time\n}\n\nfunc (act *activity) String() string {\n\treturn act.caller.String() + \" \" +\n\t\tact.sent.String() + \" sent, \" +\n\t\tact.returned.String() + \" returned, at \" +\n\t\tact.time.Format(\"2006-01-02 3:04pm MST\")\n}\n\nvar latest [10]*activity\n\nfunc addActivity(act *activity) {\n\tfor i := len(latest) - 2; i \u003e= 0; i-- {\n\t\tlatest[i+1] = latest[i] // shift by +1.\n\t}\n\tlatest[0] = act\n}\n\n// Deposit will take the coins (to the realm's pkgaddr) if returnAmount is 0,\n// or return the specified return amount back to user.\nfunc Deposit(cur realm, returnDenom string, returnAmount int64) string {\n\truntime.AssertOriginCall()\n\tcaller := unsafe.OriginCaller()\n\tsend := chain.Coins{{returnDenom, returnAmount}}\n\t// record activity\n\tact := \u0026activity{\n\t\tcaller:   caller,\n\t\tsent:     unsafe.OriginSend(),\n\t\treturned: send,\n\t\ttime:     time.Now(),\n\t}\n\taddActivity(act)\n\t// return if any.\n\tif returnAmount \u003e 0 {\n\t\tbanker_ := banker.NewBanker(banker.BankerTypeOriginSend, cur)\n\t\tpkgaddr := cur.Address()\n\t\t// TODO: use std.Coins constructors, this isn't generally safe.\n\t\tbanker_.SendCoins(pkgaddr, caller, send)\n\t\treturn \"returned!\"\n\t} else {\n\t\treturn \"thank you!\"\n\t}\n}\n\nfunc bankerAddr(cur realm) address {\n\treturn cur.Address()\n}\n\nfunc Render(cur realm, path string) string {\n\t// get realm coins.\n\tbanker_ := banker.NewReadonlyBanker()\n\tcoins := banker_.GetCoins(bankerAddr(cross(cur)))\n\n\t// render\n\tres := \"\"\n\tres += \"## recent activity\\n\"\n\tres += \"\\n\"\n\tfor _, act := range latest {\n\t\tif act == nil {\n\t\t\tbreak\n\t\t}\n\t\tres += \" * \" + act.String() + \"\\n\"\n\t}\n\tres += \"\\n\"\n\tres += \"## total deposits\\n\"\n\tres += coins.String()\n\treturn res\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/banktest\"\ngno = \"0.9\"\n"},{"name":"z_0_filetest.gno","body":"// PKGPATH: gno.land/r/demo/bank1\n// SEND: 100000000ugnot\n\npackage bank1\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/banktest\"\n)\n\nfunc main(cur realm) {\n\t// set up main address and banktest addr.\n\tbanktestAddr := chain.PackageAddress(\"gno.land/r/archive/banktest\")\n\tmainaddr := chain.PackageAddress(\"gno.land/r/demo/bank1\")\n\ttesting.SetOriginCaller(mainaddr)\n\n\t// get and print balance of mainaddr.\n\t// with the SEND, + 200 gnot given by the TestContext, main should have 300gnot.\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tmainbal := banker_.GetCoins(mainaddr)\n\tprintln(\"main before:\", mainbal)\n\n\t// simulate a Deposit call. use Send + OriginSend to simulate -send.\n\tbanker_.SendCoins(mainaddr, banktestAddr, chain.Coins{{\"ugnot\", 100_000_000}})\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 100_000_000}})\n\ttesting.SetRealm(testing.NewUserRealm(mainaddr))\n\tres := banktest.Deposit(cross(cur), \"ugnot\", 50_000_000) // bank1 can't send? should be r/demo/bank1 to r/demo/banktest, is bank1 -\u003e bank1.\n\tprintln(\"Deposit():\", res)\n\n\t// print main balance after.\n\tmainbal = banker_.GetCoins(mainaddr)\n\tprintln(\"main after:\", mainbal)\n\n\t// simulate a Render(). banker should have given back all coins.\n\tres = banktest.Render(cross(cur), \"\")\n\tprintln(res)\n}\n\n// Output:\n// main before: 100000000ugnot\n// Deposit(): returned!\n// main after: 50000000ugnot\n// ## recent activity\n//\n//  * g1tnpdmvrmtgql8fmxgsq9rwtst5hsxahk3f05dk 100000000ugnot sent, 50000000ugnot returned, at 2009-02-13 11:31pm UTC\n//\n// ## total deposits\n// 50000000ugnot\n"},{"name":"z_1_filetest.gno","body":"// PKGPATH: gno.land/r/demo/bank1\n\npackage bank1\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/banktest\"\n)\n\nfunc main(cur realm) {\n\tbanktestAddr := chain.PackageAddress(\"gno.land/r/archive/banktest\")\n\tmainaddr := chain.PackageAddress(\"gno.land/r/demo/bank1\")\n\n\t// simulate a Deposit call.\n\ttesting.IssueCoins(banktestAddr, chain.Coins{{\"ugnot\", 100000000}})\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 100000000}})\n\ttesting.SetRealm(testing.NewUserRealm(mainaddr))\n\n\tres := banktest.Deposit(cross(cur), \"ugnot\", 101000000)\n\tprintln(res)\n}\n\n// Error:\n// cannot send \"101000000ugnot\", limit \"100000000ugnot\" exceeded with \"\" already spent\n"},{"name":"z_2_filetest.gno","body":"// PKGPATH: gno.land/r/demo/bank1\n\npackage bank1\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/banktest\"\n)\n\nfunc main(cur realm) {\n\tbanktestAddr := chain.PackageAddress(\"gno.land/r/archive/banktest\")\n\tmainaddr := chain.PackageAddress(\"gno.land/r/demo/bank1\")\n\n\t// print main balance before.\n\ttesting.SetOriginCaller(mainaddr)\n\n\tbanker_ := banker.NewReadonlyBanker()\n\tmainbal := banker_.GetCoins(mainaddr)\n\tprintln(\"main before:\", mainbal) // plus OriginSend equals 300.\n\n\t// simulate a Deposit call.\n\ttesting.IssueCoins(banktestAddr, chain.Coins{{\"ugnot\", 100000000}})\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 100000000}})\n\ttesting.SetRealm(testing.NewUserRealm(mainaddr))\n\tres := banktest.Deposit(cross(cur), \"ugnot\", 55000000)\n\tprintln(\"Deposit():\", res)\n\n\t// print main balance after.\n\tmainbal = banker_.GetCoins(mainaddr)\n\tprintln(\"main after:\", mainbal) // now 255.\n\n\t// simulate a Render().\n\tres = banktest.Render(cross(cur), \"\")\n\tprintln(res)\n}\n\n// Output:\n// main before:\n// Deposit(): returned!\n// main after: 55000000ugnot\n// ## recent activity\n//\n//  * g1tnpdmvrmtgql8fmxgsq9rwtst5hsxahk3f05dk 100000000ugnot sent, 55000000ugnot returned, at 2009-02-13 11:31pm UTC\n//\n// ## total deposits\n// 45000000ugnot\n"},{"name":"z_3_filetest.gno","body":"// PKGPATH: gno.land/r/demo/bank1\n\npackage bank1\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n)\n\nfunc main(cur realm) {\n\tbanktestAddr := chain.PackageAddress(\"gno.land/r/archive/banktest\")\n\n\tmainaddr := chain.PackageAddress(\"gno.land/r/demo/bank1\")\n\ttesting.SetOriginCaller(mainaddr)\n\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tsend := chain.Coins{{\"ugnot\", 123}}\n\tbanker_.SendCoins(banktestAddr, mainaddr, send)\n}\n\n// Error:\n// can only send coins from realm that created banker \"g1tnpdmvrmtgql8fmxgsq9rwtst5hsxahk3f05dk\", not \"g1hsf4l9pxp4vklr6udmr29ztgqvhswjl8mt78fh\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"NimUERAgyVbBaFRKscGuVAQK5GqRkRdXe++r5Z7AMn0EYp86jyxENZMXeq35rAI9v5EV9T16vAtxNNEeU1dNBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"groups","path":"gno.land/r/archive/groups","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/groups\"\ngno = \"0.9\"\n"},{"name":"group.gno","body":"package groups\n\nimport (\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\ntype GroupID uint64\n\nfunc (gid GroupID) String() string {\n\treturn strconv.Itoa(int(gid))\n}\n\ntype Group struct {\n\tid           GroupID\n\turl          string\n\tname         string\n\tlastMemberID MemberID\n\tmembers      avl.Tree\n\tcreator      address\n\tcreatedAt    time.Time\n}\n\nfunc newGroup(url string, name string, creator address) *Group {\n\tif !reName.MatchString(name) {\n\t\tpanic(\"invalid name: \" + name)\n\t}\n\tif gGroupsByName.Has(name) {\n\t\tpanic(\"Group with such name already exists\")\n\t}\n\treturn \u0026Group{\n\t\tid:        incGetGroupID(),\n\t\turl:       url,\n\t\tname:      name,\n\t\tcreator:   creator,\n\t\tmembers:   avl.Tree{},\n\t\tcreatedAt: time.Now(),\n\t}\n}\n\nfunc (group *Group) newMember(id MemberID, address_XXX address, weight int, metadata string) *Member {\n\tif group.members.Has(address_XXX.String()) {\n\t\tpanic(\"this member for this group already exists\")\n\t}\n\treturn \u0026Member{\n\t\tid:          id,\n\t\taddress_XXX: address_XXX,\n\t\tweight:      weight,\n\t\tmetadata:    metadata,\n\t\tcreatedAt:   time.Now(),\n\t}\n}\n\nfunc (group *Group) HasPermission(addr address, perm Permission) bool {\n\tif group.creator != addr {\n\t\treturn false\n\t}\n\treturn isValidPermission(perm)\n}\n\nfunc (group *Group) RenderGroup() string {\n\tstr := \"Group ID: \" + groupIDKey(group.id) + \"\\n\\n\" +\n\t\t\"Group Name: \" + group.name + \"\\n\\n\" +\n\t\t\"Group Creator: \" + usernameOf(group.creator) + \"\\n\\n\" +\n\t\t\"Group createdAt: \" + group.createdAt.String() + \"\\n\\n\" +\n\t\t\"Group Last MemberID: \" + memberIDKey(group.lastMemberID) + \"\\n\\n\"\n\n\tstr += \"Group Members: \\n\\n\"\n\tgroup.members.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tmember := value.(*Member)\n\t\tstr += member.getMemberStr()\n\t\treturn false\n\t})\n\treturn str\n}\n\nfunc (group *Group) deleteGroup() {\n\tgidkey := groupIDKey(group.id)\n\t_, gGroupsRemoved := gGroups.Remove(gidkey)\n\tif !gGroupsRemoved {\n\t\tpanic(\"group does not exist with id \" + group.id.String())\n\t}\n\tgGroupsByName.Remove(group.name)\n}\n\nfunc (group *Group) deleteMember(mid MemberID) {\n\tgidkey := groupIDKey(group.id)\n\tif !gGroups.Has(gidkey) {\n\t\tpanic(\"group does not exist with id \" + group.id.String())\n\t}\n\n\tg := getGroup(group.id)\n\tmidkey := memberIDKey(mid)\n\tg.members.Remove(midkey)\n}\n"},{"name":"groups.gno","body":"package groups\n\nimport (\n\t\"regexp\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n//----------------------------------------\n// Realm (package) state\n\nvar (\n\tgGroups       avl.Tree // id -\u003e *Group\n\tgGroupsCtr    int      // increments Group.id\n\tgGroupsByName avl.Tree // name -\u003e *Group\n)\n\n//----------------------------------------\n// Constants\n\nvar reName = regexp.MustCompile(`^[a-z]+[_a-z0-9]{2,29}$`)\n"},{"name":"member.gno","body":"package groups\n\nimport (\n\t\"strconv\"\n\t\"time\"\n)\n\ntype MemberID uint64\n\ntype Member struct {\n\tid          MemberID\n\taddress_XXX address\n\tweight      int\n\tmetadata    string\n\tcreatedAt   time.Time\n}\n\nfunc (mid MemberID) String() string {\n\treturn strconv.Itoa(int(mid))\n}\n\nfunc (member *Member) getMemberStr() string {\n\tmemberDataStr := \"\"\n\tmemberDataStr += \"\\t\\t\\t[\" + memberIDKey(member.id) + \", \" + member.address_XXX.String() + \", \" + strconv.Itoa(member.weight) + \", \" + member.metadata + \", \" + member.createdAt.String() + \"],\\n\\n\"\n\treturn memberDataStr\n}\n"},{"name":"misc.gno","body":"package groups\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/r/sys/users\"\n)\n\n//----------------------------------------\n// private utility methods\n// XXX ensure these cannot be called from public.\n\nfunc getGroup(gid GroupID) *Group {\n\tgidkey := groupIDKey(gid)\n\tgroup_ := gGroups.Get(gidkey)\n\tif group_ == nil {\n\t\tpanic(\"group id (\" + gid.String() + \") does not exists\")\n\t}\n\tgroup := group_.(*Group)\n\treturn group\n}\n\nfunc incGetGroupID() GroupID {\n\tgGroupsCtr++\n\treturn GroupID(gGroupsCtr)\n}\n\nfunc padLeft(str string, length int) string {\n\tif len(str) \u003e= length {\n\t\treturn str\n\t}\n\treturn strings.Repeat(\" \", length-len(str)) + str\n}\n\nfunc padZero(u64 uint64, length int) string {\n\tstr := strconv.Itoa(int(u64))\n\tif len(str) \u003e= length {\n\t\treturn str\n\t}\n\treturn strings.Repeat(\"0\", length-len(str)) + str\n}\n\nfunc groupIDKey(gid GroupID) string {\n\treturn padZero(uint64(gid), 10)\n}\n\nfunc memberIDKey(mid MemberID) string {\n\treturn padZero(uint64(mid), 10)\n}\n\nfunc indentBody(indent string, body string) string {\n\tlines := strings.Split(body, \"\\n\")\n\tres := \"\"\n\tfor i, line := range lines {\n\t\tif i \u003e 0 {\n\t\t\tres += \"\\n\"\n\t\t}\n\t\tres += indent + line\n\t}\n\treturn res\n}\n\n// NOTE: length must be greater than 3.\nfunc summaryOf(str string, length int) string {\n\tlines := strings.SplitN(str, \"\\n\", 2)\n\tline := lines[0]\n\tif len(line) \u003e length {\n\t\tline = line[:(length-3)] + \"...\"\n\t} else if len(lines) \u003e 1 {\n\t\t// len(line) \u003c= 80\n\t\tline = line + \"...\"\n\t}\n\treturn line\n}\n\nfunc displayAddressMD(addr address) string {\n\tuser := users.ResolveAddress(addr)\n\tif user == nil {\n\t\treturn \"[\" + addr.String() + \"](/u/\" + addr.String() + \")\"\n\t}\n\treturn \"[@\" + user.Name() + \"](/u/\" + user.Name() + \")\"\n}\n\nfunc usernameOf(addr address) string {\n\tuser := users.ResolveAddress(addr)\n\tif user == nil {\n\t\tpanic(\"user not found\")\n\t}\n\treturn user.Name()\n}\n\nfunc isValidPermission(perm Permission) bool {\n\treturn perm == EditPermission || perm == DeletePermission\n}\n"},{"name":"public.gno","body":"package groups\n\nimport (\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/r/sys/users\"\n)\n\n//----------------------------------------\n// Public facing functions\n\nfunc GetGroupIDFromName(name string) (GroupID, bool) {\n\tgroupI := gGroupsByName.Get(name)\n\tif groupI == nil {\n\t\treturn 0, false\n\t}\n\treturn groupI.(*Group).id, true\n}\n\nfunc CreateGroup(cur realm, name string) GroupID {\n\truntime.AssertOriginCall()\n\tcaller := unsafe.OriginCaller()\n\tusernameOf(caller)\n\turl := \"/r/demo/groups:\" + name\n\tgroup := newGroup(url, name, caller)\n\tgidkey := groupIDKey(group.id)\n\tgGroups.Set(gidkey, group)\n\tgGroupsByName.Set(name, group)\n\treturn group.id\n}\n\nfunc AddMember(cur realm, gid GroupID, address_XXX string, weight int, metadata string) MemberID {\n\truntime.AssertOriginCall()\n\tcaller := unsafe.OriginCaller()\n\tusernameOf(caller)\n\tgroup := getGroup(gid)\n\tif !group.HasPermission(caller, EditPermission) {\n\t\tpanic(\"unauthorized to edit group\")\n\t}\n\tuser := users.ResolveAddress(address(address_XXX))\n\tif user == nil {\n\t\tpanic(\"unknown address \" + address_XXX)\n\t}\n\tmid := group.lastMemberID\n\tmember := group.newMember(mid, address(address_XXX), weight, metadata)\n\tmidkey := memberIDKey(mid)\n\tgroup.members.Set(midkey, member)\n\tmid++\n\tgroup.lastMemberID = mid\n\treturn member.id\n}\n\nfunc DeleteGroup(cur realm, gid GroupID) {\n\truntime.AssertOriginCall()\n\tcaller := unsafe.OriginCaller()\n\tgroup := getGroup(gid)\n\tif !group.HasPermission(caller, DeletePermission) {\n\t\tpanic(\"unauthorized to delete group\")\n\t}\n\tgroup.deleteGroup()\n}\n\nfunc DeleteMember(cur realm, gid GroupID, mid MemberID) {\n\truntime.AssertOriginCall()\n\tcaller := unsafe.OriginCaller()\n\tgroup := getGroup(gid)\n\tif !group.HasPermission(caller, DeletePermission) {\n\t\tpanic(\"unauthorized to delete member\")\n\t}\n\tgroup.deleteMember(mid)\n}\n"},{"name":"render.gno","body":"package groups\n\nimport (\n\t\"strings\"\n)\n\n//----------------------------------------\n// Render functions\n\nfunc RenderGroup(gid GroupID) string {\n\tgroup := getGroup(gid)\n\tif group == nil {\n\t\treturn \"missing Group\"\n\t}\n\treturn group.RenderGroup()\n}\n\nfunc Render(path string) string {\n\tif path == \"\" {\n\t\tstr := \"List of all Groups:\\n\\n\"\n\t\tgGroups.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tgroup := value.(*Group)\n\t\t\tstr += \" * [\" + group.name + \"](\" + group.url + \")\\n\"\n\t\t\treturn false\n\t\t})\n\t\treturn str\n\t}\n\tparts := strings.Split(path, \"/\")\n\tif len(parts) == 1 {\n\t\t// /r/demo/groups:Group_NAME\n\t\tname := parts[0]\n\t\tgroupI := gGroupsByName.Get(name)\n\t\tif groupI == nil {\n\t\t\treturn \"Group does not exist: \" + name\n\t\t}\n\t\treturn groupI.(*Group).RenderGroup()\n\t} else {\n\t\treturn \"unrecognized path \" + path\n\t}\n}\n"},{"name":"role.gno","body":"package groups\n\ntype Permission string\n\nconst (\n\tDeletePermission Permission = \"role:delete\"\n\tEditPermission   Permission = \"role:edit\"\n)\n"},{"name":"z_0_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_0_a\npackage z_0_a\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"gno.land/r/archive/groups\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\tprintln(groups.Render(\"\"))\n}\n\n// Error:\n// user not found\n"},{"name":"z_0_c_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_0_c\npackage z_0_c\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\tprintln(groups.Render(\"\"))\n}\n\n// Output:\n// 1\n// List of all Groups:\n//\n//  * [test_group](/r/demo/groups:test_group)\n"},{"name":"z_1_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_1_a\npackage z_1_a\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nconst admin = address(\"g1manfred47kzduec920z88wfr64ylksmdcedlf5\")\n\nfunc main(cur realm) {\n\tcaller := unsafe.OriginCaller() // main\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"main123\", caller)\n\ttesting.SetHeight(123)\n\n\ttest1 := testutils.TestAddress(\"gnouser1\")\n\ttesting.SetOriginCaller(test1)\n\ttesting.SetRealm(testing.NewUserRealm(test1))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"test123\", test1)\n\ttesting.SetHeight(123)\n\n\ttest2 := testutils.TestAddress(\"gnouser2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetRealm(testing.NewUserRealm(test2))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"test223\", test2)\n\ttesting.SetHeight(123)\n\n\ttest3 := testutils.TestAddress(\"gnouser3\")\n\ttesting.SetOriginCaller(test3)\n\ttesting.SetRealm(testing.NewUserRealm(test3))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"test323\", test3)\n\ttesting.SetHeight(123)\n\n\ttesting.SetOriginCaller(caller)\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\n\tgroups.AddMember(cross(cur), gid, test3.String(), 32, \"i am from UAE\")\n\tprintln(groups.Render(\"test_group\"))\n}\n\n// Output:\n// 1\n// Group ID: 0000000001\n//\n// Group Name: test_group\n//\n// Group Creator: main123\n//\n// Group createdAt: 2009-02-13 23:31:30 +0000 UTC\n//\n// Group Last MemberID: 0000000001\n//\n// Group Members:\n//\n// \t\t\t[0000000000, g1vahx7atnv4erxh6lta047h6lta047h6ll85gpy, 32, i am from UAE, 2009-02-13 23:31:30 +0000 UTC],\n"},{"name":"z_1_b_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_1_b\npackage z_1_b\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\tcaller := unsafe.OriginCaller()\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", caller)\n\ttesting.SetHeight(123)\n\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\tgroups.AddMember(cross(cur), 2, \"g1vahx7atnv4erxh6lta047h6lta047h6ll85gpy\", 55, \"metadata3\")\n\tprintln(groups.Render(\"\"))\n}\n\n// Error:\n// group id (2) does not exists\n"},{"name":"z_1_c_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_1_c\npackage z_1_c\n\n// SEND: 200000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/groups\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\n\t// add member via anon user\n\ttest2 := testutils.TestAddress(\"test2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 9000000}})\n\n\tgroups.AddMember(cross(cur), gid, test2.String(), 42, \"metadata3\")\n}\n\n// Error:\n// user not found\n"},{"name":"z_2_a_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_2_a\npackage z_2_a\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nconst admin = address(\"g1manfred47kzduec920z88wfr64ylksmdcedlf5\")\n\nfunc main(cur realm) {\n\tcaller := unsafe.OriginCaller() // main123\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"main123\", caller)\n\ttesting.SetHeight(123)\n\n\ttest1 := testutils.TestAddress(\"gnouser1\")\n\ttesting.SetOriginCaller(test1)\n\ttesting.SetRealm(testing.NewUserRealm(test1))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"test123\", test1)\n\ttesting.SetHeight(123)\n\n\ttest2 := testutils.TestAddress(\"gnouser2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetRealm(testing.NewUserRealm(test2))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"test223\", test2)\n\ttesting.SetHeight(123)\n\n\ttest3 := testutils.TestAddress(\"gnouser3\")\n\ttesting.SetOriginCaller(test3)\n\ttesting.SetRealm(testing.NewUserRealm(test3))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"test323\", test3)\n\ttesting.SetHeight(123)\n\n\ttesting.SetOriginCaller(caller)\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(groups.Render(\"test_group\"))\n\n\tgroups.AddMember(cross(cur), gid, test2.String(), 42, \"metadata3\")\n\n\tgroups.DeleteMember(cross(cur), gid, 0)\n\tprintln(groups.RenderGroup(gid))\n}\n\n// Output:\n// Group ID: 0000000001\n//\n// Group Name: test_group\n//\n// Group Creator: main123\n//\n// Group createdAt: 2009-02-13 23:31:30 +0000 UTC\n//\n// Group Last MemberID: 0000000000\n//\n// Group Members:\n//\n//\n// Group ID: 0000000001\n//\n// Group Name: test_group\n//\n// Group Creator: main123\n//\n// Group createdAt: 2009-02-13 23:31:30 +0000 UTC\n//\n// Group Last MemberID: 0000000001\n//\n// Group Members:\n"},{"name":"z_2_b_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_2_b\npackage z_2_b\n\n// SEND: 200000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\tgroups.DeleteMember(cross(cur), 2, 0)\n\tprintln(groups.Render(\"\"))\n}\n\n// Output:\n// 1\n\n// Error:\n// group id (2) does not exists\n"},{"name":"z_2_d_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_2_d\npackage z_2_d\n\n// SEND: 200000000ugnot\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\n\t// delete member via anon user\n\ttest2 := testutils.TestAddress(\"test2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 9000000}})\n\n\tgroups.DeleteMember(cross(cur), gid, 0)\n\tprintln(groups.Render(\"\"))\n}\n\n// Output:\n// 1\n\n// Error:\n// unauthorized to delete member\n"},{"name":"z_2_e_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_2_e\npackage z_2_e\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))) // so that CurrentRealm.Addr() matches OrigCaller\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", address(\"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\"))\n\ttesting.SetHeight(123)\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\tgroups.DeleteGroup(cross(cur), gid)\n\tprintln(groups.Render(\"\"))\n}\n\n// Output:\n// 1\n// List of all Groups:\n"},{"name":"z_2_f_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_2_f\npackage z_2_f\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\tcaller := unsafe.OriginCaller()\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", caller)\n\ttesting.SetHeight(123)\n\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\n\tprintln(gid)\n\tgroups.DeleteGroup(cross(cur), 20)\n\tprintln(groups.Render(\"\"))\n}\n\n// Error:\n// group id (20) does not exists\n"},{"name":"z_2_g_filetest.gno","body":"// PKGPATH: gno.land/r/archive/groups/filetests/z_2_g\npackage z_2_g\n\n// SEND: 1000000ugnot\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/archive/groups\"\n\tuinit \"gno.land/r/sys/users/init\"\n)\n\nvar gid groups.GroupID\n\nfunc main(cur realm) {\n\tcaller := unsafe.OriginCaller()\n\ttesting.SetRealm(testing.NewUserRealm(caller))\n\ttesting.SetHeight(0)\n\tuinit.RegisterUser(cross(cur), \"gnouser123\", caller)\n\ttesting.SetHeight(123)\n\n\tgid = groups.CreateGroup(cross(cur), \"test_group\")\n\tprintln(gid)\n\n\t// delete group via anon user\n\ttest2 := testutils.TestAddress(\"test2\")\n\ttesting.SetOriginCaller(test2)\n\ttesting.SetOriginSend(chain.Coins{{\"ugnot\", 9000000}})\n\n\tgroups.DeleteGroup(cross(cur), gid)\n\tprintln(groups.Render(\"\"))\n}\n\n// Error:\n// unauthorized to delete group\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"6vg4sC6+WHHbb963vu0K4tz6E2iniNsEQxVifkPCpKPNwzQsXs6OwCWZTKOT0yJ6JUUorQnStlPPWl0VIIIiAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"keystore","path":"gno.land/r/archive/keystore","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/keystore\"\ngno = \"0.9\"\n"},{"name":"keystore.gno","body":"package keystore\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar data avl.Tree\n\nconst (\n\tBaseURL               = \"/r/demo/keystore\"\n\tStatusOK              = \"ok\"\n\tStatusNoUser          = \"user not found\"\n\tStatusNotFound        = \"key not found\"\n\tStatusNoWriteAccess   = \"no write access\"\n\tStatusCouldNotExecute = \"could not execute\"\n\tStatusNoDatabases     = \"no databases\"\n)\n\nfunc init() {\n\tdata = avl.Tree{} // user -\u003e avl.Tree\n}\n\n// KeyStore stores the owner-specific avl.Tree\ntype KeyStore struct {\n\tOwner address\n\tData  avl.Tree\n}\n\n// Set will set a value to a key\n// requires write-access (original caller must be caller)\nfunc Set(_ realm, k, v string) string {\n\torigOwner := unsafe.OriginCaller()\n\treturn set(origOwner.String(), k, v)\n}\n\n// set (private) will set a key to value\n// requires write-access (original caller must be caller)\nfunc set(owner, k, v string) string {\n\torigOwner := unsafe.OriginCaller()\n\tif origOwner.String() != owner {\n\t\treturn StatusNoWriteAccess\n\t}\n\tvar keystore *KeyStore\n\tkeystoreInterface := data.Get(owner)\n\tif keystoreInterface == nil {\n\t\tkeystore = \u0026KeyStore{\n\t\t\tOwner: origOwner,\n\t\t\tData:  avl.Tree{},\n\t\t}\n\t\tdata.Set(owner, keystore)\n\t} else {\n\t\tkeystore = keystoreInterface.(*KeyStore)\n\t}\n\tkeystore.Data.Set(k, v)\n\treturn StatusOK\n}\n\n// Remove removes a key\n// requires write-access (original owner must be caller)\nfunc Remove(_ realm, k string) string {\n\torigOwner := unsafe.OriginCaller()\n\treturn remove(origOwner.String(), k)\n}\n\n// remove (private) removes a key\n// requires write-access (original owner must be caller)\nfunc remove(owner, k string) string {\n\torigOwner := unsafe.OriginCaller()\n\tif origOwner.String() != owner {\n\t\treturn StatusNoWriteAccess\n\t}\n\tvar keystore *KeyStore\n\tkeystoreInterface := data.Get(owner)\n\tif keystoreInterface == nil {\n\t\tkeystore = \u0026KeyStore{\n\t\t\tOwner: origOwner,\n\t\t\tData:  avl.Tree{},\n\t\t}\n\t\tdata.Set(owner, keystore)\n\t} else {\n\t\tkeystore = keystoreInterface.(*KeyStore)\n\t}\n\t_, removed := keystore.Data.Remove(k)\n\tif !removed {\n\t\treturn StatusCouldNotExecute\n\t}\n\treturn StatusOK\n}\n\n// Get returns a value for a key\n// read-only\nfunc Get(k string) string {\n\torigOwner := unsafe.OriginCaller()\n\treturn remove(origOwner.String(), k)\n}\n\n// get (private) returns a value for a key\n// read-only\nfunc get(owner, k string) string {\n\tkeystoreInterface := data.Get(owner)\n\tif keystoreInterface == nil {\n\t\treturn StatusNoUser\n\t}\n\tkeystore := keystoreInterface.(*KeyStore)\n\tval := keystore.Data.Get(k)\n\tif val == nil {\n\t\treturn StatusNotFound\n\t}\n\treturn val.(string)\n}\n\n// Size returns size of database\n// read-only\nfunc Size() string {\n\torigOwner := unsafe.OriginCaller()\n\treturn size(origOwner.String())\n}\n\nfunc size(owner string) string {\n\tkeystoreInterface := data.Get(owner)\n\tif keystoreInterface == nil {\n\t\treturn StatusNoUser\n\t}\n\tkeystore := keystoreInterface.(*KeyStore)\n\treturn ufmt.Sprintf(\"%d\", keystore.Data.Size())\n}\n\n// Render provides read-only url access to the functions of the keystore\n// \"\" -\u003e show all keystores listed by owner\n// \"owner\" -\u003e show all keys for that owner's keystore\n// \"owner:size\" -\u003e returns size of owner's keystore\n// \"owner:get:key\" -\u003e show value for that key in owner's keystore\nfunc Render(p string) string {\n\tvar response string\n\targs := strings.Split(p, \":\")\n\tnumArgs := len(args)\n\tif p == \"\" {\n\t\tnumArgs = 0\n\t}\n\tswitch numArgs {\n\tcase 0:\n\t\tif data.Size() == 0 {\n\t\t\treturn StatusNoDatabases\n\t\t}\n\t\tdata.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tks := value.(*KeyStore)\n\t\t\tresponse += ufmt.Sprintf(\"- [%s](%s:%s) (%d keys)\\n\", ks.Owner, BaseURL, ks.Owner, ks.Data.Size())\n\t\t\treturn false\n\t\t})\n\tcase 1:\n\t\towner := args[0]\n\t\tkeystoreInterface := data.Get(owner)\n\t\tif keystoreInterface == nil {\n\t\t\treturn StatusNoUser\n\t\t}\n\t\tks := keystoreInterface.(*KeyStore)\n\t\ti := 0\n\t\tresponse += ufmt.Sprintf(\"# %s database\\n\\n\", ks.Owner)\n\t\tks.Data.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tresponse += ufmt.Sprintf(\"- %d [%s](%s:%s:get:%s)\\n\", i, key, BaseURL, ks.Owner, key)\n\t\t\ti++\n\t\t\treturn false\n\t\t})\n\tcase 2:\n\t\towner := args[0]\n\t\tcmd := args[1]\n\t\tif cmd == \"size\" {\n\t\t\treturn size(owner)\n\t\t}\n\tcase 3:\n\t\towner := args[0]\n\t\tcmd := args[1]\n\t\tkey := args[2]\n\t\tif cmd == \"get\" {\n\t\t\treturn get(owner, key)\n\t\t}\n\t}\n\n\treturn response\n}\n"},{"name":"keystore_test.gno","body":"package keystore\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestRender(cur realm, t *testing.T) {\n\tvar (\n\t\tauthor1 address = testutils.TestAddress(\"author1\")\n\t\tauthor2 address = testutils.TestAddress(\"author2\")\n\t)\n\n\ttt := []struct {\n\t\tcaller address\n\t\towner  address\n\t\tps     []string\n\t\texp    string\n\t}{\n\t\t// can set database if the owner is the caller\n\t\t{author1, author1, []string{\"set\", \"hello\", \"gno\"}, StatusOK},\n\t\t{author1, author1, []string{\"size\"}, \"1\"},\n\t\t{author1, author1, []string{\"set\", \"hello\", \"world\"}, StatusOK},\n\t\t{author1, author1, []string{\"size\"}, \"1\"},\n\t\t{author1, author1, []string{\"set\", \"hi\", \"gno\"}, StatusOK},\n\t\t{author1, author1, []string{\"size\"}, \"2\"},\n\t\t// only owner can remove\n\t\t{author1, author1, []string{\"remove\", \"hi\"}, StatusOK},\n\t\t{author1, author1, []string{\"get\", \"hi\"}, StatusNotFound},\n\t\t{author1, author1, []string{\"size\"}, \"1\"},\n\t\t// add back\n\t\t{author1, author1, []string{\"set\", \"hi\", \"gno\"}, StatusOK},\n\t\t{author1, author1, []string{\"size\"}, \"2\"},\n\n\t\t// different owner has different database\n\t\t{author2, author2, []string{\"set\", \"hello\", \"universe\"}, StatusOK},\n\t\t// either author can get the other info\n\t\t{author1, author2, []string{\"get\", \"hello\"}, \"universe\"},\n\t\t// either author can get the other info\n\t\t{author2, author1, []string{\"get\", \"hello\"}, \"world\"},\n\t\t{author1, author2, []string{\"get\", \"hello\"}, \"universe\"},\n\t\t// anyone can view the databases\n\t\t{author1, author2, []string{}, `- [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/keystore:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) (2 keys)\n- [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/keystore:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00) (1 keys)`},\n\t\t// anyone can view the keys in a database\n\t\t{author1, author2, []string{\"\"}, `# g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00 database\n\n- 0 [hello](/r/demo/keystore:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00:get:hello)`},\n\t}\n\tfor _, tc := range tt {\n\t\tp := \"\"\n\t\tif len(tc.ps) \u003e 0 {\n\t\t\tp = tc.owner.String()\n\t\t\tfor _, psv := range tc.ps {\n\t\t\t\tp += \":\" + psv\n\t\t\t}\n\t\t}\n\t\tp = strings.TrimSuffix(p, \":\")\n\t\tt.Run(p, func(t *testing.T) {\n\t\t\ttesting.SetOriginCaller(tc.caller)\n\t\t\tvar act string\n\t\t\tif len(tc.ps) \u003e 0 \u0026\u0026 tc.ps[0] == \"set\" {\n\t\t\t\tact = strings.TrimSpace(Set(cross(cur), tc.ps[1], tc.ps[2]))\n\t\t\t} else if len(tc.ps) \u003e 0 \u0026\u0026 tc.ps[0] == \"remove\" {\n\t\t\t\tact = strings.TrimSpace(Remove(cross(cur), tc.ps[1]))\n\t\t\t} else {\n\t\t\t\tact = strings.TrimSpace(Render(p))\n\t\t\t}\n\n\t\t\tuassert.Equal(t, tc.exp, act, ufmt.Sprintf(\"%v -\u003e '%s'\", tc.ps, p))\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"0xw/jeYgG+x3VlJ96xew3XNg8T18oUt5X3e8KWvBthR5nMeXK/T3feb2PwSDiQXWmKXytm5UHhgbJECyyRGzCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"evaluation","path":"gno.land/r/archive/nir1218_evaluation_proposal/evaluation","files":[{"name":"EVALUATION.md","body":"# `Contribution Evaluation`\n\n## Abstract\n\nThis document describes general ideas regarding contributions evaluation. The principles laid out are intended to be part of the Evaluation DAO.\n\n## Contents\n\n- [Concepts](#concepts)\n\n  - [Committee](#committee)\n  - [Evaluation](#evaluation)\n  - [Contribution](#contribution)\n  - [Pull Request](#pull-request)\n  - [Vote](#vote)\n\n- [Future Improvements](#future-improvements)\n\n- [Implementation](#implementation)\n\n## Concepts\n\n### General Ideas\n\nContributors DAO will designate members of a committee. In the beginning, the evaluation committee members will be the core development team members or any other trusted entity.\nA committee will be given the mandate to evaluate a certain set of contributions.\nFor example, the first committee will evaluate code contributions inside Gno central repository.\nA contribution will be associated with a pull request managed in Git.\nA Committee as a trusted entity can decide on a category and its corresponding evaluation criteria.\nA member can propose to add a category and its corresponding evaluation criteria.\nA member can propose a contribution for evaluation. However, the pull request category must be from the list of approved categories.\nAt the time of writing, a member can vote based on as set of options either \"YES\" or \"NO\", all members need to approve a category or a contribution.\n\n### Committee\n\nA group of designated members who are given a mandate to act as an evaluation authority.\nA DAO may elect a committee and designate its members based on contributions or merits of the members.\nA committee member can propose a contribution to avoid spam and confirm viable contributions will be evaluated.\n\n### Evaluation\n\nA logical entity to group a certain types of contributions.\n\n#### Category\n\nA group of contributions that should be evaluated based on the same principles and guide lines.\nAn example of a category is a bounty, a chore, a defect, or a document.\n\n### Contribution\n\nA contribution is associated with a pull request.\nA contribution has an evaluation life cycle.\nA submission time is set when a contribution is added.\nA last evaluation time is set when a contribution is evaluated and approved by a member.\nAn approval time is set when a contribution is approved by all members (or when a future threshold is reached)\n\n#### Submission\n\nAny committee member can submit a contribution.\n\n#### Status\n\nWhen a contribution is submitted its status is set to \"proposed\", its status will change to \"approved\" once approved by the committee or to \"declined\" otherwise.\nIntermediate status options such as \"negotiation\", \"discussion\", \"evaluation\" are TBD.\nA further discussion around the idea of deleting a contribution is required as it raises questions regarding record keeping, double evaluations, and the motive.\n\n#### Approval\n\nA contribution is approved once it reaches a certain threshold.\n\n### Pull Request\n\nA pull request from a source control tool, namely GitHub.\n\n### Vote\n\n#### Voters\n\nVoters are committee members, all committee members have the right and obligation to vote on a contribution.\n\n#### Voting Options\n\nThe voting options available to a voter.\nA committee may set voting options for its evaluation categories.\nThe initial option set includes the following options:\n\n- `YES`\n- `NO`\n\n#### Voting Period\n\nVoting period is set by the committee, all committee members are obligated to vote within the voting period.\n\n#### Threshold\n\nThreshold is the minimum percentage of `YES` votes from the total votes.\n\n#### Tally Votes\n\n## Future Improvements\n\nThe current documentation describes the basic ideas as expressed in the code.\nFuture improvements listed below will be decided based on future discussions and peer reviews.\n\n- Committee negotiates contributions\nFIXME Next line is unfinished:\n- A committee may set voting options for its categories and evaluated contributions, otherwise; the Contributors DAO may set a global\n- A committee may set a threshold required for a category or a contribution to be approved, otherwise; the Contributors DAO may set a global threshold and quorum.\n- A committee sets evaluation criteria scoring range (1-10), scoring a contribution is essential when there are competing contributions (Game of Realm). Otherwise, the evaluation is a binary decision. Moreover, scoring should be translated to rewards of any sort, or become discussion points durning negotiation about the viability of a contribution.\n- Committee members assess contributions based on the evaluation criteria and vote accordingly.\n\n## Implementation\n\nThe implementation written is to express the ideas described above using code. Not all ideas have been implemented.\n"},{"name":"category.gno","body":"package evaluation\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\ntype Category struct {\n\tname        string\n\tcriteria    []string\n\tstatus      string\n\tvotes       avl.Tree\n\ttallyResult TallyResult\n}\n\nfunc NewCategory(name string, criteria []string) *Category {\n\ttallyResult := TallyResult{}\n\ttallyResult.results.Set(VoteYes, 0)\n\ttallyResult.results.Set(VoteNo, 0)\n\n\tc := \u0026Category{\n\t\tname:        name,\n\t\tcriteria:    criteria,\n\t\tstatus:      \"Proposed\",\n\t\tvotes:       avl.Tree{},\n\t\ttallyResult: tallyResult,\n\t}\n\treturn c\n}\n\nfunc (c *Category) Approve() {\n\t// TODO error handling\n\tc.status = \"Approved\"\n}\n\nfunc (c Category) Status() string {\n\treturn c.status\n}\n\nfunc (c *Category) Tally() {\n\t// TODO error handling\n\tc.votes.Iterate(\"\", \"\", func(address_XXX string, vote any) bool {\n\t\tv := vote.(Vote)\n\t\tvalue := c.tallyResult.results.Get(v.option)\n\t\tif value == nil {\n\t\t\treturn false\n\t\t}\n\t\tcount := value.(int)\n\t\tc.tallyResult.results.Set(v.option, count+1)\n\t\treturn true\n\t})\n}\n"},{"name":"committee.gno","body":"package evaluation\n\nimport (\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Committee struct {\n\tmembers    []address // TODO - use avl tree or address set?\n\tcategories avl.Tree  // A category is mapped to a list of evaluation criteria\n\tevaluation *Evaluation\n}\n\nconst ApprovedStatus = \"Approved\"\n\nfunc NewCommittee() *Committee {\n\tc := \u0026Committee{\n\t\tmembers:    []address{},\n\t\tcategories: avl.Tree{},\n\t\tevaluation: NewEvalutaion(),\n\t}\n\treturn c\n}\n\nfunc (c *Committee) DesignateMembers(members []address) []address {\n\tc.members = append(c.members, members...)\n\treturn c.members\n}\n\nfunc (c *Committee) DismissMembers(members []address) []address {\n\t// TODO\n\treturn []address{}\n}\n\nfunc (c *Committee) AddCategory(name string, criteria []string) bool {\n\t// TODO error handling\n\tif !c.isMember(unsafe.OriginCaller()) {\n\t\treturn false\n\t}\n\tcategory := NewCategory(name, criteria)\n\tc.categories.Set(name, category)\n\treturn true\n}\n\nfunc (c *Committee) ApproveCategory(name string, option string) bool {\n\tif !c.isMember(unsafe.OriginCaller()) {\n\t\treturn false\n\t}\n\n\tvalue := c.categories.Get(name)\n\tif value == nil {\n\t\treturn false\n\t}\n\tcategory := value.(*Category)\n\tif category.Status() == ApprovedStatus {\n\t\treturn false\n\t}\n\n\tvote := NewVote(unsafe.OriginCaller(), option)\n\tcategory.votes.Set(unsafe.OriginCaller().String(), vote)\n\tcategory.Tally()\n\n\t// TODO Add threshold factor for a category approval\n\t// TODO Add quorum factor for a category approval\n\t// Current assumption is all members voted YES so category is approved\n\n\tresult := category.tallyResult.results.Get(VoteYes)\n\tif result == nil {\n\t\treturn false\n\t}\n\n\tif result.(int) == len(c.members) {\n\t\tcategory.Approve()\n\t\treturn true\n\t}\n\n\treturn false\n}\n\n// TODO error handling\nfunc (c *Committee) AddContribution(pr *PullRequest, contributor address) (contributionId int, ok bool) {\n\tif !c.isMember(unsafe.OriginCaller()) {\n\t\treturn -1, false\n\t}\n\t// Check the category of the PR matches a category this committee evaluates\n\t// TODO check the category is an approved category\n\tif c.categories.Has(pr.category) {\n\t\treturn c.evaluation.AddContribution(pr, contributor)\n\t}\n\n\treturn -1, false\n}\n\n// TODO error handling\nfunc (c *Committee) ApproveContribution(id int, option string) bool {\n\tif !c.isMember(unsafe.OriginCaller()) {\n\t\treturn false\n\t}\n\n\tvalue := c.evaluation.contributions.Get(ufmt.Sprintf(\"%d\", id))\n\tif value == nil {\n\t\treturn false\n\t}\n\tcontribution := value.(*Contribution)\n\t// Already approved\n\tif contribution.status == ApprovedStatus {\n\t\treturn false\n\t}\n\n\tvote := NewVote(unsafe.OriginCaller(), option)\n\tcontribution.votes = append(contribution.votes, vote)\n\tcontribution.Tally()\n\n\t// TODO Add threshold factor for a contribution approval\n\t// TODO Add quorum factor for a contribution approval\n\t// Current assumption is all members voted YES so contribution is approved\n\n\tresult := contribution.tallyResult.results.Get(VoteYes)\n\tif result == nil {\n\t\treturn false\n\t}\n\n\tif result.(int) == len(c.members) {\n\t\tcontribution.Approve()\n\t\treturn true\n\t}\n\n\treturn false\n}\n\nfunc (c *Committee) isMember(m address) bool {\n\tfor _, member := range c.members {\n\t\tif m == member {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"},{"name":"committee_test.gno","body":"package evaluation\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nfunc TestCommitteeMembers(t *testing.T) {\n\tmembers := []address{testutils.TestAddress(\"member1\"), testutils.TestAddress(\"member2\"), testutils.TestAddress(\"member3\")}\n\tc := NewCommittee()\n\n\tt.Run(\"Designate Committee Members\", func(t *testing.T) {\n\t\tc.DesignateMembers(members)\n\t\tif !isEqualAddressSlice(c.members, members) {\n\t\t\tt.Errorf(\"Designated Committee members got %v expcted %v\", members, c.members)\n\t\t}\n\t})\n\n\tt.Run(\"Dismiss Committee Members\", func(t *testing.T) {\n\t\tc.DismissMembers(members)\n\t})\n}\n\nfunc TestCategoryEvaluationCriteria(t *testing.T) {\n\tmember := testutils.TestAddress(\"member\")\n\tcategory := \"document\"\n\tcriteria := []string{\"clarity\", \"usage\"}\n\tcategory2 := \"bounty\"\n\tcriteria2 := []string{\"complexity\"}\n\texpectedGategory := NewCategory(category, criteria)\n\texpectedGategory2 := NewCategory(category2, criteria2)\n\n\tc := NewCommittee()\n\tc.DesignateMembers([]address{member})\n\n\tt.Run(\"Add First Committee Category and Evaluation Criteria\", func(t *testing.T) {\n\t\ttesting.SetOriginCaller(member)\n\t\tc.AddCategory(category, criteria)\n\t\tvalue := c.categories.Get(category)\n\t\tif value == nil {\n\t\t\tt.Errorf(\"Add first category %s failed\", category)\n\t\t}\n\t\tgotCategory := value.(*Category)\n\t\tif gotCategory.name != expectedGategory.name {\n\t\t\tt.Errorf(\"First Committee category got %s expected %s\", gotCategory.name, expectedGategory.name)\n\t\t}\n\t})\n\n\tt.Run(\"Add Second Committee Category and Evaluation Criteria\", func(t *testing.T) {\n\t\ttesting.SetOriginCaller(member)\n\t\tc.AddCategory(category2, criteria2)\n\t\tvalue2 := c.categories.Get(category2)\n\t\tif value2 == nil {\n\t\t\tt.Errorf(\"Add second category %s failed\", category2)\n\t\t}\n\t\tgotCategory2 := value2.(*Category)\n\t\tif gotCategory2.name != expectedGategory2.name {\n\t\t\tt.Errorf(\"Second Committee category got %s expected %s\", gotCategory2.name, expectedGategory2.name)\n\t\t}\n\t})\n\n\tt.Run(\"Approve First Committee Category\", func(t *testing.T) {\n\t\ttesting.SetOriginCaller(member)\n\t\tapproved := c.ApproveCategory(category, VoteYes)\n\t\tif !approved {\n\t\t\tvalue := c.categories.Get(category)\n\t\t\tgotCategory := value.(*Category)\n\t\t\tt.Errorf(\"Approved First Committee category got %s expected %s\", gotCategory.status, \"Approved\")\n\t\t}\n\t})\n}\n\nfunc isEqualStringSlice(a, b []string) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc isEqualAddressSlice(a, b []address) bool {\n\tif len(a) != len(b) {\n\t\treturn false\n\t}\n\tfor i := range a {\n\t\tif a[i] != b[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"},{"name":"contribution.gno","body":"package evaluation\n\nimport (\n\t\"time\"\n)\n\nvar contributionStatus = map[string]string{}\n\ntype Contribution struct {\n\tid               int\n\tcontributor      address\n\tstatus           string // approved, proposed, negotiation, discussion, evaluation, etc.\n\tvotes            []Vote\n\ttallyResult      TallyResult\n\tsubmitTime       time.Time\n\tlastEvaluateTime time.Time\n\tapproveTime      time.Time\n}\n\nfunc init() {\n\tcontributionStatus = make(map[string]string)\n\tcontributionStatus[\"Proposed\"] = \"Proposed\"\n\tcontributionStatus[\"Approved\"] = \"Approved\"\n\tcontributionStatus[\"Evaluated\"] = \"Evaluated\"\n\tcontributionStatus[\"Negotiated\"] = \"Negotiated\"\n}\n\nfunc NewContribution(id int, contributor address) *Contribution {\n\tc := \u0026Contribution{\n\t\tid:          id,\n\t\tcontributor: contributor,\n\t\tstatus:      contributionStatus[\"Proposed\"],\n\t\tvotes:       []Vote{},\n\t\ttallyResult: TallyResult{},\n\t}\n\treturn c\n}\n\nfunc (c Contribution) Id() int {\n\treturn c.id\n}\n\nfunc (c Contribution) Status() string {\n\treturn c.status\n}\n\nfunc (c *Contribution) UpdateStatus(status string) bool {\n\tif c.status == contributionStatus[\"Approved\"] {\n\t\treturn false\n\t}\n\tc.status = status\n\treturn true\n}\n\nfunc (c *Contribution) Approve() {\n\t// TODO error handling\n\tc.status = \"Approved\"\n}\n\nfunc (c *Contribution) Tally() {\n\t// TODO error handling\n\tfor _, v := range c.votes {\n\t\tif c.tallyResult.results.Has(v.option) {\n\t\t\tvalue := c.tallyResult.results.Get(v.option)\n\t\t\tcount := value.(int)\n\t\t\tc.tallyResult.results.Set(v.option, count+1)\n\t\t}\n\t}\n}\n"},{"name":"contribution_test.gno","body":"package evaluation\n\nimport \"testing\"\n\nfunc TestContributionUpdateStatus(t *testing.T) {\n\tc := NewContribution(1, \"contributor\")\n\n\tt.Run(\"Status Update Negotiated\", func(t *testing.T) {\n\t\tok := c.UpdateStatus(\"Negotiated\")\n\t\tif !ok {\n\t\t\tt.Error(\"Expected Successful Status Update but failed\")\n\t\t}\n\t})\n\n\tt.Run(\"Status Update Evaluated\", func(t *testing.T) {\n\t\tok := c.UpdateStatus(\"Evaluated\")\n\t\tif !ok {\n\t\t\tt.Error(\"Expected Successful Status Update but failed\")\n\t\t}\n\t})\n\n\tt.Run(\"Status Update Approved\", func(t *testing.T) {\n\t\tok := c.UpdateStatus(\"Approved\")\n\t\tif !ok {\n\t\t\tt.Error(\"Expected Successful Status Update but failed\")\n\t\t}\n\t})\n\n\tt.Run(\"Status Update Approved Invalid\", func(t *testing.T) {\n\t\tok := c.UpdateStatus(\"Approved\")\n\t\tif ok {\n\t\t\tt.Error(\"Expected Failed Status Update but succeded\")\n\t\t}\n\t})\n}\n"},{"name":"evaluation.gno","body":"package evaluation\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Evaluation struct {\n\tcontributions avl.Tree\n\tpullrequests  avl.Tree\n}\n\ntype Evaluator interface {\n\tEvaluate() Points\n}\n\nfunc NewEvalutaion() *Evaluation {\n\te := \u0026Evaluation{\n\t\tcontributions: avl.Tree{},\n\t\tpullrequests:  avl.Tree{},\n\t}\n\treturn e\n}\n\nfunc (e *Evaluation) AddContribution(pr *PullRequest, contributor address) (int, bool) {\n\tid := pr.Id()\n\te.pullrequests.Set(ufmt.Sprintf(\"%d\", id), pr)\n\tc := NewContribution(id, contributor)\n\te.contributions.Set(ufmt.Sprintf(\"%d\", id), c)\n\treturn id, true\n}\n\nfunc (e *Evaluation) UpdateContribution(id int, status string) bool {\n\tc := e.contributions.Get(ufmt.Sprintf(\"%d\", id))\n\tif c != nil {\n\t\tcontribtution := c.(*Contribution)\n\t\treturn contribtution.UpdateStatus(status)\n\t}\n\treturn false\n}\n"},{"name":"evaluation_test.gno","body":"package evaluation\n\n/*\n\t1. At what stage of the PR a contribution should be evaluated?\n\t\tShould the PR be approved first?\n\t2. Can a contribution be re-evaluated before approved (current assumption is once a contribution is approved its state is final)?\n\t3. Can an evaluation criteria change up until it is approved (current assumption is that the evaluation criteria is set when the contribution is added)?\n*/\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\te = NewEvalutaion()\n\n\tid          = 792\n\tname        = \"Evaluation DAO Kick Off\"\n\tdescription = \"The PR is to initiate a discussion regarding the evaluation DAO\"\n\tstatus      = \"Draft\"\n\tcategory    = \"feat\"\n\tcriteria    = map[string]int32{\"simplicity\": 1, \"usefullnes\": 1, \"quality\": 1}\n\taddress_XXX = testutils.TestAddress(\"contributor\")\n)\n\nfunc TestEvaluationAddContribution(t *testing.T) {\n\tpr := NewPullRequest(id, name, description, status, category)\n\tcontributionId, _ := e.AddContribution(pr, address_XXX)\n\n\tt.Run(\"\", func(t *testing.T) {\n\t\tif contributionId != id {\n\t\t\tt.Errorf(\"Got Contribution Id %d expected %d\", contributionId, id)\n\t\t}\n\t})\n\n\tt.Run(\"Contribution added using the pull request id\", func(t *testing.T) {\n\t\tc := e.contributions.Get(ufmt.Sprintf(\"%d\", id))\n\t\tcontribtution := c.(*Contribution)\n\t\tif contribtution.Id() != id {\n\t\t\tt.Errorf(\"Got Contribution Id %d expected %d\", contribtution.Id(), id)\n\t\t}\n\t})\n\n\tt.Run(\"Pull Request added using the pull request id\", func(t *testing.T) {\n\t\tpr := e.pullrequests.Get(ufmt.Sprintf(\"%d\", id))\n\t\tpullrequest := pr.(*PullRequest)\n\t\tif pullrequest.Id() != id {\n\t\t\tt.Errorf(\"Got Pull Request Id %d expected %d\", pullrequest.Id(), id)\n\t\t}\n\t})\n}\n\nfunc TestEvaluationUpdateContribution(t *testing.T) {\n\tt.Run(\"\", func(t *testing.T) {\n\t\tstatus := \"Negotiated\"\n\t\tok := e.UpdateContribution(id, status)\n\t\tif !ok {\n\t\t\tt.Error(\"Expected evaluation to update contribution's status successfully but failed\")\n\t\t}\n\t})\n\n\tt.Run(\"Contribution doesn't exist\", func(t *testing.T) {\n\t\tid := 1\n\t\tstatus := \"Negotiated\"\n\t\tok := e.UpdateContribution(id, status)\n\t\tif ok {\n\t\t\tt.Error(\"Expected evaluation to fail but pass\")\n\t\t}\n\t})\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/nir1218_evaluation_proposal/evaluation\"\ngno = \"0.9\"\n"},{"name":"points.gno","body":"package evaluation\n\n// Points could be converted to rewards\ntype Points struct {\n\ttotal   int64\n\tfactors map[string]int32\n}\n\nfunc NewPoints(t int64, f map[string]int32) Points {\n\treturn Points{\n\t\ttotal:   t,\n\t\tfactors: f,\n\t}\n}\n"},{"name":"pull_request.gno","body":"package evaluation\n\nvar pullRequestStatus map[string]struct{}\n\ntype PullRequest struct {\n\tid          int\n\tname        string\n\tdescription string\n\tstatus      string // Draft, Review required, Changes requested, Approved\n\tcategory    string // bounty, chore, defect, document etc.\n}\n\nfunc init() {\n\tpullRequestStatus = make(map[string]struct{})\n\tpullRequestStatus[\"Draft\"] = struct{}{}\n\tpullRequestStatus[\"Approved\"] = struct{}{}\n\tpullRequestStatus[\"Changes requested\"] = struct{}{}\n\tpullRequestStatus[\"Review required\"] = struct{}{}\n}\n\nfunc NewPullRequest(id int, name string, description string, status string, category string) *PullRequest {\n\tpr := \u0026PullRequest{\n\t\tid:          id,\n\t\tname:        name,\n\t\tdescription: description,\n\t\tstatus:      status,\n\t\tcategory:    category,\n\t}\n\treturn pr\n}\n\nfunc (pr PullRequest) Id() int {\n\treturn pr.id\n}\n\nfunc (pr PullRequest) Status() string {\n\treturn pr.status\n}\n\nfunc (pr *PullRequest) UpdateName(name string) {\n\tpr.name = name\n}\n\nfunc (pr *PullRequest) UpdateDescription(description string) {\n\tpr.description = description\n}\n\nfunc (pr *PullRequest) UpdateStatus(status string) bool {\n\tif validateStatus(status) {\n\t\tpr.status = status\n\t\treturn true\n\t}\n\treturn false\n}\n\nfunc validateStatus(status string) bool {\n\t_, ok := pullRequestStatus[status]\n\treturn ok\n}\n"},{"name":"pull_request_test.gno","body":"package evaluation\n\nimport \"testing\"\n\nfunc TestPullRequestUpdateStatus(t *testing.T) {\n\tvar (\n\t\tid          = 792\n\t\tname        = \"Evaluation DAO Kick Off\"\n\t\tdescription = \"The PR is to initiate a discussion regarding the evaluation DAO\"\n\t\tstatus      = \"Draft\"\n\t\tcategory    = \"feat\"\n\t)\n\n\tvalidPR := NewPullRequest(id, name, description, status, category)\n\n\tt.Run(\"Valid Status Approved\", func(t *testing.T) {\n\t\tstatus := \"Approved\"\n\t\tif !validPR.UpdateStatus(status) {\n\t\t\tt.Error(\"expected validation to pass\")\n\t\t}\n\t})\n\n\tt.Run(\"Valid Status Draft\", func(t *testing.T) {\n\t\tstatus := \"Draft\"\n\t\tif !validPR.UpdateStatus(status) {\n\t\t\tt.Error(\"expected validation to pass\")\n\t\t}\n\t})\n\n\tt.Run(\"Valid Status Changes requested\", func(t *testing.T) {\n\t\tstatus := \"Changes requested\"\n\t\tif !validPR.UpdateStatus(status) {\n\t\t\tt.Error(\"expected validation to pass\")\n\t\t}\n\t})\n\n\tt.Run(\"Valid Status Review required\", func(t *testing.T) {\n\t\tstatus := \"Review required\"\n\t\tif !validPR.UpdateStatus(status) {\n\t\t\tt.Error(\"expected validation to pass\")\n\t\t}\n\t})\n\n\tt.Run(\"Invalid Status\", func(t *testing.T) {\n\t\tstatus := \"Junk\"\n\t\tif validPR.UpdateStatus(status) {\n\t\t\tt.Error(\"expected validation to fail\")\n\t\t}\n\t})\n}\n"},{"name":"tally.gno","body":"package evaluation\n\nimport \"gno.land/p/nt/avl/v0\"\n\ntype TallyResult struct {\n\tresults avl.Tree\n}\n\ntype Tally interface {\n\tTally()\n}\n"},{"name":"task.gno","body":"package evaluation\n\n// Maybe a task in the project management system\ntype Task struct {\n\tid          int\n\tname        string\n\tdescription string\n\tstatus      string\n}\n"},{"name":"vote.gno","body":"package evaluation\n\nconst (\n\tVoteYes = \"YES\"\n\tVoteNo  = \"NO\"\n)\n\ntype Vote struct {\n\tvoter  address\n\toption string\n}\n\nfunc NewVote(voter address, option string) Vote {\n\tv := Vote{\n\t\tvoter:  voter,\n\t\toption: option,\n\t}\n\treturn v\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"yp0n1JsEZE0yOamyPA1kOY7PZKGY0bjftQyT8JmjZMq1psBdt/Nx8P9s3HRtzMpMZeMX2OQximqfgNF1K3oLBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ui","path":"gno.land/r/archive/ui","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/ui\"\ngno = \"0.9\"\n"},{"name":"ui.gno","body":"package ui\n\nimport \"gno.land/p/archive/ui\"\n\nfunc Render(path string) string {\n\t// TODO: build this realm as a demo one with one page per feature.\n\n\t// TODO: pagination\n\t// TODO: non-standard markdown\n\t// TODO: error, warn\n\t// TODO: header\n\t// TODO: HTML\n\t// TODO: toc\n\t// TODO: forms\n\t// TODO: comments\n\n\tdom := ui.DOM{\n\t\tPrefix: \"r/demo/ui:\",\n\t}\n\n\tdom.Title = \"UI Demo\"\n\n\tdom.Header.Append(ui.Breadcrumb{\n\t\tui.Link{Text: \"foo\", Path: \"foo\"},\n\t\tui.Link{Text: \"bar\", Path: \"foo/bar\"},\n\t})\n\n\tdom.Body.Append(\n\t\tui.Paragraph(\"Simple UI demonstration.\"),\n\t\tui.BulletList{\n\t\t\tui.Text(\"a text\"),\n\t\t\tui.Link{Text: \"a relative link\", Path: \"foobar\"},\n\t\t\tui.Text(\"another text\"),\n\t\t\t// ui.H1(\"a H1 text\"),\n\t\t\tui.Bold(\"a bold text\"),\n\t\t\tui.Italic(\"italic text\"),\n\t\t\tui.Text(\"raw markdown with **bold** text in the middle.\"),\n\t\t\tui.Code(\"some inline code\"),\n\t\t\tui.Link{Text: \"a remote link\", URL: \"https://gno.land\"},\n\t\t},\n\t)\n\n\tdom.Footer.Append(ui.Text(\"I'm the footer.\"))\n\tdom.Body.Append(ui.Text(\"another string.\"))\n\tdom.Body.Append(ui.Paragraph(\"a paragraph.\"), ui.HR{})\n\n\treturn dom.String()\n}\n"},{"name":"ui_test.gno","body":"package ui\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestRender(t *testing.T) {\n\tgot := Render(\"\")\n\texpected := \"# UI Demo\\n\\n[foo](r/demo/ui:foo) / [bar](r/demo/ui:foo/bar)\\n\\n\\nSimple UI demonstration.\\n\\n- a text\\n- [a relative link](r/demo/ui:foobar)\\n- another text\\n- **a bold text**\\n- _italic text_\\n- raw markdown with **bold** text in the middle.\\n- `some inline code`\\n- [a remote link](https://gno.land)\\n\\nanother string.\\n\\na paragraph.\\n\\n\\n---\\n\\n\\nI'm the footer.\\n\\n\"\n\tuassert.Equal(t, expected, got)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"KslO4PHFki8k++Qcron/IGieClUB4T8Km9kzy3ukZBGvN1VxuVGNIB9Tl9YyrpIQNcyhELr7aeJEgjDb7uwABw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"userbook","path":"gno.land/r/archive/userbook","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/archive/userbook\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"// Package userbook demonstrates a small userbook system working with gnoweb\npackage userbook\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/r/sys/users\"\n\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst usersLink = \"/r/sys/users\"\n\nfunc Render(path string) string {\n\tp := pager.NewPager(signupsTree, 20, true)\n\tpage := p.MustGetPageByPath(path)\n\n\tout := \"# Welcome to UserBook!\\n\\n\"\n\n\tout += ufmt.Sprintf(\"## [Click here to sign up!](%s)\\n\\n\", txlink.Call(\"SignUp\"))\n\tout += \"---\\n\\n\"\n\n\tfor _, item := range page.Items {\n\t\tsignup := item.Value.(*Signup)\n\t\tuser := signup.address_XXX.String()\n\n\t\tif data := users.ResolveAddress(signup.address_XXX); data != nil {\n\t\t\tuser = ufmt.Sprintf(\"[%s](%s:%s)\", data.Name(), usersLink, data.Name())\n\t\t}\n\n\t\tout += ufmt.Sprintf(\"- **User #%d - %s - signed up on %s**\\n\\n\", signup.ordinal, user, signup.timestamp.Format(\"January 2 2006, 03:04:04 PM\"))\n\t}\n\n\tout += \"---\\n\\n\"\n\tout += \"**Page \" + strconv.Itoa(page.PageNumber) + \" of \" + strconv.Itoa(page.TotalPages) + \"**\\n\\n\"\n\tout += page.Picker(path)\n\treturn out\n}\n"},{"name":"userbook.gno","body":"// Package userbook demonstrates a small userbook system working with gnoweb\npackage userbook\n\nimport (\n\t\"chain\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Signup struct {\n\taddress_XXX address\n\tordinal     int\n\ttimestamp   time.Time\n}\n\nvar (\n\tsignupsTree = avl.NewTree()\n\ttracker     = avl.NewTree()\n\tidCounter   seqid.ID\n)\n\nconst signUpEvent = \"SignUp\"\n\nfunc init(cur realm) {\n\tSignUp(cross(cur)) // Sign up the deployer\n}\n\nfunc SignUp(cur realm) string {\n\t// Get transaction caller\n\tcaller := cur.Previous().Address()\n\n\t// Check if the user is already signed up\n\tif tracker.Has(caller.String()) {\n\t\tpanic(caller.String() + \" is already signed up!\")\n\t}\n\n\tnow := time.Now()\n\n\t// Sign up the user\n\tsignupsTree.Set(idCounter.Next().String(), \u0026Signup{\n\t\tcaller,\n\t\tsignupsTree.Size(),\n\t\tnow,\n\t})\n\n\ttracker.Set(caller.String(), struct{}{})\n\n\tchain.Emit(signUpEvent, \"account\", caller.String())\n\n\treturn ufmt.Sprintf(\"%s added to userbook! Timestamp: %s\", caller.String(), now.Format(time.RFC822Z))\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"pYV/UzosL71WiXw5XevoTQF5wlpRCvh6g68HgKmY+K8wi96sV+i98YRnxz2DEUsEKOecbFg74QmTRaAQit9CCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"gnoface","path":"gno.land/r/demo/art/gnoface","files":[{"name":"gnoface.gno","body":"package gnoface\n\nimport (\n\t\"math/rand\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc Render(path string) string {\n\tseed := uint64(entropy.New().Value())\n\n\tpath = strings.TrimSpace(path)\n\tif path != \"\" {\n\t\ts, err := strconv.Atoi(path)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tseed = uint64(s)\n\t}\n\n\toutput := ufmt.Sprintf(\"Gnoface #%d\\n\", seed)\n\toutput += \"```\\n\" + Draw(seed) + \"```\\n\"\n\treturn output\n}\n\nfunc Draw(seed uint64) string {\n\tvar (\n\t\thairs = []string{\n\t\t\t\"     s\",\n\t\t\t\"  .......\",\n\t\t\t\"   s s s\",\n\t\t\t\"   /\\\\ /\\\\\",\n\t\t\t\"  |||||||\",\n\t\t}\n\t\theadtop = []string{\n\t\t\t\" /-------\\\\\",\n\t\t\t\" /~~~~~~~\\\\\",\n\t\t\t\" /|||||||\\\\\",\n\t\t\t\" ////////\\\\\",\n\t\t\t\" |||||||||\",\n\t\t\t\" /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\n\t\t}\n\t\theadspace = []string{\n\t\t\t\" |       |\",\n\t\t}\n\t\teyebrow = []string{\n\t\t\t\"~\",\n\t\t\t\"*\",\n\t\t\t\"_\",\n\t\t\t\".\",\n\t\t}\n\t\tear = []string{\n\t\t\t\"o\",\n\t\t\t\" \",\n\t\t\t\"D\",\n\t\t\t\"O\",\n\t\t\t\"\u003c\",\n\t\t\t\"\u003e\",\n\t\t\t\".\",\n\t\t\t\"|\",\n\t\t\t\")\",\n\t\t\t\"(\",\n\t\t}\n\t\teyesmiddle = []string{\n\t\t\t\"| o   o |\",\n\t\t\t\"| o   _ |\",\n\t\t\t\"| _   o |\",\n\t\t\t\"| .   . |\",\n\t\t\t\"| O   O |\",\n\t\t\t\"| v   v |\",\n\t\t\t\"| X   X |\",\n\t\t\t\"| x   X |\",\n\t\t\t\"| X   D |\",\n\t\t\t\"| ~   ~ |\",\n\t\t}\n\t\tnose = []string{\n\t\t\t\" |   o   |\",\n\t\t\t\" |   O   |\",\n\t\t\t\" |   V   |\",\n\t\t\t\" |   L   |\",\n\t\t\t\" |   C   |\",\n\t\t\t\" |   ~   |\",\n\t\t\t\" |  . .  |\",\n\t\t\t\" |   .   |\",\n\t\t}\n\t\tmouth = []string{\n\t\t\t\" |  __/  |\",\n\t\t\t\" |  \\\\_/  |\",\n\t\t\t\" |   .   |\",\n\t\t\t\" |  ___  |\",\n\t\t\t\" |  ~~~  |\",\n\t\t\t\" |  ===  |\",\n\t\t\t\" |  \u003c=\u003e  |\",\n\t\t}\n\t\theadbottom = []string{\n\t\t\t\" \\\\-------/\",\n\t\t\t\" \\\\~~~~~~~/\",\n\t\t\t\" \\\\_______/\",\n\t\t}\n\t)\n\n\tr := rand.New(rand.NewPCG(seed, 0xdeadbeef))\n\n\treturn pick(r, hairs) + \"\\n\" +\n\t\tpick(r, headtop) + \"\\n\" +\n\t\tpick(r, headspace) + \"\\n\" +\n\t\t\" | \" + pick(r, eyebrow) + \"   \" + pick(r, eyebrow) + \" |\\n\" +\n\t\tpick(r, ear) + pick(r, eyesmiddle) + pick(r, ear) + \"\\n\" +\n\t\tpick(r, headspace) + \"\\n\" +\n\t\tpick(r, nose) + \"\\n\" +\n\t\tpick(r, headspace) + \"\\n\" +\n\t\tpick(r, mouth) + \"\\n\" +\n\t\tpick(r, headspace) + \"\\n\" +\n\t\tpick(r, headbottom) + \"\\n\"\n}\n\nfunc pick(r *rand.Rand, slice []string) string {\n\treturn slice[r.IntN(len(slice))]\n}\n\n// based on https://github.com/moul/pipotron/blob/master/dict/ascii-face.yml\n"},{"name":"gnoface_test.gno","body":"package gnoface\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc TestDraw(t *testing.T) {\n\tcases := []struct {\n\t\tseed     uint64\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tseed: 42,\n\t\t\texpected: `\n  |||||||\n |||||||||\n |       |\n | .   ~ |\n)| v   v |O\n |       |\n |   L   |\n |       |\n |  ___  |\n |       |\n \\~~~~~~~/\n`[1:],\n\t\t},\n\t\t{\n\t\t\tseed: 1337,\n\t\t\texpected: `\n  .......\n |||||||||\n |       |\n | .   _ |\nD| x   X |O\n |       |\n |   ~   |\n |       |\n |  ~~~  |\n |       |\n \\~~~~~~~/\n`[1:],\n\t\t},\n\t\t{\n\t\t\tseed: 123456789,\n\t\t\texpected: `\n  .......\n ////////\\\n |       |\n | ~   * |\n|| x   X |o\n |       |\n |   V   |\n |       |\n |   .   |\n |       |\n \\-------/\n`[1:],\n\t\t},\n\t}\n\tfor _, tc := range cases {\n\t\tname := ufmt.Sprintf(\"%d\", tc.seed)\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\tgot := Draw(tc.seed)\n\t\t\tuassert.Equal(t, string(tc.expected), got)\n\t\t})\n\t}\n}\n\nfunc TestRender(t *testing.T) {\n\tcases := []struct {\n\t\tpath     string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tpath: \"42\",\n\t\t\texpected: \"Gnoface #42\\n```\" + `\n  |||||||\n |||||||||\n |       |\n | .   ~ |\n)| v   v |O\n |       |\n |   L   |\n |       |\n |  ___  |\n |       |\n \\~~~~~~~/\n` + \"```\\n\",\n\t\t},\n\t\t{\n\t\t\tpath: \"1337\",\n\t\t\texpected: \"Gnoface #1337\\n```\" + `\n  .......\n |||||||||\n |       |\n | .   _ |\nD| x   X |O\n |       |\n |   ~   |\n |       |\n |  ~~~  |\n |       |\n \\~~~~~~~/\n` + \"```\\n\",\n\t\t},\n\t\t{\n\t\t\tpath: \"123456789\",\n\t\t\texpected: \"Gnoface #123456789\\n```\" + `\n  .......\n ////////\\\n |       |\n | ~   * |\n|| x   X |o\n |       |\n |   V   |\n |       |\n |   .   |\n |       |\n \\-------/\n` + \"```\\n\",\n\t\t},\n\t}\n\tfor _, tc := range cases {\n\t\tt.Run(tc.path, func(t *testing.T) {\n\t\t\tgot := Render(tc.path)\n\t\t\tuassert.Equal(t, tc.expected, got)\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/art/gnoface\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"5IuROYWrNC7tTxkkkILXE46e+vRb07ttyGQfG3eRIZaQRN5iw3UafuwTUsnqIOraYP7dlODFhub2OoXfrYHFAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"millipede","path":"gno.land/r/demo/art/millipede","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/art/millipede\"\ngno = \"0.9\"\n"},{"name":"millipede.gno","body":"package millipede\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst (\n\tminSize     = 1\n\tdefaultSize = 20\n\tmaxSize     = 100\n)\n\nfunc Draw(size int) string {\n\tif size \u003c minSize || size \u003e maxSize {\n\t\tpanic(\"invalid millipede size\")\n\t}\n\tpaddings := []string{\"  \", \" \", \"\", \" \", \"  \", \"   \", \"    \", \"    \", \"   \"}\n\tvar b strings.Builder\n\tb.WriteString(\"    ╚⊙ ⊙╝\\n\")\n\tfor i := 0; i \u003c size; i++ {\n\t\tb.WriteString(paddings[i%9] + \"╚═(███)═╝\\n\")\n\t}\n\treturn b.String()\n}\n\nfunc Render(path string) string {\n\tsize := defaultSize\n\n\tpath = strings.TrimSpace(path)\n\tif path != \"\" {\n\t\tvar err error\n\t\tsize, err = strconv.Atoi(path)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\n\toutput := \"```\\n\" + Draw(size) + \"```\\n\"\n\tif size \u003e minSize {\n\t\toutput += ufmt.Sprintf(\"[%d](/r/demo/art/millipede:%d)\u003c \", size-1, size-1)\n\t}\n\tif size \u003c maxSize {\n\t\toutput += ufmt.Sprintf(\" \u003e[%d](/r/demo/art/millipede:%d)\", size+1, size+1)\n\t}\n\treturn output\n}\n\n// based on https://github.com/getmillipede/millipede-go/blob/977f046c39c35a650eac0fd30245e96b22c7803c/main.go\n"},{"name":"millipede_test.gno","body":"package millipede\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestRender(t *testing.T) {\n\tcases := []struct {\n\t\tpath     string\n\t\texpected string\n\t}{\n\t\t{\n\t\t\tpath: \"\",\n\t\t\texpected: \"```\" + `\n    ╚⊙ ⊙╝\n  ╚═(███)═╝\n ╚═(███)═╝\n╚═(███)═╝\n ╚═(███)═╝\n  ╚═(███)═╝\n   ╚═(███)═╝\n    ╚═(███)═╝\n    ╚═(███)═╝\n   ╚═(███)═╝\n  ╚═(███)═╝\n ╚═(███)═╝\n╚═(███)═╝\n ╚═(███)═╝\n  ╚═(███)═╝\n   ╚═(███)═╝\n    ╚═(███)═╝\n    ╚═(███)═╝\n   ╚═(███)═╝\n  ╚═(███)═╝\n ╚═(███)═╝\n` + \"```\\n[19](/r/demo/art/millipede:19)\u003c  \u003e[21](/r/demo/art/millipede:21)\",\n\t\t},\n\t\t{\n\t\t\tpath: \"4\",\n\t\t\texpected: \"```\" + `\n    ╚⊙ ⊙╝\n  ╚═(███)═╝\n ╚═(███)═╝\n╚═(███)═╝\n ╚═(███)═╝\n` + \"```\\n[3](/r/demo/art/millipede:3)\u003c  \u003e[5](/r/demo/art/millipede:5)\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.path, func(t *testing.T) {\n\t\t\tgot := Render(tc.path)\n\t\t\tuassert.Equal(t, tc.expected, got)\n\t\t})\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"7KxfsCNJ0FSozlZ+7t+/9KP6c1wcTcF17TCBn0PQ6AE9T5XFNQg1fu6yiKsZUjFBiCxnKcozLDDF0f/Jn5MCCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"btree_dao","path":"gno.land/r/demo/btree_dao","files":[{"name":"btree_dao.gno","body":"package btree_dao\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/demo/btree\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// RegistrationDetails holds the details of a user's registration in the BTree DAO.\n// It stores the user's address, registration time, their B-Tree if they planted one,\n// and their NFT ID.\ntype RegistrationDetails struct {\n\tAddress   address\n\tRegTime   time.Time\n\tUserBTree *btree.BTree\n\tNFTID     string\n}\n\n// Less implements the btree.Record interface for RegistrationDetails.\n// It compares two RegistrationDetails based on their registration time.\n// Returns true if the current registration time is before the other registration time.\nfunc (rd *RegistrationDetails) Less(than btree.Record) bool {\n\tother := than.(*RegistrationDetails)\n\treturn rd.RegTime.Before(other.RegTime)\n}\n\nvar (\n\tdao     *grc721.BasicNFT\n\ttokenID = 0\n\tmembers = btree.New()\n)\n\nfunc init(cur realm) {\n\tdao = grc721.NewBasicNFT(0, cur, \"BTree DAO\", \"BTDAO\")\n}\n\n// PlantTree allows a user to plant their B-Tree in the DAO forest.\n// It mints an NFT to the user and registers their tree in the DAO.\n// Returns an error if the tree is already planted, empty, or if NFT minting fails.\nfunc PlantTree(_ realm, userBTree *btree.BTree) error {\n\treturn plantImpl(userBTree, \"\")\n}\n\n// PlantSeed allows a user to register as a seed in the DAO with a message.\n// It mints an NFT to the user and registers them as a seed member.\n// Returns an error if the message is empty or if NFT minting fails.\nfunc PlantSeed(_ realm, message string) error {\n\treturn plantImpl(nil, message)\n}\n\n// plantImpl is the internal implementation that handles both tree planting and seed registration.\n// For tree planting (userBTree != nil), it verifies the tree isn't already planted and isn't empty.\n// For seed planting (userBTree == nil), it verifies the seed message isn't empty.\n// In both cases, it mints an NFT to the user and adds their registration details to the members tree.\n// Returns an error if any validation fails or if NFT minting fails.\nfunc plantImpl(userBTree *btree.BTree, seedMessage string) error {\n\t// Get the caller's address\n\tuserAddress := unsafe.OriginCaller()\n\n\tvar nftID string\n\tvar regDetails *RegistrationDetails\n\n\tif userBTree != nil {\n\t\t// Handle tree planting\n\t\tvar treeExists bool\n\t\tmembers.Ascend(func(record btree.Record) bool {\n\t\t\tregDetails := record.(*RegistrationDetails)\n\t\t\tif regDetails.UserBTree == userBTree {\n\t\t\t\ttreeExists = true\n\t\t\t\treturn false\n\t\t\t}\n\t\t\treturn true\n\t\t})\n\t\tif treeExists {\n\t\t\treturn errors.New(\"tree is already planted in the forest\")\n\t\t}\n\n\t\tif userBTree.Len() == 0 {\n\t\t\treturn errors.New(\"cannot plant an empty tree\")\n\t\t}\n\n\t\tnftID = ufmt.Sprintf(\"%d\", tokenID)\n\t\tregDetails = \u0026RegistrationDetails{\n\t\t\tAddress:   userAddress,\n\t\t\tRegTime:   time.Now(),\n\t\t\tUserBTree: userBTree,\n\t\t\tNFTID:     nftID,\n\t\t}\n\t} else {\n\t\t// Handle seed planting\n\t\tif seedMessage == \"\" {\n\t\t\treturn errors.New(\"seed message cannot be empty\")\n\t\t}\n\t\tnftID = \"seed_\" + ufmt.Sprintf(\"%d\", tokenID)\n\t\tregDetails = \u0026RegistrationDetails{\n\t\t\tAddress:   userAddress,\n\t\t\tRegTime:   time.Now(),\n\t\t\tUserBTree: nil,\n\t\t\tNFTID:     nftID,\n\t\t}\n\t}\n\n\t// Mint an NFT to the user\n\terr := dao.Mint(userAddress, grc721.TokenID(nftID))\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tmembers.Insert(regDetails)\n\ttokenID++\n\treturn nil\n}\n\n// Render generates a Markdown representation of the DAO members.\n// It displays:\n// - Total number of NFTs minted\n// - Total number of members\n// - Size of the biggest planted tree\n// - The first 3 members (OGs)\n// - The latest 10 members\n// Each member entry includes their address and owned NFTs (🌳 for trees, 🌱 for seeds).\n// The path parameter is currently unused.\n// Returns a formatted Markdown string.\nfunc Render(path string) string {\n\tvar latestMembers []string\n\tvar ogMembers []string\n\n\t// Get total size and first member\n\ttotalSize := members.Len()\n\tbiggestTree := 0\n\tif maxMember := members.Max(); maxMember != nil {\n\t\tif userBTree := maxMember.(*RegistrationDetails).UserBTree; userBTree != nil {\n\t\t\tbiggestTree = userBTree.Len()\n\t\t}\n\t}\n\n\t// Collect the latest 10 members\n\tmembers.Descend(func(record btree.Record) bool {\n\t\tif len(latestMembers) \u003c 10 {\n\t\t\tregDetails := record.(*RegistrationDetails)\n\t\t\taddr := regDetails.Address\n\t\t\tnftList := \"\"\n\t\t\tbalance, err := dao.BalanceOf(addr)\n\t\t\tif err == nil \u0026\u0026 balance \u003e 0 {\n\t\t\t\tnftList = \" (NFTs: \"\n\t\t\t\tfor i := int64(0); i \u003c balance; i++ {\n\t\t\t\t\tif i \u003e 0 {\n\t\t\t\t\t\tnftList += \", \"\n\t\t\t\t\t}\n\t\t\t\t\tif regDetails.UserBTree == nil {\n\t\t\t\t\t\tnftList += \"🌱#\" + regDetails.NFTID\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnftList += \"🌳#\" + regDetails.NFTID\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnftList += \")\"\n\t\t\t}\n\t\t\tlatestMembers = append(latestMembers, string(addr)+nftList)\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\n\t// Collect the first 3 members (OGs)\n\tmembers.Ascend(func(record btree.Record) bool {\n\t\tif len(ogMembers) \u003c 3 {\n\t\t\tregDetails := record.(*RegistrationDetails)\n\t\t\taddr := regDetails.Address\n\t\t\tnftList := \"\"\n\t\t\tbalance, err := dao.BalanceOf(addr)\n\t\t\tif err == nil \u0026\u0026 balance \u003e 0 {\n\t\t\t\tnftList = \" (NFTs: \"\n\t\t\t\tfor i := int64(0); i \u003c balance; i++ {\n\t\t\t\t\tif i \u003e 0 {\n\t\t\t\t\t\tnftList += \", \"\n\t\t\t\t\t}\n\t\t\t\t\tif regDetails.UserBTree == nil {\n\t\t\t\t\t\tnftList += \"🌱#\" + regDetails.NFTID\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnftList += \"🌳#\" + regDetails.NFTID\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnftList += \")\"\n\t\t\t}\n\t\t\togMembers = append(ogMembers, string(addr)+nftList)\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\n\tvar sb strings.Builder\n\n\tsb.WriteString(md.H1(\"B-Tree DAO Members\"))\n\tsb.WriteString(md.H2(\"Total NFTs Minted\"))\n\tsb.WriteString(ufmt.Sprintf(\"Total NFTs minted: %d\\n\\n\", dao.TokenCount()))\n\tsb.WriteString(md.H2(\"Member Stats\"))\n\tsb.WriteString(ufmt.Sprintf(\"Total members: %d\\n\", totalSize))\n\tif biggestTree \u003e 0 {\n\t\tsb.WriteString(ufmt.Sprintf(\"Biggest tree size: %d\\n\", biggestTree))\n\t}\n\tsb.WriteString(md.H2(\"OG Members\"))\n\tsb.WriteString(md.BulletList(ogMembers))\n\tsb.WriteString(md.H2(\"Latest Members\"))\n\tsb.WriteString(md.BulletList(latestMembers))\n\n\treturn sb.String()\n}\n"},{"name":"btree_dao_test.gno","body":"package btree_dao\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/demo/btree\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc setupTest() {\n\ttesting.SetOriginCaller(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\"))\n\tmembers = btree.New()\n}\n\ntype TestElement struct {\n\tvalue int\n}\n\nfunc (te *TestElement) Less(than btree.Record) bool {\n\treturn te.value \u003c than.(*TestElement).value\n}\n\nfunc TestPlantTree(cur realm, t *testing.T) {\n\tsetupTest()\n\n\ttree := btree.New()\n\telements := []int{30, 10, 50, 20, 40}\n\tfor _, val := range elements {\n\t\ttree.Insert(\u0026TestElement{value: val})\n\t}\n\n\terr := PlantTree(cross(cur), tree)\n\turequire.NoError(t, err)\n\n\tfound := false\n\tmembers.Ascend(func(record btree.Record) bool {\n\t\tregDetails := record.(*RegistrationDetails)\n\t\tif regDetails.UserBTree == tree {\n\t\t\tfound = true\n\t\t\treturn false\n\t\t}\n\t\treturn true\n\t})\n\tuassert.True(t, found)\n\n\terr = PlantTree(cross(cur), tree)\n\tuassert.Error(t, err)\n\n\temptyTree := btree.New()\n\terr = PlantTree(cross(cur), emptyTree)\n\tuassert.Error(t, err)\n}\n\nfunc TestPlantSeed(cur realm, t *testing.T) {\n\tsetupTest()\n\n\terr := PlantSeed(cross(cur), \"Hello DAO!\")\n\turequire.NoError(t, err)\n\n\tfound := false\n\tmembers.Ascend(func(record btree.Record) bool {\n\t\tregDetails := record.(*RegistrationDetails)\n\t\tif regDetails.UserBTree == nil {\n\t\t\tfound = true\n\t\t\tuassert.NotEmpty(t, regDetails.NFTID)\n\t\t\tuassert.True(t, strings.Contains(regDetails.NFTID, \"seed_\"))\n\t\t\treturn false\n\t\t}\n\t\treturn true\n\t})\n\tuassert.True(t, found)\n\n\terr = PlantSeed(cross(cur), \"\")\n\tuassert.Error(t, err)\n}\n\nfunc TestRegistrationDetailsOrdering(t *testing.T) {\n\tsetupTest()\n\n\trd1 := \u0026RegistrationDetails{\n\t\tAddress: address(\"test1\"),\n\t\tRegTime: time.Now(),\n\t\tNFTID:   \"0\",\n\t}\n\trd2 := \u0026RegistrationDetails{\n\t\tAddress: address(\"test2\"),\n\t\tRegTime: time.Now().Add(time.Hour),\n\t\tNFTID:   \"1\",\n\t}\n\n\tuassert.True(t, rd1.Less(rd2))\n\tuassert.False(t, rd2.Less(rd1))\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/btree_dao\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"zPC8na6VwbJtmteRARoeHyRFzopImEYGVapDY9824VkeBI8KkxfDO2eQjDTODf9XuCe563zw1ON+kM9PhAmADg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"bar20","path":"gno.land/r/demo/defi/bar20","files":[{"name":"bar20.gno","body":"// Package bar20 is similar to gno.land/r/demo/foo20 but exposes a safe-object\n// that can be used by `maketx run`, another contract importing foo20, and in\n// the future when we'll support `maketx call Token.XXX`.\npackage bar20\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n)\n\nvar (\n\tToken *grc20.Token\n\tadm   *grc20.PrivateLedger\n\t// UserTeller is intentionally exported for `maketx run` consumption.\n\t// It is the canonical *fnTeller (verifiable via grc20.IsCanonicalTeller);\n\t// foreign realms reading this var can call its methods, but the methods\n\t// resolve the source-of-funds from cur.Previous().Address() via\n\t// IsCurrent, so a caller can only spend their OWN balance — not the\n\t// reader's. No cur is leaked via this exposure.\n\tUserTeller grc20.Teller\n)\n\nfunc init(cur realm) {\n\tToken, adm = grc20.NewToken(0, cur, \"Bar\", \"BAR\", 4)\n\tUserTeller = Token.CallerTeller()\n\tgrc20reg.Register(cross(cur), Token, \"\")\n}\n\nfunc Faucet(cur realm) string {\n\tcaller := cur.Previous().Address()\n\tif err := adm.Mint(caller, 1_000_000); err != nil {\n\t\treturn \"error: \" + err.Error()\n\t}\n\treturn \"OK\"\n}\n\nfunc Render(path string) string {\n\tparts := strings.Split(path, \"/\")\n\tc := len(parts)\n\n\tswitch {\n\tcase path == \"\":\n\t\treturn Token.RenderHome()\n\tcase c == 2 \u0026\u0026 parts[0] == \"balance\":\n\t\towner := address(parts[1])\n\t\tbalance := Token.BalanceOf(owner)\n\t\treturn ufmt.Sprintf(\"%d\\n\", balance)\n\tdefault:\n\t\treturn \"404\\n\"\n\t}\n}\n"},{"name":"bar20_test.gno","body":"package bar20\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestPackage(cur realm, t *testing.T) {\n\talice := testutils.TestAddress(\"alice\")\n\ttesting.SetOriginCaller(alice)\n\n\turequire.Equal(t, UserTeller.BalanceOf(alice), int64(0))\n\turequire.Equal(t, Faucet(cross(cur)), \"OK\")\n\turequire.Equal(t, UserTeller.BalanceOf(alice), int64(1_000_000))\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/defi/bar20\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"KJ2p5PLG+A6YGJr/TvL7tJ5pXT+OA5rLE7r6qEX1bxoDZ1kt8LuYQMPix8OrK+NlV08rdadA1VnLkjj9CxVhDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"foo1155","path":"gno.land/r/demo/foo1155","files":[{"name":"foo1155.gno","body":"package foo1155\n\nimport (\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/demo/tokens/grc1155\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tdummyURI         = \"ipfs://xyz\"\n\tadmin    address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\" // govdao t1 multisig\n\tfoo              = grc1155.NewBasicGRC1155Token(dummyURI)\n)\n\nfunc init() {\n\tmintGRC1155Token(admin) // @administrator (10)\n}\n\nfunc mintGRC1155Token(owner address) {\n\tfor i := 1; i \u003c= 10; i++ {\n\t\ttid := grc1155.TokenID(ufmt.Sprintf(\"%d\", i))\n\t\tfoo.SafeMint(owner, tid, 100)\n\t}\n}\n\n// Getters\n\nfunc BalanceOf(user address, tid grc1155.TokenID) int64 {\n\tbalance, err := foo.BalanceOf(user, tid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn balance\n}\n\nfunc BalanceOfBatch(ul []address, batch []grc1155.TokenID) []int64 {\n\tbalanceBatch, err := foo.BalanceOfBatch(ul, batch)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn balanceBatch\n}\n\nfunc IsApprovedForAll(owner, user address) bool {\n\treturn foo.IsApprovedForAll(owner, user)\n}\n\n// Setters\n\nfunc SetApprovalForAll(_ realm, user address, approved bool) {\n\terr := foo.SetApprovalForAll(user, approved)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc TransferFrom(_ realm, from, to address, tid grc1155.TokenID, amount int64) {\n\terr := foo.SafeTransferFrom(from, to, tid, amount)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc BatchTransferFrom(_ realm, from, to address, batch []grc1155.TokenID, amounts []int64) {\n\terr := foo.SafeBatchTransferFrom(from, to, batch, amounts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Admin\n\nfunc Mint(_ realm, to address, tid grc1155.TokenID, amount int64) {\n\tcaller := unsafe.OriginCaller()\n\tassertIsAdmin(caller)\n\terr := foo.SafeMint(to, tid, amount)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc MintBatch(_ realm, to address, batch []grc1155.TokenID, amounts []int64) {\n\tcaller := unsafe.OriginCaller()\n\tassertIsAdmin(caller)\n\terr := foo.SafeBatchMint(to, batch, amounts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc Burn(_ realm, from address, tid grc1155.TokenID, amount int64) {\n\tcaller := unsafe.OriginCaller()\n\tassertIsAdmin(caller)\n\terr := foo.Burn(from, tid, amount)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc BurnBatch(_ realm, from address, batch []grc1155.TokenID, amounts []int64) {\n\tcaller := unsafe.OriginCaller()\n\tassertIsAdmin(caller)\n\terr := foo.BatchBurn(from, batch, amounts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Render\n\nfunc Render(path string) string {\n\tswitch {\n\tcase path == \"\":\n\t\treturn foo.RenderHome()\n\tdefault:\n\t\treturn \"404\\n\"\n\t}\n}\n\n// Util\n\nfunc assertIsAdmin(address_XXX address) {\n\tif address_XXX != admin {\n\t\tpanic(\"restricted access\")\n\t}\n}\n"},{"name":"foo1155_test.gno","body":"package foo1155\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/tokens/grc1155\"\n)\n\nfunc TestFoo721(t *testing.T) {\n\tadmin := address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\tbob := address(\"g1ze6et22ces5atv79y4xh38s4kuraey4y2fr6tw\")\n\ttid1 := grc1155.TokenID(\"1\")\n\n\tfor _, tc := range []struct {\n\t\tname     string\n\t\texpected any\n\t\tfn       func() any\n\t}{\n\t\t{\"BalanceOf(admin, tid1)\", int64(100), func() any { return BalanceOf(admin, tid1) }},\n\t\t{\"BalanceOf(bob, tid1)\", int64(0), func() any { return BalanceOf(bob, tid1) }},\n\t\t{\"IsApprovedForAll(admin, bob)\", false, func() any { return IsApprovedForAll(admin, bob) }},\n\t} {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tgot := tc.fn()\n\t\t\tif tc.expected != got {\n\t\t\t\tt.Errorf(\"expected: %v got: %v\", tc.expected, got)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/foo1155\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"wgrWzBSvgI4wdP4vLIBi/M3yUyFbKGuO6J1jB1ClpIzWlEVGXPyDw9N59/xkSpg82Z/yfV6UASHs8cUzbjrrBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"foo721","path":"gno.land/r/demo/foo721","files":[{"name":"foo721.gno","body":"package foo721\n\nimport (\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tadmin address = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\" // govdao t1 multisig\n\tfoo   *grc721.BasicNFT\n)\n\nfunc init(cur realm) {\n\tfoo = grc721.NewBasicNFT(0, cur, \"FooNFT\", \"FNFT\")\n\tmintNNFT(admin, 10)                                     // @administrator (10)\n\tmintNNFT(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\", 5) // govdao t1 multisig (5)\n}\n\nfunc mintNNFT(owner address, n int64) {\n\tcount := foo.TokenCount()\n\tfor i := count; i \u003c count+n; i++ {\n\t\ttid := grc721.TokenID(ufmt.Sprintf(\"%d\", i))\n\t\tfoo.Mint(owner, tid)\n\t}\n}\n\n// Getters\n\nfunc BalanceOf(user address) int64 {\n\tbalance, err := foo.BalanceOf(user)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn balance\n}\n\nfunc OwnerOf(tid grc721.TokenID) address {\n\towner, err := foo.OwnerOf(tid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn owner\n}\n\nfunc IsApprovedForAll(owner, user address) bool {\n\treturn foo.IsApprovedForAll(owner, user)\n}\n\nfunc GetApproved(tid grc721.TokenID) address {\n\taddr, err := foo.GetApproved(tid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn addr\n}\n\n// Setters\n\n// The Approve / SetApprovalForAll / TransferFrom wrappers are the\n// owning realm's trusted boundary: they derive `caller` from\n// `cur.Previous().Address()` (unforgeable per-frame identity) and\n// hand the address-typed parameter to the concrete writer methods on\n// the unexported `foo *basicNFT`. The basicNFT writer methods take\n// `caller address` only — no rlm crosses the call, so even if the\n// underlying type were ever exposed as an interface, no cur could\n// leak through. See `p/demo/tokens/grc721/igrc721.gno` for the\n// Reader/Writer split doctrine.\nfunc Approve(cur realm, user address, tid grc721.TokenID) {\n\tcaller := cur.Previous().Address()\n\terr := foo.Approve(caller, user, tid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc SetApprovalForAll(cur realm, user address, approved bool) {\n\tcaller := cur.Previous().Address()\n\terr := foo.SetApprovalForAll(caller, user, approved)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc TransferFrom(cur realm, from, to address, tid grc721.TokenID) {\n\tcaller := cur.Previous().Address()\n\terr := foo.TransferFrom(caller, from, to, tid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Admin\n\nfunc Mint(cur realm, to address, tid grc721.TokenID) {\n\tcaller := cur.Previous().Address()\n\tassertIsAdmin(caller)\n\terr := foo.Mint(to, tid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc Burn(cur realm, tid grc721.TokenID) {\n\tcaller := cur.Previous().Address()\n\tassertIsAdmin(caller)\n\terr := foo.Burn(tid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Render\n\nfunc Render(path string) string {\n\tswitch {\n\tcase path == \"\":\n\t\treturn foo.RenderHome()\n\tdefault:\n\t\treturn \"404\\n\"\n\t}\n}\n\n// Util\n\nfunc assertIsAdmin(address_XXX address) {\n\tif address_XXX != admin {\n\t\tpanic(\"restricted access\")\n\t}\n}\n"},{"name":"foo721_test.gno","body":"package foo721\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/demo/tokens/grc721\"\n)\n\nfunc TestFoo721(t *testing.T) {\n\tadmin := address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\thariom := address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\n\tfor _, tc := range []struct {\n\t\tname     string\n\t\texpected any\n\t\tfn       func() any\n\t}{\n\t\t{\"BalanceOf(admin)\", int64(15), func() interface{} { return BalanceOf(admin) }},\n\t\t{\"BalanceOf(hariom)\", int64(15), func() interface{} { return BalanceOf(hariom) }},\n\t\t{\"OwnerOf(0)\", admin, func() interface{} { return OwnerOf(grc721.TokenID(\"0\")) }},\n\t\t{\"IsApprovedForAll(admin, hariom)\", false, func() interface{} { return IsApprovedForAll(admin, hariom) }},\n\t} {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tgot := tc.fn()\n\t\t\tif tc.expected != got {\n\t\t\t\tt.Errorf(\"expected: %v got: %v\", tc.expected, got)\n\t\t\t}\n\t\t})\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/foo721\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"d2ePpyClh7C7bvU+NXAuivtmG8z5Jv9HwSXL0D1Y3flilwBsrdSGzVGLkjpg5KYeljUbsVxlzF8In7O5SmReCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"shifumi","path":"gno.land/r/demo/games/shifumi","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/games/shifumi\"\ngno = \"0.9\"\n"},{"name":"shifumi.gno","body":"package shifumi\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\n\t\"gno.land/r/sys/users\"\n)\n\nconst (\n\tempty = iota\n\trock\n\tpaper\n\tscissors\n\tlast\n)\n\ntype game struct {\n\tplayer1, player2 address // shifumi is a 2 players game\n\tmove1, move2     int     // can be empty, rock, paper, or scissors\n}\n\nvar games avl.Tree\nvar id seqid.ID\n\nfunc (g *game) play(player address, move int) error {\n\tif !(move \u003e empty \u0026\u0026 move \u003c last) {\n\t\treturn errors.New(\"invalid move\")\n\t}\n\tif player != g.player1 \u0026\u0026 player != g.player2 {\n\t\treturn errors.New(\"invalid player\")\n\t}\n\tif player == g.player1 \u0026\u0026 g.move1 == empty {\n\t\tg.move1 = move\n\t\treturn nil\n\t}\n\tif player == g.player2 \u0026\u0026 g.move2 == empty {\n\t\tg.move2 = move\n\t\treturn nil\n\t}\n\treturn errors.New(\"already played\")\n}\n\nfunc (g *game) winner() int {\n\tif g.move1 == empty || g.move2 == empty {\n\t\treturn -1\n\t}\n\tif g.move1 == g.move2 {\n\t\treturn 0\n\t}\n\tif g.move1 == rock \u0026\u0026 g.move2 == scissors ||\n\t\tg.move1 == paper \u0026\u0026 g.move2 == rock ||\n\t\tg.move1 == scissors \u0026\u0026 g.move2 == paper {\n\t\treturn 1\n\t}\n\treturn 2\n}\n\n// NewGame creates a new game where player1 is the caller and player2 the argument.\n// A new game index is returned.\nfunc NewGame(cur realm, player address) int {\n\tgames.Set(id.Next().String(), \u0026game{player1: cur.Previous().Address(), player2: player})\n\treturn int(id)\n}\n\n// Play executes a move for the game at index idx, where move can be:\n// 1 (rock), 2 (paper), 3 (scissors).\nfunc Play(cur realm, idx, move int) {\n\tv := games.Get(seqid.ID(idx).String())\n\tif v == nil {\n\t\tpanic(\"game not found\")\n\t}\n\tif err := v.(*game).play(cur.Previous().Address(), move); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc Render(path string) string {\n\tmov1 := []string{\"\", \" 🤜  \", \" 🫱  \", \" 👉  \"}\n\tmov2 := []string{\"\", \" 🤛  \", \" 🫲  \", \" 👈  \"}\n\twin := []string{\"pending\", \"draw\", \"player1\", \"player2\"}\n\n\toutput := `# 👊  ✋  ✌️  Shifumi\nActions:\n* [NewGame](shifumi$help\u0026func=NewGame) opponentAddress\n* [Play](shifumi$help\u0026func=Play) gameIndex move (1=rock, 2=paper, 3=scissors)\n\n game  | player1 |     | player2 |       | win \n --- | --- | --- | --- | --- | ---\n`\n\t// Output the 100 most recent games.\n\tmaxGames := 100\n\tfor n := int(id); n \u003e 0 \u0026\u0026 int(id)-n \u003c maxGames; n-- {\n\t\tv := games.Get(seqid.ID(n).String())\n\t\tif v == nil {\n\t\t\tcontinue\n\t\t}\n\t\tg := v.(*game)\n\t\toutput += strconv.Itoa(n) + \" | \" +\n\t\t\tshortName(g.player1) + \" | \" + mov1[g.move1] + \" | \" +\n\t\t\tshortName(g.player2) + \" | \" + mov2[g.move2] + \" | \" +\n\t\t\twin[g.winner()+1] + \"\\n\"\n\t}\n\treturn output\n}\n\nfunc shortName(addr address) string {\n\tuser := users.ResolveAddress(addr)\n\tif user != nil {\n\t\treturn user.Name()\n\t}\n\tif len(addr) \u003c 10 {\n\t\treturn string(addr)\n\t}\n\treturn string(addr)[:10] + \"...\"\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"q69Q1oGz2gjUkuJuTPk+thdqEXJYstPTxDdQrZZEDkZQ9D1O5wp9P1BpzYRktF1ToCYbLzfdbZT570/6rg3AAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"math_eval","path":"gno.land/r/demo/math_eval","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/math_eval\"\ngno = \"0.9\"\n"},{"name":"math_eval.gno","body":"// eval realm is capable of evaluating 32-bit integer\n// expressions as they would appear in Go. For example:\n// /r/demo/math_eval:(4+12)/2-1+11*15\npackage math_eval\n\nimport (\n\tevalint32 \"gno.land/p/demo/math_eval/int32\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc Render(p string) string {\n\tif len(p) == 0 {\n\t\treturn `\nevaluates 32-bit integer expressions. for example:\n\t\t\n[(4+12)/2-1+11*15](/r/demo/math_eval:(4+12)/2-1+11*15)\n\n`\n\t}\n\texpr, err := evalint32.Parse(p)\n\tif err != nil {\n\t\treturn err.Error()\n\t}\n\tres, err := evalint32.Eval(expr, nil)\n\tif err != nil {\n\t\treturn err.Error()\n\t}\n\n\treturn ufmt.Sprintf(\"%s = %d\", p, res)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"X4F9bZFmm/rSXH+yW2i0n9IULZTZvUguYyfaMAypKz9RhMdfM5Zeh3hb04BchVfE3X66V9h+MOQUdmS82nw4Ag=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"memeland","path":"gno.land/r/demo/memeland","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/memeland\"\ngno = \"0.9\"\n"},{"name":"memeland.gno","body":"package memeland\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/demo/memeland\"\n)\n\nvar m *memeland.Memeland\n\nfunc init() {\n\tm = memeland.NewMemeland(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\")\n}\n\nfunc PostMeme(cur realm, data string, timestamp int64) string {\n\treturn m.PostMeme(0, cur, data, timestamp)\n}\n\nfunc Upvote(cur realm, id string) string {\n\treturn m.Upvote(0, cur, id)\n}\n\nfunc GetPostsInRange(startTimestamp, endTimestamp int64, page, pageSize int, sortBy string) string {\n\treturn m.GetPostsInRange(startTimestamp, endTimestamp, page, pageSize, sortBy)\n}\n\nfunc RemovePost(cur realm, id string) string {\n\treturn m.RemovePost(0, cur, id)\n}\n\nfunc GetOwner() address {\n\treturn m.Owner()\n}\n\nfunc TransferOwnership(cur realm, newOwner address) {\n\tif err := m.TransferOwnership(0, cur, newOwner); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc Render(path string) string {\n\tnumOfMemes := int(m.MemeCounter)\n\tif numOfMemes == 0 {\n\t\treturn \"No memes posted yet! :/\"\n\t}\n\n\t// Default render is get Posts since year 2000 to now\n\treturn m.GetPostsInRange(0, time.Now().Unix(), 1, 10, \"DATE_CREATED\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"f5UWAA1EHiJYm5A1i5Lj1dxesX9dGkakG9S56t6vKCpB2LV5QR5mk3Wrx1FM5p9d/VFg5yqPc2vnnd6agzSNBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"microblog","path":"gno.land/r/demo/microblog","files":[{"name":"README.md","body":"# microblog realm\n\n## Getting started:\n\n(One-time) Add the microblog package:\n\n```\ngnokey maketx addpkg --pkgpath \"gno.land/p/demo/microblog\" --pkgdir \"examples/gno.land/p/demo/microblog\" \\\n    --deposit 100000000ugnot --gas-fee 1000000ugnot --gas-wanted 2000000 --chainid dev --remote localhost:26657 \u003cYOURKEY\u003e\n```\n\n(One-time) Add the microblog realm:\n\n```\ngnokey maketx addpkg --pkgpath \"gno.land/r/demo/microblog\" --pkgdir \"examples/gno.land/r/demo/microblog\" \\\n    --deposit 100000000ugnot --gas-fee 1000000ugnot --gas-wanted 2000000 --chainid dev --remote localhost:26657 \u003cYOURKEY\u003e\n```\n\nAdd a microblog post:\n\n```\ngnokey maketx call --pkgpath \"gno.land/r/demo/microblog\" --func \"NewPost\" --args \"hello, world\" \\\n    --gas-fee \"1000000ugnot\" --gas-wanted \"2000000\" --chainid dev --remote localhost:26657 \u003cYOURKEY\u003e\n```\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/microblog\"\ngno = \"0.9\"\n"},{"name":"microblog.gno","body":"// Microblog is a website with shortform posts from users.\n// The API is simple - \"AddPost\" takes markdown and\n// adds it to the users site.\n// The microblog location is determined by the user address\n// /r/demo/microblog:\u003cYOUR-ADDRESS\u003e\npackage microblog\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/demo/microblog\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\tsusers \"gno.land/r/sys/users\"\n)\n\nvar (\n\ttitle  = \"gno-based microblog\"\n\tprefix = \"/r/demo/microblog:\"\n\tm      *microblog.Microblog\n)\n\nfunc init() {\n\tm = microblog.NewMicroblog(title, prefix)\n}\n\nfunc renderHome() string {\n\toutput := ufmt.Sprintf(\"# %s\\n\\n\", m.Title)\n\toutput += \"# pages\\n\\n\"\n\n\tfor _, page := range m.GetPages() {\n\t\tif u := susers.ResolveAddress(page.Author); u != nil {\n\t\t\toutput += ufmt.Sprintf(\"- [%s (%s)](%s%s)\\n\", u.Name(), page.Author.String(), m.Prefix, page.Author.String())\n\t\t} else {\n\t\t\toutput += ufmt.Sprintf(\"- [%s](%s%s)\\n\", page.Author.String(), m.Prefix, page.Author.String())\n\t\t}\n\t}\n\n\treturn output\n}\n\nfunc renderUser(user string) string {\n\tsilo := m.Pages.Get(user)\n\tif silo == nil {\n\t\treturn \"404\" // StatusNotFound\n\t}\n\n\treturn PageToString((silo.(*microblog.Page)))\n}\n\nfunc Render(path string) string {\n\tparts := strings.Split(path, \"/\")\n\n\tisHome := path == \"\"\n\tisUser := len(parts) == 1\n\n\tswitch {\n\tcase isHome:\n\t\treturn renderHome()\n\n\tcase isUser:\n\t\treturn renderUser(parts[0])\n\t}\n\n\treturn \"404\" // StatusNotFound\n}\n\nfunc PageToString(p *microblog.Page) string {\n\to := \"\"\n\tif u := susers.ResolveAddress(p.Author); u != nil {\n\t\to += ufmt.Sprintf(\"# [%s](/u/%s)\\n\\n\", u, u)\n\t}\n\to += ufmt.Sprintf(\"## [%s](/r/demo/microblog:%s)\\n\\n\", p.Author, p.Author)\n\n\to += ufmt.Sprintf(\"joined %s, last updated %s\\n\\n\", p.CreatedAt.Format(\"2006-02-01\"), p.LastPosted.Format(\"2006-02-01\"))\n\to += \"## feed\\n\\n\"\n\tfor _, u := range p.GetPosts() {\n\t\to += u.String() + \"\\n\\n\"\n\t}\n\treturn o\n}\n\n// NewPost takes a single argument (post markdown) and\n// adds a post to the address of the caller.\nfunc NewPost(cur realm, text string) string {\n\tif err := m.NewPost(0, cur, text); err != nil {\n\t\treturn \"unable to add new post\"\n\t}\n\treturn \"added new post\"\n}\n"},{"name":"microblog_test.gno","body":"package microblog\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// postAs threads the SetRealm'd cur into m.NewPost via cross(rlm).\n// Callers SetRealm in the enclosing crossing frame so the cur HIV\n// mutation lands where rlm captures it.\nfunc postAs(_ int, rlm realm, text string) error {\n\tvar err error\n\tfunc(cur realm) { err = m.NewPost(0, cur, text) }(cross(rlm))\n\treturn err\n}\n\nfunc TestMicroblog(cur realm, t *testing.T) {\n\tvar (\n\t\tauthor1 address = testutils.TestAddress(\"author1\")\n\t\tauthor2 address = testutils.TestAddress(\"author2\")\n\t)\n\n\turequire.Equal(t, \"404\", Render(\"/wrongpath\"), \"rendering not giving 404\")\n\turequire.NotEqual(t, \"404\", Render(\"\"), \"rendering / should not give 404\")\n\ttesting.SetRealm(testing.NewUserRealm(author1))\n\turequire.NoError(t, postAs(0, cur, \"goodbyte, web2\"), \"could not create post\")\n\n\t_, err := m.GetPage(author1.String())\n\turequire.NoError(t, err, \"silo should exist\")\n\n\t_, err = m.GetPage(\"no such author\")\n\turequire.Error(t, err, \"silo should not exist\")\n\n\ttesting.SetRealm(testing.NewUserRealm(author2))\n\turequire.NoError(t, postAs(0, cur, \"hello, web3\"), \"could not create post\")\n\turequire.NoError(t, postAs(0, cur, \"hello again, web3\"), \"could not create post\")\n\turequire.NoError(t, postAs(0, cur, \"hi again,\\n web4?\"), \"could not create post\")\n\n\tprintln(\"--- MICROBLOG ---\\n\\n\")\n\n\texpected := `# gno-based microblog\n\n# pages\n\n- [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/microblog:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6)\n- [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/microblog:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00)\n`\n\turequire.Equal(t, expected, Render(\"\"), \"incorrect rendering\")\n\n\texpected = `## [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/microblog:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6)\n\njoined 2009-13-02, last updated 2009-13-02\n\n## feed\n\n\u003e goodbyte, web2\n\u003e\n\u003e *Fri, 13 Feb 2009 23:31:30 UTC*`\n\n\turequire.Equal(t, expected, strings.TrimSpace(Render(author1.String())), \"incorrect rendering\")\n\n\texpected = `## [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/microblog:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00)\n\njoined 2009-13-02, last updated 2009-13-02\n\n## feed\n\n\u003e hi again,\n\u003e\n\u003e web4?\n\u003e\n\u003e *Fri, 13 Feb 2009 23:31:30 UTC*\n\n\u003e hello again, web3\n\u003e\n\u003e *Fri, 13 Feb 2009 23:31:30 UTC*\n\n\u003e hello, web3\n\u003e\n\u003e *Fri, 13 Feb 2009 23:31:30 UTC*`\n\n\turequire.Equal(t, expected, strings.TrimSpace(Render(author2.String())), \"incorrect rendering\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"DX62uQo1XhzrN0yicFIoM4r1+aknpby5o/EMwCa90hKnT5qMjslnqjKDzkfVIfDTMzlPxhz0TiRRdQksLiJoCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"mirror","path":"gno.land/r/demo/mirror","files":[{"name":"doc.gno","body":"// Package mirror demonstrates that users can pass realm functions\n// as arguments to other realms.\npackage mirror\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/mirror\"\ngno = \"0.9\"\n"},{"name":"mirror.gno","body":"package mirror\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar store avl.Tree\n\nfunc Register(_ realm, pkgpath string, rndr func(string) string) {\n\tif store.Has(pkgpath) {\n\t\treturn\n\t}\n\n\tif rndr == nil {\n\t\treturn\n\t}\n\n\tstore.Set(pkgpath, rndr)\n}\n\nfunc Render(path string) string {\n\tif raw := store.Get(path); raw != nil {\n\t\treturn raw.(func(string) string)(\"\")\n\t}\n\n\tif store.Size() == 0 {\n\t\treturn \"None are fair.\"\n\t}\n\n\treturn \"Mirror, mirror on the wall, which realm's the fairest of them all?\"\n}\n\n// Credits to @jeronimoalbi\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"UTGOAg/MnWEAdZAkBkLdWHCKsVmqn2LWrnxPiKCffcyp5nBEq9/e5hh4cU3CCKEmpLUgZqf3BdKGNSfO1u+UAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"releases_example","path":"gno.land/r/demo/releases_example","files":[{"name":"dummy.gno","body":"package releases_example\n\nfunc init() {\n\t// dummy example data\n\tchangelog.NewRelease(\n\t\t\"v1\",\n\t\t\"r/demo/examples_example_v1\",\n\t\t\"initial release\",\n\t)\n\tchangelog.NewRelease(\n\t\t\"v2\",\n\t\t\"r/demo/examples_example_v2\",\n\t\t\"various improvements\",\n\t)\n}\n"},{"name":"example.gno","body":"// this package demonstrates a way to manage contract releases.\npackage releases_example\n\nimport (\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/demo/releases\"\n)\n\nvar (\n\tchangelog = releases.NewChangelog(\"example_app\")\n\tadmin     = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\") // govdao t1 multisig\n)\n\nfunc init() {\n\t// FIXME: admin = std.GetCreator()\n}\n\nfunc NewRelease(_ realm, name, url, notes string) {\n\tcaller := unsafe.OriginCaller()\n\tif caller != admin {\n\t\tpanic(\"restricted area\")\n\t}\n\tchangelog.NewRelease(name, url, notes)\n}\n\nfunc UpdateAdmin(_ realm, address_XXX address) {\n\tcaller := unsafe.OriginCaller()\n\tif caller != admin {\n\t\tpanic(\"restricted area\")\n\t}\n\tadmin = address_XXX\n}\n\nfunc Render(path string) string {\n\treturn changelog.Render(path)\n}\n"},{"name":"example0_test.gno","body":"package releases_example\n\nimport (\n\t\"gno.land/p/demo/releases\"\n)\n\nfunc ExampleNewChangelog() {\n\tprintln(\"-----------\")\n\tchangelog := releases.NewChangelog(\"example\")\n\tprint(changelog.Render(\"\"))\n\n\tprintln(\"-----------\")\n\tchangelog.NewRelease(\"v1\", \"r/blahblah\", \"* initial version\")\n\tprint(changelog.Render(\"\"))\n\n\tprintln(\"-----------\")\n\tchangelog.NewRelease(\"v2\", \"r/blahblah2\", \"* various improvements\\n* new shiny logo\")\n\tprint(changelog.Render(\"\"))\n\n\tprintln(\"-----------\")\n\tprint(changelog.Latest().Render())\n\n\t// Output:\n\t// -----------\n\t// # example\n\t//\n\t// -----------\n\t// # example\n\t//\n\t// ## [example v1 (latest)](r/blahblah)\n\t//\n\t// * initial version\n\t//\n\t// -----------\n\t// # example\n\t//\n\t// ## [example v2 (latest)](r/blahblah2)\n\t//\n\t// * various improvements\n\t// * new shiny logo\n\t//\n\t// ## [example v1](r/blahblah)\n\t//\n\t// * initial version\n\t//\n\t// -----------\n\t// ## [example v2 (latest)](r/blahblah2)\n\t//\n\t// * various improvements\n\t// * new shiny logo\n}\n"},{"name":"example1_test.gno","body":"package releases_example\n\nfunc ExampleRender() {\n\tprintln(\"-----------\")\n\tprint(Render(\"\"))\n\n\tprintln(\"-----------\")\n\tprint(Render(\"v1\"))\n\n\tprintln(\"-----------\")\n\tprint(Render(\"v42\"))\n\n\t// Output:\n\t// -----------\n\t// # example_app\n\t//\n\t// ## [example_app v2 (latest)](r/demo/examples_example_v2)\n\t//\n\t// various improvements\n\t//\n\t// ## [example_app v1](r/demo/examples_example_v1)\n\t//\n\t// initial release\n\t//\n\t// -----------\n\t// ## [example_app v1](r/demo/examples_example_v1)\n\t//\n\t// initial release\n\t//\n\t// -----------\n\t// no such release\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/releases_example\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"616C32/yrQJ3a3Cgf3NmpJ3dC4xNr5NsncoVJm3OyE5huwWsPpdXdquMsJtBoXOdnzp05m7Qb9IKj02yExwnDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"tamagotchi","path":"gno.land/r/demo/tamagotchi","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/tamagotchi\"\ngno = \"0.9\"\n"},{"name":"realm.gno","body":"package tamagotchi\n\nimport (\n\t\"chain/runtime\"\n\n\t\"gno.land/p/demo/tamagotchi\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar t *tamagotchi.Tamagotchi\n\nfunc init(cur realm) {\n\tReset(cur, \"gnome#0\")\n}\n\nfunc Reset(cur realm, optionalName string) string {\n\tname := optionalName\n\tif name == \"\" {\n\t\theight := runtime.ChainHeight()\n\t\tname = ufmt.Sprintf(\"gnome#%d\", height)\n\t}\n\n\tt = tamagotchi.New(name)\n\n\treturn ufmt.Sprintf(\"A new tamagotchi is born. Their name is %s %s.\", t.Name(), t.Face())\n}\n\nfunc Feed(cur realm) string {\n\tt.Feed()\n\treturn t.Markdown()\n}\n\nfunc Play(cur realm) string {\n\tt.Play()\n\treturn t.Markdown()\n}\n\nfunc Heal(cur realm) string {\n\tt.Heal()\n\treturn t.Markdown()\n}\n\nfunc Render(path string) string {\n\ttama := t.Markdown()\n\tlinks := `Actions:\n* [Feed](/r/demo/tamagotchi$help\u0026func=Feed)\n* [Play](/r/demo/tamagotchi$help\u0026func=Play)\n* [Heal](/r/demo/tamagotchi$help\u0026func=Heal)\n* [Reset](/r/demo/tamagotchi$help\u0026func=Reset)\n`\n\n\treturn tama + \"\\n\\n\" + links\n}\n"},{"name":"z0_filetest.gno","body":"// PKGPATH: gno.land/r/demo/tamagotchi/filetests/z0_filetest\n\npackage z0_filetest\n\nimport (\n\t\"gno.land/r/demo/tamagotchi\"\n)\n\nfunc main(cur realm) {\n\ttamagotchi.Reset(cross(cur), \"tamagnotchi\")\n\tprintln(tamagotchi.Render(\"\"))\n}\n\n// Output:\n// # tamagnotchi 😃\n//\n// * age: 0\n// * hunger: 50\n// * happiness: 50\n// * health: 50\n// * sleepy: 0\n//\n// Actions:\n// * [Feed](/r/demo/tamagotchi$help\u0026func=Feed)\n// * [Play](/r/demo/tamagotchi$help\u0026func=Play)\n// * [Heal](/r/demo/tamagotchi$help\u0026func=Heal)\n// * [Reset](/r/demo/tamagotchi$help\u0026func=Reset)\n//\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"XvgNw0qKoW+biiVk7sqYW1cFSIeNipeRnT0k3muMpataA+z5PVEvM/gcSBiwfDLu7QuQaRSRUHMzl7jHFFkcCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"todolist","path":"gno.land/r/demo/todolist","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/demo/todolist\"\ngno = \"0.9\"\n"},{"name":"todolist.gno","body":"package todolist\n\nimport (\n\t\"bytes\"\n\t\"strconv\"\n\n\t\"gno.land/p/demo/todolist\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// State variables\nvar (\n\ttodolistTree *avl.Tree\n\ttlid         seqid.ID\n)\n\n// Constructor\nfunc init() {\n\ttodolistTree = avl.NewTree()\n}\n\nfunc NewTodoList(_ realm, title string) (int, string) {\n\t// Create new Todolist\n\ttl := todolist.NewTodoList(title)\n\t// Update AVL tree with new state\n\ttlid.Next()\n\ttodolistTree.Set(strconv.Itoa(int(tlid)), tl)\n\treturn int(tlid), \"created successfully\"\n}\n\nfunc AddTask(_ realm, todolistID int, title string) string {\n\t// Get Todolist from AVL tree\n\ttl := todolistTree.Get(strconv.Itoa(todolistID))\n\tif tl == nil {\n\t\tpanic(\"Todolist not found\")\n\t}\n\n\t// get the number of tasks in the todolist\n\tid := tl.(*todolist.TodoList).Tasks.Size()\n\n\t// create the task\n\ttask := todolist.NewTask(title)\n\n\t// Cast raw data from tree into Todolist struct\n\ttl.(*todolist.TodoList).AddTask(id, task)\n\n\treturn \"task added successfully\"\n}\n\nfunc ToggleTaskStatus(_ realm, todolistID int, taskID int) string {\n\t// Get Todolist from AVL tree\n\ttl := todolistTree.Get(strconv.Itoa(todolistID))\n\tif tl == nil {\n\t\tpanic(\"Todolist not found\")\n\t}\n\n\t// Get the task from the todolist\n\ttask := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID))\n\tif task == nil {\n\t\tpanic(\"Task not found\")\n\t}\n\n\t// Change the status of the task\n\ttodolist.ToggleTaskStatus(task.(*todolist.Task))\n\n\treturn \"task status changed successfully\"\n}\n\nfunc RemoveTask(_ realm, todolistID int, taskID int) string {\n\t// Get Todolist from AVL tree\n\ttl := todolistTree.Get(strconv.Itoa(todolistID))\n\tif tl == nil {\n\t\tpanic(\"Todolist not found\")\n\t}\n\n\t// Get the task from the todolist\n\tif !tl.(*todolist.TodoList).Tasks.Has(strconv.Itoa(taskID)) {\n\t\tpanic(\"Task not found\")\n\t}\n\n\t// Change the status of the task\n\ttl.(*todolist.TodoList).RemoveTask(strconv.Itoa(taskID))\n\n\treturn \"task status changed successfully\"\n}\n\nfunc RemoveTodoList(_ realm, todolistID int) string {\n\t// Get Todolist from AVL tree\n\tif !todolistTree.Has(strconv.Itoa(todolistID)) {\n\t\tpanic(\"Todolist not found\")\n\t}\n\n\t// Remove the todolist\n\ttodolistTree.Remove(strconv.Itoa(todolistID))\n\n\treturn \"Todolist removed successfully\"\n}\n\nfunc Render(path string) string {\n\tif path == \"\" {\n\t\treturn renderHomepage()\n\t}\n\n\treturn \"unknown page\"\n}\n\nfunc renderHomepage() string {\n\t// Define empty buffer\n\tvar b bytes.Buffer\n\n\tb.WriteString(\"# Welcome to ToDolist\\n\\n\")\n\n\t// If no todolists have been created\n\tif todolistTree.Size() == 0 {\n\t\tb.WriteString(\"### No todolists available currently!\")\n\t\treturn b.String()\n\t}\n\n\t// Iterate through AVL tree\n\ttodolistTree.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t// cast raw data from tree into Todolist struct\n\t\ttl := value.(*todolist.TodoList)\n\n\t\t// Add Todolist name\n\t\tb.WriteString(\n\t\t\tufmt.Sprintf(\n\t\t\t\t\"## Todolist #%s: %s\\n\",\n\t\t\t\tkey, // Todolist ID\n\t\t\t\ttl.GetTodolistTitle(),\n\t\t\t),\n\t\t)\n\n\t\t// Add Todolist owner\n\t\tb.WriteString(\n\t\t\tufmt.Sprintf(\n\t\t\t\t\"#### Todolist owner : %s\\n\",\n\t\t\t\ttl.GetTodolistOwner(),\n\t\t\t),\n\t\t)\n\n\t\t// List all todos that are currently Todolisted\n\t\tif todos := tl.GetTasks(); len(todos) \u003e 0 {\n\t\t\tb.WriteString(\n\t\t\t\tufmt.Sprintf(\"Currently Todo tasks: %d\\n\\n\", len(todos)),\n\t\t\t)\n\n\t\t\tfor index, todo := range todos {\n\t\t\t\tb.WriteString(\n\t\t\t\t\tufmt.Sprintf(\"#%d - %s  \", index, todo.Title),\n\t\t\t\t)\n\t\t\t\t// displays a checked box if task is marked as done, an empty box if not\n\t\t\t\tif todo.Done {\n\t\t\t\t\tb.WriteString(\n\t\t\t\t\t\t\"☑\\n\\n\",\n\t\t\t\t\t)\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tb.WriteString(\n\t\t\t\t\t\"☐\\n\\n\",\n\t\t\t\t)\n\t\t\t}\n\t\t} else {\n\t\t\tb.WriteString(\"No tasks in this list currently\\n\")\n\t\t}\n\n\t\tb.WriteString(\"\\n\")\n\t\treturn false\n\t})\n\n\treturn b.String()\n}\n"},{"name":"todolist_test.gno","body":"package todolist\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strconv\"\n\t\"testing\"\n\n\t\"gno.land/p/demo/todolist\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nvar (\n\tnode any\n\ttdl  *todolist.TodoList\n)\n\nfunc TestNewTodoList(cur realm, t *testing.T) {\n\ttitle := \"My Todo List\"\n\ttlid, _ := NewTodoList(cross(cur), title)\n\tuassert.Equal(t, 1, tlid, \"tlid does not match\")\n\n\t// get the todolist node from the tree\n\tnode = todolistTree.Get(strconv.Itoa(tlid))\n\t// convert the node to a TodoList struct\n\ttdl = node.(*todolist.TodoList)\n\n\tuassert.Equal(t, title, tdl.Title, \"title does not match\")\n\tuassert.Equal(t, 1, tlid, \"tlid does not match\")\n\tuassert.Equal(t, tdl.Owner.String(), unsafe.OriginCaller().String(), \"owner does not match\")\n\tuassert.Equal(t, 0, len(tdl.GetTasks()), \"Expected no tasks in the todo list\")\n}\n\nfunc TestAddTask(cur realm, t *testing.T) {\n\tAddTask(cross(cur), 1, \"Task 1\")\n\n\ttasks := tdl.GetTasks()\n\tuassert.Equal(t, 1, len(tasks), \"total task does not match\")\n\tuassert.Equal(t, \"Task 1\", tasks[0].Title, \"task title does not match\")\n\tuassert.False(t, tasks[0].Done, \"Expected task to be not done\")\n}\n\nfunc TestToggleTaskStatus(cur realm, t *testing.T) {\n\tToggleTaskStatus(cross(cur), 1, 0)\n\ttask := tdl.GetTasks()[0]\n\tuassert.True(t, task.Done, \"Expected task to be done, but it is not marked as done\")\n\n\tToggleTaskStatus(cross(cur), 1, 0)\n\tuassert.False(t, task.Done, \"Expected task to be not done, but it is marked as done\")\n}\n\nfunc TestRemoveTask(cur realm, t *testing.T) {\n\tRemoveTask(cross(cur), 1, 0)\n\ttasks := tdl.GetTasks()\n\tuassert.Equal(t, 0, len(tasks), \"Expected no tasks in the todo list\")\n}\n\nfunc TestRemoveTodoList(cur realm, t *testing.T) {\n\tRemoveTodoList(cross(cur), 1)\n\tuassert.Equal(t, 0, todolistTree.Size(), \"Expected no tasks in the todo list\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"VDFlnmtBWDQrsgm/rbe+cJD/t9yOnLTXlryeaQkLxw81NacctlcztgijSVgGc1888Qi1NNvwZA3L/g4JTYscAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"config","path":"gno.land/r/devrels/config","files":[{"name":"config.gno","body":"package config\n\nimport (\n\t\"gno.land/p/moul/authz\"\n)\n\nvar (\n\tGnoDevRels = authz.NewMemberAuthority(\n\t\taddress(\"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"), // @moul\n\t\taddress(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"), // @leohhhn\n\t)\n\tAuth = authz.NewWithAuthority(GnoDevRels)\n)\n\nfunc Render(_ string) string {\n\treturn Auth.Authority().String()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/devrels/config\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"CWegvT7tsjWAtLxa0+0hUdnFolJ59Oc4S3fAvKLgpCDkNKRxZQtyJREqPk4cs7RC5NQu3sl26Da2R/4de4pUAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"adder","path":"gno.land/r/docs/adder","files":[{"name":"adder.gno","body":"package adder\n\nimport (\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/moul/txlink\"\n)\n\n// Global variables to store the current number and last update timestamp\nvar (\n\tnumber     int\n\tlastUpdate time.Time\n)\n\n// Add function to update the number and timestamp\nfunc Add(_ realm, n int) {\n\tnumber += n\n\tlastUpdate = time.Now()\n}\n\n// Render displays the current number value, last update timestamp, and a link to call Add with 42\nfunc Render(_ string) string {\n\t// Display the current number and formatted last update time\n\tresult := \"# Add Example\\n\\n\"\n\tresult += \"Current Number: \" + strconv.Itoa(number) + \"\\n\\n\"\n\tresult += \"Last Updated: \" + formatTimestamp(lastUpdate) + \"\\n\\n\"\n\n\t// Generate a transaction link to call Add with 42 as the default parameter\n\ttxLink := txlink.Call(\"Add\", \"n\", \"42\")\n\tresult += \"[Increase Number](\" + txLink + \")\\n\"\n\n\treturn result\n}\n\n// Helper function to format the timestamp for readability\nfunc formatTimestamp(timestamp time.Time) string {\n\tif timestamp.IsZero() {\n\t\treturn \"Never\"\n\t}\n\treturn timestamp.Format(\"2006-01-02 15:04:05\")\n}\n"},{"name":"adder_test.gno","body":"package adder\n\nimport (\n\t\"testing\"\n)\n\nfunc TestRenderAndAdd(cur realm, t *testing.T) {\n\t// Initial Render output\n\toutput := Render(\"\")\n\texpected := `# Add Example\n\nCurrent Number: 0\n\nLast Updated: Never\n\n[Increase Number](/r/docs/adder$help\u0026func=Add\u0026n=42)\n`\n\tif output != expected {\n\t\tt.Errorf(\"Initial Render failed, got:\\n%s\", output)\n\t}\n\n\t// Call Add with a value of 10\n\tAdd(cross(cur), 10)\n\n\t// Call Add again with a value of -5\n\tAdd(cross(cur), -5)\n\n\t// Render after two Add calls\n\tfinalOutput := Render(\"\")\n\n\t// Initial Render output\n\toutput = Render(\"\")\n\texpected = `# Add Example\n\nCurrent Number: 5\n\nLast Updated: 2009-02-13 23:31:30\n\n[Increase Number](/r/docs/adder$help\u0026func=Add\u0026n=42)\n`\n\tif output != expected {\n\t\tt.Errorf(\"Final Render failed, got:\\n%s\\nexpected:\\n%s\", output, finalOutput)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/adder\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"cq5wsT5PAVWicJtPdY9q53/C/JVGOLpGxREfuZLlJTTguEtjAnHtZYpsbDGwXIKJ8hTaRbpTBhe12md2dSKUDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"avl_pager","path":"gno.land/r/docs/avl_pager","files":[{"name":"avl_pager.gno","body":"package avl_pager\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n)\n\n// Tree instance for 100 items\nvar tree *avl.Tree\n\n// Initialize a tree with 100 items.\nfunc init() {\n\ttree = avl.NewTree()\n\tfor i := 1; i \u003c= 100; i++ {\n\t\tkey := \"Item\" + strconv.Itoa(i)\n\t\ttree.Set(key, \"Value of \"+key)\n\t}\n}\n\n// Render paginated content based on the given URL path.\n// URL format: `...?page=\u003cpage\u003e\u0026size=\u003csize\u003e` (default is page 1 and size 10).\nfunc Render(path string) string {\n\tp := pager.NewPager(tree, 10, false) // Default page size is 10\n\tpage := p.MustGetPageByPath(path)\n\n\t// Header and pagination info\n\tresult := \"# Paginated Items\\n\"\n\tresult += \"Page \" + strconv.Itoa(page.PageNumber) + \" of \" + strconv.Itoa(page.TotalPages) + \"\\n\\n\"\n\tresult += page.Picker(path) + \"\\n\\n\"\n\n\t// Display items on the current page\n\tfor _, item := range page.Items {\n\t\tresult += \"- \" + item.Key + \": \" + item.Value.(string) + \"\\n\"\n\t}\n\n\tresult += \"\\n\" + page.Picker(path) // Repeat page picker for ease of navigation\n\treturn result\n}\n"},{"name":"avl_pager_test.gno","body":"package avl_pager\n\nimport (\n\t\"testing\"\n)\n\nfunc TestRender(t *testing.T) {\n\treq := \"?sort=name\u0026order=asc\"\n\toutput := Render(req)\n\texpected := `# Paginated Items\nPage 1 of 10\n\n**1** | [2](?page=2\u0026order=asc\u0026sort=name) | [3](?page=3\u0026order=asc\u0026sort=name) | … | [10](?page=10\u0026order=asc\u0026sort=name)\n\n- Item1: Value of Item1\n- Item10: Value of Item10\n- Item100: Value of Item100\n- Item11: Value of Item11\n- Item12: Value of Item12\n- Item13: Value of Item13\n- Item14: Value of Item14\n- Item15: Value of Item15\n- Item16: Value of Item16\n- Item17: Value of Item17\n\n**1** | [2](?page=2\u0026order=asc\u0026sort=name) | [3](?page=3\u0026order=asc\u0026sort=name) | … | [10](?page=10\u0026order=asc\u0026sort=name)`\n\tif output != expected {\n\t\tt.Errorf(\"Render(%q) failed, got:\\n%s\\nwant:\\n%s\", req, output, expected)\n\t}\n}\n\nfunc TestRender_page2(t *testing.T) {\n\treq := \"?page=2\u0026size=10\u0026sort=name\u0026order=asc\"\n\toutput := Render(req)\n\texpected := `# Paginated Items\nPage 2 of 10\n\n[1](?page=1\u0026order=asc\u0026size=10\u0026sort=name) | **2** | [3](?page=3\u0026order=asc\u0026size=10\u0026sort=name) | [4](?page=4\u0026order=asc\u0026size=10\u0026sort=name) | … | [10](?page=10\u0026order=asc\u0026size=10\u0026sort=name)\n\n- Item18: Value of Item18\n- Item19: Value of Item19\n- Item2: Value of Item2\n- Item20: Value of Item20\n- Item21: Value of Item21\n- Item22: Value of Item22\n- Item23: Value of Item23\n- Item24: Value of Item24\n- Item25: Value of Item25\n- Item26: Value of Item26\n\n[1](?page=1\u0026order=asc\u0026size=10\u0026sort=name) | **2** | [3](?page=3\u0026order=asc\u0026size=10\u0026sort=name) | [4](?page=4\u0026order=asc\u0026size=10\u0026sort=name) | … | [10](?page=10\u0026order=asc\u0026size=10\u0026sort=name)`\n\tif output != expected {\n\t\tt.Errorf(\"Render(%q) failed, got:\\n%s\\nwant:\\n%s\", req, output, expected)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/avl_pager\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"XCst48v+j0DPDUvPE4Yf6dwkErLuak8htzGMZx6AAqkdCl8EJlQgH3cBwuEYxjTWSDkdECppW1D6pCDDj0sNBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"avl_pager_params","path":"gno.land/r/docs/avl_pager_params","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/avl_pager_params\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package avl_pager_params\n\nimport (\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// We'll keep some demo data in an AVL tree to showcase pagination.\nvar (\n\titems     *avl.Tree\n\tidCounter seqid.ID\n)\n\nfunc init() {\n\titems = avl.NewTree()\n\t// Populate the tree with 15 sample items for demonstration.\n\tfor i := 1; i \u003c= 15; i++ {\n\t\tid := idCounter.Next().String()\n\t\titems.Set(id, \"Some item value: \"+id)\n\t}\n}\n\nfunc Render(path string) string {\n\t// 1) Parse the incoming path to split route vs. query.\n\treq := realmpath.Parse(path)\n\t//    - req.Path contains everything *before* ? or $ (? - query params, $ - gnoweb params)\n\t//    - The remaining part (page=2, size=5, etc.) is not in req.Path.\n\n\t// 2) If no specific route is provided (req.Path == \"\"), we’ll show a “home” page\n\t//    that displays a list of configs in paginated form.\n\tif req.Path == \"\" {\n\t\treturn renderHome(path)\n\t}\n\n\t// 3) If a route *is* provided (e.g. :SomeKey),\n\t//    we will interpret it as a request for a specific page.\n\treturn renderConfigItem(req.Path)\n}\n\n// renderHome shows a paginated list of config items if route == \"\".\nfunc renderHome(fullPath string) string {\n\t// Create a Pager for our config tree, with a default page size of 5.\n\tp := pager.NewPager(items, 5, false)\n\n\t// MustGetPageByPath uses the *entire* path (including query parts: ?page=2, etc.)\n\tpage := p.MustGetPageByPath(fullPath)\n\n\t// Start building the output (plain text or markdown).\n\tout := \"# AVL Pager + Render paths\\n\\n\"\n\tout += `This realm showcases how to maintain a paginated list while properly parsing render paths. \nYou can see how a single page can include a paginated element (like the example below), and how clicking \nan item can take you to a dedicated page for that specific item.\n\nNo matter how you browse through the paginated list, the introductory text (this section) remains the same.\n\n`\n\n\tout += ufmt.Sprintf(\"Showing page %d of %d\\n\\n\", page.PageNumber, page.TotalPages)\n\n\t// List items for this page.\n\tfor _, item := range page.Items {\n\t\t// Link each item to a details page: e.g. \":Config01\"\n\t\tout += ufmt.Sprintf(\"- [Item %s](/r/docs/avl_pager_params:%s)\\n\", item.Key, item.Key)\n\t}\n\n\t// Insert pagination controls (previous/next links, etc.).\n\tout += \"\\n\" + page.Picker(fullPath) + \"\\n\\n\"\n\tout += \"### [Go back to r/docs](/r/docs/home)\"\n\n\treturn out\n}\n\n// renderConfigItem shows details for a single item, e.g. \":item001\".\nfunc renderConfigItem(itemName string) string {\n\tvalue := items.Get(itemName)\n\tif value == nil {\n\t\treturn ufmt.Sprintf(\"**No item found** for key: %s\", itemName)\n\t}\n\n\tout := ufmt.Sprintf(\"# Item %s\\n\\n%s\\n\\n\", itemName, value.(string))\n\tout += \"[Go back](/r/docs/avl_pager_params)\"\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"K0mzgj6Ju8/+1zuZ1HhK/1vmlfFjcgiiX2NugGX9xSCOLA88L0vKykpEpEiu29tDlVKB6hXYuk3fCJALdboXDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"buttons","path":"gno.land/r/docs/buttons","files":[{"name":"buttons.gno","body":"package buttons\n\nimport (\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\tmotd       = \"The Initial Message\\n\\n\"\n\tlastCaller address\n)\n\nfunc UpdateMOTD(cur realm, newmotd string) {\n\tmotd = newmotd\n\tlastCaller = cur.Previous().Address()\n}\n\nfunc Render(path string) string {\n\tif path == \"motd\" {\n\t\tout := \"# Message of the Day:\\n\\n\"\n\t\tout += \"---\\n\\n\"\n\t\tout += \"# \" + motd + \"\\n\\n\"\n\t\tout += \"---\\n\\n\"\n\t\tlink := txlink.Call(\"UpdateMOTD\", \"newmotd\", \"Message!\") // \"/r/docs/buttons$help\u0026func=UpdateMOTD\u0026newmotd=Message!\"\n\t\tout += ufmt.Sprintf(\"Click **[here](%s)** to update the Message of The Day!\\n\\n\", link)\n\t\tout += \"[Go back to home page](/r/docs/buttons)\\n\\n\"\n\t\tout += \"Last updated by \" + lastCaller.String()\n\n\t\treturn out\n\t}\n\n\tout := `# Buttons\n\nUsers can create simple hyperlink buttons to view specific realm pages and\ndo specific realm actions, such as calling a specific function with some arguments.\n\nThe foundation for this functionality are markdown links; for example, you can\nclick...\n` + \"\\n## [here](/r/docs/buttons:motd)\\n\" + `...to view this realm's message of the day.`\n\n\treturn out\n}\n"},{"name":"buttons_test.gno","body":"package buttons\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestRenderMotdLink(t *testing.T) {\n\tres := Render(\"motd\")\n\tconst wantLink = \"/r/docs/buttons$help\u0026func=UpdateMOTD\u0026newmotd=Message%21\"\n\tif !strings.Contains(res, wantLink) {\n\t\tt.Fatalf(\"%s\\ndoes not contain correct help page link: %s\", res, wantLink)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/buttons\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"SBmxmgcCGhkIFFoJNl4LBSi7SeQisqhdCya0RLpakcVzW6HTEyrMqVtTX+cy9B17qW5XLqUuRCwQWKAUdTj9DQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"charts","path":"gno.land/r/docs/charts","files":[{"name":"charts.gno","body":"package charts\n\nfunc Render(path string) string {\n\t// Handle subroutes for specific chart types\n\tswitch path {\n\tcase \"piechart\":\n\t\treturn RenderPieChart()\n\tcase \"gauge\":\n\t\treturn RenderGauge()\n\tdefault:\n\t\treturn RenderMainDocs()\n\t}\n}\n\n// RenderMainDocs renders the main charts documentation page\nfunc RenderMainDocs() string {\n\t// Generate live examples\n\tpieChartExample := RenderPieChartExample()\n\tgaugeExample := RenderGaugeExample()\n\n\treturn `\n# Chart Packages Documentation\n\n## Available Packages\n\n- **[Piechart](/p/samcrew/piechart)** - SVG Pie Charts\n  - [View All Examples](/r/docs/charts:piechart) - Detailed documentation with multiple examples\n- **[Gauge](/p/samcrew/gauge)** - SVG Gauge Charts  \n  - [View All Examples](/r/docs/charts:gauge) - Detailed documentation with multiple examples\n\n## Examples\n\n### [Piechart](/p/samcrew/piechart) - SVG Pie Charts\nGenerate pie charts with label as SVG images .\n\n` + pieChartExample + `\n\n[→ View all Piechart examples](/r/docs/charts:piechart)\n\n### [Gauge](/p/samcrew/gauge) - SVG Gauge Charts  \nGenerate progress bars and gauges as SVG images with customizable styling and labels.\n\n` + gaugeExample + `\n\n[→ View all Gauge examples](/r/docs/charts:gauge)\n\n---\n\n*For complete API documentation, visit the individual package pages.*\n`\n}\n"},{"name":"gauge.gno","body":"package charts\n\nimport (\n\t\"gno.land/p/samcrew/gauge\"\n)\n\n// RenderGaugeExample creates a simple gauge bar example\nfunc RenderGaugeExample() string {\n\treturn gauge.Render(75, 100, \"Progress\", \"#2ecc71\", gauge.DefaultConfig)\n}\n\n// Render renders the gauge documentation page\nfunc RenderGauge() string {\n\t// Multiple gauge examples\n\tbasicExample := RenderGaugeExample()\n\n\t// Custom config example\n\tcustomConfig := gauge.Config{\n\t\tPercentOnly:  true,\n\t\tWidth:        400,\n\t\tCanvasHeight: 35,\n\t\tFontSize:     14,\n\t\tPaddingH:     8,\n\t}\n\tcustomExample := gauge.Render(90, 100, \"Loading\", \"#3498db\", customConfig)\n\n\t// Different progress example\n\tprogressExample := gauge.Render(33, 50, \"Health\", \"#e74c3c\", gauge.DefaultConfig)\n\n\treturn `\n# [Gauge](/p/samcrew/gauge) Documentation\n\nGenerate progress bars and gauges as SVG images with customizable styling and labels.\n\n## Basic Usage\n\n` + \"```go\" + `\nimport \"gno.land/p/samcrew/gauge\"\n\n// Basic gauge with default config\ngauge := gauge.Render(75, 100, \"Progress\", \"#2ecc71\", gauge.DefaultConfig)\n` + \"```\" + `\n\n` + basicExample + `\n\n## Custom Configuration Example\n\n` + \"```go\" + `\nconfig := gauge.Config{\n    PercentOnly:  true, // Show only percentage\n    Width:        400,  // Custom width in pixels\n    CanvasHeight: 35,   // Custom height in pixels\n    FontSize:     14,   // Font size in pixels\n    PaddingH:     8,    // Horizontal padding in pixels\n}\ngauge := gauge.Render(90, 100, \"Loading\", \"#3498db\", config)\n` + \"```\" + `\n\n` + customExample + `\n\n## Different Progress Example\n\n` + \"```go\" + `\nhealthGauge := gauge.Render(33, 50, \"Health\", \"#e74c3c\", gauge.DefaultConfig)\n` + \"```\" + `\n\n` + progressExample + `\n\n## API Reference\n\n` + \"```go\" + `\ntype Config struct {\n    PercentOnly  bool // Only display the percentage on the right side\n    Width        int  // Width of the gauge in pixels\n    CanvasHeight int  // Height of the gauge in pixels\n    FontSize     int  // Font size of the text in pixels\n    PaddingH     int  // Horizontal padding (for the text) in pixels\n}\n\nvar DefaultConfig = Config{\n    PercentOnly:  false,\n    Width:        300,\n    CanvasHeight: 30,\n    FontSize:     16,\n    PaddingH:     6,\n}\n\n// value: Current value (must be ≤ total)\n// total: Maximum value (must be \u003e 0)\n// label: Text label displayed on the left\n// color: Fill color (hex format, e.g., \"#4caf50\")\n// config: Configuration options\n// Returns: SVG string as markdown image\nfunc Render(value int, total int, label string, color string, config Config) string\n` + \"```\" + `\n---\n\n[← Back to Charts](/r/docs/charts) | [View Package](/p/samcrew/gauge)\n`\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/charts\"\ngno = \"0.9\"\n"},{"name":"piechart.gno","body":"package charts\n\nimport (\n\t\"gno.land/p/samcrew/piechart\"\n)\n\n// RenderPieChartExample creates a simple data distribution pie chart example\nfunc RenderPieChartExample() string {\n\tdata := []piechart.PieSlice{\n\t\t{Value: 35, Color: \"#e74c3c\", Label: \"Category A\"},\n\t\t{Value: 25, Color: \"#3498db\", Label: \"Category B\"},\n\t\t{Value: 20, Color: \"#2ecc71\", Label: \"Category C\"},\n\t\t{Value: 15, Color: \"#f39c12\", Label: \"Category D\"},\n\t\t{Value: 5, Color: \"#9b59b6\", Label: \"Category E\"},\n\t}\n\treturn piechart.Render(data, \"Data Distribution Example\")\n}\n\n// Render renders the piechart documentation page\nfunc RenderPieChart() string {\n\t// Multiple piechart examples\n\tbasicExample := RenderPieChartExample()\n\n\t// Custom example with different data\n\tcustomData := []piechart.PieSlice{\n\t\t{Value: 40, Color: \"#FF6B6B\", Label: \"Red Section\"},\n\t\t{Value: 30, Color: \"#4ECDC4\", Label: \"Teal Section\"},\n\t\t{Value: 20, Color: \"#45B7D1\", Label: \"Blue Section\"},\n\t\t{Value: 10, Color: \"#96CEB4\", Label: \"Green Section\"},\n\t}\n\tcustomExample := piechart.Render(customData, \"Custom Color Example\")\n\n\t// Small dataset example\n\tsmallData := []piechart.PieSlice{\n\t\t{Value: 70, Color: \"#FF9F43\", Label: \"Majority\"},\n\t\t{Value: 30, Color: \"#686DE0\", Label: \"Minority\"},\n\t}\n\tsmallExample := piechart.Render(smallData, \"Simple Split\")\n\n\treturn `\n# [Piechart](/p/samcrew/piechart) Documentation\n\nGenerate pie charts with label as SVG images.\n\n## Basic Usage\n\n` + \"```go\" + `\nimport \"gno.land/p/samcrew/piechart\"\n\ndata := []piechart.PieSlice{\n    {Value: 35, Color: \"#e74c3c\", Label: \"Category A\"},\n    {Value: 25, Color: \"#3498db\", Label: \"Category B\"},\n    {Value: 20, Color: \"#2ecc71\", Label: \"Category C\"},\n    {Value: 15, Color: \"#f39c12\", Label: \"Category D\"},\n    {Value: 5, Color: \"#9b59b6\", Label: \"Category E\"},\n}\nchart := piechart.Render(data, \"Data Distribution Example\")\n` + \"```\" + `\n\n` + basicExample + `\n\n---\n\n## Custom Colors Example\n\n` + \"```go\" + `\ncustomData := []piechart.PieSlice{\n    {Value: 40, Color: \"#FF6B6B\", Label: \"Red Section\"},\n    {Value: 30, Color: \"#4ECDC4\", Label: \"Teal Section\"},\n    {Value: 20, Color: \"#45B7D1\", Label: \"Blue Section\"},\n    {Value: 10, Color: \"#96CEB4\", Label: \"Green Section\"},\n}\nchart := piechart.Render(customData, \"Custom Color Example\")\n` + \"```\" + `\n\n` + customExample + `\n\n---\n\n## Simple Split Example\n\n` + \"```go\" + `\nsimpleData := []piechart.PieSlice{\n    {Value: 70, Color: \"#FF9F43\", Label: \"Majority\"},\n    {Value: 30, Color: \"#686DE0\", Label: \"Minority\"},\n}\nchart := piechart.Render(simpleData, \"Simple Split\")\n` + \"```\" + `\n\n` + smallExample + `\n\n## API Reference\n\n` + \"```go\" + `\ntype PieSlice struct {\n    Value float64 // Numeric value for the slice\n    Color string  // Hex color code (e.g., \"#ff6b6b\")\n    Label string  // Display label for the slice\n}\n\n// slices: Array of PieSlice structs containing the data\n// title: Chart title (empty string for no title)\n// Returns: SVG markup as a string\nfunc Render(slices []PieSlice, title string) string\n` + \"```\" + `\n\n---\n\n[← Back to Charts](/r/docs/charts) | [View Package](/p/samcrew/piechart)\n`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"tV0sN68W2YR/ihHMmJQq4WbOF1IMaGYUS+2ou0kPdnYHQgo2iZ3YK/p6iE2gfTjrZQcDuHWeUr37Wo4f4moXDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"complexargs","path":"gno.land/r/docs/complexargs","files":[{"name":"complexargs.gno","body":"package complexargs\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\n// Some example complex types, ie a slice, and a custom type\nvar (\n\tslice    = []int{1, 2, 3}\n\tmyObject = \u0026CustomType{\n\t\tName:    \"Alice\",\n\t\tNumbers: []int{4, 5, 6},\n\t}\n)\n\ntype CustomType struct {\n\tName    string\n\tNumbers []int\n}\n\n// SetSlice takes a complex argument that must be called using MsgRun or from another contract that imports this one.\nfunc SetSlice(_ realm, newSlice []int) {\n\tslice = newSlice\n}\n\n// SetMyObject takes a complex argument that must be called using MsgRun or from another contract that imports this one.\nfunc SetMyObject(_ realm, newCoolObject CustomType) {\n\tmyObject = \u0026newCoolObject\n}\n\nfunc Render(_ string) string {\n\tout := \"# Complex argument functions\\n\\n\"\n\tout += `Exposed realm functions and methods that take in complex arguments, such as slices, structs, pointers, etc,\ncannot be called via a standard \"MsgCall\" transaction. To call these functions, users need to use \"MsgRun\".\n\nCheck out the source code to see example functions and objects.\n\nIn this case, the following \"MsgRun\" code would be used to call the function:\n`\n\tout += ufmt.Sprintf(\"```go\\n%s\\n```\\n\\n\", msgrun)\n\n\tout += \"Value of int slice: \"\n\tfor i, v := range slice {\n\t\tif i \u003e 0 {\n\t\t\tout += \", \"\n\t\t}\n\t\tout += ufmt.Sprintf(\"%d\", v)\n\t}\n\tout += \"\\n\\n\"\n\n\tif myObject != nil {\n\t\ts := \"\"\n\t\tfor i, v := range myObject.Numbers {\n\t\t\tif i \u003e 0 {\n\t\t\t\ts += \",\"\n\t\t\t}\n\t\t\ts += ufmt.Sprintf(\"%d\", v)\n\t\t}\n\t\tout += ufmt.Sprintf(\"Value of myObject: `CustomObject{Name: %s, Numbers: %s}`\", myObject.Name, s)\n\t}\n\n\tout = strings.ReplaceAll(out, \"\\\"\", \"`\")\n\treturn out\n}\n\nconst msgrun = `package main\n\n// Import the realm you want to call\nimport \"gno.land/r/docs/complexargs\"\n\nfunc main(cur realm) {\n\t// Create the complex arguments to pass:\n\tslice := []int{1, 2, 3}\n\t// Call the function\n\tcomplexargs.SetSlice(cross(cur), slice)\n\n\t// The same can be done with custom types:\n\tobj := complexargs.CustomType{Name: \"whatever\", Numbers: []int{1, 10, 100}}\n\tcomplexargs.SetMyObject(obj)\n}`\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/complexargs\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"mnqKBFiqq2r9aJ6GGzicNltCCYSYHYKLJfNkcsweHNyfw3K7Unc1E9nwOd9nVR7X1addmjKEbQg5w84/hx2rAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"counter","path":"gno.land/r/docs/composition/counter","files":[{"name":"counter.gno","body":"// Package counter is a sub-component of /r/docs/composition.\n// It holds a counter that only the composition realm can change.\npackage counter\n\nimport (\n\t\"strconv\"\n)\n\nvar count int64\n\n// allowedCaller is the only realm allowed to modify the counter\nconst allowedCaller = \"gno.land/r/docs/composition\"\n\n// Increment increases the counter by 1 (restricted to composition realm)\nfunc Increment(cur realm) {\n\tassertAuthorized(cur)\n\tcount++\n}\n\n// Decrement decreases the counter by 1 (restricted to composition realm)\nfunc Decrement(cur realm) {\n\tassertAuthorized(cur)\n\tcount--\n}\n\n// Reset sets the counter back to zero (restricted to composition realm)\nfunc Reset(cur realm) {\n\tassertAuthorized(cur)\n\tcount = 0\n}\n\n// assertAuthorized ensures only the composition realm can modify state\nfunc assertAuthorized(cur realm) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous()\n\tif caller.PkgPath() != allowedCaller {\n\t\tpanic(\"unauthorized: only \" + allowedCaller + \" can modify counter\")\n\t}\n}\n\n// GetCountStr returns the current count as a string\nfunc GetCountStr() string {\n\treturn strconv.FormatInt(count, 10)\n}\n\n// RenderValue returns a markdown snippet showing the current value\nfunc RenderValue() string {\n\treturn \"**\" + strconv.FormatInt(count, 10) + \"**\"\n}\n\nfunc Render(_ string) string {\n\treturn \"# Counter Component\\n\\nCurrent value: \" + RenderValue() + \"\\n\\nThis is a sub-component of [/r/docs/composition](/r/docs/composition).\"\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/composition/counter\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Zb5FUEUgG0jxixrRqU+67vYfugFLF/s/cQ8cSG0CcXxYkG4PhIfbGUcp61mOcuWignkmm7/xFdoXREg+YVkXAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"logger","path":"gno.land/r/docs/composition/logger","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/composition/logger\"\ngno = \"0.9\"\n"},{"name":"logger.gno","body":"// Package logger is a sub-component of /r/docs/composition.\n// It holds an append-only log that only the composition realm can change.\npackage logger\n\nimport (\n\t\"time\"\n)\n\nvar logs []string\n\n// allowedCaller is the only realm allowed to modify the logger\nconst allowedCaller = \"gno.land/r/docs/composition\"\n\n// Log adds a new entry to the log (restricted to composition realm)\nfunc Log(cur realm, message string) {\n\tassertAuthorized(cur)\n\ttimestamp := time.Now().Format(\"15:04:05\")\n\tlogs = append(logs, \"[\"+timestamp+\"] \"+message)\n}\n\n// Clear removes all log entries (restricted to composition realm)\nfunc Clear(cur realm) {\n\tassertAuthorized(cur)\n\tlogs = nil\n}\n\n// assertAuthorized ensures only the composition realm can modify state\nfunc assertAuthorized(cur realm) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous()\n\tif caller.PkgPath() != allowedCaller {\n\t\tpanic(\"unauthorized: only \" + allowedCaller + \" can modify logger\")\n\t}\n}\n\n// RenderLog returns a markdown snippet showing the log entries\nfunc RenderLog() string {\n\tif len(logs) == 0 {\n\t\treturn \"_No activity logged yet._\"\n\t}\n\n\tout := \"\"\n\t// Show last 10 entries, most recent first\n\tstart := len(logs) - 10\n\tif start \u003c 0 {\n\t\tstart = 0\n\t}\n\tfor i := len(logs) - 1; i \u003e= start; i-- {\n\t\tout += \"- \" + logs[i] + \"\\n\"\n\t}\n\treturn out\n}\n\nfunc Render(_ string) string {\n\treturn \"# Logger Component\\n\\n\" + RenderLog() + \"\\n\\nThis is a sub-component of [/r/docs/composition](/r/docs/composition).\"\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"GyPdLgcvnEbL3n4IG3XquYYmg25b0Ib2TN2k1+g+YHyQpLuehqP7tlu1P72zLjN07yxz3G/ewsYYoWpXLVaYCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"composition","path":"gno.land/r/docs/composition","files":[{"name":"composition.gno","body":"// Package composition shows how to build a realm from smaller component realms.\n// It calls into the counter and logger sub-realms.\n// Each sub-realm restricts writes to this composition realm.\npackage composition\n\nimport (\n\t\"gno.land/r/docs/composition/counter\"\n\t\"gno.land/r/docs/composition/logger\"\n)\n\n// This realm demonstrates composition by combining functionality\n// from two sub-realms: a counter and a logger.\n\n// IncrementAndLog increments the counter and logs the action\nfunc IncrementAndLog(cur realm) {\n\tcounter.Increment(cross(cur))\n\tlogger.Log(cross(cur), \"Counter incremented to \"+counter.GetCountStr())\n}\n\n// DecrementAndLog decrements the counter and logs the action\nfunc DecrementAndLog(cur realm) {\n\tcounter.Decrement(cross(cur))\n\tlogger.Log(cross(cur), \"Counter decremented to \"+counter.GetCountStr())\n}\n\n// Reset resets the counter and clears logs\nfunc Reset(cur realm) {\n\tcounter.Reset(cross(cur))\n\tlogger.Clear(cross(cur))\n\tlogger.Log(cross(cur), \"System reset\")\n}\n\nfunc Render(_ string) string {\n\treturn `# Realm Composition\n\nAllows you to build complex realms by combining simpler ones,\nfollowing the \"separation of concerns\" principle.\n\n## Code Pattern\n\n` + \"```go\" + `\npackage mycomposite\n\nimport (\n    \"gno.land/r/component/a\"\n    \"gno.land/r/component/b\"\n)\n\nfunc ComposedAction(cur realm) {\n    // Pass cross(cur) when calling external realms\n    a.DoSomething(cross(cur))\n    b.DoSomethingElse(cross(cur))\n}\n` + \"```\" + `\n\n## Access Control Pattern\n\nTo prevent users from bypassing the composition realm and calling components directly,\neach component checks that the caller is the authorized composition realm:\n\n` + \"```go\" + `\n// In component realm\nconst allowedCaller = \"gno.land/r/my/composite\"\n\nfunc ModifyState(cur realm) {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    caller := cur.Previous()\n    if caller.PkgPath() != allowedCaller {\n        panic(\"unauthorized\")\n    }\n    // ... modify state\n}\n` + \"```\" + `\n\n**Alternative**: If components don't need their own persistent state,\nuse pure packages (` + \"`/p/...`\" + `) instead of realms (` + \"`/r/...`\" + `).\nPackages are stateless libraries that can only be imported, so all state\nlives in the parent realm, eliminating bypass concerns entirely.\n\n## This Example\n\nThis realm combines a counter and a logger. Each action increments or decrements\nthe counter while automatically logging the change.\n\nThe counter and logger can only be modified through this composition realm.\nDirect calls to ` + \"`counter.Increment()`\" + ` or ` + \"`logger.Log()`\" + ` will fail with an authorization error.\n\n## Components\n\n- **Counter** ([source](/r/docs/composition/counter))\n- **Logger** ([source](/r/docs/composition/logger))\n\n## Live Demo\n\n### Current Counter Value\n\n` + counter.RenderValue() + `\n\n### Activity Log\n\n` + logger.RenderLog() + `\n\n### Actions\n\n- [Increment and Log](/r/docs/composition$help\u0026func=IncrementAndLog)\n- [Decrement and Log](/r/docs/composition$help\u0026func=DecrementAndLog)\n- [Reset All](/r/docs/composition$help\u0026func=Reset)\n`\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/composition\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"XKIpxcpJjXhfuPBSnOeDzPCbMzumjvg0S3Z44zMWtcSIm6UTscrol+Sd7Hn/8Rm9YyVHu93kAskR9ysB0X1JAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"crossing","path":"gno.land/r/docs/crossing","files":[{"name":"crossing.gno","body":"// Package crossing explains Gno's crossing mechanism.\n// Crossing tracks which realm is acting, and cross-calls move it from one realm to another.\npackage crossing\n\nimport (\n\t\"chain/runtime/unsafe\"\n)\n\nvar owner address\n\nfunc init(cur realm) {\n\t// init runs automatically only once on deployment. It can take a \"cur realm\"\n\t// handle, so \"owner\" is set from cur.Address(), the address of the realm\n\t// being deployed.\n\towner = cur.Address()\n}\n\n// PublicFunction's first parameter is a \"cur realm\", so the call crosses into\n// this realm. cur.Previous() is the idiomatic way to read the caller, but only\n// after cur.IsCurrent() confirms the handle is this frame's live realm.\nfunc PublicFunction(cur realm) string {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous().Address()\n\treturn \"Called by: \" + caller.String()\n}\n\n// InternalHelper has no realm parameter, so the call does not cross. It runs\n// in the caller's context, like a pure package helper.\nfunc InternalHelper() string {\n\tcaller := unsafe.CurrentRealm().Address()\n\treturn \"I'm a helper function called by: \" + caller.String()\n}\n\n// InternalHelperWithRealm takes a realm but doesn't cross: only a first-position\n// realm crosses, so rlm is a plain value here. It lets shared code that can't be\n// a crossing function (e.g. a /p/ package) receive and verify the acting realm.\nfunc InternalHelperWithRealm(_ int, rlm realm) string {\n\t// The call didn't cross, so IsCurrent() holds for the caller's live realm\n\t// and rejects any other.\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"rlm is not the current realm\")\n\t}\n\n\treturn \"I'm a helper function called by: \" + rlm.Address().String()\n}\n\n// GetOwner returns the owner address\nfunc GetOwner() address {\n\treturn owner\n}\n\nfunc Render(_ string) string {\n\treturn `# Crossing in Gno\n\nCrossing is how Gno tracks which realm is acting at any moment. Every realm is a\nsmart contract with its own storage, and a realm can only write to its own. When\none realm calls into another, the acting realm switches to the one being called.\nGno follows that switch so each change is saved to the realm that owns it.\n\n## What is Crossing?\n\nWhen Realm A calls a function that lives in Realm B, execution \"crosses\" from A\ninto B, and B becomes the realm that is acting. Inside that function, changes\nare saved to B's storage. Once it returns, the acting realm goes back to A.\n\nGno follows this chain of calls, so at any point the code can ask:\n\n- **Who started the transaction?** → ` + \"`unsafe.OriginCaller()`\" + `\n- **Who called me?** → ` + \"`unsafe.PreviousRealm()`\" + `\n- **Where am I?** → ` + \"`unsafe.CurrentRealm()`\" + `\n- **Whose state is being modified?** → Always the current realm's state\n\nTo let other realms cross into a function, give it a ` + \"`cur realm`\" + ` parameter.\n` + \"`cur`\" + ` is a handle to this realm's own context while the function runs:\n\n` + \"```go\" + `\nfunc ModifyState(cur realm, newValue string) {\n    state = newValue  // Modifies THIS realm's state\n}\n` + \"```\" + `\n\nThe parameter alone does not cross. The caller crosses by marking the call with\n` + \"`cross`\" + `, passing its own ` + \"`cur`\" + `:\n\n` + \"```go\" + `\nother.ModifyState(cross(cur), \"hello\")  // crosses into the \"other\" realm\n` + \"```\" + `\n\nCalling another realm's function without ` + \"`cross`\" + ` is rejected. The plain\n` + \"`fn(cur, ...)`\" + ` form is only for calls within the same realm, where nothing crosses.\n\n## The Call Chain\n\nWhen User → Realm A → Realm B → Realm C:\n\n` + \"```\" + `\n User Transaction                                            \n                                                             \n  User (EOA)                                                 \n    │                                                        \n    ▼ call                                                   \n  ┌─────────────┐  OriginCaller() = User                     \n  │   Realm A            │  PreviousRealm() = User (as realm)         \n  │                      │  CurrentRealm() = Realm A                  \n  └─────────────┘                                            \n    │                                                        \n    ▼ cross                                                  \n  ┌─────────────┐  OriginCaller() = User                     \n  │   Realm B            │  PreviousRealm() = Realm A                 \n  │                      │  CurrentRealm() = Realm B                  \n  └─────────────┘               \n    │                                                                                  \n    ▼ cross                                                  \n  ┌─────────────┐  OriginCaller() = User                     \n  │   Realm C            │  PreviousRealm() = Realm B                 \n  │                      │  CurrentRealm() = Realm C                  \n  └─────────────┘                                            \n                                                             \n` + \"```\" + `\n\n\n## Crossing in Packages (/p/)\n\nUnlike realms, packages (under ` + \"`/p/`\" + `) don't have their own persistent state.\nWhen a realm imports a package, any variables declared by that package are\nstored within the realm's state. This means each realm gets its\nown isolated instance of the package's data.\n\n## Crossing in [Tests](https://docs.gno.land/resources/gno-stdlibs/#testing)\n\n` + \"```go\" + `\nimport (\n    \"testing\"\n\n    \"gno.land/p/nt/testutils/v0\"\n)\n\nfunc TestWithCaller(t *testing.T) {\n    // Simulate PreviousRealm() being another realm\n    testing.SetRealm(testing.NewCodeRealm(\"gno.land/r/caller\"))\n\n    // Simulate OriginCaller() being an end user\n    testing.SetOriginCaller(testutils.TestAddress(\"user1\"))\n}\n` + \"```\" + `\n\n---\n\n### See also:\n- [Common Mistakes](/r/docs/mistakes) - OriginCaller vs cur.Previous()\n`\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/crossing\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"19H/gINY01bIim76D7FOYg1fgp+FlysxlmYqkjuxom7KPxotTdzvKwSw30w4HEliLivNT3OA6600AlQgqI8oCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"events","path":"gno.land/r/docs/events","files":[{"name":"events.gno","body":"package events\n\nimport \"chain\"\n\nfunc Render(_ string) string {\n\tout := \"# Emitting events in Gno\\n\\n\"\n\n\tout += `Emitting events in blockchain systems is one of the ways to make off-chain life easier.\nTo emit an event, simply use the **Emit()** function found in the **std** package.\n\nThis function takes in string arguments as follows:\n\n`\n\tout += \"```\\nruntime.Emit(\\\"EventName\\\", \\\"key1\\\", \\\"value1\\\", \\\"key2\\\", \\\"value2\\\")\\n```\\n\"\n\tout += \"The function takes in a variadic number of key:value pairs after the event name.\\n\\n\"\n\tout += \"Events are stored in block results, and can be listened to via the [tx-indexer](https://github.com/gnolang/tx-indexer).\\n\\n\"\n\n\tout += \"Click [here](/r/docs/events$help\u0026func=Emit\u0026customEventValue=) to submit a transaction that will emit an event.\"\n\n\treturn out\n}\n\nfunc Emit(customEventValue string) {\n\tchain.Emit(\"ExampleEvent\", \"key\", customEventValue)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/events\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"fCz2csBYcZhtq/37i5ve7ujLiim6vHUer8SrE3nVUxiMIXZdVhqTBeyPAeaGw0IkuX96MkBiTmlg81uQiLpgAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"factory","path":"gno.land/r/docs/factory","files":[{"name":"factory.gno","body":"// Package factory demonstrates the factory pattern.\n// It creates different token types (fungible, non-fungible, soulbound) behind a single Token interface.\npackage factory\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n)\n\n// TokenType represents different types of tokens\ntype TokenType int\n\nconst (\n\tFungible TokenType = iota\n\tNonFungible\n\tSoulBound\n)\n\n// Token is the common interface for all token types\ntype Token interface {\n\tGetInfo() string\n\tGetType() string\n\tTransfer(to address, amount uint64) error\n}\n\n// FungibleToken implements divisible tokens (like coins)\ntype FungibleToken struct {\n\tname   string\n\tamount uint64\n\towner  address\n}\n\nfunc (f *FungibleToken) GetInfo() string {\n\treturn f.name + \": \" + strconv.FormatUint(f.amount, 10) + \" units\"\n}\n\nfunc (f *FungibleToken) GetType() string {\n\treturn \"Fungible\"\n}\n\nfunc (f *FungibleToken) Transfer(to address, amount uint64) error {\n\tif amount \u003e f.amount {\n\t\treturn errors.New(\"insufficient balance\")\n\t}\n\tf.amount -= amount\n\treturn nil\n}\n\n// NonFungibleToken implements unique tokens (like NFTs)\ntype NonFungibleToken struct {\n\tname  string\n\towner address\n}\n\nfunc (n *NonFungibleToken) GetInfo() string {\n\treturn \"NFT: \" + n.name\n}\n\nfunc (n *NonFungibleToken) GetType() string {\n\treturn \"Non-Fungible\"\n}\n\nfunc (n *NonFungibleToken) Transfer(to address, amount uint64) error {\n\tif amount != 1 {\n\t\treturn errors.New(\"NFTs can only be transferred as a whole (amount must be 1)\")\n\t}\n\tn.owner = to\n\treturn nil\n}\n\n// SoulBoundToken implements non-transferable tokens\ntype SoulBoundToken struct {\n\tname  string\n\towner address\n}\n\nfunc (s *SoulBoundToken) GetInfo() string {\n\treturn \"SoulBound: \" + s.name\n}\n\nfunc (s *SoulBoundToken) GetType() string {\n\treturn \"SoulBound\"\n}\n\nfunc (s *SoulBoundToken) Transfer(to address, amount uint64) error {\n\treturn errors.New(\"soulbound tokens cannot be transferred\")\n}\n\n// CreateToken is the factory function that creates different token types\nfunc CreateToken(tokenType TokenType, name string, owner address) Token {\n\tswitch tokenType {\n\tcase Fungible:\n\t\treturn \u0026FungibleToken{name: name, amount: 1000, owner: owner}\n\tcase NonFungible:\n\t\treturn \u0026NonFungibleToken{name: name, owner: owner}\n\tcase SoulBound:\n\t\treturn \u0026SoulBoundToken{name: name, owner: owner}\n\tdefault:\n\t\tpanic(\"unknown token type\")\n\t}\n}\n\n// Demo variables\nvar (\n\tlastToken Token\n)\n\n// CreateFungibleToken creates a fungible token\nfunc CreateFungibleToken(cur realm, name string) string {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous().Address()\n\ttoken := CreateToken(Fungible, name, caller)\n\tlastToken = token\n\treturn \"Created: \" + token.GetInfo()\n}\n\n// CreateNFT creates a non-fungible token\nfunc CreateNFT(cur realm, name string) string {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous().Address()\n\ttoken := CreateToken(NonFungible, name, caller)\n\tlastToken = token\n\treturn \"Created: \" + token.GetInfo()\n}\n\n// CreateSoulBound creates a soul-bound token\nfunc CreateSoulBound(cur realm, name string) string {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous().Address()\n\ttoken := CreateToken(SoulBound, name, caller)\n\tlastToken = token\n\treturn \"Created: \" + token.GetInfo()\n}\n\nfunc Render(path string) string {\n\tout := `# Factory Pattern\n\nCreates different types of objects that share a common interface.\n\n## Code Pattern\n\n` + \"```go\" + `\n// Common interface\ntype Token interface {\n    GetInfo() string\n    GetType() string\n    Transfer(to address, amount uint64) error\n}\n\n// Concrete types\ntype FungibleToken struct { name string; amount uint64 }\ntype NonFungibleToken struct { name string }\ntype SoulBoundToken struct { name string }\n\n// Factory function\nfunc CreateToken(tokenType TokenType, name string) Token {\n    switch tokenType {\n    case Fungible:\n        return \u0026FungibleToken{name: name, amount: 1000}\n    case NonFungible:\n        return \u0026NonFungibleToken{name: name}\n    case SoulBound:\n        return \u0026SoulBoundToken{name: name}\n    default:\n        panic(\"unknown token type\")\n    }\n}\n\n// Work with interface, not concrete types\ntoken := CreateToken(Fungible, \"USDC\")\nif err := token.Transfer(recipientAddr, 100); err != nil {\n    panic(err)\n}\n` + \"```\" + `\n\n## Live Demo\n\n- [Create Fungible Token](/r/docs/factory$help\u0026func=CreateFungibleToken)\n- [Create NFT](/r/docs/factory$help\u0026func=CreateNFT)\n- [Create SoulBound Token](/r/docs/factory$help\u0026func=CreateSoulBound)\n\n`\n\n\tif lastToken != nil {\n\t\tout += \"### Last Created Token\\n\\n\"\n\t\tout += \"**Type:** \" + lastToken.GetType() + \"\\n\\n\"\n\t\tout += \"**Info:** \" + lastToken.GetInfo() + \"\\n\\n\"\n\t}\n\n\treturn out\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/factory\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ScBH5ZzRyHudF3Aun8z9MLiBoNeasBCLTOnvin793x5W2wWRRkikRlPhqWHZSeY2o+erKO8P7Mhn7cIVs6WnBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"hello","path":"gno.land/r/docs/hello","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/hello\"\ngno = \"0.9\"\n"},{"name":"hello.gno","body":"// Package hello_world demonstrates basic usage of Render().\n// Try adding `:World` at the end of the URL, like `.../hello:World`.\npackage hello\n\n// Render outputs a greeting. It customizes the message based on the provided path.\nfunc Render(path string) string {\n\tif path == \"\" {\n\t\treturn \"# Hello, 世界！\"\n\t}\n\treturn \"# Hello, \" + path + \"!\"\n}\n"},{"name":"hello_test.gno","body":"package hello\n\nimport (\n\t\"testing\"\n)\n\nfunc TestHello(t *testing.T) {\n\texpected := \"# Hello, 世界！\"\n\tgot := Render(\"\")\n\tif got != expected {\n\t\tt.Fatalf(\"Expected %s, got %s\", expected, got)\n\t}\n\n\tgot = Render(\"world\")\n\texpected = \"# Hello, world!\"\n\tif got != expected {\n\t\tt.Fatalf(\"Expected %s, got %s\", expected, got)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"KqWINH5xQCtS1WI13OGOmP5G2OtkBIrh1cCyFjbf8UVUTd2F3++hqn9o2bPfXD2QAFgNVz/uQIYE6f+6W+W3CQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/docs/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport \"strings\"\n\nfunc Render(_ string) string {\n\tcontent := `# Gno Examples Documentation\n\nWelcome to the Gno examples documentation index.\nExplore various examples to learn more about Gno functionality and usage.\n\n## Getting Started\n- [Hello World](/r/docs/hello) - A simple introductory example.\n- [Adder](/r/docs/adder) - An interactive example to update a number with transactions.\n- [Source](/r/docs/source) - View realm source code.\n\n## Development \u0026 Testing\n- [Common Mistakes](/r/docs/mistakes) - Pitfalls to avoid: overflow, errors, OriginCaller attacks.\n- [Crossing](/r/docs/crossing) - Understanding realm boundary crossing.\n- [Calling non-primitive argument functions](/r/docs/complexargs) - Learn how to call functions that take in complex arguments.\n- [Test-Driven Development (TDD)](/r/docs/tdd) - Writing tests before code in Gno.\n\n## Data \u0026 Serialization\n- [AVL Pager](/r/docs/avl_pager) - Paginate through AVL tree items.\n- [AVL Pager + Render paths](/r/docs/avl_pager_params) - Handle render arguments with pagination.\n\n## Design Patterns\n- [Composition](/r/docs/composition) - Composing functionality from multiple realms.\n- [Factory Pattern](/r/docs/factory) - Create and manage multiple object instances.\n- [Registry Pattern](/r/docs/registry) - Register and discover entities in a central directory.\n- [Safe Objects](/r/docs/safeobjects) - A Gno-specific pattern allowing you to expose objects with admin privileges preserved.\n- [Common Solidity Patterns](/r/docs/soliditypatterns) - A list of common Solidity coding practices, converted to Gno equivalents.\n\n## Rendering \u0026 UI\n- [Buttons](/r/docs/buttons) - Add buttons to your realm's render.\n- [Transaction Links](/r/docs/txlink) - Create clickable transaction links in your realm's render!\n- [Optional Render](/r/docs/optional_render) - Render() is optional in realms.\n- [Routing for Render paths](/r/docs/routing) - Route Render paths with the ^p/demo/mux^ package.\n- [Embed images](/r/docs/img_embed) - Demonstrates how to embed an image in a realm render.\n- [Markdown](/r/docs/markdown) - Documentation for Gno Flavored Markdown syntax and features.\n- [p/moul/md Package Demo](/r/docs/moul_md) - Learn how to programmatically generate markdown using the p/moul/md package.\n- [Chart Packages Demo](/r/docs/charts) - Create charts to display data in your realm's render.\n\n## Users \u0026 Profiles\n- [Resolving usernames and addresses](/r/docs/resolveusers) - How to resolve usernames and addresses via the ^r/sys/users^ realm.\n- [User \u0026 Profile Integration](/r/docs/userprofile) - Using ^r/sys/users^ and ^r/demo/profile^ together.\n\n## Advanced Topics\n- [Emitting Gno Events](/r/docs/events) - Emit Gno Events to make life off-chain easier.\n- [Private Realms](/r/docs/private) - Documentation for private realms in Gno.\n- [MiniSocial](/r/docs/minisocial) - Minimalistic social media app for learning purposes.\n\n\u003c!-- meta issue with suggestions: https://github.com/gnolang/gno/issues/3292 --\u003e\n\n## Other resources\n\n- [Official documentation](https://docs.gno.land) \u003c!-- should be /docs with gnoweb embedding the docs/ folder. --\u003e\n`\n\treturn strings.ReplaceAll(content, \"^\", \"`\")\n}\n"},{"name":"home_test.gno","body":"package home\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestRenderHome(t *testing.T) {\n\toutput := Render(\"\")\n\n\t// Check for the presence of key sections\n\tif !contains(output, \"# Gno Examples Documentation\") {\n\t\tt.Errorf(\"Render output is missing the title.\")\n\t}\n\tif !contains(output, \"Official documentation\") {\n\t\tt.Errorf(\"Render output is missing the official documentation link.\")\n\t}\n}\n\nfunc contains(s, substr string) bool {\n\treturn strings.Index(s, substr) \u003e= 0\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"M6xRA5LogK8uzlHqP06qMjPhUrlHRbEGeqPktIBLm6ArDA4Wlhwbc6yqfXnmCLAdiq+iLjE7eAkBL8IoyhS+CA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"img_embed","path":"gno.land/r/docs/img_embed","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/img_embed\"\ngno = \"0.9\"\n"},{"name":"img_embed.gno","body":"package img_embed\n\n// Render displays a title and an embedded image from Imgur\nfunc Render(path string) string {\n\treturn `# Image Embed Example\n\nHere’s an example of embedding an image in a Gno realm:\n\n![Example Image](https://i.imgur.com/So4rBPB.jpeg)\n\nOnly a limited set of content providers is supported. Check the list out [here](/r/docs/markdown#images).`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ZcBa/tQZUp+DGpCrde6Ure/ghbVJshhI2c8StKXrDgzDo5WKiP9MiKz0yEumyH787vLcL+HUTOqvGPjuzK0yCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"markdown","path":"gno.land/r/docs/markdown","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/markdown\"\ngno = \"0.9\"\n"},{"name":"markdown.gno","body":"package markdown\n\nimport \"strings\"\n\n// this package can be used to test markdown rendering engines.\n\nfunc Render(path string) string {\n\toutput := `# Markdown on Gno\n\n## Introduction\n\nMarkdown on Gno is based on standard markdown, but also has some unique features, making it the Gno Flavored Markdown. This document describes the current markdown support in Gno, demonstrating both the syntax and its rendered output.\n\n\u003e [!NOTE]\n\u003e Markdown support in Gno is still evolving. New features and improvements will be added in future releases.\n\n## Basic Syntax\n\n### Headings\n\nHeadings are created using hash symbols (#). The number of hash symbols indicates the heading level.\n\n±±±markdown\n# Heading 1\n## Heading 2\n### Heading 3\n#### Heading 4\n##### Heading 5\n###### Heading 6\n±±±\n\n# Heading 1\n## Heading 2\n### Heading 3\n#### Heading 4\n##### Heading 5\n###### Heading 6\n\n### Text Formatting\n\nYou can format text using the following syntax:\n\n±±±markdown\n**Bold text**\n*Italic text*\n~~Strikethrough text~~\n**Bold and _nested italic_**\n***All bold and italic***\n±±±\n\n**Bold text**\n*Italic text*\n~~Strikethrough text~~\n**Bold and _nested italic_**\n***All bold and italic***\n\n### Links\n\nLinks can be created using the following syntax:\n\n±±±markdown\n[Link text](https://example.com)\n[Link with title](https://example.com \"Link title\")\n±±±\n\n[Link text](https://example.com)\n[Link with title](https://example.com \"Link title\")\n\n### Lists\n\nUnordered lists use asterisks, plus signs, or hyphens:\n\n±±±markdown\n* Item 1\n* Item 2\n  * Nested item 1\n  * Nested item 2\n±±±\n\n* Item 1\n* Item 2\n  * Nested item 1\n  * Nested item 2\n\nOrdered lists use numbers:\n\n±±±markdown\n1. First item\n2. Second item\n   1. Nested item 1\n   2. Nested item 2\n±±±\n\n1. First item\n2. Second item\n   1. Nested item 1\n   2. Nested item 2\n\n### Blockquotes\n\nBlockquotes are created using the \u003e character:\n\n±±±markdown\n\u003e This is a blockquote\n\u003e\n\u003e\u003e It can span multiple lines\n±±±\n\n\u003e This is a blockquote\n\u003e\n\u003e\u003e It can span multiple lines\n\n### Code\n\nInline code uses single backticks:\n\n±±±markdown\nUse ±func main()± to define the entry point.\n±±±\n\nUse ±func main()± to define the entry point.\n\nCode blocks use triple backticks with an optional language identifier:\n\n\u003c!-- XXX: make the example with 'gno' instead of 'go' --\u003e\n\n±±±markdown\n±±±go\npackage main\n\nfunc main() {\n    println(\"Hello, Gno!\")\n}\n±±±\n\n±±±go\npackage main\n\nfunc main() {\n    println(\"Hello, Gno!\")\n}\n±±±\n\n### Horizontal Rules\n\nHorizontal rules are created using three or more asterisks, hyphens, or underscores:\n\n±±±markdown\n---\n±±±\n\n---\n\n### Task Lists\n\nGno supports task lists for tracking to-do items:\n\n±±±markdown\n- [x] Completed task\n- [ ] Pending task\n- [ ] Another pending task\n- [x] Another completed task\n±±±\n\n- [x] Completed task\n- [ ] Pending task\n- [ ] Another pending task\n- [x] Another completed task\n\n### Footnotes\n\nGno supports footnotes for adding references and citations:\n\n±±±markdown\nHere is a sentence with a footnote[^1].\n\n[^1]: This is the footnote content.\n±±±\n\nHere is a sentence with a footnote[^1].\n\n[^1]: This is the footnote content.\n\nYou can also use multiple footnotes in the same document:\n\n±±±markdown\nThis is the first sentence with a footnote[^1].\nThis is another sentence with a different footnote[^2].\n\n[^1]: First footnote content.\n[^2]: Second footnote content with more details.\n±±±\n\nThis is the first sentence with a footnote[^1].\nThis is another sentence with a different footnote[^2].\n\n[^1]: First footnote content.\n[^2]: Second footnote content with more details.\n\n## Tables\n\nTables are created using pipes and hyphens:\n\n±±±markdown\n| Header 1 | Header 2 |\n| -------- | -------- |\n| Cell 1   | Cell 2   |\n| Cell 3   | Cell 4   |\n±±±\n\n| Header 1 | Header 2 |\n| -------- | -------- |\n| Cell 1   | Cell 2   |\n| Cell 3   | Cell 4   |\n\n## Images\n\nImages can be included using the following syntax:\n\n±±±markdown\n![Alt text](/public/imgs/gnoland.svg \"Optional title\")\n±±±\n\n![Alt text](/public/imgs/gnoland.svg \"Optional title\")\n\nCurrently, only a short list of content providers are supported:\n\n±±±markdown\n// Gno-related hosts\n\"https://gnolang.github.io\"\n\"https://assets.gnoteam.com\"\n\"https://sa.gno.services\"\n\n// Other providers should respect DMCA guidelines\n// NOTE: Feel free to open a PR to add more providers here :)\n\n// imgur\n\"https://imgur.com\"\n\"https://*.imgur.com\"\n\n// GitHub\n\"https://*.github.io\"\n\"https://github.com\"\n\"https://*.githubusercontent.com\"\n\n// IPFS\n\"https://ipfs.io\"\n\"https://cloudflare-ipfs.com\"\n±±±\n\n\n## Gno-Specific Features\n\n### HTML Support\n\nBy design, most typical HTML support is disabled in Gno's markdown implementation. This is an intentional decision for both security and ecosystem cohesion.\n\nWhile traditional markdown often allows arbitrary HTML tags, Gno Flavored Markdown takes a more controlled approach:\n\n- We may progressively whitelist certain HTML components or add custom ones over time\n- Our priority is to enhance our flavored markdown to natively support all essential components\n- We aim to eventually support all the initially HTML-supported features, but with syntax that is:\n  - More readable when viewing the source directly\n  - More integrable with custom browsers such as gnobro in CLI\n\nThis approach allows for a more consistent rendering experience across different Gno interfaces while maintaining security and readability as core principles.\n\n### Columns\n\nGno-Flavored Markdown introduces a column layout system that uses special tags. You can create columns with\n±\u003cgno-columns\u003e± blocks and separate the content with ±\u003cgno-columns-sep\u003e±.\n\n#### Basic Syntax\n±±±markdown\n\u003cgno-columns\u003e\nLeft content\n\n\u003cgno-columns-sep\u003e\n\nRight content\n\u003c/gno-columns\u003e\n±±±\n\n- Renders as:\n\n---\n\n\u003cgno-columns\u003e\nLeft content\n\n\u003cgno-columns-sep\u003e\n\nRight content\n\u003c/gno-columns\u003e\n\n---\n\n#### Key Features\n\n1. **Multiple Columns**:\n\nDepending on the screen size. A maximum of four rows can be displayed in\none row.\n\n***Note**: Any overflow will result in items being displayed in the next row. Also, keep in mind that on smaller screens\n(e.g., phones), this overflow may occur with fewer elements.*\n\n±±±markdown\n\u003cgno-columns\u003e\nFirst column\n\n\u003cgno-columns-sep\u003e\n\nSecond column\n\n\u003cgno-columns-sep\u003e\n\nThird column\n\n\u003cgno-columns-sep\u003e\n\nFourth column\n\n\u003cgno-columns-sep\u003e\n\nFifth column\n\u003c/gno-columns\u003e\n±±±\n\n- Renders as:\n\n---\n\n\u003cgno-columns\u003e\nFirst column\n\n\u003cgno-columns-sep\u003e\n\nSecond column\n\n\u003cgno-columns-sep\u003e\n\nThird column\n\n\u003cgno-columns-sep\u003e\n\nFourth column\n\n\u003cgno-columns-sep\u003e\n\nFifth column\n\u003c/gno-columns\u003e\n\n---\n\n2. **Mixed Content**:\n\nColumns can be mixed with any markdown content\n\n***Note**: Nested columns are currently not possible*\n\n±±±markdown\n\u003cgno-columns\u003e\n### Text Section\nParagraph with _formatting_\n\n\u003cgno-columns-sep\u003e\n\nMy Image ![Alt](/public/imgs/gnoland.svg)\n\u003c/gno-columns\u003e\n±±±\n\n- Renders as:\n\n---\n\n\u003cgno-columns\u003e\n### Text Section\nParagraph with _formatting_\n\n\u003cgno-columns-sep\u003e\n\nMy Image ![Alt](/public/imgs/gnoland.svg)\n\u003c/gno-columns\u003e\n\n---\n\n### Forms\n\nGno-Flavored Markdown introduces a secure form system that integrates directly with realms. The system uses custom HTML-like tags that are rendered with proper styling and validation.\n\n#### Basic Usage\n\nCreate a form using the ±\u003cgno-form\u003e± tag and add inputs with ±\u003cgno-input\u003e±:\n\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"name\" placeholder=\"Enter your name\" /\u003e\n  \u003cgno-input name=\"email\" placeholder=\"Enter your email\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"name\" placeholder=\"Enter your name\" /\u003e\n  \u003cgno-input name=\"email\" placeholder=\"Enter your email\" /\u003e\n\u003c/gno-form\u003e\n\n#### Form Structure\n\n1. **Form Container**\n   - Must start with ±\u003cgno-form\u003e± and end with ±\u003c/gno-form\u003e±\n   - Optional attributes:\n     - ±path±: Render path after form submission (e.g. ±path=\"user\"± will redirect to ±:user?[params]±)\n     - ±exec±: Specify a realm function to execute (e.g. ±exec=\"CreatePost\"±).\n       When set, fields must be defined manually and their ±name± attributes must match the function's parameter names in the same order they appear in the function signature.\n   - Form data is always sent as query parameters\n   - Cannot be nested (forms inside forms are not allowed)\n   - Automatically includes a header showing the realm name\n\n2. **Input Fields**\n   - Created with ±\u003cgno-input\u003e± tag\n   - Attributes:\n     - ±name±: Unique identifier for the input (required)\n     - ±type±: Type of input (optional, defaults to \"text\")\n     - ±placeholder±: Hint text shown in the input (optional, defaults to \"Enter value\")\n     - ±value±: Default value for non-selectable inputs (optional)\n     - ±description±: Description of the input as title (optional - can be used as title for group of one or multiple inputs)\n     - ±required±: Set to \"true\" to mark the field as required (optional)\n     - ±readonly±: Set to \"true\" to make the field read-only (optional)\n   - Supported input types:\n     - ±type=\"text\"± (default): For text input\n     - ±type=\"number\"±: For numeric values only (with browser validation)\n     - ±type=\"email\"±: For email addresses (with basic browser validation)\n     - ±type=\"tel\"±: For phone numbers (no specific validation)\n     - ±type=\"password\"±: For password input (masked characters)\n     - ±type=\"radio\"±: For single selection from a group (requires ±value± attribute)\n     - ±type=\"checkbox\"±: For multiple selections (requires ±value± attribute)\n   - Additional attributes for radio/checkbox:\n     - ±value±: The value to submit when selected (required for radio/checkbox)\n     - ±checked±: Set to \"true\" to pre-select the option (optional)\n   - Note: Input validation is handled by the browser's HTML5 validation\n   - Any other type will default to \"text\"\n   - Each input must have a unique name\n   - Inputs are rendered with labels and proper styling\n\n3. **Textarea Fields**\n   - Created with ±\u003cgno-textarea\u003e± tag\n   - Required attributes:\n     - ±name±: Unique identifier for the textarea (required)\n     - ±placeholder±: Hint text shown in the textarea (optional, defaults to \"Enter value\")\n   - Optional attributes:\n     - ±value±: Default content for the textarea (optional)\n     - ±rows±: Number of visible rows (2-10, defaults to 4)\n     - ±required±: Set to \"true\" to mark the field as required (optional)\n     - ±readonly±: Set to \"true\" to make the field read-only (optional)\n   - Perfect for longer text input like messages, descriptions, or comments\n   - Each textarea must have a unique name\n   - Textareas are rendered with labels and proper styling\n\n4. **Select Fields**\n   - Created with ±\u003cgno-select\u003e± tag (similar to radio buttons)\n   - Required attributes:\n     - ±name±: Unique identifier for the select group (required). Use underscores to separate words.\n     - ±value±: The value to submit and display text (required)\n   - Optional attributes:\n     - ±description±: Description of the select group (optional)\n     - ±selected±: Set to \"true\" to pre-select the option (optional)\n     - ±required±: Set to \"true\" to mark the field as required (optional)\n   - Multiple ±\u003cgno-select\u003e± elements with the same ±name± form a select group\n   - All options with the same ±name± form a group where only one can be selected\n   - The ±value± attribute serves both as the submitted value and the displayed text\n\n#### Example Use Cases\n\n1. Form with Path and Query Parameters\n±±±markdown\n\u003cgno-form path=\"user\"\u003e\n  \u003cgno-input name=\"username\" type=\"text\" placeholder=\"Enter username\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form path=\"user\"\u003e\n  \u003cgno-input name=\"username\" type=\"text\" placeholder=\"Enter username\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" /\u003e\n\u003c/gno-form\u003e\n\nThis form will submit to ±:user?username=value\u0026age=value± on the same realm.\n\n2. Form with Query Parameters Only\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"recipient\" type=\"text\" placeholder=\"Enter recipient address\" description=\"Recipient Information\" /\u003e\n  \u003cgno-input name=\"email\" type=\"email\" placeholder=\"Your email\" /\u003e\n  \u003cgno-input name=\"pswd\" type=\"password\" placeholder=\"Your password\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"recipient\" type=\"text\" placeholder=\"Enter recipient address\" description=\"Recipient Information\" /\u003e\n  \u003cgno-input name=\"email\" type=\"email\" placeholder=\"Your email\" /\u003e\n  \u003cgno-input name=\"pswd\" type=\"password\" placeholder=\"Your password\" /\u003e\n\u003c/gno-form\u003e\n\nThis form will submit to ±?recipient=value\u0026email=value\u0026pswd=value± on the same realm.\n\n3. Form with Radio Buttons\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"blue\" description=\"What is your favorite color?\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"red\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"green\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"blue\" description=\"What is your favorite color?\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"red\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"green\" /\u003e\n\u003c/gno-form\u003e\n\nRadio buttons allow users to select one option from a group. All radio buttons with the same ±name± form a group where only one can be selected.\n\n4. Form with Checkboxes\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"coding\" description=\"What do you like to do?\"/\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"gaming\" /\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"reading\" /\u003e\n  \u003cgno-input name=\"newsletter\" type=\"checkbox\" value=\"subscribe\" description=\"Subscribe to newsletter\" placeholder=\"To get the latest news\" checked=\"true\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"coding\" description=\"What do you like to do?\" /\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"gaming\" /\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"reading\" /\u003e\n  \u003cgno-input name=\"newsletter\" type=\"checkbox\" value=\"subscribe\" description=\"Subscribe to newsletter\" placeholder=\"To get the latest news\" checked=\"true\" /\u003e\n\u003c/gno-form\u003e\n\nCheckboxes allow users to select multiple options. Use ±checked=\"true\"± to pre-select a checkbox.\n\n5. Form with Textarea\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"message\" type=\"text\" placeholder=\"Subject\" /\u003e\n  \u003cgno-textarea name=\"content\" placeholder=\"Enter your message here\" rows=\"6\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"message\" type=\"text\" placeholder=\"Subject\" /\u003e\n  \u003cgno-textarea name=\"content\" placeholder=\"Enter your message here\" rows=\"6\" /\u003e\n\u003c/gno-form\u003e\n\nTextareas are perfect for longer text input. The ±rows± attribute controls the height (4-10 rows, default is 4).\n\n6. Form with Select Options\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-select name=\"country\" value=\"United States\" description=\"Select your country\" /\u003e\n  \u003cgno-select name=\"country\" value=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-select name=\"country\" value=\"United States\" description=\"Select your country\" selected=\"true\" /\u003e\n  \u003cgno-select name=\"country\" value=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n\u003c/gno-form\u003e\n\nSelect options work like radio buttons but with a more semantic meaning. All ±\u003cgno-select\u003e± elements with the same ±name± form a group where only one can be selected. Use ±selected=\"true\"± to pre-select an option. The ±value± attribute serves both as the submitted value and the displayed text.\n\n7. Complex Form with Mixed Elements\n±±±markdown\n\u003cgno-form path=\"contact\"\u003e\n  \u003cgno-input name=\"name\" type=\"text\" placeholder=\"Your full name\" description=\"Your Information\" /\u003e\n  \u003cgno-input name=\"email\" type=\"email\" placeholder=\"Your email address\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" /\u003e\n  \n  \u003cgno-input name=\"color\" type=\"radio\" value=\"blue\" description=\"What is your favorite color?\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"red\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"green\" /\u003e\n  \n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"tech\" description=\"What do you like to do?\" /\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"sports\" /\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"music\"/\u003e\n  \n  \u003cgno-select name=\"country\" value=\"us\" text=\"United States\" description=\"Select your country\" /\u003e\n  \u003cgno-select name=\"country\" value=\"fr\" text=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"de\" text=\"Germany\" /\u003e\n  \n  \u003cgno-textarea name=\"message\" placeholder=\"Tell us about yourself\" rows=\"5\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form path=\"contact\"\u003e\n  \u003cgno-input name=\"name\" type=\"text\" placeholder=\"Your full name\" description=\"Your Information\" /\u003e\n  \u003cgno-input name=\"email\" type=\"email\" placeholder=\"Your email address\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" /\u003e\n  \n  \u003cgno-input name=\"color\" type=\"radio\" value=\"blue\" description=\"What is your favorite color?\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"red\" /\u003e\n  \u003cgno-input name=\"color\" type=\"radio\" value=\"green\" /\u003e\n  \n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"tech\" description=\"What do you like to do?\" /\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"sports\" /\u003e\n  \u003cgno-input name=\"interests\" type=\"checkbox\" value=\"music\"/\u003e\n  \n  \u003cgno-select name=\"country\" value=\"us\" text=\"United States\" description=\"Select your country\" /\u003e\n  \u003cgno-select name=\"country\" value=\"fr\" text=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"de\" text=\"Germany\" /\u003e\n  \n  \u003cgno-textarea name=\"message\" placeholder=\"Tell us about yourself\" rows=\"5\" /\u003e\n\u003c/gno-form\u003e\n\nThis example shows how to combine all form element types in a single form.\n\n8. Form with Default Values\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"username\" type=\"text\" placeholder=\"Enter username\" value=\"Alice\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" value=\"42\" /\u003e\n  \u003cgno-textarea name=\"bio\" placeholder=\"Short bio\" rows=\"4\" value=\"Super builder\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"username\" type=\"text\" placeholder=\"Enter username\" value=\"Alice\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" value=\"42\" /\u003e\n  \u003cgno-textarea name=\"bio\" placeholder=\"Short bio\" rows=\"4\" value=\"Super builder\" /\u003e\n\u003c/gno-form\u003e\n\n9. Form with Readonly Fields\nThe ±readonly=\"true\"± attribute makes fields non-editable while displaying their values. This is useful for showing fixed values that users should see but cannot modify.\n\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"username\" value=\"alice\" readonly=\"true\" description=\"Your fixed username\" /\u003e\n  \u003cgno-input name=\"role\" type=\"text\" value=\"admin\" readonly=\"true\" /\u003e\n  \u003cgno-input name=\"status\" type=\"radio\" value=\"active\" checked=\"true\" readonly=\"true\" description=\"Account status\" /\u003e\n  \u003cgno-input name=\"status\" type=\"radio\" value=\"inactive\" readonly=\"true\" /\u003e\n  \u003cgno-input name=\"verified\" type=\"checkbox\" value=\"yes\" checked=\"true\" readonly=\"true\" /\u003e\n  \u003cgno-textarea name=\"terms\" value=\"Terms and conditions text...\" rows=\"3\" readonly=\"true\" /\u003e\n  \u003cgno-select name=\"plan\" value=\"premium\" selected=\"true\" readonly=\"true\" description=\"Your subscription\" /\u003e\n  \u003cgno-select name=\"plan\" value=\"basic\" readonly=\"true\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"username\" value=\"alice\" readonly=\"true\" description=\"Your fixed username\" /\u003e\n  \u003cgno-input name=\"role\" type=\"text\" value=\"admin\" readonly=\"true\" /\u003e\n  \u003cgno-input name=\"status\" type=\"radio\" value=\"active\" checked=\"true\" readonly=\"true\" description=\"Account status\" /\u003e\n  \u003cgno-input name=\"status\" type=\"radio\" value=\"inactive\" readonly=\"true\" /\u003e\n  \u003cgno-input name=\"verified\" type=\"checkbox\" value=\"yes\" checked=\"true\" readonly=\"true\" /\u003e\n  \u003cgno-textarea name=\"terms\" value=\"Terms and conditions text...\" rows=\"3\" readonly=\"true\" /\u003e\n  \u003cgno-select name=\"plan\" value=\"premium\" selected=\"true\" readonly=\"true\" description=\"Your subscription\" /\u003e\n  \u003cgno-select name=\"plan\" value=\"basic\" readonly=\"true\" /\u003e\n\u003c/gno-form\u003e\n\n**Note**: For text-based inputs and textareas, the HTML ±readonly± attribute is used. For radio buttons, checkboxes, and select dropdowns, ±disabled± is used instead since ±readonly± doesn't work on these elements in HTML.\n\n10. Form with Required Fields\nThe ±required=\"true\"± attribute marks fields as mandatory. Required fields display a \"(required)\" badge next to their label and use HTML5 browser validation.\n\n±±±markdown\n\u003cgno-form\u003e\n  \u003cgno-input name=\"username\" placeholder=\"Enter username\" required=\"true\" /\u003e\n  \u003cgno-input name=\"email\" type=\"email\" placeholder=\"Your email\" required=\"true\" /\u003e\n  \u003cgno-textarea name=\"bio\" placeholder=\"Tell us about yourself\" rows=\"4\" required=\"true\" /\u003e\n  \u003cgno-select name=\"country\" value=\"USA\" required=\"true\" /\u003e\n  \u003cgno-select name=\"country\" value=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form\u003e\n  \u003cgno-input name=\"username\" placeholder=\"Enter username\" required=\"true\"/\u003e\n  \u003cgno-input name=\"email\" type=\"email\" placeholder=\"Your email\" required=\"true\" /\u003e\n  \u003cgno-textarea name=\"bio\" placeholder=\"Tell us about yourself\" rows=\"4\" required=\"true\" /\u003e\n  \u003cgno-select name=\"country\" value=\"USA\" required=\"true\" /\u003e\n  \u003cgno-select name=\"country\" value=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n\u003c/gno-form\u003e\n\nRequired fields are validated by the browser before submission. The form cannot be submitted until all required fields are filled.\n\n11. Exec Form with Manual Fields\nWhen using the ±exec± attribute, a command preview appears for the specified function. You must manually define form fields whose ±name± attributes match the function's parameter names, placed in the same order as they appear in the function signature.\n\n±±±markdown\n\u003cgno-form exec=\"CreatePost\"\u003e\n  \u003cgno-input name=\"title\" type=\"text\" placeholder=\"Post title\" /\u003e\n  \u003cgno-textarea name=\"content\" rows=\"5\" placeholder=\"Post content\"\u003e\u003c/gno-textarea\u003e\n  \u003cgno-input name=\"tags\" type=\"text\" placeholder=\"Comma-separated tags\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form exec=\"CreatePost\"\u003e\n  \u003cgno-input name=\"title\" type=\"text\" placeholder=\"Post title\" /\u003e\n  \u003cgno-textarea name=\"content\" rows=\"5\" placeholder=\"Post content\"\u003e\u003c/gno-textarea\u003e\n  \u003cgno-input name=\"tags\" type=\"text\" placeholder=\"Comma-separated tags\" /\u003e\n\u003c/gno-form\u003e\n\nThe command preview shows the full gnokey command with placeholders for each field value.\n\n12. Exec Form with All Field Types\nHere's an example demonstrating all supported field types in a single exec form:\n\n±±±markdown\n\u003cgno-form exec=\"CreateProfile\"\u003e\n  \u003cgno-input name=\"username\" placeholder=\"Your username\" description=\"Profile Information\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" /\u003e\n  \u003cgno-textarea name=\"bio\" placeholder=\"Tell us about yourself\" rows=\"4\" /\u003e\n  \u003cgno-select name=\"country\" value=\"USA\" description=\"Select your country\" /\u003e\n  \u003cgno-select name=\"country\" value=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n  \u003cgno-input name=\"visibility\" type=\"radio\" value=\"public\" description=\"Profile visibility\" required=\"true\" /\u003e\n  \u003cgno-input name=\"visibility\" type=\"radio\" value=\"private\" /\u003e\n  \u003cgno-input name=\"notifications\" type=\"checkbox\" value=\"email\" description=\"Notifications\" checked=\"true\" /\u003e\n  \u003cgno-input name=\"notifications\" type=\"checkbox\" value=\"sms\" /\u003e\n\u003c/gno-form\u003e\n±±±\n\n\u003cgno-form exec=\"CreateProfile\"\u003e\n  \u003cgno-input name=\"username\" placeholder=\"Your username\" description=\"Profile Information\" value=\"alice\" /\u003e\n  \u003cgno-input name=\"age\" type=\"number\" placeholder=\"Your age\" /\u003e\n  \u003cgno-textarea name=\"bio\" placeholder=\"Tell us about yourself\" rows=\"4\" /\u003e\n  \u003cgno-select name=\"country\" value=\"USA\" description=\"Select your country\" /\u003e\n  \u003cgno-select name=\"country\" value=\"France\" /\u003e\n  \u003cgno-select name=\"country\" value=\"Germany\" /\u003e\n  \u003cgno-input name=\"visibility\" type=\"radio\" value=\"public\" description=\"Profile visibility\" required=\"true\" /\u003e\n  \u003cgno-input name=\"visibility\" type=\"radio\" value=\"private\" /\u003e\n  \u003cgno-input name=\"notifications\" type=\"checkbox\" value=\"email\" description=\"Notifications\" checked=\"true\" /\u003e\n  \u003cgno-input name=\"notifications\" type=\"checkbox\" value=\"sms\" /\u003e\n\u003c/gno-form\u003e\n\nAll field types update the command preview in real-time as you interact with them.\n\n#### Important Rules\n\n1. **Validation Rules**:\n   - Every ±\u003cgno-input\u003e± must have a ±name± attribute\n   - Every ±\u003cgno-textarea\u003e± must have a ±name± attribute\n   - Every ±\u003cgno-select\u003e± must have a ±name± and ±value± attribute\n   - Duplicate attribute names are not allowed (except for radio, checkbox, and select groups)\n   - Forms must be properly closed\n   - Nested forms will not render\n   - Radio and checkbox inputs must have a ±value± attribute\n   - The ±checked± attribute is only valid for radio and checkbox inputs\n   - The ±selected± attribute is only valid for ±\u003cgno-select\u003e± elements\n   - The ±readonly=\"true\"± attribute makes fields non-editable (works on all field types)\n   - The ±required=\"true\"± attribute marks fields as required (works on input, textarea, and select)\n   - Textarea ±rows± attribute must be between 2 and 10 (defaults to 4 if invalid or not specified)\n\n2. **Security Features**:\n   - Forms are processed on the realm where they are defined\n   - Each form submission is associated with its realm\n   - The realm name is displayed in the form header\n   - Input validation is handled by the realm's smart contract\n\n--- \n\n### Alerts\n\nAlerts are a way to highlight important information in your markdown.\n\nThere are five types of alerts:\n\n- INFO\n- NOTE\n- TIP\n- SUCCESS\n- WARNING\n- CAUTION\n\n**NOTE**: The default alert type is INFO (if a wrong alert type is used).\n\nThe alert boxes are expandable/collapsible and can have a title. \nBy default, the alert boxes are opened. The title is optional and if not provided, the alert type will be used as the title.\n\n±±±markdown\n\u003e [!NOTE]\n\u003e This is a note\n±±±\n\n\u003e [!NOTE]\n\u003e This is a note\n\n±±±markdown\n\u003e [!NOTE]-\n\u003e This is a closed note\n±±±\n\n\u003e [!NOTE]-\n\u003e This is a closed note\n\n±±±markdown\n\u003e [!NOTE] This is a note with title\n\u003e This is a note with title\n±±±\n\n\u003e [!NOTE] This is a note with title\n\u003e This is a note with title\n\n±±±markdown\n\u003e [!INFO]\n\u003e This is an info\n±±±\n\n\u003e [!INFO]\n\u003e This is an info\n\n±±±markdown\n\u003e [!TIP]\n\u003e This is a tip\n±±±\n\n\u003e [!TIP]\n\u003e This is a tip\n\n±±±\n\u003e [!SUCCESS]\n\u003e This is a success\n±±±\n\n\u003e [!SUCCESS]\n\u003e This is a success\n\n±±±\n\u003e [!WARNING]\n\u003e This is a warning\n±±±\n\n\u003e [!WARNING]\n\u003e This is a warning\n\n±±±\n\u003e [!CAUTION]\n\u003e This is a caution\n±±±\n\n\u003e [!CAUTION]\n\u003e This is a caution\n\n±±±markdown\n\u003e [!FOO]\n\u003e The default alert if a wrong alert type is used\n±±±\n\n\u003e [!FOO]\n\u003e This is the default alert\n\n±±±markdown\n\u003e [!NOTE]\n\u003e This is a note\n\u003e \u003e [!NOTE]\n\u003e \u003e This is a scoped note\n±±±\n\n\u003e [!NOTE]\n\u003e This is a note\n\u003e \u003e [!NOTE]\n\u003e \u003e This is a scoped note\n\n### User Mentions and Bech32 Addresses\n\nGno markdown automatically recognizes and renders user mentions and Bech32 addresses as clickable links.\n\n#### User Mentions\n\n// You can mention users using the \"@\" symbol followed by a username. The username can contain letters, numbers, and underscores.\n\n±±±markdown\nHello @alice, how are you doing?\n\nThis is a mention of @bob_user and @test-123.\n\nYou can also mention users like @user_name_123.\n±±±\n\n\nHello @alice, how are you doing?\n\nThis is a mention of @bob_user.\n\nYou can also mention users like @user_name_123.\n\n#### Bech32 Addresses\n\nGno markdown can automatically recognize and render valid Bech32 addresses as clickable links.\n\n±±±markdown\nThis is a GNO address: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\n\nYou can also reference g1abc123def456ghi789jkl012mno345pqr678stu901vwx234yz5.\n±±±\n\nThis is a GNO address: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\n\nYou can also reference g1abc123def456ghi789jkl012mno345pqr678stu901vwx234yz5.\n\n### Smart Contract Integration\n\nXXX: TODO\n\n±±±markdown\ngno.land/r/boards\ngno.land/r/boards:foo/bar\ngno.land/r/docs/markdown$source\n±±±\n\ngno.land/r/boards\ngno.land/r/boards:foo/bar\ngno.land/r/docs/markdown$source\n\n### And more...\n\nXXX: TODO\n\n## Future Enhancements\n\nThe markdown support in Gno is being actively developed. Future enhancements may include:\n\n- Extended support for custom components\n- Interactive elements specific to blockchain functions\n- Rich rendering of on-chain data\n- Better integration with Gno's package system\n\n[Read more](https://github.com/gnolang/gno/issues/3255)\n\n## Conclusion\n\nMarkdown on Gno provides a familiar syntax for developers who have experience with GitHub Flavored Markdown, while adding blockchain-specific extensions that make it more powerful in the context of the Gno platform.\n\nAs the Gno ecosystem grows, expect the markdown capabilities to expand accordingly, providing even richer formatting and interactive elements for documentation and user interfaces.\n\n## See Also\n\nFor programmatic markdown generation using Go code, check out the [±p/moul/md± demo](/r/docs/moul_md) which demonstrates how to use the ±p/moul/md± package to generate markdown content in your Gno realms.\n`\n\toutput = strings.ReplaceAll(output, \"±\", \"`\")\n\treturn output\n}\n"},{"name":"markdown_test.gno","body":"package markdown\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestRender(t *testing.T) {\n\toutput := Render(\"\")\n\tif !strings.Contains(output, \"Gno Flavored Markdown\") {\n\t\tt.Errorf(\"invalid output\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"nGTi4G4LA15dbVchTyPZ3QP53tVzCTcQrf1N8u7yVpyco+5Fr+nfJRUYT7wXQ8L7RkN8ifjjCT652cFkhpXUCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"minisocial","path":"gno.land/r/docs/minisocial","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/minisocial\"\ngno = \"0.9\"\n"},{"name":"minisocial.gno","body":"package minisocial\n\nfunc Render(_ string) string {\n\treturn `# MiniSocial\nMiniSocial is a minimalistic social media platform made for example purposes. \n\nThere are two versions of this app:\n- [V1](/r/docs/minisocial/v1) - handles simple post creation and stores posts in a slice\n- [V2](/r/docs/minisocial/v2) - handles post creation, updating, and deletion, \nand manages storage more efficiently with an AVL tree. V2 also utilizes different p/ packages to handle pagination, \neasier Markdown formatting, etc.`\n\n}\n\n// Original work \u0026 inspiration here:\n// https://gno.land/r/leon/fosdem25/microposts\n// https://gno.land/r/moul/microposts\n// Find the full tutorial on the official gno.land docs at docs.gno.land\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"cIF+69HUuSfuYDORccuojAAs3Ok4t6tSJUo7STGQT+2By1iwYdO4f2sKiMezP3fWZiIVgMf8ISZBimp07HpVCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"minisocial","path":"gno.land/r/docs/minisocial/v1","files":[{"name":"admin.gno","body":"package minisocial\n\nimport \"gno.land/p/nt/ownable/v0\"\n\nvar Ownable = ownable.NewWithAddress(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\") // @leohhhn\n\n// ResetPosts allows admin deletion of the posts\nfunc ResetPosts(cur realm) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\tposts = nil\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/minisocial/v1\"\ngno = \"0.9\"\n"},{"name":"posts.gno","body":"package minisocial\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/moul/helplink\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar posts []*Post // inefficient for large amounts of posts; see v2\n\n// CreatePost creates a new post\nfunc CreatePost(cur realm, text string) error {\n\t// If the body of the post is empty, return an error\n\tif text == \"\" {\n\t\treturn errors.New(\"empty post text\")\n\t}\n\n\t// Append the new post to the list\n\tposts = append(posts, \u0026Post{\n\t\ttext:      text,                     // Set the input text\n\t\tauthor:    cur.Previous().Address(), // The author of the address is the previous realm, the realm that called this one\n\t\tcreatedAt: time.Now(),               // Capture the time of the transaction, in this case the block timestamp\n\t})\n\n\treturn nil\n}\n\nfunc Render(_ string) string {\n\toutput := \"# MiniSocial\\n\\n\" // \\n is needed just like in standard Markdown\n\t// Create a clickable link to create a post\n\toutput += helplink.Func(\"Write a post!\", \"CreatePost\", \"text\", \"\")\n\toutput += \"\\n\\n\"\n\n\t// Handle the edge case\n\tif len(posts) == 0 {\n\t\toutput += \"No posts.\\n\"\n\t\treturn output\n\t}\n\n\t// Let's append the text of each post to the output\n\tfor i, post := range posts {\n\t\t// Let's append some post metadata\n\t\toutput += ufmt.Sprintf(\"#### Post #%d\\n\\n\", i)\n\t\t// Add the stringified post\n\t\toutput += post.String()\n\t\t// Add a line break for cleaner UI\n\t\toutput += \"---\\n\\n\"\n\t}\n\n\treturn output\n}\n"},{"name":"posts_test.gno","body":"package minisocial\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\" // Provides testing utilities\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestCreatePostSingle(cur realm, t *testing.T) {\n\t// Get a test address for alice\n\taliceAddr := testutils.TestAddress(\"alice\")\n\t// TestSetRealm sets the realm caller, in this case Alice\n\ttesting.SetRealm(testing.NewUserRealm(aliceAddr))\n\n\ttext1 := \"Hello World!\"\n\terr := CreatePost(cross(cur), text1)\n\tuassert.True(t, err == nil, \"expected no error\")\n\n\t// Get the rendered page\n\tgot := Render(\"\")\n\n\t// Content should have the text and alice's address in it\n\tcondition := strings.Contains(got, text1) \u0026\u0026 strings.Contains(got, aliceAddr.String())\n\tuassert.True(t, condition, \"expected render to contain text \u0026 alice's address\")\n}\n\nfunc TestCreatePostMultiple(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(testutils.TestAddress(\"alice\")))\n\n\t// Initialize a slice to hold the test posts and their authors\n\tposts := []struct {\n\t\ttext   string\n\t\tauthor string\n\t}{\n\t\t{\"Hello World!\", \"alice\"},\n\t\t{\"This is some new text!\", \"bob\"},\n\t\t{\"Another post by alice\", \"alice\"},\n\t\t{\"A post by charlie!\", \"charlie\"},\n\t}\n\n\tfor _, p := range posts {\n\t\t// Set the appropriate caller realm based on the author\n\t\tauthorAddr := testutils.TestAddress(p.author)\n\t\ttesting.SetRealm(testing.NewUserRealm(authorAddr))\n\n\t\t// Create the post\n\t\terr := CreatePost(cross(cur), p.text)\n\t\tuassert.True(t, err == nil, \"expected no error for post \"+p.text)\n\t}\n\n\t// Get the rendered page\n\tgot := Render(\"\")\n\n\t// Check that all posts and their authors are present in the rendered output\n\tfor _, p := range posts {\n\t\texpectedText := p.text\n\t\texpectedAuthor := testutils.TestAddress(p.author).String() // Get the address for the author\n\t\tcondition := strings.Contains(got, expectedText) \u0026\u0026 strings.Contains(got, expectedAuthor)\n\t\tuassert.True(t, condition, \"expected render to contain text '\"+expectedText+\"' and address '\"+expectedAuthor+\"'\")\n\t}\n}\n"},{"name":"types.gno","body":"package minisocial\n\nimport (\n\t// The standard Gno package\n\t\"time\" // For handling time operations\n\n\t\"gno.land/p/nt/ufmt/v0\" // For string formatting, like `fmt`\n)\n\n// Post defines the main data we keep about each post\ntype Post struct {\n\ttext      string    // Main text body\n\tauthor    address   // Address of the post author, provided by the execution context\n\tcreatedAt time.Time // When the post was created\n}\n\n// String stringifies a Post\nfunc (p Post) String() string {\n\tout := p.text\n\tout += \"\\n\\n\"\n\tout += ufmt.Sprintf(\"_by %s_, \", p.author)\n\t// We can use `ufmt` to format strings, and the built-in time library formatting function\n\tout += ufmt.Sprintf(\"_on %s_\", p.createdAt.Format(\"02 Jan 2006, 15:04\"))\n\n\tout += \"\\n\\n\"\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"b0Au2pZck430Aa7BFeOcs8EPOX3Y/gG8YF2a5JipAa52aseAAucJGscUbRnieUhcWm5MiKLwlxHwjt/EtGC2AA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"minisocial","path":"gno.land/r/docs/minisocial/v2","files":[{"name":"admin.gno","body":"package minisocial\n\nimport (\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\nvar Ownable = ownable.NewWithAddress(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\") // @leohhhn\n\n// ResetPosts allows admin deletion of the posts\nfunc ResetPosts(cur realm) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\tposts = avl.NewTree()\n\tpostID = seqid.ID(0)\n\tpag = pager.NewPager(posts, 5, true)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/minisocial/v2\"\ngno = \"0.9\"\n"},{"name":"posts.gno","body":"package minisocial\n\nimport (\n\t\"errors\"\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/sys/users\"\n)\n\nvar (\n\tpostID seqid.ID                         // counter for post IDs\n\tposts  = avl.NewTree()                  // seqid.ID.String() \u003e *Post\n\tpag    = pager.NewPager(posts, 5, true) // To help with pagination in rendering\n\n\t// Errors\n\tErrEmptyPost           = errors.New(\"empty post text\")\n\tErrPostNotFound        = errors.New(\"post not found\")\n\tErrUpdateWindowExpired = errors.New(\"update window expired\")\n\tErrUnauthorized        = errors.New(\"you're not authorized to update this post\")\n)\n\n// CreatePost creates a new post\nfunc CreatePost(cur realm, text string) error {\n\tif text == \"\" {\n\t\treturn ErrEmptyPost\n\t}\n\n\t// Get the next ID\n\t// seqid.IDs are sequentially stored in the AVL tree\n\t// This provides chronological order when iterating\n\tid := postID.Next()\n\n\t// Set the key:value pair into the AVL tree:\n\t// avl.Tree.Set takes a string for a key, and anything as a value.\n\t// Stringify the key, and set the pointer to a new Post struct\n\tposts.Set(id.String(), \u0026Post{\n\t\tid:        id,                       // Set the ID, used later for editing or deletion\n\t\ttext:      text,                     // Set the input text\n\t\tauthor:    cur.Previous().Address(), // The author of the address is the previous realm, the realm that called this one\n\t\tcreatedAt: time.Now(),               // Capture the time of the transaction, in this case the block timestamp\n\t\tupdatedAt: time.Now(),\n\t})\n\n\treturn nil\n}\n\n// UpdatePost allows the author to update a post\n// The post can only be updated up to 10 minutes after posting\nfunc UpdatePost(cur realm, id string, text string) error {\n\t// Try to get the post\n\traw := posts.Get(id)\n\tif raw == nil {\n\t\treturn ErrPostNotFound\n\t}\n\n\t// Cast post from AVL tree\n\tpost := raw.(*Post)\n\tif cur.Previous().Address() != post.author {\n\t\treturn ErrUnauthorized\n\t}\n\n\t// Can only update 10 mins after it was posted\n\tif post.updatedAt.After(post.createdAt.Add(time.Minute * 10)) {\n\t\treturn ErrUpdateWindowExpired\n\t}\n\n\tpost.text = text\n\tpost.updatedAt = time.Now()\n\n\treturn nil\n}\n\n// DeletePost deletes a post with a specific id\n// Only the creator of a post can delete the post\nfunc DeletePost(cur realm, id string) error {\n\t// Try to get the post\n\traw := posts.Get(id)\n\tif raw == nil {\n\t\treturn ErrPostNotFound\n\t}\n\n\t// Cast post from AVL tree\n\tpost := raw.(*Post)\n\tif cur.Previous().Address() != post.author {\n\t\treturn ErrUnauthorized\n\t}\n\n\t// Use avl.Tree.Remove\n\t_, removed := posts.Remove(id)\n\tif !removed {\n\t\t// This shouldn't happen after all checks above\n\t\t// If it does, discard any possible state changes\n\t\tpanic(\"failed to remove post\")\n\t}\n\n\treturn nil\n}\n\n// Render renders the main page of threads\nfunc Render(path string) string {\n\tout := md.H1(\"MiniSocial\")\n\tif posts.Size() == 0 {\n\t\tout += \"No posts yet!\\n\\n\"\n\t\treturn out\n\t}\n\n\t// Get the page from the path\n\tpage := pag.MustGetPageByPath(path)\n\n\t// Iterate over items in the page\n\tfor _, item := range page.Items {\n\t\tpost := item.Value.(*Post)\n\n\t\t// Try resolving the address for a username\n\t\ttext := post.author.String()\n\t\tuser := users.ResolveAddress(post.author)\n\t\tif user != nil {\n\t\t\ttext = user.RenderLink(\"\")\n\t\t}\n\n\t\tout += md.H4(ufmt.Sprintf(\"Post #%d - %s\\n\\n\", int(post.id), text))\n\n\t\tout += post.String()\n\t\tout += md.HorizontalRule()\n\t}\n\n\tout += page.Picker(path)\n\tout += \"\\n\\n\"\n\tout += \"Page \" + strconv.Itoa(page.PageNumber) + \" of \" + strconv.Itoa(page.TotalPages) + \"\\n\\n\"\n\n\treturn out\n}\n"},{"name":"posts_test.gno","body":"package minisocial\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\" // Provides testing utilities\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nfunc TestCreatePostSingle(cur realm, t *testing.T) {\n\t// Get a test address for alice\n\taliceAddr := testutils.TestAddress(\"alice\")\n\t// TestSetRealm sets the realm caller, in this case Alice\n\ttesting.SetRealm(testing.NewUserRealm(aliceAddr))\n\n\ttext1 := \"Hello World!\"\n\terr := CreatePost(cross(cur), text1)\n\tuassert.True(t, err == nil, \"expected no error\")\n\n\t// Get the rendered page\n\tgot := Render(\"\")\n\n\t// Content should have the text and alice's address in it\n\tcondition := strings.Contains(got, text1) \u0026\u0026 strings.Contains(got, aliceAddr.String())\n\tuassert.True(t, condition, \"expected render to contain text \u0026 alice's address\")\n}\n\nfunc TestCreatePostMultiple(cur realm, t *testing.T) {\n\t// Initialize a slice to hold the test posts and their authors\n\tposts := []struct {\n\t\ttext   string\n\t\tauthor string\n\t}{\n\t\t{\"Hello World!\", \"alice\"},\n\t\t{\"This is some new text!\", \"bob\"},\n\t\t{\"Another post by alice\", \"alice\"},\n\t\t{\"A post by charlie!\", \"charlie\"},\n\t}\n\n\tfor _, p := range posts {\n\t\t// Set the appropriate caller realm based on the author\n\t\tauthorAddr := testutils.TestAddress(p.author)\n\t\ttesting.SetRealm(testing.NewUserRealm(authorAddr))\n\n\t\t// Create the post\n\t\terr := CreatePost(cross(cur), p.text)\n\t\tuassert.True(t, err == nil, \"expected no error for post \"+p.text)\n\t}\n\n\t// Get the rendered page\n\tgot := Render(\"\")\n\n\t// Check that all posts and their authors are present in the rendered output\n\tfor _, p := range posts {\n\t\texpectedText := p.text\n\t\texpectedAuthor := testutils.TestAddress(p.author).String() // Get the address for the author\n\t\tcondition := strings.Contains(got, expectedText) \u0026\u0026 strings.Contains(got, expectedAuthor)\n\t\tuassert.True(t, condition, \"expected render to contain text '\"+expectedText+\"' and address '\"+expectedAuthor+\"'\")\n\t}\n}\n\nfunc TestReset(cur realm, t *testing.T) {\n\taliceAddr := testutils.TestAddress(\"alice\")\n\ttesting.SetRealm(testing.NewUserRealm(aliceAddr))\n\n\ttext1 := \"Hello World!\"\n\t_ = CreatePost(cross(cur), text1)\n\n\tgot := Render(\"\")\n\tuassert.True(t, strings.Contains(got, text1), \"expected render to contain text1\")\n\n\t// Set admin\n\ttesting.SetRealm(testing.NewUserRealm(Ownable.Owner()))\n\tResetPosts(cross(cur))\n\n\tgot = Render(\"\")\n\tuassert.False(t, strings.Contains(got, text1), \"expected render to not contain text1\")\n\n\ttext2 := \"Some other Text!!\"\n\t_ = CreatePost(cross(cur), text2)\n\n\tgot = Render(\"\")\n\tuassert.False(t, strings.Contains(got, text1), \"expected render to not contain text1\")\n\n\tuassert.True(t, strings.Contains(got, text2), \"expected render to contain text2\")\n}\n\n// TODO: Add tests for Update \u0026 Delete\n"},{"name":"types.gno","body":"package minisocial\n\nimport (\n\t// The standard Gno package\n\t\"time\" // For handling time operations\n\n\t\"gno.land/p/nt/seqid/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/sys/users\"\n)\n\n// Post defines the main data we keep about each post\ntype Post struct {\n\tid        seqid.ID\n\ttext      string\n\tauthor    address\n\tcreatedAt time.Time\n\tupdatedAt time.Time\n}\n\nfunc (p Post) String() string {\n\tout := p.text + \"\\n\\n\"\n\n\tauthor := p.author.String()\n\t// We can import and use the r/sys/users package to resolve addresses\n\tuser := users.ResolveAddress(p.author)\n\tif user != nil {\n\t\t// RenderLink provides a link that is clickable\n\t\t// The link goes to the user's profile page\n\t\tauthor = user.RenderLink(\"\")\n\t}\n\n\tout += ufmt.Sprintf(\"_by %s on %s_\\n\\n\", author, p.createdAt.Format(\"02 Jan 2006, 15:04\"))\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"fnzmF/3pXSg9jmakRVhP6T9S5ipQhmLyOkkZSvaXKy4fbZlRtl2Bp/0aVO2s8CpjAlH8Gf6Pv8S7Bb8xDbBjAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"mistakes","path":"gno.land/r/docs/mistakes","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/mistakes\"\ngno = \"0.9\"\n"},{"name":"mistakes.gno","body":"// Package mistakes shows common mistakes in Gno development and how to avoid them.\npackage mistakes\n\nfunc Render(_ string) string {\n\treturn `# Common Mistakes in Gno\n\n## 1. Integer Overflow\n\nGo (and Gno) integers can overflow silently:\n\n` + \"```go\" + `\nvar balance int64 = 9223372036854775807 // max int64\nbalance += 1 // Wraps to -9223372036854775808!\n` + \"```\" + `\n\nUse the [` + \"`math/overflow`\" + `](https://docs.gno.land/resources/gno-stdlibs) standard library instead:\n\n` + \"```go\" + `\nimport \"math/overflow\"\n\nresult, ok := overflow.Add64(a, b)\nif !ok {\n    panic(\"overflow\")\n}\n` + \"```\" + `\n\n---\n\n## 2. Errors Don't Revert State\n\nReturning an error doesn't revert state changes:\n\n` + \"```go\" + `\n// WRONG\nvar balances map[address]int64\n\nfunc Transfer(cur realm, to address, amount int64) error {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    from := cur.Previous().Address()\n    balances[from] -= amount\n    if balances[from] \u003c 0 {\n        return errors.New(\"insufficient balance\")  // State NOT reverted!\n    }\n    balances[to] += amount\n    return nil\n}\n` + \"```\" + `\n\nCheck conditions first, or use panic to revert:\n\n` + \"```go\" + `\n// CORRECT\nvar balances map[address]int64\n\nfunc Transfer(cur realm, to address, amount int64) {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    from := cur.Previous().Address()\n    if balances[from] \u003c amount {\n        panic(\"insufficient balance\")  // Reverts all changes\n    }\n    balances[from] -= amount\n    balances[to] += amount\n}\n` + \"```\" + `\n\n---\n\n## 3. OriginCaller vs cur.Previous()\n\nUsing [` + \"`OriginCaller`\" + `](/r/docs/crossing) for authorization enables middleman attacks:\n\n` + \"```go\" + `\n// VULNERABLE: Malicious realm can call this on behalf of any user\nvar balances map[address]int64\n\nfunc Withdraw(_ realm, amount int64) {\n    caller := unsafe.OriginCaller()  // Always returns the EOA who signed tx\n    balances[caller] -= amount\n}\n` + \"```\" + `\n\nUse [` + \"`cur.Previous().Address()`\" + `](/r/docs/crossing) for permission checks,\nguarded by ` + \"`cur.IsCurrent()`\" + ` so a non-crossing call can't pass a non-current\nrealm handle to forge the caller:\n\n` + \"```go\" + `\n// SAFE\nvar balances map[address]int64\n\nfunc Withdraw(cur realm, amount int64) {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    caller := cur.Previous().Address()  // Returns immediate caller\n    balances[caller] -= amount\n}\n` + \"```\" + `\n\n---\n\n## 4. Not Validating Addresses\n\n` + \"```go\" + `\n// WRONG\nvar admin address\n\nfunc SetAdmin(newAdmin address) {\n    admin = newAdmin  // Could be an empty or malformed address!\n}\n\n// CORRECT\nvar admin address\n\nfunc SetAdmin(_ realm, newAdmin address) {\n    if !newAdmin.IsValid() {\n        panic(\"invalid address\")\n    }\n    admin = newAdmin\n}\n` + \"```\" + `\n\n---\n\n## 5. Returning References to Internal State\n\nReturning a pointer, slice, map, or interface value gives the caller a live\nreference to your realm's data. The read-only taint stops an external realm from\nwriting to a returned field directly, but it does not stop a mutating method on\nthe value, and same-realm code is not tainted at all. Either path skips the\nchecks your setters enforce.\n\n` + \"```go\" + `\n// RISKY\ntype Config struct {\n    admin address\n}\n\n// SetAdmin has no checks; any caller holding a *Config can call it.\nfunc (c *Config) SetAdmin(a address) { c.admin = a }\n\nvar config Config\n\n// Returns a live pointer, so a caller can call SetAdmin directly\nfunc GetConfig() *Config {\n    return \u0026config\n}\n` + \"```\" + `\n\nReturn a copy, or expose only read methods, when the caller should not change\nstored state:\n\n` + \"```go\" + `\n// SAFE\nvar admins []address\n\nfunc Admins() []address {\n    return append([]address(nil), admins...)\n}\n` + \"```\" + `\n`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ZMFQclaNvpF7sjAxFbuG7HdyltaKDLNRxS/60imomUSceYHVxTjQRSotjQN//lXZyG7QPowjlGsoeD3R8QM0DA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"moul_md","path":"gno.land/r/docs/moul_md","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/moul_md\"\ngno = \"0.9\"\n"},{"name":"moul_md.gno","body":"package moul_md\n\nimport \"gno.land/p/moul/md\"\n\n// This package demonstrates the usage of the gno.land/p/moul/md package.\n\nfunc Render(path string) string {\n\tsections := []string{\n\t\trenderIntro(),\n\t\trenderTextFormatting(),\n\t\trenderHeaders(),\n\t\trenderLists(),\n\t\trenderLinksAndImages(),\n\t\trenderCode(),\n\t\trenderBlockquotes(),\n\t\trenderAdvancedFeatures(),\n\t\trenderUtilityFunctions(),\n\t\trenderPracticalExample(),\n\t\trenderBestPractices(),\n\t\trenderSeeAlso(),\n\t}\n\n\toutput := \"\"\n\tfor _, section := range sections {\n\t\toutput += section\n\t}\n\treturn output\n}\n\nfunc renderIntro() string {\n\treturn md.H1(\"Package Demo: `p/moul/md` \") +\n\t\tmd.Paragraph(\"This document demonstrates the features of the \"+md.Link(\"gno.land/p/moul/md\", \"https://gno.land/p/moul/md\")+\" package, showing both the source code and the rendered result for each feature.\") +\n\t\t\"\\n\" +\n\t\tmd.Blockquote(md.Bold(\"Note:\")+\" The md package provides helper functions to generate markdown programmatically, making it easier to create dynamic documentation and content.\") +\n\t\t\"\\n\"\n}\n\nfunc renderTextFormatting() string {\n\treturn md.H2(\"Text Formatting\") + \"\\n\" +\n\t\t// Bold\n\t\trenderExample(\n\t\t\t\"Bold Text\",\n\t\t\t`md.Bold(\"Important text\")`,\n\t\t\tmd.Bold(\"Important text\"),\n\t\t) +\n\t\t// Italic\n\t\trenderExample(\n\t\t\t\"Italic Text\",\n\t\t\t`md.Italic(\"Emphasized text\")`,\n\t\t\tmd.Italic(\"Emphasized text\"),\n\t\t) +\n\t\t// Strikethrough\n\t\trenderExample(\n\t\t\t\"Strikethrough Text\",\n\t\t\t`md.Strikethrough(\"Deprecated feature\")`,\n\t\t\tmd.Strikethrough(\"Deprecated feature\"),\n\t\t) +\n\t\t// Combined\n\t\trenderExample(\n\t\t\t\"Combined Formatting\",\n\t\t\t`md.Bold(md.Italic(\"Very important\"))`,\n\t\t\tmd.Bold(md.Italic(\"Very important\")),\n\t\t)\n}\n\nfunc renderHeaders() string {\n\tcode := `md.H1(\"Main Title\")\nmd.H2(\"Section\")\nmd.H3(\"Subsection\")\nmd.H4(\"Sub-subsection\")\nmd.H5(\"Minor heading\")\nmd.H6(\"Smallest heading\")`\n\n\tresult := md.H1(\"Main Title\") +\n\t\tmd.H2(\"Section\") +\n\t\tmd.H3(\"Subsection\") +\n\t\tmd.H4(\"Sub-subsection\") +\n\t\tmd.H5(\"Minor heading\") +\n\t\tmd.H6(\"Smallest heading\")\n\n\treturn md.H2(\"Headers\") +\n\t\tmd.Paragraph(\"The package supports all six levels of markdown headers:\") +\n\t\t\"\\n\" +\n\t\tmd.LanguageCodeBlock(\"go\", code) +\n\t\t\"\\nResult:\\n\" +\n\t\tresult +\n\t\t\"\\n\"\n}\n\nfunc renderLists() string {\n\toutput := md.H2(\"Lists\") + \"\\n\"\n\n\t// Bullet Lists\n\tbulletCode := `items := []string{\n    \"First item\",\n    \"Second item\",\n    \"Third item with\\nmultiple lines\",\n}\nmd.BulletList(items)`\n\n\toutput += md.H3(\"Bullet Lists\") +\n\t\tmd.LanguageCodeBlock(\"go\", bulletCode) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.BulletList([]string{\n\t\t\t\"First item\",\n\t\t\t\"Second item\",\n\t\t\t\"Third item with\\nmultiple lines\",\n\t\t}) + \"\\n\"\n\n\t// Ordered Lists\n\torderedCode := `steps := []string{\n    \"First step\",\n    \"Second step\",\n    \"Third step with\\nadditional details\",\n}\nmd.OrderedList(steps)`\n\n\toutput += md.H3(\"Ordered Lists\") +\n\t\tmd.LanguageCodeBlock(\"go\", orderedCode) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.OrderedList([]string{\n\t\t\t\"First step\",\n\t\t\t\"Second step\",\n\t\t\t\"Third step with\\nadditional details\",\n\t\t}) + \"\\n\"\n\n\t// Todo Lists\n\ttodoCode := `tasks := []string{\n    \"Completed task\",\n    \"Another completed task\",\n    \"Pending task\",\n    \"Another pending task\",\n}\ncompleted := []bool{true, true, false, false}\nmd.TodoList(tasks, completed)`\n\n\toutput += md.H3(\"Todo Lists\") +\n\t\tmd.LanguageCodeBlock(\"go\", todoCode) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.TodoList(\n\t\t\t[]string{\n\t\t\t\t\"Completed task\",\n\t\t\t\t\"Another completed task\",\n\t\t\t\t\"Pending task\",\n\t\t\t\t\"Another pending task\",\n\t\t\t},\n\t\t\t[]bool{true, true, false, false},\n\t\t) + \"\\n\"\n\n\t// Nested Lists\n\tnestedCode := `md.BulletItem(\"Parent item\") +\nmd.Nested(\n    md.BulletItem(\"Nested item 1\") +\n    md.BulletItem(\"Nested item 2\") +\n    md.Nested(\n        md.BulletItem(\"Deeply nested\"),\n        \"    \",\n    ),\n    \"  \",\n)`\n\n\tnestedResult := md.BulletItem(\"Parent item\") +\n\t\tmd.Nested(\n\t\t\tmd.BulletItem(\"Nested item 1\")+\n\t\t\t\tmd.BulletItem(\"Nested item 2\")+\n\t\t\t\tmd.Nested(\n\t\t\t\t\tmd.BulletItem(\"Deeply nested\"),\n\t\t\t\t\t\"    \",\n\t\t\t\t),\n\t\t\t\"  \",\n\t\t)\n\n\toutput += md.H3(\"Nested Lists\") +\n\t\tmd.LanguageCodeBlock(\"go\", nestedCode) +\n\t\t\"\\nResult:\\n\" +\n\t\tnestedResult + \"\\n\"\n\n\treturn output\n}\n\nfunc renderLinksAndImages() string {\n\treturn md.H2(\"Links and Images\") + \"\\n\" +\n\t\t// Regular Links\n\t\trenderExample(\n\t\t\t\"Regular Links\",\n\t\t\t`md.Link(\"Gno Homepage\", \"https://gno.land\")`,\n\t\t\tmd.Link(\"Gno Homepage\", \"https://gno.land\"),\n\t\t) +\n\t\t// User Links\n\t\trenderExample(\n\t\t\t\"User Links\",\n\t\t\t`md.UserLink(\"moul\")`,\n\t\t\tmd.UserLink(\"moul\"),\n\t\t) +\n\t\trenderExample(\n\t\t\t\"\",\n\t\t\t`md.UserLink(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")`,\n\t\t\tmd.UserLink(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t) +\n\t\t// Images\n\t\tmd.H3(\"Images\") +\n\t\tmd.LanguageCodeBlock(\"go\", `md.Image(\"Gno Logo\", \"/public/imgs/gnoland.svg\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.Image(\"Gno Logo\", \"/public/imgs/gnoland.svg\") + \"\\n\" +\n\t\t// Clickable Images\n\t\tmd.H3(\"Clickable Images\") +\n\t\tmd.LanguageCodeBlock(\"go\", `md.InlineImageWithLink(\"Click me!\", \"/public/imgs/gnoland.svg\", \"https://gno.land\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.InlineImageWithLink(\"Click me!\", \"/public/imgs/gnoland.svg\", \"https://gno.land\") + \"\\n\"\n}\n\nfunc renderCode() string {\n\treturn md.H2(\"Code\") + \"\\n\" +\n\t\t// Inline Code\n\t\trenderExample(\n\t\t\t\"Inline Code\",\n\t\t\t`\"Use \" + md.InlineCode(\"gno test\") + \" to run tests\"`,\n\t\t\t\"Use \"+md.InlineCode(\"gno test\")+\" to run tests\",\n\t\t) +\n\t\t// Simple Code Block\n\t\tmd.H3(\"Simple Code Block\") +\n\t\tmd.LanguageCodeBlock(\"go\", `md.CodeBlock(\"func main() {\\n    println(\\\"Hello, Gno!\\\")\\n}\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.CodeBlock(\"func main() {\\n    println(\\\"Hello, Gno!\\\")\\n}\") + \"\\n\" +\n\t\t// Language Code Block\n\t\tmd.H3(\"Language-specific Code Block\") +\n\t\tmd.LanguageCodeBlock(\"go\", `md.LanguageCodeBlock(\"go\", \"package main\\n\\nfunc main() {\\n    println(\\\"Hello!\\\")\\n}\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.LanguageCodeBlock(\"go\", \"package main\\n\\nfunc main() {\\n    println(\\\"Hello!\\\")\\n}\") + \"\\n\"\n}\n\nfunc renderBlockquotes() string {\n\treturn md.H2(\"Blockquotes\") +\n\t\tmd.LanguageCodeBlock(\"go\", `md.Blockquote(\"This is an important note.\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.Blockquote(\"This is an important note.\") + \"\\n\" +\n\t\tmd.LanguageCodeBlock(\"go\", `md.Blockquote(\"Multi-line quotes\\nare also supported\\nwith proper formatting.\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.Blockquote(\"Multi-line quotes\\nare also supported\\nwith proper formatting.\") + \"\\n\"\n}\n\nfunc renderAdvancedFeatures() string {\n\toutput := md.H2(\"Advanced Features\") + \"\\n\"\n\n\t// Collapsible Sections\n\toutput += md.H3(\"Collapsible Sections\") +\n\t\tmd.LanguageCodeBlock(\"go\", `md.CollapsibleSection(\"Click to expand\", \"Hidden content here!\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.CollapsibleSection(\"Click to expand\", \"Hidden content here!\") + \"\\n\"\n\n\t// Two Columns\n\tcolumnsCode := `md.Columns([]string{\n    \"Left column content\",\n    \"Right column content\",\n}, false)`\n\n\toutput += md.H3(\"Two Columns\") +\n\t\tmd.LanguageCodeBlock(\"go\", columnsCode) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.Columns([]string{\n\t\t\t\"Left column content\",\n\t\t\t\"Right column content\",\n\t\t}, false) + \"\\n\"\n\n\t// Multiple Columns\n\tmultiColumnsCode := `md.ColumnsN([]string{\n    \"Item 1\", \"Item 2\", \"Item 3\",\n    \"Item 4\", \"Item 5\", \"Item 6\",\n}, 3, true)`\n\n\toutput += md.H3(\"Multiple Columns (3 per row)\") +\n\t\tmd.LanguageCodeBlock(\"go\", multiColumnsCode) +\n\t\t\"\\nResult:\\n\" +\n\t\tmd.ColumnsN([]string{\n\t\t\t\"Item 1\", \"Item 2\", \"Item 3\",\n\t\t\t\"Item 4\", \"Item 5\", \"Item 6\",\n\t\t}, 3, true) + \"\\n\"\n\n\treturn output\n}\n\nfunc renderUtilityFunctions() string {\n\treturn md.H2(\"Utility Functions\") + \"\\n\" +\n\t\t// Horizontal Rule\n\t\trenderExampleWithRawResult(\n\t\t\t\"Horizontal Rule\",\n\t\t\t`md.HorizontalRule()`,\n\t\t\tmd.HorizontalRule(),\n\t\t) +\n\t\t// Footnotes\n\t\tmd.H3(\"Footnotes\") +\n\t\tmd.LanguageCodeBlock(\"go\", `\"This has a footnote[^1].\\n\\n\" + md.FootnoteDefinition(\"1\", \"Footnote content here.\")`) +\n\t\t\"\\nResult:\\n\" +\n\t\t\"This has a footnote[^1].\\n\\n\" +\n\t\tmd.FootnoteDefinition(\"1\", \"Footnote content here.\") + \"\\n\" +\n\t\t// Paragraphs\n\t\trenderExampleWithRawResult(\n\t\t\t\"Paragraphs\",\n\t\t\t`md.Paragraph(\"This ensures proper spacing.\")`,\n\t\t\tmd.Paragraph(\"This ensures proper spacing.\"),\n\t\t)\n}\n\nfunc renderPracticalExample() string {\n\tcode := `// Example function that generates a rich user profile with balanced columns\nfunc RenderProfile(username, name, bio string, avatar string, tasks []string, completed []bool) string {\n    // First row: Avatar/Basic Info | Bio\n    avatarSection := md.Image(name + \" avatar\", avatar) + \"\\n\\n\" +\n        md.H3(\"Basic Info\") +\n        md.BulletList([]string{\n            md.Bold(\"Name:\") + \" \" + name,\n            md.Bold(\"Username:\") + \" \" + md.UserLink(username),\n            md.Bold(\"Status:\") + \" 🟢 Active\",\n            md.Bold(\"Joined:\") + \" January 2024\",\n        })\n    \n    bioSection := md.H3(\"Bio\") +\n        md.Blockquote(bio)\n    \n    // Second row: Tasks | Links\n    tasksSection := md.H3(\"Current Tasks\") +\n        md.TodoList(tasks, completed)\n    \n    linksSection := md.H3(\"Links\") +\n        md.BulletList([]string{\n            md.Link(\"GitHub\", \"https://github.com/\" + username),\n            md.Link(\"Portfolio\", \"https://example.com/\" + username),\n            md.Link(\"LinkedIn\", \"https://linkedin.com/in/\" + username),\n        })\n    \n    // Combine with main title and two sets of columns\n    return md.H1(\"User Profile: \" + name) +\n        md.HorizontalRule() +\n        md.Columns([]string{avatarSection, bioSection}, true) +\n        \"\\n\" +\n        md.Columns([]string{tasksSection, linksSection}, true)\n}\n\n// Usage:\nprofile := RenderProfile(\n    \"johndoe\",\n    \"John Doe\", \n    \"Passionate Gno developer building the future of smart contracts. Love working with blockchain technology and contributing to open source.\",\n    \"/public/imgs/gnoland.svg\",\n    []string{\"Complete Gno tutorial\", \"Build first realm\", \"Deploy to testnet\", \"Write documentation\"},\n    []bool{true, true, false, false},\n)`\n\n\t// Generate actual result - First row\n\tavatarSection := md.Image(\"John Doe avatar\", \"/public/imgs/gnoland.svg\") + \"\\n\\n\" +\n\t\tmd.H3(\"Basic Info\") +\n\t\tmd.BulletList([]string{\n\t\t\tmd.Bold(\"Name:\") + \" John Doe\",\n\t\t\tmd.Bold(\"Username:\") + \" \" + md.UserLink(\"johndoe\"),\n\t\t\tmd.Bold(\"Status:\") + \" 🟢 Active\",\n\t\t\tmd.Bold(\"Joined:\") + \" January 2024\",\n\t\t})\n\n\tbioSection := md.H3(\"Bio\") +\n\t\tmd.Blockquote(\"Passionate Gno developer building the future of smart contracts. Love working with blockchain technology and contributing to open source.\")\n\n\t// Second row\n\ttasksSection := md.H3(\"Current Tasks\") +\n\t\tmd.TodoList(\n\t\t\t[]string{\"Complete Gno tutorial\", \"Build first realm\", \"Deploy to testnet\", \"Write documentation\"},\n\t\t\t[]bool{true, true, false, false},\n\t\t)\n\n\tlinksSection := md.H3(\"Links\") +\n\t\tmd.BulletList([]string{\n\t\t\tmd.Link(\"GitHub\", \"https://github.com/johndoe\"),\n\t\t\tmd.Link(\"Portfolio\", \"https://example.com/johndoe\"),\n\t\t\tmd.Link(\"LinkedIn\", \"https://linkedin.com/in/johndoe\"),\n\t\t})\n\n\tresult := md.H1(\"User Profile: John Doe\") +\n\t\tmd.HorizontalRule() +\n\t\tmd.Columns([]string{avatarSection, bioSection}, true) +\n\t\t\"\\n\" +\n\t\tmd.Columns([]string{tasksSection, linksSection}, true)\n\n\treturn md.H2(\"Practical Example\") +\n\t\tmd.Paragraph(\"Here's a complete example showing how to build a rich user profile page using columns, images, and various md features:\") +\n\t\t\"\\n\" +\n\t\tmd.LanguageCodeBlock(\"go\", code) +\n\t\t\"\\nResult when called with the above parameters:\\n\" +\n\t\tresult + \"\\n\"\n}\n\nfunc renderBestPractices() string {\n\tpractices := []string{\n\t\t\"Prioritize code readability over performance in Render() - use clear variable names, logical grouping, and readable concatenation for humans\",\n\t\t\"Use the md package for all programmatic markdown generation in Render() methods\",\n\t\t\"Combine functions for complex formatting: \" + md.InlineCode(\"md.Bold(md.Italic(\\\"text\\\"))\"),\n\t\t\"Use \" + md.InlineCode(\"md.Paragraph()\") + \" to ensure proper spacing between elements\",\n\t\t\"Leverage \" + md.InlineCode(\"md.ColumnsN()\") + \" for responsive layouts\",\n\t}\n\n\treturn md.H2(\"Best Practices\") +\n\t\tmd.OrderedList(practices) +\n\t\t\"\\n\"\n}\n\nfunc renderSeeAlso() string {\n\tlinks := []string{\n\t\tmd.Link(\"gno.land/p/moul/md\", \"https://gno.land/p/moul/md\") + \" - The md package source code\",\n\t\tmd.Link(\"Markdown on Gno\", \"https://gno.land/r/docs/markdown\") + \" - Comprehensive guide on Gno Flavored Markdown syntax\",\n\t}\n\n\treturn md.H2(\"See Also\") +\n\t\tmd.BulletList(links)\n}\n\n// Helper function to render an example with code and result\nfunc renderExample(title, code, result string) string {\n\toutput := \"\"\n\tif title != \"\" {\n\t\toutput += md.H3(title)\n\t}\n\toutput += md.LanguageCodeBlock(\"go\", code) + \"\\n\"\n\toutput += md.Paragraph(\"Result: \"+result) + \"\\n\"\n\treturn output\n}\n\n// Helper function for examples where result needs raw output\nfunc renderExampleWithRawResult(title, code, result string) string {\n\toutput := md.H3(title)\n\toutput += md.LanguageCodeBlock(\"go\", code) + \"\\n\"\n\toutput += \"Result:\\n\" + result + \"\\n\"\n\treturn output\n}\n"},{"name":"moul_md_filetest.gno","body":"// Package main\n// This is a filetest. It's somewhat similar to Go's 'Example' files/functions. It's there to showcase functionality and print the output of a specific scenario related to the package it's testing. In this case, it's related to the `Render` function.\npackage main\n\nimport \"gno.land/r/docs/moul_md\"\n\nfunc main() {\n\t// Test the main Render function\n\tprintln(moul_md.Render(\"\"))\n}\n\n// Output:\n// # Package Demo: `p/moul/md`\n// This document demonstrates the features of the [gno\\.land/p/moul/md](https://gno.land/p/moul/md) package, showing both the source code and the rendered result for each feature.\n//\n//\n//\n// \u003e **Note:** The md package provides helper functions to generate markdown programmatically, making it easier to create dynamic documentation and content.\n//\n//\n// ## Text Formatting\n//\n// ### Bold Text\n// ```go\n// md.Bold(\"Important text\")\n// ```\n//\n// Result: **Important text**\n//\n//\n// ### Italic Text\n// ```go\n// md.Italic(\"Emphasized text\")\n// ```\n//\n// Result: *Emphasized text*\n//\n//\n// ### Strikethrough Text\n// ```go\n// md.Strikethrough(\"Deprecated feature\")\n// ```\n//\n// Result: ~~Deprecated feature~~\n//\n//\n// ### Combined Formatting\n// ```go\n// md.Bold(md.Italic(\"Very important\"))\n// ```\n//\n// Result: ***Very important***\n//\n//\n// ## Headers\n// The package supports all six levels of markdown headers:\n//\n//\n// ```go\n// md.H1(\"Main Title\")\n// md.H2(\"Section\")\n// md.H3(\"Subsection\")\n// md.H4(\"Sub-subsection\")\n// md.H5(\"Minor heading\")\n// md.H6(\"Smallest heading\")\n// ```\n//\n// Result:\n// # Main Title\n// ## Section\n// ### Subsection\n// #### Sub-subsection\n// ##### Minor heading\n// ###### Smallest heading\n//\n// ## Lists\n//\n// ### Bullet Lists\n// ```go\n// items := []string{\n//     \"First item\",\n//     \"Second item\",\n//     \"Third item with\\nmultiple lines\",\n// }\n// md.BulletList(items)\n// ```\n//\n// Result:\n// - First item\n// - Second item\n// - Third item with\n//   multiple lines\n//\n// ### Ordered Lists\n// ```go\n// steps := []string{\n//     \"First step\",\n//     \"Second step\",\n//     \"Third step with\\nadditional details\",\n// }\n// md.OrderedList(steps)\n// ```\n//\n// Result:\n// 1. First step\n// 2. Second step\n// 3. Third step with\n//    additional details\n//\n// ### Todo Lists\n// ```go\n// tasks := []string{\n//     \"Completed task\",\n//     \"Another completed task\",\n//     \"Pending task\",\n//     \"Another pending task\",\n// }\n// completed := []bool{true, true, false, false}\n// md.TodoList(tasks, completed)\n// ```\n//\n// Result:\n// - [x] Completed task\n// - [x] Another completed task\n// - [ ] Pending task\n// - [ ] Another pending task\n//\n// ### Nested Lists\n// ```go\n// md.BulletItem(\"Parent item\") +\n// md.Nested(\n//     md.BulletItem(\"Nested item 1\") +\n//     md.BulletItem(\"Nested item 2\") +\n//     md.Nested(\n//         md.BulletItem(\"Deeply nested\"),\n//         \"    \",\n//     ),\n//     \"  \",\n// )\n// ```\n//\n// Result:\n// - Parent item\n//   - Nested item 1\n//   - Nested item 2\n//       - Deeply nested\n//\n// ## Links and Images\n//\n// ### Regular Links\n// ```go\n// md.Link(\"Gno Homepage\", \"https://gno.land\")\n// ```\n//\n// Result: [Gno Homepage](https://gno.land)\n//\n//\n// ### User Links\n// ```go\n// md.UserLink(\"moul\")\n// ```\n//\n// Result: [@moul](/u/moul)\n//\n//\n// ```go\n// md.UserLink(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n// ```\n//\n// Result: [g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5](/u/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5)\n//\n//\n// ### Images\n// ```go\n// md.Image(\"Gno Logo\", \"/public/imgs/gnoland.svg\")\n// ```\n//\n// Result:\n// ![Gno Logo](/public/imgs/gnoland.svg)\n// ### Clickable Images\n// ```go\n// md.InlineImageWithLink(\"Click me!\", \"/public/imgs/gnoland.svg\", \"https://gno.land\")\n// ```\n//\n// Result:\n// [![Click me\\!](/public/imgs/gnoland.svg)](https://gno.land)\n// ## Code\n//\n// ### Inline Code\n// ```go\n// \"Use \" + md.InlineCode(\"gno test\") + \" to run tests\"\n// ```\n//\n// Result: Use `gno test` to run tests\n//\n//\n// ### Simple Code Block\n// ```go\n// md.CodeBlock(\"func main() {\\n    println(\\\"Hello, Gno!\\\")\\n}\")\n// ```\n//\n// Result:\n// ```\n// func main() {\n//     println(\"Hello, Gno!\")\n// }\n// ```\n//\n// ### Language-specific Code Block\n// ```go\n// md.LanguageCodeBlock(\"go\", \"package main\\n\\nfunc main() {\\n    println(\\\"Hello!\\\")\\n}\")\n// ```\n//\n// Result:\n// ```go\n// package main\n//\n// func main() {\n//     println(\"Hello!\")\n// }\n// ```\n//\n// ## Blockquotes\n// ```go\n// md.Blockquote(\"This is an important note.\")\n// ```\n//\n// Result:\n//\n// \u003e This is an important note.\n//\n//\n// ```go\n// md.Blockquote(\"Multi-line quotes\\nare also supported\\nwith proper formatting.\")\n// ```\n//\n// Result:\n//\n// \u003e Multi-line quotes\n// \u003e are also supported\n// \u003e with proper formatting.\n//\n//\n// ## Advanced Features\n//\n// ### Collapsible Sections\n// ```go\n// md.CollapsibleSection(\"Click to expand\", \"Hidden content here!\")\n// ```\n//\n// Result:\n// \u003cdetails\u003e\u003csummary\u003eClick to expand\u003c/summary\u003e\n//\n// Hidden content here!\n// \u003c/details\u003e\n//\n// ### Two Columns\n// ```go\n// md.Columns([]string{\n//     \"Left column content\",\n//     \"Right column content\",\n// }, false)\n// ```\n//\n// Result:\n// \u003cgno-columns\u003e\n// Left column content\n// \u003cgno-columns-sep\u003e\n// Right column content\n// \u003c/gno-columns\u003e\n//\n// ### Multiple Columns (3 per row)\n// ```go\n// md.ColumnsN([]string{\n//     \"Item 1\", \"Item 2\", \"Item 3\",\n//     \"Item 4\", \"Item 5\", \"Item 6\",\n// }, 3, true)\n// ```\n//\n// Result:\n// \u003cgno-columns\u003e\n// Item 1\n// \u003cgno-columns-sep\u003e\n// Item 2\n// \u003cgno-columns-sep\u003e\n// Item 3\n// \u003c/gno-columns\u003e\n// \u003cgno-columns\u003e\n// Item 4\n// \u003cgno-columns-sep\u003e\n// Item 5\n// \u003cgno-columns-sep\u003e\n// Item 6\n// \u003c/gno-columns\u003e\n//\n// ## Utility Functions\n//\n// ### Horizontal Rule\n// ```go\n// md.HorizontalRule()\n// ```\n//\n// Result:\n// ---\n//\n// ### Footnotes\n// ```go\n// \"This has a footnote[^1].\\n\\n\" + md.FootnoteDefinition(\"1\", \"Footnote content here.\")\n// ```\n//\n// Result:\n// This has a footnote[^1].\n//\n// [^1]:\n//     Footnote content here.\n//\n// ### Paragraphs\n// ```go\n// md.Paragraph(\"This ensures proper spacing.\")\n// ```\n//\n// Result:\n// This ensures proper spacing.\n//\n//\n// ## Practical Example\n// Here's a complete example showing how to build a rich user profile page using columns, images, and various md features:\n//\n//\n// ```go\n// // Example function that generates a rich user profile with balanced columns\n// func RenderProfile(username, name, bio string, avatar string, tasks []string, completed []bool) string {\n//     // First row: Avatar/Basic Info | Bio\n//     avatarSection := md.Image(name + \" avatar\", avatar) + \"\\n\\n\" +\n//         md.H3(\"Basic Info\") +\n//         md.BulletList([]string{\n//             md.Bold(\"Name:\") + \" \" + name,\n//             md.Bold(\"Username:\") + \" \" + md.UserLink(username),\n//             md.Bold(\"Status:\") + \" 🟢 Active\",\n//             md.Bold(\"Joined:\") + \" January 2024\",\n//         })\n//\n//     bioSection := md.H3(\"Bio\") +\n//         md.Blockquote(bio)\n//\n//     // Second row: Tasks | Links\n//     tasksSection := md.H3(\"Current Tasks\") +\n//         md.TodoList(tasks, completed)\n//\n//     linksSection := md.H3(\"Links\") +\n//         md.BulletList([]string{\n//             md.Link(\"GitHub\", \"https://github.com/\" + username),\n//             md.Link(\"Portfolio\", \"https://example.com/\" + username),\n//             md.Link(\"LinkedIn\", \"https://linkedin.com/in/\" + username),\n//         })\n//\n//     // Combine with main title and two sets of columns\n//     return md.H1(\"User Profile: \" + name) +\n//         md.HorizontalRule() +\n//         md.Columns([]string{avatarSection, bioSection}, true) +\n//         \"\\n\" +\n//         md.Columns([]string{tasksSection, linksSection}, true)\n// }\n//\n// // Usage:\n// profile := RenderProfile(\n//     \"johndoe\",\n//     \"John Doe\",\n//     \"Passionate Gno developer building the future of smart contracts. Love working with blockchain technology and contributing to open source.\",\n//     \"/public/imgs/gnoland.svg\",\n//     []string{\"Complete Gno tutorial\", \"Build first realm\", \"Deploy to testnet\", \"Write documentation\"},\n//     []bool{true, true, false, false},\n// )\n// ```\n//\n// Result when called with the above parameters:\n// # User Profile: John Doe\n// ---\n// \u003cgno-columns\u003e\n// ![John Doe avatar](/public/imgs/gnoland.svg)\n//\n// ### Basic Info\n// - **Name:** John Doe\n// - **Username:** [@johndoe](/u/johndoe)\n// - **Status:** 🟢 Active\n// - **Joined:** January 2024\n//\n// \u003cgno-columns-sep\u003e\n// ### Bio\n//\n// \u003e Passionate Gno developer building the future of smart contracts. Love working with blockchain technology and contributing to open source.\n//\n//\n// \u003cgno-columns-sep\u003e\n//\n// \u003cgno-columns-sep\u003e\n//\n// \u003c/gno-columns\u003e\n//\n// \u003cgno-columns\u003e\n// ### Current Tasks\n// - [x] Complete Gno tutorial\n// - [x] Build first realm\n// - [ ] Deploy to testnet\n// - [ ] Write documentation\n//\n// \u003cgno-columns-sep\u003e\n// ### Links\n// - [GitHub](https://github.com/johndoe)\n// - [Portfolio](https://example.com/johndoe)\n// - [LinkedIn](https://linkedin.com/in/johndoe)\n//\n// \u003cgno-columns-sep\u003e\n//\n// \u003cgno-columns-sep\u003e\n//\n// \u003c/gno-columns\u003e\n//\n// ## Best Practices\n// 1. Prioritize code readability over performance in Render() - use clear variable names, logical grouping, and readable concatenation for humans\n// 2. Use the md package for all programmatic markdown generation in Render() methods\n// 3. Combine functions for complex formatting: `md.Bold(md.Italic(\"text\"))`\n// 4. Use `md.Paragraph()` to ensure proper spacing between elements\n// 5. Leverage `md.ColumnsN()` for responsive layouts\n//\n// ## See Also\n// - [gno\\.land/p/moul/md](https://gno.land/p/moul/md) - The md package source code\n// - [Markdown on Gno](https://gno.land/r/docs/markdown) - Comprehensive guide on Gno Flavored Markdown syntax\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"6tnENt+Fef/yTcYSy+v33+U1ptXPU5+AagTMeone1dhIZuXCaU+KllqGUkkvL/N1bVnTs0Z9bnH0VnaET1KyAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"optional_render","path":"gno.land/r/docs/optional_render","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/optional_render\"\ngno = \"0.9\"\n"},{"name":"optional_render.gno","body":"package optional_render\n\nfunc Info() string {\n\treturn `Having a Render() function in your realm is optional!\nIf you do decide to have a Render() function, it must have the following signature:\nfunc Render(path string) string { ... }`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"dOyACXhrjj80QpoKq0g5+mGoLiFcqsxulIXHaw/HI4jEbeEP5+Utwce19rQFLFHfKrg5syJuxxPW1YpBbYwzCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"private","path":"gno.land/r/docs/private","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/private\"\ngno = \"0.9\"\nprivate = true\n"},{"name":"private.gno","body":"package private\n\nimport \"strings\"\n\n// Documentation for private realms in Gno.\nfunc Render(path string) string {\n\toutput := `# Private Realms\n\n## Introduction\n\nThis document describes the ±private± flag in ±gnomod.toml±.  \nThis flag is **optional** and can only be used by **realms**.\n\n\u003e [!NOTE]\n\u003e The ±private± flag does not make a realm secret or hidden.  \n\u003e All code remains open-source and visible on-chain.\n\n## Declaring a Private Realm\n\nTo mark a realm as private, add the following ±private± value in your ±gnomod.toml± file:\n\n±±±toml\nmodule = \"gno.land/r/demo/privaterealm\"\ngno = \"0.9\"\nprivate = true\n±±±\n\n## Rules of a Private Realm\n\nA realm marked as private follows strict rules:\n\n- It cannot be imported by other packages.\n- It can be re-uploaded and the new version fully overwrites the old one.\n- Memory, pointers, or types defined in this package cannot be stored elsewhere.\n\nThese restrictions aim to achieve one main goal:\n\n\u003e [!TIP]\n\u003e **Private realms are swappable.**  \n\u003e You can redeploy or update them without impacting other realms.  \n\n## Use Cases\n\nPrivate realms are useful when:\n\n- The realm represents a **home realm** for a specific user (±r/username/home±).\n- The realm is **designed to evolve** and may need to be replaced later.\n- You want to ensure that no external state depends on its internal types or values.\n`\n\toutput = strings.ReplaceAll(output, \"±\", \"`\")\n\treturn output\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"3k2XkKlB57gyoil8G/OIVw3sTDD73nUjiXsv5v/imxAEtwF2Lb0s76roQhRnI5U5PxtsAsVZ4Q/Iwo5tRBRVAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"registry","path":"gno.land/r/docs/registry","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/registry\"\ngno = \"0.9\"\n"},{"name":"registry.gno","body":"// Package registry is a service registry.\n// Callers register, update, and remove named entries stored in an avl tree.\n// Each entry is owned by the address that created it.\npackage registry\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/markdown/sanitize/v0\"\n)\n\n// Service represents a registered service\ntype Service struct {\n\tName        string\n\tDescription string\n\tEndpoint    string\n\tOwner       address\n\tActive      bool\n}\n\nvar services avl.Tree // key (owner:name) -\u003e *Service\n\nconst (\n\tmaxNameLength        = 50\n\tmaxDescriptionLength = 200\n\tmaxEndpointLength    = 500\n)\n\n// Register adds a new service to the registry\nfunc Register(cur realm, name, description, endpoint string) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tname = strings.TrimSpace(name)\n\tif name == \"\" {\n\t\tpanic(\"name cannot be empty\")\n\t}\n\tif len(name) \u003e maxNameLength {\n\t\tpanic(\"name too long\")\n\t}\n\tif len(description) \u003e maxDescriptionLength {\n\t\tpanic(\"description too long\")\n\t}\n\tif len(endpoint) \u003e maxEndpointLength {\n\t\tpanic(\"endpoint too long\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tkey := caller.String() + \":\" + name\n\n\tif services.Has(key) {\n\t\tpanic(\"service with this name from this address already exists\")\n\t}\n\n\tservice := \u0026Service{\n\t\tName:        name,\n\t\tDescription: description,\n\t\tEndpoint:    endpoint,\n\t\tOwner:       caller,\n\t\tActive:      true,\n\t}\n\n\tservices.Set(key, service)\n}\n\n// Update updates an existing service (owner only)\nfunc Update(cur realm, name, description, endpoint string) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tif len(description) \u003e maxDescriptionLength {\n\t\tpanic(\"description too long\")\n\t}\n\tif len(endpoint) \u003e maxEndpointLength {\n\t\tpanic(\"endpoint too long\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tkey := caller.String() + \":\" + name\n\n\tval := services.Get(key)\n\tif val == nil {\n\t\tpanic(\"service not found\")\n\t}\n\n\tservice := val.(*Service)\n\tservice.Description = description\n\tservice.Endpoint = endpoint\n\tservices.Set(key, service)\n}\n\n// Deactivate marks a service as inactive (owner only)\nfunc Deactivate(cur realm, name string) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous().Address()\n\tkey := caller.String() + \":\" + name\n\n\tval := services.Get(key)\n\tif val == nil {\n\t\tpanic(\"service not found\")\n\t}\n\n\tservice := val.(*Service)\n\tservice.Active = false\n\tservices.Set(key, service)\n}\n\n// Delete removes a service permanently (owner only)\nfunc Delete(cur realm, name string) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\tcaller := cur.Previous().Address()\n\tkey := caller.String() + \":\" + name\n\n\tif !services.Has(key) {\n\t\tpanic(\"service not found\")\n\t}\n\n\tservices.Remove(key)\n}\n\n// Lookup returns a copy of a service by owner address and name. Returning a\n// value (not the stored *Service) keeps callers from mutating registry state\n// through the returned pointer. See /r/docs/mistakes.\nfunc Lookup(owner address, name string) (Service, bool) {\n\tkey := owner.String() + \":\" + name\n\tval := services.Get(key)\n\tif val == nil {\n\t\treturn Service{}, false\n\t}\n\treturn *val.(*Service), true\n}\n\n// GetAllServices returns copies of all services, like Lookup.\nfunc GetAllServices() []Service {\n\tvar result []Service\n\tservices.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\tresult = append(result, *value.(*Service))\n\t\treturn false\n\t})\n\treturn result\n}\n\nfunc Render(path string) string {\n\tif path != \"\" {\n\t\t// Path format: \"address:name\". Split on the first colon only, since a\n\t\t// bech32 address never contains one but a name might.\n\t\tparts := strings.SplitN(path, \":\", 2)\n\t\tif len(parts) != 2 {\n\t\t\treturn \"# Invalid Path\\n\\nPath should be in format `address:name`.\"\n\t\t}\n\n\t\towner := address(parts[0])\n\t\tname := parts[1]\n\t\tservice, ok := Lookup(owner, name)\n\t\tif !ok {\n\t\t\treturn \"# Service Not Found\\n\\nNo service with address `\" + sanitize.InlineText(parts[0]) + \"` and name `\" + sanitize.InlineText(name) + \"` is registered.\"\n\t\t}\n\t\treturn renderService(service)\n\t}\n\n\treturn renderHome()\n}\n\nfunc renderHome() string {\n\tout := `# Registry Pattern\n\nAllows entities to register themselves in a shared\nindex. Other contracts can then look up registered entities.\nCommon use cases include mapping data, token discovery and realm showcasing.\n\n## Code Pattern\n\n` + \"```go\" + `\npackage registry\n\nimport (\n    \"gno.land/p/nt/avl/v0\"\n)\n\ntype Entry struct {\n    Name  string\n    Owner address\n    Data  string // Your custom payload\n}\n\nvar (\n    entries        avl.Tree // key (owner:name) -\u003e *Entry\n)\n\nfunc Register(cur realm, name, data string) {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    caller := cur.Previous().Address()\n    key := caller.String() + \":\" + name\n\n    if entries.Has(key) {\n        panic(\"you already have an entry with this name\")\n    }\n\n    entries.Set(key, \u0026Entry{\n        Name:  name,\n        Owner: caller,\n        Data:  data,\n    })\n}\n\n// Return a copy, not the stored *Entry.\nfunc Lookup(owner address, name string) (Entry, bool) {\n    key := owner.String() + \":\" + name\n    val := entries.Get(key)\n    if val == nil {\n        return Entry{}, false\n    }\n    return *val.(*Entry), true\n}\n` + \"```\" + `\n\n\n## This Example\n\nThis service registry allows anyone to register a service with a unique name.\nOnly the owner can update or deactivate their service, but anyone can look up\nservices by name.\n\n## Live Demo\n\n[Register Service](/r/docs/registry$help\u0026func=Register)\n\n### Registered Services\n\n`\n\tcount := services.Size()\n\n\tif count == 0 {\n\t\tout += \"_No services registered yet._\\n\"\n\t} else {\n\t\tservices.Iterate(\"\", \"\", func(key string, value any) bool {\n\t\t\tservice := value.(*Service)\n\t\t\tstatus := \"✅\"\n\t\t\tif !service.Active {\n\t\t\t\tstatus = \"❌\"\n\t\t\t}\n\t\t\tout += \"- \" + status + \" [\" + sanitize.InlineText(service.Name) + \"](\" + sanitize.URL(\"/r/docs/registry:\"+key) + \")\\n\"\n\t\t\treturn false\n\t\t})\n\t}\n\n\tout += `\n---\n\n\n## Examples\n\n- [r/sys/users](/r/sys/users) - Official username registry. Users register\ntheir username and address, and anyone can look them up.\n\n- [r/demo/defi/grc20reg](/r/demo/defi/grc20reg) - GRC20 token registry.\nToken realms register themselves so other contracts can discover available tokens.\n\n- [r/leon/hor](/r/leon/hor) - Hall of Realms. Realms register themselves by\ncalling ` + \"`hor.Register()`\" + ` from their init function to be showcased in the exhibition.\n\n[View source code](/r/docs/registry$source)\n`\n\treturn out\n}\n\nfunc renderService(service Service) string {\n\tstatus := \"Active ✅\"\n\tif !service.Active {\n\t\tstatus = \"Inactive ❌\"\n\t}\n\n\treturn `# Service: ` + sanitize.InlineText(service.Name) + `\n\n## Details\n\n| Property | Value |\n|----------|-------|\n| Status | ` + status + ` |\n| Description | ` + sanitize.TableCell(service.Description) + ` |\n| Endpoint | ` + sanitize.TableCell(service.Endpoint) + ` |\n| Owner | ` + service.Owner.String() + ` |\n\n---\n\n[Back to Registry](/r/docs/registry)\n`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Vlztjqcw2hpwOVYpcCXAiZIW02yCLJYEV05sMOIDY5lReWlDz3a3ohHHMIR1SPcnkc7Uic06N81oXL1eiVheDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"resolveusers","path":"gno.land/r/docs/resolveusers","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/resolveusers\"\ngno = \"0.9\"\n"},{"name":"resolve.gno","body":"package resolveusers\n\nimport (\n\t\"gno.land/r/sys/users\" // Import the user registry\n)\n\nfunc Render(path string) string {\n\tout := \"# Username checker\\n\\n\"\n\n\t// Default render\n\tif path == \"\" {\n\t\tout += `This is short example on how developers can use the gno.land user registry to resolve\nusernames and addresses in their realms. Check out the example below.\n\n---\n\n`\n\t\tout += \"Add `:{name OR address}` to the search bar to check for a name or address.\\n\\n\"\n\t\tout += \"Here are some examples:\\n\\n\"\n\t\tout += \"- [@test1](/r/docs/resolveusers:test1)\\n\"\n\t\tout += \"- [g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5](/r/docs/resolveusers:g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5)\\n\"\n\n\t\treturn out\n\t}\n\n\t// Try to resolve the search input in the registry\n\tdata, _ := users.ResolveAny(path)\n\tif data != nil {\n\t\tout += \"## Found the user you're looking for: \"\n\t\t// RenderLink will return a clickable gnoweb link leading to the user's page\n\t\tout += data.RenderLink(\"\")\n\t\treturn out\n\t}\n\n\t// Note: the above can be done with known inputs as well, ie ResolveName \u0026 ResolveAddress.\n\n\tout += \"## Didn't find that user :/\"\n\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Qccgww2MG9rGTsQdAmTQW2N/6wT+M32efXn6aYUFPvzB+zjk9W83zJhbciP3Z397HtY8Hh5X+lifBqSdmtzjDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"routing","path":"gno.land/r/docs/routing","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/routing\"\ngno = \"0.9\"\n"},{"name":"routing.gno","body":"package routing\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/sys/users\"\n)\n\nfunc Render(path string) string {\n\t// Initialize the router\n\trouter := mux.NewRouter()\n\n\t// Then, pass specific path patterns and their function handlers\n\t// The handler functions need to have a specific signature:\n\t// func(*mux.ResponseWriter, *mux.Request)\n\n\t// Below are some examples for specific path patterns and their handlers\n\n\t// When no render path is passed, ie the root render\n\trouter.HandleFunc(\"\", homeHandler)\n\n\t// When a specific render path is passed\n\trouter.HandleFunc(\"staticpage\", staticpageHandler)\n\n\t// When a render path with a variable is passed, ie `addr`\n\trouter.HandleFunc(\"user/{name}\", profileHandler)\n\n\t// When a wildcard path is passed\n\trouter.HandleFunc(\"wildcard/*/\", wildcardHandler)\n\n\t// Finally, Pass the render path to the router\n\treturn router.Render(path)\n}\n\nfunc homeHandler(res *mux.ResponseWriter, _ *mux.Request) {\n\tout := \"# Routing\\n\\n\"\n\n\tout += `This short example showcases how the [p/demo/mux](/p/demo/mux) package works.\nThis pure package aims to offer similar functionality to [**http.ServeMux**](https://pkg.go.dev/net/http#ServeMux) in Go, but for Gno's **Render()** requests.\n\nThis home page is handled by the homeHandler function. Check out the examples below for more ways\nto use this package:\n- [A static page](/r/docs/routing:staticpage)\n- [A path handling path variables, such as user pages](/r/docs/routing:user/test1)\n- [A path handling wildcard](/r/docs/routing:wildcard/wildcardhelp/whatever)\n`\n\n\t// Write to the result at the end\n\tres.Write(out)\n}\n\nfunc staticpageHandler(res *mux.ResponseWriter, req *mux.Request) {\n\tout := \"# Welcome to the static page!\\n\\n\"\n\tout += \"There isn't much on this page, but it's cool because shows you that using query parameters is also possible!\\n\\n\"\n\tout += \"Try adding [`?gno_is=value`](/r/docs/routing:staticpage?gno_is=cool) to the URL.\\n\\n\"\n\n\t// We can use the Query API to get the values of query parameters\n\tval := req.Query.Get(\"gno_is\")\n\tif val != \"\" {\n\t\tout += ufmt.Sprintf(\"You inputted a value for the parameter `gno_is`: **%s**\\n\\n\", val)\n\n\t}\n\n\tout += \"### Multiple values for the same parameter\\n\\n\"\n\tout += \"The same parameter can have [more than one value](/r/docs/routing:staticpage?gno_is=cool\u0026gno_is=transparent).\\n\\n\"\n\tout += \"We can access it via the `*mux.Request.Query` map.\\n\\n\"\n\n\tres.Write(out)\n}\n\nfunc profileHandler(res *mux.ResponseWriter, req *mux.Request) {\n\tout := \"# User display page\\n\\n\"\n\n\t// Integrate with r/sys/users to get user data\n\tname := req.GetVar(\"name\")\n\tuserData, _ := users.ResolveName(name)\n\tif userData == nil {\n\t\tout += \"This name does not exist in the [User Registry](/r/sys/users)!\"\n\t\tres.Write(out)\n\t\treturn\n\t}\n\n\tout += \"Found an address matching the name:\\n\\n\"\n\tout += userData.RenderLink(\"\")\n\tres.Write(out)\n}\n\nfunc wildcardHandler(res *mux.ResponseWriter, req *mux.Request) {\n\tout := \"# Wildcard handler\\n\\n\"\n\n\t// Use GetVar(\"*\") to access the wildcard part of the route\n\twild := req.GetVar(\"*\")\n\tout += ufmt.Sprintf(\"This page matched a wildcard route.\\n\\n\")\n\tout += ufmt.Sprintf(\"**Wildcard path:** `%s`\\n\\n\", wild)\n\n\tout += \"You can use this to match and handle arbitrarily nested paths.\\n\\n\"\n\tout += \"Try visiting:\\n\"\n\tout += \"- [/r/docs/routing:wildcard/foo/bar](/r/docs/routing:wildcard/foo/bar)\\n\"\n\tout += \"- [/r/docs/routing:wildcard/a/b/c](/r/docs/routing:wildcard/a/b/c)\\n\"\n\n\t// Optionally split the wildcard into segments\n\tsegments := strings.Split(wild, \"/\")\n\tout += \"\\n### Wildcard segments:\\n\"\n\tfor i, seg := range segments {\n\t\tout += ufmt.Sprintf(\"- Segment %d: `%s`\\n\", i+1, seg)\n\t}\n\n\tres.Write(out)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"zqArAbrADv1d6LQGWmkaT0frHGyAHQ0U8yOGXp81ktfugwP/sUANXrZ5oW2xliVMZ5ssFNtJ8Vbe1pc+wlFdCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"safeobjects","path":"gno.land/r/docs/safeobjects","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/safeobjects\"\ngno = \"0.9\"\n"},{"name":"safeobjects.gno","body":"package safeobjects\n\nimport (\n\t\"chain/runtime/unsafe\"\n)\n\n// ExposedSafeObject methods exposed to the public. However, the methods all\n// contain a caller check - making them, and the object itself \"safe\".\n// The methods are only callable by the current admin.\n// A prime example of a safe object can be found in \"p/demo/ownable\".\nvar ExposedSafeObject = SafeObject{\n\tprivilegedData: \"This message can only be set by the admin.\",\n\tadmin:          address(\"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"),\n}\n\n// SafeObject is an object that contains some privileged data\n// This data might be privileged because only certain users\n// have the edit rights, or for other reasons.\ntype SafeObject struct {\n\tprivilegedData string\n\tadmin          address\n}\n\n// Set is a function only the admin can call\nfunc (so SafeObject) Set(value string) {\n\tif unsafe.CurrentRealm().Address() != so.admin {\n\t\tpanic(\"caller is not authorized\")\n\t}\n\n\tso.privilegedData = value\n}\n\n// UpdateAdmin is a function only the admin can call\nfunc (so SafeObject) UpdateAdmin(newAdmin address) {\n\tif unsafe.CurrentRealm().Address() != so.admin {\n\t\tpanic(\"caller is not authorized\")\n\t}\n\n\tif !newAdmin.IsValid() {\n\t\tpanic(\"new admin address is invalid\")\n\t}\n\n\tso.admin = newAdmin\n}\n\n// Get is callable by anyone\nfunc (so SafeObject) Get() string {\n\treturn so.privilegedData\n}\n\nfunc Render(_ string) string {\n\treturn `\n# Safe Objects\n\n**Safe Objects** are objects that can be exposed at the realm top level\nwhile still keeping the write access to their memory limited.\n\nSafe Objects allow only authorized users (like admins) can modify their\ninternal state, even if they are exposed at the realm top level.\n\nA prime example of a commonly used safe object is the Ownable object, found under in [**p/demo/ownable**](/p/demo/ownable).\n\nTo call methods on exposed safe objects, users need to use [MsgRun](/r/docs/complexargs).\n\n---\n\n` + \"`r/docs/safeobjects.ExposedSafeObject`\" + ` values:\n- Message: ` + ExposedSafeObject.privilegedData + `\n- Admin: ` + ExposedSafeObject.admin.String()\n\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Ib70UUX7dLpKzkCWwOR4w49HN61oY7kV+4ef3QVi/c5nTYOlztZJMiV1HuNVRjmtXzXhf4BlZAdyqP26fqCiBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"soliditypatterns","path":"gno.land/r/docs/soliditypatterns","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/soliditypatterns\"\ngno = \"0.9\"\n"},{"name":"soliditypatterns.gno","body":"package soliditypatterns\n\nfunc Render(_ string) string {\n\tcontent := `# Common Solidity Patterns\n\nThis is a list of common Solidity patterns, converted and explained to match Gno best practices.\n\n## Examples\n\n- [Basic Counter](/r/docs/soliditypatterns/counter) - Increment and decrement a counter stored in a contract.\n- [Only Owner can change](/r/docs/soliditypatterns/ownable) - Simple example of ownership over a contract.\n- [State Lock](/r/docs/soliditypatterns/statelock) - Modifiers.\n- [Sending Coins (Banker)](/r/docs/soliditypatterns/banker) - The Gno equivalent of Solidity's payable and ETH transfers.\n- [Reentrancy](/r/docs/soliditypatterns/reentrancy) - Why reentrancy attacks are NOT possible in Gno.\n\n\u003e [!NOTE]\n\u003e  If you have experience with Solidity, or a common pattern is missing from this list, please add more examples to the [Monorepo](https://github.com/gnolang/gno/tree/master/examples/gno.land/r/docs/soliditypatterns)!\n\n`\n\treturn content\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"u+5A4DCpHqz+j5EDK3tCZMaUlQh09ecAZfYvfzPKiptwQDHcE+NPdQY93f/WcSWY+58EekUSzdi+CKGUOeGNBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"banker","path":"gno.land/r/docs/soliditypatterns/banker","files":[{"name":"banker.gno","body":"// Package banker recreates Solidity's payable/withdraw pattern in Gno.\n// It accepts deposits and lets callers withdraw their balance through the chain banker.\npackage banker\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"chain/runtime/unsafe\"\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\n// This example demonstrates the Gno equivalent of Solidity's\n// payable functions and ETH transfers.\n\nvar deposits avl.Tree // address -\u003e int64\n\n// Deposit accepts coins and tracks the sender's balance\n// In Solidity, this would be a payable function\nfunc Deposit(cur realm) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\n\t// Require a direct user call so coins are credited to the real sender.\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tsent := unsafe.OriginSend()\n\n\t// Check that coins were sent\n\tamount := sent.AmountOf(\"ugnot\")\n\tif amount \u003c= 0 {\n\t\tpanic(\"must send some ugnot\")\n\t}\n\n\t// Get current balance\n\tcurrentBalance := int64(0)\n\tif val := deposits.Get(caller.String()); val != nil {\n\t\tcurrentBalance = val.(int64)\n\t}\n\n\t// Update balance\n\tdeposits.Set(caller.String(), currentBalance+amount)\n}\n\n// Withdraw sends coins back to the caller\n// This is equivalent to Solidity's transfer() or call{value: x}()\nfunc Withdraw(cur realm, amount int64) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"invalid realm\")\n\t}\n\n\t// Require a direct user call so only the balance owner can withdraw.\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"invalid non-user call\")\n\t}\n\n\t// A negative amount would inflate the balance and send negative coins.\n\tif amount \u003c= 0 {\n\t\tpanic(\"amount must be positive\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\n\t// Get current balance\n\tcurrentBalance := int64(0)\n\tif val := deposits.Get(caller.String()); val != nil {\n\t\tcurrentBalance = val.(int64)\n\t}\n\n\t// Check balance\n\tif currentBalance \u003c amount {\n\t\tpanic(\"insufficient balance\")\n\t}\n\n\tnewBalance := currentBalance - amount\n\tif newBalance == 0 {\n\t\tdeposits.Remove(caller.String())\n\t} else {\n\t\tdeposits.Set(caller.String(), newBalance)\n\t}\n\n\t// Create a banker to send coins\n\tbnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\n\t// Get the realm's address\n\trealmAddr := cur.Address()\n\n\t// Send coins from realm to caller\n\tbnk.SendCoins(realmAddr, caller, chain.Coins{chain.NewCoin(\"ugnot\", amount)})\n}\n\n// GetBalance returns the deposited balance for an address\nfunc GetBalance(addr address) int64 {\n\tbalance := deposits.Get(addr.String())\n\tif balance == nil {\n\t\treturn 0\n\t}\n\treturn balance.(int64)\n}\n\n// GetRealmBalance returns the total coins held by this realm\nfunc GetRealmBalance() int64 {\n\tbnk := banker.NewReadonlyBanker()\n\trealmAddr := unsafe.CurrentRealm().Address()\n\tcoins := bnk.GetCoins(realmAddr)\n\treturn coins.AmountOf(\"ugnot\")\n}\n\nfunc RenderDemo() string {\n\tout := \"## Live Demo\\n\\n\"\n\tout += \"Realm Balance: **\" + strconv.FormatInt(GetRealmBalance(), 10) + \" ugnot**\\n\\n\"\n\tout += \"[Deposit](/r/docs/soliditypatterns/banker$help\u0026func=Deposit) | \"\n\tout += \"[Withdraw](/r/docs/soliditypatterns/banker$help\u0026func=Withdraw)\\n\"\n\treturn out\n}\n\nfunc Render(_ string) string {\n\treturn `# Sending Coins (Banker)\n\nIn Solidity, you send ETH using ` + \"`transfer()`\" + `, ` + \"`send()`\" + `, or ` + \"`call{value: x}()`\" + `.\nIn Gno, you use the **Banker** to send coins.\n\n` + RenderDemo() + `\n## Solidity vs Gno Comparison\n\n### Solidity: Receiving ETH\n\n` + \"```solidity\" + `\n// Payable function receives ETH\nfunction deposit() public payable {\n    balances[msg.sender] += msg.value;\n}\n` + \"```\" + `\n\n### Gno: Receiving Coins\n\n` + \"```go\" + `\nimport \"chain/runtime/unsafe\"\n\nvar deposits map[address]int64\n\nfunc Deposit(cur realm) {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    caller := cur.Previous().Address()\n    sent := unsafe.OriginSend()\n\n    // Unlike Solidity's single msg.value, Gno supports multiple coin types\n    // A user can send ugnot, custom tokens, or both in one transaction\n    amount := sent.AmountOf(\"ugnot\")\n    deposits[caller] += amount\n}\n` + \"```\" + `\n\n\u003e **Note:** This example uses a map for simplicity. In production, prefer an AVL tree\n\u003e for better performance. See [source code](/r/docs/soliditypatterns/banker$source).\n\n---\n\n### Solidity: Sending ETH\n\n` + \"```solidity\" + `\n// Using transfer (reverts on failure)\npayable(recipient).transfer(amount);\n\n// Using call\n(bool success, ) = recipient.call{value: amount}(\"\");\nrequire(success, \"Transfer failed\");\n` + \"```\" + `\n\n### Gno: Sending Coins\n\n` + \"```go\" + `\nimport (\n    \"chain\"\n    \"chain/banker\"\n    \"chain/runtime/unsafe\"\n)\n\nvar deposits map[address]int64\n\nfunc Withdraw(cur realm, amount int64) {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    caller := cur.Previous().Address()\n\n    // Update state before sending (checks-effects-interactions)\n    deposits[caller] -= amount\n\n    // Create banker with send capability\n    bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\n    // Send coins\n    realmAddr := cur.Address()\n    bnk.SendCoins(realmAddr, caller, chain.Coins{chain.NewCoin(\"ugnot\", amount)})\n}\n` + \"```\" + `\n\n## Banker Types\n\n| Type | Purpose |\n|------|---------|\n| ` + \"`BankerTypeReadonly`\" + ` | Read balances only |\n| ` + \"`BankerTypeRealmSend`\" + ` | Send coins from realm |\n| ` + \"`BankerTypeRealmIssue`\" + ` | Issue new realm-specific tokens |\n\n## Key Differences from Solidity\n\nGno requires an explicit Banker with appropriate permissions.\nReentrancy attacks are not possible (see [Reentrancy](/r/docs/soliditypatterns/reentrancy)).\nMultiple coin denominations are supported, not just one native token.\n\n[View source code](/r/docs/soliditypatterns/banker$source)\n\n---\n\n[Back to Solidity Patterns](/r/docs/soliditypatterns) | [Banker Documentation](https://docs.gno.land/resources/gno-stdlibs/#banker)\n`\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/soliditypatterns/banker\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"DlvtmwGr3Acr+FphNtbLaqkWZu92AqlzYQszcJVbAsIVCpHNERrYxM0HpfE+WBxBWHghEr62PTWvVRJ8MGEwDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"counter","path":"gno.land/r/docs/soliditypatterns/counter","files":[{"name":"demo.gno","body":"package counter\n\nimport \"strconv\"\n\nvar count int64 = 0\n\nfunc Increment(cur realm) {\n\tcount++\n}\n\nfunc Decrement(cur realm) {\n\tcount--\n}\n\nfunc GetCount(cur realm) int64 {\n\treturn count\n}\n\nfunc RenderDemo() string {\n\tcontent := \"## Live Demo\\n\"\n\tcontent += \"Counter Value : **\" + strconv.FormatInt(count, 10) + \"**\\n\\n\"\n\tcontent += \"[Increment](counter$help\u0026func=Increment)\\n\\n\"\n\tcontent += \"[Decrement](counter$help\u0026func=Decrement)\\n\\n\"\n\treturn content\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/soliditypatterns/counter\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package counter\n\nimport \"strings\"\n\nfunc Render(path string) string {\n\tcontent := `\n# Counter\n\nThis Solidity contract defines a counter that can be incremented or decremented by anyone.\n\n---\n\n### Solidity Version\n\n^^^solidity\ncontract Counter {\n    int public count = 0;\n\n    function increment() public {\n        count++;\n    }\n\n    function decrement() public {\n        count--;\n    }\n}\n^^^\n\n#### Explanation\n\nThis Solidity contract does:\n\n* Declare a ^count^ variable readable by anyone initialized at 0\n* Define two functions, ^increment^ and ^decrement^, that increase and decrease the counter by 1.\n* These functions are marked ^public^, meaning anyone can call them\n\n---\n\n### Gno Version\n\n^^^go\npackage counter\n\nvar count int64 = 0\n\nfunc Increment(cur realm) {\n    count++\n}\n\nfunc Decrement(cur realm) {\n    count--\n}\n^^^\n\n* We define a global variable ^count^ of type ^int64^. Since it is declared at the package level, its value is **persistent**, just like in Solidity. As it is capitalized, it can be accessed from outside the package (read-only).\n* We provide two functions: ^Increment^ and ^Decrement^, which modify the counter value using Go-style syntax (^count++^ and ^count--^). As they are capitalized, they can be called from outside the package.\n \n---\n`\n\treturn strings.ReplaceAll(content+RenderDemo(), \"^\", \"`\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"djey4J9VhbH1UPn2jpGTH+EG8UOwPicU3xawegByy55uLBwfYukwLiC6jVP6HzVYdK/eL1tlcMldZ//BJ6TeCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"ownable","path":"gno.land/r/docs/soliditypatterns/ownable","files":[{"name":"demo.gno","body":"package ownable\n\nimport (\n\t\"chain/runtime/unsafe\"\n)\n\nvar owner address = \"\"\n\nfunc InitOwner(cur realm) {\n\tif len(owner) != 0 {\n\t\tpanic(\"owner already defined\")\n\t}\n\towner = cur.Previous().Address()\n}\n\nfunc assertOnlyOwner() {\n\tif unsafe.PreviousRealm().Address() != owner {\n\t\tpanic(\"caller isn't the owner\")\n\t}\n}\n\nfunc ChangeOwner(cur realm, newOwner address) {\n\tassertOnlyOwner()\n\towner = newOwner\n}\n\nfunc RenderDemo() string {\n\tcontent := \"## Live Demo\\n\"\n\tcontent += \"Owner address: **\" + owner.String() + \"**\\n\\n\"\n\tcontent += \"[InitOwner](ownable$help\u0026func=InitOwner)\\n\\n\"\n\tcontent += \"[ChangeOwner](ownable$help\u0026func=ChangeOwner)\\n\\n\"\n\treturn content\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/soliditypatterns/ownable\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package ownable\n\nimport \"strings\"\n\nfunc Render(path string) string {\n\tcontent := `\n# Ownable\n\nThis Solidity contract defines a counter that can be incremented or decremented by anyone.\n\n---\n\n### Solidity Version\n\n^^^solidity\ncontract OnlyOwnerCanChange {\n    address private owner;\n\n    constructor() {\n        owner = msg.sender;\n    }\n\n    modifier onlyOwner {\n        require(msg.sender == owner, \"Not owner\");\n        _;\n    }\n\n    function changeOwner(address memory newOwner) public onlyOwner {\n        owner = newOwner\n    }\n}\n^^^\n\n* It declares a state variable ^owner^ of type ^address^, which stores the address of the account that deployed the contract.\n* The ^constructor^ sets the ^owner^ variable to the address that deployed the contract using ^msg.sender^.\n* It defines a ^modifier onlyOwner^ that restricts function execution to the owner only, using ^require^ to check that the caller is the current ^owner^. The special symbol ^_;^ is where the modified function's code runs.\n* It defines a ^changeOwner^ function that allows the current owner to assign a new owner address. The function uses the ^onlyOwner^ modifier to enforce access control.\n\n---\n## Gno Version\n\n^^^go\npackage onlyownercanchange\n\nimport \"chain/runtime/unsafe\"\n\nvar owner address = \"\"\n\nfunc InitOwner(cur realm) {\n\tif len(owner) != 0 {\n\t\tpanic(\"owner already defined\")\n\t}\n\towner = unsafe.PreviousRealm().Address()\n}\n\nfunc assertOnlyOwner() {\n\tif unsafe.PreviousRealm().Address() != owner {\n\t\tpanic(\"caller isn't the owner\")\n\t}\n}\n\nfunc ChangeOwner(cur realm, newOwner address) {\n\tassertOnlyOwner()\n\towner = newOwner\n}\n^^^\n\n* We declare a persistent global variable ^owner^ of type ^address^ (an alias for ^string^ in Gno), initialized to the empty string ^\"\"^. This holds the address of the contract owner.\n* The function ^InitOwner^ is used to initialize the ^owner^. It can only be called once: if ^owner^ is already set (non-empty), it throws an error using ^panic^. It sets the ^owner^ to the address of the previous realm (the caller) via ^unsafe.PreviousRealm().Address()^, which is similar to ^msg.sender^ in Solidity.\n* The helper function ^assertOnlyOwner^ checks that the caller is the current ^owner^ by comparing ^unsafe.PreviousRealm().Address()^ with ^owner^. If not, it panics. This is equivalent to a Solidity ^modifier onlyOwner^.\n* The function ^ChangeOwner^ allows the current owner to transfer ownership to a new address. It first calls ^assertOnlyOwner()^ to ensure that only the current owner can do this.\n* All functions are capitalized (exported), meaning they can be called externally. However, ^assertOnlyOwner^ is unexported (lowercase), meaning it's only usable within the package.\n\n\n\u003e [!NOTE]\n\u003e For a library that handles this for you, check out [^/p/demo/ownable^](/p/demo/ownable).\n\n---\n`\n\treturn strings.ReplaceAll(content+RenderDemo(), \"^\", \"`\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"7TQyXW1l43xG+LlciSI/22lN1rVLrc0Pl2QbSk6szY7XKhRf0k2KoFmKfl8caLfAA6Jot6k5Ky4jo1uknsOqDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"reentrancy","path":"gno.land/r/docs/soliditypatterns/reentrancy","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/soliditypatterns/reentrancy\"\ngno = \"0.9\"\n"},{"name":"reentrancy.gno","body":"// Package reentrancy explains why Gno is not exposed to Solidity-style reentrancy.\n// It shows how to keep transfers safe.\npackage reentrancy\n\nfunc Render(_ string) string {\n\treturn `# Reentrancy in Gno\n\nIn Solidity, sending ETH automatically triggers the recipient's ` + \"`receive()`\" + ` or ` + \"`fallback()`\" + `\nfunction. An attacker can exploit this to call back into the original contract before state\nupdates complete. The infamous DAO hack used this to drain $60M.\n\n## The Solidity Problem\n\n` + \"```solidity\" + `\ncontract VulnerableVault {\n    mapping(address =\u003e uint) balances;\n\n    function withdraw() public {\n        uint amount = balances[msg.sender];\n        msg.sender.call{value: amount}(\"\");  // 1. Sends ETH, triggering attacker's receive()\n        balances[msg.sender] = 0;            // 3. State update happens too late!\n    }\n}\n\ncontract Attacker {\n    VulnerableVault vault;\n\n    receive() external payable {\n        if (address(vault).balance \u003e= 1 ether) {\n            vault.withdraw();  // 2. Re-enters withdraw() while balance is still non-zero\n        }\n    }\n}\n` + \"```\" + `\n\n## Why Gno is Safe\n\nCoin transfers in Gno do **not** execute code on the recipient. Unlike Solidity's\n` + \"`receive()`\" + ` and ` + \"`fallback()`\" + ` hooks, Gno realm code only runs through explicit\n` + \"`cross`\" + ` function calls—eliminating the reentrancy attack vector entirely.\n\n` + \"```go\" + `\nfunc Withdraw(cur realm, amount int64) {\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    // Require a direct user call so only the balance owner can withdraw.\n    if !cur.Previous().IsUserCall() {\n        panic(\"invalid non-user call\")\n    }\n\n    caller := cur.Previous().Address()\n\n    bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n    bnk.SendCoins(cur.Address(), caller, chain.Coins{chain.NewCoin(\"ugnot\", amount)})  // No code executes on recipient\n    deposits[caller] -= amount  // Safe - no reentrancy possible\n}\n` + \"```\" + `\n\n## Best Practice: Checks-Effects-Interactions\n\nEven though Gno prevents reentrancy attacks, following the **Checks-Effects-Interactions**\npattern keeps your code clear and maintainable:\n\n` + \"```go\" + `\nfunc Withdraw(cur realm, amount int64) {\n    // 1. Checks - validate all preconditions\n    if !cur.IsCurrent() {\n        panic(\"invalid realm\")\n    }\n    if !cur.Previous().IsUserCall() {\n        panic(\"invalid non-user call\")\n    }\n\n    caller := cur.Previous().Address()\n    balance := deposits[caller]\n    if balance \u003c amount {\n        panic(\"insufficient balance\")\n    }\n\n    // 2. Effects - update state before external calls\n    deposits[caller] -= amount\n\n    // 3. Interactions - perform external operations last\n    bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n    bnk.SendCoins(cur.Address(), caller, chain.Coins{chain.NewCoin(\"ugnot\", amount)})\n}\n` + \"```\" + `\n\nThis pattern remains a best practice for code clarity and future-proofing, even when\nreentrancy isn't a concern.\n\n---\n\n\n## See Also\n\n- [Banker](/r/docs/soliditypatterns/banker) - Coin transfer patterns\n`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"kc2WJtSrt07PBruflFg6SZ82lRUIM/jH9uPpDBKjx2t19Q7WmjxYbROx4nsmFJVGKQTpVkal3NQKh9i7xu0+Cw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"statelock","path":"gno.land/r/docs/soliditypatterns/statelock","files":[{"name":"demo.gno","body":"package statelock\n\ntype State string\n\nconst (\n\tStateActive   State = \"Active\"\n\tStateLocked   State = \"Locked\"\n\tStateDisabled State = \"Disabled\"\n)\n\nvar state State = StateActive\n\nfunc assertInState(expected State) {\n\tif state != expected {\n\t\tpanic(\"invalid state: expected \" + expected + \", got \" + state)\n\t}\n}\n\nfunc Lock(cur realm) {\n\tassertInState(StateActive)\n\tstate = StateLocked\n}\n\nfunc Unlock(cur realm) {\n\tassertInState(StateLocked)\n\tstate = StateActive\n}\n\nfunc RenderDemo() string {\n\tcontent := \"## Live Demo\\n\"\n\tcontent += \"Contract State : **\" + string(state) + \"**\\n\\n\"\n\tcontent += \"[Lock](statelock$help\u0026func=Lock)\\n\\n\"\n\tcontent += \"[Unlock](statelock$help\u0026func=Unlock)\\n\\n\"\n\treturn content\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/soliditypatterns/statelock\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package statelock\n\nimport \"strings\"\n\nfunc Render(path string) string {\n\tcontent := `\n# State Lock\n\nThis example showcases how a Solidity modifier might be implemented in Gno.\n\n---\n\n## In Solidity\n\n^^^solidity\ncontract StateLock {\n    enum State { Active, Locked, Disabled }\n\n    State public state = State.Active;\n\n    modifier inState(State _state) {\n        require(state == _state);\n        _;\n    }\n\n    function lock() public inState(State.Active) {\n        state = State.Locked;\n    }\n\n    function unlock() public inState(State.Locked) {\n        state = State.Active;\n    }\n}\n^^^\n\n* An ^enum State^ is declared with three possible values: ^Active^, ^Locked^, and ^Disabled^.\n* A ^public^ state variable ^state^ is declared to hold the current state. It can be read externally thanks to the ^public^ keyword.\n* A ^modifier inState^ is defined to restrict function execution based on the current ^state^. It checks that ^state == _state^ using ^require^, and then allows the function body to continue.\n* The ^lock^ function can only be called if the contract is in the ^Created^ state. When called, it changes the state to ^Locked^.\n\n\n---\n\n## Gno Version\n\n^^^go\ntype State string\n\nconst (\n\tStateActive   State = \"Active\"\n\tStateLocked   State = \"Locked\"\n\tStateDisabled State = \"Disabled\"\n)\n\nvar state State = StateActive\n\nfunc assertInState(expected State) {\n\tif state != expected {\n\t\tpanic(\"invalid state: expected \" + expected + \", got \" + state)\n\t}\n}\n\nfunc Lock(cur realm) {\n\tassertInState(StateActive)\n\tstate = StateLocked\n}\n\nfunc Unlock(cur realm) {\n\tassertInState(StateLocked)\n\tstate = StateActive\n}\n^^^\n\n* We define a custom type ^State^ as a ^string^ to simulate enum-like behavior.\n* We declare a ^const^ block listing all valid states: ^StateActive^, ^StateLocked^, and ^StateDisabled^. These are as the Solidity ^Enum^.\n* A global variable ^state^ of type ^State^ is initialized to ^StateActive^.\n* The helper function ^assertInState^ ensures that the contract is in the expected state. If not, it throws an error using ^panic^, showing both the expected and actual state values. This is equivalent to a Solidity ^modifier^.\n* The ^Lock^ function changes the state from ^Active^ to ^Locked^. It first ensures the state is currently ^Active^ using ^assertInState^.\n* The ^Unlock^ function reverses the state from ^Locked^ to ^Active^, again checking the current state before proceeding.\n\n---\n\n`\n\treturn strings.ReplaceAll(content+RenderDemo(), \"^\", \"`\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"8YmDeqqFTEUZIDBir+MFua20q+8rkK7GmOBmxzJ2Kf1RisUA5d1LiSi+knBsukUodJ8lBcNQLOURpfZtbhJoDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"source","path":"gno.land/r/docs/source","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/source\"\ngno = \"0.9\"\n"},{"name":"source.gno","body":"package source\n\n// Welcome to the source code of this realm!\n\nfunc Render(_ string) string {\n\treturn `# Viewing source code  \ngno.land makes it easy to view the source code of any pure\npackage or realm, by using ABCI queries.\n\ngno.land's web frontend, ` + \"`gnoweb`, \" + ` makes this easy by\nproviding a intuitive UI that fetches the source of the\nrealm, that you can inspect anywhere by simply clicking\non the [source] button.\n\nCheck it out in the top right corner!\n`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"f5fnCH9dAB/7razcY8AfLa0rS7tXbxYRjqBnUCxEXMBVgvYXXVevQxRXmwzvOc2nRA6Hffg/fzFD61aQhzsMAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"tdd","path":"gno.land/r/docs/tdd","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/tdd\"\ngno = \"0.9\"\n"},{"name":"tdd.gno","body":"// Package tdd demonstrates test-driven development with a small calculator.\npackage tdd\n\n// Calculator provides simple arithmetic operations\n\nvar lastResult int64\n\nfunc Add(_ realm, a, b int64) int64 {\n\tlastResult = a + b\n\treturn lastResult\n}\n\nfunc Subtract(_ realm, a, b int64) int64 {\n\tlastResult = a - b\n\treturn lastResult\n}\n\nfunc Multiply(_ realm, a, b int64) int64 {\n\tlastResult = a * b\n\treturn lastResult\n}\n\nfunc Divide(_ realm, a, b int64) int64 {\n\tif b == 0 {\n\t\tpanic(\"division by zero\")\n\t}\n\tlastResult = a / b\n\treturn lastResult\n}\n\nfunc GetLastResult() int64 {\n\treturn lastResult\n}\n\nfunc Render(_ string) string {\n\treturn `# Test-Driven Development (TDD) in Gno\n\nTDD is writing tests BEFORE writing code.\n\n## The TDD Cycle\n\n1. **Red** - Write a failing test\n2. **Green** - Write minimal code to pass\n3. **Refactor** - Improve while keeping tests green\n\n## Quick Example\n\nFirst, write the test. A function that changes realm state takes a cur realm\nparameter, so the test takes one too and calls it across the realm boundary\nwith cross(cur):\n\n` + \"```go\" + `\nimport \"testing\"\n\n// calculator_test.gno\nfunc TestAdd(cur realm, t *testing.T) {\n    result := Add(cross(cur), 2, 3)\n    if result != 5 {\n        t.Errorf(\"Add(2, 3) = %d; want 5\", result)\n    }\n}\n` + \"```\" + `\n\nThen write the code:\n\n` + \"```go\" + `\n// calculator.gno\nfunc Add(_ realm, a, b int64) int64 {\n    return a + b\n}\n` + \"```\" + `\n\n## Table-Driven Tests\n\nTest multiple cases efficiently:\n\n` + \"```go\" + `\nfunc TestAdd(cur realm, t *testing.T) {\n    tests := []struct {\n        a, b, want int64\n    }{\n        {2, 3, 5},\n        {-2, -3, -5},\n        {0, 5, 5},\n    }\n\n    for _, tt := range tests {\n        got := Add(cross(cur), tt.a, tt.b)\n        if got != tt.want {\n            t.Errorf(\"Add(%d, %d) = %d; want %d\",\n                tt.a, tt.b, got, tt.want)\n        }\n    }\n}\n` + \"```\" + `\n\n## Testing Panics\n\nA panic that crosses a realm boundary aborts the program, so it can't be caught\nwith recover. Use the revive builtin to assert that a cross-call panics:\n\n` + \"```go\" + `\nfunc TestDivideByZero(cur realm, t *testing.T) {\n    rec := revive(func() {\n        Divide(cross(cur), 10, 0)\n    })\n    if rec == nil {\n        t.Error(\"expected panic\")\n    }\n}\n` + \"```\" + `\n\n## Testing with Addresses\n\n` + \"```go\" + `\nimport \"gno.land/p/nt/testutils/v0\"\n\nfunc TestWithCaller(cur realm, t *testing.T) {\n    addr := testutils.TestAddress(\"user1\")\n    testing.SetOriginCaller(addr)\n\n    result := SomeFunctionThatChecksCaller(cross(cur))\n    // Assert...\n}\n` + \"```\" + `\n\n## Testing with Coins\n\n` + \"```go\" + `\nfunc TestWithCoins(cur realm, t *testing.T) {\n    testing.SetOriginSend(chain.Coins{chain.NewCoin(\"ugnot\", 1000)})\n\n    result := SomePayableFunction(cross(cur))\n    // Assert...\n}\n` + \"```\" + `\n\n## Running Tests\n\n` + \"```bash\" + `\ngno test .              # Run all tests\ngno test -v .           # Verbose output\ngno test -run TestAdd . # Run specific test\n` + \"```\" + `\n\n---\n\n### See also:\n- [gno test documentation](https://docs.gno.land/reference/gno-cli/gno-test)\n- [Crossing](/r/docs/crossing) - How to test realm boundaries\n- [Common Mistakes](/r/docs/mistakes) - Pitfalls to avoid\n`\n}\n"},{"name":"tdd_test.gno","body":"package tdd\n\nimport \"testing\"\n\nfunc TestAdd(cur realm, t *testing.T) {\n\ttests := []struct {\n\t\tname     string\n\t\ta, b     int64\n\t\texpected int64\n\t}{\n\t\t{\"positive numbers\", 2, 3, 5},\n\t\t{\"negative numbers\", -2, -3, -5},\n\t\t{\"mixed signs\", -2, 3, 1},\n\t\t{\"zero\", 0, 5, 5},\n\t}\n\n\tfor _, tt := range tests {\n\t\tresult := Add(cross(cur), tt.a, tt.b)\n\t\tif result != tt.expected {\n\t\t\tt.Errorf(\"%s: Add(%d, %d) = %d; want %d\",\n\t\t\t\ttt.name, tt.a, tt.b, result, tt.expected)\n\t\t}\n\t}\n}\n\nfunc TestSubtract(cur realm, t *testing.T) {\n\tresult := Subtract(cross(cur), 5, 3)\n\tif result != 2 {\n\t\tt.Errorf(\"Subtract(5, 3) = %d; want 2\", result)\n\t}\n}\n\nfunc TestMultiply(cur realm, t *testing.T) {\n\tresult := Multiply(cross(cur), 4, 5)\n\tif result != 20 {\n\t\tt.Errorf(\"Multiply(4, 5) = %d; want 20\", result)\n\t}\n}\n\nfunc TestDivide(cur realm, t *testing.T) {\n\tresult := Divide(cross(cur), 10, 2)\n\tif result != 5 {\n\t\tt.Errorf(\"Divide(10, 2) = %d; want 5\", result)\n\t}\n}\n\nfunc TestGetLastResult(cur realm, t *testing.T) {\n\tAdd(cross(cur), 7, 8)\n\tif GetLastResult() != 15 {\n\t\tt.Errorf(\"GetLastResult() = %d; want 15\", GetLastResult())\n\t}\n\n\tMultiply(cross(cur), 3, 4)\n\tif GetLastResult() != 12 {\n\t\tt.Errorf(\"GetLastResult() = %d; want 12\", GetLastResult())\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"/8oRZhMNjP9QpDHpIRf1+nnIQ24UarshKTpw+AP85epJLoYdJcn0+HL7DwIeCoM+aNS+nMew3SBTlbw9hnSDDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"txlink","path":"gno.land/r/docs/txlink","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/txlink\"\ngno = \"0.9\"\n"},{"name":"txlink.gno","body":"package txlink\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/moul/txlink\"\n)\n\ntype Message struct {\n\tAuthor string\n\tText   string\n\tTime   time.Time\n}\n\nvar messages []Message\n\n// Post saves a message with the caller as the author\nfunc Post(cur realm, text string) {\n\tmsg := Message{\n\t\tAuthor: cur.Previous().Address().String(),\n\t\tText:   text,\n\t\tTime:   time.Now(),\n\t}\n\tmessages = append(messages, msg)\n}\n\nfunc Render(_ string) string {\n\tout := \"# Transaction Links\\n\\n\"\n\tout += `Transaction links (\"txlinks\" for short) are a standardized way of creating\nURLs that Gno-enabled wallets can interpret and generate transactions from.\n\nThey allow developers to turn basic hyperlinks into realm calls\nwith optional arguments and funds attached.\n\nArguments with empty values are treated as _wallet-settable_, allowing the\nuser to customize them before submitting the transaction.\n\nYou can also link to functions in _other realms_, using the pkgpath of the realm in question.\n\nCheck out the links below.\n\n`\n\n\tout += \"## Post a Message\\n\"\n\n\t// Basic call\n\tout += \"- [Say Hello](\" + txlink.Call(\"Post\", \"text\", \"Hello from txlink!\") + \") - A simple, static argument call\\n\"\n\n\t// A link builder including some funds specified in the string format (chain.Coin.String())\n\tcustom := txlink.NewLink(\"Post\").\n\t\tAddArgs(\"text\", \"Support this board with 1ugnot\").\n\t\tSetSend(\"1ugnot\").\n\t\tURL()\n\tout += \"- [Send Supportive Message](\" + custom + \") - A more complex call with coins included\\n\"\n\n\t// Up to the user to set the argument\n\tout += \"- [Write Your Own Message](\" + txlink.NewLink(\"Post\").\n\t\tAddArgs(\"text\", \"\"). // Should be editable in the user's wallet\n\t\tURL() + \") - A user-defined argument call\\n\"\n\n\t// Cross-realm example\n\tcrossRealm := txlink.Realm(\"gno.land/r/docs/minisocial/v2\").\n\t\tNewLink(\"CreatePost\").\n\t\tAddArgs(\"text\", \"Hello from `/r/docs/txlink`!\").\n\t\tURL()\n\tout += \"- [Say Hello on MiniSocial V2!](\" + crossRealm + \") - A call to another realm\\n\"\n\n\tout += \"\\n## Recent Messages\\n\"\n\tif len(messages) == 0 {\n\t\tout += \"_No messages yet._\\n\\n\"\n\t\treturn out\n\t}\n\n\tfor i := len(messages) - 1; i \u003e= 0 \u0026\u0026 i \u003e= len(messages)-5; i-- {\n\t\tmsg := messages[i]\n\t\tout += \"- **\" + msg.Author + \"**: \" + msg.Text + \" (\" + msg.Time.Format(\"15:04:05\") + \")\\n\"\n\t}\n\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"A58CkaTyykpM6AQ3YeOJoS37Pv7WskZL3VkFBr2TKUw8rDJxsQDr/akWDgvc8LRbLqmGGVop/wtxL0UXNWujCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"userprofile","path":"gno.land/r/docs/userprofile","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/docs/userprofile\"\ngno = \"0.9\"\n"},{"name":"userprofile.gno","body":"// Package userprofile shows how to integrate /r/sys/users with /r/demo/profile.\n// It reads a user's profile.\npackage userprofile\n\nimport (\n\t\"gno.land/p/nt/markdown/sanitize/v0\"\n\t\"gno.land/r/demo/profile\"\n\t\"gno.land/r/sys/users\"\n)\n\n// This example demonstrates using both r/sys/users and r/demo/profile\n// to create a rich user experience.\n\nfunc Render(path string) string {\n\tif path == \"\" {\n\t\treturn renderHome()\n\t}\n\n\treturn renderUser(path)\n}\n\nfunc renderHome() string {\n\treturn `# User \u0026 Profile Integration\n\nThis example demonstrates how to use both [` + \"`/r/sys/users`\" + `](/r/sys/users) and [` + \"`/r/demo/profile`\" + `](/r/demo/profile)\nto create rich user experiences in your realm.\n\n## /r/sys/users\n\nThe [user registry](/r/sys/users) provides:\n- Username registration\n- Address ↔ Username resolution\n\n` + \"```go\" + `\nimport \"gno.land/r/sys/users\"\n\n// Resolve a username to user data\ndata, _ := users.ResolveName(\"alice\")\naddr := data.Addr()\n\n// Resolve an address to user data\ndata = users.ResolveAddress(someAddress)\nname := data.Name()\n\n// Resolve either (smart detection)\ndata, _ = users.ResolveAny(\"alice\")\ndata, _ = users.ResolveAny(\"g1...\")\n` + \"```\" + `\n\n## /r/demo/profile\n\nThe [profile registry](/r/demo/profile) provides:\n- Display name\n- Avatar\n- Bio\n- Custom fields\n\n[Set your profile →](/r/demo/profile$help\u0026func=SetStringField)\n\n` + \"```go\" + `\nimport \"gno.land/r/demo/profile\"\n\n// Get profile fields\ndisplayName := profile.GetStringField(addr, profile.DisplayName, \"Anonymous\")\navatar := profile.GetStringField(addr, profile.Avatar, \"\")\nbio := profile.GetStringField(addr, profile.Bio, \"\")\n\n// Set profile fields (for current user)\nprofile.SetStringField(cross(cur), profile.DisplayName, \"Alice\")\nprofile.SetStringField(cross(cur), profile.Bio, \"Gno developer\")\n` + \"```\" + `\n\n## Combined Usage Pattern\n\n` + \"```go\" + `\nfunc RenderUser(nameOrAddr string) string {\n    // First, resolve the user\n    userData, _ := users.ResolveAny(nameOrAddr)\n    if userData == nil {\n        return \"User not found\"\n    }\n\n    addr := userData.Addr()\n    name := userData.Name()\n\n    // Then, get their profile\n    displayName := profile.GetStringField(addr, profile.DisplayName, name)\n    avatar := profile.GetStringField(addr, profile.Avatar, \"\")\n    bio := profile.GetStringField(addr, profile.Bio, \"No bio\")\n\n    // Render combined info\n    out := \"# \" + displayName + \"\\n\"\n    if avatar != \"\" {\n        out += \"![avatar](\" + avatar + \")\\n\"\n    }\n    out += bio + \"\\n\"\n    out += \"\\nUsername: @\" + name + \"\\n\"\n    out += \"Address: \" + addr.String() + \"\\n\"\n\n    return out\n}\n` + \"```\" + `\n\n## Live Demo\n\nTry looking up at test user:\n- [@test1](/r/docs/userprofile:test1)\n\n---\n\n### See also:\n- [/r/sys/users](/r/sys/users) - User registry\n- [/r/demo/profile](/r/demo/profile) - Profile data\n`\n}\n\nfunc renderUser(nameOrAddr string) string {\n\tout := \"# User Lookup\\n\\n\"\n\n\t// Try to resolve the user\n\tuserData, _ := users.ResolveAny(nameOrAddr)\n\tif userData == nil {\n\t\tout += \"User not found: `\" + sanitize.InlineText(nameOrAddr) + \"`\\n\\n\"\n\t\tout += \"[Back to Home](/r/docs/userprofile)\\n\"\n\t\treturn out\n\t}\n\n\taddr := userData.Addr()\n\tname := userData.Name()\n\n\tout += \"## Found User\\n\\n\"\n\n\t// User registry data\n\tout += \"### Registry Data (r/sys/users)\\n\\n\"\n\tout += \"| Field | Value |\\n\"\n\tout += \"|-------|-------|\\n\"\n\tout += \"| Username | @\" + sanitize.TableCell(name) + \" |\\n\"\n\tout += \"| Address | `\" + addr.String() + \"` |\\n\"\n\tout += \"| Profile Link | \" + userData.RenderLink(\"\") + \" |\\n\"\n\tout += \"\\n\"\n\n\t// Profile data\n\tout += \"### Profile Data (r/demo/profile)\\n\\n\"\n\n\tdisplayName := profileCell(profile.GetStringField(addr, profile.DisplayName, \"\"))\n\tavatar := profileCell(profile.GetStringField(addr, profile.Avatar, \"\"))\n\tbio := profileCell(profile.GetStringField(addr, profile.Bio, \"\"))\n\n\tout += \"| Field | Value |\\n\"\n\tout += \"|-------|-------|\\n\"\n\tout += \"| Display Name | \" + displayName + \" |\\n\"\n\tout += \"| Avatar | \" + avatar + \" |\\n\"\n\tout += \"| Bio | \" + bio + \" |\\n\"\n\tout += \"\\n\"\n\n\tout += \"---\\n\\n\"\n\tout += \"[Back to Home](/r/docs/userprofile)\\n\"\n\n\treturn out\n}\n\n// profileCell sanitizes a profile field for a markdown table cell, keeping\n// the \"_not set_\" placeholder when the field is empty.\nfunc profileCell(v string) string {\n\tif v == \"\" {\n\t\treturn \"_not set_\"\n\t}\n\treturn sanitize.TableCell(v)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"SQ6TyR+Fb+1cmm1C46IXT/uddHznaAdbnrGRzbifG1Lo+M2mK8W0yeuWNJDfwRoKcFWpi5JBi/LTCU8RN4u7AA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"gnomaze","path":"gno.land/r/gfanton/gnomaze","files":[{"name":"asset.gno","body":"package gnomaze\n\ntype svg string\n\nvar svgassets = map[string]svg{\n\t\"banana\": `\u003cg id=\"SVGRepo_iconCarrier\"\u003e\u003cpath d=\"M746.666667 234.666667c21.333333 0 149.333333-21.333333 149.333333 170.666666S618.666667 938.666667 234.666667 938.666667c-42.666667 0-85.333333-21.333333-85.333334-21.333334l-21.333333-64s21.333333-85.333333 149.333333-149.333333 218.944-117.632 273.344-172.010667C593.344 489.344 661.333333 405.333333 661.333333 341.333333c0-41.770667 42.666667-85.333333 42.666667-85.333333\" fill=\"#FFE082\"\u003e\u003c/path\u003e\u003cpath d=\"M170.666667 874.666667s149.333333-42.666667 277.333333-106.666667 405.333333-256 320-533.333333c0 0 128-21.333333 128 170.666666S618.666667 938.666667 234.666667 938.666667c-42.666667 0-85.333333-21.333333-85.333334-21.333334l21.333334-42.666666z\" fill=\"#FFCA28\"\u003e\u003c/path\u003e\u003cpath d=\"M876.010667 297.344C836.010667 211.562667 746.666667 256 746.666667 85.333333c-64 0-106.666667 42.666667-106.666667 42.666667s64 64 64 128c42.666667 0 77.056-3.349333 80.874667 101.333333 28.842667-122.218667 91.136-59.989333 91.136-59.989333z\" fill=\"#C0CA33\"\u003e\u003c/path\u003e\u003cpath d=\"M661.333333 341.333333s-1.344-38.677333 42.666667-85.333333 85.994667-8.661333 85.994667-8.661333l-5.12 109.994666s-18.666667-69.333333-48-71.104C675.008 282.496 661.333333 341.333333 661.333333 341.333333z\" fill=\"#C0CA33\"\u003e\u003c/path\u003e\u003cpath d=\"M128 874.666667l21.333333 42.666666h21.333334v-42.666666l-42.666667-21.333334z\" fill=\"#5D4037\"\u003e\u003c/path\u003e\u003cpath d=\"M746.666667 85.333333c-64 0-106.666667 42.666667-106.666667 42.666667h64l42.666667-42.666667z\" fill=\"#827717\"\u003e\u003c/path\u003e\u003c/g\u003e`,\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gfanton/gnomaze\"\ngno = \"0.9\"\n"},{"name":"maze.gno","body":"// This package demonstrate the capability of gno to build dynamic svg image\n// based on different query parameters.\n// Raycasting implementation as been heavily inspired by this project: https://github.com/AZHenley/raycasting\n\npackage gnomaze\n\nimport (\n\t\"chain/runtime\"\n\t\"encoding/base64\"\n\t\"hash/adler32\"\n\t\"math\"\n\t\"math/rand\"\n\t\"net/url\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/leon/hor\"\n)\n\nconst baseLevel = 7\n\n// Constants for cell dimensions\nconst (\n\tcellSize = 1.0\n\thalfCell = cellSize / 2\n)\n\ntype CellKind int\n\nconst (\n\tCellKindEmpty = iota\n\tCellKindWall\n)\n\nvar (\n\tlevel            int = 1\n\tsalt             int64\n\tmaze             [][]int\n\tendPos, startPos Position\n)\n\nfunc init(cur realm) {\n\t// Generate the map\n\tseed := uint64(runtime.ChainHeight())\n\trng := rand.New(rand.NewPCG(seed, uint64(time.Now().Unix())))\n\tgenerateLevel(rng, level)\n\tsalt = rng.Int64()\n\n\t// Register to hor\n\thor.Register(cross(cur), \"GnoMaze, A 3D Maze Game\", \"\")\n}\n\n// Position represents the X, Y coordinates in the maze\ntype Position struct{ X, Y int }\n\n// Player represents a player with position and viewing angle\ntype Player struct {\n\tX, Y, Angle, FOV float64\n}\n\n// PlayerState holds the player's grid position and direction\ntype PlayerState struct {\n\tCellX     int // Grid X position\n\tCellY     int // Grid Y position\n\tDirection int // 0-7 (0 = east, 1 = SE, 2 = S, etc.)\n}\n\n// Angle calculates the direction angle in radians\nfunc (p *PlayerState) Angle() float64 {\n\treturn float64(p.Direction) * math.Pi / 4\n}\n\n// Position returns the player's exact position in the grid\nfunc (p *PlayerState) Position() (float64, float64) {\n\treturn float64(p.CellX) + halfCell, float64(p.CellY) + halfCell\n}\n\n// SumCode returns a hash string based on the player's position\nfunc (p *PlayerState) SumCode() string {\n\ta := adler32.New()\n\n\tvar width int\n\tif len(maze) \u003e 0 {\n\t\twidth = len(maze[0])\n\t}\n\n\tufmt.Fprintf(a, \"%d-%d-%d\", p.CellY*width+p.CellX, level, salt)\n\treturn strconv.FormatUint(uint64(a.Sum32()), 10)\n}\n\n// Move updates the player's position based on movement deltas\nfunc (p *PlayerState) Move(dx, dy int) {\n\tnewX := p.CellX + dx\n\tnewY := p.CellY + dy\n\n\tif newY \u003e= 0 \u0026\u0026 newY \u003c len(maze) \u0026\u0026 newX \u003e= 0 \u0026\u0026 newX \u003c len(maze[0]) {\n\t\tif maze[newY][newX] == 0 {\n\t\t\tp.CellX = newX\n\t\t\tp.CellY = newY\n\t\t}\n\t}\n}\n\n// Rotate changes the player's direction\nfunc (p *PlayerState) Rotate(clockwise bool) {\n\tif clockwise {\n\t\tp.Direction = (p.Direction + 1) % 8\n\t} else {\n\t\tp.Direction = (p.Direction + 7) % 8\n\t}\n}\n\n// GenerateNextLevel validates the answer and generates a new level\nfunc GenerateNextLevel(cur realm, answer string) {\n\tseed := uint64(runtime.ChainHeight())\n\trng := rand.New(rand.NewPCG(seed, uint64(time.Now().Unix())))\n\n\tendState := PlayerState{CellX: endPos.X, CellY: endPos.Y}\n\thash := endState.SumCode()\n\tif hash != answer {\n\t\tpanic(\"invalid answer\")\n\t}\n\n\t// Generate new map\n\tlevel++\n\tsalt = rng.Int64()\n\tgenerateLevel(rng, level)\n}\n\n// generateLevel creates a new maze for the given level\nfunc generateLevel(rng *rand.Rand, level int) {\n\tif level \u003c 0 {\n\t\tpanic(\"invalid level\")\n\t}\n\n\tsize := level + baseLevel\n\tmaze, startPos, endPos = generateMap(rng, size, size)\n}\n\n// generateMap creates a random maze using a depth-first search algorithm.\nfunc generateMap(rng *rand.Rand, width, height int) ([][]int, Position, Position) {\n\t// Initialize the maze grid filled with walls.\n\tm := make([][]int, height)\n\tfor y := range m {\n\t\tm[y] = make([]int, width)\n\t\tfor x := range m[y] {\n\t\t\tm[y][x] = CellKindWall\n\t\t}\n\t}\n\n\t// Define start position and initialize stack for DFS\n\tstart := Position{1, 1}\n\tstack := []Position{start}\n\tm[start.Y][start.X] = CellKindEmpty\n\n\t// Initialize distance matrix and track farthest\n\tdist := make([][]int, height)\n\tfor y := range dist {\n\t\tdist[y] = make([]int, width)\n\t\tfor x := range dist[y] {\n\t\t\tdist[y][x] = -1\n\t\t}\n\t}\n\tdist[start.Y][start.X] = CellKindEmpty\n\tmaxDist := 0\n\tcandidates := []Position{start}\n\n\t// Possible directions for movement: right, left, down, up\n\tdirections := []Position{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}\n\n\t// Generate maze paths using DFS\n\tfor len(stack) \u003e 0 {\n\t\tcurrent := stack[len(stack)-1]\n\t\tstack = stack[:len(stack)-1]\n\n\t\tvar dirCandidates []struct {\n\t\t\tnext, wall Position\n\t\t}\n\n\t\t// Evaluate possible candidates for maze paths\n\t\tfor _, d := range directions {\n\t\t\tnx, ny := current.X+d.X*2, current.Y+d.Y*2\n\t\t\twx, wy := current.X+d.X, current.Y+d.Y\n\n\t\t\t// Check if the candidate position is within bounds and still a wall\n\t\t\tif nx \u003e 0 \u0026\u0026 nx \u003c width-1 \u0026\u0026 ny \u003e 0 \u0026\u0026 ny \u003c height-1 \u0026\u0026 m[ny][nx] == 1 {\n\t\t\t\tdirCandidates = append(dirCandidates, struct{ next, wall Position }{\n\t\t\t\t\tPosition{nx, ny}, Position{wx, wy},\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\t// If candidates are available, choose one and update the maze\n\t\tif len(dirCandidates) \u003e 0 {\n\t\t\tchosen := dirCandidates[rng.IntN(len(dirCandidates))]\n\t\t\tm[chosen.wall.Y][chosen.wall.X] = CellKindEmpty\n\t\t\tm[chosen.next.Y][chosen.next.X] = CellKindEmpty\n\n\t\t\t// Update distance for the next cell\n\t\t\tcurrentDist := dist[current.Y][current.X]\n\t\t\tnextDist := currentDist + 2\n\t\t\tdist[chosen.next.Y][chosen.next.X] = nextDist\n\n\t\t\t// Update maxDist and candidates\n\t\t\tif nextDist \u003e maxDist {\n\t\t\t\tmaxDist = nextDist\n\t\t\t\tcandidates = []Position{chosen.next}\n\t\t\t} else if nextDist == maxDist {\n\t\t\t\tcandidates = append(candidates, chosen.next)\n\t\t\t}\n\n\t\t\tstack = append(stack, current, chosen.next)\n\t\t}\n\t}\n\n\t// Select a random farthest position as the end\n\tvar end Position\n\tif len(candidates) \u003e 0 {\n\t\tend = candidates[rng.IntN(len(candidates))]\n\t} else {\n\t\tend = Position{width - 2, height - 2} // Fallback to bottom-right\n\t}\n\n\treturn m, start, end\n}\n\n// castRay simulates a ray casting in the maze to find walls\nfunc castRay(playerX, playerY, rayAngle float64, m [][]int) (distance float64, wallHeight float64, endCellHit bool, endDistance float64) {\n\tx, y := playerX, playerY\n\tdx, dy := math.Cos(rayAngle), math.Sin(rayAngle)\n\tsteps := 0\n\tendCellHit = false\n\tendDistance = 0.0\n\n\tfor {\n\t\tix, iy := int(math.Floor(x)), int(math.Floor(y))\n\t\tif ix == endPos.X \u0026\u0026 iy == endPos.Y {\n\t\t\tendCellHit = true\n\t\t\tendDistance = math.Sqrt(math.Pow(x-playerX, 2) + math.Pow(y-playerY, 2))\n\t\t}\n\n\t\tif iy \u003c 0 || iy \u003e= len(m) || ix \u003c 0 || ix \u003e= len(m[0]) || m[iy][ix] != 0 {\n\t\t\tbreak\n\t\t}\n\n\t\tx += dx * 0.1\n\t\ty += dy * 0.1\n\t\tsteps++\n\t\tif steps \u003e 400 {\n\t\t\tbreak\n\t\t}\n\t}\n\n\tdistance = math.Sqrt(math.Pow(x-playerX, 2) + math.Pow(y-playerY, 2))\n\twallHeight = 300.0 / distance\n\treturn\n}\n\n// GenerateSVG creates an SVG representation of the maze scene\nfunc GenerateSVG(cur realm, p *PlayerState) string {\n\tconst (\n\t\tsvgWidth, svgHeight = 800, 600\n\t\toffsetX, offsetY    = 0.0, 500.0\n\t\tgroundLevel         = 300\n\t\trays                = 124\n\t\tfov                 = math.Pi / 4\n\t\tminiMapSize         = 100.0\n\t\tvisibleCells        = 7\n\t\tdirLen              = 2.0\n\t)\n\n\tm := maze\n\tplayerX, playerY := p.Position()\n\tangle := p.Angle()\n\n\tsliceWidth := float64(svgWidth) / float64(rays)\n\tangleStep := fov / float64(rays)\n\n\tvar svg strings.Builder\n\tsvg.WriteString(`\u003csvg width=\"800\" height=\"600\" xmlns=\"http://www.w3.org/2000/svg\"\u003e`)\n\tsvg.WriteString(`\u003crect x=\"0\" y=\"0\" width=\"800\" height=\"300\" fill=\"rgb(20,40,20)\"/\u003e`)\n\tsvg.WriteString(`\u003crect x=\"0\" y=\"300\" width=\"800\" height=\"300\" fill=\"rgb(40,60,40)\"/\u003e`)\n\n\tvar drawBanana func()\n\tfor i := 0; i \u003c rays; i++ {\n\t\trayAngle := angle - fov/2 + float64(i)*angleStep\n\t\tdistance, wallHeight, endHit, endDist := castRay(playerX, playerY, rayAngle, m)\n\t\tdarkness := 1.0 + distance/4.0\n\t\tcolorVal1 := int(180.0 / darkness)\n\t\tcolorVal2 := int(32.0 / darkness)\n\t\tyPos := groundLevel - wallHeight/2\n\n\t\tufmt.Fprintf(\u0026svg,\n\t\t\t`\u003crect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" fill=\"rgb(%d,69,%d)\"/\u003e`,\n\t\t\tfloat64(i)*sliceWidth, yPos, sliceWidth, wallHeight, colorVal1, colorVal2)\n\n\t\tif drawBanana != nil {\n\t\t\tcontinue // Banana already drawn\n\t\t}\n\n\t\t// Only draw banana if the middle ray hit the end\n\t\t// XXX: improve this by checking for a hit in the middle of the end cell\n\t\tif i == rays/2 \u0026\u0026 endHit \u0026\u0026 endDist \u003c distance {\n\t\t\ticonHeight := 10.0 / endDist\n\t\t\tscale := iconHeight / 100\n\t\t\tx := float64(i)*sliceWidth + sliceWidth/2\n\t\t\ty := groundLevel + 20 + (iconHeight*scale)/2\n\n\t\t\tdrawBanana = func() {\n\t\t\t\tufmt.Fprintf(\u0026svg,\n\t\t\t\t\t`\u003cg transform=\"translate(%f %f) scale(%f)\"\u003e%s\u003c/g\u003e`,\n\t\t\t\t\tx, y, scale, string(svgassets[\"banana\"]),\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\t}\n\n\tif drawBanana != nil {\n\t\tdrawBanana()\n\t}\n\n\tplayerCellX, playerCellY := int(math.Floor(playerX)), int(math.Floor(playerY))\n\n\txStart := max(0, playerCellX-visibleCells/2)\n\txEnd := min(len(m[0]), playerCellX+visibleCells/2+1)\n\n\tyStart := max(0, playerCellY-visibleCells/2)\n\tyEnd := min(len(m), playerCellY+visibleCells/2+1)\n\n\tscaleX := miniMapSize / float64(xEnd-xStart)\n\tscaleY := miniMapSize / float64(yEnd-yStart)\n\n\tfor y := yStart; y \u003c yEnd; y++ {\n\t\tfor x := xStart; x \u003c xEnd; x++ {\n\t\t\tcolor := \"black\"\n\t\t\tif m[y][x] == 1 {\n\t\t\t\tcolor = \"rgb(149,0,32)\"\n\t\t\t}\n\t\t\tufmt.Fprintf(\u0026svg,\n\t\t\t\t`\u003crect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" fill=\"%s\"/\u003e`,\n\t\t\t\tfloat64(x-xStart)*scaleX+offsetX, float64(y-yStart)*scaleY+offsetY, scaleX, scaleY, color)\n\t\t}\n\t}\n\n\tpx := (playerX-float64(xStart))*scaleX + offsetX\n\tpy := (playerY-float64(yStart))*scaleY + offsetY\n\tufmt.Fprintf(\u0026svg, `\u003ccircle cx=\"%f\" cy=\"%f\" r=\"%f\" fill=\"rgb(200,200,200)\"/\u003e`, px, py, scaleX/2)\n\n\tdx := math.Cos(angle) * dirLen\n\tdy := math.Sin(angle) * dirLen\n\tufmt.Fprintf(\u0026svg,\n\t\t`\u003cline x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke=\"rgb(200,200,200)\" stroke-width=\"1\"/\u003e`,\n\t\tpx, py, (playerX+dx-float64(xStart))*scaleX+offsetX, (playerY+dy-float64(yStart))*scaleY+offsetY)\n\n\tsvg.WriteString(`\u003c/svg\u003e`)\n\treturn svg.String()\n}\n\n// renderGrid3D creates a 3D view of the grid\nfunc renderGrid3D(cur realm, p *PlayerState) string {\n\tsvg := GenerateSVG(cur, p)\n\tbase64SVG := base64.StdEncoding.EncodeToString([]byte(svg))\n\treturn ufmt.Sprintf(\"![SVG Image](data:image/svg+xml;base64,%s)\", base64SVG)\n}\n\n// generateDirLink generates a link to change player direction\nfunc generateDirLink(path string, p *PlayerState, action string) string {\n\tnewState := *p // Make copy\n\n\tswitch action {\n\tcase \"forward\":\n\t\tdx, dy := directionDeltas(newState.Direction)\n\t\tnewState.Move(dx, dy)\n\tcase \"left\":\n\t\tnewState.Rotate(false)\n\tcase \"right\":\n\t\tnewState.Rotate(true)\n\t}\n\n\tvals := make(url.Values)\n\tvals.Set(\"x\", strconv.Itoa(newState.CellX))\n\tvals.Set(\"y\", strconv.Itoa(newState.CellY))\n\tvals.Set(\"dir\", strconv.Itoa(newState.Direction))\n\n\tvals.Set(\"sum\", newState.SumCode())\n\treturn path + \"?\" + vals.Encode()\n}\n\n// isPlayerTouchingWall checks if the player's position is inside a wall\nfunc isPlayerTouchingWall(x, y float64) bool {\n\tix, iy := int(math.Floor(x)), int(math.Floor(y))\n\tif iy \u003c 0 || iy \u003e= len(maze) || ix \u003c 0 || ix \u003e= len(maze[0]) {\n\t\treturn true\n\t}\n\treturn maze[iy][ix] == CellKindEmpty\n}\n\n// directionDeltas provides deltas for movement based on direction\nfunc directionDeltas(d int) (x, y int) {\n\ts := []struct{ x, y int }{\n\t\t{1, 0},   // 0 == E\n\t\t{1, 1},   // SE\n\t\t{0, 1},   // S\n\t\t{-1, 1},  // SW\n\t\t{-1, 0},  // W\n\t\t{-1, -1}, // NW\n\t\t{0, -1},  // N\n\t\t{1, -1},  // NE\n\t}[d]\n\treturn s.x, s.y\n}\n\n// atoiDefault converts string to integer with a default fallback\nfunc atoiDefault(s string, def int) int {\n\tif s == \"\" {\n\t\treturn def\n\t}\n\ti, _ := strconv.Atoi(s)\n\treturn i\n}\n\n// Render renders the game interface\nfunc Render(cur realm, path string) string {\n\tu, _ := url.Parse(path)\n\tquery := u.Query()\n\n\tp := PlayerState{\n\t\tCellX:     atoiDefault(query.Get(\"x\"), startPos.X),\n\t\tCellY:     atoiDefault(query.Get(\"y\"), startPos.Y),\n\t\tDirection: atoiDefault(query.Get(\"dir\"), 0), // Start facing east\n\t}\n\n\tcpath := strings.TrimPrefix(cur.PkgPath(), runtime.ChainDomain())\n\tpsum := p.SumCode()\n\treset := \"[reset](\" + cpath + \")\"\n\n\tif startPos.X != p.CellX || startPos.Y != p.CellY {\n\t\tif sum := query.Get(\"sum\"); psum != sum {\n\t\t\treturn \"invalid sum : \" + reset\n\t\t}\n\t}\n\n\tif endPos.X == p.CellX \u0026\u0026 endPos.Y == p.CellY {\n\t\treturn strings.Join([]string{\n\t\t\tufmt.Sprintf(\"### Congrats you win level %d !!\", level),\n\t\t\tufmt.Sprintf(\"Code for next level is: %s\", psum),\n\t\t\tufmt.Sprintf(\"[Generate Next Level: %d](%s)\", level+1, txlink.Call(\"GenerateNextLevel\", \"answer\", psum)),\n\t\t}, \"\\n\\n\")\n\t}\n\n\t// Generate commands\n\tcommands := strings.Join([]string{\n\t\t\"\u003cgno-columns\u003e\",\n\t\t\"\u003cgno-columns-sep\u003e\",\n\t\tufmt.Sprintf(\"[▲](%s)\", generateDirLink(cpath, \u0026p, \"forward\")),\n\t\t\"\u003cgno-columns-sep\u003e\",\n\t\t\"\u003c/gno-columns\u003e\",\n\t\t\"\u003cgno-columns\u003e\",\n\t\tufmt.Sprintf(\"[◄](%s)\", generateDirLink(cpath, \u0026p, \"left\")),\n\t\t\"\u003cgno-columns-sep\u003e\",\n\t\t\"\u003cgno-columns-sep\u003e\",\n\t\tufmt.Sprintf(\"[►](%s)\", generateDirLink(cpath, \u0026p, \"right\")),\n\t\t\"\u003c/gno-columns\u003e\",\n\t}, \"\\n\\n\")\n\n\t// Generate view\n\tview := strings.Join([]string{\n\t\t\"\u003cgno-columns\u003e\",\n\t\trenderGrid3D(cross(cur), \u0026p),\n\t\t\"\u003c/gno-columns\u003e\",\n\t}, \"\\n\\n\")\n\n\treturn strings.Join([]string{\n\t\t\"## Find the banana: Level \" + strconv.Itoa(level),\n\t\t\"---\", view, \"---\", commands, \"---\",\n\t\treset,\n\t\tufmt.Sprintf(\"Position: (%d, %d) Direction: %fπ\", p.CellX, p.CellY, float64(p.Direction)/math.Pi),\n\t}, \"\\n\\n\")\n}\n\n// max returns the maximum of two integers\nfunc max(a, b int) int {\n\tif a \u003e b {\n\t\treturn a\n\t}\n\treturn b\n}\n\n// min returns the minimum of two integers\nfunc min(a, b int) int {\n\tif a \u003c b {\n\t\treturn a\n\t}\n\treturn b\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Gml6/ZkaN/cnjn2NAQr53CMNHf3IrhbDSb/IgKrY1Uq+QaQ9vPMHIT5/Ms/I+Qo1i1y1jZDLJq2enrLdQbnNBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"faucet","path":"gno.land/r/gnoland/faucet","files":[{"name":"admin.gno","body":"package faucet\n\nimport (\n\t\"chain\"\n\t\"errors\"\n)\n\nfunc AdminSetInPause(cur realm, inPause bool) string {\n\tif err := assertIsAdmin(0, cur); err != nil {\n\t\treturn err.Error()\n\t}\n\tgInPause = inPause\n\treturn \"\"\n}\n\nfunc AdminSetMessage(cur realm, message string) string {\n\tif err := assertIsAdmin(0, cur); err != nil {\n\t\treturn err.Error()\n\t}\n\tgMessage = message\n\treturn \"\"\n}\n\nfunc AdminSetTransferLimit(cur realm, amount int64) string {\n\tif err := assertIsAdmin(0, cur); err != nil {\n\t\treturn err.Error()\n\t}\n\tgLimit = chain.NewCoin(\"ugnot\", amount)\n\treturn \"\"\n}\n\nfunc AdminSetAdminAddr(cur realm, addr address) string {\n\tif err := assertIsAdmin(0, cur); err != nil {\n\t\treturn err.Error()\n\t}\n\tgAdminAddr = addr\n\treturn \"\"\n}\n\nfunc AdminAddController(cur realm, addr address) string {\n\tif err := assertIsAdmin(0, cur); err != nil {\n\t\treturn err.Error()\n\t}\n\n\tsize := gControllers.Size()\n\n\tif size \u003e= gControllersMaxSize {\n\t\treturn \"can not add more controllers than allowed\"\n\t}\n\n\tif gControllers.Has(addr.String()) {\n\t\treturn addr.String() + \" exists, no need to add.\"\n\t}\n\n\tgControllers.Set(addr.String(), addr)\n\n\treturn \"\"\n}\n\nfunc AdminRemoveController(cur realm, addr address) string {\n\tif err := assertIsAdmin(0, cur); err != nil {\n\t\treturn err.Error()\n\t}\n\n\tif !gControllers.Has(addr.String()) {\n\t\treturn addr.String() + \" is not on the controller list\"\n\t}\n\n\t_, ok := gControllers.Remove(addr.String())\n\n\t// it not should happen.\n\t// we will check anyway to prevent issues in the underline implementation.\n\n\tif !ok {\n\t\treturn addr.String() + \" is not on the controller list\"\n\t}\n\n\treturn \"\"\n}\n\nfunc assertIsAdmin(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tcaller := rlm.Previous().Address()\n\tif caller != gAdminAddr {\n\t\treturn errors.New(\"restricted for admin\")\n\t}\n\treturn nil\n}\n"},{"name":"faucet.gno","body":"package faucet\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"errors\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar (\n\t// configurable by admin.\n\tgAdminAddr          address = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\") // govdao t1 multisig\n\tgControllers                = bptree.NewBPTree32()\n\tgControllersMaxSize         = 10 // limit it to 10\n\tgInPause                    = false\n\tgMessage                    = \"# Community Faucet.\\n\\n\"\n\n\t// internal vars, for stats.\n\tgTotalTransferred chain.Coins\n\tgTotalTransfers   = uint(0)\n\n\t// per request limit, 350 gnot\n\tgLimit chain.Coin = chain.NewCoin(\"ugnot\", 350_000_000)\n)\n\nfunc Transfer(cur realm, to address, send int64) string {\n\tif err := assertIsController(0, cur); err != nil {\n\t\treturn err.Error()\n\t}\n\n\tif gInPause {\n\t\treturn errors.New(\"faucet in pause\").Error()\n\t}\n\n\t// limit the per request\n\tif send \u003e gLimit.Amount {\n\t\treturn errors.New(\"Per request limit \" + gLimit.String() + \" exceed\").Error()\n\t}\n\tsendCoins := chain.Coins{chain.NewCoin(\"ugnot\", send)}\n\n\tgTotalTransferred = gTotalTransferred.Add(sendCoins)\n\tgTotalTransfers++\n\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tpkgaddr := cur.Address()\n\tbanker_.SendCoins(pkgaddr, to, sendCoins)\n\treturn \"\"\n}\n\nfunc GetPerTransferLimit() int64 {\n\treturn gLimit.Amount\n}\n\nfunc bankerAddr(cur realm) address {\n\treturn cur.Address()\n}\n\nfunc Render(cur realm, _ string) string {\n\tbanker_ := banker.NewReadonlyBanker()\n\tbalance := banker_.GetCoins(bankerAddr(cross(cur)))\n\n\toutput := gMessage\n\tif gInPause {\n\t\toutput += \"Status: inactive.\\n\"\n\t} else {\n\t\toutput += \"Status: active.\\n\"\n\t}\n\toutput += ufmt.Sprintf(\"Balance: %s.\\n\", balance.String())\n\toutput += ufmt.Sprintf(\"Total transfers: %s (in %d times).\\n\\n\", gTotalTransferred.String(), gTotalTransfers)\n\n\toutput += \"Package address: \" + cur.Address().String() + \"\\n\\n\"\n\toutput += ufmt.Sprintf(\"Admin: %s\\n\\n \", gAdminAddr.String())\n\toutput += ufmt.Sprintf(\"Controllers:\\n\\n \")\n\n\tfor i := 0; i \u003c gControllers.Size(); i++ {\n\t\t_, v := gControllers.GetByIndex(i)\n\t\toutput += ufmt.Sprintf(\"%s  \", v.(address))\n\t}\n\n\toutput += \"\\n\\n\"\n\toutput += ufmt.Sprintf(\"Per request limit: %s\\n\\n\", gLimit.String())\n\n\treturn output\n}\n\nfunc assertIsController(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tcaller := rlm.Previous().Address()\n\tok := gControllers.Has(caller.String())\n\tif !ok {\n\t\treturn errors.New(caller.String() + \" is not on the controller list\")\n\t}\n\treturn nil\n}\n"},{"name":"faucet_test.gno","body":"package faucet\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nfunc TestPackage(cur realm, t *testing.T) {\n\tvar (\n\t\tadminaddr        = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\t\tfaucetaddr       = chain.PackageAddress(\"gno.land/r/gnoland/faucet\")\n\t\tcontrolleraddr1  = testutils.TestAddress(\"controller1\")\n\t\tcontrolleraddr2  = testutils.TestAddress(\"controller2\")\n\t\tcontrolleraddr3  = testutils.TestAddress(\"controller3\")\n\t\tcontrolleraddr4  = testutils.TestAddress(\"controller4\")\n\t\tcontrolleraddr5  = testutils.TestAddress(\"controller5\")\n\t\tcontrolleraddr6  = testutils.TestAddress(\"controller6\")\n\t\tcontrolleraddr7  = testutils.TestAddress(\"controller7\")\n\t\tcontrolleraddr8  = testutils.TestAddress(\"controller8\")\n\t\tcontrolleraddr9  = testutils.TestAddress(\"controller9\")\n\t\tcontrolleraddr10 = testutils.TestAddress(\"controller10\")\n\t\tcontrolleraddr11 = testutils.TestAddress(\"controller11\")\n\n\t\ttest1addr = testutils.TestAddress(\"test1\")\n\t)\n\t// deposit 1000gnot to faucet contract\n\ttesting.IssueCoins(faucetaddr, chain.Coins{{\"ugnot\", 1_000_000_000}})\n\tassertBalance(t, faucetaddr, 1_000_000_000)\n\n\t// by default, balance is empty, and as a user I cannot call Transfer, or Admin commands.\n\tassertBalance(t, test1addr, 0)\n\ttesting.SetRealm(testing.NewUserRealm(test1addr))\n\tassertErr(t, Transfer(cross(cur), test1addr, 1_000_000))\n\n\tassertErr(t, AdminAddController(cross(cur), controlleraddr1))\n\ttesting.SetRealm(testing.NewUserRealm(controlleraddr1))\n\tassertErr(t, Transfer(cross(cur), test1addr, 1_000_000))\n\n\t// as an admin, add the controller to contract and deposit more 2000gnot to contract\n\ttesting.SetRealm(testing.NewUserRealm(adminaddr))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr1))\n\tassertBalance(t, faucetaddr, 1_000_000_000)\n\n\t// now, send some tokens as controller.\n\ttesting.SetRealm(testing.NewUserRealm(controlleraddr1))\n\tassertNoErr(t, Transfer(cross(cur), test1addr, 1_000_000))\n\tassertBalance(t, test1addr, 1_000_000)\n\tassertNoErr(t, Transfer(cross(cur), test1addr, 1_000_000))\n\tassertBalance(t, test1addr, 2_000_000)\n\tassertBalance(t, faucetaddr, 998_000_000)\n\n\t// remove controller\n\t// as an admin, remove controller\n\ttesting.SetRealm(testing.NewUserRealm(adminaddr))\n\tassertNoErr(t, AdminRemoveController(cross(cur), controlleraddr1))\n\ttesting.SetRealm(testing.NewUserRealm(controlleraddr1))\n\tassertErr(t, Transfer(cross(cur), test1addr, 1_000_000))\n\n\t// duplicate controller\n\ttesting.SetRealm(testing.NewUserRealm(adminaddr))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr1))\n\tassertErr(t, AdminAddController(cross(cur), controlleraddr1))\n\t// add more than more than allowed controllers\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr2))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr3))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr4))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr5))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr6))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr7))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr8))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr9))\n\tassertNoErr(t, AdminAddController(cross(cur), controlleraddr10))\n\tassertErr(t, AdminAddController(cross(cur), controlleraddr11))\n\n\t// send more than per transfer limit\n\ttesting.SetRealm(testing.NewUserRealm(adminaddr))\n\tassertNoErr(t, AdminSetTransferLimit(cross(cur), 300_000_000))\n\ttesting.SetRealm(testing.NewUserRealm(controlleraddr1))\n\tassertErr(t, Transfer(cross(cur), test1addr, 301_000_000))\n\n\t// block transefer from the address not on the controllers list.\n\ttesting.SetRealm(testing.NewUserRealm(controlleraddr11))\n\tassertErr(t, Transfer(cross(cur), test1addr, 1_000_000))\n}\n\nfunc assertErr(t *testing.T, err string) {\n\tt.Helper()\n\n\tif err == \"\" {\n\t\tt.Logf(\"info: got err: %v\", err)\n\t\tt.Errorf(\"expected an error, got nil.\")\n\t}\n}\n\nfunc assertNoErr(t *testing.T, err string) {\n\tt.Helper()\n\tif err != \"\" {\n\t\tt.Errorf(\"got err: %v.\", err)\n\t}\n}\n\nfunc assertBalance(t *testing.T, addr address, expectedBal int64) {\n\tt.Helper()\n\tbanker_ := banker.NewReadonlyBanker()\n\tcoins := banker_.GetCoins(addr)\n\tgot := coins.AmountOf(\"ugnot\")\n\n\tif expectedBal != got {\n\t\tt.Errorf(\"invalid balance: expected %d, got %d.\", expectedBal, got)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/faucet\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"z0_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/faucet/filetests/z0_filetest\n\npackage z0_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/r/gnoland/faucet\"\n)\n\n// mints ugnot to current realm\nfunc init() {\n\tfaucetaddr := chain.PackageAddress(\"gno.land/r/gnoland/faucet\")\n\ttesting.IssueCoins(faucetaddr, chain.Coins{{\"ugnot\", 200_000_000}})\n}\n\n// assert render with empty path and no controllers\nfunc main(cur realm) {\n\tprintln(faucet.Render(cross(cur), \"\"))\n}\n\n// Output:\n// # Community Faucet.\n//\n// Status: active.\n// Balance: 200000000ugnot.\n// Total transfers:  (in 0 times).\n//\n// Package address: g1ttrq7mp4zy6dssnmgyyktnn4hcj3ys8xhju0n7\n//\n// Admin: g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n//\n//  Controllers:\n//\n//\n//\n// Per request limit: 350000000ugnot\n"},{"name":"z1_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/faucet/filetests/z1_filetest\n\npackage z1_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/r/gnoland/faucet\"\n)\n\n// mints ugnot to current realm\nfunc init() {\n\tfaucetaddr := chain.PackageAddress(\"gno.land/r/gnoland/faucet\")\n\ttesting.IssueCoins(faucetaddr, chain.Coins{{\"ugnot\", 200_000_000}})\n}\n\n// assert render with a path and no controllers\nfunc main(cur realm) {\n\tprintln(faucet.Render(cross(cur), \"path\"))\n}\n\n// Output:\n// # Community Faucet.\n//\n// Status: active.\n// Balance: 200000000ugnot.\n// Total transfers:  (in 0 times).\n//\n// Package address: g1ttrq7mp4zy6dssnmgyyktnn4hcj3ys8xhju0n7\n//\n// Admin: g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n//\n//  Controllers:\n//\n//\n//\n// Per request limit: 350000000ugnot\n"},{"name":"z2_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/faucet/filetests/z2_filetest\n\npackage z2_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnoland/faucet\"\n)\n\n// mints ugnot to current realm\nfunc init() {\n\tfaucetaddr := chain.PackageAddress(\"gno.land/r/gnoland/faucet\")\n\ttesting.IssueCoins(faucetaddr, chain.Coins{{\"ugnot\", 200_000_000}})\n}\n\n// assert render with empty path and 2 controllers\nfunc main(cur realm) {\n\tvar (\n\t\tadminaddr       = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\t\tcontrolleraddr1 = testutils.TestAddress(\"controller1\")\n\t\tcontrolleraddr2 = testutils.TestAddress(\"controller2\")\n\t)\n\ttesting.SetRealm(testing.NewUserRealm(adminaddr))\n\terr := faucet.AdminAddController(cross(cur), controlleraddr1)\n\tif err != \"\" {\n\t\tpanic(err)\n\t}\n\terr = faucet.AdminAddController(cross(cur), controlleraddr2)\n\tif err != \"\" {\n\t\tpanic(err)\n\t}\n\tprintln(faucet.Render(cross(cur), \"\"))\n}\n\n// Output:\n// # Community Faucet.\n//\n// Status: active.\n// Balance: 200000000ugnot.\n// Total transfers:  (in 0 times).\n//\n// Package address: g1ttrq7mp4zy6dssnmgyyktnn4hcj3ys8xhju0n7\n//\n// Admin: g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n//\n//  Controllers:\n//\n//  g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav  g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v\n//\n// Per request limit: 350000000ugnot\n"},{"name":"z3_filetest.gno","body":"// PKGPATH: gno.land/r/gnoland/faucet/filetests/z3_filetest\n\npackage z3_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/r/gnoland/faucet\"\n)\n\n// mints coints to current realm\nfunc init() {\n\tfaucetaddr := chain.PackageAddress(\"gno.land/r/gnoland/faucet\")\n\ttesting.IssueCoins(faucetaddr, chain.Coins{{\"ugnot\", 200_000_000}})\n}\n\n// assert render with 2 controllers and 2 transfers\nfunc main(cur realm) {\n\tvar (\n\t\tadminaddr       = address(\"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\")\n\t\tcontrolleraddr1 = testutils.TestAddress(\"controller1\")\n\t\tcontrolleraddr2 = testutils.TestAddress(\"controller2\")\n\t\ttestaddr1       = testutils.TestAddress(\"test1\")\n\t\t_               = testutils.TestAddress(\"test2\")\n\t)\n\ttesting.SetRealm(testing.NewUserRealm(adminaddr))\n\terr := faucet.AdminAddController(cross(cur), controlleraddr1)\n\tif err != \"\" {\n\t\tpanic(err)\n\t}\n\terr = faucet.AdminAddController(cross(cur), controlleraddr2)\n\tif err != \"\" {\n\t\tpanic(err)\n\t}\n\ttesting.SetRealm(testing.NewUserRealm(controlleraddr1))\n\terr = faucet.Transfer(cross(cur), testaddr1, 1_000_000)\n\tif err != \"\" {\n\t\tpanic(err)\n\t}\n\ttesting.SetRealm(testing.NewUserRealm(controlleraddr2))\n\terr = faucet.Transfer(cross(cur), testaddr1, 2_000_000)\n\tif err != \"\" {\n\t\tpanic(err)\n\t}\n\tprintln(faucet.Render(cross(cur), \"\"))\n}\n\n// Output:\n// # Community Faucet.\n//\n// Status: active.\n// Balance: 197000000ugnot.\n// Total transfers: 3000000ugnot (in 2 times).\n//\n// Package address: g1ttrq7mp4zy6dssnmgyyktnn4hcj3ys8xhju0n7\n//\n// Admin: g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\n//\n//  Controllers:\n//\n//  g1vdhkuarjdakxcetjx9047h6lta047h6lsdacav  g1vdhkuarjdakxcetjxf047h6lta047h6lnrev3v\n//\n// Per request limit: 350000000ugnot\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"qvzZ1e3NKyZe/GW84lCmlke0dzA3jYxWX0V4/48WQlXgx2jBopngaibfquigoCUnuKf6A2eqyNGy+cpnFCcVCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"pages","path":"gno.land/r/gnoland/pages","files":[{"name":"admin.gno","body":"package pages\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/bptree/v0\"\n)\n\nvar (\n\tadminAddr     address\n\tmoderatorList bptree.BPTree\n\tinPause       bool\n)\n\nfunc init() {\n\t// adminAddr = unsafe.OriginCaller() // FIXME: find a way to use this from the main's genesis.\n\tadminAddr = \"g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh\" // govdao t1 multisig\n}\n\nfunc AdminSetAdminAddr(_ realm, addr address) {\n\tassertIsAdmin()\n\tadminAddr = addr\n}\n\nfunc AdminSetInPause(_ realm, state bool) {\n\tassertIsAdmin()\n\tinPause = state\n}\n\nfunc AdminAddModerator(_ realm, addr address) {\n\tassertIsAdmin()\n\tmoderatorList.Set(addr.String(), true)\n}\n\nfunc AdminRemoveModerator(_ realm, addr address) {\n\tassertIsAdmin()\n\tmoderatorList.Set(addr.String(), false) // entry kept as a revocation record; isModerator checks the value\n}\n\nfunc ModAddPost(_ realm, slug, title, body, publicationDate, authors, tags string) {\n\tassertIsModerator()\n\n\tcaller := unsafe.OriginCaller()\n\ttagList := strings.Split(tags, \",\")\n\tauthorList := strings.Split(authors, \",\")\n\n\terr := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)\n\tcheckErr(err)\n}\n\nfunc ModEditPost(_ realm, slug, title, body, publicationDate, authors, tags string) {\n\tassertIsModerator()\n\n\ttagList := strings.Split(tags, \",\")\n\tauthorList := strings.Split(authors, \",\")\n\n\terr := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)\n\tcheckErr(err)\n}\n\nfunc isAdmin(addr address) bool {\n\treturn addr == adminAddr\n}\n\nfunc isModerator(addr address) bool {\n\t// Removed moderators stay in the list with a false value, so the\n\t// stored value must be checked, not just key presence.\n\tactive, _ := moderatorList.Get(addr.String()).(bool)\n\treturn active\n}\n\nfunc assertIsAdmin() {\n\tcaller := unsafe.OriginCaller()\n\tif !isAdmin(caller) {\n\t\tpanic(\"access restricted.\")\n\t}\n}\n\nfunc assertIsModerator() {\n\tcaller := unsafe.OriginCaller()\n\tif isAdmin(caller) || isModerator(caller) {\n\t\treturn\n\t}\n\tpanic(\"access restricted\")\n}\n\nfunc assertNotInPause() {\n\tif inPause {\n\t\tpanic(\"access restricted (pause)\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/pages\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"page_about.gno","body":"package pages\n\nfunc init() {\n\tpath := \"about\"\n\ttitle := \"Gno.land Is A Platform To Write Smart Contracts In Gno\"\n\t// XXX: description := \"On Gno.land, developers write smart contracts and other blockchain apps using Gno without learning a language that’s exclusive to a single ecosystem.\"\n\tbody := `\nGno.land is a next-generation smart contract platform using Gno, an interpreted version of the general-purpose Go\nprogramming language. On Gno.land, smart contracts can be uploaded on-chain only by publishing their full source code,\nmaking it trivial to verify the contract or fork it into an improved version. With a system to publish reusable code\nlibraries on-chain, Gno.land serves as the “GitHub” of the ecosystem, with realms built using fully transparent,\nauditable code that anyone can inspect and reuse.\n\nGno.land addresses many pressing issues in the blockchain space, starting with the ease of use and intuitiveness of\nsmart contract platforms. Developers can write smart contracts without having to learn a new language that’s exclusive\nto a single ecosystem or limited by design. Go developers can easily port their existing web apps to Gno.land or build\nnew ones from scratch, making web3 vastly more accessible.\n\nSecured by Proof of Contribution (PoC), a DAO-managed Proof-of-Authority consensus mechanism, Gno.land prioritizes\nfairness and merit, rewarding the people most active on the platform. PoC restructures the financial incentives that\noften corrupt blockchain projects, opting instead to reward contributors for their work based on expertise, commitment, and\nalignment.\n\nOne of our inspirations for Gno.land is the gospels, which built a system of moral code that lasted thousands of years.\nBy observing a minimal production implementation, Gno.land’s design will endure over time and serve as a reference for\nfuture generations with censorship-resistant tools that improve their understanding of the world.\n`\n\tif err := b.NewPost(\"\", path, title, body, \"2022-05-20T13:17:22Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_contribute.gno","body":"package pages\n\nfunc init() {\n\tpath := \"contribute\"\n\ttitle := \"Contributor Ecosystem: Call for Contributions\"\n\tbody := `\n\nGno.land puts at the center of its identity the contributors that help to create and shape the project into what it is; incentivizing those who contribute the most and help advance its vision. Eventually, contributions will be incentivized directly on-chain; in the meantime, this page serves to illustrate our current off-chain initiatives.\n\nGno.land is still in full-steam development. For now, we're looking for the earliest of adopters; curious to explore a new way to build smart contracts and eager to make an impact. Joining Gno.land's development now means you can help to shape the base of its development ecosystem, which will pave the way for the next generation of blockchain programming.\n\nAs an open-source project, we welcome all contributions. On this page you can find some pointers on where to get started; as well as some incentives for the most valuable and important contributions.\n\n## Where to get started\n\nIf you are interested in contributing to Gno.land, you can jump in on our [GitHub monorepo](https://github.com/gnolang/gno/blob/master/CONTRIBUTING.md) - where most development happens.\n\nA good place to start are the issues tagged [\"good first issue\"](https://github.com/gnolang/gno/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22). They should allow you to make some impact on the Gno repository while you're still exploring the details of how everything works.\n\n## Gno Bounties\n\n\u003e **Note: The Gno Bounty Program is currently paused until further notice.**\n\nTwo reasons prompted this pause:\n\n- **Focus on Mainnet.** The team is fully focused on the Gno.land Beta Mainnet launch — the highest priority — as well as the critical post-beta work required to deliver a complete Mainnet.\n- **Decline in submission quality.** The bounty program was created to encourage innovative, creative, and technical contributions that meaningfully advance Gno.land. However, with the rise of generative AI, the overall quality of submissions has declined significantly. A large portion of entries are clearly AI-generated; others, though less obviously so, are often inaccurate upon review due to overreliance on AI. Reviewing and validating these submissions has become increasingly time-consuming, diverting focus from development work.\n\nCommunity contributions remain strongly encouraged, particularly:\n\n- Tooling and infrastructure projects\n- Application-focused development\n- Longer-term, high-quality initiatives\n\nThese types of contributions are better aligned with Gno's goals than short-term bounties, and are very welcome provided they follow the [contributing guidelines](https://github.com/gnolang/gno/blob/master/CONTRIBUTING.md). Sustained, high-quality contributions will be rewarded over time via the Gno.land GovDAO.\n\n## Gno.land Grants\n\nThe Gno.land grants program is to encourage and support the growth of the Gno.land contributor community, and build out the usability of the platform and smart contract library. The program provides financial resources to contributors to explore the Gno tech stack, and build dApps, tooling, infrastructure, products, and smart contract libraries in Gno.land.\n\nFor more details on Gno.land grants, suggested topics, and how to apply, visit our grants [repository](https://github.com/gnolang/grants).\n\n## Join Game of Realms\n\nGame of Realms is the overarching contributor network of gnomes, currently running off-chain, and will eventually transition on-chain. At this stage, a Game of Realms contribution is comprised of high-impact contributions identified as ['notable contributions'](https://github.com/gnolang/game-of-realms/tree/main/contributors).\n\nThese contributions are not linked to immediate financial rewards, but are notable in nature, in the sense they are a challenge, make a significant addition to the project, and require persistence, with minimal feedback loops from the core team.\n\nThe selection of a notable contribution or the sum of contributions that equal 'notable' is based on the impact it has on the development of the project. For now, it is focused on code contributions, and will evolve over time. The Gno development teams will initially qualify and evaluate notable contributions, and vote off-chain on adding them to the 'notable contributions' folder on GitHub.\n\nYou can always contribute to the project, and all contributions will be noticed. Contributing now is a way to build your personal contributor profile in Gno.land early on in the ecosystem, and signal your commitment to the project, the community, and its future.\n\nThere are a variety of ways to make your contributions count:\n\n- Core code contributions\n- Realm and pure package development\n- Validator tooling\n- Developer tooling\n- Tutorials and documentation\n\nTo start, we recommend you create a PR in the Game of Realms [repository](https://github.com/gnolang/game-of-realms) to create your profile page for all your contributions.`\n\n\tif err := b.NewPost(\"\", path, title, body, \"2024-09-05T00:00:00Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_ecosystem.gno","body":"package pages\n\nfunc init() {\n\tvar (\n\t\tpath  = \"ecosystem\"\n\t\ttitle = \"Discover gno.land Ecosystem Projects \u0026 Initiatives\"\n\t\t// XXX: description = \"Dive further into the gno.land ecosystem and discover the core infrastructure, projects, smart contracts, and tooling we’re building.\"\n\t\tbody = `\n### [Gno Playground](https://play.gno.land)\n\nGno Playground is a simple web interface that lets you write, test, and experiment with your Gno code to improve your\nunderstanding of the Gno language. You can share your code, run unit tests, deploy your realms and packages, and execute\nfunctions in your code using the repo.\n\nVisit the playground at [play.gno.land](https://play.gno.land)!\n\n### [Gno Studio Connect](https://gno.studio/connect)\n\nGno Studio Connect provides seamless access to realms, making it simple to explore, interact, and engage\nwith gno.land’s smart contracts through function calls. Connect focuses on function calls, enabling users to interact\nwith any realm’s exposed function(s) on gno.land.\n\nSee your realm interactions in [Gno Studio Connect](https://gno.studio/connect)\n\n### [Gnoscan](https://gnoscan.io)\n\nDeveloped by the Onbloc team, Gnoscan is gno.land’s blockchain explorer. Anyone can use Gnoscan to easily find\ninformation that resides on the gno.land blockchain, such as wallet addresses, TX hashes, blocks, and contracts.\nGnoscan makes our on-chain data easy to read and intuitive to discover.\n\nExplore the gno.land blockchain at [gnoscan.io](https://gnoscan.io)!\n\n### Adena\n\nAdena is a user-friendly non-custodial wallet for gno.land. Open-source and developed by Onbloc, Adena allows gnomes to\ninteract easily with the chain. With an emphasis on UX, Adena is built to handle millions of realms and tokens with a\nhigh-quality interface, support for NFTs and custom tokens, and seamless integration. Install Adena via the [official website](https://www.adena.app/)\n\n### Gnoswap\n\nGnoswap is currently under development and led by the Onbloc team. Gnoswap will be the first DEX on gno.land and is an\nautomated market maker (AMM) protocol written in Gno that allows for permissionless token exchanges on the platform.\n\n### Flippando\n\nFlippando is a simple on-chain memory game, ported from Solidity to Gno, which starts with an empty matrix to flip tiles\non to see what’s underneath. If the tiles match, they remain uncovered; if not, they are briefly shown, and the player\nmust memorize their colors until the entire matrix is uncovered. The end result can be minted as an NFT, which can later\nbe assembled into bigger, more complex NFTs, creating a digital “painting” with the uncovered tiles.  Play the game at [Flippando](https://gno.flippando.xyz/flip)\n\n### Gno Native Kit\n\n[Gno Native Kit](https://github.com/gnolang/gnonative) is a framework that allows developers to build and port gno.land (d)apps written in the (d)app's native language.\n\n\n`\n\t)\n\tif err := b.NewPost(\"\", path, title, body, \"2022-05-20T13:17:23Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_gnolang.gno","body":"package pages\n\nfunc init() {\n\tvar (\n\t\tpath  = \"gnolang\"\n\t\ttitle = \"About the Gno, the Language for Gno.land\"\n\t\t// TODO fix broken images\n\t\tbody = `\n\n[Gno](https://github.com/gnolang/gno) is an interpretation of the widely-used Go (Golang) programming language for blockchain created by Cosmos co-founder Jae Kwon in 2022 to mark a new era in smart contracting. Gno is ~99% identical to Go, so Go programmers can start coding in Gno right away, with a minimal learning curve. For example, Gno comes with blockchain-specific standard libraries, but any code that doesn’t use blockchain-specific logic can run in Go with minimal processing. Libraries that don’t make sense in the blockchain context, such as network or operating-system access, are not available in Gno. Otherwise, Gno loads and uses many standard libraries that power Go, so most of the parsing of the source code is the same.\n\nUnder the hood, the Gno code is parsed into an abstract syntax tree (AST) and the AST itself is used in the interpreter, rather than bytecode as in many virtual machines such as Java, Python, or Wasm. This makes even the GnoVM accessible to any Go programmer. The novel design of the intuitive GnoVM interpreter allows Gno to freeze and resume the program by persisting and loading the entire memory state. Gno is deterministic, auto-persisted, and auto-Merkle-ized, allowing (smart contract) programs to be succinct, as the programmer doesn’t have to serialize and deserialize objects to persist them into a database (unlike programming applications with the Cosmos SDK).\n\n## How Gno Differs from Go\n\n![Gno and Go differences](static/img/gno-language/go-and-gno.jpg)\n\nThe composable nature of Go/Gno allows for type-checked interactions between contracts, making Gno.land safer and more powerful, as well as operationally cheaper and faster. Smart contracts on Gno.land are light, simple, more focused, and easily interoperable—a network of interconnected contracts rather than siloed monoliths that limit interactions with other contracts.\n\n![Example of Gno code](static/img/gno-language/code-example.jpg)\n\n## Gno Inherits Go’s Built-in Security Features\n\nGo supports secure programming through exported/non-exported fields, enabling a “least-authority” design. It is easy to create objects and APIs that expose only what should be accessible to callers while hiding what should not be simply by the capitalization of letters, thus allowing a succinct representation of secure logic that can be called by multiple users.\n\nAnother major advantage of Go is that the language comes with an ecosystem of great tooling, like the compiler and third-party tools that statically analyze code. Gno inherits these advantages from Go directly to create a smart contract programming language that provides embedding, composability, type-check safety, and garbage collection, helping developers to write secure code relying on the compiler, parser, and interpreter to give warning alerts for common mistakes.\n\n## Gno vs Solidity\n\nThe most widely-adopted smart contract language today is Ethereum’s EVM-compatible Solidity. With bytecode built from the ground up and Turing complete, Solidity opened up a world of possibilities for decentralized applications (dApps) and there are currently more than 10 million contracts deployed on Ethereum. However, Solidity provides limited tooling and its EVM has a stack limit and computational inefficiencies.\n\nSolidity is designed for one purpose only (writing smart contracts) and is bound by the limitations of the EVM. In addition, developers have to learn several languages if they want to understand the whole stack or work across different ecosystems. Gno aspires to exceed Solidity on multiple fronts (and other smart contract languages like CosmWasm or Substrate) as every part of the stack is written in Gno. It’s easy for developers to understand the entire system just by studying a relatively small code base.\n\n## Gno Is Essential for the Wider Adoption of Web3\n\nGno makes imports as easy as they are in web2 with runtime-based imports for seamless dependency flow comprehension, and support for complex structs, beyond primitive types. Gno is ultimately cost-effective as dependencies are loaded once, enabling remote function calls as local, and providing automatic and independent per-realm state persistence.\n\nUsing Gno, developers can rapidly accelerate application development and adopt a modular structure by reusing and reassembling existing modules without building from scratch. They can embed one structure inside another in an intuitive way while preserving localism, and the language specification is simple, successfully balancing practicality and minimalism.\n\nThe Go language is so well designed that the Gno smart contract system will become the new gold standard for smart contract development and other blockchain applications. As a programming language that is universally adopted, secure, composable, and complete, Gno is essential for the broader adoption of web3 and its sustainable growth.`\n\t)\n\tif err := b.NewPost(\"\", path, title, body, \"2022-05-20T13:17:24Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_license.gno","body":"package pages\n\nfunc init() {\n\tvar (\n\t\tpath  = \"license\"\n\t\ttitle = \"Gno Network General Public License\"\n\t\tbody  = `Copyright (C) 2024 NewTendermint, LLC\n\nThis program is free software: you can redistribute it and/or modify it under\nthe terms of the GNO Network General Public License as published by\nNewTendermint, LLC, either version 4 of the License, or (at your option) any\nlater version published by NewTendermint, LLC.\n\nThis program is distributed in the hope that it will be useful, but is provided\nas-is and WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNO Network\nGeneral Public License for more details.\n\nYou should have received a copy of the GNO Network General Public License along\nwith this program.  If not, see \u003chttps://gno.land/license\u003e.\n\nAttached below are the terms of the GNO Network General Public License, Version\n4 (a fork of the GNU Affero General Public License 3).\n\n## Additional Terms\n\n### Strong Attribution\n\nIf any of your user interfaces, such as websites and mobile applications, serve\nas the primary point of entry to a platform or blockchain that 1) offers users\nthe ability to upload their own smart contracts to the platform or blockchain,\nand 2) leverages any Covered Work (including the GNO virtual machine) to run\nthose smart contracts on the platform or blockchain (\"Applicable Work\"), then\nthe Applicable Work must prominently link to (1) gno.land or (2) any other URL\ndesignated by NewTendermint, LLC that has not been rejected by the governance of\nthe first chain known as gno.land, provided that the identity of the first chain\nis not ambiguous.  In the event the identity of the first chain is ambiguous,\nthen NewTendermint, LLC's designation shall control.  Such link must appear\nconspicuously in the header or footer of the Applicable Work, such that all\nusers may learn of gno.land or the URL designated by NewTendermint, LLC.\n\nThis additional attribution requirement shall remain in effect for (1) 7\nyears from the date of publication of the Applicable Work, or (2) 7 years from\nthe date of publication of the Covered Work (including republication of new\nversions), whichever is later, but no later than 12 years after the application\nof this strong attribution requirement to the publication of the Applicable\nWork.  For purposes of this Strong Attribution requirement, Covered Work shall\nmean any work that is licensed under the GNO Network General Public License,\nVersion 4 or later, by NewTendermint, LLC.\n\n\n# GNO NETWORK GENERAL PUBLIC LICENSE\n\nVersion 4, 7 May 2024\n\nModified from the GNU AFFERO GENERAL PUBLIC LICENSE.\nGNU is not affiliated with GNO or NewTendermint, LLC.\nCopyright (C) 2022 NewTendermint, LLC.\n\n## Preamble\n\nThe GNO Network General Public License is a free, copyleft license for\nsoftware and other kinds of works, specifically designed to ensure\ncooperation with the community in the case of network server software.\n\nThe licenses for most software and other practical works are designed\nto take away your freedom to share and change the works.  By contrast,\nour General Public Licenses are intended to guarantee your freedom to\nshare and change all versions of a program--to make sure it remains free\nsoftware for all its users.\n\nWhen we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthem if you wish), that you receive source code or can get it if you\nwant it, that you can change the software or use pieces of it in new\nfree programs, and that you know you can do these things.\n\nDevelopers that use our General Public Licenses protect your rights\nwith two steps: (1) assert copyright on the software, and (2) offer\nyou this License which gives you legal permission to copy, distribute\nand/or modify the software.\n\nA secondary benefit of defending all users' freedom is that\nimprovements made in alternate versions of the program, if they\nreceive widespread use, become available for other developers to\nincorporate.  Many developers of free software are heartened and\nencouraged by the resulting cooperation.  However, in the case of\nsoftware used on network servers, this result may fail to come about.\nThe GNU General Public License permits making a modified version and\nletting the public access it on a server without ever releasing its\nsource code to the public.\n\nThe GNO Network General Public License is designed specifically to\nensure that, in such cases, the modified source code becomes available\nto the community.  It requires the operator of a network server to\nprovide the source code of the modified version running there to the\nusers of that server.  Therefore, public use of a modified version, on\na publicly accessible server, gives the public access to the source\ncode of the modified version.\n\nThe precise terms and conditions for copying, distribution and\nmodification follow.\n\n## TERMS AND CONDITIONS\n\n### 0. Definitions.\n\n\"This License\" refers to version 4 of the GNO Network General Public License.\n\n\"Copyright\" also means copyright-like laws that apply to other kinds of\nworks, such as semiconductor masks.\n\n\"The Program\" refers to any copyrightable work licensed under this\nLicense.  Each licensee is addressed as \"you\".  \"Licensees\" and\n\"recipients\" may be individuals or organizations.\n\nTo \"modify\" a work means to copy from or adapt all or part of the work\nin a fashion requiring copyright permission, other than the making of an\nexact copy.  The resulting work is called a \"modified version\" of the\nearlier work or a work \"based on\" the earlier work.\n\nA \"covered work\" means either the unmodified Program or a work based\non the Program.\n\nTo \"propagate\" a work means to do anything with it that, without\npermission, would make you directly or secondarily liable for\ninfringement under applicable copyright law, except executing it on a\ncomputer or modifying a private copy.  Propagation includes copying,\ndistribution (with or without modification), making available to the\npublic, and in some countries other activities as well.\n\nTo \"convey\" a work means any kind of propagation that enables other\nparties to make or receive copies.  Mere interaction with a user through\na computer network, with no transfer of a copy, is not conveying.\n\nAn interactive user interface displays \"Appropriate Legal Notices\"\nto the extent that it includes a convenient and prominently visible\nfeature that (1) displays an appropriate copyright notice, and (2)\ntells the user that there is no warranty for the work (except to the\nextent that warranties are provided), that licensees may convey the\nwork under this License, and how to view a copy of this License.  If\nthe interface presents a list of user commands or options, such as a\nmenu, a prominent item in the list meets this criterion.\n\n### 1. Source Code.\n\nThe \"source code\" for a work means the preferred form of the work\nfor making modifications to it.  \"Object code\" means any non-source\nform of a work.\n\nA \"Standard Interface\" means an interface that either is an official\nstandard defined by a recognized standards body, or, in the case of\ninterfaces specified for a particular programming language, one that\nis widely used among developers working in that language.\n\nThe \"System Libraries\" of an executable work include anything, other\nthan the work as a whole, that (a) is included in the normal form of\npackaging a Major Component, but which is not part of that Major\nComponent, and (b) serves only to enable use of the work with that\nMajor Component, or to implement a Standard Interface for which an\nimplementation is available to the public in source code form.  A\n\"Major Component\", in this context, means a major essential component\n(kernel, window system, and so on) of the specific operating system\n(if any) on which the executable work runs, or a compiler used to\nproduce the work, or an object code interpreter used to run it.\n\nThe \"Corresponding Source\" for a work in object code form means all\nthe source code needed to generate, install, and (for an executable\nwork) run the object code and to modify the work, including scripts to\ncontrol those activities.  However, it does not include the work's\nSystem Libraries, or general-purpose tools or generally available free\nprograms which are used unmodified in performing those activities but\nwhich are not part of the work.  For example, Corresponding Source\nincludes interface definition files associated with source files for\nthe work, and the source code for shared libraries and dynamically\nlinked subprograms that the work is specifically designed to require,\nsuch as by intimate data communication or control flow between those\nsubprograms and other parts of the work.\n\nThe Corresponding Source need not include anything that users\ncan regenerate automatically from other parts of the Corresponding\nSource.\n\nThe Corresponding Source for a work in source code form is that\nsame work.\n\n### 2. Basic Permissions.\n\nAll rights granted under this License are granted for the term of\ncopyright on the Program, and are irrevocable provided the stated\nconditions are met.  This License explicitly affirms your unlimited\npermission to run the unmodified Program.  The output from running a\ncovered work is covered by this License only if the output, given its\ncontent, constitutes a covered work.  This License acknowledges your\nrights of fair use or other equivalent, as provided by copyright law.\n\nYou may make, run and propagate covered works that you do not\nconvey, without conditions so long as your license otherwise remains\nin force.  You may convey covered works to others for the sole purpose\nof having them make modifications exclusively for you, or provide you\nwith facilities for running those works, provided that you comply with\nthe terms of this License in conveying all material for which you do\nnot control copyright.  Those thus making or running the covered works\nfor you must do so exclusively on your behalf, under your direction\nand control, on terms that prohibit them from making any copies of\nyour copyrighted material outside their relationship with you.\n\nConveying under any other circumstances is permitted solely under\nthe conditions stated below.  Sublicensing is not allowed; section 10\nmakes it unnecessary.\n\n### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.\n\nNo covered work shall be deemed part of an effective technological\nmeasure under any applicable law fulfilling obligations under article\n11 of the WIPO copyright treaty adopted on 20 December 1996, or\nsimilar laws prohibiting or restricting circumvention of such\nmeasures.\n\nWhen you convey a covered work, you waive any legal power to forbid\ncircumvention of technological measures to the extent such circumvention\nis effected by exercising rights under this License with respect to\nthe covered work, and you disclaim any intention to limit operation or\nmodification of the work as a means of enforcing, against the work's\nusers, your or third parties' legal rights to forbid circumvention of\ntechnological measures.\n\n### 4. Conveying Verbatim Copies.\n\nYou may convey verbatim copies of the Program's source code as you\nreceive it, in any medium, provided that you conspicuously and\nappropriately publish on each copy an appropriate copyright notice;\nkeep intact all notices stating that this License and any\nnon-permissive terms added in accord with section 7 apply to the code;\nkeep intact all notices of the absence of any warranty; and give all\nrecipients a copy of this License along with the Program.\n\nYou may charge any price or no price for each copy that you convey,\nand you may offer support or warranty protection for a fee.\n\n### 5. Conveying Modified Source Versions.\n\nYou may convey a work based on the Program, or the modifications to\nproduce it from the Program, in the form of source code under the\nterms of section 4, provided that you also meet all of these conditions:\n\n-   a) The work must carry prominent notices stating that you modified\n    it, and giving a relevant date.\n-   b) The work must carry prominent notices stating that it is\n    released under this License and any conditions added under section\n    7.  This requirement modifies the requirement in section 4 to\n    \"keep intact all notices\".\n-   c) You must license the entire work, as a whole, under this\n    License to anyone who comes into possession of a copy.  This\n    License will therefore apply, along with any applicable section 7\n    additional terms, to the whole of the work, and all its parts,\n    regardless of how they are packaged.  This License gives no\n    permission to license the work in any other way, but it does not\n    invalidate such permission if you have separately received it.\n-   d) If the work has interactive user interfaces, each must display\n    Appropriate Legal Notices; however, if the Program has interactive\n    interfaces that do not display Appropriate Legal Notices, your\n    work need not make them do so.\n\nA compilation of a covered work with other separate and independent\nworks, which are not by their nature extensions of the covered work,\nand which are not combined with it such as to form a larger program,\nin or on a volume of a storage or distribution medium, is called an\n\"aggregate\" if the compilation and its resulting copyright are not\nused to limit the access or legal rights of the compilation's users\nbeyond what the individual works permit.  Inclusion of a covered work\nin an aggregate does not cause this License to apply to the other\nparts of the aggregate.\n\n### 6. Conveying Non-Source Forms.\n\n  You may convey a covered work in object code form under the terms\nof sections 4 and 5, provided that you also convey the\nmachine-readable Corresponding Source under the terms of this License,\nin one of these ways:\n\n-   a) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by the\n    Corresponding Source fixed on a durable physical medium\n    customarily used for software interchange.\n-   b) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by a\n    written offer, valid for at least three years and valid for as\n    long as you offer spare parts or customer support for that product\n    model, to give anyone who possesses the object code either (1) a\n    copy of the Corresponding Source for all the software in the\n    product that is covered by this License, on a durable physical\n    medium customarily used for software interchange, for a price no\n    more than your reasonable cost of physically performing this\n    conveying of source, or (2) access to copy the\n    Corresponding Source from a network server at no charge.\n-   c) Convey individual copies of the object code with a copy of the\n    written offer to provide the Corresponding Source.  This\n    alternative is allowed only occasionally and noncommercially, and\n    only if you received the object code with such an offer, in accord\n    with subsection 6b.\n-   d) Convey the object code by offering access from a designated\n    place (gratis or for a charge), and offer equivalent access to the\n    Corresponding Source in the same way through the same place at no\n    further charge.  You need not require recipients to copy the\n    Corresponding Source along with the object code.  If the place to\n    copy the object code is a network server, the Corresponding Source\n    may be on a different server (operated by you or a third party)\n    that supports equivalent copying facilities, provided you maintain\n    clear directions next to the object code saying where to find the\n    Corresponding Source.  Regardless of what server hosts the\n    Corresponding Source, you remain obligated to ensure that it is\n    available for as long as needed to satisfy these requirements.\n-   e) Convey the object code using peer-to-peer transmission, provided\n    you inform other peers where the object code and Corresponding\n    Source of the work are being offered to the general public at no\n    charge under subsection 6d.\n\nA separable portion of the object code, whose source code is excluded\nfrom the Corresponding Source as a System Library, need not be\nincluded in conveying the object code work.\n\nA \"User Product\" is either (1) a \"consumer product\", which means any\ntangible personal property which is normally used for personal, family,\nor household purposes, or (2) anything designed or sold for incorporation\ninto a dwelling.  In determining whether a product is a consumer product,\ndoubtful cases shall be resolved in favor of coverage.  For a particular\nproduct received by a particular user, \"normally used\" refers to a\ntypical or common use of that class of product, regardless of the status\nof the particular user or of the way in which the particular user\nactually uses, or expects or is expected to use, the product.  A product\nis a consumer product regardless of whether the product has substantial\ncommercial, industrial or non-consumer uses, unless such uses represent\nthe only significant mode of use of the product.\n\n\"Installation Information\" for a User Product means any methods,\nprocedures, authorization keys, or other information required to install\nand execute modified versions of a covered work in that User Product from\na modified version of its Corresponding Source.  The information must\nsuffice to ensure that the continued functioning of the modified object\ncode is in no case prevented or interfered with solely because\nmodification has been made.\n\nIf you convey an object code work under this section in, or with, or\nspecifically for use in, a User Product, and the conveying occurs as\npart of a transaction in which the right of possession and use of the\nUser Product is transferred to the recipient in perpetuity or for a\nfixed term (regardless of how the transaction is characterized), the\nCorresponding Source conveyed under this section must be accompanied\nby the Installation Information.  But this requirement does not apply\nif neither you nor any third party retains the ability to install\nmodified object code on the User Product (for example, the work has\nbeen installed in ROM).\n\nThe requirement to provide Installation Information does not include a\nrequirement to continue to provide support service, warranty, or updates\nfor a work that has been modified or installed by the recipient, or for\nthe User Product in which it has been modified or installed.  Access to a\nnetwork may be denied when the modification itself materially and\nadversely affects the operation of the network or violates the rules and\nprotocols for communication across the network.\n\nCorresponding Source conveyed, and Installation Information provided,\nin accord with this section must be in a format that is publicly\ndocumented (and with an implementation available to the public in\nsource code form), and must require no special password or key for\nunpacking, reading or copying.\n\n### 7. Additional Terms.\n\n\"Additional permissions\" are terms that supplement the terms of this\nLicense by making exceptions from one or more of its conditions.\nAdditional permissions that are applicable to the entire Program shall\nbe treated as though they were included in this License, to the extent\nthat they are valid under applicable law.  If additional permissions\napply only to part of the Program, that part may be used separately\nunder those permissions, but the entire Program remains governed by\nthis License without regard to the additional permissions.\n\nWhen you convey a copy of a covered work, you may at your option\nremove any additional permissions from that copy, or from any part of\nit.  (Additional permissions may be written to require their own\nremoval in certain cases when you modify the work.)  You may place\nadditional permissions on material, added by you to a covered work,\nfor which you have or can give appropriate copyright permission.\n\nNotwithstanding any other provision of this License, for material you\nadd to a covered work, you may (if authorized by the copyright holders of\nthat material) supplement the terms of this License with terms:\n\n-   a) Disclaiming warranty or limiting liability differently from the\n    terms of sections 15 and 16 of this License; or\n-   b) Requiring preservation of specified reasonable legal notices or\n    author attributions in that material or in the Appropriate Legal\n    Notices displayed by works containing it; or\n-   c) Prohibiting misrepresentation of the origin of that material, or\n    requiring that modified versions of such material be marked in\n    reasonable ways as different from the original version; or\n-   d) Limiting the use for publicity purposes of names of licensors or\n    authors of the material; or\n-   e) Declining to grant rights under trademark law for use of some\n    trade names, trademarks, or service marks; or\n-   f) Requiring indemnification of licensors and authors of that\n    material by anyone who conveys the material (or modified versions of\n    it) with contractual assumptions of liability to the recipient, for\n    any liability that these contractual assumptions directly impose on\n    those licensors and authors; or\n-   g) Requiring strong attribution such as notices on any user interfaces\n    that run or convey any covered work, such as a prominent link to a URL\n    on the header of a website, such that all users of the covered work may\n    become aware of the notice, for a period no longer than 20 years.\n\nAll other non-permissive additional terms are considered \"further\nrestrictions\" within the meaning of section 10.  If the Program as you\nreceived it, or any part of it, contains a notice stating that it is\ngoverned by this License along with a term that is a further\nrestriction, you may remove that term.  If a license document contains\na further restriction but permits relicensing or conveying under this\nLicense, you may add to a covered work material governed by the terms\nof that license document, provided that the further restriction does\nnot survive such relicensing or conveying.\n\nIf you add terms to a covered work in accord with this section, you\nmust place, in the relevant source files, a statement of the\nadditional terms that apply to those files, or a notice indicating\nwhere to find the applicable terms.\n\nAdditional terms, permissive or non-permissive, may be stated in the\nform of a separately written license, or stated as exceptions;\nthe above requirements apply either way.\n\n### 8. Termination.\n\nYou may not propagate or modify a covered work except as expressly\nprovided under this License.  Any attempt otherwise to propagate or\nmodify it is void, and will automatically terminate your rights under\nthis License (including any patent licenses granted under the third\nparagraph of section 11).\n\nHowever, if you cease all violation of this License, then your\nlicense from a particular copyright holder is reinstated (a)\nprovisionally, unless and until the copyright holder explicitly and\nfinally terminates your license, and (b) permanently, if the copyright\nholder fails to notify you of the violation by some reasonable means\nprior to 60 days after the cessation.\n\nMoreover, your license from a particular copyright holder is\nreinstated permanently if the copyright holder notifies you of the\nviolation by some reasonable means, this is the first time you have\nreceived notice of violation of this License (for any work) from that\ncopyright holder, and you cure the violation prior to 30 days after\nyour receipt of the notice.\n\nTermination of your rights under this section does not terminate the\nlicenses of parties who have received copies or rights from you under\nthis License.  If your rights have been terminated and not permanently\nreinstated, you do not qualify to receive new licenses for the same\nmaterial under section 10.\n\n### 9. Acceptance Not Required for Having Copies.\n\nYou are not required to accept this License in order to receive or\nrun a copy of the Program.  Ancillary propagation of a covered work\noccurring solely as a consequence of using peer-to-peer transmission\nto receive a copy likewise does not require acceptance.  However,\nnothing other than this License grants you permission to propagate or\nmodify any covered work.  These actions infringe copyright if you do\nnot accept this License.  Therefore, by modifying or propagating a\ncovered work, you indicate your acceptance of this License to do so.\n\n### 10. Automatic Licensing of Downstream Recipients.\n\nEach time you convey a covered work, the recipient automatically\nreceives a license from the original licensors, to run, modify and\npropagate that work, subject to this License.  You are not responsible\nfor enforcing compliance by third parties with this License.\n\nAn \"entity transaction\" is a transaction transferring control of an\norganization, or substantially all assets of one, or subdividing an\norganization, or merging organizations.  If propagation of a covered\nwork results from an entity transaction, each party to that\ntransaction who receives a copy of the work also receives whatever\nlicenses to the work the party's predecessor in interest had or could\ngive under the previous paragraph, plus a right to possession of the\nCorresponding Source of the work from the predecessor in interest, if\nthe predecessor has it or can get it with reasonable efforts.\n\nYou may not impose any further restrictions on the exercise of the\nrights granted or affirmed under this License.  For example, you may\nnot impose a license fee, royalty, or other charge for exercise of\nrights granted under this License, and you may not initiate litigation\n(including a cross-claim or counterclaim in a lawsuit) alleging that\nany patent claim is infringed by making, using, selling, offering for\nsale, or importing the Program or any portion of it.\n\n### 11. Patents.\n\nA \"contributor\" is a copyright holder who authorizes use under this\nLicense of the Program or a work on which the Program is based.  The\nwork thus licensed is called the contributor's \"contributor version\".\n\nA contributor's \"essential patent claims\" are all patent claims\nowned or controlled by the contributor, whether already acquired or\nhereafter acquired, that would be infringed by some manner, permitted\nby this License, of making, using, or selling its contributor version,\nbut do not include claims that would be infringed only as a\nconsequence of further modification of the contributor version.  For\npurposes of this definition, \"control\" includes the right to grant\npatent sublicenses in a manner consistent with the requirements of\nthis License.\n\nEach contributor grants you a non-exclusive, worldwide, royalty-free\npatent license under the contributor's essential patent claims, to\nmake, use, sell, offer for sale, import and otherwise run, modify and\npropagate the contents of its contributor version.\n\nIn the following three paragraphs, a \"patent license\" is any express\nagreement or commitment, however denominated, not to enforce a patent\n(such as an express permission to practice a patent or covenant not to\nsue for patent infringement).  To \"grant\" such a patent license to a\nparty means to make such an agreement or commitment not to enforce a\npatent against the party.\n\nIf you convey a covered work, knowingly relying on a patent license,\nand the Corresponding Source of the work is not available for anyone\nto copy, free of charge and under the terms of this License, through a\npublicly available network server or other readily accessible means,\nthen you must either (1) cause the Corresponding Source to be so\navailable, or (2) arrange to deprive yourself of the benefit of the\npatent license for this particular work, or (3) arrange, in a manner\nconsistent with the requirements of this License, to extend the patent\nlicense to downstream recipients.  \"Knowingly relying\" means you have\nactual knowledge that, but for the patent license, your conveying the\ncovered work in a country, or your recipient's use of the covered work\nin a country, would infringe one or more identifiable patents in that\ncountry that you have reason to believe are valid.\n\nIf, pursuant to or in connection with a single transaction or\narrangement, you convey, or propagate by procuring conveyance of, a\ncovered work, and grant a patent license to some of the parties\nreceiving the covered work authorizing them to use, propagate, modify\nor convey a specific copy of the covered work, then the patent license\nyou grant is automatically extended to all recipients of the covered\nwork and works based on it.\n\nA patent license is \"discriminatory\" if it does not include within\nthe scope of its coverage, prohibits the exercise of, or is\nconditioned on the non-exercise of one or more of the rights that are\nspecifically granted under this License.  You may not convey a covered\nwork if you are a party to an arrangement with a third party that is\nin the business of distributing software, under which you make payment\nto the third party based on the extent of your activity of conveying\nthe work, and under which the third party grants, to any of the\nparties who would receive the covered work from you, a discriminatory\npatent license (a) in connection with copies of the covered work\nconveyed by you (or copies made from those copies), or (b) primarily\nfor and in connection with specific products or compilations that\ncontain the covered work, unless you entered into that arrangement,\nor that patent license was granted, prior to 28 March 2007.\n\nNothing in this License shall be construed as excluding or limiting\nany implied license or other defenses to infringement that may\notherwise be available to you under applicable patent law.\n\n### 12. No Surrender of Others' Freedom.\n\nIf conditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot convey a\ncovered work so as to simultaneously satisfy your obligations under this\nLicense and any other pertinent obligations, then as a consequence you may\nnot convey it at all.  For example, if you agree to terms that obligate you\nto collect a royalty for further conveying from those to whom you convey\nthe Program, the only way you could satisfy both those terms and this\nLicense would be to refrain entirely from conveying the Program.\n\n### 13. Remote Network Interaction; Use with the GNU General Public License.\n\nNotwithstanding any other provision of this License, if you modify the\nProgram, your modified version must prominently offer all users\ninteracting with it remotely through a computer network (if your version\nsupports such interaction) an opportunity to receive the Corresponding\nSource of your version by providing access to the Corresponding Source\nfrom a network server at no charge, through some standard or customary\nmeans of facilitating copying of software.  This Corresponding Source\nshall include the Corresponding Source for any work covered by version 3\nof the GNU General Public License that is incorporated pursuant to the\nfollowing paragraph.\n\nNotwithstanding any other provision of this License, you have\npermission to link or combine any covered work with a work licensed\nunder version 3 of the GNU General Public License into a single\ncombined work, and to convey the resulting work.  The terms of this\nLicense will continue to apply to the part which is the covered work,\nbut the work with which it is combined will remain governed by version\n3 of the GNU General Public License.\n\n### 14. Revised Versions of this License.\n\nNewTendermint LLC may publish revised and/or new versions of\nthe GNO Network General Public License from time to time.  Such new versions\nwill be similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\nEach version is given a distinguishing version number.  If the\nProgram specifies that a certain numbered version of the GNO Network General\nPublic License \"or any later version\" applies to it, you have the\noption of following the terms and conditions either of that numbered\nversion or of any later version published by the Gno Software\nFoundation.  If the Program does not specify a version number of the\nGNO Network General Public License, you may choose any version ever published\nby NewTendermint LLC.\n\nIf the Program specifies that a proxy can decide which future\nversions of the GNO Network General Public License can be used, that proxy's\npublic statement of acceptance of a version permanently authorizes you\nto choose that version for the Program.\n\nLater license versions may give you additional or different\npermissions.  However, no additional obligations are imposed on any\nauthor or copyright holder as a result of your choosing to follow a\nlater version.\n\n### 15. Disclaimer of Warranty.\n\nTHERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\nAPPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\nHOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\nOF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\nTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\nIS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\nALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\n### 16. Limitation of Liability.\n\nIN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS\nTHE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY\nGENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE\nUSE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF\nDATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\nPARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),\nEVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF\nSUCH DAMAGES.\n\n### 17. Interpretation of Sections 15 and 16.\n\nIf the disclaimer of warranty and limitation of liability provided\nabove cannot be given local legal effect according to their terms,\nreviewing courts shall apply local law that most closely approximates\nan absolute waiver of all civil liability in connection with the\nProgram, unless a warranty or assumption of liability accompanies a\ncopy of the Program in return for a fee.\n\nEND OF TERMS AND CONDITIONS\n\n## How to Apply These Terms to Your New Programs\n\nIf you develop a new program, and you want it to be of the greatest\npossible use to the public, the best way to achieve this is to make it\nfree software which everyone can redistribute and change under these terms.\n\nTo do so, attach the following notices to the program.  It is safest\nto attach them to the start of each source file to most effectively\nstate the exclusion of warranty; and each file should have at least\nthe \"copyright\" line and a pointer to where the full notice is found.\n\n    \u003cone line to give the program's name and a brief idea of what it does.\u003e\n    Copyright (C) \u003cyear\u003e  \u003cname of author\u003e\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNO Network General Public License as published by\n    NewTendermint LLC, either version 4 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNO Network General Public License for more details.\n\n    You should have received a copy of the GNO Network General Public License\n    along with this program.  If not, see \u003chttps://gno.land/license\u003e.\n\nAlso add information on how to contact you by electronic and paper mail.\n\nIf your software can interact with users remotely through a computer\nnetwork, you should also make sure that it provides a way for users to\nget its source.  For example, if your program is a web application, its\ninterface could display a \"Source\" link that leads users to an archive\nof the code.  There are many ways you could offer source, and different\nsolutions will be better for different programs; see section 13 for the\nspecific requirements.\n`\n\t)\n\tif err := b.NewPost(\"\", path, title, body, \"2024-04-22T00:00:00Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_links.gno","body":"package pages\n\nfunc init() {\n\tpath := \"links\"\n\ttitle := \"Gno.land Linker\"\n\tbody := `\n\nBelow are Gno.land's most important links.\n\n#### [Gno GitHub, the project's main repository](https://github.com/gnolang/gno)\n#### [Getting Started with Gno](https://github.com/gnolang/getting-started/)\n#### [Gno.land Official Docs](https://docs.gno.land/)\n#### [Gnoverse Github, a community organization for Gnomes](https://github.com/gnoverse)\n#### [Gno.land Workshops, a repo for Gno resources](https://github.com/gnolang/workshops)\n#### [Gno.land Students Program](https://github.com/gnolang/student-contributors-program/)\n#### [Gno.land Contributor page](https://gno.land/contribute)\n#### [Gno.land Discord](https://discord.gg/gnoland)\n#### [Gno.land X](https://x.com/_gnoland)\n#### [Gno.land YouTube](https://www.youtube.com/@_gnoland)\n#### [Gno.land Playground, an online editor for exploring Gno.land](https://play.gno.land)\n#### [GnoScan, a Gno.land block explorer](https://gnoscan.io/)\n`\n\tif err := b.NewPost(\"\", path, title, body, \"2025-06-23T13:17:30Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_newsletter.gno","body":"package pages\n\nimport (\n\t\"gno.land/p/leon/svgbtn\"\n)\n\nfunc init() {\n\tpath := \"newsletter\"\n\ttitle := \"Sign up for Gno News\"\n\tbody := \"**Stay in the Gno by signing up for our newsletter. You'll get the scoop on dev updates, fresh content, and community news.**\\n\\n\"\n\tbody += svgbtn.Button(\n\t\t256,\n\t\t44,\n\t\t\"#226c57\",\n\t\t\"#ffffff\",\n\t\t\"Subscribe to stay in the Gno\",\n\t\t\"https://land.us18.list-manage.com/subscribe?u=8befe3303cf82796d2c1a1aff\u0026id=271812000b\",\n\t)\n\n\tif err := b.NewPost(\"\", path, title, body, \"2025-07-14T00:00:00Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_partners.gno","body":"package pages\n\nfunc init() {\n\tpath := \"partners\"\n\ttitle := \"Partnerships\"\n\tbody := `### Fund and Grants Program\n\nAre you a builder, tinkerer, or researcher? If you’re looking to create awesome dApps, tooling, infrastructure, \nor smart contract libraries on Gno.land, you can apply for a grant. The Gno.land Ecosystem Fund and Grants program \nprovides financial contributions for individuals and teams to innovate on the platform.\n\nRead more about our Funds and Grants program [here](https://github.com/gnolang/grants).\n`\n\tif err := b.NewPost(\"\", path, title, body, \"2022-05-20T13:17:25Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_start.gno","body":"package pages\n\nfunc init() {\n\tpath := \"start\"\n\ttitle := \"Getting Started with Gno\"\n\tbody := `## Getting Started with Gno\n\nVisit the [getting-started](https://github.com/gnolang/getting-started) repo to try Gno in 5 minutes!\n`\n\tif err := b.NewPost(\"\", path, title, body, \"2022-05-20T13:17:26Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_testnets.gno","body":"package pages\n\nfunc init() {\n\tpath := \"testnets\"\n\ttitle := \"Gno.land Testnet List\"\n\tbody := `\n- [Staging](https://docs.gno.land/resources/gnoland-networks/#staging-environments-portal-loops) - a rolling testnet\n\nFor a list of RPC endpoints, see the [reference documentation](https://docs.gno.land/reference/rpc-endpoints).\n\n## Local development\n\nSee the \"Build on Gno.land\" section in the [Official Documentation](https://docs.gno.land/).\n`\n\tif err := b.NewPost(\"\", path, title, body, \"2022-05-20T13:17:27Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"page_tokenomics.gno","body":"package pages\n\nfunc init() {\n\tvar (\n\t\tpath  = \"tokenomics\"\n\t\ttitle = \"Gno.land Tokenomics\"\n\t\t// XXX: description = \"\"\"\n\t\tbody = `Lorem Ipsum`\n\t)\n\tif err := b.NewPost(\"\", path, title, body, \"2022-05-20T13:17:28Z\", nil, nil); err != nil {\n\t\tpanic(err)\n\t}\n\n}\n"},{"name":"pages.gno","body":"package pages\n\nimport (\n\t\"gno.land/p/demo/blog\"\n)\n\n// TODO: switch from p/blog to p/pages\n\nvar b = \u0026blog.Blog{\n\tTitle:        \"Gnoland's Pages\",\n\tPrefix:       \"/r/gnoland/pages:\",\n\tNoBreadcrumb: true,\n}\n\nfunc Render(path string) string {\n\treturn b.Render(path)\n}\n"},{"name":"pages_test.gno","body":"package pages\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestHome(t *testing.T) {\n\tprintedOnce := false\n\tgot := Render(\"\")\n\n\texpectedSubtrings := []string{\n\t\t\"/r/gnoland/pages:p/tokenomics\",\n\t\t\"/r/gnoland/pages:p/links\",\n\t\t\"/r/gnoland/pages:p/contribute\",\n\t\t\"/r/gnoland/pages:p/start\",\n\t\t\"/r/gnoland/pages:p/about\",\n\t\t\"/r/gnoland/pages:p/gnolang\",\n\t}\n\n\tfor _, substring := range expectedSubtrings {\n\t\tif !strings.Contains(got, substring) {\n\t\t\tif !printedOnce {\n\t\t\t\tprintedOnce = true\n\t\t\t}\n\t\t\tt.Errorf(\"expected %q, but not found.\", substring)\n\t\t}\n\t}\n}\n\nfunc TestAbout(t *testing.T) {\n\tprintedOnce := false\n\tgot := Render(\"p/about\")\n\texpectedSubtrings := []string{\n\t\t\"Gno.land Is A Platform To Write Smart Contracts In Gno\",\n\t\t\"Gno.land is a next-generation smart contract platform using Gno, an interpreted version of the general-purpose Go\\nprogramming language.\",\n\t}\n\tfor _, substring := range expectedSubtrings {\n\t\tif !strings.Contains(got, substring) {\n\t\t\tif !printedOnce {\n\t\t\t\tprintedOnce = true\n\t\t\t}\n\t\t\tt.Errorf(\"expected %q, but not found.\", substring)\n\t\t}\n\t}\n}\n"},{"name":"util.gno","body":"package pages\n\nfunc checkErr(err error) {\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"fmMoAGvPohrL40DHXfls43QZmxw2ZzK2aMsE15dfRv+t4UJdl0aZ4xj9urXTakexQX5xWJ9pd5tFhJKbb4WPAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7","package":{"name":"monit","path":"gno.land/r/gnoland/monit","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/gnoland/monit\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7\"\n"},{"name":"monit.gno","body":"// Package monit links a monitoring system with the chain in both directions.\n//\n// The agent will periodically call Incr() and verify that the value is always\n// higher than the previously known one. The contract will store the last update\n// time and use it to detect whether or not the monitoring agent is functioning\n// correctly.\npackage monit\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/watchdog/v0\"\n)\n\nvar (\n\tcounter          int\n\tlastUpdate       time.Time\n\tlastCaller       address\n\twatchdogDuration = 5 * time.Minute\n\twd               = watchdog.Watchdog{Duration: watchdogDuration}\n\tOwnable          *ownable.Ownable\n)\n\nfunc init(cur realm) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"realm must be initialized by EOA\")\n\t}\n\tOwnable = ownable.NewWithAddress(cur.Previous().Address())\n}\n\n// Incr increments the counter and informs the watchdog that we're alive.\n// This function can be called by anyone.\nfunc Incr(cur realm) int {\n\tcounter++\n\tlastUpdate = time.Now()\n\tlastCaller = cur.Previous().Address()\n\twd.Alive()\n\treturn counter\n}\n\n// Reset resets the realm state.\n// This function can only be called by the admin.\nfunc Reset(cur realm) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\tcounter = 0\n\tlastCaller = cur.Previous().Address()\n\tlastUpdate = time.Now()\n\twd = watchdog.Watchdog{Duration: watchdogDuration}\n}\n\nfunc Render(_ string) string {\n\tstatus := wd.Status()\n\treturn ufmt.Sprintf(\n\t\t\"counter=%d\\nlast update=%s\\nlast caller=%s\\nstatus=%s\",\n\t\tcounter, lastUpdate, lastCaller, status,\n\t)\n}\n"},{"name":"monit_test.gno","body":"package monit\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/watchdog/v0\"\n)\n\nfunc initTest() {\n\tcounter = 0\n\tlastUpdate = time.Time{}\n\tlastCaller = address(\"\")\n\twd = watchdog.Watchdog{Duration: watchdogDuration}\n\tcreator := address(\"g1creator\")\n\tOwnable = ownable.NewWithAddress(creator)\n}\n\nfunc TestPackage(cur realm, t *testing.T) {\n\tinitTest()\n\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\n\t// initial state, watchdog is KO.\n\t{\n\t\texpected := `counter=0\nlast update=0001-01-01 00:00:00 +0000 UTC\nlast caller=\nstatus=KO`\n\t\tgot := Render(\"\")\n\t\tuassert.Equal(t, expected, got)\n\t}\n\n\t// call Incr(), watchdog is OK.\n\tIncr(cross(cur))\n\tIncr(cross(cur))\n\tIncr(cross(cur))\n\t{\n\t\texpected := `counter=3\nlast update=2009-02-13 23:31:30 +0000 UTC\nlast caller=g1user\nstatus=OK`\n\t\tgot := Render(\"\")\n\t\tuassert.Equal(t, expected, got)\n\t}\n\n\t/* XXX: improve tests once we've the missing std.TestSkipTime feature\n\t\t// wait 1h, watchdog is KO.\n\t\tuse std.TestSkipTime(time.Hour)\n\t\t{\n\t\t\texpected := `counter=3\n\tlast update=2009-02-13 22:31:30 +0000 UTC\n\tlast caller=g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n\tstatus=KO`\n\t\t\tgot := Render(\"\")\n\t\t\tuassert.Equal(t, expected, got)\n\t\t}\n\n\t\t// call Incr(), watchdog is OK.\n\t\tIncr()\n\t\t{\n\t\t\texpected := `counter=4\n\tlast update=2009-02-13 23:31:30 +0000 UTC\n\tlast caller=g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm\n\tstatus=OK`\n\t\t\tgot := Render(\"\")\n\t\t\tuassert.Equal(t, expected, got)\n\t\t}\n\t*/\n}\n\nfunc TestReset(cur realm, t *testing.T) {\n\tinitTest()\n\n\t// Initial state check\n\tinitialCounter := counter\n\tinitialLastUpdate := lastUpdate\n\tinitialStatus := wd.Status()\n\n\t// Call Incr to change the state\n\tuser := address(\"g1user\")\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\tIncr(cross(cur))\n\tuassert.True(t, counter \u003e initialCounter, \"counter should have increased after Incr\")\n\tuassert.True(t, lastUpdate.After(initialLastUpdate), \"lastUpdate should have been updated after Incr\")\n\tuassert.Equal(t, user, lastCaller, \"lastCaller mismatch\")\n\tuassert.NotEqual(t, initialStatus, wd.Status(), \"watchdog status should have changed after Incr\") // Status changes after Alive() is called\n\n\t// Call Reset as the owner\n\townerAddr := Ownable.Owner()\n\ttesting.SetRealm(testing.NewUserRealm(ownerAddr)) // Simulate call from the owner\n\tReset(cross(cur))\n\tuassert.Equal(t, 0, counter, \"counter should be 0 after Reset\")\n\tuassert.Equal(t, ownerAddr, lastCaller, \"lastCaller should be the owner address after Reset\")\n\tuassert.Equal(t, watchdogDuration.String(), wd.Duration.String(), \"watchdog duration mismatch after Reset\")\n\tuassert.Equal(t, \"KO\", wd.Status(), \"watchdog status should be KO after Reset\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"EFnpj01djVW9N43jOB5ioQEo1vw77yv1ADcY+JGOzWbCl5Mgo86sjhx1jJJFLYfUvpjzwkdN36d4WNGLjkLvAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/grepsuzette/home","files":[{"name":"README.md","body":"Dwilgelindildong, traveler.\n\n"},{"name":"contribs.gno","body":"package home\n\nfunc r3() string {\n\treturn `# Greps' notable contributions\n\nMy main contributions until the gno.land beta launch are listed below; most aren't in the monorepo, note.\n\n### Port Joeson from coffeescript to golang\n\nWorked on this from june 2022 until january 2023. Bounty [applied for](https://github.com/gnolang/bounties-old/issues/33) on Feb 2, 2023. \n\nHere is the port I did in Go: [grepsuzette/joeson](https://github.com/grepsuzette/joeson/).\n\n    4. Port JOESON to Go\n    github.com/jaekwon/joescript\n    The intent is to create an independent left-recursive PEG parser for Gno.\n    Optional: port Joescript or Javascript.\n    1000 ATOMs from @jaekwon\n    More GNOTs than from #3.\n\nThere have been many examples posted, including a minimal [LISP REPL](https://github.com/grepsuzette/joeson/tree/master/examples/lisp-repl) and a theorical [study on precedence](https://github.com/grepsuzette/joeson/blob/master/examples/precedence/precedence_test.go) (precedence is often problematic with PEG parsers, this allowed to find a solution, used in the next part). \n\n### GNO grammar - partial\n\nIn summer 2023, started to port the GNO grammar using Joeson (since there was no news about joeson, so this was an attempt to demonstrate it worked). Grammar was posted in [PR 1156](https://github.com/gnolang/gno/pull/1156). There are only 3 files, they are quite dense:\n\n1. [joeson_test.go](https://github.com/grepsuzette/gno/blob/joeson/gnovm/pkg/gnolang/joeson_test.go)\n1. [joeson_rules.go](https://github.com/grepsuzette/gno/blob/joeson/gnovm/pkg/gnolang/joeson_rules.go)\n1. [joeson_f.go](https://github.com/grepsuzette/gno/blob/joeson/gnovm/pkg/gnolang/joeson_f.go)\n\n### gnAsteroid\n\n![asteroid](https://raw.githubusercontent.com/grepsuzette/gfx/master/asteroid160.png)\n\n**gnAsteroid** is an asteroid creation-kit, it was started around the time the joeson port was published, but didn't have a name back then. \n\nAsteroids orbit gno.land, it's the same blockchain, but different frontend,\nthemable, working with wiki-like markdown files (enabling realms from gno.land\nto be rendered there).\n\n* [asteroid 0](https://gnAsteroid.com) - asteroid explaining what it is, containing instructions, to use, deploy on [Akash](https://gnasteroid.com/publishing/akash.md), [Vercel](https://gnasteroid.com/publishing/vercel.md).\n* [greps' asteroid](https://greps.gnAsteroid.com)\n* [gnAsteroid](https://github.com/gnAsteroid/gnAsteroid) - The github for gnAsteroid.\n\n### Research with markdown and gnoweb, mini-games, experiments (summer-oct 2024)\n\nA series of experiments with gnoweb 1.0 lead from the summer 2024, to try to advocate for keeping html\nand css enabled in gnoweb, or at least to try to determine what we could\npotentially miss without. Gnoweb1.0, markdown, html, css, js-less.\n\nNote those still work with [gnAsteroid](https://gnAsteroid.com), or with gnoweb\nrunning with the -web-html switch. As of now they are rendered through an\nasteroid.\n\n| 1                                                                                   | 2                                                                                     |\n| :-------------------:                                                               | :-------------------------:                                                           |\n| ![parrot](https://raw.githubusercontent.com/grepsuzette/gfx/master/parrot160.png)   | ![octopus](https://raw.githubusercontent.com/grepsuzette/gfx/master/octopus160.png)   |\n| [tic-tac-toe](https://greps.gnAsteroid.com/r/grepsuzette/pr2554/v6/games/tictactoe) | [minesweeper](https://greps.gnAsteroid.com/r/grepsuzette/pr2554/v6/games/minesweeper) |\n\nCheck the [other experiments here](/conjects/gnoweb.md).\n\n![octopus](https://raw.githubusercontent.com/grepsuzette/gfx/master/screen-minesweeper390.png)\n\n### Tendermint vuln retrospective (2023)\n\nAlso worked on an anthology of publicly knowned vulnerabilities that affected Tendermint. \n\n* [Cosmos-sdk vulnerability retrospective](https://github.com/gnolang/gno/issues/587)\n* found most vulns were not affecting our Tendermint version, however:\n* [demonstrated vulnerability to BSC 2022-10-07 hack](https://github.com/gnolang/gno/pull/583)\n* [proposed fix to vuln to BSC 2022-10-07 hack (merged)](https://github.com/gnolang/gno/pull/584)\n* not all of them were tested, as I was hoping some more feedback before to continue.\n\nThere is also a small [GNO mail](https://github.com/gnolang/gno/pull/641) which got no UI is discussed in [one of my articles](https://greps.gnasteroid.com/articles/encryptedmail.md).\n\nThanks for reading!\n`\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/grepsuzette/home\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package home\n\nfunc Render(path string) string {\n\tswitch path {\n\tcase \"3\":\n\t\treturn r3()\n\tcase \"2\":\n\t\treturn r2()\n\tdefault:\n\t\treturn r1()\n\t}\n}\n\nconst tripleBackquote = \"```\"\nconst art = `\n                           (    )\n                          (    )\n                            )  )\n                           (  (                  /\\\n                            (_)                 /  \\  /\\\n                    ________[_]________      /\\/    \\/  \\\n           /\\      /\\        ______    \\    /   /\\/\\  /\\/\\\n          /  \\    //_\\       \\    /\\    \\  /\\/\\/    \\/    \\\n   /\\    / /\\/\\  //___\\       \\__/  \\    \\/      +     '   \n  /  \\  /\\/    \\//_____\\       \\ |[]|     \\    .    t     .\n /\\/\\/\\/       //_______\\       \\|__|      \\     p    e    \n/      \\      /XXXXXXXXXX\\                  \\      o    l  \n        \\    /_I_II  I__I_\\__________________\\  +    r    e\n               I_I|  I__I_____[]_|_[]_____I      .     t   \n               I_II  I__I_____[]_|_[]_____I          +    '\n               I II__I  I     XXXXXXX     I    \n            ~~~~~\"   \"~~~~~~~~~~~~~~~~~~~~~~~~   :*:*:*:*:*\n`\n\nfunc r1() string {\n\treturn \"# greps' (gn)home\" +\n\t\t`\nYou've reached the terrestrial realms of Grepsuzette on gno.land. \n\n` + tripleBackquote + art + tripleBackquote + `\n\nI am often on my [GNO asteroid](https://greps.gnAsteroid.com) too.\n\n* Public address: g1fjh9y7ausp27dqsdq0qrcsnmgvwm6829v2au7d\n* Contributor since summer 2022 ([notable contributions](/r/grepsuzette/home:3))\n* You can try my games in GNO (they use gnoweb -html):\n  * [tic-tac-toe](https://greps.gnasteroid.com/r/grepsuzette/pr2554/v6/games/tictactoe)\n  * [minesweeper](https://greps.gnasteroid.com/r/grepsuzette/pr2554/v6/games/minesweeper)\n`\n}\n\nfunc r2() string {\n\treturn `A manual index, until there's an automated way:\n\n* [home](home/): greps' home on gno.land\n* [games](games/): series of games\n\nI'm often on my [GNO asteroid][1] too.\n\n[1]: https://greps.gnAsteroid.com\n`\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"7idVdt5746nG5P1/at9xZ/e9jIU2eXTb8QtVRk6+JjUWmiAeW48ebFtXOQhJHavsNwKB0zXODhuubXeWWdpjBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"eventix","path":"gno.land/r/jjoptimist/eventix","files":[{"name":"eventix.gno","body":"package eventix\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\ntype Event struct {\n\tname        string\n\tdescription string\n\tdate        time.Time\n\tmaxTickets  int\n\tprice       uint64\n\t// SECURITY XXX: paymentToken is `any` and accepted from the event\n\t// creator. We only check the dynamic type (*grc20.Token or \"ugnot\"),\n\t// not the token's identity. An attacker can deploy their own grc20\n\t// token (the *grc20.Token type is canonical, so the type check passes)\n\t// where they hold an unlimited \"balance\" on every address. Buyers\n\t// then pay for tickets in worthless counterfeit tokens; the eventix\n\t// realm thinks it got paid and mints real NFT tickets. Free tickets\n\t// for the attacker.\n\t// Fix: restrict paymentToken to tokens registered in r/demo/defi/grc20reg\n\t// (or a hardcoded allowlist of trusted token pkgPaths). Reject any\n\t// *grc20.Token that doesn't come from a known good source.\n\tpaymentToken any\n\tticketsSold  int\n}\n\nvar (\n\tevents              = avl.NewTree()\n\teventCounter uint64 = 0\n\ttickets      *grc721.BasicNFT\n)\n\nfunc init(cur realm) {\n\ttickets = grc721.NewBasicNFT(0, cur, \"Event Ticket\", \"EVTIX\")\n}\n\nfunc CreateEvent(_ realm, name, description, dateStr string, paymentToken any, maxTickets int, price uint64) uint64 {\n\t// validate inputs\n\tif maxTickets \u003c= 0 {\n\t\tpanic(\"Maximum tickets must be greater than 0\")\n\t}\n\n\tdate, err := time.Parse(\"2006-01-02T15:04:05Z\", dateStr)\n\tif err != nil {\n\t\tpanic(\"Invalid date format. Use: YYYY-MM-DDThh:mm:ssZ\")\n\t}\n\n\tswitch pt := paymentToken.(type) {\n\tcase string:\n\t\tif pt != \"ugnot\" {\n\t\t\tpanic(\"Unsupported native token\")\n\t\t}\n\tcase *grc20.Token:\n\t\tif pt == nil {\n\t\t\tpanic(\"Invalid GRC20 token\")\n\t\t}\n\tdefault:\n\t\tpanic(\"Unsupported payment token type\")\n\t}\n\n\tnewID := eventCounter + 1\n\tevent := Event{\n\t\tname:         name,\n\t\tdescription:  description,\n\t\tdate:         date,\n\t\tmaxTickets:   maxTickets,\n\t\tprice:        price,\n\t\tticketsSold:  0,\n\t\tpaymentToken: paymentToken,\n\t}\n\tevents.Set(strconv.Itoa(int(newID)), event)\n\teventCounter = newID\n\tchain.Emit(\"EventCreated\", \"id\", strconv.FormatUint(newID, 10), \"name\", name)\n\treturn newID\n}\n\nfunc BuyTicket(cur realm, eventId uint64) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\tevent, exists := getEvent(eventId)\n\tif !exists {\n\t\tpanic(\"Event does not exist\")\n\t}\n\n\tif event.ticketsSold \u003e= event.maxTickets {\n\t\tpanic(\"Event is sold out\")\n\t}\n\n\tbuyer := cur.Previous().Address()\n\n\tswitch pt := event.paymentToken.(type) {\n\tcase string:\n\t\tif pt != \"ugnot\" {\n\t\t\tpanic(\"Unsupported native token\")\n\t\t}\n\t\tif unsafe.OriginSend().AmountOf(\"ugnot\") != int64(event.price) {\n\t\t\tpanic(ufmt.Sprintf(\"Invalid payment amount: needs to be %dugnot\", event.price))\n\t\t}\n\tcase *grc20.Token:\n\t\tif err := pt.CallerTeller().Transfer(0, cur, cur.Address(), int64(event.price)); err != nil {\n\t\t\tpanic(\"GRC20 transfer error: \" + err.Error())\n\t\t}\n\tdefault:\n\t\tpanic(\"Unsupported payment token type\")\n\t}\n\n\t// Mint NFT ticket\n\ttokenId := grc721.TokenID(ufmt.Sprintf(\"event_%d_ticket_%d\", eventId, event.ticketsSold+1))\n\ttickets.Mint(buyer, tokenId)\n\n\tevent.ticketsSold++\n\tevents.Set(strconv.Itoa(int(eventId)), event)\n}\n\nfunc Render(path string) string {\n\toutput := \"# Event Ticketing System\\n\\n\"\n\n\tpg := pager.NewPager(events, 10, true).MustGetPageByPath(path)\n\n\tfor _, item := range pg.Items {\n\t\tid, _ := strconv.ParseUint(item.Key, 10, 64)\n\t\tevent := item.Value.(Event)\n\n\t\toutput += ufmt.Sprintf(\"## Event #%d: %s\\n\", id, event.name)\n\t\toutput += ufmt.Sprintf(\"Description: %s\\n\", event.description)\n\t\toutput += ufmt.Sprintf(\"Date: %s\\n\", event.date.Format(time.RFC3339))\n\t\toutput += ufmt.Sprintf(\"Tickets: %d/%d\\n\", event.ticketsSold, event.maxTickets)\n\t\toutput += ufmt.Sprintf(\"Price: %d %v\\n\\n\", event.price, event.paymentToken)\n\n\t\tif event.ticketsSold \u003c event.maxTickets {\n\t\t\toutput += ufmt.Sprintf(\"[Buy Ticket](/r/jjoptimist/eventix/BuyTicket?eventId=%d)\\n\", id)\n\t\t} else {\n\t\t\toutput += \"**SOLD OUT**\\n\"\n\t\t}\n\t\toutput += \"---\\n\\n\"\n\t}\n\n\toutput += pg.Picker(path)\n\n\treturn output\n}\n\nfunc getEvent(eventId uint64) (Event, bool) {\n\tvalue := events.Get(strconv.Itoa(int(eventId)))\n\tif value == nil {\n\t\treturn Event{}, false\n\t}\n\treturn value.(Event), true\n}\n"},{"name":"eventix_test.gno","body":"package eventix\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\t\"testing\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestCreateEvent(cur realm, t *testing.T) {\n\t// Test successful event creation\n\teventId := CreateEvent(\n\t\tcross(cur),\n\t\t\"Test Event\",\n\t\t\"A test event\",\n\t\t\"2024-12-31T23:59:59Z\",\n\t\t\"ugnot\",\n\t\t100,\n\t\t1000000,\n\t)\n\n\t// Convert eventId to int for comparison since CreateEvent returns uint64\n\tuassert.Equal(t, uint64(1), eventId)\n\n\tevent, exists := getEvent(eventId)\n\turequire.True(t, exists, \"Event was not created\")\n\tuassert.Equal(t, \"Test Event\", event.name)\n\n\t// Test invalid date format\n\turequire.AbortsWithMessage(t, cur, \"Invalid date format. Use: YYYY-MM-DDThh:mm:ssZ\", func() {\n\t\tCreateEvent(cross(cur), \"Test\", \"Test\", \"invalid-date\", \"ugnot\", 100, 1000000)\n\t})\n}\n\nfunc TestBuyTicket(cur realm, t *testing.T) {\n\t// Setup test event\n\teventId := CreateEvent(\n\t\tcross(cur),\n\t\t\"Test Event\",\n\t\t\"A test event\",\n\t\t\"2024-12-31T23:59:59Z\",\n\t\t\"ugnot\",\n\t\t2,\n\t\t1000000,\n\t)\n\n\t// Setup test buyer\n\tbuyer := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\ttesting.SetOriginCaller(buyer)\n\n\tuassert.AbortsWithMessage(t, cur, \"Invalid payment amount: needs to be 1000000ugnot\", func() {\n\t\tBuyTicket(cross(cur), eventId)\n\t})\n\n\t// Test successful purchase\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", 1000000)))\n\tBuyTicket(cross(cur), eventId)\n\n\tevent, _ := getEvent(eventId)\n\tif event.ticketsSold != 1 {\n\t\tt.Errorf(\"Expected 1 ticket sold, got %d\", event.ticketsSold)\n\t}\n\n\t// Verify NFT ownership\n\ttokenId := grc721.TokenID(ufmt.Sprintf(\"event_%d_ticket_%d\", eventId, 1))\n\towner, err := tickets.OwnerOf(tokenId)\n\turequire.NoError(t, err)\n\turequire.Equal(t, buyer, owner)\n\n\t// Test buying sold out event\n\ttesting.SetOriginSend(chain.NewCoins(chain.NewCoin(\"ugnot\", 1000000)))\n\tBuyTicket(cross(cur), eventId) // Buy second ticket\n\n\tuassert.AbortsWithMessage(t, cur, \"Event is sold out\", func() {\n\t\tBuyTicket(cross(cur), eventId)\n\t})\n}\n\nfunc TestBuyTicketWithGRC20(cur realm, t *testing.T) {\n\tpkgAddr := chain.PackageAddress(\"gno.land/r/jjoptimist/eventix\")\n\talice := testutils.TestAddress(\"alice\")\n\n\t// Token construction needs a realm-context cur (NewToken asserts\n\t// IsCurrent + non-empty PkgPath); SetRealm-to-EOA happens after.\n\tvar token *grc20.Token\n\tvar ledger *grc20.PrivateLedger\n\tfunc(cur realm) {\n\t\ttoken, ledger = grc20.NewToken(0, cur, \"Test Token\", \"TEST\", 6)\n\t}(cross(cur))\n\n\ttesting.SetRealm(testing.NewUserRealm(alice))\n\tbuyer := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n\tadminAddr := address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf6\")\n\n\t// Set up admin and mint tokens\n\ttesting.SetOriginCaller(adminAddr)\n\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\terr := ledger.Mint(buyer, 2000000) // Use ledger to mint\n\turequire.NoError(t, err)\n\n\t// Create event with GRC20 token payment\n\ttesting.SetOriginCaller(buyer)\n\ttesting.SetRealm(testing.NewUserRealm(buyer))\n\n\tvar eventId uint64\n\tcrossThrough(testing.NewCodeRealm(\"gno.land/r/jjoptimist/eventix\"), func() {\n\t\teventId = CreateEvent(\n\t\t\tcross(cur),\n\t\t\t\"GRC20 Event\",\n\t\t\t\"An event with GRC20 payment\",\n\t\t\t\"2024-12-31T23:59:59Z\",\n\t\t\ttoken,\n\t\t\t2,\n\t\t\t1000000,\n\t\t)\n\t})\n\n\t// Set up for approval\n\ttesting.SetOriginCaller(buyer)\n\ttesting.SetRealm(testing.NewUserRealm(buyer))\n\n\t// Approve using the ledger (following grc20factory_test pattern)\n\terr = ledger.Approve(buyer, pkgAddr, 1000000)\n\turequire.NoError(t, err)\n\n\t// Verify approval\n\tallowance := token.Allowance(buyer, pkgAddr)\n\turequire.Equal(t, int64(1000000), allowance, \"Approval should be set correctly\")\n\n\ttesting.SetOriginCaller(buyer)\n\ttesting.SetRealm(testing.NewUserRealm(buyer))\n\n\t// Buy ticket\n\tBuyTicket(cross(cur), eventId)\n\n\t// Verify purchase\n\tevent, exists := getEvent(eventId)\n\turequire.True(t, exists)\n\turequire.Equal(t, 1, event.ticketsSold)\n\n\t// Verify NFT ownership\n\ttokenId := grc721.TokenID(ufmt.Sprintf(\"event_%d_ticket_%d\", eventId, 1))\n\towner, err := tickets.OwnerOf(tokenId)\n\turequire.NoError(t, err)\n\turequire.Equal(t, buyer, owner)\n\n\t// Verify GRC20 balance changes\n\tbuyerBalance := token.BalanceOf(buyer)\n\turequire.Equal(t, int64(1000000), buyerBalance) // Should have 1M left after spending 1M\n}\n\nfunc crossThrough(rlm runtime.Realm, cr func()) {\n\ttesting.SetRealm(rlm)\n\tcr()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/jjoptimist/eventix\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"17RJ8pQg0WZJVN8zLXNPRoZLfv/IDOqAtfYuC3eBTqi6tKz61ySeF3gXkghstn3KcJSusc85Ug9P7XmHtfTsBg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/jjoptimist/home","files":[{"name":"config.gno","body":"package home\n\nimport \"gno.land/p/nt/ownable/v0\"\n\ntype Config struct {\n\tTitle       string\n\tDescription string\n\tGithub      string\n}\n\nvar config = Config{\n\tTitle:       \"JJOptimist's Home Realm 🏠\",\n\tDescription: \"Exploring Gno and building on-chain\",\n\tGithub:      \"jjoptimist\",\n}\n\nvar Ownable = ownable.NewWithAddress(address(\"g16vfw3r7zuz43fhky3xfsuc2hdv9tnhvlkyn0nj\"))\n\nfunc GetConfig() Config {\n\treturn config\n}\n\nfunc UpdateConfig(cur realm, newTitle, newDescription, newGithub string) {\n\tOwnable.AssertOwnedBy(cur.Previous().Address())\n\tconfig.Title = newTitle\n\tconfig.Description = newDescription\n\tconfig.Github = newGithub\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/jjoptimist/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"chain/runtime\"\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/r/leon/hor\"\n)\n\nconst (\n\tgnomeArt1 = `   /\\\n  /  \\\n ,,,,,\n(o.o)\n(\\_/)\n-\"-\"-`\n\n\tgnomeArt2 = `   /\\\n  /  \\\n ,,,,,\n(^.^)\n(\\_/)\n -\"-`\n\n\tgnomeArt3 = `   /\\\n  /  \\\n ,,,,,\n(*.*)\n(\\_/)\n\"-\"-\"`\n\n\tgnomeArt4 = `   /\\\n  /  \\\n ,,,,,\n(o.~)\n(\\_/)\n -\"-`\n)\n\nvar creation time.Time\n\nfunc getGnomeArt(height int64) string {\n\tvar art string\n\tswitch {\n\tcase height%7 == 0:\n\t\tart = gnomeArt4 // winking gnome\n\tcase height%5 == 0:\n\t\tart = gnomeArt3 // starry-eyed gnome\n\tcase height%3 == 0:\n\t\tart = gnomeArt2 // happy gnome\n\tdefault:\n\t\tart = gnomeArt1 // regular gnome\n\t}\n\treturn \"```\\n\" + art + \"\\n```\\n\"\n}\n\nfunc init(cur realm) {\n\tcreation = time.Now()\n\thor.Register(cross(cur), \"JJoptimist's Home Realm\", \"\")\n}\n\nfunc Render(path string) string {\n\theight := runtime.ChainHeight()\n\n\toutput := \"# \" + config.Title + \"\\n\\n\"\n\n\toutput += \"## About Me\\n\"\n\toutput += \"- 👋 Hi, I'm JJOptimist\\n\"\n\toutput += getGnomeArt(height)\n\toutput += \"- 🌱 \" + config.Description + \"\\n\"\n\n\toutput += \"## Contact\\n\"\n\toutput += \"- 📫 GitHub: [\" + config.Github + \"](https://github.com/\" + config.Github + \")\\n\"\n\n\toutput += \"\\n---\\n\"\n\toutput += \"_Realm created: \" + creation.Format(\"2006-01-02 15:04:05 UTC\") + \"_\\n\"\n\toutput += \"_Owner: \" + Ownable.Owner().String() + \"_\\n\"\n\toutput += \"_Current Block Height: \" + strconv.Itoa(int(height)) + \"_\"\n\n\treturn output\n}\n"},{"name":"home_test.gno","body":"package home\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestConfig(t *testing.T) {\n\tcfg := GetConfig()\n\n\tif cfg.Title != \"JJOptimist's Home Realm 🏠\" {\n\t\tt.Errorf(\"Expected title to be 'JJOptimist's Home Realm 🏠', got %s\", cfg.Title)\n\t}\n\tif cfg.Description != \"Exploring Gno and building on-chain\" {\n\t\tt.Errorf(\"Expected description to be 'Exploring Gno and building on-chain', got %s\", cfg.Description)\n\t}\n\tif cfg.Github != \"jjoptimist\" {\n\t\tt.Errorf(\"Expected github to be 'jjoptimist', got %s\", cfg.Github)\n\t}\n}\n\nfunc TestRender(t *testing.T) {\n\toutput := Render(\"\")\n\n\t// Test that required sections are present\n\tif !strings.Contains(output, \"# \"+config.Title) {\n\t\tt.Error(\"Rendered output missing title\")\n\t}\n\tif !strings.Contains(output, \"## About Me\") {\n\t\tt.Error(\"Rendered output missing About Me section\")\n\t}\n\tif !strings.Contains(output, \"## Contact\") {\n\t\tt.Error(\"Rendered output missing Contact section\")\n\t}\n\tif !strings.Contains(output, config.Description) {\n\t\tt.Error(\"Rendered output missing description\")\n\t}\n\tif !strings.Contains(output, config.Github) {\n\t\tt.Error(\"Rendered output missing github link\")\n\t}\n}\n\nfunc TestGetGnomeArt(t *testing.T) {\n\ttests := []struct {\n\t\theight   int64\n\t\texpected string\n\t}{\n\t\t{7, gnomeArt4}, // height divisible by 7\n\t\t{5, gnomeArt3}, // height divisible by 5\n\t\t{3, gnomeArt2}, // height divisible by 3\n\t\t{2, gnomeArt1}, // default case\n\t}\n\n\tfor _, tt := range tests {\n\t\tart := getGnomeArt(tt.height)\n\t\tif !strings.Contains(art, tt.expected) {\n\t\t\tt.Errorf(\"For height %d, expected art containing %s, got %s\", tt.height, tt.expected, art)\n\t\t}\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"qyRSTdNrRk3ht7YBdWq8mBqTxXAqp18IltDqtxSTYYB+7I1Ko/VaWcyK4MtN00RQ11/255VSAquzcJ1qZ+rhCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"derive","path":"gno.land/r/leon/derive","files":[{"name":"derive.gno","body":"package derive\n\nimport (\n\t\"chain\"\n\t\"chain/runtime\"\n\t\"net/url\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/leon/hor\"\n)\n\nvar cd = runtime.ChainDomain()\n\nfunc Render(path string) string {\n\tout := \"# Derive Realm Address\\n\\n\"\n\n\tout += `\u003cgno-form\u003e\n \t \u003cgno-input name=\"pkgpath\" placeholder=\"gno.land/{r,p}/...\" /\u003e\n\t\u003c/gno-form\u003e\n`\n\n\tif strings.Contains(path, \"pkgpath\") {\n\t\tparsed := parsePkgPath(path)\n\t\tif parsed == \"\" {\n\t\t\tout += \"Please input a valid pkgpath.\"\n\t\t\treturn out\n\t\t}\n\t\tout += ufmt.Sprintf(\"### [%s](%s) matches %s\", parsed, strings.TrimPrefix(parsed, \"gno.land\"), chain.PackageAddress(parsed))\n\t}\n\n\treturn out\n}\n\nfunc parsePkgPath(path string) string {\n\tu, err := url.Parse(path)\n\tif err != nil {\n\t\treturn \"\"\n\t}\n\n\tpkgpath := u.Query().Get(\"pkgpath\")\n\tif pkgpath == \"\" {\n\t\treturn \"\"\n\t}\n\n\tswitch {\n\tcase strings.HasPrefix(pkgpath, \"gno.land/r/\"),\n\t\tstrings.HasPrefix(pkgpath, \"gno.land/p/\"):\n\t\t// already absolute and valid\n\t\treturn pkgpath\n\n\tcase strings.HasPrefix(pkgpath, \"r/\"),\n\t\tstrings.HasPrefix(pkgpath, \"p/\"):\n\t\t// relative path, normalize\n\t\treturn \"gno.land/\" + pkgpath\n\n\tcase strings.HasPrefix(pkgpath, \"/r/\"),\n\t\tstrings.HasPrefix(pkgpath, \"/p/\"):\n\t\t// relative with leading slash\n\t\treturn \"gno.land\" + pkgpath\n\n\tdefault:\n\t\t// reject invalid\n\t\treturn \"\"\n\t}\n}\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"Derive\", \"Quickly derive a Gno address from a pkg path.\")\n}\n"},{"name":"derive_test.gno","body":"package derive\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestParsePkgPath(t *testing.T) {\n\tcases := []struct {\n\t\tname     string\n\t\tpath     string\n\t\texpected string\n\t}{\n\t\t// r/ namespace\n\t\t{\"full r path\", \"gno.land/r/leon/home\", \"gno.land/r/leon/home\"},\n\t\t{\"relative r path with slash\", \"/r/leon/home\", \"gno.land/r/leon/home\"},\n\t\t{\"relative r path no slash\", \"r/leon/home\", \"gno.land/r/leon/home\"},\n\n\t\t// p/ namespace\n\t\t{\"full p path\", \"gno.land/p/leon/home\", \"gno.land/p/leon/home\"},\n\t\t{\"relative p path with slash\", \"/p/leon/home\", \"gno.land/p/leon/home\"},\n\t\t{\"relative p path no slash\", \"p/leon/home\", \"gno.land/p/leon/home\"},\n\n\t\t// invalid cases\n\t\t{\"random text\", \"randomtext\", \"\"},\n\t\t{\"random text\", \"randomtext with space\", \"\"},\n\t\t{\"empty\", \"\", \"\"},\n\t\t{\"wrong namespace q/\", \"q/leon/home\", \"\"},\n\t\t{\"no pkgpath query param\", \"\", \"\"},\n\t\t{\"malformed url\", \"::::\", \"\"},\n\t}\n\n\tfor _, c := range cases {\n\t\tt.Run(c.name, func(t *testing.T) {\n\t\t\turl := \"gno.land/r/leon/derive?pkgpath=\" + c.path\n\t\t\tresult := parsePkgPath(url)\n\t\t\tif result != c.expected {\n\t\t\t\tt.Errorf(\"for input %q expected %q, got %q\", c.path, c.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestDerive(t *testing.T) {\n\tgot := Render(\"?pkgpath=gno.land/r/leon/home\")\n\tif !strings.Contains(got, \"g1n0z9kw63c6ze8fgle93s2e86hfm2qz025cgkey\") {\n\t\t// manually checked the above address\n\t\tt.Fatal(\"Failed derivation!\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/leon/derive\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"vMWLGRvbjVMITY8HVfnYfhJ5AFu7eB2frIQmGg4wBs8iRlo+nh+x85oaZyMIVhgKEjCCdHslP+xZFlvvVK5EAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5","package":{"name":"home","path":"gno.land/r/leon/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/leon/home\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g125em6arxsnj49vx35f0n0z34putv5ty3376fg5\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"chain/banker\"\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"strconv\"\n\n\t\"gno.land/p/demo/svg\"\n\t\"gno.land/p/leon/svgbtn\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/demo/art/gnoface\"\n\t\"gno.land/r/demo/art/millipede\"\n\t\"gno.land/r/demo/mirror\"\n\t\"gno.land/r/leon/config\"\n\t\"gno.land/r/leon/hor\"\n)\n\nvar (\n\tpfp        string // link to profile picture\n\tpfpCaption string // profile picture caption\n\tabtMe      [2]string\n)\n\nfunc Render(path string) string {\n\tout := \"# Leon's Homepage\\n\\n\"\n\n\tif path == \"buttons\" {\n\t\treturn renderButtonPage()\n\t}\n\n\tout += renderAboutMe()\n\tout += renderArt()\n\tout += config.Banner()\n\tout += \"\\n\\n\"\n\tout += svgbtn.Button(\n\t\t1200,\n\t\t50,\n\t\tgnomeBodyColors[int(runtime.ChainHeight()+1)%len(gnomeBodyColors)],\n\t\t\"#ffffff\",\n\t\t\"Support my work!\",\n\t\t\"/r/leon/home$help\u0026func=Donate\u0026.send=1000000ugnot\",\n\t)\n\n\treturn out\n}\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"Leon's Home Realm\", \"\")\n\tmirror.Register(cross(cur), cur.PkgPath(), Render)\n\n\tpfp = \"https://i.imgflip.com/91vskx.jpg\"\n\tpfpCaption = \"[My favourite painting \u0026 pfp](https://en.wikipedia.org/wiki/Wanderer_above_the_Sea_of_Fog)\"\n\tabtMe = [2]string{\n\t\t`### About me\nHi, I'm Leon, a DevRel Engineer at gno.land. I am a tech enthusiast, \nlife-long learner, and sharer of knowledge.`,\n\t\t`### Contributions\nMy contributions to Gno.land can mainly be found \n[on GitHub](https://github.com/gnolang/gno/issues?q=sort:updated-desc+author:leohhhn), and on [the chain](/u/leon).\n\nTODO import r/gh`,\n\t}\n}\n\nfunc UpdatePFP(cur realm, url, caption string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tpfp = url\n\tpfpCaption = caption\n}\n\nfunc UpdateAboutMe(cur realm, col1, col2 string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tabtMe[0] = col1\n\tabtMe[1] = col2\n}\n\nfunc Donate(cur realm) string {\n\tsent := unsafe.OriginSend()\n\tif len(sent) == 0 {\n\t\treturn \":c\"\n\t}\n\n\tbanker.NewBanker(banker.BankerTypeOriginSend, cur).SendCoins(\n\t\tcur.Address(),\n\t\tconfig.OwnableMain.Owner(),\n\t\tsent,\n\t) // wish this was prettier :)\n\n\treturn \"Thanks for donating \" + sent.String() + \" \u003c3\"\n}\n\nfunc renderAboutMe() string {\n\treturn md.Columns([]string{\n\t\tufmt.Sprintf(\"![my profile pic](%s)\\n\\n%s\\n\", pfp, pfpCaption),\n\t\tabtMe[0],\n\t\tabtMe[1],\n\t}, false)\n}\n\nfunc renderArt() string {\n\tout := \"# Gno Art\\n\"\n\n\tout += md.Columns([]string{\n\t\tgnoface.Render(strconv.Itoa(int(runtime.ChainHeight()))),\n\t\trenderMillipede(),\n\t\t\"SVG Gnome\\n\" + RenderSVGGnome(),\n\t}, false)\n\n\tout += \"This art is dynamic; it will change with every new block.\\n\\n\"\n\n\treturn out\n}\n\nfunc renderMillipede() string {\n\tout := \"Millipede\\n\\n\"\n\tout += \"```\\n\" + millipede.Draw(int(runtime.ChainHeight())%10+1) + \"```\\n\"\n\treturn out\n}\n\nfunc renderBlogPosts() string {\n\tout := \"\"\n\t// out += \"## Leon's Blog Posts\"\n\n\t// todo fetch blog posts authored by @leohhhn\n\t// and render them\n\treturn out\n}\n\nfunc RenderSVGGnome() string { // exported for your pleasure :)\n\tc := svg.NewCanvas(430, 430).WithViewBox(13, 25, 75, 75)\n\n\t// Body: blue triangle\n\tbody := svg.NewPolygon(\"50,50 30,100 70,100\", gnomeBodyColors[int(runtime.ChainHeight())%len(gnomeBodyColors)])\n\n\t// Head: peach circle (overlaps body)\n\thead := svg.NewCircle(50, 60, 10, \"#FAD7B6\")\n\n\t// Hat: red triangle on top of head\n\that := svg.NewPolygon(\"50,30 35,55 65,55\", \"#E53935\")\n\n\t// Eyes: two small black dots\n\tleftEye := svg.NewCircle(46, 59, 1, \"#000\")\n\trightEye := svg.NewCircle(54, 59, 1, \"#000\")\n\n\t// Beard: small white triangle under head\n\tbeard := svg.NewPolygon(\"50,85 42,63 58,63\", \"#FFF\")\n\n\t// Layering order matters (bottom to top)\n\tc.Append(body, head, beard, hat, leftEye, rightEye)\n\n\treturn c.Render(\"svg gnome\")\n}\n\nfunc renderButtonPage() string {\n\tout := \"# Buttons Demo\\n\\n\"\n\n\tout += md.ColumnsN([]string{\n\t\tsvgbtn.PrimaryButton(140, 45, \"Click Me\", \"/r/leon/home:click\") + \"\\n\\n\",\n\t\tsvgbtn.DangerButton(140, 45, \"Delete\", \"/delete\") + \"\\n\\n\",\n\t\tsvgbtn.SuccessButton(140, 45, \"Go Home\", \"/r/leon/home\") + \"\\n\\n\",\n\t\tsvgbtn.SmallButton(100, 45, \"Edit\", \"/edit\") + \"\\n\\n\",\n\t\tsvgbtn.WideButton(200, 40, \"Big Action\", \"/big\") + \"\\n\\n\",\n\t\tsvgbtn.TextButton(100, 30, \"More Info\", \"/r/leon/home:info\") + \"\\n\\n\",\n\t\tsvgbtn.IconButton(100, 40, \"Config\", \"/r/leon/config\") + \"\\n\\n\",\n\t}, 3, true)\n\n\treturn out\n}\n\nvar gnomeBodyColors = []string{\n\t\"#4CAF50\", // Green\n\t\"#2196F3\", // Blue\n\t\"#9C27B0\", // Purple\n\t\"#FF5722\", // Orange\n\t\"#795548\", // Brown\n\t\"#607D8B\", // Grayish Blue\n\t\"#E91E63\", // Pink\n\t\"#FFC107\", // Amber\n\t\"#00BCD4\", // Cyan\n\t\"#8BC34A\", // Light Green\n\t\"#FF9800\", // Deep Orange\n\t\"#3F51B5\", // Indigo\n\t\"#673AB7\", // Deep Purple\n\t\"#009688\", // Teal\n\t\"#F44336\", // Red\n\t\"#CDDC39\", // Lime\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Q2wEXd/vH7WRHqkch1Asg/69zszWwZyZJ5fLhcRF9CkgplVnEPLH6E8QIhlDuexe/b7bdoBT+b1THdainjWLCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"blog","path":"gno.land/r/lou/blog","files":[{"name":"actions.gno","body":"package blog\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/lou/blog\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/txlink\"\n)\n\nfunc CreatePost(cur realm, slug, title, body, publicationDate, authors, tags string) {\n\tauthorsField := strings.Split(authors, \" \")\n\ttagsField := strings.Split(tags, \" \")\n\n\tpost, err := blog.NewPost(\n\t\tslug,\n\t\ttitle,\n\t\tbody,\n\t\tpublicationDate,\n\t\tunsafe.OriginCaller().String(),\n\t\tauthorsField,\n\t\ttagsField,\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tactionLinks := md.Link(\"Like\", txlink.Call(\"LikePostBySlug\", \"slug\", post.Slug()))\n\tpost.SetPreviewFooter(actionLinks + \"\\n\\n\")\n\tif err := myBlog.AddPost(0, cur, post); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc UpdatePost(cur realm, id, slug, title, body, publicationDate, authors, tags string) {\n\tvar err error\n\tvar post *blog.Post\n\tauthorsField := strings.Split(authors, \" \")\n\ttagsField := strings.Split(tags, \" \")\n\n\t_, err = myBlog.GetPostById(id)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tpost, err = blog.NewPost(\n\t\tslug,\n\t\ttitle,\n\t\tbody,\n\t\tpublicationDate,\n\t\tunsafe.OriginCaller().String(),\n\t\tauthorsField,\n\t\ttagsField,\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif err := myBlog.UpdatePostById(0, cur, id, post); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc UpdatePostBySlug(cur realm, slug, title, body, publicationDate, authors, tags string) {\n\tvar err error\n\tvar post *blog.Post\n\tauthorsField := strings.Split(authors, \" \")\n\ttagsField := strings.Split(tags, \" \")\n\n\t_, err = myBlog.GetPostBySlug(slug)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tpost, err = blog.NewPost(\n\t\tslug,\n\t\ttitle,\n\t\tbody,\n\t\tpublicationDate,\n\t\tunsafe.OriginCaller().String(),\n\t\tauthorsField,\n\t\ttagsField,\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif err := myBlog.UpdatePostBySlug(0, cur, slug, post); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc DeletePost(cur realm, slug string) {\n\tif err := myBlog.DeletePostBySlug(0, cur, slug); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc ToggleLikePostBySlug(cur realm, slug string) {\n\tif err := myBlog.LikePostBySlug(0, cur, slug); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc AddCommentToPostBySlug(cur realm, slug, comment string) {\n\tvar err error\n\tpost, err := myBlog.GetPostBySlug(slug)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tcomm, err := blog.NewComment(cur.Previous().Address().String(), comment)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif err := post.AddComment(comm); err != nil {\n\t\tpanic(err)\n\t}\n}\n"},{"name":"blog.gno","body":"package blog\n\nimport (\n\t\"gno.land/p/lou/blog\"\n\n\t\"gno.land/r/leon/hor\"\n\t\"gno.land/r/sys/users\"\n)\n\nvar (\n\tmyBlog *blog.Blog\n\tadmin  = address(\"g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\")\n)\n\nfunc init(cur realm) {\n\tmyBlog, _ = blog.NewBlog(\n\t\t0, cur,\n\t\t\"Lou's Blog\",\n\t\tadmin,\n\t\tblog.WithUserResolver(myResolver),\n\t)\n\thor.Register(cross(cur), \"Lou's blog realm\", \"\")\n}\n\nfunc Render(path string) string {\n\tout := \"\"\n\tout += myBlog.Render(path)\n\treturn out\n}\n\nfunc myResolver(input string) (string, bool) {\n\tdata, ok := users.ResolveAny(input)\n\tif !ok || data == nil {\n\t\treturn \"\", false\n\t}\n\treturn data.Name(), true\n}\n"},{"name":"blog_test.gno","body":"package blog\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/lou/blog\"\n)\n\nfunc clearState(_ int, rlm realm, t *testing.T) {\n\tt.Helper()\n\tvar err error\n\n\tadmin = address(\"g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\")\n\tmyBlog, err = blog.NewBlog(\n\t\t0, rlm,\n\t\t\"Lou's Blog\",\n\t\tadmin,\n\t\tblog.WithUserResolver(myResolver),\n\t)\n\tif err != nil {\n\t\tt.Errorf(err.Error())\n\t}\n\t// inPause = false\n}\n\nfunc TestPackage(cur realm, t *testing.T) {\n\tclearState(0, cur, t)\n\n\ttesting.SetOriginCaller(admin)\n\ttesting.SetRealm(testing.NewUserRealm(admin))\n\n\t{\n\t\tprintln(\"Creating posts and rendering previews...\")\n\t\tCreatePost(cross(cur), \"slug1\", \"title1\", \"body1\", \"2022-05-20T13:17:22Z\", \"lou\", \"tag1 tag2\")\n\t\tCreatePost(cross(cur), \"slug2\", \"title2\", \"body2\", \"2022-05-20T13:17:23Z\", \"lou\", \"tag1 tag3\")\n\t\tgot := Render(\"\")\n\t\texpected := `# Lou's Blog\n\n[⊞ grid](/r/lou/blog:?mode=grid) | [≔ list](/r/lou/blog:?mode=list) | [⧖ relative](/r/lou/blog:?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:?order=asc\u0026sort=recent) | [↕ last updated](/r/lou/blog:?order=asc\u0026sort=update) | [past year](/r/lou/blog:?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\n\u003cgno-columns\u003e\n\u003cgno-columns\u003e\n## [title2](/r/lou/blog:posts/slug2)\n\n\n##### */slug2*\n\n\njust now\n\n**tags:** ` + \"`\" + `tag1` + \"`\" + `, ` + \"`\" + `tag3` + \"`\" + `\n\n**[comments \\(0](/r/lou/blog:posts/slug2#comments)**) | ❤️ (0)\n\n[Like](/r/lou/blog$help\u0026func=LikePostBySlug\u0026slug=slug2)\n\n\n\n---\n\u003cgno-columns-sep\u003e\n## [title1](/r/lou/blog:posts/slug1)\n\n\n##### */slug1*\n\n\njust now\n\n**tags:** ` + \"`\" + `tag1` + \"`\" + `, ` + \"`\" + `tag2` + \"`\" + `\n\n**[comments \\(0](/r/lou/blog:posts/slug1#comments)**) | ❤️ (0)\n\n[Like](/r/lou/blog$help\u0026func=LikePostBySlug\u0026slug=slug1)\n\n\n\n---\n\u003cgno-columns-sep\u003e\n\u003c/gno-columns\u003e`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t{\n\t\tprintln(\"Viewing one of the post...\")\n\t\tgot := Render(\"posts/slug2\")\n\t\texpected := `# title2\n\n\n*Author(s):* [lou](/r/lou/blog:authors/lou)\n\n\nbody2\n\n---\n**Created on:** May 20, 2022 at 1:17 PM\n\n**Published on:** February 13, 2009 at 11:31 PM\n\n**Publisher:** [g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/u/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)\n\n**Tags:** [\\#tag1](/r/lou/blog:tags/tag1), [\\#tag3](/r/lou/blog:tags/tag3)\n\n\n❤️ 0\n\n---\n### Comments (0)\n\n\nNo comments yet.`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t{\n\t\tprintln(\"Listing of tags...\")\n\t\tprintln(\"Listing\")\n\t\tgot := Render(\"authors\")\n\t\texpected := `## [/r/lou/blog](/r/lou/blog)/authors\n[↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:authors?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:authors?order=asc\u0026sort=recent) | [↕ most common](/r/lou/blog:authors?order=asc\u0026sort=common) | [past year](/r/lou/blog:authors?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:authors?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:authors?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\n### [lou](/r/lou/blog:authors/lou) (2)`\n\t\tassertMDEquals(t, got, expected)\n\n\t\tprintln(\"Invalid tag\")\n\t\tgot = Render(\"tags/invalid\")\n\t\texpected = `## [/r/lou/blog](/r/lou/blog)/[tags](/r/lou/blog:tags)/invalid\n[⊞ grid](/r/lou/blog:tags/invalid?mode=grid) | [≔ list](/r/lou/blog:tags/invalid?mode=list) | [⧖ relative](/r/lou/blog:tags/invalid?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:tags/invalid?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:tags/invalid?order=asc\u0026sort=recent) | [↕ last updated](/r/lou/blog:tags/invalid?order=asc\u0026sort=update) | [past year](/r/lou/blog:tags/invalid?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:tags/invalid?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:tags/invalid?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\nNo posts found for this tag.\u003cgno-columns\u003e`\n\t\tassertMDEquals(t, got, expected)\n\n\t\tprintln(\"Valid tag\")\n\t\tgot = Render(\"tags/tag2\")\n\t\texpected = `## [/r/lou/blog](/r/lou/blog)/[tags](/r/lou/blog:tags)/tag2\n[⊞ grid](/r/lou/blog:tags/tag2?mode=grid) | [≔ list](/r/lou/blog:tags/tag2?mode=list) | [⧖ relative](/r/lou/blog:tags/tag2?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:tags/tag2?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:tags/tag2?order=asc\u0026sort=recent) | [↕ last updated](/r/lou/blog:tags/tag2?order=asc\u0026sort=update) | [past year](/r/lou/blog:tags/tag2?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:tags/tag2?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:tags/tag2?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\n\u003cgno-columns\u003e\n\u003cgno-columns\u003e\n## [title1](/r/lou/blog:posts/slug1)\n\n\n##### */slug1*\n\n\njust now\n\n**author(s):** lou\n\n**tags:** ` + \"`tag1`, `tag2`\" + `\n\n**[comments \\(0](/r/lou/blog:posts/slug1#comments)**) | ❤️ (0)\n\n[Like](/r/lou/blog$help\u0026func=LikePostBySlug\u0026slug=slug1)\n\n\n\n---\n\n\u003c/gno-columns\u003e`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t{\n\t\tprintln(\"Listing of authors...\")\n\t\tprintln(\"Listing\")\n\t\tgot := Render(\"authors\")\n\t\texpected := `## [/r/lou/blog](/r/lou/blog)/authors\n[↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:authors?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:authors?order=asc\u0026sort=recent) | [↕ most common](/r/lou/blog:authors?order=asc\u0026sort=common) | [past year](/r/lou/blog:authors?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:authors?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:authors?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\n### [lou](/r/lou/blog:authors/lou) (2)`\n\t\tassertMDEquals(t, got, expected)\n\n\t\tprintln(\"Invalid author\")\n\t\tgot = Render(\"authors/invalid\")\n\t\texpected = `## [/r/lou/blog](/r/lou/blog)/[authors](/r/lou/blog:authors)/invalid\n#### [@invalid](/u/invalid)'s profile\n[⊞ grid](/r/lou/blog:authors/invalid?mode=grid) | [≔ list](/r/lou/blog:authors/invalid?mode=list) | [⧖ relative](/r/lou/blog:authors/invalid?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:authors/invalid?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:authors/invalid?order=asc\u0026sort=recent) | [↕ last updated](/r/lou/blog:authors/invalid?order=asc\u0026sort=update) | [past year](/r/lou/blog:authors/invalid?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:authors/invalid?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:authors/invalid?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\nNo posts found for this author.\u003cgno-columns\u003e`\n\t\tassertMDEquals(t, got, expected)\n\n\t\tprintln(\"Valid author\")\n\t\tgot = Render(\"authors/lou\")\n\t\texpected = `## [/r/lou/blog](/r/lou/blog)/[authors](/r/lou/blog:authors)/lou\n#### [@lou](/u/lou)'s profile\n[⊞ grid](/r/lou/blog:authors/lou?mode=grid) | [≔ list](/r/lou/blog:authors/lou?mode=list) | [⧖ relative](/r/lou/blog:authors/lou?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:authors/lou?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:authors/lou?order=asc\u0026sort=recent) | [↕ last updated](/r/lou/blog:authors/lou?order=asc\u0026sort=update) | [past year](/r/lou/blog:authors/lou?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:authors/lou?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:authors/lou?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\n\u003cgno-columns\u003e\n\u003cgno-columns\u003e\n## [title2](/r/lou/blog:posts/slug2)\n\n\n##### */slug2*\n\n\njust now\n\n**author(s):** lou\n\n**tags:** ` + \"`tag1`, `tag3`\" + `\n\n**[comments \\(0](/r/lou/blog:posts/slug2#comments)**) | ❤️ (0)\n\n[Like](/r/lou/blog$help\u0026func=LikePostBySlug\u0026slug=slug2)\n\n\n\n---\n\n## [title1](/r/lou/blog:posts/slug1)\n\n\n##### */slug1*\n\n\njust now\n\n**author(s):** lou\n\n**tags:** ` + \"`tag1`, `tag2`\" + `\n\n**[comments \\(0](/r/lou/blog:posts/slug1#comments)**) | ❤️ (0)\n\n[Like](/r/lou/blog$help\u0026func=LikePostBySlug\u0026slug=slug1)\n\n\n\n---\n\n\u003c/gno-columns\u003e`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t{\n\t\tprintln(\"Adding comments on a post...\")\n\t\tAddCommentToPostBySlug(cross(cur), \"slug1\", \"comment1\")\n\t\tAddCommentToPostBySlug(cross(cur), \"slug2\", \"comment2\")\n\t\tAddCommentToPostBySlug(cross(cur), \"slug1\", \"comment3\")\n\t\tAddCommentToPostBySlug(cross(cur), \"slug2\", \"comment4\")\n\t\tAddCommentToPostBySlug(cross(cur), \"slug1\", \"comment5\")\n\t\tgot := Render(\"posts/slug2\")\n\t\texpected := `# title2\n\n\n[2 Comment\\(s\\)](#comments)\n\n*Author(s):* [lou](/r/lou/blog:authors/lou)\n\n\nbody2\n\n---\n**Created on:** May 20, 2022 at 1:17 PM\n\n**Published on:** February 13, 2009 at 11:31 PM\n\n**Publisher:** [g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/u/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)\n\n**Tags:** [\\#tag1](/r/lou/blog:tags/tag1), [\\#tag3](/r/lou/blog:tags/tag3)\n\n\n❤️ 0\n\n---\n### Comments (2)\n\n\n**[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)** *just now*\n\ncomment4\n❤️ 0 \n\n\n\n**[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)** *just now*\n\ncomment2\n❤️ 0`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t{\n\t\tprintln(\"Listing of commenters...\")\n\t\tprintln(\"Listing invalid\")\n\t\tgot := Render(\"commenters\")\n\t\texpected := `Commenter slug is required.`\n\t\tassertMDEquals(t, got, expected)\n\n\t\tprintln(\"Invalid commenter\")\n\t\tgot = Render(\"commenters/invalid\")\n\t\texpected = `## [/r/lou/blog](/r/lou/blog)/commenters/invalid\n#### [@invalid](/u/invalid)'s profile\n[⊞ grid](/r/lou/blog:commenters/invalid?mode=grid) | [≔ list](/r/lou/blog:commenters/invalid?mode=list) | [⧖ relative](/r/lou/blog:commenters/invalid?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:commenters/invalid?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:commenters/invalid?order=asc\u0026sort=recent) | [↕ last updated](/r/lou/blog:commenters/invalid?order=asc\u0026sort=update) | [past year](/r/lou/blog:commenters/invalid?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:commenters/invalid?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:commenters/invalid?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\nNo posts found for this commenter.\u003cgno-columns\u003e`\n\t\tassertMDEquals(t, got, expected)\n\n\t\tprintln(\"Valid commenter\")\n\t\tgot = Render(\"commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\")\n\t\texpected = `## [/r/lou/blog](/r/lou/blog)/commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\n#### [@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/u/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)'s profile\n[⊞ grid](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?mode=grid) | [≔ list](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?mode=list) | [⧖ relative](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?time=short) | [↕ alphabetical \\(A\\-Z\\)](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?order=asc\u0026sort=alpha) | [↕ recent](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?order=asc\u0026sort=recent) | [↕ last updated](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?order=asc\u0026sort=update) | [past year](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?end=2009-02-13\u0026start=2008-02-13), [this year](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?end=2009-02-13\u0026start=2009-01-01), [last 30 days](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs?end=2009-02-13\u0026start=2009-01-14) | [⟳ reset](/r/lou/blog)\n\n\u003cgno-columns\u003e\n\u003cgno-columns\u003e\n#### **[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)**\n\n\ncomment1\n\n*just now*\n\nin [title1](/r/lou/blog:posts/slug1)\n\n---\n#### **[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)**\n\n\ncomment3\n\n*just now*\n\nin [title1](/r/lou/blog:posts/slug1)\n\n---\n#### **[@g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs](/r/lou/blog:commenters/g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs)**\n\n\ncomment5\n\n*just now*\n\nin [title1](/r/lou/blog:posts/slug1)\n\n---\n\u003c/gno-columns\u003e\n**1** | [2](?page=2)`\n\t\tassertMDEquals(t, got, expected)\n\t}\n\n\t{\n\t\tprintln(\"Invalid paths...\")\n\t\tnotFoundPaths := []string{\n\t\t\t\"p/slug3\",\n\t\t\t\"p\",\n\t\t\t\"p/\",\n\t\t\t\"x/x\",\n\t\t\t\"t\",\n\t\t\t\"t/\",\n\t\t\t\"/\",\n\t\t\t\"p/slug1/\",\n\t\t}\n\t\tfor _, notFoundPath := range notFoundPaths {\n\t\t\tgot := Render(notFoundPath)\n\t\t\texpected := \"404\"\n\t\t\tif got != expected {\n\t\t\t\tt.Errorf(\"path %q: expected %q, got %q.\", notFoundPath, expected, got)\n\t\t\t} else {\n\t\t\t\tprintln(\"PASSED\")\n\t\t\t}\n\t\t}\n\t}\n\n\t// {\n\t// \tprintln(\"Updating a post...\")\n\t// \tnewTitle := \"title3\"\n\t// \tnewDate := \"2022-05-20T13:17:23Z\"\n\n\t// \tCreatePost(cross, \"slug1\", \"title1\", \"body1\", \"2022-05-20T13:17:22Z\", \"lou\", \"tag1 tag2\")\n\t// \tUpdatePostBySlug(cur, \"slug2\", newTitle, \"body2++\", newDate, \"lou\", \"tag1 tag4\")\n\t// \tgot := Render(\"posts/slug2\")\n\t// \texpected := ``\n\t// \tassertMDEquals(t, got, expected)\n\n\t// \thome := Render(\"\")\n\n\t// \tif strings.Count(home, newTitle) != 1 {\n\t// \t\tt.Errorf(\"post not edited properly\")\n\t// \t}\n\t// }\n\n\t// {\n\t// \t// fix\n\t// \tprintln(\"Deleting a post...\")\n\t// \ttitle := \"example title\"\n\t// \tslug := \"testSlug1\"\n\t// \tCreatePost(cross, slug, title, \"body1\", \"2022-05-25T13:17:22Z\", \"lou\", \"tag1,tag2\")\n\n\t// \tgot := Render(\"\")\n\n\t// \tif !strings.Contains(got, title) {\n\t// \t\tt.Errorf(\"post was not added properly\")\n\t// \t}\n\n\t// \tpostRender := Render(\"p/\" + slug)\n\n\t// \tif !strings.Contains(postRender, title) {\n\t// \t\tt.Errorf(\"post not rendered properly\")\n\t// \t}\n\n\t// \tDeletePost(cur, slug)\n\t// \tgot = Render(\"\")\n\n\t// \tif strings.Contains(got, title) {\n\t// \t\tt.Errorf(\"post was not removed\")\n\t// \t}\n\n\t// \tpostRender = Render(\"p/\" + slug)\n\n\t// \tassertMDEquals(t, postRender, \"404\")\n\t// }\n\t//\n\t//\t// TODO: ?mode=...\n\t//\n}\n\nfunc assertMDEquals(t *testing.T, got, expected string) {\n\tt.Helper()\n\texpected = strings.TrimSpace(expected)\n\tgot = strings.TrimSpace(got)\n\tif expected != got {\n\t\tt.Errorf(\"invalid render output.\\nexpected %q.\\ngot      %q.\", expected, got)\n\t} else {\n\t\tprintln(\"PASSED\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/lou/blog\"\ngno = \"0.9\"\n"},{"name":"sample.gno","body":"package blog\n\nimport (\n\t\"gno.land/p/lou/blog\"\n)\n\nfunc CreateSampleBlog(_ int, rlm realm) *blog.Blog {\n\tpost1, err := blog.NewPost(\n\t\t\"ai-future\",\n\t\t\"The Future of AI\",\n\t\t\"Artificial Intelligence is transforming every industry, from healthcare to entertainment.\",\n\t\t\"2024-03-01T10:00:00Z\",\n\t\t\"g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\",\n\t\t[]string{\"g1abcde12345\"},\n\t\t[]string{\"ai\", \"technology\", \"future\"},\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tcomm, err := blog.NewComment(\n\t\t\"g1abcde12345\",\n\t\t\"Exciting times ahead!\",\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tpost1.AddComment(comm)\n\n\tpost2, err := blog.NewPost(\n\t\t\"blockchain-real-world\",\n\t\t\"Blockchain in the Real World\",\n\t\t\"Beyond cryptocurrencies, blockchain is finding uses in supply chains, voting systems, and more.\",\n\t\t\"2023-12-15T08:30:00Z\",\n\t\t\"g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\",\n\t\t[]string{\"g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\"},\n\t\t[]string{\"blockchain\", \"tech\", \"supplychain\"},\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treply, err := blog.NewComment(\n\t\t\"g1ffzxha57dh0qgv9ma5v393ur0zexfvp6lsjpae\",\n\t\t\"heyyy\",\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tcomm2, err := blog.NewComment(\n\t\t\"g14x2crt492f84shxa9s7e5ulk7lnf2p3euz7r9n\",\n\t\t\"This tech still needs better UX.\",\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tcomm3, err := blog.NewComment(\n\t\t\"g1fjnxhv3v5x02j49fyl49epmhjyzscm8e4au94t\",\n\t\t\"Absolutely, usability is key for adoption.\",\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tcomm4, err := blog.NewComment(\n\t\t\"g14x2crt492f84shxa9s7e5ulk7lnf2p3euz7r9n\",\n\t\t\"... still needs better UX.\",\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tpost2.AddComment(comm2)\n\tpost2.AddComment(comm3)\n\tpost2.AddReply(comm2.ID(), reply)\n\tpost2.AddReply(reply.ID(), comm4)\n\n\tpost3, err := blog.NewPost(\n\t\t\"getting-things-done\",\n\t\t\"Getting Things Done in 2025\",\n\t\t\"With distractions everywhere, productivity tools are more essential than ever.\",\n\t\t\"2025-01-10T09:15:00Z\",\n\t\t\"g1pfyhn0d7g4tnp6wft9ge4cuu88ppr9u8mdggfs\",\n\t\t[]string{\"g1produser1\"},\n\t\t[]string{\"productivity\", \"lifehacks\", \"focus\", \"ai\"},\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tmyBlog.AddPost(0, rlm, post1)\n\tmyBlog.AddPost(0, rlm, post2)\n\tmyBlog.AddPost(0, rlm, post3)\n\treturn myBlog\n}\n\nfunc Test(cur realm) {\n\tCreateSampleBlog(0, cur)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"PRFddJoa+Z8vyPnmzD+fB1oVuuksrJxYLVYhYB+R3rh+qShc82eIV8A5mEQGLhVZOTuc/9FX0R+siGOgPIs2Dw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/manfred/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/manfred/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nfunc Render(path string) string {\n\treturn \"Moved to r/moul\"\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"jahCrM4qta1sLerYsnycbVzHz0n7UkpG9JV6kER84cTgjlfRwTOdfJkS7hPvlAnyyRwANpRJATgVA7UCVDToBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/mason/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/mason/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"gno.land/p/mason/md\"\n)\n\nconst (\n\tgnomeArt1 = `   /\\\n                  /  \\\n                  ,,,,,\n                  (o.o)\n                  (\\_/)\n                  -\"-\"-`\n\tdgnonut = `\n                        #$$$$$$$$*                         \n                     #$$@@@@@@@@@@$$$#                      \n                   #$$@@@@@@@@@@@@@$$$$#                    \n                 #$$$@@@@$$$$$$$$$$$$$$$$*                  \n                #$$$$$$$$$$$$$$$$$$$$$$$$$#!                \n               #$$$$$$$$$############$$$$$##*               \n             !##$$$$$$####**********#####$###*              \n            =##$$$$$###****!!!!!!!!!***######*!             \n            *##$$$###***!!!!!!==!!!!!!**######*=            \n           !*#######***!!!=;;;;;====!!!!**####**            \n          !*#######**!!!==;;::::::;;==!!!**###**!           \n          !*######***!==;::~~~~~~:::;;=!!!***#***=          \n         =**#####**!!==;::~-,,,,,--~:;;=!!!******!          \n         !**####***!==;:~-,..  ..,,-~:;==!!******!;         \n        ;!**###***!!=;:~-,.       ..-~:;==!!*****!=         \n        =!*******!!==::-.          .,-::==!!*****!=         \n        =!*******!!=;:~,            .-~:;=!!!****!=:        \n       ~=!*******!==;:-.            .,-:;=!!!****!=;        \n       :=!*******!==;~,.             ,-:;==!!!***!=;        \n       :=!******!!==:~,              ,-:;=!!!***!!=;        \n       :=!!*****!!=;:~,              ,~:;=!!****!!=;-       \n       :=!!!****!!==;~,              -~;==!!****!!=;-       \n       :;=!!*****!!=;:-              -:;=!!*****!!=:-       \n       ~;=!!!****!!==;~              :;=!!*****!!!;:-       \n       ~;==!!****!!!==:              ;=!!******!!=;:,       \n       ~:==!!!****!!!=;~            :=!********!!=;:.       \n       -:;==!!*****!!!!;            =!*********!==;:        \n       ,~;==!!*******!!==          =**#####****!==:~        \n       ,~:;=!!!!*********!        **#######***!!=;~-        \n        -~;;=!!!!**********!    *##$$$$$###***!!=:~.        \n        ,~:;==!!!****##########$$$$$$$$###****!=;:~         \n         -~:;==!!!***####$$$$$$@@@@@$$$###**!!=;:~,         \n         ,-~:;=!!!***####$$$$@@@@@@$$$$##**!!!=;:-.         \n          -~:;;=!!!***###$$$$$@@@@$$$$##***!!=;:-.          \n          .-~:;;=!!!***###$$$$$$$$$$$##***!==;:~-           \n           .-~:;==!!!!**####$$$$$$$###**!!==;:~-            \n            ,-~::;==!!!!***########****!!==;:~-.            \n             ,-~:;;==!!!!!***********!!!==;:~,.             \n              ,,~~::;====!!!!!!!!!!!!!==;::~,.              \n               .,-~::;;;===!!!!!!!!===;::~-,.               \n                 ,--~~:;;;;========;;::~--.                 \n                  .,,-~~:::::::::::~~~-,,.                  \n                    ..,---~~~~~~~~~--,..                    \n                       ..,,,,,,,,,...                       \n                             ...`\n)\n\nfunc Render(path string) string {\n\thome := md.New()\n\thome.H1(\"Mason's Realm\")\n\n\thome.Im(\"https://cdn.esawebb.org/archives/images/screen/weic2428a.jpg\", \"Placeholder\")\n\thome.P(\"Welcome to my realm. \" + md.Link(\"github\", \"https://github.com/masonmcbride\"))\n\n\thome.H3(\"Dgnonut\")\n\thome.Code(dgnonut)\n\n\thome.H3(\"More\")\n\thome.Code(gnomeArt1)\n\thome.Bullet(\"Credit to \" + md.Link(\"JJOptimist\", \"https://gno.land/r/jjoptimist/home\") + \" for this gnome art.\")\n\thome.Bullet(\"I'm testing out my markdown system.\")\n\treturn home.Render()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"b5Gy0+PWBsDzz5+bV6N1ZRc50hDK7kcGk8EnGfVjJB2Z1M7mFp0SNFeLfV1A1plnv9iRO62NLeTvezW0pGXrDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/matijamarjanovic/home","files":[{"name":"config.gno","body":"package home\n\nimport (\n\t\"gno.land/p/moul/authz\"\n)\n\nvar (\n\tmainAddr   = address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")\n\tbackupAddr = address(\"g1kjsl2ungmc95mgluq96w8dqlep8d4n8cxdfthk\")\n\tauth       = authz.NewMemberAuthority(mainAddr, backupAddr)\n\tAuth       = authz.NewWithAuthority(auth)\n)\n\nfunc Authorize(cur realm, addr string) {\n\tauth.AddMember(0, cur, address(addr))\n}\n\nfunc Address() address {\n\treturn mainAddr\n}\n\nfunc Backup() address {\n\treturn backupAddr\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/matijamarjanovic/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"chain/banker\"\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/leon/hor\"\n)\n\nvar (\n\tpfp        string\n\tpfpCaption string\n\tabtMe      string\n\n\tmodernVotes  int64\n\tclassicVotes int64\n\tminimalVotes int64\n\tcurrentTheme string\n\n\tmodernLink  string\n\tclassicLink string\n\tminimalLink string\n)\n\nfunc init(cur realm) {\n\tpfp = \"https://avatars.githubusercontent.com/u/93043005?s=60\u0026v=4\"\n\tpfpCaption = \"My github profile picture\"\n\tabtMe = `Blockchain \u0026 Full Stack Developer and a Computer Science Student who enjoys hackathons and building cool stuff. My current passion is DeFi. Outside of work, I enjoy travelling, weightlifting and combat sports.`\n\n\tmodernVotes = 0\n\tclassicVotes = 0\n\tminimalVotes = 0\n\tcurrentTheme = \"classic\"\n\tmodernLink = txlink.NewLink(\"VoteModern\").URL()\n\tclassicLink = txlink.NewLink(\"VoteClassic\").URL()\n\tminimalLink = txlink.NewLink(\"VoteMinimal\").URL()\n\thor.Register(cross(cur), \"Matija Marijanovic's Home Realm\", \"\")\n}\n\nfunc UpdatePFP(cur realm, url, caption string) {\n\tif err := Auth.DoByPrevious(0, cur, \"update_pfp\", func() error {\n\t\tpfp = url\n\t\tpfpCaption = caption\n\t\treturn nil\n\t}); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc UpdateAboutMe(cur realm, col1 string) {\n\tif err := Auth.DoByPrevious(0, cur, \"update_about_me\", func() error {\n\t\tabtMe = col1\n\t\treturn nil\n\t}); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc VoteModern(cur realm) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\tugnotAmount := unsafe.OriginSend().AmountOf(\"ugnot\")\n\tvotes := ugnotAmount\n\tmodernVotes += votes\n\tupdateCurrentTheme()\n}\n\nfunc VoteClassic(cur realm) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\tugnotAmount := unsafe.OriginSend().AmountOf(\"ugnot\")\n\tvotes := ugnotAmount\n\tclassicVotes += votes\n\tupdateCurrentTheme()\n}\n\nfunc VoteMinimal(cur realm) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\tugnotAmount := unsafe.OriginSend().AmountOf(\"ugnot\")\n\tvotes := ugnotAmount\n\tminimalVotes += votes\n\tupdateCurrentTheme()\n}\n\nfunc CollectBalance(cur realm) {\n\tif err := Auth.DoByPrevious(0, cur, \"collect_balance\", func() error {\n\t\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\t\townerAddr := Address()\n\t\trealmAddr := cur.Address()\n\t\tbanker_.SendCoins(realmAddr, ownerAddr, banker_.GetCoins(realmAddr))\n\t\treturn nil\n\t}); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc Render(path string) string {\n\tvar sb strings.Builder\n\n\tswitch currentTheme {\n\tcase \"modern\":\n\t\t// Modern theme - Clean and minimalist with emojis\n\t\tsb.WriteString(md.H1(\"🚀 Matija's Space\"))\n\t\tsb.WriteString(md.Image(pfpCaption, pfp))\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(md.Italic(pfpCaption))\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(md.HorizontalRule())\n\t\tsb.WriteString(abtMe)\n\t\tsb.WriteString(\"\\n\")\n\n\tcase \"minimal\":\n\t\t// Minimal theme - No emojis, minimal formatting\n\t\tsb.WriteString(md.H1(\"Matija Marjanovic\"))\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(abtMe)\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(md.Image(pfpCaption, pfp))\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(pfpCaption)\n\t\tsb.WriteString(\"\\n\")\n\n\tdefault:\n\t\t// Classic theme - Traditional blog style with decorative elements\n\t\tsb.WriteString(md.H1(\"✨ Welcome to Matija's Homepage ✨\"))\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(md.Image(pfpCaption, pfp))\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(pfpCaption)\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(md.HorizontalRule())\n\t\tsb.WriteString(md.H2(\"About me\"))\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(abtMe)\n\t\tsb.WriteString(\"\\n\")\n\t}\n\n\tswitch currentTheme {\n\tcase \"modern\":\n\t\tsb.WriteString(md.HorizontalRule())\n\t\tsb.WriteString(md.H2(\"🎨 Theme Selector\"))\n\t\tsb.WriteString(\"Choose your preferred viewing experience:\\n\")\n\t\titems := []string{\n\t\t\tmd.Link(ufmt.Sprintf(\"Modern Design (%d votes)\", modernVotes), modernLink),\n\t\t\tmd.Link(ufmt.Sprintf(\"Classic Style (%d votes)\", classicVotes), classicLink),\n\t\t\tmd.Link(ufmt.Sprintf(\"Minimal Look (%d votes)\", minimalVotes), minimalLink),\n\t\t}\n\t\tsb.WriteString(md.BulletList(items))\n\n\tcase \"minimal\":\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(md.H3(\"Theme Selection\"))\n\t\tsb.WriteString(ufmt.Sprintf(\"Current theme: %s\\n\", currentTheme))\n\t\tsb.WriteString(ufmt.Sprintf(\"Votes - Modern: %d | Classic: %d | Minimal: %d\\n\",\n\t\t\tmodernVotes, classicVotes, minimalVotes))\n\t\tsb.WriteString(md.Link(\"Modern\", modernLink))\n\t\tsb.WriteString(\" | \")\n\t\tsb.WriteString(md.Link(\"Classic\", classicLink))\n\t\tsb.WriteString(\" | \")\n\t\tsb.WriteString(md.Link(\"Minimal\", minimalLink))\n\t\tsb.WriteString(\"\\n\")\n\n\tdefault:\n\t\tsb.WriteString(md.HorizontalRule())\n\t\tsb.WriteString(md.H2(\"✨ Theme Customization ✨\"))\n\t\tsb.WriteString(md.Bold(\"Choose Your Preferred Theme:\"))\n\t\tsb.WriteString(\"\\n\\n\")\n\t\titems := []string{\n\t\t\tufmt.Sprintf(\"Modern 🚀 (%d votes) - %s\", modernVotes, md.Link(\"Vote\", modernLink)),\n\t\t\tufmt.Sprintf(\"Classic ✨ (%d votes) - %s\", classicVotes, md.Link(\"Vote\", classicLink)),\n\t\t\tufmt.Sprintf(\"Minimal ⚡ (%d votes) - %s\", minimalVotes, md.Link(\"Vote\", minimalLink)),\n\t\t}\n\t\tsb.WriteString(md.BulletList(items))\n\t}\n\n\t// Theme-specific footer/links section\n\tswitch currentTheme {\n\tcase \"modern\":\n\t\tsb.WriteString(md.HorizontalRule())\n\t\tsb.WriteString(md.Link(\"GitHub\", \"https://github.com/matijamarjanovic\"))\n\t\tsb.WriteString(\" | \")\n\t\tsb.WriteString(md.Link(\"LinkedIn\", \"https://www.linkedin.com/in/matijamarjanovic\"))\n\t\tsb.WriteString(\"\\n\")\n\n\tcase \"minimal\":\n\t\tsb.WriteString(\"\\n\")\n\t\tsb.WriteString(md.Link(\"GitHub\", \"https://github.com/matijamarjanovic\"))\n\t\tsb.WriteString(\" | \")\n\t\tsb.WriteString(md.Link(\"LinkedIn\", \"https://www.linkedin.com/in/matijamarjanovic\"))\n\t\tsb.WriteString(\"\\n\")\n\n\tdefault:\n\t\tsb.WriteString(md.HorizontalRule())\n\t\tsb.WriteString(md.H3(\"✨ Connect With Me\"))\n\t\titems := []string{\n\t\t\tmd.Link(\"🌟 GitHub\", \"https://github.com/matijamarjanovic\"),\n\t\t\tmd.Link(\"💼 LinkedIn\", \"https://www.linkedin.com/in/matijamarjanovic\"),\n\t\t}\n\t\tsb.WriteString(md.BulletList(items))\n\t}\n\n\treturn sb.String()\n}\n\nfunc maxOfThree(a, b, c int64) int64 {\n\tmax := a\n\tif b \u003e max {\n\t\tmax = b\n\t}\n\tif c \u003e max {\n\t\tmax = c\n\t}\n\treturn max\n}\n\nfunc updateCurrentTheme() {\n\tmaxVotes := maxOfThree(modernVotes, classicVotes, minimalVotes)\n\n\tif maxVotes == modernVotes {\n\t\tcurrentTheme = \"modern\"\n\t} else if maxVotes == classicVotes {\n\t\tcurrentTheme = \"classic\"\n\t} else {\n\t\tcurrentTheme = \"minimal\"\n\t}\n}\n"},{"name":"home_test.gno","body":"package home\n\nimport (\n\t\"chain\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nfunc TestUpdatePFP(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tpfp = \"\"\n\tpfpCaption = \"\"\n\n\tUpdatePFP(cross(cur), \"https://example.com/pic.png\", \"New Caption\")\n\n\turequire.Equal(t, pfp, \"https://example.com/pic.png\", \"Profile picture URL should be updated\")\n\turequire.Equal(t, pfpCaption, \"New Caption\", \"Profile picture caption should be updated\")\n}\n\nfunc TestUpdateAboutMe(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tabtMe = \"\"\n\n\tUpdateAboutMe(cross(cur), \"This is my new bio.\")\n\n\turequire.Equal(t, abtMe, \"This is my new bio.\", \"About Me should be updated\")\n}\n\nfunc TestVoteModern(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tmodernVotes, classicVotes, minimalVotes = 0, 0, 0\n\n\tcoinsSent := chain.NewCoins(chain.NewCoin(\"ugnot\", 75000000))\n\tcoinsSpent := chain.NewCoins(chain.NewCoin(\"ugnot\", 1))\n\n\ttesting.SetOriginSend(coinsSent)\n\ttesting.SetOriginSpend(coinsSpent)\n\tVoteModern(cross(cur))\n\n\tuassert.Equal(t, int64(75000000), modernVotes, \"Modern votes should be calculated correctly\")\n\tuassert.Equal(t, \"modern\", currentTheme, \"Theme should be updated to modern\")\n}\n\nfunc TestVoteClassic(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tmodernVotes, classicVotes, minimalVotes = 0, 0, 0\n\n\tcoinsSent := chain.NewCoins(chain.NewCoin(\"ugnot\", 75000000))\n\tcoinsSpent := chain.NewCoins(chain.NewCoin(\"ugnot\", 1))\n\n\ttesting.SetOriginSend(coinsSent)\n\ttesting.SetOriginSpend(coinsSpent)\n\tVoteClassic(cross(cur))\n\n\tuassert.Equal(t, int64(75000000), classicVotes, \"Classic votes should be calculated correctly\")\n\tuassert.Equal(t, \"classic\", currentTheme, \"Theme should be updated to classic\")\n}\n\nfunc TestVoteMinimal(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tmodernVotes, classicVotes, minimalVotes = 0, 0, 0\n\n\tcoinsSent := chain.NewCoins(chain.NewCoin(\"ugnot\", 75000000))\n\tcoinsSpent := chain.NewCoins(chain.NewCoin(\"ugnot\", 1))\n\n\ttesting.SetOriginSend(coinsSent)\n\ttesting.SetOriginSpend(coinsSpent)\n\tVoteMinimal(cross(cur))\n\n\tuassert.Equal(t, int64(75000000), minimalVotes, \"Minimal votes should be calculated correctly\")\n\tuassert.Equal(t, \"minimal\", currentTheme, \"Theme should be updated to minimal\")\n}\n\nfunc TestRender(t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tmodernVotes, classicVotes, minimalVotes = 0, 0, 0\n\tcurrentTheme = \"classic\"\n\tpfp = \"https://example.com/pic.png\"\n\tpfpCaption = \"Test Caption\"\n\tabtMe = \"Test About Me\"\n\n\tout := Render(\"\")\n\turequire.NotEqual(t, out, \"\", \"Render output should not be empty\")\n\n\tuassert.True(t, strings.Contains(out, \"✨ Welcome to Matija's Homepage ✨\"), \"Classic theme should have correct header\")\n\tuassert.True(t, strings.Contains(out, pfp), \"Should contain profile picture URL\")\n\tuassert.True(t, strings.Contains(out, pfpCaption), \"Should contain profile picture caption\")\n\tuassert.True(t, strings.Contains(out, \"About me\"), \"Should contain About me section\")\n\tuassert.True(t, strings.Contains(out, abtMe), \"Should contain about me content\")\n\tuassert.True(t, strings.Contains(out, \"Theme Customization\"), \"Should contain theme customization section\")\n\tuassert.True(t, strings.Contains(out, \"Connect With Me\"), \"Should contain connect section\")\n}\n\nfunc TestRenderModernTheme(t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tmodernVotes, classicVotes, minimalVotes = 100, 0, 0\n\tcurrentTheme = \"modern\"\n\tupdateCurrentTheme()\n\n\tout := Render(\"\")\n\tuassert.True(t, strings.Contains(out, \"🚀 Matija's Space\"), \"Modern theme should have correct header\")\n}\n\nfunc TestRenderMinimalTheme(t *testing.T) {\n\ttesting.SetRealm(testing.NewUserRealm(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\")))\n\tmodernVotes, classicVotes, minimalVotes = 0, 0, 100\n\tcurrentTheme = \"minimal\"\n\tupdateCurrentTheme()\n\n\tout := Render(\"\")\n\tuassert.True(t, strings.Contains(out, \"Matija Marjanovic\"), \"Minimal theme should have correct header\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"WfM58sP5oYjE0v8X8KY0wfGTgnlViaXrUDQx7KVubSvA59iwsIeM2oMxKJ4YqhrkjAujrhXWER1kraIebBu/CA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"tokenhub","path":"gno.land/r/matijamarjanovic/tokenhub","files":[{"name":"errors.gno","body":"package tokenhub\n\nimport (\n\t\"errors\"\n)\n\nvar (\n\tErrNFTAlreadyRegistered = errors.New(\"NFT already registered\")\n\tErrNFTNotFound          = errors.New(\"NFT not found\")\n\tErrMTAlreadyRegistered  = errors.New(\"multi-token already registered\")\n\tErrMTNotFound           = errors.New(\"multi-token not found\")\n\tErrMTInfoNotFound       = errors.New(\"multi-token info not found\")\n\tErrNFTtokIDNotExists    = errors.New(\"NFT token ID does not exists\")\n\tErrNFTNotMetadata       = errors.New(\"NFT must implement IGRC721CollectionMetadata\")\n)\n"},{"name":"getters.gno","body":"package tokenhub\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/demo/tokens/grc1155\"\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n\t\"gno.land/r/sys/users\"\n)\n\n// GetUserTokenBalances returns a string of all the grc20 tokens the user owns\nfunc GetUserTokenBalances(userNameOrAddress string) string {\n\treturn getTokenBalances(userNameOrAddress, false)\n}\n\n// GetUserTokenBalancesNonZero returns a string of all the grc20 tokens the user owns, but only the ones that have a balance greater than 0\nfunc GetUserTokenBalancesNonZero(userNameOrAddress string) string {\n\treturn getTokenBalances(userNameOrAddress, true)\n}\n\n// GetUserNFTBalances returns a string of all the NFTs the user owns\nfunc GetUserNFTBalances(userNameOrAddress string) string {\n\treturn getNFTBalances(userNameOrAddress)\n}\n\n// GetUserMultiTokenBalances returns a string of all the multi-tokens the user owns\nfunc GetUserMultiTokenBalances(userNameOrAddress string) string {\n\treturn getMultiTokenBalances(userNameOrAddress, false)\n}\n\n// GetUserMultiTokenBalancesNonZero returns a string of all the multi-tokens the user owns, but only the ones that have a balance greater than 0\nfunc GetUserMultiTokenBalancesNonZero(userNameOrAddress string) string {\n\treturn getMultiTokenBalances(userNameOrAddress, true)\n}\n\n// GetToken returns a token instance for a given key\nfunc GetToken(key string) *grc20.Token {\n\treturn grc20reg.Get(key)\n}\n\n// MustGetToken returns a token instance for a given key, panics if the token is not found\nfunc MustGetToken(key string) *grc20.Token {\n\treturn grc20reg.MustGet(key)\n}\n\n// GetNFT returns a reader-only view of an NFT for a given key.\n//\n// SECURITY: the returned IGRC721Reader is data-untrusted. A malicious\n// NFTGetter registered via RegisterNFT can return a Reader that lies\n// about OwnerOf / BalanceOf / etc. Treat aggregator results as display\n// data, not authority data — never gate a transfer or mint on these\n// values. Writes are concrete methods on the owning realm's *basicNFT;\n// to mutate an NFT, call the owning realm's wrapper directly.\nfunc GetNFT(key string) grc721.IGRC721Reader {\n\tnftGetter := registeredNFTs.Get(key)\n\tif nftGetter == nil {\n\t\treturn nil\n\t}\n\treturn (nftGetter.(grc721.NFTGetter))()\n}\n\n// MustGetNFT returns a reader-only view of an NFT for a given key,\n// panics if the NFT is not found. See GetNFT for data-trust notes.\nfunc MustGetNFT(key string) grc721.IGRC721Reader {\n\tnftGetter := GetNFT(key)\n\tif nftGetter == nil {\n\t\tpanic(\"unknown NFT: \" + key)\n\t}\n\treturn nftGetter\n}\n\n// GetMultiToken returns a multi-token instance for a given key\nfunc GetMultiToken(key string) grc1155.IGRC1155 {\n\tinfo := registeredMTs.Get(key)\n\tif info == nil {\n\t\treturn nil\n\t}\n\tmt := info.(GRC1155TokenInfo).Collection\n\treturn mt()\n}\n\n// MustGetMultiToken returns a multi-token instance for a given key, panics if the multi-token is not found\nfunc MustGetMultiToken(key string) grc1155.IGRC1155 {\n\tinfo := GetMultiToken(key)\n\tif info == nil {\n\t\tpanic(\"unknown multi-token: \" + key)\n\t}\n\treturn info\n}\n\n// GetAllNFTs returns a string of all the NFTs registered\nfunc GetAllNFTs() string {\n\tvar out string\n\tregisteredNFTs.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tout += ufmt.Sprintf(\"NFT:%s,\", key)\n\t\treturn false\n\t})\n\treturn out\n}\n\n// GetAllTokens returns a string of all the tokens registered\nfunc GetAllTokens() string {\n\tvar out string\n\tgrc20reg.GetRegistry().Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tout += \"Token:\" + key + \",\"\n\t\treturn false\n\t})\n\treturn out\n}\n\n// GetAllTokenWithDetails returns a string of all the tokens registered with their details\nfunc GetAllTokenWithDetails() string {\n\tvar out string\n\tgrc20reg.GetRegistry().Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\ttoken := value.(*grc20.Token)\n\t\tout += ufmt.Sprintf(\"Token:%s,Name:%s,Symbol:%s,Decimals:%d;\", key, token.GetName(), token.GetSymbol(), token.GetDecimals())\n\t\treturn false\n\t})\n\treturn out\n}\n\n// GetAllMultiTokens returns a string of all the multi-tokens registered\nfunc GetAllMultiTokens() string {\n\tvar out string\n\tregisteredMTs.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tout += \"MultiToken:\" + key + \",\"\n\t\treturn false\n\t})\n\treturn out\n}\n\n// GetAllRegistered returns a string of all the registered tokens, NFTs and multi-tokens\nfunc GetAllRegistered() string {\n\treturn GetAllNFTs() + GetAllTokens() + GetAllMultiTokens()\n}\n\n// getNFTBalances returns a string of all the NFTs the user owns\nfunc getNFTBalances(input string) string {\n\taddr := getAddressForUsername(input)\n\tif !addr.IsValid() {\n\t\tpanic(\"invalid address or username: \" + input)\n\t}\n\tvar out string\n\n\tregisteredNFTs.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tnftGetter := value.(grc721.NFTGetter)\n\t\tnft := nftGetter()\n\t\tkey_parts := strings.Split(key, \".\")\n\t\towner, err := nft.OwnerOf(grc721.TokenID(key_parts[len(key_parts)-1]))\n\t\tif err == nil \u0026\u0026 addr == owner { // show only the nfts owner owns\n\t\t\tout += \"NFT:\" + key + \",\"\n\t\t}\n\t\treturn false\n\t})\n\n\treturn out\n}\n\n// getTokenBalances returns a string of all the tokens the user owns\nfunc getTokenBalances(input string, nonZero bool) string {\n\n\taddr := getAddressForUsername(input)\n\tif !addr.IsValid() {\n\t\tpanic(\"invalid address or username: \" + input)\n\t}\n\tvar out string\n\tgrc20reg.GetRegistry().Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\n\t\ttoken := value.(*grc20.Token)\n\t\tbalance := token.BalanceOf(addr)\n\t\tif !nonZero || balance \u003e 0 {\n\t\t\tout += ufmt.Sprintf(\"Token:%s:%d,\", key, balance)\n\t\t}\n\t\treturn false\n\t})\n\n\treturn out\n}\n\n// getMultiTokenBalances returns a string of all the multi-tokens the user owns\nfunc getMultiTokenBalances(input string, nonZero bool) string {\n\taddr := getAddressForUsername(input)\n\tif !addr.IsValid() {\n\t\tpanic(\"invalid address or username: \" + input)\n\t}\n\tvar out string\n\n\tregisteredMTs.Iterate(\"\", \"\", func(key string, value interface{}) bool {\n\t\tinfo := value.(GRC1155TokenInfo)\n\t\tmt := info.Collection()\n\t\tbalance, err := mt.BalanceOf(addr, grc1155.TokenID(info.TokenID))\n\t\tif err == nil {\n\t\t\tif !nonZero || balance \u003e 0 {\n\t\t\t\tout += ufmt.Sprintf(\"MultiToken:%s:%d,\", key, balance)\n\t\t\t}\n\t\t}\n\t\treturn false\n\t})\n\n\treturn out\n}\n\n// getAddressForUsername returns an address for a given username or address\nfunc getAddressForUsername(addrOrName string) address {\n\taddr := address(addrOrName)\n\tif addr.IsValid() {\n\t\treturn addr\n\t}\n\n\tif userData, _ := users.ResolveName(addrOrName); userData != nil {\n\t\treturn userData.Addr()\n\t}\n\n\treturn \"\"\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/matijamarjanovic/tokenhub\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package tokenhub\n\nimport (\n\t\"regexp\"\n\t\"strings\"\n\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/avl/v0/pager\"\n\t\"gno.land/p/nt/fqname/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n)\n\nconst (\n\ttoken = \"token\" // grc20\n\tnft   = \"nft\"   // grc721\n\tmt    = \"mt\"    // grc1155\n)\n\nfunc Render(path string) string {\n\tvar out string\n\n\tswitch {\n\tcase path == \"\":\n\t\tout = renderHome()\n\n\tcase strings.HasPrefix(path, token):\n\t\tout = renderToken(path)\n\n\tcase strings.HasPrefix(path, nft):\n\t\tout = renderNFT(path)\n\n\tcase strings.HasPrefix(path, mt):\n\t\tout = renderMT(path)\n\t}\n\n\treturn out\n}\n\nfunc renderHome() string {\n\tout := md.H1(\"Token Hub\")\n\tout += md.Paragraph(\"Token Hub provides listings of all existing token types on Gno.land - GRC20 tokens, GRC721 NFTs, and GRC1155 multi-tokens. You can browse these listings to find available tokens, check balances, and access token metadata. If you're developing wallets or interfaces, you can query this registry to display token information to your users.\")\n\n\tlinks := []string{\n\t\t\"[GRC20 Tokens](/r/matijamarjanovic/tokenhub:tokens)\",\n\t\t\"[GRC721 NFTs](/r/matijamarjanovic/tokenhub:nfts)\",\n\t\t\"[GRC1155 Multi-Tokens](/r/matijamarjanovic/tokenhub:mts)\",\n\t}\n\tout += md.BulletList(links)\n\n\tout += md.H2(\"How to Register Your Tokens\")\n\tout += md.Paragraph(\"You can register your tokens with the following import and function calls:\")\n\n\tregisterCode := `// Import packages\nimport (\n\t\"gno.land/r/matijamarjanovic/tokenhub\"\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/demo/tokens/grc1155\"\n)\n\n// grc20.NewToken and grc721.NewBasicNFT must be constructed from a realm\n// context (init or a crossing function) because they assert cur.IsCurrent().\nfunc init(cur realm) {\n\t// GRC20 token\n\tmyToken, myLedger := grc20.NewToken(0, cur, \"My Token\", \"MTK\", 6)\n\tmyTokenPath := tokenhub.RegisterToken(myToken, \"my_token\")\n\n\t// GRC721 NFT\n\tmyNFT := grc721.NewBasicNFT(0, cur, \"My NFT Collection\", \"MNFT\")\n\tmyNFT.Mint(\"g1your_address_here\", \"1\")\n\tnftErr := tokenhub.RegisterNFT(myNFT.Getter(), \"my_collection\", \"1\")\n\n\t// GRC1155 multi-token\n\tmyMultiToken := grc1155.NewBasicGRC1155Token(\"https://metadata.example.com/\")\n\tmyMultiToken.SafeMint(\"g1your_address_here\", \"123\", 10)\n\tmultiErr := tokenhub.RegisterMultiToken(myMultiToken.Getter(), \"123\")\n}`\n\n\tout += md.LanguageCodeBlock(\"go\", registerCode)\n\n\tout += \"\\n\"\n\tout += md.H2(\"Querying Token Information\")\n\tout += md.Paragraph(\"You can query token information and balances using functions like:\")\n\n\tqueryCode := `// Get all registered tokens\nallTokens := tokenhub.GetAllTokens()\n\n// Get token balances for a user\nbalances := tokenhub.GetUserTokenBalances(\"g1...\")\n\n// Get non-zero token balances\nnonZeroBalances := tokenhub.GetUserTokenBalancesNonZero(\"g1...\")`\n\n\tout += md.LanguageCodeBlock(\"go\", queryCode)\n\n\treturn out\n}\n\nfunc renderToken(path string) string {\n\tout := md.H1(\"GRC20 Tokens\")\n\tout += md.Paragraph(\"Below is a list of all registered GRC20 tokens and their registry keys (keys are used to query token information).\")\n\n\tvar tokenItems []string\n\n\ttokenPager := pager.NewPager(grc20reg.GetRegistry(), pageSize, false)\n\tpage := tokenPager.MustGetPageByPath(path)\n\n\tfor _, item := range page.Items {\n\t\ttoken := item.Value.(*grc20.Token)\n\t\tpkgPath, _ := fqname.Parse(item.Key)\n\t\tlinkURL := formatLinkURL(pkgPath, 0)\n\n\t\ttokenItems = append(tokenItems, ufmt.Sprintf(\"%s (%s) - %s\",\n\t\t\tmd.Link(token.GetName(), linkURL),\n\t\t\tmd.InlineCode(token.GetSymbol()),\n\t\t\tmd.InlineCode(item.Key)))\n\t}\n\n\tout += renderItemsList(tokenItems, page, \"No tokens registered yet\")\n\treturn out\n}\n\nfunc renderNFT(path string) string {\n\tout := md.H1(\"GRC721 NFTs\")\n\tout += md.Paragraph(\"Below is a list of all registered GRC721 NFT collections and their registry keys (keys are used to query token information).\")\n\n\tvar nftItems []string\n\tnftPager := pager.NewPager(registeredNFTs, pageSize, false)\n\tpage := nftPager.MustGetPageByPath(path)\n\n\tfor _, item := range page.Items {\n\t\tnftGetter := item.Value.(grc721.NFTGetter)\n\t\tnft := nftGetter()\n\t\tmetadata, ok := nft.(grc721.IGRC721CollectionMetadata)\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\n\t\tpkgPath, _ := fqname.Parse(item.Key)\n\t\tlinkURL := formatLinkURL(pkgPath, 2)\n\n\t\tnftItems = append(nftItems, ufmt.Sprintf(\"%s (%s) - %s\",\n\t\t\tmd.Link(metadata.Name(), linkURL),\n\t\t\tmd.InlineCode(metadata.Symbol()),\n\t\t\tmd.InlineCode(item.Key)))\n\t}\n\n\tout += renderItemsList(nftItems, page, \"No NFTs registered yet\")\n\treturn out\n}\n\nfunc renderMT(path string) string {\n\tout := md.H1(\"GRC1155 Multi-Tokens\")\n\tout += md.Paragraph(\"Below is a list of all registered GRC1155 multi-tokens and their registry keys (keys are used to query token information).\")\n\n\tvar mtItems []string\n\n\tmtPager := pager.NewPager(registeredMTs, pageSize, false)\n\tpage := mtPager.MustGetPageByPath(path)\n\n\tfor _, item := range page.Items {\n\t\tinfo := item.Value.(GRC1155TokenInfo)\n\t\tpkgPath, _ := fqname.Parse(item.Key)\n\t\tlinkURL := formatLinkURL(pkgPath, 1)\n\n\t\tmtItems = append(mtItems, ufmt.Sprintf(\"%s %s - %s\",\n\t\t\tmd.Bold(\"TokenID:\"),\n\t\t\tmd.Link(info.TokenID, linkURL),\n\t\t\tmd.InlineCode(item.Key)))\n\t}\n\n\tout += renderItemsList(mtItems, page, \"No multi-tokens registered yet\")\n\treturn out\n}\n\nfunc renderItemsList(items []string, page *pager.Page, emptyMessage string) string {\n\tvar out string\n\tif len(items) == 0 {\n\t\tout += md.Italic(emptyMessage)\n\t\tout += \"\\n\"\n\t\treturn out\n\t}\n\n\tout += md.BulletList(items)\n\tout += \"\\n\"\n\tout += md.HorizontalRule()\n\n\tpicker := page.Picker(page.Pager.PageQueryParam)\n\tif picker != \"\" {\n\t\tout += md.Paragraph(picker)\n\t}\n\n\treturn out\n}\n\nfunc formatLinkURL(pkgPath string, trailingSegmentsToRemove int) string {\n\tre1 := regexp.MustCompile(`gno\\.land/r/matijamarjanovic/tokenhub\\.`)\n\tpkgPath = re1.ReplaceAllString(pkgPath, \"\")\n\n\tre2 := regexp.MustCompile(`gno\\.land`)\n\turl := re2.ReplaceAllString(pkgPath, \"\")\n\n\tif trailingSegmentsToRemove \u003e 0 {\n\t\tre3 := regexp.MustCompile(`\\.`)\n\t\tparts := re3.Split(url, -1)\n\t\tif len(parts) \u003e trailingSegmentsToRemove {\n\t\t\turl = strings.Join(parts[:len(parts)-trailingSegmentsToRemove], \".\")\n\t\t}\n\t}\n\n\treturn url\n}\n"},{"name":"tokenhub.gno","body":"package tokenhub\n\nimport (\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/nt/fqname/v0\"\n\t\"gno.land/r/demo/defi/grc20reg\"\n\n\t\"gno.land/p/demo/tokens/grc1155\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/nt/avl/v0\"\n\n\t\"gno.land/r/leon/hor\"\n)\n\ntype GRC1155TokenInfo struct {\n\tCollection grc1155.MultiTokenGetter\n\tTokenID    string\n}\n\nvar (\n\tregisteredTokens = avl.NewTree() // rlmPath[.slug] -\u003e *grc20.Token\n\tregisteredNFTs   = avl.NewTree() // rlmPath[.slug] -\u003e grc721.NFTGetter\n\tregisteredMTs    = avl.NewTree() // rlmPath[.slug] -\u003e GRC1155TokenInfo\n)\n\nconst pageSize = 10\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"Token Hub\", \"Registry for tokens and NFTs on gno.land\")\n}\n\n// RegisterToken is a function that uses gno.land/r/demo/defi/grc20reg to register a token\n// RegisterToken registers a token in grc20reg with the given slug.\n// Returns the registry key that can be used to retrieve the token.\nfunc RegisterToken(cur realm, token *grc20.Token, slug string) string {\n\tgrc20reg.Register(cross(cur), token, slug)\n\treturn fqname.Construct(cur.PkgPath(), slug)\n}\n\n// RegisterNFT is a function that registers an NFT in an avl.Tree\nfunc RegisterNFT(cur realm, nftGetter grc721.NFTGetter, collection string, tokenId string) error {\n\tnft := nftGetter()\n\t_, ok := nft.(grc721.IGRC721CollectionMetadata)\n\tif !ok {\n\t\treturn ErrNFTNotMetadata\n\t}\n\n\tnftOwner, err := nft.OwnerOf(grc721.TokenID(tokenId))\n\n\tif err != nil {\n\t\treturn err\n\t}\n\tif !nftOwner.IsValid() {\n\t\treturn ErrNFTtokIDNotExists\n\t}\n\n\trlmPath := cur.Previous().PkgPath()\n\tkey := rlmPath + \".\" + collection + \".\" + tokenId\n\n\tif registeredNFTs.Has(key) {\n\t\treturn ErrNFTAlreadyRegistered\n\t}\n\n\tregisteredNFTs.Set(key, nftGetter)\n\treturn nil\n}\n\n// RegisterMultiToken is a function that registers a multi-token in an avl.Tree\n// The avl.Tree value is a struct defined in this realm. It contains not only the getter (like other token types) but also the tokenID\nfunc RegisterMultiToken(cur realm, mtGetter grc1155.MultiTokenGetter, tokenID string) error {\n\trlmPath := cur.Previous().PkgPath()\n\tkey := rlmPath + \".\" + tokenID\n\n\tif registeredMTs.Has(key) {\n\t\treturn ErrMTAlreadyRegistered\n\t}\n\n\tregisteredMTs.Set(key, GRC1155TokenInfo{\n\t\tCollection: mtGetter,\n\t\tTokenID:    tokenID,\n\t})\n\treturn nil\n}\n"},{"name":"tokenhub_test.gno","body":"package tokenhub\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/demo/tokens/grc1155\"\n\t\"gno.land/p/demo/tokens/grc20\"\n\t\"gno.land/p/demo/tokens/grc721\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nconst testRealmPkgPath = \"gno.land/r/matijamarjanovic/testrealm\"\n\n// newTestNFT constructs an NFT through a same-realm cross so NewBasicNFT's\n// IsCurrent + non-empty PkgPath checks pass.\nfunc newTestNFT(_ int, rlm realm, name, symbol string) *grc721.BasicNFT {\n\tvar nft *grc721.BasicNFT\n\tfunc(cur realm) {\n\t\tnft = grc721.NewBasicNFT(0, cur, name, symbol)\n\t}(cross(rlm))\n\treturn nft\n}\n\nfunc TestTokenRegistration(cur realm, t *testing.T) {\n\tresetState(t)\n\ttesting.SetRealm(testing.NewCodeRealm(testRealmPkgPath))\n\n\ttoken, _ := grc20.NewToken(0, cur, \"Test Token\", \"TEST\", 6)\n\tRegisterToken(cross(cur), token, \"test_token\")\n\n\tretrievedToken := GetToken(\"gno.land/r/matijamarjanovic/tokenhub.test_token\")\n\n\turequire.True(t, retrievedToken != nil, \"Should retrieve registered token\")\n\tuassert.Equal(t, \"Test Token\", retrievedToken.GetName(), \"Token name should match\")\n\tuassert.Equal(t, \"TEST\", retrievedToken.GetSymbol(), \"Token symbol should match\")\n}\n\nfunc TestNFTRegistration(cur realm, t *testing.T) {\n\tresetState(t)\n\ttesting.SetRealm(testing.NewCodeRealm(testRealmPkgPath))\n\n\tnft := newTestNFT(0, cur, \"Test NFT\", \"TNFT\")\n\tnft.Mint(unsafe.CurrentRealm().Address(), grc721.TokenID(\"1\"))\n\terr := RegisterNFT(cross(cur), nft.Getter(), \"test_nft\", \"1\")\n\n\turequire.NoError(t, err, \"Should register NFT without error\")\n\n\tretrievedNFT := GetNFT(testRealmPkgPath + \".test_nft.1\")\n\n\turequire.True(t, retrievedNFT != nil, \"Should retrieve registered NFT\")\n\n\tmetadata, ok := retrievedNFT.(grc721.IGRC721CollectionMetadata)\n\turequire.True(t, ok, \"NFT should implement IGRC721CollectionMetadata\")\n\tuassert.Equal(t, \"Test NFT\", metadata.Name(), \"NFT name should match\")\n\tuassert.Equal(t, \"TNFT\", metadata.Symbol(), \"NFT symbol should match\")\n}\n\nfunc TestGRC1155Registration(cur realm, t *testing.T) {\n\tresetState(t)\n\ttesting.SetRealm(testing.NewCodeRealm(testRealmPkgPath))\n\n\tmt := grc1155.NewBasicGRC1155Token(\"test-uri\")\n\terr := RegisterMultiToken(cross(cur), mt.Getter(), \"1\")\n\turequire.NoError(t, err, \"Should register multi-token without error\")\n\n\tmultiToken := GetMultiToken(testRealmPkgPath + \".1\")\n\turequire.True(t, multiToken != nil, \"Should retrieve multi-token\")\n\t_, ok := multiToken.(grc1155.IGRC1155)\n\turequire.True(t, ok, \"Retrieved multi-token should implement IGRC1155\")\n\n\terr = RegisterMultiToken(cross(cur), mt.Getter(), \"1\")\n\tuassert.True(t, err != nil, \"Should not allow duplicate registration\")\n}\n\nfunc TestBalanceRetrieval(cur realm, t *testing.T) {\n\tresetState(t)\n\ttesting.SetRealm(testing.NewCodeRealm(testRealmPkgPath))\n\n\ttoken, ledger := grc20.NewToken(0, cur, \"Test Token\", \"TEST\", 6)\n\tRegisterToken(cross(cur), token, \"test_token\")\n\tledger.Mint(unsafe.CurrentRealm().Address(), 1000)\n\n\tbalances := GetUserTokenBalances(unsafe.CurrentRealm().Address().String())\n\tuassert.True(t, strings.Contains(balances,\n\t\t\"Token:gno.land/r/matijamarjanovic/tokenhub.test_token:1000\"), \"Should show correct GRC20 balance\")\n\n\tnft := newTestNFT(0, cur, \"Test NFT\", \"TNFT\")\n\tnft.Mint(unsafe.CurrentRealm().Address(), grc721.TokenID(\"1\"))\n\tuassert.NoError(t, RegisterNFT(cross(cur), nft.Getter(), \"test_nft\", \"1\"))\n\n\tbalances = GetUserNFTBalances(unsafe.CurrentRealm().Address().String())\n\tuassert.True(t, strings.Contains(balances, \"NFT:\"+testRealmPkgPath+\".test_nft.1\"), \"Should show correct NFT balance\")\n\n\tmt := grc1155.NewBasicGRC1155Token(\"test-uri\")\n\terr := RegisterMultiToken(cross(cur), mt.Getter(), \"1\")\n\turequire.NoError(t, err, \"Should register multi-token without error\")\n\n\tbalances = GetUserMultiTokenBalances(unsafe.CurrentRealm().Address().String())\n\tuassert.True(t, strings.Contains(balances, \"MultiToken:\"+testRealmPkgPath+\".1:0\"), \"Should show multi-token balance\")\n\n\tnonZeroBalances := GetUserTokenBalancesNonZero(unsafe.CurrentRealm().Address().String())\n\tuassert.True(t, strings.Contains(nonZeroBalances,\n\t\t\"Token:gno.land/r/matijamarjanovic/tokenhub.test_token:1000\"), \"Should show non-zero GRC20 balance\")\n}\n\nfunc TestErrorCases(cur realm, t *testing.T) {\n\tresetState(t)\n\ttesting.SetRealm(testing.NewCodeRealm(testRealmPkgPath))\n\n\tnft := newTestNFT(0, cur, \"Test NFT\", \"TNFT\")\n\terr := RegisterNFT(cross(cur), nft.Getter(), \"test_nft\", \"1\")\n\n\terr = RegisterNFT(cross(cur), nft.Getter(), \"test_nft\", \"1\")\n\tuassert.True(t, err != nil, \"Should not allow duplicate registration\")\n\n\tmt := grc1155.NewBasicGRC1155Token(\"test-uri\")\n\terr = RegisterMultiToken(cross(cur), mt.Getter(), \"1\")\n\n\terr = RegisterMultiToken(cross(cur), mt.Getter(), \"1\")\n\tuassert.True(t, err != nil, \"Should not allow duplicate registrasdasdation\")\n}\n\nfunc TestTokenListingFunctions(cur realm, t *testing.T) {\n\tresetState(t)\n\ttesting.SetRealm(testing.NewCodeRealm(testRealmPkgPath))\n\n\tgrc20Token, _ := grc20.NewToken(0, cur, \"Test Token\", \"TEST\", 6)\n\tRegisterToken(cross(cur), grc20Token, \"listing_token\")\n\n\tgrc20List := GetAllTokens()\n\tuassert.True(t, strings.Contains(grc20List, \"Token:gno.land/r/matijamarjanovic/tokenhub.listing_token\"),\n\t\t\"GetAllGRC20Tokens should list registered token\")\n\n\tnftToken := newTestNFT(0, cur, \"Listing NFT\", \"LNFT\")\n\tnftToken.Mint(address(\"g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y\"), grc721.TokenID(\"1\"))\n\tRegisterNFT(cross(cur), nftToken.Getter(), \"listing_nft\", \"1\")\n\n\tnftList := GetAllNFTs()\n\tuassert.True(t, strings.Contains(nftList, \"NFT:\"+testRealmPkgPath+\".listing_nft.1\"),\n\t\t\"GetAllNFTs should list registered NFT\")\n\n\tmultiToken := grc1155.NewBasicGRC1155Token(\"test-uri\")\n\tRegisterMultiToken(cross(cur), multiToken.Getter(), \"listing_mt\")\n\n\tgrc1155List := GetAllMultiTokens()\n\tuassert.True(t, strings.Contains(grc1155List, \"MultiToken:\"+testRealmPkgPath+\".listing_mt\"),\n\t\t\"GetAllMultiTokens should list registered multi-token\")\n\n\tcompleteList := GetAllRegistered()\n\tuassert.True(t, strings.Contains(completeList, \"NFT:\"+testRealmPkgPath+\".listing_nft.1\"),\n\t\t\"GetAllTokens should list NFTs\")\n\tuassert.True(t, strings.Contains(completeList, \"Token:gno.land/r/matijamarjanovic/tokenhub.listing_token\"),\n\t\t\"GetAllTokens should list GRC20 tokens\")\n\tuassert.True(t, strings.Contains(completeList, \"MultiToken:\"+testRealmPkgPath+\".listing_mt\"),\n\t\t\"GetAllTokens should list multi-tokens\")\n}\n\nfunc TestMustGetFunctions(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(testRealmPkgPath))\n\n\ttoken, _ := grc20.NewToken(0, cur, \"Must Token\", \"MUST\", 6)\n\tRegisterToken(cross(cur), token, \"must_token\")\n\n\tretrievedToken := MustGetToken(\"gno.land/r/matijamarjanovic/tokenhub.must_token\")\n\tuassert.Equal(t, \"Must Token\", retrievedToken.GetName(), \"Token name should match\")\n\n\t// Under the unified declaring-realm borrow, calling MustGetToken\n\t// (which dispatches to grc20reg.MustGet in /r/demo/defi/grc20reg)\n\t// crosses a soft realm boundary. Panics across borrows are aborts\n\t// not catchable by plain defer/recover — use revive() to capture.\n\tr := revive(func() { MustGetToken(\"non_existent_token\") })\n\tuassert.True(t, r != nil, \"MustGetToken should panic for non-existent token\")\n\tuassert.True(t, strings.Contains(r.(string), \"unknown token\"), \"Panic message should mention unknown token\")\n\n\tnft := newTestNFT(0, cur, \"Must NFT\", \"MNFT\")\n\tnft.Mint(unsafe.CurrentRealm().Address(), grc721.TokenID(\"1\"))\n\tRegisterNFT(cross(cur), nft.Getter(), \"must_nft\", \"1\")\n\n\t// NFT registration key is cur.Previous().PkgPath() + \".\" + collection + \".\" + tokenId\n\t// where cur.Previous() at the cross-call site is the realm set via SetRealm above (testRealmPkgPath).\n\tretrievedNFT := MustGetNFT(testRealmPkgPath + \".must_nft.1\")\n\tmetadata, ok := retrievedNFT.(grc721.IGRC721CollectionMetadata)\n\turequire.True(t, ok, \"NFT should implement IGRC721CollectionMetadata\")\n\tuassert.Equal(t, \"Must NFT\", metadata.Name(), \"NFT name should match\")\n\n\t// MustGetNFT's panic is local (panic site is in this package, no foreign\n\t// realm call), so defer/recover catches it without crossing any borrow.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tr := recover()\n\t\t\tuassert.True(t, r != nil, \"MustGetNFT should panic for non-existent NFT\")\n\t\t\tuassert.True(t, strings.Contains(r.(string), \"unknown NFT\"), \"Panic message should mention unknown NFT\")\n\t\t}()\n\t\tMustGetNFT(\"non_existent_nft\")\n\t}()\n\n\tmt := grc1155.NewBasicGRC1155Token(\"must-uri\")\n\tRegisterMultiToken(cross(cur), mt.Getter(), \"must_mt\")\n\n\t// MultiToken registration key is cur.Previous().PkgPath() + \".\" + tokenID.\n\tretrievedMT := MustGetMultiToken(testRealmPkgPath + \".must_mt\")\n\t_, ok = retrievedMT.(grc1155.IGRC1155)\n\turequire.True(t, ok, \"Retrieved multi-token should implement IGRC1155\")\n\n\t// Local panic, same as MustGetNFT above.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tr := recover()\n\t\t\tuassert.True(t, r != nil, \"MustGetMultiToken should panic for non-existent multi-token\")\n\t\t\tuassert.True(t, strings.Contains(r.(string), \"unknown multi-token\"), \"Panic message should mention unknown multi-token\")\n\t\t}()\n\t\tMustGetMultiToken(\"non_existent_mt\")\n\t}()\n}\n\nfunc resetState(t *testing.T) {\n\tt.Helper()\n\n\tregisteredTokens = avl.NewTree()\n\tregisteredNFTs = avl.NewTree()\n\tregisteredMTs = avl.NewTree()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"4bLIms8vbUzC9d1jMCmIQ+Ha8GQjrJq/qC4mbu2iyGyz+Hia1x3dQgbSXjiNYPCDE2D4lNQATBKkmmgDF0ksBA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"calculator","path":"gno.land/r/miko/calculator","files":[{"name":"calculator.gno","body":"package calculator\n\nimport (\n\t\"net/url\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/r/leon/hor\"\n)\n\ntype Node struct {\n\tvalue string // Value of the current node\n\tleft  *Node\n\tright *Node\n}\n\nconst (\n\tspecialCharacters             = \"+-*/.\"\n\tspecialCharactersWithoutMinus = \"+*/.\"\n\ttopPriority                   = \"*/\"\n\tlowPriority                   = \"+-\"\n\trealmPath                     = \"/r/miko/calculator\"\n)\n\nvar (\n\tval        float64\n\tdisplayVal string\n\n\toperationMap = map[string]func(left float64, right float64) float64{\n\t\t\"+\": func(left float64, right float64) float64 { return left + right },\n\t\t\"-\": func(left float64, right float64) float64 { return left - right },\n\t\t\"*\": func(left float64, right float64) float64 { return left * right },\n\t\t\"/\": func(left float64, right float64) float64 {\n\t\t\tif right == 0 {\n\t\t\t\tpanic(\"Division by 0 is forbidden\")\n\t\t\t}\n\t\t\treturn left / right\n\t\t},\n\t}\n)\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"Miko's calculator\", \"\")\n}\n\nfunc evaluateValidity(line string) (bool, string) {\n\tif len(line) == 0 {\n\t\treturn false, \"Invalid empty input\"\n\t} // edge case empty line\n\tif strings.Index(specialCharactersWithoutMinus, string(line[0])) != -1 ||\n\t\tstrings.Index(specialCharacters, string(line[len(line)-1])) != -1 {\n\t\treturn false, \"Invalid equation\"\n\t} // edge case special character at begining or end\n\n\tisPriorSpecial := false\n\tcountParenthesis := 0\n\n\tfor i := 0; i \u003c len(line); i++ {\n\t\tif line[i] == '(' {\n\t\t\tcountParenthesis += 1\n\t\t\tcontinue\n\t\t}\n\t\tif line[i] == ')' {\n\t\t\tif isPriorSpecial == true {\n\t\t\t\treturn false, \"Invalid equation\"\n\t\t\t}\n\t\t\tcountParenthesis -= 1\n\t\t\tisPriorSpecial = false\n\t\t\tcontinue\n\t\t}\n\t\tif strings.Index(specialCharacters, string(line[i])) != -1 {\n\t\t\tif isPriorSpecial \u0026\u0026 !(line[i] == '-' \u0026\u0026 i \u003c (len(line)-1) \u0026\u0026 line[i+1] \u003e= '0' \u0026\u0026 line[i+1] \u003c= '9') { // if we have two subsequent operator and the second one isn't a - before a number (negative number)\n\t\t\t\treturn false, \"Invalid equation\"\n\t\t\t}\n\t\t\tisPriorSpecial = true\n\t\t\tcontinue\n\t\t}\n\t\tif line[i] \u003c '0' || line[i] \u003e '9' {\n\t\t\treturn false, \"Invalid character encountered \"\n\t\t}\n\t\tisPriorSpecial = false\n\t}\n\n\tif countParenthesis != 0 {\n\t\treturn false, \"Invalid equation\"\n\t}\n\treturn true, \"\"\n}\n\nfunc searchForPriority(priorityList string, line string) *Node {\n\tcountParenthesis := 0\n\tfor iPrio := 0; iPrio \u003c len(priorityList); iPrio++ {\n\t\tfor idx := 0; idx \u003c len(line); idx++ {\n\t\t\tif line[idx] == '(' {\n\t\t\t\tcountParenthesis += 1\n\t\t\t}\n\t\t\tif line[idx] == ')' {\n\t\t\t\tcountParenthesis -= 1\n\t\t\t}\n\t\t\tif countParenthesis == 0 \u0026\u0026 line[idx] == priorityList[iPrio] \u0026\u0026\n\t\t\t\t!(line[idx] == '-' \u0026\u0026 (idx == 0 || strings.Index(specialCharacters, string(line[idx-1])) != -1)) { // - is not a substract sign if at the begining or after another sign\n\t\t\t\treturn \u0026Node{string(line[idx]), createTree(line[:idx]), createTree(line[idx+1:])}\n\t\t\t}\n\n\t\t}\n\t}\n\treturn nil\n}\n\n// checks if the expression in line is contained in one big parenthesis\nfunc isInOneParenthesis(line string) bool {\n\tif line[0] != '(' || line[len(line)-1] != ')' {\n\t\treturn false\n\t}\n\tcountParenthesis := 1\n\tfor i := 1; i \u003c len(line)-1; i++ {\n\t\tif line[i] == '(' {\n\t\t\tcountParenthesis += 1\n\t\t}\n\t\tif line[i] == ')' {\n\t\t\tcountParenthesis -= 1\n\t\t}\n\t\tif countParenthesis == 0 {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc createTree(line string) *Node {\n\tif isInOneParenthesis(line) {\n\t\treturn createTree(line[1 : len(line)-1])\n\t}\n\tnode := searchForPriority(lowPriority, line) // we put the lowest priority at the top of the tree, these operations will be executed last\n\tif node != nil {\n\t\treturn node\n\t}\n\tnode = searchForPriority(topPriority, line)\n\tif node != nil {\n\t\treturn node\n\t}\n\n\t// if this code is reached, the only value possible in line is a number\n\treturn \u0026Node{line, nil, nil}\n}\n\nfunc readTree(tree *Node) float64 {\n\toperation, exists := operationMap[tree.value]\n\n\tif exists { // check if the current node is an operator\n\t\treturn operation(readTree(tree.left), readTree(tree.right))\n\t}\n\n\tparsedValue, _ := strconv.ParseFloat(tree.value, 64)\n\n\treturn parsedValue\n}\n\n// expression is the equation you want to solve (p replaces the + symbol)\n// exemple: 2p4/2\nfunc ComputeResult(expression string) string {\n\tvalid, errString := evaluateValidity(expression)\n\n\tif !valid { // If a basic error is encountered, return the expression without the = at the end and display the same expression\n\t\tprintln(errString) // display error for debug\n\t\treturn expression\n\t}\n\ttree := createTree(expression)\n\tval = readTree(tree)\n\tdisplayVal = strconv.FormatFloat(val, 'g', 6, 64)\n\treturn displayVal\n}\n\nfunc removeLast(path string) string {\n\tlenPath := len(path)\n\tif lenPath \u003e 0 {\n\t\tpath = path[:lenPath-1]\n\t}\n\treturn path\n}\n\nfunc createTable(req *realmpath.Request, query url.Values, expression string) mdtable.Table {\n\tline := make([]string, 0, 4)\n\tquery.Set(\"expression\", \"\")\n\tline = append(line, md.Link(\"res\", req.String()))\n\tfor _, str := range []string{\"(\", \")\"} {\n\t\tquery.Set(\"expression\", expression+str)\n\t\tline = append(line, md.Link(str, req.String()))\n\t}\n\tquery.Set(\"expression\", removeLast(expression))\n\tline = append(line, md.Link(\"del\", req.String())) // req and del are two special cases\n\ttable := mdtable.Table{\n\t\tHeaders: line,\n\t}\n\tline = []string{}\n\tfor _, c := range \"789+456-123*0.=/\" {\n\t\tquery.Set(\"expression\", expression+string(c))\n\t\tline = append(line, md.Link(string(c), req.String()))\n\t\tif len(line) == 4 {\n\t\t\ttable.Append(line)\n\t\t\tline = []string{}\n\t\t}\n\t}\n\treturn table\n}\n\nfunc Render(path string) string {\n\treq := realmpath.Parse(path)\n\tquery := req.Query\n\texpression := query.Get(\"expression\")\n\n\tif expression == \"\" {\n\t\tdisplayVal = \"0\"\n\t} else {\n\t\tif expression[len(expression)-1] == '=' {\n\t\t\texpression = ComputeResult(expression[:len(expression)-1])\n\t\t} else {\n\t\t\tdisplayVal = expression\n\t\t}\n\t}\n\tout := md.H1(\"Calculator page\")\n\tout += md.H3(\"Have you ever wanted to do maths but never actually found a calculator ?\")\n\tout += md.H3(\"Do I have the realm for you...\")\n\tout += \"---------------\\n\"\n\tout += md.H2(\"Result: \" + displayVal)\n\n\ttable := createTable(req, query, expression)\n\tout += table.String()\n\treturn out\n}\n"},{"name":"calculator_test.gno","body":"package calculator\n\nimport \"testing\"\n\nfunc Test_Addition(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"1+1\")\n\t// Verify the value is equal to 2\n\tif value != \"2\" {\n\t\tt.Fatalf(\"1 + 1 is not equal to 2\")\n\t}\n}\n\nfunc Test_Subtraction(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"1-1\")\n\t// Verify the value is equal to 0\n\tif value != \"0\" {\n\t\tt.Fatalf(\"1 - 1 is not equal to 0\")\n\t}\n}\n\nfunc Test_Multiplication(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"1*4\")\n\t// Verify the value is equal to 4\n\tif value != \"4\" {\n\t\tt.Fatalf(\"1 * 4 is not equal to 4\")\n\t}\n}\n\nfunc Test_Division(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"4/2\")\n\t// Verify the value is equal to 2\n\tif value != \"2\" {\n\t\tt.Fatalf(\"4 / 2 is not equal to 2\")\n\t}\n}\n\nfunc Test_AdditionDecimal(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"1.2+1.3\")\n\t// Verify the value is equal to 2.5\n\tif value != \"2.5\" {\n\t\tt.Fatalf(\"1.2 + 1.3 is not equal to 2.5\")\n\t}\n}\n\nfunc Test_SubtractionDecimal(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"1.3-1.2\")\n\t// Verify the value is equal to 0.1\n\tif value != \"0.1\" {\n\t\tt.Fatalf(\"1.3 - 1.2 is not equal to 0.1\")\n\t}\n}\n\nfunc Test_MultiplicationDecimal(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"3*1.5\")\n\t// Verify the value is equal to 4.5\n\tif value != \"4.5\" {\n\t\tt.Fatalf(\"3 * 1.5 is not equal to 4.5\")\n\t}\n}\n\nfunc Test_DivisionDecimal(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"2/0.5\")\n\t// Verify the value is equal to 4\n\tif value != \"4\" {\n\t\tt.Fatalf(\"2 / 0.5 is not equal to 4\")\n\t}\n}\n\nfunc Test_BaseParenthesis(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"2*(3+4)\")\n\t// Verify the value is equal to 14\n\tif value != \"14\" {\n\t\tt.Fatalf(\"2 * (3 + 4) is not equal to 14\")\n\t}\n}\n\nfunc Test_AdvancedParenthesis(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"2*(3+4/(6-2))\")\n\t// Verify the value is equal to 8\n\tif value != \"8\" {\n\t\tt.Fatalf(\"2 * (3 + 4 / (6 - 2)) is not equal to 8\")\n\t}\n}\n\nfunc Test_Negative(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"-2+10\")\n\t// Verify the value is equal to 8\n\tif value != \"8\" {\n\t\tt.Fatalf(\"-2 + 10 is not equal to 8\")\n\t}\n}\n\nfunc Test_Negative2(t *testing.T) {\n\t// Increment the value\n\tvalue := ComputeResult(\"-2*-10\")\n\t// Verify the value is equal to 20\n\tif value != \"20\" {\n\t\tt.Fatalf(\"-2 * -10 is not equal to 20\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/miko/calculator\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"NWntT+lOFdXOY06902nZKTK0jgTQkX/WMp1pq5cUXcf25WjysLutGIA3osZWdhJM+1FtL85D7MpZUABcdU26CQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"chess","path":"gno.land/r/morgan/chess","files":[{"name":"chess.gno","body":"// Realm chess implements a Gno chess server.\npackage chess\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/morgan/chess\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// realm state\nvar (\n\t// (not \"games\" because that's too useful a variable name)\n\tgameStore     avl.Tree // string (game ID) -\u003e *Game\n\tgameIDCounter seqid.ID\n\n\t// Value must be sorted by game ID, descending\n\tuser2Games avl.Tree // address -\u003e []*Game\n)\n\n// Game represents a chess game.\ntype Game struct {\n\tID string `json:\"id\"`\n\n\tWhite    address        `json:\"white\"`\n\tBlack    address        `json:\"black\"`\n\tPosition chess.Position `json:\"position\"`\n\tState    GameState      `json:\"state\"`\n\tWinner   Winner         `json:\"winner\"`\n\n\tCreator     address   `json:\"creator\"`\n\tCreatedAt   time.Time `json:\"created_at\"`\n\tDrawOfferer *address  `json:\"draw_offerer\"` // set on draw offers\n\tConcluder   *address  `json:\"concluder\"`    // set on non-auto draws, and aborts\n\n\tTime *TimeControl `json:\"time\"`\n}\n\nfunc (g Game) json() string {\n\ts, err := g.MarshalJSON()\n\tcheckErr(err)\n\treturn string(s)\n}\n\n// Winner represents the \"direct\" outcome of a game\n// (white, black or draw?)\ntype Winner byte\n\nconst (\n\tWinnerNone Winner = iota\n\tWinnerWhite\n\tWinnerBlack\n\tWinnerDraw\n)\n\nvar winnerString = [...]string{\n\tWinnerNone:  \"none\",\n\tWinnerWhite: \"white\",\n\tWinnerBlack: \"black\",\n\tWinnerDraw:  \"draw\",\n}\n\n// GameState represents the current game state.\ntype GameState byte\n\nconst (\n\tGameStateInvalid = iota\n\n\tGameStateOpen\n\n\t// \"automatic\" endgames following moves\n\tGameStateCheckmated\n\tGameStateStalemate\n\tGameStateDrawn75Move\n\tGameStateDrawn5Fold\n\n\t// single-party draws\n\tGameStateDrawn50Move\n\tGameStateDrawn3Fold\n\tGameStateDrawnInsufficient\n\n\t// timeout by either player\n\tGameStateTimeout\n\t// aborted within first two moves\n\tGameStateAborted\n\t// resignation by either player\n\tGameStateResigned\n\t// draw by agreement\n\tGameStateDrawnByAgreement\n)\n\nvar gameStatesSnake = [...]string{\n\tGameStateInvalid:           \"invalid\",\n\tGameStateOpen:              \"open\",\n\tGameStateCheckmated:        \"checkmated\",\n\tGameStateStalemate:         \"stalemate\",\n\tGameStateDrawn75Move:       \"drawn_75_move\",\n\tGameStateDrawn5Fold:        \"drawn_5_fold\",\n\tGameStateDrawn50Move:       \"drawn_50_move\",\n\tGameStateDrawn3Fold:        \"drawn_3_fold\",\n\tGameStateDrawnInsufficient: \"drawn_insufficient\",\n\tGameStateTimeout:           \"timeout\",\n\tGameStateAborted:           \"aborted\",\n\tGameStateResigned:          \"resigned\",\n\tGameStateDrawnByAgreement:  \"drawn_by_agreement\",\n}\n\n// IsFinished returns whether the game is in a finished state.\nfunc (g GameState) IsFinished() bool {\n\treturn g != GameStateOpen\n}\n\n// NewGame initialized a new game with the given opponent.\n// opponent may be a bech32 address or \"@user\" (r/demo/users).\n//\n// seconds and increment specifies the time control for the given game.\n// seconds is the amount of time given to play to each player; increment\n// is by how many seconds the player's time should be increased when they make a move.\n// seconds \u003c= 0 means no time control (correspondence).\n//\n// XXX: Disabled for GnoChess production temporarily. (prefixed with x for unexported)\n// Ideally, we'd need this to work either by not forcing users not to have\n// parallel games OR by introducing a \"request\" system, so that a game is not\n// immediately considered \"open\" when calling NewGame.\nfunc xNewGame(cur realm, opponentRaw string, seconds, increment int) string {\n\tassertOriginCall(0, cur)\n\tif seconds \u003e= 0 \u0026\u0026 increment \u003c 0 {\n\t\tpanic(\"negative increment invalid\")\n\t}\n\n\topponent := parsePlayer(0, cur, opponentRaw)\n\tcaller := cur.Previous().Address()\n\tassertUserNotInLobby(caller)\n\n\treturn newGame(caller, opponent, seconds, increment).json()\n}\n\nfunc getUserGames(user address) []*Game {\n\tval := user2Games.Get(user.String())\n\tif val == nil {\n\t\treturn nil\n\t}\n\treturn val.([]*Game)\n}\n\nfunc assertGamesFinished(games []*Game) {\n\tfor _, g := range games {\n\t\tif g.State.IsFinished() {\n\t\t\tcontinue\n\t\t}\n\t\terr := g.claimTimeout()\n\t\tif err != nil {\n\t\t\tpanic(\"can't start new game: game \" + g.ID + \" is not yet finished\")\n\t\t}\n\t}\n}\n\nfunc newGame(caller, opponent address, seconds, increment int) *Game {\n\tgames := getUserGames(caller)\n\t// Ensure player has no ongoing games.\n\tassertGamesFinished(games)\n\tassertGamesFinished(getUserGames(opponent))\n\n\tif caller == opponent {\n\t\tpanic(\"can't create a game with yourself\")\n\t}\n\n\tisBlack := determineColor(games, caller, opponent)\n\n\t// id is zero-padded to work well with avl's alphabetic order.\n\tid := gameIDCounter.Next().String()\n\tg := \u0026Game{\n\t\tID:        id,\n\t\tWhite:     caller,\n\t\tBlack:     opponent,\n\t\tPosition:  chess.NewPosition(),\n\t\tState:     GameStateOpen,\n\t\tCreator:   caller,\n\t\tCreatedAt: time.Now(),\n\t\tTime:      NewTimeControl(seconds, increment),\n\t}\n\tif isBlack {\n\t\tg.White, g.Black = g.Black, g.White\n\t}\n\n\tgameStore.Set(g.ID, g)\n\taddToUser2Games(caller, g)\n\taddToUser2Games(opponent, g)\n\n\treturn g\n}\n\nfunc addToUser2Games(addr address, game *Game) {\n\tvar games []*Game\n\tv := user2Games.Get(string(addr))\n\tif v != nil {\n\t\tgames = v.([]*Game)\n\t}\n\t// game must be at top, because it is the latest ID\n\tgames = append([]*Game{game}, games...)\n\tuser2Games.Set(string(addr), games)\n}\n\nfunc determineColor(games []*Game, caller, opponent address) (isBlack bool) {\n\t// fast path for no games\n\tif len(games) == 0 {\n\t\treturn false\n\t}\n\n\t// Determine color of player. If the player has already played with\n\t// opponent, invert from last game played among them.\n\t// Otherwise invert from last game played by the player.\n\tisBlack = games[0].White == caller\n\n\t// \"try\" to save gas if the user has really a lot of past games\n\tif len(games) \u003e 256 {\n\t\tgames = games[:256]\n\t}\n\tfor _, game := range games {\n\t\tif game.White == opponent || game.Black == opponent {\n\t\t\treturn game.White == caller\n\t\t}\n\t}\n\treturn\n}\n\n// GetGame returns a game, knowing its ID.\nfunc GetGame(id string) string {\n\treturn getGame(id, false).json()\n}\n\nfunc getGame(id string, wantOpen bool) *Game {\n\tgraw := gameStore.Get(id)\n\tif graw == nil {\n\t\tpanic(\"game not found\")\n\t}\n\tg := graw.(*Game)\n\tif wantOpen \u0026\u0026 g.State.IsFinished() {\n\t\tpanic(\"game is already finished\")\n\t}\n\treturn g\n}\n\n// MakeMove specifies a move to be done on the given game, specifying in\n// algebraic notation the square where to move the piece.\n// If the piece is a pawn which is moving to the last row, a promotion piece\n// must be specified.\n// Castling is specified by indicating the king's movement.\nfunc MakeMove(cur realm, gameID, from, to string, promote chess.Piece) string {\n\tassertOriginCall(0, cur)\n\tg := getGame(gameID, true)\n\n\t// determine if this is a black move\n\tisBlack := len(g.Position.Moves)%2 == 1\n\n\tcaller := cur.Previous().Address()\n\tif (isBlack \u0026\u0026 g.Black != caller) ||\n\t\t(!isBlack \u0026\u0026 g.White != caller) {\n\t\t// either not a player involved; or not the caller's turn.\n\t\tpanic(\"you are not allowed to make a move at this time\")\n\t}\n\n\t// game is time controlled? add move to time control\n\tif g.Time != nil {\n\t\tvalid := g.Time.AddMove()\n\t\tif !valid \u0026\u0026 len(g.Position.Moves) \u003c 2 {\n\t\t\tg.State = GameStateAborted\n\t\t\tg.Concluder = \u0026caller\n\t\t\tg.Winner = WinnerNone\n\t\t\treturn g.json()\n\t\t}\n\t\tif !valid {\n\t\t\tg.State = GameStateTimeout\n\t\t\tif caller == g.White {\n\t\t\t\tg.Winner = WinnerBlack\n\t\t\t} else {\n\t\t\t\tg.Winner = WinnerWhite\n\t\t\t}\n\t\t\tg.saveResult()\n\t\t\treturn g.json()\n\t\t}\n\t}\n\n\t// validate move\n\tm := chess.Move{\n\t\tFrom: chess.SquareFromString(from),\n\t\tTo:   chess.SquareFromString(to),\n\t}\n\tif m.From == chess.SquareInvalid || m.To == chess.SquareInvalid {\n\t\tpanic(\"invalid from/to square\")\n\t}\n\tif promote \u003e 0 \u0026\u0026 promote \u003c= chess.PieceKing {\n\t\tm.Promotion = promote\n\t}\n\tnewp, ok := g.Position.ValidateMove(m)\n\tif !ok {\n\t\tpanic(\"illegal move\")\n\t}\n\n\t// add move and record new board\n\tg.Position = newp\n\n\to := newp.IsFinished()\n\tif o == chess.NotFinished {\n\t\t// opponent of draw offerer has made a move. take as implicit rejection of draw.\n\t\tif g.DrawOfferer != nil \u0026\u0026 *g.DrawOfferer != caller {\n\t\t\tg.DrawOfferer = nil\n\t\t}\n\n\t\treturn g.json()\n\t}\n\n\tswitch {\n\tcase o == chess.Checkmate \u0026\u0026 isBlack:\n\t\tg.State = GameStateCheckmated\n\t\tg.Winner = WinnerBlack\n\tcase o == chess.Checkmate \u0026\u0026 !isBlack:\n\t\tg.State = GameStateCheckmated\n\t\tg.Winner = WinnerWhite\n\tcase o == chess.Stalemate:\n\t\tg.State = GameStateStalemate\n\t\tg.Winner = WinnerDraw\n\n\tcase o == chess.Drawn75Move:\n\t\tg.State = GameStateDrawn75Move\n\t\tg.Winner = WinnerDraw\n\tcase o == chess.Drawn5Fold:\n\t\tg.State = GameStateDrawn5Fold\n\t\tg.Winner = WinnerDraw\n\t}\n\tg.DrawOfferer = nil\n\tg.saveResult()\n\n\treturn g.json()\n}\n\nfunc (g *Game) claimTimeout() error {\n\t// no assert origin call or caller check: anyone can claim a game to have\n\t// finished in timeout.\n\n\tif g.Time == nil {\n\t\treturn errors.New(\"game is not time controlled\")\n\t}\n\n\t// game is time controlled? add move to time control\n\tto := g.Time.TimedOut()\n\tif !to {\n\t\treturn errors.New(\"game is not timed out\")\n\t}\n\n\tif nmov := len(g.Position.Moves); nmov \u003c 2 {\n\t\tg.State = GameStateAborted\n\t\tif nmov == 1 {\n\t\t\tg.Concluder = \u0026g.Black\n\t\t} else {\n\t\t\tg.Concluder = \u0026g.White\n\t\t}\n\t\tg.Winner = WinnerNone\n\t\treturn nil\n\t}\n\n\tg.State = GameStateTimeout\n\tif len(g.Position.Moves)\u00261 == 0 {\n\t\tg.Winner = WinnerBlack\n\t} else {\n\t\tg.Winner = WinnerWhite\n\t}\n\tg.DrawOfferer = nil\n\tg.saveResult()\n\n\treturn nil\n}\n\n// ClaimTimeout should be called when the caller believes the game has resulted\n// in a timeout.\nfunc ClaimTimeout(cur realm, gameID string) string {\n\tg := getGame(gameID, true)\n\terr := g.claimTimeout()\n\tcheckErr(err)\n\n\treturn g.json()\n}\n\nfunc Abort(cur realm, gameID string) string {\n\tassertOriginCall(0, cur)\n\tg := getGame(gameID, true)\n\terr := abort(0, cur, g)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\treturn g.json()\n}\n\nfunc abort(_ int, rlm realm, g *Game) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tif len(g.Position.Moves) \u003e= 2 {\n\t\treturn errors.New(\"game can no longer be aborted; if you wish to quit, resign\")\n\t}\n\n\tcaller := rlm.Previous().Address()\n\tif caller != g.White \u0026\u0026 caller != g.Black {\n\t\treturn errors.New(\"you are not involved in this game\")\n\t}\n\tg.State = GameStateAborted\n\tg.Concluder = \u0026caller\n\tg.DrawOfferer = nil\n\tg.Winner = WinnerNone\n\n\treturn nil\n}\n\nfunc Resign(cur realm, gameID string) string {\n\tassertOriginCall(0, cur)\n\tg := getGame(gameID, true)\n\terr := resign(0, cur, g)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\treturn g.json()\n}\n\nfunc resign(_ int, rlm realm, g *Game) error {\n\tif !rlm.IsCurrent() {\n\t\treturn errors.New(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tif len(g.Position.Moves) \u003c 2 {\n\t\treturn abort(0, rlm, g)\n\t}\n\tcaller := rlm.Previous().Address()\n\tswitch caller {\n\tcase g.Black:\n\t\tg.State = GameStateResigned\n\t\tg.Winner = WinnerWhite\n\tcase g.White:\n\t\tg.State = GameStateResigned\n\t\tg.Winner = WinnerBlack\n\tdefault:\n\t\treturn errors.New(\"you are not involved in this game\")\n\t}\n\tg.DrawOfferer = nil\n\tg.saveResult()\n\n\treturn nil\n}\n\n// DrawOffer creates a draw offer in the current game, if one doesn't already\n// exist.\nfunc DrawOffer(cur realm, gameID string) string {\n\tassertOriginCall(0, cur)\n\tg := getGame(gameID, true)\n\tcaller := cur.Previous().Address()\n\n\tswitch {\n\tcase caller != g.Black \u0026\u0026 caller != g.White:\n\t\tpanic(\"you are not involved in this game\")\n\tcase g.DrawOfferer != nil:\n\t\tpanic(\"a draw offer in this game already exists\")\n\t}\n\n\tg.DrawOfferer = \u0026caller\n\treturn g.json()\n}\n\n// DrawRefuse refuse a draw offer in the given game.\nfunc DrawRefuse(cur realm, gameID string) string {\n\tassertOriginCall(0, cur)\n\tg := getGame(gameID, true)\n\tcaller := cur.Previous().Address()\n\n\tswitch {\n\tcase caller != g.Black \u0026\u0026 caller != g.White:\n\t\tpanic(\"you are not involved in this game\")\n\tcase g.DrawOfferer == nil:\n\t\tpanic(\"no draw offer present\")\n\tcase *g.DrawOfferer == caller:\n\t\tpanic(\"can't refuse an offer you sent yourself\")\n\t}\n\n\tg.DrawOfferer = nil\n\treturn g.json()\n}\n\n// Draw implements draw by agreement, as well as \"single-party\" draw:\n// - Threefold repetition (§9.2)\n// - Fifty-move rule (§9.3)\n// - Insufficient material (§9.4)\n// Note: stalemate happens as a consequence of a Move, and thus is handled in that function.\nfunc Draw(cur realm, gameID string) string {\n\tassertOriginCall(0, cur)\n\tg := getGame(gameID, true)\n\n\tcaller := cur.Previous().Address()\n\tif caller != g.Black \u0026\u0026 caller != g.White {\n\t\tpanic(\"you are not involved in this game\")\n\t}\n\n\t// accepted draw offer (do early to avoid gas for g.Position.IsFinished())\n\tif g.DrawOfferer != nil \u0026\u0026 *g.DrawOfferer != caller {\n\t\tg.State = GameStateDrawnByAgreement\n\t\tg.Winner = WinnerDraw\n\t\tg.Concluder = \u0026caller\n\n\t\tg.saveResult()\n\n\t\treturn g.json()\n\t}\n\n\to := g.Position.IsFinished()\n\tswitch {\n\tcase o\u0026chess.Can50Move != 0:\n\t\tg.State = GameStateDrawn50Move\n\tcase o\u0026chess.Can3Fold != 0:\n\t\tg.State = GameStateDrawn3Fold\n\tcase o\u0026chess.CanInsufficient != 0:\n\t\tg.State = GameStateDrawnInsufficient\n\tdefault:\n\t\tpanic(\"this game can't be automatically drawn\")\n\t}\n\tg.Concluder = \u0026caller\n\tg.Winner = WinnerDraw\n\tg.DrawOfferer = nil\n\n\tg.saveResult()\n\n\treturn g.json()\n}\n\nfunc checkErr(err error) {\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n}\n\n// assertOriginCall is a user-call guard — it accepts EOA maketx-call AND\n// maketx-run ephemeral realms. Despite the name, this is NOT equivalent to\n// runtime.AssertOriginCall() (which is strictly direct MsgCall only).\n//\n// WARNING: do not reuse this helper from any function that reads\n// unsafe.OriginSend() to verify payment. IsUser() lets a maketx-run script\n// consume the origin-send envelope before calling us, bypassing the check.\n// For payment paths, use IsUserCall() (EOA only) — see\n// docs/resources/effective-gno.md#verifying-inbound-coin-payments.\nfunc assertOriginCall(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tif !rlm.Previous().IsUser() {\n\t\tpanic(\"invalid non-origin call\")\n\t}\n}\n"},{"name":"chess_test.gno","body":"package chess\n\nimport (\n\t\"chain\"\n\t\"fmt\"\n\t\"os\"\n\t\"strconv\"\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/morgan/chess\"\n\t\"gno.land/p/morgan/chess/glicko2\"\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nfunc cleanup() {\n\tgameStore = avl.Tree{}\n\tgameIDCounter = 0\n\tuser2Games = avl.Tree{}\n\tplayerStore = avl.Tree{}\n\tleaderboard = [CategoryMax]leaderboardType{}\n\tlobby = [tcLobbyMax][]lobbyPlayer{}\n\tlobbyPlayer2Game = avl.Tree{}\n\tplayerRatings = [CategoryMax][]*glicko2.PlayerRating{}\n}\n\nfunc TestNewGame(cur realm, t *testing.T) {\n\tcleanup()\n\n\tg := xNewGame(cur, chain.PackageAddress(\"xx\").String(), 0, 0)\n\tprintln(g)\n}\n\nconst (\n\twhite address = \"g1white\"\n\tblack address = \"g1black\"\n)\n\n/*\nsyntax:\n\n\t[\u003ccommand\u003e ][#[!][\u003cbuf\u003e] \u003cchecker\u003e]\n\ncommand is executed; result of command is stored in buffer.\nthe test is split in lines. other white space is ignored (strings.Fields).\n\n\u003cbuf\u003e: all commands below will generally store a string result value in the\nbuffer \"result\", which is the default and thus may be omitted.\nif the command panics, the panic value is stored in the buffer \"panic\".\n(if it doesn't, buffer panic is set to an empty string).\nif following a command there is no checker on the #panic buffer, the line\n\"#!panic empty\" is implicitly added.\nif \u003cbuf\u003e is preceded by ! (e.g. \"#!panic empty\"), then if the checker fails,\nprocessing is stopped on that line.\n\n\u003ccommand\u003e:\n\n\tnewgame [\u003cwhite\u003e \u003cblack\u003e [\u003cseconds\u003e [\u003cincrement\u003e]]]\n\t\tstores game ID in buffer #id.\n\t\t\u003cwhite\u003e and \u003cblack\u003e are two addresses. if they are not passed, \u003cwhite\u003e\n\t\tassumes value \"white\" and \u003cblack\u003e \"black\"\n\tmove \u003cplayer\u003e \u003clan_move\u003e\n\t\tlan_move is in the same format as Move.String.\n\t\tretrieves game id from #id.\n\tdraw \u003cplayer\u003e\n\tdrawoffer \u003cplayer\u003e\n\tabort \u003cplayer\u003e\n\ttimeout \u003cplayer\u003e\n\t\t(ClaimTimeout)\n\tresign \u003cplayer\u003e\n\tgame [\u003cid\u003e]\n\t\tif not given, id is retrieved from buffer #id.\n\tplayer \u003cplayer\u003e\n\tname \u003cpredicate\u003e\n\t\tsets the name of the test to predicate.\n\tcopy \u003cdst\u003e [\u003csrc\u003e]\n\t\tcopies buffer src to buffer dst.\n\t\tif src not specified, assumed result.\n\t\t(don't specify the #; ie: copy oldresult result)\n\tsleep \u003cseconds\u003e\n\t\tsleep for the given amount of seconds (float).\n\nNOTE: for all values of \u003cplayer\u003e, including \u003cwhite\u003e and \u003cblack\u003e in newgame,\nthe addresses are passed prefixed by \"g1\", and the matching checkers should\nexpect this.\n\n\u003cchecker\u003e:\n\n\tempty\n\t\tthe buffer should be empty.\n\tequal \u003cpredicate\u003e\n\t\tpredicate may start with #, which indicates a buffer.\n\tcontains [\u003cpredicate\u003e...]\n\t\tthe buffer should contain all of the given predicates.\n\tcontainssp \u003cpredicate\u003e\n\t\tthe buffer should contain the given predicate, which contains spaces.\n*/\nvar commandTests = [...]string{\n\t`\tname NewGameNegativeIncrement\n\t\tnewgame white black 10 -5 #panic containssp negative increment invalid\n\t`,\n\t`\tname NewGameDouble\n\t\tnewgame\n\t\tnewgame #panic contains is not yet finished\n\t`,\n\t`\tname NewGameWithSelf\n\t\tnewgame white white #panic contains game with yourself\n\t`,\n\t// ColoursInvert within games played by two players\n\t`\tname ColoursInvert\n\t\tnewgame\n\t\tmove white e2e4\n\t\tmove black e7e5\n\t\tmove white f1c4\n\t\tresign white\n\t\tnewgame\n\t\t# contains \"white\":\"g1black\" \"black\":\"g1white\"\n\t\t#id equal 0000002\n\t`,\n\t// Otherwise, invert from p1's history.\n\t`\tname ColoursInvert3p\n\t\tnewgame p1 p2 #! contains \"white\":\"g1p1\" \"black\":\"g1p2\"\n\t\tmove p1 e2e4\n\t\tabort p1\n\t\tnewgame p1 p3 # contains \"white\":\"g1p3\" \"black\":\"g1p1\"\n\t`,\n\t`\tname ScholarsMate\n\t\tnewgame #id equal 0000001\n\t\tmove white e2e4\n\t\tmove black e7e5\n\t\tmove white f1c4\n\t\tmove black b8c6\n\t\tmove white d1f3\n\t\tmove black d7d6\n\t\tmove white f3f7\n\t\tcopy moveres\n\t\tgame # equal #moveres\n\t\t# contains \"state\":\"checkmated\" \"winner\":\"white\"\n\t\t# containssp r1bqkbnr/ppp2Qpp/2np4/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 4\n\t\tplayer white\n\t\t# contains \"address\":\"g1white\" \"position\":0 \"wins\":1 \"losses\":0 \"draws\":0\n\t\tplayer black\n\t\t# contains \"address\":\"g1black\" \"position\":1 \"wins\":0 \"losses\":1 \"draws\":0\n\t`,\n\t`\tname DrawByAgreement\n\t\tnewgame\n\t\tmove white e2e4\n\t\tmove black e7e5\n\t\tmove white f1c4\n\t\tmove black b8c6\n\t\tcopy moveres\n\t\tgame # equal #moveres\n\t\t# contains \"open\" \"concluder\":null \"draw_offerer\":null\n\t\tdrawoffer white\n\t\t# contains \"open\" \"concluder\":null \"draw_offerer\":\"g1white\"\n\t\tdraw black\n\t\t# contains \"drawn_by_agreement\" \"concluder\":\"g1black\" \"draw_offerer\":\"g1white\"\n\t`,\n\t`\tname AbortFirstMove\n\t\tnewgame\n\t\tabort white # contains \"winner\":\"none\" \"concluder\":\"g1white\"\n\t`,\n\n\t`\tname ThreefoldRepetition\n\t\tnewgame\n\n\t\tmove white g1f3\n\t\tmove black g8f6\n\t\tmove white f3g1\n\t\tmove black f6g8\n\n\t\tmove white g1f3\n\t\tmove black g8f6\n\t\tmove white f3g1\n\t\tmove black f6g8\n\n\t\tdraw black # contains \"winner\":\"draw\" \"concluder\":\"g1black\"\n\t\t# contains \"state\":\"drawn_3_fold\"\n\t`,\n\t`\tname FivefoldRepetition\n\t\tnewgame\n\n\t\tmove white g1f3\n\t\tmove black g8f6\n\t\tmove white f3g1\n\t\tmove black f6g8\n\n\t\tmove white g1f3\n\t\tmove black g8f6\n\t\tmove white f3g1\n\t\tmove black f6g8\n\n\t\tmove white g1f3\n\t\tmove black g8f6\n\t\tmove white f3g1\n\t\tmove black f6g8\n\n\t\tmove white g1f3\n\t\tmove black g8f6\n\t\tmove white f3g1\n\t\tmove black f6g8\n\n\t\t# contains \"winner\":\"draw\" \"concluder\":null \"state\":\"drawn_5_fold\"\n\n\t\tmove white g1f3 #panic contains game is already finished\n\t`,\n\t`\tname TimeoutAborted\n\t\tnewgame white black 3\n\t\tmove white e2e4 #! contains \"state\":\"open\"\n\t\tsleep 31\n\t\tmove black e7e5\n\t\tgame\n\t\t# contains e2e4\n\t\t# contains \"aborted\"\n\t\t# contains \"concluder\":\"g1black\"\n\t`,\n\t`\tname TimeoutAbandoned\n\t\tnewgame white black 1\n\t\tmove white e2e4\n\t\tmove black e7e5\n\t\tsleep 61\n\t\ttimeout black\n\t\t# contains \"state\":\"timeout\" \"winner\":\"black\"\n\t`,\n}\n\nfunc TestCommands(cur realm, t *testing.T) {\n\tfor _, command := range commandTests {\n\t\trunCommandTest(cur, t, command)\n\t}\n}\n\n// testCommandRunner is used to represent the single testCommand types below.\n// the Run function is used to execute the actual command, after parsing.\n//\n// This could have been implemented with simple closures generated within the\n// parser, however it's not because of this issue:\n// https://github.com/gnolang/gno/issues/1135\ntype testCommandRunner interface {\n\tRun(cur realm, t *testing.T, bufs map[string]string)\n}\n\n// testCommandChecker is a wrapper for a runner which is performing a check.\n// This is marked in order not to wrap the calls to them as a panic.\ntype testCommandChecker struct{ testCommandRunner }\n\nfunc (testCommandChecker) Checker() {}\n\ntype testCommandFunc func(cur realm, t *testing.T, bufs map[string]string)\n\nfunc (tc testCommandFunc) Run(cur realm, t *testing.T, bufs map[string]string) {\n\ttc(cross(cur), t, bufs)\n}\n\n// testCommandColorID represents a testCommand, which uses a function of the\n// form func(gameID string) string (hence ID), and that takes as the first\n// parameter a \u003cplayer\u003e which will be the caller.\ntype testCommandColorID struct {\n\tfn   func(realm, string) string\n\taddr address\n}\n\nfunc newTestCommandColorID(fn func(realm, string) string, s string, addr string) testCommandRunner {\n\treturn \u0026testCommandColorID{fn, address(\"g1\" + addr)}\n}\n\nfunc (tc *testCommandColorID) Run(cur realm, t *testing.T, bufs map[string]string) {\n\ttesting.SetRealm(testing.NewUserRealm(tc.addr))\n\tbufs[\"result\"] = tc.fn(cross(cur), bufs[\"id\"])\n}\n\ntype testCommandNewGame struct {\n\tw, b          address\n\tseconds, incr int\n}\n\nfunc (tc *testCommandNewGame) Run(cur realm, t *testing.T, bufs map[string]string) {\n\ttesting.SetRealm(testing.NewUserRealm(tc.w))\n\tres := xNewGame(cross(cur), string(tc.b), tc.seconds, tc.incr)\n\tbufs[\"result\"] = res\n\n\tconst idMagicString = `\"id\":\"`\n\tidx := strings.Index(res, idMagicString)\n\tif idx \u003c 0 {\n\t\tpanic(\"id not found\")\n\t}\n\tid := res[idx+len(idMagicString):]\n\tid = id[:strings.IndexByte(id, '\"')]\n\tbufs[\"id\"] = id\n}\n\ntype testCommandMove struct {\n\taddr      address\n\tfrom, to  string\n\tpromotion chess.Piece\n}\n\nfunc (tc *testCommandMove) Run(cur realm, t *testing.T, bufs map[string]string) {\n\ttesting.SetRealm(testing.NewUserRealm(tc.addr))\n\tbufs[\"result\"] = MakeMove(cross(cur), bufs[\"id\"], tc.from, tc.to, tc.promotion)\n}\n\ntype testCommandGame struct {\n\tidWanted string\n}\n\nfunc (tc *testCommandGame) Run(cur realm, t *testing.T, bufs map[string]string) {\n\tidl := tc.idWanted\n\tif idl == \"\" {\n\t\tidl = bufs[\"id\"]\n\t}\n\tbufs[\"result\"] = GetGame(idl)\n}\n\ntype testCommandPlayer struct {\n\taddr string\n}\n\nfunc (tc *testCommandPlayer) Run(cur realm, t *testing.T, bufs map[string]string) {\n\tbufs[\"result\"] = GetPlayer(cross(cur), tc.addr)\n}\n\ntype testCommandCopy struct {\n\tdst, src string\n}\n\nfunc (tc *testCommandCopy) Run(cur realm, t *testing.T, bufs map[string]string) {\n\tbufs[tc.dst] = bufs[tc.src]\n}\n\ntype testCommandSleep struct {\n\tdur time.Duration\n}\n\nfunc (tc *testCommandSleep) Run(cur realm, t *testing.T, bufs map[string]string) {\n\tos.Sleep(tc.dur)\n}\n\ntype testChecker struct {\n\tfn    func(t *testing.T, bufs map[string]string, tc *testChecker)\n\ttf    func(*testing.T, string, ...interface{})\n\tbufp  string\n\tpreds []string\n}\n\nfunc (*testChecker) Checker() {}\nfunc (tc *testChecker) Run(cur realm, t *testing.T, bufs map[string]string) {\n\ttc.fn(t, bufs, tc)\n}\n\nfunc parseCommandTest(t *testing.T, command string) (funcs []testCommandRunner, testName string) {\n\tlines := strings.Split(command, \"\\n\")\n\tatoi := func(s string) int {\n\t\tn, err := strconv.Atoi(s)\n\t\tcheckErr(err)\n\t\treturn n\n\t}\n\t// used to detect whether to auto-add a panic checker\n\tvar hasPanicChecker bool\n\tpanicChecker := func(lineNum int, testName string) testCommandRunner {\n\t\treturn testCommandChecker{testCommandFunc(\n\t\t\tfunc(cur realm, t *testing.T, bufs map[string]string) {\n\t\t\t\tif bufs[\"panic\"] != \"\" {\n\t\t\t\t\tt.Fatalf(\"%s:%d: buffer \\\"panic\\\" is not empty (%q)\", testName, lineNum, bufs[\"panic\"])\n\t\t\t\t}\n\t\t\t},\n\t\t)}\n\t}\n\n\tfor lineNum, line := range lines {\n\t\tflds := strings.Fields(line)\n\t\tif len(flds) == 0 {\n\t\t\tcontinue\n\t\t}\n\t\tcommand, checker := flds, ([]string)(nil)\n\t\tfor idx, fld := range flds {\n\t\t\tif strings.HasPrefix(fld, \"#\") {\n\t\t\t\tcommand, checker = flds[:idx], flds[idx:]\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tvar cmd string\n\t\tif len(command) \u003e 0 {\n\t\t\tcmd = command[0]\n\n\t\t\t// there is a new command; if hasPanicChecker == false,\n\t\t\t// it means the previous command did not have a panic checker.\n\t\t\t// add it.\n\t\t\tif !hasPanicChecker \u0026\u0026 len(funcs) \u003e 0 {\n\t\t\t\t// no lineNum+1 because it was the previous line\n\t\t\t\tfuncs = append(funcs, panicChecker(lineNum, testName))\n\t\t\t}\n\t\t}\n\t\tswitch cmd {\n\t\tcase \"\": // move on\n\t\tcase \"newgame\":\n\t\t\tw, b := white, black\n\t\t\tvar seconds, incr int\n\t\t\tswitch len(command) {\n\t\t\tcase 1:\n\t\t\tcase 5:\n\t\t\t\tincr = atoi(command[4])\n\t\t\t\tfallthrough\n\t\t\tcase 4:\n\t\t\t\tseconds = atoi(command[3])\n\t\t\t\tfallthrough\n\t\t\tcase 3:\n\t\t\t\tw, b = address(\"g1\"+command[1]), address(\"g1\"+command[2])\n\t\t\tdefault:\n\t\t\t\tpanic(\"invalid newgame command \" + line)\n\t\t\t}\n\t\t\tfuncs = append(funcs,\n\t\t\t\t\u0026testCommandNewGame{w, b, seconds, incr},\n\t\t\t)\n\t\tcase \"move\":\n\t\t\tif len(command) != 3 {\n\t\t\t\tpanic(\"invalid move command \" + line)\n\t\t\t}\n\t\t\tif len(command[2]) \u003c 4 || len(command[2]) \u003e 5 {\n\t\t\t\tpanic(\"invalid lan move \" + command[2])\n\t\t\t}\n\t\t\tfrom, to := command[2][:2], command[2][2:4]\n\t\t\tvar promotion chess.Piece\n\t\t\tif len(command[2]) == 5 {\n\t\t\t\tpromotion = chess.PieceFromChar(command[2][4])\n\t\t\t\tif promotion == chess.PieceEmpty {\n\t\t\t\t\tpanic(\"invalid piece for promotion: \" + string(command[2][4]))\n\t\t\t\t}\n\t\t\t}\n\t\t\tfuncs = append(funcs, \u0026testCommandMove{\n\t\t\t\taddr:      address(\"g1\" + command[1]),\n\t\t\t\tfrom:      from,\n\t\t\t\tto:        to,\n\t\t\t\tpromotion: promotion,\n\t\t\t})\n\t\tcase \"abort\":\n\t\t\tfuncs = append(funcs, newTestCommandColorID(Abort, \"abort\", command[1]))\n\t\tcase \"draw\":\n\t\t\tfuncs = append(funcs, newTestCommandColorID(Draw, \"draw\", command[1]))\n\t\tcase \"drawoffer\":\n\t\t\tfuncs = append(funcs, newTestCommandColorID(DrawOffer, \"drawoffer\", command[1]))\n\t\tcase \"timeout\":\n\t\t\tfuncs = append(funcs, newTestCommandColorID(ClaimTimeout, \"timeout\", command[1]))\n\t\tcase \"resign\":\n\t\t\tfuncs = append(funcs, newTestCommandColorID(Resign, \"resign\", command[1]))\n\t\tcase \"game\":\n\t\t\tif len(command) \u003e 2 {\n\t\t\t\tpanic(\"invalid game command \" + line)\n\t\t\t}\n\t\t\ttc := \u0026testCommandGame{}\n\t\t\tif len(command) == 2 {\n\t\t\t\ttc.idWanted = command[1]\n\t\t\t}\n\t\t\tfuncs = append(funcs, tc)\n\t\tcase \"player\":\n\t\t\tif len(command) != 2 {\n\t\t\t\tpanic(\"invalid player command \" + line)\n\t\t\t}\n\t\t\tfuncs = append(funcs, \u0026testCommandPlayer{\"g1\" + command[1]})\n\t\tcase \"name\":\n\t\t\ttestName = strings.Join(command[1:], \" \")\n\t\tcase \"copy\":\n\t\t\tif len(command) \u003e 3 || len(command) \u003c 2 {\n\t\t\t\tpanic(\"invalid copy command \" + line)\n\t\t\t}\n\t\t\ttc := \u0026testCommandCopy{dst: command[1], src: \"result\"}\n\t\t\tif len(command) == 3 {\n\t\t\t\ttc.src = command[2]\n\t\t\t}\n\t\t\tfuncs = append(funcs, tc)\n\t\tcase \"sleep\":\n\t\t\tif len(command) != 2 {\n\t\t\t\tpanic(\"invalid sleep command \" + line)\n\t\t\t}\n\t\t\tfuncs = append(funcs, \u0026testCommandSleep{\n\t\t\t\ttime.Duration(atoi(command[1])) * time.Second,\n\t\t\t})\n\t\tdefault:\n\t\t\tpanic(\"invalid command \" + cmd)\n\t\t}\n\n\t\tif len(checker) == 0 {\n\t\t\tcontinue\n\t\t}\n\t\tif len(checker) == 1 {\n\t\t\tpanic(\"no checker specified \" + line)\n\t\t}\n\n\t\tbufp := checker[0]\n\t\tuseFatal := false\n\t\tif len(bufp) \u003e 1 \u0026\u0026 bufp[1] == '!' {\n\t\t\tbufp = bufp[2:]\n\t\t\tuseFatal = true\n\t\t} else {\n\t\t\tbufp = bufp[1:]\n\t\t}\n\t\tif bufp == \"\" {\n\t\t\tbufp = \"result\"\n\t\t}\n\t\tif bufp == \"panic\" \u0026\u0026 !hasPanicChecker {\n\t\t\thasPanicChecker = true\n\t\t}\n\t\ttf := func(ln int, testName string, useFatal bool) func(*testing.T, string, ...interface{}) {\n\t\t\treturn func(t *testing.T, s string, v ...interface{}) {\n\t\t\t\tfn := t.Errorf\n\t\t\t\tif useFatal {\n\t\t\t\t\tfn = t.Fatalf\n\t\t\t\t}\n\t\t\t\tfn(\"%s:%d: \"+s, append([]interface{}{testName, ln}, v...)...)\n\t\t\t}\n\t\t}(lineNum+1, testName, useFatal)\n\n\t\tswitch checker[1] {\n\t\tcase \"empty\":\n\t\t\tif len(checker) != 2 {\n\t\t\t\tpanic(\"invalid empty checker \" + line)\n\t\t\t}\n\t\t\tfuncs = append(funcs, \u0026testChecker{\n\t\t\t\tfn: func(t *testing.T, bufs map[string]string, tc *testChecker) {\n\t\t\t\t\tif bufs[tc.bufp] != \"\" {\n\t\t\t\t\t\ttc.tf(t, \"buffer %q is not empty (%v)\", tc.bufp, bufs[tc.bufp])\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\ttf:   tf,\n\t\t\t\tbufp: bufp,\n\t\t\t})\n\t\tcase \"equal\":\n\t\t\tpred := strings.Join(checker[2:], \" \")\n\t\t\tfuncs = append(funcs, \u0026testChecker{\n\t\t\t\tfn: func(t *testing.T, bufs map[string]string, tc *testChecker) {\n\t\t\t\t\texp := tc.preds[0]\n\t\t\t\t\tif exp[0] == '#' {\n\t\t\t\t\t\texp = bufs[exp[1:]]\n\t\t\t\t\t}\n\t\t\t\t\tif bufs[tc.bufp] != exp {\n\t\t\t\t\t\ttc.tf(t, \"buffer %q: want %v got %v\", tc.bufp, exp, bufs[tc.bufp])\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\ttf:    tf,\n\t\t\t\tbufp:  bufp,\n\t\t\t\tpreds: []string{pred},\n\t\t\t})\n\t\tcase \"contains\":\n\t\t\tpreds := checker[2:]\n\t\t\tif len(preds) == 0 {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tfuncs = append(funcs, \u0026testChecker{\n\t\t\t\tfn: func(t *testing.T, bufs map[string]string, tc *testChecker) {\n\t\t\t\t\tfor _, pred := range tc.preds {\n\t\t\t\t\t\tif !strings.Contains(bufs[tc.bufp], pred) {\n\t\t\t\t\t\t\ttc.tf(t, \"buffer %q: %v does not contain %v\", tc.bufp, bufs[tc.bufp], pred)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\ttf:    tf,\n\t\t\t\tbufp:  bufp,\n\t\t\t\tpreds: preds,\n\t\t\t})\n\t\tcase \"containssp\":\n\t\t\tpred := strings.Join(checker[2:], \" \")\n\t\t\tif pred == \"\" {\n\t\t\t\tpanic(\"invalid contanssp checker \" + line)\n\t\t\t}\n\t\t\tfuncs = append(funcs, \u0026testChecker{\n\t\t\t\tfn: func(t *testing.T, bufs map[string]string, tc *testChecker) {\n\t\t\t\t\tif !strings.Contains(bufs[tc.bufp], tc.preds[0]) {\n\t\t\t\t\t\ttc.tf(t, \"buffer %q: %v does not contain %v\", tc.bufp, bufs[tc.bufp], tc.preds[0])\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\ttf:    tf,\n\t\t\t\tbufp:  bufp,\n\t\t\t\tpreds: []string{pred},\n\t\t\t})\n\t\tdefault:\n\t\t\tpanic(\"invalid checker \" + checker[1])\n\t\t}\n\t}\n\tif !hasPanicChecker {\n\t\tfuncs = append(funcs, panicChecker(len(lines), testName))\n\t}\n\treturn\n}\n\nfunc runCommandTest(cur realm, t *testing.T, command string) {\n\tfuncs, testName := parseCommandTest(t, command)\n\n\tt.Run(testName, func(cur realm, t *testing.T) {\n\t\tcleanup()\n\t\tbufs := make(map[string]string, 3)\n\t\tfor _, f := range funcs {\n\t\t\tif _, ok := f.(interface{ Checker() }); ok {\n\t\t\t\tf.Run(cross(cur), t, bufs)\n\t\t\t} else {\n\t\t\t\tcatchPanic(cur, f, t, bufs)\n\t\t\t}\n\t\t}\n\t})\n}\n\nfunc catchPanic(cur realm, tc testCommandRunner, t *testing.T, bufs map[string]string) {\n\t// XXX: should prefer testing.Recover, but see: https://github.com/gnolang/gno/issues/1650\n\te := revive(func() { tc.Run(cross(cur), t, bufs) })\n\tif e == nil {\n\t\tbufs[\"panic\"] = \"\"\n\t\treturn\n\t}\n\tbufs[\"result\"] = \"\"\n\tbufs[\"panic\"] = fmt.Sprint(e)\n}\n"},{"name":"discovery.gno","body":"// this file concerns mostly with \"discovery\"; ie. finding information\n// about a user's chess playing and previous games\n\npackage chess\n\nimport (\n\t\"bytes\"\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"gno.land/p/morgan/chess/glicko2\"\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/r/sys/users\"\n)\n\ntype Category byte\n\nconst (\n\t// blanks are reserved for future use (bullet and classic)\n\t_ Category = iota\n\tBlitz\n\tRapid\n\t_\n\tCorrespondence\n\tCategoryMax\n)\n\nvar categoryString = [CategoryMax]string{\n\tBlitz:          \"blitz\",\n\tRapid:          \"rapid\",\n\tCorrespondence: \"correspondence\",\n}\n\nvar categoryList = [...]Category{Blitz, Rapid, Correspondence}\n\nfunc (c Category) String() string {\n\tif c \u003e CategoryMax || categoryString[c] == \"\" {\n\t\tpanic(\"invalid category\")\n\t}\n\treturn categoryString[c]\n}\n\nfunc CategoryFromString(s string) Category {\n\tfor i, cs := range categoryString {\n\t\tif s == cs {\n\t\t\treturn Category(i)\n\t\t}\n\t}\n\tpanic(\"invalid category\")\n}\n\nfunc (tc *TimeControl) Category() Category {\n\t// https://lichess.org/faq#time-controls\n\tif tc == nil {\n\t\treturn Correspondence\n\t}\n\n\ttotalTime := tc.Seconds + tc.Increment*40\n\tswitch {\n\tcase tc.Seconds \u003c= 0 || tc.Increment \u003c 0:\n\t\t// should not happen\n\t\treturn Correspondence\n\tcase totalTime \u003c 60*8:\n\t\treturn Blitz\n\tdefault:\n\t\treturn Rapid\n\t}\n}\n\n// realm state\nvar (\n\tplayerStore   avl.Tree // address -\u003e *Player\n\tleaderboard   [CategoryMax]leaderboardType\n\tplayerRatings [CategoryMax][]*glicko2.PlayerRating\n)\n\nfunc GetPlayer(cur realm, player string) string {\n\taddr := parsePlayer(0, cur, player)\n\tv := playerStore.Get(addr.String())\n\tif v == nil {\n\t\tpanic(\"player not found\")\n\t}\n\tb, err := v.(*Player).MarshalJSON()\n\tcheckErr(err)\n\treturn string(b)\n}\n\n// Player contains game-related player information.\ntype Player struct {\n\tAddress      address\n\tCategoryInfo [CategoryMax]CategoryInfo\n}\n\ntype CategoryInfo struct {\n\tWins, Losses, Draws int\n\t*glicko2.PlayerRating\n}\n\n// Score for determining leaderboards.\nfunc (p Player) Score(cat Category) float64 {\n\treturn p.CategoryInfo[cat].Rating\n}\n\n// Leaderboard position, 0 indexed.\n// Dynamically calculated to avoid having to shift positions when LB changes.\nfunc (p Player) LeaderboardPosition(cat Category) int {\n\tpos, ok := leaderboard[cat].find(p.Score(cat), p.Address)\n\tif !ok {\n\t\treturn -1\n\t}\n\treturn pos\n}\n\nfunc (g *Game) saveResult() {\n\tw, b := getPlayer(g.White), getPlayer(g.Black)\n\n\tcat := g.Time.Category()\n\n\t// Get numeric result for glicko2.\n\tvar result float64\n\tswitch g.Winner {\n\tcase WinnerWhite:\n\t\tw.CategoryInfo[cat].Wins++\n\t\tb.CategoryInfo[cat].Losses++\n\t\tresult = 1\n\tcase WinnerBlack:\n\t\tw.CategoryInfo[cat].Losses++\n\t\tb.CategoryInfo[cat].Wins++\n\t\tresult = 0\n\tcase WinnerDraw:\n\t\tw.CategoryInfo[cat].Draws++\n\t\tb.CategoryInfo[cat].Draws++\n\t\tresult = 0.5\n\tdefault:\n\t\treturn // TODO: maybe panic\n\t}\n\n\t// Call glicko 2 rating calculator.\n\towr, obr := w.CategoryInfo[cat].Rating, b.CategoryInfo[cat].Rating\n\tglicko2.UpdateRatings(playerRatings[cat], []glicko2.RatingScore{{\n\t\tWhite: g.White,\n\t\tBlack: g.Black,\n\t\tScore: result,\n\t}})\n\n\t// Save in playerStore.\n\tplayerStore.Set(w.Address.String(), w)\n\tplayerStore.Set(b.Address.String(), b)\n\tleaderboard[cat], _ = leaderboard[cat].push(g.White, owr, w.CategoryInfo[cat].Rating)\n\tleaderboard[cat], _ = leaderboard[cat].push(g.Black, obr, b.CategoryInfo[cat].Rating)\n}\n\nfunc getPlayer(addr address) *Player {\n\tpraw := playerStore.Get(addr.String())\n\tif praw != nil {\n\t\treturn praw.(*Player)\n\t}\n\tp := new(Player)\n\tp.Address = addr\n\tfor _, cat := range categoryList {\n\t\tpr := glicko2.NewPlayerRating(addr)\n\t\tp.CategoryInfo[cat] = CategoryInfo{\n\t\t\tPlayerRating: pr,\n\t\t}\n\t\tplayerRatings[cat] = append(playerRatings[cat], pr)\n\t}\n\tplayerStore.Set(addr.String(), p)\n\treturn p\n}\n\ntype lbEntry struct {\n\taddr  address\n\tscore float64\n}\n\ntype leaderboardType []lbEntry\n\n// find performs binary search on leaderboard to find the first\n// position where score appears, or anything lesser than it.\n// Additionally, if addr is given, it finds the position where the given address appears.\n// The second return parameter returns whether the address was found.\n//\n// The index will be 0 if the score is higher than any other on the leaderboard,\n// and len(leaderboards) if it is lower than any other.\nfunc (lb leaderboardType) find(score float64, addr address) (int, bool) {\n\ti := sort.Search(len(lb), func(i int) bool {\n\t\treturn lb[i].score \u003c= score\n\t})\n\n\tif addr == \"\" || i == len(lb) {\n\t\treturn i, false\n\t}\n\n\tfor j := 0; i+j \u003c len(lb) \u0026\u0026 lb[i+j].score == score; j++ {\n\t\tif lb[i+j].addr == addr {\n\t\t\treturn i + j, true\n\t\t}\n\t}\n\n\treturn i, false\n}\n\n// push adds or modifies the player's position in the leaderboard.\n// the new leaderboard, and the new position of the player in the leaderboard is returned (0-indexed)\nfunc (lb leaderboardType) push(player address, oldScore, newScore float64) (leaderboardType, int) {\n\t// determine where the player is, currently\n\toldPos, found := lb.find(oldScore, player)\n\tif found \u0026\u0026 (oldScore == newScore) {\n\t\treturn lb, oldPos\n\t}\n\n\t// determine where to place the player next.\n\tnewPos, _ := lb.find(newScore, \"\")\n\n\tvar n leaderboardType\n\tswitch {\n\tcase !found:\n\t\tn = append(leaderboardType{}, lb[:newPos]...)\n\t\tn = append(n, lbEntry{player, newScore})\n\t\tn = append(n, lb[newPos:]...)\n\n\tcase oldPos == newPos:\n\t\tn = lb\n\t\tn[newPos] = lbEntry{player, newScore}\n\tcase oldPos \u003e newPos:\n\t\tn = append(leaderboardType{}, lb[:newPos]...)\n\t\tn = append(n, lbEntry{player, newScore})\n\t\tn = append(n, lb[newPos:oldPos]...)\n\t\tn = append(n, lb[oldPos+1:]...)\n\tdefault: // oldPos \u003c newPos\n\t\tn = append(leaderboardType{}, lb[:oldPos]...)\n\t\tn = append(n, lb[oldPos+1:newPos]...)\n\t\tn = append(n, lbEntry{player, newScore})\n\t\tn = append(n, lb[newPos:]...)\n\t}\n\treturn n, newPos\n}\n\n// Leaderboard returns a list of all users, ordered by their position in the leaderboard.\n// category is one of blitz, rapid or correspondence.\nfunc Leaderboard(category string) string {\n\tcat := CategoryFromString(category)\n\tvar buf bytes.Buffer\n\tbuf.WriteByte('[')\n\tfor idx, entry := range leaderboard[cat] {\n\t\tp := playerStore.Get(entry.addr.String())\n\t\td, err := p.(*Player).MarshalJSON()\n\t\tcheckErr(err)\n\t\tbuf.Write(d)\n\t\tif idx != len(leaderboard[cat])-1 {\n\t\t\tbuf.WriteByte(',')\n\t\t}\n\t}\n\tbuf.WriteByte(']')\n\treturn buf.String()\n}\n\n// ListGames provides game listing functionality, with filter-based search functionality.\n//\n// available filters:\n//\n//\tplayer:\u003cplayer\u003e white:\u003cplayer\u003e black:\u003cplayer\u003e finished:bool\n//\tlimit:int id\u003ccmp\u003eint sort:asc/desc\n//\t\u003ccmp\u003e: '\u003c' or '\u003e'\n//\t\u003cplayer\u003e: either a bech32 address, \"@user\" (r/demo/users), or \"caller\"\nfunc ListGames(cur realm, filters string) string {\n\tft := parseFilters(0, cur, filters)\n\tresults := make([]*Game, 0, ft.limit)\n\tcb := func(g *Game) (stop bool) {\n\t\tif !ft.valid(g) {\n\t\t\treturn false\n\t\t}\n\t\tresults = append(results, g)\n\t\treturn len(results) \u003e= ft.limit\n\t}\n\n\t// iterate over user2games array if we have one;\n\t// if we don't, iterate over games.\n\tif ft.u2gAddr != \"\" {\n\t\tv := user2Games.Get(ft.u2gAddr.String())\n\t\tif v == nil {\n\t\t\treturn \"[]\"\n\t\t}\n\t\tgames := v.([]*Game)\n\t\tif ft.reverse {\n\t\t\tfor i := len(games) - 1; i \u003e= 0; i-- {\n\t\t\t\tif cb(games[i]) {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tfor _, game := range games {\n\t\t\t\tif cb(game) {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t} else {\n\t\tfn := gameStore.Iterate\n\t\tif ft.reverse {\n\t\t\tfn = gameStore.ReverseIterate\n\t\t}\n\t\tfn(ft.minID, ft.maxID, func(_ string, v interface{}) bool {\n\t\t\treturn cb(v.(*Game))\n\t\t})\n\t}\n\n\t// fast path: no results\n\tif len(results) == 0 {\n\t\treturn \"[]\"\n\t}\n\n\t// encode json\n\tvar buf bytes.Buffer\n\tbuf.WriteByte('[')\n\tfor idx, g := range results {\n\t\tbuf.WriteString(g.json())\n\t\tif idx != len(results)-1 {\n\t\t\tbuf.WriteByte(',')\n\t\t}\n\t}\n\tbuf.WriteByte(']')\n\n\treturn buf.String()\n}\n\ntype listGamesFilters struct {\n\tfilters []func(*Game) bool\n\tu2gAddr address\n\tmaxID   string\n\tminID   string\n\tlimit   int\n\treverse bool\n}\n\nfunc (l *listGamesFilters) valid(game *Game) bool {\n\tfor _, filt := range l.filters {\n\t\tif !filt(game) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// parseFilters parses a filter spec string into a listGamesFilters.\n// callerRlm must be the caller's own live cur; it's threaded into\n// parsePlayer for the \"caller\" / \"@user\" / addr resolution sub-cases.\n// IsCurrent guard mirrors parsePlayer's (Class-2 defense).\nfunc parseFilters(_ int, callerRlm realm, filters string) (r listGamesFilters) {\n\tif !callerRlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\t// default to desc order\n\tr.reverse = true\n\n\tparts := strings.Fields(filters)\n\tfor _, part := range parts {\n\t\tidx := strings.IndexAny(part, \":\u003c\u003e\")\n\t\tif idx \u003c 0 {\n\t\t\tpanic(\"invalid filter: \" + part)\n\t\t}\n\t\tfilt, pred := part[:idx+1], part[idx+1:]\n\t\tswitch filt {\n\t\tcase \"player:\":\n\t\t\ta := parsePlayer(0, callerRlm, pred)\n\t\t\tr.filters = append(r.filters, func(g *Game) bool { return g.White == a || g.Black == a })\n\t\t\tif r.u2gAddr == \"\" {\n\t\t\t\tr.u2gAddr = a\n\t\t\t}\n\t\tcase \"white:\":\n\t\t\ta := parsePlayer(0, callerRlm, pred)\n\t\t\tr.filters = append(r.filters, func(g *Game) bool { return g.White == a })\n\t\t\tif r.u2gAddr == \"\" {\n\t\t\t\tr.u2gAddr = a\n\t\t\t}\n\t\tcase \"black:\":\n\t\t\ta := parsePlayer(0, callerRlm, pred)\n\t\t\tr.filters = append(r.filters, func(g *Game) bool { return g.Black == a })\n\t\t\tif r.u2gAddr == \"\" {\n\t\t\t\tr.u2gAddr = a\n\t\t\t}\n\t\tcase \"finished:\":\n\t\t\tb := parseBool(pred)\n\t\t\tr.filters = append(r.filters, func(g *Game) bool { return g.State.IsFinished() == b })\n\t\tcase \"id\u003c\":\n\t\t\tr.maxID = pred\n\t\tcase \"id\u003e\":\n\t\t\tr.minID = pred\n\t\tcase \"limit:\":\n\t\t\tn, err := strconv.Atoi(pred)\n\t\t\tcheckErr(err)\n\t\t\tr.limit = n\n\t\tcase \"sort:\":\n\t\t\tr.reverse = pred == \"desc\"\n\t\tdefault:\n\t\t\tpanic(\"invalid filter: \" + filt)\n\t\t}\n\t}\n\treturn\n}\n\nfunc parseBool(s string) bool {\n\tswitch s {\n\tcase \"true\", \"True\", \"TRUE\", \"1\":\n\t\treturn true\n\tcase \"false\", \"False\", \"FALSE\", \"0\":\n\t\treturn false\n\t}\n\tpanic(\"invalid bool \" + s)\n}\n\n// parsePlayer resolves a player spec — a bech32 address, \"@user\" (r/sys/users\n// alias), or \"caller\" — to an address. callerRlm must be the caller's\n// own live cur; \"caller\" yields callerRlm.Previous().Address() (the\n// realm that called the exported API).\n//\n// SECURITY: the IsCurrent gate closes Class-2 designation forgery —\n// without it, a hostile caller could pass a stashed realm value whose\n// .Previous().Address() resolves to a victim's address and have\n// parsePlayer return it. See docs/resources/gno-security.md.\nfunc parsePlayer(_ int, callerRlm realm, s string) address {\n\tif !callerRlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tswitch {\n\tcase s == \"\":\n\t\tpanic(\"invalid address/user\")\n\tcase s == \"caller\":\n\t\treturn callerRlm.Previous().Address()\n\tcase s[0] == '@':\n\t\tu, _ := users.ResolveName(s[1:])\n\t\tif u == nil {\n\t\t\tpanic(\"user not found: \" + s[1:])\n\t\t}\n\t\treturn u.Addr()\n\tcase s[0] == 'g':\n\t\treturn address(s)\n\tdefault:\n\t\tpanic(\"invalid address/user: \" + s)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/morgan/chess\"\ngno = \"0.9\"\n"},{"name":"json.gno","body":"package chess\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"strconv\"\n\t\"time\"\n\n\t\"gno.land/p/morgan/chess\"\n\t\"gno.land/p/morgan/chess/glicko2\"\n\t\"gno.land/r/sys/users\"\n)\n\n// This file contains a bunch of JSON marshalers.\n// These should disappear eventually!\n// https://github.com/gnolang/gno/issues/1655\n\nfunc (g Game) MarshalJSON() (retBytes []byte, err error) {\n\tvar b bytes.Buffer\n\tb.WriteByte('{')\n\n\tnilAddr := func(na *address) string {\n\t\tif na == nil {\n\t\t\treturn `null`\n\t\t}\n\t\treturn `\"` + na.String() + `\"`\n\t}\n\tmjson := func(s string, val interface{ MarshalJSON() ([]byte, error) }, comma bool) {\n\t\tif err != nil {\n\t\t\treturn\n\t\t}\n\t\tvar res []byte\n\t\tres, err = val.MarshalJSON()\n\t\tif err != nil {\n\t\t\treturn\n\t\t}\n\t\tb.WriteString(`\"` + s + `\":`)\n\t\tb.Write(res)\n\t\tif comma {\n\t\t\tb.WriteByte(',')\n\t\t}\n\t}\n\n\tb.WriteString(`\"id\":\"` + g.ID + `\",`)\n\tb.WriteString(`\"white\":\"` + g.White.String() + `\",`)\n\tb.WriteString(`\"black\":\"` + g.Black.String() + `\",`)\n\n\tmjson(\"position\", jsonPosition{g.Position}, true)\n\tmjson(\"state\", g.State, true)\n\tmjson(\"winner\", g.Winner, true)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tb.WriteString(`\"creator\":\"` + g.Creator.String() + `\",`)\n\tb.WriteString(`\"created_at\":\"` + g.CreatedAt.Format(time.RFC3339) + `\",`)\n\tb.WriteString(`\"draw_offerer\":` + nilAddr(g.DrawOfferer) + \",\")\n\tb.WriteString(`\"concluder\":` + nilAddr(g.Concluder) + \",\")\n\n\tmjson(\"time\", g.Time, false)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tb.WriteByte('}')\n\treturn b.Bytes(), nil\n}\n\ntype jsonPosition struct {\n\tchess.Position\n}\n\nfunc (p jsonPosition) MarshalJSON() ([]byte, error) {\n\tvar b bytes.Buffer\n\tb.WriteByte('{')\n\n\tbfen := p.EncodeFEN()\n\tb.WriteString(`\"fen\":\"` + bfen + `\",`)\n\n\tb.WriteString(`\"moves\":[`)\n\n\tfor idx, m := range p.Moves {\n\t\tb.WriteString(`\"` + m.String() + `\"`)\n\t\tif idx != len(p.Moves)-1 {\n\t\t\tb.WriteByte(',')\n\t\t}\n\t}\n\n\tb.WriteByte(']')\n\tb.WriteByte('}')\n\treturn b.Bytes(), nil\n}\n\nfunc (w Winner) MarshalJSON() ([]byte, error) {\n\tif n := int(w); n \u003c len(winnerString) {\n\t\treturn []byte(`\"` + winnerString[n] + `\"`), nil\n\t}\n\treturn nil, errors.New(\"invalid winner value\")\n}\n\nfunc (g GameState) MarshalJSON() ([]byte, error) {\n\tif int(g) \u003e= len(gameStatesSnake) {\n\t\treturn nil, errors.New(\"invalid game state\")\n\t}\n\treturn []byte(`\"` + gameStatesSnake[g] + `\"`), nil\n}\n\nfunc (tc *TimeControl) MarshalJSON() ([]byte, error) {\n\tif tc == nil {\n\t\treturn []byte(\"null\"), nil\n\t}\n\tvar buf bytes.Buffer\n\n\tbuf.WriteByte('{')\n\tbuf.WriteString(`\"seconds\":` + strconv.Itoa(tc.Seconds) + `,`)\n\tbuf.WriteString(`\"increment\":` + strconv.Itoa(tc.Increment) + `,`)\n\tbuf.WriteString(`\"started_at\":\"` + tc.StartedAt.Format(time.RFC3339) + `\",`)\n\n\tbuf.WriteString(`\"move_timestamps\":[`)\n\tfor idx, mt := range tc.MoveTimestamps {\n\t\tbuf.WriteString(`\"` + mt.Format(time.RFC3339) + `\"`)\n\t\tif idx != len(tc.MoveTimestamps)-1 {\n\t\t\tbuf.WriteByte(',')\n\t\t}\n\t}\n\tbuf.WriteString(\"],\")\n\n\tbuf.WriteString(`\"white_time\":` + strconv.FormatInt(tc.WhiteTime.Milliseconds(), 10) + \",\")\n\tbuf.WriteString(`\"black_time\":` + strconv.FormatInt(tc.BlackTime.Milliseconds(), 10))\n\tbuf.WriteByte('}')\n\n\treturn buf.Bytes(), nil\n}\n\nfunc (p Player) MarshalJSON() ([]byte, error) {\n\tu := users.ResolveAddress(p.Address)\n\n\tvar buf bytes.Buffer\n\tbuf.WriteByte('{')\n\n\tbuf.WriteString(`\"address\":\"` + p.Address.String() + `\",`)\n\tif u == nil {\n\t\tbuf.WriteString(`\"username\":\"\",`)\n\t} else {\n\t\tbuf.WriteString(`\"username\":\"` + u.Name() + `\",`)\n\t}\n\n\tfor idx, cat := range categoryList {\n\t\tstat := p.CategoryInfo[cat]\n\t\tbuf.WriteString(`\"` + cat.String() + `\":{`)\n\t\tbuf.WriteString(`\"wins\":` + strconv.Itoa(stat.Wins) + \",\")\n\t\tbuf.WriteString(`\"losses\":` + strconv.Itoa(stat.Losses) + \",\")\n\t\tbuf.WriteString(`\"draws\":` + strconv.Itoa(stat.Draws) + \",\")\n\t\tbuf.WriteString(`\"rating\":`)\n\t\tif res, err := (jsonPlayerRating{stat.PlayerRating}).MarshalJSON(); err != nil {\n\t\t\treturn nil, err\n\t\t} else {\n\t\t\tbuf.Write(res)\n\t\t}\n\t\tbuf.WriteByte(',')\n\t\tbuf.WriteString(`\"position\":` + strconv.Itoa(p.LeaderboardPosition(cat)))\n\t\tbuf.WriteByte('}')\n\t\tif idx != len(categoryList)-1 {\n\t\t\tbuf.WriteByte(',')\n\t\t}\n\t}\n\n\tbuf.WriteByte('}')\n\treturn buf.Bytes(), nil\n}\n\ntype jsonPlayerRating struct{ *glicko2.PlayerRating }\n\nfunc (p jsonPlayerRating) MarshalJSON() ([]byte, error) {\n\tvar buf bytes.Buffer\n\tbuf.WriteByte('{')\n\tbuf.WriteString(`\"rating\":` + strconv.FormatFloat(p.Rating, 'f', 5, 64) + `,`)\n\tbuf.WriteString(`\"deviation\":` + strconv.FormatFloat(p.RatingDeviation, 'f', 5, 64) + `,`)\n\tbuf.WriteString(`\"volatility\":` + strconv.FormatFloat(p.RatingVolatility, 'f', 5, 64))\n\tbuf.WriteByte('}')\n\treturn buf.Bytes(), nil\n}\n"},{"name":"lobby.gno","body":"package chess\n\nimport (\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\ntype lobbyPlayer struct {\n\tjoinedAt time.Time\n\tseenAt   time.Time\n\tplayer   *Player\n}\n\nfunc (l lobbyPlayer) r(cat Category) float64 { return l.player.CategoryInfo[cat].Rating }\n\n// returns whether the rating r is within l's current \"acceptable\" range.\nfunc (l lobbyPlayer) acceptRating(cat Category, r float64) bool {\n\tdelta := int(time.Since(l.joinedAt) / time.Second)\n\tif delta \u003e= 10 {\n\t\treturn true\n\t}\n\n\thalfsize := bracketSize[delta] / 2\n\trat := l.r(cat)\n\treturn r \u003e= (rat-halfsize) \u0026\u0026 r \u003c= (rat+halfsize)\n}\n\ntype tcLobby byte\n\n// time control lobby divisions. N+M -\u003e tcLobbyNpM\nconst (\n\ttcLobby5p0 tcLobby = iota\n\ttcLobby10p5\n\ttcLobbyMax\n)\n\nfunc (tc tcLobby) Category() Category {\n\tswitch tc {\n\tcase tcLobby5p0:\n\t\treturn Blitz\n\tdefault:\n\t\treturn Rapid\n\t}\n}\n\nfunc (tc tcLobby) Time() (secs, incr int) {\n\tswitch tc {\n\tcase tcLobby5p0:\n\t\treturn 60 * 5, 0\n\tcase tcLobby10p5:\n\t\treturn 60 * 10, 5\n\tdefault:\n\t\tpanic(\"invalid tc value\")\n\t}\n}\n\nvar (\n\tlobby            [tcLobbyMax][]lobbyPlayer\n\tlobbyPlayer2Game avl.Tree // player addr -\u003e *Game. set after a user is matched; reset when joining again.\n)\n\nfunc LobbyJoin(cur realm, seconds, increment int) {\n\tassertOriginCall(0, cur)\n\tvar tc tcLobby\n\tswitch {\n\tcase seconds == (60*5) \u0026\u0026 increment == 0:\n\t\ttc = tcLobby5p0\n\tcase seconds == (60*10) \u0026\u0026 increment == 5:\n\t\ttc = tcLobby10p5\n\tdefault:\n\t\tpanic(\"can only use time controls 5+0 or 10+5\")\n\t}\n\n\t// Ensure that user's previous games are finished (or timed out).\n\tcaller := cur.Previous().Address()\n\n\tgames := getUserGames(caller)\n\t// XXX: temporary to avoid ever prohibiting a user from joining the lobby.\n\t// when possible, change back to assertGamesFinished(games)\n\tfor _, g := range games {\n\t\tif !g.State.IsFinished() {\n\t\t\tif err := resign(0, cur, g); err != nil {\n\t\t\t\tpanic(\"internal error (could not resign game \" + g.ID + \"): \" + err.Error())\n\t\t\t}\n\t\t}\n\t}\n\tassertUserNotInLobby(caller)\n\n\t// remove caller from lobbyPlayer2Game, so LobbyGameFound\n\t// returns the right value.\n\tlobbyPlayer2Game.Remove(caller.String())\n\n\tnow := time.Now()\n\tlobby[tc] = append(lobby[tc], lobbyPlayer{joinedAt: now, seenAt: now, player: getPlayer(caller)})\n\trefreshLobby(0, cur, tc)\n}\n\nfunc assertUserNotInLobby(caller address) {\n\tfor _, sublob := range lobby {\n\t\tfor _, pl := range sublob {\n\t\t\tif pl.player.Address == caller {\n\t\t\t\tpanic(\"you are already in the lobby\")\n\t\t\t}\n\t\t}\n\t}\n}\n\n// refreshLobby serves to run through the lobby, kick timed out users, and see if any users\n// can be matched with the current user.\nfunc refreshLobby(_ int, rlm realm, tc tcLobby) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tcallerAddr := rlm.Previous().Address()\n\tnow := time.Now()\n\tfor idx, player := range lobby[tc] {\n\t\tif player.player.Address == callerAddr {\n\t\t\t// mark player as seen now\n\t\t\tlobby[tc][idx].seenAt = now\n\t\t\tbreak\n\t\t}\n\t}\n\n\t// lobby housekeeping: kick any player that hasn't contacted us for the\n\t// past 30 seconds.\n\t// do this BEFORE matching the caller, as we want to give them someone who\n\t// is seemingly active in the lobby.\n\tfor i := 0; i \u003c len(lobby[tc]); i++ {\n\t\tif now.Sub(lobby[tc][i].seenAt) \u003e= time.Second*30 {\n\t\t\tnewLobby := append([]lobbyPlayer{}, lobby[tc][:i]...)\n\t\t\tlobby[tc] = append(newLobby, lobby[tc][i+1:]...)\n\t\t\ti--\n\t\t}\n\t}\n\n\t// determine sub lobby\n\tsublob := lobby[tc]\n\n\tcallerPos := -1\n\tvar caller lobbyPlayer\n\tfor idx, player := range sublob {\n\t\tif player.player.Address == callerAddr {\n\t\t\tcallerPos = idx\n\t\t\tcaller = player\n\t\t\tbreak\n\t\t}\n\t}\n\t// caller is not involved in lobby, or lobby only contains the player\n\tif callerPos \u003c 0 || len(sublob) \u003c 2 {\n\t\treturn\n\t}\n\n\tcat := tc.Category()\n\tcallerRating := caller.r(cat)\n\tcallerForce := now.Sub(caller.joinedAt) \u003e= time.Second*10\n\n\tfor i, player := range sublob {\n\t\tif i == callerPos {\n\t\t\tcontinue\n\t\t}\n\t\t// force if either the caller or the player have been waiting for more than 10s.\n\t\tforce := callerForce || (now.Sub(player.joinedAt) \u003e= time.Second*10)\n\t\t// find player whose rating falls in each other's range.\n\t\tif force || (caller.acceptRating(cat, player.r(cat)) \u0026\u0026\n\t\t\tplayer.acceptRating(cat, callerRating)) {\n\t\t\tlobbyMatch(tc, callerPos, i)\n\t\t\treturn\n\t\t}\n\t}\n}\n\nfunc lobbyMatch(tc tcLobby, p1, p2 int) {\n\t// Get the two players, create a new game with them.\n\tsecs, incr := tc.Time()\n\ta1, a2 := lobby[tc][p1].player.Address, lobby[tc][p2].player.Address\n\n\tgame := newGame(a1, a2, secs, incr)\n\n\t// remove p1 and p2 from lobby\n\tif p1 \u003e p2 {\n\t\tp1, p2 = p2, p1\n\t}\n\tnl := append([]lobbyPlayer{}, lobby[tc][:p1]...)\n\tnl = append(nl, lobby[tc][p1+1:p2]...)\n\tnl = append(nl, lobby[tc][p2+1:]...)\n\tlobby[tc] = nl\n\n\t// add to lobbyPlayer2Game\n\tlobbyPlayer2Game.Set(a1.String(), game)\n\tlobbyPlayer2Game.Set(a2.String(), game)\n}\n\n/*\ngenerated by python code:\n\n\tfor i in range(0,10):\n\t\tprint((i+1)**(3.694692926)+49, ',')\n\nrationale: give brackets in an exponential range between\n50 and 5000, dividing it into 10 steps.\n\"magic constant\" obtained solving for in c in the equation:\n\n\t5000=(x+1)^c+49 (x = steps, 10 in our case)\n\nwhich comes out to be ln(delta)/ln(steps), delta = 5000-49, steps = 10.\n*/\nvar bracketSize = [...]float64{\n\t50.0,\n\t61.9483191543645,\n\t106.91839582826664,\n\t216.65896892328266,\n\t431.3662312611604,\n\t798.94587409321,\n\t1374.4939512498888,\n\t2219.9018387103433,\n\t3403.5405753197747,\n\t5000,\n}\n\nfunc LobbyGameFound(cur realm) string {\n\trefreshLobby(0, cur, tcLobby5p0)\n\trefreshLobby(0, cur, tcLobby10p5)\n\n\tval := lobbyPlayer2Game.Get(cur.Previous().Address().String())\n\tif val == nil {\n\t\treturn \"null\"\n\t}\n\treturn val.(*Game).json()\n}\n\nfunc LobbyQuit(cur realm) {\n\tcaller := cur.Previous().Address()\n\tfor tc, sublob := range lobby {\n\t\tfor i, pl := range sublob {\n\t\t\tif pl.player.Address == caller {\n\t\t\t\tnewLobby := append([]lobbyPlayer{}, sublob[:i]...)\n\t\t\t\tnewLobby = append(newLobby, sublob[i+1:]...)\n\t\t\t\tlobby[tc] = newLobby\n\t\t\t\tlobbyPlayer2Game.Remove(caller.String())\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}\n\n\tpanic(\"you are not in the lobby\")\n}\n"},{"name":"lobby_test.gno","body":"package chess\n\nimport (\n\t\"os\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/morgan/chess/glicko2\"\n)\n\nfunc TestLobbyJoin(cur realm, t *testing.T) {\n\tcleanup()\n\ttesting.SetRealm(testing.NewUserRealm(white))\n\tLobbyJoin(cross(cur), 10*60, 5)\n\tos.Sleep(time.Second * 5)\n\ttesting.SetRealm(testing.NewUserRealm(black))\n\tLobbyJoin(cross(cur), 10*60, 5)\n\tres := LobbyGameFound(cross(cur))\n\tif res == \"null\" {\n\t\tt.Errorf(\"LobbyGameFound is null\")\n\t}\n}\n\nfunc sublobbyToIDs(pl []lobbyPlayer) []string {\n\ts := make([]string, len(pl))\n\tfor idx, p := range pl {\n\t\ts[idx] = string(p.player.Address)\n\t}\n\treturn s\n}\n\nfunc TestLobbyGameFound(cur realm, t *testing.T) {\n\tcheck := func(checker ...func(t *testing.T)) func(t *testing.T) {\n\t\treturn func(t *testing.T) {\n\t\t\tfor _, ck := range checker {\n\t\t\t\tck(t)\n\t\t\t}\n\t\t}\n\t}\n\tids := func(ids ...address) func(t *testing.T) {\n\t\treturn func(t *testing.T) {\n\t\t\tif len(ids) != len(lobby[0]) {\n\t\t\t\tt.Errorf(\"lobby doesn't match expected ids: lobby: %v, newIDs: %v\", sublobbyToIDs(lobby[0]), ids)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tfor idx, i := range ids {\n\t\t\t\tif pa := lobby[0][idx].player.Address; pa != i {\n\t\t\t\t\tt.Errorf(\"check pos %d: player id doesnt match (got %q want %q)\", idx, pa, i)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tnumGames := func(n int) func(t *testing.T) {\n\t\treturn func(t *testing.T) {\n\t\t\tl := gameStore.Size()\n\t\t\tif l != n {\n\t\t\t\tt.Errorf(\"invalid gameStore size; want %d got %d\", n, l)\n\t\t\t}\n\t\t}\n\t}\n\n\ttype pl struct {\n\t\tid     address\n\t\trating float64\n\t\t// use negative values here to indicate how many seconds in the past;\n\t\t// ie: joinedAt: -1, means player joined 1 second ago.\n\t\tjoinedAt int\n\t\tseenAt   int\n\t}\n\ttt := []struct {\n\t\tname   string\n\t\tpre    []pl\n\t\tcaller address\n\t\tcheck  func(t *testing.T)\n\t}{\n\t\t{\n\t\t\t\"equalRating\",\n\t\t\t[]pl{{\"1\", 1200, -1, -1}, {\"2\", 1200, 0, 0}},\n\t\t\t\"1\",\n\t\t\tcheck(ids(), numGames(1)),\n\t\t},\n\t\t{\n\t\t\t\"minimumApart\", // delta \u003c= 25\n\t\t\t[]pl{{\"1\", 1200, 0, 0}, {\"2\", 1225, 0, 0}},\n\t\t\t\"2\",\n\t\t\tcheck(ids(), numGames(1)),\n\t\t},\n\t\t{\n\t\t\t\"tooFarApart\", // delta \u003e 25\n\t\t\t[]pl{{\"1\", 1200, 0, 0}, {\"2\", 1230, 0, 0}},\n\t\t\t\"2\",\n\t\t\tcheck(ids(\"1\", \"2\"), numGames(0)),\n\t\t},\n\t\t{\n\t\t\t\"oldHighPriority\",\n\t\t\t// kicked hasn't been seen in too long, so should not be considered.\n\t\t\t// 1 is active and has been looking for 30s, so it gets priority, even if 2-3 is\n\t\t\t// a closer match.\n\t\t\t[]pl{{\"kicked\", 1800, -60, -50}, {\"1\", 1900, -30, -10}, {\"2\", 1400, 0, 0}, {\"3\", 1420, 0, 0}},\n\t\t\t\"3\",\n\t\t\tcheck(ids(\"2\"), numGames(1)),\n\t\t},\n\t\t{\n\t\t\t\"oldHighPriority2\",\n\t\t\t[]pl{{\"comeback\", 1800, -60, -50}, {\"1\", 1900, -30, -10}, {\"2\", 1400, 0, 0}, {\"3\", 1420, 0, 0}},\n\t\t\t// same as last one, except the player who was kicked last time, because\n\t\t\t// he's the caller, has their seenAt set back to the current time, so they're matched with 1.\n\t\t\t\"comeback\",\n\t\t\tcheck(ids(\"2\", \"3\"), numGames(1)),\n\t\t},\n\t\t{\n\t\t\t\"alone\",\n\t\t\t[]pl{{\"1\", 1200, 0, 0}},\n\t\t\t\"1\",\n\t\t\tcheck(ids(\"1\"), numGames(0)),\n\t\t},\n\t\t{\n\t\t\t\"brackFail\",\n\t\t\t[]pl{{\"1\", 1200, -4, -4}, {\"2\", 1450, -5, -5}},\n\t\t\t\"1\",\n\t\t\tcheck(ids(\"1\", \"2\"), numGames(0)),\n\t\t},\n\t\t{\n\t\t\t\"brackFail2\",\n\t\t\t[]pl{{\"1\", 1200, -5, -5}, {\"2\", 1450, -4, -4}},\n\t\t\t\"1\",\n\t\t\tcheck(ids(\"1\", \"2\"), numGames(0)),\n\t\t},\n\t\t{\n\t\t\t\"brackSuccess\",\n\t\t\t[]pl{{\"1\", 1200, -5, -5}, {\"2\", 1450, -5, -5}},\n\t\t\t\"1\",\n\t\t\tcheck(ids(), numGames(1)),\n\t\t},\n\t}\n\n\tfor _, tc := range tt {\n\t\tt.Run(tc.name, func(cur realm, t *testing.T) {\n\t\t\tcleanup()\n\t\t\tnow := time.Now()\n\t\t\tfor _, p := range tc.pre {\n\t\t\t\tlobby[0] = append(lobby[0], lobbyPlayer{\n\t\t\t\t\tjoinedAt: now.Add(time.Duration(p.joinedAt) * time.Second),\n\t\t\t\t\tseenAt:   now.Add(time.Duration(p.seenAt) * time.Second),\n\t\t\t\t\tplayer: \u0026Player{\n\t\t\t\t\t\tAddress: p.id,\n\t\t\t\t\t\tCategoryInfo: [CategoryMax]CategoryInfo{\n\t\t\t\t\t\t\tBlitz: {PlayerRating: \u0026glicko2.PlayerRating{Rating: p.rating}},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t}\n\n\t\t\ttesting.SetRealm(testing.NewUserRealm(tc.caller))\n\t\t\tLobbyGameFound(cross(cur))\n\n\t\t\tif tc.check != nil {\n\t\t\t\ttc.check(t)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestLobbyJoin_HasOpenGames(cur realm, t *testing.T) {\n\tcleanup()\n\tg := \u0026Game{\n\t\tID:    \"123\",\n\t\tWhite: white,\n\t\tBlack: black,\n\t\tState: GameStateOpen,\n\t}\n\tgameStore.Set(g.ID, g)\n\taddToUser2Games(white, g)\n\taddToUser2Games(black, g)\n\n\ttesting.SetRealm(testing.NewUserRealm(white))\n\tLobbyJoin(cross(cur), 10*60, 5)\n\tif g.State != GameStateAborted {\n\t\tt.Errorf(\"state wrong: want %d got %d\", GameStateAborted, g.State)\n\t}\n\tif g.Winner != WinnerNone {\n\t\tt.Errorf(\"winner wrong: want %q got %q\", \"none\", g.Winner)\n\t}\n}\n"},{"name":"time.gno","body":"package chess\n\nimport (\n\t\"time\"\n)\n\n// TimeControl keeps track of time control information for the game.\ntype TimeControl struct {\n\tSeconds   int\n\tIncrement int\n\n\tStartedAt      time.Time\n\tMoveTimestamps []time.Time\n\tWhiteTime      time.Duration\n\tBlackTime      time.Duration\n}\n\nfunc NewTimeControl(seconds, incr int) *TimeControl {\n\tif seconds \u003c= 0 {\n\t\treturn nil\n\t}\n\treturn \u0026TimeControl{\n\t\tSeconds:   seconds,\n\t\tIncrement: incr,\n\n\t\tStartedAt: time.Now(),\n\t\tWhiteTime: time.Duration(seconds) * time.Second,\n\t\tBlackTime: time.Duration(seconds) * time.Second,\n\t}\n}\n\n// AddMove records that at the current time, a new move was added.\nfunc (tc *TimeControl) AddMove() (valid bool) {\n\tnd, v := tc.timedOut()\n\tif v {\n\t\treturn false\n\t}\n\tif len(tc.MoveTimestamps)\u00261 == 0 {\n\t\ttc.WhiteTime = nd\n\t} else {\n\t\ttc.BlackTime = nd\n\t}\n\ttc.MoveTimestamps = append(tc.MoveTimestamps, time.Now())\n\treturn true\n}\n\nfunc (tc *TimeControl) TimedOut() bool {\n\t_, v := tc.timedOut()\n\treturn v\n}\n\nfunc (tc *TimeControl) timedOut() (time.Duration, bool) {\n\tmts := tc.MoveTimestamps\n\n\t// First move for each player: they both have up to 30 seconds.\n\tswitch len(mts) {\n\tcase 0:\n\t\tdelta := time.Since(tc.StartedAt)\n\t\treturn tc.WhiteTime, delta \u003e time.Second*30\n\tcase 1:\n\t\tdelta := time.Since(mts[0])\n\t\treturn tc.BlackTime, delta \u003e time.Second*30\n\t}\n\n\t// Determine color. Determine time since last move. Try subtracting from\n\t// color's time. If \u003e= 0, good. If \u003c 0, timeout.\n\tdelta := time.Since(mts[len(mts)-1])\n\n\tif len(mts)\u00261 == 0 { // white\n\t\tnt := tc.WhiteTime - delta\n\t\treturn nt + tc.incr(), nt \u003c 0\n\t}\n\n\tnt := tc.BlackTime - delta\n\treturn nt + tc.incr(), nt \u003c 0\n}\n\nfunc (tc *TimeControl) incr() time.Duration {\n\t// there is always at least a one second increment, to account for\n\t// block time and the delay between user making a move and tx happening\n\treturn time.Second + time.Duration(tc.Increment)*time.Second\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"q+yBeMq0ZFWoCH4aORr0f3j+jN5VqFVJzpM75srMnB0WfAq3ZH/OZxqfyKya912BzQ1hnGiI04qdy2gQRpG4Cg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"guestbook","path":"gno.land/r/morgan/guestbook","files":[{"name":"admin.gno","body":"package guestbook\n\nimport (\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\nvar owner *ownable.Ownable\n\nfunc init(cur realm) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"realm must be initialized by EOA\")\n\t}\n\towner = ownable.NewWithAddress(cur.Previous().Address())\n}\n\n// AdminDelete removes the guestbook message with the given ID.\n// The user will still be marked as having submitted a message, so they\n// won't be able to re-submit a new message.\nfunc AdminDelete(cur realm, signatureID string) {\n\towner.AssertOwnedBy(cur.Previous().Address())\n\tid, err := seqid.FromString(signatureID)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tidb := id.Binary()\n\tif !guestbook.Has(idb) {\n\t\tpanic(\"signature does not exist\")\n\t}\n\tguestbook.Remove(idb)\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/morgan/guestbook\"\ngno = \"0.9\"\n"},{"name":"guestbook.gno","body":"// Realm guestbook contains an implementation of a simple guestbook.\n// Come and sign yourself up!\npackage guestbook\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\t\"unicode\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// Signature is a single entry in the guestbook.\ntype Signature struct {\n\tMessage string\n\tAuthor  address\n\tTime    time.Time\n}\n\nconst (\n\tmaxMessageLength = 140\n\tmaxPerPage       = 25\n)\n\nvar (\n\tsignatureID seqid.ID\n\tguestbook   avl.Tree // id -\u003e Signature\n\thasSigned   avl.Tree // address -\u003e struct{}\n)\n\nfunc init(cur realm) {\n\tSign(cur, \"You reached the end of the guestbook!\")\n}\n\nconst (\n\terrNotAUser                  = \"this guestbook can only be signed by users\"\n\terrAlreadySigned             = \"you already signed the guestbook!\"\n\terrInvalidCharacterInMessage = \"invalid character in message\"\n)\n\n// Sign signs the guestbook, with the specified message.\nfunc Sign(cur realm, message string) {\n\tprev := cur.Previous()\n\tswitch {\n\tcase !prev.IsUser():\n\t\tpanic(errNotAUser)\n\tcase hasSigned.Has(prev.Address().String()):\n\t\tpanic(errAlreadySigned)\n\t}\n\tmessage = validateMessage(message)\n\n\tguestbook.Set(signatureID.Next().Binary(), Signature{\n\t\tMessage: message,\n\t\tAuthor:  prev.Address(),\n\t\t// NOTE: time.Now() will yield the \"block time\", which is deterministic.\n\t\tTime: time.Now(),\n\t})\n\thasSigned.Set(prev.Address().String(), struct{}{})\n}\n\nfunc validateMessage(msg string) string {\n\tif len(msg) \u003e maxMessageLength {\n\t\tpanic(\"Keep it brief! (max \" + strconv.Itoa(maxMessageLength) + \" bytes!)\")\n\t}\n\tout := \"\"\n\tfor _, ch := range msg {\n\t\tswitch {\n\t\tcase unicode.IsLetter(ch),\n\t\t\tunicode.IsNumber(ch),\n\t\t\tunicode.IsSpace(ch),\n\t\t\tunicode.IsPunct(ch):\n\t\t\tout += string(ch)\n\t\tdefault:\n\t\t\tpanic(errInvalidCharacterInMessage)\n\t\t}\n\t}\n\treturn out\n}\n\nfunc Render(maxID string) string {\n\tvar bld strings.Builder\n\n\tbld.WriteString(\"# Guestbook 📝\\n\\n[Come sign the guestbook!](./guestbook$help\u0026func=Sign)\\n\\n---\\n\\n\")\n\n\tvar maxIDBinary string\n\tif maxID != \"\" {\n\t\tmid, err := seqid.FromString(maxID)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\t// AVL iteration is exclusive, so we need to decrease the ID value to get the \"true\" maximum.\n\t\tmid--\n\t\tmaxIDBinary = mid.Binary()\n\t}\n\n\tvar lastID seqid.ID\n\tvar printed int\n\tguestbook.ReverseIterate(\"\", maxIDBinary, func(key string, val any) bool {\n\t\tsig := val.(Signature)\n\t\tmessage := strings.ReplaceAll(sig.Message, \"\\n\", \"\\n\u003e \")\n\t\tbld.WriteString(\"\u003e \" + message + \"\\n\u003e\\n\")\n\t\tidValue, ok := seqid.FromBinary(key)\n\t\tif !ok {\n\t\t\tpanic(\"invalid seqid id\")\n\t\t}\n\n\t\tbld.WriteString(\"\u003e _Written by \" + sig.Author.String() + \" at \" + sig.Time.Format(time.DateTime) + \"_ (#\" + idValue.String() + \")\\n\\n---\\n\\n\")\n\t\tlastID = idValue\n\n\t\tprinted++\n\t\t// stop after exceeding limit\n\t\treturn printed \u003e= maxPerPage\n\t})\n\n\tif printed == 0 {\n\t\tbld.WriteString(\"No messages!\")\n\t} else if printed \u003e= maxPerPage {\n\t\tbld.WriteString(\"\u003cp style='text-align:right'\u003e\u003ca href='./guestbook:\" + lastID.String() + \"'\u003eNext page\u003c/a\u003e\u003c/p\u003e\")\n\t}\n\n\treturn bld.String()\n}\n"},{"name":"guestbook_test.gno","body":"package guestbook\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/avl/v0\"\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\nfunc TestSign(cur realm, t *testing.T) {\n\tguestbook = avl.Tree{}\n\thasSigned = avl.Tree{}\n\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\tSign(cross(cur), \"Hello!\")\n\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user2\"))\n\tSign(cross(cur), \"Hello2!\")\n\n\tres := Render(\"\")\n\tt.Log(res)\n\tif !strings.Contains(res, \"\u003e Hello!\\n\u003e\\n\u003e _Written by g1user \") {\n\t\tt.Error(\"does not contain first user's message\")\n\t}\n\tif !strings.Contains(res, \"\u003e Hello2!\\n\u003e\\n\u003e _Written by g1user2 \") {\n\t\tt.Error(\"does not contain second user's message\")\n\t}\n\tif guestbook.Size() != 2 {\n\t\tt.Error(\"invalid guestbook size\")\n\t}\n}\n\nfunc TestSign_FromRealm(cur realm, t *testing.T) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/sys/users/init\"))\n\n\trec := revive(func() { Sign(cross(cur), \"Hey!\") })\n\tif rec == nil {\n\t\tt.Fatal(\"expected panic\")\n\t}\n\trecString, ok := rec.(string)\n\tif !ok {\n\t\tt.Fatal(\"not a string\", rec)\n\t} else if recString != errNotAUser {\n\t\tt.Fatal(\"invalid error\", recString)\n\t}\n}\n\nfunc TestSign_Double(cur realm, t *testing.T) {\n\t// Should not allow signing twice.\n\tguestbook = avl.Tree{}\n\thasSigned = avl.Tree{}\n\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\tSign(cross(cur), \"Hello!\")\n\n\trec := revive(func() { Sign(cross(cur), \"Hello again!\") })\n\tif rec == nil {\n\t\tt.Fatal(\"expected panic\")\n\t}\n\trecString, ok := rec.(string)\n\tif !ok {\n\t\tt.Error(\"type assertion failed\", rec)\n\t} else if recString != errAlreadySigned {\n\t\tt.Error(\"invalid error message\", recString)\n\t}\n}\n\nfunc TestSign_InvalidMessage(cur realm, t *testing.T) {\n\t// Should not allow control characters in message.\n\tguestbook = avl.Tree{}\n\thasSigned = avl.Tree{}\n\n\ttesting.SetRealm(testing.NewUserRealm(\"g1user\"))\n\n\trec := revive(func() { Sign(cross(cur), \"\\x00Hello!\") })\n\tif rec == nil {\n\t\tt.Fatal(\"expected panic\")\n\t}\n\trecString, ok := rec.(string)\n\tif !ok {\n\t\tt.Error(\"type assertion failed\", rec)\n\t} else if recString != errInvalidCharacterInMessage {\n\t\tt.Error(\"invalid error message\", recString)\n\t}\n}\n\nfunc TestAdminDelete(cur realm, t *testing.T) {\n\tconst (\n\t\tuserAddr  address = \"g1user\"\n\t\tadminAddr address = \"g1admin\"\n\t)\n\n\tguestbook = avl.Tree{}\n\thasSigned = avl.Tree{}\n\towner = ownable.NewWithAddress(adminAddr)\n\tsignatureID = 0\n\n\ttesting.SetRealm(testing.NewUserRealm(userAddr))\n\n\tconst bad = \"Very Bad Message! Nyeh heh heh!\"\n\tSign(cross(cur), bad)\n\n\tif rnd := Render(\"\"); !strings.Contains(rnd, bad) {\n\t\tt.Fatal(\"render does not contain bad message\", rnd)\n\t}\n\n\ttesting.SetRealm(testing.NewUserRealm(adminAddr))\n\tAdminDelete(cross(cur), signatureID.String())\n\n\tif rnd := Render(\"\"); strings.Contains(rnd, bad) {\n\t\tt.Error(\"render contains bad message\", rnd)\n\t}\n\tif guestbook.Size() != 0 {\n\t\tt.Error(\"invalid guestbook size\")\n\t}\n\tif hasSigned.Size() != 1 {\n\t\tt.Error(\"invalid hasSigned size\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"dsSo3HvIQMW/AT0lCosMK8ai3/L4EnwYBqXWWbXT0LjC9qxwzz9iczKgVWaKdRvV6px02r+NzelU9VTrrcWGAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/morgan/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/morgan/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport \"gno.land/r/leon/hor\"\n\nconst staticHome = `# morgan's (gn)home\n\n- [📝 sign my guestbook](/r/morgan/guestbook)\n`\n\nfunc init(cur realm) { hor.Register(cross(cur), \"Morgan's Home Realm\", \"\") }\n\nfunc Render(path string) string {\n\treturn staticHome\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"EFrrkZ8wh3lhDPVZsIE3jHs4bLdfWCsngVxpz3AQ8t6hunaScTPvVhi1xZHJOaB0yeDtX+13GCmNw8w5946DAw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"config","path":"gno.land/r/moul/config","files":[{"name":"config.gno","body":"package config\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/moul/authz\"\n)\n\nvar Authorizer *authz.Authorizer\n\nfunc init(cur realm) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"r/moul/config must be initialized by an EOA\")\n\t}\n\tAuthorizer = authz.NewWithMembers(cur.Previous().Address())\n}\n\n// AddManager adds a new address to the list of authorized managers.\n// This only works if the current authority is a MemberAuthority.\n// The caller must be authorized by the current authority.\nfunc AddManager(cur realm, addr address) error {\n\tmemberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)\n\tif !ok {\n\t\treturn errors.New(\"current authority is not a MemberAuthority, cannot add manager directly\")\n\t}\n\treturn memberAuth.AddMember(0, cur, addr)\n}\n\n// RemoveManager removes an address from the list of authorized managers.\n// This only works if the current authority is a MemberAuthority.\n// The caller must be authorized by the current authority.\nfunc RemoveManager(cur realm, addr address) error {\n\tmemberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)\n\tif !ok {\n\t\treturn errors.New(\"current authority is not a MemberAuthority, cannot remove manager directly\")\n\t}\n\treturn memberAuth.RemoveMember(0, cur, addr)\n}\n\n// SECURITY XXX: TransferManagement accepts any Authority impl as the\n// new authority — `newAuthority` is an open interface. Two risks:\n// (a) If the caller passes a foreign Authority impl, the impl's\n//\n//\tAuthorize method runs with whatever this realm's authority\n//\tgrants. Hostile impls can always-approve (silent privilege\n//\tescalation) or always-deny (DoS / lockout).\n//\n// (b) Once the new authority is installed, there is no way back if it\n//\n//\tturns out hostile — the new authority controls all future\n//\tTransfer calls. An attacker who becomes the authority (legit\n//\tor via a malicious newAuthority) can install a permanent\n//\tDroppedAuthority to lock this realm forever.\n//\n// Fix: restrict newAuthority to canonical authz impls (MemberAuthority,\n// ContractAuthority, etc. from p/moul/authz). Optionally wrap Transfer\n// with a quorum/cooldown check or a one-shot guard so a single\n// compromised authority can't permanently brick the config realm.\n//\n// TransferManagement transfers the authority to manage keys to a new authority.\n// The caller must be authorized by the current authority.\nfunc TransferManagement(cur realm, newAuthority authz.Authority) error {\n\tif newAuthority == nil {\n\t\treturn errors.New(\"new authority cannot be nil\")\n\t}\n\treturn Authorizer.Transfer(0, cur, newAuthority)\n}\n\n// ListManagers returns a slice of all managed keys.\nfunc ListManagers(cur realm) []address {\n\tvar keyList []address\n\tmemberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)\n\tif !ok {\n\t\treturn keyList\n\t}\n\ttree := memberAuth.Tree()\n\tif !ok || tree == nil {\n\t\treturn keyList // Return empty list if tree is not as expected or nil\n\t}\n\ttree.Iterate(\"\", \"\", func(key string, _ any) bool {\n\t\tkeyList = append(keyList, address(key))\n\t\treturn false\n\t})\n\treturn keyList\n}\n\nfunc HasManager(cur realm, addr address) bool {\n\tmemberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)\n\tif !ok {\n\t\treturn false // Return false if not a MemberAuthority or doesn't exist\n\t}\n\t// Use the MemberAuthority's specific RemoveMember method,\n\t// which internally performs the authorization check.\n\treturn memberAuth.Has(addr)\n}\n"},{"name":"config_test.gno","body":"package config\n\nimport (\n\t\"chain\"\n\t\"chain/runtime/unsafe\"\n\t\"errors\"\n\t\"testing\"\n\n\t\"gno.land/p/moul/authz\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nvar (\n\toriginAddr     = testutils.TestAddress(\"origin\")\n\tmanager1Addr   = testutils.TestAddress(\"manager1\")\n\tmanager2Addr   = testutils.TestAddress(\"manager2\")\n\tnonManagerAddr = testutils.TestAddress(\"nonManager\")\n)\n\n// Helper to reset the Authorizer for each test, simulating initialization.\nfunc setupTest(cur realm, t *testing.T) {\n\tt.Helper()\n\n\t// Set the initial caller context\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\t// Initialize the Authorizer with the originAddr as the sole member,\n\t// simulating the state after NewWithOrigin() in a real deployment.\n\tAuthorizer = authz.NewWithAuthority(authz.NewMemberAuthority(originAddr))\n\t// Ensure the origin address is the initial manager\n\tuassert.True(t, HasManager(cur, originAddr), \"origin should be the initial manager\")\n}\n\nfunc TestAddManager(cur realm, t *testing.T) {\n\tsetupTest(cur, t)\n\n\t// Origin adds manager1 - Should succeed\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr := AddManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err, \"origin adding manager1 should succeed\")\n\tuassert.True(t, HasManager(cur, manager1Addr), \"manager1 should now be a manager\")\n\n\t// Non-manager tries to add manager2 - Should fail\n\ttesting.SetRealm(testing.NewUserRealm(nonManagerAddr))\n\terr = AddManager(cross(cur), manager2Addr)\n\tuassert.Error(t, err, \"non-manager adding manager2 should fail\")\n\tuassert.False(t, HasManager(cur, manager2Addr), \"manager2 should not have been added\")\n\n\t// Manager1 adds manager2 - Should succeed\n\ttesting.SetRealm(testing.NewUserRealm(manager1Addr))\n\terr = AddManager(cross(cur), manager2Addr)\n\tuassert.NoError(t, err, \"manager1 adding manager2 should succeed\")\n\tuassert.True(t, HasManager(cur, manager2Addr), \"manager2 should now be a manager\")\n\n\t// Transfer authority away from MemberAuthority\n\ttesting.SetRealm(testing.NewUserRealm(originAddr)) // Origin transfers\n\terr = TransferManagement(cross(cur), authz.NewAutoAcceptAuthority())\n\tuassert.NoError(t, err, \"transferring authority should succeed\")\n\n\t// Try adding after transfer - Should fail (wrong authority type)\n\ttesting.SetRealm(testing.NewUserRealm(manager1Addr))\n\terr = AddManager(cross(cur), nonManagerAddr) // Try adding someone new\n\tuassert.ErrorContains(t, err, \"current authority is not a MemberAuthority\", \"adding manager should fail after transfer\")\n}\n\nfunc TestRemoveManager(cur realm, t *testing.T) {\n\tsetupTest(cur, t)\n\n\t// Add manager1 first\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr := AddManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err, \"setup: failed to add manager1\")\n\tuassert.True(t, HasManager(cur, manager1Addr), \"setup: manager1 should be added\")\n\n\t// Non-manager tries to remove manager1 - Should fail\n\ttesting.SetRealm(testing.NewUserRealm(nonManagerAddr))\n\terr = RemoveManager(cross(cur), manager1Addr)\n\tuassert.Error(t, err, \"non-manager removing manager1 should fail\")\n\tuassert.True(t, HasManager(cur, manager1Addr), \"manager1 should still be a manager\")\n\n\t// Origin removes manager1 - Should succeed\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr = RemoveManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err, \"origin removing manager1 should succeed\")\n\tuassert.False(t, HasManager(cur, manager1Addr), \"manager1 should now be removed\")\n\n\t// Add manager1 again for next test case\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr = AddManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err, \"setup: failed to re-add manager1\")\n\n\t// Transfer authority\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr = TransferManagement(cross(cur), authz.NewAutoAcceptAuthority())\n\tuassert.NoError(t, err, \"transferring authority should succeed\")\n\n\t// Try removing after transfer - Should fail (wrong authority type)\n\ttesting.SetRealm(testing.NewUserRealm(originAddr)) // Use origin, doesn't matter which user now\n\terr = RemoveManager(cross(cur), manager1Addr)\n\tuassert.ErrorContains(t, err, \"current authority is not a MemberAuthority\", \"removing manager should fail after transfer\")\n}\n\nfunc TestListManagers(cur realm, t *testing.T) {\n\tsetupTest(cur, t)\n\tinitialList := ListManagers(cross(cur))\n\tassertAddrSliceEqual(t, []address{originAddr}, initialList)\n\t// Add manager1 and manager2\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr := AddManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err)\n\terr = AddManager(cross(cur), manager2Addr)\n\tuassert.NoError(t, err)\n\n\t// List should contain origin, manager1, manager2\n\tlist1 := ListManagers(cross(cur))\n\texpected1 := []address{manager2Addr, manager1Addr, originAddr}\n\tassertAddrSliceEqual(t, expected1, list1)\n\n\t// Remove manager1\n\ttesting.SetRealm(testing.NewUserRealm(originAddr)) // Can be origin or manager2\n\terr = RemoveManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err)\n\n\t// List should contain origin, manager2\n\tlist2 := ListManagers(cross(cur))\n\texpected2 := []address{manager2Addr, originAddr}\n\tassertAddrSliceEqual(t, expected2, list2)\n\n\t// Transfer authority\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr = TransferManagement(cross(cur), authz.NewAutoAcceptAuthority())\n\tuassert.NoError(t, err)\n\n\t// List should be empty after transfer\n\tlist3 := ListManagers(cross(cur))\n\tuassert.True(t, len(list3) == 0, \"manager list should be empty after transfer\")\n}\n\nfunc TestHasManager(cur realm, t *testing.T) {\n\tsetupTest(cur, t)\n\n\t// Initially, only origin is manager\n\tuassert.True(t, HasManager(cross(cur), originAddr), \"origin should initially be a manager\")\n\tuassert.False(t, HasManager(cross(cur), manager1Addr), \"manager1 should not initially be a manager\")\n\tuassert.False(t, HasManager(cross(cur), nonManagerAddr), \"nonManager should not initially be a manager\")\n\n\t// Add manager1\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr := AddManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err)\n\n\t// Check again\n\tuassert.True(t, HasManager(cross(cur), originAddr), \"origin should still be a manager\")\n\tuassert.True(t, HasManager(cross(cur), manager1Addr), \"manager1 should now be a manager\")\n\tuassert.False(t, HasManager(cross(cur), nonManagerAddr), \"nonManager should still not be a manager\")\n\n\t// Transfer authority\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr = TransferManagement(cross(cur), authz.NewAutoAcceptAuthority())\n\tuassert.NoError(t, err)\n\n\t// After transfer, HasManager should always return false for MemberAuthority checks\n\tuassert.False(t, HasManager(cross(cur), originAddr), \"HasManager should be false after transfer\")\n\tuassert.False(t, HasManager(cross(cur), manager1Addr), \"HasManager should be false after transfer\")\n\tuassert.False(t, HasManager(cross(cur), nonManagerAddr), \"HasManager should be false after transfer\")\n}\n\nfunc TestTransferManagement(cur realm, t *testing.T) {\n\tsetupTest(cur, t)\n\n\t// Add manager1\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr := AddManager(cross(cur), manager1Addr)\n\tuassert.NoError(t, err)\n\n\t// Create a new authority (MemberAuthority with manager2)\n\tnewAuthority := authz.NewMemberAuthority(manager2Addr)\n\n\t// Non-manager tries to transfer - Should fail\n\ttesting.SetRealm(testing.NewUserRealm(nonManagerAddr))\n\terr = TransferManagement(cross(cur), newAuthority)\n\tuassert.Error(t, err, \"non-manager transfer should fail\")\n\t_, isMemberAuth := Authorizer.Authority().(*authz.MemberAuthority)\n\tuassert.True(t, isMemberAuth, \"authority should still be MemberAuthority\") // Verify it didn't change\n\n\t// Manager1 tries to transfer - Should succeed\n\ttesting.SetRealm(testing.NewUserRealm(manager1Addr))\n\terr = TransferManagement(cross(cur), newAuthority)\n\tuassert.NoError(t, err, \"manager1 transfer should succeed\")\n\n\t// Verify current authority is the new one\n\tcurrentAuth := Authorizer.Authority()\n\tuassert.True(t, currentAuth == newAuthority, \"current authority should be the new one\")\n\n\t// Verify origin is no longer a manager under the *new* authority\n\ttesting.SetRealm(testing.NewUserRealm(manager2Addr)) // Need new manager to check\n\tuassert.False(t, HasManager(cross(cur), originAddr), \"origin should not be manager under new authority\")\n\tuassert.False(t, HasManager(cross(cur), manager1Addr), \"manager1 should not be manager under new authority\")\n\tuassert.True(t, HasManager(cross(cur), manager2Addr), \"manager2 should be manager under new authority\")\n\n\t// Try adding a manager using the old origin - Should fail\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr = AddManager(cross(cur), nonManagerAddr)\n\tuassert.Error(t, err, \"origin should not be able to add manager after transfer\")\n\n\t// Try adding a manager using the new manager (manager2) - Should succeed\n\ttesting.SetRealm(testing.NewUserRealm(manager2Addr))\n\terr = AddManager(cross(cur), nonManagerAddr)\n\tuassert.NoError(t, err, \"new manager (manager2) should be able to add managers\")\n\tuassert.True(t, HasManager(cross(cur), nonManagerAddr), \"nonManager should be added by manager2\")\n\n\t// Try transferring to nil - Should fail\n\ttesting.SetRealm(testing.NewUserRealm(manager2Addr))\n\terr = TransferManagement(cross(cur), nil)\n\tuassert.ErrorContains(t, err, \"new authority cannot be nil\", \"transferring to nil should fail\")\n}\n\nfunc TestTransferToContractAuthority(cur realm, t *testing.T) {\n\tsetupTest(cur, t) // Origin is the initial manager\n\n\tcontractPath := \"gno.land/r/testcontract\"\n\tcontractRealm := testing.NewCodeRealm(contractPath) // Simulate contract realm\n\n\t// Define a simple contract authority handler\n\thandlerExecuted := false // Track if the handler itself gets called\n\tcontractAuth := authz.NewContractAuthority(contractPath, func(title string, action authz.PrivilegedAction) error {\n\t\t// Simulate contract checking the caller *before* executing\n\t\tcaller := unsafe.CurrentRealm().Address()\n\t\texpectedContractAddr := chain.PackageAddress(contractPath)\n\t\tif caller != expectedContractAddr {\n\t\t\t// Fail before marking executed or running action\n\t\t\t// Note: In a real scenario, this handler might just ignore the call\n\t\t\t// if the caller isn't right, rather than returning an error,\n\t\t\t// depending on the desired contract logic. Returning an error\n\t\t\t// here helps the test verify the handler wasn't improperly called.\n\t\t\treturn errors.New(\"handler: caller is not the contract\")\n\t\t}\n\n\t\t// Only mark executed and run action if caller is correct\n\t\thandlerExecuted = true\n\t\treturn action()\n\t})\n\n\t// Origin transfers management to the contract authority\n\ttesting.SetRealm(testing.NewUserRealm(originAddr))\n\terr := TransferManagement(cross(cur), contractAuth)\n\tuassert.NoError(t, err, \"transfer to contract authority failed\")\n\tuassert.True(t, Authorizer.Authority() == contractAuth, \"authority should now be the contract authority\")\n\n\t// Now, actions like AddManager/RemoveManager should fail because the current\n\t// authority is no longer a MemberAuthority. The contract would need its own\n\t// logic executed via Authorizer.DoByCurrent() to manage members if desired.\n\n\t// Try adding a manager (will check authority type) - Should fail\n\ttesting.SetRealm(testing.NewUserRealm(originAddr)) // Caller doesn't matter for this check\n\terr = AddManager(cross(cur), manager1Addr)\n\tuassert.ErrorContains(t, err, \"current authority is not a MemberAuthority\", \"AddManager should fail with ContractAuthority\")\n\n\t// Simulate an action authorized *by the contract* using Authorizer.Do\n\tvar contractActionExecuted bool\n\thandlerExecuted = false         // Reset tracker\n\ttesting.SetRealm(contractRealm) // Call must originate from the contract now\n\terr = Authorizer.DoByCurrent(0, cur, \"some_contract_action\", func() error {\n\t\tcontractActionExecuted = true\n\t\t// Imagine contract logic here\n\t\treturn nil\n\t})\n\tuassert.NoError(t, err, \"contract action via Authorizer.Do failed\")\n\tuassert.True(t, handlerExecuted, \"handler should have been executed by contract call\") // Verify handler ran\n\tuassert.True(t, contractActionExecuted, \"contract action should have been executed\")\n\n\t// Simulate an action from a user - Should fail before handler is called\n\tvar userActionExecuted bool\n\thandlerExecuted = false // Reset tracker\n\ttesting.SetRealm(testing.NewUserRealm(nonManagerAddr))\n\terr = Authorizer.DoByCurrent(0, cur, \"some_user_action\", func() error {\n\t\tuserActionExecuted = true\n\t\treturn nil\n\t})\n\t// The ContractAuthority.Authorize method should return an error\n\t// because the handler now returns an error if the caller isn't the contract.\n\tuassert.Error(t, err, \"user action via Authorizer.Do should fail when contract is authority\")\n\tuassert.ErrorContains(t, err, \"handler: caller is not the contract\", \"error should originate from handler check\") // Check specific error\n\tuassert.False(t, handlerExecuted, \"handler should NOT have been executed by user call\")                           // Verify handler didn't run past the check\n\tuassert.False(t, userActionExecuted, \"user action should not have been executed\")\n}\n\n// Helper to check if a slice contains a specific address\nfunc containsAddr(list []address, addr address) bool {\n\tfor _, item := range list {\n\t\tif item == addr {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc assertAddrSliceEqual(t *testing.T, expected, actual []address) {\n\tt.Helper()\n\tif len(expected) != len(actual) {\n\t\tt.Fatalf(\"expected slice length %d, got %d. Expected: %v, Got: %v\", len(expected), len(actual), expected, actual)\n\t}\n\n\tfor i := range expected {\n\t\tif expected[i] != actual[i] {\n\t\t\tt.Fatalf(\"slices differ at index %d. Expected: %v, Got: %v\", i, expected, actual)\n\t\t}\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/moul/config\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"heb/ZC4nq/pjnet8LuFu1a2UCU7QzaA7l/5zTcWbHOK3h+eXjmuJ92pKkW7w8Tr3oL9MFnYGI0LLrvURxTjFCA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1manfred47kzduec920z88wfr64ylksmdcedlf5","package":{"name":"microposts","path":"gno.land/r/moul/microposts","files":[{"name":"README.md","body":"# fork of `leon/fosdem25/microposts`\n\nremoving optional lines to make the code more concise for slides.\n\nOriginal work here: https://gno.land/r/leon/fosdem25/microposts\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/moul/microposts\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1manfred47kzduec920z88wfr64ylksmdcedlf5\"\n"},{"name":"microposts_test.gno","body":"package microposts\n\n// empty file just to make sure that `gno test` tries to parse the implementation.\n"},{"name":"post.gno","body":"package microposts\n\nimport (\n\t\"time\"\n)\n\ntype Post struct {\n\ttext      string\n\tauthor    address\n\tcreatedAt time.Time\n}\n\nfunc (p Post) String() string {\n\tout := p.text + \"\\n\"\n\tout += \"_\" + p.createdAt.Format(\"02 Jan 2006, 15:04\") + \", by \" + p.author.String() + \"_\"\n\treturn out\n}\n"},{"name":"realm.gno","body":"package microposts\n\nimport (\n\t\"strconv\"\n\t\"time\"\n)\n\nvar posts []*Post\n\nfunc CreatePost(cur realm, text string) {\n\tposts = append(posts, \u0026Post{\n\t\ttext:      text,\n\t\tauthor:    cur.Previous().Address(), // provided by env\n\t\tcreatedAt: time.Now(),\n\t})\n}\n\nfunc Render(_ string) string {\n\tout := \"# Posts\\n\"\n\tfor i := len(posts) - 1; i \u003e= 0; i-- {\n\t\tout += \"### Post \" + strconv.Itoa(i) + \"\\n\" + posts[i].String()\n\t}\n\treturn out\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"ddYgYZCvLaHL1kx7nqX83gLhrx4Q04re1xBhsKvzoNLS719I2PI72MYdA/6Eo6WvIlq+yuFas2X4SKC9rQu0Bw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"config","path":"gno.land/r/n2p5/config","files":[{"name":"config.gno","body":"package config\n\nimport (\n\t\"gno.land/p/n2p5/mgroup\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nconst (\n\toriginalOwner = \"g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t\" // n2p5\n)\n\nvar (\n\tadminGroup  = mgroup.New(originalOwner)\n\tdescription = \"\"\n)\n\n// AddBackupOwner adds a backup owner to the Owner Group.\n// A backup owner can claim ownership of the contract.\nfunc AddBackupOwner(cur realm, addr address) {\n\tif err := adminGroup.AddBackupOwner(0, cur, addr); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// RemoveBackupOwner removes a backup owner from the Owner Group.\n// The primary owner cannot be removed.\nfunc RemoveBackupOwner(cur realm, addr address) {\n\tif err := adminGroup.RemoveBackupOwner(0, cur, addr); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// ClaimOwnership allows an authorized user in the ownerGroup\n// to claim ownership of the contract.\nfunc ClaimOwnership(cur realm) {\n\tif err := adminGroup.ClaimOwnership(0, cur); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// AddAdmin adds an admin to the Admin Group.\nfunc AddAdmin(cur realm, addr address) {\n\tif err := adminGroup.AddMember(0, cur, addr); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// RemoveAdmin removes an admin from the Admin Group.\n// The primary owner cannot be removed.\nfunc RemoveAdmin(cur realm, addr address) {\n\tif err := adminGroup.RemoveMember(0, cur, addr); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Owner returns the current owner of the claims contract.\nfunc Owner() address {\n\treturn adminGroup.Owner()\n}\n\n// BackupOwners returns the current backup owners of the claims contract.\nfunc BackupOwners() []string {\n\treturn adminGroup.BackupOwners()\n}\n\n// Admins returns the current admin members of the claims contract.\nfunc Admins() []string {\n\treturn adminGroup.Members()\n}\n\n// IsAdmin checks if an address is in the config adminGroup.\nfunc IsAdmin(addr address) bool {\n\treturn adminGroup.IsMember(addr)\n}\n\n// toMarkdownList formats a slice of strings as a markdown list.\nfunc toMarkdownList(items []string) string {\n\tvar result string\n\tfor _, item := range items {\n\t\tresult += ufmt.Sprintf(\"- %s\\n\", item)\n\t}\n\treturn result\n}\n\nfunc Render(path string) string {\n\towner := adminGroup.Owner().String()\n\tbackupOwners := toMarkdownList(BackupOwners())\n\tadminMembers := toMarkdownList(Admins())\n\treturn ufmt.Sprintf(`\n# Config Dashboard\n\nThis dashboard shows the current configuration owner, backup owners, and admin members.\n- The owner has the exclusive ability to manage the backup owners and admin members.\n- Backup owners can claim ownership of the contract and become the owner.\n- Admin members are used to authorize actions in other realms, such as [my home realm](/r/n2p5/home).\n\n#### Owner\n\n%s\n\n#### Backup Owners\n\n%s\n\n#### Admin Members\n\n%s\n\n`,\n\t\towner,\n\t\tbackupOwners,\n\t\tadminMembers)\n}\n"},{"name":"config_test.gno","body":"package config\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nfunc TestAddBackupOwner(cur realm, t *testing.T) {\n\towner := address(\"g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t\")\n\tu1 := testutils.TestAddress(\"u1\")\n\tu2 := testutils.TestAddress(\"u2\")\n\n\ttesting.SetOriginCaller(owner)\n\tAddBackupOwner(cross(cur), u1)\n\tb := BackupOwners()\n\tif b[1] != u1.String() {\n\t\tt.Error(\"failed to add u1 to backupowners\")\n\t}\n\ttesting.SetOriginCaller(u1)\n\tr := revive(func() {\n\t\tAddBackupOwner(cross(cur), u2)\n\t})\n\tif r != ownable.ErrUnauthorized {\n\t\tt.Error(\"failed to catch unauthorized access\")\n\t}\n\n\ttesting.SetOriginCaller(owner)\n\tRemoveBackupOwner(cross(cur), u1)\n\tRemoveBackupOwner(cross(cur), u2)\n}\n\nfunc TestRemoveBackupOwner(cur realm, t *testing.T) {\n\towner := address(\"g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t\")\n\tu1 := testutils.TestAddress(\"u1\")\n\tu2 := testutils.TestAddress(\"u2\")\n\n\ttesting.SetOriginCaller(owner)\n\tAddBackupOwner(cross(cur), u1)\n\n\ttesting.SetOriginCaller(u2)\n\tr := revive(func() {\n\t\tRemoveBackupOwner(cross(cur), u1)\n\t})\n\tif r != ownable.ErrUnauthorized {\n\t\tt.Error(\"failed to catch unauthorized access\")\n\t}\n\n\ttesting.SetOriginCaller(owner)\n\tRemoveBackupOwner(cross(cur), u1)\n\n\tif len(BackupOwners()) != 1 {\n\t\tt.Error(\"BackupOwners should be length == 1 \")\n\t}\n}\n\nfunc TestClaimOwnership(cur realm, t *testing.T) {\n\towner := address(\"g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t\")\n\tu1 := testutils.TestAddress(\"u1\")\n\n\tif owner != Owner() {\n\t\tt.Errorf(\"expected: %v, got: %v\", owner, Owner())\n\t}\n\n\ttesting.SetOriginCaller(owner)\n\tAddBackupOwner(cross(cur), u1)\n\n\ttesting.SetOriginCaller(u1)\n\tClaimOwnership(cross(cur))\n\n\tif u1 != Owner() {\n\t\tt.Errorf(\"expected: %v, got: %v\", owner, Owner())\n\t}\n\n\ttesting.SetOriginCaller(owner)\n\tClaimOwnership(cross(cur))\n}\n\nfunc TestAddAdmin(cur realm, t *testing.T) {\n\towner := address(\"g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t\")\n\tu1 := testutils.TestAddress(\"u1\")\n\tu2 := testutils.TestAddress(\"u2\")\n\n\ttesting.SetOriginCaller(owner)\n\tAddAdmin(cross(cur), u1)\n\tadmins := Admins()\n\tif admins[1] != u1.String() {\n\t\tt.Error(\"failed to add u1 to admins group\")\n\t}\n\ttesting.SetOriginCaller(u1)\n\tr := revive(func() {\n\t\tAddAdmin(cross(cur), u2)\n\t})\n\tif r != ownable.ErrUnauthorized {\n\t\tt.Error(\"failed to catch unauthorized access\")\n\t}\n\n\t// cleanup\n\ttesting.SetOriginCaller(owner)\n\tRemoveAdmin(cross(cur), u1)\n}\n\nfunc TestRemoveAdmin(cur realm, t *testing.T) {\n\towner := address(\"g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t\")\n\tu1 := testutils.TestAddress(\"u1\")\n\tu2 := testutils.TestAddress(\"u2\")\n\n\ttesting.SetOriginCaller(owner)\n\tAddAdmin(cross(cur), u1)\n\n\ttesting.SetOriginCaller(u2)\n\tr := revive(func() {\n\t\tRemoveAdmin(cross(cur), u1)\n\t})\n\tif r != ownable.ErrUnauthorized {\n\t\tt.Error(\"failed to catch unauthorized access\")\n\t}\n\n\ttesting.SetOriginCaller(owner)\n\tRemoveAdmin(cross(cur), u1)\n\n\tif len(Admins()) != 1 {\n\t\tt.Error(\"Admin should be length == 1 \")\n\t}\n}\n\nfunc TestIsAdmin(cur realm, t *testing.T) {\n\towner := address(\"g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t\")\n\tu1 := testutils.TestAddress(\"u1\")\n\tu2 := testutils.TestAddress(\"u2\")\n\n\ttesting.SetOriginCaller(owner)\n\tAddAdmin(cross(cur), u1)\n\n\tif !IsAdmin(owner) {\n\t\tt.Error(\"owner should be admin\")\n\t}\n\tif !IsAdmin(u1) {\n\t\tt.Error(\"u1 should be admin\")\n\t}\n\tif IsAdmin(u2) {\n\t\tt.Error(\"u2 should not be admin\")\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/n2p5/config\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"23kzxtkdJbaWvn/Zz26kPFcWCrQOvEkBhbFN4VnNWg+6MG2O4T7/57TMCQQOGvuaYQSFoEhBpCRqttMKYdrWDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"haystack","path":"gno.land/r/n2p5/haystack","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/n2p5/haystack\"\ngno = \"0.9\"\n"},{"name":"haystack.gno","body":"package haystack\n\nimport (\n\t\"gno.land/p/n2p5/haystack\"\n)\n\nvar storage = haystack.New()\n\nfunc Render(path string) string {\n\treturn `\nPut a Needle in the Haystack.\n`\n}\n\n// Add takes a fixed-length hex-encoded needle bytes and adds it to the haystack key-value store.\n// If storage encounters an error, it will panic.\nfunc Add(_ realm, needleHex string) {\n\terr := storage.Add(needleHex)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Get takes a fixed-length hex-encoded needle hash and returns the hex-encoded needle bytes.\n// If storage encounters an error, it will panic.\nfunc Get(hashHex string) string {\n\tneedleHex, err := storage.Get(hashHex)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn needleHex\n}\n"},{"name":"haystack_test.gno","body":"package haystack\n\nimport (\n\t\"encoding/hex\"\n\t\"testing\"\n\n\t\"gno.land/p/n2p5/haystack\"\n\t\"gno.land/p/n2p5/haystack/needle\"\n\t\"gno.land/p/nt/testutils/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\n\nfunc TestHaystack(cur realm, t *testing.T) {\n\tt.Parallel()\n\t// needleHex returns a hex-encoded needle and its hash for a given index.\n\tgenNeedleHex := func(i int) (string, string) {\n\t\tb := make([]byte, needle.PayloadLength)\n\t\tb[0] = byte(i)\n\t\tn, _ := needle.New(b)\n\t\treturn hex.EncodeToString(n.Bytes()), hex.EncodeToString(n.Hash())\n\t}\n\n\tu1 := testutils.TestAddress(\"u1\")\n\tu2 := testutils.TestAddress(\"u2\")\n\n\tt.Run(\"Add\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tn1, _ := genNeedleHex(1)\n\t\tn2, _ := genNeedleHex(2)\n\t\tn3, _ := genNeedleHex(3)\n\n\t\ttesting.SetOriginCaller(u1)\n\t\turequire.NotPanics(t, cur, func() { Add(cross(cur), n1) })\n\t\turequire.AbortsWithMessage(t, cur,\n\t\t\thaystack.ErrorDuplicateNeedle.Error(),\n\t\t\tfunc() {\n\t\t\t\tAdd(cross(cur), n1)\n\t\t\t})\n\t\ttesting.SetOriginCaller(u2)\n\t\turequire.NotPanics(t, cur, func() { Add(cross(cur), n2) })\n\t\turequire.NotPanics(t, cur, func() { Add(cross(cur), n3) })\n\t})\n\n\tt.Run(\"Get\", func(t *testing.T) {\n\t\tt.Parallel()\n\n\t\tn1, h1 := genNeedleHex(4)\n\t\t_, h2 := genNeedleHex(5)\n\n\t\ttesting.SetOriginCaller(u1)\n\t\turequire.NotPanics(t, cur, func() { Add(cross(cur), n1) })\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tresult := Get(h1)\n\t\t\turequire.Equal(t, n1, result)\n\t\t})\n\n\t\ttesting.SetOriginCaller(u2)\n\t\turequire.NotPanics(t, cur, func() {\n\t\t\tresult := Get(h1)\n\t\t\turequire.Equal(t, n1, result)\n\t\t})\n\t\turequire.PanicsWithMessage(t, cur,\n\t\t\thaystack.ErrorNeedleNotFound.Error(),\n\t\t\tfunc() {\n\t\t\t\tGet(h2)\n\t\t\t})\n\t})\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"dUgZUXTgRheP7sYHdjIH9ZvoJZ0D7HidtJC3vSOLfMO8Kc0Egt7VXtVdBuzCedtJPZuWJdSDVKpNpQ2UE1DXCQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/n2p5/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/n2p5/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/n2p5/chonk\"\n\n\t\"gno.land/r/leon/hor\"\n\t\"gno.land/r/n2p5/config\"\n)\n\nvar (\n\tactive  = chonk.New()\n\tpreview = chonk.New()\n)\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"n2p5's Home Realm\", \"\")\n\n}\n\n// Add appends a string to the preview Chonk.\nfunc Add(cur realm, chunk string) {\n\tassertAdmin(0, cur)\n\tpreview.Add(chunk)\n}\n\n// Flush clears the preview Chonk.\nfunc Flush(cur realm) {\n\tassertAdmin(0, cur)\n\tpreview.Flush()\n}\n\n// Promote promotes the preview Chonk to the active Chonk\n// and creates a new preview Chonk.\nfunc Promote(cur realm) {\n\tassertAdmin(0, cur)\n\tactive = preview\n\tpreview = chonk.New()\n}\n\n// Render returns the contents of the scanner for the active or preview Chonk\n// based on the path provided.\nfunc Render(path string) string {\n\tvar result string\n\tscanner := getScanner(path)\n\tfor scanner.Scan() {\n\t\tresult += scanner.Text()\n\t}\n\treturn result\n}\n\n// assertAdmin panics if the caller is not an admin as defined in the config realm.\nfunc assertAdmin(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(\"unauthorized: rlm is not the caller's live cur\")\n\t}\n\tcaller := rlm.Previous().Address()\n\tif !config.IsAdmin(caller) {\n\t\tpanic(\"forbidden: must be admin\")\n\t}\n}\n\n// getScanner returns the scanner for the active or preview Chonk based\n// on the path provided.\nfunc getScanner(path string) *chonk.Scanner {\n\tif isPreview(path) {\n\t\treturn preview.Scanner()\n\t}\n\treturn active.Scanner()\n}\n\n// isPreview returns true if the path prefix is \"preview\".\nfunc isPreview(path string) bool {\n\treturn strings.HasPrefix(path, \"preview\")\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"Mjee4fFUmupetsK94b4Y7pj/uMH2GRevbXe85U831odpP8PhoK8eCsGz1OG4z/ftBPICT9OjA4zfV/f4c8byDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"loci","path":"gno.land/r/n2p5/loci","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/n2p5/loci\"\ngno = \"0.9\"\n"},{"name":"loci.gno","body":"package loci\n\nimport (\n\t\"chain\"\n\t\"encoding/base64\"\n\n\t\"gno.land/p/n2p5/loci\"\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nvar store *loci.LociStore\n\nfunc init() {\n\tstore = loci.New()\n}\n\n// Set takes a base64 encoded string and stores it in the Loci store.\n// Keyed by the address of the caller. It also emits a \"set\" event with\n// the address of the caller.\nfunc Set(cur realm, value string) {\n\tb, err := base64.StdEncoding.DecodeString(value)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tstore.Set(0, cur, b)\n\tchain.Emit(\"SetValue\", \"ForAddr\", string(cur.Previous().Address()))\n}\n\n// Get retrieves the value stored at the provided address and\n// returns it as a base64 encoded string.\nfunc Get(_ realm, addr address) string {\n\treturn base64.StdEncoding.EncodeToString(store.Get(addr))\n}\n\nfunc Render(cur realm, path string) string {\n\tif path == \"\" {\n\t\treturn `\n# Welcome to Loci\n\nLoci is a simple key-value store keyed by the caller's gno.land address.\nOnly the caller can set the value for their address, but anyone can\nretrieve the value for any address. There are only two functions: Set and Get.\nIf you'd like to set a value, simply base64 encode any message you'd like and\nit will be stored in in Loci. If you'd like to retrieve a value, simply provide\nthe address of the value you'd like to retrieve.\n\nFor convenience, you can also use gnoweb to view the value for a given address,\nif one exists. For instance append :g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t to\nthis URL to view the value stored at that address.\n`\n\t}\n\treturn renderGet(cross(cur), address(path))\n}\n\nfunc renderGet(cur realm, addr address) string {\n\tvalue := \"```\\n\" + Get(cur, addr) + \"\\n```\"\n\n\treturn ufmt.Sprintf(`\n# Loci Value Viewer\n\n**Address:** %s\n\n%s\n\n`, addr, value)\n}\n"},{"name":"z_0_filetest.gno","body":"// PKGPATH: gno.land/r/test/test\npackage test\n\nimport (\n\t\"gno.land/r/n2p5/loci\"\n)\n\nfunc main(cur realm) {\n\tcaller := cur\n\tprintln(\"caller: \" + string(caller.Address()))\n\n\t// test nothing being set, yet.\n\tr0 := loci.Get(cross(cur), caller.Address())\n\tprintln(\"expect: \" + \"\")\n\tprintln(\"got   : \" + r0)\n\n\t// set the value, which uses the CurrentRealm as the caller.\n\tinput1 := \"aGVsbG8sIHdvcmxkCg==\"\n\tloci.Set(cross(cur), input1)\n\tprintln(\"set   : \" + string(input1))\n\tr1 := loci.Get(cross(cur), caller.Address())\n\tprintln(\"expect: \" + input1)\n\tprintln(\"got   : \" + r1)\n\n\t// change the value, which should override the previous value.\n\tinput2 := \"Z29vZGJ5ZSwgd29ybGQK\"\n\tloci.Set(cross(cur), input2)\n\tprintln(\"set   : \" + string(input2))\n\tr2 := loci.Get(cross(cur), caller.Address())\n\tprintln(\"expect: \" + input2)\n\tprintln(\"got   : \" + r2)\n\n}\n\n// Output:\n// caller: g1z7fga7u94pdmamlvcrtvsfwxgsye0qv3rres7n\n// expect:\n// got   :\n// set   : aGVsbG8sIHdvcmxkCg==\n// expect: aGVsbG8sIHdvcmxkCg==\n// got   : aGVsbG8sIHdvcmxkCg==\n// set   : Z29vZGJ5ZSwgd29ybGQK\n// expect: Z29vZGJ5ZSwgd29ybGQK\n// got   : Z29vZGJ5ZSwgd29ybGQK\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"1euT9cgrFjS3siHjR5nFChqvl+/bpJjW/GT27kzEbn1l7KkW7bzobyzjEUT2hnvNTOg5OSEZOQrdAvg6Ej2kAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"config","path":"gno.land/r/nemanya/config","files":[{"name":"config.gno","body":"package config\n\nimport \"errors\"\n\nvar (\n\tmain   address\n\tbackup address\n\n\tErrInvalidAddr  = errors.New(\"Invalid address\")\n\tErrUnauthorized = errors.New(\"Unauthorized\")\n)\n\nfunc init() {\n\tmain = \"g1x9qyf6f34v2g52k4q5smn5tctmj3hl2kj7l2ql\"\n}\n\nfunc Address() address {\n\treturn main\n}\n\nfunc Backup() address {\n\treturn backup\n}\n\nfunc SetAddress(cur realm, a address) error {\n\tif !a.IsValid() {\n\t\treturn ErrInvalidAddr\n\t}\n\n\tif err := checkAuthorized(0, cur); err != nil {\n\t\treturn err\n\t}\n\n\tmain = a\n\treturn nil\n}\n\nfunc SetBackup(cur realm, a address) error {\n\tif !a.IsValid() {\n\t\treturn ErrInvalidAddr\n\t}\n\n\tif err := checkAuthorized(0, cur); err != nil {\n\t\treturn err\n\t}\n\n\tbackup = a\n\treturn nil\n}\n\nfunc checkAuthorized(_ int, rlm realm) error {\n\tif !rlm.IsCurrent() {\n\t\treturn ErrUnauthorized\n\t}\n\tcaller := rlm.Previous().Address()\n\tisAuthorized := caller == main || caller == backup\n\n\tif !isAuthorized {\n\t\treturn ErrUnauthorized\n\t}\n\n\treturn nil\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/nemanya/config\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"wfT4msBiHujwif/6EejR/CwpprzBtrFNqEiRYTclRuTA0z1SVBbNIOUw1qyZoe7kfiO0VGu8C+QLRAxD80rCAg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/nemanya/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/nemanya/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/r/nemanya/config\"\n)\n\ntype SocialLink struct {\n\tURL  string\n\tText string\n}\n\ntype Sponsor struct {\n\tAddress address\n\tAmount  chain.Coins\n}\n\ntype Project struct {\n\tName        string\n\tDescription string\n\tURL         string\n\tImageURL    string\n\tSponsors    map[address]Sponsor\n}\n\nvar (\n\ttextArt        string\n\taboutMe        string\n\tsponsorInfo    string\n\tsocialLinks    map[string]SocialLink\n\tgnoProjects    map[string]Project\n\totherProjects  map[string]Project\n\ttotalDonations chain.Coins\n)\n\nfunc init() {\n\ttextArt = renderTextArt()\n\taboutMe = \"I am a student of IT at Faculty of Sciences in Novi Sad, Serbia. My background is mainly in web and low-level programming, but since Web3 Bootcamp at Petnica this year I've been actively learning about blockchain and adjacent technologies. I am excited about contributing to the gno.land ecosystem and learning from the community.\\n\\n\"\n\tsponsorInfo = \"You can sponsor a project by sending GNOT to this address. Your sponsorship will be displayed on the project page. Thank you for supporting the development of gno.land!\\n\\n\"\n\n\tsocialLinks = map[string]SocialLink{\n\t\t\"GitHub\":   {URL: \"https://github.com/Nemanya8\", Text: \"Explore my repositories and open-source contributions.\"},\n\t\t\"LinkedIn\": {URL: \"https://www.linkedin.com/in/nemanjamatic/\", Text: \"Connect with me professionally.\"},\n\t\t\"Email Me\": {URL: \"mailto:matic.nemanya@gmail.com\", Text: \"Reach out for collaboration or inquiries.\"},\n\t}\n\n\tgnoProjects = make(map[string]Project)\n\totherProjects = make(map[string]Project)\n\n\tgnoProjects[\"Liberty Bridge\"] = Project{\n\t\tName:        \"Liberty Bridge\",\n\t\tDescription: \"Liberty Bridge was my first Web3 project, developed as part of the Web3 Bootcamp at Petnica. This project served as a centralized bridge between Ethereum and gno.land, enabling seamless asset transfers and fostering interoperability between the two ecosystems.\\n\\n The primary objective of Liberty Bridge was to address the challenges of connecting decentralized networks by implementing a user-friendly solution that simplified the process for users. The project incorporated mechanisms to securely transfer assets between the Ethereum and gno.land blockchains, ensuring efficiency and reliability while maintaining a centralized framework for governance and operations.\\n\\n Through this project, I gained hands-on knowledge of blockchain interoperability, Web3 protocols, and the intricacies of building solutions that bridge different blockchain ecosystems.\\n\\n\",\n\t\tURL:         \"https://gno.land\",\n\t\tImageURL:    \"https://github.com/Milosevic02/LibertyBridge/raw/main/lb_banner.png\",\n\t\tSponsors:    make(map[address]Sponsor),\n\t}\n\n\totherProjects[\"Incognito\"] = Project{\n\t\tName:        \"Incognito\",\n\t\tDescription: \"Incognito is a Web3 platform built for Ethereum-based chains, designed to connect advertisers with users in a privacy-first and mutually beneficial way. Its modular architecture makes it easily expandable to other blockchains. Developed during the ETH Sofia Hackathon, it was recognized as a winning project for its innovation and impact.\\n\\n The platform allows advertisers to send personalized ads while sharing a portion of the marketing budget with users. It uses machine learning to match users based on wallet activity, ensuring precise targeting. User emails are stored securely on-chain and never shared, prioritizing privacy and transparency.\\n\\n With all campaign data stored on-chain, Incognito ensures decentralization and accountability. By rewarding users and empowering advertisers, it sets a new standard for fair and transparent blockchain-based advertising.\",\n\t\tURL:         \"https://github.com/Milosevic02/Incognito-ETHSofia\",\n\t\tImageURL:    \"\",\n\t\tSponsors:    make(map[address]Sponsor),\n\t}\n}\n\nfunc Render(_ string) string {\n\tvar sb strings.Builder\n\tsb.WriteString(\"# Hi, I'm\\n\")\n\tsb.WriteString(textArt)\n\tsb.WriteString(\"---\\n\")\n\tsb.WriteString(\"## About me\\n\")\n\tsb.WriteString(aboutMe)\n\tsb.WriteString(sponsorInfo)\n\tsb.WriteString(ufmt.Sprintf(\"# Total Sponsor Donations: %s\\n\", totalDonations.String()))\n\tsb.WriteString(\"---\\n\")\n\tsb.WriteString(renderProjects(gnoProjects, \"Gno Projects\"))\n\tsb.WriteString(\"---\\n\")\n\tsb.WriteString(renderProjects(otherProjects, \"Other Projects\"))\n\tsb.WriteString(\"---\\n\")\n\tsb.WriteString(renderSocialLinks())\n\n\treturn sb.String()\n}\n\nfunc renderTextArt() string {\n\tvar sb strings.Builder\n\tsb.WriteString(\"```\\n\")\n\tsb.WriteString(\"      ___           ___           ___           ___           ___           ___           ___     \\n\")\n\tsb.WriteString(\"     /\\\\__\\\\         /\\\\  \\\\         /\\\\__\\\\         /\\\\  \\\\         /\\\\__\\\\         |\\\\__\\\\         /\\\\  \\\\    \\n\")\n\tsb.WriteString(\"    /::|  |       /::\\\\  \\\\       /::|  |       /::\\\\  \\\\       /::|  |        |:|  |       /::\\\\  \\\\   \\n\")\n\tsb.WriteString(\"   /:|:|  |      /:/\\\\:\\\\  \\\\     /:|:|  |      /:/\\\\:\\\\  \\\\     /:|:|  |        |:|  |      /:/\\\\:\\\\  \\\\  \\n\")\n\tsb.WriteString(\"  /:/|:|  |__   /::\\\\~\\\\:\\\\  \\\\   /:/|:|__|__   /::\\\\~\\\\:\\\\  \\\\   /:/|:|  |__      |:|__|__   /::\\\\~\\\\:\\\\  \\\\ \\n\")\n\tsb.WriteString(\" /:/ |:| /\\\\__\\\\ /:/\\\\:\\\\ \\\\:\\\\__\\\\ /:/ |::::\\\\__\\\\ /:/\\\\:\\\\ \\\\:\\\\__\\\\ /:/ |:| /\\\\__\\\\     /::::\\\\__\\\\ /:/\\\\:\\\\ \\\\:\\\\__\\\\\\n\")\n\tsb.WriteString(\" \\\\/__|:|/:/  / \\\\:\\\\~\\\\:\\\\ \\\\/__/ \\\\/__/~~/:/  / \\\\/__\\\\:\\\\/:/  / \\\\/__|:|/:/  /    /:/~~/~    \\\\/__\\\\:\\\\/:/  / \\n\")\n\tsb.WriteString(\"     |:/:/  /   \\\\:\\\\ \\\\:\\\\__\\\\         /:/  /       \\\\::/  /      |:/:/  /    /:/  /           \\\\::/  /  \\n\")\n\tsb.WriteString(\"     |::/  /     \\\\:\\\\ \\\\/__/        /:/  /        /:/  /       |::/  /     \\\\/__/            /:/  /   \\n\")\n\tsb.WriteString(\"     /:/  /       \\\\:\\\\__\\\\         /:/  /        /:/  /        /:/  /                      /:/  /    \\n\")\n\tsb.WriteString(\"     \\\\/__/         \\\\/__/         \\\\/__/         \\\\/__/         \\\\/__/                       \\\\/__/     \\n\")\n\tsb.WriteString(\"\\n```\\n\")\n\treturn sb.String()\n}\n\nfunc renderSocialLinks() string {\n\tvar sb strings.Builder\n\tsb.WriteString(\"## Links\\n\\n\")\n\tsb.WriteString(\"You can find me here:\\n\\n\")\n\tsb.WriteString(ufmt.Sprintf(\"- [GitHub](%s) - %s\\n\", socialLinks[\"GitHub\"].URL, socialLinks[\"GitHub\"].Text))\n\tsb.WriteString(ufmt.Sprintf(\"- [LinkedIn](%s) - %s\\n\", socialLinks[\"LinkedIn\"].URL, socialLinks[\"LinkedIn\"].Text))\n\tsb.WriteString(ufmt.Sprintf(\"- [Email Me](%s) - %s\\n\", socialLinks[\"Email Me\"].URL, socialLinks[\"Email Me\"].Text))\n\tsb.WriteString(\"\\n\")\n\treturn sb.String()\n}\n\nfunc renderProjects(projectsMap map[string]Project, title string) string {\n\tvar sb strings.Builder\n\tsb.WriteString(ufmt.Sprintf(\"## %s\\n\\n\", title))\n\tfor _, project := range projectsMap {\n\t\tif project.ImageURL != \"\" {\n\t\t\tsb.WriteString(ufmt.Sprintf(\"![%s](%s)\\n\\n\", project.Name, project.ImageURL))\n\t\t}\n\t\tsb.WriteString(ufmt.Sprintf(\"### [%s](%s)\\n\\n\", project.Name, project.URL))\n\t\tsb.WriteString(project.Description + \"\\n\\n\")\n\n\t\tif len(project.Sponsors) \u003e 0 {\n\t\t\tsb.WriteString(ufmt.Sprintf(\"#### %s Sponsors\\n\", project.Name))\n\t\t\tfor _, sponsor := range project.Sponsors {\n\t\t\t\tsb.WriteString(ufmt.Sprintf(\"- %s: %s\\n\", sponsor.Address.String(), sponsor.Amount.String()))\n\t\t\t}\n\t\t\tsb.WriteString(\"\\n\")\n\t\t}\n\t}\n\treturn sb.String()\n}\n\nfunc UpdateLink(cur realm, name, newURL string) {\n\tif !isAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tif _, exists := socialLinks[name]; !exists {\n\t\tpanic(\"Link with the given name does not exist\")\n\t}\n\n\tsocialLinks[name] = SocialLink{\n\t\tURL:  newURL,\n\t\tText: socialLinks[name].Text,\n\t}\n}\n\nfunc UpdateAboutMe(cur realm, text string) {\n\tif !isAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\taboutMe = text\n}\n\nfunc AddGnoProject(cur realm, name, description, url, imageURL string) {\n\tif !isAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\tproject := Project{\n\t\tName:        name,\n\t\tDescription: description,\n\t\tURL:         url,\n\t\tImageURL:    imageURL,\n\t\tSponsors:    make(map[address]Sponsor),\n\t}\n\tgnoProjects[name] = project\n}\n\nfunc DeleteGnoProject(cur realm, projectName string) {\n\tif !isAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tif _, exists := gnoProjects[projectName]; !exists {\n\t\tpanic(\"Project not found\")\n\t}\n\n\tdelete(gnoProjects, projectName)\n}\n\nfunc AddOtherProject(cur realm, name, description, url, imageURL string) {\n\tif !isAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\tproject := Project{\n\t\tName:        name,\n\t\tDescription: description,\n\t\tURL:         url,\n\t\tImageURL:    imageURL,\n\t\tSponsors:    make(map[address]Sponsor),\n\t}\n\totherProjects[name] = project\n}\n\nfunc RemoveOtherProject(cur realm, projectName string) {\n\tif !isAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tif _, exists := otherProjects[projectName]; !exists {\n\t\tpanic(\"Project not found\")\n\t}\n\n\tdelete(otherProjects, projectName)\n}\n\nfunc isAuthorized(addr address) bool {\n\treturn addr == config.Address() || addr == config.Backup()\n}\n\nfunc SponsorGnoProject(cur realm, projectName string) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\taddress_XXX := unsafe.OriginCaller()\n\tamount := unsafe.OriginSend()\n\n\tif amount.AmountOf(\"ugnot\") == 0 {\n\t\tpanic(\"Donation must include GNOT\")\n\t}\n\n\tproject, exists := gnoProjects[projectName]\n\tif !exists {\n\t\tpanic(\"Gno project not found\")\n\t}\n\n\tproject.Sponsors[address_XXX] = Sponsor{\n\t\tAddress: address_XXX,\n\t\tAmount:  project.Sponsors[address_XXX].Amount.Add(amount),\n\t}\n\n\ttotalDonations = totalDonations.Add(amount)\n\n\tgnoProjects[projectName] = project\n}\n\nfunc SponsorOtherProject(cur realm, projectName string) {\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"only user-call (maketx call) accepted\")\n\t}\n\taddress_XXX := unsafe.OriginCaller()\n\tamount := unsafe.OriginSend()\n\n\tif amount.AmountOf(\"ugnot\") == 0 {\n\t\tpanic(\"Donation must include GNOT\")\n\t}\n\n\tproject, exists := otherProjects[projectName]\n\tif !exists {\n\t\tpanic(\"Other project not found\")\n\t}\n\n\tproject.Sponsors[address_XXX] = Sponsor{\n\t\tAddress: address_XXX,\n\t\tAmount:  project.Sponsors[address_XXX].Amount.Add(amount),\n\t}\n\n\ttotalDonations = totalDonations.Add(amount)\n\n\totherProjects[projectName] = project\n}\n\nfunc Withdraw(cur realm) string {\n\trealmAddress := cur.Previous().Address()\n\tif !isAuthorized(realmAddress) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\tcoins := banker_.GetCoins(realmAddress)\n\n\tif len(coins) == 0 {\n\t\treturn \"No coins available to withdraw\"\n\t}\n\n\tbanker_.SendCoins(realmAddress, config.Address(), coins)\n\n\treturn \"Successfully withdrew all coins to config address\"\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"tnW3g+wMSP89Nbp4Bf9Vz5qNmdu9pUb/3ds3g7FQJxbKJSRcl3DcvwydiWItQC/H6p3qwaQVCE4cu+8fxSpCAQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l","package":{"name":"commondao","path":"gno.land/r/nt/commondao/v0","files":[{"name":"README.md","body":"\u003e **v0 - Unaudited**\n\u003e This is an initial version of this realm that has not yet been formally audited.\n\u003e A fully audited version will be published as a subsequent release.\n\u003e Use in production at your own risk.\n\n# commondao (realm)\n\nReference realm implementation of the `commondao` package for managing Decentralized Autonomous Organizations.\n"},{"name":"commondao.gno","body":"package commondao\n\nimport (\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/p/nt/bptree/v0\"\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/seqid/v0\"\n)\n\n// CommonDAOID is the ID of the realm's DAO.\nconst CommonDAOID uint64 = 1\n\nvar (\n\tdaoID     seqid.ID\n\trealmLink = txlink.Realm(\"gno.land/r/nt/commondao/v0\")\n\tdaos      = bptree.NewBPTree32() // string(ID) -\u003e *commondao.CommonDAO\n\townership = bptree.NewBPTree32() // string(address) -\u003e []uint64(DAO ID)\n\tinvites   = bptree.NewBPTree32() // string(address) -\u003e address(inviter)\n\ttrees     = bptree.NewBPTree32() // string(root ID) -\u003e bptree.BPTree(string(DAO ID) -\u003e *commondao.CommonDAO)\n\toptions   = bptree.NewBPTree32() // string(ID) -\u003e *Options\n)\n\nfunc getDAO(daoID uint64) *commondao.CommonDAO {\n\tkey := makeIDKey(daoID)\n\tif v := daos.Get(key); v != nil {\n\t\treturn v.(*commondao.CommonDAO)\n\t}\n\treturn nil\n}\n\nfunc mustGetDAO(daoID uint64) *commondao.CommonDAO {\n\tdao := getDAO(daoID)\n\tif dao == nil {\n\t\tpanic(\"DAO not found\")\n\t}\n\treturn dao\n}\n\nfunc getTree(rootID uint64) *bptree.BPTree {\n\tkey := makeIDKey(rootID)\n\tif v := trees.Get(key); v != nil {\n\t\treturn v.(*bptree.BPTree)\n\t}\n\treturn nil\n}\n\nfunc getOwnership(addr address) []uint64 {\n\tif v := ownership.Get(addr.String()); v != nil {\n\t\treturn v.([]uint64)\n\t}\n\treturn nil\n}\n\nfunc getOptions(daoID uint64) *Options {\n\tkey := makeIDKey(daoID)\n\tif v := options.Get(key); v != nil {\n\t\treturn v.(*Options)\n\t}\n\treturn nil\n}\n\nfunc makeIDKey(daoID uint64) string {\n\treturn seqid.ID(daoID).String()\n}\n\nfunc createDAO(name string, owner address, o []Option) *commondao.CommonDAO {\n\tid := daoID.Next()\n\tdao := commondao.New(\n\t\tcommondao.WithID(uint64(id)),\n\t\tcommondao.WithName(name),\n\t)\n\n\tdaoOptions := defaultOptions\n\tfor _, apply := range o {\n\t\tapply(\u0026daoOptions)\n\t}\n\n\tids := append(getOwnership(owner), uint64(id))\n\townership.Set(owner.String(), ids)\n\n\tdaos.Set(id.String(), dao)\n\toptions.Set(id.String(), \u0026daoOptions)\n\treturn dao\n}\n\nfunc createSubDAO(parent *commondao.CommonDAO, name string, o []Option) *commondao.CommonDAO {\n\tid := daoID.Next()\n\tdao := commondao.New(\n\t\tcommondao.WithID(uint64(id)),\n\t\tcommondao.WithParent(parent),\n\t\tcommondao.WithName(name),\n\t)\n\n\tdaoOptions := defaultOptions\n\tfor _, apply := range o {\n\t\tapply(\u0026daoOptions)\n\t}\n\n\toptions.Set(id.String(), \u0026daoOptions)\n\tdaos.Set(id.String(), dao)\n\n\trootID := parent.TopParent().ID()\n\ttree := getTree(rootID)\n\tif tree == nil {\n\t\ttree = bptree.NewBPTree32()\n\t\ttrees.Set(makeIDKey(rootID), tree)\n\t}\n\n\ttree.Set(id.String(), dao)\n\treturn dao\n}\n"},{"name":"doc.gno","body":"// v0 - Unaudited: This is an initial version that has not yet been formally audited.\n// A fully audited version will be published as a subsequent release.\n// Use in production at your own risk.\n//\n// Package commondao provides a reference realm implementation of the commondao\n// package for managing Decentralized Autonomous Organizations.\npackage commondao\n"},{"name":"genesis.gno","body":"package commondao\n\nimport (\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nfunc init(cur realm) {\n\t// Create a DAO for the realm\n\tid := daoID.Next()\n\tdao := commondao.New(\n\t\tcommondao.WithID(uint64(id)),\n\t\tcommondao.WithName(\"Common DAO\"),\n\t\tcommondao.WithDescription(\n\t\t\t\"This DAO is responsible for managing `commondao` realm functionalities.\",\n\t\t),\n\t)\n\n\t// Add initial members\n\tdao.Members().Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tdao.Members().Add(\"g1manfred47kzduec920z88wfr64ylksmdcedlf5\") // @moul\n\n\t// Index DAO\n\tdaos.Set(id.String(), dao)\n\n\t// Make commondao realm the owner of Common DAO.\n\t// cur is the chain-injected init cur for this realm; cur.Address() is\n\t// the commondao realm's package address.\n\townership.Set(cur.Address().String(), []uint64{CommonDAOID})\n\n\t// Save DAO configuration\n\toptions.Set(id.String(), \u0026Options{\n\t\tAllowListing:       true,\n\t\tAllowRender:        true,\n\t\tAllowVoting:        true,\n\t\tAllowExecution:     true,\n\t\tAllowMembersUpdate: true,\n\t})\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/nt/commondao/v0\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l\"\n"},{"name":"iterator.gno","body":"package commondao\n\nimport (\n\t\"gno.land/p/nt/bptree/v0/rotree\"\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// NewIterator returns a SubDAO iterator.\nfunc NewIterator(cur realm, rootID uint64, options ...IteratorOption) Iterator {\n\tassertIsOwner(cur.Previous().Address(), rootID)\n\n\ttree := getTree(rootID)\n\tif tree == nil {\n\t\treturn Iterator{}\n\t}\n\n\tit := Iterator{tree: tree}\n\tfor _, apply := range options {\n\t\tapply(\u0026it)\n\t}\n\n\tsize := it.tree.Size()\n\tif it.count \u003e 0 {\n\t\tit.maxIndex = it.current + it.count\n\t\tif it.maxIndex \u003e size {\n\t\t\tit.maxIndex = size\n\t\t}\n\t} else {\n\t\tit.maxIndex = size\n\t}\n\n\tit.count = it.maxIndex - it.current\n\n\treturn it\n}\n\n// Iterator defines an iterator of SubDAOs.\ntype Iterator struct {\n\ttree                     rotree.IReadOnlyTree\n\tcount, current, maxIndex int\n\tpath                     string\n\tdao                      *commondao.CommonDAO\n}\n\n// Count returns the number of DAOs to iterate.\nfunc (it Iterator) Count() int {\n\treturn it.count\n}\n\n// Next returns true when a new DAO is available.\nfunc (it *Iterator) Next() bool {\n\tif it.tree == nil || it.current == it.maxIndex {\n\t\treturn false\n\t}\n\n\tpath, v := it.tree.GetByIndex(it.current)\n\tif v == nil {\n\t\treturn false\n\t}\n\n\tit.dao = v.(*commondao.CommonDAO)\n\tit.path = path\n\tit.current++\n\treturn true\n}\n\n// DAO returns the current DAO.\nfunc (it Iterator) DAO() *commondao.CommonDAO {\n\treturn it.dao\n}\n\n// Path returns the current DAO path.\nfunc (it Iterator) Path() string {\n\treturn it.path\n}\n"},{"name":"iterator_options.gno","body":"package commondao\n\n// IteratorOption configures iterators.\ntype IteratorOption func(*Iterator)\n\n// WithOffset assigns a initial offset to start iterating DAOs.\n// Offset can be used to reduce the number of iterated DAOs for example during pagination.\nfunc WithOffset(offset uint) IteratorOption {\n\treturn func(it *Iterator) {\n\t\tit.current = int(offset)\n\t}\n}\n\n// WithCount assigns a number to limit the number of iterated DAOs.\n// A zero count means to iterates until the last DAO.\nfunc WithCount(count uint) IteratorOption {\n\treturn func(it *Iterator) {\n\t\tit.count = int(count)\n\t}\n}\n"},{"name":"options.gno","body":"package commondao\n\n// TODO: List default DAO options somewhere to make devs aware of them\nvar defaultOptions = Options{\n\tAllowRender:    true,\n\tAllowChildren:  true,\n\tAllowVoting:    true,\n\tAllowExecution: true,\n}\n\n// Option configures DAO options within CommonDAO realm.\ntype Option func(*Options)\n\n// Options configures DAO behavior within CommonDAO realm.\n// These options are only valid for the CommonDAO ream, dApps that\n// own a DAO can implement these features by themselves.\ntype Options struct {\n\t// AllowListing adds the DAO to the public list of realm DAOs.\n\tAllowListing bool\n\n\t// AllowRender enables support for rendering the DAO within the CommonDAO realm.\n\tAllowRender bool\n\n\t// AllowChildren enables hierarchical DAOs by allowing the creation of SubDAOs.\n\tAllowChildren bool\n\n\t// AllowVoting enables proposal voting support.\n\tAllowVoting bool\n\n\t// AllowExecution enables proposal execution support.\n\tAllowExecution bool\n\n\t// AllowMembersUpdate allows creating a proposal to update DAO members.\n\tAllowMembersUpdate bool\n\n\t// AllowTextProposals allows text-only, non-executable proposals.\n\tAllowTextProposals bool\n\n\t// AllowSubDAOProposals allows creating SubDAOs though proposals.\n\tAllowSubDAOProposals bool\n\n\t// AllowDissolutionProposals allows dissolving DAOs though proposals.\n\tAllowDissolutionProposals bool\n}\n\n// SetAllowListing toggles DAO public listing support.\nfunc (o *Options) SetAllowListing(allow bool) {\n\to.AllowListing = allow\n}\n\n// SetAllowRender toggles DAO render support within the CommonDAO realm.\nfunc (o *Options) SetAllowRender(allow bool) {\n\to.AllowRender = allow\n}\n\n// SetAllowChildren toggles DAO support for SubDAOs.\n// Enabling child DAOs is necessary for hierarchical tree based DAOs.\nfunc (o *Options) SetAllowChildren(allow bool) {\n\to.AllowChildren = allow\n}\n\n// SetAllowVoting toggles proposal voting support though the CommonDAO realm.\nfunc (o *Options) SetAllowVoting(allow bool) {\n\to.AllowVoting = allow\n}\n\n// SetAllowExecution toggles proposal execution support though the CommonDAO realm.\nfunc (o *Options) SetAllowExecution(allow bool) {\n\to.AllowExecution = allow\n}\n\n// SetAllowMembersUpdate toggles support for DAO members update proposals.\n// This type of proposal allows adding or removing DAO members.\nfunc (o *Options) SetAllowMembersUpdate(allow bool) {\n\to.AllowMembersUpdate = allow\n}\n\n// SetAllowTextProposals toggles support generic text proposals.\nfunc (o *Options) SetAllowTextProposals(allow bool) {\n\to.AllowTextProposals = allow\n}\n\n// SetAllowSubDAOPorposals toggles support for SubDAO creation proposals.\n// SubDAOs can only be created when DAO children support is enabled.\nfunc (o *Options) SetAllowSubDAOPorposals(allow bool) {\n\to.AllowSubDAOProposals = allow\n}\n\n// SetAllowDissolutionProposals toggles support for DAO dissolution proposals.\nfunc (o *Options) SetAllowDissolutionProposals(allow bool) {\n\to.AllowDissolutionProposals = allow\n}\n\n// AllowListing configures DAO listing within CommonDAO realm.\nfunc AllowListing(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowListing = allow\n\t}\n}\n\n// AllowRender configures DAO, proposals and votes rendering within CommonDAO.\nfunc AllowRender(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowRender = allow\n\t}\n}\n\n// AllowChildren configures DAO support for SubDAOs.\nfunc AllowChildren(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowChildren = allow\n\t}\n}\n\n// AllowVoting configures DAO support for voting through CommonDAO realm.\nfunc AllowVoting(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowVoting = allow\n\t}\n}\n\n// AllowExecution configures DAO support for executing proposals through CommonDAO realm.\nfunc AllowExecution(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowExecution = allow\n\t}\n}\n\n// AllowMembersUpdate configures support for DAO members update proposals.\nfunc AllowMembersUpdate(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowMembersUpdate = allow\n\t}\n}\n\n// AllowTextProposals configures support generic text proposals.\nfunc AllowTextProposals(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowTextProposals = allow\n\t}\n}\n\n// AllowSubDAOPorposals configures support for SubDAO creation proposals.\nfunc AllowSubDAOPorposals(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowSubDAOProposals = allow\n\t}\n}\n\n// AllowDissolutionProposals configures support for DAO dissolution proposals.\nfunc AllowDissolutionProposals(allow bool) Option {\n\treturn func(o *Options) {\n\t\to.AllowDissolutionProposals = allow\n\t}\n}\n"},{"name":"proposal_members.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// newMembersUpdatePropDefinition creates a new proposal definition for adding/removing DAO members.\nfunc newMembersUpdatePropDefinition(dao *commondao.CommonDAO, add, remove addrset.Set) membersUpdatePropDefinition {\n\tif dao == nil {\n\t\tpanic(\"DAO is required\")\n\t}\n\n\tif dao.Members().Size() == 0 {\n\t\tpanic(\"a DAO with at least one member is required to create member update proposals\")\n\t}\n\n\tif add.Size() == 0 \u0026\u0026 remove.Size() == 0 {\n\t\tpanic(\"no members were specified to be added or removed\")\n\t}\n\n\treturn membersUpdatePropDefinition{\n\t\tdao:      dao,\n\t\ttoAdd:    add,\n\t\ttoRemove: remove,\n\t}\n}\n\n// membersUpdatePropDefinition defines a proposal type for adding/removing DAO members.\ntype membersUpdatePropDefinition struct {\n\tdao             *commondao.CommonDAO\n\ttoAdd, toRemove addrset.Set\n}\n\nfunc (membersUpdatePropDefinition) Title() string               { return \"Members Update\" }\nfunc (membersUpdatePropDefinition) VotingPeriod() time.Duration { return time.Hour * 24 * 7 }\n\nfunc (p membersUpdatePropDefinition) Body() string {\n\tvar b strings.Builder\n\n\tif p.toAdd.Size() \u003e 0 {\n\t\titems := make([]string, 0, p.toAdd.Size())\n\t\tp.toAdd.IterateByOffset(0, p.toAdd.Size(), func(addr address) bool {\n\t\t\titems = append(items, addr.String())\n\t\t\treturn false\n\t\t})\n\n\t\tb.WriteString(md.Paragraph(\n\t\t\tmd.Bold(\"Members to Add:\") + \"\\n\" + md.BulletList(items),\n\t\t))\n\t}\n\n\tif p.toRemove.Size() \u003e 0 {\n\t\titems := make([]string, 0, p.toRemove.Size())\n\t\tp.toRemove.IterateByOffset(0, p.toRemove.Size(), func(addr address) bool {\n\t\t\titems = append(items, addr.String())\n\t\t\treturn false\n\t\t})\n\n\t\tb.WriteString(md.Paragraph(\n\t\t\tmd.Bold(\"Members to Remove:\") + \"\\n\" + md.BulletList(items),\n\t\t))\n\t}\n\n\treturn b.String()\n}\n\nfunc (p membersUpdatePropDefinition) Validate() (err error) {\n\tmembers := p.dao.Members()\n\n\tp.toAdd.IterateByOffset(0, p.toAdd.Size(), func(addr address) bool {\n\t\tif members.Has(addr) {\n\t\t\terr = errors.New(\"address is already a DAO member: \" + addr.String())\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tp.toRemove.IterateByOffset(0, p.toRemove.Size(), func(addr address) bool {\n\t\tif !members.Has(addr) {\n\t\t\terr = errors.New(\"address is not a DAO member: \" + addr.String())\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\n\treturn err\n}\n\nfunc (membersUpdatePropDefinition) Tally(ctx commondao.VotingContext) (bool, error) {\n\t// When DAO has one or two members succeed when there is a YES vote, otherwise\n\t// tally requires at least three votes to be able to tally by 2/3s super majority\n\tif ctx.Members.Size() \u003c 3 {\n\t\treturn ctx.VotingRecord.VoteCount(commondao.ChoiceYes) \u003e 0, nil\n\t}\n\n\tif !commondao.IsQuorumReached(commondao.QuorumTwoThirds, ctx.VotingRecord, ctx.Members) {\n\t\treturn false, commondao.ErrNoQuorum\n\t}\n\n\tc, success := commondao.SelectChoiceBySuperMajority(ctx.VotingRecord, ctx.Members.Size())\n\tif success {\n\t\treturn c == commondao.ChoiceYes, nil\n\t}\n\treturn false, nil\n}\n\nfunc (p membersUpdatePropDefinition) Executor() commondao.ExecFunc {\n\treturn p.execute\n}\n\nfunc (p membersUpdatePropDefinition) execute(realm) error {\n\tmembers := p.dao.Members()\n\n\tp.toAdd.IterateByOffset(0, p.toAdd.Size(), func(addr address) bool {\n\t\tmembers.Add(addr)\n\t\treturn false\n\t})\n\n\tp.toRemove.IterateByOffset(0, p.toRemove.Size(), func(addr address) bool {\n\t\tmembers.Remove(addr)\n\t\treturn false\n\t})\n\n\treturn nil\n}\n"},{"name":"proposal_members_test.gno","body":"package commondao\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nvar _ commondao.ProposalDefinition = (*membersUpdatePropDefinition)(nil)\n\nfunc TestMembersPropDefinitionTally(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tmembers []address\n\t\tvotes   []commondao.Vote\n\t\terr     error\n\t\tsuccess bool\n\t}{\n\t\t{\n\t\t\tname: \"succeed\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsuccess: true,\n\t\t},\n\t\t{\n\t\t\tname: \"fail\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"no quorum\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\terr: commondao.ErrNoQuorum,\n\t\t},\n\t\t{\n\t\t\tname: \"succeed with two members\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsuccess: true,\n\t\t},\n\t\t{\n\t\t\tname: \"fail with two members\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar (\n\t\t\t\tp       membersUpdatePropDefinition\n\t\t\t\trecord  commondao.VotingRecord\n\t\t\t\tmembers = commondao.NewMemberStorage()\n\t\t\t)\n\n\t\t\tfor _, m := range tc.members {\n\t\t\t\tmembers.Add(m)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.votes {\n\t\t\t\trecord.AddVote(v)\n\t\t\t}\n\n\t\t\tctx := commondao.MustNewVotingContext(\u0026record, members)\n\n\t\t\tsuccess, err := p.Tally(ctx)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err, \"expect an error\")\n\t\t\t\tuassert.False(t, success, \"expect tally to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\tuassert.Equal(t, tc.success, success, \"expect tally success to match\")\n\t\t})\n\t}\n}\n\nfunc TestMembersPropDefinitionExecute(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname                 string\n\t\tdao                  *commondao.CommonDAO\n\t\tmembers, add, remove []address\n\t\terrMsg               string\n\t}{\n\t\t{\n\t\t\tname: \"add member\",\n\t\t\tdao: commondao.New(\n\t\t\t\tcommondao.WithMember(\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\"),\n\t\t\t\tcommondao.WithMember(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"),\n\t\t\t),\n\t\t\tadd: []address{\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"add multiple members\",\n\t\t\tdao: commondao.New(\n\t\t\t\tcommondao.WithMember(\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\"),\n\t\t\t),\n\t\t\tadd: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t},\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\",\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"remove member\",\n\t\t\tdao: commondao.New(\n\t\t\t\tcommondao.WithMember(\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\"),\n\t\t\t\tcommondao.WithMember(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"),\n\t\t\t),\n\t\t\tremove:  []address{\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\"},\n\t\t\tmembers: []address{\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"},\n\t\t},\n\t\t{\n\t\t\tname: \"remove multiple members\",\n\t\t\tdao: commondao.New(\n\t\t\t\tcommondao.WithMember(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"),\n\t\t\t\tcommondao.WithMember(\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\"),\n\t\t\t\tcommondao.WithMember(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"),\n\t\t\t),\n\t\t\tremove: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t},\n\t\t\tmembers: []address{\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\"},\n\t\t},\n\t\t{\n\t\t\tname: \"add and remove members\",\n\t\t\tdao: commondao.New(\n\t\t\t\tcommondao.WithMember(\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\"),\n\t\t\t\tcommondao.WithMember(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"),\n\t\t\t),\n\t\t\tadd:    []address{\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},\n\t\t\tremove: []address{\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"},\n\t\t\tmembers: []address{\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae\",\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar add, remove addrset.Set\n\n\t\t\tfor _, addr := range tc.add {\n\t\t\t\tadd.Add(addr)\n\t\t\t}\n\n\t\t\tfor _, addr := range tc.remove {\n\t\t\t\tremove.Add(addr)\n\t\t\t}\n\n\t\t\tp := newMembersUpdatePropDefinition(tc.dao, add, remove)\n\t\t\tfn := p.Executor()\n\n\t\t\terr := fn(cross(cur))\n\n\t\t\tif tc.errMsg != \"\" {\n\t\t\t\tuassert.ErrorContains(t, err, tc.errMsg, \"expect error\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\turequire.Equal(t, len(tc.members), tc.dao.Members().Size(), \"number of members must match\")\n\n\t\t\tvar i int\n\t\t\ttc.dao.Members().IterateByOffset(0, tc.dao.Members().Size(), func(addr address) bool {\n\t\t\t\turequire.Equal(t, tc.members[i], addr, \"member address must match\")\n\n\t\t\t\ti++\n\t\t\t\treturn false\n\t\t\t})\n\t\t})\n\t}\n}\n"},{"name":"proposal_subdao.gno","body":"package commondao\n\nimport (\n\t\"errors\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// newSubDAOPropDefinition creates a new proposal definition for adding a SubDAO.\nfunc newSubDAOPropDefinition(parent *commondao.CommonDAO, name string, members *addrset.Set) subDAOPropDefinition {\n\tif parent == nil {\n\t\tpanic(\"parent DAO is required\")\n\t}\n\n\tname = strings.TrimSpace(name)\n\tassertDAONameIsValid(name)\n\n\treturn subDAOPropDefinition{\n\t\tparent:  parent,\n\t\tname:    name,\n\t\tmembers: members,\n\t}\n}\n\n// subDAOPropDefinition defines a proposal type for adding a SubDAO.\ntype subDAOPropDefinition struct {\n\tparent  *commondao.CommonDAO\n\tname    string\n\tmembers *addrset.Set\n}\n\nfunc (p subDAOPropDefinition) Title() string             { return \"New SubDAO: \" + p.name }\nfunc (subDAOPropDefinition) VotingPeriod() time.Duration { return time.Hour * 24 * 7 }\n\nfunc (p subDAOPropDefinition) Body() string {\n\tvar b strings.Builder\n\n\tb.WriteString(md.Paragraph(\n\t\tmd.Bold(\"Parent DAO:\") + \"\\n\" + md.Link(p.parent.Name(), daoURL(p.parent.ID())),\n\t))\n\n\tb.WriteString(md.Paragraph(\n\t\tmd.Bold(\"SubDAO Name:\") + \"\\n\" + p.name,\n\t))\n\n\tif p.members != nil \u0026\u0026 p.members.Size() \u003e 0 {\n\t\titems := make([]string, 0, p.members.Size())\n\t\tp.members.IterateByOffset(0, p.members.Size(), func(addr address) bool {\n\t\t\titems = append(items, addr.String())\n\t\t\treturn false\n\t\t})\n\n\t\tb.WriteString(md.Paragraph(\n\t\t\tmd.Bold(\"Members:\") + \"\\n\" + md.BulletList(items),\n\t\t))\n\t}\n\n\treturn b.String()\n}\n\nfunc (p subDAOPropDefinition) Validate() (err error) {\n\toptions := getOptions(p.parent.ID())\n\tif !(options.AllowSubDAOProposals \u0026\u0026 options.AllowChildren) {\n\t\treturn errors.New(\"SubDAO support is not enabled\")\n\t}\n\n\tp.parent.Children().ForEach(func(_ int, v any) bool {\n\t\tsubDAO := v.(*commondao.CommonDAO)\n\t\tif subDAO.Name() == p.name {\n\t\t\terr = errors.New(\"a SubDAO with the same name already exists\")\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\treturn err\n}\n\nfunc (subDAOPropDefinition) Tally(ctx commondao.VotingContext) (bool, error) {\n\tif !commondao.IsQuorumReached(commondao.QuorumFull, ctx.VotingRecord, ctx.Members) {\n\t\treturn false, commondao.ErrNoQuorum\n\t}\n\n\tc, success := commondao.SelectChoiceBySuperMajority(ctx.VotingRecord, ctx.Members.Size())\n\tif success {\n\t\treturn c == commondao.ChoiceYes, nil\n\t}\n\treturn false, nil\n}\n\nfunc (p subDAOPropDefinition) Executor() commondao.ExecFunc {\n\treturn p.execute\n}\n\nfunc (p subDAOPropDefinition) execute(realm) error {\n\tsubDAO := createSubDAO(p.parent, p.name, nil)\n\n\tif p.members != nil {\n\t\tp.members.IterateByOffset(0, p.members.Size(), func(addr address) bool {\n\t\t\tsubDAO.Members().Add(addr)\n\t\t\treturn false\n\t\t})\n\t}\n\n\tp.parent.Children().Append(subDAO)\n\treturn nil\n}\n\n// newDissolvePropDefinition creates a new proposal definition for dissolving a SubDAO.\nfunc newDissolvePropDefinition(dao *commondao.CommonDAO) dissolvePropDefinition {\n\tif dao == nil {\n\t\tpanic(\"SubDAO is required\")\n\t}\n\n\treturn dissolvePropDefinition{dao}\n}\n\n// dissolvePropDefinition defines a proposal type for dissolving a SubDAO.\ntype dissolvePropDefinition struct {\n\tdao *commondao.CommonDAO\n}\n\nfunc (p dissolvePropDefinition) Title() string             { return \"Dissolve DAO: \" + p.dao.Name() }\nfunc (dissolvePropDefinition) VotingPeriod() time.Duration { return time.Hour * 24 * 7 }\n\nfunc (p dissolvePropDefinition) Body() string {\n\tvar b strings.Builder\n\n\tb.WriteString(md.Bold(\"DAO:\") + \"\\n\")\n\tb.WriteString(md.Link(p.dao.Name(), daoURL(p.dao.ID())))\n\n\treturn b.String()\n}\n\nfunc (p dissolvePropDefinition) Validate() (err error) {\n\tif p.dao.IsDeleted() {\n\t\treturn errors.New(\"DAO has been already dissolveed\")\n\t}\n\treturn nil\n}\n\nfunc (dissolvePropDefinition) Tally(ctx commondao.VotingContext) (bool, error) {\n\tif !commondao.IsQuorumReached(commondao.QuorumFull, ctx.VotingRecord, ctx.Members) {\n\t\treturn false, commondao.ErrNoQuorum\n\t}\n\n\tc, success := commondao.SelectChoiceBySuperMajority(ctx.VotingRecord, ctx.Members.Size())\n\tif success {\n\t\treturn c == commondao.ChoiceYes, nil\n\t}\n\treturn false, nil\n}\n\nfunc (p dissolvePropDefinition) Executor() commondao.ExecFunc {\n\treturn p.execute\n}\n\nfunc (p dissolvePropDefinition) execute(realm) error {\n\tp.dao.SetDeleted(true)\n\treturn nil\n}\n"},{"name":"proposal_subdao_test.gno","body":"package commondao\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\nvar _ commondao.ProposalDefinition = (*subDAOPropDefinition)(nil)\n\nfunc TestSubDAOPropDefinitionTally(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tmembers []address\n\t\tvotes   []commondao.Vote\n\t\terr     error\n\t\tsuccess bool\n\t}{\n\t\t{\n\t\t\tname: \"succeed\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsuccess: true,\n\t\t},\n\t\t{\n\t\t\tname: \"fail\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"fail with no votes\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\terr: commondao.ErrNoQuorum,\n\t\t},\n\t\t{\n\t\t\tname: \"no quorum\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\terr: commondao.ErrNoQuorum,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar (\n\t\t\t\tp       subDAOPropDefinition\n\t\t\t\trecord  commondao.VotingRecord\n\t\t\t\tmembers = commondao.NewMemberStorage()\n\t\t\t)\n\n\t\t\tfor _, m := range tc.members {\n\t\t\t\tmembers.Add(m)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.votes {\n\t\t\t\trecord.AddVote(v)\n\t\t\t}\n\n\t\t\tctx := commondao.MustNewVotingContext(\u0026record, members)\n\n\t\t\tsuccess, err := p.Tally(ctx)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err, \"expect an error\")\n\t\t\t\tuassert.False(t, success, \"expect tally to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\tuassert.Equal(t, tc.success, success, \"expect tally success to match\")\n\t\t})\n\t}\n}\n\nfunc TestSubDAOPropDefinitionExecute(cur realm, t *testing.T) {\n\t// Create a parent DAO\n\tparentDAO := createDAO(\"Foo\", \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\", nil)\n\turequire.Equal(t, 0, parentDAO.Children().Len(), \"expected no initial SubDAOs\")\n\n\t// Initialize some members for the SubDAO\n\tvar members addrset.Set\n\tmembers.Add(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\")\n\tmembers.Add(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\")\n\n\t// Create the proposal and execute it\n\tname := \"Bar\"\n\tp := newSubDAOPropDefinition(parentDAO, name, \u0026members)\n\tfn := p.Executor()\n\terr := fn(cross(cur))\n\turequire.NoError(t, err, \"expected no execution error\")\n\n\t// Expect SubDAO to be created\n\turequire.Equal(t, 1, parentDAO.Children().Len(), \"expected SubDAO to be created\")\n\tsubDAOAny, _ := parentDAO.Children().Get(0)\n\tsubDAO := subDAOAny.(*commondao.CommonDAO)\n\n\t// Make sure SubDAO is indexed properly\n\tsubDAO = getDAO(subDAO.ID())\n\turequire.True(t, subDAO != nil, \"expected SubDAO to be indexed by the realm\")\n\n\t// Make sure SubDAO values are the right ones\n\tuassert.Equal(t, name, subDAO.Name(), \"expected SubDAO name to match\")\n\tuassert.Equal(t, 2, subDAO.Members().Size(), \"expected SubDAO to have two members\")\n\n\tvar i int\n\tsubDAO.Members().IterateByOffset(0, subDAO.Members().Size(), func(addr address) bool {\n\t\ts, _ := members.Tree().GetByIndex(i)\n\t\turequire.Equal(t, address(s), addr, \"expected member to be found\")\n\t\ti++\n\t\treturn false\n\t})\n}\n\nfunc TestDissolvePropDefinitionTally(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tmembers []address\n\t\tvotes   []commondao.Vote\n\t\terr     error\n\t\tsuccess bool\n\t}{\n\t\t{\n\t\t\tname: \"succeed\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsuccess: true,\n\t\t},\n\t\t{\n\t\t\tname: \"fail\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"fail with no votes\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\terr: commondao.ErrNoQuorum,\n\t\t},\n\t\t{\n\t\t\tname: \"no quorum\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\terr: commondao.ErrNoQuorum,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar (\n\t\t\t\tp       dissolvePropDefinition\n\t\t\t\trecord  commondao.VotingRecord\n\t\t\t\tmembers = commondao.NewMemberStorage()\n\t\t\t)\n\n\t\t\tfor _, m := range tc.members {\n\t\t\t\tmembers.Add(m)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.votes {\n\t\t\t\trecord.AddVote(v)\n\t\t\t}\n\n\t\t\tctx := commondao.MustNewVotingContext(\u0026record, members)\n\n\t\t\tsuccess, err := p.Tally(ctx)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err, \"expect an error\")\n\t\t\t\tuassert.False(t, success, \"expect tally to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\tuassert.Equal(t, tc.success, success, \"expect tally success to match\")\n\t\t})\n\t}\n}\n\nfunc TestDissolvePropDefinitionExecute(cur realm, t *testing.T) {\n\tdao := commondao.New()\n\turequire.False(t, dao.IsDeleted(), \"expected DAO not to be soft deleted by default\")\n\n\tp := newDissolvePropDefinition(dao)\n\tfn := p.Executor()\n\terr := fn(cross(cur))\n\turequire.NoError(t, err, \"expected no execution error\")\n\n\tuassert.True(t, dao.IsDeleted(), \"expected DAO to be soft deleted\")\n}\n"},{"name":"proposal_text.gno","body":"package commondao\n\nimport (\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\nconst (\n\tmaxTextBody         = 15_000\n\tmaxTextTitle        = 255\n\tminTextVotingPeriod = time.Hour * 24\n)\n\n// NewTextPropDefinition creates a new general text proposal definition.\nfunc NewTextPropDefinition(title, body string, quorum float64, votingPeriod time.Duration) TextPropDefinition {\n\ttitle = strings.TrimSpace(title)\n\tif title == \"\" {\n\t\tpanic(\"proposal title is empty\")\n\t}\n\n\tif len(title) \u003e maxTextTitle {\n\t\tpanic(\"proposal title is too long, max length is 255 chars\")\n\t}\n\n\tbody = strings.TrimSpace(body)\n\tif body == \"\" {\n\t\tpanic(\"proposal body is empty\")\n\t}\n\n\tif len(body) \u003e maxTextBody {\n\t\tpanic(\"proposal body is too long, max length is 15000 chars\")\n\t}\n\n\tif votingPeriod \u003c minTextVotingPeriod {\n\t\tpanic(\"minimum proposal voting period is one day\")\n\t}\n\n\tif quorum \u003c commondao.QuorumOneThird {\n\t\tpanic(\"minimum quorum allowed is one third (0.33)\")\n\t}\n\n\treturn TextPropDefinition{\n\t\ttitle:        title,\n\t\tbody:         body,\n\t\tvotingPeriod: votingPeriod,\n\t\tquorum:       quorum,\n\t}\n}\n\n// TextPropDefinition defines a proposal type for general text proposals.\n// These type of proposals are not executable so nothing happens when they pass.\ntype TextPropDefinition struct {\n\ttitle, body  string\n\tvotingPeriod time.Duration\n\tquorum       float64\n}\n\nfunc (p TextPropDefinition) Title() string               { return p.title }\nfunc (p TextPropDefinition) Body() string                { return p.body }\nfunc (p TextPropDefinition) VotingPeriod() time.Duration { return p.votingPeriod }\n\nfunc (p TextPropDefinition) Tally(ctx commondao.VotingContext) (bool, error) {\n\tif !commondao.IsQuorumReached(p.quorum, ctx.VotingRecord, ctx.Members) {\n\t\treturn false, commondao.ErrNoQuorum\n\t}\n\n\tc, success := commondao.SelectChoiceByPlurality(ctx.VotingRecord)\n\tif success {\n\t\treturn c == commondao.ChoiceYes, nil\n\t}\n\treturn false, nil\n}\n"},{"name":"proposal_text_test.gno","body":"package commondao\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/uassert/v0\"\n\t\"gno.land/p/nt/urequire/v0\"\n)\n\n// cur is a zero-value realm used as a placeholder when forwarding to\n// uassert/urequire dispatch helpers that gained an `rlm realm` param.\n// These tests pass `func()` callbacks (no crossing inside the callback),\n// so rlm is ignored — a nil realm here is safe.\nvar cur realm\nvar _ commondao.ProposalDefinition = (*TextPropDefinition)(nil)\n\nfunc TestTextPropDefinitionNew(cur realm, t *testing.T) {\n\tcases := []struct {\n\t\tname         string\n\t\ttitle, body  string\n\t\tquorum       float64\n\t\tvotingPeriod time.Duration\n\t\tpanicMsg     string\n\t}{\n\t\t{\n\t\t\tname:         \"ok\",\n\t\t\ttitle:        \"Test\",\n\t\t\tbody:         \"Foo\",\n\t\t\tvotingPeriod: time.Hour * 24 * 7,\n\t\t\tquorum:       commondao.QuorumOneThird,\n\t\t},\n\t\t{\n\t\t\tname:     \"empty proposal title\",\n\t\t\ttitle:    \"\",\n\t\t\tpanicMsg: \"proposal title is empty\",\n\t\t},\n\t\t{\n\t\t\tname:     \"long proposal title\",\n\t\t\ttitle:    strings.Repeat(\"A\", 256),\n\t\t\tpanicMsg: \"proposal title is too long, max length is 255 chars\",\n\t\t},\n\t\t{\n\t\t\tname:     \"empty proposal body\",\n\t\t\ttitle:    \"Test\",\n\t\t\tbody:     \"\",\n\t\t\tpanicMsg: \"proposal body is empty\",\n\t\t},\n\t\t{\n\t\t\tname:     \"long proposal body\",\n\t\t\ttitle:    \"Test\",\n\t\t\tbody:     strings.Repeat(\"A\", 15001),\n\t\t\tpanicMsg: \"proposal body is too long, max length is 15000 chars\",\n\t\t},\n\t\t{\n\t\t\tname:         \"invalid voting period\",\n\t\t\ttitle:        \"Test\",\n\t\t\tbody:         \"Foo\",\n\t\t\tvotingPeriod: time.Hour * 10,\n\t\t\tpanicMsg:     \"minimum proposal voting period is one day\",\n\t\t},\n\t\t{\n\t\t\tname:         \"invalid quorum\",\n\t\t\ttitle:        \"Test\",\n\t\t\tbody:         \"Foo\",\n\t\t\tvotingPeriod: time.Hour * 24 * 7,\n\t\t\tquorum:       0.32,\n\t\t\tpanicMsg:     \"minimum quorum allowed is one third (0.33)\",\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tif tc.panicMsg != \"\" {\n\t\t\t\turequire.PanicsWithMessage(t, cur, tc.panicMsg, func() {\n\t\t\t\t\tNewTextPropDefinition(tc.title, tc.body, tc.quorum, tc.votingPeriod)\n\t\t\t\t}, \"expected definition to panic with message\")\n\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NotPanics(t, cur, func() {\n\t\t\t\tNewTextPropDefinition(tc.title, tc.body, tc.quorum, tc.votingPeriod)\n\t\t\t}, \"expected definition to be created\")\n\t\t})\n\t}\n}\n\nfunc TestTextPropDefinitionTally(t *testing.T) {\n\tcases := []struct {\n\t\tname    string\n\t\tmembers []address\n\t\tvotes   []commondao.Vote\n\t\terr     error\n\t\tsuccess bool\n\t}{\n\t\t{\n\t\t\tname: \"succeed\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\tsuccess: true,\n\t\t},\n\t\t{\n\t\t\tname: \"fail\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\tChoice:  commondao.ChoiceNo,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"no quorum\",\n\t\t\tmembers: []address{\n\t\t\t\t\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\",\n\t\t\t\t\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\",\n\t\t\t},\n\t\t\tvotes: []commondao.Vote{\n\t\t\t\t{\n\t\t\t\t\tAddress: \"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\",\n\t\t\t\t\tChoice:  commondao.ChoiceYes,\n\t\t\t\t},\n\t\t\t},\n\t\t\terr: commondao.ErrNoQuorum,\n\t\t},\n\t}\n\n\tfor _, tc := range cases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar (\n\t\t\t\tp       = TextPropDefinition{quorum: commondao.QuorumTwoThirds}\n\t\t\t\trecord  commondao.VotingRecord\n\t\t\t\tmembers = commondao.NewMemberStorage()\n\t\t\t)\n\n\t\t\tfor _, m := range tc.members {\n\t\t\t\tmembers.Add(m)\n\t\t\t}\n\n\t\t\tfor _, v := range tc.votes {\n\t\t\t\trecord.AddVote(v)\n\t\t\t}\n\n\t\t\tctx := commondao.MustNewVotingContext(\u0026record, members)\n\n\t\t\tsuccess, err := p.Tally(ctx)\n\n\t\t\tif tc.err != nil {\n\t\t\t\turequire.ErrorIs(t, err, tc.err, \"expect an error\")\n\t\t\t\tuassert.False(t, success, \"expect tally to fail\")\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\turequire.NoError(t, err, \"expect no error\")\n\t\t\tuassert.Equal(t, tc.success, success, \"expect tally success to match\")\n\t\t})\n\t}\n}\n"},{"name":"public.gno","body":"package commondao\n\nimport (\n\t\"chain/runtime/unsafe\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// TODO: Limit the number of DAOs per address, maybe discuss fees ranges to avoid spaming\n\n// Invite invites a user to the realm.\n// A user invitation is required to start creating new DAOs.\nfunc Invite(cur realm, invitee address) {\n\tif !invitee.IsValid() {\n\t\tpanic(\"invalid address\")\n\t}\n\n\tdao := mustGetDAO(CommonDAOID)\n\tcaller := cur.Previous().Address()\n\tif !dao.Members().Has(caller) {\n\t\tpanic(\"unauthorized\")\n\t}\n\n\tinvites.Set(invitee.String(), caller.String())\n}\n\n// IsInvited check if an address has an invitation to the realm.\nfunc IsInvited(addr address) bool {\n\treturn isInvited(addr)\n}\n\n// New creates a new CommonDAO.\n// An invitation is required to be able to create new DAOs.\nfunc New(cur realm, name string, options ...Option) *commondao.CommonDAO {\n\tname = strings.TrimSpace(name)\n\tassertDAONameIsValid(name)\n\n\tcaller := cur.Previous().Address()\n\tif !hasOwnership(caller) {\n\t\torig := unsafe.OriginCaller()\n\t\tassertIsInvited(orig)\n\t\tinvites.Remove(orig.String())\n\t}\n\n\treturn createDAO(name, caller, options)\n}\n\n// NewSubDAO creates a new SubDAO.\nfunc NewSubDAO(cur realm, name string, parentID uint64, options ...Option) *commondao.CommonDAO {\n\tcaller := cur.Previous().Address()\n\tassertIsOwner(caller, parentID)\n\n\tparentOptions := getOptions(parentID)\n\tif !parentOptions.AllowChildren {\n\t\tpanic(\"SubDAO support is not enabled\")\n\t}\n\n\tname = strings.TrimSpace(name)\n\tassertDAONameIsValid(name)\n\n\tparent := getDAO(parentID)\n\tsubDAO := createSubDAO(parent, name, options)\n\tparent.Children().Append(subDAO)\n\treturn subDAO\n}\n\n// GetOptions returns the configuration of a DAO.\n// It panics if caller doesn't have ownership of the DAO instance.\nfunc GetOptions(cur realm, daoID uint64) *Options {\n\tif !isOwner(cur.Previous().Address(), daoID) {\n\t\tpanic(\"unauthorized\")\n\t}\n\n\treturn getOptions(daoID)\n}\n\n// IsOwner checks if an address has ownership of a DAO.\nfunc IsOwner(addr address, daoID uint64) bool {\n\treturn isOwner(addr, daoID)\n}\n\n// TransferOwnership transfers DAO ownership to a different address.\nfunc TransferOwnership(cur realm, daoID uint64, newOwner address) {\n\tcaller := cur.Previous().Address()\n\tassertIsOwner(caller, daoID)\n\n\tif !newOwner.IsValid() {\n\t\tpanic(\"invalid owner address\")\n\t}\n\tids := getOwnership(caller)\n\tfor i, id := range ids {\n\t\tif id == daoID {\n\t\t\townership.Set(caller.String(), append(ids[:i], ids[i+1:]...))\n\t\t\townership.Set(newOwner.String(), append(getOwnership(newOwner), id))\n\n\t\t\tbreak\n\t\t}\n\t}\n}\n\n// Get returns a common DAO searched by ID.\n// It panics if caller doesn't have ownership of the DAO instance.\n// Only toplevel DAOs are returned, to get SubDAOs use `GetSubDAO()`.\nfunc Get(cur realm, daoID uint64) *commondao.CommonDAO {\n\tassertIsOwner(cur.Previous().Address(), daoID)\n\treturn mustGetDAO(daoID)\n}\n\n// GetSize returns the number of SubDAOs under a specified root DAO.\nfunc GetSize(cur realm, rootID uint64) int {\n\tassertIsOwner(cur.Previous().Address(), rootID)\n\n\ttree := getTree(rootID)\n\tif tree == nil {\n\t\treturn 0\n\t}\n\treturn tree.Size()\n}\n\n// Vote submits a vote for a DAO proposal.\nfunc Vote(cur realm, daoID, proposalID uint64, vote commondao.VoteChoice, reason string) {\n\tdao := mustGetDAO(daoID)\n\to := getOptions(dao.ID())\n\tif !o.AllowVoting {\n\t\tpanic(\"voting is not enabled\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tassertCallerIsMember(caller, dao)\n\n\terr := dao.Vote(caller, proposalID, vote, reason)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// Execute executes a DAO proposal.\nfunc Execute(cur realm, daoID, proposalID uint64) {\n\tdao := mustGetDAO(daoID)\n\to := getOptions(dao.ID())\n\tif !o.AllowExecution {\n\t\tpanic(\"proposal execution is not enabled\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tassertCallerIsMember(caller, dao)\n\n\terr := dao.Execute(proposalID, cur)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc hasOwnership(addr address) bool {\n\treturn ownership.Has(addr.String())\n}\n\nfunc isInvited(addr address) bool {\n\treturn invites.Has(addr.String())\n}\n\nfunc isOwner(addr address, daoID uint64) bool {\n\t// Make sure to check the root DAO ID, in case daoID belongs to a SubDAO.\n\t// This is required because ownership is assigned to the root DAO.\n\tdaoID = mustGetDAO(daoID).TopParent().ID()\n\n\tfor _, id := range getOwnership(addr) {\n\t\tif id == daoID {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nfunc assertIsInvited(addr address) {\n\tif !isInvited(addr) {\n\t\tpanic(\"unauthorized\")\n\t}\n}\n\nfunc assertIsOwner(addr address, daoID uint64) {\n\tif !isOwner(addr, daoID) {\n\t\tpanic(\"unauthorized\")\n\t}\n}\n\nfunc assertDAONameIsValid(name string) {\n\tif name == \"\" {\n\t\tpanic(\"DAO name is empty\")\n\t}\n\n\tif len(name) \u003e 60 {\n\t\tpanic(\"DAO name is too long, max length is 60 characters\")\n\t}\n}\n\nfunc assertCallerIsMember(caller address, dao *commondao.CommonDAO) {\n\tif !dao.Members().Has(caller) {\n\t\tpanic(\"caller is not a DAO member\")\n\t}\n}\n"},{"name":"public_proposals.gno","body":"package commondao\n\nimport (\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/moul/addrset\"\n\t\"gno.land/p/nt/commondao/v0\"\n)\n\n// CreateTextProposal creates a new general text proposal.\n//\n// Parameters:\n// - daoID: ID of the root DAO (required)\n// - title: Title of the proposal (required)\n// - body: Body of the proposal (required)\n// - votingDays: The number of days where proposal accepts votes.\n//\n// The default voting period is 7 days.\nfunc CreateTextProposal(cur realm, daoID uint64, title, body string, votingDays uint8) uint64 {\n\tdao := mustGetDAO(daoID)\n\to := getOptions(daoID)\n\tif !o.AllowTextProposals {\n\t\tpanic(\"forbidden\")\n\t}\n\n\tif votingDays \u003e 30 {\n\t\tpanic(\"maximum proposal voting period is 30 days\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tassertCallerIsMember(caller, dao)\n\n\tvar votingPeriod time.Duration\n\tif votingDays == 0 {\n\t\tvotingPeriod = time.Hour * 24 * 7\n\t} else {\n\t\tvotingPeriod = time.Hour * 24 * time.Duration(votingDays)\n\t}\n\n\tdef := NewTextPropDefinition(title, body, commondao.QuorumOneThird, votingPeriod)\n\tp, err := dao.Propose(caller, def)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn p.ID()\n}\n\n// CreateMembersUpdateProposal creates a new proposal to add and/or remove DAO members.\n//\n// Parameters:\n// - daoID: ID of the root DAO (required)\n// - newMembers: Comma separated list of addresses to add as members\n// - removeMembers: Comma separated list of member addresses to remove\nfunc CreateMembersUpdateProposal(cur realm, daoID uint64, newMembers, removeMembers string) uint64 {\n\tdao := mustGetDAO(daoID)\n\to := getOptions(daoID)\n\tif !o.AllowMembersUpdate {\n\t\tpanic(\"forbidden\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tassertCallerIsMember(caller, dao)\n\n\ttoAdd := mustParseStringToAddrset(newMembers)\n\ttoRemove := mustParseStringToAddrset(removeMembers)\n\tdef := newMembersUpdatePropDefinition(dao, toAdd, toRemove)\n\tp, err := dao.Propose(caller, def)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tif err = p.Validate(); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn p.ID()\n}\n\n// CreateSubDAOProposal creates a new proposal to create a new SubDAO.\n//\n// Parameters:\n// - daoID: ID of the parent DAO (required)\n// - name: A name for the SubDAO (required)\n// - members: Comma separated list of addresses to add as members\nfunc CreateSubDAOProposal(cur realm, daoID uint64, name, members string) uint64 {\n\tdao := mustGetDAO(daoID)\n\to := getOptions(daoID)\n\tif !(o.AllowSubDAOProposals \u0026\u0026 o.AllowChildren) {\n\t\tpanic(\"forbidden\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tassertCallerIsMember(caller, dao)\n\n\tdaoMembers := mustParseStringToAddrset(members)\n\tdef := newSubDAOPropDefinition(dao, name, \u0026daoMembers)\n\tp, err := dao.Propose(caller, def)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tif err = p.Validate(); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn p.ID()\n}\n\n// CreateDissolutionProposal creates a new proposal to dissolve a DAO or SubDAO.\n//\n// SubDAOs can only be dissolveed by the the parent DAO.\n//\n// Parameters:\n// - daoID: ID of the DAO to dissolve (required)\nfunc CreateDissolutionProposal(cur realm, daoID uint64) uint64 {\n\t// When DAO to dissolve is a SubDAO make sure that proposal is created in the parent DAO\n\tdao := mustGetDAO(daoID)\n\tdissolveDAO := dao\n\tif parent := dao.Parent(); parent != nil {\n\t\tdao = parent\n\t}\n\n\to := getOptions(dao.ID())\n\tif !o.AllowDissolutionProposals {\n\t\tpanic(\"forbidden\")\n\t}\n\n\tcaller := cur.Previous().Address()\n\tassertCallerIsMember(caller, dao)\n\n\tdef := newDissolvePropDefinition(dissolveDAO)\n\tp, err := dao.Propose(caller, def)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tif err = p.Validate(); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn p.ID()\n}\n\nfunc mustParseStringToAddrset(s string) (set addrset.Set) {\n\tif s == \"\" {\n\t\treturn\n\t}\n\n\tfor _, raw := range strings.Split(s, \"\\n\") {\n\t\traw = strings.TrimSpace(raw)\n\t\tif raw == \"\" {\n\t\t\tcontinue\n\t\t}\n\n\t\taddr := address(raw)\n\t\tif !addr.IsValid() {\n\t\t\tpanic(\"invalid address: \" + addr.String())\n\t\t}\n\n\t\tif !set.Has(addr) {\n\t\t\tset.Add(addr)\n\t\t}\n\t}\n\treturn set\n}\n"},{"name":"render.gno","body":"package commondao\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gno.land/p/jeronimoalbi/pager\"\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/mdtable\"\n\t\"gno.land/p/moul/realmpath\"\n\t\"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/mux/v0\"\n\t\"gno.land/p/nt/ufmt/v0\"\n\n\t\"gno.land/r/sys/users\"\n)\n\nconst dateFormat = \"Mon, 02 Jan 2006 03:04pm MST\"\n\nfunc Render(path string) string {\n\trouter := mux.NewRouter()\n\trouter.HandleFunc(\"\", renderHome)\n\trouter.HandleFunc(\"{daoID}\", renderDAO)\n\trouter.HandleFunc(\"{daoID}/settings\", renderSettings)\n\trouter.HandleFunc(\"{daoID}/proposals\", renderProposalsList)\n\trouter.HandleFunc(\"{daoID}/proposals/{proposalID}\", renderProposal)\n\trouter.HandleFunc(\"{daoID}/proposals/{proposalID}/vote/{address}\", renderProposalVote)\n\treturn router.Render(path)\n}\n\nfunc renderHome(res *mux.ResponseWriter, req *mux.Request) {\n\tres.Write(md.H1(\"Common DAO\"))\n\tres.Write(md.HorizontalRule())\n\tres.Write(ufmt.Sprintf(\n\t\tmd.Paragraph(\"This realm can be used to create CommonDAO instances based on %s package.\"),\n\t\tmd.Link(\"commondao\", \"/p/nt/commondao/v0/\"),\n\t))\n\n\tpages, err := pager.New(req.RawPath, daos.Size(), pager.WithPageSize(10))\n\tif err != nil {\n\t\tres.Write(err.Error())\n\t\treturn\n\t}\n\n\tvar items []string\n\tdaos.ReverseIterateByOffset(pages.Offset(), pages.PageSize(), func(_ string, v any) bool {\n\t\tdao := v.(*commondao.CommonDAO)\n\t\to := getOptions(dao.ID())\n\t\tif o.AllowRender \u0026\u0026 o.AllowListing {\n\t\t\titems = append(items, md.Link(dao.Name(), daoURL(dao.ID())))\n\t\t}\n\t\treturn false\n\t})\n\n\tif len(items) == 0 {\n\t\treturn\n\t}\n\n\tres.Write(md.Paragraph(\"Here is a list of some of the DAOs that were created:\"))\n\tres.Write(md.Paragraph(md.BulletList(items)))\n\n\tif pages.HasPages() {\n\t\tres.Write(md.Paragraph(pager.Picker(pages)))\n\t}\n}\n\nfunc renderDAO(res *mux.ResponseWriter, req *mux.Request) {\n\tdao := mustGetDAOFromRequest(req)\n\n\t// Render header messages\n\tif dao.IsDeleted() {\n\t\tres.Write(md.Blockquote(\"⚠ This DAO has been dissolved\"))\n\t}\n\n\t// Render header\n\tres.Write(md.H1(dao.Name()))\n\tif desc := dao.Description(); desc != \"\" {\n\t\tres.Write(md.Paragraph(desc))\n\t}\n\n\t// Render main menu\n\tmenu := []string{\n\t\tmd.Link(\"View Proposals\", daoProposalsURL(dao.ID())),\n\t\tmd.Link(\"View Settings\", settingsURL(dao.ID())),\n\t}\n\n\tif parentDAO := dao.Parent(); parentDAO != nil {\n\t\tmenu = append(menu, md.Link(\"Go to Parent DAO\", daoURL(parentDAO.ID())))\n\t}\n\n\tres.Write(md.Paragraph(strings.Join(menu, \" • \")))\n\tres.Write(md.HorizontalRule())\n\n\t// Render members\n\tmembers := dao.Members()\n\tif members.Size() == 0 {\n\t\tres.Write(md.Paragraph(md.Bold(\"⚠ The DAO has no members\")))\n\t} else {\n\t\trenderMembers(res, req.RawPath, members)\n\t}\n\n\t// Render organization tree\n\tif dao.Children().Len() \u003e 0 {\n\t\tr := parseRealmPath(req.RawPath)\n\t\tdissolvedVisible := r.Query.Has(\"dissolved\") || dao.IsDeleted()\n\t\tif dissolvedVisible || !dao.IsDeleted() {\n\t\t\tres.Write(md.H2(\"Tree\"))\n\n\t\t\t// Render toggle only when DAO is not dissolved,\n\t\t\t// otherwise render the whole tree when DAO is dissolved\n\t\t\tif !dao.IsDeleted() {\n\t\t\t\tvar toggleLink string\n\t\t\t\tif dissolvedVisible {\n\t\t\t\t\tr.Query.Del(\"dissolved\")\n\t\t\t\t\ttoggleLink = md.Link(\"hide\", r.String())\n\t\t\t\t} else {\n\t\t\t\t\tr.Query.Add(\"dissolved\", \"\")\n\t\t\t\t\ttoggleLink = md.Link(\"show\", r.String())\n\t\t\t\t}\n\n\t\t\t\tres.Write(md.Paragraph(\"Dissolved: \" + toggleLink))\n\t\t\t}\n\n\t\t\trenderTree(res, dao, \"\", dissolvedVisible)\n\t\t}\n\t}\n\n\t// Render latest proposals\n\tproposals := dao.ActiveProposals()\n\tif proposals.Size() \u003e 0 {\n\t\tres.Write(md.H2(\"Latest Proposals\"))\n\t\tproposals.Iterate(0, 3, true, func(p *commondao.Proposal) bool {\n\t\t\trenderProposalsListItem(res, dao, p)\n\t\t\treturn false\n\t\t})\n\t}\n\n\t// Render proposal creation links\n\tif !dao.IsDeleted() {\n\t\trenderCreateProposalSection(dao.ID(), res)\n\t}\n}\n\nfunc renderCreateProposalSection(daoID uint64, res *mux.ResponseWriter) {\n\tvar (\n\t\tcols []string\n\t\to    = getOptions(daoID)\n\t)\n\n\tif o.AllowTextProposals {\n\t\tcols = append(cols, md.Paragraph(textProposalLink(daoID))+\n\t\t\tmd.Paragraph(\n\t\t\t\t\"This type of proposal is also known as text proposal which can be used for example \"+\n\t\t\t\t\t\"to get consensus on initiatives without actually making any change on-chain.\",\n\t\t\t),\n\t\t)\n\t}\n\n\tif o.AllowSubDAOProposals {\n\t\tcols = append(cols, md.Paragraph(newSubDAOLink(daoID))+\n\t\t\tmd.Paragraph(\n\t\t\t\t\"This type of proposal is used to create SubDAOs, \"+\n\t\t\t\t\t\"which are used to create tree based DAOs.\",\n\t\t\t),\n\t\t)\n\t}\n\n\tif o.AllowDissolutionProposals {\n\t\tcols = append(cols, md.Paragraph(dissolveSubDAOLink(daoID))+\n\t\t\tmd.Paragraph(\n\t\t\t\t\"This type of proposal can be used to dissolve DAOs and SubDAOs.\",\n\t\t\t)+\n\t\t\tmd.Paragraph(\n\t\t\t\t\"Dissolving a DAO can't be undone, once the dissolution proposal passes \"+\n\t\t\t\t\t\"and is executed DAO will be readonly.\",\n\t\t\t),\n\t\t)\n\t}\n\n\tif o.AllowMembersUpdate {\n\t\tcols = append(cols, md.Paragraph(updateMembersLink(daoID))+\n\t\t\tmd.Paragraph(\n\t\t\t\t\"This type of proposal can be used to add new members to this DAO \"+\n\t\t\t\t\t\"and also to remove existing ones.\",\n\t\t\t)+\n\t\t\tmd.Paragraph(\n\t\t\t\t\"A single proposal allows new members to be added and any number of existing ones \"+\n\t\t\t\t\t\"removed within the same proposal.\",\n\t\t\t),\n\t\t)\n\t}\n\n\tif len(cols) == 0 {\n\t\treturn\n\t}\n\n\tres.Write(md.H2(\"Create Proposal\"))\n\tres.Write(md.Paragraph(\"These are the proposal supported by this DAO:\"))\n\tres.Write(md.Columns(cols, false))\n}\n\nfunc renderMembers(res *mux.ResponseWriter, path string, members commondao.MemberStorage) {\n\tpages, err := pager.New(path, members.Size(), pager.WithPageQueryParam(\"members\"), pager.WithPageSize(8))\n\tif err != nil {\n\t\tres.Write(err.Error())\n\t\treturn\n\t}\n\n\ttable := mdtable.Table{Headers: []string{\"Members\"}}\n\tmembers.IterateByOffset(pages.Offset(), pages.PageSize(), func(addr address) bool {\n\t\ttable.Append([]string{userLink(addr)})\n\t\treturn false\n\t})\n\n\tres.Write(md.Paragraph(table.String()))\n\n\tif pages.HasPages() {\n\t\tres.Write(md.Paragraph(pager.Picker(pages)))\n\t}\n}\n\nfunc renderTree(res *mux.ResponseWriter, dao *commondao.CommonDAO, indent string, showDissolved bool) {\n\tdaoLink := md.Link(dao.Name(), daoURL(dao.ID()))\n\tif dao.IsDeleted() {\n\t\t// Strikethough dissolved DAO names\n\t\tdaoLink = md.Strikethrough(daoLink)\n\t}\n\n\tres.Write(indent + md.BulletItem(daoLink))\n\n\tindent += \"  \"\n\tdao.Children().ForEach(func(_ int, v any) bool {\n\t\tsubDAO, ok := v.(*commondao.CommonDAO)\n\t\tif !ok {\n\t\t\treturn false\n\t\t}\n\n\t\tif showDissolved || !subDAO.IsDeleted() {\n\t\t\trenderTree(res, subDAO, indent, showDissolved)\n\t\t}\n\t\treturn false\n\t})\n}\n\nfunc renderSettings(res *mux.ResponseWriter, req *mux.Request) {\n\tdao := mustGetDAOFromRequest(req)\n\to := getOptions(dao.ID())\n\n\t// Render header\n\tres.Write(md.H1(dao.Name() + \": Settings\"))\n\n\t// Render main menu\n\tres.Write(md.Paragraph(goToDAOLink(dao.ID())))\n\tres.Write(md.HorizontalRule())\n\n\t// Render options\n\ttable := mdtable.Table{Headers: []string{\"Options\", \"Values\"}}\n\ttable.Append([]string{\"Allow Render\", strconv.FormatBool(o.AllowRender)})\n\ttable.Append([]string{\"Allow SubDAOs\", strconv.FormatBool(o.AllowChildren)})\n\ttable.Append([]string{\"Enable Voting\", strconv.FormatBool(o.AllowVoting)})\n\ttable.Append([]string{\"Enable Proposal Execution\", strconv.FormatBool(o.AllowExecution)})\n\ttable.Append([]string{\"Text Proposals\", strconv.FormatBool(o.AllowTextProposals)})\n\ttable.Append([]string{\"Members Update proposals\", strconv.FormatBool(o.AllowMembersUpdate)})\n\ttable.Append([]string{\"SubDAO creation proposals\", strconv.FormatBool(o.AllowSubDAOProposals)})\n\ttable.Append([]string{\"DAO dissolution proposals\", strconv.FormatBool(o.AllowDissolutionProposals)})\n\n\tres.Write(md.H2(\"Options\"))\n\tres.Write(table.String())\n}\n\nfunc renderProposalsList(res *mux.ResponseWriter, req *mux.Request) {\n\tdao := mustGetDAOFromRequest(req)\n\n\t// Render header\n\tres.Write(md.H1(dao.Name() + \": Proposals\"))\n\n\t// Render main menu\n\tres.Write(md.Paragraph(goToDAOLink(dao.ID())))\n\tres.Write(md.HorizontalRule())\n\n\t// Render proposals\n\tif dao.ActiveProposals().Size() == 0 \u0026\u0026 dao.FinishedProposals().Size() == 0 {\n\t\tres.Write(md.Paragraph(md.Bold(\"⚠ The DAO has no proposals\")))\n\t\treturn\n\t}\n\n\tproposals := dao.ActiveProposals()\n\trenderFinished := req.Query.Has(\"finished\")\n\tif renderFinished {\n\t\tproposals = dao.FinishedProposals()\n\t}\n\n\tpages, err := pager.New(req.RawPath, proposals.Size(), pager.WithPageSize(8))\n\tif err != nil {\n\t\tres.Write(err.Error())\n\t\treturn\n\t}\n\n\tvar viewLink, sortLink string\n\n\tr := parseRealmPath(req.RawPath)\n\tif renderFinished {\n\t\tr.Query.Del(\"finished\")\n\t\tviewLink = md.Link(\"active\", r.String())\n\t} else {\n\t\tr.Query.Add(\"finished\", \"\")\n\t\tviewLink = md.Link(\"finished\", r.String())\n\t}\n\n\tr = parseRealmPath(req.RawPath)\n\treverseSort := r.Query.Get(\"order\") != \"asc\"\n\tif reverseSort {\n\t\tr.Query.Set(\"order\", \"asc\")\n\t\tsortLink = md.Link(\"oldest\", r.String())\n\t} else {\n\t\tr.Query.Set(\"order\", \"desc\")\n\t\tsortLink = md.Link(\"newest\", r.String())\n\t}\n\n\tres.Write(md.Paragraph(\"View: \" + viewLink + \" • Sort by: \" + sortLink))\n\n\tif proposals.Size() == 0 {\n\t\tif renderFinished {\n\t\t\tres.Write(md.Paragraph(\"Currently there are no finished proposals\"))\n\t\t} else {\n\t\t\tres.Write(md.Paragraph(\"Currently there are no active proposals\"))\n\t\t}\n\t} else {\n\t\tproposals.Iterate(pages.Offset(), pages.PageSize(), reverseSort, func(p *commondao.Proposal) bool {\n\t\t\trenderProposalsListItem(res, dao, p)\n\t\t\treturn false\n\t\t})\n\t}\n\n\t// Render pager\n\tif pages.HasPages() {\n\t\tres.Write(md.HorizontalRule())\n\t\tres.Write(pager.Picker(pages))\n\t}\n}\n\nfunc renderProposalsListItem(res *mux.ResponseWriter, dao *commondao.CommonDAO, p *commondao.Proposal) {\n\tdef := p.Definition()\n\trecord := p.VotingRecord()\n\to := getOptions(dao.ID())\n\n\t// Render title\n\tres.Write(ufmt.Sprintf(\"**[#%d %s](%s)**  \\n\", p.ID(), md.EscapeText(def.Title()), proposalURL(dao.ID(), p.ID())))\n\n\t// Render details\n\tres.Write(ufmt.Sprintf(\"Created by %s  \\n\", userLink(p.Creator())))\n\tres.Write(ufmt.Sprintf(\"Voting ends on %s  \\n\", p.VotingDeadline().UTC().Format(dateFormat)))\n\n\t// Render status\n\tstatus := []string{\n\t\tufmt.Sprintf(\"Votes: **%d**\", record.Size()),\n\t\tufmt.Sprintf(\"Status: **%s**\", string(p.Status())),\n\t}\n\n\t// Render actions\n\tif o.AllowVoting \u0026\u0026 isVotingPeriodActive(p) {\n\t\tstatus = append(status, voteLink(dao.ID(), p.ID()))\n\t}\n\n\tif o.AllowExecution \u0026\u0026 isExecutionAllowed(p) {\n\t\tstatus = append(status, executeLink(dao.ID(), p.ID()))\n\t}\n\n\tres.Write(md.Paragraph(strings.Join(status, \" • \")))\n}\n\nfunc renderProposal(res *mux.ResponseWriter, req *mux.Request) {\n\tdao := mustGetDAOFromRequest(req)\n\tp := mustGetProposalFromRequest(req, dao)\n\n\t// Check that proposal has no issues\n\tif err := p.Validate(); err != nil {\n\t\tres.Write(md.Blockquote(\"⚠ **ERROR**: \" + err.Error()))\n\t}\n\n\tvotingActive := isVotingPeriodActive(p)\n\tif votingActive {\n\t\tres.Write(\n\t\t\tmd.Blockquote(\"Voting ends on \" + md.Bold(p.VotingDeadline().UTC().Format(dateFormat))),\n\t\t)\n\t}\n\n\tdef := p.Definition()\n\n\t// Render header\n\tres.Write(md.H1(\"#\" + strconv.FormatUint(p.ID(), 10) + \" \" + md.EscapeText(def.Title())))\n\n\t// Render main menu\n\titems := []string{goToDAOLink(dao.ID())}\n\to := getOptions(dao.ID())\n\tif o.AllowVoting \u0026\u0026 votingActive {\n\t\titems = append(items, voteLink(dao.ID(), p.ID()))\n\t}\n\n\tif o.AllowExecution \u0026\u0026 isExecutionAllowed(p) {\n\t\titems = append(items, executeLink(dao.ID(), p.ID()))\n\t}\n\n\tres.Write(md.Paragraph(strings.Join(items, \" • \")))\n\tres.Write(md.HorizontalRule())\n\n\t// Render details\n\tres.Write(md.H2(\"Details\"))\n\tres.Write(md.BulletItem(\"Proposer: \" + userLink(p.Creator())))\n\tres.Write(md.BulletItem(\"Submit Time: \" + p.CreatedAt().UTC().Format(time.RFC1123)))\n\n\trecord := p.VotingRecord()\n\tif p.Status() == commondao.StatusActive {\n\t\tctx := commondao.MustNewVotingContext(record, dao.Members())\n\t\tpasses, _ := def.Tally(ctx)\n\t\tif passes {\n\t\t\tres.Write(md.BulletItem(\"Expected Outcome: **pass** ☑\"))\n\t\t} else {\n\t\t\tres.Write(md.BulletItem(\"Expected Outcome: **fail** ☒\"))\n\t\t}\n\t}\n\n\tstatusItem := \"Status: \" + md.Bold(string(p.Status()))\n\tif reason := p.StatusReason(); reason != \"\" {\n\t\tstatusItem += \" • \" + md.Italic(reason)\n\t}\n\tres.Write(md.BulletItem(statusItem))\n\n\t// Render proposal body\n\tif body := def.Body(); body != \"\" {\n\t\tres.Write(md.H2(\"Description\"))\n\t\tres.Write(md.Paragraph(body))\n\t}\n\n\t// Render voting stats and votes\n\tif record.Size() \u003e 0 {\n\t\trenderProposalStats(res, record)\n\t\trenderProposalVotes(res, req.RawPath, dao, p)\n\t}\n}\n\nfunc renderProposalStats(res *mux.ResponseWriter, record *commondao.VotingRecord) {\n\ttotalCount := float64(record.Size())\n\ttable := mdtable.Table{Headers: []string{\"Vote Choices\", \"Percentage of Votes\"}}\n\n\trecord.IterateVotesCount(func(c commondao.VoteChoice, voteCount int) bool {\n\t\tpercentage := float64(voteCount*100) / totalCount\n\n\t\ttable.Append([]string{string(c), strconv.FormatFloat(percentage, 'f', 2, 64) + \"%\"})\n\t\treturn false\n\t})\n\n\tres.Write(md.H2(\"Stats\"))\n\tres.Write(md.Paragraph(table.String()))\n}\n\nfunc renderProposalVotes(res *mux.ResponseWriter, path string, dao *commondao.CommonDAO, p *commondao.Proposal) {\n\tres.Write(md.H2(\"Votes\")) // Render title here so it appears before any pager errors\n\n\trecord := p.VotingRecord()\n\tpages, err := pager.New(path, record.Size(), pager.WithPageQueryParam(\"votes\"), pager.WithPageSize(5))\n\tif err != nil {\n\t\tres.Write(err.Error())\n\t\treturn\n\t}\n\n\ttable := mdtable.Table{Headers: []string{\"Users\", \"Votes\"}}\n\trecord.Iterate(pages.Offset(), pages.PageSize(), false, func(v commondao.Vote) bool {\n\t\tvoteDetails := md.Link(string(v.Choice), voteURL(dao.ID(), p.ID(), v.Address))\n\t\tif v.Reason != \"\" {\n\t\t\tvoteDetails += \" with a reason\"\n\t\t}\n\n\t\ttable.Append([]string{userLink(v.Address), voteDetails})\n\t\treturn false\n\t})\n\n\tres.Write(ufmt.Sprintf(\"Total number of votes: **%d**\\n\", record.Size()))\n\tres.Write(md.Paragraph(table.String()))\n\n\tif pages.HasPages() {\n\t\tres.Write(md.Paragraph(pager.Picker(pages)))\n\t}\n}\n\nfunc renderProposalVote(res *mux.ResponseWriter, req *mux.Request) {\n\tmember := address(req.GetVar(\"address\"))\n\tif !member.IsValid() {\n\t\tres.Write(\"Invalid address\")\n\t\treturn\n\t}\n\n\tdao := mustGetDAOFromRequest(req)\n\tp := mustGetProposalFromRequest(req, dao)\n\tv, found := p.VotingRecord().GetVote(member)\n\tif !found {\n\t\tres.Write(\"Vote not found\")\n\t\treturn\n\t}\n\n\tlinks := []string{\n\t\tgoToDAOLink(dao.ID()),\n\t\tgoToProposalLink(dao.ID(), p.ID()),\n\t}\n\n\tres.Write(ufmt.Sprintf(\"# Vote: Proposal #%d\\n\", p.ID()))\n\tres.Write(md.Paragraph(strings.Join(links, \" • \")))\n\tres.Write(md.HorizontalRule())\n\n\tres.Write(md.H2(\"Details\"))\n\tres.Write(md.BulletItem(\"User: \" + userLink(v.Address)))\n\tres.Write(md.BulletItem(\"Vote: \" + string(v.Choice)))\n\n\tif v.Reason != \"\" {\n\t\tres.Write(md.H2(\"Reason\"))\n\t\tres.Write(v.Reason)\n\t}\n}\n\nfunc mustGetDAOFromRequest(req *mux.Request) *commondao.CommonDAO {\n\trawID := req.GetVar(\"daoID\")\n\tdaoID, err := strconv.ParseUint(rawID, 10, 64)\n\tif err != nil {\n\t\tpanic(\"Invalid DAO ID\")\n\t}\n\n\to := getOptions(daoID)\n\tif o == nil || !o.AllowRender {\n\t\tpanic(\"Forbidden\")\n\t}\n\n\treturn mustGetDAO(daoID)\n}\n\nfunc mustGetProposalFromRequest(req *mux.Request, dao *commondao.CommonDAO) *commondao.Proposal {\n\trawID := req.GetVar(\"proposalID\")\n\tproposalID, err := strconv.ParseUint(rawID, 10, 64)\n\tif err != nil {\n\t\tpanic(\"Invalid proposal ID\")\n\t}\n\n\tp := dao.GetProposal(proposalID)\n\tif p == nil {\n\t\tpanic(\"Proposal not found\")\n\t}\n\treturn p\n}\n\nfunc parseRealmPath(path string) *realmpath.Request {\n\tr := realmpath.Parse(path)\n\tr.Realm = string(realmLink)\n\treturn r\n}\n\nfunc voteLink(daoID, proposalID uint64) string {\n\treturn md.Link(\"Vote\", realmLink.Call(\n\t\t\"Vote\",\n\t\t\"daoID\", strconv.FormatUint(daoID, 10),\n\t\t\"proposalID\", strconv.FormatUint(proposalID, 10),\n\t\t\"vote\", \"\",\n\t\t\"reason\", \"\",\n\t))\n}\n\nfunc executeLink(daoID, proposalID uint64) string {\n\treturn md.Link(\"Execute\", realmLink.Call(\n\t\t\"Execute\",\n\t\t\"daoID\", strconv.FormatUint(daoID, 10),\n\t\t\"proposalID\", strconv.FormatUint(proposalID, 10),\n\t))\n}\n\nfunc textProposalLink(daoID uint64) string {\n\treturn ufmt.Sprintf(\"[General Proposal](%s)\", realmLink.Call(\n\t\t\"CreateTextProposal\",\n\t\t\"daoID\", strconv.FormatUint(daoID, 10),\n\t\t\"title\", \"\",\n\t\t\"body\", \"\",\n\t\t\"votingDays\", \"7\",\n\t))\n}\n\nfunc updateMembersLink(daoID uint64) string {\n\treturn md.Link(\"Update Members\", realmLink.Call(\n\t\t\"CreateMembersUpdateProposal\",\n\t\t\"daoID\", strconv.FormatUint(daoID, 10),\n\t\t\"newMembers\", \"\",\n\t\t\"removeMembers\", \"\",\n\t))\n}\n\nfunc newSubDAOLink(daoID uint64) string {\n\treturn md.Link(\"New SubDAO\", realmLink.Call(\n\t\t\"CreateSubDAOProposal\",\n\t\t\"daoID\", strconv.FormatUint(daoID, 10),\n\t\t\"name\", \"\",\n\t\t\"members\", \"\",\n\t))\n}\n\nfunc dissolveSubDAOLink(daoID uint64) string {\n\treturn md.Link(\"Dissolve DAO\", realmLink.Call(\n\t\t\"CreateDissolutionProposal\",\n\t\t\"daoID\", strconv.FormatUint(daoID, 10),\n\t))\n}\n\nfunc goToDAOLink(daoID uint64) string {\n\treturn md.Link(\"Go to DAO\", daoURL(daoID))\n}\n\nfunc goToProposalLink(daoID, proposalID uint64) string {\n\treturn md.Link(\"Go to Proposal\", proposalURL(daoID, proposalID))\n}\n\nfunc userLink(addr address) string {\n\tuser := users.ResolveAddress(addr)\n\tif user != nil {\n\t\treturn user.RenderLink(\"\")\n\t}\n\treturn addr.String()\n}\n\nfunc isVotingPeriodActive(p *commondao.Proposal) bool {\n\treturn p.Status() == commondao.StatusActive \u0026\u0026 time.Now().Before(p.VotingDeadline())\n}\n\nfunc isExecutionAllowed(p *commondao.Proposal) bool {\n\treturn p.Status() == commondao.StatusActive \u0026\u0026 !time.Now().Before(p.VotingDeadline())\n}\n"},{"name":"uri.gno","body":"package commondao\n\nimport (\n\t\"chain/runtime\"\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n)\n\nfunc currentRealmPath() string {\n\treturn strings.TrimPrefix(string(realmLink), runtime.ChainDomain())\n}\n\nfunc daoURL(daoID uint64) string {\n\tpath := currentRealmPath()\n\treturn ufmt.Sprintf(\"%s:%d\", path, daoID)\n}\n\nfunc settingsURL(daoID uint64) string {\n\tpath := currentRealmPath()\n\treturn ufmt.Sprintf(\"%s:%d/settings\", path, daoID)\n}\n\nfunc daoProposalsURL(daoID uint64) string {\n\tpath := currentRealmPath()\n\treturn ufmt.Sprintf(\"%s:%d/proposals\", path, daoID)\n}\n\nfunc proposalURL(daoID, proposalID uint64) string {\n\tpath := currentRealmPath()\n\treturn ufmt.Sprintf(\"%s:%d/proposals/%d\", path, daoID, proposalID)\n}\n\nfunc voteURL(daoID, proposalID uint64, addr address) string {\n\tpath := currentRealmPath()\n\treturn ufmt.Sprintf(\"%s:%d/proposals/%d/vote/%s\", path, daoID, proposalID, addr)\n}\n"},{"name":"z_1_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_1_a_filetest\n\npackage z_1_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tcommondao.Invite(cross(cur), user)\n\n\tprintln(commondao.IsInvited(user))\n}\n\n// Output:\n// true\n"},{"name":"z_1_b_filetest.gno","body":"package main\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nfunc main() {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tprintln(commondao.IsInvited(user))\n}\n\n// Output:\n// false\n"},{"name":"z_1_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_1_c_filetest\n\npackage z_1_c_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\tuser    = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tinvitee = address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\") // @test2\n)\n\nfunc main(cur realm) {\n\t// Call as a users which is not a Common DAO member\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.Invite(cross(cur), invitee)\n}\n\n// Error:\n// unauthorized\n"},{"name":"z_2_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_2_a_filetest\n\npackage z_2_a_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tname  = \"Foo\"\n)\n\nvar realmAddr = chain.PackageAddress(\"gno.land/r/demo/test\")\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n}\n\nfunc main(cur realm) {\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\n\tdao := commondao.New(cross(cur), name)\n\tif dao == nil {\n\t\tpanic(\"expected DAO to be created\")\n\t}\n\n\tprintln(dao.Name() == name)\n\tprintln(commondao.IsOwner(realmAddr, dao.ID()))\n\tprintln(commondao.IsInvited(user))\n\tprintln(commondao.GetOptions(cross(cur), dao.ID()) != nil)\n}\n\n// Output:\n// true\n// true\n// false\n// true\n"},{"name":"z_2_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_2_b_filetest\n\npackage z_2_b_filetest\n\nimport (\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nfunc main(cur realm) {\n\tcommondao.New(cross(cur), \"\")\n}\n\n// Error:\n// DAO name is empty\n"},{"name":"z_2_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_2_c_filetest\n\npackage z_2_c_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst user = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\nfunc main(cur realm) {\n\t// Calling with a user that was not invited\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.New(cross(cur), \"Foo\")\n}\n\n// Error:\n// unauthorized\n"},{"name":"z_3_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_3_a_filetest\n\npackage z_3_a_filetest\n\nimport (\n\t\"chain\"\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner    = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser     = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tnewRealm = address(\"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\")\n)\n\nvar (\n\tdaoID     uint64\n\trealmAddr = chain.PackageAddress(\"gno.land/r/test\")\n)\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tdaoID = commondao.New(cross(cur), \"Foo\").ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\tcommondao.TransferOwnership(cross(cur), daoID, newRealm)\n\n\tprintln(commondao.IsOwner(realmAddr, daoID))\n\tprintln(commondao.IsOwner(newRealm, daoID))\n}\n\n// Output:\n// false\n// true\n"},{"name":"z_3_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_3_b_filetest\n\npackage z_3_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner    = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser     = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tnewRealm = address(\"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\")\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/tests\"))\n\tdaoID = commondao.New(cross(cur), \"Foo\").ID()\n}\n\nfunc main(cur realm) {\n\t// Use a caller that is not the owner of the DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.TransferOwnership(cross(cur), daoID, newRealm)\n}\n\n// Error:\n// unauthorized\n"},{"name":"z_4_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_4_a_filetest\n\npackage z_4_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tname  = \"B\"\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a couple of DAOs\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tcommondao.New(cross(cur), \"A\")\n\tdaoID = commondao.New(cross(cur), name).ID()\n\tcommondao.New(cross(cur), \"C\")\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\tdao := commondao.Get(cross(cur), daoID)\n\tif dao == nil {\n\t\tpanic(\"expected DAO to be found\")\n\t}\n\n\tprintln(dao.Name() == name)\n\tprintln(dao.ID() == daoID)\n}\n\n// Output:\n// true\n// true\n"},{"name":"z_4_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_4_b_filetest\n\npackage z_4_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tdaoID = commondao.New(cross(cur), \"Foo\").ID()\n}\n\nfunc main(cur realm) {\n\t// Use a caller that is not the owner of the DAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/tests/vm\"))\n\n\tcommondao.Get(cross(cur), daoID)\n}\n\n// Error:\n// unauthorized\n"},{"name":"z_5_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_5_a_filetest\n\npackage z_5_a_filetest\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\tpdao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\ntype propDef struct{}\n\nfunc (propDef) Title() string                          { return \"\" }\nfunc (propDef) Body() string                           { return \"\" }\nfunc (propDef) VotingPeriod() time.Duration            { return time.Hour }\nfunc (propDef) Validate() error                        { return nil }\nfunc (propDef) Tally(pdao.VotingContext) (bool, error) { return false, nil }\n\nvar (\n\tdaoID    uint64\n\tproposal *pdao.Proposal\n\tvote     pdao.VoteChoice = pdao.ChoiceYes\n)\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdaoID = dao.ID()\n\n\t// Configure DAO\n\tdao.Members().Add(user)\n\n\t// Create a new proposal\n\tproposal, _ = dao.Propose(user, propDef{})\n}\n\nfunc main(cur realm) {\n\t// User must be the caller to Vote()\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.Vote(cross(cur), daoID, proposal.ID(), vote, \"\")\n\n\trecord := proposal.VotingRecord()\n\tif record.Size() != 1 {\n\t\tpanic(\"expected a single vote\")\n\t}\n\n\tprintln(record.HasVoted(user))\n\trecord.Iterate(0, record.Size(), false, func(v pdao.Vote) bool {\n\t\tprintln(v.Choice == vote)\n\t\treturn false\n\t})\n}\n\n// Output:\n// true\n// true\n"},{"name":"z_5_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_5_b_filetest\n\npackage z_5_b_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst user = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.Vote(cross(cur), 404, 1, pcommondao.ChoiceYes, \"\")\n}\n\n// Error:\n// DAO not found\n"},{"name":"z_5_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_5_c_filetest\n\npackage z_5_c_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tdaoID = commondao.New(cross(cur), \"Foo\").ID()\n}\n\nfunc main(cur realm) {\n\tcommondao.Vote(cross(cur), daoID, 1, pcommondao.ChoiceYes, \"\")\n}\n\n// Error:\n// caller is not a DAO member\n"},{"name":"z_5_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_5_d_filetest\n\npackage z_5_d_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdaoID = dao.ID()\n\n\t// Configure DAO\n\tdao.Members().Add(user)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.Vote(cross(cur), daoID, 404, pcommondao.ChoiceYes, \"\")\n}\n\n// Error:\n// proposal not found\n"},{"name":"z_5_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_5_e_filetest\n\npackage z_5_e_filetest\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\tpdao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\ntype propDef struct{}\n\nfunc (propDef) Title() string                          { return \"\" }\nfunc (propDef) Body() string                           { return \"\" }\nfunc (propDef) VotingPeriod() time.Duration            { return time.Hour }\nfunc (propDef) Validate() error                        { return nil }\nfunc (propDef) Tally(pdao.VotingContext) (bool, error) { return false, nil }\n\nvar (\n\tdaoID    uint64\n\tproposal *pdao.Proposal\n)\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdaoID = dao.ID()\n\n\t// Configure DAO\n\tdao.Members().Add(user)\n\n\t// Create a new proposal\n\tproposal, _ = dao.Propose(user, propDef{})\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.Vote(cross(cur), daoID, proposal.ID(), \"invalid\", \"\")\n}\n\n// Error:\n// invalid vote choice\n"},{"name":"z_5_f_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_5_f_filetest\n\npackage z_5_f_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/nt/commondao/v0\"))\n\tdao := commondao.New(cross(cur), \"Foo\", commondao.AllowVoting(false)) // Disallow voting\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\tcommondao.Vote(cross(cur), daoID, 0, \"\", \"\")\n}\n\n// Error:\n// voting is not enabled\n"},{"name":"z_6_a_filetest.gno","body":"// PKGPATH: gno.land/r/demo/test\npackage test\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\tpdao \"gno.land/p/nt/commondao/v0\"\n\t\"gno.land/p/nt/testutils/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst owner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\nvar (\n\tdao      *pdao.CommonDAO\n\tproposal *pdao.Proposal\n\texecuted bool\n\tuser1    = testutils.TestAddress(\"user1\")\n)\n\ntype propDef struct{}\n\nfunc (propDef) Title() string                          { return \"\" }\nfunc (propDef) Body() string                           { return \"\" }\nfunc (propDef) VotingPeriod() time.Duration            { return 0 }\nfunc (propDef) Validate() error                        { return nil }\nfunc (propDef) Tally(pdao.VotingContext) (bool, error) { return true, nil }\n\nfunc (propDef) Executor() pdao.ExecFunc {\n\treturn func(realm) error {\n\t\texecuted = true\n\t\treturn nil\n\t}\n}\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user1)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\n\t// Configure DAO\n\tdao.Members().Add(user1)\n\n\t// Create a new proposal\n\tproposal, _ = dao.Propose(user1, propDef{})\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user1))\n\n\tcommondao.Execute(cross(cur), dao.ID(), proposal.ID())\n\n\tp := dao.FinishedProposals().Get(proposal.ID())\n\tif p == nil {\n\t\tpanic(\"expected proposal to be finished\")\n\t}\n\n\tprintln(string(p.Status()))\n\tprintln(executed)\n}\n\n// Output:\n// executed\n// true\n"},{"name":"z_6_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_6_b_filetest\n\npackage z_6_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst owner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\n\tcommondao.Execute(cross(cur), 404, 1)\n}\n\n// Error:\n// DAO not found\n"},{"name":"z_6_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_6_c_filetest\n\npackage z_6_c_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tdaoID = commondao.New(cross(cur), \"Foo\").ID()\n}\n\nfunc main(cur realm) {\n\tcommondao.Execute(cross(cur), daoID, 1)\n}\n\n// Error:\n// caller is not a DAO member\n"},{"name":"z_6_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_6_d_filetest\n\npackage z_6_d_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\n\t// Configure DAO\n\tdao.Members().Add(user)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.Execute(cross(cur), dao.ID(), 404)\n}\n\n// Error:\n// proposal not found\n"},{"name":"z_6_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_6_e_filetest\n\npackage z_6_e_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// Create a new DAO which gives ownership to `test`\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/nt/commondao/v0\"))\n\tdao := commondao.New(cross(cur), \"Foo\", commondao.AllowExecution(false)) // Disallow execution\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\tcommondao.Execute(cross(cur), daoID, 0)\n}\n\n// Error:\n// proposal execution is not enabled\n"},{"name":"z_7_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_7_a_filetest\n\npackage z_7_a_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tname  = \"Foo\"\n)\n\nvar parentID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create the root DAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/nt/commondao/v0\"))\n\tparentDAO := commondao.New(cross(cur), \"Parent DAO\", commondao.AllowChildren(true))\n\tparentID = parentDAO.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/nt/commondao/v0\"))\n\n\tdao := commondao.NewSubDAO(cross(cur), name, parentID)\n\tif dao == nil {\n\t\tpanic(\"expected subDAO to be created\")\n\t}\n\n\tprintln(dao.Name() == name)\n\tprintln(dao.Parent().ID() == parentID)\n\n\t// Check that SubDAO is added as a child to the parent DAO\n\tif v, ok := dao.Parent().Children().Get(0); ok \u0026\u0026 v != nil {\n\t\tif subDAO, ok := v.(*pcommondao.CommonDAO); ok {\n\t\t\tprintln(subDAO.ID() == dao.ID())\n\t\t}\n\t}\n}\n\n// Output:\n// true\n// true\n// true\n"},{"name":"z_7_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_7_b_filetest\n\npackage z_7_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.NewSubDAO(cross(cur), \"Foo\", 404)\n}\n\n// Error:\n// DAO not found\n"},{"name":"z_7_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_7_c_filetest\n\npackage z_7_c_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar parentID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create the root DAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tparentID = commondao.New(cross(cur), \"Parent DAO\").ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\tcommondao.NewSubDAO(cross(cur), \"\", parentID)\n}\n\n// Error:\n// DAO name is empty\n"},{"name":"z_7_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_7_d_filetest\n\npackage z_7_d_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar parentID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create the root DAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/nt/commondao/v0\"))\n\tparentDAO := commondao.New(cross(cur), \"Parent DAO\", commondao.AllowChildren(false)) // Disallow SubDAOs\n\tparentID = parentDAO.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/nt/commondao/v0\"))\n\n\tcommondao.NewSubDAO(cross(cur), \"Foo\", parentID)\n}\n\n// Error:\n// SubDAO support is not enabled\n"},{"name":"z_7_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_7_e_filetest\n\npackage z_7_e_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner  = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser   = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tcaller = address(\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\") // @test2\n)\n\nvar parentID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create the root DAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\tparentID = commondao.New(cross(cur), \"Parent DAO\").ID()\n}\n\nfunc main(cur realm) {\n\t// Call with a realm that is not the owner of the parent DAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\n\tcommondao.NewSubDAO(cross(cur), \"Foo\", parentID)\n}\n\n// Error:\n// unauthorized\n"},{"name":"z_8_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_8_a_filetest\n\npackage z_8_a_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar rootID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO with subDAOs\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\trootDAO := commondao.New(cross(cur), \"Root DAO\")\n\tfooDAO := commondao.NewSubDAO(cross(cur), \"A\", rootDAO.ID())\n\tbarDAO := commondao.NewSubDAO(cross(cur), \"B\", fooDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"C\", barDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"D\", fooDAO.ID())\n\n\trootID = rootDAO.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\titer := commondao.NewIterator(cross(cur), rootID)\n\n\tprintln(\"Count =\", iter.Count())\n\n\tfor iter.Next() {\n\t\tif dao := iter.DAO(); dao != nil {\n\t\t\tprintln(dao.Name())\n\t\t}\n\t}\n}\n\n// Output:\n// Count = 4\n// A\n// B\n// C\n// D\n"},{"name":"z_8_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_8_b_filetest\n\npackage z_8_b_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar rootID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO with subDAOs\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\trootDAO := commondao.New(cross(cur), \"Root DAO\")\n\tfooDAO := commondao.NewSubDAO(cross(cur), \"A\", rootDAO.ID())\n\tbarDAO := commondao.NewSubDAO(cross(cur), \"B\", fooDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"C\", barDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"D\", fooDAO.ID())\n\n\trootID = rootDAO.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\titer := commondao.NewIterator(cross(cur), rootID, commondao.WithCount(2))\n\n\tprintln(\"Count =\", iter.Count())\n\n\tfor iter.Next() {\n\t\tif dao := iter.DAO(); dao != nil {\n\t\t\tprintln(dao.Name())\n\t\t}\n\t}\n}\n\n// Output:\n// Count = 2\n// A\n// B\n"},{"name":"z_8_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_8_c_filetest\n\npackage z_8_c_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar rootID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO with subDAOs\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\trootDAO := commondao.New(cross(cur), \"Root DAO\")\n\tfooDAO := commondao.NewSubDAO(cross(cur), \"A\", rootDAO.ID())\n\tbarDAO := commondao.NewSubDAO(cross(cur), \"B\", fooDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"C\", barDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"D\", fooDAO.ID())\n\n\trootID = rootDAO.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\titer := commondao.NewIterator(cross(cur), rootID, commondao.WithOffset(2))\n\n\tprintln(\"Count =\", iter.Count())\n\n\tfor iter.Next() {\n\t\tif dao := iter.DAO(); dao != nil {\n\t\t\tprintln(dao.Name())\n\t\t}\n\t}\n}\n\n// Output:\n// Count = 2\n// C\n// D\n"},{"name":"z_8_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_8_d_filetest\n\npackage z_8_d_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar rootID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO with subDAOs\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\trootDAO := commondao.New(cross(cur), \"Root DAO\")\n\tfooDAO := commondao.NewSubDAO(cross(cur), \"A\", rootDAO.ID())\n\tbarDAO := commondao.NewSubDAO(cross(cur), \"B\", fooDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"C\", barDAO.ID())\n\tcommondao.NewSubDAO(cross(cur), \"D\", fooDAO.ID())\n\n\trootID = rootDAO.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\titer := commondao.NewIterator(cross(cur), rootID, commondao.WithOffset(1), commondao.WithCount(2))\n\n\tprintln(\"Count =\", iter.Count())\n\n\tfor iter.Next() {\n\t\tif dao := iter.DAO(); dao != nil {\n\t\t\tprintln(dao.Name())\n\t\t}\n\t}\n}\n\n// Output:\n// Count = 2\n// B\n// C\n"},{"name":"z_8_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_8_e_filetest\n\npackage z_8_e_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar rootID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO without subDAOs\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\trootID = commondao.New(cross(cur), \"Root DAO\").ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\n\titer := commondao.NewIterator(cross(cur), rootID)\n\n\tprintln(\"Count =\", iter.Count())\n\tprintln(iter.Next())\n}\n\n// Output:\n// Count = 0\n// false\n"},{"name":"z_8_f_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/z_8_f_filetest\n\npackage z_8_f_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar rootID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO without subDAOs\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/test\"))\n\trootID = commondao.New(cross(cur), \"Root DAO\").ID()\n}\n\nfunc main(cur realm) {\n\t// Use a caller that is not the owner of the DAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/tests\"))\n\n\tcommondao.NewIterator(cross(cur), rootID)\n}\n\n// Error:\n// unauthorized\n"},{"name":"zp_0_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_a_filetest\n\npackage zp_0_a_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner         = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser          = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tnewMembers    = \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\\ng147ah9520z0r6jh9mjr6c75rv6l8aypzvcd3f7d\"\n\tremoveMembers = \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowMembersUpdate(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tpID := commondao.CreateMembersUpdateProposal(cross(cur), dao.ID(), newMembers, removeMembers)\n\n\tp := dao.ActiveProposals().Get(pID)\n\tif p == nil {\n\t\tpanic(\"expected proposal to be created\")\n\t}\n\n\tprintln(string(p.Status()))\n\tprintln(p.Creator() == user)\n\tprintln(p.Definition().Title() == \"Members Update\")\n\tprintln(\"\")\n\tprintln(p.Definition().Body())\n}\n\n// Output:\n// active\n// true\n// true\n//\n// **Members to Add:**\n// - g147ah9520z0r6jh9mjr6c75rv6l8aypzvcd3f7d\n// - g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\n//\n//\n// **Members to Remove:**\n// - g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\n//\n"},{"name":"zp_0_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_b_filetest\n\npackage zp_0_b_filetest\n\nimport (\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nfunc main(cur realm) {\n\tcommondao.CreateMembersUpdateProposal(cross(cur), 404, \"\", \"\")\n}\n\n// Error:\n// DAO not found\n"},{"name":"zp_0_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_d_filetest\n\npackage zp_0_d_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO with a subDAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdaoID = dao.ID()\n\n\toptions := commondao.GetOptions(cross(cur), daoID)\n\toptions.SetAllowMembersUpdate(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(\"g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5\"))\n\n\tcommondao.CreateMembersUpdateProposal(cross(cur), daoID, \"\", \"\")\n}\n\n// Error:\n// caller is not a DAO member\n"},{"name":"zp_0_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_e_filetest\n\npackage zp_0_e_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\t// Invite a user to be able to start creating DAOs\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\t// The origin must be the invited user where invitation\n\t// is removed after the first user call to create a DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\t// Create root DAO with a subDAO\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\tdaoID = dao.ID()\n\n\toptions := commondao.GetOptions(cross(cur), daoID)\n\toptions.SetAllowMembersUpdate(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateMembersUpdateProposal(cross(cur), daoID, \"not-an-address\", \"\")\n}\n\n// Error:\n// invalid address: not-an-address\n"},{"name":"zp_0_f_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_f_filetest\n\npackage zp_0_f_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tsubDAO := commondao.NewSubDAO(cross(cur), \"Bar\", dao.ID())\n\tsubDAO.Members().Add(user)\n\n\tdaoID = dao.ID()\n\n\toptions := commondao.GetOptions(cross(cur), daoID)\n\toptions.SetAllowMembersUpdate(false) // Disallow udpating members\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateMembersUpdateProposal(cross(cur), daoID, \"\", \"\")\n}\n\n// Error:\n// forbidden\n"},{"name":"zp_0_g_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_g_filetest\n\npackage zp_0_g_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowMembersUpdate(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateMembersUpdateProposal(cross(cur), daoID, \"\", \"\")\n}\n\n// Error:\n// no members were specified to be added or removed\n"},{"name":"zp_0_h_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_h_filetest\n\npackage zp_0_h_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowMembersUpdate(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateMembersUpdateProposal(cross(cur), daoID, user.String(), \"\")\n}\n\n// Error:\n// address is already a DAO member: g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\n"},{"name":"zp_0_i_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_0_i_filetest\n\npackage zp_0_i_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowMembersUpdate(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateMembersUpdateProposal(cross(cur), daoID, \"\", owner.String())\n}\n\n// Error:\n// address is not a DAO member: g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\n"},{"name":"zp_1_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_1_a_filetest\n\npackage zp_1_a_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner      = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser       = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\ttitle      = \"General Proposal\"\n\tbody       = \"Foo Bar\"\n\tvotingDays = uint8(1)\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\tdao.Members().Add(owner)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowTextProposals(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tpID := commondao.CreateTextProposal(cross(cur), dao.ID(), title, body, votingDays)\n\n\tp := dao.ActiveProposals().Get(pID)\n\tif p == nil {\n\t\tpanic(\"expected proposal to be created\")\n\t}\n\n\tprintln(string(p.Status()))\n\tprintln(p.Creator() == user)\n\tprintln(p.Definition().Title() == title)\n\tprintln(p.VotingDeadline())\n\tprintln(p.Definition().Body())\n}\n\n// Output:\n// active\n// true\n// true\n// 2009-02-14 23:31:30 +0000 UTC\n// Foo Bar\n"},{"name":"zp_1_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_1_b_filetest\n\npackage zp_1_b_filetest\n\nimport (\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nfunc main(cur realm) {\n\tcommondao.CreateTextProposal(cross(cur), 404, \"\", \"\", 0)\n}\n\n// Error:\n// DAO not found\n"},{"name":"zp_1_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_1_c_filetest\n\npackage zp_1_c_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\tdao.Members().Add(owner)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowTextProposals(false) // Disallow text proposals\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateTextProposal(cross(cur), dao.ID(), \"\", \"\", 0)\n}\n\n// Error:\n// forbidden\n"},{"name":"zp_1_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_1_d_filetest\n\npackage zp_1_d_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\tdao.Members().Add(owner)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowTextProposals(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateTextProposal(cross(cur), dao.ID(), \"\", \"\", 31)\n}\n\n// Error:\n// maximum proposal voting period is 30 days\n"},{"name":"zp_1_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_1_e_filetest\n\npackage zp_1_e_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowTextProposals(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateTextProposal(cross(cur), dao.ID(), \"\", \"\", 1)\n}\n\n// Error:\n// caller is not a DAO member\n"},{"name":"zp_2_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_a_filetest\n\npackage zp_2_a_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner   = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser    = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tname    = \"Foo SubDAO\"\n\tmembers = \"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\\ng1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowChildren(true)\n\toptions.SetAllowSubDAOPorposals(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tpID := commondao.CreateSubDAOProposal(cross(cur), dao.ID(), name, members)\n\n\tp := dao.ActiveProposals().Get(pID)\n\tif p == nil {\n\t\tpanic(\"expected proposal to be created\")\n\t}\n\n\tprintln(string(p.Status()))\n\tprintln(p.Creator() == user)\n\tprintln(p.Definition().Title() == (\"New SubDAO: \" + name))\n\tprintln(p.VotingDeadline())\n\tprintln(p.Definition().Body())\n}\n\n// Output:\n// active\n// true\n// true\n// 2009-02-20 23:31:30 +0000 UTC\n// **Parent DAO:**\n// [Foo](/r/nt/commondao/v0:2)\n//\n// **SubDAO Name:**\n// Foo SubDAO\n//\n// **Members:**\n// - g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\n// - g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\n"},{"name":"zp_2_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_b_filetest\n\npackage zp_2_b_filetest\n\nimport (\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nfunc main(cur realm) {\n\tcommondao.CreateSubDAOProposal(cross(cur), 404, \"\", \"\")\n}\n\n// Error:\n// DAO not found\n"},{"name":"zp_2_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_c_filetest\n\npackage zp_2_c_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\n\t// Disallow SubDAO creation\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowSubDAOPorposals(false)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\tcommondao.CreateSubDAOProposal(cross(cur), daoID, \"\", \"\")\n}\n\n// Error:\n// forbidden\n"},{"name":"zp_2_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_d_filetest\n\npackage zp_2_d_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowChildren(true)\n\toptions.SetAllowSubDAOPorposals(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\t// Call with a user that is not a member of the DAO\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateSubDAOProposal(cross(cur), daoID, \"\", \"\")\n}\n\n// Error:\n// caller is not a DAO member\n"},{"name":"zp_2_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_e_filetest\n\npackage zp_2_e_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowChildren(true)\n\toptions.SetAllowSubDAOPorposals(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateSubDAOProposal(cross(cur), daoID, \"Name\", \"INVALID\")\n}\n\n// Error:\n// invalid address: INVALID\n"},{"name":"zp_2_f_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_f_filetest\n\npackage zp_2_f_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n\tname  = \"Foo\"\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\tcommondao.NewSubDAO(cross(cur), name, dao.ID())\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowChildren(true)\n\toptions.SetAllowSubDAOPorposals(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateSubDAOProposal(cross(cur), daoID, name, \"\")\n}\n\n// Error:\n// a SubDAO with the same name already exists\n"},{"name":"zp_2_g_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_g_filetest\n\npackage zp_2_g_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowChildren(true)\n\toptions.SetAllowSubDAOPorposals(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateSubDAOProposal(cross(cur), daoID, \"\", \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n}\n\n// Error:\n// DAO name is empty\n"},{"name":"zp_2_h_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_2_h_filetest\n\npackage zp_2_h_filetest\n\nimport (\n\t\"strings\"\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar (\n\tdaoID uint64\n\tname  = strings.Repeat(\"A\", 61)\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowChildren(true)\n\toptions.SetAllowSubDAOPorposals(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateSubDAOProposal(cross(cur), daoID, name, \"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\")\n}\n\n// Error:\n// DAO name is too long, max length is 60 characters\n"},{"name":"zp_3_a_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_3_a_filetest\n\npackage zp_3_a_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar dao *pcommondao.CommonDAO\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowDissolutionProposals(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tpID := commondao.CreateDissolutionProposal(cross(cur), dao.ID())\n\n\tp := dao.ActiveProposals().Get(pID)\n\tif p == nil {\n\t\tpanic(\"expected proposal to be created\")\n\t}\n\n\tprintln(string(p.Status()))\n\tprintln(p.Creator() == user)\n\tprintln(p.Definition().Title() == (\"Dissolve DAO: \" + dao.Name()))\n\tprintln(p.VotingDeadline())\n\tprintln(p.Definition().Body())\n}\n\n// Output:\n// active\n// true\n// true\n// 2009-02-20 23:31:30 +0000 UTC\n// **DAO:**\n// [Foo](/r/nt/commondao/v0:2)\n"},{"name":"zp_3_b_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_3_b_filetest\n\npackage zp_3_b_filetest\n\nimport (\n\t\"testing\"\n\n\tpcommondao \"gno.land/p/nt/commondao/v0\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar (\n\tdao    *pcommondao.CommonDAO\n\tsubDAO *pcommondao.CommonDAO\n)\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao = commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowDissolutionProposals(true)\n\n\t// Create a SubDAO to be dissolveed\n\tsubDAO = commondao.NewSubDAO(cross(cur), \"Bar\", dao.ID())\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tpID := commondao.CreateDissolutionProposal(cross(cur), subDAO.ID())\n\n\tp := dao.ActiveProposals().Get(pID)\n\tif p == nil {\n\t\tpanic(\"expected proposal to be created in the parent DAO\")\n\t}\n\n\t// Make sure proposal doesn't exists in the SubDAO\n\tprintln(subDAO.ActiveProposals().Get(pID) == nil)\n\n\tprintln(string(p.Status()))\n\tprintln(p.Creator() == user)\n\tprintln(p.Definition().Title() == (\"Dissolve DAO: \" + dao.Name()))\n\tprintln(p.VotingDeadline())\n\tprintln(p.Definition().Body())\n}\n\n// Output:\n// true\n// active\n// true\n// false\n// 2009-02-20 23:31:30 +0000 UTC\n// **DAO:**\n// [Bar](/r/nt/commondao/v0:3)\n"},{"name":"zp_3_c_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_3_c_filetest\n\npackage zp_3_c_filetest\n\nimport (\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nfunc main(cur realm) {\n\tcommondao.CreateDissolutionProposal(cross(cur), 404)\n}\n\n// Error:\n// DAO not found\n"},{"name":"zp_3_d_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_3_d_filetest\n\npackage zp_3_d_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdaoID = commondao.New(cross(cur), \"Foo\").ID()\n\n\toptions := commondao.GetOptions(cross(cur), daoID)\n\toptions.SetAllowDissolutionProposals(false)\n}\n\nfunc main(cur realm) {\n\tcommondao.CreateDissolutionProposal(cross(cur), daoID)\n}\n\n// Error:\n// forbidden\n"},{"name":"zp_3_e_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_3_e_filetest\n\npackage zp_3_e_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowDissolutionProposals(true)\n\n\tdaoID = dao.ID()\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateDissolutionProposal(cross(cur), daoID)\n}\n\n// Error:\n// caller is not a DAO member\n"},{"name":"zp_3_f_filetest.gno","body":"// PKGPATH: gno.land/r/nt/commondao/v0/filetests/zp_3_f_filetest\n\npackage zp_3_f_filetest\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/r/nt/commondao/v0\"\n)\n\nconst (\n\towner = address(\"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq\") // @devx\n\tuser  = address(\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\") // @test1\n)\n\nvar daoID uint64\n\nfunc init(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(owner))\n\tcommondao.Invite(cross(cur), user)\n\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\ttesting.SetRealm(testing.NewCodeRealm(\"gno.land/r/demo/test\"))\n\tdao := commondao.New(cross(cur), \"Foo\")\n\tdao.Members().Add(user)\n\n\toptions := commondao.GetOptions(cross(cur), dao.ID())\n\toptions.SetAllowDissolutionProposals(true)\n\n\tdaoID = dao.ID()\n\n\t// Dissolve the DAO\n\tdao.SetDeleted(true)\n}\n\nfunc main(cur realm) {\n\ttesting.SetRealm(testing.NewUserRealm(user))\n\n\tcommondao.CreateDissolutionProposal(cross(cur), daoID)\n}\n\n// Error:\n// DAO has been already dissolveed\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"O0s/Rk2u3+uqDvEG5F6iHWKEESI3BimuP25IU6m9XRKEwQZ0RNtMez88BkZwhCqiePqWocfp+7T1ts9MFRtYBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"config","path":"gno.land/r/sacha/config","files":[{"name":"config.gno","body":"package config\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\nvar (\n\tOwnableMain   = ownable.NewWithAddress(\"g1jpg2hmsh8j4f60u4vkseqh5azeupns8vhrstfm\")\n\tOwnableBackup = ownable.NewWithAddress(\"g1ez7rzp7dms7he05yy9nwdczl97qw233z892hsm\")\n\n\tErrUnauthorized = errors.New(\"You're not Sacha\")\n)\n\nfunc IsAuthorized(addr address) bool {\n\treturn addr == OwnableMain.Owner() || addr == OwnableBackup.Owner()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sacha/config\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"GObx27rp/T/7BHOQVne59R/Cd/Li1mevW8SMHx8a9aYjmM0JRNEewPJlx7lmo7HkCRO036SRuX5ay1b/hTj0DA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"coinflip","path":"gno.land/r/sacha/coinflip","files":[{"name":"coinflip.gno","body":"package coinflip\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"chain/runtime\"\n\t\"chain/runtime/unsafe\"\n\t\"math/rand\"\n\n\t\"gno.land/p/demo/entropy\"\n\t\"gno.land/p/moul/fifo\"\n\t\"gno.land/r/sacha/config\"\n)\n\nconst denom = \"ugnot\"\nconst minBetAmount = 1\n\nvar (\n\tbets       = []Bet{}\n\tlatestBets = fifo.New(8)\n)\n\nfunc Heads(cur realm) {\n\truntime.AssertOriginCall()\n\n\tflipResult := flip(1)\n\tbetLogic(cross(cur), flipResult)\n}\n\nfunc Tails(cur realm) {\n\truntime.AssertOriginCall()\n\n\tflipResult := flip(0)\n\tbetLogic(cross(cur), flipResult)\n}\n\nfunc GetResults(cur realm) []Bet {\n\treturn bets\n}\n\nfunc GetRealmBalance(cur realm) int64 {\n\tbanker_ := banker.NewReadonlyBanker()\n\tcoins := banker_.GetCoins(cur.Address())\n\tbalance := coins.AmountOf(denom)\n\n\treturn balance\n}\n\nfunc Withdrawal(cur realm, amount int64) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\truntime.AssertOriginCall()\n\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\n\tif GetRealmBalance(cur) \u003c amount {\n\t\tpanic(\"insufficient balance\")\n\t}\n\n\tbanker_.SendCoins(cur.Address(), unsafe.OriginCaller(), chain.NewCoins(chain.NewCoin(denom, amount)))\n}\n\nfunc flip(choice int) bool {\n\tseed1 := uint64(entropy.New().Seed())\n\tseed2 := uint64(entropy.New().Seed())\n\n\tr := rand.New(rand.NewPCG(seed1, seed2))\n\tresult := r.IntN(2)\n\n\treturn result == choice\n}\n\nfunc getBet(cur realm) chain.Coin {\n\tbet := unsafe.OriginSend()\n\tif len(bet) != 1 || bet[0].Denom != denom {\n\t\tpanic(\"bet is invalid\")\n\t}\n\n\tamount := bet[0].Amount\n\n\tif amount \u003c minBetAmount {\n\t\tpanic(\"Bet too low\")\n\t}\n\tif amount*2 \u003e GetRealmBalance(cross(cur)) {\n\t\tpanic(\"Bet too high\")\n\t}\n\n\treturn bet[0]\n}\n\nfunc sendPrize(cur realm, caller address, amount int64) {\n\tbanker_ := banker.NewBanker(banker.BankerTypeRealmSend, cur)\n\twonCoins := chain.NewCoins(chain.NewCoin(denom, amount))\n\n\tbanker_.SendCoins(cur.Address(), caller, wonCoins)\n}\n\nfunc betLogic(cur realm, flipResult bool) {\n\tcaller := unsafe.OriginCaller()\n\tbet := getBet(cross(cur))\n\n\tif flipResult == true {\n\t\tsendPrize(cross(cur), caller, bet.Amount*2)\n\t\tchain.Emit(\"BetResult\", \"Result\", \"win\")\n\t} else {\n\t\tchain.Emit(\"BetResult\", \"Result\", \"lose\")\n\t}\n\n\tbets = append(bets, Bet{Address: caller, Result: flipResult, Amount: bet})\n\tlatestBets.Append(Bet{Address: caller, Result: flipResult, Amount: bet})\n}\n"},{"name":"coinflip_test.gno","body":"package coinflip\n\nimport (\n\t\"chain\"\n\t\"chain/banker\"\n\t\"testing\"\n\n\t\"gno.land/p/nt/testutils/v0\"\n)\n\nvar coinflipAddr = chain.PackageAddress(\"gno.land/r/sacha/coinflip\")\n\nfunc TestGetRealmBalance(cur realm, t *testing.T) {\n\tvalue := GetRealmBalance(cross(cur))\n\n\tif value != 0 {\n\t\tt.Fatalf(\"Expected 0, got %v\", value)\n\t}\n}\n\nfunc TestFlip(cur realm, t *testing.T) {\n\tvalue := flip(1)\n\n\tif value != true {\n\t\tt.Fatalf(\"Expected true, got %v\", value)\n\t}\n\n\tvalue = flip(0)\n\n\tif value != false {\n\t\tt.Fatalf(\"Expected false, got %v\", value)\n\t}\n}\n\nfunc TestWithdrawal(cur realm, t *testing.T) {\n\tbanker_ := banker.NewReadonlyBanker()\n\tsachaAddr := address(\"g1jpg2hmsh8j4f60u4vkseqh5azeupns8vhrstfm\")\n\n\ttesting.IssueCoins(coinflipAddr, chain.Coins{{\"ugnot\", 100000000}})\n\n\ttesting.SetRealm(testing.NewUserRealm(sachaAddr))\n\n\tWithdrawal(cross(cur), 1000)\n\n\tmainbal := banker_.GetCoins(coinflipAddr)\n\tsachabal := banker_.GetCoins(sachaAddr)\n\n\tif !(mainbal[0].Amount == 99999000 \u0026\u0026 sachabal[0].Amount == 1000) {\n\t\tt.Fatalf(\"Expected main balance to be 99999100 and sacha balance to be 900, got %v and %v\", mainbal, sachabal)\n\t}\n}\n\nfunc TestSendPrize(cur realm, t *testing.T) {\n\tbanker_ := banker.NewReadonlyBanker()\n\taliceAddr := testutils.TestAddress(\"alice\")\n\n\ttesting.IssueCoins(coinflipAddr, chain.Coins{{\"ugnot\", 100000000}})\n\n\ttesting.SetRealm(testing.NewUserRealm(aliceAddr))\n\n\tsendPrize(cross(cur), aliceAddr, 1000)\n\n\tmainbal := banker_.GetCoins(coinflipAddr)\n\talicebal := banker_.GetCoins(aliceAddr)\n\tif !(mainbal[0].Amount == 99999000 \u0026\u0026 alicebal[0].Amount == 1000) {\n\t\tt.Fatalf(\"Expected main balance to be 99999100 and alice balance to be 900, got %v and %v\", mainbal, alicebal)\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/sacha/coinflip\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package coinflip\n\nimport (\n\t\"chain\"\n\t\"strconv\"\n\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/p/moul/txlink\"\n\t\"gno.land/r/sys/users\"\n)\n\nvar (\n\tgithubUsername    string\n\tpocInnovationName string\n)\n\nfunc init() {\n\tgithubUsername = \"dujardin09\"\n\tpocInnovationName = \"PoCInnovation\"\n}\n\nfunc renderResult(address address, result bool, amount chain.Coin) string {\n\taddressFmt := \"**\" + address.String()[:30] + \"**: \"\n\tresultFmt := \"lost\"\n\tamountFmt := \" **\" + amount.String() + \"** \"\n\temoji := \"📉\"\n\n\tdata := users.ResolveAddress(address)\n\tif data != nil {\n\t\taddressFmt = \"**\" + data.RenderLink(\"\") + \"**: \"\n\t}\n\n\tif result == true {\n\t\tresultFmt = \"won\"\n\t\temoji = \"📈\"\n\t}\n\n\treturn addressFmt + resultFmt + amountFmt + emoji\n}\n\nfunc renderLatestResults() string {\n\tsize := latestBets.Size()\n\n\tif size == 0 {\n\t\treturn \"No results yet\"\n\t}\n\n\tentries := latestBets.Entries()\n\n\tvar out string\n\tfor i := size - 1; i \u003e= 0; i-- {\n\t\tbet := entries[i].(Bet)\n\t\tout += md.BulletItem(renderResult(bet.Address, bet.Result, bet.Amount))\n\t}\n\treturn out\n}\n\nfunc renderGame() string {\n\tout := \"# \"\n\tout += md.Link(\"Heads\", txlink.Call(\"Heads\")) + \" or \"\n\tout += md.Link(\"Tails\", txlink.Call(\"Tails\")) + \" ?\\n\"\n\n\tout += md.H3(\"Latests Results:\\n\\n\")\n\tout += renderLatestResults()\n\n\treturn out\n}\n\nfunc renderRight(_ int, rlm realm) string {\n\tout := md.Image(\"Coin Flip Illustration\", \"https://ipfs.io/ipfs/QmVZxCKz3J5iMHrACNLfFWpfpfcKT2aSmZMfvZsCQnMWbf\")\n\tout += \"\\n\"\n\tout += renderBalance(0, rlm)\n\treturn out\n}\n\nfunc renderFooter() string {\n\tout := md.HorizontalRule()\n\tout += md.BulletList([]string{\n\t\tmd.Link(\"Home\", \"home\"),\n\t\tmd.Link(\"GitHub: @\"+githubUsername, \"https://github.com/\"+githubUsername),\n\t\tmd.Link(\"PoC-Innovation\", \"https://github.com/\"+pocInnovationName),\n\t})\n\treturn out\n}\n\nfunc renderBalance(_ int, rlm realm) string {\n\tbalance := strconv.Itoa(int(GetRealmBalance(cross(rlm))))\n\tout := md.H2(\"Balance: \" + balance + denom + \"\\n\")\n\n\treturn out\n}\n\nfunc Render(cur realm, _ string) string {\n\tout := md.Columns([]string{\n\t\trenderGame(),\n\t\trenderRight(0, cur),\n\t}, false)\n\n\tout += renderFooter()\n\n\treturn out\n}\n"},{"name":"types.gno","body":"package coinflip\n\nimport (\n\t\"chain\"\n)\n\ntype Bet struct {\n\tAddress address\n\tResult  bool\n\tAmount  chain.Coin\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"JUU+/GQxS8QAStmVqvaN4uxVJA8jqH5V0JnyrbRMLBbRPc9/z/gBX2NBxAhRk0yVKhH5xUR8h+m65oMJObzODw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/sacha/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/sacha/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"gno.land/p/moul/md\"\n\t\"gno.land/r/leon/hor\"\n)\n\nvar (\n\tbanner            string\n\ttitle             string\n\tstory             string\n\tgame              map[string]string\n\tart               string\n\tgithubUsername    string\n\tpocInnovationName string\n)\n\nfunc init(cur realm) {\n\thor.Register(cross(cur), \"Sacha's Home Realm\", \"A home away from home.\")\n\n\tbanner = \"https://ipfs.io/ipfs/QmWKvxG4GifnCbCp5yPMpNmkjr7jvR9rHTmwA86Daap9sY\"\n\tstory = \"In the quiet layers of gno.land, a gnome brushed against a line of forgotten code. No one saw him. No one knows what he changed. But sometimes, in the logs...\\n\\n\\\"gnonk.\\\"\\n\\nJust once. Then silence.\"\n\ttitle = \"Sacha's Space 🏗️\"\n\tgame = make(map[string]string)\n\tgame[\"coinflip\"] = \"🪙 Flip a coin!\"\n\tart = `⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣾⣿⣿⣷⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣦⡀⠒⢶⣄⠀⠀⠀⠀⠀⠀⠀\n⠀⢰⣶⣷⣶⣶⣤⣄⠀⣠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣾⣿⡆⠀⠀⠀⠀⠀⠀\n⠀⢿⣿⣿⣿⣿⡟⢁⣄⠙⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀\n⠀⠘⣿⣿⣿⣿⣧⡈⠻⢷⣦⣄⡉⠛⠿⢿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀\n⠀⠀⠈⠻⣿⣿⣿⣿⣶⣄⡈⠙⠻⢷⣶⣤⣄⣈⡉⠛⠛⠛⠃⢠⣀⣀⡀⠀⠀⠀\n⠀⠀⠀⠀⠈⠙⠻⢿⣿⣿⣿⣿⣶⣦⣤⣍⣉⠙⠛⠛⠛⠿⠃⢸⣿⣿⣿⣷⡀⠀\n⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠻⠿⣿⣿⣿⣿⣿⣿⣿⣷⣶⣶⣾⣿⣿⣿⣿⣿⣧⠀\n⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠙⠛⠻⠏⠀⠉⠻⢿⣿⣿⣿⣿⠿⠋⠀`\n\tgithubUsername = \"dujardin09\"\n\tpocInnovationName = \"PoCInnovation\"\n}\n\nfunc renderStory() string {\n\tout := md.H2(\"📖 Story\")\n\tout += md.Paragraph(story)\n\treturn out\n}\n\nfunc renderGame() string {\n\tout := md.H2(\"🎰 Game\")\n\tfor name, title := range game {\n\t\tout += md.H3(md.Link(title, name))\n\t}\n\treturn out\n}\n\nfunc renderArt() string {\n\tout := md.H2(\"🎨 Art\")\n\tout += md.CodeBlock(art)\n\treturn out\n}\n\nfunc renderBody() string {\n\treturn md.Columns([]string{\n\t\trenderStory(),\n\t\trenderGame(),\n\t\trenderArt(),\n\t}, false)\n}\n\nfunc renderTitle() string {\n\treturn md.H1(title)\n}\n\nfunc renderBanner() string {\n\tout := md.Paragraph(md.Image(\"banner\", banner))\n\treturn out\n}\n\nfunc renderFooter() string {\n\tout := md.HorizontalRule()\n\tout += md.BulletList([]string{\n\t\tmd.Link(\"Home\", \"home\"),\n\t\tmd.Link(\"GitHub: @\"+githubUsername, \"https://github.com/\"+githubUsername),\n\t\tmd.Link(\"PoC-Innovation\", \"https://github.com/\"+pocInnovationName),\n\t})\n\treturn out\n}\n\nfunc Render(_ string) string {\n\tout := renderBanner()\n\tout += renderTitle()\n\tout += renderBody()\n\tout += renderFooter()\n\n\treturn out\n}\n"},{"name":"update.gno","body":"package home\n\nimport (\n\t\"gno.land/r/sacha/config\"\n)\n\nfunc UpdateTitle(cur realm, newTitle string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\ttitle = newTitle\n}\n\nfunc UpdateBanner(cur realm, newBanner string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tbanner = newBanner\n}\n\nfunc UpdateStory(cur realm, newStory string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tstory = newStory\n}\n\nfunc UpdateArt(cur realm, newArt string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tart = newArt\n}\n\nfunc UpdateFooter(cur realm, newGitHubUsername string, newPocInnovationName string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tgithubUsername = newGitHubUsername\n\tpocInnovationName = newPocInnovationName\n}\n\nfunc RemoveGame(cur realm, name string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tif _, exists := game[name]; exists {\n\t\tdelete(game, name)\n\t} else {\n\t\tpanic(\"Game not found\")\n\t}\n}\n\nfunc AddGame(cur realm, name string, title string) {\n\tif !config.IsAuthorized(cur.Previous().Address()) {\n\t\tpanic(config.ErrUnauthorized)\n\t}\n\n\tgame[name] = title\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"185BaGrtWMNwCI3m28NVPXaqs2yZtHK8aHm06e/cnFq2GCdDFOCFd4lEVSZRs6675Q9fI/CDU+clDUZMJHwxBQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen","package":{"name":"daodemo","path":"gno.land/r/samcrew/daodemo","files":[{"name":"daodemo.gno","body":"package daodemo\n\n// This is the most basic example of a DAO using DAOKIT.\n// It is a simple DAO that has a single admin role and a single public-relationships role.\n// It is used to demonstrate the basic functionality of DAOKIT.\n\nimport (\n\t\"gno.land/p/samcrew/basedao\"\n\t\"gno.land/p/samcrew/daocond\"\n\t\"gno.land/r/demo/profile\"\n)\n\n// DAO is the canonical public surface — a concrete *basedao.DAOWrapper,\n// NOT the daokit.DAO interface. Holding the concrete wrapper prevents\n// any /r/ from down-casting to the address-typed interface (which would\n// re-introduce a Class-2 designation-forgery surface). All mutating\n// methods on *DAOWrapper take (_ int, rlm realm, ...) and validate\n// IsCurrent before deriving the caller.\nvar DAO *basedao.DAOWrapper\n\n// TODO: add a little bit more complexity to show daocond initialization\nfunc init(cur realm) {\n\tinitialRoles := []basedao.RoleInfo{\n\t\t{Name: \"admin\", Description: \"Admin is the superuser\"},\n\t\t{Name: \"public-relationships\", Description: \"Responsible of communication with the public\"},\n\t\t{Name: \"finance-officer\", Description: \"Responsible of funds management\"},\n\t}\n\n\tinitialMembers := []basedao.Member{\n\t\t{Address: \"g126gx6p6d3da4ymef35ury6874j6kys044r7zlg\", Roles: []string{\"admin\", \"public-relationships\"}},\n\t\t{Address: \"g1ld6uaykyugld4rnm63rcy7vju4zx23lufml3jv\", Roles: []string{\"public-relationships\"}},\n\t\t{Address: \"g1r69l0vhp7tqle3a0rk8m8fulr8sjvj4h7n0tth\", Roles: []string{\"finance-officer\"}},\n\t\t{Address: \"g16jv3rpz7mkt0gqulxas56se2js7v5vmc6n6e0r\", Roles: []string{}},\n\t}\n\n\t// create the member store now to be able to use it in the condition\n\tmemberStore := basedao.NewMembersStore(initialRoles, initialMembers)\n\n\tmembersMajority := daocond.MembersThreshold(0.6, memberStore.IsMember, memberStore.MembersCount)\n\tpublicRelationships := daocond.RoleCount(1, \"public-relationships\", memberStore.HasRole)\n\tfinanceOfficer := daocond.RoleCount(1, \"finance-officer\", memberStore.HasRole)\n\n\t// and \u0026 or use va_args so you can pass as many conditions as you want\n\tadminCond := daocond.And(membersMajority, publicRelationships, financeOfficer)\n\n\tDAO = basedao.New(\u0026basedao.Config{\n\t\tName:             \"Demo DAOKIT DAO\",\n\t\tDescription:      \"This is a demo DAO built with [DAOKIT](/p/samcrew/daokit)\",\n\t\tMembers:          memberStore,\n\t\tInitialCondition: adminCond,\n\t\tGetProfileString: profile.GetStringField,\n\t\tSetProfileString: profile.SetStringField,\n\t}, cur)\n}\n\nfunc Vote(cur realm, proposalID uint64, vote daocond.Vote) {\n\tDAO.Vote(0, cur, proposalID, vote)\n}\n\nfunc Execute(cur realm, proposalID uint64) {\n\tDAO.Execute(0, cur, proposalID)\n}\n\nfunc Render(path string) string {\n\treturn DAO.Render(path)\n}\n"},{"name":"daodemo_test.gno","body":"package daodemo\n\nimport \"testing\"\n\nfunc TestInit(t *testing.T) {\n\tmembersCount := DAO.Private().Members.MembersCount()\n\tif membersCount != 4 {\n\t\tt.Fatalf(\"Expected 4 members, got %d\", membersCount)\n\t}\n\n\troles := DAO.Private().Members.GetRoles()\n\texpectedRoles := []string{\"admin\", \"public-relationships\", \"finance-officer\"}\n\tif len(roles) != len(expectedRoles) {\n\t\tt.Fatalf(\"Expected %d roles, got %d\", len(expectedRoles), len(roles))\n\t}\n\tfor _, role := range roles {\n\t\terr := true\n\t\tfor _, expectedRole := range expectedRoles {\n\t\t\tif role == expectedRole {\n\t\t\t\terr = false\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tif err {\n\t\t\tt.Fatalf(\"Expected roles %v, got %v\", expectedRoles, roles)\n\t\t}\n\t}\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/samcrew/daodemo\"\ngno = \"0.9\"\n\n[addpkg]\n  creator = \"g1kfd9f5zlvcvy6aammcmqswa7cyjpu2nyt9qfen\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"sFIOAt8lNZ6YeVCie7xecMqnPmk9PKwN5/+pHIxQg2BSNdLCltsufjraiUvCHFR37jPOCMOupvf0yt39o1AgDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"registry","path":"gno.land/r/stefann/registry","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/stefann/registry\"\ngno = \"0.9\"\n"},{"name":"registry.gno","body":"package registry\n\nimport (\n\t\"errors\"\n\n\t\"gno.land/p/nt/ownable/v0\"\n)\n\nvar (\n\tmainAddr   address\n\tbackupAddr address\n\towner      *ownable.Ownable\n)\n\nfunc init() {\n\tmainAddr = \"g1sd5ezmxt4rwpy52u6wl3l3y085n8x0p6nllxm8\"\n\tbackupAddr = \"g13awn2575t8s2vf3svlprc4dg0e9z5wchejdxk8\"\n\n\towner = ownable.NewWithAddress(mainAddr)\n}\n\nfunc MainAddr() address {\n\treturn mainAddr\n}\n\nfunc BackupAddr() address {\n\treturn backupAddr\n}\n\nfunc SetMainAddr(cur realm, addr address) error {\n\tif !addr.IsValid() {\n\t\treturn errors.New(\"config: invalid address\")\n\t}\n\n\towner.AssertOwnedBy(cur.Previous().Address())\n\n\tmainAddr = addr\n\treturn nil\n}\n\nfunc SetBackupAddr(cur realm, addr address) error {\n\tif !addr.IsValid() {\n\t\treturn errors.New(\"config: invalid address\")\n\t}\n\n\towner.AssertOwnedBy(cur.Previous().Address())\n\n\tbackupAddr = addr\n\treturn nil\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"smjg1fMOOtKzFq92yJxuNn75ficiYfnky1dO6hycdIKJJsIeNXU1dJbJNjQ8qbOG+s0pijESKhfBxsjFNfg0BQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"home","path":"gno.land/r/sunspirit/home","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/sunspirit/home\"\ngno = \"0.9\"\n"},{"name":"home.gno","body":"package home\n\nimport (\n\t\"strings\"\n\n\t\"gno.land/p/nt/ufmt/v0\"\n\t\"gno.land/p/sunspirit/md\"\n)\n\nfunc Render(path string) string {\n\tvar sb strings.Builder\n\n\tsb.WriteString(md.H1(\"Sunspirit's Home\") + md.LineBreak(1))\n\n\tsb.WriteString(md.Paragraph(ufmt.Sprintf(\n\t\t\"Welcome to Sunspirit’s home! This is where I’ll bring %s to Gno.land, crafted with my experience and creativity.\",\n\t\tmd.Italic(md.Bold(\"simple, useful dapps\")),\n\t)) + md.LineBreak(1))\n\n\tsb.WriteString(md.Paragraph(ufmt.Sprintf(\n\t\t\"📚 I’ve created a Markdown rendering library at %s. Feel free to use it for your own projects!\",\n\t\tmd.Link(\"gno.land/p/sunspirit/md\", \"/p/sunspirit/md\"),\n\t)) + md.LineBreak(1))\n\n\tsb.WriteString(md.Paragraph(\"💬 I’d love to hear your feedback to help improve this library!\") + md.LineBreak(1))\n\n\tsb.WriteString(md.Paragraph(ufmt.Sprintf(\n\t\t\"🌐 You can check out a demo of this package in action at %s.\",\n\t\tmd.Link(\"gno.land/r/sunspirit/md\", \"/r/sunspirit/md\"),\n\t)) + md.LineBreak(1))\n\tsb.WriteString(md.HorizontalRule())\n\n\treturn sb.String()\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"w2vuAAGd2n9Y17TSugYF0vGZx+xR1/X9SCRx63yedYgeycWDysvr3+C6bW0Kdk90M/6v5EWFkOyXzna5lB7CDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"md","path":"gno.land/r/sunspirit/md","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/sunspirit/md\"\ngno = \"0.9\"\n"},{"name":"md.gno","body":"package md\n\nimport (\n\t\"gno.land/p/sunspirit/md\"\n\t\"gno.land/p/sunspirit/table\"\n)\n\nfunc Render(path string) string {\n\ttitle := \"A simple, flexible, and easy-to-use library for creating markdown documents in gno.land\"\n\n\tmdBuilder := md.NewBuilder().\n\t\tAdd(md.H1(md.Italic(md.Bold(title)))).\n\n\t\t// Bold Text section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"1. Bold Text\")),\n\t\t\tmd.Paragraph(\"To make text bold, use the `md.Bold()` function:\"),\n\t\t\tmd.Bold(\"This is bold text\"),\n\t\t).\n\n\t\t// Italic Text section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"2. Italic Text\")),\n\t\t\tmd.Paragraph(\"To make text italic, use the `md.Italic()` function:\"),\n\t\t\tmd.Italic(\"This is italic text\"),\n\t\t).\n\n\t\t// Strikethrough Text section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"3. Strikethrough Text\")),\n\t\t\tmd.Paragraph(\"To add strikethrough, use the `md.Strikethrough()` function:\"),\n\t\t\tmd.Strikethrough(\"This text is strikethrough\"),\n\t\t).\n\n\t\t// Headers section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"4. Headers (H1 to H6)\")),\n\t\t\tmd.Paragraph(\"You can create headers (H1 to H6) using the `md.H1()` to `md.H6()` functions:\"),\n\t\t\tmd.H1(\"This is a level 1 header\"),\n\t\t\tmd.H2(\"This is a level 2 header\"),\n\t\t\tmd.H3(\"This is a level 3 header\"),\n\t\t\tmd.H4(\"This is a level 4 header\"),\n\t\t\tmd.H5(\"This is a level 5 header\"),\n\t\t\tmd.H6(\"This is a level 6 header\"),\n\t\t).\n\n\t\t// Bullet List section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"5. Bullet List\")),\n\t\t\tmd.Paragraph(\"To create bullet lists, use the `md.BulletList()` function:\"),\n\t\t\tmd.BulletList([]string{\"Item 1\", \"Item 2\", \"Item 3\"}),\n\t\t).\n\n\t\t// Ordered List section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"6. Ordered List\")),\n\t\t\tmd.Paragraph(\"To create ordered lists, use the `md.OrderedList()` function:\"),\n\t\t\tmd.OrderedList([]string{\"First\", \"Second\", \"Third\"}),\n\t\t).\n\n\t\t// Todo List section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"7. Todo List\")),\n\t\t\tmd.Paragraph(\"You can create a todo list using the `md.TodoList()` function, which supports checkboxes:\"),\n\t\t\tmd.TodoList([]string{\"Task 1\", \"Task 2\"}, []bool{true, false}),\n\t\t).\n\n\t\t// Blockquote section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"8. Blockquote\")),\n\t\t\tmd.Paragraph(\"To create blockquotes, use the `md.Blockquote()` function:\"),\n\t\t\tmd.Blockquote(\"This is a blockquote.\\nIt can span multiple lines.\"),\n\t\t).\n\n\t\t// Inline Code section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"9. Inline Code\")),\n\t\t\tmd.Paragraph(\"To insert inline code, use the `md.InlineCode()` function:\"),\n\t\t\tmd.InlineCode(\"fmt.Println() // inline code\"),\n\t\t).\n\n\t\t// Code Block section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"10. Code Block\")),\n\t\t\tmd.Paragraph(\"For multi-line code blocks, use the `md.CodeBlock()` function:\"),\n\t\t\tmd.CodeBlock(\"package main\\n\\nfunc main() {\\n\\t// Your code here\\n}\"),\n\t\t).\n\n\t\t// Horizontal Rule section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"11. Horizontal Rule\")),\n\t\t\tmd.Paragraph(\"To add a horizontal rule (separator), use the `md.HorizontalRule()` function:\"),\n\t\t\tmd.LineBreak(1),\n\t\t\tmd.HorizontalRule(),\n\t\t).\n\n\t\t// Language-specific Code Block section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"12. Language-specific Code Block\")),\n\t\t\tmd.Paragraph(\"To create language-specific code blocks, use the `md.LanguageCodeBlock()` function:\"),\n\t\t\tmd.LanguageCodeBlock(\"go\", \"package main\\n\\nfunc main() {}\"),\n\t\t).\n\n\t\t// Hyperlink section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"13. Hyperlink\")),\n\t\t\tmd.Paragraph(\"To create a hyperlink, use the `md.Link()` function:\"),\n\t\t\tmd.Link(\"Gnoland official docs\", \"https://docs.gno.land\"),\n\t\t).\n\n\t\t// Image section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"14. Image\")),\n\t\t\tmd.Paragraph(\"To insert an image, use the `md.Image()` function:\"),\n\t\t\tmd.LineBreak(1),\n\t\t\tmd.Image(\"Gnoland Logo\", \"https://gnolang.github.io/blog/2024-05-21_the-gnome/src/banner.png\"),\n\t\t).\n\n\t\t// Footnote section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"15. Footnote\")),\n\t\t\tmd.Paragraph(\"To create footnotes, use the `md.Footnote()` function:\"),\n\t\t\tmd.LineBreak(1),\n\t\t\tmd.Footnote(\"1\", \"This is a footnote.\"),\n\t\t).\n\n\t\t// Table section\n\t\tAdd(\n\t\t\tmd.H3(md.Bold(\"16. Table\")),\n\t\t\tmd.Paragraph(\"To create a table, use the `md.Table()` function. Here's an example of a table:\"),\n\t\t)\n\n\t// Create a table using the table package\n\ttb, _ := table.New([]string{\"Feature\", \"Description\"}, [][]string{\n\t\t{\"Bold\", \"Make text bold using \" + md.Bold(\"double asterisks\")},\n\t\t{\"Italic\", \"Make text italic using \" + md.Italic(\"single asterisks\")},\n\t\t{\"Strikethrough\", \"Cross out text using \" + md.Strikethrough(\"double tildes\")},\n\t})\n\tmdBuilder.Add(md.Table(tb))\n\n\t// Escaping Markdown section\n\tmdBuilder.Add(\n\t\tmd.H3(md.Bold(\"17. Escaping Markdown\")),\n\t\tmd.Paragraph(\"Sometimes, you need to escape special Markdown characters (like *, _, and `). Use the `md.EscapeMarkdown()` function for this:\"),\n\t)\n\n\t// Example of escaping markdown\n\ttext := \"- Escape special chars like *, _, and ` in markdown\"\n\tmdBuilder.Add(\n\t\tmd.H4(\"Text Without Escape:\"),\n\t\ttext,\n\t\tmd.LineBreak(1),\n\t\tmd.H4(\"Text With Escape:\"),\n\t\tmd.EscapeMarkdown(text),\n\t)\n\n\treturn mdBuilder.Render(md.LineBreak(1))\n}\n"},{"name":"md_test.gno","body":"package md\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc TestRender(t *testing.T) {\n\toutput := Render(\"\")\n\tif !strings.Contains(output, \"A simple, flexible, and easy-to-use library for creating markdown documents in gno.land\") {\n\t\tt.Errorf(\"invalid output\")\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"KqeifljHUnveJ9BYDuZeiJNJ/ZQ3sMwMx8PSW8dXqwuiZ7dRZDwEe50CWMNhIO9Y0In/MyzCeYHjjVuPUTUSDA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"storage","path":"gno.land/r/tests/vm/benchmark/storage","files":[{"name":"boards.gno","body":"package storage\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar boards avl.Tree\n\ntype Board interface {\n\tAddPost(title, content string)\n\tGetPost(id int) (Post, bool)\n\tSize() int\n}\n\n// posts are persisted in an avl tree\ntype TreeBoard struct {\n\tid    int\n\tposts *avl.Tree\n}\n\nfunc (b *TreeBoard) AddPost(title, content string) {\n\tn := b.posts.Size()\n\tp := Post{n, title, content}\n\tb.posts.Set(strconv.Itoa(n), p)\n}\n\nfunc (b *TreeBoard) GetPost(id int) (Post, bool) {\n\tp := b.posts.Get(strconv.Itoa(id))\n\tif p != nil {\n\t\treturn p.(Post), true\n\t} else {\n\t\treturn Post{}, false\n\t}\n}\n\nfunc (b *TreeBoard) Size() int {\n\treturn b.posts.Size()\n}\n\n// posts are persisted in a map\ntype MapBoard struct {\n\tid    int\n\tposts map[int]Post\n}\n\nfunc (b *MapBoard) AddPost(title, content string) {\n\tn := len(b.posts)\n\tp := Post{n, title, content}\n\tb.posts[n] = p\n}\n\nfunc (b *MapBoard) GetPost(id int) (Post, bool) {\n\tp, ok := b.posts[id]\n\tif ok {\n\t\treturn p, ok\n\t} else {\n\t\treturn Post{}, ok\n\t}\n}\n\nfunc (b *MapBoard) Size() int {\n\treturn len(b.posts)\n}\n\n// posts are persisted in a slice\ntype SliceBoard struct {\n\tid    int\n\tposts []Post\n}\n\nfunc (b *SliceBoard) AddPost(title, content string) {\n\tn := len(b.posts)\n\tp := Post{n, title, content}\n\tb.posts = append(b.posts, p)\n}\n\nfunc (b *SliceBoard) GetPost(id int) (Post, bool) {\n\tif id \u003c len(b.posts) {\n\t\tp := b.posts[id]\n\n\t\treturn p, true\n\t} else {\n\t\treturn Post{}, false\n\t}\n}\n\nfunc (b *SliceBoard) Size() int {\n\treturn len(b.posts)\n}\n\ntype Post struct {\n\tid      int\n\ttitle   string\n\tcontent string\n}\n"},{"name":"forum.gno","body":"package storage\n\nimport (\n\t\"strconv\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nfunc init() {\n\t// we write to three common data structure for persistence\n\t// avl.Tree, map and slice.\n\tposts0 := avl.NewTree()\n\tb0 := \u0026TreeBoard{0, posts0}\n\tboards.Set(strconv.Itoa(0), b0)\n\n\tposts1 := make(map[int]Post)\n\tb1 := \u0026MapBoard{1, posts1}\n\tboards.Set(strconv.Itoa(1), b1)\n\n\tposts2 := []Post{}\n\tb2 := \u0026SliceBoard{2, posts2}\n\tboards.Set(strconv.Itoa(2), b2)\n}\n\n// post to all boards.\nfunc AddPost(title, content string) {\n\tfor i := 0; i \u003c boards.Size(); i++ {\n\t\tboardId := strconv.Itoa(i)\n\t\tb := boards.Get(boardId)\n\t\tif b != nil {\n\t\t\tb.(Board).AddPost(title, content)\n\t\t}\n\t}\n}\n\nfunc GetPost(boardId, postId int) string {\n\tb := boards.Get(strconv.Itoa(boardId))\n\tvar res string\n\n\tif b != nil {\n\t\tp, ok := b.(Board).GetPost(postId)\n\t\tif ok {\n\t\t\tres = p.title + \",\" + p.content\n\t\t}\n\t}\n\treturn res\n}\n\nfunc GetPostSize(boardId int) int {\n\tb := boards.Get(strconv.Itoa(boardId))\n\tvar res int\n\n\tif b != nil {\n\t\tres = b.(Board).Size()\n\t} else {\n\t\tres = -1\n\t}\n\n\treturn res\n}\n\nfunc GetBoardSize() int {\n\treturn boards.Size()\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/benchmark/storage\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"6/4UhZH8o5A8315kVkwQBcPXg222k8OmwFvsxQ587xiGZwoKGYkPg5Ly1BuSFWx2Ukrbc65PLmHMDZapG5ZBDw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"crossrealm_c","path":"gno.land/r/tests/vm/crossrealm_c","files":[{"name":"crossrealm.gno","body":"package crossrealm_c\n\nimport (\n\t\"chain/runtime/unsafe\"\n\n\t\"gno.land/p/demo/tests/p_crossrealm\"\n\t\"gno.land/r/tests/vm/crossrealm\"\n)\n\n// EntryPoint is a non-crossing helper. Callers thread their own\n// live cur as `rlm`; `passThrough` and the downstream `cross(rlm)`\n// calls forward it into crossrealm's crossing functions.\nfunc EntryPoint(_ int, rlm realm) {\n\tprintln(\"crossrealm_c  EntryPoint: \" + unsafe.PreviousRealm().PkgPath())\n\tprintln(\"crossrealm_c  EntryPoint: \" + unsafe.PreviousRealm().Address())\n\tprintln(\" \")\n\tpassThrough(0, rlm)\n}\n\nfunc passThrough(_ int, rlm realm) {\n\towner := crossrealm.EntryPoint().Owner()\n\tprintln(\"Owner from crossrealm EntryPoint no crossing: \" + owner)\n\tprintln()\n\towner2 := crossrealm.EntryPointWithCrossing(0, rlm).Owner()\n\tprintln(\"Owner from crossrealm EntryPoint with crossing: \" + owner2)\n\tprintln()\n\towner = crossrealm.PrevRealmNoCrossing().Owner()\n\tprintln(\"Owner from crossrealm PrevRealmNoCrossing: \" + owner)\n\tprintln()\n\towner = crossrealm.PrevRealmCrossing(cross(rlm)).Owner()\n\tprintln(\"Owner from crossrealm PrevRealmCrossing: \" + owner)\n}\n\nfunc CurRealmEntryPoint(_ int, rlm realm) {\n\n\tprintln(\"crossrealm CurRealmWithCrossing: \" + crossrealm.CurRealmCrossing(cross(rlm)).Address())\n\tprintln(\"crossrealm CurRealmNoCrossing: \" + crossrealm.CurRealmNoCrossing().Address())\n\n\tprintln(\"p_crossrealm CurRealm: \" + p_crossrealm.CurrentRealm().Address())\n\tprintln(\"crossrealm PkgCurRealmWithCrossing: \" + crossrealm.PkgCurRealmCrossing(cross(rlm)).Address())\n\tprintln(\"crossrealm PkgCurRealmNoCrossing: \" + crossrealm.PkgCurRealmNoCrossing().Address())\n\n}\n"},{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/crossrealm_c\"\ngno = \"0.9\"\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"9zrwZZyrv01EUmw2Z/JF3e7A+a8JwEeerFJkeXuhyMG7TtNX4+NqKka2D8ClUpDbJ22rC4rcYAgwI9r77RRWCg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"deep","path":"gno.land/r/tests/vm/deep/very/deep","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/deep/very/deep\"\ngno = \"0.9\"\n"},{"name":"render.gno","body":"package deep\n\n// This can't be called from a MsgCall as it isn't crossing.\nfunc Render(path string) string {\n\tif path == \"\" {\n\t\treturn \"it works!\"\n\t} else {\n\t\treturn \"hi \" + path\n\t}\n}\n\n// For testing with MsgCall.\nfunc RenderCrossing(cur realm, path string) string {\n\tif path == \"\" {\n\t\treturn \"it works!\"\n\t} else {\n\t\treturn \"hi \" + path\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"EpSwQxELEfs6EyFsgSzJyNTr0RJQ7UhQVxJY6Iv4dEwUQm19oAd+OdsY+kgHLolULszQVC9tug/v3fyLJ1RdBw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"init","path":"gno.land/r/tests/vm/init","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/init\"\ngno = \"0.9\"\n"},{"name":"init.gno","body":"package init\n\nvar (\n\ts string\n)\n\nfunc init() {\n\ts = \"hello\"\n}\n\nfunc main() {\n}\n"},{"name":"init_test.gno","body":"package init\n\nimport (\n\t\"testing\"\n\n\t\"gno.land/p/nt/uassert/v0\"\n)\n\nvar (\n\th = s\n)\n\nfunc TestInit(t *testing.T) {\n\tuassert.Empty(t, h)\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"jy2D0qobYwNZdFEvVPp7FIYe+BxLuu65jORhfhPUkj6PDeyJRL+cyv1DxnF9QhnCePq0cnbJz+ooOIQgQ7XwDg=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"map_delete","path":"gno.land/r/tests/vm/map_delete","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/map_delete\"\ngno = \"0.9\"\n"},{"name":"map_delete.gno","body":"package map_delete\n\nvar mapus map[uint64]string = make(map[uint64]string)\n\nfunc init() {\n\tmapus[3] = \"three\"\n\tmapus[5] = \"five\"\n\tmapus[9] = \"nine\"\n}\n\nfunc DeleteMap(_ realm, k uint64) {\n\tdelete(mapus, k)\n}\n\nfunc GetMap(k uint64) bool {\n\t_, exist := mapus[k]\n\treturn exist\n}\n"},{"name":"map_delete_test.gno","body":"package map_delete\n\nimport \"testing\"\n\nfunc TestGetMap(t *testing.T) {\n\tif !(GetMap(3)) {\n\t\tt.Error(\"Expected true, got \", GetMap(3))\n\t}\n}\n\nfunc TestDeleteMap(cur realm, t *testing.T) {\n\tDeleteMap(cross(cur), 3)\n}\n\nfunc TestGetMapAfterDelete(t *testing.T) {\n\tif GetMap(3) {\n\t\tt.Error(\"Expected false, got \", GetMap(3))\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"8/jn/O2nvVw96qIw9aWqtMfl3FsQYl6WO05F5dVv7mP/pc/RPgka++ymhDKY5RICklE/q6gSqM6KY+rAKSfHAA=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"skipheight","path":"gno.land/r/tests/vm/skipheight","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/skipheight\"\ngno = \"0.9\"\n"},{"name":"skiptime.gno","body":"package skipheight\n"},{"name":"skiptime_test.gno","body":"package skipheight\n\nimport (\n\t\"chain/runtime\"\n\t\"testing\"\n\t\"time\"\n)\n\nfunc TestSkipHeights(t *testing.T) {\n\toldHeight := runtime.ChainHeight()\n\tshouldEQ(t, oldHeight, 123)\n\n\toldNow := time.Now().Unix()\n\tshouldEQ(t, oldNow, 1234567890)\n\n\t// skip 3 blocks == 15 seconds\n\ttesting.SkipHeights(3)\n\n\tshouldEQ(t, runtime.ChainHeight()-oldHeight, 3)\n\tshouldEQ(t, time.Now().Unix()-oldNow, 15)\n}\n\nfunc shouldEQ(t *testing.T, got, expected int64) {\n\tif got != expected {\n\t\tt.Fatalf(\"expected %d, got %d.\", expected, got)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"LlnCW2pxnvtPO4w6wcG59gDRf7YvjXcNH+EGu3kKRW174qzsAwwl/qCHULm5nABGxVHOlMHFeGOiuiwtSt3yCw=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"types","path":"gno.land/r/tests/vm/types","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/tests/vm/types\"\ngno = \"0.9\"\n"},{"name":"types.gno","body":"// package to test types behavior in various conditions (TXs, imports).\npackage types\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"gno.land/p/nt/avl/v0\"\n)\n\nvar (\n\tgInt         int      = -42\n\tgUint        uint     = 42\n\tgString      string   = \"a string\"\n\tgStringSlice []string = []string{\"a\", \"string\", \"slice\"}\n\tgError       error    = errors.New(\"an error\")\n\tgIntSlice    []int    = []int{-42, 0, 42}\n\tgUintSlice   []uint   = []uint{0, 42, 84}\n\tgTree        avl.Tree\n\t// gInterface  = any{-42, \"a string\", uint(42)}\n)\n\nfunc init() {\n\tgTree.Set(\"a\", \"content of A\")\n\tgTree.Set(\"b\", \"content of B\")\n}\n\nfunc Noop()                     {}\nfunc RetTimeNow() time.Time     { return time.Now() }\nfunc RetString() string         { return gString }\nfunc RetStringPointer() *string { return \u0026gString }\nfunc RetUint() uint             { return gUint }\nfunc RetInt() int               { return gInt }\nfunc RetUintPointer() *uint     { return \u0026gUint }\nfunc RetIntPointer() *int       { return \u0026gInt }\nfunc RetTree() avl.Tree         { return gTree }\nfunc RetIntSlice() []int        { return gIntSlice }\nfunc RetUintSlice() []uint      { return gUintSlice }\nfunc RetStringSlice() []string  { return gStringSlice }\nfunc RetError() error           { return gError }\nfunc Panic()                    { panic(\"PANIC!\") }\n\n// TODO: floats\n// TODO: typed errors\n// TODO: ret interface\n// TODO: recover\n// TODO: take types as input\n\nfunc Render(path string) string {\n\treturn \"package to test data types.\"\n}\n"},{"name":"types_test.gno","body":"package types\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"NQPbCHyoLiYOcuuIZCmQVbp5dDAjV3lZh0SHkEHdXMuo0Y+T4SOfz0tRcEyExZV72V9UnfkdB9P6gpCgoYSUDQ=="}],"memo":""}},{"tx":{"msg":[{"@type":"/vm.m_addpkg","creator":"g19a8gk7lqmejwdclafu0v2yuhfezwxjm7jt2q8u","package":{"name":"registry","path":"gno.land/r/ursulovic/registry","files":[{"name":"gnomod.toml","body":"module = \"gno.land/r/ursulovic/registry\"\ngno = \"0.9\"\n"},{"name":"registry.gno","body":"package registry\n\nimport \"errors\"\n\nvar (\n\tmainAddress   address\n\tbackupAddress address\n\n\tErrInvalidAddr  = errors.New(\"Ivan's registry: Invalid address\")\n\tErrUnauthorized = errors.New(\"Ivan's registry: Unauthorized\")\n)\n\nfunc init() {\n\tmainAddress = \"g1d24j8fwnc0w5q427fauyey4gdd30qgu69k6n0x\"\n\tbackupAddress = \"g1mw2xft3eava9kfhqw3fjj3kkf3pkammty0mtv7\"\n}\n\nfunc MainAddress() address {\n\treturn mainAddress\n}\n\nfunc BackupAddress() address {\n\treturn backupAddress\n}\n\nfunc SetMainAddress(cur realm, addr address) error {\n\tassertAuthorized(0, cur)\n\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddr\n\t}\n\n\tmainAddress = addr\n\treturn nil\n}\n\nfunc SetBackupAddress(cur realm, addr address) error {\n\tassertAuthorized(0, cur)\n\n\tif !addr.IsValid() {\n\t\treturn ErrInvalidAddr\n\t}\n\n\tbackupAddress = addr\n\treturn nil\n}\n\n// assertAuthorized checks that the caller realm (rlm.Previous()) is the\n// main or backup address. rlm must be the caller's live cur — the\n// IsCurrent gate makes the helper safe-by-construction so a future\n// non-crossing call site can't silently bypass auth by passing a\n// stashed or sibling-frame realm value. See\n// docs/resources/gno-security.md (Class-2 designation forgery).\nfunc assertAuthorized(_ int, rlm realm) {\n\tif !rlm.IsCurrent() {\n\t\tpanic(ErrUnauthorized)\n\t}\n\tcaller := rlm.Previous().Address()\n\tisAuthorized := caller == mainAddress || caller == backupAddress\n\n\tif !isAuthorized {\n\t\tpanic(ErrUnauthorized)\n\t}\n}\n"}],"type":{"@type":"/gno.MemPackageType","value":"MPUserAll"}},"send":"","max_deposit":""}],"fee":{"gas_wanted":"50000","gas_fee":"1ugnot"},"signatures":[{"pub_key":{"@type":"/tm.PubKeyEd25519","value":"HkoYvxRap2nJGSia0bPmrFO8C9KefF5mMPwtnTxjjoI="},"signature":"A7pRWvcRu/4FV3FxZxHHvZ5vU+n03NXFjhvF2Wt0DqNICsQ8KWOHGF7LGEJSwPik+sNkVYivzNmKOppAu6BXBw=="}],"memo":""}}]}}}}