start using async

This commit is contained in:
Florian Dold 2016-09-28 17:52:36 +02:00
parent af10553b32
commit 9b37ee6ba6

View File

@ -242,16 +242,9 @@ function getWithdrawDenomList(amountAvailable: AmountJson,
let remaining = Amounts.copy(amountAvailable); let remaining = Amounts.copy(amountAvailable);
const ds: Denomination[] = []; const ds: Denomination[] = [];
console.log("available denoms");
console.log(denoms);
denoms = denoms.filter(isWithdrawableDenom); denoms = denoms.filter(isWithdrawableDenom);
denoms.sort((d1, d2) => Amounts.cmp(d2.value, d1.value)); denoms.sort((d1, d2) => Amounts.cmp(d2.value, d1.value));
console.log("withdrawable denoms");
console.log(denoms);
// This is an arbitrary number of coins // This is an arbitrary number of coins
// we can withdraw in one go. It's not clear if this limit // we can withdraw in one go. It's not clear if this limit
// is useful ... // is useful ...
@ -635,9 +628,11 @@ export class Wallet {
// Don't show progress while we're sleeping // Don't show progress while we're sleeping
this.stopOperation(opId); this.stopOperation(opId);
// random, exponential backoff truncated at 3 minutes // random, exponential backoff truncated at 3 minutes
let nextDelay = Math.min(2 * retryDelayMs + retryDelayMs * Math.random(), 3000 * 60); let nextDelay = Math.min(2 * retryDelayMs + retryDelayMs * Math.random(),
3000 * 60);
console.warn(`Failed to deplete reserve, trying again in ${retryDelayMs} ms`); console.warn(`Failed to deplete reserve, trying again in ${retryDelayMs} ms`);
setTimeout(() => this.processReserve(reserveRecord, nextDelay), retryDelayMs); setTimeout(() => this.processReserve(reserveRecord, nextDelay),
retryDelayMs);
}); });
} }
@ -646,11 +641,14 @@ export class Wallet {
this.withdrawExecute(preCoin) this.withdrawExecute(preCoin)
.then((c) => this.storeCoin(c)) .then((c) => this.storeCoin(c))
.catch((e) => { .catch((e) => {
console.error("Failed to withdraw coin from precoin, retrying in", retryDelayMs, "ms"); console.error("Failed to withdraw coin from precoin, retrying in",
retryDelayMs,
"ms");
console.error(e); console.error(e);
// exponential backoff truncated at one minute // exponential backoff truncated at one minute
let nextRetryDelayMs = Math.min(retryDelayMs * 2, 1000 * 60); let nextRetryDelayMs = Math.min(retryDelayMs * 2, 1000 * 60);
setTimeout(() => this.processPreCoin(preCoin, nextRetryDelayMs), retryDelayMs); setTimeout(() => this.processPreCoin(preCoin, nextRetryDelayMs),
retryDelayMs);
}); });
} }
@ -813,22 +811,14 @@ export class Wallet {
/** /**
* Withdraw coins from a reserve until it is empty. * Withdraw coins from a reserve until it is empty.
*/ */
private depleteReserve(reserve: any, exchange: IExchangeInfo): Promise<void> { private async depleteReserve(reserve: any,
exchange: IExchangeInfo): Promise<void> {
let denomsAvailable: Denomination[] = copy(exchange.active_denoms); let denomsAvailable: Denomination[] = copy(exchange.active_denoms);
let denomsForWithdraw = getWithdrawDenomList(reserve.current_amount, let denomsForWithdraw = getWithdrawDenomList(reserve.current_amount,
denomsAvailable); denomsAvailable);
// Number of coins we try to withdraw at once let ps = denomsForWithdraw.map((denom) => this.withdraw(denom, reserve));
const concurrency = 1; await Promise.all(ps);
let ps = denomsForWithdraw.map((denom) => {
console.log("withdrawing", JSON.stringify(denom));
// Do the withdraw asynchronously, so crypto is interleaved
// with requests
return this.withdraw(denom, reserve);
});
return Promise.all(ps).then(() => void 0);
} }
@ -1086,33 +1076,33 @@ export class Wallet {
/** /**
* Retrive the full event history for this wallet. * Retrive the full event history for this wallet.
*/ */
getHistory(): Promise<any> { async getHistory(): Promise<any> {
function collect(x: any, acc: any) { function collect(x: any, acc: any) {
acc.push(x); acc.push(x);
return acc; return acc;
} }
return Query(this.db) let history = await
Query(this.db)
.iter("history", {indexName: "timestamp"}) .iter("history", {indexName: "timestamp"})
.reduce(collect, []) .reduce(collect, []);
.then(acc => ({history: acc}));
return {history};
} }
/** /**
* Check if there's an equivalent contract we've already purchased. * Check if there's an equivalent contract we've already purchased.
*/ */
checkRepurchase(contract: Contract): Promise<CheckRepurchaseResult> { async checkRepurchase(contract: Contract): Promise<CheckRepurchaseResult> {
if (!contract.repurchase_correlation_id) { if (!contract.repurchase_correlation_id) {
console.log("no repurchase: no correlation id"); console.log("no repurchase: no correlation id");
return Promise.resolve({isRepurchase: false}); return {isRepurchase: false};
} }
return Query(this.db) let result: Transaction = await Query(this.db)
.getIndexed("transactions", .getIndexed("transactions",
"repurchase", "repurchase",
[contract.merchant_pub, contract.repurchase_correlation_id]) [contract.merchant_pub, contract.repurchase_correlation_id]);
.then((result: Transaction) => {
console.log("db result", result);
let isRepurchase: boolean;
if (result) { if (result) {
console.assert(result.contract.repurchase_correlation_id == contract.repurchase_correlation_id); console.assert(result.contract.repurchase_correlation_id == contract.repurchase_correlation_id);
return { return {
@ -1123,6 +1113,5 @@ export class Wallet {
} else { } else {
return {isRepurchase: false}; return {isRepurchase: false};
} }
});
} }
} }