aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-09-13 11:09:33 -0300
committerSebastian <sebasjm@gmail.com>2023-09-13 11:09:33 -0300
commita654c88a584b1249d1e60c0b8de0aeff72e5979e (patch)
tree74cd027ce6cba01aec3096f7e60c4eafa6c699ae
parentfabe92195ac5a8307b085713929dfea2b4d29a01 (diff)
failing p2p test case
-rw-r--r--packages/taler-wallet-core/src/dbless.ts2
-rw-r--r--packages/taler-wallet-core/src/util/coinSelection.test.ts64
-rw-r--r--packages/taler-wallet-core/src/util/coinSelection.ts6
3 files changed, 71 insertions, 1 deletions
diff --git a/packages/taler-wallet-core/src/dbless.ts b/packages/taler-wallet-core/src/dbless.ts
index 65c293bdf..d70eab888 100644
--- a/packages/taler-wallet-core/src/dbless.ts
+++ b/packages/taler-wallet-core/src/dbless.ts
@@ -31,6 +31,7 @@ import {
AmountJson,
Amounts,
AmountString,
+ BankAccessApiClient,
codecForAny,
codecForBankWithdrawalOperationPostResponse,
codecForBatchDepositSuccess,
@@ -53,7 +54,6 @@ import {
HttpRequestLibrary,
readSuccessResponseJsonOrThrow,
} from "@gnu-taler/taler-util/http";
-import { BankAccessApiClient } from "../../taler-util/src/bank-api-client.js";
import { TalerCryptoInterface } from "./crypto/cryptoImplementation.js";
import { DenominationRecord } from "./db.js";
import { isWithdrawableDenom } from "./index.js";
diff --git a/packages/taler-wallet-core/src/util/coinSelection.test.ts b/packages/taler-wallet-core/src/util/coinSelection.test.ts
index b907eb160..f678e75e7 100644
--- a/packages/taler-wallet-core/src/util/coinSelection.test.ts
+++ b/packages/taler-wallet-core/src/util/coinSelection.test.ts
@@ -18,8 +18,72 @@ import {
AgeRestriction,
AmountJson,
Amounts,
+ DenomKeyType,
Duration,
TransactionAmountMode,
} from "@gnu-taler/taler-util";
import test, { ExecutionContext } from "ava";
+import { testing_greedySelectPeer } from "./coinSelection.js"
+type Tester<T> = {
+ deep: {
+ equal(another: T): ReturnType<ExecutionContext["deepEqual"]>;
+ equals(another: T): ReturnType<ExecutionContext["deepEqual"]>;
+ }
+}
+
+function expect<T>(t: ExecutionContext, thing: T): Tester<T> {
+ return {
+ deep: {
+ equal: (another: T) => t.deepEqual(thing, another),
+ equals: (another: T) => t.deepEqual(thing, another),
+ },
+ };
+}
+
+const inTheDistantFuture = AbsoluteTime.toProtocolTimestamp(
+ AbsoluteTime.addDuration(AbsoluteTime.now(), Duration.fromSpec({ hours: 1 }))
+)
+const inThePast = AbsoluteTime.toProtocolTimestamp(
+ AbsoluteTime.subtractDuraction(AbsoluteTime.now(), Duration.fromSpec({ hours: 1 }))
+)
+test("should select the coin", (t) => {
+ const instructedAmount = Amounts.parseOrThrow("LOCAL:2")
+ const tally = {
+ amountAcc: Amounts.zeroOfCurrency(instructedAmount.currency),
+ depositFeesAcc: Amounts.zeroOfCurrency(instructedAmount.currency),
+ lastDepositFee: Amounts.zeroOfCurrency(instructedAmount.currency),
+ };
+ const coins = testing_greedySelectPeer(
+ // candidates available
+ [{
+ "denomPub": {
+ "age_mask": 0,
+ "cipher": DenomKeyType.Rsa,
+ "rsa_public_key": "PPP"
+ },
+ "denomPubHash": "XXX",
+ "value": "LOCAL:10",
+ "feeDeposit": "LOCAL:0.1",
+ "feeRefresh": "LOCAL:0",
+ "feeRefund": "LOCAL:0",
+ "feeWithdraw": "LOCAL:0",
+ "stampExpireDeposit": inTheDistantFuture,
+ "stampExpireLegal": inTheDistantFuture,
+ "stampExpireWithdraw": inTheDistantFuture,
+ "stampStart": inThePast,
+ "exchangeBaseUrl": "http://exchange.localhost/",
+ "numAvailable": 5,
+ "maxAge": 32
+ }],
+ instructedAmount, tally);
+
+ expect(t, coins).deep.equal({
+ "XXX;32;http://exchange.localhost/": {
+ exchangeBaseUrl: "http://exchange.localhost/",
+ denomPubHash: "XXX",
+ maxAge: 32,
+ contributions: [Amounts.parseOrThrow("LOCAL:2")],
+ }
+ });
+}); \ No newline at end of file
diff --git a/packages/taler-wallet-core/src/util/coinSelection.ts b/packages/taler-wallet-core/src/util/coinSelection.ts
index 6fd0f1b86..b8ce5e0f2 100644
--- a/packages/taler-wallet-core/src/util/coinSelection.ts
+++ b/packages/taler-wallet-core/src/util/coinSelection.ts
@@ -894,6 +894,12 @@ interface PeerCoinSelectionTally {
lastDepositFee: AmountJson;
}
+/**
+ * exporting for testing
+ */
+export function testing_greedySelectPeer(...args: Parameters<typeof greedySelectPeer>): ReturnType<typeof greedySelectPeer> {
+ return greedySelectPeer(...args)
+}
function greedySelectPeer(
candidates: AvailableDenom[],
instructedAmount: AmountLike,