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(); } } }
@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; }
@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; }
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; }
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; }
private static BlockInfo getBlockInfoForHash(List<BlockInfo> blocks, byte[] hash) { for (BlockInfo blockInfo : blocks) if (areEqual(hash, blockInfo.getHash())) return blockInfo; return null; }