Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure TX fee split of DLC channels #18

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dlc-manager/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn create_transactions(payouts: &[Payout]) -> DlcTransactions {
input_amount: 300000000,
collateral: 100000000,
};
create_dlc_transactions(&offer_params, &accept_params, payouts, 1000, 2, 0, 1000, 3).unwrap()
create_dlc_transactions(&offer_params, &accept_params, payouts, 1000, 2, 0, 1000, 3, dlc::FeeConfig::EvenSplit).unwrap()
}

fn accept_seckey() -> SecretKey {
Expand Down
11 changes: 7 additions & 4 deletions dlc-manager/src/channel/offered_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//! the model for it and method for working with it.

use dlc::PartyParams;
use dlc_messages::channel::OfferChannel;
// use dlc_messages::channel::OfferChannel;
use dlc_messages::{channel::OfferChannel, FeeConfig};
use secp256k1_zkp::PublicKey;

use crate::{contract::offered_contract::OfferedContract, conversion_utils::get_tx_input_infos, error::Error, ContractId, DlcChannelId, ReferenceId};
Expand Down Expand Up @@ -39,7 +38,9 @@ pub struct OfferedChannel {
/// The nSequence value to use for the CETs.
pub cet_nsequence: u32,
/// The reference id set by the api user.
pub reference_id: Option<ReferenceId>
pub reference_id: Option<ReferenceId>,
/// How the two parties pay for transaction fees.
pub fee_config: Option<FeeConfig>,
}

impl OfferedChannel {
Expand Down Expand Up @@ -72,7 +73,8 @@ impl OfferedChannel {
fee_rate_per_vb: offered_contract.fee_rate_per_vb,
fund_output_serial_id: offered_contract.fund_output_serial_id,
cet_nsequence: crate::manager::CET_NSEQUENCE,
reference_id
reference_id,
fee_config: self.fee_config,
}
}

Expand All @@ -98,6 +100,7 @@ impl OfferedChannel {
counter_party,
cet_nsequence: offer_channel.cet_nsequence,
reference_id: offer_channel.reference_id,
fee_config: offer_channel.fee_config,
};

let (inputs, input_amount) = get_tx_input_infos(&offer_channel.funding_inputs)?;
Expand Down
2 changes: 1 addition & 1 deletion dlc-manager/src/channel/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use lightning::ln::msgs::DecodeError;
use lightning::util::ser::{Readable, Writeable, Writer};

impl_dlc_writeable!(PartyBasePoints, { (own_basepoint, writeable), (publish_basepoint, writeable), (revocation_basepoint, writeable) });
impl_dlc_writeable!(OfferedChannel, { (offered_contract_id, writeable), (temporary_channel_id, writeable), (party_points, writeable), (per_update_point, writeable), (offer_per_update_seed, writeable), (is_offer_party, writeable), (counter_party, writeable), (cet_nsequence, writeable), (reference_id, option) });
impl_dlc_writeable!(OfferedChannel, { (offered_contract_id, writeable), (temporary_channel_id, writeable), (party_points, writeable), (per_update_point, writeable), (offer_per_update_seed, writeable), (is_offer_party, writeable), (counter_party, writeable), (cet_nsequence, writeable), (reference_id, option), (fee_config, option) });
impl_dlc_writeable!(AcceptedChannel, {
(accepted_contract_id, writeable),
(offer_base_points, writeable),
Expand Down
22 changes: 18 additions & 4 deletions dlc-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use crate::{chain_monitor::{ChainMonitor, ChannelInfo, TxType}, channel::{
}, contract_updater::{
accept_contract_internal, verify_accepted_and_sign_contract_internal,
verify_signed_contract_internal,
}, error::Error, subchannel::{ClosingSubChannel, SubChannel}, Blockchain, ContractId, DlcChannelId, Signer, Time, Wallet, ReferenceId, manager::CET_NSEQUENCE};
}, error::Error, manager::CET_NSEQUENCE, subchannel::{ClosingSubChannel, SubChannel}, Blockchain, ContractId, DlcChannelId, ReferenceId, Signer, Time, Wallet};
use bitcoin::{OutPoint, Script, Sequence, Transaction, Address};
use dlc::{
channel::{get_tx_adaptor_signature, verify_tx_adaptor_signature, DlcChannelTransactions}, util::dlc_channel_extra_fee, PartyParams
channel::{get_tx_adaptor_signature, verify_tx_adaptor_signature, DlcChannelTransactions}, util::dlc_channel_extra_fee, PartyParams, FeeConfig
};
use dlc_messages::{
channel::{
Expand Down Expand Up @@ -100,6 +100,7 @@ pub fn offer_channel<C: Signing, W: Deref, B: Deref, T: Deref>(
blockchain: &B,
time: &T,
temporary_channel_id: DlcChannelId,
fee_config: FeeConfig,
is_sub_channel: bool,
reference_id: Option<ReferenceId>
) -> Result<(OfferedChannel, OfferedContract), Error>
Expand All @@ -118,6 +119,8 @@ where
blockchain,
!is_sub_channel,
extra_fee,
true,
fee_config,
)?;
let party_points = crate::utils::get_party_base_points(secp, wallet)?;

Expand Down Expand Up @@ -155,7 +158,8 @@ where
is_offer_party: true,
counter_party: *counter_party,
cet_nsequence,
reference_id
reference_id,
fee_config: Some(fee_config.into()),
};

Ok((offered_channel, offered_contract))
Expand All @@ -170,6 +174,7 @@ pub fn accept_channel_offer<W: Deref, B: Deref>(
offered_contract: &OfferedContract,
wallet: &W,
blockchain: &B,
fee_config: FeeConfig,
) -> Result<(AcceptedChannel, AcceptedContract, AcceptChannel), Error>
where
W::Target: Wallet,
Expand All @@ -183,6 +188,7 @@ where
blockchain,
None,
None,
fee_config,
)
}

