Skip to content

Commit

Permalink
feat(config): add consumption fee configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mj committed Oct 12, 2024
1 parent 2bcb8d6 commit c17160e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
18 changes: 15 additions & 3 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions txpool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand Down
15 changes: 14 additions & 1 deletion txpool/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
},
},
{
Expand Down
8 changes: 6 additions & 2 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}

Expand Down

0 comments on commit c17160e

Please sign in to comment.