-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_cosvm_export.go
405 lines (307 loc) · 14.1 KB
/
vm_cosvm_export.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package vm
import (
"crypto/sha256"
"encoding/binary"
"fmt"
"github.com/coschain/contentos-go/common/constants"
"github.com/go-interpreter/wagon/exec"
)
func e_sha256(proc *exec.Process, pSrc int32, lenSrc int32, pDst int32, lenDst int32) int32 {
w := proc.GetTag().(*CosVMNative)
srcBuf := w.cosVM.read(proc, pSrc, lenSrc, "sha256().read")
out := sha256.Sum256(srcBuf)
return w.cosVM.write(proc, out[:], pDst, lenDst, "sha256().write")
}
func e_currentBlockNumber(proc *exec.Process) int64 {
w := proc.GetTag().(*CosVMNative)
return int64(w.CurrentBlockNumber())
}
func e_currentTimestamp(proc *exec.Process) int64 {
w := proc.GetTag().(*CosVMNative)
return int64(w.CurrentTimestamp())
}
func e_currentBlockProducer(proc *exec.Process, pDst int32, dstSize int32) (length int32) {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.CurrentBlockProducer()), pDst, dstSize, "currentBlockProducer()")
}
func e_getBlockProducers(proc *exec.Process, pDst int32, dstSize int32) (length int32) {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.GetBlockProducers()), pDst, dstSize, "getBlockProducers()")
}
func e_printString(proc *exec.Process, pStr int32, lenStr int32) {
w := proc.GetTag().(*CosVMNative)
w.PrintString(string(w.cosVM.read(proc, pStr, lenStr, "printString()")))
}
func e_printInt64(proc *exec.Process, value int64) {
w := proc.GetTag().(*CosVMNative)
w.PrintInt64(value)
}
func e_printUint64(proc *exec.Process, value int64) {
w := proc.GetTag().(*CosVMNative)
w.PrintUint64(uint64(value))
}
func e_requiredAuth(proc *exec.Process, pStr int32, pLen int32) {
w := proc.GetTag().(*CosVMNative)
w.RequiredAuth(string(w.cosVM.read(proc, pStr, pLen, "requiredAuth()")))
}
func e_getUserBalance(proc *exec.Process, ptr int32, len int32) int64 {
w := proc.GetTag().(*CosVMNative)
return int64(w.GetUserBalance(string(w.cosVM.read(proc, ptr, len, "getUserBalance()"))))
}
func e_userExist(proc *exec.Process, ptr int32, len int32) int32 {
w := proc.GetTag().(*CosVMNative)
if w.UserExist(string(w.cosVM.read(proc, ptr, len, "userExist()"))) {
return 1
} else {
return 0
}
}
func e_getContractBalance(proc *exec.Process, cPtr int32, cLen int32, nPtr int32, nLen int32) int64 {
w := proc.GetTag().(*CosVMNative)
return int64(w.GetContractBalance(
string(w.cosVM.read(proc, cPtr, cLen, "getContractBalance().contract")),
string(w.cosVM.read(proc, nPtr, nLen, "getContractBalance().owner")),
))
}
func e_cosAssert(proc *exec.Process, condition int32, pStr int32, len int32) {
w := proc.GetTag().(*CosVMNative)
w.CosAssert(condition != 0, string(w.cosVM.read(proc, pStr, len, "cosAssert().msg")))
}
func e_cosAbort(proc *exec.Process) {
w := proc.GetTag().(*CosVMNative)
w.CosAbort()
}
func e_readContractOpParams(proc *exec.Process, ptr, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.ReadContractOpParams()), ptr, length, "readContractOpParams()")
}
func e_readContractName(proc *exec.Process, pStr int32, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.ReadContractName()), pStr, length, "readContractName()")
}
func e_readContractMethod(proc *exec.Process, pStr int32, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.ReadContractMethod()), pStr, length, "readContractMethod()")
}
func e_readContractOwner(proc *exec.Process, pStr int32, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.ReadContractOwner()), pStr, length, "readContractOwner()")
}
func e_readContractCaller(proc *exec.Process, pStr int32, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.ReadContractCaller()), pStr, length, "readContractCaller()")
}
func e_contractCalledByUser(proc *exec.Process) int32 {
w := proc.GetTag().(*CosVMNative)
r := int32(0)
if w.ContractCalledByUser() {
r = 1
}
return r
}
func e_readCallingContractOwner(proc *exec.Process, pStr int32, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.ReadCallingContractOwner()), pStr, length, "readCallingContractOwner()")
}
func e_readCallingContractName(proc *exec.Process, pStr int32, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.ReadCallingContractName()), pStr, length, "readCallingContractName()")
}
func e_contractTransferToUser(proc *exec.Process, pTo, pToLen int32, amount int64, pMemo, pMemoLen int32) {
w := proc.GetTag().(*CosVMNative)
w.ContractTransferToUser(string(w.cosVM.read(proc, pTo, pToLen, "contractTransferToUser().to")), uint64(amount))
}
func e_contractTransferToUserVest(proc *exec.Process, pTo, pToLen int32, amount int64, pMemo, pMemoLen int32) {
w := proc.GetTag().(*CosVMNative)
w.ContractTransferToUserVest(string(w.cosVM.read(proc, pTo, pToLen, "contractTransferToUserVest().to")), uint64(amount))
}
func e_contractTransferToContract(proc *exec.Process, pToOwner, pToOwnerLen, pToContract, pToContractLen int32, amount int64, pMemo, pMemoLen int32) {
w := proc.GetTag().(*CosVMNative)
w.ContractTransferToContract(
string(w.cosVM.read(proc, pToOwner, pToOwnerLen, "contractTransferToContract().toOwner")),
string(w.cosVM.read(proc, pToContract, pToContractLen, "contractTransferToContract().toContract")),
uint64(amount))
}
func e_readContractSenderValue(proc *exec.Process) int64 {
w := proc.GetTag().(*CosVMNative)
return int64(w.ReadContractSenderValue())
}
func e_contractCall(proc *exec.Process, owner, ownerSize, contract, contractSize, method, methodSize, param, paramSize int32, coins int64) {
w := proc.GetTag().(*CosVMNative)
w.ContractCall(
string(w.cosVM.read(proc, owner, ownerSize, "contractCall().owner")),
string(w.cosVM.read(proc, contract, contractSize, "contractCall().contract")),
string(w.cosVM.read(proc, method, methodSize, "contractCall().method")),
w.cosVM.read(proc, param, paramSize, "contractCall().param"),
uint64(coins),
)
}
func e_tableGetRecord(proc *exec.Process, tableName, tableNameLen int32, primary, primaryLen int32, value, valueLen int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc,
w.TableGetRecord(
string(w.cosVM.read(proc, tableName, tableNameLen, "tableGetRecord().table_name")),
w.cosVM.read(proc, primary, primaryLen, "tableGetRecord().primary"),
),
value, valueLen, "tableGetRecord()")
}
func e_tableNewRecord(proc *exec.Process, tableName, tableNameLen int32, value, valueLen int32) {
w := proc.GetTag().(*CosVMNative)
w.TableNewRecord(
string(w.cosVM.read(proc, tableName, tableNameLen, "tableNewRecord().table_name")),
w.cosVM.read(proc, value, valueLen, "tableNewRecord().value"),
)
}
func e_tableUpdateRecord(proc *exec.Process, tableName, tableNameLen int32, primary, primaryLen int32, value, valueLen int32) {
w := proc.GetTag().(*CosVMNative)
w.TableUpdateRecord(
string(w.cosVM.read(proc, tableName, tableNameLen, "tableUpdateRecord().table_name")),
w.cosVM.read(proc, primary, primaryLen, "tableUpdateRecord().primary"),
w.cosVM.read(proc, value, valueLen, "tableUpdateRecord().value"),
)
}
func e_tableDeleteRecord(proc *exec.Process, tableName, tableNameLen int32, primary, primaryLen int32) {
w := proc.GetTag().(*CosVMNative)
w.TableDeleteRecord(
string(w.cosVM.read(proc, tableName, tableNameLen, "tableDeleteRecord().table_name")),
w.cosVM.read(proc, primary, primaryLen, "tableDeleteRecord().primary"),
)
}
func e_tableGetRecordEx(proc *exec.Process, ownerName, ownerNameLen int32, contractName, contractNameLen int32, tableName, tableNameLen int32, primary, primaryLen int32, value, valueLen int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc,
w.TableGetRecordEx(
string(w.cosVM.read(proc, ownerName, ownerNameLen, "tableGetRecordEx().owner_name")),
string(w.cosVM.read(proc, contractName, contractNameLen, "tableGetRecordEx().contract_name")),
string(w.cosVM.read(proc, tableName, tableNameLen, "tableGetRecordEx().table_name")),
w.cosVM.read(proc, primary, primaryLen, "tableGetRecordEx().primary"),
),
value, valueLen, "tableGetRecordEx()")
}
func e_setCopyrightAdmin(proc *exec.Process, name, nameLen int32) {
w := proc.GetTag().(*CosVMNative)
w.CosAssert(w.ReadContractOwner() == constants.COSSysAccount,
"SetCopyrightAdmin: access denied",
)
w.SetCopyrightAdmin(string(w.cosVM.read(proc, name, nameLen, "setCopyrightAdmin().name")))
}
func e_setCopyright(proc *exec.Process, postIds, postIdsLen, copyrights, copyrightsLen, memoPtrs, memoPtrLen, memoSizes, memoSizeLen int32) int32 {
w := proc.GetTag().(*CosVMNative)
w.CosAssert(
w.ReadContractOwner() == constants.COSSysAccount && w.ReadContractCaller() == w.GetCopyrightAdmin(),
"SetCopyright(): access denied",
)
postIdValues := w.cosVM.read(proc, postIds, postIdsLen, "setCopyright().postIds")
valInts := w.cosVM.read(proc, copyrights, copyrightsLen, "setCopyright().copyrights")
memoPtr := w.cosVM.read(proc, memoPtrs, memoPtrLen, "setCopyright().memoPtrs")
memoSize := w.cosVM.read(proc, memoSizes, memoSizeLen, "setCopyright().memoSizes")
count := int(postIdsLen / 8)
w.CosAssert(postIdsLen == copyrightsLen*2 && postIdsLen == memoPtrLen*2 && postIdsLen == memoSizeLen*2, "setCopyright(): illegal parameters")
for i := 0; i < count; i++ {
longOffset := i * 8
offset := i * 4
id := binary.LittleEndian.Uint64(postIdValues[longOffset:])
value := binary.LittleEndian.Uint32(valInts[offset:])
w.CosAssert(value >= constants.CopyrightUnkown && value <= constants.CopyrightConfirmation,
fmt.Sprintf("setCopyright().copyright[%d]=%d: out of bounds", i, value))
memo := string(w.cosVM.read(proc,
int32(binary.LittleEndian.Uint32(memoPtr[offset:])),
int32(binary.LittleEndian.Uint32(memoSize[offset:])),
fmt.Sprintf("setCopyright().memos[%d]", i),
))
w.SetUserCopyright(id, value, memo)
}
return int32(count)
}
func e_setReputationAdmin(proc *exec.Process, name, nameLen int32) {
w := proc.GetTag().(*CosVMNative)
w.CosAssert(
w.ReadContractOwner() == constants.COSSysAccount,
"SetReputationAdmin(): access denied",
)
w.SetReputationAdmin(string(w.cosVM.read(proc, name, nameLen, "setReputationAdmin().name")))
}
func e_getReputationAdmin(proc *exec.Process, name, nameLen int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.write(proc, []byte(w.GetReputationAdmin()), name, nameLen, "getReputationAdmin()")
}
func e_setReputation(proc *exec.Process, namePtrs, namePtrLen, nameSizes, nameSizeLen, reputations, reputationsLen, memoPtrs, memoPtrLen, memoSizes, memoSizeLen int32) int32 {
w := proc.GetTag().(*CosVMNative)
w.CosAssert(
w.ReadContractOwner() == constants.COSSysAccount && w.ReadContractCaller() == w.GetReputationAdmin(),
"SetReputation(): access denied",
)
namePtr := w.cosVM.read(proc, namePtrs, namePtrLen, "setReputation().namePtrs")
nameSize := w.cosVM.read(proc, nameSizes, nameSizeLen, "setReputation().nameSizes")
valInts := w.cosVM.read(proc, reputations, reputationsLen, "setReputation().reputations")
memoPtr := w.cosVM.read(proc, memoPtrs, memoPtrLen, "setReputation().memoPtrs")
memoSize := w.cosVM.read(proc, memoSizes, memoSizeLen, "setReputation().memoSizes")
count := int(namePtrLen / 4)
w.CosAssert(namePtrLen == nameSizeLen && namePtrLen == reputationsLen && namePtrLen == memoPtrLen && namePtrLen == memoSizeLen, "setReputation(): illegal parameters")
for i := 0; i < count; i++ {
offset := i * 4
name := string(w.cosVM.read(proc,
int32(binary.LittleEndian.Uint32(namePtr[offset:])),
int32(binary.LittleEndian.Uint32(nameSize[offset:])),
fmt.Sprintf("setReputation().names[%d]", i),
))
value := binary.LittleEndian.Uint32(valInts[offset:])
w.CosAssert(value >= constants.MinReputation && value <= constants.MaxReputation,
fmt.Sprintf("setReputation().reputation[%d]=%d: out of bounds", i, value))
memo := string(w.cosVM.read(proc,
int32(binary.LittleEndian.Uint32(memoPtr[offset:])),
int32(binary.LittleEndian.Uint32(memoSize[offset:])),
fmt.Sprintf("setReputation().memos[%d]", i),
))
w.SetUserReputation(name, value, memo)
}
return int32(count)
}
func e_freeze(proc *exec.Process, namePtrs, namePtrLen, nameSizes, nameSizeLen, op, memoPtrs, memoPtrLen, memoSizes, memoSizeLen int32) int32 {
w := proc.GetTag().(*CosVMNative)
w.CosAssert(
w.ReadContractOwner() == constants.COSSysAccount,
"setFreeze(): access denied",
)
namePtr := w.cosVM.read(proc, namePtrs, namePtrLen, "setFreeze().namePtrs")
nameSize := w.cosVM.read(proc, nameSizes, nameSizeLen, "setFreeze().nameSizes")
memoPtr := w.cosVM.read(proc, memoPtrs, memoPtrLen, "setFreeze().memoPtrs")
memoSize := w.cosVM.read(proc, memoSizes, memoSizeLen, "setFreeze().memoSizes")
w.CosAssert(op == 0 || op == 1,"freeze=1 or unfreeze=0")
count := int(namePtrLen / 4)
w.CosAssert(namePtrLen == nameSizeLen && namePtrLen == memoPtrLen && namePtrLen == memoSizeLen, "setFreeze(): illegal parameters")
for i := 0; i < count; i++ {
offset := i * 4
name := string(w.cosVM.read(proc,
int32(binary.LittleEndian.Uint32(namePtr[offset:])),
int32(binary.LittleEndian.Uint32(nameSize[offset:])),
fmt.Sprintf("setFreeze().names[%d]", i),
))
memo := string(w.cosVM.read(proc,
int32(binary.LittleEndian.Uint32(memoPtr[offset:])),
int32(binary.LittleEndian.Uint32(memoSize[offset:])),
fmt.Sprintf("setFreeze().memos[%d]", i),
))
w.SetUserFreeze(name, uint32(op), memo)
}
return int32(count)
}
func e_memcpy(proc *exec.Process, dst, src, size int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.memcpy(proc, dst, src, size)
}
func e_memset(proc *exec.Process, dst, value, size int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.memset(proc, dst, value, size)
}
func e_memmove(proc *exec.Process, dst, src, size int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.memmove(proc, dst, src, size)}
func e_memcmp(proc *exec.Process, lhs, rhs, size int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.memcmp(proc, lhs, rhs, size)
}
func e_copy(proc *exec.Process, src int32, dst int32, length int32) int32 {
w := proc.GetTag().(*CosVMNative)
return w.cosVM.copy(proc, dst, src, length)
}