Search in sources :

Example 56 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TrieTest method testGetMissingNodes_wCompleteTrie.

@Test
public void testGetMissingNodes_wCompleteTrie() {
    MockDB mockDB = new MockDB("temp", log);
    mockDB.open();
    TrieImpl trie = new TrieImpl(mockDB);
    for (Map.Entry<ByteArrayWrapper, byte[]> e : getSampleTrieUpdates().entrySet()) {
        trie.update(e.getKey().toBytes(), e.getValue());
    }
    trie.getCache().commitForTest();
    byte[] root = trie.getRootHash();
    trie = new TrieImpl(mockDB);
    assertThat(trie.getMissingNodes(root)).isEmpty();
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) MockDB(org.aion.db.impl.mockdb.MockDB) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 57 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TrieTest method testGetReferencedTrieNodes.

@Test
public void testGetReferencedTrieNodes() {
    MockDB mockDB = new MockDB("temp", log);
    mockDB.open();
    TrieImpl trie = new TrieImpl(mockDB);
    for (Map.Entry<ByteArrayWrapper, byte[]> e : getSampleTrieUpdates().entrySet()) {
        trie.update(e.getKey().toBytes(), e.getValue());
    }
    trie.getCache().commitForTest();
    byte[] root = trie.getRootHash();
    byte[] value = mockDB.get(root).get();
    trie = new TrieImpl(mockDB);
    // empty for limit <= 0
    assertThat(trie.getReferencedTrieNodes(value, -2)).isEmpty();
    assertThat(trie.getReferencedTrieNodes(value, 0)).isEmpty();
    // partial size
    assertThat(trie.getReferencedTrieNodes(value, 3).size()).isEqualTo(3);
    assertThat(trie.getReferencedTrieNodes(value, 4).size()).isEqualTo(4);
    // full size except for initial root
    int full = trie.getTrieSize(root) - 1;
    assertThat(trie.getReferencedTrieNodes(value, full).size()).isEqualTo(full);
    assertThat(trie.getReferencedTrieNodes(value, 2 * full).size()).isEqualTo(full);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) MockDB(org.aion.db.impl.mockdb.MockDB) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 58 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TrieTest method testGetReferencedTrieNodes_withStartFromAllNodes.

@Test
public void testGetReferencedTrieNodes_withStartFromAllNodes() {
    MockDB mockDB = new MockDB("temp", log);
    mockDB.open();
    TrieImpl trie = new TrieImpl(mockDB);
    for (Map.Entry<ByteArrayWrapper, byte[]> e : getSampleTrieUpdates().entrySet()) {
        trie.update(e.getKey().toBytes(), e.getValue());
    }
    trie.getCache().commitForTest();
    byte[] value, root = trie.getRootHash();
    Set<ByteArrayWrapper> allKeys = trie.getTrieKeys(root);
    trie = new TrieImpl(mockDB);
    Value v;
    for (ByteArrayWrapper key : allKeys) {
        value = mockDB.get(key.toBytes()).get();
        v = Value.fromRlpEncoded(value);
        // empty for limit <= 0
        assertThat(trie.getReferencedTrieNodes(key.toBytes(), -2)).isEmpty();
        assertThat(trie.getReferencedTrieNodes(value, -2)).isEmpty();
        assertThat(trie.getReferencedTrieNodes(key.toBytes(), 0)).isEmpty();
        assertThat(trie.getReferencedTrieNodes(value, 0)).isEmpty();
        // partial size = 1 for non-leafs and 0 for leafs
        assertThat(trie.getReferencedTrieNodes(value, 1).size()).isAtMost(1);
        assertThat(trie.getReferencedTrieNodes(key.toBytes(), 1).size()).isAtMost(1);
        if (v.isList() && v.asList().size() > 2) {
            // partial size = 4 for branch node
            assertThat(trie.getReferencedTrieNodes(value, 100).size()).isEqualTo(4);
            assertThat(trie.getReferencedTrieNodes(key.toBytes(), 100).size()).isEqualTo(5);
        } else if (v.isList()) {
            // at most whole list
            assertThat(trie.getReferencedTrieNodes(value, 100).size()).isAtMost(5);
            assertThat(trie.getReferencedTrieNodes(key.toBytes(), 100).size()).isAtMost(6);
        } else {
            // partial size = 0 for leafs
            assertThat(trie.getReferencedTrieNodes(value, 100).size()).isAtMost(0);
            assertThat(trie.getReferencedTrieNodes(key.toBytes(), 100).size()).isAtMost(0);
        }
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) MockDB(org.aion.db.impl.mockdb.MockDB) Value(org.aion.rlp.Value) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 59 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class ExternalStateForAvm method getStorage.

@Override
public byte[] getStorage(AionAddress address, byte[] key) {
    ByteArrayWrapper storageKey = ByteArrayWrapper.wrap(key);
    ByteArrayWrapper value = this.repositoryCache.getStorageValue(address, storageKey);
    return (value == null) ? null : value.toBytes();
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 60 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class ExternalStateForAvm method putStorage.

@Override
public void putStorage(AionAddress address, byte[] key, byte[] value) {
    ByteArrayWrapper storageKey = ByteArrayWrapper.wrap(key);
    ByteArrayWrapper storageValue = ByteArrayWrapper.wrap(value);
    this.repositoryCache.addStorageRow(address, storageKey, storageValue);
    setVmType(address);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)130 Test (org.junit.Test)51 HashMap (java.util.HashMap)39 ArrayList (java.util.ArrayList)33 AionAddress (org.aion.types.AionAddress)26 Block (org.aion.zero.impl.types.Block)24 Map (java.util.Map)20 BigInteger (java.math.BigInteger)14 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 IOException (java.io.IOException)13 MockDB (org.aion.db.impl.mockdb.MockDB)13 DataWord (org.aion.util.types.DataWord)13 PooledTransaction (org.aion.base.PooledTransaction)11 List (java.util.List)10 AionTransaction (org.aion.base.AionTransaction)10 Properties (java.util.Properties)8 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 ECKey (org.aion.crypto.ECKey)5 RLPElement (org.aion.rlp.RLPElement)5