From a6d0c9169b60afdcb40a41efbdbd6ee264fbf7a8 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Mon, 11 Mar 2013 15:04:17 +0100 Subject: [PATCH] Clear a few more misc IntelliJ inspection alerts. --- .../java/com/google/bitcoin/core/Peer.java | 8 +- .../com/google/bitcoin/core/PeerGroup.java | 165 +++++++++--------- 2 files changed, 82 insertions(+), 91 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Peer.java b/core/src/main/java/com/google/bitcoin/core/Peer.java index fba9a230..a8631b18 100644 --- a/core/src/main/java/com/google/bitcoin/core/Peer.java +++ b/core/src/main/java/com/google/bitcoin/core/Peer.java @@ -127,7 +127,7 @@ public class Peer { private volatile Channel vChannel; private volatile VersionMessage vPeerVersionMessage; private boolean isAcked; - private PeerHandler handler; + private final PeerHandler handler; /** * Construct a peer that reads/writes from the given block chain. @@ -983,7 +983,7 @@ public class Peer { /** * Sends the given message on the peers Channel. */ - public ChannelFuture sendMessage(Message m) throws IOException { + public ChannelFuture sendMessage(Message m) { // This does not need to be locked. return Channels.write(vChannel, m); } @@ -1103,9 +1103,9 @@ public class Peer { // The future that will be invoked when the pong is heard back. public SettableFuture future; // The random nonce that lets us tell apart overlapping pings/pongs. - public long nonce; + public final long nonce; // Measurement of the time elapsed. - public long startTimeMsec; + public final long startTimeMsec; public PendingPing(long nonce) { future = SettableFuture.create(); diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index 2a91e1d2..78bb6e2f 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -889,13 +889,9 @@ public class PeerGroup extends AbstractIdleService { } boolean success = false; for (Peer p : announceToPeers) { - try { - log.info("{}: Announcing {} pending wallet transactions", p.getAddress(), inv.getItems().size()); - p.sendMessage(inv); - success = true; - } catch (IOException e) { - log.warn("Failed to announce 'inv' to peer: {}", p); - } + log.info("{}: Announcing {} pending wallet transactions", p.getAddress(), inv.getItems().size()); + p.sendMessage(inv); + success = true; } return success; } @@ -1138,96 +1134,91 @@ public class PeerGroup extends AbstractIdleService { log.info("broadcastTransaction: Enough peers, adding {} to the memory pool and sending to {}", tx.getHashAsString(), somePeer); final Transaction pinnedTx = memoryPool.seen(tx, somePeer.getAddress()); - try { - // Prepare to send the transaction by adding a listener that'll be called when confidence changes. - // Only bother with this if we might actually hear back: - if (minConnections > 1) tx.getConfidence().addEventListener(new TransactionConfidence.Listener() { - public void onConfidenceChanged(Transaction tx) { - // The number of peers that announced this tx has gone up. This will run in a peer thread. - final int numSeenPeers = tx.getConfidence().numBroadcastPeers(); - boolean done = false; - log.info("broadcastTransaction: TX {} seen by {} peers", pinnedTx.getHashAsString(), - numSeenPeers); + // Prepare to send the transaction by adding a listener that'll be called when confidence changes. + // Only bother with this if we might actually hear back: + if (minConnections > 1) tx.getConfidence().addEventListener(new TransactionConfidence.Listener() { + public void onConfidenceChanged(Transaction tx) { + // The number of peers that announced this tx has gone up. This will run in a peer thread. + final int numSeenPeers = tx.getConfidence().numBroadcastPeers(); + boolean done = false; + log.info("broadcastTransaction: TX {} seen by {} peers", pinnedTx.getHashAsString(), + numSeenPeers); + lock.lock(); + try { + if (numSeenPeers >= minConnections) { + // We've seen the min required number of peers announce the transaction. Note that we + // can't wait for the current number of connected peers right now because we could have + // added more peers after the broadcast took place, which means they won't have seen + // the transaction. In future when peers sync up their memory pools after they connect + // we could come back and change this. + // + // Now tell the wallet about the transaction. If the wallet created the transaction then + // it already knows and will ignore this. If it's a transaction we received from + // somebody else via a side channel and are now broadcasting, this will put it into the + // wallet now we know it's valid. + for (Wallet wallet : wallets) { + try { + if (wallet.isPendingTransactionRelevant(pinnedTx)) { + // Assumption here is there are no dependencies of the created transaction. + wallet.receivePending(pinnedTx, null); + } + } catch (Throwable t) { + future.setException(t); + return; + } + } + done = true; + } + } finally { + lock.unlock(); + } + if (done) { + // We're done! Run this outside of the peer group lock as setting the future may immediately + // invoke any listeners associated with it and it's simpler if the PeerGroup isn't locked. + log.info("broadcastTransaction: {} complete", pinnedTx.getHashAsString()); + tx.getConfidence().removeEventListener(this); + future.set(pinnedTx); + } + } + }); + + // Satoshis code sends an inv in this case and then lets the peer request the tx data. We just + // blast out the TX here for a couple of reasons. Firstly it's simpler: in the case where we have + // just a single connection we don't have to wait for getdata to be received and handled before + // completing the future in the code immediately below. Secondly, it's faster. The reason the + // Satoshi client sends an inv is privacy - it means you can't tell if the peer originated the + // transaction or not. However, we are not a fully validating node and this is advertised in + // our version message, as SPV nodes cannot relay it doesn't give away any additional information + // to skip the inv here - we wouldn't send invs anyway. + ChannelFuture sendComplete = somePeer.sendMessage(pinnedTx); + // If we've been limited to talk to only one peer, we can't wait to hear back because the + // remote peer won't tell us about transactions we just announced to it for obvious reasons. + // So we just have to assume we're done, at that point. This happens when we're not given + // any peer discovery source and the user just calls connectTo() once. + if (minConnections == 1) { + sendComplete.addListener(new ChannelFutureListener() { + public void operationComplete(ChannelFuture _) throws Exception { lock.lock(); try { - if (numSeenPeers >= minConnections) { - // We've seen the min required number of peers announce the transaction. Note that we - // can't wait for the current number of connected peers right now because we could have - // added more peers after the broadcast took place, which means they won't have seen - // the transaction. In future when peers sync up their memory pools after they connect - // we could come back and change this. - // - // Now tell the wallet about the transaction. If the wallet created the transaction then - // it already knows and will ignore this. If it's a transaction we received from - // somebody else via a side channel and are now broadcasting, this will put it into the - // wallet now we know it's valid. - for (Wallet wallet : wallets) { - try { - if (wallet.isPendingTransactionRelevant(pinnedTx)) { - // Assumption here is there are no dependencies of the created transaction. - wallet.receivePending(pinnedTx, null); - } - } catch (Throwable t) { - future.setException(t); - return; + for (Wallet wallet : wallets) { + try { + if (wallet.isPendingTransactionRelevant(pinnedTx)) { + // Assumption here is there are no dependencies of the created + // transaction. + wallet.receivePending(pinnedTx, null); } + } catch (Throwable t) { + future.setException(t); + return; } - done = true; } } finally { lock.unlock(); } - if (done) { - // We're done! Run this outside of the peer group lock as setting the future may immediately - // invoke any listeners associated with it and it's simpler if the PeerGroup isn't locked. - log.info("broadcastTransaction: {} complete", pinnedTx.getHashAsString()); - tx.getConfidence().removeEventListener(this); - future.set(pinnedTx); - } + future.set(pinnedTx); + return; } }); - - // Satoshis code sends an inv in this case and then lets the peer request the tx data. We just - // blast out the TX here for a couple of reasons. Firstly it's simpler: in the case where we have - // just a single connection we don't have to wait for getdata to be received and handled before - // completing the future in the code immediately below. Secondly, it's faster. The reason the - // Satoshi client sends an inv is privacy - it means you can't tell if the peer originated the - // transaction or not. However, we are not a fully validating node and this is advertised in - // our version message, as SPV nodes cannot relay it doesn't give away any additional information - // to skip the inv here - we wouldn't send invs anyway. - ChannelFuture sendComplete = somePeer.sendMessage(pinnedTx); - // If we've been limited to talk to only one peer, we can't wait to hear back because the - // remote peer won't tell us about transactions we just announced to it for obvious reasons. - // So we just have to assume we're done, at that point. This happens when we're not given - // any peer discovery source and the user just calls connectTo() once. - if (minConnections == 1) { - sendComplete.addListener(new ChannelFutureListener() { - public void operationComplete(ChannelFuture _) throws Exception { - lock.lock(); - try { - for (Wallet wallet : wallets) { - try { - if (wallet.isPendingTransactionRelevant(pinnedTx)) { - // Assumption here is there are no dependencies of the created - // transaction. - wallet.receivePending(pinnedTx, null); - } - } catch (Throwable t) { - future.setException(t); - return; - } - } - } finally { - lock.unlock(); - } - future.set(pinnedTx); - return; - } - }); - } - } catch (IOException e) { - future.setException(e); - return; } } }, MoreExecutors.sameThreadExecutor());