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));
}
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));
}
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);
}
}
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;
}
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());
}
Aggregations