5

I am new to Ethereum and tried everything to connect testnet from two local machine nodes.i tried with running two consoles in single machine by admin.addPeers(nodeUrl) and could connect. when i tried with the same for different machines using same network id it is showing peer count as zero. please help to solve this. thanks in advance

6 Answers 6

8

Since you're running two nodes on two different machines, you need to mention the IP address of the node while connecting through addPeer(enodeURI).

Detailed walkthrough:

  • You need to have the same genesis file in every machine you wish to start a node.

  • All the nodes should have the same --networkid

  • The --rpcport and --port should be different for every geth instance.

  • If working on the same machine, each geth instance should have different --datadir.

Starting geth instances:

Node 1:

geth --genesis path/to/genesis.json --datadir path/to/directory --networkid 1234 --port 30301 --rpcport 8101 console

Node 2:

geth --genesis path/to/genesis.json --datadir path/to/directory --networkid 1234 --port 30302 --rpcport 8102 console

You might add commands like --maxpeers and --nodiscover if you wish.

After starting these instances, check for a similar message on the console:

I0829 13:30:07.347738   3987 backend.go:303] Successfully wrote genesis block. New genesis hash = 82b6159155c00fb0b420046012a02257a176ad5dcfce4be4a15da39c166518e2

If this is the case, you successfully started the nodes with a private blockchain. If you see a message similar to Warning: Wrote default ethereum genesis block, then there's something wrong.

Connecting nodes:

Node 1: (on the JavaScript console)

admin.nodeInfo.enode

The output will be something like:

"enode://da97197a3335806658f17fdc167e943bd040fdf59ed882389d5f87b29f31362b12f13bd58438b7b5619497b54bab252a696cafc7cdd9696c4db1cacdb7e6a962@[::]:30301"

Observe that the port number in the end of URI is 30301.

Node 2:

Execute the same command on Node 2 and you'll get a similar output with 30302 in the end:

"enode://47e61e304d802fb98403fbf877e1018d13044630a16eb9c15c1d0fb139d465e02d995acd239768f6ced04579d9639d8a75c73c30d7443a9d6d6146a44c8b5e7b@[::]:30302"

To connect these nodes, find out the IP address of Node 2. Since this is a local network, each machine should have a unique private IP address. While using the addPeer(enodeURI) function, the [::] in the enodeURI should be replaced with the IP address of the machine on which the node is running. Now, on Node 1:

admin.addPeer("enode://47e61e304d802fb98403fbf877e1018d13044630a16eb9c15c1d0fb139d465e02d995acd239768f6ced04[email protected]:30302") 

After this command returns true, the net.peerCount should return 1 and admin.peers should return the details of the node which is added/connected. Before connecting, check if net.listening returns true on both nodes.

Sources:

Setting up private network or local cluster

Connecting to the network

How To Create A Private Ethereum Chain

2
  • --genesis is no longer valid Aug 5, 2017 at 3:55
  • @galahad, thank you for detailed answer. I know this is last year's answer and things have changed. Still I am asking you about the third step i.e. addPeer. Do we have to mention the ip the machine if we are trying to run on same machine? And does both nodes have to mine even if they are same network? Oct 7, 2017 at 16:32
2

Roland's answer is good but you should keep in mind that geth (and all ethereum as of yet) bind to the same default ports meaning that you have to change them from their defaults when running multiple instances on the same host (i.e. 127.0.0.1).

It's pretty easy to just increment the default ports for each instance and then create a (bash) script to run them independently (as background processes).

If you want more independence between the geth instances consider creating a separate data directory for each (modify the --datadir command line argument from the other answer):

--datadir "~/privethnet/geth_client"
--datadir "~/privethnet/geth_bootstrap"
3
  • bud he said two machines... Jul 12, 2016 at 15:43
  • Two local machines can also refer to virtual hosts. Perhaps he can clarify?
    – Sebi
    Jul 12, 2016 at 15:53
  • Thanks guys.. two local machines means two laptops lets say.. Jul 13, 2016 at 14:53
1

An example setup for a private network

Create a file named genesis.json with following content:

{
    "nonce": "0xdeadbeefdeadbeef",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {
    }
}

Run the following: geth --datadir ./ init genesis.json

geth --identity "nodeA" --rpc --rpcport "8000" --rpccorsdomain "*" --datadir "~/privethnet/" --port "30303" --ipcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --rpcapi "db,eth,net,web3" --autodag --networkid 1900 --nat "any" console


geth --identity "nodeB" --rpc --rpcport "8000" --rpccorsdomain "*" --datadir "~/privethnet/" --port "30303" --ipcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --rpcapi "db,eth,net,web3" --autodag --networkid 1900 --nat "any" console

Add the peer using the peer’s URL:
Find the peernode form the geth startup log 0712 13:34:00.865021 p2p/discover/udp.go:217] Listening, enode:...

admin.addPeer(nodeURL)

Check if peer node was added: admin.peers

Examples of nodeURLS are: Node A nodeURL:

enode://da97197a3335806658f17fdc167e943bd040fdf59ed882389d5f87b29f31362b12f13bd58438b7b56194[email protected]:30303

Node B nodeURL:

enode://9d4687d7cfa8c2215d2f2b4278a3e0ab4bde22ab838dd203b234866d69405b4b704fce4a71638f8c6601[email protected]:30303
1
  • Thank you .. i had tried this and i could get the peer count as well.. but when i do transaction and mining that will not get transferred.. lets say i have two accounts in first machine and i transfered money from acc1 to acc2 in first machine and run mining from second machine it will not get transfered .. i am not getting what is the mistake .. Jul 13, 2016 at 15:04
1

A user on Github named FleetingCloud has put together a nice command-style UI script for setting up Ethereum on a private network. I highly recommend it.

https://libraries.io/github/FleetingClouds/InitializeEthereumPrivateNetwork

Edit:

After cloning the repo, just run the initializeEthereumPrivateNetwork.sh script. It will then ask you what you would like to name your network, the directories of the geth on root and client nodes, etc.

1
  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – q9f
    Aug 26, 2016 at 12:55
0

--genesis is no longer valid. Need to use init instead. Here is my code I replaced with step 1:

geth --identity "MyTestNode" --nodiscover --networkid 1999 --port 30301 -- rpcport 8101 --datadir /home/appo/geth/  init /home/appo/.json console

You can set whatever flags and ports you want, this is just the code I ran on my machine.

0

Here's some more up to date scripts on connecting both execution clients and consensus clients in a local setup.

https://github.com/rzmahmood/ethereum-pos-testnet

It includes the bootnode discovery for both clients, and allows you to configure how many clients you want to run locally.

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.