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

feat(cli): speed up dev deploy with temporary automine during deploy #3130

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/real-pianos-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/cli": patch
---

Speed up deployment in development by temporarily enabling automine mode for the duration of the deployment.
8 changes: 8 additions & 0 deletions packages/cli/src/runDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { WorldDeploy } from "./deploy/common";
import { build } from "./build";
import { kmsKeyToAccount } from "@latticexyz/common/kms";
import { configToModules } from "./deploy/configToModules";
import { enableAutomine } from "./utils/enableAutomine";

export const deployOptions = {
configPath: { type: "string", desc: "Path to the MUD config file" },
Expand Down Expand Up @@ -133,6 +134,9 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {

console.log("Deploying from", client.account.address);

// Attempt to enable automine for the duration of the deploy. Noop if automine is not available.
const { reset: resetMiningMode } = await enableAutomine(client);

const startTime = Date.now();
const worldDeploy = await deploy({
deployerAddress: opts.deployerAddress as Hex | undefined,
Expand All @@ -155,6 +159,10 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
opts.kms ? true : false,
);
}

// Reset mining mode after deploy
await resetMiningMode();

console.log(chalk.green("Deployment completed in", (Date.now() - startTime) / 1000, "seconds"));

const deploymentInfo = {
Expand Down
59 changes: 59 additions & 0 deletions packages/cli/src/utils/enableAutomine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { getAutomine, getBlock, setAutomine, setIntervalMining } from "viem/actions";
import { debug, error } from "../debug";
import { Client } from "viem";
import { getAction } from "viem/utils";

type MiningMode =
| {
type: "automine";
}
| {
type: "interval";
blockTime: number;
};

export type EnableAutomineResult = { reset: () => Promise<void> };

export async function enableAutomine(client: Client): Promise<EnableAutomineResult> {
try {
debug("Enabling automine");
const prevMiningMode = await getMiningMode(client);
await setMiningMode(client, { type: "automine" });
return {
reset: () => {
debug("Disabling automine");
return setMiningMode(client, prevMiningMode);
},
};
} catch (e) {
debug("Skipping automine");
error(e);
return { reset: async () => void 0 };
}
}

async function getMiningMode(client: Client): Promise<MiningMode> {
const localClient = { mode: "anvil", ...client }; // set default mode to "anvil", potential error is caught by enableAutomine
const isAutomine = await getAction(localClient, getAutomine, "getAutomine")({});
if (isAutomine) {
return { type: "automine" };
}

const blockTime = await getBlockTime(client);
return { type: "interval", blockTime };
}

async function setMiningMode(client: Client, miningMode: MiningMode): Promise<void> {
if (miningMode.type === "automine") {
await getAction(client, setAutomine, "setAutomine")(true);
} else {
await getAction(client, setIntervalMining, "setIntervalMining")({ interval: miningMode.blockTime });
}
}

async function getBlockTime(client: Client): Promise<number> {
const latestBlock = await getAction(client, getBlock, "getBlock")({ blockTag: "latest" });
const previousBlock = await getAction(client, getBlock, "getBlock")({ blockNumber: latestBlock.number - 1n });
const blockTime = latestBlock.timestamp - previousBlock.timestamp;
return Number(blockTime);
}
Loading