Skip to content

Commit

Permalink
More network refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 23, 2024
1 parent 59ba3d1 commit e03fb66
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 165 deletions.
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/crypto/Hashing.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static Hash sha256(String message) {
*/
private static final ThreadLocal<MessageDigest> sha256Store;
/**
* Private store for thread-local MessageDigent objects. Avoids cost of
* Private store for thread-local MessageDigest objects. Avoids cost of
* recreating these every time they are needed.
*/
private static final ThreadLocal<MessageDigest> sha3Store;
Expand All @@ -141,7 +141,7 @@ public static Hash sha256(String message) {
});
}
/**
* Threadlocal store for MessageDigest instances. TODO: figure out if this is
* Thread local store for MessageDigest instances. TODO: figure out if this is
* useful for performance. Probably not since digest initialisation is the
* bottleneck anyway?
*/
Expand Down
65 changes: 0 additions & 65 deletions convex-core/src/main/java/convex/core/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand All @@ -29,7 +24,6 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import convex.core.Constants;
import convex.core.data.AArrayBlob;
import convex.core.data.ACell;
import convex.core.data.ALongBlob;
Expand Down Expand Up @@ -884,66 +878,7 @@ public static void print(StringBuilder sb,Object v) {
}
}

/**
* Converts a Object to an InetSocketAddress
*
* @param o An Object to convert to a socket address. May be a String or existing InetSocketAddress
* @return A valid InetSocketAddress, or null if not in valid format
*/
public static InetSocketAddress toInetSocketAddress(Object o) {
if (o instanceof InetSocketAddress) {
return (InetSocketAddress) o;
} else if (o instanceof String) {
return toInetSocketAddress((String)o);
} else if (o instanceof URL) {
return toInetSocketAddress((URL)o);
} else {
return null;
}
}

/**
* Converts a String to an InetSocketAddress
*
* @param s A string in the format of a valid URL or "myhost.com:17888"
* @return A valid InetSocketAddress, or null if not in valid format
*/
public static InetSocketAddress toInetSocketAddress(String s) {
if (s==null) return null;
s=s.trim();
try {
// Try URL parsing first
URL url=new URI(s).toURL();
InetSocketAddress sa= toInetSocketAddress(url);
return sa;
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException ex) {
// Try to parse as host:port
int colon = s.lastIndexOf(':');
if (colon < 0) return null;
try {
String hostName = s.substring(0, colon); // up to last colon
int port = Utils.toInt(s.substring(colon + 1)); // after last colon
InetSocketAddress addr = new InetSocketAddress(hostName, port);
return addr;
} catch (SecurityException e) {
// shouldn't happen?
throw Utils.sneakyThrow(e);
}
}
}

/**
* Converts a URL to an InetSocketAddress. Will assume default port if not specified.
*
* @param url A valid URL
* @return A valid InetSocketAddress for the URL
*/
public static InetSocketAddress toInetSocketAddress(URL url) {
String host=url.getHost();
int port=url.getPort();
if (port<0) port=Constants.DEFAULT_PEER_PORT;
return new InetSocketAddress(host,port);
}

