Basic Data Structure for Blockchain and how to write

Blockchain

These days, crypto is becoming a big thing and everybody literally everybody is discussing it but there are so few people who actually know what is working behind these cryptocurrencies and what is actually going on in the new era of technology.

I am not going to write about what is going on in the crypto market and which coin is doing what rather I am going to discuss what works behind them and does it is worth a try?

What is behind crypto currencies and What is actually blockchain?

Crypto currencies are nothing but a glimse of what a blockchain network can actually do. These days everyone is looking to buy crypto currency and some of them are looking for ways to create their own crypto currency but the question is WHAT IS A CRYPTO CURRENCY? it is a virtual currency which is secure and token base, which resides on their own blockchain.

What is actually blockchain? it is a special database which is stored in form of blocks which are linked together in the form of blocks. in other words, it is an inmuteable database and no entry can be updated, it can only store new entries which makes it transparent and secure.

Now the question arise, if blockchain is a database, it must have some kind of data structure as well, yes it does and let me show you what that could be possibly

Basic data structure of a Blockchain?

Every block has a structure which brings together the transactions to include in the public ledger and all of this creates blockchain. A block has a header which contains metadata information, and in that metadata we have complete information of all the previous transactions. a block header is made of 3 different things. Firstly it contains the previous information of the block, Secondly it contains the mining competition and bounce and timestamp and lastly the third piece of metadata is the Merkle Tree root, which contains all the information of transactions in a very efficient way.

An example of a binary hash tree. Hashes 0-0 and 0-1 are the hash values of data blocks L1 and L2, respectively, and hash 0 is the hash of the concatenation of hashes 0-0 and 0–1.
Credits wikipedia

Basic blockchain data structure using JavaScript:

So if you have basic understanding of Javascript, you will be able to understand this below code very easily, or else you can learn basics of Javascript and it will help you understand what this code is all about.

Lets create a function called Blockchain:

function blockchain() {
    this.chain = [];
    this.pendingTransactions = [];
}

in the above code, I have created a basic function called blockchain, in this function, we see two arrays, one stores the current chain information while the other stores all the pendingTransactions in as array.

Now, lets look at creating a new Block using Javascript,

blockchain.prototype.createNewBlock = function(nonce, previousBlockHash, hash){
    const newBlock = {
        index: this.chain.length + 1,
        timestamp: Date.now(),
        transactions: this.pendingTransactiions,
        nonce: nonce,
        hash: hash,
        previousBlock: previousBlock
    }
    

    this.pendingTransactions = [];
    this.chain.push(newBlock);

    return newBlock;


}

In the above functions, we added a new function into our blockchain which includes nonce, previous block information, and a hash, timestamp and index. All of these information variables are important in order to complete a block to be valid. Complete code is given below:

function blockchain() {
    this.chain = [];
    this.pendingTransactions = [];

    this.getLastBlock = function() {
        return this.chain[this.chain.length - 1];
    };
}

blockchain.prototype.createNewBlock = function(nonce, previousBlockHash, hash) {
    const newBlock = {
        index: this.chain.length + 1,
        timestamp: Date.now(),
        transactions: this.pendingTransactions,
        nonce: nonce,
        hash: hash,
        previousBlockHash: previousBlockHash
    };

    this.pendingTransactions = [];
    this.chain.push(newBlock);

    return newBlock;
};

module.exports = blockchain;

Using the above code block, you can understand how to get started in blockchain world using Javascript.