Search in sources :

Example 6 with NodeStopper

use of co.rsk.util.NodeStopper 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 7 with NodeStopper

use of co.rsk.util.NodeStopper in project rskj by rsksmart.

the class CliToolsTest method startBootstrap.

@Test
public void startBootstrap() throws Exception {
    // check thread setup
    Thread thread = new Thread(() -> {
    });
    StartBootstrap.setUpThread(thread);
    assertEquals("main", thread.getName());
    // check happy flow of bootstrap node start
    ArgumentCaptor<Thread> threadCaptor = ArgumentCaptor.forClass(Thread.class);
    NodeRunner runner = mock(NodeRunner.class);
    RskContext ctx = mock(RskContext.class);
    doReturn(runner).when(ctx).getNodeRunner();
    PreflightChecksUtils preflightChecks = mock(PreflightChecksUtils.class);
    Runtime runtime = mock(Runtime.class);
    NodeStopper nodeStopper = mock(NodeStopper.class);
    StartBootstrap.runBootstrapNode(ctx, preflightChecks, runtime, nodeStopper);
    verify(preflightChecks, times(1)).runChecks();
    verify(runner, times(1)).run();
    verify(runtime, times(1)).addShutdownHook(threadCaptor.capture());
    assertEquals("stopper", threadCaptor.getValue().getName());
    // check unhappy flow of bootstrap node start
    doThrow(new RuntimeException()).when(preflightChecks).runChecks();
    StartBootstrap.runBootstrapNode(ctx, preflightChecks, runtime, nodeStopper);
    verify(preflightChecks, times(2)).runChecks();
    verify(runner, times(1)).run();
    verify(runtime, times(1)).addShutdownHook(any());
    verify(ctx, times(1)).close();
    verify(nodeStopper, times(1)).stop(1);
}
Also used : RskContext(co.rsk.RskContext) NodeStopper(co.rsk.util.NodeStopper) NodeRunner(co.rsk.NodeRunner) PreflightChecksUtils(co.rsk.util.PreflightChecksUtils) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 8 with NodeStopper

use of co.rsk.util.NodeStopper in project rskj by rsksmart.

the class CliToolsTest method showStateInfo.

@Test
public void showStateInfo() throws FileNotFoundException, DslProcessorException {
    DslParser parser = DslParser.fromResource("dsl/contracts02.txt");
    World world = new World();
    WorldDslProcessor processor = new WorldDslProcessor(world);
    processor.processCommands(parser);
    String[] args = new String[] { "best" };
    RskContext rskContext = mock(RskContext.class);
    doReturn(world.getBlockStore()).when(rskContext).getBlockStore();
    doReturn(world.getTrieStore()).when(rskContext).getTrieStore();
    NodeStopper stopper = mock(NodeStopper.class);
    StringBuilder output = new StringBuilder();
    ShowStateInfo showStateInfoCliTool = new ShowStateInfo(output::append);
    showStateInfoCliTool.execute(args, () -> rskContext, stopper);
    String data = output.toString();
    Block block = world.getBlockByName("b02");
    String blockLine = "Block hash: " + ByteUtil.toHexString(block.getHash().getBytes());
    Assert.assertTrue(data.contains(blockLine));
    String longValueLine = "Trie long values: 1";
    Assert.assertTrue(data.contains(longValueLine));
    verify(stopper).stop(0);
}
Also used : WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) RskContext(co.rsk.RskContext) DslParser(co.rsk.test.dsl.DslParser) NodeStopper(co.rsk.util.NodeStopper) Block(org.ethereum.core.Block) World(co.rsk.test.World) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 9 with NodeStopper

use of co.rsk.util.NodeStopper in project rskj by rsksmart.

the class CliToolsTest method exportState.

