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