-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockChain.java
More file actions
135 lines (114 loc) · 3.86 KB
/
Copy pathBlockChain.java
File metadata and controls
135 lines (114 loc) · 3.86 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package blockchain;
import java.io.File;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;
public class BlockChain implements Serializable {
private static final long serialVersionUID = 3L;
private final List<Block> CHAIN;
private final boolean SAVE;
private final File PATH;
private int currentID;
private int zeros;
private final List<Message> MESSAGES;
public BlockChain() {
this(null);
}
public BlockChain(File file) {
this.SAVE = file != null;
this.PATH = file;
this.CHAIN = new ArrayList<>();
this.MESSAGES = new ArrayList<>();
this.addBlock();
if (this.SAVE) {
this.saveChain();
}
}
private String getZeroString() {
return this.zeros > 0 ? "0".repeat(this.zeros) : "";
}
public void addBlock() {
this.currentID++;
ExecutorService minerExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
ExecutorService messageStream = Executors.newSingleThreadExecutor();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
Future<Block> blockFuture = minerExecutor.submit(new Miner(getPreviousHash(),
getZeroString(), this.currentID, Instant.now(), this.consolidateMessages()));
this.MESSAGES.clear();
Messages messagesRun = new Messages(this.MESSAGES);
messageStream.submit(messagesRun);
try {
this.CHAIN.add(blockFuture.get());
minerExecutor.shutdownNow();
minerExecutor.awaitTermination(100L, TimeUnit.MILLISECONDS);
messagesRun.stop();
messageStream.awaitTermination(100L, TimeUnit.MILLISECONDS);
if (!validateChain()) {
this.currentID--;
this.CHAIN.remove(this.CHAIN.size() - 1);
this.addBlock();
return;
}
this.changeZeroValue(this.CHAIN.get(CHAIN.size() - 1).
getTIME_TO_CREATE_HASH());
if (this.SAVE) {
this.saveChain();
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
private String consolidateMessages() {
if (this.CHAIN.size() == 0) {
return "Block data: no messages\n";
}
StringBuilder blockData = new StringBuilder("Block data:\n");
this.MESSAGES.forEach(message -> blockData.append(message).append("\n"));
return blockData.toString();
}
private void changeZeroValue(long time) {
if (time < 20) {
this.zeros++;
} else if (time > 40) {
this.zeros--;
}
}
private void saveChain() {
SerializeBlockChain.Serialize(this, this.PATH);
}
private boolean validateChain() {
Iterator<Block> chainIterator = this.CHAIN.iterator();
String hash = chainIterator.next().getCurrentHash();
Block currBlock;
while (chainIterator.hasNext()) {
currBlock = chainIterator.next();
if (validateHash(currBlock.getPreviousHash(), hash)) {
hash = currBlock.getCurrentHash();
} else {
return false;
}
}
return true;
}
private boolean validateHash(String curr, String prev) {
return curr.equals(prev);
}
private String getPreviousHash() {
return this.CHAIN.size() > 0 ?
this.CHAIN.get(CHAIN.size() - 1).getCurrentHash() : "0";
}
public void printChain() {
for (Block block : this.CHAIN) {
System.out.println(block);
System.out.println();
}
}
}