How to Implement Ethereum Project On A Private Chain. Made Simple For Beginners!! All Terms Explained!!

Well lets all think back to a point in our lives where we had to go to a court! How difficult was it? we all know the pain. Since the advent of blockchain we can see a lot of implementation of the same in many fields, Judiciary is to be one of them. This use case is what I hope to implement by the end of this series. But lets start from step 1.

Here my goal is provide a help to all the aspirants who believe in blockchain and ethereum to be the answer to many atrocities of the world. When I got into ethereum I found it rather too vast and impossible to make sense out of. So, I went and did the one thing I thought would help… Code in it.

To my luck I found some articles that helped me setup my TestNet, but each of these had their own story to tell and none of them were written in detail (Please dont get me wrong, The authors of those articles were brilliant but so good that the new comers couldn’t comprehend their speed). None were giving me a holistic help on how to setup my Private Chain and steps were little vague plus there were NO videos in YouTube to help me out. So I spent considerable time reading and making it work.

Thus I thought why not help my fellow beginners, I will explain all the small terms that scratch your head. I will walk you through on how to create and deploy smart contracts, How to create a private chain in windows and linux step by step. Only thing I hope is that you have a basic idea on what a blockchain is. I will not go deep into how its underlying tech works cause you can get it anywhere, nevertheless I will write up a small WorkFlow.

Lets Get Started!!!

Important Terms:

  • > Mining : blockchain’s security comes from its cryptography and distributed nature. When a transaction is carried out it gives out a hash value so large that it is not easy to break. Mining is the process where a node (called miner) uses computation to ‘guess’ which value would satisfy the hash ie, a hash cannot be decrypted we need to give various input values and hope that the output hash is equal to the hash of the transaction. Thus a node that guesses and gets the hash first is said to have mined first and is rewarded with an ether in ethereum chains (or bitcoin in other blockchains).
  • > What is Ethereum: It is just a platform for you to build up different applications on blockchain
  • > What is Private Ethereum Chain: there is a Main Net of ethereum where all the major miners are and makor work is done! but when you have a firm or a private organisation (like your project team mates or simply you), then you dont need to spent ethers and deploy in main net. You can test your codes here in the private chain with fake ethers. This chain resides in your computer system so you may do as you please with it!
  • > What is Ethereum TestNet : Like the name suggests its a test area, just a different name for private chain explained above. (How to deploy? look down towards the end)
  • > What is a smart contract : it is a self / externally executing contract which when conditions are met performs its functions. We can code smart contracts to do all the functions that we want to implement in our chain.
  • > What is Solidity : It is the programming language you use to write smart contracts. It is similar to JavaScript. (I will tell you how to learn it at the end)
  • > What is Geth : It is command line tool that runs a node. It is implemented in Go Language. Basically, This is what we would use to create and maintain the blockchain in your PC. it has a JavaScript console in it.(Where to get it, How to use? All at the end).
  • >What is 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” — This is same as Geth but as mentioned above Eth has different customisation.
  • > What is Remix : Smart contracts written in solidity cannot be parsed and compiled by our Geth. So we use Remix, its an online tool that ‘compile’ solidity into a JSON format. so that we can execute it in Geth.
  • > What is Genesis Block : it is the first block in your chain and contains information such as gas limit etc.

Okay guys, these are some of the terms I wanted to discuss but if you want to know about a particular one then comment below and I’ll try to explain.

I will explain a work flow of how a blockchain would execute in part 2 of this series. Now I will continue on to talk about how to start with solidity and geth.

Solidity : How to learn?

The one way to learn a programming language is to code in it but since Solidity is new there arent many places to go so I suggest using this amazing interactive training made by Loom Networks. Learn Solidity by making a whole program called CryptoZombies . I learnt from there and its been great. Take notes while you code there and understand various syntax and working concepts explained there.

There maybe many videos to help you out but its always best to learn coding by using interactive environments like codeacademy. And Cryptozombies is like codeacademy for Solidity.

How to install Geth….?

You can use homebrew if you use mac : instructions.

I will be discussing on how to install Geth in windows. first install Geth from here

now after download install it in a folder called ‘Geth’ in F drive. (Anywhere is fine but its pretty easy to move your way through the terminal if you follow my directory).

  1. now open terminal and navigate to the folder F:\\Geth.
navigation

2. now its time to create a Genesis File: The first block on your chain.

code:

{

“config”: {

“chainId”: 987,

“homesteadBlock”: 0,

“eip155Block”: 0,

“eip158Block”: 0

},

“difficulty”: “0x400”,

“gasLimit”: “0x8000000”,

“alloc”: {}

}

Now save it as a JSON file: CustGenesis.json

I took this from one of the articles written by Abhishek Chakravarty. (great article)

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- difficulty:This value is used to control the Block generation time of a Blockchain. The higher the difficulty, the statistically more calculations a Miner must perform to discover a valid block.On our test network, we  will keep this value low to avoid waiting during tests, since the generation of a valid Block is required to execute a transaction on the Blockchain.- gasLimit:  this value specifies current chain-wide limit of 'Gas' expenditure per block. Gas is Ethereum's fuel that is spent during transactions. We will mark this value high enough in our case to avoid being limited during tests.- 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.

3. Now we write the genesis by into a chain we create.

type: geth — datadir = ./<folder_you_want_to_store_chain> init CustGenesis.json

Genesis block written

4. Congrats you created your blockchain!!! now lets kick start the chain.

type: geth — datadir <folder_where_chain_stored> — networkid <number between 100 to 1000> console

Starting blockchain and Javascript Console

Please Note: if it doesnt work:

— make sure you open the cmd (type cmd in cortana)

— make sure to type ‘console’ at the end and try changing the network id

— if nothing works turn off the cmd and re open and try again .(any more problems please comment below)

5. Almost done!! we have launched our blockchain.

now we must create accounts, mine some fake ethers. every user and smart contract of a blockchain should have a account address.

type: personal.newAccount()

give a passphrase (long password) when asked for.

new account

the value returned is your account’s address, copy and save it somewhere.

6. Now: lets mine some ethers into it..why???? Because to deploy a smart contract (which we will code in part 2) we must have ethers and since only we use this testNet we need to mine the ethers (but these do not have real world currency value because these are not made from mining the mainnet)

type: miner.start()

this will take some time and maybe a lot more than one may think.

miner starts

please note: miner may get stuck at the above stage but dont worry it will resume work after a while. shutdown extra processes in the background to fasten the process. or try exiting and restarting

mining after 12 minutes

Type: miner.stop() to stop mining after you gained enough ethers

How to use Geth and JS console: important commands

miner.start(): starts mining

miner.stop(): stops miner

personal.newAccount: new account creation

eth.getBalance(“address of the account”): to know how much ether

personal.unlockAccount(“account address”): unlocks an account

web3.eth.accounts[index]: to get the address of all the indexed account

web3: this will show you all objects and functions in the console

exit: quit the console

7. Check your account balance by typing eth.getBalance(“the address you saved when you created your first account”)

Conclusion

We have thus seen how to use geth, create a chain, start a chain etc. Now in the part 2 we will implement a small project (legal use case)using solidity.

--

--