Skip to content

Commit a85e453

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 15b482a commit a85e453

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
@@ -453,6 +453,12 @@ where
453453
self.sweeper_state.lock().unwrap().outputs.clone()
454454
}
455455

456+
/// Gets the latest best block which was connected either via the [`Listen`] or
457+
/// [`Confirm`] interfaces.
458+
pub fn current_best_block(&self) -> BestBlock {
459+
self.sweeper_state.lock().unwrap().best_block
460+
}
461+
456462
fn regenerate_spend_if_necessary(
457463
&self, sweeper_state: &mut SweeperState,
458464
) -> Option<Transaction> {
@@ -761,3 +767,56 @@ where
761767
})
762768
}
763769
}
770+
771+
impl<B: Deref, F: Deref, K: Deref, L: Deref>
772+
ReadableArgs<(
773+
B,
774+
Option<F>,
775+
K,
776+
L,
777+
Box<
778+
dyn Fn(&[&SpendableOutputDescriptor]) -> Result<Transaction, ()>
779+
+ Send
780+
+ Sync
781+
+ 'static,
782+
>,
783+
)> for (BestBlock, OutputSweeper<B, F, K, L>)
784+
where
785+
B::Target: BroadcasterInterface,
786+
F::Target: Filter + Sync + Send,
787+
K::Target: KVStore,
788+
L::Target: Logger,
789+
{
790+
#[inline]
791+
fn read<R: io::Read>(
792+
reader: &mut R,
793+
args: (
794+
B,
795+
Option<F>,
796+
K,
797+
L,
798+
Box<
799+
dyn Fn(&[&SpendableOutputDescriptor]) -> Result<Transaction, ()>
800+
+ Send
801+
+ Sync
802+
+ 'static,
803+
>,
804+
),
805+
) -> Result<Self, DecodeError> {
806+
let (broadcaster, chain_data_source, kv_store, logger, spend_outputs_callback) = args;
807+
let state = SweeperState::read(reader)?;
808+
let best_block = state.best_block;
809+
let sweeper_state = Mutex::new(state);
810+
Ok((
811+
best_block,
812+
OutputSweeper {
813+
sweeper_state,
814+
broadcaster,
815+
kv_store,
816+
chain_data_source,
817+
logger,
818+
spend_outputs_callback,
819+
},
820+
))
821+
}
822+
}

0 commit comments

Comments
 (0)