Skip to content

Commit 444bd1b

Browse files
committed
closing db, correct stub side effects
1 parent 7736188 commit 444bd1b

8 files changed

Lines changed: 89 additions & 18 deletions

File tree

noir-projects/noir-contracts/contracts/account/simulated_ecdsa_account_contract/src/main.nr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ pub contract SimulatedEcdsaAccount {
2828
// used for any security purpose.
2929
let seed = unsafe { random() };
3030

31-
// Emit the initialization nullifier for the signing_public_key SinglePrivateImmutable.
31+
// Emit the initialization nullifier for the contract itself.
3232
self.context.push_nullifier(seed);
3333

34+
// Emit the initialization nullifier for the signing_public_key SinglePrivateImmutable.
35+
self.context.push_nullifier(seed + 1);
36+
3437
// Emit the note hash for the signing key note.
35-
self.context.push_note_hash(seed + 1);
38+
self.context.push_note_hash(seed + 2);
3639

3740
// Emit a private log tied to the note hash, matching the length of a real note delivery
3841
// log (MESSAGE_CIPHERTEXT_LEN fields). The signing key note is a SinglePrivateImmutable

noir-projects/noir-contracts/contracts/account/simulated_schnorr_account_contract/src/main.nr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ pub contract SimulatedSchnorrAccount {
2828
// security purpose.
2929
let seed = unsafe { random() };
3030

31-
// Emit the initialization nullifier for the signing_public_key SinglePrivateImmutable.
31+
// Emit the initialization nullifier for the contract itself.
3232
self.context.push_nullifier(seed);
3333

34+
// Emit the initialization nullifier for the signing_public_key SinglePrivateImmutable.
35+
self.context.push_nullifier(seed + 1);
36+
3437
// Emit the note hash for the signing key note.
35-
self.context.push_note_hash(seed + 1);
38+
self.context.push_note_hash(seed + 2);
3639

3740
// Emit a private log tied to the note hash, matching the length of a real note delivery
3841
// log (MESSAGE_CIPHERTEXT_LEN fields). The signing key note is a SinglePrivateImmutable

yarn-project/end-to-end/src/e2e_kernelless_simulation.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import { EcdsaKAccountContract } from '@aztec/accounts/ecdsa';
2+
import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
3+
import { NO_FROM } from '@aztec/aztec.js/account';
14
import { AztecAddress } from '@aztec/aztec.js/addresses';
25
import { CallAuthorizationRequest } from '@aztec/aztec.js/authorization';
36
import { Fr } from '@aztec/aztec.js/fields';
47
import type { Logger } from '@aztec/aztec.js/log';
58
import type { AztecNode } from '@aztec/aztec.js/node';
9+
import { randomBytes } from '@aztec/foundation/crypto/random';
10+
import { Fq } from '@aztec/foundation/curves/bn254';
611
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
712
import { type TokenContract, TokenContractArtifact } from '@aztec/noir-contracts.js/Token';
813
import { AuthWitTestContract, AuthWitTestContractArtifact } from '@aztec/noir-test-contracts.js/AuthWitTest';
@@ -420,4 +425,60 @@ describe('Kernelless simulation', () => {
420425
noteHashMembershipWitnessSpy.mockRestore();
421426
});
422427
});
428+
429+
describe('account contract deployment', () => {
430+
it('simulates Schnorr account deployment and gas matches with-kernels counterpart', async () => {
431+
const signingKey = Fq.random();
432+
const accountManager = await wallet.createAccount({
433+
secret: Fr.random(),
434+
salt: Fr.random(),
435+
contract: new SchnorrAccountContract(signingKey),
436+
type: 'schnorr',
437+
});
438+
const deployMethod = await accountManager.getDeployMethod();
439+
const deployOptions = { from: NO_FROM, skipClassPublication: true };
440+
441+
wallet.setSimulationMode('kernelless-override');
442+
const kernellessResult = await deployMethod.simulate(deployOptions);
443+
const kernellessGas = kernellessResult.estimatedGas!;
444+
445+
wallet.setSimulationMode('full');
446+
const withKernelsResult = await deployMethod.simulate(deployOptions);
447+
const withKernelsGas = withKernelsResult.estimatedGas!;
448+
449+
logger.info(`Schnorr kernelless gas: L2=${kernellessGas.gasLimits.l2Gas} DA=${kernellessGas.gasLimits.daGas}`);
450+
logger.info(
451+
`Schnorr with kernels gas: L2=${withKernelsGas.gasLimits.l2Gas} DA=${withKernelsGas.gasLimits.daGas}`,
452+
);
453+
454+
expect(kernellessGas.gasLimits.daGas).toEqual(withKernelsGas.gasLimits.daGas);
455+
expect(kernellessGas.gasLimits.l2Gas).toEqual(withKernelsGas.gasLimits.l2Gas);
456+
});
457+
458+
it('simulates ECDSA account deployment and gas matches with-kernels counterpart', async () => {
459+
const signingKey = randomBytes(32);
460+
const accountManager = await wallet.createAccount({
461+
secret: Fr.random(),
462+
salt: Fr.random(),
463+
contract: new EcdsaKAccountContract(signingKey),
464+
type: 'ecdsasecp256k1',
465+
});
466+
const deployMethod = await accountManager.getDeployMethod();
467+
const deployOptions = { from: NO_FROM, skipClassPublication: true };
468+
469+
wallet.setSimulationMode('kernelless-override');
470+
const kernellessResult = await deployMethod.simulate(deployOptions);
471+
const kernellessGas = kernellessResult.estimatedGas!;
472+
473+
wallet.setSimulationMode('full');
474+
const withKernelsResult = await deployMethod.simulate(deployOptions);
475+
const withKernelsGas = withKernelsResult.estimatedGas!;
476+
477+
logger.info(`ECDSA kernelless gas: L2=${kernellessGas.gasLimits.l2Gas} DA=${kernellessGas.gasLimits.daGas}`);
478+
logger.info(`ECDSA with kernels gas: L2=${withKernelsGas.gasLimits.l2Gas} DA=${withKernelsGas.gasLimits.daGas}`);
479+
480+
expect(kernellessGas.gasLimits.daGas).toEqual(withKernelsGas.gasLimits.daGas);
481+
expect(kernellessGas.gasLimits.l2Gas).toEqual(withKernelsGas.gasLimits.l2Gas);
482+
});
483+
});
423484
});

