Beispiel #1
0
        public BlockTree(IEnumerable <Block> unverifiedBlocks)
        {
            // build up index of all blocks
            var unverifiedBlockIndex = new BlockIndex();

            foreach (var unverifiedBlock in unverifiedBlocks)
            {
                unverifiedBlockIndex.Add(unverifiedBlock);
            }

            // get the root node
            Root = unverifiedBlockIndex.TryGetChildren(ReadOnlyMemory <Byte> .Empty, out var foundBlocks) ?
                   foundBlocks.Single() :
                   throw new NoRootBlockException();

            // verify the blocks and build a tree
            var queue = new Queue <Block>(unverifiedBlockIndex.Count / 2);

            queue.Enqueue(Root);
            while (queue.Count != 0)
            {
                var current = queue.Dequeue();
                if (!unverifiedBlockIndex.TryGetChildren(current.Signature, out var children))
                {
                    continue;
                }
                foreach (var child in children)
                {
                    if (current.VerifyChild(child))
                    {
                        queue.Enqueue(child);
                    }
                    else
                    {
                        throw new InvalidBlocksException(current, child);
                    }
                }
            }

            blockIndex = unverifiedBlockIndex;             // now verified
        }
Beispiel #2
0
 public BlockTree(Byte[] rootData, Key key)
 {
     Root       = new Block(ReadOnlySpan <Byte> .Empty, rootData, key);
     blockIndex = new BlockIndex();
     blockIndex.Add(Root);
 }