@Override
 public boolean equals(Object o) {
   if (this == o) return true;
   if (o == null || getClass() != o.getClass()) return false;
   NetworkParameters other = (NetworkParameters) o;
   return getId().equals(other.getId());
 }
 private void createNewStore(NetworkParameters params) throws BlockStoreException {
   try {
     // Set up the genesis block. When we start out fresh, it is by
     // definition the top of the chain.
     StoredBlock storedGenesisHeader =
         new StoredBlock(
             params.getGenesisBlock().cloneAsHeader(), params.getGenesisBlock().getWork(), 0);
     // The coinbase in the genesis block is not spendable. This is because of how the reference
     // client inits
     // its database - the genesis transaction isn't actually in the db so its spent flags can
     // never be updated.
     List<Transaction> genesisTransactions = Lists.newLinkedList();
     StoredUndoableBlock storedGenesis =
         new StoredUndoableBlock(params.getGenesisBlock().getHash(), genesisTransactions);
     put(storedGenesisHeader, storedGenesis);
     setChainHead(storedGenesisHeader);
     setVerifiedChainHead(storedGenesisHeader);
   } catch (VerificationException e) {
     throw new RuntimeException(e); // Cannot happen.
   }
 }
 /**
  * Parses a wallet from the given stream, using the provided Wallet instance to load data into.
  * This is primarily used when you want to register extensions. Data in the proto will be added
  * into the wallet where applicable and overwrite where not.
  *
  * <p>A wallet can be unreadable for various reasons, such as inability to open the file, corrupt
  * data, internally inconsistent data, a wallet extension marked as mandatory that cannot be
  * handled and so on. You should always handle {@link UnreadableWalletException} and communicate
  * failure to the user in an appropriate manner.
  *
  * @throws UnreadableWalletException thrown in various error conditions (see description).
  */
 public Wallet readWallet(InputStream input) throws UnreadableWalletException {
   try {
     Protos.Wallet walletProto = parseToProto(input);
     final String paramsID = walletProto.getNetworkIdentifier();
     NetworkParameters params = NetworkParameters.fromID(paramsID);
     if (params == null)
       throw new UnreadableWalletException("Unknown network parameters ID " + paramsID);
     return readWallet(params, null, walletProto);
   } catch (IOException e) {
     throw new UnreadableWalletException("Could not parse input stream to protobuf", e);
   }
 }
 /**
  * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning
  * of the stream.
  *
  * @param is input stream to test
  * @return true if input stream is a wallet
  */
 public static boolean isWallet(InputStream is) {
   try {
     final CodedInputStream cis = CodedInputStream.newInstance(is);
     final int tag = cis.readTag();
     final int field = WireFormat.getTagFieldNumber(tag);
     if (field != 1) // network_identifier
     return false;
     final String network = cis.readString();
     return NetworkParameters.fromID(network) != null;
   } catch (IOException x) {
     return false;
   }
 }
 private void initNewStore(NetworkParameters params) throws Exception {
   byte[] header;
   header = HEADER_MAGIC.getBytes("US-ASCII");
   buffer.put(header);
   // Insert the genesis block.
   lock.lock();
   try {
     setRingCursor(buffer, FILE_PROLOGUE_BYTES);
   } finally {
     lock.unlock();
   }
   Block genesis = params.getGenesisBlock().cloneAsHeader();
   StoredBlock storedGenesis = new StoredBlock(genesis, genesis.getWork(), 0);
   put(storedGenesis);
   setChainHead(storedGenesis);
 }
  // expensiveChecks enables checks that require looking at blocks further back in the chain
  // than the previous one when connecting (eg median timestamp check)
  // It could be exposed, but for now we just set it to shouldVerifyTransactions()
  private void connectBlock(
      final Block block,
      StoredBlock storedPrev,
      boolean expensiveChecks,
      @Nullable final List<Sha256Hash> filteredTxHashList,
      @Nullable final Map<Sha256Hash, Transaction> filteredTxn)
      throws BlockStoreException, VerificationException, PrunedException {
    checkState(lock.isHeldByCurrentThread());
    boolean filtered = filteredTxHashList != null && filteredTxn != null;
    // Check that we aren't connecting a block that fails a checkpoint check
    if (!params.passesCheckpoint(storedPrev.getHeight() + 1, block.getHash()))
      throw new VerificationException(
          "Block failed checkpoint lockin at " + (storedPrev.getHeight() + 1));
    if (shouldVerifyTransactions()) {
      checkNotNull(block.transactions);
      for (Transaction tx : block.transactions)
        if (!tx.isFinal(storedPrev.getHeight() + 1, block.getTimeSeconds()))
          throw new VerificationException("Block contains non-final transaction");
    }

    StoredBlock head = getChainHead();
    if (storedPrev.equals(head)) {
      if (filtered && filteredTxn.size() > 0) {
        log.debug(
            "Block {} connects to top of best chain with {} transaction(s) of which we were sent {}",
            block.getHashAsString(),
            filteredTxHashList.size(),
            filteredTxn.size());
        for (Sha256Hash hash : filteredTxHashList) log.debug("  matched tx {}", hash);
      }
      if (expensiveChecks
          && block.getTimeSeconds() <= getMedianTimestampOfRecentBlocks(head, blockStore))
        throw new VerificationException("Block's timestamp is too early");

      // This block connects to the best known block, it is a normal continuation of the system.
      TransactionOutputChanges txOutChanges = null;
      if (shouldVerifyTransactions())
        txOutChanges = connectTransactions(storedPrev.getHeight() + 1, block);
      StoredBlock newStoredBlock =
          addToBlockStore(
              storedPrev, block.transactions == null ? block : block.cloneAsHeader(), txOutChanges);
      setChainHead(newStoredBlock);
      log.debug("Chain is now {} blocks high, running listeners", newStoredBlock.getHeight());
      informListenersForNewBlock(
          block, NewBlockType.BEST_CHAIN, filteredTxHashList, filteredTxn, newStoredBlock);
    } else {
      // This block connects to somewhere other than the top of the best known chain. We treat these
      // differently.
      //
      // Note that we send the transactions to the wallet FIRST, even if we're about to re-organize
      // this block
      // to become the new best chain head. This simplifies handling of the re-org in the Wallet
      // class.
      StoredBlock newBlock = storedPrev.build(block);
      boolean haveNewBestChain = newBlock.moreWorkThan(head);
      if (haveNewBestChain) {
        log.info("Block is causing a re-organize");
      } else {
        StoredBlock splitPoint = findSplit(newBlock, head, blockStore);
        if (splitPoint != null && splitPoint.equals(newBlock)) {
          // newStoredBlock is a part of the same chain, there's no fork. This happens when we
          // receive a block
          // that we already saw and linked into the chain previously, which isn't the chain head.
          // Re-processing it is confusing for the wallet so just skip.
          log.warn(
              "Saw duplicated block in main chain at height {}: {}",
              newBlock.getHeight(),
              newBlock.getHeader().getHash());
          return;
        }
        if (splitPoint == null) {
          // This should absolutely never happen
          // (lets not write the full block to disk to keep any bugs which allow this to happen
          //  from writing unreasonable amounts of data to disk)
          throw new VerificationException("Block forks the chain but splitPoint is null");
        } else {
          // We aren't actually spending any transactions (yet) because we are on a fork
          addToBlockStore(storedPrev, block);
          int splitPointHeight = splitPoint.getHeight();
          String splitPointHash = splitPoint.getHeader().getHashAsString();
          log.info(
              "Block forks the chain at height {}/block {}, but it did not cause a reorganize:\n{}",
              splitPointHeight,
              splitPointHash,
              newBlock.getHeader().getHashAsString());
        }
      }

      // We may not have any transactions if we received only a header, which can happen during fast
      // catchup.
      // If we do, send them to the wallet but state that they are on a side chain so it knows not
      // to try and
      // spend them until they become activated.
      if (block.transactions != null || filtered) {
        informListenersForNewBlock(
            block, NewBlockType.SIDE_CHAIN, filteredTxHashList, filteredTxn, newBlock);
      }

      if (haveNewBestChain) handleNewBestChain(storedPrev, newBlock, block, expensiveChecks);
    }
  }
  // filteredTxHashList contains all transactions, filteredTxn just a subset
  private boolean add(
      Block block,
      boolean tryConnecting,
      @Nullable List<Sha256Hash> filteredTxHashList,
      @Nullable Map<Sha256Hash, Transaction> filteredTxn)
      throws BlockStoreException, VerificationException, PrunedException {
    // TODO: Use read/write locks to ensure that during chain download properties are still low
    // latency.
    lock.lock();
    try {
      // Quick check for duplicates to avoid an expensive check further down (in findSplit). This
      // can happen a lot
      // when connecting orphan transactions due to the dumb brute force algorithm we use.
      if (block.equals(getChainHead().getHeader())) {
        return true;
      }
      if (tryConnecting && orphanBlocks.containsKey(block.getHash())) {
        return false;
      }

      // If we want to verify transactions (ie we are running with full blocks), verify that block
      // has transactions
      if (shouldVerifyTransactions() && block.transactions == null)
        throw new VerificationException("Got a block header while running in full-block mode");

      // Check for already-seen block, but only for full pruned mode, where the DB is
      // more likely able to handle these queries quickly.
      if (shouldVerifyTransactions() && blockStore.get(block.getHash()) != null) {
        return true;
      }

      // Does this block contain any transactions we might care about? Check this up front before
      // verifying the
      // blocks validity so we can skip the merkle root verification if the contents aren't
      // interesting. This saves
      // a lot of time for big blocks.
      boolean contentsImportant = shouldVerifyTransactions();
      if (block.transactions != null) {
        contentsImportant = contentsImportant || containsRelevantTransactions(block);
      }

      // Prove the block is internally valid: hash is lower than target, etc. This only checks the
      // block contents
      // if there is a tx sending or receiving coins using an address in one of our wallets. And
      // those transactions
      // are only lightly verified: presence in a valid connecting block is taken as proof of
      // validity. See the
      // article here for more details: http://code.google.com/p/bitcoinj/wiki/SecurityModel
      try {
        block.verifyHeader();
        if (contentsImportant) block.verifyTransactions();

      } catch (VerificationException e) {
        log.error("Failed to verify block: ", e);
        log.error(block.getHashAsString());
        throw e;
      }

      // Try linking it to a place in the currently known blocks.
      StoredBlock storedPrev = getStoredBlockInCurrentScope(block.getPrevBlockHash());
      if (storedPrev == null) {
        // We can't find the previous block. Probably we are still in the process of downloading the
        // chain and a
        // block was solved whilst we were doing it. We put it to one side and try to connect it
        // later when we
        // have more blocks.
        checkState(tryConnecting, "bug in tryConnectingOrphans");
        log.warn(
            "Block does not connect: {} prev {}",
            block.getHashAsString(),
            block.getPrevBlockHash());
        orphanBlocks.put(block.getHash(), new OrphanBlock(block, filteredTxHashList, filteredTxn));
        return false;
      } else {
        checkState(lock.isHeldByCurrentThread());
        // It connects to somewhere on the chain. Not necessarily the top of the best known chain.
        params.checkDifficultyTransitions(storedPrev, block, blockStore);
        connectBlock(
            block, storedPrev, shouldVerifyTransactions(), filteredTxHashList, filteredTxn);
      }
      if (tryConnecting) tryConnectingOrphans();
      return true;
    } finally {
      lock.unlock();
    }
  }
  /**
   * Loads wallet data from the given protocol buffer and inserts it into the given Wallet object.
   * This is primarily useful when you wish to pre-register extension objects. Note that if loading
   * fails the provided Wallet object may be in an indeterminate state and should be thrown away.
   *
   * <p>A wallet can be unreadable for various reasons, such as inability to open the file, corrupt
   * data, internally inconsistent data, a wallet extension marked as mandatory that cannot be
   * handled and so on. You should always handle {@link UnreadableWalletException} and communicate
   * failure to the user in an appropriate manner.
   *
   * @throws UnreadableWalletException thrown in various error conditions (see description).
   */
  public Wallet readWallet(
      NetworkParameters params, @Nullable WalletExtension[] extensions, Protos.Wallet walletProto)
      throws UnreadableWalletException {
    if (walletProto.getVersion() > 1) throw new UnreadableWalletException.FutureVersion();
    if (!walletProto.getNetworkIdentifier().equals(params.getId()))
      throw new UnreadableWalletException.WrongNetwork();

    int sigsRequiredToSpend = walletProto.getSigsRequiredToSpend();

    // Read the scrypt parameters that specify how encryption and decryption is performed.
    KeyChainGroup chain;
    if (walletProto.hasEncryptionParameters()) {
      Protos.ScryptParameters encryptionParameters = walletProto.getEncryptionParameters();
      final KeyCrypterScrypt keyCrypter = new KeyCrypterScrypt(encryptionParameters);
      chain =
          KeyChainGroup.fromProtobufEncrypted(
              params, walletProto.getKeyList(), sigsRequiredToSpend, keyCrypter);
    } else {
      chain =
          KeyChainGroup.fromProtobufUnencrypted(
              params, walletProto.getKeyList(), sigsRequiredToSpend);
    }
    Wallet wallet = factory.create(params, chain);

    List<Script> scripts = Lists.newArrayList();
    for (Protos.Script protoScript : walletProto.getWatchedScriptList()) {
      try {
        Script script =
            new Script(
                protoScript.getProgram().toByteArray(), protoScript.getCreationTimestamp() / 1000);
        scripts.add(script);
      } catch (ScriptException e) {
        throw new UnreadableWalletException("Unparseable script in wallet");
      }
    }

    wallet.addWatchedScripts(scripts);

    if (walletProto.hasDescription()) {
      wallet.setDescription(walletProto.getDescription());
    }

    // Read all transactions and insert into the txMap.
    for (Protos.Transaction txProto : walletProto.getTransactionList()) {
      readTransaction(txProto, wallet.getParams());
    }

    // Update transaction outputs to point to inputs that spend them
    for (Protos.Transaction txProto : walletProto.getTransactionList()) {
      WalletTransaction wtx = connectTransactionOutputs(txProto);
      wallet.addWalletTransaction(wtx);
    }

    // Update the lastBlockSeenHash.
    if (!walletProto.hasLastSeenBlockHash()) {
      wallet.setLastBlockSeenHash(null);
    } else {
      wallet.setLastBlockSeenHash(byteStringToHash(walletProto.getLastSeenBlockHash()));
    }
    if (!walletProto.hasLastSeenBlockHeight()) {
      wallet.setLastBlockSeenHeight(-1);
    } else {
      wallet.setLastBlockSeenHeight(walletProto.getLastSeenBlockHeight());
    }
    // Will default to zero if not present.
    wallet.setLastBlockSeenTimeSecs(walletProto.getLastSeenBlockTimeSecs());

    if (walletProto.hasKeyRotationTime()) {
      wallet.setKeyRotationTime(new Date(walletProto.getKeyRotationTime() * 1000));
    }

    loadExtensions(wallet, extensions != null ? extensions : new WalletExtension[0], walletProto);

    for (Protos.Tag tag : walletProto.getTagsList()) {
      wallet.setTag(tag.getTag(), tag.getData());
    }

    for (Protos.TransactionSigner signerProto : walletProto.getTransactionSignersList()) {
      try {
        Class signerClass = Class.forName(signerProto.getClassName());
        TransactionSigner signer = (TransactionSigner) signerClass.newInstance();
        signer.deserialize(signerProto.getData().toByteArray());
        wallet.addTransactionSigner(signer);
      } catch (Exception e) {
        throw new UnreadableWalletException(
            "Unable to deserialize TransactionSigner instance: " + signerProto.getClassName(), e);
      }
    }

    if (walletProto.hasVersion()) {
      wallet.setVersion(walletProto.getVersion());
    }

    // Make sure the object can be re-used to read another wallet without corruption.
    txMap.clear();

    return wallet;
  }