Search in sources :

Example 1 with BlockValidator

use of co.rsk.validators.BlockValidator in project rskj by rsksmart.

the class BlockValidatorTest method validateEmptyBlock.

@Test
public void validateEmptyBlock() {
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    Block genesis = new BlockGenerator().getGenesisBlock();
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    Block block = new BlockBuilder().parent(genesis).build();
    BlockValidator validator = createValidator(blockStore);
    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) BlockValidator(co.rsk.validators.BlockValidator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 2 with BlockValidator

use of co.rsk.validators.BlockValidator in project rskj by rsksmart.

the class BlockChainBuilder method build.

public BlockChainImpl build() {
    BlocksIndex blocksIndex = new HashMapBlocksIndex();
    if (config == null) {
        config = new TestSystemProperties();
    }
    if (trieStore == null) {
        trieStore = new TrieStoreImpl(new HashMapDB().setClearOnClose(false));
    }
    if (repository == null) {
        repository = new MutableRepository(trieStore, new Trie(trieStore));
    }
    if (stateRootHandler == null) {
        stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
    }
    if (genesis == null) {
        genesis = new BlockGenerator().getGenesisBlock();
    }
    GenesisLoaderImpl.loadGenesisInitalState(repository, genesis);
    repository.commit();
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
    if (blockStore == null) {
        blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), blocksIndex);
    }
    if (receiptStore == null) {
        KeyValueDataSource ds = new HashMapDB();
        ds.init();
        receiptStore = new ReceiptStoreImpl(ds);
    }
    if (listener == null) {
        listener = new BlockExecutorTest.SimpleEthereumListener();
    }
    if (bridgeSupportFactory == null) {
        bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
    }
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(trieStore).blockStore(blockStore);
    BlockValidator blockValidator = validatorBuilder.build();
    ReceivedTxSignatureCache receivedTxSignatureCache = new ReceivedTxSignatureCache();
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(receivedTxSignatureCache);
    TransactionExecutorFactory transactionExecutorFactory = new TransactionExecutorFactory(config, blockStore, receiptStore, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), blockTxSignatureCache);
    repositoryLocator = new RepositoryLocator(trieStore, stateRootHandler);
    transactionPool = new TransactionPoolImpl(config, repositoryLocator, this.blockStore, blockFactory, new TestCompositeEthereumListener(), transactionExecutorFactory, new ReceivedTxSignatureCache(), 10, 100);
    BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), repositoryLocator, transactionExecutorFactory);
    BlockChainImpl blockChain = new BlockChainLoader(blockStore, receiptStore, transactionPool, listener, blockValidator, blockExecutor, genesis, stateRootHandler, repositoryLocator).loadBlockchain();
    if (this.testing) {
        blockChain.setBlockValidator(new DummyBlockValidator());
        blockChain.setNoValidation(true);
    }
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    if (this.blocks != null) {
        for (Block b : this.blocks) {
            blockExecutor.executeAndFillAll(b, blockChain.getBestBlock().getHeader());
            blockChain.tryToConnect(b);
        }
    }
    return blockChain;
}
Also used : BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockValidator(co.rsk.validators.BlockValidator) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) TestCompositeEthereumListener(org.ethereum.listener.TestCompositeEthereumListener) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) Trie(co.rsk.trie.Trie) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) BlockChainLoader(org.ethereum.core.genesis.BlockChainLoader) HashMapDB(org.ethereum.datasource.HashMapDB) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) TestSystemProperties(co.rsk.config.TestSystemProperties)

Example 3 with BlockValidator

use of co.rsk.validators.BlockValidator in project rskj by rsksmart.

the class BlockChainImplInvalidTest method setup.

@Before
public void setup() {
    objects = new RskTestContext(new String[0]) {

        @Override
        protected synchronized RskSystemProperties buildRskSystemProperties() {
            RskSystemProperties rskSystemProperties = super.buildRskSystemProperties();
            ActivationConfig activationConfigSpy = spy(rskSystemProperties.getActivationConfig());
            RskSystemProperties rskSystemPropertiesSpy = spy(rskSystemProperties);
            doReturn(true).when(activationConfigSpy).isActive(eq(ConsensusRule.RSKIP126), anyLong());
            doReturn(activationConfigSpy).when(rskSystemPropertiesSpy).getActivationConfig();
            return rskSystemPropertiesSpy;
        }

        @Override
        protected GenesisLoader buildGenesisLoader() {
            return new TestGenesisLoader(getTrieStore(), "rsk-unittests.json", BigInteger.ZERO, true, true, true);
        }

        @Override
        public BlockValidator buildBlockValidator() {
            return new BlockValidatorImpl(getBlockStore(), getBlockParentDependantValidationRule(), getBlockValidationRule());
        }
    };
    blockChain = objects.getBlockchain();
}
Also used : GenesisLoader(org.ethereum.core.genesis.GenesisLoader) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RskTestContext(org.ethereum.util.RskTestContext) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RskSystemProperties(co.rsk.config.RskSystemProperties) BlockValidator(co.rsk.validators.BlockValidator) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Before(org.junit.Before)

Example 4 with BlockValidator

use of co.rsk.validators.BlockValidator in project rskj by rsksmart.

the class BlockValidatorTest method validateBlockWithTransaction.

@Test
public void validateBlockWithTransaction() {
    BlockChainImpl blockChain = BlockChainImplTest.createBlockChain();
    Block genesis = BlockChainImplTest.getGenesisBlock(blockChain);
    genesis.seal();
    Block parent = new BlockBuilder().parent(genesis).build();
    parent.seal();
    List<Transaction> txs = new ArrayList<>();
    txs.add(BlockExecutorTest.generateBlockWithOneTransaction().getTransaction());
    Block block = new BlockBuilder().parent(parent).transactions(txs).build();
    ;
    block.seal();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(parent));
    BlockValidator validator = createValidator(blockChain.getBlockStore());
    Assert.assertTrue(validator.isValid(block));
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) ArrayList(java.util.ArrayList) SimpleBlock(co.rsk.peg.simples.SimpleBlock) BlockValidator(co.rsk.validators.BlockValidator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 5 with BlockValidator

use of co.rsk.validators.BlockValidator in project rskj by rsksmart.

the class BlockValidatorTest method validateChildBlock.

@Test
public void validateChildBlock() {
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    Block genesis = new BlockGenerator().getGenesisBlock();
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    Block block = new BlockGenerator().createChildBlock(genesis);
    BlockValidator validator = createValidator(blockStore);
    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) BlockValidator(co.rsk.validators.BlockValidator) Test(org.junit.Test)

Aggregations

BlockValidator (co.rsk.validators.BlockValidator)6 HashMapDB (org.ethereum.datasource.HashMapDB)4 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 SimpleBlock (co.rsk.peg.simples.SimpleBlock)3 Test (org.junit.Test)3 BlockBuilder (co.rsk.test.builders.BlockBuilder)2 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)2 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)2 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)2 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)2 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 TestSystemProperties (co.rsk.config.TestSystemProperties)1 RskAddress (co.rsk.core.RskAddress)1 TransactionExecutorFactory (co.rsk.core.TransactionExecutorFactory)1 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 BridgeSupportFactory (co.rsk.peg.BridgeSupportFactory)1 RepositoryBlockStore (co.rsk.peg.RepositoryBlockStore)1 RemascTransaction (co.rsk.remasc.RemascTransaction)1