Пример #1
0
        public bool TryToConnect(BlockBranch branch)
        {
            if (this.blocks.Count == 0)
            {
                return(false);
            }

            if (branch.blocks.Count == 0)
            {
                return(false);
            }

            Block pblock = branch.GetBlock(this.blocks[0].Number - 1, this.blocks[0].ParentHash);

            if (pblock == null)
            {
                return(false);
            }

            this.parent = branch;

            return(true);
        }
Пример #2
0
        public void Process(Block block)
        {
            int nprocessed = 0;

            if (this.chain == null)
            {
                this.chain = new BlockChain(block);
                nprocessed++;
            }
            else if (this.chain.TryToAdd(block))
            {
                nprocessed++;
            }

            foreach (var branch in this.branches)
            {
                if (branch.TryToAddFirst(block))
                {
                    nprocessed++;
                }
                else if (branch.TryToAddLast(block))
                {
                    nprocessed++;
                }
            }

            if (nprocessed == 0)
            {
                BlockBranch branch = new BlockBranch();
                branch.TryToAddFirst(block);
                this.branches.Add(branch);
            }

            foreach (var branch in this.branches)
            {
                if (branch.IsConnected())
                {
                    continue;
                }

                if (branch.TryToConnect(this.chain))
                {
                    continue;
                }

                foreach (var branch2 in this.branches)
                {
                    branch.TryToConnect(branch2);
                }
            }

            IList <BlockBranch> toremove = new List <BlockBranch>();

            foreach (var branch in this.branches)
            {
                if (!branch.HasGenesis())
                {
                    continue;
                }

                if (branch.BestBlockNumber > this.chain.BestBlockNumber)
                {
                    this.chain = branch.ToBlockChain(branch.BestBlockNumber);
                    toremove.Add(branch);
                }
            }

            foreach (var branch in toremove)
            {
                this.branches.Remove(branch);
            }
        }