Skip to content

Commit

Permalink
Add channel keys id to channel details and remove sign with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Aug 15, 2023
1 parent 26db954 commit e4d3ac5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
15 changes: 9 additions & 6 deletions lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,15 @@ where C::Target: chain::Filter,
handler(event).await;
}
}

/// Retrieves the latest holder commitment transaction (and possibly HTLC transactions) for
/// the channel identified with the given `funding_txo`. Errors if no monitor is registered
/// for the given `funding_txo`.
pub fn get_latest_holder_commitment_txn(&self, funding_txo: &OutPoint) -> Result<Vec<bitcoin::Transaction>, ()> {
let monitors = self.monitors.read().unwrap();
let monitor = monitors.get(funding_txo).ok_or(())?;
Ok(monitor.monitor.get_latest_holder_commitment_txn_internal(&self.logger))
}
}

impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
Expand Down Expand Up @@ -669,12 +678,6 @@ where C::Target: chain::Filter,
}
}

fn get_latest_holder_commitment_txn(&self, funding_txo: OutPoint) -> Result<Vec<bitcoin::Transaction>, ()> {
let monitors = self.monitors.read().unwrap();
let monitor = monitors.get(&funding_txo).ok_or(())?;
Ok(monitor.monitor.get_latest_holder_commitment_txn_internal(&self.logger))
}

/// Note that we persist the given `ChannelMonitor` update while holding the
/// `ChainMonitor` monitors lock.
fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
Expand Down
3 changes: 0 additions & 3 deletions lightning/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,6 @@ pub trait Watch<ChannelSigner: WriteableEcdsaChannelSigner> {
/// Update the outpoint funding the channel.
fn update_channel_funding_txo(&self, old_funding_txo: OutPoint, new_funding_txo: OutPoint, channel_value_satoshis: u64) -> ChannelMonitorUpdateStatus;

/// Get the latest commitment transaction to broadcast
fn get_latest_holder_commitment_txn(&self, funding_txo: OutPoint) -> Result<Vec<bitcoin::Transaction>, ()>;

/// Returns any monitor events since the last call. Subsequent calls must only return new
/// events.
///
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ pub(super) struct Channel<Signer: ChannelSigner> {

/// The unique identifier used to re-derive the private key material for the channel through
/// [`SignerProvider::derive_channel_signer`].
channel_keys_id: [u8; 32],
pub(crate) channel_keys_id: [u8; 32],

/// When we generate [`ChannelMonitorUpdate`]s to persist, they may not be persisted immediately.
/// If we then persist the [`channelmanager::ChannelManager`] and crash before the persistence
Expand Down
30 changes: 7 additions & 23 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VA
use crate::ln::outbound_payment;
use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment};
use crate::ln::wire::Encode;
use crate::chain::keysinterface::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner, ExtraSign};
use crate::chain::keysinterface::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner};
use crate::util::config::{UserConfig, ChannelConfig};
use crate::util::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
use crate::util::events;
Expand Down Expand Up @@ -1235,6 +1235,8 @@ pub struct ChannelDetails {
pub counter_funding_pubkey: Option<PublicKey>,
///
pub original_funding_outpoint: Option<OutPoint>,
///
pub channel_keys_id: [u8; 32],
}

impl ChannelDetails {
Expand Down Expand Up @@ -1799,6 +1801,7 @@ where
holder_funding_pubkey: channel.channel_transaction_parameters.holder_pubkeys.funding_pubkey,
counter_funding_pubkey: channel.channel_transaction_parameters.counterparty_parameters.as_ref().map(|params| params.pubkeys.funding_pubkey),
original_funding_outpoint: channel.channel_transaction_parameters.original_funding_outpoint,
channel_keys_id: channel.channel_keys_id,
});
}
}
Expand Down Expand Up @@ -2007,10 +2010,6 @@ where
Ok(())
}

fn sign_with_fund_key_callback_internal<SF>(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, cb: &mut SF) where SF: FnMut(&SecretKey) {
channel_lock.channel.holder_signer.sign_with_fund_key_callback(cb);
}

