mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-08-01 12:31:23 +00:00
Dump out versions and chain heights in PrintPeers.
This commit is contained in:
@@ -16,17 +16,24 @@
|
||||
|
||||
package com.google.bitcoin.examples;
|
||||
|
||||
import com.google.bitcoin.core.NetworkConnection;
|
||||
import com.google.bitcoin.core.NetworkParameters;
|
||||
import com.google.bitcoin.core.TCPNetworkConnection;
|
||||
import com.google.bitcoin.discovery.DnsDiscovery;
|
||||
import com.google.bitcoin.discovery.IrcDiscovery;
|
||||
import com.google.bitcoin.discovery.PeerDiscoveryException;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Prints a list of IP addresses connected to the rendezvous point on the LFnet IRC channel.
|
||||
*/
|
||||
public class PrintPeers {
|
||||
private static InetSocketAddress[] dnsPeers, ircPeers;
|
||||
|
||||
private static void printElapsed(long start) {
|
||||
long now = System.currentTimeMillis();
|
||||
System.out.println(String.format("Took %.2f seconds", (now - start) / 1000.0));
|
||||
@@ -52,21 +59,56 @@ public class PrintPeers {
|
||||
System.out.println("-> " + message);
|
||||
}
|
||||
};
|
||||
printPeers(d.getPeers());
|
||||
ircPeers = d.getPeers();
|
||||
printPeers(ircPeers);
|
||||
printElapsed(start);
|
||||
}
|
||||
|
||||
private static void printDNS() throws PeerDiscoveryException {
|
||||
long start = System.currentTimeMillis();
|
||||
DnsDiscovery dns = new DnsDiscovery(NetworkParameters.prodNet());
|
||||
printPeers(dns.getPeers());
|
||||
dnsPeers = dns.getPeers();
|
||||
printPeers(dnsPeers);
|
||||
printElapsed(start);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws PeerDiscoveryException {
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("=== IRC ===");
|
||||
printIRC();
|
||||
System.out.println("=== DNS ===");
|
||||
printDNS();
|
||||
System.out.println("=== Version/chain heights ===");
|
||||
|
||||
ExecutorService pool = Executors.newFixedThreadPool(100);
|
||||
ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
|
||||
for (InetSocketAddress peer : dnsPeers) addrs.add(peer.getAddress());
|
||||
for (InetSocketAddress peer : ircPeers) addrs.add(peer.getAddress());
|
||||
System.out.println("Scanning " + addrs.size() + " peers:");
|
||||
|
||||
final Object lock = new Object();
|
||||
final long[] bestHeight = new long[1];
|
||||
|
||||
for (final InetAddress addr : addrs) {
|
||||
pool.submit(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
NetworkConnection conn = new TCPNetworkConnection(addr,
|
||||
NetworkParameters.prodNet(), 0, 1000);
|
||||
synchronized (lock) {
|
||||
long nodeHeight = conn.getVersionMessage().bestHeight;
|
||||
long diff = bestHeight[0] - nodeHeight;
|
||||
if (diff > 0) {
|
||||
System.out.println("Node is behind by " + diff + " blocks: " + addr.toString());
|
||||
} else {
|
||||
bestHeight[0] = nodeHeight;
|
||||
}
|
||||
}
|
||||
conn.shutdown();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
pool.awaitTermination(1, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user