diff --git a/lib/orderbook/OrderBook.ts b/lib/orderbook/OrderBook.ts index 77bfec0d7..1b4ca4478 100644 --- a/lib/orderbook/OrderBook.ts +++ b/lib/orderbook/OrderBook.ts @@ -702,33 +702,34 @@ class OrderBook extends EventEmitter { const currenciesToVerify = new Set(); pairIds.forEach((pairId) => { const [baseCurrency, quoteCurrency] = pairId.split('/'); - currenciesToVerify.add(baseCurrency); - currenciesToVerify.add(quoteCurrency); + if (!peer.verifiedCurrencies.has(baseCurrency)) { + currenciesToVerify.add(baseCurrency); + } + if (!peer.verifiedCurrencies.has(quoteCurrency)) { + currenciesToVerify.add(quoteCurrency); + } }); - const verifiedCurrencies = new Set(); const verifiedPairs: string[] = []; const sanitySwapPromises: Promise[] = []; - if (this.swaps) { - // Set a time limit for all sanity swaps to complete. - const sanitySwapTimeout = setTimeoutPromise(OrderBook.MAX_SANITY_SWAP_TIME, false); - - currenciesToVerify.forEach((currency) => { - if (this.currencyInstances.has(currency)) { - // perform sanity swaps for each of the currencies that we support - const sanitySwapPromise = new Promise(async (resolve) => { - // success resolves to true if the sanity swap succeeds before the timeout - const success = await Promise.race([this.swaps!.executeSanitySwap(currency, peer), sanitySwapTimeout]); - if (success) { - verifiedCurrencies.add(currency); - } - resolve(); - }); - sanitySwapPromises.push(sanitySwapPromise); - } - }); - } + // Set a time limit for all sanity swaps to complete. + const sanitySwapTimeout = setTimeoutPromise(OrderBook.MAX_SANITY_SWAP_TIME, false); + + currenciesToVerify.forEach((currency) => { + if (this.currencyInstances.has(currency)) { + // perform sanity swaps for each of the currencies that we support + const sanitySwapPromise = new Promise(async (resolve) => { + // success resolves to true if the sanity swap succeeds before the timeout + const success = await Promise.race([this.swaps!.executeSanitySwap(currency, peer), sanitySwapTimeout]); + if (success) { + peer.verifiedCurrencies.add(currency); + } + resolve(); + }); + sanitySwapPromises.push(sanitySwapPromise); + } + }); // wait for all sanity swaps to finish or timeout await Promise.all(sanitySwapPromises); @@ -736,7 +737,7 @@ class OrderBook extends EventEmitter { // activate pairs that have had both currencies verified pairIds.forEach(async (pairId) => { const [baseCurrency, quoteCurrency] = pairId.split('/'); - if (verifiedCurrencies.has(baseCurrency) && verifiedCurrencies.has(quoteCurrency)) { + if (peer.verifiedCurrencies.has(baseCurrency) && peer.verifiedCurrencies.has(quoteCurrency)) { peer.activePairs.add(pairId); verifiedPairs.push(pairId); } diff --git a/lib/p2p/Peer.ts b/lib/p2p/Peer.ts index 59ddfd3c7..8564a8b01 100644 --- a/lib/p2p/Peer.ts +++ b/lib/p2p/Peer.ts @@ -66,6 +66,8 @@ class Peer extends EventEmitter { public discoverTimer?: NodeJS.Timer; /** Trading pairs advertised by this peer which we have verified that we can swap. */ public activePairs = new Set(); + /** Currencies that we have verified that we can swap with for this peer. */ + public verifiedCurrencies = new Set(); /** Whether we have received and authenticated a [[SessionInitPacket]] from the peer. */ private opened = false; private opening = false; @@ -341,6 +343,9 @@ class Peer extends EventEmitter { throw new Error('cannot deactivate a trading pair before handshake is complete'); } if (this.activePairs.delete(pairId)) { + const [baseCurrency, quoteCurrency] = pairId.split('/'); + this.verifiedCurrencies.delete(baseCurrency); + this.verifiedCurrencies.delete(quoteCurrency); this.emit('pairDropped', pairId); } // TODO: notify peer that we have deactivated this pair?