Skip to content

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

Merged
arajasek merged 1 commit intomainfrom
asr/iterator
Mar 9, 2026
Merged

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

Conversation

@arajasek
Copy link
Copy Markdown
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
Copy Markdown

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✅ passedMar 9, 2026, 5:10 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
Copy Markdown

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.31%. Comparing base (aabf783) to head (3527b41).
⚠️ Report is 1 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.32%   58.31%   -0.01%     
==========================================
  Files        2079     2079              
  Lines      171753   171792      +39     
==========================================
+ Hits       100172   100182      +10     
- Misses      62652    62681      +29     
  Partials     8929     8929              
Flag Coverage Δ
sei-chain-pr 59.18% <31.11%> (?)
sei-db 70.41% <ø> (ø)

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.

@arajasek arajasek enabled auto-merge (squash) March 9, 2026 17:09
@arajasek arajasek merged commit 1ecfed0 into main Mar 9, 2026
35 checks passed
@arajasek arajasek deleted the asr/iterator branch March 9, 2026 17:27
github-merge-queue Bot pushed a commit that referenced this pull request Mar 30, 2026
## Describe your changes and provide context

#2994 introduced an iterator for the cachekv store for the specific
purpose of migrating balances during association. There are concerns
with that change -- in particular future stores won't support iterators
at all.

Instead, we can simply fallback to v2 when associatons need to occur,
similar to how we handle interop. This PR makes that change, and adds a
test for it.

## Testing performed to validate your change
github-actions Bot pushed a commit that referenced this pull request Mar 30, 2026
## Describe your changes and provide context

#2994 introduced an iterator for the cachekv store for the specific
purpose of migrating balances during association. There are concerns
with that change -- in particular future stores won't support iterators
at all.

Instead, we can simply fallback to v2 when associatons need to occur,
similar to how we handle interop. This PR makes that change, and adds a
test for it.

## Testing performed to validate your change

(cherry picked from commit 95ed1f5)
jewei1997 pushed a commit that referenced this pull request Mar 30, 2026
## Describe your changes and provide context

#2994 introduced an iterator for the cachekv store for the specific
purpose of migrating balances during association. There are concerns
with that change -- in particular future stores won't support iterators
at all.

Instead, we can simply fallback to v2 when associatons need to occur,
similar to how we handle interop. This PR makes that change, and adds a
test for it.

## Testing performed to validate your change
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.

4 participants