Skip to content

Commit

Permalink
commit dirty half-way state
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Jan 17, 2024
2 parents e5122bf + 61b8616 commit f314091
Show file tree
Hide file tree
Showing 11 changed files with 1,149 additions and 143 deletions.
85 changes: 84 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,90 @@ Up-to-date with main branch as of v0.0.118 (Oct 24, commit d2242f6; originally b

See also `ldk-sample` https://github.com/catenocrypt/ldk-sample/tree/splicing-hapa2

To test: `cargo test splic`
To test: `RUSTFLAGS="--cfg=dual_funding" cargo test splic`

Detailed steps
--------------
(as of Jan 10, e5122bf, splicing-hapa5-iatx)

[S1I] splice_channel() - ChannelManager API
[S2I] get_splice() - Channel
message out: splice
handle_splice() - ChannelManager
[S3A] internal_splice() - ChannelManager
[S4A] get_splice_ack - Channel
[A] begin_interactive_funding_tx_construction() - Channel
message in: splice_ack
handle_splice_ack() - ChannelManager
[S5I] internal_splice_ack - ChannelManager
event: SpliceAckedInputsContributionReady
action by client, create input for new funding
contribute_funding_inputs - ChannelManager API
[I] begin_interactive_funding_tx_construction() - Channel
message out: tx_add_input
[A] handle_tx_add_input - ChannelManager
message in: tx_complete
[I] handle_tx_complete
message out: tx_add_input, second
[A] handle_tx_add_input - ChannelManager
message in: tx_complete
[I] handle_tx_complete
message out: tx_add_output - for change
[A] handle_tx_add_output
message in: tx_complete
[I] handle_tx_complete
message out: tx_add_output - for new funding
[A] handle_tx_add_output
message in: tx_complete
[I] handle_tx_complete
event: FundingTransactionReadyForSigning
message_out: tx_complete
message out: udpate_htlcs
[A] handle_tx_complete
message in: update_htlcs
[A] handle_commitment_signed() - ChannelManager
[A] internal_commitment_signed() - ChannelManager
[A] commitment_signed_initial_v2 - Channel
[I] handle_commitment_signed() - ChannelManager
[I] internal_commitment_signed() - ChannelManager


Previous as of Jan 8, dfe53d7, splicing-hapa4

[S1I] splice_channel() - ChannelManager API
[S2I] get_splice() - Channel
message out: splice
[S3A] internal_splice() - ChannelManager
[S4A] get_splice_ack - Channel
message in: splice_ack
handle_splice_ack()
[S5I] internal_splice_ack - ChannelManager
event: SpliceAcked
action by client
splice_transaction_generated - ChannelManager API
[S6I] splice_transaction_generated_intern() - ChannelManager
[S7I] splice_generated() - Channel
message out: splice_created
[S8A] internal_splice_created() - ChannelManager
[S9A] splice_created() - Channel
message in: tx_complete
[S10I] internal_tx_complete() - ChannelManager
[S11I] splice_tx_complete() - Channel
message out: splice_comm_signed
[S12A] internal_splice_comm_signed() - ChannelManager
[S13A] splice_comm_signed() - Channel
message in: splice_comm_ack
[S14I] internal_splice_comm_ack() - ChannelManager
[S15I] splice_comm_ack() - Channel
message out: splice_signed
[S16A] internal_splice_signed() - ChannelManager
[S17A] splice_signed() - Channel
message in: splice_signed_ack
[S18I] internal_splice_signed_ack() - ChannelManager
[S19I] splice_signed_ack() - Channel
waiting for confirmation
transactions_confirmed() - Channel
commit_pending_splice() - Channel


Status
Expand Down
64 changes: 51 additions & 13 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,22 +1122,48 @@ pub enum Event {
/// [`ChannelManager::funding_transaction_signed`]: crate::ln::channelmanager::ChannelManager::funding_transaction_signed
unsigned_transaction: Transaction,
},
// /// #SPLICING
// /// Indicates that the splice negotiation is done, `splice_ack` msg was received
// /// TODO Change name, this should come after tx negotiation, maybe not needed in this form
// SpliceAcked {
// /// The channel_id of the channel where the splice was initiated
// channel_id: ChannelId,
// /// The counterparty's node_id
// counterparty_node_id: PublicKey,
// /// The current funding TX outpoint, which must be an input to the new splice TX
// current_funding_outpoint: OutPoint,
// /// The pre-splice channel value, in satoshis.
// pre_channel_value_satoshis: u64,
// /// The post-splice channel value, in satoshis.
// post_channel_value_satoshis: u64,
// /// The script which should be used in the transaction output (channel funding output).
// output_script: ScriptBuf,
// }
/// #SPLICING
/// Indicates that the splice negotiation is done, `splice_ack` msg was received
/// Indicates that the splice negotiation is done, `splice_ack` msg was received, and interactive transaction negotiation can start.
/// Similar to FundingInputsContributionReady
/// TODO Change name, this should come after tx negotiation, maybe not needed in this form
SpliceAcked {
/// The channel_id of the channel where the splice was initiated
SpliceAckedInputsContributionReady {
/// The channel_id of the channel that requires funding inputs which you'll need to pass into
/// [`ChannelManager::contribute_funding_inputs`].
///
/// [`ChannelManager::contribute_funding_inputs`]: crate::ln::channelmanager::ChannelManager::contribute_funding_inputs
channel_id: ChannelId,
/// The counterparty's node_id
/// The counterparty's node_id, which you'll need to pass back into
/// [`ChannelManager::contribute_funding_inputs`].
///
/// [`ChannelManager::contribute_funding_inputs`]: crate::ln::channelmanager::ChannelManager::contribute_funding_inputs
counterparty_node_id: PublicKey,
/// The current funding TX outpoint, which must be an input to the new splice TX
current_funding_outpoint: OutPoint,
/// The pre-splice channel value, in satoshis.
pre_channel_value_satoshis: u64,
/// The post-splice channel value, in satoshis.
post_channel_value_satoshis: u64,
/// The script which should be used in the transaction output (channel funding output).
output_script: ScriptBuf,
/// The value, in satoshis, that we commited to contribute to the channel value during
/// establishment.
holder_funding_satoshis: u64,
/// The value, in satoshis, that the counterparty commited to contribute to the channel value
/// during channel establishment.
counterparty_funding_satoshis: u64,
}
}

Expand Down Expand Up @@ -1372,15 +1398,27 @@ impl Writeable for Event {
})
},
// #SPLICING
&Event::SpliceAcked { ref channel_id, ref counterparty_node_id, ref current_funding_outpoint, ref pre_channel_value_satoshis, ref post_channel_value_satoshis, ref output_script } => {
// &Event::SpliceAcked { ref channel_id, ref counterparty_node_id, ref current_funding_outpoint, ref pre_channel_value_satoshis, ref post_channel_value_satoshis, ref output_script } => {
// 33u8.write(writer)?; // TODO value
// write_tlv_fields!(writer, {
// (0, channel_id, required),
// (2, counterparty_node_id, required),
// (4, current_funding_outpoint, required),
// (6, pre_channel_value_satoshis, required),
// (8, post_channel_value_satoshis, required),
// (10, output_script, required),
// });
// },
// #SPLICING
&Event::SpliceAckedInputsContributionReady { ref channel_id, ref counterparty_node_id, ref pre_channel_value_satoshis, ref post_channel_value_satoshis, ref holder_funding_satoshis, ref counterparty_funding_satoshis } => {
33u8.write(writer)?; // TODO value
write_tlv_fields!(writer, {
(0, channel_id, required),
(2, counterparty_node_id, required),
(4, current_funding_outpoint, required),
(6, pre_channel_value_satoshis, required),
(8, post_channel_value_satoshis, required),
(10, output_script, required),
(4, pre_channel_value_satoshis, required),
(6, post_channel_value_satoshis, required),
(8, holder_funding_satoshis, required),
(10, counterparty_funding_satoshis, required),
});
},
&Event::ConnectionNeeded { .. } => {
Expand Down
27 changes: 22 additions & 5 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,18 @@ impl TxCreationKeys {
/// Create per-state keys from channel base points and the per-commitment point.
/// Key set is asymmetric and can't be used as part of counter-signatory set of transactions.
pub fn derive_new<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, broadcaster_delayed_payment_base: &DelayedPaymentBasepoint, broadcaster_htlc_base: &HtlcBasepoint, countersignatory_revocation_base: &RevocationBasepoint, countersignatory_htlc_base: &HtlcBasepoint) -> TxCreationKeys {
TxCreationKeys {
let revocation_key = RevocationKey::from_basepoint(&secp_ctx, &countersignatory_revocation_base, &per_commitment_point);
println!(" derive_new {:?} {:?} {:?}", countersignatory_revocation_base, per_commitment_point, revocation_key);
let res = TxCreationKeys {
per_commitment_point: per_commitment_point.clone(),
revocation_key: RevocationKey::from_basepoint(&secp_ctx, &countersignatory_revocation_base, &per_commitment_point),
revocation_key,
// revocation_key: RevocationKey::from_basepoint(&secp_ctx, &countersignatory_revocation_base, &per_commitment_point),
broadcaster_htlc_key: HtlcKey::from_basepoint(&secp_ctx, &broadcaster_htlc_base, &per_commitment_point),
countersignatory_htlc_key: HtlcKey::from_basepoint(&secp_ctx, &countersignatory_htlc_base, &per_commitment_point),
broadcaster_delayed_payment_key: DelayedPaymentKey::from_basepoint(&secp_ctx, &broadcaster_delayed_payment_base, &per_commitment_point),
}
};
// println!("TxCreationKeys::derive_new {:?}", res.revocation_key); // TODO remove
res
}

/// Generate per-state keys from channel static keys.
Expand Down Expand Up @@ -845,7 +850,10 @@ pub struct CounterpartyChannelTransactionParameters {
impl ChannelTransactionParameters {
/// Whether the late bound parameters are populated.
pub fn is_populated(&self) -> bool {
self.counterparty_parameters.is_some() && self.funding_outpoint.is_some()
// self.counterparty_parameters.is_some() && self.funding_outpoint.is_some()
let res = self.counterparty_parameters.is_some() && self.funding_outpoint.is_some();
if !res { println!("ChannelTransactionParameters::is_populated(): {} {} ", self.counterparty_parameters.is_some(), self.funding_outpoint.is_some()); }
res
}

/// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters,
Expand Down Expand Up @@ -1123,7 +1131,10 @@ impl BuiltCommitmentTransaction {
/// Signs the counterparty's commitment transaction.
pub fn sign_counterparty_commitment<T: secp256k1::Signing>(&self, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
let sighash = self.get_sighash_all(funding_redeemscript, channel_value_satoshis);
sign(secp_ctx, &sighash, funding_key)
let sig = sign(secp_ctx, &sighash, funding_key);
let secret_pubkey_forprint = PublicKey::from_secret_key(&secp_ctx, &funding_key);
println!("SCC_B START BuiltCommitmentTransaction (chan_utils) sigpkey {:?} {:?} sighash {:?} signature {:?}", secret_pubkey_forprint, funding_key, sighash, sig);
sig
}

/// Signs the holder commitment transaction because we are about to broadcast it.
Expand Down Expand Up @@ -1364,10 +1375,14 @@ impl CommitmentTransaction {
///
/// This is not exported to bindings users due to the generic though we likely should expose a version without
pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, broadcaster_funding_key: PublicKey, countersignatory_funding_key: PublicKey, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
println!("new_with_auxiliary_htlc_data START"); // TODO remove
// Sort outputs and populate output indices while keeping track of the auxiliary data
let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters, &broadcaster_funding_key, &countersignatory_funding_key).unwrap();

let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);

println!("new_with_auxiliary_htlc_data {} {} {:?}", outputs.len(), outputs[0].value, outputs[0].script_pubkey); // TODO remove

let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
let txid = transaction.txid();
CommitmentTransaction {
Expand Down Expand Up @@ -1432,6 +1447,7 @@ impl CommitmentTransaction {
let script = if channel_parameters.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
get_to_countersignatory_with_anchors_redeemscript(&countersignatory_pubkeys.payment_point).to_v0_p2wsh()
} else {
println!("internal_build_outputs payment_point {:?}", countersignatory_pubkeys.payment_point); // TODO remove
Payload::p2wpkh(&BitcoinPublicKey::new(countersignatory_pubkeys.payment_point)).unwrap().script_pubkey()
};
txouts.push((
Expand All @@ -1449,6 +1465,7 @@ impl CommitmentTransaction {
contest_delay,
&keys.broadcaster_delayed_payment_key,
);
println!("internal_build_outputs redeem_script {:?} {:?}", redeem_script, keys.revocation_key); // TODO remove
txouts.push((
TxOut {
script_pubkey: redeem_script.to_v0_p2wsh(),
Expand Down
Loading

0 comments on commit f314091

Please sign in to comment.