-
Notifications
You must be signed in to change notification settings - Fork 199
/
esdtPause.go
127 lines (105 loc) · 3.22 KB
/
esdtPause.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package builtInFunctions
import (
"bytes"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/core/vmcommon"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/vm"
)
var _ process.BuiltinFunction = (*esdtPause)(nil)
type esdtPause struct {
keyPrefix []byte
pause bool
accounts state.AccountsAdapter
}
// NewESDTPauseFunc returns the esdt pause/un-pause built-in function component
func NewESDTPauseFunc(
accounts state.AccountsAdapter,
pause bool,
) (*esdtPause, error) {
if check.IfNil(accounts) {
return nil, process.ErrNilAccountsAdapter
}
e := &esdtPause{
keyPrefix: []byte(core.ElrondProtectedKeyPrefix + core.ESDTKeyIdentifier),
pause: pause,
accounts: accounts,
}
return e, nil
}
// SetNewGasConfig is called whenever gas cost is changed
func (e *esdtPause) SetNewGasConfig(_ *process.GasCost) {
}
// ProcessBuiltinFunction resolves ESDT pause function call
func (e *esdtPause) ProcessBuiltinFunction(
_, _ state.UserAccountHandler,
vmInput *vmcommon.ContractCallInput,
) (*vmcommon.VMOutput, error) {
if vmInput == nil {
return nil, process.ErrNilVmInput
}
if vmInput.CallValue.Cmp(zero) != 0 {
return nil, process.ErrBuiltInFunctionCalledWithValue
}
if len(vmInput.Arguments) != 1 {
return nil, process.ErrInvalidArguments
}
if !bytes.Equal(vmInput.CallerAddr, vm.ESDTSCAddress) {
return nil, process.ErrAddressIsNotESDTSystemSC
}
if !core.IsSystemAccountAddress(vmInput.RecipientAddr) {
return nil, process.ErrOnlySystemAccountAccepted
}
esdtTokenKey := append(e.keyPrefix, vmInput.Arguments[0]...)
log.Trace(vmInput.Function, "sender", vmInput.CallerAddr, "receiver", vmInput.RecipientAddr, "token", esdtTokenKey)
err := e.togglePause(esdtTokenKey)
if err != nil {
return nil, err
}
vmOutput := &vmcommon.VMOutput{ReturnCode: vmcommon.Ok}
return vmOutput, nil
}
func (e *esdtPause) togglePause(token []byte) error {
systemSCAccount, err := e.getSystemAccount()
if err != nil {
return err
}
val, _ := systemSCAccount.DataTrieTracker().RetrieveValue(token)
esdtMetaData := ESDTGlobalMetadataFromBytes(val)
esdtMetaData.Paused = e.pause
err = systemSCAccount.DataTrieTracker().SaveKeyValue(token, esdtMetaData.ToBytes())
if err != nil {
return err
}
return e.accounts.SaveAccount(systemSCAccount)
}
func (e *esdtPause) getSystemAccount() (state.UserAccountHandler, error) {
systemSCAccount, err := e.accounts.LoadAccount(core.SystemAccountAddress)
if err != nil {
return nil, err
}
userAcc, ok := systemSCAccount.(state.UserAccountHandler)
if !ok {
return nil, process.ErrWrongTypeAssertion
}
return userAcc, nil
}
// IsPaused returns true if the token is paused
func (e *esdtPause) IsPaused(pauseKey []byte) bool {
systemSCAccount, err := e.getSystemAccount()
if err != nil {
return false
}
val, _ := systemSCAccount.DataTrieTracker().RetrieveValue(pauseKey)
if len(val) != lengthOfESDTMetadata {
return false
}
esdtMetaData := ESDTGlobalMetadataFromBytes(val)
return esdtMetaData.Paused
}
// IsInterfaceNil returns true if underlying object in nil
func (e *esdtPause) IsInterfaceNil() bool {
return e == nil
}