Search in sources :

Example 31 with ByteArrayWrapper

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

the class TrieImpl method copy.

// Returns a copy of this trie
public TrieImpl copy() {
    synchronized (cache) {
        TrieImpl trie = new TrieImpl(this.cache.getDb(), this.root);
        for (ByteArrayWrapper key : this.cache.getNodes().keySet()) {
            Node node = this.cache.getNodes().get(key);
            trie.cache.getNodes().put(key, node.copy());
        }
        return trie;
    }
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Example 32 with ByteArrayWrapper

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

the class TrieImpl method serialize.

public byte[] serialize() {
    synchronized (cache) {
        Map<ByteArrayWrapper, Node> map = getCache().getNodes();
        int keysTotalSize = 0;
        int valsTotalSize = 0;
        Set<ByteArrayWrapper> keys = map.keySet();
        for (ByteArrayWrapper key : keys) {
            Node node = map.get(key);
            if (node == null) {
                continue;
            }
            byte[] keyBytes = key.getData();
            keysTotalSize += keyBytes.length;
            byte[] valBytes = node.getValue().getData();
            valsTotalSize += valBytes.length + calcElementPrefixSize(valBytes);
        }
        byte[] root = null;
        try {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(b);
            o.writeObject(this.getRoot());
            root = b.toByteArray();
            root = RLP.encodeElement(root);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] keysHeader = RLP.encodeLongElementHeader(keysTotalSize);
        byte[] valsHeader = RLP.encodeListHeader(valsTotalSize);
        byte[] listHeader = RLP.encodeListHeader(keysTotalSize + keysHeader.length + valsTotalSize + valsHeader.length + root.length);
        byte[] rlpData = new byte[keysTotalSize + keysHeader.length + valsTotalSize + valsHeader.length + listHeader.length + root.length];
        // copy headers:
        // [ rlp_list_header, rlp_keys_header, rlp_keys, rlp_vals_header,
        // rlp_val]
        System.arraycopy(listHeader, 0, rlpData, 0, listHeader.length);
        System.arraycopy(keysHeader, 0, rlpData, listHeader.length, keysHeader.length);
        System.arraycopy(valsHeader, 0, rlpData, (listHeader.length + keysHeader.length + keysTotalSize), valsHeader.length);
        System.arraycopy(root, 0, rlpData, (listHeader.length + keysHeader.length + keysTotalSize + valsTotalSize + valsHeader.length), root.length);
        int k_1 = 0;
        int k_2 = 0;
        for (ByteArrayWrapper key : keys) {
            Node node = map.get(key);
            if (node == null) {
                continue;
            }
            System.arraycopy(key.getData(), 0, rlpData, (listHeader.length + keysHeader.length + k_1), key.getData().length);
            k_1 += key.getData().length;
            byte[] valBytes = RLP.encodeElement(node.getValue().getData());
            System.arraycopy(valBytes, 0, rlpData, listHeader.length + keysHeader.length + keysTotalSize + valsHeader.length + k_2, valBytes.length);
            k_2 += valBytes.length;
        }
        return rlpData;
    }
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 33 with ByteArrayWrapper

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

the class RLPTest method testEncodeSet_1.

@Test
public void testEncodeSet_1() {
    Set<ByteArrayWrapper> data = new HashSet<>();
    ByteArrayWrapper element1 = new ByteArrayWrapper(Hex.decode("1111111111111111111111111111111111111111111111111111111111111111"));
    ByteArrayWrapper element2 = new ByteArrayWrapper(Hex.decode("2222222222222222222222222222222222222222222222222222222222222222"));
    data.add(element1);
    data.add(element2);
    byte[] setEncoded = encodeSet(data);
    RLPList list = (RLPList) decode2(setEncoded).get(0);
    byte[] element1_ = list.get(0).getRLPData();
    byte[] element2_ = list.get(1).getRLPData();
    assertTrue(data.contains(wrap(element1_)));
    assertTrue(data.contains(wrap(element2_)));
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 34 with ByteArrayWrapper

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

the class AbstractSyncQueue method getParent.

public HeaderElement<BLK, BHW> getParent(HeaderElement<?, ?> self) {
    long bn = self.header.getNumber();
    Map<ByteArrayWrapper, HeaderElement<BLK, BHW>> genHeaders = headers.get(bn - 1);
    if (genHeaders == null) {
        return null;
    }
    return genHeaders.get(new ByteArrayWrapper(self.header.getHeader().getParentHash()));
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Example 35 with ByteArrayWrapper

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

the class DetailsDataStore method update.

public synchronized void update(Address key, IContractDetails<DataWord> contractDetails) {
    contractDetails.setAddress(key);
    ByteArrayWrapper wrappedKey = wrap(key.toBytes());
    // Put into cache.
    byte[] rawDetails = contractDetails == null ? null : contractDetails.getEncoded();
    detailsSrc.put(key.toBytes(), rawDetails);
    // Remove from the remove set.
    removes.remove(wrappedKey);
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Aggregations

ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)45 BigInteger (java.math.BigInteger)10 Address (org.aion.base.type.Address)5 ITransaction (org.aion.base.type.ITransaction)4 Entry (java.util.Map.Entry)3 DataWord (org.aion.mcf.vm.types.DataWord)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 Test (org.junit.Test)3 java.util (java.util)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ImportResult (org.aion.mcf.core.ImportResult)2 Value (org.aion.rlp.Value)2 AccountState (org.aion.txpool.common.AccountState)2 TxDependList (org.aion.txpool.common.TxDependList)2 AionTxReceipt (org.aion.zero.types.AionTxReceipt)2