Expand All @@ -199,6 +205,7 @@ pub(crate) fn accept_channel_offer_internal<W: Deref, B: Deref>(
PartyBasePoints,
PublicKey,
)>,
fee_config: FeeConfig,
) -> Result<(AcceptedChannel, AcceptedContract, AcceptChannel), Error>
where
W::Target: Wallet,
Expand All @@ -221,7 +228,9 @@ where
wallet,
blockchain,
sub_channel_info.is_none(),
extra_fee
extra_fee,
false,
fee_config
)?;
let accept_points = crate::utils::get_party_base_points(secp, wallet)?;
let per_update_seed = wallet.get_new_secret_key()?;
Expand Down Expand Up @@ -301,6 +310,7 @@ where
offered_contract.cet_locktime,
offered_contract.fund_output_serial_id,
Sequence(offered_channel.cet_nsequence),
fee_config,
)?;
let accept_fund_sk = wallet.get_secret_key_for_pubkey(&accept_params.fund_pubkey)?;
let funding_output_value = txs.dlc_transactions.get_fund_output().value;
Expand Down Expand Up @@ -385,6 +395,7 @@ pub fn verify_and_sign_accepted_channel<S: Deref>(
cet_nsequence: u32,
signer: &S,
chain_monitor: &Mutex<ChainMonitor>,
fee_config: FeeConfig,
) -> Result<(SignedChannel, SignedContract, SignChannel), Error>
where
S::Target: Signer,
Expand All @@ -398,6 +409,7 @@ where
signer,
None,
chain_monitor,
fee_config,
)
}

Expand All @@ -410,6 +422,7 @@ pub(crate) fn verify_and_sign_accepted_channel_internal<S: Deref>(
signer: &S,
sub_channel_info: Option<SubChannelSignVerifyInfo>,
chain_monitor: &Mutex<ChainMonitor>,
fee_config: FeeConfig,
) -> Result<(SignedChannel, SignedContract, SignChannel), Error>
where
S::Target: Signer,
Expand Down Expand Up @@ -513,6 +526,7 @@ where
offered_contract.cet_locktime,
offered_contract.fund_output_serial_id,
Sequence(cet_nsequence),
fee_config,
)?;
let offer_fund_sk =
signer.get_secret_key_for_pubkey(&offered_contract.offer_params.fund_pubkey)?;
Expand Down
8 changes: 7 additions & 1 deletion dlc-manager/src/contract_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Deref;

