harness: fix/modernize peer-to-peer-push

This commit is contained in:
Florian Dold 2023-06-21 08:17:25 +02:00
parent 7dcfd02dae
commit 9f8faed2d1
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 64 additions and 23 deletions

View File

@ -53,6 +53,9 @@ import {
HarnessExchangeBankAccount, HarnessExchangeBankAccount,
openPromise, openPromise,
WalletCoreApiClient, WalletCoreApiClient,
WalletCoreRequestType,
WalletCoreResponseType,
WalletOperations,
} from "@gnu-taler/taler-wallet-core"; } from "@gnu-taler/taler-wallet-core";
import { deepStrictEqual } from "assert"; import { deepStrictEqual } from "assert";
import axiosImp, { AxiosError } from "axios"; import axiosImp, { AxiosError } from "axios";
@ -2247,6 +2250,16 @@ export class WalletClient {
remoteWallet: RemoteWallet | undefined = undefined; remoteWallet: RemoteWallet | undefined = undefined;
private waiter: WalletNotificationWaiter = makeNotificationWaiter(); private waiter: WalletNotificationWaiter = makeNotificationWaiter();
async call<Op extends keyof WalletOperations>(
operation: Op,
payload: WalletCoreRequestType<Op>,
): Promise<WalletCoreResponseType<Op>> {
if (!this.remoteWallet) {
throw Error("wallet not connected");
}
const client = getClientFromRemoteWallet(this.remoteWallet);
return client.call(operation, payload);
}
constructor(private args: WalletClientArgs) {} constructor(private args: WalletClientArgs) {}
async connect(): Promise<void> { async connect(): Promise<void> {

View File

@ -17,35 +17,55 @@
/** /**
* Imports. * Imports.
*/ */
import { AbsoluteTime, Duration, j2s } from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { GlobalTestState, WalletCli } from "../harness/harness.js";
import { import {
createSimpleTestkudosEnvironment, AbsoluteTime,
withdrawViaBank, Duration,
NotificationType,
TransactionMajorState,
TransactionMinorState,
WalletNotification,
j2s,
} from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { GlobalTestState } from "../harness/harness.js";
import {
createSimpleTestkudosEnvironmentV2,
createWalletDaemonWithClient,
withdrawViaBankV2,
} from "../harness/helpers.js"; } from "../harness/helpers.js";
/** /**
* Run test for basic, bank-integrated withdrawal and payment. * Run a test for basic peer-push payments.
*/ */
export async function runPeerToPeerPushTest(t: GlobalTestState) { export async function runPeerToPeerPushTest(t: GlobalTestState) {
// Set up test environment const { bank, exchange } = await createSimpleTestkudosEnvironmentV2(t);
const { bank, exchange } = await createSimpleTestkudosEnvironment(t); let allW1Notifications: WalletNotification[] = [];
let allW2Notifications: WalletNotification[] = [];
const wallet1 = new WalletCli(t, "w1"); const w1 = await createWalletDaemonWithClient(t, {
const wallet2 = new WalletCli(t, "w2"); name: "w1",
handleNotification(wn) {
allW1Notifications.push(wn);
},
});
const w2 = await createWalletDaemonWithClient(t, {
name: "w2",
handleNotification(wn) {
allW2Notifications.push(wn);
},
});
// Withdraw digital cash into the wallet. // Withdraw digital cash into the wallet.
await withdrawViaBank(t, { const withdrawRes = await withdrawViaBankV2(t, {
wallet: wallet1, walletClient: w1.walletClient,
bank, bank,
exchange, exchange,
amount: "TESTKUDOS:20", amount: "TESTKUDOS:20",
}); });
await wallet1.runUntilDone(); await withdrawRes.withdrawalFinishedCond;
const purse_expiration = AbsoluteTime.toProtocolTimestamp( const purse_expiration = AbsoluteTime.toProtocolTimestamp(
AbsoluteTime.addDuration( AbsoluteTime.addDuration(
@ -55,7 +75,7 @@ export async function runPeerToPeerPushTest(t: GlobalTestState) {
); );
{ {
const resp = await wallet1.client.call( const resp = await w1.walletClient.call(
WalletApiOperation.InitiatePeerPushDebit, WalletApiOperation.InitiatePeerPushDebit,
{ {
partialContractTerms: { partialContractTerms: {
@ -68,7 +88,8 @@ export async function runPeerToPeerPushTest(t: GlobalTestState) {
console.log(resp); console.log(resp);
} }
const resp = await wallet1.client.call(
const resp = await w1.walletClient.call(
WalletApiOperation.InitiatePeerPushDebit, WalletApiOperation.InitiatePeerPushDebit,
{ {
partialContractTerms: { partialContractTerms: {
@ -81,7 +102,17 @@ export async function runPeerToPeerPushTest(t: GlobalTestState) {
console.log(resp); console.log(resp);
const checkResp = await wallet2.client.call( const peerPushReadyCond = w1.walletClient.waitForNotificationCond(
(x) =>
x.type === NotificationType.TransactionStateTransition &&
x.newTxState.major === TransactionMajorState.Pending &&
x.newTxState.minor === TransactionMinorState.Ready &&
x.transactionId === resp.transactionId,
);
await peerPushReadyCond;
const checkResp = await w2.walletClient.call(
WalletApiOperation.PreparePeerPushCredit, WalletApiOperation.PreparePeerPushCredit,
{ {
talerUri: resp.talerUri, talerUri: resp.talerUri,
@ -90,7 +121,7 @@ export async function runPeerToPeerPushTest(t: GlobalTestState) {
console.log(checkResp); console.log(checkResp);
const acceptResp = await wallet2.client.call( const acceptResp = await w2.walletClient.call(
WalletApiOperation.ConfirmPeerPushCredit, WalletApiOperation.ConfirmPeerPushCredit,
{ {
peerPushPaymentIncomingId: checkResp.peerPushPaymentIncomingId, peerPushPaymentIncomingId: checkResp.peerPushPaymentIncomingId,
@ -99,14 +130,11 @@ export async function runPeerToPeerPushTest(t: GlobalTestState) {
console.log(acceptResp); console.log(acceptResp);
await wallet1.runUntilDone(); const txn1 = await w1.walletClient.call(
await wallet2.runUntilDone();
const txn1 = await wallet1.client.call(
WalletApiOperation.GetTransactions, WalletApiOperation.GetTransactions,
{}, {},
); );
const txn2 = await wallet2.client.call( const txn2 = await w2.walletClient.call(
WalletApiOperation.GetTransactions, WalletApiOperation.GetTransactions,
{}, {},
); );
@ -115,7 +143,7 @@ export async function runPeerToPeerPushTest(t: GlobalTestState) {
console.log(`txn2: ${j2s(txn2)}`); console.log(`txn2: ${j2s(txn2)}`);
const ex1 = await t.assertThrowsTalerErrorAsync(async () => { const ex1 = await t.assertThrowsTalerErrorAsync(async () => {
await wallet1.client.call(WalletApiOperation.InitiatePeerPushDebit, { await w1.walletClient.call(WalletApiOperation.InitiatePeerPushDebit, {
partialContractTerms: { partialContractTerms: {
summary: "(this will fail)", summary: "(this will fail)",
amount: "TESTKUDOS:15", amount: "TESTKUDOS:15",