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));
}
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));
}
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()));
}
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));
}
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));
}
Aggregations