Skip to content

Commit

Permalink
Move ModuleAccount to x/auth (#6029)
Browse files Browse the repository at this point in the history
* Move ModuleAccount to x/auth

* Update x/auth module

* Update x/staking

* Update x/mint

* Update x/gov

* Update x/distribution

* Update simapp

* Update x/bank

* Update std codec

* Add changelog entries

* Update CHANGELOG.md

Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
  • Loading branch information
alexanderbez and fedekunze authored Apr 20, 2020
1 parent 1083fa9 commit fea2315
Show file tree
Hide file tree
Showing 72 changed files with 1,103 additions and 1,098 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ older clients.

### API Breaking Changes

* (x/auth) [\#6029](https://github.com/cosmos/cosmos-sdk/pull/6029) Module accounts have been moved from `x/supply` to `x/auth`.
* (x/supply) [\#6010](https://github.com/cosmos/cosmos-sdk/pull/6010) All `x/supply` types and APIs have been moved to `x/bank`.
* (baseapp) [\#5865](https://github.com/cosmos/cosmos-sdk/pull/5865) The `SimulationResponse` returned from tx simulation is now JSON encoded instead of Amino binary.
* [\#5719](https://github.com/cosmos/cosmos-sdk/pull/5719) Bump Go requirement to 1.14+
Expand Down
224 changes: 112 additions & 112 deletions codec/std/codec.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion codec/std/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ message Account {
cosmos_sdk.x.auth.vesting.v1.ContinuousVestingAccount continuous_vesting_account = 2;
cosmos_sdk.x.auth.vesting.v1.DelayedVestingAccount delayed_vesting_account = 3;
cosmos_sdk.x.auth.vesting.v1.PeriodicVestingAccount periodic_vesting_account = 4;
cosmos_sdk.x.bank.v1.ModuleAccount module_account = 5;
cosmos_sdk.x.auth.v1.ModuleAccount module_account = 5;
}
}

Expand Down
38 changes: 19 additions & 19 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ var (
maccPerms = map[string][]string{
auth.FeeCollectorName: nil,
distr.ModuleName: nil,
mint.ModuleName: {bank.Minter},
staking.BondedPoolName: {bank.Burner, bank.Staking},
staking.NotBondedPoolName: {bank.Burner, bank.Staking},
gov.ModuleName: {bank.Burner},
transfer.GetModuleAccountName(): {bank.Minter, bank.Burner},
mint.ModuleName: {auth.Minter},
staking.BondedPoolName: {auth.Burner, auth.Staking},
staking.NotBondedPoolName: {auth.Burner, auth.Staking},
gov.ModuleName: {auth.Burner},
transfer.GetModuleAccountName(): {auth.Minter, auth.Burner},
}

// module accounts that are allowed to receive tokens
Expand Down Expand Up @@ -188,21 +188,21 @@ func NewSimApp(

// add keepers
app.AccountKeeper = auth.NewAccountKeeper(
appCodec, keys[auth.StoreKey], app.subspaces[auth.ModuleName], auth.ProtoBaseAccount,
appCodec, keys[auth.StoreKey], app.subspaces[auth.ModuleName], auth.ProtoBaseAccount, maccPerms,
)
app.BankKeeper = bank.NewBaseKeeper(
appCodec, keys[bank.StoreKey], app.AccountKeeper, app.subspaces[bank.ModuleName], app.BlacklistedAccAddrs(), maccPerms,
appCodec, keys[bank.StoreKey], app.AccountKeeper, app.subspaces[bank.ModuleName], app.BlacklistedAccAddrs(),
)
stakingKeeper := staking.NewKeeper(
appCodec, keys[staking.StoreKey], app.BankKeeper, app.subspaces[staking.ModuleName],
appCodec, keys[staking.StoreKey], app.AccountKeeper, app.BankKeeper, app.subspaces[staking.ModuleName],
)
app.MintKeeper = mint.NewKeeper(
appCodec, keys[mint.StoreKey], app.subspaces[mint.ModuleName], &stakingKeeper,
app.BankKeeper, auth.FeeCollectorName,
app.AccountKeeper, app.BankKeeper, auth.FeeCollectorName,
)
app.DistrKeeper = distr.NewKeeper(
appCodec, keys[distr.StoreKey], app.subspaces[distr.ModuleName], app.BankKeeper, &stakingKeeper,
auth.FeeCollectorName, app.ModuleAccountAddrs(),
appCodec, keys[distr.StoreKey], app.subspaces[distr.ModuleName], app.AccountKeeper, app.BankKeeper,
&stakingKeeper, auth.FeeCollectorName, app.ModuleAccountAddrs(),
)
app.SlashingKeeper = slashing.NewKeeper(
appCodec, keys[slashing.StoreKey], &stakingKeeper, app.subspaces[slashing.ModuleName],
Expand All @@ -219,7 +219,7 @@ func NewSimApp(
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgrade.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper))
app.GovKeeper = gov.NewKeeper(
appCodec, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.BankKeeper,
appCodec, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
)

Expand All @@ -238,7 +238,7 @@ func NewSimApp(
app.TransferKeeper = transfer.NewKeeper(
app.cdc, keys[transfer.StoreKey],
app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.BankKeeper, scopedTransferKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
)
transferModule := transfer.NewAppModule(app.TransferKeeper)

Expand All @@ -261,12 +261,12 @@ func NewSimApp(
// must be passed by reference here.
app.mm = module.NewManager(
genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx),
auth.NewAppModule(app.AccountKeeper, app.BankKeeper),
auth.NewAppModule(app.AccountKeeper),
bank.NewAppModule(app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(*app.CapabilityKeeper),
crisis.NewAppModule(&app.CrisisKeeper),
gov.NewAppModule(app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(app.MintKeeper, app.BankKeeper),
mint.NewAppModule(app.MintKeeper, app.AccountKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
Expand Down Expand Up @@ -302,10 +302,10 @@ func NewSimApp(
// NOTE: this is not required apps that don't use the simulator for fuzz testing
// transactions
app.sm = module.NewSimulationManager(
auth.NewAppModule(app.AccountKeeper, app.BankKeeper),
auth.NewAppModule(app.AccountKeeper),
bank.NewAppModule(app.BankKeeper, app.AccountKeeper),
gov.NewAppModule(app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(app.MintKeeper, app.BankKeeper),
mint.NewAppModule(app.MintKeeper, app.AccountKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
Expand Down Expand Up @@ -376,7 +376,7 @@ func (app *SimApp) LoadHeight(height int64) error {
func (app *SimApp) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
modAccAddrs[bank.NewModuleAddress(acc).String()] = true
modAccAddrs[auth.NewModuleAddress(acc).String()] = true
}

return modAccAddrs
Expand All @@ -386,7 +386,7 @@ func (app *SimApp) ModuleAccountAddrs() map[string]bool {
func (app *SimApp) BlacklistedAccAddrs() map[string]bool {
blacklistedAddrs := make(map[string]bool)
for acc := range maccPerms {
blacklistedAddrs[bank.NewModuleAddress(acc).String()] = !allowedReceivingModAcc[acc]
blacklistedAddrs[auth.NewModuleAddress(acc).String()] = !allowedReceivingModAcc[acc]
}

return blacklistedAddrs
Expand Down
2 changes: 1 addition & 1 deletion simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestBlackListedAddrs(t *testing.T) {
app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0)

for acc := range maccPerms {
require.Equal(t, !allowedReceivingModAcc[acc], app.BankKeeper.BlacklistedAddr(app.BankKeeper.GetModuleAddress(acc)))
require.Equal(t, !allowedReceivingModAcc[acc], app.BankKeeper.BlacklistedAddr(app.AccountKeeper.GetModuleAddress(acc)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions simapp/genesis_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"
)

var _ authexported.GenesisAccount = (*SimGenesisAccount)(nil)
Expand Down Expand Up @@ -37,7 +37,7 @@ func (sga SimGenesisAccount) Validate() error {
}

if sga.ModuleName != "" {
ma := bank.ModuleAccount{
ma := auth.ModuleAccount{
BaseAccount: sga.BaseAccount, Name: sga.ModuleName, Permissions: sga.ModulePermissions,
}
if err := ma.Validate(); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions x/auth/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
QueryAccount = types.QueryAccount
QueryParams = types.QueryParams
MaxGasWanted = types.MaxGasWanted
Minter = types.Minter
Burner = types.Burner
Staking = types.Staking
)

var (
Expand Down Expand Up @@ -60,6 +63,9 @@ var (
ValidateGenAccounts = types.ValidateGenAccounts
GetGenesisStateFromAppState = types.GetGenesisStateFromAppState
NewStdSignature = types.NewStdSignature
NewModuleAddress = types.NewModuleAddress
NewEmptyModuleAccount = types.NewEmptyModuleAccount
NewModuleAccount = types.NewModuleAccount

// variable aliases
ModuleCdc = types.ModuleCdc
Expand Down Expand Up @@ -89,4 +95,5 @@ type (
TxBuilder = types.TxBuilder
GenesisAccountIterator = types.GenesisAccountIterator
Codec = types.Codec
ModuleAccount = types.ModuleAccount
)
2 changes: 1 addition & 1 deletion x/auth/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestAnteHandlerFees(t *testing.T) {
app.BankKeeper.SetBalances(ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("atom", 149)))
checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInsufficientFunds)

modAcc := app.BankKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
modAcc := app.AccountKeeper.GetModuleAccount(ctx, types.FeeCollectorName)

require.True(t, app.BankKeeper.GetAllBalances(ctx, modAcc.GetAddress()).Empty())
require.True(sdk.IntEq(t, app.BankKeeper.GetAllBalances(ctx, addr1).AmountOf("atom"), sdk.NewInt(149)))
Expand Down
2 changes: 1 addition & 1 deletion x/auth/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

if addr := dfd.bankKeeper.GetModuleAddress(types.FeeCollectorName); addr == nil {
if addr := dfd.ak.GetModuleAddress(types.FeeCollectorName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.FeeCollectorName))
}

Expand Down
10 changes: 10 additions & 0 deletions x/auth/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ type Account interface {
String() string
}

// ModuleAccountI defines an account interface for modules that hold tokens in
// an escrow.
type ModuleAccountI interface {
Account

GetName() string
GetPermissions() []string
HasPermission(string) bool
}

// GenesisAccounts defines a slice of GenesisAccount objects
type GenesisAccounts []GenesisAccount

Expand Down
5 changes: 2 additions & 3 deletions x/auth/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package auth
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

// InitGenesis - Init store state from genesis data
//
// CONTRACT: old coins from the FeeCollectionKeeper need to be transferred through
// a genesis port script to the new fee collector account
func InitGenesis(ctx sdk.Context, ak AccountKeeper, bk types.BankKeeper, data GenesisState) {
func InitGenesis(ctx sdk.Context, ak AccountKeeper, data GenesisState) {
ak.SetParams(ctx, data.Params)
data.Accounts = SanitizeGenesisAccounts(data.Accounts)

Expand All @@ -19,7 +18,7 @@ func InitGenesis(ctx sdk.Context, ak AccountKeeper, bk types.BankKeeper, data Ge
ak.SetAccount(ctx, acc)
}

bk.GetModuleAccount(ctx, FeeCollectorName)
ak.GetModuleAccount(ctx, FeeCollectorName)
}

// ExportGenesis returns a GenesisState for a given context and keeper
Expand Down
78 changes: 78 additions & 0 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type AccountKeeper struct {
key sdk.StoreKey
cdc types.Codec
paramSubspace paramtypes.Subspace
permAddrs map[string]types.PermissionsForAddress

// The prototypical Account constructor.
proto func() exported.Account
Expand All @@ -29,18 +30,25 @@ type AccountKeeper struct {
// (binary) encode and decode concrete sdk.Accounts.
func NewAccountKeeper(
cdc types.Codec, key sdk.StoreKey, paramstore paramtypes.Subspace, proto func() exported.Account,
maccPerms map[string][]string,
) AccountKeeper {

// set KeyTable if it has not already been set
if !paramstore.HasKeyTable() {
paramstore = paramstore.WithKeyTable(types.ParamKeyTable())
}

permAddrs := make(map[string]types.PermissionsForAddress)
for name, perms := range maccPerms {
permAddrs[name] = types.NewPermissionsForAddress(name, perms)
}

return AccountKeeper{
key: key,
proto: proto,
cdc: cdc,
paramSubspace: paramstore,
permAddrs: permAddrs,
}
}

Expand Down Expand Up @@ -96,6 +104,76 @@ func (ak AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 {
return accNumber
}

// ValidatePermissions validates that the module account has been granted
// permissions within its set of allowed permissions.
func (ak AccountKeeper) ValidatePermissions(macc exported.ModuleAccountI) error {
permAddr := ak.permAddrs[macc.GetName()]
for _, perm := range macc.GetPermissions() {
if !permAddr.HasPermission(perm) {
return fmt.Errorf("invalid module permission %s", perm)
}
}

return nil
}

// GetModuleAddress returns an address based on the module name
func (ak AccountKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
permAddr, ok := ak.permAddrs[moduleName]
if !ok {
return nil
}

return permAddr.GetAddress()
}

// GetModuleAddressAndPermissions returns an address and permissions based on the module name
func (ak AccountKeeper) GetModuleAddressAndPermissions(moduleName string) (addr sdk.AccAddress, permissions []string) {
permAddr, ok := ak.permAddrs[moduleName]
if !ok {
return addr, permissions
}

return permAddr.GetAddress(), permAddr.GetPermissions()
}

// GetModuleAccountAndPermissions gets the module account from the auth account store and its
// registered permissions
func (ak AccountKeeper) GetModuleAccountAndPermissions(ctx sdk.Context, moduleName string) (exported.ModuleAccountI, []string) {
addr, perms := ak.GetModuleAddressAndPermissions(moduleName)
if addr == nil {
return nil, []string{}
}

acc := ak.GetAccount(ctx, addr)
if acc != nil {
macc, ok := acc.(exported.ModuleAccountI)
if !ok {
panic("account is not a module account")
}
return macc, perms
}

// create a new module account
macc := types.NewEmptyModuleAccount(moduleName, perms...)
maccI := (ak.NewAccount(ctx, macc)).(exported.ModuleAccountI) // set the account number
ak.SetModuleAccount(ctx, maccI)

return maccI, perms
}

// GetModuleAccount gets the module account from the auth account store, if the account does not
// exist in the AccountKeeper, then it is created.
func (ak AccountKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) exported.ModuleAccountI {
acc, _ := ak.GetModuleAccountAndPermissions(ctx, moduleName)
return acc
}

// SetModuleAccount sets the module account to the auth account store
func (ak AccountKeeper) SetModuleAccount(ctx sdk.Context, macc exported.ModuleAccountI) { //nolint:interfacer
ak.SetAccount(ctx, macc)
}

func (ak AccountKeeper) decodeAccount(bz []byte) exported.Account {
acc, err := ak.cdc.UnmarshalAccount(bz)
if err != nil {
Expand Down
Loading

0 comments on commit fea2315

Please sign in to comment.