Search in sources :

Example 66 with HashMapDB

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

the class BlockValidatorTest method invalidUncleHasNoSavedParent.

@Test
public void invalidUncleHasNoSavedParent() {
    IndexedBlockStore store = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block uncle1a = blockGenerator.createChildBlock(new BlockGenerator().createChildBlock(genesis));
    List<BlockHeader> uncles1 = new ArrayList<>();
    uncles1.add(uncle1a.getHeader());
    Block block1 = blockGenerator.createChildBlock(genesis, null, uncles1, 1, null);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    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) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 67 with HashMapDB

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

the class RepositoryImplOriginalTest method testMultiThread.

// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    MutableTrieImpl mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
    final Repository repository = new MutableRepository(mutableTrie);
    final DataWord cowKey1 = DataWord.valueFromHex("c1");
    final DataWord cowKey2 = DataWord.valueFromHex("c2");
    final DataWord cowVal0 = DataWord.valueFromHex("c0a0");
    Repository track2 = repository.startTracking();
    track2.addStorageRow(COW, cowKey2, cowVal0);
    track2.commit();
    // Changes commited to repository
    assertThat(repository.getStorageValue(COW, cowKey2), is(cowVal0));
    final CountDownLatch failSema = new CountDownLatch(1);
    // First create the 10 snapshots. The snapshots should not be created while the
    // repository is being changed.
    Repository[] snaps = new Repository[10];
    for (int i = 0; i < 10; ++i) {
        snaps[i] = new MutableRepository(trieStore, trieStore.retrieve(repository.getRoot()).get());
    }
    for (int i = 0; i < 10; ++i) {
        int finalI = i;
        new Thread(() -> {
            try {
                int cnt = 1;
                while (running) {
                    Repository snap = snaps[finalI].startTracking();
                    snap.addBalance(COW, Coin.valueOf(10L));
                    snap.addStorageRow(COW, cowKey1, DataWord.valueOf(cnt));
                    snap.rollback();
                    cnt++;
                }
            } catch (Throwable e) {
                e.printStackTrace();
                failSema.countDown();
            }
        }).start();
    }
    new Thread(() -> {
        int cnt = 1;
        try {
            while (running) {
                Repository track21 = repository.startTracking();
                DataWord cVal = DataWord.valueOf(cnt);
                track21.addStorageRow(COW, cowKey1, cVal);
                track21.addBalance(COW, Coin.valueOf(1L));
                track21.commit();
                assertEquals(BigInteger.valueOf(cnt), repository.getBalance(COW).asBigInteger());
                assertEquals(cVal, repository.getStorageValue(COW, cowKey1));
                assertEquals(cowVal0, repository.getStorageValue(COW, cowKey2));
                cnt++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            try {
                repository.addStorageRow(COW, cowKey1, DataWord.valueOf(123));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            failSema.countDown();
        }
    }).start();
    failSema.await(10, TimeUnit.SECONDS);
    running = false;
    if (failSema.getCount() == 0) {
        throw new RuntimeException("Test failed.");
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) CountDownLatch(java.util.concurrent.CountDownLatch) TrieStore(co.rsk.trie.TrieStore) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 68 with HashMapDB

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

the class DebugModuleImplTest method debug_traceTransaction_retrieveSimpleContractCreationTrace.

@Test
public void debug_traceTransaction_retrieveSimpleContractCreationTrace() throws Exception {
    DslParser parser = DslParser.fromResource("dsl/contracts01.txt");
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    WorldDslProcessor processor = new WorldDslProcessor(world);
    processor.processCommands(parser);
    Transaction transaction = world.getTransactionByName("tx01");
    DebugModuleImpl debugModule = new DebugModuleImpl(world.getBlockStore(), receiptStore, messageHandler, world.getBlockExecutor());
    JsonNode result = debugModule.traceTransaction(transaction.getHash().toJsonString(), null);
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isObject());
    ObjectNode oResult = (ObjectNode) result;
    Assert.assertTrue(oResult.get("error").textValue().isEmpty());
    Assert.assertTrue(oResult.get("result").isTextual());
    JsonNode structLogs = oResult.get("structLogs");
    Assert.assertTrue(structLogs.isArray());
    Assert.assertTrue(structLogs.size() > 0);
}
Also used : ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) Transaction(org.ethereum.core.Transaction) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DslParser(co.rsk.test.dsl.DslParser) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 69 with HashMapDB

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

the class TraceModuleImplTest method executeContractWithDelegateCall.

@Test
public void executeContractWithDelegateCall() throws Exception {
    DslParser parser = DslParser.fromResource("dsl/delegatecall01.txt");
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    WorldDslProcessor processor = new WorldDslProcessor(world);
    processor.processCommands(parser);
    Account delegateCallAccount = world.getAccountByName("delegatecall");
    Account delegatedAccount = world.getAccountByName("delegated");
    Assert.assertNotNull(delegateCallAccount);
    Assert.assertNotNull(delegatedAccount);
    Transaction transaction = world.getTransactionByName("tx01");
    TraceModuleImpl traceModule = new TraceModuleImpl(world.getBlockChain(), world.getBlockStore(), receiptStore, world.getBlockExecutor());
    JsonNode result = traceModule.traceTransaction(transaction.getHash().toJsonString());
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isArray());
    ArrayNode aresult = (ArrayNode) result;
    Assert.assertEquals(2, aresult.size());
    Assert.assertTrue(result.get(0).isObject());
    ObjectNode oresult = (ObjectNode) result.get(1);
    Assert.assertNotNull(oresult.get("action"));
    Assert.assertNotNull(oresult.get("action").get("callType"));
    Assert.assertEquals("\"delegatecall\"", oresult.get("action").get("callType").toString());
    Assert.assertEquals("\"" + delegatedAccount.getAddress().toJsonString() + "\"", oresult.get("action").get("to").toString());
    Assert.assertEquals("\"" + delegateCallAccount.getAddress().toJsonString() + "\"", oresult.get("action").get("from").toString());
}
Also used : ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) Account(org.ethereum.core.Account) Transaction(org.ethereum.core.Transaction) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DslParser(co.rsk.test.dsl.DslParser) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 70 with HashMapDB

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

the class TraceModuleImplTest method retrieveEmptyContractCreationTrace.

@Test
public void retrieveEmptyContractCreationTrace() throws Exception {
    DslParser parser = DslParser.fromResource("dsl/contracts09.txt");
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    WorldDslProcessor processor = new WorldDslProcessor(world);
    processor.processCommands(parser);
    Transaction transaction = world.getTransactionByName("tx01");
    TraceModuleImpl traceModule = new TraceModuleImpl(world.getBlockChain(), world.getBlockStore(), receiptStore, world.getBlockExecutor());
    JsonNode result = traceModule.traceTransaction(transaction.getHash().toJsonString());
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isArray());
    ArrayNode aresult = (ArrayNode) result;
    Assert.assertEquals(1, aresult.size());
    Assert.assertTrue(result.get(0).isObject());
    ObjectNode oresult = (ObjectNode) result.get(0);
    Assert.assertNotNull(oresult.get("type"));
    Assert.assertEquals("\"create\"", oresult.get("type").toString());
}
Also used : ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) Transaction(org.ethereum.core.Transaction) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DslParser(co.rsk.test.dsl.DslParser) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) 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