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();
}
}
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());
}
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();
}
}
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);
}
}
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;
}
Aggregations