Search in sources :

Example 6 with HashMapDB

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

the class ReceiptStoreImplTest method addTwoTransactionsAndGetTransactionByDescendantBlocks.

@Test
public void addTwoTransactionsAndGetTransactionByDescendantBlocks() {
    World world = new World();
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1a = new BlockBuilder().difficulty(10).parent(genesis).build();
    Block block1b = new BlockBuilder().difficulty(block1a.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
    Block block2a = new BlockBuilder().parent(block1a).build();
    Block block2b = new BlockBuilder().parent(block1b).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1a));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2a));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block2b));
    ReceiptStore store = new ReceiptStoreImpl(new HashMapDB());
    TransactionReceipt receipt0 = createReceipt();
    byte[] blockHash0 = Hex.decode("010203040506070809");
    store.add(block1a.getHash().getBytes(), 3, receipt0);
    TransactionReceipt receipt = createReceipt();
    byte[] blockHash = Hex.decode("0102030405060708");
    store.add(block1b.getHash().getBytes(), 42, receipt);
    TransactionInfo result = store.get(receipt.getTransaction().getHash().getBytes(), block2a.getHash().getBytes(), world.getBlockChain().getBlockStore());
    Assert.assertNotNull(result.getBlockHash());
    Assert.assertArrayEquals(block1a.getHash().getBytes(), result.getBlockHash());
    Assert.assertEquals(3, result.getIndex());
    Assert.assertArrayEquals(receipt.getEncoded(), result.getReceipt().getEncoded());
    result = store.get(receipt.getTransaction().getHash().getBytes(), block2b.getHash().getBytes(), world.getBlockChain().getBlockStore());
    Assert.assertNotNull(result.getBlockHash());
    Assert.assertArrayEquals(block1b.getHash().getBytes(), result.getBlockHash());
    Assert.assertEquals(42, result.getIndex());
    Assert.assertArrayEquals(receipt.getEncoded(), result.getReceipt().getEncoded());
    result = store.get(receipt.getTransaction().getHash().getBytes(), genesis.getHash().getBytes(), world.getBlockChain().getBlockStore());
    Assert.assertNull(result);
}
Also used : World(co.rsk.test.World) HashMapDB(org.ethereum.datasource.HashMapDB) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 7 with HashMapDB

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

the class ReceiptStoreImplTest method getUnknownTransactionByBlock.

@Test
public void getUnknownTransactionByBlock() {
    ReceiptStore store = new ReceiptStoreImpl(new HashMapDB());
    TransactionReceipt receipt = createReceipt();
    byte[] blockHash = Hex.decode("010203040506070809");
    TransactionInfo result = store.get(receipt.getTransaction().getHash().getBytes(), blockHash, null);
    Assert.assertNull(result);
}
Also used : HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 8 with HashMapDB

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

the class TestRunner method runTestCase.

public List<String> runTestCase(BlockTestCase testCase) {
    /* 1 */
    // Create genesis + init pre state
    Block genesis = BlockBuilder.build(testCase.getGenesisBlockHeader(), null, null);
    Repository repository = RepositoryBuilder.build(testCase.getPre());
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    EthereumListener listener = new CompositeEthereumListener();
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, null, null, new DummyBlockValidator());
    // BlockchainImpl blockchain = new BlockchainImpl(blockStore, repository, wallet, adminInfo, listener,
    // new CommonConfig().parentHeaderValidator(), receiptStore);
    blockchain.setNoValidation(true);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, null, receiptStore, null, listener, 10, 100);
    blockchain.setBestBlock(genesis);
    blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
    blockchain.setTransactionPool(transactionPool);
    /* 2 */
    // Create block traffic list
    List<Block> blockTraffic = new ArrayList<>();
    for (BlockTck blockTck : testCase.getBlocks()) {
        Block block = BlockBuilder.build(blockTck.getBlockHeader(), blockTck.getTransactions(), blockTck.getUncleHeaders());
        setNewStateRoot = !((blockTck.getTransactions() == null) && (blockTck.getUncleHeaders() == null) && (blockTck.getBlockHeader() == null));
        Block tBlock = null;
        try {
            byte[] rlp = parseData(blockTck.getRlp());
            tBlock = new Block(rlp);
            ArrayList<String> outputSummary = BlockHeaderValidator.valid(tBlock.getHeader(), block.getHeader());
            if (!outputSummary.isEmpty()) {
                for (String output : outputSummary) logger.error("at block {}: {}", Integer.toString(blockTraffic.size()), output);
            }
            blockTraffic.add(tBlock);
        } catch (Exception e) {
            System.out.println("*** Exception");
        }
    }
    // Inject blocks to the blockchain execution
    for (Block block : blockTraffic) {
        ImportResult importResult = blockchain.tryToConnect(block);
        logger.debug("{} ~ {} difficulty: {} ::: {}", block.getShortHash(), shortHash(block.getParentHash().getBytes()), block.getCumulativeDifficulty(), importResult.toString());
    }
    // Check state root matches last valid block
    List<String> results = new ArrayList<>();
    String currRoot = Hex.toHexString(repository.getRoot());
    byte[] bestHash = Hex.decode(testCase.getLastblockhash());
    String finalRoot = Hex.toHexString(blockStore.getBlockByHash(bestHash).getStateRoot());
    /*
        if (!blockchain.byTest) // If this comes from ETH, it won't match
        if (!finalRoot.equals(currRoot)){
            String formattedString = String.format("Root hash doesn't match best: expected: %s current: %s",
                    finalRoot, currRoot);
            results.add(formattedString);
        }
        */
    Repository postRepository = RepositoryBuilder.build(testCase.getPostState());
    List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false);
    results.addAll(repoResults);
    return results;
}
Also used : CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) EthereumListener(org.ethereum.listener.EthereumListener) ImportResult(org.ethereum.core.ImportResult) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) IOException(java.io.IOException) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) Repository(org.ethereum.core.Repository) BlockTck(org.ethereum.jsontestsuite.model.BlockTck) CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) Block(org.ethereum.core.Block) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource)

