Skip to content

Commit

Permalink
fix: persist node id of peer for swapped order
Browse files Browse the repository at this point in the history
This fixes a bug where the node id of the peer with whom we swapped is
missing when persisting a swapped order to the database.
  • Loading branch information
sangaman committed May 27, 2020
1 parent 4f881cb commit 7ffa091
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,12 @@ class OrderBook extends EventEmitter {
*/
public executeSwap = async (maker: PeerOrder, taker: OwnOrder): Promise<SwapSuccess> => {
// make sure the order is in the database before we begin the swap
await this.repository.addOrderIfNotExists(maker);
if (!(await this.repository.getOrder(maker.id))) {
await this.repository.addOrderIfNotExists({
...maker,
nodeId: this.pool.getNodeId(maker.peerPubKey),
});
}
try {
const swapResult = await this.swaps.executeSwap(maker, taker);
this.emit('peerOrder.filled', maker);
Expand Down Expand Up @@ -583,6 +588,7 @@ class OrderBook extends EventEmitter {
if (takerOrder) {
addOrderPromises.push(this.repository.addOrderIfNotExists(takerOrder));
}

await Promise.all(addOrderPromises);
await this.repository.addTrade({
quantity,
Expand Down
6 changes: 6 additions & 0 deletions lib/orderbook/OrderBookRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class OrderbookRepository {
}
}

public getOrder = (id: string) => {
return this.models.Order.findOne({
where: { id },
});
}

public addTrade = (trade: db.TradeFactory) => {
return this.models.Trade.create(trade);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/p2p/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class NodeList extends EventEmitter {
this.nodes.forEach(callback);
}

public getId = (nodePubKey: string) => {
return this.nodes.get(nodePubKey)?.id;
}

/**
* Return list of public keys of all banned nodes with a given alias.
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class Pool extends EventEmitter {
return this.nodeState.tokenIdentifiers[currency];
}

public getNodeId = (nodePubKey: string) => {
return this.nodes.getId(nodePubKey);
}

/**
* Initialize the Pool by connecting to known nodes and listening to incoming peer connections, if configured to do so.
*/
Expand Down

0 comments on commit 7ffa091

Please sign in to comment.