Search in sources :

Example 81 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers.

@Test
public void dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(0, hash, parentHash, blockchain.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.DEFAULT, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    processor.processStatus(sender, status);
    Assert.assertEquals(1, processor.getPeersCount());
    Assert.assertEquals(0, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertFalse(ids.isEmpty());
    Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
    Assert.assertTrue(sender.getMessages().isEmpty());
    processor.onTimePassed(Duration.ofMinutes(1));
    Assert.assertTrue(sender.getMessages().isEmpty());
    processor.onTimePassed(Duration.ofMinutes(1));
    Assert.assertTrue(sender.getMessages().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) RskSystemProperties(co.rsk.config.RskSystemProperties) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 82 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method processSkeletonResponseWithoutBlockIdentifiers.

@Test
public void processSkeletonResponseWithoutBlockIdentifiers() {
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    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<BlockIdentifier> blockIdentifiers = new ArrayList<>();
    SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
    processor.registerExpectedMessage(response);
    processor.processSkeletonResponse(sender, response);
    Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
    Assert.assertTrue(sender.getMessages().isEmpty());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
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 83 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class SyncProcessorTest method processStatusWithAdvancedPeers.

@Test
public void processStatusWithAdvancedPeers() {
    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);
    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 List<Message> messages = new ArrayList<>();
    when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
        messages.add(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);
    processor.processStatus(sender, status);
    Assert.assertEquals(1, processor.getPeersCount());
    Assert.assertEquals(1, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertFalse(ids.isEmpty());
    Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
    Assert.assertFalse(messages.isEmpty());
    Assert.assertEquals(1, messages.size());
    Message message = messages.get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
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) BlockDifficulty(co.rsk.core.BlockDifficulty) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 84 with RskSystemProperties

use of co.rsk.config.RskSystemProperties 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 85 with RskSystemProperties

use of co.rsk.config.RskSystemProperties 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)

Aggregations

RskSystemProperties (co.rsk.config.RskSystemProperties)112 Test (org.junit.Test)81 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)48 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)45 Blockchain (org.ethereum.core.Blockchain)45 Block (org.ethereum.core.Block)41 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)35 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)20 Ignore (org.junit.Ignore)10 Keccak256 (co.rsk.crypto.Keccak256)8 RegTestConfig (org.ethereum.config.blockchain.RegTestConfig)8 Repository (org.ethereum.core.Repository)8 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)8 Channel (org.ethereum.net.server.Channel)7 ChannelManager (org.ethereum.net.server.ChannelManager)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 BlockDifficulty (co.rsk.core.BlockDifficulty)6 RskAddress (co.rsk.core.RskAddress)6 PersonalModule (co.rsk.rpc.modules.personal.PersonalModule)6 BeforeClass (org.junit.BeforeClass)6