use of org.aion.rlp.RLPList 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);
}
}
use of org.aion.rlp.RLPList in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withMissingConcatenatedData.
@Test(expected = IllegalArgumentException.class)
public void testDecode_withMissingConcatenatedData() {
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));
when(mockDatabase.get(rootHash)).thenReturn(Optional.empty());
RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, rootHash);
}
use of org.aion.rlp.RLPList in project aion by aionnetwork.
the class AvmContractDetailsTest method testDecode_withExternalStorageAndMultiCode.
@Test
public void testDecode_withExternalStorageAndMultiCode() {
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);
byte[] graphHash = RandomUtils.nextBytes(32);
byte[] graphBytes = RandomUtils.nextBytes(100);
when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash), RLP.encodeElement(graphHash))));
when(mockDatabase.get(graphHash)).thenReturn(Optional.of(graphBytes));
RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
AvmContractDetails details = AvmContractDetails.decodeAtRoot(input, mockDatabase, mockDatabase, rootHash);
assertThat(details.address).isEqualTo(address);
// because it uses the setCodes method
assertThat(details.isDirty()).isTrue();
assertThat(details.isDeleted()).isFalse();
assertThat(details.getObjectGraph()).isEqualTo(graphBytes);
assertThat(details.getCodes().size()).isEqualTo(2);
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes1));
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes2));
assertThat(details.getCode(h256(codeBytes1))).isEqualTo(codeBytes1);
assertThat(details.getCode(h256(codeBytes2))).isEqualTo(codeBytes2);
byte[] concatenated = new byte[storageHash.length + graphHash.length];
System.arraycopy(storageHash, 0, concatenated, 0, storageHash.length);
System.arraycopy(graphHash, 0, concatenated, storageHash.length, graphHash.length);
assertThat(details.getStorageHash()).isEqualTo(h256(concatenated));
}
use of org.aion.rlp.RLPList 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.RLPList in project aion by aionnetwork.
the class FvmContractDetailsTest method testDecode_withExternalStorageAndMultiCode.
@Test
public void testDecode_withExternalStorageAndMultiCode() {
AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
byte[] storageHash = RandomUtils.nextBytes(32);
RLPElement root = mock(RLPItem.class);
when(root.getRLPData()).thenReturn(storageHash);
byte[] codeBytes1 = RandomUtils.nextBytes(100);
byte[] codeBytes2 = RandomUtils.nextBytes(100);
RLPList code = new RLPList();
code.add(new RLPItem(codeBytes1));
code.add(new RLPItem(codeBytes2));
RLPContractDetails input = new RLPContractDetails(address, true, root, null, code);
FvmContractDetails details = FvmContractDetails.decodeAtRoot(input, mockDatabase, storageHash);
assertThat(details.address).isEqualTo(address);
// because it uses the setCodes method
assertThat(details.isDirty()).isTrue();
assertThat(details.isDeleted()).isFalse();
assertThat(details.getCodes().size()).isEqualTo(2);
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes1));
assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes2));
assertThat(details.getCode(h256(codeBytes1))).isEqualTo(codeBytes1);
assertThat(details.getCode(h256(codeBytes2))).isEqualTo(codeBytes2);
assertThat(details.getStorageHash()).isEqualTo(storageHash);
}
Aggregations