Skip to content

Commit ea7bdd1

Browse files
fix: Do not call Remove during Walk (cosmos#19833)
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
1 parent 4599439 commit ea7bdd1

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
9898
* (baseapp) [#18551](https://github.com/cosmos/cosmos-sdk/pull/18551) Fix SelectTxForProposal the calculation method of tx bytes size is inconsistent with CometBFT
9999
* (server) [#18994](https://github.com/cosmos/cosmos-sdk/pull/18994) Update server context directly rather than a reference to a sub-object
100100
* (crypto) [#19691](https://github.com/cosmos/cosmos-sdk/pull/19691) Fix tx sign doesn't throw an error when incorrect Ledger is used.
101+
* [#19833](https://github.com/cosmos/cosmos-sdk/pull/19833) Fix some places in which we call Remove inside a Walk.
101102

102103
### API Breaking Changes
103104

x/feegrant/keeper/keeper.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,15 @@ func (k Keeper) RemoveExpiredAllowances(ctx context.Context, limit int) error {
302302
rng := collections.NewPrefixUntilTripleRange[time.Time, sdk.AccAddress, sdk.AccAddress](exp)
303303
count := 0
304304

305+
keysToRemove := []collections.Triple[time.Time, sdk.AccAddress, sdk.AccAddress]{}
305306
err := k.FeeAllowanceQueue.Walk(ctx, rng, func(key collections.Triple[time.Time, sdk.AccAddress, sdk.AccAddress], value bool) (stop bool, err error) {
306307
grantee, granter := key.K2(), key.K3()
307308

308309
if err := k.FeeAllowance.Remove(ctx, collections.Join(grantee, granter)); err != nil {
309310
return true, err
310311
}
311312

312-
if err := k.FeeAllowanceQueue.Remove(ctx, key); err != nil {
313-
return true, err
314-
}
313+
keysToRemove = append(keysToRemove, key)
315314

316315
// limit the amount of iterations to avoid taking too much time
317316
count++
@@ -325,5 +324,11 @@ func (k Keeper) RemoveExpiredAllowances(ctx context.Context, limit int) error {
325324
return err
326325
}
327326

327+
for _, key := range keysToRemove {
328+
if err := k.FeeAllowanceQueue.Remove(ctx, key); err != nil {
329+
return err
330+
}
331+
}
332+
328333
return nil
329334
}

x/gov/keeper/tally.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func defaultCalculateVoteResultsAndVotingPower(
247247

248248
// iterate over all votes, tally up the voting power of each validator
249249
rng := collections.NewPrefixedPairRange[uint64, sdk.AccAddress](proposalID)
250+
votesToRemove := []collections.Pair[uint64, sdk.AccAddress]{}
250251
if err := k.Votes.Walk(ctx, rng, func(key collections.Pair[uint64, sdk.AccAddress], vote v1.Vote) (bool, error) {
251252
// if validator, just record it in the map
252253
voter, err := k.authKeeper.AddressCodec().StringToBytes(vote.Voter)
@@ -292,11 +293,19 @@ func defaultCalculateVoteResultsAndVotingPower(
292293
return false, err
293294
}
294295

295-
return false, k.Votes.Remove(ctx, collections.Join(vote.ProposalId, sdk.AccAddress(voter)))
296+
votesToRemove = append(votesToRemove, key)
297+
return false, nil
296298
}); err != nil {
297299
return math.LegacyDec{}, nil, err
298300
}
299301

302+
// remove all votes from store
303+
for _, key := range votesToRemove {
304+
if err := k.Votes.Remove(ctx, key); err != nil {
305+
return math.LegacyDec{}, nil, err
306+
}
307+
}
308+
300309
// iterate over the validators again to tally their voting power
301310
for _, val := range validators {
302311
if len(val.Vote) == 0 {

x/slashing/keeper/signing_info.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,7 @@ func (k Keeper) DeleteMissedBlockBitmap(ctx context.Context, addr sdk.ConsAddres
182182
}
183183

184184
rng := collections.NewPrefixedPairRange[[]byte, uint64](addr.Bytes())
185-
return k.ValidatorMissedBlockBitmap.Walk(ctx, rng, func(key collections.Pair[[]byte, uint64], value []byte) (bool, error) {
186-
err := k.ValidatorMissedBlockBitmap.Remove(ctx, key)
187-
if err != nil {
188-
return true, err
189-
}
190-
return false, nil
191-
})
185+
return k.ValidatorMissedBlockBitmap.Clear(ctx, rng)
192186
}
193187

194188
// IterateMissedBlockBitmap iterates over a validator's signed blocks window

x/staking/keeper/cons_pubkey.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ func (k Keeper) deleteConsKeyIndexKey(ctx context.Context, valAddr sdk.ValAddres
202202
StartInclusive(collections.Join(valAddr.Bytes(), time.Time{})).
203203
EndInclusive(collections.Join(valAddr.Bytes(), ts))
204204

205-
return k.ValidatorConsensusKeyRotationRecordIndexKey.Walk(ctx, rng, func(key collections.Pair[[]byte, time.Time]) (stop bool, err error) {
206-
return false, k.ValidatorConsensusKeyRotationRecordIndexKey.Remove(ctx, key)
207-
})
205+
return k.ValidatorConsensusKeyRotationRecordIndexKey.Clear(ctx, rng)
208206
}
209207

210208
// getAndRemoveAllMaturedRotatedKeys returns all matured valaddresses.
@@ -213,14 +211,23 @@ func (k Keeper) getAndRemoveAllMaturedRotatedKeys(ctx context.Context, matureTim
213211

214212
// get an iterator for all timeslices from time 0 until the current HeaderInfo time
215213
rng := new(collections.Range[time.Time]).EndInclusive(matureTime)
214+
keysToRemove := []time.Time{}
216215
err := k.ValidatorConsensusKeyRotationRecordQueue.Walk(ctx, rng, func(key time.Time, value types.ValAddrsOfRotatedConsKeys) (stop bool, err error) {
217216
valAddrs = append(valAddrs, value.Addresses...)
218-
return false, k.ValidatorConsensusKeyRotationRecordQueue.Remove(ctx, key)
217+
keysToRemove = append(keysToRemove, key)
218+
return false, nil
219219
})
220220
if err != nil {
221221
return nil, err
222222
}
223223

224+
// remove all the keys from the list
225+
for _, key := range keysToRemove {
226+
if err := k.ValidatorConsensusKeyRotationRecordQueue.Remove(ctx, key); err != nil {
227+
return nil, err
228+
}
229+
}
230+
224231
return valAddrs, nil
225232
}
226233

0 commit comments

Comments
 (0)