Пример #1
0
  /**
   * Pre-generate enough keys to reach the lookahead size, but only if there are more than the
   * lookaheadThreshold to be generated, so that the Bloom filter does not have to be regenerated
   * that often.
   *
   * <p>The returned mutable list of keys must be inserted into the basic key chain.
   */
  private List<DeterministicKey> maybeLookAhead(
      DeterministicKey parent, int issued, int lookaheadSize, int lookaheadThreshold) {
    checkState(lock.isHeldByCurrentThread());
    final int numChildren = hierarchy.getNumChildren(parent.getPath());
    final int needed = issued + lookaheadSize + lookaheadThreshold - numChildren;

    if (needed <= lookaheadThreshold) return new ArrayList<DeterministicKey>();

    log.info(
        "{} keys needed for {} = {} issued + {} lookahead size + {} lookahead threshold - {} num children",
        needed,
        parent.getPathAsString(),
        issued,
        lookaheadSize,
        lookaheadThreshold,
        numChildren);

    List<DeterministicKey> result = new ArrayList<DeterministicKey>(needed);
    long now = System.currentTimeMillis();
    int nextChild = numChildren;
    for (int i = 0; i < needed; i++) {
      DeterministicKey key = HDKeyDerivation.deriveThisOrNextChildKey(parent, nextChild);
      key = key.getPubOnly();
      hierarchy.putKey(key);
      result.add(key);
      nextChild = key.getChildNumber().num() + 1;
    }
    log.info("Took {} msec", System.currentTimeMillis() - now);
    return result;
  }
Пример #2
0
 static byte[] addChecksum(byte[] input) {
   int inputLength = input.length;
   byte[] checksummed = new byte[inputLength + 4];
   System.arraycopy(input, 0, checksummed, 0, inputLength);
   byte[] checksum = Sha256Hash.hashTwice(input);
   System.arraycopy(checksum, 0, checksummed, inputLength, 4);
   return checksummed;
 }
    /**
     * Find the GhostDriver main file (i.e. {@code "main.js"}).
     *
     * Looks into the Capabilities and the System Properties for
     * {@link PhantomJSDriverService#PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY}.
     *
     * NOTE: If both the Capability and the System Property are set, the Capability takes priority.
     *
     * @param desiredCapabilities Capabilities in which we will look for the path to GhostDriver
     * @param docsLink            The link to the GhostDriver documentation page
     * @param downloadLink        The link to the GhostDriver download page
     * @return The driver executable as a {@link File} object
     * @throws IllegalStateException If the executable not found or cannot be executed
     */
    protected static File findGhostDriver(Capabilities desiredCapabilities, String docsLink, String downloadLink) {
        // Recover path to GhostDriver from the System Properties or the Capabilities
        String ghostdriverpath;
        if (desiredCapabilities != null &&
                desiredCapabilities.getCapability(PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY) != null) {
            ghostdriverpath = (String) desiredCapabilities.getCapability(PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY);
        } else {
            ghostdriverpath = System.getProperty(PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY);
        }

        if (ghostdriverpath != null) {
            // Check few things on the file before returning it
            File ghostdriver = new File(ghostdriverpath);
            checkState(ghostdriver.exists(),
                    "The GhostDriver does not exist: %s",
                    ghostdriver.getAbsolutePath());
            checkState(ghostdriver.isFile(),
                    "The GhostDriver is a directory: %s",
                    ghostdriver.getAbsolutePath());
            checkState(ghostdriver.canRead(),
                    "The GhostDriver is not a readable file: %s",
                    ghostdriver.getAbsolutePath());

            return ghostdriver;
        }

        // This means that no GhostDriver System Property nor Capability was set
        return null;
    }
  /**
   * Looks into the Capabilities, the current $PATH and the System Properties for {@link
   * PhantomJSDriverService#PHANTOMJS_EXECUTABLE_PATH_PROPERTY}.
   *
   * <p>NOTE: If the Capability, the $PATH and the System Property are set, the Capability takes
   * priority over the System Property, that in turn takes priority over the $PATH.
   *
   * @param desiredCapabilities Capabilities in which we will look for the path to PhantomJS
   * @param docsLink The link to the PhantomJS documentation page
   * @param downloadLink The link to the PhantomJS download page
   * @return The driver executable as a {@link File} object
   * @throws IllegalStateException If the executable not found or cannot be executed
   */
  @SuppressWarnings("deprecation")
  protected static File findPhantomJS(
      Capabilities desiredCapabilities, String docsLink, String downloadLink) {
    String phantomjspath = null;
    if (desiredCapabilities != null
        && desiredCapabilities.getCapability(PHANTOMJS_EXECUTABLE_PATH_PROPERTY) != null) {
      phantomjspath =
          (String) desiredCapabilities.getCapability(PHANTOMJS_EXECUTABLE_PATH_PROPERTY);
    } else {
      phantomjspath = CommandLine.find(PHANTOMJS_DEFAULT_EXECUTABLE);
      phantomjspath = System.getProperty(PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjspath);
    }

    checkState(
        phantomjspath != null,
        "The path to the driver executable must be set by the %s capability/system property/PATH variable;"
            + " for more information, see %s. "
            + "The latest version can be downloaded from %s",
        PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
        docsLink,
        downloadLink);

    File phantomjs = new File(phantomjspath);
    checkExecutable(phantomjs);
    return phantomjs;
  }
