Search in sources :

Example 1 with BlockMiner

use of co.rsk.blockchain.utils.BlockMiner 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 2 with BlockMiner

use of co.rsk.blockchain.utils.BlockMiner in project rskj by rsksmart.

the class ProofOfWorkRuleTest method test_2.

@Ignore
// invalid block
@Test
public void test_2() {
    // mined block
    Block b = new BlockMiner(activationConfig).mineBlock(new BlockGenerator(networkConstants, activationConfig).getBlock(1));
    byte[] mergeMiningHeader = b.getBitcoinMergedMiningHeader();
    // TODO improve, the mutated block header could be still valid
    mergeMiningHeader[0]++;
    b.setBitcoinMergedMiningHeader(mergeMiningHeader);
    assertFalse(rule.isValid(b));
}
Also used : Block(org.ethereum.core.Block) BlockMiner(co.rsk.blockchain.utils.BlockMiner) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Ignore(org.junit.Ignore) ParameterizedNetworkUpgradeTest(co.rsk.mine.ParameterizedNetworkUpgradeTest) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 3 with BlockMiner

use of co.rsk.blockchain.utils.BlockMiner in project rskj by rsksmart.

the class ProofOfWorkRuleTest method test_3.

@Ignore
// stress test
@Test
public void test_3() {
    int iterCnt = 1_000_000;
    // mined block
    Block b = new BlockMiner(activationConfig).mineBlock(new BlockGenerator(networkConstants, activationConfig).getBlock(1));
    long start = System.currentTimeMillis();
    for (int i = 0; i < iterCnt; i++) rule.isValid(b);
    long total = System.currentTimeMillis() - start;
    System.out.println(String.format("Time: total = %d ms, per block = %.2f ms", total, (double) total / iterCnt));
}
Also used : Block(org.ethereum.core.Block) BlockMiner(co.rsk.blockchain.utils.BlockMiner) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Ignore(org.junit.Ignore) ParameterizedNetworkUpgradeTest(co.rsk.mine.ParameterizedNetworkUpgradeTest) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 4 with BlockMiner

use of co.rsk.blockchain.utils.BlockMiner in project rskj by rsksmart.

the class ProofOfWorkRuleTest method test_1.

@Test
public void test_1() {
    // mined block
    Block b = new BlockMiner(activationConfig).mineBlock(new BlockGenerator(networkConstants, activationConfig).getBlock(1));
    assertTrue(rule.isValid(b));
}
Also used : Block(org.ethereum.core.Block) BlockMiner(co.rsk.blockchain.utils.BlockMiner) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ParameterizedNetworkUpgradeTest(co.rsk.mine.ParameterizedNetworkUpgradeTest) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 5 with BlockMiner

use of co.rsk.blockchain.utils.BlockMiner in project rskj by rsksmart.

the class ProofOfWorkRuleTest method bytesAfterMergedMiningHashAreLessThan128.

@Test
public void bytesAfterMergedMiningHashAreLessThan128() {
    RskSystemProperties props = new TestSystemProperties() {

        @Override
        public ActivationConfig getActivationConfig() {
            return ActivationConfigsForTest.all();
        }
    };
    ActivationConfig config = props.getActivationConfig();
    Constants networkConstants = props.getNetworkConstants();
    BlockGenerator blockGenerator = new BlockGenerator(networkConstants, config);
    Block newBlock = blockGenerator.getBlock(1);
    while (newBlock.getNumber() < 455) newBlock = blockGenerator.createChildBlock(newBlock);
    String output1 = "6a24b9e11b6de9aa87561948d72e494fed2fb56bf8fd4193425f9350037f34dec5b13be7a86e";
    String output2 = "aa21a9ed90a5e7d6d8093d20aa54fb01f57da374e016d4a01ddec0210088675e5e3fee4e";
    byte[] mergedMiningLink = org.bouncycastle.util.Arrays.concatenate(RskMiningConstants.RSK_TAG, newBlock.getHashForMergedMining());
    co.rsk.bitcoinj.core.NetworkParameters params = co.rsk.bitcoinj.params.RegTestParams.get();
    co.rsk.bitcoinj.core.BtcTransaction bitcoinMergedMiningCoinbaseTransaction = MinerUtils.getBitcoinCoinbaseTransaction(params, mergedMiningLink);
    bitcoinMergedMiningCoinbaseTransaction.addOutput(new co.rsk.bitcoinj.core.TransactionOutput(params, bitcoinMergedMiningCoinbaseTransaction, co.rsk.bitcoinj.core.Coin.valueOf(0), Hex.decode(output1)));
    bitcoinMergedMiningCoinbaseTransaction.addOutput(new co.rsk.bitcoinj.core.TransactionOutput(params, bitcoinMergedMiningCoinbaseTransaction, co.rsk.bitcoinj.core.Coin.valueOf(0), Hex.decode(output2)));
    Block newBlock1 = new BlockMiner(config).mineBlock(newBlock, bitcoinMergedMiningCoinbaseTransaction);
    ProofOfWorkRule rule = new ProofOfWorkRule(props);
    assertTrue(rule.isValid(newBlock1));
}
Also used : RskMiningConstants(co.rsk.config.RskMiningConstants) Constants(org.ethereum.config.Constants) BlockMiner(co.rsk.blockchain.utils.BlockMiner) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Block(org.ethereum.core.Block) RskSystemProperties(co.rsk.config.RskSystemProperties) TestSystemProperties(co.rsk.config.TestSystemProperties) ParameterizedNetworkUpgradeTest(co.rsk.mine.ParameterizedNetworkUpgradeTest) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Aggregations

BlockMiner (co.rsk.blockchain.utils.BlockMiner)10 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)9 Block (org.ethereum.core.Block)9 ParameterizedNetworkUpgradeTest (co.rsk.mine.ParameterizedNetworkUpgradeTest)6 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 Test (org.junit.Test)6 BigInteger (java.math.BigInteger)4 Keccak256 (co.rsk.crypto.Keccak256)3 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)2 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)2 NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)2 RskMiningConstants (co.rsk.config.RskMiningConstants)2 RskSystemProperties (co.rsk.config.RskSystemProperties)2 TestSystemProperties (co.rsk.config.TestSystemProperties)2 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)2 Constants (org.ethereum.config.Constants)2 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)2 BlockFactory (org.ethereum.core.BlockFactory)2 Ignore (org.junit.Ignore)2 ArrayList (java.util.ArrayList)1