Search in sources :

Example 11 with BlockFactory

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

the class GitHubBasicTest method runDifficultyFrontierTest.

@Test
public void runDifficultyFrontierTest() throws IOException, ParseException {
    BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
    String json = JSONReader.loadJSONFromCommit("BasicTests/difficultyFrontier.json", shacommit);
    DifficultyTestSuite testSuite = new DifficultyTestSuite(json);
    for (DifficultyTestCase testCase : testSuite.getTestCases()) {
        logger.info("Running {}\n", testCase.getName());
        BlockHeader current = testCase.getCurrent(blockFactory);
        BlockHeader parent = testCase.getParent(blockFactory);
        assertEquals(testCase.getExpectedDifficulty(), DIFFICULTY_CALCULATOR.calcDifficulty(current, parent));
    }
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 12 with BlockFactory

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

the class ImportBlocks method onExecute.

@Override
protected void onExecute(@Nonnull String[] args, @Nonnull RskContext ctx) throws Exception {
    BlockFactory blockFactory = ctx.getBlockFactory();
    BlockStore blockStore = ctx.getBlockStore();
    String filename = args[0];
    try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
        importBlocks(blockFactory, blockStore, reader);
    }
}
Also used : BlockStore(org.ethereum.db.BlockStore) BlockFactory(org.ethereum.core.BlockFactory) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 13 with BlockFactory

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

the class CliToolsTest method rewindBlocks.

@Test
public void rewindBlocks() {
    TestSystemProperties config = new TestSystemProperties();
    BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
    KeyValueDataSource keyValueDataSource = new HashMapDB();
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, keyValueDataSource, new HashMapBlocksIndex());
    int blocksToGenerate = 14;
    Keccak256 parentHash = Keccak256.ZERO_HASH;
    for (long i = 0; i < blocksToGenerate; i++) {
        Block block = mock(Block.class);
        Keccak256 blockHash = randomHash();
        when(block.getHash()).thenReturn(blockHash);
        when(block.getParentHash()).thenReturn(parentHash);
        when(block.getNumber()).thenReturn(i);
        when(block.getEncoded()).thenReturn(TestUtils.randomBytes(128));
        indexedBlockStore.saveBlock(block, ZERO, true);
        parentHash = blockHash;
    }
    Block bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is((long) blocksToGenerate - 1));
    RskContext rskContext = mock(RskContext.class);
    doReturn(indexedBlockStore).when(rskContext).getBlockStore();
    RepositoryLocator repositoryLocator = mock(RepositoryLocator.class);
    doReturn(Optional.of(mock(RepositorySnapshot.class))).when(repositoryLocator).findSnapshotAt(any());
    doReturn(repositoryLocator).when(rskContext).getRepositoryLocator();
    NodeStopper stopper = mock(NodeStopper.class);
    StringBuilder output = new StringBuilder();
    RewindBlocks rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { "fmi" }, () -> rskContext, stopper);
    String data = output.toString();
    Assert.assertTrue(data.contains("No inconsistent block has been found"));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    long blockToRewind = blocksToGenerate / 2;
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { String.valueOf(blockToRewind) }, () -> rskContext, stopper);
    bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is(blockToRewind));
    data = output.toString();
    Assert.assertTrue(data.contains("New highest block number stored in db: " + blockToRewind));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { String.valueOf(blocksToGenerate + 1) }, () -> rskContext, stopper);
    bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is(blockToRewind));
    data = output.toString();
    Assert.assertTrue(data.contains("No need to rewind"));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    doReturn(Optional.empty()).when(repositoryLocator).findSnapshotAt(any());
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { "fmi" }, () -> rskContext, stopper);
    data = output.toString();
    Assert.assertTrue(data.contains("Min inconsistent block number: 0"));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { "rbc" }, () -> rskContext, stopper);
    data = output.toString();
    Assert.assertTrue(data.contains("Min inconsistent block number: 0"));
    Assert.assertTrue(data.contains("New highest block number stored in db: -1"));
    verify(stopper).stop(0);
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) Keccak256(co.rsk.crypto.Keccak256) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryLocator(co.rsk.db.RepositoryLocator) RskContext(co.rsk.RskContext) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) NodeStopper(co.rsk.util.NodeStopper) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 14 with BlockFactory

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

the class CliToolsTest method connectBlocks.

