aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-10-17 23:49:04 +0200
committerFlorian Dold <florian.dold@gmail.com>2016-10-17 23:49:04 +0200
commit78ea2ba5b110bc36d8625a1dd524257906788552 (patch)
treef60ce522e2af61f21d66a66e72fd32b881957398 /lib
parent8c0c4b5331d3cb467bb24c253fae3ca623ba5205 (diff)
downloadwallet-core-78ea2ba5b110bc36d8625a1dd524257906788552.tar.xz
automatic refresh with resume
Diffstat (limited to 'lib')
-rw-r--r--lib/wallet/cryptoLib.ts1
-rw-r--r--lib/wallet/types.ts2
-rw-r--r--lib/wallet/wallet.ts80
3 files changed, 70 insertions, 13 deletions
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<void> {
+ async createRefreshSession(oldCoinPub: string): Promise<RefreshSession|undefined> {
+
// FIXME: this is not running in a transaction.
let coin = await this.q().get<Coin>("coins", oldCoinPub);
if (!coin) {
- console.error("coin not found");
- return;
+ throw Error("coin not found");
}
let exchange = await this.q().get<IExchangeInfo>("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<RefreshSession>("refresh", oldCoinPub);
- if (!r) {
- throw Error("refresh session does not exist anymore");
+
+ async refresh(oldCoinPub: string): Promise<void> {
+ let refreshSession: RefreshSession|undefined;
+ let oldSession = await this.q().get<RefreshSession>("refresh", oldCoinPub);
+ if (oldSession) {
+ refreshSession = oldSession;
+ } else {
+ refreshSession = await this.q().get<RefreshSession>("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<RefreshSession>("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<IExchangeInfo>("exchanges", refreshSession.exchangeBaseUrl);
+ let exchange = await this.q().get<IExchangeInfo>("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<any> {
- const doPaymentSucceeded = async () => {
+ const doPaymentSucceeded = async() => {
let t = await this.q().get<Transaction>("transactions", contractHash);
if (!t) {
console.error("contract not found");
@@ -1398,4 +1452,4 @@ export class Wallet {
doPaymentSucceeded();
return;
}
-}
+} \ No newline at end of file