Skip to content

Commit

Permalink
chore: clean up x/foundation api (#933)
Browse files Browse the repository at this point in the history
* Hide unnecessary apis

* Use x/foundation mocks

* Lint

* Update CHANGELOG.md

* Remove interface

* Revert "Remove interface"

This reverts commit 8dd124d.
  • Loading branch information
0Tech authored Mar 21, 2023
1 parent 2ec553e commit aabe516
Show file tree
Hide file tree
Showing 37 changed files with 363 additions and 149 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
93 changes: 93 additions & 0 deletions x/foundation/keeper/exported.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package internal

import (
"time"
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -52,17 +52,17 @@ 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)
})
}

// 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
Expand All @@ -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
Expand All @@ -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))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package internal

import (
sdk "github.com/line/lbm-sdk/types"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper_test
package internal_test

import (
sdk "github.com/line/lbm-sdk/types"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}

Expand All @@ -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,
})
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package internal

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper_test
package internal_test

func (s *KeeperTestSuite) TestExec() {
testCases := map[string]struct {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package internal

import (
sdk "github.com/line/lbm-sdk/types"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper_test
package internal_test

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package internal

import (
"context"
Expand Down
Loading

0 comments on commit aabe516

Please sign in to comment.