This is PART-4 of The Product Manager’s guide to the Blockchain series! If you somehow landed on my publication for the first time, Welcome! I recommend you start from , and then read and part3 before reading this post. However If you are the explorer type, read on!
(Update: Here’s the latest — of the blockchain series )
In of this series, we looked at the mechanics of Ethereum and also talked about the concept of Ethereum Accounts, Smart Contracts and Gas — the fuel that helps all these pieces to work together. Its been a lot of reading so far, but while you can read all the blockchain content available on the internet, nothing helps understand blockchains better than building one yourself. So that’s what I did. You can simply follow this post and build a little prototype to see how everything we’ve talked about so far comes together.
, but basically the Ethereum blockchain network is simply lots of EVM (Ethereum Virtual Machines) or “nodes” connected to every other node to create a mesh. Each node runs a copy of the entire blockchain and competes to mine the next block or validate a transaction. Whenever a new block is added, the blockchain updates and is propagated to the entire network, such that each node is in sync.
To become a node in the Ethereum network, your computer will have to download and update a copy of the entire Ethereum blockchain. To achieve this Ethereum provides tools that you can download, connect to the Ethereum network with and then interact with it. These are:Geth — “……if you have experience with web development and are interested in building frontends for dapps, you should experiment with Geth.”
Eth — “…If you want added security by running two different implementations in parallel or are serious about GPU mining, then the C++ “Eth” client is for you.”
Pyethapp — “ …If you are a Python developer that wants to build decentralized apps or are interested in Ethereum for research or an academic purpose, this is a great client”
Each of these tools will eventually provide us access to a console — a JavaScript environment that contains all of the main features that we’d need later so I am guessing no matter what tool you choose to set up your blockchain, we should eventually converge on the console.
I simply chose Geth since I have some understanding of web development. The rest of this post assumes that we are using Geth to interact with the Ethereum Network.
Ethereum’s Go implementation is called Geth
Geth is a command line interface (CLI) tool that communicates with the Ethereum Network and acts as the a link between your computer, its hardware and the rest of the ethereum nodes or network computers. If a block is mined by another node, your Geth program will pick it up and then pass on the new information onto your GPU or CPU to update the blockchain. With Geth, you can
Homebrew, The missing package manager for macOS
I simply used Homebrew which makes installing Geth (really anything) really easy. If you have a mac but don’t have Homebrew ,I didn’t have any either, but Ethereum lets you create your own ‘private’ blockchain network, sort of a dev/staging version of the main network. This private network is exactly like the main Ethereum chain from a functionality standpoint, except any transactions and smart contracts deployed on this network are only accessible to nodes that are connected t this private network. So that’s what we’ll do — we will create a private blockchain.
blockchain As you probably know by now, a blockchain is nothing but a digital ledger in which transactions are recorded chronologically and publicly. These transactions are recorded in blocks, and nodes in a distributed network compete to find the next valid block. Once a valid block is found, it is added to the blockchain, and this information relayed to the network. Every node then updates their blockchain to the latest copy.
Every blockchain starts with a Genesis Block, the very first block in the chain, block ZERO— the only block that does not have a predecessor.To create our private blockchain then, we will create a genesis block. To do this, we will create a custom Genesis file, and ask Geth to use that genesis file to create our own genesis block , which in turn will be the start of our custom private blockchain. Here’s what a Genesis file looks like:
config: the config block defines the settings for our custom chain and has certain attributes to create a private blockchain
- chainId: identifies our blockchain, the main Ethereum chain has its own ID, but we will set it to a unique value for our private chain.
- homesteadBlock: Homestead is the second major version of the Ethereum platform and is the first production release of Ethereum. It includes several protocol changes. Since we are already on homestead version, this attribute is 0
.
- eip155Block/eip158Block: Homestead version was released with a few backward-incompatible protocol changes, and therefore requires a hard fork. These protocol changes/improvements proposed through a process Ethereum Improvement Proposals (EIPs). Our chain however won’t be hard-forking for these changes, so leave as 0
- alloc: This is where you can create your wallet and pre fill it with fake ether. For this post however, we will mine our ether locally quickly so we don’t use this option.
At this point, lets go ahead and create our CustomGenesis.json
file . I simply used a text editor and stored it in a folder on my computer.
geth — identity “yourIdentity” — init /path_to_folder/CustomGenesis.json — datadir /path_to_your_data_directory/ACPrivateChain
This snippet instructs Geth to use the CustomGenesis.json
file you created as to be the first block of your custom blockchain. Then, we also specify a data directory where our private chain data will be stored. Geth will create the data directory for you. Just choose a location that is separate from the public Ethereum chain folder, if you have one
my terminal window showing successful genesis state written
geth --datadir /path_to_your_data_directory/ACPrivateChain --networkid 9876
This snippet tells geth to start our private network and use the directory we just specified to access our private blockchain details .
Note that we also specified a parameter called networkid
. This marks the identity of your Ethereum network. We’ve used 9876 in this example, but You SHOULD replace it with a random number of your choice to create your own network and to prevent others from inadvertently connecting to your network
The Main Ethereum network has a networkid=1
.
0: Olympic, Ethereum public pre-release testnet1
: Frontier, Homestead, Metropolis, the Ethereum public main network1
: Classic, the (un)forked public Ethereum Classic main network, chain ID _61 _1
: Expanse, an alternative Ethereum implementation, chain ID _2 _2
: Morden, the public Ethereum testnet, now Ethereum Classic testnet3
: Ropsten, the public cross-client Ethereum testnet4
: Rinkeby, the public Geth Ethereum testnet42
: Kovan, the public Parity Ethereum testnet7762959
: Musicoin, the music blockchain
Another important thing. Every time you want to access your private chain, you will have to run these two snippets as is and in that order since Geth DOES NOT remember the parameters of your private blockchain network. Therefore, I suggest you save the set-up steps below, so you can access it later.
#1geth — identity “yourIdentity” — init /path_to_folder/CustomGenesis.json — datadir /path_to_your_data_directory/YOUR_FOLDER
#2geth --datadir /path_to_your_data_directory/ACPrivateChain --networkid YOUR_NETWORK_ID
If you’ve followed along then at this point, You have a private blockchain, and a private network up and running! Now, we can start interacting with our private chain and our private network through accounts.Types of accounts in Ethereum Once you have your first geth instance open, open another console/terminal and type the following:
geth attach /path_to_your_data_directory/YOUR_FOLDER/geth.ipc
This will connect your 2nd console to the Geth instance running on your first terminal window (make sure its actually running). You should get a Javascript console like soOnce you have the javascript console, you are all set to create an account . personal.newAccount()
You will be asked to setup a password following which, a new account will be generated. Simple! Now, save this account # as since you will need it very often. Also, remember you passphrase because you’ll need it to access your account. But you knew that already. Next, check to see how much ether this account has using the following line of code eth.getBalance("ENTER_YOUR_ACCOUNT_NUMBER_HERE") here’s what I got
Your balance should be 0
. Rightly so, since you wouldn’t have any ether when you first create an account.
At this point, if you look at the other console that is running the Geth instance, you will see blocks being mined successfully by your miner. Give it 10–15 seconds and check your balance again.
Woohoo! Lots of ether show up in your new account. (Remember this is fake ether, you cannot use this ether to make transactions on the Main Ethereum network) but you can use this to test our several functions of the blockchain including transfers, deploying contracts etc.
Finally, stop the miner using the miner.stop() script.