Search in sources :

Example 51 with Blockchain

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

the class MinerManagerTest method refreshWorkRunOnce.

@Test
public void refreshWorkRunOnce() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    MinerServerImpl minerServer = getMinerServer(blockchain);
    MinerClientImpl minerClient = getMinerClient(minerServer);
    MinerClientImpl.RefreshWork refreshWork = minerClient.createRefreshWork();
    Assert.assertNotNull(refreshWork);
    try {
        minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
        refreshWork.run();
        Assert.assertTrue(minerClient.mineBlock());
        Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    } finally {
        refreshWork.cancel();
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) Test(org.junit.Test)

Example 52 with Blockchain

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

the class BlockSyncServiceTest method sendBlockMessageAndAddItToBlockchainWithCommonAncestors.

@Test
public void sendBlockMessageAndAddItToBlockchainWithCommonAncestors() {
    Blockchain blockchain = BlockChainBuilder.ofSize(10);
    BlockStore store = new BlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    Block initialBestBlock = blockchain.getBestBlock();
    Assert.assertEquals(10, initialBestBlock.getNumber());
    Block branchingPoint = blockchain.getBlockByNumber(7);
    BlockGenerator blockGenerator = new BlockGenerator();
    List<Block> extendedChain = blockGenerator.getBlockChain(branchingPoint, 10, 1000000l);
    // we have just surpassed the best branch
    for (int i = 0; i < extendedChain.size(); i++) {
        Block newBestBlock = extendedChain.get(i);
        blockSyncService.processBlock(newBestBlock, null, false);
        Assert.assertEquals(newBestBlock.getNumber(), blockchain.getBestBlock().getNumber());
        Assert.assertEquals(newBestBlock.getHash(), blockchain.getBestBlock().getHash());
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 53 with Blockchain

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

the class BlockSyncServiceTest method sendBlockMessagesAndAddThemToBlockchain.

@Test
public void sendBlockMessagesAndAddThemToBlockchain() {
    for (int i = 0; i < 50; i += 5) {
        Blockchain blockchain = BlockChainBuilder.ofSize(10 * i);
        BlockStore store = new BlockStore();
        BlockNodeInformation nodeInformation = new BlockNodeInformation();
        RskSystemProperties config = new RskSystemProperties();
        BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
        Assert.assertEquals(10 * i, blockchain.getBestBlock().getNumber());
        List<Block> extendedChain = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), i);
        for (Block block : extendedChain) {
            blockSyncService.processBlock(block, null, false);
            Assert.assertEquals(block.getNumber(), blockchain.getBestBlock().getNumber());
            Assert.assertEquals(block.getHash(), blockchain.getBestBlock().getHash());
        }
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 54 with Blockchain

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

the class NodeBlockProcessorTest method processGetBlockMessageUsingEmptyStore.

@Test
public void processGetBlockMessageUsingEmptyStore() throws UnknownHostException {
    final Block block = new BlockGenerator().getBlock(3);
    final BlockStore store = new BlockStore();
    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()).isEmpty());
    Assert.assertTrue(sender.getMessages().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 55 with Blockchain

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

the class NodeBlockProcessorTest method processBlockHashRequestMessageUsingBlockInBlockchain.

@Test
public void processBlockHashRequestMessageUsingBlockInBlockchain() throws UnknownHostException {
    final Blockchain blockchain = BlockChainBuilder.ofSize(10);
    final Block block = blockchain.getBlockByNumber(5);
    final Keccak256 blockHash = block.getHash();
    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();
    Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
    processor.processBlockRequest(sender, 100, 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_RESPONSE_MESSAGE, message.getMessageType());
    final BlockResponseMessage bMessage = (BlockResponseMessage) message;
    Assert.assertEquals(100, bMessage.getId());
    Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) 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