use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class BlockUtil method newBlockFromRlp.
/**
* Decodes the given encoding into a new instance of a block or returns {@code null} if the RLP
* encoding does not describe a valid block.
*
* @param rlp RLP encoded block data
* @return a new instance of a block or {@code null} if the RLP encoding does not describe a
* valid block
* @implNote Assumes the data is from a safe (internal) source.
*/
public static Block newBlockFromRlp(byte[] rlp) {
// return null when given empty bytes
if (rlp == null || rlp.length == 0) {
return null;
}
// attempt decoding, return null if it fails
try {
SharedRLPList params = RLP.decode2SharedList(rlp);
SharedRLPList block = (SharedRLPList) params.get(0);
SharedRLPList header = (SharedRLPList) block.get(0);
List<AionTransaction> txs = parseTransactions((SharedRLPList) block.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(rlp), e);
return null;
}
}
use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class TrieImpl method deserialize.
public void deserialize(SharedRLPList storage) {
lock.lock();
try {
SharedRLPList rlpList = (SharedRLPList) storage.get(0);
SharedRLPList valsList = (SharedRLPList) rlpList.get(1);
for (int i = 0; i < valsList.size(); ++i) {
byte[] val = valsList.get(i).getRLPData();
byte[] key = new byte[ByteUtil.EMPTY_WORD.length];
Value value = Value.fromRlpEncoded(val);
System.arraycopy(rlpList.get(0).getRLPData(), i * ByteUtil.EMPTY_WORD.length, key, 0, ByteUtil.EMPTY_WORD.length);
cache.getNodes().put(wrap(key), new Node(value));
}
this.deserializeRoot(rlpList.get(2).getRLPData());
} finally {
lock.unlock();
}
}
Aggregations