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/timecapsule/v1"
gno = "0.9"
private = true

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186411
  max_deposit = "6000000ugnot"

render_example_test.gno

package timecapsule

// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # ⏳ Time Capsule Guestbook
	//
	// Leave a message for the future. It stays sealed until its unlock block height, then anyone can read it — call `Leave(message, delayBlocks)`.
	//
	// 0 capsule(s) total · 0 unlocked · 0 still sealed.
	//
	// ## Unlocked messages
	//
	// _None yet. Be the first to leave one that will open later._
}

timecapsule.gno

// Package timecapsule is a public guestbook where every message is sealed
// until a future block height, then permanently unlocked for anyone to read.
package timecapsule

import (
	"strconv"
	"strings"

	"chain"
	"chain/runtime"

	"gno.land/p/nym-moultest123/kit/store/v0"
	"gno.land/p/nt/markdown/sanitize/v0"
)

// capsule carries no id field: the id belongs to the store, which hands it
// back on lookup and iteration.
type capsule struct {
	author       address
	message      string
	createdAt    int64
	unlockHeight int64
}

// capsules assigns the capsule ids. v0 kept its own nextID plus an idKey()
// that called strings.Repeat WITHOUT the length guard the other realms had, so
// at the 10^12th capsule it did not mis-sort, it panicked: Repeat rejects a
// negative count, and the realm would have stopped accepting Leave calls. The
// store key is 8 fixed bytes with no width to outgrow.
var capsules = store.Named("capsule")

const (
	maxDelayBlocks int64 = 1_000_000
	maxMessageLen  int   = 500
)

// Leave seals a new message that stays hidden until `delayBlocks` blocks
// from now, then becomes publicly readable forever. Returns the capsule id.
func Leave(cur realm, message string, delayBlocks int64) int64 {
	if !cur.Previous().IsUserCall() {
		panic("only direct user calls can leave a capsule")
	}

	message = strings.TrimSpace(message)
	if message == "" {
		panic("message must not be empty")
	}
	if len(message) > maxMessageLen {
		panic("message too long")
	}
	if delayBlocks <= 0 || delayBlocks > maxDelayBlocks {
		panic("delayBlocks out of range")
	}

	now := runtime.ChainHeight()
	c := &capsule{
		author:       cur.Previous().Address(),
		message:      message,
		createdAt:    now,
		unlockHeight: now + delayBlocks,
	}
	id := capsules.Add(c)

	chain.Emit("CapsuleSealed",
		"id", id.String(),
		"unlockHeight", strconv.FormatInt(c.unlockHeight, 10),
	)
	return int64(id)
}

func Render(path string) string {
	switch {
	case path == "":
		return renderHome()
	case path == "sealed":
		return renderSealed()
	case strings.HasPrefix(path, "capsule/"):
		return renderCapsule(strings.TrimPrefix(path, "capsule/"))
	default:
		return "> [!WARNING]\n> Path not found\n"
	}
}

func renderHome() string {
	now := runtime.ChainHeight()

	revealed := 0
	sealed := 0
	capsules.Each(func(_ store.ID, value any) {
		if value.(*capsule).unlockHeight <= now {
			revealed++
		} else {
			sealed++
		}
	})

	var out strings.Builder
	out.WriteString("# ⏳ Time Capsule Guestbook\n\n")
	out.WriteString("Leave a message for the future. It stays sealed until its unlock block height, then anyone can read it — call `Leave(message, delayBlocks)`.\n\n")
	out.WriteString(strconv.Itoa(revealed+sealed) + " capsule(s) total · " +
		strconv.Itoa(revealed) + " unlocked · " + strconv.Itoa(sealed) + " still sealed")
	if sealed > 0 {
		out.WriteString(" (see [sealed](sealed))")
	}
	out.WriteString(".\n\n## Unlocked messages\n\n")

	if revealed == 0 {
		out.WriteString("_None yet. Be the first to leave one that will open later._\n")
		return out.String()
	}

	capsules.Each(func(id store.ID, value any) {
		c := value.(*capsule)
		if c.unlockHeight <= now {
			out.WriteString(renderEntry(id, c))
		}
	})
	return out.String()
}

func renderSealed() string {
	now := runtime.ChainHeight()

	var out strings.Builder
	out.WriteString("# Sealed Capsules\n\n")

	found := false
	capsules.Each(func(id store.ID, value any) {
		c := value.(*capsule)
		if c.unlockHeight > now {
			found = true
			out.WriteString("- capsule [#" + id.String() + "](capsule/" +
				id.String() + ") by `" + c.author.String() +
				"` — unlocks at block " + strconv.FormatInt(c.unlockHeight, 10) +
				" (" + strconv.FormatInt(c.unlockHeight-now, 10) + " blocks left)\n")
		}
	})
	if !found {
		out.WriteString("_No sealed capsules right now._\n")
	}
	return out.String()
}

