From 58bc4bea715af0fe4e758aa9b1ef9723013d1c76 Mon Sep 17 00:00:00 2001 From: Richard Holzeis Date: Thu, 15 Feb 2024 17:32:26 +0100 Subject: [PATCH] wip: add reference id --- dlc-manager/src/channel/accepted_channel.rs | 6 +- dlc-manager/src/channel/mod.rs | 12 +- dlc-manager/src/channel/offered_channel.rs | 9 +- dlc-manager/src/channel/ser.rs | 41 ++--- dlc-manager/src/channel/signed_channel.rs | 26 ++- dlc-manager/src/channel_updater.rs | 148 ++++++++++++------ dlc-manager/src/lib.rs | 3 + dlc-manager/src/manager.rs | 75 ++++++--- dlc-manager/src/sub_channel_manager.rs | 10 +- dlc-manager/tests/channel_execution_tests.rs | 31 ++-- .../tests/ln_dlc_channel_execution_tests.rs | 2 + dlc-messages/src/channel.rs | 71 +++++++-- sample/src/cli.rs | 6 +- 13 files changed, 312 insertions(+), 128 deletions(-) diff --git a/dlc-manager/src/channel/accepted_channel.rs b/dlc-manager/src/channel/accepted_channel.rs index e2cf30d6..995846bd 100644 --- a/dlc-manager/src/channel/accepted_channel.rs +++ b/dlc-manager/src/channel/accepted_channel.rs @@ -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; @@ -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 } impl AcceptedChannel { @@ -46,6 +48,7 @@ impl AcceptedChannel { contract: &AcceptedContract, buffer_adaptor_signature: &EcdsaAdaptorSignature, cet_adaptor_signatures: &[EcdsaAdaptorSignature], + reference_id: Option, ) -> AcceptChannel { AcceptChannel { temporary_channel_id: self.temporary_channel_id, @@ -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 } } } diff --git a/dlc-manager/src/channel/mod.rs b/dlc-manager/src/channel/mod.rs index cc6cbc6e..44410ee5 100644 --- a/dlc-manager/src/channel/mod.rs +++ b/dlc-manager/src/channel/mod.rs @@ -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, @@ -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, } /// A channel that failed when validating an @@ -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, } #[derive(Clone)] @@ -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, } #[derive(Clone)] @@ -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, } #[derive(Clone)] @@ -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, } impl Channel { diff --git a/dlc-manager/src/channel/offered_channel.rs b/dlc-manager/src/channel/offered_channel.rs index bda3c9ed..05d36bec 100644 --- a/dlc-manager/src/channel/offered_channel.rs +++ b/dlc-manager/src/channel/offered_channel.rs @@ -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; @@ -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 } impl OfferedChannel { @@ -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 } } @@ -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)?; diff --git a/dlc-manager/src/channel/ser.rs b/dlc-manager/src/channel/ser.rs index 325178c7..4940c1bf 100644 --- a/dlc-manager/src/channel/ser.rs +++ b/dlc-manager/src/channel/ser.rs @@ -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), @@ -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), @@ -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), @@ -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)}); diff --git a/dlc-manager/src/channel/signed_channel.rs b/dlc-manager/src/channel/signed_channel.rs index 1a0aed4f..e97f2834 100644 --- a/dlc-manager/src/channel/signed_channel.rs +++ b/dlc-manager/src/channel/signed_channel.rs @@ -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; @@ -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, }, /// A [`SignedChannel`] is in `SettledOffered` state when the local party /// has sent a [`dlc_messages::channel::SettleOffer`] message. @@ -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, }, /// A [`SignedChannel`] is in `SettledReceived` state when the local party /// has received a [`dlc_messages::channel::SettleOffer`] message. @@ -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, }, /// A [`SignedChannel`] is in `SettledAccepted` state when the local party /// has sent a [`dlc_messages::channel::SettleAccept`] message. @@ -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, }, /// A [`SignedChannel`] is in `SettledConfirmed` state when the local party /// has sent a [`dlc_messages::channel::SettleConfirm`] message. @@ -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, }, /// A [`SignedChannel`] is in `Settled` state when the local party /// has all the necessary information to close the channel with the last @@ -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, }, /// A [`SignedChannel`] is in `RenewOffered` state when the local party /// has sent or received a [`dlc_messages::channel::RenewOffer`] message. @@ -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, }, /// A [`SignedChannel`] is in `RenewAccepted` state when the local party /// has sent a [`dlc_messages::channel::RenewAccept`] message. @@ -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, }, /// A [`SignedChannel`] is in `RenewConfirmed` state when the local party /// has sent a [`dlc_messages::channel::RenewConfirm`] message. @@ -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, }, /// Finalize the renewal of the contract within a DLC channel. RenewFinalized { @@ -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, }, /// A [`SignedChannel`] is in `Closing` state when the local party /// has broadcast a buffer transaction and is waiting to finalize the @@ -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, }, /// A [`SignedChannel`] is in `CollaborativeCloseOffered` state when the local party /// has sent a [`dlc_messages::channel::CollaborativeCloseOffer`] message. @@ -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, }, }, /// Enum automatically generated associating a number to each signed channel diff --git a/dlc-manager/src/channel_updater.rs b/dlc-manager/src/channel_updater.rs index 5a76af44..0542f892 100644 --- a/dlc-manager/src/channel_updater.rs +++ b/dlc-manager/src/channel_updater.rs @@ -2,28 +2,20 @@ use std::{ops::Deref, sync::Mutex}; -use crate::{ - chain_monitor::{ChainMonitor, ChannelInfo, TxType}, - channel::{ - accepted_channel::AcceptedChannel, - offered_channel::OfferedChannel, - party_points::PartyBasePoints, - signed_channel::{SignedChannel, SignedChannelState}, - Channel, ClosedChannel, - }, - contract::{ - accepted_contract::AcceptedContract, contract_info::ContractInfo, - contract_input::ContractInput, offered_contract::OfferedContract, - signed_contract::SignedContract, AdaptorInfo, - }, - 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, -}; +use crate::{chain_monitor::{ChainMonitor, ChannelInfo, TxType}, channel::{ + accepted_channel::AcceptedChannel, + offered_channel::OfferedChannel, + party_points::PartyBasePoints, + signed_channel::{SignedChannel, SignedChannelState}, + Channel, ClosedChannel, +}, contract::{ + accepted_contract::AcceptedContract, contract_info::ContractInfo, + contract_input::ContractInput, offered_contract::OfferedContract, + signed_contract::SignedContract, AdaptorInfo, +}, 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}; use bitcoin::{OutPoint, Script, Sequence, Transaction}; use dlc::{ channel::{get_tx_adaptor_signature, verify_tx_adaptor_signature, DlcChannelTransactions}, @@ -110,6 +102,7 @@ pub fn offer_channel( time: &T, temporary_channel_id: DlcChannelId, is_sub_channel: bool, + reference_id: Option ) -> Result<(OfferedChannel, OfferedContract), Error> where W::Target: Wallet, @@ -160,6 +153,7 @@ where is_offer_party: true, counter_party: *counter_party, cet_nsequence, + reference_id }; Ok((offered_channel, offered_contract)) @@ -208,6 +202,7 @@ where W::Target: Wallet, B::Target: Blockchain, { + assert_eq!(offered_channel.offered_contract_id, offered_contract.id); let (accept_params, funding_inputs, accept_points, per_update_seed) = @@ -345,6 +340,8 @@ where &dlc_transactions, )?; + let reference_id = offered_channel.reference_id.clone(); + let accepted_channel = AcceptedChannel { offer_base_points: offered_channel.party_points.clone(), accept_base_points: accept_points, @@ -358,12 +355,14 @@ where accept_per_update_seed: PublicKey::from_secret_key(secp, &per_update_seed), counter_party: offered_contract.counter_party, accept_buffer_adaptor_signature: buffer_adaptor_signature, + reference_id, }; let accept_channel = accepted_channel.get_accept_channel_msg( &accepted_contract, &buffer_adaptor_signature, &adaptor_sigs, + reference_id, ); Ok((accepted_channel, accepted_contract, accept_channel)) @@ -603,6 +602,7 @@ where buffer_transaction, is_offer: true, total_collateral, + reference_id: accept_channel.reference_id }, update_idx: INITIAL_UPDATE_NUMBER, channel_id, @@ -630,6 +630,7 @@ where buffer_adaptor_signature: own_buffer_adaptor_signature, refund_signature: signed_contract.offer_refund_signature, funding_signatures: signed_contract.funding_signatures.clone(), + reference_id: accept_channel.reference_id }; Ok((signed_channel, signed_contract, sign_channel)) @@ -767,6 +768,7 @@ where buffer_transaction: accepted_channel.buffer_transaction.clone(), is_offer: false, total_collateral: accepted_contract.offered_contract.total_collateral, + reference_id: accepted_channel.reference_id, }, update_idx: INITIAL_UPDATE_NUMBER, fund_tx: signed_contract @@ -801,6 +803,7 @@ pub fn settle_channel_offer( peer_timeout: u64, signer: &S, time: &T, + reference_id: Option ) -> Result where S::Target: Signer, @@ -828,6 +831,7 @@ where counter_payout, next_per_update_point, timeout: time.unix_time_now() + peer_timeout, + reference_id }; std::mem::swap(&mut channel.state, &mut state); @@ -838,7 +842,8 @@ where channel_id: channel.channel_id, counter_payout, next_per_update_point, - timestamp: get_unix_time_now() + timestamp: get_unix_time_now(), + reference_id }; Ok(settle_channel_offer) @@ -870,6 +875,7 @@ pub fn on_settle_offer( own_payout: settle_offer.counter_payout, counter_next_per_update_point: settle_offer.next_per_update_point, counter_payout: total_collateral - settle_offer.counter_payout, + reference_id: settle_offer.reference_id, }; std::mem::swap(&mut signed_channel.state, &mut new_state); @@ -923,14 +929,15 @@ where S::Target: Signer, T::Target: Time, { - let (own_payout, counter_next_per_update_point, counter_payout) = + let (own_payout, counter_next_per_update_point, counter_payout, reference_id) = if let SignedChannelState::SettledReceived { own_payout, counter_next_per_update_point, counter_payout, + reference_id } = channel.state { - (own_payout, counter_next_per_update_point, counter_payout) + (own_payout, counter_next_per_update_point, counter_payout, reference_id) } else { return Err(Error::InvalidState( "Signed channel was not in SettledReceived state as expected.".to_string(), @@ -1001,12 +1008,14 @@ where timeout: time.unix_time_now() + peer_timeout, own_payout, counter_payout, + reference_id }; let msg = SettleAccept { channel_id: channel.channel_id, next_per_update_point: own_next_per_update_point, settle_adaptor_signature, + reference_id }; Ok(msg) @@ -1063,12 +1072,13 @@ where T::Target: Time, S::Target: Signer, { - let (counter_payout, next_per_update_point) = match channel.state { + let (counter_payout, next_per_update_point, reference_id) = match channel.state { SignedChannelState::SettledOffered { counter_payout, next_per_update_point, + reference_id, .. - } => (counter_payout, next_per_update_point), + } => (counter_payout, next_per_update_point, reference_id), _ => { return Err(Error::InvalidState( "Signed channel was not in SettledOffered state as expected.".to_string(), @@ -1143,6 +1153,7 @@ where timeout: time.unix_time_now() + peer_timeout, own_payout: final_offer_payout, counter_payout: final_accept_payout, + reference_id }; channel.state = state; @@ -1151,6 +1162,7 @@ where channel_id: channel.channel_id, prev_per_update_secret, settle_adaptor_signature, + reference_id }; Ok(msg) @@ -1189,6 +1201,7 @@ where own_settle_adaptor_signature, own_payout, counter_payout, + reference_id ) = match &channel.state { SignedChannelState::SettledAccepted { counter_next_per_update_point, @@ -1197,6 +1210,7 @@ where own_settle_adaptor_signature, own_payout, counter_payout, + reference_id, .. } => ( own_next_per_update_point, @@ -1205,6 +1219,7 @@ where own_settle_adaptor_signature, *own_payout, *counter_payout, + *reference_id ), _ => { return Err(Error::InvalidState( @@ -1264,6 +1279,7 @@ where own_settle_adaptor_signature: *own_settle_adaptor_signature, own_payout, counter_payout, + reference_id }; channel.own_per_update_point = *own_next_per_update_point; @@ -1275,6 +1291,7 @@ where let msg = SettleFinalize { channel_id: channel.channel_id, prev_per_update_secret, + reference_id }; Ok(msg) @@ -1297,6 +1314,7 @@ pub fn settle_channel_on_finalize( own_settle_adaptor_signature, own_payout, counter_payout, + reference_id ) = match &channel.state { SignedChannelState::SettledConfirmed { settle_tx, @@ -1306,6 +1324,7 @@ pub fn settle_channel_on_finalize( own_settle_adaptor_signature, own_payout, counter_payout, + reference_id, .. } => ( settle_tx.clone(), @@ -1315,6 +1334,7 @@ pub fn settle_channel_on_finalize( *own_settle_adaptor_signature, *own_payout, *counter_payout, + *reference_id ), _ => { return Err(Error::InvalidState( @@ -1347,6 +1367,7 @@ pub fn settle_channel_on_finalize( own_settle_adaptor_signature, own_payout, counter_payout, + reference_id }; channel.roll_back_state = None; @@ -1360,16 +1381,17 @@ pub fn settle_channel_on_finalize( /// Creates a [`Reject`] message and rolls back the state of the channel. Expects /// the channel to be in [`SignedChannelState::SettledOffered`] state. pub fn reject_settle_offer(signed_channel: &mut SignedChannel) -> Result { - get_signed_channel_state!(signed_channel, SettledReceived,)?; - signed_channel.state = signed_channel .roll_back_state .take() .expect("to have a rollback state"); + let reference_id = get_signed_channel_state!(signed_channel, SettledReceived, reference_id)?; + Ok(Reject { channel_id: signed_channel.channel_id, - timestamp: get_unix_time_now() + timestamp: get_unix_time_now(), + reference_id: *reference_id }) } @@ -1386,6 +1408,7 @@ pub fn renew_offer( cet_nsequence: u32, signer: &S, time: &T, + reference_id: Option ) -> Result<(RenewOffer, OfferedContract), Error> where S::Target: Signer, @@ -1462,6 +1485,7 @@ where is_offer: true, counter_payout, timeout: time.unix_time_now() + peer_timeout, + reference_id }; std::mem::swap(&mut signed_channel.state, &mut state); @@ -1475,6 +1499,7 @@ where cet_locktime: offered_contract.cet_locktime, refund_locktime: offered_contract.refund_locktime, cet_nsequence, + reference_id }; Ok((msg, offered_contract)) @@ -1528,6 +1553,7 @@ pub fn on_renew_offer( offer_next_per_update_point: renew_offer.next_per_update_point, is_offer: false, timeout: time.unix_time_now() + peer_timeout, + reference_id: renew_offer.reference_id }; std::mem::swap(&mut signed_channel.state, &mut state); @@ -1578,12 +1604,13 @@ where S::Target: Signer, T::Target: Time, { - let (offer_next_per_update_point, own_payout) = match signed_channel.state { + let (offer_next_per_update_point, own_payout, reference_id) = match signed_channel.state { SignedChannelState::RenewOffered { offer_next_per_update_point, counter_payout, + reference_id, .. - } => (offer_next_per_update_point, counter_payout), + } => (offer_next_per_update_point, counter_payout, reference_id), _ => { return Err(Error::InvalidState( "Signed channel was not in SettledOffered state as expected.".to_string(), @@ -1663,6 +1690,7 @@ where buffer_script_pubkey, timeout: time.unix_time_now() + peer_timeout, own_payout, + reference_id }; signed_channel.state = state; @@ -1672,6 +1700,7 @@ where next_per_update_point: accept_per_update_point, cet_adaptor_signatures: (&adaptor_sigs as &[_]).into(), refund_signature: accepted_contract.accept_refund_signature, + reference_id }; Ok((accepted_contract, renew_accept)) @@ -1807,6 +1836,8 @@ where &accept_revoke_params.publish_pk.inner, )?; + let reference_id = *get_signed_channel_state!(signed_channel, RenewOffered, reference_id)?; + let state = SignedChannelState::RenewConfirmed { contract_id: signed_contract.accepted_contract.get_contract_id(), offer_per_update_point: *offer_per_update_point, @@ -1817,6 +1848,7 @@ where timeout: time.unix_time_now() + peer_timeout, own_payout, total_collateral: offered_contract.total_collateral, + reference_id }; signed_channel.state = state; @@ -1826,6 +1858,7 @@ where buffer_adaptor_signature: own_buffer_adaptor_signature, cet_adaptor_signatures: (&cet_adaptor_signatures as &[_]).into(), refund_signature: signed_contract.offer_refund_signature, + reference_id }; Ok((signed_contract, renew_confirm)) @@ -1885,13 +1918,14 @@ where own_payout, buffer_transaction, buffer_script_pubkey, + reference_id ) = get_signed_channel_state!( signed_channel, RenewAccepted, offer_per_update_point, accept_per_update_point, own_payout | buffer_transaction, - buffer_script_pubkey + buffer_script_pubkey, reference_id )?; let own_publish_pk = signed_channel @@ -1976,6 +2010,8 @@ where }, ); + let reference_id = reference_id.clone(); + signed_channel.state = SignedChannelState::RenewFinalized { contract_id: signed_contract.accepted_contract.get_contract_id(), prev_offer_per_update_point, @@ -1986,12 +2022,14 @@ where timeout: time.unix_time_now() + peer_timeout, own_payout: *own_payout, total_collateral, + reference_id }; let renew_finalize = RenewFinalize { channel_id: signed_channel.channel_id, per_update_secret: prev_per_update_secret, buffer_adaptor_signature, + reference_id }; Ok((signed_contract, renew_finalize)) @@ -2015,6 +2053,7 @@ where accept_per_update_point, offer_buffer_adaptor_signature, buffer_transaction, + reference_id ) = get_signed_channel_state!( signed_channel, RenewConfirmed, @@ -2022,7 +2061,7 @@ where total_collateral, offer_per_update_point, accept_per_update_point, - offer_buffer_adaptor_signature | buffer_transaction + offer_buffer_adaptor_signature | buffer_transaction, reference_id )?; let offer_revoke_params = signed_channel.own_points.get_revokable_params( @@ -2057,6 +2096,7 @@ where buffer_transaction: buffer_transaction.clone(), is_offer: true, total_collateral: *total_collateral, + reference_id: *reference_id }; if PublicKey::from_secret_key(secp, &renew_finalize.per_update_secret) @@ -2082,6 +2122,8 @@ where signed_channel.update_idx, ))?; + let reference_id= reference_id.clone(); + signed_channel.own_per_update_point = *offer_per_update_point; signed_channel.counter_per_update_point = *accept_per_update_point; @@ -2092,6 +2134,7 @@ where let msg = RenewRevoke { channel_id: signed_channel.channel_id, per_update_secret: prev_per_update_secret, + reference_id }; Ok(msg) @@ -2110,6 +2153,7 @@ pub fn renew_channel_on_revoke( offer_buffer_adaptor_signature, accept_buffer_adaptor_signature, buffer_transaction, + reference_id ) = get_signed_channel_state!( signed_channel, RenewFinalized, @@ -2117,7 +2161,7 @@ pub fn renew_channel_on_revoke( total_collateral, prev_offer_per_update_point, offer_buffer_adaptor_signature, - accept_buffer_adaptor_signature | buffer_transaction + accept_buffer_adaptor_signature | buffer_transaction, reference_id )?; if PublicKey::from_secret_key(secp, &renew_revoke.per_update_secret) @@ -2145,6 +2189,7 @@ pub fn renew_channel_on_revoke( buffer_transaction: buffer_transaction.clone(), is_offer: true, total_collateral: *total_collateral, + reference_id: *reference_id }; Ok(()) @@ -2154,7 +2199,7 @@ pub fn renew_channel_on_revoke( /// the channel to be in [`SignedChannelState::RenewOffered`] state and the local /// party not to be the offer party. pub fn reject_renew_offer(signed_channel: &mut SignedChannel) -> Result { - let is_offer = get_signed_channel_state!(signed_channel, RenewOffered, is_offer)?; + let (is_offer, reference_id) = get_signed_channel_state!(signed_channel, RenewOffered, is_offer | reference_id)?; if *is_offer { return Err(Error::InvalidState( @@ -2162,6 +2207,8 @@ pub fn reject_renew_offer(signed_channel: &mut SignedChannel) -> Result Result( counter_payout: u64, signer: &S, time: &T, + reference_id: Option ) -> Result<(CollaborativeCloseOffer, Transaction), Error> where S::Target: Signer, @@ -2228,6 +2277,7 @@ where close_tx: close_tx.clone(), timeout: time.unix_time_now() + super::manager::PEER_TIMEOUT, is_offer: true, + reference_id }; std::mem::swap(&mut state, &mut signed_channel.state); signed_channel.roll_back_state = Some(state); @@ -2237,6 +2287,7 @@ where channel_id: signed_channel.channel_id, counter_payout, close_signature, + reference_id }, close_tx, )) @@ -2286,7 +2337,8 @@ where offer_signature: close_offer.close_signature, close_tx, timeout: time.unix_time_now() + peer_timeout, - is_offer: false + is_offer: false, + reference_id: close_offer.reference_id }; std::mem::swap(&mut state, &mut signed_channel.state); @@ -2305,10 +2357,10 @@ pub fn accept_collaborative_close_offer( where S::Target: Signer, { - let (offer_signature, close_tx, is_offer) = get_signed_channel_state!( + let (offer_signature, close_tx, is_offer, reference_id) = get_signed_channel_state!( signed_channel, CollaborativeCloseOffered, - offer_signature | close_tx, is_offer + offer_signature | close_tx, is_offer, reference_id )?; if *is_offer { @@ -2339,6 +2391,7 @@ where counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: *reference_id }); Ok((close_tx, channel)) } @@ -2460,7 +2513,8 @@ pub fn initiate_unilateral_close_established_channel( mut buffer_transaction: Transaction, signer: &S, sub_channel: Option<(SubChannel, &ClosingSubChannel)>, - is_initiator: bool, + is_initiator: bool, //TODO(holzeis): Check what this flag is for. It kind of is in conflict with the function name. + reference_id: Option ) -> Result<(), Error> where S::Target: Signer, @@ -2564,6 +2618,7 @@ where buffer_transaction, contract_id, is_initiator, + reference_id, }; Ok(()) @@ -2583,8 +2638,8 @@ pub fn finalize_unilateral_close_settled_channel( where S::Target: Signer, { - let buffer_transaction = - get_signed_channel_state!(signed_channel, Closing, buffer_transaction)?; + let (buffer_transaction, reference_id) = + get_signed_channel_state!(signed_channel, Closing, buffer_transaction | reference_id)?; let (range_info, oracle_sigs) = crate::utils::get_range_info_and_oracle_sigs(contract_info, adaptor_info, attestations)?; @@ -2667,6 +2722,7 @@ where counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: *reference_id }; let channel = if is_initiator { Channel::Closed(closed_channel) @@ -2700,10 +2756,10 @@ pub(crate) fn close_settled_channel_internal( where S::Target: Signer, { - let (counter_settle_adaptor_signature, settle_tx) = get_signed_channel_state!( + let (counter_settle_adaptor_signature, settle_tx, reference_id) = get_signed_channel_state!( signed_channel, Settled, - counter_settle_adaptor_signature | settle_tx + counter_settle_adaptor_signature | settle_tx, reference_id )?; let mut settle_tx = settle_tx.clone(); @@ -2802,12 +2858,14 @@ where counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: *reference_id }) } else { Channel::CounterClosed(ClosedChannel { counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: *reference_id }) }; Ok((settle_tx, channel)) diff --git a/dlc-manager/src/lib.rs b/dlc-manager/src/lib.rs index 68d500b6..9a155dff 100644 --- a/dlc-manager/src/lib.rs +++ b/dlc-manager/src/lib.rs @@ -65,6 +65,9 @@ pub type ContractId = [u8; 32]; /// Type alias for a DLC channel ID. pub type DlcChannelId = [u8; 32]; +/// The reference id struct for a user provided id to refer to the rust dlc data and messages +pub type ReferenceId = [u8; 32]; + /// Time trait to provide current unix time. Mainly defined to facilitate testing. pub trait Time { /// Must return the unix epoch corresponding to the current time. diff --git a/dlc-manager/src/manager.rs b/dlc-manager/src/manager.rs index 4f3d7a30..21c1d396 100644 --- a/dlc-manager/src/manager.rs +++ b/dlc-manager/src/manager.rs @@ -18,7 +18,7 @@ use crate::error::Error; use crate::sub_channel_manager::get_sub_channel_in_state; use crate::subchannel::{ClosingSubChannel, SubChannel, SubChannelState}; use crate::utils::get_object_in_state; -use crate::{ContractId, DlcChannelId, Signer}; +use crate::{ContractId, DlcChannelId, ReferenceId, Signer}; use bitcoin::consensus::encode::serialize_hex; use bitcoin::{Address, OutPoint}; use bitcoin::Transaction; @@ -791,6 +791,7 @@ where &self, contract_input: &ContractInput, counter_party: PublicKey, + reference_id: Option, ) -> Result { let oracle_announcements = contract_input .contract_infos @@ -810,6 +811,7 @@ where &self.time, crate::utils::get_new_temporary_id(), false, + reference_id )?; let msg = offered_channel.get_offer_channel_msg(&offered_contract); @@ -840,10 +842,11 @@ where self.wallet.unreserve_utxos(&utxos)?; } + let reference_id = offered_channel.reference_id; let counterparty = offered_channel.counter_party; self.store.upsert_channel(Channel::Cancelled(offered_channel), Some(Contract::Rejected(offered_contract)))?; - let msg = Reject{ channel_id: *channel_id, timestamp: get_unix_time_now() }; + let msg = Reject{ channel_id: *channel_id, timestamp: get_unix_time_now(), reference_id }; Ok((msg, counterparty)) } @@ -897,10 +900,10 @@ where } /// Force close the channel with given [`DlcChannelId`]. - pub fn force_close_channel(&self, channel_id: &DlcChannelId) -> Result<(), Error> { + pub fn force_close_channel(&self, channel_id: &DlcChannelId, reference_id: Option) -> Result<(), Error> { let channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; - self.force_close_channel_internal(channel, None, true) + self.force_close_channel_internal(channel, None, true, reference_id) } /// Offer to settle the balance of a channel so that the counter party gets @@ -910,6 +913,7 @@ where &self, channel_id: &DlcChannelId, counter_payout: u64, + reference_id: Option ) -> Result<(SettleOffer, PublicKey), Error> { let mut signed_channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; @@ -921,6 +925,7 @@ where PEER_TIMEOUT, &self.wallet, &self.time, + reference_id, )?; let counter_party = signed_channel.counter_party; @@ -983,6 +988,7 @@ where channel_id: &DlcChannelId, counter_payout: u64, contract_input: &ContractInput, + reference_id: Option ) -> Result<(RenewOffer, PublicKey), Error> { let mut signed_channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; @@ -1004,6 +1010,7 @@ where CET_NSEQUENCE, &self.wallet, &self.time, + reference_id, )?; let counter_party = offered_contract.counter_party; @@ -1118,6 +1125,7 @@ where &self, channel_id: &DlcChannelId, counter_payout: u64, + reference_id: Option ) -> Result { let mut signed_channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; @@ -1128,6 +1136,7 @@ where counter_payout, &self.wallet, &self.time, + reference_id )?; self.chain_monitor.lock().unwrap().add_tx( @@ -1321,6 +1330,7 @@ where error_message: format!("Error validating accept channel: {e}"), accept_message: accept_channel.clone(), counter_party: *peer_id, + reference_id: accept_channel.reference_id }; self.store .upsert_channel(Channel::FailedAccept(channel), None)?; @@ -1391,6 +1401,7 @@ where error_message: format!("Error validating accept channel: {e}"), sign_message: sign_channel.clone(), counter_party: *peer_id, + reference_id: accepted_channel.reference_id }; self.store .upsert_channel(Channel::FailedSign(channel), None)?; @@ -1445,7 +1456,8 @@ where if let SignedChannelState::SettledOffered { .. } = signed_channel.state { return Ok(Some(Reject { channel_id: settle_offer.channel_id, - timestamp: get_unix_time_now() + timestamp: get_unix_time_now(), + reference_id: settle_offer.reference_id })); } @@ -1652,7 +1664,8 @@ where if is_offer { return Ok(Some(Reject { channel_id: renew_offer.channel_id, - timestamp: get_unix_time_now() + timestamp: get_unix_time_now(), + reference_id: renew_offer.reference_id, })); } } @@ -2105,6 +2118,7 @@ where buffer_transaction: tx.clone(), is_initiator: false, contract_id, + reference_id: None }; std::mem::swap(&mut signed_channel.state, &mut state); @@ -2260,6 +2274,7 @@ where temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, punish_txid: signed_tx.txid(), + reference_id: None }); //TODO(tibo): should probably make sure the tx is confirmed somewhere before @@ -2272,15 +2287,15 @@ where true } TxType::CollaborativeClose => { + let (counter_payout, reference_id) = get_signed_channel_state!( + signed_channel, + CollaborativeCloseOffered, + counter_payout | reference_id + )?; if let Some(SignedChannelState::Established { signed_contract_id, .. }) = signed_channel.roll_back_state { - let counter_payout = get_signed_channel_state!( - signed_channel, - CollaborativeCloseOffered, - counter_payout - )?; let closed_contract = self.get_collaboratively_closed_contract( &signed_contract_id, *counter_payout, @@ -2289,10 +2304,12 @@ where self.store .update_contract(&Contract::Closed(closed_contract))?; } + let closed_channel = Channel::CollaborativelyClosed(ClosedChannel { counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: *reference_id }); self.chain_monitor .lock() @@ -2306,6 +2323,7 @@ where counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: None, }); self.chain_monitor .lock() @@ -2318,18 +2336,20 @@ where let contract_id = signed_channel.get_contract_id(); let closed_channel = { match &signed_channel.state { - SignedChannelState::Closing { is_initiator, .. } => { + SignedChannelState::Closing { is_initiator, reference_id, .. } => { if *is_initiator { Channel::Closed(ClosedChannel { counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: *reference_id }) } else { Channel::CounterClosed(ClosedChannel { counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: *reference_id }) } } @@ -2339,6 +2359,7 @@ where counter_party: signed_channel.counter_party, temporary_channel_id: signed_channel.temporary_channel_id, channel_id: signed_channel.channel_id, + reference_id: None }) } } @@ -2399,10 +2420,11 @@ where &self, channel_id: &DlcChannelId, sub_channel: (SubChannel, &ClosingSubChannel), + reference_id: Option ) -> Result<(), Error> { let channel = get_channel_in_state!(self, channel_id, Signed, None as Option)?; let is_initiator = sub_channel.1.is_initiator; - self.force_close_channel_internal(channel, Some(sub_channel), is_initiator) + self.force_close_channel_internal(channel, Some(sub_channel), is_initiator, reference_id) } fn force_close_channel_internal( @@ -2410,6 +2432,7 @@ where mut channel: SignedChannel, sub_channel: Option<(SubChannel, &ClosingSubChannel)>, is_initiator: bool, + reference_id: Option ) -> Result<(), Error> { match &channel.state { SignedChannelState::Established { @@ -2427,13 +2450,16 @@ where is_initiator, counter_buffer_adaptor_signature, buffer_transaction, + reference_id ) } SignedChannelState::RenewFinalized { buffer_transaction, offer_buffer_adaptor_signature, + reference_id, .. } => { + let reference_id = reference_id.clone(); warn!("Force closing renew finalized channel with id: {}", channel.channel_id.to_hex()); let offer_buffer_adaptor_signature = *offer_buffer_adaptor_signature; @@ -2444,6 +2470,7 @@ where is_initiator, offer_buffer_adaptor_signature, buffer_transaction, + reference_id ) } SignedChannelState::Settled { .. } => { @@ -2451,19 +2478,20 @@ where self.close_settled_channel(channel, sub_channel, is_initiator) } - SignedChannelState::SettledOffered { .. } - | SignedChannelState::SettledReceived { .. } - | SignedChannelState::SettledAccepted { .. } - | SignedChannelState::SettledConfirmed { .. } - | SignedChannelState::RenewOffered { .. } - | SignedChannelState::RenewAccepted { .. } - | SignedChannelState::RenewConfirmed { .. } - | SignedChannelState::CollaborativeCloseOffered { .. } => { + SignedChannelState::SettledOffered { reference_id, .. } + | SignedChannelState::SettledReceived { reference_id, .. } + | SignedChannelState::SettledAccepted { reference_id, .. } + | SignedChannelState::SettledConfirmed { reference_id, .. } + | SignedChannelState::RenewOffered { reference_id, .. } + | SignedChannelState::RenewAccepted { reference_id, .. } + | SignedChannelState::RenewConfirmed { reference_id, .. } + | SignedChannelState::CollaborativeCloseOffered { reference_id, .. } => { + let reference_id = *reference_id; channel.state = channel .roll_back_state .take() .expect("to have a rollback state"); - self.force_close_channel_internal(channel, sub_channel, is_initiator) + self.force_close_channel_internal(channel, sub_channel, is_initiator, reference_id) } SignedChannelState::Closing { .. } => Err(Error::InvalidState( "Channel is already closing.".to_string(), @@ -2479,6 +2507,7 @@ where is_initiator: bool, buffer_adaptor_signature: EcdsaAdaptorSignature, buffer_transaction: Transaction, + reference_id: Option ) -> Result<(), Error> { crate::channel_updater::initiate_unilateral_close_established_channel( &self.secp, @@ -2488,6 +2517,7 @@ where &self.wallet, sub_channel, is_initiator, + reference_id )?; let buffer_transaction = @@ -2573,6 +2603,7 @@ where counter_party: channel.counter_party, temporary_channel_id: channel.temporary_channel_id, channel_id, + reference_id: None }); Ok((closed_channel, contract)) diff --git a/dlc-manager/src/sub_channel_manager.rs b/dlc-manager/src/sub_channel_manager.rs index 6265a768..7230e9c9 100644 --- a/dlc-manager/src/sub_channel_manager.rs +++ b/dlc-manager/src/sub_channel_manager.rs @@ -405,6 +405,7 @@ where self.dlc_channel_manager.get_time(), temporary_channel_id, true, + None )?; // TODO(tibo): refactor properly. @@ -1060,7 +1061,7 @@ where if let Err(e) = self .dlc_channel_manager - .force_close_sub_channel(&dlc_channel_id, (closing.clone(), &state)) + .force_close_sub_channel(&dlc_channel_id, (closing.clone(), &state), None) { error!("Error force closing DLC subchannel {}", e); } @@ -1687,6 +1688,7 @@ 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 }; let (offered_channel, offered_contract) = @@ -1890,6 +1892,7 @@ where refund_signature: sub_channel_accept.refund_signature, negotiation_fields: None, payout_spk: sub_channel_accept.payout_spk.clone(), + reference_id: None }; let sub_channel_info = SubChannelSignVerifyInfo { @@ -2079,6 +2082,7 @@ where funding_signatures: FundingSignatures { funding_signatures: vec![], }, + reference_id: None }; let offer_revoke_params = accepted_sub_channel @@ -3309,6 +3313,7 @@ where is_offer_party: false, counter_party: dlc_channel.counter_party, cet_nsequence: CET_NSEQUENCE, + reference_id: None }; self.dlc_channel_manager .get_store() @@ -3384,6 +3389,7 @@ where counter_party: dlc_channel.counter_party, // TODO(tibo): use value from original offer cet_nsequence: CET_NSEQUENCE, + reference_id: None }; self.ln_channel_manager.set_funding_outpoint( channel_lock, @@ -3451,6 +3457,7 @@ where is_offer_party: false, counter_party: dlc_channel.counter_party, cet_nsequence: CET_NSEQUENCE, + reference_id: None }; self.dlc_channel_manager .get_store() @@ -3700,6 +3707,7 @@ where counter_party: channel.get_counter_party_id(), temporary_channel_id: channel.get_temporary_id(), channel_id: channel.get_id(), + reference_id: None }; let closed_channel = if counter_closed { Channel::CounterClosed(closed_channel_data) diff --git a/dlc-manager/tests/channel_execution_tests.rs b/dlc-manager/tests/channel_execution_tests.rs index 5c472b00..ba70ddef 100644 --- a/dlc-manager/tests/channel_execution_tests.rs +++ b/dlc-manager/tests/channel_execution_tests.rs @@ -485,6 +485,7 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" .parse() .unwrap(), + None ) .expect("Send offer error"); @@ -685,7 +686,7 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) { closer .lock() .unwrap() - .force_close_channel(&channel_id) + .force_close_channel(&channel_id, None) .expect("to be able to unilaterally close the channel."); } TestPath::BufferCheat => { @@ -815,7 +816,7 @@ fn close_established_channel( first .lock() .unwrap() - .force_close_channel(&channel_id) + .force_close_channel(&channel_id, None) .expect("to be able to unilaterally close."); assert_channel_state!(first, channel_id, Signed, Closing); @@ -873,13 +874,13 @@ fn cheat_punish( first .lock() .unwrap() - .force_close_channel(&channel_id) + .force_close_channel(&channel_id, None) .expect("the cheater to be able to close on established"); } else { first .lock() .unwrap() - .force_close_channel(&channel_id) + .force_close_channel(&channel_id, None) .expect("the cheater to be able to close on settled"); } @@ -902,7 +903,7 @@ fn settle_channel( let (settle_offer, _) = first .lock() .unwrap() - .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) + .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, None) .expect("to be able to offer a settlement of the contract."); first_send @@ -953,7 +954,7 @@ fn settle_reject( let (settle_offer, _) = first .lock() .unwrap() - .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) + .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, None) .expect("to be able to reject a settlement of the contract."); first_send @@ -999,13 +1000,13 @@ fn settle_race( let (settle_offer, _) = first .lock() .unwrap() - .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) + .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, None) .expect("to be able to offer a settlement of the contract."); let (settle_offer_2, _) = second .lock() .unwrap() - .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) + .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, None) .expect("to be able to offer a settlement of the contract."); first_send @@ -1059,7 +1060,7 @@ fn renew_channel( let (renew_offer, _) = first .lock() .unwrap() - .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input) + .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input, None) .expect("to be able to renew channel contract"); first_send @@ -1122,7 +1123,7 @@ fn renew_reject( let (renew_offer, _) = first .lock() .unwrap() - .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input) + .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input, None) .expect("to be able to renew channel contract"); first_send @@ -1166,7 +1167,7 @@ fn renew_race( let (renew_offer, _) = first .lock() .unwrap() - .renew_offer(&channel_id, test_utils::OFFER_COLLATERAL, contract_input) + .renew_offer(&channel_id, test_utils::OFFER_COLLATERAL, contract_input, None) .expect("to be able to renew channel contract"); let mut contract_input_2 = contract_input.clone(); @@ -1176,7 +1177,7 @@ fn renew_race( let (renew_offer_2, _) = second .lock() .unwrap() - .renew_offer(&channel_id, test_utils::OFFER_COLLATERAL, &contract_input_2) + .renew_offer(&channel_id, test_utils::OFFER_COLLATERAL, &contract_input_2, None) .expect("to be able to renew channel contract"); first_send @@ -1222,7 +1223,7 @@ fn collaborative_close( let close_offer = first .lock() .unwrap() - .offer_collaborative_close(&channel_id, test_utils::ACCEPT_COLLATERAL) + .offer_collaborative_close(&channel_id, test_utils::ACCEPT_COLLATERAL, None) .expect("to be able to propose a collaborative close"); first_send .send(Some(Message::Channel( @@ -1278,7 +1279,7 @@ fn renew_timeout( let (renew_offer, _) = first .lock() .unwrap() - .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input) + .renew_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, contract_input, None) .expect("to be able to offer a settlement of the contract."); first_send @@ -1359,7 +1360,7 @@ fn settle_timeout( let (settle_offer, _) = first .lock() .unwrap() - .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL) + .settle_offer(&channel_id, test_utils::ACCEPT_COLLATERAL, None) .expect("to be able to offer a settlement of the contract."); first_send diff --git a/dlc-manager/tests/ln_dlc_channel_execution_tests.rs b/dlc-manager/tests/ln_dlc_channel_execution_tests.rs index 6521d9b8..e4ca9ffb 100644 --- a/dlc-manager/tests/ln_dlc_channel_execution_tests.rs +++ b/dlc-manager/tests/ln_dlc_channel_execution_tests.rs @@ -2224,6 +2224,7 @@ fn settle(test_params: &LnDlcTestParams, channel_id: &DlcChannelId) { .settle_offer( channel_id, test_params.test_params.contract_input.accept_collateral, + None, ) .unwrap(); @@ -2274,6 +2275,7 @@ fn renew(test_params: &LnDlcTestParams, dlc_channel_id: &DlcChannelId) { dlc_channel_id, test_params.test_params.contract_input.accept_collateral, &test_params.test_params.contract_input, + None ) .unwrap(); diff --git a/dlc-messages/src/channel.rs b/dlc-messages/src/channel.rs index 4a664dc4..7f13a753 100644 --- a/dlc-messages/src/channel.rs +++ b/dlc-messages/src/channel.rs @@ -80,6 +80,8 @@ pub struct OfferChannel { pub refund_locktime: u32, /// The nSequence value to use for the CETs. pub cet_nsequence: u32, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(OfferChannel, { @@ -104,8 +106,9 @@ impl_dlc_writeable!(OfferChannel, { (fee_rate_per_vb, writeable), (cet_locktime, writeable), (refund_locktime, writeable), - (cet_nsequence, writeable) -}); + (cet_nsequence, writeable), + (reference_id, option)} +); impl OfferChannel { /// Returns whether the message satisfies validity requirements. @@ -191,6 +194,8 @@ pub struct AcceptChannel { pub refund_signature: Signature, /// Fields used to negotiate parameters with the counter party. pub negotiation_fields: Option, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(AcceptChannel, { @@ -209,7 +214,8 @@ impl_dlc_writeable!(AcceptChannel, { (cet_adaptor_signatures, writeable), (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (refund_signature, writeable), - (negotiation_fields, option) + (negotiation_fields, option), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -238,6 +244,8 @@ pub struct SignChannel { pub refund_signature: Signature, /// The signatures for the offer party's inputs. pub funding_signatures: FundingSignatures, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(SignChannel, { @@ -245,7 +253,8 @@ impl_dlc_writeable!(SignChannel, { (cet_adaptor_signatures, writeable), (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (refund_signature, writeable), - (funding_signatures, writeable) + (funding_signatures, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -272,13 +281,16 @@ pub struct SettleOffer { pub next_per_update_point: PublicKey, /// The timestamp when the message was created pub timestamp: u64, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(SettleOffer, { (channel_id, writeable), (counter_payout, writeable), (next_per_update_point, writeable), - (timestamp, writeable) + (timestamp, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -304,12 +316,15 @@ pub struct SettleAccept { /// The adaptor signature for the settle transaction generated by the sending /// party. pub settle_adaptor_signature: EcdsaAdaptorSignature, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(SettleAccept, { (channel_id, writeable), (next_per_update_point, writeable), - (settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}) + (settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -335,12 +350,15 @@ pub struct SettleConfirm { /// The adaptor signature for the settlement transaction generated by the /// sending party. pub settle_adaptor_signature: EcdsaAdaptorSignature, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(SettleConfirm, { (channel_id, writeable), (prev_per_update_secret, writeable), - (settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}) + (settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -363,11 +381,14 @@ pub struct SettleFinalize { /// The pre-image of the per update point used by the sending party during /// the establishment of the previous channel state. pub prev_per_update_secret: SecretKey, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(SettleFinalize, { (channel_id, writeable), - (prev_per_update_secret, writeable) + (prev_per_update_secret, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq)] @@ -401,6 +422,8 @@ pub struct RenewOffer { pub refund_locktime: u32, /// The nSequence value to use for the CETs. pub cet_nsequence: u32, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(RenewOffer, { @@ -410,7 +433,8 @@ impl_dlc_writeable!(RenewOffer, { (contract_info, writeable), (cet_locktime, writeable), (refund_locktime, writeable), - (cet_nsequence, writeable) + (cet_nsequence, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -437,13 +461,16 @@ pub struct RenewAccept { pub cet_adaptor_signatures: CetAdaptorSignatures, /// The refund signature generated by the offer party. pub refund_signature: Signature, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(RenewAccept, { (channel_id, writeable), (next_per_update_point, writeable), (cet_adaptor_signatures, writeable), - (refund_signature, writeable) + (refund_signature, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -470,13 +497,16 @@ pub struct RenewConfirm { pub cet_adaptor_signatures: CetAdaptorSignatures, /// The refund signature generated by the offer party. pub refund_signature: Signature, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(RenewConfirm, { (channel_id, writeable), (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), (cet_adaptor_signatures, writeable), - (refund_signature, writeable) + (refund_signature, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -502,12 +532,15 @@ pub struct RenewFinalize { /// The adaptor signature for the buffer transaction generated by the accept /// party. pub buffer_adaptor_signature: EcdsaAdaptorSignature, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(RenewFinalize, { (channel_id, writeable), (per_update_secret, writeable), - (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}) + (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -530,11 +563,14 @@ pub struct RenewRevoke { /// The pre image of the per update point used by the sending party to setup /// the previous channel state. pub per_update_secret: SecretKey, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(RenewRevoke, { (channel_id, writeable), - (per_update_secret, writeable) + (per_update_secret, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -558,12 +594,15 @@ pub struct CollaborativeCloseOffer { pub counter_payout: u64, /// The signature of the sending party for the closing transaction. pub close_signature: Signature, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } impl_dlc_writeable!(CollaborativeCloseOffer, { (channel_id, writeable), (counter_payout, writeable), - (close_signature, writeable) + (close_signature, writeable), + (reference_id, option) }); #[derive(Clone, Debug, PartialEq, Eq)] @@ -586,6 +625,8 @@ pub struct Reject { pub channel_id: [u8; 32], /// The timestamp when the message was created pub timestamp: u64, + /// The reference id set by the api user. + pub reference_id: Option<[u8; 32]> } -impl_dlc_writeable!(Reject, { (channel_id, writeable), (timestamp, writeable) }); +impl_dlc_writeable!(Reject, { (channel_id, writeable), (timestamp, writeable), (reference_id, option) }); diff --git a/sample/src/cli.rs b/sample/src/cli.rs index 6b9afde7..2ef21b02 100644 --- a/sample/src/cli.rs +++ b/sample/src/cli.rs @@ -208,7 +208,7 @@ pub(crate) async fn poll_for_user_input( manager_clone .lock() .unwrap() - .offer_channel(&contract_input, pubkey) + .offer_channel(&contract_input, pubkey, None) .expect("Error sending offer channel"), )) } @@ -356,7 +356,7 @@ pub(crate) async fn poll_for_user_input( let (msg, node_id) = dlc_manager .lock() .unwrap() - .settle_offer(&channel_id, counter_payout) + .settle_offer(&channel_id, counter_payout, None) .expect("Error getting settle offer message."); dlc_message_handler.send_message( node_id, @@ -423,7 +423,7 @@ pub(crate) async fn poll_for_user_input( manager_clone .lock() .unwrap() - .renew_offer(&channel_id, counter_payout, &contract_input) + .renew_offer(&channel_id, counter_payout, &contract_input, None) .expect("Error sending offer") }) .await