-
Notifications
You must be signed in to change notification settings - Fork 180
/
gasuseddb.go
172 lines (149 loc) · 3.9 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
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
package baseapp
import (
"log"
"path/filepath"
"sync"
"github.com/gogo/protobuf/proto"
lru "github.com/hashicorp/golang-lru"
"github.com/okex/exchain/libs/cosmos-sdk/client/flags"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
db "github.com/okex/exchain/libs/tm-db"
"github.com/spf13/viper"
)
const (
HistoryGasUsedDbDir = "data"
HistoryGasUsedDBName = "hgu"
FlagGasUsedFactor = "gu_factor"
preciseBlockNum = 20
)
var (
once sync.Once
GasUsedFactor = 0.4
regressionFactor = 0.05
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)] = append(h.latestGu[string(key)], gasUsed)
h.latestGuMtx.Unlock()
}
func (h *HistoryGasUsedRecordDB) GetHgu(key []byte) *HguRecord {
hgu, cacheHit := h.getHgu(key)
if hgu != nil && !cacheHit {
// 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, 0, len(h.latestGu))
for key, allGas := range h.latestGu {
latestGasKeys = append(latestGasKeys, gasKey{
gas: meanGas(allGas),
key: key,
})
delete(h.latestGu, key)
}
h.jobQueue <- func() { h.flushHgu(latestGasKeys...) } // closure
}
func (h *HistoryGasUsedRecordDB) getHgu(key []byte) (hgu *HguRecord, fromCache bool) {
v, ok := h.cache.Get(string(key))
if ok {
return v.(*HguRecord), true
}
data, err := h.guDB.Get(key)
if err != nil || len(data) == 0 {
return nil, false
}
var r HguRecord
err = proto.Unmarshal(data, &r)
if err != nil {
return nil, false
}
return &r, false
}
func (h *HistoryGasUsedRecordDB) flushHgu(gks ...gasKey) {
for _, gk := range gks {
hgu, cacheHit := h.getHgu([]byte(gk.key))
if hgu == nil {
hgu = &HguRecord{
MaxGas: gk.gas,
MinGas: gk.gas,
MovingAverageGas: gk.gas,
}
} else {
// MovingAverageGas = 0.4 * newGas + 0.6 * oldMovingAverageGas
hgu.MovingAverageGas = int64(GasUsedFactor*float64(gk.gas) + (1.0-GasUsedFactor)*float64(hgu.MovingAverageGas))
// MaxGas = 0.05 * MovingAverageGas + 0.95 * oldMaxGas
hgu.MaxGas = int64(regressionFactor*float64(hgu.MovingAverageGas) + (1.0-regressionFactor)*float64(hgu.MaxGas))
// MinGas = 0.05 * MovingAverageGas + 0.95 * oldMinGas
hgu.MinGas = int64(regressionFactor*float64(hgu.MovingAverageGas) + (1.0-regressionFactor)*float64(hgu.MinGas))
hgu.BlockNum++
if gk.gas > hgu.MaxGas {
hgu.MaxGas = gk.gas
} else if gk.gas < hgu.MinGas {
hgu.MinGas = gk.gas
}
// add to cache if hit
if cacheHit {
h.cache.Add(gk.key, hgu)
}
}
data, err := proto.Marshal(hgu)
if err != nil {
log.Println("flushHgu marshal error:", err)
continue
}
h.guDB.Set([]byte(gk.key), data)
}
}
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 meanGas(allGas []int64) int64 {
var totalGas int64
for _, gas := range allGas {
totalGas += gas
}
return totalGas / int64(len(allGas))
}