Skip to content

Commit

Permalink
Add system info panel including jar hash
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 6, 2024
1 parent cc5f6c5 commit da41dd0
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 34 deletions.
7 changes: 6 additions & 1 deletion convex-core/src/main/java/convex/core/cpos/BlockResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ public boolean equals(BlockResult a) {
*/
public static BlockResult createInvalidBlock(State state, Block block, AString message) {
Result r=Result.create(null, message,ErrorCodes.PEER);
AVector<Result> rs=Vectors.repeat(r, block.getTransactions().size());
AVector<Result> rs;
if (block==null) {
rs=null;
} else {
rs=Vectors.repeat(r, block.getTransactions().size());
}

return new BlockResult(state,rs);
}
Expand Down
37 changes: 19 additions & 18 deletions convex-core/src/main/java/convex/core/cvm/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import convex.core.data.prim.CVMLong;
import convex.core.exceptions.BadFormatException;
import convex.core.exceptions.InvalidDataException;
import convex.core.exceptions.ValidationException;
import convex.core.init.Init;
import convex.core.lang.RT;
import convex.core.lang.impl.TransactionContext;
Expand Down Expand Up @@ -205,26 +204,28 @@ public State withBalance(Address address, long newBalance) {
* @throws InvalidBlockException
*/
public BlockResult applyBlock(SignedData<Block> signedBlock) {
Block block=signedBlock.getValue();
Counters.applyBlock++;

// First check the Block passes pre-conditions for application
BlockResult maybeFailed=checkBlock(signedBlock);
if (maybeFailed!=null) {
return maybeFailed;
}

// Prepare block, including scheduled transactions and time based updates
State state = prepareBlock(block);

// Create TransactionContext after Block is prepared so we have correct timestamp etc.
TransactionContext tctx=TransactionContext.create(state);
tctx.block=signedBlock;

Block block=null;
try {
block=signedBlock.getValue();
Counters.applyBlock++;

// First check the Block passes pre-conditions for application
BlockResult maybeFailed=checkBlock(signedBlock);
if (maybeFailed!=null) {
return maybeFailed;
}

// Prepare block, including scheduled transactions and time based updates
State state = prepareBlock(block);

// Create TransactionContext after Block is prepared so we have correct timestamp etc.
TransactionContext tctx=TransactionContext.create(state);
tctx.block=signedBlock;

BlockResult blockResult= state.applyTransactions(block,tctx);
return blockResult;
} catch (ValidationException e) {
} catch (Exception e) {
// Invalid block, so no state upadtes
return BlockResult.createInvalidBlock(this,block,Strings.create(e.getMessage()));
}
}
Expand Down
13 changes: 13 additions & 0 deletions convex-core/src/main/java/convex/core/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import convex.core.data.Blob;

/**
* Generic file handling utilities. Used in CLI etc.
*/
Expand All @@ -34,6 +36,14 @@ public static String loadFileAsString(String fileName) throws IOException {
}
return result;
}

public static Blob loadFileAsBlob(Path file) throws IOException {
return Blob.wrap(Files.readAllBytes(file));
}

public static byte[] loadFileAsBytes(Path file) throws IOException {
return Files.readAllBytes(file);
}

/**
* Write a file as a UTF-8 String to the specified path
Expand Down Expand Up @@ -76,4 +86,7 @@ public static File getFile(String path) {
}
}




}
28 changes: 25 additions & 3 deletions convex-core/src/test/java/convex/core/cpos/OrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

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

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Test;

import convex.core.data.ObjectsTest;
import convex.core.crypto.AKeyPair;
import convex.core.data.RecordTest;
import convex.core.data.Refs;
import convex.core.data.SignedData;
import convex.core.data.Vectors;
import convex.test.Samples;

public class OrderTest {
AKeyPair KP=Samples.KEY_PAIR;

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

// Consensus cells (1+4) + timestamp (1) + empty vector (1)+Top leevl
assertEquals(4+CPoSConstants.CONSENSUS_LEVELS,Refs.totalRefCount(o));
ObjectsTest.doAnyValueTests(o);
RecordTest.doRecordTests(o);
}

@Test public void testBigOrder() {
Order o=Order.create();
o=o.withTimestamp(1234);
assertEquals(1234,o.getTimestamp());

int NUM_BLOCKS=300;
SignedData<Block> sb=KP.signData(Block.of(123));
o=o.withBlocks(Vectors.repeat(sb, NUM_BLOCKS));
assertEquals(NUM_BLOCKS,o.getBlockCount());
assertEquals(NUM_BLOCKS,o.getConsensusPoint(0));
assertEquals(0,o.getConsensusPoint(1));
assertEquals(sb,o.getBlock(10));


RecordTest.doRecordTests(o);
}
}
7 changes: 0 additions & 7 deletions convex-core/src/test/java/convex/core/data/RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import convex.core.Result;
import convex.core.cpos.Belief;
import convex.core.cpos.Block;
import convex.core.cpos.Order;
import convex.core.cvm.Address;
import convex.core.cvm.RecordFormat;
import convex.core.cvm.State;
Expand Down Expand Up @@ -47,12 +46,6 @@ public void testBlock() {
doRecordTests(b);
}

@Test
public void testOrder() {
Order order=Order.create();
doRecordTests(order);
}

@Test
public void testState() {
State s = InitTest.STATE;
Expand Down
5 changes: 5 additions & 0 deletions convex-gui/src/main/java/convex/gui/tools/HackerTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.concurrent.TimeoutException;

import javax.swing.JFrame;
Expand Down Expand Up @@ -64,6 +65,9 @@ public static void main(String[] args) throws TimeoutException {
public HackerTools() {
super ("Hacker Tools");
setLayout(new BorderLayout());

// tabs.setLayout(new MigLayout("","[fill]"));

keyGenPanel = new KeyGenPanel(null);
messagePanel = new MessageFormatPanel();
this.add(tabs, BorderLayout.CENTER);
Expand All @@ -72,6 +76,7 @@ public HackerTools() {
tabs.add("KeyRing", new KeyRingPanel());
tabs.add("Encoding", messagePanel);
tabs.add("Data Lattice", dataPanel);
tabs.add("System Info", new SystemInfoPanel());

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

Expand Down
81 changes: 81 additions & 0 deletions convex-gui/src/main/java/convex/gui/tools/SystemInfoPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package convex.gui.tools;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.swing.JLabel;
import javax.swing.JPanel;

import convex.core.crypto.Hashing;
import convex.core.text.Text;
import convex.core.util.FileUtils;
import convex.core.util.Utils;
import convex.gui.components.ActionButton;
import convex.gui.utils.Toolkit;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class SystemInfoPanel extends JPanel {
public SystemInfoPanel() {
MigLayout layout = new MigLayout("wrap 1","[]");
this.setLayout(layout);

JPanel versionPanel=new JPanel();
versionPanel.setLayout(new MigLayout("wrap 2"));
versionPanel.add(new JLabel("Code Version:"));
versionPanel.add(new JLabel(Utils.getVersion()));
try {
versionPanel.add(new JLabel("Running code at:"));
URL sourceLocation=SystemInfoPanel.class.getProtectionDomain().getCodeSource().getLocation();
String path = sourceLocation.getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
versionPanel.add(new JLabel(decodedPath));

Path jarFile=new File(sourceLocation.toURI()).toPath();
versionPanel.add(new JLabel("SHA256 Hash: "));
if (Files.isRegularFile(jarFile)) {
JPanel hashPanel=new JPanel();
hashPanel.add(new ActionButton("Compute...,",0xe8b6, e->{
try {
String hash;
hash = Hashing.sha256(FileUtils.loadFileAsBytes(jarFile)).toHexString();
hashPanel.removeAll();
hashPanel.add(new JLabel(hash.toUpperCase()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}));

versionPanel.add(hashPanel);
} else {
versionPanel.add(new JLabel("Not a file: "+jarFile));
}
} catch (Exception e) {
// ignore
}

add(Toolkit.withTitledBorder("Convex Version", versionPanel));

JPanel systemPanel=new JPanel();
systemPanel.setLayout(new MigLayout("wrap 2"));
systemPanel.add(new JLabel("Operating System:"));
systemPanel.add(new JLabel(System.getProperty("os.name")));
systemPanel.add(new JLabel("Available Processors:"));
systemPanel.add(new JLabel(Integer.toString(Runtime.getRuntime().availableProcessors())));
add(Toolkit.withTitledBorder("System Information", systemPanel));

JPanel javaPanel=new JPanel();
javaPanel.add(new JLabel("Java Version:"));
javaPanel.add(new JLabel(Runtime.version().toString()));
javaPanel.add(new JLabel("Maximum Memory:"));
javaPanel.add(new JLabel(Text.toFriendlyNumber(Runtime.getRuntime().maxMemory())));
add(Toolkit.withTitledBorder("Java Runtime", javaPanel));

}

}
1 change: 1 addition & 0 deletions convex-gui/src/main/java/convex/gui/utils/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,5 @@ public static void showMessge(Component parent, Object message) {
public static void showErrorMessage(Component parent, String attemptFailure,Exception e) {
JOptionPane.showMessageDialog(parent, attemptFailure+ "\n"+e.getMessage());
}

}
15 changes: 10 additions & 5 deletions convex-peer/src/main/java/convex/peer/TransactionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,17 @@ private void reportTransactions(Block block, BlockResult br, long blockNum) {
if (m != null) {
ACell id = m.getID();
log.trace("Returning transaction result ID {}", id);
Result res = br.getResults().get(j);
Result res = null;

extInfo.put(Keywords.LOC,Vectors.of(blockNum,j));
extInfo.put(Keywords.TX,t.getHash());

res=res.withExtraInfo(extInfo);
try {
res=br.getResults().get(j);
extInfo.put(Keywords.LOC,Vectors.createLongs(blockNum,j));
extInfo.put(Keywords.TX,t.getHash());

res=res.withExtraInfo(extInfo);
} catch (Exception e) {
res=Result.error(ErrorCodes.FATAL, "Failed to produce result").withSource(SourceCodes.PEER);
}

boolean reported = m.returnResult(res);
if (!reported) {
Expand Down

0 comments on commit da41dd0

Please sign in to comment.