forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
global_params.go
243 lines (222 loc) · 10.1 KB
/
global_params.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package global_params
import (
"bytes"
"fmt"
"github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/common/serialization"
"github.com/dnaproject2/DNA/errors"
"github.com/dnaproject2/DNA/smartcontract/service/native"
"github.com/dnaproject2/DNA/smartcontract/service/native/utils"
)
type paramType byte
const (
VERSION_CONTRACT_GLOBAL_PARAMS = byte(0)
CURRENT_VALUE paramType = 0x00
PREPARE_VALUE paramType = 0x01
INIT_NAME = "init"
ACCEPT_ADMIN_NAME = "acceptAdmin"
TRANSFER_ADMIN_NAME = "transferAdmin"
SET_OPERATOR = "setOperator"
SET_GLOBAL_PARAM_NAME = "setGlobalParam"
GET_GLOBAL_PARAM_NAME = "getGlobalParam"
CREATE_SNAPSHOT_NAME = "createSnapshot"
)
func InitGlobalParams() {
native.Contracts[utils.ParamContractAddress] = RegisterParamContract
}
func RegisterParamContract(native *native.NativeService) {
native.Register(INIT_NAME, ParamInit)
native.Register(ACCEPT_ADMIN_NAME, AcceptAdmin)
native.Register(TRANSFER_ADMIN_NAME, TransferAdmin)
native.Register(SET_OPERATOR, SetOperator)
native.Register(SET_GLOBAL_PARAM_NAME, SetGlobalParam)
native.Register(GET_GLOBAL_PARAM_NAME, GetGlobalParam)
native.Register(CREATE_SNAPSHOT_NAME, CreateSnapshot)
}
func ParamInit(native *native.NativeService) ([]byte, error) {
contract := native.ContextRef.CurrentContext().ContractAddress
storageAdmin, _ := GetStorageRole(native, generateAdminKey(contract, false))
storageOperator, _ := GetStorageRole(native, generateAdminKey(contract, false))
if storageAdmin != common.ADDRESS_EMPTY || storageOperator != common.ADDRESS_EMPTY {
return utils.BYTE_FALSE, errors.NewErr("init param, admin or operator has already existed!")
}
initParams := Params{}
args, err := serialization.ReadVarBytes(bytes.NewBuffer(native.Input))
if err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode, "init param, read native input failed!")
}
argsBuffer := bytes.NewBuffer(args)
if err := initParams.Deserialize(argsBuffer); err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode, "init param, deserialize params failed!")
}
native.CacheDB.Put(generateParamKey(contract, CURRENT_VALUE), getParamStorageItem(initParams).ToArray())
native.CacheDB.Put(generateParamKey(contract, PREPARE_VALUE), getParamStorageItem(initParams).ToArray())
var admin common.Address
if admin, err = utils.ReadAddress(argsBuffer); err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode, "init param, deserialize admin failed!")
}
native.CacheDB.Put(generateAdminKey(contract, false), getRoleStorageItem(admin).ToArray())
operator := admin
native.CacheDB.Put(GenerateOperatorKey(contract), getRoleStorageItem(operator).ToArray())
return utils.BYTE_TRUE, nil
}
func AcceptAdmin(native *native.NativeService) ([]byte, error) {
var destinationAdmin common.Address
destinationAdmin, err := utils.ReadAddress(bytes.NewBuffer(native.Input))
if err != nil {
return utils.BYTE_FALSE, errors.NewErr("accept admin, deserialize admin failed!")
}
if !native.ContextRef.CheckWitness(destinationAdmin) {
return utils.BYTE_FALSE, errors.NewErr("accept admin, authentication failed!")
}
contract := native.ContextRef.CurrentContext().ContractAddress
transferAdmin, err := GetStorageRole(native, generateAdminKey(contract, true))
if err != nil || transferAdmin == common.ADDRESS_EMPTY || transferAdmin != destinationAdmin {
return utils.BYTE_FALSE, fmt.Errorf("accept admin, destination account hasn't been approved, caused by %v", err)
}
// delete transfer admin item
native.CacheDB.Delete(generateAdminKey(contract, true))
// modify admin in database
native.CacheDB.Put(generateAdminKey(contract, false), getRoleStorageItem(destinationAdmin).ToArray())
NotifyRoleChange(native, contract, ACCEPT_ADMIN_NAME, destinationAdmin)
return utils.BYTE_TRUE, nil
}
func TransferAdmin(native *native.NativeService) ([]byte, error) {
contract := native.ContextRef.CurrentContext().ContractAddress
admin, err := GetStorageRole(native, generateAdminKey(contract, false))
if err != nil || admin == common.ADDRESS_EMPTY {
return utils.BYTE_FALSE, fmt.Errorf("transfer admin, admin doesn't exist, caused by %v", err)
}
if !native.ContextRef.CheckWitness(admin) {
return utils.BYTE_FALSE, errors.NewErr("transfer admin, authentication failed!")
}
destinationAdmin, err := utils.ReadAddress(bytes.NewBuffer(native.Input))
if err != nil {
return utils.BYTE_FALSE, errors.NewErr("transfer admin, deserialize admin failed!")
}
native.CacheDB.Put(generateAdminKey(contract, true),
getRoleStorageItem(destinationAdmin).ToArray())
NotifyTransferAdmin(native, contract, TRANSFER_ADMIN_NAME, admin, destinationAdmin)
return utils.BYTE_TRUE, nil
}
func SetOperator(native *native.NativeService) ([]byte, error) {
contract := native.ContextRef.CurrentContext().ContractAddress
admin, err := GetStorageRole(native, generateAdminKey(contract, false))
if err != nil || admin == common.ADDRESS_EMPTY {
return utils.BYTE_FALSE, fmt.Errorf("set operator, admin doesn't exist, caused by %v", err)
}
if !native.ContextRef.CheckWitness(admin) {
return utils.BYTE_FALSE, errors.NewErr("set operator, authentication failed!")
}
destinationOperator, err := utils.ReadAddress(bytes.NewBuffer(native.Input))
if err != nil {
return utils.BYTE_FALSE, errors.NewErr("set operator, deserialize operator failed!")
}
native.CacheDB.Put(GenerateOperatorKey(contract), getRoleStorageItem(destinationOperator).ToArray())
NotifyRoleChange(native, contract, SET_OPERATOR, destinationOperator)
return utils.BYTE_TRUE, nil
}
func SetGlobalParam(native *native.NativeService) ([]byte, error) {
contract := native.ContextRef.CurrentContext().ContractAddress
operator, err := GetStorageRole(native, GenerateOperatorKey(contract))
if err != nil || operator == common.ADDRESS_EMPTY {
return utils.BYTE_FALSE, fmt.Errorf("set param, operator doesn't exist, caused by %v", err)
}
if !native.ContextRef.CheckWitness(operator) {
return utils.BYTE_FALSE, errors.NewErr("set param, authentication failed!")
}
params := Params{}
if err := params.Deserialize(bytes.NewBuffer(native.Input)); err != nil {
return utils.BYTE_FALSE, errors.NewErr("set param, deserialize failed!")
}
if len(params) == 0 {
return utils.BYTE_FALSE, errors.NewErr("set param, params is nil!")
}
// read old param from database
storageParams, err := getStorageParam(native, generateParamKey(contract, PREPARE_VALUE))
if err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode,
"set param, read storage prepare param error!")
}
// update param
for _, param := range params {
storageParams.SetParam(param)
}
native.CacheDB.Put(generateParamKey(contract, PREPARE_VALUE),
getParamStorageItem(storageParams).ToArray())
NotifyParamChange(native, contract, SET_GLOBAL_PARAM_NAME, params)
return utils.BYTE_TRUE, nil
}
func GetGlobalParam(native *native.NativeService) ([]byte, error) {
var paramNameList ParamNameList
if err := paramNameList.Deserialize(bytes.NewBuffer(native.Input)); err != nil {
return utils.BYTE_FALSE, errors.NewErr("get param, deserialize failed!")
}
if len(paramNameList) == 0 {
return utils.BYTE_FALSE, errors.NewErr("get param, required params is nil!")
}
// read from db
contract := native.ContextRef.CurrentContext().ContractAddress
storageParams, err := getStorageParam(native, generateParamKey(contract, CURRENT_VALUE))
if err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode,
"get param, read storage current param error!")
}
if len(storageParams) == 0 {
return utils.BYTE_FALSE, errors.NewErr("get param, there are no params!")
}
params := new(Params)
for _, paramName := range paramNameList { // read param not in cache
if index, value := storageParams.GetParam(paramName); index >= 0 {
params.SetParam(value)
} else {
params.SetParam(Param{Key: paramName, Value: ""})
}
}
result := new(bytes.Buffer)
err = params.Serialize(result)
if err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode, "get param, serialize result error!")
}
return result.Bytes(), nil
}
func CreateSnapshot(native *native.NativeService) ([]byte, error) {
contract := native.ContextRef.CurrentContext().ContractAddress
operator, err := GetStorageRole(native, GenerateOperatorKey(contract))
if err != nil || operator == common.ADDRESS_EMPTY {
return utils.BYTE_FALSE, fmt.Errorf("create snapshot, operator doesn't exist, caused by %v", err)
}
if !native.ContextRef.CheckWitness(operator) {
return utils.BYTE_FALSE, errors.NewErr("create snapshot, authentication failed!")
}
// read prepare param
prepareParam, err := getStorageParam(native, generateParamKey(contract, PREPARE_VALUE))
if err != nil {
return utils.BYTE_FALSE, errors.NewDetailErr(err, errors.ErrNoCode,
"create snapshot, read storage prepare param error!")
}
if len(prepareParam) == 0 {
return utils.BYTE_FALSE, errors.NewErr("create snapshot, prepare param doesn't exist!")
}
// set prepare value to current value, make it effective
native.CacheDB.Put(generateParamKey(contract, CURRENT_VALUE), getParamStorageItem(prepareParam).ToArray())
NotifyParamChange(native, contract, CREATE_SNAPSHOT_NAME, prepareParam)
return utils.BYTE_TRUE, nil
}