Skip to content

Detailed notes on develop Blockchains, and for Blockchains.

Notifications You must be signed in to change notification settings

vitlug/jsmeetup-ethereum

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Notes from JavaScript Meetup @ Headout, Bangalore

Tools required:

  1. Ganache (Download): Tool to help you host your local blockchains. Think of it as IIS or XAMP/LAMP.
  2. Truffle: npm install truffle -g to install Truffle. It is used to automate development tasks like adding a block to your blockchain, migrating all changes, testing etc. Think of it as manage.py from Django.
  3. Web3 ( npm install web3 ): Used to talk to your Blockchain

How to

  1. git clone https://github.com/dappuniversity/social-network main
  2. cd main
  3. npm install
  4. Open up Ganache and start a Blockchain at the server mentioned in truffle-config.js
  5. npm start
  6. Install Metamask, a Crypto Wallet
  7. Go to Ganache, next to account there will be a key icon, click on it, copy the longer string
  8. Import account into Metamask
  9. Make a post
  10. Confirm Transaction.
  11. There you go.

Important Links

The Presentation

The best explaination of Blockchain. Ever.

The development introduction


Notes on Blockchain

Preface

Welcome, and thank you for your interest. I started Blockchain development after I had a talk with guys from Cosmos India at a Hackathon in my University. I used to not only not understand Blockchain, I had an aversion towards it. Just like how I hated IoT, AI and Machine Learning. They seemed to be overused as buzzwords at hackathons, and as someone who believed in solving problems through design, I used to be mad that I wasn't in on the circle! I mean, I was right to an extent, a lot of problems can be solved via smarter design, but there are certain technical limitations. Haptic feedback doesn't have the same effect as a smooth voice UX, which is essentially Natural Language Processing. I started writing this short book as a challenge. I didn't understand the deeper concepts before this book. Some would say I still don't. And that's okay, I have an issues page where every problem in this book will be tackled by experts. After I drop the book, I will not be extensively working on it anymore. If you feel something is wrong, first please try the issues page, and then mail me regarding it.

The approach I take with this book is backwards. I will start with explaining a few basic concepts, and then go on to use examples from the official documentations and explain them in detail. This is what is working for me. However, you don't have to worry, I will still explain things fairly well.

One question a lot of friends asked me is why is this free? The answer is quite simple. There are a lot of codes I sort of took from other people. Telusko, StackOverflow, Official Documentation, Savjee, dApp University, Blockgeeks, Data Dash, Investopedia and IOHL. I still put in the work of making my best explanation of the code and their theoretical material, but it would be dishonest to call it my own work, these people contributed when there was much less documentation available, these are the real geniuses.

I am putting my understanding and consolidating all these documentations in one place, the price if nothing but your email-ID. Yes, I will send you the new things I write, new podcast episodes I launch or if there is a new venture I am trying.

Now enough of this talk, let's get into development!

What you will learn from this eBook

Fundamental concepts of Blockchain and developing simple apps. I will also guide you in making your own small blockchain using vanilla Node JS. Apart from that we will be using Truffle, Ganache, Ethereum and Solidity to make a cool few dApps. To help you further, here is an index.

  • Develop your first blockchain
  • Implementing Proof of Work
  • Miner Rewards and transactions
  • Signing Transactions
  • Introduction to Solidity
  • Introduction to Ganache and Truffle
  • Introduction to Web3.JS
  • ToDo list dApp with React
  • References

A few more important things

You can send me a mail for any doubts or discrepancies at contact@abhinavmir.tech . You can reach me @abhinavmir on Twitter, Instagram, LinkedIn or Dev.to. I will also credit all the original sources in the references, I'd be really glad if you supported them.


Develop your first blockchain

Understand the basics.

Pre-requisites: Some rudimentary knowledge of OOPs in JavaScript.

We are using Node for this exercise. You could use Python or Go for the same, but I am using this since I am going to show you how to integrate this with your Node app. I take heavy inspiration from Savjee, I am using his series on blockchain as inspiration and putting my best understanding out there.

If you don't have Node, feel free to install it from your favorite package manager or the internet. A little bit about Node first: Node is JavaScript outside of your browser. You don't need script tags to run this JS, all you need to do is-

node filename.js

This is because some madlad took Chrome's V8 engine out of Chrome and wrapped it in C and served us Node. Talk about *Jugaad.*

