use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withIncorrectEncodingForConcatenatedData.
@Test(expected = IllegalArgumentException.class)
public void testDecode_withIncorrectEncodingForConcatenatedData() {
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] rootHash = RandomUtils.nextBytes(32);
RLPElement root = mock(RLPItem.class);
when(root.getRLPData()).thenReturn(rootHash);
byte[] codeBytes1 = RandomUtils.nextBytes(100);
byte[] codeBytes2 = RandomUtils.nextBytes(100);
RLPList code = new RLPList();
code.add(new RLPItem(codeBytes1));
code.add(new RLPItem(codeBytes2));
byte[] storageHash = RandomUtils.nextBytes(32);
when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeElement(storageHash)));
RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, rootHash);
}
use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class BroadcastTx method decode.
/* return the encodedData of the Transaction list, the caller function need to cast the return byte[] array
*/
public static List<byte[]> decode(final byte[] _msgBytes) {
SharedRLPList paramsList = (SharedRLPList) RLP.decode2SharedList(_msgBytes).get(0);
List<byte[]> txl = new ArrayList<>();
for (RLPElement aParamsList : paramsList) {
txl.add(SharedRLPList.getRLPDataCopy((SharedRLPList) aParamsList));
}
return txl;
}
use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class ResponseTrieData method decode.
/**
* Decodes a message into a trie node response.
*
* @param message a {@code byte} array representing a response to a trie node request.
* @return the decoded trie node response
* @implNote The decoder must return {@code null} if any of the component values are {@code
* null} or invalid.
*/
public static ResponseTrieData 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;
}
if (list.size() != TRIE_DATA_RESPONSE_COMPONENTS) {
return null;
} else {
// decode the key
byte[] hash = list.get(0).getRLPData();
if (hash.length != HASH_SIZE) {
return null;
}
// decode the value
byte[] value = list.get(1).getRLPData();
if (value.length == 0) {
return null;
}
// decode the referenced nodes
RLPElement referenced = list.get(2);
if (!(referenced instanceof RLPList)) {
return null;
}
Map<ByteArrayWrapper, byte[]> nodes = decodeReferencedNodes((RLPList) referenced);
if (nodes == null) {
return null;
}
// decode the db type
byte[] type = list.get(3).getRLPData();
DatabaseType dbType;
try {
dbType = DatabaseType.valueOf(new String(type));
} catch (IllegalArgumentException e) {
return null;
}
return new ResponseTrieData(ByteArrayWrapper.wrap(hash), value, nodes, dbType);
}
}
}
use of org.aion.rlp.RLPElement in project aion by aionnetwork.
the class ResponseTrieData method decodeReferencedNodes.
/**
* Decodes the list of key-value pair encodings into a map representation.
*
* @return a map of all the key-value pair encodings
*/
private static Map<ByteArrayWrapper, byte[]> decodeReferencedNodes(RLPList referenced) {
if (referenced.isEmpty()) {
return Collections.emptyMap();
}
RLPList current;
byte[] hash, value;
Map<ByteArrayWrapper, byte[]> nodes = new HashMap<>();
for (RLPElement pair : referenced) {
if (!(pair instanceof RLPList)) {
return null;
}
current = (RLPList) pair;
// decode the key
hash = current.get(0).getRLPData();
if (hash.length != HASH_SIZE) {
return null;
}
// decode the value
value = current.get(1).getRLPData();
if (value.length == 0) {
return null;
}
nodes.put(ByteArrayWrapper.wrap(hash), value);
}
return nodes;
}
use of org.aion.rlp.RLPElement 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;
}
}
}
Aggregations