Search in sources :

Example 6 with SharedRLPList

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

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

the class ContractInformation method deserializeInformation.

private static Information deserializeInformation(SharedRLPList list) {
    // validity check
    if (list.size() != 2)
        return null;
    Information info = new Information();
    // validity check blocks
    RLPElement code = list.get(0);
    if (!(code.isList()))
        return null;
    for (RLPElement e : ((SharedRLPList) code)) {
        // validity check pair
        if (!(e.isList()))
            return null;
        SharedRLPList pair = (SharedRLPList) e;
        // validity check hash
        if (pair.size() != 2) {
            return null;
        }
        byte[] key = pair.get(0).getRLPData();
        if (key.length != HASH_SIZE) {
            return null;
        }
        // validity check completeness status
        byte[] flag = pair.get(1).getRLPData();
        if (flag.length > 1 || (flag.length == 1 && flag[0] != 1))
            return null;
        ByteArrayWrapper keyWrap = ByteArrayWrapper.wrap(key);
        // zero (i.e. false) decodes to empty byte array
        info.blocks.put(keyWrap, flag.length == 1);
    }
    // validity check VM
    byte[] array = list.get(1).getRLPData();
    if (array.length != 1)
        return null;
    info.vm = InternalVmType.getInstance(array[0]);
    // return correct instance
    return info;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 8 with SharedRLPList

use of org.aion.rlp.SharedRLPList 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 9 with SharedRLPList

use of org.aion.rlp.SharedRLPList 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 10 with SharedRLPList

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

the class AionRepositoryImpl method loadImportableState.

@VisibleForTesting
public void loadImportableState(byte[] fullState, DatabaseType dbType) {
    SharedRLPList data = RLP.decode2SharedList(fullState);
    RLPElement elements = data.get(0);
    if (elements.isList()) {
        for (RLPElement element : (SharedRLPList) elements) {
            if (element.isList()) {
                byte[] data0;
                if (data.get(0).isList()) {
                    data0 = SharedRLPList.getRLPDataCopy((SharedRLPList) data.get(0));
                } else {
                    data0 = data.get(0).getRLPData();
                }
                byte[] data1;
                if (data.get(1).isList()) {
                    data1 = SharedRLPList.getRLPDataCopy((SharedRLPList) data.get(1));
                } else {
                    data1 = data.get(1).getRLPData();
                }
                importTrieNode(data0, data1, dbType);
            } else {
                throw new IllegalStateException();
            }
        }
    } else {
        throw new IllegalStateException();
    }
}
Also used : RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

SharedRLPList (org.aion.rlp.SharedRLPList)27 RLPElement (org.aion.rlp.RLPElement)18 ArrayList (java.util.ArrayList)6 AionAddress (org.aion.types.AionAddress)6 AionTransaction (org.aion.base.AionTransaction)5 Test (org.junit.Test)4 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)3 BigInteger (java.math.BigInteger)2 RLPList (org.aion.rlp.RLPList)2 Log (org.aion.types.Log)2 SecureTrie (org.aion.zero.impl.trie.SecureTrie)2 Block (org.aion.zero.impl.types.Block)2 BlockHeader (org.aion.zero.impl.types.BlockHeader)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AionTxReceipt (org.aion.base.AionTxReceipt)1 ISignature (org.aion.crypto.ISignature)1 Value (org.aion.rlp.Value)1 InternalTransaction (org.aion.types.InternalTransaction)1