Skip to content

Commit 2bea12c

Browse files
committed
fix: use parent_burn_block_hash to 'crawl' forked blocks
1 parent 8d7f7a8 commit 2bea12c

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,34 @@ impl StacksMessageCodec for NakamotoBlockVote {
7272
}
7373
}
7474

75+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
76+
/// Struct for storing information about a burn block
77+
pub struct BurnBlockInfo {
78+
/// The hash of the burn block
79+
pub block_hash: BurnchainHeaderHash,
80+
/// The height of the burn block
81+
pub block_height: u64,
82+
/// The consensus hash of the burn block
83+
pub consensus_hash: ConsensusHash,
84+
/// The hash of the parent burn block
85+
pub parent_burn_block_hash: BurnchainHeaderHash,
86+
}
87+
88+
impl FromRow<BurnBlockInfo> for BurnBlockInfo {
89+
fn from_row(row: &rusqlite::Row) -> Result<Self, DBError> {
90+
let block_hash: BurnchainHeaderHash = row.get(0)?;
91+
let block_height: u64 = row.get(1)?;
92+
let consensus_hash: ConsensusHash = row.get(2)?;
93+
let parent_burn_block_hash: BurnchainHeaderHash = row.get(3)?;
94+
Ok(BurnBlockInfo {
95+
block_hash,
96+
block_height,
97+
consensus_hash,
98+
parent_burn_block_hash,
99+
})
100+
}
101+
}
102+
75103
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
76104
/// Store extra version-specific info in `BlockInfo`
77105
pub enum ExtraBlockInfo {
@@ -1068,6 +1096,26 @@ impl SignerDb {
10681096
Ok(Some(receive_time))
10691097
}
10701098

1099+
/// Lookup the burn block for a given burn block hash.
1100+
pub fn get_burn_block_by_hash(
1101+
&self,
1102+
burn_block_hash: &BurnchainHeaderHash,
1103+
) -> Result<BurnBlockInfo, DBError> {
1104+
let query =
1105+
"SELECT block_hash, block_height, consensus_hash, parent_burn_block_hash FROM burn_blocks WHERE block_hash = ?";
1106+
let args = params![burn_block_hash];
1107+
1108+
query_row(&self.db, query, args)?.ok_or(DBError::NotFoundError)
1109+
}
1110+
1111+
/// Lookup the burn block for a given consensus hash.
1112+
pub fn get_burn_block_by_ch(&self, ch: &ConsensusHash) -> Result<BurnBlockInfo, DBError> {
1113+
let query = "SELECT block_hash, block_height, consensus_hash, parent_burn_block_hash FROM burn_blocks WHERE consensus_hash = ?";
1114+
let args = params![ch];
1115+
1116+
query_row(&self.db, query, args)?.ok_or(DBError::NotFoundError)
1117+
}
1118+
10711119
/// Insert or replace a block into the database.
10721120
/// Preserves the `broadcast` column if replacing an existing block.
10731121
pub fn insert_block(&mut self, block_info: &BlockInfo) -> Result<(), DBError> {

stacks-signer/src/v0/signer_state.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -749,24 +749,22 @@ impl LocalStateMachine {
749749
"prior_state_machine.burn_block" => %prior_state_machine.burn_block,
750750
);
751751
// Determine the tenures that were forked
752-
let mut sortition_info =
753-
client.get_sortition_by_consensus_hash(&prior_state_machine.burn_block)?;
752+
let mut parent_burn_block_info =
753+
db.get_burn_block_by_ch(&prior_state_machine.burn_block)?;
754754
let last_forked_tenure = prior_state_machine.burn_block;
755755
let mut first_forked_tenure = prior_state_machine.burn_block;
756756
let mut forked_tenures = vec![(
757757
prior_state_machine.burn_block,
758758
prior_state_machine.burn_block_height,
759759
)];
760-
while sortition_info.burn_block_height > expected_burn_block.burn_block_height {
761-
let Some(stacks_parent_ch) = sortition_info.stacks_parent_ch else {
762-
info!("No stacks parent ch found for sortition info";
763-
"sortition_info" => ?sortition_info,
764-
);
765-
break;
766-
};
767-
sortition_info = client.get_sortition_by_consensus_hash(&stacks_parent_ch)?;
768-
first_forked_tenure = sortition_info.consensus_hash;
769-
forked_tenures.push((stacks_parent_ch, sortition_info.burn_block_height));
760+
while parent_burn_block_info.block_height > expected_burn_block.burn_block_height {
761+
parent_burn_block_info =
762+
db.get_burn_block_by_hash(&parent_burn_block_info.parent_burn_block_hash)?;
763+
first_forked_tenure = parent_burn_block_info.consensus_hash;
764+
forked_tenures.push((
765+
parent_burn_block_info.consensus_hash,
766+
parent_burn_block_info.block_height,
767+
));
770768
}
771769
let fork_info =
772770
client.get_tenure_forking_info(&first_forked_tenure, &last_forked_tenure)?;

0 commit comments

Comments
 (0)