Skip to content

Commit

Permalink
refactor(x/bank)!: remove Address.String() (cosmos#19954)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <[email protected]>
  • Loading branch information
JulianToledano and tac0turtle authored Apr 5, 2024
1 parent d881554 commit c753458
Show file tree
Hide file tree
Showing 37 changed files with 499 additions and 251 deletions.
5 changes: 4 additions & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ func initGenFiles(
var bankGenState banktypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState)

bankGenState.Balances = banktypes.SanitizeGenesisBalances(genBalances)
bankGenState.Balances, err = banktypes.SanitizeGenesisBalances(genBalances, clientCtx.AddressCodec)
if err != nil {
return err
}
for _, bal := range bankGenState.Balances {
bankGenState.Supply = bankGenState.Supply.Add(bal.Coins...)
}
Expand Down
111 changes: 66 additions & 45 deletions tests/integration/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,6 @@ var (
halfCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 5)}

sendMsg1 = types.NewMsgSend(addr1.String(), addr2.String(), coins)

multiSendMsg1 = &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1, coins)},
Outputs: []types.Output{types.NewOutput(addr2, coins)},
}
multiSendMsg2 = &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1, coins)},
Outputs: []types.Output{
types.NewOutput(addr2, halfCoins),
types.NewOutput(addr3, halfCoins),
},
}
multiSendMsg3 = &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr2, coins)},
Outputs: []types.Output{
types.NewOutput(addr1, coins),
},
}
multiSendMsg4 = &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1, coins)},
Outputs: []types.Output{
types.NewOutput(moduleAccAddr, coins),
},
}
invalidMultiSendMsg = &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1, coins), types.NewInput(addr2, coins)},
Outputs: []types.Output{},
}
)

type suite struct {
Expand Down Expand Up @@ -191,17 +163,25 @@ func TestSendNotEnoughBalance(t *testing.T) {
}

func TestMsgMultiSendWithAccounts(t *testing.T) {
addr1Str, err := cdctestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr1)
require.NoError(t, err)
acc := &authtypes.BaseAccount{
Address: addr1.String(),
Address: addr1Str,
}

addr2Str, err := cdctestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr2)
require.NoError(t, err)

moduleStrAddr, err := cdctestutil.CodecOptions{}.GetAddressCodec().BytesToString(moduleAccAddr)
require.NoError(t, err)

genAccs := []authtypes.GenesisAccount{acc}
s := createTestSuite(t, genAccs)
baseApp := s.App.BaseApp
ctx := baseApp.NewContext(false)

require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))))
_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
_, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
require.NoError(t, err)
_, err = baseApp.Commit()
require.NoError(t, err)
Expand All @@ -212,8 +192,11 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {

testCases := []appTestCase{
{
desc: "make a valid tx",
msgs: []sdk.Msg{multiSendMsg1},
desc: "make a valid tx",
msgs: []sdk.Msg{&types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1Str, coins)},
Outputs: []types.Output{types.NewOutput(addr2Str, coins)},
}},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true,
Expand All @@ -225,26 +208,37 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {
},
},
{
desc: "wrong accNum should pass Simulate, but not Deliver",
msgs: []sdk.Msg{multiSendMsg1},
desc: "wrong accNum should pass Simulate, but not Deliver",
msgs: []sdk.Msg{&types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1Str, coins)},
Outputs: []types.Output{types.NewOutput(addr2Str, coins)},
}},
accNums: []uint64{1}, // wrong account number
accSeqs: []uint64{1},
expSimPass: true, // doesn't check signature
expPass: false,
privKeys: []cryptotypes.PrivKey{priv1},
},
{
desc: "wrong accSeq should not pass Simulate",
msgs: []sdk.Msg{multiSendMsg4},
desc: "wrong accSeq should not pass Simulate",
msgs: []sdk.Msg{&types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1Str, coins)},
Outputs: []types.Output{
types.NewOutput(moduleStrAddr, coins),
},
}},
accNums: []uint64{0},
accSeqs: []uint64{0}, // wrong account sequence
expSimPass: false,
expPass: false,
privKeys: []cryptotypes.PrivKey{priv1},
},
{
desc: "multiple inputs not allowed",
msgs: []sdk.Msg{invalidMultiSendMsg},
desc: "multiple inputs not allowed",
msgs: []sdk.Msg{&types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1Str, coins), types.NewInput(addr2Str, coins)},
Outputs: []types.Output{},
}},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: false,
Expand Down Expand Up @@ -272,12 +266,19 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {
}

