Search in sources :

Example 76 with BlockHeader

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

the class BlockTimeStampValidationRuleTest method timestampsAreCloseEnough.

@Test
public void timestampsAreCloseEnough() {
    int validPeriod = 540;
    BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod, postRskip179Config, timeProvider, bitcoinNetworkParameters);
    byte[] bitcoinMergedMiningHeader = new byte[0];
    when(timeProvider.currentTimeMillis()).thenReturn(10_000_000L);
    BlockHeader header = mock(BlockHeader.class);
    Block block = mock(Block.class);
    when(block.getHeader()).thenReturn(header);
    when(header.getBitcoinMergedMiningHeader()).thenReturn(bitcoinMergedMiningHeader);
    BtcBlock btcBlock = mock(BtcBlock.class);
    MessageSerializer messageSerializer = mock(MessageSerializer.class);
    when(messageSerializer.makeBlock(eq(bitcoinMergedMiningHeader))).thenReturn(btcBlock);
    when(bitcoinNetworkParameters.getDefaultSerializer()).thenReturn(messageSerializer);
    when(btcBlock.getTimeSeconds()).thenReturn(1_000L);
    when(header.getTimestamp()).thenReturn(1_000L);
    assertTrue(validationRule.isValid(header));
    when(btcBlock.getTimeSeconds()).thenReturn(1_000L + MAX_TIMESTAMPS_DIFF_IN_SECS);
    when(header.getTimestamp()).thenReturn(1_000L);
    assertFalse(validationRule.isValid(header));
    when(btcBlock.getTimeSeconds()).thenReturn(1_000L + MAX_TIMESTAMPS_DIFF_IN_SECS);
    when(header.getTimestamp()).thenReturn(1_001L);
    assertTrue(validationRule.isValid(header));
    when(btcBlock.getTimeSeconds()).thenReturn(1_001L);
    when(header.getTimestamp()).thenReturn(1_000L + MAX_TIMESTAMPS_DIFF_IN_SECS);
    assertTrue(validationRule.isValid(header));
}
Also used : MessageSerializer(co.rsk.bitcoinj.core.MessageSerializer) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Block(org.ethereum.core.Block) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 77 with BlockHeader

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

the class BlockTimeStampValidationRuleTest method blockInTheFutureLimit.

@Test
public void blockInTheFutureLimit() {
    int validPeriod = 540;
    BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod, preRskip179Config, timeProvider);
    when(timeProvider.currentTimeMillis()).thenReturn(10_000_000L);
    BlockHeader header = mock(BlockHeader.class);
    Block block = mock(Block.class);
    when(block.getHeader()).thenReturn(header);
    when(header.getTimestamp()).thenReturn(10_000L + validPeriod);
    assertTrue(validationRule.isValid(header));
}
Also used : BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 78 with BlockHeader

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

the class BlockTimeStampValidationRuleTest method blockInThePast.

@Test
public void blockInThePast() {
    int validPeriod = 540;
    BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod, preRskip179Config, timeProvider);
    when(timeProvider.currentTimeMillis()).thenReturn(10_000_000L);
    BlockHeader header = mock(BlockHeader.class);
    Block block = mock(Block.class);
    when(block.getHeader()).thenReturn(header);
    when(header.getTimestamp()).thenReturn(10_000L - 1000);
    assertTrue(validationRule.isValid(header));
}
Also used : BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 79 with BlockHeader

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

the class BlockTimeStampValidationRuleTest method blockTimeGreaterThanParentTime.

@Test
public void blockTimeGreaterThanParentTime() {
    int validPeriod = 540;
    BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod, preRskip179Config, timeProvider);
    when(timeProvider.currentTimeMillis()).thenReturn(10_000_000L);
    BlockHeader header = mock(BlockHeader.class);
    Block block = mock(Block.class);
    when(block.getHeader()).thenReturn(header);
    Block parent = mock(Block.class);
    when(header.getTimestamp()).thenReturn(10_000L);
    when(parent.getTimestamp()).thenReturn(10_000L - 1000);
    assertTrue(validationRule.isValid(header, parent));
}
Also used : BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 80 with BlockHeader

use of org.ethereum.core.BlockHeader 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);
    BlockStore blockStore = new IndexedBlockStore(null, new HashMapDB(), new HashMapBlocksIndex());
    blockStore.saveBlock(genesis, new BlockDifficulty(BigInteger.valueOf(1)), true);
    blockStore.saveBlock(block1, new BlockDifficulty(BigInteger.valueOf(2)), true);
    BlockUnclesValidationRule rule = new BlockUnclesValidationRule(blockStore, 10, 10, new BlockHeaderCompositeRule(), new BlockHeaderParentCompositeRule());
    Assert.assertFalse(rule.isValid(block));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) BlockHeader(org.ethereum.core.BlockHeader) 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