From 136c8fab007d0259913b84ec0e43b6a434a4022a Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 11:03:23 -0500 Subject: [PATCH 01/10] basic keeper funcs --- x/feemarket/keeper/keeper.go | 77 ++++++++++++++++++++++++++++++++++++ x/feemarket/types/keys.go | 11 +++++- 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 x/feemarket/keeper/keeper.go diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go new file mode 100644 index 0000000..e075ce5 --- /dev/null +++ b/x/feemarket/keeper/keeper.go @@ -0,0 +1,77 @@ +package keeper + +import ( + "fmt" + + "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" + + "github.com/skip-mev/feemarket/x/feemarket/types" +) + +type Keeper struct { + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + + // The address that is capable of executing a MsgParams message. + // Typically, this will be the governance module's address. + authority string +} + +// NewKeeper is a wrapper around NewKeeperWithRewardsAddressProvider for backwards compatibility. +func NewKeeper( + cdc codec.BinaryCodec, + storeKey storetypes.StoreKey, + authority string, +) Keeper { + return Keeper{ + cdc, + storeKey, + authority, + } +} + +// Logger returns a auction module-specific 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 { + return k.authority +} + +// GetParams returns the feemarket module's parameters. +func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) { + store := ctx.KVStore(k.storeKey) + + key := types.KeyParams + bz := store.Get(key) + + if len(bz) == 0 { + return types.Params{}, fmt.Errorf("no params found for the feemarket module") + } + + params := types.Params{} + if err := params.Unmarshal(bz); err != nil { + return types.Params{}, err + } + + return params, nil +} + +// SetParams sets the feemarket module's parameters. +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { + store := ctx.KVStore(k.storeKey) + + bz, err := params.Marshal() + if err != nil { + return err + } + + store.Set(types.KeyParams, bz) + + return nil +} diff --git a/x/feemarket/types/keys.go b/x/feemarket/types/keys.go index ead39b8..a2ca5b0 100644 --- a/x/feemarket/types/keys.go +++ b/x/feemarket/types/keys.go @@ -1,8 +1,15 @@ package types const ( - // ModuleName is the name of the module. + // ModuleName is the name of the feemarket module. ModuleName = "feemarket" - // StoreKey is the store key string for the sla module. + // StoreKey is the store key string for the feemarket module. StoreKey = ModuleName ) + +const ( + prefixParams = iota + 1 +) + +// KeyParams is the store key for the feemarket module's parameters. +var KeyParams = []byte{prefixParams} From 5ed1165d32b486632a7987a19c82bf5685fffadd Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 11:07:53 -0500 Subject: [PATCH 02/10] test4 --- testutils/testutils.go | 198 ++++++++++++++++++++++++++++++ x/feemarket/keeper/keeper_test.go | 55 +++++++++ 2 files changed, 253 insertions(+) create mode 100644 testutils/testutils.go create mode 100644 x/feemarket/keeper/keeper_test.go diff --git a/testutils/testutils.go b/testutils/testutils.go new file mode 100644 index 0000000..abe9c33 --- /dev/null +++ b/testutils/testutils.go @@ -0,0 +1,198 @@ +package testutils + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/tx/signing" + authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + "github.com/cosmos/cosmos-sdk/x/auth/tx" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types" +) + +type EncodingConfig struct { + InterfaceRegistry types.InterfaceRegistry + Codec codec.Codec + TxConfig client.TxConfig + Amino *codec.LegacyAmino +} + +func CreateTestEncodingConfig() EncodingConfig { + cdc := codec.NewLegacyAmino() + interfaceRegistry := types.NewInterfaceRegistry() + + banktypes.RegisterInterfaces(interfaceRegistry) + cryptocodec.RegisterInterfaces(interfaceRegistry) + feemarkettypes.RegisterInterfaces(interfaceRegistry) + stakingtypes.RegisterInterfaces(interfaceRegistry) + + codec := codec.NewProtoCodec(interfaceRegistry) + + return EncodingConfig{ + InterfaceRegistry: interfaceRegistry, + Codec: codec, + TxConfig: tx.NewTxConfig(codec, tx.DefaultSignModes), + Amino: cdc, + } +} + +type Account struct { + PrivKey cryptotypes.PrivKey + PubKey cryptotypes.PubKey + Address sdk.AccAddress + ConsKey cryptotypes.PrivKey +} + +func (acc Account) Equals(acc2 Account) bool { + return acc.Address.Equals(acc2.Address) +} + +func RandomAccounts(r *rand.Rand, n int) []Account { + accs := make([]Account, n) + + for i := 0; i < n; i++ { + pkSeed := make([]byte, 15) + r.Read(pkSeed) + + accs[i].PrivKey = secp256k1.GenPrivKeyFromSecret(pkSeed) + accs[i].PubKey = accs[i].PrivKey.PubKey() + accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address()) + + accs[i].ConsKey = ed25519.GenPrivKeyFromSecret(pkSeed) + } + + return accs +} + +func CreateTx(txCfg client.TxConfig, account Account, nonce, timeout uint64, msgs []sdk.Msg, fees ...sdk.Coin) (authsigning.Tx, error) { + txBuilder := txCfg.NewTxBuilder() + if err := txBuilder.SetMsgs(msgs...); err != nil { + return nil, err + } + + sigV2 := signing.SignatureV2{ + PubKey: account.PrivKey.PubKey(), + Data: &signing.SingleSignatureData{ + SignMode: signing.SignMode_SIGN_MODE_DIRECT, + Signature: nil, + }, + Sequence: nonce, + } + if err := txBuilder.SetSignatures(sigV2); err != nil { + return nil, err + } + + txBuilder.SetTimeoutHeight(timeout) + + txBuilder.SetFeeAmount(fees) + + return txBuilder.GetTx(), nil +} + +func CreateFreeTx(txCfg client.TxConfig, account Account, nonce, timeout uint64, validator string, amount sdk.Coin, fees ...sdk.Coin) (authsigning.Tx, error) { + msgs := []sdk.Msg{ + &stakingtypes.MsgDelegate{ + DelegatorAddress: account.Address.String(), + ValidatorAddress: validator, + Amount: amount, + }, + } + + return CreateTx(txCfg, account, nonce, timeout, msgs, fees...) +} + +func CreateRandomTx(txCfg client.TxConfig, account Account, nonce, numberMsgs, timeout uint64, gasLimit uint64, fees ...sdk.Coin) (authsigning.Tx, error) { + msgs := make([]sdk.Msg, numberMsgs) + for i := 0; i < int(numberMsgs); i++ { + msgs[i] = &banktypes.MsgSend{ + FromAddress: account.Address.String(), + ToAddress: account.Address.String(), + } + } + + txBuilder := txCfg.NewTxBuilder() + if err := txBuilder.SetMsgs(msgs...); err != nil { + return nil, err + } + + sigV2 := signing.SignatureV2{ + PubKey: account.PrivKey.PubKey(), + Data: &signing.SingleSignatureData{ + SignMode: signing.SignMode_SIGN_MODE_DIRECT, + Signature: nil, + }, + Sequence: nonce, + } + if err := txBuilder.SetSignatures(sigV2); err != nil { + return nil, err + } + + txBuilder.SetTimeoutHeight(timeout) + + txBuilder.SetFeeAmount(fees) + + txBuilder.SetGasLimit(gasLimit) + + return txBuilder.GetTx(), nil +} + +func CreateRandomTxBz(txCfg client.TxConfig, account Account, nonce, numberMsgs, timeout, gasLimit uint64) ([]byte, error) { + tx, err := CreateRandomTx(txCfg, account, nonce, numberMsgs, timeout, gasLimit) + if err != nil { + return nil, err + } + + return txCfg.TxEncoder()(tx) +} + +func CreateTxWithSigners(txCfg client.TxConfig, nonce, timeout uint64, signers []Account) (authsigning.Tx, error) { + msgs := []sdk.Msg{} + for _, signer := range signers { + msg := CreateRandomMsgs(signer.Address, 1) + msgs = append(msgs, msg...) + } + + txBuilder := txCfg.NewTxBuilder() + if err := txBuilder.SetMsgs(msgs...); err != nil { + return nil, err + } + + sigV2 := signing.SignatureV2{ + PubKey: signers[0].PrivKey.PubKey(), + Data: &signing.SingleSignatureData{ + SignMode: signing.SignMode_SIGN_MODE_DIRECT, + Signature: nil, + }, + Sequence: nonce, + } + + if err := txBuilder.SetSignatures(sigV2); err != nil { + return nil, err + } + + txBuilder.SetTimeoutHeight(timeout) + + return txBuilder.GetTx(), nil +} + +func CreateRandomMsgs(acc sdk.AccAddress, numberMsgs int) []sdk.Msg { + msgs := make([]sdk.Msg, numberMsgs) + for i := 0; i < numberMsgs; i++ { + msgs[i] = &banktypes.MsgSend{ + FromAddress: acc.String(), + ToAddress: acc.String(), + } + } + + return msgs +} diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go new file mode 100644 index 0000000..8cdf021 --- /dev/null +++ b/x/feemarket/keeper/keeper_test.go @@ -0,0 +1,55 @@ +package keeper_test + +import ( + "github.com/skip-mev/feemarket/x/feemarket/keeper" + "testing" + + storetypes "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/stretchr/testify/suite" +) + +type KeeperTestSuite struct { + suite.Suite + + auctionkeeper keeper.Keeper + encCfg testutils.EncodingConfig + ctx sdk.Context + key *storetypes.KVStoreKey + authorityAccount sdk.AccAddress +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} + +func (s *KeeperTestSuite) SetupTest() { + s.encCfg = testutils.CreateTestEncodingConfig() + s.key = storetypes.NewKVStoreKey(types.StoreKey) + testCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test")) + s.ctx = testCtx.Ctx + + s.accountKeeper = mocks.NewAccountKeeper(s.T()) + s.accountKeeper.On("GetModuleAddress", types.ModuleName).Return(sdk.AccAddress{}).Maybe() + + s.bankKeeper = mocks.NewBankKeeper(s.T()) + s.distrKeeper = mocks.NewDistributionKeeper(s.T()) + s.stakingKeeper = mocks.NewStakingKeeper(s.T()) + s.authorityAccount = sdk.AccAddress([]byte("authority")) + s.auctionkeeper = keeper.NewKeeper( + s.encCfg.Codec, + s.key, + s.accountKeeper, + s.bankKeeper, + s.distrKeeper, + s.stakingKeeper, + s.authorityAccount.String(), + ) + + err := s.auctionkeeper.SetParams(s.ctx, types.DefaultParams()) + s.Require().NoError(err) + + s.msgServer = keeper.NewMsgServerImpl(s.auctionkeeper) +} From 56afdb303298f617e52db48359a74724e8eec741 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 11:15:42 -0500 Subject: [PATCH 03/10] fix test --- x/feemarket/keeper/keeper_test.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go index 8cdf021..f6c05b2 100644 --- a/x/feemarket/keeper/keeper_test.go +++ b/x/feemarket/keeper/keeper_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "github.com/skip-mev/feemarket/x/feemarket/keeper" "testing" storetypes "github.com/cosmos/cosmos-sdk/store/types" @@ -9,12 +8,16 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/suite" + + "github.com/skip-mev/feemarket/testutils" + "github.com/skip-mev/feemarket/x/feemarket/keeper" + "github.com/skip-mev/feemarket/x/feemarket/types" ) type KeeperTestSuite struct { suite.Suite - auctionkeeper keeper.Keeper + feemarketKeeper keeper.Keeper encCfg testutils.EncodingConfig ctx sdk.Context key *storetypes.KVStoreKey @@ -31,25 +34,13 @@ func (s *KeeperTestSuite) SetupTest() { testCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test")) s.ctx = testCtx.Ctx - s.accountKeeper = mocks.NewAccountKeeper(s.T()) - s.accountKeeper.On("GetModuleAddress", types.ModuleName).Return(sdk.AccAddress{}).Maybe() - - s.bankKeeper = mocks.NewBankKeeper(s.T()) - s.distrKeeper = mocks.NewDistributionKeeper(s.T()) - s.stakingKeeper = mocks.NewStakingKeeper(s.T()) - s.authorityAccount = sdk.AccAddress([]byte("authority")) - s.auctionkeeper = keeper.NewKeeper( + s.authorityAccount = []byte("authority") + s.feemarketKeeper = keeper.NewKeeper( s.encCfg.Codec, s.key, - s.accountKeeper, - s.bankKeeper, - s.distrKeeper, - s.stakingKeeper, s.authorityAccount.String(), ) - err := s.auctionkeeper.SetParams(s.ctx, types.DefaultParams()) + err := s.feemarketKeeper.SetParams(s.ctx, types.DefaultParams()) s.Require().NoError(err) - - s.msgServer = keeper.NewMsgServerImpl(s.auctionkeeper) } From e2c700817b342b6d709dd20a0bdf0c3b5a3c4a20 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 11:31:57 -0500 Subject: [PATCH 04/10] test --- x/feemarket/keeper/keeper.go | 44 ++++++++++++++++++++++++++----- x/feemarket/keeper/keeper_test.go | 24 ++++++++++++++++- x/feemarket/types/keys.go | 10 +++++-- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index e075ce5..00b7b20 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -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" @@ -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 @@ -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 @@ -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() diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go index f6c05b2..c04a40e 100644 --- a/x/feemarket/keeper/keeper_test.go +++ b/x/feemarket/keeper/keeper_test.go @@ -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" @@ -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 @@ -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) + }) +} diff --git a/x/feemarket/types/keys.go b/x/feemarket/types/keys.go index a2ca5b0..edfcb50 100644 --- a/x/feemarket/types/keys.go +++ b/x/feemarket/types/keys.go @@ -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} +) From cb57c3ae74e1f5484bbae2cff875c0e86bf78eba Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 11:46:27 -0500 Subject: [PATCH 05/10] keeper --- x/feemarket/keeper/keeper.go | 33 +++++++++++++++++++++++++++---- x/feemarket/keeper/keeper_test.go | 13 ++++++------ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index 00b7b20..550c749 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -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) } @@ -55,8 +57,31 @@ 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) @@ -64,7 +89,7 @@ func (k *Keeper) SetData(ctx sdk.Context, data []byte) { } // 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) diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go index c04a40e..a4a1581 100644 --- a/x/feemarket/keeper/keeper_test.go +++ b/x/feemarket/keeper/keeper_test.go @@ -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) }) } From de5a79afce40b8af4f3c8b11d14dbd9101ae60f4 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 11:49:05 -0500 Subject: [PATCH 06/10] beautify --- x/feemarket/keeper/keeper.go | 3 +-- x/feemarket/keeper/keeper_test.go | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index 550c749..2bdd300 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -3,13 +3,12 @@ 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" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/skip-mev/feemarket/x/feemarket/interfaces" "github.com/skip-mev/feemarket/x/feemarket/types" ) diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go index a4a1581..533ad5f 100644 --- a/x/feemarket/keeper/keeper_test.go +++ b/x/feemarket/keeper/keeper_test.go @@ -3,8 +3,6 @@ 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" @@ -13,6 +11,7 @@ import ( "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/types" ) From 8b9e117705db3b69bb5021522e25fd875804076b Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 11:50:48 -0500 Subject: [PATCH 07/10] beautify --- x/feemarket/keeper/keeper.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index 2bdd300..67f8848 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -56,7 +56,7 @@ func (k *Keeper) Plugin() interfaces.FeeMarketImplementation { return k.plugin } -// SetFeeMarket sets the fee market implementation data in the keeper +// 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 { @@ -64,11 +64,12 @@ func (k *Keeper) SetFeeMarket(ctx sdk.Context, fm interfaces.FeeMarketImplementa } k.setData(ctx, bz) + k.plugin = fm return nil } -// GetFeeMarket gets arbitrary byte data in the keeper. +// GetFeeMarket gets the fee market implementation data in the keeper. Will func (k *Keeper) GetFeeMarket(ctx sdk.Context) (interfaces.FeeMarketImplementation, error) { bz, err := k.getData(ctx) if err != nil { @@ -79,7 +80,7 @@ func (k *Keeper) GetFeeMarket(ctx sdk.Context) (interfaces.FeeMarketImplementati return k.plugin, err } -// SetData sets arbitrary byte data in the keeper. +// setData sets arbitrary byte data in the keeper. func (k *Keeper) setData(ctx sdk.Context, data []byte) { // TODO: limit max data size? @@ -87,7 +88,7 @@ func (k *Keeper) setData(ctx sdk.Context, data []byte) { store.Set(types.KeyData, data) } -// GetData gets arbitrary byte data in the keeper. +// 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) From 2a00da28f69553e849aa891f277b9c685ef2a4ee Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 12:37:52 -0500 Subject: [PATCH 08/10] keystate --- x/feemarket/keeper/keeper.go | 4 ++-- x/feemarket/types/keys.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index 67f8848..b16b4a3 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -85,13 +85,13 @@ func (k *Keeper) setData(ctx sdk.Context, data []byte) { // TODO: limit max data size? store := ctx.KVStore(k.storeKey) - store.Set(types.KeyData, data) + store.Set(types.KeyState, 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) + bz := store.Get(types.KeyState) if len(bz) == 0 { return nil, fmt.Errorf("no data set in the keeper") diff --git a/x/feemarket/types/keys.go b/x/feemarket/types/keys.go index edfcb50..a0cc483 100644 --- a/x/feemarket/types/keys.go +++ b/x/feemarket/types/keys.go @@ -9,13 +9,13 @@ const ( const ( prefixParams = iota + 1 - prefixData + prefixState ) 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} + // KeyState is the store key for the feemarket module's data. + KeyState = []byte{prefixState} ) From 91d899af99116837949a5bf2783cef403c3c7bab Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 12:40:24 -0500 Subject: [PATCH 09/10] simplify --- testutils/testutils.go | 67 ------------------------------------------ 1 file changed, 67 deletions(-) diff --git a/testutils/testutils.go b/testutils/testutils.go index abe9c33..5bc9d8c 100644 --- a/testutils/testutils.go +++ b/testutils/testutils.go @@ -74,43 +74,6 @@ func RandomAccounts(r *rand.Rand, n int) []Account { return accs } -func CreateTx(txCfg client.TxConfig, account Account, nonce, timeout uint64, msgs []sdk.Msg, fees ...sdk.Coin) (authsigning.Tx, error) { - txBuilder := txCfg.NewTxBuilder() - if err := txBuilder.SetMsgs(msgs...); err != nil { - return nil, err - } - - sigV2 := signing.SignatureV2{ - PubKey: account.PrivKey.PubKey(), - Data: &signing.SingleSignatureData{ - SignMode: signing.SignMode_SIGN_MODE_DIRECT, - Signature: nil, - }, - Sequence: nonce, - } - if err := txBuilder.SetSignatures(sigV2); err != nil { - return nil, err - } - - txBuilder.SetTimeoutHeight(timeout) - - txBuilder.SetFeeAmount(fees) - - return txBuilder.GetTx(), nil -} - -func CreateFreeTx(txCfg client.TxConfig, account Account, nonce, timeout uint64, validator string, amount sdk.Coin, fees ...sdk.Coin) (authsigning.Tx, error) { - msgs := []sdk.Msg{ - &stakingtypes.MsgDelegate{ - DelegatorAddress: account.Address.String(), - ValidatorAddress: validator, - Amount: amount, - }, - } - - return CreateTx(txCfg, account, nonce, timeout, msgs, fees...) -} - func CreateRandomTx(txCfg client.TxConfig, account Account, nonce, numberMsgs, timeout uint64, gasLimit uint64, fees ...sdk.Coin) (authsigning.Tx, error) { msgs := make([]sdk.Msg, numberMsgs) for i := 0; i < int(numberMsgs); i++ { @@ -155,36 +118,6 @@ func CreateRandomTxBz(txCfg client.TxConfig, account Account, nonce, numberMsgs, return txCfg.TxEncoder()(tx) } -func CreateTxWithSigners(txCfg client.TxConfig, nonce, timeout uint64, signers []Account) (authsigning.Tx, error) { - msgs := []sdk.Msg{} - for _, signer := range signers { - msg := CreateRandomMsgs(signer.Address, 1) - msgs = append(msgs, msg...) - } - - txBuilder := txCfg.NewTxBuilder() - if err := txBuilder.SetMsgs(msgs...); err != nil { - return nil, err - } - - sigV2 := signing.SignatureV2{ - PubKey: signers[0].PrivKey.PubKey(), - Data: &signing.SingleSignatureData{ - SignMode: signing.SignMode_SIGN_MODE_DIRECT, - Signature: nil, - }, - Sequence: nonce, - } - - if err := txBuilder.SetSignatures(sigV2); err != nil { - return nil, err - } - - txBuilder.SetTimeoutHeight(timeout) - - return txBuilder.GetTx(), nil -} - func CreateRandomMsgs(acc sdk.AccAddress, numberMsgs int) []sdk.Msg { msgs := make([]sdk.Msg, numberMsgs) for i := 0; i < numberMsgs; i++ { From ffa7fa61f0eadde7201782b4a88a7f56b14e8b97 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Fri, 10 Nov 2023 12:41:27 -0500 Subject: [PATCH 10/10] docs --- x/feemarket/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index b16b4a3..06de7ff 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -24,7 +24,7 @@ type Keeper struct { authority string } -// NewKeeper is a wrapper around NewKeeperWithRewardsAddressProvider for backwards compatibility. +// NewKeeper constructs a new feemarket keeper. func NewKeeper( cdc codec.BinaryCodec, storeKey storetypes.StoreKey,