Esempio n. 1
0
        //creates a weighted list and randomly selects and id from it
        public int selectLeader()
        {
            distributions d            = new distributions(1);
            int           theChosenOne = 0;
            bool          leaderFound  = false;

            createWeightedList();
            broadcastStakes();

            //debugging option for easy leader selection
            //theChosenOne = d.getUniformBetween(0, weightedPeer_IDs.Count);
            //Console.WriteLine("##### RAND " + theChosenOne + " " + weightedPeer_IDs.Count);

            int j = 0;

            //for reals algo leader select
            while (j < 100 && !leaderFound)
            {
                //Console.WriteLine(j);
                foreach (KeyValuePair <int, peer_obj> kvp in peersResource)
                {
                    //start broadcasting leader requests
                    if (kvp.Value.current_stake > j)
                    {
                        if (kvp.Value.leaderRequest(peersResource, weightedPeer_IDs))
                        {
                            //leader is chosen
                            theChosenOne = kvp.Key;
                            leaderFound  = true;
                        }
                    }
                }
                j++;
                //Console.WriteLine(j);
            }

            datar.totIT += j;
            epochIT     += j;
            if (!leaderFound)
            {
                theChosenOne = selectLeader();
            }

            //Console.WriteLine("Epoch: " + epoch + " Leader was found after : " + j + " iterations");
            return(theChosenOne);
        }