Skip to content

Commit e869346

Browse files
committed
refactor: use EpochList in more places
1 parent 079455b commit e869346

File tree

17 files changed

+171
-125
lines changed

17 files changed

+171
-125
lines changed

stacks-common/src/types/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,37 @@ impl<L: Clone> EpochList<L> {
486486
self.0.truncate(index + 1);
487487
}
488488
}
489+
490+
/// Determine which epoch, if any, a given burnchain height falls into.
491+
pub fn epoch_id_at_height(&self, height: u64) -> Option<StacksEpochId> {
492+
for epoch in self.0.iter() {
493+
if epoch.start_height <= height && height < epoch.end_height {
494+
return Some(epoch.epoch_id);
495+
}
496+
}
497+
None
498+
}
499+
500+
/// Determine which epoch, if any, a given burnchain height falls into.
501+
pub fn epoch_at_height(&self, height: u64) -> Option<StacksEpoch<L>> {
502+
for epoch in self.0.iter() {
503+
if epoch.start_height <= height && height < epoch.end_height {
504+
return Some(epoch.clone());
505+
}
506+
}
507+
None
508+
}
509+
510+
/// Pushes a new `StacksEpoch` to the end of the list
511+
pub fn push(&mut self, epoch: StacksEpoch<L>) {
512+
if let Some(last) = self.0.last() {
513+
assert!(
514+
epoch.start_height == last.end_height && epoch.epoch_id > last.epoch_id,
515+
"Epochs must be pushed in order"
516+
);
517+
}
518+
self.0.push(epoch);
519+
}
489520
}
490521

