Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to rust-lightning:0.0.117 #176

Open
wants to merge 1 commit into
base: feature/ln-dlc-channels
Choose a base branch
from

Conversation

luckysori
Copy link
Collaborator

@luckysori luckysori commented Nov 22, 2023

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 ChannelIds, 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.

@luckysori luckysori added the ln label Nov 22, 2023
@luckysori luckysori self-assigned this Nov 22, 2023
@Tibo-lg
Copy link
Contributor

Tibo-lg commented Nov 24, 2023

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 ChannelIds, 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.

I feel it'd be cleaner to just define our own structs, and implement From<ldk::ChannelId> for the SubChannelId one. Then we could derive the serde::Serialize, and also have a more consistent naming. I feel it can be confusing that Channel structs use DlcChannelId while SubChannel use ChannelId. Unless there is some advantage that I'm not seeing?

@luckysori luckysori force-pushed the chore/upgrade-ldk-117 branch 2 times, most recently from 65b6bce to 61e33c8 Compare November 28, 2023 20:57
@luckysori
Copy link
Collaborator Author

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 ChannelIds, 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.

I feel it'd be cleaner to just define our own structs, and implement From<ldk::ChannelId> for the SubChannelId one. Then we could derive the serde::Serialize, and also have a more consistent naming. I feel it can be confusing that Channel structs use DlcChannelId while SubChannel use ChannelId. Unless there is some advantage that I'm not seeing?

I think what you propose is even better, but I wasn't sure if you wanted to go that far since before this PR we were just using type aliases everywhere. I can work on adding a couple of newtypes like you suggest.

@luckysori luckysori force-pushed the chore/upgrade-ldk-117 branch 2 times, most recently from cb6964f to 1fe6ab5 Compare November 29, 2023 10:13
@holzeis holzeis force-pushed the chore/upgrade-ldk-117 branch 3 times, most recently from 07d1fe3 to b7721f9 Compare November 29, 2023 14:38
@luckysori luckysori force-pushed the chore/upgrade-ldk-117 branch 2 times, most recently from 1fe6ab5 to 745717d Compare December 1, 2023 05:01
@luckysori
Copy link
Collaborator Author

luckysori commented Dec 20, 2023

@Tibo-lg: I've tried to address your comment.

It's probably not perfect because it was a pretty painful process, so let me know if I can make it better.

I had to move some stuff around so that the new structs could be used in all the necessary places. I also left a couple of TODOs and a NOTE. Let me know what you think about those too.


Still fixing some tests

As part of this update `rust-lightning` introduced a `ChannelId`
struct, which motivated us to also introduce the `DlcChannelId` and
`SubChannelId` structs.
Copy link
Contributor

@Tibo-lg Tibo-lg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, still a couple of things that need fixing.

@@ -130,8 +131,8 @@ macro_rules! check_for_timed_out_channels {
if is_timed_out {
let sub_channel = if channel.is_sub_channel() {
log::info!(
"Skipping force-closure of subchannel {}: not supported",
bitcoin::hashes::hex::ToHex::to_hex(&channel.channel_id[..])
"Skipping force-closure of subchannel {:?}: not supported",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean it will be printed as an array rather than a hex string? It'd be nice to have an easy way to print these ids.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we need to print this as hex. I'll fix it.

Comment on lines +791 to +792
let channel_id = crate::utils::get_new_temporary_id();
let channel_id = DlcChannelId::from_bytes(channel_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to have some wrapper within the type for this (can do it later as well).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. I will do that.

@@ -1756,7 +1763,7 @@ where
self.chain_monitor.lock().unwrap().add_tx(
prev_tx_id,
ChannelInfo {
channel_id: signed_channel.channel_id,
channel_id: signed_channel.channel_id.inner(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to implement Deref or into (really just a suggestion, not sure it'd be much better).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I think From/Into makes sense. Can do that.

@@ -228,10 +231,11 @@ where
ln_channel_manager,
dlc_channel_manager,
actions: Mutex::new(actions),
phantom: PhantomData,
phantom_cs: PhantomData,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need the CS generic parameter anymore.

@@ -359,8 +363,7 @@ where
let per_split_seed_pk =
PublicKey::from_secret_key(self.dlc_channel_manager.get_secp(), &per_split_seed);

let temporary_channel_id: ContractId =
subchannel::generate_temporary_channel_id(*channel_id, update_idx, 0);
let temporary_channel_id = generate_temporary_dlc_channel_id(channel_id, update_idx, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this would be better as a method on the type.

Comment on lines +268 to +271
_: u64,
_: &NodeId,
_: &NodeId,
_: ChannelUsage,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why you changed these and not the last one :D (I feel not changing them is also fine)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's weird. Definitely not intentional. I'll revert it.

@@ -714,3 +715,59 @@ pub fn refresh_wallet<B: Deref, W: Deref>(
retry += 1;
}
}

/// Serialize an hexadecimal value.
pub fn serialize_hex<S>(hex: &[u8], s: S) -> Result<S::Ok, S::Error>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are the same methods in dlc::serde_utils, maybe easier to just reference that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, will do that!

@@ -12,8 +12,8 @@ use-serde = ["serde", "secp256k1-zkp/use-serde"]

[dependencies]
bitcoin = {version = "0.29.2"}
dlc = {version = "0.4.0", path = "../dlc"}
lightning = {version = "0.0.116"}
dlc = {version = "0.4.0", path = "../dlc", features = ["serde"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think serde is supposed to be feature gated (not 100% sure it's correctly setup but at least that was the goal)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. We want to only enable serde for the dlc crate if the serde feature flag is passed. I'll fix that.

@@ -9,10 +9,11 @@ version = "0.4.0"

[dependencies]
bitcoin = {version = "0.29.2"}
lightning = {version = "0.0.117"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather this crate to stay very simple and not import lightning unless necessary. Actually is there a reason that you put ids.rs here and not in dlc-manager? It doesn't seem to be used within this crate.

Copy link
Collaborator Author

@luckysori luckysori Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually is there a reason that you put ids.rs here and not in dlc-manager? It doesn't seem to be used within this crate.

I was also hesitant about this. But it seemed to me like IDs belong in a core crate like dlc (seems to be).

The reason why I didn't want to put them in dlc-manager was because IDs are used in dlc-messages, meaning that dlc-messages would have to depend on dlc-manager which seems weird. And, more importantly, that would create a circular dependency.

I would rather this crate to stay very simple and not import lightning unless necessary.

I considered adding a feature flag to the dlc crate, but it was getting a bit tedious so I dropped it. Should I try that again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah a circular dependency wouldn't be good. What do you think about just not using these types for the dlc-messages crate? I guess it's not that a big deal to have it in dlc though, so I'll leave it up to you.

use secp256k1_zkp::{PublicKey, SecretKey};
use simple_wallet::WalletStorage;
use std::collections::HashMap;
use std::sync::{Mutex, RwLock};

pub struct MemoryStorage {
contracts: RwLock<HashMap<ContractId, Contract>>,
channels: RwLock<HashMap<ChannelId, Channel>>,
sub_channels: RwLock<HashMap<ChannelId, SubChannel>>,
channels: RwLock<HashMap<[u8; 32], Channel>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for not using the types here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, I had the types here until the very last second, when the compiler started giving me a hard time about the types not implementing certain traits. I can give it a go again.

@luckysori luckysori removed their assignment Feb 9, 2024
@luckysori luckysori removed the request for review from holzeis February 9, 2024 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants