Skip to content

Commit

Permalink
feat(config): add consumption fee configs (#1547)
Browse files Browse the repository at this point in the history
Co-authored-by: mj <[email protected]>
  • Loading branch information
akbariandev and mj authored Oct 14, 2024
1 parent 2bcb8d6 commit 693872b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
23 changes: 20 additions & 3 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 6 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,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
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 {
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,
}
}

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.FixedFee)

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{
FixedFee: 0.01,
DailyLimit: 280,
UnitPrice: 0,
}
},
},
{
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: 10,
Fee: &FeeConfig{
FixedFee: 0.000001,
DailyLimit: 280,
UnitPrice: 0.0,
},
}
}

Expand Down

0 comments on commit 693872b

Please sign in to comment.