Пример #5
0
 @Override
 public void close() throws BlockStoreException {
   try {
     buffer.force();
     if (System.getProperty("os.name").toLowerCase().contains("win")) {
       log.info("Windows mmap hack: Forcing buffer cleaning");
       WindowsMMapHack.forceRelease(buffer);
     }
     buffer = null; // Allow it to be GCd and the underlying file mapping to go away.
     randomAccessFile.close();
   } catch (IOException e) {
     throw new BlockStoreException(e);
   }
 }
  /**
   * Find the GhostDriver main file (i.e. <code>"main.js"</code>).
   *
   * <p>Looks into the Capabilities and the System Properties for {@link
   * PhantomJSDriverService#PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY}.
   *
   * <p>NOTE: If both the Capability and the System Property are set, the Capability takes priority.
   *
   * @param desiredCapabilities Capabilities in which we will look for the path to GhostDriver
   * @param docsLink The link to the GhostDriver documentation page
   * @param downloadLink The link to the GhostDriver download page
   * @return The driver executable as a {@link File} object
   * @throws IllegalStateException If the executable not found or cannot be executed
   */
  protected static File findGhostDriver(
      Capabilities desiredCapabilities, String docsLink, String downloadLink) {
    // Recover path to GhostDriver from the System Properties or the Capabilities
    String ghostdriverpath = null;
    if (desiredCapabilities != null
        && (String) desiredCapabilities.getCapability(PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY)
            != null) {
      ghostdriverpath =
          (String) desiredCapabilities.getCapability(PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY);
    } else {
      ghostdriverpath = System.getProperty(PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY, ghostdriverpath);
    }

    if (ghostdriverpath != null) {
      checkState(
          ghostdriverpath != null,
          "The path to the driver executable must be set by the '%s' capability/system property;"
              + " for more information, see %s. "
              + "The latest version can be downloaded from %s",
          PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY,
          docsLink,
          downloadLink);

      // Check few things on the file before returning it
      File ghostdriver = new File(ghostdriverpath);
      checkState(
          ghostdriver.exists(),
          "The GhostDriver does not exist: %s",
          ghostdriver.getAbsolutePath());
      checkState(
          ghostdriver.isFile(),
          "The GhostDriver is a directory: %s",
          ghostdriver.getAbsolutePath());
      checkState(
          ghostdriver.canRead(),
          "The GhostDriver is not a readable file: %s",
          ghostdriver.getAbsolutePath());

      return ghostdriver;
    }

    // This means that no GhostDriver System Property nor Capability was set
    return null;
  }
Пример #7
0
 /**
  * Returns private key bytes, padded with zeros to 33 bytes.
  *
  * @throws java.lang.IllegalStateException if the private key bytes are missing.
  */
 public byte[] getPrivKeyBytes33() {
   byte[] bytes33 = new byte[33];
   byte[] priv = getPrivKeyBytes();
   System.arraycopy(priv, 0, bytes33, 33 - priv.length, priv.length);
   return bytes33;
 }