Search in sources :

Example 1 with ByteArrayKeyValueStore

use of org.aion.db.impl.ByteArrayKeyValueStore in project aion by aionnetwork.

the class InnerContractDetailsTest method testCommitToStored_withStorageOnFvm.

@Test
public void testCommitToStored_withStorageOnFvm() {
    AionAddress address = mock(AionAddress.class);
    ByteArrayKeyValueStore db = mock(XorDataSource.class);
    FvmContractDetails parent = new FvmContractDetails(address, db);
    InnerContractDetails child = new InnerContractDetails(null);
    Map<ByteArrayWrapper, ByteArrayWrapper> storage = new HashMap<>();
    for (int i = 0; i < 3; i++) {
        ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(32));
        ByteArrayWrapper value = ByteArrayWrapper.wrap(RandomUtils.nextBytes(100));
        child.put(key, value);
        storage.put(key, value);
    }
    ByteArrayWrapper deletedKey = ByteArrayWrapper.wrap(RandomUtils.nextBytes(32));
    child.delete(deletedKey);
    storage.put(deletedKey, null);
    assertThat(child.isDirty()).isTrue();
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.FVM);
    child.commitTo(parent);
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.FVM);
    for (ByteArrayWrapper key : storage.keySet()) {
        assertThat(parent.get(key)).isEqualTo(storage.get(key));
    }
    assertThat(parent.isDirty()).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) ByteArrayKeyValueStore(org.aion.db.impl.ByteArrayKeyValueStore) Test(org.junit.Test)

Example 2 with ByteArrayKeyValueStore

use of org.aion.db.impl.ByteArrayKeyValueStore in project aion by aionnetwork.

the class InnerContractDetailsTest method testCommitToStored_withCodeOnFvm.

@Test
public void testCommitToStored_withCodeOnFvm() {
    AionAddress address = mock(AionAddress.class);
    ByteArrayKeyValueStore db = mock(XorDataSource.class);
    FvmContractDetails parent = new FvmContractDetails(address, db);
    byte[] code = RandomUtils.nextBytes(100);
    InnerContractDetails child = new InnerContractDetails(null);
    child.setCode(code);
    assertThat(child.isDirty()).isTrue();
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.FVM);
    child.commitTo(parent);
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.FVM);
    assertThat(parent.getCode(h256(code))).isEqualTo(code);
    assertThat(parent.isDirty()).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayKeyValueStore(org.aion.db.impl.ByteArrayKeyValueStore) Test(org.junit.Test)

Example 3 with ByteArrayKeyValueStore

use of org.aion.db.impl.ByteArrayKeyValueStore in project aion by aionnetwork.

the class InnerContractDetailsTest method testCommitToStored_withCodeOnAvm.

@Test
public void testCommitToStored_withCodeOnAvm() {
    AionAddress address = mock(AionAddress.class);
    ByteArrayKeyValueStore db = mock(XorDataSource.class);
    AvmContractDetails parent = new AvmContractDetails(address, db, db);
    byte[] code = RandomUtils.nextBytes(100);
    InnerContractDetails child = new InnerContractDetails(null);
    child.setCode(code);
    assertThat(child.isDirty()).isTrue();
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.AVM);
    child.commitTo(parent);
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.AVM);
    assertThat(parent.getCode(h256(code))).isEqualTo(code);
    assertThat(parent.isDirty()).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayKeyValueStore(org.aion.db.impl.ByteArrayKeyValueStore) Test(org.junit.Test)

Example 4 with ByteArrayKeyValueStore

use of org.aion.db.impl.ByteArrayKeyValueStore in project aion by aionnetwork.

the class InnerContractDetailsTest method testCommitToStored_withObjectGraph.

@Test
public void testCommitToStored_withObjectGraph() {
    AionAddress address = mock(AionAddress.class);
    ByteArrayKeyValueStore db = mock(XorDataSource.class);
    AvmContractDetails parent = new AvmContractDetails(address, db, db);
    byte[] graph = RandomUtils.nextBytes(100);
    InnerContractDetails child = new InnerContractDetails(null);
    child.setObjectGraph(graph);
    assertThat(child.isDirty()).isTrue();
    child.commitTo(parent);
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.AVM);
    assertThat(parent.getObjectGraph()).isEqualTo(graph);
    assertThat(parent.isDirty()).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayKeyValueStore(org.aion.db.impl.ByteArrayKeyValueStore) Test(org.junit.Test)

Example 5 with ByteArrayKeyValueStore

use of org.aion.db.impl.ByteArrayKeyValueStore in project aion by aionnetwork.

the class DetailsDataStore method getSnapshot.

/**
 * Fetches the ContractDetails with the given root.
 *
 * @param vm the virtual machine used at contract deployment
 * @param key the contract address as bytes
 * @param storageRoot the requested storage root
 * @return a snapshot of the contract details with the requested root
 */
public synchronized StoredContractDetails getSnapshot(InternalVmType vm, byte[] key, byte[] storageRoot) {
    Optional<byte[]> rawDetails = detailsSrc.get(key);
    if (rawDetails.isPresent()) {
        // decode raw details and return snapshot
        RLPContractDetails rlpDetails = fromEncoding(rawDetails.get());
        ByteArrayKeyValueStore storage = createStorageSource(rlpDetails.address);
        if (vm == InternalVmType.AVM) {
            ByteArrayKeyValueStore graph = createGraphSource(rlpDetails.address);
            return AvmContractDetails.decodeAtRoot(rlpDetails, storage, graph, storageRoot);
        } else if (vm == InternalVmType.FVM) {
            return FvmContractDetails.decodeAtRoot(rlpDetails, storage, storageRoot);
        } else {
            // There is no need to instantiate a ContractDetails object.
            return null;
        }
    } else {
        return null;
    }
}
Also used : ByteArrayKeyValueStore(org.aion.db.impl.ByteArrayKeyValueStore)

Aggregations

ByteArrayKeyValueStore (org.aion.db.impl.ByteArrayKeyValueStore)7 AionAddress (org.aion.types.AionAddress)5 Test (org.junit.Test)5 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)3 HashMap (java.util.HashMap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 XorDataSource (org.aion.db.store.XorDataSource)1 SecureTrie (org.aion.zero.impl.trie.SecureTrie)1 Trie (org.aion.zero.impl.trie.Trie)1