Skip to content

Commit 1d48b01

Browse files
committed
Implement ReadableArgs on (BestBlock, OutputSweeper)
.. and allow to query the best currently known block. This allows to detect from which block to resume chain syncing.
1 parent 12328f1 commit 1d48b01

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

lightning/src/util/sweep.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ where
507507
self.sweeper_state.lock().unwrap().outputs.clone()
508508
}
509509

510+
/// Gets the latest best block which was connected either via the [`Listen`] or
511+
/// [`Confirm`] interfaces.
512+
pub fn current_best_block(&self) -> BestBlock {
513+
self.sweeper_state.lock().unwrap().best_block
514+
}
515+
510516
fn regenerate_spend_if_necessary(
511517
&self, sweeper_state: &mut SweeperState,
512518
) -> Option<Transaction> {
@@ -798,3 +804,56 @@ where
798804
})
799805
}
800806
}
807+
808+
impl<B: Deref, F: Deref, K: Deref, L: Deref>
809+
ReadableArgs<(
810+
B,
811+
Option<F>,
812+
K,
813+
L,
814+
Box<
815+
dyn Fn(&[&SpendableOutputDescriptor]) -> Result<Transaction, ()>
816+
+ Send
817+
+ Sync
818+
+ 'static,
819+
>,
820+
)> for (BestBlock, OutputSweeper<B, F, K, L>)
821+
where
822+
B::Target: BroadcasterInterface,
823+
F::Target: Filter + Sync + Send,
824+
K::Target: KVStore,
825+
L::Target: Logger,
826+
{
827+
#[inline]
828+
fn read<R: io::Read>(
829+
reader: &mut R,
830+
args: (
831+
B,
832+
Option<F>,
833+
K,
834+
L,
835+
Box<
836+
dyn Fn(&[&SpendableOutputDescriptor]) -> Result<Transaction, ()>
837+
+ Send
838+
+ Sync
839+
+ 'static,
840+
>,
841+
),
842+
) -> Result<Self, DecodeError> {
843+
let (broadcaster, chain_data_source, kv_store, logger, spend_outputs_callback) = args;
844+
let state = SweeperState::read(reader)?;
845+
let best_block = state.best_block;
846+
let sweeper_state = Mutex::new(state);
847+
Ok((
848+
best_block,
849+
OutputSweeper {
850+
sweeper_state,
851+
broadcaster,
852+
kv_store,
853+
chain_data_source,
854+
logger,
855+
spend_outputs_callback,
856+
},
857+
))
858+
}
859+
}

0 commit comments

Comments
 (0)