Beispiel #1
0
        public long TargetChainHeight()
        {
            var lastEnqueuedBlockCacheEntity = BlockCacheEntities.LastOrDefault();

            if (lastEnqueuedBlockCacheEntity != null)
            {
                return(lastEnqueuedBlockCacheEntity.Height + 1);
            }
            var lastDequeuedBlockCacheEntity = DequeuedBlockCacheEntities.LastOrDefault();

            if (lastDequeuedBlockCacheEntity != null)
            {
                return(lastDequeuedBlockCacheEntity.Height + 1);
            }
            return(_initTargetHeight);
        }
        public long TargetChainHeight()
        {
            if (BlockCacheEntities.Count >= CrossChainConstants.ChainCacheEntityCapacity)
            {
                return(-1);
            }
            var lastEnqueuedBlockCacheEntity = BlockCacheEntities.LastOrDefault();

            if (lastEnqueuedBlockCacheEntity != null)
            {
                return(lastEnqueuedBlockCacheEntity.Height + 1);
            }
            var lastDequeuedBlockCacheEntity = DequeuedBlockCacheEntities.LastOrDefault();

            if (lastDequeuedBlockCacheEntity != null)
            {
                return(lastDequeuedBlockCacheEntity.Height + 1);
            }
            return(_targetHeight);
        }
Beispiel #3
0
        /// <summary>
        /// Try take element from cached queue.
        /// Make sure that more than <see cref="CrossChainConstants.MinimalBlockCacheEntityCount"/> block cache entities are left in <see cref="BlockCacheEntities"/>.
        /// </summary>
        /// <param name="height">Height of block info needed</param>
        /// <param name="blockCacheEntity"></param>
        /// <param name="isCacheSizeLimited">Use <see cref="CrossChainConstants.ChainCacheEntityCapacity"/> as cache count threshold if true.</param>
        /// <returns></returns>
        public bool TryTake(long height, out IBlockCacheEntity blockCacheEntity, bool isCacheSizeLimited)
        {
            // clear outdated data
            var cachedInQueue = DequeueBlockCacheEntitiesBeforeHeight(height);
            // isCacheSizeLimited means minimal caching size limit, so that most nodes have this block.
            var lastQueuedHeight = BlockCacheEntities.LastOrDefault()?.Height ?? 0;

            if (cachedInQueue && !(isCacheSizeLimited && lastQueuedHeight < height + CrossChainConstants.MinimalBlockCacheEntityCount))
            {
                return(TryDequeue(out blockCacheEntity));
            }

            blockCacheEntity = GetLastDequeuedBlockCacheEntity(height);
            if (blockCacheEntity != null)
            {
                return(!isCacheSizeLimited ||
                       BlockCacheEntities.Count + DequeuedBlockCacheEntities.Count(ci => ci.Height >= height)
                       >= CrossChainConstants.MinimalBlockCacheEntityCount);
            }

            return(false);
        }