Step-By-Step Guide Installing Ethereum & Building A Blockchain On Ubuntu 16.04 Linux Server [PDF]

  • 0 0 0
  • Suka dengan makalah ini dan mengunduhnya? Anda bisa menerbitkan file PDF Anda sendiri secara online secara gratis dalam beberapa menit saja! Sign Up
File loading please wait...
Citation preview

Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



Global Open Versity Blockchain DevOps Hands-on Hands on Labs Training Manual



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:



A TLI & GOV Open Access Technical Academic Publications Enhancing education & empowering people worldwide through eLearning in the 21st Century 1 April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada



www.globalopenversity.org and The Lake Institute



Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



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.



Verifiable Data and Smart Contracts A Blockchain can create a verifiable verifiable record of any data, file, or contract. This can be useful in any industry that uses big data, like the medical industry or government.



Understanding the Code Before we begin, it’s assumed that you have a basic understanding of programming and some understanding nderstanding of computer science theory and of course Linux OS. Hands guide is meant to serve as an introduction to blockchain programming for those looking to This Hands-on expand their technical knowledge. We further believe that the best way to truly understand unders tand a concept is to put it into practice. If you are interested in learning how to implement a blockchain contract, then in next Hands Hands-on doc, we we’ve ’ve put together an easy-to-follow, easy follow, step-by-step step step tutorial. 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 “Smart Contract” or “Decentralized Application (DApp)” is determined by code in a Con Contract tract Account which will be activated when an EOA sends a transaction to the Contract Account. The Contract Accounts are triggered by an external EOA to produce an answer that can be agreed upon by all nodes that see it. Each account contains at most four fields: • Nonce (a counter used to make sure each transaction occurs only once) • Ether Balance • Contract Code • Storage 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



www.globalopenversity.org and The Lake Institute



Global Open Versity



• •



Installing Ethereum & Building and Deploying a Blockchain v1.0



Amount of Ether transferred Data



Ether is Ethereum’s cryptocurrency (ETH/USD = ~$50, as of today) which is exchanged as a fee whenever an action runs in Ethereum.



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



www.globalopenversity.org and The Lake Institute



Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



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.



Installing geth on Linux Linux Ubuntu 16.04 First you need to install Ethereum from PPA. In your terminal, run the following: sudo sudo sudo sudo



apt-get apt get install -y y softwaresoftware-properties properties-common add-apt-repository add repository -y y ppa:ethereum/ethereum apt-get apt get update apt-get apt get install -y y ethereum



Create Genesis File The Genesis block is the first block in the chain, the Genesis file is a JSON file that defines the characteristics of that initial block and subsequently the rest of the blockchain. 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. 1. Create a directory to hold your network files mkdir my-eth-chain my chain cd my-eth-chain my chain 2. Create your genesis file nano myGenesis.json 3. Open your genesis file and paste the following Note:: there are 4 required value (config, difficulty difficulty, gasLimit,, alloc)) you need to specify in genesis.json. genesis.json. I’ve tried removing all of them and made sure all of them are required XD 4 April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada



www.globalopenversity.org and The Lake Institute



Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



{ "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" " } 4. Save this new genesis block file by hitting Ctrl+X, Y, Enter.



config • •



chainId  chainId — this is your chain’s identifier, and is used in replay protection. homesteadBlock, homesteadBlock, eip155Block, eip158Block, byzantiumBlock 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 h high 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



www.globalopenversity.org and The Lake Institute



Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



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.



Start your Node! Now the real fun begins, we’re all set to fire up our new Ethereum blockchain.



1. Instantiate your data directory 5. From the terminal enter the following command $ geth --datadir datadir ./myDataDir init ./myGenesis.json you should see out output display as shown in Fig. 1.



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") will output the exact value you set. section 6. You’re done with this section.



6 April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada



www.globalopenversity.org and The Lake Institute



Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



2. Start your Ethereum peer node. Networkid helps ensure the privacy of your network. You can use any number here (where we used “1114”), but other peers joining your network must use the same one. $ geth --datadir datadir ./myDataDir --networkid networkid 15 console 2> 2>> > myEth.log 7. From the terminal enter the following command Output should look like this:



Fig. 2



Note: this is the geth JavaScript console. Any command with the symbol > should be typed here.



3. Display your Ethereum logs 8. Open another terminal window cd my-eth-chain my chain Type tail -f f myEth.log Output should look like this:



7 April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada



www.globalopenversity.org and The Lake Institute



Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



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" myDataDir/keystore directory and 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. 9. In the geth JavaScript console, create an account: > personal.newAccount("") personal.newAccount("") Type and enter password twice: > personal.newAccount() personal.newAccount(



8 April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada



www.globalopenversity.org and The Lake Institute



Global Open Versity



Installing Ethereum & Building and Deploying a Blockchain v1.0



"0xedeb444469a46300bcab856338d75fa589b86013" Note: do o 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.



5. Set Default Account 11. Now from the geth console to check your default account run: > 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



www.globalopenversity.org and The Lake Institute