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 0a935e60..253a57d5 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -1283,7 +1283,7 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac * @param numPeers How many peers to wait for. * @return a future that will be triggered when the number of connected peers >= numPeers */ - public ListenableFuture waitForPeers(final int numPeers) { + public ListenableFuture> waitForPeers(final int numPeers) { return waitForPeersOfVersion(numPeers, 0); } @@ -1295,16 +1295,17 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac * @param protocolVersion The protocol version the awaited peers must implement (or better). * @return a future that will be triggered when the number of connected peers implementing protocolVersion or higher >= numPeers */ - public ListenableFuture waitForPeersOfVersion(final int numPeers, final long protocolVersion) { - int foundPeers = countPeersOfAtLeastVersion(protocolVersion); - if (foundPeers >= numPeers) { - return Futures.immediateFuture(this); + public ListenableFuture> waitForPeersOfVersion(final int numPeers, final long protocolVersion) { + List foundPeers = findPeersOfAtLeastVersion(protocolVersion); + if (foundPeers.size() >= numPeers) { + return Futures.immediateFuture(foundPeers); } - final SettableFuture future = SettableFuture.create(); + final SettableFuture> future = SettableFuture.create(); addEventListener(new AbstractPeerEventListener() { @Override public void onPeerConnected(Peer peer, int peerCount) { - if (countPeersOfAtLeastVersion(protocolVersion) >= numPeers) { - future.set(PeerGroup.this); + final List peers = findPeersOfAtLeastVersion(protocolVersion); + if (peers.size() >= numPeers) { + future.set(peers); removeEventListener(this); } } @@ -1312,14 +1313,17 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac return future; } - private int countPeersOfAtLeastVersion(long protocolVersion) { + /** + * Returns a mutable array list of peers that implement the given protocol version or better. + */ + public List findPeersOfAtLeastVersion(long protocolVersion) { lock.lock(); try { - int foundPeers = 0; + ArrayList results = new ArrayList(peers.size()); for (Peer peer : peers) if (peer.getPeerVersionMessage().clientVersion >= protocolVersion) - foundPeers++; - return foundPeers; + results.add(peer); + return results; } finally { lock.unlock(); } diff --git a/core/src/main/java/com/google/bitcoin/core/TransactionBroadcast.java b/core/src/main/java/com/google/bitcoin/core/TransactionBroadcast.java index 2016e365..f3f7fc55 100644 --- a/core/src/main/java/com/google/bitcoin/core/TransactionBroadcast.java +++ b/core/src/main/java/com/google/bitcoin/core/TransactionBroadcast.java @@ -65,8 +65,7 @@ public class TransactionBroadcast { public ListenableFuture broadcast() { log.info("Waiting for {} peers required for broadcast ...", minConnections); - ListenableFuture peerAvailabilityFuture = peerGroup.waitForPeers(minConnections); - peerAvailabilityFuture.addListener(new EnoughAvailablePeers(), Threading.SAME_THREAD); + peerGroup.waitForPeers(minConnections).addListener(new EnoughAvailablePeers(), Threading.SAME_THREAD); return future; } diff --git a/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java b/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java index 1603c9b6..4736f899 100644 --- a/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java +++ b/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java @@ -585,7 +585,7 @@ public class PeerGroupTest extends TestWithPeerGroup { @Test public void waitForNumPeers1() throws Exception { - ListenableFuture future = peerGroup.waitForPeers(3); + ListenableFuture> future = peerGroup.waitForPeers(3); peerGroup.startAsync(); peerGroup.awaitRunning(); assertFalse(future.isDone()); @@ -604,7 +604,7 @@ public class PeerGroupTest extends TestWithPeerGroup { final int baseVer = peerGroup.getMinRequiredProtocolVersion() + 3000; final int newVer = baseVer + 1000; - ListenableFuture future = peerGroup.waitForPeersOfVersion(2, newVer); + ListenableFuture> future = peerGroup.waitForPeersOfVersion(2, newVer); VersionMessage ver1 = new VersionMessage(params, 10); ver1.clientVersion = baseVer;