Skip to content

Commit

Permalink
keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
aljo242 committed Nov 10, 2023
1 parent e2c7008 commit cb57c3a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
33 changes: 29 additions & 4 deletions x/feemarket/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ func NewKeeper(
plugin interfaces.FeeMarketImplementation,
authority string,
) *Keeper {
return &Keeper{
k := &Keeper{
cdc,
storeKey,
plugin,
authority,
}

return k
}

// Logger returns a auction module-specific logger.
// Logger returns a feemarket module-specific logger.
func (k *Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
Expand All @@ -55,16 +57,39 @@ func (k *Keeper) Plugin() interfaces.FeeMarketImplementation {
return k.plugin
}

// SetFeeMarket sets the fee market implementation data in the keeper
func (k *Keeper) SetFeeMarket(ctx sdk.Context, fm interfaces.FeeMarketImplementation) error {
bz, err := fm.Marshal()
if err != nil {
return fmt.Errorf("unable to marshal fee market implemenation: %w", err)
}

k.setData(ctx, bz)

return nil
}

// GetFeeMarket gets arbitrary byte data in the keeper.
func (k *Keeper) GetFeeMarket(ctx sdk.Context) (interfaces.FeeMarketImplementation, error) {
bz, err := k.getData(ctx)
if err != nil {
return nil, err
}

err = k.plugin.Unmarshal(bz)
return k.plugin, err
}

// SetData sets arbitrary byte data in the keeper.
func (k *Keeper) SetData(ctx sdk.Context, data []byte) {
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) {
func (k *Keeper) getData(ctx sdk.Context) ([]byte, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyData)

Expand Down
13 changes: 6 additions & 7 deletions x/feemarket/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,18 @@ func (s *KeeperTestSuite) SetupTest() {
s.Require().NoError(err)
}

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

s.Run("set and get valid data", func() {
data := []byte("testdata")
plugin := defaultmarket.NewDefaultFeeMarket()
s.feemarketKeeper.SetFeeMarket(s.ctx, plugin)

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

gotData, err := s.feemarketKeeper.GetData(s.ctx)
gotPlugin, err := s.feemarketKeeper.GetFeeMarket(s.ctx)
s.Require().NoError(err)
s.Require().Equal(data, gotData)
s.Require().Equal(plugin, gotPlugin)
})
}

0 comments on commit cb57c3a

Please sign in to comment.