From 4734708d51e0581902f842c82b908e18299d3585 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 13 Jun 2024 13:48:50 +0200 Subject: [PATCH] updates --- go.mod | 12 +-- x/claim/exported/exported.go | 19 +++++ x/claim/genesis_test.go | 7 +- x/claim/keeper/airdrop_supply.go | 4 +- x/claim/keeper/airdrop_supply_test.go | 35 +++++---- x/claim/keeper/claim_record_test.go | 21 +++-- x/claim/keeper/grpc_airdrop_supply_test.go | 12 ++- x/claim/keeper/grpc_claim_record_test.go | 23 +++--- x/claim/keeper/grpc_initial_claim_test.go | 7 +- x/claim/keeper/grpc_mission_test.go | 19 +++-- x/claim/keeper/grpc_params_test.go | 7 +- x/claim/keeper/initial_claim_test.go | 15 ++-- x/claim/keeper/invariants_test.go | 89 +++++++++++----------- x/claim/keeper/keeper.go | 31 +++----- x/claim/keeper/keeper_test.go | 26 ++++++- x/claim/keeper/migrations.go | 24 ++++++ x/claim/keeper/mission.go | 8 +- x/claim/keeper/mission_test.go | 73 ++++++++++-------- x/claim/keeper/msg_server_claim.go | 6 +- x/claim/keeper/msg_server_claim_test.go | 42 ++++++---- x/claim/keeper/msg_test.go | 16 ---- x/claim/keeper/params.go | 31 +++----- x/claim/keeper/params_test.go | 9 +-- x/claim/migrations/v2/store.go | 10 +++ x/claim/module.go | 19 ++++- x/claim/simulation/simulation.go | 10 +-- x/claim/types/keys.go | 13 +--- x/claim/types/message_claim.go | 18 ----- x/claim/types/mission_test.go | 12 ++- 29 files changed, 327 insertions(+), 291 deletions(-) create mode 100644 x/claim/exported/exported.go create mode 100644 x/claim/keeper/migrations.go delete mode 100644 x/claim/keeper/msg_test.go create mode 100644 x/claim/migrations/v2/store.go diff --git a/go.mod b/go.mod index 7396448..1ff4b88 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,9 @@ require ( cosmossdk.io/client/v2 v2.0.0-beta.1 cosmossdk.io/core v0.11.0 cosmossdk.io/depinject v1.0.0-alpha.4 + cosmossdk.io/errors v1.0.1 cosmossdk.io/log v1.3.1 + cosmossdk.io/math v1.3.0 cosmossdk.io/store v1.1.0 cosmossdk.io/tools/confix v0.1.1 cosmossdk.io/x/feegrant v0.1.0 @@ -25,6 +27,7 @@ require ( github.com/cosmos/cosmos-proto v1.0.0-beta.5 github.com/cosmos/cosmos-sdk v0.50.7 github.com/cosmos/gogoproto v1.4.12 + github.com/golang/protobuf v1.5.4 github.com/gorilla/mux v1.8.1 github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 @@ -33,8 +36,11 @@ require ( github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 golang.org/x/tools v0.20.0 + google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa + google.golang.org/grpc v1.63.2 google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 google.golang.org/protobuf v1.34.1 + gopkg.in/yaml.v2 v2.4.0 ) require ( @@ -42,8 +48,6 @@ require ( connectrpc.com/connect v1.16.0 // indirect connectrpc.com/otelconnect v0.7.0 // indirect cosmossdk.io/collections v0.4.0 // indirect - cosmossdk.io/errors v1.0.1 // indirect - cosmossdk.io/math v1.3.0 // indirect cosmossdk.io/x/tx v0.13.3 // indirect filippo.io/edwards25519 v1.0.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect @@ -114,7 +118,6 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/glog v1.2.0 // indirect github.com/golang/mock v1.6.0 // indirect - github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/btree v1.1.2 // indirect github.com/google/cel-go v0.20.1 // indirect @@ -215,11 +218,8 @@ require ( golang.org/x/term v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect - google.golang.org/grpc v1.63.2 // indirect gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect gotest.tools/v3 v3.5.1 // indirect nhooyr.io/websocket v1.8.6 // indirect diff --git a/x/claim/exported/exported.go b/x/claim/exported/exported.go new file mode 100644 index 0000000..552b1a3 --- /dev/null +++ b/x/claim/exported/exported.go @@ -0,0 +1,19 @@ +package exported + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" +) + +type ( + ParamSet = paramtypes.ParamSet + + // Subspace defines an interface that implements the legacy x/params Subspace + // type. + // + // NOTE: This is used solely for migration of x/params managed parameters. + Subspace interface { + GetParamSet(ctx sdk.Context, ps ParamSet) + Get(ctx sdk.Context, key []byte, ptr any) + } +) diff --git a/x/claim/genesis_test.go b/x/claim/genesis_test.go index a93ed97..e86f5df 100644 --- a/x/claim/genesis_test.go +++ b/x/claim/genesis_test.go @@ -7,7 +7,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/testutil/sample" "github.com/ignite/modules/x/claim" @@ -42,11 +41,11 @@ func TestGenesis(t *testing.T) { // this line is used by starport scaffolding # genesis/test/state } - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow import and export of genesis", func(t *testing.T) { - claim.InitGenesis(ctx, *tk.ClaimKeeper, genesisState) - got := claim.ExportGenesis(ctx, *tk.ClaimKeeper) + claim.InitGenesis(ctx, *tk, genesisState) + got := claim.ExportGenesis(ctx, *tk) require.NotNil(t, got) nullify.Fill(&genesisState) diff --git a/x/claim/keeper/airdrop_supply.go b/x/claim/keeper/airdrop_supply.go index 04a57f5..c455d35 100644 --- a/x/claim/keeper/airdrop_supply.go +++ b/x/claim/keeper/airdrop_supply.go @@ -70,8 +70,8 @@ func (k Keeper) EndAirdrop(goCtx context.Context) error { return nil } - decayInfo := k.DecayInformation(ctx) - if decayInfo.Enabled && ctx.BlockTime().After(decayInfo.DecayEnd) { + params := k.GetParams(ctx) + if params.DecayInformation.Enabled && ctx.BlockTime().After(params.DecayInformation.DecayEnd) { err := k.distrKeeper.FundCommunityPool( ctx, sdk.NewCoins(airdropSupply), diff --git a/x/claim/keeper/airdrop_supply_test.go b/x/claim/keeper/airdrop_supply_test.go index cf3bc4f..6068835 100644 --- a/x/claim/keeper/airdrop_supply_test.go +++ b/x/claim/keeper/airdrop_supply_test.go @@ -8,19 +8,18 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" claim "github.com/ignite/modules/x/claim/types" ) func TestAirdropSupplyGet(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow get", func(t *testing.T) { sampleSupply := sdk.NewCoin("foo", sdkmath.NewInt(1000)) - tk.ClaimKeeper.SetAirdropSupply(ctx, sampleSupply) + tk.SetAirdropSupply(ctx, sampleSupply) - rst, found := tk.ClaimKeeper.GetAirdropSupply(ctx) + rst, found := tk.GetAirdropSupply(ctx) require.True(t, found) require.Equal(t, nullify.Fill(&sampleSupply), @@ -30,14 +29,14 @@ func TestAirdropSupplyGet(t *testing.T) { } func TestAirdropSupplyRemove(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow remove", func(t *testing.T) { - tk.ClaimKeeper.SetAirdropSupply(ctx, sdk.NewCoin("foo", sdkmath.NewInt(1000))) - _, found := tk.ClaimKeeper.GetAirdropSupply(ctx) + tk.SetAirdropSupply(ctx, sdk.NewCoin("foo", sdkmath.NewInt(1000))) + _, found := tk.GetAirdropSupply(ctx) require.True(t, found) - tk.ClaimKeeper.RemoveAirdropSupply(ctx) - _, found = tk.ClaimKeeper.GetAirdropSupply(ctx) + tk.RemoveAirdropSupply(ctx) + _, found = tk.GetAirdropSupply(ctx) require.False(t, found) }) } @@ -45,7 +44,7 @@ func TestAirdropSupplyRemove(t *testing.T) { func TestKeeper_InitializeAirdropSupply(t *testing.T) { // TODO: use mock for bank module to test critical errors // https://github.com/ignite/modules/issues/13 - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) tests := []struct { name string @@ -70,10 +69,10 @@ func TestKeeper_InitializeAirdropSupply(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := tk.ClaimKeeper.InitializeAirdropSupply(ctx, tt.airdropSupply) + err := tk.InitializeAirdropSupply(ctx, tt.airdropSupply) require.NoError(t, err) - airdropSupply, found := tk.ClaimKeeper.GetAirdropSupply(ctx) + airdropSupply, found := tk.GetAirdropSupply(ctx) require.True(t, found) require.True(t, airdropSupply.IsEqual(tt.airdropSupply)) @@ -88,7 +87,7 @@ func TestKeeper_InitializeAirdropSupply(t *testing.T) { } func TestEndAirdrop(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) tests := []struct { name string @@ -131,14 +130,14 @@ func TestEndAirdrop(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := tk.ClaimKeeper.InitializeAirdropSupply(ctx, tt.airdropSupply) + err := tk.InitializeAirdropSupply(ctx, tt.airdropSupply) require.NoError(t, err) - params := tk.ClaimKeeper.GetParams(ctx) + params := tk.GetParams(ctx) params.DecayInformation = tt.decayInfo - tk.ClaimKeeper.SetParams(ctx, params) + tk.SetParams(ctx, params) - err = tk.ClaimKeeper.EndAirdrop(ctx) + err = tk.EndAirdrop(ctx) require.NoError(t, err) if tt.wantDistribute { feePool := tk.DistrKeeper.GetFeePool(ctx) @@ -148,7 +147,7 @@ func TestEndAirdrop(t *testing.T) { } } - airdropSupply, found := tk.ClaimKeeper.GetAirdropSupply(ctx) + airdropSupply, found := tk.GetAirdropSupply(ctx) require.True(t, found) require.Equal(t, tt.expectedSupply, airdropSupply) }) diff --git a/x/claim/keeper/claim_record_test.go b/x/claim/keeper/claim_record_test.go index c6b33fa..a63c3c4 100644 --- a/x/claim/keeper/claim_record_test.go +++ b/x/claim/keeper/claim_record_test.go @@ -7,7 +7,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/testutil/sample" "github.com/ignite/modules/x/claim/keeper" @@ -26,12 +25,12 @@ func createNClaimRecord(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.C } func TestClaimRecordGet(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow get", func(t *testing.T) { - items := createNClaimRecord(tk.ClaimKeeper, ctx, 10) + items := createNClaimRecord(tk, ctx, 10) for _, item := range items { - rst, found := tk.ClaimKeeper.GetClaimRecord(ctx, + rst, found := tk.GetClaimRecord(ctx, item.Address, ) require.True(t, found) @@ -44,15 +43,15 @@ func TestClaimRecordGet(t *testing.T) { } func TestClaimRecordRemove(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow remove", func(t *testing.T) { - items := createNClaimRecord(tk.ClaimKeeper, ctx, 10) + items := createNClaimRecord(tk, ctx, 10) for _, item := range items { - tk.ClaimKeeper.RemoveClaimRecord(ctx, + tk.RemoveClaimRecord(ctx, item.Address, ) - _, found := tk.ClaimKeeper.GetClaimRecord(ctx, + _, found := tk.GetClaimRecord(ctx, item.Address, ) require.False(t, found) @@ -61,13 +60,13 @@ func TestClaimRecordRemove(t *testing.T) { } func TestClaimRecordGetAll(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow get all", func(t *testing.T) { - items := createNClaimRecord(tk.ClaimKeeper, ctx, 10) + items := createNClaimRecord(tk, ctx, 10) require.ElementsMatch(t, nullify.Fill(items), - nullify.Fill(tk.ClaimKeeper.GetAllClaimRecord(ctx)), + nullify.Fill(tk.GetAllClaimRecord(ctx)), ) }) } diff --git a/x/claim/keeper/grpc_airdrop_supply_test.go b/x/claim/keeper/grpc_airdrop_supply_test.go index 6412a06..8950dd9 100644 --- a/x/claim/keeper/grpc_airdrop_supply_test.go +++ b/x/claim/keeper/grpc_airdrop_supply_test.go @@ -9,17 +9,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/x/claim/types" ) func TestAirdropSupplyQuery(t *testing.T) { - var ( - ctx, tk, _ = testkeeper.NewTestSetup(t) - sampleSupply = sdk.NewCoin("foo", sdkmath.NewInt(1000)) - ) - tk.ClaimKeeper.SetAirdropSupply(ctx, sampleSupply) + ctx, tk := createClaimKeeper(t) + sampleSupply := sdk.NewCoin("foo", sdkmath.NewInt(1000)) + + tk.SetAirdropSupply(ctx, sampleSupply) for _, tc := range []struct { name string @@ -38,7 +36,7 @@ func TestAirdropSupplyQuery(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - response, err := tk.ClaimKeeper.AirdropSupply(ctx, tc.request) + response, err := tk.AirdropSupply(ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { diff --git a/x/claim/keeper/grpc_claim_record_test.go b/x/claim/keeper/grpc_claim_record_test.go index 4faa08e..80fa8f5 100644 --- a/x/claim/keeper/grpc_claim_record_test.go +++ b/x/claim/keeper/grpc_claim_record_test.go @@ -9,16 +9,13 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/x/claim/types" ) func TestClaimRecordQuerySingle(t *testing.T) { - var ( - ctx, tk, _ = testkeeper.NewTestSetup(t) - msgs = createNClaimRecord(tk.ClaimKeeper, ctx, 2) - ) + ctx, tk := createClaimKeeper(t) + msgs := createNClaimRecord(tk, ctx, 2) for _, tc := range []struct { name string @@ -53,7 +50,7 @@ func TestClaimRecordQuerySingle(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - response, err := tk.ClaimKeeper.ClaimRecord(ctx, tc.request) + response, err := tk.ClaimRecord(ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { @@ -68,10 +65,8 @@ func TestClaimRecordQuerySingle(t *testing.T) { } func TestClaimRecordQueryPaginated(t *testing.T) { - var ( - ctx, tk, _ = testkeeper.NewTestSetup(t) - msgs = createNClaimRecord(tk.ClaimKeeper, ctx, 5) - ) + ctx, tk := createClaimKeeper(t) + msgs := createNClaimRecord(tk, ctx, 5) request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllClaimRecordRequest { return &types.QueryAllClaimRecordRequest{ @@ -86,7 +81,7 @@ func TestClaimRecordQueryPaginated(t *testing.T) { t.Run("should paginate by offset", func(t *testing.T) { step := 2 for i := 0; i < len(msgs); i += step { - resp, err := tk.ClaimKeeper.ClaimRecordAll(ctx, request(nil, uint64(i), uint64(step), false)) + resp, err := tk.ClaimRecordAll(ctx, request(nil, uint64(i), uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.ClaimRecord), step) require.Subset(t, @@ -99,7 +94,7 @@ func TestClaimRecordQueryPaginated(t *testing.T) { step := 2 var next []byte for i := 0; i < len(msgs); i += step { - resp, err := tk.ClaimKeeper.ClaimRecordAll(ctx, request(next, 0, uint64(step), false)) + resp, err := tk.ClaimRecordAll(ctx, request(next, 0, uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.ClaimRecord), step) require.Subset(t, @@ -110,7 +105,7 @@ func TestClaimRecordQueryPaginated(t *testing.T) { } }) t.Run("should paginate all", func(t *testing.T) { - resp, err := tk.ClaimKeeper.ClaimRecordAll(ctx, request(nil, 0, 0, true)) + resp, err := tk.ClaimRecordAll(ctx, request(nil, 0, 0, true)) require.NoError(t, err) require.Equal(t, len(msgs), int(resp.Pagination.Total)) require.ElementsMatch(t, @@ -119,7 +114,7 @@ func TestClaimRecordQueryPaginated(t *testing.T) { ) }) t.Run("should return InvalidRequest", func(t *testing.T) { - _, err := tk.ClaimKeeper.ClaimRecordAll(ctx, nil) + _, err := tk.ClaimRecordAll(ctx, nil) require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request")) }) } diff --git a/x/claim/keeper/grpc_initial_claim_test.go b/x/claim/keeper/grpc_initial_claim_test.go index 175104f..e5ceb47 100644 --- a/x/claim/keeper/grpc_initial_claim_test.go +++ b/x/claim/keeper/grpc_initial_claim_test.go @@ -7,14 +7,13 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/x/claim/types" ) func TestInitialClaimQuery(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) - item := createTestInitialClaim(tk.ClaimKeeper, ctx) + ctx, tk := createClaimKeeper(t) + item := createTestInitialClaim(tk, ctx) for _, tc := range []struct { desc string request *types.QueryGetInitialClaimRequest @@ -32,7 +31,7 @@ func TestInitialClaimQuery(t *testing.T) { }, } { t.Run(tc.desc, func(t *testing.T) { - response, err := tk.ClaimKeeper.InitialClaim(ctx, tc.request) + response, err := tk.InitialClaim(ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { diff --git a/x/claim/keeper/grpc_mission_test.go b/x/claim/keeper/grpc_mission_test.go index ade81b2..8a0e43a 100644 --- a/x/claim/keeper/grpc_mission_test.go +++ b/x/claim/keeper/grpc_mission_test.go @@ -9,15 +9,14 @@ import ( "google.golang.org/grpc/status" "github.com/ignite/modules/pkg/errors" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/x/claim/types" ) func TestMissionQuerySingle(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - msgs := createNMission(tk.ClaimKeeper, ctx, 2) + msgs := createNMission(tk, ctx, 2) for _, tc := range []struct { name string request *types.QueryGetMissionRequest @@ -45,7 +44,7 @@ func TestMissionQuerySingle(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - response, err := tk.ClaimKeeper.Mission(ctx, tc.request) + response, err := tk.Mission(ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { @@ -60,9 +59,9 @@ func TestMissionQuerySingle(t *testing.T) { } func TestMissionQueryPaginated(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - msgs := createNMission(tk.ClaimKeeper, ctx, 5) + msgs := createNMission(tk, ctx, 5) request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllMissionRequest { return &types.QueryAllMissionRequest{ @@ -77,7 +76,7 @@ func TestMissionQueryPaginated(t *testing.T) { t.Run("should paginate by offset", func(t *testing.T) { step := 2 for i := 0; i < len(msgs); i += step { - resp, err := tk.ClaimKeeper.MissionAll(ctx, request(nil, uint64(i), uint64(step), false)) + resp, err := tk.MissionAll(ctx, request(nil, uint64(i), uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.Mission), step) require.Subset(t, @@ -90,7 +89,7 @@ func TestMissionQueryPaginated(t *testing.T) { step := 2 var next []byte for i := 0; i < len(msgs); i += step { - resp, err := tk.ClaimKeeper.MissionAll(ctx, request(next, 0, uint64(step), false)) + resp, err := tk.MissionAll(ctx, request(next, 0, uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.Mission), step) require.Subset(t, @@ -101,7 +100,7 @@ func TestMissionQueryPaginated(t *testing.T) { } }) t.Run("should paginate all", func(t *testing.T) { - resp, err := tk.ClaimKeeper.MissionAll(ctx, request(nil, 0, 0, true)) + resp, err := tk.MissionAll(ctx, request(nil, 0, 0, true)) require.NoError(t, err) require.Equal(t, len(msgs), int(resp.Pagination.Total)) require.ElementsMatch(t, @@ -110,7 +109,7 @@ func TestMissionQueryPaginated(t *testing.T) { ) }) t.Run("should return InvalidRequest", func(t *testing.T) { - _, err := tk.ClaimKeeper.MissionAll(ctx, nil) + _, err := tk.MissionAll(ctx, nil) require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request")) }) } diff --git a/x/claim/keeper/grpc_params_test.go b/x/claim/keeper/grpc_params_test.go index 9ec6526..e3584a7 100644 --- a/x/claim/keeper/grpc_params_test.go +++ b/x/claim/keeper/grpc_params_test.go @@ -5,18 +5,17 @@ import ( "github.com/stretchr/testify/require" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/x/claim/types" ) func TestParamsQuery(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow params get query", func(t *testing.T) { params := types.DefaultParams() - tk.ClaimKeeper.SetParams(ctx, params) + tk.SetParams(ctx, params) - response, err := tk.ClaimKeeper.Params(ctx, &types.QueryParamsRequest{}) + response, err := tk.Params(ctx, &types.QueryParamsRequest{}) require.NoError(t, err) require.EqualValues(t, params.DecayInformation, response.Params.DecayInformation) require.Equal(t, params.AirdropStart.Unix(), response.Params.AirdropStart.Unix()) diff --git a/x/claim/keeper/initial_claim_test.go b/x/claim/keeper/initial_claim_test.go index 8fad333..4fbcd0a 100644 --- a/x/claim/keeper/initial_claim_test.go +++ b/x/claim/keeper/initial_claim_test.go @@ -6,7 +6,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/x/claim/keeper" "github.com/ignite/modules/x/claim/types" @@ -19,11 +18,11 @@ func createTestInitialClaim(keeper *keeper.Keeper, ctx sdk.Context) types.Initia } func TestInitialClaimGet(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow get", func(t *testing.T) { - item := createTestInitialClaim(tk.ClaimKeeper, ctx) - rst, found := tk.ClaimKeeper.GetInitialClaim(ctx) + item := createTestInitialClaim(tk, ctx) + rst, found := tk.GetInitialClaim(ctx) require.True(t, found) require.Equal(t, nullify.Fill(&item), @@ -33,12 +32,12 @@ func TestInitialClaimGet(t *testing.T) { } func TestInitialClaimRemove(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow remove", func(t *testing.T) { - createTestInitialClaim(tk.ClaimKeeper, ctx) - tk.ClaimKeeper.RemoveInitialClaim(ctx) - _, found := tk.ClaimKeeper.GetInitialClaim(ctx) + createTestInitialClaim(tk, ctx) + tk.RemoveInitialClaim(ctx) + _, found := tk.GetInitialClaim(ctx) require.False(t, found) }) } diff --git a/x/claim/keeper/invariants_test.go b/x/claim/keeper/invariants_test.go index da33915..f5d5a6b 100644 --- a/x/claim/keeper/invariants_test.go +++ b/x/claim/keeper/invariants_test.go @@ -7,7 +7,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/sample" "github.com/ignite/modules/x/claim/keeper" "github.com/ignite/modules/x/claim/types" @@ -15,186 +14,186 @@ import ( func TestClaimRecordInvariant(t *testing.T) { t.Run("should not break with a completed and claimed mission", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 10, Description: "test mission", Weight: sdkmath.LegacyNewDec(100), }) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: []uint64{10}, ClaimedMissions: []uint64{10}, }) - msg, broken := keeper.ClaimRecordInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.ClaimRecordInvariant(*tk)(ctx) require.False(t, broken, msg) }) t.Run("should not break with a completed but not claimed mission", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 10, Description: "test mission", Weight: sdkmath.LegacyNewDec(100), }) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: []uint64{10}, }) - msg, broken := keeper.ClaimRecordInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.ClaimRecordInvariant(*tk)(ctx) require.False(t, broken, msg) }) t.Run("should break with claimed but not completed mission", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: []uint64{}, ClaimedMissions: []uint64{10}, }) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 10, Description: "test mission", Weight: sdkmath.LegacyNewDec(100), }) - msg, broken := keeper.ClaimRecordInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.ClaimRecordInvariant(*tk)(ctx) require.True(t, broken, msg) }) } func TestClaimRecordMissionInvariant(t *testing.T) { t.Run("should not break with valid state", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: []uint64{0, 1}, }) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 0, Description: "mission 0", Weight: sdkmath.LegacyZeroDec(), }) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 1, Description: "mission 1", Weight: sdkmath.LegacyZeroDec(), }) - msg, broken := keeper.ClaimRecordMissionInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.ClaimRecordMissionInvariant(*tk)(ctx) require.False(t, broken, msg) }) t.Run("should break with invalid state", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: []uint64{0, 1}, }) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 1, Description: "mission 1", Weight: sdkmath.LegacyZeroDec(), }) - msg, broken := keeper.ClaimRecordMissionInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.ClaimRecordMissionInvariant(*tk)(ctx) require.True(t, broken, msg) }) } func TestAirdropSupplyInvariant(t *testing.T) { t.Run("should not break with valid state", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: nil, }) - msg, broken := keeper.AirdropSupplyInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.AirdropSupplyInvariant(*tk)(ctx) require.False(t, broken, msg) }) t.Run("should not break with valid state and completed missions", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: []uint64{0, 1}, }) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 0, Description: "", Weight: sdkmath.LegacyZeroDec(), }) - tk.ClaimKeeper.SetMission(ctx, types.Mission{ + tk.SetMission(ctx, types.Mission{ MissionID: 1, Description: "", Weight: sdkmath.LegacyZeroDec(), }) - msg, broken := keeper.AirdropSupplyInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.AirdropSupplyInvariant(*tk)(ctx) require.False(t, broken, msg) }) t.Run("should break with duplicated address in claim record", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) + tk.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) addr := sample.AccAddress() - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: addr, Claimable: sdkmath.NewInt(5), CompletedMissions: nil, }) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: addr, Claimable: sdkmath.NewInt(5), CompletedMissions: nil, }) - msg, broken := keeper.AirdropSupplyInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.AirdropSupplyInvariant(*tk)(ctx) require.True(t, broken, msg) }) t.Run("should break with address completing non existing mission", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(10), CompletedMissions: []uint64{0, 1, 2}, }) - msg, broken := keeper.AirdropSupplyInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.AirdropSupplyInvariant(*tk)(ctx) require.True(t, broken, msg) }) t.Run("should break with airdrop supply not equal to claimable amounts", func(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) - tk.ClaimKeeper.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) - tk.ClaimKeeper.SetClaimRecord(ctx, types.ClaimRecord{ + tk.InitializeAirdropSupply(ctx, sdk.NewCoin("test", sdkmath.NewInt(10))) + tk.SetClaimRecord(ctx, types.ClaimRecord{ Address: sample.AccAddress(), Claimable: sdkmath.NewInt(9), CompletedMissions: nil, }) - msg, broken := keeper.AirdropSupplyInvariant(*tk.ClaimKeeper)(ctx) + msg, broken := keeper.AirdropSupplyInvariant(*tk)(ctx) require.True(t, broken, msg) }) } diff --git a/x/claim/keeper/keeper.go b/x/claim/keeper/keeper.go index a0f215f..4d0bca9 100644 --- a/x/claim/keeper/keeper.go +++ b/x/claim/keeper/keeper.go @@ -8,45 +8,38 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/ignite/modules/x/claim/types" ) -type ( - Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey - memKey storetypes.StoreKey - paramstore paramtypes.Subspace - accountKeeper types.AccountKeeper - distrKeeper types.DistrKeeper - bankKeeper types.BankKeeper - } -) +type Keeper struct { + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + memKey storetypes.StoreKey + accountKeeper types.AccountKeeper + distrKeeper types.DistrKeeper + bankKeeper types.BankKeeper + + authority string +} func NewKeeper( cdc codec.BinaryCodec, storeKey, memKey storetypes.StoreKey, - ps paramtypes.Subspace, accountKeeper types.AccountKeeper, distrkeeper types.DistrKeeper, bankKeeper types.BankKeeper, + authority string, ) *Keeper { - // set KeyTable if it has not already been set - if !ps.HasKeyTable() { - ps = ps.WithKeyTable(types.ParamKeyTable()) - } - return &Keeper{ cdc: cdc, storeKey: storeKey, memKey: memKey, - paramstore: ps, accountKeeper: accountKeeper, distrKeeper: distrkeeper, bankKeeper: bankKeeper, + authority: authority, } } diff --git a/x/claim/keeper/keeper_test.go b/x/claim/keeper/keeper_test.go index 5e94698..bd04afa 100644 --- a/x/claim/keeper/keeper_test.go +++ b/x/claim/keeper/keeper_test.go @@ -1,6 +1,19 @@ package keeper_test -import "math/rand" +import ( + "math/rand" + "testing" + + storetypes "cosmossdk.io/store/types" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + + "github.com/ignite/modules/testutil/sample" + "github.com/ignite/modules/x/claim" + "github.com/ignite/modules/x/claim/keeper" + "github.com/ignite/modules/x/claim/types" +) var r *rand.Rand @@ -9,3 +22,14 @@ func init() { s := rand.NewSource(1) r = rand.New(s) } + +func createClaimKeeper(t *testing.T) (sdk.Context, *keeper.Keeper) { + t.Helper() + + encCfg := moduletestutil.MakeTestEncodingConfig(claim.AppModuleBasic{}) + key := storetypes.NewKVStoreKey(types.StoreKey) + memKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) + testCtx := testutil.DefaultContextWithKeys(map[string]*storetypes.KVStoreKey{types.ModuleName: key}, map[string]*storetypes.TransientStoreKey{types.ModuleName: storetypes.NewTransientStoreKey("transient_test")}, map[string]*storetypes.MemoryStoreKey{types.ModuleName: memKey}) + + return testCtx, keeper.NewKeeper(encCfg.Codec, key, memKey, nil, nil, nil, sample.AccAddress()) +} diff --git a/x/claim/keeper/migrations.go b/x/claim/keeper/migrations.go new file mode 100644 index 0000000..ed7fa00 --- /dev/null +++ b/x/claim/keeper/migrations.go @@ -0,0 +1,24 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/ignite/modules/x/claim/exported" + v2 "github.com/ignite/modules/x/claim/migrations/v2" +) + +// Migrator is a struct for handling in-place store migrations. +type Migrator struct { + keeper Keeper + legacySubspace exported.Subspace +} + +// NewMigrator returns a new Migrator. +func NewMigrator(keeper Keeper, legacySubspace exported.Subspace) Migrator { + return Migrator{keeper: keeper, legacySubspace: legacySubspace} +} + +// Migrate1to2 migrates from version 2 to 3. +func (m Migrator) Migrate2to3(ctx sdk.Context) error { + return v2.MigrateStore(ctx) +} diff --git a/x/claim/keeper/mission.go b/x/claim/keeper/mission.go index 757bbf1..ee3c6b5 100644 --- a/x/claim/keeper/mission.go +++ b/x/claim/keeper/mission.go @@ -95,8 +95,8 @@ func (k Keeper) CompleteMission( } // try to claim the mission if airdrop start is reached - airdropStart := k.AirdropStart(ctx) - if ctx.BlockTime().After(airdropStart) { + params := k.GetParams(ctx) + if ctx.BlockTime().After(params.AirdropStart) { return k.ClaimMission(ctx, claimRecord, missionID) } @@ -145,8 +145,8 @@ func (k Keeper) ClaimMission( claimable := sdk.NewCoins(sdk.NewCoin(airdropSupply.Denom, claimableAmount)) // calculate claimable after decay factor - decayInfo := k.DecayInformation(ctx) - claimable = decayInfo.ApplyDecayFactor(claimable, ctx.BlockTime()) + params := k.GetParams(ctx) + claimable = params.DecayInformation.ApplyDecayFactor(claimable, ctx.BlockTime()) // check final claimable non-zero if claimable.Empty() { diff --git a/x/claim/keeper/mission_test.go b/x/claim/keeper/mission_test.go index 5a9697f..d7faedd 100644 --- a/x/claim/keeper/mission_test.go +++ b/x/claim/keeper/mission_test.go @@ -9,7 +9,6 @@ import ( "github.com/stretchr/testify/require" errorsignite "github.com/ignite/modules/pkg/errors" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/nullify" "github.com/ignite/modules/testutil/sample" "github.com/ignite/modules/x/claim/keeper" @@ -27,12 +26,12 @@ func createNMission(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Missi } func TestMissionGet(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow get", func(t *testing.T) { - items := createNMission(tk.ClaimKeeper, ctx, 10) + items := createNMission(tk, ctx, 10) for _, item := range items { - got, found := tk.ClaimKeeper.GetMission(ctx, item.MissionID) + got, found := tk.GetMission(ctx, item.MissionID) require.True(t, found) require.Equal(t, nullify.Fill(&item), @@ -43,32 +42,32 @@ func TestMissionGet(t *testing.T) { } func TestMissionGetAll(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow get all", func(t *testing.T) { - items := createNMission(tk.ClaimKeeper, ctx, 10) + items := createNMission(tk, ctx, 10) require.ElementsMatch(t, nullify.Fill(items), - nullify.Fill(tk.ClaimKeeper.GetAllMission(ctx)), + nullify.Fill(tk.GetAllMission(ctx)), ) }) } func TestMissionRemove(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow remove", func(t *testing.T) { - items := createNMission(tk.ClaimKeeper, ctx, 10) + items := createNMission(tk, ctx, 10) for _, item := range items { - tk.ClaimKeeper.RemoveMission(ctx, item.MissionID) - _, found := tk.ClaimKeeper.GetMission(ctx, item.MissionID) + tk.RemoveMission(ctx, item.MissionID) + _, found := tk.GetMission(ctx, item.MissionID) require.False(t, found) } }) } func TestKeeper_ClaimMission(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) // prepare addresses addr := make([]string, 20) @@ -98,9 +97,17 @@ func TestKeeper_ClaimMission(t *testing.T) { name: "should fail if no airdrop supply", inputState: inputState{ noAirdropSupply: true, - claimRecord: sample.ClaimRecord(r), - mission: sample.Mission(r), - params: types.DefaultParams(), + claimRecord: types.ClaimRecord{ + Address: addr[0], + Claimable: sdkmath.NewInt(r.Int63n(100000)), + CompletedMissions: []uint64{1, 2, 3}, + }, + mission: types.Mission{ + MissionID: 1, + Description: "dummy mission", + Weight: sdkmath.LegacyNewDec(r.Int63n(1_000_000)).Quo(sdkmath.LegacyNewDec(1_000_000)), + }, + params: types.DefaultParams(), }, missionID: 1, address: sample.AccAddress(), @@ -386,22 +393,22 @@ func TestKeeper_ClaimMission(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // initialize input state require.NoError(t, tt.inputState.params.Validate()) - tk.ClaimKeeper.SetParams(ctx, tt.inputState.params) + tk.SetParams(ctx, tt.inputState.params) if !tt.inputState.noAirdropSupply { - err := tk.ClaimKeeper.InitializeAirdropSupply(ctx, tt.inputState.airdropSupply) + err := tk.InitializeAirdropSupply(ctx, tt.inputState.airdropSupply) require.NoError(t, err) } if !tt.inputState.noMission { - tk.ClaimKeeper.SetMission(ctx, tt.inputState.mission) + tk.SetMission(ctx, tt.inputState.mission) } if !tt.inputState.noClaimRecord { - tk.ClaimKeeper.SetClaimRecord(ctx, tt.inputState.claimRecord) + tk.SetClaimRecord(ctx, tt.inputState.claimRecord) } if !tt.inputState.blockTime.IsZero() { ctx = ctx.WithBlockTime(tt.inputState.blockTime) } - claimed, err := tk.ClaimKeeper.ClaimMission(ctx, tt.inputState.claimRecord, tt.missionID) + claimed, err := tk.ClaimMission(ctx, tt.inputState.claimRecord, tt.missionID) if tt.err != nil { require.ErrorIs(t, err, tt.err) } else { @@ -421,12 +428,12 @@ func TestKeeper_ClaimMission(t *testing.T) { ) // completed mission is added in claim record - claimRecord, found := tk.ClaimKeeper.GetClaimRecord(ctx, tt.address) + claimRecord, found := tk.GetClaimRecord(ctx, tt.address) require.True(t, found) require.True(t, claimRecord.IsMissionCompleted(tt.missionID)) // airdrop supply is updated with distributed balance - airdropSupply, found := tk.ClaimKeeper.GetAirdropSupply(ctx) + airdropSupply, found := tk.GetAirdropSupply(ctx) require.True(t, found) expectedAidropSupply := tt.inputState.airdropSupply.Sub(tt.expectedBalance) @@ -439,20 +446,20 @@ func TestKeeper_ClaimMission(t *testing.T) { // clear input state if !tt.inputState.noAirdropSupply { - tk.ClaimKeeper.RemoveAirdropSupply(ctx) + tk.RemoveAirdropSupply(ctx) } if !tt.inputState.noMission { - tk.ClaimKeeper.RemoveMission(ctx, tt.inputState.mission.MissionID) + tk.RemoveMission(ctx, tt.inputState.mission.MissionID) } if !tt.inputState.noClaimRecord { - tk.ClaimKeeper.RemoveClaimRecord(ctx, tt.inputState.claimRecord.Address) + tk.RemoveClaimRecord(ctx, tt.inputState.claimRecord.Address) } }) } } func TestKeeper_CompleteMission(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) addr := make([]string, 7) for i := 0; i < len(addr); i++ { @@ -518,7 +525,7 @@ func TestKeeper_CompleteMission(t *testing.T) { blockTime: time.Unix(0, 0), }, missionID: 1, - address: sample.Address(sample.Rand()), + address: sample.AccAddress(), err: types.ErrClaimRecordNotFound, }, { @@ -607,17 +614,17 @@ func TestKeeper_CompleteMission(t *testing.T) { t.Run(tt.name, func(t *testing.T) { require.NoError(t, tt.inputState.params.Validate()) if !tt.inputState.airdropSupply.IsNil() && !tt.inputState.airdropSupply.IsZero() { - err := tk.ClaimKeeper.InitializeAirdropSupply(ctx, tt.inputState.airdropSupply) + err := tk.InitializeAirdropSupply(ctx, tt.inputState.airdropSupply) require.NoError(t, err) } - tk.ClaimKeeper.SetParams(ctx, tt.inputState.params) - tk.ClaimKeeper.SetMission(ctx, tt.inputState.mission) - tk.ClaimKeeper.SetClaimRecord(ctx, tt.inputState.claimRecord) + tk.SetParams(ctx, tt.inputState.params) + tk.SetMission(ctx, tt.inputState.mission) + tk.SetClaimRecord(ctx, tt.inputState.claimRecord) if !tt.inputState.blockTime.IsZero() { ctx = ctx.WithBlockTime(tt.inputState.blockTime) } - claimed, err := tk.ClaimKeeper.CompleteMission(ctx, tt.missionID, tt.address) + claimed, err := tk.CompleteMission(ctx, tt.missionID, tt.address) if tt.err != nil { require.ErrorIs(t, err, tt.err) return @@ -625,7 +632,7 @@ func TestKeeper_CompleteMission(t *testing.T) { require.NoError(t, err) require.Equal(t, tt.expectedClaimed, claimed) - claimRecord, found := tk.ClaimKeeper.GetClaimRecord(ctx, tt.address) + claimRecord, found := tk.GetClaimRecord(ctx, tt.address) require.True(t, found) require.True(t, claimRecord.IsMissionCompleted(tt.missionID)) }) diff --git a/x/claim/keeper/msg_server_claim.go b/x/claim/keeper/msg_server_claim.go index 47e7547..3fa1334 100644 --- a/x/claim/keeper/msg_server_claim.go +++ b/x/claim/keeper/msg_server_claim.go @@ -37,12 +37,12 @@ func (k msgServer) Claim(goCtx context.Context, msg *types.MsgClaim) (*types.Msg } // check if airdrop start time already reached - airdropStart := k.AirdropStart(ctx) - if ctx.BlockTime().Before(airdropStart) { + params := k.GetParams(ctx) + if ctx.BlockTime().Before(params.AirdropStart) { return &types.MsgClaimResponse{}, errors.Wrapf( types.ErrAirdropStartNotReached, "airdrop start not reached: %s", - airdropStart.String(), + params.AirdropStart.String(), ) } claimed, err := k.ClaimMission(ctx, claimRecord, msg.MissionID) diff --git a/x/claim/keeper/msg_server_claim_test.go b/x/claim/keeper/msg_server_claim_test.go index 7f82499..445ad2f 100644 --- a/x/claim/keeper/msg_server_claim_test.go +++ b/x/claim/keeper/msg_server_claim_test.go @@ -9,13 +9,15 @@ import ( "github.com/stretchr/testify/require" errorsignite "github.com/ignite/modules/pkg/errors" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/testutil/sample" + "github.com/ignite/modules/x/claim/keeper" "github.com/ignite/modules/x/claim/types" ) func TestMsgClaim(t *testing.T) { - sdkCtx, tk, ts := testkeeper.NewTestSetup(t) + sdkCtx, tk := createClaimKeeper(t) + ts := keeper.NewMsgServerImpl(*tk) + // prepare addresses var addr []string for i := 0; i < 20; i++ { @@ -70,8 +72,12 @@ func TestMsgClaim(t *testing.T) { noInitialClaim: true, noClaimRecord: true, airdropSupply: sdk.NewCoin("foo", sdkmath.NewInt(1000)), - mission: sample.Mission(r), - params: types.DefaultParams(), + mission: types.Mission{ + MissionID: 1, + Description: "dummy mission", + Weight: sdkmath.LegacyNewDec(r.Int63n(1_000_000)).Quo(sdkmath.LegacyNewDec(1_000_000)), + }, + params: types.DefaultParams(), }, msg: types.MsgClaim{ Claimer: sample.AccAddress(), @@ -190,7 +196,11 @@ func TestMsgClaim(t *testing.T) { inputState: inputState{ noInitialClaim: true, airdropSupply: sdk.NewCoin("foo", sdkmath.NewInt(1000)), - mission: sample.Mission(r), + mission: types.Mission{ + MissionID: 1, + Description: "dummy mission", + Weight: sdkmath.LegacyNewDec(r.Int63n(1_000_000)).Quo(sdkmath.LegacyNewDec(1_000_000)), + }, claimRecord: types.ClaimRecord{ Address: addr[5], Claimable: sdkmath.NewIntFromUint64(1000), @@ -493,19 +503,19 @@ func TestMsgClaim(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // initialize input state require.NoError(t, tt.inputState.params.Validate()) - tk.ClaimKeeper.SetParams(sdkCtx, tt.inputState.params) + tk.SetParams(sdkCtx, tt.inputState.params) if !tt.inputState.noAirdropSupply { - err := tk.ClaimKeeper.InitializeAirdropSupply(sdkCtx, tt.inputState.airdropSupply) + err := tk.InitializeAirdropSupply(sdkCtx, tt.inputState.airdropSupply) require.NoError(t, err) } if !tt.inputState.noInitialClaim { - tk.ClaimKeeper.SetInitialClaim(sdkCtx, tt.inputState.initialClaim) + tk.SetInitialClaim(sdkCtx, tt.inputState.initialClaim) } if !tt.inputState.noMission { - tk.ClaimKeeper.SetMission(sdkCtx, tt.inputState.mission) + tk.SetMission(sdkCtx, tt.inputState.mission) } if !tt.inputState.noClaimRecord { - tk.ClaimKeeper.SetClaimRecord(sdkCtx, tt.inputState.claimRecord) + tk.SetClaimRecord(sdkCtx, tt.inputState.claimRecord) } if !tt.inputState.blockTime.IsZero() { sdkCtx = sdkCtx.WithBlockTime(tt.inputState.blockTime) @@ -531,12 +541,12 @@ func TestMsgClaim(t *testing.T) { ) // completed mission is added in claim record - claimRecord, found := tk.ClaimKeeper.GetClaimRecord(sdkCtx, tt.msg.Claimer) + claimRecord, found := tk.GetClaimRecord(sdkCtx, tt.msg.Claimer) require.True(t, found) require.True(t, claimRecord.IsMissionCompleted(tt.msg.MissionID)) // airdrop supply is updated with distributed balance - airdropSupply, found := tk.ClaimKeeper.GetAirdropSupply(sdkCtx) + airdropSupply, found := tk.GetAirdropSupply(sdkCtx) require.True(t, found) expectedAidropSupply := tt.inputState.airdropSupply.Sub(tt.expectedBalance) @@ -549,16 +559,16 @@ func TestMsgClaim(t *testing.T) { // clear input state if !tt.inputState.noAirdropSupply { - tk.ClaimKeeper.RemoveAirdropSupply(sdkCtx) + tk.RemoveAirdropSupply(sdkCtx) } if !tt.inputState.noMission { - tk.ClaimKeeper.RemoveMission(sdkCtx, tt.inputState.mission.MissionID) + tk.RemoveMission(sdkCtx, tt.inputState.mission.MissionID) } if !tt.inputState.noClaimRecord { - tk.ClaimKeeper.RemoveClaimRecord(sdkCtx, tt.inputState.claimRecord.Address) + tk.RemoveClaimRecord(sdkCtx, tt.inputState.claimRecord.Address) } if !tt.inputState.noInitialClaim { - tk.ClaimKeeper.RemoveInitialClaim(sdkCtx) + tk.RemoveInitialClaim(sdkCtx) } }) } diff --git a/x/claim/keeper/msg_test.go b/x/claim/keeper/msg_test.go deleted file mode 100644 index 3a6f632..0000000 --- a/x/claim/keeper/msg_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package keeper_test - -import ( - "context" - "testing" - - testkeeper "github.com/ignite/modules/testutil/keeper" - "github.com/ignite/modules/x/claim/keeper" - "github.com/ignite/modules/x/claim/types" -) - -func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) { - ctx, tk, _ := testkeeper.NewTestSetup(t) - - return keeper.NewMsgServerImpl(*tk.ClaimKeeper), ctx -} diff --git a/x/claim/keeper/params.go b/x/claim/keeper/params.go index cc23659..752703e 100644 --- a/x/claim/keeper/params.go +++ b/x/claim/keeper/params.go @@ -1,34 +1,25 @@ package keeper import ( - "time" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ignite/modules/x/claim/types" ) -// GetParams get all parameters as types.Params -func (k Keeper) GetParams(ctx sdk.Context) types.Params { - return types.NewParams( - k.DecayInformation(ctx), - k.AirdropStart(ctx), - ) -} - // SetParams set the params func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { - k.paramstore.SetParamSet(ctx, ¶ms) + store := ctx.KVStore(k.storeKey) + b := k.cdc.MustMarshal(¶ms) + store.Set(types.ParamsKey, b) } -// DecayInformation returns the param that defines decay information -func (k Keeper) DecayInformation(ctx sdk.Context) (totalSupplyRange types.DecayInformation) { - k.paramstore.Get(ctx, types.KeyDecayInformation, &totalSupplyRange) - return -} +// GetParams get all parameters as types.Params +func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { + store := ctx.KVStore(k.storeKey) + b := store.Get(types.ParamsKey) + if b == nil { + panic("stored mint params should not have been nil") + } -// AirdropStart returns the param that defines airdrop start -func (k Keeper) AirdropStart(ctx sdk.Context) (airdropStart time.Time) { - k.paramstore.Get(ctx, types.KeyAirdropStart, &airdropStart) + k.cdc.MustUnmarshal(b, ¶ms) return } diff --git a/x/claim/keeper/params_test.go b/x/claim/keeper/params_test.go index 77f1f80..7021d03 100644 --- a/x/claim/keeper/params_test.go +++ b/x/claim/keeper/params_test.go @@ -6,20 +6,19 @@ import ( "github.com/stretchr/testify/require" - testkeeper "github.com/ignite/modules/testutil/keeper" "github.com/ignite/modules/x/claim/types" ) func TestGetParams(t *testing.T) { - ctx, tk, _ := testkeeper.NewTestSetup(t) + ctx, tk := createClaimKeeper(t) t.Run("should allow params get", func(t *testing.T) { params := types.NewParams(types.NewEnabledDecay( time.Unix(1000, 0), time.Unix(10000, 0), ), time.Now()) - tk.ClaimKeeper.SetParams(ctx, params) - require.EqualValues(t, params.DecayInformation, tk.ClaimKeeper.GetParams(ctx).DecayInformation) - require.Equal(t, params.AirdropStart.Unix(), tk.ClaimKeeper.GetParams(ctx).AirdropStart.Unix()) + tk.SetParams(ctx, params) + require.EqualValues(t, params.DecayInformation, tk.GetParams(ctx).DecayInformation) + require.Equal(t, params.AirdropStart.Unix(), tk.GetParams(ctx).AirdropStart.Unix()) }) } diff --git a/x/claim/migrations/v2/store.go b/x/claim/migrations/v2/store.go new file mode 100644 index 0000000..136619f --- /dev/null +++ b/x/claim/migrations/v2/store.go @@ -0,0 +1,10 @@ +package v2 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func MigrateStore(ctx sdk.Context) error { + // todo migrations from params subspaces to modules params + return nil +} diff --git a/x/claim/module.go b/x/claim/module.go index 1b9e405..4b23404 100644 --- a/x/claim/module.go +++ b/x/claim/module.go @@ -13,6 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/ignite/modules/x/claim/exported" "github.com/ignite/modules/x/claim/keeper" "github.com/ignite/modules/x/claim/types" ) @@ -79,9 +80,10 @@ func (am AppModule) IsAppModule() {} type AppModule struct { AppModuleBasic - keeper keeper.Keeper - accountKeeper types.AccountKeeper - bankKeeper types.BankKeeper + keeper keeper.Keeper + accountKeeper types.AccountKeeper + bankKeeper types.BankKeeper + legacySubspace exported.Subspace } func NewAppModule( @@ -89,12 +91,14 @@ func NewAppModule( keeper keeper.Keeper, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, + legacySubspace exported.Subspace, ) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, accountKeeper: accountKeeper, bankKeeper: bankKeeper, + legacySubspace: legacySubspace, } } @@ -108,6 +112,13 @@ func (am AppModule) Name() string { func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterQueryServer(cfg.QueryServer(), am.keeper) types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) + + m := keeper.NewMigrator(am.keeper, am.legacySubspace) + // NOTE(@julienrbrt): It should looks the x/claim module has been introduced using a consensus version starting from 2. + // The Migrate1to2 function is not needed in this case. + if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { + panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) + } } // RegisterInvariants registers the claim module's invariants. @@ -134,7 +145,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw } // ConsensusVersion implements ConsensusVersion. -func (AppModule) ConsensusVersion() uint64 { return 2 } +func (AppModule) ConsensusVersion() uint64 { return 3 } // EndBlock executes all ABCI EndBlock logic respective to the claim module. func (am AppModule) EndBlock(ctx context.Context) error { diff --git a/x/claim/simulation/simulation.go b/x/claim/simulation/simulation.go index 3fab9f7..f8c94e2 100644 --- a/x/claim/simulation/simulation.go +++ b/x/claim/simulation/simulation.go @@ -30,7 +30,7 @@ func SimulateMsgClaim( // check the account has a claim record and initial claim has not been completed cr, found := k.GetClaimRecord(ctx, simAccount.Address.String()) if !found { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "account has no claim record"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgClaim{}), "account has no claim record"), nil, nil } var ( @@ -47,7 +47,7 @@ func SimulateMsgClaim( if !hasMission { return simtypes.NoOpMsg( types.ModuleName, - msg.Type(), + sdk.MsgTypeURL(&types.MsgClaim{}), fmt.Sprintf("%s don't have mission to claim", simAccount.Address.String()), ), nil, nil } @@ -57,12 +57,12 @@ func SimulateMsgClaim( claimableAmount := cr.ClaimableFromMission(mission) claimable := sdk.NewCoins(sdk.NewCoin(airdropSupply.Denom, claimableAmount)) // calculate claimable after decay factor - decayInfo := k.DecayInformation(ctx) - claimable = decayInfo.ApplyDecayFactor(claimable, ctx.BlockTime()) + params := k.GetParams(ctx) + claimable = params.DecayInformation.ApplyDecayFactor(claimable, ctx.BlockTime()) // check final claimable non-zero if claimable.Empty() { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), types.ErrNoClaimable.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgClaim{}), types.ErrNoClaimable.Error()), nil, nil } // initialize basic message diff --git a/x/claim/types/keys.go b/x/claim/types/keys.go index a55ef44..f3e0012 100644 --- a/x/claim/types/keys.go +++ b/x/claim/types/keys.go @@ -7,9 +7,6 @@ const ( // StoreKey defines the primary module store key StoreKey = ModuleName - // RouterKey is the message route for slashing - RouterKey = ModuleName - // MemStoreKey defines the in-memory store key MemStoreKey = "mem_claim" ) @@ -19,13 +16,9 @@ func KeyPrefix(p string) []byte { } const ( - MissionKey = "Mission-value-" -) - -const ( + MissionKey = "Mission-value-" AirdropSupplyKey = "AirdropSupply-value-" + InitialClaimKey = "InitialClaim-value-" ) -const ( - InitialClaimKey = "InitialClaim-value-" -) +var ParamsKey = []byte{0x02} diff --git a/x/claim/types/message_claim.go b/x/claim/types/message_claim.go index 349b1ba..dbf8014 100644 --- a/x/claim/types/message_claim.go +++ b/x/claim/types/message_claim.go @@ -6,8 +6,6 @@ import ( "github.com/ignite/modules/pkg/errors" ) -const TypeMsgClaim = "claim" - var _ sdk.Msg = &MsgClaim{} func NewMsgClaim(creator string, missionID uint64) *MsgClaim { @@ -17,22 +15,6 @@ func NewMsgClaim(creator string, missionID uint64) *MsgClaim { } } -func (msg *MsgClaim) Route() string { - return RouterKey -} - -func (msg *MsgClaim) Type() string { - return TypeMsgClaim -} - -func (msg *MsgClaim) GetSigners() []sdk.AccAddress { - creator, err := sdk.AccAddressFromBech32(msg.Claimer) - if err != nil { - panic(err) - } - return []sdk.AccAddress{creator} -} - func (msg *MsgClaim) GetSignBytes() []byte { bz := ModuleCdc.MustMarshalJSON(msg) return sdk.MustSortJSON(bz) diff --git a/x/claim/types/mission_test.go b/x/claim/types/mission_test.go index 2aa9ccf..7d18309 100644 --- a/x/claim/types/mission_test.go +++ b/x/claim/types/mission_test.go @@ -6,7 +6,7 @@ import ( sdkmath "cosmossdk.io/math" "github.com/stretchr/testify/require" - "github.com/ignite/modules/testutil/sample" + "github.com/ignite/modules/x/claim/types" claim "github.com/ignite/modules/x/claim/types" ) @@ -17,9 +17,13 @@ func TestMission_Validate(t *testing.T) { valid bool }{ { - name: "should validate valid mission", - mission: sample.Mission(r), - valid: true, + name: "should validate valid mission", + mission: types.Mission{ + MissionID: 1, + Description: "dummy mission", + Weight: sdkmath.LegacyNewDec(r.Int63n(1_000_000)).Quo(sdkmath.LegacyNewDec(1_000_000)), + }, + valid: true, }, { name: "should accept weigth 0",