The first thing you'll need to do is make a new directory and CD into that. Then you need to init an NPM package, and create an index.js file.

mkdir blockchain-example
cd blockchain-example
npm init -y
vi index.js

I am using Vim because I am terribly lazy, feel free to use any text editor of choice. Now let's talk a little bit about what a blockchain is before we delve further into code. The best explanation I have read is this article by IHODL.

To quote from their website-

Blockchain is a distributed database existing on multiple computers at the same time. It is constantly growing as new sets of recordings or blocks, are added to it.

Each block contains a timestamp and a link to the previous block, so they actually form a chain.

The database is not managed by any particular body; instead, everyone in the network gets a copy of the whole database.

All blocks are encrypted in a special way, so everyone can have access to all the information but only a user who owns a special cryptographic key is able to add a new record to a particular chain.

In addition, cryptography is used to guarantee synchronization of copies of the blockchain on each computer (or node) in the network.

Read more at their website.

https://www.researchgate.net/figure/Simple-example-of-blockchain-technology_fig4_325486515https://www.researchgate.net/figure/Simple-example-of-blockchain-technology_fig4_325486515

Look at the diagram above. All transactions are stored in Blocks, so every one knows what data is being transacted. Now before you are up in arms about security, this data is not open for everyone to see (It is in very few cases!). They are hashed, as we will see. You can only verify this transaction, verifying means that they can't be tampered with. For simplicity, these transactions are shown as currency here, but they could be any data.

Let's jump into code and from there explain a little here and there?

const sha256 = require('sha256'); 

class Block {  
constructor(index, timestamp, data, prevHash) {    
this.index = index;    
this.timestamp = timestamp;    
this.data = data;    
this.prevHash = prevHash;    
this.thisHash = sha256(      this.index + this.timestamp + this.data + this.prevHash    );  
}}

Do you understand what happened? We made a goddamn block. This is the fundamental logic behind every blockchain unicorn founder that got rich. Pay attention.

First, we import SHA256, it's an encryption algorithm. As I said, it's to encrypt the data.

The class BLOCK is a block template we will use for every block.

Each block will have five elements: index, timestamp, data, Previous Hash and Present Hash.

For CryptoCurrencies (esp the earlier ones like Bitcoin), the data is the address of the sender, receiver of bitcoin and amount of Bitcoin transacted.

Now that you know what One block looks like, I am sure you have a better understanding. Let's move on to what would we do about the first block, right? Since our first block has no Previous Hash!

We create something called the Genesis Block.

All of this will come under a class called blockChain.

const createGenesisBlock = () => new Block(0, Date.now(), 'Genesis Block', '0');

What it do? Simple. Create an instance of the class with 0 as the index, present date as date, 'Genesis Block' as data (You can keep this as anything as long as you remember what your initial block is named) and 0 as the previous hash.

We will need two functions: getLatestBlock() and calculateHash().

getLatestBlock() {
return this.chain[this.chain.length - 1];}


calculateHash() {
return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();}

getLatestBlock() fetches the latest block from the chain. Out of context it might be confusing, check the GitHub, you'll get a better understanding.

calculateHash() calculates the Hash, we have talked about this before.

Now let's create an addBlock function.

addBlock(*newBlock*) {

newBlock.previousHash = this.getLatestBlock().hash;

newBlock.hash = newBlock.calculateHash();

this.chain.push(newBlock);

}

This function takes in a parameter called newBlock, which is basically the block we will be adding. To use this function we will run it something like this:

let bkc_app=new blockChain();
bkc_app.addBlock(new Block(index, Date(), data))

Now addBlock will take that block we need to add as parameter, add a previousHash to it by getting the hash of the latestBlock, adds a present Hash to it by doing a calculateHash() on it and pushes it to the chain.

Yay, the simplest blockchain in the world is now ready!

Let's take it for a spin.

let bkc_app= new Blockchain();

data = {
'story':'Quick Brown Fox'
}

bkc_app.addBlock(new Block(1, Date(), data))

console.log(bkc_app, JSON.stringify(khbr, null, 4))

Here's the output:

