Search in sources :

Example 16 with ByteArrayWrapper

use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.

the class MutableTrieCache method getAccountWrapper.

// This method returns a wrapper with the same content and size expected for a account key
// when the key is from the same size than the original wrapper, it returns the same object
private ByteArrayWrapper getAccountWrapper(ByteArrayWrapper originalWrapper) {
    byte[] key = originalWrapper.getData();
    int size = TrieKeyMapper.domainPrefix().length + TrieKeyMapper.ACCOUNT_KEY_SIZE + TrieKeyMapper.SECURE_KEY_SIZE;
    return key.length == size ? originalWrapper : new ByteArrayWrapper(Arrays.copyOf(key, size));
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper)

Example 17 with ByteArrayWrapper

use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.

the class MutableTrieCache method internalGet.

private <T> Optional<T> internalGet(byte[] key, Function<byte[], T> trieRetriever, Function<byte[], T> cacheTransformer) {
    ByteArrayWrapper wrapper = new ByteArrayWrapper(key);
    ByteArrayWrapper accountWrapper = getAccountWrapper(wrapper);
    Map<ByteArrayWrapper, byte[]> accountItems = cache.get(accountWrapper);
    boolean isDeletedAccount = deleteRecursiveLog.contains(accountWrapper);
    if (accountItems == null || !accountItems.containsKey(wrapper)) {
        if (isDeletedAccount) {
            return Optional.empty();
        }
        // uncached account
        return Optional.ofNullable(trieRetriever.apply(key));
    }
    byte[] cacheItem = accountItems.get(wrapper);
    if (cacheItem == null) {
        // deleted account key
        return Optional.empty();
    }
    // cached account key
    return Optional.ofNullable(cacheTransformer.apply(cacheItem));
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper)

Example 18 with ByteArrayWrapper

use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.

the class Trie method collectKeys.

// key is the key with exactly collectKeyLen bytes.
// in non-expanded form (binary)
// special value Integer.MAX_VALUE means collect them all.
private void collectKeys(Set<ByteArrayWrapper> set, TrieKeySlice key, int collectKeyLen) {
    if (collectKeyLen != Integer.MAX_VALUE && key.length() > collectKeyLen) {
        return;
    }
    boolean shouldCollect = collectKeyLen == Integer.MAX_VALUE || key.length() == collectKeyLen;
    if (valueLength.compareTo(Uint24.ZERO) > 0 && shouldCollect) {
        // convert bit string into byte[]
        set.add(new ByteArrayWrapper(key.encode()));
    }
    for (byte k = 0; k < ARITY; k++) {
        Trie node = this.retrieveNode(k);
        if (node == null) {
            continue;
        }
        TrieKeySlice nodeKey = key.rebuildSharedPath(k, node.getSharedPath());
        node.collectKeys(set, nodeKey, collectKeyLen);
    }
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper)

Example 19 with ByteArrayWrapper

use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.

the class MutableTrieCacheTest method setToString.

private String setToString(Set<ByteArrayWrapper> set) {
    String r = "";
    ArrayList<String> list = new ArrayList<>();
    for (ByteArrayWrapper item : set) {
        list.add(new String(item.getData(), StandardCharsets.UTF_8));
    }
    Collections.sort(list);
    for (String s : list) {
        r = r + s + ";";
    }
    return r;
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper)

Example 20 with ByteArrayWrapper

use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.

the class MapSnapshotTest method writeSnapshot_WhenWrite_ShouldPopulateOutputStream.

@Test
public void writeSnapshot_WhenWrite_ShouldPopulateOutputStream() throws IOException {
    byte[] key = new byte[] { 0, 1, 2, 3, 4 };
    byte[] value = new byte[] { 5, 6, 7, 8, 9 };
    Map<ByteArrayWrapper, byte[]> outMap = new HashMap<>();
    outMap.put(ByteUtil.wrap(key), value);
    ByteArrayOutputStream expectedOutputStream = new ByteArrayOutputStream();
    DigestOutputStream digestOutput = new DigestOutputStream(expectedOutputStream, HashUtil.makeMessageDigest());
    DataOutputStream dataOutputStream = new DataOutputStream(digestOutput);
    dataOutputStream.writeInt(1);
    dataOutputStream.writeInt(key.length);
    dataOutputStream.write(key);
    dataOutputStream.writeInt(value.length);
    dataOutputStream.write(value);
    byte[] digest = digestOutput.getMessageDigest().digest();
    dataOutputStream.writeInt(digest.length);
    dataOutputStream.write(digest);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    MapSnapshot.Out outSnapshot = new MapSnapshot.Out(outputStream);
    outSnapshot.write(outMap);
    assertArrayEquals(expectedOutputStream.toByteArray(), outputStream.toByteArray());
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper) HashMap(java.util.HashMap) DigestOutputStream(java.security.DigestOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayWrapper (org.ethereum.db.ByteArrayWrapper)23 Test (org.junit.Test)5 Metric (co.rsk.metrics.profilers.Metric)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 DataWord (org.ethereum.vm.DataWord)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 Path (java.nio.file.Path)1 DigestOutputStream (java.security.DigestOutputStream)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MapSnapshot (org.ethereum.util.MapSnapshot)1