cosmos-sdk/x/auth/test_common.go

141 lines
4.1 KiB
Go
Raw Normal View History

// nolint
package auth
import (
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/params/subspace"
2019-06-28 13:11:27 -07:00
"github.com/cosmos/cosmos-sdk/x/supply/exported"
)
type testInput struct {
cdc *codec.Codec
ctx sdk.Context
ak AccountKeeper
2019-06-28 13:11:27 -07:00
sk types.SupplyKeeper
}
// moduleAccount defines an account for modules that holds coins on a pool
type moduleAccount struct {
*types.BaseAccount
2019-07-05 16:25:56 -07:00
Name string `json:"name" yaml:"name"` // name of the module
Permission string `json:"permission" yaml"permission"` // permission of module account (minter/burner/holder)
}
// GetName returns the the name of the holder's module
func (ma moduleAccount) GetName() string {
return ma.Name
}
// GetPermission returns permission granted to the module account (holder/minter/burner)
func (ma moduleAccount) GetPermission() string {
return ma.Permission
}
func setupTestInput() testInput {
db := dbm.NewMemDB()
cdc := codec.New()
2019-06-28 13:11:27 -07:00
types.RegisterCodec(cdc)
cdc.RegisterInterface((*exported.ModuleAccountI)(nil), nil)
cdc.RegisterConcrete(&moduleAccount{}, "cosmos-sdk/ModuleAccount", nil)
2019-06-28 13:11:27 -07:00
codec.RegisterCrypto(cdc)
authCapKey := sdk.NewKVStoreKey("authCapKey")
keyParams := sdk.NewKVStoreKey("subspace")
tkeyParams := sdk.NewTransientStoreKey("transient_subspace")
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(authCapKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
ms.LoadLatestVersion()
ps := subspace.NewSubspace(cdc, keyParams, tkeyParams, types.DefaultParamspace)
ak := NewAccountKeeper(cdc, authCapKey, ps, types.ProtoBaseAccount)
2019-06-28 13:11:27 -07:00
sk := NewDummySupplyKeeper(ak)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "test-chain-id"}, false, log.NewNopLogger())
ak.SetParams(ctx, types.DefaultParams())
2019-06-28 13:11:27 -07:00
return testInput{cdc: cdc, ctx: ctx, ak: ak, sk: sk}
}
// DummySupplyKeeper defines a supply keeper used only for testing to avoid
// circle dependencies
type DummySupplyKeeper struct {
ak AccountKeeper
}
// NewDummySupplyKeeper creates a DummySupplyKeeper instance
func NewDummySupplyKeeper(ak AccountKeeper) DummySupplyKeeper {
return DummySupplyKeeper{ak}
}
// SendCoinsFromAccountToModule for the dummy supply keeper
func (sk DummySupplyKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, fromAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) sdk.Error {
fromAcc := sk.ak.GetAccount(ctx, fromAddr)
moduleAcc := sk.GetModuleAccount(ctx, recipientModule)
newFromCoins, hasNeg := fromAcc.GetCoins().SafeSub(amt)
if hasNeg {
return sdk.ErrInsufficientCoins(fromAcc.GetCoins().String())
}
newToCoins := moduleAcc.GetCoins().Add(amt)
if err := fromAcc.SetCoins(newFromCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
if err := moduleAcc.SetCoins(newToCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
sk.ak.SetAccount(ctx, fromAcc)
sk.ak.SetAccount(ctx, moduleAcc)
return nil
}
// GetModuleAccount for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) exported.ModuleAccountI {
addr := sk.GetModuleAddress(moduleName)
acc := sk.ak.GetAccount(ctx, addr)
if acc != nil {
macc, ok := acc.(exported.ModuleAccountI)
if ok {
return macc
}
}
moduleAddress := sk.GetModuleAddress(moduleName)
baseAcc := types.NewBaseAccountWithAddress(moduleAddress)
2019-06-28 13:11:27 -07:00
// create a new module account
macc := &moduleAccount{
BaseAccount: &baseAcc,
Name: moduleName,
Permission: "basic",
}
2019-06-28 13:11:27 -07:00
maccI := (sk.ak.NewAccount(ctx, macc)).(exported.ModuleAccountI)
sk.ak.SetAccount(ctx, maccI)
return maccI
}
// GetModuleAddress for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(moduleName)))
}