@Test
public void exportState() throws IOException, DslProcessorException {
    DslParser parser = DslParser.fromResource("dsl/contracts02.txt");
    World world = new World();
    WorldDslProcessor processor = new WorldDslProcessor(world);
    processor.processCommands(parser);
    File stateFile = new File(tempFolder.getRoot(), "state.txt");
    String[] args = new String[] { "2", stateFile.getAbsolutePath() };
    RskContext rskContext = mock(RskContext.class);
    doReturn(world.getBlockStore()).when(rskContext).getBlockStore();
    doReturn(world.getTrieStore()).when(rskContext).getTrieStore();
    NodeStopper stopper = mock(NodeStopper.class);
    ExportState exportStateCliTool = new ExportState();
    exportStateCliTool.execute(args, () -> rskContext, stopper);
    String data = new String(Files.readAllBytes(stateFile.toPath()), StandardCharsets.UTF_8);
    Block block = world.getBlockByName("b02");
    Optional<Trie> otrie = world.getTrieStore().retrieve(block.getStateRoot());
    Assert.assertTrue(otrie.isPresent());
    Trie trie = otrie.get();
    byte[] encoded = trie.toMessage();
    String line = ByteUtil.toHexString(encoded);
    Assert.assertTrue(data.contains(line));
    verify(stopper).stop(0);
}
Also used : WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) World(co.rsk.test.World) RskContext(co.rsk.RskContext) DslParser(co.rsk.test.dsl.DslParser) NodeStopper(co.rsk.util.NodeStopper) Block(org.ethereum.core.Block) Trie(co.rsk.trie.Trie) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 10 with NodeStopper

use of co.rsk.util.NodeStopper in project rskj by rsksmart.

the class CliToolsTest method importState.

@Test
public void importState() throws IOException {
    byte[] value = new byte[42];
    Random random = new Random();
    random.nextBytes(value);
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(ByteUtil.toHexString(value));
    stringBuilder.append("\n");
    File stateFile = new File(tempFolder.getRoot(), "state.txt");
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(stateFile))) {
        writer.write(stringBuilder.toString());
    }
    String databaseDir = new File(tempFolder.getRoot(), "db").getAbsolutePath();
    String[] args = new String[] { stateFile.getAbsolutePath() };
    RskContext rskContext = mock(RskContext.class);
    RskSystemProperties rskSystemProperties = mock(RskSystemProperties.class);
    doReturn(databaseDir).when(rskSystemProperties).databaseDir();
    doReturn(rskSystemProperties).when(rskContext).getRskSystemProperties();
    NodeStopper stopper = mock(NodeStopper.class);
    ImportState importStateCliTool = new ImportState();
    importStateCliTool.execute(args, () -> rskContext, stopper);
    byte[] key = new Keccak256(Keccak256Helper.keccak256(value)).getBytes();
    KeyValueDataSource trieDB = LevelDbDataSource.makeDataSource(Paths.get(databaseDir, "unitrie"));
    byte[] result = trieDB.get(key);
    trieDB.close();
    Assert.assertNotNull(result);
    Assert.assertArrayEquals(value, result);
    verify(stopper).stop(0);
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Random(java.util.Random) RskContext(co.rsk.RskContext) NodeStopper(co.rsk.util.NodeStopper) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Aggregations

NodeStopper (co.rsk.util.NodeStopper)11 RskContext (co.rsk.RskContext)10 Test (org.junit.Test)10 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)9 World (co.rsk.test.World)6 DslParser (co.rsk.test.dsl.DslParser)6 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)6 Block (org.ethereum.core.Block)6 Keccak256 (co.rsk.crypto.Keccak256)3 BlockFactory (org.ethereum.core.BlockFactory)3 Blockchain (org.ethereum.core.Blockchain)3 HashMapDB (org.ethereum.datasource.HashMapDB)3 PreflightChecksUtils (co.rsk.util.PreflightChecksUtils)2 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)2 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)2 ReceiptStore (org.ethereum.db.ReceiptStore)2 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)2 NodeRunner (co.rsk.NodeRunner)1 RskSystemProperties (co.rsk.config.RskSystemProperties)1 TestSystemProperties (co.rsk.config.TestSystemProperties)1