예제 #1
0
  private void connectBlock(
      StoredBlock newStoredBlock, StoredBlock storedPrev, List<Transaction> newTransactions)
      throws BlockStoreException, VerificationException {
    if (storedPrev.equals(chainHead)) {
      // This block connects to the best known block, it is a normal continuation of the system.
      setChainHead(newStoredBlock);
      log.trace("Chain is now {} blocks high", chainHead.getHeight());
      if (newTransactions != null)
        sendTransactionsToWallet(newStoredBlock, NewBlockType.BEST_CHAIN, newTransactions);
    } 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.
      boolean haveNewBestChain = newStoredBlock.moreWorkThan(chainHead);
      if (haveNewBestChain) {
        log.info("Block is causing a re-organize");
      } else {
        StoredBlock splitPoint = findSplit(newStoredBlock, chainHead);
        String splitPointHash = splitPoint != null ? splitPoint.getHeader().getHashAsString() : "?";
        log.info(
            "Block forks the chain at {}, but it did not cause a reorganize:\n{}",
            splitPointHash,
            newStoredBlock);
      }

      // We may not have any transactions if we received only a header. That never happens today but
      // will in
      // future when getheaders is used as an optimization.
      if (newTransactions != null) {
        sendTransactionsToWallet(newStoredBlock, NewBlockType.SIDE_CHAIN, newTransactions);
      }

      if (haveNewBestChain) handleNewBestChain(newStoredBlock);
    }
  }