Skip to content

Commit b72a6ab

Browse files
committed
check_missing_anchor_block should stall if missing anchor block regardless of epoch
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent e40e0cf commit b72a6ab

File tree

2 files changed

+27
-72
lines changed

2 files changed

+27
-72
lines changed

stackslib/src/chainstate/coordinator/mod.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ impl<
10341034
}
10351035

10361036
/// Check to see if the discovery of a PoX anchor block means it's time to process a new reward
1037-
/// cycle. Based on the canonical affirmation map, this may not always be the case.
1037+
/// cycle.
10381038
///
10391039
/// This mutates `rc_info` to be the affirmed anchor block status.
10401040
///
@@ -1043,31 +1043,21 @@ impl<
10431043
/// Returns Ok(None) if not
10441044
fn check_missing_anchor_block(
10451045
&self,
1046-
header: &BurnchainBlockHeader,
1046+
_header: &BurnchainBlockHeader,
10471047
rc_info: &mut RewardCycleInfo,
10481048
) -> Result<Option<BlockHeaderHash>, Error> {
1049-
let cur_epoch =
1050-
SortitionDB::get_stacks_epoch(self.sortition_db.conn(), header.block_height)?
1051-
.unwrap_or_else(|| {
1052-
panic!("BUG: no epoch defined at height {}", header.block_height)
1053-
});
1054-
1055-
// TODO: Always stop if the anchor block is not present
1056-
if cur_epoch.epoch_id >= StacksEpochId::Epoch21 {
1057-
// anchor blocks are always assumed to be present in the chain history,
1058-
// so report its absence if we don't have it.
1059-
if let PoxAnchorBlockStatus::SelectedAndUnknown(missing_anchor_block, _) =
1060-
&rc_info.anchor_status
1061-
{
1062-
info!("Currently missing PoX anchor block {missing_anchor_block}, which is assumed to be present");
1063-
return Ok(Some(missing_anchor_block.clone()));
1064-
}
1049+
// anchor blocks are always assumed to be present in the chain history,
1050+
// so report its absence if we don't have it.
1051+
if let PoxAnchorBlockStatus::SelectedAndUnknown(missing_anchor_block, _) =
1052+
&rc_info.anchor_status
1053+
{
1054+
info!("Currently missing PoX anchor block {missing_anchor_block}, which is assumed to be present");
1055+
return Ok(Some(missing_anchor_block.clone()));
10651056
}
10661057

10671058
test_debug!(
1068-
"Reward cycle info at height {}: {:?}",
1069-
&header.block_height,
1070-
&rc_info
1059+
"Reward cycle info at height {}: {rc_info:?}",
1060+
&_header.block_height
10711061
);
10721062
Ok(None)
10731063
}

stackslib/src/chainstate/coordinator/tests.rs

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6448,7 +6448,7 @@ fn test_pox_fork_out_of_order() {
64486448
let burnchain_blinded = get_burnchain_db(path_blinded, None);
64496449
let b = get_burnchain(path, None);
64506450

6451-
eprintln!("Making block {}", ix);
6451+
eprintln!("Making block {ix}");
64526452
let (op, block) = if ix == 0 {
64536453
make_genesis_block(
64546454
&b,
@@ -6461,9 +6461,7 @@ fn test_pox_fork_out_of_order() {
64616461
ix as u32,
64626462
)
64636463
} else {
6464-
let parent = if ix == 1 {
6465-
stacks_blocks[0].1.header.block_hash()
6466-
} else if ix == 6 {
6464+
let parent = if ix == 1 || ix == 6 {
64676465
stacks_blocks[0].1.header.block_hash()
64686466
} else if ix == 11 {
64696467
stacks_blocks[5].1.header.block_hash()
@@ -6506,8 +6504,7 @@ fn test_pox_fork_out_of_order() {
65066504
let ic = sort_db.index_handle_at_tip();
65076505
let bhh = ic.get_last_anchor_block_hash().unwrap().unwrap();
65086506
eprintln!(
6509-
"Anchor block={}, selected at height={}",
6510-
&bhh,
6507+
"Anchor block={bhh}, selected at height={}",
65116508
SortitionDB::get_block_snapshot_for_winning_stacks_block(
65126509
&sort_db.index_conn(),
65136510
&ic.context.chain_tip,
@@ -6537,7 +6534,7 @@ fn test_pox_fork_out_of_order() {
65376534

65386535
// load the block into staging
65396536
let block_hash = block.header.block_hash();
6540-
eprintln!("Block hash={}, ix={}", &block_hash, ix);
6537+
eprintln!("Block hash={block_hash}, ix={ix}");
65416538

65426539
assert_eq!(&tip.winning_stacks_block_hash, &block_hash);
65436540
stacks_blocks.push((tip.sortition_id.clone(), block.clone()));
@@ -6560,10 +6557,11 @@ fn test_pox_fork_out_of_order() {
65606557
assert_eq!(&pox_id.to_string(), "11111");
65616558
}
65626559

6560+
// Because we no longer continue processing without an anchor block, the blinded signer has not advanced.
65636561
{
65646562
let ic = sort_db_blind.index_handle_at_tip();
65656563
let pox_id = ic.get_pox_id().unwrap();
6566-
assert_eq!(&pox_id.to_string(), "11000");
6564+
assert_eq!(&pox_id.to_string(), "11");
65676565
}
65686566

65696567
// now, we reveal to the blinded coordinator, but out of order.
@@ -6586,38 +6584,27 @@ fn test_pox_fork_out_of_order() {
65866584
{
65876585
let ic = sort_db_blind.index_handle_at_tip();
65886586
let pox_id = ic.get_pox_id().unwrap();
6589-
assert_eq!(&pox_id.to_string(), "11110");
6587+
assert_eq!(&pox_id.to_string(), "1111");
65906588
}
65916589

65926590
let block_height = eval_at_chain_tip(path_blinded, &sort_db_blind, "block-height");
65936591
assert_eq!(block_height, Value::UInt(1));
65946592

65956593
// reveal [6-10]
6596-
for (_sort_id, block) in stacks_blocks[6..=10].iter() {
6597-
// cannot use sort_id from stacks_blocks, because the blinded coordinator
6598-
// has different sortition_id's for blocks 6-10 (because it's missing
6599-
// the 2nd anchor block).
6600-
let sort_id = SortitionDB::get_block_snapshot_for_winning_stacks_block(
6601-
&sort_db_blind.index_conn(),
6602-
&SortitionDB::get_canonical_sortition_tip(sort_db_blind.conn()).unwrap(),
6603-
&block.header.block_hash(),
6604-
)
6605-
.unwrap()
6606-
.unwrap()
6607-
.sortition_id;
6594+
for (sort_id, block) in stacks_blocks[6..=10].iter() {
66086595
reveal_block(
66096596
path_blinded,
66106597
&sort_db_blind,
66116598
&mut coord_blind,
6612-
&sort_id,
6599+
sort_id,
66136600
block,
66146601
);
66156602
}
66166603

66176604
{
66186605
let ic = sort_db_blind.index_handle_at_tip();
66196606
let pox_id = ic.get_pox_id().unwrap();
6620-
assert_eq!(&pox_id.to_string(), "11110");
6607+
assert_eq!(&pox_id.to_string(), "1111");
66216608
}
66226609

66236610
let block_height = eval_at_chain_tip(path_blinded, &sort_db_blind, "block-height");
@@ -6637,19 +6624,7 @@ fn test_pox_fork_out_of_order() {
66376624
);
66386625

66396626
// reveal [1-5]
6640-
for (_sort_id, block) in stacks_blocks[1..=5].iter() {
6641-
// cannot use sort_id from stacks_blocks, because the blinded coordinator
6642-
// has different sortition_id's for blocks 6-10 (because it's missing
6643-
// the 2nd anchor block).
6644-
let sort_id = SortitionDB::get_block_snapshot_for_winning_stacks_block(
6645-
&sort_db_blind.index_conn(),
6646-
&SortitionDB::get_canonical_sortition_tip(sort_db_blind.conn()).unwrap(),
6647-
&block.header.block_hash(),
6648-
)
6649-
.unwrap()
6650-
.unwrap()
6651-
.sortition_id;
6652-
6627+
for (sort_id, block) in stacks_blocks[1..=5].iter() {
66536628
// before processing the last of these blocks, the stacks_block[9] should still
66546629
// be the canonical tip
66556630
let block_hash = eval_at_chain_tip(
@@ -6670,7 +6645,7 @@ fn test_pox_fork_out_of_order() {
66706645
path_blinded,
66716646
&sort_db_blind,
66726647
&mut coord_blind,
6673-
&sort_id,
6648+
sort_id,
66746649
block,
66756650
);
66766651
}
@@ -6685,24 +6660,12 @@ fn test_pox_fork_out_of_order() {
66856660
assert_eq!(block_height, Value::UInt(6));
66866661

66876662
// reveal [11-14]
6688-
for (_sort_id, block) in stacks_blocks[11..].iter() {
6689-
// cannot use sort_id from stacks_blocks, because the blinded coordinator
6690-
// has different sortition_id's for blocks 6-10 (because it's missing
6691-
// the 2nd anchor block).
6692-
let sort_id = SortitionDB::get_block_snapshot_for_winning_stacks_block(
6693-
&sort_db_blind.index_conn(),
6694-
&SortitionDB::get_canonical_sortition_tip(sort_db_blind.conn()).unwrap(),
6695-
&block.header.block_hash(),
6696-
)
6697-
.unwrap()
6698-
.unwrap()
6699-
.sortition_id;
6700-
6663+
for (sort_id, block) in stacks_blocks[11..].iter() {
67016664
reveal_block(
67026665
path_blinded,
67036666
&sort_db_blind,
67046667
&mut coord_blind,
6705-
&sort_id,
6668+
sort_id,
67066669
block,
67076670
);
67086671
}
@@ -6765,6 +6728,8 @@ fn reveal_block<T: BlockEventDispatcher, N: CoordinatorNotices, U: RewardSetProv
67656728
.unwrap();
67666729
preprocess_block(&mut chainstate, sort_db, &sortition, block.clone());
67676730
coord.handle_new_stacks_block().unwrap();
6731+
// Force the coordinator to wake up and advance
6732+
coord.handle_new_burnchain_block().unwrap();
67686733
}
67696734

67706735
fn preprocess_block(

0 commit comments

Comments
 (0)