Skip to content

Commit

Permalink
refactor: Move x/auth's AccountI and ModuleAccountI interfaces to typ…
Browse files Browse the repository at this point in the history
…es package (#13937)
  • Loading branch information
likhita-809 authored Jan 3, 2023
1 parent 03196d7 commit 3606995
Show file tree
Hide file tree
Showing 67 changed files with 394 additions and 381 deletions.
1 change: 1 addition & 0 deletions simapp/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"cosmossdk.io/math"
simappparams "cosmossdk.io/simapp/params"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e/auth/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (s *E2ETestSuite) TestCLISignAminoJSON() {
// query account info
queryResJSON, err := authclitestutil.QueryAccountExec(val1.ClientCtx, val1.Address)
require.NoError(err)
var account authtypes.AccountI
var account sdk.AccountI
require.NoError(val1.ClientCtx.Codec.UnmarshalInterfaceJSON(queryResJSON.Bytes(), &account))

/**** test signature-only ****/
Expand Down Expand Up @@ -1327,7 +1327,7 @@ func (s *E2ETestSuite) TestMultisignBatch() {

queryResJSON, err := authclitestutil.QueryAccountExec(val.ClientCtx, addr)
s.Require().NoError(err)
var account authtypes.AccountI
var account sdk.AccountI
s.Require().NoError(val.ClientCtx.Codec.UnmarshalInterfaceJSON(queryResJSON.Bytes(), &account))

// sign-batch file
Expand Down Expand Up @@ -1398,7 +1398,7 @@ func (s *E2ETestSuite) TestGetAccountCmd() {
s.Require().Error(err)
s.Require().NotEqual("internal", err.Error())
} else {
var acc authtypes.AccountI
var acc sdk.AccountI
s.Require().NoError(val.ClientCtx.Codec.UnmarshalInterfaceJSON(out.Bytes(), &acc))
s.Require().Equal(val.Address, acc.GetAddress())
}
Expand Down Expand Up @@ -1456,11 +1456,11 @@ func (s *E2ETestSuite) TestQueryModuleAccountByNameCmd() {
var res authtypes.QueryModuleAccountByNameResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))

var account authtypes.AccountI
var account sdk.AccountI
err := val.ClientCtx.InterfaceRegistry.UnpackAny(res.Account, &account)
s.Require().NoError(err)

moduleAccount, ok := account.(authtypes.ModuleAccountI)
moduleAccount, ok := account.(sdk.ModuleAccountI)
s.Require().True(ok)
s.Require().Equal(tc.moduleName, moduleAccount.GetName())
}
Expand Down
1 change: 1 addition & 0 deletions tools/rosetta/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

crgerrs "cosmossdk.io/tools/rosetta/lib/errors"
crgtypes "cosmossdk.io/tools/rosetta/lib/types"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down
42 changes: 42 additions & 0 deletions types/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types

import (
"github.com/cosmos/gogoproto/proto"

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

// AccountI is an interface used to store coins at a given address within state.
// It presumes a notion of sequence numbers for replay protection,
// a notion of account numbers for replay protection for previously pruned accounts,
// and a pubkey for authentication purposes.
//
// Many complex conditions can be used in the concrete struct which implements AccountI.
type AccountI interface {
proto.Message

GetAddress() AccAddress
SetAddress(AccAddress) error // errors if already set.

GetPubKey() cryptotypes.PubKey // can return nil.
SetPubKey(cryptotypes.PubKey) error

GetAccountNumber() uint64
SetAccountNumber(uint64) error

GetSequence() uint64
SetSequence(uint64) error

// Ensure that account implements stringer
String() string
}

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

GetName() string
GetPermissions() []string
HasPermission(string) bool
}
4 changes: 2 additions & 2 deletions x/auth/ante/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
// Interface provides support to use non-sdk AccountKeeper for AnteHandler's decorators.
type AccountKeeper interface {
GetParams(ctx sdk.Context) (params types.Params)
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
SetAccount(ctx sdk.Context, acc types.AccountI)
GetAccount(ctx sdk.Context, addr sdk.AccAddress) sdk.AccountI
SetAccount(ctx sdk.Context, acc sdk.AccountI)
GetModuleAddress(moduleName string) sdk.AccAddress
}

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 @@ -122,7 +122,7 @@ func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee
}

