Skip to content

Commit

Permalink
feat(x/callback): Implement the storage and validation of params (#498)
Browse files Browse the repository at this point in the history
* adding params types

* adding params test

* adding param keeper get and set

* fix lint
  • Loading branch information
spoo-bar authored Nov 6, 2023
1 parent e5ae961 commit 4a57254
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 12 deletions.
12 changes: 6 additions & 6 deletions x/callback/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package keeper
import (
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramTypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/archway-network/archway/x/callback/types"
)

// Keeper provides module state operations.
type Keeper struct {
cdc codec.Codec
paramStore paramTypes.Subspace
cdc codec.Codec
storeKey storetypes.StoreKey
}

// NewKeeper creates a new Keeper instance.
func NewKeeper(cdc codec.Codec, ps paramTypes.Subspace) Keeper {
func NewKeeper(cdc codec.Codec, storeKey storetypes.StoreKey) Keeper {
return Keeper{
cdc: cdc,
paramStore: ps,
cdc: cdc,
storeKey: storeKey,
}
}

Expand Down
29 changes: 29 additions & 0 deletions x/callback/keeper/params.go
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, &params)
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(&params)
if err != nil {
return err
}
store.Set(types.ParamsKey, bz)
return nil
}
4 changes: 4 additions & 0 deletions x/callback/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ const (
// QuerierRoute is the querier route for the module.
QuerierRoute = ModuleName
)

var (
ParamsKey = []byte{0x01}
)
49 changes: 43 additions & 6 deletions x/callback/types/params.go
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
}
81 changes: 81 additions & 0 deletions x/callback/types/params_test.go
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)
})
}
}

0 comments on commit 4a57254

Please sign in to comment.