From 61e33c81ad5ddbd2211fcf805f221a8604d6e051 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Tue, 21 Nov 2023 20:18:16 +1100 Subject: [PATCH] Update to rust-lightning:0.0.117 The highlight in terms of code changes in `rust-dlc` is: Use new LDK Channel ID struct ============================= Start to use newly introduced `ChannelId` struct. With a stronger type we are less likely to mix up LN `ChannelId`s, DLC channel IDs and contract IDs. To that end, we've renamed the old `ChannelId` type alias to `DlcChannelId` since it is now only supposed to be used for (temporary or final) DLC channel IDs. We should consider introducing a struct at some point. To ensure backwards-compatibility we're still serialising the LN channel IDs into bytes, but we now have to use the `serialize_with` attribute since LDK's `ChannelId` doesn't implement serde itself. The same applies to deserialisation. - --- Cargo.toml | 6 +- bitcoin-rpc-provider/Cargo.toml | 2 +- dlc-manager/Cargo.toml | 6 +- dlc-manager/src/chain_monitor.rs | 9 +- dlc-manager/src/channel/accepted_channel.rs | 6 +- dlc-manager/src/channel/mod.rs | 44 +++---- dlc-manager/src/channel/offered_channel.rs | 6 +- dlc-manager/src/channel/signed_channel.rs | 12 +- dlc-manager/src/channel_updater.rs | 9 +- dlc-manager/src/contract/signed_contract.rs | 6 +- dlc-manager/src/contract_updater.rs | 6 +- dlc-manager/src/lib.rs | 15 +-- dlc-manager/src/manager.rs | 35 +++--- dlc-manager/src/sub_channel_manager.rs | 117 ++++++++++++------ dlc-manager/src/subchannel/mod.rs | 70 ++++++----- dlc-manager/tests/channel_execution_tests.rs | 29 +++-- dlc-manager/tests/custom_signer.rs | 15 +++ .../tests/ln_dlc_channel_execution_tests.rs | 45 ++++--- dlc-messages/Cargo.toml | 2 +- dlc-messages/src/serde_utils.rs | 20 +++ dlc-messages/src/sub_channel.rs | 91 ++++++++++++-- dlc-sled-storage-provider/Cargo.toml | 2 +- dlc-sled-storage-provider/src/lib.rs | 16 ++- electrs-blockchain-provider/Cargo.toml | 4 +- fuzz/Cargo.toml | 2 +- mocks/Cargo.toml | 2 +- mocks/src/memory_storage_provider.rs | 16 ++- sample/Cargo.toml | 4 +- sample/src/cli.rs | 8 +- simple-wallet/Cargo.toml | 2 +- 30 files changed, 383 insertions(+), 224 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a5198501..b6ec7fe6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,6 @@ members = [ resolver = "2" [patch.crates-io] -lightning = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "a57281b" } -lightning-net-tokio = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "a57281b" } -lightning-persister = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "a57281b" } +lightning = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "039157ce" } +lightning-net-tokio = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "039157ce" } +lightning-persister = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "039157ce" } diff --git a/bitcoin-rpc-provider/Cargo.toml b/bitcoin-rpc-provider/Cargo.toml index c2214c58..2feb63ef 100644 --- a/bitcoin-rpc-provider/Cargo.toml +++ b/bitcoin-rpc-provider/Cargo.toml @@ -9,7 +9,7 @@ bitcoin = {version = "0.29.2"} bitcoincore-rpc = {version = "0.16.0"} bitcoincore-rpc-json = {version = "0.16.0"} dlc-manager = {path = "../dlc-manager"} -lightning = {version = "0.0.116"} +lightning = {version = "0.0.117"} log = "0.4.14" rust-bitcoin-coin-selection = { rev = "23a6bf85", git = "https://github.com/p2pderivatives/rust-bitcoin-coin-selection", features = ["rand"]} simple-wallet = {path = "../simple-wallet"} diff --git a/dlc-manager/Cargo.toml b/dlc-manager/Cargo.toml index 08343a38..6c6b78fc 100644 --- a/dlc-manager/Cargo.toml +++ b/dlc-manager/Cargo.toml @@ -19,7 +19,7 @@ bitcoin = {version = "0.29.2"} dlc = {version = "0.4.0", path = "../dlc"} dlc-messages = {version = "0.4.0", path = "../dlc-messages"} dlc-trie = {version = "0.4.0", path = "../dlc-trie"} -lightning = {version = "0.0.116"} +lightning = {version = "0.0.117"} log = "0.4.14" rand_chacha = {version = "0.3.1", optional = true} secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std"]} @@ -37,8 +37,8 @@ dlc-manager = {path = ".", features = ["use-serde"]} dlc-messages = {path = "../dlc-messages", features = ["serde"]} electrs-blockchain-provider = {path = "../electrs-blockchain-provider"} env_logger = "0.9.1" -lightning-persister = {version = "0.0.116"} -lightning-transaction-sync = {version = "0.0.116", features=["esplora-blocking"]} +lightning-persister = {version = "0.0.117"} +lightning-transaction-sync = {version = "0.0.117", features=["esplora-blocking"]} mocks = {path = "../mocks"} secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std", "global-context", "use-serde"]} serde = "1.0" diff --git a/dlc-manager/src/chain_monitor.rs b/dlc-manager/src/chain_monitor.rs index eb238312..796bc9c5 100644 --- a/dlc-manager/src/chain_monitor.rs +++ b/dlc-manager/src/chain_monitor.rs @@ -11,8 +11,6 @@ use lightning::ln::msgs::DecodeError; use lightning::util::ser::{Readable, Writeable, Writer}; use secp256k1_zkp::EcdsaAdaptorSignature; -use crate::ChannelId; - /// A `ChainMonitor` keeps a list of transaction ids to watch for in the blockchain, /// and some associated information used to apply an action when the id is seen. #[derive(Debug, PartialEq, Eq)] @@ -26,7 +24,8 @@ impl_dlc_writeable!(ChainMonitor, { (watched_tx, { cb_writeable, write_hash_map, #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct ChannelInfo { - pub channel_id: ChannelId, + /// The identifier for _either_ a Lightning channel or a DLC channel. + pub channel_id: [u8; 32], pub tx_type: TxType, } @@ -111,7 +110,7 @@ impl ChainMonitor { .insert(outpoint, WatchState::new(channel_info)); } - pub(crate) fn cleanup_channel(&mut self, channel_id: ChannelId) { + pub(crate) fn cleanup_channel(&mut self, channel_id: [u8; 32]) { log::debug!("Cleaning up data related to channel {channel_id:?}"); self.watched_tx @@ -220,7 +219,7 @@ impl WatchState { } } - fn channel_id(&self) -> ChannelId { + fn channel_id(&self) -> [u8; 32] { self.channel_info().channel_id } } diff --git a/dlc-manager/src/channel/accepted_channel.rs b/dlc-manager/src/channel/accepted_channel.rs index 2ed3ade1..e2cf30d6 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, ChannelId, ContractId}; +use crate::{contract::accepted_contract::AcceptedContract, ContractId, DlcChannelId}; use super::party_points::PartyBasePoints; @@ -31,9 +31,9 @@ pub struct AcceptedChannel { /// The script pubkey of the buffer transaction output. pub buffer_script_pubkey: Script, /// The temporary id of the channel. - pub temporary_channel_id: ChannelId, + pub temporary_channel_id: DlcChannelId, /// The actual id of the channel. - pub channel_id: ChannelId, + pub channel_id: DlcChannelId, /// The image of the per update seed used by the accept party. pub accept_per_update_seed: PublicKey, /// The accept party adaptor signature for the buffer transaction. diff --git a/dlc-manager/src/channel/mod.rs b/dlc-manager/src/channel/mod.rs index d05155d5..db71124d 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::{ChannelId, ContractId}; +use crate::{ContractId, DlcChannelId}; use self::{ accepted_channel::AcceptedChannel, offered_channel::OfferedChannel, @@ -95,8 +95,8 @@ impl Channel { pub struct FailedAccept { /// The [`secp256k1_zkp::PublicKey`] of the counter party. pub counter_party: PublicKey, - /// The temporary [`crate::ChannelId`] of the channel. - pub temporary_channel_id: ChannelId, + /// The temporary [`DlcChannelId`] of the channel. + pub temporary_channel_id: DlcChannelId, /// An message describing the error encountered while validating the /// [`dlc_messages::channel::AcceptChannel`] message. pub error_message: String, @@ -110,8 +110,8 @@ pub struct FailedAccept { pub struct FailedSign { /// The [`secp256k1_zkp::PublicKey`] of the counter party. pub counter_party: PublicKey, - /// The [`crate::ChannelId`] of the channel. - pub channel_id: ChannelId, + /// The [`DlcChannelId`] of the channel. + pub channel_id: DlcChannelId, /// An message describing the error encountered while validating the /// [`dlc_messages::channel::SignChannel`] message. pub error_message: String, @@ -124,10 +124,10 @@ pub struct FailedSign { pub struct ClosingChannel { /// The [`secp256k1_zkp::PublicKey`] of the counter party. pub counter_party: PublicKey, - /// The temporary [`crate::ChannelId`] of the channel. - pub temporary_channel_id: ChannelId, - /// The [`crate::ChannelId`] for the channel. - pub channel_id: ChannelId, + /// The temporary [`DlcChannelId`] of the channel. + pub temporary_channel_id: DlcChannelId, + /// The [`DlcChannelId`] for the channel. + pub channel_id: DlcChannelId, /// The previous state the channel was before being closed, if that state was the `Signed` one, /// otherwise is `None`. pub rollback_state: Option, @@ -145,10 +145,10 @@ pub struct ClosingChannel { pub struct ClosedChannel { /// The [`secp256k1_zkp::PublicKey`] of the counter party. pub counter_party: PublicKey, - /// The temporary [`crate::ChannelId`] of the channel. - pub temporary_channel_id: ChannelId, - /// The [`crate::ChannelId`] for the channel. - pub channel_id: ChannelId, + /// The temporary [`DlcChannelId`] of the channel. + pub temporary_channel_id: DlcChannelId, + /// The [`DlcChannelId`] for the channel. + pub channel_id: DlcChannelId, } #[derive(Clone)] @@ -157,17 +157,17 @@ pub struct ClosedChannel { pub struct ClosedPunishedChannel { /// The [`secp256k1_zkp::PublicKey`] of the counter party. pub counter_party: PublicKey, - /// The temporary [`crate::ChannelId`] of the channel. - pub temporary_channel_id: ChannelId, - /// The [`crate::ChannelId`] for the channel. - pub channel_id: ChannelId, + /// The temporary [`DlcChannelId`] of the channel. + pub temporary_channel_id: DlcChannelId, + /// The [`DlcChannelId`] for the channel. + pub channel_id: DlcChannelId, /// The transaction id of the punishment transaction that was broadcast. pub punish_txid: Txid, } impl Channel { - /// Returns the temporary [`crate::ChannelId`] for the channel. - pub fn get_temporary_id(&self) -> ChannelId { + /// Returns the temporary [`DlcChannelId`] for the channel. + pub fn get_temporary_id(&self) -> DlcChannelId { match self { Channel::Offered(o) => o.temporary_channel_id, Channel::Accepted(a) => a.temporary_channel_id, @@ -181,8 +181,8 @@ impl Channel { } } - /// Returns the [`crate::ChannelId`] for the channel. - pub fn get_id(&self) -> ChannelId { + /// Returns the [`DlcChannelId`] for the channel. + pub fn get_id(&self) -> DlcChannelId { match self { Channel::Offered(o) => o.temporary_channel_id, Channel::Accepted(a) => a.channel_id, @@ -212,7 +212,7 @@ impl Channel { /// Generate a temporary contract id for a DLC based on the channel id and the update index of the DLC channel. pub fn generate_temporary_contract_id( - channel_id: ChannelId, + channel_id: DlcChannelId, channel_update_idx: u64, ) -> ContractId { let mut data = Vec::with_capacity(65); diff --git a/dlc-manager/src/channel/offered_channel.rs b/dlc-manager/src/channel/offered_channel.rs index b13a8dad..bda3c9ed 100644 --- a/dlc-manager/src/channel/offered_channel.rs +++ b/dlc-manager/src/channel/offered_channel.rs @@ -8,7 +8,7 @@ use secp256k1_zkp::PublicKey; use crate::{ contract::offered_contract::OfferedContract, conversion_utils::get_tx_input_infos, - error::Error, ChannelId, ContractId, + error::Error, ContractId, DlcChannelId, }; use super::party_points::PartyBasePoints; @@ -25,8 +25,8 @@ pub struct OfferedChannel { /// The temporary [`crate::ContractId`] of the contract that was offered for /// channel setup. pub offered_contract_id: ContractId, - /// The temporary [`crate::ChannelId`] of the channel. - pub temporary_channel_id: ChannelId, + /// The temporary [`DlcChannelId`] of the channel. + pub temporary_channel_id: DlcChannelId, /// The set of base points that the offer party will use during the lifetime /// of the channel. pub party_points: PartyBasePoints, diff --git a/dlc-manager/src/channel/signed_channel.rs b/dlc-manager/src/channel/signed_channel.rs index e6b8c7dc..66922c10 100644 --- a/dlc-manager/src/channel/signed_channel.rs +++ b/dlc-manager/src/channel/signed_channel.rs @@ -4,10 +4,10 @@ use bitcoin::{Script, Transaction}; use dlc::PartyParams; -use lightning::ln::chan_utils::CounterpartyCommitmentSecrets; +use lightning::ln::{chan_utils::CounterpartyCommitmentSecrets, ChannelId}; use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature, PublicKey}; -use crate::{ChannelId, ContractId}; +use crate::{ContractId, DlcChannelId}; use super::party_points::PartyBasePoints; @@ -346,12 +346,12 @@ impl SignedChannelState { /// A channel that had a successful setup. #[derive(Clone)] pub struct SignedChannel { - /// The [`crate::ChannelId`] for the channel. - pub channel_id: ChannelId, + /// The [`DlcChannelId`] for the channel. + pub channel_id: DlcChannelId, /// The [`secp256k1_zkp::PublicKey`] of the counter party's node. pub counter_party: PublicKey, - /// The temporary [`crate::ChannelId`] for the channel. - pub temporary_channel_id: ChannelId, + /// The temporary [`DlcChannelId`] for the channel. + pub temporary_channel_id: DlcChannelId, /// The contract setup parameters for the local party. pub own_params: PartyParams, /// The base points used for channel updates and revocation by the local party. diff --git a/dlc-manager/src/channel_updater.rs b/dlc-manager/src/channel_updater.rs index d8c33cd3..7c25c65a 100644 --- a/dlc-manager/src/channel_updater.rs +++ b/dlc-manager/src/channel_updater.rs @@ -22,7 +22,7 @@ use crate::{ }, error::Error, subchannel::{ClosingSubChannel, SubChannel}, - Blockchain, ChannelId, ContractId, Signer, Time, Wallet, + Blockchain, ContractId, DlcChannelId, Signer, Time, Wallet, }; use bitcoin::{OutPoint, Script, Sequence, Transaction}; use dlc::{ @@ -38,8 +38,9 @@ use dlc_messages::{ oracle_msgs::{OracleAnnouncement, OracleAttestation}, FundingSignatures, }; -use lightning::ln::chan_utils::{ - build_commitment_secret, derive_private_key, CounterpartyCommitmentSecrets, +use lightning::ln::{ + chan_utils::{build_commitment_secret, derive_private_key, CounterpartyCommitmentSecrets}, + ChannelId, }; use secp256k1_zkp::{All, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey, Signing}; @@ -107,7 +108,7 @@ pub fn offer_channel( wallet: &W, blockchain: &B, time: &T, - temporary_channel_id: ContractId, + temporary_channel_id: DlcChannelId, is_sub_channel: bool, ) -> Result<(OfferedChannel, OfferedContract), Error> where diff --git a/dlc-manager/src/contract/signed_contract.rs b/dlc-manager/src/contract/signed_contract.rs index f392a892..7711c4b7 100644 --- a/dlc-manager/src/contract/signed_contract.rs +++ b/dlc-manager/src/contract/signed_contract.rs @@ -1,7 +1,7 @@ //! #SignedContract use crate::conversion_utils::PROTOCOL_VERSION; -use crate::ChannelId; +use crate::DlcChannelId; use super::accepted_contract::AcceptedContract; use dlc_messages::CetAdaptorSignature; @@ -22,8 +22,8 @@ pub struct SignedContract { pub offer_refund_signature: Signature, /// The signatures for the funding inputs of the offering party. pub funding_signatures: FundingSignatures, - /// The [`ChannelId`] to which the contract was associated if any. - pub channel_id: Option, + /// The [`DlcChannelId`] to which the contract was associated if any. + pub channel_id: Option, } impl SignedContract { diff --git a/dlc-manager/src/contract_updater.rs b/dlc-manager/src/contract_updater.rs index f4f14d41..3cf44b0f 100644 --- a/dlc-manager/src/contract_updater.rs +++ b/dlc-manager/src/contract_updater.rs @@ -20,7 +20,7 @@ use crate::{ }, conversion_utils::get_tx_input_infos, error::Error, - Blockchain, ChannelId, Signer, Time, Wallet, + Blockchain, DlcChannelId, Signer, Time, Wallet, }; /// Creates an [`OfferedContract`] and [`OfferDlc`] message from the provided @@ -300,7 +300,7 @@ pub(crate) fn verify_accepted_and_sign_contract_internal( input_script_pubkey: Option