-
Notifications
You must be signed in to change notification settings - Fork 113
/
system.go
259 lines (225 loc) · 6.67 KB
/
system.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package native
import (
"errors"
"encoding/json"
"github.com/bitly/go-simplejson"
"github.com/iost-official/go-iost/core/contract"
"github.com/iost-official/go-iost/vm/host"
)
var systemABIs *abiSet
func init() {
systemABIs = newAbiSet()
systemABIs.Register(requireAuth)
systemABIs.Register(receipt)
systemABIs.Register(setCode)
systemABIs.Register(updateCode)
systemABIs.Register(initSetCode)
systemABIs.Register(cancelDelaytx)
systemABIs.Register(hostSettings)
systemABIs.Register(updateNativeCode)
}
// var .
var (
requireAuth = &abi{
name: "requireAuth",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
var b bool
b, cost = h.RequireAuth(args[0].(string), args[1].(string))
rtn = []interface{}{
b,
}
return rtn, cost, nil
},
}
receipt = &abi{
name: "receipt",
args: []string{"string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = h.Receipt(args[0].(string))
return []interface{}{}, cost, nil
},
}
// setcode can only be invoked in native vm, avoid updating contract during running
setCode = &abi{
name: "setCode",
args: []string{"string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
con := &contract.Contract{}
codeRaw := args[0].(string)
if codeRaw[0] == '{' {
err = json.Unmarshal([]byte(codeRaw), con)
if err != nil {
return nil, host.CommonErrorCost(1), err
}
} else {
err = con.B64Decode(codeRaw)
if err != nil {
return nil, host.CommonErrorCost(1), err
}
}
info, cost1 := h.TxInfo()
cost.AddAssign(cost1)
var json *simplejson.Json
json, err = simplejson.NewJson(info)
if err != nil {
return nil, cost, err
}
var id string
id, err = json.Get("hash").String()
if err != nil {
return nil, cost, err
}
actID := "Contract" + id
con.ID = actID
publisher := h.Context().Value("publisher").(string)
cost.AddAssign(host.SetCodeCost(len(con.Code)))
if !CheckCost(h, cost) {
return nil, cost, host.ErrOutOfGas
}
cost2, err := h.SetCode(con, publisher)
cost.AddAssign(cost2)
if err != nil {
return nil, cost, err
}
cost2, err = h.MapPut("contract_owner", actID, publisher, publisher)
cost.AddAssign(cost2)
return []interface{}{actID}, cost, err
},
}
// updateCode can only be invoked in native vm, avoid updating contract during running
updateCode = &abi{
name: "updateCode",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
con := &contract.Contract{}
codeRaw := args[0].(string)
cost.AddAssign(host.CommonOpCost(1))
stackHeight := h.Context().Value("stack_height").(int)
if stackHeight != 1 {
return nil, cost, errors.New("can't call UpdateCode from other contract")
}
if codeRaw[0] == '{' {
err = json.Unmarshal([]byte(codeRaw), con)
if err != nil {
return nil, host.CommonErrorCost(1), err
}
} else {
err = con.B64Decode(codeRaw)
if err != nil {
return nil, host.CommonErrorCost(1), err
}
}
cost.AddAssign(host.SetCodeCost(len(con.Code)))
if !CheckCost(h, cost) {
return nil, cost, host.ErrOutOfGas
}
cost1, err := h.UpdateCode(con, []byte(args[1].(string)))
cost.AddAssign(cost1)
return []interface{}{}, cost, err
},
}
// initSetCode can only be invoked in genesis block, use specific id for deploying contract
initSetCode = &abi{
name: "initSetCode",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
if h.Context().Value("number").(int64) != 0 {
return []interface{}{}, cost, errors.New("initSetCode in normal block")
}
con := &contract.Contract{}
err = con.B64Decode(args[1].(string))
if err != nil {
return nil, host.CommonErrorCost(1), err
}
actID := args[0].(string)
con.ID = actID
cost2, err := h.SetCode(con, AdminAccount)
cost.AddAssign(cost2)
if err != nil {
return nil, cost, err
}
cost2, err = h.MapPut("contract_owner", actID, AdminAccount)
cost.AddAssign(cost2)
return []interface{}{actID}, cost, err
},
}
// updateNativeCode can only be invoked in native vm, avoid updating contract during running
updateNativeCode = &abi{
name: "updateNativeCode",
args: []string{"string", "string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
con := &contract.Contract{}
conID := args[0].(string)
version := args[1].(string)
codeRaw := args[2].(string)
// check auth
ok, cost0 := h.RequireAuth(AdminAccount, SystemPermission)
cost.AddAssign(cost0)
if !ok {
return nil, cost, errors.New("update native code need admin@system permission")
}
cost.AddAssign(host.CommonOpCost(1))
if version != "" {
con = SystemContractABI(conID, version)
if con == nil {
return nil, cost, errors.New("invalid contractID or version")
}
} else {
if codeRaw[0] == '{' {
err = json.Unmarshal([]byte(codeRaw), con)
if err != nil {
return nil, host.CommonErrorCost(1), err
}
} else {
err = con.B64Decode(codeRaw)
if err != nil {
return nil, host.CommonErrorCost(1), err
}
}
}
cost0, err = h.UpdateCode(con, []byte(""))
cost.AddAssign(cost0)
return []interface{}{}, cost, err
},
}
// cancelDelaytx cancels a delay transaction.
cancelDelaytx = &abi{
name: "cancelDelaytx",
args: []string{"string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost, err = h.CancelDelaytx(args[0].(string))
if err != nil {
return []interface{}{}, cost, err
}
// generate receipt
message, err := json.Marshal(args)
cost.AddAssign(host.CommonOpCost(1))
if err != nil {
return nil, cost, err
}
cost.AddAssign(h.Receipt(string(message)))
return []interface{}{}, cost, err
},
}
// hostSettings set host json
hostSettings = &abi{
name: "hostSettings",
args: []string{"string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
// check auth
ok, cost0 := h.RequireAuth(AdminAccount, SystemPermission)
cost.AddAssign(cost0)
if !ok {
return nil, cost, errors.New("set host settings need admin@system permission")
}
cost0, _ = h.MapPut("settings", "host", args[0])
cost.AddAssign(cost0)
return nil, cost, nil
},
}
)