Skip to content

Commit d182072

Browse files
committed
Implement Confirm/Listen interfaces
1 parent 9654ed0 commit d182072

File tree

1 file changed

+175
-7
lines changed

1 file changed

+175
-7
lines changed

src/sweep.rs

Lines changed: 175 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
use crate::hex_utils;
22
use crate::io::{KVStore, SPENDABLE_OUTPUT_INFO_PERSISTENCE_NAMESPACE};
33
use crate::logger::{log_debug, log_error, Logger};
4-
use crate::wallet::Wallet;
4+
use crate::wallet::{num_blocks_from_conf_target, Wallet};
55
use crate::{Error, KeysManager};
66

77
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
8-
use lightning::chain::BestBlock;
8+
use lightning::chain::{self, BestBlock, Confirm, Filter, Listen};
99
use lightning::impl_writeable_tlv_based;
1010
use lightning::sign::{EntropySource, SpendableOutputDescriptor};
1111
use lightning::util::ser::Writeable;
1212

1313
use bitcoin::secp256k1::Secp256k1;
14-
use bitcoin::{BlockHash, LockTime, PackedLockTime, Transaction};
14+
use bitcoin::{BlockHash, BlockHeader, LockTime, PackedLockTime, Script, Transaction, Txid};
1515

1616
use std::ops::Deref;
1717
use std::sync::{Arc, Mutex};
1818

