Skip to content

Commit

Permalink
Merge branch 'master' into send-payjoin
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould authored Jul 7, 2023
2 parents 562087e + bf98cfc commit 4e13614
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 137 deletions.
8 changes: 4 additions & 4 deletions mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ homepage = "https://mutinywallet.com"
repository = "https://github.com/mutinywallet/mutiny-node"

[dependencies]
lnurl-rs = { version = "=0.2.4", default-features = false, features = ["async", "async-https"] }
lnurl-rs = { git = "https://github.com/TonyGiorgio/lnurl-rs.git", branch = "ldk-116-0d107", default-features = false, features = ["async", "async-https"] }

cfg-if = "1.0.0"
bip39 = { version = "2.0.0" }
Expand All @@ -26,9 +26,9 @@ serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
uuid = { version = "1.1.2", features = ["v4"] }
esplora-client = { version = "0.5", default-features = false }
lightning = { version = "0.0.115", default-features = false, features = ["max_level_trace", "grind_signatures", "no-std"] }
lightning-invoice = { version = "0.23", default-features = false, features = ["no-std"] }
lightning-rapid-gossip-sync = { version = "0.0.115", default-features = false, features = ["no-std"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning.git", rev = "0d1072b7c3fb5366742473c38069c421cdd60b87", default-features = false, features = ["max_level_trace", "grind_signatures", "no-std"] }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning.git", rev = "0d1072b7c3fb5366742473c38069c421cdd60b87", default-features = false, features = ["no-std"] }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning.git", rev = "0d1072b7c3fb5366742473c38069c421cdd60b87", default-features = false, features = ["no-std"] }
chrono = "0.4.22"
futures-util = { version = "0.3", default-features = false }
reqwest = { version = "0.11", default-features = false, features = ["json"] }
Expand Down
95 changes: 38 additions & 57 deletions mutiny-core/src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ extern crate lightning_rapid_gossip_sync;
use lightning::chain;
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
use lightning::chain::chainmonitor::{ChainMonitor, Persist};
use lightning::chain::keysinterface::{EntropySource, NodeSigner, SignerProvider};
use lightning::events::{Event, PathFailure};
use lightning::ln::channelmanager::ChannelManager;
use lightning::ln::msgs::{ChannelMessageHandler, OnionMessageHandler, RoutingMessageHandler};
use lightning::ln::peer_handler::{CustomMessageHandler, PeerManager, SocketDescriptor};
use lightning::ln::peer_handler::APeerManager;
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
use lightning::routing::router::Router;
use lightning::routing::scoring::{Score, WriteableScore};
use lightning::routing::utxo::UtxoLookup;
use lightning::sign::{EntropySource, NodeSigner, SignerProvider};
use lightning::util::logger::Logger;
use lightning::util::persist::Persister;
use lightning_rapid_gossip_sync::RapidGossipSync;
Expand All @@ -59,7 +58,7 @@ const PING_TIMER: u64 = 1;
const NETWORK_PRUNE_TIMER: u64 = 60 * 60;

#[cfg(not(test))]
const SCORER_PERSIST_TIMER: u64 = 30;
const SCORER_PERSIST_TIMER: u64 = 60 * 60;
#[cfg(test)]
const SCORER_PERSIST_TIMER: u64 = 1;

Expand Down Expand Up @@ -165,14 +164,7 @@ impl<
R: Deref<Target = RapidGossipSync<G, L>>,
G: Deref<Target = NetworkGraph<L>>,
L: Deref,
>
GossipSync<
&P2PGossipSync<G, &'a (dyn UtxoLookup + Send + Sync), L>,
R,
G,
&'a (dyn UtxoLookup + Send + Sync),
L,
>
> GossipSync<&P2PGossipSync<G, &'a (dyn UtxoLookup), L>, R, G, &'a (dyn UtxoLookup), L>
where
L::Target: Logger,
{
Expand All @@ -185,10 +177,10 @@ where
/// This is not exported to bindings users as the bindings concretize everything and have constructors for us
impl<'a, L: Deref>
GossipSync<
&P2PGossipSync<&'a NetworkGraph<L>, &'a (dyn UtxoLookup + Send + Sync), L>,
&P2PGossipSync<&'a NetworkGraph<L>, &'a (dyn UtxoLookup), L>,
&RapidGossipSync<&'a NetworkGraph<L>, L>,
&'a NetworkGraph<L>,
&'a (dyn UtxoLookup + Send + Sync),
&'a (dyn UtxoLookup),
L,
>
where
Expand All @@ -215,10 +207,12 @@ where
}
}

