Search in sources :

Example 26 with RLPList

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);
    }
}
Also used : RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block) RLPList(org.aion.rlp.RLPList)

Example 27 with RLPList

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);
}
Also used : SharedRLPItem(org.aion.rlp.SharedRLPItem) RLPItem(org.aion.rlp.RLPItem) AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) SharedRLPList(org.aion.rlp.SharedRLPList) RLPList(org.aion.rlp.RLPList) Test(org.junit.Test)

Example 28 with RLPList

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));
}
Also used : SharedRLPItem(org.aion.rlp.SharedRLPItem) RLPItem(org.aion.rlp.RLPItem) AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) SharedRLPList(org.aion.rlp.SharedRLPList) RLPList(org.aion.rlp.RLPList) Test(org.junit.Test)

Example 29 with RLPList

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);
}
Also used : SharedRLPItem(org.aion.rlp.SharedRLPItem) RLPItem(org.aion.rlp.RLPItem) AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) SharedRLPList(org.aion.rlp.SharedRLPList) RLPList(org.aion.rlp.RLPList) Test(org.junit.Test)

Example 30 with RLPList

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);
}
Also used : SharedRLPItem(org.aion.rlp.SharedRLPItem) RLPItem(org.aion.rlp.RLPItem) AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) SharedRLPList(org.aion.rlp.SharedRLPList) RLPList(org.aion.rlp.RLPList) Test(org.junit.Test)

Aggregations

RLPList (org.aion.rlp.RLPList)30 RLPElement (org.aion.rlp.RLPElement)18 RLPItem (org.aion.rlp.RLPItem)7 SharedRLPList (org.aion.rlp.SharedRLPList)7 SharedRLPItem (org.aion.rlp.SharedRLPItem)5 AionAddress (org.aion.types.AionAddress)5 RLPContractDetails (org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails)5 Test (org.junit.Test)5 BigInteger (java.math.BigInteger)4 ArrayList (java.util.ArrayList)3 A0BlockHeader (org.aion.zero.types.A0BlockHeader)3 HashMap (java.util.HashMap)2 DataWord (org.aion.mcf.vm.types.DataWord)2 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)2 DatabaseType (org.aion.zero.impl.sync.DatabaseType)2 SecureTrie (org.aion.zero.impl.trie.SecureTrie)2 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)1 ISignature (org.aion.crypto.ISignature)1 TxTouchedStorage (org.aion.mcf.core.TxTouchedStorage)1 Value (org.aion.rlp.Value)1