Skip to content

Commit

Permalink
chore: optimized range loops
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Dec 23, 2024
1 parent 37f0e06 commit 053a3b6
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 25 deletions.
15 changes: 5 additions & 10 deletions x/bundles/keeper/msg_server_skip_uploader_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
Expand Down Expand Up @@ -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())
}
})
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
})
Expand Down Expand Up @@ -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())
}
})
Expand Down
3 changes: 1 addition & 2 deletions x/pool/keeper/msg_server_disable_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 1 addition & 3 deletions x/query/keeper/grpc_account_funded.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions x/query/keeper/grpc_query_stakers_by_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
9 changes: 3 additions & 6 deletions x/stakers/keeper/exported_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -143,8 +142,7 @@ func (k Keeper) GetActiveStakers(ctx sdk.Context) []string {
// 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
Expand Down Expand Up @@ -190,8 +188,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)
}

Expand Down
3 changes: 1 addition & 2 deletions x/stakers/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 053a3b6

Please sign in to comment.