Search in sources :

Example 16 with SharedRLPList

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

Example 17 with SharedRLPList

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

the class BroadcastNewBlockHandler method receive.

@Override
public void receive(int _nodeIdHashcode, String _displayId, final byte[] _msgBytes) {
    // for runtime survey information
    long startTime, duration;
    if (_msgBytes == null)
        return;
    startTime = System.nanoTime();
    byte[] rawdata = BroadcastNewBlock.decode(_msgBytes);
    if (rawdata == null) {
        p2pMgr.errCheck(_nodeIdHashcode, _displayId);
        log.error("<new-block-handler decode-error, from {} len: {}>", _displayId, _msgBytes.length);
        if (log.isTraceEnabled()) {
            log.trace("new-block-handler dump: {}", ByteUtil.toHexString(_msgBytes));
        }
        duration = System.nanoTime() - startTime;
        surveyLog.debug("Receive Stage 6: process propagated block, duration = {} ns.", duration);
        return;
    }
    try {
        // preventative try-catch: it's unlikely that exceptions can pass up to here
        SharedRLPList list = RLP.decode2SharedList(rawdata);
        RLPElement element = list.get(0);
        if (element.isList()) {
            // returns null when decoding failed
            Block block = BlockUtil.newBlockFromUnsafeSource((SharedRLPList) element);
            if (block != null) {
                BlockPropagationHandler.PropStatus result = this.propHandler.processIncomingBlock(_nodeIdHashcode, _displayId, block);
                duration = System.nanoTime() - startTime;
                surveyLog.debug("Receive Stage 6: process propagated block, duration = {} ns.", duration);
                log.debug("<block-prop node=" + _displayId + " block-hash=" + block.getShortHash() + " status=" + result.name() + ">");
            }
        } else {
            throw new IllegalArgumentException("decoded data is not a list.");
        }
    } catch (Exception e) {
        log.error("RLP decode error!", e);
    }
}
Also used : RLPElement(org.aion.rlp.RLPElement) BroadcastNewBlock(org.aion.zero.impl.sync.msg.BroadcastNewBlock) Block(org.aion.zero.impl.types.Block) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 18 with SharedRLPList

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

the class TransformedCodeSerializer method decodeInnerMap.

private static Map<Integer, byte[]> decodeInnerMap(SharedRLPList list) {
    Map<Integer, byte[]> map = new HashMap<>();
    for (RLPElement e : list) {
        // validity check pair
        if (!(e.isList())) {
            return null;
        }
        SharedRLPList pair = (SharedRLPList) e;
        // validity check
        if (pair.size() != 2) {
            return null;
        }
        int avmVersion = Byte.toUnsignedInt(pair.get(0).getRLPData()[0]);
        byte[] transformedCode = pair.get(1).getRLPData();
        // zero (i.e. false) decodes to empty byte array
        map.put(avmVersion, transformedCode);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 19 with SharedRLPList

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

the class KdfParams method parse.

public static KdfParams 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;
    KdfParams kdfParams = new KdfParams();
    kdfParams.setC(ByteUtil.byteArrayToInt(list.get(0).getRLPData()));
    kdfParams.setDklen(ByteUtil.byteArrayToInt(list.get(1).getRLPData()));
    kdfParams.setN(ByteUtil.byteArrayToInt(list.get(2).getRLPData()));
    kdfParams.setP(ByteUtil.byteArrayToInt(list.get(3).getRLPData()));
    kdfParams.setR(ByteUtil.byteArrayToInt(list.get(4).getRLPData()));
    kdfParams.setSalt(new String(list.get(5).getRLPData(), "US-ASCII"));
    return kdfParams;
}
Also used : RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 20 with SharedRLPList

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

the class KeystoreCrypto method parse.

public static KeystoreCrypto 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;
    KeystoreCrypto kc = new KeystoreCrypto();
    kc.setCipher(new String(list.get(0).getRLPData(), "UTF-8"));
    kc.setCipherText(new String(list.get(1).getRLPData(), "US-ASCII"));
    kc.setKdf(new String(list.get(2).getRLPData(), "UTF-8"));
    kc.setMac(new String(list.get(3).getRLPData(), "US-ASCII"));
    kc.setCipherParams(CipherParams.parse(list.get(4).getRLPData()));
    kc.setKdfParams(KdfParams.parse(list.get(5).getRLPData()));
    return kc;
}
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