/// Updates scorer based on event and returns whether an update occurred so we can decide whether
/// to persist.
fn update_scorer<'a, S: 'static + Deref<Target = SC>, SC: 'a + WriteableScore<'a>>(
scorer: &'a S,
event: &Event,
) {
) -> bool {
let mut score = scorer.lock();
match event {
Event::PaymentPathFailed {
Expand Down Expand Up @@ -250,8 +244,9 @@ fn update_scorer<'a, S: 'static + Deref<Target = SC>, SC: 'a + WriteableScore<'a
} => {
score.probe_failed(path, *scid);
}
_ => {}
_ => return false,
}
true
}

macro_rules! define_run_body {
Expand Down Expand Up @@ -288,7 +283,7 @@ macro_rules! define_run_body {
// ChannelManager, we want to minimize methods blocking on a ChannelManager
// generally, and as a fallback place such blocking only immediately before
// persistence.
$peer_manager.process_events();
$peer_manager.as_ref().process_events();

// Exit the loop if the background processor was requested to stop.
if $loop_exit_check {
Expand Down Expand Up @@ -333,28 +328,29 @@ macro_rules! define_run_body {
// more than a handful of seconds to complete, and shouldn't disconnect all our
// peers.
log_trace!($logger, "100ms sleep took more than a second, disconnecting peers.");
$peer_manager.disconnect_all_peers();
$peer_manager.as_ref().disconnect_all_peers();
last_ping_call = $get_timer(PING_TIMER);
} else if $timer_elapsed(&mut last_ping_call, PING_TIMER) {
log_trace!($logger, "Calling PeerManager's timer_tick_occurred");
$peer_manager.timer_tick_occurred();
$peer_manager.as_ref().timer_tick_occurred();
last_ping_call = $get_timer(PING_TIMER);
}

// Note that we want to run a graph prune once not long after startup before
// falling back to our usual hourly prunes. This avoids short-lived clients never
// pruning their network graph. We run once 60 seconds after startup before
// continuing our normal cadence.
// continuing our normal cadence. For RGS, since 60 seconds is likely too long,
// we prune after an initial sync completes.
let prune_timer = if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER };
if $timer_elapsed(&mut last_prune_call, prune_timer) {
let prune_timer_elapsed = $timer_elapsed(&mut last_prune_call, prune_timer);
let should_prune = match $gossip_sync {
GossipSync::Rapid(_) => !have_pruned || prune_timer_elapsed,
_ => prune_timer_elapsed,
};
if should_prune {
// The network graph must not be pruned while rapid sync completion is pending
if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
#[cfg(feature = "std")] {
log_trace!($logger, "Pruning and persisting network graph.");
network_graph.remove_stale_channels_and_tracking();
}
#[cfg(not(feature = "std"))] {
// log_warn!($logger, "Not pruning network graph, consider enabling `std` or doing so manually with remove_stale_channels_and_tracking_with_time.");
log_trace!($logger, "Persisting network graph.");
}

Expand Down Expand Up @@ -404,7 +400,7 @@ macro_rules! define_run_body {
} }
}

pub(crate) mod bg_futures_util {
pub(crate) mod futures_util {
use core::future::Future;
use core::marker::Unpin;
use core::pin::Pin;
Expand Down Expand Up @@ -475,27 +471,9 @@ pub(crate) mod bg_futures_util {
unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE)) }
}
}
use bg_futures_util::{dummy_waker, Selector, SelectorOutput};
use crate::background::futures_util::{dummy_waker, Selector, SelectorOutput};
use core::task;

