0

I am compiling following code using Remix IDE.

contract Ballot {

    struct Voter {
        uint weight;
        bool voted;
        uint8 vote;
        address delegate;
    }
    struct Proposal {
        uint voteCount;
    }

    address chairperson;
    mapping(address => Voter) voters;
    Proposal[] proposals;

    /// Create a new ballot with $(_numProposals) different proposals.
    constructor(uint8 _numProposals) public {
        chairperson = msg.sender;
        voters[chairperson].weight = 1;
        proposals.length = _numProposals;
    }

    /// Give $(toVoter) the right to vote on this ballot.
    /// May only be called by $(chairperson).
    function giveRightToVote(address toVoter) public {
        if (msg.sender != chairperson || voters[toVoter].voted) return;
        voters[toVoter].weight = 1;
    }

    /// Delegate your vote to the voter $(to).
    function delegate(address to) public {
        Voter storage sender = voters[msg.sender]; // assigns reference
        if (sender.voted) return;
        while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
            to = voters[to].delegate;
        if (to == msg.sender) return;
        sender.voted = true;
        sender.delegate = to;
        Voter storage delegateTo = voters[to];
        if (delegateTo.voted)
            proposals[delegateTo.vote].voteCount += sender.weight;
        else
            delegateTo.weight += sender.weight;
    }

    /// Give a single vote to proposal $(toProposal).
    function vote(uint8 toProposal) public {
        Voter storage sender = voters[msg.sender];
        if (sender.voted || toProposal >= proposals.length) return;
        sender.voted = true;
        sender.vote = toProposal;
        proposals[toProposal].voteCount += sender.weight;
    }

    function winningProposal() public constant returns (uint8 _winningProposal) {
        uint256 winningVoteCount = 0;
        for (uint8 prop = 0; prop < proposals.length; prop++)
            if (proposals[prop].voteCount > winningVoteCount) {
                winningVoteCount = proposals[prop].voteCount;
                _winningProposal = prop;
            }
    }
}

First, I compiled the code with version 0.4.19 and then compiled same code by changing version to 0.4.25. The bytecode size is greater for version 0.4.25.

I was working with a smart contract in my system, I changed the version of all the smart contract to 0.4.25 from 0.4.19. Because 1 smart contract is not getting deployed as the size is getting increased.

1
  • 1
    New version of the compiler fix old bugs, and the fix might increase the generated code. You should always use the more recent version if possible, or else check if your code may be affected by bugs fixed in new releases.
    – Ismael
    Commented Nov 18, 2018 at 16:21

1 Answer 1

1

The binary objects generated with Solidity compiler version 0.4.22, or later, pack more information due to multiple changes in the code generator, according to the release notes [1]. However, the final optimized bytecode, generated by the more recent compiler version, is expected to be smaller after improvements in the optimizer, most notably removing unnecessary operations (advancements done after version 0.4.19).

Consider the following compile.js JavaScript example code: var input = <Ballot source code here>; var solc = require("solc");
var regularOutput = solc.compile(input, 0); var optimizedOutput = solc.compile(input, 1); console.log("Regular output: " + regularOutput.contracts[":Ballot"].bytecode.length); console.log("Optimized output: " + optimizedOutput.contracts[":Ballot"].bytecode.length);

Given the provided Ballot contract as input [2] and the previously defined compile.js code, run node compile.js using [email protected] and [email protected] JavaScript packages respectively:

Running yarn add [email protected] && node compile.js results in: Regular output: 4272 Optimized output: 2382

Running yarn add [email protected] && node compile.js results in: Regular output: 4344 Optimized output: 2318

With that example at least, it is possible to state the Solidity compiler version 0.4.25 did get an improvement at generating optimized bytecode due to changes in optimizer, even though the non-optimized builds now pack more information.

[1] Solidity compiler - Version 0.4.22 - https://github.com/ethereum/solidity/releases/tag/v0.4.22. [2] The constructor() in the original code was replaced with function Ballot in order to work in version 0.4.19.

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.