policy.gno
1.98 Kb · 59 lines
1package releases
2
3import (
4 "strconv"
5
6 "gno.land/p/moul/pausable/v0"
7)
8
9// Policy is what a release's status means for the realm that is that release,
10// at block height `height`, as a p/moul/pausable State:
11//
12// Draft, Prerelease, Supported, Latest running
13// Deprecated running until its sunset, read-only from it
14// Yanked, Vulnerable paused
15//
16// It is a pure function of the stored status and the height, so a deprecated
17// version goes read-only at its sunset block with no transaction from anyone.
18// An unknown status fails closed, like pausable.MustParse.
19func Policy(r Release, height int64) pausable.State {
20 switch r.Status {
21 case Draft, Prerelease, Supported, Latest:
22 return pausable.State{}
23 case Deprecated:
24 if r.Sunset > 0 && height >= r.Sunset {
25 return pausable.State{Level: pausable.ReadOnly, Reason: reasonOr(r, "deprecated since block "+strconv.FormatInt(r.Sunset, 10))}
26 }
27 return pausable.State{Level: pausable.Running, Reason: reasonOr(r, "deprecated")}
28 case Yanked:
29 return pausable.State{Level: pausable.Paused, Reason: reasonOr(r, "yanked")}
30 default:
31 return pausable.State{Level: pausable.Paused, Reason: reasonOr(r, "vulnerable")}
32 }
33}
34
35func reasonOr(r Release, fallback string) string {
36 if r.Reason != "" {
37 return r.Reason
38 }
39 return fallback
40}
41
42// State is Policy for the release published at pkgPath. A path this project
43// does not list is running: being unlisted is not a reason to stop, and a
44// registry that paused whatever it had not heard of would let a typo take a
45// realm down.
46func (p *Project) State(pkgPath string, height int64) pausable.State {
47 r, ok := p.ByPath(pkgPath)
48 if !ok {
49 return pausable.State{}
50 }
51 return Policy(r, height)
52}
53
54// AssertWritable aborts unless the release at pkgPath may still be written to
55// at this height. A version puts it at the top of every state-changing
56// function.
57func (p *Project) AssertWritable(pkgPath string, height int64) {
58 p.State(pkgPath, height).AssertWritable()
59}