-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathpxe_service.ts
660 lines (580 loc) · 25.1 KB
/
pxe_service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import {
AcirSimulator,
ExecutionResult,
collectEncryptedLogs,
collectEnqueuedPublicFunctionCalls,
collectUnencryptedLogs,
resolveOpcodeLocations,
} from '@aztec/acir-simulator';
import {
AztecAddress,
CircuitsWasm,
CompleteAddress,
EthAddress,
FunctionData,
GrumpkinPrivateKey,
KernelCircuitPublicInputsFinal,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX,
PartialAddress,
PublicCallRequest,
} from '@aztec/circuits.js';
import { computeCommitmentNonce, siloNullifier } from '@aztec/circuits.js/abis';
import { encodeArguments } from '@aztec/foundation/abi';
import { padArrayEnd } from '@aztec/foundation/collection';
import { Fr, Point } from '@aztec/foundation/fields';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import NoirVersion from '@aztec/noir-compiler/noir-version';
import {
AuthWitness,
AztecNode,
ContractDao,
ContractData,
DeployedContract,
ExtendedContractData,
FunctionCall,
KeyStore,
L2Block,
L2BlockL2Logs,
L2Tx,
LogType,
MerkleTreeId,
NodeInfo,
NotePreimage,
PXE,
SimulationError,
Tx,
TxExecutionRequest,
TxHash,
TxL2Logs,
TxReceipt,
TxStatus,
getNewContractPublicFunctions,
isNoirCallStackUnresolved,
toContractDao,
} from '@aztec/types';
import { PXEServiceConfig, getPackageInfo } from '../config/index.js';
import { ContractDataOracle } from '../contract_data_oracle/index.js';
import { Database } from '../database/index.js';
import { KernelOracle } from '../kernel_oracle/index.js';
import { KernelProver } from '../kernel_prover/kernel_prover.js';
import { getAcirSimulator } from '../simulator/index.js';
import { Synchronizer } from '../synchronizer/index.js';
/**
* A Private eXecution Environment (PXE) implementation.
*/
export class PXEService implements PXE {
private synchronizer: Synchronizer;
private contractDataOracle: ContractDataOracle;
private simulator: AcirSimulator;
private log: DebugLogger;
private sandboxVersion: string;
constructor(
private keyStore: KeyStore,
private node: AztecNode,
private db: Database,
private config: PXEServiceConfig,
logSuffix?: string,
) {
this.log = createDebugLogger(logSuffix ? `aztec:pxe_service_${logSuffix}` : `aztec:pxe_service`);
this.synchronizer = new Synchronizer(node, db, logSuffix);
this.contractDataOracle = new ContractDataOracle(db, node);
this.simulator = getAcirSimulator(db, node, keyStore, this.contractDataOracle);
this.sandboxVersion = getPackageInfo().version;
}
/**
* Starts the PXE Service by beginning the synchronisation process between the Aztec node and the database.
*
* @returns A promise that resolves when the server has started successfully.
*/
public async start() {
const { l2BlockPollingIntervalMS, l2StartingBlock } = this.config;
await this.synchronizer.start(l2StartingBlock, 1, l2BlockPollingIntervalMS);
const info = await this.getNodeInfo();
this.log.info(`Started PXE connected to chain ${info.chainId} version ${info.protocolVersion}`);
}
/**
* Stops the PXE Service, halting processing of new transactions and shutting down the synchronizer.
* This function ensures that all ongoing tasks are completed before stopping the server.
* It is useful for gracefully shutting down the server during maintenance or restarts.
*
* @returns A Promise resolving once the server has been stopped successfully.
*/
public async stop() {
await this.synchronizer.stop();
this.log.info('Stopped');
}
/** Returns an estimate of the db size in bytes. */
public estimateDbSize() {
return this.db.estimateSize();
}
public addAuthWitness(witness: AuthWitness) {
return this.db.addAuthWitness(witness.requestHash, witness.witness);
}
public async registerAccount(privKey: GrumpkinPrivateKey, partialAddress: PartialAddress): Promise<CompleteAddress> {
const completeAddress = await CompleteAddress.fromPrivateKeyAndPartialAddress(privKey, partialAddress);
const wasAdded = await this.db.addCompleteAddress(completeAddress);
if (wasAdded) {
const pubKey = this.keyStore.addAccount(privKey);
this.synchronizer.addAccount(pubKey, this.keyStore, this.config.l2StartingBlock);
this.log.info(`Registered account ${completeAddress.address.toString()}`);
this.log.debug(`Registered account\n ${completeAddress.toReadableString()}`);
} else {
this.log.info(`Account:\n "${completeAddress.address.toString()}"\n already registered.`);
}
return completeAddress;
}
public async getRegisteredAccounts(): Promise<CompleteAddress[]> {
// Get complete addresses of both the recipients and the accounts
const addresses = await this.db.getCompleteAddresses();
// Filter out the addresses not corresponding to accounts
const accountPubKeys = await this.keyStore.getAccounts();
const accounts = addresses.filter(address => accountPubKeys.find(pubKey => pubKey.equals(address.publicKey)));
return accounts;
}
public async getRegisteredAccount(address: AztecAddress): Promise<CompleteAddress | undefined> {
const result = await this.getRegisteredAccounts();
const account = result.find(r => r.address.equals(address));
return Promise.resolve(account);
}
public async registerRecipient(recipient: CompleteAddress): Promise<void> {
const wasAdded = await this.db.addCompleteAddress(recipient);
if (wasAdded) {
this.log.info(`Added recipient:\n ${recipient.toReadableString()}`);
} else {
this.log.info(`Recipient:\n "${recipient.toReadableString()}"\n already registered.`);
}
}
public async getRecipients(): Promise<CompleteAddress[]> {
// Get complete addresses of both the recipients and the accounts
const addresses = await this.db.getCompleteAddresses();
// Filter out the addresses corresponding to accounts
const accountPubKeys = await this.keyStore.getAccounts();
const recipients = addresses.filter(address => !accountPubKeys.find(pubKey => pubKey.equals(address.publicKey)));
return recipients;
}
public async getRecipient(address: AztecAddress): Promise<CompleteAddress | undefined> {
const result = await this.getRecipients();
const recipient = result.find(r => r.address.equals(address));
return Promise.resolve(recipient);
}
public async addContracts(contracts: DeployedContract[]) {
const contractDaos = contracts.map(c => toContractDao(c.abi, c.completeAddress, c.portalContract));
await Promise.all(contractDaos.map(c => this.db.addContract(c)));
for (const contract of contractDaos) {
const portalInfo =
contract.portalContract && !contract.portalContract.isZero() ? ` with portal ${contract.portalContract}` : '';
this.log.info(`Added contract ${contract.name} at ${contract.completeAddress.address}${portalInfo}`);
}
}
public async getContracts(): Promise<AztecAddress[]> {
return (await this.db.getContracts()).map(c => c.completeAddress.address);
}
public async getPublicStorageAt(contract: AztecAddress, storageSlot: Fr) {
if ((await this.getContractData(contract)) === undefined) {
throw new Error(`Contract ${contract.toString()} is not deployed`);
}
return await this.node.getPublicStorageAt(contract, storageSlot.value);
}
public async getPrivateStorageAt(owner: AztecAddress, contract: AztecAddress, storageSlot: Fr) {
if ((await this.getContractData(contract)) === undefined) {
throw new Error(`Contract ${contract.toString()} is not deployed`);
}
const notes = await this.db.getNoteSpendingInfo(contract, storageSlot);
const ownerCompleteAddress = await this.db.getCompleteAddress(owner);
if (!ownerCompleteAddress) throw new Error(`Owner ${owner} not registered in PXE`);
const { publicKey: ownerPublicKey } = ownerCompleteAddress;
const ownerNotes = notes.filter(n => n.publicKey.equals(ownerPublicKey));
return ownerNotes.map(n => n.notePreimage);
}
public async addNote(
account: AztecAddress,
contractAddress: AztecAddress,
storageSlot: Fr,
preimage: NotePreimage,
txHash: TxHash,
nonce?: Fr,
) {
const { publicKey } = (await this.db.getCompleteAddress(account)) ?? {};
if (!publicKey) {
throw new Error('Unknown account.');
}
if (!nonce) {
[nonce] = await this.getNoteNonces(contractAddress, storageSlot, preimage, txHash);
}
if (!nonce) {
throw new Error(`Cannot find the note in tx: ${txHash}.`);
}
const { innerNoteHash, siloedNoteHash, uniqueSiloedNoteHash, innerNullifier } =
await this.simulator.computeNoteHashAndNullifier(contractAddress, nonce, storageSlot, preimage.items);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386)
// This can always be `uniqueSiloedNoteHash` once notes added from public also include nonces.
const noteHashToLookUp = nonce.isZero() ? siloedNoteHash : uniqueSiloedNoteHash;
const index = await this.node.findLeafIndex(MerkleTreeId.PRIVATE_DATA_TREE, noteHashToLookUp.toBuffer());
if (index === undefined) {
throw new Error('Note does not exist.');
}
const wasm = await CircuitsWasm.get();
const siloedNullifier = siloNullifier(wasm, contractAddress, innerNullifier!);
const nullifierIndex = await this.node.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, siloedNullifier.toBuffer());
if (nullifierIndex !== undefined) {
throw new Error('The note has been destroyed.');
}
await this.db.addNoteSpendingInfo({
contractAddress,
storageSlot,
notePreimage: preimage,
nonce,
innerNoteHash,
siloedNullifier,
index,
publicKey,
});
}
public async getNoteNonces(
contractAddress: AztecAddress,
storageSlot: Fr,
preimage: NotePreimage,
txHash: TxHash,
): Promise<Fr[]> {
const tx = await this.node.getTx(txHash);
if (!tx) {
throw new Error(`Unknown tx: ${txHash}`);
}
const wasm = await CircuitsWasm.get();
const nonces: Fr[] = [];
const firstNullifier = tx.newNullifiers[0];
const commitments = tx.newCommitments;
for (let i = 0; i < commitments.length; ++i) {
const commitment = commitments[i];
if (commitment.equals(Fr.ZERO)) break;
const nonce = computeCommitmentNonce(wasm, firstNullifier, i);
const { siloedNoteHash, uniqueSiloedNoteHash } = await this.simulator.computeNoteHashAndNullifier(
contractAddress,
nonce,
storageSlot,
preimage.items,
);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386)
// Remove this once notes added from public also include nonces.
if (commitment.equals(siloedNoteHash)) {
nonces.push(Fr.ZERO);
break;
}
if (commitment.equals(uniqueSiloedNoteHash)) {
nonces.push(nonce);
}
}
return nonces;
}
public async getBlock(blockNumber: number): Promise<L2Block | undefined> {
// If a negative block number is provided the current block number is fetched.
if (blockNumber < 0) {
blockNumber = await this.node.getBlockNumber();
}
return await this.node.getBlock(blockNumber);
}
public async simulateTx(txRequest: TxExecutionRequest, simulatePublic: boolean) {
if (!txRequest.functionData.isPrivate) {
throw new Error(`Public entrypoints are not allowed`);
}
if (txRequest.functionData.isInternal === undefined) {
throw new Error(`Unspecified internal are not allowed`);
}
// We get the contract address from origin, since contract deployments are signalled as origin from their own address
// TODO: Is this ok? Should it be changed to be from ZERO?
const deployedContractAddress = txRequest.txContext.isContractDeploymentTx ? txRequest.origin : undefined;
const newContract = deployedContractAddress ? await this.db.getContract(deployedContractAddress) : undefined;
const tx = await this.#simulateAndProve(txRequest, newContract);
if (simulatePublic) await this.#simulatePublicCalls(tx);
this.log.info(`Executed local simulation for ${await tx.getTxHash()}`);
return tx;
}
public async sendTx(tx: Tx): Promise<TxHash> {
const txHash = await tx.getTxHash();
if (await this.node.getTx(txHash)) {
throw new Error(`A settled tx with equal hash ${txHash.toString()} exists.`);
}
this.log.info(`Sending transaction ${txHash}`);
await this.node.sendTx(tx);
return txHash;
}
public async viewTx(functionName: string, args: any[], to: AztecAddress, _from?: AztecAddress) {
// TODO - Should check if `from` has the permission to call the view function.
const functionCall = await this.#getFunctionCall(functionName, args, to);
const executionResult = await this.#simulateUnconstrained(functionCall);
// TODO - Return typed result based on the function abi.
return executionResult;
}
public async getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
const settledTx = await this.node.getTx(txHash);
if (settledTx) {
const deployedContractAddress = settledTx.newContractData.find(
c => !c.contractAddress.equals(AztecAddress.ZERO),
)?.contractAddress;
return new TxReceipt(
txHash,
TxStatus.MINED,
'',
settledTx.blockHash,
settledTx.blockNumber,
deployedContractAddress,
);
}
const pendingTx = await this.node.getPendingTxByHash(txHash);
if (pendingTx) {
return new TxReceipt(txHash, TxStatus.PENDING, '');
}
return new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.');
}
public async getTx(txHash: TxHash): Promise<L2Tx | undefined> {
return await this.node.getTx(txHash);
}
async getBlockNumber(): Promise<number> {
return await this.node.getBlockNumber();
}
public async getExtendedContractData(contractAddress: AztecAddress): Promise<ExtendedContractData | undefined> {
return await this.node.getExtendedContractData(contractAddress);
}
public async getContractData(contractAddress: AztecAddress): Promise<ContractData | undefined> {
return await this.node.getContractData(contractAddress);
}
public async getUnencryptedLogs(from: number, limit: number): Promise<L2BlockL2Logs[]> {
return await this.node.getLogs(from, limit, LogType.UNENCRYPTED);
}
async #getFunctionCall(functionName: string, args: any[], to: AztecAddress): Promise<FunctionCall> {
const contract = await this.db.getContract(to);
if (!contract) {
throw new Error(`Unknown contract ${to}: add it to PXE Service by calling server.addContracts(...)`);
}
const functionDao = contract.functions.find(f => f.name === functionName);
if (!functionDao) {
throw new Error(`Unknown function ${functionName} in contract ${contract.name}.`);
}
return {
args: encodeArguments(functionDao, args),
functionData: FunctionData.fromAbi(functionDao),
to,
};
}
public async getNodeInfo(): Promise<NodeInfo> {
const [version, chainId, contractAddresses] = await Promise.all([
this.node.getVersion(),
this.node.getChainId(),
this.node.getL1ContractAddresses(),
]);
const nodeInfo: NodeInfo = {
sandboxVersion: this.sandboxVersion,
compatibleNargoVersion: NoirVersion.tag,
chainId,
protocolVersion: version,
l1ContractAddresses: contractAddresses,
};
return nodeInfo;
}
/**
* Retrieves the simulation parameters required to run an ACIR simulation.
* This includes the contract address, function ABI, portal contract address, and historic tree roots.
*
* @param execRequest - The transaction request object containing details of the contract call.
* @returns An object containing the contract address, function ABI, portal contract address, and historic tree roots.
*/
async #getSimulationParameters(execRequest: FunctionCall | TxExecutionRequest) {
const contractAddress = (execRequest as FunctionCall).to ?? (execRequest as TxExecutionRequest).origin;
const functionAbi = await this.contractDataOracle.getFunctionAbi(
contractAddress,
execRequest.functionData.selector,
);
const debug = await this.contractDataOracle.getFunctionDebugMetadata(
contractAddress,
execRequest.functionData.selector,
);
const portalContract = await this.contractDataOracle.getPortalContractAddress(contractAddress);
return {
contractAddress,
functionAbi: {
...functionAbi,
debug,
},
portalContract,
};
}
async #simulate(txRequest: TxExecutionRequest): Promise<ExecutionResult> {
// TODO - Pause syncing while simulating.
const { contractAddress, functionAbi, portalContract } = await this.#getSimulationParameters(txRequest);
this.log('Executing simulator...');
try {
const result = await this.simulator.run(txRequest, functionAbi, contractAddress, portalContract);
this.log('Simulation completed!');
return result;
} catch (err) {
if (err instanceof SimulationError) {
await this.#enrichSimulationError(err);
}
throw err;
}
}
/**
* Simulate an unconstrained transaction on the given contract, without considering constraints set by ACIR.
* The simulation parameters are fetched using ContractDataOracle and executed using AcirSimulator.
* Returns the simulation result containing the outputs of the unconstrained function.
*
* @param execRequest - The transaction request object containing the target contract and function data.
* @returns The simulation result containing the outputs of the unconstrained function.
*/
async #simulateUnconstrained(execRequest: FunctionCall) {
const { contractAddress, functionAbi } = await this.#getSimulationParameters(execRequest);
this.log('Executing unconstrained simulator...');
try {
const result = await this.simulator.runUnconstrained(execRequest, functionAbi, contractAddress, this.node);
this.log('Unconstrained simulation completed!');
return result;
} catch (err) {
if (err instanceof SimulationError) {
await this.#enrichSimulationError(err);
}
throw err;
}
}
/**
* Simulate the public part of a transaction.
* This allows to catch public execution errors before submitting the transaction.
* It can also be used for estimating gas in the future.
* @param tx - The transaction to be simulated.
*/
async #simulatePublicCalls(tx: Tx) {
try {
await this.node.simulatePublicCalls(tx);
} catch (err) {
// Try to fill in the noir call stack since the PXE may have access to the debug metadata
if (err instanceof SimulationError) {
const callStack = err.getCallStack();
const originalFailingFunction = callStack[callStack.length - 1];
const debugInfo = await this.contractDataOracle.getFunctionDebugMetadata(
originalFailingFunction.contractAddress,
originalFailingFunction.functionSelector,
);
const noirCallStack = err.getNoirCallStack();
if (debugInfo && isNoirCallStackUnresolved(noirCallStack)) {
const parsedCallStack = resolveOpcodeLocations(noirCallStack, debugInfo);
err.setNoirCallStack(parsedCallStack);
}
await this.#enrichSimulationError(err);
}
throw err;
}
}
/**
* Simulate a transaction, generate a kernel proof, and create a private transaction object.
* The function takes in a transaction request and an ECDSA signature. It simulates the transaction,
* then generates a kernel proof using the simulation result. Finally, it creates a private
* transaction object with the generated proof and public inputs. If a new contract address is provided,
* the function will also include the new contract's public functions in the transaction object.
*
* @param txExecutionRequest - The transaction request to be simulated and proved.
* @param signature - The ECDSA signature for the transaction request.
* @param newContract - Optional. The address of a new contract to be included in the transaction object.
* @returns A private transaction object containing the proof, public inputs, and encrypted logs.
*/
async #simulateAndProve(txExecutionRequest: TxExecutionRequest, newContract: ContractDao | undefined) {
// TODO - Pause syncing while simulating.
// Get values that allow us to reconstruct the block hash
const executionResult = await this.#simulate(txExecutionRequest);
const kernelOracle = new KernelOracle(this.contractDataOracle, this.node);
const kernelProver = new KernelProver(kernelOracle);
this.log(`Executing kernel prover...`);
const { proof, publicInputs } = await kernelProver.prove(txExecutionRequest.toTxRequest(), executionResult);
const newContractPublicFunctions = newContract ? getNewContractPublicFunctions(newContract) : [];
const encryptedLogs = new TxL2Logs(collectEncryptedLogs(executionResult));
const unencryptedLogs = new TxL2Logs(collectUnencryptedLogs(executionResult));
const enqueuedPublicFunctions = collectEnqueuedPublicFunctionCalls(executionResult);
const contractData = new ContractData(
newContract?.completeAddress.address ?? AztecAddress.ZERO,
newContract?.portalContract ?? EthAddress.ZERO,
);
const extendedContractData = new ExtendedContractData(
contractData,
newContractPublicFunctions,
newContract?.completeAddress.partialAddress ?? Fr.ZERO,
newContract?.completeAddress.publicKey ?? Point.ZERO,
);
// HACK(#1639): Manually patches the ordering of the public call stack
// TODO(#757): Enforce proper ordering of enqueued public calls
await this.patchPublicCallStackOrdering(publicInputs, enqueuedPublicFunctions);
return new Tx(publicInputs, proof, encryptedLogs, unencryptedLogs, enqueuedPublicFunctions, [extendedContractData]);
}
/**
* Adds contract and function names to a simulation error.
* @param err - The error to enrich.
*/
async #enrichSimulationError(err: SimulationError) {
// Maps contract addresses to the set of functions selectors that were in error.
// Using strings because map and set don't use .equals()
const mentionedFunctions: Map<string, Set<string>> = new Map();
err.getCallStack().forEach(({ contractAddress, functionSelector }) => {
if (!mentionedFunctions.has(contractAddress.toString())) {
mentionedFunctions.set(contractAddress.toString(), new Set());
}
mentionedFunctions.get(contractAddress.toString())!.add(functionSelector.toString());
});
await Promise.all(
[...mentionedFunctions.entries()].map(async ([contractAddress, selectors]) => {
const parsedContractAddress = AztecAddress.fromString(contractAddress);
const contract = await this.db.getContract(parsedContractAddress);
if (contract) {
err.enrichWithContractName(parsedContractAddress, contract.name);
selectors.forEach(selector => {
const functionAbi = contract.functions.find(f => f.selector.toString() === selector);
if (functionAbi) {
err.enrichWithFunctionName(parsedContractAddress, functionAbi.selector, functionAbi.name);
}
});
}
}),
);
}
// HACK(#1639): this is a hack to fix ordering of public calls enqueued in the call stack. Since the private kernel
// cannot keep track of side effects that happen after or before a nested call, we override the public call stack
// it emits with whatever we got from the simulator collected enqueued calls. As a sanity check, we at least verify
// that the elements are the same, so we are only tweaking their ordering.
// See yarn-project/end-to-end/src/e2e_ordering.test.ts
// See https://github.com/AztecProtocol/aztec-packages/issues/1615
// TODO(#757): Enforce proper ordering of enqueued public calls
private async patchPublicCallStackOrdering(
publicInputs: KernelCircuitPublicInputsFinal,
enqueuedPublicCalls: PublicCallRequest[],
) {
const callToHash = (call: PublicCallRequest) => call.toPublicCallStackItem().then(item => item.hash());
const enqueuedPublicCallsHashes = await Promise.all(enqueuedPublicCalls.map(callToHash));
const { publicCallStack } = publicInputs.end;
// Validate all items in enqueued public calls are in the kernel emitted stack
const areEqual = enqueuedPublicCallsHashes.reduce(
(accum, enqueued) => accum && !!publicCallStack.find(item => item.equals(enqueued)),
true,
);
if (!areEqual) {
throw new Error(
`Enqueued public function calls and public call stack do not match.\nEnqueued calls: ${enqueuedPublicCallsHashes
.map(h => h.toString())
.join(', ')}\nPublic call stack: ${publicCallStack.map(i => i.toString()).join(', ')}`,
);
}
// Override kernel output
publicInputs.end.publicCallStack = padArrayEnd(
enqueuedPublicCallsHashes,
Fr.ZERO,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX,
);
}
public async isGlobalStateSynchronized() {
return await this.synchronizer.isGlobalStateSynchronized();
}
public async isAccountStateSynchronized(account: AztecAddress) {
return await this.synchronizer.isAccountStateSynchronized(account);
}
public getSyncStatus() {
return Promise.resolve(this.synchronizer.getSyncStatus());
}
public getKeyStore() {
return this.keyStore;
}
}