From 693872b2aa17ced23bcbc50bc23d99a543fba149 Mon Sep 17 00:00:00 2001 From: Muhammad Javad Akbarian <94112796+akbariandev@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:54:47 +0330 Subject: [PATCH] feat(config): add consumption fee configs (#1547) Co-authored-by: mj --- config/example_config.toml | 23 ++++++++++++++++++++--- tests/main_test.go | 7 ++++++- txpool/config.go | 30 +++++++++++++++++++++++++----- txpool/config_test.go | 15 ++++++++++++++- txpool/txpool_test.go | 8 ++++++-- 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/config/example_config.toml b/config/example_config.toml index dbf492274..c9be86134 100644 --- a/config/example_config.toml +++ b/config/example_config.toml @@ -119,9 +119,26 @@ # Default is `1000`. max_size = 1000 - # `min_fee` indicates the minimum fee in PAC for the transaction to enter into the pool. - # Default is `0.01`. - min_fee = 0.01 + # `tx_pool.fee` contains configuration to calulcate the transaction fee. + [tx_pool.fee] + + # The `fixed_fee` is a constant fee applied to each transaction, regardless of its size or type. + # Default is `0.01` PAC. + fixed_fee = 0.01 + + # The `daily_limit` is the number of bytes an account can send each day without paying a fee. + # The `daily_limit` is part of he consumptional fee model. + # To undesrstand how condumptional fee model works, you can refer to + # PIP-31: Consumptional Fee Mode (https://pips.pactus.org/PIPs/pip-31) + # Default is `280` bytes. + daily_limit = 280 + + # The `unit_price` defines the fee per byte in PAC. + # The `unit_price` is part of he consumptional fee model. + # To undesrstand how condumptional fee model works, you can refer to + # PIP-31: Consumptional Fee Mode (https://pips.pactus.org/PIPs/pip-31) + # Default is `0.0` PAC. + unit_price = 0.0 # `logger` contains configuration options for the logger. [logger] diff --git a/tests/main_test.go b/tests/main_test.go index 947383ecd..1f0d11156 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -14,6 +14,7 @@ import ( "github.com/pactus-project/pactus/genesis" "github.com/pactus-project/pactus/node" "github.com/pactus-project/pactus/store" + "github.com/pactus-project/pactus/txpool" "github.com/pactus-project/pactus/types/account" "github.com/pactus-project/pactus/types/amount" "github.com/pactus-project/pactus/types/validator" @@ -69,7 +70,11 @@ func TestMain(m *testing.M) { tValKeys[i][2] = bls.NewValidatorKey(key2) tConfigs[i] = config.DefaultConfigMainnet() - tConfigs[i].TxPool.MinFeePAC = 0.000001 + tConfigs[i].TxPool.Fee = &txpool.FeeConfig{ + FixedFee: 0.000001, + DailyLimit: 280, + UnitPrice: 0, + } tConfigs[i].Store.Path = util.TempDirPath() tConfigs[i].Consensus.ChangeProposerTimeout = 2 * time.Second tConfigs[i].Consensus.ChangeProposerDelta = 2 * time.Second diff --git a/txpool/config.go b/txpool/config.go index 87e36cae0..02b86242c 100644 --- a/txpool/config.go +++ b/txpool/config.go @@ -5,14 +5,28 @@ import ( ) type Config struct { - MaxSize int `toml:"max_size"` - MinFeePAC float64 `toml:"min_fee"` + MaxSize int `toml:"max_size"` + Fee *FeeConfig `toml:"fee"` +} + +type FeeConfig struct { + FixedFee float64 `toml:"fixed_fee"` + DailyLimit uint32 `toml:"daily_limit"` + UnitPrice float64 `toml:"unit_price"` } func DefaultConfig() *Config { return &Config{ - MaxSize: 1000, - MinFeePAC: 0.01, + MaxSize: 1000, + Fee: DefaultFeeConfig(), + } +} + +func DefaultFeeConfig() *FeeConfig { + return &FeeConfig{ + FixedFee: 0.01, + DailyLimit: 280, + UnitPrice: 0, } } @@ -24,11 +38,17 @@ func (conf *Config) BasicCheck() error { } } + if conf.Fee.DailyLimit == 0 { + return ConfigError{ + Reason: "dailyLimit can't be zero", + } + } + return nil } func (conf *Config) minFee() amount.Amount { - amt, _ := amount.NewAmount(conf.MinFeePAC) + amt, _ := amount.NewAmount(conf.Fee.FixedFee) return amt } diff --git a/txpool/config_test.go b/txpool/config_test.go index a7b112603..a3a1414b9 100644 --- a/txpool/config_test.go +++ b/txpool/config_test.go @@ -50,11 +50,24 @@ func TestConfigBasicCheck(t *testing.T) { c.MaxSize = 9 }, }, + { + name: "Invalid DailyLimit", + expectedErr: ConfigError{ + Reason: "dailyLimit can't be zero", + }, + updateFn: func(c *Config) { + c.Fee.DailyLimit = 0 + }, + }, { name: "Valid Config", updateFn: func(c *Config) { c.MaxSize = 100 - c.MinFeePAC = 1.0 + c.Fee = &FeeConfig{ + FixedFee: 0.01, + DailyLimit: 280, + UnitPrice: 0, + } }, }, { diff --git a/txpool/txpool_test.go b/txpool/txpool_test.go index beda5952c..f1e0570bb 100644 --- a/txpool/txpool_test.go +++ b/txpool/txpool_test.go @@ -27,8 +27,12 @@ type testData struct { func testConfig() *Config { return &Config{ - MaxSize: 100, - MinFeePAC: 0.000001, + MaxSize: 10, + Fee: &FeeConfig{ + FixedFee: 0.000001, + DailyLimit: 280, + UnitPrice: 0.0, + }, } }