Skip to content

Commit

Permalink
feat: Add reference id
Browse files Browse the repository at this point in the history
Allows the library user to provide an optional reference or correlation id to track channel messages and states based on an external id.

rust-dlc will take that id and pass it over on messages and states.

A reference id can be provided when starting the following flows.

- offer channel
- settle offer
- renew offer
- offer collaborative close
- force close channel
  • Loading branch information
holzeis committed Feb 21, 2024
1 parent fad1a26 commit d783019
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 130 deletions.
6 changes: 5 additions & 1 deletion dlc-manager/src/channel/accepted_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitcoin::{Script, Transaction};
use dlc_messages::channel::AcceptChannel;
use secp256k1_zkp::{EcdsaAdaptorSignature, PublicKey};

use crate::{contract::accepted_contract::AcceptedContract, ContractId, DlcChannelId};
use crate::{contract::accepted_contract::AcceptedContract, ContractId, DlcChannelId, ReferenceId};

use super::party_points::PartyBasePoints;

Expand Down Expand Up @@ -38,6 +38,8 @@ pub struct AcceptedChannel {
pub accept_per_update_seed: PublicKey,
/// The accept party adaptor signature for the buffer transaction.
pub accept_buffer_adaptor_signature: EcdsaAdaptorSignature,
/// The reference id set by the api user.
pub reference_id: Option<ReferenceId>
}

impl AcceptedChannel {
Expand All @@ -46,6 +48,7 @@ impl AcceptedChannel {
contract: &AcceptedContract,
buffer_adaptor_signature: &EcdsaAdaptorSignature,
cet_adaptor_signatures: &[EcdsaAdaptorSignature],
reference_id: Option<ReferenceId>,
) -> AcceptChannel {
AcceptChannel {
temporary_channel_id: self.temporary_channel_id,
Expand All @@ -64,6 +67,7 @@ impl AcceptedChannel {
own_basepoint: self.accept_base_points.own_basepoint,
first_per_update_point: self.accept_per_update_point,
buffer_adaptor_signature: *buffer_adaptor_signature,
reference_id
}
}
}
43 changes: 42 additions & 1 deletion dlc-manager/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use bitcoin::{hashes::Hash, Transaction, Txid};
use dlc_messages::channel::{AcceptChannel, SignChannel};
use secp256k1_zkp::PublicKey;

use crate::{ContractId, DlcChannelId};
use crate::{ContractId, DlcChannelId, ReferenceId};
use crate::channel::signed_channel::SignedChannelState;

use self::{
accepted_channel::AcceptedChannel, offered_channel::OfferedChannel,
Expand Down Expand Up @@ -91,6 +92,36 @@ impl Channel {
Channel::Cancelled(o) => o.counter_party
}
}

/// Returns the reference id associated with the channel
pub fn get_reference_id(&self) -> Option<ReferenceId> {
match self {
Channel::Offered(o) => o.reference_id,
Channel::Accepted(a) => a.reference_id,
Channel::Signed(s) => match s.state {
SignedChannelState::Established { reference_id, .. } => reference_id,
SignedChannelState::SettledOffered { reference_id, .. } => reference_id,
SignedChannelState::SettledReceived { reference_id, .. } => reference_id,
SignedChannelState::SettledAccepted { reference_id, .. } => reference_id,
SignedChannelState::SettledConfirmed { reference_id, .. } => reference_id,
SignedChannelState::Settled { reference_id, .. } => reference_id,
SignedChannelState::RenewOffered { reference_id, .. } => reference_id,
SignedChannelState::RenewAccepted { reference_id, .. } => reference_id,
SignedChannelState::RenewConfirmed { reference_id, .. } => reference_id,
SignedChannelState::RenewFinalized { reference_id, .. } => reference_id,
SignedChannelState::Closing { reference_id, .. } => reference_id,
SignedChannelState::CollaborativeCloseOffered { reference_id, .. } => reference_id,
},
Channel::FailedAccept(f) => f.reference_id,
Channel::FailedSign(f) => f.reference_id,
Channel::Closing(c) => c.reference_id,
Channel::Closed(c) | Channel::CounterClosed(c) | Channel::CollaborativelyClosed(c) => {
c.reference_id
}
Channel::ClosedPunished(c) => c.reference_id,
Channel::Cancelled(o) => o.reference_id
}
}
}

/// A channel that failed when validating an
Expand All @@ -106,6 +137,8 @@ pub struct FailedAccept {
pub error_message: String,
/// The [`dlc_messages::channel::AcceptChannel`] that was received.
pub accept_message: AcceptChannel,
/// The reference id set by the api user.
pub reference_id: Option<ReferenceId>,
}

