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;
}
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations