Skip to content

Commit e54b227

Browse files
committed
Add Peer backup interfaces
1 parent 43286e4 commit e54b227

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

convex-cli/src/main/java/convex/cli/peer/PeerBackup.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
description = "Backup stored data for a peer")
2121
public class PeerBackup extends APeerCommand {
2222

23-
@Option(names = { "--output-file" },
23+
@Option(names = { "--output-file","-o" },
2424
description = "Output file for peer CAD3 data. Defaults to timestamped CAD3 file.")
2525
private String outFile;
2626

@@ -60,6 +60,7 @@ protected void execute() throws InterruptedException {
6060
}
6161
}
6262

63+
// Load the peer data from store
6364
AMap<Keyword,ACell> peerData;
6465
try {
6566
peerData=convex.core.cvm.Peer.getPeerData(etchMixin.getEtchStore(), k);
@@ -70,6 +71,7 @@ protected void execute() throws InterruptedException {
7071
throw new CLIError("Unable to access peers data",e);
7172
}
7273

74+
// Write peer data to specified target file
7375
if (outFile==null) {
7476
outFile="peer-backup-"+k.toHexString(8)+"-"+Utils.timeString()+".cad3";
7577
}

convex-cli/src/main/java/convex/cli/peer/PeerStart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void execute() throws InterruptedException {
170170
if (genesisKey!=null) {
171171
AccountKey gpk=genesisKey.getAccountKey();
172172
State state=Init.createState(gpk,gpk,List.of(gpk));
173-
informWarning("Greated genesis State: "+state.getHash());
173+
informWarning("Created genesis State: "+state.getHash());
174174
config.put(Keywords.STATE, state);
175175
}
176176
Server server=API.launchPeer(config);

convex-gui/src/main/java/convex/gui/components/FilePicker.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package convex.gui.components;
22

33
import java.awt.event.ActionEvent;
4+
import java.io.File;
45

56
import javax.swing.JButton;
67
import javax.swing.JFileChooser;
78
import javax.swing.JPanel;
89
import javax.swing.JTextField;
910

11+
import convex.core.util.FileUtils;
1012
import net.miginfocom.swing.MigLayout;
1113

1214
@SuppressWarnings("serial")
@@ -35,10 +37,26 @@ public FilePicker(String filename) {
3537
add(button);
3638
}
3739

40+
public void setFileChooser(JFileChooser chooser) {
41+
this.fileChooser=chooser;
42+
}
43+
3844
public void selectFile(ActionEvent e) {
3945
int selected=fileChooser.showDialog(this, "Select");
4046
if (selected==JFileChooser.APPROVE_OPTION) {
4147
fileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
4248
}
4349
}
50+
51+
/**
52+
* Get the file selected by the file picker
53+
* @return File object, or null if not a valid selected file
54+
*/
55+
public File getFile() {
56+
try {
57+
return FileUtils.getFile(fileField.getText());
58+
} catch (Exception e) {
59+
return null;
60+
}
61+
}
4462
}

convex-gui/src/main/java/convex/gui/peer/PeerLaunchDialog.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
import javax.swing.JSpinner;
1212
import javax.swing.JTabbedPane;
1313
import javax.swing.SpinnerNumberModel;
14+
import java.awt.Component;
15+
import java.io.File;
1416

1517
import convex.core.crypto.AKeyPair;
1618
import convex.core.crypto.wallet.AWalletEntry;
1719
import convex.core.crypto.wallet.HotWalletEntry;
20+
import convex.core.data.ACell;
21+
import convex.core.data.AMap;
22+
import convex.core.data.Keyword;
1823
import convex.core.util.FileUtils;
1924
import convex.gui.components.FilePicker;
2025
import convex.gui.components.HostCombo;
@@ -24,6 +29,7 @@
2429
import convex.gui.utils.SymbolIcon;
2530
import convex.gui.utils.Toolkit;
2631
import convex.net.IPUtils;
32+
import convex.peer.Server;
2733
import net.miginfocom.swing.MigLayout;
2834

2935
public class PeerLaunchDialog {
@@ -59,7 +65,7 @@ public static void runLaunchDialog(JComponent parent) {
5965
});
6066
testNetPanel.add(randomise);
6167
testNetPanel.add(Toolkit.makeHelp("Randomise the genesis key. Fine for testing purposes."));
62-
68+
6369

6470
// Temporary peer options
6571
JPanel joinPanel=new JPanel();
@@ -79,11 +85,16 @@ public static void runLaunchDialog(JComponent parent) {
7985
FilePicker filePicker=new FilePicker(FileUtils.getFile("~/.convex/etch.db").getAbsolutePath());
8086
joinPanel.add(filePicker);
8187
joinPanel.add(Toolkit.makeHelp("Select Etch database file for peer operation."));
82-
88+
89+
// Loaded per options
90+
JPanel loadPanel=new JPanel();
91+
FilePicker backupPicker=new FilePicker(null);
92+
loadPanel.add(backupPicker);
8393

8494
JTabbedPane tabs=new JTabbedPane();
8595
tabs.add(testNetPanel,"Local Testnet");
8696
tabs.add(joinPanel,"Join Network");
97+
tabs.add(loadPanel,"Restore Backup");
8798

8899
int result = JOptionPane.showConfirmDialog(parent, tabs,
89100
"Peer Launch Details",
@@ -92,7 +103,8 @@ public static void runLaunchDialog(JComponent parent) {
92103
SymbolIcon.get(0xeb9b,Toolkit.ICON_SIZE));
93104
if (result == JOptionPane.OK_OPTION) {
94105
try {
95-
if (tabs.getSelectedComponent()==testNetPanel) {
106+
Component selected=tabs.getSelectedComponent();
107+
if (selected==testNetPanel) {
96108
int numPeers=(Integer)peerCountSpinner.getValue();
97109
AWalletEntry we=keyField.getWalletEntry();
98110
if (we==null) throw new IllegalStateException("No key pair selected");
@@ -107,7 +119,7 @@ public static void runLaunchDialog(JComponent parent) {
107119

108120
AKeyPair kp=we.getKeyPair();
109121
PeerGUI.launchPeerGUI(numPeers, kp);
110-
} else if (tabs.getSelectedComponent()==joinPanel) {
122+
} else if (selected==joinPanel) {
111123
String host=hostField.getText();
112124
InetSocketAddress sa=IPUtils.toInetSocketAddress(host);
113125
if (sa==null) throw new IllegalArgumentException("Invalid host address for joining");
@@ -116,6 +128,12 @@ public static void runLaunchDialog(JComponent parent) {
116128
if (we==null) throw new IllegalArgumentException("No peer key selected");
117129

118130
PeerGUI.launchPeerGUI(sa,we);
131+
} else if (selected==loadPanel) {
132+
File f=backupPicker.getFile();
133+
AKeyPair kp=null; // TODO;
134+
AMap<Keyword,ACell> peerData=FileUtils.loadCAD3(f.toPath());
135+
Server server=Server.fromPeerData(kp,peerData);
136+
PeerGUI.launchPeerGUI(server);
119137
}
120138
} catch (InterruptedException e) {
121139
Thread.currentThread().interrupt();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,5 +817,11 @@ public void waitForShutdown() throws InterruptedException {
817817
}
818818
}
819819

820+
public static Server fromPeerData(AKeyPair kp, AMap<Keyword,ACell> peerData) throws LaunchException, ConfigException, InterruptedException {
821+
HashMap<Keyword, Object> config=new HashMap<>();
822+
config.put(Keywords.KEYPAIR, kp);
823+
return API.launchPeer(config);
824+
}
825+
820826

821827
}

0 commit comments

Comments
 (0)