Skip to content

Commit 1d0c6c6

Browse files
authored
Merge pull request #3101 from tnull/2024-06-fix-tx-sync
tx-sync: Make confirmables `Deref` and run `rustfmt`
2 parents c166c21 + 43b4186 commit 1d0c6c6

File tree

8 files changed

+287
-209
lines changed

8 files changed

+287
-209
lines changed

ci/rustfmt.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ TMP_FILE=$(mktemp)
1515
find . -name '*.rs' -type f |sort >$TMP_FILE
1616
for file in $(comm -23 $TMP_FILE rustfmt_excluded_files); do
1717
echo "Checking formatting of $file"
18-
rustfmt $VERS --check $file
18+
rustfmt $VERS --edition 2021 --check $file
1919
done

lightning-transaction-sync/src/common.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use lightning::chain::{Confirm, WatchedOutput};
2-
use lightning::chain::channelmonitor::ANTI_REORG_DELAY;
3-
use bitcoin::{Txid, BlockHash, Transaction, OutPoint};
41
use bitcoin::block::Header;
2+
use bitcoin::{BlockHash, OutPoint, Transaction, Txid};
3+
use lightning::chain::channelmonitor::ANTI_REORG_DELAY;
4+
use lightning::chain::{Confirm, WatchedOutput};
55

6-
use std::collections::{HashSet, HashMap};
7-
6+
use std::collections::{HashMap, HashSet};
7+
use std::ops::Deref;
88

99
// Represents the current state.
1010
pub(crate) struct SyncState {
@@ -33,10 +33,11 @@ impl SyncState {
3333
pending_sync: false,
3434
}
3535
}
36-
pub fn sync_unconfirmed_transactions(
37-
&mut self, confirmables: &Vec<&(dyn Confirm + Sync + Send)>,
38-
unconfirmed_txs: Vec<Txid>,
39-
) {
36+
pub fn sync_unconfirmed_transactions<C: Deref>(
37+
&mut self, confirmables: &Vec<C>, unconfirmed_txs: Vec<Txid>,
38+
) where
39+
C::Target: Confirm,
40+
{
4041
for txid in unconfirmed_txs {
4142
for c in confirmables {
4243
c.transaction_unconfirmed(&txid);
@@ -46,21 +47,24 @@ impl SyncState {
4647

4748
// If a previously-confirmed output spend is unconfirmed, re-add the watched output to
4849
// the tracking map.
49-
self.outputs_spends_pending_threshold_conf.retain(|(conf_txid, _, prev_outpoint, output)| {
50-
if txid == *conf_txid {
51-
self.watched_outputs.insert(*prev_outpoint, output.clone());
52-
false
53-
} else {
54-
true
55-
}
56-
})
50+
self.outputs_spends_pending_threshold_conf.retain(
51+
|(conf_txid, _, prev_outpoint, output)| {
52+
if txid == *conf_txid {
53+
self.watched_outputs.insert(*prev_outpoint, output.clone());
54+
false
55+
} else {
56+
true
57+
}
58+
},
59+
)
5760
}
5861
}
5962

60-
pub fn sync_confirmed_transactions(
61-
&mut self, confirmables: &Vec<&(dyn Confirm + Sync + Send)>,
62-
confirmed_txs: Vec<ConfirmedTx>
63-
) {
63+
pub fn sync_confirmed_transactions<C: Deref>(
64+
&mut self, confirmables: &Vec<C>, confirmed_txs: Vec<ConfirmedTx>,
65+
) where
66+
C::Target: Confirm,
67+
{
6468
for ctx in confirmed_txs {
6569
for c in confirmables {
6670
c.transactions_confirmed(
@@ -74,20 +78,19 @@ impl SyncState {
7478

7579
for input in &ctx.tx.input {
7680
if let Some(output) = self.watched_outputs.remove(&input.previous_output) {
77-
self.outputs_spends_pending_threshold_conf.push((ctx.tx.txid(), ctx.block_height, input.previous_output, output));
81+
let spent = (ctx.tx.txid(), ctx.block_height, input.previous_output, output);
82+
self.outputs_spends_pending_threshold_conf.push(spent);
7883
}
7984
}
8085
}
8186
}
8287

8388
pub fn prune_output_spends(&mut self, cur_height: u32) {
84-
self.outputs_spends_pending_threshold_conf.retain(|(_, conf_height, _, _)| {
85-
cur_height < conf_height + ANTI_REORG_DELAY - 1
86-
});
89+
self.outputs_spends_pending_threshold_conf
90+
.retain(|(_, conf_height, _, _)| cur_height < conf_height + ANTI_REORG_DELAY - 1);
8791
}
8892
}
8993

90-
9194
// A queue that is to be filled by `Filter` and drained during the next syncing round.
9295
pub(crate) struct FilterQueue {
9396
// Transactions that were registered via the `Filter` interface and have to be processed.
@@ -98,10 +101,7 @@ pub(crate) struct FilterQueue {
98101

99102
impl FilterQueue {
100103
pub fn new() -> Self {
101-
Self {
102-
transactions: HashSet::new(),
103-
outputs: HashMap::new(),
104-
}
104+
Self { transactions: HashSet::new(), outputs: HashMap::new() }
105105
}
106106

107107
// Processes the transaction and output queues and adds them to the given [`SyncState`].

0 commit comments

Comments
 (0)