|
1 | 1 | #![allow(unused_variables)]
|
2 | 2 | use crate::config::{PAYJOIN_REQUEST_TIMEOUT, PAYJOIN_RETRY_INTERVAL};
|
| 3 | +use crate::error::Error; |
3 | 4 | use crate::io::utils::ohttp_headers;
|
4 | 5 | use crate::logger::FilesystemLogger;
|
5 |
| -use crate::types::Wallet; |
| 6 | +use crate::types::{ChainSource, EventQueue, Wallet}; |
| 7 | +use crate::Event; |
6 | 8 |
|
7 | 9 | use bitcoin::block::Header;
|
8 |
| -use bitcoin::{BlockHash, Script, ScriptBuf, Txid}; |
| 10 | +use bitcoin::psbt::Psbt; |
| 11 | +use bitcoin::{BlockHash, Script, Transaction, Txid}; |
| 12 | +use lightning::chain::channelmonitor::ANTI_REORG_DELAY; |
9 | 13 | use lightning::chain::transaction::TransactionData;
|
10 |
| -use lightning::chain::WatchedOutput; |
| 14 | +use lightning::chain::{BestBlock, Filter, WatchedOutput}; |
11 | 15 | use lightning::util::logger::Logger;
|
12 | 16 | use lightning::{log_error, log_info};
|
13 | 17 |
|
14 |
| -use std::sync::Arc; |
| 18 | +use std::sync::{Arc, Mutex}; |
15 | 19 |
|
| 20 | +#[derive(Clone, Debug)] |
16 | 21 | enum PayjoinTransaction {
|
17 |
| - PendingInitialBroadcast, |
18 |
| - PendingFirstConfirmation, |
19 |
| - PendingThresholdConfirmation, |
| 22 | + PendingFirstConfirmation { |
| 23 | + tx: Transaction, |
| 24 | + first_broadcast_height: u32, |
| 25 | + first_broadcast_hash: BlockHash, |
| 26 | + }, |
| 27 | + PendingThresholdConfirmations { |
| 28 | + tx: Transaction, |
| 29 | + first_broadcast_height: u32, |
| 30 | + first_broadcast_hash: BlockHash, |
| 31 | + latest_confirmation_height: u32, |
| 32 | + latest_confirmation_hash: BlockHash, |
| 33 | + }, |
| 34 | +} |
| 35 | + |
| 36 | +impl PayjoinTransaction { |
| 37 | + fn txid(&self) -> Option<Txid> { |
| 38 | + match self { |
| 39 | + PayjoinTransaction::PendingFirstConfirmation { tx, .. } => Some(tx.txid()), |
| 40 | + PayjoinTransaction::PendingThresholdConfirmations { tx, .. } => Some(tx.txid()), |
| 41 | + } |
| 42 | + } |
20 | 43 | }
|
21 | 44 |
|
22 | 45 | pub(crate) struct PayjoinSender {
|
23 | 46 | logger: Arc<FilesystemLogger>,
|
24 |
| - wallet: Arc<Wallet>, |
25 | 47 | payjoin_relay: payjoin::Url,
|
26 |
| - transaction_queue: std::sync::Mutex<Vec<(Txid, ScriptBuf)>>, |
| 48 | + chain_source: Arc<ChainSource>, |
| 49 | + best_known_block: Mutex<Option<BestBlock>>, |
| 50 | + transactions: Mutex<Vec<PayjoinTransaction>>, |
| 51 | + event_queue: Arc<EventQueue>, |
| 52 | + wallet: Arc<Wallet>, |
27 | 53 | }
|
28 | 54 |
|
29 | 55 | impl PayjoinSender {
|
30 | 56 | pub(crate) fn new(
|
31 |
| - logger: Arc<FilesystemLogger>, wallet: Arc<Wallet>, payjoin_relay: payjoin::Url, |
| 57 | + logger: Arc<FilesystemLogger>, payjoin_relay: payjoin::Url, chain_source: Arc<ChainSource>, |
| 58 | + event_queue: Arc<EventQueue>, wallet: Arc<Wallet>, |
32 | 59 | ) -> Self {
|
33 | 60 | Self {
|
34 | 61 | logger,
|
35 |
| - wallet, |
36 | 62 | payjoin_relay,
|
37 |
| - transaction_queue: std::sync::Mutex::new(Vec::new()), |
| 63 | + transactions: Mutex::new(Vec::new()), |
| 64 | + best_known_block: Mutex::new(None), |
| 65 | + chain_source, |
| 66 | + event_queue, |
| 67 | + wallet, |
38 | 68 | }
|
39 | 69 | }
|
40 | 70 |
|
@@ -94,48 +124,131 @@ impl PayjoinSender {
|
94 | 124 | return None;
|
95 | 125 | }
|
96 | 126 | }
|
| 127 | + |
| 128 | + fn internal_transactions_confirmed( |
| 129 | + &self, header: &Header, txdata: &TransactionData, height: u32, |
| 130 | + ) { |
| 131 | + let (index, tx) = txdata[0]; |
| 132 | + let txid = tx.txid(); |
| 133 | + let mut transactions = self.transactions.lock().unwrap(); |
| 134 | + let position = transactions.iter().position(|o| o.txid() == Some(txid)).unwrap(); |
| 135 | + let pj_tx = transactions.remove(position); |
| 136 | + dbg!("found confirmed", &pj_tx); |
| 137 | + let pj_tx = match pj_tx { |
| 138 | + PayjoinTransaction::PendingFirstConfirmation { |
| 139 | + ref tx, |
| 140 | + first_broadcast_height, |
| 141 | + first_broadcast_hash, |
| 142 | + } => transactions.push(PayjoinTransaction::PendingThresholdConfirmations { |
| 143 | + tx: tx.clone(), |
| 144 | + first_broadcast_height, |
| 145 | + first_broadcast_hash, |
| 146 | + latest_confirmation_height: height, |
| 147 | + latest_confirmation_hash: header.block_hash(), |
| 148 | + }), |
| 149 | + PayjoinTransaction::PendingThresholdConfirmations { |
| 150 | + ref tx, |
| 151 | + first_broadcast_height, |
| 152 | + first_broadcast_hash, |
| 153 | + latest_confirmation_height, |
| 154 | + latest_confirmation_hash, |
| 155 | + } => { |
| 156 | + dbg!("Transaction confirmed here"); |
| 157 | + dbg!("height: {}", height); |
| 158 | + dbg!("first_broadcast_height: {}", first_broadcast_height); |
| 159 | + if height - first_broadcast_height >= ANTI_REORG_DELAY { |
| 160 | + let _ = self.event_queue.add_event(Event::PayjoinTxSendSuccess { txid }); |
| 161 | + } else { |
| 162 | + transactions.push(PayjoinTransaction::PendingThresholdConfirmations { |
| 163 | + tx: tx.clone(), |
| 164 | + first_broadcast_height, |
| 165 | + first_broadcast_hash, |
| 166 | + latest_confirmation_height: height, |
| 167 | + latest_confirmation_hash: header.block_hash(), |
| 168 | + }); |
| 169 | + } |
| 170 | + }, |
| 171 | + }; |
| 172 | + // dbg!("here blocked"); |
| 173 | + // *self.transactions.lock().unwrap() = transactions.clone(); |
| 174 | + // dbg!("here2 unblocked"); |
| 175 | + } |
| 176 | + |
| 177 | + pub(crate) fn finalise_payjoin_transaction( |
| 178 | + &self, payjoin_proposal: &mut Psbt, original_psbt: &mut Psbt, |
| 179 | + ) -> Result<Transaction, Error> { |
| 180 | + let wallet = self.wallet.clone(); |
| 181 | + wallet.sign_payjoin_proposal(payjoin_proposal, original_psbt)?; |
| 182 | + let tx = payjoin_proposal.clone().extract_tx(); |
| 183 | + let our_input = tx.output.iter().find(|output| { |
| 184 | + wallet.is_mine(&output.script_pubkey).unwrap_or(false) |
| 185 | + }); |
| 186 | + if let Some(our_input) = our_input { |
| 187 | + let best_known_block = self.best_known_block.lock().unwrap(); |
| 188 | + dbg!(&best_known_block); |
| 189 | + let best_known_block = best_known_block.as_ref(); |
| 190 | + dbg!(&best_known_block); |
| 191 | + let (current_height, current_hash) = match best_known_block { |
| 192 | + Some(b) => (b.height, b.block_hash), |
| 193 | + None => return Err(Error::PayjoinReceiverRequestValidationFailed), // fixeror |
| 194 | + }; |
| 195 | + self.transactions.lock().unwrap().push( |
| 196 | + PayjoinTransaction::PendingFirstConfirmation { |
| 197 | + tx: tx.clone(), |
| 198 | + first_broadcast_height: current_height, |
| 199 | + first_broadcast_hash: current_hash, |
| 200 | + } |
| 201 | + ); |
| 202 | + self.register_tx(&tx.txid(), &our_input.script_pubkey); |
| 203 | + Ok(tx) |
| 204 | + } else { |
| 205 | + Err(Error::PayjoinReceiverRequestValidationFailed) // fixeror |
| 206 | + } |
| 207 | + } |
97 | 208 | }
|
98 | 209 |
|
99 |
| -impl lightning::chain::Filter for PayjoinSender { |
| 210 | +impl Filter for PayjoinSender { |
100 | 211 | fn register_tx(&self, txid: &Txid, script_pubkey: &Script) {
|
101 | 212 | dbg!("Registering transaction {:?}", txid);
|
102 |
| - self.transaction_queue.lock().unwrap().push((txid.clone(), script_pubkey.into())); |
| 213 | + self.chain_source.register_tx(txid, script_pubkey); |
| 214 | + } |
| 215 | + |
| 216 | + fn register_output(&self, output: WatchedOutput) { |
| 217 | + self.chain_source.register_output(output); |
103 | 218 | }
|
104 |
| - fn register_output(&self, output: WatchedOutput) {} |
105 | 219 | }
|
106 | 220 |
|
107 | 221 | impl lightning::chain::Confirm for PayjoinSender {
|
108 | 222 | fn transactions_confirmed(&self, header: &Header, txdata: &TransactionData, height: u32) {
|
109 |
| - dbg!("Confirmed transaction {:?}", txdata); |
110 |
| - // let (index, tx) = txdata[0]; |
111 |
| - // let txid = tx.txid(); |
112 |
| - // let my_input = |
113 |
| - // tx.input.iter().find(|input| self.wallet.is_mine(&input.script_sig).unwrap_or(false)); |
114 |
| - // if let Some(my_input) = my_input { |
115 |
| - // self.transaction_queue.push((txid, my_input.script_sig)) |
116 |
| - // } |
| 223 | + dbg!("Confirmed transaction"); |
| 224 | + self.internal_transactions_confirmed(header, txdata, height); |
117 | 225 | }
|
118 | 226 | fn transaction_unconfirmed(&self, txid: &Txid) {
|
119 |
| - dbg!("Unconfirmed transaction {:?}", txid); |
120 |
| - } |
| 227 | + dbg!("Unconfirmed transaction {:?}", txid); |
| 228 | + } |
121 | 229 | fn best_block_updated(&self, header: &Header, height: u32) {
|
122 |
| - dbg!("Best block updated {:?}", header); |
123 |
| - } |
| 230 | + dbg!("Best block updated {:?}", height); |
| 231 | + *self.best_known_block.lock().unwrap() = |
| 232 | + Some(BestBlock { height, block_hash: header.block_hash() }); |
| 233 | + } |
124 | 234 | fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
|
125 |
| - // let state_lock = self.sweeper_state.lock().unwrap(); |
126 |
| - // state_lock |
127 |
| - // .outputs |
128 |
| - // .iter() |
129 |
| - // .filter_map(|o| match o.status { |
130 |
| - // OutputSpendStatus::PendingThresholdConfirmations { |
131 |
| - // ref latest_spending_tx, |
132 |
| - // confirmation_height, |
133 |
| - // confirmation_hash, |
134 |
| - // .. |
135 |
| - // } => Some((latest_spending_tx.txid(), confirmation_height, Some(confirmation_hash))), |
136 |
| - // _ => None, |
137 |
| - // }) |
138 |
| - // .collect::<Vec<_>>() |
139 |
| - vec![] |
| 235 | + dbg!("Getting relevant txids"); |
| 236 | + let state_lock = self.transactions.lock().unwrap(); |
| 237 | + state_lock |
| 238 | + .iter() |
| 239 | + .filter_map(|o| match o { |
| 240 | + PayjoinTransaction::PendingThresholdConfirmations { |
| 241 | + tx, |
| 242 | + latest_confirmation_height, |
| 243 | + latest_confirmation_hash, |
| 244 | + .. |
| 245 | + } => Some(( |
| 246 | + tx.clone().txid(), |
| 247 | + latest_confirmation_height.clone(), |
| 248 | + Some(latest_confirmation_hash.clone()), |
| 249 | + )), |
| 250 | + _ => None, |
| 251 | + }) |
| 252 | + .collect::<Vec<_>>() |
140 | 253 | }
|
141 | 254 | }
|
0 commit comments