• No results found

Getting Started

In document BLOCKCHAIN DEEP DIVE (Page 108-113)

Here is the thing, you don’t have any ether to start with. With that in mind, let’s limit this deployment to a “private” blockchain network that will sort of run as a development or staging version of the main ethereum network. From a functionality standpoint, this private network will be identical to the main blockchain, with the exception that all transactions and smart contracts deployed on this network will be accessible only to the nodes connected in this private network. Geth will aid in this private or “testnet” setup. Using the tool, you’ll be able to do everything the ethereum platform advertises, without needing real ether.

Remember, the blockchain is nothing more than a digital and public ledger preserving transactions in their chronological order. When new transactions are verified and configured into a block, the block is then appended to the chain, which is then

distributed across the network. Every node on that network will update its local copy of the chain to the latest copy. But you need to start from some point—a beginning or a genesis. Every blockchain starts with a genesis block, that is, a block “zero” or the very first block of the chain. It will be the only block without a predecessor. To create your private blockchain, you need to create this genesis block. To do this, you need to create a custom genesis file and then tell Geth to use that file to create your own genesis block.

Create a directory path to host all of your ethereum-related data and configurations, and change into the config subdirectory:

$ mkdir ~/eth-evm

$ cd ~/eth-evm

$ mkdir config data

$ cd config

DEEP DIVE

Open your preferred text editor and save the following contents to a file named Genesis.json in that same directory:

{

"config": {

"chainId": 999,

"homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 },

"difficulty": "0x400", "gasLimit": "0x8000000", "alloc": {}

}

This is what your genesis file will look like. This simple JSON-formatted string describes the following:

config — this block defines the settings for your custom chain.

chainId — this identifies your Blockchain, and because the main ethereum network has its own, you need to configure your own unique value for your private chain.

homesteadBlock — defines the version and protocol of the ethereum platform.

eip155Block / eip158Block — these fields add support for non-backward-compatible protocol changes to the Homestead version used. For the purposes of this example, you won’t be leveraging these, so they are set to “0”.

difficulty — this value controls block generation time of the blockchain. The higher the value, the more calculations a miner must perform to discover a valid block. Because this example is simply deploying a test network, let’s keep this value low to reduce wait times.

DEEP DIVE

gasLimit — gas is ethereum’s fuel spent during transactions. As you do not want to be limited in your tests, keep this value high.

alloc — this section prefunds accounts, but because you’ll be mining your ether locally, you don’t need this option.

Now it’s time to instantiate the data directory. Open a terminal window, and

assuming you have the Geth binary installed and that it’s accessible via your working path, type the following:

$ geth --datadir /home/petros/eth-evm/data/PrivateBlockchain ↪init /home/petros/eth-evm/config/Genesis.json

WARN [02-10|15:11:41] No etherbase set and no accounts found ↪as default

INFO [02-10|15:11:41] Allocated cache and file handles ↪database=/home/petros/eth-evm/data/PrivateBlockchain/

↪geth/chaindata cache=16 handles=16

INFO [02-10|15:11:41] Writing custom genesis block INFO [02-10|15:11:41] Successfully wrote genesis state ↪database=chaindata

hash=d1a12d...4c8725

INFO [02-10|15:11:41] Allocated cache and file handles ↪database=/home/petros/eth-evm/data/PrivateBlockchain/

↪geth/lightchaindata cache=16 handles=16

INFO [02-10|15:11:41] Writing custom genesis block INFO [02-10|15:11:41] Successfully wrote genesis state ↪database=lightchaindata

The command will need to reference a working data directory to store your private chain data. Here, I have specified eth-evm/data/PrivateBlockchain subdirectories in my home directory. You’ll also need to tell the utility to initialize using your genesis file.

This command populates your data directory with a tree of subdirectories and files:

DEEP DIVE

$ ls -R /home/petros/eth-evm/

.:

config data ./config:

Genesis.json ./data:

PrivateBlockchain

./data/PrivateBlockchain:

geth keystore

./data/PrivateBlockchain/geth:

chaindata lightchaindata LOCK nodekey nodes transactions.rlp ./data/PrivateBlockchain/geth/chaindata:

000002.ldb 000003.log CURRENT LOCK LOG MANIFEST-000004 ./data/PrivateBlockchain/geth/lightchaindata:

000001.log CURRENT LOCK LOG MANIFEST-000000 ./data/PrivateBlockchain/geth/nodes:

000001.log CURRENT LOCK LOG MANIFEST-000000 ./data/PrivateBlockchain/keystore:

Your private blockchain is now created. The next step involves starting the private network that will allow you to mine new blocks and have them added to your blockchain. To do this, type:

petros@ubuntu-evm1:~/eth-evm$ geth --datadir

↪/home/petros/eth-evm/data/PrivateBlockchain --networkid 9999

DEEP DIVE

WARN [02-10|15:11:59] No etherbase set and no accounts found ↪as default

INFO [02-10|15:11:59] Starting peer-to-peer node

↪instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.2 INFO [02-10|15:11:59] Allocated cache and file handles

↪database=/home/petros/eth-evm/data/PrivateBlockchain/

↪geth/chaindata cache=128 handles=1024

WARN [02-10|15:11:59] Upgrading database to use lookup entries INFO [02-10|15:11:59] Initialised chain configuration

↪config="{ChainID: 999 Homestead: 0 DAO: <nil> DAOSupport:

↪false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil>

↪Engine: unknown}"

