Skip to content

Commit

Permalink
feat(cli): speed up dev deploy with temporary automine during deploy (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs authored Sep 6, 2024
1 parent 7e2cdad commit d3ab5c3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
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);
}

0 comments on commit d3ab5c3

Please sign in to comment.