200 lines of code to achieve a simple block chain __ block chain

Source: Internet
Author: User
Tags hash javascript array


Original English: Lauri Hartikka

The basic concept of a block chain is simple: a distributed database that stores an ever-growing list,list containing many sequential records. In general, however, when we talk about block chains, we also talk about the problem of using block chains, which can easily be confused. This is the case for a block-chain project like the popular Bitcoin and the Etheric square. The term "block chain" is often closely linked to concepts like trading, smart contracts, and cryptographic currencies.

This makes it much more difficult to understand the block chain, especially if you want to understand the source code. I will use the 200 line JS implementation of the super simple block chain to help you understand it, I gave this code named Naivechain. Block Structure

The first logical step is to determine the block structure. To ensure that things are as simple as possible, we only select the most necessary parts: index (subscript), timestamp (timestamp), data (data), hash (hash value), and previous hash (pre-hashing).

This block must be able to find the hash value of the previous block in order to ensure the integrity of the entire chain.

Class Block {
    Constructor (index, Previoushash, timestamp, data, hash) {
        this.index = index;
        This.previoushash = Previoushash.tostring ();
        This.timestamp = timestamp;
        This.data = data;
        This.hash = Hash.tostring ();
    }
}
Block Hash

In order to save the full data, you must hash the block. SHA-256 will encrypt the contents of the block, recording this value should have nothing to do with "digging", because there is no need to solve the problem of work proof.

var Calculatehash = (index, Previoushash, timestamp, data) => {return
    cryptojs.sha256 (index + Previoushash + times Tamp + data). ToString ();
Building of Blocks

To generate a block, you must know the hash value of the previous block, and then create the rest of the desired content (= index, hash, data and timestamp). The data portion of the block is provided by the end user.

var generatenextblock = (blockdata) => {
    var previousblock = Getlatestblock ();
    var nextindex = previousblock.index + 1;
    var nexttimestamp = new Date (). GetTime ()/1000;
    var Nexthash = Calculatehash (Nextindex, Previousblock.hash, Nexttimestamp, blockdata);
    Return to new block (Nextindex, Previousblock.hash, Nexttimestamp, Blockdata, Nexthash);
Storage of Blocks

An in-memory JavaScript array is used to store block chains. The first block of a block chain is usually called the "origin block" and is hard-coded.

var getgenesisblock = () => {return
    new blocks (0, "0", 1465154705, "My Genesis block!!", "816534932C2B7154836DA6AFC 367695e6337db8a921823784c14378abed4f7d7 ");
 
var blockchain = [Getgenesisblock ()];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.