public override void HandleBroadcast(IObserverDto <ProtocolMessage> messageDto)
        {
            try
            {
                Logger.Verbose("received {message} from {port}", messageDto.Payload.CorrelationId.ToCorrelationId(),
                               messageDto.Payload.PeerId.Port);
                var deserialised = messageDto.Payload.FromProtocolMessage <CandidateDeltaBroadcast>();

                if (!_hashProvider.IsValidHash(deserialised.PreviousDeltaDfsHash.ToByteArray()))
                {
                    Logger.Error($"PreviousDeltaDfsHash is not a valid hash");
                    return;
                }

                if (!_hashProvider.IsValidHash(deserialised.Hash.ToByteArray()))
                {
                    Logger.Error($"Hash is not a valid hash");
                    return;
                }

                deserialised.IsValid();

                _deltaVoter.OnNext(deserialised);
            }
            catch (Exception exception)
            {
                Logger.Error(exception, $"Failed to process candidate delta broadcast {messageDto.Payload.ToJsonString()}.");
            }
        }
Ejemplo n.º 2
0
        public override void HandleBroadcast(IObserverDto <ProtocolMessage> messageDto)
        {
            if (!_syncState.IsSynchronized)
            {
                return;
            }

            try
            {
                Logger.Verbose("received {message} from {port}", messageDto.Payload.CorrelationId.ToCorrelationId(),
                               messageDto.Payload.PeerId.Port);

                // @TODO here we use the protobuff message to parse rather than using the CandidateDeltaBroadcastDao
                /////////////////////////////////////////////////////////////////////////////////////////////////
                var deserialized            = messageDto.Payload.FromProtocolMessage <CandidateDeltaBroadcast>();
                var previousDeltaDfsHashCid = deserialized.PreviousDeltaDfsHash.ToByteArray().ToCid();
                /////////////////////////////////////////////////////////////////////////////////////////////////

                if (!_hashProvider.IsValidHash(previousDeltaDfsHashCid.Hash.ToArray()))
                {
                    Logger.Error("PreviousDeltaDfsHash is not a valid hash");
                    return;
                }

                /////////////////////////////////////////////////////////////////////////////////////////////////
                var hashCid = deserialized.Hash.ToByteArray().ToCid();
                /////////////////////////////////////////////////////////////////////////////////////////////////

                if (!_hashProvider.IsValidHash(hashCid.Hash.ToArray()))
                {
                    Logger.Error("Hash is not a valid hash");
                    return;
                }


                var messagePoaNode = _peerRepository.GetPeersByIpAndPublicKey(messageDto.Payload.PeerId.Ip, messageDto.Payload.PeerId.PublicKey).FirstOrDefault();
                if (messagePoaNode == null)
                {
                    Logger.Error($"Message from IP address '{messageDto.Payload.PeerId.Ip}' with public key '{messageDto.Payload.PeerId.PublicKey}' is not found in producer node list.");
                    return;
                }

                if (deserialized.IsValid())
                {
                    _deltaVoter.OnNext(deserialized);
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception,
                             $"Failed to process candidate delta broadcast {messageDto.Payload.ToJsonString()}.");
            }
        }
        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.º 4
0
        public override void HandleBroadcast(IObserverDto <ProtocolMessage> messageDto)
        {
            try
            {
                Logger.Verbose("received {message} from {port}", messageDto.Payload.CorrelationId.ToCorrelationId(),
                               messageDto.Payload.PeerId.Port);

                // @TODO here we use the protobuff message to parse rather than using the CandidateDeltaBroadcastDao
                /////////////////////////////////////////////////////////////////////////////////////////////////
                var deserialized            = messageDto.Payload.FromProtocolMessage <CandidateDeltaBroadcast>();
                var previousDeltaDfsHashCid = deserialized.PreviousDeltaDfsHash.ToByteArray().ToCid();
                /////////////////////////////////////////////////////////////////////////////////////////////////

                if (!_hashProvider.IsValidHash(previousDeltaDfsHashCid.Hash.ToArray()))
                {
                    Logger.Error("PreviousDeltaDfsHash is not a valid hash");
                    return;
                }

                /////////////////////////////////////////////////////////////////////////////////////////////////
                var hashCid = deserialized.Hash.ToByteArray().ToCid();
                /////////////////////////////////////////////////////////////////////////////////////////////////

                if (!_hashProvider.IsValidHash(hashCid.Hash.ToArray()))
                {
                    Logger.Error("Hash is not a valid hash");
                    return;
                }

                if (deserialized.IsValid())
                {
                    _deltaVoter.OnNext(deserialized);
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception,
                             $"Failed to process candidate delta broadcast {messageDto.Payload.ToJsonString()}.");
            }
        }
Ejemplo n.º 5
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);
                });
        }