Playground
counter.gno
// Package counter implements "Counter Club": a single shared integer counter
// with a rolling history of the last 20 mutations. Anyone may Inc, Dec, or
// Reset the value; every change records the caller, the delta applied, the
// resulting value, and the block height it happened at.
package counter
import (
"chain"
"chain/runtime"
"chain/runtime/unsafe"
"strconv"
"strings"
)
// maxHistory is the number of recent changes kept in the log.
const maxHistory = 20
// change is a single recorded mutation of the counter.
type change struct {
caller address // who triggered the change
delta int // how much the value moved (+1, -1, or reset amount)
value int // the resulting counter value after the change
height int64 // block height at which it occurred
}
// value is the current shared counter.
var value int
// total counts every mutation ever applied (not capped, unlike history).
var total int
// history holds the last maxHistory changes, oldest first.
var history []change
// record appends a change to the history, trimming to the last maxHistory.
func record(delta, newValue int) {
c := change{
caller: unsafe.PreviousRealm().Address(),
delta: delta,
value: newValue,
height: runtime.ChainHeight(),
}
history = append(history, c)
if len(history) > maxHistory {
history = history[len(history)-maxHistory:]
}
total++
chain.Emit(
"CounterChange",
"delta", strconv.Itoa(delta),
"value", strconv.Itoa(newValue),
)
}
// Inc adds one to the shared counter and logs the change.
func Inc(cur realm) int {
value++
record(1, value)
return value
}
// Dec subtracts one from the shared counter and logs the change.
func Dec(cur realm) int {
value--
record(-1, value)
return value
}
// Reset sets the shared counter back to zero and logs the change. The recorded
// delta is the amount required to bring the previous value to zero.
func Reset(cur realm) int {
delta := -value
value = 0
record(delta, value)
return value
}
// Value returns the current counter value (read-only helper).
func Value() int {
return value
}
// Total returns the total number of mutations ever applied.
func Total() int {
return total
}
// Render produces a Markdown view: the big current value, the total number of
// changes, and a table of the most recent changes (newest first).
func Render(path string) string {
var b strings.Builder
b.WriteString("# Counter Club\n\n")
b.WriteString("A shared counter anyone can Inc, Dec, or Reset. ")
b.WriteString("The last ")
b.WriteString(strconv.Itoa(maxHistory))
b.WriteString(" changes are logged below.\n\n")
// Big current value.
b.WriteString("## Current value\n\n")
b.WriteString("# ")
b.WriteString(strconv.Itoa(value))
b.WriteString("\n\n")
b.WriteString("**Total changes:** ")
b.WriteString(strconv.Itoa(total))
b.WriteString("\n\n")
// Recent history, newest first.
b.WriteString("## Recent history\n\n")
if len(history) == 0 {
b.WriteString("_No changes yet — be the first to count!_\n")
return b.String()
}
b.WriteString("| # | Caller | Delta | New value | Height |\n")
b.WriteString("|---|--------|-------|-----------|--------|\n")
n := len(history)
for i := n - 1; i >= 0; i-- {
c := history[i]
b.WriteString("| ")
b.WriteString(strconv.Itoa(n - i))
b.WriteString(" | ")
b.WriteString(c.caller.String())
b.WriteString(" | ")
b.WriteString(signed(c.delta))
b.WriteString(" | ")
b.WriteString(strconv.Itoa(c.value))
b.WriteString(" | ")
b.WriteString(strconv.FormatInt(c.height, 10))
b.WriteString(" |\n")
}
return b.String()
}
// signed formats an int with an explicit leading sign for non-negative values.
func signed(n int) string {
if n >= 0 {
return "+" + strconv.Itoa(n)
}
return strconv.Itoa(n)
}
counter_test.gno
package counter
import (
"strings"
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
// TestIncDecReset drives the full mutation lifecycle across two callers and
// checks value, total count, and history accounting.
func TestIncDecReset(cur realm, t *testing.T) {
// reset package-level state for a clean run
value = 0
total = 0
history = nil
alice := testutils.TestAddress("alice")
bob := testutils.TestAddress("bob")
testing.SetRealm(testing.NewUserRealm(alice))
uassert.Equal(t, 1, Inc(cross(cur)))
uassert.Equal(t, 2, Inc(cross(cur)))
testing.SetRealm(testing.NewUserRealm(bob))
uassert.Equal(t, 1, Dec(cross(cur)))
uassert.Equal(t, 1, Value())
uassert.Equal(t, 3, Total())
uassert.Equal(t, 3, len(history))
// Reset should bring the value to zero with a delta of -1.
uassert.Equal(t, 0, Reset(cross(cur)))
uassert.Equal(t, 0, Value())
uassert.Equal(t, 4, Total())
last := history[len(history)-1]
uassert.Equal(t, -1, last.delta)
uassert.Equal(t, 0, last.value)
uassert.Equal(t, bob.String(), last.caller.String())
}
// TestHistoryCapAndRender checks the rolling 20-entry cap and that Render
// reflects the current value and total.
func TestHistoryCapAndRender(cur realm, t *testing.T) {
value = 0
total = 0
history = nil
caller := testutils.TestAddress("counter")
testing.SetRealm(testing.NewUserRealm(caller))
// Apply 25 increments; history must cap at maxHistory (20), total = 25.
for i := 0; i < 25; i++ {
Inc(cross(cur))
}
uassert.Equal(t, 25, Value())
uassert.Equal(t, 25, Total())
uassert.Equal(t, maxHistory, len(history))
// Oldest surviving entry should have value 6 (changes 1..5 dropped).
uassert.Equal(t, 6, history[0].value)
out := Render("")
uassert.True(t, strings.Contains(out, "# 25"), "render shows current value")
uassert.True(t, strings.Contains(out, "Total changes:"), "render shows total")
uassert.True(t, strings.Contains(out, "| +1 |"), "render shows a delta cell")
}
// TestRenderEmpty verifies the empty-state message before any mutations.
func TestRenderEmpty(t *testing.T) {
value = 0
total = 0
history = nil
out := Render("")
uassert.True(t, strings.Contains(out, "No changes yet"), "shows empty state")
uassert.True(t, strings.Contains(out, "# 0"), "shows zero value")
}
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/counter/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186407 max_deposit = "5000000ugnot"
render_example_test.gno
package counter
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Counter Club
//
// A shared counter anyone can Inc, Dec, or Reset. The last 20 changes are logged below.
//
// ## Current value
//
// # 0
//
// **Total changes:** 0
//
// ## Recent history
//
// _No changes yet — be the first to count!_
}