Playground
admin.gno
package bolo // adminAddr is the registry's admin (API §3.7, §3.6): a constant in source, never // the deployer. // // Until the real admin key exists, it is an address nobody holds a key for, so a // deployment made by mistake has no admin at all. Tests and local runs stage the // realm with a test admin instead (internal/stage). const adminAddr address = "g1at2h7kdhz2m9lv6azn54mwu4wl95k94wu90uwj"
doc.gno
// Package bolo is Bolo's version registry (API §3.7): it tells clients which // version new campaigns start on. Every version lives at its own path, // gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/vN, and keeps its campaigns and their money for good (ADR // D14); the registry holds no money and only points. package bolo
errors.gno
package bolo
// Error is the type of every failure the registry reports (API C10, §7). Each one is
// a constant led by its copy key, the same keys v0 uses (UI spec §8).
type Error string
func (e Error) Error() string { return string(e) }
const (
ErrNotUserCall Error = "err.use_wallet: not a direct transaction from a wallet"
ErrSessionKey Error = "err.master_key: a session key can't do this"
ErrNotAdmin Error = "err.not_allowed: only the admin can do this"
ErrSetting Error = "err.input: invalid setting"
)
errors_test.gno
package bolo
import (
"strings"
"testing"
)
// Each error and its copy key, as API §7 pairs them for the registry.
var errorKeys = map[Error]string{
ErrNotUserCall: "err.use_wallet",
ErrSessionKey: "err.master_key",
ErrNotAdmin: "err.not_allowed",
ErrSetting: "err.input",
}
// TestErrorsLeadWithCopyKeys: every error starts with its own copy key, then ": "
// and a message (API C10).
func TestErrorsLeadWithCopyKeys(t *testing.T) {
if len(errorKeys) != 4 {
t.Errorf("%d errors, API §7 gives the registry 4", len(errorKeys))
}
for e, want := range errorKeys {
key, msg, ok := strings.Cut(string(e), ": ")
if !ok || key != want || msg == "" {
t.Errorf("%q doesn't start with %s", string(e), want)
}
}
}
gnomod.toml
module = "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo" gno = "0.9" [addpkg] creator = "g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv" height = 359083 max_deposit = "3000000ugnot"
guards.gno
package bolo
import (
"chain"
"chain/runtime"
)
// assertCurrent is the first statement of every crossing function (API C2).
func assertCurrent(cur realm) {
if !cur.IsCurrent() {
panic("bolo registry: not the current realm")
}
}
// assertCodeCaller refuses any caller but a deployed realm (API C11). A maketx run
// script is ephemeral and code at once: it is refused here, as is a user.
func assertCodeCaller(cur realm) {
if p := cur.Previous(); !p.IsCode() || p.IsEphemeral() {
panic(ErrNotUserCall)
}
}
// assertSigner runs right after C11's branch in every write: no session key, in
// either branch (C11), then the caller must be who (API §3.7).
func assertSigner(cur realm, who address) {
if _, _, _, isSession := runtime.GetSessionInfo(); isSession {
panic(ErrSessionKey)
}
if cur.Previous().Address() != who {
panic(ErrNotAdmin)
}
}
// checkCandidate refuses, with ErrSetting, a malformed address (C12: exactly 40
// characters, a lowercase "g" first, then IsValid) and the registry's own address
// and storage-deposit address, which can never accept the admin role (API §3.7).
func checkCandidate(a address) {
if len(a) != 40 || a[0] != 'g' || !a.IsValid() {
panic(ErrSetting)
}
if a == chain.PackageAddress(registryPath) || a == chain.DeriveStorageDepositAddr(registryPath) {
panic(ErrSetting)
}
}
init.gno
package bolo
// init refuses to deploy the registry from its admin's own key (API §3.7, §3.6, ADR
// D11): the key that deploys under the namespace must not also be the admin, or one
// stolen key could deploy a malicious version and point the registry at it.
func init(cur realm) {
if cur.Previous().Address() == adminAddr {
panic("bolo registry: the admin key may not deploy the registry; deploy from the namespace key, which must not be the admin")
}
}
registry.gno
package bolo
import (
"chain"
"chain/runtime"
"strings"
"gno.land/p/nt/avl/v0"
)
const (
registryPath = "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo"
versionPrefix = registryPath + "/v"
// maxVersions keeps Versions() to one bounded read (INV-9).
maxVersions = 50
// maxPathLen is the longest package path the chain deploys: a longer one could
// never hold a version.
maxPathLen = 256
)
// The registry's state (API §3.7), all unexported (C6).
var (
admin address = adminAddr // a realm once a DAO accepts the role (ADR D11)
pendingAdmin address // the proposed admin until it accepts; "" for none
versions []string // every version added, oldest first
added avl.Tree // path → true, for lookups
current string // "" until the first SetCurrent
)
// The registry's events (API §6).
const (
evVersionAdded = "VersionAdded"
evCurrentSet = "CurrentSet"
evAdminProposed = "AdminProposed"
evAdminChanged = "AdminChanged"
)
// Current returns the version new campaigns should use: a pkgpath, or "" before the
// first SetCurrent.
func Current() string { return current }
// Versions returns every version ever added, oldest first, as a copy.
func Versions() []string { return append([]string(nil), versions...) }
// GetAdmin returns the admin and the proposed admin, "" when there is none.
func GetAdmin() (address, address) { return admin, pendingAdmin }
// AddVersion registers a version. None is ever removed.
func AddVersion(cur realm, path string) {
assertCurrent(cur)
if cur.Previous().IsUserCall() {
runtime.AssertOriginCall()
} else {
assertCodeCaller(cur)
}
assertSigner(cur, admin)
checkNewVersion(path)
added.Set(path, true)
versions = append(versions, path)
chain.Emit(evVersionAdded, "path", path)
}
// SetCurrent points new campaigns, and their donors' money, at a version already
// added.
func SetCurrent(cur realm, path string) {
assertCurrent(cur)
if cur.Previous().IsUserCall() {
runtime.AssertOriginCall()
} else {
assertCodeCaller(cur)
}
assertSigner(cur, admin)
if !added.Has(path) {
panic(ErrSetting)
}
current = path
chain.Emit(evCurrentSet, "path", path)
}
// ProposeAdmin proposes next as the registry's admin, who takes over by accepting.
// A second proposal replaces the first.
func ProposeAdmin(cur realm, next address) {
assertCurrent(cur)
if cur.Previous().IsUserCall() {
runtime.AssertOriginCall()
} else {
assertCodeCaller(cur)
}
assertSigner(cur, admin)
checkCandidate(next)
pendingAdmin = next
chain.Emit(evAdminProposed, "next", next.String())
}
// AcceptAdmin makes the proposed admin the admin, and ends the proposal.
func AcceptAdmin(cur realm) {
assertCurrent(cur)
if cur.Previous().IsUserCall() {
runtime.AssertOriginCall()
} else {
assertCodeCaller(cur)
}
assertSigner(cur, pendingAdmin)
previous := admin
admin, pendingAdmin = pendingAdmin, ""
chain.Emit(evAdminChanged, "admin", admin.String(), "previous", previous.String())
}
// Render is Bolo's front page on gnoweb, the twin of the web app's Home (UI spec §3,
// §4.13): the name, the tagline, a link to the current version, then the versions,
// oldest first, the current one marked. It ignores path: nothing a visitor types
// reaches the page (API §3.7).
func Render(path string) string {
if len(versions) == 0 {
return "# Bolo\n\nNo version is registered yet.\n"
}
var b strings.Builder
b.WriteString("# Bolo\n\nGive together. See where it goes.\n\n")
if current != "" {
b.WriteString("**[Open Bolo](" + strings.TrimPrefix(current, "gno.land") + ")**, where new campaigns start.\n\n")
}
b.WriteString("## Versions\n\nEach campaign runs to its end on the version it started on: money never moves between versions.\n\n")
for _, v := range versions {
b.WriteString("- [" + v + "](" + strings.TrimPrefix(v, "gno.land") + ")")
if v == current {
b.WriteString(" — current")
}
b.WriteString("\n")
}
return b.String()
}
// checkNewVersion refuses, with ErrSetting, a path that isn't a version path, one
// already added, and one past the cap (API §3.7).
func checkNewVersion(path string) {
if !isVersionPath(path) || added.Has(path) || len(versions) >= maxVersions {
panic(ErrSetting)
}
}
// isVersionPath reports whether path names a Bolo version (API §3.7), parsed by
// hand: exactly versionPrefix, then 0 or a number without a leading zero, and
// nothing else, in at most maxPathLen bytes. v012 would be another package than
// v12, so it is refused.
func isVersionPath(path string) bool {
if len(path) > maxPathLen || !strings.HasPrefix(path, versionPrefix) {
return false
}
n := path[len(versionPrefix):]
if n == "" || (n[0] == '0' && len(n) > 1) {
return false
}
for i := 0; i < len(n); i++ {
if n[i] < '0' || n[i] > '9' {
return false
}
}
return true
}
registry_test.gno
package bolo
import (
"chain"
"strings"
"testing"
"gno.land/p/nt/testutils/v0"
)
func TestIsVersionPath(t *testing.T) {
for _, path := range []string{
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v12",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v909",
// The longest path the chain deploys: 256 bytes.
versionPrefix + "1" + strings.Repeat("0", 256-len(versionPrefix)-1),
} {
if !isVersionPath(path) {
t.Errorf("%s refused", path)
}
}
for _, path := range []string{
"",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1x",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1/x",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/V1",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1/../x",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v012",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v00",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v-1",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1 ",
" gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1",
"gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolov1",
"gno.land/r/other/bolo/v1",
"gno.land/p/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1",
// One byte too long for the chain to ever deploy.
versionPrefix + "1" + strings.Repeat("0", 256-len(versionPrefix)),
} {
if isVersionPath(path) {
t.Errorf("%q accepted", path)
}
}
}
func TestCheckNewVersion(t *testing.T) {
defer restoreVersions(versions)
checkNewVersion("gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v3")
for name, path := range map[string]string{
"not a version": "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v3x",
"added already": "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0",
} {
added.Set("gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0", true)
if !panicsWith(func() { checkNewVersion(path) }, ErrSetting) {
t.Errorf("%s: not refused", name)
}
}
// The 50th fits, the 51st doesn't (API §3.7, INV-9).
for len(versions) < 49 {
versions = append(versions, "filler")
}
checkNewVersion("gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v50")
versions = append(versions, "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v50")
if !panicsWith(func() { checkNewVersion("gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v51") }, ErrSetting) {
t.Errorf("a 51st version: not refused")
}
}
func TestCheckCandidate(t *testing.T) {
checkCandidate(testutils.TestAddress("dao"))
for name, a := range map[string]address{
"the registry": chain.PackageAddress("gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo"),
"its storage deposit": chain.DeriveStorageDepositAddr("gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo"),
"malformed": "g1notanaddress",
"in capitals": address(strings.ToUpper(string(testutils.TestAddress("dao")))),
} {
if !panicsWith(func() { checkCandidate(a) }, ErrSetting) {
t.Errorf("%s: not refused", name)
}
}
}
// TestRegistry walks a fresh registry, each write called straight from the test's
// body as AssertOriginCall requires (plan §3.3). The writes' refusals are tested in
// txtar (registry.txtar): a crossing call's panic can't be recovered here.
func TestRegistry(cur realm, t *testing.T) {
defer restoreRegistry(admin, pendingAdmin, current, versions)
if Current() != "" || len(Versions()) != 0 || !strings.Contains(Render(""), "No version is registered yet.") {
t.Errorf("a fresh registry isn't empty")
}
if a, p := GetAdmin(); a != adminAddr || p != "" {
t.Errorf("GetAdmin() = %s, %q", a, p)
}
testing.SetRealm(testing.NewUserRealm(adminAddr))
AddVersion(cross(cur), "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0")
AddVersion(cross(cur), "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1")
// No version is current yet: the page links to none.
if got := Render(""); strings.Contains(got, "Open Bolo") || !strings.Contains(got, "- [gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1](/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1)\n") {
t.Errorf("Render before SetCurrent:\n%s", got)
}
SetCurrent(cross(cur), "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1")
if Current() != "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1" {
t.Errorf("Current() = %q", Current())
}
vs := Versions()
if len(vs) != 2 || vs[0] != "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0" || vs[1] != "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1" {
t.Errorf("Versions() = %v", vs)
}
// Versions hands out a copy (C6).
vs[0] = "changed"
if Versions()[0] != "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0" {
t.Errorf("Versions shares its slice with the realm")
}
want := "# Bolo\n\nGive together. See where it goes.\n\n" +
"**[Open Bolo](/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1)**, where new campaigns start.\n\n" +
"## Versions\n\nEach campaign runs to its end on the version it started on: money never moves between versions.\n\n" +
"- [gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0](/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0)\n" +
"- [gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1](/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v1) — current\n"
if got := Render(""); got != want {
t.Errorf("Render =\n%s\nwant\n%s", got, want)
}
// Render ignores its path.
if got := Render("<b>x</b>?page=2"); got != want {
t.Errorf("Render echoes its path:\n%s", got)
}
// The handover, to a realm, which then acts through C11's realm branch.
dao := chain.PackageAddress("gno.land/r/test/dao")
ProposeAdmin(cross(cur), testutils.TestAddress("someone"))
ProposeAdmin(cross(cur), dao)
if a, p := GetAdmin(); a != adminAddr || p != dao {
t.Errorf("after two proposals: GetAdmin() = %s, %s", a, p)
}
testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/dao"))
AcceptAdmin(cross(cur))
if a, p := GetAdmin(); a != dao || p != "" {
t.Errorf("after accepting: GetAdmin() = %s, %q", a, p)
}
SetCurrent(cross(cur), "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0")
if Current() != "gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0" {
t.Errorf("the realm admin's write didn't apply")
}
}
// restoreVersions puts back the versions list and its lookup tree.
func restoreVersions(saved []string) {
for _, path := range versions[len(saved):] {
added.Remove(path)
}
added.Remove("gno.land/r/g12yg9nh4ncma44emgm8msxe8aavzywt0p95tanv/bolo/v0")
for _, path := range saved {
added.Set(path, true)
}
versions = saved
}
// restoreRegistry puts back what TestRegistry changes.
func restoreRegistry(a, pending address, cur string, saved []string) {
admin, pendingAdmin, current = a, pending, cur
restoreVersions(saved)
}
// panicsWith reports whether f panics with err.
func panicsWith(f func(), err Error) (ok bool) {
defer func() {
r := recover()
e, isErr := r.(Error)
ok = isErr && e == err
}()
f()
return false
}