Skip to content

Commit

Permalink
feat: test-env graphman passthrough + more
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Dec 13, 2023
1 parent 4143d48 commit b916e27
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 48 deletions.
3 changes: 2 additions & 1 deletion packages/ens-test-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"files": [
"src"
],
"types": "src/index.d.ts",
"types": "src/execute.d.ts",
"module": "src/execute.js",
"scripts": {
"prepublish": "cp ../../LICENSE ./",
"ver": "pnpm version --no-workspaces-update"
Expand Down
19 changes: 19 additions & 0 deletions packages/ens-test-env/src/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[store]
[store.primary]
connection = "postgresql://graph-node:let-me-in@postgres:5432/graph-node"
pool_size = 10

[deployment]
[[deployment.rule]]
indexers = ["index_node_1"]

[chains]
ingestor = "index_0"
[chains.mainnet]
shard = "primary"
provider = [
{ label = "mainnet-rpc-0", url = "http://anvil:8545", features = [
"archive",
"traces",
] },
]
2 changes: 2 additions & 0 deletions packages/ens-test-env/src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ services:
GRAPH_ETHEREUM_CLEANUP_BLOCKS: 'true'
extra_hosts:
- 'host.docker.internal:host-gateway'
volumes:
- $GRAPH_CONFIG_FILE:/opt/graph-node/config.toml
ipfs:
image: ipfs/kubo:v0.16.0
ports:
Expand Down
23 changes: 23 additions & 0 deletions packages/ens-test-env/src/execute.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable */
const compose = require('docker-compose')
const path = require('path')

const rewindSubgraph = async ({ blockNumber, blockHash }) => {
const dockerComposeFile = path.resolve(__dirname, './docker-compose.yml')
const composeOpts = {
log: true,
composeOptions: ['-p', 'ens-test-env'],
config: dockerComposeFile,
}
const items = await compose.ps({ ...composeOpts, log: false })
if (items.data.services.length === 0) {
throw new Error('No services running')
}
await compose.exec(
'graph-node',
`graphman --config /opt/graph-node/config.toml rewind ${blockHash} ${blockNumber} graphprotocol/ens --force --sleep 1`,
composeOpts,
)
}

module.exports.rewindSubgraph = rewindSubgraph
8 changes: 8 additions & 0 deletions packages/ens-test-env/src/execute.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */
export const rewindSubgraph = async ({
blockNumber,
blockHash,
}: {
blockNumber: number
blockHash: `0x${string}`
}) => Promise<void>
24 changes: 24 additions & 0 deletions packages/ens-test-env/src/execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable */
import compose from 'docker-compose'
import path from 'path'
import { URL as URLClass } from 'url'

const __dirname = new URLClass('.', import.meta.url).pathname

export const rewindSubgraph = async ({ blockNumber, blockHash }) => {
const dockerComposeFile = path.resolve(__dirname, './docker-compose.yml')
const composeOpts = {
log: true,
composeOptions: ['-p', 'ens-test-env'],
config: dockerComposeFile,
}
const items = await compose.ps({ ...composeOpts, log: false })
if (items.data.services.length === 0) {
throw new Error('No services running')
}
await compose.exec(
'graph-node',
`graphman --config /opt/graph-node/config.toml rewind ${blockHash} ${blockNumber} graphprotocol/ens --force --sleep 1`,
composeOpts,
)
}
1 change: 1 addition & 0 deletions packages/ens-test-env/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ program
data: path.resolve(cwd, './data'),
archive: path.resolve(cwd, './archive'),
composeFile: path.resolve(__dirname, './docker-compose.yml'),
graphConfigFile: path.resolve(__dirname, './config.toml'),
}
const configPaths = config.paths || {}
for (const [key, value] of Object.entries(configPaths)) {
Expand Down
1 change: 1 addition & 0 deletions packages/ens-test-env/src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const main = async (_config, _options, justKill) => {
opts.env = {
...process.env,
DATA_FOLDER: config.paths.data,
GRAPH_CONFIG_FILE: config.paths.graphConfigFile,
GRAPH_LOG_LEVEL: 'info',
ANVIL_EXTRA_ARGS: '',
BLOCK_TIMESTAMP: Math.floor(new Date().getTime() / 1000),
Expand Down
7 changes: 6 additions & 1 deletion packages/ensjs/deploy/02_get_contract_addresses.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ const func = async function (hre) {
]),
)

const block = await hre.ethers.provider.getBlock()

await writeFile(
resolve(__dirname, '../.env.local'),
`DEPLOYMENT_ADDRESSES='${JSON.stringify(deploymentAddressMap)}'`,
`DEPLOYMENT_ADDRESSES='${JSON.stringify(deploymentAddressMap)}'
DEPLOYMENT_FINISHED_AT_BLOCK=${block.number}
DEPLOYMENT_FINISHED_AT_HASH=${block.hash}
`,
)
console.log('Wrote contract addresses to .env.local')

Expand Down
2 changes: 2 additions & 0 deletions packages/ensjs/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const config = {
'^(\\.{1,2}/.*)\\.js$': '$1',
'^multiformats$': '<rootDir>/node_modules/multiformats/src/index.js',
'^multiformats/(.*)$': '<rootDir>/node_modules/multiformats/src/$1',
'^@ensdomains/ens-test-env$':
'<rootDir>/node_modules/@ensdomains/ens-test-env/src/execute.cjs',
},
}

