Skip to content

Commit

Permalink
fixed UpdateAuth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freeelancer committed Mar 7, 2024
1 parent 07092bc commit b68ffad
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 91 deletions.
32 changes: 12 additions & 20 deletions x/smartaccount/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,23 @@ import (

"github.com/stretchr/testify/suite"

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

"github.com/terra-money/core/v2/app/test_helpers"
"github.com/terra-money/core/v2/x/feeshare/types"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/terra-money/core/v2/x/smartaccount/keeper"
"github.com/terra-money/core/v2/x/smartaccount/test_helpers"
"github.com/terra-money/core/v2/x/smartaccount/types"
)

// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
SendCoins(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error
}

type IntegrationTestSuite struct {
test_helpers.AppTestSuite

queryClient types.QueryClient
test_helpers.SmartAccountTestSuite
msgServer types.MsgServer
wasmKeeper *wasmkeeper.PermissionedKeeper
}

func (s *IntegrationTestSuite) SetupTest() {
s.Setup()

s.queryClient = types.NewQueryClient(s.QueryHelper)
func (s *IntegrationTestSuite) Setup() {
s.SmartAccountTestSuite.SetupTests()
s.msgServer = keeper.NewMsgServer(s.SmartAccountKeeper)
s.wasmKeeper = wasmkeeper.NewDefaultPermissionKeeper(s.App.Keepers.WasmKeeper)
s.Ctx = s.Ctx.WithChainID("test")
}

func TestKeeperTestSuite(t *testing.T) {
Expand Down
24 changes: 15 additions & 9 deletions x/smartaccount/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
"github.com/terra-money/core/v2/x/smartaccount/types"
)

type MsgServer struct {
var _ types.MsgServer = msgServer{}

type msgServer struct {
k Keeper
}

// NewMsgServer returns the MsgServer implementation.
func NewMsgServer(k Keeper) types.MsgServer {
return &MsgServer{k}
return &msgServer{k}
}

func (ms MsgServer) CreateSmartAccount(
func (ms msgServer) CreateSmartAccount(
goCtx context.Context, msg *types.MsgCreateSmartAccount,
) (*types.MsgCreateSmartAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand All @@ -38,7 +40,7 @@ func (ms MsgServer) CreateSmartAccount(
return &types.MsgCreateSmartAccountResponse{}, nil
}

func (ms MsgServer) UpdateAuthorization(
func (ms msgServer) UpdateAuthorization(
goCtx context.Context, msg *types.MsgUpdateAuthorization,
) (*types.MsgUpdateAuthorizationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand All @@ -52,16 +54,20 @@ func (ms MsgServer) UpdateAuthorization(
return nil, err
}
setting.Authorization = msg.AuthorizationMsgs
// TODO: check if this is right
for _, auth := range msg.AuthorizationMsgs {
sudoInitMsg := types.SudoMsg{Initialization: auth.InitMsg}
sudoInitMsgBs, err := json.Marshal(sudoInitMsg)
if err != nil {
return nil, err
}

contractAddress := sdk.AccAddress(auth.ContractAddress)
ms.k.wasmKeeper.Sudo(ctx, contractAddress, sudoInitMsgBs)
contractAddress, err := sdk.AccAddressFromBech32(auth.ContractAddress)
if err != nil {
return nil, err
}
if _, err := ms.k.wasmKeeper.Sudo(ctx, contractAddress, sudoInitMsgBs); err != nil {
return nil, err
}
}
setting.Fallback = msg.Fallback
if err := ms.k.SetSetting(ctx, *setting); err != nil {
Expand All @@ -70,7 +76,7 @@ func (ms MsgServer) UpdateAuthorization(
return &types.MsgUpdateAuthorizationResponse{}, nil
}

func (ms MsgServer) UpdateTransactionHooks(
func (ms msgServer) UpdateTransactionHooks(
goCtx context.Context, msg *types.MsgUpdateTransactionHooks,
) (*types.MsgUpdateTransactionHooksResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand All @@ -91,7 +97,7 @@ func (ms MsgServer) UpdateTransactionHooks(
}

// DisableSmartAccount converts smart acc back to a basic acc
func (ms MsgServer) DisableSmartAccount(
func (ms msgServer) DisableSmartAccount(
goCtx context.Context, msg *types.MsgDisableSmartAccount,
) (*types.MsgDisableSmartAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
119 changes: 85 additions & 34 deletions x/smartaccount/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package keeper_test

import (
smartaccountkeeper "github.com/terra-money/core/v2/x/smartaccount/keeper"
"encoding/base64"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/terra-money/core/v2/x/smartaccount/test_helpers"
"github.com/terra-money/core/v2/x/smartaccount/types"
)

Expand All @@ -15,22 +18,21 @@ func (s *IntegrationTestSuite) TestMsgCreateAndDisableSmartAccount() {
s.Require().Error(err)

// Create a smart account
ms := smartaccountkeeper.NewMsgServer(s.App.Keepers.SmartAccountKeeper)
msg := types.NewMsgCreateSmartAccount(sender.String())
_, err = ms.CreateSmartAccount(s.Ctx, msg)
_, err = s.msgServer.CreateSmartAccount(s.Ctx, msg)
s.Require().NoError(err)

// Ensure that the smart account was created
_, err = s.App.Keepers.SmartAccountKeeper.GetSetting(s.Ctx, sender.String())
s.Require().NoError(err)

// Ensure that the smart account cannot be created again
_, err = ms.CreateSmartAccount(s.Ctx, msg)
_, err = s.msgServer.CreateSmartAccount(s.Ctx, msg)
s.Require().Error(err)

// Disable the smart account
msgDisable := types.NewMsgDisableSmartAccount(sender.String())
_, err = ms.DisableSmartAccount(s.Ctx, msgDisable)
_, err = s.msgServer.DisableSmartAccount(s.Ctx, msgDisable)
s.Require().NoError(err)

// Ensure that the smart account was disabled
Expand All @@ -40,53 +42,76 @@ func (s *IntegrationTestSuite) TestMsgCreateAndDisableSmartAccount() {

func (s *IntegrationTestSuite) TestMsgUpdateAuthorization() {
s.Setup()
sender := s.TestAccs[0]

// Create a smart account
ms := smartaccountkeeper.NewMsgServer(s.App.Keepers.SmartAccountKeeper)
msg := types.NewMsgCreateSmartAccount(sender.String())
_, err := ms.CreateSmartAccount(s.Ctx, msg)
// create smart account 1
acc := s.TestAccs[1]
msg := types.NewMsgCreateSmartAccount(acc.String())
_, err := s.msgServer.CreateSmartAccount(s.Ctx, msg)
s.Require().NoError(err)

// update authorization
authorization := types.AuthorizationMsg{
ContractAddress: "abc",
InitMsg: &types.Initialization{},
// testAcc1 using private key of testAcc0
pubKey := s.TestAccPrivs[0].PubKey()
// endcoding this since this should be encoded in base64 when submitted by the user
pkEncoded := []byte(base64.StdEncoding.EncodeToString(pubKey.Bytes()))

codeId, _, err := s.wasmKeeper.Create(s.Ctx, acc, test_helpers.SmartAuthContractWasm, nil)
require.NoError(s.T(), err)
contractAddr, _, err := s.wasmKeeper.Instantiate(s.Ctx, codeId, acc, acc, []byte("{}"), "auth", sdk.NewCoins())
require.NoError(s.T(), err)

// create updateAuth msg
initMsg := types.Initialization{
Senders: []string{},
Account: acc.String(),
Msg: pkEncoded,
}
msgUpdate := types.NewMsgUpdateAuthorization(sender.String(), []*types.AuthorizationMsg{&authorization}, true)
_, err = ms.UpdateAuthorization(s.Ctx, msgUpdate)
s.Require().NoError(err)
authMsg := &types.AuthorizationMsg{
ContractAddress: contractAddr.String(),
InitMsg: &initMsg,
}
_, err = s.msgServer.UpdateAuthorization(s.Ctx, types.NewMsgUpdateAuthorization(
acc.String(),
[]*types.AuthorizationMsg{authMsg},
false,
))
require.NoError(s.T(), err)

// Ensure that the smart account was updated
setting, err := s.App.Keepers.SmartAccountKeeper.GetSetting(s.Ctx, sender.String())
setting, err := s.App.Keepers.SmartAccountKeeper.GetSetting(s.Ctx, acc.String())
s.Require().NoError(err)
s.Require().Equal(sender.String(), setting.Owner)
s.Require().Equal([]*types.AuthorizationMsg{&authorization}, setting.Authorization)
s.Require().Equal(acc.String(), setting.Owner)
s.CheckAuthorizationEqual([]*types.AuthorizationMsg{authMsg}, setting.Authorization)

// update authorization again
authorization2 := types.AuthorizationMsg{
ContractAddress: "bbc",
InitMsg: &types.Initialization{},
// deploy another auth contract
contractAddr2, _, err := s.wasmKeeper.Instantiate(s.Ctx, codeId, acc, acc, []byte("{}"), "auth", sdk.NewCoins())
require.NoError(s.T(), err)

// create updateAuth msg
authMsg2 := &types.AuthorizationMsg{
ContractAddress: contractAddr2.String(),
InitMsg: &initMsg,
}
msgUpdate2 := types.NewMsgUpdateAuthorization(sender.String(), []*types.AuthorizationMsg{&authorization2}, true)
_, err = ms.UpdateAuthorization(s.Ctx, msgUpdate2)
s.Require().NoError(err)
_, err = s.msgServer.UpdateAuthorization(s.Ctx, types.NewMsgUpdateAuthorization(
acc.String(),
[]*types.AuthorizationMsg{authMsg2},
false,
))
require.NoError(s.T(), err)

// Ensure that the smart account was updated again
setting, err = s.App.Keepers.SmartAccountKeeper.GetSetting(s.Ctx, sender.String())
setting, err = s.App.Keepers.SmartAccountKeeper.GetSetting(s.Ctx, acc.String())
s.Require().NoError(err)
s.Require().Equal(sender.String(), setting.Owner)
s.Require().Equal([]*types.AuthorizationMsg{&authorization2}, setting.Authorization)
s.Require().Equal(acc.String(), setting.Owner)
s.CheckAuthorizationEqual([]*types.AuthorizationMsg{authMsg2}, setting.Authorization)
}

func (s *IntegrationTestSuite) TestMsgUpdateTransactionHooks() {
s.Setup()
sender := s.TestAccs[0]

// Create a smart account
ms := smartaccountkeeper.NewMsgServer(s.App.Keepers.SmartAccountKeeper)
msg := types.NewMsgCreateSmartAccount(sender.String())
_, err := ms.CreateSmartAccount(s.Ctx, msg)
_, err := s.msgServer.CreateSmartAccount(s.Ctx, msg)
s.Require().NoError(err)

// update transaction hooks
Expand All @@ -97,7 +122,7 @@ func (s *IntegrationTestSuite) TestMsgUpdateTransactionHooks() {
pretx,
posttx,
)
_, err = ms.UpdateTransactionHooks(s.Ctx, msgUpdate)
_, err = s.msgServer.UpdateTransactionHooks(s.Ctx, msgUpdate)
s.Require().NoError(err)

// Ensure that the smart account was updated
Expand All @@ -115,7 +140,7 @@ func (s *IntegrationTestSuite) TestMsgUpdateTransactionHooks() {
pretx,
posttx,
)
_, err = ms.UpdateTransactionHooks(s.Ctx, msgUpdate)
_, err = s.msgServer.UpdateTransactionHooks(s.Ctx, msgUpdate)
s.Require().NoError(err)

// Ensure that the smart account was updated again
Expand All @@ -125,3 +150,29 @@ func (s *IntegrationTestSuite) TestMsgUpdateTransactionHooks() {
s.Require().Equal(pretx, setting.PreTransaction)
s.Require().Equal(posttx, setting.PostTransaction)
}

func (s *IntegrationTestSuite) CheckSettingEqual(a types.Setting, b types.Setting) {
s.Require().Equal(a.Owner, b.Owner)
s.Require().Equal(a.Fallback, b.Fallback)
s.CheckAuthorizationEqual(a.Authorization, b.Authorization)
s.Require().Equal(a.PreTransaction, b.PreTransaction)
s.Require().Equal(a.PostTransaction, b.PostTransaction)
}

func (s *IntegrationTestSuite) CheckAuthorizationEqual(a []*types.AuthorizationMsg, b []*types.AuthorizationMsg) {
s.Require().Equal(len(a), len(b))
for i := range a {
s.Require().Equal(a[i].ContractAddress, b[i].ContractAddress)
s.Require().Equal(a[i].InitMsg.Msg, b[i].InitMsg.Msg)
s.Require().Equal(a[i].InitMsg.Account, b[i].InitMsg.Account)
if a[i].InitMsg.Senders == nil && b[i].InitMsg.Senders == nil {
return
} else if a[i].InitMsg.Senders == nil {
s.Require().Len(b[i].InitMsg.Senders, 0)
} else if b[i].InitMsg.Senders == nil {
s.Require().Len(a[i].InitMsg.Senders, 0)
} else {
s.Require().Equal(a[i].InitMsg.Senders, b[i].InitMsg.Senders)
}
}
}
3 changes: 0 additions & 3 deletions x/smartaccount/test_helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package test_helpers
import (
"github.com/terra-money/core/v2/app/test_helpers"
"github.com/terra-money/core/v2/x/smartaccount/keeper"
wasmkeeper "github.com/terra-money/core/v2/x/wasm/keeper"
)

type SmartAccountTestSuite struct {
test_helpers.AppTestSuite

SmartAccountKeeper keeper.Keeper
WasmKeeper wasmkeeper.Keeper
}

func (s *SmartAccountTestSuite) SetupTests() {
s.Setup()
s.SmartAccountKeeper = s.App.Keepers.SmartAccountKeeper
s.WasmKeeper = s.App.Keepers.WasmKeeper
}
25 changes: 0 additions & 25 deletions x/smartaccount/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import sdk "github.com/cosmos/cosmos-sdk/types"
var (
_ sdk.Msg = &MsgCreateSmartAccount{}
_ sdk.Msg = &MsgDisableSmartAccount{}
_ sdk.Msg = &MsgUpdateAuthorization{}
_ sdk.Msg = &MsgUpdateTransactionHooks{}
)

Expand All @@ -21,14 +20,6 @@ func NewMsgDisableSmartAccount(account string) *MsgDisableSmartAccount {
}
}

func NewMsgUpdateAuthorization(account string, authorizationMsgs []*AuthorizationMsg, fallback bool) *MsgUpdateAuthorization {
return &MsgUpdateAuthorization{
Account: account,
AuthorizationMsgs: authorizationMsgs,
Fallback: fallback,
}
}

func NewMsgUpdateTransactionHooks(account string, preTransactionHooks, postTransactionHooks []string) *MsgUpdateTransactionHooks {
return &MsgUpdateTransactionHooks{
Account: account,
Expand Down Expand Up @@ -69,22 +60,6 @@ func (m MsgDisableSmartAccount) ValidateBasic() error {
return nil
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateAuthorization) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}

// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m MsgUpdateAuthorization) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.Account)
return []sdk.AccAddress{addr}
}

// ValidateBasic does a sanity check on the provided data.
func (m MsgUpdateAuthorization) ValidateBasic() error {
return nil
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateTransactionHooks) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
Expand Down
Loading

0 comments on commit b68ffad

Please sign in to comment.