From 78ea2ba5b110bc36d8625a1dd524257906788552 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 17 Oct 2016 23:49:04 +0200 Subject: automatic refresh with resume --- lib/wallet/cryptoLib.ts | 1 + lib/wallet/types.ts | 2 ++ lib/wallet/wallet.ts | 80 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 70 insertions(+), 13 deletions(-) (limited to 'lib/wallet') diff --git a/lib/wallet/cryptoLib.ts b/lib/wallet/cryptoLib.ts index 2782327ac..db82b5cf4 100644 --- a/lib/wallet/cryptoLib.ts +++ b/lib/wallet/cryptoLib.ts @@ -335,6 +335,7 @@ namespace RpcFunctions { norevealIndex: undefined, exchangeBaseUrl, transferPrivs, + finished: false, }; return refreshSession; diff --git a/lib/wallet/types.ts b/lib/wallet/types.ts index 9d634618a..5beff72bd 100644 --- a/lib/wallet/types.ts +++ b/lib/wallet/types.ts @@ -226,6 +226,8 @@ export interface RefreshSession { hash: string; exchangeBaseUrl: string; + + finished: boolean; } diff --git a/lib/wallet/wallet.ts b/lib/wallet/wallet.ts index 2c5210607..85fd8c6f0 100644 --- a/lib/wallet/wallet.ts +++ b/lib/wallet/wallet.ts @@ -380,6 +380,21 @@ export class Wallet { console.log("resuming precoin"); this.processPreCoin(preCoin); }); + + this.q() + .iter("refresh") + .reduce((r: RefreshSession) => { + this.continueRefreshSession(r); + }); + + // FIXME: optimize via index + this.q() + .iter("coins") + .reduce((c: Coin) => { + if (c.dirty && !c.transactionPending) { + this.refresh(c.coinPub); + } + }); } @@ -751,6 +766,7 @@ export class Wallet { console.error("Unable to confirm reserve, not found in DB"); return; } + console.log("reserve confirmed"); const historyEntry = { type: "confirm-reserve", timestamp: now, @@ -1111,14 +1127,14 @@ export class Wallet { } - async refresh(oldCoinPub: string): Promise { + async createRefreshSession(oldCoinPub: string): Promise { + // FIXME: this is not running in a transaction. let coin = await this.q().get("coins", oldCoinPub); if (!coin) { - console.error("coin not found"); - return; + throw Error("coin not found"); } let exchange = await this.q().get("exchanges", @@ -1145,7 +1161,7 @@ export class Wallet { if (newCoinDenoms.length == 0) { console.log("not refreshing, value too small"); - return; + return undefined; } @@ -1165,13 +1181,41 @@ export class Wallet { .put("coins", coin) .finish(); - await this.refreshMelt(refreshSession); + return refreshSession; + } - let r = await this.q().get("refresh", oldCoinPub); - if (!r) { - throw Error("refresh session does not exist anymore"); + + async refresh(oldCoinPub: string): Promise { + let refreshSession: RefreshSession|undefined; + let oldSession = await this.q().get("refresh", oldCoinPub); + if (oldSession) { + refreshSession = oldSession; + } else { + refreshSession = await this.q().get("refresh", + oldCoinPub); } - await this.refreshReveal(r); + if (!refreshSession) { + // refreshing not necessary + return; + } + this.continueRefreshSession(refreshSession); + } + + async continueRefreshSession(refreshSession: RefreshSession) { + if (refreshSession.finished) { + return; + } + if (typeof refreshSession.norevealIndex !== "number") { + let coinPub = refreshSession.meltCoinPub; + await this.refreshMelt(refreshSession); + let r = await this.q().get("refresh", coinPub); + if (!r) { + throw Error("refresh session does not exist anymore"); + } + refreshSession = r; + } + + await this.refreshReveal(refreshSession); } @@ -1263,12 +1307,15 @@ export class Wallet { console.log("/refresh/reveal did not contain ev_sigs"); } - let exchange = await this.q().get("exchanges", refreshSession.exchangeBaseUrl); + let exchange = await this.q().get("exchanges", + refreshSession.exchangeBaseUrl); if (!exchange) { console.error(`exchange ${refreshSession.exchangeBaseUrl} not found`); return; } + let coins: Coin[] = []; + for (let i = 0; i < respJson.ev_sigs.length; i++) { let denom = exchange.all_denoms.find((d) => d.denom_pub == refreshSession.newDenoms[i]); if (!denom) { @@ -1290,8 +1337,15 @@ export class Wallet { transactionPending: false, }; - await this.q().put("coins", coin).finish(); + coins.push(coin); } + + refreshSession.finished = true; + + await this.q() + .putAll("coins", coins) + .put("refresh", refreshSession) + .finish(); } @@ -1376,7 +1430,7 @@ export class Wallet { async paymentSucceeded(contractHash: string): Promise { - const doPaymentSucceeded = async () => { + const doPaymentSucceeded = async() => { let t = await this.q().get("transactions", contractHash); if (!t) { console.error("contract not found"); @@ -1398,4 +1452,4 @@ export class Wallet { doPaymentSucceeded(); return; } -} +} \ No newline at end of file -- cgit v1.2.3