Search in sources :

Example 11 with RskContext

use of co.rsk.RskContext in project rskj by rsksmart.

the class CliToolRskContextAware method execute.

public void execute(@Nonnull String[] args, @Nonnull Factory<RskContext> contextFactory, @Nonnull NodeStopper stopper) {
    Objects.requireNonNull(args, "args should not be null");
    Objects.requireNonNull(contextFactory, "contextFactory should not be null");
    Objects.requireNonNull(stopper, "stopper should not be null");
    String cliToolName = getClass().getSimpleName();
    try (RskContext ctx = contextFactory.create()) {
        printInfo("{} started", cliToolName);
        onExecute(args, ctx);
        printInfo("{} finished", cliToolName);
        stopper.stop(0);
    } catch (Exception e) {
        printError("{} failed", cliToolName, e);
        stopper.stop(1);
    }
}
Also used : RskContext(co.rsk.RskContext)

Example 12 with RskContext

use of co.rsk.RskContext 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 13 with RskContext

use of co.rsk.RskContext 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 14 with RskContext

use of co.rsk.RskContext 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 15 with RskContext

use of co.rsk.RskContext 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)

Aggregations

RskContext (co.rsk.RskContext)20 Test (org.junit.Test)17 NodeStopper (co.rsk.util.NodeStopper)10 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)9 RskTestContext (org.ethereum.util.RskTestContext)8 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)4 BlockFactory (org.ethereum.core.BlockFactory)3 Blockchain (org.ethereum.core.Blockchain)3 HashMapDB (org.ethereum.datasource.HashMapDB)3 PreflightChecksUtils (co.rsk.util.PreflightChecksUtils)2 Genesis (org.ethereum.core.Genesis)2 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)2 BlockStore (org.ethereum.db.BlockStore)2 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)2 ReceiptStore (org.ethereum.db.ReceiptStore)2 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)2