A Comprehensive Guide to Ethereum Blockchain (Part 2)
So let’s start by creating your private blockchain where Geth client will interact with your local private blockchain, As mentioned earlier in Part 1, every blockchain starts with Genesis block which has no predecessor (these instructions assume a windows environment) and I divided this whole process into 14 points:
- Create a new folder on your desktop called Private Chain.
- Open command prompt in this folder and create a data directory for your chaindata by typing mkdir chaindata.
- Next, you need to create and save your genesis.json block in your Private Chain folder, as the genesis block will be used to initialize your private network and store data in the data directory chaindata.
- Open Notepad, copy & paste the code below into a new file called genesis.json and save this file in your Private Chain folder.
{
"coinbase" : "0x0000000000000000000000000000000000000001",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {},
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
every bock consists of these attributes. Here is some explanation of the important ones:
Gas
Given that Ethereum is a distributed platform, there must be a way to limit the resources available to a given smart contract otherwise it could starve the whole network’s computing power. Gas solves that issue by fixing a cost for every instruction executed in the EVM.
Difficulty
This value is used to control the Mining Complexity (Block generation time) of a Blockchain. The higher the difficulty, the statistically more calculations a Miner must perform to discover a valid block. On your test network, we will keep this value low to speed up the process during tests, since the generation of a valid Block is required to execute a transaction on the Blockchain.
This is where you can create your wallet and pre-fill it with fake ethers. It is sort of an opening balance in your wallet so do not necessarily need to mine before you can run your tests.
Config
The config block defines the settings for your custom chain and has certain attributes to create a private blockchain
5. Next initialize your private network in the chaindata by using the genesis.json file using the following command:
geth --nodiscover --networkid 1999 --datadir=./chaindata/ init ./genesis.json
6. Start Geth and connect to your own private chain. As you will see the geth.ipc endpoint socket connection will be opened and so will port 30303.
geth --datadir=./chaindata/
Whenever you start Geth an ipc endpoint will open, which is used to process connections from Geth to programs like MetaMask, Ethereum Wallet, Mist Browser, or for opening the Geth Javascript API console, here you will use Mist Browser which is a standalone desktop application.
Mist Browser
Mist is powerful in that it includes a Geth node that runs in the background upon launch; a connection to the Ethereum blockchain is made right away. But because you want to practice developing a smart contract, you are going to run a node on your private network.
You can download Mist browser from here
As geth client is already running and ipc is open to connect Mist browser, So lets open Mist browser, once you open Mist the geth.ipc endpoint will open; Mist will then make the connection to your private network. Make sure Mist is connected i.e. Private-Net is displayed in the upper right corner of the Mist launch window.
7. Now you can launch the application, it is connected to your private-chain, here you can create & deploy smart contracts, but before doing that you need to have your account. So Let’s create an account by clicking “Add Account” (synonymous with “wallet”). Choose “Create new account”, and enter a super safe password (even though this is an account on your private network and will not be connected to the actual Ethereum network, it is a good habit). Click “OK” to create your wallet.
8. To create Smart Contracts, Mist browser provides Remix IDE which you can access from the develop tab. Smart contracts are written in a language called Solidity, a java-script type language with the file extension “.sol”
9. Paste the following code in the file named HelloWorld.sol
pragma solidity ^0.4.16;
contract HelloWorld {
uint256 counter = 5; //state variable we assigned earlier
function add() public { //increases counter by 1
counter++;
}
function subtract() public { //decreases counter by 1
counter--;
}
function getCounter() public constant returns (uint256) {
return counter;
}
}
While calling functions on the Ethereum network will require a small amount of gas to be spent; no gas is required when returning a constant function.
10. Your code should be in Remix and “Auto compile” should be checked by default under the “Compile” tab. Make your way over to the “Run” tab. Default settings will have the environment set to JavaScript VM, an account will be selected, gas limit will be 3000000, and your gas price & value will be 0. Click on “Create” under your “Run” tab, you will notice amount of ether will be ticked town slightly after contract has been created, now It’s time to deploy.
11. Open Mist browser, make sure it is connected to your private chain at bottom left corner then move to Contracts tab → Deploy New Contract → paste your code from Remix into the source code box here.
12. To deploy contract, you need some ether in your wallet, you can mine for ether on your private network by running the miner. You will do this through the Geth JavaScript console. Open another instance of command prompt and navigate to private chain folder and execute:
geth attach
Once it’s attached now you can starting mining, execute:
miner.start();
After it you will see that the console returns “null” and mining has begun! Take a look at your wallet on Mist, you will see increments of 5 ether continuously added to your wallet, You can stop the miner anytime by executing:
13. Now you can deploy your contract, Contracts tab → Deploy → After you have entered your password, make your way back over to the “Wallets” tab. Scroll down to “Latest Transactions” and you will see the contract you just deployed. You will also notice that the contract shows 0 of 12 confirmations and is stuck in a perpetual state of “creating contract”. It’s mean you need to start mining again as you are not working in peer-to-peer network so others can mine, execute:
miner.start();
Once you start mining again you will see that the number of confirmations will start to tick up, eventually fully deploying your contract.
14. Now click on the contract name. From here you can do all sorts of things. You can transfer ether to the contract, copy the contract address, generate a QR code to be scanned, get the contract interface (ABI), and even execute contract functions.
As you can see your “Hello World” contract is displaying your getCounter() function with the counter state variable of 5. Make your way over to “select function” and select either “add” or “subtract” function. Once selected, hit “execute”. Do not forget, you are the only one on this private network so you need to confirm the transaction by running miner yourself!
Congratulations! you have done with creating and deploying your first Smart Contract on Private Blockchain, Stay connected for the last part of this article series, Create & Deploy Node.js app based on Ethereum BlockChain.
Please reach out to me in case of any query contact@farhanhabib.com and also don’t forget to applaud if you like this article. Thanks
Originally published at https://cfarhanhabib.medium.com on July 19, 2021.