public IDictionary <ulong, IHashTrieNode> DownloadTrie(string trieName) { Logger.LogWarning($"downloading {trieName} trie"); IDictionary <ulong, IHashTrieNode> trie = new Dictionary <ulong, IHashTrieNode>(); Queue <ulong> queue = new Queue <ulong>(); ulong root = DownloadRoot(trieName); if (root > 0) { trie[root] = DownloadNode(root); if (trie[root].Type == NodeType.Internal) { queue.Enqueue(root); } while (queue.Count > 0) { ulong cur = queue.Dequeue(); IDictionary <ulong, IHashTrieNode> childs = DownloadChild(cur); foreach (var item in childs) { ulong version = item.Key; IHashTrieNode child = item.Value; trie[version] = child; if (child.Type == NodeType.Internal) { queue.Enqueue(version); } } } } Logger.LogWarning($"{trieName} download done"); return(trie); }
public static JObject Web3NodeWithChildrenHash(IHashTrieNode node, List <byte[]> childrenHash) { if (node is null) { return new JObject { } } ; switch (node) { case InternalNode internalNode: var childrenHashJArray = new JArray(); foreach (var childHash in childrenHash) { childrenHashJArray.Add(Web3Data(childHash)); } return(new JObject { ["NodeType"] = Web3Number(1), ["Hash"] = Web3Data(internalNode.Hash.ToUInt256()), ["ChildrenMask"] = Web3Number((ulong)internalNode.ChildrenMask), ["ChildrenHash"] = childrenHashJArray, }); case LeafNode leafNode: return(new JObject { ["NodeType"] = Web3Number(2), ["Hash"] = Web3Data(leafNode.Hash.ToUInt256()), ["KeyHash"] = Web3Data(leafNode.KeyHash.ToUInt256()), ["Value"] = Web3Data(leafNode.Value), }); } return(new JObject { }); }
public void DeleteNode(ulong id, IHashTrieNode node) { // first delete the version by hash and then delete the node. var prefix = EntryPrefix.VersionByHash.BuildPrefix(node.Hash); Delete(prefix, false); prefix = EntryPrefix.PersistentHashMap.BuildPrefix(id); Delete(prefix); }
public void WriteNodeToBatch(ulong id, IHashTrieNode node, RocksDbAtomicWrite tx) { var prefix = EntryPrefix.PersistentHashMap.BuildPrefix(id); tx.Put(prefix, NodeSerializer.ToBytes(node)); var hashPrefix = EntryPrefix.VersionByHash.BuildPrefix(node.Hash); tx.Put(hashPrefix, UInt64Utils.ToBytes(id)); }
public IHashTrieNode?Get(ulong key) { if (cacheMap.TryGetValue(key, out var node)) { IHashTrieNode value = node.Value.Value; _lruList.Remove(node); _lruList.AddLast(node); return(value); } return(null); }
private IHashTrieNode DownloadNode(ulong version) { Logger.LogWarning($"downloding node with version {version}"); JObject?receivedInfo = (JObject?)_CallJsonRPCAPI("la_getNodeByVersion", new JArray { Web3DataFormatUtils.Web3Number(version) }, _peerURL); IHashTrieNode node = Web3DataFormatUtils.NodeFromJson(receivedInfo); Logger.LogWarning($"Completed downloding node with version {version}"); return(node); }
public void Add(ulong key, IHashTrieNode value) { if (cacheMap.TryGetValue(key, out var oldNode)) { _lruList.Remove(oldNode); cacheMap.Remove(key); } if (cacheMap.Count >= Capacity) { RemoveFirst(); } LRUCacheItem cacheItem = new LRUCacheItem(key, value); LinkedListNode <LRUCacheItem> node = new LinkedListNode <LRUCacheItem>(cacheItem); _lruList.AddLast(node); cacheMap.Add(key, node); }
public static byte[] ToBytes(IHashTrieNode node) { switch (node) { case InternalNode internalNode: byte[] nodeByte = new byte[] { 0 } .Concat(internalNode.ChildrenMask.ToBytes()) .Concat(internalNode.Hash) .ToArray(); var _children = internalNode.Children.ToArray(); uint sz = 0; for (int i = 0; i < _children.Length; i++) { ulong childId = _children[i]; sz++; while (childId > 0) { childId >>= 8; sz++; } } byte[] childByte = new byte[sz]; uint idx = 0; for (int i = 0; i < _children.Length; i++) { ulong childId = _children[i]; byte curSize = 0; while (childId > 0) { curSize++; childByte[idx + curSize] = (byte)(childId & 255); childId >>= 8; } childByte[idx] = curSize; idx += (uint)(curSize + 1); } return(nodeByte.Concat(childByte).ToArray()); case LeafNode leafNode: return(new byte[] { 1 }.Concat(leafNode.KeyHash).Concat(leafNode.Value).ToArray()); } throw new InvalidOperationException($"Type {node.GetType()} is not supported"); }
private void DeleteNode(ulong id, IHashTrieNode node, IDbShrinkRepository _repo) { _lruCache.Remove(id); _repo.DeleteNode(id, node); }