Skip to content

Commit

Permalink
singletonify the rest of the core module
Browse files Browse the repository at this point in the history
  • Loading branch information
christophsturm committed Aug 6, 2019
1 parent 48cbf64 commit 690d744
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 21 deletions.
23 changes: 2 additions & 21 deletions core/src/main/java/bisq/core/CoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@
import bisq.core.dao.DaoModule;
import bisq.core.filter.FilterModule;
import bisq.core.network.p2p.seed.DefaultSeedNodeRepository;
import bisq.core.notifications.MobileMessageEncryption;
import bisq.core.notifications.MobileModel;
import bisq.core.notifications.MobileNotificationService;
import bisq.core.notifications.MobileNotificationValidator;
import bisq.core.notifications.alerts.DisputeMsgEvents;
import bisq.core.notifications.alerts.MyOfferTakenEvents;
import bisq.core.notifications.alerts.TradeEvents;
import bisq.core.notifications.alerts.market.MarketAlerts;
import bisq.core.notifications.alerts.price.PriceAlert;
import bisq.core.offer.OfferModule;
import bisq.core.presentation.CorePresentationModule;
import bisq.core.proto.network.CoreNetworkProtoResolver;
Expand All @@ -55,7 +46,6 @@

import org.springframework.core.env.Environment;

import com.google.inject.Singleton;
import com.google.inject.name.Names;

