Search in sources :

Example 11 with SharedRLPList

use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.

the class DetailsDataStore method fromEncoding.

/**
 * Extracts an RLPContractDetails object from the RLP encoding.
 *
 * Accepted encodings:
 * <ul>
 *   <li>{ 0:address, 1:isExternalStorage, 2:storageRoot, 3:storageTrie, 4:code }
 *   <li>{ 0:address, 1:storageRoot, 2:code }
 * </ul>
 *
 * @param encoding The encoded ContractDetails to decode.
 */
public static RLPContractDetails fromEncoding(byte[] encoding) {
    if (encoding == null) {
        throw new NullPointerException("Cannot decode ContractDetails from null RLP encoding.");
    }
    if (encoding.length == 0) {
        throw new IllegalArgumentException("Cannot decode ContractDetails from empty RLP encoding.");
    }
    SharedRLPList decoded = (SharedRLPList) (RLP.decode2SharedList(encoding)).get(0);
    int elements = decoded.size();
    if (elements == 3 || elements == 5) {
        // extract 0:address from the encoding
        AionAddress address;
        RLPElement addressRLP = decoded.get(0);
        if (addressRLP == null || addressRLP.getRLPData() == null || addressRLP.getRLPData().length != AionAddress.LENGTH) {
            throw new IllegalArgumentException("Cannot decode ContractDetails with invalid contract address.");
        } else {
            address = new AionAddress(addressRLP.getRLPData());
        }
        if (elements == 3) {
            return new RLPContractDetails(address, true, decoded.get(1), null, decoded.get(2));
        } else {
            boolean isExternalStorage = decoded.get(1).getRLPData().length > 0;
            return new RLPContractDetails(address, isExternalStorage, decoded.get(2), decoded.get(3), decoded.get(4));
        }
    } else {
        throw new IllegalStateException("Incompatible data storage. Please shutdown the kernel and perform database migration to version 1.0 (Denali) of the kernel as instructed in the release.");
    }
}
Also used : AionAddress(org.aion.types.AionAddress) RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 12 with SharedRLPList

use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.

the class FvmContractDetailsTest method testEncodingCorrectSize.

@Test
public void testEncodingCorrectSize() {
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    byte[] code = RandomUtils.nextBytes(512);
    FvmContractDetails details = new FvmContractDetails(address, mockDatabase);
    details.setCode(code);
    // ensure correct size after VM type is set
    SharedRLPList data = (SharedRLPList) RLP.decode2SharedList(details.getEncoded()).get(0);
    assertThat(data.size()).isEqualTo(3);
}
Also used : AionAddress(org.aion.types.AionAddress) SharedRLPList(org.aion.rlp.SharedRLPList) Test(org.junit.Test)

Example 13 with SharedRLPList

use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.

the class AvmContractDetailsTest method testEncodingCorrectSize.

@Test
public void testEncodingCorrectSize() {
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    byte[] code = RandomUtils.nextBytes(512);
    AvmContractDetails details = new AvmContractDetails(address, mockDatabase, mockDatabase);
    details.setCode(code);
    // ensure correct size after VM type is set
    SharedRLPList data = (SharedRLPList) RLP.decode2SharedList(details.getEncoded()).get(0);
    assertThat(data.size()).isEqualTo(3);
}
Also used : AionAddress(org.aion.types.AionAddress) SharedRLPList(org.aion.rlp.SharedRLPList) Test(org.junit.Test)

Example 14 with SharedRLPList

use of org.aion.rlp.SharedRLPList in project aion by aionnetwork.

the class SyncMgr method validateAndAddBlocks.

/**
 * @param _nodeIdHashcode int
 * @param _displayId String
 * @param _bodies List<byte[]> Assemble and validate blocks batch and add batch to import queue
 *     from network response blocks bodies
 */
public void validateAndAddBlocks(int _nodeIdHashcode, String _displayId, final List<SharedRLPList> _bodies) {
    if (_bodies == null)
        return;
    log.debug("<received-bodies size={} node={}>", _bodies.size(), _displayId);
    // the requests are made such that the size varies to better map headers to bodies
    ByteArrayWrapper firstNodeRoot = ByteArrayWrapper.wrap(BlockUtil.getTxTrieRootFromUnsafeSource(_bodies.get(0)));
    List<BlockHeader> headers = syncHeaderRequestManager.matchAndDropHeaders(_nodeIdHashcode, _bodies.size(), firstNodeRoot);
    if (headers == null) {
        log.debug("<assemble-and-validate-blocks could not match headers for node={} size={} txTrieRoot={}>", _displayId, _bodies.size(), firstNodeRoot);
        return;
    }
    // assemble batch
    List<Block> blocks = new ArrayList<>(_bodies.size());
    Iterator<BlockHeader> headerIt = headers.iterator();
    Iterator<SharedRLPList> bodyIt = _bodies.iterator();
    while (headerIt.hasNext() && bodyIt.hasNext()) {
        Block block = BlockUtil.newBlockWithHeaderFromUnsafeSource(headerIt.next(), (SharedRLPList) bodyIt.next().get(0));
        if (block == null) {
            log.debug("<assemble-and-validate-blocks node={} size={}>", _displayId, _bodies.size());
            break;
        } else {
            blocks.add(block);
        }
    }
    int m = blocks.size();
    if (m == 0) {
        return;
    }
    log.debug("<assembled-blocks from={} size={} node={}>", blocks.get(0).getNumber(), blocks.size(), _displayId);
    // add batch
    syncExecutors.execute(() -> filterBlocks(new BlocksWrapper(_nodeIdHashcode, _displayId, blocks)));
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block) BlockHeader(org.aion.zero.impl.types.BlockHeader) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 15 with SharedRLPList

use of org.aion.rlp.SharedRLPList 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;
}
Also used : RLPElement(org.aion.rlp.RLPElement) ArrayList(java.util.ArrayList) SharedRLPList(org.aion.rlp.SharedRLPList)

Aggregations

SharedRLPList (org.aion.rlp.SharedRLPList)27 RLPElement (org.aion.rlp.RLPElement)18 ArrayList (java.util.ArrayList)6 AionAddress (org.aion.types.AionAddress)6 AionTransaction (org.aion.base.AionTransaction)5 Test (org.junit.Test)4 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)3 BigInteger (java.math.BigInteger)2 RLPList (org.aion.rlp.RLPList)2 Log (org.aion.types.Log)2 SecureTrie (org.aion.zero.impl.trie.SecureTrie)2 Block (org.aion.zero.impl.types.Block)2 BlockHeader (org.aion.zero.impl.types.BlockHeader)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AionTxReceipt (org.aion.base.AionTxReceipt)1 ISignature (org.aion.crypto.ISignature)1 Value (org.aion.rlp.Value)1 InternalTransaction (org.aion.types.InternalTransaction)1