fn set_funding_outpoint_internal(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, funding_outpoint: &OutPoint, channel_value: u64, own_balance: u64) {
let chan = &mut channel_lock.channel;

Expand All @@ -2020,14 +2019,6 @@ where
}
}

fn get_latest_holder_commitment_txn_internal(&self, channel_lock: &ChannelLock<<SP::Target as SignerProvider>::Signer>) -> Result<Vec<Transaction>, APIError> {
let chan = &channel_lock.channel;

self.chain_monitor.get_latest_holder_commitment_txn(
chan.get_original_funding_txo().ok_or_else(|| APIError::APIMisuseError { err: "Channel does not have a funding txo.".to_string() })?
).map_err(|_| APIError::APIMisuseError { err: "No channel monitor for channel".to_string() })
}

fn close_channel_internal(&self, channel_id: &[u8; 32], counterparty_node_id: &PublicKey, target_feerate_sats_per_1000_weight: Option<u32>) -> Result<(), APIError> {
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);

Expand Down Expand Up @@ -2147,21 +2138,11 @@ where
self.revoke_and_ack_commitment_internal(channel_lock, revoke_and_ack)
}

///
pub fn sign_with_fund_key_callback<SF>(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, cb: &mut SF) where SF: FnMut(&SecretKey) {
self.sign_with_fund_key_callback_internal(channel_lock, cb);
}

///
pub fn set_funding_outpoint(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, funding_output: &OutPoint, channel_value_satoshis: u64, value_to_self_msat: u64) {
self.set_funding_outpoint_internal(channel_lock, funding_output, channel_value_satoshis, value_to_self_msat);
}

///
pub fn get_latest_holder_commitment_txn(&self, channel_lock: &ChannelLock<<SP::Target as SignerProvider>::Signer>) -> Result<Vec<Transaction>, APIError> {
self.get_latest_holder_commitment_txn_internal(channel_lock)
}

#[inline]
fn finish_force_close_channel(&self, shutdown_res: ShutdownResult) {
let (monitor_update_option, mut failed_htlcs) = shutdown_res;
Expand Down Expand Up @@ -6794,6 +6775,7 @@ impl Writeable for ChannelDetails {
(40, self.holder_funding_pubkey, required),
(41, self.counter_funding_pubkey, required),
(42, self.original_funding_outpoint, option),
(43, self.channel_keys_id, required),
});
Ok(())
}
Expand Down Expand Up @@ -6834,6 +6816,7 @@ impl Readable for ChannelDetails {
(40, holder_funding_pubkey, required),
(41, counter_funding_pubkey, required),
(42, original_funding_outpoint, option),
(43, channel_keys_id, required),
});

// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
Expand Down Expand Up @@ -6872,6 +6855,7 @@ impl Readable for ChannelDetails {
holder_funding_pubkey: holder_funding_pubkey.0.unwrap(),
counter_funding_pubkey: counter_funding_pubkey.0.unwrap(),
original_funding_outpoint,
channel_keys_id: channel_keys_id.0.unwrap(),
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,12 @@ mod tests {
inbound_htlc_minimum_msat: None,
inbound_htlc_maximum_msat: None,
config: None,
fee_rate_per_kw: 0,
funding_redeemscript: None,
holder_funding_pubkey: node_id,
counter_funding_pubkey: None,
original_funding_outpoint: None,
channel_keys_id: [0; 32],
}
}

Expand Down
6 changes: 1 addition & 5 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,9 @@ impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
return self.chain_monitor.release_pending_monitor_events();
}

fn update_channel_funding_txo(&self, _: OutPoint, new_funding_txo: OutPoint, _: u64) -> chain::ChannelMonitorUpdateStatus {
fn update_channel_funding_txo(&self, _: OutPoint, _: OutPoint, _: u64) -> chain::ChannelMonitorUpdateStatus {
todo!()
}

fn get_latest_holder_commitment_txn(&self, _funding_txo: OutPoint) -> Result<Vec<bitcoin::Transaction>, ()> {
todo!()
}
}

pub struct TestPersister {
Expand Down

0 comments on commit e4d3ac5

Please sign in to comment.