package com.itranswarp.bitcoin.io;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.itranswarp.cryptocurrency.common.Discover;

/**
 * Discover full nodes by DNS query:
 * https://bitcoin.org/en/developer-guide#peer-discovery
 * 
 * @author liaoxuefeng
 */
public class BitCoinPeerDiscover implements Discover {

	static Log log = LogFactory.getLog(BitCoinPeerDiscover.class);

	@Override
	public String[] lookup() throws IOException {
		InetAddress[] addrs = InetAddress.getAllByName("bitseed.xf2.org");
		return Arrays.asList(addrs).stream().map((addr) -> {
			return addr.getHostAddress();
		}).toArray(String[]::new);
	}

	public static void main(String[] args) throws Exception {
		String[] nodes = new BitCoinPeerDiscover().lookup();
		for (String node : nodes) {
			log.info("Found node: " + node);
		}
	}
}

@@ -73,24 +73,25 @@
 ava.net.Inet
+4
 Address;%0Aimp
@@ -87,16 +87,83 @@
 ddress;%0A
+import java.net.InetAddress;%0Aimport java.net.UnknownHostException;%0A
 import j
@@ -576,183 +576,690 @@
 ;%0A%0A%09
-@Override%0A%09public String%5B%5D lookup() throws IOException %7B%0A%09%09InetAddress%5B%5D addrs = InetAddress.getAllByName(%22bitseed.xf2.org%22
+// https://en.bitcoin.it/wiki/Satoshi_Client_Node_Discovery#DNS_Addresses%0A%09static final String%5B%5D DNS_SEEDS = %7B %22bitseed.xf2.org%22, %22dnsseed.bluematt.me%22, %22seed.bitcoin.sipa.be%22,%0A%09%09%09%22dnsseed.bitcoin.dashjr.org%22, %22seed.bitcoinstats.com%22 %7D;%0A%0A%09static final InetAddress%5B%5D EMPTY_ADDRS = new InetAddress%5B0%5D;%0A%0A%09@Override%0A%09public String%5B%5D lookup() throws IOException %7B%0A%09%09String%5B%5D ips = Arrays.stream(DNS_SEEDS).parallel().map((host) -%3E %7B%0A%09%09%09try %7B%0A%09%09%09%09return InetAddress.getAllByName(host);%0A%09%09%09%7D catch (UnknownHostException e) %7B%0A%09%09%09%09log.warn(%22Cannot look up host: %22 + host
 );%0A%09%09
+%09
+%09
 return 
-Arrays.asList(addrs).stream(
+EMPTY_ADDRS;%0A%09%09%09%7D%0A%09%09%7D).flatMap(x -%3E Arrays.stream(x)).filter(addr -%3E addr instanceof Inet4Address
 ).map(
-(
 addr
-)
  -%3E 
@@ -1322,16 +1322,128 @@
 ::new);%0A
+%09%09if (ips.length == 0) %7B%0A%09%09%09throw new IOException(%22Cannot lookup pears from all DNS seeds.%22);%0A%09%09%7D%0A%09%09return ips;%0A
 %09%7D%0A%0A%09pub

use dns seeds