/**
* Filters the array, returning an array containing only the elements where the
Expand Down
18 changes: 0 additions & 18 deletions convex-core/src/test/java/convex/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.function.Function;
Expand Down Expand Up @@ -283,22 +281,6 @@ public void testToInt() {
assertEquals(-1, Utils.toInt("-1"));
}

@Test
public void testInetSocketAddress() {
String s = "http://www.something-unusual.com:18888";
InetSocketAddress sa = Utils.toInetSocketAddress(s);
assertNotNull(sa);

assertNotNull(Utils.toInetSocketAddress("localhost:8080"));

InetSocketAddress sa1=Utils.toInetSocketAddress("12.13.14.15:8080");
assertNotNull(sa1);
assertNotNull(Utils.toInetSocketAddress("http:12.13.14.15:8080"));

assertNull(Utils.toInetSocketAddress("@@@"));

}

@Test
public void testBinarySearchLeftmost() {
AVector<CVMLong> L = Vectors.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import convex.api.Convex;
import convex.core.crypto.wallet.AWalletEntry;
import convex.core.init.Init;
import convex.core.util.Utils;
import convex.gui.components.account.AddressCombo;
import convex.gui.keys.KeyRingPanel;
import convex.gui.utils.SymbolIcon;
import convex.gui.utils.Toolkit;
import convex.net.IPUtils;
import net.miginfocom.swing.MigLayout;

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ public static Convex tryConnect(JComponent parent,String prompt) {
if (result == JOptionPane.OK_OPTION) {
try {
String target=pan.hostField.getText();
InetSocketAddress sa=Utils.toInetSocketAddress(target);
InetSocketAddress sa=IPUtils.toInetSocketAddress(target);
log.info("Attempting connect to: "+sa);
Convex convex=Convex.connect(sa);
convex.setAddress(pan.addressField.getAddress());
Expand Down
4 changes: 2 additions & 2 deletions convex-gui/src/main/java/convex/gui/wallet/SwapPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import convex.core.data.ACell;
import convex.core.data.Address;
import convex.core.data.prim.AInteger;
import convex.core.util.Utils;
import convex.gui.components.AbstractGUI;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.BalanceLabel;
import convex.gui.components.DecimalAmountField;
import convex.gui.utils.SymbolIcon;
import convex.gui.utils.Toolkit;
import convex.net.IPUtils;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
Expand Down Expand Up @@ -184,7 +184,7 @@ protected void executeTrade() {
public static void main(String[] args) throws InterruptedException, IOException, TimeoutException {
// call to set up Look and Feel
Toolkit.init();
InetSocketAddress sa=Utils.toInetSocketAddress("localhost:18888");
InetSocketAddress sa=IPUtils.toInetSocketAddress("localhost:18888");
Convex convex=Convex.connect(sa);
convex.setAddress(Address.create(11));
new SwapPanel(convex,TokenInfo.getFungible(convex,"currency.USDF"),TokenInfo.convexCoin()).run();
Expand Down
4 changes: 2 additions & 2 deletions convex-gui/src/main/java/convex/gui/wallet/TransferPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import convex.core.Result;
import convex.core.data.Address;
import convex.core.data.prim.AInteger;
import convex.core.util.Utils;
import convex.gui.components.AbstractGUI;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
Expand All @@ -21,6 +20,7 @@
import convex.gui.components.account.AddressCombo;
import convex.gui.models.ComboModel;
import convex.gui.utils.Toolkit;
import convex.net.IPUtils;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
Expand Down Expand Up @@ -137,7 +137,7 @@ protected Result executeTransfer() throws InterruptedException {
public static void main(String[] args) throws InterruptedException, IOException, TimeoutException {
// call to set up Look and Feel
Toolkit.init();
InetSocketAddress sa=Utils.toInetSocketAddress("localhost:18888");
InetSocketAddress sa=IPUtils.toInetSocketAddress("localhost:18888");
Convex convex=Convex.connect(sa);
convex.setAddress(Address.create(11));
new TransferPanel(convex,TokenInfo.getFungible(convex,"currency.USDF")).run();
Expand Down
82 changes: 82 additions & 0 deletions convex-peer/src/main/java/convex/net/IPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import convex.core.Constants;
import convex.core.util.Utils;

public class IPUtils {

public static InetAddress tryGetIP() throws InterruptedException {
Expand Down Expand Up @@ -42,6 +48,82 @@ public static String tryGetWTF() throws InterruptedException {
}
}

/**
* Converts a Object to an InetSocketAddress
*
* @param o An Object to convert to a socket address. May be a String or existing InetSocketAddress
* @return A valid InetSocketAddress, or null if not in valid format
*/
public static InetSocketAddress toInetSocketAddress(Object o) {
if (o instanceof InetSocketAddress) {
return (InetSocketAddress) o;
} else if (o instanceof String) {
return toInetSocketAddress((String)o);
} else if (o instanceof URL) {
return toInetSocketAddress((URL)o);
} else if (o instanceof URI) {
return toInetSocketAddress((URI)o);
} else {
return null;
}
}

/**
* Converts a String to an InetSocketAddress
*
* @param s A string in the format of a valid URL or "myhost.com:17888"
* @return A valid InetSocketAddress, or null if not in valid format
*/
public static InetSocketAddress toInetSocketAddress(String s) {
if (s==null) return null;
s=s.trim();
try {
// Try URI parsing first
URI uri=new URI(s);
InetSocketAddress sa= toInetSocketAddress(uri);
return sa;
} catch (URISyntaxException | IllegalArgumentException ex) {
// Try to parse as host:port
int colon = s.lastIndexOf(':');
if (colon < 0) return null;
try {
String hostName = s.substring(0, colon); // up to last colon
int port = Utils.toInt(s.substring(colon + 1)); // after last colon
InetSocketAddress addr = new InetSocketAddress(hostName, port);
return addr;
} catch (SecurityException e) {
// shouldn't happen?
throw Utils.sneakyThrow(e);
}
}
}

