Skip to content

Commit

Permalink
Check and return commit tx number
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Sep 25, 2023
1 parent 6fd6114 commit 78cbe21
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2410,12 +2410,14 @@ where
}, None));
}

/// Executes the given callback prividing it with a [`ChannelLock`], ensuring that no other
/// Executes the given callback providing it with a [`ChannelLock`], ensuring that no other
/// operation will be executed on the referenced channel at the same time. Errors if the
/// channel peer is disconnected or the channel is not in a useable state. If the callback
/// returns an error, the channel value and funding outpoint are reset to the values they had
/// prior to the callback call.
pub fn with_useable_channel_lock<C, RV>(&self, channel_id: &[u8; 32], counter_party_node_id: &PublicKey, callback: C) -> Result<RV, APIError>
/// prior to the callback call. If `commit_tx_number` is `Some`, it will be checked against the
/// next commitment number for the requested channel, and will return an error if the two
/// values differ.
pub fn with_useable_channel_lock<C, RV>(&self, channel_id: &[u8; 32], counter_party_node_id: &PublicKey, commit_tx_number: Option<u64>, callback: C) -> Result<RV, APIError>
where
C: FnOnce(&mut ChannelLock<<SP::Target as SignerProvider>::Signer>) -> Result<RV, APIError>
{
Expand All @@ -2436,6 +2438,12 @@ where
return Err(APIError::ChannelUnavailable { err: "Channel is not useable.".to_string() });
}

if let Some(commit_tx_number) = commit_tx_number {
if commit_tx_number != chan.get_cur_holder_commitment_transaction_number() - 1 {
return Err(APIError::ExternalError { err: format!("Invalid commitment transaction number, expected {} but got {}", chan.get_cur_holder_commitment_transaction_number(), commit_tx_number) });
}
}

let channel_value = chan.context.get_value_satoshis();
let own_balance = chan.context.get_available_balances(&self.fee_estimator).balance_msat;
let funding_outpoint = chan.context.channel_transaction_parameters.funding_outpoint.unwrap();
Expand Down Expand Up @@ -2484,7 +2492,7 @@ where
}
}

fn get_updated_funding_outpoint_commitment_signed_internal(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, funding_outpoint: &OutPoint, channel_value_satoshis: u64, own_balance: u64) -> Result<CommitmentSigned, APIError> {
fn get_updated_funding_outpoint_commitment_signed_internal(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, funding_outpoint: &OutPoint, channel_value_satoshis: u64, own_balance: u64) -> Result<(CommitmentSigned, u64), APIError> {
if own_balance > channel_value_satoshis * 1000 {
return Err(APIError::APIMisuseError { err: "value_to_self must be smaller than channel_value".to_string() });
}
Expand All @@ -2506,8 +2514,9 @@ where

let res = chan.monitor_updating_restored(&self.logger, &self.node_signer, self.genesis_hash, &self.default_configuration, self.best_block.read().unwrap().height());

let commit_tx_number = chan.get_cur_counterparty_commitment_transaction_number();

return Ok(res.commitment_update.unwrap().commitment_signed)
return Ok((res.commitment_update.unwrap().commitment_signed, commit_tx_number))
}

fn on_commitment_signed_get_raa_internal(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, commitment_signature: &secp256k1::ecdsa::Signature, htlc_signatures: &[secp256k1::ecdsa::Signature]) -> Result<msgs::RevokeAndACK, APIError> {
Expand Down Expand Up @@ -2695,7 +2704,7 @@ where
}

///
pub fn get_updated_funding_outpoint_commitment_signed(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, funding_outpoint: &OutPoint, channel_value_satoshis: u64, value_to_self_msat: u64) -> Result<CommitmentSigned, APIError> {
pub fn get_updated_funding_outpoint_commitment_signed(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, funding_outpoint: &OutPoint, channel_value_satoshis: u64, value_to_self_msat: u64) -> Result<(CommitmentSigned, u64), APIError> {
self.get_updated_funding_outpoint_commitment_signed_internal(channel_lock, funding_outpoint, channel_value_satoshis, value_to_self_msat)
}

Expand Down

0 comments on commit 78cbe21

Please sign in to comment.