Skip to content

Commit

Permalink
Modify contract change detection
Browse files Browse the repository at this point in the history
  • Loading branch information
zajck committed Jun 12, 2023
1 parent 451dc3d commit 3967780
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

find . -name '*.js' -not -path '*/node_modules/*' | xargs git diff --cached --name-only | xargs -r npx eslint --fix
find . -name '*.js' -not -path '*/node_modules/*' | xargs git diff --cached --name-only | xargs -r npx prettier --write
git diff --cached --name-only | grep -e contracts -e *.txt | xargs -r npx solhint --fix
git diff --cached --name-only | grep -e contracts -e *.txt | xargs -r npx prettier --write
git diff --cached --name-only | grep -e ^contracts -e sol$ | xargs -r npx solhint --fix
git diff --cached --name-only | grep -e ^contracts -e sol$ | xargs -r npx prettier --write
npm run natspec-interface-id:fix
1 change: 1 addition & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { task } = require("hardhat/config");
require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-web3");
require("hardhat-contract-sizer");
require("hardhat-preprocessor");

const lazyImport = async (module) => {
return await require(module);
Expand Down
32 changes: 28 additions & 4 deletions scripts/util/detect-changed-contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ Detects is contract changed between two versions
@param {string} referenceCommit - commit/tag/branch to compare to
@param {string} targetCommit - commit/tag/branch to compare. If not provided, it will compare to current branch.
*/
async function detectChangedContract(referenceCommit, targetCommit) {
async function detectChangedContract(referenceCommit, targetCommit = "HEAD") {
// By default compiler adds metadata ipfs hash to the end of bytecode.
// Even if contract is not changed, the metadata hash can be different, which makes the bytecode different and hard to detect if change has happened.
// To make comparison clean, we remove the metadata hash from the bytecode.
for (const compiler of hre.config.solidity.compilers) {
// This setting is solidity v0.8.9 style
// versions >= 0.8.18 use compiler.settings["metadata"] = {appendCBOR: false}
compiler.settings["metadata"] = { bytecodeHash: "none" };
compiler.settings["metadata"] = { bytecodeHash: "none", appendCBOR: false };
}

// Protocol versions < 2.3.0 use solidity 0.8.9. To make bytecode comparison clean, we need to replace the pragma
hre.config.preprocess = {
eachLine: () => ({
transform: (line) => {
if (line.match(/^\s*pragma /i)) {
//
line = line.replace(/solidity\s+0\.8\.9/i, "solidity 0.8.18");
}
return line;
},
}),
};

// Check if reference commit is provided
if (!referenceCommit) {
console.log("Please provide a reference commit");
Expand All @@ -40,6 +51,13 @@ async function detectChangedContract(referenceCommit, targetCommit) {
shell.exec(`rm -rf contracts`);
shell.exec(`git checkout ${referenceCommit} contracts`);

// Protocol versions < 2.3.0 use different OZ contracts. We need to install them
const isOldVersion = ["v2.0", "v2.1", "v2.2"].some((v) => referenceCommit.startsWith(v));
if (isOldVersion) {
// Temporary install old OZ contracts
shell.exec("npm i @openzeppelin/contracts-upgradeable@4.7.1");
}

// Compile old version
await hre.run("clean");
await hre.run("compile");
Expand All @@ -54,6 +72,12 @@ async function detectChangedContract(referenceCommit, targetCommit) {
console.log(`Checking out version ${targetCommit}`);
shell.exec(`git checkout ${targetCommit} contracts`);

if (isOldVersion) {
// If reference commit is old version, we need to revert to target version
shell.exec(`git checkout ${targetCommit} package.json package-lock.json`);
shell.exec("npm i");
}

// Compile new version
await hre.run("clean");
// If some contract was removed, compilation succeeds, but afterwards it falsely reports missing artifacts
Expand Down

0 comments on commit 3967780

Please sign in to comment.