Part 2

Zaros
PerpetualsDEXFoundrySolidity
70,000 USDC
View results
Submission Details
Severity: low
Invalid

Redundant Import of Errors.sol: Unnecessary Gas Consumption During Compilation

Summary

The smart contract imports the same file, Errors.sol, twice, leading to redundant compilation overhead. While this does not directly impact runtime gas costs, it increases deployment costs and can cause unnecessary complexity in the contract codebase. Removing the duplicate import will optimize compilation efficiency.

Vulnerability Details

In the contract, Errors.sol is imported twice as shown below:

import { Errors } from "@zaros/utils/Errors.sol";
import { Errors } from "@zaros/utils/Errors.sol";

Solidity compiles each imported file, and redundant imports may lead to increased compilation time and higher deployment costs, especially in large-scale projects.

Root Cause

The contract unnecessarily includes the same import twice, which causes additional gas costs during contract deployment. Solidity compilers will process the file twice, even though it is not needed.\

Impact

  • Increased contract deployment cost due to unnecessary compilation.

  • Longer compilation time, affecting development efficiency.

  • Potential confusion for developers maintaining the contract.

Tools Used

  • Hardhat for compiling and deploying the contract.

  • Slither for static analysis to detect redundant imports.

  • Solidity Compiler to check bytecode size and gas impact.

Proof of Concept

To demonstrate the impact of redundant imports, I compare the compiled bytecode size and gas usage before and after removing the duplicate import.

Test Code (Hardhat)

Hardhat test to ensure that after removing the redundant import, the contract still functions correctly and that deployment costs are reduced.

const { ethers } = require("hardhat");
async function main() {
const ContractBeforeFix = await ethers.getContractFactory("ContractWithRedundantImport");
const contractBefore = await ContractBeforeFix.deploy();
await contractBefore.deployed();
console.log("Gas used before fix:", (await contractBefore.deployTransaction.wait()).gasUsed.toString());
const ContractAfterFix = await ethers.getContractFactory("ContractWithoutRedundantImport");
const contractAfter = await ContractAfterFix.deploy();
await contractAfter.deployed();
console.log("Gas used after fix:", (await contractAfter.deployTransaction.wait()).gasUsed.toString());
}
main().catch((error) => {
console.error(error);
process.exit(1);
});

The test compare gas usage before and after removing the redundant import.

Mitigation

Remove the duplicate import from the contract. The corrected version should look like this:

import { Errors } from "@zaros/utils/Errors.sol";

This ensures optimal compilation efficiency and reduces deployment costs.

Updates

Lead Judging Commences

inallhonesty Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.