Bitcoin Forum
April 26, 2024, 09:08:15 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Poll
Question: Would you like me to create similar guide on 'How to create cryptocurrency on the following blockchain?'
Yes
No

Pages: [1] 2 »  All
  Print  
Author Topic: How to create Blockchain? Build your own chain (Easy-Peasy Guide)  (Read 540 times)
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 17, 2020, 01:11:14 PM
Last edit: January 18, 2020, 07:07:38 AM by webtricks
Merited by RapTarX (3), pooya87 (2), hugeblack (2), DdmrDdmr (2), Heisenberg_Hunter (2), secone (2), xandry (1), ABCbits (1), sheenshane (1), tbct_mt2 (1), cryptoaddictchie (1), noorman0 (1), zasad@ (1), akirasendo17 (1), HBKMusiK (1), plvbob0070 (1), hendra147 (1), Yabes (1)
 #1

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.

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714122495
Hero Member
*
Offline Offline

Posts: 1714122495

View Profile Personal Message (Offline)

Ignore
1714122495
Reply with quote  #2

1714122495
Report to moderator
1714122495
Hero Member
*
Offline Offline

Posts: 1714122495

View Profile Personal Message (Offline)

Ignore
1714122495
Reply with quote  #2

1714122495
Report to moderator
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 17, 2020, 01:11:45 PM
Last edit: January 17, 2020, 01:33:57 PM by webtricks
Merited by hugeblack (2), DdmrDdmr (2), pooya87 (1), ABCbits (1), Heisenberg_Hunter (1), cozwaldo (1)
 #2

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.

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 17, 2020, 01:12:00 PM
Merited by hugeblack (2)
 #3

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.

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
tbct_mt2
Hero Member
*****
Offline Offline

Activity: 2310
Merit: 835



View Profile WWW
January 17, 2020, 01:20:50 PM
 #4

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.

.
 airbet 
██
██
██
██
██
██
██
██
██
██
██
██
██
 .

▄████▄▄▄██████▄
███████████████
███████████████
███████▀▀▀▀████
██████████████
▀███▀███████▄██
██████████▄███
██████████████
███████████████
███████████████
██████████████
█████▐████████
██████▀███████▀
▄███████████████▄
████████████████
█░██████████████
████████████████
████████████████
█████████████████
█████████████████
███████░█░███████
████████████████
█████████████████
██████████████░█
████████████████
▀███████████████▀
.
.
.
.
██▄▄▄
████████▄▄
██████▀▀████▄
██████▄░░████▄
██████████████
████████░░▀███▌
░████████▄▄████
██████████████▌
███░░░█████████
█████████░░░██▀
░░░███████████▀
██████░░░██▀
░░▀▀███▀

   
|.
....
██
██
██
██
██
██
██
██
██
██
██
██
██
.
 PLAY NOW 
hatshepsut93
Legendary
*
Offline Offline

Activity: 2954
Merit: 2145



View Profile
January 17, 2020, 03:21:39 PM
 #5

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.

.BEST.CHANGE..███████████████
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
███████████████
..BUY/ SELL CRYPTO..
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 17, 2020, 05:17:03 PM
Merited by hatshepsut93 (1)
 #6

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.

That's the problem with people at large. Everyone thinks from the producer's point of view. Nobody gives due importance to consumer point-of-view. I didn't create this thread so that person start deploying high-class applications using code above. Rather I wanted to give general understanding to everyone about how blockchain works under the hood.
The term 'blockchain' is nowadays is floating like some fairy tale on internet. Many websites such as gambling, exchanges and others claim that they are using blockchain for their operations. But that's not true always. But people (aka consumers) have no idea what implementations should they check on website to confirm whether such sites actually using blockchain or not. After reading the guide above, I can bet that people will have clearer idea about blockchain even if they don't understand code. That is what I wanna achieve through this thread. Period.

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
HBKMusiK
Full Member
***
Offline Offline

Activity: 693
Merit: 120


View Profile
January 17, 2020, 09:20:49 PM
Merited by webtricks (2)
 #7

