Search in sources :

Example 91 with BlockChainBuilder

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

the class SyncProcessorTest method processSkeletonResponseWithConnectionPoint.

@Test
public void processSkeletonResponseWithConnectionPoint() {
    Blockchain blockchain = new BlockChainBuilder().ofSize(25);
    final 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);
    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 = 25;
    int step = 10;
    int linkCount = 10;
    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);
    Message message = sender.getMessages().get(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 : 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 92 with BlockChainBuilder

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

the class SyncProcessorTest method processStatusWithPeerWithSameDifficulty.

@Test
public void processStatusWithPeerWithSameDifficulty() {
    final NetBlockStore store = new NetBlockStore();
    Blockchain blockchain = new BlockChainBuilder().ofSize(100);
    SimplePeer sender = new SimplePeer(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();
    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, 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), mock(EthereumListener.class));
    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.isSyncing());
}
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 93 with BlockChainBuilder

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

the class SyncProcessorTest method processBlockHeadersResponseRejectsNonSolicitedMessages.

@Test
public void processBlockHeadersResponseRejectsNonSolicitedMessages() {
    Blockchain blockchain = new BlockChainBuilder().ofSize(3);
    Block block = blockchain.getBlockByNumber(2);
    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<>();
    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 : 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 94 with BlockChainBuilder

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

the class SyncProcessorTest method processSkeletonResponseWithoutBlockIdentifiers.

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

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

the class AsyncNodeBlockProcessorTest method noSyncingWithEmptyBlockchain.

@Test
public void noSyncingWithEmptyBlockchain() {
    final NetBlockStore store = new NetBlockStore();
    final Blockchain blockchain = new BlockChainBuilder().ofSize(0);
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    final TestSystemProperties config = new TestSystemProperties();
    final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE);
    Assert.assertFalse(processor.hasBetterBlockToSync());
}
Also used : Blockchain(org.ethereum.core.Blockchain) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) 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