Example #1
0
        private void ProcessDeltaIndexRange(IEnumerable <DeltaIndex> deltaIndexRange)
        {
            var deltaIndexRangeDao = deltaIndexRange.Select(x =>
                                                            DeltaIndexDao.ToDao <DeltaIndex>(x, _mapperProvider)).ToList();

            var firstDeltaIndex = deltaIndexRangeDao.FirstOrDefault();

            if (firstDeltaIndex == null || firstDeltaIndex.Cid != _previousHash)
            {
                _logger.Error($"Sync Error - Previous delta({_previousHash}) does not match next delta({firstDeltaIndex.Cid})");
                return;
            }

            deltaIndexRangeDao.Remove(firstDeltaIndex);

            DownloadDeltas(deltaIndexRangeDao);
            UpdateState(deltaIndexRangeDao);

            CheckSyncProgressAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Progress(CurrentHighestDeltaIndexStored, _rangeSize);
        }
Example #2
0
        public void Init()
        {
            _cancellationToken = new CancellationToken();

            _manualResetEventSlim = new ManualResetEventSlim(false);

            _testScheduler = new TestScheduler();
            _hashProvider  = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));

            _peerSettings = Substitute.For <IPeerSettings>();
            _peerSettings.PeerId.Returns(PeerIdHelper.GetPeerId());

            _deltaDfsReader = Substitute.For <IDeltaDfsReader>();
            _deltaDfsReader.TryReadDeltaFromDfs(Arg.Any <Cid>(), out Arg.Any <Delta>()).Returns(x => true);

            _deltaCache = Substitute.For <IDeltaCache>();
            _deltaCache.GenesisHash.Returns("bafk2bzacecji5gcdd6lxsoazgnbg46c3vttjwwkptiw27enachziizhhkir2w".ToCid());

            _ledger = Substitute.For <ILedger>();

            _peerService = Substitute.For <IPeerService>();

            _deltaHashProvider = new DeltaHashProvider(_deltaCache, Substitute.For <IDeltaIndexService>(), Substitute.For <ILogger>());

            _deltaIndexService = new DeltaIndexService(new InMemoryRepository <DeltaIndexDao, string>());
            _deltaIndexService.Add(new DeltaIndexDao {
                Cid = _hashProvider.ComputeUtf8MultiHash("0").ToCid(), Height = 0
            });

            _peerClient = Substitute.For <IPeerClient>();
            ModifyPeerClient <LatestDeltaHashRequest>((request, senderPeerIdentifier) =>
            {
                var deltaHeightResponse = new LatestDeltaHashResponse
                {
                    DeltaIndex = new DeltaIndex
                    {
                        Cid = _hashProvider.ComputeUtf8MultiHash(_syncTestHeight.ToString()).ToCid().ToArray()
                              .ToByteString(),
                        Height = (uint)_syncTestHeight
                    }
                };

                _deltaHeightReplaySubject.OnNext(new ObserverDto(Substitute.For <IChannelHandlerContext>(),
                                                                 deltaHeightResponse.ToProtocolMessage(senderPeerIdentifier, CorrelationId.GenerateCorrelationId())));
            });

            ModifyPeerClient <DeltaHistoryRequest>((request, senderPeerIdentifier) =>
            {
                var data = GenerateSampleData((int)request.Height, (int)request.Range, (int)_syncTestHeight);
                _deltaIndexService.Add(data.DeltaIndex.Select(x => DeltaIndexDao.ToDao <DeltaIndex>(x, _mapperProvider)));

                _deltaHistoryReplaySubject.OnNext(new ObserverDto(Substitute.For <IChannelHandlerContext>(),
                                                                  data
                                                                  .ToProtocolMessage(senderPeerIdentifier, CorrelationId.GenerateCorrelationId())));
            });

            _peerRepository = new PeerRepository(new InMemoryRepository <Peer, string>());
            Enumerable.Repeat(new Peer {
                PeerId = PeerIdHelper.GetPeerId()
            }, 5).ToList().ForEach(_peerRepository.Add);

            _deltaHeightReplaySubject  = new ReplaySubject <IObserverDto <ProtocolMessage> >(1);
            _deltaHistoryReplaySubject = new ReplaySubject <IObserverDto <ProtocolMessage> >(1);

            var mergeMessageStreams = _deltaHeightReplaySubject.AsObservable()
                                      .Merge(_deltaHistoryReplaySubject.AsObservable());

            _peerService.MessageStream.Returns(mergeMessageStreams);

            _deltaHashProvider = Substitute.For <IDeltaHashProvider>();
            _deltaHashProvider.TryUpdateLatestHash(Arg.Any <Cid>(), Arg.Any <Cid>()).Returns(true);

            _mapperProvider = new TestMapperProvider();

            _userOutput = Substitute.For <IUserOutput>();

            _deltaHeightWatcher = new DeltaHeightWatcher(_peerClient, _peerRepository, _peerService, minimumPeers: 0);

            var dfsService = Substitute.For <IDfsService>();

            _peerSyncManager = new PeerSyncManager(_peerClient, _peerRepository,
                                                   _peerService, _userOutput, _deltaHeightWatcher, Substitute.For <IDfsService>(), 0.7, 0);
        }