Search in sources :

Example 1 with BisqEnvironment

use of bisq.core.app.BisqEnvironment in project bisq-api by mrosseel.

the class ApiHeadlessModule method configure.

@Override
protected void configure() {
    bind(BisqEnvironment.class).toInstance((BisqEnvironment) environment);
    // bind(CachingViewLoader.class).in(Singleton.class);
    bind(KeyStorage.class).in(Singleton.class);
    bind(KeyRing.class).in(Singleton.class);
    bind(User.class).in(Singleton.class);
    // bind(NotificationCenter.class).in(Singleton.class);
    bind(Clock.class).in(Singleton.class);
    bind(Preferences.class).in(Singleton.class);
    bind(BridgeAddressProvider.class).to(Preferences.class).in(Singleton.class);
    bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);
    File storageDir = new File(environment.getRequiredProperty(Storage.STORAGE_DIR));
    bind(File.class).annotatedWith(named(Storage.STORAGE_DIR)).toInstance(storageDir);
    File keyStorageDir = new File(environment.getRequiredProperty(KeyStorage.KEY_STORAGE_DIR));
    bind(File.class).annotatedWith(named(KeyStorage.KEY_STORAGE_DIR)).toInstance(keyStorageDir);
    bind(BtcWalletService.class).in(Singleton.class);
    bind(NetworkProtoResolver.class).to(CoreNetworkProtoResolver.class).in(Singleton.class);
    bind(PersistenceProtoResolver.class).to(CorePersistenceProtoResolver.class).in(Singleton.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);
    // ordering is used for shut down sequence
    install(tradeModule());
    install(encryptionServiceModule());
    install(arbitratorModule());
    install(offerModule());
    install(p2pModule());
    install(bitcoinModule());
    install(daoModule());
    install(alertModule());
    install(filterModule());
    install(apiModule());
}
Also used : KeyRing(bisq.common.crypto.KeyRing) User(bisq.core.user.User) CoreNetworkProtoResolver(bisq.core.proto.network.CoreNetworkProtoResolver) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) KeyStorage(bisq.common.crypto.KeyStorage) BisqEnvironment(bisq.core.app.BisqEnvironment) Clock(bisq.common.Clock) Preferences(bisq.core.user.Preferences) File(java.io.File) CorePersistenceProtoResolver(bisq.core.proto.persistable.CorePersistenceProtoResolver) DefaultSeedNodeRepository(bisq.core.network.p2p.seed.DefaultSeedNodeRepository)

Example 2 with BisqEnvironment

use of bisq.core.app.BisqEnvironment in project bisq-core by bisq-network.

the class WalletsSetup method initialize.