func TestMsgMultiSendMultipleOut(t *testing.T) {
ac := cdctestutil.CodecOptions{}.GetAddressCodec()
addr1Str, err := ac.BytesToString(addr1)
require.NoError(t, err)
acc1 := &authtypes.BaseAccount{
Address: addr1.String(),
Address: addr1Str,
}
addr2Str, err := ac.BytesToString(addr2)
require.NoError(t, err)
acc2 := &authtypes.BaseAccount{
Address: addr2.String(),
Address: addr2Str,
}
addr3Str, err := ac.BytesToString(addr3)
require.NoError(t, err)

genAccs := []authtypes.GenesisAccount{acc1, acc2}
s := createTestSuite(t, genAccs)
Expand All @@ -286,14 +287,20 @@ func TestMsgMultiSendMultipleOut(t *testing.T) {

require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
_, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1})
require.NoError(t, err)
_, err = baseApp.Commit()
require.NoError(t, err)

testCases := []appTestCase{
{
msgs: []sdk.Msg{multiSendMsg2},
msgs: []sdk.Msg{&types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1Str, coins)},
Outputs: []types.Output{
types.NewOutput(addr2Str, halfCoins),
types.NewOutput(addr3Str, halfCoins),
},
}},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true,
Expand All @@ -320,9 +327,15 @@ func TestMsgMultiSendMultipleOut(t *testing.T) {
}

func TestMsgMultiSendDependent(t *testing.T) {
ac := cdctestutil.CodecOptions{}.GetAddressCodec()
addr1Str, err := ac.BytesToString(addr1)
require.NoError(t, err)
addr2Str, err := ac.BytesToString(addr2)
require.NoError(t, err)

acc1 := authtypes.NewBaseAccountWithAddress(addr1)
acc2 := authtypes.NewBaseAccountWithAddress(addr2)
err := acc2.SetAccountNumber(1)
err = acc2.SetAccountNumber(1)
require.NoError(t, err)

genAccs := []authtypes.GenesisAccount{acc1, acc2}
Expand All @@ -338,7 +351,10 @@ func TestMsgMultiSendDependent(t *testing.T) {

testCases := []appTestCase{
{
msgs: []sdk.Msg{multiSendMsg1},
msgs: []sdk.Msg{&types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1Str, coins)},
Outputs: []types.Output{types.NewOutput(addr2Str, coins)},
}},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true,
Expand All @@ -350,7 +366,12 @@ func TestMsgMultiSendDependent(t *testing.T) {
},
},
{
msgs: []sdk.Msg{multiSendMsg3},
msgs: []sdk.Msg{&types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr2Str, coins)},
Outputs: []types.Output{
types.NewOutput(addr1Str, coins),
},
}},
accNums: []uint64{1},
accSeqs: []uint64{0},
expSimPass: true,
Expand Down
15 changes: 13 additions & 2 deletions tests/integration/bank/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
authtypes "cosmossdk.io/x/auth/types"
_ "cosmossdk.io/x/bank"
"cosmossdk.io/x/bank/testutil"
"cosmossdk.io/x/bank/types"
stakingtypes "cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -123,17 +124,22 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
// b.Skip("Skipping benchmark with buggy code reported at https://github.com/cosmos/cosmos-sdk/issues/10023")
b.ReportAllocs()

addr1Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr1)
require.NoError(b, err)
acc := authtypes.BaseAccount{
Address: addr1.String(),
}

addr2Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr2)
require.NoError(b, err)

// construct genesis state
genAccs := []authtypes.GenesisAccount{&acc}
s := createTestSuite(&testing.T{}, genAccs)
baseApp := s.App.BaseApp
ctx := baseApp.NewContext(false)

_, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
_, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
require.NoError(b, err)

require.NoError(b, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))
Expand All @@ -144,8 +150,13 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
txGen := moduletestutil.MakeTestTxConfig(codectestutil.CodecOptions{})
txEncoder := txGen.TxEncoder()

multiSendMsg := &types.MsgMultiSend{
Inputs: []types.Input{types.NewInput(addr1Str, coins)},
Outputs: []types.Output{types.NewOutput(addr2Str, coins)},
}

// pre-compute all txs
txs, err := genSequenceOfTxs(txGen, []sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
txs, err := genSequenceOfTxs(txGen, []sdk.Msg{multiSendMsg}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
require.NoError(b, err)
b.ResetTimer()

Expand Down
32 changes: 25 additions & 7 deletions tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,25 +150,35 @@ func TestGRPCQueryBalance(t *testing.T) {
coin := getCoin(rt)
fundAccount(f, addr, coin)

req := banktypes.NewQueryBalanceRequest(addr, coin.GetDenom())
addrStr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr)
assert.NilError(t, err)

req := banktypes.NewQueryBalanceRequest(addrStr, coin.GetDenom())

testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.Balance, 0, true)
})

addr1Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr1)
assert.NilError(t, err)

fundAccount(f, addr1, coin1)
req := banktypes.NewQueryBalanceRequest(addr1, coin1.GetDenom())
req := banktypes.NewQueryBalanceRequest(addr1Str, coin1.GetDenom())
testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.Balance, 1087, false)
}

func TestGRPCQueryAllBalances(t *testing.T) {
t.Parallel()
f := initDeterministicFixture(t)
addressCodec := codectestutil.CodecOptions{}.GetAddressCodec()

rapid.Check(t, func(rt *rapid.T) {
addr := testdata.AddressGenerator(rt).Draw(rt, "address")
numCoins := rapid.IntRange(1, 10).Draw(rt, "num-count")
coins := make(sdk.Coins, 0, numCoins)

addrStr, err := addressCodec.BytesToString(addr)
assert.NilError(t, err)

for i := 0; i < numCoins; i++ {
coin := getCoin(rt)

Expand All @@ -178,7 +188,7 @@ func TestGRPCQueryAllBalances(t *testing.T) {

fundAccount(f, addr, coins...)

req := banktypes.NewQueryAllBalancesRequest(addr, testdata.PaginationGenerator(rt, uint64(numCoins)).Draw(rt, "pagination"), false)
req := banktypes.NewQueryAllBalancesRequest(addrStr, testdata.PaginationGenerator(rt, uint64(numCoins)).Draw(rt, "pagination"), false)
testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.AllBalances, 0, true)
})

Expand All @@ -188,7 +198,10 @@ func TestGRPCQueryAllBalances(t *testing.T) {
)

fundAccount(f, addr1, coins...)
req := banktypes.NewQueryAllBalancesRequest(addr1, nil, false)
addr1Str, err := addressCodec.BytesToString(addr1)
assert.NilError(t, err)

req := banktypes.NewQueryAllBalancesRequest(addr1Str, nil, false)

testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.AllBalances, 357, false)
}
Expand All @@ -199,6 +212,8 @@ func TestGRPCQuerySpendableBalances(t *testing.T) {

rapid.Check(t, func(rt *rapid.T) {
addr := testdata.AddressGenerator(rt).Draw(rt, "address")
addrStr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr)
assert.NilError(t, err)

// Denoms must be unique, otherwise sdk.NewCoins will panic.
denoms := rapid.SliceOfNDistinct(rapid.StringMatching(denomRegex), 1, 10, rapid.ID[string]).Draw(rt, "denoms")
Expand All @@ -213,10 +228,10 @@ func TestGRPCQuerySpendableBalances(t *testing.T) {
coins = sdk.NewCoins(append(coins, coin)...)
}

err := banktestutil.FundAccount(f.ctx, f.bankKeeper, addr, coins)
err = banktestutil.FundAccount(f.ctx, f.bankKeeper, addr, coins)
assert.NilError(t, err)

req := banktypes.NewQuerySpendableBalancesRequest(addr, testdata.PaginationGenerator(rt, uint64(len(denoms))).Draw(rt, "pagination"))
req := banktypes.NewQuerySpendableBalancesRequest(addrStr, testdata.PaginationGenerator(rt, uint64(len(denoms))).Draw(rt, "pagination"))
testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.SpendableBalances, 0, true)
})

Expand All @@ -228,7 +243,10 @@ func TestGRPCQuerySpendableBalances(t *testing.T) {
err := banktestutil.FundAccount(f.ctx, f.bankKeeper, addr1, coins)
assert.NilError(t, err)

req := banktypes.NewQuerySpendableBalancesRequest(addr1, nil)
addr1Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr1)
assert.NilError(t, err)

req := banktypes.NewQuerySpendableBalancesRequest(addr1Str, nil)
testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.SpendableBalances, 1777, false)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/sims/authz/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (suite *SimTestSuite) TestSimulateRevoke() {

granter := accounts[0]
grantee := accounts[1]
a := banktypes.NewSendAuthorization(initCoins, nil)
a := banktypes.NewSendAuthorization(initCoins, nil, suite.accountKeeper.AddressCodec())
expire := time.Now().Add(30 * time.Hour)

err := suite.authzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire)
Expand Down Expand Up @@ -200,7 +200,7 @@ func (suite *SimTestSuite) TestSimulateExec() {

granter := accounts[0]
grantee := accounts[1]
a := banktypes.NewSendAuthorization(initCoins, nil)
a := banktypes.NewSendAuthorization(initCoins, nil, suite.accountKeeper.AddressCodec())
expire := suite.ctx.HeaderInfo().Time.Add(1 * time.Hour)

err := suite.authzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire)
Expand Down
Loading

0 comments on commit c753458

Please sign in to comment.