Skip to content

Commit fc6cf93

Browse files
committed
refactor: add EpochList type
Wrapper struct to make the accessing of the individual epochs easier and standardized.
1 parent 8e097ef commit fc6cf93

File tree

23 files changed

+377
-368
lines changed

23 files changed

+377
-368
lines changed

stacks-common/src/types/mod.rs

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cmp::Ordering;
22
use std::fmt;
3+
use std::ops::{Deref, DerefMut, Index, IndexMut};
34

45
#[cfg(feature = "canonical")]
56
pub mod sqlite;
@@ -235,21 +236,6 @@ impl StacksEpochId {
235236
StacksEpochId::Epoch30 => cur_reward_cycle > first_epoch30_reward_cycle,
236237
}
237238
}
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-
}
253239
}
254240

255241
impl std::fmt::Display for StacksEpochId {
@@ -475,3 +461,58 @@ impl<L: PartialEq + Eq> Ord for StacksEpoch<L> {
475461
self.epoch_id.cmp(&other.epoch_id)
476462
}
477463
}
464+
465+
/// A wrapper for holding a list of Epochs, indexable by StacksEpochId
466+
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq)]
467+
pub struct EpochList<L: Clone>(Vec<StacksEpoch<L>>);
468+
469+
impl<L: Clone> EpochList<L> {
470+
pub fn new(epochs: &[StacksEpoch<L>]) -> EpochList<L> {
471+
EpochList(epochs.to_vec())
472+
}
473+
474+
pub fn get(&self, index: StacksEpochId) -> Option<&StacksEpoch<L>> {
475+
self.0.get(StacksEpoch::find_epoch_by_id(&self.0, index)?)
476+
}
477+
478+
pub fn get_mut(&mut self, index: StacksEpochId) -> Option<&mut StacksEpoch<L>> {
479+
let index = StacksEpoch::find_epoch_by_id(&self.0, index)?;
480+
self.0.get_mut(index)
481+
}
482+
483+
/// Truncates the list after the given epoch id
484+
pub fn truncate_after(&mut self, epoch_id: StacksEpochId) {
485+
if let Some(index) = StacksEpoch::find_epoch_by_id(&self.0, epoch_id) {
486+
self.0.truncate(index + 1);
487+
}
488+
}
489+
}
490+
491+
impl<L: Clone> Index<StacksEpochId> for EpochList<L> {
492+
type Output = StacksEpoch<L>;
493+
fn index(&self, index: StacksEpochId) -> &StacksEpoch<L> {
494+
self.get(index)
495+
.expect("Invalid StacksEpochId: could not find corresponding epoch")
496+
}
497+
}
498+
499+
impl<L: Clone> IndexMut<StacksEpochId> for EpochList<L> {
500+
fn index_mut(&mut self, index: StacksEpochId) -> &mut StacksEpoch<L> {
501+
self.get_mut(index)
502+
.expect("Invalid StacksEpochId: could not find corresponding epoch")
503+
}
504+
}
505+
506+
impl<L: Clone> Deref for EpochList<L> {
507+
type Target = [StacksEpoch<L>];
508+
509+
fn deref(&self) -> &Self::Target {
510+
&self.0
511+
}
512+
}
513+
514+
impl<L: Clone> DerefMut for EpochList<L> {
515+
fn deref_mut(&mut self) -> &mut Self::Target {
516+
&mut self.0
517+
}
518+
}

