[Ethereum] Ethereum gas limit issue while sending transaction from web3 client (private network)

blockchaingeth-debugginggo-ethereumweb3js

I have setup a private Ethereum system using geth. And am running a client that uses a web3.js file to connect to the Ethereum system. While trying to send a transaction or call some contract function, I get an error –

(node:500) UnhandledPromiseRejectionWarning: Error: Returned error: exceeds block gas limit

I have started the Ethereum nodes with a higher gas limit using targetgaslimit and also from the genesis file.

Here's how I started ethereum:

geth --datadir /root/.ethereum/geth/datastore/datadir --networkid 15 --rpc --rpcapi web3,eth,personal,miner,net,txpool --rpcaddr <ip> --targetgaslimit=9000000000000 --gasprice '1' console

Snippet from genesis.json:

difficulty" : "0xB",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",

As you can see, the gas limit is set to a higher value. Now, I have a smart contract deployed on the Ethereum network –

pragma solidity ^0.4.13;

contract Simple {
  uint public _num = 2;

  //contructor
  function Simple(uint _inNum) public {
        _num = _inNum;
  }

  function setNum(uint _num2set) public {
        _num = _num2set;
  }

  function getNum() public constant returns (uint) {
        return _num;
  }

  function getArea(uint _side) returns (uint) {
        return (_side * _side);
  }

  function arithmetics(uint _a, uint _b) returns (uint o_sum, uint o_product) {
    o_sum = _a + _b;
    o_product = _a * _b;
  }

  function multiply(uint _a, uint _b) returns (uint) {
    return _a * _b;
  }
}

Now, I am trying to run a web3 client to connect to the Ethereum node.

I have written some sample application JS code that connects to an Ethereum node –

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://172.17.0.8:8545"));

Now, I am trying to send a transaction or call one of the functions in the Ethereum smart contract by running the following command on the Node console-

simpleContract.methods.getNum().send({from: '0x41088ec8B25cc67c2558261B62FeD73f8B26ccc2', gasPrice: '0x1', gasLimit: '0x77359400'}).then(console.log)

But I always get exceeded gas limit error. From what I see, this transaction shouldn't exceed the gas limit at all. The gas limit while starting geth,as well as in the command are reasonable. when I check gas limit also, I get a reasonable number –

web3.eth.getBlock("latest").then(console.log)
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }
> { difficulty: '635410',
  extraData: '0xd88301080f846765746888676f312e31302e31856c696e7578',
  gasLimit: 8000000,
  gasUsed: 0,

Here's the complete error I see:

> simpleContract.methods.getNum().send({from: '0x41088ec8B25cc67c2558261B62FeD73f8B26ccc2', gasPrice: '0x1', gasLimit: '0x77359400'}).then(console.log)
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }
> (node:500) UnhandledPromiseRejectionWarning: Error: Returned error: exceeds block gas limit
    at Object.ErrorResponse (/node_modules/web3-core-helpers/src/errors.js:29:16)
    at /node_modules/web3-core-requestmanager/src/index.js:140:36
    at XMLHttpRequest.request.onreadystatechange (/node_modules/web3-providers-http/src/index.js:91:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
(node:500) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 62)

Any pointers on what the issue could be. I even tried explicitly setting gas and gas price: gas: '0x800000', gasPrice: '0x1', gasLimit: '0x77359400', but that did not work either. In fact, if I set gas to a lower value, I get an error:

(node:500) UnhandledPromiseRejectionWarning: Error: Returned error: insufficient funds for gas * price + value

Please suggest.

Thanks!

Best Answer

In your genesis file you have as gas limit 0x2fefd8. Your --targetgaslimit is 9000000000000 and and your method call has gasLimit: '0x77359400'

The point is: your targetgaslimit will not take effect immediatly. The block gas limit will grow towards this value slowly.

To check the gas limit of the last block, call eth.getBlock("latest").gasLimit

This is also the max. value you may pass to your function call, otherwise the transaction will be rejected.

Related Topic