Search in sources :

Example 1 with Value

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

the class Cache method put.

/**
 * Put the node in the cache if RLP encoded value is longer than 32 bytes
 *
 * @param o
 *         the Node which could be a pair-, multi-item Node or single
 *         Value
 * @return keccak hash of RLP encoded node if length > 32 otherwise
 * return node itself
 */
public synchronized Object put(Object o) {
    Value value = new Value(o);
    byte[] enc = value.encode();
    if (enc.length >= 32) {
        byte[] sha = HashUtil.h256(value.encode());
        ByteArrayWrapper key = wrap(sha);
        this.nodes.put(key, new Node(value, true));
        this.removedNodes.remove(key);
        this.isDirty = true;
        return sha;
    }
    return value;
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) Value(org.aion.rlp.Value)

Example 2 with Value

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

the class Cache method get.

public synchronized Value get(byte[] key) {
    ByteArrayWrapper wrappedKey = wrap(key);
    Node node = nodes.get(wrappedKey);
    if (node != null) {
        // cachehits++;
        return node.getValue();
    }
    if (this.dataSource != null) {
        Optional<byte[]> data = (this.dataSource == null) ? Optional.empty() : this.dataSource.get(key);
        if (data.isPresent()) {
            // dbhits++;
            Value val = fromRlpEncoded(data.get());
            nodes.put(wrappedKey, new Node(val, false));
            return val;
        }
    }
    return null;
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) Value(org.aion.rlp.Value)

Example 3 with Value

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

the class TrieImpl method getTrieDump.

public String getTrieDump() {
    synchronized (cache) {
        TraceAllNodes traceAction = new TraceAllNodes();
        Value value = new Value(root);
        if (value.isHashCode()) {
            this.scanTree(this.getRootHash(), traceAction);
        } else {
            traceAction.doOnNode(this.getRootHash(), value);
        }
        final String root;
        if (this.getRoot() instanceof Value) {
            root = "root: " + Hex.toHexString(getRootHash()) + " => " + this.getRoot() + "\n";
        } else {
            root = "root: " + Hex.toHexString(getRootHash()) + "\n";
        }
        return root + traceAction.getOutput();
    }
}
Also used : Value(org.aion.rlp.Value)

Example 4 with Value

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

the class TrieImpl method get.

private Object get(Object node, byte[] key) {
    synchronized (cache) {
        int keypos = 0;
        while (key.length - keypos != 0 && !isEmptyNode(node)) {
            Value currentNode = this.getNode(node);
            if (currentNode == null) {
                return null;
            }
            if (currentNode.length() == PAIR_SIZE) {
                // Decode the key
                byte[] k = unpackToNibbles(currentNode.get(0).asBytes());
                Object v = currentNode.get(1).asObj();
                if (key.length - keypos >= k.length && Arrays.equals(k, copyOfRange(key, keypos, k.length + keypos))) {
                    node = v;
                    keypos += k.length;
                } else {
                    return "";
                }
            } else {
                node = currentNode.get(key[keypos]).asObj();
                keypos++;
            }
        }
        return node;
    }
}
Also used : Value(org.aion.rlp.Value)

Example 5 with Value

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

the class TrieImpl method deserialize.

public void deserialize(byte[] data) {
    synchronized (cache) {
        RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
        RLPItem keysElement = (RLPItem) rlpList.get(0);
        RLPList valsList = (RLPList) rlpList.get(1);
        RLPItem root = (RLPItem) rlpList.get(2);
        for (int i = 0; i < valsList.size(); ++i) {
            byte[] val = valsList.get(i).getRLPData();
            byte[] key = new byte[32];
            Value value = Value.fromRlpEncoded(val);
            System.arraycopy(keysElement.getRLPData(), i * 32, key, 0, 32);
            cache.getNodes().put(wrap(key), new Node(value));
        }
        this.deserializeRoot(root.getRLPData());
    }
}
Also used : RLPItem(org.aion.rlp.RLPItem) Value(org.aion.rlp.Value) RLPList(org.aion.rlp.RLPList)

Aggregations

Value (org.aion.rlp.Value)11 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)2 RLPItem (org.aion.rlp.RLPItem)1 RLPList (org.aion.rlp.RLPList)1