Search in sources :

Example 11 with RLPElement

use of org.aion.rlp.RLPElement in project aion by aionnetwork.

the class CipherParams method parse.

public static CipherParams parse(byte[] bytes) throws UnsupportedEncodingException {
    RLPElement element = RLP.decode2SharedList(bytes).get(0);
    if (!element.isList()) {
        throw new IllegalArgumentException("The keystore decoded rlp element is not a list");
    }
    SharedRLPList list = (SharedRLPList) element;
    CipherParams cp = new CipherParams();
    cp.setIv(new String(list.get(0).getRLPData(), "US-ASCII"));
    return cp;
}
Also used : RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 12 with RLPElement

use of org.aion.rlp.RLPElement 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 13 with RLPElement

use of org.aion.rlp.RLPElement 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 14 with RLPElement

use of org.aion.rlp.RLPElement in project aion by aionnetwork.

the class AvmContractDetailsTest method testDecode_withIncorrectListForConcatenatedData.

@Test(expected = IllegalArgumentException.class)
public void testDecode_withIncorrectListForConcatenatedData() {
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    byte[] rootHash = RandomUtils.nextBytes(32);
    RLPElement root = mock(RLPItem.class);
    when(root.getRLPData()).thenReturn(rootHash);
    byte[] codeBytes1 = RandomUtils.nextBytes(100);
    byte[] codeBytes2 = RandomUtils.nextBytes(100);
    RLPList code = new RLPList();
    code.add(new RLPItem(codeBytes1));
    code.add(new RLPItem(codeBytes2));
    byte[] storageHash = RandomUtils.nextBytes(32);
    when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash))));
    RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
    AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, rootHash);
}
Also used : SharedRLPItem(org.aion.rlp.SharedRLPItem) RLPItem(org.aion.rlp.RLPItem) AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) SharedRLPList(org.aion.rlp.SharedRLPList) RLPList(org.aion.rlp.RLPList) Test(org.junit.Test)

Example 15 with RLPElement

use of org.aion.rlp.RLPElement in project aion by aionnetwork.

the class AbstractBlockSummary method decodeMap.

protected static <K, V> Map<K, V> decodeMap(RLPList list, Functional.Function<byte[], K> keyDecoder, Functional.Function<byte[], V> valueDecoder) {
    Map<K, V> result = new HashMap<>();
    for (RLPElement entry : list) {
        K key = keyDecoder.apply(((RLPList) entry).get(0).getRLPData());
        V value = valueDecoder.apply(((RLPList) entry).get(1).getRLPData());
        result.put(key, value);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) RLPElement(org.aion.rlp.RLPElement) RLPList(org.aion.rlp.RLPList)

Aggregations

RLPElement (org.aion.rlp.RLPElement)39 SharedRLPList (org.aion.rlp.SharedRLPList)23 RLPList (org.aion.rlp.RLPList)18 AionAddress (org.aion.types.AionAddress)11 RLPContractDetails (org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)7 HashMap (java.util.HashMap)6 RLPItem (org.aion.rlp.RLPItem)6 SecureTrie (org.aion.zero.impl.trie.SecureTrie)6 SharedRLPItem (org.aion.rlp.SharedRLPItem)5 ByteArrayKeyValueDatabase (org.aion.db.impl.ByteArrayKeyValueDatabase)4 MockDB (org.aion.db.impl.mockdb.MockDB)4 BigInteger (java.math.BigInteger)3 Logger (org.slf4j.Logger)3 DataWord (org.aion.mcf.vm.types.DataWord)2 Log (org.aion.types.Log)2 Block (org.aion.zero.impl.types.Block)2 A0BlockHeader (org.aion.zero.types.A0BlockHeader)2