0

I have recently started using Ethereum. I am trying to test my contract using multiple machines. Only solution I found was to setup a private network using Geth and add peers. Following is the genesis block for my network.

{
"config": {
"chainId": 16,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc": {},
"difficulty" : "200"
"gasLimit" : "99999999999999"
}

I created nodes/accounts on both the machines. Have been able to add peers as well as send ether and do dummy mining etc. on both the nodes. I use the following command to start the network.

geth --datadir ./exp3 --networkid 16 --rpc --rpcapi "eth,net,web3,personal,miner" --nodiscover --allow-insecure-unlock --port 3000 console

My project was in Truffle Suite so I changed my truffle-config as following:

rpc: {
host: "localhost",
port: 8545},
networks: {
development: {
  host: "127.0.0.1",     // Localhost (default: none)
  port: 8545,            // Standard Ethereum port (default: none)
  network_id: "16",       // Any network (default: none)
  from: "0xf4ffdeedd1f02171e554e4ef40fb42a9f087166f",
  gas: 20000000,
  websockets: true}

All of this runs well, the contract is depoyed. Now, when I run a NodeJS script to even estimate the gas it shows the error: "gas required exceeds allowance (93846700840810) or always failing transaction". My nodejs script is:

var Web3 = require('web3');
var web3_http = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
const fs = require('fs');
const con = JSON.parse(fs.readFileSync('./build/contracts/DecentralisedBroker.json', 'utf8'));
var abi = con.abi;
var cont = new web3_http.eth.Contract(abi, "0x85256Ccc77DD205dabE906bF63c6128c311E7881");
console.log(cont.options.address);
cont.methods.registerUser('XYZ').estimateGas()
.then(function(gasAmount){
console.log(gasAmount);
}).catch(function(error){
console.log(error);});

I don't understand why this problem occurs. The gas limit for each block in genesis.json is pretty high, which I don't think is crossed at all during this call. Also my geth client account has sufficient dummy ether. It may be the case that call is not made from my geth client address but even if I specify the address in the call, it shows the same thing. Is there a way to make calls via the geth client(using nodejs) in ./exp3 folder where the blockchain is instantiated in my system. Following is a snapshot of the function in contract.

struct User{
    string user_name;
    uint8 flag;
    Specification[] requests;
  }
mapping(uint => uint) indexOfPendingRequests;
  mapping(address => User) users;
  address private_blockchain;
  uint total_requests;
constructor(address _blockchain) public {
    private_blockchain = _blockchain;
    total_requests = 0;
}
function registerUser(string memory name) public{
    require(msg.sender != private_blockchain, 'Permissioned blockchain can not be a user');
    require(bytes(name).length > 0, 'Name should be > 0.');
    if(users[msg.sender].flag != 1){
      users[msg.sender].user_name = name;
      users[msg.sender].flag = 1;
    }
    return;
  }

Can you please specify what may be the problem. Also, is there any other efficient way to test contracts on multiple machines (submitting transactions and listening to events, etc.). Thank you.

6
  • Could you show other relevant parts of your contract, namely how storage variables are defined? Probably you function fails because some of required conditions are not met. Oct 28, 2019 at 10:54
  • I have added the struct as well as the constructor of the contract. Is the fault due to some error on contract or can it be due to some other issue while initiating the network or accessing the contract on the network? Oct 28, 2019 at 11:03
  • What is the value of private_blockchain storage variable? Is it possible, that this variable has zero value? You didn't provide from parameter for estimateGas call, so msg.sender is most probably zero, and thus the first require may fail. Oct 28, 2019 at 12:15
  • No, I have deployed the contract with a value for private_blockchain. It shows the same error even if I mention an address in from (the address from which contract was deployed in truffle-config file. This is the address of the account in ./exp3 folder) . Oct 28, 2019 at 16:48
  • Have also tried another deployement with the first require commented out. It's still the same error. The geth client account is unlocked as well. Oct 28, 2019 at 18:34

1 Answer 1

0

When estimating the gas it internally runs the whole function (but obviously doesn't modify the blockchain). Looking at your function I can see it uses msg.sender, but who's message.sender when you call estimateGas in your Javascript code? It's not defined, so on the very first line it doesn't pass the require statement.

You can fix it this way:

cont.methods.registerUser('XYZ').estimateGas({ from: "0x.............." });

See the documentation for references.

Also, you might be able to use defaultAccount:

web3.eth.defaultAccount = "0x................";

Yet it's something I personally do not recommend.

4
  • Have tried it by specifying an account as well, but it doesn't work. Won't the gas/ether be cut from the account? I have ether in the mentioned account but it still doesn't run the function. Oct 29, 2019 at 7:46
  • I have tried re-deploying it using truffle migrate --reset after removing both require statements. It still shows the same error. I have checked whether my script uses the correct account by web3.eth.getBalance method and it returns the actual balance. Can there be any other issue with deployment or contract itself? Oct 29, 2019 at 8:51
  • Maybe you are using the same account as in the private_blockchain variable? Oct 29, 2019 at 8:54
  • No, both the accounts are different. Moreover, in the latest deployment I have removed both the require statements and it's still showing gas limit exceeded. When running eth.getBlock("latest").gasLimit, it returns 82009175511223 which is pretty sufficient is assume and eth.getBalance returns 879950000000000000000. Oct 29, 2019 at 9:03

Your Answer

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

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