Search in sources :

Example 76 with Blockchain

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

the class ThreeAsyncNodeUsingSyncProcessorTest method synchronizeWithTwoPeers200AndOneFails.

@Test
public void synchronizeWithTwoPeers200AndOneFails() {
    Blockchain b1 = BlockChainBuilder.ofSize(200, true);
    Blockchain b2 = BlockChainBuilder.ofSize(0, true);
    SimpleAsyncNode node1 = SimpleAsyncNode.createDefaultNode(b1);
    SimpleAsyncNode node2 = SimpleAsyncNode.createDefaultNode(b1);
    SyncConfiguration syncConfiguration = new SyncConfiguration(2, 1, 0, 1, 20, 192);
    SimpleAsyncNode node3 = SimpleAsyncNode.createNode(b2, syncConfiguration);
    Assert.assertEquals(200, node1.getBestBlock().getNumber());
    Assert.assertEquals(200, node2.getBestBlock().getNumber());
    Assert.assertEquals(0, node3.getBestBlock().getNumber());
    node1.sendFullStatusTo(node3);
    node2.sendFullStatusTo(node3);
    // sync setup
    int setupRequests = SyncUtils.syncSetupRequests(200, 0, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    node3.waitUntilNTasksWithTimeout(setupRequests);
    node3.waitUntilNTasksWithTimeout(5);
    // synchronize 200 (extra tasks are from old sync protocol messages)
    BodyResponseMessage response = new BodyResponseMessage(new Random().nextLong(), null, null);
    node3.getSyncProcessor().registerExpectedMessage(response);
    node3.getSyncProcessor().processBodyResponse(node1.getMessageChannel(node3), response);
    node3.waitExactlyNTasksWithTimeout(200 + setupRequests - 15);
    Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertTrue(node3.getSyncProcessor().getExpectedResponses().isEmpty());
    Assert.assertEquals(200, node3.getBestBlock().getNumber());
    Assert.assertEquals(node1.getBestBlock().getHash(), node3.getBestBlock().getHash());
    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 : BodyResponseMessage(co.rsk.net.messages.BodyResponseMessage) SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Random(java.util.Random) Blockchain(org.ethereum.core.Blockchain) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 77 with Blockchain

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

the class TwoAsyncNodeTest method createNodeWithUncles.

private static SimpleAsyncNode createNodeWithUncles(int size) {
    final World world = new World();
    final BlockStore store = new BlockStore();
    final Blockchain blockchain = world.getBlockChain();
    List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), size, 0, true);
    for (Block b : blocks) blockchain.tryToConnect(b);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    NodeMessageHandler handler = new NodeMessageHandler(config, processor, null, null, null, null, null, new DummyBlockValidationRule());
    return new SimpleAsyncNode(handler);
}
Also used : DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Block(org.ethereum.core.Block) SyncConfiguration(co.rsk.net.sync.SyncConfiguration)

Example 78 with Blockchain

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

the class TwoNodeTest method createNode.

private static SimpleNode createNode(int size) {
    final World world = new World();
    final BlockStore store = new BlockStore();
    final Blockchain blockchain = world.getBlockChain();
    List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), size);
    for (Block b : blocks) blockchain.tryToConnect(b);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    NodeMessageHandler handler = new NodeMessageHandler(new RskSystemProperties(), processor, null, null, null, null, null, new DummyBlockValidationRule());
    return new SimpleNode(handler);
}
Also used : DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimpleNode(co.rsk.net.simples.SimpleNode) Block(org.ethereum.core.Block) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration)

Example 79 with Blockchain

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

the class SimpleAsyncNode method createNodeWithWorldBlockChain.

public static SimpleAsyncNode createNodeWithWorldBlockChain(int size, boolean withUncles, boolean mining) {
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    BlockChainBuilder.extend(blockchain, size, withUncles, mining);
    return createNode(blockchain, SyncConfiguration.IMMEDIATE_FOR_TESTING);
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World)

Example 80 with Blockchain

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

the class SelectionRuleTest method addBlockTest.

@Test
public void addBlockTest() {
    Blockchain blockchain = createBlockchain();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block lowDifficultyBlock = blockGenerator.createChildBlock(blockchain.getBestBlock(), 0, 1);
    Block highDifficultyBlock = blockGenerator.createChildBlock(lowDifficultyBlock, 0, 5);
    Block highDifficultyBlockWithMoreFees = blockGenerator.createChildBlock(lowDifficultyBlock, 10L, new ArrayList<>(), highDifficultyBlock.getDifficulty().getBytes());
    // diff test
    assertFalse(SelectionRule.shouldWeAddThisBlock(lowDifficultyBlock.getDifficulty(), highDifficultyBlock.getDifficulty(), lowDifficultyBlock, highDifficultyBlock));
    assertTrue(SelectionRule.shouldWeAddThisBlock(highDifficultyBlock.getDifficulty(), lowDifficultyBlock.getDifficulty(), highDifficultyBlock, lowDifficultyBlock));
    // At same difficulty, more fees
    assertTrue(SelectionRule.shouldWeAddThisBlock(highDifficultyBlockWithMoreFees.getDifficulty(), highDifficultyBlock.getDifficulty(), highDifficultyBlockWithMoreFees, highDifficultyBlock));
// Low hash is proved in smallerBlockHashTest
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) 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