Search in sources :

Example 26 with Blockchain

use of org.ethereum.core.Blockchain in project rskj by rsksmart.

the class NodeBlockProcessorTest method processGetBlockMessageUsingBlockInStore.

@Test
public void processGetBlockMessageUsingBlockInStore() throws UnknownHostException {
    final Block block = new BlockGenerator().getBlock(3);
    final Keccak256 blockHash = block.getHash();
    final BlockStore store = new BlockStore();
    store.saveBlock(block);
    final Blockchain blockchain = BlockChainBuilder.ofSize(0);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final SimpleMessageChannel sender = new SimpleMessageChannel();
    Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
    processor.processGetBlock(sender, block.getHash().getBytes());
    Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(1, sender.getMessages().size());
    final Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_MESSAGE, message.getMessageType());
    final BlockMessage bMessage = (BlockMessage) message;
    Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Blockchain(org.ethereum.core.Blockchain) Keccak256(co.rsk.crypto.Keccak256) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Block(org.ethereum.core.Block) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 27 with Blockchain

use of org.ethereum.core.Blockchain in project rskj by rsksmart.

the class NodeBlockProcessorTest method processGetBlockHeaderMessageUsingBlockInBlockchain.

@Test
public void processGetBlockHeaderMessageUsingBlockInBlockchain() throws UnknownHostException {
    final Blockchain blockchain = BlockChainBuilder.ofSize(10);
    final Block block = blockchain.getBlockByNumber(5);
    final BlockStore store = new BlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final SimpleMessageChannel sender = new SimpleMessageChannel();
    processor.processBlockHeadersRequest(sender, 1, block.getHash().getBytes(), 1);
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(1, sender.getMessages().size());
    final Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_RESPONSE_MESSAGE, message.getMessageType());
    final BlockHeadersResponseMessage bMessage = (BlockHeadersResponseMessage) message;
    Assert.assertEquals(block.getHeader().getHash(), bMessage.getBlockHeaders().get(0).getHash());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 28 with Blockchain

use of org.ethereum.core.Blockchain in project rskj by rsksmart.

the class NodeMessageHandlerUtil method createHandler.

public static NodeMessageHandler createHandler(BlockValidationRule validationRule) {
    final World world = new World();
    final BlockStore store = new BlockStore();
    final Blockchain blockchain = world.getBlockChain();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    SyncProcessor syncProcessor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), RskMockFactory.getChannelManager(), syncConfiguration, new DummyBlockValidationRule(), DIFFICULTY_CALCULATOR);
    NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    return new NodeMessageHandler(config, processor, syncProcessor, new SimpleChannelManager(), null, null, RskMockFactory.getPeerScoringManager(), validationRule);
}
Also used : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) SyncConfiguration(co.rsk.net.sync.SyncConfiguration)

Example 29 with Blockchain

use of org.ethereum.core.Blockchain in project rskj by rsksmart.

the class NodeMessageHandlerUtil method createHandlerWithSyncProcessor.

public static NodeMessageHandler createHandlerWithSyncProcessor(SyncConfiguration syncConfiguration, ChannelManager channelManager) {
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    return createHandlerWithSyncProcessor(blockchain, syncConfiguration, channelManager);
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World)

Example 30 with Blockchain

use of org.ethereum.core.Blockchain in project rskj by rsksmart.

the class ThreeAsyncNodeUsingSyncProcessorTest method ignoreNewBlockHashesWhenSyncing.

@Test
public void ignoreNewBlockHashesWhenSyncing() {
    Blockchain b1 = BlockChainBuilder.ofSize(30, true);
    Blockchain b2 = BlockChainBuilder.copyAndExtend(b1, 1, true);
    SimpleAsyncNode node1 = SimpleAsyncNode.createNode(b1, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SimpleAsyncNode node2 = SimpleAsyncNode.createNode(b2, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SimpleAsyncNode node3 = SimpleAsyncNode.createNodeWithBlockChainBuilder(0);
    Assert.assertEquals(30, node1.getBestBlock().getNumber());
    Assert.assertEquals(31, node2.getBestBlock().getNumber());
    Assert.assertEquals(0, node3.getBestBlock().getNumber());
    node1.sendFullStatusTo(node3);
    // receive the hash of a better block than node1's best
    // while it's syncing with node1
    node3.receiveMessageFrom(node2, new NewBlockHashMessage(node2.getBestBlock().getHash().getBytes()));
    // receive and ignore NewBlockHashMessage
    node3.waitUntilNTasksWithTimeout(1);
    // sync setup
    node3.waitUntilNTasksWithTimeout(SyncUtils.syncSetupRequests(30, 0, SyncConfiguration.IMMEDIATE_FOR_TESTING));
    // synchronize 30 new blocks from node 1
    node3.waitExactlyNTasksWithTimeout(30);
    Assert.assertEquals(30, node1.getBestBlock().getNumber());
    Assert.assertEquals(31, node2.getBestBlock().getNumber());
    Assert.assertEquals(30, node3.getBestBlock().getNumber());
    Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node2.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node3.getSyncProcessor().getExpectedResponses().isEmpty());
    node1.joinWithTimeout();
    node2.joinWithTimeout();
    node3.joinWithTimeout();
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
    Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node3.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node3.getNodeID()));
    Assert.assertFalse(node3.getSyncProcessor().isPeerSyncing(node1.getNodeID()));
    Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node2.getNodeID()));
}
Also used : SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Blockchain(org.ethereum.core.Blockchain) NewBlockHashMessage(co.rsk.net.messages.NewBlockHashMessage) Test(org.junit.Test)

Aggregations

Blockchain (org.ethereum.core.Blockchain)86 Test (org.junit.Test)75 Block (org.ethereum.core.Block)51 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)47 RskSystemProperties (co.rsk.config.RskSystemProperties)45 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)38 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)25 World (co.rsk.test.World)19 SimpleAsyncNode (co.rsk.net.simples.SimpleAsyncNode)14 Keccak256 (co.rsk.crypto.Keccak256)7 Ignore (org.junit.Ignore)7 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)5 BlockIdentifier (org.ethereum.core.BlockIdentifier)3 BlockChainStatus (co.rsk.core.bc.BlockChainStatus)2 NewBlockHashMessage (co.rsk.net.messages.NewBlockHashMessage)2 PersonalModule (co.rsk.rpc.modules.personal.PersonalModule)2 Repository (org.ethereum.core.Repository)2 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)2 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)1 BlockDifficulty (co.rsk.core.BlockDifficulty)1