Skip to content

Commit

Permalink
Merge pull request #953 from MutinyWallet/mv-channel-config-change
Browse files Browse the repository at this point in the history
Move channel config load after watch_channel
  • Loading branch information
benthecarman authored Jan 9, 2024
2 parents 1492d8b + c643a79 commit 76c96f6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 53 deletions.
12 changes: 10 additions & 2 deletions mutiny-core/src/ldkstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
pub(crate) async fn read_channel_manager(
&self,
network: Network,
accept_underpaying_htlcs: bool,
chain_monitor: Arc<ChainMonitor<S>>,
mutiny_chain: Arc<MutinyChain<S>>,
fee_estimator: Arc<MutinyFeeEstimator<S>>,
Expand All @@ -215,6 +216,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
let bytes = FromHex::from_hex(&hex)?;
let res = Self::parse_channel_manager(
bytes,
accept_underpaying_htlcs,
chain_monitor,
mutiny_chain,
fee_estimator,
Expand All @@ -234,6 +236,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {

Self::create_new_channel_manager(
network,
accept_underpaying_htlcs,
chain_monitor,
mutiny_chain,
fee_estimator,
Expand All @@ -250,6 +253,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
let bytes = self.read_value(CHANNEL_MANAGER_KEY)?;
Self::parse_channel_manager(
bytes,
accept_underpaying_htlcs,
chain_monitor,
mutiny_chain,
fee_estimator,
Expand All @@ -265,6 +269,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
#[allow(clippy::too_many_arguments)]
fn parse_channel_manager(
bytes: Vec<u8>,
accept_underpaying_htlcs: bool,
chain_monitor: Arc<ChainMonitor<S>>,
mutiny_chain: Arc<MutinyChain<S>>,
fee_estimator: Arc<MutinyFeeEstimator<S>>,
Expand All @@ -286,7 +291,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
mutiny_chain,
router,
mutiny_logger,
default_user_config(),
default_user_config(accept_underpaying_htlcs),
channel_monitor_mut_references,
);
let mut readable_kv_value = Cursor::new(bytes);
Expand All @@ -307,6 +312,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
#[allow(clippy::too_many_arguments)]
pub(crate) async fn create_new_channel_manager(
network: Network,
accept_underpaying_htlcs: bool,
chain_monitor: Arc<ChainMonitor<S>>,
mutiny_chain: Arc<MutinyChain<S>>,
fee_estimator: Arc<MutinyFeeEstimator<S>>,
Expand Down Expand Up @@ -344,7 +350,7 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
keys_manager.clone(),
keys_manager.clone(),
keys_manager,
default_user_config(),
default_user_config(accept_underpaying_htlcs),
chain_params,
utils::now().as_secs() as u32,
);
Expand Down Expand Up @@ -978,6 +984,7 @@ mod test {
let read = persister
.read_channel_manager(
network,
false,
chain_monitor.clone(),
chain.clone(),
fees.clone(),
Expand All @@ -1001,6 +1008,7 @@ mod test {
let read = persister
.read_channel_manager(
network,
false,
chain_monitor,
chain.clone(),
fees.clone(),
Expand Down
14 changes: 14 additions & 0 deletions mutiny-core/src/lsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ impl LspConfig {
token,
})
}

pub fn accept_underpaying_htlcs(&self) -> bool {
match self {
LspConfig::VoltageFlow(_) => false,
LspConfig::Lsps(_) => true,
}
}
}

pub fn deserialize_lsp_config<'de, D>(deserializer: D) -> Result<Option<LspConfig>, D::Error>
Expand Down Expand Up @@ -128,6 +135,13 @@ impl<S: MutinyStorage> AnyLsp<S> {
)?;
Ok(Self::Lsps(lsps_client))
}

pub fn accept_underpaying_htlcs(&self) -> bool {
match self {
AnyLsp::VoltageFlow(_) => false,
AnyLsp::Lsps(_) => true,
}
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
Expand Down
114 changes: 63 additions & 51 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,31 @@ impl<S: MutinyStorage> NodeBuilder<S> {
scoring_params(),
));

log_info!(logger, "creating lsp client");
let lsp_config: Option<LspConfig> = match node_index.lsp {
None => {
log_info!(logger, "no lsp saved, using configured one if present");
self.lsp_config
}
Some(ref lsp) => {
if self.lsp_config.as_ref() == Some(lsp) {
log_info!(logger, "lsp config matches saved lsp config");
self.lsp_config
} else {
log_info!(logger, "lsp config does not match saved lsp config");
None
}
}
};

// init channel manager
let accept_underpaying_htlcs = lsp_config
.as_ref()
.is_some_and(|l| l.accept_underpaying_htlcs());
let mut read_channel_manager = persister
.read_channel_manager(
network,
accept_underpaying_htlcs,
chain_monitor.clone(),
chain.clone(),
fee_estimator.clone(),
Expand All @@ -403,53 +424,6 @@ impl<S: MutinyStorage> NodeBuilder<S> {
let channel_manager: Arc<PhantomChannelManager<S>> =
Arc::new(read_channel_manager.channel_manager);

// Check all existing channels against default configs.
// If we have default config changes, those should apply
// to all existing and new channels.
let default_config = default_user_config().channel_config;
for channel in channel_manager.list_channels() {
// unwrap is safe after LDK.0.0.109
if channel.config.unwrap() != default_config {
match channel_manager.update_channel_config(
&channel.counterparty.node_id,
&[channel.channel_id],
&default_config,
) {
Ok(_) => {
log_debug!(
logger,
"changed default config for channel: {}",
channel.channel_id.to_hex()
)
}
Err(e) => {
log_error!(
logger,
"error changing default config for channel: {} - {e:?}",
channel.channel_id.to_hex()
)
}
};
}
}

log_info!(logger, "creating lsp client");
let lsp_config: Option<LspConfig> = match node_index.lsp {
None => {
log_info!(logger, "no lsp saved, using configured one if present");
self.lsp_config
}
Some(ref lsp) => {
if self.lsp_config.as_ref() == Some(lsp) {
log_info!(logger, "lsp config matches saved lsp config");
self.lsp_config
} else {
log_info!(logger, "lsp config does not match saved lsp config");
None
}
}
};

let stop = Arc::new(AtomicBool::new(false));

let (lsp_client, liquidity) = match lsp_config {
Expand Down Expand Up @@ -621,6 +595,36 @@ impl<S: MutinyStorage> NodeBuilder<S> {
}
}

// Check all existing channels against default configs.
// If we have default config changes, those should apply
// to all existing and new channels.
let default_config = default_user_config(accept_underpaying_htlcs).channel_config;
for channel in channel_manager.list_channels() {
// unwrap is safe after LDK.0.0.109
if channel.config.unwrap() != default_config {
match channel_manager.update_channel_config(
&channel.counterparty.node_id,
&[channel.channel_id],
&default_config,
) {
Ok(_) => {
log_debug!(
logger,
"changed default config for channel: {}",
channel.channel_id.to_hex()
)
}
Err(e) => {
log_error!(
logger,
"error changing default config for channel: {} - {e:?}",
channel.channel_id.to_hex()
)
}
};
}
}

let background_persister = persister.clone();
let background_event_handler = event_handler.clone();
let background_processor_logger = logger.clone();
Expand Down Expand Up @@ -1693,7 +1697,11 @@ impl<S: MutinyStorage> Node<S> {
fee_rate: Option<f32>,
user_channel_id: Option<u128>,
) -> Result<u128, MutinyError> {
let mut config = default_user_config();
let accept_underpaying_htlcs = self
.lsp_client
.as_ref()
.is_some_and(|l| l.accept_underpaying_htlcs());
let mut config = default_user_config(accept_underpaying_htlcs);

// if we are opening channel to LSP, turn off SCID alias until CLN is updated
// LSP protects all invoice information anyways, so no UTXO leakage
Expand Down Expand Up @@ -1797,7 +1805,11 @@ impl<S: MutinyStorage> Node<S> {
// channel size is the total value of the utxos minus the fee
let channel_value_satoshis = utxo_value - expected_fee;

let mut config = default_user_config();
let accept_underpaying_htlcs = self
.lsp_client
.as_ref()
.is_some_and(|l| l.accept_underpaying_htlcs());
let mut config = default_user_config(accept_underpaying_htlcs);
// if we are opening channel to LSP, turn off SCID alias until CLN is updated
// LSP protects all invoice information anyways, so no UTXO leakage
if let Some(lsp) = self.lsp_client.clone() {
Expand Down Expand Up @@ -2158,7 +2170,7 @@ pub(crate) fn split_peer_connection_string(
Ok((pubkey, peer_addr_str.to_string()))
}

pub(crate) fn default_user_config() -> UserConfig {
pub(crate) fn default_user_config(accept_underpaying_htlcs: bool) -> UserConfig {
UserConfig {
channel_handshake_limits: ChannelHandshakeLimits {
// lnd's max to_self_delay is 2016, so we want to be compatible.
Expand All @@ -2183,7 +2195,7 @@ pub(crate) fn default_user_config() -> UserConfig {
max_dust_htlc_exposure: MaxDustHTLCExposure::FixedLimitMsat(
21_000_000 * 100_000_000 * 1_000,
),
accept_underpaying_htlcs: true,
accept_underpaying_htlcs,
..Default::default()
},
..Default::default()
Expand Down

0 comments on commit 76c96f6

Please sign in to comment.