-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Custom Transactions] Define the
TxBuilder
trait
This commit defines the `TxBuilder` trait to give users the ability to customize the build of the lightning commitment transactions. The `TxBuilder` trait has a single method, `build_commitment_transaction`, which builds the commitment transaction, and populates the output indices of the HTLCs passed in. Note that the method itself does not impose any sorting.
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,4 +67,5 @@ check-cfg = [ | |
"cfg(splicing)", | ||
"cfg(async_payments)", | ||
"cfg(dual_funding)", | ||
"cfg(custom_tx)", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//! Defines the `TxBuilder` trait, and the `SpecTxBuilder` type | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
use bitcoin::secp256k1::{self, PublicKey, Secp256k1}; | ||
use bitcoin::{Amount, Transaction}; | ||
|
||
use crate::ln::chan_utils::{ChannelTransactionParameters, HTLCOutputInCommitment, TxCreationKeys}; | ||
use crate::prelude::*; | ||
|
||
/// A trait for types that can build commitment transactions, both for the holder, and the counterparty. | ||
pub trait TxBuilder { | ||
/// Set the counterparty static channel data, including basepoints, | ||
/// `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint. | ||
/// | ||
/// This data is static, and will never change for a channel once set. | ||
/// | ||
/// channel_parameters.is_populated() MUST be true. | ||
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters); | ||
/// Build a commitment transaction, and populate the elements of `htlcs` with their output indices. | ||
/// Do not sort `htlcs`; this will be done by the caller as needed. | ||
/// This method will be called only after all the channel parameters have been provided via `provide_channel_parameters`. | ||
fn build_commitment_transaction( | ||
&self, is_holder_tx: bool, commitment_number: u64, per_commitment_point: &PublicKey, | ||
to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, | ||
trimmed_value_sat: Amount, htlcs: Vec<&mut HTLCOutputInCommitment>, | ||
secp_ctx: &Secp256k1<secp256k1::All>, | ||
) -> Transaction; | ||
} | ||
|
||
/// A type that builds commitment transactions according to the Lightning Specification. | ||
#[derive(Clone, Debug, Default)] | ||
pub struct SpecTxBuilder { | ||
channel_parameters: Option<ChannelTransactionParameters>, | ||
} | ||
|
||
impl TxBuilder for SpecTxBuilder { | ||
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) { | ||
assert!( | ||
self.channel_parameters.is_none() | ||
|| self.channel_parameters.as_ref().unwrap() == channel_parameters | ||
); | ||
if self.channel_parameters.is_some() { | ||
// The channel parameters were already set and they match, return early. | ||
return; | ||
} | ||
assert!(channel_parameters.is_populated(), "Channel parameters must be fully populated"); | ||
self.channel_parameters = Some(channel_parameters.clone()); | ||
} | ||
fn build_commitment_transaction( | ||
&self, is_holder_tx: bool, commitment_number: u64, per_commitment_point: &PublicKey, | ||
to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, | ||
trimmed_value_sat: Amount, htlcs: Vec<&mut HTLCOutputInCommitment>, | ||
secp_ctx: &Secp256k1<secp256k1::All>, | ||
) -> Transaction { | ||
let params = if is_holder_tx { | ||
self.channel_parameters.as_ref().unwrap().as_holder_broadcastable() | ||
} else { | ||
self.channel_parameters.as_ref().unwrap().as_counterparty_broadcastable() | ||
}; | ||
let keys = TxCreationKeys::from_channel_static_keys( | ||
per_commitment_point, | ||
params.broadcaster_pubkeys(), | ||
params.countersignatory_pubkeys(), | ||
secp_ctx, | ||
); | ||
/* | ||
let (obscured_commitment_transaction_number, txins) = | ||
internal_build_inputs(commitment_number, ¶ms); | ||
let txouts = internal_build_outputs( | ||
&keys, | ||
to_broadcaster_value_sat, | ||
to_countersignatory_value_sat, | ||
htlcs, | ||
¶ms, | ||
); | ||
make_transaction(obscured_commitment_transaction_number, txins, txouts) | ||
*/ | ||
todo!(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters