@Inject public PeerGroupService(NetworkParameters params, PeerDiscovery peerDiscovery) { this.netParams = params; this.peerGroup = new PeerGroup(params); peerGroup.setUserAgent(userAgentName, appVersion); peerGroup.addPeerDiscovery(peerDiscovery); }
/** * Convenience for connecting only to peers that can serve specific services. It will configure suitable peer * discoveries. * @param requiredServices Required services as a bitmask, e.g. {@link VersionMessage#NODE_NETWORK}. */ public void setRequiredServices(long requiredServices) { lock.lock(); try { this.requiredServices = requiredServices; peerDiscoverers.clear(); addPeerDiscovery(MultiplexingDiscovery.forServices(params, requiredServices)); } finally { lock.unlock(); } }
/** * Convenience for connecting only to peers that can serve specific services. It will configure suitable peer * discoveries. * @param requiredServices Required services as a bitmask, e.g. {@link VersionMessage#NODE_NETWORK}. */ public void setRequiredServices(long requiredServices) { lock.lock(); try { this.requiredServices = requiredServices; peerDiscoverers.clear(); addPeerDiscovery(MultiplexingDiscovery.forServices(params, requiredServices)); } finally { lock.unlock(); } }
/** * Convenience for connecting only to peers that can serve specific services. It will configure suitable peer * discoveries. * @param requiredServices Required services as a bitmask, e.g. {@link VersionMessage#NODE_NETWORK}. */ public void setRequiredServices(long requiredServices) { lock.lock(); try { this.requiredServices = requiredServices; peerDiscoverers.clear(); addPeerDiscovery(MultiplexingDiscovery.forServices(params, requiredServices)); } finally { lock.unlock(); } }
/** * Convenience for connecting only to peers that can serve specific services. It will configure suitable peer * discoveries. * @param requiredServices Required services as a bitmask, e.g. {@link VersionMessage#NODE_NETWORK}. */ public void setRequiredServices(long requiredServices) { lock.lock(); try { this.requiredServices = requiredServices; peerDiscoverers.clear(); addPeerDiscovery(MultiplexingDiscovery.forServices(params, requiredServices)); } finally { lock.unlock(); } }
@Bean public PeerGroup peerGroup(BlockChain bitcoinBlockchain, Context bitcoinContext, NetworkParameters chainNetworkParameters) { PeerGroup peerGroup = new PeerGroup(bitcoinContext, bitcoinBlockchain); // Regtest has no peer-to-peer functionality if (chainNetworkParameters.equals(MainNetParams.get())) { Stream.of(appConfig.getBitcoinNodePeerGroupSeed()) .forEach((peer) -> { try { peerGroup.addAddress(Inet4Address.getByName(peer)); } catch (UnknownHostException e) { LOG.error("Not possible to add peer {} to the peer group. Unknown error: {}", peer, e); } }); } else if (chainNetworkParameters.equals(TestNet3Params.get())) { peerGroup.addPeerDiscovery(new DnsDiscovery(chainNetworkParameters)); } return peerGroup; }
@Test public void multiplePeerDiscovery() throws InterruptedException { peerGroup.setMaxPeersToDiscoverCount(98); peerGroup.addPeerDiscovery(createPeerDiscovery(1, 0)); peerGroup.addPeerDiscovery(createPeerDiscovery(2, 100)); peerGroup.addPeerDiscovery(createPeerDiscovery(96, 200)); peerGroup.addPeerDiscovery(createPeerDiscovery(3, 300)); peerGroup.addPeerDiscovery(createPeerDiscovery(1, 400)); peerGroup.addDiscoveredEventListener(new PeerDiscoveredEventListener() { @Override public void onPeersDiscovered(Set<PeerAddress> peerAddresses) { assertEquals(99, peerAddresses.size()); } }); peerGroup.start(); }
@Test public void peerDiscoveryPolling() throws InterruptedException { // Check that if peer discovery fails, we keep trying until we have some nodes to talk with. final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean result = new AtomicBoolean(); peerGroup.addPeerDiscovery(new PeerDiscovery() { @Override public InetSocketAddress[] getPeers(long services, long unused, TimeUnit unused2) throws PeerDiscoveryException { if (!result.getAndSet(true)) { // Pretend we are not connected to the internet. throw new PeerDiscoveryException("test failure"); } else { // Return a bogus address. latch.countDown(); return new InetSocketAddress[]{new InetSocketAddress("localhost", 1)}; } } @Override public void shutdown() { } }); peerGroup.start(); latch.await(); // Check that we did indeed throw an exception. If we got here it means we threw and then PeerGroup tried // again a bit later. assertTrue(result.get()); }
/** * * The constructor takes in a NetworkParameters variable that determines whether we * are connecting to the Production Net or the Test Net. It also takes in an int * which determines the minimum number of peers to connect to before broadcasting a transaction. * */ public Bitcoin(NetworkParameters netParams, int minPeers) { this.netParams = netParams; this.minPeers = minPeers; if (minPeers == 0) { peerGroup = null; } else { peerGroup = new PeerGroup(netParams); peerGroup.setMinBroadcastConnections(minPeers); peerGroup.addPeerDiscovery(new DnsDiscovery(netParams)); peerGroup.start(); } this.context = Context.getOrCreate(this.netParams); }
for (HttpDiscovery.Details httpSeed : httpSeeds) discoveries.add(new HttpDiscovery(params, httpSeed, httpClient)); result.addPeerDiscovery(new MultiplexingDiscovery(params, discoveries)); } else { result.addPeerDiscovery(new TorDiscovery(params, torClient));
for (HttpDiscovery.Details httpSeed : httpSeeds) discoveries.add(new HttpDiscovery(params, httpSeed, httpClient)); result.addPeerDiscovery(new MultiplexingDiscovery(params, discoveries)); } else { result.addPeerDiscovery(new TorDiscovery(params, torClient));
final BlockChain chain = new BlockChain(PARAMS, store); final PeerGroup peerGroup = new PeerGroup(PARAMS, chain); peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS)); long now = new Date().getTime() / 1000; peerGroup.setFastCatchupTimeSecs(now);
public static void main(String[] args) throws InterruptedException { BriefLogFormatter.init(); PeerGroup peerGroup = new PeerGroup(PARAMS); peerGroup.setMaxConnections(32); peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS)); peerGroup.addOnTransactionBroadcastListener(new OnTransactionBroadcastListener() { @Override public void onTransaction(Peer peer, Transaction tx) { Result result = DefaultRiskAnalysis.FACTORY.create(null, tx, NO_DEPS).analyze(); incrementCounter(TOTAL_KEY); log.info("tx {} result {}", tx.getHash(), result); incrementCounter(result.name()); if (result == Result.NON_STANDARD) incrementCounter(Result.NON_STANDARD + "-" + DefaultRiskAnalysis.isStandard(tx)); } }); peerGroup.start(); while (true) { Thread.sleep(STATISTICS_FREQUENCY_MS); printCounters(); } }
BlockChain chain = new BlockChain(params, chainStore); PeerGroup peers = new PeerGroup(params, chain); peers.addPeerDiscovery(new DnsDiscovery(params));
public void init () { synchronized (initialized) { if (!initialized) { try { blockStore = new SPVBlockStore(Constants.getNetwork(), new File("blockheaders")); } catch (Exception e) { blockStore = new MemoryBlockStore(Constants.getNetwork()); } try { blockChain = new BlockChain(Constants.getNetwork(), blockStore); peerGroup = new PeerGroup(Constants.getNetwork(), blockChain); peerGroup.addPeerDiscovery(new DnsDiscovery(Constants.getNetwork())); peerGroup.setDownloadTxDependencies(false); peerGroup.setBloomFilteringEnabled(false); peerGroup.setFastCatchupTimeSecs(System.currentTimeMillis()); peerGroup.start(); peerGroup.addEventListener(new EventListener(), Threading.SAME_THREAD); registerShutdownHook(); final DownloadProgressTracker listener = new DownloadProgressTracker(); peerGroup.startBlockChainDownload(listener); } catch (Exception e) { throw new RuntimeException(e); } initialized = true; } } }
peers.addPeerDiscovery(new DnsDiscovery(params));
/** * <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); }
private void setupNetwork() { params = MainNetParams.get(); peerGroup = new PeerGroup(params, null /* no chain */); peerGroup.setUserAgent("PeerMonitor", "1.0"); peerGroup.setMaxConnections(4); peerGroup.addPeerDiscovery(new DnsDiscovery(params)); peerGroup.addConnectedEventListener(new PeerConnectedEventListener() { @Override public void onPeerConnected(final Peer peer, int peerCount) { refreshUI(); lookupReverseDNS(peer); } }); peerGroup.addDisconnectedEventListener(new PeerDisconnectedEventListener() { @Override public void onPeerDisconnected(final Peer peer, int peerCount) { refreshUI(); synchronized (reverseDnsLookups) { reverseDnsLookups.remove(peer); } } }); }
peerGroup.addDiscoveredEventListener(mPeerDiscoveredEventListener); peerGroup.setPeerDiscoveryTimeoutMillis(discoveryTimeout); peerGroup.addPeerDiscovery(new PeerDiscovery() { private final PeerDiscovery normalPeerDiscovery = MultiplexingDiscovery .forServices(Constants.NETWORK_PARAMETERS, 0);
peerGroup.addDisconnectedEventListener(disconnectedListener); peerGroup.addPreMessageReceivedEventListener(preMessageReceivedListener); peerGroup.addPeerDiscovery(new PeerDiscovery() { @Override public InetSocketAddress[] getPeers(long services, long unused, TimeUnit unused2) throws PeerDiscoveryException {