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

[r2r] lightning ordermatching wip + library updates and more unit tests #1655

Merged
merged 25 commits into from
Mar 10, 2023

Conversation

shamardy
Copy link
Collaborator

@shamardy shamardy commented Feb 13, 2023

Intermediate PR as part of #1045

  • Updated rust-lightning to v0.0.113
  • Added channel current_confirmations and required_confirmations (which are easy to retrieve after the above update) to channel details RPCs.
  • Used uuid for internal channel id instead of u64 after rust-lightning update since they now use u128 for user_channel_id
  • Updated uuid lib to latest version to use conversion from/to u128 features
  • Added unit tests for multi path payments.
  • Added unit tests for claiming swaps on-chain for closed channels which is not currently working. After some discussion with rust-lightning devs this will be implemented in rust-lightning instead of AtomicDEX Support claiming from closed channels lightningdevkit/rust-lightning#2017
  • Started working in lightning ordermatching, protocol_info fields are used to check if an order can be matched or not depending on if a route is found between maker/taker for a lightning payment.
  • Fixed 2 issues I discovered while executing a KMD/LNBTC swap, they were:
    • When electrums were down, if a channel was closed, the channel closing transaction wasn't broadcasted. Now I check for a network error and rebroadcast transactions after a delay if there was a network error.
    • If an invoice payment failed, if I retry paying the same invoice later, the payment wasn't updated to successful in DB if it were. Now I added a method to update the payment in the DB to update it in this case.

To be done in next PRs:

  • Complete lightning order matching implementation and balance checks.
    • Right now inbound liquidity is not checked when placing an order to receive lightning BTC.
    • Show lightning node address in orderbook/best_orders response.
    • Fees can't be calculated accurately before an order is placed since they depend on the route. Will need to find a workaround for this.
  • After updating to v0.0.113, an async event handler can be used instead of background processor, this paves the way for lightning to work in WASM.

@shamardy shamardy changed the title lightning ordermatching wip + library updates and more unit tests [r2r] lightning ordermatching wip + library updates and more unit tests Feb 13, 2023
Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

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

Amazing work! My notes from the partial review

Comment on lines 214 to 219
let expect_msg = "for the foreseeable future this shouldn't happen";
let current_time: u32 = get_local_duration_since_epoch()
.expect(expect_msg)
.as_secs()
.try_into()
.expect(expect_msg);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let expect_msg = "for the foreseeable future this shouldn't happen";
let current_time: u32 = get_local_duration_since_epoch()
.expect(expect_msg)
.as_secs()
.try_into()
.expect(expect_msg);
let current_time: u32 = get_local_duration_since_epoch()
.map_to_mm(|e| EnableLightningError::Internal(e.to_string()))?
.as_secs()
.try_into()
.map_to_mm(|e| EnableLightningError::Internal(format!("{:?}", e)))?;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

/// Get some of the coin config info in serialized format for p2p messaging.
fn coin_protocol_info(&self) -> Vec<u8>;
/// Get some of the coin protocol related info in serialized format for p2p messaging.
fn coin_protocol_info(&self, amount_to_receive: Option<MmNumber>) -> Vec<u8>;
Copy link
Member

Choose a reason for hiding this comment

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

Some of the implementations returning empty byte array, and this value is usually passed as Option type to the target. I am not sure if Some(Vec::new()) is handled properly. I would rather update the return type into Option<Vec<u8>> here and return None for functions that are returning Vec::new().

Copy link
Collaborator Author

@shamardy shamardy Feb 17, 2023

Choose a reason for hiding this comment

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

coin_protocol_info first implementation used to return Option<Vec<u8>> and was changed in the following PR #1064 by Artem. We had some backward compatibility issues related to this before #1017, #1046 that would make me hesitant to change this.

Copy link
Member

Choose a reason for hiding this comment

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

@shamardy I think in most cases, an empty vec == None...what I'm saying it's unnecessary to use Option<Vec<_>> but instead just Vec<_>.

seems this is how optional keys field are mostly stored in structures mm2 so no problem ):

Cargo.lock Outdated
Comment on lines 459 to 463
[[package]]
name = "bech32"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445"
Copy link
Member

Choose a reason for hiding this comment

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

Only dependency that is still using 0.8.1 was KomodoPlatform/librustzcash, I upgraded bech32 to 0.9.1 there in k-1.0.0 tag. So we can avoid having 2 versions of bech32 that are almost identical.

You can apply the following patch for the version migration:

diff --git a/mm2src/coins/Cargo.toml b/mm2src/coins/Cargo.toml
index f35490716..1aa4824dc 100644
--- a/mm2src/coins/Cargo.toml
+++ b/mm2src/coins/Cargo.toml
@@ -125,10 +125,10 @@ tokio = { version = "1.7" }
 tokio-rustls = { version = "0.23" }
 tonic = { version = "0.7", features = ["tls", "tls-webpki-roots", "compression"] }
 webpki-roots = { version = "0.22" }
