Search in sources :

Example 6 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplLogsTest method getWeb3WithContractInvoke.

private Web3Impl getWeb3WithContractInvoke() {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    Block genesis = world.getBlockByName("g00");
    Transaction tx;
    tx = getContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    BlockChainImpl blockChain = world.getBlockChain();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    byte[] contractAddress = tx.getContractAddress().getBytes();
    Transaction tx2 = getContractTransactionWithInvoke(acc1, contractAddress);
    List<Transaction> tx2s = new ArrayList<>();
    tx2s.add(tx2);
    Block block2 = new BlockBuilder(world).parent(block1).transactions(tx2s).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2));
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), receiptStore, null, null, 10, 100);
    Web3Impl web3 = createWeb3(world.getBlockChain(), transactionPool, receiptStore);
    web3.personal_newAccountWithSeed("default");
    web3.personal_newAccountWithSeed("notDefault");
    return web3;
}
Also used : ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) ReceiptStore(org.ethereum.db.ReceiptStore) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 7 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplLogsTest method getWeb3WithContractCall.

private Web3Impl getWeb3WithContractCall(World world) {
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    // acc1 Account created address should be 661b05ca9eb621164906671efd2731ce0d7dd8b4
    Block genesis = world.getBlockByName("g00");
    Transaction tx;
    tx = getContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    BlockChainImpl blockChain = world.getBlockChain();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    byte[] contractAddress = tx.getContractAddress().getBytes();
    // Now create a transaction that invokes Increment()
    Transaction tx2 = getContractTransactionWithInvoke(acc1, contractAddress);
    List<Transaction> tx2s = new ArrayList<>();
    tx2s.add(tx2);
    Block block2 = new BlockBuilder(world).parent(block1).transactions(tx2s).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2));
    Transaction tx3 = getContractTransactionWithCall(acc1, contractAddress);
    List<Transaction> tx3s = new ArrayList<>();
    tx3s.add(tx3);
    Block block3 = new BlockBuilder(world).parent(block2).transactions(tx3s).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block3));
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), null, null, null, 10, 100);
    Web3Impl web3 = createWeb3(world.getBlockChain(), transactionPool);
    web3.personal_newAccountWithSeed("default");
    web3.personal_newAccountWithSeed("notDefault");
    return web3;
}
Also used : TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 8 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class BlockUnclesValidationRuleTest method rejectBlockWithSiblingUncle.

@Test
public void rejectBlockWithSiblingUncle() {
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block uncle = blockGenerator.createChildBlock(block1);
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle.getHeader());
    Block block = blockGenerator.createChildBlock(block1, null, uncles, 1, null);
    BlockChainImpl blockChain = BlockChainImplTest.createBlockChain();
    BlockStore store = blockChain.getBlockStore();
    store.saveBlock(genesis, new BlockDifficulty(BigInteger.valueOf(1)), true);
    store.saveBlock(block1, new BlockDifficulty(BigInteger.valueOf(2)), true);
    BlockUnclesValidationRule rule = new BlockUnclesValidationRule(config, store, 10, 10, new BlockCompositeRule(), new BlockParentCompositeRule());
    Assert.assertFalse(rule.isValid(block));
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockStore(org.ethereum.db.BlockStore) ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 9 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class ImportLightTest method createBlockchain.

public static BlockChainImpl createBlockchain(Genesis genesis) {
    RskSystemProperties config = new RskSystemProperties();
    config.setBlockchainConfig(new GenesisConfig(new GenesisConfig.GenesisConstants() {

        @Override
        public BlockDifficulty getMinimumDifficulty() {
            return new BlockDifficulty(BigInteger.ONE);
        }
    }));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    EthereumListenerAdapter listener = new EthereumListenerAdapter();
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, listener, new AdminInfo(), new DummyBlockValidator());
    blockchain.setNoValidation(true);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, null, receiptStore, null, listener, 10, 100);
    blockchain.setTransactionPool(transactionPool);
    Repository track = repository.startTracking();
    for (RskAddress addr : genesis.getPremine().keySet()) {
        track.createAccount(addr);
        track.addBalance(addr, genesis.getPremine().get(addr).getAccountState().getBalance());
    }
    track.commit();
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    blockchain.setBestBlock(genesis);
    blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
    return blockchain;
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) AdminInfo(org.ethereum.manager.AdminInfo) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) EthereumListenerAdapter(org.ethereum.listener.EthereumListenerAdapter) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) BlockDifficulty(co.rsk.core.BlockDifficulty) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) RskSystemProperties(co.rsk.config.RskSystemProperties) ReceiptStore(org.ethereum.db.ReceiptStore) GenesisConfig(org.ethereum.config.blockchain.GenesisConfig)

Example 10 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class RemascTestRunner method start.

public void start() {
    this.blockchain = this.builder.build();
    ((BlockChainImpl) this.blockchain).setNoValidation(true);
    this.addedSiblings = new ArrayList<>();
    List<Block> mainChainBlocks = new ArrayList<>();
    this.blockchain.tryToConnect(this.genesis);
    BlockExecutor blockExecutor = new BlockExecutor(new RskSystemProperties(), blockchain.getRepository(), null, blockchain.getBlockStore(), null);
    for (int i = 0; i <= this.initialHeight; i++) {
        int finalI = i;
        List<SiblingElement> siblingsForCurrentHeight = this.siblingElements.stream().filter(siblingElement -> siblingElement.getHeightToBeIncluded() == finalI).collect(Collectors.toList());
        List<BlockHeader> blockSiblings = new ArrayList<>();
        // Going to add siblings
        BlockDifficulty cummDifficulty = BlockDifficulty.ZERO;
        if (siblingsForCurrentHeight.size() > 0) {
            cummDifficulty = blockchain.getTotalDifficulty();
        }
        for (SiblingElement sibling : siblingsForCurrentHeight) {
            RskAddress siblingCoinbase = TestUtils.randomAddress();
            Block mainchainSiblingParent = mainChainBlocks.get(sibling.getHeight() - 1);
            Block siblingBlock = createBlock(this.genesis, mainchainSiblingParent, PegTestUtils.createHash3(), siblingCoinbase, null, minerFee, Long.valueOf(i), this.txValue, this.txSigningKey, null);
            blockSiblings.add(siblingBlock.getHeader());
            blockchain.getBlockStore().saveBlock(siblingBlock, cummDifficulty.add(siblingBlock.getCumulativeDifficulty()), false);
            this.addedSiblings.add(siblingBlock);
        }
        long txNonce = i;
        RskAddress coinbase = TestUtils.randomAddress();
        Block block = createBlock(this.genesis, this.blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, blockSiblings, minerFee, txNonce, this.txValue, this.txSigningKey, null);
        mainChainBlocks.add(block);
        blockExecutor.executeAndFillAll(block, this.blockchain.getBestBlock());
        block.seal();
        ImportResult result = this.blockchain.tryToConnect(block);
        System.out.println(result);
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) TestUtils(org.ethereum.TestUtils) HashUtil(org.ethereum.crypto.HashUtil) RLP(org.ethereum.util.RLP) RskAddress(co.rsk.core.RskAddress) BlockExecutor(co.rsk.core.bc.BlockExecutor) Coin(co.rsk.core.Coin) PegTestUtils(co.rsk.peg.PegTestUtils) Keccak256(co.rsk.crypto.Keccak256) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) BigInteger(java.math.BigInteger) RskSystemProperties(co.rsk.config.RskSystemProperties) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) org.ethereum.core(org.ethereum.core) ECKey(org.ethereum.crypto.ECKey) BlockExecutor(co.rsk.core.bc.BlockExecutor) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) BlockDifficulty(co.rsk.core.BlockDifficulty) RskAddress(co.rsk.core.RskAddress) RskSystemProperties(co.rsk.config.RskSystemProperties)

Aggregations

BlockChainImpl (co.rsk.core.bc.BlockChainImpl)28 Test (org.junit.Test)15 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)14 ArrayList (java.util.ArrayList)14 World (co.rsk.test.World)11 BlockBuilder (co.rsk.test.builders.BlockBuilder)11 AccountBuilder (co.rsk.test.builders.AccountBuilder)10 BlockDifficulty (co.rsk.core.BlockDifficulty)7 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 Block (org.ethereum.core.Block)6 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)4 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)4 BigInteger (java.math.BigInteger)4 BlockHeader (org.ethereum.core.BlockHeader)4 HashMapDB (org.ethereum.datasource.HashMapDB)4 RskAddress (co.rsk.core.RskAddress)3 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)3 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)3 BlockStore (org.ethereum.db.BlockStore)3 SimpleEthereum (org.ethereum.rpc.Simples.SimpleEthereum)3