Search in sources :

Example 6 with PeerAddress

use of org.bitcoinj.core.PeerAddress in project cryptoputty by alokmenghrajani.

the class CrawlerThread method run.

public void run() {
    // Continuously get peers and try to connect to them.
    while (true) {
        PeerGroup peerGroup = CryptoputtyApplication.kit.peerGroup();
        if (peerGroup == null) {
            continue;
        }
        List<Peer> connectedPeers = peerGroup.getConnectedPeers();
        Collections.shuffle(connectedPeers);
        for (Peer peer : connectedPeers) {
            try {
                Thread.sleep(1000);
                AddressMessage newPeers = peer.getAddr().get(5000, TimeUnit.MILLISECONDS);
                for (PeerAddress newPeer : newPeers.getAddresses()) {
                    peerGroup.addAddress(newPeer);
                }
            } catch (TimeoutException e) {
            // Don't do anything...
            } catch (InterruptedException | ExecutionException e) {
                log.warn("failed to get peers");
                e.printStackTrace();
            }
        }
    }
}
Also used : PeerAddress(org.bitcoinj.core.PeerAddress) Peer(org.bitcoinj.core.Peer) PeerGroup(org.bitcoinj.core.PeerGroup) ExecutionException(java.util.concurrent.ExecutionException) AddressMessage(org.bitcoinj.core.AddressMessage) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with PeerAddress

use of org.bitcoinj.core.PeerAddress in project bisq-core by bisq-network.

the class BtcNodeConverter method create.

@Nullable
private PeerAddress create(Socks5Proxy proxy, String host, int port) {
    try {
        // We use DnsLookupTor to not leak with DNS lookup
        // Blocking call. takes about 600 ms ;-(
        InetAddress lookupAddress = facade.torLookup(proxy, host);
        InetSocketAddress address = new InetSocketAddress(lookupAddress, port);
        return new PeerAddress(address.getAddress(), address.getPort());
    } catch (Exception e) {
        log.error("Failed to create peer address", e);
        return null;
    }
}
Also used : PeerAddress(org.bitcoinj.core.PeerAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) DnsLookupException(bisq.network.DnsLookupException) Nullable(javax.annotation.Nullable)

Example 8 with PeerAddress

use of org.bitcoinj.core.PeerAddress in project bisq-core by bisq-network.

the class BtcNodeConverter method convertWithTor.

@Nullable
PeerAddress convertWithTor(BtcNode node, Socks5Proxy proxy) {
    int port = node.getPort();
    PeerAddress result = create(proxy, node.getHostNameOrAddress(), port);
    if (result == null) {
        String address = node.getAddress();
        if (address != null) {
            result = create(proxy, address, port);
        } else {
            log.warn("Lookup failed, no address for node", node);
        }
    }
    return result;
}
Also used : PeerAddress(org.bitcoinj.core.PeerAddress) Nullable(javax.annotation.Nullable)

Example 9 with PeerAddress

use of org.bitcoinj.core.PeerAddress in project bisq-core by bisq-network.

the class WalletConfig method startUp.

