Skip to content

Commit d15fa4d

Browse files
committed
Add OutputSweeper and persistence utils
We add an `OutputSweeper` object that will keep track of sweepable outputs. To this end, we start by adding the general structures and the required utilities to persist the `SpendableOutputStatus` to our `KVStore`.
1 parent ea31d2d commit d15fa4d

File tree

6 files changed

+596
-25
lines changed

6 files changed

+596
-25
lines changed

src/event.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,7 @@ where
611611
);
612612

613613
match res {
614-
Ok(Some(spending_tx)) => {
615-
self.tx_broadcaster.broadcast_transactions(&[&spending_tx])
616-
}
617-
Ok(None) => {
618-
log_debug!(self.logger, "Omitted spending static outputs: {:?}", outputs);
619-
}
614+
Ok(spending_tx) => self.tx_broadcaster.broadcast_transactions(&[&spending_tx]),
620615
Err(err) => {
621616
log_error!(self.logger, "Error spending outputs: {:?}", err);
622617
}

src/io/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ pub(crate) const PEER_INFO_PERSISTENCE_KEY: &str = "peers";
2121
pub(crate) const PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE: &str = "payments";
2222
pub(crate) const PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
2323

24+
/// The spendable output information will be persisted under this prefix.
25+
pub(crate) const SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE: &str = "spendable_outputs";
26+
pub(crate) const SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
27+
2428
/// RapidGossipSync's `latest_sync_timestamp` will be persisted under this key.
2529
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_PRIMARY_NAMESPACE: &str = "";
2630
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_SECONDARY_NAMESPACE: &str = "";

src/io/utils.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::WALLET_KEYS_SEED_LEN;
33

44
use crate::logger::log_error;
55
use crate::peer_store::PeerStore;
6+
use crate::sweep::SpendableOutputInfo;
67
use crate::{Error, EventQueue, PaymentDetails};
78

89
use lightning::routing::gossip::NetworkGraph;
@@ -199,6 +200,36 @@ where
199200
Ok(res)
200201
}
201202

203+
/// Read previously persisted spendable output information from the store.
204+
pub(crate) fn read_spendable_outputs<K: KVStore + Sync + Send, L: Deref>(
205+
kv_store: Arc<K>, logger: L,
206+
) -> Result<Vec<SpendableOutputInfo>, std::io::Error>
207+
where
208+
L::Target: Logger,
209+
{
210+
let mut res = Vec::new();
211+
212+
for stored_key in kv_store.list(
213+
SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE,
214+
SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE,
215+
)? {
216+
let mut reader = Cursor::new(kv_store.read(
217+
SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE,
218+
SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE,
219+
&stored_key,
220+
)?);
221+
let output = SpendableOutputInfo::read(&mut reader).map_err(|e| {
222+
log_error!(logger, "Failed to deserialize SpendableOutputInfo: {}", e);
223+
std::io::Error::new(
224+
std::io::ErrorKind::InvalidData,
225+
"Failed to deserialize SpendableOutputInfo",
226+
)
227+
})?;
228+
res.push(output);
229+
}
230+
Ok(res)
231+
}
232+
202233
pub(crate) fn read_latest_rgs_sync_timestamp<K: KVStore + Sync + Send, L: Deref>(
203234
kv_store: Arc<K>, logger: L,
204235
) -> Result<u32, std::io::Error>

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub mod io;
8585
mod logger;
8686
mod payment_store;
8787
mod peer_store;
88+
mod sweep;
8889
#[cfg(test)]
8990
mod test;
9091
mod tx_broadcaster;

0 commit comments

Comments
 (0)