示例#1
0
        public void Start()
        {
            // init block IDs for verbosity (instead of using int or guid)
            foreach (char c in chars)
            {
                blockIDs.Enqueue(c.ToString());
            }

            // spawn miners
            var miners = new Miner[MinerCount];

            for (int i = 0; i < miners.Length; i++)
            {
                // create the same DAG for every miner - only genesis block
                var dag = new DAG
                {
                    Tips = new Block[] { GenesisBlock }
                };

                miners[i] = new Miner {
                    Index = i, DAG = dag
                };

                Console.WriteLine("Spawned miner #{0}", i);
            }

            // Set ConnectedMiners for every miner.
            // Currently every miner knows all the others, but it is easy to plug-in any other
            // network layout here (i.e. every miner is connected to 50% of the miners, etc.)
            for (int i = 0; i < miners.Length; i++)
            {
                var miner = miners[i];

                miner.ConnectedMiners = miners
                                        .Except(new[] { miner })
                                        .ToArray();
            }

            // only genesis block exists at this point
            int currentBlockCount = 1;

            while (currentBlockCount < BlockCount)
            {
                if (lastBlockCreationTime.Add(CreationRate) > DateTime.Now)
                {
                    Thread.Sleep(500);
                    continue;
                }

                int minerIndex = rand.Next(MinerCount);

                var miner = miners[minerIndex];

                CreateNewBlock(miner);

                currentBlockCount++;

                lastBlockCreationTime = DateTime.Now;
            }
        }
示例#2
0
        void AddBlock(DAG dag, Block newBlock)
        {
            lock (dag.SyncObject)
            {
                var oldTips = dag.Tips;

                // create the new Tips list and add 'newBlock'
                var newTips = new List <Block> {
                    newBlock
                };

                // Add every tip that is not a parent of 'newBlock'
                foreach (var block in oldTips)
                {
                    bool isParentOfNewBlock = false;

                    // check if 'block' is a parent of 'newBlock'
                    foreach (var newBlockParent in newBlock.Parents)
                    {
                        if (block.ID == newBlockParent.ID)
                        {
                            isParentOfNewBlock = true;
                            break;
                        }
                    }

                    if (isParentOfNewBlock == false)
                    {
                        // only add blocks that are not parent of 'newBlock'
                        newTips.Add(block);
                    }
                }

                dag.Tips = newTips.ToArray();
            }
        }