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/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/upgrade/store/logic/v1"
gno = "0.9"
private = true

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 914348
  max_deposit = "5000000ugnot"

logic.gno

// Package logic is version 1 of the logic realm of the "state realm + swappable
// logic" upgrade pattern (pattern C of the exploration; see ../../README.md).
//
// Same shape as v0, different rule. The counter it reads and writes is the same
// one v0 was using: the upgrade changed the code without touching the data.
package logic

import (
	root "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/upgrade/store/root/v0"

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

// Step is this version's business rule: the change that justified the upgrade.
const Step = 1000

// Inc applies this version's rule to the shared counter.
func Inc(cur realm) int {
	return root.Inc(cross(cur), Step)
}

// Get reads the shared counter, including everything v0 wrote.
func Get() int {
	return root.Get()
}

func Render(_ string) string {
	return ufmt.Sprintf("store/logic/v1: counter=%d step=%d\n", Get(), Step)
}

logic_test.gno

package logic

import (
	"testing"

	logicv0 "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/upgrade/store/logic/v0"
	root "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/upgrade/store/root/v0"

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

// TestSwapLogic shows the upgrade: the counter survives it untouched, and only
// the realm root currently points at may write.
func TestSwapLogic(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewUserRealm(root.Ownable.Owner()))

	// v0 is live out of the box.
	uassert.Equal(t, 1, logicv0.Inc(cross(cur)))
	uassert.Equal(t, 2, logicv0.Inc(cross(cur)))
	uassert.AbortsContains(t, cur, "is not the live logic realm", func() {
		Inc(cross(cur))
	})

	// The upgrade: one call, no migration.
	root.SetLive(cross(cur), "gno.land/r/moul/x/upgrade/store/logic/v1")

	// v1 writes on top of the data v0 left; v0 is locked out by the same gate.
	uassert.Equal(t, 1002, Inc(cross(cur)), "v1 adds its own step to v0's data")
	uassert.AbortsContains(t, cur, "is not the live logic realm", func() {
		logicv0.Inc(cross(cur))
	})

	// Reads stay open to every version, live or not.
	uassert.Equal(t, 1002, logicv0.Get(), "a retired version still reads the truth")
	uassert.Equal(t, "store/logic/v1: counter=1002 step=1000\n", Render(""))
	uassert.Equal(t, "store/logic/v0: counter=1002 step=1\n", logicv0.Render(""))
}