/// <summary>
        /// Initialize coefficients of every type of tokens supporting charging fee.
        /// Currently for acs1, charge primary token, like ELF;
        /// for acs8, charge resource tokens including READ, STO, WRITE, TRAFFIC.
        /// </summary>
        public override Empty InitialCoefficients(Empty input)
        {
            Assert(State.AllCalculateFeeCoefficients.Value == null, "Coefficient already initialized");
            var allCalculateFeeCoefficients = new AllCalculateFeeCoefficients();

            if (allCalculateFeeCoefficients.Value.All(x => x.FeeTokenType != (int)FeeTypeEnum.Read))
            {
                allCalculateFeeCoefficients.Value.Add(GetReadFeeInitialCoefficient());
            }
            if (allCalculateFeeCoefficients.Value.All(x => x.FeeTokenType != (int)FeeTypeEnum.Storage))
            {
                allCalculateFeeCoefficients.Value.Add(GetStorageFeeInitialCoefficient());
            }
            if (allCalculateFeeCoefficients.Value.All(x => x.FeeTokenType != (int)FeeTypeEnum.Write))
            {
                allCalculateFeeCoefficients.Value.Add(GetWriteFeeInitialCoefficient());
            }
            if (allCalculateFeeCoefficients.Value.All(x => x.FeeTokenType != (int)FeeTypeEnum.Traffic))
            {
                allCalculateFeeCoefficients.Value.Add(GetTrafficFeeInitialCoefficient());
            }
            if (allCalculateFeeCoefficients.Value.All(x => x.FeeTokenType != (int)FeeTypeEnum.Tx))
            {
                allCalculateFeeCoefficients.Value.Add(GetTxFeeInitialCoefficient());
            }
            State.AllCalculateFeeCoefficients.Value = allCalculateFeeCoefficients;

            Context.Fire(new CalculateFeeAlgorithmUpdated
            {
                AllTypeFeeCoefficients = allCalculateFeeCoefficients,
            });

            return(new Empty());
        }
Exemple #2
0
        protected override Dictionary <string, CalculateFunction> Deserialize(ByteString byteString)
        {
            var allCalculateFeeCoefficients = new AllCalculateFeeCoefficients();

            allCalculateFeeCoefficients.MergeFrom(byteString);
            Logger.LogDebug($"Deserialize AllCalculateFeeCoefficients: {allCalculateFeeCoefficients}");
            return(allCalculateFeeCoefficients.Value.ToDictionary(
                       c => ((FeeTypeEnum)c.FeeTokenType).ToString().ToUpper(),
                       c => c.ToCalculateFunction()));
        }
Exemple #3
0
        protected override ByteString Serialize(Dictionary <string, CalculateFunction> functionMap)
        {
            var allCalculateFeeCoefficients = new AllCalculateFeeCoefficients();

            foreach (var functionPair in functionMap)
            {
                allCalculateFeeCoefficients.Value.Add(new CalculateFeeCoefficients
                {
                    FeeTokenType          = functionPair.Value.CalculateFeeCoefficients.FeeTokenType,
                    PieceCoefficientsList = { functionPair.Value.CalculateFeeCoefficients.PieceCoefficientsList }
                });
            }

            Logger.LogDebug($"Serialize AllCalculateFeeCoefficients: {allCalculateFeeCoefficients}");
            return(allCalculateFeeCoefficients.ToByteString());
        }
        public Dictionary <string, CalculateFunction> GetCalculateFunctions(IChainContext chainContext)
        {
            var allCalculateFeeCoefficients = new AllCalculateFeeCoefficients();

            foreach (var coefficients in _coefficientsDicCache)
            {
                allCalculateFeeCoefficients.Value.Add(new CalculateFeeCoefficients
                {
                    FeeTokenType          = coefficients.Key,
                    PieceCoefficientsList =
                    {
                        coefficients.Value.Select(v => new CalculateFeePieceCoefficients
                        {
                            Value ={ v             }
                        })
                    }
                });
            }
            return(allCalculateFeeCoefficients.ToCalculateFunctionDictionary());
        }
 internal static Dictionary <string, CalculateFunction> ToCalculateFunctionDictionary(
     this AllCalculateFeeCoefficients allCalculateFeeCoefficients)
 {
     return(allCalculateFeeCoefficients.Value.ToDictionary(c => ((FeeTypeEnum)c.FeeTokenType).ToString().ToUpper(),
                                                           c => c.ToCalculateFunction()));
 }