Simple Blockchain Example in Java


blockchain Simple Blockchain Example in Java blockchain java

blockchain

Blockchain is a hot topic – and we’ll use simple Java code to illustrate the idea. First, we need to define a Block class that is basically a Node in a doubly-linked list. And to ensure the data integrity, we will have a hash field (normally SHA-256) that is computed based on the entire block data. It is like sealing the block with signature so any subsequent modification of the block will result in mismatch of the signature and the one computed based on the block data.

The blocks are linked so virtually it is a double-linked list so that you can trace back to the first block or in the opposite direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.helloacm;
 
class Block {
    static int BlockNumber = 0;
 
    public Block(Block prev, Block next, String data) {
        this.prev = prev;
        this.hash = data.hashCode();
        this.next = next;
        this.data = data;
        BlockNumber ++;
        this.blockNumber = BlockNumber;
    }
 
    private Block prev;
    private Block next;
    private long hash;
    private String data;
    private int blockNumber;
 
    public int getBlockNumber() {
        return this.blockNumber;
    }
 
    public String getData() {
        return this.data;
    }
 
    public Block getNext() {
        return this.next;
    }
 
    public Block getPrev() {
        return this.prev;
    }
 
    // check if current block is valid and connected to the first block
    public boolean isValid() {
        Block cur = this;
        while (cur.prev != null) {
            if (!validateHash()) {
                return false;
            }
            Block prevBlock = cur.prev;
            if (prevBlock != null) {
                if (prevBlock.next != cur) {
                    return false;
                }
            }
            cur = prevBlock;
        }
        return cur.validateHash() && cur.data == "0";
    }
 
    // connect the current block to next block
    public void generateBlock(Block next) {
        this.next = next;
        next.prev = this;
    }
 
    // current block should have correct hash
    public boolean validateHash() {
        return this.data.hashCode() == this.hash;
    }
}
 
public class Main {
    public static void main(String[] args) {
        var firstBlock = new Block(null, null, "0");
        var data = new String[] {"Hello", "World!", "How", "Are", "You?"};
        var prev = firstBlock;
        for (var s: data) {
            Block cur = new Block(prev, null, s);
            prev.generateBlock(cur);;
            prev = cur;
        }
        var root = firstBlock;
        while (root != null) {
            if (!root.isValid()) {
                throw new RuntimeException("Block Invalid.");
            }
            System.out.println(String.format("Block %d - Data %s", root.getBlockNumber(), root.getData()));
            root = root.getNext(); // get to next block
        }
    }
}
package com.helloacm;

class Block {
    static int BlockNumber = 0;

    public Block(Block prev, Block next, String data) {
        this.prev = prev;
        this.hash = data.hashCode();
        this.next = next;
        this.data = data;
        BlockNumber ++;
        this.blockNumber = BlockNumber;
    }

    private Block prev;
    private Block next;
    private long hash;
    private String data;
    private int blockNumber;

    public int getBlockNumber() {
        return this.blockNumber;
    }

    public String getData() {
        return this.data;
    }

    public Block getNext() {
        return this.next;
    }

    public Block getPrev() {
        return this.prev;
    }

    // check if current block is valid and connected to the first block
    public boolean isValid() {
        Block cur = this;
        while (cur.prev != null) {
            if (!validateHash()) {
                return false;
            }
            Block prevBlock = cur.prev;
            if (prevBlock != null) {
                if (prevBlock.next != cur) {
                    return false;
                }
            }
            cur = prevBlock;
        }
        return cur.validateHash() && cur.data == "0";
    }

    // connect the current block to next block
    public void generateBlock(Block next) {
        this.next = next;
        next.prev = this;
    }

    // current block should have correct hash
    public boolean validateHash() {
        return this.data.hashCode() == this.hash;
    }
}

public class Main {
    public static void main(String[] args) {
        var firstBlock = new Block(null, null, "0");
        var data = new String[] {"Hello", "World!", "How", "Are", "You?"};
        var prev = firstBlock;
        for (var s: data) {
            Block cur = new Block(prev, null, s);
            prev.generateBlock(cur);;
            prev = cur;
        }
        var root = firstBlock;
        while (root != null) {
            if (!root.isValid()) {
                throw new RuntimeException("Block Invalid.");
            }
            System.out.println(String.format("Block %d - Data %s", root.getBlockNumber(), root.getData()));
            root = root.getNext(); // get to next block
        }
    }
}

The above Blockchain code generates a blockchain and traces from the root – validate each block and print its content.

Block 1 – Data 0
Block 2 – Data Hello
Block 3 – Data World!
Block 4 – Data How
Block 5 – Data Are
Block 6 – Data You?

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
517 words
Last Post: How to Define N-nary Tree in Java?
Next Post: Teaching Kids Programming - Using GroupBy Algorithm to Compress String

The Permanent URL is: Simple Blockchain Example in Java

Leave a Reply