Search in sources :

Example 31 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockUtilsTest method unknowAncestorsHashesUsingUncles.

@Test
public void unknowAncestorsHashesUsingUncles() {
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockGenerator blockGenerator = new BlockGenerator();
    Genesis genesis = blockGenerator.getGenesisBlock();
    BlockChainImpl blockChain = blockChainBuilder.setGenesis(genesis).build();
    BlockStore store = new BlockStore();
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, blockGenerator);
    Block block1 = blockBuilder.parent(genesis).build();
    Block block1b = blockBuilder.parent(genesis).build();
    Block block2 = blockBuilder.parent(block1).build();
    Block uncle1 = blockBuilder.parent(block1).build();
    Block uncle2 = blockBuilder.parent(block1).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block3 = blockBuilder.parent(block2).uncles(uncles).build();
    store.saveBlock(block3);
    blockChain.tryToConnect(genesis);
    blockChain.tryToConnect(block1);
    blockChain.tryToConnect(block1b);
    Set<Keccak256> hashes = BlockUtils.unknownAncestorsHashes(genesis.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1b.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block2.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    hashes = BlockUtils.unknownAncestorsHashes(block3.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(3, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    Assert.assertTrue(hashes.contains(uncle1.getHash()));
    Assert.assertTrue(hashes.contains(uncle2.getHash()));
}
Also used : BlockStore(co.rsk.net.BlockStore) ArrayList(java.util.ArrayList) Genesis(org.ethereum.core.Genesis) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 32 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockUtilsTest method blockInSomeBlockChain.

@Test
public void blockInSomeBlockChain() {
    BlockChainImpl blockChain = new BlockChainBuilder().build();
    Block genesis = new BlockGenerator().getGenesisBlock();
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    Block block1 = new BlockBuilder().parent(genesis).build();
    Block block1b = new BlockBuilder().parent(genesis).build();
    Block block2 = new BlockBuilder().parent(block1).build();
    Block block3 = new BlockBuilder().parent(block2).build();
    blockChain.getBlockStore().saveBlock(block3, new BlockDifficulty(BigInteger.ONE), false);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    blockChain.tryToConnect(block1);
    blockChain.tryToConnect(block1b);
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(genesis, blockChain));
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block1, blockChain));
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block1b, blockChain));
    Assert.assertFalse(BlockUtils.blockInSomeBlockChain(block2, blockChain));
    Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block3, blockChain));
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 33 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class RskForksBridgeTest method buildBlock.

private Block buildBlock(Block parent, long difficulty, Transaction... txs) {
    List<Transaction> txList = Arrays.asList(txs);
    World world = new World(blockChain, genesis);
    BlockBuilder blockBuilder = new BlockBuilder(world).difficulty(difficulty).parent(parent).transactions(txList).uncles(new ArrayList<>());
    return blockBuilder.build();
}
Also used : World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 34 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class RskForksBridgeTest method buildBlock.

private Block buildBlock(Block parent, long difficulty) {
    World world = new World(blockChain, genesis);
    BlockBuilder blockBuilder = new BlockBuilder(world).difficulty(difficulty).parent(parent);
    return blockBuilder.build();
}
Also used : World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 35 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class NodeBlockProcessorUnclesTest method addBlockWithTwoUnknownUncles.

@Test
public void addBlockWithTwoUnknownUncles() throws UnknownHostException {
    BlockChainImpl blockChain = new BlockChainBuilder().build();
    NodeBlockProcessor processor = createNodeBlockProcessor(blockChain);
    Block genesis = processor.getBlockchain().getBestBlock();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, new BlockGenerator());
    Block block1 = blockBuilder.parent(genesis).build();
    Block uncle1 = blockBuilder.parent(genesis).build();
    Block uncle2 = blockBuilder.parent(genesis).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block2 = blockBuilder.parent(block1).uncles(uncles).build();
    processor.processBlock(null, block1);
    SimpleMessageChannel sender = new SimpleMessageChannel();
    processor.processBlock(sender, block2);
    Assert.assertEquals(2, processor.getBlockchain().getBestBlock().getNumber());
    Assert.assertArrayEquals(block2.getHash().getBytes(), processor.getBlockchain().getBestBlockHash());
    Assert.assertEquals(0, sender.getGetBlockMessages().size());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockBuilder (co.rsk.test.builders.BlockBuilder)61 Test (org.junit.Test)54 World (co.rsk.test.World)36 ArrayList (java.util.ArrayList)27 AccountBuilder (co.rsk.test.builders.AccountBuilder)26 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)17 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)13 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)12 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)11 SimpleBlock (co.rsk.peg.simples.SimpleBlock)11 Block (org.ethereum.core.Block)11 HashMapDB (org.ethereum.datasource.HashMapDB)9 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)8 SimpleEthereum (org.ethereum.rpc.Simples.SimpleEthereum)8 RemascTransaction (co.rsk.remasc.RemascTransaction)7 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)7 TransactionResultDTO (org.ethereum.rpc.dto.TransactionResultDTO)6 BlockHeader (org.ethereum.core.BlockHeader)4 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)3 BlockStore (org.ethereum.db.BlockStore)3