@Test public void testValidateChain() { List<Block> randomChain = TestUtils.getRandomChain(new byte[32], 0, 100); SyncQueueImpl queue = new SyncQueueImpl(randomChain); byte[] nodeId = randomPeerId(); List<Block> chain = TestUtils.getRandomChain(randomChain.get(randomChain.size() - 1).getHash(), 100, SyncQueueImpl.MAX_CHAIN_LEN - 100 - 1); queue.addHeaders(createHeadersFromBlocks(chain, nodeId)); List<SyncQueueImpl.HeaderElement> longestChain = queue.getLongestChain(); assertEquals(SyncQueueIfc.ValidatedHeaders.Empty, queue.validateChain(longestChain)); chain = TestUtils.getRandomChain(chain.get(chain.size() - 1).getHash(), SyncQueueImpl.MAX_CHAIN_LEN - 1, SyncQueueImpl.MAX_CHAIN_LEN); queue.addHeaders(createHeadersFromBlocks(chain, nodeId)); chain = TestUtils.getRandomChain(chain.get(chain.size() - 1).getHash(), 2 * SyncQueueImpl.MAX_CHAIN_LEN - 1, SyncQueueImpl.MAX_CHAIN_LEN); queue.withParentHeaderValidator(RedRule); SyncQueueIfc.ValidatedHeaders ret = queue.addHeadersAndValidate(createHeadersFromBlocks(chain, nodeId)); assertFalse(ret.isValid()); assertArrayEquals(nodeId, ret.getNodeId()); queue.withParentHeaderValidator(GreenRule); ret = queue.addHeadersAndValidate(createHeadersFromBlocks(chain, nodeId)); assertEquals(SyncQueueIfc.ValidatedHeaders.Empty, ret); }
ValidatedHeaders validateChain(List<HeaderElement> chain) { if (parentHeaderValidator == null) return ValidatedHeaders.Empty; for (int i = 1; i < chain.size(); i++) { BlockHeaderWrapper parent = chain.get(i - 1).header; BlockHeaderWrapper header = chain.get(i).header; if (!parentHeaderValidator.validate(header.getHeader(), parent.getHeader())) { return new ValidatedHeaders(Collections.singletonList(header), false, parentHeaderValidator.getErrors().isEmpty() ? "" : parentHeaderValidator.getErrors().get(0)); } } return ValidatedHeaders.Empty; }
SyncQueueImpl implements SyncQueueIfc { ValidatedHeaders validateChain(List<HeaderElement> chain) { if (parentHeaderValidator == null) return ValidatedHeaders.Empty; for (int i = 1; i < chain.size(); i++) { BlockHeaderWrapper parent = chain.get(i - 1).header; BlockHeaderWrapper header = chain.get(i).header; if (!parentHeaderValidator.validate(header.getHeader(), parent.getHeader())) { return new ValidatedHeaders(Collections.singletonList(header), false, parentHeaderValidator.getErrors().isEmpty() ? "" : parentHeaderValidator.getErrors().get(0)); } } return ValidatedHeaders.Empty; } }
SyncQueueImpl implements SyncQueueIfc { ValidatedHeaders validateChain(List<HeaderElement> chain) { if (parentHeaderValidator == null) return ValidatedHeaders.Empty; for (int i = 1; i < chain.size(); i++) { BlockHeaderWrapper parent = chain.get(i - 1).header; BlockHeaderWrapper header = chain.get(i).header; if (!parentHeaderValidator.validate(header.getHeader(), parent.getHeader())) { return new ValidatedHeaders(Collections.singletonList(header), false, parentHeaderValidator.getErrors().isEmpty() ? "" : parentHeaderValidator.getErrors().get(0)); } } return ValidatedHeaders.Empty; } SyncQueueImpl(List<Block> initBlocks);  SyncQueueImpl(Blockchain bc);  SyncQueueImpl(Blockchain bc, Long endBlockNumber); }
SyncQueueImpl implements SyncQueueIfc { ValidatedHeaders validateChain(List<HeaderElement> chain) { if (parentHeaderValidator == null) return ValidatedHeaders.Empty; for (int i = 1; i < chain.size(); i++) { BlockHeaderWrapper parent = chain.get(i - 1).header; BlockHeaderWrapper header = chain.get(i).header; if (!parentHeaderValidator.validate(header.getHeader(), parent.getHeader())) { return new ValidatedHeaders(Collections.singletonList(header), false, parentHeaderValidator.getErrors().isEmpty() ? "" : parentHeaderValidator.getErrors().get(0)); } } return ValidatedHeaders.Empty; } 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 { ValidatedHeaders validateChain(List<HeaderElement> chain) { if (parentHeaderValidator == null) return ValidatedHeaders.Empty; for (int i = 1; i < chain.size(); i++) { BlockHeaderWrapper parent = chain.get(i - 1).header; BlockHeaderWrapper header = chain.get(i).header; if (!parentHeaderValidator.validate(header.getHeader(), parent.getHeader())) { return new ValidatedHeaders(Collections.singletonList(header), false, parentHeaderValidator.getErrors().isEmpty() ? "" : parentHeaderValidator.getErrors().get(0)); } } return ValidatedHeaders.Empty; } 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);  }