From c18423fde35a2774eee11384270c4a1c5770cf26 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Wed, 17 Dec 2014 18:58:32 +0100 Subject: [PATCH] Peer: utility for requesting addr data --- .../src/main/java/org/bitcoinj/core/Peer.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/src/main/java/org/bitcoinj/core/Peer.java b/core/src/main/java/org/bitcoinj/core/Peer.java index 6a92d0ae..fb3846e7 100644 --- a/core/src/main/java/org/bitcoinj/core/Peer.java +++ b/core/src/main/java/org/bitcoinj/core/Peer.java @@ -132,6 +132,7 @@ public class Peer extends PeerSocketHandler { SettableFuture future; } private final CopyOnWriteArrayList getDataFutures; + @GuardedBy("getAddrFutures") private final LinkedList> getAddrFutures; // Outstanding pings against this peer and how long the last one took to complete. private final ReentrantLock lastPingTimesLock = new ReentrantLock(); @@ -207,6 +208,7 @@ public class Peer extends PeerSocketHandler { this.vDownloadData = chain != null; this.getDataFutures = new CopyOnWriteArrayList(); this.eventListeners = new CopyOnWriteArrayList(); + this.getAddrFutures = new LinkedList>(); this.fastCatchupTimeSecs = params.getGenesisBlock().getTimeSeconds(); this.isAcked = false; this.pendingPings = new CopyOnWriteArrayList(); @@ -355,6 +357,7 @@ public class Peer extends PeerSocketHandler { // We don't care about addresses of the network right now. But in future, // we should save them in the wallet so we don't put too much load on the seed nodes and can // properly explore the network. + processAddressMessage((AddressMessage) m); } else if (m instanceof HeadersMessage) { processHeaders((HeadersMessage) m); } else if (m instanceof AlertMessage) { @@ -399,6 +402,16 @@ public class Peer extends PeerSocketHandler { } } + private void processAddressMessage(AddressMessage m) { + SettableFuture future; + synchronized (getAddrFutures) { + future = getAddrFutures.poll(); + if (future == null) // Not an addr message we are waiting for. + return; + } + future.set(m); + } + private void processVersionMessage(VersionMessage m) throws ProtocolException { if (vPeerVersionMessage != null) throw new ProtocolException("Got two version messages from peer"); @@ -1173,6 +1186,16 @@ public class Peer extends PeerSocketHandler { return req.future; } + /** Sends a getaddr request to the peer and returns a future that completes with the answer once the peer has replied. */ + public ListenableFuture getAddr() { + SettableFuture future = SettableFuture.create(); + synchronized (getAddrFutures) { + getAddrFutures.add(future); + } + sendMessage(new GetAddrMessage(params)); + return future; + } + /** * When downloading the block chain, the bodies will be skipped for blocks created before the given date. Any * transactions relevant to the wallet will therefore not be found, but if you know your wallet has no such