Skip to content

Commit 9a9cecd

Browse files
Tibo-lgluckysori
authored andcommitted
Check and return commit tx number
1 parent 6fd6114 commit 9a9cecd

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lightning/src/ln/channelmanager.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ pub use crate::ln::outbound_payment::{PaymentSendFailure, Retry, RetryableSendFa
8282
use crate::ln::script::ShutdownScript;
8383
use super::msgs::{CommitmentSigned, RevokeAndACK};
8484

85+
pub type NumberedCommitmentSigned = (CommitmentSigned, u64);
86+
8587
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
8688
//
8789
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
@@ -2410,12 +2412,14 @@ where
24102412
}, None));
24112413
}
24122414

2413-
/// Executes the given callback prividing it with a [`ChannelLock`], ensuring that no other
2415+
/// Executes the given callback providing it with a [`ChannelLock`], ensuring that no other
24142416
/// operation will be executed on the referenced channel at the same time. Errors if the
24152417
/// channel peer is disconnected or the channel is not in a useable state. If the callback
24162418
/// returns an error, the channel value and funding outpoint are reset to the values they had
2417-
/// prior to the callback call.
2418-
pub fn with_useable_channel_lock<C, RV>(&self, channel_id: &[u8; 32], counter_party_node_id: &PublicKey, callback: C) -> Result<RV, APIError>
2419+
/// prior to the callback call. If `commit_tx_number` is `Some`, it will be checked against the
2420+
/// next commitment number for the requested channel, and will return an error if the two
2421+
/// values differ.
2422+
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>
24192423
where
24202424
C: FnOnce(&mut ChannelLock<<SP::Target as SignerProvider>::Signer>) -> Result<RV, APIError>
24212425
{
@@ -2436,6 +2440,12 @@ where
24362440
return Err(APIError::ChannelUnavailable { err: "Channel is not useable.".to_string() });
24372441
}
24382442

2443+
if let Some(commit_tx_number) = commit_tx_number {
2444+
if commit_tx_number != chan.get_cur_holder_commitment_transaction_number() - 1 {
2445+
return Err(APIError::ExternalError { err: format!("Invalid commitment transaction number, expected {} but got {}", chan.get_cur_holder_commitment_transaction_number(), commit_tx_number) });
2446+
}
2447+
}
2448+
24392449
let channel_value = chan.context.get_value_satoshis();
24402450
let own_balance = chan.context.get_available_balances(&self.fee_estimator).balance_msat;
24412451
let funding_outpoint = chan.context.channel_transaction_parameters.funding_outpoint.unwrap();
@@ -2484,7 +2494,7 @@ where
24842494
}
24852495
}
24862496

2487-
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> {
2497+
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> {
24882498
if own_balance > channel_value_satoshis * 1000 {
24892499
return Err(APIError::APIMisuseError { err: "value_to_self must be smaller than channel_value".to_string() });
24902500
}
@@ -2506,8 +2516,9 @@ where
25062516

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

2519+
let commit_tx_number = chan.get_cur_counterparty_commitment_transaction_number();
25092520

2510-
return Ok(res.commitment_update.unwrap().commitment_signed)
2521+
return Ok((res.commitment_update.unwrap().commitment_signed, commit_tx_number))
25112522
}
25122523

25132524
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> {
@@ -2694,22 +2705,24 @@ where
26942705
self.close_channel_internal(channel_id, counterparty_node_id, target_feerate_sats_per_1000_weight, shutdown_script)
26952706
}
26962707

2697-
///
2698-
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> {
2708+
/// Updates the funding output and returns the `CommitmentSigned` message for the updated
2709+
/// commitment transaction, as well as the commitment transaction number.
2710+
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<NumberedCommitmentSigned, APIError> {
26992711
self.get_updated_funding_outpoint_commitment_signed_internal(channel_lock, funding_outpoint, channel_value_satoshis, value_to_self_msat)
27002712
}
27012713

2702-
///
2714+
/// Process and validates the given commitment signature and returns the RAA to be given to the
2715+
/// counterparty on success.
27032716
pub fn on_commitment_signed_get_raa(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, commitment_signature: &secp256k1::ecdsa::Signature, htlc_signatures: &[secp256k1::ecdsa::Signature]) -> Result<RevokeAndACK, APIError> {
27042717
self.on_commitment_signed_get_raa_internal(channel_lock, commitment_signature, htlc_signatures)
27052718
}
27062719

2707-
///
2720+
/// Process the given RAA message.
27082721
pub fn revoke_and_ack_commitment(&self, channel_lock: &mut ChannelLock<<SP::Target as SignerProvider>::Signer>, revoke_and_ack: &RevokeAndACK) -> Result<(), APIError> {
27092722
self.revoke_and_ack_commitment_internal(channel_lock, revoke_and_ack)
27102723
}
27112724

2712-
///
2725+
/// Set the funding outpoint for the channel to the given values.
27132726
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) {
27142727
self.set_funding_outpoint_internal(channel_lock, funding_output, channel_value_satoshis, value_to_self_msat);
27152728
}

0 commit comments

Comments
 (0)