How to deploy a local private Ethereum blockchain
This deployment guide covers the steps required to install your own private Ethereum blockchain in your local computer, for testing purposes. It also covers the deployment of a simple smart contract and the setup of a stats dashboard.
For deploying and running the Ethereum blockchain we will use go-ethereum, aka geth
, and for deploying the smart contract we will use Truffle.
Steps 1–7 are used to get the private network up and running. Steps 8–10 focus on testing the private network and step 11 covers the deployment of a stats dashboard.
- Install
geth
- Configure the Genesis block
- Create local nodes
- Generate initial accounts
- Initialize and start the nodes
- Connect to each node from the command line
- Interconnect the nodes
- Send some Ether
- Start a miner
- Deploy a Smart Contract
- Run Ethereum Network Stats dashboard
1. Install geth
geth
is the command line interface for running a full Ethereum node implemented in Go. To install it on a Mac, run the following commands:
brew tap ethereum/ethereum
brew install ethereum
For installing it on other OS, check the wiki.
2. Configure the Genesis block
Next we need to create a json file which will define how the first block of the blockchain will be created. Each node in the network must be initialized with the same genesis block.
Create a file called genesis.json
and save it in the parent folder of the nodes directories. Copy the content below but replace the addresses with those of the accounts created in the 3rd step.
The config
section ensures that certain protocol upgrades are immediately available. The alloc
section pre-funds accounts.
3. Create local nodes
For the purpose of this private blockchain, we will create some nodes locally in our machine.
Create 3 new directories. Each of them will represent a single node in the blockchain:
$ mkdir node01 node02 node03
4. Generate initial accounts
Let’s create 2 Ethereum accounts in the first node, so that later we can do some transactions for testing purposes:
$ geth --datadir "/PATH_TO_NODE01/" account new
You will be prompted for a secret password. Write down the address of the created accounts and memorize the password.
5. Initialize and start the nodes
The first step is to initialize each node with the genesis file previously created. For this, run this command for each of the nodes, replacing with the correct path:
$ geth --datadir "/PATH_TO_NODE/" init /PATH_TO/genesis.json
Then start each node by running the following command for each of them. You will need to use different numbers both for the port and the rpcport.
$ geth --identity "name_of_your_node" --rpc --rpcport "8000" --rpccorsdomain "*" --datadir "/PATH_TO_NODE/" --port "30303" --nodiscover --rpcapi "db,eth,net,web3,personal,miner,admin" --networkid 1900 --nat "any"
You can see a list and description of the geth command line options here.
6. Connect to each node from the command line
In order to interact with the nodes you need to open a terminal and use geth to attach to it. Run the following command for each of the nodes, in a separate terminal and use the correct port number for each of them:
$ geth attach http://127.0.0.1:8000
The console will enable you to use the Javascript API to interact with the node you have attached to.
7. Interconnect the nodes
The first node will be our admin node. Run the following command in the console to read some characteristics about it:
> admin.nodeInfo
Copy the enode address completely:
enode: "enode://26f7b83...ed769122f1692e@[::]:30303?discport=0"
Now you can connect the other nodes to this one by running the following command in their respective terminals:
> admin.addPeer("enode://26f7b8...92e@[::]:30303?discport=0")
You can verify that the nodes are peering by entering the following command in the terminal corresponding to the admin node:
> net.peerCount
2
8. Send some Ether
Next we will execute a transaction. Go to the console attached to the first node and run the following command to check that you can see the accounts created before:
> eth.accounts
Then check the balance of the first account:
> eth.getBalance(eth.accounts[0])
Now unlock the first account in order to start a transaction from it:
> personal.unlockAccount(eth.accounts[0])
Now send some Ether:
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:1000})
You will be asked to enter the password you used when creating the account. The transaction should be created successfully. The next step is to start a miner so that this transaction is actually processed and persisted in the blockchain.
9. Start a miner
The first thing to do is to set an account to receive the mining awards. This is called the etherbase account. We will use the admin node as a miner. Go to the console attached to it and run the following command:
> miner.setEtherbase(eth.accounts[0])
Then run the following command:
> miner.start()
You should see a null
message in the console, and then in the other console where you started the node you should see the progress of the mining that you just started.
You can now verify the balance of the account and check that the transfer was done properly.
10. Deploy a Smart Contract
In this section we will create and deploy a simple smart contract into our network. For this we will be using Truffle, which is a development framework for Ethereum.
Run the following command to install the Truffle library:
$ npm install -g truffle
Next we will initialize a new Truffle project by running this command in a new folder:
$ truffle init
You will notice that some initial files and directories were created. We need to modify the truffle-config.js
so that Truffle knows how to connect to our network. Use the following content for that file, considering that for the port number you need to use the port where your first node is running, and for the from
address you must complete the address of the first account created in that node:
Next, open the contracts
folder and create a new file called hello.sol
. Complete it with the following code, which is a simple contract written in the Solidity language:
The next step is to update the migrations file so that Truffle knows that it needs to compile and migrate the contract that we just created. Open the 1_initial_migration.js
file located in the migrations
folder and modify its content to match the following:
We can now use Truffle to compile our contract and deploy it into our network. Notice that Truffle will also compile and deploy its migrations
contract, which is used to track the deployed versions of our contracts and keep them updated.
Go to the contracts
folder and run the following command:
$ truffle compile
Next we need to unlock the account in the first node again, as we are going to deploy the contract using that account. Go to the terminal that is attached to that node and run this command:
> personal.unlockAccount(eth.accounts[0])
Finally, go back to the contracts
folder and run this command to deploy the contract:
$ truffle migrate
The contract should now be deployed in your network in a new address.
11. Run Ethereum Network Stats dashboard
The next thing is to set up the Ethereum Network Stats frontend to have a broad view of what’s happening in our network.
Clone the repository in a new folder and install the dependencies:
$ git clone https://github.com/cubedro/eth-netstats
$ cd eth-netstats
$ npm install
$ sudo npm install -g grunt-cli
Now you need to start the client and specify a secret:
$ WS_SECRET=s3cr3t npm start
Then you need to start each of the nodes again, but this time adding a parameter to specify the location of the stats application. Go to the console where you are running each of the nodes, stop the execution, add the following parameter (replacing with the right node number) and run it again:
--ethstats node01:s3cr3t@localhost:3000
Open the browser and navigate to localhost:3000. You should see the Stats explorer displaying a lot of information, including the client nodes:
Thanks for reading! Feel free to clap if you enjoyed it.