Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keeper genesis #12

Merged
merged 28 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions proto/feemarket/feemarket/v1/panic.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";
aljo242 marked this conversation as resolved.
Show resolved Hide resolved
package feemarket.feemarket.v1;

option go_package = "github.com/skip-mev/feemarket/x/feemarket/plugins/mocks";

import "cosmos_proto/cosmos.proto";

// PanicMarket is a message that contains the information about a panic
// fee market used ONLY for testing
//
// NOTE: Most operations of this fee market will panic for testing.
message PanicMarket {
option (cosmos_proto.implements_interface) =
"feemarket.feemarket.v1.FeeMarketImplementation";

// Data represents arbitrary data stored in the implementation.
bytes data = 1;
}
43 changes: 43 additions & 0 deletions x/feemarket/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/skip-mev/feemarket/x/feemarket/types"
)

// InitGenesis initializes the feemarket module's state from a given genesis state.
func (k *Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) {
if err := gs.Params.ValidateBasic(); err != nil {
panic(err)
}

// Set the feemarket module's parameters.
if err := k.SetParams(ctx, gs.Params); err != nil {
panic(err)
}

if len(gs.Plugin) == 0 && gs.Params.Enabled {
panic("plugin must be set if feemarket is enabled")
}
aljo242 marked this conversation as resolved.
Show resolved Hide resolved

// set the fee market implementation
if err := k.plugin.Unmarshal(gs.Plugin); err != nil {
panic(err)
}

aljo242 marked this conversation as resolved.
Show resolved Hide resolved
if err := k.Init(ctx); err != nil {
panic(err)
}
}

// ExportGenesis returns a GenesisState for a given context.
func (k *Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
// Get the feemarket module's parameters.
params, err := k.GetParams(ctx)
if err != nil {
panic(err)
}
aljo242 marked this conversation as resolved.
Show resolved Hide resolved

return types.NewGenesisState(k.plugin, params)
}
82 changes: 82 additions & 0 deletions x/feemarket/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package keeper_test

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/skip-mev/feemarket/testutils"
"github.com/skip-mev/feemarket/x/feemarket/keeper"
"github.com/skip-mev/feemarket/x/feemarket/plugins/defaultmarket"
"github.com/skip-mev/feemarket/x/feemarket/plugins/mocks"
"github.com/skip-mev/feemarket/x/feemarket/types"
)

func (s *KeeperTestSuite) TestInitGenesis() {
s.Run("default genesis should not panic", func() {
s.Require().NotPanics(func() {
s.feemarketKeeper.InitGenesis(s.ctx, *types.NewDefaultGenesisState())
})
})

s.Run("valid genesis should not panic", func() {
bz, err := defaultmarket.NewDefaultFeeMarket().Marshal()
s.Require().NoError(err)

gs := types.GenesisState{
Plugin: bz,
Params: types.Params{},
}

s.Require().NotPanics(func() {
s.feemarketKeeper.InitGenesis(s.ctx, gs)
})
})

s.Run("0 bytes plugin bytes should panic", func() {
gs := types.GenesisState{
Plugin: make([]byte, 0),
Params: types.Params{
Enabled: true,
},
}

s.Require().Panics(func() {
s.feemarketKeeper.InitGenesis(s.ctx, gs)
})
})

s.Run("invalid plugin bytes should panic if module enabled", func() {
gs := types.GenesisState{
Plugin: []byte("invalid"),
Params: types.Params{
Enabled: true,
},
}

s.Require().Panics(func() {
s.feemarketKeeper.InitGenesis(s.ctx, gs)
})
})

s.Run("plugin init fail should panic", func() {
encCfg := testutils.CreateTestEncodingConfig()
key := storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("new test"))
ctx := testCtx.Ctx

plugin := mocks.NewPanicMarket()
k := keeper.NewKeeper(encCfg.Codec, key, plugin, s.authorityAccount.String())
bz, err := plugin.Marshal()
s.Require().NoError(err)

gs := types.GenesisState{
Plugin: bz,
Params: types.Params{
Enabled: true,
},
}

s.Require().Panics(func() {
k.InitGenesis(ctx, gs)
})
})
}
88 changes: 88 additions & 0 deletions x/feemarket/plugins/mocks/panic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package mocks

import (
"encoding/json"

sdk "github.com/cosmos/cosmos-sdk/types"

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

var _ interfaces.FeeMarketImplementation = &PanicMarket{}

// NewPanicMarket returns an instance of a new PanicMarket.
func NewPanicMarket() *PanicMarket {
return &PanicMarket{
Data: []byte("panic"),
}
}

// ValidateBasic is a no-op.
func (fm *PanicMarket) ValidateBasic() error {
panic("panic feemarket")
}

// Init which initializes the fee market (in InitGenesis).
func (fm *PanicMarket) Init(_ sdk.Context) error {
panic("panic feemarket")
}

// Export which exports the fee market (in ExportGenesis).
func (fm *PanicMarket) Export(_ sdk.Context) (json.RawMessage, error) {
panic("panic feemarket")
}

// BeginBlockUpdateHandler allows the fee market to be updated
// after every block. This will be added to the BeginBlock chain.
func (fm *PanicMarket) BeginBlockUpdateHandler(_ sdk.Context) interfaces.UpdateHandler {
return func(ctx sdk.Context) error {
panic("panic feemarket")
}
}

// EndBlockUpdateHandler allows the fee market to be updated
// after every block. This will be added to the EndBlock chain.
func (fm *PanicMarket) EndBlockUpdateHandler(_ sdk.Context) interfaces.UpdateHandler {
return func(ctx sdk.Context) error {
panic("panic feemarket")
}
}

// GetFeeMarketInfo retrieves the fee market's information about
// how to pay for a transaction (min gas price, min tip,
// where the fees are being distributed, etc.).
func (fm *PanicMarket) GetFeeMarketInfo(_ sdk.Context) map[string]string {
panic("panic feemarket")
}

// GetID returns the identifier of the fee market.
func (fm *PanicMarket) GetID() string {
return "panic"
}

// FeeAnteHandler will be called in the module AnteHandler.
// Performs no actions.
func (fm *PanicMarket) FeeAnteHandler(
_ sdk.Context,
_ sdk.Tx,
_ bool,
_ sdk.AnteHandler,
) sdk.AnteHandler {
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) {
panic("panic feemarket")
}
}

// FeePostHandler will be called in the module PostHandler
// if PostHandlers are implemented. Performs no actions.
func (fm *PanicMarket) FeePostHandler(
_ sdk.Context,
_ sdk.Tx,
_,
_ bool,
_ sdk.PostHandler,
) sdk.PostHandler {
return func(ctx sdk.Context, tx sdk.Tx, simulate, success bool) (newCtx sdk.Context, err error) {
panic("panic feemarket")
}
}
Loading
Loading