/// Processes background events in a future.
///
/// `sleeper` should return a future which completes in the given amount of time and returns a
/// boolean indicating whether the background processing should exit. Once `sleeper` returns a
/// future which outputs `true`, the loop will exit and this function's future will complete.
/// The `sleeper` future is free to return early after it has triggered the exit condition.
///
/// See [`BackgroundProcessor::start`] for information on which actions this handles.
///
/// Requires the `futures` feature. Note that while this method is available without the `std`
/// feature, doing so will skip calling [`NetworkGraph::remove_stale_channels_and_tracking`],
/// you should call [`NetworkGraph::remove_stale_channels_and_tracking_with_time`] regularly
/// manually instead.
///
/// The `mobile_interruptable_platform` flag should be set if we're currently running on a
/// mobile device, where we may need to check for interruption of the application regularly. If you
/// are unsure, you should set the flag, as the performance impact of it is minimal unless there
/// are hundreds or thousands of simultaneous process calls running.
pub async fn process_events_async<
'a,
UL: 'static + Deref,
Expand All @@ -510,19 +488,15 @@ pub async fn process_events_async<
G: 'static + Deref<Target = NetworkGraph<L>>,
L: 'static + Deref,
P: 'static + Deref,
Descriptor: 'static + SocketDescriptor,
CMH: 'static + Deref,
RMH: 'static + Deref,
OMH: 'static + Deref,
EventHandlerFuture: core::future::Future<Output = ()>,
EventHandler: Fn(Event) -> EventHandlerFuture,
PS: 'static + Deref,
M: 'static + Deref<Target = ChainMonitor<<SP::Target as SignerProvider>::Signer, CF, T, F, L, P>>,
CM: 'static + Deref<Target = ChannelManager<CW, T, ES, NS, SP, F, R, L>>,
PGS: 'static + Deref<Target = P2PGossipSync<G, UL, L>>,
RGS: 'static + Deref<Target = RapidGossipSync<G, L>>,
UMH: 'static + Deref,
PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, OMH, L, UMH, NS>>,
APM: APeerManager,
PM: 'static + Deref<Target = APM>,
S: 'static + Deref<Target = SC>,
SC: for<'b> WriteableScore<'b>,
SleepFuture: core::future::Future<Output = bool> + core::marker::Unpin,
Expand Down Expand Up @@ -551,23 +525,30 @@ where
R::Target: 'static + Router,
L::Target: 'static + Logger,
P::Target: 'static + Persist<<SP::Target as SignerProvider>::Signer>,
CMH::Target: 'static + ChannelMessageHandler,
OMH::Target: 'static + OnionMessageHandler,
RMH::Target: 'static + RoutingMessageHandler,
UMH::Target: 'static + CustomMessageHandler,
PS::Target: 'static + Persister<'a, CW, T, ES, NS, SP, F, R, L, SC>,
{
let mut should_break = false;
let async_event_handler = |event| {
let network_graph = gossip_sync.network_graph();
let event_handler = &event_handler;
let scorer = &scorer;
let logger = &logger;
let persister = &persister;
async move {
if let Some(network_graph) = network_graph {
handle_network_graph_update(network_graph, &event)
}
if let Some(ref scorer) = scorer {
update_scorer(scorer, &event);
if update_scorer(scorer, &event) {
log_trace!(logger, "Persisting scorer after update");
if let Err(e) = persister.persist_scorer(&scorer) {
log_error!(
logger,
"Error: Failed to persist scorer, check your disk and permissions {}",
e
)
}
}
}
event_handler(event).await;
}
Expand Down
13 changes: 9 additions & 4 deletions mutiny-core/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ impl<S: MutinyStorage> Filter for MutinyChain<S> {
}

impl<S: MutinyStorage> BroadcasterInterface for MutinyChain<S> {
fn broadcast_transaction(&self, tx: &Transaction) {
let tx_clone = tx.clone();
fn broadcast_transactions(&self, txs: &[&Transaction]) {
let txs_clone = txs
.iter()
.map(|tx| (*tx).clone())
.collect::<Vec<Transaction>>();
let wallet = self.wallet.clone();
let logger = self.logger.clone();
utils::spawn(async move {
if let Err(e) = wallet.broadcast_transaction(tx_clone).await {
log_warn!(logger, "Error broadcasting transaction: {e}")
for tx in txs_clone {
if let Err(e) = wallet.broadcast_transaction(tx).await {
log_warn!(logger, "Error broadcasting transaction: {e}")
}
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ use lightning::{log_debug, log_error, log_info, log_trace};

use bitcoin::{BlockHash, Script, Txid};

use esplora_client::r#async::AsyncClient;
use esplora_client::Builder;
use bdk_esplora::esplora_client::r#async::AsyncClient;
use bdk_esplora::esplora_client::Builder;

use core::ops::Deref;
use std::collections::HashSet;
Expand Down
3 changes: 2 additions & 1 deletion mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use anyhow::anyhow;
use bitcoin::hashes::hex::ToHex;
use bitcoin::secp256k1::PublicKey;
use bitcoin::secp256k1::Secp256k1;
use lightning::chain::keysinterface::SpendableOutputDescriptor;
use lightning::events::{Event, PaymentPurpose};
use lightning::sign::SpendableOutputDescriptor;
use lightning::{
chain::chaininterface::{ConfirmationTarget, FeeEstimator},
log_debug, log_error, log_info, log_warn,
Expand Down Expand Up @@ -547,6 +547,7 @@ impl<S: MutinyStorage> EventHandler<S> {
}
}
Event::HTLCIntercepted { .. } => {}
Event::BumpTransaction(_) => {} // we do not support anchors
}
}

Expand Down
9 changes: 5 additions & 4 deletions mutiny-core/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use std::sync::Arc;

use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::Network;
use lightning::ln::msgs::NodeAnnouncement;
use lightning::routing::gossip::NodeId;
use lightning::routing::scoring::ProbabilisticScoringParameters;
use lightning::util::logger::Logger;
use lightning::util::ser::{ReadableArgs, Writeable};
use lightning::{
ln::msgs::NodeAnnouncement, routing::scoring::ProbabilisticScoringDecayParameters,
};
use lightning::{log_debug, log_error, log_info, log_warn};
use reqwest::Client;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -67,7 +68,7 @@ async fn get_gossip_data(
Some(prob_scorer_str) => {
let prob_scorer_bytes: Vec<u8> = Vec::from_hex(&prob_scorer_str)?;
let mut readable_bytes = lightning::io::Cursor::new(prob_scorer_bytes);
let params = ProbabilisticScoringParameters::default();
let params = ProbabilisticScoringDecayParameters::default();
let args = (params, Arc::clone(&network_graph), Arc::clone(&logger));
ProbScorer::read(&mut readable_bytes, args)
}
Expand Down Expand Up @@ -142,7 +143,7 @@ pub async fn get_gossip_sync(
let prob_scorer = match gossip_data.scorer {
Some(scorer) => scorer,
None => {
let params = ProbabilisticScoringParameters::default();
let params = ProbabilisticScoringDecayParameters::default();
ProbScorer::new(params, gossip_data.network_graph.clone(), logger.clone())
}
};
Expand Down
23 changes: 12 additions & 11 deletions mutiny-core/src/keymanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use bitcoin::secp256k1::ecdsa::Signature;
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, Signing};
use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey};
use bitcoin::{Script, Transaction, TxOut};
use lightning::chain::keysinterface::{
use lightning::ln::msgs::{DecodeError, UnsignedGossipMessage};
use lightning::ln::script::ShutdownScript;
use lightning::log_warn;
use lightning::sign::{
EntropySource, InMemorySigner, KeyMaterial, NodeSigner,
PhantomKeysManager as LdkPhantomKeysManager, Recipient, SignerProvider,
SpendableOutputDescriptor,
};
use lightning::ln::msgs::{DecodeError, UnsignedGossipMessage};
use lightning::ln::script::ShutdownScript;
use lightning::log_warn;
use lightning::util::logger::Logger;
use std::sync::Arc;

Expand Down Expand Up @@ -69,6 +69,7 @@ impl<S: MutinyStorage> PhantomKeysManager<S> {
outputs,
address.script_pubkey(),
feerate_sat_per_1000_weight,
None, // tx locktime of 0
secp_ctx,
);

Expand Down Expand Up @@ -156,21 +157,21 @@ impl<S: MutinyStorage> SignerProvider for PhantomKeysManager<S> {
self.inner.read_chan_signer(reader)
}

fn get_destination_script(&self) -> Script {
let mut wallet = self.wallet.wallet.try_write().unwrap();
wallet
fn get_destination_script(&self) -> Result<Script, ()> {
let mut wallet = self.wallet.wallet.try_write().map_err(|_| ())?;
Ok(wallet
.get_address(AddressIndex::New)
.address
.script_pubkey()
.script_pubkey())
}

fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
let mut wallet = self.wallet.wallet.try_write().unwrap();
fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
let mut wallet = self.wallet.wallet.try_write().map_err(|_| ())?;
let script = wallet
.get_address(AddressIndex::New)
.address
.script_pubkey();
ShutdownScript::try_from(script).unwrap()
ShutdownScript::try_from(script).map_err(|_| ())
}
}

Expand Down
Loading

0 comments on commit 4e13614

Please sign in to comment.