Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add index method for StacksEpochId #5350

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
15 changes: 15 additions & 0 deletions stacks-common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ impl StacksEpochId {
StacksEpochId::Epoch30 => cur_reward_cycle > first_epoch30_reward_cycle,
}
}

/// Return the index for this epoch in the list of epochs
pub fn index(&self) -> usize {
match self {
StacksEpochId::Epoch10 => 0,
StacksEpochId::Epoch20 => 1,
StacksEpochId::Epoch2_05 => 2,
StacksEpochId::Epoch21 => 3,
StacksEpochId::Epoch22 => 4,
StacksEpochId::Epoch23 => 5,
StacksEpochId::Epoch24 => 6,
StacksEpochId::Epoch25 => 7,
StacksEpochId::Epoch30 => 8,
}
}
}

impl std::fmt::Display for StacksEpochId {
Expand Down
10 changes: 3 additions & 7 deletions testnet/stacks-node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,7 @@ impl Config {
self.burnchain.get_bitcoin_network().1,
self.burnchain.epochs.as_ref(),
);
let Some(epoch_30) = StacksEpoch::find_epoch_by_id(&epochs, StacksEpochId::Epoch30)
.map(|epoch_ix| epochs[epoch_ix].clone())
else {
let Some(epoch_30) = epochs.get(StacksEpochId::Epoch30.index()) else {
// no Epoch 3.0, so just return
return;
};
Expand Down Expand Up @@ -610,9 +608,7 @@ impl Config {
let _ = StacksEpoch::validate_epochs(epochs);

// sanity check: v1_unlock_height must happen after pox-2 instantiation
let epoch21_index = StacksEpoch::find_epoch_by_id(&epochs, StacksEpochId::Epoch21)
.expect("FATAL: no epoch 2.1 defined");

let epoch21_index = StacksEpochId::Epoch21.index();
let epoch21 = &epochs[epoch21_index];
let v1_unlock_height = burnchain.pox_constants.v1_unlock_height as u64;

Expand Down Expand Up @@ -714,7 +710,7 @@ impl Config {
}

// Stacks 1.0 must start at 0
if matched_epochs[0].1 != 0 {
if matched_epochs[StacksEpochId::Epoch10.index()].1 != 0 {
return Err("Stacks 1.0 must start at height = 0".into());
}

Expand Down
5 changes: 3 additions & 2 deletions testnet/stacks-node/src/run_loop/boot_nakamoto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ impl BootRunLoop {
config.burnchain.get_bitcoin_network().1,
config.burnchain.epochs.as_ref(),
);
let epoch_3 = &epochs[StacksEpoch::find_epoch_by_id(&epochs, StacksEpochId::Epoch30)
.ok_or("No Epoch-3.0 defined")?];
let epoch_3 = epochs
.get(StacksEpochId::Epoch30.index())
.ok_or("No Epoch-3.0 defined")?;

Ok(u64::from(burn_height) >= epoch_3.start_height - 1)
}
Expand Down
12 changes: 6 additions & 6 deletions testnet/stacks-node/src/tests/epoch_205.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ fn test_exact_block_costs() {

let (mut conf, _miner_account) = neon_integration_test_conf();
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_205_transition_height;
epochs[2].start_height = epoch_205_transition_height;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_205_transition_height;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_205_transition_height;

conf.burnchain.epochs = Some(epochs);
conf.node.mine_microblocks = true;
Expand Down Expand Up @@ -305,8 +305,8 @@ fn test_dynamic_db_method_costs() {

let (mut conf, _miner_account) = neon_integration_test_conf();
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_205_transition_height;
epochs[2].start_height = epoch_205_transition_height;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_205_transition_height;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_205_transition_height;

conf.burnchain.epochs = Some(epochs);

Expand Down Expand Up @@ -508,8 +508,8 @@ fn transition_empty_blocks() {
let (mut conf, miner_account) = neon_integration_test_conf();

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;

conf.burnchain.epochs = Some(epochs);

Expand Down
104 changes: 52 additions & 52 deletions testnet/stacks-node/src/tests/epoch_21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ fn advance_to_2_1(
test_observer::register_any(&mut conf);

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;

conf.burnchain.epochs = Some(epochs);

Expand Down Expand Up @@ -577,10 +577,10 @@ fn transition_fixes_bitcoin_rigidity() {
test_observer::register_any(&mut conf);

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;

conf.burnchain.epochs = Some(epochs);

Expand Down Expand Up @@ -1497,10 +1497,10 @@ fn transition_removes_pox_sunset() {
let epoch_21 = epoch_21_rc * reward_cycle_len + 1;

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = 1;
epochs[2].start_height = 1;
epochs[2].end_height = epoch_21;
epochs[3].start_height = epoch_21;
epochs[StacksEpochId::Epoch20.index()].end_height = 1;
epochs[StacksEpochId::Epoch2_05.index()].start_height = 1;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_21;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_21;

conf.burnchain.epochs = Some(epochs);

Expand Down Expand Up @@ -1770,10 +1770,10 @@ fn transition_empty_blocks() {
let (mut conf, miner_account) = neon_integration_test_conf();

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;

conf.node.mine_microblocks = false;
conf.burnchain.max_rbf = 1000000;
Expand Down Expand Up @@ -2051,10 +2051,10 @@ fn test_pox_reorgs_three_flaps() {

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

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

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

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

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

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

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

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

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

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

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

let privks: Vec<_> = (0..5)
Expand Down Expand Up @@ -4750,10 +4750,10 @@ fn trait_invocation_cross_epoch() {
conf.initial_balances.append(&mut initial_balances);
test_observer::register_any(&mut conf);
let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
conf.burnchain.epochs = Some(epochs);

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

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
conf.burnchain.epochs = Some(epochs);

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

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
conf.burnchain.epochs = Some(epochs);

let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());
Expand Down
48 changes: 24 additions & 24 deletions testnet/stacks-node/src/tests/epoch_22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ fn disable_pox() {
conf.initial_balances.append(&mut initial_balances);

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[3].end_height = epoch_2_2;
epochs[4].start_height = epoch_2_2;
epochs[4].end_height = STACKS_EPOCH_MAX;
epochs.truncate(5);
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].end_height = epoch_2_2;
epochs[StacksEpochId::Epoch22.index()].start_height = epoch_2_2;
epochs[StacksEpochId::Epoch22.index()].end_height = STACKS_EPOCH_MAX;
epochs.truncate(StacksEpochId::Epoch22.index() + 1);
conf.burnchain.epochs = Some(epochs);

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

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[3].end_height = epoch_2_2;
epochs[4].start_height = epoch_2_2;
epochs[4].end_height = STACKS_EPOCH_MAX;
epochs.truncate(5);
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].end_height = epoch_2_2;
epochs[StacksEpochId::Epoch22.index()].start_height = epoch_2_2;
epochs[StacksEpochId::Epoch22.index()].end_height = STACKS_EPOCH_MAX;
epochs.truncate(StacksEpochId::Epoch22.index() + 1);
conf.burnchain.epochs = Some(epochs);

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

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

let privks: Vec<_> = (0..5)
Expand Down
20 changes: 10 additions & 10 deletions testnet/stacks-node/src/tests/epoch_23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ fn trait_invocation_behavior() {
conf.initial_balances.append(&mut initial_balances);

let mut epochs = core::STACKS_EPOCHS_REGTEST.to_vec();
epochs[1].end_height = epoch_2_05;
epochs[2].start_height = epoch_2_05;
epochs[2].end_height = epoch_2_1;
epochs[3].start_height = epoch_2_1;
epochs[3].end_height = epoch_2_2;
epochs[4].start_height = epoch_2_2;
epochs[4].end_height = epoch_2_3;
epochs[5].start_height = epoch_2_3;
epochs[5].end_height = STACKS_EPOCH_MAX;
epochs.truncate(6);
epochs[StacksEpochId::Epoch20.index()].end_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].start_height = epoch_2_05;
epochs[StacksEpochId::Epoch2_05.index()].end_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].start_height = epoch_2_1;
epochs[StacksEpochId::Epoch21.index()].end_height = epoch_2_2;
epochs[StacksEpochId::Epoch22.index()].start_height = epoch_2_2;
epochs[StacksEpochId::Epoch22.index()].end_height = epoch_2_3;
epochs[StacksEpochId::Epoch23.index()].start_height = epoch_2_3;
epochs[StacksEpochId::Epoch23.index()].end_height = STACKS_EPOCH_MAX;
epochs.truncate(StacksEpochId::Epoch23.index() + 1);
conf.burnchain.epochs = Some(epochs);

let mut burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());
Expand Down
Loading