From 62dd1916be1bfffa5db8715e42de1504b19f9166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lz=2Esir=CE=94rthurmoney=28=29?= <95722332+sirarthurmoney@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:05:35 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=E2=80=8D=E2=99=82=EF=B8=8F?= =?UTF-8?q?=E2=9B=8F=EF=B8=8FUpdate=20checkWire=20task=20to=20printout=20t?= =?UTF-8?q?able=20of=20connected=20peers=20(#323)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/giant-icons-bake.md | 6 ++ .../src/tasks/oapp/config.check.ts | 50 ++++++++-- .../valid.multi.network.config.connected.js | 57 +++++++++++ ...multi.network.config.missing.connection.js | 53 ++++++++++ ...lid.multi.network.config.one.connection.js | 37 +++++++ .../__snapshots__/config.check.test.ts.snap | 97 +++++++++++++++++++ .../test/task/oapp/config.check.test.ts | 48 ++++++--- 7 files changed, 322 insertions(+), 26 deletions(-) create mode 100644 .changeset/giant-icons-bake.md create mode 100644 tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.connected.js create mode 100644 tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.missing.connection.js create mode 100644 tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.one.connection.js create mode 100644 tests/ua-devtools-evm-hardhat-test/test/task/oapp/__snapshots__/config.check.test.ts.snap diff --git a/.changeset/giant-icons-bake.md b/.changeset/giant-icons-bake.md new file mode 100644 index 000000000..86fb64b8a --- /dev/null +++ b/.changeset/giant-icons-bake.md @@ -0,0 +1,6 @@ +--- +"@layerzerolabs/ua-devtools-evm-hardhat-test": patch +"@layerzerolabs/ua-devtools-evm-hardhat": patch +--- + +Update checkWire task to printout table of connected peers diff --git a/packages/ua-devtools-evm-hardhat/src/tasks/oapp/config.check.ts b/packages/ua-devtools-evm-hardhat/src/tasks/oapp/config.check.ts index 2d4df9456..42deaddce 100644 --- a/packages/ua-devtools-evm-hardhat/src/tasks/oapp/config.check.ts +++ b/packages/ua-devtools-evm-hardhat/src/tasks/oapp/config.check.ts @@ -1,7 +1,6 @@ import { ActionType } from 'hardhat/types' import { task } from 'hardhat/config' -import { createLogger, setDefaultLogLevel } from '@layerzerolabs/io-devtools' -import { printRecords } from '@layerzerolabs/io-devtools/swag' +import { createLogger, printBoolean, printCrossTable, setDefaultLogLevel } from '@layerzerolabs/io-devtools' import { TASK_LZ_OAPP_CONFIG_CHECK } from '@/constants/tasks' import { printLogo } from '@layerzerolabs/io-devtools/swag' import { OAppOmniGraph } from '@layerzerolabs/ua-devtools' @@ -10,7 +9,7 @@ import { createOAppFactory } from '@layerzerolabs/ua-devtools-evm' import { checkOAppPeers } from '@layerzerolabs/ua-devtools' import { validateAndTransformOappConfig } from '@/utils/taskHelpers' import { getNetworkNameForEid } from '@layerzerolabs/devtools-evm-hardhat' -import { printBoolean } from '@layerzerolabs/io-devtools' +import { areVectorsEqual } from '@layerzerolabs/devtools' interface TaskArgs { oappConfig: string @@ -27,6 +26,14 @@ export const checkWire: ActionType = async ({ oappConfig: oappConfigPa const logger = createLogger() const graph: OAppOmniGraph = await validateAndTransformOappConfig(oappConfigPath, logger) + // need points for OApp Peer Matrix + const points = graph.contracts + .map(({ point }) => point) + .map((point) => ({ + ...point, + networkName: getNetworkNameForEid(point.eid), + })) + // At this point we are ready read data from the OApp logger.verbose(`Reading peers from OApps`) const contractFactory = createConnectedContractFactory() @@ -34,14 +41,37 @@ export const checkWire: ActionType = async ({ oappConfig: oappConfigPa try { const peers = await checkOAppPeers(graph, oAppFactory) + const peerNetworkMatrix = points.map((row) => { + /** + * for each point in the network (referred to as 'row'), create a row in the matrix + */ + const connectionsForCurrentRow = points.reduce((tableRow, column) => { + /** + * find a peer with a vector matching the connection from 'column' to 'row' + */ + const connection = peers.find((peer) => { + return areVectorsEqual(peer.vector, { from: column, to: row }) + }) + /** + * update the row with a key-value pair indicating the connection status for the current column + */ + return { + ...tableRow, + [column.networkName]: printBoolean(connection?.hasPeer), + } + }, {}) + /** + * return the row representing connections for the current 'row' + */ + return connectionsForCurrentRow + }) - const formattedPeers = peers.map((peer) => ({ - 'From network': getNetworkNameForEid(peer.vector.from.eid), - 'To network': getNetworkNameForEid(peer.vector.to.eid), - Connected: printBoolean(peer.hasPeer), - })) - - printRecords(formattedPeers) + console.log( + printCrossTable(peerNetworkMatrix, ['from → to', ...points.map(({ networkName }) => networkName)]), + `\n\t${printBoolean(true)} - Connected\n`, + `\t${printBoolean(false)} - Not Connected\n`, + `\t${printBoolean(undefined)} - Ignored` + ) return peers } catch (error) { diff --git a/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.connected.js b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.connected.js new file mode 100644 index 000000000..fc00b4968 --- /dev/null +++ b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.connected.js @@ -0,0 +1,57 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { EndpointId } = require('@layerzerolabs/lz-definitions'); + +const ethContract = { + eid: EndpointId.ETHEREUM_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +const avaxContract = { + eid: EndpointId.AVALANCHE_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +const bscContract = { + eid: EndpointId.BSC_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +module.exports = { + contracts: [ + { + contract: avaxContract, + }, + { + contract: ethContract, + }, + { + contract: bscContract, + }, + ], + connections: [ + { + from: ethContract, + to: avaxContract, + }, + { + from: ethContract, + to: bscContract, + }, + { + from: avaxContract, + to: ethContract, + }, + { + from: avaxContract, + to: bscContract, + }, + { + from: bscContract, + to: ethContract, + }, + { + from: bscContract, + to: avaxContract, + }, + ], +}; diff --git a/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.missing.connection.js b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.missing.connection.js new file mode 100644 index 000000000..3d334c157 --- /dev/null +++ b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.missing.connection.js @@ -0,0 +1,53 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { EndpointId } = require('@layerzerolabs/lz-definitions'); + +const ethContract = { + eid: EndpointId.ETHEREUM_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +const avaxContract = { + eid: EndpointId.AVALANCHE_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +const bscContract = { + eid: EndpointId.BSC_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +module.exports = { + contracts: [ + { + contract: avaxContract, + }, + { + contract: ethContract, + }, + { + contract: bscContract, + }, + ], + connections: [ + { + from: ethContract, + to: avaxContract, + }, + { + from: ethContract, + to: bscContract, + }, + { + from: avaxContract, + to: ethContract, + }, + { + from: avaxContract, + to: bscContract, + }, + { + from: bscContract, + to: ethContract, + }, + ], +}; diff --git a/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.one.connection.js b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.one.connection.js new file mode 100644 index 000000000..9b95c6380 --- /dev/null +++ b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__data__/configs/valid.multi.network.config.one.connection.js @@ -0,0 +1,37 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { EndpointId } = require('@layerzerolabs/lz-definitions'); + +const ethContract = { + eid: EndpointId.ETHEREUM_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +const avaxContract = { + eid: EndpointId.AVALANCHE_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +const bscContract = { + eid: EndpointId.BSC_V2_MAINNET, + contractName: 'DefaultOApp', +}; + +module.exports = { + contracts: [ + { + contract: avaxContract, + }, + { + contract: ethContract, + }, + { + contract: bscContract, + }, + ], + connections: [ + { + from: ethContract, + to: avaxContract, + }, + ], +}; diff --git a/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__snapshots__/config.check.test.ts.snap b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__snapshots__/config.check.test.ts.snap new file mode 100644 index 000000000..7a8a80f16 --- /dev/null +++ b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/__snapshots__/config.check.test.ts.snap @@ -0,0 +1,97 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`task lz:oapp:config:check should show all chains are connected after running wire with three networks 1`] = ` +[ + "┌───────────┬─────────┬───────────┬───────┐ +│ from → to │ britney │ vengaboys │ tango │ +├───────────┼─────────┼───────────┼───────┤ +│ britney │ ∅ │ ✓ │ ✓ │ +├───────────┼─────────┼───────────┼───────┤ +│ vengaboys │ ✓ │ ∅ │ ✓ │ +├───────────┼─────────┼───────────┼───────┤ +│ tango │ ✓ │ ✓ │ ∅ │ +└───────────┴─────────┴───────────┴───────┘", + " + ✓ - Connected +", + " ⤫ - Not Connected +", + " ∅ - Ignored", +] +`; + +exports[`task lz:oapp:config:check should show all chains are connected after running wire with two networks 1`] = ` +[ + "┌───────────┬─────────┬───────────┐ +│ from → to │ britney │ vengaboys │ +├───────────┼─────────┼───────────┤ +│ britney │ ∅ │ ✓ │ +├───────────┼─────────┼───────────┤ +│ vengaboys │ ✓ │ ∅ │ +└───────────┴─────────┴───────────┘", + " + ✓ - Connected +", + " ⤫ - Not Connected +", + " ∅ - Ignored", +] +`; + +exports[`task lz:oapp:config:check should show all chains are connected expect one after running wire with three networks 1`] = ` +[ + "┌───────────┬─────────┬───────────┬───────┐ +│ from → to │ britney │ vengaboys │ tango │ +├───────────┼─────────┼───────────┼───────┤ +│ britney │ ∅ │ ✓ │ ✓ │ +├───────────┼─────────┼───────────┼───────┤ +│ vengaboys │ ✓ │ ∅ │ ✓ │ +├───────────┼─────────┼───────────┼───────┤ +│ tango │ ∅ │ ✓ │ ∅ │ +└───────────┴─────────┴───────────┴───────┘", + " + ✓ - Connected +", + " ⤫ - Not Connected +", + " ∅ - Ignored", +] +`; + +exports[`task lz:oapp:config:check should show all chains are not connected expect one after running wire with three networks 1`] = ` +[ + "┌───────────┬─────────┬───────────┬───────┐ +│ from → to │ britney │ vengaboys │ tango │ +├───────────┼─────────┼───────────┼───────┤ +│ britney │ ∅ │ ∅ │ ∅ │ +├───────────┼─────────┼───────────┼───────┤ +│ vengaboys │ ✓ │ ∅ │ ∅ │ +├───────────┼─────────┼───────────┼───────┤ +│ tango │ ∅ │ ∅ │ ∅ │ +└───────────┴─────────┴───────────┴───────┘", + " + ✓ - Connected +", + " ⤫ - Not Connected +", + " ∅ - Ignored", +] +`; + +exports[`task lz:oapp:config:check should show no chains are connected with two networks 1`] = ` +[ + "┌───────────┬─────────┬───────────┐ +│ from → to │ britney │ vengaboys │ +├───────────┼─────────┼───────────┤ +│ britney │ ∅ │ ⤫ │ +├───────────┼─────────┼───────────┤ +│ vengaboys │ ⤫ │ ∅ │ +└───────────┴─────────┴───────────┘", + " + ✓ - Connected +", + " ⤫ - Not Connected +", + " ∅ - Ignored", +] +`; diff --git a/tests/ua-devtools-evm-hardhat-test/test/task/oapp/config.check.test.ts b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/config.check.test.ts index c12a5ee69..bd0e3c7f8 100644 --- a/tests/ua-devtools-evm-hardhat-test/test/task/oapp/config.check.test.ts +++ b/tests/ua-devtools-evm-hardhat-test/test/task/oapp/config.check.test.ts @@ -22,26 +22,42 @@ describe(`task ${TASK_LZ_OAPP_CONFIG_CHECK}`, () => { await deployOApp() }) - it('should show no chains are connected', async () => { + it('should show no chains are connected with two networks', async () => { + const consoleSpy = jest.spyOn(console, 'log') const oappConfig = configPathFixture('valid.config.connected.js') - const result = await hre.run(TASK_LZ_OAPP_CONFIG_CHECK, { oappConfig }) - const expectPoint = { eid: expect.any(Number), address: expect.any(String) } - const expectVector = { from: expectPoint, to: expectPoint } - expect(result).toEqual([ - { vector: expectVector, hasPeer: false }, - { vector: expectVector, hasPeer: false }, - ]) + await hre.run(TASK_LZ_OAPP_CONFIG_CHECK, { oappConfig }) + expect(consoleSpy.mock.lastCall).toMatchSnapshot() }) - it('should show both chains are connected after running wire', async () => { + it('should show all chains are connected after running wire with two networks', async () => { + const consoleSpy = jest.spyOn(console, 'log') const oappConfig = configPathFixture('valid.config.connected.js') await hre.run(TASK_LZ_OAPP_WIRE, { oappConfig, ci: true }) - const result = await hre.run(TASK_LZ_OAPP_CONFIG_CHECK, { oappConfig }) - const expectPoint = { eid: expect.any(Number), address: expect.any(String) } - const expectVector = { from: expectPoint, to: expectPoint } - expect(result).toEqual([ - { vector: expectVector, hasPeer: true }, - { vector: expectVector, hasPeer: true }, - ]) + await hre.run(TASK_LZ_OAPP_CONFIG_CHECK, { oappConfig }) + expect(consoleSpy.mock.lastCall).toMatchSnapshot() + }) + + it('should show all chains are connected after running wire with three networks', async () => { + const consoleSpy = jest.spyOn(console, 'log') + const oappConfig = configPathFixture('valid.multi.network.config.connected.js') + await hre.run(TASK_LZ_OAPP_WIRE, { oappConfig, ci: true }) + await hre.run(TASK_LZ_OAPP_CONFIG_CHECK, { oappConfig }) + expect(consoleSpy.mock.lastCall).toMatchSnapshot() + }) + + it('should show all chains are connected expect one after running wire with three networks', async () => { + const consoleSpy = jest.spyOn(console, 'log') + const oappConfig = configPathFixture('valid.multi.network.config.missing.connection.js') + await hre.run(TASK_LZ_OAPP_WIRE, { oappConfig, ci: true }) + await hre.run(TASK_LZ_OAPP_CONFIG_CHECK, { oappConfig }) + expect(consoleSpy.mock.lastCall).toMatchSnapshot() + }) + + it('should show all chains are not connected expect one after running wire with three networks', async () => { + const consoleSpy = jest.spyOn(console, 'log') + const oappConfig = configPathFixture('valid.multi.network.config.one.connection.js') + await hre.run(TASK_LZ_OAPP_WIRE, { oappConfig, ci: true }) + await hre.run(TASK_LZ_OAPP_CONFIG_CHECK, { oappConfig }) + expect(consoleSpy.mock.lastCall).toMatchSnapshot() }) })