Beispiel #1
0
        public async Task <Exception> DivideRounds()
        {
            logger.Debug("Divide Rounds {UndeterminedEvents}", UndeterminedEvents.Count);

            foreach (var hash in UndeterminedEvents)

            {
                var roundNumber = await Round(hash);

                var witness = await Witness(hash);

                var(roundInfo, err) = await Store.GetRound(roundNumber);

                //If the RoundInfo is not found in the Store's Cache, then the Hashgraph
                //is not aware of it yet. We need to add the roundNumber to the queue of
                //undecided rounds so that it will be processed in the other consensus
                //methods
                if (err != null && err.StoreErrorType != StoreErrorType.KeyNotFound)
                {
                    return(err);
                }

                //If the RoundInfo is actually taken from the Store's DB, then it still
                //has not been processed by the Hashgraph consensus methods (The 'queued'
                //field is not exported and therefore not persisted in the DB).
                //RoundInfos taken from the DB directly will always have this field set
                //to false
                if (!roundInfo.Queued())
                {
                    logger.Debug("Undetermined Event Queued {hex}", hash);

                    UndecidedRounds.Enqueue(roundNumber);

                    roundInfo.SetQueued();
                }

                roundInfo.AddEvent(hash, witness);

                err = await Store.SetRound(roundNumber, roundInfo);

                if (err != null)
                {
                    return(err);
                }
            }

            return(null);
        }
Beispiel #2
0
        //assign round received and timestamp to all evs
        public async Task <Exception> DecideRoundReceived()
        {
            foreach (var x in UndeterminedEvents)
            {
                var r = await Round(x);

                for (var i = r + 1; i <= Store.LastRound(); i++)

                {
                    var(tr, err) = await Store.GetRound(i);

                    if (err != null && err.StoreErrorType != StoreErrorType.KeyNotFound)
                    {
                        return(err);
                    }

                    //skip if some witnesses are left undecided
                    if (!(tr.WitnessesDecided() && UndecidedRounds.Peek() > i))
                    {
                        continue;
                    }

                    var fws = tr.FamousWitnesses();

                    //set of famous witnesses that see x
                    var s = new List <string>();
                    foreach (var w in fws)
                    {
                        if (await See(w, x))
                        {
                            s.Add(w);
                        }
                    }

                    if (s.Count > fws.Length / 2)
                    {
                        var(ex, erre) = await Store.GetEvent(x);

                        if (erre != null)
                        {
                            return(erre);
                        }

                        ex.SetRoundReceived(i);

                        var t = new List <string>();
                        foreach (var a in s)
                        {
                            t.Add(await OldestSelfAncestorToSee(a, x));
                        }

                        ex.SetConsensusTimestamp(await MedianTimestamp(t));

                        await Store.SetEvent(ex);

                        break;
                    }
                }
            }

            return(null);
        }