// /////////////////////////////////////////////////////////////////////////////////////////
// Lifecycle
// /////////////////////////////////////////////////////////////////////////////////////////
public void initialize(@Nullable DeterministicSeed seed, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
    Log.traceCall();
    // 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
    // a future version.
    Threading.USER_THREAD = UserThread.getExecutor();
    Timer timeoutTimer = UserThread.runAfter(() -> exceptionHandler.handleException(new TimeoutException("Wallet did not initialize in " + STARTUP_TIMEOUT + " seconds.")), STARTUP_TIMEOUT);
    backupWallets();
    final Socks5Proxy socks5Proxy = preferences.getUseTorForBitcoinJ() ? socks5ProxyProvider.getSocks5Proxy() : null;
    log.info("Socks5Proxy for bitcoinj: socks5Proxy=" + socks5Proxy);
    walletConfig = new WalletConfig(params, socks5Proxy, walletDir, bisqEnvironment, userAgent, numConnectionForBtc, btcWalletFileName, BSQ_WALLET_FILE_NAME, SPV_CHAIN_FILE_NAME) {

        @Override
        protected void onSetupCompleted() {
            // We are here in the btcj thread Thread[ STARTING,5,main]
            super.onSetupCompleted();
            final PeerGroup peerGroup = walletConfig.peerGroup();
            // We don't want to get our node white list polluted with nodes from AddressMessage calls.
            if (preferences.getBitcoinNodes() != null && !preferences.getBitcoinNodes().isEmpty())
                peerGroup.setAddPeersFromAddressMessage(false);
            peerGroup.addConnectedEventListener((peer, peerCount) -> {
                // We get called here on our user thread
                numPeers.set(peerCount);
                connectedPeers.set(peerGroup.getConnectedPeers());
            });
            peerGroup.addDisconnectedEventListener((peer, peerCount) -> {
                // We get called here on our user thread
                numPeers.set(peerCount);
                connectedPeers.set(peerGroup.getConnectedPeers());
            });
            // Map to user thread
            UserThread.execute(() -> {
                addressEntryList.onWalletReady(walletConfig.getBtcWallet());
                timeoutTimer.stop();
                setupCompletedHandlers.stream().forEach(Runnable::run);
            });
            // onSetupCompleted in walletAppKit is not the called on the last invocations, so we add a bit of delay
            UserThread.runAfter(resultHandler::handleResult, 100, TimeUnit.MILLISECONDS);
        }
    };
    if (params == RegTestParams.get()) {
        walletConfig.setMinBroadcastConnections(1);
        if (regTestHost == RegTestHost.LOCALHOST) {
            walletConfig.setPeerNodesForLocalHost();
        } else if (regTestHost == RegTestHost.REG_TEST_SERVER) {
            walletConfig.setMinBroadcastConnections(1);
            configPeerNodesForRegTestServer();
        } else {
            configPeerNodes(socks5Proxy);
        }
    } else if (bisqEnvironment.isBitcoinLocalhostNodeRunning()) {
        walletConfig.setMinBroadcastConnections(1);
        walletConfig.setPeerNodesForLocalHost();
    } else {
        configPeerNodes(socks5Proxy);
    }
    walletConfig.setDownloadListener(downloadListener).setBlockingStartup(false);
    // If seed is non-null it means we are restoring from backup.
    walletConfig.setSeed(seed);
    walletConfig.addListener(new Service.Listener() {

        @Override
        public void failed(@NotNull Service.State from, @NotNull Throwable failure) {
            walletConfig = null;
            log.error("Service failure from state: {}; failure={}", from, failure);
            timeoutTimer.stop();
            UserThread.execute(() -> exceptionHandler.handleException(failure));
        }
    }, Threading.USER_THREAD);
    walletConfig.startAsync();
}
Also used : RegTestParams(org.bitcoinj.params.RegTestParams) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) RegTestHost(bisq.core.btc.RegTestHost) StringUtils(org.apache.commons.lang3.StringUtils) InetAddress(java.net.InetAddress) NetworkParameters(org.bitcoinj.core.NetworkParameters) AddressEntryList(bisq.core.btc.AddressEntryList) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) Context(org.bitcoinj.core.Context) PeerGroup(org.bitcoinj.core.PeerGroup) BlockChain(org.bitcoinj.core.BlockChain) ReadOnlyDoubleProperty(javafx.beans.property.ReadOnlyDoubleProperty) Set(java.util.Set) DownloadProgressTracker(org.bitcoinj.core.listeners.DownloadProgressTracker) Collectors(java.util.stream.Collectors) ExceptionHandler(bisq.common.handlers.ExceptionHandler) FileUtil(bisq.common.storage.FileUtil) BooleanProperty(javafx.beans.property.BooleanProperty) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) DeterministicSeed(org.bitcoinj.wallet.DeterministicSeed) AddressEntry(bisq.core.btc.AddressEntry) Preferences(bisq.core.user.Preferences) UserThread(bisq.common.UserThread) ReadOnlyIntegerProperty(javafx.beans.property.ReadOnlyIntegerProperty) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) Address(org.bitcoinj.core.Address) NotNull(org.jetbrains.annotations.NotNull) ReadOnlyObjectProperty(javafx.beans.property.ReadOnlyObjectProperty) Wallet(org.bitcoinj.wallet.Wallet) Timer(bisq.common.Timer) DoubleProperty(javafx.beans.property.DoubleProperty) Peer(org.bitcoinj.core.Peer) IntegerProperty(javafx.beans.property.IntegerProperty) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Socks5MultiDiscovery(bisq.network.Socks5MultiDiscovery) PeerAddress(org.bitcoinj.core.PeerAddress) Named(javax.inject.Named) Nullable(javax.annotation.Nullable) BitcoinNodes(bisq.core.btc.BitcoinNodes) ObjectProperty(javafx.beans.property.ObjectProperty) BtcNode(bisq.core.btc.BitcoinNodes.BtcNode) Socks5ProxyProvider(bisq.network.Socks5ProxyProvider) ResultHandler(bisq.common.handlers.ResultHandler) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Log(bisq.common.app.Log) IOException(java.io.IOException) BisqEnvironment(bisq.core.app.BisqEnvironment) BtcOptionKeys(bisq.core.btc.BtcOptionKeys) UnknownHostException(java.net.UnknownHostException) File(java.io.File) Threading(org.bitcoinj.utils.Threading) Service(com.google.common.util.concurrent.Service) TimeUnit(java.util.concurrent.TimeUnit) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) Paths(java.nio.file.Paths) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Timer(bisq.common.Timer) Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) PeerGroup(org.bitcoinj.core.PeerGroup) Service(com.google.common.util.concurrent.Service) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with BisqEnvironment