-zcash_client_backend = { git = "https://github.com/KomodoPlatform/librustzcash.git" }
-zcash_client_sqlite = { git = "https://github.com/KomodoPlatform/librustzcash.git" }
-zcash_primitives = { features = ["transparent-inputs"], git = "https://github.com/KomodoPlatform/librustzcash.git" }
-zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git" }
+zcash_client_backend = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.0.0" }
+zcash_client_sqlite = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.0.0" }
+zcash_primitives = { features = ["transparent-inputs"], git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.0.0" }
+zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.0.0" }

 [target.'cfg(windows)'.dependencies]
 winapi = "0.3"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks a lot for upgrading librustzcash. Patch applied.

@shamardy shamardy changed the title [r2r] lightning ordermatching wip + library updates and more unit tests [wip] lightning ordermatching wip + library updates and more unit tests Feb 17, 2023
@shamardy shamardy changed the title [wip] lightning ordermatching wip + library updates and more unit tests [r2r] lightning ordermatching wip + library updates and more unit tests Feb 17, 2023
Copy link

@caglaryucekaya caglaryucekaya left a comment

Choose a reason for hiding this comment

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

Great work! Some questions and comments

mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_serialization.rs Show resolved Hide resolved
mm2src/coins/lightning.rs Show resolved Hide resolved
mm2src/coins/lightning.rs Show resolved Hide resolved
onur-ozkan
onur-ozkan previously approved these changes Feb 21, 2023
Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

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

lgtm! just one suggestion

mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
@shamardy shamardy changed the title [r2r] lightning ordermatching wip + library updates and more unit tests [wip] lightning ordermatching wip + library updates and more unit tests Mar 3, 2023
@shamardy
Copy link
Collaborator Author

shamardy commented Mar 3, 2023

I fixed 2 issues I discovered while executing a KMD/LNBTC swap, they were:

  • When electrums were down, if a channel was closed, the channel closing transaction wasn't broadcasted. Now I check for a network error and rebroadcast transactions after a delay if there was a network error.
  • If an invoice payment failed, if I retry paying the same invoice later, the payment wasn't updated to successful in DB if it were. Now I added a method to update the payment in the DB to update it in this case.

@caglaryucekaya @ozkanonur can you please review the new commits?

@shamardy shamardy changed the title [wip] lightning ordermatching wip + library updates and more unit tests [r2r] lightning ordermatching wip + library updates and more unit tests Mar 3, 2023
onur-ozkan
onur-ozkan previously approved these changes Mar 6, 2023
Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

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

@ozkanonur can you please review the new commits?

Reviewed f7953a7 and e683bea

Both looks good to me

@shamardy
Copy link
Collaborator Author

shamardy commented Mar 7, 2023

@caglaryucekaya can you please approve if there are no more issues with this PR?

Copy link

@caglaryucekaya caglaryucekaya left a comment

Choose a reason for hiding this comment

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

LGTM! Just two comments

if !e.get_inner().is_network_error() {
break;
}
Timer::sleep(TRY_LOOP_INTERVAL).await;

Choose a reason for hiding this comment

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

Should this loop repeat infinitely if the network error keeps coming?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes for now. When broadcasting transaction through p2p network is implemented, a loop to check if the transaction is on-chain or not can be added instead.

error!("Converting transaction to bytes error:{}", e);
break;
},
};

Choose a reason for hiding this comment

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

I think you can take this out of the loop, there's no need to repeat it in case of network failure

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

caglaryucekaya
caglaryucekaya previously approved these changes Mar 7, 2023
Copy link

@caglaryucekaya caglaryucekaya left a comment

Choose a reason for hiding this comment

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

Great work 🔥

@@ -57,7 +57,7 @@ fn insert_saved_swap_sql(swap: SavedSwap) -> Option<(&'static str, Vec<String>)>
#[derive(Debug)]
pub enum SelectRecentSwapsUuidsErr {
Sql(SqlError),
Parse(uuid::parser::ParseError),
Parse(uuid::Error),
Copy link
Member

@borngraced borngraced Mar 8, 2023

Choose a reason for hiding this comment

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

uuid::Error is used in multiple places already so it's better to import once

use uuid::Error as UuidError;

and use everywhere

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@@ -68,8 +68,8 @@ impl From<SqlError> for SelectRecentSwapsUuidsErr {
fn from(err: SqlError) -> Self { SelectRecentSwapsUuidsErr::Sql(err) }
}

impl From<uuid::parser::ParseError> for SelectRecentSwapsUuidsErr {
fn from(err: uuid::parser::ParseError) -> Self { SelectRecentSwapsUuidsErr::Parse(err) }
impl From<uuid::Error> for SelectRecentSwapsUuidsErr {
Copy link
Member

Choose a reason for hiding this comment

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

UuidError

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@borngraced borngraced left a comment

Choose a reason for hiding this comment

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

Great work! just a note and suggestion.

Copy link
Member

@borngraced borngraced left a comment

Choose a reason for hiding this comment

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

Great job and welldone!

@shamardy shamardy merged commit ebdc8c2 into dev Mar 10, 2023
@shamardy shamardy deleted the ln-onchain-swaps branch March 10, 2023 15:30
shamardy added a commit that referenced this pull request Mar 10, 2023
shamardy added a commit that referenced this pull request Mar 14, 2023
* add change logs for PRs merged to dev only

* remove {version} - {date} and add dev branch instead

* add ibc transfer change logs

* add lightning PR #1655 to change logs

* add changelog for #1706

* add changelog for #1694, #1665

---------

Reviewed-by: laruh, caglaryucekaya <[email protected]>
@ca333 ca333 mentioned this pull request Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants