Search in sources :

Example 21 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class BlockValidatorTest method invalidUncleAlreadyUsed.

@Test
public void invalidUncleAlreadyUsed() {
    IndexedBlockStore store = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block uncle1a = blockGenerator.createChildBlock(genesis);
    Block uncle1b = blockGenerator.createChildBlock(genesis);
    List<BlockHeader> uncles1 = new ArrayList<>();
    uncles1.add(uncle1a.getHeader());
    uncles1.add(uncle1b.getHeader());
    Block block1 = blockGenerator.createChildBlock(genesis, null, uncles1, 1, null);
    Block uncle2a = blockGenerator.createChildBlock(genesis);
    Block uncle2b = blockGenerator.createChildBlock(genesis);
    List<BlockHeader> uncles2 = new ArrayList<>();
    uncles2.add(uncle2a.getHeader());
    uncles2.add(uncle2b.getHeader());
    Block block2 = blockGenerator.createChildBlock(block1, null, uncles2, 1, null);
    Block block3 = blockGenerator.createChildBlock(block2, null, uncles2, 1, null);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    store.saveBlock(uncle1a, TEST_DIFFICULTY, false);
    store.saveBlock(uncle1b, TEST_DIFFICULTY, false);
    store.saveBlock(block1, TEST_DIFFICULTY, true);
    store.saveBlock(uncle2a, TEST_DIFFICULTY, false);
    store.saveBlock(uncle2b, TEST_DIFFICULTY, false);
    store.saveBlock(block2, TEST_DIFFICULTY, true);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addBlockUnclesValidationRule(store).blockStore(store).build();
    Assert.assertFalse(validator.isValid(block3));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 22 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class BlockValidatorTest method invalidUncleHasNoCommonAncestor.

@Test
public void invalidUncleHasNoCommonAncestor() {
    IndexedBlockStore store = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    BlockHeader newHeader = blockFactory.getBlockHeaderBuilder().setCoinbase(TestUtils.randomAddress()).setDifficulty(TEST_DIFFICULTY).setEmptyMergedMiningForkDetectionData().setMinimumGasPrice(Coin.valueOf(10)).build();
    Block uncle1a = blockGenerator.createChildBlock(blockFactory.newBlock(newHeader, Collections.emptyList(), Collections.emptyList()));
    List<BlockHeader> uncles1 = new ArrayList<>();
    uncles1.add(uncle1a.getHeader());
    Block block1 = blockGenerator.createChildBlock(genesis, null, uncles1, 1, null);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    store.saveBlock(uncle1a, TEST_DIFFICULTY, false);
    store.saveBlock(block1, TEST_DIFFICULTY, true);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addBlockUnclesValidationRule(store).blockStore(store).build();
    Assert.assertFalse(validator.isValid(block1));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 23 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class BlockValidatorTest method getThreeAncestorSet.

@Test
public void getThreeAncestorSet() {
    IndexedBlockStore store = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    Block block1 = blockGenerator.createChildBlock(genesis);
    store.saveBlock(block1, TEST_DIFFICULTY, true);
    Block block2 = blockGenerator.createChildBlock(block1);
    store.saveBlock(block2, TEST_DIFFICULTY, true);
    Block block3 = blockGenerator.createChildBlock(block2);
    store.saveBlock(block3, TEST_DIFFICULTY, true);
    Block block4 = blockGenerator.createChildBlock(block3);
    store.saveBlock(block4, TEST_DIFFICULTY, true);
    Block block5 = blockGenerator.createChildBlock(block4);
    store.saveBlock(block5, TEST_DIFFICULTY, true);
    Set<Keccak256> ancestors = FamilyUtils.getAncestors(store, block5, 3);
    Assert.assertFalse(ancestors.isEmpty());
    Assert.assertEquals(3, ancestors.size());
    Assert.assertFalse(ancestors.contains(genesis.getHash()));
    Assert.assertFalse(ancestors.contains(block1.getHash()));
    Assert.assertTrue(ancestors.contains(block2.getHash()));
    Assert.assertTrue(ancestors.contains(block3.getHash()));
    Assert.assertTrue(ancestors.contains(block4.getHash()));
    Assert.assertFalse(ancestors.contains(block5.getHash()));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) Keccak256(co.rsk.crypto.Keccak256) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 24 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class BlockUnclesValidationRuleTest method rejectBlockWithUncleHavingHigherNumber.

@Test
public void rejectBlockWithUncleHavingHigherNumber() {
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block uncle1 = blockGenerator.createChildBlock(block1);
    Block uncle2 = blockGenerator.createChildBlock(uncle1);
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle2.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

HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)24 HashMapDB (org.ethereum.datasource.HashMapDB)23 Test (org.junit.Test)23 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)19 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)13 Block (org.ethereum.core.Block)8 BlockDifficulty (co.rsk.core.BlockDifficulty)5 Keccak256 (co.rsk.crypto.Keccak256)5 Ignore (org.junit.Ignore)4 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)3 ArrayList (java.util.ArrayList)3 BlockStore (org.ethereum.db.BlockStore)3 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)2 MutableTrieImpl (co.rsk.db.MutableTrieImpl)2 RepositoryLocator (co.rsk.db.RepositoryLocator)2 Trie (co.rsk.trie.Trie)2 TrieStore (co.rsk.trie.TrieStore)2 BigInteger (java.math.BigInteger)2 BlockFactory (org.ethereum.core.BlockFactory)2