示例#1
0
  private void addInternalBlock(Block block, BigInteger cummDifficulty, boolean mainChain) {

    List<BlockInfo> blockInfos =
        block.getNumber() >= index.size()
            ? new ArrayList<BlockInfo>()
            : index.get((int) block.getNumber());

    BlockInfo blockInfo = new BlockInfo();
    blockInfo.setCummDifficulty(cummDifficulty);
    blockInfo.setHash(block.getHash());
    blockInfo.setMainChain(
        mainChain); // FIXME:maybe here I should force reset main chain for all uncles on that level

    blockInfos.add(blockInfo);
    index.set((int) block.getNumber(), blockInfos);

    blocks.put(block.getHash(), block);
  }
示例#2
0
  @Override
  public void reBranch(Block forkBlock) {

    Block bestBlock = getBestBlock();

    long maxLevel = Math.max(bestBlock.getNumber(), forkBlock.getNumber());

    // 1. First ensure that you are one the save level
    long currentLevel = maxLevel;
    Block forkLine = forkBlock;
    if (forkBlock.getNumber() > bestBlock.getNumber()) {

      while (currentLevel > bestBlock.getNumber()) {
        List<BlockInfo> blocks = getBlockInfoForLevel(currentLevel);
        BlockInfo blockInfo = getBlockInfoForHash(blocks, forkLine.getHash());
        if (blockInfo != null) {
          blockInfo.setMainChain(true);
          setBlockInfoForLevel(currentLevel, blocks);
        }
        forkLine = getBlockByHash(forkLine.getParentHash());
        --currentLevel;
      }
    }

    Block bestLine = bestBlock;
    if (bestBlock.getNumber() > forkBlock.getNumber()) {

      while (currentLevel > forkBlock.getNumber()) {

        List<BlockInfo> blocks = getBlockInfoForLevel(currentLevel);
        BlockInfo blockInfo = getBlockInfoForHash(blocks, bestLine.getHash());
        if (blockInfo != null) {
          blockInfo.setMainChain(false);
          setBlockInfoForLevel(currentLevel, blocks);
        }
        bestLine = getBlockByHash(bestLine.getParentHash());
        --currentLevel;
      }
    }

    // 2. Loop back on each level until common block
    while (!bestLine.isEqual(forkLine)) {

      List<BlockInfo> levelBlocks = getBlockInfoForLevel(currentLevel);
      BlockInfo bestInfo = getBlockInfoForHash(levelBlocks, bestLine.getHash());
      if (bestInfo != null) {
        bestInfo.setMainChain(false);
        setBlockInfoForLevel(currentLevel, levelBlocks);
      }

      BlockInfo forkInfo = getBlockInfoForHash(levelBlocks, forkLine.getHash());
      if (forkInfo != null) {
        forkInfo.setMainChain(true);
        setBlockInfoForLevel(currentLevel, levelBlocks);
      }

      bestLine = getBlockByHash(bestLine.getParentHash());
      forkLine = getBlockByHash(forkLine.getParentHash());

      --currentLevel;
    }
  }