// DeductFees deducts fees from the given account.
func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc types.AccountI, fees sdk.Coins) error {
func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc sdk.AccountI, fees sdk.Coins) error {
if !fees.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "invalid fee amount: %s", fees)
}
Expand Down
2 changes: 1 addition & 1 deletion x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func ConsumeMultisignatureVerificationGas(

// GetSignerAcc returns an account for a given address that is expected to sign
// a transaction.
func GetSignerAcc(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) (types.AccountI, error) {
func GetSignerAcc(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) (sdk.AccountI, error) {
if acc := ak.GetAccount(ctx, addr); acc != nil {
return acc, nil
}
Expand Down
6 changes: 3 additions & 3 deletions x/auth/ante/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/auth/ante/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// TestAccount represents an account used in the tests in x/auth/ante.
type TestAccount struct {
acc types.AccountI
acc sdk.AccountI
priv cryptotypes.PrivKey
}

Expand Down
16 changes: 8 additions & 8 deletions x/auth/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// NewAccountWithAddress implements AccountKeeperI.
func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) types.AccountI {
func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) sdk.AccountI {
acc := ak.proto()
err := acc.SetAddress(addr)
if err != nil {
Expand All @@ -17,7 +17,7 @@ func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddre
}

// NewAccount sets the next account number to a given account interface
func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc types.AccountI) types.AccountI {
func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc sdk.AccountI) sdk.AccountI {
if err := acc.SetAccountNumber(ak.NextAccountNumber(ctx)); err != nil {
panic(err)
}
Expand All @@ -38,7 +38,7 @@ func (ak AccountKeeper) HasAccountAddressByID(ctx sdk.Context, id uint64) bool {
}

// GetAccount implements AccountKeeperI.
func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI {
func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) sdk.AccountI {
store := ctx.KVStore(ak.storeKey)
bz := store.Get(types.AddressStoreKey(addr))
if bz == nil {
Expand All @@ -59,8 +59,8 @@ func (ak AccountKeeper) GetAccountAddressByID(ctx sdk.Context, id uint64) string
}

// GetAllAccounts returns all accounts in the accountKeeper.
func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []types.AccountI) {
ak.IterateAccounts(ctx, func(acc types.AccountI) (stop bool) {
func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []sdk.AccountI) {
ak.IterateAccounts(ctx, func(acc sdk.AccountI) (stop bool) {
accounts = append(accounts, acc)
return false
})
Expand All @@ -69,7 +69,7 @@ func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []types.Accoun
}

// SetAccount implements AccountKeeperI.
func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc types.AccountI) {
func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc sdk.AccountI) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.storeKey)

Expand All @@ -84,7 +84,7 @@ func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc types.AccountI) {

// RemoveAccount removes an account for the account mapper store.
// NOTE: this will cause supply invariant violation if called
func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc types.AccountI) {
func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc sdk.AccountI) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.storeKey)
store.Delete(types.AddressStoreKey(addr))
Expand All @@ -93,7 +93,7 @@ func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc types.AccountI) {

// IterateAccounts iterates over all the stored accounts and performs a callback function.
// Stops iteration when callback returns true.
func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account types.AccountI) (stop bool)) {
func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account sdk.AccountI) (stop bool)) {
store := ctx.KVStore(ak.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix)

Expand Down
10 changes: 5 additions & 5 deletions x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (suite *DeterministicTestSuite) SetupTest() {
}

// createAndSetAccount creates a random account and sets to the keeper store.
func (suite *DeterministicTestSuite) createAndSetAccounts(t *rapid.T, count int) []types.AccountI {
accs := make([]types.AccountI, 0, count)
func (suite *DeterministicTestSuite) createAndSetAccounts(t *rapid.T, count int) []sdk.AccountI {
accs := make([]sdk.AccountI, 0, count)

// We need all generated account-numbers unique
accNums := rapid.SliceOfNDistinct(rapid.Uint64(), count, count, func(i uint64) uint64 { return i }).Draw(t, "acc-numss")
Expand Down Expand Up @@ -239,12 +239,12 @@ func (suite *DeterministicTestSuite) createAndReturnQueryClient(ak keeper.Accoun

func (suite *DeterministicTestSuite) setModuleAccounts(
ctx sdk.Context, ak keeper.AccountKeeper, maccs []string,
) []types.AccountI {
) []sdk.AccountI {
sort.Strings(maccs)
moduleAccounts := make([]types.AccountI, 0, len(maccs))
moduleAccounts := make([]sdk.AccountI, 0, len(maccs))
for _, m := range maccs {
acc, _ := ak.GetModuleAccountAndPermissions(ctx, m)
acc1, ok := acc.(types.AccountI)
acc1, ok := acc.(sdk.AccountI)
suite.Require().True(ok)
moduleAccounts = append(moduleAccounts, acc1)
}
Expand Down
2 changes: 1 addition & 1 deletion x/auth/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (ak AccountKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
params := ak.GetParams(ctx)

var genAccounts types.GenesisAccounts
ak.IterateAccounts(ctx, func(account types.AccountI) bool {
ak.IterateAccounts(ctx, func(account sdk.AccountI) bool {
genAccount := account.(types.GenesisAccount)
genAccounts = append(genAccounts, genAccount)
return false
Expand Down
20 changes: 10 additions & 10 deletions x/auth/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccounts() {
func(res *types.QueryAccountsResponse) {
addresses := make([]sdk.AccAddress, len(res.Accounts))
for i, acc := range res.Accounts {
var account types.AccountI
var account sdk.AccountI
err := suite.encCfg.InterfaceRegistry.UnpackAny(acc, &account)
suite.Require().NoError(err)
addresses[i] = account.GetAddress()
Expand Down Expand Up @@ -123,7 +123,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccount() {
},
true,
func(res *types.QueryAccountResponse) {
var newAccount types.AccountI
var newAccount sdk.AccountI
err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Account, &newAccount)
suite.Require().NoError(err)
suite.Require().NotNil(newAccount)
Expand Down Expand Up @@ -280,11 +280,11 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() {
func(res *types.QueryModuleAccountsResponse) {
mintModuleExists := false
for _, acc := range res.Accounts {
var account types.AccountI
var account sdk.AccountI
err := suite.encCfg.InterfaceRegistry.UnpackAny(acc, &account)
suite.Require().NoError(err)

moduleAccount, ok := account.(types.ModuleAccountI)
moduleAccount, ok := account.(sdk.ModuleAccountI)

suite.Require().True(ok)
if moduleAccount.GetName() == "mint" {
Expand All @@ -303,11 +303,11 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() {
func(res *types.QueryModuleAccountsResponse) {
mintModuleExists := false
for _, acc := range res.Accounts {
var account types.AccountI
var account sdk.AccountI
err := suite.encCfg.InterfaceRegistry.UnpackAny(acc, &account)
suite.Require().NoError(err)

moduleAccount, ok := account.(types.ModuleAccountI)
moduleAccount, ok := account.(sdk.ModuleAccountI)

suite.Require().True(ok)
if moduleAccount.GetName() == "falseCase" {
Expand All @@ -332,10 +332,10 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() {
// Make sure output is sorted alphabetically.
var moduleNames []string
for _, any := range res.Accounts {
var account types.AccountI
var account sdk.AccountI
err := suite.encCfg.InterfaceRegistry.UnpackAny(any, &account)
suite.Require().NoError(err)
moduleAccount, ok := account.(types.ModuleAccountI)
moduleAccount, ok := account.(sdk.ModuleAccountI)
suite.Require().True(ok)
moduleNames = append(moduleNames, moduleAccount.GetName())
}
Expand Down Expand Up @@ -366,11 +366,11 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccountByName() {
},
true,
func(res *types.QueryModuleAccountByNameResponse) {
var account types.AccountI
var account sdk.AccountI
err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Account, &account)
suite.Require().NoError(err)

moduleAccount, ok := account.(types.ModuleAccountI)
moduleAccount, ok := account.(sdk.ModuleAccountI)
suite.Require().True(ok)
suite.Require().Equal(moduleAccount.GetName(), "mint")
},
Expand Down
Loading

0 comments on commit 3606995

Please sign in to comment.