public async Task <long> GetUnitPriceAsync(IChainContext chainContext)
        {
            var keys = _forkCache.Keys.ToArray();

            if (keys.Length == 0)
            {
                return(await GetUnitPriceAsync());
            }
            var  minHeight  = keys.Select(k => k.BlockHeight).Min();
            long?unitPrice  = null;
            var  blockIndex = new BlockIndex
            {
                BlockHash   = chainContext.BlockHash,
                BlockHeight = chainContext.BlockHeight
            };

            do
            {
                if (_forkCache.TryGetValue(blockIndex, out var value))
                {
                    unitPrice = value;
                }

                var link = _chainBlockLinkService.GetCachedChainBlockLink(blockIndex.BlockHash);
                blockIndex.BlockHash = link?.PreviousBlockHash;
                blockIndex.BlockHeight--;
            } while (blockIndex.BlockHash != null && blockIndex.BlockHeight >= minHeight);

            if (unitPrice == null)
            {
                unitPrice = await GetUnitPriceAsync();
            }
            Logger.LogTrace($"Get tx size fee unit price: {unitPrice.Value}");
            return(unitPrice.Value);
        }
        public async Task <List <AvailableTokenInfoInCache> > GetExtraAcceptedTokensInfoAsync(
            IChainContext chainContext)
        {
            var keys = _cacheProvider.GetForkCacheKeys();

            if (keys.Length == 0)
            {
                return(await GetExtraAcceptedTokensInfoFromCacheAsync());
            }
            var blockIndex = new BlockIndex
            {
                BlockHash   = chainContext.BlockHash,
                BlockHeight = chainContext.BlockHeight
            };
            var minHeight = keys.Select(k => k.BlockHeight).Min();
            List <AvailableTokenInfoInCache> tokenInfoDic = null;

            do
            {
                if (_cacheProvider.TryGetExtraAcceptedTokensInfoFromForkCache(blockIndex, out var value))
                {
                    tokenInfoDic = value;
                    break;
                }

                var link = _chainBlockLinkService.GetCachedChainBlockLink(blockIndex.BlockHash);
                blockIndex.BlockHash = link?.PreviousBlockHash;
                blockIndex.BlockHeight--;
            } while (blockIndex.BlockHash != null && blockIndex.BlockHeight >= minHeight);

            return(tokenInfoDic ?? await GetExtraAcceptedTokensInfoFromCacheAsync());
        }
        private SmartContractRegistrationCache GetSmartContractRegistrationCacheFromForkCache(
            IChainContext chainContext, Address address)
        {
            if (!_smartContractRegistrationCacheProvider.TryGetForkCache(address, out var caches))
            {
                return(null);
            }
            var cacheList = caches.ToList();

            if (cacheList.Count == 0)
            {
                return(null);
            }
            var minHeight   = cacheList.Min(s => s.BlockHeight);
            var blockHashes = cacheList.Select(s => s.BlockHash).ToList();
            var blockHash   = chainContext.BlockHash;
            var blockHeight = chainContext.BlockHeight;

            do
            {
                if (blockHashes.Contains(blockHash))
                {
                    return(cacheList.Last(s => s.BlockHash == blockHash));
                }

                var link = _chainBlockLinkService.GetCachedChainBlockLink(blockHash);
                blockHash = link?.PreviousBlockHash;
                blockHeight--;
            } while (blockHash != null && blockHeight >= minHeight);

            return(null);
        }
Esempio n. 4
0
        public async Task <int> GetLimitAsync(IChainContext chainContext)
        {
            var keys = _forkCache.Keys.ToArray();

            if (keys.Length == 0)
            {
                return(await GetLimitAsync());
            }
            var minHeight  = keys.Select(k => k.BlockHeight).Min();
            int?limit      = null;
            var blockIndex = new BlockIndex
            {
                BlockHash   = chainContext.BlockHash,
                BlockHeight = chainContext.BlockHeight
            };

            do
            {
                if (_forkCache.TryGetValue(blockIndex, out var value))
                {
                    limit = value;
                    break;
                }

                var link = _chainBlockLinkService.GetCachedChainBlockLink(blockIndex.BlockHash);
                blockIndex.BlockHash = link?.PreviousBlockHash;
                blockIndex.BlockHeight--;
            } while (blockIndex.BlockHash != null && blockIndex.BlockHeight >= minHeight);

            return(limit ?? await GetLimitAsync());
        }
Esempio n. 5
0
        private async Task <Dictionary <int, ICalculateWay> > GetPieceWiseFuncUnderContextAsync()
        {
            var keys = _cacheCacheProvider.GetForkCacheKeys();

            if (keys.Length == 0)
            {
                return(await GetDefaultPieceWiseFunctionAsync());
            }
            var minHeight = keys.Select(k => k.BlockHeight).Min();
            Dictionary <int, ICalculateWay> algorithm = null;
            var blockIndex = new BlockIndex
            {
                BlockHash   = CalculateAlgorithmContext.BlockIndex.BlockHash,
                BlockHeight = CalculateAlgorithmContext.BlockIndex.BlockHeight
            };

            do
            {
                if (_cacheCacheProvider.TryGetPieceWiseFunctionFromForkCacheByBlockIndex(blockIndex, out var value))
                {
                    algorithm = value;
                    break;
                }

                var link = _chainBlockLinkService.GetCachedChainBlockLink(blockIndex.BlockHash);
                blockIndex.BlockHash = link?.PreviousBlockHash;
                blockIndex.BlockHeight--;
            } while (blockIndex.BlockHash != null && blockIndex.BlockHeight >= minHeight);

            return(algorithm ?? await GetDefaultPieceWiseFunctionAsync());
        }
        public bool CheckContractAddress(IChainContext chainContext, Address address)
        {
            if (!_initialized)
            {
                return(true);
            }
            if (_addressList.Value.Contains(address))
            {
                return(true);
            }
            if (!_forkCache.TryGetValue(address, out var blockIndices))
            {
                return(false);
            }

            var minHeight  = blockIndices.Select(k => k.BlockHeight).Min();
            var blockIndex = new BlockIndex
            {
                BlockHash   = chainContext.BlockHash,
                BlockHeight = chainContext.BlockHeight
            };

            do
            {
                if (blockIndices.Contains(blockIndex))
                {
                    return(true);
                }

                var link = _chainBlockLinkService.GetCachedChainBlockLink(blockIndex.BlockHash);
                blockIndex.BlockHash = link?.PreviousBlockHash;
                blockIndex.BlockHeight--;
            } while (blockIndex.BlockHash != null && blockIndex.BlockHeight >= minHeight);

            return(false);
        }