Skip to content

Commit 12521f0

Browse files
committed
refactor: aac update test marf computation
1 parent ef3aa82 commit 12521f0

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

stackslib/src/chainstate/tests/consensus.rs

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::burnchains::PoxConstants;
3838
use crate::chainstate::burn::db::sortdb::SortitionDB;
3939
use crate::chainstate::nakamoto::{NakamotoBlock, NakamotoBlockHeader, NakamotoChainState};
4040
use crate::chainstate::stacks::boot::RewardSet;
41-
use crate::chainstate::stacks::db::{StacksChainState, StacksEpochReceipt};
41+
use crate::chainstate::stacks::db::{ClarityTx, StacksChainState, StacksEpochReceipt};
4242
use crate::chainstate::stacks::{
4343
Error as ChainstateError, StacksTransaction, TenureChangeCause, MINER_BLOCK_CONSENSUS_HASH,
4444
MINER_BLOCK_HEADER_HASH,
@@ -209,8 +209,6 @@ impl From<Result<StacksEpochReceipt, ChainstateError>> for ExpectedResult {
209209
/// Represents a block to be appended in a test and its expected result.
210210
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
211211
pub struct TestBlock {
212-
/// Hex representation of the MARF hash for block construction.
213-
pub marf_hash: String,
214212
/// Transactions to include in the block
215213
pub transactions: Vec<StacksTransaction>,
216214
}
@@ -430,13 +428,17 @@ impl ConsensusTest<'_> {
430428
};
431429
block.header.tx_merkle_root = tx_merkle_root;
432430

433-
// Set the MARF root hash: compute it for success cases,
434-
// or use an all-zero hash for failure cases.
435-
block.header.state_index_root = if test_block.is_success() {
436-
self.compute_block_marf_root_hash(block.header.timestamp, &block.txs)
437-
} else {
438-
TrieHash::from_bytes(&[0; 32]).unwrap()
431+
// Set the MARF root hash or use an all-zero hash in case of failure.
432+
// NOTE: It is expected to fail when trying computing the marf for invalid block/transactions.
433+
/*
434+
let marf_result = self.compute_block_marf_root_hash(block.header.timestamp, &block.txs);
435+
block.header.state_index_root = match marf_result {
436+
Ok(marf) => marf,
437+
Err(_) => TrieHash::from_bytes(&[0; 32]).unwrap(),
439438
};
439+
*/
440+
441+
block.header.state_index_root = TrieHash::from_bytes(&[0; 32]).unwrap();
440442

441443
self.chain.miner.sign_nakamoto_block(&mut block);
442444
let mut signers = self.chain.config.test_signers.clone().unwrap_or_default();
@@ -457,7 +459,7 @@ impl ConsensusTest<'_> {
457459
&mut self,
458460
block_time: u64,
459461
block_txs: &Vec<StacksTransaction>,
460-
) -> TrieHash {
462+
) -> Result<TrieHash, String> {
461463
let node = self.chain.stacks_node.as_mut().unwrap();
462464
let sortdb = self.chain.sortdb.as_ref().unwrap();
463465
let burndb_conn = sortdb.index_handle_at_tip();
@@ -479,7 +481,26 @@ impl ConsensusTest<'_> {
479481
&MINER_BLOCK_CONSENSUS_HASH,
480482
&MINER_BLOCK_HEADER_HASH,
481483
);
484+
let result = Self::inner_compute_block_marf_root_hash(
485+
&mut clarity_tx,
486+
block_time,
487+
block_txs,
488+
chain_tip.burn_header_height,
489+
);
490+
clarity_tx.rollback_block();
491+
return result;
492+
}
482493

494+
/// This is where the real MARF computation happens.
495+
/// It is extrapolated into an _inner_ method to simplify rollback handling,
496+
/// ensuring that rollback can be applied consistently on both success and failure
497+
/// in the _outer_ method.
498+
fn inner_compute_block_marf_root_hash(
499+
clarity_tx: &mut ClarityTx,
500+
block_time: u64,
501+
block_txs: &Vec<StacksTransaction>,
502+
burn_header_height: u32,
503+
) -> Result<TrieHash, String> {
483504
clarity_tx
484505
.connection()
485506
.as_free_transaction(|clarity_tx_conn| {
@@ -488,22 +509,17 @@ impl ConsensusTest<'_> {
488509
Ok(())
489510
})
490511
})
491-
.expect("MARF: Failure on block metadata setup!");
512+
.map_err(|e| e.to_string())?;
492513

