1use std::sync::Arc;
4
5use zebra_chain::{
6 amount::{Amount, DeferredPoolBalanceChange},
7 block::{self, Block},
8 transaction::Transaction,
9 transparent,
10 value_balance::ValueBalance,
11};
12
13use crate::{
14 request::ContextuallyVerifiedBlock, service::chain_tip::ChainTipBlock,
15 SemanticallyVerifiedBlock,
16};
17
18pub trait Prepare {
20 fn prepare(self) -> SemanticallyVerifiedBlock;
23}
24
25impl Prepare for Arc<Block> {
26 fn prepare(self) -> SemanticallyVerifiedBlock {
27 self.into()
28 }
29}
30
31impl<T> From<T> for ChainTipBlock
32where
33 T: Prepare,
34{
35 fn from(block: T) -> Self {
36 block.prepare().into()
37 }
38}
39
40impl SemanticallyVerifiedBlock {
41 #[cfg(test)]
46 pub fn test_with_zero_spent_utxos(&self) -> ContextuallyVerifiedBlock {
47 ContextuallyVerifiedBlock::test_with_zero_spent_utxos(self)
48 }
49
50 #[cfg(test)]
55 pub fn test_with_zero_chain_pool_change(&self) -> ContextuallyVerifiedBlock {
56 ContextuallyVerifiedBlock::test_with_zero_chain_pool_change(self)
57 }
58}
59
60impl ContextuallyVerifiedBlock {
61 pub fn test_with_zero_spent_utxos(block: impl Into<SemanticallyVerifiedBlock>) -> Self {
66 let block = block.into();
67
68 let zero_output = transparent::Output {
69 value: Amount::zero(),
70 lock_script: transparent::Script::new(&[]),
71 };
72
73 let zero_utxo = transparent::OrderedUtxo::new(zero_output, block::Height(1), 1);
74
75 let zero_spent_utxos = block
76 .block
77 .transactions
78 .iter()
79 .map(AsRef::as_ref)
80 .flat_map(Transaction::inputs)
81 .flat_map(|input| input.outpoint())
82 .map(|outpoint| (outpoint, zero_utxo.clone()))
83 .collect();
84
85 ContextuallyVerifiedBlock::with_block_and_spent_utxos(
86 block,
87 zero_spent_utxos,
88 DeferredPoolBalanceChange::zero(),
89 &zebra_chain::parameters::Network::Mainnet,
90 ValueBalance::zero(),
91 )
92 .expect("all UTXOs are provided with zero values")
93 }
94
95 pub fn test_with_zero_chain_pool_change(block: impl Into<SemanticallyVerifiedBlock>) -> Self {
100 let SemanticallyVerifiedBlock {
101 block,
102 hash,
103 height,
104 new_outputs,
105 transaction_hashes,
106 received_time,
107 } = block.into();
108
109 Self {
110 block,
111 hash,
112 height,
113 new_outputs: new_outputs.clone(),
114 spent_outputs: new_outputs,
118 transaction_hashes,
119 chain_value_pool_change: ValueBalance::zero(),
120 received_time,
121 }
122 }
123}