-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathreset.rs
627 lines (584 loc) · 20.7 KB
/
reset.rs
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use ore_api::prelude::*;
use steel::*;
/// Reset tops up the bus balances and updates the emissions and reward rates.
pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
// Load accounts.
let [signer_info, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, config_info, mint_info, treasury_info, treasury_tokens_info, token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
let bus_0 = bus_0_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 0)?;
let bus_1 = bus_1_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 1)?;
let bus_2 = bus_2_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 2)?;
let bus_3 = bus_3_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 3)?;
let bus_4 = bus_4_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 4)?;
let bus_5 = bus_5_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 5)?;
let bus_6 = bus_6_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 6)?;
let bus_7 = bus_7_info
.as_account_mut::<Bus>(&ore_api::ID)?
.assert_mut(|b| b.id == 7)?;
let config = config_info
.is_config()?
.as_account_mut::<Config>(&ore_api::ID)?;
let mint = mint_info
.has_address(&MINT_ADDRESS)?
.is_writable()?
.as_mint()?;
treasury_info.is_treasury()?.is_writable()?;
treasury_tokens_info.is_treasury_tokens()?.is_writable()?;
token_program.is_program(&spl_token::ID)?;
// Validate enough time has passed since the last reset.
let clock = Clock::get()?;
if config
.last_reset_at
.saturating_add(EPOCH_DURATION)
.gt(&clock.unix_timestamp)
{
return Ok(());
}
// Process epoch.
let busses = [bus_0, bus_1, bus_2, bus_3, bus_4, bus_5, bus_6, bus_7];
let amount_to_mint = config.process_epoch(busses, &clock, &mint)?;
// Fund the treasury token account.
mint_to_signed(
mint_info,
treasury_tokens_info,
treasury_info,
token_program,
amount_to_mint,
&[TREASURY],
)?;
Ok(())
}
trait EpochProcessor {
fn process_epoch(
&mut self,
busses: [&mut Bus; 8],
clock: &Clock,
mint: &Mint,
) -> Result<u64, ProgramError>;
}
impl EpochProcessor for Config {
fn process_epoch(
&mut self,
busses: [&mut Bus; 8],
clock: &Clock,
mint: &Mint,
) -> Result<u64, ProgramError> {
// Max supply check.
if mint.supply() >= MAX_SUPPLY {
return Err(OreError::MaxSupply.into());
}
// Update timestamp.
self.last_reset_at = clock.unix_timestamp;
// Adjust emissions curve based on current supply.
self.target_emmissions_rate = get_target_emissions_rate(mint.supply());
// Calculate target rewards to distribute in coming epoch (emissions rate multiplied by epoch duration).
let target_epoch_rewards = self.target_emmissions_rate * EPOCH_MINUTES as u64;
// Reset bus counters and calculate theoretical rewards mined in the last epoch.
let mut amount_to_mint = 0u64;
let mut remaining_supply = MAX_SUPPLY.saturating_sub(mint.supply());
let mut theoretical_epoch_rewards = 0u64;
for bus in busses {
// Reset theoretical rewards.
theoretical_epoch_rewards += bus.theoretical_rewards;
bus.theoretical_rewards = 0;
// Reset bus rewards.
let topup_amount = target_epoch_rewards
.saturating_sub(bus.rewards)
.min(remaining_supply);
remaining_supply -= topup_amount;
amount_to_mint += topup_amount;
bus.rewards += topup_amount;
}
// Update base reward rate for next epoch.
self.base_reward_rate = calculate_new_reward_rate(
self.base_reward_rate,
theoretical_epoch_rewards,
target_epoch_rewards,
);
// If base reward rate is too low, increment min difficulty by 1 and double base reward rate.
if self.base_reward_rate < BASE_REWARD_RATE_MIN_THRESHOLD {
self.min_difficulty += 1;
self.base_reward_rate *= 2;
}
// If base reward rate is too high, decrement min difficulty by 1 and halve base reward rate.
if self.base_reward_rate >= BASE_REWARD_RATE_MAX_THRESHOLD && self.min_difficulty > 1 {
self.min_difficulty -= 1;
self.base_reward_rate /= 2;
}
Ok(amount_to_mint)
}
}
/// This function calculates what the new reward rate should be based on how many total rewards
/// were mined in the prior epoch. The math is largely identitical to function used by the Bitcoin
/// network to update the difficulty between each epoch.
///
/// new_rate = current_rate * (target_rewards / actual_rewards)
///
/// The new rate is then smoothed by a constant factor to avoid large fluctuations. In Ore's case,
/// the epochs are short (60 seconds) so a smoothing factor of 2 has been chosen. That is, the reward rate
/// can at most double or halve from one epoch to the next.
pub(crate) fn calculate_new_reward_rate(
current_rate: u64,
epoch_rewards: u64,
target_epoch_rewards: u64,
) -> u64 {
// Avoid division by zero. Leave the reward rate unchanged, if detected.
if epoch_rewards.eq(&0) {
return current_rate;
}
// Calculate new reward rate.
let new_rate = (current_rate as u128)
.saturating_mul(target_epoch_rewards as u128)
.saturating_div(epoch_rewards as u128) as u64;
// Smooth reward rate so it cannot change by more than a constant factor from one epoch to the next.
let new_rate_min = current_rate.saturating_div(SMOOTHING_FACTOR);
let new_rate_max = current_rate.saturating_mul(SMOOTHING_FACTOR);
let new_rate_smoothed = new_rate.min(new_rate_max).max(new_rate_min);
// Prevent reward rate from dropping below 1 or exceeding BUS_EPOCH_REWARDS and return.
new_rate_smoothed.max(1).min(target_epoch_rewards)
}
/// This function calculates the target emissions rate (ORE / min) based on the current supply.
/// It is designed to reduce emissions by 10% approximately every 12 months with a hardcap at 5 million ORE.
pub(crate) fn get_target_emissions_rate(current_supply: u64) -> u64 {
match current_supply {
n if n < ONE_ORE * 525_600 => 100_000_000_000, // Year ~1
n if n < ONE_ORE * 998_640 => 90_000_000_000, // Year ~2
n if n < ONE_ORE * 1_424_376 => 81_000_000_000, // Year ~3
n if n < ONE_ORE * 1_807_538 => 72_900_000_000, // Year ~4
n if n < ONE_ORE * 2_152_384 => 65_610_000_000, // Year ~5
n if n < ONE_ORE * 2_462_746 => 59_049_000_000, // Year ~6
n if n < ONE_ORE * 2_742_071 => 53_144_100_000, // Year ~7
n if n < ONE_ORE * 2_993_464 => 47_829_690_000, // Year ~8
n if n < ONE_ORE * 3_219_717 => 43_046_721_000, // Year ~9
n if n < ONE_ORE * 3_423_346 => 38_742_048_900, // Year ~10
n if n < ONE_ORE * 3_606_611 => 34_867_844_010, // Year ~11
n if n < ONE_ORE * 3_771_550 => 31_381_059_609, // Year ~12
n if n < ONE_ORE * 3_919_995 => 28_242_953_648, // Year ~13
n if n < ONE_ORE * 4_053_595 => 25_418_658_283, // Year ~14
n if n < ONE_ORE * 4_173_836 => 22_876_792_454, // Year ~15
n if n < ONE_ORE * 4_282_052 => 20_589_113_208, // Year ~16
n if n < ONE_ORE * 4_379_447 => 18_530_201_887, // Year ~17
n if n < ONE_ORE * 4_467_102 => 16_677_181_698, // Year ~18
n if n < ONE_ORE * 4_545_992 => 15_009_463_528, // Year ~19
n if n < ONE_ORE * 4_616_993 => 13_508_517_175, // Year ~20
n if n < ONE_ORE * 4_680_893 => 12_157_665_457, // Year ~21
n if n < ONE_ORE * 4_738_404 => 10_941_898_911, // Year ~22
n if n < ONE_ORE * 4_790_164 => 9_847_709_019, // Year ~23
n if n < ONE_ORE * 4_836_747 => 8_862_938_117, // Year ~24
n if n < ONE_ORE * 4_878_672 => 7_976_644_305, // Year ~25
n if n < ONE_ORE * 4_916_405 => 7_178_979_874, // Year ~26
n if n < ONE_ORE * 4_950_365 => 6_461_081_886, // Year ~27
n if n < ONE_ORE * 4_980_928 => 5_814_973_607, // Year ~28
n if n < ONE_ORE * 5_000_000 => 5_233_476_327, // Year ~29
_ => 0,
}
}
#[cfg(test)]
mod tests {
use rand::{distributions::Uniform, Rng};
use solana_program::program_option::COption;
use steel::{Clock, Mint};
use crate::{calculate_new_reward_rate, reset::EpochProcessor};
use ore_api::{
consts::{
BASE_REWARD_RATE_MIN_THRESHOLD, BUS_COUNT, EPOCH_MINUTES, ONE_ORE, SMOOTHING_FACTOR,
TOKEN_DECIMALS,
},
state::{Bus, Config},
};
const FUZZ_SIZE: u64 = 10_000;
const TARGET_EPOCH_REWARDS: u64 = ONE_ORE * EPOCH_MINUTES as u64;
const MAX_EPOCH_REWARDS: u64 = TARGET_EPOCH_REWARDS * BUS_COUNT as u64;
#[test]
fn test_calculate_new_reward_rate_target() {
let current_rate = 1000;
let new_rate =
calculate_new_reward_rate(current_rate, TARGET_EPOCH_REWARDS, TARGET_EPOCH_REWARDS);
assert!(new_rate.eq(¤t_rate));
}
#[test]
fn test_calculate_new_reward_rate_div_by_zero() {
let current_rate = 1000;
let new_rate = calculate_new_reward_rate(current_rate, 0, TARGET_EPOCH_REWARDS);
assert!(new_rate.eq(¤t_rate));
}
#[test]
fn test_calculate_new_reward_rate_lower() {
let current_rate = 1000;
let new_rate = calculate_new_reward_rate(
current_rate,
TARGET_EPOCH_REWARDS.saturating_add(10_000_000_000),
TARGET_EPOCH_REWARDS,
);
assert!(new_rate.lt(¤t_rate));
}
#[test]
fn test_calculate_new_reward_rate_lower_edge() {
let current_rate = BASE_REWARD_RATE_MIN_THRESHOLD;
let new_rate =
calculate_new_reward_rate(current_rate, TARGET_EPOCH_REWARDS + 1, TARGET_EPOCH_REWARDS);
assert!(new_rate.lt(¤t_rate));
}
#[test]
fn test_calculate_new_reward_rate_lower_fuzz() {
let mut rng = rand::thread_rng();
for _ in 0..FUZZ_SIZE {
let current_rate: u64 = rng.sample(Uniform::new(1, TARGET_EPOCH_REWARDS));
let actual_rewards: u64 =
rng.sample(Uniform::new(TARGET_EPOCH_REWARDS, MAX_EPOCH_REWARDS));
let new_rate =
calculate_new_reward_rate(current_rate, actual_rewards, TARGET_EPOCH_REWARDS);
assert!(new_rate.lt(¤t_rate));
}
}
#[test]
fn test_calculate_new_reward_rate_higher() {
let current_rate = 1000;
let new_rate = calculate_new_reward_rate(
current_rate,
TARGET_EPOCH_REWARDS.saturating_sub(10_000_000_000),
TARGET_EPOCH_REWARDS,
);
assert!(new_rate.gt(¤t_rate));
}
#[test]
fn test_calculate_new_reward_rate_higher_fuzz() {
let mut rng = rand::thread_rng();
for _ in 0..FUZZ_SIZE {
let current_rate: u64 = rng.sample(Uniform::new(1, TARGET_EPOCH_REWARDS));
let actual_rewards: u64 = rng.sample(Uniform::new(1, TARGET_EPOCH_REWARDS));
let new_rate =
calculate_new_reward_rate(current_rate, actual_rewards, TARGET_EPOCH_REWARDS);
assert!(new_rate.gt(¤t_rate));
}
}
#[test]
fn test_calculate_new_reward_rate_max_smooth() {
let current_rate = 1000;
let new_rate = calculate_new_reward_rate(current_rate, 1, TARGET_EPOCH_REWARDS);
assert!(new_rate.eq(¤t_rate.saturating_mul(SMOOTHING_FACTOR)));
}
#[test]
fn test_calculate_new_reward_rate_min_smooth() {
let current_rate = 1000;
let new_rate = calculate_new_reward_rate(current_rate, u64::MAX, TARGET_EPOCH_REWARDS);
assert!(new_rate.eq(¤t_rate.saturating_div(SMOOTHING_FACTOR)));
}
#[test]
fn test_calculate_new_reward_rate_max_inputs() {
let new_rate = calculate_new_reward_rate(
TARGET_EPOCH_REWARDS,
MAX_EPOCH_REWARDS,
TARGET_EPOCH_REWARDS,
);
assert!(new_rate.eq(&TARGET_EPOCH_REWARDS.saturating_div(SMOOTHING_FACTOR)));
}
#[test]
fn test_calculate_new_reward_rate_min_inputs() {
let new_rate = calculate_new_reward_rate(1, 1, TARGET_EPOCH_REWARDS);
assert!(new_rate.eq(&1u64.saturating_mul(SMOOTHING_FACTOR)));
}
#[allow(deprecated)]
#[test]
fn test_process_epoch_simple() {
let mut config = Config {
base_reward_rate: 1024,
last_reset_at: 0,
min_difficulty: 1,
target_emmissions_rate: ONE_ORE,
};
let bus_0 = &mut Bus {
id: 0,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_1 = &mut Bus {
id: 1,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_2 = &mut Bus {
id: 2,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_3 = &mut Bus {
id: 3,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_4 = &mut Bus {
id: 4,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_5 = &mut Bus {
id: 5,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_6 = &mut Bus {
id: 6,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_7 = &mut Bus {
id: 7,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let busses = [bus_0, bus_1, bus_2, bus_3, bus_4, bus_5, bus_6, bus_7];
let clock = Clock::default();
let mint = Mint::V0(spl_token::state::Mint {
mint_authority: COption::None,
supply: ONE_ORE * 100,
decimals: TOKEN_DECIMALS,
is_initialized: true,
freeze_authority: COption::None,
});
let amount_to_mint = config.process_epoch(busses, &clock, &mint).unwrap();
assert_eq!(config.target_emmissions_rate, ONE_ORE);
assert_eq!(
ONE_ORE * EPOCH_MINUTES as u64 * BUS_COUNT as u64,
amount_to_mint
);
}
#[allow(deprecated)]
#[test]
fn test_process_epoch_emissions_boundary() {
let mut config = Config {
base_reward_rate: 1024,
last_reset_at: 0,
min_difficulty: 1,
target_emmissions_rate: ONE_ORE,
};
let bus_0 = &mut Bus {
id: 0,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_1 = &mut Bus {
id: 1,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_2 = &mut Bus {
id: 2,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_3 = &mut Bus {
id: 3,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_4 = &mut Bus {
id: 4,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_5 = &mut Bus {
id: 5,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_6 = &mut Bus {
id: 6,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_7 = &mut Bus {
id: 7,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let busses = [bus_0, bus_1, bus_2, bus_3, bus_4, bus_5, bus_6, bus_7];
let clock = Clock::default();
let mint = Mint::V0(spl_token::state::Mint {
mint_authority: COption::None,
supply: ONE_ORE * 525_600,
decimals: TOKEN_DECIMALS,
is_initialized: true,
freeze_authority: COption::None,
});
let amount_to_mint = config.process_epoch(busses, &clock, &mint).unwrap();
assert_eq!(config.target_emmissions_rate, 90_000_000_000);
assert_eq!(
90_000_000_000 * EPOCH_MINUTES as u64 * BUS_COUNT as u64,
amount_to_mint
);
}
#[allow(deprecated)]
#[test]
fn test_process_epoch_max_supply() {
let mut config = Config {
base_reward_rate: 1024,
last_reset_at: 0,
min_difficulty: 1,
target_emmissions_rate: 5_233_476_327,
};
let bus_0 = &mut Bus {
id: 0,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_1 = &mut Bus {
id: 1,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_2 = &mut Bus {
id: 2,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_3 = &mut Bus {
id: 3,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_4 = &mut Bus {
id: 4,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_5 = &mut Bus {
id: 5,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_6 = &mut Bus {
id: 6,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_7 = &mut Bus {
id: 7,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let busses = [bus_0, bus_1, bus_2, bus_3, bus_4, bus_5, bus_6, bus_7];
let clock = Clock::default();
let mint = Mint::V0(spl_token::state::Mint {
mint_authority: COption::None,
supply: ONE_ORE * 4_999_999,
decimals: TOKEN_DECIMALS,
is_initialized: true,
freeze_authority: COption::None,
});
let amount_to_mint = config.process_epoch(busses, &clock, &mint).unwrap();
assert_eq!(config.target_emmissions_rate, 5_233_476_327);
assert_eq!(ONE_ORE, amount_to_mint);
}
#[allow(deprecated)]
#[test]
fn test_process_epoch_zero_emissions() {
let mut config = Config {
base_reward_rate: 1024,
last_reset_at: 0,
min_difficulty: 1,
target_emmissions_rate: 5_233_476_327,
};
let bus_0 = &mut Bus {
id: 0,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_1 = &mut Bus {
id: 1,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_2 = &mut Bus {
id: 2,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_3 = &mut Bus {
id: 3,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_4 = &mut Bus {
id: 4,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_5 = &mut Bus {
id: 5,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_6 = &mut Bus {
id: 6,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let bus_7 = &mut Bus {
id: 7,
rewards: 0,
theoretical_rewards: 0,
top_balance: 0,
};
let busses = [bus_0, bus_1, bus_2, bus_3, bus_4, bus_5, bus_6, bus_7];
let clock = Clock::default();
let mint = Mint::V0(spl_token::state::Mint {
mint_authority: COption::None,
supply: ONE_ORE * 5_000_000,
decimals: TOKEN_DECIMALS,
is_initialized: true,
freeze_authority: COption::None,
});
let amount_to_mint = config.process_epoch(busses, &clock, &mint);
assert!(amount_to_mint.is_err());
}
}