Search in sources :

Example 16 with RLPElement

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

the class AionBlockHeaderWrapper method parse.

protected void parse(byte[] bytes) {
    List<RLPElement> params = RLP.decode2(bytes);
    List<RLPElement> wrapper = (RLPList) params.get(0);
    byte[] headerBytes = wrapper.get(0).getRLPData();
    this.header = new A0BlockHeader(headerBytes);
    this.nodeId = wrapper.get(1).getRLPData();
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) RLPElement(org.aion.rlp.RLPElement) RLPList(org.aion.rlp.RLPList)

Example 17 with RLPElement

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

the class ResBlocksHeaders method decode.

public static ResBlocksHeaders decode(final byte[] _msgBytes) {
    if (_msgBytes == null || _msgBytes.length == 0)
        return null;
    else {
        try {
            RLPList list = (RLPList) RLP.decode2(_msgBytes).get(0);
            List<A0BlockHeader> blockHeaders = new ArrayList<>();
            for (RLPElement aList : list) {
                RLPList rlpData = ((RLPList) aList);
                blockHeaders.add(A0BlockHeader.fromRLP(rlpData, true));
            }
            return new ResBlocksHeaders(blockHeaders);
        } catch (Exception ex) {
            return null;
        }
    }
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) RLPList(org.aion.rlp.RLPList)

Example 18 with RLPElement

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

the class AionContractDetailsImpl method decode.

@Override
public void decode(byte[] rlpCode) {
    RLPList data = RLP.decode2(rlpCode);
    RLPList rlpList = (RLPList) data.get(0);
    RLPItem address = (RLPItem) rlpList.get(0);
    RLPItem isExternalStorage = (RLPItem) rlpList.get(1);
    RLPItem storage = (RLPItem) rlpList.get(2);
    RLPElement code = rlpList.get(3);
    RLPList keys = (RLPList) rlpList.get(4);
    RLPItem storageRoot = (RLPItem) rlpList.get(5);
    if (address.getRLPData() == null) {
        this.address = Address.EMPTY_ADDRESS();
    } else {
        this.address = Address.wrap(address.getRLPData());
    }
    this.externalStorage = !Arrays.equals(isExternalStorage.getRLPData(), EMPTY_BYTE_ARRAY);
    this.storageTrie.deserialize(storage.getRLPData());
    if (code instanceof RLPList) {
        for (RLPElement e : ((RLPList) code)) {
            setCode(e.getRLPData());
        }
    } else {
        setCode(code.getRLPData());
    }
    for (RLPElement key : keys) {
        addKey(key.getRLPData());
    }
    if (externalStorage) {
        storageTrie.withPruningEnabled(prune >= 0);
        storageTrie.setRoot(storageRoot.getRLPData());
        storageTrie.getCache().setDB(getExternalStorageDataSource());
    }
    externalStorage = (storage.getRLPData().length > detailsInMemoryStorageLimit) || externalStorage;
    this.rlpEncoded = rlpCode;
}
Also used : RLPItem(org.aion.rlp.RLPItem) RLPElement(org.aion.rlp.RLPElement) RLPList(org.aion.rlp.RLPList)

Example 19 with RLPElement

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

Example 20 with RLPElement

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

the class DetailsDataStore method fromEncoding.

/**
 * Extracts an RLPContractDetails object from the RLP encoding.
 *
 * Accepted encodings:
 * <ul>
 *   <li>{ 0:address, 1:isExternalStorage, 2:storageRoot, 3:storageTrie, 4:code }
 *   <li>{ 0:address, 1:storageRoot, 2:code }
 * </ul>
 *
 * @param encoding The encoded ContractDetails to decode.
 */
public static RLPContractDetails fromEncoding(byte[] encoding) {
    if (encoding == null) {
        throw new NullPointerException("Cannot decode ContractDetails from null RLP encoding.");
    }
    if (encoding.length == 0) {
        throw new IllegalArgumentException("Cannot decode ContractDetails from empty RLP encoding.");
    }
    SharedRLPList decoded = (SharedRLPList) (RLP.decode2SharedList(encoding)).get(0);
    int elements = decoded.size();
    if (elements == 3 || elements == 5) {
        // extract 0:address from the encoding
        AionAddress address;
        RLPElement addressRLP = decoded.get(0);
        if (addressRLP == null || addressRLP.getRLPData() == null || addressRLP.getRLPData().length != AionAddress.LENGTH) {
            throw new IllegalArgumentException("Cannot decode ContractDetails with invalid contract address.");
        } else {
            address = new AionAddress(addressRLP.getRLPData());
        }
        if (elements == 3) {
            return new RLPContractDetails(address, true, decoded.get(1), null, decoded.get(2));
        } else {
            boolean isExternalStorage = decoded.get(1).getRLPData().length > 0;
            return new RLPContractDetails(address, isExternalStorage, decoded.get(2), decoded.get(3), decoded.get(4));
        }
    } else {
        throw new IllegalStateException("Incompatible data storage. Please shutdown the kernel and perform database migration to version 1.0 (Denali) of the kernel as instructed in the release.");
    }
}
Also used : AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList)

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