Skip to content

Commit 111595a

Browse files
committed
feat: add marf_hash to ExpectedBlockOutput
1 parent 12521f0 commit 111595a

5 files changed

+279
-134
lines changed

stackslib/src/chainstate/tests/consensus.rs

Lines changed: 120 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ pub struct ExpectedTransactionOutput {
166166
/// Represents the expected outputs for a block's execution.
167167
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
168168
pub struct ExpectedBlockOutput {
169+
/// The expected block marf
170+
pub marf_hash: TrieHash,
169171
/// The expected outputs for each transaction, in input order.
170172
pub transactions: Vec<ExpectedTransactionOutput>,
171173
/// The total execution cost of the block.
@@ -183,8 +185,11 @@ pub enum ExpectedResult {
183185
Failure(String),
184186
}
185187

186-
impl From<Result<StacksEpochReceipt, ChainstateError>> for ExpectedResult {
187-
fn from(result: Result<StacksEpochReceipt, ChainstateError>) -> Self {
188+
impl ExpectedResult {
189+
fn create_from(
190+
result: Result<StacksEpochReceipt, ChainstateError>,
191+
marf_hash: TrieHash,
192+
) -> Self {
188193
match result {
189194
Ok(epoch_receipt) => {
190195
let transactions: Vec<ExpectedTransactionOutput> = epoch_receipt
@@ -197,6 +202,7 @@ impl From<Result<StacksEpochReceipt, ChainstateError>> for ExpectedResult {
197202
.collect();
198203
let total_block_cost = epoch_receipt.anchored_block_cost.clone();
199204
ExpectedResult::Success(ExpectedBlockOutput {
205+
marf_hash,
200206
transactions,
201207
total_block_cost,
202208
})
@@ -367,14 +373,12 @@ impl ConsensusTest<'_> {
367373
);
368374

369375
debug!("--------- Appended block: {} ---------", result.is_ok());
370-
results.push(
371-
result
372-
.map(|(receipt, clarity_commit, _, _)| {
373-
clarity_commit.commit();
374-
receipt
375-
})
376-
.into(),
377-
);
376+
let remapped_result = result.map(|(receipt, clarity_commit, _, _)| {
377+
clarity_commit.commit();
378+
receipt
379+
});
380+
let expected_marf = nakamoto_block.header.state_index_root;
381+
results.push(ExpectedResult::create_from(remapped_result, expected_marf));
378382
chainstate_tx.commit().unwrap();
379383
}
380384

@@ -430,15 +434,11 @@ impl ConsensusTest<'_> {
430434

431435
// Set the MARF root hash or use an all-zero hash in case of failure.
432436
// NOTE: It is expected to fail when trying computing the marf for invalid block/transactions.
433-
/*
434437
let marf_result = self.compute_block_marf_root_hash(block.header.timestamp, &block.txs);
435438
block.header.state_index_root = match marf_result {
436439
Ok(marf) => marf,
437440
Err(_) => TrieHash::from_bytes(&[0; 32]).unwrap(),
438441
};
439-
*/
440-
441-
block.header.state_index_root = TrieHash::from_bytes(&[0; 32]).unwrap();
442442

443443
self.chain.miner.sign_nakamoto_block(&mut block);
444444
let mut signers = self.chain.config.test_signers.clone().unwrap_or_default();
@@ -517,9 +517,7 @@ impl ConsensusTest<'_> {
517517
NakamotoChainState::finish_block(clarity_tx, None, false, burn_header_height)
518518
.map_err(|e| e.to_string())?;
519519

520-
let trie_hash = clarity_tx.seal();
521-
//clarity_tx.rollback_block();
522-
Ok(trie_hash)
520+
Ok(clarity_tx.seal())
523521
}
524522
}
525523

@@ -559,42 +557,6 @@ fn test_append_empty_blocks() {
559557
insta::assert_ron_snapshot!(result);
560558
}
561559

562-
//#[test]
563-
fn test_append_state_index_root_mismatches() {
564-
let mut epoch_blocks = HashMap::new();
565-
epoch_blocks.insert(
566-
StacksEpochId::Epoch30,
567-
vec![TestBlock {
568-
transactions: vec![],
569-
}],
570-
);
571-
epoch_blocks.insert(
572-
StacksEpochId::Epoch31,
573-
vec![TestBlock {
574-
transactions: vec![],
575-
}],
576-
);
577-
epoch_blocks.insert(
578-
StacksEpochId::Epoch32,
579-
vec![TestBlock {
580-
transactions: vec![],
581-
}],
582-
);
583-
epoch_blocks.insert(
584-
StacksEpochId::Epoch33,
585-
vec![TestBlock {
586-
transactions: vec![],
587-
}],
588-
);
589-
590-
let test_vector = ConsensusTestVector {
591-
initial_balances: vec![],
592-
epoch_blocks,
593-
};
594-
let result = ConsensusTest::new(function_name!(), test_vector).run();
595-
insta::assert_ron_snapshot!(result);
596-
}
597-
598560
#[test]
599561
fn test_append_stx_transfers_success() {
600562
let sender_privks = [
@@ -759,38 +721,110 @@ fn test_append_block_with_contract_upload_success() {
759721
};
760722

761723
let result = ConsensusTest::new(function_name!(), test_vector).run();
762-
// Example of expecting the same result across all blocks
763-
insta::allow_duplicates! {
764-
for res in result {
765-
// Example of inline snapshot
766-
insta::assert_ron_snapshot!(res, @r"
767-
Success(ExpectedBlockOutput(
768-
transactions: [
769-
ExpectedTransactionOutput(
770-
return_type: Response(ResponseData(
771-
committed: true,
772-
data: Bool(true),
773-
)),
774-
cost: ExecutionCost(
775-
write_length: 13,
776-
write_count: 2,
777-
read_length: 1,
778-
read_count: 1,
779-
runtime: 8114,
780-
),
781-
),
782-
],
783-
total_block_cost: ExecutionCost(
784-
write_length: 13,
785-
write_count: 2,
786-
read_length: 1,
787-
read_count: 1,
788-
runtime: 8114,
789-
),
790-
))
791-
");
792-
}
793-
}
724+
insta::assert_ron_snapshot!(result, @r#"
725+
[
726+
Success(ExpectedBlockOutput(
727+
marf_hash: "b45acd35f4c48a834a2f898ca8bb6c48416ac6bec9d8a3f3662b61ab97b1edde",
728+
transactions: [
729+
ExpectedTransactionOutput(
730+
return_type: Response(ResponseData(
731+
committed: true,
732+
data: Bool(true),
733+
)),
734+
cost: ExecutionCost(
735+
write_length: 13,
736+
write_count: 2,
737+
read_length: 1,
738+
read_count: 1,
739+
runtime: 8114,
740+
),
741+
),
742+
],
743+
total_block_cost: ExecutionCost(
744+
write_length: 13,
745+
write_count: 2,
746+
read_length: 1,
747+
read_count: 1,
748+
runtime: 8114,
749+
),
750+
)),
751+
Success(ExpectedBlockOutput(
752+
marf_hash: "521d75234ec6c64f68648b6b0f6f385d89b58efb581211a411e0e88aa71f3371",
753+
transactions: [
754+
ExpectedTransactionOutput(
755+
return_type: Response(ResponseData(
756+
committed: true,
757+
data: Bool(true),
758+
)),
759+
cost: ExecutionCost(
760+
write_length: 13,
761+
write_count: 2,
762+
read_length: 1,
763+
read_count: 1,
764+
runtime: 8114,
765+
),
766+
),
767+
],
768+
total_block_cost: ExecutionCost(
769+
write_length: 13,
770+
write_count: 2,
771+
read_length: 1,
772+
read_count: 1,
773+
runtime: 8114,
774+
),
775+
)),
776+
Success(ExpectedBlockOutput(
777+
marf_hash: "511e1cc37e83ef3de4ea56962574d6ddd2d8840d24d9238f19eee5a35127df6a",
778+
transactions: [
779+
ExpectedTransactionOutput(
780+
return_type: Response(ResponseData(
781+
committed: true,
782+
data: Bool(true),
783+
)),
784+
cost: ExecutionCost(
785+
write_length: 13,
786+
write_count: 2,
787+
read_length: 1,
788+
read_count: 1,
789+
runtime: 8114,
790+
),
791+
),
792+
],
793+
total_block_cost: ExecutionCost(
794+
write_length: 13,
795+
write_count: 2,
796+
read_length: 1,
797+
read_count: 1,
798+
runtime: 8114,
799+
),
800+
)),
801+
Success(ExpectedBlockOutput(
802+
marf_hash: "3520c2dd96f7d91e179c4dcd00f3c49c16d6ec21434fb16921922558282eab26",
803+
transactions: [
804+
ExpectedTransactionOutput(
805+
return_type: Response(ResponseData(
806+
committed: true,
807+
data: Bool(true),
808+
)),
809+
cost: ExecutionCost(
810+
write_length: 13,
811+
write_count: 2,
812+
read_length: 1,
813+
read_count: 1,
814+
runtime: 8114,
815+
),
816+
),
817+
],
818+
total_block_cost: ExecutionCost(
819+
write_length: 13,
820+
write_count: 2,
821+
read_length: 1,
822+
read_count: 1,
823+
runtime: 8114,
824+
),
825+
)),
826+
]
827+
"#);
794828
}
795829

796830
#[test]
@@ -856,9 +890,5 @@ fn test_append_block_with_contract_call_success() {
856890
};
857891

858892
let result = ConsensusTest::new(function_name!(), test_vector).run();
859-
insta::allow_duplicates! {
860-
for res in result {
861-
insta::assert_ron_snapshot!(res);
862-
}
863-
}
893+
insta::assert_ron_snapshot!(result);
864894
}

0 commit comments

Comments
 (0)