Skip to content

Commit

Permalink
wip: add reference id
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Feb 15, 2024
1 parent dc9cac8 commit 58bc4be
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 128 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
}
}
}
12 changes: 11 additions & 1 deletion dlc-manager/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 self::{
accepted_channel::AcceptedChannel, offered_channel::OfferedChannel,
Expand Down Expand Up @@ -106,6 +106,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 +123,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 +146,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 +159,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 +175,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
9 changes: 5 additions & 4 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,6 +38,8 @@ 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 {
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: None
}
}

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)});
26 changes: 25 additions & 1 deletion dlc-manager/src/channel/signed_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use dlc::PartyParams;
use lightning::ln::{chan_utils::CounterpartyCommitmentSecrets, ChannelId};
use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature, PublicKey};

use crate::{ContractId, DlcChannelId};
use crate::{ContractId, DlcChannelId, ReferenceId};

use super::party_points::PartyBasePoints;

Expand Down Expand Up @@ -109,6 +109,8 @@ typed_enum!(
is_offer: bool,
/// The total amount of collateral in the channel
total_collateral: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `SettledOffered` state when the local party
/// has sent a [`dlc_messages::channel::SettleOffer`] message.
Expand All @@ -121,6 +123,8 @@ typed_enum!(
/// The UNIX epoch at which the counter party will be considered
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `SettledReceived` state when the local party
/// has received a [`dlc_messages::channel::SettleOffer`] message.
Expand All @@ -132,6 +136,8 @@ typed_enum!(
/// The per update point to be used by the counter party for the setup
/// of the next channel state.
counter_next_per_update_point: PublicKey,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `SettledAccepted` state when the local party
/// has sent a [`dlc_messages::channel::SettleAccept`] message.
Expand All @@ -154,6 +160,8 @@ typed_enum!(
own_payout: u64,
/// The payout that was proposed to the counter party.
counter_payout: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `SettledConfirmed` state when the local party
/// has sent a [`dlc_messages::channel::SettleConfirm`] message.
Expand All @@ -179,6 +187,8 @@ typed_enum!(
own_payout: u64,
/// The payout that was proposed to the counter party.
counter_payout: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `Settled` state when the local party
/// has all the necessary information to close the channel with the last
Expand All @@ -196,6 +206,8 @@ typed_enum!(
own_payout: u64,
/// The amount the counter party holds in the channel.
counter_payout: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `RenewOffered` state when the local party
/// has sent or received a [`dlc_messages::channel::RenewOffer`] message.
Expand All @@ -212,6 +224,8 @@ typed_enum!(
/// The UNIX epoch at which the counter party will be considered
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `RenewAccepted` state when the local party
/// has sent a [`dlc_messages::channel::RenewAccept`] message.
Expand All @@ -233,6 +247,8 @@ typed_enum!(
timeout: u64,
/// The payout to the local party attributed for closing the previous state.
own_payout: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `RenewConfirmed` state when the local party
/// has sent a [`dlc_messages::channel::RenewConfirm`] message.
Expand All @@ -259,6 +275,8 @@ typed_enum!(
own_payout: u64,
/// The total amount of collateral in the channel.
total_collateral: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// Finalize the renewal of the contract within a DLC channel.
RenewFinalized {
Expand All @@ -284,6 +302,8 @@ typed_enum!(
own_payout: u64,
/// The total amount of collateral in the channel.
total_collateral: u64,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `Closing` state when the local party
/// has broadcast a buffer transaction and is waiting to finalize the
Expand All @@ -296,6 +316,8 @@ typed_enum!(
contract_id: ContractId,
/// Whether the local party initiated the closing of the channel.
is_initiator: bool,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
/// A [`SignedChannel`] is in `CollaborativeCloseOffered` state when the local party
/// has sent a [`dlc_messages::channel::CollaborativeCloseOffer`] message.
Expand All @@ -311,6 +333,8 @@ typed_enum!(
timeout: u64,
/// Indicates whether the local party offered the collaborative close or not.
is_offer: bool,
/// The reference id set by the api user.
reference_id: Option<ReferenceId>,
},
},
/// Enum automatically generated associating a number to each signed channel
Expand Down
Loading

0 comments on commit 58bc4be

Please sign in to comment.