INFO [02-10|15:11:59] Disk storage enabled for ethash caches ↪dir=/home/petros/eth-evm/data/PrivateBlockchain/

↪geth/ethash count=3

INFO [02-10|15:11:59] Disk storage enabled for ethash DAGs ↪dir=/home/petros/.ethash count=2

INFO [02-10|15:11:59] Initialising Ethereum protocol ↪versions="[63 62]" network=9999

INFO [02-10|15:11:59] Database deduplication successful ↪deduped=0

INFO [02-10|15:11:59] Loaded most recent local header ↪number=0 hash=d1a12d...4c8725 td=1024

INFO [02-10|15:11:59] Loaded most recent local full block ↪number=0 hash=d1a12d...4c8725 td=1024

INFO [02-10|15:11:59] Loaded most recent local fast block ↪number=0 hash=d1a12d...4c8725 td=1024

INFO [02-10|15:11:59] Regenerated local transaction journal ↪transactions=0 accounts=0

INFO [02-10|15:11:59] Starting P2P networking INFO [02-10|15:12:01] UDP listener up

↪self=enode://f51957cd4441f19d187f9601541d66dcbaf560

↪770d3da1db4e71ce5ba3ebc66e60ffe73c2ff01ae683be0527b77c0f96

DEEP DIVE

↪a178e53b925968b7aea824796e36ad27@[::]:30303

INFO [02-10|15:12:01] IPC endpoint opened: /home/petros/eth-evm/

↪data/PrivateBlockchain/geth.ipc

INFO [02-10|15:12:01] RLPx listener up

↪self=enode://f51957cd4441f19d187f9601541d66dcbaf560

↪770d3da1db4e71ce5ba3ebc66e60ffe73c2ff01ae683be0527b77c0f96

↪a178e53b925968b7aea824796e36ad27@[::]:30303

Notice the use of the new parameter, networkid. This networkid helps ensure the privacy of your network. Any number can be used here. I have decided to use 9999. Note that other peers joining your network will need to use the same ID.

Your private network is now live! Remember, every time you need to access your private blockchain, you will need to use these last two commands with the exact same parameters (the Geth tool will not remember it for you):

$ geth --datadir /home/petros/eth-evm/data/PrivateBlockchain ↪init /home/petros/eth-evm/config/Genesis.json

$ geth --datadir /home/petros/eth-evm/data/PrivateBlockchain ↪--networkid 9999

In document BLOCKCHAIN DEEP DIVE (Page 108-113)