Skip to content

Commit

Permalink
Skip modified compilation for namespaces if solc < 0.8.20
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Oct 10, 2023
1 parent d4f26a9 commit 41290a2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ export { ValidateUpgradeSafetyOptions, validateUpgradeSafety, ProjectReport, Ref

export { getUpgradeInterfaceVersion } from './upgrade-interface-version';
export { makeNamespacedInput } from './utils/make-namespaced';
export { isNamespaceSupported } from './storage/namespace';
5 changes: 5 additions & 0 deletions packages/core/src/storage/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getAnnotationArgs, getDocumentation, hasAnnotationTag } from '../utils/
import { Node } from 'solidity-ast/node';
import { CompilationContext, getTypeMembers, loadLayoutType } from './extract';
import { UpgradesError } from '../error';
import * as versions from 'compare-versions';

/**
* Loads a contract's namespaces and namespaced type information into the storage layout.
Expand Down Expand Up @@ -260,3 +261,7 @@ function findLayoutStructMember(
function findTypeWithLabel(types: Record<string, TypeItem>, label: string) {
return Object.values(types).find(type => type.label === label);
}

export function isNamespaceSupported(solcVersion: string) {
return versions.compare(solcVersion, '0.8.20', '>=');
}
5 changes: 2 additions & 3 deletions packages/core/src/validate/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Node } from 'solidity-ast/node';
import { isNodeType, findAll, ASTDereferencer, astDereferencer } from 'solidity-ast/utils';
import type { ContractDefinition, FunctionDefinition } from 'solidity-ast';
import debug from '../utils/debug';
import * as versions from 'compare-versions';

import { SolcOutput, SolcBytecode, SolcInput } from '../solc-api';
import { SrcDecoder } from '../src-decoder';
Expand All @@ -14,7 +13,7 @@ import { extractStorageLayout } from '../storage/extract';
import { StorageLayout } from '../storage/layout';
import { getFullyQualifiedName } from '../utils/contract-name';
import { getAnnotationArgs as getSupportedAnnotationArgs, getDocumentation } from '../utils/annotations';
import { getStorageLocationAnnotation } from '../storage/namespace';
import { getStorageLocationAnnotation, isNamespaceSupported } from '../storage/namespace';
import { UpgradesError } from '../error';

export type ValidationRunData = Record<string, ContractValidation>;
Expand Down Expand Up @@ -243,7 +242,7 @@ function checkNamespaceSolidityVersion(source: string, solcVersion?: string, sol
() =>
`Structs with the @custom:storage-location annotation can only be used with Solidity version 0.8.20 or higher. Pass the solcVersion parameter to the validate function, or remove the annotation if the struct is not used for namespaced storage.`,
);
} else if (versions.compare(solcVersion, '0.8.20', '<')) {
} else if (!isNamespaceSupported(solcVersion)) {
throw new UpgradesError(
`${source}: Namespace annotations require Solidity version >= 0.8.20, but ${solcVersion} was used`,
() =>
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin-hardhat/contracts/MakeNamespaced0516.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity ^0.5.16;

/**
* @dev foo
*/
contract MakeNamespaced0516 {
/**
* @dev bar
*/
function bar() public view {
}
}
11 changes: 7 additions & 4 deletions packages/plugin-hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ subtask(TASK_COMPILE_SOLIDITY, async (args: { force: boolean }, hre, runSuper) =
});

subtask(TASK_COMPILE_SOLIDITY_COMPILE, async (args: RunCompilerArgs, hre, runSuper) => {
const { validate, solcInputOutputDecoder, makeNamespacedInput } = await import('@openzeppelin/upgrades-core');
const { isNamespaceSupported, validate, solcInputOutputDecoder, makeNamespacedInput } = await import('@openzeppelin/upgrades-core');

Check warning on line 100 in packages/plugin-hardhat/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `'@openzeppelin/upgrades-core'` with `⏎····'@openzeppelin/upgrades-core'⏎··`
const { writeValidations } = await import('./utils/validations');

// TODO: patch input
Expand All @@ -107,9 +107,12 @@ subtask(TASK_COMPILE_SOLIDITY_COMPILE, async (args: RunCompilerArgs, hre, runSup
if (isFullSolcOutput(output)) {
const decodeSrc = solcInputOutputDecoder(args.input, output);

const namespacedInput = makeNamespacedInput(args.input, output);
const { output: namespacedOutput } = await runSuper({ ...args, quiet: true, input: namespacedInput });
await checkNamespacedCompileErrors(namespacedOutput);
let namespacedOutput = undefined;
if (isNamespaceSupported(args.solcVersion)) {
const namespacedInput = makeNamespacedInput(args.input, output);
namespacedOutput = (await runSuper({ ...args, quiet: true, input: namespacedInput })).output;
await checkNamespacedCompileErrors(namespacedOutput);
}

const validations = validate(output, decodeSrc, args.solcVersion, args.input, namespacedOutput);
await writeValidations(hre, validations);
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-hardhat/test/namespaced.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test.before(async t => {
t.context.InheritsNamespaceV2_BadAndHasLayout = await ethers.getContractFactory(
'InheritsNamespaceV2_BadAndHasLayout',
);
t.context.MakeNamespaced0516 = await ethers.getContractFactory('MakeNamespaced0516');
});

test('validate namespace - ok', async t => {
Expand Down Expand Up @@ -231,3 +232,9 @@ test('moving namespace to inherited contract - delete variable and has layout -
const error = await t.throwsAsync(() => upgrades.validateUpgrade(Example, InheritsNamespaceV2_BadAndHasLayout));
t.snapshot(error.message);
});

test('validate 0.5.16 contract without namespaces - ok', async t => {
const { MakeNamespaced0516 } = t.context;

await upgrades.validateImplementation(MakeNamespaced0516);
});

0 comments on commit 41290a2

Please sign in to comment.