Пример #1
0
        private async Task <Dictionary <int, ICalculateWay> > GetDefaultPieceWiseFunctionAsync()
        {
            var tempCache = _cacheCacheProvider.GetPieceWiseFunctionFromNormalCache();

            if (tempCache != null)
            {
                return(tempCache);
            }

            var chain = await _blockchainService.GetChainAsync();

            var tokenStub = _tokenStTokenContractReaderFactory.Create(new ChainContext
            {
                BlockHash   = chain.LastIrreversibleBlockHash,
                BlockHeight = chain.LastIrreversibleBlockHeight
            });

            CalculateFeeCoefficientsOfType parameters;

            if (CalculateAlgorithmContext.CalculateFeeTypeEnum == (int)FeeTypeEnum.Tx)
            {
                parameters = await tokenStub.GetCalculateFeeCoefficientOfSender.CallAsync(new Empty());
            }
            else
            {
                parameters = await tokenStub.GetCalculateFeeCoefficientOfContract.CallAsync(new SInt32Value
                                                                                            { Value = CalculateAlgorithmContext.CalculateFeeTypeEnum });
            }
            var calWayDic = new Dictionary <int, ICalculateWay>();

            foreach (var func in parameters.Coefficients)
            {
                ICalculateWay newCalculateWay = null;
                switch (func.FunctionType)
                {
                case CalculateFunctionTypeEnum.Liner:
                    newCalculateWay = new LinerCalculateWay();
                    break;

                case CalculateFunctionTypeEnum.Power:
                    newCalculateWay = new PowerCalculateWay();
                    break;
                }

                if (newCalculateWay == null)
                {
                    Logger.LogWarning($"could not find mapped function type {func.FunctionType}");
                    continue;
                }

                var funDicToLowerKey = func.CoefficientDic.ToDictionary(x => x.Key.ToLower(), x => x.Value);
                calWayDic[func.PieceKey] = newCalculateWay;
                calWayDic[func.PieceKey].InitParameter(funDicToLowerKey);
            }
            _cacheCacheProvider.SetPieceWiseFunctionToNormalCache(calWayDic);
            return(calWayDic);
        }
Пример #2
0
        public void LinerCalculateWay_Test()
        {
            var param = new Dictionary <string, int>
            {
                { "numerator", 1 },
                { "denominator", 2 },
                { "constantvalue", 5000 }
            };
            var liner = new LinerCalculateWay();

            liner.InitParameter(param);

            var parameterDic = liner.GetParameterDic();

            parameterDic["numerator"].ShouldBe(1);
            parameterDic["denominator"].ShouldBe(2);
            parameterDic["constantvalue"].ShouldBe(5000);

            var cost = liner.GetCost(1000);

            cost.ShouldBe(Precision * 1000 / 2 + 5000);
        }