Search in sources :

Example 86 with TestSystemProperties

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

the class ChannelManagerImplTest method blockAddressIsNotAvailable.

@Test
public void blockAddressIsNotAvailable() {
    TestSystemProperties config = mock(TestSystemProperties.class);
    when(config.maxConnectionsAllowed()).thenReturn(1);
    when(config.networkCIDR()).thenReturn(32);
    SyncPool syncPool = mock(SyncPool.class);
    ChannelManagerImpl channelManager = new ChannelManagerImpl(config, syncPool);
    String remoteId = "remoteId";
    NodeManager nodeManager = new NodeManager(null, config);
    Channel peer = spy(new Channel(null, null, nodeManager, null, null, null, remoteId));
    peer.setInetSocketAddress(new InetSocketAddress("127.0.0.1", 5554));
    peer.setNode(new NodeID(HashUtil.randomPeerId()).getID());
    when(peer.isProtocolsInitialized()).thenReturn(true);
    when(peer.isActive()).thenReturn(true);
    when(peer.isUsingNewProtocol()).thenReturn(true);
    Channel otherPeer = new Channel(null, null, nodeManager, null, null, null, remoteId);
    otherPeer.setInetSocketAddress(new InetSocketAddress("127.0.0.1", 5554));
    channelManager.add(peer);
    channelManager.tryProcessNewPeers();
    Assert.assertFalse(channelManager.isAddressBlockAvailable(otherPeer.getInetSocketAddress().getAddress()));
}
Also used : NodeManager(org.ethereum.net.NodeManager) SyncPool(org.ethereum.sync.SyncPool) InetSocketAddress(java.net.InetSocketAddress) NodeID(co.rsk.net.NodeID) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 87 with TestSystemProperties

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

the class ChannelManagerImplTest method getNumberOfPeersToSendStatusTo.

@Test
public void getNumberOfPeersToSendStatusTo() {
    ChannelManagerImpl channelManagerImpl = new ChannelManagerImpl(new TestSystemProperties(), null);
    ;
    assertEquals(1, channelManagerImpl.getNumberOfPeersToSendStatusTo(1));
    assertEquals(2, channelManagerImpl.getNumberOfPeersToSendStatusTo(2));
    assertEquals(3, channelManagerImpl.getNumberOfPeersToSendStatusTo(3));
    assertEquals(3, channelManagerImpl.getNumberOfPeersToSendStatusTo(5));
    assertEquals(3, channelManagerImpl.getNumberOfPeersToSendStatusTo(9));
    assertEquals(3, channelManagerImpl.getNumberOfPeersToSendStatusTo(12));
    assertEquals(4, channelManagerImpl.getNumberOfPeersToSendStatusTo(20));
    assertEquals(5, channelManagerImpl.getNumberOfPeersToSendStatusTo(25));
    assertEquals(10, channelManagerImpl.getNumberOfPeersToSendStatusTo(1000));
}
Also used : TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 88 with TestSystemProperties

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

the class SyncProcessorTest method syncWithAdvancedStatusAnd5Peers.

@Test
public void syncWithAdvancedStatusAnd5Peers() {
    final NetBlockStore store = new NetBlockStore();
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.ofSize(0);
    BlockStore blockStore = builder.getBlockStore();
    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();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final ChannelManager channelManager = mock(ChannelManager.class);
    SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.DEFAULT, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.DEFAULT, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    List<SimplePeer> senders = new ArrayList<>();
    int lessPeers = SyncConfiguration.DEFAULT.getExpectedPeers() - 1;
    for (int i = 0; i < lessPeers; i++) {
        SimplePeer sender = new SimplePeer();
        senders.add(sender);
        when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    }
    Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
    senders.forEach(s -> processor.processStatus(s, status));
    Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
    Assert.assertEquals(lessPeers, processor.getNoAdvancedPeers());
    Set<NodeID> knownPeersNodeIDs = processor.getKnownPeersNodeIDs();
    Assert.assertTrue(senders.stream().map(SimplePeer::getPeerNodeID).allMatch(knownPeersNodeIDs::contains));
    SimplePeer lastSender = new SimplePeer();
    Assert.assertFalse(processor.getKnownPeersNodeIDs().contains(lastSender.getPeerNodeID()));
    processor.processStatus(lastSender, status);
    // now test with all senders
    senders.add(lastSender);
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertTrue(ids.contains(lastSender.getPeerNodeID()));
    Assert.assertFalse(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
    Assert.assertEquals(1, senders.stream().map(SimplePeer::getMessages).mapToInt(List::size).sum());
    Message message = senders.stream().map(SimplePeer::getMessages).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 : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) java.util(java.util) StatusUtils(co.rsk.net.utils.StatusUtils) StateRootHandler(co.rsk.db.StateRootHandler) HashMapDB(org.ethereum.datasource.HashMapDB) Keccak256(co.rsk.crypto.Keccak256) Hex(org.bouncycastle.util.encoders.Hex) co.rsk.core(co.rsk.core) PeerScoringManager(co.rsk.scoring.PeerScoringManager) Duration(java.time.Duration) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) ChannelManager(org.ethereum.net.server.ChannelManager) co.rsk.net.sync(co.rsk.net.sync) RepositoryLocator(co.rsk.db.RepositoryLocator) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) co.rsk.net.messages(co.rsk.net.messages) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) BlockExecutor(co.rsk.core.bc.BlockExecutor) BlockStore(org.ethereum.db.BlockStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) EthereumListener(org.ethereum.listener.EthereumListener) SimplePeer(co.rsk.net.simples.SimplePeer) StateRootsStoreImpl(co.rsk.db.StateRootsStoreImpl) Mockito(org.mockito.Mockito) RskMockFactory(org.ethereum.util.RskMockFactory) TestSystemProperties(co.rsk.config.TestSystemProperties) ByteUtil(org.ethereum.util.ByteUtil) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) co.rsk.validators(co.rsk.validators) Assert(org.junit.Assert) org.ethereum.core(org.ethereum.core) ECKey(org.ethereum.crypto.ECKey) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) EthereumListener(org.ethereum.listener.EthereumListener) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockStore(org.ethereum.db.BlockStore) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 89 with TestSystemProperties

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