Example 9 with HashMapDB

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

the class StateTestRunner method runImpl.

public List<String> runImpl() {
    logger.info("");
    repository = RepositoryBuilder.build(stateTestCase.getPre());
    logger.info("loaded repository");
    transaction = TransactionBuilder.build(stateTestCase.getTransaction());
    logger.info("transaction: {}", transaction.toString());
    BlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    blockchain = new BlockChainImpl(config, repository, blockStore, null, null, null, null, null);
    env = EnvBuilder.build(stateTestCase.getEnv());
    invokeFactory = new TestProgramInvokeFactory(env);
    block = BlockBuilder.build(env);
    block.setStateRoot(repository.getRoot());
    block.flushRLP();
    blockchain.setBestBlock(block);
    // blockchain.setProgramInvokeFactory(invokeFactory);
    // blockchain.startTracking();
    ProgramResult programResult = executeTransaction(transaction);
    repository.flushNoReconnect();
    List<LogInfo> origLogs = programResult.getLogInfoList();
    List<LogInfo> postLogs = LogBuilder.build(stateTestCase.getLogs());
    List<String> logsResult = LogsValidator.valid(origLogs, postLogs);
    Repository postRepository = RepositoryBuilder.build(stateTestCase.getPost());
    List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false);
    logger.info("--------- POST Validation---------");
    List<String> outputResults = OutputValidator.valid(Hex.toHexString(programResult.getHReturn()), stateTestCase.getOut());
    List<String> results = new ArrayList<>();
    results.addAll(repoResults);
    results.addAll(logsResult);
    results.addAll(outputResults);
    for (String result : results) {
        logger.error(result);
    }
    logger.info("\n\n");
    return results;
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) LogInfo(org.ethereum.vm.LogInfo) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ProgramResult(org.ethereum.vm.program.ProgramResult) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) TestProgramInvokeFactory(org.ethereum.jsontestsuite.TestProgramInvokeFactory) Repository(org.ethereum.core.Repository)

Example 10 with HashMapDB

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

Aggregations

HashMapDB (org.ethereum.datasource.HashMapDB)141 Test (org.junit.Test)130 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)28 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)23 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)22 SimpleBlock (co.rsk.peg.simples.SimpleBlock)18 ArrayList (java.util.ArrayList)18 RepositoryImpl (co.rsk.db.RepositoryImpl)15 DataWord (org.ethereum.vm.DataWord)14 Repository (org.ethereum.core.Repository)12 RskAddress (co.rsk.core.RskAddress)11 TrieStore (co.rsk.trie.TrieStore)10 BlockBuilder (co.rsk.test.builders.BlockBuilder)9 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)8 ReceiptStore (org.ethereum.db.ReceiptStore)8 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)8 Keccak256 (co.rsk.crypto.Keccak256)7 World (co.rsk.test.World)7 TrieImpl (co.rsk.trie.TrieImpl)6 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)6