I really like your tutorial, good job, it's really straightforward for newbies (and not only). You deserve a reward for your work, enjoy:

Note: I am not a designer, I am doing it just for fun.

webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 18, 2020, 07:08:58 AM
 #8

I really like your tutorial, good job, it's really straightforward for newbies (and not only). You deserve a reward for your work, enjoy:
XX---Header Image---XX
Note: I am not a designer, I am doing it just for fun.

Thanks. Nice design!
Added to OP alongwith credits. Smiley

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
coupable
Hero Member
*****
Offline Offline

Activity: 2338
Merit: 757


View Profile
January 18, 2020, 02:33:20 PM
 #9

Excellent work (y)
Even i consider myself as a veteran user, i find your guide so much helpful for me to correctly understand different elements in the blockchain .

As it looks so easy to start blockchain coding, what coding language can be used as different languages have always different structures .

I would also like to ask if this blockchain structure remains the same with all apps we used to know? I mean is it the same with Ethereum (using Solidity) or Beam (using MimbleWimble) or any other independent blockchain?

Am not a programmer, neither thinking to start coding with blockchain programming languages. But those questions came into my mind after reading this great easy topic .
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 19, 2020, 06:40:57 AM
 #10

As it looks so easy to start blockchain coding, what coding language can be used as different languages have always different structures .

You can use any programming language. It is possible to create blockchain with Python, Java and C++. In fact, Bitcoin Core is written in C++. But the easiest is Javascript (which is actually front-end language but could be used in back-end with Node.js).

I would also like to ask if this blockchain structure remains the same with all apps we used to know? I mean is it the same with Ethereum (using Solidity) or Beam (using MimbleWimble) or any other independent blockchain?

Am not a programmer, neither thinking to start coding with blockchain programming languages. But those questions came into my mind after reading this great easy topic .

Basic structure of all blockchains is same. You may notice that we created 5 properties of our Block class: timestamp, lastHash, hash, data and nonce. Among these, you will always find timestamp, lastHash, hash and nonce in any blockchain. Since, Bitcoin and Ethereum store transactional data, therefore, data field in these blockchains is detailed. To save transactions in the block, such blockchains use many other properties like in case of Bitcoin we have: merkle root, version, nBits, size, weight, etc.

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
secone
Hero Member
*****
Offline Offline

Activity: 700
Merit: 501


View Profile
January 19, 2020, 08:28:19 AM
Merited by webtricks (1)
 #11

+2
that's a great tutorial, simple and easy to understand, next time or next tutorial, i recomended to use bitcoinlib.js , to create address , raw transaction, sing message etc.
im use bitcoinlib.js + WVvalidator js for my last project i combine with vue js , its simple to newbie than native js.
LbtalkL
Full Member
***
Offline Offline

Activity: 1176
Merit: 162


View Profile
January 19, 2020, 09:32:45 AM
 #12

Thanks for the guide this gives me an idea on how to create one, as crypto enthusiast we need to be knowlegdeable in this kind of stuff. But I am not very good with this stuff it is too complex for me, this programming, etc. But this is really a good tutorial for a developer aspirants.
hendra147
Hero Member
*****
Offline Offline

Activity: 644
Merit: 509


View Profile
January 19, 2020, 09:46:48 AM
 #13

can you share the source code?
i just copy all the code above in 1 files and get this errror


