Search in sources :

Example 41 with SimplePeer

use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.

the class AsyncNodeBlockProcessorTest method processBodyRequestMessageUsingBlockInBlockchain.

@Test
public void processBodyRequestMessageUsingBlockInBlockchain() {
    final Blockchain blockchain = new BlockChainBuilder().ofSize(10);
    final Block block = blockchain.getBlockByNumber(3);
    final NetBlockStore store = new NetBlockStore();
    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);
    final SimplePeer sender = new SimplePeer();
    processor.processBodyRequest(sender, 100, block.getHash().getBytes());
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(1, sender.getMessages().size());
    final Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BODY_RESPONSE_MESSAGE, message.getMessageType());
    final BodyResponseMessage bMessage = (BodyResponseMessage) message;
    Assert.assertEquals(100, bMessage.getId());
    Assert.assertEquals(block.getTransactionsList(), bMessage.getTransactions());
    Assert.assertEquals(block.getUncleList(), bMessage.getUncles());
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 42 with SimplePeer

use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.

the class AsyncNodeBlockProcessorTest method processBlockHashRequestMessageUsingBlockInBlockchain.

@Test
public void processBlockHashRequestMessageUsingBlockInBlockchain() {
    final Blockchain blockchain = new BlockChainBuilder().ofSize(10);
    final Block block = blockchain.getBlockByNumber(5);
    final Keccak256 blockHash = block.getHash();
    final NetBlockStore store = new NetBlockStore();
    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);
    final SimplePeer sender = new SimplePeer();
    Assert.assertTrue(nodeInformation.getNodesByBlock(block.getHash()).isEmpty());
    processor.processBlockRequest(sender, 100, block.getHash().getBytes());
    Assert.assertTrue(nodeInformation.getNodesByBlock(block.getHash()).contains(sender.getPeerNodeID()));
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(1, sender.getMessages().size());
    final Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_RESPONSE_MESSAGE, message.getMessageType());
    final BlockResponseMessage bMessage = (BlockResponseMessage) message;
    Assert.assertEquals(100, bMessage.getId());
    Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
Also used : Blockchain(org.ethereum.core.Blockchain) Keccak256(co.rsk.crypto.Keccak256) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) Block(org.ethereum.core.Block) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 43 with SimplePeer

use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.

the class AsyncNodeBlockProcessorTest method processBlockRetrievingParentUsingSender.

@Test
public void processBlockRetrievingParentUsingSender() {
    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);
    final SimplePeer sender = new SimplePeer();
    final BlockGenerator blockGenerator = new BlockGenerator();
    final Block genesis = blockGenerator.getGenesisBlock();
    final Block parent = blockGenerator.createChildBlock(genesis);
    final Block block = blockGenerator.createChildBlock(parent);
    processor.processBlock(sender, block);
    Assert.assertEquals(1, processor.getNodeInformation().getNodesByBlock(block.getHash().getBytes()).size());
    Assert.assertTrue(store.hasBlock(block));
    Assert.assertEquals(1, sender.getMessages().size());
    Assert.assertEquals(1, store.size());
    final Message message = sender.getMessages().get(0);
    Assert.assertNotNull(message);
    Assert.assertEquals(MessageType.GET_BLOCK_MESSAGE, message.getMessageType());
    final GetBlockMessage gbMessage = (GetBlockMessage) message;
    Assert.assertArrayEquals(block.getParentHash().getBytes(), gbMessage.getBlockHash());
}
Also used : Blockchain(org.ethereum.core.Blockchain) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimplePeer(co.rsk.net.simples.SimplePeer) Block(org.ethereum.core.Block) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 44 with SimplePeer

use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.

the class MessageCounterTest method decrement.

@Test
public void decrement() {
    SimplePeer sender = new SimplePeer(new NodeID(new byte[] { 1 }));
    MessageCounter counter = new MessageCounter();
    counter.increment(sender);
    counter.increment(sender);
    counter.decrement(sender);
    Assert.assertEquals(1, counter.getValue(sender));
}
Also used : SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 45 with SimplePeer

use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.

the class MessageCounterTest method givenConcurrentCalls_then_expectCorrectCount.

@Test
public void givenConcurrentCalls_then_expectCorrectCount() throws InterruptedException {
    /**
     *  this queue will be added with a '+' when the counter is incremented
     *  and added with a '-' when the counter is decremented
     */
    ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    SimplePeer sender = new SimplePeer(new NodeID(new byte[] { 1 }));
    MessageCounter counter = new MessageCounter();
    Thread inc1 = new Thread(new Incrementer(counter, sender, executor, queue));
    Thread inc2 = new Thread(new Incrementer(counter, sender, executor, queue));
    Thread inc3 = new Thread(new Incrementer(counter, sender, executor, queue));
    inc1.start();
    inc2.start();
    inc3.start();
    inc1.join();
    inc2.join();
    inc3.join();
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    int changeCount = countConcurrency(queue);
    // assert we had more than a 100 proofs of concurrency
    Assert.assertThat(changeCount, greaterThan(100));
    // counter must be zero at this point
    Assert.assertEquals(0, counter.getValue(sender));
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Aggregations

SimplePeer (co.rsk.net.simples.SimplePeer)109 Test (org.junit.Test)108 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)81 TestSystemProperties (co.rsk.config.TestSystemProperties)68 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)54 Blockchain (org.ethereum.core.Blockchain)49 Block (org.ethereum.core.Block)46 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)41 BlockStore (org.ethereum.db.BlockStore)28 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)26 EthereumListener (org.ethereum.listener.EthereumListener)26 SimpleBlockProcessor (co.rsk.net.simples.SimpleBlockProcessor)19 PeerScoringManager (co.rsk.scoring.PeerScoringManager)16 Keccak256 (co.rsk.crypto.Keccak256)13 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)13 ChannelManager (org.ethereum.net.server.ChannelManager)11 PeerScoring (co.rsk.scoring.PeerScoring)10 World (co.rsk.test.World)8 Ignore (org.junit.Ignore)7 BlockBuilder (co.rsk.test.builders.BlockBuilder)6