Search in sources :

Example 11 with EthereumImpl

use of org.ethereum.facade.EthereumImpl in project rskj by rsksmart.

the class MinerServerTest method submitBitcoinBlockTwoTags.

@Test
public void submitBitcoinBlockTwoTags() {
    EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
    Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
    BlockUnclesValidationRule unclesValidationRule = Mockito.mock(BlockUnclesValidationRule.class);
    Mockito.when(unclesValidationRule.isValid(Mockito.any())).thenReturn(true);
    MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), blockchain.getRepository(), blockchain.getBlockStore(), blockchain.getTransactionPool(), DIFFICULTY_CALCULATOR, new GasLimitCalculator(config), unclesValidationRule, config, null), ConfigUtils.getDefaultMiningConfig());
    try {
        byte[] extraData = ByteBuffer.allocate(4).putInt(1).array();
        minerServer.setExtraData(extraData);
        minerServer.start();
        MinerWork work = minerServer.getWork();
        Block bestBlock = blockchain.getBestBlock();
        extraData = ByteBuffer.allocate(4).putInt(2).array();
        minerServer.setExtraData(extraData);
        minerServer.buildBlockToMine(bestBlock, false);
        // only the tag is used
        MinerWork work2 = minerServer.getWork();
        Assert.assertNotEquals(work2.getBlockHashForMergedMining(), work.getBlockHashForMergedMining());
        BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlockWithTwoTags(work, work2);
        findNonce(work, bitcoinMergedMiningBlock);
        SubmitBlockResult result;
        result = ((MinerServerImpl) minerServer).submitBitcoinBlock(work2.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, true);
        Assert.assertEquals("OK", result.getStatus());
        Assert.assertNotNull(result.getBlockInfo());
        Assert.assertEquals("0x1", result.getBlockInfo().getBlockIncludedHeight());
        Assert.assertEquals("0x494d504f525445445f42455354", result.getBlockInfo().getBlockImportedResult());
        // Submit again the save PoW for a different header
        result = ((MinerServerImpl) minerServer).submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, false);
        Assert.assertEquals("ERROR", result.getStatus());
        Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
    } finally {
        minerServer.stop();
    }
}
Also used : BlockUnclesValidationRule(co.rsk.validators.BlockUnclesValidationRule) EthereumImpl(org.ethereum.facade.EthereumImpl) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 12 with EthereumImpl

use of org.ethereum.facade.EthereumImpl in project rskj by rsksmart.

the class MinerServerTest method submitBitcoinBlockTransactionsWhenBlockHasTransactions.

@Test
public void submitBitcoinBlockTransactionsWhenBlockHasTransactions() {
    EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
    Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
    BlockUnclesValidationRule unclesValidationRule = Mockito.mock(BlockUnclesValidationRule.class);
    Mockito.when(unclesValidationRule.isValid(Mockito.any())).thenReturn(true);
    MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), blockchain.getRepository(), blockchain.getBlockStore(), blockchain.getTransactionPool(), DIFFICULTY_CALCULATOR, new GasLimitCalculator(config), unclesValidationRule, config, null), ConfigUtils.getDefaultMiningConfig());
    try {
        minerServer.start();
        MinerWork work = minerServer.getWork();
        BtcTransaction otherTx = Mockito.mock(BtcTransaction.class);
        Sha256Hash otherTxHash = Sha256Hash.wrap("aaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccdddd");
        Mockito.when(otherTx.getHash()).thenReturn(otherTxHash);
        Mockito.when(otherTx.getHashAsString()).thenReturn(otherTxHash.toString());
        BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work, Collections.singletonList(otherTx));
        findNonce(work, bitcoinMergedMiningBlock);
        // noinspection ConstantConditions
        BtcTransaction coinbase = bitcoinMergedMiningBlock.getTransactions().get(0);
        List<String> txs = Arrays.asList(coinbase.getHashAsString(), otherTxHash.toString());
        SubmitBlockResult result = minerServer.submitBitcoinBlockTransactions(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, coinbase, txs);
        Assert.assertEquals("OK", result.getStatus());
        Assert.assertNotNull(result.getBlockInfo());
        Assert.assertEquals("0x1", result.getBlockInfo().getBlockIncludedHeight());
        Assert.assertEquals("0x494d504f525445445f42455354", result.getBlockInfo().getBlockImportedResult());
        Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
    } finally {
        minerServer.stop();
    }
}
Also used : EthereumImpl(org.ethereum.facade.EthereumImpl) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockUnclesValidationRule(co.rsk.validators.BlockUnclesValidationRule) Test(org.junit.Test)

