Search in sources :

Example 1 with ConsensusValidationMainchainView

use of co.rsk.core.bc.ConsensusValidationMainchainView 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 2 with ConsensusValidationMainchainView

use of co.rsk.core.bc.ConsensusValidationMainchainView 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)

Example 3 with ConsensusValidationMainchainView

use of co.rsk.core.bc.ConsensusValidationMainchainView in project rskj by rsksmart.

the class ForkDetectionDataRuleTest method invalidForRskip110ActiveAndForkDetectionDataButMissingBlocksForCalculation.

@Test
public void invalidForRskip110ActiveAndForkDetectionDataButMissingBlocksForCalculation() {
    long blockNumber = 4242;
    enableRulesAt(blockNumber, ConsensusRule.RSKIP110);
    Keccak256 parentBlockHash = new Keccak256(getRandomHash());
    int requiredBlocksForForkDataCalculation = 449;
    ConsensusValidationMainchainView mainchainView = mock(ConsensusValidationMainchainView.class);
    when(mainchainView.get(parentBlockHash, requiredBlocksForForkDataCalculation)).thenReturn(new ArrayList<>());
    ForkDetectionDataRule rule = new ForkDetectionDataRule(activationConfig, mainchainView, mock(ForkDetectionDataCalculator.class), requiredBlocksForForkDataCalculation);
    BlockHeader header = mock(BlockHeader.class);
    when(header.getNumber()).thenReturn(blockNumber);
    when(header.getParentHash()).thenReturn(parentBlockHash);
    Keccak256 blockHash = new Keccak256(getRandomHash());
    when(header.getHash()).thenReturn(blockHash);
    byte[] forkDetectionData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    when(header.getMiningForkDetectionData()).thenReturn(forkDetectionData);
    assertFalse(rule.isValid(header));
}
Also used : 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

ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)3 Keccak256 (co.rsk.crypto.Keccak256)3 ForkDetectionDataCalculator (co.rsk.mine.ForkDetectionDataCalculator)3 BlockHeader (org.ethereum.core.BlockHeader)3 Test (org.junit.Test)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecureRandom (java.security.SecureRandom)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)2 ConsensusRule (org.ethereum.config.blockchain.upgrades.ConsensusRule)2 Block (org.ethereum.core.Block)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Before (org.junit.Before)2 AdditionalMatchers.geq (org.mockito.AdditionalMatchers.geq)2 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)2 Mockito.mock (org.mockito.Mockito.mock)2