Skip to content

Commit

Permalink
🪚 OmniGraph™ Fix config loading in the wire task (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Dec 12, 2023
1 parent 1185ca7 commit 381bc69
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 45 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ docker compose logs -f

This allows you to monitor logs coming from e.g. the `hardhat` nodes

#### Exposing test networks on `localhost`

It is possible to expose the test networks defined in `docker-compose.yaml` on your host machine. To do this, you can run:

```bash
yarn start
```

You will need to use the `MNEMONIC` defined in `docker-compose.templates.yaml` if you require funded accounts:

```bash
export MNEMONIC='test test test test test test test test test test test junk'
```

To stop the network containers, just run:

```bash
yarn stop
```

**Don't forget that the state of the local networks disappears after they are stopped and any deployment files created in one session will be invalid in the next one.**

### Troubleshooting

#### Problems with committing
Expand Down
24 changes: 24 additions & 0 deletions docker-compose.local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
#
# Docker compose for exposed test networks
#
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
version: "3.9"

services:
# ~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~
#
# Expose nodes on host ports
#
# .oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
network-vengaboys:
ports:
- "10001:8545"

network-britney:
ports:
- "10002:8545"
2 changes: 1 addition & 1 deletion docker-compose.templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
# We'll provide a single testing MNEMONIC for the project so that the test EVM nodes
# account are in sync with the hardhat accounts
environment:
- MNEMONIC='test test test test test test test test test test test test'
- MNEMONIC=test test test test test test test test test test test junk
logging:
driver: local
options:
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ services:
condition: service_healthy
logging:
driver: none
# The default containerized setup will specify the network URLs
# for the internal networks
#
# This works in conjunction with hardhat configs in the test projects.
#
# If these environment variables are not specified, the exposed networks are used
# that need to be started using docker compose up:
#
# docker compose -f docker-compose.yaml -f docker-compose.local.yaml up network-britney network-vengaboys
environment:
- NETWORK_URL_BRITNEY=http://network-britney:8545
- NETWORK_URL_VENGABOYS=http://network-vengaboys:8545
volumes:
- ./node_modules/.cache/turbo:/app/node_modules/.cache/turbo
- ./packages:/app/packages
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"dev": "npx turbo run dev",
"lint": "npx turbo run lint",
"prepare": "husky install",
"start": "docker compose -f docker-compose.yaml -f docker-compose.local.yaml up network-britney network-vengaboys",
"stop": "docker compose down",
"test": "docker compose run --rm $DOCKER_COMPOSE_RUN_TESTS_ARGS tests"
},
"lint-staged": {
Expand Down
20 changes: 18 additions & 2 deletions packages/ua-utils-evm-hardhat-test/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import assert from 'assert'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import type { HardhatUserConfig } from 'hardhat/types'

// These tasks are only for when you want to play with this setup
// using your own keyboard (using exposed networks)
import './tasks'

const MNEMONIC = process.env.MNEMONIC
assert(MNEMONIC, `Missing MNEMONIC environment variable`)

Expand All @@ -18,14 +22,26 @@ const config: HardhatUserConfig = {
networks: {
vengaboys: {
eid: EndpointId.ETHEREUM_MAINNET,
url: 'http://network-vengaboys:8545',
// Containerized setup defines these environment variables
// to point the networks to the internal ones
//
// If these are not specified, exposed networks are used
//
// See root README.md for usage with exposed network
url: process.env.NETWORK_URL_VENGABOYS ?? 'http://localhost:10001',
accounts: {
mnemonic: MNEMONIC,
},
},
britney: {
eid: EndpointId.AVALANCHE_MAINNET,
url: 'http://network-britney:8545',
// Containerized setup defines these environment variables
// to point the networks to the internal ones
//
// If these are not specified, exposed networks are used
//
// See root README.md for usage with exposed network
url: process.env.NETWORK_URL_BRITNEY ?? 'http://localhost:10002',
accounts: {
mnemonic: MNEMONIC,
},
Expand Down
25 changes: 25 additions & 0 deletions packages/ua-utils-evm-hardhat-test/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { task } from 'hardhat/config'
import { setupDefaultEndpoint } from '../test/__utils__/endpoint'
import { deployOApp } from '../test/__utils__/oapp'

/**
* Task that will:
*
* - Deploy EndpointV2-related infrastructure
* - Wire the EndpointV2-related infrastructure with default configuration
* - Deploy the DefaultOApp
*
* If you want to expose the networks locally
* and deploy your own endpoints, this task is tjust for you!
*
* See the root README.md section for info about how to expose networks locally
*/
task('lz:test:oapp:deploy', 'Deploy the test OApp on a default EndpointV2 infrastructure', async () => {
// Deploy the DefaultOApp along with the EndpointV2
//
// This will wipe the existing deployments so watch out
await deployOApp()

// This will wire up the endpoints
await setupDefaultEndpoint()
})
40 changes: 27 additions & 13 deletions packages/ua-utils-evm-hardhat-test/test/__utils__/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,43 @@ export const getDefaultUlnConfig = (dvnAddress: string): Uln302UlnConfig => {
}

/**
* Helper function that deploys a fresh endpoint infrastructure:
*
* - EndpointV2
* - ReceiveUln302
* - SendUln302
* Deploys an enpoint fixture. Useful for tests
*/
export const deployEndpointFixture = async () => {
const environmentFactory = createNetworkEnvironmentFactory()
const eth = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
const avax = await environmentFactory(EndpointId.AVALANCHE_MAINNET)

await Promise.all([eth.deployments.fixture('EndpointV2'), avax.deployments.fixture('EndpointV2')])
}

/**
* Deploys an enpoint fixture. Useful for when deployment files need to be persisted
*/
export const deployEndpoint = async () => {
const environmentFactory = createNetworkEnvironmentFactory()
const eth = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
const avax = await environmentFactory(EndpointId.AVALANCHE_MAINNET)

await Promise.all([
eth.deployments.run('EndpointV2', { writeDeploymentsToFiles: true }),
avax.deployments.run('EndpointV2', { writeDeploymentsToFiles: true }),
])
}

/**
* Helper function that wires the endpoint infrastructure.
*
* After deploying, it will wire up the elements with minimal configuration
* The contracts still need to be deployed (use deployEndpoint or deployEndpointFixture)
*/
export const setupDefaultEndpoint = async (): Promise<void> => {
// This is the tooling we are going to need
const logger = createLogger()
const environmentFactory = createNetworkEnvironmentFactory()
const contractFactory = createConnectedContractFactory()
const signerFactory = createSignerFactory()
const ulnSdkFactory = createUln302Factory(contractFactory)
const endpointSdkFactory = createEndpointFactory(contractFactory, ulnSdkFactory)

// First we deploy the endpoint
const eth = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
const avax = await environmentFactory(EndpointId.AVALANCHE_MAINNET)

await Promise.all([eth.deployments.fixture('EndpointV2'), avax.deployments.fixture('EndpointV2')])

// For the graphs, we'll also need the pointers to the contracts
const ethSendUlnPoint = omniContractToPoint(await contractFactory(ethSendUln))
const avaxSendUlnPoint = omniContractToPoint(await contractFactory(avaxSendUln))
Expand Down
11 changes: 11 additions & 0 deletions packages/ua-utils-evm-hardhat-test/test/__utils__/oapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ export const deployOApp = async () => {
const eth = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
const avax = await environmentFactory(EndpointId.AVALANCHE_MAINNET)

await Promise.all([
eth.deployments.run('OApp', { writeDeploymentsToFiles: true }),
avax.deployments.run('OApp', { writeDeploymentsToFiles: true }),
])
}

export const deployOAppFixture = async () => {
const environmentFactory = createNetworkEnvironmentFactory()
const eth = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
const avax = await environmentFactory(EndpointId.AVALANCHE_MAINNET)

await Promise.all([eth.deployments.fixture('OApp'), avax.deployments.fixture('OApp')])
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'hardhat'
import { createConnectedContractFactory } from '@layerzerolabs/utils-evm-hardhat'
import { omniContractToPoint } from '@layerzerolabs/utils-evm'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { getDefaultUlnConfig, setupDefaultEndpoint } from '../__utils__/endpoint'
import { deployEndpointFixture, getDefaultUlnConfig, setupDefaultEndpoint } from '../__utils__/endpoint'
import { createEndpointFactory, createUln302Factory } from '@layerzerolabs/protocol-utils-evm'

describe('endpoint/config', () => {
Expand All @@ -16,6 +16,7 @@ describe('endpoint/config', () => {
const avaxDvn = { eid: EndpointId.AVALANCHE_MAINNET, contractName: 'DVN' }

beforeEach(async () => {
await deployEndpointFixture()
await setupDefaultEndpoint()
})

Expand Down
7 changes: 4 additions & 3 deletions packages/ua-utils-evm-hardhat-test/test/oapp/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
import type { OmniGraphHardhat } from '@layerzerolabs/utils-evm-hardhat'
import { omniContractToPoint } from '@layerzerolabs/utils-evm'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { setupDefaultEndpoint } from '../__utils__/endpoint'
import { deployOApp } from '../__utils__/oapp'
import { deployEndpointFixture, setupDefaultEndpoint } from '../__utils__/endpoint'
import { deployOAppFixture } from '../__utils__/oapp'

describe('oapp/config', () => {
const ethContract = { eid: EndpointId.ETHEREUM_MAINNET, contractName: 'DefaultOApp' }
Expand Down Expand Up @@ -39,11 +39,12 @@ describe('oapp/config', () => {
}

beforeAll(async () => {
await deployEndpointFixture()
await setupDefaultEndpoint()
})

beforeEach(async () => {
await deployOApp()
await deployOAppFixture()
})

it('should return all setPeer transactions', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { getDefaultExecutorConfig, getDefaultUlnConfig, setupDefaultEndpoint } from '../__utils__/endpoint'
import {
deployEndpointFixture,
getDefaultExecutorConfig,
getDefaultUlnConfig,
setupDefaultEndpoint,
} from '../__utils__/endpoint'
import { createContractFactory, getEidForNetworkName } from '@layerzerolabs/utils-evm-hardhat'
import hre from 'hardhat'
import { TASK_LZ_GET_DEFAULT_CONFIG } from '@layerzerolabs/ua-utils-evm-hardhat'
import { omniContractToPoint } from '@layerzerolabs/utils-evm'

describe('task: getDefaultConfig', () => {
beforeEach(async () => {
await deployEndpointFixture()
await setupDefaultEndpoint()
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { getDefaultExecutorConfig, getDefaultUlnConfig, setupDefaultEndpoint } from '../__utils__/endpoint'
import {
deployEndpointFixture,
getDefaultExecutorConfig,
getDefaultUlnConfig,
setupDefaultEndpoint,
} from '../__utils__/endpoint'
import { createContractFactory, getEidForNetworkName } from '@layerzerolabs/utils-evm-hardhat'
import hre from 'hardhat'
import { AddressZero } from '@ethersproject/constants'
Expand All @@ -7,6 +12,7 @@ import { omniContractToPoint } from '@layerzerolabs/utils-evm'

describe('task: getOAppConfig', () => {
beforeEach(async () => {
await deployEndpointFixture()
await setupDefaultEndpoint()
})

Expand Down
18 changes: 14 additions & 4 deletions packages/ua-utils-evm-hardhat-test/test/task/oapp/wire.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import hre from 'hardhat'
import { isFile, promptToContinue } from '@layerzerolabs/io-utils'
import { resolve } from 'path'
import { relative, resolve } from 'path'
import { TASK_LZ_WIRE_OAPP } from '@layerzerolabs/ua-utils-evm-hardhat'
import { deployOApp } from '../../__utils__/oapp'
import { deployOAppFixture } from '../../__utils__/oapp'
import { cwd } from 'process'

jest.mock('@layerzerolabs/io-utils', () => {
const original = jest.requireActual('@layerzerolabs/io-utils')
Expand Down Expand Up @@ -31,7 +32,7 @@ describe('task/oapp/wire', () => {

describe('with invalid configs', () => {
beforeAll(async () => {
await deployOApp()
await deployOAppFixture()
})

it('should fail if the config file does not exist', async () => {
Expand Down Expand Up @@ -77,7 +78,7 @@ describe('task/oapp/wire', () => {

describe('with valid configs', () => {
beforeEach(async () => {
await deployOApp()
await deployOAppFixture()
})

it('should exit if there is nothing to wire', async () => {
Expand All @@ -88,6 +89,15 @@ describe('task/oapp/wire', () => {
expect(promptToContinueMock).not.toHaveBeenCalled()
})

it('should work with relative paths', async () => {
const oappConfigAbsolute = configPathFixture('valid.config.empty.js')
const oappConfig = relative(cwd(), oappConfigAbsolute)

await hre.run(TASK_LZ_WIRE_OAPP, { oappConfig })

expect(promptToContinueMock).not.toHaveBeenCalled()
})

it('should have debug output if requested (so called eye test, check the test output)', async () => {
const oappConfig = configPathFixture('valid.config.connected.js')

Expand Down
20 changes: 11 additions & 9 deletions packages/ua-utils-evm-hardhat/src/tasks/oapp/getDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ export const getDefaultConfig: ActionType<TaskArgs> = async (taskArgs) => {
receiveUlnConfig,
}

printRecord({
localNetworkName,
remoteNetworkName,
sendLibrary,
receiveLibrary,
sendUlnConfig,
sendExecutorConfig,
receiveUlnConfig,
})
console.log(
printRecord({
localNetworkName,
remoteNetworkName,
sendLibrary,
receiveLibrary,
sendUlnConfig,
sendExecutorConfig,
receiveUlnConfig,
})
)
}
}
return configs
Expand Down
Loading

0 comments on commit 381bc69

Please sign in to comment.