use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class DownloadingBodiesSyncState method tryFindBlockHeader.
private Optional<BlockHeader> tryFindBlockHeader(NodeID peerId) {
// we start from the last chunk that can be downloaded
for (int segmentNumber = segmentByNode.get(peerId); segmentNumber >= 0; segmentNumber--) {
Deque<Integer> chunks = chunksBySegment.get(segmentNumber);
// if the segment stack is empty then continue to next segment
if (!chunks.isEmpty()) {
int chunkNumber = chunks.pollLast();
Deque<BlockHeader> headers = pendingHeaders.get(chunkNumber);
BlockHeader header = headers.poll();
while (header != null) {
// we double check if the header was not downloaded or obtained by another way
if (!syncInformation.isKnownBlock(header.getHash().getBytes())) {
chunksBeingDownloaded.put(peerId, chunkNumber);
segmentsBeingDownloaded.put(peerId, segmentNumber);
return Optional.of(header);
}
header = headers.poll();
}
}
}
return Optional.empty();
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class DownloadingHeadersSyncState method newBlockHeaders.
@Override
public void newBlockHeaders(List<BlockHeader> chunk) {
Optional<ChunkDescriptor> currentChunk = chunksDownloadHelper.getCurrentChunk();
if (!currentChunk.isPresent() || chunk.size() != currentChunk.get().getCount() || !ByteUtil.fastEquals(chunk.get(0).getHash().getBytes(), currentChunk.get().getHash())) {
syncEventsHandler.onErrorSyncing("Invalid chunk received from node {} {}", EventType.INVALID_MESSAGE, syncInformation.getSelectedPeerId(), currentChunk.get().getHash());
return;
}
Deque<BlockHeader> headers = new ArrayDeque<>();
// the headers come ordered by block number desc
// we start adding the first parent header
headers.add(chunk.get(chunk.size() - 1));
for (int k = 1; k < chunk.size(); ++k) {
BlockHeader parentHeader = chunk.get(chunk.size() - k);
BlockHeader header = chunk.get(chunk.size() - k - 1);
if (!syncInformation.blockHeaderIsValid(header, parentHeader)) {
syncEventsHandler.onErrorSyncing("Invalid header received from node {} {} {}", EventType.INVALID_HEADER, syncInformation.getSelectedPeerId(), header.getNumber(), header.getShortHash());
return;
}
headers.add(header);
}
pendingHeaders.add(headers);
if (!chunksDownloadHelper.hasNextChunk()) {
// Finished verifying headers
syncEventsHandler.startDownloadingBodies(pendingHeaders, skeletons);
return;
}
resetTimeElapsed();
trySendRequest();
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class BlockStoreTest method removeHeader.
@Test
public void removeHeader() {
BlockStore store = new BlockStore();
BlockHeader blockHeader = new BlockHeader(new byte[] {}, new byte[] {}, RskAddress.nullAddress().getBytes(), new Bloom().getData(), new byte[] {}, 1, new byte[] {}, 0, 0, new byte[] {}, new byte[] {}, new byte[] {}, new byte[] {}, new byte[] {}, 0);
store.saveHeader(blockHeader);
store.removeHeader(blockHeader);
Assert.assertFalse(store.hasHeader(blockHeader.getHash()));
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class NodeBlockProcessorUnclesTest method addBlockWithTwoUnknownUncles.
@Test
public void addBlockWithTwoUnknownUncles() throws UnknownHostException {
BlockChainImpl blockChain = new BlockChainBuilder().build();
NodeBlockProcessor processor = createNodeBlockProcessor(blockChain);
Block genesis = processor.getBlockchain().getBestBlock();
BlockBuilder blockBuilder = new BlockBuilder(blockChain, new BlockGenerator());
Block block1 = blockBuilder.parent(genesis).build();
Block uncle1 = blockBuilder.parent(genesis).build();
Block uncle2 = blockBuilder.parent(genesis).build();
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle1.getHeader());
uncles.add(uncle2.getHeader());
Block block2 = blockBuilder.parent(block1).uncles(uncles).build();
processor.processBlock(null, block1);
SimpleMessageChannel sender = new SimpleMessageChannel();
processor.processBlock(sender, block2);
Assert.assertEquals(2, processor.getBlockchain().getBestBlock().getNumber());
Assert.assertArrayEquals(block2.getHash().getBytes(), processor.getBlockchain().getBestBlockHash());
Assert.assertEquals(0, sender.getGetBlockMessages().size());
}
use of org.ethereum.core.BlockHeader in project rskj by rsksmart.
the class IndexedBlockStore method getListHeadersEndWith.
@Override
public synchronized List<BlockHeader> getListHeadersEndWith(byte[] hash, long qty) {
List<Block> blocks = getListBlocksEndWith(hash, qty);
List<BlockHeader> headers = new ArrayList<>(blocks.size());
for (Block b : blocks) {
headers.add(b.getHeader());
}
return headers;
}
Aggregations