1use std::collections::HashMap;
4
5use crate::{
6 block::{self, Block, Height},
7 transaction::{self, Transaction},
8 transparent,
9};
10
11#[derive(Clone, Debug, PartialEq, Eq, Hash)]
13#[cfg_attr(
14 any(test, feature = "proptest-impl"),
15 derive(proptest_derive::Arbitrary, serde::Serialize)
16)]
17pub struct Utxo {
18 pub output: transparent::Output,
20
21 pub height: block::Height,
26 pub from_coinbase: bool,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
38#[cfg_attr(
39 any(test, feature = "proptest-impl"),
40 derive(proptest_derive::Arbitrary)
41)]
42pub struct OrderedUtxo {
45 pub utxo: Utxo,
47 pub tx_index_in_block: usize,
55}
56
57impl AsRef<Utxo> for Utxo {
58 fn as_ref(&self) -> &Utxo {
59 self
60 }
61}
62
63impl AsRef<Utxo> for OrderedUtxo {
64 fn as_ref(&self) -> &Utxo {
65 &self.utxo
66 }
67}
68
69impl Utxo {
70 pub fn new(output: transparent::Output, height: block::Height, from_coinbase: bool) -> Utxo {
72 Utxo {
73 output,
74 height,
75 from_coinbase,
76 }
77 }
78
79 pub fn from_location(
81 output: transparent::Output,
82 height: block::Height,
83 tx_index_in_block: usize,
84 ) -> Utxo {
85 let from_coinbase = tx_index_in_block == 0;
88
89 Utxo {
90 output,
91 height,
92 from_coinbase,
93 }
94 }
95}
96
97impl OrderedUtxo {
98 pub fn new(
100 output: transparent::Output,
101 height: block::Height,
102 tx_index_in_block: usize,
103 ) -> OrderedUtxo {
104 let from_coinbase = tx_index_in_block == 0;
107
108 OrderedUtxo {
109 utxo: Utxo::new(output, height, from_coinbase),
110 tx_index_in_block,
111 }
112 }
113
114 pub fn from_utxo(utxo: Utxo, tx_index_in_block: usize) -> OrderedUtxo {
116 OrderedUtxo {
117 utxo,
118 tx_index_in_block,
119 }
120 }
121}
122
123#[derive(Copy, Clone, Debug, PartialEq, Eq)]
129#[cfg_attr(
130 any(test, feature = "proptest-impl"),
131 derive(proptest_derive::Arbitrary)
132)]
133pub enum CoinbaseSpendRestriction {
134 DisallowCoinbaseSpend,
138
139 CheckCoinbaseMaturity {
143 spend_height: block::Height,
145 },
146}
147
148pub fn utxos_from_ordered_utxos(
150 ordered_utxos: impl IntoIterator<Item = (transparent::OutPoint, OrderedUtxo)>,
151) -> HashMap<transparent::OutPoint, Utxo> {
152 ordered_utxos
153 .into_iter()
154 .map(|(outpoint, ordered_utxo)| (outpoint, ordered_utxo.utxo))
155 .collect()
156}
157
158pub fn outputs_from_utxos(
160 utxos: HashMap<transparent::OutPoint, Utxo>,
161) -> HashMap<transparent::OutPoint, transparent::Output> {
162 utxos
163 .into_iter()
164 .map(|(outpoint, utxo)| (outpoint, utxo.output))
165 .collect()
166}
167
168pub fn new_outputs(
171 block: &Block,
172 transaction_hashes: &[transaction::Hash],
173) -> HashMap<transparent::OutPoint, Utxo> {
174 utxos_from_ordered_utxos(new_ordered_outputs(block, transaction_hashes))
175}
176
177#[cfg(any(test, feature = "proptest-impl"))]
182pub fn new_outputs_with_height(
183 block: &Block,
184 height: Height,
185 transaction_hashes: &[transaction::Hash],
186) -> HashMap<transparent::OutPoint, Utxo> {
187 utxos_from_ordered_utxos(new_ordered_outputs_with_height(
188 block,
189 height,
190 transaction_hashes,
191 ))
192}
193
194pub fn new_ordered_outputs(
197 block: &Block,
198 transaction_hashes: &[transaction::Hash],
199) -> HashMap<transparent::OutPoint, OrderedUtxo> {
200 let height = block.coinbase_height().expect("block has coinbase height");
201
202 new_ordered_outputs_with_height(block, height, transaction_hashes)
203}
204
205pub fn new_ordered_outputs_with_height(
211 block: &Block,
212 height: Height,
213 transaction_hashes: &[transaction::Hash],
214) -> HashMap<transparent::OutPoint, OrderedUtxo> {
215 let mut new_ordered_outputs = HashMap::new();
216
217 for (tx_index_in_block, (transaction, hash)) in block
218 .transactions
219 .iter()
220 .zip(transaction_hashes.iter().cloned())
221 .enumerate()
222 {
223 new_ordered_outputs.extend(new_transaction_ordered_outputs(
224 transaction,
225 hash,
226 tx_index_in_block,
227 height,
228 ));
229 }
230
231 new_ordered_outputs
232}
233
234pub fn new_transaction_ordered_outputs(
240 transaction: &Transaction,
241 hash: transaction::Hash,
242 tx_index_in_block: usize,
243 height: block::Height,
244) -> HashMap<transparent::OutPoint, OrderedUtxo> {
245 let mut new_ordered_outputs = HashMap::new();
246
247 for (output_index_in_transaction, output) in transaction.outputs().iter().cloned().enumerate() {
248 let output_index_in_transaction = output_index_in_transaction
249 .try_into()
250 .expect("unexpectedly large number of outputs");
251
252 new_ordered_outputs.insert(
253 transparent::OutPoint {
254 hash,
255 index: output_index_in_transaction,
256 },
257 OrderedUtxo::new(output, height, tx_index_in_block),
258 );
259 }
260
261 new_ordered_outputs
262}