Code:
/home/xx/Desktop/javascript challenge/blockchain.js:10
static createGenesis() {
       ^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 19, 2020, 11:23:53 AM
Last edit: January 19, 2020, 11:37:34 AM by webtricks
 #14

can you share the source code?
i just copy all the code above in 1 files and get this errror


Code:
/home/xx/Desktop/javascript challenge/blockchain.js:10
static createGenesis() {
       ^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11



Yeah, sure.
I have uploaded complete code in order here:
https://webtricks.website/blockchainCode.html

Do tell me if you get any other error. I will be happy to clear it. Smiley

+2
that's a great tutorial, simple and easy to understand, next time or next tutorial, i recomended to use bitcoinlib.js , to create address , raw transaction, sing message etc.
im use bitcoinlib.js + WVvalidator js for my last project i combine with vue js , its simple to newbie than native js.

Great. I am currently using some other library to create keys and signatures. I will try bitcoinlib.js and if I find it relatively easier than will use it in my tutorial.
Thanks for suggestion.

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
hendra147
Hero Member
*****
Offline Offline

Activity: 644
Merit: 509


View Profile
January 19, 2020, 03:13:38 PM
Merited by webtricks (1)
 #15



Yeah, sure.
I have uploaded complete code in order here:
https://webtricks.website/blockchainCode.html

Do tell me if you get any other error. I will be happy to clear it. Smiley

working perfectly Smiley
result :
Code:
 xxx@ubuntu  ~/Desktop/javascript challenge  node blockchain
[
  Block {
    timestamp: '17/01/2020',
    lastHash: 'dummy-last-hash',
    hash: 'dummy-hash',
    data: 'data in genesis block',
    nonce: 0
  },
  Block {
    timestamp: 1579446587014,
    lastHash: 'dummy-hash',
    hash: '0000ef84be4441c614dd4c19a15b8470ac3b2bcbf36a2300f105f56906962c62',
    data: 'This dummy data is saved in the second block of the webbyChain',
    nonce: 113386
  },
  Block {
    timestamp: 1579446587835,
    lastHash: '0000ef84be4441c614dd4c19a15b8470ac3b2bcbf36a2300f105f56906962c62',
    hash: '00009f5354bbfe4ee07e918e708a641a811f8dfe2d1a0ce74e9f9eef434309f1',
    data: 'This immutable data is saved in the third block',
    nonce: 91432
  },
  Block {
    timestamp: 1579446588321,
    lastHash: '00009f5354bbfe4ee07e918e708a641a811f8dfe2d1a0ce74e9f9eef434309f1',
    hash: '0000a44bd43f873587da1cb34c467dfd9a94916017dfc5878d67ebed04928df1',
    data: 'Adding top secret in the fourth block',
    nonce: 55393
  }
]

i will read detail per part maybe 10times  Cheesy, i can't fully understand wiwth read twice, maybe i will repost in local indonesian section due to make people in mycountry learning bitcoin by coding
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 19, 2020, 04:33:07 PM
 #16

~~
~~
i will read detail per part maybe 10times  Cheesy, i can't fully understand wiwth read twice, maybe i will repost in local indonesian section due to make people in mycountry learning bitcoin by coding

Great!
Let me give you an additional feature for the above chain. This one line will convert timestamp from milliseconds to proper date and timing:
Code:
        timestamp = new Date(timestamp).toLocaleString();

Add this line in the createBlock() function at the place mentioned below:
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));
       
        //add this code here
        timestamp = new Date(timestamp).toLocaleString();

        return new this(timestamp, lastHash, hash, data, nonce);
    }

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 29, 2020, 03:53:21 PM
 #17

final bump

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
nakamura12
Hero Member
*****
Offline Offline

Activity: 2254
Merit: 669


Bitcoin Casino Est. 2013


View Profile
January 29, 2020, 08:32:12 PM
 #18

Thanks for the guide this gives me an idea on how to create one, as crypto enthusiast we need to be knowlegdeable in this kind of stuff. But I am not very good with this stuff it is too complex for me, this programming, etc. But this is really a good tutorial for a developer aspirants.
It is not just a guide but also helping newbies understand how blockchain works by knowing how blockchain is created. It would be very helpful for both developers and also for newbies who wants to learn and understand how blockchain is and also how to create one.

███▄▀██▄▄
░░▄████▄▀████ ▄▄▄
░░████▄▄▄▄░░█▀▀
███ ██████▄▄▀█▌
░▄░░███▀████
░▐█░░███░██▄▄
░░▄▀░████▄▄▄▀█
░█░▄███▀████ ▐█
▀▄▄███▀▄██▄
░░▄██▌░░██▀
░▐█▀████ ▀██
░░█▌██████ ▀▀██▄
░░▀███
▄▄██▀▄███
▄▄▄████▀▄████▄░░
▀▀█░░▄▄▄▄████░░
▐█▀▄▄█████████
████▀███░░▄░
▄▄██░███░░█▌░
█▀▄▄▄████░▀▄░░
█▌████▀███▄░█░
▄██▄▀███▄▄▀
▀██░░▐██▄░░
██▀████▀█▌░
▄██▀▀██████▐█░░
███▀░░
pooya87
Legendary
*
Offline Offline

