use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.
the class AionRepositoryImpl method importTrieNode.
/**
* Imports a trie node to the indicated blockchain database.
*
* @param key the hash key of the trie node to be imported
* @param value the value of the trie node to be imported
* @param dbType the database where the key-value pair should be stored
* @return a {@link TrieNodeResult} indicating the success or failure of the import operation
* @throws IllegalArgumentException if the given key is null or the database type is not
* supported
*/
public TrieNodeResult importTrieNode(byte[] key, byte[] value, DatabaseType dbType) {
// empty keys are not allowed
if (key == null || key.length != V1Constants.HASH_SIZE) {
return TrieNodeResult.INVALID_KEY;
}
// not allowing deletions to be imported
if (value == null || value.length == 0) {
return TrieNodeResult.INVALID_VALUE;
}
ByteArrayKeyValueDatabase db = selectDatabase(dbType);
Optional<byte[]> stored = db.get(key);
if (stored.isPresent()) {
if (Arrays.equals(stored.get(), value)) {
return TrieNodeResult.KNOWN;
} else {
return TrieNodeResult.INCONSISTENT;
}
}
db.put(key, value);
db.commit();
return TrieNodeResult.IMPORTED;
}
use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.
the class FvmContractDetailsTest method testDecode_withInLineStorageAndEmptyStorageTrie.
@Test
public void testDecode_withInLineStorageAndEmptyStorageTrie() {
SecureTrie trie = new SecureTrie(null);
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[] storageHash = EMPTY_TRIE_HASH;
RLPElement root = mock(SharedRLPItem.class);
when(root.getRLPData()).thenReturn(storageHash);
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, storageHash);
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);
assertThat(details.getStorageHash()).isEqualTo(storageHash);
}
use of org.aion.db.impl.ByteArrayKeyValueDatabase 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.db.impl.ByteArrayKeyValueDatabase 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();
}
use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.
the class AionRepositoryImpl method getTrieNode.
/**
* Retrieves the value for a given node from the database associated with the given type.
*
* @param key the key of the node to be retrieved
* @param dbType the database where the key should be found
* @return the {@code byte} array value associated with the given key or {@code null} when the
* key cannot be found in the database.
* @throws IllegalArgumentException if the given key is null or the database type is not
* supported
*/
public byte[] getTrieNode(byte[] key, DatabaseType dbType) {
ByteArrayKeyValueDatabase db = selectDatabase(dbType);
Optional<byte[]> value = db.get(key);
return value.orElse(null);
}
Aggregations