Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fork feature #405

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INFURA_KEY=<fillme>
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ jobs:

- name: Test 🧪
run: npm test
env:
INFURA_KEY: ${{ secrets.INFURA_KEY }}
134 changes: 19 additions & 115 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@typescript-eslint/parser": "^5.36.2",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"dotenv": "^16.3.1",
"eslint": "^8.23.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
Expand Down
8 changes: 8 additions & 0 deletions registry/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ const config: HardhatUserConfig = {
networks: {
// we need automining for the validator event processor to work
hardhat: {
forking: process.env.FORK
? {
url: process.env.FORK,
blockNumber: process.env.FORK_BLOCK_NUMBER
? Number(process.env.FORK_BLOCK_NUMBER)
: undefined,
}
: undefined,
mining: {
auto: !(process.env.HARDHAT_DISABLE_AUTO_MINING === "true"),
interval: [100, 3000],
Expand Down
68 changes: 40 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,35 @@ class LocalTableland {
}

// make sure we are starting fresh
// TODO: I don't think this is doing anything anymore...
this.#_cleanup();

// Run a local hardhat node
this.registry = spawn(
isWindows() ? "npx.cmd" : "npx",
["hardhat", "node"],
{
// we can't run in windows if we use detached mode
detached: !isWindows(),
cwd: this.registryDir,
env: {
...process.env,
HARDHAT_NETWORK: "hardhat",
HARDHAT_UNLIMITED_CONTRACT_SIZE: "true",
},
// There's two ways of signaling a mainnet fork should be used. The `FORK`
// env var, or the config has a fork property. Either way this means we
// don't need to deploy the registry, and the validator should listen to
// a different contract address, specifically the mainnet address.
const shouldFork = !!(process.env.FORK || config.fork);
const hardhatCommandArr = ["hardhat", "node"];

if (config.fork) {
hardhatCommandArr.push("--fork");
hardhatCommandArr.push(config.fork);
if (config.forkBlockNumber) {
hardhatCommandArr.push("--fork-block-number");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the JS class config is set, but the dev can also use the command flags or env vars to specify.

hardhatCommandArr.push(config.forkBlockNumber);
}
);
}

// Run a local hardhat node
this.registry = spawn(isWindows() ? "npx.cmd" : "npx", hardhatCommandArr, {
// we can't run in windows if we use detached mode
detached: !isWindows(),
cwd: this.registryDir,
env: {
...process.env,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.env might also have fork config.

HARDHAT_NETWORK: "hardhat",
HARDHAT_UNLIMITED_CONTRACT_SIZE: "true",
},
});

this.registry.on("error", (err) => {
throw new Error(`registry errored with: ${err}`);
Expand All @@ -106,18 +117,19 @@ class LocalTableland {
// wait until initialization is done
await waitForReady(registryReadyEvent, this.initEmitter);

// Deploy the Registry to the Hardhat node
logSync(
spawnSync(
isWindows() ? "npx.cmd" : "npx",
["hardhat", "run", "--network", "localhost", "scripts/deploy.ts"],
{
cwd: this.registryDir,
}
),
!inDebugMode()
);

if (!shouldFork) {
// Deploy the Registry to the Hardhat node
logSync(
spawnSync(
isWindows() ? "npx.cmd" : "npx",
["hardhat", "run", "--network", "localhost", "scripts/deploy.ts"],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only deploy the registry if NOT using a fork, since the fork already has the registry deployed.

{
cwd: this.registryDir,
}
),
!inDebugMode()
);
}
// need to determine if we are starting the validator via docker
// and a local repo, or if are running a binary etc...
const ValidatorClass = this.validatorDir ? ValidatorDev : ValidatorPkg;
Expand All @@ -127,7 +139,7 @@ class LocalTableland {
// run this before starting in case the last instance of the validator didn't get cleanup after
// this might be needed if a test runner force quits the parent local-tableland process
this.validator.cleanup();
this.validator.start();
this.validator.start(shouldFork);

// TODO: It seems like this check isn't sufficient to see if the process is gonna get to a point
// where the on error listener can be attached.
Expand Down
40 changes: 37 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,27 @@ overrideDefaults(getChainId("local-tableland"), {

export type ConfigDescriptor = {
name: string;
env: "VALIDATOR_DIR" | "REGISTRY_DIR" | "VERBOSE" | "SILENT";
file: "validatorDir" | "registryDir" | "verbose" | "silent";
arg: "validator" | "registry" | "verbose" | "silent";
env:
| "VALIDATOR_DIR"
| "REGISTRY_DIR"
| "VERBOSE"
| "SILENT"
| "FORK"
| "FORK_BLOCK_NUMBER";
file:
| "validatorDir"
| "registryDir"
| "verbose"
| "silent"
| "fork"
| "forkBlockNumber";
arg:
| "validator"
| "registry"
| "verbose"
| "silent"
| "fork"
| "forkBlockNumber";
isPath: boolean;
};

Expand Down Expand Up @@ -62,6 +80,20 @@ const configDescriptors: ConfigDescriptor[] = [
arg: "silent",
isPath: false,
},
{
name: "fork",
env: "FORK",
file: "fork",
arg: "fork",
isPath: false,
},
{
name: "forkBlockNumber",
env: "FORK_BLOCK_NUMBER",
file: "forkBlockNumber",
arg: "forkBlockNumber",
isPath: false,
},
];

export type Config = {
Expand All @@ -71,6 +103,8 @@ export type Config = {
registryDir?: string;
verbose?: boolean;
silent?: boolean;
fork?: string;
forkBlockNumber?: string;
};

export const buildConfig = function (config: Config) {
Expand Down
Loading