@Override
protected void startUp() throws Exception {
    // Runs in a separate thread.
    Context.propagate(context);
    if (!directory.exists()) {
        if (!directory.mkdirs()) {
            throw new IOException("Could not create directory " + directory.getAbsolutePath());
        }
    }
    log.info("Wallet directory: {}", directory);
    try {
        File chainFile = new File(directory, spvChainFileName);
        boolean chainFileExists = chainFile.exists();
        // BTC wallet
        vBtcWalletFile = new File(directory, btcWalletFileName);
        boolean shouldReplayWallet = (vBtcWalletFile.exists() && !chainFileExists) || seed != null;
        BisqKeyChainGroup keyChainGroup;
        if (seed != null)
            keyChainGroup = new BisqKeyChainGroup(params, new BtcDeterministicKeyChain(seed), true);
        else
            keyChainGroup = new BisqKeyChainGroup(params, true);
        vBtcWallet = createOrLoadWallet(vBtcWalletFile, shouldReplayWallet, keyChainGroup, false, seed);
        vBtcWallet.allowSpendingUnconfirmedTransactions();
        if (seed != null)
            keyChainGroup = new BisqKeyChainGroup(params, new BisqDeterministicKeyChain(seed), false);
        else
            keyChainGroup = new BisqKeyChainGroup(params, new BisqDeterministicKeyChain(vBtcWallet.getKeyChainSeed()), false);
        // BSQ wallet
        if (BisqEnvironment.isBaseCurrencySupportingBsq()) {
            vBsqWalletFile = new File(directory, bsqWalletFileName);
            vBsqWallet = createOrLoadWallet(vBsqWalletFile, shouldReplayWallet, keyChainGroup, true, seed);
        }
        // Initiate Bitcoin network objects (block store, blockchain and peer group)
        vStore = provideBlockStore(chainFile);
        if (!chainFileExists || seed != null) {
            if (checkpoints != null) {
                // Initialize the chain file with a checkpoint to speed up first-run sync.
                long time;
                if (seed != null) {
                    // we created both wallets at the same time
                    time = seed.getCreationTimeSeconds();
                    if (chainFileExists) {
                        log.info("Deleting the chain file in preparation from restore.");
                        vStore.close();
                        if (!chainFile.delete())
                            throw new IOException("Failed to delete chain file in preparation for restore.");
                        vStore = new SPVBlockStore(params, chainFile);
                    }
                } else {
                    time = vBtcWallet.getEarliestKeyCreationTime();
                }
                if (time > 0)
                    CheckpointManager.checkpoint(params, checkpoints, vStore, time);
                else
                    log.warn("Creating a new uncheckpointed block store due to a wallet with a creation time of zero: this will result in a very slow chain sync");
            } else if (chainFileExists) {
                log.info("Deleting the chain file in preparation from restore.");
                vStore.close();
                if (!chainFile.delete())
                    throw new IOException("Failed to delete chain file in preparation for restore.");
                vStore = new SPVBlockStore(params, chainFile);
            }
        }
        vChain = new BlockChain(params, vStore);
        vPeerGroup = createPeerGroup();
        vPeerGroup.setBroadcastToAllPeers(true);
        if (minBroadcastConnections > 0)
            vPeerGroup.setMinBroadcastConnections(minBroadcastConnections);
        vPeerGroup.setUserAgent(userAgent, Version.VERSION);
        // before we're actually connected the broadcast waits for an appropriate number of connections.
        if (peerAddresses != null) {
            for (PeerAddress addr : peerAddresses) vPeerGroup.addAddress(addr);
            log.info("We try to connect to {} btc nodes", numConnectionForBtc);
            vPeerGroup.setMaxConnections(Math.min(numConnectionForBtc, peerAddresses.length));
            peerAddresses = null;
        } else if (!params.equals(RegTestParams.get())) {
            vPeerGroup.addPeerDiscovery(discovery != null ? discovery : new DnsDiscovery(params));
        }
        vChain.addWallet(vBtcWallet);
        vPeerGroup.addWallet(vBtcWallet);
        if (vBsqWallet != null) {
            // noinspection ConstantConditions
            vChain.addWallet(vBsqWallet);
            // noinspection ConstantConditions
            vPeerGroup.addWallet(vBsqWallet);
        }
        onSetupCompleted();
        if (blockingStartup) {
            vPeerGroup.start();
            // Make sure we shut down cleanly.
            installShutdownHook();
            final DownloadProgressTracker listener = new DownloadProgressTracker();
            vPeerGroup.startBlockChainDownload(listener);
            listener.await();
        } else {
            Futures.addCallback(vPeerGroup.startAsync(), new FutureCallback() {

                @Override
                public void onSuccess(@Nullable Object result) {
                    final PeerDataEventListener listener = downloadListener == null ? new DownloadProgressTracker() : downloadListener;
                    vPeerGroup.startBlockChainDownload(listener);
                }

                @Override
                public void onFailure(@NotNull Throwable t) {
                    throw new RuntimeException(t);
                }
            });
        }
    } catch (BlockStoreException e) {
        throw new IOException(e);
    }
}
Also used : BlockChain(org.bitcoinj.core.BlockChain) BlockStoreException(org.bitcoinj.store.BlockStoreException) PeerDataEventListener(org.bitcoinj.core.listeners.PeerDataEventListener) SPVBlockStore(org.bitcoinj.store.SPVBlockStore) IOException(java.io.IOException) DnsDiscovery(org.bitcoinj.net.discovery.DnsDiscovery) PeerAddress(org.bitcoinj.core.PeerAddress) DownloadProgressTracker(org.bitcoinj.core.listeners.DownloadProgressTracker) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 10 with PeerAddress

use of org.bitcoinj.core.PeerAddress in project bisq-core by bisq-network.

the class BtcNodeConverterTest method testConvertWithTor.

@Test
public void testConvertWithTor() throws DnsLookupException {
    InetAddress expected = mock(InetAddress.class);
    Facade facade = mock(Facade.class);
    when(facade.torLookup(any(), anyString())).thenReturn(expected);
    BtcNode node = mock(BtcNode.class);
    when(node.getHostNameOrAddress()).thenReturn("aaa.onion");
    PeerAddress peerAddress = new BtcNodeConverter(facade).convertWithTor(node, mock(Socks5Proxy.class));
    // noinspection ConstantConditions
    assertEquals(expected, peerAddress.getAddr());
}
Also used : Facade(bisq.core.btc.wallet.BtcNodeConverter.Facade) PeerAddress(org.bitcoinj.core.PeerAddress) Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) BtcNode(bisq.core.btc.BitcoinNodes.BtcNode) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

PeerAddress (org.bitcoinj.core.PeerAddress)14 BtcNode (bisq.core.btc.BitcoinNodes.BtcNode)8 Test (org.junit.Test)8 InetAddress (java.net.InetAddress)5 Nullable (javax.annotation.Nullable)4 Facade (bisq.core.btc.wallet.BtcNodeConverter.Facade)3 Socks5Proxy (com.runjva.sourceforge.jsocks.protocol.Socks5Proxy)3 UnknownHostException (java.net.UnknownHostException)2 DnsLookupException (bisq.network.DnsLookupException)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 InetSocketAddress (java.net.InetSocketAddress)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AddressMessage (org.bitcoinj.core.AddressMessage)1 BlockChain (org.bitcoinj.core.BlockChain)1 Peer (org.bitcoinj.core.Peer)1 PeerGroup (org.bitcoinj.core.PeerGroup)1