Skip to content

Commit

Permalink
New PeerException types for better safety
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 28, 2024
1 parent 7395be1 commit 2e39885
Show file tree
Hide file tree
Showing 26 changed files with 132 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package convex.benchmarks;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -13,7 +12,6 @@
import org.openjdk.jmh.runner.options.Options;

import convex.api.Convex;
import convex.core.exceptions.ResultException;
import convex.core.Coin;
import convex.core.Result;
import convex.core.crypto.AKeyPair;
Expand Down Expand Up @@ -56,11 +54,12 @@ public class LatencyBenchmark {

client=Convex.connect(server.getHostAddress(), HERO,KPS[0]);
client2=Convex.connect(server.getHostAddress(), VILLAIN,KPS[1]);
} catch (ResultException | IOException | TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} catch (Exception e) {
e.printStackTrace();
throw new Error(e);
}

}

Expand Down
3 changes: 3 additions & 0 deletions convex-cli/src/main/java/convex/cli/local/LocalGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convex.cli.local;

import convex.cli.CLIError;
import convex.peer.PeerException;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;

Expand Down Expand Up @@ -31,6 +32,8 @@ public void run() {
} catch (InterruptedException t) {
Thread.currentThread().interrupt();
throw new CLIError("Interrupted while running GUI",t);
} catch (PeerException e) {
throw new CLIError("Peer launch failed",e);
}
}

Expand Down
7 changes: 6 additions & 1 deletion convex-cli/src/main/java/convex/cli/local/LocalStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import convex.core.data.AccountKey;
import convex.core.init.Init;
import convex.peer.API;
import convex.peer.PeerException;
import convex.peer.Server;
import convex.restapi.RESTServer;
import picocli.CommandLine.Command;
Expand Down Expand Up @@ -156,7 +157,11 @@ public List<Server> launchLocalPeers(List<AKeyPair> keyPairList, int peerPorts[]
List<AccountKey> keyList=keyPairList.stream().map(kp->kp.getAccountKey()).collect(Collectors.toList());

State genesisState=Init.createState(keyList);
return API.launchLocalPeers(keyPairList,genesisState, peerPorts);
try {
return API.launchLocalPeers(keyPairList,genesisState, peerPorts);
} catch (PeerException e) {
throw new CLIError(ExitCodes.CONFIG,"Failed to launch peer(s) : "+e.getMessage(),e);
}
}

