Skip to content

Commit

Permalink
Merge pull request #913 from MutinyWallet/remove-redshift
Browse files Browse the repository at this point in the history
Remove redshift
  • Loading branch information
TonyGiorgio authored Dec 24, 2023
2 parents d18d4bb + b0681a4 commit f17881b
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 883 deletions.
28 changes: 0 additions & 28 deletions mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::lsp::{AnyLsp, Lsp};
use crate::node::BumpTxEventHandler;
use crate::nodemanager::ChannelClosure;
use crate::onchain::OnChainWallet;
use crate::redshift::RedshiftStorage;
use crate::storage::MutinyStorage;
use crate::utils::sleep;
use anyhow::anyhow;
Expand Down Expand Up @@ -50,10 +49,6 @@ impl MillisatAmount {
pub fn is_none(&self) -> bool {
self.0.is_none()
}

pub fn amount_sats(&self) -> u64 {
self.0.unwrap_or(0) / 1_000
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -557,29 +552,6 @@ impl<S: MutinyStorage> EventHandler<S> {
user_channel_id,
counterparty_node_id.to_hex(),
channel_type);

// Channel is ready, if it is a redshift channel, should update the status.
if let Ok(Some(mut redshift)) = self
.persister
.storage
.get_redshift(&user_channel_id.to_be_bytes())
{
// get channel
if let Some(chan) = self
.channel_manager
.list_channels_with_counterparty(&counterparty_node_id)
.iter()
.find(|c| c.channel_id == channel_id)
{
// update status, unwrap is safe because the channel is ready
redshift.channel_opened(chan.funding_txo.unwrap().into_bitcoin_outpoint());

// persist
if let Err(e) = self.persister.storage.persist_redshift(redshift) {
log_error!(self.logger, "Failed to persist redshift: {e}");
}
}
}
}
Event::ChannelPending {
channel_id,
Expand Down
7 changes: 0 additions & 7 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub mod nodemanager;
pub mod nostr;
mod onchain;
mod peermanager;
pub mod redshift;
pub mod scorer;
pub mod sql;
pub mod storage;
Expand Down Expand Up @@ -432,11 +431,6 @@ impl<S: MutinyStorage> MutinyWallet<S> {
);
NodeManager::start_sync(self.node_manager.clone());

// Redshifts disabled in safe mode
if !self.config.safe_mode {
NodeManager::start_redshifts(self.node_manager.clone());
}

Ok(())
}

Expand Down Expand Up @@ -954,7 +948,6 @@ impl<S: MutinyStorage> MutinyWallet<S> {
/// Stops all of the nodes and background processes.
/// Returns after node has been stopped.
pub async fn stop(&self) -> Result<(), MutinyError> {
// TODO stop redshift and NWC as well
self.node_manager.stop().await
}

Expand Down
77 changes: 0 additions & 77 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::logging::LOGGING_KEY;
use crate::redshift::{RedshiftManager, RedshiftStatus, RedshiftStorage};
use crate::storage::{MutinyStorage, DEVICE_ID_KEY, KEYCHAIN_STORE_KEY, NEED_FULL_SYNC_KEY};
use crate::utils::{sleep, spawn};
use crate::MutinyWalletConfig;
Expand Down Expand Up @@ -649,13 +648,6 @@ impl<S: MutinyStorage> NodeManager<S> {
Ok(nm)
}

/// Returns the node with the given pubkey
pub(crate) async fn get_node(&self, pk: &PublicKey) -> Result<Arc<Node<S>>, MutinyError> {
let nodes = self.nodes.lock().await;
let node = nodes.get(pk).ok_or(MutinyError::NotFound)?;
Ok(node.clone())
}

// New function to get a node by PublicKey or return the first node
pub(crate) async fn get_node_by_key_or_first(
&self,
Expand Down Expand Up @@ -703,75 +695,6 @@ impl<S: MutinyStorage> NodeManager<S> {
Ok(())
}

/// Starts a background tasks to poll redshifts until they are ready and then start attempting payments.
///
/// This function will first find redshifts that are in the [RedshiftStatus::AttemptingPayments] state and start attempting payments
/// and redshifts that are in the [RedshiftStatus::ClosingChannels] state and finish closing channels.
/// This is done in case the node manager was shutdown while attempting payments or closing channels.
pub(crate) fn start_redshifts(nm: Arc<NodeManager<S>>) {
// find AttemptingPayments redshifts and restart attempting payments
// find ClosingChannels redshifts and restart closing channels
// use unwrap_or_default() to handle errors
let all = nm.storage.get_redshifts().unwrap_or_default();
for redshift in all {
match redshift.status {
RedshiftStatus::AttemptingPayments => {
// start attempting payments
let nm_clone = nm.clone();
utils::spawn(async move {
if let Err(e) = nm_clone.attempt_payments(redshift).await {
log_error!(nm_clone.logger, "Error attempting redshift payments: {e}");
}
});
}
RedshiftStatus::ClosingChannels => {
// finish closing channels
let nm_clone = nm.clone();
utils::spawn(async move {
if let Err(e) = nm_clone.close_channels(redshift).await {
log_error!(nm_clone.logger, "Error closing redshift channels: {e}");
}
});
}
_ => {} // ignore other statuses
}
}

utils::spawn(async move {
loop {
if nm.stop.load(Ordering::Relaxed) {
break;
}
// find redshifts with channels ready
// use unwrap_or_default() to handle errors
let all = nm.storage.get_redshifts().unwrap_or_default();
for mut redshift in all {
if redshift.status == RedshiftStatus::ChannelOpened {
// update status
redshift.status = RedshiftStatus::AttemptingPayments;
if let Err(e) = nm.storage.persist_redshift(redshift.clone()) {
log_error!(nm.logger, "Error persisting redshift status update: {e}");
}

// start attempting payments
let payment_nm = nm.clone();
utils::spawn(async move {
if let Err(e) = payment_nm.attempt_payments(redshift).await {
log_error!(
payment_nm.logger,
"Error attempting redshift payments: {e}"
);
}
});
}
}

// sleep 10 seconds
sleep(10_000).await;
}
});
}

/// Creates a background process that will sync the wallet with the blockchain.
/// This will also update the fee estimates every 10 minutes.
pub fn start_sync(nm: Arc<NodeManager<S>>) {
Expand Down
Loading

0 comments on commit f17881b

Please sign in to comment.