Skip to content

Commit

Permalink
Make Config option fields public and inline accessors
Browse files Browse the repository at this point in the history
See updated Config Javadoc for rationale.
  • Loading branch information
cbeams committed Jan 10, 2020
1 parent 29abb9e commit 63205d7
Show file tree
Hide file tree
Showing 36 changed files with 218 additions and 476 deletions.
416 changes: 79 additions & 337 deletions common/src/main/java/bisq/common/config/Config.java

Large diffs are not rendered by default.

76 changes: 38 additions & 38 deletions common/src/test/java/bisq/common/config/ConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ConfigTests {
@Test
public void whenNoArgCtorIsCalled_thenDefaultAppNameIsSetToTempValue() {
Config config = new Config();
String defaultAppName = config.getDefaultAppName();
String defaultAppName = config.defaultAppName;
String regex = "Bisq\\d{2,}Temp";
assertTrue(format("Temp app name '%s' failed to match '%s'", defaultAppName, regex),
defaultAppName.matches(regex));
Expand All @@ -44,56 +44,56 @@ public void whenNoArgCtorIsCalled_thenDefaultAppNameIsSetToTempValue() {
@Test
public void whenAppNameOptionIsSet_thenAppNamePropertyDiffersFromDefaultAppNameProperty() {
Config config = configWithOpts(opt(APP_NAME, "My-Bisq"));
assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getAppName(), not(equalTo(config.getDefaultAppName())));
assertThat(config.appName, equalTo("My-Bisq"));
assertThat(config.appName, not(equalTo(config.defaultAppName)));
}

@Test
public void whenNoOptionsAreSet_thenDataDirPropertiesEqualDefaultValues() {
Config config = new Config();
assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(config.getDefaultAppDataDir()));
assertThat(config.getConfigFile(), equalTo(config.getDefaultConfigFile()));
assertThat(config.appName, equalTo(config.defaultAppName));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(config.defaultAppDataDir));
assertThat(config.configFile, equalTo(config.defaultConfigFile));
}

@Test
public void whenAppNameOptionIsSet_thenDataDirPropertiesReflectItsValue() {
Config config = configWithOpts(opt(APP_NAME, "My-Bisq"));
assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), "My-Bisq")));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.appName, equalTo("My-Bisq"));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(new File(config.userDataDir, "My-Bisq")));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
public void whenAppDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
File appDataDir = Files.createTempDirectory("myapp").toFile();
Config config = configWithOpts(opt(APP_DATA_DIR, appDataDir));
assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(appDataDir));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.appName, equalTo(config.defaultAppName));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(appDataDir));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
public void whenUserDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
File userDataDir = Files.createTempDirectory("myuserdata").toFile();
Config config = configWithOpts(opt(USER_DATA_DIR, userDataDir));
assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(userDataDir));
assertThat(config.getAppDataDir(), equalTo(new File(userDataDir, config.getDefaultAppName())));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.appName, equalTo(config.defaultAppName));
assertThat(config.userDataDir, equalTo(userDataDir));
assertThat(config.appDataDir, equalTo(new File(userDataDir, config.defaultAppName)));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
public void whenAppNameAndAppDataDirOptionsAreSet_thenDataDirPropertiesReflectTheirValues() throws IOException {
File appDataDir = Files.createTempDirectory("myapp").toFile();
Config config = configWithOpts(opt(APP_NAME, "My-Bisq"), opt(APP_DATA_DIR, appDataDir));
assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(appDataDir));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.appName, equalTo("My-Bisq"));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(appDataDir));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
Expand All @@ -103,7 +103,7 @@ public void whenOptionIsSetAtCommandLineAndInConfigFile_thenCommandLineValueTake
writer.println(new ConfigFileOption(APP_NAME, "Bisq-configFileValue"));
}
Config config = configWithOpts(opt(APP_NAME, "Bisq-commandLineValue"));
assertThat(config.getAppName(), equalTo("Bisq-commandLineValue"));
assertThat(config.appName, equalTo("Bisq-commandLineValue"));
}

