-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint.go
286 lines (249 loc) · 8.29 KB
/
checkpoint.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/petri-labs/kaiju/x/ve/types"
)
// RegulateUserCheckpoint regulates user checkpoint.
// veID: must be valid ve id
// lockedOld:
// Amount: can be zero
// End: can be expired or zero
// lockedNew:
// Amount: can be zero
// End: must be in the future or be zero
func (k Keeper) RegulateUserCheckpoint(ctx sdk.Context, veID uint64, lockedOld types.LockedBalance, lockedNew types.LockedBalance) {
// check whether timestamp is regulated
types.CheckRegulatedUnixTime(lockedOld.End)
types.CheckRegulatedUnixTime(lockedNew.End)
// use block time as now timestamp
now := uint64(ctx.BlockTime().Unix())
// user point initialized with zero values
userPointOld := types.Checkpoint{
Bias: sdk.ZeroInt(),
Slope: sdk.ZeroInt(),
}
userPointNew := types.Checkpoint{
Bias: sdk.ZeroInt(),
Slope: sdk.ZeroInt(),
}
// calculate slope and bias from now on,
// kept at zero after the unlocking time
if lockedOld.End > now && lockedOld.Amount.IsPositive() {
userPointOld.Slope = lockedOld.Amount.QuoRaw(types.MaxLockTime)
userPointOld.Bias = userPointOld.Slope.MulRaw(int64(lockedOld.End - now))
}
if lockedNew.End > now && lockedNew.Amount.IsPositive() {
// slope is always proportional to locked amount
userPointNew.Slope = lockedNew.Amount.QuoRaw(types.MaxLockTime)
// bias represents the voting power at the present:
// slope * (end - now)
// so that at any future time t, voting power will decay to:
// bias - slope * (t - now)
userPointNew.Bias = userPointNew.Slope.MulRaw(int64(lockedNew.End - now))
}
// regulate system checkpoint history
userSlopeChange := userPointNew.Slope.Sub(userPointOld.Slope)
userBiasChange := userPointNew.Bias.Sub(userPointOld.Bias)
k.regulateCheckpoint(ctx, userSlopeChange, userBiasChange)
slopeChangeOld := k.GetSlopeChange(ctx, lockedOld.End)
slopeChangeNew := k.GetSlopeChange(ctx, lockedNew.End)
// Schedule future slope changes at any unlocking time of any user's ve,
// since the slope should be reset to zero after unlocking time is up.
// Actually equivalent to, for the second segment of the piecewise linear function,
// the slope should be zero.
if lockedOld.End > now {
// old slope waw subtracted, so here add it back to cancel it
slopeChangeOld = slopeChangeOld.Add(userPointOld.Slope)
if lockedNew.End == lockedOld.End {
// subtract new slope
slopeChangeOld = slopeChangeOld.Sub(userPointNew.Slope)
}
k.SetSlopeChange(ctx, lockedOld.End, slopeChangeOld)
}
if lockedNew.End > now {
if lockedNew.End > lockedOld.End {
// subtract new slope, i.e., the slope is reset to zero
slopeChangeNew = slopeChangeNew.Sub(userPointNew.Slope)
k.SetSlopeChange(ctx, lockedNew.End, slopeChangeNew)
} else {
// has been handled in slopeChangeOld
}
}
// increase user epoch
userEpoch := k.GetUserEpoch(ctx, veID) + 1
k.SetUserEpoch(ctx, veID, userEpoch)
// set new user checkpoint
userPointNew.Timestamp = now
userPointNew.Block = ctx.BlockHeight()
k.SetUserCheckpoint(ctx, veID, userEpoch, userPointNew)
}
func (k Keeper) RegulateCheckpoint(ctx sdk.Context) {
now := uint64(ctx.BlockTime().Unix())
epoch := k.GetEpoch(ctx)
pointLast := k.GetCheckpoint(ctx, epoch)
if now-pointLast.Timestamp >= types.RegulatedPeriod {
k.regulateCheckpoint(ctx, sdk.ZeroInt(), sdk.ZeroInt())
}
}
func (k Keeper) regulateCheckpoint(ctx sdk.Context, userSlopeChange, userBiasChange sdk.Int) {
now := uint64(ctx.BlockTime().Unix())
epoch := k.GetEpoch(ctx)
pointLast := types.Checkpoint{
Bias: sdk.ZeroInt(),
Slope: sdk.ZeroInt(),
Timestamp: now,
Block: ctx.BlockHeight(),
}
if epoch > 0 {
pointLast = k.GetCheckpoint(ctx, epoch)
}
timeLast := pointLast.Timestamp
pointLastInitial := pointLast
blockSlope := sdk.ZeroDec()
if timeLast < now {
// block increasing slope, proportional to lapse of time
// here use Dec to maintain sufficient precision
blockSlope = sdk.NewDecFromInt(sdk.NewInt(ctx.BlockHeight() - pointLast.Block)).QuoInt64(int64(now - timeLast))
}
ti := types.RegulatedUnixTime(timeLast)
i := 0
for {
i++
if i > 2 {
// since checkpoint will always be regulated when past the regulated period at end block,
// this loop will not exceed two rounds
panic("broken checkpoint regulation")
}
ti = types.NextRegulatedUnixTime(ti)
var slopeChange sdk.Int
if ti > now {
// since ti is regulated, now must not be at regulated time
// so set slope change as zero
slopeChange = sdk.ZeroInt()
// up to the current time
ti = now
} else {
// ti is at regulated time
slopeChange = k.GetSlopeChange(ctx, ti)
}
// calculate new bias and slope
pointLast.Bias = pointLast.Bias.Sub(pointLast.Slope.MulRaw(int64(ti - timeLast)))
if pointLast.Bias.IsNegative() {
// can happen
pointLast.Bias = sdk.ZeroInt()
}
pointLast.Slope = pointLast.Slope.Add(slopeChange)
if pointLast.Slope.IsNegative() {
// cannot happen, just in case
pointLast.Slope = sdk.ZeroInt()
}
timeLast = ti
pointLast.Timestamp = ti
// calculate block approximately
pointLast.Block = pointLastInitial.Block + blockSlope.MulInt64(int64(ti-pointLastInitial.Timestamp)).TruncateInt64()
// increase epoch
epoch += 1
if ti == now {
pointLast.Block = ctx.BlockHeight()
break // break loop
} else {
// set new checkpoint
k.SetCheckpoint(ctx, epoch, pointLast)
}
}
// TODO: delete slope changes in the past, since they will be no longer used
// set new last epoch
k.SetEpoch(ctx, epoch)
// add new change at now to the new last point
if userBiasChange.IsPositive() {
pointLast.Bias = pointLast.Bias.Add(userBiasChange)
if pointLast.Bias.IsNegative() {
pointLast.Bias = sdk.ZeroInt()
}
}
if userSlopeChange.IsPositive() {
pointLast.Slope = pointLast.Slope.Add(userSlopeChange)
if pointLast.Slope.IsNegative() {
pointLast.Slope = sdk.ZeroInt()
}
}
// set new checkpoint
k.SetCheckpoint(ctx, epoch, pointLast)
}
func (k Keeper) SetEpoch(ctx sdk.Context, epoch uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(epoch)
store.Set(types.EpochKey(), bz)
}
func (k Keeper) GetEpoch(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.EpochKey())
if bz == nil {
return 0
}
return sdk.BigEndianToUint64(bz)
}
func (k Keeper) SetCheckpoint(ctx sdk.Context, epoch uint64, point types.Checkpoint) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&point)
store.Set(types.PointKey(epoch), bz)
}
func (k Keeper) GetCheckpoint(ctx sdk.Context, epoch uint64) types.Checkpoint {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.PointKey(epoch))
if bz == nil {
return types.Checkpoint{
Bias: sdk.ZeroInt(),
Slope: sdk.ZeroInt(),
}
}
var point types.Checkpoint
k.cdc.MustUnmarshal(bz, &point)
return point
}
func (k Keeper) SetUserEpoch(ctx sdk.Context, veID uint64, epoch uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(epoch)
store.Set(types.UserEpochKey(veID), bz)
}
func (k Keeper) GetUserEpoch(ctx sdk.Context, veID uint64) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.UserEpochKey(veID))
if bz == nil {
return 0
}
return sdk.BigEndianToUint64(bz)
}
func (k Keeper) SetUserCheckpoint(ctx sdk.Context, veID uint64, epoch uint64, point types.Checkpoint) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&point)
store.Set(types.UserPointKey(veID, epoch), bz)
}
func (k Keeper) GetUserCheckpoint(ctx sdk.Context, veID uint64, epoch uint64) types.Checkpoint {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.UserPointKey(veID, epoch))
if bz == nil {
return types.Checkpoint{
Bias: sdk.ZeroInt(),
Slope: sdk.ZeroInt(),
}
}
var point types.Checkpoint
k.cdc.MustUnmarshal(bz, &point)
return point
}
func (k Keeper) SetSlopeChange(ctx sdk.Context, timestamp uint64, slopeChange sdk.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: slopeChange})
store.Set(types.SlopeChangeKey(timestamp), bz)
}
func (k Keeper) GetSlopeChange(ctx sdk.Context, timestamp uint64) sdk.Int {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SlopeChangeKey(timestamp))
if bz == nil {
return sdk.ZeroInt()
}
var slopeChange sdk.IntProto
k.cdc.MustUnmarshal(bz, &slopeChange)
return slopeChange.Int
}