From b86d05ca9dcd936be3a87cc51b27cb80c0691535 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 5 Jul 2016 17:11:01 +0200 Subject: [PATCH 001/112] Don't log cancel msg at shutdown --- .../src/main/java/io/bitsquare/p2p/peers/BroadcastHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/src/main/java/io/bitsquare/p2p/peers/BroadcastHandler.java b/network/src/main/java/io/bitsquare/p2p/peers/BroadcastHandler.java index 21321398bac..e472774beaa 100644 --- a/network/src/main/java/io/bitsquare/p2p/peers/BroadcastHandler.java +++ b/network/src/main/java/io/bitsquare/p2p/peers/BroadcastHandler.java @@ -83,7 +83,7 @@ public BroadcastHandler(NetworkNode networkNode, PeerManager peerManager) { public void cancel() { stopped = true; - onFault("Broadcast canceled."); + onFault("Broadcast canceled.", false); } From 63334522559e2e3e0fe79ea0257e6d70fbdaba8f Mon Sep 17 00:00:00 2001 From: danda Date: Wed, 13 Jul 2016 14:49:59 -0700 Subject: [PATCH 002/112] initial work/investigation towards routing bitcoinj over jtorproxy. work in progress --- .../io/bitsquare/btc/ProxySocketFactory.java | 94 ++++++++++++++++ .../bitsquare/btc/WalletAppKitTorProxy.java | 104 ++++++++++++++++++ .../java/io/bitsquare/btc/WalletService.java | 21 +++- .../io/bitsquare/gui/main/MainViewModel.java | 62 +++++++---- .../src/main/java/io/nucleo/net/TorNode.java | 4 + .../p2p/network/LocalhostNetworkNode.java | 3 +- .../io/bitsquare/p2p/network/NetworkNode.java | 7 ++ .../bitsquare/p2p/network/TorNetworkNode.java | 8 +- 8 files changed, 272 insertions(+), 31 deletions(-) create mode 100644 core/src/main/java/io/bitsquare/btc/ProxySocketFactory.java create mode 100644 core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java diff --git a/core/src/main/java/io/bitsquare/btc/ProxySocketFactory.java b/core/src/main/java/io/bitsquare/btc/ProxySocketFactory.java new file mode 100644 index 00000000000..0126530e6ea --- /dev/null +++ b/core/src/main/java/io/bitsquare/btc/ProxySocketFactory.java @@ -0,0 +1,94 @@ + +/** + * Copyright (C) 2010-2014 Leon Blakey + * + * This file is part of PircBotX. + * + * PircBotX is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * PircBotX 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * PircBotX. If not, see . + */ +package io.bitsquare.btc; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.Socket; +import java.net.UnknownHostException; +import javax.net.SocketFactory; + +/** + * A basic SocketFactory for creating sockets that connect through the specified + * proxy. + * + * @author Leon Blakey + */ +public class ProxySocketFactory extends SocketFactory { + protected final Proxy proxy; + + /** + * Create all sockets with the specified proxy. + * + * @param proxy An existing proxy + */ + public ProxySocketFactory(Proxy proxy) { + this.proxy = proxy; + } + + /** + * A convenience constructor for creating a proxy with the specified host + * and port. + * + * @param proxyType The type of proxy were connecting to + * @param hostname The hostname of the proxy server + * @param port The port of the proxy server + */ + public ProxySocketFactory(Proxy.Type proxyType, String hostname, int port) { + this.proxy = new Proxy(proxyType, new InetSocketAddress(hostname, port)); + } + + @Override + public Socket createSocket() throws IOException { + Socket socket = new Socket(proxy); + return socket; + } + + @Override + public Socket createSocket(String string, int i) throws IOException, UnknownHostException { + Socket socket = new Socket(proxy); + socket.connect(new InetSocketAddress(string, i)); + return socket; + } + + @Override + public Socket createSocket(String string, int i, InetAddress localAddress, int localPort) throws IOException, UnknownHostException { + Socket socket = new Socket(proxy); + socket.bind(new InetSocketAddress(localAddress, localPort)); + socket.connect(new InetSocketAddress(string, i)); + return socket; + } + + @Override + public Socket createSocket(InetAddress ia, int i) throws IOException { + Socket socket = new Socket(proxy); + socket.connect(new InetSocketAddress(ia, i)); + return socket; + } + + @Override + public Socket createSocket(InetAddress ia, int i, InetAddress localAddress, int localPort) throws IOException { + Socket socket = new Socket(proxy); + socket.bind(new InetSocketAddress(localAddress, localPort)); + socket.connect(new InetSocketAddress(ia, i)); + return socket; + } +} \ No newline at end of file diff --git a/core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java b/core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java new file mode 100644 index 00000000000..fef2b2a7084 --- /dev/null +++ b/core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java @@ -0,0 +1,104 @@ +/* + * 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 . + */ + +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); + } + } +} diff --git a/core/src/main/java/io/bitsquare/btc/WalletService.java b/core/src/main/java/io/bitsquare/btc/WalletService.java index a424f1a5e5b..9526444b07c 100644 --- a/core/src/main/java/io/bitsquare/btc/WalletService.java +++ b/core/src/main/java/io/bitsquare/btc/WalletService.java @@ -30,6 +30,7 @@ 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; @@ -52,6 +53,8 @@ import java.io.File; import java.io.IOException; import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.net.UnknownHostException; import java.nio.file.Paths; import java.util.*; @@ -88,7 +91,8 @@ public class WalletService { private final UserAgent userAgent; private final boolean useTor; - private WalletAppKit walletAppKit; + private WalletAppKitTorProxy walletAppKit; + private NodeAddress nodeAddressProxy; private Wallet wallet; private final IntegerProperty numPeers = new SimpleIntegerProperty(0); private final ObjectProperty> connectedPeers = new SimpleObjectProperty<>(); @@ -128,7 +132,7 @@ public WalletService(RegTestHost regTestHost, TradeWalletService tradeWalletServ // Public Methods /////////////////////////////////////////////////////////////////////////////////////////// - public void initialize(@Nullable DeterministicSeed seed, ResultHandler resultHandler, ExceptionHandler exceptionHandler) { + public void initialize(@Nullable DeterministicSeed seed, NodeAddress nodeAddressProxy, 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 @@ -143,8 +147,15 @@ public void initialize(@Nullable DeterministicSeed seed, ResultHandler resultHan 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 WalletAppKit(params, walletDir, "Bitsquare") { + walletAppKit = new WalletAppKitTorProxy(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 @@ -250,6 +261,8 @@ public List getData(Peer peer, GetDataMessage m) { // from jtorproxy. To get supported it via nio / netty will be harder if (useTor && params.getId().equals(NetworkParameters.ID_MAINNET)) walletAppKit.useTor(); + + // Now configure and start the appkit. This will take a second or two - we could show a temporary splash screen // or progress widget to keep the user engaged whilst we initialise, but we don't. @@ -317,7 +330,7 @@ public void restoreSeedWords(DeterministicSeed seed, ResultHandler resultHandler Context.propagate(ctx); walletAppKit.stopAsync(); walletAppKit.awaitTerminated(); - initialize(seed, resultHandler, exceptionHandler); + initialize(seed, nodeAddressProxy, resultHandler, exceptionHandler); } catch (Throwable t) { t.printStackTrace(); log.error("Executing task failed. " + t.getMessage()); diff --git a/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java b/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java index cda3c1c9838..2a173b34a4b 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java +++ b/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java @@ -18,6 +18,7 @@ package io.bitsquare.gui.main; import com.google.inject.Inject; +import com.runjva.sourceforge.jsocks.protocol.Socks5Proxy; import io.bitsquare.alert.Alert; import io.bitsquare.alert.AlertManager; import io.bitsquare.app.BitsquareApp; @@ -51,6 +52,7 @@ import io.bitsquare.gui.util.BSFormatter; import io.bitsquare.locale.CurrencyUtil; import io.bitsquare.locale.TradeCurrency; +import io.bitsquare.p2p.NodeAddress; import io.bitsquare.p2p.P2PService; import io.bitsquare.p2p.P2PServiceListener; import io.bitsquare.p2p.network.CloseConnectionReason; @@ -222,8 +224,8 @@ public void initializeAllServices() { showStartupTimeoutPopup(); }, 4, TimeUnit.MINUTES); - walletInitialized = initBitcoinWallet(); p2pNetWorkReady = initP2PNetwork(); + walletInitialized = initBitcoinWallet(); // need to store it to not get garbage collected allServicesDone = EasyBind.combine(walletInitialized, p2pNetWorkReady, (a, b) -> a && b); @@ -335,6 +337,7 @@ public void onError(Throwable throwable) { public void onTorNodeReady() { bootstrapState.set("Tor node created"); p2PNetworkIconId.set("image-connection-tor"); + initWalletService(); } @Override @@ -395,6 +398,11 @@ public void onSetupFailed(Throwable throwable) { } private BooleanProperty initBitcoinWallet() { + final BooleanProperty walletInitialized = new SimpleBooleanProperty(); + return walletInitialized; + } + + private void initWalletService() { ObjectProperty walletServiceException = new SimpleObjectProperty<>(); btcInfoBinding = EasyBind.combine(walletService.downloadPercentageProperty(), walletService.numPeersProperty(), walletServiceException, (downloadPercentage, numPeers, exception) -> { @@ -434,30 +442,36 @@ private BooleanProperty initBitcoinWallet() { btcInfoBinding.subscribe((observable, oldValue, newValue) -> { btcInfo.set(newValue); }); - - final BooleanProperty walletInitialized = new SimpleBooleanProperty(); + + NodeAddress nodeAddressProxy = null; + Socks5Proxy proxy = p2PService.getNetworkNode().getSocksProxy(); + if( proxy != null ) { + nodeAddressProxy = new NodeAddress(proxy.getInetAddress().getHostName(), proxy.getPort()); +// nodeAddressProxy = new NodeAddress("localhost", 9050); + } + walletService.initialize(null, - () -> { - numBtcPeers = walletService.numPeersProperty().get(); - - if (walletService.getWallet().isEncrypted()) { - if (p2pNetWorkReady.get()) - splashP2PNetworkProgress.set(0); - - walletPasswordWindow - .onAesKey(aesKey -> { - tradeWalletService.setAesKey(aesKey); - walletService.setAesKey(aesKey); - walletInitialized.set(true); - }) - .hideCloseButton() - .show(); - } else { - walletInitialized.set(true); - } - }, - walletServiceException::set); - return walletInitialized; + nodeAddressProxy, + () -> { + numBtcPeers = walletService.numPeersProperty().get(); + + if (walletService.getWallet().isEncrypted()) { + if (p2pNetWorkReady.get()) + splashP2PNetworkProgress.set(0); + + walletPasswordWindow + .onAesKey(aesKey -> { + tradeWalletService.setAesKey(aesKey); + walletService.setAesKey(aesKey); + walletInitialized.set(true); + }) + .hideCloseButton() + .show(); + } else { + walletInitialized.set(true); + } + }, + walletServiceException::set); } private void onAllServicesInitialized() { diff --git a/jtorproxy/src/main/java/io/nucleo/net/TorNode.java b/jtorproxy/src/main/java/io/nucleo/net/TorNode.java index 9ab8688df58..14efd8b0164 100644 --- a/jtorproxy/src/main/java/io/nucleo/net/TorNode.java +++ b/jtorproxy/src/main/java/io/nucleo/net/TorNode.java @@ -33,6 +33,10 @@ public TorNode(M mgr) throws IOException { log.info("TorSocks running on port " + proxyPort); this.proxy = setupSocksProxy(proxyPort); } + + public Socks5Proxy getSocksProxy() { + return proxy; + } private Socks5Proxy setupSocksProxy(int proxyPort) throws UnknownHostException { Socks5Proxy proxy = new Socks5Proxy(PROXY_LOCALHOST, proxyPort); diff --git a/network/src/main/java/io/bitsquare/p2p/network/LocalhostNetworkNode.java b/network/src/main/java/io/bitsquare/p2p/network/LocalhostNetworkNode.java index 356d83a1891..798b3c16b9d 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/LocalhostNetworkNode.java +++ b/network/src/main/java/io/bitsquare/p2p/network/LocalhostNetworkNode.java @@ -80,8 +80,7 @@ public void start(@Nullable SetupListener setupListener) { protected Socket createSocket(NodeAddress peerNodeAddress) throws IOException { return new Socket(peerNodeAddress.hostName, peerNodeAddress.port); } - - + /////////////////////////////////////////////////////////////////////////////////////////// // Tor delay simulation /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java b/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java index c1f0cdc0b4a..eadb17a198e 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java +++ b/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java @@ -1,6 +1,7 @@ package io.bitsquare.p2p.network; import com.google.common.util.concurrent.*; +import com.runjva.sourceforge.jsocks.protocol.Socks5Proxy; import io.bitsquare.app.Log; import io.bitsquare.common.UserThread; import io.bitsquare.common.util.Utilities; @@ -213,6 +214,12 @@ private OutboundConnection getOutboundConnection(@NotNull NodeAddress peersNodeA } } + @Nullable + public Socks5Proxy getSocksProxy() { + return null; + } + + public SettableFuture sendMessage(Connection connection, Message message) { Log.traceCall("\n\tmessage=" + StringUtils.abbreviate(message.toString(), 100) + "\n\tconnection=" + connection); // connection.sendMessage might take a bit (compression, write to stream), so we use a thread to not block diff --git a/network/src/main/java/io/bitsquare/p2p/network/TorNetworkNode.java b/network/src/main/java/io/bitsquare/p2p/network/TorNetworkNode.java index c516f0a4790..da8c3ce10ff 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/TorNetworkNode.java +++ b/network/src/main/java/io/bitsquare/p2p/network/TorNetworkNode.java @@ -6,6 +6,7 @@ import com.google.common.util.concurrent.MoreExecutors; import com.msopentech.thali.java.toronionproxy.JavaOnionProxyContext; import com.msopentech.thali.java.toronionproxy.JavaOnionProxyManager; +import com.runjva.sourceforge.jsocks.protocol.Socks5Proxy; import io.bitsquare.app.Log; import io.bitsquare.common.Timer; import io.bitsquare.common.UserThread; @@ -91,10 +92,15 @@ public void start(@Nullable SetupListener setupListener) { @Override protected Socket createSocket(NodeAddress peerNodeAddress) throws IOException { - checkArgument(peerNodeAddress.hostName.endsWith(".onion"), "PeerAddress is not an onion address"); + // FIXME: disabling temporarily. + // checkArgument(peerNodeAddress.hostName.endsWith(".onion"), "PeerAddress is not an onion address"); return torNetworkNode.connectToHiddenService(peerNodeAddress.hostName, peerNodeAddress.port); } + + public Socks5Proxy getSocksProxy() { + return torNetworkNode != null ? torNetworkNode.getSocksProxy() : null; + } public void shutDown(Runnable shutDownCompleteHandler) { Log.traceCall(); From 84e78c40fd70bc73ee62263ff969e6dfcee98a7e Mon Sep 17 00:00:00 2001 From: danda Date: Thu, 14 Jul 2016 19:07:54 -0700 Subject: [PATCH 003/112] Change SafeSocks setting to 0. enables bitcoinj over jtorproxy --- jtorproxy/src/main/resources/torrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jtorproxy/src/main/resources/torrc b/jtorproxy/src/main/resources/torrc index a7bb71ac771..aaf3a59544d 100644 --- a/jtorproxy/src/main/resources/torrc +++ b/jtorproxy/src/main/resources/torrc @@ -4,5 +4,5 @@ DisableNetwork 1 AvoidDiskWrites 1 PidFile pid RunAsDaemon 1 -SafeSocks 1 +SafeSocks 0 SOCKSPort auto From 3835d9d09c52d0725395ddb6770e6cf886b4b201 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 19 Jul 2016 17:16:51 +0200 Subject: [PATCH 004/112] Fix time period formatting --- gui/src/main/java/io/bitsquare/gui/util/BSFormatter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gui/src/main/java/io/bitsquare/gui/util/BSFormatter.java b/gui/src/main/java/io/bitsquare/gui/util/BSFormatter.java index 393fe80a7a9..ac7685a9db2 100644 --- a/gui/src/main/java/io/bitsquare/gui/util/BSFormatter.java +++ b/gui/src/main/java/io/bitsquare/gui/util/BSFormatter.java @@ -444,6 +444,8 @@ public static String formatDurationAsWords(long durationMillis, boolean showSeco duration = StringUtils.replaceOnce(duration, " 1 hours", " 1 hour"); duration = StringUtils.replaceOnce(duration, " 1 days", " 1 day"); if (duration.startsWith(", ")) + duration = duration.replace(" ,", ""); + else if (duration.startsWith(", ")) duration = duration.replace(", ", ""); if (duration.equals("")) duration = "Trade period is over"; From 902ce4dd884245dcc980f0673fb5564ddca2f3e5 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 19 Jul 2016 17:40:06 +0200 Subject: [PATCH 005/112] Prevent that shutdown is called twice --- .../java/io/bitsquare/app/BitsquareApp.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java index 92e37ccd383..4110454cb34 100644 --- a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java +++ b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java @@ -346,20 +346,21 @@ private void showFPSWindow() { @Override public void stop() { - shutDownRequested = true; - - new Popup().headLine("Shut down in progress") - .backgroundInfo("Shutting down application can take a few seconds.\n" + - "Please don't interrupt that process.") - .hideCloseButton() - .useAnimation(false) - .show(); - UserThread.runAfter(() -> { - gracefulShutDown(() -> { - log.info("App shutdown complete"); - System.exit(0); - }); - }, 200, TimeUnit.MILLISECONDS); + if (!shutDownRequested) { + new Popup().headLine("Shut down in progress") + .backgroundInfo("Shutting down application can take a few seconds.\n" + + "Please don't interrupt that process.") + .hideCloseButton() + .useAnimation(false) + .show(); + UserThread.runAfter(() -> { + gracefulShutDown(() -> { + log.info("App shutdown complete"); + System.exit(0); + }); + }, 200, TimeUnit.MILLISECONDS); + shutDownRequested = true; + } } private void gracefulShutDown(ResultHandler resultHandler) { From 3bf075b7c22156c0ad614c7487ba34a10aef9ee4 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 19 Jul 2016 18:44:48 +0200 Subject: [PATCH 006/112] Remove scanner as it conflicts when running seednode with nohup --- .../io/bitsquare/seednode/SeedNodeMain.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/seednode/src/main/java/io/bitsquare/seednode/SeedNodeMain.java b/seednode/src/main/java/io/bitsquare/seednode/SeedNodeMain.java index cafdf448d88..eaadf5d789e 100644 --- a/seednode/src/main/java/io/bitsquare/seednode/SeedNodeMain.java +++ b/seednode/src/main/java/io/bitsquare/seednode/SeedNodeMain.java @@ -27,7 +27,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Scanner; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; @@ -36,7 +35,6 @@ public class SeedNodeMain extends BitsquareExecutable { private static final Logger log = LoggerFactory.getLogger(SeedNodeMain.class); private SeedNode seedNode; - private boolean isStopped; public static void main(String[] args) throws Exception { final ThreadFactory threadFactory = new ThreadFactoryBuilder() @@ -82,17 +80,12 @@ protected void doExecute(OptionSet options) { SeedNode.setEnvironment(new BitsquareEnvironment(options)); UserThread.execute(() -> seedNode = new SeedNode()); - while (!isStopped) { + while (true) { try { - Scanner scanner = new Scanner(System.in); - while (scanner.hasNextLine()) { - String inputString = scanner.nextLine(); - if (inputString.equals("q")) { - UserThread.execute(seedNode::shutDown); - isStopped = true; - } - } - } catch (Throwable ignore) { + Thread.sleep(Long.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + log.error(e.getMessage()); } } } From a0947d903f73f32f695697f5038c7d6bdc481155 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 19 Jul 2016 18:45:41 +0200 Subject: [PATCH 007/112] Add more details for logging. --- .../io/bitsquare/p2p/storage/P2PDataStorage.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java b/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java index 99841eb5e16..195da3e2878 100644 --- a/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java +++ b/network/src/main/java/io/bitsquare/p2p/storage/P2PDataStorage.java @@ -446,7 +446,8 @@ private boolean checkSignature(PublicKey ownerPubKey, byte[] hashOfDataAndSeqNr, boolean result = Sig.verify(ownerPubKey, hashOfDataAndSeqNr, signature); if (!result) log.error("Signature verification failed at checkSignature. " + - "That should not happen. Consider it might be an attempt of fraud."); + "That should not happen. ownerPubKey=" + ownerPubKey + + ", hashOfDataAndSeqNr=" + Arrays.toString(hashOfDataAndSeqNr) + ", signature=" + Arrays.toString(signature)); return result; } catch (CryptoException e) { @@ -481,7 +482,10 @@ private boolean checkPublicKeys(ProtectedStorageEntry protectedStorageEntry, boo } if (!result) - log.error("PublicKey of payload data and ProtectedData are not matching. Consider it might be an attempt of fraud"); + log.error("PublicKey of payload data and ProtectedData are not matching. protectedStorageEntry=" + + (protectedStorageEntry != null ? protectedStorageEntry.toString() : "null") + + "protectedStorageEntry.getStoragePayload().getOwnerPubKey()=" + + (protectedStorageEntry.getStoragePayload() != null ? protectedStorageEntry.getStoragePayload().getOwnerPubKey().toString() : "null")); return result; } @@ -489,7 +493,9 @@ private boolean checkIfStoredDataPubKeyMatchesNewDataPubKey(PublicKey ownerPubKe ProtectedStorageEntry storedData = map.get(hashOfData); boolean result = storedData.ownerPubKey != null && storedData.ownerPubKey.equals(ownerPubKey); if (!result) - log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud"); + log.error("New data entry does not match our stored data. storedData.ownerPubKey=" + + (storedData.ownerPubKey != null ? storedData.ownerPubKey.toString() : "null") + + ", ownerPubKey=" + ownerPubKey); return result; } @@ -502,7 +508,8 @@ private boolean checkIfStoredMailboxDataMatchesNewMailboxData(PublicKey receiver boolean result = entry.receiversPubKey.equals(receiversPubKey) && getHashAsByteArray(entry.getStoragePayload()).equals(hashOfData); if (!result) - log.error("New data entry does not match our stored data. Consider it might be an attempt of fraud"); + log.error("New data entry does not match our stored data. entry.receiversPubKey=" + entry.receiversPubKey + + ", receiversPubKey=" + receiversPubKey); return result; } else { From ee9df3e476b94218fe5a1e497ec95f7acdc04c4f Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 19 Jul 2016 18:47:52 +0200 Subject: [PATCH 008/112] Increase delay at shutdown --- .../main/java/io/bitsquare/trade/offer/OpenOfferManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java index 10cbc493b81..f099211e14d 100644 --- a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java +++ b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java @@ -160,7 +160,7 @@ public void shutDown(@Nullable Runnable completeHandler) { if (offerBookService.isBootstrapped()) { openOffers.forEach(openOffer -> offerBookService.removeOfferAtShutDown(openOffer.getOffer())); if (completeHandler != null) - UserThread.runAfter(completeHandler::run, size * 200 + 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(completeHandler::run, size * 500 + 500, TimeUnit.MILLISECONDS); } else { if (completeHandler != null) completeHandler.run(); @@ -174,7 +174,7 @@ public void removeAllOpenOffers(@Nullable Runnable completeHandler) { }, errorMessage -> { })); if (completeHandler != null) - UserThread.runAfter(completeHandler::run, size * 200 + 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(completeHandler::run, size * 500 + 500, TimeUnit.MILLISECONDS); } /////////////////////////////////////////////////////////////////////////////////////////// From f15bbf98eb2440e6c3c58785c506dd6b464f1443 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 19 Jul 2016 19:00:59 +0200 Subject: [PATCH 009/112] Increase timout for shutdown, reduce delay at remove offers at shutdown --- .../main/java/io/bitsquare/trade/offer/OpenOfferManager.java | 4 ++-- gui/src/main/java/io/bitsquare/app/BitsquareApp.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java index f099211e14d..9935b4a9941 100644 --- a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java +++ b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java @@ -160,7 +160,7 @@ public void shutDown(@Nullable Runnable completeHandler) { if (offerBookService.isBootstrapped()) { openOffers.forEach(openOffer -> offerBookService.removeOfferAtShutDown(openOffer.getOffer())); if (completeHandler != null) - UserThread.runAfter(completeHandler::run, size * 500 + 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(completeHandler::run, size * 50 + 500, TimeUnit.MILLISECONDS); } else { if (completeHandler != null) completeHandler.run(); @@ -174,7 +174,7 @@ public void removeAllOpenOffers(@Nullable Runnable completeHandler) { }, errorMessage -> { })); if (completeHandler != null) - UserThread.runAfter(completeHandler::run, size * 500 + 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(completeHandler::run, size * 50 + 500, TimeUnit.MILLISECONDS); } /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java index 4110454cb34..cd27e142097 100644 --- a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java +++ b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java @@ -379,8 +379,8 @@ private void gracefulShutDown(ResultHandler resultHandler) { injector.getInstance(WalletService.class).shutDown(); }); }); - // we wait max 5 sec. - UserThread.runAfter(resultHandler::handleResult, 5); + // we wait max 20 sec. + UserThread.runAfter(resultHandler::handleResult, 20); } else { UserThread.runAfter(resultHandler::handleResult, 1); } From 5cb6a4f9125f900ef8b0b60de46b8e558dbde4f5 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 19 Jul 2016 19:11:56 +0200 Subject: [PATCH 010/112] Adjust delay at remove offers at shutdown --- .../main/java/io/bitsquare/trade/offer/OpenOfferManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java index 9935b4a9941..10cbc493b81 100644 --- a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java +++ b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java @@ -160,7 +160,7 @@ public void shutDown(@Nullable Runnable completeHandler) { if (offerBookService.isBootstrapped()) { openOffers.forEach(openOffer -> offerBookService.removeOfferAtShutDown(openOffer.getOffer())); if (completeHandler != null) - UserThread.runAfter(completeHandler::run, size * 50 + 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(completeHandler::run, size * 200 + 500, TimeUnit.MILLISECONDS); } else { if (completeHandler != null) completeHandler.run(); @@ -174,7 +174,7 @@ public void removeAllOpenOffers(@Nullable Runnable completeHandler) { }, errorMessage -> { })); if (completeHandler != null) - UserThread.runAfter(completeHandler::run, size * 50 + 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(completeHandler::run, size * 200 + 500, TimeUnit.MILLISECONDS); } /////////////////////////////////////////////////////////////////////////////////////////// From fcb08c12ade4f6c137c4a04c36024334f960ef07 Mon Sep 17 00:00:00 2001 From: danda Date: Tue, 19 Jul 2016 12:54:48 -0700 Subject: [PATCH 011/112] some cleanup of the bitcoinj/jtorproxy integration --- .../bitsquare/btc/WalletAppKitBitSquare.java | 59 ++++++++++ .../bitsquare/btc/WalletAppKitTorProxy.java | 104 ------------------ .../java/io/bitsquare/btc/WalletService.java | 16 +-- .../io/bitsquare/gui/main/MainViewModel.java | 25 +++-- 4 files changed, 81 insertions(+), 123 deletions(-) create mode 100644 core/src/main/java/io/bitsquare/btc/WalletAppKitBitSquare.java delete mode 100644 core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java diff --git a/core/src/main/java/io/bitsquare/btc/WalletAppKitBitSquare.java b/core/src/main/java/io/bitsquare/btc/WalletAppKitBitSquare.java new file mode 100644 index 00000000000..d325a09ea77 --- /dev/null +++ b/core/src/main/java/io/bitsquare/btc/WalletAppKitBitSquare.java @@ -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 . + */ + +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; + } +} diff --git a/core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java b/core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java deleted file mode 100644 index fef2b2a7084..00000000000 --- a/core/src/main/java/io/bitsquare/btc/WalletAppKitTorProxy.java +++ /dev/null @@ -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 . - */ - -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); - } - } -} diff --git a/core/src/main/java/io/bitsquare/btc/WalletService.java b/core/src/main/java/io/bitsquare/btc/WalletService.java index 9526444b07c..0f621801819 100644 --- a/core/src/main/java/io/bitsquare/btc/WalletService.java +++ b/core/src/main/java/io/bitsquare/btc/WalletService.java @@ -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> connectedPeers = new SimpleObjectProperty<>(); @@ -132,7 +131,7 @@ public WalletService(RegTestHost regTestHost, TradeWalletService tradeWalletServ // 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 void initialize(@Nullable DeterministicSeed seed, NodeAddress nodeAddress 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 void restoreSeedWords(DeterministicSeed seed, ResultHandler resultHandler 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()); diff --git a/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java b/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java index 2a173b34a4b..5f34a4c7515 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java +++ b/gui/src/main/java/io/bitsquare/gui/main/MainViewModel.java @@ -85,6 +85,8 @@ 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 @@ private void initWalletService() { 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(); From f0e3370dbb74477ae37428dd6a3ac620614a3946 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Wed, 20 Jul 2016 00:34:50 +0200 Subject: [PATCH 012/112] Rename option classes --- ...{OptionKeys.java => CommonOptionKeys.java} | 2 +- .../java/io/bitsquare/alert/AlertManager.java | 4 +- .../alert/PrivateNotificationManager.java | 4 +- .../bitsquare/app/BitsquareEnvironment.java | 37 ++++++++++--------- .../io/bitsquare/app/BitsquareExecutable.java | 19 +++++----- .../io/bitsquare/filter/FilterManager.java | 4 +- .../io/bitsquare/filter/FilterModule.java | 4 +- .../java/io/bitsquare/app/BitsquareApp.java | 4 +- .../java/io/bitsquare/headless/Headless.java | 3 +- .../java/io/bitsquare/monitor/Monitor.java | 3 +- ...OptionKeys.java => NetworkOptionKeys.java} | 2 +- .../main/java/io/bitsquare/p2p/P2PModule.java | 28 +++++++------- .../java/io/bitsquare/p2p/P2PService.java | 18 ++++----- .../java/io/bitsquare/p2p/DummySeedNode.java | 27 +++++++------- .../java/io/bitsquare/seednode/SeedNode.java | 3 +- 15 files changed, 84 insertions(+), 78 deletions(-) rename common/src/main/java/io/bitsquare/common/{OptionKeys.java => CommonOptionKeys.java} (83%) rename network/src/main/java/io/bitsquare/network/{OptionKeys.java => NetworkOptionKeys.java} (93%) diff --git a/common/src/main/java/io/bitsquare/common/OptionKeys.java b/common/src/main/java/io/bitsquare/common/CommonOptionKeys.java similarity index 83% rename from common/src/main/java/io/bitsquare/common/OptionKeys.java rename to common/src/main/java/io/bitsquare/common/CommonOptionKeys.java index 50c66cd6b89..ff4e2c491c6 100644 --- a/common/src/main/java/io/bitsquare/common/OptionKeys.java +++ b/common/src/main/java/io/bitsquare/common/CommonOptionKeys.java @@ -1,6 +1,6 @@ package io.bitsquare.common; -public class OptionKeys { +public class CommonOptionKeys { public static final String LOG_LEVEL_KEY = "logLevel"; public static final String IGNORE_DEV_MSG_KEY = "ignoreDevMsg"; } diff --git a/core/src/main/java/io/bitsquare/alert/AlertManager.java b/core/src/main/java/io/bitsquare/alert/AlertManager.java index 6bcb59f4ca3..32a6dd3ef22 100644 --- a/core/src/main/java/io/bitsquare/alert/AlertManager.java +++ b/core/src/main/java/io/bitsquare/alert/AlertManager.java @@ -19,7 +19,7 @@ import com.google.inject.Inject; import com.google.inject.name.Named; -import io.bitsquare.common.OptionKeys; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.crypto.KeyRing; import io.bitsquare.p2p.P2PService; import io.bitsquare.p2p.storage.HashMapChangedListener; @@ -56,7 +56,7 @@ public class AlertManager { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public AlertManager(P2PService p2PService, KeyRing keyRing, User user, @Named(OptionKeys.IGNORE_DEV_MSG_KEY) boolean ignoreDevMsg) { + public AlertManager(P2PService p2PService, KeyRing keyRing, User user, @Named(CommonOptionKeys.IGNORE_DEV_MSG_KEY) boolean ignoreDevMsg) { this.p2PService = p2PService; this.keyRing = keyRing; this.user = user; diff --git a/core/src/main/java/io/bitsquare/alert/PrivateNotificationManager.java b/core/src/main/java/io/bitsquare/alert/PrivateNotificationManager.java index d653bd2d763..ca8d73aa0a7 100644 --- a/core/src/main/java/io/bitsquare/alert/PrivateNotificationManager.java +++ b/core/src/main/java/io/bitsquare/alert/PrivateNotificationManager.java @@ -19,7 +19,7 @@ import com.google.inject.Inject; import com.google.inject.name.Named; -import io.bitsquare.common.OptionKeys; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.crypto.KeyRing; import io.bitsquare.crypto.DecryptedMsgWithPubKey; import io.bitsquare.p2p.Message; @@ -58,7 +58,7 @@ public class PrivateNotificationManager { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public PrivateNotificationManager(P2PService p2PService, KeyRing keyRing, @Named(OptionKeys.IGNORE_DEV_MSG_KEY) boolean ignoreDevMsg) { + public PrivateNotificationManager(P2PService p2PService, KeyRing keyRing, @Named(CommonOptionKeys.IGNORE_DEV_MSG_KEY) boolean ignoreDevMsg) { this.p2PService = p2PService; this.keyRing = keyRing; diff --git a/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index e00edd87fd8..190c575323a 100644 --- a/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -22,9 +22,10 @@ import io.bitsquare.btc.BitcoinNetwork; import io.bitsquare.btc.UserAgent; import io.bitsquare.btc.WalletService; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.crypto.KeyStorage; import io.bitsquare.common.util.Utilities; -import io.bitsquare.network.OptionKeys; +import io.bitsquare.network.NetworkOptionKeys; import io.bitsquare.storage.Storage; import io.bitsquare.util.spring.JOptCommandLinePropertySource; import joptsimple.OptionSet; @@ -132,23 +133,23 @@ protected BitsquareEnvironment(PropertySource commandLineProperties) { (String) commandLineProperties.getProperty(APP_DATA_DIR_KEY) : appDataDir(userDataDir, appName); - logLevel = commandLineProperties.containsProperty(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY) ? - (String) commandLineProperties.getProperty(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY) : + logLevel = commandLineProperties.containsProperty(CommonOptionKeys.LOG_LEVEL_KEY) ? + (String) commandLineProperties.getProperty(CommonOptionKeys.LOG_LEVEL_KEY) : LOG_LEVEL_DEFAULT; - seedNodes = commandLineProperties.containsProperty(OptionKeys.SEED_NODES_KEY) ? - (String) commandLineProperties.getProperty(OptionKeys.SEED_NODES_KEY) : + seedNodes = commandLineProperties.containsProperty(NetworkOptionKeys.SEED_NODES_KEY) ? + (String) commandLineProperties.getProperty(NetworkOptionKeys.SEED_NODES_KEY) : ""; - myAddress = commandLineProperties.containsProperty(OptionKeys.MY_ADDRESS) ? - (String) commandLineProperties.getProperty(OptionKeys.MY_ADDRESS) : + myAddress = commandLineProperties.containsProperty(NetworkOptionKeys.MY_ADDRESS) ? + (String) commandLineProperties.getProperty(NetworkOptionKeys.MY_ADDRESS) : ""; - banList = commandLineProperties.containsProperty(OptionKeys.BAN_LIST) ? - (String) commandLineProperties.getProperty(OptionKeys.BAN_LIST) : + banList = commandLineProperties.containsProperty(NetworkOptionKeys.BAN_LIST) ? + (String) commandLineProperties.getProperty(NetworkOptionKeys.BAN_LIST) : ""; - ignoreDevMsg = commandLineProperties.containsProperty(io.bitsquare.common.OptionKeys.IGNORE_DEV_MSG_KEY) ? - (String) commandLineProperties.getProperty(io.bitsquare.common.OptionKeys.IGNORE_DEV_MSG_KEY) : + ignoreDevMsg = commandLineProperties.containsProperty(CommonOptionKeys.IGNORE_DEV_MSG_KEY) ? + (String) commandLineProperties.getProperty(CommonOptionKeys.IGNORE_DEV_MSG_KEY) : ""; MutablePropertySources propertySources = this.getPropertySources(); @@ -206,12 +207,12 @@ private PropertySource defaultProperties() { { setProperty(APP_DATA_DIR_KEY, appDataDir); - setProperty(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY, logLevel); + setProperty(CommonOptionKeys.LOG_LEVEL_KEY, logLevel); - setProperty(OptionKeys.SEED_NODES_KEY, seedNodes); - setProperty(OptionKeys.MY_ADDRESS, myAddress); - setProperty(OptionKeys.BAN_LIST, banList); - setProperty(io.bitsquare.common.OptionKeys.IGNORE_DEV_MSG_KEY, ignoreDevMsg); + setProperty(NetworkOptionKeys.SEED_NODES_KEY, seedNodes); + setProperty(NetworkOptionKeys.MY_ADDRESS, myAddress); + setProperty(NetworkOptionKeys.BAN_LIST, banList); + setProperty(CommonOptionKeys.IGNORE_DEV_MSG_KEY, ignoreDevMsg); setProperty(APP_NAME_KEY, appName); setProperty(USER_DATA_DIR_KEY, userDataDir); @@ -223,9 +224,9 @@ private PropertySource defaultProperties() { setProperty(Storage.DIR_KEY, Paths.get(btcNetworkDir, "db").toString()); setProperty(KeyStorage.DIR_KEY, Paths.get(btcNetworkDir, "keys").toString()); - setProperty(OptionKeys.TOR_DIR, Paths.get(btcNetworkDir, "tor").toString()); + setProperty(NetworkOptionKeys.TOR_DIR, Paths.get(btcNetworkDir, "tor").toString()); - setProperty(OptionKeys.NETWORK_ID, String.valueOf(bitcoinNetwork.ordinal())); + setProperty(NetworkOptionKeys.NETWORK_ID, String.valueOf(bitcoinNetwork.ordinal())); } }); } diff --git a/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java b/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java index dd5e75cceb8..5dff054b290 100644 --- a/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java +++ b/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java @@ -20,7 +20,8 @@ import io.bitsquare.BitsquareException; import io.bitsquare.btc.BitcoinNetwork; import io.bitsquare.btc.RegTestHost; -import io.bitsquare.network.OptionKeys; +import io.bitsquare.common.CommonOptionKeys; +import io.bitsquare.network.NetworkOptionKeys; import io.bitsquare.p2p.P2PService; import io.bitsquare.util.joptsimple.EnumValueConverter; import joptsimple.OptionException; @@ -74,29 +75,29 @@ protected void customizeOptionParsing(OptionParser parser) { .withRequiredArg(); parser.accepts(APP_DATA_DIR_KEY, description("Application data directory", DEFAULT_APP_DATA_DIR)) .withRequiredArg(); - parser.accepts(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY, description("Log level [OFF, ALL, ERROR, WARN, INFO, DEBUG, TRACE]", LOG_LEVEL_DEFAULT)) + parser.accepts(CommonOptionKeys.LOG_LEVEL_KEY, description("Log level [OFF, ALL, ERROR, WARN, INFO, DEBUG, TRACE]", LOG_LEVEL_DEFAULT)) .withRequiredArg(); - parser.accepts(OptionKeys.SEED_NODES_KEY, description("Override hard coded seed nodes as comma separated list: E.g. rxdkppp3vicnbgqt.onion:8002, mfla72c4igh5ta2t.onion:8002", "")) + parser.accepts(NetworkOptionKeys.SEED_NODES_KEY, description("Override hard coded seed nodes as comma separated list: E.g. rxdkppp3vicnbgqt.onion:8002, mfla72c4igh5ta2t.onion:8002", "")) .withRequiredArg(); - parser.accepts(OptionKeys.MY_ADDRESS, description("My own onion address (used for botstrap nodes to exclude itself)", "")) + parser.accepts(NetworkOptionKeys.MY_ADDRESS, description("My own onion address (used for botstrap nodes to exclude itself)", "")) .withRequiredArg(); - parser.accepts(OptionKeys.BAN_LIST, description("Nodes to exclude from network connections.", "")) + parser.accepts(NetworkOptionKeys.BAN_LIST, description("Nodes to exclude from network connections.", "")) .withRequiredArg(); - parser.accepts(io.bitsquare.common.OptionKeys.IGNORE_DEV_MSG_KEY, description("If set to true all signed messages from Bitsquare developers are ignored " + + parser.accepts(CommonOptionKeys.IGNORE_DEV_MSG_KEY, description("If set to true all signed messages from Bitsquare developers are ignored " + "(Global alert, Version update alert, Filters for offers, nodes or payment account data)", false)) .withRequiredArg() .ofType(boolean.class); // use a fixed port as arbitrator use that for his ID - parser.accepts(OptionKeys.PORT_KEY, description("Port to listen on", 9999)) + parser.accepts(NetworkOptionKeys.PORT_KEY, description("Port to listen on", 9999)) .withRequiredArg() .ofType(int.class); - parser.accepts(OptionKeys.USE_LOCALHOST, description("Use localhost network for development", false)) + parser.accepts(NetworkOptionKeys.USE_LOCALHOST, description("Use localhost network for development", false)) .withRequiredArg() .ofType(boolean.class); - parser.accepts(OptionKeys.MAX_CONNECTIONS, description("Max. connections a peer will try to keep", P2PService.MAX_CONNECTIONS_DEFAULT)) + parser.accepts(NetworkOptionKeys.MAX_CONNECTIONS, description("Max. connections a peer will try to keep", P2PService.MAX_CONNECTIONS_DEFAULT)) .withRequiredArg() .ofType(int.class); parser.accepts(BitcoinNetwork.KEY, description("Bitcoin network", BitcoinNetwork.DEFAULT)) diff --git a/core/src/main/java/io/bitsquare/filter/FilterManager.java b/core/src/main/java/io/bitsquare/filter/FilterManager.java index ee131d8c312..b7c72a24341 100644 --- a/core/src/main/java/io/bitsquare/filter/FilterManager.java +++ b/core/src/main/java/io/bitsquare/filter/FilterManager.java @@ -19,7 +19,7 @@ import com.google.inject.Inject; import com.google.inject.name.Named; -import io.bitsquare.common.OptionKeys; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.crypto.KeyRing; import io.bitsquare.common.util.Tuple3; import io.bitsquare.common.util.Utilities; @@ -59,7 +59,7 @@ public class FilterManager { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public FilterManager(P2PService p2PService, KeyRing keyRing, User user, @Named(OptionKeys.IGNORE_DEV_MSG_KEY) boolean ignoreDevMsg) { + public FilterManager(P2PService p2PService, KeyRing keyRing, User user, @Named(CommonOptionKeys.IGNORE_DEV_MSG_KEY) boolean ignoreDevMsg) { this.p2PService = p2PService; this.keyRing = keyRing; this.user = user; diff --git a/core/src/main/java/io/bitsquare/filter/FilterModule.java b/core/src/main/java/io/bitsquare/filter/FilterModule.java index c7b90ddae4a..4d1acc5411f 100644 --- a/core/src/main/java/io/bitsquare/filter/FilterModule.java +++ b/core/src/main/java/io/bitsquare/filter/FilterModule.java @@ -19,7 +19,7 @@ import com.google.inject.Singleton; import io.bitsquare.app.AppModule; -import io.bitsquare.common.OptionKeys; +import io.bitsquare.common.CommonOptionKeys; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; @@ -36,6 +36,6 @@ public FilterModule(Environment env) { @Override protected final void configure() { bind(FilterManager.class).in(Singleton.class); - bindConstant().annotatedWith(named(OptionKeys.IGNORE_DEV_MSG_KEY)).to(env.getRequiredProperty(OptionKeys.IGNORE_DEV_MSG_KEY)); + bindConstant().annotatedWith(named(CommonOptionKeys.IGNORE_DEV_MSG_KEY)).to(env.getRequiredProperty(CommonOptionKeys.IGNORE_DEV_MSG_KEY)); } } diff --git a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java index cd27e142097..927f977e38b 100644 --- a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java +++ b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java @@ -24,7 +24,7 @@ import io.bitsquare.alert.AlertManager; import io.bitsquare.arbitration.ArbitratorManager; import io.bitsquare.btc.WalletService; -import io.bitsquare.common.OptionKeys; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.UserThread; import io.bitsquare.common.handlers.ResultHandler; import io.bitsquare.common.util.Utilities; @@ -109,7 +109,7 @@ public void start(Stage stage) throws IOException { log.info("Log files under: " + logPath); Version.printVersion(); Utilities.printSysInfo(); - Log.setLevel(Level.toLevel(env.getRequiredProperty(OptionKeys.LOG_LEVEL_KEY))); + Log.setLevel(Level.toLevel(env.getRequiredProperty(CommonOptionKeys.LOG_LEVEL_KEY))); UserThread.setExecutor(Platform::runLater); UserThread.setTimerClass(UITimer.class); diff --git a/headless/src/main/java/io/bitsquare/headless/Headless.java b/headless/src/main/java/io/bitsquare/headless/Headless.java index ddefe016c7a..58bb882f8bb 100644 --- a/headless/src/main/java/io/bitsquare/headless/Headless.java +++ b/headless/src/main/java/io/bitsquare/headless/Headless.java @@ -8,6 +8,7 @@ import io.bitsquare.app.Version; import io.bitsquare.arbitration.ArbitratorManager; import io.bitsquare.btc.WalletService; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.UserThread; import io.bitsquare.common.handlers.ResultHandler; import io.bitsquare.common.util.Utilities; @@ -45,7 +46,7 @@ public Headless() { log.info("Log files under: " + logPath); Version.printVersion(); Utilities.printSysInfo(); - Log.setLevel(Level.toLevel(env.getRequiredProperty(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY))); + Log.setLevel(Level.toLevel(env.getRequiredProperty(CommonOptionKeys.LOG_LEVEL_KEY))); // setup UncaughtExceptionHandler Thread.UncaughtExceptionHandler handler = (thread, throwable) -> { diff --git a/monitor/src/main/java/io/bitsquare/monitor/Monitor.java b/monitor/src/main/java/io/bitsquare/monitor/Monitor.java index ec8a0c2835b..799adf1106e 100644 --- a/monitor/src/main/java/io/bitsquare/monitor/Monitor.java +++ b/monitor/src/main/java/io/bitsquare/monitor/Monitor.java @@ -8,6 +8,7 @@ import io.bitsquare.app.Version; import io.bitsquare.arbitration.ArbitratorManager; import io.bitsquare.btc.WalletService; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.UserThread; import io.bitsquare.common.handlers.ResultHandler; import io.bitsquare.common.util.Utilities; @@ -46,7 +47,7 @@ public Monitor() { log.info("Log files under: " + logPath); Version.printVersion(); Utilities.printSysInfo(); - Log.setLevel(Level.toLevel(env.getRequiredProperty(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY))); + Log.setLevel(Level.toLevel(env.getRequiredProperty(CommonOptionKeys.LOG_LEVEL_KEY))); // setup UncaughtExceptionHandler Thread.UncaughtExceptionHandler handler = (thread, throwable) -> { diff --git a/network/src/main/java/io/bitsquare/network/OptionKeys.java b/network/src/main/java/io/bitsquare/network/NetworkOptionKeys.java similarity index 93% rename from network/src/main/java/io/bitsquare/network/OptionKeys.java rename to network/src/main/java/io/bitsquare/network/NetworkOptionKeys.java index da89860de59..a3597259ffb 100644 --- a/network/src/main/java/io/bitsquare/network/OptionKeys.java +++ b/network/src/main/java/io/bitsquare/network/NetworkOptionKeys.java @@ -1,6 +1,6 @@ package io.bitsquare.network; -public class OptionKeys { +public class NetworkOptionKeys { public static final String TOR_DIR = "torDir"; public static final String USE_LOCALHOST = "useLocalhost"; public static final String MAX_CONNECTIONS = "maxConnections"; diff --git a/network/src/main/java/io/bitsquare/p2p/P2PModule.java b/network/src/main/java/io/bitsquare/p2p/P2PModule.java index dbd2ddb7ddf..ea1d65fe32e 100644 --- a/network/src/main/java/io/bitsquare/p2p/P2PModule.java +++ b/network/src/main/java/io/bitsquare/p2p/P2PModule.java @@ -20,7 +20,7 @@ import com.google.inject.Singleton; import com.google.inject.name.Names; import io.bitsquare.app.AppModule; -import io.bitsquare.network.OptionKeys; +import io.bitsquare.network.NetworkOptionKeys; import io.bitsquare.p2p.seed.SeedNodesRepository; import org.springframework.core.env.Environment; @@ -40,23 +40,23 @@ protected void configure() { bind(SeedNodesRepository.class).in(Singleton.class); bind(P2PService.class).in(Singleton.class); - Boolean useLocalhost = env.getProperty(OptionKeys.USE_LOCALHOST, boolean.class, false); - bind(boolean.class).annotatedWith(Names.named(OptionKeys.USE_LOCALHOST)).toInstance(useLocalhost); + Boolean useLocalhost = env.getProperty(NetworkOptionKeys.USE_LOCALHOST, boolean.class, false); + bind(boolean.class).annotatedWith(Names.named(NetworkOptionKeys.USE_LOCALHOST)).toInstance(useLocalhost); - File torDir = new File(env.getRequiredProperty(OptionKeys.TOR_DIR)); - bind(File.class).annotatedWith(named(OptionKeys.TOR_DIR)).toInstance(torDir); + File torDir = new File(env.getRequiredProperty(NetworkOptionKeys.TOR_DIR)); + bind(File.class).annotatedWith(named(NetworkOptionKeys.TOR_DIR)).toInstance(torDir); // use a fixed port as arbitrator use that for his ID - Integer port = env.getProperty(OptionKeys.PORT_KEY, int.class, 9999); - bind(int.class).annotatedWith(Names.named(OptionKeys.PORT_KEY)).toInstance(port); + Integer port = env.getProperty(NetworkOptionKeys.PORT_KEY, int.class, 9999); + bind(int.class).annotatedWith(Names.named(NetworkOptionKeys.PORT_KEY)).toInstance(port); - Integer maxConnections = env.getProperty(OptionKeys.MAX_CONNECTIONS, int.class, P2PService.MAX_CONNECTIONS_DEFAULT); - bind(int.class).annotatedWith(Names.named(OptionKeys.MAX_CONNECTIONS)).toInstance(maxConnections); + Integer maxConnections = env.getProperty(NetworkOptionKeys.MAX_CONNECTIONS, int.class, P2PService.MAX_CONNECTIONS_DEFAULT); + bind(int.class).annotatedWith(Names.named(NetworkOptionKeys.MAX_CONNECTIONS)).toInstance(maxConnections); - Integer networkId = env.getProperty(OptionKeys.NETWORK_ID, int.class, 1); - bind(int.class).annotatedWith(Names.named(OptionKeys.NETWORK_ID)).toInstance(networkId); - bindConstant().annotatedWith(named(OptionKeys.SEED_NODES_KEY)).to(env.getRequiredProperty(OptionKeys.SEED_NODES_KEY)); - bindConstant().annotatedWith(named(OptionKeys.MY_ADDRESS)).to(env.getRequiredProperty(OptionKeys.MY_ADDRESS)); - bindConstant().annotatedWith(named(OptionKeys.BAN_LIST)).to(env.getRequiredProperty(OptionKeys.BAN_LIST)); + Integer networkId = env.getProperty(NetworkOptionKeys.NETWORK_ID, int.class, 1); + bind(int.class).annotatedWith(Names.named(NetworkOptionKeys.NETWORK_ID)).toInstance(networkId); + bindConstant().annotatedWith(named(NetworkOptionKeys.SEED_NODES_KEY)).to(env.getRequiredProperty(NetworkOptionKeys.SEED_NODES_KEY)); + bindConstant().annotatedWith(named(NetworkOptionKeys.MY_ADDRESS)).to(env.getRequiredProperty(NetworkOptionKeys.MY_ADDRESS)); + bindConstant().annotatedWith(named(NetworkOptionKeys.BAN_LIST)).to(env.getRequiredProperty(NetworkOptionKeys.BAN_LIST)); } } \ No newline at end of file diff --git a/network/src/main/java/io/bitsquare/p2p/P2PService.java b/network/src/main/java/io/bitsquare/p2p/P2PService.java index 90bbe550505..f5cfd350b80 100644 --- a/network/src/main/java/io/bitsquare/p2p/P2PService.java +++ b/network/src/main/java/io/bitsquare/p2p/P2PService.java @@ -14,7 +14,7 @@ import io.bitsquare.common.crypto.PubKeyRing; import io.bitsquare.crypto.DecryptedMsgWithPubKey; import io.bitsquare.crypto.EncryptionService; -import io.bitsquare.network.OptionKeys; +import io.bitsquare.network.NetworkOptionKeys; import io.bitsquare.p2p.messaging.*; import io.bitsquare.p2p.network.*; import io.bitsquare.p2p.peers.BanList; @@ -102,15 +102,15 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis // Called also from SeedNodeP2PService @Inject public P2PService(SeedNodesRepository seedNodesRepository, - @Named(OptionKeys.PORT_KEY) int port, - @Named(OptionKeys.TOR_DIR) File torDir, - @Named(OptionKeys.USE_LOCALHOST) boolean useLocalhost, - @Named(OptionKeys.NETWORK_ID) int networkId, - @Named(OptionKeys.MAX_CONNECTIONS) int maxConnections, + @Named(NetworkOptionKeys.PORT_KEY) int port, + @Named(NetworkOptionKeys.TOR_DIR) File torDir, + @Named(NetworkOptionKeys.USE_LOCALHOST) boolean useLocalhost, + @Named(NetworkOptionKeys.NETWORK_ID) int networkId, + @Named(NetworkOptionKeys.MAX_CONNECTIONS) int maxConnections, @Named(Storage.DIR_KEY) File storageDir, - @Named(OptionKeys.SEED_NODES_KEY) String seedNodes, - @Named(OptionKeys.MY_ADDRESS) String myAddress, - @Named(OptionKeys.BAN_LIST) String banList, + @Named(NetworkOptionKeys.SEED_NODES_KEY) String seedNodes, + @Named(NetworkOptionKeys.MY_ADDRESS) String myAddress, + @Named(NetworkOptionKeys.BAN_LIST) String banList, Clock clock, @Nullable EncryptionService encryptionService, @Nullable KeyRing keyRing) { diff --git a/network/src/test/java/io/bitsquare/p2p/DummySeedNode.java b/network/src/test/java/io/bitsquare/p2p/DummySeedNode.java index 0853134c388..b28648cab0f 100644 --- a/network/src/test/java/io/bitsquare/p2p/DummySeedNode.java +++ b/network/src/test/java/io/bitsquare/p2p/DummySeedNode.java @@ -5,9 +5,10 @@ import io.bitsquare.app.Log; import io.bitsquare.app.Version; import io.bitsquare.common.Clock; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.UserThread; import io.bitsquare.common.util.Utilities; -import io.bitsquare.network.OptionKeys; +import io.bitsquare.network.NetworkOptionKeys; import io.bitsquare.p2p.peers.BanList; import io.bitsquare.p2p.seed.SeedNodesRepository; import org.jetbrains.annotations.Nullable; @@ -77,30 +78,30 @@ public void processArgs(String[] args) { String arg = args[i]; if (arg.startsWith("--")) arg = arg.substring(2); - if (arg.startsWith(OptionKeys.MY_ADDRESS)) { - arg = arg.substring(OptionKeys.MY_ADDRESS.length() + 1); + if (arg.startsWith(NetworkOptionKeys.MY_ADDRESS)) { + arg = arg.substring(NetworkOptionKeys.MY_ADDRESS.length() + 1); checkArgument(arg.contains(":") && arg.split(":").length == 2 && arg.split(":")[1].length() > 3, "Wrong program argument: " + arg); mySeedNodeAddress = new NodeAddress(arg); log.info("From processArgs: mySeedNodeAddress=" + mySeedNodeAddress); - } else if (arg.startsWith(OptionKeys.NETWORK_ID)) { - arg = arg.substring(OptionKeys.NETWORK_ID.length() + 1); + } else if (arg.startsWith(NetworkOptionKeys.NETWORK_ID)) { + arg = arg.substring(NetworkOptionKeys.NETWORK_ID.length() + 1); networkId = Integer.parseInt(arg); log.info("From processArgs: networkId=" + networkId); checkArgument(networkId > -1 && networkId < 3, "networkId out of scope (Mainnet = 0, TestNet = 1, Regtest = 2)"); Version.setBtcNetworkId(networkId); - } else if (arg.startsWith(OptionKeys.MAX_CONNECTIONS)) { - arg = arg.substring(OptionKeys.MAX_CONNECTIONS.length() + 1); + } else if (arg.startsWith(NetworkOptionKeys.MAX_CONNECTIONS)) { + arg = arg.substring(NetworkOptionKeys.MAX_CONNECTIONS.length() + 1); maxConnections = Integer.parseInt(arg); log.info("From processArgs: maxConnections=" + maxConnections); checkArgument(maxConnections < MAX_CONNECTIONS_LIMIT, "maxConnections seems to be a bit too high..."); - } else if (arg.startsWith(OptionKeys.USE_LOCALHOST)) { - arg = arg.substring(OptionKeys.USE_LOCALHOST.length() + 1); + } else if (arg.startsWith(NetworkOptionKeys.USE_LOCALHOST)) { + arg = arg.substring(NetworkOptionKeys.USE_LOCALHOST.length() + 1); checkArgument(arg.equals("true") || arg.equals("false")); useLocalhost = ("true").equals(arg); log.info("From processArgs: useLocalhost=" + useLocalhost); - } else if (arg.startsWith(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY)) { - arg = arg.substring(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY.length() + 1); + } else if (arg.startsWith(CommonOptionKeys.LOG_LEVEL_KEY)) { + arg = arg.substring(CommonOptionKeys.LOG_LEVEL_KEY.length() + 1); logLevel = Level.toLevel(arg.toUpperCase()); log.info("From processArgs: logLevel=" + logLevel); } else if (arg.startsWith(SEED_NODES_LIST)) { @@ -116,8 +117,8 @@ public void processArgs(String[] args) { }); log.info("From processArgs: progArgSeedNodes=" + progArgSeedNodes); progArgSeedNodes.remove(mySeedNodeAddress); - } else if (arg.startsWith(OptionKeys.BAN_LIST)) { - arg = arg.substring(OptionKeys.BAN_LIST.length() + 1); + } else if (arg.startsWith(NetworkOptionKeys.BAN_LIST)) { + arg = arg.substring(NetworkOptionKeys.BAN_LIST.length() + 1); checkArgument(arg.contains(":") && arg.split(":").length > 1 && arg.split(":")[1].length() > 3, "Wrong program argument " + arg); List list = Arrays.asList(arg.split(",")); diff --git a/seednode/src/main/java/io/bitsquare/seednode/SeedNode.java b/seednode/src/main/java/io/bitsquare/seednode/SeedNode.java index 15732a13f51..dd10fbaab0d 100644 --- a/seednode/src/main/java/io/bitsquare/seednode/SeedNode.java +++ b/seednode/src/main/java/io/bitsquare/seednode/SeedNode.java @@ -8,6 +8,7 @@ import io.bitsquare.app.Version; import io.bitsquare.arbitration.ArbitratorManager; import io.bitsquare.btc.WalletService; +import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.common.UserThread; import io.bitsquare.common.handlers.ResultHandler; import io.bitsquare.common.util.Utilities; @@ -42,7 +43,7 @@ public SeedNode() { log.info("Log files under: " + logPath); Version.printVersion(); Utilities.printSysInfo(); - Log.setLevel(Level.toLevel(env.getRequiredProperty(io.bitsquare.common.OptionKeys.LOG_LEVEL_KEY))); + Log.setLevel(Level.toLevel(env.getRequiredProperty(CommonOptionKeys.LOG_LEVEL_KEY))); // setup UncaughtExceptionHandler Thread.UncaughtExceptionHandler handler = (thread, throwable) -> { From d4946ef4434c3ebdcc610d69239f0e4eb689df3d Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Wed, 20 Jul 2016 01:13:08 +0200 Subject: [PATCH 013/112] Add seednodes and useTor options, re-enable useTor in preferences and settingsview --- .../bitsquare/app/BitsquareEnvironment.java | 16 ++++++++-- .../io/bitsquare/app/BitsquareExecutable.java | 6 ++++ .../java/io/bitsquare/btc/BitcoinModule.java | 3 ++ .../java/io/bitsquare/btc/BtcOptionKeys.java | 6 ++++ .../java/io/bitsquare/btc/WalletService.java | 27 +++++++++++++--- .../java/io/bitsquare/user/Preferences.java | 10 +++--- .../settings/network/NetworkSettingsView.fxml | 32 +++++++++---------- .../settings/network/NetworkSettingsView.java | 14 +++++--- 8 files changed, 81 insertions(+), 33 deletions(-) create mode 100644 core/src/main/java/io/bitsquare/btc/BtcOptionKeys.java diff --git a/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index 190c575323a..fa1d91bb557 100644 --- a/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/core/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -20,6 +20,7 @@ import ch.qos.logback.classic.Level; import io.bitsquare.BitsquareException; import io.bitsquare.btc.BitcoinNetwork; +import io.bitsquare.btc.BtcOptionKeys; import io.bitsquare.btc.UserAgent; import io.bitsquare.btc.WalletService; import io.bitsquare.common.CommonOptionKeys; @@ -83,8 +84,7 @@ public static void setDefaultAppName(String defaultAppName) { private final String btcNetworkDir; private final String logLevel; private BitcoinNetwork bitcoinNetwork; - private final String seedNodes, ignoreDevMsg; - private final String myAddress, banList; + private final String btcSeedNodes, seedNodes, ignoreDevMsg, useTorForBtc, myAddress, banList; public BitsquareEnvironment(OptionSet options) { this(new JOptCommandLinePropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME, checkNotNull( @@ -152,6 +152,15 @@ protected BitsquareEnvironment(PropertySource commandLineProperties) { (String) commandLineProperties.getProperty(CommonOptionKeys.IGNORE_DEV_MSG_KEY) : ""; + btcSeedNodes = commandLineProperties.containsProperty(BtcOptionKeys.BTC_SEED_NODES) ? + (String) commandLineProperties.getProperty(BtcOptionKeys.BTC_SEED_NODES) : + ""; + + useTorForBtc = commandLineProperties.containsProperty(BtcOptionKeys.USE_TOR_FOR_BTC) ? + (String) commandLineProperties.getProperty(BtcOptionKeys.USE_TOR_FOR_BTC) : + ""; + + MutablePropertySources propertySources = this.getPropertySources(); propertySources.addFirst(commandLineProperties); try { @@ -214,6 +223,9 @@ private PropertySource defaultProperties() { setProperty(NetworkOptionKeys.BAN_LIST, banList); setProperty(CommonOptionKeys.IGNORE_DEV_MSG_KEY, ignoreDevMsg); + setProperty(BtcOptionKeys.BTC_SEED_NODES, btcSeedNodes); + setProperty(BtcOptionKeys.USE_TOR_FOR_BTC, useTorForBtc); + setProperty(APP_NAME_KEY, appName); setProperty(USER_DATA_DIR_KEY, userDataDir); diff --git a/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java b/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java index 5dff054b290..8f46771cb78 100644 --- a/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java +++ b/core/src/main/java/io/bitsquare/app/BitsquareExecutable.java @@ -19,6 +19,7 @@ import io.bitsquare.BitsquareException; import io.bitsquare.btc.BitcoinNetwork; +import io.bitsquare.btc.BtcOptionKeys; import io.bitsquare.btc.RegTestHost; import io.bitsquare.common.CommonOptionKeys; import io.bitsquare.network.NetworkOptionKeys; @@ -90,6 +91,11 @@ protected void customizeOptionParsing(OptionParser parser) { .withRequiredArg() .ofType(boolean.class); + parser.accepts(BtcOptionKeys.BTC_SEED_NODES, description("Custom seed nodes used for BitcoinJ.", "")) + .withRequiredArg(); + parser.accepts(BtcOptionKeys.USE_TOR_FOR_BTC, description("If set to true BitcoinJ is routed over our native Tor instance.", "")) + .withRequiredArg(); + // use a fixed port as arbitrator use that for his ID parser.accepts(NetworkOptionKeys.PORT_KEY, description("Port to listen on", 9999)) .withRequiredArg() diff --git a/core/src/main/java/io/bitsquare/btc/BitcoinModule.java b/core/src/main/java/io/bitsquare/btc/BitcoinModule.java index 9342570216e..421e7bed084 100644 --- a/core/src/main/java/io/bitsquare/btc/BitcoinModule.java +++ b/core/src/main/java/io/bitsquare/btc/BitcoinModule.java @@ -48,6 +48,9 @@ protected void configure() { File walletDir = new File(env.getRequiredProperty(WalletService.DIR_KEY)); bind(File.class).annotatedWith(named(WalletService.DIR_KEY)).toInstance(walletDir); + bindConstant().annotatedWith(named(BtcOptionKeys.BTC_SEED_NODES)).to(env.getRequiredProperty(BtcOptionKeys.BTC_SEED_NODES)); + bindConstant().annotatedWith(named(BtcOptionKeys.USE_TOR_FOR_BTC)).to(env.getRequiredProperty(BtcOptionKeys.USE_TOR_FOR_BTC)); + bind(AddressEntryList.class).in(Singleton.class); bind(TradeWalletService.class).in(Singleton.class); bind(WalletService.class).in(Singleton.class); diff --git a/core/src/main/java/io/bitsquare/btc/BtcOptionKeys.java b/core/src/main/java/io/bitsquare/btc/BtcOptionKeys.java new file mode 100644 index 00000000000..04231ac4f70 --- /dev/null +++ b/core/src/main/java/io/bitsquare/btc/BtcOptionKeys.java @@ -0,0 +1,6 @@ +package io.bitsquare.btc; + +public class BtcOptionKeys { + public static final String BTC_SEED_NODES = "btcSeedNodes"; + public static final String USE_TOR_FOR_BTC = "useTorForBtc"; +} diff --git a/core/src/main/java/io/bitsquare/btc/WalletService.java b/core/src/main/java/io/bitsquare/btc/WalletService.java index 1c338326dbf..ae173f404a7 100644 --- a/core/src/main/java/io/bitsquare/btc/WalletService.java +++ b/core/src/main/java/io/bitsquare/btc/WalletService.java @@ -85,6 +85,7 @@ public class WalletService { private final RegTestHost regTestHost; private final TradeWalletService tradeWalletService; private final AddressEntryList addressEntryList; + private final String seedNodes; private final NetworkParameters params; private final File walletDir; private final UserAgent userAgent; @@ -105,14 +106,30 @@ public class WalletService { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public WalletService(RegTestHost regTestHost, TradeWalletService tradeWalletService, AddressEntryList addressEntryList, UserAgent userAgent, - @Named(DIR_KEY) File appDir, Preferences preferences) { + public WalletService(RegTestHost regTestHost, + TradeWalletService tradeWalletService, + AddressEntryList addressEntryList, + UserAgent userAgent, + @Named(DIR_KEY) File appDir, + Preferences preferences, + @Named(BtcOptionKeys.BTC_SEED_NODES) String seedNodes, + @Named(BtcOptionKeys.USE_TOR_FOR_BTC) String useTorFlagFromOptions) { this.regTestHost = regTestHost; this.tradeWalletService = tradeWalletService; this.addressEntryList = addressEntryList; + this.seedNodes = seedNodes; this.params = preferences.getBitcoinNetwork().getParameters(); this.walletDir = new File(appDir, "bitcoin"); this.userAgent = userAgent; + + // We support a checkbox in the settings to set the use tor flag. + // If we get the options set we override that setting. + if (useTorFlagFromOptions != null && !useTorFlagFromOptions.isEmpty()) { + if (useTorFlagFromOptions.equals("false")) + preferences.setUseTorForBitcoinJ(false); + else if (useTorFlagFromOptions.equals("true")) + preferences.setUseTorForBitcoinJ(true); + } useTor = preferences.getUseTorForBitcoinJ(); storage = new Storage<>(walletDir); @@ -247,9 +264,11 @@ public List getData(Peer peer, GetDataMessage m) { // 1333 / (2800 + 1333) = 0.32 -> 32 % probability that a pub key is in our wallet walletAppKit.setBloomFilterFalsePositiveRate(0.00005); + // Pass custom seed nodes if set in options + if (seedNodes != null && !seedNodes.isEmpty()) { + //TODO Check how to pass seed nodes to the wallet kit. Probably via walletAppKit.setPeerNodes + } - // TODO Get bitcoinj running over our tor proxy. BlockingClientManager need to be used to use the socket - // from jtorproxy. To get supported it via nio / netty will be harder if (useTor && params.getId().equals(NetworkParameters.ID_MAINNET)) walletAppKit.useTor(); diff --git a/core/src/main/java/io/bitsquare/user/Preferences.java b/core/src/main/java/io/bitsquare/user/Preferences.java index 141704b031f..94dbcb9c62c 100644 --- a/core/src/main/java/io/bitsquare/user/Preferences.java +++ b/core/src/main/java/io/bitsquare/user/Preferences.java @@ -111,8 +111,7 @@ public static TradeCurrency getDefaultTradeCurrency() { private boolean autoSelectArbitrators = true; private final Map dontShowAgainMap; private boolean tacAccepted; - // Don't remove as we don't want to break old serialized data - private boolean useTorForBitcoinJ = false; + private boolean useTorForBitcoinJ = true; private boolean showOwnOffersInOfferBook = true; private Locale preferredLocale; private TradeCurrency preferredTradeCurrency; @@ -185,8 +184,7 @@ public Preferences(Storage storage, BitsquareEnvironment bitsquareE defaultLocale = preferredLocale; preferredTradeCurrency = persisted.getPreferredTradeCurrency(); defaultTradeCurrency = preferredTradeCurrency; - // useTorForBitcoinJ = persisted.getUseTorForBitcoinJ(); - useTorForBitcoinJ = false; + useTorForBitcoinJ = persisted.getUseTorForBitcoinJ(); useStickyMarketPrice = persisted.getUseStickyMarketPrice(); usePercentageBasedPrice = persisted.getUsePercentageBasedPrice(); showOwnOffersInOfferBook = persisted.getShowOwnOffersInOfferBook(); @@ -372,10 +370,10 @@ public void setNonTradeTxFeePerKB(long nonTradeTxFeePerKB) throws Exception { storage.queueUpForSave(); } - /* public void setUseTorForBitcoinJ(boolean useTorForBitcoinJ) { + public void setUseTorForBitcoinJ(boolean useTorForBitcoinJ) { this.useTorForBitcoinJ = useTorForBitcoinJ; storage.queueUpForSave(); - }*/ + } public void setShowOwnOffersInOfferBook(boolean showOwnOffersInOfferBook) { this.showOwnOffersInOfferBook = showOwnOffersInOfferBook; diff --git a/gui/src/main/java/io/bitsquare/gui/main/settings/network/NetworkSettingsView.fxml b/gui/src/main/java/io/bitsquare/gui/main/settings/network/NetworkSettingsView.fxml index 44006d185e4..e39a9bddc87 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/settings/network/NetworkSettingsView.fxml +++ b/gui/src/main/java/io/bitsquare/gui/main/settings/network/NetworkSettingsView.fxml @@ -34,14 +34,14 @@ - -