A Journey into Blockchain

Private Network with Ethereum

  1. Build the node directories locally
  2. Create accounts
  3. Write the “genesis block”
  4. Start gethon each node
  5. Connect the nodes
  6. Exchange some ether
  7. Start the miner

Preface

This post is designed to get you up and running with blockchain quickly while exposing you to some of the underlying tech. There are easier ways to create a blockchain private network but, they often obscure a lot of the implementation and build work. Ethereum is one of the more popular implementations of blockchain. Therefore, there are quite a bit of resources and development tools to support the Ethereum development community.

  • Blockchain is designed to be decentralized however, it does not have to be. An entire blockchain can exist on a single machine
  • Ethereum is just one implementation of blockchain; there are several others

Step 1, Installing geth

There are several client tool implementations available for the Ethereum network. In fact, if you go to the Ethereum website you’ll find a whole list of them, go-ethereum being the most popular. Geth is the tool you will use to interface with an instance running the Ethereum blockchain allowing you to interact with the Ethereum network.

brew update
brew upgrade
brew tap ethereum/ethereum
brew install ethereum

Step 2, Node Directory

The real public Ethereum network is made up of thousands of nodes all over the world creating a decentralized network. In a private network, each node is explicitly defined and only selected entities are permitted to interact with the blockchain. There are many different applications and uses for public vs private network some of which are described here. For my purpose I chose to start small and create all my nodes locally on one machine.

$ mkdir node01 node02 node03

Step 3, Create Ethereum Accounts

I want to start off my private network with some preloaded Ethereum accounts. Before I can preload the accounts, I must have the accounts created already. Select node02 as the node to create preloaded accounts. Then use the following command to add an 3 accounts to node02:

$ geth --datadir "/path_to_your_node02_directory/" account new
Create an account on node02.

Step 4, Write the Genesis File

The genesis file is a simple JSON files used to describe the very first block on the blockchain. Each node in your network must be initialized using the same genesis block.

Genesis file used to initialize the private blockchain
"alloc": {
"[YOUR ACCOUNT ADDRESS]": { "balance": "[PREALLOCATED AMOUNT]" }

Step 5, Start the Nodes

Before we can start the nodes you must initialize each node using the genesis file created in the previous step.

$ geth --datadir "/path_to_your/node_directory/" init /path_to/custom_genesis_file.json
$ geth --identity "name_of_your_node" --rpc --rpcport "8000" --rpccorsdomain "*" --datadir "/path_to_your/node_directory/" --port "30303" --nodiscover --rpcapi "db,eth,net,web3,personal,miner,admin" --networkid 1900 --nat "any"
Starting all the nodes using geth.
  • --rpc: Enable the HTTP-RPC server
  • --rpcport: HTTP-RPC server listening port (default: 8545). Each node will need to have a different value
  • --datadir: Data directory for the databases and keystore. This should be different for each node
  • --port: Network listening port (default: 30303). Each node will need to have a different value
  • --nodiscover: Disables the peer discovery mechanism (manual peer addition). Without this option enabled, the node will always be looking for other peers to connect to
  • --rpcapi: API’s offered over the HTTP-RPC interface. Not all those shown in the example are needed. In fact, you should be cautious of what APIs you make available here
  • --networkid: Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby) (default: 1). Each node in the network must use the same value
  • --nat: NAT port mapping mechanism (any|none|upnp|pmp|extip:<IP>) (default: “any”)
geth attach [HTTP ENDPOINT OR IPC ENDPOINT]

Step 6, Connecting the Nodes

After starting each node, they can now be connected to the admin node. node01 will serve as the admin node. Open up a console for that node using geth attachand enter admin.nodeInfo. This will output several characteristics about the node. Copy the enode address beginning with the open quote ".

enode: "enode://26f7b8f0e11839cf6f3b560ee2d2d88f642cf5e0da8aa665c53319f3ca52ed316392cd61b9ca0505348eed769122f4c550107e1aefafdee316dfc843c5f9692e@[::]:30303?discport=0",
admin.addPeer("enode://26f7b8f0e11839cf6f3b560ee2d2d88f642cf5e0da8aa665c53319f3ca52ed316392cd61b9ca0505348eed769122f4c550107e1aefafdee316dfc843c5f9692e@[::]:30303?discport=0")
> net.peerCount
2

Step 7, Exchange Some Ether

Open up a geth console in node02 and enter the command eth.accounts. You should see a list containing the 3 accounts you created in Step 3.

> eth.getBalance(eth.accounts[0])
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:100000})
> personal.unlockAccount(eth.accounts[0])

Step 8, Start a Miner

First thing you must do is set the etherbase for the node that you are working in. The etherbase account is the account designated to receive the rewards earned from mining the blockchain. Open a geth console on node02and run the command miner.setEtherbase(eth.accounts[0]).

A depiction of the private network, each node containing a copy of the blockchain, with node02 running the miner.
Starting the miner on the second node.

Conclusion

When I first dove into blockchain I found it challenging to identify what I should focus on first (i.e. blockchain admin, cryptocurrencies, smart contracts, etc.). Since it’s all rooted from the same underlying technology I thought I would start with creating a simple private blockchain. This way I could observe the basic operations that occur on a blockchain and begin to understand how the blockchain itself works. It’s important to note that a private blockchain essentially removes one of the most enticing pieces of this technology, decentralization. Although it does have other viable applications, I found private blockchain was a great way to explore the technology.

  • Can you transfer ether from an account on one node to an account on another node?
  • What happens if you transfer ether and no miner is running?

I work in the DC area as a software engineering consultant. I spend most of my time working on fullstack web applications and the big data space.