org.ethereum.util.Value Java Examples

The following examples show how to use org.ethereum.util.Value. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TrieImpl.java    From ethereumj with MIT License 6 votes vote down vote up
/**
 * Helper method to retrieve the actual node
 * If the node is not a list and length is > 32
 * bytes get the actual node from the db
 *
 * @param node -
 * @return
 */
private Value getNode(Object node) {

    Value val = new Value(node);

    // in that case we got a node
    // so no need to encode it
    if (!val.isBytes()) {
        return val;
    }

    byte[] keyBytes = val.asBytes();
    if (keyBytes.length == 0) {
        return val;
    } else if (keyBytes.length < 32) {
        return new Value(keyBytes);
    }
    return this.cache.get(keyBytes);
}
 
Example #2
Source File: TrieImpl.java    From ethereumj with MIT License 6 votes vote down vote up
/****************************************
 * 			Private functions			*
 ****************************************/

private Object get(Object node, byte[] key) {

    // Return the node if key is empty (= found)
    if (key.length == 0 || isEmptyNode(node)) {
        return 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 >= k.length && Arrays.equals(k, copyOfRange(key, 0, k.length))) {
            return this.get(v, copyOfRange(key, k.length, key.length));
        } else {
            return "";
        }
    } else {
        return this.get(currentNode.get(key[0]).asObj(), copyOfRange(key, 1, key.length));
    }
}
 
Example #3
Source File: TrieImpl.java    From ethereumj with MIT License 5 votes vote down vote up
public String getTrieDump() {

        String root = "";
        TraceAllNodes traceAction = new TraceAllNodes();
        this.scanTree(this.getRootHash(), traceAction);

        if (this.getRoot() instanceof Value) {
            root = "root: " + Hex.toHexString(getRootHash()) +  " => " + this.getRoot() +  "\n";
        } else {
            root = "root: " + Hex.toHexString(getRootHash()) + "\n";
        }
        return root + traceAction.getOutput();
    }
 
Example #4
Source File: TrieImpl.java    From ethereumj with MIT License 5 votes vote down vote up
private Object[] copyNode(Value currentNode) {
    Object[] itemList = emptyStringSlice(LIST_SIZE);
    for (int i = 0; i < LIST_SIZE; i++) {
        Object cpy = currentNode.get(i).asObj();
        if (cpy != null)
            itemList[i] = cpy;
    }
    return itemList;
}
 
Example #5
Source File: Cache.java    From ethereumj with MIT License 5 votes vote down vote up
/**
 * 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 sha3 hash of RLP encoded node if length &gt; 32 otherwise return node itself
 */
public Object put(Object o) {
	Value value = new Value(o);
	byte[] enc = value.encode();
	if (enc.length >= 32) {
		byte[] sha = HashUtil.sha3(enc);
		this.nodes.put(new ByteArrayWrapper(sha), new Node(value, true));
		this.isDirty = true;
		return sha;
	}
	return value;
}
 
Example #6
Source File: TrieIterator.java    From ethereumj with MIT License 5 votes vote down vote up
private List<byte[]> collect() {
	if (this.trie.getRoot() == "") {
		return null;
	}
	this.getNode(new Value(this.trie.getRoot()).asBytes());
	return this.shas;
}
 
Example #7
Source File: TrieIterator.java    From ethereumj with MIT License 5 votes vote down vote up
private void workNode(Value currentNode) {
	if (currentNode.length() == 2) {
		byte[] k = unpackToNibbles(currentNode.get(0).asBytes());

		if (currentNode.get(1).asString() == "") {
			this.workNode(currentNode.get(1));
		} else {
			if (k[k.length-1] == 16) {
				this.values.add(currentNode.get(1).asString());
			} else {
				this.shas.add(currentNode.get(1).asBytes());
				this.getNode(currentNode.get(1).asBytes());
			}
		}
	} else {
		for (int i = 0; i < currentNode.length(); i++) {
			if (i == 16 && currentNode.get(i).length() != 0) {
				this.values.add(currentNode.get(i).asString());
			} else {
				if (currentNode.get(i).asString() == "") {
					this.workNode(currentNode.get(i));
				} else {
					String val = currentNode.get(i).asString();
					if (val != "") {
						this.shas.add(currentNode.get(1).asBytes());
						this.getNode(val.getBytes());
					}
				}
			}
		}
	}
}
 
Example #8
Source File: Cache.java    From ethereumj with MIT License 5 votes vote down vote up
public Value get(byte[] key) {
	ByteArrayWrapper keyObj = new ByteArrayWrapper(key);
	// First check if the key is the cache
	if (this.nodes.get(keyObj) != null) {
		return this.nodes.get(keyObj).getValue();
	}

	// Get the key of the database instead and cache it
	byte[] data = this.db.get(key);
	Value value = Value.fromRlpEncoded(data);
	// Create caching node
	this.nodes.put(keyObj, new Node(value, false));

	return value;
}
 
