From 7f8e91b5a7fab3a26f7b41f965811cc4e76e57c5 Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 9 Oct 2023 19:36:43 +0530 Subject: [PATCH 1/9] add: contract sizer --- hardhat.config.js | 5 ++ package-lock.json | 133 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 139 insertions(+) diff --git a/hardhat.config.js b/hardhat.config.js index 709fbfc6..6156c53e 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -2,6 +2,7 @@ require('dotenv').config(); require('@nomicfoundation/hardhat-toolbox'); require('solidity-coverage'); require('solidity-docgen'); +require("hardhat-contract-sizer"); const env = process.env.ENV || 'testnet'; const { importNetworks, readJSON } = require('@axelar-network/axelar-contract-deployments/evm/utils'); @@ -44,4 +45,8 @@ module.exports = { gasReporter: { enabled: process.env.REPORT_GAS !== undefined, }, + contractSizer: { + runOnCompile: true, + strict: true, + } }; diff --git a/package-lock.json b/package-lock.json index e0162a71..ca83e986 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "eslint-config-richardpringle": "^2.0.0", "fs-extra": "^11.1.1", "hardhat": "^2.13.0", + "hardhat-contract-sizer": "^2.10.0", "mocha": "^10.2.0", "prettier": "^2.8.8", "prettier-plugin-solidity": "^1.1.3", @@ -151,6 +152,16 @@ "case": "^1.6.3" } }, + "node_modules/@colors/colors": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz", + "integrity": "sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==", + "dev": true, + "optional": true, + "engines": { + "node": ">=0.1.90" + } + }, "node_modules/@cspotcode/source-map-support": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", @@ -6022,6 +6033,128 @@ } } }, + "node_modules/hardhat-contract-sizer": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/hardhat-contract-sizer/-/hardhat-contract-sizer-2.10.0.tgz", + "integrity": "sha512-QiinUgBD5MqJZJh1hl1jc9dNnpJg7eE/w4/4GEnrcmZJJTDbVFNe3+/3Ep24XqISSkYxRz36czcPHKHd/a0dwA==", + "dev": true, + "dependencies": { + "chalk": "^4.0.0", + "cli-table3": "^0.6.0", + "strip-ansi": "^6.0.0" + }, + "peerDependencies": { + "hardhat": "^2.0.0" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/cli-table3": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/cli-table3/-/cli-table3-0.6.3.tgz", + "integrity": "sha512-w5Jac5SykAeZJKntOxJCrm63Eg5/4dhMWIcuTbo9rpE+brgaSZo0RuNJZeOyMgsUdhDeojvgyQLmjI+K50ZGyg==", + "dev": true, + "dependencies": { + "string-width": "^4.2.0" + }, + "engines": { + "node": "10.* || >= 12.*" + }, + "optionalDependencies": { + "@colors/colors": "1.5.0" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "node_modules/hardhat-contract-sizer/node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dev": true, + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/hardhat-contract-sizer/node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/hardhat-gas-reporter": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/hardhat-gas-reporter/-/hardhat-gas-reporter-1.0.9.tgz", diff --git a/package.json b/package.json index ad1e27ce..b82554ad 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "eslint-config-richardpringle": "^2.0.0", "fs-extra": "^11.1.1", "hardhat": "^2.13.0", + "hardhat-contract-sizer": "^2.10.0", "mocha": "^10.2.0", "prettier": "^2.8.8", "prettier-plugin-solidity": "^1.1.3", From 158143538e13fb4a4b3921eda4c4b1c82d94567a Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Mon, 9 Oct 2023 19:41:49 +0530 Subject: [PATCH 2/9] fix: prettier and lint --- hardhat.config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hardhat.config.js b/hardhat.config.js index 6156c53e..65054730 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -2,7 +2,7 @@ require('dotenv').config(); require('@nomicfoundation/hardhat-toolbox'); require('solidity-coverage'); require('solidity-docgen'); -require("hardhat-contract-sizer"); +require('hardhat-contract-sizer'); const env = process.env.ENV || 'testnet'; const { importNetworks, readJSON } = require('@axelar-network/axelar-contract-deployments/evm/utils'); @@ -48,5 +48,5 @@ module.exports = { contractSizer: { runOnCompile: true, strict: true, - } + }, }; From 714121e8de06e51459116fcc0c5881e0ed92970f Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Tue, 10 Oct 2023 11:54:13 +0530 Subject: [PATCH 3/9] chore: update hardhat config to enable contractSizer only if set in .env --- hardhat.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardhat.config.js b/hardhat.config.js index 65054730..95ee3b74 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -46,7 +46,7 @@ module.exports = { enabled: process.env.REPORT_GAS !== undefined, }, contractSizer: { - runOnCompile: true, + runOnCompile: process.env.CONTRACT_SIZER_ENABLED === 'true', strict: true, }, }; From 2001c7c183500919c688e126e30bdbe679848286 Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Tue, 10 Oct 2023 13:51:05 +0530 Subject: [PATCH 4/9] add: test to check contract size donot exceeds 24kb --- test/tokenService.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/tokenService.js b/test/tokenService.js index 93b0bca9..5b21a056 100644 --- a/test/tokenService.js +++ b/test/tokenService.js @@ -3,10 +3,12 @@ const chai = require('chai'); const { expect } = chai; require('dotenv').config(); +const fs = require('fs'); const { ethers } = require('hardhat'); const { AddressZero, MaxUint256 } = ethers.constants; const { defaultAbiCoder, solidityPack, keccak256 } = ethers.utils; const { Contract, Wallet } = ethers; +const path = require('path'); const TokenManager = require('../artifacts/contracts/token-manager/TokenManager.sol/TokenManager.json'); const Token = require('../artifacts/contracts/interfaces/IStandardizedToken.sol/IStandardizedToken.json'); @@ -1460,3 +1462,36 @@ describe('Interchain Token Service', () => { }); }); }); + +describe('Contract Size Check', function () { + const maxContractSize = 24 * 1024; // 24KB + + function checkContractSize(filePath) { + const stats = fs.statSync(filePath); + + if (stats.isFile()) { + const contractName = path.basename(filePath, '.json'); + + if (contractName.endsWith('.dbg')) { + return; + } + + const contractArtifact = JSON.parse(fs.readFileSync(filePath, 'utf-8')); + const bytecode = contractArtifact.bytecode.slice(2); + const contractSize = bytecode.length / 2; + expect(parseInt(contractSize), `${contractName} exceeds 24KB (Size: ${contractSize} bytes)`).to.be.at.most( + parseInt(maxContractSize), + ); + } else if (stats.isDirectory()) { + const files = fs.readdirSync(filePath); + files.forEach((file) => { + checkContractSize(path.join(filePath, file)); + }); + } + } + + it('should not exceed 24KB', () => { + const contractsDir = path.join(__dirname, '..', 'artifacts', 'contracts'); + checkContractSize(contractsDir); + }); +}); From 072380edddad30687bc6d752c60c2dbcda809707 Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Tue, 10 Oct 2023 19:11:38 +0530 Subject: [PATCH 5/9] chore: update hardhat config to only fail compilation if set in env, and update test.yaml --- .github/workflows/test.yaml | 3 +++ hardhat.config.js | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e8478135..e3a318d6 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -23,5 +23,8 @@ jobs: - name: Install Dependencies run: npm ci + - name: Set environment variable + run: export FAIL_COMPILE_IF_SIZE_EXCEEDS=true + - name: Test run: npm run test diff --git a/hardhat.config.js b/hardhat.config.js index 95ee3b74..f8f55da4 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -46,7 +46,7 @@ module.exports = { enabled: process.env.REPORT_GAS !== undefined, }, contractSizer: { - runOnCompile: process.env.CONTRACT_SIZER_ENABLED === 'true', - strict: true, + runOnCompile: true, + strict: process.env.FAIL_COMPILE_IF_SIZE_EXCEEDS, }, }; From dad3be232da450023124d06d0410590f17783e9e Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Tue, 10 Oct 2023 19:12:48 +0530 Subject: [PATCH 6/9] Revert "add: test to check contract size donot exceeds 24kb" This reverts commit 2001c7c183500919c688e126e30bdbe679848286. --- test/tokenService.js | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/test/tokenService.js b/test/tokenService.js index 5b21a056..93b0bca9 100644 --- a/test/tokenService.js +++ b/test/tokenService.js @@ -3,12 +3,10 @@ const chai = require('chai'); const { expect } = chai; require('dotenv').config(); -const fs = require('fs'); const { ethers } = require('hardhat'); const { AddressZero, MaxUint256 } = ethers.constants; const { defaultAbiCoder, solidityPack, keccak256 } = ethers.utils; const { Contract, Wallet } = ethers; -const path = require('path'); const TokenManager = require('../artifacts/contracts/token-manager/TokenManager.sol/TokenManager.json'); const Token = require('../artifacts/contracts/interfaces/IStandardizedToken.sol/IStandardizedToken.json'); @@ -1462,36 +1460,3 @@ describe('Interchain Token Service', () => { }); }); }); - -describe('Contract Size Check', function () { - const maxContractSize = 24 * 1024; // 24KB - - function checkContractSize(filePath) { - const stats = fs.statSync(filePath); - - if (stats.isFile()) { - const contractName = path.basename(filePath, '.json'); - - if (contractName.endsWith('.dbg')) { - return; - } - - const contractArtifact = JSON.parse(fs.readFileSync(filePath, 'utf-8')); - const bytecode = contractArtifact.bytecode.slice(2); - const contractSize = bytecode.length / 2; - expect(parseInt(contractSize), `${contractName} exceeds 24KB (Size: ${contractSize} bytes)`).to.be.at.most( - parseInt(maxContractSize), - ); - } else if (stats.isDirectory()) { - const files = fs.readdirSync(filePath); - files.forEach((file) => { - checkContractSize(path.join(filePath, file)); - }); - } - } - - it('should not exceed 24KB', () => { - const contractsDir = path.join(__dirname, '..', 'artifacts', 'contracts'); - checkContractSize(contractsDir); - }); -}); From da32bfc1fdd65c1d81cdff1a0a22050dbd758c6f Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Wed, 11 Oct 2023 12:15:01 +0530 Subject: [PATCH 7/9] add: env variable to run contractSizer optionally --- hardhat.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardhat.config.js b/hardhat.config.js index f8f55da4..f0ba47ea 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -46,7 +46,7 @@ module.exports = { enabled: process.env.REPORT_GAS !== undefined, }, contractSizer: { - runOnCompile: true, + runOnCompile: process.env.CONTRACT_SIZER_ENABLED, strict: process.env.FAIL_COMPILE_IF_SIZE_EXCEEDS, }, }; From da955ffdb578ce42bd9a8c4bc7483691b6860a3f Mon Sep 17 00:00:00 2001 From: blockchainguyy Date: Thu, 12 Oct 2023 11:50:51 +0530 Subject: [PATCH 8/9] chore: add CONTRACT_SIZER_ENABLED var in test worlflow file --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e3a318d6..cef7334b 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -24,7 +24,7 @@ jobs: run: npm ci - name: Set environment variable - run: export FAIL_COMPILE_IF_SIZE_EXCEEDS=true + run: export FAIL_COMPILE_IF_SIZE_EXCEEDS=true CONTRACT_SIZER_ENABLED=true - name: Test run: npm run test From ae0b0641330734147ff6008a7f6b6dcac1d5b636 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 12 Oct 2023 05:46:25 -0400 Subject: [PATCH 9/9] refactor to use a single env var --- .github/workflows/test.yaml | 2 +- hardhat.config.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index cef7334b..f2d8d0a8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -24,7 +24,7 @@ jobs: run: npm ci - name: Set environment variable - run: export FAIL_COMPILE_IF_SIZE_EXCEEDS=true CONTRACT_SIZER_ENABLED=true + run: export CHECK_CONTRACT_SIZE=true - name: Test run: npm run test diff --git a/hardhat.config.js b/hardhat.config.js index 3c420bb8..80120b46 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -47,7 +47,7 @@ module.exports = { enabled: process.env.REPORT_GAS !== undefined, }, contractSizer: { - runOnCompile: process.env.CONTRACT_SIZER_ENABLED, - strict: process.env.FAIL_COMPILE_IF_SIZE_EXCEEDS, + runOnCompile: process.env.CHECK_CONTRACT_SIZE, + strict: process.env.CHECK_CONTRACT_SIZE, }, };