Blockchain {
  chain: [
    Block {
      index: 0,
      timestamp: '01/01/2017',
      data: 'Genesis block',
      previousHash: '0',
      hash: ''
    },
    Block {
      index: 1,
      timestamp: 'Wed Dec 18 2019 23:50:02 GMT+0530 (India Standard Time)',
      data: [Object],
      previousHash: '',
      hash: '976c639156690751d29cd04b829e8f6448cf141d064e2149adf9d08253b6d759'
    }
  ]
} {
    "chain": [
        {
            "index": 0,
            "timestamp": "01/01/2017",
            "data": "Genesis block",
            "previousHash": "0",
            "hash": ""
        },
        {
            "index": 1,
            "timestamp": "Wed Dec 18 2019 23:50:02 GMT+0530 (India Standard Time)",
            "data": {
                "link": "Quick Brown Fox"
            },
            "previousHash": "",
            "hash": "976c639156690751d29cd04b829e8f6448cf141d064e2149adf9d08253b6d759"
        }
    ]
}

Get the Code here.


Proof of Work and how to implement it

What is Proof of Work (PoW) ?

A Proof of work is an easy to verify, but hard to generate data. Producing a PoW can be a random process with low probability of success. A lot of trial and error is required to a PoW. In this chapter I will go over what it is, why is it and how is it implemented. I will also go over Proof-Of-Stake, since it is a very important topic.

In classical computing, a PoW is a consensus mechanism. It helps deter DoS Attacks and any misuse of automation or bulk requests, such as spam. Before I explain PoW, Let me talk about Consensus in Computer Science. In Blockchain, we have tons of consensus algorithms. If a new block is to be added to the blockchain, Consensus Algorithms decides where will this block be added.

In a centralized system, every node accepts the center as an authority. In a decentralized network, there is no centralized authority. The mutual distrust is an obstacle to consensus. This is called the Byzantine Fault, which is an abstract analogy to this problem of distrust. In Byzantine's General problem: Assume there are some few generals separated by distance, and have to coordinate on a battle plan. So they must all share the battle plans, but there can be one malicious general (traitor!) who will mess with the battle plan.

In Bitcoins, every node is transparent in their transaction history to one another. To make a transaction, a node must undergo a process to produce a proof of work. PoW was initially developed as a protocol against email spam. To send an email, you must perform some computation on your system. This is different from CAPTCHA, which are easily solvable by humans. PoW problems aren't. Blockchains are append only, so we have to be sure that we are adding the correct block to the chain.

So in Bitcoin, it works something like this:

  • One node has to add a new block to get a fraction of bitcoin
  • But we don't want too many nodes doing this
  • So we want a node to do some work before we accept the block
  • We give it a hashed output and allow the computer to do the guessing
  • Once we do this, we expect all nodes to compete
  • They provide a solution which is super easy to verify in Polynomial time
  • This solution is passed to neighbor nodes, if verified it moves forward.
  • Kind of like a tree structure
  • The winner (miner) takes the block
  • More miners there are, more the difficulty of the problems

The Obvious Drawbacks

We are in struggle to save electricity, time and resources, and mining wastes a ton of these resources because

  1. The winner is paid in bitcoins but:
  2. The rest lost out on bitcoins AND used up computing space that could have been used for something better
  3. The computing power is used up on something intangible, trivial and useless.

Proof of Stake

Proof of Stake or PoS abstracts the idea of actually computing away. The process of reaching consensus remains the same, but the method changes. Unlike PoW, in PoS you "stake" your money in the network, and possibly bad behavior results in a loss of your stake.

Now let us implement it!

In our code we will add a new method called mineBlock.

mineBlock(difficulty)
    {
        while(this.hash.substring(0, difficulty)!= Array(difficulty+1).join("0"))
        {
            this.nonce++; //don't worry about this value now, I will explain
            this.hash=this.calculateHash();
        }

        console.log("Block Mined:"+this.hash);
    }

So what's happening here?

Bitcoin implements proof-of-work by requiring that the hash of a block starts with a specific number of zero's. This is also called the difficulty. Put that aside for now, and focus on how a block is added to the blockchain in real-life in BitCoins.

  • A block is just a transaction detail
  • There are certain X amount of BTC in existence
  • And Y number to be mined (Where Y>X)
  • That means a certain number of computational problems are provided so that you can solve them
  • Give a PoW and add to a block that "I mined and this is my PoW and this is what I claim"
  • Neighboring nodes verify your claims

[Rest Coming Soon]

About

Detailed notes on develop Blockchains, and for Blockchains.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published