forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BisqExecutable.java
475 lines (412 loc) · 20.6 KB
/
BisqExecutable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* This file is part of Bisq.
*
* Bisq 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.
*
* Bisq 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 Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.app;
import bisq.core.arbitration.ArbitratorManager;
import bisq.core.btc.BaseCurrencyNetwork;
import bisq.core.btc.BtcOptionKeys;
import bisq.core.btc.setup.RegTestHost;
import bisq.core.btc.setup.WalletsSetup;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.dao.DaoOptionKeys;
import bisq.core.dao.DaoSetup;
import bisq.core.exceptions.BisqException;
import bisq.core.offer.OpenOfferManager;
import bisq.core.setup.CorePersistedDataHost;
import bisq.core.setup.CoreSetup;
import bisq.core.trade.TradeManager;
import bisq.core.util.joptsimple.EnumValueConverter;
import bisq.network.NetworkOptionKeys;
import bisq.network.p2p.P2PService;
import bisq.common.CommonOptionKeys;
import bisq.common.UserThread;
import bisq.common.app.AppModule;
import bisq.common.app.DevEnv;
import bisq.common.handlers.ResultHandler;
import bisq.common.proto.persistable.PersistedDataHost;
import bisq.common.setup.GracefulShutDownHandler;
import bisq.common.storage.CorruptedDatabaseFilesHandler;
import bisq.common.storage.Storage;
import bisq.common.util.Utilities;
import org.springframework.core.env.JOptCommandLinePropertySource;
import org.springframework.util.StringUtils;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import static bisq.core.app.BisqEnvironment.DEFAULT_APP_NAME;
import static bisq.core.app.BisqEnvironment.DEFAULT_USER_DATA_DIR;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;
import static java.lang.String.join;
@Slf4j
public abstract class BisqExecutable implements GracefulShutDownHandler {
static {
Utilities.removeCryptographyRestrictions();
}
protected Injector injector;
protected AppModule module;
protected BisqEnvironment bisqEnvironment;
public static boolean setupInitialOptionParser(String[] args) throws IOException {
// We don't want to do the full argument parsing here as that might easily change in update versions
// So we only handle the absolute minimum which is APP_NAME, APP_DATA_DIR_KEY and USER_DATA_DIR
OptionParser parser = new OptionParser();
parser.allowsUnrecognizedOptions();
parser.accepts(AppOptionKeys.USER_DATA_DIR_KEY, description("User data directory", DEFAULT_USER_DATA_DIR))
.withRequiredArg();
parser.accepts(AppOptionKeys.APP_NAME_KEY, description("Application name", DEFAULT_APP_NAME))
.withRequiredArg();
OptionSet options;
try {
options = parser.parse(args);
} catch (OptionException ex) {
System.out.println("error: " + ex.getMessage());
System.out.println();
parser.printHelpOn(System.out);
System.exit(EXIT_FAILURE);
return false;
}
BisqEnvironment bisqEnvironment = getBisqEnvironment(options);
// need to call that before BisqAppMain().execute(args)
BisqExecutable.initAppDir(bisqEnvironment.getProperty(AppOptionKeys.APP_DATA_DIR_KEY));
return true;
}
private static final int EXIT_SUCCESS = 0;
public static final int EXIT_FAILURE = 1;
private static final String HELP_KEY = "help";
public void execute(String[] args) throws Exception {
OptionParser parser = new OptionParser();
parser.accepts(HELP_KEY, "This help text").forHelp();
this.customizeOptionParsing(parser);
OptionSet options;
try {
options = parser.parse(args);
if (options.has(HELP_KEY)) {
parser.printHelpOn(System.out);
System.exit(EXIT_SUCCESS);
return;
}
} catch (OptionException ex) {
System.out.println("error: " + ex.getMessage());
System.out.println();
parser.printHelpOn(System.out);
System.exit(EXIT_FAILURE);
return;
}
this.doExecute(options);
}
///////////////////////////////////////////////////////////////////////////////////////////
// First synchronous execution tasks
///////////////////////////////////////////////////////////////////////////////////////////
protected void doExecute(OptionSet options) {
setupEnvironment(options);
configUserThread();
configCoreSetup(options);
addCapabilities();
// If application is JavaFX application we need to wait until it is initialized
launchApplication();
}
protected abstract void configUserThread();
protected void setupEnvironment(OptionSet options) {
bisqEnvironment = getBisqEnvironment(options);
}
protected void configCoreSetup(OptionSet options) {
CoreSetup.setup(getBisqEnvironment(options));
}
protected void addCapabilities() {
}
// The onApplicationLaunched call must map to UserThread, so that all following methods are running in the
// thread the application is running and we don't run into thread interference.
protected abstract void launchApplication();
///////////////////////////////////////////////////////////////////////////////////////////
// If application is a JavaFX application we need wait for onApplicationLaunched
///////////////////////////////////////////////////////////////////////////////////////////
// Headless versions can call inside launchApplication the onApplicationLaunched() manually
protected void onApplicationLaunched() {
setupGuice();
startApplication();
}
///////////////////////////////////////////////////////////////////////////////////////////
// We continue with a series of synchronous execution tasks
///////////////////////////////////////////////////////////////////////////////////////////
protected void setupGuice() {
module = getModule();
injector = getInjector();
applyInjector();
}
protected abstract AppModule getModule();
protected Injector getInjector() {
return Guice.createInjector(module);
}
protected void applyInjector() {
setupDevEnv();
setCorruptedDataBaseFilesHandler();
setupPersistedDataHosts(injector);
}
protected void setupDevEnv() {
DevEnv.setDevMode(injector.getInstance(Key.get(Boolean.class, Names.named(CommonOptionKeys.USE_DEV_MODE))));
BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork();
boolean isRegTestOrTestNet = (baseCurrencyNetwork.isTestnet() || baseCurrencyNetwork.isRegtest());
boolean isDaoActivatedOptionSet = injector.getInstance(Key.get(Boolean.class, Names.named(DaoOptionKeys.DAO_ACTIVATED)));
DevEnv.setDaoActivated(isDaoActivatedOptionSet || isRegTestOrTestNet);
}
private void setCorruptedDataBaseFilesHandler() {
CorruptedDatabaseFilesHandler corruptedDatabaseFilesHandler = injector.getInstance(CorruptedDatabaseFilesHandler.class);
Storage.setCorruptedDatabaseFilesHandler(corruptedDatabaseFilesHandler);
}
protected void setupPersistedDataHosts(Injector injector) {
try {
PersistedDataHost.apply(CorePersistedDataHost.getPersistedDataHosts(injector));
} catch (Throwable t) {
// If we are in dev mode we want to get the exception if some db files are corrupted
// We need to delay it as the stage is not created yet and so popups would not be shown.
if (DevEnv.isDevMode())
UserThread.runAfter(() -> {
log.error("Error at PersistedDataHost.apply: " + t.toString());
throw t;
}, 2);
}
}
protected abstract void startApplication();
// Once the application is ready we get that callback and we start the setup
protected void onApplicationStarted() {
startAppSetup();
}
protected void startAppSetup() {
BisqSetup bisqSetup = injector.getInstance(BisqSetup.class);
bisqSetup.start();
}
///////////////////////////////////////////////////////////////////////////////////////////
// GracefulShutDownHandler implementation
///////////////////////////////////////////////////////////////////////////////////////////
// This might need to be overwritten in case the application is not using all modules
@Override
public void gracefulShutDown(ResultHandler resultHandler) {
try {
if (injector != null) {
injector.getInstance(ArbitratorManager.class).shutDown();
injector.getInstance(TradeManager.class).shutDown();
injector.getInstance(DaoSetup.class).shutDown();
injector.getInstance(OpenOfferManager.class).shutDown(() -> {
injector.getInstance(P2PService.class).shutDown(() -> {
injector.getInstance(WalletsSetup.class).shutDownComplete.addListener((ov, o, n) -> {
module.close(injector);
log.debug("Graceful shutdown completed");
resultHandler.handleResult();
System.exit(0);
});
injector.getInstance(WalletsSetup.class).shutDown();
injector.getInstance(BtcWalletService.class).shutDown();
injector.getInstance(BsqWalletService.class).shutDown();
});
});
// we wait max 20 sec.
UserThread.runAfter(() -> {
log.warn("Timeout triggered resultHandler");
resultHandler.handleResult();
}, 20);
} else {
log.warn("injector == null triggered resultHandler");
UserThread.runAfter(resultHandler::handleResult, 1);
}
} catch (Throwable t) {
log.error("App shutdown failed with exception");
t.printStackTrace();
System.exit(1);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// customizeOptionParsing
///////////////////////////////////////////////////////////////////////////////////////////
protected void customizeOptionParsing(OptionParser parser) {
//CommonOptionKeys
parser.accepts(CommonOptionKeys.LOG_LEVEL_KEY,
description("Log level [OFF, ALL, ERROR, WARN, INFO, DEBUG, TRACE]", BisqEnvironment.LOG_LEVEL_DEFAULT))
.withRequiredArg();
//NetworkOptionKeys
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(NetworkOptionKeys.MY_ADDRESS,
description("My own onion address (used for bootstrap nodes to exclude itself)", ""))
.withRequiredArg();
parser.accepts(NetworkOptionKeys.BAN_LIST,
description("Nodes to exclude from network connections.", ""))
.withRequiredArg();
// use a fixed port as arbitrator use that for his ID
parser.accepts(NetworkOptionKeys.PORT_KEY,
description("Port to listen on", 9999))
.withRequiredArg()
.ofType(int.class);
parser.accepts(NetworkOptionKeys.USE_LOCALHOST_FOR_P2P,
description("Use localhost P2P network for development", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(NetworkOptionKeys.MAX_CONNECTIONS,
description("Max. connections a peer will try to keep", P2PService.MAX_CONNECTIONS_DEFAULT))
.withRequiredArg()
.ofType(int.class);
parser.accepts(NetworkOptionKeys.SOCKS_5_PROXY_BTC_ADDRESS,
description("A proxy address to be used for Bitcoin network. [host:port]", ""))
.withRequiredArg();
parser.accepts(NetworkOptionKeys.SOCKS_5_PROXY_HTTP_ADDRESS,
description("A proxy address to be used for Http requests (should be non-Tor). [host:port]", ""))
.withRequiredArg();
//AppOptionKeys
parser.accepts(AppOptionKeys.USER_DATA_DIR_KEY,
description("User data directory", BisqEnvironment.DEFAULT_USER_DATA_DIR))
.withRequiredArg();
parser.accepts(AppOptionKeys.APP_NAME_KEY,
description("Application name", BisqEnvironment.DEFAULT_APP_NAME))
.withRequiredArg();
parser.accepts(AppOptionKeys.MAX_MEMORY,
description("Max. permitted memory (used only at headless versions)", 600))
.withRequiredArg();
parser.accepts(AppOptionKeys.APP_DATA_DIR_KEY,
description("Application data directory", BisqEnvironment.DEFAULT_APP_DATA_DIR))
.withRequiredArg();
parser.accepts(AppOptionKeys.IGNORE_DEV_MSG_KEY,
description("If set to true all signed network_messages from bisq developers are ignored " +
"(Global alert, Version update alert, Filters for offers, nodes or trading account data)", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(AppOptionKeys.DESKTOP_WITH_HTTP_API,
description("If set to true Bisq Desktop starts with Http API", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(AppOptionKeys.DESKTOP_WITH_GRPC_API,
description("If set to true Bisq Desktop starts with gRPC API", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS,
description("If that is true all the privileged features which requires a private key to enable it are overridden by a dev key pair " +
"(This is for developers only!)", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(AppOptionKeys.REFERRAL_ID,
description("Optional Referral ID (e.g. for API users or pro market makers)", ""))
.withRequiredArg();
parser.accepts(CommonOptionKeys.USE_DEV_MODE,
description("Enables dev mode which is used for convenience for developer testing", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(AppOptionKeys.DUMP_STATISTICS,
description("If set to true the trade statistics are stored as json file in the data dir.", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(AppOptionKeys.PROVIDERS,
description("Custom providers (comma separated)", false))
.withRequiredArg();
//BtcOptionKeys
parser.accepts(BtcOptionKeys.BASE_CURRENCY_NETWORK,
description("Base currency network", BisqEnvironment.getDefaultBaseCurrencyNetwork().name()))
.withRequiredArg()
.ofType(String.class);
//.withValuesConvertedBy(new EnumValueConverter(String.class));
parser.accepts(BtcOptionKeys.REG_TEST_HOST,
description("", RegTestHost.DEFAULT))
.withRequiredArg()
.ofType(RegTestHost.class)
.withValuesConvertedBy(new EnumValueConverter(RegTestHost.class));
parser.accepts(BtcOptionKeys.BTC_NODES,
description("Custom nodes used for BitcoinJ as comma separated IP addresses.", ""))
.withRequiredArg();
parser.accepts(BtcOptionKeys.USE_TOR_FOR_BTC,
description("If set to true BitcoinJ is routed over tor (socks 5 proxy).", ""))
.withRequiredArg();
parser.accepts(BtcOptionKeys.SOCKS5_DISCOVER_MODE,
description("Specify discovery mode for Bitcoin nodes. One or more of: [ADDR, DNS, ONION, ALL]" +
" (comma separated, they get OR'd together). Default value is ALL", "ALL"))
.withRequiredArg();
parser.accepts(BtcOptionKeys.USE_ALL_PROVIDED_NODES,
description("Set to true if connection of bitcoin nodes should include clear net nodes", ""))
.withRequiredArg();
parser.accepts(BtcOptionKeys.USER_AGENT,
description("User agent at btc node connections", ""))
.withRequiredArg();
parser.accepts(BtcOptionKeys.NUM_CONNECTIONS_FOR_BTC,
description("Number of connections to the Bitcoin network", "9"))
.withRequiredArg();
//RpcOptionKeys
parser.accepts(DaoOptionKeys.RPC_USER,
description("Bitcoind rpc username", ""))
.withRequiredArg();
parser.accepts(DaoOptionKeys.RPC_PASSWORD,
description("Bitcoind rpc password", ""))
.withRequiredArg();
parser.accepts(DaoOptionKeys.RPC_PORT,
description("Bitcoind rpc port", ""))
.withRequiredArg();
parser.accepts(DaoOptionKeys.RPC_BLOCK_NOTIFICATION_PORT,
description("Bitcoind rpc port for block notifications", ""))
.withRequiredArg();
parser.accepts(DaoOptionKeys.DUMP_BLOCKCHAIN_DATA,
description("If set to true the blockchain data from RPC requests to Bitcoin Core are stored " +
"as json file in the data dir.", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(DaoOptionKeys.FULL_DAO_NODE,
description("If set to true the node requests the blockchain data via RPC requests from Bitcoin Core and " +
"provide the validated BSQ txs to the network. It requires that the other RPC properties are " +
"set as well.", false))
.withRequiredArg()
.ofType(boolean.class);
parser.accepts(DaoOptionKeys.GENESIS_TX_ID,
description("Genesis transaction ID when not using the hard coded one", ""))
.withRequiredArg();
parser.accepts(DaoOptionKeys.GENESIS_BLOCK_HEIGHT,
description("Genesis transaction block height when not using the hard coded one", -1))
.withRequiredArg();
parser.accepts(DaoOptionKeys.DAO_ACTIVATED,
description("Developer flag. If true it enables dao phase 2 features.", false))
.withRequiredArg()
.ofType(boolean.class);
}
public static BisqEnvironment getBisqEnvironment(OptionSet options) {
return new BisqEnvironment(new JOptCommandLinePropertySource(BisqEnvironment.BISQ_COMMANDLINE_PROPERTY_SOURCE_NAME, checkNotNull(options)));
}
protected static String description(String descText, Object defaultValue) {
String description = "";
if (StringUtils.hasText(descText))
description = description.concat(descText);
if (defaultValue != null)
description = join(" ", description, format("(default: %s)", defaultValue));
return description;
}
public static void initAppDir(String appDir) {
Path dir = Paths.get(appDir);
if (Files.exists(dir)) {
if (!Files.isWritable(dir))
throw new BisqException("Application data directory '%s' is not writeable", dir);
else
return;
}
try {
Files.createDirectories(dir);
} catch (IOException ex) {
throw new BisqException(ex, "Application data directory '%s' could not be created", dir);
}
}
}