-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(x/callback): Implement the storage and validation of params (#498)
* adding params types * adding params test * adding param keeper get and set * fix lint
- Loading branch information
Showing
5 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/archway-network/archway/x/callback/types" | ||
) | ||
|
||
// GetParams return all module parameters. | ||
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { | ||
store := ctx.KVStore(k.storeKey) | ||
bz := store.Get(types.ParamsKey) | ||
if bz == nil { | ||
return params | ||
} | ||
|
||
k.cdc.MustUnmarshal(bz, ¶ms) | ||
return params | ||
} | ||
|
||
// SetParams sets all module parameters. | ||
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { | ||
store := ctx.KVStore(k.storeKey) | ||
bz, err := k.cdc.Marshal(¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
store.Set(types.ParamsKey, bz) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,57 @@ | ||
package types | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
fmt "fmt" | ||
|
||
"cosmossdk.io/math" | ||
) | ||
|
||
var ( | ||
DefaultCallbackGasLimit = uint64(1000000) | ||
DefaultMaxBlockReservationLimit = uint64(3) | ||
DefaultMaxFutureReservationLimit = uint64(10000) | ||
DefaultBlockReservationFeeMultiplier = math.LegacyMustNewDecFromStr("1.0") | ||
DefaultFutureReservationFeeMultiplier = math.LegacyMustNewDecFromStr("1.0") | ||
) | ||
|
||
// NewParams creates a new Params instance. | ||
func NewParams(inflationRewardsRatio, txFeeRebateRatio sdk.Dec, maxwithdrawRecords uint64) Params { | ||
panic("unimplemented 👻") | ||
func NewParams( | ||
callbackGasLimit uint64, | ||
maxBlockReservationLimit uint64, | ||
maxFutureReservationLimit uint64, | ||
blockReservationFeeMultiplier math.LegacyDec, | ||
futureReservationFeeMultiplier math.LegacyDec, | ||
) Params { | ||
return Params{ | ||
CallbackGasLimit: callbackGasLimit, | ||
MaxBlockReservationLimit: maxBlockReservationLimit, | ||
MaxFutureReservationLimit: maxFutureReservationLimit, | ||
BlockReservationFeeMultiplier: &blockReservationFeeMultiplier, | ||
FutureReservationFeeMultiplier: &futureReservationFeeMultiplier, | ||
} | ||
} | ||
|
||
// DefaultParams returns a default set of parameters. | ||
func DefaultParams() Params { | ||
panic("unimplemented 👻") | ||
return NewParams( | ||
DefaultCallbackGasLimit, | ||
DefaultMaxBlockReservationLimit, | ||
DefaultMaxFutureReservationLimit, | ||
DefaultBlockReservationFeeMultiplier, | ||
DefaultFutureReservationFeeMultiplier, | ||
) | ||
} | ||
|
||
// Validate perform object fields validation. | ||
func (m Params) Validate() error { | ||
panic("unimplemented 👻") | ||
func (p Params) Validate() error { | ||
if p.CallbackGasLimit == 0 { | ||
return fmt.Errorf("CallbackGasLimit must be greater than 0") | ||
} | ||
if p.BlockReservationFeeMultiplier.IsNegative() { | ||
return fmt.Errorf("BlockReservationFeeMultiplier must be greater than 0") | ||
} | ||
if p.FutureReservationFeeMultiplier.IsNegative() { | ||
return fmt.Errorf("FutureReservationFeeMultiplier must be greater than 0") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/archway-network/archway/x/callback/types" | ||
) | ||
|
||
func TestParamsValidate(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
params types.Params | ||
errExpected bool | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "OK: Default values", | ||
params: types.DefaultParams(), | ||
errExpected: false, | ||
}, | ||
{ | ||
name: "OK: All valid values", | ||
params: types.NewParams( | ||
100, | ||
100, | ||
100, | ||
math.LegacyMustNewDecFromStr("1.0"), | ||
math.LegacyMustNewDecFromStr("1.0"), | ||
), | ||
errExpected: false, | ||
}, | ||
{ | ||
name: "Fail: CallbackGasLimit: zero", | ||
params: types.NewParams( | ||
0, | ||
100, | ||
100, | ||
math.LegacyMustNewDecFromStr("1.0"), | ||
math.LegacyMustNewDecFromStr("1.0"), | ||
), | ||
errExpected: true, | ||
}, | ||
{ | ||
name: "Fail: BlockReservationFeeMultiplier: negative", | ||
params: types.NewParams( | ||
100, | ||
100, | ||
100, | ||
math.LegacyMustNewDecFromStr("-1.0"), | ||
math.LegacyMustNewDecFromStr("1.0"), | ||
), | ||
errExpected: true, | ||
}, | ||
{ | ||
name: "Fail: FutureReservationFeeMultiplier: negative", | ||
params: types.NewParams( | ||
100, | ||
100, | ||
100, | ||
math.LegacyMustNewDecFromStr("1.0"), | ||
math.LegacyMustNewDecFromStr("-1.0"), | ||
), | ||
errExpected: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := tc.params.Validate() | ||
if tc.errExpected { | ||
assert.Error(t, err) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} |