diff --git a/lib/orderbook/OrderBook.ts b/lib/orderbook/OrderBook.ts index cc1013164..33f26bae4 100644 --- a/lib/orderbook/OrderBook.ts +++ b/lib/orderbook/OrderBook.ts @@ -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 } @@ -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}`); @@ -275,7 +276,7 @@ class OrderBook extends EventEmitter { this.pool.broadcastOrderInvalidation({ orderId, pairId, - }); + }, takerPubKey); } return true; diff --git a/lib/p2p/Pool.ts b/lib/p2p/Pool.ts index 3efe5c6ba..eb804d306 100644 --- a/lib/p2p/Pool.ts +++ b/lib/p2p/Pool.ts @@ -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 } diff --git a/lib/swaps/Swaps.ts b/lib/swaps/Swaps.ts index e27c04e6d..91e21f756 100644 --- a/lib/swaps/Swaps.ts +++ b/lib/swaps/Swaps.ts @@ -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. */ @@ -235,6 +237,7 @@ class Swaps extends EventEmitter { const deal: SwapDeal = { ...swapRequestBody, + peerPubKey: peer.nodePubKey!, phase: SwapDealPhase.SwapCreated, state: SwapDealState.Active, stateReason: '', @@ -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,