@Test public void testFirstBlockGapedLongestChain() { List<Block> randomChain = TestUtils.getRandomAltChain(new byte[32], 0, 100, 5); Iterator<Block> it = randomChain.iterator(); while (it.hasNext()) { if (it.next().getHeader().getNumber() == 1) it.remove(); } SyncQueueImpl syncQueue = new SyncQueueImpl(randomChain); assert syncQueue.getLongestChain().size() == 1; }
List<HeaderElement> getLongestChain() { Map<ByteArrayWrapper, HeaderElement> lastValidatedGen = headers.get(darkZoneNum); assert lastValidatedGen.size() == 1; HeaderElement lastHeader = lastValidatedGen.values().iterator().next(); Map<byte[], HeaderElement> chainedParents = new ByteArrayMap<>(); chainedParents.put(lastHeader.header.getHash(), lastHeader); for(long curNum = darkZoneNum + 1; ; curNum++) { Map<byte[], HeaderElement> chainedBlocks = new ByteArrayMap<>(); Map<ByteArrayWrapper, HeaderElement> curLevel = headers.get(curNum); if (curLevel == null) break; for (HeaderElement element : curLevel.values()) { if (chainedParents.containsKey(element.header.getHeader().getParentHash())) { chainedBlocks.put(element.header.getHash(), element); } } if (chainedBlocks.isEmpty()) break; chainedParents = chainedBlocks; } List<HeaderElement> ret = new ArrayList<>(); for (HeaderElement el = chainedParents.values().iterator().next(); el != lastHeader.getParent(); el = el.getParent()) { ret.add(0, el); } return ret; }
SyncQueueImpl implements SyncQueueIfc { List<HeaderElement> getLongestChain() { Map<ByteArrayWrapper, HeaderElement> lastValidatedGen = headers.get(darkZoneNum); assert lastValidatedGen.size() == 1; HeaderElement lastHeader = lastValidatedGen.values().iterator().next(); Map<byte[], HeaderElement> chainedParents = new ByteArrayMap<>(); chainedParents.put(lastHeader.header.getHash(), lastHeader); for(long curNum = darkZoneNum + 1; ; curNum++) { Map<byte[], HeaderElement> chainedBlocks = new ByteArrayMap<>(); Map<ByteArrayWrapper, HeaderElement> curLevel = headers.get(curNum); if (curLevel == null) break; for (HeaderElement element : curLevel.values()) { if (chainedParents.containsKey(element.header.getHeader().getParentHash())) { chainedBlocks.put(element.header.getHash(), element); } } if (chainedBlocks.isEmpty()) break; chainedParents = chainedBlocks; } List<HeaderElement> ret = new ArrayList<>(); for (HeaderElement el = chainedParents.values().iterator().next(); el != lastHeader.getParent(); el = el.getParent()) { ret.add(0, el); } return ret; } }
SyncQueueImpl implements SyncQueueIfc { List<HeaderElement> getLongestChain() { Map<ByteArrayWrapper, HeaderElement> lastValidatedGen = headers.get(darkZoneNum); assert lastValidatedGen.size() == 1; HeaderElement lastHeader = lastValidatedGen.values().iterator().next(); Map<byte[], HeaderElement> chainedParents = new ByteArrayMap<>(); chainedParents.put(lastHeader.header.getHash(), lastHeader); for(long curNum = darkZoneNum + 1; ; curNum++) { Map<byte[], HeaderElement> chainedBlocks = new ByteArrayMap<>(); Map<ByteArrayWrapper, HeaderElement> curLevel = headers.get(curNum); if (curLevel == null) break; for (HeaderElement element : curLevel.values()) { if (chainedParents.containsKey(element.header.getHeader().getParentHash())) { chainedBlocks.put(element.header.getHash(), element); } } if (chainedBlocks.isEmpty()) break; chainedParents = chainedBlocks; } List<HeaderElement> ret = new ArrayList<>(); for (HeaderElement el = chainedParents.values().iterator().next(); el != lastHeader.getParent(); el = el.getParent()) { ret.add(0, el); } return ret; } SyncQueueImpl(List<Block> initBlocks);  SyncQueueImpl(Blockchain bc);  SyncQueueImpl(Blockchain bc, Long endBlockNumber); }
SyncQueueImpl implements SyncQueueIfc { List<HeaderElement> getLongestChain() { Map<ByteArrayWrapper, HeaderElement> lastValidatedGen = headers.get(darkZoneNum); assert lastValidatedGen.size() == 1; HeaderElement lastHeader = lastValidatedGen.values().iterator().next(); Map<byte[], HeaderElement> chainedParents = new ByteArrayMap<>(); chainedParents.put(lastHeader.header.getHash(), lastHeader); for(long curNum = darkZoneNum + 1; ; curNum++) { Map<byte[], HeaderElement> chainedBlocks = new ByteArrayMap<>(); Map<ByteArrayWrapper, HeaderElement> curLevel = headers.get(curNum); if (curLevel == null) break; for (HeaderElement element : curLevel.values()) { if (chainedParents.containsKey(element.header.getHeader().getParentHash())) { chainedBlocks.put(element.header.getHash(), element); } } if (chainedBlocks.isEmpty()) break; chainedParents = chainedBlocks; } List<HeaderElement> ret = new ArrayList<>(); for (HeaderElement el = chainedParents.values().iterator().next(); el != lastHeader.getParent(); el = el.getParent()) { ret.add(0, el); } return ret; } SyncQueueImpl(List<Block> initBlocks);  SyncQueueImpl(Blockchain bc);  SyncQueueImpl(Blockchain bc, Long endBlockNumber); @Override synchronized List<HeadersRequest> requestHeaders(int maxSize, int maxRequests, int maxTotalHeaders); @Override synchronized List<BlockHeaderWrapper> addHeaders(Collection<BlockHeaderWrapper> headers); @Override synchronized ValidatedHeaders addHeadersAndValidate(Collection<BlockHeaderWrapper> headers); @Override synchronized int getHeadersCount(); @Override synchronized BlocksRequest requestBlocks(int maxSize); @Override synchronized List<Block> addBlocks(Collection<Block> blocks); synchronized List<Block> pollBlocks(); SyncQueueImpl withParentHeaderValidator(DependentBlockHeaderRule validator); }
SyncQueueImpl implements SyncQueueIfc { List<HeaderElement> getLongestChain() { Map<ByteArrayWrapper, HeaderElement> lastValidatedGen = headers.get(darkZoneNum); assert lastValidatedGen.size() == 1; HeaderElement lastHeader = lastValidatedGen.values().iterator().next(); Map<byte[], HeaderElement> chainedParents = new ByteArrayMap<>(); chainedParents.put(lastHeader.header.getHash(), lastHeader); for(long curNum = darkZoneNum + 1; ; curNum++) { Map<byte[], HeaderElement> chainedBlocks = new ByteArrayMap<>(); Map<ByteArrayWrapper, HeaderElement> curLevel = headers.get(curNum); if (curLevel == null) break; for (HeaderElement element : curLevel.values()) { if (chainedParents.containsKey(element.header.getHeader().getParentHash())) { chainedBlocks.put(element.header.getHash(), element); } } if (chainedBlocks.isEmpty()) break; chainedParents = chainedBlocks; } List<HeaderElement> ret = new ArrayList<>(); for (HeaderElement el = chainedParents.values().iterator().next(); el != lastHeader.getParent(); el = el.getParent()) { ret.add(0, el); } return ret; } SyncQueueImpl(List<Block> initBlocks);  SyncQueueImpl(Blockchain bc);  SyncQueueImpl(Blockchain bc, Long endBlockNumber); @Override synchronized List<HeadersRequest> requestHeaders(int maxSize, int maxRequests, int maxTotalHeaders); @Override synchronized List<BlockHeaderWrapper> addHeaders(Collection<BlockHeaderWrapper> headers); @Override synchronized ValidatedHeaders addHeadersAndValidate(Collection<BlockHeaderWrapper> headers); @Override synchronized int getHeadersCount(); @Override synchronized BlocksRequest requestBlocks(int maxSize); @Override synchronized List<Block> addBlocks(Collection<Block> blocks); synchronized List<Block> pollBlocks(); SyncQueueImpl withParentHeaderValidator(DependentBlockHeaderRule validator);  }