Beispiel #1
0
        public byte[] RecalculateHash(ulong root)
        {
            var node = GetNodeById(root);

            if (node is null)
            {
                return new byte[] {}
            }
            ;

            switch (node)
            {
            case InternalNode internalNode:
                List <byte[]> childrenHashes = new List <byte[]>();

                foreach (var child in internalNode.Children)
                {
                    childrenHashes.Add(GetNodeById(child).Hash);
                }

                return(childrenHashes
                       .Zip(InternalNode.GetChildrenLabels(internalNode.ChildrenMask), (bytes, i) => new[] { i }.Concat(bytes))
                       .SelectMany(bytes => bytes)
                       .KeccakBytes());

            case LeafNode leafNode:
                return(leafNode.KeyHash.Length.ToBytes().Concat(leafNode.KeyHash).Concat(leafNode.Value).KeccakBytes());
            }
            return(new byte[] {});
        }