mirror of
https://github.com/status-im/ethereumj-personal.git
synced 2025-01-10 03:45:44 +00:00
commit
2022ddcb6d
@ -14,6 +14,7 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
|
import org.spongycastle.util.encoders.DecoderException;
|
||||||
import org.spongycastle.util.encoders.Hex;
|
import org.spongycastle.util.encoders.Hex;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
@ -63,6 +64,22 @@ public class Utils {
|
|||||||
return result.toString() + "·(" + "10^" + pow + ")";
|
return result.toString() + "·(" + "10^" + pow + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes a hex string to address bytes and checks validity
|
||||||
|
*
|
||||||
|
* @param hex - a hex string of the address, e.g., 6c386a4b26f73c802f34673f7248bb118f97424a
|
||||||
|
* @return - decode and validated address byte[]
|
||||||
|
*/
|
||||||
|
public static byte[] addressStringToBytes(String hex) {
|
||||||
|
byte[] addr = null;
|
||||||
|
try { addr = Hex.decode(hex); }
|
||||||
|
catch(DecoderException addressIsNotValid) { return null; }
|
||||||
|
|
||||||
|
if(isValidAddress(addr))
|
||||||
|
return addr;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isValidAddress(byte[] addr) {
|
public static boolean isValidAddress(byte[] addr) {
|
||||||
return addr != null && addr.length == 20;
|
return addr != null && addr.length == 20;
|
||||||
}
|
}
|
||||||
|
@ -617,13 +617,15 @@ public class Program {
|
|||||||
ContractDetails contractDetails = this.result.getRepository().
|
ContractDetails contractDetails = this.result.getRepository().
|
||||||
getContractDetails(this.programAddress.getLast20Bytes());
|
getContractDetails(this.programAddress.getLast20Bytes());
|
||||||
StringBuilder storageData = new StringBuilder();
|
StringBuilder storageData = new StringBuilder();
|
||||||
List<DataWord> storageKeys = new ArrayList<>(contractDetails.getStorage().keySet());
|
if(contractDetails != null) {
|
||||||
Collections.sort((List<DataWord>) storageKeys);
|
List<DataWord> storageKeys = new ArrayList<>(contractDetails.getStorage().keySet());
|
||||||
for (DataWord key : storageKeys) {
|
Collections.sort((List<DataWord>) storageKeys);
|
||||||
storageData.append(" ").append(key).append(" -> ").
|
for (DataWord key : storageKeys) {
|
||||||
append(contractDetails.getStorage().get(key)).append("\n");
|
storageData.append(" ").append(key).append(" -> ").
|
||||||
|
append(contractDetails.getStorage().get(key)).append("\n");
|
||||||
|
}
|
||||||
|
if (storageData.length() > 0) storageData.insert(0, "\n");
|
||||||
}
|
}
|
||||||
if (storageData.length() > 0) storageData.insert(0, "\n");
|
|
||||||
|
|
||||||
StringBuilder memoryData = new StringBuilder();
|
StringBuilder memoryData = new StringBuilder();
|
||||||
StringBuilder oneLine = new StringBuilder();
|
StringBuilder oneLine = new StringBuilder();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package org.ethereum.util;
|
package org.ethereum.util;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.spongycastle.util.Arrays;
|
||||||
import org.spongycastle.util.encoders.Hex;
|
import org.spongycastle.util.encoders.Hex;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
@ -59,4 +60,31 @@ public class UtilsTest {
|
|||||||
|
|
||||||
assertEquals(expected, result);
|
assertEquals(expected, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddressStringToBytes() {
|
||||||
|
// valid address
|
||||||
|
String HexStr = "6c386a4b26f73c802f34673f7248bb118f97424a";
|
||||||
|
byte[] expected = Hex.decode(HexStr);
|
||||||
|
byte[] result = Utils.addressStringToBytes(HexStr);
|
||||||
|
assertEquals(Arrays.areEqual(expected, result), true);
|
||||||
|
|
||||||
|
// invalid address, we removed the last char so it cannot decode
|
||||||
|
HexStr = "6c386a4b26f73c802f34673f7248bb118f97424";
|
||||||
|
expected = null;
|
||||||
|
result = Utils.addressStringToBytes(HexStr);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
|
||||||
|
// invalid address, longer than 20 bytes
|
||||||
|
HexStr = new String(Hex.encode("I am longer than 20 bytes, i promise".getBytes()));
|
||||||
|
expected = null;
|
||||||
|
result = Utils.addressStringToBytes(HexStr);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
|
||||||
|
// invalid address, shorter than 20 bytes
|
||||||
|
HexStr = new String(Hex.encode("I am short".getBytes()));
|
||||||
|
expected = null;
|
||||||
|
result = Utils.addressStringToBytes(HexStr);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
}
|
}
|
@ -221,19 +221,14 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void populateContractDetails() {
|
private void populateContractDetails() {
|
||||||
|
byte[] addr = Utils.addressStringToBytes(contractAddrInput.getText());
|
||||||
String contractAddr = contractAddrInput.getText();
|
if(addr == null) {
|
||||||
if (contractAddr == null || contractAddr.length() == 0) {
|
alertStatusMsg("Not a valid contract address");
|
||||||
alertStatusMsg("");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Pattern.matches("[0-9a-fA-F]+", contractAddr) || (contractAddr.length() != 40)) {
|
|
||||||
alertStatusMsg("Not a valid contract address");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ContractDetails contractDetails = UIEthereumManager.ethereum
|
ContractDetails contractDetails = UIEthereumManager.ethereum
|
||||||
.getRepository().getContractDetails(Hex.decode(contractAddr));
|
.getRepository().getContractDetails(addr);
|
||||||
if (contractDetails == null) {
|
if (contractDetails == null) {
|
||||||
alertStatusMsg("No contract for that address");
|
alertStatusMsg("No contract for that address");
|
||||||
return;
|
return;
|
||||||
@ -316,15 +311,15 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog {
|
|||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playContractCall() {
|
private void playContractCall() {
|
||||||
|
byte[] addr = Utils.addressStringToBytes(contractAddrInput.getText());
|
||||||
String contractAddr = contractAddrInput.getText();
|
if(addr == null) {
|
||||||
if (!Pattern.matches("[0-9a-fA-F]+", contractAddr) || (contractAddr.length() != 40)) {
|
alertStatusMsg("Not a valid contract address");
|
||||||
alertStatusMsg("Not a valid contract address");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ContractDetails contractDetails = UIEthereumManager.ethereum
|
ContractDetails contractDetails = UIEthereumManager.ethereum
|
||||||
.getRepository().getContractDetails(Hex.decode(contractAddr));
|
.getRepository().getContractDetails(addr);
|
||||||
if (contractDetails == null) {
|
if (contractDetails == null) {
|
||||||
alertStatusMsg("No contract for that address");
|
alertStatusMsg("No contract for that address");
|
||||||
return;
|
return;
|
||||||
|
@ -207,6 +207,8 @@ class ContractSubmitDialog extends JDialog implements MessageAwareDialog {
|
|||||||
this.getContentPane().revalidate();
|
this.getContentPane().revalidate();
|
||||||
this.getContentPane().repaint();
|
this.getContentPane().repaint();
|
||||||
this.setResizable(false);
|
this.setResizable(false);
|
||||||
|
|
||||||
|
this.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected JRootPane createRootPane() {
|
protected JRootPane createRootPane() {
|
||||||
@ -231,7 +233,6 @@ class ContractSubmitDialog extends JDialog implements MessageAwareDialog {
|
|||||||
rootPane.getActionMap().put("ESCAPE", actionListener);
|
rootPane.getActionMap().put("ESCAPE", actionListener);
|
||||||
|
|
||||||
this.setSize(500, 430);
|
this.setSize(500, 430);
|
||||||
this.setVisible(true);
|
|
||||||
return rootPane;
|
return rootPane;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,18 +34,16 @@ public class ProgramPlayDialog extends JPanel implements ActionListener,
|
|||||||
private ProgramInvoke pi;
|
private ProgramInvoke pi;
|
||||||
|
|
||||||
public ProgramPlayDialog(byte[] code) {
|
public ProgramPlayDialog(byte[] code) {
|
||||||
this(code, new ProgramInvokeMockImpl(), null);
|
this(code, new ProgramInvokeMockImpl());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProgramPlayDialog(byte[] code, Transaction tx, Block lastBlock) {
|
public ProgramPlayDialog(byte[] code, Transaction tx, Block lastBlock) {
|
||||||
this(code,
|
this(code, ProgramInvokeFactory.createProgramInvoke(tx,
|
||||||
ProgramInvokeFactory.createProgramInvoke(tx,
|
lastBlock,
|
||||||
lastBlock,
|
WorldManager.getInstance().getRepository()));
|
||||||
WorldManager.getInstance().getRepository()),
|
|
||||||
WorldManager.getInstance().getRepository());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProgramPlayDialog(byte[] code, ProgramInvoke programInvoke, Repository tractRepository) {
|
public ProgramPlayDialog(byte[] code, ProgramInvoke programInvoke) {
|
||||||
pi = programInvoke;
|
pi = programInvoke;
|
||||||
|
|
||||||
outputList = new ArrayList<String>();
|
outputList = new ArrayList<String>();
|
||||||
@ -56,8 +54,8 @@ public class ProgramPlayDialog extends JPanel implements ActionListener,
|
|||||||
program.fullTrace();
|
program.fullTrace();
|
||||||
vm.play(program);
|
vm.play(program);
|
||||||
|
|
||||||
if(tractRepository != null)
|
if(programInvoke.getRepository() != null)
|
||||||
tractRepository.rollback();
|
programInvoke.getRepository().rollback();
|
||||||
|
|
||||||
doGUI();
|
doGUI();
|
||||||
}
|
}
|
||||||
@ -134,7 +132,7 @@ public class ProgramPlayDialog extends JPanel implements ActionListener,
|
|||||||
* this method should be invoked from the
|
* this method should be invoked from the
|
||||||
* event-dispatching thread.
|
* event-dispatching thread.
|
||||||
*/
|
*/
|
||||||
public static void createAndShowGUI(byte[] runCode, Transaction tx, Block lastBlock) {
|
public static void createAndShowGUI(byte[] runCode, final Transaction tx, Block lastBlock) {
|
||||||
|
|
||||||
final ProgramPlayDialog ppd;
|
final ProgramPlayDialog ppd;
|
||||||
if (tx != null)
|
if (tx != null)
|
||||||
@ -162,7 +160,10 @@ public class ProgramPlayDialog extends JPanel implements ActionListener,
|
|||||||
frame.addWindowListener(new java.awt.event.WindowAdapter() {
|
frame.addWindowListener(new java.awt.event.WindowAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
||||||
ppd.pi.getRepository().close();
|
if (tx == null) {
|
||||||
|
ppd.pi.getRepository().close();
|
||||||
|
ppd.pi = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -42,6 +42,7 @@ import org.ethereum.core.Block;
|
|||||||
import org.ethereum.core.Transaction;
|
import org.ethereum.core.Transaction;
|
||||||
import org.ethereum.db.ContractDetails;
|
import org.ethereum.db.ContractDetails;
|
||||||
import org.ethereum.manager.WorldManager;
|
import org.ethereum.manager.WorldManager;
|
||||||
|
import org.ethereum.util.Utils;
|
||||||
import org.ethereum.vm.DataWord;
|
import org.ethereum.vm.DataWord;
|
||||||
import org.ethereum.vm.OpCode;
|
import org.ethereum.vm.OpCode;
|
||||||
import org.ethereum.vm.Program;
|
import org.ethereum.vm.Program;
|
||||||
@ -50,6 +51,7 @@ import org.ethereum.vm.ProgramInvokeFactory;
|
|||||||
import org.ethereum.vm.Program.ProgramListener;
|
import org.ethereum.vm.Program.ProgramListener;
|
||||||
import org.spongycastle.util.encoders.DecoderException;
|
import org.spongycastle.util.encoders.DecoderException;
|
||||||
import org.spongycastle.util.encoders.Hex;
|
import org.spongycastle.util.encoders.Hex;
|
||||||
|
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
|
|
||||||
@ -121,7 +123,9 @@ public class StateExplorerWindow extends JFrame{
|
|||||||
btnSearch.addMouseListener(new MouseAdapter(){
|
btnSearch.addMouseListener(new MouseAdapter(){
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
searchAccount(txfAccountAddress.getText());
|
byte[] addr = Utils.addressStringToBytes(txfAccountAddress.getText());
|
||||||
|
if(addr != null)
|
||||||
|
searchAccount(addr);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -130,9 +134,12 @@ public class StateExplorerWindow extends JFrame{
|
|||||||
btnPlayCode.addMouseListener(new MouseAdapter(){
|
btnPlayCode.addMouseListener(new MouseAdapter(){
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
byte[] code = WorldManager.getInstance().getRepository().getCode(Hex.decode(txfAccountAddress.getText()));
|
byte[] addr = Utils.addressStringToBytes(txfAccountAddress.getText());
|
||||||
if(code != null)
|
if(addr != null) {
|
||||||
ProgramPlayDialog.createAndShowGUI(code, null, null);
|
byte[] code = WorldManager.getInstance().getRepository().getCode(addr);
|
||||||
|
if(code != null)
|
||||||
|
ProgramPlayDialog.createAndShowGUI(code, null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -202,13 +209,8 @@ public class StateExplorerWindow extends JFrame{
|
|||||||
panel.add(scrollPane);
|
panel.add(scrollPane);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void searchAccount(String accountStr){
|
private void searchAccount(byte[] add){
|
||||||
txaPrinter.clean();
|
txaPrinter.clean();
|
||||||
|
|
||||||
byte[] add = null;
|
|
||||||
try { add = Hex.decode(txfAccountAddress.getText()); }
|
|
||||||
catch(DecoderException ex) { return; }
|
|
||||||
|
|
||||||
txaPrinter.println(accountDetailsString(add, dataModel));
|
txaPrinter.println(accountDetailsString(add, dataModel));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -349,5 +351,5 @@ public class StateExplorerWindow extends JFrame{
|
|||||||
new StateExplorerWindow(null).setVisible(true);
|
new StateExplorerWindow(null).setVisible(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user