Demystifying Ethereum Private Blockchain in less than 4 minutes!
What is Ethereum?
Ethereum is a open-source public blockchain, it also features development of smart contract in a language called Solidity, and provides Turing-Complete Virtual machine that is Ethereum Virtual Machine, which can execute script using an international network of public node.
What is Private Blockchain?
“A private blockchain network requires an invitation and must be validated by either the network starter or by a set of rules put in place by the network starter. Businesses who set up a private blockchain, will generally set up a permissioned network. This places restrictions on who is allowed to participate in the network, and only in certain transactions. Participants need to obtain an invitation or permission to join. The access control mechanism could vary: existing participants could decide future entrants; a regulatory authority could issue licenses for participation; or a consortium could make the decisions instead. Once an entity has joined the network, it will play a role in maintaining the blockchain in a decentralized manner”
Building a Private Ethereum Blockchain
Setting up Required Tool
Let’s setup the environment required to build private ethereum blockchain
Installing Geth (go-ethereum)
brew tap ethereum/ethereum
brew install ethereum
brew install ethereum --devel
Installing Truffle
npm install -g truffle
Installing Solidity
npm install -g solc
Setting up Ethereum Private Blockchain
Creating the project directory and navigating to the project
mkdir project
cd project
Creating the genesis block
touch genesis.json
Paste the following code in genesis.json and save the file
{"config": {"chainId": 42,"homesteadBlock": 0,"eip155Block": 0,"eip158Block": 0},"alloc": {"0x0000000000000000000000000000000000000001": {"balance": "111111111"},"0x0000000000000000000000000000000000000002": {"balance": "222222222"}},"coinbase" : "0x0000000000000000000000000000000000000001","difficulty" : "0x20000","extraData" : "","gasLimit" : "0x8880000","nonce" : "0x0000000000000042","mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000","parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000","timestamp" : "0x00"}
Create a folder to store your chaindata
mkdir privchain
Initiate the chaindata in privchain directory
geth --datadir privchain init genesis.json
Open a new tab in terminal and run the private ethereum blockchain
geth --port 3000 --networkid 58342 --nodiscover --datadir=./privchain --maxpeers=0 --rpc --rpcport 8545 --rpcaddr 127.0.0.1 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,miner"
Open a new tab in terminal and connect to the private blockchain so that your can create a account and mine some ether before deploying the smart contract
geth attach http://127.0.0.1:8545
Create and unlock you account
> personal.newAccount('password')
> personal.unlockAccount(web3.eth.coinbase, "password", 15000)
Start mining Ether
> miner.start()
Check how many Ether you have mined
web3.fromWei(eth.getBalance(eth.coinbase), "ether")
To stop mining
miner.stop();
Creating a smart contract and deploying it on private ethereum blockchain
Navigate to the same directory of the project and initialize truffle
truffle init
Create a new file called Hello.sol in contracts folder and paste in the following code
pragma solidity ^0.4.15;contract Hello {string public message; function Hello() {message = "Hello, World"; }}
Create a new file called 2_deploy_contracts.js in migartion folder and paste in the following code
var Hello = artifacts.require("./Hello.sol");module.exports = function(deployer) {deployer.deploy(Hello);};
Now to to your truffle.js and replace the current code with the following code
module.exports = {rpc: {host:"localhost",port:8545},networks: {development: {host: "localhost",port: 8545, // port where your blockchain is running network_id: "*",from: "f6a9eb4b8dd10ce0cd8fb8d8a54eb03d57d9c578",gas: 18000000}}};
The from hex code which represent your account comes following line on code in Geth Javascript Console
> personal.listAccounts[0]
Save all your changes and run
truffle compile
truffle migrate
Test if you have successfully deployed the smart contract by initializing truffle console in a new terminal tab
truffle console
Test if you can access your contract
> var app
> Hello.deployed().then(function(instance) { app = instance; })
> app.message.call()
Next Step?
Running private ethereum blockchain network on IBM Cloud :)
Read more:
- Follow me for more: https://www.engineerability.com