From 1fb33f99ee6fcefcf311db48f9b909a3100ef12c Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 4 Sep 2024 10:27:59 +0200 Subject: [PATCH] Refactoring ts integration tests to get worker url from the parachain (#3041) --- .../integration-tests/.env.local.example | 1 - .../ts-tests/integration-tests/.env.staging | 3 +-- .../assertion_contracts.test.ts | 1 - .../integration-tests/common/utils/context.ts | 24 +++++++++++++++---- .../common/utils/integration-setup.ts | 2 +- .../di_bitcoin_identity.test.ts | 1 - .../integration-tests/di_evm_identity.test.ts | 1 - .../di_solana_identity.test.ts | 1 - .../di_substrate_identity.test.ts | 1 - .../ts-tests/integration-tests/di_vc.test.ts | 1 - .../discord_identity.test.ts | 1 - .../ts-tests/integration-tests/dr_vc.test.ts | 1 - .../twitter_identity.test.ts | 1 - .../integration-tests/vc_correctness.test.ts | 2 +- 14 files changed, 22 insertions(+), 19 deletions(-) diff --git a/tee-worker/ts-tests/integration-tests/.env.local.example b/tee-worker/ts-tests/integration-tests/.env.local.example index 8541c84a83..f38241681c 100644 --- a/tee-worker/ts-tests/integration-tests/.env.local.example +++ b/tee-worker/ts-tests/integration-tests/.env.local.example @@ -1,5 +1,4 @@ NODE_ENV = local -WORKER_ENDPOINT = ws://localhost:2000 NODE_ENDPOINT = ws://localhost:9944 BINARY_DIR=../../bin LITENTRY_CLI_DIR=../../bin/litentry-cli diff --git a/tee-worker/ts-tests/integration-tests/.env.staging b/tee-worker/ts-tests/integration-tests/.env.staging index ad00256bb8..16a7f4c4da 100644 --- a/tee-worker/ts-tests/integration-tests/.env.staging +++ b/tee-worker/ts-tests/integration-tests/.env.staging @@ -1,5 +1,4 @@ NODE_ENV = staging -WORKER_ENDPOINT = ws://litentry-worker-1:2011 NODE_ENDPOINT = "ws://litentry-node:9912" BINARY_DIR=/usr/local/bin -LITENTRY_CLI_DIR=/usr/local/bin/litentry-cli \ No newline at end of file +LITENTRY_CLI_DIR=/usr/local/bin/litentry-cli diff --git a/tee-worker/ts-tests/integration-tests/assertion_contracts.test.ts b/tee-worker/ts-tests/integration-tests/assertion_contracts.test.ts index 66817d4906..fa6b0c2b5a 100644 --- a/tee-worker/ts-tests/integration-tests/assertion_contracts.test.ts +++ b/tee-worker/ts-tests/integration-tests/assertion_contracts.test.ts @@ -40,7 +40,6 @@ describe('Test Vc (direct request)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/common/utils/context.ts b/tee-worker/ts-tests/integration-tests/common/utils/context.ts index 059a257197..d9690ccd79 100644 --- a/tee-worker/ts-tests/integration-tests/common/utils/context.ts +++ b/tee-worker/ts-tests/integration-tests/common/utils/context.ts @@ -1,4 +1,4 @@ -import { WsProvider, ApiPromise } from 'parachain-api'; +import { WsProvider, ApiPromise, PalletTeebagEnclave } from 'parachain-api'; import { cryptoWaitReady } from '@polkadot/util-crypto'; import { hexToString } from '@polkadot/util'; import WebSocketAsPromised from 'websocket-as-promised'; @@ -27,10 +27,7 @@ export async function initWorkerConnection(endpoint: string): Promise { +export async function initIntegrationTestContext(substrateEndpoint: string): Promise { const provider = new WsProvider(substrateEndpoint); await cryptoWaitReady(); @@ -45,6 +42,7 @@ export async function initIntegrationTestContext( const chainIdentifier = api.registry.chainSS58 as number; + const workerEndpoint = await getWorkerEndpoint(api); const wsp = await initWorkerConnection(workerEndpoint); const requestId = 1; @@ -63,6 +61,22 @@ export async function initIntegrationTestContext( }; } +async function getWorkerEndpoint(api: ApiPromise): Promise { + const registry = await api.query.teebag.enclaveRegistry.entries(); + const identityEnclaves = registry.reduce((enclaves, [, enclave]) => { + if (enclave.isEmpty || !enclave.unwrap().workerType.isIdentity) { + return enclaves; + } + return [...enclaves, enclave.unwrap()]; + }, [] as PalletTeebagEnclave[]); + + if (identityEnclaves.length === 0) { + throw new Error('No identity worker found'); + } + + return identityEnclaves[Math.floor(Math.random() * identityEnclaves.length)].url.toHuman() as string; +} + export async function getEnclave(api: ApiPromise): Promise<{ mrEnclave: HexString; teeShieldingKey: KeyObject; diff --git a/tee-worker/ts-tests/integration-tests/common/utils/integration-setup.ts b/tee-worker/ts-tests/integration-tests/common/utils/integration-setup.ts index d677332517..085d06ddb9 100644 --- a/tee-worker/ts-tests/integration-tests/common/utils/integration-setup.ts +++ b/tee-worker/ts-tests/integration-tests/common/utils/integration-setup.ts @@ -27,7 +27,7 @@ export function describeLitentry(title: string, cb: (context: IntegrationTestCon before('Starting Litentry(parachain&tee)', async function () { //env url - const tmp = await initIntegrationTestContext(process.env.WORKER_ENDPOINT!, process.env.NODE_ENDPOINT!); + const tmp = await initIntegrationTestContext(process.env.NODE_ENDPOINT!); context.mrEnclave = tmp.mrEnclave; context.api = tmp.api; context.tee = tmp.tee; diff --git a/tee-worker/ts-tests/integration-tests/di_bitcoin_identity.test.ts b/tee-worker/ts-tests/integration-tests/di_bitcoin_identity.test.ts index 9df232d003..9bd1b80363 100644 --- a/tee-worker/ts-tests/integration-tests/di_bitcoin_identity.test.ts +++ b/tee-worker/ts-tests/integration-tests/di_bitcoin_identity.test.ts @@ -60,7 +60,6 @@ describe('Test Identity (bitcoin direct invocation)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/di_evm_identity.test.ts b/tee-worker/ts-tests/integration-tests/di_evm_identity.test.ts index fc897e6a74..46363e0a88 100644 --- a/tee-worker/ts-tests/integration-tests/di_evm_identity.test.ts +++ b/tee-worker/ts-tests/integration-tests/di_evm_identity.test.ts @@ -48,7 +48,6 @@ describe('Test Identity (evm direct invocation)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/di_solana_identity.test.ts b/tee-worker/ts-tests/integration-tests/di_solana_identity.test.ts index f625b16858..456bef79fe 100644 --- a/tee-worker/ts-tests/integration-tests/di_solana_identity.test.ts +++ b/tee-worker/ts-tests/integration-tests/di_solana_identity.test.ts @@ -46,7 +46,6 @@ describe('Test Identity (solana direct invocation)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/di_substrate_identity.test.ts b/tee-worker/ts-tests/integration-tests/di_substrate_identity.test.ts index 55fe4cc4b8..061f2c4315 100644 --- a/tee-worker/ts-tests/integration-tests/di_substrate_identity.test.ts +++ b/tee-worker/ts-tests/integration-tests/di_substrate_identity.test.ts @@ -57,7 +57,6 @@ describe('Test Identity (direct invocation)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/di_vc.test.ts b/tee-worker/ts-tests/integration-tests/di_vc.test.ts index daed597dea..6f29690e6f 100644 --- a/tee-worker/ts-tests/integration-tests/di_vc.test.ts +++ b/tee-worker/ts-tests/integration-tests/di_vc.test.ts @@ -40,7 +40,6 @@ describe('Test Vc (direct invocation)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/discord_identity.test.ts b/tee-worker/ts-tests/integration-tests/discord_identity.test.ts index 041e70730e..f0dad4163d 100644 --- a/tee-worker/ts-tests/integration-tests/discord_identity.test.ts +++ b/tee-worker/ts-tests/integration-tests/discord_identity.test.ts @@ -51,7 +51,6 @@ describe('Test Discord Identity (direct invocation)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/dr_vc.test.ts b/tee-worker/ts-tests/integration-tests/dr_vc.test.ts index f24ca0f724..e8c37d5e4d 100644 --- a/tee-worker/ts-tests/integration-tests/dr_vc.test.ts +++ b/tee-worker/ts-tests/integration-tests/dr_vc.test.ts @@ -41,7 +41,6 @@ describe('Test Vc (direct request)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/twitter_identity.test.ts b/tee-worker/ts-tests/integration-tests/twitter_identity.test.ts index 49c874d49d..59369118e1 100644 --- a/tee-worker/ts-tests/integration-tests/twitter_identity.test.ts +++ b/tee-worker/ts-tests/integration-tests/twitter_identity.test.ts @@ -54,7 +54,6 @@ describe('Test Twitter Identity (direct invocation)', function () { before(async () => { context = await initIntegrationTestContext( - process.env.WORKER_ENDPOINT!, // @fixme evil assertion; centralize env access process.env.NODE_ENDPOINT! // @fixme evil assertion; centralize env access ); teeShieldingKey = await getTeeShieldingKey(context); diff --git a/tee-worker/ts-tests/integration-tests/vc_correctness.test.ts b/tee-worker/ts-tests/integration-tests/vc_correctness.test.ts index 1abe11fc06..5524e79d51 100644 --- a/tee-worker/ts-tests/integration-tests/vc_correctness.test.ts +++ b/tee-worker/ts-tests/integration-tests/vc_correctness.test.ts @@ -43,7 +43,7 @@ describe('Test Vc (direct invocation)', function () { const errorArray: { id: string; index: number; assertion: any; error: any }[] = []; this.timeout(6000000); before(async () => { - context = await initIntegrationTestContext(enclaveEndpoint, nodeEndpoint); + context = await initIntegrationTestContext(nodeEndpoint); teeShieldingKey = await getTeeShieldingKey(context); });