Skip to content

feat(giga): implement iterator for the cachekv#2994

Open
arajasek wants to merge 1 commit intomainfrom
asr/iterator
Open

feat(giga): implement iterator for the cachekv#2994
arajasek wants to merge 1 commit intomainfrom
asr/iterator

Conversation

@arajasek
Copy link
Contributor

Describe your changes and provide context

Since we need Giga to interoperate with v2, we need to return all balances for accounts that are being migrated. This PR implements the Iterator to do that.

If we don't want to implement Iterator on the cachekv, an alternative is to just squish the logic into GetAllBalances or SpendableCoins directly. That would look something like this:

// rangeStore is an interface for stores that support GetAllKeyStrsInRange
type rangeStore interface {
	GetAllKeyStrsInRange(start, end []byte) []string
	Get(key []byte) []byte
}

// GetAllBalances returns all the balances for a given account address.
func (k BaseViewKeeper) GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
	store := k.GetKVStore(ctx)
	accountPrefix := types.CreateAccountBalancesPrefix(addr)

	// Try to use GetAllKeyStrsInRange directly (avoids Iterator which may not be supported)
	if rs, ok := store.(rangeStore); ok {
		// Calculate end key for prefix range (prefix + 0xFF...)
		end := make([]byte, len(accountPrefix))
		copy(end, accountPrefix)
		for i := len(end) - 1; i >= 0; i-- {
			end[i]++
			if end[i] != 0 {
				break
			}
		}

		keyStrs := rs.GetAllKeyStrsInRange(accountPrefix, end)
		balances := sdk.NewCoins()
		for _, keyStr := range keyStrs {
			bz := rs.Get([]byte(keyStr))
			if bz != nil {
				var balance sdk.Coin
				k.cdc.MustUnmarshal(bz, &balance)
				balances = append(balances, balance)
			}
		}
		return balances.Sort()
	}

	// Fall back to standard iteration for non-GIGA stores
	accountStore := prefix.NewStore(store, accountPrefix)
	balances := sdk.NewCoins()
	iterator := accountStore.Iterator(nil, nil)
	defer iterator.Close()

	for ; iterator.Valid(); iterator.Next() {
		var balance sdk.Coin
		k.cdc.MustUnmarshal(iterator.Value(), &balance)
		balances = append(balances, balance)
	}

	return balances.Sort()
}

Testing performed to validate your change

@github-actions
Copy link

github-actions bot commented Feb 27, 2026

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedFeb 27, 2026, 7:36 PM


func (mi *memIterator) Key() []byte {
if !mi.Valid() {
panic("iterator is invalid")

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

func (mi *memIterator) Value() []byte {
if !mi.Valid() {
panic("iterator is invalid")

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

❌ Patch coverage is 31.11111% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.12%. Comparing base (7cfe94a) to head (b2cffcc).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
giga/deps/store/cachekv.go 0.00% 30 Missing ⚠️
giga/deps/xbank/keeper/view.go 93.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2994      +/-   ##
==========================================
- Coverage   58.12%   58.12%   -0.01%     
==========================================
  Files        2111     2111              
  Lines      173544   173583      +39     
==========================================
+ Hits       100879   100889      +10     
- Misses      63706    63735      +29     
  Partials     8959     8959              
Flag Coverage Δ
sei-chain-pr 59.18% <31.11%> (?)
sei-db 69.50% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
giga/deps/xbank/keeper/view.go 92.98% <93.33%> (+1.49%) ⬆️
giga/deps/store/cachekv.go 42.01% <0.00%> (-13.54%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant