Ejemplo n.º 1
0
        /// <summary>
        /// Add node to tree. Returns false if node cannot be added because tree would get higher than allowed.
        /// </summary>
        /// <param name="node">Node to be added</param>
        /// <param name="metadata">Metadata node</param>
        /// <param name="blidnginMaskNode">Blinding mask node</param>
        /// <returns>false if node cannot be added because tree would get higher than allowed, true otherwise.</returns>
        public bool AddNode(TreeNode node, IdentityMetadata metadata = null, TreeNode blidnginMaskNode = null)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (metadata != null)
            {
                node = AddMetadata(node, metadata);
            }

            if (blidnginMaskNode != null)
            {
                node = CreateParentNode(blidnginMaskNode, node);
            }

            if (_heads.Count == 0)
            {
                if (node.Level > _maxTreeHeight)
                {
                    return(false);
                }
            }
            else if (CalcNewTreeHeight(node) > _maxTreeHeight)
            {
                return(false);
            }

            AddNodeToForest(node, _heads);

            return(true);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Create aggregation tree node instance.
 /// </summary>
 /// <param name="metadata">Metadata to be added to hash chain</param>
 /// <param name="level">the level value of the aggregation tree node</param>
 public TreeNode(IdentityMetadata metadata, uint level = 0)
 {
     if (metadata == null)
     {
         throw new ArgumentNullException(nameof(metadata));
     }
     Metadata = metadata;
     Level    = level;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Add a data hash to be signed. Returns false if the hash cannot be added because tree would get higher than allowed.
        /// </summary>
        /// <param name="dataHash">Data hash to be added</param>
        /// <param name="metadata">Metadata to be added together with data hash</param>
        /// <param name="level">the level value of the aggregation tree node</param>
        /// <returns>false if data hash cannot be added because tree would get higher than allowed, true otherwise.</returns>
        public bool Add(DataHash dataHash, IdentityMetadata metadata = null, uint level = 0)
        {
            if (!_canAddItems)
            {
                throw new BlockSigningException("Signing process is started. Cannot add new items.");
            }

            if (dataHash == null)
            {
                throw new ArgumentNullException(nameof(dataHash));
            }

            TreeNode node = new TreeNode(dataHash, level);

            if (!_treeBuilder.AddNode(node, metadata, _useBlindingMasks ? GetBlindingMaskNode(node) : null))
            {
                return(false);
            }

            _leafNodes.Add(node);
            return(true);
        }
Ejemplo n.º 4
0
        private TreeNode AddMetadata(TreeNode node, IdentityMetadata metadata)
        {
            DataHash nodeHash = DataHasher
                                .AddData(node.Hash.Imprint)
                                .AddData(metadata.AggregationHashChainMetadata.EncodeValue())
                                .AddData(Util.EncodeUnsignedLong(node.Level + 1))
                                .GetHash();

            TreeNode metadataNode = new TreeNode(metadata, node.Level);

            TreeNode parent = new TreeNode(node.Level + 1)
            {
                Hash  = nodeHash,
                Left  = node,
                Right = metadataNode
            };

            node.IsLeftNode         = true;
            metadataNode.IsLeftNode = false;
            node.Parent             = metadataNode.Parent = parent;
            node = parent;
            return(node);
        }