public RESTServer launchRestAPI(Server server) {
Expand Down
3 changes: 3 additions & 0 deletions convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import convex.core.init.Init;
import convex.etch.EtchStore;
import convex.peer.API;
import convex.peer.PeerException;
import convex.peer.Server;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
Expand Down Expand Up @@ -93,6 +94,8 @@ public void run() {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CLIError("Peer genesis interrupted");
} catch (PeerException e) {
throw new CLIError("Peer genesis failed: "+e.getMessage(),e);
} finally {
etch.close();
}
Expand Down
3 changes: 3 additions & 0 deletions convex-cli/src/main/java/convex/cli/peer/PeerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import convex.etch.EtchStore;
import convex.peer.API;
import convex.peer.ConfigException;
import convex.peer.LaunchException;
import convex.peer.Server;
import convex.restapi.RESTServer;
import picocli.CommandLine.Command;
Expand Down Expand Up @@ -152,6 +153,8 @@ public void run() {
} catch (InterruptedException e) {
informWarning("Peer interrupted before normal shutdown");
Thread.currentThread().interrupt();
} catch (LaunchException e) {
throw new CLIError("Error in peer configuration: "+e.getMessage(),e);
} finally {
if (restServer!=null) restServer.close();
}
Expand Down
4 changes: 3 additions & 1 deletion convex-cli/src/test/java/convex/cli/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import convex.core.data.Keywords;
import convex.etch.EtchStore;
import convex.peer.API;
import convex.peer.ConfigException;
import convex.peer.LaunchException;
import convex.peer.Server;

public class ClientTest {
Expand Down Expand Up @@ -49,7 +51,7 @@ public class ClientTest {
assertEquals("4a12d868487648cd7a206f6d4879a7941463d80e185ccf6bb4e951429c4f4e37",kp.getAccountKey().toHexString());
}

@Test public void testClientCommands() throws IOException, TimeoutException, InterruptedException {
@Test public void testClientCommands() throws IOException, TimeoutException, InterruptedException, LaunchException, ConfigException {

try (EtchStore store = EtchStore.createTemp(TEMP_ETCH.getCanonicalPath())) {

Expand Down
4 changes: 3 additions & 1 deletion convex-cli/src/test/java/convex/cli/peer/GenesisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import convex.core.data.Keywords;
import convex.core.util.Utils;
import convex.peer.API;
import convex.peer.ConfigException;
import convex.peer.LaunchException;
import convex.peer.Server;

public class GenesisTest {
Expand All @@ -45,7 +47,7 @@ public class GenesisTest {
}
}

@Test public void testGenesisPeer() throws TimeoutException, InterruptedException, IOException {
@Test public void testGenesisPeer() throws TimeoutException, InterruptedException, IOException, LaunchException, ConfigException {
CLTester importTester = CLTester.run(
"key",
"import",
Expand Down
11 changes: 7 additions & 4 deletions convex-gui/src/main/java/convex/gui/peer/PeerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import convex.gui.tools.MessageFormatPanel;
import convex.gui.utils.Toolkit;
import convex.peer.API;
import convex.peer.PeerException;
import convex.peer.Server;
import convex.restapi.RESTServer;
import net.miginfocom.swing.MigLayout;
Expand All @@ -69,8 +70,9 @@ public class PeerGUI extends AbstractGUI {
* Launch the application.
* @param args Command line args
* @throws InterruptedException
* @throws PeerException
*/
public static void main(String[] args) throws InterruptedException {
public static void main(String[] args) throws InterruptedException, PeerException {
// call to set up Look and Feel
Toolkit.init();

Expand All @@ -79,7 +81,7 @@ public static void main(String[] args) throws InterruptedException {
System.exit(0);
}

public static PeerGUI launchPeerGUI(int peerNum, AKeyPair genesis, boolean topLevel) throws InterruptedException {
public static PeerGUI launchPeerGUI(int peerNum, AKeyPair genesis, boolean topLevel) throws InterruptedException, PeerException {
PeerGUI manager = new PeerGUI(peerNum,genesis);
manager.run();
return manager;
Expand All @@ -104,8 +106,9 @@ public static PeerGUI launchPeerGUI(int peerNum, AKeyPair genesis, boolean topLe
* @param genesis Genesis key pair
* @param peerCount number of peers to initialise in genesis
* @throws InterruptedException
* @throws PeerException If peer startup fails
*/
public PeerGUI(int peerCount, AKeyPair genesis) throws InterruptedException {
public PeerGUI(int peerCount, AKeyPair genesis) throws InterruptedException, PeerException {
super ("Peer Manager");
// Create key pairs for peers, use genesis key as first keypair
genesisKey=genesis;
Expand Down Expand Up @@ -333,7 +336,7 @@ public Server getPrimaryServer() {
return null;
}

public void launchAllPeers() throws InterruptedException {
public void launchAllPeers() throws InterruptedException, PeerException {
List<Server> serverList = API.launchLocalPeers(KEYPAIRS,genesisState);
for (Server server: serverList) {
ConvexLocal convex=Convex.connect(server, server.getPeerController(), server.getKeyPair());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void runLaunchDialog(JComponent parent) {
try {
int numPeers=(Integer)peerCountSpinner.getValue();
AWalletEntry we=keyField.getWalletEntry();
if (we==null) throw new Exception("No key pair selected");
if (we==null) throw new IllegalStateException("No key pair selected");

if (we.isLocked()) {
boolean unlocked= UnlockWalletDialog.offerUnlock(parent,we);
Expand All @@ -75,9 +75,11 @@ public static void runLaunchDialog(JComponent parent) {
kp=we.getKeyPair();

PeerGUI.launchPeerGUI(numPeers, kp,false);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
Toast.display(parent, "Launch Failed: "+e.getMessage(), Color.RED);
e.printStackTrace();
JOptionPane.showMessageDialog(parent, "Peer launch failed\n\nReason : "+e.getMessage());
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions convex-gui/src/test/java/convex/gui/GUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import convex.gui.peer.PeerGUI;
import convex.gui.tools.HackerTools;
import convex.gui.utils.Toolkit;
import convex.peer.PeerException;
import convex.peer.Server;

/**
Expand All @@ -37,12 +38,14 @@ public class GUITest {
manager=new PeerGUI(3,AKeyPair.generate());
SERVER=manager.getPrimaryServer();
CONVEX=Convex.connect(SERVER);
} catch (HeadlessException e) {
// we should have null manager
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new Error(e);
}
} catch (HeadlessException e) {
// we should have null manager
} catch (PeerException e) {
throw new Error(e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import convex.java.Convex;
import convex.java.JSON;
import convex.peer.API;
import convex.peer.ConfigException;
import convex.peer.LaunchException;
import convex.peer.Server;

public class RemoteClientTest {
Expand All @@ -31,7 +33,7 @@ public class RemoteClientTest {
static Address genesis=Init.GENESIS_ADDRESS;

@BeforeAll
public static void init() throws InterruptedException {
public static void init() throws InterruptedException, ConfigException, LaunchException {
Server s=API.launchPeer();
RESTServer rs=RESTServer.create(s);
rs.start(0);
Expand Down
19 changes: 12 additions & 7 deletions convex-peer/src/main/java/convex/peer/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -64,7 +63,7 @@ public class API {
* @throws InterruptedException
* @throws ConfigException if configuration is invalid
*/
public static Server launchPeer(Map<Keyword, Object> peerConfig) throws InterruptedException {
public static Server launchPeer(Map<Keyword, Object> peerConfig) throws LaunchException, InterruptedException, ConfigException {
// clone the config, we don't want to change the input. Can use getConfig() on Server to see final result.
HashMap<Keyword,Object> config=new HashMap<>(peerConfig);

Expand All @@ -91,8 +90,8 @@ public static Server launchPeer(Map<Keyword, Object> peerConfig) throws Interrup
Server server = Server.create(config);
server.launch();
return server;
} catch (TimeoutException | IOException e) {
throw new Error("Failed to launch peer",e);
} catch (IOException e) {
throw new LaunchException("Peer launch failed",e);
} finally {
Stores.setCurrent(tempStore);
}
Expand All @@ -102,8 +101,10 @@ public static Server launchPeer(Map<Keyword, Object> peerConfig) throws Interrup
* Launches a peer with a default configuration. Mainly for testing.
* @return Newly launched Server instance
* @throws InterruptedException
* @throws ConfigException
* @throws IOException
*/
public static Server launchPeer() throws InterruptedException {
public static Server launchPeer() throws InterruptedException, ConfigException, LaunchException {
AKeyPair kp=AKeyPair.generate();
State genesis=Init.createState(Lists.of(kp.getAccountKey()));
HashMap<Keyword, Object> config=new HashMap<>();
Expand All @@ -122,9 +123,11 @@ public static Server launchPeer() throws InterruptedException {
*
* @return List of Servers launched
* @throws InterruptedException
* @throws ConfigException
* @throws LaunchException
*
*/
public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genesisState) throws InterruptedException {
public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genesisState) throws InterruptedException, ConfigException, LaunchException {
return launchLocalPeers(keyPairs, genesisState, null);
}
/**
Expand All @@ -138,9 +141,11 @@ public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genes
*
* @return List of Servers launched
* @throws InterruptedException
* @throws ConfigException
* @throws LaunchException
*
*/
public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genesisState, int peerPorts[]) throws InterruptedException {
public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genesisState, int peerPorts[]) throws InterruptedException, ConfigException, LaunchException {
int count=keyPairs.size();

List<Server> serverList = new ArrayList<Server>();
Expand Down
15 changes: 10 additions & 5 deletions convex-peer/src/main/java/convex/peer/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ public static <T extends AStore> T checkStore(Map<Keyword, Object> config) {
* Checks if the config specifies a valid keystore
* @param config Configuration map for peer
* @return Keystore specified in Config, or null if not specified
* @throws ConfigException
*/
public static KeyStore checkKeyStore(Map<Keyword, Object> config) {
public static KeyStore checkKeyStore(Map<Keyword, Object> config) throws ConfigException {
Object o=config.get(Keywords.KEYSTORE);
if (o==null) return null;
if (o instanceof KeyStore) return (KeyStore)o;
Expand Down Expand Up @@ -156,8 +157,9 @@ public static KeyStore checkKeyStore(Map<Keyword, Object> config) {
* @param config Config map to check
* @param key
* @return Password, or null if unspecified
* @throws ConfigException
*/
private static char[] checkPass(Map<Keyword, Object> config, Keyword key) {
private static char[] checkPass(Map<Keyword, Object> config, Keyword key) throws ConfigException {
Object po=config.get(key);
if (po==null) return null;
if (po instanceof char[]) {
Expand All @@ -175,9 +177,10 @@ private static char[] checkPass(Map<Keyword, Object> config, Keyword key) {
* Establishes a store in the given config
* @param config Configuration map fpr peer (may be modified)
* @return Store specified in Config under :store
* @throws ConfigException
*/
@SuppressWarnings("unchecked")
public static <T extends AStore> T ensureStore(Map<Keyword, Object> config) {
public static <T extends AStore> T ensureStore(Map<Keyword, Object> config) throws ConfigException {
T store=checkStore(config);
if (store!=null) return store;

Expand Down Expand Up @@ -210,8 +213,9 @@ public static void ensureFlags(Map<Keyword, Object> config) {
* Ensures we have a hot peer :keypair set in config
*
* @param config Configuration map for peer (may be modified)
* @throws ConfigException
*/
public static AKeyPair ensurePeerKey(HashMap<Keyword, Object> config) {
public static AKeyPair ensurePeerKey(HashMap<Keyword, Object> config) throws ConfigException {
Object o=config.get(Keywords.KEYPAIR);
if (o!=null) {
if (o instanceof AKeyPair) {
Expand All @@ -227,8 +231,9 @@ public static AKeyPair ensurePeerKey(HashMap<Keyword, Object> config) {
/**
* Checks that the config specifies a source for the genesis state
* @param config
* @throws ConfigException
*/
public static void ensureGenesisState(HashMap<Keyword, Object> config) {
public static void ensureGenesisState(HashMap<Keyword, Object> config) throws ConfigException {

if (!(config.containsKey(Keywords.STATE)
||config.containsKey(Keywords.STORE)
Expand Down
5 changes: 4 additions & 1 deletion convex-peer/src/main/java/convex/peer/ConfigException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package convex.peer;

/**
* Message thrown when a failure occurs during peer configuration
*/
@SuppressWarnings("serial")
public class ConfigException extends RuntimeException {
public class ConfigException extends PeerException {

public ConfigException(String message, Throwable cause) {
super(message, cause);
Expand Down
Loading

0 comments on commit 2e39885

Please sign in to comment.