-
Notifications
You must be signed in to change notification settings - Fork 1
/
callback.go
184 lines (160 loc) · 4.22 KB
/
callback.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
package executor
import (
"github.com/assetcloud/chain/common"
"github.com/assetcloud/chain/common/address"
"github.com/assetcloud/chain/types"
types2 "github.com/assetcloud/plugin/plugin/dapp/wasm/types"
)
//stateDB wrapper
func setStateDB(key, value []byte) {
wasmCB.stateKVC.Add(key, value)
}
func getStateDBSize(key []byte) int {
value, err := getStateDB(key)
if err != nil {
return 0
}
return len(value)
}
func getStateDB(key []byte) ([]byte, error) {
return wasmCB.stateKVC.Get(key)
}
//localDB wrapper
func setLocalDB(key, value []byte) {
wasmCB.localCache = append(wasmCB.localCache, &types2.LocalDataLog{
Key: append(calcLocalPrefix(wasmCB.contractName), key...),
Value: value,
})
}
func getLocalDBSize(key []byte) int {
value, err := getLocalDB(key)
if err != nil {
return 0
}
return len(value)
}
func getLocalDB(key []byte) ([]byte, error) {
newKey := append(calcLocalPrefix(wasmCB.contractName), key...)
// 先查缓存,再查数据库
for _, kv := range wasmCB.localCache {
if string(newKey) == string(kv.Key) {
return kv.Value, nil
}
}
return wasmCB.GetLocalDB().Get(newKey)
}
//account wrapper
func getBalance(addr, execer string) (balance, frozen int64, err error) {
accounts, err := wasmCB.GetCoinsAccount().GetBalance(wasmCB.GetAPI(), &types.ReqBalance{
Addresses: []string{addr},
Execer: execer,
})
if err != nil {
return -1, -1, err
}
return accounts[0].Balance, accounts[0].Frozen, nil
}
func transfer(from, to string, amount int64) error {
receipt, err := wasmCB.GetCoinsAccount().Transfer(from, to, amount)
if err != nil {
return err
}
wasmCB.receiptLogs = append(wasmCB.receiptLogs, receipt.Logs...)
return nil
}
func transferToExec(addr, execaddr string, amount int64) error {
receipt, err := wasmCB.GetCoinsAccount().TransferToExec(addr, execaddr, amount)
if err != nil {
return err
}
wasmCB.receiptLogs = append(wasmCB.receiptLogs, receipt.Logs...)
return nil
}
func transferWithdraw(addr, execaddr string, amount int64) error {
receipt, err := wasmCB.GetCoinsAccount().TransferWithdraw(addr, execaddr, amount)
if err != nil {
return err
}
wasmCB.receiptLogs = append(wasmCB.receiptLogs, receipt.Logs...)
return nil
}
func execFrozen(addr string, amount int64) error {
receipt, err := wasmCB.GetCoinsAccount().ExecFrozen(addr, wasmCB.execAddr, amount)
if err != nil {
log.Error("execFrozen", "error", err)
return err
}
wasmCB.kvs = append(wasmCB.kvs, receipt.KV...)
wasmCB.receiptLogs = append(wasmCB.receiptLogs, receipt.Logs...)
return nil
}
func execActive(addr string, amount int64) error {
receipt, err := wasmCB.GetCoinsAccount().ExecActive(addr, wasmCB.execAddr, amount)
if err != nil {
return err
}
wasmCB.kvs = append(wasmCB.kvs, receipt.KV...)
wasmCB.receiptLogs = append(wasmCB.receiptLogs, receipt.Logs...)
return nil
}
func execTransfer(from, to string, amount int64) error {
receipt, err := wasmCB.GetCoinsAccount().ExecTransfer(from, to, wasmCB.execAddr, amount)
if err != nil {
return err
}
wasmCB.kvs = append(wasmCB.kvs, receipt.KV...)
wasmCB.receiptLogs = append(wasmCB.receiptLogs, receipt.Logs...)
return nil
}
func execTransferFrozen(from, to string, amount int64) error {
receipt, err := wasmCB.GetCoinsAccount().ExecTransferFrozen(from, to, wasmCB.execAddr, amount)
if err != nil {
return err
}
wasmCB.kvs = append(wasmCB.kvs, receipt.KV...)
wasmCB.receiptLogs = append(wasmCB.receiptLogs, receipt.Logs...)
return nil
}
func execAddress(name string) string {
return address.ExecAddress(name)
}
func getFrom() string {
return wasmCB.tx.From()
}
func getHeight() int64 {
return wasmCB.GetHeight()
}
func getRandom() int64 {
req := &types.ReqRandHash{
ExecName: "ticket",
BlockNum: 5,
Hash: wasmCB.GetLastHash(),
}
hash, err := wasmCB.GetExecutorAPI().GetRandNum(req)
if err != nil {
return -1
}
var rand int64
for _, c := range hash {
rand = rand*256 + int64(c)
}
if rand < 0 {
return -rand
}
return rand
}
func printlog(s string) {
wasmCB.customLogs = append(wasmCB.customLogs, s)
}
func sha256(data []byte) []byte {
return common.Sha256(data)
}
func getENVSize(n int) int {
return len(wasmCB.ENV[n])
}
func getENV(n int) string {
return wasmCB.ENV[n]
}
func totalENV() int {
return len(wasmCB.ENV)
}