Skip to content

Commit

Permalink
Skip modified compilation for namespaces if solc < 0.8.20 (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau authored Oct 11, 2023
1 parent d4f26a9 commit 760e4be
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changelog

## Unreleased

- Fix Hardhat compile error when using Solidity 0.5.x. ([#892](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/892))

## 1.30.0 (2023-09-27)

- Support new upgrade interface in OpenZeppelin Contracts 5.0. ([883](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/883))
- Support new upgrade interface in OpenZeppelin Contracts 5.0. ([#883](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/883))
- Add validations for namespaced storage layout. ([#876](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/876))
- Deprecate low-level API. Use [CLI or high-level API](https://docs.openzeppelin.com/upgrades-plugins/1.x/api-core) instead.

Expand Down
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
4 changes: 4 additions & 0 deletions packages/plugin-hardhat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fix Hardhat compile error when using Solidity 0.5.x. ([#892](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/892))

## 2.3.1 (2023-09-28)

- Check for non-zero admin address when importing transparent proxy. ([#887](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/887))
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 {
}
}
13 changes: 9 additions & 4 deletions packages/plugin-hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ 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'
);
const { writeValidations } = await import('./utils/validations');

// TODO: patch input
Expand All @@ -107,9 +109,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 760e4be

Please sign in to comment.