Activity: 3430
Merit: 10504



View Profile
January 30, 2020, 06:26:02 AM
Merited by webtricks (4)
 #19

good job, just some points for the sake of completion:
part 1(a):
a block doesn't have to store its own hash (or get it in constructor) instead it takes everything else (timestamp, nonce,...) and compute its own hash.

additionally this part is skipping an important matter: merkle root
a block is a container that is supposed to store transactions. in order to make sure transactions inside that block can't be changed, a hash that is generated from all transactions is also used and stored in creation of a block header. but instead of hashing all at once we create a merkle tree and only store the root hash in header.
you could skip timestamp like you skipped block version but merkle root is important.

part 1(c):
you may want to use a nested loop, the outer one changing time and  the inner one changing nonce.
additionally you may want to use another hash algorithm instead of SHA for higher speed since you don't want security in here. something like MD5.
an important distinction
PoW has nothing to do with "number of starting zeros", it is all about comparing integers.
first of all you have to flip the endianness of the digest meaning reversing the hash bytes:
block hash bytes: { xx, xx, 00, 00 }
reverse block hash bytes: { 00, 00, xx, xx }
then convert it to an integer and compare it with the target (another integer). example (all in big-endian):
target = 260 = 0x000104 (3 bytes)
hash1 = 261 = 0x000105              -> has 1 starting zero but 261 > 260 => reject
hash2 = 259 = 0x000103              -> has 1 starting zero and 259 < 260 => accept

Part 1(d)
just a reminder: bitcoin's hash is SHA256 of SHA256 (in other words double hash) but it doesn't matter here since it is just for learning i just wanted to remind and as i said you can use another faster hash like MD5.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728



View Profile
January 30, 2020, 08:15:58 AM
 #20

<--snip-->

Nice additions, especially the Proof of Work part. I wanted to include the proper Proof of Work implementation in the guide but things were becoming very complicated. However, you explained it very easily. Kudos to you.  Wink

But as I said in post #3 that this is just a dummy version of blockchain and in no way related to Bitcoin. Also we are not saving transactions in blockchain for now so merkle root is irrelevant.

I'm thinking to create series of threads in Beginners & Help related to technical aspects of Bitcoin. It will include everything: how addresses are created, what is elliptic curve, etc. Being from financial background, I self-learnt everything about bitcoin from web sources. Although, there are valuable resources already available like bitcoin.it but things are still complicated for newcomers. I have created alternative to all codes in JavaScript which is lot easier than other programming languages. What do you say, should I go ahead?

/]..JAMBLER.io../]Create Your Bitcoin Mixing
Business Now for   F R E E 
▄█████████████████████████████
█████████████████████████
████▀████████████████████
███▀█████▄█▀███▀▀▀██████
██▀█████▄█▄██████████████
██▄▄████▀▄▄▄▀▀▀▀▀▄▄██████
█████▄▄▄██████████▀▄████
█████▀▄█▄██████▀█▄█████
███████▀▄█▀█▄██▀█▄███████
█████████▄█▀▄█▀▄█████████
█████████████████████████
█████████████████████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/our-partners.php]      OUR      
PARTNERS

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
▄█████████████████████████████
████████▀▀█████▀▀████████
█████▀█████████████▀█████
████████████████████████
███████████████▄█████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████▀█████████
████████████████████████
█████▄█████████████▄█████
████████▄▄█████▄▄████████
▀█████████████████████████████
█████████████████████████████████████████████████
.
/become-seller.php]   INVEST   
BITCOIN

.
█████████████████████████████████████████████████
████▄
██
██
██
██
██
██
██
██
██
██
██
████▀
Pages: [1] 2 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!