Search in sources :

Example 11 with IndexedBlockStore

use of org.ethereum.db.IndexedBlockStore in project rskj by rsksmart.

the class BlockValidatorTest method invalidSiblingUncles.

@Test
public void invalidSiblingUncles() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    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) ArrayList(java.util.ArrayList) SimpleBlock(co.rsk.peg.simples.SimpleBlock) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 12 with IndexedBlockStore

use of org.ethereum.db.IndexedBlockStore in project rskj by rsksmart.

the class BlockValidatorTest method getUsedUncles.

@Test
public void getUsedUncles() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    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) ArrayList(java.util.ArrayList) SimpleBlock(co.rsk.peg.simples.SimpleBlock) Keccak256(co.rsk.crypto.Keccak256) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 13 with IndexedBlockStore

use of org.ethereum.db.IndexedBlockStore in project rskj by rsksmart.

the class BlockValidatorTest method validUncles.

@Test
public void validUncles() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    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) ArrayList(java.util.ArrayList) SimpleBlock(co.rsk.peg.simples.SimpleBlock) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 14 with IndexedBlockStore

use of org.ethereum.db.IndexedBlockStore in project rskj by rsksmart.

the class BlockValidatorTest method getThreeAncestorSet.

@Test
public void getThreeAncestorSet() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    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) SimpleBlock(co.rsk.peg.simples.SimpleBlock) Keccak256(co.rsk.crypto.Keccak256) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 15 with IndexedBlockStore

use of org.ethereum.db.IndexedBlockStore in project rskj by rsksmart.

the class BlockValidatorTest method tooManyUncles.

@Test
public void tooManyUncles() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    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) ArrayList(java.util.ArrayList) SimpleBlock(co.rsk.peg.simples.SimpleBlock) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Aggregations

HashMapDB (org.ethereum.datasource.HashMapDB)23 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)23 Test (org.junit.Test)20 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)18 SimpleBlock (co.rsk.peg.simples.SimpleBlock)18 ArrayList (java.util.ArrayList)14 BlockBuilder (co.rsk.test.builders.BlockBuilder)5 Keccak256 (co.rsk.crypto.Keccak256)3 RepositoryImpl (co.rsk.db.RepositoryImpl)3 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)3 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)2 BlockValidator (co.rsk.validators.BlockValidator)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 BlockDifficulty (co.rsk.core.BlockDifficulty)1 RskAddress (co.rsk.core.RskAddress)1 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)1 BlockParentDependantValidationRule (co.rsk.validators.BlockParentDependantValidationRule)1 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)1 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)1 GenesisConfig (org.ethereum.config.blockchain.GenesisConfig)1