Search in sources :

Example 46 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockHeadersMessage method parse.

private void parse() {
    RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
    blockHeaders = new ArrayList<>();
    for (int i = 0; i < paramsList.size(); ++i) {
        RLPList rlpData = ((RLPList) paramsList.get(i));
        blockHeaders.add(new BlockHeader(rlpData, true));
    }
    parsed = true;
}
Also used : BlockHeader(org.ethereum.core.BlockHeader) RLPList(org.ethereum.util.RLPList)

Example 47 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockHeadersMessage method toString.

@Override
public String toString() {
    if (!parsed) {
        parse();
    }
    StringBuilder payload = new StringBuilder();
    payload.append("count( ").append(blockHeaders.size()).append(" )");
    if (logger.isDebugEnabled()) {
        payload.append(" ");
        for (BlockHeader header : blockHeaders) {
            payload.append(Hex.toHexString(header.getHash().getBytes()).substring(0, 6)).append(" ").append(header.getNumber()).append(" | ");
        }
        if (!blockHeaders.isEmpty()) {
            payload.delete(payload.length() - 3, payload.length());
        }
    }
    return "[" + getCommand().name() + " " + payload + "]";
}
Also used : BlockHeader(org.ethereum.core.BlockHeader)

Example 48 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockHeadersByHashMessageTest method createMessage.

@Test
public void createMessage() {
    List<BlockHeader> blocks = new ArrayList<>();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block = blockGenerator.getGenesisBlock();
    for (int k = 1; k <= 4; k++) {
        Block b = blockGenerator.createChildBlock(block);
        blocks.add(b.getHeader());
    }
    BlockHeadersResponseMessage message = new BlockHeadersResponseMessage(1, blocks);
    Assert.assertEquals(1, message.getId());
    List<BlockHeader> mblocks = message.getBlockHeaders();
    Assert.assertEquals(mblocks.size(), blocks.size());
    for (int i = 0; i < blocks.size(); i++) Assert.assertEquals(blocks.get(1).getHash(), mblocks.get(1).getHash());
}
Also used : ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 49 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockHeadersMessageTest method getBlockHeaders.

@Test
public void getBlockHeaders() {
    List<BlockHeader> blocks = new ArrayList<>();
    BlockGenerator blockGenerator = new BlockGenerator();
    for (int k = 0; k < 5; k++) {
        Block b = blockGenerator.getBlock(k);
        blocks.add(b.getHeader());
    }
    BlockHeadersMessage message = new BlockHeadersMessage(blocks);
    List<BlockHeader> mblocks = message.getBlockHeaders();
    Assert.assertEquals(mblocks.size(), blocks.size());
    for (int i = 0; i < blocks.size(); i++) {
        Assert.assertEquals(blocks.get(1).getHash(), mblocks.get(1).getHash());
    }
}
Also used : ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 50 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class FamilyUtilsTest method getUnclesHeaders.

@Test
public void getUnclesHeaders() {
    BlockStore store = createBlockStore();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block uncle11 = blockGenerator.createChildBlock(genesis);
    Block uncle111 = blockGenerator.createChildBlock(uncle11);
    Block uncle12 = blockGenerator.createChildBlock(genesis);
    Block uncle121 = blockGenerator.createChildBlock(uncle12);
    Block block2 = blockGenerator.createChildBlock(block1);
    Block uncle21 = blockGenerator.createChildBlock(block1);
    Block uncle22 = blockGenerator.createChildBlock(block1);
    Block block3 = blockGenerator.createChildBlock(block2);
    Block uncle31 = blockGenerator.createChildBlock(block2);
    Block uncle32 = blockGenerator.createChildBlock(block2);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    store.saveBlock(block1, TEST_DIFFICULTY, true);
    store.saveBlock(uncle11, TEST_DIFFICULTY, false);
    store.saveBlock(uncle12, TEST_DIFFICULTY, false);
    store.saveBlock(uncle111, TEST_DIFFICULTY, false);
    store.saveBlock(uncle121, TEST_DIFFICULTY, false);
    store.saveBlock(block2, TEST_DIFFICULTY, true);
    store.saveBlock(uncle21, TEST_DIFFICULTY, false);
    store.saveBlock(uncle22, TEST_DIFFICULTY, false);
    store.saveBlock(block3, TEST_DIFFICULTY, true);
    store.saveBlock(uncle31, TEST_DIFFICULTY, false);
    store.saveBlock(uncle32, TEST_DIFFICULTY, false);
    List<BlockHeader> list = FamilyUtils.getUnclesHeaders(store, block3, 3);
    Assert.assertNotNull(list);
    Assert.assertFalse(list.isEmpty());
    Assert.assertEquals(4, list.size());
    Assert.assertTrue(containsHash(uncle11.getHash(), list));
    Assert.assertTrue(containsHash(uncle12.getHash(), list));
    Assert.assertTrue(containsHash(uncle21.getHash(), list));
    Assert.assertTrue(containsHash(uncle22.getHash(), list));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

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