recently I am learning how to deploy and call smart contract on ethereum. I am working on a very simple contract as follows:
pragma solidity >=0.4.24 <0.6.0;
contract TestContract{
uint public a;
constructor() public
{
a = 2;
}
function addNumber(uint b) public
{
a = a + b;
}
function read() public view returns (uint)
{
return a;
}
}
I firstly tried to deploy the contract on my private chain, with the geth compilied from current master default go-ethereum source code. Though the contract can be successfully deployed, calling function addNumber
through web3 throws the following errors gas required exceeds allowance (1726103387) or always failing transaction
。I have no idea why it is the case, because my private chain is on mining, and my account has enough ether given so small contract code.
Then I deployed the contract on an old version of go-ethereum, both deployment and calling function addNumber
seemed fine in the first place, the web3 calling didn't return any error, but then I found function addNumber
actually did't increase (b = 1
in my test case), a
reamined to 0 despite many times of calling. I also checked the corresponding transactions in geth and learn they were already exits in a mined block.
Then I changed to compile the contract code using solidity ^0.4.0 (also remove constructor and keyword view as solidity ^0.4.0 does not support them), and deployed on chain. In this case, the contract works fine for both version of go-ethereum. namely, a
is increased whenever calling function addNumber
with b
= 1
So my question is:
- why calling
addNumber
in a recent version of go-ethereum throws error: gas required exceeds allowance (1726103387) or always failing transaction. - why the same contract behaves totally different in different version of go-ethereum and solidity.
- In my opinion, the smart contract is 'too smart'. Is there any idea to choose between different version of go-ethereum and solidity?
var hash = contract.addNumber.sendTransaction(1, {from: web3.eth.accounts[1]})
tovar hash = contract.addNumber.sendTransaction(1, {from: web3.eth.accounts[1], gas: 30000000})