Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/guestbook/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186407
  max_deposit = "5000000ugnot"

guestbook.gno

// Package guestbook is a public, on-chain guestbook realm for gno.land.
//
// Anyone can sign the guestbook with a short message via the Sign crossing
// function. Every entry records the caller's address, the message, and the
// block height at which it was signed. Render lists all entries newest-first
// as a Markdown table.
package guestbook

import (
	"strconv"
	"strings"

	"chain"
	"chain/runtime"

	"gno.land/p/nt/avl/v0"
)

// Entry is a single signed guestbook line.
type Entry struct {
	ID      int    // 1-based sequence number
	Author  string // bech32 address of the signer
	Message string // the message left by the signer
	Height  int64  // block height at which the entry was signed
}

const maxMessageLen = 280

var (
	// entries is keyed by a zero-padded sequence so avl iteration is ordered.
	entries = avl.NewTree()
	count   int
)

// Sign appends a new entry to the guestbook attributed to the immediate caller.
//
// It is a crossing function (gno 0.9 interrealm convention): callers invoke it
// as Sign(cross(cur), "hello"). The cur.IsCurrent() guard is the authentication
// primitive — without it a stale/forged realm value could spoof the author.
func Sign(cur realm, message string) {
	if !cur.IsCurrent() {
		panic("guestbook: spoofed realm; Sign must be called via cross(cur)")
	}

	author := cur.Previous().Address().String()
	e := addEntry(author, message, runtime.ChainHeight())

	chain.Emit("Signed",
		"id", strconv.Itoa(e.ID),
		"author", e.Author,
		"height", strconv.FormatInt(e.Height, 10),
	)
}

// addEntry validates the message and appends an entry. Non-crossing internal
// helper so the append/validation logic is unit-testable without a realm frame.
// Panics (aborting the tx, reverting state) on an invalid message.
func addEntry(author, message string, height int64) *Entry {
	msg := strings.TrimSpace(message)
	if msg == "" {
		panic("guestbook: message must not be empty")
	}
	if len(msg) > maxMessageLen {
		panic("guestbook: message too long (max " + strconv.Itoa(maxMessageLen) + " bytes)")
	}

	count++
	e := &Entry{
		ID:      count,
		Author:  author,
		Message: msg,
		Height:  height,
	}
	entries.Set(seqKey(count), e)
	return e
}

// Count returns the total number of signatures. Read-only, non-crossing.
func Count() int {
	return count
}

// Render lists every entry newest-first as a Markdown table plus a total count.
func Render(path string) string {
	var b strings.Builder

	b.WriteString("# Guestbook\n\n")

	if count == 0 {
		b.WriteString("_No one has signed yet. Be the first!_\n")
		return b.String()
	}

	b.WriteString("**Total signatures:** ")
	b.WriteString(strconv.Itoa(count))
	b.WriteString("\n\n")

	b.WriteString("| # | Who | Message | Height |\n")
	b.WriteString("|---|-----|---------|--------|\n")

	// avl iterates ascending by key; ReverseIterate gives newest-first.
	entries.ReverseIterate("", "", func(_ string, v any) bool {
		e := v.(*Entry)
		b.WriteString("| ")
		b.WriteString(strconv.Itoa(e.ID))
		b.WriteString(" | ")
		b.WriteString(shortAddr(e.Author))
		b.WriteString(" | ")
		b.WriteString(escapeCell(e.Message))
		b.WriteString(" | ")
		b.WriteString(strconv.FormatInt(e.Height, 10))
		b.WriteString(" |\n")
		return false
	})

	return b.String()
}

// seqKey renders a sequence number as a fixed-width, zero-padded string so
// avl's lexicographic key order matches numeric order.
func seqKey(n int) string {
	s := strconv.Itoa(n)
	const width = 16
	if len(s) >= width {
		return s
	}
	return strings.Repeat("0", width-len(s)) + s
}

// shortAddr abbreviates a bech32 address for compact display.
func shortAddr(addr string) string {
	if len(addr) <= 12 {
		return addr
	}
	return addr[:8] + "…" + addr[len(addr)-4:]
}

// escapeCell neutralizes characters that would break a Markdown table cell.
func escapeCell(s string) string {
	s = strings.ReplaceAll(s, "\\", "\\\\")
	s = strings.ReplaceAll(s, "|", "\\|")
	s = strings.ReplaceAll(s, "\r\n", " ")
	s = strings.ReplaceAll(s, "\n", " ")
	s = strings.ReplaceAll(s, "\r", " ")
	return s
}

guestbook_test.gno

package guestbook

import (
	"strings"
	"testing"

	"gno.land/p/nt/avl/v0"
)

// resetState clears package globals between tests (globals persist across
// test functions in the same package binary).
func resetState() {
	entries = avl.NewTree()
	count = 0
}

func TestAddEntryAppendsAndCounts(t *testing.T) {
	resetState()

	addEntry("g1alice", "hello world", 100)
	addEntry("g1bob", "second entry", 101)

	if got := Count(); got != 2 {
		t.Fatalf("Count() = %d, want 2", got)
	}

	out := Render("")
	if !strings.Contains(out, "**Total signatures:** 2") {
		t.Errorf("Render missing total; got:\n%s", out)
	}
	if !strings.Contains(out, "hello world") {
		t.Errorf("Render missing first message; got:\n%s", out)
	}
	if !strings.Contains(out, "second entry") {
		t.Errorf("Render missing second message; got:\n%s", out)
	}
	if !strings.Contains(out, "100") || !strings.Contains(out, "101") {
		t.Errorf("Render missing heights; got:\n%s", out)
	}
}

func TestRenderNewestFirst(t *testing.T) {
	resetState()

	addEntry("g1alice", "AAA-oldest", 1)
	addEntry("g1alice", "BBB-newest", 2)

	out := Render("")
	iNew := strings.Index(out, "BBB-newest")
	iOld := strings.Index(out, "AAA-oldest")
	if iNew < 0 || iOld < 0 {
		t.Fatalf("both messages must appear; got:\n%s", out)
	}
	if iNew > iOld {
		t.Errorf("newest entry should render before oldest; got:\n%s", out)
	}
}

func TestRenderEmpty(t *testing.T) {
	resetState()

	out := Render("")
	if !strings.Contains(out, "No one has signed yet") {
		t.Errorf("empty render should invite signing; got:\n%s", out)
	}
}

func TestEmptyMessagePanics(t *testing.T) {
	resetState()

	defer func() {
		if r := recover(); r == nil {
			t.Errorf("signing an empty message should panic")
		}
	}()
	addEntry("g1alice", "   ", 1)
}

func TestEscapeCell(t *testing.T) {
	cases := []struct {
		in, want string
	}{
		{"plain", "plain"},
		{"a|b", "a\\|b"},
		{"line1\nline2", "line1 line2"},
	}
	for _, tc := range cases {
		if got := escapeCell(tc.in); got != tc.want {
			t.Errorf("escapeCell(%q) = %q, want %q", tc.in, got, tc.want)
		}
	}
}

func TestSeqKeyOrders(t *testing.T) {
	if a, b := seqKey(2), seqKey(10); !(a < b) {
		t.Errorf("seqKey(2)=%q should sort before seqKey(10)=%q", a, b)
	}
}

render_example_test.gno

package guestbook

// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Guestbook
	//
	// _No one has signed yet. Be the first!_
}