Skip to content

Commit

Permalink
refactor: minor changes from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
obycode committed Nov 6, 2024
1 parent 7f6eab9 commit e9c141f
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 75 deletions.
18 changes: 6 additions & 12 deletions stacks-common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,22 +489,12 @@ impl<L: Clone> EpochList<L> {

/// Determine which epoch, if any, a given burnchain height falls into.
pub fn epoch_id_at_height(&self, height: u64) -> Option<StacksEpochId> {
for epoch in self.0.iter() {
if epoch.start_height <= height && height < epoch.end_height {
return Some(epoch.epoch_id);
}
}
None
StacksEpoch::find_epoch(self, height).map(|idx| self.0[idx].epoch_id)
}

/// Determine which epoch, if any, a given burnchain height falls into.
pub fn epoch_at_height(&self, height: u64) -> Option<StacksEpoch<L>> {
for epoch in self.0.iter() {
if epoch.start_height <= height && height < epoch.end_height {
return Some(epoch.clone());
}
}
None
StacksEpoch::find_epoch(self, height).map(|idx| self.0[idx].clone())
}

/// Pushes a new `StacksEpoch` to the end of the list
Expand All @@ -517,6 +507,10 @@ impl<L: Clone> EpochList<L> {
}
self.0.push(epoch);
}

pub fn to_vec(&self) -> Vec<StacksEpoch<L>> {
self.0.clone()
}
}