@Test
Expand Down Expand Up @@ -142,7 +142,7 @@ public void whenConfigFileOptionIsSetInConfigFile_thenConfigExceptionIsThrown()
public void whenConfigFileOptionIsSetToExistingFile_thenConfigFilePropertyReflectsItsValue() throws IOException {
File configFile = File.createTempFile("bisq", "properties");
Config config = configWithOpts(opt(CONFIG_FILE, configFile.getAbsolutePath()));
assertThat(config.getConfigFile(), equalTo(configFile));
assertThat(config.configFile, equalTo(configFile));
}

@Test
Expand All @@ -151,7 +151,7 @@ public void whenConfigFileOptionIsSetToRelativePath_thenThePathIsPrefixedByAppDa
File appDataDir = configFile.getParentFile();
String relativeConfigFilePath = configFile.getName();
Config config = configWithOpts(opt(APP_DATA_DIR, appDataDir), opt(CONFIG_FILE, relativeConfigFilePath));
assertThat(config.getConfigFile(), equalTo(configFile));
assertThat(config.configFile, equalTo(configFile));
}

@Test
Expand All @@ -161,22 +161,22 @@ public void whenAppNameIsSetInConfigFile_thenDataDirPropertiesReflectItsValue()
writer.println(new ConfigFileOption(APP_NAME, "My-Bisq"));
}
Config config = configWithOpts(opt(CONFIG_FILE, configFile.getAbsolutePath()));
assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), config.getAppName())));
assertThat(config.getConfigFile(), equalTo(configFile));
assertThat(config.appName, equalTo("My-Bisq"));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(new File(config.userDataDir, config.appName)));
assertThat(config.configFile, equalTo(configFile));
}

@Test
public void whenBannedBtcNodesOptionIsSet_thenBannedBtcNodesPropertyReturnsItsValue() {
Config config = configWithOpts(opt(BANNED_BTC_NODES, "foo.onion:8333,bar.onion:8333"));
assertThat(config.getBannedBtcNodes(), contains("foo.onion:8333", "bar.onion:8333"));
assertThat(config.bannedBtcNodes, contains("foo.onion:8333", "bar.onion:8333"));
}

@Test
public void whenHelpOptionIsSet_thenIsHelpRequestedIsTrue() {
assertFalse(new Config().isHelpRequested());
assertTrue(configWithOpts(opt(HELP)).isHelpRequested());
assertFalse(new Config().helpRequested);
assertTrue(configWithOpts(opt(HELP)).helpRequested);
}

@Test
Expand All @@ -201,11 +201,11 @@ public void whenConfigIsConstructed_thenNoConsoleOutputSideEffectsShouldOccur()
@Test
public void whenConfigIsConstructed_thenAppDataDirAndSubdirsAreCreated() {
Config config = new Config();
assertTrue(config.getAppDataDir().exists());
assertTrue(config.getKeyStorageDir().exists());
assertTrue(config.getStorageDir().exists());
assertTrue(config.getTorDir().exists());
assertTrue(config.getWalletDir().exists());
assertTrue(config.appDataDir.exists());
assertTrue(config.keyStorageDir.exists());
assertTrue(config.storageDir.exists());
assertTrue(config.torDir.exists());
assertTrue(config.walletDir.exists());
}

@Test
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/bisq/core/CoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ protected void configure() {

bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class);

bind(File.class).annotatedWith(named(STORAGE_DIR)).toInstance(config.getStorageDir());
bind(File.class).annotatedWith(named(STORAGE_DIR)).toInstance(config.storageDir);

CoinFormatter btcFormatter = new ImmutableCoinFormatter(config.getBaseCurrencyNetworkParameters().getMonetaryFormat());
CoinFormatter btcFormatter = new ImmutableCoinFormatter(config.baseCurrencyNetworkParameters.getMonetaryFormat());
bind(CoinFormatter.class).annotatedWith(named(FormattingUtils.BTC_FORMATTER_KEY)).toInstance(btcFormatter);

bind(File.class).annotatedWith(named(KEY_STORAGE_DIR)).toInstance(config.getKeyStorageDir());
bind(File.class).annotatedWith(named(KEY_STORAGE_DIR)).toInstance(config.keyStorageDir);

