some cleanup of the bitcoinj/jtorproxy integration

This commit is contained in:
danda 2016-07-19 12:54:48 -07:00
parent 84e78c40fd
commit fcb08c12ad
4 changed files with 81 additions and 123 deletions

View File

@ -0,0 +1,59 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.btc;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.net.BlockingClientManager;
import org.bitcoinj.net.discovery.IrcDiscovery;
import org.bitcoinj.core.PeerGroup;
import java.io.File;
import java.net.Proxy;
import java.util.concurrent.TimeoutException;
public class WalletAppKitBitSquare extends WalletAppKit {
private Proxy proxy;
/**
* Creates a new WalletAppKit, with a newly created {@link Context}. Files will be stored in the given directory.
*/
public WalletAppKitBitSquare(NetworkParameters params, Proxy proxy, File directory, String filePrefix) {
super(params, directory, filePrefix);
this.proxy = proxy;
}
public Proxy getProxy() {
return proxy;
}
protected PeerGroup createPeerGroup() throws TimeoutException {
// discovery = new IrcDiscovery("#bitcoin");
int CONNECT_TIMEOUT_MSEC = 60 * 1000;
ProxySocketFactory proxySocketFactory = new ProxySocketFactory(proxy);
BlockingClientManager mgr = new BlockingClientManager(proxySocketFactory);
PeerGroup result = new PeerGroup(params, vChain, mgr);
mgr.setConnectTimeoutMillis(CONNECT_TIMEOUT_MSEC);
result.setConnectTimeoutMillis(CONNECT_TIMEOUT_MSEC);
// We can't use TorDiscovery cuz we don't have a torClient object.
// result.addPeerDiscovery(new TorDiscovery(params, torClient));
return result;
}
}

View File

@ -1,104 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.btc;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.Service;
import io.bitsquare.btc.listeners.AddressConfidenceListener;
import io.bitsquare.btc.listeners.BalanceListener;
import io.bitsquare.btc.listeners.TxConfidenceListener;
import io.bitsquare.common.Timer;
import io.bitsquare.common.UserThread;
import io.bitsquare.common.handlers.ErrorMessageHandler;
import io.bitsquare.common.handlers.ExceptionHandler;
import io.bitsquare.common.handlers.ResultHandler;
import io.bitsquare.p2p.NodeAddress;
import io.bitsquare.storage.FileUtil;
import io.bitsquare.storage.Storage;
import io.bitsquare.user.Preferences;
import javafx.beans.property.*;
import org.bitcoinj.core.*;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.utils.Threading;
import org.bitcoinj.wallet.DeterministicSeed;
import org.bitcoinj.net.BlockingClientManager;
import org.bitcoinj.net.discovery.IrcDiscovery;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.crypto.params.KeyParameter;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.IOException;
import java.net.Proxy;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
public class WalletAppKitTorProxy extends WalletAppKit {
Proxy proxy;
/**
* Creates a new WalletAppKit, with a newly created {@link Context}. Files will be stored in the given directory.
*/
public WalletAppKitTorProxy(NetworkParameters params, Proxy proxy, File directory, String filePrefix) {
super(params, directory, filePrefix);
this.proxy = proxy;
}
protected PeerGroup createPeerGroup() throws TimeoutException {
// System.setProperty("socksProxyHost", "127.0.0.1");
// System.setProperty("socksProxyPort", "9050");
// System.setProperty("socksProxyHost", proxy.address().getHostString());
// System.setProperty("socksProxyPort", Integer.toString(proxy.address().getPort()));
// TESTING: always use tor.
if (true || useTor) {
// discovery = new IrcDiscovery("#bitcoin");
int CONNECT_TIMEOUT_MSEC = 60 * 1000;
ProxySocketFactory proxySocketFactory = new ProxySocketFactory(proxy);
BlockingClientManager mgr = new BlockingClientManager(proxySocketFactory);
PeerGroup result = new PeerGroup(params, vChain, mgr);
mgr.setConnectTimeoutMillis(CONNECT_TIMEOUT_MSEC);
result.setConnectTimeoutMillis(CONNECT_TIMEOUT_MSEC);
// We can't use TorDiscovery cuz we don't have a torClient object.
// result.addPeerDiscovery(new TorDiscovery(params, torClient));
return result;
}
else {
return new PeerGroup(params, vChain);
}
}
}

