From 7dd1fce5aa45e27ef4683440033bc64a52c6aae3 Mon Sep 17 00:00:00 2001 From: "Miron Cuperman (devrandom)" Date: Wed, 20 Jul 2011 22:10:14 +0000 Subject: [PATCH] Clean up Peer exception handling --- src/com/google/bitcoin/core/NetworkConnection.java | 9 ++++++++- src/com/google/bitcoin/core/Peer.java | 2 -- src/com/google/bitcoin/core/PeerGroup.java | 10 ++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/com/google/bitcoin/core/NetworkConnection.java b/src/com/google/bitcoin/core/NetworkConnection.java index c52adcc3..04e8fa66 100644 --- a/src/com/google/bitcoin/core/NetworkConnection.java +++ b/src/com/google/bitcoin/core/NetworkConnection.java @@ -100,8 +100,15 @@ public class NetworkConnection { }); // BitCoinJ is a client mode implementation. That means there's not much point in us talking to other client // mode nodes because we can't download the data from them we need to find/verify transactions. - if (!versionMessage.hasBlockChain()) + if (!versionMessage.hasBlockChain()) { + // Shut down the socket + try { + shutdown(); + } catch (IOException ex) { + // ignore exceptions while aborting + } throw new ProtocolException("Peer does not have a copy of the block chain."); + } // newer clients use checksumming serializer.useChecksumming(peerVersion >= 209); // Handshake is done! diff --git a/src/com/google/bitcoin/core/Peer.java b/src/com/google/bitcoin/core/Peer.java index 494e40f7..0da734f2 100644 --- a/src/com/google/bitcoin/core/Peer.java +++ b/src/com/google/bitcoin/core/Peer.java @@ -97,10 +97,8 @@ public class Peer { try { conn = new NetworkConnection(address, params, bestHeight, 60000); } catch (IOException ex) { - log.error("while trying to open connection", ex); throw new RuntimeException(ex); } catch (ProtocolException ex) { - log.error("while trying to negotiate connection", ex); throw new RuntimeException(ex); } } diff --git a/src/com/google/bitcoin/core/PeerGroup.java b/src/com/google/bitcoin/core/PeerGroup.java index 8bed1e6d..db0aeff0 100644 --- a/src/com/google/bitcoin/core/PeerGroup.java +++ b/src/com/google/bitcoin/core/PeerGroup.java @@ -223,13 +223,15 @@ public class PeerGroup { handleNewPeer(peer); log.info("running " + peer); peer.run(); - } - finally { + } catch (RuntimeException ex) { + // do not propagate RuntimeException - log and try next peer + log.error("error while talking to peer", ex); + } finally { // In all cases, put the address back on the queue. // We will retry this peer after all other peers have been tried. inactives.add(address); - peers.remove(peer); - handlePeerDeath(peer); + if (peers.remove(peer)) + handlePeerDeath(peer); } } };