Skip to content

Commit

Permalink
feat(rpc): add failureReason to SwapFailure msg
Browse files Browse the repository at this point in the history
This specifies the `SwapFailureReason` for each `SwapFailure` message
sent in gRPC responses.

Closes #807.
  • Loading branch information
sangaman committed Mar 17, 2019
1 parent 00ca2f8 commit f2d9228
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ export const orderHandler = (argv: Arguments, isSell = false) => {
};

const formatPlaceOrderOutput = (response: PlaceOrderResponse.AsObject) => {
const { internalMatchesList, swapSuccessesList, remainingOrder } = response;
if (internalMatchesList.length === 0 && swapSuccessesList.length === 0) {
const { internalMatchesList, swapSuccessesList, swapFailuresList, remainingOrder } = response;
if (internalMatchesList.length === 0 && swapSuccessesList.length === 0 && swapFailuresList.length === 0) {
console.log('no matches found');
} else {
internalMatchesList.forEach(formatInternalMatch);
swapSuccessesList.forEach(formatSwapSuccess);
swapFailuresList.forEach(formatSwapFailure);
}
if (remainingOrder) {
formatRemainingOrder(remainingOrder);
Expand Down
2 changes: 2 additions & 0 deletions lib/constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export enum SwapFailureReason {
SwapTimedOut = 10,
/** The deal timed out while we were waiting for the peer to respond to our swap request. */
DealTimedOut = 11,
/** The swap failed due to an unrecognized error. */
UnknownError = 12,
}

export enum DisconnectionReason {
Expand Down
22 changes: 12 additions & 10 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { errorCodes as serviceErrorCodes } from '../service/errors';
import { errorCodes as p2pErrorCodes } from '../p2p/errors';
import { errorCodes as lndErrorCodes } from '../lndclient/errors';
import { LndInfo } from '../lndclient/LndClient';
import { SwapSuccess } from '../swaps/types';
import { SwapSuccess, SwapFailure } from '../swaps/types';
import { SwapFailureReason } from '../constants/enums';

/**
Expand Down Expand Up @@ -57,15 +57,16 @@ const createSwapSuccess = (result: SwapSuccess) => {
};

/**
* Creates an xudrpc SwapFailure message from a [[PeerOrder]] that could not be swapped.
* Creates an xudrpc SwapFailure message from a [[SwapFailure]].
*/
const createSwapFailure = (order: PeerOrder) => {
const swapFailure = new xudrpc.SwapFailure();
swapFailure.setOrderId(order.id);
swapFailure.setPairId(order.pairId);
swapFailure.setPeerPubKey(order.peerPubKey);
swapFailure.setQuantity(order.quantity);
return swapFailure;
const createSwapFailure = (swapFailure: SwapFailure) => {
const grpcSwapFailure = new xudrpc.SwapFailure();
grpcSwapFailure.setOrderId(swapFailure.orderId);
grpcSwapFailure.setPairId(swapFailure.pairId);
grpcSwapFailure.setPeerPubKey(swapFailure.peerPubKey);
grpcSwapFailure.setQuantity(swapFailure.quantity);
grpcSwapFailure.setFailureReason(SwapFailureReason[swapFailure.failureReason]);
return grpcSwapFailure;
};

/**
Expand Down Expand Up @@ -106,7 +107,7 @@ const createPlaceOrderEvent = (e: PlaceOrderEvent) => {
placeOrderEvent.setRemainingOrder(createOrder(e.payload as Order));
break;
case PlaceOrderEventType.SwapFailure:
placeOrderEvent.setSwapFailure(createSwapFailure(e.payload as PeerOrder));
placeOrderEvent.setSwapFailure(createSwapFailure(e.payload as SwapFailure));
break;
}
return placeOrderEvent;
Expand Down Expand Up @@ -323,6 +324,7 @@ class GrpcService {
code = status.FAILED_PRECONDITION;
break;
case SwapFailureReason.UnexpectedLndError:
case SwapFailureReason.UnknownError:
default:
code = status.UNKNOWN;
break;
Expand Down
22 changes: 17 additions & 5 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { CurrencyInstance, PairInstance, CurrencyFactory } from '../db/types';
import { Pair, OrderIdentifier, OwnOrder, OrderPortion, OwnLimitOrder, PeerOrder, Order, PlaceOrderEvent,
PlaceOrderEventType, PlaceOrderResult, OutgoingOrder, OwnMarketOrder, isOwnOrder, IncomingOrder } from './types';
import { SwapRequestPacket, SwapFailedPacket } from '../p2p/packets';
import { SwapSuccess, SwapDeal } from '../swaps/types';
import { SwapSuccess, SwapDeal, SwapFailure } from '../swaps/types';
import Bluebird from 'bluebird';

interface OrderBook {
Expand Down Expand Up @@ -309,7 +309,7 @@ class OrderBook extends EventEmitter {
/** Successful swaps performed for the placed order. */
const swapSuccesses: SwapSuccess[] = [];
/** Failed swaps attempted for the placed order. */
const swapFailures: PeerOrder[] = [];
const swapFailures: SwapFailure[] = [];

/**
* The routine for retrying a portion of the order that failed a swap attempt.
Expand Down Expand Up @@ -374,14 +374,26 @@ class OrderBook extends EventEmitter {
onUpdate && onUpdate({ type: PlaceOrderEventType.SwapSuccess, payload: swapResult });
} catch (err) {
const failMsg = `swap for ${portion.quantity} failed during order matching`;
let failureReason: SwapFailureReason;
if (typeof err === 'number') {
// treat the error as a SwapFailureReason
this.logger.warn(`${failMsg} due to ${SwapFailureReason[err]}, will repeat matching routine for failed quantity`);
failureReason = err;
} else {
this.logger.error(`${failMsg}, will repeat matching routine for failed quantity`, err);
this.logger.error(`${failMsg} due to unexpected error, will repeat matching routine for failed quantity`, err);
failureReason = SwapFailureReason.UnknownError;
throw err;
}
swapFailures.push(maker);
onUpdate && onUpdate({ type: PlaceOrderEventType.SwapFailure, payload: maker });

const swapFailure: SwapFailure = {
failureReason,
orderId: maker.id,
pairId: maker.pairId,
quantity: portion.quantity,
peerPubKey: maker.peerPubKey,
};
swapFailures.push(swapFailure);
onUpdate && onUpdate({ type: PlaceOrderEventType.SwapFailure, payload: swapFailure });
await retryFailedSwap(portion.quantity);
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/orderbook/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SwapClients } from '../constants/enums';
import { SwapSuccess } from 'lib/swaps/types';
import { SwapSuccess, SwapFailure } from 'lib/swaps/types';

export type OrderMatch = {
maker: Order;
Expand All @@ -14,13 +14,13 @@ export type MatchingResult = {
export type PlaceOrderResult = {
internalMatches: OwnOrder[];
swapSuccesses: SwapSuccess[];
swapFailures: PeerOrder[];
swapFailures: SwapFailure[];
remainingOrder?: OwnOrder;
};

export type PlaceOrderEvent = {
type: PlaceOrderEventType;
payload: OwnOrder | SwapSuccess | PeerOrder;
payload: OwnOrder | SwapSuccess | SwapFailure;
};

export enum PlaceOrderEventType {
Expand Down
4 changes: 4 additions & 0 deletions lib/proto/xudrpc.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/proto/xudrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion lib/proto/xudrpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/swaps/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ export type SwapSuccess = Pick<SwapDeal, 'orderId' | 'localId' | 'pairId' | 'rHa
/** The quantity that was swapped. */
quantity: number;
};

export type SwapFailure = Pick<SwapDeal, 'orderId' | 'pairId' | 'quantity' | 'peerPubKey' > & {
/** The quantity that was attempted and failed for the swap. */
quantity: number;
failureReason: SwapFailureReason;
};
2 changes: 2 additions & 0 deletions proto/xudrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ message SwapFailure {
double quantity = 3 [json_name = "quantity"];
// The node pub key of the peer that we attempted to swap with.
string peer_pub_key = 4 [json_name = "peer_pub_key"];
// The reason why the swap failed.
string failure_reason = 5 [json_name = "failure_reason"];
}

message GetInfoRequest {}
Expand Down

0 comments on commit f2d9228

Please sign in to comment.