<commit_before>package org.multibit.hd.core.services;

import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListeningExecutorService;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.utils.Threading;
import org.multibit.commons.concurrent.SafeExecutors;
import org.multibit.hd.brit.core.seed_phrase.Bip39SeedPhraseGenerator;
import org.multibit.hd.brit.core.seed_phrase.SeedPhraseGenerator;
import org.multibit.hd.brit.core.services.BRITServices;
import org.multibit.hd.brit.core.services.FeeService;
import org.multibit.hd.core.config.BitcoinConfiguration;
import org.multibit.hd.core.config.Configuration;
import org.multibit.hd.core.config.Configurations;
import org.multibit.hd.core.config.Yaml;
import org.multibit.hd.core.dto.WalletId;
import org.multibit.hd.core.dto.WalletMode;
import org.multibit.hd.core.dto.WalletPassword;
import org.multibit.hd.core.dto.WalletSummary;
import org.multibit.hd.core.error_reporting.ExceptionHandler;
import org.multibit.hd.core.events.ShutdownEvent;
import org.multibit.hd.core.exceptions.CoreException;
import org.multibit.hd.core.exceptions.PaymentsLoadException;
import org.multibit.hd.core.logging.LoggingFactory;
import org.multibit.hd.core.managers.InstallationManager;
import org.multibit.hd.core.managers.WalletManager;
import org.multibit.hd.core.utils.BitcoinNetwork;
import org.multibit.hd.hardware.core.HardwareWalletClient;
import org.multibit.hd.hardware.core.HardwareWalletService;
import org.multibit.hd.hardware.core.wallets.HardwareWallets;
import org.multibit.hd.hardware.keepkey.clients.KeepKeyHardwareWalletClient;
import org.multibit.hd.hardware.keepkey.wallets.v1.KeepKeyV1HidHardwareWallet;
import org.multibit.hd.hardware.trezor.clients.TrezorHardwareWalletClient;
import org.multibit.hd.hardware.trezor.wallets.v1.TrezorV1HidHardwareWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.openpgp.PGPException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>Factory to provide the following to application API:</p>
 * <ul>
 * <li>Entry point to configured instances of Core services</li>
 * </ul>
 *
 * @since 0.0.1
 */
public class CoreServices {

  private static final Logger log = LoggerFactory.getLogger(CoreServices.class);

  private static final int TREZOR_WALLET_SERVICE_INDEX = 0;
  private static final int KEEP_KEY_WALLET_SERVICE_INDEX = 1;

  /**
   * Keep track of selected application events (e.g. exchange rate changes, environment alerts etc)
   * Not an optional service
   */
  private static ApplicationEventService applicationEventService;

  /**
   * Keep track of environment events (e.g. debugger, file permissions etc) across all wallets
   * Not an optional service
   */
  private static EnvironmentCheckingService environmentCheckingService;

  /**
   * Keep track of shutdown events and ensure the configuration is persisted
   * Not an optional service
   */
  private static ConfigurationService configurationService;

  /**
   * Allow lightweight processing of external data as part of Payment Protocol support
   * Not an optional service
   */
  private static PaymentProtocolService paymentProtocolService;

  /**
   * Keep track of the Bitcoin network for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<BitcoinNetworkService> bitcoinNetworkService = Optional.absent();

  /**
   * Keep track of the various hardware wallet services for the application
   * Empty if system does not support hardware wallets (or none attached)
   * Device listed in position 0 is the active one for the current session
   */
  private static List<Optional<HardwareWalletService>> hardwareWalletServices = Lists.newArrayList();

  /**
   * Keep track of the current hardware wallet service
   */
  private static Optional<HardwareWalletService> currentHardwareWalletService = Optional.absent();

  /**
   * Keeps track of the contact service for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<PersistentContactService> contactService = Optional.absent();

  /**
   * Keeps track of the wallet service for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<WalletService> walletService = Optional.absent();

  /**
   * Keeps track of the backup service for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<BackupService> backupService = Optional.absent();

  /**
   * Manages CoreService startup and shutdown operations
   */
  private static volatile ListeningExecutorService coreServices = null;
  private static Context context;

  /**
   * The depth of blocks at which we will not track confidence in detail
   */
  private static int EVENT_HORIZON = Integer.MAX_VALUE;

  public static Context getContext() {
    return context;
  }

  /**
   * Utilities have a private constructor
   */
  private CoreServices() {
  }