/**
* Converts a URL to an InetSocketAddress. Will assume default port if not specified.
*
* @param url A valid URL
* @return A valid InetSocketAddress for the URL
*/
public static InetSocketAddress toInetSocketAddress(URL url) {
String host=url.getHost();
int port=url.getPort();
if (port<0) port=Constants.DEFAULT_PEER_PORT;
return new InetSocketAddress(host,port);
}

/**
* Converts a URI to an InetSocketAddress. Will assume default port if not specified.
*
* @param uri A valid URI
* @return A valid InetSocketAddress for the URI
*/
public static InetSocketAddress toInetSocketAddress(URI uri) {
String host=uri.getHost();
int port=uri.getPort();
if (port<0) port=Constants.DEFAULT_PEER_PORT;
return new InetSocketAddress(host,port);
}

public static void main(String[] args) throws InterruptedException {
System.out.println(tryGetIP());

Expand Down
5 changes: 3 additions & 2 deletions convex-peer/src/main/java/convex/net/NIOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ private void accept(Selector selector) throws IOException, ClosedChannelExceptio
log.debug("New connection accepted: {}", socketChannel);
socketChannel.configureBlocking(false);

// TODO: Confirm we don't want Nagle?
// Generally, we want to send packets as fast as possible.
// We don't want Nagle
// Generally, we want to send packets as fast as possible, they are usually quite small
// Low latency is the primary concern
socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, true);
socketChannel.register(selector, SelectionKey.OP_READ);
}
Expand Down
12 changes: 1 addition & 11 deletions convex-peer/src/main/java/convex/peer/API.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package convex.peer;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -54,7 +53,7 @@ public class API {
* <li>:persist (optional, Boolean) - Boolean flag to determine if peer state should be persisted in store at server close. Default true.
* <li>:url (optional, String) - public URL for server. If provided, peer will set its public on-chain address based on this, and the bind-address to 0.0.0.0.
* <li>:auto-manage (optional Boolean) - set to true for peer to auto-manage own account. Defaults to true.
* <li>:bind-address (optional String) - IP address of the ethernet device to bind too. For public peers set too 0.0.0.0. Default to 127.0.0.1.
* <li>:bind-address (optional String) - IP address of the ethernet device to bind too.
* </ul>
*
* @param peerConfig Configuration map for the new Peer
Expand All @@ -80,18 +79,9 @@ public static Server launchPeer(Map<Keyword, Object> peerConfig) throws LaunchEx
Config.ensurePeerKey(config);
Config.ensureGenesisState(config);

// if URL is set and it is not a local address and no BIND_ADDRESS is set, then the default for BIND_ADDRESS will be 0.0.0.0
if (config.containsKey(Keywords.URL) && !config.containsKey(Keywords.BIND_ADDRESS)) {
InetAddress ip = InetAddress.getByName((String) config.get(Keywords.URL));
if (! (ip.isAnyLocalAddress() || ip.isLoopbackAddress()) ) {
config.put(Keywords.BIND_ADDRESS, "0.0.0.0");
}
}
Server server = Server.create(config);
server.launch();
return server;
} catch (IOException e) {
throw new LaunchException("Peer launch failed",e);
} finally {
Stores.setCurrent(tempStore);
}
Expand Down
3 changes: 2 additions & 1 deletion convex-peer/src/main/java/convex/peer/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import convex.core.util.Utils;
import convex.net.ChallengeRequest;
import convex.net.Connection;
import convex.net.IPUtils;
import convex.net.Message;

/**
Expand Down Expand Up @@ -219,7 +220,7 @@ private void tryRandomConnect(State s) throws InterruptedException {
if (ps==null) continue; // skip
AString hostName=ps.getHostname();
if (hostName==null) continue;
InetSocketAddress maybeAddress=Utils.toInetSocketAddress(hostName.toString());
InetSocketAddress maybeAddress=IPUtils.toInetSocketAddress(hostName.toString());
if (maybeAddress==null) continue;
long peerStake=ps.getPeerStake();
if (peerStake>Constants.MINIMUM_EFFECTIVE_STAKE) {
Expand Down
Loading

0 comments on commit e03fb66

Please sign in to comment.