-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blockchain.java
133 lines (116 loc) · 4.45 KB
/
Blockchain.java
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
package blockchain;
import java.util.ArrayList;
import java.util.List;
public class Blockchain {
private final List<Block> blocks;
private final List<TranslationMessage> translationMessages;
private final List<String> blockchainContent;
private int currentTranslationId;
private int zeroCount;
private int currentBlockId;
private String zeroString;
private String prevHash;
private long generateTime;
public Blockchain() {
blocks = new ArrayList<>();
translationMessages = new ArrayList<>();
currentBlockId = 1;
currentTranslationId = 1;
blockchainContent = new ArrayList<>();
zeroCount = 0;
zeroString = "";
prevHash = "0";
}
public int getCurrentTranslationId() {
return currentTranslationId;
}
public int getZeroCount() {
return zeroCount;
}
public int getCurrentBlockId() {
return currentBlockId;
}
public String getZeroString() {
return zeroString;
}
public String getPrevHash() {
return prevHash;
}
private void updateZeroString() {
zeroString = "0".repeat(zeroCount);
}
public synchronized void createBlock(Block block, long generateTime) {
if (block.isEquel(zeroString) &&
prevHash.equals(block.getPrevHash()) &&
currentBlockId == block.getId()
) {
StringBuilder content = new StringBuilder();
content.append(block.toString());
content.append("Block data: ");
if (currentBlockId == 1 || translationMessages.isEmpty()) content.append("\nNo transactions\n");
else {
content.append("\n");
for (TranslationMessage item : translationMessages) content.append(item).append("\n");
}
content.append("Block was generating for ").append(generateTime).append(" seconds\n");
if (generateTime < 2) {
if (zeroCount < 3)
zeroCount += 2;
else
zeroCount++;
content.append("N was increased to ").append(zeroCount).append("\n");
} else if (generateTime > 3) {
zeroCount--;
content.append("N was decreased by 1\n");
} else {
content.append("N stays the same\n");
}
translationMessages.clear();
currentBlockId++;
prevHash = block.getHash();
updateZeroString();
block.getMiner().income(100);
blocks.add(block);
blockchainContent.add(content.toString());
System.out.println(content.toString());
}
}
@Override
public synchronized String toString() {
StringBuilder sb = new StringBuilder();
for (String item : blockchainContent) {
sb.append(item).append("\n\n");
}
return sb.toString();
}
public synchronized boolean createMessage(TranslationMessage translationMessage) {
if (translationMessages.isEmpty() || translationMessage.getId() > translationMessages.get(translationMessages.size() - 1).getId()) {
VerifyMessage verify = new VerifyMessage();
try {
if (verify.verify(String.valueOf(translationMessage.getId()
+ translationMessage.getExpendManId()
+ translationMessage.getMoney()
+ translationMessage.getIncomeManId()),
translationMessage.getSignature(),
translationMessage.getPublicKey())) {
//捡钱加钱
if (translationMessage.getExpendMan().getMoney() > translationMessage.getMoney()) {
translationMessages.add(translationMessage);
translationMessage.getIncomeMan().income(translationMessage.getMoney());
translationMessage.getExpendMan().expend(translationMessage.getMoney());
currentTranslationId++;
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public int getNewestChatID() {
if (translationMessages.isEmpty())
return 0;
return translationMessages.get(translationMessages.size() - 1).getId();
}
}