Repository navigation
Expand file tree
/
Copy pathtransaction.rs
More file actions
206 lines (189 loc) · 5.8 KB
/
Copy pathtransaction.rs
File metadata and controls
206 lines (189 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use crate::chain::{BlockHash, OutPoint, Transaction, TxIn, TxOut, Txid};
use crate::util::BlockId;
use std::collections::{BTreeSet, HashMap};
#[cfg(feature = "liquid")]
lazy_static! {
static ref REGTEST_INITIAL_ISSUANCE_PREVTX: Txid =
"50cdc410c9d0d61eeacc531f52d2c70af741da33af127c364e52ac1ee7c030a5"
.parse()
.unwrap();
static ref TESTNET_INITIAL_ISSUANCE_PREVTX: Txid =
"0c52d2526a5c9f00e9fb74afd15dd3caaf17c823159a514f929ae25193a43a52"
.parse()
.unwrap();
}
#[derive(Serialize, Deserialize, Debug)]
pub struct TransactionStatus {
pub confirmed: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_height: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_hash: Option<BlockHash>,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_time: Option<u32>,
}
impl From<Option<BlockId>> for TransactionStatus {
fn from(blockid: Option<BlockId>) -> TransactionStatus {
match blockid {
Some(b) => TransactionStatus {
confirmed: true,
block_height: Some(b.height as usize),
block_hash: Some(b.hash),
block_time: Some(b.time),
},
None => TransactionStatus {
confirmed: false,
block_height: None,
block_hash: None,
block_time: None,
},
}
}
}
#[cfg(feature = "liquid")]
pub fn optional_value_for_newer_blocks(
block_id: Option<BlockId>,
check_time: u32,
value: usize,
) -> Option<usize> {
match block_id {
// use the provided value only if it was after the "activation" time
Some(b) if b.time >= check_time => Some(value),
// otherwise don't include it
Some(_) => None,
// also use the value for unconfirmed blocks
None => Some(value),
}
}
#[derive(Serialize, Deserialize)]
pub struct TxInput {
pub txid: Txid,
pub vin: u32,
}
pub fn is_coinbase(txin: &TxIn) -> bool {
#[cfg(not(feature = "liquid"))]
return txin.previous_output.is_null();
#[cfg(feature = "liquid")]
return txin.is_coinbase();
}
pub fn has_prevout(txin: &TxIn) -> bool {
#[cfg(not(feature = "liquid"))]
return !txin.previous_output.is_null();
#[cfg(feature = "liquid")]
return !txin.is_coinbase()
&& !txin.is_pegin
&& txin.previous_output.txid != *REGTEST_INITIAL_ISSUANCE_PREVTX
&& txin.previous_output.txid != *TESTNET_INITIAL_ISSUANCE_PREVTX;
}
pub fn is_spendable(txout: &TxOut) -> bool {
#[cfg(not(feature = "liquid"))]
return !txout.script_pubkey.is_op_return();
#[cfg(feature = "liquid")]
return !txout.is_fee() && !txout.script_pubkey.is_provably_unspendable();
}
pub fn extract_tx_prevouts<'a>(
tx: &Transaction,
txos: &'a HashMap<OutPoint, TxOut>,
allow_missing: bool,
) -> HashMap<u32, &'a TxOut> {
tx.input
.iter()
.enumerate()
.filter(|(_, txi)| has_prevout(txi))
.filter_map(|(index, txi)| {
Some((
index as u32,
txos.get(&txi.previous_output).or_else(|| {
assert!(allow_missing, "missing outpoint {:?}", txi.previous_output);
None
})?,
))
})
.collect()
}
pub fn get_prev_outpoints<'a>(txs: impl Iterator<Item = &'a Transaction>) -> BTreeSet<OutPoint> {
txs.flat_map(|tx| {
tx.input
.iter()
.filter(|txin| has_prevout(txin))
.map(|txin| txin.previous_output)
})
.collect()
}
pub fn serialize_outpoint<S>(outpoint: &OutPoint, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
use serde::ser::SerializeStruct;
let mut s = serializer.serialize_struct("OutPoint", 2)?;
s.serialize_field("txid", &outpoint.txid)?;
s.serialize_field("vout", &outpoint.vout)?;
s.end()
}
#[cfg(all(test, feature = "liquid"))]
mod test {
use super::optional_value_for_newer_blocks;
use crate::util::BlockId;
use bitcoin::hashes::Hash;
use elements::BlockHash;
#[test]
fn opt_value_newer_block() {
let value = 123;
let check_time = 32;
let hash = BlockHash::from_slice(&[0; 32]).unwrap();
let height = 456;
// unconfirmed block should include the value
let block_id = None;
assert_eq!(
optional_value_for_newer_blocks(block_id, check_time, value),
Some(value)
);
// block time before check_time should NOT include the value
let block_id = Some(BlockId {
height,
hash,
time: 0,
});
assert_eq!(
optional_value_for_newer_blocks(block_id, check_time, value),
None
);
let block_id = Some(BlockId {
height,
hash,
time: 31,
});
assert_eq!(
optional_value_for_newer_blocks(block_id, check_time, value),
None
);
// block time on or after check_time should include the value
let block_id = Some(BlockId {
height,
hash,
time: 32,
});
assert_eq!(
optional_value_for_newer_blocks(block_id, check_time, value),
Some(value)
);
let block_id = Some(BlockId {
height,
hash,
time: 33,
});
assert_eq!(
optional_value_for_newer_blocks(block_id, check_time, value),
Some(value)
);
let block_id = Some(BlockId {
height,
hash,
time: 333,
});
assert_eq!(
optional_value_for_newer_blocks(block_id, check_time, value),
Some(value)
);
}
}