diff --git a/proto/feemarket/feemarket/v1/panic.proto b/proto/feemarket/feemarket/v1/panic.proto new file mode 100644 index 0000000..465eb78 --- /dev/null +++ b/proto/feemarket/feemarket/v1/panic.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +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; +} diff --git a/x/feemarket/keeper/genesis.go b/x/feemarket/keeper/genesis.go new file mode 100644 index 0000000..34da53c --- /dev/null +++ b/x/feemarket/keeper/genesis.go @@ -0,0 +1,41 @@ +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) + } + + // set the fee market implementation + if err := k.plugin.Unmarshal(gs.Plugin); err != nil { + panic(err) + } + if err := k.plugin.ValidateBasic(); err != nil { + panic(err) + } + 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) + } + + return types.NewGenesisState(k.plugin, params) +} diff --git a/x/feemarket/keeper/genesis_test.go b/x/feemarket/keeper/genesis_test.go new file mode 100644 index 0000000..678b9e0 --- /dev/null +++ b/x/feemarket/keeper/genesis_test.go @@ -0,0 +1,70 @@ +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("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) + }) + }) +} diff --git a/x/feemarket/plugins/eip1559/types/params_test.go b/x/feemarket/plugins/eip1559/types/params_test.go index 302d602..3f2498c 100644 --- a/x/feemarket/plugins/eip1559/types/params_test.go +++ b/x/feemarket/plugins/eip1559/types/params_test.go @@ -4,8 +4,9 @@ import ( "testing" "cosmossdk.io/math" - "github.com/skip-mev/feemarket/x/feemarket/plugins/eip1559/types" "github.com/stretchr/testify/require" + + "github.com/skip-mev/feemarket/x/feemarket/plugins/eip1559/types" ) func TestParams(t *testing.T) { diff --git a/x/feemarket/plugins/eip1559/types/state_test.go b/x/feemarket/plugins/eip1559/types/state_test.go index 2b43081..6c3be2c 100644 --- a/x/feemarket/plugins/eip1559/types/state_test.go +++ b/x/feemarket/plugins/eip1559/types/state_test.go @@ -4,8 +4,9 @@ import ( "testing" "cosmossdk.io/math" - "github.com/skip-mev/feemarket/x/feemarket/plugins/eip1559/types" "github.com/stretchr/testify/require" + + "github.com/skip-mev/feemarket/x/feemarket/plugins/eip1559/types" ) func TestState(t *testing.T) { diff --git a/x/feemarket/plugins/mocks/panic.go b/x/feemarket/plugins/mocks/panic.go new file mode 100644 index 0000000..94ac25d --- /dev/null +++ b/x/feemarket/plugins/mocks/panic.go @@ -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") + } +} diff --git a/x/feemarket/plugins/mocks/panic.pb.go b/x/feemarket/plugins/mocks/panic.pb.go new file mode 100644 index 0000000..0a6f11c --- /dev/null +++ b/x/feemarket/plugins/mocks/panic.pb.go @@ -0,0 +1,327 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: feemarket/feemarket/v1/panic.proto + +package mocks + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-proto" + proto "github.com/cosmos/gogoproto/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// 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. +type PanicMarket struct { + // Data represents arbitrary data stored in the implementation. + Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` +} + +func (m *PanicMarket) Reset() { *m = PanicMarket{} } +func (m *PanicMarket) String() string { return proto.CompactTextString(m) } +func (*PanicMarket) ProtoMessage() {} +func (*PanicMarket) Descriptor() ([]byte, []int) { + return fileDescriptor_497e7cde817162d1, []int{0} +} +func (m *PanicMarket) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PanicMarket) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PanicMarket.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PanicMarket) XXX_Merge(src proto.Message) { + xxx_messageInfo_PanicMarket.Merge(m, src) +} +func (m *PanicMarket) XXX_Size() int { + return m.Size() +} +func (m *PanicMarket) XXX_DiscardUnknown() { + xxx_messageInfo_PanicMarket.DiscardUnknown(m) +} + +var xxx_messageInfo_PanicMarket proto.InternalMessageInfo + +func (m *PanicMarket) GetData() []byte { + if m != nil { + return m.Data + } + return nil +} + +func init() { + proto.RegisterType((*PanicMarket)(nil), "feemarket.feemarket.v1.PanicMarket") +} + +func init() { + proto.RegisterFile("feemarket/feemarket/v1/panic.proto", fileDescriptor_497e7cde817162d1) +} + +var fileDescriptor_497e7cde817162d1 = []byte{ + // 204 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x4a, 0x4b, 0x4d, 0xcd, + 0x4d, 0x2c, 0xca, 0x4e, 0x2d, 0xd1, 0x47, 0xb0, 0xca, 0x0c, 0xf5, 0x0b, 0x12, 0xf3, 0x32, 0x93, + 0xf5, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0xc4, 0xe0, 0x32, 0x7a, 0x08, 0x56, 0x99, 0xa1, 0x94, + 0x64, 0x72, 0x7e, 0x71, 0x6e, 0x7e, 0x71, 0x3c, 0x58, 0x95, 0x3e, 0x84, 0x03, 0xd1, 0xa2, 0x14, + 0xca, 0xc5, 0x1d, 0x00, 0x32, 0xc1, 0x17, 0xac, 0x58, 0x48, 0x88, 0x8b, 0x25, 0x25, 0xb1, 0x24, + 0x51, 0x82, 0x51, 0x81, 0x51, 0x83, 0x27, 0x08, 0xcc, 0xb6, 0x32, 0x3a, 0xb5, 0x45, 0x57, 0x0f, + 0xbb, 0xc9, 0x7a, 0x6e, 0xa9, 0xa9, 0x10, 0x9d, 0x9e, 0xb9, 0x05, 0x39, 0xa9, 0xb9, 0xa9, 0x79, + 0x25, 0x89, 0x25, 0x99, 0xf9, 0x79, 0x4e, 0x81, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, + 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, + 0xc7, 0x10, 0x65, 0x9e, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x5f, 0x9c, + 0x9d, 0x59, 0xa0, 0x9b, 0x9b, 0x5a, 0x86, 0xe4, 0xa3, 0x0a, 0x24, 0x76, 0x41, 0x4e, 0x69, 0x7a, + 0x66, 0x5e, 0xb1, 0x7e, 0x6e, 0x7e, 0x72, 0x76, 0x71, 0x12, 0x1b, 0xd8, 0xc1, 0xc6, 0x80, 0x00, + 0x00, 0x00, 0xff, 0xff, 0xe7, 0x29, 0xf1, 0xd2, 0x09, 0x01, 0x00, 0x00, +} + +func (m *PanicMarket) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PanicMarket) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PanicMarket) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Data) > 0 { + i -= len(m.Data) + copy(dAtA[i:], m.Data) + i = encodeVarintPanic(dAtA, i, uint64(len(m.Data))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintPanic(dAtA []byte, offset int, v uint64) int { + offset -= sovPanic(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *PanicMarket) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Data) + if l > 0 { + n += 1 + l + sovPanic(uint64(l)) + } + return n +} + +func sovPanic(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozPanic(x uint64) (n int) { + return sovPanic(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *PanicMarket) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPanic + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PanicMarket: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PanicMarket: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Data", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPanic + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthPanic + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthPanic + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Data = append(m.Data[:0], dAtA[iNdEx:postIndex]...) + if m.Data == nil { + m.Data = []byte{} + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipPanic(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthPanic + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipPanic(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowPanic + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowPanic + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowPanic + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthPanic + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupPanic + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthPanic + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthPanic = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowPanic = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupPanic = fmt.Errorf("proto: unexpected end of group") +)