private CalculateFeeCoefficientsOfType GetTxFeeInitialCoefficient()
        {
            var totalParameter  = new CalculateFeeCoefficientsOfType();
            var txFeeParameter1 = new CalculateFeeCoefficient
            {
                FeeType        = FeeTypeEnum.Tx,
                FunctionType   = CalculateFunctionTypeEnum.Liner,
                PieceKey       = 1000000,
                CoefficientDic = { { "numerator", 1 }, { "denominator", 800 }, { "constantValue".ToLower(), 10000 } }
            };
            var txFeeParameter2 = new CalculateFeeCoefficient
            {
                FeeType        = FeeTypeEnum.Tx,
                FunctionType   = CalculateFunctionTypeEnum.Power,
                PieceKey       = int.MaxValue,
                CoefficientDic =
                {
                    { "numerator",            1 }, { "denominator", 800 }, { "power", 2 }, { "changeSpanBase".ToLower(), 100 }, { "weight", 1 },
                    { "weightBase".ToLower(), 1 }
                }
            };

            totalParameter.Coefficients.Add(txFeeParameter1);
            totalParameter.Coefficients.Add(txFeeParameter2);
            return(totalParameter);
        }
Exemple #2
0
        public async Task Update_Coefficient_For_Sender_Should_Success()
        {
            await CreateAndIssueVoteToken();

            const int pieceKey    = 1000000;
            var       updateInput = new CoefficientFromSender
            {
                LinerCoefficient = new LinerCoefficient
                {
                    ConstantValue = 1,
                    Denominator   = 2,
                    Numerator     = 3
                },
                PieceKey = pieceKey,
                IsLiner  = true
            };
            var proposalId = await CreateToRootForUserFeeByTwoLayer(updateInput);

            await ApproveToRootForUserFeeByTwoLayer(proposalId);
            await VoteToReferendum(proposalId);
            await ReleaseToRootForUserFeeByTwoLayer(proposalId);

            var userCoefficientRet = await MainChainTester.ExecuteContractWithMiningAsync(TokenContractAddress,
                                                                                          nameof(TokenContractContainer.TokenContractStub.GetCalculateFeeCoefficientOfSender), new Empty());

            userCoefficientRet.Status.ShouldBe(TransactionResultStatus.Mined);
            var userCoefficient = new CalculateFeeCoefficientsOfType();

            userCoefficient.MergeFrom(userCoefficientRet.ReturnValue);
            var hasModified = userCoefficient.Coefficients.Single(x => x.PieceKey == pieceKey);

            hasModified.CoefficientDic["ConstantValue".ToLower()].ShouldBe(1);
            hasModified.CoefficientDic["Denominator".ToLower()].ShouldBe(2);
            hasModified.CoefficientDic["Numerator".ToLower()].ShouldBe(3);
        }
Exemple #3
0
        private async Task HandleTestAsync(CalculateFeeCoefficientsOfType param, BlockIndex blockIndex)
        {
            var firstData        = param.Coefficients.First();
            var selectedStrategy = firstData.FeeType switch
            {
                FeeTypeEnum.Tx => (ICalculateCostStrategy)Application.ServiceProvider
                .GetRequiredService <ICalculateTxCostStrategy>(),
                FeeTypeEnum.Read => Application.ServiceProvider.GetRequiredService <ICalculateReadCostStrategy>(),
                FeeTypeEnum.Write => Application.ServiceProvider.GetRequiredService <ICalculateWriteCostStrategy>(),
                FeeTypeEnum.Storage => Application.ServiceProvider.GetRequiredService <ICalculateStorageCostStrategy>(),
                FeeTypeEnum.Traffic => Application.ServiceProvider.GetRequiredService <ICalculateTrafficCostStrategy>(),
                _ => null
            };

            if (selectedStrategy == null)
            {
                return;
            }

            var calculateWayList = new List <ICalculateWay>();

            foreach (var coefficient in param.Coefficients)
            {
                var paramDic     = coefficient.CoefficientDic.ToDictionary(x => x.Key.ToLower(), x => x.Value);
                var calculateWay = coefficient.FunctionType switch
                {
                    CalculateFunctionTypeEnum.Liner => (ICalculateWay) new LinerCalculateWay(),
                    CalculateFunctionTypeEnum.Power => new PowerCalculateWay(),
                    _ => null
                };

                if (calculateWay == null)
                {
                    continue;
                }
                calculateWay.PieceKey = coefficient.PieceKey;
                calculateWay.InitParameter(paramDic);
                calculateWayList.Add(calculateWay);
            }

            if (calculateWayList.Any())
            {
                selectedStrategy.AddAlgorithm(blockIndex, calculateWayList);
            }
        }
    }
}
Exemple #4
0
        public async Task Update_Coefficient_For_Contract_Should_Success()
        {
            const int         pieceKey = 1000000;
            const FeeTypeEnum feeType  = FeeTypeEnum.Traffic;
            var updateInput            = new CoefficientFromContract
            {
                FeeType     = feeType,
                Coefficient = new CoefficientFromSender
                {
                    LinerCoefficient = new LinerCoefficient
                    {
                        ConstantValue = 1,
                        Denominator   = 2,
                        Numerator     = 3
                    },
                    PieceKey = pieceKey,
                    IsLiner  = true
                }
            };

            var proposalId = await CreateToRootForDeveloperFeeByTwoLayer(updateInput);

            await ApproveToRootForDeveloperFeeByTwoLayer(proposalId);

            var middleApproveProposalId = await ApproveToRootForDeveloperFeeByMiddleLayer(proposalId);

            await ApproveThenReleaseMiddleProposalForDeveloper(middleApproveProposalId);

            await ReleaseToRootForDeveloperFeeByTwoLayer(proposalId);

            var developerCoefficientRet = await MainChainTester.ExecuteContractWithMiningAsync(TokenContractAddress,
                                                                                               nameof(TokenContractContainer.TokenContractStub.GetCalculateFeeCoefficientOfContract), new SInt32Value
            {
                Value = (int)feeType
            });

            developerCoefficientRet.Status.ShouldBe(TransactionResultStatus.Mined);
            var userCoefficient = new CalculateFeeCoefficientsOfType();

            userCoefficient.MergeFrom(developerCoefficientRet.ReturnValue);
            var hasModified = userCoefficient.Coefficients.Single(x => x.PieceKey == pieceKey);

            hasModified.CoefficientDic["ConstantValue".ToLower()].ShouldBe(1);
            hasModified.CoefficientDic["Denominator".ToLower()].ShouldBe(2);
            hasModified.CoefficientDic["Numerator".ToLower()].ShouldBe(3);
        }