-
Notifications
You must be signed in to change notification settings - Fork 183
/
cache.go
342 lines (292 loc) · 6.92 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
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
package types
import (
"fmt"
"sync"
"time"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/okex/exchain/libs/cosmos-sdk/store/types"
"github.com/okex/exchain/libs/tendermint/crypto"
"github.com/okex/exchain/libs/tendermint/libs/log"
"github.com/spf13/viper"
)
var (
maxAccInMap = 100000
deleteAccCount = 10000
maxStorageInMap = 10000000
deleteStorageCount = 1000000
FlagMultiCache = "multi-cache"
MaxAccInMultiCache = "multi-cache-acc"
MaxStorageInMultiCache = "multi-cache-storage"
UseCache bool
)
type Account interface {
Copy() Account
GetAddress() AccAddress
SetAddress(AccAddress) error
GetPubKey() crypto.PubKey
SetPubKey(crypto.PubKey) error
GetAccountNumber() uint64
SetAccountNumber(uint64) error
GetSequence() uint64
SetSequence(uint64) error
GetCoins() Coins
SetCoins(Coins) error
SpendableCoins(blockTime time.Time) Coins
String() string
}
type storageWithCache struct {
value []byte
dirty bool
}
type accountWithCache struct {
acc Account
gas uint64
isDirty bool
}
type codeWithCache struct {
code []byte
isDirty bool
}
type Cache struct {
useCache bool
parent *Cache
gasConfig types.GasConfig
storageMap map[ethcmn.Address]map[ethcmn.Hash]*storageWithCache
accMap map[ethcmn.Address]*accountWithCache
codeMap map[ethcmn.Hash]*codeWithCache
accMutex sync.RWMutex
}
func initCacheParam() {
UseCache = viper.GetBool(FlagMultiCache)
if data := viper.GetInt(MaxAccInMultiCache); data != 0 {
maxAccInMap = data
deleteAccCount = maxAccInMap / 10
}
if data := viper.GetInt(MaxStorageInMultiCache); data != 0 {
maxStorageInMap = data
deleteStorageCount = maxStorageInMap / 10
}
}
func NewChainCache() *Cache {
initCacheParam()
return NewCache(nil, UseCache)
}
func NewCache(parent *Cache, useCache bool) *Cache {
return &Cache{
useCache: useCache,
parent: parent,
storageMap: make(map[ethcmn.Address]map[ethcmn.Hash]*storageWithCache),
accMap: make(map[ethcmn.Address]*accountWithCache),
codeMap: make(map[ethcmn.Hash]*codeWithCache),
gasConfig: types.KVGasConfig(),
}
}
func (c *Cache) skip() bool {
if c == nil || !c.useCache {
return true
}
return false
}
func (c *Cache) IsEnabled() bool {
return !c.skip()
}
func (c *Cache) UpdateAccount(addr AccAddress, acc Account, lenBytes int, isDirty bool) {
if c.skip() {
return
}
ethAddr := ethcmn.BytesToAddress(addr.Bytes())
accWithCache := &accountWithCache{
acc: acc,
isDirty: isDirty,
gas: types.Gas(lenBytes)*c.gasConfig.ReadCostPerByte + c.gasConfig.ReadCostFlat,
}
c.accMutex.Lock()
c.accMap[ethAddr] = accWithCache
c.accMutex.Unlock()
}
func (c *Cache) UpdateStorage(addr ethcmn.Address, key ethcmn.Hash, value []byte, isDirty bool) {
if c.skip() {
return
}
if _, ok := c.storageMap[addr]; !ok {
c.storageMap[addr] = make(map[ethcmn.Hash]*storageWithCache, 0)
}
c.storageMap[addr][key] = &storageWithCache{
value: value,
dirty: isDirty,
}
}
func (c *Cache) UpdateCode(key []byte, value []byte, isdirty bool) {
if c.skip() {
return
}
hash := ethcmn.BytesToHash(key)
c.codeMap[hash] = &codeWithCache{
code: value,
isDirty: isdirty,
}
}
func (c *Cache) GetAccount(addr ethcmn.Address) (Account, uint64, bool) {
if c.skip() {
return nil, 0, false
}
c.accMutex.RLock()
data, ok := c.accMap[addr]
c.accMutex.RUnlock()
if ok {
return data.acc, data.gas, ok
}
if c.parent != nil {
acc, gas, ok := c.parent.GetAccount(addr)
return acc, gas, ok
}
return nil, 0, false
}
func (c *Cache) GetStorage(addr ethcmn.Address, key ethcmn.Hash) ([]byte, bool) {
if c.skip() {
return nil, false
}
if _, hasAddr := c.storageMap[addr]; hasAddr {
data, hasKey := c.storageMap[addr][key]
if hasKey {
return data.value, hasKey
}
}
if c.parent != nil {
return c.parent.GetStorage(addr, key)
}
return nil, false
}
func (c *Cache) GetCode(key []byte) ([]byte, bool) {
if c.skip() {
return nil, false
}
hash := ethcmn.BytesToHash(key)
if data, ok := c.codeMap[hash]; ok {
return data.code, ok
}
if c.parent != nil {
return c.parent.GetCode(hash.Bytes())
}
return nil, false
}
func (c *Cache) Write(updateDirty bool) {
if c.skip() {
return
}
if c.parent == nil {
return
}
c.writeStorage(updateDirty)
c.writeAcc(updateDirty)
c.writeCode(updateDirty)
}
func (c *Cache) writeStorage(updateDirty bool) {
for addr, storages := range c.storageMap {
if _, ok := c.parent.storageMap[addr]; !ok {
c.parent.storageMap[addr] = make(map[ethcmn.Hash]*storageWithCache, 0)
}
for key, v := range storages {
if needWriteToParent(updateDirty, v.dirty) {
c.parent.storageMap[addr][key] = v
}
}
}
c.storageMap = make(map[ethcmn.Address]map[ethcmn.Hash]*storageWithCache)
}
func (c *Cache) setAcc(addr ethcmn.Address, v *accountWithCache) {
c.accMutex.Lock()
c.accMap[addr] = v
c.accMutex.Unlock()
}
func (c *Cache) writeAcc(updateDirty bool) {
c.accMutex.RLock()
for addr, v := range c.accMap {
if needWriteToParent(updateDirty, v.isDirty) {
c.parent.setAcc(addr, v)
}
}
c.accMutex.RUnlock()
c.accMutex.Lock()
for k := range c.accMap {
delete(c.accMap, k)
}
c.accMutex.Unlock()
}
func (c *Cache) writeCode(updateDirty bool) {
for hash, v := range c.codeMap {
if needWriteToParent(updateDirty, v.isDirty) {
c.parent.codeMap[hash] = v
}
}
c.codeMap = make(map[ethcmn.Hash]*codeWithCache)
}
func needWriteToParent(updateDirty bool, dirty bool) bool {
// not dirty
if !dirty {
return true
}
// dirty
if updateDirty {
return true
}
return false
}
func (c *Cache) storageSize() int {
lenStorage := 0
for _, v := range c.storageMap {
lenStorage += len(v)
}
return lenStorage
}
func (c *Cache) TryDelete(logger log.Logger, height int64) {
if c.skip() {
return
}
if height%1000 == 0 {
c.logInfo(logger, "null")
}
lenStorage := c.storageSize()
if c.lenOfAccMap() < maxAccInMap && lenStorage < maxStorageInMap {
return
}
deleteMsg := ""
lenOfAcc := c.lenOfAccMap()
if lenOfAcc >= maxAccInMap {
deleteMsg += fmt.Sprintf("Acc:Deleted Before:%d", lenOfAcc)
cnt := 0
c.accMutex.Lock()
for key := range c.accMap {
delete(c.accMap, key)
cnt++
if cnt > deleteAccCount {
break
}
}
c.accMutex.Unlock()
}
if lenStorage >= maxStorageInMap {
deleteMsg += fmt.Sprintf("Storage:Deleted Before:len(contract):%d, len(storage):%d", len(c.storageMap), lenStorage)
cnt := 0
for key, value := range c.storageMap {
cnt += len(value)
delete(c.storageMap, key)
if cnt > deleteStorageCount {
break
}
}
}
if deleteMsg != "" {
c.logInfo(logger, deleteMsg)
}
}
func (c *Cache) logInfo(logger log.Logger, deleteMsg string) {
nowStats := fmt.Sprintf("len(acc):%d len(contracts):%d len(storage):%d", c.lenOfAccMap(), len(c.storageMap), c.storageSize())
logger.Info("MultiCache", "deleteMsg", deleteMsg, "nowStats", nowStats)
}
func (c *Cache) lenOfAccMap() (l int) {
c.accMutex.RLock()
l = len(c.accMap)
c.accMutex.RUnlock()
return
}