Skip to content

Commit

Permalink
Merge pull request #1072 from ExchangeUnion/raiden-first-leg
Browse files Browse the repository at this point in the history
Prevent Raiden from being first swap leg
  • Loading branch information
sangaman authored Jul 8, 2019
2 parents 495dd0c + 7c76a3d commit a63fec9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/http/HttpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class HttpService {
constructor(private service: Service) {}

public resolveHashRaiden = async (resolveRequest: RaidenResolveRequest): Promise<RaidenResolveResponse> => {
// TODO: add reveal_timeout, settle time out, token, etc
const secret = await this.service.resolveHash({
rHash: resolveRequest.secrethash.slice(2),
amount: resolveRequest.amount,
Expand Down
5 changes: 3 additions & 2 deletions lib/raidenclient/RaidenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function parseResponseBody<T>(res: http.IncomingMessage): Promise<T> {
*/
class RaidenClient extends SwapClient {
public readonly type = SwapClientType.Raiden;
public readonly cltvDelta: number = 1;
public readonly cltvDelta: number = 5760;
public address?: string;
/** A map of currency symbols to token addresses. */
public tokenAddresses = new Map<string, string>();
Expand Down Expand Up @@ -152,8 +152,9 @@ class RaidenClient extends SwapClient {

public getRoutes = async (_amount: number, _destination: string) => {
// stub placeholder, query routes not currently implemented in raiden
// assume a fixed lock time of 100 Raiden's blocks
return [{
getTotalTimeLock: () => 1,
getTotalTimeLock: () => 101,
}];
}

Expand Down
4 changes: 2 additions & 2 deletions lib/swaps/SwapClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { Models } from '../db/DB';
import { SwapClientType } from '../constants/enums';
import { EventEmitter } from 'events';

function isRaidenClient(swapClient: SwapClient): swapClient is RaidenClient {
export function isRaidenClient(swapClient: SwapClient): swapClient is RaidenClient {
return (swapClient.type === SwapClientType.Raiden);
}

function isLndClient(swapClient: SwapClient): swapClient is LndClient {
export function isLndClient(swapClient: SwapClient): swapClient is LndClient {
return (swapClient.type === SwapClientType.Lnd);
}

Expand Down
32 changes: 29 additions & 3 deletions lib/swaps/Swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SwapDealInstance } from '../db/types';
import { SwapDeal, SwapSuccess, SanitySwap, ResolveRequest } from './types';
import { generatePreimageAndHash, setTimeoutPromise } from '../utils/utils';
import { PacketType } from '../p2p/packets';
import SwapClientManager from './SwapClientManager';
import SwapClientManager, { isRaidenClient } from './SwapClientManager';
import { errors } from './errors';

export type OrderToAccept = Pick<SwapDeal, 'quantity' | 'price' | 'localId' | 'isBuy'> & {
Expand Down Expand Up @@ -444,6 +444,29 @@ class Swaps extends EventEmitter {
const { makerCurrency, makerAmount, makerUnits, takerCurrency, takerAmount, takerUnits } =
Swaps.calculateMakerTakerAmounts(quantity, price, isBuy, requestBody.pairId);

const makerSwapClient = this.swapClientManager.get(makerCurrency)!;
if (!makerSwapClient) {
await this.sendErrorToPeer({
peer,
rHash,
failureReason: SwapFailureReason.SwapClientNotSetup,
errorMessage: 'Unsupported maker currency',
reqId: requestPacket.header.id,
});
return false;
}

if (isRaidenClient(makerSwapClient)) {
await this.sendErrorToPeer({
peer,
rHash,
failureReason: SwapFailureReason.InvalidSwapRequest,
errorMessage: 'Raiden based tokens can not be first leg',
reqId: requestPacket.header.id,
});
return false;
}

const takerSwapClient = this.swapClientManager.get(takerCurrency);
if (!takerSwapClient) {
await this.sendErrorToPeer({
Expand Down Expand Up @@ -571,7 +594,6 @@ class Swaps extends EventEmitter {
return false;
}

const makerSwapClient = this.swapClientManager.get(makerCurrency)!;
try {
await makerSwapClient.addInvoice(deal.rHash, deal.makerUnits, deal.makerCltvDelta);
} catch (err) {
Expand Down Expand Up @@ -694,12 +716,16 @@ class Swaps extends EventEmitter {
let expectedAmount: number;
let source: string;
let destination: string;

// TODO: check cltv value
switch (deal.role) {
case SwapRole.Maker:
expectedAmount = deal.makerUnits;
source = 'Taker';
destination = 'Maker';
if (deal.makerCltvDelta! > 50 * 2) {
this.failDeal(deal, SwapFailureReason.InvalidResolveRequest, 'Wrong CLTV received on first leg');
return false;
}
break;
case SwapRole.Taker:
expectedAmount = deal.takerUnits;
Expand Down

0 comments on commit a63fec9

Please sign in to comment.