Search in sources :

Example 31 with RLPElement

use of org.aion.rlp.RLPElement 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 32 with RLPElement

use of org.aion.rlp.RLPElement 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 33 with RLPElement

use of org.aion.rlp.RLPElement 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 34 with RLPElement

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

Example 35 with RLPElement

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

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