wallet-core: handle suspended coins properly in refresh

This commit is contained in:
Florian Dold 2022-09-19 14:41:50 +02:00
parent 97267e7d1f
commit ffe6a95214
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 26 additions and 18 deletions

View File

@ -67,6 +67,7 @@ import {
EXCHANGE_COINS_LOCK, EXCHANGE_COINS_LOCK,
InternalWalletState, InternalWalletState,
} from "../internal-wallet-state.js"; } from "../internal-wallet-state.js";
import { assertUnreachable } from "../util/assertUnreachable.js";
import { import {
readSuccessResponseJsonOrThrow, readSuccessResponseJsonOrThrow,
readUnexpectedResponseDetails, readUnexpectedResponseDetails,
@ -879,17 +880,30 @@ export async function createRefreshGroup(
!!denom, !!denom,
"denomination for existing coin must be in database", "denomination for existing coin must be in database",
); );
if (coin.status !== CoinStatus.Dormant) { switch (coin.status) {
coin.status = CoinStatus.Dormant; case CoinStatus.Dormant:
const coinAv = await tx.coinAvailability.get([ break;
coin.exchangeBaseUrl, case CoinStatus.Fresh: {
coin.denomPubHash, coin.status = CoinStatus.Dormant;
coin.maxAge, const coinAv = await tx.coinAvailability.get([
]); coin.exchangeBaseUrl,
checkDbInvariant(!!coinAv); coin.denomPubHash,
checkDbInvariant(coinAv.freshCoinCount > 0); coin.maxAge,
coinAv.freshCoinCount--; ]);
await tx.coinAvailability.put(coinAv); checkDbInvariant(!!coinAv);
checkDbInvariant(coinAv.freshCoinCount > 0);
coinAv.freshCoinCount--;
await tx.coinAvailability.put(coinAv);
break;
}
case CoinStatus.FreshSuspended: {
// For suspended coins, we don't have to adjust coin
// availability, as they are not counted as available.
coin.status = CoinStatus.Dormant;
break;
}
default:
assertUnreachable(coin.status);
} }
const refreshAmount = coin.currentAmount; const refreshAmount = coin.currentAmount;
inputPerCoin.push(refreshAmount); inputPerCoin.push(refreshAmount);

View File

@ -937,10 +937,7 @@ async function setCoinSuspended(
if (c.status !== CoinStatus.Fresh) { if (c.status !== CoinStatus.Fresh) {
return; return;
} }
if ( if (coinAvailability.freshCoinCount === 0) {
coinAvailability.freshCoinCount == null ||
coinAvailability.freshCoinCount === 0
) {
throw Error( throw Error(
`invalid coin count ${coinAvailability.freshCoinCount} in DB`, `invalid coin count ${coinAvailability.freshCoinCount} in DB`,
); );
@ -951,9 +948,6 @@ async function setCoinSuspended(
if (c.status == CoinStatus.Dormant) { if (c.status == CoinStatus.Dormant) {
return; return;
} }
if (coinAvailability.freshCoinCount == null) {
coinAvailability.freshCoinCount = 0;
}
coinAvailability.freshCoinCount++; coinAvailability.freshCoinCount++;
c.status = CoinStatus.Fresh; c.status = CoinStatus.Fresh;
} }