Search in sources :

Example 41 with BlockHeader

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();
}
Also used : BlockHeader(org.ethereum.core.BlockHeader)

Example 42 with BlockHeader

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();
}
Also used : BlockHeader(org.ethereum.core.BlockHeader)

Example 43 with BlockHeader

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()));
}
Also used : Bloom(org.ethereum.core.Bloom) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 44 with BlockHeader

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());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 45 with BlockHeader

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;
}
Also used : Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader)

Aggregations

BlockHeader (org.ethereum.core.BlockHeader)65 Test (org.junit.Test)36 Block (org.ethereum.core.Block)27 ArrayList (java.util.ArrayList)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)9 BlockDifficulty (co.rsk.core.BlockDifficulty)5 Keccak256 (co.rsk.crypto.Keccak256)5 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)4 BlockBuilder (co.rsk.test.builders.BlockBuilder)4 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 BlockStore (org.ethereum.db.BlockStore)4 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)3 BigInteger (java.math.BigInteger)3 MainNetConfig (org.ethereum.config.net.MainNetConfig)3 DataWord (org.ethereum.vm.DataWord)3 DifficultyCalculator (co.rsk.core.DifficultyCalculator)2 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)2 HashSet (java.util.HashSet)2 Bloom (org.ethereum.core.Bloom)2 Transaction (org.ethereum.core.Transaction)2