Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
aljo242 committed Nov 10, 2023
1 parent 56afdb3 commit e2c7008
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
44 changes: 38 additions & 6 deletions x/feemarket/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"fmt"

"github.com/skip-mev/feemarket/x/feemarket/interfaces"

"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -15,6 +17,9 @@ type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey

// plugin is the fee market implementation to be used.
plugin interfaces.FeeMarketImplementation

// The address that is capable of executing a MsgParams message.
// Typically, this will be the governance module's address.
authority string
Expand All @@ -24,27 +29,54 @@ type Keeper struct {
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
plugin interfaces.FeeMarketImplementation,
authority string,
) Keeper {
return Keeper{
) *Keeper {
return &Keeper{
cdc,
storeKey,
plugin,
authority,
}
}

// Logger returns a auction module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
func (k *Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}

// GetAuthority returns the address that is capable of executing a MsgUpdateParams message.
func (k Keeper) GetAuthority() string {
func (k *Keeper) GetAuthority() string {
return k.authority
}

// Plugin returns the plugged fee market implementation of the keeper.
func (k *Keeper) Plugin() interfaces.FeeMarketImplementation {
return k.plugin
}

// SetData sets arbitrary byte data in the keeper.
func (k *Keeper) SetData(ctx sdk.Context, data []byte) {
// TODO: limit max data size?

store := ctx.KVStore(k.storeKey)
store.Set(types.KeyData, data)
}

// GetData gets arbitrary byte data in the keeper.
func (k *Keeper) GetData(ctx sdk.Context) ([]byte, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyData)

if len(bz) == 0 {
return nil, fmt.Errorf("no data set in the keeper")
}

return bz, nil
}

// GetParams returns the feemarket module's parameters.
func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) {
func (k *Keeper) GetParams(ctx sdk.Context) (types.Params, error) {
store := ctx.KVStore(k.storeKey)

key := types.KeyParams
Expand All @@ -63,7 +95,7 @@ func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) {
}

// SetParams sets the feemarket module's parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
func (k *Keeper) SetParams(ctx sdk.Context, params types.Params) error {
store := ctx.KVStore(k.storeKey)

bz, err := params.Marshal()
Expand Down
24 changes: 23 additions & 1 deletion x/feemarket/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper_test
import (
"testing"

"github.com/skip-mev/feemarket/x/feemarket/plugins/defaultmarket"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,7 +19,7 @@ import (
type KeeperTestSuite struct {
suite.Suite

feemarketKeeper keeper.Keeper
feemarketKeeper *keeper.Keeper
encCfg testutils.EncodingConfig
ctx sdk.Context
key *storetypes.KVStoreKey
Expand All @@ -34,13 +36,33 @@ func (s *KeeperTestSuite) SetupTest() {
testCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test"))
s.ctx = testCtx.Ctx

plugin := defaultmarket.NewDefaultFeeMarket()

s.authorityAccount = []byte("authority")
s.feemarketKeeper = keeper.NewKeeper(
s.encCfg.Codec,
s.key,
plugin,
s.authorityAccount.String(),
)

err := s.feemarketKeeper.SetParams(s.ctx, types.DefaultParams())
s.Require().NoError(err)
}

func (s *KeeperTestSuite) TestData() {
s.Run("get with no data returns error", func() {
_, err := s.feemarketKeeper.GetData(s.ctx)
s.Require().Error(err)
})

s.Run("set and get valid data", func() {
data := []byte("testdata")

s.feemarketKeeper.SetData(s.ctx, data)

gotData, err := s.feemarketKeeper.GetData(s.ctx)
s.Require().NoError(err)
s.Require().Equal(data, gotData)
})
}
10 changes: 8 additions & 2 deletions x/feemarket/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ const (

const (
prefixParams = iota + 1
prefixData
)

// KeyParams is the store key for the feemarket module's parameters.
var KeyParams = []byte{prefixParams}
var (
// KeyParams is the store key for the feemarket module's parameters.
KeyParams = []byte{prefixParams}

// KeyData is the store key for the feemarket module's data.
KeyData = []byte{prefixData}
)

0 comments on commit e2c7008

Please sign in to comment.