From aabe51635fb510f14ac6b9bb6944080e2fceb5ef Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Tue, 21 Mar 2023 14:59:33 +0900 Subject: [PATCH] chore: clean up x/foundation api (#933) * Hide unnecessary apis * Use x/foundation mocks * Lint * Update CHANGELOG.md * Remove interface * Revert "Remove interface" This reverts commit 8dd124d5f0b10191a0495b895d6998bafb0652ee. --- CHANGELOG.md | 1 + Makefile | 1 + x/foundation/keeper/exported.go | 93 +++++++++++++++++++ x/foundation/keeper/{ => internal}/abci.go | 2 +- .../keeper/{ => internal}/abci_test.go | 24 ++--- .../keeper/{ => internal}/censorship.go | 2 +- .../keeper/{ => internal}/censorship_test.go | 28 +++--- x/foundation/keeper/{ => internal}/exec.go | 2 +- .../keeper/{ => internal}/exec_test.go | 4 +- x/foundation/keeper/{ => internal}/genesis.go | 2 +- .../keeper/{ => internal}/genesis_test.go | 2 +- .../keeper/{ => internal}/grpc_query.go | 2 +- .../keeper/{ => internal}/grpc_query_test.go | 47 ++++++---- .../keeper/{ => internal}/invariants.go | 2 +- .../keeper/{ => internal}/invariants_test.go | 22 ++--- x/foundation/keeper/{ => internal}/keeper.go | 2 +- .../keeper/{ => internal}/keeper_test.go | 50 ++++++---- x/foundation/keeper/{ => internal}/keys.go | 2 +- x/foundation/keeper/{ => internal}/member.go | 2 +- .../keeper/{ => internal}/member_test.go | 6 +- .../keeper/{ => internal}/migrations.go | 2 +- .../keeper/{ => internal}/msg_server.go | 2 +- .../keeper/{ => internal}/msg_server_test.go | 10 +- x/foundation/keeper/{ => internal}/params.go | 2 +- .../keeper/{ => internal}/params_test.go | 12 +-- .../keeper/{ => internal}/proposal.go | 2 +- .../keeper/{ => internal}/proposal_handler.go | 2 +- .../{ => internal}/proposal_handler_test.go | 16 ++-- .../keeper/{ => internal}/proposal_test.go | 31 +++++-- x/foundation/keeper/{ => internal}/tally.go | 2 +- .../keeper/{ => internal}/treasury.go | 2 +- .../keeper/{ => internal}/treasury_test.go | 14 +-- x/foundation/keeper/{ => internal}/vote.go | 2 +- .../keeper/{ => internal}/vote_test.go | 10 +- x/foundation/msgs_test.go | 4 +- x/stakingplus/keeper/keeper_test.go | 54 ++++++++--- .../testutil/expected_keepers_mocks.go | 49 ++++++++++ 37 files changed, 363 insertions(+), 149 deletions(-) create mode 100644 x/foundation/keeper/exported.go rename x/foundation/keeper/{ => internal}/abci.go (97%) rename x/foundation/keeper/{ => internal}/abci_test.go (80%) rename x/foundation/keeper/{ => internal}/censorship.go (99%) rename x/foundation/keeper/{ => internal}/censorship_test.go (84%) rename x/foundation/keeper/{ => internal}/exec.go (99%) rename x/foundation/keeper/{ => internal}/exec_test.go (92%) rename x/foundation/keeper/{ => internal}/genesis.go (99%) rename x/foundation/keeper/{ => internal}/genesis_test.go (99%) rename x/foundation/keeper/{ => internal}/grpc_query.go (99%) rename x/foundation/keeper/{ => internal}/grpc_query_test.go (54%) rename x/foundation/keeper/{ => internal}/invariants.go (99%) rename x/foundation/keeper/{ => internal}/invariants_test.go (79%) rename x/foundation/keeper/{ => internal}/keeper.go (99%) rename x/foundation/keeper/{ => internal}/keeper_test.go (78%) rename x/foundation/keeper/{ => internal}/keys.go (99%) rename x/foundation/keeper/{ => internal}/member.go (99%) rename x/foundation/keeper/{ => internal}/member_test.go (93%) rename x/foundation/keeper/{ => internal}/migrations.go (95%) rename x/foundation/keeper/{ => internal}/msg_server.go (99%) rename x/foundation/keeper/{ => internal}/msg_server_test.go (98%) rename x/foundation/keeper/{ => internal}/params.go (98%) rename x/foundation/keeper/{ => internal}/params_test.go (68%) rename x/foundation/keeper/{ => internal}/proposal.go (99%) rename x/foundation/keeper/{ => internal}/proposal_handler.go (99%) rename x/foundation/keeper/{ => internal}/proposal_handler_test.go (87%) rename x/foundation/keeper/{ => internal}/proposal_test.go (74%) rename x/foundation/keeper/{ => internal}/tally.go (99%) rename x/foundation/keeper/{ => internal}/treasury.go (99%) rename x/foundation/keeper/{ => internal}/treasury_test.go (80%) rename x/foundation/keeper/{ => internal}/vote.go (99%) rename x/foundation/keeper/{ => internal}/vote_test.go (85%) create mode 100644 x/stakingplus/testutil/expected_keepers_mocks.go diff --git a/CHANGELOG.md b/CHANGELOG.md index abc167ed1a..dfa9625d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (ante) [\#895](https://github.com/line/lbm-sdk/pull/895) Remove max gas validation * (x/collection,token) [\#900](https://github.com/line/lbm-sdk/pull/900) Add uri for MsgModify and deprecate the old ones * (x/foundation) [\#912](https://github.com/line/lbm-sdk/pull/912) Introduce censorship into x/foundation +* (x/foundation) [\#933](https://github.com/line/lbm-sdk/pull/933) Clean up x/foundation apis ### Bug Fixes * (client) [\#817](https://github.com/line/lbm-sdk/pull/817) remove support for composite (BLS) type diff --git a/Makefile b/Makefile index 6078ab800c..3750345080 100644 --- a/Makefile +++ b/Makefile @@ -152,6 +152,7 @@ mocks: $(MOCKS_DIR) mockgen -source=types/handler.go -package mocks -destination tests/mocks/types_handler.go mockgen -package mocks -destination tests/mocks/grpc_server.go github.com/gogo/protobuf/grpc Server mockgen -package mocks -destination tests/mocks/tendermint_tendermint_libs_log_DB.go github.com/line/ostracon/libs/log Logger + mockgen -source=x/stakingplus/expected_keepers.go -package testutil -destination x/stakingplus/testutil/expected_keepers_mocks.go .PHONY: mocks $(MOCKS_DIR): diff --git a/x/foundation/keeper/exported.go b/x/foundation/keeper/exported.go new file mode 100644 index 0000000000..8961eb105f --- /dev/null +++ b/x/foundation/keeper/exported.go @@ -0,0 +1,93 @@ +package keeper + +import ( + "github.com/line/lbm-sdk/baseapp" + "github.com/line/lbm-sdk/codec" + sdk "github.com/line/lbm-sdk/types" + "github.com/line/lbm-sdk/x/foundation" + "github.com/line/lbm-sdk/x/foundation/keeper/internal" + govtypes "github.com/line/lbm-sdk/x/gov/types" +) + +type Keeper interface { + GetAuthority() string + Accept(ctx sdk.Context, grantee sdk.AccAddress, msg sdk.Msg) error + + InitGenesis(ctx sdk.Context, gs *foundation.GenesisState) error + ExportGenesis(ctx sdk.Context) *foundation.GenesisState +} + +type keeper struct { + impl internal.Keeper +} + +func NewKeeper( + cdc codec.Codec, + key sdk.StoreKey, + router *baseapp.MsgServiceRouter, + authKeeper foundation.AuthKeeper, + bankKeeper foundation.BankKeeper, + feeCollectorName string, + config foundation.Config, + authority string, +) Keeper { + return &keeper{ + impl: internal.NewKeeper( + cdc, + key, + router, + authKeeper, + bankKeeper, + feeCollectorName, + config, + authority, + ), + } +} + +// GetAuthority returns the x/foundation module's authority. +func (k keeper) GetAuthority() string { + return k.impl.GetAuthority() +} + +func (k keeper) Accept(ctx sdk.Context, grantee sdk.AccAddress, msg sdk.Msg) error { + return k.impl.Accept(ctx, grantee, msg) +} + +func (k keeper) InitGenesis(ctx sdk.Context, gs *foundation.GenesisState) error { + return k.impl.InitGenesis(ctx, gs) +} + +func (k keeper) ExportGenesis(ctx sdk.Context) *foundation.GenesisState { + return k.impl.ExportGenesis(ctx) +} + +func NewMsgServer(k Keeper) foundation.MsgServer { + impl := k.(*keeper).impl + return internal.NewMsgServer(impl) +} + +func NewQueryServer(k Keeper) foundation.QueryServer { + impl := k.(*keeper).impl + return internal.NewQueryServer(impl) +} + +func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) { + impl := k.(*keeper).impl + internal.RegisterInvariants(ir, impl) +} + +func BeginBlocker(ctx sdk.Context, k Keeper) { + impl := k.(*keeper).impl + internal.BeginBlocker(ctx, impl) +} + +func EndBlocker(ctx sdk.Context, k Keeper) { + impl := k.(*keeper).impl + internal.EndBlocker(ctx, impl) +} + +func NewFoundationProposalsHandler(k Keeper) govtypes.Handler { + impl := k.(*keeper).impl + return internal.NewFoundationProposalsHandler(impl) +} diff --git a/x/foundation/keeper/abci.go b/x/foundation/keeper/internal/abci.go similarity index 97% rename from x/foundation/keeper/abci.go rename to x/foundation/keeper/internal/abci.go index 3c371b7047..4a84d31511 100644 --- a/x/foundation/keeper/abci.go +++ b/x/foundation/keeper/internal/abci.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "time" diff --git a/x/foundation/keeper/abci_test.go b/x/foundation/keeper/internal/abci_test.go similarity index 80% rename from x/foundation/keeper/abci_test.go rename to x/foundation/keeper/internal/abci_test.go index 07a73dd96e..43c43db206 100644 --- a/x/foundation/keeper/abci_test.go +++ b/x/foundation/keeper/internal/abci_test.go @@ -1,26 +1,26 @@ -package keeper_test +package internal_test import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/x/foundation" - "github.com/line/lbm-sdk/x/foundation/keeper" + "github.com/line/lbm-sdk/x/foundation/keeper/internal" ) func (s *KeeperTestSuite) TestBeginBlocker() { ctx, _ := s.ctx.CacheContext() - s.keeper.SetParams(ctx, foundation.Params{ + s.impl.SetParams(ctx, foundation.Params{ FoundationTax: sdk.MustNewDecFromStr("0.5"), }) - before := s.keeper.GetTreasury(ctx) + before := s.impl.GetTreasury(ctx) s.Require().Equal(1, len(before)) s.Require().Equal(sdk.NewDecFromInt(s.balance), before[0].Amount) // collect - keeper.BeginBlocker(ctx, s.keeper) + internal.BeginBlocker(ctx, s.impl) - after := s.keeper.GetTreasury(ctx) + after := s.impl.GetTreasury(ctx) s.Require().Equal(1, len(after)) // s.balance + s.balance * 0.5 s.Require().Equal(sdk.NewDecFromInt(s.balance.Add(s.balance.Quo(sdk.NewInt(2)))), after[0].Amount) @@ -52,7 +52,7 @@ func (s *KeeperTestSuite) TestEndBlocker() { }, } { s.Run(name, func() { - proposal, err := s.keeper.GetProposal(ctx, tc.id) + proposal, err := s.impl.GetProposal(ctx, tc.id) s.Require().NoError(err) s.Require().NotNil(proposal) s.Require().Equal(tc.status, proposal.Status) @@ -60,9 +60,9 @@ func (s *KeeperTestSuite) TestEndBlocker() { } // voting periods end - votingPeriod := s.keeper.GetFoundationInfo(ctx).GetDecisionPolicy().GetVotingPeriod() + votingPeriod := s.impl.GetFoundationInfo(ctx).GetDecisionPolicy().GetVotingPeriod() ctx = ctx.WithBlockTime(ctx.BlockTime().Add(votingPeriod)) - keeper.EndBlocker(ctx, s.keeper) + internal.EndBlocker(ctx, s.impl) for name, tc := range map[string]struct { id uint64 @@ -87,7 +87,7 @@ func (s *KeeperTestSuite) TestEndBlocker() { }, } { s.Run(name, func() { - proposal, err := s.keeper.GetProposal(ctx, tc.id) + proposal, err := s.impl.GetProposal(ctx, tc.id) if tc.removed { s.Require().Error(err) return @@ -101,8 +101,8 @@ func (s *KeeperTestSuite) TestEndBlocker() { // proposals expire maxExecutionPeriod := foundation.DefaultConfig().MaxExecutionPeriod ctx = ctx.WithBlockTime(ctx.BlockTime().Add(maxExecutionPeriod)) - keeper.EndBlocker(ctx, s.keeper) + internal.EndBlocker(ctx, s.impl) // all proposals must be pruned - s.Require().Empty(s.keeper.GetProposals(ctx)) + s.Require().Empty(s.impl.GetProposals(ctx)) } diff --git a/x/foundation/keeper/censorship.go b/x/foundation/keeper/internal/censorship.go similarity index 99% rename from x/foundation/keeper/censorship.go rename to x/foundation/keeper/internal/censorship.go index cfb6d3e81c..02e7217d07 100644 --- a/x/foundation/keeper/censorship.go +++ b/x/foundation/keeper/internal/censorship.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/censorship_test.go b/x/foundation/keeper/internal/censorship_test.go similarity index 84% rename from x/foundation/keeper/censorship_test.go rename to x/foundation/keeper/internal/censorship_test.go index 6cca8f913a..36a8bde944 100644 --- a/x/foundation/keeper/censorship_test.go +++ b/x/foundation/keeper/internal/censorship_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( sdk "github.com/line/lbm-sdk/types" @@ -15,15 +15,15 @@ func (s *KeeperTestSuite) TestUpdateCensorship() { msgTypeURL, dummyURL, } { - s.keeper.SetCensorship(ctx, foundation.Censorship{ + s.impl.SetCensorship(ctx, foundation.Censorship{ MsgTypeUrl: url, Authority: foundation.CensorshipAuthorityFoundation, }) } // check preconditions - s.Require().True(s.keeper.IsCensoredMessage(ctx, msgTypeURL)) - _, err := s.keeper.GetAuthorization(ctx, s.stranger, msgTypeURL) + s.Require().True(s.impl.IsCensoredMessage(ctx, msgTypeURL)) + _, err := s.impl.GetAuthorization(ctx, s.stranger, msgTypeURL) s.Require().NoError(err) // test update censorship @@ -32,16 +32,16 @@ func (s *KeeperTestSuite) TestUpdateCensorship() { Authority: foundation.CensorshipAuthorityUnspecified, } s.Require().NoError(removingCensorship.ValidateBasic()) - err = s.keeper.UpdateCensorship(ctx, removingCensorship) + err = s.impl.UpdateCensorship(ctx, removingCensorship) s.Require().NoError(err) // check censorship - _, err = s.keeper.GetCensorship(ctx, msgTypeURL) + _, err = s.impl.GetCensorship(ctx, msgTypeURL) s.Require().Error(err) - s.Require().False(s.keeper.IsCensoredMessage(ctx, msgTypeURL)) + s.Require().False(s.impl.IsCensoredMessage(ctx, msgTypeURL)) // check authorizations - _, err = s.keeper.GetAuthorization(ctx, s.stranger, msgTypeURL) + _, err = s.impl.GetAuthorization(ctx, s.stranger, msgTypeURL) s.Require().Error(err) // 2. re-enable the removed censorship, which must fail @@ -50,7 +50,7 @@ func (s *KeeperTestSuite) TestUpdateCensorship() { Authority: foundation.CensorshipAuthorityGovernance, } s.Require().NoError(newCensorship.ValidateBasic()) - err = s.keeper.UpdateCensorship(ctx, newCensorship) + err = s.impl.UpdateCensorship(ctx, newCensorship) s.Require().Error(err) } @@ -68,7 +68,7 @@ func (s *KeeperTestSuite) TestGrant() { }, "not being censored": { malleate: func(ctx sdk.Context) { - s.keeper.UpdateCensorship(ctx, foundation.Censorship{ + s.impl.UpdateCensorship(ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityUnspecified, }) @@ -89,7 +89,7 @@ func (s *KeeperTestSuite) TestGrant() { tc.malleate(ctx) } - err := s.keeper.Grant(ctx, tc.grantee, tc.auth) + err := s.impl.Grant(ctx, tc.grantee, tc.auth) if tc.valid { s.Require().NoError(err) } else { @@ -120,7 +120,7 @@ func (s *KeeperTestSuite) TestRevoke() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.Revoke(ctx, tc.grantee, tc.url) + err := s.impl.Revoke(ctx, tc.grantee, tc.url) if tc.valid { s.Require().NoError(err) } else { @@ -148,7 +148,7 @@ func (s *KeeperTestSuite) TestAccept() { }, "not being censored": { malleate: func(ctx sdk.Context) { - s.keeper.UpdateCensorship(ctx, foundation.Censorship{ + s.impl.UpdateCensorship(ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityUnspecified, }) @@ -178,7 +178,7 @@ func (s *KeeperTestSuite) TestAccept() { tc.malleate(ctx) } - err := s.keeper.Accept(ctx, tc.grantee, tc.msg) + err := s.impl.Accept(ctx, tc.grantee, tc.msg) if tc.valid { s.Require().NoError(err) } else { diff --git a/x/foundation/keeper/exec.go b/x/foundation/keeper/internal/exec.go similarity index 99% rename from x/foundation/keeper/exec.go rename to x/foundation/keeper/internal/exec.go index cf317b2fe9..3c6cd455cd 100644 --- a/x/foundation/keeper/exec.go +++ b/x/foundation/keeper/internal/exec.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "fmt" diff --git a/x/foundation/keeper/exec_test.go b/x/foundation/keeper/internal/exec_test.go similarity index 92% rename from x/foundation/keeper/exec_test.go rename to x/foundation/keeper/internal/exec_test.go index a235c62d38..b51e86372f 100644 --- a/x/foundation/keeper/exec_test.go +++ b/x/foundation/keeper/internal/exec_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test func (s *KeeperTestSuite) TestExec() { testCases := map[string]struct { @@ -33,7 +33,7 @@ func (s *KeeperTestSuite) TestExec() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.Exec(ctx, tc.proposalID) + err := s.impl.Exec(ctx, tc.proposalID) if tc.valid { s.Require().NoError(err) } else { diff --git a/x/foundation/keeper/genesis.go b/x/foundation/keeper/internal/genesis.go similarity index 99% rename from x/foundation/keeper/genesis.go rename to x/foundation/keeper/internal/genesis.go index c1afd5c2c5..2ce163d938 100644 --- a/x/foundation/keeper/genesis.go +++ b/x/foundation/keeper/internal/genesis.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/genesis_test.go b/x/foundation/keeper/internal/genesis_test.go similarity index 99% rename from x/foundation/keeper/genesis_test.go rename to x/foundation/keeper/internal/genesis_test.go index ba03227819..3262f3c6a8 100644 --- a/x/foundation/keeper/genesis_test.go +++ b/x/foundation/keeper/internal/genesis_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( "testing" diff --git a/x/foundation/keeper/grpc_query.go b/x/foundation/keeper/internal/grpc_query.go similarity index 99% rename from x/foundation/keeper/grpc_query.go rename to x/foundation/keeper/internal/grpc_query.go index 503663cee7..7fa25ca0ee 100644 --- a/x/foundation/keeper/grpc_query.go +++ b/x/foundation/keeper/internal/grpc_query.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "context" diff --git a/x/foundation/keeper/grpc_query_test.go b/x/foundation/keeper/internal/grpc_query_test.go similarity index 54% rename from x/foundation/keeper/grpc_query_test.go rename to x/foundation/keeper/internal/grpc_query_test.go index a81f8a5efb..a2c436b683 100644 --- a/x/foundation/keeper/grpc_query_test.go +++ b/x/foundation/keeper/internal/grpc_query_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( gocontext "context" @@ -11,8 +11,10 @@ import ( "github.com/line/lbm-sdk/baseapp" "github.com/line/lbm-sdk/simapp" sdk "github.com/line/lbm-sdk/types" + authtypes "github.com/line/lbm-sdk/x/auth/types" "github.com/line/lbm-sdk/x/foundation" "github.com/line/lbm-sdk/x/foundation/keeper" + "github.com/line/lbm-sdk/x/foundation/keeper/internal" ) type FoundationTestSuite struct { @@ -21,18 +23,31 @@ type FoundationTestSuite struct { app *simapp.SimApp ctx sdk.Context queryClient foundation.QueryClient + + impl internal.Keeper } -func (suite *FoundationTestSuite) SetupTest() { - suite.app = simapp.Setup(false) - suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{}) +func (s *FoundationTestSuite) SetupTest() { + s.app = simapp.Setup(false) + s.ctx = s.app.BaseApp.NewContext(false, tmproto.Header{}) + + queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, s.app.InterfaceRegistry()) + foundation.RegisterQueryServer(queryHelper, keeper.NewQueryServer(s.app.FoundationKeeper)) + s.queryClient = foundation.NewQueryClient(queryHelper) - queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry()) - foundation.RegisterQueryServer(queryHelper, keeper.NewQueryServer(suite.app.FoundationKeeper)) - suite.queryClient = foundation.NewQueryClient(queryHelper) + s.impl = internal.NewKeeper( + s.app.AppCodec(), + s.app.GetKey(foundation.ModuleName), + s.app.MsgServiceRouter(), + s.app.AccountKeeper, + s.app.BankKeeper, + authtypes.FeeCollectorName, + foundation.DefaultConfig(), + foundation.DefaultAuthority().String(), + ) } -func (suite *FoundationTestSuite) TestQueryParams() { +func (s *FoundationTestSuite) TestQueryParams() { var ( req *foundation.QueryParamsRequest expResponse foundation.QueryParamsResponse @@ -49,7 +64,7 @@ func (suite *FoundationTestSuite) TestQueryParams() { params := foundation.Params{ FoundationTax: sdk.OneDec(), } - suite.app.FoundationKeeper.SetParams(suite.ctx, params) + s.impl.SetParams(s.ctx, params) req = &foundation.QueryParamsRequest{} expResponse = foundation.QueryParamsResponse{Params: params} @@ -59,19 +74,19 @@ func (suite *FoundationTestSuite) TestQueryParams() { } for _, tc := range testCases { - suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { - suite.SetupTest() // reset + s.Run(fmt.Sprintf("Case %s", tc.msg), func() { + s.SetupTest() // reset tc.malleate() - res, err := suite.queryClient.Params(gocontext.Background(), req) + res, err := s.queryClient.Params(gocontext.Background(), req) if tc.expPass { - suite.Require().NoError(err) - suite.Require().NotNil(res) - suite.Require().Equal(&expResponse, res) + s.Require().NoError(err) + s.Require().NotNil(res) + s.Require().Equal(&expResponse, res) } else { - suite.Require().Error(err) + s.Require().Error(err) } }) } diff --git a/x/foundation/keeper/invariants.go b/x/foundation/keeper/internal/invariants.go similarity index 99% rename from x/foundation/keeper/invariants.go rename to x/foundation/keeper/internal/invariants.go index 7c7d224666..17ba279a27 100644 --- a/x/foundation/keeper/invariants.go +++ b/x/foundation/keeper/internal/invariants.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "fmt" diff --git a/x/foundation/keeper/invariants_test.go b/x/foundation/keeper/internal/invariants_test.go similarity index 79% rename from x/foundation/keeper/invariants_test.go rename to x/foundation/keeper/internal/invariants_test.go index f7aee23d0b..aebfb9385f 100644 --- a/x/foundation/keeper/invariants_test.go +++ b/x/foundation/keeper/internal/invariants_test.go @@ -1,9 +1,9 @@ -package keeper_test +package internal_test import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/x/foundation" - "github.com/line/lbm-sdk/x/foundation/keeper" + "github.com/line/lbm-sdk/x/foundation/keeper/internal" ) func (s *KeeperTestSuite) TestModuleAccountInvariant() { @@ -17,7 +17,7 @@ func (s *KeeperTestSuite) TestModuleAccountInvariant() { "treasury differs from the balance": { malleate: func(ctx sdk.Context) { balance := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance.Add(sdk.OneInt()))) - s.keeper.SetPool(ctx, foundation.Pool{ + s.impl.SetPool(ctx, foundation.Pool{ Treasury: sdk.NewDecCoinsFromCoins(balance...), }) }, @@ -31,7 +31,7 @@ func (s *KeeperTestSuite) TestModuleAccountInvariant() { tc.malleate(ctx) } - invariant := keeper.ModuleAccountInvariant(s.keeper) + invariant := internal.ModuleAccountInvariant(s.impl) _, broken := invariant(ctx) s.Require().Equal(!tc.valid, broken) }) @@ -48,10 +48,10 @@ func (s *KeeperTestSuite) TestTotalWeightInvariant() { }, "total weight differs from the number of foundation members": { malleate: func(ctx sdk.Context) { - info := s.keeper.GetFoundationInfo(ctx) - numMembers := len(s.keeper.GetMembers(ctx)) + info := s.impl.GetFoundationInfo(ctx) + numMembers := len(s.impl.GetMembers(ctx)) info.TotalWeight = sdk.NewDec(int64(numMembers)).Add(sdk.OneDec()) - s.keeper.SetFoundationInfo(ctx, info) + s.impl.SetFoundationInfo(ctx, info) }, }, } @@ -63,7 +63,7 @@ func (s *KeeperTestSuite) TestTotalWeightInvariant() { tc.malleate(ctx) } - invariant := keeper.TotalWeightInvariant(s.keeper) + invariant := internal.TotalWeightInvariant(s.impl) _, broken := invariant(ctx) s.Require().Equal(!tc.valid, broken) }) @@ -80,9 +80,9 @@ func (s *KeeperTestSuite) TestProposalInvariant() { }, "active old proposal exists": { malleate: func(ctx sdk.Context) { - info := s.keeper.GetFoundationInfo(ctx) + info := s.impl.GetFoundationInfo(ctx) info.Version-- - s.keeper.SetFoundationInfo(ctx, info) + s.impl.SetFoundationInfo(ctx, info) }, }, } @@ -94,7 +94,7 @@ func (s *KeeperTestSuite) TestProposalInvariant() { tc.malleate(ctx) } - invariant := keeper.ProposalInvariant(s.keeper) + invariant := internal.ProposalInvariant(s.impl) _, broken := invariant(ctx) s.Require().Equal(!tc.valid, broken) }) diff --git a/x/foundation/keeper/keeper.go b/x/foundation/keeper/internal/keeper.go similarity index 99% rename from x/foundation/keeper/keeper.go rename to x/foundation/keeper/internal/keeper.go index 0d64bc0216..85f3f6af9b 100644 --- a/x/foundation/keeper/keeper.go +++ b/x/foundation/keeper/internal/keeper.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "github.com/line/ostracon/libs/log" diff --git a/x/foundation/keeper/keeper_test.go b/x/foundation/keeper/internal/keeper_test.go similarity index 78% rename from x/foundation/keeper/keeper_test.go rename to x/foundation/keeper/internal/keeper_test.go index 656be6f8d8..c340537b44 100644 --- a/x/foundation/keeper/keeper_test.go +++ b/x/foundation/keeper/internal/keeper_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( "testing" @@ -13,7 +13,8 @@ import ( sdk "github.com/line/lbm-sdk/types" authtypes "github.com/line/lbm-sdk/x/auth/types" "github.com/line/lbm-sdk/x/foundation" - "github.com/line/lbm-sdk/x/foundation/keeper" + keeper "github.com/line/lbm-sdk/x/foundation/keeper" + "github.com/line/lbm-sdk/x/foundation/keeper/internal" govtypes "github.com/line/lbm-sdk/x/gov/types" minttypes "github.com/line/lbm-sdk/x/mint/types" ) @@ -24,6 +25,7 @@ type KeeperTestSuite struct { app *simapp.SimApp keeper keeper.Keeper + impl internal.Keeper queryServer foundation.QueryServer msgServer foundation.MsgServer proposalHandler govtypes.Handler @@ -58,17 +60,27 @@ func (s *KeeperTestSuite) SetupTest() { s.ctx = s.app.BaseApp.NewContext(checkTx, tmproto.Header{}) s.keeper = s.app.FoundationKeeper + s.impl = internal.NewKeeper( + s.app.AppCodec(), + s.app.GetKey(foundation.ModuleName), + s.app.MsgServiceRouter(), + s.app.AccountKeeper, + s.app.BankKeeper, + authtypes.FeeCollectorName, + foundation.DefaultConfig(), + foundation.DefaultAuthority().String(), + ) s.queryServer = keeper.NewQueryServer(s.keeper) s.msgServer = keeper.NewMsgServer(s.keeper) s.proposalHandler = keeper.NewFoundationProposalsHandler(s.keeper) - s.keeper.SetParams(s.ctx, foundation.Params{ + s.impl.SetParams(s.ctx, foundation.Params{ FoundationTax: sdk.OneDec(), }) - s.keeper.SetCensorship(s.ctx, foundation.Censorship{ + s.impl.SetCensorship(s.ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityFoundation, }) @@ -77,14 +89,14 @@ func (s *KeeperTestSuite) SetupTest() { return sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) } - s.authority = sdk.MustAccAddressFromBech32(s.keeper.GetAuthority()) + s.authority = sdk.MustAccAddressFromBech32(s.impl.GetAuthority()) s.members = make([]sdk.AccAddress, 10) for i := range s.members { s.members[i] = createAddress() member := foundation.Member{ Address: s.members[i].String(), } - s.keeper.SetMember(s.ctx, member) + s.impl.SetMember(s.ctx, member) } s.stranger = createAddress() @@ -92,10 +104,10 @@ func (s *KeeperTestSuite) SetupTest() { info.TotalWeight = sdk.NewDec(int64(len(s.members))) err := info.SetDecisionPolicy(workingPolicy()) s.Require().NoError(err) - s.keeper.SetFoundationInfo(s.ctx, info) + s.impl.SetFoundationInfo(s.ctx, info) s.balance = sdk.NewInt(1000000) - s.keeper.SetPool(s.ctx, foundation.Pool{ + s.impl.SetPool(s.ctx, foundation.Pool{ Treasury: sdk.NewDecCoinsFromCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }) holders := []sdk.AccAddress{ @@ -120,12 +132,12 @@ func (s *KeeperTestSuite) SetupTest() { } // create a proposal - activeProposal, err := s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{newMsgCreateDog("shiba1")}) + activeProposal, err := s.impl.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{newMsgCreateDog("shiba1")}) s.Require().NoError(err) s.activeProposal = *activeProposal for _, member := range s.members[1:] { - err := s.keeper.Vote(s.ctx, foundation.Vote{ + err := s.impl.Vote(s.ctx, foundation.Vote{ ProposalId: s.activeProposal, Voter: member.String(), Option: foundation.VOTE_OPTION_YES, @@ -134,12 +146,12 @@ func (s *KeeperTestSuite) SetupTest() { } // create a proposal voted by all members - votedProposal, err := s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{newMsgCreateDog("shiba2")}) + votedProposal, err := s.impl.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{newMsgCreateDog("shiba2")}) s.Require().NoError(err) s.votedProposal = *votedProposal for _, member := range s.members { - err := s.keeper.Vote(s.ctx, foundation.Vote{ + err := s.impl.Vote(s.ctx, foundation.Vote{ ProposalId: s.votedProposal, Voter: member.String(), Option: foundation.VOTE_OPTION_NO, @@ -148,15 +160,15 @@ func (s *KeeperTestSuite) SetupTest() { } // create an withdrawn proposal - withdrawnProposal, err := s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{newMsgCreateDog("shiba3")}) + withdrawnProposal, err := s.impl.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{newMsgCreateDog("shiba3")}) s.Require().NoError(err) s.withdrawnProposal = *withdrawnProposal - err = s.keeper.WithdrawProposal(s.ctx, s.withdrawnProposal) + err = s.impl.WithdrawProposal(s.ctx, s.withdrawnProposal) s.Require().NoError(err) // create an invalid proposal which contains invalid message - invalidProposal, err := s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{ + invalidProposal, err := s.impl.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{ &foundation.MsgWithdrawFromTreasury{ Authority: s.authority.String(), To: s.stranger.String(), @@ -167,7 +179,7 @@ func (s *KeeperTestSuite) SetupTest() { s.invalidProposal = *invalidProposal for _, member := range s.members { - err := s.keeper.Vote(s.ctx, foundation.Vote{ + err := s.impl.Vote(s.ctx, foundation.Vote{ ProposalId: s.invalidProposal, Voter: member.String(), Option: foundation.VOTE_OPTION_YES, @@ -176,12 +188,12 @@ func (s *KeeperTestSuite) SetupTest() { } // create an invalid proposal which contains invalid message - noHandlerProposal, err := s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{testdata.NewTestMsg(s.authority)}) + noHandlerProposal, err := s.impl.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{testdata.NewTestMsg(s.authority)}) s.Require().NoError(err) s.noHandlerProposal = *noHandlerProposal for _, member := range s.members { - err := s.keeper.Vote(s.ctx, foundation.Vote{ + err := s.impl.Vote(s.ctx, foundation.Vote{ ProposalId: s.noHandlerProposal, Voter: member.String(), Option: foundation.VOTE_OPTION_YES, @@ -193,7 +205,7 @@ func (s *KeeperTestSuite) SetupTest() { s.nextProposal = s.noHandlerProposal + 1 // grant stranger to receive foundation treasury - err = s.keeper.Grant(s.ctx, s.stranger, &foundation.ReceiveFromTreasuryAuthorization{}) + err = s.impl.Grant(s.ctx, s.stranger, &foundation.ReceiveFromTreasuryAuthorization{}) s.Require().NoError(err) } diff --git a/x/foundation/keeper/keys.go b/x/foundation/keeper/internal/keys.go similarity index 99% rename from x/foundation/keeper/keys.go rename to x/foundation/keeper/internal/keys.go index 6515ad6787..e5b140608e 100644 --- a/x/foundation/keeper/keys.go +++ b/x/foundation/keeper/internal/keys.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "encoding/binary" diff --git a/x/foundation/keeper/member.go b/x/foundation/keeper/internal/member.go similarity index 99% rename from x/foundation/keeper/member.go rename to x/foundation/keeper/internal/member.go index eaccbf745f..a01be954d4 100644 --- a/x/foundation/keeper/member.go +++ b/x/foundation/keeper/internal/member.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/member_test.go b/x/foundation/keeper/internal/member_test.go similarity index 93% rename from x/foundation/keeper/member_test.go rename to x/foundation/keeper/internal/member_test.go index c90ab113c7..d46e2aaa4d 100644 --- a/x/foundation/keeper/member_test.go +++ b/x/foundation/keeper/internal/member_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( "time" @@ -37,7 +37,7 @@ func (s *KeeperTestSuite) TestUpdateDecisionPolicy() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.UpdateDecisionPolicy(ctx, tc.policy) + err := s.impl.UpdateDecisionPolicy(ctx, tc.policy) if tc.valid { s.Require().NoError(err) } else { @@ -91,7 +91,7 @@ func (s *KeeperTestSuite) TestUpdateMembers() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.UpdateMembers(ctx, tc.updates) + err := s.impl.UpdateMembers(ctx, tc.updates) if tc.valid { s.Require().NoError(err) } else { diff --git a/x/foundation/keeper/migrations.go b/x/foundation/keeper/internal/migrations.go similarity index 95% rename from x/foundation/keeper/migrations.go rename to x/foundation/keeper/internal/migrations.go index 775ea928f7..d2f4726089 100644 --- a/x/foundation/keeper/migrations.go +++ b/x/foundation/keeper/internal/migrations.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/msg_server.go b/x/foundation/keeper/internal/msg_server.go similarity index 99% rename from x/foundation/keeper/msg_server.go rename to x/foundation/keeper/internal/msg_server.go index c648025f29..31b8bc0d5f 100644 --- a/x/foundation/keeper/msg_server.go +++ b/x/foundation/keeper/internal/msg_server.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "context" diff --git a/x/foundation/keeper/msg_server_test.go b/x/foundation/keeper/internal/msg_server_test.go similarity index 98% rename from x/foundation/keeper/msg_server_test.go rename to x/foundation/keeper/internal/msg_server_test.go index 46f3652b4d..d0360aceb4 100644 --- a/x/foundation/keeper/msg_server_test.go +++ b/x/foundation/keeper/internal/msg_server_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( "time" @@ -26,7 +26,7 @@ func (s *KeeperTestSuite) TestMsgUpdateParams() { }, "enabling foundation tax": { malleate: func(ctx sdk.Context) { - s.keeper.SetParams(ctx, foundation.Params{ + s.impl.SetParams(ctx, foundation.Params{ FoundationTax: sdk.ZeroDec(), }) }, @@ -285,7 +285,7 @@ func (s *KeeperTestSuite) TestMsgSubmitProposal() { "exec fails": { malleate: func(ctx sdk.Context) { // try exec will fail because of a non-zero MinExecutionPeriod. - err := s.keeper.UpdateDecisionPolicy(ctx, &foundation.ThresholdDecisionPolicy{ + err := s.impl.UpdateDecisionPolicy(ctx, &foundation.ThresholdDecisionPolicy{ Threshold: sdk.OneDec(), Windows: &foundation.DecisionPolicyWindows{ VotingPeriod: time.Hour, @@ -402,7 +402,7 @@ func (s *KeeperTestSuite) TestMsgVote() { "exec fails": { malleate: func(ctx sdk.Context) { // try exec will fail because of a non-zero MinExecutionPeriod. - err := s.keeper.UpdateDecisionPolicy(ctx, &foundation.ThresholdDecisionPolicy{ + err := s.impl.UpdateDecisionPolicy(ctx, &foundation.ThresholdDecisionPolicy{ Threshold: sdk.OneDec(), Windows: &foundation.DecisionPolicyWindows{ VotingPeriod: time.Hour, @@ -528,7 +528,7 @@ func (s *KeeperTestSuite) TestMsgLeaveFoundation() { Remove: true, } } - err := s.keeper.UpdateMembers(ctx, requests) + err := s.impl.UpdateMembers(ctx, requests) s.Require().NoError(err) }, address: s.members[0], diff --git a/x/foundation/keeper/params.go b/x/foundation/keeper/internal/params.go similarity index 98% rename from x/foundation/keeper/params.go rename to x/foundation/keeper/internal/params.go index bfdf3c0c78..e8dab52b18 100644 --- a/x/foundation/keeper/params.go +++ b/x/foundation/keeper/internal/params.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/params_test.go b/x/foundation/keeper/internal/params_test.go similarity index 68% rename from x/foundation/keeper/params_test.go rename to x/foundation/keeper/internal/params_test.go index 5a437968c7..0d62fc56d7 100644 --- a/x/foundation/keeper/params_test.go +++ b/x/foundation/keeper/internal/params_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( sdk "github.com/line/lbm-sdk/types" @@ -8,7 +8,7 @@ import ( func (s *KeeperTestSuite) TestUpdateParams() { ctx, _ := s.ctx.CacheContext() - s.keeper.SetParams(ctx, foundation.Params{ + s.impl.SetParams(ctx, foundation.Params{ FoundationTax: sdk.OneDec(), }) @@ -19,12 +19,12 @@ func (s *KeeperTestSuite) TestUpdateParams() { FoundationTax: sdk.ZeroDec(), } s.Require().NoError(removingParams.ValidateBasic()) - err := s.keeper.UpdateParams(ctx, removingParams) + err := s.impl.UpdateParams(ctx, removingParams) s.Require().NoError(err) // check params - s.Require().Equal(removingParams, s.keeper.GetParams(ctx)) - s.Require().Equal(removingParams.FoundationTax, s.keeper.GetFoundationTax(ctx)) + s.Require().Equal(removingParams, s.impl.GetParams(ctx)) + s.Require().Equal(removingParams.FoundationTax, s.impl.GetFoundationTax(ctx)) // 2. re-enable the features, which must fail // a. foundation tax @@ -32,6 +32,6 @@ func (s *KeeperTestSuite) TestUpdateParams() { FoundationTax: sdk.OneDec(), } s.Require().NoError(taxParams.ValidateBasic()) - err = s.keeper.UpdateParams(ctx, taxParams) + err = s.impl.UpdateParams(ctx, taxParams) s.Require().Error(err) } diff --git a/x/foundation/keeper/proposal.go b/x/foundation/keeper/internal/proposal.go similarity index 99% rename from x/foundation/keeper/proposal.go rename to x/foundation/keeper/internal/proposal.go index 11ca4138b4..6efe06c777 100644 --- a/x/foundation/keeper/proposal.go +++ b/x/foundation/keeper/internal/proposal.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( "time" diff --git a/x/foundation/keeper/proposal_handler.go b/x/foundation/keeper/internal/proposal_handler.go similarity index 99% rename from x/foundation/keeper/proposal_handler.go rename to x/foundation/keeper/internal/proposal_handler.go index baf3601c20..29203dfa4d 100644 --- a/x/foundation/keeper/proposal_handler.go +++ b/x/foundation/keeper/internal/proposal_handler.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/proposal_handler_test.go b/x/foundation/keeper/internal/proposal_handler_test.go similarity index 87% rename from x/foundation/keeper/proposal_handler_test.go rename to x/foundation/keeper/internal/proposal_handler_test.go index 206846abf5..fb784e1a4c 100644 --- a/x/foundation/keeper/proposal_handler_test.go +++ b/x/foundation/keeper/internal/proposal_handler_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( "github.com/line/lbm-sdk/testutil/testdata" @@ -17,7 +17,7 @@ func (s *KeeperTestSuite) TestProposalHandler() { }{ "valid": { malleate: func(ctx sdk.Context) { - s.keeper.SetCensorship(ctx, foundation.Censorship{ + s.impl.SetCensorship(ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityGovernance, }) @@ -31,18 +31,18 @@ func (s *KeeperTestSuite) TestProposalHandler() { }, valid: true, require: func(ctx sdk.Context) { - s.Require().False(s.keeper.IsCensoredMessage(ctx, sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)))) + s.Require().False(s.impl.IsCensoredMessage(ctx, sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)))) }, }, "bad signer": { malleate: func(ctx sdk.Context) { - s.keeper.SetCensorship(ctx, foundation.Censorship{ + s.impl.SetCensorship(ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityGovernance, }) }, msg: &foundation.MsgUpdateCensorship{ - Authority: s.keeper.GetAuthority(), + Authority: s.impl.GetAuthority(), Censorship: foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityUnspecified, @@ -51,7 +51,7 @@ func (s *KeeperTestSuite) TestProposalHandler() { }, "message type not allowed": { malleate: func(ctx sdk.Context) { - s.keeper.SetCensorship(ctx, foundation.Censorship{ + s.impl.SetCensorship(ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityGovernance, }) @@ -60,7 +60,7 @@ func (s *KeeperTestSuite) TestProposalHandler() { }, "no handler found": { malleate: func(ctx sdk.Context) { - s.keeper.SetCensorship(ctx, foundation.Censorship{ + s.impl.SetCensorship(ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityGovernance, }) @@ -69,7 +69,7 @@ func (s *KeeperTestSuite) TestProposalHandler() { }, "message execution failed": { malleate: func(ctx sdk.Context) { - s.keeper.SetCensorship(ctx, foundation.Censorship{ + s.impl.SetCensorship(ctx, foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), Authority: foundation.CensorshipAuthorityGovernance, }) diff --git a/x/foundation/keeper/proposal_test.go b/x/foundation/keeper/internal/proposal_test.go similarity index 74% rename from x/foundation/keeper/proposal_test.go rename to x/foundation/keeper/internal/proposal_test.go index 70f67404bf..bd43f0d77b 100644 --- a/x/foundation/keeper/proposal_test.go +++ b/x/foundation/keeper/internal/proposal_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( "testing" @@ -10,7 +10,9 @@ import ( "github.com/line/lbm-sdk/simapp" "github.com/line/lbm-sdk/testutil/testdata" sdk "github.com/line/lbm-sdk/types" + authtypes "github.com/line/lbm-sdk/x/auth/types" "github.com/line/lbm-sdk/x/foundation" + "github.com/line/lbm-sdk/x/foundation/keeper/internal" ) func (s *KeeperTestSuite) TestSubmitProposal() { @@ -40,7 +42,7 @@ func (s *KeeperTestSuite) TestSubmitProposal() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - _, err := s.keeper.SubmitProposal(ctx, tc.proposers, tc.metadata, []sdk.Msg{tc.msg}) + _, err := s.impl.SubmitProposal(ctx, tc.proposers, tc.metadata, []sdk.Msg{tc.msg}) if tc.valid { s.Require().NoError(err) } else { @@ -71,7 +73,7 @@ func (s *KeeperTestSuite) TestWithdrawProposal() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.WithdrawProposal(ctx, tc.id) + err := s.impl.WithdrawProposal(ctx, tc.id) if tc.valid { s.Require().NoError(err) } else { @@ -87,19 +89,28 @@ func TestAbortProposal(t *testing.T) { testdata.RegisterInterfaces(app.InterfaceRegistry()) ctx := app.BaseApp.NewContext(checkTx, tmproto.Header{}) - keeper := app.FoundationKeeper + impl := internal.NewKeeper( + app.AppCodec(), + app.GetKey(foundation.ModuleName), + app.MsgServiceRouter(), + app.AccountKeeper, + app.BankKeeper, + authtypes.FeeCollectorName, + foundation.DefaultConfig(), + foundation.DefaultAuthority().String(), + ) createAddress := func() sdk.AccAddress { return sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) } - authority := sdk.MustAccAddressFromBech32(keeper.GetAuthority()) + authority := sdk.MustAccAddressFromBech32(impl.GetAuthority()) members := make([]sdk.AccAddress, 10) for i := range members { members[i] = createAddress() } - keeper.SetMember(ctx, foundation.Member{ + impl.SetMember(ctx, foundation.Member{ Address: members[0].String(), }) @@ -107,14 +118,14 @@ func TestAbortProposal(t *testing.T) { info.TotalWeight = sdk.NewDec(int64(len(members))) err := info.SetDecisionPolicy(workingPolicy()) require.NoError(t, err) - keeper.SetFoundationInfo(ctx, info) + impl.SetFoundationInfo(ctx, info) // create proposals of different versions and abort them for _, newMember := range members[1:] { - _, err := keeper.SubmitProposal(ctx, []string{members[0].String()}, "", []sdk.Msg{testdata.NewTestMsg(authority)}) + _, err := impl.SubmitProposal(ctx, []string{members[0].String()}, "", []sdk.Msg{testdata.NewTestMsg(authority)}) require.NoError(t, err) - err = keeper.UpdateMembers(ctx, []foundation.MemberRequest{ + err = impl.UpdateMembers(ctx, []foundation.MemberRequest{ { Address: newMember.String(), }, @@ -122,7 +133,7 @@ func TestAbortProposal(t *testing.T) { require.NoError(t, err) } - for _, proposal := range keeper.GetProposals(ctx) { + for _, proposal := range impl.GetProposals(ctx) { require.Equal(t, foundation.PROPOSAL_STATUS_ABORTED, proposal.Status) } } diff --git a/x/foundation/keeper/tally.go b/x/foundation/keeper/internal/tally.go similarity index 99% rename from x/foundation/keeper/tally.go rename to x/foundation/keeper/internal/tally.go index a658bd4221..f90559c37f 100644 --- a/x/foundation/keeper/tally.go +++ b/x/foundation/keeper/internal/tally.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/treasury.go b/x/foundation/keeper/internal/treasury.go similarity index 99% rename from x/foundation/keeper/treasury.go rename to x/foundation/keeper/internal/treasury.go index 2162fb7b28..0490308f66 100644 --- a/x/foundation/keeper/treasury.go +++ b/x/foundation/keeper/internal/treasury.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/treasury_test.go b/x/foundation/keeper/internal/treasury_test.go similarity index 80% rename from x/foundation/keeper/treasury_test.go rename to x/foundation/keeper/internal/treasury_test.go index 71852d0711..22e5803a6b 100644 --- a/x/foundation/keeper/treasury_test.go +++ b/x/foundation/keeper/internal/treasury_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( sdk "github.com/line/lbm-sdk/types" @@ -22,17 +22,17 @@ func (s *KeeperTestSuite) TestFundTreasury() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - before := s.keeper.GetTreasury(ctx) + before := s.impl.GetTreasury(ctx) amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)) - err := s.keeper.FundTreasury(ctx, s.stranger, amount) + err := s.impl.FundTreasury(ctx, s.stranger, amount) if !tc.valid { s.Require().Error(err) return } s.Require().NoError(err) - after := s.keeper.GetTreasury(ctx) + after := s.impl.GetTreasury(ctx) s.Require().Equal(before.Add(sdk.NewDecCoinsFromCoins(amount...)...), after) }) } @@ -56,17 +56,17 @@ func (s *KeeperTestSuite) TestWithDrawFromTreasury() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - before := s.keeper.GetTreasury(ctx) + before := s.impl.GetTreasury(ctx) amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)) - err := s.keeper.WithdrawFromTreasury(ctx, s.stranger, amount) + err := s.impl.WithdrawFromTreasury(ctx, s.stranger, amount) if !tc.valid { s.Require().Error(err) return } s.Require().NoError(err) - after := s.keeper.GetTreasury(ctx) + after := s.impl.GetTreasury(ctx) s.Require().Equal(before.Sub(sdk.NewDecCoinsFromCoins(amount...)), after) }) } diff --git a/x/foundation/keeper/vote.go b/x/foundation/keeper/internal/vote.go similarity index 99% rename from x/foundation/keeper/vote.go rename to x/foundation/keeper/internal/vote.go index d888d08b62..a8b6bb9126 100644 --- a/x/foundation/keeper/vote.go +++ b/x/foundation/keeper/internal/vote.go @@ -1,4 +1,4 @@ -package keeper +package internal import ( sdk "github.com/line/lbm-sdk/types" diff --git a/x/foundation/keeper/vote_test.go b/x/foundation/keeper/internal/vote_test.go similarity index 85% rename from x/foundation/keeper/vote_test.go rename to x/foundation/keeper/internal/vote_test.go index 9864a80378..b73a7e9233 100644 --- a/x/foundation/keeper/vote_test.go +++ b/x/foundation/keeper/internal/vote_test.go @@ -1,4 +1,4 @@ -package keeper_test +package internal_test import ( "time" @@ -9,7 +9,7 @@ import ( func (s *KeeperTestSuite) TestVote() { // no such a vote - _, err := s.keeper.GetVote(s.ctx, s.nextProposal, s.members[0]) + _, err := s.impl.GetVote(s.ctx, s.nextProposal, s.members[0]) s.Require().Error(err) testCases := map[string]struct { @@ -57,7 +57,7 @@ func (s *KeeperTestSuite) TestVote() { proposalID: s.activeProposal, voter: s.members[0], option: foundation.VOTE_OPTION_YES, - after: s.keeper.GetFoundationInfo(s.ctx).GetDecisionPolicy().GetVotingPeriod(), + after: s.impl.GetFoundationInfo(s.ctx).GetDecisionPolicy().GetVotingPeriod(), }, } @@ -72,14 +72,14 @@ func (s *KeeperTestSuite) TestVote() { Option: tc.option, Metadata: tc.metadata, } - err := s.keeper.Vote(ctx, vote) + err := s.impl.Vote(ctx, vote) if !tc.valid { s.Require().Error(err) return } s.Require().NoError(err) - _, err = s.keeper.GetVote(ctx, vote.ProposalId, sdk.MustAccAddressFromBech32(vote.Voter)) + _, err = s.impl.GetVote(ctx, vote.ProposalId, sdk.MustAccAddressFromBech32(vote.Voter)) s.Require().NoError(err) }) } diff --git a/x/foundation/msgs_test.go b/x/foundation/msgs_test.go index 0f8b19c8c8..1d468df8d5 100644 --- a/x/foundation/msgs_test.go +++ b/x/foundation/msgs_test.go @@ -755,10 +755,10 @@ func TestMsgSubmitProposalAminoJSON(t *testing.T) { }, "MsgUpdateCensorship": { &foundation.MsgUpdateCensorship{ - Authority: addrs[0].String(), + Authority: addrs[0].String(), Censorship: foundation.Censorship{ MsgTypeUrl: sdk.MsgTypeURL((*foundation.MsgWithdrawFromTreasury)(nil)), - Authority: foundation.CensorshipAuthorityGovernance, + Authority: foundation.CensorshipAuthorityGovernance, }, }, fmt.Sprintf("{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSubmitProposal\",\"value\":{\"exec\":1,\"messages\":[{\"type\":\"lbm-sdk/MsgUpdateCensorship\",\"value\":{\"authority\":\"%s\",\"censorship\":{\"authority\":1,\"msg_type_url\":\"/lbm.foundation.v1.MsgWithdrawFromTreasury\"}}}],\"metadata\":\"MsgUpdateCensorship\",\"proposers\":[\"%s\"]}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", addrs[0].String(), proposer.String()), diff --git a/x/stakingplus/keeper/keeper_test.go b/x/stakingplus/keeper/keeper_test.go index 8a3b252613..004a6c1717 100644 --- a/x/stakingplus/keeper/keeper_test.go +++ b/x/stakingplus/keeper/keeper_test.go @@ -1,20 +1,23 @@ package keeper_test import ( + "fmt" "testing" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/line/lbm-sdk/crypto/keys/secp256k1" "github.com/line/lbm-sdk/simapp" sdk "github.com/line/lbm-sdk/types" - "github.com/line/lbm-sdk/x/foundation" + sdkerrors "github.com/line/lbm-sdk/types/errors" minttypes "github.com/line/lbm-sdk/x/mint/types" stakingkeeper "github.com/line/lbm-sdk/x/staking/keeper" stakingtypes "github.com/line/lbm-sdk/x/staking/types" "github.com/line/lbm-sdk/x/stakingplus" "github.com/line/lbm-sdk/x/stakingplus/keeper" + "github.com/line/lbm-sdk/x/stakingplus/testutil" ) type KeeperTestSuite struct { @@ -32,12 +35,15 @@ type KeeperTestSuite struct { } func (s *KeeperTestSuite) SetupTest() { + ctrl := gomock.NewController(s.T()) + foundationKeeper := testutil.NewMockFoundationKeeper(ctrl) + checkTx := false s.app = simapp.Setup(checkTx) s.ctx = s.app.BaseApp.NewContext(checkTx, tmproto.Header{}) s.keeper = s.app.StakingKeeper - s.msgServer = keeper.NewMsgServerImpl(s.keeper, s.app.FoundationKeeper) + s.msgServer = keeper.NewMsgServerImpl(s.keeper, foundationKeeper) createAddress := func() sdk.AccAddress { return sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) @@ -67,17 +73,43 @@ func (s *KeeperTestSuite) SetupTest() { s.Require().NoError(err) } - // allow Msg/CreateValidator - s.app.FoundationKeeper.SetCensorship(s.ctx, foundation.Censorship{ - MsgTypeUrl: stakingplus.CreateValidatorAuthorization{}.MsgTypeURL(), - Authority: foundation.CensorshipAuthorityFoundation, - }) - err := s.app.FoundationKeeper.Grant(s.ctx, s.grantee, &stakingplus.CreateValidatorAuthorization{ - ValidatorAddress: sdk.ValAddress(s.grantee).String(), - }) - s.Require().NoError(err) + // approve Msg/CreateValidator to grantee + foundationKeeper. + EXPECT(). + Accept(gomock.Any(), s.grantee, NewCreateValidatorAuthorizationMatcher(s.grantee)). + Return(nil) + foundationKeeper. + EXPECT(). + Accept(gomock.Any(), gomock.Any(), gomock.Any()). + Return(sdkerrors.ErrUnauthorized) } func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } + +type CreateValidatorAuthorizationMatcher struct { + authz stakingplus.CreateValidatorAuthorization +} + +func NewCreateValidatorAuthorizationMatcher(grantee sdk.AccAddress) *CreateValidatorAuthorizationMatcher { + return &CreateValidatorAuthorizationMatcher{ + authz: stakingplus.CreateValidatorAuthorization{ + ValidatorAddress: sdk.ValAddress(grantee).String(), + }, + } +} + +func (c CreateValidatorAuthorizationMatcher) Matches(x interface{}) bool { + msg, ok := x.(sdk.Msg) + if !ok { + return false + } + + resp, err := c.authz.Accept(sdk.Context{}, msg) + return resp.Accept && (err == nil) +} + +func (c CreateValidatorAuthorizationMatcher) String() string { + return fmt.Sprintf("grants %s to %s", c.authz.MsgTypeURL(), c.authz.ValidatorAddress) +} diff --git a/x/stakingplus/testutil/expected_keepers_mocks.go b/x/stakingplus/testutil/expected_keepers_mocks.go new file mode 100644 index 0000000000..5bf7c18ee1 --- /dev/null +++ b/x/stakingplus/testutil/expected_keepers_mocks.go @@ -0,0 +1,49 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: x/stakingplus/expected_keepers.go + +// Package testutil is a generated GoMock package. +package testutil + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + types "github.com/line/lbm-sdk/types" +) + +// MockFoundationKeeper is a mock of FoundationKeeper interface. +type MockFoundationKeeper struct { + ctrl *gomock.Controller + recorder *MockFoundationKeeperMockRecorder +} + +// MockFoundationKeeperMockRecorder is the mock recorder for MockFoundationKeeper. +type MockFoundationKeeperMockRecorder struct { + mock *MockFoundationKeeper +} + +// NewMockFoundationKeeper creates a new mock instance. +func NewMockFoundationKeeper(ctrl *gomock.Controller) *MockFoundationKeeper { + mock := &MockFoundationKeeper{ctrl: ctrl} + mock.recorder = &MockFoundationKeeperMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockFoundationKeeper) EXPECT() *MockFoundationKeeperMockRecorder { + return m.recorder +} + +// Accept mocks base method. +func (m *MockFoundationKeeper) Accept(ctx types.Context, grantee types.AccAddress, msg types.Msg) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Accept", ctx, grantee, msg) + ret0, _ := ret[0].(error) + return ret0 +} + +// Accept indicates an expected call of Accept. +func (mr *MockFoundationKeeperMockRecorder) Accept(ctx, grantee, msg interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Accept", reflect.TypeOf((*MockFoundationKeeper)(nil).Accept), ctx, grantee, msg) +}