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

Playground

gnomod.toml

module = "gno.land/r/moul/x/upgrade/selfreg/impl/v1"
gno = "0.9"

[addpkg]
  creator = "g1manfred47kzduec920z88wfr64ylksmdcedlf5"
  height = 410018
  max_deposit = "5000000ugnot"

impl.gno

// Package impl is version 1 of the implementation realm of the
// "self-registering implementation" upgrade pattern (pattern E; see
// ../../README.md).
//
// Identical wiring to v0, different behaviour. Deploying it takes the facade
// over on the spot, with no transaction from anybody.
package impl

import (
	facade "gno.land/r/moul/x/upgrade/selfreg/facade/v0"
)

type greeter struct{}

func (greeter) Greet(name string) string { return "HELLO, " + name + "!" }
func (greeter) Version() string          { return "impl/v1" }

// Instance exposes the singleton so a test (or a rollback) can re-register it
// without redeploying.
func Instance() facade.Impl { return greeter{} }

func init(cur realm) {
	facade.Register(cross(cur), Instance())
}

// Register re-registers this implementation with the facade. init does it once
// at deploy; exposing it as a transaction is what makes a rollback to an
// already-deployed version a single call.
func Register(cur realm) {
	facade.Register(cross(cur), Instance())
}

// Seal asks the facade to end its upgradeability, forever. It only succeeds
// while this implementation is the live one: the facade reads the caller off
// the crossing frame, so this cannot be used to seal somebody else's tenure.
func Seal(cur realm) {
	facade.Seal(cross(cur))
}

impl_test.gno

package impl

import (
	"testing"

	facade "gno.land/r/moul/x/upgrade/selfreg/facade/v0"
	implv0 "gno.land/r/moul/x/upgrade/selfreg/impl/v0"

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

// TestTakeoverOnDeploy checks that the last implementation to run its init owns
// the facade, and that re-registering an older one rolls the behaviour back.
//
// Both impl realms are imported here, so both inits have already run by the
// time this test starts; v1 is imported last and therefore holds the facade.
func TestTakeoverOnDeploy(cur realm, t *testing.T) {
	uassert.Equal(t, "gno.land/r/moul/x/upgrade/selfreg/impl/v1", facade.Live())
	uassert.Equal(t, "impl/v1", facade.Version())
	uassert.Equal(t, "HELLO, world!", facade.Greet("world"))

	// Rolling back is the same one call: the facade only ever holds a pointer.
	implv0.Register(cross(cur))
	uassert.Equal(t, "gno.land/r/moul/x/upgrade/selfreg/impl/v0", facade.Live())
	uassert.Equal(t, "hello, world", facade.Greet("world"))

	// And forward again.
	Register(cross(cur))
	uassert.Equal(t, "impl/v1", facade.Version())
	uassert.Equal(t, "selfreg/facade/v0 [open]: impl/v1 (gno.land/r/moul/x/upgrade/selfreg/impl/v1)\nHELLO, world!\n", facade.Render(""))
}

// TestSeal ends the realm's upgradeability and checks that it is terminal.
//
// Runs after TestTakeoverOnDeploy on purpose: sealing is one-way, so every test
// that needs to register must already have run.
func TestSeal(cur realm, t *testing.T) {
	// The live implementation is the only actor that may seal, because with no
	// owner it is the only one the facade already trusts.
	uassert.Equal(t, "gno.land/r/moul/x/upgrade/selfreg/impl/v1", facade.Live())
	uassert.Equal(t, facade.StageOpen, facade.Stage())
	uassert.AbortsContains(t, cur, "only the live implementation may seal", func() {
		implv0.Seal(cross(cur))
	})

	Seal(cross(cur))
	uassert.Equal(t, facade.StageSealed, facade.Stage())

	// Nothing registers again, not even the implementation that sealed it.
	uassert.AbortsContains(t, cur, "is sealed", func() {
		Register(cross(cur))
	})
	uassert.AbortsContains(t, cur, "is sealed", func() {
		implv0.Register(cross(cur))
	})

	// What was live stays live, and says so.
	uassert.Equal(t, "HELLO, world!", facade.Greet("world"))
	uassert.Equal(t, "selfreg/facade/v0 [sealed]: impl/v1 (gno.land/r/moul/x/upgrade/selfreg/impl/v1)\nHELLO, world!\n", facade.Render(""))
}