Skip to content

Commit 0fe2d66

Browse files
committed
fix: don't activate unsupported pairs with peers
This ensures that, even when not running in strict mode, we only activate trading pairs with peers when we support both currencies of the pair and the trading pair itself locally. Previously we would activate every trading pair, like BTC/LTC when we don't have a swap client for LTC, resulting in us requesting and receiving order updates for trading pairs that are irrelevent to us.
1 parent 62b9312 commit 0fe2d66

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Diff for: lib/orderbook/OrderBook.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,11 @@ class OrderBook extends EventEmitter {
994994
if (peer.isPairActive(pairId)) {
995995
return false; // don't verify a pair that is already active
996996
}
997+
if (!this.tradingPairs.has(pairId)) {
998+
// if we don't support the trading pair locally, then we don't want to verify or activate it
999+
return false;
1000+
}
1001+
9971002
const [baseCurrency, quoteCurrency] = pairId.split('/');
9981003
const peerCurrenciesEnabled = !peer.disabledCurrencies.has(baseCurrency)
9991004
&& !peer.disabledCurrencies.has(quoteCurrency);
@@ -1050,7 +1055,9 @@ class OrderBook extends EventEmitter {
10501055

10511056
// activate verified currencies
10521057
currenciesToVerify.forEach((swappable, currency) => {
1053-
if (swappable || !this.strict) { // always activate currencies if not in strict mode
1058+
// in strict mode, we only activate "swappable" currencies where a route to peer is possible or a sanity swap has completed
1059+
// in non-strict mode, we activate any currency which we also support locally
1060+
if (swappable || (!this.strict && this.swaps.swapClientManager.has(currency))) {
10541061
peer.activateCurrency(currency);
10551062
}
10561063
});

Diff for: lib/swaps/SwapClientManager.ts

+9
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ class SwapClientManager extends EventEmitter {
247247
return this.swapClients.get(currency);
248248
}
249249

250+
/**
251+
* Returns whether the swap client manager has a client for a given currency.
252+
* @param currency the currency that the swap client is linked to.
253+
* @returns `true` if a swap client exists, false otherwise.
254+
*/
255+
public has = (currency: string): boolean => {
256+
return this.swapClients.has(currency);
257+
}
258+
250259
/** Gets the type of swap client for a given currency. */
251260
public getType = (currency: string) => {
252261
return this.swapClients.get(currency)?.type;

Diff for: test/jest/Orderbook.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,11 @@ describe('OrderBook', () => {
175175
test('nosanityswaps enabled adds pairs and requests orders', async () => {
176176
orderbook['nosanityswaps'] = true;
177177
await orderbook['verifyPeerPairs'](peer);
178-
expect(mockActivateCurrency).toHaveBeenCalledTimes(3);
178+
expect(mockActivateCurrency).toHaveBeenCalledTimes(2);
179179
expect(mockActivateCurrency).toHaveBeenCalledWith('BTC');
180180
expect(mockActivateCurrency).toHaveBeenCalledWith('LTC');
181-
expect(mockActivateCurrency).toHaveBeenCalledWith('WETH');
182-
expect(mockActivatePair).toHaveBeenCalledTimes(2);
181+
expect(mockActivatePair).toHaveBeenCalledTimes(1);
183182
expect(mockActivatePair).toHaveBeenCalledWith(advertisedPairs[0]);
184-
expect(mockActivatePair).toHaveBeenCalledWith(advertisedPairs[1]);
185183
});
186184

187185
test('isPeerCurrencySupported returns true for a known currency with matching identifiers', async () => {

0 commit comments

Comments
 (0)