mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 17:26:28 +01:00
FetchTransactions, PrintPeers: more CompletableFuture conversions
This commit is contained in:
parent
8ec6c05c6b
commit
b8c72c996a
2 changed files with 8 additions and 9 deletions
|
@ -22,10 +22,10 @@ import org.bitcoinj.params.TestNet3Params;
|
||||||
import org.bitcoinj.store.BlockStore;
|
import org.bitcoinj.store.BlockStore;
|
||||||
import org.bitcoinj.store.MemoryBlockStore;
|
import org.bitcoinj.store.MemoryBlockStore;
|
||||||
import org.bitcoinj.utils.BriefLogFormatter;
|
import org.bitcoinj.utils.BriefLogFormatter;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the given transaction and its dependencies from a peers memory pool then prints them out.
|
* Downloads the given transaction and its dependencies from a peers memory pool then prints them out.
|
||||||
|
@ -45,7 +45,7 @@ public class FetchTransactions {
|
||||||
Peer peer = peerGroup.getConnectedPeers().get(0);
|
Peer peer = peerGroup.getConnectedPeers().get(0);
|
||||||
|
|
||||||
Sha256Hash txHash = Sha256Hash.wrap(args[0]);
|
Sha256Hash txHash = Sha256Hash.wrap(args[0]);
|
||||||
ListenableFuture<Transaction> future = peer.getPeerMempoolTransaction(txHash);
|
Future<Transaction> future = peer.getPeerMempoolTransaction(txHash);
|
||||||
System.out.println("Waiting for node to send us the requested transaction: " + txHash);
|
System.out.println("Waiting for node to send us the requested transaction: " + txHash);
|
||||||
Transaction tx = future.get();
|
Transaction tx = future.get();
|
||||||
System.out.println(tx);
|
System.out.println(tx);
|
||||||
|
|
|
@ -28,13 +28,12 @@ import org.bitcoinj.net.NioClientManager;
|
||||||
import org.bitcoinj.params.MainNetParams;
|
import org.bitcoinj.params.MainNetParams;
|
||||||
import org.bitcoinj.utils.BriefLogFormatter;
|
import org.bitcoinj.utils.BriefLogFormatter;
|
||||||
import com.google.common.util.concurrent.Futures;
|
import com.google.common.util.concurrent.Futures;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
|
||||||
import com.google.common.util.concurrent.SettableFuture;
|
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,13 +76,13 @@ public class PrintPeers {
|
||||||
final Object lock = new Object();
|
final Object lock = new Object();
|
||||||
final long[] bestHeight = new long[1];
|
final long[] bestHeight = new long[1];
|
||||||
|
|
||||||
List<ListenableFuture<Void>> futures = new ArrayList<>();
|
List<CompletableFuture<Void>> futures = new ArrayList<>();
|
||||||
NioClientManager clientManager = new NioClientManager();
|
NioClientManager clientManager = new NioClientManager();
|
||||||
for (final InetAddress addr : addrs) {
|
for (final InetAddress addr : addrs) {
|
||||||
InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
|
InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
|
||||||
final Peer peer = new Peer(params, new VersionMessage(params, 0),
|
final Peer peer = new Peer(params, new VersionMessage(params, 0),
|
||||||
new PeerAddress(params, address), null);
|
new PeerAddress(params, address), null);
|
||||||
final SettableFuture<Void> future = SettableFuture.create();
|
final CompletableFuture<Void> future = new CompletableFuture<>();
|
||||||
// Once the connection has completed version handshaking ...
|
// Once the connection has completed version handshaking ...
|
||||||
peer.addConnectedEventListener((p, peerCount) -> {
|
peer.addConnectedEventListener((p, peerCount) -> {
|
||||||
// Check the chain height it claims to have.
|
// Check the chain height it claims to have.
|
||||||
|
@ -102,18 +101,18 @@ public class PrintPeers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Now finish the future and close the connection
|
// Now finish the future and close the connection
|
||||||
future.set(null);
|
future.complete(null);
|
||||||
peer.close();
|
peer.close();
|
||||||
});
|
});
|
||||||
peer.addDisconnectedEventListener((p, peerCount) -> {
|
peer.addDisconnectedEventListener((p, peerCount) -> {
|
||||||
if (!future.isDone())
|
if (!future.isDone())
|
||||||
System.out.println("Failed to talk to " + addr);
|
System.out.println("Failed to talk to " + addr);
|
||||||
future.set(null);
|
future.complete(null);
|
||||||
});
|
});
|
||||||
clientManager.openConnection(address, peer);
|
clientManager.openConnection(address, peer);
|
||||||
futures.add(future);
|
futures.add(future);
|
||||||
}
|
}
|
||||||
// Wait for every tried connection to finish.
|
// Wait for every tried connection to finish.
|
||||||
Futures.successfulAsList(futures).get();
|
CompletableFuture.allOf(futures.toArray( new CompletableFuture[0])).join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue