Step-By-Step Guide Installing Ethereum & Building A Blockchain On Ubuntu 16.04 Linux Server
Step-By-Step Guide Installing Ethereum & Building A Blockchain On Ubuntu 16.04 Linux Server
Step--By-Step
Step Guide Installing Ethereum & Building
uilding a Blockchain
lockchain on
Ubuntu 16.04 Linux Server
By Kefa Rabah, [email protected] March 05, 2018 The Lake Institute
A blockchain is a digital ledger of records that’s arranged in chunks of data called blocks. These blocks
then link with one another through a cryptographic validation known as a hashing function function.. Linked
together, these blocks form an unbroken chain
chain —
— a blockchain
blockchain.
The reason that this type of data structure is useful for things like cryptocurrencies is decentralization
zation,
meaning the records inside this chain aren’t stored in any single location, are accessible by everyone, and
are immutable by any one party.
Applications
Although blockchain is most commonly associated with Bitcoin, there are many uses for this technology.
There are several broad categories of blockchain applications, a couple of which include:
1
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
Currency
urrency and Digital Assets
The Blockchain that makes up Bitcoin sends money globally to individuals and merchants. But
Blockchains can also create digital assets like stocks and bonds.
This Hands-on
Hands guide is meant to serve as an introduction to blockchain programming for those looking to
expand their technical knowledge.
You will be using Ethereum to make a smart contract. Ethereum is a blockchain with a built in Turing
Turing--
complete programming language, meaning that it can run applications modeling any computable problem
(as long as runtime and memory allows). Be sure to familiarize yourself with the following Ethereum terms
before you begin:
Accounts are the basic unit or object in Ethereum. The blockchain tracks the state of every account.
There are two types of accounts: Externally Owned Accounts (EOA)
(EOA),, which are controlled by human
users, and Contract Accounts,
Accounts, which are controlled by their internal contract code and can be activated
by an Externally Owned Account.
A transaction is a signed data packet which stores messages sent from account to account. It contains
the following data fields:
• Recipient
• Signature
ignature
2
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
Source: EtherScan
In order to reduce
reduce risk of malicious transactions, like Distributed Denial of Service (DDoS) attacks,
Ethereum charges a fee for every transaction done through it.
So who’s collecting these fees? There are nodes called miners which collect the fees in order to verify
and execute all transactions. The miners group the transactions into blocks which can then be added to
the blockchain.
blockchain
Now that you’ve got a basic understanding of how Etherium works, it’s time to get started.
Note that we have used open source Ubuntu Linux, but this will also work on other Linux distributions, as
well as on MacOS (with Homebrew)) and Windows 10 (using your terminal and the latest stable binary
binary). A
full list of command line tools and installation techniques can be found here
here.
“Hello, Testnet”
This is a guide for starting your own custom Ethereum blockchain on Ubuntu Linux, not to be confused
with starting a node on the main Ethereum blockchain. Here we are starting private blockchain – an
entirely new and separate blockchain
blockchain that cannot interact with Ethereum mainnet.
Starting your own Ethereum blockchain is useful, educational, and safer than the public testnet. Learning
to set up a private testnet provides tangibility to otherwise abstract concepts such as mining, network
peers, and even the geth datadir or hashchain.
hashchain
3
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
The Hands-on
Hands on Tutorial
The tutorial presented here is to help developers have a better understanding through starting everything
from scratch. But you can always do this in a much more easier way: Use the official CLI that is
automatically installed followed with geth called puppeth
puppeth,, which helps create and customize your
genesis file. For example, simply
simply type puppet in your terminal window and you will be prompted to input
the required parameters such as where should the file be saved at, which consensus to use (PoW or
PoA), and which accounts should be prefunded.
Making a Blockchain
Prerequisites
You need to have Geth
th installed. The easiest way to do this is through homebrew.
sudo apt-get
apt get install -y
y software-
software-properties
properties-common
sudo add-apt-repository
add repository -y
y ppa:ethereum/ethereum
sudo apt-get
apt get update
sudo apt-get
apt get install -y
y ethereum
The Genesis block is the first block in the chain; iti iss the start of the blockchain
blockchain.. The Genesis file is a
JSON file, "genesis.json
genesis.json" is the file that defines that defines the characteristics of that initial block and
subsequently the rest of the blockchain. It is like the "settings
settings" for your blockchain. For example, the
chain configuration, level of difficulty to mine blocks, etc.
mkdir my-eth-chain
my chain
cd my-eth-chain
my chain
nano myGenesis.json
{
"config":
: {
"chainId
chainId": 15,
"homesteadBlock
homesteadBlock":
homesteadBlock 0,
"eip155Block
eip155Block":
: 0,
"eip158Block
eip158Block":
: 0,
"byzantiumBlock": 0
},
"alloc":
: { "0xedeb444469a46300bcab856338d75fa589b86013
0xedeb444469a46300bcab856338d75fa589b86013
0xedeb444469a46300bcab856338d75fa589b86013": {"balance
balance":
"111111111
111111111"},
},
"0x0000000000000000000000000000000000000002
0x0000000000000000000000000000000000000002
0x0000000000000000000000000000000000000002": {"balance
balance": "222222222
222222222"}
},
"coinbase"
": "0x0000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000",
"difficulty
difficulty" : "0x20000
0x20000",
"extraData
extraData" : "",
"gasLimit"
" : "0x2fefd8
0x2fefd8",
"nonce" : "0x0000000000000042
0x0000000000000042",
0x0000000000000042
"mixhash" :
"0x0000000000000000000000000000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash
parentHash" :
"0x0000000000000000000000000000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp
timestamp" : "0x00""
}
config
• chainId — this is your chain’s identifier, and is used in replay protection.
chainId
• homesteadBlock, eip155Block, eip158Block, byzantiumBlock
homesteadBlock, byzantiumBlock — these relate to chain forking and
versioning, so in our case let’s leave them 0 since we’re starting a new blockchain.
difficulty
This dictates how difficult it is to mine a block. Setting this value low (~10
(~10–10000)
10000) is helpful in a private
blockchain as it lets you mine blocks quickly, which equals fast transactions, and plenty of ETH to test
with. For comparison, the Ethereum
Ethereum mainnet Genesis file defines a difficulty of 17179869184.
gasLimit
This is the the total amount of gas that can be used in each block. With such a low mining difficulty, blocks
will be moving pretty quick, but you should still set this value pretty hhigh
igh to avoid hitting the limit and
slowing down your network.
alloc
Here you can allocate ETH to specific addresses. This won’t create the account for you, so make sure it’s
an account you already have control of. You will need to add the account to your private chain in order to
use it, and to do that you need access to the keystore/utc file. For example, Geth and MyEtherWallet
EtherWallet
5
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
give you access to this file when you create an account, but Metamask and Coinbase do not. The
addresses provided are not real addresses, they are just examples. Here we allocate 100,000 and
120,000 ETH,
ETH respectively.
$ geth --datadir
datadir ./myDataDir init ./myGenesis.json
Fig. 1
You should then see that the genesis state was successfully written.
Note:
But then you may notice there’s a warning in the output (as can be seen from Fig. 1):
WARN [07–05|17:36:27]
[07 05|17:36:27] No etherbase set and no accounts found as default
This is because the "alloc" parameter in the genesis file does not create any account. It is just an
address with balance in it. For example in this case,
eth.getBalance("7df9a875a174b3bc565e6424a0050ebc1b2d1d82")
6
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
$ geth --datadir
datadir ./myDataDir --networkid
networkid 15 console 2>
2>>> myEth.log
Fig. 2
Note: this is the geth JavaScript console. Any command with the symbol > should be typed here.
cd my-eth-chain
my chain
Type
tail -f
f myEth.log
7
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
Note: now
ow we can start Geth and connect to our own private chain. As you will see the geth.ipc
endpoint socket connection will be opened and so will port 30303.
Whenever we start Geth the IPC endpoint will open, which is used to process connections to Geth
with programs like MetaMask, Ethereum Wallet, Mist, or for opening the Geth Javascript API console.
We will discuss all of these next.
4. Import/Create an Account
If you allocated
llocated ETH in the Genesis file, import the corresponding account by dragging the UTC file into
the "myDataDir/keystore
myDataDir/keystore directory and
myDataDir/keystore" and skip to step 5.
From the geth console, we’ll need to create a new account and a new password by typing in
whatever you want. You will have to type your new password twice.
> personal.newAccount("<YOUR_PASSPHRASE>")
personal.newAccount("<YOUR_PASSPHRASE>")
> personal.newAccount()
personal.newAccount(
8
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
"0xedeb444469a46300bcab856338d75fa589b86013"
Note: doo not forget this passphrase! You will be typing this a lot, so for this test network you can keep
it simple.
10. Save this address somewhere and exit by typing “exit” and hitting Enter.
> eth.coinbase
Note: iff this address is the same as the one from step 4, skip the rest of step 5.
OR
To set your default account, type
> miner.setEtherbase(web3.eth.accounts[0])
Network Connectivity
With all nodes that you want to run initialized to the desired genesis state, you'll need to start a bootstrap
node that others can use to find each other in your network and/or over the internet. The clean way is to
configure and run a dedicated bootnode:
bootnode --genkey=boot.key
genkey=boot.key
bootnode --nodekey=boot.key
nodekey=boot.key
9
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada