use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class SyncProcessorTest method processBlockHeadersResponseWithEmptyList.
@Test
public void processBlockHeadersResponseWithEmptyList() {
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<BlockHeader> headers = new ArrayList<>();
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 processBlockResponseAddsToBlockchain.
@Test
public void processBlockResponseAddsToBlockchain() {
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));
BlockResponseMessage response = new BlockResponseMessage(new Random().nextLong(), block);
processor.registerExpectedMessage(response);
processor.processBlockResponse(sender, response);
Assert.assertEquals(11, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class SyncProcessorTest method processBodyResponseRejectsNonSolicitedMessages.
@Test
public void processBodyResponseRejectsNonSolicitedMessages() {
Blockchain blockchain = new BlockChainBuilder().ofSize(3);
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));
BodyResponseMessage response = new BodyResponseMessage(new Random().nextLong(), null, null);
processor.registerExpectedMessage(response);
processor.processBodyResponse(sender, response);
Assert.assertTrue(sender.getMessages().isEmpty());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class SyncProcessorTest method findConnectionPointBlockchainWith30BlocksVsBlockchainWith100Blocks.
@Test
public void findConnectionPointBlockchainWith30BlocksVsBlockchainWith100Blocks() {
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.ofSize(30);
org.ethereum.db.BlockStore blockStore = builder.getBlockStore();
Blockchain advancedBlockchain = BlockChainBuilder.copyAndExtend(blockchain, 70);
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
final ChannelManager channelManager = mock(ChannelManager.class);
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
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);
SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new DummyBlockValidationRule(), 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));
Status status = StatusUtils.fromBlockchain(advancedBlockchain);
processor.processStatus(sender, status);
List<Message> messages = sender.getMessages();
BlockHeadersRequestMessage requestMessage = (BlockHeadersRequestMessage) messages.get(0);
processor.processBlockHeadersResponse(sender, new BlockHeadersResponseMessage(requestMessage.getId(), Collections.singletonList(advancedBlockchain.getBestBlock().getHeader())));
long[] binarySearchHeights = new long[] { 50, 25, 37, 31, 28, 29, 30, 30 };
for (int k = 0; k < binarySearchHeights.length; k++) {
Assert.assertEquals(k + 2, messages.size());
Message message = messages.get(k + 1);
Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
BlockHashRequestMessage request = (BlockHashRequestMessage) message;
long requestId = request.getId();
Assert.assertEquals(binarySearchHeights[k], request.getHeight());
Block block = advancedBlockchain.getBlockByNumber(binarySearchHeights[k]);
processor.processBlockHashResponse(sender, new BlockHashResponseMessage(requestId, block.getHash().getBytes()));
}
Assert.assertEquals(binarySearchHeights.length + 2, messages.size());
Message message = messages.get(messages.size() - 1);
Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
SkeletonRequestMessage request = (SkeletonRequestMessage) message;
Assert.assertEquals(30, request.getStartNumber());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class SyncProcessorTest method processBodyResponseWithTransactionAddsToBlockchain.
@Test
public void processBodyResponseWithTransactionAddsToBlockchain() {
Account senderAccount = createAccount("sender");
Account receiverAccount = createAccount("receiver");
Map<RskAddress, AccountState> accounts = new HashMap<>();
accounts.put(senderAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.valueOf(20000000)));
accounts.put(receiverAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.ZERO));
final NetBlockStore store = new NetBlockStore();
BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
Blockchain blockchain = blockChainBuilder.ofSize(0, false, accounts);
Block genesis = blockchain.getBestBlock();
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
List<Transaction> txs = Collections.singletonList(createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO));
Block block = new BlockGenerator().createChildBlock(genesis, txs, blockChainBuilder.getRepository().getRoot());
StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(blockChainBuilder.getTrieStore(), stateRootHandler), new TransactionExecutorFactory(config, blockChainBuilder.getBlockStore(), null, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
Assert.assertEquals(1, block.getTransactionsList().size());
blockExecutor.executeAndFillAll(block, genesis.getHeader());
Assert.assertEquals(21000, block.getFeesPaidToMiner().asBigInteger().intValueExact());
Assert.assertEquals(1, block.getTransactionsList().size());
Assert.assertEquals(1, 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 = block.getTransactionsList();
List<BlockHeader> uncles = block.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(1, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Aggregations