From 47cd15d74cf68ea6079b4aa64e1360d1c75299f3 Mon Sep 17 00:00:00 2001 From: TateB Date: Fri, 15 Apr 2022 11:10:27 +1000 Subject: [PATCH] added waitForGraph to ens-test-env --- .pnp.cjs | 3 +- package.json | 3 + packages/ens-test-env/README.md | 96 +++++++++++++++++++ packages/ens-test-env/package.json | 4 + .../src/ens-test-env.default.config.js | 1 + packages/ens-test-env/src/manager.js | 5 +- packages/ensjs/package.json | 3 +- yarn.lock | 1 + 8 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 packages/ens-test-env/README.md diff --git a/.pnp.cjs b/.pnp.cjs index de90ed88..9e6d52aa 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -4706,7 +4706,8 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["ts-jest", "virtual:dbab5548d6c479b3d0fbae056bca9e1ff56916aea7763afdc9f1e6c586db7a0fc611c24fbb7ceb65db0555e200d7d546a786802dd5359ca981e1d851cccb800c#npm:27.1.4"],\ ["ts-node", "virtual:dbab5548d6c479b3d0fbae056bca9e1ff56916aea7763afdc9f1e6c586db7a0fc611c24fbb7ceb65db0555e200d7d546a786802dd5359ca981e1d851cccb800c#npm:10.7.0"],\ ["typechain", "virtual:dbab5548d6c479b3d0fbae056bca9e1ff56916aea7763afdc9f1e6c586db7a0fc611c24fbb7ceb65db0555e200d7d546a786802dd5359ca981e1d851cccb800c#npm:8.0.0"],\ - ["typescript", "patch:typescript@npm%3A4.6.2#~builtin::version=4.6.2&hash=bda367"]\ + ["typescript", "patch:typescript@npm%3A4.6.2#~builtin::version=4.6.2&hash=bda367"],\ + ["wait-on", "npm:6.0.1"]\ ],\ "linkType": "SOFT"\ }]\ diff --git a/package.json b/package.json index e5e99a5a..9d869799 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "workspaces": [ "packages/*" ], + "scripts": { + "publish:local:ens-test-env": "yalc publish packages/ens-test-env --push --up" + }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.15.0", "@typescript-eslint/parser": "^5.15.0", diff --git a/packages/ens-test-env/README.md b/packages/ens-test-env/README.md new file mode 100644 index 00000000..7fc2e328 --- /dev/null +++ b/packages/ens-test-env/README.md @@ -0,0 +1,96 @@ +# ens-test-env + +## How it works + +The testing environment used here is implemented in the stateless and stateful tests for ENS app. +The environment consists of two parts: the ganache Ethereum node, and the docker graph instance. +Which environment type you use is dependent on your testing circumstances. + +## Environment Types + +### Stateless + +For most testing sitatutions you can use the default settings, which will create a fresh mainnet +fork from a specified block as well as deploying a fresh subgraph with the same specified block +as it's start block. + +Tests should ideally be designed to be stateless where possible, which entails not using hardcoded +addresses and not relying on any specific blockchain/graph state. This allows for a much higher +test reliability and low maintenance. + +### Stateful + +Some tests may require a specific existing state, for example if a test relies on an old deployment +of a contract which can no longer be accurately replicated from a fresh mainnet fork. The stateful +environment uses pre-existing subgraph data at a specified block to allow full state access prior +to the mainnet fork. + +The stateful environment can also be used to more closely replicate a production environment for +true full end-to-end tests. You may also want to use this environment for testing with your own +personal wallet without using mainnet. + +The downside of using the stateful environment is that a test can potentially become unreliable if +one of it's dependencies changes. Alongside reliability, running a stateful test most of the time +will require access to a specific private key. Given this, you should try to avoid writing stateful +tests wherever possible. + +## Contract deployments + +Contract deployments are a small but neccessary part of testing ENS app, you can deploy contracts to +both stateless and stateful environments. After the locally tested contract is deployment, the +deployment script should be left in the repo to serve as an archive. + +## Updating the graph-node dataset + +Generally, you will want to set a graft variable in the `subgraph.yaml` file for the subgraph. You +can find more about the ENS subgraph [here](https://github.com/ensdomains/ens-subgraph). You'll also +documentation for grafting available [here](https://thegraph.com/docs/en/developer/create-subgraph-hosted/#grafting-onto-existing-subgraphs). + +To update the graph-node dataset, the BLOCK_HEIGHT variable must be changed within the `.env` file. +It should be set to the same value as the graft block. + +If the dataset is a dependency for a local test, you will need to first let your local graph-node +dataset update so that your test can pass. + +Once your data is up to date, you can run + +```bash +yarn archive:compress +``` + +in this directory, which will give you a archive file for your dataset. + +### Dataset naming scheme + +```js +const file = `data_${BLOCK_HEIGHT}_${SUBGRAPH_ID}_${EPOCH_TIME}_${NETWORK}.archive` +// e.g. data_14119046_QmTmU4syjQb8gfNq8TCQGv441qp2zQMNKnQ4smjKhpLQ6F_1643850493_ropsten.archive.tar.lz4 +``` + +## Running the environment + +_Run all commands in the root directory._ + +To run the test environment on your local machine, first copy the `.env.example` file and change +`FORK_RPC_URL` to an archive node of your choosing. You'll also need to have docker installed on +your machine. + +After this you can run: + +```bash +# Normal +yarn env:load && yarn env:start +# Docker root install +yarn env:load && yarn env:start:root +# CI environments (cacheless) +yarn env:start:ci +``` + +After this you can then run the app with: + +```bash +# Dev build +yarn dev:glocal +# Production build +yarn build:glocal && yarn start +``` diff --git a/packages/ens-test-env/package.json b/packages/ens-test-env/package.json index facb1c1d..8aa21cd9 100644 --- a/packages/ens-test-env/package.json +++ b/packages/ens-test-env/package.json @@ -1,6 +1,10 @@ { "name": "@ensdomains/ens-test-env", "type": "module", + "version": "0.1.1", + "files": [ + "src" + ], "dependencies": { "ansi-colors": "^4.1.1", "cli-progress": "^3.10.0", diff --git a/packages/ens-test-env/src/ens-test-env.default.config.js b/packages/ens-test-env/src/ens-test-env.default.config.js index 3f870b0e..9e663f63 100644 --- a/packages/ens-test-env/src/ens-test-env.default.config.js +++ b/packages/ens-test-env/src/ens-test-env.default.config.js @@ -23,6 +23,7 @@ module.exports = { prefixColor: 'blue.bold', cwd: path.resolve('./'), finishOnExit: true, + waitForGraph: true, }, ], paths: { diff --git a/packages/ens-test-env/src/manager.js b/packages/ens-test-env/src/manager.js index 2055b8ae..dd47b527 100644 --- a/packages/ens-test-env/src/manager.js +++ b/packages/ens-test-env/src/manager.js @@ -94,9 +94,12 @@ export const main = async (deployGraph, config) => { config.scripts && config.scripts.forEach((script, i) => { + if (script.waitForGraph) { + script.command = `yarn wait-on http://localhost:8040 && ${script.command}` + } cmdsToRun.push(script) if (script.finishOnExit) { - inxsToFinishOnExit.push(i) + inxsToFinishOnExit.push(deployGraph ? i + 1 : i) } }) diff --git a/packages/ensjs/package.json b/packages/ensjs/package.json index 4eee0dc7..956ae162 100644 --- a/packages/ensjs/package.json +++ b/packages/ensjs/package.json @@ -51,7 +51,8 @@ "ts-jest": "^27.1.4", "ts-node": "^10.7.0", "typechain": "^8.0.0", - "typescript": "^4.6.2" + "typescript": "^4.6.2", + "wait-on": "^6.0.1" }, "peerDependencies": { "ethers": "*" diff --git a/yarn.lock b/yarn.lock index ffac6ead..ebdba6e2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -525,6 +525,7 @@ __metadata: ts-node: ^10.7.0 typechain: ^8.0.0 typescript: ^4.6.2 + wait-on: ^6.0.1 peerDependencies: ethers: "*" languageName: unknown