Search code examples
solidityweb3jsgo-ethereum

Why won't this smart contract function work with a gas limit of 1 billion?


Using git, I got the ethereum source code and built it myself. And connected with Remix IDE's web3 provider environment. In the code below, the nums() function was executed well, but the add() function exceeded the gas limit.

A.sol


pragma solidity >=0.4.21 <0.6.0;

contract A {
    uint num;
    constructor(uint n) public {
        num = n;
    }

    function add(uint n) public {
        num += n;
    }

    function nums() public view returns (uint) {
        return num;
    }
}

B.sol


pragma solidity >=0.4.21 <0.6.0;

import "./A.sol";

contract B {
    A a;
    constructor(address addr) public {
        a = A(addr);
    }

    function add(uint n) public {
        a.add(n);
    }

    function nums() public view returns (uint) {
        return a.nums();
    }
}

The gas limit is set to 0x2fefd8000, and it is set to 1000000000 when running on Remix IDE. But there is no reason why the gas limit is over 800000. Do you know why?


Solution

  • Set your gas limit to 3000000; it should work fine then.

    The reason why your transactions were failing was because your gas limit was set to 1000000000.

    Transactions with a higher gas cost than what a single entire block allows for can't be executed, so such transactions will automatically fail.