Skip to content

Commit 91952ac

Browse files
committed
feat: Refactor replay_block() and use it to validate mock mined blocks
1 parent 792e1c1 commit 91952ac

File tree

5 files changed

+284
-166
lines changed

5 files changed

+284
-166
lines changed

stackslib/src/chainstate/stacks/block.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@ impl StacksBlock {
651651
pub fn has_microblock_parent(&self) -> bool {
652652
self.header.has_microblock_parent()
653653
}
654+
655+
/// Returns size in bytes of `StacksMessageCodec` representation
656+
/// Note that this will serialize the block, so don't call if there is a better way to get block size
657+
pub fn block_size(&self) -> Result<usize, codec_error> {
658+
let mut buf = vec![];
659+
self.consensus_serialize(&mut buf)?;
660+
Ok(buf.len())
661+
}
654662
}
655663

656664
impl StacksMessageCodec for StacksMicroblockHeader {

stackslib/src/chainstate/stacks/db/blocks.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3716,30 +3716,44 @@ impl StacksChainState {
37163716
blocks_conn: &DBConn,
37173717
staging_block: &StagingBlock,
37183718
) -> Result<Option<Vec<StacksMicroblock>>, Error> {
3719-
if staging_block.parent_microblock_hash == EMPTY_MICROBLOCK_PARENT_HASH
3720-
&& staging_block.parent_microblock_seq == 0
3721-
{
3719+
Self::inner_find_parent_microblock_stream(
3720+
blocks_conn,
3721+
&staging_block.anchored_block_hash,
3722+
&staging_block.parent_anchored_block_hash,
3723+
&staging_block.parent_consensus_hash,
3724+
&staging_block.parent_microblock_hash,
3725+
staging_block.parent_microblock_seq,
3726+
)
3727+
}
3728+
3729+
/// Allow `find_parent_microblock_stream()` to be called without `StagingBlock`
3730+
pub fn inner_find_parent_microblock_stream(
3731+
blocks_conn: &DBConn,
3732+
anchored_block_hash: &BlockHeaderHash,
3733+
parent_anchored_block_hash: &BlockHeaderHash,
3734+
parent_consensus_hash: &ConsensusHash,
3735+
parent_microblock_hash: &BlockHeaderHash,
3736+
parent_microblock_seq: u16,
3737+
) -> Result<Option<Vec<StacksMicroblock>>, Error> {
3738+
if *parent_microblock_hash == EMPTY_MICROBLOCK_PARENT_HASH && parent_microblock_seq == 0 {
37223739
// no parent microblocks, ever
37233740
return Ok(Some(vec![]));
37243741
}
37253742

37263743
// find the microblock stream fork that this block confirms
37273744
match StacksChainState::load_microblock_stream_fork(
37283745
blocks_conn,
3729-
&staging_block.parent_consensus_hash,
3730-
&staging_block.parent_anchored_block_hash,
3731-
&staging_block.parent_microblock_hash,
3746+
parent_consensus_hash,
3747+
parent_anchored_block_hash,
3748+
parent_microblock_hash,
37323749
)? {
37333750
Some(microblocks) => {
37343751
return Ok(Some(microblocks));
37353752
}
37363753
None => {
37373754
// parent microblocks haven't arrived yet, or there are none
37383755
debug!(
3739-
"No parent microblock stream for {}: expected a stream with tail {},{}",
3740-
staging_block.anchored_block_hash,
3741-
staging_block.parent_microblock_hash,
3742-
staging_block.parent_microblock_seq
3756+
"No parent microblock stream for {anchored_block_hash}: expected a stream with tail {parent_microblock_hash},{parent_microblock_seq}",
37433757
);
37443758
return Ok(None);
37453759
}

stackslib/src/chainstate/stacks/miner.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ use crate::net::Error as net_error;
7373
pub struct AssembledAnchorBlock {
7474
/// Consensus hash of the parent Stacks block
7575
pub parent_consensus_hash: ConsensusHash,
76+
/// Consensus hash this Stacks block
77+
pub consensus_hash: ConsensusHash,
7678
/// Burnchain tip's block hash when we finished mining
7779
pub my_burn_hash: BurnchainHeaderHash,
7880
/// Burnchain tip's block height when we finished mining

0 commit comments

Comments
 (0)