How to connect peers to a private network using geth

+1 vote

I am trying to setup a private network. I have created peers and now I want to connect these peers to the private
network. 

I have executed the following code :

geth --rpc --rpcaddr "0.0.0.0" --rpcport "8545" --rpccorsdomain "http://localhost:5000, http://localhost:6000" --port "2403" --ipcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --rpcapi "db,eth,net,web3" --networkid 1001201 --datadir "E:\User\priv\data" init "E:\User\priv\genesis.json"
geth --datadir "E:\User\priv\data"

After this, in the second terminal I execute:

geth attach ipc:\\.\pipe\geth.ipc

I get the same enode value on both the systems when I run admin.nodeInfo.enode
both of them return true.
But admin.peerCount returns 0.

What is the problem? How to solve it?

Jul 13, 2018 in Blockchain by digger
• 26,740 points
14,055 views

4 answers to this question.

+1 vote
Best answer

Follow the below steps to connect peers to the private network.

Step 1: Initialise a Genesis File.

geth --identity "YourNodeName" --rpc --rpcport "8092" --datadir "DirectoryPathToStoreData" --port "30330" --nodiscover --rpcapi "db,eth,net,web3" --networkid 2010 init PathOfGenesisFile

Note: All Peers that want to connect to a particular network should have the same Genesis File.

Step 2: Open a Geth JavaScript console.

geth --identity "YourNodeName" --rpc --rpcport "8092" --datadir "DirectoryPathToStoreData" --port "30330" --nodiscover --rpcapi "db,eth,net,web3" --networkid 2010 console

Step 3: To add peers to a system, run the following command on the system that you want to add the peers to.

admin.addPeer("Argument") 

where Argument is the enode value of another node which is obtained by typing admin.nodeInfo() in another system.

Eg:

admin.nodeInfo
{
  enode: "enode://965dc2b0f39e199d3a07871293015d8a4ad9349834fc27b7ee5e87bea89547e4fafda3ce7982ae07c14b8770c87a6c14e3fe2f91ef0d0ca717eb6ae56aa3e74b@[::]:30330?discport=0",
  //////////
}

where 30330 is the network id of another system.

Replace [::] with IP address of another system.

You can create a private blockchain without creating a Genesis File. For that, look at this:
https://medium.com/enpit-developer-blog/getting-started-with-ethereum-blockchain-and-smart-contract-development-a1ae41c0110

Hope this helps!

To find out more about it, it's recommended to go for the Blockchain online course.

Thanks.

answered Jul 13, 2018 by slayer
• 29,370 points

selected May 3, 2019 by Omkar
+1 vote

The command to add a peer to a private network using geth is as follows:

admin.addPeer("enode://<enode id>@10.10.23.196:30301");

Replace enode id with the ID that was displayed when the node was created. 

answered May 3, 2019 by John

Hi @John. I tried this command.

First I created the peer and noted the enode id and later used that enode id in the command as mentioned above. I got no errors. I want to check if the peer was added or not. How can I check it?

Hi @Osma. After adding the peer, run the below command:

> admin.peers

This will display all the peers added in that private network. If your peer was successfully added, then you should see it's enode id. 

Hey. Thanks, @John. When I added the peer and checked it, the peer was there. Then I quit geth and restarted it again. Then I checked for the peer. The peer disappeared. I don't know what I did wrong.
You didn't do anything wrong @Osma. That's how geth is programmed. When you add a peer, and exit geth, that data is cleared and that's why you cannot see the peer when you restart geth.
+1 vote

You can also make use of the static-nodes.json file. Find for this (you'll find it in your project directory). Open the file with a text editor and add the enode address to the file. The enode address in the static-nodes.json should be in the below format:

[
"<enode address of peer1>",
"<enode address of peer2>",
"<enode address of peern>"
]

This will add the node to the network

answered May 5, 2019 by Shiv
0 votes
answered Feb 6, 2020 by anonymous

Related Questions In Blockchain

0 votes
1 answer
+1 vote
1 answer
+1 vote
1 answer

How can I initiate a transaction on ethereum private network using mobile device?

There is no android wallet to connect ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,400 points
868 views
0 votes
0 answers

Error: Unexpected token o in JSON at position 1

I have been working with Interacting a ...READ MORE

Mar 6, 2019 in Blockchain by saeedi
• 120 points

edited Mar 6, 2019 by Omkar 2,397 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,882 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,399 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP