Search in sources :

Example 86 with BlockGenerator

use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.

the class BlockchainTest method connectOneChainThenConnectAnotherChain.

@Test
public void connectOneChainThenConnectAnotherChain() {
    Blockchain blockchain = createBlockchain();
    final int height = 200;
    final long chain1Diff = 2;
    final long chain2Diff = 1;
    BlockGenerator blockGenerator = new BlockGenerator();
    List<Block> chain1 = blockGenerator.getBlockChain(blockchain.getBestBlock(), height, chain1Diff);
    List<Block> chain2 = blockGenerator.getBlockChain(blockchain.getBestBlock(), height, chain2Diff);
    for (Block b : chain1) Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(b));
    for (Block b : chain2) Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, blockchain.tryToConnect(b));
    Block newblock = blockGenerator.createChildBlock(chain2.get(chain2.size() - 1), 0, 2 * height * chain2Diff);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(newblock));
    Assert.assertEquals(blockchain.getBestBlock(), newblock);
    Assert.assertEquals(chain2.size() + 1, newblock.getNumber());
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 87 with BlockGenerator

use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.

the class BlockchainTest method childBlock.

@Test
public void childBlock() {
    Blockchain blockchain = createBlockchain();
    Block block = new BlockGenerator().createChildBlock(blockchain.getBestBlock());
    Assert.assertNotNull(block);
    Assert.assertEquals(1, block.getNumber());
    Assert.assertEquals(blockchain.getBestBlock().getHash(), block.getParentHash());
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 88 with BlockGenerator

use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.

the class BlockchainTest method addTwoBlocks.

@Test
public void addTwoBlocks() {
    Blockchain blockchain = createBlockchain();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block1 = blockGenerator.createChildBlock(blockchain.getBestBlock());
    Block block2 = blockGenerator.createChildBlock(block1);
    blockchain.tryToConnect(block1);
    blockchain.tryToConnect(block2);
    Assert.assertEquals(blockchain.getBestBlock(), block2);
    Assert.assertEquals(2, block2.getNumber());
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 89 with BlockGenerator

use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.

the class BlockchainTest method tryToConnectWithFork.

@Test
public void tryToConnectWithFork() {
    Blockchain blockchain = createBlockchain();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block1 = blockGenerator.createChildBlock(blockchain.getBestBlock(), 0, 1);
    Block block1b = blockGenerator.createChildBlock(blockchain.getBestBlock(), 0, 2);
    Block block2 = blockGenerator.createChildBlock(block1, 0, 3);
    Block block2b = blockGenerator.createChildBlock(block1b, 0, 1);
    Block block3b = blockGenerator.createChildBlock(block2b, 0, 4);
    Assert.assertEquals(ImportResult.NO_PARENT, blockchain.tryToConnect(block2));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block1));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block2));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, blockchain.tryToConnect(block1b));
    Assert.assertEquals(ImportResult.EXIST, blockchain.tryToConnect(block1));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, blockchain.tryToConnect(block2b));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block3b));
    Assert.assertEquals(blockchain.getBestBlock(), block3b);
    Assert.assertEquals(3, block3b.getNumber());
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 90 with BlockGenerator

use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.

the class FileBlockRecorderTest method writeBlock.

@Test
public void writeBlock() throws Exception {
    FileBlockRecorder recorder = new FileBlockRecorder("testrecorder.txt");
    recorder.writeBlock(new BlockGenerator().getGenesisBlock());
    File file = new File("testrecorder.txt");
    Assert.assertTrue(file.exists());
    recorder.close();
    Assert.assertTrue(file.delete());
}
Also used : BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) File(java.io.File) Test(org.junit.Test)

Aggregations

BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)250 Test (org.junit.Test)237 Block (org.ethereum.core.Block)104 ArrayList (java.util.ArrayList)39 Blockchain (org.ethereum.core.Blockchain)38 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)36 RskSystemProperties (co.rsk.config.RskSystemProperties)35 SimpleBlock (co.rsk.peg.simples.SimpleBlock)34 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)33 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)31 HashMapDB (org.ethereum.datasource.HashMapDB)22 BlockBuilder (co.rsk.test.builders.BlockBuilder)17 Keccak256 (co.rsk.crypto.Keccak256)15 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)15 BlockStore (org.ethereum.db.BlockStore)15 World (co.rsk.test.World)14 RepositoryImpl (co.rsk.db.RepositoryImpl)13 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)13 Ignore (org.junit.Ignore)12 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)10