Search in sources :

Example 11 with RLPList

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

the class AvmContractDetails method decodeAtRoot.

/**
 * Decodes an AvmContractDetails object from the RLP encoding and returns a snapshot to the
 * specific point in the blockchain history given by the consensus root hash.
 *
 * @param input the stored encoding representing the contract details
 * @param storageSource the data source for the contract storage data
 * @param objectGraphSource the data source for the object graph
 * @param consensusRoot the consensus root linking to specific external storage and object graph
 *     data at the point of interest in the blockchain history
 * @return a snapshot of the contract details with the information it contained at the specified
 *     point in the blockchain history
 */
public static AvmContractDetails decodeAtRoot(RLPContractDetails input, ByteArrayKeyValueStore storageSource, ByteArrayKeyValueStore objectGraphSource, byte[] consensusRoot) {
    Objects.requireNonNull(input, "The contract data for the snapshot cannot be null.");
    Objects.requireNonNull(consensusRoot, "The consensus root for the snapshot cannot be null.");
    // additional null check are performed by the constructor
    AvmContractDetails details = new AvmContractDetails(input.address, storageSource, objectGraphSource);
    RLPElement code = input.code;
    if (code instanceof SharedRLPList) {
        for (RLPElement e : ((SharedRLPList) code)) {
            if (e.isList()) {
                details.setCode(SharedRLPList.getRLPDataCopy((SharedRLPList) e));
            } else {
                details.setCode(e.getRLPData());
            }
        }
    } else if (code instanceof RLPList) {
        for (RLPElement e : ((RLPList) code)) {
            details.setCode(e.getRLPData());
        }
    } else {
        details.setCode(code.getRLPData());
    }
    // The root is the concatenated storage hash.
    // It points to the external storage hash and the object graph hash.
    RLPElement storage = input.storageTrie;
    // Instantiates the storage interpreting the storage root according to the VM specification.
    byte[] storageRootHash;
    Optional<byte[]> concatenatedData = details.objectGraphSource.get(consensusRoot);
    if (concatenatedData.isPresent()) {
        SharedRLPList data = RLP.decode2SharedList(concatenatedData.get());
        if (!(data.get(0).isList())) {
            throw new IllegalArgumentException("Invalid concatenated storage for AVM.");
        }
        SharedRLPList pair = (SharedRLPList) data.get(0);
        if (pair.size() != 2) {
            throw new IllegalArgumentException("Invalid concatenated storage for AVM.");
        }
        storageRootHash = pair.get(0).getRLPData();
        details.objectGraphHash = pair.get(1).getRLPData();
    } else {
        // As a result the concatenated storage hash cannot be missing from the database.
        throw new IllegalArgumentException("Invalid concatenated storage for AVM.");
    }
    // load/deserialize storage trie
    if (input.isExternalStorage) {
        // ensure transition from old encoding
        details.storageTrie = new SecureTrie(details.externalStorageSource, storageRootHash);
    } else {
        details.storageTrie = new SecureTrie(null);
        details.storageTrie.deserialize((SharedRLPList) storage);
        // switch from in-memory to external storage
        details.storageTrie.getCache().setDB(details.externalStorageSource);
        details.storageTrie.sync();
    }
    if (Arrays.equals(storageRootHash, ConstantUtil.EMPTY_TRIE_HASH)) {
        details.storageTrie = new SecureTrie(details.storageTrie.getCache(), "".getBytes());
    }
    return details;
}
Also used : RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList) SecureTrie(org.aion.zero.impl.trie.SecureTrie) RLPList(org.aion.rlp.RLPList) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 12 with RLPList

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

the class FvmContractDetails method decodeAtRoot.

/**
 * Decodes an FvmContractDetails object from the RLP encoding and returns a snapshot to the
 * specific point in the blockchain history given by the consensus root hash.
 *
 * @param input the stored encoding representing the contract details
 * @param storageSource the data source for the contract storage data
 * @param consensusRoot the consensus root linking to specific external storage and object graph
 *     data at the point of interest in the blockchain history
 * @return a snapshot of the contract details with the information it contained at the specified
 *     point in the blockchain history
 */
