ethereum.org
Build Your Own Blockchain
How to Create a Private Ethereum Blockchain from Ground-up?
GBD ravine (
ey, Oct 19, 2017-12 minread
Ethereum is a decentralized platform that runs smart contracts, applications that run
exactly as programmed without possibility of downtime, censorship, fraud or third party
interference. In this blog post I will take you through all the steps required in setting up a
fully functioning private ethereum blockchain, inside your local network — which
includes:
+ Setting up a private blockchain with ethereum using geth.
* Setting up the MetaMask ethereum wallet to work with the private blockchain.+ Transfer funds between multiple accounts.
* Create, deploy and invoke a smart contract on the private blockchain using remix.
* Setting up ethereum block explorer over the private blockchain.
Install Geth
Go Ethereum (or geth) is one of the three original implementations (along with C+ +
and Python) of the ethereum protocol. It is written in Go, fully open source and licensed
under the GNU LGPL v3. Go Ethereum is available either as a standalone client called
geth that you can install on pretty much any operating system, or as a library that you
can embed in your Go, Android or iOS projects.
To install geth on Mac OS X, we use homebrew. Homebrew installs the stuff you need
that Apple didn’t. This blog assumes you have homebrew installed already, in case not
check this out. Once you have homebrew installed, following commands will install
geth.
/ethereum
reum
brew tap ether,
brew install e
Installing geth on Ubuntu is straightforward, you just need to use apt-get — the
commands are shown below.
sudo apt-g P
sudo add-apt-repository -y ppa:thereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
For Windows, you can find the corresponding geth installation here. If you find any
difficulties in any of the above installations, check this out.
Create a Miner Account
First we need to create an account for ethereum mining. This will generate a
public/private key pair for you — and will be password protected. Do not lose your
password, otherwise you will never be able to recover the keys. By default, keys arestored inside, /keystore. Everything geth persists, gets written inside
datadir (except for the PoW Ethash DAG). The default data directory locations are
platform specific. It is always better to override the path of the datadir, and maintain
your own location for your private blockchain.
* Mac: ~/Library/Ethereum
« Linux: ~/.ethereum
* Windows: %APPDATA%\Ethereum
The Ethash DAG is stored at ~/.ethash (Mac/Linux) or %APPDATA%\Ethash (Windows)
so that it can be reused by all clients.
Following command shows how to create an account, with a custom path for the
datadir. Once completed, it will print your ethereum address.
geth account new --datadir
ory> init attach ipe:/geth.ipe
Exampl
geth --datadir /path/to/data/dir attach ipe:/path/to/data/dir
/geth.ipe
The console connects to the mining node over ipe. ipe (inter-process communications)
works on the local computer. In this case geth creates an ipe pipe (which is represented
by the file /geth.ipc) on local computer’s filesystem — and
console makes the connection to that node over épe.
View All Accounts
Once you are connected to the geth console, you can try out the following command to
list all the available accounts.
counts
9b359e86893efa3d9732e4c65ced51567edd0")
View Account Balance
Following command shows how to view the balance of a given account from the geth
console.> eth. getBalance ("0x7a69b359e86893efa3d9732e4c65ced51567edd0")
1.295e+21
Connect MetaMask Ethereum Wallet
MetaMask is an ethereum wallet, running as a chrome extension. It injects the ethereum
web3 API into every website’s javascript context, so that those apps can read from the
blockchain. MetaMask also lets the user create and manage their own identities, so when
an application wants to perform a transaction and write to the blockchain, the user gets
a secure interface to review the transaction, before approving or rejecting it.
To connect MetaMask to the private ethereum blockchain, we need to pick the right host
name and the port. Web3 API is the ethereum javascript API implemented in web3.js. To
talk to an ethereum node from a javascript application, MetaMask uses the web3.js
library, which gives a convenient interface for the rpe methods. Under the hood it
communicates to a local node through rpe calls. web3.js works with any ethereum node,
which exposes an rpc layer. You might have noticed in the above, when we start the
mining node, we can pass the parameter rpcapi to specify, which interfaces we need to
expose from that node. By default, if you do not specify anything, eth,net, web3 — will
be exposed.
mo ¥
Main Ethereum Network
local-eth-account ***
psten Test Network ca eI
000
Kovan Test Network
Rinkeby Test Network
No transaction history.
Co Mele react r sy}
@ Custom RPC
Transfer EtherMetaMask will create you an ethereum account — a private key and an ethereum
address, Following shows you how to transfer ether from the first account you created at
the very beginning to the MetaMask account, from the geth console. To transfer funds
from an account, we have to use that account's private key for the signature. To use the
private key, we need to unlock it, as shown below.
personal. unlockAccount (
"0x7a69b359e86893efa3d9732e4c65ced51567edd0", "password")
personal is a management API provided by geth. In addition to personal, geth also
provides management APIs: admin, debug, miner and txpool. Once we unlock the
account, we can define three variables in the console, for the sender, receiver and
amount to be sent. The value of the sender is the ethereum address we created at the
very beginning of this blog, and the value of the receiver is the ethereum address created
under MetaMask.
> var sender = "0x7a69b359e86893efa3d9732e4c6S5ced51567edd0";
> var receiver = "0xA9£28458e51170F285440990c196c1592D3a73£5"
> var amount = web3.toWei(1, "ether")
Following command will do the funds transfer — it refers the variables we defined
above.
> eth.sendTransaction({from:sender, to:receiver, value: amount})
View Account Balance In MetaMask
Once we completed the funds transfer following the above steps, you can find the
account balance in two ways. One way is directly from the MetaMask plugin, as shown.
below.
M 0 MSS. e &local-eth-account #2
OxA9f28...
SENT TOKENS
1.000
305.99 US
A
Oo
No transaction history.
The other way is via the geth console, with the following command.
> eth. getBalance ("0xA9£28458eE1170F285440990c196c1592D3a73£5")
1000000000000 oo
Remex Solidity Editor
Solidity is the most popular programming language to write ethereum smart contracts.
Remix is an IDE for solidity and has an integrated debugger and testing environment.
You can access remix online editor from here. Remix can be connected to any ethereum
network. To connect it to our local blockchain, make sure that you have started your
mining node with the following command, with the highlighted parameter. The meaning
of this parameter was discussed before. Instead of “*” (which is more open) you can also
use “https://remix.ethereum.org” as the value of rpccorsdomain.
geth --mine --rpe --rpecorsdomain "*" --networkid -~
datadir To connect, remix to our private network, we need to change the Environment to Web3
Provider, under the tab Run. When you do this change, remix will prompt you to
specify the Web3 Provider Endpoint — set the value http://localhost:8545. Unless you
have changed the port explicitly, the default mining node will start on the port 8545.
4 Compile Run Settings Debugger Analysis Support
Environment Web3 Provider $
Account 0x7a6...7edd0 (12324 ether) +
Gas limit 3000000
Gas Price 0
Value °
Writing a Smart Contract
Now we are all set to write our very first smart contract to run on ethereum. Copy the
following code and paste it on the remix online editor. This is a very simple smart
contract — and I do not wish to do a line by line explanation. In the next blog will
explain solidity in detail.
ragma solidity 0.4.11;
// a string variable
string public greeting;
// the function with the same name
constructor
function
greet.
llo(string greeting) {
eeting;
// change
function
e greeting message
Greeting (string _greeting)
_greeting;// get the greeting mi
function greet ()
greetin gr
returns (string _greeting)
If you have not changed any default settings in remix, it is set to auto compile. If not you
need to compile.
4 Compile Run Settings Debugger Analysis Support
@ Start to compile @ Auto compile
browser/Untitled.sol:Hello $ Details Publish on Swarm
After compiling, when you click on the Details button, it will show you the estimated gas
need to create this smart contract.
Introduction to Smart Contracts - Solidity 0.4.19 documentation
Acontract in the sense of Solidity is a collection of code (its functions) and data (its state) that
resides ata.
solidity.readthedocs.io
Deploying a Smart Contract
Now we can deploy our smart contract to our private blockchain. Under the Run tab,
make sure you have the right ethereum account selected, and then the right gas limit is
specified. You can keep gas price and value as zero.
+ Compile Run Settings Debugger Analysis Support
Environment Web3 Provider ¢
Account 0x7a6...7edd0 (15654 ether) 7KGas limit 3000000
Gas Price 0
Value °
browser/Untitled.sol:Hello
AtAddress Enter contract's address - i.e. 0x60606...
Create string greeting
We use an ethereum transaction, signed by the selected account above to deploy the
smart contract to the blockchain. To do the signing, first we need to unlock the account,
via the geth console.
> personal.unlockAccount (
"0x7a69b359086893ef£a3d9732e4c65ced51567edd0", "password")
Now you can click on the Create button to deploy the smart contract. In our smart
contract, we have a constructor which accepts a string parameter, that’s why remix
shows you an input box along with the Create button. You can type some value there
(within quotes, e.g: “Hi”) — or just keep it empty. You will see the following message on
the geth console when you submit the smart contract for creation. Also note that, if you
have not specified the right gas limit, the above will return an error.
10-19/07:31:08] Submitted contract creation
hash=0x£5511bb9d088672ac0d3896b8590b9a3e2
a549ed33a contract=0x42b7E903Fb42e191a7D623chI
00£02deecdd739¢3
1n4330D329478
Invoking a Smart Contract
Once you deploy the smart contract, remix UI gets changed a bit — as shown below,
under the Run tab.browser/Untitied.sol:Hello at 0x42b...29d78 (blockchain) 5
greeting 0: string:
greet tring: _greeting
setGreeting string _greeting
Now you can set some value to the setGreeting method — and click on it to invoke the
smart contract. Once again, make sure that you have your account unlocked, because, to
invoke a smart contract we use ethereum transactions and we need the signature of the
initiator. Once done with the setGreeting method, you can invoke other methods too.
* browser/Untitled.sol:Hello at 0x42b...29d78 (blockchain) ®
greeting 0: string: Hi
greet 0: string: _greeting Hi
setGreeting "Hi"
Ethereum Block Explorer
You may be familiar with etherscan, which gives a lot of insights into the ethereum
public blockchain. But we cannot use it to point to our local blockchain. Ethereum block
explorer even though not as feature rich as etherscan, is quite useful to find out what is
going on in our local blockchain.
To setup ethereum block explorer first we need to get its source code from the following
git repo.
git clone https: //github.com/carsenk/explorerThen, run the following install command from the explorer directory.
npm install
Once the installation is done, start the ethereum block explorer with the following
command — and then you can access the web console from http://localhost:8000.
npm start
ETHEREXPLORER ome acknaninf
e cen
ne Ere a on
& Recent Blocks
GE Yr cremainmondinortetsceisisoey ose cataarenaem @ ome
To connect ethereum block explorer to our local blockchain, make sure that you have
started your mining node with the following command, with the highlighted parameter
— the meaning of this parameter was discussed before.
geth
dataa:
@ --rpecorsdomain "*" --networkid --
o-data-directory>Update: There is a discussion on this blog on Hacker News. Please feel free to join
https://news.ycombinator.com/item?id= 15509147
Summary
In this blog post we discussed how to set up a private blockchain with ethereum using
geth. Then we got MetaMask ethereum wallet to work with the private blockchain and
transferred funds to the ethereum account created in MetaMask. remix online IDE was
used to create, deploy and invoke a smart contract on the private blockchain. Finally we
did set up ethereum block explorer over the private blockchain.
Prabath Siriwardena
Visit Amazon.com's Prabath Siriwardena Page and shop for all Prabath
Siriwardena books. Check out pictures...
Ethereum Blockchain Technology