Skip to content

Commit

Permalink
Add convex peer backup CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 13, 2025
1 parent 647ab20 commit 43286e4
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 29 deletions.
14 changes: 14 additions & 0 deletions convex-cli/src/main/java/convex/cli/mixins/EtchMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

import convex.cli.CLIError;
import convex.cli.Constants;
import convex.core.data.AccountKey;
import convex.core.util.FileUtils;
import convex.etch.EtchStore;
import convex.peer.API;
import picocli.CommandLine.Option;
import picocli.CommandLine.ScopeType;

Expand Down Expand Up @@ -46,4 +49,15 @@ public synchronized EtchStore getEtchStore(String fileName) {
public EtchStore getEtchStore() {
return getEtchStore(etchStoreFilename);
}

public List<AccountKey> getPeerList() {
EtchStore etchStore=getEtchStore();

try {
List<AccountKey> keys=API.listPeers(getEtchStore());
return keys;
} catch (IOException e) {
throw new CLIError("Unable to list peers in store: "+etchStore);
}
}
}
9 changes: 9 additions & 0 deletions convex-cli/src/main/java/convex/cli/mixins/PeerKeyMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import convex.core.data.AccountKey;
import picocli.CommandLine.Option;
import picocli.CommandLine.ScopeType;

Expand Down Expand Up @@ -58,4 +59,12 @@ public char[] getKeyPassword() {
}
return keypass;
}

public AccountKey getAcountKey() {
String ks=getPublicKey();
AccountKey result= AccountKey.parse(ks);
return result;
}


}
1 change: 1 addition & 0 deletions convex-cli/src/main/java/convex/cli/peer/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
PeerCreate.class,
PeerStart.class,
PeerList.class,
PeerBackup.class,
PeerGenesis.class,
Help.class
},
Expand Down
86 changes: 86 additions & 0 deletions convex-cli/src/main/java/convex/cli/peer/PeerBackup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package convex.cli.peer;

import java.io.File;
import java.io.IOException;
import java.util.List;

import convex.cli.CLIError;
import convex.core.data.ACell;
import convex.core.data.AMap;
import convex.core.data.AccountKey;
import convex.core.data.Keyword;
import convex.core.util.FileUtils;
import convex.core.util.Utils;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(
name = "backup",
mixinStandardHelpOptions = true,
description = "Backup stored data for a peer")
public class PeerBackup extends APeerCommand {

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


@SuppressWarnings("resource")
@Override
protected void execute() throws InterruptedException {

AccountKey k= peerKeyMixin.getAcountKey();

if (k==null) {
List<AccountKey> peerList=etchMixin.getPeerList();
int n=peerList.size();
if (n==0) {
throw new CLIError("No peers available in store: "+etchMixin.getEtchStore());
}

String s=peerKeyMixin.getPublicKey();
if (s!=null) {
peerList.removeIf(pk->!pk.toHexString().startsWith(s));
n=peerList.size();
}

if (n==0) {
throw new CLIError("No peer in store with prefix: "+s);
} if (n==1) {
k=peerList.get(0);
} else if (n==0) {
throw new CLIError("No peers available in store: "+etchMixin.getEtchStore());
} else {
informWarning("Need to select peer to backup, available peers are:");
for (AccountKey pk: peerList) {
inform(pk.toHexString());
}

throw new CLIError("Please specify which peer to backup with --peer-key");
}
}

AMap<Keyword,ACell> peerData;
try {
peerData=convex.core.cvm.Peer.getPeerData(etchMixin.getEtchStore(), k);
if (peerData==null) {
throw new CLIError("No peer data found for key: "+k);
}
} catch (Exception e) {
throw new CLIError("Unable to access peers data",e);
}

if (outFile==null) {
outFile="peer-backup-"+k.toHexString(8)+"-"+Utils.timeString()+".cad3";
}
File f=FileUtils.getFile(outFile);

try {
FileUtils.writeCAD3(f.toPath(), peerData);
inform("Peer data written to: "+f);
} catch (IOException e) {
throw new CLIError("Unable to write peer data",e);
}
}

}
21 changes: 4 additions & 17 deletions convex-cli/src/main/java/convex/cli/peer/PeerList.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package convex.cli.peer;

import java.io.IOException;
import java.util.List;

import convex.cli.CLIError;
import convex.cli.ExitCodes;
import convex.core.data.AccountKey;
import convex.etch.EtchStore;
import convex.peer.API;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;

Expand All @@ -25,18 +20,10 @@ public class PeerList extends APeerCommand {


@Override
public void execute() {

EtchStore etch=etchMixin.getEtchStore();
try {
List<AccountKey> keys=API.listPeers(etch);
for (AccountKey k: keys) {
println(k.toHexString());
}
} catch (IOException e) {
throw new CLIError(ExitCodes.IOERR,"IO Error reating etch store at "+etch,e);
} finally {
etch.close();
public void execute() {
List<AccountKey> keys=etchMixin.getPeerList();
for (AccountKey k: keys) {
println(k.toHexString());
}
}
}
10 changes: 0 additions & 10 deletions convex-core/src/main/java/convex/core/cvm/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,6 @@ public static Peer restorePeer(AStore store, AKeyPair keyPair, ACell rootKey) th
Peer peer=Peer.fromData(keyPair,peerData);
return peer;
}

/**
* Like {@link #getPeerData(AStore, ACell)} but uses a null root key.
* @param store store from which to load Peer data
* @return Peer data map
* @throws IOException In case of IOException
*/
public static AMap<Keyword, ACell> getPeerData(AStore store) throws IOException {
return getPeerData(store, null);
}

/**
* Gets Peer Data from a Store.
Expand Down
4 changes: 2 additions & 2 deletions convex-peer/src/main/java/convex/peer/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genes
/**
* Gets the list of peers registered in the given Etch Store
* @param store Store from which to read peers
* @return null if peer list not present
* @return A new ArrayList of keys, or null if peer list not present
* @throws IOException in case of IO error reading peers from store
*/
public static List<AccountKey> listPeers(AStore store) throws IOException {
public static ArrayList<AccountKey> listPeers(AStore store) throws IOException {
AMap<ACell,ACell> data=store.getRootData();
ArrayList<AccountKey> results=new ArrayList<>();
if (data==null) return results;
Expand Down

0 comments on commit 43286e4

Please sign in to comment.