Search in sources :

Example 6 with IndexedBlockStore

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

the class BlockValidatorTest method invalidUncleIsAncestor.

@Test
public void invalidUncleIsAncestor() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block uncle1a = blockGenerator.createChildBlock(genesis);
    List<BlockHeader> uncles1 = new ArrayList<>();
    uncles1.add(uncle1a.getHeader());
    uncles1.add(genesis.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) 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 7 with IndexedBlockStore

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

the class BlockValidatorTest method validateHeader.

@Test
public void validateHeader() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block parent = new BlockBuilder().parent(genesis).build();
    Block block = new BlockBuilder().parent(parent).build();
    store.saveBlock(parent, TEST_DIFFICULTY, true);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addParentBlockHeaderValidator().addBlockRootValidationRule().addBlockUnclesValidationRule(null).blockStore(store).build();
    Assert.assertTrue(validator.isValid(block));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) SimpleBlock(co.rsk.peg.simples.SimpleBlock) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 8 with IndexedBlockStore

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

the class BlockValidatorTest method invalidUnclesUncleIncludedMultipeTimes.

@Test
public void invalidUnclesUncleIncludedMultipeTimes() {
    IndexedBlockStore store = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block uncle1a = blockGenerator.createChildBlock(genesis);
    List<BlockHeader> uncles1 = new ArrayList<>();
    uncles1.add(uncle1a.getHeader());
    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) 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 9 with IndexedBlockStore

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

the class BlockChainImplTest method addValidMGPBlock.

@Test
public void addValidMGPBlock() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), (DB) null);
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.blockStore(blockStore).addPrevMinGasPriceRule().addTxsMinGasPriceRule();
    BlockChainImpl blockChain = createBlockChain(repository, blockStore, validatorBuilder.build());
    Repository track = repository.startTracking();
    Account account = BlockExecutorTest.createAccount("acctest1", track, Coin.valueOf(100000));
    Assert.assertTrue(account.getEcKey().hasPrivKey());
    track.commit();
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000100", BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.valueOf(22000L));
    tx.sign(account.getEcKey().getPrivKeyBytes());
    txs.add(tx);
    Block genesis = getGenesisBlock(blockChain);
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    Block block = new BlockBuilder().minGasPrice(BigInteger.ZERO).transactions(txs).parent(genesis).build();
    BlockExecutor executor = new BlockExecutor(config, repository, null, null, null);
    executor.executeAndFill(block, genesis);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryImpl(co.rsk.db.RepositoryImpl) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 10 with IndexedBlockStore

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

the class BlockChainImplTest method addInvalidMGPBlock.

@Test
public void addInvalidMGPBlock() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(repository).addPrevMinGasPriceRule().addTxsMinGasPriceRule();
    BlockChainImpl blockChain = createBlockChain(repository, blockStore, validatorBuilder.build());
    Block genesis = getGenesisBlock(blockChain);
    Block block = new BlockBuilder().minGasPrice(BigInteger.ONE).parent(genesis).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000006", BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(1L), BigInteger.TEN);
    tx.sign(new byte[] { 22, 11, 00 });
    txs.add(tx);
    block = new BlockBuilder().transactions(txs).minGasPrice(BigInteger.valueOf(11L)).parent(genesis).build();
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryImpl(co.rsk.db.RepositoryImpl) BlockBuilder(co.rsk.test.builders.BlockBuilder) 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