roles.gno
2.51 Kb · 75 lines
1package arena
2
3import "gno.land/r/g1t2kg2vtr3fukg43eujkn6x53gfdyakhngt4sfd/gnofly/game/v0"
4
5// The arena realm keeps no roles of its own. Who owns it, who administers
6// it, who referees its rounds and where its fee goes are the answers of the
7// game realm, asked again at every call: a referee removed there is refused
8// here in the same block.
9
10// authority is the set of questions this realm asks about roles.
11type authority interface {
12 Owner() address
13 IsAdmin(addr address) bool
14 IsReferee(addr address) bool
15 Treasury() address
16}
17
18// gameRoles answers with the roles of the game realm.
19type gameRoles struct{}
20
21func (gameRoles) Owner() address { return game.Owner() }
22func (gameRoles) IsAdmin(addr address) bool { return game.IsAdmin(addr) }
23func (gameRoles) IsReferee(addr address) bool { return game.IsReferee(addr) }
24func (gameRoles) Treasury() address { return game.Treasury() }
25
26// roles is the authority of this realm: the game realm, always.
27//
28// It is a variable for one reason. Under `gno test` an imported realm is
29// initialized without a deployer, and a test of this package has no way to
30// become the owner of the game realm, so the tests put their own answers
31// here. Nothing outside the test files assigns it, and there is no setter.
32var roles authority = gameRoles{}
33
34// callerOf returns the verified caller of a crossing function. rlm must be
35// the cur of the crossing function calling this helper: a stale or forged
36// realm value fails IsCurrent, and Previous is only meaningful on a live one.
37//
38// It takes the realm in second position on purpose, like p/nt/ownable does:
39// a function whose first parameter is a realm is a crossing function.
40func callerOf(_ int, rlm realm) address {
41 if !rlm.IsCurrent() {
42 panic(errSpoofedRealm)
43 }
44 return rlm.Previous().Address()
45}
46
47// assertOwner panics unless the caller is the owner of the game realm, and
48// returns the caller.
49func assertOwner(_ int, rlm realm) address {
50 caller := callerOf(0, rlm)
51 if caller != roles.Owner() {
52 panic(errNotOwner)
53 }
54 return caller
55}
56
57// assertAdmin panics unless the caller is an admin of the game realm, and
58// returns the caller.
59func assertAdmin(_ int, rlm realm) address {
60 caller := callerOf(0, rlm)
61 if !roles.IsAdmin(caller) {
62 panic(errNotAdmin)
63 }
64 return caller
65}
66
67// assertReferee panics unless the caller is a referee of the game realm, and
68// returns the caller.
69func assertReferee(_ int, rlm realm) address {
70 caller := callerOf(0, rlm)
71 if !roles.IsReferee(caller) {
72 panic(errNotReferee)
73 }
74 return caller
75}