Search in sources :

Example 66 with BlockHeader

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

the class NodeBlockProcessorUnclesTest method addBlockWithTwoKnownUncles.

@Test
public void addBlockWithTwoKnownUncles() {
    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);
    processor.processBlock(null, uncle1);
    processor.processBlock(null, uncle2);
    SimplePeer sender = new SimplePeer();
    processor.processBlock(sender, block2);
    Assert.assertEquals(2, blockChain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block2.getHash().getBytes(), blockChain.getBestBlockHash());
    Assert.assertTrue(sender.getGetBlockMessages().isEmpty());
}
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)

Example 67 with BlockHeader

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

the class HashRateCalculatorTest method init.

@Before
public void init() {
    blockStore = Mockito.mock(BlockStore.class);
    block = Mockito.mock(Block.class);
    blockHeader = Mockito.mock(BlockHeader.class);
    Mockito.when(block.getHeader()).thenReturn(blockHeader);
    Mockito.when(block.getHash()).thenReturn(new Keccak256(FAKE_GENERIC_HASH));
    Mockito.when(blockHeader.getParentHash()).thenReturn(new Keccak256(FAKE_GENERIC_HASH)).thenReturn(new Keccak256(OHTER_FAKE_GENERIC_HASH)).thenReturn(new Keccak256(FAKE_GENERIC_HASH)).thenReturn(null);
    Mockito.when(blockHeader.getHash()).thenReturn(new Keccak256(FAKE_GENERIC_HASH));
    Mockito.when(blockStore.getBlockByHash(Mockito.any())).thenReturn(block).thenReturn(block).thenReturn(block).thenReturn(null);
    Mockito.when(blockStore.getBestBlock()).thenReturn(block);
    Mockito.when(blockStore.getBlockByHash(Mockito.any())).thenReturn(block);
}
Also used : BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) Before(org.junit.Before)

Example 68 with BlockHeader

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

the class ForkDetectionDataRuleTest method invalidForRskip110ActiveAndForkDetectionDataBecauseDataDoesNotMatch.

@Test
public void invalidForRskip110ActiveAndForkDetectionDataBecauseDataDoesNotMatch() {
    long blockNumber = 4242;
    enableRulesAt(blockNumber, ConsensusRule.RSKIP110);
    Keccak256 parentBlockHash = new Keccak256(getRandomHash());
    int requiredBlocksForForkDataCalculation = 449;
    List<BlockHeader> previousBlocks = IntStream.range(0, requiredBlocksForForkDataCalculation).mapToObj(i -> mock(BlockHeader.class)).collect(Collectors.toList());
    ConsensusValidationMainchainView mainchainView = mock(ConsensusValidationMainchainView.class);
    when(mainchainView.get(parentBlockHash, requiredBlocksForForkDataCalculation)).thenReturn(previousBlocks);
    ForkDetectionDataCalculator calculator = mock(ForkDetectionDataCalculator.class);
    Keccak256 blockHash = new Keccak256(getRandomHash());
    byte[] forkDetectionData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    when(calculator.calculateWithBlockHeaders(previousBlocks)).thenReturn(forkDetectionData);
    ForkDetectionDataRule rule = new ForkDetectionDataRule(activationConfig, mainchainView, calculator, requiredBlocksForForkDataCalculation);
    BlockHeader header = mock(BlockHeader.class);
    when(header.getNumber()).thenReturn(blockNumber);
    when(header.getHash()).thenReturn(blockHash);
    when(header.getParentHash()).thenReturn(parentBlockHash);
    byte[] headerForkDetectionData = new byte[] { 1, 2, 3, 4, 5, 6, 42, 8, 9, 10, 11, 12 };
    when(header.getMiningForkDetectionData()).thenReturn(headerForkDetectionData);
    assertFalse(rule.isValid(header));
}
Also used : IntStream(java.util.stream.IntStream) ForkDetectionDataCalculator(co.rsk.mine.ForkDetectionDataCalculator) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockHeader(org.ethereum.core.BlockHeader) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Keccak256(co.rsk.crypto.Keccak256) Collectors(java.util.stream.Collectors) Block(org.ethereum.core.Block) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) AdditionalMatchers.geq(org.mockito.AdditionalMatchers.geq) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) ForkDetectionDataCalculator(co.rsk.mine.ForkDetectionDataCalculator) Test(org.junit.Test)

