Skip to content

Commit

Permalink
fees: add minGasPrice module param
Browse files Browse the repository at this point in the history
  • Loading branch information
loredanacirstea committed May 11, 2022
1 parent 23e56dd commit 41907a8
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 90 deletions.
1 change: 1 addition & 0 deletions docs/protocol/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ Params defines the fees module params
| `developer_shares` | [string](#string) | | developer_shares defines the proportion of the transaction fees to be distributed to the registered contract owner |
| `validator_shares` | [string](#string) | | developer_shares defines the proportion of the transaction fees to be distributed to validators |
| `addr_derivation_cost_create` | [uint64](#uint64) | | parameter to configure the cost of address derivation |
| `min_gas_price` | [string](#string) | | parameter to configure the minimum gas price value for transactions |

<!-- end messages -->

Expand Down
5 changes: 5 additions & 0 deletions proto/evmos/fees/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ message Params {
];
// parameter to configure the cost of address derivation
uint64 addr_derivation_cost_create = 4;
// parameter to configure the minimum gas price value for transactions
string min_gas_price = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
4 changes: 2 additions & 2 deletions x/fees/types/fees.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 75 additions & 26 deletions x/fees/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions x/fees/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ var (
// Cost for executing `crypto.CreateAddress`
// must be at least 36 gas for the contained keccak256(word) operation
DefaultAddrDerivationCostCreate = uint64(50)
DefaultMinGasPrice = sdk.NewDecWithPrec(25, 4) // 0.0025
ParamStoreKeyEnableFees = []byte("EnableFees")
ParamStoreKeyDeveloperShares = []byte("DeveloperShares")
ParamStoreKeyValidatorShares = []byte("ValidatorShares")
ParamStoreKeyAddrDerivationCostCreate = []byte("AddrDerivationCostCreate")
ParamStoreKeyMinGasPrice = []byte("MinGasPrice")
)

// ParamKeyTable returns the parameter key table.
Expand All @@ -32,12 +34,14 @@ func NewParams(
developerShares,
validatorShares sdk.Dec,
addrDerivationCostCreate uint64,
minGasPrice sdk.Dec,
) Params {
return Params{
EnableFees: enableFees,
DeveloperShares: developerShares,
ValidatorShares: validatorShares,
AddrDerivationCostCreate: addrDerivationCostCreate,
MinGasPrice: minGasPrice,
}
}

Expand All @@ -47,6 +51,7 @@ func DefaultParams() Params {
DeveloperShares: DefaultDeveloperShares,
ValidatorShares: DefaultValidatorShares,
AddrDerivationCostCreate: DefaultAddrDerivationCostCreate,
MinGasPrice: DefaultMinGasPrice,
}
}

Expand All @@ -57,6 +62,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(ParamStoreKeyDeveloperShares, &p.DeveloperShares, validateShares),
paramtypes.NewParamSetPair(ParamStoreKeyValidatorShares, &p.ValidatorShares, validateShares),
paramtypes.NewParamSetPair(ParamStoreKeyAddrDerivationCostCreate, &p.AddrDerivationCostCreate, validateUint64),
paramtypes.NewParamSetPair(ParamStoreKeyMinGasPrice, &p.MinGasPrice, validateMinGasPrice),
}
}

Expand Down Expand Up @@ -100,6 +106,24 @@ func validateShares(i interface{}) error {
return nil
}

func validateMinGasPrice(i interface{}) error {
v, ok := i.(sdk.Dec)

if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.IsNil() {
return fmt.Errorf("invalid parameter: nil")
}

if v.IsNegative() {
return fmt.Errorf("value cannot be negative: %T", i)
}

return nil
}

func (p Params) Validate() error {
if err := validateBool(p.EnableFees); err != nil {
return err
Expand All @@ -116,6 +140,9 @@ func (p Params) Validate() error {
if err := validateUint64(p.AddrDerivationCostCreate); err != nil {
return err
}
if err := validateMinGasPrice(p.MinGasPrice); err != nil {
return err
}

return nil
}
82 changes: 74 additions & 8 deletions x/fees/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestParamsValidate(t *testing.T) {
devShares := sdk.NewDecWithPrec(60, 2)
validatorShares := sdk.NewDecWithPrec(40, 2)
derivCostCreate := uint64(50)
minGasPrice := sdk.NewDecWithPrec(20, 4)

testCases := []struct {
name string
Expand All @@ -20,17 +21,17 @@ func TestParamsValidate(t *testing.T) {
{"default", DefaultParams(), false},
{
"valid: enabled",
NewParams(true, devShares, validatorShares, derivCostCreate),
NewParams(true, devShares, validatorShares, derivCostCreate, minGasPrice),
false,
},
{
"valid: disabled",
NewParams(false, devShares, validatorShares, derivCostCreate),
NewParams(false, devShares, validatorShares, derivCostCreate, minGasPrice),
false,
},
{
"valid: 100% devs",
Params{true, sdk.NewDecFromInt(sdk.NewInt(1)), sdk.NewDecFromInt(sdk.NewInt(0)), derivCostCreate},
Params{true, sdk.NewDecFromInt(sdk.NewInt(1)), sdk.NewDecFromInt(sdk.NewInt(0)), derivCostCreate, minGasPrice},
false,
},
{
Expand All @@ -40,17 +41,22 @@ func TestParamsValidate(t *testing.T) {
},
{
"invalid: share > 1",
Params{true, sdk.NewDecFromInt(sdk.NewInt(2)), sdk.NewDecFromInt(sdk.NewInt(0)), derivCostCreate},
Params{true, sdk.NewDecFromInt(sdk.NewInt(2)), sdk.NewDecFromInt(sdk.NewInt(0)), derivCostCreate, minGasPrice},
true,
},
{
"invalid: share < 0",
Params{true, sdk.NewDecFromInt(sdk.NewInt(-1)), sdk.NewDecFromInt(sdk.NewInt(0)), derivCostCreate},
Params{true, sdk.NewDecFromInt(sdk.NewInt(-1)), sdk.NewDecFromInt(sdk.NewInt(0)), derivCostCreate, minGasPrice},
true,
},
{
"invalid: sum shares > 1 ",
Params{true, sdk.NewDecFromInt(sdk.NewInt(1)), sdk.NewDecFromInt(sdk.NewInt(1)), derivCostCreate},
Params{true, sdk.NewDecFromInt(sdk.NewInt(1)), sdk.NewDecFromInt(sdk.NewInt(1)), derivCostCreate, minGasPrice},
true,
},
{
"invalid: min gas price negative",
Params{true, devShares, validatorShares, derivCostCreate, sdk.NewDecFromInt(sdk.NewInt(-1))},
true,
},
}
Expand All @@ -66,8 +72,66 @@ func TestParamsValidate(t *testing.T) {
}
}

func TestParamsValidateMinGasPrice(t *testing.T) {
testCases := []struct {
name string
value interface{}
expError bool
}{
{"default", DefaultMinGasPrice, false},
{"valid", sdk.NewDecFromInt(sdk.NewInt(1)), false},
{"invalid - wrong type - bool", false, true},
{"invalid - wrong type - string", "", true},
{"invalid - wrong type - int64", int64(123), true},
{"invalid - wrong type - sdk.Int", sdk.NewInt(1), true},
{"invalid - is nil", nil, true},
{"invalid - is negative", sdk.NewDecFromInt(sdk.NewInt(-1)), true},
}

for _, tc := range testCases {
err := validateShares(tc.value)

if tc.expError {
require.Error(t, err, tc.name)
} else {
require.NoError(t, err, tc.name)
}
}
}

func TestParamsValidateShares(t *testing.T) {
testCases := []struct {
name string
value interface{}
expError bool
}{
{"default", DefaultDeveloperShares, false},
{"default", DefaultValidatorShares, false},
{"valid", sdk.NewDecFromInt(sdk.NewInt(1)), false},
{"invalid - wrong type - bool", false, true},
{"invalid - wrong type - string", "", true},
{"invalid - wrong type - int64", int64(123), true},
{"invalid - wrong type - sdk.Int", sdk.NewInt(1), true},
{"invalid - is nil", nil, true},
{"invalid - is negative", sdk.NewDecFromInt(sdk.NewInt(-1)), true},
{"invalid - is > 1", sdk.NewDecFromInt(sdk.NewInt(2)), true},
}

for _, tc := range testCases {
err := validateShares(tc.value)

if tc.expError {
require.Error(t, err, tc.name)
} else {
require.NoError(t, err, tc.name)
}
}
}

func TestParamsValidateBool(t *testing.T) {
err := validateBool(true)
err := validateBool(DefaultEnableFees)
require.NoError(t, err)
err = validateBool(true)
require.NoError(t, err)
err = validateBool(false)
require.NoError(t, err)
Expand All @@ -78,7 +142,9 @@ func TestParamsValidateBool(t *testing.T) {
}

func TestParamsValidateUint64(t *testing.T) {
err := validateUint64(uint64(0))
err := validateUint64(DefaultAddrDerivationCostCreate)
require.NoError(t, err)
err = validateUint64(uint64(0))
require.NoError(t, err)
err = validateUint64(uint64(1))
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 41907a8

Please sign in to comment.