Code
- geth version : v1.4.18-stable/linux/go1.5.1
- web3 version : 0.17.0-alpha
Here is the script I launch in a docker container to run geth :
#! /bin/bash
genesis_json_file=development/genesis_config/genesis_block.json
default_password=development/genesis_config/default_password # contain the password "toto"
datadir=data
rm -rf $datadir/*
geth --datadir $datadir init $genesis_json_file
geth --password $default_password --datadir $datadir account new
geth --datadir $datadir --mine --minerthreads 1 --rpc --rpcaddr "0.0.0.0" --rpcapi "eth,web3,personal"
Now, I am able to reach the geth API from my host system :
$ curl http://0.0.0.0:8545
> {"jsonrpc":"2.0","error":{"code":-32600,"message":"EOF"}}
Sometimes issues come when I try to connect to geth using web3. Here is my js script :
function connect(host, port) {
uri = 'http://' + host + ':' + port;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
}
else {
/* set the provider you want from Web3.providers */
process.stdout.write('Setting Http provider : ' + uri + "\n")
var prov = new Web3.providers.HttpProvider(uri);
web3 = new Web3(prov);
}
if(!web3.isConnected()) {
process.stdout.write('Cannot reach ' + uri + "\n");
return false;
}
else {
process.stdout.write('Connected to ' + uri + "\n");
return true;
}
}
function compile_contract(file_path) {
# No problem here
# This return the compiled contract
...
}
function send_contract_handler(e, contract) {
if (e){
console.log('send contract ERR : ' + e);
}
else if (typeof contract.address === 'undefined') {
console.log("Contract transaction send. Hash: " + contract.transactionHash);
console.log('Waiting for mining process');
}
else{
console.log('Contract mined! address: ' + contract.address);
}
}
function send_contract(from_addr, contract) {
/* Unlock account */
web3.eth.defaultAccount = from_addr;
var ret = web3.personal.unlockAccount(from_addr, 'toto');
/* Get account balance */
var balance = web3.eth.getBalance(from_addr);
/* Ask for a gas price estimation to send the contract */
var estimation = web3.eth.estimateGas({
from: from_addr,
data: contract.bytecode
});
/* Check if the sender account has enough gas */
if (estimation > parseInt(balance.toString(10)))
console.log('estimation to high : not enought gas');
/* Send the contract creation transaction to the block chain */
return web3.eth.contract(JSON.parse(contract.interface)).new(
{
from: from_addr,
data: contract.bytecode,
gas: estimation
},
send_contract_handler
);
}
if (require.main === module) {
/* Connect to a running node */
while (!connect('0.0.0.0', 8545));
/* Compile contract */
var output = compile_contract(contractPath);
if (!output)
return 2;
/* Send contract using the coinbase addr */
return send_contract(web3.eth.coinbase, output);
}
Issues
When I run this script I have 3 different possible scenarios for the same code:
All works perfectly fine. Script output:
Setting Http provider : http://0.0.0.0:8545 Connected to http://0.0.0.0:8545 ./contract/simple.sol will be compiled from_addr : 0x5b481d0f036af15ec3025fed428cdd49b45b4a3e Contract transaction send. Hash: 0x95cf8c756860605cefd5ef89fe673fa3a267dfb66766cf4a0d38b94194053935 Waiting for mining process Contract mined! address: 0x4af7d1639f590dfc9e5594a3ba0bb9ead17a14bc
I am not able to be connected to the node: web3.isConnected() always return false. Script output:
Cannot reach http://0.0.0.0:8545 Cannot reach http://0.0.0.0:8545 ... Cannot reach http://0.0.0.0:8545
The connection failed on this line :
var balance = web3.eth.getBalance(from_addr);
Script output:
Setting Http provider : http://0.0.0.0:8545 Connected to http://0.0.0.0:8545 ./contract/simple.sol will be compiled from_addr : 0x5b481d0f036af15ec3025fed428cdd49b45b4a3e /home/xxx/node_modules/solc/soljson.js:1 (function (exports, require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath["normalize"](filename);var ret=nodeFS["readFileSync"](fi Error: CONNECTION ERROR: Couldn't connect to node http://0.0.0.0:8545. at Object.InvalidConnection (/home/xxx/node_modules/web3/lib/web3/errors.js:28:16) at HttpProvider.send (/home/xxx/node_modules/web3/lib/web3/httpprovider.js:75:22) at RequestManager.send (/home/xxx/node_modules/web3/lib/web3/requestmanager.js:58:32) at Eth.send [as getBalance] (/home/xxx/node_modules/web3/lib/web3/method.js:145:58) at send_contract (/home/xxx/myscript.js:76:26) at Object.<anonymous> (/home/xxx/myscript.js:108:10) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12)
Hints
- Scenario 1 is the most common scenario. This usually happen when I just restart my computer
- Scenario 2 - 3 usually appears after some successful try
- Even in scenario 2 - 3 I am able to reach the get API with curl or telnet
This seems to be a network issue or a bad state in geth...
Sorry for this long and imprecise question. If someone have an idea why this happen, or a good method to debug it, he will be welcome!
127.0.0.1
orlocalhost
instead of0.0.0.0
.