491522
impl<L: Clone> Index<StacksEpochId> for EpochList<L> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6637,7 +6637,7 @@ pub mod tests {
66376637
pub fn connect_test_with_epochs(
66386638
first_block_height: u64,
66396639
first_burn_hash: &BurnchainHeaderHash,
6640-
epochs: Vec<StacksEpoch>,
6640+
epochs: EpochList,
66416641
) -> Result<SortitionDB, db_error> {
66426642
let mut rng = rand::thread_rng();
66436643
let mut buf = [0u8; 32];

stackslib/src/chainstate/coordinator/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub fn setup_states_with_epochs(
290290
pox_consts: Option<PoxConstants>,
291291
initial_balances: Option<Vec<(PrincipalData, u64)>>,
292292
stacks_epoch_id: StacksEpochId,
293-
epochs_opt: Option<Vec<StacksEpoch>>,
293+
epochs_opt: Option<EpochList>,
294294
) {
295295
let mut burn_block = None;
296296
let mut others = vec![];

stackslib/src/chainstate/stacks/boot/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ pub mod test {
16401640
pub fn instantiate_pox_peer_with_epoch<'a>(
16411641
burnchain: &Burnchain,
16421642
test_name: &str,
1643-
epochs: Option<Vec<StacksEpoch>>,
1643+
epochs: Option<EpochList>,
16441644
observer: Option<&'a TestEventObserver>,
16451645
) -> (TestPeer<'a>, Vec<StacksPrivateKey>) {
16461646
let mut peer_config = TestPeerConfig::new(test_name, 0, 0);

stackslib/src/chainstate/stacks/boot/pox_3_tests.rs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn get_tip(sortdb: Option<&SortitionDB>) -> BlockSnapshot {
8181
SortitionDB::get_canonical_burn_chain_tip(&sortdb.unwrap().conn()).unwrap()
8282
}
8383

84-
fn make_test_epochs_pox() -> (Vec<StacksEpoch>, PoxConstants) {
84+
fn make_test_epochs_pox() -> (EpochList, PoxConstants) {
8585
let EMPTY_SORTITIONS = 25;
8686
let EPOCH_2_1_HEIGHT = EMPTY_SORTITIONS + 11; // 36
8787
let EPOCH_2_2_HEIGHT = EPOCH_2_1_HEIGHT + 14; // 50
@@ -92,7 +92,7 @@ fn make_test_epochs_pox() -> (Vec<StacksEpoch>, PoxConstants) {
9292

9393
// cycle 11 = 60
9494

95-
let epochs = vec![
95+
let epochs = EpochList::new(&[
9696
StacksEpoch {
9797
epoch_id: StacksEpochId::Epoch10,
9898
start_height: 0,
@@ -142,7 +142,7 @@ fn make_test_epochs_pox() -> (Vec<StacksEpoch>, PoxConstants) {
142142
block_limit: ExecutionCost::max_value(),
143143
network_epoch: PEER_VERSION_EPOCH_2_4,
144144
},
145-
];
145+
]);
146146

147147
let mut pox_constants = PoxConstants::mainnet_default();
148148
pox_constants.reward_cycle_length = 5;
@@ -279,7 +279,7 @@ fn simple_pox_lockup_transition_pox_2() {
279279
assert_eq!(alice_balance, 0);
280280

281281
// produce blocks until immediately before the 2.1 epoch switch
282-
while get_tip(peer.sortdb.as_ref()).block_height < epochs[3].start_height {
282+
while get_tip(peer.sortdb.as_ref()).block_height < epochs[StacksEpochId::Epoch21].start_height {
283283
peer.tenure_with_txs(&[], &mut coinbase_nonce);
284284

285285
// alice is still locked, balance should be 0
@@ -377,7 +377,7 @@ fn simple_pox_lockup_transition_pox_2() {
377377
assert_eq!(alice_balance, 512 * POX_THRESHOLD_STEPS_USTX);
378378

379379
// now, let's roll the chain forward until just before Epoch-2.2
380-
while get_tip(peer.sortdb.as_ref()).block_height < epochs[4].start_height {
380+
while get_tip(peer.sortdb.as_ref()).block_height < epochs[StacksEpochId::Epoch22].start_height {
381381
peer.tenure_with_txs(&[], &mut coinbase_nonce);
382382
// at this point, alice's balance should always include this half lockup
383383
let alice_balance = get_balance(&mut peer, &key_to_stacks_addr(&alice).into());
@@ -394,7 +394,8 @@ fn simple_pox_lockup_transition_pox_2() {
394394
assert_eq!(alice_balance, 1024 * POX_THRESHOLD_STEPS_USTX);
395395

396396
// now, roll the chain forward to Epoch-2.4
397-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
397+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
398+
{
398399
peer.tenure_with_txs(&[], &mut coinbase_nonce);
399400
// at this point, alice's balance should always be unlocked
400401
let alice_balance = get_balance(&mut peer, &key_to_stacks_addr(&alice).into());
@@ -612,7 +613,8 @@ fn pox_auto_unlock(alice_first: bool) {
612613
let mut coinbase_nonce = 0;
613614

614615
// produce blocks until epoch 2.1
615-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[3].start_height {
616+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch21].start_height
617+
{
616618
peer.tenure_with_txs(&[], &mut coinbase_nonce);
617619
}
618620

@@ -762,7 +764,8 @@ fn pox_auto_unlock(alice_first: bool) {
762764
// now, lets check behavior in Epochs 2.2-2.4, with pox-3 auto unlock tests
763765

764766
// produce blocks until epoch 2.2
765-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[4].start_height {
767+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch22].start_height
768+
{
766769
peer.tenure_with_txs(&[], &mut coinbase_nonce);
767770
let alice_balance = get_balance(&mut peer, &key_to_stacks_addr(&alice).into());
768771
assert_eq!(alice_balance, 0);
@@ -774,7 +777,8 @@ fn pox_auto_unlock(alice_first: bool) {
774777
assert_eq!(alice_balance, 1024 * POX_THRESHOLD_STEPS_USTX);
775778

776779
// produce blocks until epoch 2.4
777-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
780+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
781+
{
778782
peer.tenure_with_txs(&[], &mut coinbase_nonce);
779783
}
780784

@@ -1051,7 +1055,8 @@ fn delegate_stack_increase() {
10511055
let mut coinbase_nonce = 0;
10521056

10531057
// produce blocks until epoch 2.1
1054-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[3].start_height {
1058+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch21].start_height
1059+
{
10551060
peer.tenure_with_txs(&[], &mut coinbase_nonce);
10561061
}
10571062

@@ -1236,7 +1241,7 @@ fn delegate_stack_increase() {
12361241
// on pox-3
12371242

12381243
// roll the chain forward until just before Epoch-2.2
1239-
while get_tip(peer.sortdb.as_ref()).block_height < epochs[4].start_height {
1244+
while get_tip(peer.sortdb.as_ref()).block_height < epochs[StacksEpochId::Epoch22].start_height {
12401245
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
12411246
// at this point, alice's balance should always include this half lockup
12421247
assert_eq!(
@@ -1279,7 +1284,8 @@ fn delegate_stack_increase() {
12791284
);
12801285

12811286
// Roll to Epoch-2.4 and re-do the above tests
1282-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
1287+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
1288+
{
12831289
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
12841290
}
12851291

@@ -1666,7 +1672,8 @@ fn stack_increase() {
16661672
let increase_amt = total_balance - first_lockup_amt;
16671673

16681674
// produce blocks until epoch 2.1
1669-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[3].start_height {
1675+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch21].start_height
1676+
{
16701677
peer.tenure_with_txs(&[], &mut coinbase_nonce);
16711678
}
16721679

@@ -1799,7 +1806,7 @@ fn stack_increase() {
17991806
// on pox-3
18001807

18011808
// roll the chain forward until just before Epoch-2.2
1802-
while get_tip(peer.sortdb.as_ref()).block_height < epochs[4].start_height {
1809+
while get_tip(peer.sortdb.as_ref()).block_height < epochs[StacksEpochId::Epoch22].start_height {
18031810
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
18041811
// at this point, alice's balance should always include this half lockup
18051812
assert_eq!(
@@ -1828,7 +1835,8 @@ fn stack_increase() {
18281835
);
18291836

18301837
// Roll to Epoch-2.4 and re-do the above stack-increase tests
1831-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
1838+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
1839+
{
18321840
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
18331841
}
18341842

@@ -2242,7 +2250,7 @@ fn pox_extend_transition() {
22422250
}
22432251

22442252
// produce blocks until epoch 2.1
2245-
while get_tip(peer.sortdb.as_ref()).block_height < epochs[3].start_height {
2253+
while get_tip(peer.sortdb.as_ref()).block_height < epochs[StacksEpochId::Epoch21].start_height {
22462254
peer.tenure_with_txs(&[], &mut coinbase_nonce);
22472255
alice_rewards_to_v2_start_checks(latest_block, &mut peer);
22482256
}
@@ -2311,7 +2319,7 @@ fn pox_extend_transition() {
23112319
// Roll to Epoch-2.4 and re-do the above tests
23122320

23132321
// roll the chain forward until just before Epoch-2.2
2314-
while get_tip(peer.sortdb.as_ref()).block_height < epochs[4].start_height {
2322+
while get_tip(peer.sortdb.as_ref()).block_height < epochs[StacksEpochId::Epoch22].start_height {
23152323
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
23162324
// at this point, alice's balance should be locked, and so should bob's
23172325
let alice_balance = get_balance(&mut peer, &key_to_stacks_addr(&alice).into());
@@ -2338,7 +2346,8 @@ fn pox_extend_transition() {
23382346
assert_eq!(bob_account.amount_unlocked(), INITIAL_BALANCE);
23392347

23402348
// Roll to Epoch-2.4 and re-do the above stack-extend tests
2341-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
2349+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
2350+
{
23422351
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
23432352
}
23442353

@@ -2611,7 +2620,8 @@ fn delegate_extend_pox_3() {
26112620
let mut latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
26122621

26132622
// Roll to Epoch-2.4 and perform the delegate-stack-extend tests
2614-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
2623+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
2624+
{
26152625
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
26162626
}
26172627

@@ -3087,7 +3097,8 @@ fn pox_3_getters() {
30873097

30883098
let mut latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
30893099
// Roll to Epoch-2.4 and perform the delegate-stack-extend tests
3090-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
3100+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
3101+
{
30913102
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
30923103
}
30933104

@@ -3491,10 +3502,12 @@ fn get_pox_addrs() {
34913502
};
34923503

34933504
// produce blocks until epoch 2.2
3494-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
3505+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
3506+
{
34953507
peer.tenure_with_txs(&[], &mut coinbase_nonce);
34963508
// if we reach epoch 2.1, perform the check
3497-
if get_tip(peer.sortdb.as_ref()).block_height > epochs[3].start_height {
3509+
if get_tip(peer.sortdb.as_ref()).block_height > epochs[StacksEpochId::Epoch21].start_height
3510+
{
34983511
assert_latest_was_burn(&mut peer);
34993512
}
35003513
}
@@ -3700,10 +3713,12 @@ fn stack_with_segwit() {
37003713
};
37013714

37023715
// produce blocks until epoch 2.2
3703-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
3716+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
3717+
{
37043718
peer.tenure_with_txs(&[], &mut coinbase_nonce);
37053719
// if we reach epoch 2.1, perform the check
3706-
if get_tip(peer.sortdb.as_ref()).block_height > epochs[3].start_height {
3720+
if get_tip(peer.sortdb.as_ref()).block_height > epochs[StacksEpochId::Epoch21].start_height
3721+
{
37073722
assert_latest_was_burn(&mut peer);
37083723
}
37093724
}
@@ -3882,7 +3897,8 @@ fn stack_aggregation_increase() {
38823897
let mut latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
38833898

38843899
// Roll to Epoch-2.4 and perform the delegate-stack-extend tests
3885-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
3900+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
3901+
{
38863902
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
38873903
}
38883904

@@ -4296,7 +4312,8 @@ fn pox_3_delegate_stx_addr_validation() {
42964312
let mut latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
42974313

42984314
// Roll to Epoch-2.4 and perform the delegate-stack-extend tests
4299-
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[6].start_height {
4315+
while get_tip(peer.sortdb.as_ref()).block_height <= epochs[StacksEpochId::Epoch24].start_height
4316+
{
43004317
latest_block = peer.tenure_with_txs(&[], &mut coinbase_nonce);
43014318
}
43024319

0 commit comments

Comments
 (0)