Skip to content

Commit

Permalink
Splicing proto hapa7; reworked state handling and all
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Feb 2, 2024
1 parent c0c0832 commit adeee2a
Show file tree
Hide file tree
Showing 9 changed files with 694 additions and 735 deletions.
12 changes: 4 additions & 8 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,9 +1298,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
/// possibly future revocation/preimage information) to claim outputs where possible.
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
/// #SPLICING
/// TODO changed to prod & pub(crate), from test & private
// #[cfg(test)]
pub(crate) fn provide_latest_counterparty_commitment_tx<L: Deref>(
#[cfg(test)]
fn provide_latest_counterparty_commitment_tx<L: Deref>(
&self,
txid: Txid,
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
Expand Down Expand Up @@ -2440,8 +2439,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {

// TODO check reenable
// #[cfg(debug_assertions)] {
// let rebuilt_commitment_tx = self.initial_counterparty_commitment_tx().unwrap();
// debug_assert_eq!(rebuilt_commitment_tx.trust().txid(), txid);
// let rebuilt_commitment_tx = self.initial_counterparty_commitment_tx().unwrap();
// debug_assert_eq!(rebuilt_commitment_tx.trust().txid(), txid);
// }

self.provide_latest_counterparty_commitment_tx(txid, htlc_outputs, commitment_number,
Expand Down Expand Up @@ -3847,8 +3846,6 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
if *idx == input.previous_output.vout {
#[cfg(test)]
{
// TODO put it back, add witness to splice tx
/*
// If the expected script is a known type, check that the witness
// appears to be spending the correct type (ie that the match would
// actually succeed in BIP 158/159-style filters).
Expand All @@ -3864,7 +3861,6 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
} else if _script_pubkey.is_v0_p2wpkh() {
assert_eq!(&bitcoin::Address::p2wpkh(&bitcoin::PublicKey::from_slice(&input.witness.last().unwrap()).unwrap(), bitcoin::Network::Bitcoin).unwrap().script_pubkey(), _script_pubkey);
} else { panic!(); }
*/
}
return true;
}
Expand Down
894 changes: 374 additions & 520 deletions lightning/src/ln/channel.rs

Large diffs are not rendered by default.

371 changes: 214 additions & 157 deletions lightning/src/ln/channelmanager.rs

Large diffs are not rendered by default.

126 changes: 91 additions & 35 deletions lightning/src/ln/functional_tests.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lightning/src/ln/interactivetxs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ mod tests {
entropy_source, channel_id, FEERATE_FLOOR_SATS_PER_KW * 10, holder_node_id, counterparty_node_id, true, tx_locktime, session.inputs_a.clone(), session.outputs_a.clone()
);
let (mut constructor_b, first_message_b) = InteractiveTxConstructor::new(
entropy_source, channel_id, FEERATE_FLOOR_SATS_PER_KW * 10, holder_node_id, counterparty_node_id, false, tx_locktime, session.inputs_b.clone(), session.outputs_b.clone()
entropy_source, channel_id, FEERATE_FLOOR_SATS_PER_KW * 10, holder_node_id, counterparty_node_id, false, tx_locktime, session.inputs_b.clone(), session.outputs_b.clone()
);

let handle_message_send = |msg: InteractiveTxMessageSend, for_constructor: &mut InteractiveTxConstructor| {
Expand Down
1 change: 1 addition & 0 deletions lightning/src/ln/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod chan_utils;
pub mod features;
pub mod script;
mod channel_id;
mod channel_splice;

#[cfg(fuzzing)]
pub mod peer_channel_encryptor;
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl ChannelMessageHandler for ErroringMessageHandler {
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
}
// msgs::ChannelUpdate does not contain the channel_id field, so we just drop them.
// msgs::ChannelUpdate does not contain the channel_id field, so we just drop them.
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelUpdate) {}
fn peer_disconnected(&self, _their_node_id: &PublicKey) {}
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init, _inbound: bool) -> Result<(), ()> { Ok(()) }
Expand Down
17 changes: 6 additions & 11 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,8 @@ pub trait ChannelSigner {
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters);

/// #SPLICING
/// Similar to provide_channel_parameters(), but can be called a second time (or even more). Also update channel value (capacity)
fn reprovide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters, channel_value_satoshis: u64);
/// Update the channel value, and also reset the channel parameters
fn update_channel_value(&mut self, channel_value_satoshis: u64);
}

/// Specifies the recipient of an invoice.
Expand Down Expand Up @@ -1113,13 +1113,11 @@ impl ChannelSigner for InMemorySigner {
}

/// #SPLICING
fn reprovide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters, channel_value_satoshis: u64) {
assert!(self.channel_parameters.is_some());
assert!(channel_parameters.is_populated(), "Channel parameters must be fully populated");
self.channel_parameters = Some(channel_parameters.clone());

/// Update the channel value, and also reset the channel parameters
fn update_channel_value(&mut self, channel_value_satoshis: u64) {
self.channel_value_satoshis = channel_value_satoshis;
// println!("reprovide_channel_parameters channel_value {}", self.channel_value_satoshis);
assert!(self.channel_parameters.is_some());
self.channel_parameters = None;
}
}

Expand All @@ -1135,7 +1133,6 @@ impl EcdsaChannelSigner for InMemorySigner {
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &counterparty_keys.funding_pubkey);

let built_tx = trusted_tx.built_transaction();
// println!("sign_counterparty_commitment tx {:?} {:?} channel_value_satoshis {}", built_tx.txid, built_tx.transaction.encode(), self.channel_value_satoshis);
let commitment_sig = built_tx.sign_counterparty_commitment(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
let commitment_txid = built_tx.txid;

Expand Down Expand Up @@ -1271,11 +1268,9 @@ impl EcdsaChannelSigner for InMemorySigner {
/// #SPLICING
/// #SPLICE-SIG
fn sign_splicing_funding_input(&self, splicing_tx: &Transaction, splice_prev_funding_input_index: u16, splice_prev_funding_input_value: u64, redeem_script: &Script, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
// println!("sign_splicing_funding_input txlen {} idx {} val {} tx {}", splicing_tx.encode().len(), splice_prev_funding_input_index, prev_funding_value, splicing_tx.encode().to_hex());
let sighash = &sighash::SighashCache::new(splicing_tx).segwit_signature_hash(splice_prev_funding_input_index as usize, &redeem_script, splice_prev_funding_input_value, EcdsaSighashType::All).unwrap()[..];
let msg = hash_to_message!(sighash);
let sig = sign(secp_ctx, &msg, &self.funding_key);
// println!("sign_splicing_funding_input hash {} msg{} pubkey {} sig {} val {}", sighash.to_hex(), msg.to_hex(), &self.funding_key.public_key(secp_ctx), sig.serialize_der().to_hex(), splice_prev_funding_input_value);
Ok(sig)
}

Expand Down
4 changes: 2 additions & 2 deletions lightning/src/util/test_channel_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ impl ChannelSigner for TestChannelSigner {
}

/// #SPLICING
fn reprovide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters, channel_value_satoshis: u64) {
self.inner.reprovide_channel_parameters(channel_parameters, channel_value_satoshis)
fn update_channel_value(&mut self, channel_value_satoshis: u64) {
self.inner.update_channel_value(channel_value_satoshis)
}
}

Expand Down

0 comments on commit adeee2a

Please sign in to comment.