Ejemplo n.º 1
0
        CampaigningProductionSubscription_Should_Trigger_TryGetFavouriteDelta_On_Campaigning_Producing_Phase()
        {
            var favourite = DeltaHelper.GetFavouriteDelta(_hashProvider);

            _deltaVoter.TryGetFavouriteDelta(Arg.Any <Cid>(), out Arg.Any <FavouriteDeltaBroadcast>())
            .Returns(ci =>
            {
                ci[1] = favourite;
                return(true);
            });

            _cycleEventProvider.MovePastNextPhase(PhaseName.Campaigning);

            _deltaVoter.Received(1).TryGetFavouriteDelta(
                Arg.Is <Cid>(b => b == PreviousDeltaBytes),
                out Arg.Any <FavouriteDeltaBroadcast>());
            _deltaHub.Received(1).BroadcastFavouriteCandidateDelta(Arg.Is(favourite));
        }
        public void StartProducing()
        {
            _constructionProducingSubscription = _cycleEventsProvider.PhaseChanges
                                                 .Where(p => p.Name == PhaseName.Construction && p.Status == PhaseStatus.Producing)
                                                 .Select(p => _deltaBuilder.BuildCandidateDelta(p.PreviousDeltaDfsHash))
                                                 .Subscribe(c =>
            {
                _deltaVoter.OnNext(c);
                _deltaHub.BroadcastCandidate(c);
            });

            _campaigningProductionSubscription = _cycleEventsProvider.PhaseChanges
                                                 .Where(p => p.Name == PhaseName.Campaigning && p.Status == PhaseStatus.Producing)
                                                 .Select(p =>
            {
                _deltaVoter.TryGetFavouriteDelta(p.PreviousDeltaDfsHash, out var favourite);
                return(favourite);
            })
                                                 .Where(f => f != null)
                                                 .Subscribe(f =>
            {
                _deltaHub.BroadcastFavouriteCandidateDelta(f);
                _deltaElector.OnNext(f);
            });

            _votingProductionSubscription = _cycleEventsProvider.PhaseChanges
                                            .Where(p => p.Name == PhaseName.Voting && p.Status == PhaseStatus.Producing)
                                            .Select(p => _deltaElector.GetMostPopularCandidateDelta(p.PreviousDeltaDfsHash))
                                            .Where(c => c != null)
                                            .Select(c =>
            {
                _deltaCache.TryGetLocalDelta(c, out var delta);
                return(delta);
            })
                                            .Where(d => d != null)
                                            .Subscribe(d =>
            {
                _logger.Information("New Delta following {deltaHash} published",
                                    d.PreviousDeltaDfsHash);

                var newCid = _deltaHub.PublishDeltaToDfsAndBroadcastAddressAsync(d)
                             .ConfigureAwait(false).GetAwaiter().GetResult();
                var previousHash = _hashProvider.Cast(d.PreviousDeltaDfsHash.ToByteArray());

                _deltaHashProvider.TryUpdateLatestHash(previousHash, newCid.Hash);
            });
        }
Ejemplo n.º 3
0
        public void StartProducing()
        {
            Task.Delay(_cycleEventsProvider.GetTimeSpanUntilNextCycleStart()).Wait();

            _constructionProducingSubscription = _cycleEventsProvider.PhaseChanges
               .Where(p => p.Name.Equals(PhaseName.Construction) && p.Status.Equals(PhaseStatus.Producing))
               .Select(p => _deltaBuilder.BuildCandidateDelta(p.PreviousDeltaDfsHash))
               .Where(c => c != null)
               .Subscribe(c =>
                {
                    _deltaVoter.OnNext(c);
                    _deltaHub.BroadcastCandidate(c);
                });

            _campaigningProductionSubscription = _cycleEventsProvider.PhaseChanges
               .Where(p => p.Name.Equals(PhaseName.Campaigning) && p.Status.Equals(PhaseStatus.Producing))
               .Select(p =>
                {
                    _deltaVoter.TryGetFavouriteDelta(p.PreviousDeltaDfsHash, out var favourite);
                    return favourite;
                })
               .Where(f => f != null)
               .Subscribe(f =>
                {
                    _deltaHub.BroadcastFavouriteCandidateDelta(f);
                    _deltaElector.OnNext(f);
                });

            _votingProductionSubscription = _cycleEventsProvider.PhaseChanges
               .Where(p => p.Name.Equals(PhaseName.Voting) && p.Status.Equals(PhaseStatus.Producing))
               .Select(p => _deltaElector.GetMostPopularCandidateDelta(p.PreviousDeltaDfsHash))
               .Where(c => c != null)
               .Select(c =>
                {
                    _deltaCache.TryGetLocalDelta(c, out var delta);
                    return delta;
                })
               .Where(d => d != null)
               .Subscribe(async d =>
                {
                    // here were some importnt changes for Web3 so need to have a look if I can delete the comments
                    // <<<<<<< HEAD
                    //                     var newCid = _deltaHub.PublishDeltaToDfsAndBroadcastAddressAsync(d)
                    //                        .ConfigureAwait(false).GetAwaiter().GetResult();
                    //                     _deltaCache.AddLocalDelta(newCid, d);
                    //                     
                    //                     var previousHash = d.PreviousDeltaDfsHash.ToByteArray().ToCid();
                    //                     
                    //                     _logger.Information("New Delta following {deltaHash} published with new cid {newCid}", 
                    //                         d.PreviousDeltaDfsHash, newCid);
                    //
                    //                     _deltaHashProvider.TryUpdateLatestHash(previousHash, newCid);
                    // =======

                    _logger.Information("New Delta following {deltaHash} published",
                        d.PreviousDeltaDfsHash);

                    var newHashCid = _deltaHub.PublishDeltaToDfsAndBroadcastAddressAsync(d)
                       .ConfigureAwait(false).GetAwaiter().GetResult();
                    var previousHashCid = d.PreviousDeltaDfsHash.ToByteArray().ToCid();

                    _deltaHashProvider.TryUpdateLatestHash(previousHashCid, newHashCid);
                });
        }