Skip to content

Commit

Permalink
refactor: fix some non conventional issues on sudo module (#1491)
Browse files Browse the repository at this point in the history
* fix some non conventional issues on sudo module

* linter

* remove old handler

* linter

* linter

* linter

* use constants

* linter
  • Loading branch information
jgimeno authored Jul 5, 2023
1 parent 4f7d824 commit b1b4205
Show file tree
Hide file tree
Showing 24 changed files with 195 additions and 234 deletions.
8 changes: 6 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"os"
"path/filepath"

"github.com/NibiruChain/nibiru/x/sudo/keeper"

sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
dbm "github.com/cometbft/cometbft-db"
Expand Down Expand Up @@ -191,7 +195,7 @@ var (

epochstypes.ModuleName: {},
stablecointypes.StableEFModuleAccount: {authtypes.Burner},
sudo.ModuleName: {},
sudotypes.ModuleName: {},
common.TreasuryPoolModuleAccount: {},
wasm.ModuleName: {authtypes.Burner},
}
Expand Down Expand Up @@ -270,7 +274,7 @@ type NibiruApp struct {
OracleKeeper oraclekeeper.Keeper
StablecoinKeeper stablecoinkeeper.Keeper
InflationKeeper inflationkeeper.Keeper
SudoKeeper sudo.Keeper
SudoKeeper keeper.Keeper

// WASM keepers
WasmKeeper wasm.Keeper
Expand Down
12 changes: 8 additions & 4 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package app
import (
"path/filepath"

"github.com/NibiruChain/nibiru/x/sudo/keeper"

sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"

_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -137,7 +141,7 @@ func GetStoreKeys() (
epochstypes.StoreKey,
perpv2types.StoreKey,
inflationtypes.StoreKey,
sudo.StoreKey,
sudotypes.StoreKey,
wasm.StoreKey,
)
tkeys = sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -298,8 +302,8 @@ func (app *NibiruApp) InitKeepers(
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, authtypes.FeeCollectorName,
)

app.SudoKeeper = sudo.NewKeeper(
appCodec, keys[sudo.StoreKey],
app.SudoKeeper = keeper.NewKeeper(
appCodec, keys[sudotypes.StoreKey],
)

app.EpochsKeeper.SetHooks(
Expand Down Expand Up @@ -572,7 +576,7 @@ func OrderedModuleNames() []string {
oracletypes.ModuleName,
perpv2types.ModuleName,
inflationtypes.ModuleName,
sudo.ModuleName,
sudotypes.ModuleName,

// --------------------------------------------------------------------
// IBC modules
Expand Down
2 changes: 1 addition & 1 deletion x/common/testutil/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
perpv2cli "github.com/NibiruChain/nibiru/x/perp/v2/client/cli"
perpv2types "github.com/NibiruChain/nibiru/x/perp/v2/types"
sudocli "github.com/NibiruChain/nibiru/x/sudo/cli"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/pb"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"
)

// ExecQueryOption defines a type which customizes a CLI query operation.
Expand Down
2 changes: 1 addition & 1 deletion x/common/testutil/genesis/sudo_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/NibiruChain/nibiru/app"

"github.com/NibiruChain/nibiru/x/sudo"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/pb"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

Expand Down
18 changes: 9 additions & 9 deletions x/sudo/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import (
"os"
"strings"

"github.com/NibiruChain/nibiru/x/sudo/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"

"github.com/spf13/cobra"

"github.com/NibiruChain/nibiru/x/sudo/pb"
)

// GetTxCmd returns a cli command for this module's transactions
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: pb.ModuleName,
Short: fmt.Sprintf("x/%s transaction subcommands", pb.ModuleName),
Use: types.ModuleName,
Short: fmt.Sprintf("x/%s transaction subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
Expand All @@ -36,9 +36,9 @@ func GetTxCmd() *cobra.Command {
// GetQueryCmd returns a cli command for this module's queries
func GetQueryCmd() *cobra.Command {
moduleQueryCmd := &cobra.Command{
Use: pb.ModuleName,
Use: types.ModuleName,
Short: fmt.Sprintf(
"Query commands for the x/%s module", pb.ModuleName),
"Query commands for the x/%s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
Expand Down Expand Up @@ -84,7 +84,7 @@ func CmdEditSudoers() *cobra.Command {
return err
}

msg := new(pb.MsgEditSudoers)
msg := new(types.MsgEditSudoers)

// marshals contents into the proto.Message to which 'msg' points.
contents, err := os.ReadFile(args[0])
Expand Down Expand Up @@ -123,9 +123,9 @@ func CmdQuerySudoers() *cobra.Command {
return err
}

queryClient := pb.NewQueryClient(clientCtx)
queryClient := types.NewQueryClient(clientCtx)

req := new(pb.QuerySudoersRequest)
req := new(types.QuerySudoersRequest)
resp, err := queryClient.QuerySudoers(
cmd.Context(), req,
)
Expand Down
25 changes: 13 additions & 12 deletions x/sudo/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import (
"strings"
"testing"

"github.com/NibiruChain/nibiru/x/sudo/types"

"github.com/cosmos/gogoproto/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/crypto"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/common/denoms"
Expand All @@ -19,12 +26,6 @@ import (
testutilcli "github.com/NibiruChain/nibiru/x/common/testutil/cli"
"github.com/NibiruChain/nibiru/x/common/testutil/genesis"
"github.com/NibiruChain/nibiru/x/sudo/cli"
"github.com/NibiruChain/nibiru/x/sudo/pb"

"github.com/cosmos/cosmos-sdk/crypto"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ———————————————————————————————————————————————————————————————————
Expand All @@ -34,7 +35,7 @@ import (
// MsgEditSudoersPlus is a wrapper struct to extend the default MsgEditSudoers
// type with convenience functions
type MsgEditSudoersPlus struct {
pb.MsgEditSudoers
types.MsgEditSudoers
}

// ToJson converts the message into a json string and saves it in a temporary
Expand All @@ -52,7 +53,7 @@ func (msg MsgEditSudoersPlus) ToJson(t *testing.T) (fileJsonBz []byte, fileName
`, msg.Action, strings.Join(msg.Contracts, `", "`), msg.Sender)

t.Log("check the unmarshal json → proto")
tempMsg := new(pb.MsgEditSudoers)
tempMsg := new(types.MsgEditSudoers)
err := jsonpb.UnmarshalString(msgJsonStr, tempMsg)
assert.NoErrorf(t, err, "DEBUG tempMsg: %v\njsonStr: %v", tempMsg, msgJsonStr)

Expand Down Expand Up @@ -158,7 +159,7 @@ func (s *IntegrationSuite) TestCmdEditSudoers() {

var sender sdk.AccAddress = s.root.addr

pbMsg := pb.MsgEditSudoers{
pbMsg := types.MsgEditSudoers{
Action: "add_contracts",
Contracts: []string{contracts[0], contracts[1], contracts[2]},
Sender: sender.String(),
Expand Down Expand Up @@ -191,7 +192,7 @@ func (s *IntegrationSuite) TestCmdEditSudoers() {
s.True(gotContracts.Has(contract))
}

pbMsg = pb.MsgEditSudoers{
pbMsg = types.MsgEditSudoers{
Action: "remove_contracts",
Contracts: []string{contracts[1]},
Sender: sender.String(),
Expand Down Expand Up @@ -231,7 +232,7 @@ func (s *IntegrationSuite) TestMarshal_EditSudoers() {
for _, addr := range addrs[1:] {
contracts = append(contracts, addr.String())
}
msg := pb.MsgEditSudoers{
msg := types.MsgEditSudoers{
Action: "add_contracts",
Contracts: contracts,
Sender: sender.String(),
Expand All @@ -243,7 +244,7 @@ func (s *IntegrationSuite) TestMarshal_EditSudoers() {

t.Log("check unmarshal file → proto")
cdc := genesis.TEST_ENCODING_CONFIG.Marshaler
newMsg := new(pb.MsgEditSudoers)
newMsg := new(types.MsgEditSudoers)
err := cdc.UnmarshalJSON(fileJsonBz, newMsg)
assert.NoErrorf(t, err, "fileJsonBz: #%v", fileJsonBz)
require.NoError(t, newMsg.ValidateBasic(), newMsg.String())
Expand Down
15 changes: 8 additions & 7 deletions x/sudo/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package sudo
import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/x/sudo/pb"
"github.com/NibiruChain/nibiru/x/sudo/keeper"
"github.com/NibiruChain/nibiru/x/sudo/types"
)

// InitGenesis initializes the module's state from a provided genesis state JSON.
func InitGenesis(ctx sdk.Context, k Keeper, genState pb.GenesisState) {
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
if err := genState.Validate(); err != nil {
panic(err)
}
Expand All @@ -16,20 +17,20 @@ func InitGenesis(ctx sdk.Context, k Keeper, genState pb.GenesisState) {

// ExportGenesis returns the module's exported genesis state.
// This fn assumes InitGenesis has already been called.
func ExportGenesis(ctx sdk.Context, k Keeper) *pb.GenesisState {
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
pbSudoers, err := k.Sudoers.Get(ctx)
if err != nil {
panic(err)
}

return &pb.GenesisState{
return &types.GenesisState{
Sudoers: pbSudoers,
}
}

func DefaultGenesis() *pb.GenesisState {
return &pb.GenesisState{
Sudoers: pb.Sudoers{
func DefaultGenesis() *types.GenesisState {
return &types.GenesisState{
Sudoers: types.Sudoers{
Root: "",
Contracts: []string{},
},
Expand Down
12 changes: 6 additions & 6 deletions x/sudo/querier.go → x/sudo/keeper/querier.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package sudo
package keeper

import (
"context"

"github.com/NibiruChain/nibiru/x/sudo/pb"
"github.com/NibiruChain/nibiru/x/sudo/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// Ensure the interface is properly implemented at compile time
var _ pb.QueryServer = Keeper{}
var _ types.QueryServer = Keeper{}

func (k Keeper) QuerySudoers(
goCtx context.Context,
req *pb.QuerySudoersRequest,
) (resp *pb.QuerySudoersResponse, err error) {
req *types.QuerySudoersRequest,
) (resp *types.QuerySudoersResponse, err error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
sudoers, err := k.Sudoers.Get(ctx)

return &pb.QuerySudoersResponse{
return &types.QuerySudoersResponse{
Sudoers: sudoers,
}, err
}
Loading

0 comments on commit b1b4205

Please sign in to comment.