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++;
}
}
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++;
}
}
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);
}
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);
}
}
}
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));
}
Aggregations