Skip to content

Commit 45a2971

Browse files
authored
[keyring-controller] Update state in single call when persisting or unlocking (#4154)
## Explanation `persistAllKeyrings` and `#unlockKeyrings` have been refactored to update the state in one single call, to avoid side effects to be executed in the middle of the lock or unlock operations. `persistAllKeyrings` and `#unlockKeyrings` can now be executed once per time, since they are both behind the same mutex: this ensures that each vault read/write operation is executed in a mutually exclusive way <!-- 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? For example: * Fixes #12345 * Related to #67890 --> N.D. ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/keyring-controller` - **FIXED**: State updates are executed in a single call when locking or unlocking the `KeyringController`. ## 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 highlighted breaking changes using the "BREAKING" category above as appropriate
1 parent 4bf3498 commit 45a2971

File tree

3 files changed

+206
-128
lines changed

3 files changed

+206
-128
lines changed

packages/keyring-controller/jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module.exports = merge(baseConfig, {
1919
global: {
2020
branches: 95.65,
2121
functions: 100,
22-
lines: 99.17,
23-
statements: 99.18,
22+
lines: 98.99,
23+
statements: 99,
2424
},
2525
},
2626

packages/keyring-controller/src/KeyringController.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,36 @@ describe('KeyringController', () => {
31433143
});
31443144
});
31453145
});
3146+
3147+
describe('run conditions', () => {
3148+
describe('submitPassword', () => {
3149+
it('should not cause run conditions when called multiple times', async () => {
3150+
await withController(async ({ controller, initialState }) => {
3151+
await Promise.all([
3152+
controller.submitPassword(password),
3153+
controller.submitPassword(password),
3154+
controller.submitPassword(password),
3155+
controller.submitPassword(password),
3156+
]);
3157+
3158+
expect(controller.state).toStrictEqual(initialState);
3159+
});
3160+
});
3161+
3162+
it('should not cause run conditions when called multiple times in combination with persistAllKeyrings', async () => {
3163+
await withController(async ({ controller, initialState }) => {
3164+
await Promise.all([
3165+
controller.submitPassword(password),
3166+
controller.persistAllKeyrings(),
3167+
controller.submitPassword(password),
3168+
controller.persistAllKeyrings(),
3169+
]);
3170+
3171+
expect(controller.state).toStrictEqual(initialState);
3172+
});
3173+
});
3174+
});
3175+
});
31463176
});
31473177

31483178
type WithControllerCallback<ReturnValue> = ({

0 commit comments

Comments
 (0)