@Override public BigInteger getTotalDifficulty() { long maxNumber = getMaxNumber(); List<BlockInfo> blockInfos = index.get((int) maxNumber); for (BlockInfo blockInfo : blockInfos) { if (blockInfo.isMainChain()) { return blockInfo.getCummDifficulty(); } } while (true) { --maxNumber; List<BlockInfo> infos = getBlockInfoForLevel(maxNumber); for (BlockInfo blockInfo : infos) { if (blockInfo.isMainChain()) { return blockInfo.getCummDifficulty(); } } } }
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 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<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; }