View File

@ -91,8 +91,7 @@ public class WalletService {
private final UserAgent userAgent;
private final boolean useTor;
private WalletAppKitTorProxy walletAppKit;
private NodeAddress nodeAddressProxy;
private WalletAppKitBitSquare walletAppKit;
private Wallet wallet;
private final IntegerProperty numPeers = new SimpleIntegerProperty(0);
private final ObjectProperty<List<Peer>> connectedPeers = new SimpleObjectProperty<>();
@ -132,7 +131,7 @@ public class WalletService {
// Public Methods
///////////////////////////////////////////////////////////////////////////////////////////
public void initialize(@Nullable DeterministicSeed seed, NodeAddress nodeAddressProxy, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
public void initialize(@Nullable DeterministicSeed seed, Proxy proxy, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
// Tell bitcoinj to execute event handlers on the JavaFX UI thread. This keeps things simple and means
// we cannot forget to switch threads when adding event handlers. Unfortunately, the DownloadListener
// we give to the app kit is currently an exception and runs on a library thread. It'll get fixed in
@ -147,15 +146,8 @@ public class WalletService {
backupWallet();
// store for later use.
log.error( nodeAddressProxy.toString() );
this.nodeAddressProxy = nodeAddressProxy;
InetSocketAddress addr = new InetSocketAddress(nodeAddressProxy.hostName, nodeAddressProxy.port);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
log.error( nodeAddressProxy.toString() );
// If seed is non-null it means we are restoring from backup.
walletAppKit = new WalletAppKitTorProxy(params, proxy, walletDir, "Bitsquare") {
walletAppKit = new WalletAppKitBitSquare(params, proxy, walletDir, "Bitsquare") {
@Override
protected void onSetupCompleted() {
// Don't make the user wait for confirmations for now, as the intention is they're sending it
@ -330,7 +322,7 @@ public class WalletService {
Context.propagate(ctx);
walletAppKit.stopAsync();
walletAppKit.awaitTerminated();
initialize(seed, nodeAddressProxy, resultHandler, exceptionHandler);
initialize(seed, walletAppKit.getProxy(), resultHandler, exceptionHandler);
} catch (Throwable t) {
t.printStackTrace();
log.error("Executing task failed. " + t.getMessage());

View File

@ -85,6 +85,8 @@ import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.security.Security;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@ -443,15 +445,24 @@ public class MainViewModel implements ViewModel {
btcInfo.set(newValue);
});
NodeAddress nodeAddressProxy = null;
Socks5Proxy proxy = p2PService.getNetworkNode().getSocksProxy();
if( proxy != null ) {
nodeAddressProxy = new NodeAddress(proxy.getInetAddress().getHostName(), proxy.getPort());
// nodeAddressProxy = new NodeAddress("localhost", 9050);
// Use p2p service
Socks5Proxy socks5Proxy = p2PService.getNetworkNode().getSocksProxy();
Proxy proxy = proxy = new Proxy ( Proxy.Type.SOCKS,
new InetSocketAddress(socks5Proxy.getInetAddress().getHostName(),
socks5Proxy.getPort() ) );
/**
* Uncomment this to wire up user specified proxy via program args or config file.
* Could be Tor, i2p, ssh, vpn, etc.
if( preferences.getBitcoinProxyHost() != null &&
preferences.getBitcoinProxyPort() != null ) {
proxy = new Proxy( Proxy.Type.SOCKS, new InetSocketAddress(preferences.getBitcoinProxyHost(),
preferences.getBitcoinProxyPort() );
}
*/
walletService.initialize(null,
nodeAddressProxy,
proxy,
() -> {
numBtcPeers = walletService.numPeersProperty().get();