Esempio n. 1
0
        public void AddBlock(BlockMsg block)
        {
            var newNode = new BlockPoolItem(block);

            //Check with root list
            for (int i = 0; i < this.RootList.Count; i++)
            {
                if (RootList[i].Block.Header.PreviousBlockHash == block.Header.Hash)
                {
                    newNode.AddChild(this.RootList[i]);
                    RootList.RemoveAt(i);
                    break;
                }
            }

            //Search in leaves
            for (int i = 0; i < this.Leaves.Count; i++)
            {
                var leaf = this.Leaves[i];

                if (leaf.Block.Header.Hash == block.Header.PreviousBlockHash)
                {
                    //add new node into leaves, and remove parent from leaves
                    leaf.AddChild(newNode);
                    Leaves.RemoveAt(i);
                    break;
                }
            }

            //Search in parents
            if (newNode.Parent == null)
            {
                for (int i = 0; i < Leaves.Count; i++)
                {
                    var leaf   = Leaves[i];
                    var result = searchNodeByHash(leaf, block.Header.PreviousBlockHash);

                    if (result != null)
                    {
                        result.AddChild(newNode);
                        //Leaves.Add(child);
                        //Leaves.RemoveAt(i);
                        break;
                    }
                }
            }

            if (newNode.Parent == null)
            {
                this.RootList.Add(newNode);
            }

            if (newNode.Children.Count == 0)
            {
                this.Leaves.Add(newNode);
            }

            this.recalculateDeep();

            if (block.Header.Height > this.LatestHeight)
            {
                this.LatestHeight = block.Header.Height;
            }
        }