Beispiel #1
0
        public BlockChain ToBlockChain(long nblock)
        {
            if (this.parent != null)
            {
                BlockChain chain = this.parent.ToBlockChain(this.blocks[0].Number - 1);

                foreach (Block block in this.blocks)
                {
                    if (block.Number > nblock)
                    {
                        break;
                    }
                    else
                    {
                        chain.TryToAdd(block);
                    }
                }

                return(chain);
            }
            else
            {
                BlockChain chain = new BlockChain(this.blocks[0]);

                for (var k = 1; k < this.blocks.Count; k++)
                {
                    Block block = this.blocks[k];

                    if (block.Number > nblock)
                    {
                        break;
                    }

                    chain.TryToAdd(block);
                }

                return(chain);
            }
        }
Beispiel #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);
            }
        }