Beispiel #1
0
            public void WriteNode(T node)
            {
                if (node.Index != currentIndex)
                {
                    throw new InvalidOperationException();
                }

                pruningCursor.WriteNode(node);
            }
Beispiel #2
0
        public static void PruneNode <T>(IMerkleTreePruningCursor <T> cursor, int index)
            where T : IMerkleTreeNode <T>
        {
            if (!cursor.TryMoveToIndex(index))
            {
                return;
            }

            var node = cursor.ReadNode();

            if (node.Depth != 0)
            {
                return;
            }

            if (!node.Pruned)
            {
                node = node.AsPruned();
                cursor.WriteNode(node);
            }

            bool didWork;

            do
            {
                didWork = false;

                if (node.IsLeft())
                {
                    if (cursor.TryMoveRight())
                    {
                        var rightNode = cursor.ReadNode();
                        if (node.Pruned && rightNode.Pruned && node.Depth == rightNode.Depth)
                        {
                            var newNode = node.PairWith(rightNode);

                            cursor.DeleteNode();
                            //TODO cursor.MoveLeft();
                            cursor.WriteNode(newNode);

                            node    = newNode;
                            didWork = true;
                        }
                    }
                    else
                    {
                        if (node.Index != 0 && node.Pruned)
                        {
                            var newNode = node.PairWithSelf();
                            //cursor.MoveLeft();
                            cursor.WriteNode(newNode);

                            node    = newNode;
                            didWork = true;
                        }
                    }
                }
                else
                {
                    if (cursor.TryMoveLeft())
                    {
                        var leftNode = cursor.ReadNode();
                        if (node.Pruned && leftNode.Pruned && node.Depth == leftNode.Depth)
                        {
                            var newNode = leftNode.PairWith(node);
                            cursor.WriteNode(newNode);
                            cursor.MoveRight();
                            cursor.DeleteNode();
                            //TODO cursor.MoveLeft();

                            node    = newNode;
                            didWork = true;
                        }
                    }
                }
            }while (didWork);
        }