Search in sources :

Example 26 with RLPElement

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

the class AvmContractDetailsTest method testDecode_withIncorrectEncodingForConcatenatedData.

@Test(expected = IllegalArgumentException.class)
public void testDecode_withIncorrectEncodingForConcatenatedData() {
    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.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 27 with RLPElement

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

the class BroadcastTx method decode.

/* return the encodedData of the Transaction list, the caller function need to cast the return byte[] array
     */
public static List<byte[]> decode(final byte[] _msgBytes) {
    SharedRLPList paramsList = (SharedRLPList) RLP.decode2SharedList(_msgBytes).get(0);
    List<byte[]> txl = new ArrayList<>();
    for (RLPElement aParamsList : paramsList) {
        txl.add(SharedRLPList.getRLPDataCopy((SharedRLPList) aParamsList));
    }
    return txl;
}
Also used : RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 28 with RLPElement

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

the class ResponseTrieData method decode.

/**
 * Decodes a message into a trie node response.
 *
 * @param message a {@code byte} array representing a response to a trie node request.
 * @return the decoded trie node response
 * @implNote The decoder must return {@code null} if any of the component values are {@code
 *     null} or invalid.
 */
public static ResponseTrieData decode(final byte[] message) {
    if (message == null || message.length == 0) {
        return null;
    } else {
        RLPList list = RLP.decode2(message);
        if (list.get(0) instanceof RLPList) {
            list = (RLPList) list.get(0);
        } else {
            return null;
        }
        if (list.size() != TRIE_DATA_RESPONSE_COMPONENTS) {
            return null;
        } else {
            // decode the key
            byte[] hash = list.get(0).getRLPData();
            if (hash.length != HASH_SIZE) {
                return null;
            }
            // decode the value
            byte[] value = list.get(1).getRLPData();
            if (value.length == 0) {
                return null;
            }
            // decode the referenced nodes
            RLPElement referenced = list.get(2);
            if (!(referenced instanceof RLPList)) {
                return null;
            }
            Map<ByteArrayWrapper, byte[]> nodes = decodeReferencedNodes((RLPList) referenced);
            if (nodes == null) {
                return null;
            }
            // decode the db type
            byte[] type = list.get(3).getRLPData();
            DatabaseType dbType;
            try {
                dbType = DatabaseType.valueOf(new String(type));
            } catch (IllegalArgumentException e) {
                return null;
            }
            return new ResponseTrieData(ByteArrayWrapper.wrap(hash), value, nodes, dbType);
        }
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) DatabaseType(org.aion.zero.impl.sync.DatabaseType) RLPElement(org.aion.rlp.RLPElement) RLPList(org.aion.rlp.RLPList)

Example 29 with RLPElement

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

the class ResponseTrieData method decodeReferencedNodes.

/**
 * Decodes the list of key-value pair encodings into a map representation.
 *
 * @return a map of all the key-value pair encodings
 */
private static Map<ByteArrayWrapper, byte[]> decodeReferencedNodes(RLPList referenced) {
    if (referenced.isEmpty()) {
        return Collections.emptyMap();
    }
    RLPList current;
    byte[] hash, value;
    Map<ByteArrayWrapper, byte[]> nodes = new HashMap<>();
    for (RLPElement pair : referenced) {
        if (!(pair instanceof RLPList)) {
            return null;
        }
        current = (RLPList) pair;
        // decode the key
        hash = current.get(0).getRLPData();
        if (hash.length != HASH_SIZE) {
            return null;
        }
        // decode the value
        value = current.get(1).getRLPData();
        if (value.length == 0) {
            return null;
        }
        nodes.put(ByteArrayWrapper.wrap(hash), value);
    }
    return nodes;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) RLPElement(org.aion.rlp.RLPElement) RLPList(org.aion.rlp.RLPList)

Example 30 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, Logger logger) {
    if (_msgBytes == null || _msgBytes.length == 0)
        return null;
    else {
        try {
            SharedRLPList list = (SharedRLPList) RLP.decode2SharedList(_msgBytes).get(0);
            List<BlockHeader> blockHeaders = new ArrayList<>();
            for (RLPElement aList : list) {
                blockHeaders.add(BlockUtil.newHeaderFromUnsafeSource((SharedRLPList) aList));
            }
            return new ResBlocksHeaders(blockHeaders);
        } catch (Exception ex) {
            if (logger != null) {
                logger.debug("ResBlocksHeaders decode failed!", ex);
            }
            return null;
        }
    }
}
Also used : RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) BlockHeader(org.aion.zero.impl.types.BlockHeader) 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