use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class Cache method get.
public Node get(byte[] key) {
ByteArrayWrapper wrappedKey = wrap(key);
Node node = nodes.get(wrappedKey);
if (node != null) {
// cachehits++;
return node;
} else if (this.dataSource != null) {
Optional<byte[]> data = this.dataSource.get(key);
if (data.isPresent()) {
// dbhits++;
node = new Node(fromRlpEncoded(data.get()), false);
nodes.put(wrappedKey, node);
return node;
}
}
return null;
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class AionRepositoryCacheTest method testGetStorageValueIsDoubleZero.
@Test
public void testGetStorageValueIsDoubleZero() {
AionAddress address = getNewAddress();
ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(16));
cache.removeStorageRow(address, key);
assertNull(cache.getStorageValue(address, key));
key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(DOUBLE_BYTES));
cache.removeStorageRow(address, key);
assertNull(cache.getStorageValue(address, key));
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class AionRepositoryCacheTest method testOverwriteValueWithDoubleZero.
@Test
public void testOverwriteValueWithDoubleZero() {
AionAddress address = getNewAddress();
ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(DOUBLE_BYTES));
ByteArrayWrapper value = new DataWord(RandomUtils.nextBytes(DataWord.BYTES)).toWrapper();
cache.addStorageRow(address, key, value);
assertEquals(value, cache.getStorageValue(address, key));
cache.removeStorageRow(address, key);
assertNull(cache.getStorageValue(address, key));
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class FvmContractDetailsTest method testDecodeFromEncodingWithSmallStorage.
@Test
public void testDecodeFromEncodingWithSmallStorage() {
ByteArrayKeyValueDatabase db = new MockDB("db", log);
db.open();
assertThat(db.isEmpty()).isTrue();
byte[] code = ByteUtil.hexStringToBytes("60016002");
ByteArrayWrapper key_1 = ByteArrayWrapper.fromHex("111111");
ByteArrayWrapper val_1 = ByteArrayWrapper.fromHex("aaaaaa");
ByteArrayWrapper key_2 = ByteArrayWrapper.fromHex("222222");
ByteArrayWrapper val_2 = ByteArrayWrapper.fromHex("bbbbbb");
FvmContractDetails details = new FvmContractDetails(AddressUtils.ZERO_ADDRESS, db);
details.setCode(code);
details.put(key_1, val_1);
details.put(key_2, val_2);
// flush changes to the database
details.syncStorage();
FvmContractDetails decoded = FvmContractDetails.decodeAtRoot(fromEncoding(details.getEncoded()), db, details.getStorageHash());
// check code
byte[] codeHash = h256(code);
assertThat(decoded.getCode(codeHash)).isEqualTo(code);
// check address
assertThat(decoded.address).isEqualTo(AddressUtils.ZERO_ADDRESS);
// check storage
assertThat(decoded.get(key_1)).isEqualTo(val_1);
assertThat(decoded.get(key_2)).isEqualTo(val_2);
}
use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.
the class FvmContractDetailsTest method testDecode_withInLineStorageAndTransition.
@Test
public void testDecode_withInLineStorageAndTransition() {
SecureTrie trie = new SecureTrie(null);
Map<ByteArrayWrapper, ByteArrayWrapper> storage = new HashMap<>();
for (int i = 0; i < 3; i++) {
byte[] key = RandomUtils.nextBytes(32);
byte[] value = RandomUtils.nextBytes(100);
trie.update(key, RLP.encodeElement(value));
storage.put(ByteArrayWrapper.wrap(key), ByteArrayWrapper.wrap(value));
}
RLPElement storageTrie = RLP.decode2SharedList(trie.serialize());
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] codeBytes = RandomUtils.nextBytes(100);
RLPElement code = mock(SharedRLPItem.class);
when(code.getRLPData()).thenReturn(codeBytes);
byte[] rootHash = RandomUtils.nextBytes(32);
RLPElement root = mock(SharedRLPItem.class);
when(root.getRLPData()).thenReturn(rootHash);
Logger log = mock(Logger.class);
ByteArrayKeyValueDatabase db = new MockDB("db", log);
db.open();
assertThat(db.isEmpty()).isTrue();
RLPContractDetails input = new RLPContractDetails(address, false, root, storageTrie, code);
FvmContractDetails details = FvmContractDetails.decodeAtRoot(input, db, rootHash);
assertThat(details.address).isEqualTo(address);
// because it uses the setCodes method
assertThat(details.isDirty()).isTrue();
assertThat(details.isDeleted()).isFalse();
assertThat(details.getCodes().size()).isEqualTo(1);
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
byte[] storageHash = trie.getRootHash();
assertThat(details.getStorageHash()).isEqualTo(storageHash);
for (ByteArrayWrapper key : storage.keySet()) {
assertThat(details.get(key)).isEqualTo(storage.get(key));
}
assertThat(db.isEmpty()).isFalse();
}
Aggregations