yarn-project/wallets/src/embedded/embedded_wallet.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ export class EmbeddedWallet extends BaseWallet {
360360
this.estimatedGasPadding = value ?? DEFAULT_ESTIMATED_GAS_PADDING;
361361
}
362362

363-
stop() {
364-
return this.pxe.stop();
363+
async stop(): Promise<void> {
364+
await this.pxe.stop();
365+
await this.walletDB.close();
365366
}
366367
}

yarn-project/wallets/src/embedded/entrypoints/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class BrowserEmbeddedWallet extends EmbeddedWallet {
6767
1,
6868
rootLogger.createChild('wallet:data'),
6969
));
70-
const walletDB = WalletDB.init(walletDBStore, rootLogger.createChild('wallet:db').info);
70+
const walletDB = new WalletDB(walletDBStore, rootLogger.createChild('wallet:db').info);
7171

7272
return new this(pxe, aztecNode, walletDB, new LazyAccountContractsProvider(), rootLogger) as T;
7373
}

yarn-project/wallets/src/embedded/entrypoints/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class NodeEmbeddedWallet extends EmbeddedWallet {
7474
},
7575
rootLogger.createChild('wallet:data').getBindings(),
7676
));
77-
const walletDB = WalletDB.init(walletDBStore, rootLogger.createChild('wallet:db').info);
77+
const walletDB = new WalletDB(walletDBStore, rootLogger.createChild('wallet:db').info);
7878

7979
return new this(pxe, aztecNode, walletDB, new BundleAccountContractsProvider(), rootLogger) as T;
8080
}

yarn-project/wallets/src/embedded/wallet_db.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('WalletDB', () => {
1010

1111
beforeEach(async () => {
1212
const store = await openTmpStore('wallet-db-test');
13-
db = WalletDB.init(store, () => {});
13+
db = new WalletDB(store, () => {});
1414
});
1515

1616
function makeAccountData(type: AccountType = 'schnorr', alias?: string) {

yarn-project/wallets/src/embedded/wallet_db.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ function accountKey(field: string, address: AztecAddress | string): string {
1212
}
1313

1414
export class WalletDB {
15-
private constructor(
16-
private accounts: AztecAsyncMap<string, Buffer>,
17-
private aliases: AztecAsyncMap<string, Buffer>,
18-
private userLog: LogFn,
19-
) {}
15+
private accounts: AztecAsyncMap<string, Buffer>;
16+
private aliases: AztecAsyncMap<string, Buffer>;
2017

21-
static init(store: AztecAsyncKVStore, userLog: LogFn) {
22-
const accounts = store.openMap<string, Buffer>('accounts');
23-
const aliases = store.openMap<string, Buffer>('aliases');
24-
return new WalletDB(accounts, aliases, userLog);
18+
constructor(
19+
private store: AztecAsyncKVStore,
20+
private userLog: LogFn,
21+
) {
22+
this.accounts = store.openMap<string, Buffer>('accounts');
23+
this.aliases = store.openMap<string, Buffer>('aliases');
2524
}
2625

2726
async storeAccount(
@@ -131,4 +130,8 @@ export class WalletDB {
131130
await this.aliases.delete(`accounts:${alias}`);
132131
}
133132
}
133+
134+
async close() {
135+
await this.store.close();
136+
}
134137
}

0 commit comments

Comments
 (0)