changeset 139:694ea96a557a

refactor blockchain
author akahori
date Tue, 01 Jan 2019 15:07:22 +0900
parents f172c4540461
children 77169cd8a5e8
files src/main/java/christie/blockchain/Block.java src/main/java/christie/blockchain/BlockChain.java src/main/java/christie/blockchain/BlockHeader.java src/main/java/christie/blockchain/Miner.java
diffstat 4 files changed, 56 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/christie/blockchain/Block.java	Fri Dec 28 16:45:38 2018 +0900
+++ b/src/main/java/christie/blockchain/Block.java	Tue Jan 01 15:07:22 2019 +0900
@@ -10,15 +10,15 @@
 
     private List<Transaction> transactionsList = new CopyOnWriteArrayList<>();
 
-    public Block(long timestamp, String data){
-        this("".getBytes(), timestamp, data, null);
+    public Block(String data, long difficulty, long timestamp){
+        this("".getBytes(), difficulty, 0, data, timestamp, null);
     }
-    public Block(byte[] parentHash, long timestamp, String data){
-        this(parentHash, timestamp, data, null);
+    public Block(byte[] parentHash, long difficulty, long number, String data, long timestamp){
+        this(parentHash, difficulty, number, data, timestamp, null);
     }
 
-    public Block(byte[] parentHash, long timestamp, String data, List<Transaction> transactionsList) {
-        this.header = new BlockHeader(parentHash, timestamp);
+    public Block(byte[] parentHash, long difficulty, long number, String data, long timestamp, List<Transaction> transactionsList) {
+        this.header = new BlockHeader(parentHash, difficulty, number, timestamp);
         this.data = data;
         if (this.transactionsList == null) {
             this.transactionsList = new CopyOnWriteArrayList<>();
@@ -45,7 +45,9 @@
         return data;
     }
 
-    public void setNonce(long nonce) { this.header.setNonce(nonce);}
+    public void setNonce(long nonce) {
+        this.header.setNonce(nonce);
+    }
 
     public long getNonce(){
         return this.header.getNonce();
@@ -55,4 +57,8 @@
         return this.header.getPresentHashWithoutNonce();
     }
 
+    public long getNumber() {
+        return this.header.getNumber();
+    }
+
 }
--- a/src/main/java/christie/blockchain/BlockChain.java	Fri Dec 28 16:45:38 2018 +0900
+++ b/src/main/java/christie/blockchain/BlockChain.java	Tue Jan 01 15:07:22 2019 +0900
@@ -9,6 +9,8 @@
 
     public ArrayList<Block> blockList = new ArrayList<Block>();
 
+
+
     public static void main(String[] args) {
         int difficulty = 1;
         BlockChain blockChain = new BlockChain();
@@ -70,13 +72,24 @@
         // もし, 時差があって, timeが親のタイムスタンプより低いなら, 親のタイムスタンプ + 1 を代入する
         if (parent.getTimestamp() >= time) time = parent.getTimestamp() + 1;
 
-        Block block = new Block(parent.getPresentHash(), time, data);
+        return createNewBlock(parent, data, time);
+    }
+
+    public synchronized  Block createNewBlock(Block parent, String data, long time){
+        final long blockNumber = parent.getNumber() + 1;
+        long difficulty = 1;
+
+        Block block = new Block(parent.getPresentHash(),
+                difficulty, blockNumber,
+                data,
+                time);
+
         return block;
     }
 
     public synchronized Block createGenesisBlock(String data){
         long time = System.currentTimeMillis() / 1000;
-        Block block = new Block(time, data);
+        Block block = new Block(data, 1, time);
         return block;
     }
 
--- a/src/main/java/christie/blockchain/BlockHeader.java	Fri Dec 28 16:45:38 2018 +0900
+++ b/src/main/java/christie/blockchain/BlockHeader.java	Tue Jan 01 15:07:22 2019 +0900
@@ -13,14 +13,20 @@
 
     private byte[] parentHash;
 
+    private long difficulty;
+
+    private long number;
+
     private long timestamp;
 
     private long nonce;
 
     HashUtil hashUtil = new HashUtil();
 
-    public BlockHeader(byte[] parentHash, long timestamp) {
+    public BlockHeader(byte[] parentHash, long difficulty, long number, long timestamp) {
         this.parentHash = parentHash;
+        this.difficulty = difficulty;
+        this.number = number;
         this.timestamp = timestamp;
     }
 
@@ -62,18 +68,35 @@
         return getHash(false);
     }
 
+    public void setTimestamp(long timestamp) {
+        this.timestamp = timestamp;
+    }
+
     public long getTimestamp() {
         return timestamp;
     }
 
-    public void setTimestamp(long timestamp) {
-        this.timestamp = timestamp;
+    public void setNonce(long nonce) {
+        this.nonce = nonce;
     }
 
-    public void setNonce(long nonce) { this.nonce = nonce; }
-
     public long getNonce(){
         return this.nonce;
     }
 
+    public void setNumber(long number) {
+        this.number = number;
+    }
+
+    public long getNumber() {
+        return number;
+    }
+
+    public void setDifficulty(long difficulty) {
+        this.difficulty = difficulty;
+    }
+
+    public long getDifficulty() {
+        return difficulty;
+    }
 }
--- a/src/main/java/christie/blockchain/Miner.java	Fri Dec 28 16:45:38 2018 +0900
+++ b/src/main/java/christie/blockchain/Miner.java	Tue Jan 01 15:07:22 2019 +0900
@@ -32,8 +32,6 @@
         newBlock.setNonce(nonce);
         System.out.println("Block Mined!!! : " + hashStr);
 
-
-
     }
 
     public boolean increment(byte[] bytes) {