Skip to content

Commit

Permalink
aggregate callback result in TrustedCallResult
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Oct 25, 2024
1 parent ea83f50 commit 4a87aad
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 94 deletions.
150 changes: 75 additions & 75 deletions packages/worker-api/src/integriteeWorker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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();
});
});
Expand All @@ -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();
// });
// });
Expand All @@ -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();
// });
// });
Expand Down
11 changes: 5 additions & 6 deletions packages/worker-api/src/integriteeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {Hash} from '@polkadot/types/interfaces/runtime';
import type {
ShardIdentifier,
IntegriteeTrustedCallSigned,
Expand All @@ -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 {
Expand Down Expand Up @@ -81,7 +80,7 @@ export class IntegriteeWorker extends Worker {
to: String,
amount: number,
signerOptions?: TrustedSignerOptions,
): Promise<Hash> {
): Promise<TrustedCallResult> {
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])
Expand All @@ -98,7 +97,7 @@ export class IntegriteeWorker extends Worker {
toPublicAddress: string,
amount: number,
signerOptions?: TrustedSignerOptions,
): Promise<Hash> {
): Promise<TrustedCallResult> {
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions)

const shardT = this.createType('ShardIdentifier', bs58.decode(shard));
Expand All @@ -114,7 +113,7 @@ export class IntegriteeWorker extends Worker {
mrenclave: string,
guess: number,
signerOptions?: TrustedSignerOptions,
): Promise<Hash> {
): Promise<TrustedCallResult> {
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions)

const shardT = this.createType('ShardIdentifier', bs58.decode(shard));
Expand All @@ -127,7 +126,7 @@ export class IntegriteeWorker extends Worker {
return this.sendTrustedCall(signed, shardT);
}

async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier): Promise<Hash> {
async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier): Promise<TrustedCallResult> {
if (this.shieldingKey() == undefined) {
console.debug(`[sentTrustedCall] Setting the shielding pubKey of the worker.`)
await this.getShieldingKey();
Expand Down
8 changes: 7 additions & 1 deletion packages/worker-api/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,11 @@ export interface GenericTop {
toHex(): string
}

export interface TrustedCallResult {
topHash?: Hash,
status?: TrustedOperationStatus,
}

export interface ISubmittableGetter<W extends IWorkerBase, Type> {

worker: W;
Expand Down
35 changes: 23 additions & 12 deletions packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -144,10 +150,9 @@ export class Worker implements IWorkerBase {
return this.createType(returnType, value);
}

async submitAndWatchTop<Top extends GenericTop>(top: Top, shard: ShardIdentifier): Promise<Hash> {
async submitAndWatchTop<Top extends GenericTop>(top: Top, shard: ShardIdentifier): Promise<TrustedCallResult> {

console.debug(`Sending TrustedOperation: ${JSON.stringify(top)}`);

const cyphertext = await this.encrypt(top.toU8a());

const r = this.createType(
Expand All @@ -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;
}


Expand All @@ -173,9 +176,11 @@ export class Worker implements IWorkerBase {
return this.resultToRpcReturnValue(result);
}

public async subscribe(method: string, params: unknown[]): Promise<any> {
public async subscribe(method: string, params: unknown[]): Promise<TrustedCallResult> {
await this.isReady();

let topHash: Hash;

return new Promise( async (resolve, reject) => {
const onStatusChange = (error: Error | null, result: string) => {
if (error !== null) {
Expand All @@ -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
})
}
}
}
Expand All @@ -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);
Expand Down

0 comments on commit 4a87aad

Please sign in to comment.