Skip to content

Commit

Permalink
fix: fixing bugs related to cloned packages (#869)
Browse files Browse the repository at this point in the history
Co-authored-by: Matías <mjlescano@protonmail.com>
  • Loading branch information
FuzzB0t and mjlescano authored Apr 12, 2024
1 parent 7dc408f commit a98c989
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 49 deletions.
14 changes: 6 additions & 8 deletions examples/sample-foundry-project/package-lock.json

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

3 changes: 2 additions & 1 deletion examples/sample-hardhat-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ task('accounts', 'Prints the list of accounts', async (taskArgs, hre) => {
}
});

// need to use `any` type here instead of HardhatUserConfig becuase somethign borky is going on with typescript resolution of cannon config overrides
// need to use `any` type here instead of HardhatUserConfig because something borky is going on with typescript resolution of cannon config overrides
const config: any = {
solidity: '0.8.4',
defaultNetwork: 'cannon',
Expand All @@ -38,6 +38,7 @@ const config: any = {
},
mainnet: {
url: process.env.PROVIDER_URL || `https://mainnet.infura.io/v3/${process.env.INFURA_KEY}`,
accounts: process.env.PRIVATE_KEY?.split(','),
chainId: 1,
},
goerli: {
Expand Down
6 changes: 3 additions & 3 deletions examples/sample-hardhat-project/package-lock.json

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

25 changes: 15 additions & 10 deletions packages/builder/src/steps/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const cloneSpec = {
packageState: PackageState
): Promise<ChainArtifacts> {
const importLabel = packageState.currentLabel.split('.')[1] || '';
debug('exec', config);
debug(`[clone.${importLabel}]`, 'exec', config);

const targetPreset = config.targetPreset ?? 'main';
const sourcePreset = config.sourcePreset;
Expand All @@ -122,7 +122,7 @@ const cloneSpec = {

const importPkgOptions = { ...(deployInfo?.options || {}), ...(config.var || config.options || {}) };

debug('cloning package options', importPkgOptions);
debug(`[clone.${importLabel}]`, 'cloning package options', importPkgOptions);

const def = new ChainDefinition(deployInfo.def);

Expand All @@ -132,7 +132,7 @@ const cloneSpec = {
let prevMiscUrl = null;
if (ctx.imports[importLabel]?.url) {
const prevUrl = ctx.imports[importLabel].url!;
debug(`using state from previous deploy: ${prevUrl}`);
debug(`[clone.${importLabel}]`, `using state from previous deploy: ${prevUrl}`);
const prevDeployInfo = await runtime.readBlob(prevUrl);
prevState = prevDeployInfo!.state;
prevMiscUrl = prevDeployInfo!.miscUrl;
Expand All @@ -141,13 +141,14 @@ const cloneSpec = {
// if there is, we need to overwrite it. print out a warning.
if (await runtime.readDeploy(source, runtime.chainId)) {
debug(
`[clone.${importLabel}]`,
yellow(
'There is a pre-existing deployment for this preset and chain id. This build will overwrite. Did you mean `import`?'
)
);
}

debug('no previous state found, deploying from scratch');
debug(`[clone.${importLabel}]`, 'no previous state found, deploying from scratch');
}

// TODO: needs npm package from the manifest
Expand All @@ -170,20 +171,24 @@ const cloneSpec = {

// need to import the misc data for the imported package
if (prevMiscUrl) {
debug('load misc');
debug(`[clone.${importLabel}]`, 'load misc');
await importRuntime.restoreMisc(prevMiscUrl);
}

debug('start build');
debug(`[clone.${importLabel}]`, 'start build');
const builtState = await build(importRuntime, def, prevState, initialCtx);
if (importRuntime.isCancelled()) {
partialDeploy = true;
}

debug('finish build. is partial:', partialDeploy);
debug(`[clone.${importLabel}]`, 'finish build. is partial:', partialDeploy);

if (!_.isEmpty(prevState) && _.isEqual(builtState, prevState)) {
debug('built state is exactly equal to previous state. skip generation of new deploy url');
debug(
`[clone.${importLabel}]`,
'built state is exactly equal to previous state. skip generation of new deploy url',
importLabel
);
return {
imports: {
[importLabel]: ctx.imports[importLabel],
Expand All @@ -193,7 +198,7 @@ const cloneSpec = {

const newMiscUrl = await importRuntime.recordMisc();

debug('new misc:', newMiscUrl);
debug(`[clone.${importLabel}]`, 'new misc:', newMiscUrl);

// need to save state to IPFS now so we can access it in future builds
const newSubDeployUrl = await runtime.putDeploy({
Expand All @@ -210,7 +215,7 @@ const cloneSpec = {
});

if (!newSubDeployUrl) {
debug('warn: cannot record built state for import nested state');
debug(`[clone.${importLabel}]`, 'warn: cannot record built state for import nested state');
} else {
await runtime.registry.publish(
[target, ...(config.tags || ['latest']).map((t) => config.source.split(':')[0] + ':' + t)],
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class CliLoader implements CannonLoader {
}

getCacheFilePath(url: string) {
return path.join(this.dir, `qmhash-${CliLoader.getCacheHash(url)}.json`);
return path.join(this.dir, `${CliLoader.getCacheHash(url)}.json`);
}

async put(misc: any): Promise<string> {
Expand Down Expand Up @@ -148,7 +148,11 @@ export class CliLoader implements CannonLoader {
}

static getCacheHash(url: string) {
return crypto.createHash('md5').update(url.replace(IPFSLoader.PREFIX, '')).digest('hex');
const qmhash = url.replace(IPFSLoader.PREFIX, '');
const md5 = crypto.createHash('md5').update(qmhash).digest('hex');
// Whe need to add an md5 to make sure that there are not collisions,
// And we CANNOT use directly the Qm... hash because files are not case sensitive.
return `${md5}-${qmhash.toLowerCase()}`;
}
}

Expand Down
31 changes: 27 additions & 4 deletions packages/cli/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ export type CliSettings = {
address: viem.Address;
}[];

/**
* URL to use to write a package to the registry. Defaults to `frame,${DEFAULT_REGISTRY_PROVIDER_URL}`
*/
registryProviderUrl?: string;

/**
* chain Id of the registry. Defaults to `1`.
*/
registryChainId?: string;

/**
* Address of the registry.
*/
registryAddress?: string;

/**
* Which registry to read from first. Defaults to `onchain`
*/
Expand Down Expand Up @@ -137,12 +152,20 @@ function cannonSettingsSchema(fileSettings: Omit<CliSettings, 'cannonDirectory'>
.url()
.optional()
.default(fileSettings.publishIpfsUrl as string),
CANNON_REGISTRY_PROVIDER_URL: z.string().optional(),
CANNON_REGISTRY_CHAIN_ID: z.string().optional(),
CANNON_REGISTRY_PROVIDER_URL: z
.string()
.url()
.optional()
.default(fileSettings.registryProviderUrl || DEFAULT_REGISTRY_CONFIG[0].providerUrl[0]),
CANNON_REGISTRY_CHAIN_ID: z
.string()
.optional()
.default(fileSettings.registryChainId || DEFAULT_REGISTRY_CONFIG[0].chainId.toString()),
CANNON_REGISTRY_ADDRESS: z
.string()
.optional()
.refine((v) => !v || viem.isAddress(v), 'must be address'),
.refine((v) => !v || viem.isAddress(v), 'must be address')
.default(fileSettings.registryAddress || DEFAULT_REGISTRY_CONFIG[0].address),
CANNON_REGISTRY_PRIORITY: z.enum(['onchain', 'local']).default(fileSettings.registryPriority || 'onchain'),
CANNON_ETHERSCAN_API_URL: z
.string()
Expand Down Expand Up @@ -210,7 +233,7 @@ function _resolveCliSettings(overrides: Partial<CliSettings> = {}): CliSettings
// Check and normalize private keys
finalSettings.privateKey = checkAndNormalizePrivateKey(finalSettings.privateKey);

if (CANNON_REGISTRY_PROVIDER_URL && CANNON_REGISTRY_CHAIN_ID) {
if (CANNON_REGISTRY_PROVIDER_URL && CANNON_REGISTRY_CHAIN_ID && CANNON_REGISTRY_ADDRESS) {
finalSettings.registries.push({
providerUrl: [CANNON_REGISTRY_PROVIDER_URL],
chainId: parseInt(CANNON_REGISTRY_CHAIN_ID),
Expand Down
38 changes: 29 additions & 9 deletions packages/cli/test/e2e/e2e-tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,27 @@ teardown() {
assert_success
}

@test "Build - Building foundry greeter example" {
set_custom_config # Uses custom settings.json
run build-foundry.sh
@test "Build - Building foundry greeter example locally" {
run build-foundry-local.sh
echo $output
assert_success
assert_file_exists "$CANNON_DIRECTORY/tags/greeter-foundry_latest_1-main.txt"
assert_file_exists "$CANNON_DIRECTORY/tags/greeter-foundry_latest_13370-main.txt"
}

@test "Build - Building foundry greeter example live" {
set_custom_config
run build-foundry-live.sh
echo $output
assert_success
assert_file_exists "$CANNON_DIRECTORY/tags/greeter-foundry_latest_1-main.txt"
}

@test "Build - Building hardhat greeter example" {
set_custom_config # Uses custom settings.json
set_custom_config
run build-hardhat.sh
echo $output
assert_success
assert_file_exists "$CANNON_DIRECTORY/tags/greeter_latest_1-main.txt"
assert_file_exists "$CANNON_DIRECTORY/tags/greeter_latest_13370-main.txt"
}

Expand All @@ -124,16 +131,29 @@ teardown() {
}

@test "Build - Building hardhat router example live network" {
set_custom_config # Uses custom settings.json
set_custom_config
run build-router-live.sh
echo $output
assert_output --partial 'examples-router-architecture:0.0.1@main built on Ethereum (Chain ID: 1)'
assert_file_exists "$CANNON_DIRECTORY/tags/examples-router-architecture_latest_13370-main.txt"
assert_file_exists "$CANNON_DIRECTORY/tags/examples-router-architecture_latest_1-main.txt"
assert_success
}

@test "Partial Build - Ensure integrity of cloned packages in partial deployment state" {
set_custom_config
run build-foundry-partial.sh
echo $output
assert_success
assert_output --partial "Your deployment was not fully completed. Please inspect the issues listed above and resolve as necessary."
assert_file_exists "$CANNON_DIRECTORY/tags/oracle-manager_latest_1-with-owned-greeter.txt"
assert_file_exists "$CANNON_DIRECTORY/tags/owned-greeter_1.0.0_1-main.txt"
assert_file_exists "$CANNON_DIRECTORY/tags/mintable-token_latest_1-main.txt"
assert_file_exists "$CANNON_DIRECTORY/tags/trusted-multicall-forwarder_latest_1-main.txt"
assert_file_exists "$CANNON_DIRECTORY/tags/trusted-multicall-forwarder_latest_1-with-oracle-manager.txt"
}

@test "Verify - Verify greeter packages" {
set_custom_config # Uses custom settings.json
set_custom_config
run verify.sh
echo $output
assert_success
Expand All @@ -148,7 +168,7 @@ teardown() {
}

@test "Publish - Publishing greeter package" {
set_custom_config # Uses custom settings.json
set_custom_config
run publish.sh
echo $output
assert_success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,3 @@ $CANNON build $CANNON_REPO_DIR/examples/sample-foundry-project/cannonfile.toml -
output=$($CANNON build $CANNON_REPO_DIR/examples/sample-foundry-project/cannonfile.toml --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --chain-id 1)

[[ ! "$output" =~ "Executing" ]]

# test building the package again with no private key supplied
$CANNON build $CANNON_REPO_DIR/examples/sample-foundry-project/cannonfile.toml

# inspect, verify that some important properties of the build are preserved
inspectResult=$($CANNON inspect greeter-foundry:latest)

# deploy status is complete
[[ "$inspectResult" =~ "Deploy Status: complete" ]]
# no source codes should have been included
[[ "$inspectResult" =~ "0 sources included" ]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# test building the package again with no private key supplied
$CANNON build $CANNON_REPO_DIR/examples/sample-foundry-project/cannonfile.toml

# inspect, verify that some important properties of the build are preserved
inspectResult=$($CANNON inspect greeter-foundry:latest)

# deploy status is complete
[[ "$inspectResult" =~ "Deploy Status: complete" ]]
# no source codes should have been included
[[ "$inspectResult" =~ "0 sources included" ]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
git clone --depth=1 https://github.com/FuzzB0t/partial-build-example
cd partial-build-example
npm i

$CANNON build cannonfile.withcloned.toml --chain-id 1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cd $CANNON_REPO_DIR/examples/sample-hardhat-project;
npm i
npx hardhat cannon:build
npx hardhat cannon:build --network mainnet
PROVIDER_URL=http://127.0.0.1:9545 PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 npx hardhat cannon:build --network mainnet
cd -

# inspect, verify that some important properties of the build are preserved
Expand Down

0 comments on commit a98c989

Please sign in to comment.