generate cleaner history for recoup

This commit is contained in:
Florian Dold 2020-03-27 23:37:02 +05:30
parent 1c747ae004
commit 131d2b34d9
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 35 additions and 17 deletions

View File

@ -492,6 +492,7 @@ export async function getHistory(
type: HistoryEventType.FundsRecouped, type: HistoryEventType.FundsRecouped,
timestamp: rg.timestampFinished, timestamp: rg.timestampFinished,
eventId: makeEventId(HistoryEventType.FundsRecouped, rg.recoupGroupId), eventId: makeEventId(HistoryEventType.FundsRecouped, rg.recoupGroupId),
numCoinsRecouped: rg.coinPubs.length,
}); });
} }
}); });

View File

@ -72,10 +72,14 @@ async function incrementRecoupRetry(
} }
async function putGroupAsFinished( async function putGroupAsFinished(
ws: InternalWalletState,
tx: TransactionHandle, tx: TransactionHandle,
recoupGroup: RecoupGroupRecord, recoupGroup: RecoupGroupRecord,
coinIdx: number, coinIdx: number,
): Promise<void> { ): Promise<void> {
if (recoupGroup.timestampFinished) {
return;
}
recoupGroup.recoupFinishedPerCoin[coinIdx] = true; recoupGroup.recoupFinishedPerCoin[coinIdx] = true;
let allFinished = true; let allFinished = true;
for (const b of recoupGroup.recoupFinishedPerCoin) { for (const b of recoupGroup.recoupFinishedPerCoin) {
@ -87,6 +91,16 @@ async function putGroupAsFinished(
recoupGroup.timestampFinished = getTimestampNow(); recoupGroup.timestampFinished = getTimestampNow();
recoupGroup.retryInfo = initRetryInfo(false); recoupGroup.retryInfo = initRetryInfo(false);
recoupGroup.lastError = undefined; recoupGroup.lastError = undefined;
if (recoupGroup.scheduleRefreshCoins.length > 0) {
const refreshGroupId = await createRefreshGroup(
tx,
recoupGroup.scheduleRefreshCoins.map((x) => ({ coinPub: x })),
RefreshReason.Recoup,
);
processRefreshGroup(ws, refreshGroupId.refreshGroupId).then((e) => {
console.error("error while refreshing after recoup", e);
});
}
} }
await tx.put(Stores.recoupGroups, recoupGroup); await tx.put(Stores.recoupGroups, recoupGroup);
} }
@ -108,7 +122,7 @@ async function recoupTipCoin(
if (recoupGroup.recoupFinishedPerCoin[coinIdx]) { if (recoupGroup.recoupFinishedPerCoin[coinIdx]) {
return; return;
} }
await putGroupAsFinished(tx, recoupGroup, coinIdx); await putGroupAsFinished(ws, tx, recoupGroup, coinIdx);
}); });
} }
@ -179,7 +193,7 @@ async function recoupWithdrawCoin(
updatedReserve.reserveStatus = ReserveRecordStatus.QUERYING_STATUS; updatedReserve.reserveStatus = ReserveRecordStatus.QUERYING_STATUS;
await tx.put(Stores.coins, updatedCoin); await tx.put(Stores.coins, updatedCoin);
await tx.put(Stores.reserves, updatedReserve); await tx.put(Stores.reserves, updatedReserve);
await putGroupAsFinished(tx, recoupGroup, coinIdx); await putGroupAsFinished(ws, tx, recoupGroup, coinIdx);
}, },
); );
@ -227,7 +241,7 @@ async function recoupRefreshCoin(
return; return;
} }
const refreshGroupId = await ws.db.runWithWriteTransaction( await ws.db.runWithWriteTransaction(
[Stores.coins, Stores.reserves, Stores.recoupGroups, Stores.refreshGroups], [Stores.coins, Stores.reserves, Stores.recoupGroups, Stores.refreshGroups],
async (tx) => { async (tx) => {
const recoupGroup = await tx.get(Stores.recoupGroups, recoupGroupId); const recoupGroup = await tx.get(Stores.recoupGroups, recoupGroupId);
@ -254,22 +268,12 @@ async function recoupRefreshCoin(
"recoup: setting old coin amount to", "recoup: setting old coin amount to",
Amounts.toString(oldCoin.currentAmount), Amounts.toString(oldCoin.currentAmount),
); );
recoupGroup.scheduleRefreshCoins.push(oldCoin.coinPub);
await tx.put(Stores.coins, revokedCoin); await tx.put(Stores.coins, revokedCoin);
await tx.put(Stores.coins, oldCoin); await tx.put(Stores.coins, oldCoin);
await putGroupAsFinished(tx, recoupGroup, coinIdx); await putGroupAsFinished(ws, tx, recoupGroup, coinIdx);
return await createRefreshGroup(
tx,
[{ coinPub: oldCoin.coinPub }],
RefreshReason.Recoup,
);
}, },
); );
if (refreshGroupId) {
processRefreshGroup(ws, refreshGroupId.refreshGroupId).then((e) => {
console.error("error while refreshing after recoup", e);
});
}
} }
async function resetRecoupGroupRetry( async function resetRecoupGroupRetry(
@ -340,17 +344,18 @@ export async function createRecoupGroup(
recoupFinishedPerCoin: coinPubs.map(() => false), recoupFinishedPerCoin: coinPubs.map(() => false),
// Will be populated later // Will be populated later
oldAmountPerCoin: [], oldAmountPerCoin: [],
scheduleRefreshCoins: [],
}; };
for (let coinIdx = 0; coinIdx < coinPubs.length; coinIdx++) { for (let coinIdx = 0; coinIdx < coinPubs.length; coinIdx++) {
const coinPub = coinPubs[coinIdx]; const coinPub = coinPubs[coinIdx];
const coin = await tx.get(Stores.coins, coinPub); const coin = await tx.get(Stores.coins, coinPub);
if (!coin) { if (!coin) {
await putGroupAsFinished(tx, recoupGroup, coinIdx); await putGroupAsFinished(ws, tx, recoupGroup, coinIdx);
continue; continue;
} }
if (Amounts.isZero(coin.currentAmount)) { if (Amounts.isZero(coin.currentAmount)) {
await putGroupAsFinished(tx, recoupGroup, coinIdx); await putGroupAsFinished(ws, tx, recoupGroup, coinIdx);
continue; continue;
} }
recoupGroup.oldAmountPerCoin[coinIdx] = coin.currentAmount; recoupGroup.oldAmountPerCoin[coinIdx] = coin.currentAmount;

View File

@ -1397,8 +1397,19 @@ export interface RecoupGroupRecord {
*/ */
recoupFinishedPerCoin: boolean[]; recoupFinishedPerCoin: boolean[];
/**
* We store old amount (i.e. before recoup) of recouped coins here,
* as the balance of a recouped coin is set to zero when the
* recoup group is created.
*/
oldAmountPerCoin: AmountJson[]; oldAmountPerCoin: AmountJson[];
/**
* Public keys of coins that should be scheduled for refreshing
* after all individual recoups are done.
*/
scheduleRefreshCoins: string[];
/** /**
* Retry info. * Retry info.
*/ */

View File

@ -349,6 +349,7 @@ export interface HistoryFundsDepositedToSelfEvent {
*/ */
export interface HistoryFundsRecoupedEvent { export interface HistoryFundsRecoupedEvent {
type: HistoryEventType.FundsRecouped; type: HistoryEventType.FundsRecouped;
numCoinsRecouped: number;
} }
/** /**