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

fix(swaps): ensure correct takerCltvDelta and makerCltvDelta usage #1037

Merged
1 commit merged into from
Jun 20, 2019

Conversation

ghost
Copy link

@ghost ghost commented Jun 16, 2019

This PR fixes several bugs around Swaps.acceptDeal - maker's opportunity to either accept or reject the SwapRequest, more specifically the logic around setting the CLTV values.

Closes #1028
Closes #984

@ghost ghost requested review from sangaman and offerm June 16, 2019 18:37
const request = new lndrpc.QueryRoutesRequest();
const finalCltvDelta = cltvDelta || this.cltvDelta;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this.cltvDelta should be used here?
argument should not be optional IMHO

Copy link
Author

Choose a reason for hiding this comment

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

I originally thought about making the argument mandatory, but the second usage of getRoutes happens within the context of maker at the beginning of the deal execution process when taker queries routes to maker.

At this point taker has the following information:

const makerOrder = {
  id: 'fb1708f0-90d7-11e9-8836-9bf81440c6fa',
  pairId: 'LTC/BTC',
  price: 0.1,
  quantity: 10000,
  isBuy: true,
  peerPubKey:
   '038d48a01d25735cd7bd26ac8cbd7583e4c202718848ab63b08b378dc840a24e81',
  createdAt: 1560759291688,
  initialQuantity: 10000,
};

const takerOrder = {
  pairId: 'LTC/BTC',
  price: 0.1,
  quantity: 10000,
  isBuy: false,
  localId: 'fd6da320-90d7-11e9-9e9d-0f0eb626ffe9',
  hold: 0,
  id: 'fd6da320-90d7-11e9-9e9d-0f0eb626ffe9',
  initialQuantity: 10000,
  createdAt: 1560759295570,
};

What would you set the finalCltvDelta to? Furthermore, the value of takerToMakerRoutes is being used as a light sanity check (error when 0 routes found). Alternative option is to remove the getRoutes check from the taker side.

Copy link
Author

Choose a reason for hiding this comment

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

In order to be more explicit I changed getRoutes to require finalCltvDelta.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fwiw I found it a bit odd to pass an object's own property to one of its method calls as in routes = await swapClient.getRoutes(makerAmount, destination, swapClient.cltvDelta);.

Another way to consider writing this is:

public getRoutes =  async (amount: number, destination: string, finalCltvDelta = this.cltvDelta) => {

That way it's clear what the value defaults to if its not specified. Ultimately not a big deal though so it definitely be left as is.

Copy link
Author

Choose a reason for hiding this comment

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

After thinking about it for a while I agree, it's weird to pass an object's own property to one of its method calls. I changed it back.

@@ -140,7 +140,7 @@ class Swaps extends EventEmitter {
this.sanitySwaps.set(rHash, sanitySwap);
const swapClient = this.swapClientManager.get(currency)!;
try {
await swapClient.addInvoice(rHash, 1);
await swapClient.addInvoice(rHash, 1, swapClient.cltvDelta);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be handled in the same way. The first leg should include the second leg.

Copy link
Author

Choose a reason for hiding this comment

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

In the case of SanitySwap and SanitySwapAck we're currently not exchanging CLTV delta information.

If it's OK with you I'd like to handle everything related to the sanity swaps in a separate issue/PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I also think it's better to handle sanity swaps in a separate PR.

@ghost ghost force-pushed the fix/swaps-cltv branch 2 times, most recently from 665b13c to 5234161 Compare June 17, 2019 09:35
@kilrau
Copy link
Contributor

kilrau commented Jun 17, 2019

Does this fix #977 ?

@kilrau kilrau assigned ghost Jun 17, 2019
@ghost ghost force-pushed the fix/swaps-cltv branch from 5234161 to 3940221 Compare June 18, 2019 07:04
@ghost
Copy link
Author

ghost commented Jun 18, 2019

I doubt it will fix #977 because this PR didn't touch sanity swaps' flow/logic. In the case of SanitySwap and SanitySwapAck we're currently not exchanging CLTV delta information and due to that it can still fail with FinalExpiryTooSoon.

Copy link
Collaborator

@sangaman sangaman left a comment

Choose a reason for hiding this comment

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

Looks good to me, especially the new test cases. Thanks!

@@ -140,7 +140,7 @@ class Swaps extends EventEmitter {
this.sanitySwaps.set(rHash, sanitySwap);
const swapClient = this.swapClientManager.get(currency)!;
try {
await swapClient.addInvoice(rHash, 1);
await swapClient.addInvoice(rHash, 1, swapClient.cltvDelta);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I also think it's better to handle sanity swaps in a separate PR.

@ghost ghost force-pushed the fix/swaps-cltv branch from 3940221 to 5dc728b Compare June 19, 2019 17:28
@ghost ghost merged commit 4901b5e into master Jun 20, 2019
@ghost ghost deleted the fix/swaps-cltv branch June 20, 2019 07:35
This pull request was closed.
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.

Digg into SendToRoute issue LTC/BTC swap failure - Taker's xud crashed
3 participants