To create a block chain from scratch using the Java language __php

Source: Internet
Author: User

Java block chain development and communication group: 613121183

Interested can also add ha, provided a lot of block chain information, there will be information to share

At present, the network on the Block chain primer, Popular science articles, this article will no longer repeat the basic concept of the block chain, if the block chain is not very understanding, you can look at my previous collection of some of the introductory learning resources:

http://blog.51cto.com/zero01/2066321

We are curious about the technology of block chain, all want to know how the block chain in the code is how to achieve, so this is the actual combat to the theory, after all, we have seen a lot of, but the specific implementation of the block chain is not very clear, this article on the use of Java language to achieve a simple block chain.

But to fully understand the block chain is not easy, for a relatively unfamiliar technology, we need to learn in theory + practice, by writing code to learn technology will be more firmly mastered, building a block chain can deepen the understanding of the block chain.

Preparatory work

Master basic Javase and Javaweb development, be able to use Java to develop simple projects, and need to understand the HTTP protocol.

We know that a block chain is an immutable, orderly chain of records made up of chunks, records can be transactions, files, or any data you want, and it is important that they are linked by a hash value (hashes).

If you're not quite aware of what the hash is, you can view this article

Environment Description JDK1.8 Tomcat 9.0 Maven 3.5 JSON 20160810 JAVAEE-API 7.0

Pom.xml File Configuration content:

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId> javaee-api</artifactid>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactid >json</artifactId>
            <version>20160810</version>
        </dependency>
    </ Dependencies>

Then you need an HTTP client, such as a curl or other client under the Postman,linux command line, where I'm using postman.

Blockchain class
First, create a blockchain class that creates two major collections in the constructor. One for the storage block chain, one for the storage of trading lists, all the core of this article is written in this class, easy to view, in the actual development is not appropriate to do so, should be the code split carefully reduce the coupling degree.

The following is the framework code for the Blockchain class:

Package Org.zero01.dao;
Import java.util.ArrayList;
Import Java.util.HashMap;

Import java.util.List;
    public class Blockchain {//storage block chain private list<object> chain;

    The instance variable is used for the current transaction information list private list<object> currenttransactions;
        Public Blockchain () {//init block chain and current transaction information list This.chain = new arraylist<object> ();
    this.currenttransactions= new arraylist<object> ();
    Public list<object> GetChain () {return chain;
    public void Setchain (list<object> chain) {this.chain = chain;
    Public list<object> getcurrenttransactions () {return currenttransactions; } public void Setcurrenttransactions (list<object> currenttransactions) {this.currenttransactions = cur
    Renttransactions;
    Public Object Lastblock () {return null;
    Public hashmap<string, Object> Newblock () {return null; } public int NEWtransactions () {return 0;
    public static Object hash (hashmap<string, object> blocks) {return null; }
}

The blockchain class is used to manage the block chain, it can store transactions, add new blocks, and so on, let's further refine these methods.

The structure of a block

First you need to explain the structure of the blocks, each containing attributes: Index, timestamp (timestamp), transaction list (transactions), work proof (explained later), and hash value of the previous block.

The following is the structure of a block:

block = {
    ' index ': 1,
    ' timestamp ': 1506057125.900785,
    ' transactions ': [
        {
            ' sender ': ' 8527147fe1f5426f9dd545de4b27ee00 ",
            ' recipient ':" a77f5cdfa2934df3954a5c7c7da5df1f ",
            ' Amount ': 5,
        }
    ],
    ' proof ': 324984774000,
    ' Previous_hash ': " 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 "
}

Here, the concept of the block chain is clear, each new block contains the hash of the previous block, which is the key point, it protects the block chain is not denatured. If an attacker destroys one of the previous blocks, then the hash of all the blocks that follow will become incorrect. Do not understand the words, slowly digestion, you can refer to the block chain accounting principle.

Because we need to compute the hash of the chunk, we first write a tool class to compute the hash value:

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.