Search in sources :

Example 6 with ByteArrayKeyValueDatabase

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;
}
Also used : ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase)

Example 7 with ByteArrayKeyValueDatabase

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);
}
Also used : AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) MockDB(org.aion.db.impl.mockdb.MockDB) SecureTrie(org.aion.zero.impl.trie.SecureTrie) Test(org.junit.Test)

Example 8 with ByteArrayKeyValueDatabase

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);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) MockDB(org.aion.db.impl.mockdb.MockDB) Test(org.junit.Test)

Example 9 with ByteArrayKeyValueDatabase

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();
}
Also used : AionAddress(org.aion.types.AionAddress) HashMap(java.util.HashMap) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) MockDB(org.aion.db.impl.mockdb.MockDB) Logger(org.slf4j.Logger) SecureTrie(org.aion.zero.impl.trie.SecureTrie) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RLPElement(org.aion.rlp.RLPElement) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) Test(org.junit.Test)

Example 10 with ByteArrayKeyValueDatabase

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);
}
Also used : ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase)

Aggregations

ByteArrayKeyValueDatabase (org.aion.db.impl.ByteArrayKeyValueDatabase)20 Test (org.junit.Test)15 Properties (java.util.Properties)6 MockDB (org.aion.db.impl.mockdb.MockDB)6 AionAddress (org.aion.types.AionAddress)5 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)5 SecureTrie (org.aion.zero.impl.trie.SecureTrie)5 HashMap (java.util.HashMap)4 RLPElement (org.aion.rlp.RLPElement)4 RLPContractDetails (org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails)4 Logger (org.slf4j.Logger)3 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 Block (org.aion.zero.impl.types.Block)2 MiningBlock (org.aion.zero.impl.types.MiningBlock)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AionTransaction (org.aion.base.AionTransaction)1 ImportResult (org.aion.zero.impl.core.ImportResult)1 AionBlockStore (org.aion.zero.impl.db.AionBlockStore)1 BlockInfo (org.aion.zero.impl.db.AionBlockStore.BlockInfo)1