stackslib/src/burnchains/bitcoin/indexer.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::burnchains::{
4545
Burnchain, BurnchainBlockHeader, Error as burnchain_error, MagicBytes, BLOCKSTACK_MAGIC_MAINNET,
4646
};
4747
use crate::core::{
48-
StacksEpoch, StacksEpochExtension, STACKS_EPOCHS_MAINNET, STACKS_EPOCHS_REGTEST,
48+
EpochList, StacksEpoch, StacksEpochExtension, STACKS_EPOCHS_MAINNET, STACKS_EPOCHS_REGTEST,
4949
STACKS_EPOCHS_TESTNET,
5050
};
5151
use crate::util_lib::db::Error as DBError;
@@ -91,12 +91,12 @@ impl TryFrom<u32> for BitcoinNetworkType {
9191
/// Get the default epochs definitions for the given BitcoinNetworkType.
9292
/// Should *not* be used except by the BitcoinIndexer when no epochs vector
9393
/// was specified.
94-
pub fn get_bitcoin_stacks_epochs(network_id: BitcoinNetworkType) -> Vec<StacksEpoch> {
95-
match network_id {
96-
BitcoinNetworkType::Mainnet => STACKS_EPOCHS_MAINNET.to_vec(),
97-
BitcoinNetworkType::Testnet => STACKS_EPOCHS_TESTNET.to_vec(),
98-
BitcoinNetworkType::Regtest => STACKS_EPOCHS_REGTEST.to_vec(),
99-
}
94+
pub fn get_bitcoin_stacks_epochs(network_id: BitcoinNetworkType) -> EpochList {
95+
EpochList::new(match network_id {
96+
BitcoinNetworkType::Mainnet => &*STACKS_EPOCHS_MAINNET,
97+
BitcoinNetworkType::Testnet => &*STACKS_EPOCHS_TESTNET,
98+
BitcoinNetworkType::Regtest => &*STACKS_EPOCHS_REGTEST,
99+
})
100100
}
101101

102102
#[derive(Debug, Clone, PartialEq)]
@@ -112,7 +112,7 @@ pub struct BitcoinIndexerConfig {
112112
pub spv_headers_path: String,
113113
pub first_block: u64,
114114
pub magic_bytes: MagicBytes,
115-
pub epochs: Option<Vec<StacksEpoch>>,
115+
pub epochs: Option<EpochList>,
116116
}
117117

118118
#[derive(Debug)]
@@ -1041,7 +1041,7 @@ impl BurnchainIndexer for BitcoinIndexer {
10411041
/// 2) Use hard-coded static values, otherwise.
10421042
///
10431043
/// It is an error (panic) to set custom epochs if running on `Mainnet`.
1044-
fn get_stacks_epochs(&self) -> Vec<StacksEpoch> {
1044+
fn get_stacks_epochs(&self) -> EpochList {
10451045
StacksEpoch::get_epochs(self.runtime.network_id, self.config.epochs.as_ref())
10461046
}
10471047

stackslib/src/burnchains/burnchain.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use stacks_common::util::hash::to_hex;
2929
use stacks_common::util::vrf::VRFPublicKey;
3030
use stacks_common::util::{get_epoch_time_ms, get_epoch_time_secs, log, sleep_ms};
3131

32+
use super::EpochList;
3233
use crate::burnchains::affirmation::update_pox_affirmation_maps;
3334
use crate::burnchains::bitcoin::address::{
3435
to_c32_version_byte, BitcoinAddress, LegacyBitcoinAddressType,
@@ -718,7 +719,7 @@ impl Burnchain {
718719
readwrite: bool,
719720
first_block_header_hash: BurnchainHeaderHash,
720721
first_block_header_timestamp: u64,
721-
epochs: Vec<StacksEpoch>,
722+
epochs: EpochList,
722723
) -> Result<(SortitionDB, BurnchainDB), burnchain_error> {
723724
Burnchain::setup_chainstate_dirs(&self.working_dir)?;
724725

stackslib/src/burnchains/indexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub trait BurnchainIndexer {
6262
fn get_first_block_height(&self) -> u64;
6363
fn get_first_block_header_hash(&self) -> Result<BurnchainHeaderHash, burnchain_error>;
6464
fn get_first_block_header_timestamp(&self) -> Result<u64, burnchain_error>;
65-
fn get_stacks_epochs(&self) -> Vec<StacksEpoch>;
65+
fn get_stacks_epochs(&self) -> EpochList;
6666

6767
fn get_headers_path(&self) -> String;
6868
fn get_headers_height(&self) -> Result<u64, burnchain_error>;

stackslib/src/chainstate/burn/db/sortdb.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,9 +2969,9 @@ impl SortitionDB {
29692969
db_tx: &Transaction,
29702970
epochs: &[StacksEpoch],
29712971
) -> Result<(), db_error> {
2972-
let epochs = StacksEpoch::validate_epochs(epochs);
2972+
let epochs: &[StacksEpoch] = &StacksEpoch::validate_epochs(epochs);
29732973
let existing_epochs = Self::get_stacks_epochs(db_tx)?;
2974-
if existing_epochs == epochs {
2974+
if &existing_epochs == epochs {
29752975
return Ok(());
29762976
}
29772977

@@ -3482,9 +3482,10 @@ impl SortitionDB {
34823482
tx.commit()?;
34833483
} else if version == expected_version {
34843484
// this transaction is almost never needed
3485-
let validated_epochs = StacksEpoch::validate_epochs(epochs);
3485+
let validated_epochs: &[StacksEpoch] =
3486+
&StacksEpoch::validate_epochs(epochs);
34863487
let existing_epochs = Self::get_stacks_epochs(self.conn())?;
3487-
if existing_epochs == validated_epochs {
3488+
if &existing_epochs == validated_epochs {
34883489
return Ok(());
34893490
}
34903491

stackslib/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct StacksChainConfig {
5353
pub first_burn_header_hash: BurnchainHeaderHash,
5454
pub first_burn_header_timestamp: u64,
5555
pub pox_constants: PoxConstants,
56-
pub epochs: Vec<StacksEpoch>,
56+
pub epochs: EpochList,
5757
}
5858

5959
impl StacksChainConfig {
@@ -65,7 +65,7 @@ impl StacksChainConfig {
6565
.unwrap(),
6666
first_burn_header_timestamp: BITCOIN_MAINNET_FIRST_BLOCK_TIMESTAMP.into(),
6767
pox_constants: PoxConstants::mainnet_default(),
68-
epochs: STACKS_EPOCHS_MAINNET.to_vec(),
68+
epochs: EpochList::new(&*STACKS_EPOCHS_MAINNET),
6969
}
7070
}
7171
}

0 commit comments

Comments
 (0)