Search in sources :

Example 31 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule 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 32 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule 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 33 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule 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 34 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.

the class NodeMessageHandlerTest method processBlockByHashRequestMessageUsingProcessor.

@Test
public void processBlockByHashRequestMessageUsingProcessor() throws UnknownHostException {
    SimpleBlockProcessor sbp = new SimpleBlockProcessor();
    NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    Block block = BlockChainBuilder.ofSize(1, true).getBestBlock();
    Message message = new BlockRequestMessage(100, block.getHash().getBytes());
    processor.processMessage(new SimpleMessageChannel(), message);
    Assert.assertEquals(100, sbp.getRequestId());
    Assert.assertArrayEquals(block.getHash().getBytes(), sbp.getHash());
}
Also used : SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 35 with ProofOfWorkRule

use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.

the class NodeMessageHandlerTest method processStatusMessageUsingNodeBlockProcessor.

@Test
@Ignore("This should be reviewed with sync processor or deleted")
public void processStatusMessageUsingNodeBlockProcessor() throws UnknownHostException {
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    final BlockStore store = new BlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final SimpleMessageChannel sender = new SimpleMessageChannel();
    final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    BlockGenerator blockGenerator = new BlockGenerator();
    final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
    final Status status = new Status(block.getNumber(), block.getHash().getBytes());
    final Message message = new StatusMessage(status);
    handler.processMessage(sender, message);
    Assert.assertNotNull(sender.getGetBlockMessages());
    Assert.assertEquals(1, sender.getGetBlockMessages().size());
    final Message msg = sender.getGetBlockMessages().get(0);
    Assert.assertEquals(MessageType.GET_BLOCK_MESSAGE, msg.getMessageType());
    final GetBlockMessage gbMessage = (GetBlockMessage) msg;
    Assert.assertArrayEquals(block.getHash().getBytes(), gbMessage.getBlockHash());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)61 Test (org.junit.Test)57 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)40 RskSystemProperties (co.rsk.config.RskSystemProperties)21 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)16 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)16 SimpleBlockProcessor (co.rsk.net.simples.SimpleBlockProcessor)14 EthereumImpl (org.ethereum.facade.EthereumImpl)14 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)13 World (co.rsk.test.World)11 BlockUnclesValidationRule (co.rsk.validators.BlockUnclesValidationRule)11 PeerScoringManager (co.rsk.scoring.PeerScoringManager)9 ChannelManager (org.ethereum.net.server.ChannelManager)9 Channel (org.ethereum.net.server.Channel)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 TxHandler (co.rsk.net.handler.TxHandler)7 PeerScoring (co.rsk.scoring.PeerScoring)7 BlockDifficulty (co.rsk.core.BlockDifficulty)6 SimpleTransactionPool (co.rsk.net.simples.SimpleTransactionPool)5 DownloadingBodiesSyncState (co.rsk.net.sync.DownloadingBodiesSyncState)4