Search in sources :

Example 6 with BlockMiner

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

the class EnvironmentUtils method mineBlockWithCoinbaseTransaction.

private static Block mineBlockWithCoinbaseTransaction(TestSystemProperties config, Block parent) {
    BlockGenerator blockGenerator = new BlockGenerator(config.getNetworkConstants(), config.getActivationConfig());
    byte[] prefix = new byte[1000];
    byte[] compressedTag = org.bouncycastle.util.Arrays.concatenate(prefix, RskMiningConstants.RSK_TAG);
    Keccak256 blockMergedMiningHash = new Keccak256(parent.getHashForMergedMining());
    co.rsk.bitcoinj.core.NetworkParameters bitcoinNetworkParameters = co.rsk.bitcoinj.params.RegTestParams.get();
    co.rsk.bitcoinj.core.BtcTransaction bitcoinMergedMiningCoinbaseTransaction = MinerUtils.getBitcoinMergedMiningCoinbaseTransaction(bitcoinNetworkParameters, blockMergedMiningHash.getBytes());
    co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = MinerUtils.getBitcoinMergedMiningBlock(bitcoinNetworkParameters, bitcoinMergedMiningCoinbaseTransaction);
    BigInteger targetBI = DifficultyUtils.difficultyToTarget(parent.getDifficulty());
    new BlockMiner(config.getActivationConfig()).findNonce(bitcoinMergedMiningBlock, targetBI);
    // We need to clone to allow modifications
    Block newBlock = new BlockFactory(config.getActivationConfig()).cloneBlockForModification(blockGenerator.createChildBlock(parent, new ArrayList<>(), new ArrayList<>(), parent.getDifficulty().asBigInteger().longValue(), MIN_GAS_PRICE, parent.getGasLimit()));
    newBlock.setBitcoinMergedMiningHeader(bitcoinMergedMiningBlock.cloneAsHeader().bitcoinSerialize());
    byte[] merkleProof = MinerUtils.buildMerkleProof(config.getActivationConfig(), pb -> pb.buildFromBlock(bitcoinMergedMiningBlock), newBlock.getNumber());
    byte[] additionalTag = Arrays.concatenate(ADDITIONAL_TAG, blockMergedMiningHash.getBytes());
    byte[] mergedMiningTx = org.bouncycastle.util.Arrays.concatenate(compressedTag, blockMergedMiningHash.getBytes(), additionalTag);
    newBlock.setBitcoinMergedMiningCoinbaseTransaction(mergedMiningTx);
    newBlock.setBitcoinMergedMiningMerkleProof(merkleProof);
    return newBlock;
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) ArrayList(java.util.ArrayList) Keccak256(co.rsk.crypto.Keccak256) BlockMiner(co.rsk.blockchain.utils.BlockMiner) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block)

Example 7 with BlockMiner

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

the class BlockHeaderContractTest method mineBlock.

private Block mineBlock(Block parent, RskAddress coinbase) {
    NetworkParameters networkParameters = RegTestParams.get();
    BlockGenerator blockGenerator = new BlockGenerator(config.getNetworkConstants(), config.getActivationConfig());
    Block childBlock = blockGenerator.createChildBlock(parent, new ArrayList<>(), new ArrayList<>(), parent.getDifficulty().asBigInteger().longValue(), MIN_GAS_PRICE, parent.getGasLimit(), coinbase);
    Block newBlock = blockFactory.cloneBlockForModification(childBlock);
    byte[] prefix = new byte[1000];
    byte[] compressedTag = Arrays.concatenate(prefix, RskMiningConstants.RSK_TAG);
    byte[] mergedMiningHash = childBlock.getHashForMergedMining();
    BtcTransaction mergedMiningCoinbaseTransaction = MinerUtils.getBitcoinMergedMiningCoinbaseTransaction(networkParameters, mergedMiningHash);
    BtcBlock mergedMiningBlock = MinerUtils.getBitcoinMergedMiningBlock(networkParameters, mergedMiningCoinbaseTransaction);
    BigInteger targetDifficulty = DifficultyUtils.difficultyToTarget(parent.getDifficulty());
    new BlockMiner(config.getActivationConfig()).findNonce(mergedMiningBlock, targetDifficulty);
    newBlock.setBitcoinMergedMiningHeader(mergedMiningBlock.cloneAsHeader().bitcoinSerialize());
    byte[] merkleProof = MinerUtils.buildMerkleProof(config.getActivationConfig(), pb -> pb.buildFromBlock(mergedMiningBlock), newBlock.getNumber());
    newBlock.setBitcoinMergedMiningMerkleProof(merkleProof);
    byte[] additionalTag = Arrays.concatenate(ADDITIONAL_TAG, mergedMiningHash);
    byte[] mergedMiningTx = org.bouncycastle.util.Arrays.concatenate(compressedTag, mergedMiningHash, additionalTag);
    newBlock.setBitcoinMergedMiningCoinbaseTransaction(mergedMiningTx);
    return newBlock;
}
Also used : NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BigInteger(java.math.BigInteger) BlockMiner(co.rsk.blockchain.utils.BlockMiner) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator)

