Search in sources :

Example 21 with ByteArrayWrapper

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

the class IContractDetailsTest method checkStorage.

/**
 * Checks that cache's storage, given by cache.getStorage(), contains all key-value pairs in
 * keys and values, where it is assumed every n'th pair was deleted.
 */
private void checkStorage(ContractDetails cache, List<ByteArrayWrapper> keys, List<ByteArrayWrapper> values, int n) {
    Map<ByteArrayWrapper, ByteArrayWrapper> storage = cache.getStorage(keys);
    int count = 1;
    for (ByteArrayWrapper key : keys) {
        if (count % n == 0) {
            try {
                assertNull(storage.get(key));
            } catch (AssertionError e) {
                System.err.println("\nAssertion failed on key: " + key);
                e.printStackTrace();
            }
        } else {
            assertEquals(values.get(count - 1), storage.get(key));
        }
        count++;
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 22 with ByteArrayWrapper

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

the class IContractDetailsTest method testCommitEnMassOriginalIsAionContract.

/**
 * This test is specific to the InnerContractDetails class, which has a commit method. This
 * test class is not concerned with testing all of the functionality of this method, only with
 * how this method handles zero-byte values.
 */
@Test
public void testCommitEnMassOriginalIsAionContract() {
    InnerContractDetails impl = new InnerContractDetails(cache1);
    int numEntries = RandomUtils.nextInt(1_000, 5_000);
    int deleteOdds = 3;
    List<ByteArrayWrapper> keys = getKeysInBulk(numEntries);
    List<ByteArrayWrapper> values = getValuesInBulk(numEntries);
    massPutIntoCache(impl, keys, values);
    deleteEveryNthEntry(impl, keys, deleteOdds);
    Map<ByteArrayWrapper, ByteArrayWrapper> storage = impl.getStorage(keys);
    assertEquals(0, cache1.getStorage(keys).size());
    impl.commit();
    assertEquals(storage.size(), cache1.getStorage(keys).size());
    int count = 1;
    for (ByteArrayWrapper key : keys) {
        try {
            if (count % deleteOdds == 0) {
                assertNull(impl.get(key));
                assertNull(cache1.get(key));
            } else {
                assertEquals(impl.get(key), cache1.get(key));
            }
        } catch (AssertionError e) {
            System.err.println("\nAssertion failed on key: " + key);
            e.printStackTrace();
        }
        count++;
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Test(org.junit.Test)

Example 23 with ByteArrayWrapper

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

the class IContractDetailsTest method testCacheUpdatedAndGetWithOriginalAionContract.

/**
 * This test is specific to the InnerContractDetails class, which at times holds a different
 * storage value for contracts. This test checks that after an update to the cache object, the
 * original value from the contract details is not returned for use.
 */
@Test
public void testCacheUpdatedAndGetWithOriginalAionContract() {
    ByteArrayWrapper key = getRandomWord(true);
    ByteArrayWrapper value1 = getRandomWord(true);
    ByteArrayWrapper value2 = getRandomWord(true);
    // unlikely to be necessary
    while (value1.equals(value2)) {
        value2 = getRandomWord(true);
    }
    // ensure the initial cache has the value
    cache1.put(key, value1);
    InnerContractDetails impl = new InnerContractDetails(cache1);
    // check that original value is retrieved
    assertThat(impl.get(key)).isEqualTo(value1);
    // delete and check that value is missing
    impl.delete(key);
    assertThat(impl.get(key)).isEqualTo(null);
    // add new value and check correctness
    impl.put(key, value2);
    assertThat(impl.get(key)).isEqualTo(value2);
    // clean-up
    cache1.delete(key);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Test(org.junit.Test)

Example 24 with ByteArrayWrapper

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

the class IContractDetailsTest method setStorage.

/**
 * Sets the storage to contain the specified key-value mappings.
 *
 * @param contractDetails the contract details that will store the data
 * @param storage the specified mappings
 * @implNote A {@code null} value is interpreted as deletion.
 */
public void setStorage(ContractDetails contractDetails, Map<ByteArrayWrapper, ByteArrayWrapper> storage) {
    for (Map.Entry<ByteArrayWrapper, ByteArrayWrapper> entry : storage.entrySet()) {
        ByteArrayWrapper key = entry.getKey();
        ByteArrayWrapper value = entry.getValue();
        if (value != null) {
            contractDetails.put(key, value);
        } else {
            contractDetails.delete(key);
        }
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with ByteArrayWrapper

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

the class IContractDetailsTest method testPutZeroKeyAndValue.

@Test
public void testPutZeroKeyAndValue() {
    // Try single-single
    cache1.delete(DataWord.ZERO.toWrapper());
    ByteArrayWrapper result = cache1.get(DataWord.ZERO.toWrapper());
    assertNull(result);
    cache2.delete(DataWord.ZERO.toWrapper());
    assertNull(cache2.get(DataWord.ZERO.toWrapper()));
    // Try single-double
    cache1.delete(DataWord.ZERO.toWrapper());
    result = cache1.get(DataWord.ZERO.toWrapper());
    assertNull(result);
    cache2.delete(DataWord.ZERO.toWrapper());
    assertNull(cache2.get(DataWord.ZERO.toWrapper()));
    // Try double-single
    cache1.delete(ZERO_WRAPPED_32);
    result = cache1.get(ZERO_WRAPPED_32);
    assertNull(result);
    cache2.delete(ZERO_WRAPPED_32);
    assertNull(cache2.get(ZERO_WRAPPED_32));
    // Try double-double
    cache1.delete(ZERO_WRAPPED_32);
    result = cache1.get(ZERO_WRAPPED_32);
    assertNull(result);
    cache2.delete(ZERO_WRAPPED_32);
    assertNull(cache2.get(ZERO_WRAPPED_32));
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Test(org.junit.Test)

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