use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.
the class SyncProcessorTest method dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers.
@Test
public void dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers() {
final NetBlockStore store = new NetBlockStore();
Blockchain blockchain = new BlockChainBuilder().ofSize(0);
byte[] hash = HashUtil.randomHash();
byte[] parentHash = HashUtil.randomHash();
Status status = new Status(0, hash, parentHash, blockchain.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.DEFAULT, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(getChannelManager(), SyncConfiguration.DEFAULT, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
processor.processStatus(sender, status);
Assert.assertEquals(1, processor.getPeersCount());
Assert.assertEquals(0, processor.getNoAdvancedPeers());
Set<NodeID> ids = processor.getKnownPeersNodeIDs();
Assert.assertFalse(ids.isEmpty());
Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
Assert.assertTrue(sender.getMessages().isEmpty());
processor.onTimePassed(Duration.ofMinutes(1));
Assert.assertTrue(sender.getMessages().isEmpty());
processor.onTimePassed(Duration.ofMinutes(1));
Assert.assertTrue(sender.getMessages().isEmpty());
}
use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.
the class SyncProcessorTest method processBodyResponseAddsToBlockchain.
@Test
public void processBodyResponseAddsToBlockchain() {
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);
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));
List<Transaction> transactions = blockchain.getBestBlock().getTransactionsList();
List<BlockHeader> uncles = blockchain.getBestBlock().getUncleList();
long lastRequestId = new Random().nextLong();
BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
processor.registerExpectedMessage(response);
Deque<BlockHeader> headerStack = new ArrayDeque<>();
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(11, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
use of co.rsk.config.TestSystemProperties in project rskj by rsksmart.
the class SyncProcessorTest method processStatusWithAdvancedPeers.
@Test
public void processStatusWithAdvancedPeers() {
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);
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.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.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.assertFalse(messages.isEmpty());
Assert.assertEquals(1, messages.size());
Message message = messages.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.config.TestSystemProperties 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.config.TestSystemProperties 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());
}
Aggregations