diff --git a/x/auth/keeper/grpc_query.go b/x/auth/keeper/grpc_query.go index a18de05d0..30187644d 100644 --- a/x/auth/keeper/grpc_query.go +++ b/x/auth/keeper/grpc_query.go @@ -3,6 +3,7 @@ package keeper import ( "context" "errors" + "sort" "strings" "github.com/cosmos/cosmos-sdk/store/prefix" @@ -94,9 +95,16 @@ func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModule ctx := sdk.UnwrapSDKContext(c) + // For deterministic output, sort the permAddrs by module name. + sortedPermAddrs := make([]string, 0, len(ak.permAddrs)) + for moduleName := range ak.permAddrs { + sortedPermAddrs = append(sortedPermAddrs, moduleName) + } + sort.Strings(sortedPermAddrs) + modAccounts := make([]*codectypes.Any, 0, len(ak.permAddrs)) - for moduleName := range ak.permAddrs { + for _, moduleName := range sortedPermAddrs { account := ak.GetModuleAccount(ctx, moduleName) if account == nil { return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName) diff --git a/x/auth/keeper/grpc_query_test.go b/x/auth/keeper/grpc_query_test.go index 9052873d9..ba0f4b273 100644 --- a/x/auth/keeper/grpc_query_test.go +++ b/x/auth/keeper/grpc_query_test.go @@ -1,9 +1,10 @@ package keeper_test import ( - "fmt" - "context" "bytes" + "context" + "fmt" + "sort" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" @@ -11,6 +12,7 @@ import ( ) const addrStr = "cosmos13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv" + var addrBytes = []byte{0x8e, 0x22, 0xda, 0xb8, 0xa, 0x5a, 0x94, 0xdf, 0xbd, 0xb0, 0x58, 0xfa, 0x93, 0xcb, 0x11, 0x49, 0x5e, 0xac, 0xc5, 0x30} func (suite *KeeperTestSuite) TestGRPCQueryAccounts() { @@ -268,6 +270,17 @@ func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() { if tc.expPass { suite.Require().NoError(err) suite.Require().NotNil(res) + // Make sure output is sorted alphabetically. + var moduleNames []string + for _, any := range res.Accounts { + var account types.AccountI + err := suite.app.InterfaceRegistry().UnpackAny(any, &account) + suite.Require().NoError(err) + moduleAccount, ok := account.(types.ModuleAccountI) + suite.Require().True(ok) + moduleNames = append(moduleNames, moduleAccount.GetName()) + } + suite.Require().True(sort.StringsAreSorted(moduleNames)) } else { suite.Require().Error(err) suite.Require().Nil(res) @@ -289,9 +302,9 @@ func (suite *KeeperTestSuite) TestBech32Prefix() { func (suite *KeeperTestSuite) TestAddressBytesToString() { testCases := []struct { - msg string - req *types.AddressBytesToStringRequest - expPass bool + msg string + req *types.AddressBytesToStringRequest + expPass bool }{ { "success", @@ -331,9 +344,9 @@ func (suite *KeeperTestSuite) TestAddressBytesToString() { func (suite *KeeperTestSuite) TestAddressStringToBytes() { testCases := []struct { - msg string - req *types.AddressStringToBytesRequest - expPass bool + msg string + req *types.AddressStringToBytesRequest + expPass bool }{ { "success", @@ -352,10 +365,9 @@ func (suite *KeeperTestSuite) TestAddressStringToBytes() { }, { "address prefix is incorrect", - &types.AddressStringToBytesRequest{AddressString: "regen13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv" }, + &types.AddressStringToBytesRequest{AddressString: "regen13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv"}, false, }, - } for _, tc := range testCases {