Skip to content

Commit 72e27aa

Browse files
committed
fixed multipxe problems
1 parent 4623c57 commit 72e27aa

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

app/src/workers/wallet-worker.ts

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ const ChainInfoSchema = z.object({
3131
version: schemas.Fr,
3232
});
3333

34-
const RUNNING_SESSIONS = new Map<
35-
string,
36-
Map<string, Promise<{ external: ExternalWallet; internal: InternalWallet }>>
37-
>();
34+
// Session data indexed by sessionId (chainId-version)
35+
// Each session contains shared PXE resources and a map of wallets per appId
36+
type SessionData = {
37+
sharedResources: Promise<{
38+
pxe: any;
39+
node: any;
40+
db: any;
41+
pendingAuthorizations: Map<string, any>;
42+
}>;
43+
wallets: Map<string, Promise<{ external: ExternalWallet; internal: InternalWallet }>>;
44+
};
45+
46+
const RUNNING_SESSIONS = new Map<string, SessionData>();
3847

3948
async function init(
4049
chainInfo: ChainInfo,
@@ -57,12 +66,15 @@ async function init(
5766
chainInfo.version = new Fr(rollupVersion);
5867
}
5968
const sessionId = `${chainInfo.chainId.toNumber()}-${chainInfo.version.toNumber()}`;
60-
if (!RUNNING_SESSIONS.get(appId)?.has(sessionId)) {
61-
const internalInit = async () => {
62-
const l1Contracts = await node.getL1ContractAddresses();
6369

64-
const rollupAddress = l1Contracts.rollupAddress;
70+
let session = RUNNING_SESSIONS.get(sessionId);
71+
const walletExists = session?.wallets.has(appId);
6572

73+
// First, ensure we have a session with shared PXE resources
74+
if (!session) {
75+
const pxeInit = (async () => {
76+
const l1Contracts = await node.getL1ContractAddresses();
77+
const rollupAddress = l1Contracts.rollupAddress;
6678
const keychainHomeDir = join(homedir(), "keychain");
6779

6880
const configOverrides: Partial<PXEConfig> = {
@@ -97,6 +109,7 @@ async function init(
97109
walletDBLogger
98110
);
99111
const db = WalletDB.init(walletDBStore, walletDBLogger);
112+
100113
const pxe = await createPXE(
101114
node,
102115
{ ...getPXEConfig(), ...configOverrides },
@@ -111,30 +124,46 @@ async function init(
111124
}
112125
>();
113126

127+
return { pxe, node, db, pendingAuthorizations };
128+
})();
129+
130+
session = {
131+
sharedResources: pxeInit,
132+
wallets: new Map(),
133+
};
134+
RUNNING_SESSIONS.set(sessionId, session);
135+
}
136+
137+
// Wait for the shared PXE to be ready
138+
const sharedResources = await session.sharedResources;
139+
140+
// Now create wallet instances for this specific appId if they don't exist
141+
if (!walletExists) {
142+
const internalInit = async () => {
114143
const externalWalletLogger = createProxyLogger(
115-
"wallet:external",
144+
`wallet:external:${appId}`,
116145
logPort
117146
);
118147
const internalWalletLogger = createProxyLogger(
119-
"wallet:internal",
148+
`wallet:internal:${appId}`,
120149
logPort
121150
);
122151

123152
// Create both wallet instances sharing the same db, pxe and authorization logic
124153
const externalWallet = new ExternalWallet(
125-
pxe,
126-
node,
127-
db,
128-
pendingAuthorizations,
154+
sharedResources.pxe,
155+
sharedResources.node,
156+
sharedResources.db,
157+
sharedResources.pendingAuthorizations,
129158
appId,
130159
chainInfo,
131160
externalWalletLogger
132161
);
133162
const internalWallet = new InternalWallet(
134-
pxe,
135-
node,
136-
db,
137-
pendingAuthorizations,
163+
sharedResources.pxe,
164+
sharedResources.node,
165+
sharedResources.db,
166+
sharedResources.pendingAuthorizations,
138167
appId,
139168
chainInfo,
140169
internalWalletLogger
@@ -175,12 +204,12 @@ async function init(
175204

176205
return { external: externalWallet, internal: internalWallet };
177206
};
178-
const appMap = RUNNING_SESSIONS.get(appId) ?? new Map();
179-
RUNNING_SESSIONS.set(appId, appMap);
207+
180208
const walletPromise = internalInit();
181-
appMap.set(sessionId, walletPromise);
209+
session.wallets.set(appId, walletPromise);
182210
}
183-
const wallets = await RUNNING_SESSIONS.get(appId)!.get(sessionId)!;
211+
212+
const wallets = await session.wallets.get(appId)!;
184213
return wallets;
185214
}
186215

0 commit comments

Comments
 (0)