/
accountdb.go
478 lines (448 loc) · 15.7 KB
/
accountdb.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package executor
import (
"fmt"
"strconv"
"time"
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/client"
dbm "github.com/33cn/chain33/common/db"
tab "github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
et "github.com/33cn/plugin/plugin/dapp/accountmanager/types"
)
var (
// ConfNameActiveTime 有效期
ConfNameActiveTime = et.AccountmanagerX + "-" + "activeTime"
// ConfNameLockTime 密钥重置锁定期
ConfNameLockTime = et.AccountmanagerX + "-" + "lockTime"
// ConfNameManagerAddr 管理员地址
ConfNameManagerAddr = et.AccountmanagerX + "-" + "managerAddr"
// DefaultActiveTime 默认有效期
DefaultActiveTime = int64(5 * 360 * 24 * 3600)
// DefaultLockTime 默认密钥重置锁定期
DefaultLockTime = int64(15 * 24 * 3600)
// DefaultManagerAddr 默认管理员地址
DefaultManagerAddr = "12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
)
// Action action struct
type Action struct {
statedb dbm.KV
txhash []byte
fromaddr string
blocktime int64
height int64
execaddr string
localDB dbm.KVDB
index int
api client.QueueProtocolAPI
}
//NewAction ...
func NewAction(e *Accountmanager, tx *types.Transaction, index int) *Action {
hash := tx.Hash()
fromaddr := tx.From()
return &Action{e.GetStateDB(), hash, fromaddr,
e.GetBlockTime(), e.GetHeight(), dapp.ExecAddress(string(tx.Execer)), e.GetLocalDB(), index, e.GetAPI()}
}
//GetIndex get index 主键索引,实际上是以过期时间为主键
func (a *Action) GetIndex() int64 {
return a.blocktime*types.MaxTxsPerBlock + int64(a.index)
}
//GetKVSet get kv set
func (a *Action) GetKVSet(account *et.Account) (kvset []*types.KeyValue) {
kvset = append(kvset, &types.KeyValue{Key: calcAccountKey(account.AccountID), Value: types.Encode(account)})
return kvset
}
//Register ...
func (a *Action) Register(payload *et.Register) (*types.Receipt, error) {
var logs []*types.ReceiptLog
account1, err := findAccountByID(a.localDB, payload.AccountID)
if err == nil && account1 != nil {
return nil, et.ErrAccountIDExist
}
//默认有效期时五年
cfg := a.api.GetConfig()
defaultActiveTime := getConfValue(cfg, a.statedb, ConfNameActiveTime, DefaultActiveTime)
account := &et.Account{
AccountID: payload.AccountID,
Addr: a.fromaddr,
PrevAddr: "",
Status: et.Normal,
Level: et.Normal,
CreateTime: a.blocktime,
ExpireTime: a.blocktime + defaultActiveTime,
LockTime: 0,
Index: a.GetIndex(),
}
re := &et.AccountReceipt{
Account: account,
}
receiptlog := &types.ReceiptLog{Ty: et.TyRegisterLog, Log: types.Encode(re)}
logs = append(logs, receiptlog)
receipts := &types.Receipt{Ty: types.ExecOk, KV: nil, Logs: logs}
return receipts, nil
}
//Reset 为了避免别人恶意重置别人的帐号,这个操作仅有系统管理员有权限去操作
func (a *Action) Reset(payload *et.ResetKey) (*types.Receipt, error) {
var logs []*types.ReceiptLog
cfg := a.api.GetConfig()
managerAddr := getManagerAddr(cfg, a.statedb, ConfNameManagerAddr, DefaultManagerAddr)
if managerAddr != a.fromaddr {
return nil, et.ErrNotAdmin
}
account, err := findAccountByID(a.localDB, payload.AccountID)
if err != nil {
return nil, et.ErrAccountIDNotExist
}
//重置公钥锁定期暂定15天,可以由管理员去配置
defaultLockTime := getConfValue(cfg, a.statedb, ConfNameLockTime, DefaultLockTime)
account.Status = et.Locked
account.LockTime = a.blocktime + defaultLockTime
account.PrevAddr = account.Addr
account.Addr = payload.Addr
re := &et.AccountReceipt{
Account: account,
}
receiptlog := &types.ReceiptLog{Ty: et.TyResetLog, Log: types.Encode(re)}
logs = append(logs, receiptlog)
receipts := &types.Receipt{Ty: types.ExecOk, KV: nil, Logs: logs}
return receipts, nil
}
//Transfer ...
func (a *Action) Transfer(payload *et.Transfer) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kvs []*types.KeyValue
cfg := a.api.GetConfig()
account1, err := findAccountByID(a.localDB, payload.FromAccountID)
if err != nil {
elog.Error("Transfer", "fromAccountID", payload.FromAccountID, "err", et.ErrAccountIDNotExist)
return nil, et.ErrAccountIDNotExist
}
if account1.Status != et.Normal || account1.Addr != a.fromaddr || account1.ExpireTime <= a.blocktime {
elog.Error("Transfer", "fromaddr", a.fromaddr, "err", et.ErrAccountIDNotPermiss)
return nil, et.ErrAccountIDNotPermiss
}
//如果prevAddr地址不为空,先查看余额,将该地址下面得资产划转到新得公钥地址下
if account1.PrevAddr != "" {
assetDB, err := account.NewAccountDB(cfg, payload.Asset.GetExec(), payload.Asset.GetSymbol(), a.statedb)
if err != nil {
return nil, err
}
prevAccount := assetDB.LoadExecAccount(account1.PrevAddr, a.execaddr)
if prevAccount.Balance > 0 {
receipt, err := assetDB.ExecTransfer(account1.PrevAddr, account1.Addr, a.execaddr, prevAccount.Balance)
if err != nil {
return nil, err
}
logs = append(logs, receipt.Logs...)
kvs = append(kvs, receipt.KV...)
}
}
if payload.FromAccountID == payload.ToAccountID {
re := &et.TransferReceipt{
FromAccount: account1,
ToAccount: account1,
Index: a.GetIndex(),
}
receiptlog := &types.ReceiptLog{Ty: et.TyTransferLog, Log: types.Encode(re)}
logs = append(logs, receiptlog)
receipts := &types.Receipt{Ty: types.ExecOk, KV: kvs, Logs: logs}
return receipts, nil
}
account2, err := findAccountByID(a.localDB, payload.ToAccountID)
if err != nil {
elog.Error("Transfer,check to accountID", "toAccountID", payload.ToAccountID, "err", et.ErrAccountIDNotExist)
return nil, et.ErrAccountIDNotExist
}
if account2.Status != et.Normal || account2.ExpireTime <= a.blocktime {
elog.Error("Transfer", "ToAccountID", account2.AccountID, "err", et.ErrAccountIDNotPermiss)
return nil, et.ErrAccountIDNotPermiss
}
assetDB, err := account.NewAccountDB(cfg, payload.Asset.GetExec(), payload.Asset.GetSymbol(), a.statedb)
if err != nil {
return nil, err
}
fromAccount := assetDB.LoadExecAccount(a.fromaddr, a.execaddr)
if fromAccount.Balance < payload.Asset.Amount {
elog.Error("Transfer, check balance", "addr", a.fromaddr, "avail", fromAccount.Balance, "need", payload.Asset.Amount)
return nil, et.ErrAssetBalance
}
receipt, err := assetDB.ExecTransfer(account1.Addr, account2.Addr, a.execaddr, payload.Asset.Amount)
if err != nil {
return nil, err
}
logs = append(logs, receipt.Logs...)
kvs = append(kvs, receipt.KV...)
re := &et.TransferReceipt{
FromAccount: account1,
ToAccount: account2,
Index: a.GetIndex(),
}
receiptlog := &types.ReceiptLog{Ty: et.TyTransferLog, Log: types.Encode(re)}
logs = append(logs, receiptlog)
receipts := &types.Receipt{Ty: types.ExecOk, KV: kvs, Logs: logs}
return receipts, nil
}
//Supervise ...
func (a *Action) Supervise(payload *et.Supervise) (*types.Receipt, error) {
//鉴权,看一下地址是否时管理员地址
cfg := a.api.GetConfig()
managerAddr := getManagerAddr(cfg, a.statedb, ConfNameManagerAddr, DefaultManagerAddr)
if managerAddr != a.fromaddr {
return nil, et.ErrNotAdmin
}
var logs []*types.ReceiptLog
var kvs []*types.KeyValue
var re et.SuperviseReceipt
for _, ID := range payload.AccountIDs {
accountM, err := findAccountByID(a.localDB, ID)
if err != nil {
elog.Error("Supervise", "AccountID", ID, "err", et.ErrAccountIDNotExist)
return nil, et.ErrAccountIDNotExist
}
switch payload.Op {
case et.Freeze:
//TODO 冻结操作交给外部其他执行器去控制,处于freeze状态的地址禁止操作
accountM.Status = et.Frozen
case et.UnFreeze:
accountM.Status = et.Normal
case et.AddExpire:
cfg := a.api.GetConfig()
defaultActiveTime := getConfValue(cfg, a.statedb, ConfNameActiveTime, DefaultActiveTime)
accountM.Status = et.Normal
accountM.ExpireTime = a.blocktime + defaultActiveTime
case et.Authorize:
accountM.Level = payload.Level
}
re.Accounts = append(re.Accounts, accountM)
}
re.Op = payload.Op
re.Index = a.GetIndex()
receiptlog := &types.ReceiptLog{Ty: et.TySuperviseLog, Log: types.Encode(&re)}
logs = append(logs, receiptlog)
receipts := &types.Receipt{Ty: types.ExecOk, KV: kvs, Logs: logs}
return receipts, nil
}
//Apply ...
func (a *Action) Apply(payload *et.Apply) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kvs []*types.KeyValue
accountM, err := findAccountByID(a.localDB, payload.AccountID)
if err != nil {
elog.Error("Apply", "AccountID", payload.AccountID, "err", et.ErrAccountIDNotExist)
return nil, et.ErrAccountIDNotExist
}
switch payload.Op {
case et.RevokeReset:
if accountM.Status != et.Locked || accountM.PrevAddr != a.fromaddr {
elog.Error("Apply", "fromaddr", a.fromaddr, "err", et.ErrAccountIDNotPermiss)
return nil, et.ErrAccountIDNotPermiss
}
accountM.LockTime = 0
accountM.Status = et.Normal
accountM.Addr = a.fromaddr
case et.EnforceReset:
if accountM.Status != et.Locked || accountM.Addr != a.fromaddr {
elog.Error("Apply", "fromaddr", a.fromaddr, "err", et.ErrAccountIDNotPermiss)
return nil, et.ErrAccountIDNotPermiss
}
accountM.LockTime = 0
accountM.Status = et.Normal
//TODO 这里只做coins主笔资产得自动划转,token资产转移,放在转transfer中执行 fromAccountID == toAccountID
cfg := a.api.GetConfig()
coinsAssetDB, err := account.NewAccountDB(cfg, "coins", cfg.GetCoinSymbol(), a.statedb)
if err != nil {
return nil, err
}
coinsAccount := coinsAssetDB.LoadExecAccount(accountM.PrevAddr, a.execaddr)
receipt, err := coinsAssetDB.ExecTransfer(accountM.PrevAddr, accountM.Addr, a.execaddr, coinsAccount.Balance)
if err != nil {
elog.Error("Apply ExecTransfer", "AccountID", accountM.AccountID, "err", err)
}
logs = append(logs, receipt.Logs...)
kvs = append(kvs, receipt.KV...)
}
re := &et.AccountReceipt{
Account: accountM,
}
receiptlog := &types.ReceiptLog{Ty: et.TyApplyLog, Log: types.Encode(re)}
logs = append(logs, receiptlog)
receipts := &types.Receipt{Ty: types.ExecOk, KV: kvs, Logs: logs}
return receipts, nil
}
func getConfValue(cfg *types.Chain33Config, db dbm.KV, key string, defaultValue int64) int64 {
var item types.ConfigItem
value, err := getManageKey(cfg, key, db)
if err != nil {
return defaultValue
}
if value != nil {
err = types.Decode(value, &item)
if err != nil {
elog.Debug("accountmanager getConfValue", "decode db key:", key, "err", err.Error())
return defaultValue
}
}
values := item.GetArr().GetValue()
if len(values) == 0 {
elog.Debug("accountmanager getConfValue", "can't get value from values arr. key:", key)
return defaultValue
}
//取数组最后一位,作为最新配置项的值
v, err := strconv.ParseInt(values[len(values)-1], 10, 64)
if err != nil {
elog.Debug("accountmanager getConfValue", "Type conversion error:", err.Error())
return defaultValue
}
return v
}
func getManagerAddr(cfg *types.Chain33Config, db dbm.KV, key, defaultValue string) string {
var item types.ConfigItem
value, err := getManageKey(cfg, key, db)
if err != nil {
return defaultValue
}
if value != nil {
err = types.Decode(value, &item)
if err != nil {
elog.Debug("accountmanager getConfValue", "decode db key:", key, "err", err.Error())
return defaultValue
}
}
values := item.GetArr().GetValue()
if len(values) == 0 {
elog.Debug("accountmanager getConfValue", "can't get value from values arr. key:", key)
return defaultValue
}
return values[len(values)-1]
}
func getManageKey(cfg *types.Chain33Config, key string, db dbm.KV) ([]byte, error) {
manageKey := types.ManageKey(key)
value, err := db.Get([]byte(manageKey))
if err != nil {
if cfg.IsPara() { //平行链只有一种存储方式
elog.Debug("accountmanager getManage", "can't get value from db,key:", key, "err", err.Error())
return nil, err
}
elog.Debug("accountmanager getManageKey", "get db key", "not found")
return getConfigKey(key, db)
}
return value, nil
}
func getConfigKey(key string, db dbm.KV) ([]byte, error) {
configKey := types.ConfigKey(key)
value, err := db.Get([]byte(configKey))
if err != nil {
elog.Debug("accountmanager getConfigKey", "can't get value from db,key:", key, "err", err.Error())
return nil, err
}
return value, nil
}
//正序遍历数据,与传入时间进行对比,看是否逾期
func findAccountListByIndex(localdb dbm.KV, expireTime int64, primaryKey string) (*et.ReplyAccountList, error) {
table := NewAccountTable(localdb)
var rows []*tab.Row
var err error
if primaryKey == "" { //第一次查询,默认展示最新得成交记录
rows, err = table.ListIndex("index", nil, nil, et.Count, et.ListASC)
} else {
rows, err = table.ListIndex("index", nil, []byte(primaryKey), et.Count, et.ListASC)
}
if err != nil {
elog.Error("findAccountListByIndex.", "index", primaryKey, "err", err.Error())
return nil, err
}
var reply et.ReplyAccountList
for _, row := range rows {
account := row.Data.(*et.Account)
if account.ExpireTime > expireTime {
break
}
//状态变成逾期状态
account.Status = et.Expired
reply.Accounts = append(reply.Accounts, account)
}
//设置主键索引
if len(rows) == int(et.Count) {
reply.PrimaryKey = string(rows[len(rows)-1].Primary)
}
return &reply, nil
}
func findAccountByID(localdb dbm.KV, accountID string) (*et.Account, error) {
table := NewAccountTable(localdb)
prefix := []byte(accountID)
//第一次查询,默认展示最新得成交记录
rows, err := table.ListIndex("accountID", prefix, nil, 1, et.ListDESC)
if err != nil {
elog.Debug("findAccountByID.", "accountID", accountID, "err", err.Error())
return nil, err
}
for _, row := range rows {
account := row.Data.(*et.Account)
return account, nil
}
return nil, types.ErrNotFound
}
func findAccountByAddr(localdb dbm.KV, addr string) (*et.Account, error) {
table := NewAccountTable(localdb)
prefix := []byte(addr)
//第一次查询,默认展示最新得成交记录
rows, err := table.ListIndex("addr", prefix, nil, 1, et.ListDESC)
if err != nil {
elog.Error("findAccountByAddr.", "addr", addr, "err", err.Error())
return nil, err
}
for _, row := range rows {
account := row.Data.(*et.Account)
return account, nil
}
return nil, types.ErrNotFound
}
func findAccountListByStatus(localdb dbm.KV, status, direction int32, primaryKey string) (*et.ReplyAccountList, error) {
if status == et.Expired {
return findAccountListByIndex(localdb, time.Now().Unix(), primaryKey)
}
table := NewAccountTable(localdb)
prefix := []byte(fmt.Sprintf("%d", status))
var rows []*tab.Row
var err error
if primaryKey == "" { //第一次查询,默认展示最新得成交记录
rows, err = table.ListIndex("status", prefix, nil, et.Count, direction)
} else {
rows, err = table.ListIndex("status", prefix, []byte(primaryKey), et.Count, direction)
}
if err != nil {
elog.Error("findAccountListByStatus.", "status", status, "err", err.Error())
return nil, err
}
var reply et.ReplyAccountList
for _, row := range rows {
account := row.Data.(*et.Account)
reply.Accounts = append(reply.Accounts, account)
}
//设置主键索引
if len(rows) == int(et.Count) {
reply.PrimaryKey = string(rows[len(rows)-1].Primary)
}
return &reply, nil
}
func queryBalanceByID(statedb, localdb dbm.KV, cfg *types.Chain33Config, execName string, in *et.QueryBalanceByID) (*et.Balance, error) {
acc, err := findAccountByID(localdb, in.AccountID)
if err != nil {
return nil, err
}
assetDB, err := account.NewAccountDB(cfg, in.Asset.GetExec(), in.Asset.GetSymbol(), statedb)
if err != nil {
return nil, err
}
var balance et.Balance
if acc.PrevAddr != "" {
prevAccount := assetDB.LoadExecAccount(acc.PrevAddr, dapp.ExecAddress(execName))
balance.Balance += prevAccount.Balance
balance.Frozen += prevAccount.Frozen
}
currAccount := assetDB.LoadExecAccount(acc.Addr, dapp.ExecAddress(execName))
balance.Balance += currAccount.Balance
balance.Frozen += currAccount.Frozen
return &balance, nil
}