Search in sources :

Example 46 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class BlockChainImplTest method createBlockChain.

private static BlockChainImpl createBlockChain(Repository repository, IndexedBlockStore blockStore, BlockValidatorImpl blockValidator) {
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    AdminInfo adminInfo = new SimpleAdminInfo();
    EthereumListener listener = new BlockExecutorTest.SimpleEthereumListener();
    BlockChainImpl blockChain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, listener, adminInfo, blockValidator);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, blockStore, receiptStore, null, listener, 10, 100);
    blockChain.setTransactionPool(transactionPool);
    return blockChain;
}
Also used : ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) EthereumListener(org.ethereum.listener.EthereumListener) AdminInfo(org.ethereum.manager.AdminInfo) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) ReceiptStore(org.ethereum.db.ReceiptStore)

Example 47 with HashMapDB

use of org.ethereum.datasource.HashMapDB 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)

Example 48 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class TransactionModuleTest method sendSeveralTransactionsWithAutoMining.

/**
 * This test send a several transactions, and should be mine 1 transaction in each block.
 */
@Test
public void sendSeveralTransactionsWithAutoMining() {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    BlockChainImpl blockchain = world.getBlockChain();
    MiningMainchainView mainchainView = new MiningMainchainViewImpl(world.getBlockStore(), 1);
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    BlockStore blockStore = world.getBlockStore();
    TransactionPool transactionPool = world.getTransactionPool();
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironment(blockchain, mainchainView, receiptStore, transactionPool, blockStore, true, createStateRootHandler(), repositoryLocator, world.getBlockTxSignatureCache(), transactionGateway);
    for (int i = 1; i < 100; i++) {
        String tx = sendTransaction(web3, repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader()));
        // The goal of this test is transaction testing and not block mining testing
        // Hence, there is no setup for listeners and best blocks must be added manually
        // to mainchain view object that is used by miner server to build new blocks.
        mainchainView.addBest(blockchain.getBestBlock().getHeader());
        Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, tx);
        Assert.assertEquals(i, blockchain.getBestBlock().getNumber());
        Assert.assertEquals(2, blockchain.getBestBlock().getTransactionsList().size());
        Assert.assertEquals(tx, txInBlock.getHash().toJsonString());
    }
}
Also used : BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 49 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class SyncProcessorTest method processBodyResponseWithTransactionAddsToBlockchain.

@Test
public void processBodyResponseWithTransactionAddsToBlockchain() {
    Account senderAccount = createAccount("sender");
    Account receiverAccount = createAccount("receiver");
    Map<RskAddress, AccountState> accounts = new HashMap<>();
    accounts.put(senderAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.valueOf(20000000)));
    accounts.put(receiverAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.ZERO));
    final NetBlockStore store = new NetBlockStore();
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    Blockchain blockchain = blockChainBuilder.ofSize(0, false, accounts);
    Block genesis = blockchain.getBestBlock();
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    List<Transaction> txs = Collections.singletonList(createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO));
    Block block = new BlockGenerator().createChildBlock(genesis, txs, blockChainBuilder.getRepository().getRoot());
    StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
    BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(blockChainBuilder.getTrieStore(), stateRootHandler), new TransactionExecutorFactory(config, blockChainBuilder.getBlockStore(), null, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
    Assert.assertEquals(1, block.getTransactionsList().size());
    blockExecutor.executeAndFillAll(block, genesis.getHeader());
    Assert.assertEquals(21000, block.getFeesPaidToMiner().asBigInteger().intValueExact());
    Assert.assertEquals(1, block.getTransactionsList().size());
    Assert.assertEquals(1, block.getNumber());
    Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    List<Transaction> transactions = block.getTransactionsList();
    List<BlockHeader> uncles = block.getUncleList();
    long lastRequestId = new Random().nextLong();
    BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
    processor.registerExpectedMessage(response);
    Deque<BlockHeader> headerStack = new ArrayDeque<>();
    headerStack.add(block.getHeader());
    List<Deque<BlockHeader>> headers = new ArrayList<>();
    headers.add(headerStack);
    List<BlockIdentifier> bids = new ArrayList<>();
    bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
    bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
    processor.startDownloadingBodies(headers, Collections.singletonMap(sender, bids), sender);
    ((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
    processor.processBodyResponse(sender, response);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Also used : StateRootsStoreImpl(co.rsk.db.StateRootsStoreImpl) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) EthereumListener(org.ethereum.listener.EthereumListener) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) StateRootHandler(co.rsk.db.StateRootHandler) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) RepositoryLocator(co.rsk.db.RepositoryLocator) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) BlockExecutor(co.rsk.core.bc.BlockExecutor) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 50 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class TransactionModuleTest method sendRawTransactionWithAutoMining.

@Test
public void sendRawTransactionWithAutoMining() throws Exception {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    BlockChainImpl blockchain = world.getBlockChain();
    TrieStore trieStore = world.getTrieStore();
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    BlockStore blockStore = world.getBlockStore();
    TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, receiptStore, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironment(blockchain, receiptStore, trieStore, transactionPool, blockStore, true, world.getBlockTxSignatureCache(), transactionGateway);
    String txHash = sendRawTransaction(web3);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(2, blockchain.getBestBlock().getTransactionsList().size());
    Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, txHash);
    // Transaction tx must be in the block mined.
    Assert.assertEquals(txHash, txInBlock.getHash().toJsonString());
}
Also used : BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Aggregations

HashMapDB (org.ethereum.datasource.HashMapDB)208 Test (org.junit.Test)181 World (co.rsk.test.World)45 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)34 ReceiptStore (org.ethereum.db.ReceiptStore)34 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)34 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)29 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)27 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)27 TrieStore (co.rsk.trie.TrieStore)26 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)23 Trie (co.rsk.trie.Trie)21 Blockchain (org.ethereum.core.Blockchain)17 RskAddress (co.rsk.core.RskAddress)16 Transaction (org.ethereum.core.Transaction)16 DslParser (co.rsk.test.dsl.DslParser)15 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)13 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)12