Skip to content

Commit

Permalink
x/bank: use internal package (#4521)
Browse files Browse the repository at this point in the history
Reorganise x/bank packages and leverage internal special
package for enhanced encapsulation.
  • Loading branch information
Alessio Treglia authored Jun 14, 2019
1 parent d5fe9b7 commit 1e9ca4a
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 96 deletions.
1 change: 1 addition & 0 deletions .pending/breaking/sdk/4521-Flatten-x-bank-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4521 Flatten x/bank structure by hiding module internals.
5 changes: 2 additions & 3 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/genaccounts"
authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation"
"github.com/cosmos/cosmos-sdk/x/bank"
banksim "github.com/cosmos/cosmos-sdk/x/bank/simulation"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
distrsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation"
"github.com/cosmos/cosmos-sdk/x/gov"
Expand Down Expand Up @@ -605,7 +604,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation {
})
return v
}(nil),
banksim.SimulateMsgSend(app.accountKeeper, app.bankKeeper),
bank.SimulateMsgSend(app.accountKeeper, app.bankKeeper),
},
{
func(_ *rand.Rand) int {
Expand All @@ -616,7 +615,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation {
})
return v
}(nil),
banksim.SimulateSingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper),
bank.SimulateSingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper),
},
{
func(_ *rand.Rand) int {
Expand Down
26 changes: 13 additions & 13 deletions x/bank/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
package bank

import (
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

const (
Expand All @@ -15,29 +16,28 @@ const (
ModuleName = types.ModuleName
RouterKey = types.RouterKey
DefaultParamspace = types.DefaultParamspace
DefaultSendEnabled = types.DefaultSendEnabled
)

var (
// functions aliases
RegisterCodec = types.RegisterCodec
ErrNoInputs = types.ErrNoInputs
ErrNoOutputs = types.ErrNoOutputs
ErrInputOutputMismatch = types.ErrInputOutputMismatch
ErrSendDisabled = types.ErrSendDisabled
NewMsgSend = types.NewMsgSend
NewMsgMultiSend = types.NewMsgMultiSend
NewInput = types.NewInput
NewOutput = types.NewOutput
ValidateInputsOutputs = types.ValidateInputsOutputs
ParamKeyTable = types.ParamKeyTable
RegisterCodec = types.RegisterCodec
ErrNoInputs = types.ErrNoInputs
ErrNoOutputs = types.ErrNoOutputs
ErrInputOutputMismatch = types.ErrInputOutputMismatch
ErrSendDisabled = types.ErrSendDisabled
NewBaseKeeper = keeper.NewBaseKeeper
NewInput = types.NewInput
NewOutput = types.NewOutput
ParamKeyTable = types.ParamKeyTable

// variable aliases
ModuleCdc = types.ModuleCdc
ParamStoreKeySendEnabled = types.ParamStoreKeySendEnabled
)

type (
BaseKeeper = keeper.BaseKeeper // ibc module depends on this
Keeper = keeper.Keeper
MsgSend = types.MsgSend
MsgMultiSend = types.MsgMultiSend
Input = types.Input
Expand Down
12 changes: 7 additions & 5 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package bank
package bank_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/mock"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -95,11 +97,11 @@ func getMockApp(t *testing.T) *mock.App {
}

// overwrite the mock init chainer
func getInitChainer(mapp *mock.App, keeper BaseKeeper) sdk.InitChainer {
func getInitChainer(mapp *mock.App, keeper keeper.BaseKeeper) sdk.InitChainer {
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
mapp.InitChainer(ctx, req)
bankGenesis := DefaultGenesisState()
InitGenesis(ctx, keeper, bankGenesis)
bankGenesis := bank.DefaultGenesisState()
bank.InitGenesis(ctx, keeper, bankGenesis)

return abci.ResponseInitChain{}
}
Expand Down
10 changes: 6 additions & 4 deletions x/bank/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bank
package bank_test

import (
"testing"
Expand All @@ -7,7 +7,9 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/mock"
)

Expand All @@ -17,12 +19,12 @@ func getBenchmarkMockApp() (*mock.App, error) {
mapp := mock.NewApp()

types.RegisterCodec(mapp.Cdc)
bankKeeper := NewBaseKeeper(
bankKeeper := keeper.NewBaseKeeper(
mapp.AccountKeeper,
mapp.ParamsKeeper.Subspace(types.DefaultParamspace),
types.DefaultCodespace,
)
mapp.Router().AddRoute(types.RouterKey, NewHandler(bankKeeper))
mapp.Router().AddRoute(types.RouterKey, bank.NewHandler(bankKeeper))
mapp.SetInitChainer(getInitChainer(mapp, bankKeeper))

err := mapp.CompleteSetup()
Expand Down
2 changes: 1 addition & 1 deletion x/bank/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion x/bank/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"

"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

// RegisterRoutes - Central function to define routes that get registered by the main application
Expand Down
18 changes: 9 additions & 9 deletions x/bank/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/tags"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

// NewHandler returns a handler for "bank" type messages.
func NewHandler(k Keeper) sdk.Handler {
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case types.MsgSend:
Expand All @@ -26,7 +26,7 @@ func NewHandler(k Keeper) sdk.Handler {
}

// Handle MsgSend.
func handleMsgSend(ctx sdk.Context, k Keeper, msg types.MsgSend) sdk.Result {
func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) sdk.Result {
if !k.GetSendEnabled(ctx) {
return types.ErrSendDisabled(k.Codespace()).Result()
}
Expand All @@ -36,9 +36,9 @@ func handleMsgSend(ctx sdk.Context, k Keeper, msg types.MsgSend) sdk.Result {
}

resTags := sdk.NewTags(
tags.Category, tags.TxCategory,
tags.Sender, msg.FromAddress.String(),
tags.Recipient, msg.ToAddress.String(),
types.Category, types.TxCategory,
types.Sender, msg.FromAddress.String(),
types.Recipient, msg.ToAddress.String(),
)

return sdk.Result{
Expand All @@ -47,7 +47,7 @@ func handleMsgSend(ctx sdk.Context, k Keeper, msg types.MsgSend) sdk.Result {
}

// Handle MsgMultiSend.
func handleMsgMultiSend(ctx sdk.Context, k Keeper, msg types.MsgMultiSend) sdk.Result {
func handleMsgMultiSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgMultiSend) sdk.Result {
// NOTE: totalIn == totalOut should already have been checked
if !k.GetSendEnabled(ctx) {
return types.ErrSendDisabled(k.Codespace()).Result()
Expand All @@ -57,7 +57,7 @@ func handleMsgMultiSend(ctx sdk.Context, k Keeper, msg types.MsgMultiSend) sdk.R
return err.Result()
}

resTags = resTags.AppendTag(tags.Category, tags.TxCategory)
resTags = resTags.AppendTag(types.Category, types.TxCategory)
return sdk.Result{
Tags: resTags,
}
Expand Down
4 changes: 2 additions & 2 deletions x/bank/invariants.go → x/bank/internal/keeper/invariants.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package bank
package keeper

import (
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

// register bank invariants
Expand Down
13 changes: 6 additions & 7 deletions x/bank/keeper.go → x/bank/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package bank
package keeper

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/tags"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/params"
)

Expand Down Expand Up @@ -340,7 +339,7 @@ func inputOutputCoins(ctx sdk.Context, am auth.AccountKeeper, inputs []types.Inp
}

allTags = allTags.AppendTag(
tags.Sender, in.Address.String(),
types.Sender, in.Address.String(),
)
}

Expand All @@ -350,7 +349,7 @@ func inputOutputCoins(ctx sdk.Context, am auth.AccountKeeper, inputs []types.Inp
return nil, err
}
allTags = allTags.AppendTag(
tags.Recipient, out.Address.String(),
types.Recipient, out.Address.String(),
)
}

Expand Down Expand Up @@ -386,7 +385,7 @@ func delegateCoins(
setAccount(ctx, ak, acc)

return sdk.NewTags(
sdk.TagAction, tags.ActionDelegateCoins,
sdk.TagAction, types.ActionDelegateCoins,
sdk.TagDelegator, addr.String(),
), nil
}
Expand All @@ -411,7 +410,7 @@ func undelegateCoins(
setAccount(ctx, ak, acc)

return sdk.NewTags(
sdk.TagAction, tags.ActionUndelegateCoins,
sdk.TagAction, types.ActionUndelegateCoins,
sdk.TagDelegator, addr.String(),
), nil
}
Expand Down
26 changes: 13 additions & 13 deletions x/bank/keeper_test.go → x/bank/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bank
package keeper

import (
"testing"
Expand All @@ -14,7 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/params"
)

Expand Down Expand Up @@ -120,13 +120,13 @@ func TestKeeper(t *testing.T) {
require.True(t, bankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 10), sdk.NewInt64Coin("foocoin", 8))))

inputs := []types.Input{
NewInput(addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 3))),
types.NewInput(addr, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 3))),
types.NewInput(addr2, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 3), sdk.NewInt64Coin("foocoin", 2))),
}

outputs := []types.Output{
NewOutput(addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 1))),
NewOutput(addr3, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 2), sdk.NewInt64Coin("foocoin", 5))),
types.NewOutput(addr, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 1))),
types.NewOutput(addr3, sdk.NewCoins(sdk.NewInt64Coin("barcoin", 2), sdk.NewInt64Coin("foocoin", 5))),
}
bankKeeper.InputOutputCoins(ctx, inputs, outputs)
require.True(t, bankKeeper.GetCoins(ctx, addr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("barcoin", 21), sdk.NewInt64Coin("foocoin", 4))))
Expand All @@ -139,7 +139,7 @@ func TestSendKeeper(t *testing.T) {
ctx := input.ctx
paramSpace := input.pk.Subspace(types.DefaultParamspace)
bankKeeper := NewBaseKeeper(input.ak, paramSpace, types.DefaultCodespace)
sendKeeper := NewBaseSendKeeper(input.ak, paramSpace, DefaultCodespace)
sendKeeper := NewBaseSendKeeper(input.ak, paramSpace, types.DefaultCodespace)
bankKeeper.SetSendEnabled(ctx, true)

addr := sdk.AccAddress([]byte("addr1"))
Expand Down Expand Up @@ -186,10 +186,10 @@ func TestSendKeeper(t *testing.T) {
func TestViewKeeper(t *testing.T) {
input := setupTestInput()
ctx := input.ctx
paramSpace := input.pk.Subspace(DefaultParamspace)
bankKeeper := NewBaseKeeper(input.ak, paramSpace, DefaultCodespace)
paramSpace := input.pk.Subspace(types.DefaultParamspace)
bankKeeper := NewBaseKeeper(input.ak, paramSpace, types.DefaultCodespace)
bankKeeper.SetSendEnabled(ctx, true)
viewKeeper := NewBaseViewKeeper(input.ak, DefaultCodespace)
viewKeeper := NewBaseViewKeeper(input.ak, types.DefaultCodespace)

addr := sdk.AccAddress([]byte("addr1"))
acc := input.ak.NewAccountWithAddress(ctx, addr)
Expand All @@ -216,7 +216,7 @@ func TestVestingAccountSend(t *testing.T) {

origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(DefaultParamspace), DefaultCodespace)
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(types.DefaultParamspace), types.DefaultCodespace)
bankKeeper.SetSendEnabled(ctx, true)

addr1 := sdk.AccAddress([]byte("addr1"))
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestVestingAccountReceive(t *testing.T) {

origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(DefaultParamspace), DefaultCodespace)
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(types.DefaultParamspace), types.DefaultCodespace)
bankKeeper.SetSendEnabled(ctx, true)

addr1 := sdk.AccAddress([]byte("addr1"))
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestDelegateCoins(t *testing.T) {

origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(DefaultParamspace), DefaultCodespace)
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(types.DefaultParamspace), types.DefaultCodespace)
bankKeeper.SetSendEnabled(ctx, true)

addr1 := sdk.AccAddress([]byte("addr1"))
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestUndelegateCoins(t *testing.T) {

origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(DefaultParamspace), DefaultCodespace)
bankKeeper := NewBaseKeeper(input.ak, input.pk.Subspace(types.DefaultParamspace), types.DefaultCodespace)
bankKeeper.SetSendEnabled(ctx, true)

addr1 := sdk.AccAddress([]byte("addr1"))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion x/bank/tags/tags.go → x/bank/internal/types/tags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tags
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
Loading

0 comments on commit 1e9ca4a

Please sign in to comment.