Search in sources :

Example 6 with SecureTrie

use of org.aion.zero.impl.trie.SecureTrie in project aion by aionnetwork.

the class DetailsDataStoreTest method testDecodingWithSizeFiveInternal.

@Test
public void testDecodingWithSizeFiveInternal() {
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    byte[] code = RandomUtils.nextBytes(512);
    // create encoding
    byte[] rlpAddress = RLP.encodeElement(address.toByteArray());
    byte[] rlpIsExternalStorage = RLP.encodeByte((byte) 0);
    byte[] rlpStorageRoot = RLP.encodeElement(EMPTY_BYTE_ARRAY);
    SecureTrie trie = new SecureTrie(null);
    trie.update(RandomUtils.nextBytes(32), RandomUtils.nextBytes(32));
    byte[] rlpStorageTrie = RLP.encodeElement(trie.serialize());
    byte[] rlpCode = RLP.encodeElement(code);
    byte[] encoding = RLP.encodeList(rlpAddress, rlpIsExternalStorage, rlpStorageRoot, rlpStorageTrie, rlpCode);
    // decode
    RLPContractDetails details = DetailsDataStore.fromEncoding(encoding);
    assertThat(details.address).isEqualTo(address);
    assertThat(details.isExternalStorage).isFalse();
    assertThat(details.storageRoot.getRLPData()).isEqualTo(EMPTY_BYTE_ARRAY);
    assertThat(details.storageTrie.getRLPData()).isEqualTo(trie.serialize());
    assertThat(details.code.getRLPData()).isEqualTo(code);
}
Also used : AionAddress(org.aion.types.AionAddress) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) SecureTrie(org.aion.zero.impl.trie.SecureTrie) Test(org.junit.Test)

Example 7 with SecureTrie

use of org.aion.zero.impl.trie.SecureTrie in project aion by aionnetwork.

the class AvmContractDetailsTest 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);
    byte[] storageHash = RandomUtils.nextBytes(32);
    byte[] graphHash = RandomUtils.nextBytes(32);
    byte[] graphBytes = RandomUtils.nextBytes(100);
    when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash), RLP.encodeElement(graphHash))));
    when(mockDatabase.get(graphHash)).thenReturn(Optional.of(graphBytes));
    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);
    AvmContractDetails details = AvmContractDetails.decodeAtRoot(input, db, mockDatabase, rootHash);
    assertThat(details.address).isEqualTo(address);
    // because it uses the setCodes method
    assertThat(details.isDirty()).isTrue();
    assertThat(details.isDeleted()).isFalse();
    assertThat(details.getObjectGraph()).isEqualTo(graphBytes);
    assertThat(details.getCodes().size()).isEqualTo(1);
    assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
    assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
    storageHash = trie.getRootHash();
    byte[] concatenated = new byte[storageHash.length + graphHash.length];
    System.arraycopy(storageHash, 0, concatenated, 0, storageHash.length);
    System.arraycopy(graphHash, 0, concatenated, storageHash.length, graphHash.length);
    assertThat(details.getStorageHash()).isEqualTo(h256(concatenated));
    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 8 with SecureTrie

use of org.aion.zero.impl.trie.SecureTrie in project aion by aionnetwork.

the class AvmContractDetailsTest method testDecode_withInLineStorageAndEmptyStorageTrie.

@Test
public void testDecode_withInLineStorageAndEmptyStorageTrie() {
    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);
    byte[] storageHash = EMPTY_TRIE_HASH;
    byte[] graphHash = RandomUtils.nextBytes(32);
    byte[] graphBytes = RandomUtils.nextBytes(100);
    when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash), RLP.encodeElement(graphHash))));
    when(mockDatabase.get(graphHash)).thenReturn(Optional.of(graphBytes));
    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);
    AvmContractDetails details = AvmContractDetails.decodeAtRoot(input, db, mockDatabase, rootHash);
    assertThat(details.address).isEqualTo(address);
    // because it uses the setCodes method
    assertThat(details.isDirty()).isTrue();
    assertThat(details.isDeleted()).isFalse();
    assertThat(details.getObjectGraph()).isEqualTo(graphBytes);
    assertThat(details.getCodes().size()).isEqualTo(1);
    assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
    assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
    byte[] concatenated = new byte[storageHash.length + graphHash.length];
    System.arraycopy(storageHash, 0, concatenated, 0, storageHash.length);
    System.arraycopy(graphHash, 0, concatenated, storageHash.length, graphHash.length);
    assertThat(details.getStorageHash()).isEqualTo(h256(concatenated));
    for (ByteArrayWrapper key : storage.keySet()) {
        assertThat(details.get(key)).isNull();
    }
    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)

Aggregations

SecureTrie (org.aion.zero.impl.trie.SecureTrie)8 RLPElement (org.aion.rlp.RLPElement)6 AionAddress (org.aion.types.AionAddress)5 RLPContractDetails (org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails)5 Test (org.junit.Test)5 ByteArrayKeyValueDatabase (org.aion.db.impl.ByteArrayKeyValueDatabase)4 MockDB (org.aion.db.impl.mockdb.MockDB)4 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)4 HashMap (java.util.HashMap)3 Logger (org.slf4j.Logger)3 RLPList (org.aion.rlp.RLPList)2 SharedRLPList (org.aion.rlp.SharedRLPList)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 ByteArrayKeyValueStore (org.aion.db.impl.ByteArrayKeyValueStore)1 XorDataSource (org.aion.db.store.XorDataSource)1 Trie (org.aion.zero.impl.trie.Trie)1