예제 #1
0
        public bool GetIdByHash(string nodeHash, out ulong id)
        {
            id = 0;
            if (nodeHash.Equals(EmptyHash))
            {
                return(true);
            }
            if (_idCache.TryGetValue(nodeHash, out id))
            {
                return(true);
            }
            var idByte = _dbContext.Get(EntryPrefix.VersionByHash.BuildPrefix(HexUtils.HexToBytes(nodeHash)));

            if (!(idByte is null))
            {
                id = UInt64Utils.FromBytes(idByte);
                return(true);
            }
            id = _versionFactory.NewVersion();
            _idCache[nodeHash] = id;
            if (_idCache.Count >= _idCacheCapacity)
            {
                CommitIds();
            }
            return(false);
        }
예제 #2
0
        public JObject GetChildrenByHash(string nodeHash)
        {
            IHashTrieNode?node = _nodeRetrieval.TryGetNode(HexUtils.HexToBytes(nodeHash), out var childrenHash);

            if (node == null)
            {
                return new JObject {
                }
            }
            ;

            JArray children = new JArray {
            };

            foreach (var childHash in childrenHash)
            {
                JObject child = GetNodeByHash(Web3DataFormatUtils.Web3Data(childHash));

                if (child.Count == 0)
                {
                    return new JObject {
                    }
                }
                ;
                children.Add(child);
            }

            JObject nodeHashWithChildren = new JObject {
            };

            nodeHashWithChildren[Web3DataFormatUtils.Web3Data(node.Hash)] = children;
            return(nodeHashWithChildren);
        }
예제 #3
0
        public bool TryAddNode(JObject jsonNode)
        {
            string nodeHash = (string)jsonNode["Hash"];

            if (nodeHash == null)
            {
                return(false);
            }
            byte[]        nodeHashBytes = HexUtils.HexToBytes(nodeHash);
            bool          foundId       = GetIdByHash(nodeHash, out ulong id);
            IHashTrieNode?trieNode;

            if (foundId)
            {
                bool foundNode = TryGetNode(id, out trieNode);
                if (!foundNode)
                {
                    trieNode = BuildHashTrieNode(jsonNode);
                }
            }
            else
            {
                trieNode = BuildHashTrieNode(jsonNode);
            }
            _nodeCache[id] = trieNode;
            if (_nodeCache.Count >= _nodeCacheCapacity)
            {
                CommitNodes();
            }
            return(false);
        }
예제 #4
0
        public IHashTrieNode BuildHashTrieNode(JObject jsonNode)
        {
            IHashTrieNode?trieNode;

            if (((string)jsonNode["NodeType"]).Equals("0x1") == true)
            {
                var           jsonChildrenHash = (JArray)jsonNode["ChildrenHash"];
                List <byte[]> childrenHash     = new List <byte[]>();
                List <ulong>  children         = new List <ulong>();
                foreach (var jsonChildHash in jsonChildrenHash)
                {
                    string childHash = (string)jsonChildHash;
                    childrenHash.Add(HexUtils.HexToBytes(childHash));
                    bool foundId = GetIdByHash(childHash, out ulong childId);
                    children.Add(childId);
                }
                uint mask = Convert.ToUInt32((string)jsonNode["ChildrenMask"], 16);
                trieNode = new InternalNode(mask, children, childrenHash);
            }
            else
            {
                byte[] keyHash = HexUtils.HexToBytes((string)jsonNode["KeyHash"]);
                byte[] value   = HexUtils.HexToBytes((string)jsonNode["Value"]);
                trieNode = new LeafNode(keyHash, value);
            }
            return(trieNode);
        }