493-
StacksChainState::process_block_transactions(&mut clarity_tx, block_txs, 0)
494-
.expect("MARF: Failure on processing block transactions!");
514+
StacksChainState::process_block_transactions(clarity_tx, block_txs, 0)
515+
.map_err(|e| e.to_string())?;
495516

496-
NakamotoChainState::finish_block(
497-
&mut clarity_tx,
498-
None,
499-
false,
500-
chain_tip.burn_header_height,
501-
)
502-
.expect("MARF: Failure on finishing block!");
517+
NakamotoChainState::finish_block(clarity_tx, None, false, burn_header_height)
518+
.map_err(|e| e.to_string())?;
503519

504520
let trie_hash = clarity_tx.seal();
505-
clarity_tx.rollback_block();
506-
return trie_hash;
521+
//clarity_tx.rollback_block();
522+
Ok(trie_hash)
507523
}
508524
}
509525

@@ -513,28 +529,24 @@ fn test_append_empty_blocks() {
513529
epoch_blocks.insert(
514530
StacksEpochId::Epoch30,
515531
vec![TestBlock {
516-
marf_hash: "f1934080b22ef0192cfb39710690e7cb0efa9cff950832b33544bde3aa1484a5".into(),
517532
transactions: vec![],
518533
}],
519534
);
520535
epoch_blocks.insert(
521536
StacksEpochId::Epoch31,
522537
vec![TestBlock {
523-
marf_hash: "a05f1383613215f5789eb977e4c62dfbb789d90964e14865d109375f7f6dc3cf".into(),
524538
transactions: vec![],
525539
}],
526540
);
527541
epoch_blocks.insert(
528542
StacksEpochId::Epoch32,
529543
vec![TestBlock {
530-
marf_hash: "c17829daff8746329c65ae658f4087519c6a8bd8c7f21e51644ddbc9c010390f".into(),
531544
transactions: vec![],
532545
}],
533546
);
534547
epoch_blocks.insert(
535548
StacksEpochId::Epoch33,
536549
vec![TestBlock {
537-
marf_hash: "23ecbcb91cac914ba3994a15f3ea7189bcab4e9762530cd0e6c7d237fcd6dc78".into(),
538550
transactions: vec![],
539551
}],
540552
);
@@ -547,34 +559,30 @@ fn test_append_empty_blocks() {
547559
insta::assert_ron_snapshot!(result);
548560
}
549561

550-
#[test]
562+
//#[test]
551563
fn test_append_state_index_root_mismatches() {
552564
let mut epoch_blocks = HashMap::new();
553565
epoch_blocks.insert(
554566
StacksEpochId::Epoch30,
555567
vec![TestBlock {
556-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
557568
transactions: vec![],
558569
}],
559570
);
560571
epoch_blocks.insert(
561572
StacksEpochId::Epoch31,
562573
vec![TestBlock {
563-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
564574
transactions: vec![],
565575
}],
566576
);
567577
epoch_blocks.insert(
568578
StacksEpochId::Epoch32,
569579
vec![TestBlock {
570-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
571580
transactions: vec![],
572581
}],
573582
);
574583
epoch_blocks.insert(
575584
StacksEpochId::Epoch33,
576585
vec![TestBlock {
577-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
578586
transactions: vec![],
579587
}],
580588
);
@@ -620,28 +628,24 @@ fn test_append_stx_transfers_success() {
620628
epoch_blocks.insert(
621629
StacksEpochId::Epoch30,
622630
vec![TestBlock {
623-
marf_hash: "63ea49669d2216ebc7e4f8b5e1cd2c99b8aff9806794adf87dcf709c0a244798".into(),
624631
transactions: transactions.clone(),
625632
}],
626633
);
627634
epoch_blocks.insert(
628635
StacksEpochId::Epoch31,
629636
vec![TestBlock {
630-
marf_hash: "7fc538e605a4a353871c4a655ae850fe9a70c3875b65f2bb42ea3bef5effed2c".into(),
631637
transactions: transactions.clone(),
632638
}],
633639
);
634640
epoch_blocks.insert(
635641
StacksEpochId::Epoch32,
636642
vec![TestBlock {
637-
marf_hash: "4d5c9a6d07806ac5006137de22b083de66fff7119143dd5cd92e4a457d66e028".into(),
638643
transactions: transactions.clone(),
639644
}],
640645
);
641646
epoch_blocks.insert(
642647
StacksEpochId::Epoch33,
643648
vec![TestBlock {
644-
marf_hash: "66eed8c0ab31db111a5adcc83d38a7004c6e464e3b9fb9f52ec589bc6d5f2d32".into(),
645649
transactions: transactions.clone(),
646650
}],
647651
);
@@ -678,28 +682,24 @@ fn test_append_chainstate_error_expression_stack_depth_too_deep() {
678682
epoch_blocks.insert(
679683
StacksEpochId::Epoch30,
680684
vec![TestBlock {
681-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
682685
transactions: vec![tx.clone()],
683686
}],
684687
);
685688
epoch_blocks.insert(
686689
StacksEpochId::Epoch31,
687690
vec![TestBlock {
688-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
689691
transactions: vec![tx.clone()],
690692
}],
691693
);
692694
epoch_blocks.insert(
693695
StacksEpochId::Epoch32,
694696
vec![TestBlock {
695-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
696697
transactions: vec![tx.clone()],
697698
}],
698699
);
699700
epoch_blocks.insert(
700701
StacksEpochId::Epoch33,
701702
vec![TestBlock {
702-
marf_hash: "0000000000000000000000000000000000000000000000000000000000000000".into(),
703703
transactions: vec![tx.clone()],
704704
}],
705705
);
@@ -732,28 +732,24 @@ fn test_append_block_with_contract_upload_success() {
732732
epoch_blocks.insert(
733733
StacksEpochId::Epoch30,
734734
vec![TestBlock {
735-
marf_hash: "b45acd35f4c48a834a2f898ca8bb6c48416ac6bec9d8a3f3662b61ab97b1edde".into(),
736735
transactions: vec![tx.clone()],
737736
}],
738737
);
739738
epoch_blocks.insert(
740739
StacksEpochId::Epoch31,
741740
vec![TestBlock {
742-
marf_hash: "521d75234ec6c64f68648b6b0f6f385d89b58efb581211a411e0e88aa71f3371".into(),
743741
transactions: vec![tx.clone()],
744742
}],
745743
);
746744
epoch_blocks.insert(
747745
StacksEpochId::Epoch32,
748746
vec![TestBlock {
749-
marf_hash: "511e1cc37e83ef3de4ea56962574d6ddd2d8840d24d9238f19eee5a35127df6a".into(),
750747
transactions: vec![tx.clone()],
751748
}],
752749
);
753750
epoch_blocks.insert(
754751
StacksEpochId::Epoch33,
755752
vec![TestBlock {
756-
marf_hash: "3520c2dd96f7d91e179c4dcd00f3c49c16d6ec21434fb16921922558282eab26".into(),
757753
transactions: vec![tx.clone()],
758754
}],
759755
);
@@ -829,31 +825,27 @@ fn test_append_block_with_contract_call_success() {
829825
epoch_blocks.insert(
830826
StacksEpochId::Epoch30,
831827
vec![TestBlock {
832-
marf_hash: "186c8e49bcfc59bb67ed22f031f009a44681f296392e0f92bed520918ba463ae".into(),
833828
transactions: vec![tx_contract_deploy.clone(), tx_contract_call.clone()],
834829
}],
835830
);
836831

837832
epoch_blocks.insert(
838833
StacksEpochId::Epoch31,
839834
vec![TestBlock {
840-
marf_hash: "ad23713f072473cad6a32125ed5fa822bb62bbfae8ed2302209c12d2f1958128".into(),
841835
transactions: vec![tx_contract_deploy.clone(), tx_contract_call.clone()],
842836
}],
843837
);
844838

845839
epoch_blocks.insert(
846840
StacksEpochId::Epoch32,
847841
vec![TestBlock {
848-
marf_hash: "021bd30b09b5ac6ff34abd11f05244a966af937b584b1752f272cd717bb25f1d".into(),
849842
transactions: vec![tx_contract_deploy.clone(), tx_contract_call.clone()],
850843
}],
851844
);
852845

853846
epoch_blocks.insert(
854847
StacksEpochId::Epoch33,
855848
vec![TestBlock {
856-
marf_hash: "416e728daeec4de695c89d15eede8ddb7b85fb4af82daffb1e0d8166a3e93451".into(),
857849
transactions: vec![tx_contract_deploy, tx_contract_call],
858850
}],
859851
);

0 commit comments

Comments
 (0)