Skip to content

Commit

Permalink
More exception handling fixes and simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 27, 2024
1 parent 16faf97 commit df54f1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
48 changes: 19 additions & 29 deletions convex-peer/src/main/java/convex/peer/BeliefPropagator.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,37 +276,27 @@ protected boolean maybeMergeBeliefs(Belief... newBeliefs) {
*/
private Belief awaitBelief() throws InterruptedException {
ArrayList<Message> beliefMessages=new ArrayList<>();
try {

// if we did a belief merge recently, pause for a bit to await more Beliefs
LoadMonitor.down();
Message firstEvent=beliefQueue.poll(AWAIT_BELIEFS_PAUSE, TimeUnit.MILLISECONDS);
LoadMonitor.up();
if (firstEvent==null) return null; // nothing arrived

beliefMessages.add(firstEvent);
beliefQueue.drainTo(beliefMessages);
HashMap<AccountKey,SignedData<Order>> newOrders=belief.getOrdersHashMap();
// log.info("Merging Beliefs: "+allBeliefs.size());

boolean anyOrderChanged=false;
for (Message m: beliefMessages) {
boolean changed=mergeBeliefMessage(newOrders,m);
if (changed) anyOrderChanged=true;
}
if (!anyOrderChanged) return null;

Belief newBelief= Belief.create(newOrders);
return newBelief;

} catch (InterruptedException e) {
// This is expected sometimes, e.g. peer shutdown
throw e;
} catch (Exception e) {
// we didn't expect this!
log.warn("UNEXPECTED error awaiting Belief",e);
return null;
// if we did a belief merge recently, pause for a bit to await more Beliefs
LoadMonitor.down();
Message firstEvent=beliefQueue.poll(AWAIT_BELIEFS_PAUSE, TimeUnit.MILLISECONDS);
LoadMonitor.up();
if (firstEvent==null) return null; // nothing arrived

beliefMessages.add(firstEvent);
beliefQueue.drainTo(beliefMessages);
HashMap<AccountKey,SignedData<Order>> newOrders=belief.getOrdersHashMap();
// log.info("Merging Beliefs: "+allBeliefs.size());

boolean anyOrderChanged=false;
for (Message m: beliefMessages) {
boolean changed=mergeBeliefMessage(newOrders,m);
if (changed) anyOrderChanged=true;
}
if (!anyOrderChanged) return null;

Belief newBelief= Belief.create(newOrders);
return newBelief;
}


Expand Down
8 changes: 7 additions & 1 deletion convex-peer/src/test/java/convex/peer/TestNetwork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package convex.peer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import org.slf4j.Logger;
Expand All @@ -16,6 +18,7 @@
import convex.core.crypto.AKeyPair;
import convex.core.data.AccountKey;
import convex.core.data.Address;
import convex.core.exceptions.ResultException;
import convex.core.init.Init;
import convex.core.util.Utils;

Expand Down Expand Up @@ -98,8 +101,11 @@ public synchronized ConvexRemote getClient(AKeyPair kp) {
network.CONVEX.transferSync(addr, Coin.GOLD);
ConvexRemote client=Convex.connect(network.SERVER.getHostAddress(),addr,kp);
return client;
} catch (Exception t) {
} catch (IOException | ResultException | TimeoutException t) {
throw Utils.sneakyThrow(t);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
}

Expand Down

0 comments on commit df54f1b

Please sign in to comment.