halt.gno
2.02 Kb · 87 lines
1package halt
2
3import (
4 "chain"
5 "strconv"
6
7 "gno.land/r/gnoswap/access/v1"
8)
9
10var haltStates HaltStateManager
11
12func init() {
13 haltStates = newHaltStateManagerByConfig(newNoneConfig())
14}
15
16// SetHaltLevel sets the global halt level.
17//
18// Parameters:
19// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
20// - level: halt level to apply (None, SafeMode, Emergency, or Complete).
21//
22// Only callable by admin or governance.
23func SetHaltLevel(cur realm, level HaltLevel) {
24 caller := cur.Previous().Address()
25 access.AssertIsAdminOrGovernance(caller)
26
27 err := setHaltLevel(level)
28 if err != nil {
29 panic(err)
30 }
31
32 chain.Emit(
33 "SetHaltLevel",
34 "level", level.String(),
35 "description", level.Description(),
36 "caller", caller.String(),
37 )
38}
39
40// SetOperationStatus sets the halt status for a specific operation.
41//
42// Parameters:
43// - cur: Current realm context; callers use cross(cur) when crossing into this realm.
44// - op: operation type whose halt status is changed.
45// - halted: true to block the operation, false to allow it.
46//
47// Only callable by admin or governance.
48func SetOperationStatus(cur realm, op OpType, halted bool) {
49 caller := cur.Previous().Address()
50 access.AssertIsAdminOrGovernance(caller)
51
52 if !op.IsValid() {
53 panic(makeErrorWithDetails(errInvalidOpType, op.String()))
54 }
55
56 err := haltStates.setOperationHalted(op, halted)
57 if err != nil {
58 panic(err)
59 }
60
61 chain.Emit(
62 "SetOperationStatus",
63 "operation", string(op),
64 "halted", strconv.FormatBool(halted),
65 "caller", caller.String(),
66 )
67}
68
69// setHaltLevel applies predefined halt level configuration.
70func setHaltLevel(level HaltLevel) error {
71 var config HaltConfig
72
73 switch level {
74 case HaltLevelNone:
75 config = newNoneConfig()
76 case HaltLevelSafeMode:
77 config = newSafeModeConfig()
78 case HaltLevelEmergency:
79 config = newEmergencyConfig()
80 case HaltLevelComplete:
81 config = newCompleteConfig()
82 default:
83 return makeErrorWithDetails(errInvalidHaltLevel, level.String())
84 }
85
86 return haltStates.updateOperationHaltsByConfig(config)
87}