Search in sources :

Example 56 with ProofOfWorkRule

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

the class SyncProcessorTest method doesntProcessInvalidBodyResponse.

@Test
public void doesntProcessInvalidBodyResponse() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(10);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
    Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
    Assert.assertEquals(11, block.getNumber());
    Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    List<Transaction> transactions = blockchain.getBestBlock().getTransactionsList();
    List<BlockHeader> uncles = blockchain.getBestBlock().getUncleList();
    Account senderAccount = createAccount("sender");
    Account receiverAccount = createAccount("receiver");
    Transaction tx = createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    long lastRequestId = new Random().nextLong();
    BodyResponseMessage response = new BodyResponseMessage(lastRequestId, txs, uncles);
    processor.registerExpectedMessage(response);
    Deque<BlockHeader> headerStack = new ArrayDeque<>();
    headerStack.add(block.getHeader());
    headerStack.add(block.getHeader());
    headerStack.add(block.getHeader());
    List<Deque<BlockHeader>> headers = new ArrayList<>();
    headers.add(headerStack);
    List<BlockIdentifier> bids = new ArrayList<>();
    bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
    bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
    processor.startDownloadingBodies(headers, Collections.singletonMap(sender.getPeerNodeID(), bids));
    ((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
    processor.processBodyResponse(sender, response);
    Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
    Assert.assertNotEquals(block.getNumber(), blockchain.getBestBlock().getNumber());
    // if an unexpected body arrives then stops syncing
    Assert.assertFalse(processor.getSyncState().isSyncing());
}
Also used : BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) DownloadingBodiesSyncState(co.rsk.net.sync.DownloadingBodiesSyncState) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 57 with ProofOfWorkRule

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

the class SyncProcessorTest method processBlockHeadersResponseRejectsNonSolicitedMessages.

@Test
public void processBlockHeadersResponseRejectsNonSolicitedMessages() {
    Blockchain blockchain = BlockChainBuilder.ofSize(3);
    Block block = blockchain.getBlockByNumber(2);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, new BlockStore(), blockchain, new BlockNodeInformation(), syncConfiguration);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), syncConfiguration, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    List<BlockHeader> headers = new ArrayList<>();
    headers.add(block.getHeader());
    BlockHeadersResponseMessage response = new BlockHeadersResponseMessage(new Random().nextLong(), headers);
    processor.registerExpectedMessage(response);
    processor.processBlockHeadersResponse(sender, response);
    Assert.assertEquals(0, sender.getMessages().size());
    Assert.assertEquals(0, processor.getExpectedResponses().size());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 58 with ProofOfWorkRule

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

the class SyncProcessorTest method processSkeletonResponseWithConnectionPoint.

@Test
public void processSkeletonResponseWithConnectionPoint() {
    Blockchain blockchain = BlockChainBuilder.ofSize(25);
    final BlockStore store = new BlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
    final Message[] msg = new Message[1];
    when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
        msg[0] = invocation.getArgumentAt(1, Message.class);
        return true;
    });
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    int connectionPoint = 25;
    int step = 10;
    int linkCount = 10;
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), connectionPoint);
    processor.startDownloadingSkeleton(connectionPoint);
    List<BlockIdentifier> blockIdentifiers = buildSkeleton(blockchain, connectionPoint, step, linkCount);
    SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
    processor.registerExpectedMessage(response);
    processor.processSkeletonResponse(sender, response);
    Message message = msg[0];
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(5, request.getCount());
    Assert.assertArrayEquals(blockIdentifiers.get(1).getHash(), request.getHash());
    Assert.assertEquals(1, processor.getExpectedResponses().size());
}
Also used : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 59 with ProofOfWorkRule

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

the class BlockValidatorTest method invalidPOWUncles.

@Test
public void invalidPOWUncles() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockGenerator blockGenerator = new BlockGenerator();
    Blockchain blockchain = BlockChainBuilder.ofSize(30, true);
    Block genesis = blockchain.getBlockByNumber(0);
    Block uncle1a = blockchain.getBlockByNumber(1);
    List<BlockHeader> uncles1 = new ArrayList<>();
    uncles1.add(uncle1a.getHeader());
    Block block1 = blockGenerator.createChildBlock(genesis, null, uncles1, 1, null);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    store.saveBlock(uncle1a, TEST_DIFFICULTY, false);
    BlockParentDependantValidationRule parentValidationRule = Mockito.mock(BlockParentDependantValidationRule.class);
    Mockito.when(parentValidationRule.isValid(Mockito.any(), Mockito.any())).thenReturn(true);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addBlockUnclesValidationRule(store, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), parentValidationRule).blockStore(store).build();
    Assert.assertFalse(validator.isValid(block1));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) SimpleBlock(co.rsk.peg.simples.SimpleBlock) BlockParentDependantValidationRule(co.rsk.validators.BlockParentDependantValidationRule) Test(org.junit.Test)

Example 60 with ProofOfWorkRule

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