Expand Down
70 changes: 70 additions & 0 deletions packages/ensjs/src/test/usage/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { rewindSubgraph } from '@ensdomains/ens-test-env'
import getPrice from '../../functions/public/getPrice.js'
import commitName from '../../functions/wallet/commitName.js'
import registerName from '../../functions/wallet/registerName.js'
import { createSubgraphClient } from '../../subgraph.js'
import type { RegistrationParameters } from '../../utils/registerHelpers.js'
import {
publicClient,
Expand Down Expand Up @@ -49,3 +51,71 @@ export const wait = (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms)
})

const subgraphClient = createSubgraphClient({ client: publicClient })

export const getSubgraphBlock = (
{ depth }: { depth: number } = { depth: 0 },
): Promise<number> =>
subgraphClient
.request<{
_meta: {
block: {
number: number
}
}
}>(
`
{
_meta {
block {
number
}
}
}
`,
)
.then((res) => res._meta.block.number)
.catch(async (e) => {
console.error(e)
await wait(1000)
if (depth > 5) throw new Error('Could not get subgraph block')
return getSubgraphBlock({ depth: depth + 1 })
})

export const getCurrentBlock = () =>
publicClient.getBlockNumber().then((b) => Number(b))

export const syncSubgraphBlock = async (
{
depth,
changes,
prevSubgraphBlock,
}: { depth: number; changes: number; prevSubgraphBlock: number } = {
depth: 0,
changes: 0,
prevSubgraphBlock: 0,
},
): Promise<void> => {
const subgraphBlock = await getSubgraphBlock()
const currentBlock = await getCurrentBlock()
if (subgraphBlock < currentBlock) {
const params = {
depth: depth + 1,
changes: changes + (subgraphBlock !== prevSubgraphBlock ? 1 : 0),
prevSubgraphBlock: subgraphBlock,
}
await wait(1000)
return syncSubgraphBlock(params)
}
if (subgraphBlock >= currentBlock && depth === 0) {
const blockNumber = parseInt(
process.env.DEPLOYMENT_FINISHED_AT_BLOCK as string,
)
const blockHash = process.env.DEPLOYMENT_FINISHED_AT_HASH as `0x${string}`
await rewindSubgraph({ blockNumber, blockHash })
await wait(1000)
return syncSubgraphBlock()
}
return
}
3 changes: 2 additions & 1 deletion packages/ensjs/src/test/usage/ownership.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../../test/addTestContracts.js'
import { encodeAbi } from '../../utils/index.js'
import type { RegistrationParameters } from '../../utils/registerHelpers.js'
import { commitAndRegisterName } from './helper.js'
import { commitAndRegisterName, syncSubgraphBlock } from './helper.js'

let snapshot: Hex
let accounts: Address[]
Expand All @@ -27,6 +27,7 @@ beforeAll(async () => {

beforeEach(async () => {
snapshot = await testClient.snapshot()
await syncSubgraphBlock()
})

afterEach(async () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/ensjs/src/test/usage/registerName.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { encodeAbi } from '../../utils/index.js'
import { namehash } from '../../utils/normalise.js'
import type { RegistrationParameters } from '../../utils/registerHelpers.js'
import { commitAndRegisterName, wait } from './helper.js'
import { commitAndRegisterName, syncSubgraphBlock } from './helper.js'

let snapshot: Hex
let accounts: Address[]
Expand All @@ -43,6 +43,7 @@ beforeAll(async () => {

beforeEach(async () => {
snapshot = await testClient.snapshot()
await syncSubgraphBlock()
})

afterEach(async () => {
Expand Down Expand Up @@ -712,7 +713,8 @@ it.only('Register - Renew Name - Add Subname - Expire Subname - Create Subname',
testClient.increaseTime({ seconds: 61 })
testClient.mine({ blocks: 1 })

await wait(30_000)
await syncSubgraphBlock()

const result = await getSubnames(publicClient, {
name: params.name,
pageSize: 1000,
Expand Down
Loading

0 comments on commit b916e27

Please sign in to comment.