use of bisq.core.app.BisqEnvironment in project bisq-desktop by bisq-network.

the class BisqAppMain method main.

public static void main(String[] args) throws Exception {
    // 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;
    }
    BisqEnvironment bisqEnvironment = getBisqEnvironment(options);
    // need to call that before bisqAppMain().execute(args)
    initAppDir(bisqEnvironment.getProperty(AppOptionKeys.APP_DATA_DIR_KEY));
    // For some reason the JavaFX launch process results in us losing the thread context class loader: reset it.
    // In order to work around a bug in JavaFX 8u25 and below, you must include the following code as the first line of your realMain method:
    Thread.currentThread().setContextClassLoader(BisqAppMain.class.getClassLoader());
    new BisqAppMain().execute(args);
}
Also used : BisqEnvironment(bisq.core.app.BisqEnvironment) OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Example 4 with BisqEnvironment

use of bisq.core.app.BisqEnvironment in project bisq-desktop by bisq-network.

the class BisqAppModule method configure.

@Override
protected void configure() {
    bind(BisqEnvironment.class).toInstance((BisqEnvironment) environment);
    bind(CachingViewLoader.class).in(Singleton.class);
    bind(KeyStorage.class).in(Singleton.class);
    bind(KeyRing.class).in(Singleton.class);
    bind(User.class).in(Singleton.class);
    bind(NotificationCenter.class).in(Singleton.class);
    bind(Clock.class).in(Singleton.class);
    bind(Preferences.class).in(Singleton.class);
    bind(BridgeAddressProvider.class).to(Preferences.class).in(Singleton.class);
    bind(SeedNodeAddressLookup.class).in(Singleton.class);
    bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);
    File storageDir = new File(environment.getRequiredProperty(Storage.STORAGE_DIR));
    bind(File.class).annotatedWith(named(Storage.STORAGE_DIR)).toInstance(storageDir);
    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);
    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);
    Boolean useDevMode = environment.getProperty(AppOptionKeys.USE_DEV_MODE, Boolean.class, false);
    bind(boolean.class).annotatedWith(Names.named(AppOptionKeys.USE_DEV_MODE)).toInstance(useDevMode);
    // ordering is used for shut down sequence
    install(tradeModule());
    install(encryptionServiceModule());
    install(arbitratorModule());
    install(offerModule());
    install(p2pModule());
    install(bitcoinModule());
    install(daoModule());
    install(guiModule());
    install(alertModule());
    install(filterModule());
}
Also used : User(bisq.core.user.User) CoreNetworkProtoResolver(bisq.core.proto.network.CoreNetworkProtoResolver) KeyStorage(bisq.common.crypto.KeyStorage) NotificationCenter(bisq.desktop.main.overlays.notifications.NotificationCenter) Clock(bisq.common.Clock) CorePersistenceProtoResolver(bisq.core.proto.persistable.CorePersistenceProtoResolver) CachingViewLoader(bisq.desktop.common.view.CachingViewLoader) DefaultSeedNodeRepository(bisq.core.network.p2p.seed.DefaultSeedNodeRepository) KeyRing(bisq.common.crypto.KeyRing) BisqEnvironment(bisq.core.app.BisqEnvironment) Preferences(bisq.core.user.Preferences) File(java.io.File) SeedNodeAddressLookup(bisq.core.network.p2p.seed.SeedNodeAddressLookup)

Example 5 with BisqEnvironment

use of bisq.core.app.BisqEnvironment in project bisq-core by bisq-network.

the class BisqEnvironmentTests method testPropertySourcePrecedence.

@Test
public void testPropertySourcePrecedence() {
    PropertySource commandlineProps = new MockPropertySource(BISQ_COMMANDLINE_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.commandline");
    PropertySource filesystemProps = new MockPropertySource(BISQ_APP_DIR_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.bisqEnvironment").withProperty("key.y", "y.bisqEnvironment");
    ConfigurableEnvironment bisqEnvironment = new BisqEnvironment(commandlineProps) {

        @Override
        PropertySource<?> getAppDirProperties() {
            return filesystemProps;
        }
    };
    MutablePropertySources propertySources = bisqEnvironment.getPropertySources();
    assertThat(propertySources.precedenceOf(named(BISQ_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(propertySources.precedenceOf(named(BISQ_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(4));
    // we removed support for the rest
    /*  assertThat(propertySources.precedenceOf(named(BISQ_APP_DIR_PROPERTY_SOURCE_NAME)), equalTo(3));
        assertThat(propertySources.precedenceOf(named(BISQ_HOME_DIR_PROPERTY_SOURCE_NAME)), equalTo(4));
        assertThat(propertySources.precedenceOf(named(BISQ_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(5));*/
    assertThat(propertySources.size(), equalTo(5));
    // commandline value wins due to precedence
    assertThat(bisqEnvironment.getProperty("key.x"), equalTo("x.commandline"));
// TODO check why it fails
// assertThat(bisqEnvironment.getProperty("key.y"), equalTo("y.bisqEnvironment")); // bisqEnvironment value wins because it's the only one available
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) BisqEnvironment(bisq.core.app.BisqEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) PropertySource(org.springframework.core.env.PropertySource) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Aggregations

BisqEnvironment (bisq.core.app.BisqEnvironment)5 Preferences (bisq.core.user.Preferences)3 File (java.io.File)3 Clock (bisq.common.Clock)2 KeyRing (bisq.common.crypto.KeyRing)2 KeyStorage (bisq.common.crypto.KeyStorage)2 DefaultSeedNodeRepository (bisq.core.network.p2p.seed.DefaultSeedNodeRepository)2 CoreNetworkProtoResolver (bisq.core.proto.network.CoreNetworkProtoResolver)2 CorePersistenceProtoResolver (bisq.core.proto.persistable.CorePersistenceProtoResolver)2 User (bisq.core.user.User)2 Timer (bisq.common.Timer)1 UserThread (bisq.common.UserThread)1 Log (bisq.common.app.Log)1 ExceptionHandler (bisq.common.handlers.ExceptionHandler)1 ResultHandler (bisq.common.handlers.ResultHandler)1 FileUtil (bisq.common.storage.FileUtil)1 AddressEntry (bisq.core.btc.AddressEntry)1 AddressEntryList (bisq.core.btc.AddressEntryList)1 BitcoinNodes (bisq.core.btc.BitcoinNodes)1 BtcNode (bisq.core.btc.BitcoinNodes.BtcNode)1