Example 13 with EthereumImpl

use of org.ethereum.facade.EthereumImpl in project rskj by rsksmart.

the class MinerServerTest method secondBuildBlockToMineTurnsNotifyFlagOff.

@Test
public void secondBuildBlockToMineTurnsNotifyFlagOff() {
    EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
    BlockUnclesValidationRule unclesValidationRule = Mockito.mock(BlockUnclesValidationRule.class);
    Mockito.when(unclesValidationRule.isValid(Mockito.any())).thenReturn(true);
    MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, this.blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), blockchain.getRepository(), blockchain.getBlockStore(), blockchain.getTransactionPool(), DIFFICULTY_CALCULATOR, new GasLimitCalculator(config), unclesValidationRule, config, null), ConfigUtils.getDefaultMiningConfig());
    try {
        minerServer.start();
        MinerWork work = minerServer.getWork();
        String hashForMergedMining = work.getBlockHashForMergedMining();
        minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
        work = minerServer.getWork();
        assertEquals(hashForMergedMining, work.getBlockHashForMergedMining());
        assertEquals(false, work.getNotify());
    } finally {
        minerServer.stop();
    }
}
Also used : BlockUnclesValidationRule(co.rsk.validators.BlockUnclesValidationRule) EthereumImpl(org.ethereum.facade.EthereumImpl) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 14 with EthereumImpl

use of org.ethereum.facade.EthereumImpl in project rskj by rsksmart.

the class MainNetMinerTest method submitBitcoinBlockProofOfWorkNotGoodEnough.

/*
     * This test is probabilistic, but it has a really high chance to pass. We will generate
     * a random block that it is unlikely to pass the Long.MAX_VALUE difficulty, though
     * it may happen once. Twice would be suspicious.
     */
@Test
public void submitBitcoinBlockProofOfWorkNotGoodEnough() {
    /* We need a low target */
    BlockChainImpl bc = new BlockChainBuilder().build();
    Genesis gen = (Genesis) BlockChainImplTest.getGenesisBlock(bc);
    gen.getHeader().setDifficulty(new BlockDifficulty(BigInteger.valueOf(Long.MAX_VALUE)));
    bc.setStatus(gen, gen.getCumulativeDifficulty());
    World world = new World(bc, gen);
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), world.getBlockChain().getBlockStore(), null, null, null, 10, 100);
    blockchain = world.getBlockChain();
    EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
    MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), blockToMineBuilder(), ConfigUtils.getDefaultMiningConfig());
    try {
        minerServer.start();
        MinerWork work = minerServer.getWork();
        co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work);
        bitcoinMergedMiningBlock.setNonce(2);
        SubmitBlockResult result = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
        Assert.assertEquals("ERROR", result.getStatus());
        Assert.assertNull(result.getBlockInfo());
        Mockito.verify(ethereumImpl, Mockito.times(0)).addNewMinedBlock(Mockito.any());
    } finally {
        minerServer.stop();
    }
}
Also used : TransactionPool(org.ethereum.core.TransactionPool) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) EthereumImpl(org.ethereum.facade.EthereumImpl) World(co.rsk.test.World) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) Genesis(org.ethereum.core.Genesis) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Aggregations

ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)14 EthereumImpl (org.ethereum.facade.EthereumImpl)14 Test (org.junit.Test)14 BlockUnclesValidationRule (co.rsk.validators.BlockUnclesValidationRule)11 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)3 BlockDifficulty (co.rsk.core.BlockDifficulty)2 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)2 World (co.rsk.test.World)2 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)2 Genesis (org.ethereum.core.Genesis)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)1 Keccak256 (co.rsk.crypto.Keccak256)1 RemascTransaction (co.rsk.remasc.RemascTransaction)1 File (java.io.File)1 IOException (java.io.IOException)1 BlockchainNetConfig (org.ethereum.config.BlockchainNetConfig)1 Constants (org.ethereum.config.Constants)1 TransactionPool (org.ethereum.core.TransactionPool)1 ECKey (org.ethereum.crypto.ECKey)1