/
gasuseddb.go
141 lines (119 loc) · 2.97 KB
/
gasuseddb.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
package baseapp
import (
"encoding/binary"
"path/filepath"
"sync"
lru "github.com/hashicorp/golang-lru"
"github.com/FiboChain/fbc/libs/cosmos-sdk/client/flags"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
db "github.com/FiboChain/fbc/libs/tm-db"
"github.com/spf13/viper"
)
const (
HistoryGasUsedDbDir = "data"
HistoryGasUsedDBName = "hgu"
FlagGasUsedFactor = "gu_factor"
)
var (
once sync.Once
GasUsedFactor = 0.4
jobQueueLen = 10
cacheSize = 10000
historyGasUsedRecordDB HistoryGasUsedRecordDB
)
type gasKey struct {
gas int64
key string
}
type HistoryGasUsedRecordDB struct {
latestGuMtx sync.Mutex
latestGu map[string]int64
cache *lru.Cache
guDB db.DB
jobQueue chan func()
}
func InstanceOfHistoryGasUsedRecordDB() *HistoryGasUsedRecordDB {
once.Do(func() {
cache, _ := lru.New(cacheSize)
historyGasUsedRecordDB = HistoryGasUsedRecordDB{
latestGu: make(map[string]int64),
cache: cache,
guDB: initDb(),
jobQueue: make(chan func(), jobQueueLen),
}
go historyGasUsedRecordDB.updateRoutine()
})
return &historyGasUsedRecordDB
}
func (h *HistoryGasUsedRecordDB) UpdateGasUsed(key []byte, gasUsed int64) {
h.latestGuMtx.Lock()
h.latestGu[string(key)] = gasUsed
h.latestGuMtx.Unlock()
}
func (h *HistoryGasUsedRecordDB) GetHgu(key []byte) int64 {
hgu, cacheHit := h.getHgu(key)
if !cacheHit && hgu != -1 {
// add to cache before returning hgu
h.cache.Add(string(key), hgu)
}
return hgu
}
func (h *HistoryGasUsedRecordDB) FlushHgu() {
if len(h.latestGu) == 0 {
return
}
latestGasKeys := make([]gasKey, len(h.latestGu))
for key, gas := range h.latestGu {
latestGasKeys = append(latestGasKeys, gasKey{
gas: gas,
key: key,
})
delete(h.latestGu, key)
}
h.jobQueue <- func() { h.flushHgu(latestGasKeys...) } // closure
}
func (h *HistoryGasUsedRecordDB) getHgu(key []byte) (hgu int64, fromCache bool) {
v, ok := h.cache.Get(string(key))
if ok {
return v.(int64), true
}
data, err := h.guDB.Get(key)
if err != nil || len(data) == 0 {
return -1, false
}
return bytesToInt64(data), false
}
func (h *HistoryGasUsedRecordDB) flushHgu(gks ...gasKey) {
for _, gk := range gks {
hgu, cacheHit := h.getHgu([]byte(gk.key))
// avgGas = 0.4 * newGas + 0.6 * oldGas
avgGas := int64(GasUsedFactor*float64(gk.gas) + (1.0-GasUsedFactor)*float64(hgu))
// add to cache if hit
if cacheHit {
h.cache.Add(gk.key, avgGas)
}
h.guDB.Set([]byte(gk.key), int64ToBytes(avgGas))
}
}
func (h *HistoryGasUsedRecordDB) updateRoutine() {
for job := range h.jobQueue {
job()
}
}
func initDb() db.DB {
homeDir := viper.GetString(flags.FlagHome)
dbPath := filepath.Join(homeDir, HistoryGasUsedDbDir)
db, err := sdk.NewDB(HistoryGasUsedDBName, dbPath)
if err != nil {
panic(err)
}
return db
}
func int64ToBytes(i int64) []byte {
var buf = make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
func bytesToInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}