-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountindex.go
86 lines (78 loc) · 2.14 KB
/
accountindex.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
package mempool
import (
"github.com/PhenixChain/devchain/common/listmap"
"github.com/PhenixChain/devchain/types"
)
//AccountTxIndex 账户和交易索引
type AccountTxIndex struct {
maxperaccount int
accMap map[string]*listmap.ListMap
}
//NewAccountTxIndex 创建一个新的索引
func NewAccountTxIndex(maxperaccount int) *AccountTxIndex {
return &AccountTxIndex{
maxperaccount: maxperaccount,
accMap: make(map[string]*listmap.ListMap),
}
}
// TxNumOfAccount 返回账户在Mempool中交易数量
func (cache *AccountTxIndex) TxNumOfAccount(addr string) int {
if _, ok := cache.accMap[addr]; ok {
return cache.accMap[addr].Size()
}
return 0
}
// GetAccTxs 用来获取对应账户地址(列表)中的全部交易详细信息
func (cache *AccountTxIndex) GetAccTxs(addrs *types.ReqAddrs) *types.TransactionDetails {
res := &types.TransactionDetails{}
for _, addr := range addrs.Addrs {
if value, ok := cache.accMap[addr]; ok {
value.Walk(func(val interface{}) bool {
v := val.(*types.Transaction)
txAmount, err := v.Amount()
if err != nil {
txAmount = 0
}
res.Txs = append(res.Txs,
&types.TransactionDetail{
Tx: v,
Amount: txAmount,
Fromaddr: addr,
ActionName: v.ActionName(),
})
return true
})
}
}
return res
}
//Remove 根据交易哈希删除对应账户的对应交易
func (cache *AccountTxIndex) Remove(tx *types.Transaction) {
addr := tx.From()
if lm, ok := cache.accMap[addr]; ok {
lm.Remove(string(tx.Hash()))
if lm.Size() == 0 {
delete(cache.accMap, addr)
}
}
}
// Push push transaction to AccountTxIndex
func (cache *AccountTxIndex) Push(tx *types.Transaction) error {
addr := tx.From()
_, ok := cache.accMap[addr]
if !ok {
cache.accMap[addr] = listmap.New()
}
if cache.accMap[addr].Size() >= cache.maxperaccount {
return types.ErrManyTx
}
cache.accMap[addr].Push(string(tx.Hash()), tx)
return nil
}
//CanPush 是否可以push 进 account index
func (cache *AccountTxIndex) CanPush(tx *types.Transaction) bool {
if item, ok := cache.accMap[tx.From()]; ok {
return item.Size() < cache.maxperaccount
}
return true
}