Search in sources :

Example 51 with ProofOfWorkRule

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

the class SyncProcessorTest method processBlockHashResponseWithUnknownHash.

@Test(expected = Exception.class)
public void processBlockHashResponseWithUnknownHash() {
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    SyncProcessor processor = new SyncProcessor(config, blockchain, null, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.processStatus(sender, new Status(100, null));
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 52 with ProofOfWorkRule

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

the class SyncProcessorTest method sendBlockHashRequest.

@Test
public void sendBlockHashRequest() {
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    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, null, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
    processor.sendBlockHashRequest(100);
    Message message = msg[0];
    Assert.assertNotNull(message);
    Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
    BlockHashRequestMessage request = (BlockHashRequestMessage) message;
    Assert.assertNotEquals(0, request.getId());
    Assert.assertEquals(100, request.getHeight());
    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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 53 with ProofOfWorkRule

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

the class SyncProcessorTest method processStatusWithPeerWithSameDifficulty.

@Test
public void processStatusWithPeerWithSameDifficulty() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(100);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(blockchain.getStatus().getBestBlockNumber(), hash, parentHash, blockchain.getStatus().getTotalDifficulty());
    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.processStatus(sender, status);
    Assert.assertEquals(1, processor.getPeersCount());
    Assert.assertEquals(0, processor.getNoAdvancedPeers());
    Assert.assertTrue(sender.getMessages().isEmpty());
    // is null when we're not syncing
    Assert.assertEquals(0, processor.getExpectedResponses().size());
    Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) RskSystemProperties(co.rsk.config.RskSystemProperties) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 54 with ProofOfWorkRule

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

the class SyncProcessorTest method processBodyResponseAddsToBlockchain.

@Test
public void processBodyResponseAddsToBlockchain() {
    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();
    long lastRequestId = new Random().nextLong();
    BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
    processor.registerExpectedMessage(response);
    Deque<BlockHeader> headerStack = new ArrayDeque<>();
    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(11, blockchain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
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 55 with ProofOfWorkRule

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

the class SyncProcessorTest method syncWithAdvancedStatusAnd5Peers.

@Test
public void syncWithAdvancedStatusAnd5Peers() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(100, hash, parentHash, blockchain.getTotalDifficulty().add(new BlockDifficulty(BigInteger.TEN)));
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    final ChannelManager channelManager = mock(ChannelManager.class);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.DEFAULT, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    List<SimpleMessageChannel> senders = new ArrayList<>();
    List<Channel> channels = new ArrayList<>();
    Map<NodeID, List<Message>> messagesByNode = new HashMap<>();
    int lessPeers = SyncConfiguration.DEFAULT.getExpectedPeers() - 1;
    for (int i = 0; i < lessPeers; i++) {
        SimpleMessageChannel sender = new SimpleMessageChannel();
        senders.add(sender);
        Channel channel = mock(Channel.class);
        messagesByNode.put(sender.getPeerNodeID(), new ArrayList<>());
        when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
        when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
        when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
            messagesByNode.get(sender.getPeerNodeID()).add(invocation.getArgumentAt(1, Message.class));
            return true;
        });
    }
    messagesByNode.values().forEach(m -> Assert.assertTrue(m.isEmpty()));
    senders.forEach(s -> processor.processStatus(s, status));
    messagesByNode.values().forEach(m -> Assert.assertTrue(m.isEmpty()));
    Assert.assertEquals(lessPeers, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    senders.stream().map(SimpleMessageChannel::getPeerNodeID).forEach(peerId -> Assert.assertTrue(ids.contains(peerId)));
    SimpleMessageChannel lastSender = new SimpleMessageChannel();
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(lastSender.getPeerNodeID());
    Assert.assertFalse(ids.contains(lastSender.getPeerNodeID()));
    processor.processStatus(lastSender, status);
    // now test with all senders
    senders.add(lastSender);
    Assert.assertTrue(ids.contains(lastSender.getPeerNodeID()));
    Assert.assertFalse(messagesByNode.values().stream().allMatch(m -> m.isEmpty()));
    Assert.assertEquals(1, messagesByNode.values().stream().mapToInt(List::size).sum());
    Message message = messagesByNode.values().stream().filter(m -> !m.isEmpty()).findFirst().get().get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) java.util(java.util) StatusUtils(co.rsk.net.utils.StatusUtils) DifficultyCalculator(co.rsk.core.DifficultyCalculator) Hex(org.spongycastle.util.encoders.Hex) Coin(co.rsk.core.Coin) PeerScoringManager(co.rsk.scoring.PeerScoringManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DownloadingHeadersSyncState(co.rsk.net.sync.DownloadingHeadersSyncState) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) BigInteger(java.math.BigInteger) ChannelManager(org.ethereum.net.server.ChannelManager) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) co.rsk.net.messages(co.rsk.net.messages) DownloadingBodiesSyncState(co.rsk.net.sync.DownloadingBodiesSyncState) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) BlockExecutor(co.rsk.core.bc.BlockExecutor) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Mockito.when(org.mockito.Mockito.when) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Matchers.any(org.mockito.Matchers.any) RskMockFactory(org.ethereum.util.RskMockFactory) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Channel(org.ethereum.net.server.Channel) RskSystemProperties(co.rsk.config.RskSystemProperties) Assert(org.junit.Assert) org.ethereum.core(org.ethereum.core) ECKey(org.ethereum.crypto.ECKey) Mockito.mock(org.mockito.Mockito.mock) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) 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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) 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