use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class SyncProcessorTest method sendSkeletonRequest.
@Test
public void sendSkeletonRequest() {
Blockchain blockchain = new BlockChainBuilder().ofSize(100);
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), null, 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));
processor.sendSkeletonRequest(sender, 0);
Assert.assertFalse(sender.getMessages().isEmpty());
Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
SkeletonRequestMessage request = (SkeletonRequestMessage) message;
Assert.assertNotEquals(0, request.getId());
Assert.assertEquals(0, request.getStartNumber());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
use of co.rsk.net.simples.SimplePeer 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();
}
use of co.rsk.net.simples.SimplePeer 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());
}
use of co.rsk.net.simples.SimplePeer 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());
}
use of co.rsk.net.simples.SimplePeer 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());
}
Aggregations