Search in sources :

Example 26 with NetworkParameters

use of co.rsk.bitcoinj.core.NetworkParameters 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 27 with NetworkParameters

use of co.rsk.bitcoinj.core.NetworkParameters in project rskj by rsksmart.

the class Web3RskImpl method mnr_submitBitcoinBlock.

public SubmittedBlockInfo mnr_submitBitcoinBlock(String bitcoinBlockHex) {
    logger.debug("mnr_submitBitcoinBlock(): {}", bitcoinBlockHex.length());
    NetworkParameters params = RegTestParams.get();
    new Context(params);
    BtcBlock bitcoinBlock = getBtcBlock(bitcoinBlockHex, params);
    BtcTransaction coinbase = bitcoinBlock.getTransactions().get(0);
    String blockHashForMergedMining = extractBlockHashForMergedMining(coinbase);
    SubmitBlockResult result = minerServer.submitBitcoinBlock(blockHashForMergedMining, bitcoinBlock);
    return parseResultAndReturn(result);
}
Also used : Context(co.rsk.bitcoinj.core.Context) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 28 with NetworkParameters

use of co.rsk.bitcoinj.core.NetworkParameters in project rskj by rsksmart.

the class ReleaseBtcTest method releaseBtc.

@Test
public void releaseBtc() throws IOException {
    int minCentsBtc = 5;
    int maxCentsBtc = 100;
    final NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
    BridgeStorageProviderInitializer storageInitializer = (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        ReleaseRequestQueue queue;
        try {
            queue = provider.getReleaseRequestQueue();
        } catch (Exception e) {
            throw new RuntimeException("Unable to gather release request queue");
        }
        for (int i = 0; i < Helper.randomInRange(10, 100); i++) {
            Coin value = Coin.CENT.multiply(Helper.randomInRange(minCentsBtc, maxCentsBtc));
            queue.add(new BtcECKey().toAddress(parameters), value);
        }
    };
    final byte[] releaseBtcEncoded = Bridge.RELEASE_BTC.encode();
    ABIEncoder abiEncoder = (int executionIndex) -> releaseBtcEncoded;
    TxBuilder txBuilder = (int executionIndex) -> {
        long satoshis = Coin.CENT.multiply(Helper.randomInRange(minCentsBtc, maxCentsBtc)).getValue();
        BigInteger weis = Denomination.satoshisToWeis(BigInteger.valueOf(satoshis));
        ECKey sender = new ECKey();
        return Helper.buildSendValueTx(sender, weis);
    };
    ExecutionStats stats = new ExecutionStats("releaseBtc");
    executeAndAverage("releaseBtc", 1000, abiEncoder, storageInitializer, txBuilder, Helper.getRandomHeightProvider(10), stats);
    BridgePerformanceTest.addStats(stats);
}
Also used : NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ECKey(org.ethereum.crypto.ECKey) ReleaseRequestQueue(co.rsk.peg.ReleaseRequestQueue) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) IOException(java.io.IOException) Coin(co.rsk.bitcoinj.core.Coin) Repository(org.ethereum.core.Repository) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Aggregations

NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)28 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)11 Test (org.junit.Test)9 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)8 Context (co.rsk.bitcoinj.core.Context)7 BigInteger (java.math.BigInteger)6 Address (co.rsk.bitcoinj.core.Address)5 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)5 ECKey (org.ethereum.crypto.ECKey)5 DeterministicKey (co.rsk.bitcoinj.crypto.DeterministicKey)4 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)3 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)3 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 RskAddress (co.rsk.core.RskAddress)3 Keccak256 (co.rsk.crypto.Keccak256)3 SubmitBlockResult (co.rsk.mine.SubmitBlockResult)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)3 CallTransaction (org.ethereum.core.CallTransaction)3