use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.
the class AionTransactionTest method testBeaconHashPresent.
@Test
public void testBeaconHashPresent() {
byte[] nonce = RandomUtils.nextBytes(16);
AionAddress to = new AionAddress(RandomUtils.nextBytes(32));
byte[] value = RandomUtils.nextBytes(16);
byte[] data = RandomUtils.nextBytes(64);
long nrg = 0;
long nrgPrice = 0;
byte type = 0;
byte[] beaconHash = ByteUtil.hexStringToBytes("0xcafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe");
AionTransaction tx = AionTransaction.create(key, nonce, to, value, data, nrg, nrgPrice, type, beaconHash);
assertArrayEquals("AionTransaction#getBeaconHash should equal the beacon hash provided in ctor", beaconHash, tx.getBeaconHash());
SharedRLPList decoded = (SharedRLPList) RLP.decode2SharedList(tx.getEncoded()).get(0);
assertEquals("wrong number of elements in RLP encoding of AionTransaction with beacon hash", TxUtil.RLP_TX_BEACON_HASH, decoded.size() - 1);
assertArrayEquals("beacon hash given in AionTransaction ctor should be present in its RLP encoding", decoded.get(TxUtil.RLP_TX_BEACON_HASH).getRLPData(), beaconHash);
AionTransaction tx2 = TxUtil.decodeUsingRlpSharedList(tx.getEncoded());
assertNotNull(tx2);
assertTransactionEquals(tx, tx2);
}
use of org.aion.rlp.SharedRLPList 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;
}
use of org.aion.rlp.SharedRLPList 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.SharedRLPList 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.SharedRLPList 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;
}
}
Aggregations