failing p2p test case

This commit is contained in:
Sebastian 2023-09-13 11:09:33 -03:00
parent fabe92195a
commit a654c88a58
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069
3 changed files with 71 additions and 1 deletions

View File

@ -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";

View File

@ -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")],
}
});
});

View File

@ -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,