예제 #5
0
 public bool IsConsistent(JObject node)
 {
     if (node is null || node.Count == 0)
     {
         return(false);
     }
     if (((string)node["NodeType"]).Equals("0x1"))
     {
         uint          mask             = Convert.ToUInt32((string)node["ChildrenMask"], 16);
         List <byte[]> childrenHashes   = new List <byte[]>();
         var           jsonChildrenHash = (JArray)node["ChildrenHash"];
         foreach (var jsonChildHash in jsonChildrenHash)
         {
             string childHash = (string)jsonChildHash;
             childrenHashes.Add(HexUtils.HexToBytes(childHash));
         }
         byte[] hash = childrenHashes
                       .Zip(InternalNode.GetChildrenLabels(mask), (bytes, i) => new[] { i }.Concat(bytes))
                       .SelectMany(bytes => bytes)
                       .KeccakBytes();
         return(((string)node["Hash"]).Equals(HexUtils.ToHex(hash)));
     }
     else
     {
         byte[] keyHash = HexUtils.HexToBytes(((string)node["KeyHash"]));
         byte[] value   = HexUtils.HexToBytes(((string)node["Value"]));
         byte[] hash    = keyHash.Length.ToBytes().Concat(keyHash).Concat(value).KeccakBytes();
         return(((string)node["Hash"]).Equals(HexUtils.ToHex(hash)));
     }
 }
예제 #6
0
        public void AddBlock(IBlockSnapshot blockSnapshot, string blockRawHex)
        {
            byte[] raw   = HexUtils.HexToBytes(blockRawHex);
            Block  block = Block.Parser.ParseFrom(raw);

            blockSnapshot.AddBlock(block);
            Console.WriteLine("Added BlockHeader: " + block.Header.Index);
        }
예제 #7
0
        public void CommitIds()
        {
            RocksDbAtomicWrite tx = new RocksDbAtomicWrite(_dbContext);

            foreach (var item in _idCache)
            {
                tx.Put(EntryPrefix.VersionByHash.BuildPrefix(HexUtils.HexToBytes(item.Key)), UInt64Utils.ToBytes(item.Value));
            }
            tx.Commit();
            _idCache.Clear();
        }
예제 #8
0
        public JObject GetNodeByHash(string nodeHash)
        {
            IHashTrieNode?node = _nodeRetrieval.TryGetNode(HexUtils.HexToBytes(nodeHash), out var childrenHash);

            if (node == null)
            {
                return new JObject {
                }
            }
            ;
            return(Web3DataFormatUtils.Web3NodeWithChildrenHash(node, childrenHash));
        }
예제 #9
0
 public void Enqueue(string val)
 {
     _queue.Enqueue(val);
     if (_queue.Count >= 2 * BatchSize)
     {
         List <byte> list = new List <byte>();
         for (int i = 0; i < BatchSize; i++)
         {
             //    byte[] array = HexUtils.HexToBytes(_queue.Dequeue());
             //    for(int j=0; j<array.Length; j++) list.Add(array[j]);
             list.AddRange(HexUtils.HexToBytes(_queue.Dequeue()));
         }
         TotalBatch++;
         _dbContext.Save(EntryPrefix.QueueBatch.BuildPrefix((ulong)TotalBatch), list.ToArray());
     }
     Count++;
 }
예제 #10
0
        public static IHashTrieNode NodeFromJson(JObject jsonNode)
        {
            if (((string)jsonNode["NodeType"]).Equals("0x1") == true)
            {
                uint   mask         = Convert.ToUInt32((string)jsonNode["ChildrenMask"], 16);
                byte[] hash         = HexUtils.HexToBytes((string)jsonNode["Hash"]);
                var    jsonChildren = (JArray)jsonNode["Children"];

                List <ulong> children = new List <ulong>();
                foreach (var jsonChild in jsonChildren)
                {
                    children.Add(Convert.ToUInt64((string)jsonChild, 16));
                }
                return(new InternalNode(mask, children, hash));
            }
            else
            {
                byte[] keyHash = HexUtils.HexToBytes((string)jsonNode["KeyHash"]);
                byte[] value   = HexUtils.HexToBytes((string)jsonNode["Value"]);
                return(new LeafNode(keyHash, value));
            }
        }