Search in sources :

Example 51 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class ContractInformation method deserializeInformation.

private static Information deserializeInformation(SharedRLPList list) {
    // validity check
    if (list.size() != 2)
        return null;
    Information info = new Information();
    // validity check blocks
    RLPElement code = list.get(0);
    if (!(code.isList()))
        return null;
    for (RLPElement e : ((SharedRLPList) code)) {
        // validity check pair
        if (!(e.isList()))
            return null;
        SharedRLPList pair = (SharedRLPList) e;
        // validity check hash
        if (pair.size() != 2) {
            return null;
        }
        byte[] key = pair.get(0).getRLPData();
        if (key.length != HASH_SIZE) {
            return null;
        }
        // validity check completeness status
        byte[] flag = pair.get(1).getRLPData();
        if (flag.length > 1 || (flag.length == 1 && flag[0] != 1))
            return null;
        ByteArrayWrapper keyWrap = ByteArrayWrapper.wrap(key);
        // zero (i.e. false) decodes to empty byte array
        info.blocks.put(keyWrap, flag.length == 1);
    }
    // validity check VM
    byte[] array = list.get(1).getRLPData();
    if (array.length != 1)
        return null;
    info.vm = InternalVmType.getInstance(array[0]);
    // return correct instance
    return info;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RLPElement(org.aion.rlp.RLPElement) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 52 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class FvmContractDetails method getEncoded.

/**
 * Returns an rlp encoding of this FvmContractDetails object.
 *
 * <p>The encoding is a list of 3 elements:<br>
 * { 0:address, 1:storageRoot, 2:code }
 *
 * @return an rlp encoding of this.
 */
@Override
public byte[] getEncoded() {
    byte[] rlpAddress = RLP.encodeElement(address.toByteArray());
    byte[] rlpStorageRoot = RLP.encodeElement(storageTrie.getRootHash());
    byte[][] codes = new byte[getCodes().size()][];
    int i = 0;
    for (ByteArrayWrapper bytes : this.getCodes().values()) {
        codes[i++] = RLP.encodeElement(bytes.toBytes());
    }
    byte[] rlpCode = RLP.encodeList(codes);
    return RLP.encodeList(rlpAddress, rlpStorageRoot, rlpCode);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 53 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class AionRepositoryImpl method pruneBlocks.

private void pruneBlocks(long currentBlockNumber) {
    if (currentBlockNumber > bestBlockNumber) {
        // Prune only on increasing blocks
        long pruneBlockNumber = currentBlockNumber - pruneBlockCount;
        if (pruneBlockNumber >= 0) {
            byte[] pruneBlockHash = blockStore.getBlockHashByNumber(pruneBlockNumber);
            if (pruneBlockHash != null) {
                ByteArrayWrapper hash = ByteArrayWrapper.wrap(pruneBlockHash);
                stateDSPrune.prune(hash, pruneBlockNumber);
                detailsDS.getStorageDSPrune().prune(hash, pruneBlockNumber);
            }
        }
    }
    bestBlockNumber = currentBlockNumber;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 54 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class AionRepositoryImpl method getReferencedStorageNodes.

@VisibleForTesting
public List<byte[]> getReferencedStorageNodes(byte[] value, int limit, AionAddress contract) {
    if (limit <= 0) {
        return Collections.emptyList();
    } else {
        byte[] subKey = h256(("details-storage/" + contract.toString()).getBytes());
        ByteArrayKeyValueStore db = new XorDataSource(selectDatabase(DatabaseType.STORAGE), subKey, false);
        Trie trie = new SecureTrie(db);
        Map<ByteArrayWrapper, byte[]> refs = trie.getReferencedTrieNodes(value, limit);
        List<byte[]> converted = new ArrayList<>();
        for (ByteArrayWrapper key : refs.keySet()) {
            converted.add(ByteUtil.xorAlignRight(key.toBytes(), subKey));
        }
        return converted;
    }
}
Also used : XorDataSource(org.aion.db.store.XorDataSource) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) ByteArrayKeyValueStore(org.aion.db.impl.ByteArrayKeyValueStore) SecureTrie(org.aion.zero.impl.trie.SecureTrie) Trie(org.aion.zero.impl.trie.Trie) SecureTrie(org.aion.zero.impl.trie.SecureTrie) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 55 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TrieTest method testGetMissingNodes_wCompleteTrie_wStartFromValue.

@Test
public void testGetMissingNodes_wCompleteTrie_wStartFromValue() {
    MockDB mockDB = new MockDB("temp", log);
    mockDB.open();
    TrieImpl trie = new TrieImpl(mockDB);
    for (Map.Entry<ByteArrayWrapper, byte[]> e : getSampleTrieUpdates().entrySet()) {
        trie.update(e.getKey().toBytes(), e.getValue());
    }
    trie.getCache().commitForTest();
    byte[] root = trie.getRootHash();
    byte[] value = mockDB.get(root).get();
    trie = new TrieImpl(mockDB);
    assertThat(trie.getMissingNodes(value)).isEmpty();
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) MockDB(org.aion.db.impl.mockdb.MockDB) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)130 Test (org.junit.Test)51 HashMap (java.util.HashMap)39 ArrayList (java.util.ArrayList)33 AionAddress (org.aion.types.AionAddress)26 Block (org.aion.zero.impl.types.Block)24 Map (java.util.Map)20 BigInteger (java.math.BigInteger)14 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 IOException (java.io.IOException)13 MockDB (org.aion.db.impl.mockdb.MockDB)13 DataWord (org.aion.util.types.DataWord)13 PooledTransaction (org.aion.base.PooledTransaction)11 List (java.util.List)10 AionTransaction (org.aion.base.AionTransaction)10 Properties (java.util.Properties)8 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 ECKey (org.aion.crypto.ECKey)5 RLPElement (org.aion.rlp.RLPElement)5