From 2b806239a2e04f6a31c20b0c9d8dfaeec2179cc3 Mon Sep 17 00:00:00 2001 From: Rootul P Date: Mon, 29 Apr 2024 08:03:50 -0400 Subject: [PATCH] feat: delete subspace (#396) --- x/params/keeper/keeper.go | 11 +++++++++++ x/params/keeper/keeper_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/x/params/keeper/keeper.go b/x/params/keeper/keeper.go index 362231a56ad5..4922cefa77ac 100644 --- a/x/params/keeper/keeper.go +++ b/x/params/keeper/keeper.go @@ -1,6 +1,8 @@ package keeper import ( + "fmt" + "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -71,3 +73,12 @@ func (k Keeper) GetSubspaces() []types.Subspace { return spaces } + +func (k Keeper) DeleteSubspace(s string) error { + _, ok := k.spaces[s] + if !ok { + return fmt.Errorf("can not delete subspace %s because it does not exist", s) + } + delete(k.spaces, s) + return nil +} diff --git a/x/params/keeper/keeper_test.go b/x/params/keeper/keeper_test.go index e22cb8b198be..3b854b919e9d 100644 --- a/x/params/keeper/keeper_test.go +++ b/x/params/keeper/keeper_test.go @@ -5,6 +5,7 @@ import ( "testing" "cosmossdk.io/math" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -278,3 +279,21 @@ func TestJSONUpdate(t *testing.T) { space.Get(ctx, key, ¶m) require.Equal(t, paramJSON{40964096, "goodbyeworld"}, param) } + +func TestDeleteSubspace(t *testing.T) { + _, _, _, _, keeper := testComponents() + name := "test" + + t.Run("should return an error if DeleteSubspace is invoked on a non-existent subspace", func(t *testing.T) { + err := keeper.DeleteSubspace(name) + assert.Error(t, err) + }) + t.Run("should return no error if DeleteSubspace is invoked on a subspace", func(t *testing.T) { + keeper.Subspace(name) + err := keeper.DeleteSubspace(name) + assert.NoError(t, err) + _, exists := keeper.GetSubspace(name) + assert.False(t, exists) + }) + +}