Skip to content

Commit e572e63

Browse files
committed
refactor(cli-wallet): key stub-class cache on AccountType
Match the embedded wallet's pattern: cache the stub class id and preimage keyed on AccountType rather than on a 'schnorr' | 'ecdsa' flavor union, and do the schnorr-vs-ecdsa artifact dispatch inline. The two ECDSA AccountType values share a stub artifact, so on first miss for each variant we'll hash and re-register the same artifact — a bounded one-time cost per type that's worth the simpler shape.
1 parent 0035a10 commit e572e63

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

yarn-project/cli-wallet/src/utils/wallet.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ import { printGasEstimates } from './options/fees.js';
3232
export class CLIWallet extends BaseWallet {
3333
private accountCache = new Map<string, Account>();
3434
/**
35-
* Per-stub-flavor cache of the stub class id and preimage. The Promise is stored (not the resolved
36-
* value) so concurrent first-time callers dedupe on the same hashing + registration work.
35+
* Per-account-type cache of the stub class id and preimage. The Promise is stored (not the resolved
36+
* value) so concurrent first-time callers dedupe on the same hashing + registration work. ECDSA
37+
* variants all map to the same stub artifact but get their own cache slot, so we may hash + register
38+
* the same artifact one extra time per variant on first miss; that cost is bounded and one-time.
3739
*/
38-
#stubClasses = new Map<'schnorr' | 'ecdsa', Promise<ContractClassWithId & ContractClassIdPreimage>>();
40+
#stubClasses = new Map<AccountType, Promise<ContractClassWithId & ContractClassIdPreimage>>();
3941

4042
constructor(
4143
pxe: PXE,
@@ -204,27 +206,27 @@ export class CLIWallet extends BaseWallet {
204206
throw new Error(`No contract instance found for address: ${originalAddress.address}`);
205207
}
206208
const { type } = await this.db!.retrieveAccount(address);
207-
const isSchnorr = type === 'schnorr';
208-
const stubAccount = isSchnorr ? createStubSchnorrAccount(originalAddress) : createStubEcdsaAccount(originalAddress);
209-
const { id: stubClassId } = await this.#getStubClass(isSchnorr ? 'schnorr' : 'ecdsa');
209+
const stubAccount =
210+
type === 'schnorr' ? createStubSchnorrAccount(originalAddress) : createStubEcdsaAccount(originalAddress);
211+
const { id: stubClassId } = await this.#getStubClass(type);
210212
const instance = { ...contractInstance, currentContractClassId: stubClassId };
211213
return { account: stubAccount, instance };
212214
}
213215

214216
/**
215-
* Lazily hashes and registers the stub class for the given flavor, caching the result so
217+
* Lazily hashes and registers the stub class for the given account type, caching the result so
216218
* subsequent simulations skip the artifact-hashing + registration round-trip.
217219
*/
218-
#getStubClass(flavor: 'schnorr' | 'ecdsa'): Promise<ContractClassWithId & ContractClassIdPreimage> {
219-
let cached = this.#stubClasses.get(flavor);
220+
#getStubClass(type: AccountType): Promise<ContractClassWithId & ContractClassIdPreimage> {
221+
let cached = this.#stubClasses.get(type);
220222
if (!cached) {
221223
cached = (async () => {
222-
const artifact = flavor === 'schnorr' ? StubSchnorrAccountContractArtifact : StubEcdsaAccountContractArtifact;
224+
const artifact = type === 'schnorr' ? StubSchnorrAccountContractArtifact : StubEcdsaAccountContractArtifact;
223225
const stubClass = await getContractClassFromArtifact(artifact);
224226
await this.pxe.registerContractClass(artifact);
225227
return stubClass;
226228
})();
227-
this.#stubClasses.set(flavor, cached);
229+
this.#stubClasses.set(type, cached);
228230
}
229231
return cached;
230232
}

0 commit comments

Comments
 (0)