-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
95 lines (79 loc) · 2.29 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
package keeper
import (
"sync"
"github.com/furyaxyz/fuxchain/x/wasm/types"
)
var wasmParamsCache = NewCache()
type Cache struct {
paramsCache types.Params
needParamsUpdate bool
paramsMutex sync.RWMutex
blockedContractMethodsCache map[string]*types.ContractMethods
needBlockedUpdate bool
blockedMutex sync.RWMutex
}
func NewCache() *Cache {
return &Cache{
paramsCache: types.DefaultParams(),
blockedContractMethodsCache: make(map[string]*types.ContractMethods, 0),
needParamsUpdate: true,
needBlockedUpdate: true,
}
}
func (c *Cache) UpdateParams(params types.Params) {
c.paramsMutex.Lock()
defer c.paramsMutex.Unlock()
c.paramsCache = 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() types.Params {
c.paramsMutex.RLock()
defer c.paramsMutex.RUnlock()
return types.Params{
CodeUploadAccess: c.paramsCache.CodeUploadAccess,
InstantiateDefaultPermission: c.paramsCache.InstantiateDefaultPermission,
UseContractBlockedList: c.paramsCache.UseContractBlockedList,
VmbridgeEnable: c.paramsCache.VmbridgeEnable,
}
}
func (c *Cache) SetNeedBlockedUpdate() {
c.blockedMutex.Lock()
defer c.blockedMutex.Unlock()
c.needBlockedUpdate = true
}
func (c *Cache) IsNeedBlockedUpdate() bool {
c.blockedMutex.RLock()
defer c.blockedMutex.RUnlock()
return c.needBlockedUpdate
}
func (c *Cache) GetBlockedContractMethod(addr string) (contract *types.ContractMethods) {
c.blockedMutex.RLock()
bc := c.blockedContractMethodsCache[addr]
c.blockedMutex.RUnlock()
return bc
}
func (c *Cache) UpdateBlockedContractMethod(cms []*types.ContractMethods) {
c.blockedMutex.Lock()
c.blockedContractMethodsCache = make(map[string]*types.ContractMethods, len(cms))
for i, _ := range cms {
c.blockedContractMethodsCache[cms[i].ContractAddr] = cms[i]
}
c.needBlockedUpdate = false
c.blockedMutex.Unlock()
}
func GetWasmParamsCache() *Cache {
return wasmParamsCache
}
func SetNeedParamsUpdate() {
GetWasmParamsCache().SetNeedParamsUpdate()
}