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/ringlog/v0"
gno = "0.9"

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

ringlog.gno

// Package ringlog is an on-chain port of Go's container/ring: a fixed-capacity
// circular message board where new posts overwrite the oldest entries once full.
package ringlog

import (
	"chain/runtime"
	"chain/runtime/unsafe"
	"strconv"
	"strings"
)

// Cap is the maximum number of messages the ring buffer holds.
const Cap = 16

// Entry is a single posted message.
type Entry struct {
	Author  address
	Message string
	Height  int64
}

var (
	buf   [Cap]Entry // the ring storage
	head  int        // index of the next write slot
	count int        // number of entries currently stored (0 ≤ count ≤ Cap)
)

// Post adds a message to the ring buffer.
// Once Cap entries are stored, the oldest entry is overwritten.
func Post(msg string) {
	msg = strings.TrimSpace(msg)
	if len(msg) == 0 {
		panic("message cannot be empty")
	}
	if len(msg) > 280 {
		panic("message too long (max 280 chars)")
	}
	buf[head] = Entry{
		Author:  unsafe.PreviousRealm().Address(),
		Message: msg,
		Height:  runtime.ChainHeight(),
	}
	head = (head + 1) % Cap
	if count < Cap {
		count++
	}
}

// Entries returns all stored messages in chronological order (oldest first).
func Entries() []Entry {
	if count == 0 {
		return nil
	}
	result := make([]Entry, count)
	oldest := (head - count + Cap) % Cap
	for i := 0; i < count; i++ {
		result[i] = buf[(oldest+i)%Cap]
	}
	return result
}

// Len returns the number of messages currently stored.
func Len() int { return count }

// Render displays the ring buffer as a Markdown page.
func Render(path string) string {
	var sb strings.Builder

	sb.WriteString("# RingLog\n\n")
	sb.WriteString("> A ring-buffer message board (capacity: **")
	sb.WriteString(strconv.Itoa(Cap))
	sb.WriteString("**). New messages overwrite the oldest once the buffer is full.\n\n")

	if count == 0 {
		sb.WriteString("*No messages yet. Call `Post(msg)` to add one.*\n")
		return sb.String()
	}

	sb.WriteString("| # | Author | Block | Message |\n")
	sb.WriteString("|---|--------|-------|---------|\n")

	entries := Entries()
	for i, e := range entries {
		sb.WriteString("| ")
		sb.WriteString(strconv.Itoa(i + 1))
		sb.WriteString(" | `")
		sb.WriteString(shorten(e.Author.String()))
		sb.WriteString("` | ")
		sb.WriteString(strconv.FormatInt(e.Height, 10))
		sb.WriteString(" | ")
		sb.WriteString(e.Message)
		sb.WriteString(" |\n")
	}

	sb.WriteString("\n*Showing ")
	sb.WriteString(strconv.Itoa(count))
	sb.WriteString(" of ")
	sb.WriteString(strconv.Itoa(Cap))
	sb.WriteString(" slots used.*\n")

	return sb.String()
}

// shorten trims a long address for display.
func shorten(s string) string {
	if len(s) <= 14 {
		return s
	}
	return s[:6] + "…" + s[len(s)-6:]
}

ringlog_test.gno

package ringlog

import (
	"strconv"
	"strings"
	"testing"
)

func resetState() {
	buf = [Cap]Entry{}
	head = 0
	count = 0
}

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

	Post("hello world")

	if count != 1 {
		t.Errorf("want count=1, got %d", count)
	}
	entries := Entries()
	if len(entries) != 1 {
		t.Fatalf("want 1 entry, got %d", len(entries))
	}
	if entries[0].Message != "hello world" {
		t.Errorf("want 'hello world', got %q", entries[0].Message)
	}
}

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

	if Len() != 0 {
		t.Errorf("want Len()=0, got %d", Len())
	}
	Post("a")
	Post("b")
	Post("c")
	if Len() != 3 {
		t.Errorf("want Len()=3, got %d", Len())
	}
}

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

	// Write Cap+3 messages — the first 3 should be evicted.
	for i := 0; i < Cap+3; i++ {
		Post("msg" + strconv.Itoa(i))
	}

	if count != Cap {
		t.Errorf("want count=%d after overflow, got %d", Cap, count)
	}

	entries := Entries()
	if len(entries) != Cap {
		t.Fatalf("want %d entries, got %d", Cap, len(entries))
	}

	// Oldest surviving message should be msg3 (msg0..msg2 were overwritten).
	if entries[0].Message != "msg3" {
		t.Errorf("oldest entry should be 'msg3', got %q", entries[0].Message)
	}
	// Newest message should be msg(Cap+2).
	want := "msg" + strconv.Itoa(Cap+2)
	if entries[Cap-1].Message != want {
		t.Errorf("newest entry should be %q, got %q", want, entries[Cap-1].Message)
	}
}

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

	msgs := []string{"first", "second", "third"}
	for _, m := range msgs {
		Post(m)
	}

	entries := Entries()
	for i, m := range msgs {
		if entries[i].Message != m {
			t.Errorf("entry[%d]: want %q, got %q", i, m, entries[i].Message)
		}
	}
}

func TestEmptyPanicsOnEmpty(t *testing.T) {
	resetState()
	defer func() {
		if r := recover(); r == nil {
			t.Error("expected panic on empty message, got none")
		}
	}()
	Post("   ")
}

func TestTooLongPanics(t *testing.T) {
	resetState()
	defer func() {
		if r := recover(); r == nil {
			t.Error("expected panic on too-long message, got none")
		}
	}()
	Post(strings.Repeat("x", 281))
}

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

	r := Render("")
	if !strings.Contains(r, "No messages yet") {
		t.Error("empty ring should show 'No messages yet'")
	}

	Post("first message")
	r = Render("")
	if !strings.Contains(r, "first message") {
		t.Error("render should include 'first message'")
	}
	if !strings.Contains(r, "RingLog") {
		t.Error("render should include title 'RingLog'")
	}
}