Skip to content

Commit

Permalink
Update to rust-lightning:0.0.117
Browse files Browse the repository at this point in the history
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.

-
  • Loading branch information
luckysori committed Nov 28, 2023
1 parent 0f9ce7c commit 61e33c8
Show file tree
Hide file tree
Showing 30 changed files with 383 additions and 224 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion bitcoin-rpc-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
6 changes: 3 additions & 3 deletions dlc-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand All @@ -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"
Expand Down
9 changes: 4 additions & 5 deletions dlc-manager/src/chain_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -220,7 +219,7 @@ impl WatchState {
}
}

fn channel_id(&self) -> ChannelId {
fn channel_id(&self) -> [u8; 32] {
self.channel_info().channel_id
}
}
6 changes: 3 additions & 3 deletions dlc-manager/src/channel/accepted_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitcoin::{Script, Transaction};
use dlc_messages::channel::AcceptChannel;
use secp256k1_zkp::{EcdsaAdaptorSignature, PublicKey};

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

use super::party_points::PartyBasePoints;

Expand All @@ -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.
Expand Down
44 changes: 22 additions & 22 deletions dlc-manager/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitcoin::{hashes::Hash, Transaction, Txid};
use dlc_messages::channel::{AcceptChannel, SignChannel};
use secp256k1_zkp::PublicKey;

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

use self::{
accepted_channel::AcceptedChannel, offered_channel::OfferedChannel,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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<SignedChannel>,
Expand All @@ -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)]
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions dlc-manager/src/channel/offered_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions dlc-manager/src/channel/signed_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions dlc-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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};

Expand Down Expand Up @@ -107,7 +108,7 @@ pub fn offer_channel<C: Signing, W: Deref, B: Deref, T: Deref>(
wallet: &W,
blockchain: &B,
time: &T,
temporary_channel_id: ContractId,
temporary_channel_id: DlcChannelId,
is_sub_channel: bool,
) -> Result<(OfferedChannel, OfferedContract), Error>
where
Expand Down
6 changes: 3 additions & 3 deletions dlc-manager/src/contract/signed_contract.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ChannelId>,
/// The [`DlcChannelId`] to which the contract was associated if any.
pub channel_id: Option<DlcChannelId>,
}

impl SignedContract {
Expand Down
6 changes: 3 additions & 3 deletions dlc-manager/src/contract_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -300,7 +300,7 @@ pub(crate) fn verify_accepted_and_sign_contract_internal<S: Deref>(
input_script_pubkey: Option<Script>,
counter_adaptor_pk: Option<PublicKey>,
dlc_transactions: &DlcTransactions,
channel_id: Option<ChannelId>,
channel_id: Option<DlcChannelId>,
) -> Result<(SignedContract, Vec<EcdsaAdaptorSignature>), Error>
where
S::Target: Signer,
Expand Down Expand Up @@ -524,7 +524,7 @@ pub(crate) fn verify_signed_contract_internal<S: Deref>(
input_script_pubkey: Option<Script>,
counter_adaptor_pk: Option<PublicKey>,
signer: &S,
channel_id: Option<ChannelId>,
channel_id: Option<DlcChannelId>,
) -> Result<(SignedContract, Transaction), Error>
where
S::Target: Signer,
Expand Down
15 changes: 8 additions & 7 deletions dlc-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use dlc_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation};
use dlc_messages::ser_impls::{read_address, write_address};
use error::Error;
use lightning::ln::msgs::DecodeError;
use lightning::ln::ChannelId;
use lightning::util::ser::{Readable, Writeable, Writer};
use secp256k1_zkp::XOnlyPublicKey;
use secp256k1_zkp::{PublicKey, SecretKey};
Expand All @@ -60,8 +61,8 @@ use subchannel::SubChannel;
/// Type alias for a contract id.
pub type ContractId = [u8; 32];

/// Type alias for a channel id.
pub type ChannelId = [u8; 32];
/// Type alias for a DLC channel ID.
pub type DlcChannelId = [u8; 32];

/// Time trait to provide current unix time. Mainly defined to facilitate testing.
pub trait Time {
Expand Down Expand Up @@ -154,10 +155,10 @@ pub trait Storage {
/// Update the state of the channel and optionally its associated contract
/// atomically.
fn upsert_channel(&self, channel: Channel, contract: Option<Contract>) -> Result<(), Error>;
/// Delete the channel with given [`ChannelId`] if any.
fn delete_channel(&self, channel_id: &ChannelId) -> Result<(), Error>;
/// Returns the channel with given [`ChannelId`] if any.
fn get_channel(&self, channel_id: &ChannelId) -> Result<Option<Channel>, Error>;
/// Delete the channel with given [`DlcChannelId`] if any.
fn delete_channel(&self, channel_id: &DlcChannelId) -> Result<(), Error>;
/// Returns the channel with given [`DlcChannelId`] if any.
fn get_channel(&self, channel_id: &DlcChannelId) -> Result<Option<Channel>, Error>;
/// Returns the set of [`SignedChannel`] in the store. Returns only the one
/// with matching `channel_state` if set.
fn get_signed_channels(
Expand All @@ -172,7 +173,7 @@ pub trait Storage {
fn get_chain_monitor(&self) -> Result<Option<ChainMonitor>, Error>;
/// Creates or updates a [`SubChannel`].
fn upsert_sub_channel(&self, subchannel: &SubChannel) -> Result<(), Error>;
/// Returns the [`SubChannel`] with given `channel_id` if it exists.
/// Returns the [`SubChannel`] with given [`ChannelId`] if it exists.
fn get_sub_channel(&self, channel_id: ChannelId) -> Result<Option<SubChannel>, Error>;
/// Return all the [`SubChannel`] within the store.
fn get_sub_channels(&self) -> Result<Vec<SubChannel>, Error>;
Expand Down
Loading

0 comments on commit 61e33c8

Please sign in to comment.