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

chore: moving stateless GenerateAddress func to types #352

Merged
merged 1 commit into from
Aug 27, 2021
Merged
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
13 changes: 3 additions & 10 deletions modules/apps/27-interchain-accounts/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tendermint/tendermint/crypto/tmhash"

"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
Expand Down Expand Up @@ -45,9 +44,9 @@ func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpart

// Register interchain account if it has not already been created
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portId string) {
address := k.GenerateAddress(portId)
account := k.accountKeeper.GetAccount(ctx, address)
address := types.GenerateAddress(portId)

account := k.accountKeeper.GetAccount(ctx, address)
if account != nil {
// account already created, return no-op
return
Expand All @@ -57,11 +56,10 @@ func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portId string) {
authtypes.NewBaseAccountWithAddress(address),
portId,
)

k.accountKeeper.NewAccount(ctx, interchainAccount)
k.accountKeeper.SetAccount(ctx, interchainAccount)
_ = k.SetInterchainAccountAddress(ctx, portId, interchainAccount.Address)

return
}

func (k Keeper) SetInterchainAccountAddress(ctx sdk.Context, portId string, address string) string {
Expand All @@ -82,11 +80,6 @@ func (k Keeper) GetInterchainAccountAddress(ctx sdk.Context, portId string) (str
return interchainAccountAddr, nil
}

// Determine account's address that will be created.
func (k Keeper) GenerateAddress(identifier string) []byte {
return tmhash.SumTruncated(append([]byte(identifier)))
}

func (k Keeper) GetInterchainAccount(ctx sdk.Context, addr sdk.AccAddress) (types.InterchainAccount, error) {
acc := k.accountKeeper.GetAccount(ctx, addr)
if acc == nil {
Expand Down
6 changes: 6 additions & 0 deletions modules/apps/27-interchain-accounts/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tendermint/tendermint/crypto/tmhash"

connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types"
)
Expand All @@ -19,6 +20,11 @@ const (
ICAPrefix string = "ics-27"
)

// GenerateAddress returns a truncated SHA256 hash using the provided port string
func GenerateAddress(port string) []byte {
return tmhash.SumTruncated([]byte(port))
}

// GeneratePortID generates the portID for a specific owner
// on the controller chain in the format:
//
Expand Down