Pages:
Author

Topic: How to create Blockchain? Build your own chain (Easy-Peasy Guide) - page 2. (Read 596 times)

legendary
Activity: 3024
Merit: 2148
It's a good example that explains how blockchain works by building a baby blockchain in one of the simplest and most popular programming languages, but it's important to understand that the presented code isn't practical, this blockchain lacks file system support, it doesn't have networking, it doesn't have transactions, scripts and so on. You won't become a blockchain specialist by reading articles like this, you'll have to dig much deeper.
hero member
Activity: 2366
Merit: 838
You made excellent topic, despite of I don't understand what you write (codes). AFAIK, building a blockchain nowadays is not an extremely difficult task, because blockchain-based projects are open-sourced. Then, from one famous coins, people can take advantages of open-sourced code and make clone coins. This is reason behind the scene which caused many scam projects in 2017 and 2018. Scammers cloned and made new altcoins, that mostly die after a few months.

Fortunately, it is the side of scammers who always want to scam inexperienced people. Vitalik is not responsible for clone coins from ETH.
legendary
Activity: 1918
Merit: 1728
TESTING OUR CODE

Time for the test. Let’s see what we have created. After the end of Blockchain class, write this testing code:

Code:
const webbyChain = new Blockchain();
webbyChain.addBlock(‘This dummy data is saved in the second block of the webbyChain’);
webbyChain.addBlock(‘This immutable data is saved in the third block’);
webbyChain.addBlock(‘Adding top secret in the fourth block’);
console.log(webbyChain.blocks);

Outstanding! We just created instance of our Blockchain by the name of webbyChain. You can give any name to your blockchain. Next we added 3 blocks in the chain via addBlock function. Finally, we console logging the blocks array of our webbyChain so we can see the output of our blockchain in the terminal.

