/
token.go
349 lines (309 loc) · 9.86 KB
/
token.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
/*
token执行器支持token的创建,
主要提供操作有以下几种:
1)预创建token;
2)完成创建token
3)撤销预创建
*/
import (
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/common/address"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/system/dapp"
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
tokenty "github.com/33cn/plugin/plugin/dapp/token/types"
"github.com/pkg/errors"
)
var tokenlog = log.New("module", "execs.token")
const (
finisherKey = "token-finisher"
tokenAssetsPrefix = "LODB-token-assets:"
blacklist = "token-blacklist"
)
var driverName = "token"
type subConfig struct {
SaveTokenTxList bool `json:"saveTokenTxList"`
}
var subCfg subConfig
// Init 重命名执行器名称
func Init(name string, cfg *types.Chain33Config, sub []byte) {
if sub != nil {
types.MustDecode(sub, &subCfg)
}
drivers.Register(cfg, GetName(), newToken, cfg.GetDappFork(driverName, "Enable"))
InitExecType()
}
//InitExecType ...
func InitExecType() {
ety := types.LoadExecutorType(driverName)
ety.InitFuncList(types.ListMethod(&token{}))
}
// GetName 获取执行器别名
func GetName() string {
return newToken().GetName()
}
type token struct {
drivers.DriverBase
}
func newToken() drivers.Driver {
t := &token{}
t.SetChild(t)
t.SetExecutorType(types.LoadExecutorType(driverName))
return t
}
// GetDriverName 获取执行器名字
func (t *token) GetDriverName() string {
return driverName
}
// CheckTx ...
func (t *token) CheckTx(tx *types.Transaction, index int) error {
return nil
}
func (t *token) queryTokenAssetsKey(addr string) (*types.ReplyStrings, error) {
key := calcTokenAssetsKey(addr)
value, err := t.GetLocalDB().Get(key)
if value == nil || err != nil {
tokenlog.Error("tokendb", "GetTokenAssetsKey", types.ErrNotFound)
return nil, types.ErrNotFound
}
var assets types.ReplyStrings
err = types.Decode(value, &assets)
if err != nil {
tokenlog.Error("tokendb", "GetTokenAssetsKey", err)
return nil, err
}
return &assets, nil
}
func (t *token) getAccountTokenAssets(req *tokenty.ReqAccountTokenAssets) (types.Message, error) {
var reply = &tokenty.ReplyAccountTokenAssets{}
assets, err := t.queryTokenAssetsKey(req.Address)
if err != nil {
return nil, err
}
cfg := t.GetAPI().GetConfig()
for _, asset := range assets.Datas {
acc, err := account.NewAccountDB(cfg, t.GetName(), asset, t.GetStateDB())
if err != nil {
return nil, err
}
var acc1 *types.Account
if req.Execer == t.GetName() {
acc1 = acc.LoadAccount(req.Address)
} else if req.Execer != "" {
execAddress := address.ExecAddress(req.Execer)
acc1 = acc.LoadExecAccount(req.Address, execAddress)
}
if acc1 == nil {
continue
}
tokenAsset := &tokenty.TokenAsset{Symbol: asset, Account: acc1}
reply.TokenAssets = append(reply.TokenAssets, tokenAsset)
}
return reply, nil
}
func (t *token) getAddrReceiverforTokens(addrTokens *tokenty.ReqAddrTokens) (types.Message, error) {
var reply = &tokenty.ReplyAddrRecvForTokens{}
db := t.GetLocalDB()
reciver := types.Int64{}
for _, token := range addrTokens.Token {
addrRecv, err := db.Get(calcAddrKey(token, addrTokens.Addr))
if addrRecv == nil || err != nil {
continue
}
err = types.Decode(addrRecv, &reciver)
if err != nil {
continue
}
recv := &tokenty.TokenRecv{Token: token, Recv: reciver.Data}
reply.TokenRecvs = append(reply.TokenRecvs, recv)
}
return reply, nil
}
func (t *token) getTokenInfo(symbol string) (types.Message, error) {
if symbol == "" {
return nil, types.ErrInvalidParam
}
key := calcTokenStatusTokenKeyPrefixLocal(tokenty.TokenStatusCreated, symbol)
values, err := t.GetLocalDB().List(key, nil, 0, 0)
if err != nil {
return nil, err
}
if len(values) == 0 || values[0] == nil || len(values[0]) == 0 {
return nil, types.ErrNotFound
}
var tokenInfo tokenty.LocalToken
err = types.Decode(values[0], &tokenInfo)
if err != nil {
return &tokenInfo, err
}
return &tokenInfo, nil
}
func (t *token) getTokens(reqTokens *tokenty.ReqTokens) (types.Message, error) {
replyTokens := &tokenty.ReplyTokens{}
tokens, err := t.listTokenKeys(reqTokens)
if err != nil {
return nil, err
}
tokenlog.Error("token Query GetTokens", "get count", len(tokens))
if reqTokens.SymbolOnly {
for _, t1 := range tokens {
if len(t1) == 0 {
continue
}
var tokenValue tokenty.LocalToken
err = types.Decode(t1, &tokenValue)
if err == nil {
token := tokenty.LocalToken{Symbol: tokenValue.Symbol}
replyTokens.Tokens = append(replyTokens.Tokens, &token)
}
}
return replyTokens, nil
}
for _, t1 := range tokens {
// delete impl by set nil
if len(t1) == 0 {
continue
}
var token tokenty.LocalToken
err = types.Decode(t1, &token)
if err == nil {
replyTokens.Tokens = append(replyTokens.Tokens, &token)
}
}
//tokenlog.Info("token Query", "replyTokens", replyTokens)
return replyTokens, nil
}
func (t *token) listTokenKeys(reqTokens *tokenty.ReqTokens) ([][]byte, error) {
querydb := t.GetLocalDB()
if reqTokens.QueryAll {
keys, err := querydb.List(calcTokenStatusKeyPrefixLocal(reqTokens.Status), nil, 0, 0)
if err != nil && err != types.ErrNotFound {
return nil, err
}
if len(keys) == 0 {
return nil, types.ErrNotFound
}
tokenlog.Debug("token Query GetTokens", "get count", len(keys))
return keys, nil
}
var keys [][]byte
for _, token := range reqTokens.Tokens {
keys1, err := querydb.List(calcTokenStatusTokenKeyPrefixLocal(reqTokens.Status, token), nil, 0, 0)
if err != nil && err != types.ErrNotFound {
return nil, err
}
keys = append(keys, keys1...)
tokenlog.Debug("token Query GetTokens", "get count", len(keys))
}
if len(keys) == 0 {
return nil, types.ErrNotFound
}
return keys, nil
}
// value 对应 statedb 的key
func (t *token) saveLogs(receipt *tokenty.ReceiptToken) []*types.KeyValue {
var kv []*types.KeyValue
key := calcTokenStatusKeyLocal(receipt.Symbol, receipt.Owner, receipt.Status)
cfg := t.GetAPI().GetConfig()
var value []byte
if cfg.IsFork(t.GetHeight(), "ForkExecKey") {
value = calcTokenAddrNewKeyS(receipt.Symbol, receipt.Owner)
} else {
value = calcTokenAddrKeyS(receipt.Symbol, receipt.Owner)
}
kv = append(kv, &types.KeyValue{Key: key, Value: value})
//如果当前需要被更新的状态不是Status_PreCreated,则认为之前的状态是precreate,且其对应的key需要被删除
if receipt.Status != tokenty.TokenStatusPreCreated {
key = calcTokenStatusKeyLocal(receipt.Symbol, receipt.Owner, tokenty.TokenStatusPreCreated)
kv = append(kv, &types.KeyValue{Key: key, Value: nil})
}
return kv
}
func (t *token) deleteLogs(receipt *tokenty.ReceiptToken) []*types.KeyValue {
var kv []*types.KeyValue
key := calcTokenStatusKeyLocal(receipt.Symbol, receipt.Owner, receipt.Status)
kv = append(kv, &types.KeyValue{Key: key, Value: nil})
//如果当前需要被更新的状态不是Status_PreCreated,则认为之前的状态是precreate,且其对应的key需要被恢复
if receipt.Status != tokenty.TokenStatusPreCreated {
key = calcTokenStatusKeyLocal(receipt.Symbol, receipt.Owner, tokenty.TokenStatusPreCreated)
var value []byte
cfg := t.GetAPI().GetConfig()
if cfg.IsFork(t.GetHeight(), "ForkExecKey") {
value = calcTokenAddrNewKeyS(receipt.Symbol, receipt.Owner)
} else {
value = calcTokenAddrKeyS(receipt.Symbol, receipt.Owner)
}
kv = append(kv, &types.KeyValue{Key: key, Value: value})
}
return kv
}
func (t *token) makeTokenTxKvs(tx *types.Transaction, action *tokenty.TokenAction, receipt *types.ReceiptData, index int, isDel bool) ([]*types.KeyValue, error) {
var kvs []*types.KeyValue
var symbol string
if action.Ty == tokenty.ActionTransfer {
symbol = action.GetTransfer().Cointoken
} else if action.Ty == tokenty.ActionWithdraw {
symbol = action.GetWithdraw().Cointoken
} else if action.Ty == tokenty.TokenActionTransferToExec {
symbol = action.GetTransferToExec().Cointoken
} else {
return kvs, nil
}
kvs, err := tokenTxKvs(tx, symbol, t.GetHeight(), int64(index), isDel)
return kvs, err
}
func findTokenTxListUtil(req *tokenty.ReqTokenTx) ([]byte, []byte) {
var key, prefix []byte
if len(req.Addr) > 0 {
if req.Flag == 0 {
prefix = calcTokenAddrTxKey(req.Symbol, req.Addr, -1, 0)
key = calcTokenAddrTxKey(req.Symbol, req.Addr, req.Height, req.Index)
} else {
prefix = calcTokenAddrTxDirKey(req.Symbol, req.Addr, req.Flag, -1, 0)
key = calcTokenAddrTxDirKey(req.Symbol, req.Addr, req.Flag, req.Height, req.Index)
}
} else {
prefix = calcTokenTxKey(req.Symbol, -1, 0)
key = calcTokenTxKey(req.Symbol, req.Height, req.Index)
}
if req.Height == -1 {
key = nil
}
return key, prefix
}
func (t *token) getTxByToken(req *tokenty.ReqTokenTx) (types.Message, error) {
if req.Flag != 0 && req.Flag != dapp.TxIndexFrom && req.Flag != dapp.TxIndexTo {
err := types.ErrInvalidParam
return nil, errors.Wrap(err, "flag unknown")
}
key, prefix := findTokenTxListUtil(req)
tokenlog.Debug("GetTxByToken", "key", string(key), "prefix", string(prefix))
db := t.GetLocalDB()
txinfos, err := db.List(prefix, key, req.Count, req.Direction)
if err != nil {
return nil, errors.Wrap(err, "db.List to find token tx list")
}
if len(txinfos) == 0 {
return nil, errors.Wrapf(types.ErrNotFound, "key=%s, prefix=%s", string(key), string(prefix))
}
var replyTxInfos types.ReplyTxInfos
replyTxInfos.TxInfos = make([]*types.ReplyTxInfo, len(txinfos))
for index, txinfobyte := range txinfos {
var replyTxInfo types.ReplyTxInfo
err := types.Decode(txinfobyte, &replyTxInfo)
if err != nil {
return nil, err
}
replyTxInfos.TxInfos[index] = &replyTxInfo
}
return &replyTxInfos, nil
}
// CheckReceiptExecOk return true to check if receipt ty is ok
func (t *token) CheckReceiptExecOk() bool {
return true
}