-
Notifications
You must be signed in to change notification settings - Fork 130
/
schedule.go
302 lines (265 loc) · 9.25 KB
/
schedule.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
)
// A "schedule" is an increasing step function of Coins over time. It's
// specified as an absolute start time and a sequence of relative periods, with
// each step at the end of a period. A schedule may also give the time and total
// value at the last step, which can speed evaluation of the step function after
// the last step.
//
// ReadSchedule returns the value of a schedule at readTime.
func ReadSchedule(
startTime, endTime int64,
periods sdkvesting.Periods,
totalCoins sdk.Coins,
readTime int64,
) sdk.Coins {
// return empty coins if the read time is before or equal start time
if readTime <= startTime {
return sdk.NewCoins()
}
// return the total coins when the read time is equal or after
// end time
if readTime >= endTime {
return totalCoins
}
coins := sdk.Coins{} // sum of amounts for events before readTime
elapsedTime := startTime
for _, period := range periods {
if readTime < elapsedTime+period.Length {
// we're reading before the next event
break
}
coins = coins.Add(period.Amount...)
elapsedTime += period.Length
}
return coins
}
// ReadPastPeriodCount returns the amount of passed periods before read time
func ReadPastPeriodCount(
startTime, endTime int64,
periods sdkvesting.Periods,
readTime int64,
) int {
passedPeriods := 0
// return 0 if the read time is before or equal start time
if readTime <= startTime {
return 0
}
// return all the periods when the read time is equal or after
// end time
if readTime >= endTime {
return len(periods)
}
elapsedTime := startTime
// for every period, add the period length to the elapsed time until
// the read time is before the next period
for _, period := range periods {
if readTime < elapsedTime+period.Length {
// we're reading before the next event
break
}
passedPeriods++
elapsedTime += period.Length
}
return passedPeriods
}
// DisjunctPeriods returns the union of two vesting period schedules. The
// returned schedule is the union of the vesting events, with simultaneous
// events combined into a single event. Input schedules P and Q are defined by
// their start times and periods. Returns new start time, new end time, and
// merged vesting events, relative to the new start time.
func DisjunctPeriods(
startTimePeriodsA, startTimePeriodsB int64,
periodsA, periodsB sdkvesting.Periods,
) (startTime, endTime int64, periods sdkvesting.Periods) {
timePeriodA := startTimePeriodsA // time of last merged periods A event, next p event is relative to this time
timePeriodsB := startTimePeriodsB // time of last merged periods B event, next periodsB event is relative to this time
idxPeriodsA := 0 // periods A indexes before this have been merged
idxPeriodsB := 0 // periods B indexes before this have been merged
lenPeriodsA := len(periodsA)
lenPeriodsB := len(periodsB)
startTime = Min64(startTimePeriodsA, startTimePeriodsB) // we pick the earlier time
endTime = startTime // time of last merged event, or the start time
periods = sdkvesting.Periods{} // merged periods
// emit adds an output period and updates the last event time
emit := func(nextTime int64, amount sdk.Coins) {
period := sdkvesting.Period{
Length: nextTime - endTime,
Amount: amount,
}
periods = append(periods, period)
endTime = nextTime
}
// consumeA emits the next period from A, updating indexes
consumeA := func(nextPeriodA int64) {
emit(nextPeriodA, periodsA[idxPeriodsA].Amount)
timePeriodA = nextPeriodA
idxPeriodsA++
}
// consumeB emits the next period from B, updating indexes
consumeB := func(nextPeriodB int64) {
emit(nextPeriodB, periodsB[idxPeriodsB].Amount)
timePeriodsB = nextPeriodB
idxPeriodsB++
}
// consumeBoth emits a merge of the next periods from periods A and B, updating indexes
consumeBoth := func(nextTime int64) {
emit(nextTime, periodsA[idxPeriodsA].Amount.Add(periodsB[idxPeriodsB].Amount...))
timePeriodA = nextTime
timePeriodsB = nextTime
idxPeriodsA++
idxPeriodsB++
}
// while there are more events in both schedules, handle the next one, merge
// if concurrent
for idxPeriodsA < lenPeriodsA && idxPeriodsB < lenPeriodsB {
nextPeriodA := timePeriodA + periodsA[idxPeriodsA].Length // next periodsA event in absolute time
nextPeriodB := timePeriodsB + periodsB[idxPeriodsB].Length // next periodsB event in absolute time
switch {
case nextPeriodA < nextPeriodB:
consumeA(nextPeriodA)
case nextPeriodA > nextPeriodB:
consumeB(nextPeriodB)
default:
consumeBoth(nextPeriodA)
}
}
// consume remaining events in schedule Periods A
for idxPeriodsA < lenPeriodsA {
nextPeriodA := timePeriodA + periodsA[idxPeriodsA].Length
consumeA(nextPeriodA)
}
// consume remaining events in schedule PeriodsB
for idxPeriodsB < lenPeriodsB {
nextPeriodB := timePeriodsB + periodsB[idxPeriodsB].Length
consumeB(nextPeriodB)
}
return startTime, endTime, periods
}
// ConjunctPeriods returns the combination of two period schedules where the
// result is the minimum of the two schedules.
// It returns the resulting periods start and end times as well as the resulting
// conjunction periods.
// TODO: rename and add comprehensive comments, this is currently not maintainable
func ConjunctPeriods(
startTimePeriodA, startTimePeriodB int64,
periodsA, periodsB sdkvesting.Periods,
) (startTime, endTime int64, conjunctionPeriods sdkvesting.Periods) {
timePeriodsA := startTimePeriodA
timePeriodsB := startTimePeriodB
idxPeriodsA := 0
idxPeriodsB := 0
lenPeriodsA := len(periodsA)
lenPeriodsB := len(periodsB)
startTime = Min64(startTimePeriodA, startTimePeriodB)
time := startTime
conjunctionPeriods = sdkvesting.Periods{}
amount := sdk.Coins{}
totalAmountPeriodsA := amount
totalAmountPeriodsB := amount
// emit adds an output period and updates the last event time
emit := func(nextTime int64, coins sdk.Coins) {
period := sdkvesting.Period{
Length: nextTime - time,
Amount: coins,
}
conjunctionPeriods = append(conjunctionPeriods, period)
time = nextTime
amount = amount.Add(coins...)
}
// consumeA processes the next event in P and emits an event
// if the minimum of P and Q changes
consumeA := func(nextTime int64) {
totalAmountPeriodsA = totalAmountPeriodsA.Add(periodsA[idxPeriodsA].Amount...)
min := totalAmountPeriodsA.Min(totalAmountPeriodsB)
if amount.IsAllLTE(min) {
diff := min.Sub(amount...)
if !diff.IsZero() {
emit(nextTime, diff)
}
}
timePeriodsA = nextTime
idxPeriodsA++
}
// consumeB processes the next event in Q and emits an event
// if the minimum of P and Q changes
consumeB := func(nextTime int64) {
totalAmountPeriodsB = totalAmountPeriodsB.Add(periodsB[idxPeriodsB].Amount...)
min := totalAmountPeriodsA.Min(totalAmountPeriodsB)
if amount.IsAllLTE(min) {
diff := min.Sub(amount...)
if !diff.IsZero() {
emit(nextTime, diff)
}
}
timePeriodsB = nextTime
idxPeriodsB++
}
// consumeBoth processes simultaneous events in P and Q and emits an
// event if the minimum of P and Q changes
consumeBoth := func(nextTime int64) {
totalAmountPeriodsA = totalAmountPeriodsA.Add(periodsA[idxPeriodsA].Amount...)
totalAmountPeriodsB = totalAmountPeriodsB.Add(periodsB[idxPeriodsB].Amount...)
min := totalAmountPeriodsA.Min(totalAmountPeriodsB)
if amount.IsAllLTE(min) {
diff := min.Sub(amount...)
if !diff.IsZero() {
emit(nextTime, diff)
}
}
timePeriodsA = nextTime
timePeriodsB = nextTime
idxPeriodsA++
idxPeriodsB++
}
// while there are events left in both schedules, process the next one
for idxPeriodsA < lenPeriodsA && idxPeriodsB < lenPeriodsB {
nextPeriodA := timePeriodsA + periodsA[idxPeriodsA].Length // next periods A event in absolute time
nextPeriodB := timePeriodsB + periodsB[idxPeriodsB].Length // next periods B event in absolute time
switch {
case nextPeriodA < nextPeriodB:
consumeA(nextPeriodA)
case nextPeriodA > nextPeriodB:
consumeB(nextPeriodB)
default:
consumeBoth(nextPeriodA)
}
}
// consume remaining events in schedule P
for idxPeriodsA < lenPeriodsA {
nextPeriodA := timePeriodsA + periodsA[idxPeriodsA].Length
consumeA(nextPeriodA)
}
// consume remaining events in schedule Q
for idxPeriodsB < lenPeriodsB {
nextPeriodB := timePeriodsB + periodsB[idxPeriodsB].Length
consumeB(nextPeriodB)
}
endTime = time
return startTime, endTime, conjunctionPeriods
}
// AlignSchedules extends the first period's length to align the two given periods
// to the same start time. The earliest start time is chosen.
// It returns the aligned new start and end times of the periods.
func AlignSchedules(
startTimePeriodA,
startTimePeriodB int64,
periodsA, periodsB sdkvesting.Periods,
) (startTime, endTime int64) {
startTime = Min64(startTimePeriodA, startTimePeriodB)
// add the difference time between the two periods
if len(periodsA) > 0 {
periodsA[0].Length += startTimePeriodA - startTime
}
if len(periodsB) > 0 {
periodsB[0].Length += startTimePeriodB - startTime
}
endPeriodsA := startTime + periodsA.TotalLength()
endPeriodsB := startTime + periodsB.TotalLength()
// the end time between the 2 periods is the max length
endTime = Max64(endPeriodsA, endPeriodsB)
return startTime, endTime
}