Search in sources :

Example 96 with BlockChainBuilder

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

the class AsyncNodeBlockProcessorTest method advancedBlock.

@Test
public void advancedBlock() {
    final NetBlockStore store = new NetBlockStore();
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    final Blockchain blockchain = new BlockChainBuilder().ofSize(0);
    final long advancedBlockNumber = syncConfiguration.getChunkSize() * syncConfiguration.getMaxSkeletonChunks() + blockchain.getBestBlock().getNumber() + 1;
    final TestSystemProperties config = new TestSystemProperties();
    final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE);
    Assert.assertTrue(processor.isAdvancedBlock(advancedBlockNumber));
    Assert.assertFalse(processor.isAdvancedBlock(advancedBlockNumber - 1));
}
Also used : Blockchain(org.ethereum.core.Blockchain) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 97 with BlockChainBuilder

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

the class MainNetMinerTest method submitBitcoinBlockProofOfWorkNotGoodEnough.

/*
     * This test is probabilistic, but it has a really high chance to pass. We will generate
     * a random block that it is unlikely to pass the Long.MAX_VALUE difficulty, though
     * it may happen once. Twice would be suspicious.
     */
@Test
public void submitBitcoinBlockProofOfWorkNotGoodEnough() {
    /* We need a low target */
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockChainImpl blockchain = blockChainBuilder.build();
    Genesis gen = (Genesis) BlockChainImplTest.getGenesisBlock(blockChainBuilder.getTrieStore());
    gen.getHeader().setDifficulty(new BlockDifficulty(BigInteger.valueOf(Long.MAX_VALUE)));
    blockchain.setStatus(gen, gen.getCumulativeDifficulty());
    EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
    MinerClock clock = new MinerClock(true, Clock.systemUTC());
    MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, mainchainView, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), blockToMineBuilder(), clock, blockFactory, new BuildInfo("cb7f28e", "master"), ConfigUtils.getDefaultMiningConfig());
    try {
        minerServer.start();
        MinerWork work = minerServer.getWork();
        co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work);
        bitcoinMergedMiningBlock.setNonce(2);
        SubmitBlockResult result = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
        Assert.assertEquals("ERROR", result.getStatus());
        Assert.assertNull(result.getBlockInfo());
        Mockito.verify(ethereumImpl, Mockito.times(0)).addNewMinedBlock(Mockito.any());
    } finally {
        minerServer.stop();
    }
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) EthereumImpl(org.ethereum.facade.EthereumImpl) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) BuildInfo(org.ethereum.util.BuildInfo) Genesis(org.ethereum.core.Genesis) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 98 with BlockChainBuilder

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

the class BlockSyncServiceTest method sendBlockMessagesAndAddThemToBlockchain.

@Test
public void sendBlockMessagesAndAddThemToBlockchain() {
    for (int i = 0; i < 50; i += 5) {
        Blockchain blockchain = new BlockChainBuilder().ofSize(10 * i);
        NetBlockStore store = new NetBlockStore();
        BlockNodeInformation nodeInformation = new BlockNodeInformation();
        TestSystemProperties config = new TestSystemProperties();
        BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
        Assert.assertEquals(10 * i, blockchain.getBestBlock().getNumber());
        List<Block> extendedChain = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), i);
        for (Block block : extendedChain) {
            blockSyncService.processBlock(block, null, false);
            Assert.assertEquals(block.getNumber(), blockchain.getBestBlock().getNumber());
            Assert.assertEquals(block.getHash(), blockchain.getBestBlock().getHash());
        }
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 99 with BlockChainBuilder

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

the class BlockSyncServiceTest method sendBlockMessageAndAddItToBlockchainWithCommonAncestors.

@Test
public void sendBlockMessageAndAddItToBlockchainWithCommonAncestors() {
    Blockchain blockchain = new BlockChainBuilder().ofSize(10);
    NetBlockStore store = new NetBlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    Block initialBestBlock = blockchain.getBestBlock();
    Assert.assertEquals(10, initialBestBlock.getNumber());
    Block branchingPoint = blockchain.getBlockByNumber(7);
    BlockGenerator blockGenerator = new BlockGenerator();
    List<Block> extendedChain = blockGenerator.getBlockChain(branchingPoint, 10, 1000000l);
    // we have just surpassed the best branch
    for (int i = 0; i < extendedChain.size(); i++) {
        Block newBestBlock = extendedChain.get(i);
        blockSyncService.processBlock(newBestBlock, null, false);
        Assert.assertEquals(newBestBlock.getNumber(), blockchain.getBestBlock().getNumber());
        Assert.assertEquals(newBestBlock.getHash(), blockchain.getBestBlock().getHash());
    }
}
Also used : Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 100 with BlockChainBuilder

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

the class NodeBlockProcessorUnclesTest method addBlockWithTwoUnknownUncles.

@Test
public void addBlockWithTwoUnknownUncles() {
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockChainImpl blockChain = blockChainBuilder.build();
    org.ethereum.db.BlockStore blockStore = blockChainBuilder.getBlockStore();
    NodeBlockProcessor processor = createNodeBlockProcessor(blockChain);
    Block genesis = blockChain.getBestBlock();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, null, blockStore).trieStore(blockChainBuilder.getTrieStore());
    blockBuilder.parent(blockChain.getBestBlock());
    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);
    SimplePeer sender = new SimplePeer();
    processor.processBlock(sender, block2);
    Assert.assertEquals(2, blockChain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block2.getHash().getBytes(), blockChain.getBestBlockHash());
    Assert.assertEquals(0, sender.getGetBlockMessages().size());
}
Also used : 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) BlockBuilder(co.rsk.test.builders.BlockBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Aggregations

BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)155 Test (org.junit.Test)139 TestSystemProperties (co.rsk.config.TestSystemProperties)109 Blockchain (org.ethereum.core.Blockchain)88 SimplePeer (co.rsk.net.simples.SimplePeer)82 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)76 Block (org.ethereum.core.Block)69 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)62 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)25 BlockStore (org.ethereum.db.BlockStore)25 EthereumListener (org.ethereum.listener.EthereumListener)25 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)19 Ignore (org.junit.Ignore)17 RskSystemProperties (co.rsk.config.RskSystemProperties)15 Keccak256 (co.rsk.crypto.Keccak256)14 SimpleAsyncNode (co.rsk.net.simples.SimpleAsyncNode)11 AsyncNodeBlockProcessorListener (co.rsk.net.utils.AsyncNodeBlockProcessorListener)9 org.ethereum.core (org.ethereum.core)9 ECKey (org.ethereum.crypto.ECKey)9 ChannelManager (org.ethereum.net.server.ChannelManager)9