Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/auth: prefix account by type #8487

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions x/auth/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc types.AccountI) types.Ac
// GetAccount implements AccountKeeperI.
func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI {
store := ctx.KVStore(ak.key)
bz := store.Get(types.AddressStoreKey(addr))
bz := store.Get(types.AddressStoreKey("", addr)) //TODO: figure out how to pass in the type
Comment on lines 29 to +31
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure of how to proceed with this without making a breaking change. @alexanderbez suggested using bech32 HRP but the prefix is the same for different account types (base, vesting, module, etc). Any ideas? @amaurymartiny @aaronc @AdityaSripal @clevinson ?

if bz == nil {
return nil
}
Expand Down Expand Up @@ -56,15 +56,15 @@ func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc types.AccountI) {
panic(err)
}

store.Set(types.AddressStoreKey(addr), bz)
store.Set(types.AddressStoreKey(acc.Type(), addr), bz)
}

// 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) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.key)
store.Delete(types.AddressStoreKey(addr))
store.Delete(types.AddressStoreKey(acc.Type(), addr))
}

// IterateAccounts iterates over all the stored accounts and performs a callback function
Expand Down
2 changes: 1 addition & 1 deletion x/auth/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDecodeStore(t *testing.T) {
kvPairs := kv.Pairs{
Pairs: []kv.Pair{
{
Key: types.AddressStoreKey(delAddr1),
Key: types.AddressStoreKey(acc.Type(), delAddr1),
Value: accBz,
},
{
Expand Down
15 changes: 15 additions & 0 deletions x/auth/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,17 @@ func (acc BaseAccount) Validate() error {
return nil
}

// String implements the fmt.Stringer interface
func (acc BaseAccount) String() string {
out, _ := acc.MarshalYAML()
return out.(string)
}

// Type represents the registered Account proto type.
func (BaseAccount) Type() string {
return "cosmos.auth.v1beta1.BaseAccount"
}

// MarshalYAML returns the YAML representation of an account.
func (acc BaseAccount) MarshalYAML() (interface{}, error) {
bz, err := codec.MarshalYAML(codec.NewProtoCodec(codectypes.NewInterfaceRegistry()), &acc)
Expand Down Expand Up @@ -244,11 +250,17 @@ type moduleAccountPretty struct {
Permissions []string `json:"permissions" yaml:"permissions"`
}

// String implements the fmt.Stringer interface
func (ma ModuleAccount) String() string {
out, _ := ma.MarshalYAML()
return out.(string)
}

// Type represents the registered Account proto type.
func (ModuleAccount) Type() string {
return "cosmos.auth.v1beta1.ModuleAccount"
}

// MarshalYAML returns the YAML representation of a ModuleAccount.
func (ma ModuleAccount) MarshalYAML() (interface{}, error) {
accAddr, err := sdk.AccAddressFromBech32(ma.Address)
Expand Down Expand Up @@ -326,6 +338,9 @@ type AccountI interface {

// Ensure that account implements stringer
String() string

// Type represents the protobuf RegisterType string that is used for the account key
Type() string
}

// ModuleAccountI defines an account interface for modules that hold tokens in
Expand Down
12 changes: 8 additions & 4 deletions x/auth/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package types

import (
"crypto/sha256"

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

const (
// module name
// ModuleName is "auth"
ModuleName = "auth"

// StoreKey is string representation of the store key for auth
Expand All @@ -22,11 +24,13 @@ var (
// AddressStoreKeyPrefix prefix for account-by-address store
AddressStoreKeyPrefix = []byte{0x01}

// param key for global account number
// GlobalAccountNumberKey param key for global account number
GlobalAccountNumberKey = []byte("globalAccountNumber")
)

// AddressStoreKey turn an address to key used to get it from the account store
func AddressStoreKey(addr sdk.AccAddress) []byte {
return append(AddressStoreKeyPrefix, addr.Bytes()...)
func AddressStoreKey(accountType string, addr sdk.AccAddress) []byte {
typeHash := sha256.Sum256([]byte(accountType))
key := append(AddressStoreKeyPrefix, typeHash[:]...)
return append(key, addr.Bytes()...)
}
20 changes: 20 additions & 0 deletions x/auth/vesting/types/vesting_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ func (bva BaseVestingAccount) String() string {
return out.(string)
}

// Type represents the registered Account proto type.
func (BaseVestingAccount) Type() string {
return "cosmos.vesting.v1beta1.BaseVestingAccount"
}

// MarshalYAML returns the YAML representation of a BaseVestingAccount.
func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) {
accAddr, err := sdk.AccAddressFromBech32(bva.Address)
Expand Down Expand Up @@ -309,6 +314,11 @@ func (cva ContinuousVestingAccount) String() string {
return out.(string)
}

// Type represents the registered Account proto type.
func (ContinuousVestingAccount) Type() string {
return "cosmos.vesting.v1beta1.ContinuousVestingAccount"
}

// MarshalYAML returns the YAML representation of a ContinuousVestingAccount.
func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) {
accAddr, err := sdk.AccAddressFromBech32(cva.Address)
Expand Down Expand Up @@ -467,6 +477,11 @@ func (pva PeriodicVestingAccount) String() string {
return out.(string)
}

// Type represents the registered Account proto type.
func (PeriodicVestingAccount) Type() string {
return "cosmos.vesting.v1beta1.PeriodicVestingAccount"
}

// MarshalYAML returns the YAML representation of a PeriodicVestingAccount.
func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) {
accAddr, err := sdk.AccAddressFromBech32(pva.Address)
Expand Down Expand Up @@ -570,3 +585,8 @@ func (dva DelayedVestingAccount) String() string {
out, _ := dva.MarshalYAML()
return out.(string)
}

// Type represents the registered Account proto type.
func (DelayedVestingAccount) Type() string {
return "cosmos.vesting.v1beta1.DelayedVestingAccount"
}