Search in sources :

Example 1 with SharedRLPList

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

the class ResBlocksBodiesHandler method receive.

@Override
public void receive(int _nodeIdHashcode, String _displayId, final byte[] _msgBytes) {
    // for runtime survey information
    long startTime, duration;
    startTime = System.nanoTime();
    ResBlocksBodies resBlocksBodies = ResBlocksBodies.decodeUsingRef(_msgBytes);
    duration = System.nanoTime() - startTime;
    surveyLog.debug("Receive Stage 4: decode bodies, duration = {} ns.", duration);
    startTime = System.nanoTime();
    List<SharedRLPList> bodies = resBlocksBodies.getBlocksBodies();
    this.syncMgr.getSyncStats().updateResponseTime(_displayId, System.nanoTime(), RequestType.BODIES);
    if (bodies.size() == 0) {
        p2pMgr.errCheck(_nodeIdHashcode, _displayId);
        log.error("<res-bodies-empty node={}>", _displayId);
    } else {
        syncMgr.getSyncStats().updatePeerBlocks(_displayId, bodies.size(), BlockType.RECEIVED);
        syncMgr.validateAndAddBlocks(_nodeIdHashcode, _displayId, bodies);
    }
    duration = System.nanoTime() - startTime;
    surveyLog.debug("Receive Stage 5: validate bodies, duration = {} ns.", duration);
}
Also used : ResBlocksBodies(org.aion.zero.impl.sync.msg.ResBlocksBodies) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 2 with SharedRLPList

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

the class BlockUtil method newBlockFromSharedRLPList.

public static Block newBlockFromSharedRLPList(SharedRLPList rlpList) {
    // return null when given empty bytes
    if (rlpList == null || rlpList.isEmpty()) {
        return null;
    }
    try {
        SharedRLPList header = (SharedRLPList) rlpList.get(0);
        List<AionTransaction> txs = parseTransactions((SharedRLPList) rlpList.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(SharedRLPList.getRLPDataCopy(rlpList)), e);
        return null;
    }
}
Also used : AionTransaction(org.aion.base.AionTransaction) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 3 with SharedRLPList

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

the class AionTransactionTest method testBeaconHashAbsent.

@Test
public void testBeaconHashAbsent() {
    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;
    AionTransaction tx = AionTransaction.create(key, nonce, to, value, data, nrg, nrgPrice, type, null);
    assertNull("beacon hash should be null", tx.getBeaconHash());
    SharedRLPList decoded = (SharedRLPList) RLP.decode2SharedList(tx.getEncoded()).get(0);
    assertEquals("wrong number of elements in RLP encoding of AionTransaction without beacon hash", TxUtil.RLP_TX_SIG, decoded.size() - 1);
    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 4 with SharedRLPList

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

the class InvokableTxUtil method decode.

public static InternalTransaction decode(byte[] encodingWithVersion, AionAddress callingAddress, long energyPrice, long energyLimit) {
    // Right now we only have version 0, so if it is not 0, return null
    if (encodingWithVersion[0] != 0) {
        return null;
    }
    byte[] rlpEncoding = Arrays.copyOfRange(encodingWithVersion, 1, encodingWithVersion.length);
    try {
        SharedRLPList decodedTxList = RLP.decode2SharedList(rlpEncoding);
        RLPElement element = decodedTxList.get(0);
        if (!element.isList()) {
            throw new IllegalArgumentException("The rlp decode error, the decoded item should be a list");
        }
        SharedRLPList tx = (SharedRLPList) element;
        BigInteger nonce = new BigInteger(1, tx.get(RLP_META_TX_NONCE).getRLPData());
        BigInteger value = new BigInteger(1, tx.get(RLP_META_TX_VALUE).getRLPData());
        byte[] data = tx.get(RLP_META_TX_DATA).getRLPData();
        AionAddress destination;
        try {
            destination = new AionAddress(tx.get(RLP_META_TX_TO).getRLPData());
        } catch (Exception e) {
            destination = null;
        }
        AionAddress executor;
        try {
            executor = new AionAddress(tx.get(RLP_META_TX_EXECUTOR).getRLPData());
        } catch (Exception e) {
            executor = null;
        }
        if (executor != null && !executor.equals(AddressUtils.ZERO_ADDRESS) && !executor.equals(callingAddress)) {
            return null;
        }
        byte[] sigs = tx.get(RLP_META_TX_SIG).getRLPData();
        ISignature signature;
        AionAddress sender;
        if (sigs != null) {
            // Singature Factory will decode the signature based on the algo
            // presetted in main() entry.
            ISignature is = SignatureFac.fromBytes(sigs);
            if (is != null) {
                signature = is;
                sender = new AionAddress(is.getAddress());
            } else {
                return null;
            }
        } else {
            return null;
        }
        return createFromRlp(nonce, sender, destination, value, data, executor, energyLimit, energyPrice, signature, encodingWithVersion);
    } catch (Exception e) {
        LOG.error("Invokable tx -> unable to decode rlpEncoding. " + e);
        return null;
    }
}
Also used : AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) BigInteger(java.math.BigInteger) SharedRLPList(org.aion.rlp.SharedRLPList) ISignature(org.aion.crypto.ISignature)

Example 5 with SharedRLPList

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

the class KeystoreItem method parse.

public static KeystoreItem parse(byte[] bytes) throws UnsupportedEncodingException {
    RLPElement element = RLP.decode2SharedList(bytes).get(0);
    if (!element.isList()) {
        throw new IllegalArgumentException("The keystore decoded rlp element is not a list");
    }
    SharedRLPList list = (SharedRLPList) element;
    KeystoreItem ki = new KeystoreItem();
    ki.setId(new String(list.get(0).getRLPData(), "UTF-8"));
    ki.setVersion(ByteUtil.byteArrayToInt(list.get(1).getRLPData()));
    ki.setAddress(new String(list.get(2).getRLPData(), "US-ASCII"));
    ki.setKeystoreCrypto(KeystoreCrypto.parse(list.get(3).getRLPData()));
    return ki;
}
Also used : RLPElement(org.aion.rlp.RLPElement) 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