Skip to content

fix(passport): [ID-3766] cache register user promise #2652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions packages/passport/sdk/src/zkEvm/zkEvmProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export class ZkEvmProvider implements Provider {

#signerInitialisationError: unknown | undefined;

/**
* Cache for the user registration promise to prevent race conditions
* when multiple eth_requestAccounts calls happen concurrently
*/
#userRegistrationPromise?: Promise<string> | undefined;

public readonly isPassport: boolean = true;

constructor({
Expand Down Expand Up @@ -133,9 +139,17 @@ export class ZkEvmProvider implements Provider {

#handleLogout = () => {
this.#ethSigner = undefined;
this.#userRegistrationPromise = undefined;
this.#providerEventEmitter.emit(ProviderEvent.ACCOUNTS_CHANGED, []);
};

/**
* Clear the cached user registration promise
*/
#clearUserRegistrationCache = () => {
this.#userRegistrationPromise = undefined;
};

/**
* This method is called by `eth_requestAccounts` and asynchronously initialises the signer.
* The signer is stored in a promise so that it can be retrieved by the provider
Expand Down Expand Up @@ -247,18 +261,29 @@ export class ZkEvmProvider implements Provider {
if (!isZkEvmUser(user)) {
flow.addEvent('startUserRegistration');

const ethSigner = await this.#getSigner();
flow.addEvent('ethSignerResolved');
// Check if registration is already in progress to prevent race conditions
if (!this.#userRegistrationPromise) {
const ethSigner = await this.#getSigner();
flow.addEvent('ethSignerResolved');

// Cache the registration promise to prevent concurrent registrations
this.#userRegistrationPromise = registerZkEvmUser({
ethSigner,
authManager: this.#authManager,
multiRollupApiClients: this.#multiRollupApiClients,
accessToken: user.accessToken,
rpcProvider: this.#rpcProvider,
flow,
});
}

userZkEvmEthAddress = await registerZkEvmUser({
ethSigner,
authManager: this.#authManager,
multiRollupApiClients: this.#multiRollupApiClients,
accessToken: user.accessToken,
rpcProvider: this.#rpcProvider,
flow,
});
flow.addEvent('endUserRegistration');
try {
userZkEvmEthAddress = await this.#userRegistrationPromise;
flow.addEvent('endUserRegistration');
} finally {
// Clear the cache on success or error
this.#clearUserRegistrationCache();
}
} else {
userZkEvmEthAddress = user.zkEvm.ethAddress;
}
Expand Down