Skip to content

Missing state updates or optimizable state variables #975

@mingbaile

Description

@mingbaile

Hi, we are a research group on programming languages and software engineering. We recently conducted a systematic study on the causes, effects, and fixes of the inconsistent state update vulnerability in solidity. We are attempting to build a tool to detect bugs about state updates based on our findings. We have tried our prototype tool on some popular Github solidity repositories, and for your repository, we found that there are missing state updates or gas consumption that can be optimized.

The point is that when we declare a state variable in the contract, if the variable is not reassigned throughout the project, it may be a missed state update, including balance, order number, counter, contract status flag, etc. Of course, it may also be a state variable with special purposes that does not need to be changed, such as maximum supply, contract administrator address, configuration information, etc. When declaring these state variables that do not need to be changed, the constant or immutable modifier should be used as required, which will save gas.

For your repository, we found the following state variables that may need attention. Would you need to update them in the future? Or might it be more suitable to declare them with the constant or immutable modifier? :

DenialFactory.sol
State variable: initialDeposit

uint256 public initialDeposit = 0.001 ether;

Gate.sol
State variable: number1, number2

uint8 private number1 = 10;
uint16 private number2 = 255;

SpaceBank.sol
State variable: gasLimit0, gasLimit1

uint256 internal gasLimit0 = 9999999999999999999999999; //@TODO calculate these gas limit values to be as small as possible

uint256 internal gasLimit1 = 9999999999999999999999999;

Do you find our result useful? We sincerely appreciate your feedback, as it is crucial for improving our tool. Thank you so much for taking the time!
(The reason constant and immutable modifiers save gas is that their values are directly compiled into the contract’s bytecode, eliminating the need for EVM storage. This not only removes storage costs but also avoids the SLOAD operation required for reading stored values. The main difference between constant and immutable variables is that the value of immutable variables can be set in the constructor, and immutable variables may cost more gas than constant variables. In addition, there is a slight difference in the variable types they support. The official documentation describes more details: https://docs.soliditylang.org/en/latest/contracts.html)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions