From 3888b8b3660171b6ab1a3445fe22de03bd94862c Mon Sep 17 00:00:00 2001 From: Thomas van Dam Date: Wed, 21 Aug 2024 14:39:29 +0200 Subject: [PATCH] chore(data-proxy): avoid accessing the stores directly from boundaries Closes: #316 --- x/data-proxy/keeper/abci.go | 8 +-- x/data-proxy/keeper/common_test.go | 2 +- x/data-proxy/keeper/genesis.go | 14 ++--- x/data-proxy/keeper/grpc_query.go | 9 ++- x/data-proxy/keeper/grpc_query_test.go | 2 +- x/data-proxy/keeper/keeper.go | 84 +++++++++++++++----------- x/data-proxy/keeper/keeper_test.go | 23 +++---- x/data-proxy/keeper/msg_server.go | 38 ++++++------ x/data-proxy/keeper/msg_server_test.go | 49 ++++++++------- x/data-proxy/keeper/params.go | 16 +++++ 10 files changed, 140 insertions(+), 105 deletions(-) create mode 100644 x/data-proxy/keeper/params.go diff --git a/x/data-proxy/keeper/abci.go b/x/data-proxy/keeper/abci.go index 0221cfc6..f4f10250 100644 --- a/x/data-proxy/keeper/abci.go +++ b/x/data-proxy/keeper/abci.go @@ -3,8 +3,6 @@ package keeper import ( "encoding/hex" - "cosmossdk.io/collections" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/sedaprotocol/seda-chain/x/data-proxy/types" @@ -36,7 +34,7 @@ func (k *Keeper) ProcessFeeUpdates(ctx sdk.Context) error { } for _, pubkey := range pubkeys { - proxyConfig, err := k.DataProxyConfigs.Get(ctx, pubkey) + proxyConfig, err := k.GetDataProxyConfig(ctx, pubkey) if err != nil { return err } @@ -44,11 +42,11 @@ func (k *Keeper) ProcessFeeUpdates(ctx sdk.Context) error { proxyConfig.Fee = proxyConfig.FeeUpdate.NewFee proxyConfig.FeeUpdate = nil - if err := k.DataProxyConfigs.Set(ctx, pubkey, proxyConfig); err != nil { + if err := k.SetDataProxyConfig(ctx, pubkey, proxyConfig); err != nil { return err } - if err := k.FeeUpdateQueue.Remove(ctx, collections.Join(blockHeight, pubkey)); err != nil { + if err := k.RemoveFeeUpdate(ctx, blockHeight, pubkey); err != nil { return err } diff --git a/x/data-proxy/keeper/common_test.go b/x/data-proxy/keeper/common_test.go index 889a4bfc..a115c2db 100644 --- a/x/data-proxy/keeper/common_test.go +++ b/x/data-proxy/keeper/common_test.go @@ -74,7 +74,7 @@ func (s *KeeperTestSuite) SetupTest() { types.RegisterQueryServer(queryHelper, querier) s.queryClient = types.NewQueryClient(queryHelper) - err := s.keeper.Params.Set(s.ctx, types.DefaultParams()) + err := s.keeper.SetParams(s.ctx, types.DefaultParams()) s.Require().NoError(err) } diff --git a/x/data-proxy/keeper/genesis.go b/x/data-proxy/keeper/genesis.go index 7343bb82..551c06e2 100644 --- a/x/data-proxy/keeper/genesis.go +++ b/x/data-proxy/keeper/genesis.go @@ -1,8 +1,6 @@ package keeper import ( - "cosmossdk.io/collections" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/sedaprotocol/seda-chain/x/data-proxy/types" @@ -10,18 +8,18 @@ import ( // InitGenesis initializes the store based on the given genesis state. func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { - if err := k.Params.Set(ctx, data.Params); err != nil { + if err := k.SetParams(ctx, data.Params); err != nil { panic(err) } for _, dataProxyConfig := range data.DataProxyConfigs { - if err := k.DataProxyConfigs.Set(ctx, dataProxyConfig.DataProxyPubkey, *dataProxyConfig.Config); err != nil { + if err := k.SetDataProxyConfig(ctx, dataProxyConfig.DataProxyPubkey, *dataProxyConfig.Config); err != nil { panic(err) } } for _, feeUpdate := range data.FeeUpdateQueue { - if err := k.FeeUpdateQueue.Set(ctx, collections.Join(feeUpdate.UpdateHeight, feeUpdate.DataProxyPubkey)); err != nil { + if err := k.SetFeeUpdate(ctx, feeUpdate.UpdateHeight, feeUpdate.DataProxyPubkey); err != nil { panic(err) } } @@ -31,7 +29,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState { var gs types.GenesisState - params, err := k.Params.Get(ctx) + params, err := k.params.Get(ctx) if err != nil { panic(err) } @@ -55,7 +53,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState { func (k Keeper) getAllDataProxyConfigs(ctx sdk.Context) ([]types.DataProxyConfig, error) { configs := make([]types.DataProxyConfig, 0) - itr, err := k.DataProxyConfigs.Iterate(ctx, nil) + itr, err := k.dataProxyConfigs.Iterate(ctx, nil) if err != nil { return nil, err } @@ -79,7 +77,7 @@ func (k Keeper) getAllDataProxyConfigs(ctx sdk.Context) ([]types.DataProxyConfig func (k Keeper) getAllFeeUpdateRecords(ctx sdk.Context) ([]types.FeeUpdateQueueRecord, error) { feeUpdates := make([]types.FeeUpdateQueueRecord, 0) - itr, err := k.FeeUpdateQueue.Iterate(ctx, nil) + itr, err := k.feeUpdateQueue.Iterate(ctx, nil) if err != nil { return nil, err } diff --git a/x/data-proxy/keeper/grpc_query.go b/x/data-proxy/keeper/grpc_query.go index b2cf88e5..a8dd9bfb 100644 --- a/x/data-proxy/keeper/grpc_query.go +++ b/x/data-proxy/keeper/grpc_query.go @@ -2,9 +2,11 @@ package keeper import ( "context" + "encoding/hex" "errors" "cosmossdk.io/collections" + errorsmod "cosmossdk.io/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -18,7 +20,12 @@ type Querier struct { } func (q Querier) DataProxyConfig(ctx context.Context, req *types.QueryDataProxyConfigRequest) (*types.QueryDataProxyConfigResponse, error) { - result, err := q.GetDataProxyConfig(ctx, req.PubKey) + pubKeyBytes, err := hex.DecodeString(req.PubKey) + if err != nil { + return nil, errorsmod.Wrapf(err, "invalid hex in pubkey: %s", req.PubKey) + } + + result, err := q.GetDataProxyConfig(ctx, pubKeyBytes) if err != nil { if errors.Is(err, collections.ErrNotFound) { return nil, sdkerrors.ErrNotFound.Wrapf("no data proxy registered for %s", req.PubKey) diff --git a/x/data-proxy/keeper/grpc_query_test.go b/x/data-proxy/keeper/grpc_query_test.go index fd255ba7..458e5f52 100644 --- a/x/data-proxy/keeper/grpc_query_test.go +++ b/x/data-proxy/keeper/grpc_query_test.go @@ -61,7 +61,7 @@ func (s *KeeperTestSuite) TestQuerier_ProxyConfig() { pubkeyBytes, err := hex.DecodeString(tt.pubKeyHex) s.Require().NoError(err) - err = s.keeper.DataProxyConfigs.Set(s.ctx, pubkeyBytes, *tt.config) + err = s.keeper.SetDataProxyConfig(s.ctx, pubkeyBytes, *tt.config) s.Require().NoError(err) } diff --git a/x/data-proxy/keeper/keeper.go b/x/data-proxy/keeper/keeper.go index 80275f65..d07dc915 100644 --- a/x/data-proxy/keeper/keeper.go +++ b/x/data-proxy/keeper/keeper.go @@ -2,7 +2,6 @@ package keeper import ( "context" - "encoding/hex" "fmt" "cosmossdk.io/collections" @@ -22,18 +21,18 @@ type Keeper struct { authority string Schema collections.Schema - DataProxyConfigs collections.Map[[]byte, types.ProxyConfig] - FeeUpdateQueue collections.KeySet[collections.Pair[int64, []byte]] - Params collections.Item[types.Params] + dataProxyConfigs collections.Map[[]byte, types.ProxyConfig] + feeUpdateQueue collections.KeySet[collections.Pair[int64, []byte]] + params collections.Item[types.Params] } func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string) *Keeper { sb := collections.NewSchemaBuilder(storeService) k := Keeper{ authority: authority, - DataProxyConfigs: collections.NewMap(sb, types.DataProxyConfigPrefix, "configs", collections.BytesKey, codec.CollValue[types.ProxyConfig](cdc)), - FeeUpdateQueue: collections.NewKeySet(sb, types.FeeUpdatesPrefix, "fee_updates", collections.PairKeyCodec(collections.Int64Key, collections.BytesKey)), - Params: collections.NewItem(sb, types.ParamsPrefix, "params", codec.CollValue[types.Params](cdc)), + dataProxyConfigs: collections.NewMap(sb, types.DataProxyConfigPrefix, "configs", collections.BytesKey, codec.CollValue[types.ProxyConfig](cdc)), + feeUpdateQueue: collections.NewKeySet(sb, types.FeeUpdatesPrefix, "fee_updates", collections.PairKeyCodec(collections.Int64Key, collections.BytesKey)), + params: collections.NewItem(sb, types.ParamsPrefix, "params", codec.CollValue[types.Params](cdc)), } schema, err := sb.Build() @@ -49,13 +48,16 @@ func (k Keeper) GetAuthority() string { return k.authority } -func (k Keeper) GetDataProxyConfig(ctx context.Context, pubKey string) (result types.ProxyConfig, err error) { - pubKeyBytes, err := hex.DecodeString(pubKey) - if err != nil { - return types.ProxyConfig{}, err - } +func (k Keeper) HasDataProxy(ctx sdk.Context, pubKey []byte) (bool, error) { + return k.dataProxyConfigs.Has(ctx, pubKey) +} + +func (k Keeper) SetDataProxyConfig(ctx context.Context, pubKey []byte, proxyConfig types.ProxyConfig) error { + return k.dataProxyConfigs.Set(ctx, pubKey, proxyConfig) +} - config, err := k.DataProxyConfigs.Get(ctx, pubKeyBytes) +func (k Keeper) GetDataProxyConfig(ctx context.Context, pubKey []byte) (result types.ProxyConfig, err error) { + config, err := k.dataProxyConfigs.Get(ctx, pubKey) if err != nil { return types.ProxyConfig{}, err } @@ -63,28 +65,15 @@ func (k Keeper) GetDataProxyConfig(ctx context.Context, pubKey string) (result t return config, nil } -func (k Keeper) GetFeeUpdatePubKeys(ctx context.Context, activationHeight int64) ([][]byte, error) { - pubkeys := make([][]byte, 0) - rng := collections.NewPrefixedPairRange[int64, []byte](activationHeight) - - itr, err := k.FeeUpdateQueue.Iterate(ctx, rng) - if err != nil { - return nil, err - } - - keys, err := itr.Keys() - if err != nil { - return nil, err - } - - for _, k := range keys { - pubkeys = append(pubkeys, k.K2()) - } +func (k Keeper) SetFeeUpdate(ctx sdk.Context, height int64, pubKey []byte) error { + return k.feeUpdateQueue.Set(ctx, collections.Join(height, pubKey)) +} - return pubkeys, nil +func (k Keeper) RemoveFeeUpdate(ctx sdk.Context, height int64, pubKey []byte) error { + return k.feeUpdateQueue.Remove(ctx, collections.Join(height, pubKey)) } -func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxyConfig *types.ProxyConfig, newFee *sdk.Coin, updateDelay uint32) (int64, error) { +func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxyConfig types.ProxyConfig, newFee *sdk.Coin, updateDelay uint32) (int64, error) { // Determine update height updateHeight := ctx.BlockHeight() + int64(updateDelay) feeUpdate := &types.FeeUpdate{ @@ -94,7 +83,7 @@ func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxy // Delete previous pending update, if applicable if proxyConfig.FeeUpdate != nil { - err := k.FeeUpdateQueue.Remove(ctx, collections.Join(proxyConfig.FeeUpdate.UpdateHeight, pubKeyBytes)) + err := k.RemoveFeeUpdate(ctx, proxyConfig.FeeUpdate.UpdateHeight, pubKeyBytes) if err != nil { return 0, err } @@ -102,12 +91,12 @@ func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxy // Schedule new update proxyConfig.FeeUpdate = feeUpdate - err := k.FeeUpdateQueue.Set(ctx, collections.Join(updateHeight, pubKeyBytes)) + err := k.SetFeeUpdate(ctx, updateHeight, pubKeyBytes) if err != nil { return 0, err } - err = k.DataProxyConfigs.Set(ctx, pubKeyBytes, *proxyConfig) + err = k.SetDataProxyConfig(ctx, pubKeyBytes, proxyConfig) if err != nil { return 0, err } @@ -115,6 +104,31 @@ func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxy return updateHeight, nil } +func (k Keeper) GetFeeUpdatePubKeys(ctx sdk.Context, activationHeight int64) ([][]byte, error) { + pubkeys := make([][]byte, 0) + rng := collections.NewPrefixedPairRange[int64, []byte](activationHeight) + + itr, err := k.feeUpdateQueue.Iterate(ctx, rng) + if err != nil { + return nil, err + } + + keys, err := itr.Keys() + if err != nil { + return nil, err + } + + for _, k := range keys { + pubkeys = append(pubkeys, k.K2()) + } + + return pubkeys, nil +} + +func (k Keeper) HasFeeUpdate(ctx sdk.Context, height int64, pubKey []byte) (bool, error) { + return k.feeUpdateQueue.Has(ctx, collections.Join(height, pubKey)) +} + func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } diff --git a/x/data-proxy/keeper/keeper_test.go b/x/data-proxy/keeper/keeper_test.go index 771a0c1a..6c70ab1e 100644 --- a/x/data-proxy/keeper/keeper_test.go +++ b/x/data-proxy/keeper/keeper_test.go @@ -5,8 +5,6 @@ import ( "github.com/cometbft/cometbft/crypto/secp256k1" - "cosmossdk.io/collections" - "github.com/sedaprotocol/seda-chain/x/data-proxy/types" ) @@ -15,9 +13,8 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { s.SetupTest() pubKeyBytes := secp256k1.GenPrivKey().PubKey().Bytes() - pubKeyHex := hex.EncodeToString(pubKeyBytes) - err := s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, types.ProxyConfig{ + err := s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, types.ProxyConfig{ PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5", Fee: s.NewFeeFromString("10000"), Memo: "test", @@ -28,13 +25,13 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5", }) s.Require().NoError(err) - err = s.keeper.FeeUpdateQueue.Set(s.ctx, collections.Join(int64(0), pubKeyBytes)) + err = s.keeper.SetFeeUpdate(s.ctx, int64(0), pubKeyBytes) s.Require().NoError(err) err = s.keeper.EndBlock(s.ctx) s.Require().NoError(err) - proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyHex) + proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().Equal(types.ProxyConfig{ @@ -45,7 +42,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5", }, proxyConfig) - found, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(int64(0), pubKeyBytes)) + found, err := s.keeper.HasFeeUpdate(s.ctx, int64(0), pubKeyBytes) s.Require().NoError(err) s.Require().False(found) }) @@ -71,7 +68,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { updateHeight = int64(1) } - err := s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, types.ProxyConfig{ + err := s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, types.ProxyConfig{ PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5", Fee: s.NewFeeFromString("10"), Memo: "test", @@ -82,7 +79,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5", }) s.Require().NoError(err) - err = s.keeper.FeeUpdateQueue.Set(s.ctx, collections.Join(updateHeight, pubKeyBytes)) + err = s.keeper.SetFeeUpdate(s.ctx, updateHeight, pubKeyBytes) s.Require().NoError(err) } @@ -90,7 +87,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { s.Require().NoError(err) for i, testInput := range pubKeys { - proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyHex) + proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyBytes) s.Require().NoError(err) expected := types.ProxyConfig{ @@ -114,7 +111,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { if i >= 5 { updateHeight = int64(1) } - found, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(updateHeight, testInput.pubKeyBytes)) + found, err := s.keeper.HasFeeUpdate(s.ctx, updateHeight, testInput.pubKeyBytes) s.Require().NoError(err) if i < 5 { s.Require().False(found) @@ -129,7 +126,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { s.Require().NoError(err) for i, testInput := range pubKeys { - proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyHex) + proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyBytes) s.Require().NoError(err) expected := types.ProxyConfig{ @@ -152,7 +149,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() { if i >= 5 { updateHeight = int64(1) } - found, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(updateHeight, testInput.pubKeyBytes)) + found, err := s.keeper.HasFeeUpdate(s.ctx, updateHeight, testInput.pubKeyBytes) s.Require().NoError(err) s.Require().False(found) } diff --git a/x/data-proxy/keeper/msg_server.go b/x/data-proxy/keeper/msg_server.go index 41f87d08..7da845d5 100644 --- a/x/data-proxy/keeper/msg_server.go +++ b/x/data-proxy/keeper/msg_server.go @@ -51,9 +51,7 @@ func (m msgServer) RegisterDataProxy(goCtx context.Context, msg *types.MsgRegist return nil, errorsmod.Wrapf(err, "invalid hex in signature: %s", msg.Signature) } - // TODO check if fee is in native denom - - found, err := m.DataProxyConfigs.Has(ctx, pubKeyBytes) + found, err := m.HasDataProxy(ctx, pubKeyBytes) if err != nil { return nil, err } @@ -87,7 +85,7 @@ func (m msgServer) RegisterDataProxy(goCtx context.Context, msg *types.MsgRegist return nil, err } - err = m.DataProxyConfigs.Set(ctx, pubKeyBytes, proxyConfig) + err = m.SetDataProxyConfig(ctx, pubKeyBytes, proxyConfig) if err != nil { return nil, err } @@ -109,7 +107,7 @@ func (m msgServer) EditDataProxy(goCtx context.Context, msg *types.MsgEditDataPr return nil, errorsmod.Wrapf(err, "invalid hex in pubkey: %s", msg.PubKey) } - proxyConfig, err := m.DataProxyConfigs.Get(ctx, pubKeyBytes) + proxyConfig, err := m.GetDataProxyConfig(ctx, pubKeyBytes) if err != nil { if errors.Is(err, collections.ErrNotFound) { return nil, sdkerrors.ErrNotFound.Wrapf("no data proxy registered for %s", msg.PubKey) @@ -128,7 +126,7 @@ func (m msgServer) EditDataProxy(goCtx context.Context, msg *types.MsgEditDataPr // If there is no new fee we can terminate early if msg.NewFee == nil { - err = m.DataProxyConfigs.Set(ctx, pubKeyBytes, proxyConfig) + err = m.SetDataProxyConfig(ctx, pubKeyBytes, proxyConfig) if err != nil { return nil, err } @@ -138,22 +136,22 @@ func (m msgServer) EditDataProxy(goCtx context.Context, msg *types.MsgEditDataPr return &types.MsgEditDataProxyResponse{}, nil } - params, err := m.Keeper.Params.Get(ctx) + minimumUpdateDelay, err := m.GetMinimumUpdateDelay(ctx) if err != nil { return nil, err } - updateDelay := params.MinFeeUpdateDelay + updateDelay := minimumUpdateDelay // Validate custom delay if passed if msg.FeeUpdateDelay != types.UseMinimumDelay { - if msg.FeeUpdateDelay < params.MinFeeUpdateDelay { - return nil, types.ErrInvalidDelay.Wrapf("minimum delay %d, got %d", params.MinFeeUpdateDelay, msg.FeeUpdateDelay) + if msg.FeeUpdateDelay < minimumUpdateDelay { + return nil, types.ErrInvalidDelay.Wrapf("minimum delay %d, got %d", minimumUpdateDelay, msg.FeeUpdateDelay) } updateDelay = msg.FeeUpdateDelay } - updateHeight, err := m.Keeper.processProxyFeeUpdate(ctx, pubKeyBytes, &proxyConfig, msg.NewFee, updateDelay) + updateHeight, err := m.processProxyFeeUpdate(ctx, pubKeyBytes, proxyConfig, msg.NewFee, updateDelay) if err != nil { return nil, err } @@ -196,7 +194,7 @@ func (m msgServer) TransferAdmin(goCtx context.Context, msg *types.MsgTransferAd return nil, errorsmod.Wrapf(err, "invalid hex in pubkey: %s", msg.PubKey) } - proxyConfig, err := m.DataProxyConfigs.Get(ctx, pubKeyBytes) + proxyConfig, err := m.GetDataProxyConfig(ctx, pubKeyBytes) if err != nil { if errors.Is(err, collections.ErrNotFound) { return nil, sdkerrors.ErrNotFound.Wrapf("no data proxy registered for %s", msg.PubKey) @@ -210,7 +208,7 @@ func (m msgServer) TransferAdmin(goCtx context.Context, msg *types.MsgTransferAd proxyConfig.AdminAddress = msg.NewAdminAddress - err = m.DataProxyConfigs.Set(ctx, pubKeyBytes, proxyConfig) + err = m.SetDataProxyConfig(ctx, pubKeyBytes, proxyConfig) if err != nil { return nil, err } @@ -223,20 +221,20 @@ func (m msgServer) TransferAdmin(goCtx context.Context, msg *types.MsgTransferAd return &types.MsgTransferAdminResponse{}, nil } -func (m msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { +func (m msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - if _, err := sdk.AccAddressFromBech32(req.Authority); err != nil { - return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid authority address: %s", req.Authority) + if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil { + return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid authority address: %s", msg.Authority) } - if m.GetAuthority() != req.Authority { - return nil, sdkerrors.ErrorInvalidSigner.Wrapf("unauthorized authority; expected %s, got %s", m.GetAuthority(), req.Authority) + if m.GetAuthority() != msg.Authority { + return nil, sdkerrors.ErrorInvalidSigner.Wrapf("unauthorized authority; expected %s, got %s", m.GetAuthority(), msg.Authority) } - if err := req.Params.Validate(); err != nil { + if err := msg.Params.Validate(); err != nil { return nil, err } - if err := m.Params.Set(ctx, req.Params); err != nil { + if err := m.SetParams(ctx, msg.Params); err != nil { return nil, err } diff --git a/x/data-proxy/keeper/msg_server_test.go b/x/data-proxy/keeper/msg_server_test.go index f519137e..2026d375 100644 --- a/x/data-proxy/keeper/msg_server_test.go +++ b/x/data-proxy/keeper/msg_server_test.go @@ -3,8 +3,6 @@ package keeper_test import ( "encoding/hex" - "cosmossdk.io/collections" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -151,7 +149,10 @@ func (s *KeeperTestSuite) TestMsgServer_RegisterDataProxy() { s.Require().NoError(err) - proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, tt.msg.PubKey) + pubKeyBytes, err := hex.DecodeString(tt.msg.PubKey) + s.Require().NoError(err) + + proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().Equal(tt.expected, &proxyConfig) }) @@ -170,7 +171,10 @@ func (s *KeeperTestSuite) TestMsgServer_RegisterDataProxy() { _, err := s.msgSrvr.RegisterDataProxy(s.ctx, msg) s.Require().NoError(err) - proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, msg.PubKey) + pubKeyBytes, err := hex.DecodeString(msg.PubKey) + s.Require().NoError(err) + + proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().Equal(&types.ProxyConfig{ PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5", @@ -326,7 +330,7 @@ func (s *KeeperTestSuite) TestMsgServer_EditDataProxy() { s.Run(tt.name, func() { s.SetupTest() - err = s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, initialProxyConfig) + err = s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, initialProxyConfig) s.Require().NoError(err) res, err := s.msgSrvr.EditDataProxy(s.ctx, tt.msg) @@ -338,12 +342,15 @@ func (s *KeeperTestSuite) TestMsgServer_EditDataProxy() { s.Require().NoError(err) - proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, tt.msg.PubKey) + pubKeyBytes, err := hex.DecodeString(tt.msg.PubKey) + s.Require().NoError(err) + + proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().Equal(tt.expected, &proxyConfig) if proxyConfig.FeeUpdate != nil { - updateScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(proxyConfig.FeeUpdate.UpdateHeight, pubKeyBytes)) + updateScheduled, err := s.keeper.HasFeeUpdate(s.ctx, proxyConfig.FeeUpdate.UpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().True(updateScheduled) } @@ -356,7 +363,7 @@ func (s *KeeperTestSuite) TestMsgServer_EditDataProxy() { firstUpdateHeight := int64(types.DefaultMinFeeUpdateDelay + 100) secondUpdateHeight := int64(types.DefaultMinFeeUpdateDelay + 37) - err = s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, initialProxyConfig) + err = s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, initialProxyConfig) s.Require().NoError(err) firstMsg := &types.MsgEditDataProxy{ @@ -372,11 +379,11 @@ func (s *KeeperTestSuite) TestMsgServer_EditDataProxy() { s.Require().NoError(err) s.Require().NotNil(firstRes) - firstProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, firstMsg.PubKey) + firstProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().NotNil(firstProxyConfig.FeeUpdate) - firstUpdateScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(firstUpdateHeight, pubKeyBytes)) + firstUpdateScheduled, err := s.keeper.HasFeeUpdate(s.ctx, firstUpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().True(firstUpdateScheduled) @@ -393,15 +400,15 @@ func (s *KeeperTestSuite) TestMsgServer_EditDataProxy() { s.Require().NoError(err) s.Require().NotNil(secondRes) - secondProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, secondMsg.PubKey) + secondProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().NotNil(secondProxyConfig.FeeUpdate) - secondUpdateScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(secondUpdateHeight, pubKeyBytes)) + secondUpdateScheduled, err := s.keeper.HasFeeUpdate(s.ctx, secondUpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().True(secondUpdateScheduled) - firstUpdateNoLongerScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(firstUpdateHeight, pubKeyBytes)) + firstUpdateNoLongerScheduled, err := s.keeper.HasFeeUpdate(s.ctx, firstUpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().False(firstUpdateNoLongerScheduled) }) @@ -409,7 +416,7 @@ func (s *KeeperTestSuite) TestMsgServer_EditDataProxy() { s.Run("Transferring admin address should allow the new address to submit changes", func() { s.SetupTest() - err = s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, initialProxyConfig) + err = s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, initialProxyConfig) s.Require().NoError(err) editMsg := &types.MsgEditDataProxy{ @@ -493,7 +500,7 @@ func (s *KeeperTestSuite) TestMsgServer_UpdateParams() { s.SetupTest() // Register data proxy - err = s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, initialProxyConfig) + err = s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, initialProxyConfig) s.Require().NoError(err) // Edit data proxy fee and verify it is scheduled with the default delay @@ -509,11 +516,11 @@ func (s *KeeperTestSuite) TestMsgServer_UpdateParams() { s.Require().NoError(err) s.Require().NotNil(firstEditRes) - firstProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, firstEditMsg.PubKey) + firstProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().NotNil(firstProxyConfig.FeeUpdate) - firstUpdateScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(firstEditRes.FeeUpdateHeight, pubKeyBytes)) + firstUpdateScheduled, err := s.keeper.HasFeeUpdate(s.ctx, firstEditRes.FeeUpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().True(firstUpdateScheduled) @@ -527,7 +534,7 @@ func (s *KeeperTestSuite) TestMsgServer_UpdateParams() { s.Require().NoError(err) // Verify update is still pending at the original height - firstUpdateStillScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(firstEditRes.FeeUpdateHeight, pubKeyBytes)) + firstUpdateStillScheduled, err := s.keeper.HasFeeUpdate(s.ctx, firstEditRes.FeeUpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().True(firstUpdateStillScheduled) @@ -544,16 +551,16 @@ func (s *KeeperTestSuite) TestMsgServer_UpdateParams() { s.Require().NoError(err) s.Require().NotNil(secondEditRes) - secondProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, secondEditMsg.PubKey) + secondProxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes) s.Require().NoError(err) s.Require().NotNil(secondProxyConfig.FeeUpdate) // Verify the new update is scheduled and the old one is cancelled - secondUpdateScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(secondEditRes.FeeUpdateHeight, pubKeyBytes)) + secondUpdateScheduled, err := s.keeper.HasFeeUpdate(s.ctx, secondEditRes.FeeUpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().True(secondUpdateScheduled) - firstUpdateNoLongerScheduled, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(firstEditRes.FeeUpdateHeight, pubKeyBytes)) + firstUpdateNoLongerScheduled, err := s.keeper.HasFeeUpdate(s.ctx, firstEditRes.FeeUpdateHeight, pubKeyBytes) s.Require().NoError(err) s.Require().False(firstUpdateNoLongerScheduled) }) diff --git a/x/data-proxy/keeper/params.go b/x/data-proxy/keeper/params.go new file mode 100644 index 00000000..afec57a4 --- /dev/null +++ b/x/data-proxy/keeper/params.go @@ -0,0 +1,16 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/sedaprotocol/seda-chain/x/data-proxy/types" +) + +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { + return k.params.Set(ctx, params) +} + +func (k Keeper) GetMinimumUpdateDelay(ctx sdk.Context) (uint32, error) { + params, err := k.params.Get(ctx) + return params.MinFeeUpdateDelay, err +}