Search in sources :

Example 91 with World

use of co.rsk.test.World 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 92 with World

use of co.rsk.test.World 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)

Example 93 with World

use of co.rsk.test.World 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 94 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class NodeMessageHandlerTest method processGetBlockHeadersMessage.

@Test
@Ignore("Block headers message is deprecated must be rewrited or deleted")
public void processGetBlockHeadersMessage() throws UnknownHostException {
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    final BlockStore store = new BlockStore();
    final List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 10);
    for (Block b : blocks) {
        ImportResult result = blockchain.tryToConnect(b);
        System.out.println(result);
    }
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    int baseBlock = 9;
    /*
            TestCase represents a GetBlockHeaderMessage test case: it receives an input and the expected BlockHeaders.
            if expected == null, we are not expecting to receive any message.
        */
    class TestCase {

        protected final GetBlockHeadersMessage gbhMessage;

        protected final List<BlockHeader> expected;

        public TestCase(@Nonnull final GetBlockHeadersMessage gbhMessage, final List<BlockHeader> expected) {
            this.gbhMessage = gbhMessage;
            this.expected = expected;
        }
    }
    TestCase[] testCases = { new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 0), null), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 5), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 1).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 3).getHeader(), blocks.get(baseBlock - 4).getHeader())), new TestCase(new GetBlockHeadersMessage(0, blocks.get(baseBlock).getHash().getBytes(), 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), null, 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())) };
    for (int i = 0; i < testCases.length; i += 1) {
        final TestCase testCase = testCases[i];
        final SimpleMessageChannel sender = new SimpleMessageChannel();
        handler.processMessage(sender, testCase.gbhMessage);
        if (testCase.expected == null) {
            Assert.assertEquals(0, sender.getMessages().size());
            continue;
        }
        Assert.assertEquals(1, sender.getMessages().size());
        final Message message = sender.getMessages().get(0);
        Assert.assertEquals(MessageType.BLOCK_HEADERS_MESSAGE, message.getMessageType());
        final List<BlockHeader> headers = ((BlockHeadersMessage) message).getBlockHeaders();
        equalBlockHeaders(testCase.expected, headers);
    }
}
Also used : Nonnull(javax.annotation.Nonnull) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 95 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplLogsTest method getWorld3WithBlockWithEventInContractCreation.

public static World getWorld3WithBlockWithEventInContractCreation(RskSystemProperties config, ReceiptStore receiptStore) {
    World world = new World(receiptStore);
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    Block genesis = world.getBlockByName("g00");
    Transaction tx;
    tx = getContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    BlockChainImpl blockChain = world.getBlockChain();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    blockChain.tryToConnect(block1);
    return world;
}
Also used : ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Aggregations

World (co.rsk.test.World)198 Test (org.junit.Test)169 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)55 DslParser (co.rsk.test.dsl.DslParser)52 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMapDB (org.ethereum.datasource.HashMapDB)45 AccountBuilder (co.rsk.test.builders.AccountBuilder)36 Block (org.ethereum.core.Block)36 ReceiptStore (org.ethereum.db.ReceiptStore)34 BlockBuilder (co.rsk.test.builders.BlockBuilder)31 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)31 Blockchain (org.ethereum.core.Blockchain)30 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)24 Transaction (org.ethereum.core.Transaction)23 BigInteger (java.math.BigInteger)17 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 Account (org.ethereum.core.Account)15 TestSystemProperties (co.rsk.config.TestSystemProperties)14