public static void main(String[] args) throws Exception { /* * This is just a test runner that will download blockchain till block * 390000 then exit. */ FullPrunedBlockStore store = new LevelDBFullPrunedBlockStore( MainNetParams.get(), args[0], 1000, 100 * 1024 * 1024l, 10 * 1024 * 1024, 100000, true, 390000); FullPrunedBlockChain vChain = new FullPrunedBlockChain( MainNetParams.get(), store); vChain.setRunScripts(false); PeerGroup vPeerGroup = new PeerGroup(MainNetParams.get(), vChain); vPeerGroup.setUseLocalhostPeerWhenPossible(true); vPeerGroup.addAddress(InetAddress.getLocalHost()); vPeerGroup.start(); vPeerGroup.downloadBlockChain(); } }
/** * <p>Create a new peer group</p> * * @param wallet the wallet to add to the peer group after construction * @param useFastCatchup True if only block headers from genesis block is required */ private void createNewPeerGroup(Wallet wallet, boolean useFastCatchup) throws TimeoutException { String[] dnsSeeds = new String[]{ /* "seed.bitcoin.sipa.be", // Pieter Wuille - not reachable */ "dnsseed.bluematt.me", // Matt Corallo "dnsseed.bitcoin.dashjr.org", // Luke Dashjr "seed.bitcoinstats.com", // Chris Decker "seed.bitnodes.io", // Addy Yeow }; log.info("Creating new DNS peer group for '{}'", networkParameters); peerGroup = new PeerGroup(networkParameters, blockChain); peerGroup.addPeerDiscovery(new DnsDiscovery(dnsSeeds, networkParameters)); peerGroup.setConnectTimeoutMillis(CONNECTION_TIMEOUT); peerGroup.setUserAgent( InstallationManager.MBHD_APP_NAME, Configurations.currentConfiguration.getCurrentVersion()); peerGroup.setMaxConnections(MAXIMUM_NUMBER_OF_PEERS); peerGroup.setUseLocalhostPeerWhenPossible(true); peerEventListener = new MultiBitPeerEventListener(); peerGroup.addEventListener(peerEventListener); addWalletToPeerGroup(wallet, useFastCatchup); }
@Test public void preferLocalPeer() throws IOException { // Because we are using the same port (8333 or 18333) that is used by Bitcoin Core // We have to consider 2 cases: // 1. Test are executed on the same machine that is running a full node // 2. Test are executed without any full node running locally // We have to avoid to connecting to real and external services in unit tests // So we skip this test in case we have already something running on port PARAMS.getPort() // Check that if we have a localhost port 8333 or 18333 then it's used instead of the p2p network. ServerSocket local = null; try { local = new ServerSocket(PARAMS.getPort(), 100, InetAddresses.forString("127.0.0.1")); } catch(BindException e) { // Port already in use, skipping this test. return; } try { peerGroup.setUseLocalhostPeerWhenPossible(true); peerGroup.start(); local.accept().close(); // Probe connect local.accept(); // Real connect // If we get here it used the local peer. Check no others are in use. assertEquals(1, peerGroup.getMaxConnections()); assertEquals(PeerAddress.localhost(PARAMS), peerGroup.getPendingPeers().get(0).getAddress()); } finally { local.close(); } }
protected void initPeerGroup() { if (clientType == ClientType.NIO_CLIENT_MANAGER) peerGroup = createPeerGroup(new NioClientManager()); else peerGroup = createPeerGroup(new BlockingClientManager()); peerGroup.setPingIntervalMsec(0); // Disable the pings as they just get in the way of most tests. peerGroup.addWallet(wallet); peerGroup.setUseLocalhostPeerWhenPossible(false); // Prevents from connecting to bitcoin nodes on localhost. }