Search Apps Documentation Source Content File Folder Download Copy Actions Download

halt.gno

1.82 Kb ยท 86 lines
 1package halt
 2
 3import (
 4	"chain"
 5	"chain/runtime"
 6	"strconv"
 7
 8	"gno.land/r/gnoswap/access"
 9)
10
11var haltStates HaltStateManager
12
13func init() {
14	haltStates = newHaltStateManagerByConfig(newNoneConfig())
15}
16
17// SetHaltLevel sets the global halt level.
18//
19// Parameters:
20//   - level: halt level to apply (None, SafeMode, Emergency, Complete)
21//
22// Only callable by admin or governance.
23func SetHaltLevel(cur realm, level HaltLevel) {
24	caller := runtime.PreviousRealm().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 halt status for a specific operation.
41//
42// Parameters:
43//   - op: operation type
44//   - halted: true to halt, false to resume
45//
46// Only callable by admin or governance.
47func SetOperationStatus(cur realm, op OpType, halted bool) {
48	caller := runtime.PreviousRealm().Address()
49	access.AssertIsAdminOrGovernance(caller)
50
51	if !op.IsValid() {
52		panic(makeErrorWithDetails(errInvalidOpType, op.String()))
53	}
54
55	err := haltStates.setOperationHalted(op, halted)
56	if err != nil {
57		panic(err)
58	}
59
60	chain.Emit(
61		"SetOperationStatus",
62		"operation", string(op),
63		"halted", strconv.FormatBool(halted),
64		"caller", caller.String(),
65	)
66}
67
68// setHaltLevel applies predefined halt level configuration.
69func setHaltLevel(level HaltLevel) error {
70	var config HaltConfig
71
72	switch level {
73	case HaltLevelNone:
74		config = newNoneConfig()
75	case HaltLevelSafeMode:
76		config = newSafeModeConfig()
77	case HaltLevelEmergency:
78		config = newEmergencyConfig()
79	case HaltLevelComplete:
80		config = newCompleteConfig()
81	default:
82		return makeErrorWithDetails(errInvalidHaltLevel, level.String())
83	}
84
85	return haltStates.updateOperationHaltsByConfig(config)
86}