Search in sources :

Example 41 with Blockchain

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

the class BlockchainTest method addFirstBlock.

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

Example 42 with Blockchain

use of org.ethereum.core.Blockchain 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 43 with Blockchain

use of org.ethereum.core.Blockchain 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 44 with Blockchain

use of org.ethereum.core.Blockchain 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 45 with Blockchain

use of org.ethereum.core.Blockchain 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)

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