How to set up a private Ethereum blockchain and deploy a Solidity Smart Contract on the blockchain — in less than 20 mins!
From Zero to One: A step-by-step tutorial on setting up a private Ethereum blockchain and deploying Solidity smart contracts
In the following tutorial we will,
- Install the requisite software packages to run a private Ethereum blockchain on a Mac.
- Run a private blockchain in a test environment, and “mine” dummy “ether” and “gas” ( to run smart contracts).
- Create a “Hello World” smart contract in Solidity, compile it using Truffle and deploy it to the private Ethereum network.
- Run the “hello world” smart contract within the truffle console.
And we will do all of this within 15 mins or less, so let’s begin!
Step 0: Pre-installation
Before you begin installation make sure you have latest versions of Homebrew and Node/npm.
To install Homebrew:
- go to https://brew.sh/
- Cut-paste the latest homebrew cURL within your terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once homebrew installation is complete, verify you have the latest version.
$ brew --version
To install Node and npm:
- Go to https://nodejs.org/en/ and install Node and NPM
Once node and npm are install verify the version number.
$ npm -v
Step 1: Installing Ethereum, Solidify and Truffle
Next we will install the following,
- Geth (go-etheruem): This is command line client interface that allows one to connect to the Ethereum blockchain.
(The other clients that can be used to connect to Ethereum are Eth (C++ implementation), and PyEthApp(Python implementation).
All the clients have almost identical functionality and Geth is the most suited if you are familiar with javascript/web development language). - Truffle: This is the Solidity Framework and IDE, which is used to compile, deploy and test your Smart contract.
- Solidity compiler: The Truffle framework will use the Solidity compiler.
Let’s continue,
- Installing Geth
$ brew tap ethereum/ethereum
$ brew install ethereum
2. Installing Truffle
$ sudo npm install -g truffle
3. Installing Solidity
$ sudo npm install -g solc
Step 2: Setup and configuration
This step will include the following,
- Configuring the genesis block, and
- Initiating the chain data in the blockchain
Create a new project directory, and create the following genesis.json
$ mkdir project1
$ cd project1
$ touch genesis.json
1.Configuring the Genesis Block
You can use Visual Studio code or any other editor to cut-paste the following code in the genesis.json file.
{"config": {"chainId": 143,"homesteadBlock": 0,"eip155Block": 0,"eip158Block": 0},"alloc": {},"difficulty" : "0x20000","gasLimit" : "0x8880000"}
Explanation of the config file:
- chainId: A unique identifier of the new private blockchain
- homesteadBlock: Homestead is the first production release of Ethereum and since the developers are already using this version the value of this parameter can be left as ‘0’.
- eip155Block/eip158Block: EIP stands for “Ethereum Improvement Proposals”, these were implement to release Homestead. In a private blockchain development hard forks aren’t needed, hence the parameter value should be left as ‘0’.
- difficulty: controls the complexity of the mining puzzle and a lower value enables quicker mining.
- gasLimit: Establishes an upper limit for executing smart contracts.
- alloc: allows allocation of Ethers to a specific address.
2. Creating the folder where your blockchain will reside
$ mkdir blkchain
$ geth --datadir blkchain init genesis.json
The second line initializes the blockchain and the blockchain data will be stored in the “blkchain” folder. This folder will grow in size as data is added to the blockchain, and if this folder is deleted the block chain will need to be reinitialized.
Step 3: Initiate and Run the Private Ethereum Blockchain
- Bring up the Private Ethereum blockchain!
To get the private Ethereum blockchain up and running, open up a new command terminal window and run the following in the “projects” folder.
$ geth --port 3000 --networkid 58343 --nodiscover --datadir=./blkchain --maxpeers=0 --rpc --rpcport 8543 --rpcaddr 127.0.0.1 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,miner"
Explanation of the command line above:
— networkid: identity of your Ethereum network for other peers to discover. You can use any random number of your choice(except the ones listed below) to create your own network and to prevent others from inadvertently connecting to your network.
The following network id’s are reserved for specific Ethereum networks.
0
: Olympic, Ethereum public pre-release testnet1
: Frontier, Homestead, Metropolis, the Ethereum public main network1
: Classic, the (un)forked public Ethereum Classic main network, chain ID61
1
: Expanse, an alternative Ethereum implementation, chain ID2
2
: Morden, the public Ethereum testnet, now Ethereum Classic testnet3
: Ropsten, the public cross-client Ethereum testnet4
: Rinkeby, the public Geth PoA testnet8
: Ubiq, the public Gubiq main network with flux difficulty chain ID8
42
: Kovan, the public Parity PoA testnet77
: Sokol, the public POA Network testnet99
: Core, the public POA Network main network7762959
: Musicoin, the music blockchain61717561
: Aquachain, ASIC resistant chain
— nodiscover: Disables the peer discovery mechanism (manual peer addition).
— datadir: indicates the data directory where your blockchain will reside.
— maxpeers: Maximum number of network peers (network disabled if set to 0) (default: 25)
— rpc: Enable the HTTP-RPC server
— rpcapi: this allows us to communicate with the Ethereum network using the web3js RPC methods in the Geth Javascript console.
2. Connect to the private Ethereum blockchain using the Geth Javascript console.
Open a new command terminal and navigate to the “projects” folder. Run the following to launch the Geth Javascript Console,
$ geth attach http://127.0.0.1:8543
This will open the Geth Javascript console, that is connected to the private Ethereum blockchain network running on localhost:8543
3. Create an account and “mine” for dummy Ether.
The Geth Javascript console, provides web3js api’s to create a new account. Use the following Geth commands to create a new account and unlock it.
> personal.newAccount('seed')
> personal.unlockAccount(web3.eth.coinbase, "seed", 15000)
You can now start mining dummy “ether” using miner.start()
> miner.start()
In your “Geth network terminal window”, you will now see “ether” being mined.
To check your “ether” balance, use the following command in the Geth Console.
web3.fromWei(eth.getBalance(eth.coinbase), "ether")
And to stop mining use,
miner.stop()
Step 4: Create a “Hello World” Smart Contract in Solidity, and deploy it to the private Ethereum Blockchain
1.Initialize Truffle
Create a new folder in the “projects” directory to initialize truffle
$ mkdir truffle
$ cd truffle$ truffle init //this will initialize truffle and create the necessary files and folders in the directory
2. Create a Smart Contract.
Go to the “contracts” folder in the “truffle” directory, and create a new file called “Hello.sol”. You can create this file in Visual Studio, and cut paste the following code.
pragma solidity ^0.4.15;contract Hello { string public message; function Hello() { message = "Hello, World : This is a Solidity Smart Contract on the Private Ethereum Blockchain "; }}
This is the smart contract written in Solidity. Notice that the Solidity smart contract code is very similiar in form to Javascript.
3. Configure the Truffle migrations folder to deploy the Solidity code.
Go to the “migrations” folder in the “truffle” folder, and create a new file called 2_deploy_contracts.js, with the following code.
var Hello = artifacts.require("./Hello.sol");module.exports = function(deployer) { deployer.deploy(Hello);
};
Notice that the above code is Nodejs.
4. Update the truffle task runner with your account and network details. Update “truffle.js” with the following code.
module.exports = {rpc: {host:"localhost",port:8543},networks: {development: {host: "localhost", //our network is running on localhostport: 8543, // port where your blockchain is runningnetwork_id: "*",from: "0x09fcc6a75c059265fbaf2d60476794f6f9813652", // use the account-id generated during the setup processgas: 20000000}}};
Note that the “from” field should contain your account id, which was create in the Geth Javascript console when you ran “personal.newAccount(‘seed phrase’). In order to get this again you can use the following command in the Geth Javascript console.
> personal.listAccounts[0]
5. Compile the smart contract in Truffle, and deploy it to the private blockchain
$ truffle compile
$ truffle migrate
Troubleshooting:
- Ensure that you are in the “truffle” folder to run the commands above.
- The deployment of the smart contract to the blockchain may take a few seconds.
Ensure that you are mining blocks at the time of smart contract deployment, so that the bytecode is added to the newly mined block of the blockchain. In case you stopped mining before, you can restart mining using the following command in the Geth Javascript console.
> miner.start()
Step 5: Execute the Smart contract on the private Ethereum blockchain
In this step we will execute our “Hello World” smart contract on the blockchain using the Truffle console.
Open a new Terminal window and navigate to the “truffle” folder. To launch Truffle console from the terminal shell,
$ truffle console
Within the truffle console, run the following commands
> var dApp
> Hello.deployed().then(function(instance) { dApp = instance; })
> dApp.message.call()
You should see the following result,
Yay!!! You have now successfully created your very first private Ethereum blockchain and run your very first Solidity code on the Blockchain!
We covered a lot in this tutorial and following is a summary of all the steps.
Found this post useful? Hit the 👏 button below to show how much you liked it :)
Follow me on Medium for the latest posts on Blockchain!
Read Next:
Other Articles by Prashant Ram:
- BlockChain Demystified: Looking Under the hood of the BlockChain
- Understanding Digital Signatures in Blockchain and Cryptocurrency
- The Distributed Autonomous Organization (DAO) and how Blockchain/AI can take over the network — and why you cannot stop it!
Happy Coding!
Next Steps:
In the upcoming part 2 we will examine how to quickly setup a private Ethereum blockchain on the Microsoft Azure cloud and deploy a smart contract. We will also examine the Remix IDE.
References: