Search in sources :

Example 26 with SharedRLPList

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

the class BlockUtil method newBlockFromRlp.

/**
 * 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 rlp RLP encoded 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 a safe (internal) source.
 */
public static Block newBlockFromRlp(byte[] rlp) {
    // return null when given empty bytes
    if (rlp == null || rlp.length == 0) {
        return null;
    }
    // attempt decoding, return null if it fails
    try {
        SharedRLPList params = RLP.decode2SharedList(rlp);
        SharedRLPList block = (SharedRLPList) params.get(0);
        SharedRLPList header = (SharedRLPList) block.get(0);
        List<AionTransaction> txs = parseTransactions((SharedRLPList) block.get(1));
        byte[] sealType = header.get(0).getRLPData();
        if (sealType[0] == Seal.PROOF_OF_WORK.getSealId()) {
            MiningBlockHeader miningHeader = MiningBlockHeader.Builder.newInstance().withRlpList(header).build();
            return new MiningBlock(miningHeader, txs);
        } else if (sealType[0] == Seal.PROOF_OF_STAKE.getSealId()) {
            StakingBlockHeader stakingHeader = StakingBlockHeader.Builder.newInstance().withRlpList(header).build();
            return new StakingBlock(stakingHeader, txs);
        } else {
            return null;
        }
    } catch (Exception e) {
        genLog.warn("Unable to decode block bytes " + Arrays.toString(rlp), e);
        return null;
    }
}
Also used : AionTransaction(org.aion.base.AionTransaction) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 27 with SharedRLPList

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

the class TrieImpl method deserialize.

public void deserialize(SharedRLPList storage) {
    lock.lock();
    try {
        SharedRLPList rlpList = (SharedRLPList) storage.get(0);
        SharedRLPList valsList = (SharedRLPList) rlpList.get(1);
        for (int i = 0; i < valsList.size(); ++i) {
            byte[] val = valsList.get(i).getRLPData();
            byte[] key = new byte[ByteUtil.EMPTY_WORD.length];
            Value value = Value.fromRlpEncoded(val);
            System.arraycopy(rlpList.get(0).getRLPData(), i * ByteUtil.EMPTY_WORD.length, key, 0, ByteUtil.EMPTY_WORD.length);
            cache.getNodes().put(wrap(key), new Node(value));
        }
        this.deserializeRoot(rlpList.get(2).getRLPData());
    } finally {
        lock.unlock();
    }
}
Also used : Node.isEmptyNode(org.aion.zero.impl.trie.Node.isEmptyNode) Value(org.aion.rlp.Value) SharedRLPList(org.aion.rlp.SharedRLPList)

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