make sure that refreshing works after refund

This commit is contained in:
Florian Dold 2017-08-27 05:42:46 +02:00
parent ccc6d82242
commit 63914ab53b
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
3 changed files with 42 additions and 18 deletions

View File

@ -547,9 +547,18 @@ export class QueryRoot {
private finished: boolean = false; private finished: boolean = false;
private keys: { [keyName: string]: IDBValidKey } = {};
constructor(public db: IDBDatabase) { 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() { private checkFinished() {
if (this.finished) { if (this.finished) {
throw Error("Can't add work to query after it was started"); 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 * Overrides if an existing object with the same key exists
* in the store. * in the store.
*/ */
put<T>(store: Store<T>, val: T): QueryRoot { put<T>(store: Store<T>, val: T, keyName?: string): QueryRoot {
this.checkFinished(); this.checkFinished();
const doPut = (tx: IDBTransaction) => { 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.scheduleFinish();
this.addWork(doPut, store.name, true); this.addWork(doPut, store.name, true);

View File

@ -759,6 +759,11 @@ export interface RefreshSessionRecord {
* Is this session finished? * Is this session finished?
*/ */
finished: boolean; finished: boolean;
/**
* Record ID when retrieved from the DB.
*/
id?: number;
} }

View File

@ -555,7 +555,7 @@ export namespace Stores {
export const nonces = new NonceStore(); export const nonces = new NonceStore();
export const precoins = new Store<PreCoinRecord>("precoins", {keyPath: "coinPub"}); export const precoins = new Store<PreCoinRecord>("precoins", {keyPath: "coinPub"});
export const proposals = new ProposalsStore(); export const proposals = new ProposalsStore();
export const refresh = new Store<RefreshSessionRecord>("refresh", {keyPath: "meltCoinPub"}); export const refresh = new Store<RefreshSessionRecord>("refresh", {keyPath: "id", autoIncrement: true});
export const reserves = new Store<ReserveRecord>("reserves", {keyPath: "reserve_pub"}); export const reserves = new Store<ReserveRecord>("reserves", {keyPath: "reserve_pub"});
export const purchases = new PurchasesStore(); export const purchases = new PurchasesStore();
} }
@ -1836,7 +1836,7 @@ export class Wallet {
if (c.suspended) { if (c.suspended) {
return balance; return balance;
} }
if (!(c.status === CoinStatus.Dirty || c.status === CoinStatus.Fresh)) { if (!(c.status === CoinStatus.Fresh)) {
return balance; return balance;
} }
console.log("collecting balance"); console.log("collecting balance");
@ -1999,25 +1999,30 @@ export class Wallet {
// Store refresh session and subtract refreshed amount from // Store refresh session and subtract refreshed amount from
// coin in the same transaction. // coin in the same transaction.
await this.q() const query = this.q();
.put(Stores.refresh, refreshSession) query.put(Stores.refresh, refreshSession, "refreshKey")
.mutate(Stores.coins, coin.coinPub, mutateCoin) .mutate(Stores.coins, coin.coinPub, mutateCoin);
.finish(); await query.finish();
const key = query.key("refreshKey");
if (!key || typeof key !== "number") {
throw Error("insert failed");
}
refreshSession.id = key;
return refreshSession; return refreshSession;
} }
async refresh(oldCoinPub: string): Promise<void> { async refresh(oldCoinPub: string): Promise<void> {
let refreshSession: RefreshSessionRecord|undefined;
const oldSession = await this.q().get(Stores.refresh, oldCoinPub); const oldRefreshSessions = await this.q().iter(Stores.refresh).toArray();
if (oldSession) { for (const session of oldRefreshSessions) {
console.log("got old session for", oldCoinPub); console.log("got old session for", oldCoinPub, session);
console.log(oldSession); this.continueRefreshSession(session);
refreshSession = oldSession;
} else {
refreshSession = await this.createRefreshSession(oldCoinPub);
} }
let refreshSession = await this.createRefreshSession(oldCoinPub);
if (!refreshSession) { if (!refreshSession) {
// refreshing not necessary // refreshing not necessary
console.log("not refreshing", oldCoinPub); console.log("not refreshing", oldCoinPub);
@ -2031,9 +2036,8 @@ export class Wallet {
return; return;
} }
if (typeof refreshSession.norevealIndex !== "number") { if (typeof refreshSession.norevealIndex !== "number") {
const coinPub = refreshSession.meltCoinPub;
await this.refreshMelt(refreshSession); await this.refreshMelt(refreshSession);
const r = await this.q().get<RefreshSessionRecord>(Stores.refresh, coinPub); const r = await this.q().get<RefreshSessionRecord>(Stores.refresh, refreshSession.id);
if (!r) { if (!r) {
throw Error("refresh session does not exist anymore"); 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`); console.error(`Exchange ${req.exchange} not known to the wallet`);
return; return;
} }
console.log("selecting coins for return:", req);
const cds = await this.getCoinsForReturn(req.exchange, req.amount); const cds = await this.getCoinsForReturn(req.exchange, req.amount);
console.log(cds); console.log(cds);