Skip to content

Use Local Memory Type Variable Instead of Global Storage Type Variable in Event to Save Gas #40

@HighBe

Description

@HighBe

Hi, we recently have conducted a systematic study about Solidity event usage, evolution, and impact, and we are attempting to build a tool to improve the practice of Solidity event use based on our findings. We have tried our prototype tool on some of the most popular GitHub Solidity repositories, and for your repository, we find a potential optimization of gas consumption arisen from event use.

The point is that when we use emit operation to store the value of a certain variable, local memory type variable would be preferable to global storage type (state) variable if they hold the same value. The reason is that an extra SLOAD operation would be needed to access the variable if it is storage type, and the SLOAD operation costs 800 gas.

For your repository, we find that the following event use can be improved:

  • DarknodePayment.flat.sol
    function name:updateDarknodePayment
    event name:  LogDarknodePaymentUpdated
    variable:    darknodePayment->_darknodePayment
    function updateDarknodePayment(IDarknodePayment _darknodePayment) external onlyOwner {
        require(address(_darknodePayment) != address(0x0), "DarknodeRegistry: invalid Darknode Payment address");
        IDarknodePayment previousDarknodePayment = darknodePayment;
        darknodePayment = _darknodePayment;
        emit LogDarknodePaymentUpdated(previousDarknodePayment, darknodePayment);
    }

  function name:updateDarknodeRegistry
  event name:  LogDarknodeRegistryUpdated
  variable:    darknodeRegistry->_darknodeRegistry

    function updateDarknodeRegistry(DarknodeRegistry _darknodeRegistry) external onlyOwner {
        require(address(_darknodeRegistry) != address(0x0), "DarknodePayment: invalid Darknode Registry address");
        DarknodeRegistry previousDarknodeRegistry = darknodeRegistry;
        darknodeRegistry = _darknodeRegistry;
        emit LogDarknodeRegistryUpdated(previousDarknodeRegistry, darknodeRegistry);
    }

  function name:updatePayoutPercentage
  event name:  LogPayoutPercentChanged
  variable:    nextCyclePayoutPercent->_percent

    function updatePayoutPercentage(uint256 _percent) external onlyOwner validPercent(_percent) {
        uint256 oldPayoutPercent = nextCyclePayoutPercent;
        nextCyclePayoutPercent = _percent;
        emit LogPayoutPercentChanged(nextCyclePayoutPercent, oldPayoutPercent);
    }
  • DaiHard.sol
    function name:commit
    event name:  Committed
    variable:    responder->_responder
    function commit(address _responder, string calldata commPubkey)
    external
    inPhase(Phase.Open)
    /* any msg.sender */ {
        require(!autorecallAvailable(), "autorecallInterval has passed; this offer has expired.");

        responder = _responder;

        if (initiatorIsCustodian) {
            beneficiary = responder;
        }
        else {
            custodian = responder;
        }

        changePhase(Phase.Committed);
        emit Committed(responder, commPubkey);

        require(daiContract.transferFrom(msg.sender, address(this), getResponderDeposit()),
                                         "Can't transfer the required deposit from the DAI contract. Did you call approve first?"
                                         );
    }
  • RefundableCrowdsaleImpl.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient
    function transferPrimary(address recipient) public onlyPrimary {
        require(recipient != address(0));
        _primary = recipient;
        emit PrimaryTransferred(_primary);
    }
  • Escrow.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • RefundEscrow.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • RefundablePostDeliveryCrowdsaleImpl.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • SecondaryMock.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • RefundableCrowdsale.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • PullPaymentMock.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • PullPayment.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • ConditionalEscrow.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • SampleCrowdsale.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • ConditionalEscrowMock.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • Secondary.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

  • RefundablePostDeliveryCrowdsale.sol
    function name:transferPrimary
    event name:  PrimaryTransferred
    variable:    _primary->recipient

Do you find our results useful? Your reply and invaluable suggestions would be greatly appreciated, and are vital for improving our tool. Thanks a lot for your time!

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