/// A channel that failed when validating an
Expand All @@ -121,6 +154,8 @@ pub struct FailedSign {
pub error_message: String,
/// The [`dlc_messages::channel::SignChannel`] that was received.
pub sign_message: SignChannel,
/// The reference id set by the api user.
pub reference_id: Option<ReferenceId>,
}

#[derive(Clone)]
Expand All @@ -142,6 +177,8 @@ pub struct ClosingChannel {
pub contract_id: ContractId,
/// Whether the local party initiated the closing of the channel.
pub is_closer: bool,
/// The reference id set by the api user.
pub reference_id: Option<ReferenceId>,
}

#[derive(Clone)]
Expand All @@ -153,6 +190,8 @@ pub struct ClosedChannel {
pub temporary_channel_id: DlcChannelId,
/// The [`DlcChannelId`] for the channel.
pub channel_id: DlcChannelId,
/// The reference id set by the api user.
pub reference_id: Option<ReferenceId>,
}

#[derive(Clone)]
Expand All @@ -167,6 +206,8 @@ pub struct ClosedPunishedChannel {
pub channel_id: DlcChannelId,
/// The transaction id of the punishment transaction that was broadcast.
pub punish_txid: Txid,
/// The reference id set by the api user.
pub reference_id: Option<ReferenceId>,
}

impl Channel {
Expand Down
11 changes: 6 additions & 5 deletions dlc-manager/src/channel/offered_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use dlc_messages::channel::OfferChannel;
// use dlc_messages::channel::OfferChannel;
use secp256k1_zkp::PublicKey;

use crate::{
contract::offered_contract::OfferedContract, conversion_utils::get_tx_input_infos,
error::Error, ContractId, DlcChannelId,
};
use crate::{contract::offered_contract::OfferedContract, conversion_utils::get_tx_input_infos, error::Error, ContractId, DlcChannelId, ReferenceId};

use super::party_points::PartyBasePoints;

Expand Down Expand Up @@ -41,10 +38,12 @@ pub struct OfferedChannel {
pub counter_party: PublicKey,
/// 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>
}

impl OfferedChannel {
pub(crate) fn get_offer_channel_msg(&self, offered_contract: &OfferedContract) -> OfferChannel {
pub(crate) fn get_offer_channel_msg(&self, offered_contract: &OfferedContract, reference_id: Option<ReferenceId>) -> OfferChannel {
let party_points = &self.party_points;
OfferChannel {
protocol_version: crate::conversion_utils::PROTOCOL_VERSION,
Expand Down Expand Up @@ -73,6 +72,7 @@ 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
}
}

Expand All @@ -97,6 +97,7 @@ impl OfferedChannel {
is_offer_party: false,
counter_party,
cet_nsequence: offer_channel.cet_nsequence,
reference_id: offer_channel.reference_id,
};

let (inputs, input_amount) = get_tx_input_infos(&offer_channel.funding_inputs)?;
Expand Down
41 changes: 21 additions & 20 deletions 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) });
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!(AcceptedChannel, {
(accepted_contract_id, writeable),
(offer_base_points, writeable),
Expand All @@ -25,7 +25,8 @@ impl_dlc_writeable!(AcceptedChannel, {
(channel_id, writeable),
(accept_per_update_seed, writeable),
(accept_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}),
(counter_party, writeable)
(counter_party, writeable),
(reference_id, option)
});
impl_dlc_writeable!(SignedChannel, {
(channel_id, writeable),
Expand All @@ -51,23 +52,23 @@ impl_dlc_writeable!(SignedChannel, {

impl_dlc_writeable_enum!(
SignedChannelState,;
(0, Established, {(signed_contract_id, writeable), (own_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (counter_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (buffer_transaction, writeable), (is_offer, writeable), (total_collateral, writeable)}),
(1, SettledOffered, {(counter_payout, writeable), (next_per_update_point, writeable), (timeout, writeable)}),
(2, SettledReceived, {(own_payout, writeable), (counter_next_per_update_point, writeable), (counter_payout, writeable)}),
(3, SettledAccepted, {(counter_next_per_update_point, writeable), (own_next_per_update_point, writeable), (settle_tx, writeable), (own_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (counter_payout, writeable)}),
(4, SettledConfirmed, {(settle_tx, writeable), (counter_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (own_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (counter_next_per_update_point, writeable), (own_next_per_update_point, writeable), (timeout, writeable), (own_payout, writeable), (counter_payout, writeable) }),
(5, Settled, {(settle_tx, writeable), (counter_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (own_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (own_payout, writeable), (counter_payout, writeable)}),
(6, RenewOffered, {(offered_contract_id, writeable), (counter_payout, writeable), (is_offer, writeable), (offer_next_per_update_point, writeable), (timeout, writeable)}),
(7, RenewAccepted, {(contract_id, writeable), (offer_per_update_point, writeable), (accept_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (timeout, writeable), (own_payout, writeable)}),
(8, RenewConfirmed, {(contract_id, writeable), (offer_per_update_point, writeable), (accept_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (offer_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (total_collateral, writeable)}),
(10, RenewFinalized, {(contract_id, writeable), (prev_offer_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (offer_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (accept_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (total_collateral, writeable)}),
(9, Closing, {(buffer_transaction, writeable), (contract_id, writeable), (is_initiator, writeable)}),
(11, CollaborativeCloseOffered, { (counter_payout, writeable), (offer_signature, writeable), (close_tx, writeable), (timeout, writeable), (is_offer, writeable) })
(0, Established, {(signed_contract_id, writeable), (own_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (counter_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (buffer_transaction, writeable), (is_offer, writeable), (total_collateral, writeable), (reference_id, option)}),
(1, SettledOffered, {(counter_payout, writeable), (next_per_update_point, writeable), (timeout, writeable), (reference_id, option)}),
(2, SettledReceived, {(own_payout, writeable), (counter_next_per_update_point, writeable), (counter_payout, writeable), (reference_id, option)}),
(3, SettledAccepted, {(counter_next_per_update_point, writeable), (own_next_per_update_point, writeable), (settle_tx, writeable), (own_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (counter_payout, writeable), (reference_id, option)}),
(4, SettledConfirmed, {(settle_tx, writeable), (counter_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (own_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (counter_next_per_update_point, writeable), (own_next_per_update_point, writeable), (timeout, writeable), (own_payout, writeable), (counter_payout, writeable), (reference_id, option) }),
(5, Settled, {(settle_tx, writeable), (counter_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (own_settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (own_payout, writeable), (counter_payout, writeable), (reference_id, option)}),
(6, RenewOffered, {(offered_contract_id, writeable), (counter_payout, writeable), (is_offer, writeable), (offer_next_per_update_point, writeable), (timeout, writeable), (reference_id, option)}),
(7, RenewAccepted, {(contract_id, writeable), (offer_per_update_point, writeable), (accept_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (timeout, writeable), (own_payout, writeable), (reference_id, option)}),
(8, RenewConfirmed, {(contract_id, writeable), (offer_per_update_point, writeable), (accept_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (offer_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (total_collateral, writeable), (reference_id, option)}),
(10, RenewFinalized, {(contract_id, writeable), (prev_offer_per_update_point, writeable), (buffer_transaction, writeable), (buffer_script_pubkey, writeable), (offer_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (accept_buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (timeout, writeable), (own_payout, writeable), (total_collateral, writeable), (reference_id, option)}),
(9, Closing, {(buffer_transaction, writeable), (contract_id, writeable), (is_initiator, writeable), (reference_id, option)}),
(11, CollaborativeCloseOffered, { (counter_payout, writeable), (offer_signature, writeable), (close_tx, writeable), (timeout, writeable), (is_offer, writeable), (reference_id, option) })
;;
);

impl_dlc_writeable!(FailedAccept, {(temporary_channel_id, writeable), (error_message, {cb_writeable, write_string, read_string}), (accept_message, writeable), (counter_party, writeable)});
impl_dlc_writeable!(FailedSign, {(channel_id, writeable), (error_message, {cb_writeable, write_string, read_string}), (sign_message, writeable), (counter_party, writeable)});
impl_dlc_writeable!(FailedAccept, {(temporary_channel_id, writeable), (error_message, {cb_writeable, write_string, read_string}), (accept_message, writeable), (counter_party, writeable), (reference_id, option)});
impl_dlc_writeable!(FailedSign, {(channel_id, writeable), (error_message, {cb_writeable, write_string, read_string}), (sign_message, writeable), (counter_party, writeable), (reference_id, option)});

impl_dlc_writeable!(ClosingChannel, {
(channel_id, writeable),
Expand All @@ -76,8 +77,8 @@ impl_dlc_writeable!(ClosingChannel, {
(rollback_state, option),
(buffer_transaction, writeable),
(contract_id, writeable),
(is_closer, writeable)

(is_closer, writeable),
(reference_id, option)
});
impl_dlc_writeable!(ClosedChannel, {(channel_id, writeable), (counter_party, writeable), (temporary_channel_id, writeable)});
impl_dlc_writeable!(ClosedPunishedChannel, {(channel_id, writeable), (counter_party, writeable), (temporary_channel_id, writeable), (punish_txid, writeable)});
impl_dlc_writeable!(ClosedChannel, {(channel_id, writeable), (counter_party, writeable), (temporary_channel_id, writeable), (reference_id, option)});
impl_dlc_writeable!(ClosedPunishedChannel, {(channel_id, writeable), (counter_party, writeable), (temporary_channel_id, writeable), (punish_txid, writeable), (reference_id, option)});
Loading

0 comments on commit d783019

Please sign in to comment.