From 4a87aad32878d72445794fa5450a2ac3eb5eaf2d Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Fri, 25 Oct 2024 20:47:25 +0200 Subject: [PATCH] aggregate callback result in TrustedCallResult --- .../worker-api/src/integriteeWorker.spec.ts | 150 +++++++++--------- packages/worker-api/src/integriteeWorker.ts | 11 +- packages/worker-api/src/interface.ts | 8 +- packages/worker-api/src/worker.ts | 35 ++-- 4 files changed, 110 insertions(+), 94 deletions(-) diff --git a/packages/worker-api/src/integriteeWorker.spec.ts b/packages/worker-api/src/integriteeWorker.spec.ts index 421b9826..37b42e8f 100644 --- a/packages/worker-api/src/integriteeWorker.spec.ts +++ b/packages/worker-api/src/integriteeWorker.spec.ts @@ -43,78 +43,78 @@ describe('worker', () => { // To my knowledge jest does not have an option to run skipped tests specifically, does it? // Todo: add proper CI to test this too. describe('needs worker and node running', () => { - describe('getWorkerPubKey', () => { - it('should return value', async () => { - const result = await worker.getShieldingKey(); - // console.log('Shielding Key', result); - expect(result).toBeDefined(); - }); - }); - - describe('getShardVault', () => { - it('should return value', async () => { - const result = await worker.getShardVault(); - console.log('ShardVault', result.toHuman()); - expect(result).toBeDefined(); - }); - }); - - describe('getNonce', () => { - it('should return value', async () => { - const result = await worker.getNonce(alice, network.shard); - console.log('Nonce', result.toHuman); - expect(result).toBeDefined(); - }); - }); - - - describe('getAccountInfo', () => { - it('should return value', async () => { - const result = await worker.getAccountInfo(alice, network.shard); - console.log('getAccountInfo', result.toHuman()); - expect(result).toBeDefined(); - }); - }); - - describe('accountInfoGetter', () => { - it('should return value', async () => { - const getter = await worker.accountInfoGetter(charlie, network.shard); - console.log(`AccountInfoGetter: ${JSON.stringify(getter)}`); - const result = await getter.send(); - console.log('getAccountInfo:', result.toHuman()); - expect(result).toBeDefined(); - }); - }); - - describe('parentchainsInfoGetter', () => { - it('should return value', async () => { - const getter = worker.parentchainsInfoGetter(network.shard); - console.log(`parentchainsInfoGetter: ${JSON.stringify(getter)}`); - const result = await getter.send(); - console.log('parentchainsInfoGetter:', result.toHuman()); - expect(result).toBeDefined(); - }); - }); - - describe('guessTheNumberInfoGetter', () => { - it('should return value', async () => { - const getter = worker.guessTheNumberInfoGetter(network.shard); - console.log(`GuessTheNumberInfo: ${JSON.stringify(getter)}`); - const result = await getter.send(); - console.log('GuessTheNumberInfo:', result.toHuman()); - expect(result).toBeDefined(); - }); - }); - - describe('guessTheNumberAttemptsGetter', () => { - it('should return value', async () => { - const getter = await worker.guessTheNumberAttemptsTrustedGetter(charlie, network.shard); - console.log(`Attempts: ${JSON.stringify(getter)}`); - const result = await getter.send(); - console.log('Attempts:', result.toHuman()); - expect(result).toBeDefined(); - }); - }); + // describe('getWorkerPubKey', () => { + // it('should return value', async () => { + // const result = await worker.getShieldingKey(); + // // console.log('Shielding Key', result); + // expect(result).toBeDefined(); + // }); + // }); + // + // describe('getShardVault', () => { + // it('should return value', async () => { + // const result = await worker.getShardVault(); + // console.log('ShardVault', result.toHuman()); + // expect(result).toBeDefined(); + // }); + // }); + // + // describe('getNonce', () => { + // it('should return value', async () => { + // const result = await worker.getNonce(alice, network.shard); + // console.log('Nonce', result.toHuman); + // expect(result).toBeDefined(); + // }); + // }); + // + // + // describe('getAccountInfo', () => { + // it('should return value', async () => { + // const result = await worker.getAccountInfo(alice, network.shard); + // console.log('getAccountInfo', result.toHuman()); + // expect(result).toBeDefined(); + // }); + // }); + // + // describe('accountInfoGetter', () => { + // it('should return value', async () => { + // const getter = await worker.accountInfoGetter(charlie, network.shard); + // console.log(`AccountInfoGetter: ${JSON.stringify(getter)}`); + // const result = await getter.send(); + // console.log('getAccountInfo:', result.toHuman()); + // expect(result).toBeDefined(); + // }); + // }); + // + // describe('parentchainsInfoGetter', () => { + // it('should return value', async () => { + // const getter = worker.parentchainsInfoGetter(network.shard); + // console.log(`parentchainsInfoGetter: ${JSON.stringify(getter)}`); + // const result = await getter.send(); + // console.log('parentchainsInfoGetter:', result.toHuman()); + // expect(result).toBeDefined(); + // }); + // }); + // + // describe('guessTheNumberInfoGetter', () => { + // it('should return value', async () => { + // const getter = worker.guessTheNumberInfoGetter(network.shard); + // console.log(`GuessTheNumberInfo: ${JSON.stringify(getter)}`); + // const result = await getter.send(); + // console.log('GuessTheNumberInfo:', result.toHuman()); + // expect(result).toBeDefined(); + // }); + // }); + // + // describe('guessTheNumberAttemptsGetter', () => { + // it('should return value', async () => { + // const getter = await worker.guessTheNumberAttemptsTrustedGetter(charlie, network.shard); + // console.log(`Attempts: ${JSON.stringify(getter)}`); + // const result = await getter.send(); + // console.log('Attempts:', result.toHuman()); + // expect(result).toBeDefined(); + // }); + // }); describe('balance transfer should work', () => { it('should return value', async () => { @@ -127,7 +127,7 @@ describe('worker', () => { charlie.address, 1100000000000 ); - console.log('balance transfer result', result.toHuman()); + console.log('balance transfer result', JSON.stringify(result)); expect(result).toBeDefined(); }); }); @@ -144,7 +144,7 @@ describe('worker', () => { // charlie.address, // 1100000000000, // ); - // console.log('balance unshield result', result.toHuman()); + // console.log('balance unshield result', JSON.stringify(result)); // expect(result).toBeDefined(); // }); // }); @@ -159,7 +159,7 @@ describe('worker', () => { // network.mrenclave, // 1, // ); - // console.log('guess the number result', result.toHuman()); + // console.log('guess the number result', JSON.stringify(result)); // expect(result).toBeDefined(); // }); // }); diff --git a/packages/worker-api/src/integriteeWorker.ts b/packages/worker-api/src/integriteeWorker.ts index b14c42da..aa5968ba 100644 --- a/packages/worker-api/src/integriteeWorker.ts +++ b/packages/worker-api/src/integriteeWorker.ts @@ -1,4 +1,3 @@ -import type {Hash} from '@polkadot/types/interfaces/runtime'; import type { ShardIdentifier, IntegriteeTrustedCallSigned, @@ -11,7 +10,7 @@ import { type TrustedGetterArgs, type TrustedSignerOptions, type PublicGetterArgs, - type PublicGetterParams, type TrustedGetterParams, + type PublicGetterParams, type TrustedGetterParams, type TrustedCallResult, } from './interface.js'; import {Worker} from "./worker.js"; import { @@ -81,7 +80,7 @@ export class IntegriteeWorker extends Worker { to: String, amount: number, signerOptions?: TrustedSignerOptions, - ): Promise { + ): Promise { const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions) const shardT = this.createType('ShardIdentifier', bs58.decode(shard)); const params = this.createType('BalanceTransferArgs', [from, to, amount]) @@ -98,7 +97,7 @@ export class IntegriteeWorker extends Worker { toPublicAddress: string, amount: number, signerOptions?: TrustedSignerOptions, - ): Promise { + ): Promise { const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions) const shardT = this.createType('ShardIdentifier', bs58.decode(shard)); @@ -114,7 +113,7 @@ export class IntegriteeWorker extends Worker { mrenclave: string, guess: number, signerOptions?: TrustedSignerOptions, - ): Promise { + ): Promise { const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions) const shardT = this.createType('ShardIdentifier', bs58.decode(shard)); @@ -127,7 +126,7 @@ export class IntegriteeWorker extends Worker { return this.sendTrustedCall(signed, shardT); } - async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier): Promise { + async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier): Promise { if (this.shieldingKey() == undefined) { console.debug(`[sentTrustedCall] Setting the shielding pubKey of the worker.`) await this.getShieldingKey(); diff --git a/packages/worker-api/src/interface.ts b/packages/worker-api/src/interface.ts index fbcaa5f6..e7cb3606 100644 --- a/packages/worker-api/src/interface.ts +++ b/packages/worker-api/src/interface.ts @@ -7,8 +7,9 @@ import type { GuessTheNumberPublicGetter, GuessTheNumberTrustedGetter, IntegriteeGetter, - ShardIdentifier + ShardIdentifier, TrustedOperationStatus } from "@encointer/types"; +import type {Hash} from "@polkadot/types/interfaces/runtime"; export interface IWorkerBase { createType: (apiType: string, obj?: any) => any; @@ -26,6 +27,11 @@ export interface GenericTop { toHex(): string } +export interface TrustedCallResult { + topHash?: Hash, + status?: TrustedOperationStatus, +} + export interface ISubmittableGetter { worker: W; diff --git a/packages/worker-api/src/worker.ts b/packages/worker-api/src/worker.ts index 812225c0..e67ec80c 100644 --- a/packages/worker-api/src/worker.ts +++ b/packages/worker-api/src/worker.ts @@ -13,7 +13,13 @@ import type { Vault } from '@encointer/types'; -import {type GenericGetter, type GenericTop, type IWorkerBase, type WorkerOptions} from './interface.js'; +import { + type GenericGetter, + type GenericTop, + type IWorkerBase, + type TrustedCallResult, + type WorkerOptions +} from './interface.js'; import {encryptWithPublicKey, parseWebCryptoRSA} from "./webCryptoRSA.js"; import type {Bytes, u8} from "@polkadot/types-codec"; import BN from "bn.js"; @@ -144,10 +150,9 @@ export class Worker implements IWorkerBase { return this.createType(returnType, value); } - async submitAndWatchTop(top: Top, shard: ShardIdentifier): Promise { + async submitAndWatchTop(top: Top, shard: ShardIdentifier): Promise { console.debug(`Sending TrustedOperation: ${JSON.stringify(top)}`); - const cyphertext = await this.encrypt(top.toU8a()); const r = this.createType( @@ -156,11 +161,9 @@ export class Worker implements IWorkerBase { const returnValue = await this.subscribe('author_submitAndWatchExtrinsic', [r.toHex()]) - // const returnValue = await this.send('author_submitExtrinsic', [r.toHex()]) - console.debug(`[sendTrustedCall] result: ${JSON.stringify(returnValue)}`); - return this.createType('Hash', returnValue.value); + return returnValue; } @@ -173,9 +176,11 @@ export class Worker implements IWorkerBase { return this.resultToRpcReturnValue(result); } - public async subscribe(method: string, params: unknown[]): Promise { + public async subscribe(method: string, params: unknown[]): Promise { await this.isReady(); + let topHash: Hash; + return new Promise( async (resolve, reject) => { const onStatusChange = (error: Error | null, result: string) => { if (error !== null) { @@ -189,16 +194,22 @@ export class Worker implements IWorkerBase { const errorMsg = this.createType('String', directRequestStatus.value); throw new Error(`DirectRequestStatus is Error ${errorMsg}`); } + if (directRequestStatus.isOk) { - // const hash = this.createType('Hash', directRequestStatus.value); - resolve({}) + resolve({ + topHash: topHash, + status: undefined + }) } if (directRequestStatus.isTrustedOperationStatus) { console.log(`TrustedOperationStatus: ${directRequestStatus}`) const status = directRequestStatus.asTrustedOperationStatus; if (connection_can_be_closed(status)) { - resolve(status) + resolve({ + topHash: topHash, + status: status + }) } } } @@ -207,8 +218,8 @@ export class Worker implements IWorkerBase { const res = await this.#ws.subscribe(method, method, params, onStatusChange ); - let topHash = this.createType('Hash', res); - console.debug(`resHash: ${topHash}`); + topHash = this.createType('Hash', res); + console.debug(`topHash: ${topHash}`); } catch (err) { console.error(err); reject(err);