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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations