Search in sources :

Example 16 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class IndexedBlockStoreTest method test5.

// leveldb + mapdb, save part to disk part to cache, and check it exist
@Test
@Ignore
public void test5() {
    BigInteger bi = new BigInteger(32, new Random());
    String testDir = "test_db_" + bi;
    config.setDataBaseDir(testDir);
    DB indexDB = createMapDB(testDir);
    KeyValueDataSource blocksDB = new LevelDbDataSource("blocks", config.databaseDir());
    blocksDB.init();
    try {
        IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, blocksDB, new MapDBBlocksIndex(indexDB));
        BlockDifficulty cummDiff = BlockDifficulty.ZERO;
        int preloadSize = blocks.size() / 2;
        for (int i = 0; i < preloadSize; ++i) {
            Block block = blocks.get(i);
            cummDiff = cummDiff.add(block.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(block, cummDiff, true);
        }
        indexedBlockStore.flush();
        for (int i = preloadSize; i < blocks.size(); ++i) {
            Block block = blocks.get(i);
            cummDiff = cummDiff.add(block.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(block, cummDiff, true);
        }
        // testing:   getTotalDifficultyForHash(byte[])
        // testing:   getMaxNumber()
        long bestIndex = blocks.get(blocks.size() - 1).getNumber();
        assertEquals(bestIndex, indexedBlockStore.getMaxNumber());
        assertEquals(cumDifficulty, indexedBlockStore.getTotalDifficultyForHash(blocks.get(blocks.size() - 1).getHash().getBytes()));
        // testing:  getBlockByHash(byte[])
        Block block = blocks.get(50);
        Block block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
        assertEquals(block.getNumber(), block_.getNumber());
        block = blocks.get(150);
        block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
        assertEquals(block.getNumber(), block_.getNumber());
        block = blocks.get(0);
        block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
        assertEquals(block.getNumber(), block_.getNumber());
        block = blocks.get(8003);
        block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
        assertEquals(block.getNumber(), block_.getNumber());
        block_ = indexedBlockStore.getBlockByHash(Hex.decode("00112233"));
        assertEquals(null, block_);
        // testing:  getChainBlockByNumber(long)
        block = blocks.get(50);
        block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
        assertEquals(block.getNumber(), block_.getNumber());
        block = blocks.get(150);
        block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
        assertEquals(block.getNumber(), block_.getNumber());
        block = blocks.get(0);
        block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
        assertEquals(block.getNumber(), block_.getNumber());
        block = blocks.get(8003);
        block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
        assertEquals(block.getNumber(), block_.getNumber());
        block_ = indexedBlockStore.getChainBlockByNumber(10000);
        assertEquals(null, block_);
        // testing: getBlocksInformationByNumber(long)
        block = blocks.get(50);
        BlockInformation blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
        Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
        Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
        block = blocks.get(150);
        blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
        Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
        Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
        block = blocks.get(0);
        blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
        Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
        Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
        block = blocks.get(8003);
        blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
        Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
        Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
        int blocksNum = indexedBlockStore.getBlocksInformationByNumber(10000).size();
        assertEquals(0, blocksNum);
        // testing: getListHashesEndWith(byte[], long)
        block = blocks.get(8003);
        List<byte[]> hashList = indexedBlockStore.getListHashesEndWith(block.getHash().getBytes(), 100);
        for (int i = 0; i < 100; ++i) {
            block = blocks.get(8003 - i);
            String hash = ByteUtil.toHexString(hashList.get(i));
            String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
            assertEquals(hash_, hash);
        }
        // testing: getListHashesStartWith(long, long)
        block = blocks.get(7003);
        hashList = indexedBlockStore.getListHashesStartWith(block.getNumber(), 100);
        for (int i = 0; i < 100; ++i) {
            block = blocks.get(7003 + i);
            String hash = ByteUtil.toHexString(hashList.get(i));
            String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
            assertEquals(hash_, hash);
        }
        indexedBlockStore.flush();
        blocksDB.close();
        indexDB.close();
        // testing after: REOPEN
        indexDB = createMapDB(testDir);
        blocksDB = new LevelDbDataSource("blocks", config.databaseDir());
        blocksDB.init();
        indexedBlockStore = new IndexedBlockStore(blockFactory, blocksDB, new MapDBBlocksIndex(indexDB));
        // testing: getListHashesStartWith(long, long)
        block = blocks.get(7003);
        hashList = indexedBlockStore.getListHashesStartWith(block.getNumber(), 100);
        for (int i = 0; i < 100; ++i) {
            block = blocks.get(7003 + i);
            String hash = ByteUtil.toHexString(hashList.get(i));
            String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
            assertEquals(hash_, hash);
        }
    } finally {
        blocksDB.close();
        indexDB.close();
        FileUtil.recursiveDelete(testDir);
    }
}
Also used : LevelDbDataSource(org.ethereum.datasource.LevelDbDataSource) BlockDifficulty(co.rsk.core.BlockDifficulty) BigInteger(java.math.BigInteger) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) MapDBBlocksIndex(co.rsk.db.MapDBBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) DB(org.mapdb.DB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class IndexedBlockStoreTest method test8.

// leveldb + mapdb, multi branch, total re-branch test
@Test
@Ignore("Ethereum block format")
public void test8() {
    BigInteger bi = new BigInteger(32, new Random());
    String testDir = "test_db_" + bi;
    config.setDataBaseDir(testDir);
    DB indexDB = createMapDB(testDir);
    KeyValueDataSource blocksDB = new LevelDbDataSource("blocks", config.databaseDir());
    blocksDB.init();
    try {
        IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, blocksDB, new MapDBBlocksIndex(indexDB));
        Block genesis = RskTestFactory.getGenesisInstance(config);
        List<Block> bestLine = getRandomChain(blockFactory, genesis.getHash().getBytes(), 1, 100);
        indexedBlockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
        BlockDifficulty td = BlockDifficulty.ZERO;
        for (int i = 0; i < bestLine.size(); ++i) {
            Block newBlock = bestLine.get(i);
            td = td.add(newBlock.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(newBlock, td, true);
        }
        byte[] forkParentHash = bestLine.get(60).getHash().getBytes();
        long forkParentNumber = bestLine.get(60).getNumber();
        List<Block> forkLine = getRandomChain(blockFactory, forkParentHash, forkParentNumber + 1, 10);
        for (int i = 0; i < forkLine.size(); ++i) {
            Block newBlock = forkLine.get(i);
            Block parentBlock = indexedBlockStore.getBlockByHash(newBlock.getParentHash().getBytes());
            td = indexedBlockStore.getTotalDifficultyForHash(parentBlock.getHash().getBytes());
            td = td.add(newBlock.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(newBlock, td, false);
        }
        Block bestBlock = bestLine.get(bestLine.size() - 1);
        Block forkBlock = forkLine.get(forkLine.size() - 1);
        assertTrue(indexedBlockStore.getBestBlock().getNumber() == 100);
        indexedBlockStore.reBranch(forkBlock);
        assertTrue(indexedBlockStore.getBestBlock().getNumber() == 71);
        // Assert that all fork moved to the main line
        for (Block currBlock : forkLine) {
            Long number = currBlock.getNumber();
            Block chainBlock = indexedBlockStore.getChainBlockByNumber(number);
            assertEquals(currBlock.getPrintableHash(), chainBlock.getPrintableHash());
        }
        // Assert that all fork moved to the main line
        // re-branch back to previous line and assert that
        // all the block really moved
        bestBlock = bestLine.get(bestLine.size() - 1);
        indexedBlockStore.reBranch(bestBlock);
        for (Block currBlock : bestLine) {
            Long number = currBlock.getNumber();
            Block chainBlock = indexedBlockStore.getChainBlockByNumber(number);
            assertEquals(currBlock.getPrintableHash(), chainBlock.getPrintableHash());
        }
    } finally {
        blocksDB.close();
        indexDB.close();
        FileUtil.recursiveDelete(testDir);
    }
}
Also used : LevelDbDataSource(org.ethereum.datasource.LevelDbDataSource) BlockDifficulty(co.rsk.core.BlockDifficulty) BigInteger(java.math.BigInteger) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) MapDBBlocksIndex(co.rsk.db.MapDBBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) DB(org.mapdb.DB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class IndexedBlockStoreTest method test4.

// leveldb + mapdb, save some load, flush to disk, and check it exist
@Test
@Ignore
public void test4() {
    BigInteger bi = new BigInteger(32, new Random());
    String testDir = "test_db_" + bi;
    config.setDataBaseDir(testDir);
    DB indexDB = createMapDB(testDir);
    KeyValueDataSource blocksDB = new LevelDbDataSource("blocks", config.databaseDir());
    blocksDB.init();
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, blocksDB, new MapDBBlocksIndex(indexDB));
    BlockDifficulty cummDiff = BlockDifficulty.ZERO;
    for (Block block : blocks) {
        cummDiff = cummDiff.add(block.getCumulativeDifficulty());
        indexedBlockStore.saveBlock(block, cummDiff, true);
    }
    // testing:   getTotalDifficultyForHash(byte[])
    // testing:   getMaxNumber()
    long bestIndex = blocks.get(blocks.size() - 1).getNumber();
    assertEquals(bestIndex, indexedBlockStore.getMaxNumber());
    assertEquals(cumDifficulty, indexedBlockStore.getTotalDifficultyForHash(blocks.get(blocks.size() - 1).getHash().getBytes()));
    // testing:  getBlockByHash(byte[])
    Block block = blocks.get(50);
    Block block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getBlockByHash(Hex.decode("00112233"));
    assertEquals(null, block_);
    // testing:  getChainBlockByNumber(long)
    block = blocks.get(50);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getChainBlockByNumber(10000);
    assertEquals(null, block_);
    // testing: getBlocksInformationByNumber(long)
    block = blocks.get(50);
    BlockInformation blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(150);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(0);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(8003);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    int blocksNum = indexedBlockStore.getBlocksInformationByNumber(10000).size();
    assertEquals(0, blocksNum);
    // testing: getListHashesEndWith(byte[], long)
    block = blocks.get(8003);
    List<byte[]> hashList = indexedBlockStore.getListHashesEndWith(block.getHash().getBytes(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(8003 - i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
    // testing: getListHashesStartWith(long, long)
    block = blocks.get(7003);
    hashList = indexedBlockStore.getListHashesStartWith(block.getNumber(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(7003 + i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
    blocksDB.close();
    indexDB.close();
    // testing after: REOPEN
    indexDB = createMapDB(testDir);
    blocksDB = new LevelDbDataSource("blocks", config.databaseDir());
    blocksDB.init();
    indexedBlockStore = new IndexedBlockStore(blockFactory, blocksDB, new MapDBBlocksIndex(indexDB));
    // testing: getListHashesStartWith(long, long)
    block = blocks.get(7003);
    hashList = indexedBlockStore.getListHashesStartWith(block.getNumber(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(7003 + i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
    blocksDB.close();
    indexDB.close();
    FileUtil.recursiveDelete(testDir);
}
Also used : LevelDbDataSource(org.ethereum.datasource.LevelDbDataSource) BlockDifficulty(co.rsk.core.BlockDifficulty) BigInteger(java.math.BigInteger) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) MapDBBlocksIndex(co.rsk.db.MapDBBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) DB(org.mapdb.DB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class DownloadingBackwardsBodiesSyncState method newBody.

/**
 * Validates and connects the bodies in order.
 * <p>
 * As the responses may come in any order, they are stored in a priority queue by block number.
 * This allows connecting blocks sequentially with the current child.
 *
 * @param body The requested message containing the body
 * @param peer The peer sending the message.
 */
@Override
public void newBody(BodyResponseMessage body, Peer peer) {
    BlockHeader requestedHeader = inTransit.get(body.getId());
    if (requestedHeader == null) {
        peersInformation.reportEvent(peer.getPeerNodeID(), EventType.INVALID_MESSAGE);
        return;
    }
    Block block = blockFactory.newBlock(requestedHeader, body.getTransactions(), body.getUncles());
    block.seal();
    if (!block.getHash().equals(requestedHeader.getHash())) {
        peersInformation.reportEvent(peer.getPeerNodeID(), EventType.INVALID_MESSAGE);
        return;
    }
    resetTimeElapsed();
    inTransit.remove(body.getId());
    responses.add(block);
    while (!responses.isEmpty() && responses.peek().isParentOf(child)) {
        Block connectedBlock = responses.poll();
        BlockDifficulty blockchainDifficulty = blockStore.getTotalDifficultyForHash(child.getHash().getBytes());
        blockStore.saveBlock(connectedBlock, blockchainDifficulty.subtract(child.getCumulativeDifficulty()), true);
        child = connectedBlock;
    }
    if (child.getNumber() == 1) {
        connectGenesis(child);
        blockStore.flush();
        logger.info("Backward syncing complete");
        syncEventsHandler.stopSyncing();
        return;
    }
    while (!toRequest.isEmpty() && inTransit.size() < syncConfiguration.getMaxRequestedBodies()) {
        requestBodies(toRequest.remove());
    }
    if (toRequest.isEmpty() && inTransit.isEmpty()) {
        blockStore.flush();
        logger.info("Backward syncing phase complete {}", block.getNumber());
        syncEventsHandler.stopSyncing();
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader)

Example 20 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class BlockUtilsTest method blockInSomeBlockChain.

@Test
public void blockInSomeBlockChain() {
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockChainImpl blockChain = blockChainBuilder.build();
    org.ethereum.db.BlockStore blockStore = blockChainBuilder.getBlockStore();
    Block genesis = blockChain.getBestBlock();
    Block block1 = new BlockBuilder(null, null, null).parent(genesis).build();
    Block block1b = new BlockBuilder(null, null, null).parent(genesis).build();
    Block block2 = new BlockBuilder(null, null, null).parent(block1).build();
    Block block3 = new BlockBuilder(null, null, null).parent(block2).build();
    blockStore.saveBlock(block3, new BlockDifficulty(BigInteger.ONE), false);
    blockChain.tryToConnect(block1);
    blockChain.tryToConnect(block1b);
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(genesis, blockChain));
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block1, blockChain));
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block1b, blockChain));
    Assert.assertFalse(BlockUtils.blockInSomeBlockChain(block2, blockChain));
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block3, blockChain));
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockDifficulty (co.rsk.core.BlockDifficulty)60 Test (org.junit.Test)32 Block (org.ethereum.core.Block)23 BigInteger (java.math.BigInteger)14 HashMapDB (org.ethereum.datasource.HashMapDB)11 Keccak256 (co.rsk.crypto.Keccak256)9 Ignore (org.junit.Ignore)9 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)8 BlockHeader (org.ethereum.core.BlockHeader)7 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)6 Coin (co.rsk.core.Coin)5 MapDBBlocksIndex (co.rsk.db.MapDBBlocksIndex)5 LevelDbDataSource (org.ethereum.datasource.LevelDbDataSource)5 BlockStore (org.ethereum.db.BlockStore)5 DifficultyCalculator (co.rsk.core.DifficultyCalculator)4 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)4 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)4 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 ArrayList (java.util.ArrayList)4