automatic refresh with resume

This commit is contained in:
Florian Dold 2016-10-17 23:49:04 +02:00
parent 8c0c4b5331
commit 78ea2ba5b1
4 changed files with 71 additions and 14 deletions

View File

@ -335,6 +335,7 @@ namespace RpcFunctions {
norevealIndex: undefined, norevealIndex: undefined,
exchangeBaseUrl, exchangeBaseUrl,
transferPrivs, transferPrivs,
finished: false,
}; };
return refreshSession; return refreshSession;

View File

@ -226,6 +226,8 @@ export interface RefreshSession {
hash: string; hash: string;
exchangeBaseUrl: string; exchangeBaseUrl: string;
finished: boolean;
} }

View File

@ -380,6 +380,21 @@ export class Wallet {
console.log("resuming precoin"); console.log("resuming precoin");
this.processPreCoin(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"); console.error("Unable to confirm reserve, not found in DB");
return; return;
} }
console.log("reserve confirmed");
const historyEntry = { const historyEntry = {
type: "confirm-reserve", type: "confirm-reserve",
timestamp: now, 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. // FIXME: this is not running in a transaction.
let coin = await this.q().get<Coin>("coins", oldCoinPub); let coin = await this.q().get<Coin>("coins", oldCoinPub);
if (!coin) { if (!coin) {
console.error("coin not found"); throw Error("coin not found");
return;
} }
let exchange = await this.q().get<IExchangeInfo>("exchanges", let exchange = await this.q().get<IExchangeInfo>("exchanges",
@ -1145,7 +1161,7 @@ export class Wallet {
if (newCoinDenoms.length == 0) { if (newCoinDenoms.length == 0) {
console.log("not refreshing, value too small"); console.log("not refreshing, value too small");
return; return undefined;
} }
@ -1165,13 +1181,41 @@ export class Wallet {
.put("coins", coin) .put("coins", coin)
.finish(); .finish();
await this.refreshMelt(refreshSession); return refreshSession;
}
let r = await this.q().get<RefreshSession>("refresh", oldCoinPub);
if (!r) { async refresh(oldCoinPub: string): Promise<void> {
throw Error("refresh session does not exist anymore"); 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"); 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) { if (!exchange) {
console.error(`exchange ${refreshSession.exchangeBaseUrl} not found`); console.error(`exchange ${refreshSession.exchangeBaseUrl} not found`);
return; return;
} }
let coins: Coin[] = [];
for (let i = 0; i < respJson.ev_sigs.length; i++) { for (let i = 0; i < respJson.ev_sigs.length; i++) {
let denom = exchange.all_denoms.find((d) => d.denom_pub == refreshSession.newDenoms[i]); let denom = exchange.all_denoms.find((d) => d.denom_pub == refreshSession.newDenoms[i]);
if (!denom) { if (!denom) {
@ -1290,8 +1337,15 @@ export class Wallet {
transactionPending: false, 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> { async paymentSucceeded(contractHash: string): Promise<any> {
const doPaymentSucceeded = async () => { const doPaymentSucceeded = async() => {
let t = await this.q().get<Transaction>("transactions", contractHash); let t = await this.q().get<Transaction>("transactions", contractHash);
if (!t) { if (!t) {
console.error("contract not found"); console.error("contract not found");
@ -1398,4 +1452,4 @@ export class Wallet {
doPaymentSucceeded(); doPaymentSucceeded();
return; return;
} }
} }

@ -1 +1 @@
Subproject commit 02271055cbe9e62cffb3713d109a77015df625d1 Subproject commit d4de1c912ecaac7991067027b352de61b237c0c9