Skip to content

Commit

Permalink
x/bank: use internal package
Browse files Browse the repository at this point in the history
Reorganise x/bank sub-packages and leverage
internal special package for enhanced
encapsulation.
  • Loading branch information
alessio committed Jun 9, 2019
1 parent 6606007 commit 9e07d69
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 75 deletions.
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
47 changes: 31 additions & 16 deletions x/bank/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
package bank

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

const (
Expand All @@ -20,26 +23,38 @@ const (

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
NewBaseSendKeeper = keeper.NewBaseSendKeeper
NewBaseViewKeeper = keeper.NewBaseViewKeeper
NewMsgSend = types.NewMsgSend
NewMsgMultiSend = types.NewMsgMultiSend
NewInput = types.NewInput
NewOutput = types.NewOutput
ValidateInputsOutputs = types.ValidateInputsOutputs
ParamKeyTable = types.ParamKeyTable
NewHandler = handler.NewHandler
SimulateMsgSend = simulation.SimulateMsgSend
SimulateSingleInputMsgMultiSend = simulation.SimulateSingleInputMsgMultiSend

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

type (
MsgSend = types.MsgSend
MsgMultiSend = types.MsgMultiSend
Input = types.Input
Output = types.Output
BaseKeeper = keeper.BaseKeeper
BaseSendKeeper = keeper.BaseSendKeeper
BaseViewKeeper = keeper.BaseViewKeeper
Keeper = keeper.Keeper
MsgSend = types.MsgSend
MsgMultiSend = types.MsgMultiSend
SendKeeper = keeper.SendKeeper
Input = types.Input
Output = types.Output
ViewKeeper = keeper.ViewKeeper
)
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
13 changes: 7 additions & 6 deletions x/bank/handler.go → x/bank/internal/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package bank
package handler

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/tags"
"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 +27,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 @@ -47,7 +48,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 Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bank
package handler

import (
"strings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package bank
package invariants

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"
types "github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

// register bank invariants
Expand Down
12 changes: 7 additions & 5 deletions x/bank/app_test.go → x/bank/internal/keeper/app_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package bank
package keeper_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 → x/bank/internal/keeper/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bank
package keeper_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/internal/handler"
"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, handler.NewHandler(bankKeeper))
mapp.SetInitChainer(getInitChainer(mapp, bankKeeper))

err := mapp.CompleteSetup()
Expand Down
6 changes: 3 additions & 3 deletions x/bank/keeper.go → x/bank/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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/tags"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/params"
)

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
Loading

0 comments on commit 9e07d69

Please sign in to comment.