Search in sources :

Example 1 with MockDB

use of org.aion.db.impl.mockdb.MockDB in project aion by aionnetwork.

the class DataSourceArrayTest method beforeTest.

@Before
public void beforeTest() {
    db = new MockDB("test_database");
    db.open();
    testIndex = new DataSourceArray<>(new ObjectDataSource<>(db, BLOCK_INFO_SERIALIZER));
}
Also used : MockDB(org.aion.db.impl.mockdb.MockDB) Before(org.junit.Before)

Example 2 with MockDB

use of org.aion.db.impl.mockdb.MockDB in project aion by aionnetwork.

the class TrieTest method TestTrieReset.

@Test
public void TestTrieReset() {
    TrieImpl trie = new TrieImpl(new MockDB("TestTrieReset"));
    trie.update(cat, LONG_STRING);
    assertNotEquals("Expected cached nodes", 0, trie.getCache().getNodes().size());
    trie.getCache().undo();
    assertEquals("Expected no nodes after undo", 0, trie.getCache().getNodes().size());
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) MockDB(org.aion.db.impl.mockdb.MockDB) Test(org.junit.Test)

Example 3 with MockDB

use of org.aion.db.impl.mockdb.MockDB in project aion by aionnetwork.

the class TrieTest method testDeleteAll.

/**
 * @param pairs
 * @implNote By design the keys are distinct due to the use of HashMap.
 */
@Test
@Parameters(method = "keyValuePairsParameters")
public void testDeleteAll(HashMap<String, String> pairs) {
    boolean print = false;
    if (print) {
        System.out.println("Number of pairs = " + pairs.size());
    }
    TrieImpl trie = new TrieImpl(new MockDB("TestDeleteAll"));
    // empty at start
    assertThat(Hex.toHexString(trie.getRootHash())).isEqualTo(ROOT_HASH_EMPTY);
    String key, value;
    for (Map.Entry<String, String> entry : pairs.entrySet()) {
        key = entry.getKey();
        value = entry.getValue();
        if (print) {
            System.out.println("(" + key + "," + value + ")");
        }
        // insert (key,value)
        trie.update(key, value);
    }
    // not empty after inserts
    assertThat(Hex.toHexString(trie.getRootHash())).isNotEqualTo(ROOT_HASH_EMPTY);
    // ensure that everything is still there
    for (String deleteKey : pairs.keySet()) {
        trie.delete(deleteKey);
    }
    // empty at end
    assertThat(Hex.toHexString(trie.getRootHash())).isEqualTo(ROOT_HASH_EMPTY);
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) MockDB(org.aion.db.impl.mockdb.MockDB) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 4 with MockDB

use of org.aion.db.impl.mockdb.MockDB in project aion by aionnetwork.

the class TrieTestWithRootHashValues method storageHashCalc_1.

@Test
public void storageHashCalc_1() {
    byte[] key1 = Hex.decode("0000000000000000000000000000000000000000000000000000000000000010");
    byte[] key2 = Hex.decode("0000000000000000000000000000000000000000000000000000000000000014");
    byte[] key3 = Hex.decode("0000000000000000000000000000000000000000000000000000000000000016");
    byte[] key4 = Hex.decode("0000000000000000000000000000000000000000000000000000000000000017");
    byte[] val1 = Hex.decode("947e70f9460402290a3e487dae01f610a1a8218fda");
    byte[] val2 = Hex.decode("40");
    byte[] val3 = Hex.decode("94412e0c4f0102f3f0ac63f0a125bce36ca75d4e0d");
    byte[] val4 = Hex.decode("01");
    TrieImpl storage = new TrieImpl(new MockDB("Test"));
    storage.update(key1, val1);
    storage.update(key2, val2);
    storage.update(key3, val3);
    storage.update(key4, val4);
    String hash = Hex.toHexString(storage.getRootHash());
    System.out.println(hash);
    Assert.assertEquals("517eaccda568f3fa24915fed8add49d3b743b3764c0bc495b19a47c54dbc3d62", hash);
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) MockDB(org.aion.db.impl.mockdb.MockDB) Test(org.junit.Test)

Example 5 with MockDB

use of org.aion.db.impl.mockdb.MockDB in project aion by aionnetwork.

the class TrieTest method testRollbackToRootScenarios.

@Test
@Parameters(method = "keyValue1Value2Parameters")
public void testRollbackToRootScenarios(String key, String value1, String value2) {
    // create a new trie object with mock database
    TrieImpl trie = new TrieImpl(new MockDB("TestKnownRoot"));
    // -------------------------------------------------------------------------------------------------------------
    // insert (key,value1) pair into the trie
    trie.update(key, value1);
    Object oldRoot = trie.getRoot();
    // check retrieval after new addition
    assertThat(new String(trie.get(key))).isEqualTo(value1);
    // -------------------------------------------------------------------------------------------------------------
    // update to (key,value2)
    trie.update(key, value2);
    Object newRoot = trie.getRoot();
    // check retrieval after new addition
    assertThat(new String(trie.get(key))).isEqualTo(value2);
    // -------------------------------------------------------------------------------------------------------------
    // check old root retrieval
    trie.setRoot(oldRoot);
    assertThat(new String(trie.get(key))).isEqualTo(value1);
    // -------------------------------------------------------------------------------------------------------------
    // delete
    trie.update(key, "");
    assertThat(new String(trie.get(key))).isNotEqualTo(value1);
    // check old root retrieval
    trie.setRoot(oldRoot);
    assertThat(new String(trie.get(key))).isEqualTo(value1);
    // -------------------------------------------------------------------------------------------------------------
    // check new root retrieval
    trie.setRoot(newRoot);
    assertThat(new String(trie.get(key))).isEqualTo(value2);
    // -------------------------------------------------------------------------------------------------------------
    // update to (key+value1,value2)
    trie.update(key + value1, value2);
    Object updateRoot = trie.getRoot();
    // check retrieval after new addition
    assertThat(new String(trie.get(key))).isEqualTo(value2);
    // -------------------------------------------------------------------------------------------------------------
    // delete
    trie.delete(key);
    assertThat(new String(trie.get(key))).isNotEqualTo(value1);
    assertThat(new String(trie.get(key))).isNotEqualTo(value2);
    // check new root retrieval
    trie.setRoot(updateRoot);
    assertThat(new String(trie.get(key))).isEqualTo(value2);
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) MockDB(org.aion.db.impl.mockdb.MockDB) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

MockDB (org.aion.db.impl.mockdb.MockDB)6 TrieImpl (org.aion.mcf.trie.TrieImpl)5 Test (org.junit.Test)5 Parameters (junitparams.Parameters)3 Before (org.junit.Before)1