bind(NetworkProtoResolver.class).to(CoreNetworkProtoResolver.class);
bind(PersistenceProtoResolver.class).to(CorePersistenceProtoResolver.class);

bindConstant().annotatedWith(named(USE_DEV_PRIVILEGE_KEYS)).to(config.isUseDevPrivilegeKeys());
bindConstant().annotatedWith(named(USE_DEV_MODE)).to(config.isUseDevMode());
bindConstant().annotatedWith(named(REFERRAL_ID)).to(config.getReferralId());
bindConstant().annotatedWith(named(USE_DEV_PRIVILEGE_KEYS)).to(config.useDevPrivilegeKeys);
bindConstant().annotatedWith(named(USE_DEV_MODE)).to(config.useDevMode);
bindConstant().annotatedWith(named(REFERRAL_ID)).to(config.referralId);

bindConstant().annotatedWith(named(LOCAL_BITCOIN_NODE_PORT))
.to(config.getBaseCurrencyNetworkParameters().getPort());
.to(config.baseCurrencyNetworkParameters.getPort());


// ordering is used for shut down sequence
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/alert/AlertModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public AlertModule(Config config) {
protected final void configure() {
bind(AlertManager.class).in(Singleton.class);
bind(PrivateNotificationManager.class).in(Singleton.class);
bindConstant().annotatedWith(named(IGNORE_DEV_MSG)).to(config.isIgnoreDevMsg());
bindConstant().annotatedWith(named(IGNORE_DEV_MSG)).to(config.ignoreDevMsg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void play() {
InputStream inputStream = null;
try {
inputStream = getClass().getClassLoader().getResourceAsStream("prevent-app-nap-silent-sound.aiff");
File soundFile = new File(config.getAppDataDir(), "prevent-app-nap-silent-sound.aiff");
File soundFile = new File(config.appDataDir, "prevent-app-nap-silent-sound.aiff");
if (!soundFile.exists()) {
outputStream = new FileOutputStream(soundFile);
IOUtils.copy(inputStream, outputStream);
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/bisq/core/app/BisqExecutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public BisqExecutable(String fullName, String scriptName, String appName, String
public void execute(String[] args) {
try {
config = new Config(appName, osUserDataDir(), args);
if (config.isHelpRequested()) {
if (config.helpRequested) {
config.printHelp(System.out, new BisqHelpFormatter(fullName, scriptName, version));
System.exit(EXIT_SUCCESS);
}
Expand Down Expand Up @@ -148,8 +148,8 @@ protected void applyInjector() {
}

protected void setupDevEnv() {
DevEnv.setDevMode(config.isUseDevMode());
DevEnv.setDaoActivated(config.isDaoActivated());
DevEnv.setDevMode(config.useDevMode);
DevEnv.setDaoActivated(config.daoActivated);
}

protected void setupPersistedDataHosts(Injector injector) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/bisq/core/app/BisqSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ private void maybeShowTac() {
}

private void detectLocalBitcoinNode(Runnable nextStep) {
BaseCurrencyNetwork baseCurrencyNetwork = config.getBaseCurrencyNetwork();
if (config.isIgnoreLocalBtcNode() || baseCurrencyNetwork.isDaoRegTest() || baseCurrencyNetwork.isDaoTestNet()) {
BaseCurrencyNetwork baseCurrencyNetwork = config.baseCurrencyNetwork;
if (config.ignoreLocalBtcNode || baseCurrencyNetwork.isDaoRegTest() || baseCurrencyNetwork.isDaoTestNet()) {
nextStep.run();
return;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/app/SetupUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static BooleanProperty readFromResources(P2PDataStorage p2PDataStorage, C
BooleanProperty result = new SimpleBooleanProperty();
new Thread(() -> {
// Used to load different files per base currency (EntryMap_BTC_MAINNET, EntryMap_LTC,...)
final BaseCurrencyNetwork baseCurrencyNetwork = config.getBaseCurrencyNetwork();
final BaseCurrencyNetwork baseCurrencyNetwork = config.baseCurrencyNetwork;
final String postFix = "_" + baseCurrencyNetwork.name();
long ts = new Date().getTime();
p2PDataStorage.readFromResources(postFix);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/bisq/core/app/WalletAppSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ void init(@Nullable Consumer<String> chainFileLockedExceptionHandler,

private String getBtcNetworkAsString() {
String postFix;
if (config.isIgnoreLocalBtcNode())
if (config.ignoreLocalBtcNode)
postFix = " " + Res.get("mainView.footer.localhostBitcoinNode");
else if (preferences.getUseTorForBitcoinJ())
postFix = " " + Res.get("mainView.footer.usingTor");
else
postFix = "";
return Res.get(config.getBaseCurrencyNetwork().name()) + postFix;
return Res.get(config.baseCurrencyNetwork.name()) + postFix;
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/app/misc/AppSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AppSetup(EncryptionService encryptionService,
this.keyRing = keyRing;
this.config = config;

Version.setBaseCryptoNetworkId(this.config.getBaseCurrencyNetwork().ordinal());
Version.setBaseCryptoNetworkId(this.config.baseCurrencyNetwork.ordinal());
Version.printVersion();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public AppSetupWithP2PAndDAO(EncryptionService encryptionService,
this.daoSetup = daoSetup;

// TODO Should be refactored/removed. In the meantime keep in sync with CorePersistedDataHost
if (config.isDaoActivated()) {
if (config.daoActivated) {
persistedDataHosts.add(myVoteListService);
persistedDataHosts.add(ballotListService);
persistedDataHosts.add(myBlindVoteListService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected void startShutDownInterval(GracefulShutDownHandler gracefulShutDownHan
}

protected void checkMemory(Config config, GracefulShutDownHandler gracefulShutDownHandler) {
int maxMemory = config.getMaxMemory();
int maxMemory = config.maxMemory;
UserThread.runPeriodically(() -> {
Profiler.printSystemLoad(log);
if (!stopped) {
Expand Down Expand Up @@ -183,7 +183,7 @@ protected void restart(Config config, GracefulShutDownHandler gracefulShutDownHa
gracefulShutDownHandler.gracefulShutDown(() -> {
//noinspection finally
try {
final String[] tokens = config.getAppDataDir().getPath().split("_");
final String[] tokens = config.appDataDir.getPath().split("_");
String logPath = "error_" + (tokens.length > 1 ? tokens[tokens.length - 2] : "") + ".log";
RestartUtil.restartApplication(logPath);
} catch (IOException e) {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/bisq/core/app/misc/ModuleForAppWithP2p.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ protected void configure() {

bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);

bind(File.class).annotatedWith(named(STORAGE_DIR)).toInstance(config.getStorageDir());
bind(File.class).annotatedWith(named(KEY_STORAGE_DIR)).toInstance(config.getKeyStorageDir());
bind(File.class).annotatedWith(named(STORAGE_DIR)).toInstance(config.storageDir);
bind(File.class).annotatedWith(named(KEY_STORAGE_DIR)).toInstance(config.keyStorageDir);

bindConstant().annotatedWith(named(USE_DEV_PRIVILEGE_KEYS)).to(config.isUseDevPrivilegeKeys());
bindConstant().annotatedWith(named(USE_DEV_MODE)).to(config.isUseDevMode());
bindConstant().annotatedWith(named(REFERRAL_ID)).to(config.getReferralId());
bindConstant().annotatedWith(named(USE_DEV_PRIVILEGE_KEYS)).to(config.useDevPrivilegeKeys);
bindConstant().annotatedWith(named(USE_DEV_MODE)).to(config.useDevMode);
bindConstant().annotatedWith(named(REFERRAL_ID)).to(config.referralId);

bindConstant().annotatedWith(named(LOCAL_BITCOIN_NODE_PORT))
.to(config.getBaseCurrencyNetworkParameters().getPort());
.to(config.baseCurrencyNetworkParameters.getPort());

// ordering is used for shut down sequence
install(new TradeModule(config));
Expand Down
Loading

0 comments on commit 63205d7

Please sign in to comment.