Skip to content

Commit

Permalink
More networking simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 23, 2024
1 parent e03fb66 commit 56fe519
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
10 changes: 8 additions & 2 deletions convex-peer/src/main/java/convex/api/Convex.java
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,12 @@ public static ConvexLocal connect(Server server) {
}

/**
* Gets the consensus state from the remote Peer
* Gets the consensus state from the connected Peer. The acquired state will be a snapshot
* of the network global state as calculated by the Peer.
*
* SECURITY: Be aware that if this client instance is connected to an untrusted Peer, the
* Peer may lie about the latest state. If this is a security concern, the client should
* validate the consensus state independently.
*
* @return Future for consensus state
*/
Expand All @@ -991,7 +996,8 @@ public CompletableFuture<State> acquireState() {

/**
* Sets the default timeout for this Convex client instance.
* @param timeout timeout in milliseconds
*
* @param timeout timeout in milliseconds. Set to 0 or negative for no timeout
*/
public void setTimeout(long timeout) {
this.timeout=timeout;
Expand Down
13 changes: 1 addition & 12 deletions convex-peer/src/main/java/convex/api/ConvexRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,7 @@ public void closeButMaintainConnection() {
close();
}

/**
* Gets the consensus state from the connected Peer. The acquired state will be a snapshot
* of the network global state as calculated by the Peer.
*
* SECURITY: Be aware that if this client instance is connected to an untrusted Peer, the
* Peer may lie about the latest state. If this is a security concern, the client should
* validate the consensus state independently.
*
* @return Future for consensus state
* @throws TimeoutException If initial status request times out
* @throws InterruptedException In case of interrupt while acquiring
*/
@Override
public CompletableFuture<State> acquireState() {
AStore store=Stores.current();
return requestStatus().thenCompose(status->{
Expand Down
21 changes: 9 additions & 12 deletions convex-peer/src/main/java/convex/peer/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private void pollBelief() throws InterruptedException {
ArrayList<Connection> conns = new ArrayList<>(connections.values());
if (conns.size() == 0) {
// Nothing to do
// log.debug("No connections available to poll!");
return;
}

Expand All @@ -111,8 +110,8 @@ private void pollBelief() throws InterruptedException {
Connection c = conns.get(random.nextInt(conns.size()));

if (c.isClosed()) return;
Convex convex = Convex.connect(c.getRemoteAddress());
try {
;
try (Convex convex = Convex.connect(c.getRemoteAddress())) {
// use requestStatusSync to auto acquire hash of the status instead of the value
Result result=convex.requestStatusSync(POLL_TIMEOUT_MILLIS);
AVector<ACell> status = result.getValue();
Expand All @@ -122,11 +121,7 @@ private void pollBelief() throws InterruptedException {
Belief sb=(Belief) convex.acquire(h).get(POLL_ACQUIRE_TIMEOUT_MILLIS,TimeUnit.MILLISECONDS);

server.queueBelief(Message.createBelief(sb));
} finally {
convex.close();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} catch (RuntimeException | TimeoutException | ExecutionException | IOException t) {
if (server.isLive()) {
log.warn("Belief Polling failed: {}",t.getClass().toString()+" : "+t.getMessage());
Expand Down Expand Up @@ -596,14 +591,16 @@ public Connection connectToPeer(InetSocketAddress hostAddress) throws Interrupte
// Use temp client connection to query status
Convex convex=Convex.connect(hostAddress);
Result result = convex.requestStatusSync(Config.DEFAULT_CLIENT_TIMEOUT);
if (result.isError()) {
log.info("Bad status message from remote Peer");
return null;
}

AVector<ACell> status = result.getValue();
// close the temp connection to Convex API
convex.close();

if (status == null || status.count()!=Config.STATUS_COUNT) {
log.info("Bad status message from remote Peer");
return null;
}


AccountKey peerKey =RT.ensureAccountKey(status.get(3));
if (peerKey==null) return null;
Expand Down

0 comments on commit 56fe519

Please sign in to comment.