Skip to content

Commit da41dd0

Browse files
committed
Add system info panel including jar hash
1 parent cc5f6c5 commit da41dd0

File tree

9 files changed

+160
-34
lines changed

9 files changed

+160
-34
lines changed

convex-core/src/main/java/convex/core/cpos/BlockResult.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,12 @@ public boolean equals(BlockResult a) {
191191
*/
192192
public static BlockResult createInvalidBlock(State state, Block block, AString message) {
193193
Result r=Result.create(null, message,ErrorCodes.PEER);
194-
AVector<Result> rs=Vectors.repeat(r, block.getTransactions().size());
194+
AVector<Result> rs;
195+
if (block==null) {
196+
rs=null;
197+
} else {
198+
rs=Vectors.repeat(r, block.getTransactions().size());
199+
}
195200

196201
return new BlockResult(state,rs);
197202
}

convex-core/src/main/java/convex/core/cvm/State.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import convex.core.data.prim.CVMLong;
3737
import convex.core.exceptions.BadFormatException;
3838
import convex.core.exceptions.InvalidDataException;
39-
import convex.core.exceptions.ValidationException;
4039
import convex.core.init.Init;
4140
import convex.core.lang.RT;
4241
import convex.core.lang.impl.TransactionContext;
@@ -205,26 +204,28 @@ public State withBalance(Address address, long newBalance) {
205204
* @throws InvalidBlockException
206205
*/
207206
public BlockResult applyBlock(SignedData<Block> signedBlock) {
208-
Block block=signedBlock.getValue();
209-
Counters.applyBlock++;
210-
211-
// First check the Block passes pre-conditions for application
212-
BlockResult maybeFailed=checkBlock(signedBlock);
213-
if (maybeFailed!=null) {
214-
return maybeFailed;
215-
}
216-
217-
// Prepare block, including scheduled transactions and time based updates
218-
State state = prepareBlock(block);
219-
220-
// Create TransactionContext after Block is prepared so we have correct timestamp etc.
221-
TransactionContext tctx=TransactionContext.create(state);
222-
tctx.block=signedBlock;
223-
207+
Block block=null;
224208
try {
209+
block=signedBlock.getValue();
210+
Counters.applyBlock++;
211+
212+
// First check the Block passes pre-conditions for application
213+
BlockResult maybeFailed=checkBlock(signedBlock);
214+
if (maybeFailed!=null) {
215+
return maybeFailed;
216+
}
217+
218+
// Prepare block, including scheduled transactions and time based updates
219+
State state = prepareBlock(block);
220+
221+
// Create TransactionContext after Block is prepared so we have correct timestamp etc.
222+
TransactionContext tctx=TransactionContext.create(state);
223+
tctx.block=signedBlock;
224+
225225
BlockResult blockResult= state.applyTransactions(block,tctx);
226226
return blockResult;
227-
} catch (ValidationException e) {
227+
} catch (Exception e) {
228+
// Invalid block, so no state upadtes
228229
return BlockResult.createInvalidBlock(this,block,Strings.create(e.getMessage()));
229230
}
230231
}

convex-core/src/main/java/convex/core/util/FileUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
1010

11+
import convex.core.data.Blob;
12+
1113
/**
1214
* Generic file handling utilities. Used in CLI etc.
1315
*/
@@ -34,6 +36,14 @@ public static String loadFileAsString(String fileName) throws IOException {
3436
}
3537
return result;
3638
}
39+
40+
public static Blob loadFileAsBlob(Path file) throws IOException {
41+
return Blob.wrap(Files.readAllBytes(file));
42+
}
43+
44+
public static byte[] loadFileAsBytes(Path file) throws IOException {
45+
return Files.readAllBytes(file);
46+
}
3747

3848
/**
3949
* Write a file as a UTF-8 String to the specified path
@@ -76,4 +86,7 @@ public static File getFile(String path) {
7686
}
7787
}
7888

89+
90+
91+
7992
}

convex-core/src/test/java/convex/core/cpos/OrderTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.*;
5+
import org.junit.jupiter.api.Test;
66

7-
import convex.core.data.ObjectsTest;
7+
import convex.core.crypto.AKeyPair;
8+
import convex.core.data.RecordTest;
89
import convex.core.data.Refs;
10+
import convex.core.data.SignedData;
11+
import convex.core.data.Vectors;
12+
import convex.test.Samples;
913

1014
public class OrderTest {
15+
AKeyPair KP=Samples.KEY_PAIR;
1116

1217
@Test public void testEmptyOrder() {
1318
Order o=Order.create();
@@ -16,6 +21,23 @@ public class OrderTest {
1621

1722
// Consensus cells (1+4) + timestamp (1) + empty vector (1)+Top leevl
1823
assertEquals(4+CPoSConstants.CONSENSUS_LEVELS,Refs.totalRefCount(o));
19-
ObjectsTest.doAnyValueTests(o);
24+
RecordTest.doRecordTests(o);
25+
}
26+
27+
@Test public void testBigOrder() {
28+
Order o=Order.create();
29+
o=o.withTimestamp(1234);
30+
assertEquals(1234,o.getTimestamp());
31+
32+
int NUM_BLOCKS=300;
33+
SignedData<Block> sb=KP.signData(Block.of(123));
34+
o=o.withBlocks(Vectors.repeat(sb, NUM_BLOCKS));
35+
assertEquals(NUM_BLOCKS,o.getBlockCount());
36+
assertEquals(NUM_BLOCKS,o.getConsensusPoint(0));
37+
assertEquals(0,o.getConsensusPoint(1));
38+
assertEquals(sb,o.getBlock(10));
39+
40+
41+
RecordTest.doRecordTests(o);
2042
}
2143
}

convex-core/src/test/java/convex/core/data/RecordTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import convex.core.Result;
1010
import convex.core.cpos.Belief;
1111
import convex.core.cpos.Block;
12-
import convex.core.cpos.Order;
1312
import convex.core.cvm.Address;
1413
import convex.core.cvm.RecordFormat;
1514
import convex.core.cvm.State;
@@ -47,12 +46,6 @@ public void testBlock() {
4746
doRecordTests(b);
4847
}
4948

50-
@Test
51-
public void testOrder() {
52-
Order order=Order.create();
53-
doRecordTests(order);
54-
}
55-
5649
@Test
5750
public void testState() {
5851
State s = InitTest.STATE;

convex-gui/src/main/java/convex/gui/tools/HackerTools.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.awt.BorderLayout;
44
import java.awt.Dimension;
5+
import java.awt.GridLayout;
56
import java.util.concurrent.TimeoutException;
67

78
import javax.swing.JFrame;
@@ -64,6 +65,9 @@ public static void main(String[] args) throws TimeoutException {
6465
public HackerTools() {
6566
super ("Hacker Tools");
6667
setLayout(new BorderLayout());
68+
69+
// tabs.setLayout(new MigLayout("","[fill]"));
70+
6771
keyGenPanel = new KeyGenPanel(null);
6872
messagePanel = new MessageFormatPanel();
6973
this.add(tabs, BorderLayout.CENTER);
@@ -72,6 +76,7 @@ public HackerTools() {
7276
tabs.add("KeyRing", new KeyRingPanel());
7377
tabs.add("Encoding", messagePanel);
7478
tabs.add("Data Lattice", dataPanel);
79+
tabs.add("System Info", new SystemInfoPanel());
7580

7681
// walletPanel.addWalletEntry(WalletEntry.create(convex.getAddress(), convex.getKeyPair()));
7782

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package convex.gui.tools;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import java.net.URLDecoder;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
import javax.swing.JLabel;
11+
import javax.swing.JPanel;
12+
13+
import convex.core.crypto.Hashing;
14+
import convex.core.text.Text;
15+
import convex.core.util.FileUtils;
16+
import convex.core.util.Utils;
17+
import convex.gui.components.ActionButton;
18+
import convex.gui.utils.Toolkit;
19+
import net.miginfocom.swing.MigLayout;
20+
21+
@SuppressWarnings("serial")
22+
public class SystemInfoPanel extends JPanel {
23+
public SystemInfoPanel() {
24+
MigLayout layout = new MigLayout("wrap 1","[]");
25+
this.setLayout(layout);
26+
27+
JPanel versionPanel=new JPanel();
28+
versionPanel.setLayout(new MigLayout("wrap 2"));
29+
versionPanel.add(new JLabel("Code Version:"));
30+
versionPanel.add(new JLabel(Utils.getVersion()));
31+
try {
32+
versionPanel.add(new JLabel("Running code at:"));
33+
URL sourceLocation=SystemInfoPanel.class.getProtectionDomain().getCodeSource().getLocation();
34+
String path = sourceLocation.getPath();
35+
String decodedPath = URLDecoder.decode(path, "UTF-8");
36+
versionPanel.add(new JLabel(decodedPath));
37+
38+
Path jarFile=new File(sourceLocation.toURI()).toPath();
39+
versionPanel.add(new JLabel("SHA256 Hash: "));
40+
if (Files.isRegularFile(jarFile)) {
41+
JPanel hashPanel=new JPanel();
42+
hashPanel.add(new ActionButton("Compute...,",0xe8b6, e->{
43+
try {
44+
String hash;
45+
hash = Hashing.sha256(FileUtils.loadFileAsBytes(jarFile)).toHexString();
46+
hashPanel.removeAll();
47+
hashPanel.add(new JLabel(hash.toUpperCase()));
48+
} catch (IOException e1) {
49+
// TODO Auto-generated catch block
50+
e1.printStackTrace();
51+
}
52+
}));
53+
54+
versionPanel.add(hashPanel);
55+
} else {
56+
versionPanel.add(new JLabel("Not a file: "+jarFile));
57+
}
58+
} catch (Exception e) {
59+
// ignore
60+
}
61+
62+
add(Toolkit.withTitledBorder("Convex Version", versionPanel));
63+
64+
JPanel systemPanel=new JPanel();
65+
systemPanel.setLayout(new MigLayout("wrap 2"));
66+
systemPanel.add(new JLabel("Operating System:"));
67+
systemPanel.add(new JLabel(System.getProperty("os.name")));
68+
systemPanel.add(new JLabel("Available Processors:"));
69+
systemPanel.add(new JLabel(Integer.toString(Runtime.getRuntime().availableProcessors())));
70+
add(Toolkit.withTitledBorder("System Information", systemPanel));
71+
72+
JPanel javaPanel=new JPanel();
73+
javaPanel.add(new JLabel("Java Version:"));
74+
javaPanel.add(new JLabel(Runtime.version().toString()));
75+
javaPanel.add(new JLabel("Maximum Memory:"));
76+
javaPanel.add(new JLabel(Text.toFriendlyNumber(Runtime.getRuntime().maxMemory())));
77+
add(Toolkit.withTitledBorder("Java Runtime", javaPanel));
78+
79+
}
80+
81+
}

convex-gui/src/main/java/convex/gui/utils/Toolkit.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,5 @@ public static void showMessge(Component parent, Object message) {
394394
public static void showErrorMessage(Component parent, String attemptFailure,Exception e) {
395395
JOptionPane.showMessageDialog(parent, attemptFailure+ "\n"+e.getMessage());
396396
}
397+
397398
}

convex-peer/src/main/java/convex/peer/TransactionHandler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,17 @@ private void reportTransactions(Block block, BlockResult br, long blockNum) {
261261
if (m != null) {
262262
ACell id = m.getID();
263263
log.trace("Returning transaction result ID {}", id);
264-
Result res = br.getResults().get(j);
264+
Result res = null;
265265

266-
extInfo.put(Keywords.LOC,Vectors.of(blockNum,j));
267-
extInfo.put(Keywords.TX,t.getHash());
268-
269-
res=res.withExtraInfo(extInfo);
266+
try {
267+
res=br.getResults().get(j);
268+
extInfo.put(Keywords.LOC,Vectors.createLongs(blockNum,j));
269+
extInfo.put(Keywords.TX,t.getHash());
270+
271+
res=res.withExtraInfo(extInfo);
272+
} catch (Exception e) {
273+
res=Result.error(ErrorCodes.FATAL, "Failed to produce result").withSource(SourceCodes.PEER);
274+
}
270275

271276
boolean reported = m.returnResult(res);
272277
if (!reported) {

0 commit comments

Comments
 (0)