Skip to content

Commit 197e263

Browse files
committed
f Don't track spent_outputs
1 parent e7ceab9 commit 197e263

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

lightning-transaction-sync/src/esplora.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ where
143143
}
144144

145145
match maybe_await!(self.get_confirmed_transactions(&sync_state)) {
146-
Ok((confirmed_txs, spent_outputs)) => {
146+
Ok(confirmed_txs) => {
147147
// Double-check the tip hash. If if it changed, a reorg happened since
148148
// we started syncing and we need to restart last-minute.
149149
let check_tip_hash = maybe_await!(self.client.get_tip_hash())?;
@@ -156,7 +156,6 @@ where
156156
&mut sync_state,
157157
&confirmables,
158158
confirmed_txs,
159-
spent_outputs,
160159
);
161160
}
162161
Err(InternalError::Inconsistency) => {
@@ -202,7 +201,6 @@ where
202201

203202
fn sync_confirmed_transactions(
204203
&self, sync_state: &mut SyncState, confirmables: &Vec<&(dyn Confirm + Sync + Send)>, confirmed_txs: Vec<ConfirmedTx>,
205-
spent_outputs: HashSet<WatchedOutput>,
206204
) {
207205
for ctx in confirmed_txs {
208206
for c in confirmables {
@@ -214,15 +212,17 @@ where
214212
}
215213

216214
sync_state.watched_transactions.remove(&ctx.tx.txid());
215+
216+
for input in &ctx.tx.input {
217+
sync_state.watched_outputs.remove(&input.previous_output);
218+
}
217219
}
218-
219-
sync_state.watched_outputs = &sync_state.watched_outputs - &spent_outputs;
220220
}
221221

222222
#[maybe_async]
223223
fn get_confirmed_transactions(
224224
&self, sync_state: &SyncState,
225-
) -> Result<(Vec<ConfirmedTx>, HashSet<WatchedOutput>), InternalError> {
225+
) -> Result<Vec<ConfirmedTx>, InternalError> {
226226

227227
// First, check the confirmation status of registered transactions as well as the
228228
// status of dependent transactions of registered outputs.
@@ -235,10 +235,7 @@ where
235235
}
236236
}
237237

238-
// Remember all registered outputs that have been spent.
239-
let mut spent_outputs = HashSet::new();
240-
241-
for output in &sync_state.watched_outputs {
238+
for (_, output) in &sync_state.watched_outputs {
242239
if let Some(output_status) = maybe_await!(self.client
243240
.get_output_status(&output.outpoint.txid, output.outpoint.index as u64))?
244241
{
@@ -252,8 +249,6 @@ where
252249
))?
253250
{
254251
confirmed_txs.push(confirmed_tx);
255-
spent_outputs.insert(output.clone());
256-
continue;
257252
}
258253
}
259254
}
@@ -266,7 +261,7 @@ where
266261
tx1.block_height.cmp(&tx2.block_height).then_with(|| tx1.pos.cmp(&tx2.pos))
267262
});
268263

269-
Ok((confirmed_txs, spent_outputs))
264+
Ok(confirmed_txs)
270265
}
271266

272267
#[maybe_async]
@@ -378,6 +373,6 @@ where
378373

379374
fn register_output(&self, output: WatchedOutput) {
380375
let mut locked_queue = self.queue.lock().unwrap();
381-
locked_queue.outputs.insert(output);
376+
locked_queue.outputs.insert(output.outpoint.into_bitcoin_outpoint(), output);
382377
}
383378
}

lightning-transaction-sync/src/types.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use lightning::chain::WatchedOutput;
2-
use bitcoin::{Txid, BlockHash, Transaction, BlockHeader};
3-
use std::collections::HashSet;
2+
use bitcoin::{Txid, BlockHash, Transaction, BlockHeader, OutPoint};
3+
4+
use std::collections::{HashSet, HashMap};
45

56

67
// Represents the current state.
@@ -10,7 +11,7 @@ pub(crate) struct SyncState {
1011
pub watched_transactions: HashSet<Txid>,
1112
// Outputs that were previously processed, but must not be forgotten yet as
1213
// as we still need to monitor any spends on-chain.
13-
pub watched_outputs: HashSet<WatchedOutput>,
14+
pub watched_outputs: HashMap<OutPoint, WatchedOutput>,
1415
// The tip hash observed during our last sync.
1516
pub last_sync_hash: Option<BlockHash>,
1617
// Indicates whether we need to resync, e.g., after encountering an error.
@@ -21,7 +22,7 @@ impl SyncState {
2122
pub fn new() -> Self {
2223
Self {
2324
watched_transactions: HashSet::new(),
24-
watched_outputs: HashSet::new(),
25+
watched_outputs: HashMap::new(),
2526
last_sync_hash: None,
2627
pending_sync: false,
2728
}
@@ -34,14 +35,14 @@ pub(crate) struct FilterQueue {
3435
// Transactions that were registered via the `Filter` interface and have to be processed.
3536
pub transactions: HashSet<Txid>,
3637
// Outputs that were registered via the `Filter` interface and have to be processed.
37-
pub outputs: HashSet<WatchedOutput>,
38+
pub outputs: HashMap<OutPoint, WatchedOutput>,
3839
}
3940

4041
impl FilterQueue {
4142
pub fn new() -> Self {
4243
Self {
4344
transactions: HashSet::new(),
44-
outputs: HashSet::new(),
45+
outputs: HashMap::new(),
4546
}
4647
}
4748

0 commit comments

Comments
 (0)