Skip to content

Commit

Permalink
fix(build): update build command to not write to remote ipfs (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjlescano authored Apr 11, 2024
1 parent 95cccb7 commit a200690
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 31 deletions.
65 changes: 62 additions & 3 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usecannon/cli",
"version": "2.12.1",
"version": "2.12.1-alpha.19+f8a4e83d",
"description": "Utility for instantly loading cannon packages in standalone contexts",
"main": "dist/src/index.js",
"scripts": {
Expand Down Expand Up @@ -75,5 +75,5 @@
"znv": "^0.4.0",
"zod": "^3.22.4"
},
"gitHead": "a20d4d65cbeb3e9ebf23da3ba411d33c1a6a20f5"
"gitHead": "f8a4e83d010696c5f1939bf4aeb53e86038d6915"
}
3 changes: 2 additions & 1 deletion packages/cli/src/commands/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ describe('build', () => {
});

describe('provider', () => {
let provider: viem.PublicClient;
let provider: viem.PublicClient & viem.WalletClient & viem.TestClient;

beforeEach(() => {
jest.spyOn(helpers, 'loadCannonfile').mockResolvedValue({} as any);
provider = makeFakeProvider();
Expand Down
15 changes: 11 additions & 4 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ export async function build({
getSigner:
getSigner ||
async function (addr: viem.Address) {
const client = provider as unknown as viem.TestClient;

// on test network any user can be conjured
await (provider as unknown as viem.TestClient).impersonateAccount({ address: addr });
await (provider as unknown as viem.TestClient).setBalance({ address: addr, value: viem.parseEther('10000') });
await client.impersonateAccount({ address: addr });
await client.setBalance({ address: addr, value: viem.parseEther('10000') });

return {
address: addr,
Expand All @@ -156,7 +158,12 @@ export async function build({

const resolver = overrideResolver || (await createDefaultReadRegistry(cliSettings));

const runtime = new ChainBuilderRuntime(runtimeOptions, resolver, getMainLoader(cliSettings), 'ipfs');
const runtime = new ChainBuilderRuntime(
runtimeOptions,
resolver,
getMainLoader(cliSettings, { writeToIpfs: false }),
'ipfs'
);

const dump = writeScript ? await createWriteScript(runtime, writeScript, writeScriptFormat) : null;

Expand Down Expand Up @@ -240,7 +247,7 @@ export async function build({

let defaultSignerAddress: string;
if (getDefaultSigner) {
const defaultSigner = await getDefaultSigner!();
const defaultSigner = await getDefaultSigner();
if (defaultSigner) {
defaultSignerAddress = defaultSigner.address;
console.log(`Using ${defaultSignerAddress}`);
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import { spawn } from 'node:child_process';
import path from 'node:path';
import {
Expand All @@ -17,18 +16,19 @@ import {
import { blueBright, bold, gray, green, red, yellow } from 'chalk';
import { Command } from 'commander';
import Debug from 'debug';
import _ from 'lodash';
import prompts from 'prompts';
import * as viem from 'viem';
import pkg from '../package.json';
import { interact } from './commands/interact';
import commandsConfig from './commandsConfig';
import {
checkAndNormalizePrivateKey,
normalizePrivateKey,
checkCannonVersion,
checkForgeAstSupport,
ensureChainIdConsistency,
isPrivateKey,
normalizePrivateKey,
} from './helpers';
import { getMainLoader } from './loader';
import { installPlugin, listInstalledPlugins, removePlugin } from './plugins';
Expand All @@ -40,7 +40,7 @@ import { pickAnvilOptions } from './util/anvil';
import { doBuild } from './util/build';
import { getContractsRecursive } from './util/contracts-recursive';
import { parsePackageArguments, parsePackagesArguments } from './util/params';
import { resolveRegistryProviders, resolveWriteProvider, getChainIdFromProviderUrl, isURL } from './util/provider';
import { getChainIdFromProviderUrl, isURL, resolveRegistryProviders, resolveWriteProvider } from './util/provider';
import { writeModuleDeployments } from './util/write-deployments';
import './custom-steps/run';

Expand Down Expand Up @@ -216,14 +216,16 @@ applyCommandsConfig(program.command('build'), commandsConfig.build)
} else {
console.log(red('forge build failed'));
console.log(red('Make sure "forge build" runs successfully or use the --skip-compile flag.'));
reject(new Error(`forge build failed with exit code "${code}"`));
return reject(new Error(`forge build failed with exit code "${code}"`));
}

resolve(null);
});
});
} else {
console.log(yellow('Skipping forge build...'));
}

console.log(''); // Linebreak in CLI to signify end of compilation.

// Override options with CLI settings
Expand Down
16 changes: 7 additions & 9 deletions packages/cli/src/loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import fs from 'fs-extra';
import crypto from 'crypto';
import fs from 'fs-extra';
import path from 'path';
import { CliLoader, LocalLoader, getMainLoader } from './loader'; // assuming the module's name is "module.ts"
import { CliLoader, getMainLoader, LocalLoader } from './loader'; // assuming the module's name is "module.ts"
import { CliSettings } from './settings';

jest.mock('fs-extra');
jest.mock('crypto');

describe('LocalLoader', LocalLoaderTestCases);
describe('getMainLoader', getMainLoaderTestCases);

function LocalLoaderTestCases() {
describe('LocalLoader', function LocalLoaderTestCases() {
const dir = 'directory';
const loader = new LocalLoader(dir);

Expand Down Expand Up @@ -45,12 +42,13 @@ function LocalLoaderTestCases() {
expect(fs.writeFile).toHaveBeenCalledWith(path.join(dir, `${hash}.json`), json);
expect(result).toEqual(`file://${hash}.json`);
});
}
});

function getMainLoaderTestCases() {
describe('getMainLoader', function getMainLoaderTestCases() {
beforeEach(() => {
jest.clearAllMocks();
});

it('should return object with instances of loaders', () => {
const settings: CliSettings = {
providerUrl: '',
Expand Down Expand Up @@ -84,4 +82,4 @@ function getMainLoaderTestCases() {
const loaders = getMainLoader(settings);
expect(loaders.ipfs).toBeInstanceOf(CliLoader); // Changed this line
});
}
});
14 changes: 9 additions & 5 deletions packages/cli/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CannonLoader, IPFSLoader, getCannonRepoRegistryUrl } from '@usecannon/builder';
import { CannonLoader, getCannonRepoRegistryUrl, IPFSLoader } from '@usecannon/builder';
import { compress, getContentCID } from '@usecannon/builder/dist/ipfs';
import crypto from 'crypto';
import Debug from 'debug';
Expand Down Expand Up @@ -67,11 +67,13 @@ export class CliLoader implements CannonLoader {
ipfs?: IPFSLoader;
repo: IPFSLoader;
dir: string;
writeToIpfs = true;

constructor(ipfsLoader: IPFSLoader | undefined, repoLoader: IPFSLoader, fileCacheDir: string) {
constructor(ipfsLoader: IPFSLoader | undefined, repoLoader: IPFSLoader, fileCacheDir: string, writeToIpfs = true) {
this.ipfs = ipfsLoader;
this.repo = repoLoader;
this.dir = fileCacheDir;
this.writeToIpfs = writeToIpfs;
}

getLabel() {
Expand All @@ -93,7 +95,8 @@ export class CliLoader implements CannonLoader {
await fs.mkdirp(this.dir);
await fs.writeFile(this.getCacheFilePath(url), data);

if (this.ipfs) {
if (this.writeToIpfs) {
if (!this.ipfs) throw new Error('Missing IPFS loader');
await this.ipfs.put(misc);
}

Expand Down Expand Up @@ -189,14 +192,15 @@ export class IPFSLoaderWithRetries extends IPFSLoader {
}
}

export function getMainLoader(cliSettings: CliSettings) {
export function getMainLoader(cliSettings: CliSettings, { writeToIpfs = true } = {}) {
return {
ipfs: new CliLoader(
cliSettings.ipfsUrl
? new IPFSLoaderWithRetries(cliSettings.ipfsUrl, {}, cliSettings.ipfsTimeout, cliSettings.ipfsRetries)
: undefined,
new IPFSLoaderWithRetries(getCannonRepoRegistryUrl(), {}, cliSettings.ipfsTimeout, cliSettings.ipfsRetries),
path.join(cliSettings.cannonDirectory, 'ipfs_cache')
path.join(cliSettings.cannonDirectory, 'ipfs_cache'),
writeToIpfs
),
file: new LocalLoader(path.join(cliSettings.cannonDirectory, 'blobs')),
};
Expand Down
6 changes: 3 additions & 3 deletions packages/hardhat-cannon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hardhat-cannon",
"version": "2.12.1",
"version": "2.12.1-alpha.19+f8a4e83d",
"description": "Agnostic chain construction. Select the protocols and configuration you need to quickly and easily verify your project",
"repository": "github:usecannon/cannon",
"author": "Synthetix",
Expand Down Expand Up @@ -43,11 +43,11 @@
"dependencies": {
"@iarna/toml": "^3.0.0",
"@usecannon/builder": "2.12.1",
"@usecannon/cli": "2.12.1",
"@usecannon/cli": "2.12.1-alpha.19+f8a4e83d",
"chalk": "^4.1.2",
"debug": "^4.3.3",
"fs-extra": "^10.0.1",
"viem": "^2.9.3"
},
"gitHead": "a20d4d65cbeb3e9ebf23da3ba411d33c1a6a20f5"
"gitHead": "f8a4e83d010696c5f1939bf4aeb53e86038d6915"
}

0 comments on commit a200690

Please sign in to comment.