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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
| /**
* PoW区块链综合测试
*/
public class PoWBlockchainTest {
public static void main(String[] args) throws Exception {
testPoWBlockchain();
}
/**
* 测试PoW区块链
*/
private static void testPoWBlockchain() throws Exception {
System.out.println("=== PoW区块链测试开始 ===\n");
// 创建区块链
PoWBlockchain blockchain = new PoWBlockchain();
// 添加一些初始交易
addInitialTransactions(blockchain);
// 启动多个矿工
blockchain.startMining("miner-alice", 2);
blockchain.startMining("miner-bob", 2);
blockchain.startMining("miner-charlie", 1);
// 让系统运行一段时间
Thread.sleep(30000); // 30秒
// 添加更多交易
addMoreTransactions(blockchain);
// 继续运行
Thread.sleep(60000); // 60秒
// 测试矿池
testMiningPool();
// 测试难度调节
testDifficultyAdjustment(blockchain);
// 打印最终状态
blockchain.printBlockchainStatus();
// 打印账户余额
printBalances(blockchain);
// 关闭系统
blockchain.shutdown();
System.out.println("\n=== PoW区块链测试完成 ===");
}
/**
* 添加初始交易
*/
private static void addInitialTransactions(PoWBlockchain blockchain) {
System.out.println("添加初始交易...");
String[] addresses = {"alice", "bob", "charlie", "david", "eve"};
Random random = new Random();
for (int i = 0; i < 20; i++) {
String from = addresses[random.nextInt(addresses.length)];
String to = addresses[random.nextInt(addresses.length)];
if (!from.equals(to)) {
double amount = 1 + random.nextDouble() * 99; // 1-100的随机金额
Transaction tx = new Transaction(from, to, amount);
blockchain.addTransaction(tx);
}
}
System.out.println("初始交易添加完成\n");
}
/**
* 添加更多交易
*/
private static void addMoreTransactions(PoWBlockchain blockchain) {
System.out.println("添加更多交易...");
String[] addresses = {"alice", "bob", "charlie", "david", "eve", "frank", "grace"};
Random random = new Random();
for (int i = 0; i < 50; i++) {
String from = addresses[random.nextInt(addresses.length)];
String to = addresses[random.nextInt(addresses.length)];
if (!from.equals(to)) {
double amount = 0.1 + random.nextDouble() * 49.9; // 0.1-50的随机金额
Transaction tx = new Transaction(from, to, amount);
blockchain.addTransaction(tx);
}
}
System.out.println("更多交易添加完成\n");
}
/**
* 测试矿池
*/
private static void testMiningPool() throws Exception {
System.out.println("=== 测试矿池 ===");
MiningPool pool = new MiningPool("mining-pool-1");
// 矿工加入矿池
pool.joinPool("pool-miner-1", 100);
pool.joinPool("pool-miner-2", 80);
pool.joinPool("pool-miner-3", 120);
pool.joinPool("pool-miner-4", 60);
// 创建测试区块
List<Transaction> transactions = Arrays.asList(
new Transaction("test-sender", "test-receiver", 50.0)
);
Block testBlock = new Block("test-previous-hash", transactions, 3);
// 矿池协作挖矿
try {
MiningResult result = pool.collaborativeMining(testBlock);
System.out.println("矿池挖矿结果: " + result);
} catch (Exception e) {
System.out.println("矿池挖矿失败: " + e.getMessage());
}
// 打印矿池统计
System.out.println("矿池统计: " + pool.getStatistics());
pool.shutdown();
System.out.println("=== 矿池测试完成 ===\n");
}
/**
* 测试难度调节
*/
private static void testDifficultyAdjustment(PoWBlockchain blockchain) {
System.out.println("=== 测试难度调节 ===");
// 模拟快速出块场景
List<Block> fastBlocks = createFastBlocks(10, 2); // 难度2,快速出块
int newDifficulty = DifficultyAdjuster.calculateNewDifficulty(fastBlocks, 2);
System.out.println("快速出块场景,新难度: " + newDifficulty);
// 模拟慢速出块场景
List<Block> slowBlocks = createSlowBlocks(10, 6); // 难度6,慢速出块
newDifficulty = DifficultyAdjuster.calculateNewDifficulty(slowBlocks, 6);
System.out.println("慢速出块场景,新难度: " + newDifficulty);
// 预测下次调节
DifficultyPrediction prediction = DifficultyAdjuster.predictNextAdjustment(fastBlocks, 4);
System.out.println("难度预测: " + prediction);
System.out.println("=== 难度调节测试完成 ===\n");
}
/**
* 创建快速出块的模拟区块
*/
private static List<Block> createFastBlocks(int count, int difficulty) {
List<Block> blocks = new ArrayList<>();
long currentTime = System.currentTimeMillis() / 1000;
for (int i = 0; i < count; i++) {
List<Transaction> txs = Arrays.asList(new Transaction("test", "test2", 1.0));
Block block = new Block("hash" + i, txs, difficulty);
// 设置快速出块时间(5分钟间隔)
BlockHeader header = block.getHeader();
long blockTime = currentTime - (count - i) * 5 * 60; // 5分钟间隔
// 注意:实际实现中需要修改BlockHeader允许设置时间戳
blocks.add(block);
}
return blocks;
}
/**
* 创建慢速出块的模拟区块
*/
private static List<Block> createSlowBlocks(int count, int difficulty) {
List<Block> blocks = new ArrayList<>();
long currentTime = System.currentTimeMillis() / 1000;
for (int i = 0; i < count; i++) {
List<Transaction> txs = Arrays.asList(new Transaction("test", "test2", 1.0));
Block block = new Block("hash" + i, txs, difficulty);
// 设置慢速出块时间(20分钟间隔)
// 实际实现中需要修改BlockHeader以支持自定义时间戳
blocks.add(block);
}
return blocks;
}
/**
* 打印账户余额
*/
private static void printBalances(PoWBlockchain blockchain) {
System.out.println("=== 账户余额 ===");
String[] addresses = {"alice", "bob", "charlie", "david", "eve",
"miner-alice", "miner-bob", "miner-charlie"};
for (String address : addresses) {
double balance = blockchain.getBalance(address);
if (balance != 0) {
System.out.println(address + ": " + String.format("%.6f", balance));
}
}
System.out.println("================\n");
}
/**
* 性能测试
*/
private static void performanceTest() {
System.out.println("=== 性能测试 ===");
// 测试单线程挖矿性能
PoWMiner singleMiner = new PoWMiner("perf-miner");
List<Transaction> txs = Arrays.asList(new Transaction("test", "test2", 1.0));
Block testBlock = new Block("test-hash", txs, 4);
long startTime = System.currentTimeMillis();
MiningResult result = singleMiner.mine(testBlock);
long endTime = System.currentTimeMillis();
if (result.isSuccess()) {
double hashRate = (double) result.getHashCount() / ((endTime - startTime) / 1000.0);
System.out.println("单线程挖矿 - 哈希率: " + String.format("%.0f", hashRate) + " H/s");
}
// 测试多线程挖矿性能
MultiThreadPoWMiner multiMiner = new MultiThreadPoWMiner("multi-miner", 4);
Block testBlock2 = new Block("test-hash2", txs, 4);
startTime = System.currentTimeMillis();
MiningResult result2 = multiMiner.mine(testBlock2);
endTime = System.currentTimeMillis();
if (result2.isSuccess()) {
double hashRate = (double) result2.getHashCount() / ((endTime - startTime) / 1000.0);
System.out.println("多线程挖矿 - 哈希率: " + String.format("%.0f", hashRate) + " H/s");
}
multiMiner.shutdown();
// 测试GPU模拟挖矿
GPUSimulatedMiner gpuMiner = new GPUSimulatedMiner("gpu-miner", 1000);
Block testBlock3 = new Block("test-hash3", txs, 4);
startTime = System.currentTimeMillis();
MiningResult result3 = gpuMiner.parallelMine(testBlock3);
endTime = System.currentTimeMillis();
if (result3.isSuccess()) {
double hashRate = (double) result3.getHashCount() / ((endTime - startTime) / 1000.0);
System.out.println("GPU模拟挖矿 - 哈希率: " + String.format("%.0f", hashRate) + " H/s");
}
System.out.println("=== 性能测试完成 ===\n");
}
}
|