Skip to content

Commit

Permalink
Merge pull request #224 from semuxgo/gui-transaction-result
Browse files Browse the repository at this point in the history
GUI: add transaction result dialog
  • Loading branch information
semuxgo authored Jul 18, 2019
2 parents cccdaad + 04c1ec5 commit 07a0094
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/semux/api/util/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class TransactionBuilder {
*/
private byte[] data;

private long gas = 0;
private Long gas = null;
private Amount gasPrice = Amount.ZERO;

public TransactionBuilder(Kernel kernel) {
Expand Down Expand Up @@ -279,7 +279,7 @@ public Transaction buildUnsigned() {
Long gas = this.gas;
if (gas == null) {
if (type != TransactionType.CALL && type != TransactionType.CREATE) {
gasPrice = Amount.ZERO;
gas = 0L;
} else {
throw new IllegalArgumentException("Parameter `gas` is required");
}
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/semux/consensus/SemuxSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.vm.client.BlockStore;
import org.semux.Kernel;
import org.semux.config.Config;
import org.semux.core.Block;
Expand All @@ -47,7 +46,6 @@
import org.semux.net.msg.consensus.BlockPartsMessage;
import org.semux.net.msg.consensus.GetBlockMessage;
import org.semux.util.TimeUtil;
import org.semux.vm.client.SemuxBlockStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -84,11 +82,9 @@ public Thread newThread(Runnable r) {

private static final Random random = new Random();

private Kernel kernel;
private Config config;

private Blockchain chain;
private BlockStore blockStore;
private ChannelManager channelMgr;

// task queues
Expand All @@ -114,11 +110,9 @@ public Thread newThread(Runnable r) {
private final AtomicBoolean isRunning = new AtomicBoolean(false);

public SemuxSync(Kernel kernel) {
this.kernel = kernel;
this.config = kernel.getConfig();

this.chain = kernel.getBlockchain();
this.blockStore = new SemuxBlockStore(chain);
this.channelMgr = kernel.getChannelManager();

this.DOWNLOAD_TIMEOUT = config.syncDownloadTimeout();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/semux/gui/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@ public enum Action {

EXPORT_PRIVATE_KEY,

COPY_PRIVATE_KEY
COPY_PRIVATE_KEY,

SHOW_TRANSACTION_RESULT
}
106 changes: 50 additions & 56 deletions src/main/java/org/semux/gui/dialog/TransactionDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,43 @@
*/
package org.semux.gui.dialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;

import org.ethereum.vm.util.HashUtil;
import org.semux.core.Transaction;
import org.semux.core.TransactionResult;
import org.semux.core.TransactionType;
import org.semux.crypto.Hex;
import org.semux.gui.Action;
import org.semux.gui.SwingUtil;
import org.semux.message.GuiMessages;
import org.semux.util.exception.UnreachableException;

public class TransactionDialog extends JDialog {
public class TransactionDialog extends JDialog implements ActionListener {

private static final long serialVersionUID = 1L;

private JFrame parent;
private Transaction tx;
private TransactionResult result;

public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result) {
super(null, GuiMessages.get("Transaction"), ModalityType.MODELESS);
setName("TransactionDialog");

this.parent = parent;
this.tx = tx;
this.result = result;

JLabel lblHash = new JLabel(GuiMessages.get("Hash") + ":");
JLabel lblType = new JLabel(GuiMessages.get("Type") + ":");
JLabel lblFrom = new JLabel(GuiMessages.get("From") + ":");
Expand All @@ -40,14 +52,9 @@ public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result
JLabel lblNonce = new JLabel(GuiMessages.get("Nonce") + ":");
JLabel lblTimestamp = new JLabel(GuiMessages.get("Timestamp") + ":");
JLabel lblData = new JLabel(GuiMessages.get("Data") + ":");

JLabel lblSucceeded = new JLabel(GuiMessages.get("Success"));
JLabel lblGas = new JLabel(GuiMessages.get("Gas"));
JLabel lblGasPrice = new JLabel(GuiMessages.get("GasPrice"));
JLabel lblGasProvided = new JLabel(GuiMessages.get("GasProvided"));
JLabel lblGasUsed = new JLabel(GuiMessages.get("GasUsed"));
JLabel lblOutput = new JLabel(GuiMessages.get("Output"));

String notAvailable = GuiMessages.get("NotAvailable");
JLabel lblTransactionResult = new JLabel(GuiMessages.get("TransactionResult"));

JTextArea hash = SwingUtil.textAreaWithCopyPopup(Hex.encode0x(tx.getHash()));
hash.setName("hashText");
Expand All @@ -56,11 +63,6 @@ public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result
JTextArea from = SwingUtil.textAreaWithCopyPopup(Hex.encode0x(tx.getFrom()));
from.setName("fromText");
JTextArea to = SwingUtil.textAreaWithCopyPopup(Hex.encode0x(tx.getTo()));

// for creates, the TO value should display the contract address
if (tx.getType() == TransactionType.CREATE) {
to.setText(Hex.encode0x(HashUtil.calcNewAddress(tx.getFrom(), tx.getNonce())));
}
to.setName("toText");
JLabel value = new JLabel(SwingUtil.formatAmount((tx.getValue())));
value.setName("valueText");
Expand All @@ -70,29 +72,17 @@ public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result
nonce.setName("nonceText");
JLabel timestamp = new JLabel(SwingUtil.formatTimestamp(tx.getTimestamp()));
timestamp.setName("timestampText");

JLabel gasPrice = new JLabel(SwingUtil.formatAmount(tx.getGasPrice()));
gasPrice.setName("gasPriceText");

JLabel gasProvided = new JLabel(SwingUtil.formatNumber(tx.getGas()));
gasProvided.setName("gasProvidedText");

JLabel success = new JLabel(result == null ? notAvailable : Boolean.toString(result.getCode().isSuccess()));

String gasUsedValue = result == null ? notAvailable : SwingUtil.formatNumber(result.getGasUsed());
JLabel gasUsed = new JLabel(gasUsedValue);
gasUsed.setName("gasUsedText");

String outputValue = result == null ? notAvailable : Hex.encode0x(result.getReturnData());
JTextArea output = SwingUtil.textAreaWithCopyPopup(outputValue);
output.setName("outputText");
output.setLineWrap(true);
JScrollPane outputScroll = new JScrollPane(output);

JTextArea data = SwingUtil.textAreaWithCopyPopup(Hex.encode0x(tx.getData()));
data.setName("dataText");
data.setLineWrap(true);
JScrollPane dataScroll = new JScrollPane(data);
JLabel gas = new JLabel(SwingUtil.formatNumber(tx.getGas()));
gas.setName("gasText");
JLabel gasPrice = new JLabel(SwingUtil.formatAmount(tx.getGasPrice()));
gasPrice.setName("gasPriceText");
JButton display = SwingUtil
.createDefaultButton(GuiMessages.get("Display"), this, Action.SHOW_TRANSACTION_RESULT);
display.setName("display");

// @formatter:off
GroupLayout groupLayout = new GroupLayout(getContentPane());
Expand All @@ -101,11 +91,9 @@ public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result
.addGroup(groupLayout.createSequentialGroup()
.addGap(42)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(lblOutput)
.addComponent(lblGasUsed)
.addComponent(lblSucceeded)
.addComponent(lblGasProvided)
.addComponent(lblTransactionResult)
.addComponent(lblGasPrice)
.addComponent(lblGas)
.addComponent(lblData)
.addComponent(lblTimestamp)
.addComponent(lblNonce)
Expand All @@ -126,11 +114,9 @@ public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result
.addComponent(nonce)
.addComponent(timestamp)
.addComponent(dataScroll, GroupLayout.PREFERRED_SIZE, 450, GroupLayout.PREFERRED_SIZE)
.addComponent(gas)
.addComponent(gasPrice)
.addComponent(gasProvided)
.addComponent(success)
.addComponent(gasUsed)
.addComponent(outputScroll, GroupLayout.PREFERRED_SIZE, 450, GroupLayout.PREFERRED_SIZE))
.addComponent(display))
.addContainerGap(19, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
Expand Down Expand Up @@ -174,24 +160,16 @@ public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result
.addComponent(dataScroll, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblGasPrice)
.addComponent(gasPrice))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblGasProvided)
.addComponent(gasProvided))
.addComponent(lblGas)
.addComponent(gas))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSucceeded)
.addComponent(success))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblGasUsed)
.addComponent(gasUsed))
.addComponent(lblGasPrice)
.addComponent(gasPrice))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblOutput)
.addComponent(outputScroll, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
.addComponent(lblTransactionResult)
.addComponent(display))
.addContainerGap(20, Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
Expand All @@ -204,4 +182,20 @@ public TransactionDialog(JFrame parent, Transaction tx, TransactionResult result
this.setLocationRelativeTo(parent);
this.setResizable(false);
}

@Override
public synchronized void actionPerformed(ActionEvent e) {
Action action = Action.valueOf(e.getActionCommand());

switch (action) {
case SHOW_TRANSACTION_RESULT:
if (result != null) {
TransactionResultDialog dialog = new TransactionResultDialog(parent, tx, result);
dialog.setVisible(true);
}
break;
default:
throw new UnreachableException();
}
}
}
Loading

0 comments on commit 07a0094

Please sign in to comment.