Example 8 with BlockMiner

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

the class ProofOfWorkRuleTest method bytesAfterMergedMiningHashAreMoreThan128.

@Test
public void bytesAfterMergedMiningHashAreMoreThan128() {
    // This test shows that a Mining Pools can not add more than 2 outputs with 36 bytes each,
    // otherwise solutions will not be taken as valid.
    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);
    // fork detection data is for heights > 449
    while (newBlock.getNumber() < 455) newBlock = blockGenerator.createChildBlock(newBlock);
    String output1 = "6a24b9e11b6de9aa87561948d72e494fed2fb56bf8fd4193425f9350037f34dec5b13be7";
    String output2 = "aa21a9ed90a5e7d6d8093d20aa54fb01f57da374e016d4a01ddec0210088675e5e3fee4e";
    String output3 = "1111a9ed90a5e7d6d8093d20aa54fb01f57da374e016d4a01ddec0210088675e5e3fee4e";
    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 coinbaseTransaction = MinerUtils.getBitcoinCoinbaseTransaction(params, mergedMiningLink);
    coinbaseTransaction.addOutput(new co.rsk.bitcoinj.core.TransactionOutput(params, coinbaseTransaction, co.rsk.bitcoinj.core.Coin.valueOf(0), Hex.decode(output1)));
    coinbaseTransaction.addOutput(new co.rsk.bitcoinj.core.TransactionOutput(params, coinbaseTransaction, co.rsk.bitcoinj.core.Coin.valueOf(0), Hex.decode(output2)));
    coinbaseTransaction.addOutput(new co.rsk.bitcoinj.core.TransactionOutput(params, coinbaseTransaction, co.rsk.bitcoinj.core.Coin.valueOf(0), Hex.decode(output3)));
    Block block = new BlockMiner(config).mineBlock(newBlock, coinbaseTransaction);
    ProofOfWorkRule rule = new ProofOfWorkRule(props);
    assertFalse(rule.isValid(block));
}
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)

Example 9 with BlockMiner

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

the class ProofOfWorkRuleTest method mineBlockWithCoinbaseTransactionWithCompressedCoinbaseTransactionPrefix.

private Block mineBlockWithCoinbaseTransactionWithCompressedCoinbaseTransactionPrefix(Block block, byte[] compressed, ActivationConfig activationConfig) {
    Keccak256 blockMergedMiningHash = new Keccak256(block.getHashForMergedMining());
    co.rsk.bitcoinj.core.NetworkParameters bitcoinNetworkParameters = co.rsk.bitcoinj.params.RegTestParams.get();
    co.rsk.bitcoinj.core.BtcTransaction bitcoinMergedMiningCoinbaseTransaction = MinerUtils.getBitcoinMergedMiningCoinbaseTransaction(bitcoinNetworkParameters, blockMergedMiningHash.getBytes());
    co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = MinerUtils.getBitcoinMergedMiningBlock(bitcoinNetworkParameters, bitcoinMergedMiningCoinbaseTransaction);
    BigInteger targetBI = DifficultyUtils.difficultyToTarget(block.getDifficulty());
    new BlockMiner(activationConfig).findNonce(bitcoinMergedMiningBlock, targetBI);
    Block newBlock = blockFactory.cloneBlockForModification(block);
    newBlock.setBitcoinMergedMiningHeader(bitcoinMergedMiningBlock.cloneAsHeader().bitcoinSerialize());
    byte[] merkleProof = MinerUtils.buildMerkleProof(activationConfig, pb -> pb.buildFromBlock(bitcoinMergedMiningBlock), newBlock.getNumber());
    newBlock.setBitcoinMergedMiningCoinbaseTransaction(org.bouncycastle.util.Arrays.concatenate(compressed, blockMergedMiningHash.getBytes()));
    newBlock.setBitcoinMergedMiningMerkleProof(merkleProof);
    return newBlock;
}
Also used : BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockMiner(co.rsk.blockchain.utils.BlockMiner)

Example 10 with BlockMiner

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

the class ProofOfWorkRuleTest method test_RLPEncoding.

// This test must be moved to the appropiate place
@Test
public void test_RLPEncoding() {
    // mined block
    Block b = new BlockMiner(activationConfig).mineBlock(new BlockGenerator(networkConstants, activationConfig).getBlock(1));
    // last field
    byte[] lastField = b.getBitcoinMergedMiningCoinbaseTransaction();
    // force re-encode
    b.flushRLP();
    byte[] encoded = b.getEncoded();
    Block b2 = blockFactory.decodeBlock(encoded);
    // last field
    byte[] lastField2 = b2.getBitcoinMergedMiningCoinbaseTransaction();
    // force re-encode
    b2.flushRLP();
    byte[] encoded2 = b2.getEncoded();
    Assert.assertTrue(Arrays.equals(encoded, encoded2));
    Assert.assertTrue(Arrays.equals(lastField, lastField2));
}
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)

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