diff --git a/tests/e2e/auth/suite.go b/tests/e2e/auth/suite.go index cee85a1be372..45e5dee231b7 100644 --- a/tests/e2e/auth/suite.go +++ b/tests/e2e/auth/suite.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "cosmossdk.io/core/address" "cosmossdk.io/depinject" "cosmossdk.io/math" authcli "cosmossdk.io/x/auth/client/cli" @@ -22,6 +23,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" @@ -40,6 +42,7 @@ type E2ETestSuite struct { suite.Suite cfg network.Config + ac address.Codec network network.NetworkI } @@ -75,6 +78,8 @@ func (s *E2ETestSuite) SetupSuite() { _, err = kb.SaveMultisig("multi", multi) s.Require().NoError(err) s.Require().NoError(s.network.WaitForNextBlock()) + + s.ac = addresscodec.NewBech32Codec("cosmos") } func (s *E2ETestSuite) TearDownSuite() { @@ -1302,11 +1307,7 @@ func TestGetBroadcastCommandWithoutOfflineFlag(t *testing.T) { // Create new file with tx builder := txCfg.NewTxBuilder() builder.SetGasLimit(200000) - from, err := sdk.AccAddressFromBech32("cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw") - require.NoError(t, err) - to, err := sdk.AccAddressFromBech32("cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw") - require.NoError(t, err) - err = builder.SetMsgs(banktypes.NewMsgSend(from, to, sdk.Coins{sdk.NewInt64Coin("stake", 10000)})) + err = builder.SetMsgs(banktypes.NewMsgSend("cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw", "cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw", sdk.Coins{sdk.NewInt64Coin("stake", 10000)})) require.NoError(t, err) txContents, err := txCfg.TxJSONEncoder()(builder.GetTx()) require.NoError(t, err) @@ -1330,7 +1331,7 @@ func (s *E2ETestSuite) TestTxWithoutPublicKey() { // Create a txBuilder with an unsigned tx. txBuilder := txCfg.NewTxBuilder() - msg := banktypes.NewMsgSend(val1.GetAddress(), val1.GetAddress(), sdk.NewCoins( + msg := banktypes.NewMsgSend(val1.GetAddress().String(), val1.GetAddress().String(), sdk.NewCoins( sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10)), )) err := txBuilder.SetMsgs(msg) @@ -1397,9 +1398,15 @@ func (s *E2ETestSuite) TestSignWithMultiSignersAminoJSON() { // because DIRECT doesn't support multi signers via the CLI. // Since we use amino, we don't need to pre-populate signer_infos. txBuilder := val0.GetClientCtx().TxConfig.NewTxBuilder() - err := txBuilder.SetMsgs( - banktypes.NewMsgSend(val0.GetAddress(), addr1, sdk.NewCoins(val0Coin)), - banktypes.NewMsgSend(val1.GetAddress(), addr1, sdk.NewCoins(val1Coin)), + val0Str, err := s.ac.BytesToString(val0.GetAddress()) + s.Require().NoError(err) + val1Str, err := s.ac.BytesToString(val1.GetAddress()) + s.Require().NoError(err) + addrStr, err := s.ac.BytesToString(addr1) + s.Require().NoError(err) + err = txBuilder.SetMsgs( + banktypes.NewMsgSend(val0Str, addrStr, sdk.NewCoins(val0Coin)), + banktypes.NewMsgSend(val1Str, addrStr, sdk.NewCoins(val1Coin)), ) require.NoError(err) txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10)))) diff --git a/tests/e2e/bank/suite.go b/tests/e2e/bank/suite.go index 2a2ebdaa1c1b..e717bd1f6a4b 100644 --- a/tests/e2e/bank/suite.go +++ b/tests/e2e/bank/suite.go @@ -6,12 +6,14 @@ import ( "github.com/cosmos/gogoproto/proto" "github.com/stretchr/testify/suite" + "cosmossdk.io/core/address" "cosmossdk.io/math" "cosmossdk.io/x/bank/client/cli" "cosmossdk.io/x/bank/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" @@ -23,6 +25,7 @@ type E2ETestSuite struct { suite.Suite cfg network.Config + ac address.Codec network network.NetworkI } @@ -85,6 +88,7 @@ func (s *E2ETestSuite) SetupSuite() { s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg) s.Require().NoError(err) s.Require().NoError(s.network.WaitForNextBlock()) + s.ac = addresscodec.NewBech32Codec("cosmos") } func (s *E2ETestSuite) TearDownSuite() { @@ -101,10 +105,13 @@ func (s *E2ETestSuite) TestNewSendTxCmdGenOnly() { sdk.NewCoin(fmt.Sprintf("%stoken", val.GetMoniker()), math.NewInt(10)), sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10)), ) - + fromStr, err := s.ac.BytesToString(from) + s.Require().NoError(err) + toStr, err := s.ac.BytesToString(to) + s.Require().NoError(err) msgSend := &types.MsgSend{ - FromAddress: from.String(), - ToAddress: to.String(), + FromAddress: fromStr, + ToAddress: toStr, Amount: amount, } @@ -120,7 +127,7 @@ func (s *E2ETestSuite) TestNewSendTxCmdGenOnly() { tx, err := s.cfg.TxConfig.TxJSONDecoder()(bz.Bytes()) s.Require().NoError(err) - s.Require().Equal([]sdk.Msg{types.NewMsgSend(from, to, amount)}, tx.GetMsgs()) + s.Require().Equal([]sdk.Msg{types.NewMsgSend(fromStr, toStr, amount)}, tx.GetMsgs()) } func (s *E2ETestSuite) TestNewSendTxCmdDryRun() { diff --git a/tests/integration/auth/client/cli/suite_test.go b/tests/integration/auth/client/cli/suite_test.go index 350cd833327d..7b1f59e4c14e 100644 --- a/tests/integration/auth/client/cli/suite_test.go +++ b/tests/integration/auth/client/cli/suite_test.go @@ -840,11 +840,8 @@ func (s *CLITestSuite) TestGetBroadcastCommandWithoutOfflineFlag() { // Create new file with tx builder := txCfg.NewTxBuilder() builder.SetGasLimit(200000) - from, err := s.ac.StringToBytes("cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw") - s.Require().NoError(err) - to, err := s.ac.StringToBytes("cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw") - s.Require().NoError(err) - err = builder.SetMsgs(banktypes.NewMsgSend(from, to, sdk.Coins{sdk.NewInt64Coin("stake", 10000)})) + + err := builder.SetMsgs(banktypes.NewMsgSend("cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw", "cosmos1cxlt8kznps92fwu3j6npahx4mjfutydyene2qw", sdk.Coins{sdk.NewInt64Coin("stake", 10000)})) s.Require().NoError(err) txContents, err := txCfg.TxJSONEncoder()(builder.GetTx()) s.Require().NoError(err) @@ -864,12 +861,15 @@ func (s *CLITestSuite) TestGetBroadcastCommandWithoutOfflineFlag() { func (s *CLITestSuite) TestTxWithoutPublicKey() { txCfg := s.clientCtx.TxConfig + valStr, err := s.ac.BytesToString(s.val) + s.Require().NoError(err) + // Create a txBuilder with an unsigned tx. txBuilder := txCfg.NewTxBuilder() - msg := banktypes.NewMsgSend(s.val, s.val, sdk.NewCoins( + msg := banktypes.NewMsgSend(valStr, valStr, sdk.NewCoins( sdk.NewCoin("Stake", math.NewInt(10)), )) - err := txBuilder.SetMsgs(msg) + err = txBuilder.SetMsgs(msg) s.Require().NoError(err) txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("Stake", math.NewInt(150)))) txBuilder.SetGasLimit(testdata.NewTestGasLimit()) @@ -917,14 +917,21 @@ func (s *CLITestSuite) TestSignWithMultiSignersAminoJSON() { val1Coin := sdk.NewCoin("test2token", math.NewInt(10)) _, _, addr1 := testdata.KeyTestPubAddr() + valStr, err := s.ac.BytesToString(val0) + s.Require().NoError(err) + val1Str, err := s.ac.BytesToString(val1) + s.Require().NoError(err) + + addrStr, err := s.ac.BytesToString(addr1) + s.Require().NoError(err) // Creating a tx with 2 msgs from 2 signers: val0 and val1. // The validators need to sign with SIGN_MODE_LEGACY_AMINO_JSON, // because DIRECT doesn't support multi signers via the CLI. // Since we use amino, we don't need to pre-populate signer_infos. txBuilder := s.clientCtx.TxConfig.NewTxBuilder() - err := txBuilder.SetMsgs( - banktypes.NewMsgSend(val0, addr1, sdk.NewCoins(val0Coin)), - banktypes.NewMsgSend(val1, addr1, sdk.NewCoins(val1Coin)), + err = txBuilder.SetMsgs( + banktypes.NewMsgSend(valStr, addrStr, sdk.NewCoins(val0Coin)), + banktypes.NewMsgSend(val1Str, addrStr, sdk.NewCoins(val1Coin)), ) s.Require().NoError(err) txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(10)))) diff --git a/tests/integration/bank/app_test.go b/tests/integration/bank/app_test.go index c9a41f526cf5..f1ef5ef9ed7e 100644 --- a/tests/integration/bank/app_test.go +++ b/tests/integration/bank/app_test.go @@ -64,7 +64,7 @@ var ( coins = sdk.Coins{sdk.NewInt64Coin("foocoin", 10)} halfCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 5)} - sendMsg1 = types.NewMsgSend(addr1, addr2, coins) + sendMsg1 = types.NewMsgSend(addr1.String(), addr2.String(), coins) multiSendMsg1 = &types.MsgMultiSend{ Inputs: []types.Input{types.NewInput(addr1, coins)}, @@ -167,7 +167,11 @@ func TestSendNotEnoughBalance(t *testing.T) { origAccNum := res1.GetAccountNumber() origSeq := res1.GetSequence() - sendMsg := types.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 100)}) + addr1Str, err := s.AccountKeeper.AddressCodec().BytesToString(addr1) + require.NoError(t, err) + addr2Str, err := s.AccountKeeper.AddressCodec().BytesToString(addr2) + require.NoError(t, err) + sendMsg := types.NewMsgSend(addr1Str, addr2Str, sdk.Coins{sdk.NewInt64Coin("foocoin", 100)}) header := header.Info{Height: baseApp.LastBlockHeight() + 1} txConfig := moduletestutil.MakeTestTxConfig() _, _, err = simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, []sdk.Msg{sendMsg}, "", []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1) diff --git a/tests/starship/tests/transfer_test.go b/tests/starship/tests/transfer_test.go index 6eb6ce567a94..6cfe38641762 100644 --- a/tests/starship/tests/transfer_test.go +++ b/tests/starship/tests/transfer_test.go @@ -44,7 +44,7 @@ func (s *TestSuite) TestChainTokenTransfer() { s.Require().NoError(err) // build tx into the txBuilder - msg := banktypes.NewMsgSend(addr1, addr2, sdk.NewCoins(sdk.NewCoin(denom, math.NewInt(1230000)))) + msg := banktypes.NewMsgSend(addr1.String(), addr2.String(), sdk.NewCoins(sdk.NewCoin(denom, math.NewInt(1230000)))) s.Require().NoError(err) err = txBuilder.SetMsgs(msg) s.Require().NoError(err) diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index 88f068d9ea0b..c73c562b5f37 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -285,7 +285,16 @@ func SimulateMsgExec( return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, nil } - msg := []sdk.Msg{banktype.NewMsgSend(granterAddr, granteeAddr, coins)} + graStr, err := ak.AddressCodec().BytesToString(granterAddr) + if err != nil { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err + } + greStr, err := ak.AddressCodec().BytesToString(granteeAddr) + if err != nil { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err + } + + msg := []sdk.Msg{banktype.NewMsgSend(graStr, greStr, coins)} _, err = sendAuth.Accept(ctx, msg[0]) if err != nil { diff --git a/x/bank/client/cli/tx_test.go b/x/bank/client/cli/tx_test.go index 9cd2813c2dd7..879b5e6a99a3 100644 --- a/x/bank/client/cli/tx_test.go +++ b/x/bank/client/cli/tx_test.go @@ -53,6 +53,12 @@ func (s *CLITestSuite) SetupSuite() { func (s *CLITestSuite) TestMultiSendTxCmd() { accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 3) + accountStr := make([]string, len(accounts)) + for i, acc := range accounts { + addrStr, err := s.baseCtx.AddressCodec.BytesToString(acc.Address) + s.Require().NoError(err) + accountStr[i] = addrStr + } cmd := cli.NewMultiSendTxCmd() cmd.SetOutput(io.Discard) @@ -79,10 +85,10 @@ func (s *CLITestSuite) TestMultiSendTxCmd() { func() client.Context { return s.baseCtx }, - accounts[0].Address.String(), + accountStr[0], []string{ - accounts[1].Address.String(), - accounts[2].Address.String(), + accountStr[1], + accountStr[2], }, sdk.NewCoins( sdk.NewCoin("stake", sdkmath.NewInt(10)), @@ -98,8 +104,8 @@ func (s *CLITestSuite) TestMultiSendTxCmd() { }, "foo", []string{ - accounts[1].Address.String(), - accounts[2].Address.String(), + accountStr[1], + accountStr[2], }, sdk.NewCoins( sdk.NewCoin("stake", sdkmath.NewInt(10)), @@ -113,9 +119,9 @@ func (s *CLITestSuite) TestMultiSendTxCmd() { func() client.Context { return s.baseCtx }, - accounts[0].Address.String(), + accountStr[0], []string{ - accounts[1].Address.String(), + accountStr[1], "bar", }, sdk.NewCoins( @@ -130,10 +136,10 @@ func (s *CLITestSuite) TestMultiSendTxCmd() { func() client.Context { return s.baseCtx }, - accounts[0].Address.String(), + accountStr[0], []string{ - accounts[1].Address.String(), - accounts[2].Address.String(), + accountStr[1], + accountStr[2], }, nil, extraArgs, diff --git a/x/bank/keeper/grpc_query_test.go b/x/bank/keeper/grpc_query_test.go index 556cecdbeed4..9c2de78fd997 100644 --- a/x/bank/keeper/grpc_query_test.go +++ b/x/bank/keeper/grpc_query_test.go @@ -234,7 +234,10 @@ func (suite *KeeperTestSuite) TestSpendableBalanceByDenom() { _, err := queryClient.SpendableBalanceByDenom(ctx, &types.QuerySpendableBalanceByDenomRequest{}) suite.Require().Error(err) - req := types.NewQuerySpendableBalanceByDenomRequest(addr, fooDenom) + addrStr, err := suite.authKeeper.AddressCodec().BytesToString(addr) + suite.Require().NoError(err) + + req := types.NewQuerySpendableBalanceByDenomRequest(addrStr, fooDenom) acc := authtypes.NewBaseAccountWithAddress(addr) suite.mockSpendableCoins(ctx, acc) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index e3bafc0a1ad1..5ce4c51fb0e4 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -152,12 +152,16 @@ func (k BaseKeeper) DelegateCoins(ctx context.Context, delegatorAddr, moduleAccA return errorsmod.Wrap(err, "failed to track delegation") } // emit coin spent event + delAddrStr, err := k.ak.AddressCodec().BytesToString(delegatorAddr) + if err != nil { + return err + } sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx.EventManager().EmitEvent( - types.NewCoinSpentEvent(delegatorAddr, amt), + types.NewCoinSpentEvent(delAddrStr, amt), ) - err := k.addCoins(ctx, moduleAccAddr, amt) + err = k.addCoins(ctx, moduleAccAddr, amt) if err != nil { return err } @@ -370,9 +374,13 @@ func (k BaseKeeper) MintCoins(ctx context.Context, moduleName string, amounts sd k.logger.Debug("minted coins from module account", "amount", amounts.String(), "from", moduleName) + addrStr, err := k.ak.AddressCodec().BytesToString(acc.GetAddress()) + if err != nil { + return err + } // emit mint event sdkCtx.EventManager().EmitEvent( - types.NewCoinMintEvent(acc.GetAddress(), amounts), + types.NewCoinMintEvent(addrStr, amounts), ) return nil @@ -405,10 +413,14 @@ func (k BaseKeeper) BurnCoins(ctx context.Context, address []byte, amounts sdk.C k.logger.Debug("burned tokens from account", "amount", amounts.String(), "from", address) + addrStr, err := k.ak.AddressCodec().BytesToString(acc.GetAddress()) + if err != nil { + return err + } // emit burn event sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx.EventManager().EmitEvent( - types.NewCoinBurnEvent(acc.GetAddress(), amounts), + types.NewCoinBurnEvent(addrStr, amounts), ) return nil diff --git a/x/bank/keeper/send.go b/x/bank/keeper/send.go index eddc4b99e511..22b03e225357 100644 --- a/x/bank/keeper/send.go +++ b/x/bank/keeper/send.go @@ -178,7 +178,7 @@ func (k BaseSendKeeper) InputOutputCoins(ctx context.Context, input types.Input, sdkCtx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeTransfer, - sdk.NewAttribute(types.AttributeKeyRecipient, outAddress.String()), + sdk.NewAttribute(types.AttributeKeyRecipient, out.Address), sdk.NewAttribute(sdk.AttributeKeyAmount, out.Coins.String()), ), ) @@ -226,13 +226,20 @@ func (k BaseSendKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccA k.ak.SetAccount(ctx, k.ak.NewAccountWithAddress(ctx, toAddr)) } - // bech32 encoding is expensive! Only do it once for fromAddr - fromAddrString := fromAddr.String() + fromAddrString, err := k.ak.AddressCodec().BytesToString(fromAddr) + if err != nil { + return err + } + toAddrString, err := k.ak.AddressCodec().BytesToString(toAddr) + if err != nil { + return err + } + sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeTransfer, - sdk.NewAttribute(types.AttributeKeyRecipient, toAddr.String()), + sdk.NewAttribute(types.AttributeKeyRecipient, toAddrString), sdk.NewAttribute(types.AttributeKeySender, fromAddrString), sdk.NewAttribute(sdk.AttributeKeyAmount, amt.String()), ), @@ -279,9 +286,13 @@ func (k BaseSendKeeper) subUnlockedCoins(ctx context.Context, addr sdk.AccAddres } } + addrStr, err := k.ak.AddressCodec().BytesToString(addr) + if err != nil { + return err + } sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx.EventManager().EmitEvent( - types.NewCoinSpentEvent(addr, amt), + types.NewCoinSpentEvent(addrStr, amt), ) return nil @@ -304,10 +315,15 @@ func (k BaseSendKeeper) addCoins(ctx context.Context, addr sdk.AccAddress, amt s } } + addrStr, err := k.ak.AddressCodec().BytesToString(addr) + if err != nil { + return err + } + // emit coin received event sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx.EventManager().EmitEvent( - types.NewCoinReceivedEvent(addr, amt), + types.NewCoinReceivedEvent(addrStr, amt), ) return nil @@ -357,7 +373,11 @@ func (k BaseSendKeeper) IsSendEnabledCoin(ctx context.Context, coin sdk.Coin) bo // BlockedAddr checks if a given address is restricted from // receiving funds. func (k BaseSendKeeper) BlockedAddr(addr sdk.AccAddress) bool { - return k.blockedAddrs[addr.String()] + addrStr, err := k.ak.AddressCodec().BytesToString(addr) + if err != nil { + panic(err) + } + return k.blockedAddrs[addrStr] } // GetBlockedAddresses returns the full list of addresses restricted from receiving funds. diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index 32adceb96e28..89670938002d 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -119,7 +119,11 @@ func (k BaseViewKeeper) GetAccountsBalances(ctx context.Context) []types.Balance mapAddressToBalancesIdx := make(map[string]int) k.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool { - idx, ok := mapAddressToBalancesIdx[addr.String()] + addrStr, err := k.ak.AddressCodec().BytesToString(addr) + if err != nil { + panic(err) + } + idx, ok := mapAddressToBalancesIdx[addrStr] if ok { // address is already on the set of accounts balances balances[idx].Coins = balances[idx].Coins.Add(balance) @@ -128,11 +132,11 @@ func (k BaseViewKeeper) GetAccountsBalances(ctx context.Context) []types.Balance } accountBalance := types.Balance{ - Address: addr.String(), + Address: addrStr, Coins: sdk.NewCoins(balance), } balances = append(balances, accountBalance) - mapAddressToBalancesIdx[addr.String()] = len(balances) - 1 + mapAddressToBalancesIdx[addrStr] = len(balances) - 1 return false }) diff --git a/x/bank/module.go b/x/bank/module.go index 77397d8acd1b..67889d2a5dfd 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -218,11 +218,19 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { blockedAddresses := make(map[string]bool) if len(in.Config.BlockedModuleAccountsOverride) > 0 { for _, moduleName := range in.Config.BlockedModuleAccountsOverride { - blockedAddresses[authtypes.NewModuleAddress(moduleName).String()] = true + addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(moduleName)) + if err != nil { + panic(err) + } + blockedAddresses[addrStr] = true } } else { for _, permission := range in.AccountKeeper.GetModulePermissions() { - blockedAddresses[permission.GetAddress().String()] = true + addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(permission.GetAddress()) + if err != nil { + panic(err) + } + blockedAddresses[addrStr] = true } } @@ -232,12 +240,17 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } + authStr, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(err) + } + bankKeeper := keeper.NewBaseKeeper( in.Cdc, in.StoreService, in.AccountKeeper, blockedAddresses, - authority.String(), + authStr, in.Logger, ) m := NewAppModule(in.Cdc, bankKeeper, in.AccountKeeper) diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index ec6bb4535e59..cda4ca70a7ac 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -78,15 +78,22 @@ func SimulateMsgSend( if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil { return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil } - if skip { return simtypes.NoOpMsg(types.ModuleName, msgType, "skip all transfers"), nil, nil } - msg := types.NewMsgSend(from.Address, to.Address, coins) - - err := sendMsgSend(r, app, txGen, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey}) + fromstr, err := ak.AddressCodec().BytesToString(from.Address) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil + } + tostr, err := ak.AddressCodec().BytesToString(to.Address) if err != nil { + return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil + } + + msg := types.NewMsgSend(fromstr, tostr, coins) + + if err := sendMsgSend(r, app, txGen, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey}); err != nil { return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "invalid transfers"), nil, err } @@ -116,19 +123,23 @@ func SimulateMsgSendToModuleAccount( if len(coins) == 0 { return simtypes.NoOpMsg(types.ModuleName, msgType, "empty coins slice"), nil, nil } - // Check send_enabled status of each coin denom if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil { return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil } - - msg := types.NewMsgSend(from.Address, to.Address, coins) - - err := sendMsgSend(r, app, txGen, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey}) + fromstr, err := ak.AddressCodec().BytesToString(from.Address) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "invalid transfers"), nil, err + return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil } + tostr, err := ak.AddressCodec().BytesToString(to.Address) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil + } + msg := types.NewMsgSend(fromstr, tostr, coins) + if err := sendMsgSend(r, app, txGen, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey}); err != nil { + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "invalid transfers"), nil, err + } return simtypes.NewOperationMsg(msg, true, ""), nil, nil } } @@ -145,7 +156,7 @@ func sendMsgSend( err error ) - from, err := sdk.AccAddressFromBech32(msg.FromAddress) + from, err := ak.AddressCodec().StringToBytes(msg.FromAddress) if err != nil { return err } diff --git a/x/bank/types/events.go b/x/bank/types/events.go index 098eebc37251..d26c4d8f342c 100644 --- a/x/bank/types/events.go +++ b/x/bank/types/events.go @@ -24,37 +24,37 @@ const ( ) // NewCoinSpentEvent constructs a new coin spent sdk.Event -func NewCoinSpentEvent(spender sdk.AccAddress, amount sdk.Coins) sdk.Event { +func NewCoinSpentEvent(spender string, amount sdk.Coins) sdk.Event { return sdk.NewEvent( EventTypeCoinSpent, - sdk.NewAttribute(AttributeKeySpender, spender.String()), + sdk.NewAttribute(AttributeKeySpender, spender), sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), ) } // NewCoinReceivedEvent constructs a new coin received sdk.Event -func NewCoinReceivedEvent(receiver sdk.AccAddress, amount sdk.Coins) sdk.Event { +func NewCoinReceivedEvent(receiver string, amount sdk.Coins) sdk.Event { return sdk.NewEvent( EventTypeCoinReceived, - sdk.NewAttribute(AttributeKeyReceiver, receiver.String()), + sdk.NewAttribute(AttributeKeyReceiver, receiver), sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), ) } // NewCoinMintEvent construct a new coin minted sdk.Event -func NewCoinMintEvent(minter sdk.AccAddress, amount sdk.Coins) sdk.Event { +func NewCoinMintEvent(minter string, amount sdk.Coins) sdk.Event { return sdk.NewEvent( EventTypeCoinMint, - sdk.NewAttribute(AttributeKeyMinter, minter.String()), + sdk.NewAttribute(AttributeKeyMinter, minter), sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), ) } // NewCoinBurnEvent constructs a new coin burned sdk.Event -func NewCoinBurnEvent(burner sdk.AccAddress, amount sdk.Coins) sdk.Event { +func NewCoinBurnEvent(burner string, amount sdk.Coins) sdk.Event { return sdk.NewEvent( EventTypeCoinBurn, - sdk.NewAttribute(AttributeKeyBurner, burner.String()), + sdk.NewAttribute(AttributeKeyBurner, burner), sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), ) } diff --git a/x/bank/types/msgs.go b/x/bank/types/msgs.go index 05a23c280e2b..9cda2bd45fb3 100644 --- a/x/bank/types/msgs.go +++ b/x/bank/types/msgs.go @@ -11,8 +11,8 @@ var ( ) // NewMsgSend - construct a msg to send coins from one account to another. -func NewMsgSend(fromAddr, toAddr sdk.AccAddress, amount sdk.Coins) *MsgSend { - return &MsgSend{FromAddress: fromAddr.String(), ToAddress: toAddr.String(), Amount: amount} +func NewMsgSend(fromAddr, toAddr string, amount sdk.Coins) *MsgSend { + return &MsgSend{FromAddress: fromAddr, ToAddress: toAddr, Amount: amount} } // NewMsgMultiSend - construct arbitrary multi-in, multi-out send msg. diff --git a/x/bank/types/msgs_test.go b/x/bank/types/msgs_test.go index 98fc399b67c6..a6c59826bebb 100644 --- a/x/bank/types/msgs_test.go +++ b/x/bank/types/msgs_test.go @@ -12,10 +12,8 @@ import ( ) func TestMsgSendGetSignBytes(t *testing.T) { - addr1 := sdk.AccAddress([]byte("input")) - addr2 := sdk.AccAddress([]byte("output")) coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10)) - msg := NewMsgSend(addr1, addr2, coins) + msg := NewMsgSend("cosmos1d9h8qat57ljhcm", "cosmos1da6hgur4wsmpnjyg", coins) res, err := codec.NewProtoCodec(types.NewInterfaceRegistry()).MarshalAminoJSON(msg) require.NoError(t, err) diff --git a/x/bank/types/querier.go b/x/bank/types/querier.go index 5303035c2b44..6a2dcb097432 100644 --- a/x/bank/types/querier.go +++ b/x/bank/types/querier.go @@ -31,8 +31,8 @@ func NewQuerySpendableBalancesRequest(addr sdk.AccAddress, req *query.PageReques // NewQuerySpendableBalanceByDenomRequest creates a new instance of a // QuerySpendableBalanceByDenomRequest. -func NewQuerySpendableBalanceByDenomRequest(addr sdk.AccAddress, denom string) *QuerySpendableBalanceByDenomRequest { - return &QuerySpendableBalanceByDenomRequest{Address: addr.String(), Denom: denom} +func NewQuerySpendableBalanceByDenomRequest(addr, denom string) *QuerySpendableBalanceByDenomRequest { + return &QuerySpendableBalanceByDenomRequest{Address: addr, Denom: denom} } // QueryTotalSupplyParams defines the params for the following queries: diff --git a/x/bank/types/send_authorization_test.go b/x/bank/types/send_authorization_test.go index 78e9f29f7e38..fbecc2300669 100644 --- a/x/bank/types/send_authorization_test.go +++ b/x/bank/types/send_authorization_test.go @@ -16,11 +16,12 @@ import ( ) var ( - coins1000 = sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(1000))) - coins500 = sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(500))) - fromAddr = sdk.AccAddress("_____from _____") - toAddr = sdk.AccAddress("_______to________") - unknownAddr = sdk.AccAddress("_____unknown_____") + coins1000 = sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(1000))) + coins500 = sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(500))) + toAddr = sdk.AccAddress("_______to________") + fromAddrStr = "cosmos1ta047h6lveex7mfqta047h6ln9jal0" + toAddrStr = "cosmos1ta047h6lta0hgm6lta047h6lta0stgm2m3" + unknownAddrStr = "cosmos1ta047h6lw4hxkmn0wah97h6lta0sml880l" ) func TestSendAuthorization(t *testing.T) { @@ -32,9 +33,7 @@ func TestSendAuthorization(t *testing.T) { t.Log("verify authorization returns valid method name") require.Equal(t, authorization.MsgTypeURL(), "/cosmos.bank.v1beta1.MsgSend") require.NoError(t, authorization.ValidateBasic()) - send := types.NewMsgSend(fromAddr, toAddr, coins1000) - - require.NoError(t, authorization.ValidateBasic()) + send := types.NewMsgSend(fromAddrStr, toAddrStr, coins1000) t.Log("verify updated authorization returns nil") resp, err := authorization.Accept(ctx, send) @@ -45,7 +44,7 @@ func TestSendAuthorization(t *testing.T) { authorization = types.NewSendAuthorization(coins1000, nil) require.Equal(t, authorization.MsgTypeURL(), "/cosmos.bank.v1beta1.MsgSend") require.NoError(t, authorization.ValidateBasic()) - send = types.NewMsgSend(fromAddr, toAddr, coins500) + send = types.NewMsgSend(fromAddrStr, toAddrStr, coins500) require.NoError(t, authorization.ValidateBasic()) resp, err = authorization.Accept(ctx, send) @@ -66,20 +65,20 @@ func TestSendAuthorization(t *testing.T) { authzWithAllowList := types.NewSendAuthorization(coins1000, allowList) require.Equal(t, authzWithAllowList.MsgTypeURL(), "/cosmos.bank.v1beta1.MsgSend") require.NoError(t, authorization.ValidateBasic()) - send = types.NewMsgSend(fromAddr, unknownAddr, coins500) + send = types.NewMsgSend(fromAddrStr, unknownAddrStr, coins500) require.NoError(t, authzWithAllowList.ValidateBasic()) resp, err = authzWithAllowList.Accept(ctx, send) require.False(t, resp.Accept) require.False(t, resp.Delete) require.Nil(t, resp.Updated) require.Error(t, err) - require.Contains(t, err.Error(), fmt.Sprintf("cannot send to %s address", unknownAddr)) + require.Contains(t, err.Error(), fmt.Sprintf("cannot send to %s address", unknownAddrStr)) t.Log("send to address in allow list") authzWithAllowList = types.NewSendAuthorization(coins1000, allowList) require.Equal(t, authzWithAllowList.MsgTypeURL(), "/cosmos.bank.v1beta1.MsgSend") require.NoError(t, authorization.ValidateBasic()) - send = types.NewMsgSend(fromAddr, allowList[0], coins500) + send = types.NewMsgSend(fromAddrStr, toAddrStr, coins500) require.NoError(t, authzWithAllowList.ValidateBasic()) resp, err = authzWithAllowList.Accept(ctx, send) require.NoError(t, err) @@ -92,17 +91,17 @@ func TestSendAuthorization(t *testing.T) { authzWithAllowList = types.NewSendAuthorization(coins1000, allowList) require.Equal(t, authzWithAllowList.MsgTypeURL(), "/cosmos.bank.v1beta1.MsgSend") require.NoError(t, authorization.ValidateBasic()) - send = types.NewMsgSend(fromAddr, unknownAddr, coins1000) + send = types.NewMsgSend(fromAddrStr, unknownAddrStr, coins1000) require.NoError(t, authzWithAllowList.ValidateBasic()) resp, err = authzWithAllowList.Accept(ctx, send) require.Error(t, err) - require.Contains(t, err.Error(), fmt.Sprintf("cannot send to %s address", unknownAddr)) + require.Contains(t, err.Error(), fmt.Sprintf("cannot send to %s address", unknownAddrStr)) t.Log("send everything to address in allow list") authzWithAllowList = types.NewSendAuthorization(coins1000, allowList) require.Equal(t, authzWithAllowList.MsgTypeURL(), "/cosmos.bank.v1beta1.MsgSend") require.NoError(t, authorization.ValidateBasic()) - send = types.NewMsgSend(fromAddr, allowList[0], coins1000) + send = types.NewMsgSend(fromAddrStr, toAddrStr, coins1000) require.NoError(t, authzWithAllowList.ValidateBasic()) resp, err = authzWithAllowList.Accept(ctx, send) require.NoError(t, err) diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index bf018712c0e2..bf4e8eebd173 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -17,6 +17,7 @@ import ( banktypes "cosmossdk.io/x/bank/types" stakingtypes "cosmossdk.io/x/staking/types" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/testutil" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" @@ -239,6 +240,13 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { txBuilder = suite.encodingConfig.TxConfig.NewTxBuilder() ) + ac := addresscodec.NewBech32Codec("cosmos") + + addr1Str, err := ac.BytesToString(addr1) + suite.Require().NoError(err) + addr2Str, err := ac.BytesToString(addr2) + suite.Require().NoError(err) + testCases := []struct { msg string malleate func() @@ -263,7 +271,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { "success", func() { r := rand.New(rand.NewSource(time.Now().UnixNano())) - msg := banktypes.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}) + msg := banktypes.NewMsgSend(addr1Str, addr2Str, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}) tx, err := simtestutil.GenSignedMockTx( r, suite.encodingConfig.TxConfig, diff --git a/x/gov/abci_test.go b/x/gov/abci_test.go index 7dc07a51c4fd..4133da6a0eb6 100644 --- a/x/gov/abci_test.go +++ b/x/gov/abci_test.go @@ -17,6 +17,7 @@ import ( stakingkeeper "cosmossdk.io/x/staking/keeper" stakingtypes "cosmossdk.io/x/staking/types" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -400,10 +401,16 @@ func TestEndBlockerProposalHandlerFailed(t *testing.T) { valAddr := sdk.ValAddress(addrs[0]) proposer := addrs[0] + ac := addresscodec.NewBech32Codec("cosmos") + addrStr, err := ac.BytesToString(authtypes.NewModuleAddress(types.ModuleName)) + require.NoError(t, err) + addrStr1, err := ac.BytesToString(addrs[0]) + require.NoError(t, err) + createValidators(t, stakingMsgSvr, ctx, []sdk.ValAddress{valAddr}, []int64{10}) - _, err := suite.StakingKeeper.EndBlocker(ctx) + _, err = suite.StakingKeeper.EndBlocker(ctx) require.NoError(t, err) - msg := banktypes.NewMsgSend(authtypes.NewModuleAddress(types.ModuleName), addrs[0], sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100000)))) + msg := banktypes.NewMsgSend(addrStr, addrStr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100000)))) proposal, err := suite.GovKeeper.SubmitProposal(ctx, []sdk.Msg{msg}, "", "title", "summary", proposer, false) require.NoError(t, err) diff --git a/x/gov/keeper/common_test.go b/x/gov/keeper/common_test.go index 1e3a55ffda5b..e65b4d39087d 100644 --- a/x/gov/keeper/common_test.go +++ b/x/gov/keeper/common_test.go @@ -34,6 +34,8 @@ var ( _, _, addr = testdata.KeyTestPubAddr() govAcct = authtypes.NewModuleAddress(types.ModuleName) poolAcct = authtypes.NewModuleAddress(pooltypes.ModuleName) + govAcctStr = "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn" + addrStr = addr.String() TestProposal = getTestProposal() ) @@ -50,7 +52,7 @@ func getTestProposal() []sdk.Msg { } return []sdk.Msg{ - banktypes.NewMsgSend(govAcct, addr, sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000)))), + banktypes.NewMsgSend(govAcctStr, addrStr, sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000)))), legacyProposalMsg, } } diff --git a/x/gov/types/v1/msgs_test.go b/x/gov/types/v1/msgs_test.go index 1078bf524641..80e9c638abad 100644 --- a/x/gov/types/v1/msgs_test.go +++ b/x/gov/types/v1/msgs_test.go @@ -21,6 +21,10 @@ var ( sdk.AccAddress("test1"), sdk.AccAddress("test2"), } + addrStrs = []string{ + "cosmos1w3jhxap3gempvr", + "cosmos1w3jhxapjx2whzu", + } ) func init() { @@ -58,17 +62,17 @@ func TestMsgSubmitProposal_GetSignBytes(t *testing.T) { }, { "MsgSend", - []sdk.Msg{banktypes.NewMsgSend(addrs[0], addrs[0], sdk.NewCoins())}, + []sdk.Msg{banktypes.NewMsgSend(addrStrs[0], addrStrs[0], sdk.NewCoins())}, "bank/MsgSend", "Proposal for a bank msg send", false, - fmt.Sprintf(`{"type":"cosmos-sdk/v1/MsgSubmitProposal","value":{"initial_deposit":[],"messages":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[],"from_address":"%s","to_address":"%s"}}],"summary":"Proposal for a bank msg send","title":"bank/MsgSend"}}`, addrs[0], addrs[0]), + fmt.Sprintf(`{"type":"cosmos-sdk/v1/MsgSubmitProposal","value":{"initial_deposit":[],"messages":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[],"from_address":"%s","to_address":"%s"}}],"summary":"Proposal for a bank msg send","title":"bank/MsgSend"}}`, addrStrs[0], addrStrs[0]), }, } for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - msg, err := v1.NewMsgSubmitProposal(tc.proposal, sdk.NewCoins(), sdk.AccAddress{}.String(), "", tc.title, tc.summary, tc.expedited) + msg, err := v1.NewMsgSubmitProposal(tc.proposal, sdk.NewCoins(), "", "", tc.title, tc.summary, tc.expedited) require.NoError(t, err) bz, err := pc.MarshalAminoJSON(msg) require.NoError(t, err)