@Test
public void connectBlocks() throws IOException, DslProcessorException {
    DslParser parser = DslParser.fromResource("dsl/blocks01b.txt");
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    WorldDslProcessor processor = new WorldDslProcessor(world);
    processor.processCommands(parser);
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    Block block1 = world.getBlockByName("b01");
    Block block2 = world.getBlockByName("b02");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("1,");
    stringBuilder.append(ByteUtil.toHexString(block1.getHash().getBytes()));
    stringBuilder.append(",02,");
    stringBuilder.append(ByteUtil.toHexString(block1.getEncoded()));
    stringBuilder.append("\n");
    stringBuilder.append("1,");
    stringBuilder.append(ByteUtil.toHexString(block2.getHash().getBytes()));
    stringBuilder.append(",03,");
    stringBuilder.append(ByteUtil.toHexString(block2.getEncoded()));
    stringBuilder.append("\n");
    File blocksFile = new File(tempFolder.getRoot(), "blocks.txt");
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(blocksFile))) {
        writer.write(stringBuilder.toString());
    }
    String[] args = new String[] { blocksFile.getAbsolutePath() };
    RskContext rskContext = mock(RskContext.class);
    doReturn(blockchain).when(rskContext).getBlockchain();
    doReturn(world.getBlockStore()).when(rskContext).getBlockStore();
    doReturn(world.getTrieStore()).when(rskContext).getTrieStore();
    doReturn(receiptStore).when(rskContext).getReceiptStore();
    doReturn(new BlockFactory(ActivationConfigsForTest.all())).when(rskContext).getBlockFactory();
    NodeStopper stopper = mock(NodeStopper.class);
    ConnectBlocks connectBlocksCliTool = new ConnectBlocks();
    connectBlocksCliTool.execute(args, () -> rskContext, stopper);
    Assert.assertEquals(2, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(block1.getHash(), blockchain.getBlockByNumber(1).getHash());
    Assert.assertEquals(block2.getHash(), blockchain.getBlockByNumber(2).getHash());
    verify(stopper).stop(0);
}
Also used : WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) BlockFactory(org.ethereum.core.BlockFactory) Blockchain(org.ethereum.core.Blockchain) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) RskContext(co.rsk.RskContext) DslParser(co.rsk.test.dsl.DslParser) Block(org.ethereum.core.Block) NodeStopper(co.rsk.util.NodeStopper) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 15 with BlockFactory

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

the class IndexedBlockStoreTest method setup.

// @Before
public void setup() throws URISyntaxException, IOException {
    URL scenario1 = ClassLoader.getSystemResource("blockstore/load.dmp");
    File file = new File(scenario1.toURI());
    List<String> strData = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
    config = new TestSystemProperties();
    blockFactory = new BlockFactory(config.getActivationConfig());
    Block genesis = RskTestFactory.getGenesisInstance(config);
    blocks.add(genesis);
    cumDifficulty = cumDifficulty.add(genesis.getCumulativeDifficulty());
    for (String blockRLP : strData) {
        Block block = blockFactory.decodeBlock(Hex.decode(blockRLP));
        if (block.getNumber() % 1000 == 0)
            logger.info("adding block.hash: [{}] block.number: [{}]", block.getPrintableHash(), block.getNumber());
        blocks.add(block);
        cumDifficulty = cumDifficulty.add(block.getCumulativeDifficulty());
    }
    logger.info("total difficulty: {}", cumDifficulty);
    logger.info("total blocks loaded: {}", blocks.size());
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) Block(org.ethereum.core.Block) File(java.io.File) URL(java.net.URL) TestSystemProperties(co.rsk.config.TestSystemProperties)

Aggregations

BlockFactory (org.ethereum.core.BlockFactory)20 Test (org.junit.Test)7 Block (org.ethereum.core.Block)6 TestSystemProperties (co.rsk.config.TestSystemProperties)4 BlockHeader (org.ethereum.core.BlockHeader)4 Blockchain (org.ethereum.core.Blockchain)4 Before (org.junit.Before)4 RskContext (co.rsk.RskContext)3 DifficultyCalculator (co.rsk.core.DifficultyCalculator)3 Keccak256 (co.rsk.crypto.Keccak256)3 World (co.rsk.test.World)3 NodeStopper (co.rsk.util.NodeStopper)3 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)3 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)3 HashMapDB (org.ethereum.datasource.HashMapDB)3 BlockStore (org.ethereum.db.BlockStore)3 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 BlockMiner (co.rsk.blockchain.utils.BlockMiner)2 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)2 PeersInformation (co.rsk.net.sync.PeersInformation)2