Search in sources :

Example 1 with Blockchain

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

the class MinerManagerTest method mineBlockWhilePlayingBlocks.

@Test
public void mineBlockWhilePlayingBlocks() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    RskImplForTest rsk = new RskImplForTest() {

        @Override
        public boolean hasBetterBlockToSync() {
            return false;
        }

        @Override
        public boolean isPlayingBlocks() {
            return true;
        }
    };
    MinerServerImpl minerServer = getMinerServer(blockchain);
    MinerClientImpl minerClient = getMinerClient(rsk, minerServer);
    minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
    Assert.assertFalse(minerClient.mineBlock());
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) Test(org.junit.Test)

Example 2 with Blockchain

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

the class MinerManagerTest method doWork.

@Test
public void doWork() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    MinerServerImpl minerServer = getMinerServer(blockchain);
    MinerClientImpl minerClient = getMinerClient(minerServer);
    minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
    minerClient.doWork();
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) Test(org.junit.Test)

Example 3 with Blockchain

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

the class MinerManagerTest method doWorkInThread.

@Test
public void doWorkInThread() throws Exception {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    MinerServerImpl minerServer = getMinerServer(blockchain);
    MinerClientImpl minerClient = getMinerClient(minerServer);
    minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
    Thread thread = minerClient.createDoWorkThread();
    thread.start();
    try {
        Awaitility.await().timeout(Duration.FIVE_SECONDS).until(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return minerClient.isMining();
            }
        });
        Assert.assertTrue(minerClient.isMining());
    } finally {
        // enought ?
        thread.interrupt();
        minerClient.stop();
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) Test(org.junit.Test)

Example 4 with Blockchain

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

the class MinerManagerTest method doWorkEvenWithoutMinerServer.

@Test
public void doWorkEvenWithoutMinerServer() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    MinerServerImpl minerServer = getMinerServer(blockchain);
    MinerClientImpl minerClient = getMinerClient(null);
    minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
    minerClient.doWork();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) Test(org.junit.Test)

Example 5 with Blockchain

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

the class BlockSyncServiceTest method sendBlockMessagesAndAddThemToBlockchainInReverseOrder.

@Test
public void sendBlockMessagesAndAddThemToBlockchainInReverseOrder() {
    for (int i = 1; i < 52; 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());
        Block initialBestBlock = blockchain.getBestBlock();
        List<Block> extendedChain = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), i);
        Collections.reverse(extendedChain);
        for (int j = 0; j < extendedChain.size() - 1; j++) {
            Block block = extendedChain.get(j);
            blockSyncService.processBlock(block, null, false);
            // we don't have all the parents, so we wait to update the best chain
            Assert.assertEquals(initialBestBlock.getNumber(), blockchain.getBestBlock().getNumber());
            Assert.assertEquals(initialBestBlock.getHash(), blockchain.getBestBlock().getHash());
        }
        // the chain is complete, we have a new best block
        Block closingBlock = extendedChain.get(extendedChain.size() - 1);
        Block newBestBlock = extendedChain.get(0);
        blockSyncService.processBlock(closingBlock, 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)

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