Search in sources :

Example 46 with Blockchain

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

the class MinerManagerTest method refreshWorkRunTwice.

@Test
public void refreshWorkRunTwice() {
    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());
        minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
        refreshWork.run();
        Assert.assertTrue(minerClient.mineBlock());
        Assert.assertEquals(2, blockchain.getBestBlock().getNumber());
    } finally {
        refreshWork.cancel();
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) Test(org.junit.Test)

Example 47 with Blockchain

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

the class MinerManagerTest method mineBlockWhileSyncingBlocks.

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

        @Override
        public boolean hasBetterBlockToSync() {
            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 48 with Blockchain

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

the class MinerManagerTest method mineBlockTwiceReusingTheSameWork.

@Test
public void mineBlockTwiceReusingTheSameWork() {
    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);
    MinerWork minerWork = minerServer.getWork();
    Assert.assertNotNull(minerWork);
    Assert.assertTrue(minerClient.mineBlock());
    Block bestBlock = blockchain.getBestBlock();
    Assert.assertNotNull(bestBlock);
    Assert.assertEquals(1, bestBlock.getNumber());
    // reuse the same work
    Assert.assertNotNull(minerServer.getWork());
    Assert.assertTrue(minerClient.mineBlock());
    List<Block> blocks = blockchain.getBlocksByNumber(1);
    Assert.assertNotNull(blocks);
    Assert.assertEquals(2, blocks.size());
    Assert.assertFalse(blocks.get(0).getHash().equals(blocks.get(1).getHash()));
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) World(co.rsk.test.World) Test(org.junit.Test)

Example 49 with Blockchain

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

the class MinerManagerTest method mineBlock.

@Test
public void mineBlock() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    MinerManager manager = new MinerManager();
    MinerServerImpl minerServer = getMinerServer(blockchain);
    MinerClientImpl minerClient = getMinerClient(minerServer);
    manager.mineBlock(blockchain, minerClient, minerServer);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertFalse(blockchain.getBestBlock().getTransactionsList().isEmpty());
    SnapshotManager snapshotManager = new SnapshotManager();
    snapshotManager.resetSnapshots(blockchain);
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    manager.mineBlock(blockchain, minerClient, minerServer);
    manager.mineBlock(blockchain, minerClient, minerServer);
    Assert.assertEquals(2, blockchain.getBestBlock().getNumber());
    snapshotManager.resetSnapshots(blockchain);
    Assert.assertTrue(blockchain.getTransactionPool().getPendingTransactions().isEmpty());
    manager.mineBlock(blockchain, minerClient, minerServer);
    Assert.assertTrue(blockchain.getTransactionPool().getPendingTransactions().isEmpty());
}
Also used : Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) SnapshotManager(co.rsk.core.SnapshotManager) Test(org.junit.Test)

Example 50 with Blockchain

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

the class MinerManagerTest method mineBlockUsingTimeTravel.

@Test
public void mineBlockUsingTimeTravel() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    MinerManager manager = new MinerManager();
    MinerServerImpl minerServer = getMinerServer(blockchain);
    MinerClientImpl minerClient = getMinerClient(minerServer);
    long currentTime = minerServer.getCurrentTimeInSeconds();
    minerServer.increaseTime(10);
    manager.mineBlock(blockchain, minerClient, minerServer);
    Block block = blockchain.getBestBlock();
    Assert.assertEquals(1, block.getNumber());
    Assert.assertTrue(currentTime + 10 <= block.getTimestamp());
    Assert.assertTrue(currentTime + 11 > block.getTimestamp());
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) World(co.rsk.test.World) 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