From c17160e03943425feaac7c786a378a905d8ce72d Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 12 Oct 2024 23:22:02 +0330 Subject: [PATCH] feat(config): add consumption fee configs --- config/example_config.toml | 18 +++++++++++++++--- tests/main_test.go | 3 ++- txpool/config.go | 30 +++++++++++++++++++++++++----- txpool/config_test.go | 15 ++++++++++++++- txpool/txpool_test.go | 8 ++++++-- 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/config/example_config.toml b/config/example_config.toml index dbf492274..e50724d07 100644 --- a/config/example_config.toml +++ b/config/example_config.toml @@ -119,9 +119,21 @@ # 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 consumption fee model configs. + # This fee model is based on the amount of data that each address consumes daily. + [tx_pool.fee] + + # `daily_limit` indicates the limit of consumption of each node in bytes per day. + # Default is `280` bytes. + daily_limit = 280 + + # `unit_price` indicates the fee of consumption unit. + # Default is `0.0` PAC. + unit_price = 0.0 + + # `fixed_price` is the constant fee applied to each transaction. + # Default is `0.01` PAC. + fixed_price = 0.01 # `logger` contains configuration options for the logger. [logger] diff --git a/tests/main_test.go b/tests/main_test.go index 947383ecd..5a7278ca3 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,7 @@ 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.DefaultFeeConfig() 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..5f2df327b 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 { + DailyLimit uint32 `toml:"daily_limit"` + UnitPrice float64 `toml:"unit_price"` + FixedPrice float64 `toml:"fixed_price"` } func DefaultConfig() *Config { return &Config{ - MaxSize: 1000, - MinFeePAC: 0.01, + MaxSize: 1000, + Fee: DefaultFeeConfig(), + } +} + +func DefaultFeeConfig() FeeConfig { + return FeeConfig{ + DailyLimit: 280, + UnitPrice: 0, + FixedPrice: 0.01, } } @@ -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.FixedPrice) return amt } diff --git a/txpool/config_test.go b/txpool/config_test.go index a7b112603..4044689f3 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{ + DailyLimit: 280, + UnitPrice: 0, + FixedPrice: 0.01, + } }, }, { diff --git a/txpool/txpool_test.go b/txpool/txpool_test.go index beda5952c..25dabfe0c 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: 100, + Fee: FeeConfig{ + DailyLimit: 280, + UnitPrice: 0.000005, + FixedPrice: 0.01, + }, } }