the class SyncProcessorTest method findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks.

@Test
public void findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks() {
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.ofSize(0);
    Blockchain advancedBlockchain = new BlockChainBuilder().ofSize(100);
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    NetBlockStore store = new NetBlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    SyncProcessor processor = new SyncProcessor(blockchain, builder.getBlockStore(), mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new DummyBlockValidationRule(), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    processor.processStatus(sender, StatusUtils.fromBlockchain(advancedBlockchain));
    Assert.assertFalse(sender.getMessages().isEmpty());
    List<Message> messages = sender.getMessages();
    BlockHeadersRequestMessage requestMessage = (BlockHeadersRequestMessage) messages.get(0);
    processor.processBlockHeadersResponse(sender, new BlockHeadersResponseMessage(requestMessage.getId(), Collections.singletonList(advancedBlockchain.getBestBlock().getHeader())));
    long[] expectedHeights = new long[] { 50, 25, 12, 6, 3, 1 };
    for (int k = 0; k < expectedHeights.length; k++) {
        Assert.assertEquals(k + 2, messages.size());
        Message message = messages.get(k + 1);
        Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
        BlockHashRequestMessage request = (BlockHashRequestMessage) message;
        long requestId = request.getId();
        Assert.assertEquals(expectedHeights[k], request.getHeight());
        Block block = advancedBlockchain.getBlockByNumber(expectedHeights[k]);
        processor.processBlockHashResponse(sender, new BlockHashResponseMessage(requestId, block.getHash().getBytes()));
    }
    Assert.assertEquals(expectedHeights.length + 2, messages.size());
    Message message = messages.get(messages.size() - 1);
    Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
    SkeletonRequestMessage request = (SkeletonRequestMessage) message;
    Assert.assertEquals(0, request.getStartNumber());
    Assert.assertEquals(1, processor.getExpectedResponses().size());
}
Also used : EthereumListener(org.ethereum.listener.EthereumListener) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 90 with TestSystemProperties

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

the class SyncProcessorTest method doesntProcessInvalidBodyResponse.

@Test
public void doesntProcessInvalidBodyResponse() {
    final NetBlockStore store = new NetBlockStore();
    Blockchain blockchain = new BlockChainBuilder().ofSize(10);
    SimplePeer sender = new SimplePeer(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();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final EthereumListener listener = mock(EthereumListener.class);
    SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), listener);
    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, bids), sender);
    ((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.isSyncing());
}
Also used : EthereumListener(org.ethereum.listener.EthereumListener) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockStore(org.ethereum.db.BlockStore) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Aggregations

TestSystemProperties (co.rsk.config.TestSystemProperties)158 Test (org.junit.Test)130 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)109 Blockchain (org.ethereum.core.Blockchain)78 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)75 SimplePeer (co.rsk.net.simples.SimplePeer)69 Block (org.ethereum.core.Block)69 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)56 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)23 EthereumListener (org.ethereum.listener.EthereumListener)23 BlockStore (org.ethereum.db.BlockStore)21 RskSystemProperties (co.rsk.config.RskSystemProperties)17 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)16 Keccak256 (co.rsk.crypto.Keccak256)13 RepositoryLocator (co.rsk.db.RepositoryLocator)10 World (co.rsk.test.World)10 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)9 Ignore (org.junit.Ignore)9 TransactionExecutorFactory (co.rsk.core.TransactionExecutorFactory)8 AsyncNodeBlockProcessorListener (co.rsk.net.utils.AsyncNodeBlockProcessorListener)8