From 63914ab53b18ec29269c2c3fe4e01ac9b36330e5 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sun, 27 Aug 2017 05:42:46 +0200 Subject: make sure that refreshing works after refund --- src/query.ts | 18 ++++++++++++++++-- src/types.ts | 5 +++++ src/wallet.ts | 37 +++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/query.ts b/src/query.ts index ee1ac2603..d7689f2bc 100644 --- a/src/query.ts +++ b/src/query.ts @@ -547,9 +547,18 @@ export class QueryRoot { private finished: boolean = false; + private keys: { [keyName: string]: IDBValidKey } = {}; + constructor(public db: IDBDatabase) { } + /** + * Get a named key that was created during the query. + */ + key(keyName: string): IDBValidKey|undefined { + return this.keys[keyName]; + } + private checkFinished() { if (this.finished) { throw Error("Can't add work to query after it was started"); @@ -627,10 +636,15 @@ export class QueryRoot { * Overrides if an existing object with the same key exists * in the store. */ - put(store: Store, val: T): QueryRoot { + put(store: Store, val: T, keyName?: string): QueryRoot { this.checkFinished(); const doPut = (tx: IDBTransaction) => { - tx.objectStore(store.name).put(val); + const req = tx.objectStore(store.name).put(val); + if (keyName) { + req.onsuccess = () => { + this.keys[keyName] = req.result; + }; + } }; this.scheduleFinish(); this.addWork(doPut, store.name, true); diff --git a/src/types.ts b/src/types.ts index aabf4ffc0..9492d1a75 100644 --- a/src/types.ts +++ b/src/types.ts @@ -759,6 +759,11 @@ export interface RefreshSessionRecord { * Is this session finished? */ finished: boolean; + + /** + * Record ID when retrieved from the DB. + */ + id?: number; } diff --git a/src/wallet.ts b/src/wallet.ts index 72c8a70f8..e7a36f7b7 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -555,7 +555,7 @@ export namespace Stores { export const nonces = new NonceStore(); export const precoins = new Store("precoins", {keyPath: "coinPub"}); export const proposals = new ProposalsStore(); - export const refresh = new Store("refresh", {keyPath: "meltCoinPub"}); + export const refresh = new Store("refresh", {keyPath: "id", autoIncrement: true}); export const reserves = new Store("reserves", {keyPath: "reserve_pub"}); export const purchases = new PurchasesStore(); } @@ -1836,7 +1836,7 @@ export class Wallet { if (c.suspended) { return balance; } - if (!(c.status === CoinStatus.Dirty || c.status === CoinStatus.Fresh)) { + if (!(c.status === CoinStatus.Fresh)) { return balance; } console.log("collecting balance"); @@ -1999,25 +1999,30 @@ export class Wallet { // Store refresh session and subtract refreshed amount from // coin in the same transaction. - await this.q() - .put(Stores.refresh, refreshSession) - .mutate(Stores.coins, coin.coinPub, mutateCoin) - .finish(); + const query = this.q(); + query.put(Stores.refresh, refreshSession, "refreshKey") + .mutate(Stores.coins, coin.coinPub, mutateCoin); + await query.finish(); + + const key = query.key("refreshKey"); + if (!key || typeof key !== "number") { + throw Error("insert failed"); + } + + refreshSession.id = key; return refreshSession; } async refresh(oldCoinPub: string): Promise { - let refreshSession: RefreshSessionRecord|undefined; - const oldSession = await this.q().get(Stores.refresh, oldCoinPub); - if (oldSession) { - console.log("got old session for", oldCoinPub); - console.log(oldSession); - refreshSession = oldSession; - } else { - refreshSession = await this.createRefreshSession(oldCoinPub); + + const oldRefreshSessions = await this.q().iter(Stores.refresh).toArray(); + for (const session of oldRefreshSessions) { + console.log("got old session for", oldCoinPub, session); + this.continueRefreshSession(session); } + let refreshSession = await this.createRefreshSession(oldCoinPub); if (!refreshSession) { // refreshing not necessary console.log("not refreshing", oldCoinPub); @@ -2031,9 +2036,8 @@ export class Wallet { return; } if (typeof refreshSession.norevealIndex !== "number") { - const coinPub = refreshSession.meltCoinPub; await this.refreshMelt(refreshSession); - const r = await this.q().get(Stores.refresh, coinPub); + const r = await this.q().get(Stores.refresh, refreshSession.id); if (!r) { throw Error("refresh session does not exist anymore"); } @@ -2441,6 +2445,7 @@ export class Wallet { console.error(`Exchange ${req.exchange} not known to the wallet`); return; } + console.log("selecting coins for return:", req); const cds = await this.getCoinsForReturn(req.exchange, req.amount); console.log(cds); -- cgit v1.2.3