From 86e2000c67cb48aace852a76fd39b37f6a09aa0d Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Wed, 20 Dec 2023 17:33:31 +1100 Subject: [PATCH] Update to rust-lightning:0.0.117 As part of this update `rust-lightning` introduced a `ChannelId` struct, which motivated us to also introduce the `DlcChannelId` and `SubChannelId` structs. --- 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 | 7 +- dlc-manager/src/channel/mod.rs | 47 ++-- dlc-manager/src/channel/offered_channel.rs | 8 +- dlc-manager/src/channel/signed_channel.rs | 14 +- dlc-manager/src/channel_updater.rs | 25 +- dlc-manager/src/contract/signed_contract.rs | 6 +- dlc-manager/src/contract_updater.rs | 8 +- dlc-manager/src/lib.rs | 16 +- dlc-manager/src/manager.rs | 71 +++--- dlc-manager/src/sub_channel_manager.rs | 238 ++++++++++-------- dlc-manager/src/subchannel/mod.rs | 102 ++++---- dlc-manager/test_inputs/offer_channel.json | 2 +- dlc-manager/tests/channel_execution_tests.rs | 30 ++- dlc-manager/tests/custom_signer.rs | 15 ++ .../tests/ln_dlc_channel_execution_tests.rs | 61 +++-- dlc-manager/tests/manager_execution_tests.rs | 4 +- dlc-manager/tests/test_utils.rs | 57 +++++ dlc-messages/Cargo.toml | 4 +- dlc-messages/src/channel.rs | 125 ++------- dlc-messages/src/lib.rs | 27 +- dlc-messages/src/sub_channel.rs | 21 +- dlc-sled-storage-provider/Cargo.toml | 3 +- dlc-sled-storage-provider/src/lib.rs | 22 +- dlc/Cargo.toml | 3 +- dlc/src/ids.rs | 113 +++++++++ dlc/src/lib.rs | 8 + {dlc-messages => dlc}/src/serde_utils.rs | 3 + electrs-blockchain-provider/Cargo.toml | 4 +- fuzz/Cargo.toml | 2 +- mocks/Cargo.toml | 2 +- mocks/src/memory_storage_provider.rs | 32 ++- sample/Cargo.toml | 4 +- sample/src/cli.rs | 30 ++- simple-wallet/Cargo.toml | 2 +- 38 files changed, 657 insertions(+), 482 deletions(-) create mode 100644 dlc/src/ids.rs rename {dlc-messages => dlc}/src/serde_utils.rs (97%) diff --git a/Cargo.toml b/Cargo.toml index a5198501..25de8abd 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 = "15c4ff6" } +lightning-net-tokio = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "15c4ff6" } +lightning-persister = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "15c4ff6" } 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..f4e0612e 100644 --- a/dlc-manager/src/channel/accepted_channel.rs +++ b/dlc-manager/src/channel/accepted_channel.rs @@ -1,10 +1,11 @@ //! # Structure and methods for channels that have been accepted. use bitcoin::{Script, Transaction}; +use dlc::DlcChannelId; 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}; use super::party_points::PartyBasePoints; @@ -31,9 +32,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..cd803679 100644 --- a/dlc-manager/src/channel/mod.rs +++ b/dlc-manager/src/channel/mod.rs @@ -1,10 +1,11 @@ //! # Module containing structures and methods for working with DLC channels. use bitcoin::{hashes::Hash, Transaction, Txid}; +use dlc::DlcChannelId; use dlc_messages::channel::{AcceptChannel, SignChannel}; use secp256k1_zkp::PublicKey; -use crate::{ChannelId, ContractId}; +use crate::ContractId; use self::{ accepted_channel::AcceptedChannel, offered_channel::OfferedChannel, @@ -95,8 +96,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 +111,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 +125,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 +146,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 +158,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 +182,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,11 +213,11 @@ 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); - data.extend_from_slice(&channel_id); + data.extend_from_slice(&channel_id.inner()); data.extend_from_slice(&channel_update_idx.to_be_bytes()); bitcoin::hashes::sha256::Hash::hash(&data).into_inner() } diff --git a/dlc-manager/src/channel/offered_channel.rs b/dlc-manager/src/channel/offered_channel.rs index b13a8dad..7f151b8e 100644 --- a/dlc-manager/src/channel/offered_channel.rs +++ b/dlc-manager/src/channel/offered_channel.rs @@ -1,14 +1,14 @@ //! # A channel is offered when an offer was made or received. This module contains //! the model for it and method for working with it. +use dlc::DlcChannelId; use dlc::PartyParams; 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, ChannelId, ContractId, + error::Error, ContractId, }; 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..94f42efc 100644 --- a/dlc-manager/src/channel/signed_channel.rs +++ b/dlc-manager/src/channel/signed_channel.rs @@ -3,11 +3,13 @@ //! the possible states in which it can be as well as methods to work with it. use bitcoin::{Script, Transaction}; +use dlc::DlcChannelId; use dlc::PartyParams; +use dlc::SubChannelId; use lightning::ln::chan_utils::CounterpartyCommitmentSecrets; use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature, PublicKey}; -use crate::{ChannelId, ContractId}; +use crate::ContractId; use super::party_points::PartyBasePoints; @@ -346,12 +348,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. @@ -385,7 +387,7 @@ pub struct SignedChannel { /// The current fee rate to be used to create transactions. pub fee_rate_per_vb: u64, /// Whether this channel is embedded within a Lightning Network channel. - pub sub_channel_id: Option, + pub sub_channel_id: Option, } impl SignedChannel { diff --git a/dlc-manager/src/channel_updater.rs b/dlc-manager/src/channel_updater.rs index d8c33cd3..108b90b9 100644 --- a/dlc-manager/src/channel_updater.rs +++ b/dlc-manager/src/channel_updater.rs @@ -22,13 +22,14 @@ use crate::{ }, error::Error, subchannel::{ClosingSubChannel, SubChannel}, - Blockchain, ChannelId, ContractId, Signer, Time, Wallet, + Blockchain, ContractId, Signer, Time, Wallet, }; use bitcoin::{OutPoint, Script, Sequence, Transaction}; use dlc::{ channel::{get_tx_adaptor_signature, verify_tx_adaptor_signature, DlcChannelTransactions}, PartyParams, }; +use dlc::{DlcChannelId, SubChannelId}; use dlc_messages::{ channel::{ AcceptChannel, CollaborativeCloseOffer, Reject, RenewAccept, RenewConfirm, RenewFinalize, @@ -81,7 +82,7 @@ pub(crate) struct SubChannelSignVerifyInfo { pub funding_info: FundingInfo, pub own_adaptor_sk: SecretKey, pub counter_adaptor_pk: PublicKey, - pub sub_channel_id: ChannelId, + pub sub_channel_id: SubChannelId, } pub(crate) struct SubChannelSignInfo { @@ -92,7 +93,7 @@ pub(crate) struct SubChannelSignInfo { pub(crate) struct SubChannelVerifyInfo { pub funding_info: FundingInfo, pub counter_adaptor_pk: PublicKey, - pub sub_channel_id: ChannelId, + pub sub_channel_id: SubChannelId, } /// Creates an [`OfferedChannel`] and an associated [`OfferedContract`] using @@ -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 @@ -321,8 +322,9 @@ where let channel_id = crate::utils::compute_id( dlc_transactions.fund.txid(), funding_vout as u16, - &offered_channel.temporary_channel_id, + &offered_channel.temporary_channel_id.inner(), ); + let channel_id = DlcChannelId::from_bytes(channel_id); let buffer_adaptor_signature = get_tx_adaptor_signature( secp, @@ -534,8 +536,9 @@ where let channel_id = crate::utils::compute_id( dlc_transactions.fund.txid(), fund_output_index as u16, - &offered_channel.temporary_channel_id, + &offered_channel.temporary_channel_id.inner(), ); + let channel_id = DlcChannelId::from_bytes(channel_id); let accept_cet_adaptor_signatures: Vec<_> = (&accept_channel.cet_adaptor_signatures).into(); @@ -581,7 +584,7 @@ where chain_monitor.lock().unwrap().add_tx( buffer_transaction.txid(), ChannelInfo { - channel_id, + channel_id: channel_id.inner(), tx_type: TxType::BufferTx, }, ); @@ -740,7 +743,7 @@ where chain_monitor.lock().unwrap().add_tx( accepted_channel.buffer_transaction.txid(), ChannelInfo { - channel_id: accepted_channel.channel_id, + channel_id: accepted_channel.channel_id.inner(), tx_type: TxType::BufferTx, }, ); @@ -982,7 +985,7 @@ where chain_monitor.lock().unwrap().add_tx( settle_tx.txid(), ChannelInfo { - channel_id: channel.channel_id, + channel_id: channel.channel_id.inner(), tx_type: TxType::SettleTx, }, ); @@ -1115,7 +1118,7 @@ where chain_monitor.lock().unwrap().add_tx( settle_tx.txid(), ChannelInfo { - channel_id: channel.channel_id, + channel_id: channel.channel_id.inner(), tx_type: TxType::SettleTx, }, ); @@ -1960,7 +1963,7 @@ where chain_monitor.lock().unwrap().add_tx( buffer_transaction.txid(), ChannelInfo { - channel_id: signed_channel.channel_id, + channel_id: signed_channel.channel_id.inner(), tx_type: TxType::BufferTx, }, ); diff --git a/dlc-manager/src/contract/signed_contract.rs b/dlc-manager/src/contract/signed_contract.rs index f392a892..b341ccc3 100644 --- a/dlc-manager/src/contract/signed_contract.rs +++ b/dlc-manager/src/contract/signed_contract.rs @@ -1,9 +1,9 @@ //! #SignedContract use crate::conversion_utils::PROTOCOL_VERSION; -use crate::ChannelId; use super::accepted_contract::AcceptedContract; +use dlc::DlcChannelId; use dlc_messages::CetAdaptorSignature; use dlc_messages::CetAdaptorSignatures; use dlc_messages::FundingSignatures; @@ -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..5e74be53 100644 --- a/dlc-manager/src/contract_updater.rs +++ b/dlc-manager/src/contract_updater.rs @@ -3,7 +3,7 @@ use std::ops::Deref; use bitcoin::{consensus::Decodable, Script, Transaction, Witness}; -use dlc::{DlcTransactions, PartyParams}; +use dlc::{DlcChannelId, DlcTransactions, PartyParams}; use dlc_messages::{ oracle_msgs::{OracleAnnouncement, OracleAttestation}, AcceptDlc, FundingSignature, FundingSignatures, OfferDlc, SignDlc, WitnessElement, @@ -20,7 +20,7 @@ use crate::{ }, conversion_utils::get_tx_input_infos, error::Error, - Blockchain, ChannelId, Signer, Time, Wallet, + Blockchain, 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