private static StateSyncItem PushItem(IPendingSyncItems items, NodeDataType nodeDataType, int level, uint rightness, int progress = 0)
        {
            StateSyncItem stateSyncItem1 = new (Keccak.Zero, null, null, nodeDataType, level, rightness);

            items.PushToSelectedStream(stateSyncItem1, progress);
            return(stateSyncItem1);
        }
        public void Can_peek_root()
        {
            IPendingSyncItems items         = Init();
            StateSyncItem     stateSyncItem = new (Keccak.Zero, null, null, NodeDataType.State);

            items.PushToSelectedStream(stateSyncItem, 0);
            items.PeekState().Should().Be(stateSyncItem);
        }
        public void Can_recalculate_and_clear_with_root_only()
        {
            IPendingSyncItems items         = Init();
            StateSyncItem     stateSyncItem = new (Keccak.Zero, null, null, NodeDataType.State);

            items.PushToSelectedStream(stateSyncItem, 0);
            items.RecalculatePriorities();
            items.Clear();
            items.Count.Should().Be(0);
        }
Ejemplo n.º 4
0
        private AddNodeResult AddNodeToPending(StateSyncItem syncItem, DependentItem dependentItem, string reason, bool missing = false)
        {
            if (!missing)
            {
                if (syncItem.Level <= 2)
                {
                    _syncProgress.ReportSynced(syncItem.Level, syncItem.ParentBranchChildIndex, syncItem.BranchChildIndex, syncItem.NodeDataType, NodeProgressState.Requested);
                }

                if (_alreadySaved.Get(syncItem.Hash))
                {
                    Interlocked.Increment(ref _data.CheckWasCached);
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Node already in the DB - skipping {syncItem.Hash}");
                    }
                    return(AddNodeResult.AlreadySaved);
                }

                object lockToTake = syncItem.NodeDataType == NodeDataType.Code ? _codeDbLock : _stateDbLock;
                lock (lockToTake)
                {
                    IDb dbToCheck = syncItem.NodeDataType == NodeDataType.Code ? _codeDb : _stateDb;
                    Interlocked.Increment(ref _data.DbChecks);
                    bool keyExists = dbToCheck.KeyExists(syncItem.Hash);
                    if (keyExists)
                    {
                        if (_logger.IsTrace)
                        {
                            _logger.Trace($"Node already in the DB - skipping {syncItem.Hash}");
                        }
                        _alreadySaved.Set(syncItem.Hash);
                        Interlocked.Increment(ref _data.StateWasThere);
                        return(AddNodeResult.AlreadySaved);
                    }

                    Interlocked.Increment(ref _data.StateWasNotThere);
                }

                bool isAlreadyRequested;
                lock (_dependencies)
                {
                    isAlreadyRequested = _dependencies.ContainsKey(syncItem.Hash);
                    if (dependentItem != null)
                    {
                        if (_logger.IsTrace)
                        {
                            _logger.Trace($"Adding dependency {syncItem.Hash} -> {dependentItem.SyncItem.Hash}");
                        }
                        AddDependency(syncItem.Hash, dependentItem);
                    }
                }

                /* same items can have same hashes and we only need them once
                 * there is an issue when we have an item, we add it to dependencies, then we request it and the request times out
                 * and we never request it again because it was already on the dependencies list */
                if (isAlreadyRequested)
                {
                    Interlocked.Increment(ref _data.CheckWasInDependencies);
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Node already requested - skipping {syncItem.Hash}");
                    }
                    return(AddNodeResult.AlreadyRequested);
                }
            }

            _pendingItems.PushToSelectedStream(syncItem, _syncProgress.LastProgress);
            if (_logger.IsTrace)
            {
                _logger.Trace($"Added a node {syncItem.Hash} - {reason}");
            }
            return(AddNodeResult.Added);
        }