-
Notifications
You must be signed in to change notification settings - Fork 113
/
gas_handler.go
242 lines (215 loc) · 6.78 KB
/
gas_handler.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
package database
import (
"github.com/iost-official/go-iost/common"
"github.com/iost-official/go-iost/ilog"
)
const (
// GasPledgeTotalKey : how many IOST is pledged
GasPledgeTotalKey = "gt"
// GasLimitKey : how much gas can be generated max per IOST
GasLimitKey = "gl"
// GasUpdateTimeKey : when the gas state is refreshed last time, for internal use
GasUpdateTimeKey = "gu"
// GasStockKey : how much gas is there when last time refreshed
GasStockKey = "gs"
// GasPledgeKey : i pledge how much coins for others
GasPledgeKey = "gp"
// TransferableGasKey :
TransferableGasKey = "tg"
// TransferableGasQuotaKey :
TransferableGasQuotaKey = "tq"
)
// decimals of gas
const (
GasDecimal = 2
)
const gasMaxIncreaseSeconds = 3 * 24 * 3600
// Each IOST you pledge, you will get `GasImmediateReward` gas immediately.
// Then gas will be generated at a rate of `GasIncreaseRate` gas per second.
// Then it takes `GasFulfillSeconds` time to reach the limit.
// Your gas production will stop when it reaches the limit.
// When you use some gas later, the total amount will be less than the limit,
// so gas production will resume again util the limit.
// GasMinPledgeOfUser Each user must pledge a minimum amount of IOST
var GasMinPledgeOfUser = &common.Fixed{Value: 10 * IOSTRatio, Decimal: 8}
// GasMinPledgePerAction One must (un)pledge more than 1 IOST
var GasMinPledgePerAction = &common.Fixed{Value: 1 * IOSTRatio, Decimal: 8}
// GasImmediateReward immediate reward per IOST
var GasImmediateReward = &common.Fixed{Value: 100000 * 100, Decimal: 2}
// GasLimit gas limit per IOST
var GasLimit = &common.Fixed{Value: 300000 * 100, Decimal: 2}
// GasFulfillSeconds it takes 2 days to fulfill the gas buffer.
const GasFulfillSeconds int64 = 2 * 24 * 3600
// GasIncreaseRate gas increase per IOST per second
var GasIncreaseRate = GasLimit.Sub(GasImmediateReward).Div(GasFulfillSeconds)
// IOSTRatio ...
const IOSTRatio int64 = 100000000
// PledgerInfo ...
type PledgerInfo struct {
Pledger string
Amount *common.Fixed
}
// GasContractName name of basic token contract
const GasContractName = "gas.iost"
// GasHandler easy to get balance of token.iost
type GasHandler struct {
BasicHandler
MapHandler
}
// EmptyGas ...
func EmptyGas() *common.Fixed {
return &common.Fixed{
Value: 0,
Decimal: GasDecimal,
}
}
// If no key exists, return 0
func (g *GasHandler) getFixed(key string) *common.Fixed {
result := MustUnmarshal(g.BasicHandler.Get(GasContractName + Separator + key))
if result == nil {
//ilog.Errorf("GasHandler failed %v", key)
return nil
}
value, ok := result.(*common.Fixed)
if !ok {
ilog.Errorf("GasHandler failed %v %v", key, result)
return nil
}
return value
}
// putFixed ...
func (g *GasHandler) putFixed(key string, value *common.Fixed) {
if value.Err != nil {
ilog.Fatalf("GasHandler putFixed %v", value)
}
//fmt.Printf("putFixed %v %v\n", key, value)
g.BasicHandler.Put(GasContractName+Separator+key, MustMarshal(value))
}
// GasPledgeTotal ...
func (g *GasHandler) GasPledgeTotal(name string) *common.Fixed {
f := g.getFixed(name + GasPledgeTotalKey)
if f == nil {
return EmptyGas()
}
return f
}
// GasLimit ...
func (g *GasHandler) GasLimit(name string) *common.Fixed {
f := g.getFixed(name + GasLimitKey)
if f == nil {
return EmptyGas()
}
return f
}
// GasUpdateTime ...
func (g *GasHandler) GasUpdateTime(name string) int64 {
value := MustUnmarshal(g.BasicHandler.Get(GasContractName + Separator + name + GasUpdateTimeKey))
if value == nil {
return 0
}
return value.(int64)
}
// GasStock `gasStock` means the gas amount at last update time.
func (g *GasHandler) GasStock(name string) *common.Fixed {
f := g.getFixed(name + GasStockKey)
if f == nil {
return EmptyGas()
}
return f
}
// TGas ...
func (g *GasHandler) TGas(name string) *common.Fixed {
f := g.getFixed(name + TransferableGasKey)
if f == nil {
return EmptyGas()
}
return f
}
// ChangeTGas ...
func (g *GasHandler) ChangeTGas(name string, delta *common.Fixed) {
oldValue := g.TGas(name)
newValue := oldValue.Add(delta)
g.putFixed(name+TransferableGasKey, newValue)
}
// GasPledge ...
func (g *GasHandler) GasPledge(name string, pledger string) *common.Fixed {
ok := g.MapHandler.MHas(GasContractName+Separator+pledger+GasPledgeKey, name)
if !ok {
return &common.Fixed{
Value: 0,
Decimal: 8,
}
}
result := MustUnmarshal(g.MapHandler.MGet(GasContractName+Separator+pledger+GasPledgeKey, name))
value, ok := result.(*common.Fixed)
if !ok {
return nil
}
return value
}
// PledgerInfo get I pledged how much coins for others
func (g *GasHandler) PledgerInfo(name string) []PledgerInfo {
pledgees := g.MapHandler.MKeys(GasContractName + Separator + name + GasPledgeKey)
result := make([]PledgerInfo, 0)
usedPledgees := make(map[string]bool)
for _, pledgee := range pledgees {
if _, ok := usedPledgees[pledgee]; ok {
continue
}
usedPledgees[pledgee] = true
s := g.MapHandler.MGet(GasContractName+Separator+name+GasPledgeKey, pledgee)
v := MustUnmarshal(s)
pledge, ok := v.(*common.Fixed)
if !ok || pledge.IsZero() {
continue
}
result = append(result, PledgerInfo{pledgee, pledge})
}
return result
}
// PGasAtTime return pledge gas at given time. It is min(limit, last_updated_gas + time_since_last_updated * increase_speed)
func (g *GasHandler) PGasAtTime(name string, t int64) (result *common.Fixed) {
if t <= 0 {
ilog.Fatalf("PGasAtTime failed. invalid t time %v", t)
}
result = g.GasStock(name)
gasUpdateTime := g.GasUpdateTime(name)
var durationSeconds float64
if gasUpdateTime > 0 {
durationSeconds = float64(t-gasUpdateTime) / float64(1e9)
if durationSeconds > gasMaxIncreaseSeconds {
durationSeconds = gasMaxIncreaseSeconds
}
}
if durationSeconds < 0 {
ilog.Errorf("PGasAtTime durationSeconds invalid %v = %v - %v", durationSeconds, t, gasUpdateTime)
}
rate := g.GasPledgeTotal(name).Multiply(GasIncreaseRate)
if rate == nil {
// this line is compatible, since 'rate' was never nil
rate = g.GasPledgeTotal(name).ChangeDecimal(0).Multiply(GasIncreaseRate)
}
limit := g.GasLimit(name)
//fmt.Printf("PGasAtTime user %v stock %v rate %v limit %v durationSeconds %v\n", name, result, rate, limit, durationSeconds)
delta := rate.TimesF(durationSeconds)
if delta == nil {
ilog.Errorf("PGasAtTime may overflow rate %v durationSeconds %v", rate, durationSeconds)
return
}
finalResult := delta.Add(result)
if finalResult == nil {
// this line is compatible, since 'finalResult' was never nil
ilog.Errorf("PGasAtTime may overflow result %v delta %v", result, delta)
return
}
if limit.LessThan(finalResult) {
result = limit
} else {
result = finalResult
}
return
}
// TotalGasAtTime return total gas at given time.. It is pgas + tgas
func (g *GasHandler) TotalGasAtTime(name string, t int64) (result *common.Fixed) {
return g.TGas(name).Add(g.PGasAtTime(name, t))
}