Skip to content

[pull] main from MetaMask:main#322

Merged
pull[bot] merged 3 commits into
dmrazzy:mainfrom
MetaMask:main
Sep 5, 2025
Merged

[pull] main from MetaMask:main#322
pull[bot] merged 3 commits into
dmrazzy:mainfrom
MetaMask:main

Conversation

@pull

@pull pull Bot commented Sep 5, 2025

Copy link
Copy Markdown

See Commits and Changes for more details.


Created by pull[bot] (v2.0.0-alpha.3)

Can you help keep this open source service alive? 💖 Please sponsor : )

lwin-kyaw and others added 3 commits September 5, 2025 12:39
## Explanation

This PR adds `lock (mutex)` mechanism to the
seedless-onboarding-controller `setLocked` method to achieve the atomic
state updates and prevent unexpected issues (state out of sync) while
locking the controller and another operation (such as `changePassword`,
`addNewSecretData` etc) is in progress on the other hand.

As a result, we can't simply lock the seedless-onboarding-controller,
syncing with keyring's. Syncing with keyring's lock will cause more
issues in the controller communications as seedless-onboarding
`setLocked`, now, has some side effects and it won't get locked
immediately in some scenarios (when another seedless operation in
progress).
Hence, `setLocked` becomes independent of the Keyring's and this PR also
removes the `KeyringController:lock` and `KeyringController:unlock`
events from the seedless-onboarding messenger.

**BREAKING:**
- Removed `Keyring:lock` and `Keyring:unlock` events from the controller
allowed events.
- 

## References

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs),
highlighting breaking changes as necessary
- [x] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
## Explanation

### Current State
The `TokenBalancesController` currently uses a single global polling
interval for all chains, with the implementation of *WebSocket-based
balance updates** we need **dynamic interval management** per chain (for
websocket-supported chains and for non-supported chains):

- **WebSocket-supported chains** should use minimal polling when
connected (WebSocket provides real-time updates)
- **WebSocket-disconnected chains** need frequent polling as fallback
- **WebSocket connection status changes dynamically**, requiring runtime
interval adjustments
- The current single-interval approach cannot adapt to changing
WebSocket connectivity states

### Solution
This PR introduces **dynamic per-chain polling intervals** that
integrate seamlessly with the **WebSocket service's connectivity
management**:

1. **Dynamic Interval Control**: WebSocket service can adjust polling
intervals in real-time via messenger actions based on connection status
2. **Connectivity-Aware Polling**: Connected chains use long intervals
(WebSocket handles updates), disconnected chains use short intervals
(polling fallback)
3. **Interval Grouping Strategy**: Chains with the same polling interval
are batched together, preserving AccountsAPI efficiency
4. **Messenger Integration**: `updateChainPollingConfigs` action enables
WebSocket service to control intervals dynamically

### ⚠️ **IMPORTANT: This is a Non-Breaking Change**
**All existing code continues to work exactly as it does today**:
- Existing `TokenBalancesController` instantiation without
`chainPollingIntervals` works unchanged
- Default polling behavior remains identical (single global interval)
- All existing APIs (`startPolling`, `stopPolling`, `updateBalances`,
etc.) work as before
- No changes required in MetaMask Extension or any consuming code

The new functionality is **purely additive** and **opt-in**.

### WebSocket Service Integration Flow
```typescript
// WebSocket service manages intervals based on connection status:

// When WebSocket connects, it calls:
this.controllerMessenger.call('TokenBalancesController:updateChainPollingConfigs', {
  '0x1': { interval: 300000 } // 5min - WebSocket handles real-time updates
});

// When WebSocket disconnects from a chain:  
this.controllerMessenger.call('TokenBalancesController:updateChainPollingConfigs', {
  '0x1': { interval: 30000 } // 30s - fallback to frequent polling
});
```

### Technical Implementation
- **Real-time Adaptation**: WebSocket service dynamically adjusts
intervals based on connection health
- **Efficient Batching**: Chains are automatically regrouped by interval
when WebSocket service updates configs
- **Seamless Fallback**: Disconnected chains immediately switch to
frequent polling without manual intervention
- **Connection Recovery**: When WebSocket reconnects, polling
automatically reduces to backup frequency
- **Backward Compatibility**: All existing functionality preserved - new
features only activate when explicitly configured

### Example Runtime Behavior
```typescript
// Current behavior (unchanged):
new TokenBalancesController({ messenger, interval: 30000 })
// → All chains poll every 30s (exactly as today)

// New opt-in behavior:
new TokenBalancesController({ 
  messenger, 
  interval: 30000, // Default fallback
  chainPollingIntervals: { '0x1': { interval: 300000 } } // Ethereum: 5min
})
// → Ethereum polls every 5min, other chains every 30s
```

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- Added comprehensive test coverage for per-chain polling intervals and
dynamic updates
- Added tests for interval grouping, runtime configuration changes, and
batching behavior
  - **All existing tests continue to pass unchanged**
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
  - Added JSDoc comments for messenger actions and configuration options
- [x] I've communicated my changes to consumers by updating changelogs
for packages I've changed, highlighting breaking changes as necessary
- **Note**: **NO BREAKING CHANGES** - this is a purely additive
enhancement
- [x] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
- **Note**: **NO BREAKING CHANGES** - existing code works unchanged,
WebSocket service integration uses new opt-in messenger actions

---------

Co-authored-by: Salim TOUBAL <salim.toubal@outlook.com>
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

## References

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs),
highlighting breaking changes as necessary
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
@pull pull Bot locked and limited conversation to collaborators Sep 5, 2025
@pull pull Bot added the ⤵️ pull label Sep 5, 2025
@pull pull Bot merged commit e98f9ee into dmrazzy:main Sep 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants