Search in sources :

Example 16 with HashMapBlocksIndex

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

the class BlockValidatorTest method invalidPOWUncles.

@Test
public void invalidPOWUncles() {
    IndexedBlockStore store = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockGenerator blockGenerator = new BlockGenerator();
    Blockchain blockchain = new BlockChainBuilder().ofSize(30, true);
    Block genesis = blockchain.getBlockByNumber(0);
    Block uncle1a = blockchain.getBlockByNumber(1);
    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);
    BlockHeaderParentDependantValidationRule parentValidationRule = mock(BlockHeaderParentDependantValidationRule.class);
    when(parentValidationRule.isValid(Mockito.any(), Mockito.any())).thenReturn(true);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addBlockUnclesValidationRule(store, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), parentValidationRule).blockStore(store).build();
    Assert.assertFalse(validator.isValid(block1));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockHeaderParentDependantValidationRule(co.rsk.validators.BlockHeaderParentDependantValidationRule) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) Test(org.junit.Test)

Example 17 with HashMapBlocksIndex

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

the class BlockValidatorTest method tooManyUncles.

@Test
public void tooManyUncles() {
    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);
    Block block1 = blockGenerator.createChildBlock(genesis, null, null, 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());
    uncles2.add(uncle1a.getHeader());
    uncles2.add(uncle1b.getHeader());
    for (int i = 0; i < 10; i++) {
        uncles2.add(blockGenerator.createChildBlock(genesis).getHeader());
    }
    Block block2 = blockGenerator.createChildBlock(block1, 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);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addBlockUnclesValidationRule(store).blockStore(store).build();
    Assert.assertFalse(validator.isValid(block2));
}
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 18 with HashMapBlocksIndex

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

the class BlockValidatorTest method getUsedUncles.

@Test
public void getUsedUncles() {
    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);
    Set<Keccak256> used = FamilyUtils.getUsedUncles(store, block3, 6);
    Assert.assertFalse(used.isEmpty());
    Assert.assertFalse(used.contains(block3.getHash()));
    Assert.assertFalse(used.contains(block2.getHash()));
    Assert.assertTrue(used.contains(uncle2a.getHash()));
    Assert.assertTrue(used.contains(uncle2b.getHash()));
    Assert.assertFalse(used.contains(block1.getHash()));
    Assert.assertTrue(used.contains(uncle1a.getHash()));
    Assert.assertTrue(used.contains(uncle1b.getHash()));
    Assert.assertFalse(used.contains(genesis.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 19 with HashMapBlocksIndex

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

the class BlockValidatorTest method validUncles.

@Test
public void validUncles() {
    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);
    Block block2 = blockGenerator.createChildBlock(block1, null, uncles1, 1, null);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    store.saveBlock(block1, TEST_DIFFICULTY, true);
    store.saveBlock(block2, TEST_DIFFICULTY, true);
    store.saveBlock(uncle1a, TEST_DIFFICULTY, false);
    store.saveBlock(uncle1b, TEST_DIFFICULTY, false);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addBlockUnclesValidationRule(store).blockStore(store).build();
    Assert.assertTrue(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 20 with HashMapBlocksIndex

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

the class BlockValidatorTest method invalidSiblingUncles.

@Test
public void invalidSiblingUncles() {
    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);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    store.saveBlock(uncle1a, TEST_DIFFICULTY, false);
    store.saveBlock(uncle1b, 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)

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