19+
const CONSIDERED_SPENT_THRESHOLD_CONF: u32 = 6;
20+
1921
#[derive(Clone, Debug, PartialEq, Eq)]
2022
pub(crate) struct SpendableOutputInfo {
2123
id: [u8; 32],
@@ -33,29 +35,42 @@ impl_writeable_tlv_based!(SpendableOutputInfo, {
3335
(8, confirmed_in_block, option),
3436
});
3537

36-
pub(crate) struct OutputSweeper<K: KVStore + Sync + Send, L: Deref>
38+
pub(crate) struct OutputSweeper<K: KVStore + Sync + Send, F: Deref, L: Deref>
3739
where
40+
F::Target: Filter,
3841
L::Target: Logger,
3942
{
4043
outputs: Mutex<Vec<SpendableOutputInfo>>,
4144
wallet: Arc<Wallet<bdk::database::SqliteDatabase, L>>,
4245
keys_manager: Arc<KeysManager>,
4346
kv_store: Arc<K>,
4447
best_block: Mutex<BestBlock>,
48+
chain_source: Option<F>,
4549
logger: L,
4650
}
4751

48-
impl<K: KVStore + Sync + Send, L: Deref> OutputSweeper<K, L>
52+
impl<K: KVStore + Sync + Send, F: Deref, L: Deref> OutputSweeper<K, F, L>
4953
where
54+
F::Target: Filter,
5055
L::Target: Logger,
5156
{
5257
pub(crate) fn new(
5358
outputs: Vec<SpendableOutputInfo>, wallet: Arc<Wallet<bdk::database::SqliteDatabase, L>>,
54-
keys_manager: Arc<KeysManager>, kv_store: Arc<K>, best_block: BestBlock, logger: L,
59+
keys_manager: Arc<KeysManager>, kv_store: Arc<K>, best_block: BestBlock,
60+
chain_source: Option<F>, logger: L,
5561
) -> Self {
62+
if let Some(filter) = chain_source.as_ref() {
63+
for o in &outputs {
64+
if let Some(tx) = o.spending_tx.as_ref() {
65+
// TODO: can we give something better than the empty script here?
66+
filter.register_tx(&tx.txid(), &Script::new())
67+
}
68+
}
69+
}
70+
5671
let outputs = Mutex::new(outputs);
5772
let best_block = Mutex::new(best_block);
58-
Self { outputs, wallet, keys_manager, kv_store, best_block, logger }
73+
Self { outputs, wallet, keys_manager, kv_store, best_block, chain_source, logger }
5974
}
6075

6176
pub(crate) fn add_outputs(&self, output_descriptors: Vec<SpendableOutputDescriptor>) {
@@ -64,6 +79,9 @@ where
6479
let (spending_tx, broadcast_height) = match self.get_spending_tx(&output_descriptors) {
6580
Ok(Some(spending_tx)) => {
6681
self.wallet.broadcast_transactions(&[&spending_tx]);
82+
if let Some(filter) = self.chain_source.as_ref() {
83+
filter.register_tx(&spending_tx.txid(), &Script::new())
84+
}
6785
(Some(spending_tx), Some(self.best_block.lock().unwrap().height()))
6886
}
6987
Ok(None) => {
@@ -138,3 +156,153 @@ where
138156
})
139157
}
140158
}
159+
160+
impl<K: KVStore + Sync + Send, F: Deref, L: Deref> Listen for OutputSweeper<K, F, L>
161+
where
162+
F::Target: Filter,
163+
L::Target: Logger,
164+
{
165+
fn filtered_block_connected(
166+
&self, header: &BlockHeader, txdata: &chain::transaction::TransactionData, height: u32,
167+
) {
168+
{
169+
let best_block = self.best_block.lock().unwrap();
170+
assert_eq!(best_block.block_hash(), header.prev_blockhash,
171+
"Blocks must be connected in chain-order - the connected header must build on the last connected header");
172+
assert_eq!(best_block.height(), height - 1,
173+
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height");
174+
}
175+
176+
self.transactions_confirmed(header, txdata, height);
177+
self.best_block_updated(header, height);
178+
}
179+
180+
fn block_disconnected(&self, header: &BlockHeader, height: u32) {
181+
let new_height = height - 1;
182+
{
183+
let mut best_block = self.best_block.lock().unwrap();
184+
assert_eq!(best_block.block_hash(), header.block_hash(),
185+
"Blocks must be disconnected in chain-order - the disconnected header must be the last connected header");
186+
assert_eq!(best_block.height(), height,
187+
"Blocks must be disconnected in chain-order - the disconnected block must have the correct height");
188+
*best_block = BestBlock::new(header.prev_blockhash, new_height)
189+
}
190+
191+
let mut locked_outputs = self.outputs.lock().unwrap();
192+
for output_info in locked_outputs.iter_mut() {
193+
if output_info.confirmed_in_block == Some((height, header.block_hash())) {
194+
output_info.confirmed_in_block = None;
195+
}
196+
}
197+
}
198+
}
199+
200+
impl<K: KVStore + Sync + Send, F: Deref, L: Deref> Confirm for OutputSweeper<K, F, L>
201+
where
202+
F::Target: Filter,
203+
L::Target: Logger,
204+
{
205+
fn transactions_confirmed(
206+
&self, header: &BlockHeader, txdata: &chain::transaction::TransactionData, height: u32,
207+
) {
208+
let mut locked_outputs = self.outputs.lock().unwrap();
209+
for (_, tx) in txdata {
210+
locked_outputs
211+
.iter_mut()
212+
.filter(|o| o.spending_tx.as_ref().map(|t| t.txid()) == Some(tx.txid()))
213+
.for_each(|o| o.confirmed_in_block = Some((height, header.block_hash())));
214+
}
215+
}
216+
217+
fn transaction_unconfirmed(&self, txid: &Txid) {
218+
let mut locked_outputs = self.outputs.lock().unwrap();
219+
220+
// Get what height was unconfirmed.
221+
let unconf_height = locked_outputs
222+
.iter()
223+
.find(|o| o.spending_tx.as_ref().map(|t| t.txid()) == Some(*txid))
224+
.and_then(|o| o.confirmed_in_block)
225+
.map(|t| t.0);
226+
227+
// Unconfirm all >= this height.
228+
locked_outputs
229+
.iter_mut()
230+
.filter(|o| o.confirmed_in_block.map(|t| t.0) >= unconf_height)
231+
.for_each(|o| o.confirmed_in_block = None);
232+
}
233+
234+
fn best_block_updated(&self, header: &BlockHeader, height: u32) {
235+
*self.best_block.lock().unwrap() = BestBlock::new(header.block_hash(), height);
236+
237+
let mut locked_outputs = self.outputs.lock().unwrap();
238+
239+
// Rebroadcast all outputs that didn't get confirmed by now.
240+
for output_info in locked_outputs.iter_mut().filter(|o| o.confirmed_in_block.is_none()) {
241+
let should_broadcast = if let Some(bcast_height) = output_info.broadcast_height {
242+
height >= bcast_height + num_blocks_from_conf_target(ConfirmationTarget::Background)
243+
} else {
244+
true
245+
};
246+
if should_broadcast {
247+
let output_descriptors = vec![output_info.descriptor.clone()];
248+
match self.get_spending_tx(&output_descriptors) {
249+
Ok(Some(spending_tx)) => {
250+
self.wallet.broadcast_transactions(&[&spending_tx]);
251+
if let Some(filter) = self.chain_source.as_ref() {
252+
filter.register_tx(&spending_tx.txid(), &Script::new())
253+
}
254+
output_info.spending_tx = Some(spending_tx);
255+
output_info.broadcast_height = Some(height);
256+
}
257+
Ok(None) => {
258+
log_debug!(
259+
self.logger,
260+
"Omitted spending static outputs: {:?}",
261+
output_descriptors
262+
);
263+
}
264+
Err(err) => {
265+
log_error!(self.logger, "Error spending outputs: {:?}", err);
266+
}
267+
};
268+
}
269+
}
270+
271+
// Prune all outputs that have sufficient depth by now.
272+
locked_outputs.retain(|o| {
273+
if let Some((conf_height, _)) = o.confirmed_in_block {
274+
if height >= conf_height + CONSIDERED_SPENT_THRESHOLD_CONF {
275+
let key = hex_utils::to_string(&o.id);
276+
match self.kv_store.remove(SPENDABLE_OUTPUT_INFO_PERSISTENCE_NAMESPACE, &key) {
277+
Ok(_) => return false,
278+
Err(e) => {
279+
log_error!(
280+
self.logger,
281+
"Removal of key {}/{} failed due to: {}",
282+
SPENDABLE_OUTPUT_INFO_PERSISTENCE_NAMESPACE,
283+
key,
284+
e
285+
);
286+
return true;
287+
}
288+
}
289+
}
290+
}
291+
true
292+
});
293+
}
294+
295+
fn get_relevant_txids(&self) -> Vec<(Txid, Option<BlockHash>)> {
296+
let locked_outputs = self.outputs.lock().unwrap();
297+
locked_outputs
298+
.iter()
299+
.filter_map(|o| {
300+
if let Some(tx) = o.spending_tx.as_ref() {
301+
Some((tx.txid(), o.confirmed_in_block.map(|c| c.1)))
302+
} else {
303+
None
304+
}
305+
})
306+
.collect::<Vec<_>>()
307+
}
308+
}

0 commit comments

Comments
 (0)