Search in sources :

Example 36 with RLPElement

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

the class LogUtility method decodeLog.

public static Log decodeLog(SharedRLPList sharedRLPList) {
    Objects.requireNonNull(sharedRLPList);
    // Can be null
    byte[] address = sharedRLPList.get(0).getRLPData();
    // Can be null
    byte[] data = sharedRLPList.get(2).getRLPData();
    SharedRLPList encodedTopics = (SharedRLPList) sharedRLPList.get(1);
    List<byte[]> topics = new ArrayList<>();
    for (RLPElement topic1 : encodedTopics) {
        byte[] topic = topic1.getRLPData();
        topics.add(topic);
    }
    Log ret;
    if (address != null && data != null) {
        if (topics.isEmpty()) {
            ret = Log.dataOnly(address, data);
        } else {
            ret = Log.topicsAndData(address, topics, data);
        }
    } else {
        throw new IllegalArgumentException("Unable to decode Log because of null " + (address == null ? "address" : "data"));
    }
    return ret;
}
Also used : Log(org.aion.types.Log) RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 37 with RLPElement

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

the class AionTxInfo method decodeToTxInfo.

private static AionTxInfo decodeToTxInfo(SharedRLPList rlpTxInfo) {
    Objects.requireNonNull(rlpTxInfo);
    AionTxReceipt receipt = new AionTxReceipt((SharedRLPList) rlpTxInfo.get(INDEX_RECEIPT));
    ByteArrayWrapper blockHash = ByteArrayWrapper.wrap(rlpTxInfo.get(INDEX_BLOCK_HASH).getRLPData());
    int index;
    byte[] txIndex = rlpTxInfo.get(INDEX_TX_INDEX).getRLPData();
    if (txIndex == null) {
        index = 0;
    } else {
        index = new BigInteger(1, txIndex).intValue();
    }
    boolean createdWithInternalTx;
    List<InternalTransaction> internalTransactions;
    switch(rlpTxInfo.size()) {
        case SIZE_OF_OLD_ENCODING:
            // old encodings are incomplete since internal tx were not stored
            createdWithInternalTx = false;
            internalTransactions = null;
            break;
        case SIZE_WITH_BASE_DATA:
            // read the completeness flag from storage
            createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
            internalTransactions = null;
            break;
        case SIZE_WITH_INTERNAL_TRANSACTIONS:
            // read the completeness flag from storage
            createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
            // decode the internal transactions
            internalTransactions = new ArrayList<>();
            SharedRLPList internalTxRlp = (SharedRLPList) rlpTxInfo.get(INDEX_INTERNAL_TX);
            for (RLPElement item : internalTxRlp) {
                internalTransactions.add(fromRlp((SharedRLPList) item));
            }
            break;
        default:
            // incorrect encoding
            return null;
    }
    return new AionTxInfo(receipt, blockHash, index, internalTransactions, createdWithInternalTx);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RLPElement(org.aion.rlp.RLPElement) BigInteger(java.math.BigInteger) AionTxReceipt(org.aion.base.AionTxReceipt) InternalTransaction(org.aion.types.InternalTransaction) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 38 with RLPElement

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

the class BlockUtil method newBlockFromUnsafeSource.

/**
 * Decodes the given encoding into a new instance of a block or returns {@code null} if the RLP
 * encoding does not describe a valid block.
 *
 * @param rlpList an RLPList instance encoding block data
 * @return a new instance of a block or {@code null} if the RLP encoding does not describe a
 *     valid block
 * @implNote Assumes the data is from an unsafe source.
 */
public static Block newBlockFromUnsafeSource(SharedRLPList rlpList) {
    // return null when given empty bytes
    if (rlpList == null || rlpList.size() != 2) {
        return null;
    }
    try {
        // parse header
        RLPElement element = rlpList.get(0);
        if (element.isList()) {
            SharedRLPList headerRLP = (SharedRLPList) element;
            byte[] type = headerRLP.get(0).getRLPData();
            if (type[0] == Seal.PROOF_OF_WORK.getSealId()) {
                MiningBlockHeader miningHeader = MiningBlockHeader.Builder.newInstance(true).withRlpList(headerRLP).build();
                SharedRLPList transactionsRLP = (SharedRLPList) rlpList.get(1);
                List<AionTransaction> txs = parseTransactions(transactionsRLP);
                if (!BlockDetailsValidator.isValidTxTrieRoot(miningHeader.getTxTrieRoot(), txs, miningHeader.getNumber(), syncLog)) {
                    return null;
                }
                return new MiningBlock(miningHeader, txs);
            } else if (type[0] == Seal.PROOF_OF_STAKE.getSealId()) {
                StakingBlockHeader stakingHeader = StakingBlockHeader.Builder.newInstance(true).withRlpList(headerRLP).build();
                SharedRLPList transactionsRLP = (SharedRLPList) rlpList.get(1);
                List<AionTransaction> txs = parseTransactions(transactionsRLP);
                if (!BlockDetailsValidator.isValidTxTrieRoot(stakingHeader.getTxTrieRoot(), txs, stakingHeader.getNumber(), syncLog)) {
                    return null;
                }
                return new StakingBlock(stakingHeader, txs);
            } else {
                return null;
            }
        } else {
            throw new IllegalArgumentException("The first element in the rlpList is not a list");
        }
    } catch (Exception e) {
        syncLog.warn("Unable to decode block bytes " + Arrays.toString(SharedRLPList.getRLPDataCopy(rlpList)), e);
        return null;
    }
}
Also used : RLPElement(org.aion.rlp.RLPElement) AionTransaction(org.aion.base.AionTransaction) ArrayList(java.util.ArrayList) List(java.util.List) SharedRLPList(org.aion.rlp.SharedRLPList) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 39 with RLPElement

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

the class ResponseBlocks method decode.

/**
 * Decodes a message into a block range response.
 *
 * @param message a {@code byte} array representing a response to a block range request.
 * @return the decoded block range response
 * @implNote The decoder returns {@code null} when given an empty message.
 */
public static ResponseBlocks 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;
        }
        List<Block> blocks = new ArrayList<>();
        Block current;
        for (RLPElement encoded : list) {
            current = BlockUtil.newBlockFromRlp(encoded.getRLPData());
            if (current == null) {
                return null;
            } else {
                blocks.add(current);
            }
        }
        return new ResponseBlocks(blocks);
    }
}
Also used : RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block) 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