use of org.ethereum.core.BlockFactory in project rskj by rsksmart.
the class LocalBasicTest method runJsonTest.
private void runJsonTest(String jsonName, ActivationConfig activationConfig, Constants networkConstants) throws IOException {
BlockFactory blockFactory = new BlockFactory(activationConfig);
String json = getJSON(jsonName);
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);
BlockDifficulty calc = new DifficultyCalculator(activationConfig, networkConstants).calcDifficulty(current, parent);
int c = calc.compareTo(parent.getDifficulty());
if (c > 0)
logger.info(" Difficulty increase test\n");
else if (c < 0)
logger.info(" Difficulty decrease test\n");
else
logger.info(" Difficulty without change test\n");
assertEquals(testCase.getExpectedDifficulty(), calc);
}
}
use of org.ethereum.core.BlockFactory in project rskj by rsksmart.
the class BootstrapImporterTest method importData.
@Test
public void importData() throws IOException, URISyntaxException {
BlockStore blockStore = mock(BlockStore.class);
when(blockStore.getMaxNumber()).thenReturn(0L);
when(blockStore.isEmpty()).thenReturn(false);
BlockFactory blockFactory = mock(BlockFactory.class);
TrieStore trieStore = mock(TrieStore.class);
BootstrapDataProvider bootstrapDataProvider = mock(BootstrapDataProvider.class);
// using toURI() instead of getPath() prevent some errors on Windows
// (https://stackoverflow.com/questions/38887853/java-nio-file-invalidpathexception-with-getpath/38888561)
Path path = Paths.get(getClass().getClassLoader().getResource("import/bootstrap-data.bin").toURI());
byte[] oneBlockAndState = Files.readAllBytes(path);
when(bootstrapDataProvider.getBootstrapData()).thenReturn(oneBlockAndState);
when(bootstrapDataProvider.getSelectedHeight()).thenReturn(1L);
BootstrapImporter bootstrapImporter = new BootstrapImporter(blockStore, trieStore, blockFactory, bootstrapDataProvider);
bootstrapImporter.importData();
verify(blockFactory, atLeastOnce()).decodeBlock(any());
}
use of org.ethereum.core.BlockFactory in project rskj by rsksmart.
the class GetCoinbasePerformanceTestCase method mineBlock.
private Block mineBlock(Block parent, int txPerBlock, int unclesPerBlock) {
BlockGenerator blockGenerator = new BlockGenerator(constants, activationConfig);
byte[] prefix = new byte[1000];
byte[] compressedTag = Arrays.concatenate(prefix, RskMiningConstants.RSK_TAG);
Keccak256 mergedMiningHash = new Keccak256(parent.getHashForMergedMining());
NetworkParameters networkParameters = RegTestParams.get();
BtcTransaction mergedMiningCoinbaseTransaction = MinerUtils.getBitcoinMergedMiningCoinbaseTransaction(networkParameters, mergedMiningHash.getBytes());
BtcBlock mergedMiningBlock = MinerUtils.getBitcoinMergedMiningBlock(networkParameters, mergedMiningCoinbaseTransaction);
BigInteger targetDifficulty = DifficultyUtils.difficultyToTarget(parent.getDifficulty());
new BlockMiner(activationConfig).findNonce(mergedMiningBlock, targetDifficulty);
// We need to clone to allow modifications
Block newBlock = new BlockFactory(activationConfig).cloneBlockForModification(blockGenerator.createChildBlock(parent, txPerBlock, parent.getDifficulty().asBigInteger().longValue()));
newBlock.setBitcoinMergedMiningHeader(mergedMiningBlock.cloneAsHeader().bitcoinSerialize());
byte[] merkleProof = MinerUtils.buildMerkleProof(activationConfig, pb -> pb.buildFromBlock(mergedMiningBlock), newBlock.getNumber());
byte[] additionalTag = Arrays.concatenate(new byte[] { 'A', 'L', 'T', 'B', 'L', 'O', 'C', 'K', ':' }, mergedMiningHash.getBytes());
byte[] mergedMiningTx = org.bouncycastle.util.Arrays.concatenate(compressedTag, mergedMiningHash.getBytes(), additionalTag);
newBlock.setBitcoinMergedMiningCoinbaseTransaction(mergedMiningTx);
newBlock.setBitcoinMergedMiningMerkleProof(merkleProof);
return newBlock;
}
use of org.ethereum.core.BlockFactory in project rskj by rsksmart.
the class ConnectBlocks method onExecute.
@Override
protected void onExecute(@Nonnull String[] args, @Nonnull RskContext ctx) throws Exception {
BlockFactory blockFactory = ctx.getBlockFactory();
Blockchain blockchain = ctx.getBlockchain();
TrieStore trieStore = ctx.getTrieStore();
BlockStore blockStore = ctx.getBlockStore();
ReceiptStore receiptStore = ctx.getReceiptStore();
String filename = args[0];
long startTime = System.currentTimeMillis();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
connectBlocks(blockFactory, blockchain, trieStore, blockStore, receiptStore, reader);
}
long endTime = System.currentTimeMillis();
printInfo("Duration: " + (endTime - startTime) + " millis");
}
use of org.ethereum.core.BlockFactory in project rskj by rsksmart.
the class GitHubBasicTest method runDifficultyHomesteadTest.
@Test
public void runDifficultyHomesteadTest() throws IOException, ParseException {
BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
String json = JSONReader.loadJSONFromCommit("BasicTests/difficultyHomestead.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));
}
}
Aggregations