  /**
   * Start the bare minimum to allow correct operation
   */
  public static void bootstrap() {

    configurationService = new ConfigurationService();

    // Start the configuration service to ensure shutdown events are trapped
    configurationService.start();

    log.debug("Loading configuration...");
    Optional<Configuration> configuration;
    try (InputStream is = new FileInputStream(InstallationManager.getConfigurationFile())) {
      // Load configuration (providing a default if none exists)
      configuration = Yaml.readYaml(is, Configuration.class);
    } catch (YAMLException | IOException e) {
      configuration = Optional.absent();
    }

    if (configuration.isPresent()) {
      log.info("Using current configuration");
      Configurations.currentConfiguration = configuration.get();
    } else {
      log.warn("Using default configuration");
      Configurations.currentConfiguration = Configurations.newDefaultConfiguration();
    }

    // Set up the bitcoinj context
    context = new Context(NetworkParameters.fromID(NetworkParameters.ID_MAINNET), EVENT_HORIZON);
    log.debug("Context identity: {}", System.identityHashCode(context));

    // Ensure any errors can be reported
    ExceptionHandler.registerExceptionHandler();
  }<commit_msg>EVENT_HORIZON default value now set in alice<commit_after>package org.multibit.hd.core.services;

import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListeningExecutorService;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.utils.Threading;
import org.multibit.commons.concurrent.SafeExecutors;
import org.multibit.hd.brit.core.seed_phrase.Bip39SeedPhraseGenerator;
import org.multibit.hd.brit.core.seed_phrase.SeedPhraseGenerator;
import org.multibit.hd.brit.core.services.BRITServices;
import org.multibit.hd.brit.core.services.FeeService;
import org.multibit.hd.core.config.BitcoinConfiguration;
import org.multibit.hd.core.config.Configuration;
import org.multibit.hd.core.config.Configurations;
import org.multibit.hd.core.config.Yaml;
import org.multibit.hd.core.dto.WalletId;
import org.multibit.hd.core.dto.WalletMode;
import org.multibit.hd.core.dto.WalletPassword;
import org.multibit.hd.core.dto.WalletSummary;
import org.multibit.hd.core.error_reporting.ExceptionHandler;
import org.multibit.hd.core.events.ShutdownEvent;
import org.multibit.hd.core.exceptions.CoreException;
import org.multibit.hd.core.exceptions.PaymentsLoadException;
import org.multibit.hd.core.logging.LoggingFactory;
import org.multibit.hd.core.managers.InstallationManager;
import org.multibit.hd.core.managers.WalletManager;
import org.multibit.hd.core.utils.BitcoinNetwork;
import org.multibit.hd.hardware.core.HardwareWalletClient;
import org.multibit.hd.hardware.core.HardwareWalletService;
import org.multibit.hd.hardware.core.wallets.HardwareWallets;
import org.multibit.hd.hardware.keepkey.clients.KeepKeyHardwareWalletClient;
import org.multibit.hd.hardware.keepkey.wallets.v1.KeepKeyV1HidHardwareWallet;
import org.multibit.hd.hardware.trezor.clients.TrezorHardwareWalletClient;
import org.multibit.hd.hardware.trezor.wallets.v1.TrezorV1HidHardwareWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.openpgp.PGPException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>Factory to provide the following to application API:</p>
 * <ul>
 * <li>Entry point to configured instances of Core services</li>
 * </ul>
 *
 * @since 0.0.1
 */
public class CoreServices {

  private static final Logger log = LoggerFactory.getLogger(CoreServices.class);

  private static final int TREZOR_WALLET_SERVICE_INDEX = 0;
  private static final int KEEP_KEY_WALLET_SERVICE_INDEX = 1;

  /**
   * Keep track of selected application events (e.g. exchange rate changes, environment alerts etc)
   * Not an optional service
   */
  private static ApplicationEventService applicationEventService;

  /**
   * Keep track of environment events (e.g. debugger, file permissions etc) across all wallets
   * Not an optional service
   */
  private static EnvironmentCheckingService environmentCheckingService;

  /**
   * Keep track of shutdown events and ensure the configuration is persisted
   * Not an optional service
   */
  private static ConfigurationService configurationService;

  /**
   * Allow lightweight processing of external data as part of Payment Protocol support
   * Not an optional service
   */
  private static PaymentProtocolService paymentProtocolService;

  /**
   * Keep track of the Bitcoin network for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<BitcoinNetworkService> bitcoinNetworkService = Optional.absent();

  /**
   * Keep track of the various hardware wallet services for the application
   * Empty if system does not support hardware wallets (or none attached)
   * Device listed in position 0 is the active one for the current session
   */
  private static List<Optional<HardwareWalletService>> hardwareWalletServices = Lists.newArrayList();

  /**
   * Keep track of the current hardware wallet service
   */
  private static Optional<HardwareWalletService> currentHardwareWalletService = Optional.absent();

  /**
   * Keeps track of the contact service for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<PersistentContactService> contactService = Optional.absent();

  /**
   * Keeps track of the wallet service for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<WalletService> walletService = Optional.absent();

  /**
   * Keeps track of the backup service for the current wallet
   * Optional service until wallet is unlocked
   */
  private static Optional<BackupService> backupService = Optional.absent();

  /**
   * Manages CoreService startup and shutdown operations
   */
  private static volatile ListeningExecutorService coreServices = null;
  private static Context context;

  public static Context getContext() {
    return context;
  }

  /**
   * Utilities have a private constructor
   */
  private CoreServices() {
  }

  /**
   * Start the bare minimum to allow correct operation
   */
  public static void bootstrap() {

    configurationService = new ConfigurationService();

    // Start the configuration service to ensure shutdown events are trapped
    configurationService.start();

    log.debug("Loading configuration...");
    Optional<Configuration> configuration;
    try (InputStream is = new FileInputStream(InstallationManager.getConfigurationFile())) {
      // Load configuration (providing a default if none exists)
      configuration = Yaml.readYaml(is, Configuration.class);
    } catch (YAMLException | IOException e) {
      configuration = Optional.absent();
    }

    if (configuration.isPresent()) {
      log.info("Using current configuration");
      Configurations.currentConfiguration = configuration.get();
    } else {
      log.warn("Using default configuration");
      Configurations.currentConfiguration = Configurations.newDefaultConfiguration();
    }

    // Set up the bitcoinj context
    context = new Context(NetworkParameters.fromID(NetworkParameters.ID_MAINNET));
    log.debug("Context identity: {}", System.identityHashCode(context));

    // Ensure any errors can be reported
    ExceptionHandler.registerExceptionHandler();
  }
{'fraction_non_alphanumeric': 0.08891261247521733, 'fraction_numerical': 0.00213512276955925, 'mean_word_length': 4.557203389830509, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4507732', 'n_tokens_mistral': 3717, 'n_tokens_neox': 3658, 'n_words': 951}