-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
114 lines (94 loc) · 2.44 KB
/
cache.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
package types
import (
"sync"
"github.com/ethereum/go-ethereum/common"
lru "github.com/hashicorp/golang-lru"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
)
const cacheSize = 1024
var paramsCache = NewCache()
type Cache struct {
params Params
needParamsUpdate bool
paramsMutex sync.RWMutex
feeSplits *lru.Cache
shares *lru.Cache
}
func NewCache() *Cache {
c := &Cache{
params: DefaultParams(),
needParamsUpdate: true,
}
c.feeSplits, _ = lru.New(cacheSize)
c.shares, _ = lru.New(cacheSize)
return c
}
// UpdateParams the update in params is relates to the proposal and initGenesis
func (c *Cache) UpdateParams(params Params, isCheckTx bool) {
if isCheckTx {
return
}
c.paramsMutex.Lock()
defer c.paramsMutex.Unlock()
c.params = params
c.needParamsUpdate = false
}
func (c *Cache) SetNeedParamsUpdate() {
c.paramsMutex.Lock()
defer c.paramsMutex.Unlock()
c.needParamsUpdate = true
}
func (c *Cache) IsNeedParamsUpdate() bool {
c.paramsMutex.RLock()
defer c.paramsMutex.RUnlock()
return c.needParamsUpdate
}
func (c *Cache) GetParams() Params {
c.paramsMutex.RLock()
defer c.paramsMutex.RUnlock()
return NewParams(c.params.EnableFeeSplit,
c.params.DeveloperShares,
c.params.AddrDerivationCostCreate,
)
}
// UpdateFeeSplit The change in feeSplit is only related to the user tx(register,update,cancel)
func (c *Cache) UpdateFeeSplit(contract common.Address, feeSplit FeeSplit, isCheckTx bool) {
if isCheckTx {
return
}
c.feeSplits.Add(contract, feeSplit)
}
// DeleteFeeSplit The change in feeSplit is only related to the user tx(register,update,cancel)
func (c *Cache) DeleteFeeSplit(contract common.Address, isCheckTx bool) {
if isCheckTx {
return
}
c.feeSplits.Remove(contract)
}
func (c *Cache) GetFeeSplit(contract common.Address) (FeeSplit, bool) {
feeSplit, found := c.feeSplits.Get(contract)
if found {
return feeSplit.(FeeSplit), true
}
return FeeSplit{}, false
}
// UpdateShare The change in share is only related to the proposal
func (c *Cache) UpdateShare(contract common.Address, share sdk.Dec, isCheckTx bool) {
if isCheckTx {
return
}
c.shares.Add(contract, share)
}
func (c *Cache) GetShare(contract common.Address) (sdk.Dec, bool) {
share, found := c.shares.Get(contract)
if found {
return share.(sdk.Dec), true
}
return sdk.Dec{}, false
}
func SetParamsNeedUpdate() {
paramsCache.SetNeedParamsUpdate()
}
func GetParamsCache() *Cache {
return paramsCache
}