Playground
gnomod.toml
module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/upgrade/lock/v1" gno = "0.9" [addpkg] creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv" height = 914348 max_deposit = "5000000ugnot"
lock.gno
// Package lock is version 1 of the "retire the predecessor" upgrade pattern
// (pattern B of the exploration; see ../README.md).
//
// v1 refuses every write until v0 has been retired. That is the whole point:
// at most one version is writable at any height, so v1 can safely fold v0's
// frozen total into its own and report a single authoritative number, the
// thing pattern A cannot do.
package lock
import (
v0 "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/upgrade/lock/v0"
"gno.land/p/nt/ufmt/v0"
)
// Predecessor is the version this one supersedes.
const Predecessor = "gno.land/r/moul/x/upgrade/lock/v0"
var counter int
// Inc adds n once the predecessor is frozen.
func Inc(cur realm, n int) {
assertPredecessorRetired()
counter += n
}
// Get returns the predecessor's frozen total plus this version's own.
func Get() int {
return v0.Get() + counter
}
// Live reports whether the handover has happened.
func Live() bool {
return v0.Successor() == "gno.land/r/moul/x/upgrade/lock/v1"
}
func assertPredecessorRetired() {
if !Live() {
panic("lock/v1 is not live yet, " + Predecessor + " must be retired onto it first")
}
}
func Render(_ string) string {
if !Live() {
return ufmt.Sprintf("lock/v1: waiting for %s to retire\n", Predecessor)
}
return ufmt.Sprintf("lock/v1: %d (own %d + retired v0 %d)\n", Get(), counter, v0.Get())
}
lock_test.gno
package lock
import (
"testing"
v0 "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/upgrade/lock/v0"
"gno.land/p/nt/uassert/v0"
)
// TestHandover walks the whole one-writer-at-a-time sequence in order: only v0
// writes, then the retirement flips it, then only v1 writes.
func TestHandover(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(v0.Ownable.Owner()))
// Before the handover: v0 writes, v1 refuses.
v0.Inc(cross(cur), 10)
uassert.False(t, Live(), "v1 must not be live before the handover")
uassert.AbortsContains(t, cur, "not live yet", func() {
Inc(cross(cur), 1)
})
uassert.Equal(t, 10, Get(), "v1 already reports v0's running total")
// The handover itself.
v0.Retire(cross(cur), "gno.land/r/moul/x/upgrade/lock/v1")
uassert.True(t, Live(), "v1 must be live once v0 names it")
// After: v0 refuses, v1 writes, the total is single-valued.
uassert.AbortsContains(t, cur, "is retired", func() {
v0.Inc(cross(cur), 1)
})
Inc(cross(cur), 100)
uassert.Equal(t, 110, Get(), "v1 folds v0's frozen total into its own")
uassert.Equal(t, 10, v0.Get(), "v0's total is frozen where it stopped")
// Retirement is one-way.
uassert.AbortsContains(t, cur, "already retired", func() {
v0.Retire(cross(cur), "gno.land/r/moul/x/upgrade/lock/v2")
})
uassert.Equal(t, "lock/v1: 110 (own 100 + retired v0 10)\n", Render(""))
uassert.Equal(t, "lock/v0: 10 (retired, use gno.land/r/moul/x/upgrade/lock/v1)\n", v0.Render(""))
}