@aztec/pxe 0.0.1-commit.cf93bcc56 → 0.0.1-commit.d0fcfb7f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/config/package_info.js +1 -1
- package/dest/contract_function_simulator/contract_function_simulator.d.ts +1 -1
- package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
- package/dest/contract_function_simulator/contract_function_simulator.js +8 -8
- package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts +5 -5
- package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts.map +1 -1
- package/dest/contract_function_simulator/execution_tagging_index_cache.js +3 -3
- package/dest/contract_function_simulator/noir-structs/event_validation_request.js +1 -1
- package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +1 -1
- package/dest/contract_function_simulator/noir-structs/note_validation_request.js +1 -1
- package/dest/contract_function_simulator/oracle/interfaces.d.ts +7 -3
- package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +1 -1
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/private_execution_oracle.js +8 -8
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +9 -5
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +15 -11
- package/dest/contract_logging.d.ts +22 -0
- package/dest/contract_logging.d.ts.map +1 -0
- package/dest/contract_logging.js +23 -0
- package/dest/debug/pxe_debug_utils.d.ts +2 -2
- package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
- package/dest/debug/pxe_debug_utils.js +4 -4
- package/dest/entrypoints/client/bundle/index.d.ts +2 -1
- package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
- package/dest/entrypoints/client/bundle/index.js +1 -0
- package/dest/entrypoints/client/lazy/index.d.ts +2 -1
- package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
- package/dest/entrypoints/client/lazy/index.js +1 -0
- package/dest/logs/log_service.d.ts +1 -1
- package/dest/logs/log_service.d.ts.map +1 -1
- package/dest/logs/log_service.js +4 -4
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +4 -3
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -1
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.js +125 -64
- package/dest/private_kernel/hints/test_utils.d.ts +122 -0
- package/dest/private_kernel/hints/test_utils.d.ts.map +1 -0
- package/dest/private_kernel/hints/test_utils.js +203 -0
- package/dest/private_kernel/private_kernel_execution_prover.d.ts +1 -1
- package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
- package/dest/private_kernel/private_kernel_execution_prover.js +13 -5
- package/dest/private_kernel/private_kernel_oracle.d.ts +6 -2
- package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
- package/dest/private_kernel/private_kernel_oracle.js +7 -3
- package/dest/pxe.d.ts +6 -6
- package/dest/pxe.d.ts.map +1 -1
- package/dest/pxe.js +27 -23
- package/dest/storage/contract_store/contract_store.d.ts +42 -15
- package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
- package/dest/storage/contract_store/contract_store.js +140 -64
- package/dest/storage/tagging_store/recipient_tagging_store.d.ts +6 -6
- package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.d.ts +5 -5
- package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.js +4 -4
- package/dest/tagging/index.d.ts +2 -2
- package/dest/tagging/index.d.ts.map +1 -1
- package/dest/tagging/index.js +1 -1
- package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +4 -5
- package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
- package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +3 -3
- package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +6 -7
- package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
- package/dest/tagging/recipient_sync/utils/load_logs_for_range.js +12 -11
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +4 -8
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +3 -6
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +4 -7
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +14 -15
- package/package.json +16 -16
- package/src/config/package_info.ts +1 -1
- package/src/contract_function_simulator/contract_function_simulator.ts +15 -17
- package/src/contract_function_simulator/execution_tagging_index_cache.ts +5 -5
- package/src/contract_function_simulator/noir-structs/event_validation_request.ts +1 -1
- package/src/contract_function_simulator/noir-structs/note_validation_request.ts +1 -1
- package/src/contract_function_simulator/oracle/interfaces.ts +5 -2
- package/src/contract_function_simulator/oracle/private_execution_oracle.ts +12 -9
- package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +20 -13
- package/src/contract_logging.ts +39 -0
- package/src/debug/pxe_debug_utils.ts +4 -4
- package/src/entrypoints/client/bundle/index.ts +1 -0
- package/src/entrypoints/client/lazy/index.ts +1 -0
- package/src/logs/log_service.ts +10 -5
- package/src/private_kernel/hints/private_kernel_reset_private_inputs_builder.ts +157 -110
- package/src/private_kernel/hints/test_utils.ts +325 -0
- package/src/private_kernel/private_kernel_execution_prover.ts +13 -6
- package/src/private_kernel/private_kernel_oracle.ts +7 -7
- package/src/pxe.ts +33 -30
- package/src/storage/contract_store/contract_store.ts +170 -71
- package/src/storage/tagging_store/recipient_tagging_store.ts +9 -5
- package/src/storage/tagging_store/sender_tagging_store.ts +8 -8
- package/src/tagging/index.ts +1 -1
- package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +3 -6
- package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +10 -15
- package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +4 -9
- package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +11 -20
package/dest/pxe.js
CHANGED
|
@@ -13,6 +13,7 @@ import { BlockSynchronizer } from './block_synchronizer/index.js';
|
|
|
13
13
|
import { BenchmarkedNodeFactory } from './contract_function_simulator/benchmarked_node.js';
|
|
14
14
|
import { ContractFunctionSimulator, generateSimulatedProvingResult } from './contract_function_simulator/contract_function_simulator.js';
|
|
15
15
|
import { ProxiedContractStoreFactory } from './contract_function_simulator/proxied_contract_data_source.js';
|
|
16
|
+
import { displayDebugLogs } from './contract_logging.js';
|
|
16
17
|
import { ContractSyncService } from './contract_sync/contract_sync_service.js';
|
|
17
18
|
import { readCurrentClassId } from './contract_sync/helpers.js';
|
|
18
19
|
import { PXEDebugUtils } from './debug/pxe_debug_utils.js';
|
|
@@ -115,7 +116,7 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
115
116
|
const debugUtils = new PXEDebugUtils(contractSyncService, noteStore, synchronizer, anchorBlockStore);
|
|
116
117
|
const jobQueue = new SerialQueue();
|
|
117
118
|
const pxe = new PXE(node, synchronizer, keyStore, contractStore, noteStore, capsuleStore, anchorBlockStore, senderTaggingStore, senderAddressBookStore, recipientTaggingStore, addressStore, privateEventStore, contractSyncService, simulator, proverEnabled, proofCreator, protocolContractsProvider, log, jobQueue, jobCoordinator, debugUtils);
|
|
118
|
-
debugUtils.setPXEHelpers(pxe.#putInJobQueue.bind(pxe), pxe.#getSimulatorForTx.bind(pxe), pxe.#
|
|
119
|
+
debugUtils.setPXEHelpers(pxe.#putInJobQueue.bind(pxe), pxe.#getSimulatorForTx.bind(pxe), pxe.#executeUtility.bind(pxe));
|
|
119
120
|
pxe.jobQueue.start();
|
|
120
121
|
await pxe.#registerProtocolContracts();
|
|
121
122
|
log.info(`Started PXE connected to chain ${info.l1ChainId} version ${info.rollupVersion}`);
|
|
@@ -180,8 +181,8 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
180
181
|
async #registerProtocolContracts() {
|
|
181
182
|
const registered = {};
|
|
182
183
|
for (const name of protocolContractNames){
|
|
183
|
-
const { address,
|
|
184
|
-
await this.contractStore.addContractArtifact(
|
|
184
|
+
const { address, instance, artifact } = await this.protocolContractsProvider.getProtocolContractArtifact(name);
|
|
185
|
+
await this.contractStore.addContractArtifact(artifact);
|
|
185
186
|
await this.contractStore.addContractInstance(instance);
|
|
186
187
|
registered[name] = address.toString();
|
|
187
188
|
}
|
|
@@ -193,7 +194,7 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
193
194
|
const { origin: contractAddress, functionSelector } = txRequest;
|
|
194
195
|
try {
|
|
195
196
|
const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
|
|
196
|
-
await this.contractSyncService.ensureContractSynced(contractAddress, functionSelector, (privateSyncCall, execScopes)=>this.#
|
|
197
|
+
await this.contractSyncService.ensureContractSynced(contractAddress, functionSelector, (privateSyncCall, execScopes)=>this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId), anchorBlockHeader, jobId, scopes);
|
|
197
198
|
const result = await contractFunctionSimulator.run(txRequest, {
|
|
198
199
|
contractAddress,
|
|
199
200
|
selector: functionSelector,
|
|
@@ -211,15 +212,15 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
211
212
|
}
|
|
212
213
|
}
|
|
213
214
|
/**
|
|
214
|
-
*
|
|
215
|
+
* Execute a utility function call on the given contract.
|
|
215
216
|
* @param contractFunctionSimulator - The simulator to use for the function call.
|
|
216
217
|
* @param call - The function call to execute.
|
|
217
218
|
* @param authWitnesses - Authentication witnesses required for the function call.
|
|
218
219
|
* @param scopes - Optional array of account addresses whose notes can be accessed in this call. Defaults to all
|
|
219
220
|
* accounts if not specified.
|
|
220
221
|
* @param jobId - The job ID for staged writes.
|
|
221
|
-
* @returns The
|
|
222
|
-
*/ async #
|
|
222
|
+
* @returns The execution result containing the outputs of the utility function.
|
|
223
|
+
*/ async #executeUtility(contractFunctionSimulator, call, authWitnesses, scopes, jobId) {
|
|
223
224
|
try {
|
|
224
225
|
const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
|
|
225
226
|
return contractFunctionSimulator.runUtility(call, authWitnesses ?? [], anchorBlockHeader, scopes, jobId);
|
|
@@ -371,8 +372,7 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
371
372
|
*
|
|
372
373
|
* @param artifact - The build artifact for the contract class.
|
|
373
374
|
*/ async registerContractClass(artifact) {
|
|
374
|
-
const
|
|
375
|
-
await this.contractStore.addContractArtifact(contractClassId, artifact);
|
|
375
|
+
const contractClassId = await this.contractStore.addContractArtifact(artifact);
|
|
376
376
|
this.log.info(`Added contract class ${artifact.name} with id ${contractClassId}`);
|
|
377
377
|
}
|
|
378
378
|
/**
|
|
@@ -388,15 +388,14 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
388
388
|
if (artifact) {
|
|
389
389
|
// If the user provides an artifact, validate it against the expected class id and register it
|
|
390
390
|
const contractClass = await getContractClassFromArtifact(artifact);
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
throw new Error(`Artifact does not match expected class id (computed ${contractClassId} but instance refers to ${instance.currentContractClassId})`);
|
|
391
|
+
if (!contractClass.id.equals(instance.currentContractClassId)) {
|
|
392
|
+
throw new Error(`Artifact does not match expected class id (computed ${contractClass.id} but instance refers to ${instance.currentContractClassId})`);
|
|
394
393
|
}
|
|
395
394
|
const computedAddress = await computeContractAddressFromInstance(instance);
|
|
396
395
|
if (!computedAddress.equals(instance.address)) {
|
|
397
396
|
throw new Error('Added a contract in which the address does not match the contract instance.');
|
|
398
397
|
}
|
|
399
|
-
await this.contractStore.addContractArtifact(
|
|
398
|
+
await this.contractStore.addContractArtifact(artifact, contractClass);
|
|
400
399
|
const publicFunctionSignatures = artifact.functions.filter((fn)=>fn.functionType === FunctionType.PUBLIC).map((fn)=>decodeFunctionSignature(fn.name, fn.parameters));
|
|
401
400
|
await this.node.registerContractFunctionSignatures(publicFunctionSignatures);
|
|
402
401
|
} else {
|
|
@@ -432,11 +431,13 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
432
431
|
if (!contractClass.id.equals(currentClassId)) {
|
|
433
432
|
throw new Error('Could not update contract to a class different from the current one.');
|
|
434
433
|
}
|
|
435
|
-
await this.contractStore.addContractArtifact(contractClass.id, artifact);
|
|
436
434
|
const publicFunctionSignatures = artifact.functions.filter((fn)=>fn.functionType === FunctionType.PUBLIC).map((fn)=>decodeFunctionSignature(fn.name, fn.parameters));
|
|
437
435
|
await this.node.registerContractFunctionSignatures(publicFunctionSignatures);
|
|
438
436
|
currentInstance.currentContractClassId = contractClass.id;
|
|
439
|
-
await
|
|
437
|
+
await Promise.all([
|
|
438
|
+
this.contractStore.addContractArtifact(artifact, contractClass),
|
|
439
|
+
this.contractStore.addContractInstance(currentInstance)
|
|
440
|
+
]);
|
|
440
441
|
this.log.info(`Updated contract ${artifact.name} at ${contractAddress.toString()} to class ${contractClass.id}`);
|
|
441
442
|
});
|
|
442
443
|
}
|
|
@@ -640,6 +641,9 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
640
641
|
const publicSimulationTimer = new Timer();
|
|
641
642
|
publicOutput = await this.#simulatePublicCalls(simulatedTx, skipFeeEnforcement);
|
|
642
643
|
publicSimulationTime = publicSimulationTimer.ms();
|
|
644
|
+
if (publicOutput?.debugLogs?.length) {
|
|
645
|
+
await displayDebugLogs(publicOutput.debugLogs, (addr)=>this.contractStore.getDebugContractName(addr));
|
|
646
|
+
}
|
|
643
647
|
}
|
|
644
648
|
let validationTime;
|
|
645
649
|
if (!skipTxValidation) {
|
|
@@ -688,14 +692,14 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
688
692
|
});
|
|
689
693
|
}
|
|
690
694
|
/**
|
|
691
|
-
*
|
|
695
|
+
* Executes a contract utility function.
|
|
692
696
|
* @param call - The function call containing the function details, arguments, and target contract address.
|
|
693
|
-
*/
|
|
697
|
+
*/ executeUtility(call, { authwits, scopes } = {
|
|
694
698
|
scopes: 'ALL_SCOPES'
|
|
695
699
|
}) {
|
|
696
|
-
// We disable concurrent
|
|
700
|
+
// We disable concurrent executions since those might execute oracles which read and write to the PXE stores (e.g.
|
|
697
701
|
// to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
|
|
698
|
-
// delete the same read value, or reading values that another
|
|
702
|
+
// delete the same read value, or reading values that another execution is currently modifying).
|
|
699
703
|
return this.#putInJobQueue(async (jobId)=>{
|
|
700
704
|
try {
|
|
701
705
|
const totalTimer = new Timer();
|
|
@@ -705,8 +709,8 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
705
709
|
const functionTimer = new Timer();
|
|
706
710
|
const contractFunctionSimulator = this.#getSimulatorForTx();
|
|
707
711
|
const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
|
|
708
|
-
await this.contractSyncService.ensureContractSynced(call.to, call.selector, (privateSyncCall, execScopes)=>this.#
|
|
709
|
-
const executionResult = await this.#
|
|
712
|
+
await this.contractSyncService.ensureContractSynced(call.to, call.selector, (privateSyncCall, execScopes)=>this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId), anchorBlockHeader, jobId, scopes);
|
|
713
|
+
const executionResult = await this.#executeUtility(contractFunctionSimulator, call, authwits ?? [], scopes, jobId);
|
|
710
714
|
const functionTime = functionTimer.ms();
|
|
711
715
|
const totalTime = totalTimer.ms();
|
|
712
716
|
const perFunction = [
|
|
@@ -732,7 +736,7 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
732
736
|
} catch (err) {
|
|
733
737
|
const { to, name, args } = call;
|
|
734
738
|
const stringifiedArgs = args.map((arg)=>arg.toString()).join(', ');
|
|
735
|
-
throw this.#contextualizeError(err, `
|
|
739
|
+
throw this.#contextualizeError(err, `executeUtility ${to}:${name}(${stringifiedArgs})`, `scopes=${scopes === 'ALL_SCOPES' ? scopes : scopes.map((s)=>s.toString()).join(', ')}`);
|
|
736
740
|
}
|
|
737
741
|
});
|
|
738
742
|
}
|
|
@@ -755,7 +759,7 @@ import { SenderTaggingStore } from './storage/tagging_store/sender_tagging_store
|
|
|
755
759
|
const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
|
|
756
760
|
anchorBlockNumber = anchorBlockHeader.getBlockNumber();
|
|
757
761
|
const contractFunctionSimulator = this.#getSimulatorForTx();
|
|
758
|
-
await this.contractSyncService.ensureContractSynced(filter.contractAddress, null, async (privateSyncCall, execScopes)=>await this.#
|
|
762
|
+
await this.contractSyncService.ensureContractSynced(filter.contractAddress, null, async (privateSyncCall, execScopes)=>await this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId), anchorBlockHeader, jobId, filter.scopes);
|
|
759
763
|
});
|
|
760
764
|
// anchorBlockNumber is set during the job and fixed to whatever it is after a block sync
|
|
761
765
|
const sanitizedFilter = new PrivateEventFilterValidator(anchorBlockNumber).validate(filter);
|
|
@@ -1,10 +1,37 @@
|
|
|
1
1
|
import type { FUNCTION_TREE_HEIGHT } from '@aztec/constants';
|
|
2
|
-
import
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
+
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
4
|
import type { MembershipWitness } from '@aztec/foundation/trees';
|
|
4
5
|
import type { AztecAsyncKVStore } from '@aztec/kv-store';
|
|
5
6
|
import { type ContractArtifact, type FunctionAbi, type FunctionArtifactWithContractName, FunctionCall, type FunctionDebugMetadata, FunctionSelector } from '@aztec/stdlib/abi';
|
|
6
7
|
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
7
|
-
import { type
|
|
8
|
+
import { type ContractClassIdPreimage, type ContractClassWithId, type ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
9
|
+
/**
|
|
10
|
+
* All contract class data except the large packedBytecode.
|
|
11
|
+
* The expensive data from the ContractClass is precomputed and stored in this format to avoid redundant hashing.
|
|
12
|
+
* Since we have to store the artifacts anyway, the final ContractClass is reconstructed by combining this data
|
|
13
|
+
* with the packedBytecode obtained from the former. That way we can have quick class lookups without wasted storage.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SerializableContractClassData {
|
|
16
|
+
readonly version: 1;
|
|
17
|
+
readonly id: Fr;
|
|
18
|
+
readonly artifactHash: Fr;
|
|
19
|
+
readonly privateFunctionsRoot: Fr;
|
|
20
|
+
readonly publicBytecodeCommitment: Fr;
|
|
21
|
+
readonly privateFunctions: {
|
|
22
|
+
selector: FunctionSelector;
|
|
23
|
+
vkHash: Fr;
|
|
24
|
+
}[];
|
|
25
|
+
constructor(data: ContractClassIdPreimage & {
|
|
26
|
+
id: Fr;
|
|
27
|
+
privateFunctions: {
|
|
28
|
+
selector: FunctionSelector;
|
|
29
|
+
vkHash: Fr;
|
|
30
|
+
}[];
|
|
31
|
+
});
|
|
32
|
+
toBuffer(): Buffer;
|
|
33
|
+
static fromBuffer(bufferOrReader: Buffer | BufferReader): SerializableContractClassData;
|
|
34
|
+
}
|
|
8
35
|
/**
|
|
9
36
|
* ContractStore serves as a data manager and retriever for Aztec.nr contracts.
|
|
10
37
|
* It provides methods to obtain contract addresses, function ABI, bytecode, and membership witnesses
|
|
@@ -15,19 +42,24 @@ import { type ContractClass, type ContractInstanceWithAddress } from '@aztec/std
|
|
|
15
42
|
export declare class ContractStore {
|
|
16
43
|
#private;
|
|
17
44
|
constructor(store: AztecAsyncKVStore);
|
|
18
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Registers a new contract artifact and its corresponding class data.
|
|
47
|
+
* IMPORTANT: This method does not verify that the provided artifact matches the class data or that the class id matches the artifact.
|
|
48
|
+
* It is the caller's responsibility to ensure the consistency and correctness of the provided data.
|
|
49
|
+
* This is done to avoid redundant, expensive contract class computations
|
|
50
|
+
*/
|
|
51
|
+
addContractArtifact(contract: ContractArtifact, contractClassWithIdAndPreimage?: ContractClassWithId & ContractClassIdPreimage): Promise<Fr>;
|
|
19
52
|
addContractInstance(contract: ContractInstanceWithAddress): Promise<void>;
|
|
20
53
|
getContractsAddresses(): Promise<AztecAddress[]>;
|
|
21
|
-
/** Returns a contract instance for a given address.
|
|
54
|
+
/** Returns a contract instance for a given address. */
|
|
22
55
|
getContractInstance(contractAddress: AztecAddress): Promise<ContractInstanceWithAddress | undefined>;
|
|
56
|
+
/** Returns the raw contract artifact for a given class id. */
|
|
23
57
|
getContractArtifact(contractClassId: Fr): Promise<ContractArtifact | undefined>;
|
|
24
|
-
/** Returns a contract class for a given class id.
|
|
25
|
-
|
|
58
|
+
/** Returns a contract class for a given class id. */
|
|
59
|
+
getContractClassWithPreimage(contractClassId: Fr): Promise<(ContractClassWithId & ContractClassIdPreimage) | undefined>;
|
|
26
60
|
getContract(address: AztecAddress): Promise<(ContractInstanceWithAddress & ContractArtifact) | undefined>;
|
|
27
61
|
/**
|
|
28
62
|
* Retrieves the artifact of a specified function within a given contract.
|
|
29
|
-
* The function is identified by its selector, which is a unique code generated from the function's signature.
|
|
30
|
-
* Throws an error if the contract address or function selector are invalid or not found.
|
|
31
63
|
*
|
|
32
64
|
* @param contractAddress - The AztecAddress representing the contract containing the function.
|
|
33
65
|
* @param selector - The function selector.
|
|
@@ -39,20 +71,15 @@ export declare class ContractStore {
|
|
|
39
71
|
getFunctionAbi(contractAddress: AztecAddress, selector: FunctionSelector): Promise<FunctionAbi | undefined>;
|
|
40
72
|
/**
|
|
41
73
|
* Retrieves the debug metadata of a specified function within a given contract.
|
|
42
|
-
* The function is identified by its selector, which is a unique code generated from the function's signature.
|
|
43
|
-
* Returns undefined if the debug metadata for the given function is not found.
|
|
44
|
-
* Throws if the contract has not been added to the database.
|
|
45
74
|
*
|
|
46
75
|
* @param contractAddress - The AztecAddress representing the contract containing the function.
|
|
47
76
|
* @param selector - The function selector.
|
|
48
|
-
* @returns The corresponding function's
|
|
77
|
+
* @returns The corresponding function's debug metadata, or undefined.
|
|
49
78
|
*/
|
|
50
79
|
getFunctionDebugMetadata(contractAddress: AztecAddress, selector: FunctionSelector): Promise<FunctionDebugMetadata | undefined>;
|
|
51
80
|
getPublicFunctionDebugMetadata(contractAddress: AztecAddress): Promise<FunctionDebugMetadata | undefined>;
|
|
52
81
|
/**
|
|
53
82
|
* Retrieve the function membership witness for the given contract class and function selector.
|
|
54
|
-
* The function membership witness represents a proof that the function belongs to the specified contract.
|
|
55
|
-
* Throws an error if the contract address or function selector is unknown.
|
|
56
83
|
*
|
|
57
84
|
* @param contractClassId - The id of the class.
|
|
58
85
|
* @param selector - The function selector.
|
|
@@ -63,4 +90,4 @@ export declare class ContractStore {
|
|
|
63
90
|
getDebugFunctionName(contractAddress: AztecAddress, selector: FunctionSelector): Promise<string>;
|
|
64
91
|
getFunctionCall(functionName: string, args: any[], to: AztecAddress): Promise<FunctionCall>;
|
|
65
92
|
}
|
|
66
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
93
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udHJhY3Rfc3RvcmUuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9zdG9yYWdlL2NvbnRyYWN0X3N0b3JlL2NvbnRyYWN0X3N0b3JlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxFQUFFLG9CQUFvQixFQUFFLE1BQU0sa0JBQWtCLENBQUM7QUFDN0QsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLGdDQUFnQyxDQUFDO0FBRXBELE9BQU8sRUFBRSxZQUFZLEVBQ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RSxZQUFZLCtCQUc5RDtJQUVZLG9CQUFvQixDQUFDLGVBQWUsRUFBRSxZQUFZLEVBQUUsUUFBUSxFQUFFLGdCQUFnQixtQkFJMUY7SUEwQlksZUFBZSxDQUFDLFlBQVksRUFBRSxNQUFNLEVBQUUsSUFBSSxFQUFFLEdBQUcsRUFBRSxFQUFFLEVBQUUsRUFBRSxZQUFZLEdBQUcsT0FBTyxDQUFDLFlBQVksQ0FBQyxDQXlCdkc7Q0FDRiJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contract_store.d.ts","sourceRoot":"","sources":["../../../src/storage/contract_store/contract_store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,
|
|
1
|
+
{"version":3,"file":"contract_store.d.ts","sourceRoot":"","sources":["../../../src/storage/contract_store/contract_store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,EAAE,EAAE,MAAM,gCAAgC,CAAC;AAEpD,OAAO,EAAE,YAAY,EAAiC,MAAM,6BAA6B,CAAC;AAC1F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,KAAK,EAAE,iBAAiB,EAAiB,MAAM,iBAAiB,CAAC;AACxE,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAEhB,KAAK,gCAAgC,EACrC,YAAY,EACZ,KAAK,qBAAqB,EAC1B,gBAAgB,EAMjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EACL,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,EACxB,KAAK,2BAA2B,EAGjC,MAAM,wBAAwB,CAAC;AAMhC;;;;;GAKG;AACH,qBAAa,6BAA6B;IACxC,SAAgB,OAAO,IAAW;IACl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CACF"}
|
|
@@ -1,8 +1,53 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
1
2
|
import { toArray } from '@aztec/foundation/iterable';
|
|
3
|
+
import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
2
4
|
import { FunctionCall, FunctionSelector, FunctionType, contractArtifactFromBuffer, contractArtifactToBuffer, encodeArguments, getFunctionDebugMetadata } from '@aztec/stdlib/abi';
|
|
3
5
|
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
6
|
import { SerializableContractInstance, getContractClassFromArtifact } from '@aztec/stdlib/contract';
|
|
5
7
|
import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
8
|
+
const VERSION = 1;
|
|
9
|
+
/**
|
|
10
|
+
* All contract class data except the large packedBytecode.
|
|
11
|
+
* The expensive data from the ContractClass is precomputed and stored in this format to avoid redundant hashing.
|
|
12
|
+
* Since we have to store the artifacts anyway, the final ContractClass is reconstructed by combining this data
|
|
13
|
+
* with the packedBytecode obtained from the former. That way we can have quick class lookups without wasted storage.
|
|
14
|
+
*/ export class SerializableContractClassData {
|
|
15
|
+
version = VERSION;
|
|
16
|
+
id;
|
|
17
|
+
artifactHash;
|
|
18
|
+
privateFunctionsRoot;
|
|
19
|
+
publicBytecodeCommitment;
|
|
20
|
+
privateFunctions;
|
|
21
|
+
constructor(data){
|
|
22
|
+
this.id = data.id;
|
|
23
|
+
this.artifactHash = data.artifactHash;
|
|
24
|
+
this.privateFunctionsRoot = data.privateFunctionsRoot;
|
|
25
|
+
this.publicBytecodeCommitment = data.publicBytecodeCommitment;
|
|
26
|
+
this.privateFunctions = data.privateFunctions;
|
|
27
|
+
}
|
|
28
|
+
toBuffer() {
|
|
29
|
+
return serializeToBuffer(numToUInt8(this.version), this.id, this.artifactHash, this.privateFunctionsRoot, this.publicBytecodeCommitment, this.privateFunctions.length, ...this.privateFunctions.map((fn)=>serializeToBuffer(fn.selector, fn.vkHash)));
|
|
30
|
+
}
|
|
31
|
+
static fromBuffer(bufferOrReader) {
|
|
32
|
+
const reader = BufferReader.asReader(bufferOrReader);
|
|
33
|
+
const version = reader.readUInt8();
|
|
34
|
+
if (version !== VERSION) {
|
|
35
|
+
throw new Error(`Unexpected contract class data version ${version}`);
|
|
36
|
+
}
|
|
37
|
+
return new SerializableContractClassData({
|
|
38
|
+
id: reader.readObject(Fr),
|
|
39
|
+
artifactHash: reader.readObject(Fr),
|
|
40
|
+
privateFunctionsRoot: reader.readObject(Fr),
|
|
41
|
+
publicBytecodeCommitment: reader.readObject(Fr),
|
|
42
|
+
privateFunctions: reader.readVector({
|
|
43
|
+
fromBuffer: (r)=>({
|
|
44
|
+
selector: r.readObject(FunctionSelector),
|
|
45
|
+
vkHash: r.readObject(Fr)
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
6
51
|
/**
|
|
7
52
|
* ContractStore serves as a data manager and retriever for Aztec.nr contracts.
|
|
8
53
|
* It provides methods to obtain contract addresses, function ABI, bytecode, and membership witnesses
|
|
@@ -12,24 +57,46 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
12
57
|
*/ export class ContractStore {
|
|
13
58
|
/** Map from contract class id to private function tree. */ // TODO: Update it to be LRU cache so that it doesn't keep all the data all the time.
|
|
14
59
|
#privateFunctionTrees = new Map();
|
|
15
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* In-memory cache of deserialized ContractArtifact objects, keyed by class id string.
|
|
62
|
+
* Avoids repeated LMDB reads + JSON.parse + Zod validation on every oracle call.
|
|
63
|
+
* Artifacts are large but immutable after registration — safe to cache for the lifetime of the store.
|
|
64
|
+
*/ // TODO: Update it to be LRU cache so that it doesn't keep all the data all the time.
|
|
65
|
+
#contractArtifactCache = new Map();
|
|
66
|
+
/** Map from contract address to contract class id (avoids KV round-trip on hot path). */ #contractClassIdMap = new Map();
|
|
16
67
|
#store;
|
|
17
68
|
#contractArtifacts;
|
|
69
|
+
#contractClassData;
|
|
18
70
|
#contractInstances;
|
|
19
71
|
constructor(store){
|
|
20
72
|
this.#store = store;
|
|
21
73
|
this.#contractArtifacts = store.openMap('contract_artifacts');
|
|
74
|
+
this.#contractClassData = store.openMap('contract_classes');
|
|
22
75
|
this.#contractInstances = store.openMap('contracts_instances');
|
|
23
76
|
}
|
|
24
77
|
// Setters
|
|
25
|
-
|
|
26
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Registers a new contract artifact and its corresponding class data.
|
|
80
|
+
* IMPORTANT: This method does not verify that the provided artifact matches the class data or that the class id matches the artifact.
|
|
81
|
+
* It is the caller's responsibility to ensure the consistency and correctness of the provided data.
|
|
82
|
+
* This is done to avoid redundant, expensive contract class computations
|
|
83
|
+
*/ async addContractArtifact(contract, contractClassWithIdAndPreimage) {
|
|
84
|
+
const contractClass = contractClassWithIdAndPreimage ?? await getContractClassFromArtifact(contract);
|
|
85
|
+
const key = contractClass.id.toString();
|
|
86
|
+
if (this.#contractArtifactCache.has(key)) {
|
|
87
|
+
return contractClass.id;
|
|
88
|
+
}
|
|
27
89
|
const privateFunctions = contract.functions.filter((functionArtifact)=>functionArtifact.functionType === FunctionType.PRIVATE);
|
|
28
|
-
const privateSelectors = await Promise.all(privateFunctions.map(async (
|
|
90
|
+
const privateSelectors = await Promise.all(privateFunctions.map(async (fn)=>(await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)).toString()));
|
|
29
91
|
if (privateSelectors.length !== new Set(privateSelectors).size) {
|
|
30
92
|
throw new Error('Repeated function selectors of private functions');
|
|
31
93
|
}
|
|
32
|
-
|
|
94
|
+
this.#contractArtifactCache.set(key, contract);
|
|
95
|
+
await this.#store.transactionAsync(async ()=>{
|
|
96
|
+
await this.#contractArtifacts.set(key, contractArtifactToBuffer(contract));
|
|
97
|
+
await this.#contractClassData.set(key, new SerializableContractClassData(contractClass).toBuffer());
|
|
98
|
+
});
|
|
99
|
+
return contractClass.id;
|
|
33
100
|
}
|
|
34
101
|
async addContractInstance(contract) {
|
|
35
102
|
this.#contractClassIdMap.set(contract.address.toString(), contract.currentContractClassId);
|
|
@@ -37,25 +104,17 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
37
104
|
}
|
|
38
105
|
// Private getters
|
|
39
106
|
async #getContractClassId(contractAddress) {
|
|
40
|
-
|
|
107
|
+
const key = contractAddress.toString();
|
|
108
|
+
if (!this.#contractClassIdMap.has(key)) {
|
|
41
109
|
const instance = await this.getContractInstance(contractAddress);
|
|
42
110
|
if (!instance) {
|
|
43
111
|
return;
|
|
44
112
|
}
|
|
45
|
-
this.#contractClassIdMap.set(
|
|
113
|
+
this.#contractClassIdMap.set(key, instance.currentContractClassId);
|
|
46
114
|
}
|
|
47
|
-
return this.#contractClassIdMap.get(
|
|
115
|
+
return this.#contractClassIdMap.get(key);
|
|
48
116
|
}
|
|
49
|
-
|
|
50
|
-
* Retrieve or create a ContractTree instance based on the provided class id.
|
|
51
|
-
* If an existing tree with the same class id is found in the cache, it will be returned.
|
|
52
|
-
* Otherwise, a new ContractTree instance will be created using the contract data from the database
|
|
53
|
-
* and added to the cache before returning.
|
|
54
|
-
*
|
|
55
|
-
* @param classId - The class id of the contract for which the ContractTree is required.
|
|
56
|
-
* @returns A ContractTree instance associated with the specified contract address.
|
|
57
|
-
* @throws An Error if the contract is not found in the ContractDatabase.
|
|
58
|
-
*/ async #getPrivateFunctionTreeForClassId(classId) {
|
|
117
|
+
async #getPrivateFunctionTreeForClassId(classId) {
|
|
59
118
|
if (!this.#privateFunctionTrees.has(classId.toString())) {
|
|
60
119
|
const artifact = await this.getContractArtifact(classId);
|
|
61
120
|
if (!artifact) {
|
|
@@ -66,9 +125,9 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
66
125
|
}
|
|
67
126
|
return this.#privateFunctionTrees.get(classId.toString());
|
|
68
127
|
}
|
|
69
|
-
async #
|
|
70
|
-
const
|
|
71
|
-
return
|
|
128
|
+
async #getArtifactByAddress(contractAddress) {
|
|
129
|
+
const classId = await this.#getContractClassId(contractAddress);
|
|
130
|
+
return classId && this.getContractArtifact(classId);
|
|
72
131
|
}
|
|
73
132
|
// Public getters
|
|
74
133
|
getContractsAddresses() {
|
|
@@ -77,22 +136,43 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
77
136
|
return keys.map(AztecAddress.fromString);
|
|
78
137
|
});
|
|
79
138
|
}
|
|
80
|
-
/** Returns a contract instance for a given address.
|
|
139
|
+
/** Returns a contract instance for a given address. */ getContractInstance(contractAddress) {
|
|
81
140
|
return this.#store.transactionAsync(async ()=>{
|
|
82
141
|
const contract = await this.#contractInstances.getAsync(contractAddress.toString());
|
|
83
142
|
return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
|
|
84
143
|
});
|
|
85
144
|
}
|
|
86
|
-
getContractArtifact(contractClassId) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return
|
|
145
|
+
/** Returns the raw contract artifact for a given class id. */ async getContractArtifact(contractClassId) {
|
|
146
|
+
const key = contractClassId.toString();
|
|
147
|
+
const cached = this.#contractArtifactCache.get(key);
|
|
148
|
+
if (cached) {
|
|
149
|
+
return cached;
|
|
150
|
+
}
|
|
151
|
+
const artifact = await this.#store.transactionAsync(async ()=>{
|
|
152
|
+
const buf = await this.#contractArtifacts.getAsync(key);
|
|
153
|
+
return buf && contractArtifactFromBuffer(buf);
|
|
91
154
|
});
|
|
155
|
+
if (artifact) {
|
|
156
|
+
this.#contractArtifactCache.set(key, artifact);
|
|
157
|
+
}
|
|
158
|
+
return artifact;
|
|
92
159
|
}
|
|
93
|
-
/** Returns a contract class for a given class id.
|
|
160
|
+
/** Returns a contract class for a given class id. */ async getContractClassWithPreimage(contractClassId) {
|
|
161
|
+
const key = contractClassId.toString();
|
|
162
|
+
const buf = await this.#contractClassData.getAsync(key);
|
|
163
|
+
if (!buf) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
const classData = SerializableContractClassData.fromBuffer(buf);
|
|
94
167
|
const artifact = await this.getContractArtifact(contractClassId);
|
|
95
|
-
|
|
168
|
+
if (!artifact) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
const packedBytecode = artifact.functions.find((f)=>f.name === 'public_dispatch')?.bytecode ?? Buffer.alloc(0);
|
|
172
|
+
return {
|
|
173
|
+
...classData,
|
|
174
|
+
packedBytecode
|
|
175
|
+
};
|
|
96
176
|
}
|
|
97
177
|
async getContract(address) {
|
|
98
178
|
const instance = await this.getContractInstance(address);
|
|
@@ -110,17 +190,18 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
110
190
|
}
|
|
111
191
|
/**
|
|
112
192
|
* Retrieves the artifact of a specified function within a given contract.
|
|
113
|
-
* The function is identified by its selector, which is a unique code generated from the function's signature.
|
|
114
|
-
* Throws an error if the contract address or function selector are invalid or not found.
|
|
115
193
|
*
|
|
116
194
|
* @param contractAddress - The AztecAddress representing the contract containing the function.
|
|
117
195
|
* @param selector - The function selector.
|
|
118
196
|
* @returns The corresponding function's artifact as an object.
|
|
119
197
|
*/ async getFunctionArtifact(contractAddress, selector) {
|
|
120
|
-
const artifact = await this.#
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
198
|
+
const artifact = await this.#getArtifactByAddress(contractAddress);
|
|
199
|
+
if (!artifact) {
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
const fn = await this.#findFunctionArtifactBySelector(artifact, selector);
|
|
203
|
+
return fn && {
|
|
204
|
+
...fn,
|
|
124
205
|
contractName: artifact.name
|
|
125
206
|
};
|
|
126
207
|
}
|
|
@@ -136,40 +217,38 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
136
217
|
};
|
|
137
218
|
}
|
|
138
219
|
async getPublicFunctionArtifact(contractAddress) {
|
|
139
|
-
const artifact = await this.#
|
|
140
|
-
const
|
|
141
|
-
return
|
|
142
|
-
...
|
|
220
|
+
const artifact = await this.#getArtifactByAddress(contractAddress);
|
|
221
|
+
const fn = artifact && artifact.functions.find((f)=>f.functionType === FunctionType.PUBLIC);
|
|
222
|
+
return fn && {
|
|
223
|
+
...fn,
|
|
143
224
|
contractName: artifact.name
|
|
144
225
|
};
|
|
145
226
|
}
|
|
146
227
|
async getFunctionAbi(contractAddress, selector) {
|
|
147
|
-
const artifact = await this.#
|
|
228
|
+
const artifact = await this.#getArtifactByAddress(contractAddress);
|
|
148
229
|
return artifact && await this.#findFunctionAbiBySelector(artifact, selector);
|
|
149
230
|
}
|
|
150
231
|
/**
|
|
151
232
|
* Retrieves the debug metadata of a specified function within a given contract.
|
|
152
|
-
* The function is identified by its selector, which is a unique code generated from the function's signature.
|
|
153
|
-
* Returns undefined if the debug metadata for the given function is not found.
|
|
154
|
-
* Throws if the contract has not been added to the database.
|
|
155
233
|
*
|
|
156
234
|
* @param contractAddress - The AztecAddress representing the contract containing the function.
|
|
157
235
|
* @param selector - The function selector.
|
|
158
|
-
* @returns The corresponding function's
|
|
236
|
+
* @returns The corresponding function's debug metadata, or undefined.
|
|
159
237
|
*/ async getFunctionDebugMetadata(contractAddress, selector) {
|
|
160
|
-
const artifact = await this.#
|
|
161
|
-
|
|
162
|
-
|
|
238
|
+
const artifact = await this.#getArtifactByAddress(contractAddress);
|
|
239
|
+
if (!artifact) {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
const fn = await this.#findFunctionArtifactBySelector(artifact, selector);
|
|
243
|
+
return fn && getFunctionDebugMetadata(artifact, fn);
|
|
163
244
|
}
|
|
164
245
|
async getPublicFunctionDebugMetadata(contractAddress) {
|
|
165
|
-
const artifact = await this.#
|
|
166
|
-
const
|
|
167
|
-
return
|
|
246
|
+
const artifact = await this.#getArtifactByAddress(contractAddress);
|
|
247
|
+
const fn = artifact && artifact.functions.find((f)=>f.functionType === FunctionType.PUBLIC);
|
|
248
|
+
return fn && getFunctionDebugMetadata(artifact, fn);
|
|
168
249
|
}
|
|
169
250
|
/**
|
|
170
251
|
* Retrieve the function membership witness for the given contract class and function selector.
|
|
171
|
-
* The function membership witness represents a proof that the function belongs to the specified contract.
|
|
172
|
-
* Throws an error if the contract address or function selector is unknown.
|
|
173
252
|
*
|
|
174
253
|
* @param contractClassId - The id of the class.
|
|
175
254
|
* @param selector - The function selector.
|
|
@@ -179,18 +258,16 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
179
258
|
return tree?.getFunctionMembershipWitness(selector);
|
|
180
259
|
}
|
|
181
260
|
async getDebugContractName(contractAddress) {
|
|
182
|
-
const artifact = await this.#
|
|
261
|
+
const artifact = await this.#getArtifactByAddress(contractAddress);
|
|
183
262
|
return artifact?.name;
|
|
184
263
|
}
|
|
185
264
|
async getDebugFunctionName(contractAddress, selector) {
|
|
186
|
-
const artifact = await this.#
|
|
187
|
-
const
|
|
188
|
-
return `${artifact?.name ?? contractAddress}:${
|
|
265
|
+
const artifact = await this.#getArtifactByAddress(contractAddress);
|
|
266
|
+
const fn = artifact && await this.#findFunctionAbiBySelector(artifact, selector);
|
|
267
|
+
return `${artifact?.name ?? contractAddress}:${fn?.name ?? selector}`;
|
|
189
268
|
}
|
|
190
269
|
async #findFunctionArtifactBySelector(artifact, selector) {
|
|
191
|
-
const
|
|
192
|
-
for(let i = 0; i < functions.length; i++){
|
|
193
|
-
const fn = functions[i];
|
|
270
|
+
for (const fn of artifact.functions){
|
|
194
271
|
const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
|
|
195
272
|
if (fnSelector.equals(selector)) {
|
|
196
273
|
return fn;
|
|
@@ -198,12 +275,10 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
198
275
|
}
|
|
199
276
|
}
|
|
200
277
|
async #findFunctionAbiBySelector(artifact, selector) {
|
|
201
|
-
const
|
|
278
|
+
for (const fn of [
|
|
202
279
|
...artifact.functions,
|
|
203
280
|
...artifact.nonDispatchPublicFunctions ?? []
|
|
204
|
-
]
|
|
205
|
-
for(let i = 0; i < functions.length; i++){
|
|
206
|
-
const fn = functions[i];
|
|
281
|
+
]){
|
|
207
282
|
const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
|
|
208
283
|
if (fnSelector.equals(selector)) {
|
|
209
284
|
return fn;
|
|
@@ -219,10 +294,11 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
|
|
|
219
294
|
if (!functionDao) {
|
|
220
295
|
throw new Error(`Unknown function ${functionName} in contract ${contract.name}.`);
|
|
221
296
|
}
|
|
297
|
+
const selector = await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters);
|
|
222
298
|
return FunctionCall.from({
|
|
223
299
|
name: functionDao.name,
|
|
224
300
|
to,
|
|
225
|
-
selector
|
|
301
|
+
selector,
|
|
226
302
|
type: functionDao.functionType,
|
|
227
303
|
hideMsgSender: false,
|
|
228
304
|
isStatic: functionDao.isStatic,
|