Skip to content

Commit

Permalink
fix(swaps): don't retry verified currency sanity
Browse files Browse the repository at this point in the history
This modifies the logic for determining which currencies to perform
sanity swaps for. Previously, if a sanity swap failed for either
currency in a trading pair, when that trading pair was retried we would
retry sanity swaps for both currencies in the pair including the one
that worked previously. This change tracks currencies for which we
have had a successful sanity swap for and prevents retrying sanity
swaps for that currency - until we encounter an error that deactivates
a trading pair that includes that currency.

Fixes #946.
  • Loading branch information
sangaman committed May 15, 2019
1 parent eb14c76 commit b8bb45a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
47 changes: 24 additions & 23 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,41 +702,42 @@ class OrderBook extends EventEmitter {
const currenciesToVerify = new Set<string>();
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<string>();
const verifiedPairs: string[] = [];
const sanitySwapPromises: Promise<void>[] = [];

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<void>(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<void>(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);

// 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);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/p2p/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
/** Currencies that we have verified that we can swap with for this peer. */
public verifiedCurrencies = new Set<string>();
/** Whether we have received and authenticated a [[SessionInitPacket]] from the peer. */
private opened = false;
private opening = false;
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit b8bb45a

Please sign in to comment.