changeset 145:a5cd12f6a942

merge FizzBuzz
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Mon, 07 Jan 2019 17:20:53 +0900
parents fdb2fbf68bf5 (current diff) 77169cd8a5e8 (diff)
children acf127ec8d8c
files
diffstat 6 files changed, 92 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/Log/ring.dot	Mon Jan 07 17:20:53 2019 +0900
@@ -0,0 +1,8 @@
+digraph test {
+	node0 -> node1 [label="right"]
+	node0 -> node2 [label="left"]
+	node1 -> node2 [label="right"]
+	node1 -> node0 [label="left"]
+	node2 -> node0 [label="right"]
+	node2 -> node1 [label="left"]
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/ring.rb	Mon Jan 07 17:20:53 2019 +0900
@@ -0,0 +1,28 @@
+def create_nodes(node_num)
+  (0..node_num - 1).map { |i|
+    i = "node" + i.to_s
+  }
+end
+
+def print_dot(connections)
+  puts "digraph test {"
+  connections.each { |connection|
+    print "\t"
+    print connection[0]
+    print " -> "
+    print connection[1]
+    print ' [label="' + connection[2] + '"]'
+    puts
+  }
+  puts "}"
+end
+
+node_num = ARGV[0].to_i
+nodes = create_nodes(node_num)
+connections = Array.new
+nodes.each_with_index { |node, i|
+  connections << [nodes[i], nodes[(i + 1) % node_num], "right"]
+  connections << [nodes[i], nodes[i - 1], "left"]
+}
+print_dot(connections)
+
--- a/src/main/java/christie/blockchain/Block.java	Fri Jan 04 04:40:33 2019 +0900
+++ b/src/main/java/christie/blockchain/Block.java	Mon Jan 07 17:20:53 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 Jan 04 04:40:33 2019 +0900
+++ b/src/main/java/christie/blockchain/BlockChain.java	Mon Jan 07 17:20:53 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 Jan 04 04:40:33 2019 +0900
+++ b/src/main/java/christie/blockchain/BlockHeader.java	Mon Jan 07 17:20:53 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 Jan 04 04:40:33 2019 +0900
+++ b/src/main/java/christie/blockchain/Miner.java	Mon Jan 07 17:20:53 2019 +0900
@@ -32,8 +32,6 @@
         newBlock.setNonce(nonce);
         System.out.println("Block Mined!!! : " + hashStr);
 
-
-
     }
 
     public boolean increment(byte[] bytes) {