Skip to content

Commit

Permalink
Merge pull request #999 from MutinyWallet/activity-index
Browse files Browse the repository at this point in the history
Create index for sorted activity
  • Loading branch information
benthecarman authored Mar 21, 2024
2 parents 4bd50dd + 6dcc05c commit caf1175
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 112 deletions.
9 changes: 9 additions & 0 deletions mutiny-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use aes::cipher::block_padding::UnpadError;
use bdk::signer::SignerError;
use bdk::wallet::error::BuildFeeBumpError;
use bdk::wallet::tx_builder::AddUtxoError;
use hex_conservative::HexToArrayError;
use lightning::ln::channelmanager::RetryableSendFailure;
use lightning::ln::peer_handler::PeerHandleError;
use lightning_invoice::ParseOrSemanticError;
Expand Down Expand Up @@ -452,6 +453,14 @@ impl From<bitcoin::hashes::hex::Error> for MutinyError {
}
}

impl From<HexToArrayError> for MutinyError {
fn from(value: HexToArrayError) -> Self {
MutinyError::ReadError {
source: MutinyStorageError::Other(anyhow::anyhow!(value)),
}
}
}

impl From<bitcoin::address::Error> for MutinyError {
fn from(e: bitcoin::address::Error) -> Self {
match e {
Expand Down
38 changes: 18 additions & 20 deletions mutiny-core/src/ldkstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::logging::MutinyLogger;
use crate::node::{default_user_config, ChainMonitor};
use crate::node::{NetworkGraph, Router};
use crate::nodemanager::ChannelClosure;
use crate::storage::{MutinyStorage, VersionedValue};
use crate::storage::{IndexItem, MutinyStorage, VersionedValue};
use crate::utils;
use crate::utils::{sleep, spawn};
use crate::{chain::MutinyChain, scorer::HubPreferentialScorer};
Expand Down Expand Up @@ -40,7 +40,7 @@ use std::sync::Arc;
pub const CHANNEL_MANAGER_KEY: &str = "manager";
pub const MONITORS_PREFIX_KEY: &str = "monitors/";
const CHANNEL_OPENING_PARAMS_PREFIX: &str = "chan_open_params/";
const CHANNEL_CLOSURE_PREFIX: &str = "channel_closure/";
pub const CHANNEL_CLOSURE_PREFIX: &str = "channel_closure/";
const FAILED_SPENDABLE_OUTPUT_DESCRIPTOR_KEY: &str = "failed_spendable_outputs";

pub(crate) type PhantomChannelManager<S: MutinyStorage> = LdkChannelManager<
Expand Down Expand Up @@ -367,7 +367,16 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
"{CHANNEL_CLOSURE_PREFIX}{}",
user_channel_id.to_be_bytes().to_lower_hex_string()
));
self.storage.set_data(key, closure, None)?;
self.storage.set_data(key.clone(), &closure, None)?;

let index = self.storage.activity_index();
let mut index = index.try_write()?;
index.retain(|i| i.key != key); // remove old version
index.insert(IndexItem {
timestamp: Some(closure.timestamp),
key,
});

Ok(())
}

Expand All @@ -379,28 +388,17 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
"{CHANNEL_CLOSURE_PREFIX}{}",
user_channel_id.to_be_bytes().to_lower_hex_string()
));
self.storage.get_data(key)
self.storage.get_channel_closure(&key)
}

pub(crate) fn list_channel_closures(&self) -> Result<Vec<(u128, ChannelClosure)>, MutinyError> {
pub(crate) fn list_channel_closures(&self) -> Result<Vec<ChannelClosure>, MutinyError> {
let suffix = format!("_{}", self.node_id);
let map: HashMap<String, ChannelClosure> =
self.storage.scan(CHANNEL_CLOSURE_PREFIX, Some(&suffix))?;

Ok(map
.into_iter()
.map(|(key, value)| {
// convert keys to u128
let user_channel_id_str = key
.trim_start_matches(CHANNEL_CLOSURE_PREFIX)
.trim_end_matches(&suffix);
let user_channel_id: [u8; 16] = FromHex::from_hex(user_channel_id_str)
.unwrap_or_else(|_| panic!("key should be a u128 got {user_channel_id_str}"));

let user_channel_id = u128::from_be_bytes(user_channel_id);
(user_channel_id, value)
})
.collect())
map.into_iter()
.map(|(key, mut closure)| closure.set_user_channel_id_from_key(&key).map(|_| closure))
.collect::<Result<Vec<_>, _>>()
}

/// Persists the failed spendable outputs to storage.
Expand Down Expand Up @@ -821,7 +819,7 @@ mod test {
assert!(result.is_ok());

let result = persister.list_channel_closures().unwrap();
assert_eq!(result, vec![(user_channel_id, closure.clone())]);
assert_eq!(result, vec![closure.clone()]);

let result = persister.get_channel_closure(user_channel_id).unwrap();
assert_eq!(result, Some(closure));
Expand Down
Loading

0 comments on commit caf1175

Please sign in to comment.