use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::{consensus::Decodable, Script, Transaction, Witness};
use dlc::{DlcTransactions, PartyParams};
use dlc::{DlcTransactions, PartyParams, FeeConfig};
use dlc_messages::{
oracle_msgs::{OracleAnnouncement, OracleAttestation},
AcceptDlc, FundingSignature, FundingSignatures, OfferDlc, SignDlc, WitnessElement,
Expand Down Expand Up @@ -51,6 +51,8 @@ where
blockchain,
true,
0,
true,
FeeConfig::EvenSplit
)?;

let offered_contract = OfferedContract::new(
Expand Down Expand Up @@ -91,6 +93,8 @@ where
blockchain,
true,
0,
false,
FeeConfig::EvenSplit,
)?;

let dlc_transactions = dlc::create_dlc_transactions(
Expand All @@ -102,6 +106,7 @@ where
0,
offered_contract.cet_locktime,
offered_contract.fund_output_serial_id,
FeeConfig::EvenSplit,
)?;

let fund_output_value = dlc_transactions.get_fund_output().value;
Expand Down Expand Up @@ -261,6 +266,7 @@ where
0,
offered_contract.cet_locktime,
offered_contract.fund_output_serial_id,
FeeConfig::EvenSplit,
)?;
let fund_output_value = dlc_transactions.get_fund_output().value;
let fund_privkey =
Expand Down
17 changes: 15 additions & 2 deletions dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use bitcoin::consensus::encode::serialize_hex;
use bitcoin::{Address, OutPoint, Txid};
use bitcoin::Transaction;
use bitcoin::hashes::hex::ToHex;
use dlc::FeeConfig;
use dlc_messages::channel::{
AcceptChannel, CollaborativeCloseOffer, OfferChannel, Reject, RenewAccept, RenewConfirm,
RenewFinalize, RenewOffer, RenewRevoke, SettleAccept, SettleConfirm, SettleFinalize,
Expand All @@ -46,8 +47,8 @@ use std::sync::Mutex;
use bitcoin::consensus::Decodable;
use bitcoin::hashes::Hash;

/// The number of confirmations required before moving the the confirmed state.
pub const NB_CONFIRMATIONS: u32 = 1;
/// The number of confirmations required before moving a [`Contract`] to the confirmed state.
pub const NB_CONFIRMATIONS: u32 = 0;
/// The delay to set the refund value to.
pub const REFUND_DELAY: u32 = 86400 * 7;
/// The nSequence value used for CETs in DLC channels
Expand Down Expand Up @@ -576,6 +577,7 @@ where
let confirmations = self.blockchain.get_transaction_confirmations(
&contract.accepted_contract.dlc_transactions.fund.txid(),
)?;
#[allow(clippy::absurd_extreme_comparisons)]
if confirmations >= NB_CONFIRMATIONS {
self.store
.update_contract(&Contract::Confirmed(contract.clone()))?;
Expand Down Expand Up @@ -706,6 +708,7 @@ where
let confirmations = self
.blockchain
.get_transaction_confirmations(&broadcasted_txid)?;
#[allow(clippy::absurd_extreme_comparisons)]
if confirmations >= NB_CONFIRMATIONS {
let closed_contract = ClosedContract {
attestations: contract.attestations.clone(),
Expand Down Expand Up @@ -743,6 +746,7 @@ where
.blockchain
.get_transaction_confirmations(&signed_cet.txid())?;

#[allow(clippy::absurd_extreme_comparisons)]
if confirmations < 1 {
// TODO(tibo): if this fails because another tx is already in
// mempool or blockchain, we might have been cheated. There is
Expand Down Expand Up @@ -823,6 +827,7 @@ where
&self,
contract_input: &ContractInput,
counter_party: PublicKey,
fee_config: dlc::FeeConfig,
reference_id: Option<ReferenceId>,
) -> Result<OfferChannel, Error> {
let oracle_announcements = contract_input
Expand All @@ -842,6 +847,7 @@ where
&self.blockchain,
&self.time,
crate::utils::get_new_temporary_id(),
fee_config,
false,
reference_id
)?;
Expand Down Expand Up @@ -883,6 +889,7 @@ where
pub fn accept_channel(
&self,
channel_id: &DlcChannelId,
fee_config: dlc::FeeConfig,
) -> Result<(AcceptChannel, DlcChannelId, ContractId, PublicKey), Error> {
let offered_channel =
get_channel_in_state!(self, channel_id, Offered, None as Option<PublicKey>)?;
Expand All @@ -907,6 +914,7 @@ where
&offered_contract,
&self.wallet,
&self.blockchain,
fee_config,
)?;

self.wallet.import_address(&Address::p2wsh(
Expand Down Expand Up @@ -1356,6 +1364,7 @@ where

// TODO(lucas): No need to send it again if it is in mempool, unless we want to bump the
// fee.
#[allow(clippy::absurd_extreme_comparisons)]
if confirmations < 1 {
self.blockchain.send_transaction(claim_tx)?;
} else if confirmations >= NB_CONFIRMATIONS {
Expand Down Expand Up @@ -1458,6 +1467,10 @@ where
CET_NSEQUENCE,
&self.wallet,
&self.chain_monitor,
offered_channel
.fee_config
.map(FeeConfig::from)
.unwrap_or(FeeConfig::EvenSplit),
);

match res {
Expand Down
16 changes: 12 additions & 4 deletions dlc-manager/src/sub_channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{collections::HashMap, marker::PhantomData, ops::Deref, sync::Mutex};
use bitcoin::{hashes::hex::ToHex, OutPoint, PackedLockTime, Script, Sequence, Transaction, Txid};
use bitcoin::hashes::Hash;
use dlc::{channel::sub_channel::LN_GLUE_TX_WEIGHT, PartyParams};
use dlc_messages::FeeConfig;
use dlc_messages::{
channel::{AcceptChannel, OfferChannel},
oracle_msgs::OracleAnnouncement,
Expand Down Expand Up @@ -405,6 +406,7 @@ where
self.dlc_channel_manager.get_blockchain(),
self.dlc_channel_manager.get_time(),
temporary_channel_id,
FeeConfig::EvenSplit.into(),
true,
None
)?;
Expand Down Expand Up @@ -679,6 +681,7 @@ where
self.dlc_channel_manager.get_blockchain(),
Some(sub_channel_info),
params,
FeeConfig::EvenSplit.into(),
)?;

let ln_glue_signature = dlc::util::get_raw_sig_for_tx_input(
Expand Down Expand Up @@ -1689,7 +1692,8 @@ where
cet_locktime: sub_channel_offer.cet_locktime,
refund_locktime: sub_channel_offer.refund_locktime,
cet_nsequence: sub_channel_offer.cet_nsequence,
reference_id: None
reference_id: None,
fee_config: Some(FeeConfig::EvenSplit)
};

let (offered_channel, offered_contract) =
Expand Down Expand Up @@ -1918,6 +1922,7 @@ where
self.dlc_channel_manager.get_wallet(),
Some(sub_channel_info),
self.dlc_channel_manager.get_chain_monitor(),
FeeConfig::EvenSplit.into(),
)?;

dlc::verify_tx_input_sig(
Expand Down Expand Up @@ -3307,7 +3312,8 @@ where
is_offer_party: false,
counter_party: dlc_channel.counter_party,
cet_nsequence: CET_NSEQUENCE,
reference_id: None
reference_id: None,
fee_config: Some(FeeConfig::EvenSplit)
};
self.dlc_channel_manager
.get_store()
Expand Down Expand Up @@ -3383,7 +3389,8 @@ where
counter_party: dlc_channel.counter_party,
// TODO(tibo): use value from original offer
cet_nsequence: CET_NSEQUENCE,
reference_id: None
reference_id: None,
fee_config: Some(FeeConfig::EvenSplit)
};
self.ln_channel_manager.set_funding_outpoint(
channel_lock,
Expand Down Expand Up @@ -3451,7 +3458,8 @@ where
is_offer_party: false,
counter_party: dlc_channel.counter_party,
cet_nsequence: CET_NSEQUENCE,
reference_id: None
reference_id: None,
fee_config: Some(FeeConfig::EvenSplit),
};
self.dlc_channel_manager
.get_store()
Expand Down
Loading
Loading