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.
required
conditions are not met.private_blockchain
storage variable? Is it possible, that this variable has zero value? You didn't providefrom
parameter forestimateGas
call, somsg.sender
is most probably zero, and thus the firstrequire
may fail.from
(the address from which contract was deployed in truffle-config file. This is the address of the account in ./exp3 folder) .require
commented out. It's still the same error. The geth client account is unlocked as well.