コード例 #1
0
        public DeltaHashProvider(IDeltaCache deltaCache,
                                 IDeltaIndexService deltaIndexService,
                                 ILogger logger,
                                 int capacity = 10_000)
        {
            _deltaCache = deltaCache;
            _logger     = logger;
            _deltaHashUpdatesSubject = new ReplaySubject <Cid>(0);
            var comparer = ComparerBuilder.For <Timestamp>().OrderBy(u => u, descending: true);

            _capacity = capacity;
            _hashesByTimeDescending = new SortedList <Timestamp, Cid>(comparer)
            {
                Capacity = _capacity
            };

            var latestDeltaIndex = deltaIndexService.LatestDeltaIndex();

            if (deltaIndexService.LatestDeltaIndex() != null)
            {
                var foundDelta = _deltaCache.TryGetOrAddConfirmedDelta(latestDeltaIndex.Cid, out var delta);
                _hashesByTimeDescending.Add(delta.TimeStamp, latestDeltaIndex.Cid);
                return;
            }

            _hashesByTimeDescending.Add(Timestamp.FromDateTime(DateTime.UnixEpoch), _deltaCache.GenesisHash);
        }
コード例 #2
0
ファイル: Synchroniser.cs プロジェクト: jakubbober/Catalyst
        public async Task StartAsync(CancellationToken cancellationToken = default)
        {
            if (State.IsRunning)
            {
                _userOutput.WriteLine("Sync is already running.");
                return;
            }

            State.IsRunning = true;
            _previousHash   = _deltaIndexService.LatestDeltaIndex().Cid;
            _userOutput.WriteLine("Starting Sync...");

            _scoredDeltaIndexRangeDisposable = _peerSyncManager.ScoredDeltaIndexRange.Subscribe(ProcessDeltaIndexRange);

            _deltaHeightWatcher.Start();

            await _peerSyncManager.WaitForPeersAsync(cancellationToken).ConfigureAwait(false);

            var highestDeltaIndex = await _deltaHeightWatcher.GetHighestDeltaIndexAsync();

            if (highestDeltaIndex == null || highestDeltaIndex.Height <= CurrentHighestDeltaIndexStored)
            {
                await Completed().ConfigureAwait(false);

                return;
            }

            State.CurrentBlock = State.StartingBlock = CurrentHighestDeltaIndexStored;
            State.HighestBlock = highestDeltaIndex.Height;

            _peerSyncManager.Start();

            Progress(CurrentHighestDeltaIndexStored, _rangeSize);
        }
コード例 #3
0
        public Ledger(IDeltaExecutor deltaExecutor,
                      IStateProvider stateProvider,
                      IStorageProvider storageProvider,
                      ISnapshotableDb stateDb,
                      IDb codeDb,
                      IAccountRepository accounts,
                      IDeltaIndexService deltaIndexService,
                      ITransactionRepository receipts,
                      IDeltaHashProvider deltaHashProvider,
                      ISynchroniser synchroniser,
                      IMempool <PublicEntryDao> mempool,
                      IMapperProvider mapperProvider,
                      IHashProvider hashProvider,
                      ILogger logger)
        {
            Accounts         = accounts;
            _deltaExecutor   = deltaExecutor;
            _stateProvider   = stateProvider;
            _storageProvider = storageProvider;

            _stateDb        = stateDb;
            _codeDb         = codeDb;
            _mempool        = mempool;
            _mapperProvider = mapperProvider;
            _hashProvider   = hashProvider;
            _logger         = logger;
            _receipts       = receipts;
            _synchroniser   = synchroniser;

            _deltaUpdatesSubscription = deltaHashProvider.DeltaHashUpdates.Subscribe(Update);

            _deltaIndexService = deltaIndexService;

            var latestDeltaIndex = _deltaIndexService.LatestDeltaIndex();

            if (latestDeltaIndex != null)
            {
                _latestKnownDelta       = latestDeltaIndex.Cid;
                _latestKnownDeltaNumber = (long)latestDeltaIndex.Height;
                return;
            }

            _latestKnownDelta = _synchroniser.DeltaCache.GenesisHash;
            WriteLatestKnownDelta(_latestKnownDelta);
        }
コード例 #4
0
        /// <param name="deltaHeightRequest"></param>
        /// <param name="channelHandlerContext"></param>
        /// <param name="senderPeerId"></param>
        /// <param name="correlationId"></param>
        /// <returns></returns>
        protected override LatestDeltaHashResponse HandleRequest(LatestDeltaHashRequest deltaHeightRequest,
                                                                 IChannelHandlerContext channelHandlerContext,
                                                                 PeerId senderPeerId,
                                                                 ICorrelationId correlationId)
        {
            Guard.Argument(deltaHeightRequest, nameof(deltaHeightRequest)).NotNull();
            Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull();
            Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull();

            Logger.Debug("PeerId: {0} wants to know your current chain height", senderPeerId);

            var deltaIndexDao = _deltaIndexService.LatestDeltaIndex();
            var deltaIndex    = DeltaIndexDao.ToProtoBuff <DeltaIndex>(deltaIndexDao, _mapperProvider);

            return(new LatestDeltaHashResponse
            {
                IsSync = _syncState.IsSynchronized,
                DeltaIndex = deltaIndex
            });
        }