impl<L: Clone> Index<StacksEpochId> for EpochList<L> {
Expand Down
10 changes: 5 additions & 5 deletions stackslib/src/burnchains/bitcoin/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ impl TryFrom<u32> for BitcoinNetworkType {
/// Should *not* be used except by the BitcoinIndexer when no epochs vector
/// was specified.
pub fn get_bitcoin_stacks_epochs(network_id: BitcoinNetworkType) -> EpochList {
EpochList::new(match network_id {
BitcoinNetworkType::Mainnet => &*STACKS_EPOCHS_MAINNET,
BitcoinNetworkType::Testnet => &*STACKS_EPOCHS_TESTNET,
BitcoinNetworkType::Regtest => &*STACKS_EPOCHS_REGTEST,
})
match network_id {
BitcoinNetworkType::Mainnet => (*STACKS_EPOCHS_MAINNET).clone(),
BitcoinNetworkType::Testnet => (*STACKS_EPOCHS_TESTNET).clone(),
BitcoinNetworkType::Regtest => (*STACKS_EPOCHS_REGTEST).clone(),
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
11 changes: 5 additions & 6 deletions stackslib/src/chainstate/burn/db/sortdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10931,10 +10931,9 @@ pub mod tests {

fs::create_dir_all(path_root).unwrap();

let mut bad_epochs = STACKS_EPOCHS_MAINNET.to_vec();
let idx = bad_epochs.len() - 2;
bad_epochs[idx].end_height += 1;
bad_epochs[idx + 1].start_height += 1;
let mut bad_epochs = (*STACKS_EPOCHS_MAINNET).clone();
bad_epochs[StacksEpochId::Epoch25].end_height += 1;
bad_epochs[StacksEpochId::Epoch30].start_height += 1;

let sortdb = SortitionDB::connect(
&format!("{}/sortdb.sqlite", &path_root),
Expand All @@ -10949,14 +10948,14 @@ pub mod tests {
.unwrap();

let db_epochs = SortitionDB::get_stacks_epochs(sortdb.conn()).unwrap();
assert_eq!(db_epochs, bad_epochs);
assert_eq!(db_epochs, bad_epochs.to_vec());

let fixed_sortdb = SortitionDB::connect(
&format!("{}/sortdb.sqlite", &path_root),
0,
&BurnchainHeaderHash([0x00; 32]),
0,
&STACKS_EPOCHS_MAINNET.to_vec(),
&STACKS_EPOCHS_MAINNET,
PoxConstants::mainnet_default(),
None,
true,
Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl StacksChainConfig {
.unwrap(),
first_burn_header_timestamp: BITCOIN_MAINNET_FIRST_BLOCK_TIMESTAMP.into(),
pox_constants: PoxConstants::mainnet_default(),
epochs: EpochList::new(&*STACKS_EPOCHS_MAINNET),
epochs: (*STACKS_EPOCHS_MAINNET).clone(),
}
}
}
Expand Down
234 changes: 183 additions & 51 deletions stackslib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub fn check_fault_injection(fault_name: &str) -> bool {
}

lazy_static! {
pub static ref STACKS_EPOCHS_MAINNET: [StacksEpoch; 9] = [
pub static ref STACKS_EPOCHS_MAINNET: EpochList = EpochList::new(&[
StacksEpoch {
epoch_id: StacksEpochId::Epoch10,
start_height: 0,
Expand Down Expand Up @@ -302,11 +302,11 @@ lazy_static! {
block_limit: BLOCK_LIMIT_MAINNET_21.clone(),
network_epoch: PEER_VERSION_EPOCH_3_0
},
];
]);
}

lazy_static! {
pub static ref STACKS_EPOCHS_TESTNET: [StacksEpoch; 9] = [
pub static ref STACKS_EPOCHS_TESTNET: EpochList = EpochList::new(&[
StacksEpoch {
epoch_id: StacksEpochId::Epoch10,
start_height: 0,
Expand Down Expand Up @@ -370,11 +370,11 @@ lazy_static! {
block_limit: BLOCK_LIMIT_MAINNET_21.clone(),
network_epoch: PEER_VERSION_EPOCH_3_0
},
];
]);
}

lazy_static! {
pub static ref STACKS_EPOCHS_REGTEST: [StacksEpoch; 9] = [
pub static ref STACKS_EPOCHS_REGTEST: EpochList = EpochList::new(&[
StacksEpoch {
epoch_id: StacksEpochId::Epoch10,
start_height: 0,
Expand Down Expand Up @@ -438,7 +438,7 @@ lazy_static! {
block_limit: BLOCK_LIMIT_MAINNET_21.clone(),
network_epoch: PEER_VERSION_EPOCH_3_0
},
];
]);
}

/// Stacks 2.05 epoch marker. All block-commits in 2.05 must have a memo bitfield with this value
Expand Down Expand Up @@ -471,51 +471,183 @@ pub static STACKS_EPOCH_3_0_MARKER: u8 = 0x0b;

#[test]
fn test_ord_for_stacks_epoch() {
let epochs = STACKS_EPOCHS_MAINNET.clone();
assert_eq!(epochs[0].cmp(&epochs[1]), Ordering::Less);
assert_eq!(epochs[1].cmp(&epochs[2]), Ordering::Less);
assert_eq!(epochs[0].cmp(&epochs[2]), Ordering::Less);
assert_eq!(epochs[0].cmp(&epochs[0]), Ordering::Equal);
assert_eq!(epochs[1].cmp(&epochs[1]), Ordering::Equal);
assert_eq!(epochs[2].cmp(&epochs[2]), Ordering::Equal);
assert_eq!(epochs[3].cmp(&epochs[3]), Ordering::Equal);
assert_eq!(epochs[4].cmp(&epochs[4]), Ordering::Equal);
assert_eq!(epochs[2].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[2].cmp(&epochs[1]), Ordering::Greater);
assert_eq!(epochs[1].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[3].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[3].cmp(&epochs[1]), Ordering::Greater);
assert_eq!(epochs[3].cmp(&epochs[2]), Ordering::Greater);
assert_eq!(epochs[4].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[4].cmp(&epochs[1]), Ordering::Greater);
assert_eq!(epochs[4].cmp(&epochs[2]), Ordering::Greater);
assert_eq!(epochs[4].cmp(&epochs[3]), Ordering::Greater);
assert_eq!(epochs[5].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[5].cmp(&epochs[1]), Ordering::Greater);
assert_eq!(epochs[5].cmp(&epochs[2]), Ordering::Greater);
assert_eq!(epochs[5].cmp(&epochs[3]), Ordering::Greater);
assert_eq!(epochs[5].cmp(&epochs[4]), Ordering::Greater);
assert_eq!(epochs[6].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[6].cmp(&epochs[1]), Ordering::Greater);
assert_eq!(epochs[6].cmp(&epochs[2]), Ordering::Greater);
assert_eq!(epochs[6].cmp(&epochs[3]), Ordering::Greater);
assert_eq!(epochs[6].cmp(&epochs[4]), Ordering::Greater);
assert_eq!(epochs[6].cmp(&epochs[5]), Ordering::Greater);
assert_eq!(epochs[7].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[7].cmp(&epochs[1]), Ordering::Greater);
assert_eq!(epochs[7].cmp(&epochs[2]), Ordering::Greater);
assert_eq!(epochs[7].cmp(&epochs[3]), Ordering::Greater);
assert_eq!(epochs[7].cmp(&epochs[4]), Ordering::Greater);
assert_eq!(epochs[7].cmp(&epochs[5]), Ordering::Greater);
assert_eq!(epochs[7].cmp(&epochs[6]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[0]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[1]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[2]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[3]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[4]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[5]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[6]), Ordering::Greater);
assert_eq!(epochs[8].cmp(&epochs[7]), Ordering::Greater);
let epochs = &*STACKS_EPOCHS_MAINNET;
assert_eq!(
epochs[StacksEpochId::Epoch10].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Less
);
assert_eq!(
epochs[StacksEpochId::Epoch20].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Less
);
assert_eq!(
epochs[StacksEpochId::Epoch10].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Less
);
assert_eq!(
epochs[StacksEpochId::Epoch10].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Equal
);
assert_eq!(
epochs[StacksEpochId::Epoch20].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Equal
);
assert_eq!(
epochs[StacksEpochId::Epoch2_05].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Equal
);
assert_eq!(
epochs[StacksEpochId::Epoch21].cmp(&epochs[StacksEpochId::Epoch21]),
Ordering::Equal
);
assert_eq!(
epochs[StacksEpochId::Epoch22].cmp(&epochs[StacksEpochId::Epoch22]),
Ordering::Equal
);
assert_eq!(
epochs[StacksEpochId::Epoch2_05].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch2_05].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch20].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch21].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch21].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch21].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch22].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch22].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch22].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch22].cmp(&epochs[StacksEpochId::Epoch21]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch23].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch23].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch23].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch23].cmp(&epochs[StacksEpochId::Epoch21]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch23].cmp(&epochs[StacksEpochId::Epoch22]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch24].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch24].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch24].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch24].cmp(&epochs[StacksEpochId::Epoch21]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch24].cmp(&epochs[StacksEpochId::Epoch22]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch24].cmp(&epochs[StacksEpochId::Epoch23]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch25].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch25].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch25].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch25].cmp(&epochs[StacksEpochId::Epoch21]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch25].cmp(&epochs[StacksEpochId::Epoch22]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch25].cmp(&epochs[StacksEpochId::Epoch23]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch25].cmp(&epochs[StacksEpochId::Epoch24]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch10]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch20]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch2_05]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch21]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch22]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch23]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch24]),
Ordering::Greater
);
assert_eq!(
epochs[StacksEpochId::Epoch30].cmp(&epochs[StacksEpochId::Epoch25]),
Ordering::Greater
);
}

#[test]
Expand Down

0 comments on commit e9c141f

Please sign in to comment.