コード例 #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 PowCalculateWay_Test()
        {
            var param = new Dictionary <string, int>
            {
                { "power", 1 },
                { "changespanbase", 2 },
                { "weight", 5 },
                { "weightbase", 10 },
                { "numerator", 100 },
                { "denominator", 1 }
            };
            var pow = new PowerCalculateWay();

            pow.InitParameter(param);

            var parameterDic = pow.GetParameterDic();

            parameterDic.ShouldNotBeNull();

            var cost = pow.GetCost(1000);

            cost.ShouldBeGreaterThan(Precision * 1000);
        }