Skip to content

Commit 26d7aaf

Browse files
committed
refactor: add index method for StacksEpochId
This allows us to cleanup some magic numbers when accessing epochs in a list and make the expected behavior more clear to the reader.
1 parent ca84a1b commit 26d7aaf

File tree

13 files changed

+177
-170
lines changed

13 files changed

+177
-170
lines changed

stacks-common/src/types/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ impl StacksEpochId {
235235
StacksEpochId::Epoch30 => cur_reward_cycle > first_epoch30_reward_cycle,
236236
}
237237
}
238+
239+
/// Return the index for this epoch in the list of epochs
240+
pub fn index(&self) -> usize {
241+
match self {
242+
StacksEpochId::Epoch10 => 0,
243+
StacksEpochId::Epoch20 => 1,
244+
StacksEpochId::Epoch2_05 => 2,
245+
StacksEpochId::Epoch21 => 3,
246+
StacksEpochId::Epoch22 => 4,
247+
StacksEpochId::Epoch23 => 5,
248+
StacksEpochId::Epoch24 => 6,
249+
StacksEpochId::Epoch25 => 7,
250+
StacksEpochId::Epoch30 => 8,
251+
}
252+
}
238253
}
239254

240255
impl std::fmt::Display for StacksEpochId {

testnet/stacks-node/src/config.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,7 @@ impl Config {
531531
self.burnchain.get_bitcoin_network().1,
532532
self.burnchain.epochs.as_ref(),
533533
);
534-
let Some(epoch_30) = StacksEpoch::find_epoch_by_id(&epochs, StacksEpochId::Epoch30)
535-
.map(|epoch_ix| epochs[epoch_ix].clone())
536-
else {
534+
let Some(epoch_30) = epochs.get(StacksEpochId::Epoch30.index()) else {
537535
// no Epoch 3.0, so just return
538536
return;
539537
};
@@ -610,9 +608,7 @@ impl Config {
610608
let _ = StacksEpoch::validate_epochs(epochs);
611609

612610
// sanity check: v1_unlock_height must happen after pox-2 instantiation
613-
let epoch21_index = StacksEpoch::find_epoch_by_id(&epochs, StacksEpochId::Epoch21)
614-
.expect("FATAL: no epoch 2.1 defined");
615-
611+
let epoch21_index = StacksEpochId::Epoch21.index();
616612
let epoch21 = &epochs[epoch21_index];
617613
let v1_unlock_height = burnchain.pox_constants.v1_unlock_height as u64;
618614

@@ -714,7 +710,7 @@ impl Config {
714710
}
715711

716712
// Stacks 1.0 must start at 0
717-
if matched_epochs[0].1 != 0 {
713+
if matched_epochs[StacksEpochId::Epoch10.index()].1 != 0 {
718714
return Err("Stacks 1.0 must start at height = 0".into());
719715
}
720716

testnet/stacks-node/src/run_loop/boot_nakamoto.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ impl BootRunLoop {
237237
config.burnchain.get_bitcoin_network().1,
238238
config.burnchain.epochs.as_ref(),
239239
);
240-
let epoch_3 = &epochs[StacksEpoch::find_epoch_by_id(&epochs, StacksEpochId::Epoch30)
241-
.ok_or("No Epoch-3.0 defined")?];
240+
let epoch_3 = epochs
241+
.get(StacksEpochId::Epoch30.index())
242+
.ok_or("No Epoch-3.0 defined")?;
242243

243244
Ok(u64::from(burn_height) >= epoch_3.start_height - 1)
244245
}

testnet/stacks-node/src/tests/epoch_205.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ fn test_exact_block_costs() {
5757

5858
let (mut conf, _miner_account) = neon_integration_test_conf();
5959
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
60-
epochs[1].end_height = epoch_205_transition_height;
61-
epochs[2].start_height = epoch_205_transition_height;
60+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_205_transition_height;
61+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_205_transition_height;
6262

6363
conf.burnchain.epochs = Some(epochs);
6464
conf.node.mine_microblocks = true;
@@ -305,8 +305,8 @@ fn test_dynamic_db_method_costs() {
305305

306306
let (mut conf, _miner_account) = neon_integration_test_conf();
307307
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
308-
epochs[1].end_height = epoch_205_transition_height;
309-
epochs[2].start_height = epoch_205_transition_height;
308+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_205_transition_height;
309+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_205_transition_height;
310310

311311
conf.burnchain.epochs = Some(epochs);
312312

@@ -508,8 +508,8 @@ fn transition_empty_blocks() {
508508
let (mut conf, miner_account) = neon_integration_test_conf();
509509

510510
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
511-
epochs[1].end_height = epoch_2_05;
512-
epochs[2].start_height = epoch_2_05;
511+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
512+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
513513

514514
conf.burnchain.epochs = Some(epochs);
515515

testnet/stacks-node/src/tests/epoch_21.rs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ fn advance_to_2_1(
7474
test_observer::register_any(&mut conf);
7575

7676
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
77-
epochs[1].end_height = epoch_2_05;
78-
epochs[2].start_height = epoch_2_05;
79-
epochs[2].end_height = epoch_2_1;
80-
epochs[3].start_height = epoch_2_1;
77+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
78+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
79+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
80+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
8181

8282
conf.burnchain.epochs = Some(epochs);
8383

@@ -577,10 +577,10 @@ fn transition_fixes_bitcoin_rigidity() {
577577
test_observer::register_any(&mut conf);
578578

579579
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
580-
epochs[1].end_height = epoch_2_05;
581-
epochs[2].start_height = epoch_2_05;
582-
epochs[2].end_height = epoch_2_1;
583-
epochs[3].start_height = epoch_2_1;
580+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
581+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
582+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
583+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
584584

585585
conf.burnchain.epochs = Some(epochs);
586586

@@ -1497,10 +1497,10 @@ fn transition_removes_pox_sunset() {
14971497
let epoch_21 = epoch_21_rc * reward_cycle_len + 1;
14981498

14991499
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
1500-
epochs[1].end_height = 1;
1501-
epochs[2].start_height = 1;
1502-
epochs[2].end_height = epoch_21;
1503-
epochs[3].start_height = epoch_21;
1500+
epochs[StacksEpochId::Epoch20.index()].end_height = 1;
1501+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 1;
1502+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_21;
1503+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_21;
15041504

15051505
conf.burnchain.epochs = Some(epochs);
15061506

@@ -1770,10 +1770,10 @@ fn transition_empty_blocks() {
17701770
let (mut conf, miner_account) = neon_integration_test_conf();
17711771

17721772
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
1773-
epochs[1].end_height = epoch_2_05;
1774-
epochs[2].start_height = epoch_2_05;
1775-
epochs[2].end_height = epoch_2_1;
1776-
epochs[3].start_height = epoch_2_1;
1773+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
1774+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
1775+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
1776+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
17771777

17781778
conf.node.mine_microblocks = false;
17791779
conf.burnchain.max_rbf = 1000000;
@@ -2051,10 +2051,10 @@ fn test_pox_reorgs_three_flaps() {
20512051

20522052
// make epoch 2.1 start in the middle of boot-up
20532053
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
2054-
epochs[1].end_height = 101;
2055-
epochs[2].start_height = 101;
2056-
epochs[2].end_height = 151;
2057-
epochs[3].start_height = 151;
2054+
epochs[StacksEpochId::Epoch20.index()].end_height = 101;
2055+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 101;
2056+
epochs[StacksEpochId::Epoch2_05.index()].end_height = 151;
2057+
epochs[StacksEpochId::Epoch21.index()].start_height = 151;
20582058
conf_template.burnchain.epochs = Some(epochs);
20592059

20602060
let privks: Vec<_> = (0..5)
@@ -2593,10 +2593,10 @@ fn test_pox_reorg_one_flap() {
25932593

25942594
// make epoch 2.1 start in the middle of boot-up
25952595
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
2596-
epochs[1].end_height = 101;
2597-
epochs[2].start_height = 101;
2598-
epochs[2].end_height = 151;
2599-
epochs[3].start_height = 151;
2596+
epochs[StacksEpochId::Epoch20.index()].end_height = 101;
2597+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 101;
2598+
epochs[StacksEpochId::Epoch2_05.index()].end_height = 151;
2599+
epochs[StacksEpochId::Epoch21.index()].start_height = 151;
26002600
conf_template.burnchain.epochs = Some(epochs);
26012601

26022602
let privks: Vec<_> = (0..5)
@@ -3019,10 +3019,10 @@ fn test_pox_reorg_flap_duel() {
30193019

30203020
// make epoch 2.1 start in the middle of boot-up
30213021
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
3022-
epochs[1].end_height = 101;
3023-
epochs[2].start_height = 101;
3024-
epochs[2].end_height = 151;
3025-
epochs[3].start_height = 151;
3022+
epochs[StacksEpochId::Epoch20.index()].end_height = 101;
3023+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 101;
3024+
epochs[StacksEpochId::Epoch2_05.index()].end_height = 151;
3025+
epochs[StacksEpochId::Epoch21.index()].start_height = 151;
30263026
conf_template.burnchain.epochs = Some(epochs);
30273027

30283028
let privks: Vec<_> = (0..5)
@@ -3459,10 +3459,10 @@ fn test_pox_reorg_flap_reward_cycles() {
34593459

34603460
// make epoch 2.1 start in the middle of boot-up
34613461
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
3462-
epochs[1].end_height = 101;
3463-
epochs[2].start_height = 101;
3464-
epochs[2].end_height = 151;
3465-
epochs[3].start_height = 151;
3462+
epochs[StacksEpochId::Epoch20.index()].end_height = 101;
3463+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 101;
3464+
epochs[StacksEpochId::Epoch2_05.index()].end_height = 151;
3465+
epochs[StacksEpochId::Epoch21.index()].start_height = 151;
34663466
conf_template.burnchain.epochs = Some(epochs);
34673467

34683468
let privks: Vec<_> = (0..5)
@@ -3891,10 +3891,10 @@ fn test_pox_missing_five_anchor_blocks() {
38913891

38923892
// make epoch 2.1 start in the middle of boot-up
38933893
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
3894-
epochs[1].end_height = 101;
3895-
epochs[2].start_height = 101;
3896-
epochs[2].end_height = 151;
3897-
epochs[3].start_height = 151;
3894+
epochs[StacksEpochId::Epoch20.index()].end_height = 101;
3895+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 101;
3896+
epochs[StacksEpochId::Epoch2_05.index()].end_height = 151;
3897+
epochs[StacksEpochId::Epoch21.index()].start_height = 151;
38983898
conf_template.burnchain.epochs = Some(epochs);
38993899

39003900
let privks: Vec<_> = (0..5)
@@ -4291,10 +4291,10 @@ fn test_sortition_divergence_pre_21() {
42914291

42924292
// make epoch 2.1 start after we have created this error condition
42934293
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
4294-
epochs[1].end_height = 101;
4295-
epochs[2].start_height = 101;
4296-
epochs[2].end_height = 241;
4297-
epochs[3].start_height = 241;
4294+
epochs[StacksEpochId::Epoch20.index()].end_height = 101;
4295+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 101;
4296+
epochs[StacksEpochId::Epoch2_05.index()].end_height = 241;
4297+
epochs[StacksEpochId::Epoch21.index()].start_height = 241;
42984298
conf_template.burnchain.epochs = Some(epochs);
42994299

43004300
let privks: Vec<_> = (0..5)
@@ -4750,10 +4750,10 @@ fn trait_invocation_cross_epoch() {
47504750
conf.initial_balances.append(&mut initial_balances);
47514751
test_observer::register_any(&mut conf);
47524752
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
4753-
epochs[1].end_height = epoch_2_05;
4754-
epochs[2].start_height = epoch_2_05;
4755-
epochs[2].end_height = epoch_2_1;
4756-
epochs[3].start_height = epoch_2_1;
4753+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
4754+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
4755+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
4756+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
47574757
conf.burnchain.epochs = Some(epochs);
47584758

47594759
let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());
@@ -5024,10 +5024,10 @@ fn test_v1_unlock_height_with_current_stackers() {
50245024
conf.initial_balances.append(&mut initial_balances);
50255025

50265026
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
5027-
epochs[1].end_height = epoch_2_05;
5028-
epochs[2].start_height = epoch_2_05;
5029-
epochs[2].end_height = epoch_2_1;
5030-
epochs[3].start_height = epoch_2_1;
5027+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
5028+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
5029+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
5030+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
50315031
conf.burnchain.epochs = Some(epochs);
50325032

50335033
let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());
@@ -5287,10 +5287,10 @@ fn test_v1_unlock_height_with_delay_and_current_stackers() {
52875287
conf.initial_balances.append(&mut initial_balances);
52885288

52895289
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
5290-
epochs[1].end_height = epoch_2_05;
5291-
epochs[2].start_height = epoch_2_05;
5292-
epochs[2].end_height = epoch_2_1;
5293-
epochs[3].start_height = epoch_2_1;
5290+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
5291+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
5292+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
5293+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
52945294
conf.burnchain.epochs = Some(epochs);
52955295

52965296
let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());

testnet/stacks-node/src/tests/epoch_22.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ fn disable_pox() {
138138
conf.initial_balances.append(&mut initial_balances);
139139

140140
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
141-
epochs[1].end_height = epoch_2_05;
142-
epochs[2].start_height = epoch_2_05;
143-
epochs[2].end_height = epoch_2_1;
144-
epochs[3].start_height = epoch_2_1;
145-
epochs[3].end_height = epoch_2_2;
146-
epochs[4].start_height = epoch_2_2;
147-
epochs[4].end_height = STACKS_EPOCH_MAX;
148-
epochs.truncate(5);
141+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
142+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
143+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
144+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
145+
epochs[StacksEpochId::Epoch21.index()].end_height = epoch_2_2;
146+
epochs[StacksEpochId::Epoch22.index()].start_height = epoch_2_2;
147+
epochs[StacksEpochId::Epoch22.index()].end_height = STACKS_EPOCH_MAX;
148+
epochs.truncate(StacksEpochId::Epoch22.index() + 1);
149149
conf.burnchain.epochs = Some(epochs);
150150

151151
let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());
@@ -677,14 +677,14 @@ fn pox_2_unlock_all() {
677677
conf.initial_balances.append(&mut initial_balances);
678678

679679
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
680-
epochs[1].end_height = epoch_2_05;
681-
epochs[2].start_height = epoch_2_05;
682-
epochs[2].end_height = epoch_2_1;
683-
epochs[3].start_height = epoch_2_1;
684-
epochs[3].end_height = epoch_2_2;
685-
epochs[4].start_height = epoch_2_2;
686-
epochs[4].end_height = STACKS_EPOCH_MAX;
687-
epochs.truncate(5);
680+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
681+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
682+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
683+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
684+
epochs[StacksEpochId::Epoch21.index()].end_height = epoch_2_2;
685+
epochs[StacksEpochId::Epoch22.index()].start_height = epoch_2_2;
686+
epochs[StacksEpochId::Epoch22.index()].end_height = STACKS_EPOCH_MAX;
687+
epochs.truncate(StacksEpochId::Epoch22.index() + 1);
688688
conf.burnchain.epochs = Some(epochs);
689689

690690
let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());
@@ -1293,14 +1293,14 @@ fn test_pox_reorg_one_flap() {
12931293

12941294
// make epoch 2.1 and 2.2 start in the middle of boot-up
12951295
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
1296-
epochs[1].end_height = 101;
1297-
epochs[2].start_height = 101;
1298-
epochs[2].end_height = 151;
1299-
epochs[3].start_height = 151;
1300-
epochs[3].end_height = epoch_2_2;
1301-
epochs[4].start_height = epoch_2_2;
1302-
epochs[4].end_height = STACKS_EPOCH_MAX;
1303-
epochs.truncate(5);
1296+
epochs[StacksEpochId::Epoch20.index()].end_height = 101;
1297+
epochs[StacksEpochId::Epoch2_05.index()].start_height = 101;
1298+
epochs[StacksEpochId::Epoch2_05.index()].end_height = 151;
1299+
epochs[StacksEpochId::Epoch21.index()].start_height = 151;
1300+
epochs[StacksEpochId::Epoch21.index()].end_height = epoch_2_2;
1301+
epochs[StacksEpochId::Epoch22.index()].start_height = epoch_2_2;
1302+
epochs[StacksEpochId::Epoch22.index()].end_height = STACKS_EPOCH_MAX;
1303+
epochs.truncate(StacksEpochId::Epoch22.index() + 1);
13041304
conf_template.burnchain.epochs = Some(epochs);
13051305

13061306
let privks: Vec<_> = (0..5)

testnet/stacks-node/src/tests/epoch_23.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ fn trait_invocation_behavior() {
104104
conf.initial_balances.append(&mut initial_balances);
105105

106106
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
107-
epochs[1].end_height = epoch_2_05;
108-
epochs[2].start_height = epoch_2_05;
109-
epochs[2].end_height = epoch_2_1;
110-
epochs[3].start_height = epoch_2_1;
111-
epochs[3].end_height = epoch_2_2;
112-
epochs[4].start_height = epoch_2_2;
113-
epochs[4].end_height = epoch_2_3;
114-
epochs[5].start_height = epoch_2_3;
115-
epochs[5].end_height = STACKS_EPOCH_MAX;
116-
epochs.truncate(6);
107+
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
108+
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
109+
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
110+
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
111+
epochs[StacksEpochId::Epoch21.index()].end_height = epoch_2_2;
112+
epochs[StacksEpochId::Epoch22.index()].start_height = epoch_2_2;
113+
epochs[StacksEpochId::Epoch22.index()].end_height = epoch_2_3;
114+
epochs[StacksEpochId::Epoch23.index()].start_height = epoch_2_3;
115+
epochs[StacksEpochId::Epoch23.index()].end_height = STACKS_EPOCH_MAX;
116+
epochs.truncate(StacksEpochId::Epoch23.index() + 1);
117117
conf.burnchain.epochs = Some(epochs);
118118

119119
let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());

0 commit comments

Comments
 (0)