コード例 #1
0
ファイル: Hashgraph.cs プロジェクト: erinloy/babble-cs
        public async Task <HashgraphError> HandleNewConsensusEvents(IEnumerable <Event> newConsensusEvents)
        {
            var blockMap   = new Dictionary <int, List <byte[]> >(); // [RoundReceived] => []Transactions
            var blockOrder = new List <int>();                       // [index] => RoundReceived

            foreach (var e in newConsensusEvents)
            {
                Store.AddConsensusEvent(e.Hex());

                ConsensusTransactions += e.Transactions().Length;

                if (e.IsLoaded())
                {
                    PendingLoadedEvents--;
                }

                var rr = e.GetRoundReceived() ?? -1;
                var ok = blockMap.TryGetValue(rr, out var btxs);
                if (!ok)
                {
                    btxs = new List <byte[]>();
                    blockOrder.Add(rr);
                }

                btxs.AddRange(e.Transactions());
                blockMap[rr] = btxs;
            }

            foreach (var rr in blockOrder)
            {
                var blockTxs = blockMap[rr];
                if (blockTxs.Count > 0)
                {
                    var(block, err) = await CreateAndInsertBlock(rr, blockTxs.ToArray());

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

                    if (CommitChannel != null)
                    {
                        await CommitChannel.EnqueueAsync(block);
                    }
                }
            }

            return(null);
        }
コード例 #2
0
ファイル: Hashgraph.cs プロジェクト: jeremylcarter/dotnatter
        public Exception FindOrder()
        {
            DecideRoundReceived();
            var newConsensusEvents = new List <Event>();

            var newUndeterminedEvents = new List <string>();

            foreach (var x in UndeterminedEvents)
            {
                var(ex, err) = Store.GetEvent(x);

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

                if (ex.GetRoundReceived() != null)
                {
                    newConsensusEvents.Add(ex);
                }
                else
                {
                    newUndeterminedEvents.Add(x);
                }
            }
            UndeterminedEvents = newUndeterminedEvents;
            newConsensusEvents.Sort(new Event.EventByConsensus());
            foreach (var e in newConsensusEvents)
            {
                Store.AddConsensusEvent(e.Hex());

                ConsensusTransactions += e.Transactions().Length;

                if (e.IsLoaded())
                {
                    PendingLoadedEvents--;
                }
            }
            if (CommitChannel != null && newConsensusEvents.Count > 0)
            {
                foreach (var nce in newConsensusEvents)
                {
                    CommitChannel.Send(nce);
                }
            }

            return(null);
        }