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