Skip to content

Commit 47e9f56

Browse files
committed
Updates for better peer start handling
1 parent 8016b2a commit 47e9f56

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

convex-cli/src/main/java/convex/cli/mixins/RemotePeerMixin.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class RemotePeerMixin extends AMixin {
1919
private Integer port;
2020

2121
@Option(names={"--host"},
22-
defaultValue="${env:CONVEX_HOST:-"+Constants.DEFAULT_PEER_HOSTNAME+"}",
23-
description="Hostname for remote peer connection. Can specify with CONVEX_HOST. Defaulting to: ${DEFAULT-VALUE}")
22+
defaultValue="${env:CONVEX_HOST}",
23+
description="Hostname for remote peer connection. Can specify with CONVEX_HOST, or use \"none\" to disable.")
2424
private String hostname;
2525

2626
/**
@@ -48,8 +48,8 @@ public Convex connect() {
4848
}
4949

5050
public InetSocketAddress getSocketAddress() {
51-
if (port==null) port=convex.core.Constants.DEFAULT_PEER_PORT;
52-
if (hostname==null) hostname=convex.cli.Constants.DEFAULT_PEER_HOSTNAME;
51+
int port= (this.port!=null) ?this.port:convex.core.Constants.DEFAULT_PEER_PORT;
52+
String hostname=(this.hostname!=null)?this.hostname:convex.cli.Constants.DEFAULT_PEER_HOSTNAME;
5353
InetSocketAddress sa=IPUtils.parseAddress(hostname,port);
5454
return sa;
5555
}
@@ -60,6 +60,7 @@ public InetSocketAddress getSocketAddress() {
6060
*/
6161
public InetSocketAddress getSpecifiedSource() {
6262
if (hostname==null) return null;
63+
if (hostname.trim().equalsIgnoreCase("none")) return null;
6364
return getSocketAddress();
6465
}
6566

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,15 @@ public void execute() throws InterruptedException {
172172
HashMap<Keyword,Object> config=new HashMap<>();
173173
config.put(Keywords.KEYPAIR, peerKey);
174174
config.put(Keywords.STORE, store);
175-
config.put(Keywords.SOURCE, remoteSource);
176175
config.put(Keywords.URL, url);
177176
config.put(Keywords.PORT, port);
177+
178+
if (remoteSource!=null) {
179+
config.put(Keywords.SOURCE, remoteSource); // if remote source to sync with is specified
180+
} else {
181+
// if no remote host to sync with, assume we want to restore existing peer
182+
config.put(Keywords.RESTORE,true);
183+
}
178184
if (recalc!=null) config.put(Keywords.RECALC, recalc);
179185

180186
config.put(Keywords.BASE_URL, baseURL);

convex-cli/src/test/java/convex/cli/peer/LocalTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.fail;
67

78
import java.io.BufferedReader;
89
import java.io.File;
@@ -22,14 +23,18 @@
2223
import org.junit.jupiter.api.TestInstance;
2324
import org.junit.jupiter.api.TestInstance.Lifecycle;
2425

26+
import com.beust.jcommander.Strings;
27+
2528
import convex.api.Convex;
2629
import convex.api.ConvexRemote;
2730
import convex.cli.CLTester;
2831
import convex.cli.ExitCodes;
2932
import convex.cli.Helpers;
3033
import convex.core.Result;
3134
import convex.core.crypto.AKeyPair;
35+
import convex.core.crypto.BIP39;
3236
import convex.core.crypto.PFXTools;
37+
import convex.core.crypto.SLIP10;
3338
import convex.core.data.Blob;
3439
import convex.core.data.Blobs;
3540
import convex.core.util.Utils;
@@ -71,6 +76,9 @@ public static void setupLocalNet() throws IOException, InterruptedException {
7176
importTester.assertExitCode(ExitCodes.SUCCESS);
7277
assertEquals(expectedKey, importTester.getOutput().trim());
7378

79+
Blob MASTER_KEY=BIP39.getSeed(bip39, bipPassphrase);
80+
AKeyPair GENESIS_KP=SLIP10.deriveKeyPair(MASTER_KEY);
81+
assertEquals(expectedKey,GENESIS_KP.getAccountKey().toHexString());
7482

7583
String javaHome = System.getProperty("java.home");
7684
String javaCmd = javaHome + File.separator + "bin" + File.separator + "java";
@@ -99,12 +107,15 @@ public static void setupLocalNet() throws IOException, InterruptedException {
99107
ProcessBuilder builder = new ProcessBuilder(cmd);
100108
process = builder.start();
101109

110+
ArrayList<String> outputList=new ArrayList<>();
111+
102112
// We need to wait until the peer started message is seen
103113
Thread checker = new Thread(() -> {
104114
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
105115
try (Scanner s = new Scanner(reader)) {
106116
while (true) {
107117
String output = s.nextLine();
118+
outputList.add(output);
108119
// System.err.println(output);
109120
if (output.contains("Started: 1")) break;
110121
}
@@ -116,7 +127,11 @@ public static void setupLocalNet() throws IOException, InterruptedException {
116127
checker.setDaemon(true);
117128
checker.start();
118129
checker.join(10000);
119-
assertTrue(process.isAlive());
130+
if (process.isAlive()) {
131+
132+
} else {
133+
fail("Local network did note start, with output:\n"+Strings.join("\n", outputList));
134+
}
120135
}
121136

122137

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ private Peer establishPeer() throws ConfigException, LaunchException, Interrupte
189189
throw new LaunchException("Timeout trying to connect to remote peer");
190190
} catch (IllegalArgumentException e) {
191191
throw new LaunchException("Bad :SOURCE for peer launch",e);
192+
} catch (Exception e ) {
193+
// something else failed, probably an IOException
194+
throw new LaunchException("Failed to sync with remote peer host at: "+source,e);
192195
}
193-
} else if (Utils.bool(getConfig().get(Keywords.RESTORE))) {
196+
}
197+
198+
if (Utils.bool(getConfig().get(Keywords.RESTORE))) {
194199
ACell rk=RT.cvm(config.get(Keywords.ROOT_KEY));
195200
if (rk==null) rk=keyPair.getAccountKey();
196201

@@ -200,6 +205,7 @@ private Peer establishPeer() throws ConfigException, LaunchException, Interrupte
200205
return peer;
201206
}
202207
}
208+
203209
// No sync or restored state, so use passed state
204210
State genesisState = (State) config.get(Keywords.STATE);
205211
if (genesisState!=null) {

0 commit comments

Comments
 (0)