From bd859a3439232dce8732e99b027f987fe976ef36 Mon Sep 17 00:00:00 2001 From: Troy Kessler <43882936+troykessler@users.noreply.github.com> Date: Thu, 2 Jan 2025 10:47:18 +0100 Subject: [PATCH] refactor: optimized range loops (#215) --- .../keeper/msg_server_skip_uploader_role_test.go | 15 +++++---------- x/pool/keeper/msg_server_disable_pool.go | 3 +-- x/query/keeper/grpc_account_funded.go | 4 +--- x/query/keeper/grpc_query_stakers_by_pool.go | 3 +-- x/stakers/keeper/exported_functions.go | 9 +++------ x/stakers/keeper/keeper.go | 3 +-- 6 files changed, 12 insertions(+), 25 deletions(-) diff --git a/x/bundles/keeper/msg_server_skip_uploader_role_test.go b/x/bundles/keeper/msg_server_skip_uploader_role_test.go index 9d7ed2c3..a8ccf5b3 100644 --- a/x/bundles/keeper/msg_server_skip_uploader_role_test.go +++ b/x/bundles/keeper/msg_server_skip_uploader_role_test.go @@ -176,8 +176,7 @@ var _ = Describe("msg_server_skip_uploader_role.go", Ordered, func() { Expect(found).To(BeFalse()) // check that no validator got a point for the second round - poolAccounts := s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) { Expect(poolAccount.Points).To(BeZero()) } }) @@ -248,8 +247,7 @@ var _ = Describe("msg_server_skip_uploader_role.go", Ordered, func() { Expect(found).To(BeFalse()) // check that no validator got a point for the second round - poolAccounts := s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) { Expect(poolAccount.Points).To(BeZero()) } }) @@ -297,8 +295,7 @@ var _ = Describe("msg_server_skip_uploader_role.go", Ordered, func() { Expect(found).To(BeFalse()) // check that no validator got a point for the second round - poolAccounts := s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) { if poolAccount.Staker == i.STAKER_0 { Expect(poolAccount.Points).To(BeZero()) } else { @@ -364,8 +361,7 @@ var _ = Describe("msg_server_skip_uploader_role.go", Ordered, func() { Expect(finalizedBundle.Uploader).To(Equal(i.STAKER_0)) // check if no validator got a point for the second round - poolAccounts := s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) { Expect(poolAccount.Points).To(BeZero()) } }) @@ -423,8 +419,7 @@ var _ = Describe("msg_server_skip_uploader_role.go", Ordered, func() { Expect(found).To(BeFalse()) // check that no validator got a point - poolAccounts := s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range s.App().StakersKeeper.GetAllPoolAccountsOfPool(s.Ctx(), 0) { Expect(poolAccount.Points).To(BeZero()) } }) diff --git a/x/pool/keeper/msg_server_disable_pool.go b/x/pool/keeper/msg_server_disable_pool.go index fd34663c..e10f76e6 100644 --- a/x/pool/keeper/msg_server_disable_pool.go +++ b/x/pool/keeper/msg_server_disable_pool.go @@ -39,8 +39,7 @@ func (k msgServer) DisablePool( k.SetPool(ctx, pool) // remove all stakers from pool in order to "reset" it - poolMembers := k.stakersKeeper.GetAllStakerAddressesOfPool(ctx, pool.Id) - for _, staker := range poolMembers { + for _, staker := range k.stakersKeeper.GetAllStakerAddressesOfPool(ctx, pool.Id) { k.stakersKeeper.LeavePool(ctx, staker, pool.Id) } diff --git a/x/query/keeper/grpc_account_funded.go b/x/query/keeper/grpc_account_funded.go index d2ad75e9..fffba737 100644 --- a/x/query/keeper/grpc_account_funded.go +++ b/x/query/keeper/grpc_account_funded.go @@ -17,9 +17,7 @@ func (k Keeper) AccountFundedList(goCtx context.Context, req *types.QueryAccount ctx := sdk.UnwrapSDKContext(goCtx) var funded []types.Funded - fundings := k.fundersKeeper.GetFundingsOfFunder(ctx, req.Address) - - for _, funding := range fundings { + for _, funding := range k.fundersKeeper.GetFundingsOfFunder(ctx, req.Address) { if !funding.Amounts.IsZero() { pool, found := k.poolKeeper.GetPool(ctx, funding.PoolId) if !found { diff --git a/x/query/keeper/grpc_query_stakers_by_pool.go b/x/query/keeper/grpc_query_stakers_by_pool.go index b89b1af4..88b2e662 100644 --- a/x/query/keeper/grpc_query_stakers_by_pool.go +++ b/x/query/keeper/grpc_query_stakers_by_pool.go @@ -24,8 +24,7 @@ func (k Keeper) StakersByPool(c context.Context, req *types.QueryStakersByPoolRe stakers := make([]types.FullStaker, 0) - poolAccounts := k.stakerKeeper.GetAllPoolAccountsOfPool(ctx, req.PoolId) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range k.stakerKeeper.GetAllPoolAccountsOfPool(ctx, req.PoolId) { stakers = append(stakers, *k.GetFullStaker(ctx, poolAccount.Staker)) } diff --git a/x/stakers/keeper/exported_functions.go b/x/stakers/keeper/exported_functions.go index c0369461..2fa1c8d6 100644 --- a/x/stakers/keeper/exported_functions.go +++ b/x/stakers/keeper/exported_functions.go @@ -40,8 +40,7 @@ func (k Keeper) LeavePool(ctx sdk.Context, stakerAddress string, poolId uint64) // which have currently a pool account registered for the given pool // and are therefore allowed to participate in that pool. func (k Keeper) GetAllStakerAddressesOfPool(ctx sdk.Context, poolId uint64) (stakers []string) { - poolAccounts := k.GetAllPoolAccountsOfPool(ctx, poolId) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range k.GetAllPoolAccountsOfPool(ctx, poolId) { stakers = append(stakers, poolAccount.Staker) } @@ -137,8 +136,7 @@ func (k Keeper) AssertPoolAccountAuthorized(ctx sdk.Context, stakerAddress strin // GetTotalStakeOfPool returns the amount in uykve which actively secures // the given pool func (k Keeper) GetTotalStakeOfPool(ctx sdk.Context, poolId uint64) (totalStake uint64) { - effectiveStakes := k.GetValidatorPoolStakes(ctx, poolId) - for _, stake := range effectiveStakes { + for _, stake := range k.GetValidatorPoolStakes(ctx, poolId) { totalStake += stake } return @@ -184,8 +182,7 @@ func (k Keeper) GetValidatorPoolStake(ctx sdk.Context, stakerAddress string, poo // GetValidatorTotalPoolStake returns the total stake the validator has combined in every pool func (k Keeper) GetValidatorTotalPoolStake(ctx sdk.Context, staker string) (totalStake uint64) { - poolAccounts := k.GetPoolAccountsFromStaker(ctx, staker) - for _, poolAccount := range poolAccounts { + for _, poolAccount := range k.GetPoolAccountsFromStaker(ctx, staker) { totalStake += k.GetValidatorPoolStake(ctx, poolAccount.Staker, poolAccount.PoolId) } diff --git a/x/stakers/keeper/keeper.go b/x/stakers/keeper/keeper.go index 4ab1b816..ca73adee 100644 --- a/x/stakers/keeper/keeper.go +++ b/x/stakers/keeper/keeper.go @@ -86,8 +86,7 @@ func (k Keeper) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddre func (k Keeper) AfterValidatorBeginUnbonding(goCtx context.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error { ctx := sdk.UnwrapSDKContext(goCtx) - poolAccounts := k.GetPoolAccountsFromStaker(ctx, valAddr.String()) - for _, v := range poolAccounts { + for _, v := range k.GetPoolAccountsFromStaker(ctx, valAddr.String()) { k.LeavePool(ctx, v.Staker, v.PoolId) } return nil