Skip to content

Commit

Permalink
Improve handling of controller wallets in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 21, 2024
1 parent 628656e commit fd40603
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 43 deletions.
20 changes: 20 additions & 0 deletions convex-gui/src/main/java/convex/gui/components/ConnectPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convex.gui.components;

import java.awt.Color;
import java.awt.Component;
import java.net.InetSocketAddress;

import javax.swing.JComponent;
Expand All @@ -16,6 +17,7 @@
import convex.core.crypto.wallet.AWalletEntry;
import convex.core.cvm.Address;
import convex.core.init.Init;
import convex.core.lang.RT;
import convex.gui.components.account.AddressCombo;
import convex.gui.keys.KeyRingPanel;
import convex.gui.keys.UnlockWalletDialog;
Expand Down Expand Up @@ -113,4 +115,22 @@ public static Convex tryConnect(JComponent parent,String prompt) {
}
return null;
}

public static void showConnectionInfo(Component parent,Convex convex) {
StringBuilder sb=new StringBuilder();
if (convex instanceof ConvexRemote) {
sb.append("Remote host: " + convex.getHostAddress() + "\n");
}
try {
sb.append("Sequence: " + convex.getSequence() + "\n");
} catch (Exception e1) {
log.info("Failed to get sequence number");
}
sb.append("Account: " + RT.print(convex.getAddress()) + "\n");
sb.append("Public Key: " + RT.toString(convex.getAccountKey()) + "\n");
sb.append("Connected: " + convex.isConnected()+"\n");

String infoString = sb.toString();
JOptionPane.showMessageDialog(parent, infoString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import convex.core.data.AccountKey;
import convex.core.exceptions.ResultException;
import convex.gui.components.BalanceLabel;
import convex.gui.components.ConnectPanel;
import convex.gui.components.DropdownMenu;
import convex.gui.keys.KeyRingPanel;
import convex.gui.keys.UnlockWalletDialog;
Expand All @@ -43,6 +44,9 @@ public class AccountChooserPanel extends JPanel {
private BalanceLabel balanceLabel;

protected Convex convex;

AWalletEntry previousSelection = null;


public AccountChooserPanel(Convex convex) {
this.convex=convex;
Expand All @@ -69,13 +73,11 @@ public AccountChooserPanel(Convex convex) {
keyCombo=KeyPairCombo.forConvex(convex);
keyCombo.setToolTipText("Select a key pair from your Keyring. This will be used to sign transactions.");
keyCombo.addItemListener(e->{
AWalletEntry we=(AWalletEntry)e.getItem();
if (e.getStateChange()==ItemEvent.DESELECTED) {
// key pair was deselected and/or set to null
setKeyPair(null);
previousSelection=we;
return;
};
AWalletEntry we=(AWalletEntry)e.getItem();
if (we!=keyCombo.getWalletEntry()) {
} else {
setKeyPair(we);
}
});
Expand Down Expand Up @@ -126,7 +128,7 @@ public AccountChooserPanel(Convex convex) {
});
popupMenu.add(setSeqButton);

JMenuItem reconnectButton = new JMenuItem("Reconnect",Toolkit.menuIcon(0xe9d5));
JMenuItem reconnectButton = new JMenuItem("Reconnect",Toolkit.menuIcon(0xe157));
reconnectButton.addActionListener(e -> {
try {
convex.reconnect();
Expand All @@ -135,6 +137,16 @@ public AccountChooserPanel(Convex convex) {
}
});
popupMenu.add(reconnectButton);

JMenuItem connInfoButton = new JMenuItem("Connection Info...",Toolkit.menuIcon(0xe157));
connInfoButton.addActionListener(e -> {
try {
ConnectPanel.showConnectionInfo(this, convex);
} catch (Exception ex) {
log.info("Reconnect failed",ex);
}
});
popupMenu.add(connInfoButton);



Expand Down Expand Up @@ -186,6 +198,12 @@ public void updateAddress(Address a) {
}

public void setKeyPair(AWalletEntry we) {
// In case we are re-entering
if (we!=keyCombo.getWalletEntry()) {
keyCombo.setSelectedItem(we);
return;
}

System.err.println("Setting wallet entry:" +we);
if (we==null) {
convex.setKeyPair(null);
Expand All @@ -194,18 +212,16 @@ public void setKeyPair(AWalletEntry we) {
if (we.isLocked()) {
boolean unlock=UnlockWalletDialog.offerUnlock(this, we);
if (!unlock) {
convex.setKeyPair(null);
keyCombo.setSelectedItem(null);
keyCombo.setSelectedItem(previousSelection);
return;
} else {
kp=we.getKeyPair();
we.lock();
}

kp=we.getKeyPair();
we.lock();
} else {
kp=we.getKeyPair();
}
convex.setKeyPair(kp);
keyCombo.setSelectedItem(kp);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static void main (String... args) {
}

public AWalletEntry getWalletEntry() {
Object a = getSelectedItem();
Object a = getModel().getSelectedItem();
return (AWalletEntry)a;
}

Expand Down
28 changes: 26 additions & 2 deletions convex-gui/src/main/java/convex/gui/peer/PeerComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import convex.api.Convex;
import convex.api.ConvexLocal;
import convex.api.ConvexRemote;
import convex.core.crypto.AKeyPair;
import convex.core.crypto.wallet.AWalletEntry;
import convex.core.cvm.Address;
import convex.core.cvm.Peer;
import convex.core.cvm.PeerStatus;
Expand All @@ -25,6 +29,7 @@
import convex.gui.components.DropdownMenu;
import convex.gui.components.Identicon;
import convex.gui.etch.EtchWindow;
import convex.gui.keys.KeyRingPanel;
import convex.gui.models.StateModel;
import convex.gui.repl.REPLClient;
import convex.gui.server.PeerWindow;
Expand All @@ -40,12 +45,31 @@ public class PeerComponent extends BaseListComponent {

public ConvexLocal convex;
CodeLabel description;

private static final Logger log = LoggerFactory.getLogger(PeerComponent.class.getName());


public void launchPeerWindow(ConvexLocal peer) {
PeerWindow pw = new PeerWindow(peer);
Server server=peer.getLocalServer();
ConvexLocal newConvex=connectLocalControllerWallet(server);
PeerWindow pw = new PeerWindow(newConvex);
pw.run();
}

private ConvexLocal connectLocalControllerWallet(Server server) {
AKeyPair kp=null;
Address controller=server.getPeerController();
if (controller!=null) try {
AccountKey key=server.getPeer().getConsensusState().getAccount(controller).getAccountKey();
AWalletEntry we=KeyRingPanel.getKeyRingEntry(key);
kp=we.getKeyPair();
} catch (Exception e) {
log.warn("Error getting controller details",e);
}
ConvexLocal convex=ConvexLocal.connect(server,controller,kp);
return convex;
}

public void launchEtchWindow(ConvexLocal peer) {
EtchWindow ew = new EtchWindow(peer);
ew.run();
Expand Down Expand Up @@ -139,7 +163,7 @@ public PeerComponent(ConvexLocal value) {

JMenuItem walletButton = new JMenuItem("Open controller Wallet",Toolkit.menuIcon(0xe850));
walletButton.addActionListener(e -> {
new WalletApp(convex).run();
new WalletApp(connectLocalControllerWallet(server)).run();
});
popupMenu.add(walletButton);

Expand Down
23 changes: 4 additions & 19 deletions convex-gui/src/main/java/convex/gui/repl/REPLPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
Expand All @@ -27,26 +26,26 @@
import org.slf4j.LoggerFactory;

import convex.api.Convex;
import convex.api.ConvexRemote;
import convex.core.Result;
import convex.core.crypto.AKeyPair;
import convex.core.cvm.Address;
import convex.core.cvm.Symbols;
import convex.core.cvm.transactions.ATransaction;
import convex.core.data.ACell;
import convex.core.data.AList;
import convex.core.data.AString;
import convex.core.data.AVector;
import convex.core.cvm.Address;
import convex.core.data.SignedData;
import convex.core.exceptions.ParseException;
import convex.core.exceptions.ResultException;
import convex.core.lang.RT;
import convex.core.lang.Reader;
import convex.core.cvm.Symbols;
import convex.core.util.Utils;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.BaseTextPane;
import convex.gui.components.CodePane;
import convex.gui.components.ConnectPanel;
import convex.gui.components.account.AccountChooserPanel;
import convex.gui.utils.CVXHighlighter;
import convex.gui.utils.Toolkit;
Expand Down Expand Up @@ -211,21 +210,7 @@ public REPLPanel(Convex convex) {
actionPanel.add(btnClear);

btnInfo = new ActionButton("Connection Info",0xe88e,e -> {
StringBuilder sb=new StringBuilder();
if (convex instanceof ConvexRemote) {
sb.append("Remote host: " + convex.getHostAddress() + "\n");
}
try {
sb.append("Sequence: " + convex.getSequence() + "\n");
} catch (Exception e1) {
log.info("Failed to get sequence number");
}
sb.append("Account: " + RT.print(convex.getAddress()) + "\n");
sb.append("Public Key: " + RT.toString(convex.getAccountKey()) + "\n");
sb.append("Connected: " + convex.isConnected()+"\n");

String infoString = sb.toString();
JOptionPane.showMessageDialog(this, infoString);
ConnectPanel.showConnectionInfo(REPLPanel.this,convex);
});
actionPanel.setToolTipText("Show diagnostic information for the Convex connection");
actionPanel.add(btnInfo);
Expand Down
16 changes: 8 additions & 8 deletions convex-gui/src/main/java/convex/gui/server/PeerWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ public Convex getPeerView() {

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

public PeerWindow(ConvexLocal peer) {
super("Peer Control Panel - " + peer.toString());
this.peer = peer;
public PeerWindow(ConvexLocal convex) {
super("Peer Control Panel - " + convex.toString());
this.peer = convex;
this.setPreferredSize(new Dimension(1200,1000));

setLayout(new MigLayout());
add(tabbedPane, "dock center");

Server server=peer.getLocalServer();
Server server=convex.getLocalServer();
if (server!=null) {
try {
// Convex convex = Convex.connect(server.getHostAddress(), server.getPeerController(),server.getKeyPair());
tabbedPane.addTab("REPL", null, new REPLPanel(peer), null);
tabbedPane.addTab("REPL", null, new REPLPanel(convex), null);
} catch (Exception t) {
String msg=("Failed to connect to Peer: "+t);
t.printStackTrace();
Expand All @@ -55,10 +55,10 @@ public PeerWindow(ConvexLocal peer) {
}
tabbedPane.addTab("Observation", null, new JScrollPane(new ObserverPanel(server)), null);
}
tabbedPane.addTab("Stress", null, new StressPanel(peer), null);
tabbedPane.addTab("Info", null, new PeerInfoPanel(peer), null);
tabbedPane.addTab("Stress", null, new StressPanel(convex), null);
tabbedPane.addTab("Info", null, new PeerInfoPanel(convex), null);

PeerComponent pcom = new PeerComponent(peer);
PeerComponent pcom = new PeerComponent(convex);
add(pcom, "dock north");
}

Expand Down
2 changes: 1 addition & 1 deletion convex-peer/src/main/java/convex/peer/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public long getBeliefReceivedCount() {

/**
* Gets the Peer controller Address
* @return Peer controller Address
* @return Peer controller Address, or null if peer is not registered
*/
public Address getPeerController() {
return getPeer().getController();
Expand Down

0 comments on commit fd40603

Please sign in to comment.