use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class LogUtility method decodeLog.
public static Log decodeLog(SharedRLPList sharedRLPList) {
Objects.requireNonNull(sharedRLPList);
// Can be null
byte[] address = sharedRLPList.get(0).getRLPData();
// Can be null
byte[] data = sharedRLPList.get(2).getRLPData();
SharedRLPList encodedTopics = (SharedRLPList) sharedRLPList.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;
}
use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class AionTxInfo method decodeToTxInfo.
private static AionTxInfo decodeToTxInfo(SharedRLPList rlpTxInfo) {
Objects.requireNonNull(rlpTxInfo);
AionTxReceipt receipt = new AionTxReceipt((SharedRLPList) rlpTxInfo.get(INDEX_RECEIPT));
ByteArrayWrapper blockHash = ByteArrayWrapper.wrap(rlpTxInfo.get(INDEX_BLOCK_HASH).getRLPData());
int index;
byte[] txIndex = rlpTxInfo.get(INDEX_TX_INDEX).getRLPData();
if (txIndex == null) {
index = 0;
} else {
index = new BigInteger(1, txIndex).intValue();
}
boolean createdWithInternalTx;
List<InternalTransaction> internalTransactions;
switch(rlpTxInfo.size()) {
case SIZE_OF_OLD_ENCODING:
// old encodings are incomplete since internal tx were not stored
createdWithInternalTx = false;
internalTransactions = null;
break;
case SIZE_WITH_BASE_DATA:
// read the completeness flag from storage
createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
internalTransactions = null;
break;
case SIZE_WITH_INTERNAL_TRANSACTIONS:
// read the completeness flag from storage
createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
// decode the internal transactions
internalTransactions = new ArrayList<>();
SharedRLPList internalTxRlp = (SharedRLPList) rlpTxInfo.get(INDEX_INTERNAL_TX);
for (RLPElement item : internalTxRlp) {
internalTransactions.add(fromRlp((SharedRLPList) item));
}
break;
default:
// incorrect encoding
return null;
}
return new AionTxInfo(receipt, blockHash, index, internalTransactions, createdWithInternalTx);
}
use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class BlockUtil method newBlockFromUnsafeSource.
/**
* 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 rlpList an RLPList instance encoding 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 an unsafe source.
*/
public static Block newBlockFromUnsafeSource(SharedRLPList rlpList) {
// return null when given empty bytes
if (rlpList == null || rlpList.size() != 2) {
return null;
}
try {
// parse header
RLPElement element = rlpList.get(0);
if (element.isList()) {
SharedRLPList headerRLP = (SharedRLPList) element;
byte[] type = headerRLP.get(0).getRLPData();
if (type[0] == Seal.PROOF_OF_WORK.getSealId()) {
MiningBlockHeader miningHeader = MiningBlockHeader.Builder.newInstance(true).withRlpList(headerRLP).build();
SharedRLPList transactionsRLP = (SharedRLPList) rlpList.get(1);
List<AionTransaction> txs = parseTransactions(transactionsRLP);
if (!BlockDetailsValidator.isValidTxTrieRoot(miningHeader.getTxTrieRoot(), txs, miningHeader.getNumber(), syncLog)) {
return null;
}
return new MiningBlock(miningHeader, txs);
} else if (type[0] == Seal.PROOF_OF_STAKE.getSealId()) {
StakingBlockHeader stakingHeader = StakingBlockHeader.Builder.newInstance(true).withRlpList(headerRLP).build();
SharedRLPList transactionsRLP = (SharedRLPList) rlpList.get(1);
List<AionTransaction> txs = parseTransactions(transactionsRLP);
if (!BlockDetailsValidator.isValidTxTrieRoot(stakingHeader.getTxTrieRoot(), txs, stakingHeader.getNumber(), syncLog)) {
return null;
}
return new StakingBlock(stakingHeader, txs);
} else {
return null;
}
} else {
throw new IllegalArgumentException("The first element in the rlpList is not a list");
}
} catch (Exception e) {
syncLog.warn("Unable to decode block bytes " + Arrays.toString(SharedRLPList.getRLPDataCopy(rlpList)), e);
return null;
}
}
use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class ResponseBlocks method decode.
/**
* Decodes a message into a block range response.
*
* @param message a {@code byte} array representing a response to a block range request.
* @return the decoded block range response
* @implNote The decoder returns {@code null} when given an empty message.
*/
public static ResponseBlocks decode(final byte[] message) {
if (message == null || message.length == 0) {
return null;
} else {
RLPList list = RLP.decode2(message);
if (list.get(0) instanceof RLPList) {
list = (RLPList) list.get(0);
} else {
return null;
}
List<Block> blocks = new ArrayList<>();
Block current;
for (RLPElement encoded : list) {
current = BlockUtil.newBlockFromRlp(encoded.getRLPData());
if (current == null) {
return null;
} else {
blocks.add(current);
}
}
return new ResponseBlocks(blocks);
}
}
Aggregations