Search in sources :

Example 1 with BlockFactory

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);
    }
}
Also used : DifficultyTestCase(org.ethereum.jsontestsuite.DifficultyTestCase) BlockDifficulty(co.rsk.core.BlockDifficulty) DifficultyCalculator(co.rsk.core.DifficultyCalculator) BlockFactory(org.ethereum.core.BlockFactory) BlockHeader(org.ethereum.core.BlockHeader) DifficultyTestSuite(org.ethereum.jsontestsuite.DifficultyTestSuite)

Example 2 with BlockFactory

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());
}
Also used : Path(java.nio.file.Path) BlockStore(org.ethereum.db.BlockStore) BootstrapDataProvider(co.rsk.db.importer.provider.BootstrapDataProvider) BlockFactory(org.ethereum.core.BlockFactory) TrieStore(co.rsk.trie.TrieStore) Test(org.junit.Test)

Example 3 with BlockFactory

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;
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BigInteger(java.math.BigInteger) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockMiner(co.rsk.blockchain.utils.BlockMiner) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator)

Example 4 with BlockFactory

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");
}
Also used : BlockStore(org.ethereum.db.BlockStore) BlockFactory(org.ethereum.core.BlockFactory) Blockchain(org.ethereum.core.Blockchain) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) TrieStore(co.rsk.trie.TrieStore) ReceiptStore(org.ethereum.db.ReceiptStore)

Example 5 with BlockFactory

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));
    }
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

BlockFactory (org.ethereum.core.BlockFactory)20 Test (org.junit.Test)7 Block (org.ethereum.core.Block)6 TestSystemProperties (co.rsk.config.TestSystemProperties)4 BlockHeader (org.ethereum.core.BlockHeader)4 Blockchain (org.ethereum.core.Blockchain)4 Before (org.junit.Before)4 RskContext (co.rsk.RskContext)3 DifficultyCalculator (co.rsk.core.DifficultyCalculator)3 Keccak256 (co.rsk.crypto.Keccak256)3 World (co.rsk.test.World)3 NodeStopper (co.rsk.util.NodeStopper)3 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)3 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)3 HashMapDB (org.ethereum.datasource.HashMapDB)3 BlockStore (org.ethereum.db.BlockStore)3 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 BlockMiner (co.rsk.blockchain.utils.BlockMiner)2 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)2 PeersInformation (co.rsk.net.sync.PeersInformation)2