/
shorthashtx.go
62 lines (52 loc) · 1.67 KB
/
shorthashtx.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
package mempool
import (
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/listmap"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types"
)
var shashlog = log.New("module", "mempool.shash")
//SHashTxCache 通过shorthash缓存交易
type SHashTxCache struct {
max int
l *listmap.ListMap
}
//NewSHashTxCache 创建通过短hash交易的cache
func NewSHashTxCache(size int) *SHashTxCache {
return &SHashTxCache{
max: size,
l: listmap.New(),
}
}
//GetSHashTxCache 返回shorthash对应的tx交易信息
func (cache *SHashTxCache) GetSHashTxCache(sHash string) *types.Transaction {
tx, err := cache.l.GetItem(sHash)
if err != nil {
return nil
}
return tx.(*types.Transaction)
}
//Remove remove tx of SHashTxCache
func (cache *SHashTxCache) Remove(tx *types.Transaction) {
txhash := tx.Hash()
cache.l.Remove(types.CalcTxShortHash(txhash))
//shashlog.Debug("SHashTxCache:Remove", "shash", types.CalcTxShortHash(txhash), "txhash", common.ToHex(txhash))
}
//Push tx into SHashTxCache
func (cache *SHashTxCache) Push(tx *types.Transaction) {
shash := types.CalcTxShortHash(tx.Hash())
if cache.Exist(shash) {
shashlog.Error("SHashTxCache:Push:Exist", "oldhash", common.ToHex(cache.GetSHashTxCache(shash).Hash()), "newhash", common.ToHex(tx.Hash()))
return
}
if cache.l.Size() >= cache.max {
shashlog.Error("SHashTxCache:Push:ErrMemFull", "cache.l.Size()", cache.l.Size(), "cache.max", cache.max)
return
}
cache.l.Push(shash, tx)
//shashlog.Debug("SHashTxCache:Push", "shash", shash, "txhash", common.ToHex(tx.Hash()))
}
//Exist 是否存在
func (cache *SHashTxCache) Exist(shash string) bool {
return cache.l.Exist(shash)
}