Ejemplo n.º 1
0
  public void printChain() {

    Long number = getMaxNumber();

    for (int i = 0; i < number; ++i) {
      List<BlockInfo> levelInfos = index.get(i);

      if (levelInfos != null) {
        System.out.print(i);
        for (BlockInfo blockInfo : levelInfos) {
          if (blockInfo.isMainChain())
            System.out.print(" [" + shortHash(blockInfo.getHash()) + "] ");
          else System.out.print(" " + shortHash(blockInfo.getHash()) + " ");
        }
        System.out.println();
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  public BigInteger getTotalDifficultyForHash(byte[] hash) {
    Block block = this.getBlockByHash(hash);
    if (block == null) return ZERO;

    Long level = block.getNumber();
    List<BlockInfo> blockInfos = index.get(level.intValue());
    for (BlockInfo blockInfo : blockInfos)
      if (areEqual(blockInfo.getHash(), hash)) {
        return blockInfo.cummDifficulty;
      }

    return ZERO;
  }
Ejemplo n.º 3
0
  @Override
  public Block getChainBlockByNumber(long number) {
    if (number >= index.size()) {
      return null;
    }

    List<BlockInfo> blockInfos = index.get((int) number);

    for (BlockInfo blockInfo : blockInfos) {

      if (blockInfo.isMainChain()) {

        byte[] hash = blockInfo.getHash();
        return blocks.get(hash);
      }
    }

    return null;
  }
Ejemplo n.º 4
0
  public List<Block> getBlocksByNumber(long number) {

    List<Block> result = new ArrayList<>();

    if (number >= index.size()) {
      return result;
    }

    List<BlockInfo> blockInfos = index.get((int) number);

    for (BlockInfo blockInfo : blockInfos) {

      byte[] hash = blockInfo.getHash();
      Block block = blocks.get(hash);

      result.add(block);
    }
    return result;
  }
Ejemplo n.º 5
0
  public List<byte[]> getListHashesStartWith(long number, long maxBlocks) {

    List<byte[]> result = new ArrayList<>();

    int i;
    for (i = 0; i < maxBlocks; ++i) {
      List<BlockInfo> blockInfos = index.get((int) number);
      if (blockInfos == null) break;

      for (BlockInfo blockInfo : blockInfos)
        if (blockInfo.isMainChain()) {
          result.add(blockInfo.getHash());
          break;
        }

      ++number;
    }
    maxBlocks -= i;

    return result;
  }
Ejemplo n.º 6
0
  private static BlockInfo getBlockInfoForHash(List<BlockInfo> blocks, byte[] hash) {

    for (BlockInfo blockInfo : blocks) if (areEqual(hash, blockInfo.getHash())) return blockInfo;

    return null;
  }