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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations