Search in sources :

Example 21 with TrieImpl

use of org.aion.mcf.trie.TrieImpl 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)

Example 22 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testInsertRandomMultipleItems.

/**
 * @param pairs
 * @implNote By design the keys are distinct due to the use of HashMap.
 */
@Test
@Parameters(method = "keyValuePairsParameters")
public void testInsertRandomMultipleItems(HashMap<String, String> pairs) {
    boolean print = false;
    if (print) {
        System.out.println("Number of pairs = " + pairs.size());
    }
    TrieImpl trie = new TrieImpl(new MockDB("TestInsertRandomMultipleItems"));
    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);
        assertThat(new String(trie.get(key))).isEqualTo(value);
    }
    // ensure that everything is still there
    for (Map.Entry<String, String> entry : pairs.entrySet()) {
        key = entry.getKey();
        value = entry.getValue();
        assertThat(new String(trie.get(key))).isEqualTo(value);
    }
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) MockDB(org.aion.db.impl.mockdb.MockDB) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 23 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testTrieRootEquality.

@Test
@Parameters(method = "keyValuePairsParameters")
public void testTrieRootEquality(HashMap<String, String> pairs) {
    boolean print = false;
    if (print) {
        System.out.println("Number of pairs = " + pairs.size());
    }
    // create a new trie object without database
    TrieImpl trie1 = new TrieImpl(null);
    TrieImpl trie2 = new TrieImpl(null);
    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)
        trie1.update(key, value);
        trie2.update(key, value);
        // ensure equality after each addition
        assertThat(trie1).isEqualTo(trie2);
        assertThat(Hex.toHexString(trie1.getRootHash())).isEqualTo(Hex.toHexString(trie2.getRootHash()));
    }
    String oldHash = Hex.toHexString(trie1.getRootHash());
    // ensure inequality after new additions
    trie1.update("key for trie1", "value1");
    trie2.update("key for trie2", "value2");
    assertThat(trie1).isNotEqualTo(trie2);
    assertThat(Hex.toHexString(trie1.getRootHash())).isNotEqualTo(Hex.toHexString(trie2.getRootHash()));
    // ensure equality after deletions
    trie1.delete("key for trie1");
    trie2.delete("key for trie2");
    assertThat(trie1).isEqualTo(trie2);
    assertThat(Hex.toHexString(trie1.getRootHash())).isEqualTo(Hex.toHexString(trie2.getRootHash()));
    assertThat(Hex.toHexString(trie1.getRootHash())).isEqualTo(oldHash);
    // ensure inequality after one side deletion
    trie1.delete(pairs.keySet().iterator().next());
    assertThat(trie1).isNotEqualTo(trie2);
    assertThat(Hex.toHexString(trie1.getRootHash())).isNotEqualTo(Hex.toHexString(trie2.getRootHash()));
    assertThat(Hex.toHexString(trie1.getRootHash())).isNotEqualTo(oldHash);
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 24 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTestWithRootHashValues method testDeleteShortString3.

@Test
public void testDeleteShortString3() {
    String ROOT_HASH_BEFORE = "778ab82a7e8236ea2ff7bb9cfa46688e7241c1fd445bf2941416881a6ee192eb";
    String ROOT_HASH_AFTER = "05875807b8f3e735188d2479add82f96dee4db5aff00dc63f07a7e27d0deab65";
    TrieImpl trie = new TrieImpl(mockDb);
    trie.update(cat, dude);
    assertEquals(dude, new String(trie.get(cat)));
    trie.update(dog, test);
    assertEquals(test, new String(trie.get(dog)));
    assertEquals(ROOT_HASH_BEFORE, Hex.toHexString(trie.getRootHash()));
    trie.delete(dog);
    assertEquals("", new String(trie.get(dog)));
    assertEquals(ROOT_HASH_AFTER, Hex.toHexString(trie.getRootHash()));
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Test(org.junit.Test)

Example 25 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTestWithRootHashValues method testPuppy.

@Test
public void testPuppy() {
    TrieImpl trie = new TrieImpl(null);
    trie.update("do", "verb");
    trie.update("doge", "coin");
    trie.update("horse", "stallion");
    trie.update("dog", "puppy");
    assertEquals("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84", Hex.toHexString(trie.getRootHash()));
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Test(org.junit.Test)

Aggregations

TrieImpl (org.aion.mcf.trie.TrieImpl)32 Test (org.junit.Test)31 Parameters (junitparams.Parameters)5 MockDB (org.aion.db.impl.mockdb.MockDB)5 ArrayList (java.util.ArrayList)3 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 ImportResult (org.aion.mcf.core.ImportResult)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 RLPElement (org.aion.rlp.RLPElement)1 AionTransaction (org.aion.zero.types.AionTransaction)1