Skip to content

Commit

Permalink
WIP updates for CLI convex peer start launch
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 22, 2024
1 parent 32c2689 commit d0b4d98
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 74 deletions.
6 changes: 4 additions & 2 deletions convex-cli/src/main/java/convex/cli/etch/EtchDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public void visitCell(ACell cell) {
public void run() {
cli().setOut(outputFilename);

EtchStore store=store();
store.getEtch().visitIndex(new DumpVisitor(cli()));
try (EtchStore store=store()) {

store.getEtch().visitIndex(new DumpVisitor(cli()));
}
}
}
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/mixins/EtchMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class EtchMixin extends AMixin {
@Option(names={"-e", "--etch"},
scope = ScopeType.INHERIT,
defaultValue="${env:CONVEX_ETCH_FILE:-~/.convex/etch.db}",
description="Convex Etch database filename. Will default to CONVEX_ETCH_FILE or "+Constants.ETCH_FILENAME)
description="Etch database. Defaults to CONVEX_ETCH_FILE or "+Constants.ETCH_FILENAME)
String etchStoreFilename;

EtchStore etch=null;
Expand Down
17 changes: 6 additions & 11 deletions convex-cli/src/main/java/convex/cli/peer/PeerList.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package convex.cli.peer;

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

import convex.cli.CLIError;
import convex.cli.ExitCodes;
import convex.core.data.ACell;
import convex.core.data.AMap;
import convex.core.data.AccountKey;
import convex.peer.API;
import etch.EtchStore;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;
Expand All @@ -28,15 +29,9 @@ public void run() {

EtchStore etch=etchMixin.getEtchStore();
try {
AMap<ACell,ACell> rootData=etch.getRootData();
if (rootData==null) {
informWarning("No root data in store "+etch);
return;
}

long n=rootData.count();
for (long i=0; i<n; i++) {
println(rootData.entryAt(i).getKey());
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);
Expand Down
95 changes: 80 additions & 15 deletions convex-cli/src/main/java/convex/cli/peer/PeerStart.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package convex.cli.peer;

import java.util.HashMap;
import java.util.List;

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

import convex.cli.CLIError;
import convex.cli.ExitCodes;
import convex.cli.mixins.RemotePeerMixin;
import convex.core.crypto.AKeyPair;
import convex.core.exceptions.TODOException;
import convex.core.data.AccountKey;
import convex.core.data.Address;
import convex.core.data.Keyword;
import convex.core.data.Keywords;
import convex.peer.API;
import convex.peer.ConfigException;
import convex.peer.Server;
import etch.EtchStore;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Model.CommandSpec;
Expand Down Expand Up @@ -56,27 +67,81 @@ public class PeerStart extends APeerCommand {
protected RemotePeerMixin peerMixin;

@Option(names = { "-a", "--address" }, description = "Account address to use for the peer controller.")
private long addressNumber;
private String controllerAddress;



@Override
public void run() {
storeMixin.ensureKeyStore();
AKeyPair peerKey=checkPeerKey();
private AKeyPair findPeerKey(EtchStore store) {
// First check user supplied peer key. If we have it, use it
AKeyPair kp=checkPeerKey();
if (kp!=null) return kp;

// if user specified a --peer-key, but it wasn't found in keystore
String specifiedKey=peerKeyMixin.getPublicKey();
if (specifiedKey!=null) {
throw new CLIError(ExitCodes.CONFIG,"Peer key not found in Store: "+specifiedKey);
}

log.debug("Preparing to start peer: "+peerKey.getAccountKey());
// In strict mode, we insist on a peer key
paranoia("--peer-key not sepcified");

log.debug("--peer-key not available, attempting to infer from store");
try {
List<AccountKey> peerList=API.listPeers(store);
if (peerList.size()==0) {
throw new CLIError(ExitCodes.CONFIG,"No peers configured in Etch store "+store+". Consider using `convex peer create` or `convex peer genesis` first.");
} else if (peerList.size()>1) {
throw new CLIError(ExitCodes.CONFIG,"Multiple peers configured in Etch store "+store+". specify which one you want with --peer-key.");
}
AccountKey peerKey=peerList.get(0);
AKeyPair pkp=storeMixin.loadKeyFromStore(peerKey.toHexString(), peerKeyMixin.getKeyPassword());
return pkp;
} catch (Exception e) {
log.debug("Exception trying to read etch peer list",e);
}

return null;
}

throw new TODOException();
// peerManager = PeerManager.create(mainParent.getSessionFilename(), keyPair, peerAddress, store);
// peerManager.launchPeer(port, remotePeerHostname, url, bindAddress);
// peerManager.showPeerEvents();
} catch (Exception t) {
throw new CLIError("Error starting peer: "+t.getMessage(),t);
@Override
public void run() {
storeMixin.ensureKeyStore();
try (EtchStore store = etchMixin.getEtchStore()) {
AKeyPair peerKey=findPeerKey(store);
if (peerKey==null) {
informWarning("No --peer-key specified or inferred from Etch Store.");
showUsage();
return;
}

inform("Preparing to start peer: "+peerKey.getAccountKey());

Address controller=Address.parse(controllerAddress);
if (controller==null) {
paranoia("--address for peer controller not specified");
log.debug("Controller address not specified.");

}

try {
HashMap<Keyword,Object> config=new HashMap<>();
config.put(Keywords.KEYPAIR, peerKey);
config.put(Keywords.STORE, store);
Server s=API.launchPeer(config);
while (s.isRunning()) {
Thread.sleep(400);
}
informSuccess("Peer shutdown completed");
} catch (ConfigException t) {
throw new CLIError(ExitCodes.CONFIG,"Error in peer configuration: "+t.getMessage(),t);
} catch (InterruptedException e) {
informWarning("Peer interrupted before normal shutdown");
return;
}
}
}






}
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/data/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public static Address fromHex(String hexString) {
* @return Address parsed, or null if not valid
*/
public static Address parse(String s) {
if (s==null) return null;
s=s.trim();
if (s.startsWith("#")) {
s=s.substring(1);
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/store/AStore.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package convex.core.store;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.Consumer;

Expand All @@ -18,7 +19,7 @@
* made" ― Robert C. Martin
*
*/
public abstract class AStore {
public abstract class AStore implements Closeable {

/**
* Stores a @Ref in long term storage as defined by this store implementation.
Expand Down
8 changes: 6 additions & 2 deletions convex-core/src/main/java/convex/core/util/Shutdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ public synchronized void addHook(Runnable r) {
public synchronized void runHooks() {
Collection<Runnable> hooks=hookSet.keySet();
hooks.stream().forEach(r->{
r.run();
});
try {
r.run();
} catch (Throwable t) {
t.printStackTrace();
}
});
hookSet.clear();
}

Expand Down
39 changes: 18 additions & 21 deletions convex-core/src/main/java/etch/Etch.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ private Etch(File dataFile) throws IOException {
}

// shutdown hook to close file / release lock
convex.core.util.Shutdown.addHook(Shutdown.ETCH,new Runnable() {
public void run() {
close();
}
});
convex.core.util.Shutdown.addHook(Shutdown.ETCH,this::close);
}

/**
Expand Down Expand Up @@ -590,23 +586,24 @@ protected void truncateFile() throws FileNotFoundException, IOException {
* Close all files resources with this Etch store, including writing the final
* data length.
*/
synchronized void close() {
void close() {
if (!(data.getChannel().isOpen())) return; // already closed
try {
// Update data length
writeDataLength();

// Send writes to disk
flush();

regionMap.clear();
System.gc();

data.close();

log.trace("Etch closed on file: "+ getFileName() +" with data length: "+dataLength);
} catch (IOException e) {
log.error("Error closing Etch file: "+file);
synchronized(this) {
try {
// Update data length
writeDataLength();

// Send writes to disk
flush();

regionMap.clear();

data.close();

log.debug("Etch closed on file: "+ getFileName() +" with data length: "+dataLength);
} catch (Exception e) {
log.error("Error closing Etch file: "+file,e);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions convex-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<artifactId>convex-peer</artifactId>
<version>${convex.version}</version>
</dependency>

<dependency>
<groupId>world.convex</groupId>
<artifactId>convex-core</artifactId>
<version>${convex.version}</version>
</dependency>

<dependency>
<groupId>world.convex</groupId>
Expand Down
23 changes: 10 additions & 13 deletions convex-gui/src/main/java/convex/gui/etch/DatabasePanel.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package convex.gui.etch;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

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

import java.awt.Color;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import convex.gui.components.ActionPanel;
import etch.EtchStore;

import javax.swing.JButton;
import javax.swing.JFileChooser;

@SuppressWarnings("serial")
public class DatabasePanel extends JPanel {

Expand All @@ -43,7 +38,9 @@ public DatabasePanel(EtchExplorer explorer) {
panel.setLayout(new GridLayout(0, 1, 0, 0));

JPanel filePanel = new JPanel();
filePanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "File", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
//Border eb=new EtchedBorder(EtchedBorder.LOWERED, Color.WHITE, new Color(160, 160, 160));
//Border b = new TitledBorder(eb, "File", TitledBorder.LEADING, TitledBorder.TOP, (Font)null, Color.BLACK);
//filePanel.setBorder(b);
panel.add(filePanel);
filePanel.setLayout(new BorderLayout(0, 0));

Expand Down
26 changes: 26 additions & 0 deletions convex-peer/src/main/java/convex/peer/API.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package convex.peer;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -11,10 +12,14 @@

import convex.core.State;
import convex.core.crypto.AKeyPair;
import convex.core.data.ACell;
import convex.core.data.AMap;
import convex.core.data.AccountKey;
import convex.core.data.Keyword;
import convex.core.data.Keywords;
import convex.core.data.Lists;
import convex.core.init.Init;
import convex.core.lang.RT;
import convex.core.store.AStore;
import convex.core.store.Stores;
import convex.core.util.Utils;
Expand Down Expand Up @@ -186,4 +191,25 @@ public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genes

return serverList;
}

/**
* Gets the list of peers registered in the given Etch Store
* @param store
* @return null if peer list not present
* @throws IOException
*/
public static List<AccountKey> listPeers(AStore store) throws IOException {
AMap<ACell,ACell> data=store.getRootData();

ArrayList<AccountKey> results=new ArrayList<>();

long n=data.count();
for (int i=0; i<n; i++) {
ACell k=data.entryAt(i).getKey();
AccountKey ak = RT.ensureAccountKey(k);
if (ak!=null) results.add(ak);
}

return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public abstract class AThreadedComponent {

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

protected final Server server;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected boolean maybeUpdateBelief(Belief newBelief) throws InterruptedExceptio
belief=belief.proposeBlock(server.getKeyPair(),signedBlock);
if (log.isDebugEnabled()) {
Block bl=signedBlock.getValue();
log.debug("New block proposed: {} transaction(s), size= {}, hash={}", bl.getTransactions().count(), signedBlock.getMemorySize(),signedBlock.getHash());
log.debug("Block proposed: {} tx(s), size={}, hash={}", bl.getTransactions().count(), signedBlock.getMemorySize(),signedBlock.getHash());
}
published=true;
}
Expand Down
Loading

0 comments on commit d0b4d98

Please sign in to comment.