Search in sources :

Example 21 with SharedRLPList

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

the class AionTransactionTest method testBeaconHashPresent.

@Test
public void testBeaconHashPresent() {
    byte[] nonce = RandomUtils.nextBytes(16);
    AionAddress to = new AionAddress(RandomUtils.nextBytes(32));
    byte[] value = RandomUtils.nextBytes(16);
    byte[] data = RandomUtils.nextBytes(64);
    long nrg = 0;
    long nrgPrice = 0;
    byte type = 0;
    byte[] beaconHash = ByteUtil.hexStringToBytes("0xcafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe");
    AionTransaction tx = AionTransaction.create(key, nonce, to, value, data, nrg, nrgPrice, type, beaconHash);
    assertArrayEquals("AionTransaction#getBeaconHash should equal the beacon hash provided in ctor", beaconHash, tx.getBeaconHash());
    SharedRLPList decoded = (SharedRLPList) RLP.decode2SharedList(tx.getEncoded()).get(0);
    assertEquals("wrong number of elements in RLP encoding of AionTransaction with beacon hash", TxUtil.RLP_TX_BEACON_HASH, decoded.size() - 1);
    assertArrayEquals("beacon hash given in AionTransaction ctor should be present in its RLP encoding", decoded.get(TxUtil.RLP_TX_BEACON_HASH).getRLPData(), beaconHash);
    AionTransaction tx2 = TxUtil.decodeUsingRlpSharedList(tx.getEncoded());
    assertNotNull(tx2);
    assertTransactionEquals(tx, tx2);
}
Also used : AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) SharedRLPList(org.aion.rlp.SharedRLPList) Test(org.junit.Test)

Example 22 with SharedRLPList

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

the class LogUtility method decodeLog.

public static Log decodeLog(byte[] rlp) {
    SharedRLPList decodedTxList = RLP.decode2SharedList(rlp);
    RLPElement element = decodedTxList.get(0);
    if (!element.isList()) {
        throw new IllegalArgumentException("The rlp decode error, the decoded item should be a list");
    }
    SharedRLPList logInfo = (SharedRLPList) element;
    // Can be null
    byte[] address = logInfo.get(0).getRLPData();
    // Can be null
    byte[] data = logInfo.get(2).getRLPData();
    SharedRLPList encodedTopics = (SharedRLPList) logInfo.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 23 with SharedRLPList

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

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

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

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