public static FvmContractDetails decodeAtRoot(RLPContractDetails input, ByteArrayKeyValueStore storageSource, byte[] consensusRoot) {
    Objects.requireNonNull(input, "The contract data for the snapshot cannot be null.");
    Objects.requireNonNull(consensusRoot, "The consensus root for the snapshot cannot be null.");
    // additional null check are performed by the constructor
    FvmContractDetails details = new FvmContractDetails(input.address, storageSource);
    RLPElement code = input.code;
    if (code instanceof SharedRLPList) {
        for (RLPElement e : ((SharedRLPList) code)) {
            if (e.isList()) {
                details.setCode(SharedRLPList.getRLPDataCopy((SharedRLPList) e));
            } else {
                details.setCode(e.getRLPData());
            }
        }
    } else if (code instanceof RLPList) {
        for (RLPElement e : ((RLPList) code)) {
            details.setCode(e.getRLPData());
        }
    } else {
        details.setCode(code.getRLPData());
    }
    // NOTE: under normal circumstances the VM type is set by the details data store
    // Do not forget to set the vmType value externally during tests!!!
    RLPElement storage = input.storageTrie;
    // load/deserialize storage trie
    if (input.isExternalStorage) {
        // ensure transition from old encoding
        details.storageTrie = new SecureTrie(details.externalStorageSource, consensusRoot);
    } else {
        details.storageTrie = new SecureTrie(null);
        details.storageTrie.deserialize((SharedRLPList) storage);
        // switch from in-memory to external storage
        details.storageTrie.getCache().setDB(details.externalStorageSource);
        details.storageTrie.sync();
    }
    if (Arrays.equals(consensusRoot, ConstantUtil.EMPTY_TRIE_HASH)) {
        details.storageTrie = new SecureTrie(details.storageTrie.getCache(), "".getBytes());
    }
    return details;
}
Also used : RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList) SecureTrie(org.aion.zero.impl.trie.SecureTrie) RLPList(org.aion.rlp.RLPList) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 13 with RLPList

use of org.aion.rlp.RLPList 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)

Example 14 with RLPList

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

the class CipherParams method parse.

public static CipherParams parse(byte[] bytes) throws UnsupportedEncodingException {
    RLPList list = (RLPList) RLP.decode2(bytes).get(0);
    CipherParams cp = new CipherParams();
    cp.setIv(new String(list.get(0).getRLPData(), "US-ASCII"));
    return cp;
}
Also used : RLPList(org.aion.rlp.RLPList)

Example 15 with RLPList

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

the class KeystoreCrypto method parse.

public static KeystoreCrypto parse(byte[] bytes) throws UnsupportedEncodingException {
    RLPList list = (RLPList) RLP.decode2(bytes).get(0);
    KeystoreCrypto kc = new KeystoreCrypto();
    kc.setCipher(new String(list.get(0).getRLPData(), "UTF-8"));
    kc.setCipherText(new String(list.get(1).getRLPData(), "US-ASCII"));
    kc.setKdf(new String(list.get(2).getRLPData(), "UTF-8"));
    kc.setMac(new String(list.get(3).getRLPData(), "US-ASCII"));
    kc.setCipherParams(CipherParams.parse(list.get(4).getRLPData()));
    kc.setKdfParams(KdfParams.parse(list.get(5).getRLPData()));
    return kc;
}
Also used : RLPList(org.aion.rlp.RLPList)

Aggregations

RLPList (org.aion.rlp.RLPList)30 RLPElement (org.aion.rlp.RLPElement)18 RLPItem (org.aion.rlp.RLPItem)7 SharedRLPList (org.aion.rlp.SharedRLPList)7 SharedRLPItem (org.aion.rlp.SharedRLPItem)5 AionAddress (org.aion.types.AionAddress)5 RLPContractDetails (org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails)5 Test (org.junit.Test)5 BigInteger (java.math.BigInteger)4 ArrayList (java.util.ArrayList)3 A0BlockHeader (org.aion.zero.types.A0BlockHeader)3 HashMap (java.util.HashMap)2 DataWord (org.aion.mcf.vm.types.DataWord)2 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)2 DatabaseType (org.aion.zero.impl.sync.DatabaseType)2 SecureTrie (org.aion.zero.impl.trie.SecureTrie)2 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)1 ISignature (org.aion.crypto.ISignature)1 TxTouchedStorage (org.aion.mcf.core.TxTouchedStorage)1 Value (org.aion.rlp.Value)1