Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

lock.gno

1.30 Kb · 48 lines
 1// Package lock is version 1 of the "retire the predecessor" upgrade pattern
 2// (pattern B of the exploration; see ../README.md).
 3//
 4// v1 refuses every write until v0 has been retired. That is the whole point:
 5// at most one version is writable at any height, so v1 can safely fold v0's
 6// frozen total into its own and report a single authoritative number, the
 7// thing pattern A cannot do.
 8package lock
 9
10import (
11	v0 "gno.land/r/moul/x/upgrade/lock/v0"
12
13	"gno.land/p/nt/ufmt/v0"
14)
15
16// Predecessor is the version this one supersedes.
17const Predecessor = "gno.land/r/moul/x/upgrade/lock/v0"
18
19var counter int
20
21// Inc adds n once the predecessor is frozen.
22func Inc(cur realm, n int) {
23	assertPredecessorRetired()
24	counter += n
25}
26
27// Get returns the predecessor's frozen total plus this version's own.
28func Get() int {
29	return v0.Get() + counter
30}
31
32// Live reports whether the handover has happened.
33func Live() bool {
34	return v0.Successor() == "gno.land/r/moul/x/upgrade/lock/v1"
35}
36
37func assertPredecessorRetired() {
38	if !Live() {
39		panic("lock/v1 is not live yet, " + Predecessor + " must be retired onto it first")
40	}
41}
42
43func Render(_ string) string {
44	if !Live() {
45		return ufmt.Sprintf("lock/v1: waiting for %s to retire\n", Predecessor)
46	}
47	return ufmt.Sprintf("lock/v1: %d (own %d + retired v0 %d)\n", Get(), counter, v0.Get())
48}