@Override public void flush() { blocks.flush(); index.flush(); if (blocksDS instanceof Flushable) { ((Flushable) blocksDS).flush(); } if (indexDS instanceof Flushable) { ((Flushable) indexDS).flush(); } }
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); }
@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; }
@Override public boolean isBlockExist(byte[] hash) { return blocks.get(hash) != null; }
@Override public Block getBlockByHash(byte[] hash) { return blocks.get(hash); }