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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,15 @@ class LndClient extends SwapClient {
return this.unaryCall<lndrpc.ListChannelsRequest, lndrpc.ListChannelsResponse>('listChannels', new lndrpc.ListChannelsRequest());
}

public getRoutes = async (amount: number, destination: string): Promise<lndrpc.Route[]> => {
public getRoutes = async (amount: number, destination: string, finalCltvDelta = this.cltvDelta): Promise<lndrpc.Route[]> => {
const request = new lndrpc.QueryRoutesRequest();
request.setAmt(amount);
request.setFinalCltvDelta(this.cltvDelta);
request.setFinalCltvDelta(finalCltvDelta);
request.setNumRoutes(1);
request.setPubKey(destination);
try {
const routes = (await this.queryRoutes(request)).getRoutesList();
this.logger.debug(`got ${routes.length} route(s) to destination ${destination}: ${routes}, FinalCltvDelta: ${this.cltvDelta}`);
this.logger.debug(`got ${routes.length} route(s) to destination ${destination}: ${routes}, finalCltvDelta: ${finalCltvDelta}`);
return routes;
} catch (err) {
if (typeof err.message === 'string' && (
Expand All @@ -396,7 +396,7 @@ class LndClient extends SwapClient {
)) {
return [];
} else {
this.logger.error(`error calling queryRoutes to ${destination}, amount ${amount} FinalCltvDelta ${this.cltvDelta}: ${JSON.stringify(err)}`);
this.logger.error(`error calling queryRoutes to ${destination}, amount ${amount} finalCltvDelta ${finalCltvDelta}: ${JSON.stringify(err)}`);
throw err;
}
}
Expand Down Expand Up @@ -444,13 +444,13 @@ class LndClient extends SwapClient {
return unlockWalletResponse.toObject();
}

public addInvoice = async (rHash: string, amount: number) => {
public addInvoice = async (rHash: string, amount: number, cltvExpiry: number) => {
const addHoldInvoiceRequest = new lndinvoices.AddHoldInvoiceRequest();
addHoldInvoiceRequest.setHash(hexToUint8Array(rHash));
addHoldInvoiceRequest.setValue(amount);
addHoldInvoiceRequest.setCltvExpiry(this.cltvDelta); // TODO: use peer's cltv delta
addHoldInvoiceRequest.setCltvExpiry(cltvExpiry);
await this.addHoldInvoice(addHoldInvoiceRequest);
this.logger.debug(`added invoice of ${amount} for ${rHash} with CltvExpiry ${this.cltvDelta}`);
this.logger.debug(`added invoice of ${amount} for ${rHash} with cltvExpiry ${cltvExpiry}`);
this.subscribeSingleInvoice(rHash);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/swaps/SwapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ abstract class SwapClient extends EventEmitter {
* @param destination target node for the route
* @returns routes
*/
public abstract async getRoutes(amount: number, destination: string): Promise<Route[]>;
public abstract async getRoutes(amount: number, destination: string, finalCltvDelta?: number): Promise<Route[]>;

public abstract async addInvoice(rHash: string, amount: number): Promise<void>;
public abstract async addInvoice(rHash: string, amount: number, cltvExpiry: number): Promise<void>;

public abstract async settleInvoice(rHash: string, rPreimage: string): Promise<void>;

Expand Down
40 changes: 26 additions & 14 deletions lib/swaps/Swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { PacketType } from '../p2p/packets';
import SwapClientManager from './SwapClientManager';
import { errors } from './errors';

type OrderToAccept = Pick<SwapDeal, 'quantity' | 'price' | 'localId' | 'isBuy'> & {
export type OrderToAccept = Pick<SwapDeal, 'quantity' | 'price' | 'localId' | 'isBuy'> & {
quantity: number;
};

Expand Down Expand Up @@ -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.

} catch (err) {
this.logger.error('could not add invoice for sanity swap', err);
return;
Expand Down Expand Up @@ -325,7 +325,7 @@ class Swaps extends EventEmitter {

try {
await Promise.all([
swapClient.addInvoice(rHash, 1),
swapClient.addInvoice(rHash, 1, swapClient.cltvDelta),
peer.sendPacket(sanitySwapInitPacket),
peer.wait(sanitySwapInitPacket.header.id, PacketType.SanitySwapAck, Swaps.SANITY_SWAP_INIT_TIMEOUT),
]);
Expand Down Expand Up @@ -484,14 +484,7 @@ class Swaps extends EventEmitter {
}

try {
this.logger.debug(`trying to query routes from maker to taker with ${takerPubKey}
and amount of ${takerAmount} and FinalCltvDelta ${takerSwapClient.cltvDelta}`);
deal.makerToTakerRoutes = await takerSwapClient.getRoutes(takerAmount, takerPubKey);
this.logger.debug(`queried routes total of ${deal.makerToTakerRoutes.length} available routes from maker to taker`);
deal.makerToTakerRoutes.forEach((availableRoute) => {

this.logger.debug(`available route from maker to taker with total time lock: ${availableRoute.getTotalTimeLock()}`);
});
deal.makerToTakerRoutes = await takerSwapClient.getRoutes(takerAmount, takerPubKey, deal.takerCltvDelta);
} catch (err) {
this.failDeal(deal, SwapFailureReason.UnexpectedClientError, err.message);
await this.sendErrorToPeer({
Expand Down Expand Up @@ -552,11 +545,30 @@ class Swaps extends EventEmitter {
this.logger.debug(`makerCltvDelta: ${deal.makerCltvDelta}`);
}

if (!deal.makerCltvDelta) {
this.failDeal(deal, SwapFailureReason.UnexpectedClientError, 'Could not calculate makerCltvDelta.');
await this.sendErrorToPeer({
peer,
rHash,
failureReason: deal.failureReason!,
errorMessage: deal.errorMessage,
reqId: requestPacket.header.id,
});
return false;
}

const makerSwapClient = this.swapClientManager.get(makerCurrency)!;
try {
await makerSwapClient.addInvoice(deal.rHash, deal.makerAmount);
await makerSwapClient.addInvoice(deal.rHash, deal.makerAmount, deal.makerCltvDelta);
} catch (err) {
this.logger.error('could not add invoice for while accepting deal', err);
this.failDeal(deal, SwapFailureReason.UnexpectedClientError, `could not add invoice for while accepting deal: ${err.message}`);
await this.sendErrorToPeer({
peer,
rHash,
failureReason: deal.failureReason!,
errorMessage: deal.errorMessage,
reqId: requestPacket.header.id,
});
return false;
}

Expand Down Expand Up @@ -624,7 +636,7 @@ class Swaps extends EventEmitter {
}

try {
await takerSwapClient.addInvoice(deal.rHash, deal.takerAmount);
await takerSwapClient.addInvoice(deal.rHash, deal.takerAmount, takerSwapClient.cltvDelta);
} catch (err) {
this.failDeal(deal, SwapFailureReason.UnexpectedClientError, err.message);
await this.sendErrorToPeer({
Expand Down
12 changes: 0 additions & 12 deletions test/integration/Swaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,4 @@ describe('Swaps.Integration', () => {

});

describe.skip('acceptDeal', () => {

it('should reject unsupported currency', async () => {
expect(true).to.equal(false);
});

it('should reject already used hash', async () => {
expect(true).to.equal(false);
});

});

});
Loading