Skip to content

Commit

Permalink
Merge pull request #522 from ExchangeUnion/fix/duplicate-invalidation
Browse files Browse the repository at this point in the history
fix: don't send order invalidation to taker
  • Loading branch information
sangaman authored Sep 26, 2018
2 parents 2f0636d + 9b94048 commit 1579943
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class OrderBook extends EventEmitter {
this.swaps.on('swap.paid', (deal) => {
if (deal.myRole === SwapDealRole.Maker) {
// assume full order execution of an own order
this.removeOwnOrder(deal.orderId, deal.pairId);
this.removeOwnOrder(deal.orderId, deal.pairId, deal.takerPubKey);

// TODO: handle partial order execution, updating existing order
}
Expand Down Expand Up @@ -253,9 +253,10 @@ class OrderBook extends EventEmitter {

/**
* Attempts to remove a local order from the order book.
* @param takerPubKey the node pub key of the taker who filled this order, if applicable
* @returns true if an order was removed, otherwise false
*/
private removeOwnOrder = (orderId: string, pairId: string): boolean => {
private removeOwnOrder = (orderId: string, pairId: string, takerPubKey?: string): boolean => {
const matchingEngine = this.matchingEngines.get(pairId);
if (!matchingEngine) {
this.logger.warn(`invalid pairId: ${pairId}`);
Expand All @@ -275,7 +276,7 @@ class OrderBook extends EventEmitter {
this.pool.broadcastOrderInvalidation({
orderId,
pairId,
});
}, takerPubKey);
}

return true;
Expand Down
12 changes: 10 additions & 2 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,17 @@ class Pool extends EventEmitter {
// TODO: send only to peers which accepts the pairId
}

public broadcastOrderInvalidation = (order: OrderIdentifier) => {
/**
* Broadcasts an [[OrderInvalidationPacket]] to all currently connected peers.
* @param nodeToExclude the node pub key of a node to exclude from the packet broadcast
*/
public broadcastOrderInvalidation = (order: OrderIdentifier, nodeToExclude?: string) => {
const orderInvalidationPacket = new packets.OrderInvalidationPacket(order);
this.peers.forEach(peer => peer.sendPacket(orderInvalidationPacket));
this.peers.forEach((peer) => {
if (!nodeToExclude || peer.nodePubKey !== nodeToExclude) {
peer.sendPacket(orderInvalidationPacket);
}
});

// TODO: send only to peers which accepts the pairId
}
Expand Down
4 changes: 4 additions & 0 deletions lib/swaps/Swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type SwapDeal = {
state: SwapDealState;
/** The reason for being in current state */
stateReason: string;
/** The xud node pub key of the counterparty to this swap deal. */
peerPubKey: string;
/** Global order id in the XU network. */
orderId: string;
/** The quantity of the order to execute as proposed by the taker. Negative when the taker is selling. */
Expand Down Expand Up @@ -235,6 +237,7 @@ class Swaps extends EventEmitter {

const deal: SwapDeal = {
...swapRequestBody,
peerPubKey: peer.nodePubKey!,
phase: SwapDealPhase.SwapCreated,
state: SwapDealState.Active,
stateReason: '',
Expand Down Expand Up @@ -282,6 +285,7 @@ class Swaps extends EventEmitter {
// accept the deal
const deal: SwapDeal = {
...requestBody,
peerPubKey: peer.nodePubKey!,
quantity: requestBody.proposedQuantity,
phase: SwapDealPhase.SwapCreated,
state: SwapDealState.Active,
Expand Down

0 comments on commit 1579943

Please sign in to comment.