Skip to content

Commit c7a45eb

Browse files
committed
chore: refactor the sip-031 check into finish_block()
1 parent 1330f08 commit c7a45eb

File tree

2 files changed

+53
-33
lines changed

2 files changed

+53
-33
lines changed

stackslib/src/chainstate/nakamoto/miner.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,14 @@ impl NakamotoBlockBuilder {
483483
clarity_tx: &mut ClarityTx,
484484
burn_block_height: u32,
485485
) -> NakamotoBlock {
486-
NakamotoChainState::finish_block(clarity_tx, self.matured_miner_rewards_opt.as_ref())
487-
.expect("FATAL: call to `finish_block` failed");
488-
// if coinbase_tx is defined, we are at the start of a new tenure
489-
if self.coinbase_tx.is_some() {
490-
NakamotoChainState::sip_031_mint_and_transfer_on_new_tenure(
491-
clarity_tx,
492-
burn_block_height,
493-
);
494-
}
486+
let is_new_tenure = self.coinbase_tx.is_some();
487+
NakamotoChainState::finish_block(
488+
clarity_tx,
489+
self.matured_miner_rewards_opt.as_ref(),
490+
is_new_tenure,
491+
burn_block_height,
492+
)
493+
.expect("FATAL: call to `finish_block` failed");
495494
self.finalize_block(clarity_tx)
496495
}
497496

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,13 @@ impl MaturedMinerPaymentSchedules {
524524
}
525525
}
526526

527+
/// Struct for the transaction events associated with
528+
/// any operations handled in finish_block()
529+
pub struct FinishBlockEvents {
530+
pub lockup_events: Vec<StacksTransactionEvent>,
531+
pub sip31_event: Option<StacksTransactionEvent>,
532+
}
533+
527534
/// Struct containing information about the miners assigned in the
528535
/// .miners stackerdb config
529536
pub struct MinersDBInformation {
@@ -4350,7 +4357,9 @@ impl NakamotoChainState {
43504357
pub fn finish_block(
43514358
clarity_tx: &mut ClarityTx,
43524359
miner_payouts: Option<&MaturedMinerRewards>,
4353-
) -> Result<Vec<StacksTransactionEvent>, ChainstateError> {
4360+
new_tenure: bool,
4361+
chain_tip_burn_header_height: u32,
4362+
) -> Result<FinishBlockEvents, ChainstateError> {
43544363
// add miner payments
43554364
if let Some(rewards) = miner_payouts {
43564365
// grant in order by miner, then users
@@ -4369,7 +4378,17 @@ impl NakamotoChainState {
43694378

43704379
clarity_tx.increment_ustx_liquid_supply(new_unlocked_ustx);
43714380

4372-
Ok(lockup_events)
4381+
// process SIP-031 mint/transfers
4382+
let sip31_event = if new_tenure {
4383+
Self::sip_031_mint_and_transfer_on_new_tenure(clarity_tx, chain_tip_burn_header_height)
4384+
} else {
4385+
None
4386+
};
4387+
4388+
Ok(FinishBlockEvents {
4389+
lockup_events,
4390+
sip31_event,
4391+
})
43734392
}
43744393

43754394
/// Verify that the PoX bitvector from the block header is consistent with the block-commit's
@@ -4792,15 +4811,22 @@ impl NakamotoChainState {
47924811
.map(|matured_miner_rewards| matured_miner_rewards.consolidate())
47934812
.unwrap_or_default();
47944813

4795-
let mut lockup_events =
4796-
match Self::finish_block(&mut clarity_tx, matured_miner_rewards_opt.as_ref()) {
4797-
Err(ChainstateError::InvalidStacksBlock(e)) => {
4798-
clarity_tx.rollback_block();
4799-
return Err(ChainstateError::InvalidStacksBlock(e));
4800-
}
4801-
Err(e) => return Err(e),
4802-
Ok(lockup_events) => lockup_events,
4803-
};
4814+
let FinishBlockEvents {
4815+
mut lockup_events,
4816+
sip31_event,
4817+
} = match Self::finish_block(
4818+
&mut clarity_tx,
4819+
matured_miner_rewards_opt.as_ref(),
4820+
new_tenure,
4821+
chain_tip_burn_header_height,
4822+
) {
4823+
Err(ChainstateError::InvalidStacksBlock(e)) => {
4824+
clarity_tx.rollback_block();
4825+
return Err(ChainstateError::InvalidStacksBlock(e));
4826+
}
4827+
Err(e) => return Err(e),
4828+
Ok(lockup_events) => lockup_events,
4829+
};
48044830

48054831
// If any, append lockups events to the coinbase receipt
48064832
if let Some(receipt) = tx_receipts.get_mut(0) {
@@ -4829,21 +4855,16 @@ impl NakamotoChainState {
48294855
}
48304856
}
48314857

4832-
if new_tenure {
4833-
if let Some(event) = Self::sip_031_mint_and_transfer_on_new_tenure(
4834-
&mut clarity_tx,
4835-
chain_tip_burn_header_height,
4836-
) {
4837-
// for sip-031 we are safe in assuming coinbase is at index 1
4838-
if let Some(receipt) = tx_receipts.get_mut(1) {
4839-
if receipt.is_coinbase_tx() {
4840-
receipt.events.push(event);
4841-
} else {
4842-
error!("Unable to attach SIP-031 mint events, block's second transaction is not a coinbase transaction")
4843-
}
4858+
if let Some(event) = sip31_event {
4859+
// for sip-031 we are safe in assuming coinbase is at index 1
4860+
if let Some(receipt) = tx_receipts.get_mut(1) {
4861+
if receipt.is_coinbase_tx() {
4862+
receipt.events.push(event);
48444863
} else {
4845-
error!("Unable to attach SIP-031 mint events, block's second transaction not available")
4864+
error!("Unable to attach SIP-031 mint events, block's second transaction is not a coinbase transaction")
48464865
}
4866+
} else {
4867+
error!("Unable to attach SIP-031 mint events, block's second transaction not available")
48474868
}
48484869
}
48494870

0 commit comments

Comments
 (0)