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