-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboard.java
More file actions
394 lines (310 loc) · 15 KB
/
Dashboard.java
File metadata and controls
394 lines (310 loc) · 15 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class Dashboard {
private final JFrame frame;
private final JLabel walletABalanceLabel;
private final JLabel walletBBalanceLabel;
private final JLabel mempoolLabel;
private final JLabel difficultyLabel;
private final JLabel blockHeightLabel;
private final JLabel rewardLabel;
private final JLabel statusLabel;
private final JTextField amountField;
private final JTextArea logArea;
private final JComboBox<String> directionCombo;
private final JComboBox<String> minerCombo;
public Dashboard() {
setLookAndFeel();
frame = new JFrame("NoobChain Dashboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout(10, 10));
frame.getContentPane().setBackground(new Color(18, 20, 26));
JPanel root = new JPanel(new BorderLayout(10, 10));
root.setBorder(BorderFactory.createEmptyBorder(14, 14, 14, 14));
root.setBackground(new Color(18, 20, 26));
JPanel topRow = new JPanel(new GridLayout(1, 2, 10, 0));
topRow.setOpaque(false);
walletABalanceLabel = new JLabel();
walletBBalanceLabel = new JLabel();
topRow.add(makeBalanceCard("Wallet A", walletABalanceLabel, new Color(39, 174, 96)));
topRow.add(makeBalanceCard("Wallet B", walletBBalanceLabel, new Color(52, 152, 219)));
JPanel statsBar = new JPanel(new GridLayout(1, 4, 8, 0));
statsBar.setOpaque(false);
mempoolLabel = new JLabel("0", SwingConstants.CENTER);
difficultyLabel = new JLabel("0", SwingConstants.CENTER);
blockHeightLabel= new JLabel("0", SwingConstants.CENTER);
rewardLabel = new JLabel("0", SwingConstants.CENTER);
statsBar.add(makeStatCard("Mempool", mempoolLabel, new Color(243, 156, 18)));
statsBar.add(makeStatCard("Difficulty", difficultyLabel, new Color(155, 89, 182)));
statsBar.add(makeStatCard("Height", blockHeightLabel, new Color(52, 152, 219)));
statsBar.add(makeStatCard("Reward", rewardLabel, new Color(39, 174, 96)));
JPanel headerPanel = new JPanel(new BorderLayout(0, 8));
headerPanel.setOpaque(false);
headerPanel.add(topRow, BorderLayout.NORTH);
headerPanel.add(statsBar, BorderLayout.SOUTH);
logArea = new JTextArea();
logArea.setEditable(false);
logArea.setLineWrap(true);
logArea.setWrapStyleWord(true);
logArea.setFont(new Font("Consolas", Font.PLAIN, 12));
logArea.setBackground(new Color(13, 15, 20));
logArea.setForeground(new Color(200, 210, 220));
logArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
logArea.setCaretColor(new Color(39, 174, 96));
JScrollPane scroll = new JScrollPane(logArea);
scroll.setBorder(makeTitledBorder("Activity Log"));
scroll.setPreferredSize(new Dimension(820, 300));
scroll.getVerticalScrollBar().setUnitIncrement(16);
JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
row1.setOpaque(false);
amountField = new JTextField(10);
amountField.setFont(new Font("Consolas", Font.PLAIN, 14));
amountField.setBackground(new Color(28, 32, 40));
amountField.setForeground(Color.WHITE);
amountField.setCaretColor(Color.WHITE);
amountField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(60, 70, 90)),
BorderFactory.createEmptyBorder(6, 8, 6, 8)
));
directionCombo = new JComboBox<>(new String[]{"A → B", "B → A"});
styleCombo(directionCombo);
JButton sendBtn = makeButton("Submit to Mempool", new Color(39, 174, 96));
JButton mineBtn = makeButton("Mine Block", new Color(52, 152, 219));
JButton validateBtn = makeButton("Validate Chain", new Color(155, 89, 182));
JButton clearBtn = makeButton("Clear Log", new Color(80, 90, 110));
row1.add(new JLabel(styledLabel("Amount:")));
row1.add(amountField);
row1.add(new JLabel(styledLabel("Direction:")));
row1.add(directionCombo);
row1.add(sendBtn);
// Row 2: miner selection + mine + validate + clear
JPanel row2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
row2.setOpaque(false);
minerCombo = new JComboBox<>(new String[]{"Wallet A mines", "Wallet B mines"});
styleCombo(minerCombo);
row2.add(new JLabel(styledLabel("Miner:")));
row2.add(minerCombo);
row2.add(mineBtn);
row2.add(validateBtn);
row2.add(clearBtn);
// Status bar
statusLabel = new JLabel("Ready — submit a transaction or mine a block.");
statusLabel.setFont(new Font("Consolas", Font.PLAIN, 12));
statusLabel.setForeground(new Color(140, 160, 180));
statusLabel.setBorder(BorderFactory.createEmptyBorder(6, 2, 0, 0));
JPanel controlsPanel = new JPanel(new BorderLayout(0, 6));
controlsPanel.setOpaque(false);
controlsPanel.setBorder(makeTitledBorder("Controls"));
JPanel rows = new JPanel(new GridLayout(2, 1, 0, 6));
rows.setOpaque(false);
rows.add(row1);
rows.add(row2);
controlsPanel.add(rows, BorderLayout.CENTER);
controlsPanel.add(statusLabel, BorderLayout.SOUTH);
root.add(headerPanel, BorderLayout.NORTH);
root.add(scroll, BorderLayout.CENTER);
root.add(controlsPanel, BorderLayout.SOUTH);
frame.add(root, BorderLayout.CENTER);
sendBtn.addActionListener(e -> handleSend());
mineBtn.addActionListener(e -> handleMine());
validateBtn.addActionListener(e -> handleValidate());
clearBtn.addActionListener(e -> logArea.setText(""));
amountField.addActionListener(e -> handleSend());
new Timer(1000, e -> refreshAll()).start();
refreshAll();
log("INFO", "Dashboard initialized. Blockchain height: " + Noob.blockchain.size());
frame.pack();
frame.setMinimumSize(new Dimension(860, 600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void handleSend() {
if (!isReady()) return;
try {
float amount = Float.parseFloat(amountField.getText().trim());
if (amount <= 0f) { showError("Amount must be > 0."); return; }
boolean aToB = directionCombo.getSelectedIndex() == 0;
Wallet sender = aToB ? Noob.walletA : Noob.walletB;
Wallet recipient = aToB ? Noob.walletB : Noob.walletA;
String senderName = aToB ? "A" : "B";
String recipientName = aToB ? "B" : "A";
// sendFunds() now auto-submits to mempool
Transaction tx = sender.sendFunds(recipient.publicKey, (long)(amount * 100), (long)(Noob.miningReward * 10)); // fee = 10x reward for demo purposes
if (tx == null) {
showError("Insufficient funds — Wallet " + senderName + " has "
+ String.format("%.2f", sender.getBalance()));
return;
}
log("TX", String.format("Wallet %s → Wallet %s : %.2f | Mempool: %d pending",
senderName, recipientName, amount, Noob.mempool.size()));
setStatus("Transaction pending in mempool. Click Mine Block to confirm.");
amountField.setText("");
refreshAll();
} catch (NumberFormatException ex) {
showError("Invalid amount — enter a number like 10 or 25.5");
}
}
private void handleMine() {
if (!isReady()) return;
boolean aIsMinier = minerCombo.getSelectedIndex() == 0;
Wallet miner = aIsMinier ? Noob.walletA : Noob.walletB;
String minerName = aIsMinier ? "A" : "B";
if (Noob.mempool.isEmpty()) {
log("MINE", "Mempool empty — mining reward-only block for Wallet " + minerName);
} else {
log("MINE", "Mining " + Noob.mempool.size() + " pending TX(s) | Miner: Wallet " + minerName);
}
String prevHash = Noob.blockchain.get(Noob.blockchain.size() - 1).hash;
try {
Block mined = Noob.mineNextBlock(prevHash, miner);
if (mined != null) {
log("MINE", String.format(
"Block #%d mined | hash: %s... | difficulty: %d | reward: %.1f → Wallet %s",
Noob.blockchain.size() - 1,
mined.hash.substring(0, 12),
mined.difficulty,
Noob.miningReward,
minerName
));
setStatus("Block mined. Difficulty now: " + Noob.difficulty);
}
refreshAll();
} catch (Exception ex) {
showError("Mining failed: " + ex.getMessage());
}
}
private void handleValidate() {
if (!isReady()) return;
log("VALIDATE", "Validating chain of " + Noob.blockchain.size() + " blocks...");
boolean valid = Noob.isChainValid();
if (valid) {
log("VALID", "All " + Noob.blockchain.size() + " blocks passed validation.");
setStatus("Blockchain is valid ✓");
} else {
log("INVALID", "Chain validation FAILED — see console for details.");
setStatus("Chain validation FAILED.");
}
}
private void refreshAll() {
// Balances
String a = Noob.walletA != null ? StringUtil.toCoins(Noob.walletA.getBalance()): "-";
String b = Noob.walletB != null ? StringUtil.toCoins(Noob.walletB.getBalance()) : "—";
walletABalanceLabel.setText(balanceHtml(a));
walletBBalanceLabel.setText(balanceHtml(b));
// Stats
mempoolLabel.setText(String.valueOf(Noob.mempool.size()));
difficultyLabel.setText(String.valueOf(Noob.difficulty));
blockHeightLabel.setText(String.valueOf(Noob.blockchain.size() - 1)); // 0-indexed
rewardLabel.setText(StringUtil.toCoins(Noob.miningReward));
}
private void log(String tag, String msg) {
Color color = switch (tag) {
case "TX" -> new Color(39, 174, 96);
case "MINE" -> new Color(52, 152, 219);
case "VALID" -> new Color(39, 174, 96);
case "INVALID" -> new Color(231, 76, 60);
case "VALIDATE" -> new Color(155, 89, 182);
default -> new Color(140, 160, 180);
};
logArea.append("[" + tag + "] " + msg + "\n");
logArea.setCaretPosition(logArea.getDocument().getLength());
}
private void showError(String msg) {
log("ERROR", msg);
setStatus("Error: " + msg);
JOptionPane.showMessageDialog(frame, msg, "Error", JOptionPane.ERROR_MESSAGE);
}
private void setStatus(String msg) {
statusLabel.setText(msg);
}
private boolean isReady() {
if (Noob.walletA == null || Noob.walletB == null) {
showError("Wallets not initialized."); return false;
}
if (Noob.blockchain == null || Noob.blockchain.isEmpty()) {
showError("Blockchain empty."); return false;
}
return true;
}
private String balanceHtml(String value) {
return "<html><span style='font-family:Consolas;font-size:26px;" +
"font-weight:bold;color:#E8F4FD;'>" + value + "</span></html>";
}
private String styledLabel(String text) {
return "<html><span style='color:#8899aa;font-family:Consolas;" +
"font-size:12px;'>" + text + "</span></html>";
}
private JPanel makeBalanceCard(String title, JLabel valueLabel, Color accent) {
JPanel card = new JPanel(new BorderLayout(0, 6));
card.setBackground(new Color(24, 28, 36));
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 3, 0, 0, accent),
BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(45, 52, 65)),
BorderFactory.createEmptyBorder(14, 16, 14, 16)
)
));
JLabel titleLabel = new JLabel(title);
titleLabel.setFont(new Font("Consolas", Font.PLAIN, 12));
titleLabel.setForeground(new Color(120, 140, 160));
valueLabel.setText(balanceHtml("—"));
card.add(titleLabel, BorderLayout.NORTH);
card.add(valueLabel, BorderLayout.CENTER);
return card;
}
private JPanel makeStatCard(String title, JLabel valueLabel, Color accent) {
JPanel card = new JPanel(new BorderLayout(0, 4));
card.setBackground(new Color(24, 28, 36));
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(45, 52, 65)),
BorderFactory.createEmptyBorder(10, 12, 10, 12)
));
JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setFont(new Font("Consolas", Font.PLAIN, 11));
titleLabel.setForeground(new Color(100, 120, 140));
valueLabel.setFont(new Font("Consolas", Font.BOLD, 22));
valueLabel.setForeground(accent);
valueLabel.setHorizontalAlignment(SwingConstants.CENTER);
card.add(titleLabel, BorderLayout.NORTH);
card.add(valueLabel, BorderLayout.CENTER);
return card;
}
private JButton makeButton(String text, Color bg) {
JButton btn = new JButton(text);
btn.setFont(new Font("Consolas", Font.BOLD, 12));
btn.setBackground(bg);
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
btn.setBorderPainted(false);
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn.setBorder(BorderFactory.createEmptyBorder(8, 14, 8, 14));
btn.addMouseListener(new MouseAdapter() {
final Color base = bg;
public void mouseEntered(MouseEvent e) { btn.setBackground(base.brighter()); }
public void mouseExited(MouseEvent e) { btn.setBackground(base); }
});
return btn;
}
private void styleCombo(JComboBox<String> combo) {
combo.setFont(new Font("Consolas", Font.PLAIN, 13));
combo.setBackground(new Color(28, 32, 40));
combo.setForeground(Color.WHITE);
}
private TitledBorder makeTitledBorder(String title) {
TitledBorder tb = BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(new Color(50, 60, 80)), title
);
tb.setTitleFont(new Font("Consolas", Font.PLAIN, 11));
tb.setTitleColor(new Color(100, 120, 150));
return tb;
}
private void setLookAndFeel() {
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch (Exception ignored) {}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Dashboard::new);
}
}