func renderCapsule(idStr string) string {
	id, ok := store.ParseID(idStr)
	if !ok {
		return "> [!WARNING]\n> Invalid capsule id\n"
	}
	v, ok := capsules.Get(id)
	if !ok {
		return "> [!WARNING]\n> Capsule not found\n"
	}
	c := v.(*capsule)
	now := runtime.ChainHeight()

	var out strings.Builder
	out.WriteString("# Capsule #" + id.String() + "\n\n")
	out.WriteString("Sealed by `" + c.author.String() + "` at block " + strconv.FormatInt(c.createdAt, 10) + ".\n\n")

	if c.unlockHeight > now {
		out.WriteString("\U0001f512 Still sealed. Unlocks at block " + strconv.FormatInt(c.unlockHeight, 10) +
			" (" + strconv.FormatInt(c.unlockHeight-now, 10) + " blocks left).\n")
		return out.String()
	}

	out.WriteString("\U0001f513 Unlocked at block " + strconv.FormatInt(c.unlockHeight, 10) + ":\n\n> " +
		sanitize.InlineText(c.message) + "\n")
	return out.String()
}

func renderEntry(id store.ID, c *capsule) string {
	return "- **#" + id.String() + "** by `" + c.author.String() +
		"` (sealed at block " + strconv.FormatInt(c.createdAt, 10) +
		", opened at block " + strconv.FormatInt(c.unlockHeight, 10) + "):\n  > " +
		sanitize.InlineText(c.message) + "\n\n"
}

timecapsule_test.gno

package timecapsule

import (
	"strings"
	"testing"

	"gno.land/p/nym-moultest123/kit/store/v0"
	"gno.land/p/nt/testutils/v0"
	"gno.land/p/nt/uassert/v0"
)

func resetState() {
	capsules = store.Named("capsule")
}

func TestLeaveAndReveal(cur realm, t *testing.T) {
	resetState()
	alice := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(alice))

	// v1 ids start at 1: the store never assigns 0, so the zero id stays
	// usable as "absent". v0 handed out 0 for the first capsule.
	id := Leave(cross(cur), "hello, future", 5)
	uassert.Equal(t, int64(1), id)

	if got := Render(""); !strings.Contains(got, "1 still sealed") {
		t.Fatalf("expected capsule to still be sealed, got: %s", got)
	}
	if got := Render("sealed"); !strings.Contains(got, "capsule/1") {
		t.Fatalf("expected capsule 1 listed as sealed, got: %s", got)
	}

	testing.SkipHeights(5)

	if got := Render(""); !strings.Contains(got, "hello, future") {
		t.Fatalf("expected capsule to be revealed, got: %s", got)
	}
	if got := Render("capsule/1"); !strings.Contains(got, "hello, future") {
		t.Fatalf("expected capsule detail to reveal message, got: %s", got)
	}

	// Id 0 is never assigned, so the path is rejected rather than looked up.
	if got := Render("capsule/0"); !strings.Contains(got, "Invalid capsule id") {
		t.Fatalf("expected capsule/0 to be rejected, got: %s", got)
	}
}

func TestLeaveRejectsEmptyMessage(cur realm, t *testing.T) {
	resetState()
	alice := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(alice))

	uassert.AbortsWithMessage(t, cur, "message must not be empty", func() {
		Leave(cross(cur), "   ", 5)
	})
}

func TestLeaveRejectsBadDelay(cur realm, t *testing.T) {
	resetState()
	alice := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(alice))

	uassert.AbortsWithMessage(t, cur, "delayBlocks out of range", func() {
		Leave(cross(cur), "hi", 0)
	})
}

// Regression: an intermediate realm (not a direct EOA call) must never be
// able to seal a capsule as if it were a user.
func TestLeaveRejectsCodeRealm(cur realm, t *testing.T) {
	resetState()
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/some/attacker"))

	uassert.AbortsWithMessage(t, cur, "only direct user calls can leave a capsule", func() {
		Leave(cross(cur), "hi", 5)
	})
}

func TestRenderNotFound(t *testing.T) {
	resetState()
	if got := Render("nope"); !strings.Contains(got, "not found") {
		t.Fatalf("expected 404-ish message, got: %s", got)
	}
}