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