Example 69 with BlockHeader

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

the class ForkDetectionDataRuleTest method validForBlocksBeforeRskip110.

@Test
public void validForBlocksBeforeRskip110() {
    long blockNumber = 4242;
    ForkDetectionDataRule rule = new ForkDetectionDataRule(activationConfig, mock(ConsensusValidationMainchainView.class), mock(ForkDetectionDataCalculator.class), 449);
    BlockHeader header = mock(BlockHeader.class);
    when(header.getNumber()).thenReturn(blockNumber);
    assertTrue(rule.isValid(header));
}
Also used : ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockHeader(org.ethereum.core.BlockHeader) ForkDetectionDataCalculator(co.rsk.mine.ForkDetectionDataCalculator) Test(org.junit.Test)

Example 70 with BlockHeader

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

the class ForkDetectionDataRuleTest method validForRskip110ActiveAndForkDetectionData.

@Test
public void validForRskip110ActiveAndForkDetectionData() {
    long blockNumber = 4242;
    enableRulesAt(blockNumber, ConsensusRule.RSKIP110);
    Keccak256 parentBlockHash = new Keccak256(getRandomHash());
    int requiredBlocksForForkDataCalculation = 449;
    List<BlockHeader> previousBlocks = IntStream.range(0, requiredBlocksForForkDataCalculation).mapToObj(i -> mock(BlockHeader.class)).collect(Collectors.toList());
    ConsensusValidationMainchainView mainchainView = mock(ConsensusValidationMainchainView.class);
    when(mainchainView.get(parentBlockHash, requiredBlocksForForkDataCalculation)).thenReturn(previousBlocks);
    ForkDetectionDataCalculator calculator = mock(ForkDetectionDataCalculator.class);
    Keccak256 blockHash = new Keccak256(getRandomHash());
    byte[] forkDetectionData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    when(calculator.calculateWithBlockHeaders(previousBlocks)).thenReturn(forkDetectionData);
    ForkDetectionDataRule rule = new ForkDetectionDataRule(activationConfig, mainchainView, calculator, requiredBlocksForForkDataCalculation);
    BlockHeader header = mock(BlockHeader.class);
    when(header.getNumber()).thenReturn(blockNumber);
    when(header.getHash()).thenReturn(blockHash);
    when(header.getParentHash()).thenReturn(parentBlockHash);
    when(header.getMiningForkDetectionData()).thenReturn(forkDetectionData);
    assertTrue(rule.isValid(header));
}
Also used : IntStream(java.util.stream.IntStream) ForkDetectionDataCalculator(co.rsk.mine.ForkDetectionDataCalculator) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockHeader(org.ethereum.core.BlockHeader) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Keccak256(co.rsk.crypto.Keccak256) Collectors(java.util.stream.Collectors) Block(org.ethereum.core.Block) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) AdditionalMatchers.geq(org.mockito.AdditionalMatchers.geq) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) ForkDetectionDataCalculator(co.rsk.mine.ForkDetectionDataCalculator) Test(org.junit.Test)

Aggregations

BlockHeader (org.ethereum.core.BlockHeader)189 Test (org.junit.Test)128 Block (org.ethereum.core.Block)83 Keccak256 (co.rsk.crypto.Keccak256)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ArrayList (java.util.ArrayList)23 BlockStore (org.ethereum.db.BlockStore)21 RLPList (org.ethereum.util.RLPList)20 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)14 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)9 BlockDifficulty (co.rsk.core.BlockDifficulty)7 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)7 ForkDetectionDataCalculator (co.rsk.mine.ForkDetectionDataCalculator)7 BigInteger (java.math.BigInteger)7 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)6 SimplePeer (co.rsk.net.simples.SimplePeer)6 BlockBuilder (co.rsk.test.builders.BlockBuilder)6 Trie (co.rsk.trie.Trie)4 Before (org.junit.Before)4 DifficultyCalculator (co.rsk.core.DifficultyCalculator)3