top of page

Getting Started with Blockchain Development Part-1

Updated: Nov 22, 2020

Blockchain technology is taking the world by storm. It's high time to give up on indifference and catch hold of the FinTech revolution. It's time to abandon philosophical blockchain meanings and unactionable pieces of content on blockchain technology. Instead, let's get straight into hands-on with Blockchain info and real functioning today!


Part-1: Setting up a Private Ethereum Blockchain on Your System


First, let's get acquainted with the basic theory about Blockchain applications!


What exactly is Blockchain?


You (a “node”) have a file of transactions on your computer (a “ledger”). Two government accountants (let’s call them “miners”) have the same file on theirs (so it’s “distributed”). As you make a transaction, your computer sends an e-mail to each accountant to inform them.


Each accountant rushes to be the first to check whether you can afford it (and be paid their salary “Bitcoins”). The first to check and validate hits “REPLY ALL”, attaching their logic for verifying the transaction (“Proof of Work”). If the other accountant agrees, everyone updates their file…


This concept is enabled by “Blockchain” technology.


For understanding the theoretical concept completely, refer to the article here.


What is Ethereum?


Ethereum is a global, open-source platform for decentralized applications.


On Ethereum, you can write code that controls digital value, runs exactly as programmed, and is accessible anywhere in the world.


In the simplest form, an Ethereum node is any device that is running the Ethereum protocol (blockchain).


By running an Ethereum node we can connect to other nodes in the network, have direct access to the blockchain, and even do things like mine blocks, send transactions, and deploy smart contracts.


To make sure all the nodes in the network have the same copy of the data and to ensure no invalid data gets written to this database, Ethereum uses an algorithm called Proof of Work.


So basically, the blockchain stores your data, stores the code, and also runs the code in the EVM (Ethereum Virtual Machine).


In the Ethereum world, you write the application code (called a contract) in a language called Solidity. You then use the solidity compiler to compile it to Ethereum Byte Code and then deploy that byte code to the blockchain.


It has the advantage of not relying on a single central server hosted which might disappear tomorrow.


What is a dApp?


dApps or Decentralized Applications are a piece of software that communicates with the blockchain, which manages the state of all network actors.


The interface of the decentralized applications does not look any different than any website or mobile app today.


The smart contract represents the core logic of a decentralized application. Smart contracts are integral building blocks of blockchains, that process information from external sensors or events and help the blockchain manage the state of all network actors.


To build web-based dApps, Ethereum comes with a handy javascript library called web3.js which connects to your blockchain node. So, you can just include this library in your famous js framework like reactjs, angularjs, etc. and start building.


As soon as you start using a dApp, you get inbuilt bank accounts. These bank accounts are called wallets where you store money (Ether - the currency used in the Ethereum ecosystem) and transact.


Before you can use a dApp, you have to download the entire blockchain.


What is Geth?


Geth or Go Ethereum is one of the three original implementations (along with C++ and Python) of the Ethereum protocol. It is written in Go, fully open source, and licensed under the GNU LGPL v3.


What is the Genesis file?


The genesis block is the start of the blockchain, and the genesis.json is the file that defines it. It is like the “settings” for your blockchain. For example, the chain configuration, level of difficulty to mine blocks, etc.


Now that we are through with the basic theory, let's get our hands dirty by setting up a private Ethereum blockchain on your system!


1. First Download and install Geth from here: https://geth.ethereum.org/downloads/



3. Create a folder "ethereum". Then create a folder "DataDirectory" in it. Paste "genesis.json" in the "ethereum" folder.


4. Type in the following command in your terminal(note that this is a single command) to setup your private Ethereum blockchain:


(replace C:\Users\wadhw\Projects\Blockchain\ethereum\DataDirectory with the location of your data directory folder and C:\Users\wadhw\Projects\Blockchain\ethereum\genesis.json with the location of your genesis file)


geth --rpc --rpcport "8085" --datadir C:\Users\wadhw\Projects\Blockchain\ethereum\DataDirectory init C:\Users\wadhw\Projects\Blockchain\ethereum\genesis.json



5. Type in the following command in your terminal(note that this is a single command) to setup your first node on your private Ethereum blockchain:


(replace C:\Users\wadhw\Projects\Blockchain\ethereum\DataDirectory with the location of your data directory folder)


geth --rpc --rpcport "8085" --datadir C:\Users\wadhw\Projects\Blockchain\ethereum\DataDirectory --networkid 123 --nodiscover



6. In a New Terminal, type in the following command(note that this is a single command) to access the console of your first node:


geth attach ipc:\\.\pipe\geth.ipc



7. Type in the following command in your console (note that this is a single command) to set up your personal new account on your first node:


personal.newAccount()





8. Enter a passphrase and save both the passphrase and account id.


For example:

not1234

"0x9959f470704625a64c89947a6bd4bd6a79808b8c"


9. Type in the following command in your console (note that this is a single command) to get the balance of your account:


(replace "0x9959f470704625a64c89947a6bd4bd6a79808b8c" with your account id)


eth.getBalance("0x9959f470704625a64c89947a6bd4bd6a79808b8c")



9. Type in the following command in your console (note that this is a single command) to start mining your account:


miner.start()




10. After a while, type in the following command in your console (note that this is a single command) to stop mining your account:


miner.stop()




Now let's add a new node to your private Ethereum blockchain!


11. Create a folder "DataDirectory2" in the "ethereum" folder.


12. Type in the following command in your terminal(note that these are two different commands) to set up your second node on the private Ethereum blockchain and access it's console:


(replace C:\Users\wadhw\Projects\Blockchain\ethereum\DataDirectory2 with the location of your second data directory folder and C:\Users\wadhw\Projects\Blockchain\ethereum\genesis.json with the location of your genesis file)


geth --rpc --rpcport "8085" --datadir C:\Users\wadhw\Projects\Blockchain\ethereum\DataDirectory2 init C:\Users\wadhw\Projects\Blockchain\ethereum\genesis.json



geth --datadir C:\Users\wadhw\Projects\Blockchain\ethereum\DataDirectory2 --networkid 123 --nodiscover --port 30304 --ipcdisable console



13. In the console of the second node, type in the following command:


run admin.nodeInfo.enode



14. Copy the result


"enode://45da60db8e62ef9f6cea6dd4593dfcb341415d55222f589b7bb6360da1d6e4924f87f146e10d87e0b7b92435fbecba14d52d3d8502e501f0341ef9f27a45c387@127.0.0.1:30304?discport=0"


15. Type in the following command in the console of the first node:


(replace the enode result below with your result)


admin.addPeer("enode://45da60db8e62ef9f6cea6dd4593dfcb341415d55222f589b7bb6360da1d6e4924f87f146e10d87e0b7b92435fbecba14d52d3d8502e501f0341ef9f27a45c387@127.0.0.1:30304?discport=0")



16. To confirm run admin.peers on the first console and you should see the details of the node you just added



17. Now if you start mining on the first node by running miner.start(), you will see the block number increase on the second node.



18. Stop mining on the first node by running miner.stop().


19. Type in the following command in the consoles to exit:


exit


We will learn more about Ethereum mining and smart contracts in the later parts of this blog series, so don't worry right now if you don't understand why the block number increased on the second node. You are not expected to understand it right now. Believe me, if you were able to get this much done, it's a huge achievement for now!


If you have any doubts, feel free to ask in the comment section and stay tuned for the next part.


Happy Learning!


References:


Comments


Post: Blog2_Post
bottom of page