Skip to content

Wakamex fix/state cache chain collision#4002

Merged
tjayrush merged 3 commits into
developfrom
wakamex-fix/state-cache-chain-collision
Jun 17, 2025
Merged

Wakamex fix/state cache chain collision#4002
tjayrush merged 3 commits into
developfrom
wakamex-fix/state-cache-chain-collision

Conversation

@tjayrush

Copy link
Copy Markdown
Member

Description

PR Target Branch

  • I am targeting the develop branch for non-release changes.

wakamex and others added 3 commits June 16, 2025 19:17
This commit fixes issue #3993 where the in-memory balance cache in GetBalanceAt()
doesn't include the chain identifier in its cache key, causing potential collisions
when the same process handles multiple chains.

The issue:
- GetBalanceAt uses an in-memory cache with key format: "address|blockNumber"
- If the same Connection or process handles multiple chains (e.g., mainnet and gnosis),
  the cache could return incorrect balances from a different chain

The fix:
- Changed cache key format to include chain: "chain|address|blockNumber"
- This ensures each chain has separate cache entries in the in-memory map

Note: The filesystem cache already properly separates by chain through directory
structure, so this fix only affects the in-memory cache.

Example scenario that would trigger the bug:
1. Query balance on mainnet: chifra state --chain mainnet <addr> <block>
2. Query same address/block on gnosis: chifra state --chain gnosis <addr> <block>
3. Without this fix, the second query might return the cached mainnet balance\!

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This test verifies that the in-memory balance cache correctly
includes the chain identifier in cache keys to prevent collisions
when the same process handles multiple chains.

The test demonstrates:
- How the fixed cache key format prevents collisions
- What would happen with the old format (one chain overwrites another)
- The exact cache key format for different scenarios

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@tjayrush

Copy link
Copy Markdown
Member Author

Fixes issue: #3993

@tjayrush
tjayrush merged commit de065b8 into develop Jun 17, 2025
3 checks passed
@tjayrush
tjayrush deleted the wakamex-fix/state-cache-chain-collision branch June 17, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants