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