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