import java.io.File;
Expand All @@ -82,8 +72,8 @@ protected void configure() {
File keyStorageDir = new File(environment.getRequiredProperty(KeyStorage.KEY_STORAGE_DIR));
bind(File.class).annotatedWith(named(KeyStorage.KEY_STORAGE_DIR)).toInstance(keyStorageDir);

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

Boolean useDevPrivilegeKeys = environment.getProperty(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS, Boolean.class, false);
bind(boolean.class).annotatedWith(Names.named(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS)).toInstance(useDevPrivilegeKeys);
Expand All @@ -94,15 +84,6 @@ protected void configure() {
String referralId = environment.getProperty(AppOptionKeys.REFERRAL_ID, String.class, "");
bind(String.class).annotatedWith(Names.named(AppOptionKeys.REFERRAL_ID)).toInstance(referralId);

bind(MobileNotificationService.class).in(Singleton.class);
bind(MobileMessageEncryption.class).in(Singleton.class);
bind(MobileNotificationValidator.class).in(Singleton.class);
bind(MobileModel.class).in(Singleton.class);
bind(MyOfferTakenEvents.class).in(Singleton.class);
bind(TradeEvents.class).in(Singleton.class);
bind(DisputeMsgEvents.class).in(Singleton.class);
bind(PriceAlert.class).in(Singleton.class);
bind(MarketAlerts.class).in(Singleton.class);

// ordering is used for shut down sequence
install(tradeModule());
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/bisq/core/notifications/MobileModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package bisq.core.notifications;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.google.common.annotations.VisibleForTesting;

Expand All @@ -29,6 +30,7 @@

@Data
@Slf4j
@Singleton
public class MobileModel {
public static final String PHONE_SEPARATOR_ESCAPED = "\\|"; // see https://stackoverflow.com/questions/5675704/java-string-split-not-returning-the-right-values
public static final String PHONE_SEPARATOR_WRITING = "|";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.gson.Gson;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import javax.inject.Named;

Expand All @@ -54,6 +55,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

@Slf4j
@Singleton
public class MobileNotificationService {
// Used in Relay app to response of a success state. We won't want a code dependency just for that string so we keep it
// duplicated in relay and here. Must not be changed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package bisq.core.notifications;

import javax.inject.Inject;
import javax.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class MobileNotificationValidator {
@Inject
public MobileNotificationValidator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import bisq.network.p2p.P2PService;

import javax.inject.Inject;
import javax.inject.Singleton;

import javafx.collections.ListChangeListener;

Expand All @@ -36,6 +37,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class DisputeMsgEvents {
private final P2PService p2PService;
private final MobileNotificationService mobileNotificationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import bisq.core.offer.OpenOfferManager;

import javax.inject.Inject;
import javax.inject.Singleton;

import javafx.collections.ListChangeListener;

Expand All @@ -33,6 +34,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class MyOfferTakenEvents {
private final OpenOfferManager openOfferManager;
private final MobileNotificationService mobileNotificationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import bisq.common.crypto.PubKeyRing;

import javax.inject.Inject;
import javax.inject.Singleton;

import javafx.collections.ListChangeListener;

Expand All @@ -38,6 +39,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class TradeEvents {
private final PubKeyRing pubKeyRing;
private final TradeManager tradeManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import org.bitcoinj.utils.Fiat;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.List;
import java.util.UUID;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class MarketAlerts {
private final OfferBookService offerBookService;
private final MobileNotificationService mobileNotificationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
import org.bitcoinj.utils.Fiat;

import javax.inject.Inject;
import javax.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class PriceAlert {
private final PriceFeedService priceFeedService;
private final MobileNotificationService mobileNotificationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@
import bisq.common.proto.network.NetworkProtoResolver;

import javax.inject.Inject;
import javax.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

// TODO Use ProtobufferException instead of ProtobufferRuntimeException
@Slf4j
@Singleton
public class CoreNetworkProtoResolver extends CoreProtoResolver implements NetworkProtoResolver {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.io.File;

import lombok.extern.slf4j.Slf4j;

// TODO Use ProtobufferException instead of ProtobufferRuntimeException
@Slf4j
@Singleton
public class CorePersistenceProtoResolver extends CoreProtoResolver implements PersistenceProtoResolver {
private final Provider<BtcWalletService> btcWalletService;
private final NetworkProtoResolver networkProtoResolver;
Expand Down
27 changes: 27 additions & 0 deletions desktop/src/test/java/bisq/desktop/GuiceSetupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.network.p2p.seed.DefaultSeedNodeRepository;
import bisq.core.notifications.MobileModel;
import bisq.core.notifications.MobileNotificationService;
import bisq.core.notifications.MobileNotificationValidator;
import bisq.core.notifications.alerts.MyOfferTakenEvents;
import bisq.core.notifications.alerts.TradeEvents;
import bisq.core.notifications.alerts.market.MarketAlerts;
import bisq.core.notifications.alerts.price.PriceAlert;
import bisq.core.payment.TradeLimits;
import bisq.core.proto.network.CoreNetworkProtoResolver;
import bisq.core.proto.persistable.CorePersistenceProtoResolver;
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.BSFormatter;
Expand All @@ -36,6 +45,8 @@
import bisq.common.ClockWatcher;
import bisq.common.crypto.KeyRing;
import bisq.common.crypto.KeyStorage;
import bisq.common.proto.network.NetworkProtoResolver;
import bisq.common.proto.persistable.PersistenceProtoResolver;
import bisq.common.storage.CorruptedDatabaseFilesHandler;

import org.springframework.mock.env.MockPropertySource;
Expand All @@ -47,6 +58,7 @@
import org.junit.Test;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class GuiceSetupTest {

Expand Down Expand Up @@ -84,6 +96,7 @@ public void testGuiceSetup() {

// core module
// assertSingleton(BisqSetup.class); // this is a can of worms
// assertSingleton(DisputeMsgEvents.class);
assertSingleton(TorSetup.class);
assertSingleton(P2PNetworkSetup.class);
assertSingleton(WalletAppSetup.class);
Expand All @@ -98,6 +111,20 @@ public void testGuiceSetup() {
assertSingleton(AvoidStandbyModeService.class);
assertSingleton(DefaultSeedNodeRepository.class);
assertSingleton(SeedNodeRepository.class);
assertTrue(injector.getInstance(SeedNodeRepository.class) instanceof DefaultSeedNodeRepository);
assertSingleton(CoreNetworkProtoResolver.class);
assertSingleton(NetworkProtoResolver.class);
assertTrue(injector.getInstance(NetworkProtoResolver.class) instanceof CoreNetworkProtoResolver);
assertSingleton(PersistenceProtoResolver.class);
assertSingleton(CorePersistenceProtoResolver.class);
assertTrue(injector.getInstance(PersistenceProtoResolver.class) instanceof CorePersistenceProtoResolver);
assertSingleton(MobileNotificationService.class);
assertSingleton(MobileNotificationValidator.class);
assertSingleton(MobileModel.class);
assertSingleton(MyOfferTakenEvents.class);
assertSingleton(TradeEvents.class);
assertSingleton(PriceAlert.class);
assertSingleton(MarketAlerts.class);
}

private void assertSingleton(Class<?> type) {
Expand Down

0 comments on commit 690d744

Please sign in to comment.