Search in sources :

Example 6 with BlockChainBuilder

use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.

the class SyncProcessorTest method syncEventsSentToListener.

@Test
public void syncEventsSentToListener() {
    final NetBlockStore store = new NetBlockStore();
    Blockchain blockchain = new BlockChainBuilder().ofSize(10);
    SimplePeer peer = new SimplePeer(new byte[] { 0x01 });
    BlockStore blockStore = mock(BlockStore.class);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    PeersInformation peersInformation = spy(new PeersInformation(getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()));
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    EthereumListener listener = mock(EthereumListener.class);
    SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, peersInformation, mock(Genesis.class), listener);
    peersInformation.registerPeer(peer);
    Block block = mock(Block.class);
    doReturn(1L).when(block).getNumber();
    doReturn(Keccak256.ZERO_HASH).when(block).getHash();
    doReturn(block).when(blockStore).getBestBlock();
    doReturn(Optional.of(peer)).when(peersInformation).getBestPeer();
    SyncPeerStatus peerStatus = mock(SyncPeerStatus.class);
    Status status = mock(Status.class);
    doReturn(status).when(peerStatus).getStatus();
    doReturn(peerStatus).when(peersInformation).getPeer(eq(peer));
    processor.getSyncState().newPeerStatus();
    verify(listener).onLongSyncStarted();
    doReturn(1L).when(blockStore).getMinNumber();
    doReturn(block).when(blockStore).getChainBlockByNumber(anyLong());
    processor.stopSyncing();
    processor.getSyncState().newPeerStatus();
    verify(listener).onLongSyncDone();
}
Also used : EthereumListener(org.ethereum.listener.EthereumListener) BlockStore(org.ethereum.db.BlockStore) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 7 with BlockChainBuilder

use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.

the class SyncProcessorTest method processBlockHeadersResponseWithManyHeadersMissingFirstParent.

@Test
public void processBlockHeadersResponseWithManyHeadersMissingFirstParent() {
    Blockchain blockchain = new BlockChainBuilder().ofSize(0);
    Blockchain otherBlockchain = new BlockChainBuilder().ofSize(10, true);
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, new NetBlockStore(), blockchain, new BlockNodeInformation(), syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
    SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), blockSyncService, syncConfiguration, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(getChannelManager(), syncConfiguration, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    List<BlockHeader> headers = new ArrayList<>();
    for (int k = 10; k >= 2; k--) headers.add(otherBlockchain.getBlockByNumber(k).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 : EthereumListener(org.ethereum.listener.EthereumListener) 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)

Example 8 with BlockChainBuilder

use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.

the class SyncProcessorTest method syncWithAdvancedPeerAfterTimeoutWaitingPeers.

@Test
public void syncWithAdvancedPeerAfterTimeoutWaitingPeers() {
    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);
    SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, syncConfiguration, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, syncConfiguration, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    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()));
    List<Message> messages = sender.getMessages();
    Assert.assertTrue(messages.isEmpty());
    processor.onTimePassed(syncConfiguration.getTimeoutWaitingPeers().dividedBy(2));
    Assert.assertTrue(messages.isEmpty());
    processor.onTimePassed(syncConfiguration.getTimeoutWaitingPeers().dividedBy(2));
    Assert.assertFalse(messages.isEmpty());
    Assert.assertEquals(1, messages.size());
    Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
Also used : 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 9 with BlockChainBuilder

use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.

the class SyncProcessorTest method processSkeletonResponseWithTenBlockIdentifiers.

@Test
public void processSkeletonResponseWithTenBlockIdentifiers() {
    final NetBlockStore store = new NetBlockStore();
    Blockchain blockchain = new BlockChainBuilder().ofSize(0);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    Peer channel = mock(Peer.class);
    when(channel.getPeerNodeID()).thenReturn(sender.getPeerNodeID());
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
    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(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    int connectionPoint = 0;
    int step = 10;
    int linkCount = 9;
    processor.startDownloadingSkeleton(connectionPoint, sender);
    List<BlockIdentifier> blockIdentifiers = buildSkeleton(blockchain, connectionPoint, step, linkCount);
    SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
    processor.registerExpectedMessage(response);
    processor.processSkeletonResponse(sender, response);
    Assert.assertFalse(sender.getMessages().isEmpty());
    Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertArrayEquals(blockIdentifiers.get(1).getHash(), request.getHash());
    Assert.assertEquals(10, request.getCount());
    DownloadingHeadersSyncState syncState = (DownloadingHeadersSyncState) processor.getSyncState();
    List<BlockIdentifier> skeleton = syncState.getSkeleton();
    Assert.assertEquals(10, skeleton.size());
    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) BlockStore(org.ethereum.db.BlockStore) SimplePeer(co.rsk.net.simples.SimplePeer) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 10 with BlockChainBuilder

use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.

the class SyncProcessorTest method noPeers.

@Test
public void noPeers() {
    final NetBlockStore store = new NetBlockStore();
    Blockchain blockchain = new BlockChainBuilder().ofSize(0);
    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, getPeerScoringManager()), mock(Genesis.class), listener);
    Assert.assertEquals(0, processor.getPeersCount());
    Assert.assertEquals(0, processor.getNoAdvancedPeers());
    Assert.assertTrue(processor.getKnownPeersNodeIDs().isEmpty());
    verify(listener, never()).onLongSyncStarted();
    verify(listener, never()).onLongSyncDone();
}
Also used : EthereumListener(org.ethereum.listener.EthereumListener) BlockStore(org.ethereum.db.BlockStore) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Aggregations

BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)155 Test (org.junit.Test)139 TestSystemProperties (co.rsk.config.TestSystemProperties)109 Blockchain (org.ethereum.core.Blockchain)88 SimplePeer (co.rsk.net.simples.SimplePeer)82 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)76 Block (org.ethereum.core.Block)69 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)62 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)25 BlockStore (org.ethereum.db.BlockStore)25 EthereumListener (org.ethereum.listener.EthereumListener)25 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)19 Ignore (org.junit.Ignore)17 RskSystemProperties (co.rsk.config.RskSystemProperties)15 Keccak256 (co.rsk.crypto.Keccak256)14 SimpleAsyncNode (co.rsk.net.simples.SimpleAsyncNode)11 AsyncNodeBlockProcessorListener (co.rsk.net.utils.AsyncNodeBlockProcessorListener)9 org.ethereum.core (org.ethereum.core)9 ECKey (org.ethereum.crypto.ECKey)9 ChannelManager (org.ethereum.net.server.ChannelManager)9