Example #9
Source File: TrieImpl.java    From ethereumj with MIT License 5 votes vote down vote up
@Override
public byte[] get(byte[] key) {
    if (logger.isDebugEnabled()) 
    	logger.debug("Retrieving key {}", Hex.toHexString(key));
    byte[] k = binToNibbles(key);
    Value c = new Value(this.get(this.root, k));

    return (c == null)? null : c.asBytes();
}
 
Example #10
Source File: TrieImpl.java    From ethereumj with MIT License 5 votes vote down vote up
@Override
public byte[] getRootHash() {
    if (root == null
            || (root instanceof byte[] && ((byte[]) root).length == 0)
            || (root instanceof String && "".equals((String) root))) {
        return ByteUtil.EMPTY_BYTE_ARRAY;
    } else if (root instanceof byte[]) {
        return (byte[]) this.getRoot();
    } else {
        Value rootValue = new Value(this.getRoot());
        byte[] val = rootValue.encode();
        return HashUtil.sha3(val);
    }
}
 
Example #11
Source File: CollectFullSetOfNodes.java    From ethereumj with MIT License 4 votes vote down vote up
@Override
public void doOnNode(byte[] hash, Value node) {
    nodes.add(hash);
}
 
Example #12
Source File: TrieImpl.java    From ethereumj with MIT License 4 votes vote down vote up
private boolean isEmptyNode(Object node) {
    Value n = new Value(node);
    return (node == null || (n.isString() && (n.asString() == "" || n.get(0).isNull())) || n.length() == 0);
}
 
Example #13
Source File: CountAllNodes.java    From ethereumj with MIT License 4 votes vote down vote up
@Override
public void doOnNode(byte[] hash, Value node) {
    ++counted;
}
 
Example #14
Source File: TrieIterator.java    From ethereumj with MIT License 4 votes vote down vote up
private void getNode(byte[] node) {
	Value currentNode = this.trie.getCache().get(node);
	this.workNode(currentNode);
}
 
Example #15
Source File: Node.java    From ethereumj with MIT License 4 votes vote down vote up
public Node(Value val) {
	this(val, false);
}
 
Example #16
Source File: Node.java    From ethereumj with MIT License 4 votes vote down vote up
public Node(Value val, boolean dirty) {
	this.value = val;
	this.dirty = dirty;
}
 
Example #17
Source File: Node.java    From ethereumj with MIT License 4 votes vote down vote up
public Value getValue() {
	return value;
}
 
Example #18
Source File: TraceAllNodes.java    From ethereumj with MIT License 4 votes vote down vote up
@Override
public void doOnNode(byte[] hash, Value node) {

    output.append(Hex.toHexString(hash)).append(" ==> ").append(node.toString()).append("\n");
}
 
Example #19
Source File: Serializers.java    From nuls-v2 with MIT License 4 votes vote down vote up
@Override
public byte[] serialize(Value object) {
    return object.asBytes();
}
 
Example #20
Source File: Node.java    From nuls with MIT License 4 votes vote down vote up
public Value getValue() {
    return value;
}
 
Example #21
Source File: Node.java    From nuls with MIT License 4 votes vote down vote up
public Node(Value val, boolean dirty) {
    this.value = val;
    this.dirty = dirty;
}
 
Example #22
Source File: Node.java    From nuls with MIT License 4 votes vote down vote up
public Node(Value val) {
    this(val, false);
}
 
Example #23
Source File: Serializers.java    From nuls with MIT License 4 votes vote down vote up
@Override
public Value deserialize(byte[] stream) {
    return new Value(stream);
}
 
Example #24
Source File: Serializers.java    From nuls with MIT License 4 votes vote down vote up
@Override
public byte[] serialize(Value object) {
    return object.asBytes();
}
 
Example #25
Source File: Node.java    From nuls-v2 with MIT License 4 votes vote down vote up
public Value getValue() {
    return value;
}
 
Example #26
Source File: Node.java    From nuls-v2 with MIT License 4 votes vote down vote up
public Node(Value val, boolean dirty) {
    this.value = val;
    this.dirty = dirty;
}
 
Example #27
Source File: Node.java    From nuls-v2 with MIT License 4 votes vote down vote up
public Node(Value val) {
    this(val, false);
}
 
Example #28
Source File: Serializers.java    From nuls-v2 with MIT License 4 votes vote down vote up
@Override
public Value deserialize(byte[] stream) {
    return new Value(stream);
}
 
Example #29
Source File: TrieImpl.java    From ethereumj with MIT License votes vote down vote up
public void doOnNode(byte[] hash, Value node);