Once the above steps are done, open the terminal. If you are using Visual Studio Code, it will open automatically in lower area. If not press Ctrl+Shift+`. Alternatively, you can use command prompt or any other terminal. Next open the location of your file. For example, if your file is on Desktop, make sure Desktop is open in your terminal. Then run the following command:

     node index.js (the name of your file can be different)

It will take some time to create blocks as Proof of Work algorithm making sure that finding hash takes time. Once all three blocks are mined, they will be displayed alongside genesis block in your terminal.

That brings us to the end of this guide. Feel free to ask any question related to the guide. I have missed many things like proper Proof of Work algorithm working, automatic difficulty adjustment, validation of chain, hashes, etc. But the guide is already too long so let that be for next time. Cheesy
Also vote if you want me to create guide on how to create cryptocurrency over our blockchain.
legendary
Activity: 1918
Merit: 1728
PART 2: BLOCKCHAIN

Part 2(a): Blockchain Class Constructor

Let’s discuss the architecture of our blockchain first. The structure of our blockchain will be Array. It can be either Array or Object or any other type too but Array structure gives us benefit of using various Array methods offered by Javascript. Ok! So, in our constructor function for Blockchain, we will create empty array and place the first item of the array to be genesis block. Next, we will create a function which will create new block using Block class and add that into our chain’s Array. Neat, right? Let’s move to the code for our Part 2(a), write this code after the end of Block class:

Code:
class Blockchain {
    constructor() {
        this.blocks = [Block.createGenesis()];
    }
}

Cool! Now time for explanation. First of all, we created skeleton of our blockchain via Blockchain class. Next, we created constructor function for Blockchain class. Note that we don’t have any inputs for this constructor function because we don’t need any. Next, the first and the only bone of Blockchain skeleton is this.blocks which is an array. New blocks when created will be added to this array automatically. Then, in array we input the Block.createGenesis() function. Remember static createGenesis() function from Part: 1? This is the same function. But since we are using it outside Block class, we have to attach Block before the function name in order to tell Javascript the location of our genesis function.

Putting this function inside this.blocks array will make the first item of the array equals to the return value of createGenesis() function which is hard-coded value, remember? Great! So, we have achieved two things in Part 2(a), first we created array where all blocks will be pushed when created. Secondly, we pushed genesis block as first item of the array. This brings us to the final step of our blockchain, a function to add blocks. Let’s achieve that in Part 2(b).

Part 2(b): Add Blocks to the Blockchain

Let’s start with code as we are doing so far, write this code after constructor function:
Code:
    addBlock(data) {
        const createdBlock = Block.createBlock(this.blocks[this.blocks.length - 1], data);
        this.blocks.push(createdBlock);
    }

Done! That was the last nail in the coffin. Now let’s understand the code. The only input this function will take is ‘data’. This data will be the information that the user will store in the blockchain. Next comes ‘const createdBlock’. This is the block instance that addBlock function created by calling createBlock functions from Block class. Now remember createBlock function takes 2 inputs: previousBlock and data. Data will be passed from addBlock function’s input to createBlock whereas previousBlock will be extracted from our this.blocks array. I have wrote this code for previousBlock: ‘this.blocks[this.blocks.length - 1]’. Now try to understand the code, this.blocks is an array, all arrays have a length which is equal to the number of items in the array. For example, the length of our this.blocks array at the moment is 1 as it only contains genesis block for now. Therefore this.blocks.length will be equal to 1.

Now every item in the array have a key. First item will have the key of 0, second item will have 1 as the key and so on. Now once again consider code: ‘this.blocks[this.blocks.length – 1]’ and put the value of this.blocks.length, it will become: this.blocks[1-1] or this.blocks[0] i.e. genesis block. So, this line will always return the last item available in the array and pass that to createBlock function.

I hope it is clear, if not you can always ask for additional explanation here in this thread or in PM. Ok, moving forward. The final line of our code is this.blocks.push(createdBlock). This line is actually pushing (which means adding) the newly created block into this.blocks array.
Nice! With this, we have completed the coding part for the blockchain.

You can always visit: https://webtricks.website/blockchainCode.html and see the whole code in order.
legendary
Activity: 1918
Merit: 1728
Header designed by: HBKMusiK                               


You may have read lots of theoretical posts on how cryptocurrencies work, how blockchain works and several other related concepts like Proof of Work, sha256. etc. But such theoretical posts can only give you basic idea about working and lots of things still remain mystery to you. So, the goal of this thread is to give you firsthand experience about how Blockchain works using real coding.

Before I start the guide, I would like to clear few things:

First, this thread is for learning purpose only. The code below is not production ready and have several vulnerabilities. If you plan to use it for production, make sure to contact me first and I will tell you the necessary additions you have to make in order to make it ready for production.
Second, this guide will use Javascript/Node.JS environment so if you have basic idea about how Javascript works then this guide is cake-walk for you. If not, even then you can learn so much as I tried to remain as descriptive as possible. But if you get stuck somewhere, just PM me or post here and I will solve every doubt I receive. Enough said, let's start:



PART 1: BLOCK

Part 1(a): Constructor Class

Ok! So block as we all know is fundamental unit of Blockchain. Series of interconnected blocks make blockchain. Let me give you an example: Suppose 10 children are playing in a park. Each one of them is holding the hand of another child thus forming the structure of human chain, children on both extreme ends will be holding the hand of one other child while rest 8 will be holding the hands of 2 children, one on each side. Blockchain works on the same concept. The first block of the chain (known as genesis block) will be holding the hand of second block only while the most recent block will be holding the hand of last second block only. All the other blocks will be holding the hands of blocks previous and next to them.

A small pictorial representation of what I just said:


(I am bad artist, I know. Cheesy)

Ok! So now one thing is clear that every block must hold the hand of other block to form chain. In order to ensure that, blockchain uses the concept of hash and previous hash (I like to call it last hash). Every block has unique hash which is generated through SHA256 algorithm (will explain that in detail later). This hash act as the hand of every block. Second block will save hash of genesis block, third block will save hash of second block and so on...This brings us to the basic design of block, a block should have hash, lastHash (hash of previous block), data (something unique stored in block), timestamp (the timing of the creation of block) and nonce (will explain what nonce is with PoW). Before starting with the code, please make sure you have following things ready:

(i) install Node.js on your system
(ii) download any code editor (I would suggest Visual Studio Code)
(iii) create a new file with any name but with .js extension, example: app.js or index.js
(iv) open the file in visual studio code

Here comes the first snippet of code:
Code:
class Block {
    constructor(timestamp, lastHash, hash, data, nonce) {
        this.timestamp = timestamp;
        this.lastHash = lastHash;
        this.hash = hash;
        this.data = data;
        this.nonce = nonce;
    }
}

Boom! Here is the code for Part: 1(a). Now let me explain what we just did. Starting with the word 'class'. Class is actually a skeleton. We have created skeleton of name 'Block'. Like every human have same skeleton but different muscles and organs. Similarly, every block in the chain will have this Block skeleton. Timestamp, lastHash, hash, data and nonce are the bones. Every block must have these bones in order to form a skeleton. Moving forward, word 'constructor' refers to the function which will take the bones as input and create skeleton out of that. So we are actually inputting the value of timestamp, lastHash, hash, data and nonce into constructor function which in turn is setting the value of timestamp, lastHash, hash, data and nonce of Block’s instance equivalent to that.
Great? Let's move forward to Part: 1(b).


Part 1(b): Genesis Block

Here goes the code for Part: 1(b), this code will come after constructor function :
Code:
   static createGenesis() {
        return new this("17/01/2020", "dummy-last-hash", "dummy-hash", "data in genesis block", 0)
    }

Great! So, let me tell you what we just did, we have created a static function in Block class which will create a genesis block for us. You may be noticing that we have created a hard-coded value for this block (anything inside '' " is string or hard-coded value, not code). We are doing this way because genesis block cannot have lastHash, so it is not viable to create hash for it, which makes it risky to store any data in genesis block. So, it is better if we define the properties of genesis block on our own and don't use it for any storage.

Moving forward, let me describe what above code is doing. Starting with the word 'static'. We can create two types of functions in class, normal functions and static functions. Normal functions could be used with every instance of class (for example, every block created with Block class can use normal function) but it is not possible to use static function with every instance. In our chain, we only want one genesis block so it would be bad if we create normal function for this. Therefore, static function will make sure that createGenesis() function can be called only once in blockchain.
 
You maybe noticing 'return' in the code above. Return actually means return (smart, ehh). It makes sure that whenever this function is called, the function returns the value of genesis block. 'new' refers to the instance of Block class. Whenever, we create instance of any class, we have to use new keyword. ‘this’ refers to Block class’s constructor. If we are using constructor within the class then we have to refer it with ‘this’.

Enough said, let’s move forward and create the most important function i.e. createBlock.

Part 1(c): Create Block function and Proof of Work
Code will go after createGenesis() function:
Code:
   static createBlock(previousBlock, data) {
        let hash, timestamp, nonce=0;
        const lastHash = previousBlock.hash;
        do {
            timestamp = Date.now();
            nonce++;
            hash = hashGenerator(timestamp, lastHash, data, nonce);
        } while (hash.substr(0,4) !== ‘0’.repeat(4));
        return new this(timestamp, lastHash, hash, data, nonce);
    }

Neat! Now time to understand what we did above. The function takes two inputs: previousBlock which is the block previous to the one we are creating and data i.e. the actual data we want to save in the block. Then, we are letting the starting value of hash = nothing, timestamp = nothing and nonce = 0. In Javascript ‘let’ and ‘const’ are two ways of defining variables. We refer variables that don’t change their value with ‘const’ and those which change their value by ‘let’. Next, we extracted the value of lastHash from previous Block which is equal to previousBlock’s hash.

Cool! Next comes the mighty concept of Proof of Work. We have tried to achieve it via do/while loop. ‘While’ part of the do/while loop takes condition and the loop keeps running the code in ‘do’ part until the condition in while statement fulfils. So the condition we mentioned in while statement is: hash.substr(0, 4) !== ‘0’.repeat(4). Now let’s break this statement. hash.substr(0,4) means first 4 characters of hash starting from 0 i.e. first character then second, third and fourth. ‘0’.repeat(4) means four zeros or ‘0000’. So we are actually stating that keep running the loop as far as first four characters of the hash are not 0. Once the first 4 characters of hash becomes 0, the loop will break and the resultant value will become the hash of the block. This is not exactly how proof of system works but basic idea is same. We are finding hash with four zeros in the starting like 0000vddvd5vd4dv5dvdXXXXXXXXXX. If you want to increase the difficulty of Proof of Work system, increase the number of zeros to 5 or more and blocks will be mined slower. If you want to lower the difficulty then reduce the number of zeros to 3 or lower and blocks will be mined faster.

Now coming to code inside ‘do’ statement. First is timestamp which we have taken equals to Data.now() which is javascript function to generate current date. Next is nonce. Nonce is the number which keeps on incrementing by 1 at every loop so that the value of hash keeps changing, if nonce remain stagnant then it is not possible to generate new hash at every loop. The final thing in the code is hashGenerator which intakes the value of timestamp, lastHash, data and nonce and generate hash by combining all the 4 values as a single string using sha256 algorithm. We will write hashGenerator function in next part. Let’s move to it.

Part 1(d): SHA256
Let’s write the code first, this will go at the top of the file before Block class:
Code:
const crypto = require(‘crypto’);
const hashGenerator = (...inputs) => {
    const hash = crypto.createHash(‘sha256’);
    hash.update(inputs.map(item => JSON.stringify(item)).join(‘’));
    return hash.digest(‘hex’);
}

Lovely! Time for explanation. Crypto is in-built library of Node.js. So, we have simply called it into our file by requiring it. Next comes hashGenerator function. First, we are taking inputs i.e. if you remember from Part: 1(c) are timestamp, lastHash, data and nonce. Next you must be noticing three dots in front of inputs. Those dots aren’t by mistake, these dots convert all 4 inputs into Array like this: [timestamp, lastHash, data, nonce]. Bingo! Now let’s enter into the hashGenerator function. In first line we defined the value of hash equals to createHash(‘sha256’) function of crypto library. Then we are inputting each item of inputs Array through update method. We first mapping over inputs array which means looping over item of array, converting every item to string through JSON.stringify method and then joining everything as single string. Finally, we are returning the value of function through digest method but first converting generated value from line two to hex.

If you are having hard time understanding hashGenerator function then don’t worry, it’s because we have used the native syntax of crypto library and which is different from generic Javascript syntaxes.

This brings us to the end of first part of our two parts guide on creating Blockchain. We have successfully created Block class. Next we will create Blockchain class and add blocks to our blockchain.
Pages:
Jump to: