Search in sources :

Example 36 with BlockGenerator

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

the class SyncProcessorTest method processBlockResponseAddsToBlockchain.

@Test
public void processBlockResponseAddsToBlockchain() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(10);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
    Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
    Assert.assertEquals(11, block.getNumber());
    Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    BlockResponseMessage response = new BlockResponseMessage(new Random().nextLong(), block);
    processor.registerExpectedMessage(response);
    processor.processBlockResponse(sender, response);
    Assert.assertEquals(11, blockchain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 37 with BlockGenerator

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

the class SyncProcessorTest method doesntProcessUnexpectedBodyResponse.

@Test
public void doesntProcessUnexpectedBodyResponse() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(10);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
    Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
    Blockchain extended = BlockChainBuilder.copy(blockchain);
    extended.tryToConnect(block);
    Assert.assertEquals(11, block.getNumber());
    Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
    List<Transaction> transactions = blockchain.getBestBlock().getTransactionsList();
    List<BlockHeader> uncles = blockchain.getBestBlock().getUncleList();
    long lastRequestId = new Random().nextLong();
    BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
    processor.registerExpectedMessage(response);
    Deque<BlockHeader> headerStack = new ArrayDeque<>();
    headerStack.add(block.getHeader());
    List<Deque<BlockHeader>> headers = new ArrayList<>();
    headers.add(headerStack);
    int connectionPoint = 10;
    int step = 192;
    int linkCount = 1;
    processor.startDownloadingBodies(headers, Collections.singletonMap(sender.getPeerNodeID(), buildSkeleton(extended, connectionPoint, step, linkCount)));
    // ((DownloadingBodiesSyncState)processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
    processor.processBodyResponse(sender, response);
    Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
    Assert.assertNotEquals(block.getNumber(), blockchain.getBestBlock().getNumber());
    // if an invalid body arrives then stops syncing
    Assert.assertFalse(processor.getSyncState().isSyncing());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 38 with BlockGenerator

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

the class LogFilterTest method noEventsAfterEmptyBlock.

@Test
public void noEventsAfterEmptyBlock() {
    LogFilter filter = new LogFilter(null, null, false, false);
    Block block = new BlockGenerator().getBlock(1);
    filter.newBlockReceived(block);
    Object[] result = filter.getEvents();
    Assert.assertNotNull(result);
    Assert.assertEquals(0, result.length);
}
Also used : Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 39 with BlockGenerator

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

the class BlockUnclesValidationRuleTest method rejectBlockWithSiblingUncle.

@Test
public void rejectBlockWithSiblingUncle() {
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block uncle = blockGenerator.createChildBlock(block1);
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle.getHeader());
    Block block = blockGenerator.createChildBlock(block1, null, uncles, 1, null);
    BlockChainImpl blockChain = BlockChainImplTest.createBlockChain();
    BlockStore store = blockChain.getBlockStore();
    store.saveBlock(genesis, new BlockDifficulty(BigInteger.valueOf(1)), true);
    store.saveBlock(block1, new BlockDifficulty(BigInteger.valueOf(2)), true);
    BlockUnclesValidationRule rule = new BlockUnclesValidationRule(config, store, 10, 10, new BlockCompositeRule(), new BlockParentCompositeRule());
    Assert.assertFalse(rule.isValid(block));
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockStore(org.ethereum.db.BlockStore) ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 40 with BlockGenerator

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

the class BlockchainVMTest method testSEND_1.

@Test
public void testSEND_1() {
    NewBlockChainInfo binfo = createNewBlockchain();
    Blockchain blockchain = binfo.blockchain;
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block1 = blockGenerator.createChildBlock(blockchain.getBestBlock(), null, binfo.repository.getRoot());
    List<Transaction> txs = new ArrayList<>();
    Coin transferAmount = Coin.valueOf(100L);
    // Add a single transaction paying to a new address
    byte[] dstAddress = randomAddress();
    BigInteger transactionGasLimit = new BigInteger("21000");
    Coin transactionGasPrice = Coin.valueOf(1);
    Transaction t = new Transaction(ZERO_BYTE_ARRAY, transactionGasPrice.getBytes(), transactionGasLimit.toByteArray(), dstAddress, transferAmount.getBytes(), null, new RskSystemProperties().getBlockchainConfig().getCommonConstants().getChainId());
    t.sign(binfo.faucetKey.getPrivKeyBytes());
    txs.add(t);
    Block block2 = blockGenerator.createChildBlock(block1, txs, binfo.repository.getRoot());
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block1));
    MinerHelper mh = new MinerHelper(binfo.repository, binfo.blockchain);
    mh.completeBlock(block2, block1);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block2));
    Assert.assertEquals(blockchain.getBestBlock(), block2);
    Assert.assertEquals(2, block2.getNumber());
    Coin srcAmount = faucetAmount.subtract(transferAmount);
    srcAmount = srcAmount.subtract(transactionGasPrice.multiply(transactionGasLimit));
    Assert.assertEquals(binfo.repository.getBalance(new RskAddress(binfo.faucetKey.getAddress())), srcAmount);
    Assert.assertEquals(binfo.repository.getBalance(new RskAddress(dstAddress)), transferAmount);
}
Also used : ArrayList(java.util.ArrayList) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Coin(co.rsk.core.Coin) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) RskSystemProperties(co.rsk.config.RskSystemProperties) 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